1 /* 2 * Copyright 2008-2014 Cisco Systems, Inc. All rights reserved. 3 * Copyright 2007 Nuova Systems, Inc. All rights reserved. 4 * 5 * Copyright (c) 2014, Cisco Systems, Inc. 6 * All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 12 * 1. Redistributions of source code must retain the above copyright 13 * notice, this list of conditions and the following disclaimer. 14 * 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in 17 * the documentation and/or other materials provided with the 18 * distribution. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 23 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 24 * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 25 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 26 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 27 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 28 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 30 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 31 * POSSIBILITY OF SUCH DAMAGE. 32 * 33 */ 34 35 #include <rte_memzone.h> 36 #include <rte_memcpy.h> 37 #include <rte_string_fns.h> 38 39 #include "vnic_dev.h" 40 #include "vnic_resource.h" 41 #include "vnic_devcmd.h" 42 #include "vnic_stats.h" 43 44 45 enum vnic_proxy_type { 46 PROXY_NONE, 47 PROXY_BY_BDF, 48 PROXY_BY_INDEX, 49 }; 50 51 struct vnic_res { 52 void __iomem *vaddr; 53 dma_addr_t bus_addr; 54 unsigned int count; 55 }; 56 57 struct vnic_intr_coal_timer_info { 58 u32 mul; 59 u32 div; 60 u32 max_usec; 61 }; 62 63 struct vnic_dev { 64 void *priv; 65 struct rte_pci_device *pdev; 66 struct vnic_res res[RES_TYPE_MAX]; 67 enum vnic_dev_intr_mode intr_mode; 68 struct vnic_devcmd __iomem *devcmd; 69 struct vnic_devcmd_notify *notify; 70 struct vnic_devcmd_notify notify_copy; 71 dma_addr_t notify_pa; 72 u32 notify_sz; 73 dma_addr_t linkstatus_pa; 74 struct vnic_stats *stats; 75 dma_addr_t stats_pa; 76 struct vnic_devcmd_fw_info *fw_info; 77 dma_addr_t fw_info_pa; 78 enum vnic_proxy_type proxy; 79 u32 proxy_index; 80 u64 args[VNIC_DEVCMD_NARGS]; 81 u16 split_hdr_size; 82 int in_reset; 83 struct vnic_intr_coal_timer_info intr_coal_timer_info; 84 void *(*alloc_consistent)(void *priv, size_t size, 85 dma_addr_t *dma_handle, u8 *name); 86 void (*free_consistent)(void *priv, 87 size_t size, void *vaddr, 88 dma_addr_t dma_handle); 89 }; 90 91 #define VNIC_MAX_RES_HDR_SIZE \ 92 (sizeof(struct vnic_resource_header) + \ 93 sizeof(struct vnic_resource) * RES_TYPE_MAX) 94 #define VNIC_RES_STRIDE 128 95 96 void *vnic_dev_priv(struct vnic_dev *vdev) 97 { 98 return vdev->priv; 99 } 100 101 void vnic_register_cbacks(struct vnic_dev *vdev, 102 void *(*alloc_consistent)(void *priv, size_t size, 103 dma_addr_t *dma_handle, u8 *name), 104 void (*free_consistent)(void *priv, 105 size_t size, void *vaddr, 106 dma_addr_t dma_handle)) 107 { 108 vdev->alloc_consistent = alloc_consistent; 109 vdev->free_consistent = free_consistent; 110 } 111 112 static int vnic_dev_discover_res(struct vnic_dev *vdev, 113 struct vnic_dev_bar *bar, unsigned int num_bars) 114 { 115 struct vnic_resource_header __iomem *rh; 116 struct mgmt_barmap_hdr __iomem *mrh; 117 struct vnic_resource __iomem *r; 118 u8 type; 119 120 if (num_bars == 0) 121 return -EINVAL; 122 123 if (bar->len < VNIC_MAX_RES_HDR_SIZE) { 124 pr_err("vNIC BAR0 res hdr length error\n"); 125 return -EINVAL; 126 } 127 128 rh = bar->vaddr; 129 mrh = bar->vaddr; 130 if (!rh) { 131 pr_err("vNIC BAR0 res hdr not mem-mapped\n"); 132 return -EINVAL; 133 } 134 135 /* Check for mgmt vnic in addition to normal vnic */ 136 if ((ioread32(&rh->magic) != VNIC_RES_MAGIC) || 137 (ioread32(&rh->version) != VNIC_RES_VERSION)) { 138 if ((ioread32(&mrh->magic) != MGMTVNIC_MAGIC) || 139 (ioread32(&mrh->version) != MGMTVNIC_VERSION)) { 140 pr_err("vNIC BAR0 res magic/version error " \ 141 "exp (%lx/%lx) or (%lx/%lx), curr (%x/%x)\n", 142 VNIC_RES_MAGIC, VNIC_RES_VERSION, 143 MGMTVNIC_MAGIC, MGMTVNIC_VERSION, 144 ioread32(&rh->magic), ioread32(&rh->version)); 145 return -EINVAL; 146 } 147 } 148 149 if (ioread32(&mrh->magic) == MGMTVNIC_MAGIC) 150 r = (struct vnic_resource __iomem *)(mrh + 1); 151 else 152 r = (struct vnic_resource __iomem *)(rh + 1); 153 154 155 while ((type = ioread8(&r->type)) != RES_TYPE_EOL) { 156 u8 bar_num = ioread8(&r->bar); 157 u32 bar_offset = ioread32(&r->bar_offset); 158 u32 count = ioread32(&r->count); 159 u32 len; 160 161 r++; 162 163 if (bar_num >= num_bars) 164 continue; 165 166 if (!bar[bar_num].len || !bar[bar_num].vaddr) 167 continue; 168 169 switch (type) { 170 case RES_TYPE_WQ: 171 case RES_TYPE_RQ: 172 case RES_TYPE_CQ: 173 case RES_TYPE_INTR_CTRL: 174 /* each count is stride bytes long */ 175 len = count * VNIC_RES_STRIDE; 176 if (len + bar_offset > bar[bar_num].len) { 177 pr_err("vNIC BAR0 resource %d " \ 178 "out-of-bounds, offset 0x%x + " \ 179 "size 0x%x > bar len 0x%lx\n", 180 type, bar_offset, 181 len, 182 bar[bar_num].len); 183 return -EINVAL; 184 } 185 break; 186 case RES_TYPE_INTR_PBA_LEGACY: 187 case RES_TYPE_DEVCMD: 188 len = count; 189 break; 190 default: 191 continue; 192 } 193 194 vdev->res[type].count = count; 195 vdev->res[type].vaddr = (char __iomem *)bar[bar_num].vaddr + 196 bar_offset; 197 vdev->res[type].bus_addr = bar[bar_num].bus_addr + bar_offset; 198 } 199 200 return 0; 201 } 202 203 unsigned int vnic_dev_get_res_count(struct vnic_dev *vdev, 204 enum vnic_res_type type) 205 { 206 return vdev->res[type].count; 207 } 208 209 void __iomem *vnic_dev_get_res(struct vnic_dev *vdev, enum vnic_res_type type, 210 unsigned int index) 211 { 212 if (!vdev->res[type].vaddr) 213 return NULL; 214 215 switch (type) { 216 case RES_TYPE_WQ: 217 case RES_TYPE_RQ: 218 case RES_TYPE_CQ: 219 case RES_TYPE_INTR_CTRL: 220 return (char __iomem *)vdev->res[type].vaddr + 221 index * VNIC_RES_STRIDE; 222 default: 223 return (char __iomem *)vdev->res[type].vaddr; 224 } 225 } 226 227 unsigned int vnic_dev_desc_ring_size(struct vnic_dev_ring *ring, 228 unsigned int desc_count, unsigned int desc_size) 229 { 230 /* The base address of the desc rings must be 512 byte aligned. 231 * Descriptor count is aligned to groups of 32 descriptors. A 232 * count of 0 means the maximum 4096 descriptors. Descriptor 233 * size is aligned to 16 bytes. 234 */ 235 236 unsigned int count_align = 32; 237 unsigned int desc_align = 16; 238 239 ring->base_align = 512; 240 241 if (desc_count == 0) 242 desc_count = 4096; 243 244 ring->desc_count = VNIC_ALIGN(desc_count, count_align); 245 246 ring->desc_size = VNIC_ALIGN(desc_size, desc_align); 247 248 ring->size = ring->desc_count * ring->desc_size; 249 ring->size_unaligned = ring->size + ring->base_align; 250 251 return ring->size_unaligned; 252 } 253 254 void vnic_set_hdr_split_size(struct vnic_dev *vdev, u16 size) 255 { 256 vdev->split_hdr_size = size; 257 } 258 259 u16 vnic_get_hdr_split_size(struct vnic_dev *vdev) 260 { 261 return vdev->split_hdr_size; 262 } 263 264 void vnic_dev_clear_desc_ring(struct vnic_dev_ring *ring) 265 { 266 memset(ring->descs, 0, ring->size); 267 } 268 269 int vnic_dev_alloc_desc_ring(struct vnic_dev *vdev, 270 struct vnic_dev_ring *ring, 271 unsigned int desc_count, unsigned int desc_size, 272 __attribute__((unused)) unsigned int socket_id, 273 char *z_name) 274 { 275 void *alloc_addr = NULL; 276 dma_addr_t alloc_pa = 0; 277 278 vnic_dev_desc_ring_size(ring, desc_count, desc_size); 279 alloc_addr = vdev->alloc_consistent(vdev->priv, 280 ring->size_unaligned, 281 &alloc_pa, (u8 *)z_name); 282 if (!alloc_addr) { 283 pr_err("Failed to allocate ring (size=%d), aborting\n", 284 (int)ring->size); 285 return -ENOMEM; 286 } 287 ring->descs_unaligned = alloc_addr; 288 if (!alloc_pa) { 289 pr_err("Failed to map allocated ring (size=%d), aborting\n", 290 (int)ring->size); 291 vdev->free_consistent(vdev->priv, 292 ring->size_unaligned, 293 alloc_addr, 294 alloc_pa); 295 return -ENOMEM; 296 } 297 ring->base_addr_unaligned = alloc_pa; 298 299 ring->base_addr = VNIC_ALIGN(ring->base_addr_unaligned, 300 ring->base_align); 301 ring->descs = (u8 *)ring->descs_unaligned + 302 (ring->base_addr - ring->base_addr_unaligned); 303 304 vnic_dev_clear_desc_ring(ring); 305 306 ring->desc_avail = ring->desc_count - 1; 307 308 return 0; 309 } 310 311 void vnic_dev_free_desc_ring(__attribute__((unused)) struct vnic_dev *vdev, 312 struct vnic_dev_ring *ring) 313 { 314 if (ring->descs) { 315 vdev->free_consistent(vdev->priv, 316 ring->size_unaligned, 317 ring->descs_unaligned, 318 ring->base_addr_unaligned); 319 ring->descs = NULL; 320 } 321 } 322 323 static int _vnic_dev_cmd(struct vnic_dev *vdev, enum vnic_devcmd_cmd cmd, 324 int wait) 325 { 326 struct vnic_devcmd __iomem *devcmd = vdev->devcmd; 327 unsigned int i; 328 int delay; 329 u32 status; 330 int err; 331 332 status = ioread32(&devcmd->status); 333 if (status == 0xFFFFFFFF) { 334 /* PCI-e target device is gone */ 335 return -ENODEV; 336 } 337 if (status & STAT_BUSY) { 338 339 pr_err("Busy devcmd %d\n", _CMD_N(cmd)); 340 return -EBUSY; 341 } 342 343 if (_CMD_DIR(cmd) & _CMD_DIR_WRITE) { 344 for (i = 0; i < VNIC_DEVCMD_NARGS; i++) 345 writeq(vdev->args[i], &devcmd->args[i]); 346 wmb(); /* complete all writes initiated till now */ 347 } 348 349 iowrite32(cmd, &devcmd->cmd); 350 351 if ((_CMD_FLAGS(cmd) & _CMD_FLAGS_NOWAIT)) 352 return 0; 353 354 for (delay = 0; delay < wait; delay++) { 355 356 udelay(100); 357 358 status = ioread32(&devcmd->status); 359 if (status == 0xFFFFFFFF) { 360 /* PCI-e target device is gone */ 361 return -ENODEV; 362 } 363 364 if (!(status & STAT_BUSY)) { 365 if (status & STAT_ERROR) { 366 err = -(int)readq(&devcmd->args[0]); 367 if (cmd != CMD_CAPABILITY) 368 pr_err("Devcmd %d failed " \ 369 "with error code %d\n", 370 _CMD_N(cmd), err); 371 return err; 372 } 373 374 if (_CMD_DIR(cmd) & _CMD_DIR_READ) { 375 rmb();/* finish all reads initiated till now */ 376 for (i = 0; i < VNIC_DEVCMD_NARGS; i++) 377 vdev->args[i] = readq(&devcmd->args[i]); 378 } 379 380 return 0; 381 } 382 } 383 384 pr_err("Timedout devcmd %d\n", _CMD_N(cmd)); 385 return -ETIMEDOUT; 386 } 387 388 static int vnic_dev_cmd_proxy(struct vnic_dev *vdev, 389 enum vnic_devcmd_cmd proxy_cmd, enum vnic_devcmd_cmd cmd, 390 u64 *a0, u64 *a1, int wait) 391 { 392 u32 status; 393 int err; 394 395 memset(vdev->args, 0, sizeof(vdev->args)); 396 397 vdev->args[0] = vdev->proxy_index; 398 vdev->args[1] = cmd; 399 vdev->args[2] = *a0; 400 vdev->args[3] = *a1; 401 402 err = _vnic_dev_cmd(vdev, proxy_cmd, wait); 403 if (err) 404 return err; 405 406 status = (u32)vdev->args[0]; 407 if (status & STAT_ERROR) { 408 err = (int)vdev->args[1]; 409 if (err != ERR_ECMDUNKNOWN || 410 cmd != CMD_CAPABILITY) 411 pr_err("Error %d proxy devcmd %d\n", err, _CMD_N(cmd)); 412 return err; 413 } 414 415 *a0 = vdev->args[1]; 416 *a1 = vdev->args[2]; 417 418 return 0; 419 } 420 421 static int vnic_dev_cmd_no_proxy(struct vnic_dev *vdev, 422 enum vnic_devcmd_cmd cmd, u64 *a0, u64 *a1, int wait) 423 { 424 int err; 425 426 vdev->args[0] = *a0; 427 vdev->args[1] = *a1; 428 429 err = _vnic_dev_cmd(vdev, cmd, wait); 430 431 *a0 = vdev->args[0]; 432 *a1 = vdev->args[1]; 433 434 return err; 435 } 436 437 void vnic_dev_cmd_proxy_by_index_start(struct vnic_dev *vdev, u16 index) 438 { 439 vdev->proxy = PROXY_BY_INDEX; 440 vdev->proxy_index = index; 441 } 442 443 void vnic_dev_cmd_proxy_by_bdf_start(struct vnic_dev *vdev, u16 bdf) 444 { 445 vdev->proxy = PROXY_BY_BDF; 446 vdev->proxy_index = bdf; 447 } 448 449 void vnic_dev_cmd_proxy_end(struct vnic_dev *vdev) 450 { 451 vdev->proxy = PROXY_NONE; 452 vdev->proxy_index = 0; 453 } 454 455 int vnic_dev_cmd(struct vnic_dev *vdev, enum vnic_devcmd_cmd cmd, 456 u64 *a0, u64 *a1, int wait) 457 { 458 memset(vdev->args, 0, sizeof(vdev->args)); 459 460 switch (vdev->proxy) { 461 case PROXY_BY_INDEX: 462 return vnic_dev_cmd_proxy(vdev, CMD_PROXY_BY_INDEX, cmd, 463 a0, a1, wait); 464 case PROXY_BY_BDF: 465 return vnic_dev_cmd_proxy(vdev, CMD_PROXY_BY_BDF, cmd, 466 a0, a1, wait); 467 case PROXY_NONE: 468 default: 469 return vnic_dev_cmd_no_proxy(vdev, cmd, a0, a1, wait); 470 } 471 } 472 473 static int vnic_dev_capable(struct vnic_dev *vdev, enum vnic_devcmd_cmd cmd) 474 { 475 u64 a0 = (u32)cmd, a1 = 0; 476 int wait = 1000; 477 int err; 478 479 err = vnic_dev_cmd(vdev, CMD_CAPABILITY, &a0, &a1, wait); 480 481 return !(err || a0); 482 } 483 484 int vnic_dev_spec(struct vnic_dev *vdev, unsigned int offset, size_t size, 485 void *value) 486 { 487 u64 a0, a1; 488 int wait = 1000; 489 int err; 490 491 a0 = offset; 492 a1 = size; 493 494 err = vnic_dev_cmd(vdev, CMD_DEV_SPEC, &a0, &a1, wait); 495 496 switch (size) { 497 case 1: 498 *(u8 *)value = (u8)a0; 499 break; 500 case 2: 501 *(u16 *)value = (u16)a0; 502 break; 503 case 4: 504 *(u32 *)value = (u32)a0; 505 break; 506 case 8: 507 *(u64 *)value = a0; 508 break; 509 default: 510 BUG(); 511 break; 512 } 513 514 return err; 515 } 516 517 int vnic_dev_stats_clear(struct vnic_dev *vdev) 518 { 519 u64 a0 = 0, a1 = 0; 520 int wait = 1000; 521 522 return vnic_dev_cmd(vdev, CMD_STATS_CLEAR, &a0, &a1, wait); 523 } 524 525 int vnic_dev_stats_dump(struct vnic_dev *vdev, struct vnic_stats **stats) 526 { 527 u64 a0, a1; 528 int wait = 1000; 529 static u32 instance; 530 char name[NAME_MAX]; 531 532 if (!vdev->stats) { 533 snprintf((char *)name, sizeof(name), 534 "vnic_stats-%d", instance++); 535 vdev->stats = vdev->alloc_consistent(vdev->priv, 536 sizeof(struct vnic_stats), &vdev->stats_pa, (u8 *)name); 537 if (!vdev->stats) 538 return -ENOMEM; 539 } 540 541 *stats = vdev->stats; 542 a0 = vdev->stats_pa; 543 a1 = sizeof(struct vnic_stats); 544 545 return vnic_dev_cmd(vdev, CMD_STATS_DUMP, &a0, &a1, wait); 546 } 547 548 int vnic_dev_close(struct vnic_dev *vdev) 549 { 550 u64 a0 = 0, a1 = 0; 551 int wait = 1000; 552 553 return vnic_dev_cmd(vdev, CMD_CLOSE, &a0, &a1, wait); 554 } 555 556 /** Deprecated. @see vnic_dev_enable_wait */ 557 int vnic_dev_enable(struct vnic_dev *vdev) 558 { 559 u64 a0 = 0, a1 = 0; 560 int wait = 1000; 561 562 return vnic_dev_cmd(vdev, CMD_ENABLE, &a0, &a1, wait); 563 } 564 565 int vnic_dev_enable_wait(struct vnic_dev *vdev) 566 { 567 u64 a0 = 0, a1 = 0; 568 int wait = 1000; 569 570 if (vnic_dev_capable(vdev, CMD_ENABLE_WAIT)) 571 return vnic_dev_cmd(vdev, CMD_ENABLE_WAIT, &a0, &a1, wait); 572 else 573 return vnic_dev_cmd(vdev, CMD_ENABLE, &a0, &a1, wait); 574 } 575 576 int vnic_dev_disable(struct vnic_dev *vdev) 577 { 578 u64 a0 = 0, a1 = 0; 579 int wait = 1000; 580 581 return vnic_dev_cmd(vdev, CMD_DISABLE, &a0, &a1, wait); 582 } 583 584 int vnic_dev_open(struct vnic_dev *vdev, int arg) 585 { 586 u64 a0 = (u32)arg, a1 = 0; 587 int wait = 1000; 588 589 return vnic_dev_cmd(vdev, CMD_OPEN, &a0, &a1, wait); 590 } 591 592 int vnic_dev_open_done(struct vnic_dev *vdev, int *done) 593 { 594 u64 a0 = 0, a1 = 0; 595 int wait = 1000; 596 int err; 597 598 *done = 0; 599 600 err = vnic_dev_cmd(vdev, CMD_OPEN_STATUS, &a0, &a1, wait); 601 if (err) 602 return err; 603 604 *done = (a0 == 0); 605 606 return 0; 607 } 608 609 int vnic_dev_soft_reset(struct vnic_dev *vdev, int arg) 610 { 611 u64 a0 = (u32)arg, a1 = 0; 612 int wait = 1000; 613 614 return vnic_dev_cmd(vdev, CMD_SOFT_RESET, &a0, &a1, wait); 615 } 616 617 int vnic_dev_soft_reset_done(struct vnic_dev *vdev, int *done) 618 { 619 u64 a0 = 0, a1 = 0; 620 int wait = 1000; 621 int err; 622 623 *done = 0; 624 625 err = vnic_dev_cmd(vdev, CMD_SOFT_RESET_STATUS, &a0, &a1, wait); 626 if (err) 627 return err; 628 629 *done = (a0 == 0); 630 631 return 0; 632 } 633 634 int vnic_dev_get_mac_addr(struct vnic_dev *vdev, u8 *mac_addr) 635 { 636 u64 a0, a1 = 0; 637 int wait = 1000; 638 int err, i; 639 640 for (i = 0; i < ETH_ALEN; i++) 641 mac_addr[i] = 0; 642 643 err = vnic_dev_cmd(vdev, CMD_GET_MAC_ADDR, &a0, &a1, wait); 644 if (err) 645 return err; 646 647 for (i = 0; i < ETH_ALEN; i++) 648 mac_addr[i] = ((u8 *)&a0)[i]; 649 650 return 0; 651 } 652 653 int vnic_dev_packet_filter(struct vnic_dev *vdev, int directed, int multicast, 654 int broadcast, int promisc, int allmulti) 655 { 656 u64 a0, a1 = 0; 657 int wait = 1000; 658 int err; 659 660 a0 = (directed ? CMD_PFILTER_DIRECTED : 0) | 661 (multicast ? CMD_PFILTER_MULTICAST : 0) | 662 (broadcast ? CMD_PFILTER_BROADCAST : 0) | 663 (promisc ? CMD_PFILTER_PROMISCUOUS : 0) | 664 (allmulti ? CMD_PFILTER_ALL_MULTICAST : 0); 665 666 err = vnic_dev_cmd(vdev, CMD_PACKET_FILTER, &a0, &a1, wait); 667 if (err) 668 pr_err("Can't set packet filter\n"); 669 670 return err; 671 } 672 673 int vnic_dev_add_addr(struct vnic_dev *vdev, u8 *addr) 674 { 675 u64 a0 = 0, a1 = 0; 676 int wait = 1000; 677 int err; 678 int i; 679 680 for (i = 0; i < ETH_ALEN; i++) 681 ((u8 *)&a0)[i] = addr[i]; 682 683 err = vnic_dev_cmd(vdev, CMD_ADDR_ADD, &a0, &a1, wait); 684 if (err) 685 pr_err("Can't add addr [%02x:%02x:%02x:%02x:%02x:%02x], %d\n", 686 addr[0], addr[1], addr[2], addr[3], addr[4], addr[5], 687 err); 688 689 return err; 690 } 691 692 int vnic_dev_del_addr(struct vnic_dev *vdev, u8 *addr) 693 { 694 u64 a0 = 0, a1 = 0; 695 int wait = 1000; 696 int err; 697 int i; 698 699 for (i = 0; i < ETH_ALEN; i++) 700 ((u8 *)&a0)[i] = addr[i]; 701 702 err = vnic_dev_cmd(vdev, CMD_ADDR_DEL, &a0, &a1, wait); 703 if (err) 704 pr_err("Can't del addr [%02x:%02x:%02x:%02x:%02x:%02x], %d\n", 705 addr[0], addr[1], addr[2], addr[3], addr[4], addr[5], 706 err); 707 708 return err; 709 } 710 711 int vnic_dev_set_ig_vlan_rewrite_mode(struct vnic_dev *vdev, 712 u8 ig_vlan_rewrite_mode) 713 { 714 u64 a0 = ig_vlan_rewrite_mode, a1 = 0; 715 int wait = 1000; 716 717 if (vnic_dev_capable(vdev, CMD_IG_VLAN_REWRITE_MODE)) 718 return vnic_dev_cmd(vdev, CMD_IG_VLAN_REWRITE_MODE, 719 &a0, &a1, wait); 720 else 721 return 0; 722 } 723 724 int vnic_dev_raise_intr(struct vnic_dev *vdev, u16 intr) 725 { 726 u64 a0 = intr, a1 = 0; 727 int wait = 1000; 728 int err; 729 730 err = vnic_dev_cmd(vdev, CMD_IAR, &a0, &a1, wait); 731 if (err) 732 pr_err("Failed to raise INTR[%d], err %d\n", intr, err); 733 734 return err; 735 } 736 737 void vnic_dev_set_reset_flag(struct vnic_dev *vdev, int state) 738 { 739 vdev->in_reset = state; 740 } 741 742 static inline int vnic_dev_in_reset(struct vnic_dev *vdev) 743 { 744 return vdev->in_reset; 745 } 746 747 int vnic_dev_notify_setcmd(struct vnic_dev *vdev, 748 void *notify_addr, dma_addr_t notify_pa, u16 intr) 749 { 750 u64 a0, a1; 751 int wait = 1000; 752 int r; 753 754 memset(notify_addr, 0, sizeof(struct vnic_devcmd_notify)); 755 if (!vnic_dev_in_reset(vdev)) { 756 vdev->notify = notify_addr; 757 vdev->notify_pa = notify_pa; 758 } 759 760 a0 = (u64)notify_pa; 761 a1 = ((u64)intr << 32) & 0x0000ffff00000000ULL; 762 a1 += sizeof(struct vnic_devcmd_notify); 763 764 r = vnic_dev_cmd(vdev, CMD_NOTIFY, &a0, &a1, wait); 765 if (!vnic_dev_in_reset(vdev)) 766 vdev->notify_sz = (r == 0) ? (u32)a1 : 0; 767 768 return r; 769 } 770 771 int vnic_dev_notify_set(struct vnic_dev *vdev, u16 intr) 772 { 773 void *notify_addr = NULL; 774 dma_addr_t notify_pa = 0; 775 char name[NAME_MAX]; 776 static u32 instance; 777 778 if (vdev->notify || vdev->notify_pa) { 779 return vnic_dev_notify_setcmd(vdev, vdev->notify, 780 vdev->notify_pa, intr); 781 } 782 if (!vnic_dev_in_reset(vdev)) { 783 snprintf((char *)name, sizeof(name), 784 "vnic_notify-%d", instance++); 785 notify_addr = vdev->alloc_consistent(vdev->priv, 786 sizeof(struct vnic_devcmd_notify), 787 ¬ify_pa, (u8 *)name); 788 if (!notify_addr) 789 return -ENOMEM; 790 } 791 792 return vnic_dev_notify_setcmd(vdev, notify_addr, notify_pa, intr); 793 } 794 795 int vnic_dev_notify_unsetcmd(struct vnic_dev *vdev) 796 { 797 u64 a0, a1; 798 int wait = 1000; 799 int err; 800 801 a0 = 0; /* paddr = 0 to unset notify buffer */ 802 a1 = 0x0000ffff00000000ULL; /* intr num = -1 to unreg for intr */ 803 a1 += sizeof(struct vnic_devcmd_notify); 804 805 err = vnic_dev_cmd(vdev, CMD_NOTIFY, &a0, &a1, wait); 806 if (!vnic_dev_in_reset(vdev)) { 807 vdev->notify = NULL; 808 vdev->notify_pa = 0; 809 vdev->notify_sz = 0; 810 } 811 812 return err; 813 } 814 815 int vnic_dev_notify_unset(struct vnic_dev *vdev) 816 { 817 if (vdev->notify && !vnic_dev_in_reset(vdev)) { 818 vdev->free_consistent(vdev->priv, 819 sizeof(struct vnic_devcmd_notify), 820 vdev->notify, 821 vdev->notify_pa); 822 } 823 824 return vnic_dev_notify_unsetcmd(vdev); 825 } 826 827 static int vnic_dev_notify_ready(struct vnic_dev *vdev) 828 { 829 u32 *words; 830 unsigned int nwords = vdev->notify_sz / 4; 831 unsigned int i; 832 u32 csum; 833 834 if (!vdev->notify || !vdev->notify_sz) 835 return 0; 836 837 do { 838 csum = 0; 839 rte_memcpy(&vdev->notify_copy, vdev->notify, vdev->notify_sz); 840 words = (u32 *)&vdev->notify_copy; 841 for (i = 1; i < nwords; i++) 842 csum += words[i]; 843 } while (csum != words[0]); 844 845 return 1; 846 } 847 848 int vnic_dev_init(struct vnic_dev *vdev, int arg) 849 { 850 u64 a0 = (u32)arg, a1 = 0; 851 int wait = 1000; 852 int r = 0; 853 854 if (vnic_dev_capable(vdev, CMD_INIT)) 855 r = vnic_dev_cmd(vdev, CMD_INIT, &a0, &a1, wait); 856 else { 857 vnic_dev_cmd(vdev, CMD_INIT_v1, &a0, &a1, wait); 858 if (a0 & CMD_INITF_DEFAULT_MAC) { 859 /* Emulate these for old CMD_INIT_v1 which 860 * didn't pass a0 so no CMD_INITF_*. 861 */ 862 vnic_dev_cmd(vdev, CMD_GET_MAC_ADDR, &a0, &a1, wait); 863 vnic_dev_cmd(vdev, CMD_ADDR_ADD, &a0, &a1, wait); 864 } 865 } 866 return r; 867 } 868 869 int vnic_dev_deinit(struct vnic_dev *vdev) 870 { 871 u64 a0 = 0, a1 = 0; 872 int wait = 1000; 873 874 return vnic_dev_cmd(vdev, CMD_DEINIT, &a0, &a1, wait); 875 } 876 877 void vnic_dev_intr_coal_timer_info_default(struct vnic_dev *vdev) 878 { 879 /* Default: hardware intr coal timer is in units of 1.5 usecs */ 880 vdev->intr_coal_timer_info.mul = 2; 881 vdev->intr_coal_timer_info.div = 3; 882 vdev->intr_coal_timer_info.max_usec = 883 vnic_dev_intr_coal_timer_hw_to_usec(vdev, 0xffff); 884 } 885 886 int vnic_dev_link_status(struct vnic_dev *vdev) 887 { 888 if (!vnic_dev_notify_ready(vdev)) 889 return 0; 890 891 return vdev->notify_copy.link_state; 892 } 893 894 u32 vnic_dev_port_speed(struct vnic_dev *vdev) 895 { 896 if (!vnic_dev_notify_ready(vdev)) 897 return 0; 898 899 return vdev->notify_copy.port_speed; 900 } 901 902 void vnic_dev_set_intr_mode(struct vnic_dev *vdev, 903 enum vnic_dev_intr_mode intr_mode) 904 { 905 vdev->intr_mode = intr_mode; 906 } 907 908 enum vnic_dev_intr_mode vnic_dev_get_intr_mode( 909 struct vnic_dev *vdev) 910 { 911 return vdev->intr_mode; 912 } 913 914 u32 vnic_dev_intr_coal_timer_usec_to_hw(struct vnic_dev *vdev, u32 usec) 915 { 916 return (usec * vdev->intr_coal_timer_info.mul) / 917 vdev->intr_coal_timer_info.div; 918 } 919 920 u32 vnic_dev_intr_coal_timer_hw_to_usec(struct vnic_dev *vdev, u32 hw_cycles) 921 { 922 return (hw_cycles * vdev->intr_coal_timer_info.div) / 923 vdev->intr_coal_timer_info.mul; 924 } 925 926 u32 vnic_dev_get_intr_coal_timer_max(struct vnic_dev *vdev) 927 { 928 return vdev->intr_coal_timer_info.max_usec; 929 } 930 931 void vnic_dev_unregister(struct vnic_dev *vdev) 932 { 933 if (vdev) { 934 if (vdev->notify) 935 vdev->free_consistent(vdev->priv, 936 sizeof(struct vnic_devcmd_notify), 937 vdev->notify, 938 vdev->notify_pa); 939 if (vdev->stats) 940 vdev->free_consistent(vdev->priv, 941 sizeof(struct vnic_stats), 942 vdev->stats, vdev->stats_pa); 943 if (vdev->fw_info) 944 vdev->free_consistent(vdev->priv, 945 sizeof(struct vnic_devcmd_fw_info), 946 vdev->fw_info, vdev->fw_info_pa); 947 kfree(vdev); 948 } 949 } 950 951 struct vnic_dev *vnic_dev_register(struct vnic_dev *vdev, 952 void *priv, struct rte_pci_device *pdev, struct vnic_dev_bar *bar, 953 unsigned int num_bars) 954 { 955 if (!vdev) { 956 vdev = kzalloc(sizeof(struct vnic_dev), GFP_ATOMIC); 957 if (!vdev) 958 return NULL; 959 } 960 961 vdev->priv = priv; 962 vdev->pdev = pdev; 963 964 if (vnic_dev_discover_res(vdev, bar, num_bars)) 965 goto err_out; 966 967 vdev->devcmd = vnic_dev_get_res(vdev, RES_TYPE_DEVCMD, 0); 968 if (!vdev->devcmd) 969 goto err_out; 970 971 return vdev; 972 973 err_out: 974 vnic_dev_unregister(vdev); 975 return NULL; 976 } 977 978 struct rte_pci_device *vnic_dev_get_pdev(struct vnic_dev *vdev) 979 { 980 return vdev->pdev; 981 } 982 983 int vnic_dev_set_mac_addr(struct vnic_dev *vdev, u8 *mac_addr) 984 { 985 u64 a0, a1 = 0; 986 int wait = 1000; 987 int i; 988 989 for (i = 0; i < ETH_ALEN; i++) 990 ((u8 *)&a0)[i] = mac_addr[i]; 991 992 return vnic_dev_cmd(vdev, CMD_SET_MAC_ADDR, &a0, &a1, wait); 993 } 994 995 /* 996 * vnic_dev_classifier: Add/Delete classifier entries 997 * @vdev: vdev of the device 998 * @cmd: CLSF_ADD for Add filter 999 * CLSF_DEL for Delete filter 1000 * @entry: In case of ADD filter, the caller passes the RQ number in this 1001 * variable. 1002 * This function stores the filter_id returned by the 1003 * firmware in the same variable before return; 1004 * 1005 * In case of DEL filter, the caller passes the RQ number. Return 1006 * value is irrelevant. 1007 * @data: filter data 1008 */ 1009 int vnic_dev_classifier(struct vnic_dev *vdev, u8 cmd, u16 *entry, 1010 struct filter *data) 1011 { 1012 u64 a0, a1; 1013 int wait = 1000; 1014 dma_addr_t tlv_pa; 1015 int ret = -EINVAL; 1016 struct filter_tlv *tlv, *tlv_va; 1017 struct filter_action *action; 1018 u64 tlv_size; 1019 static unsigned int unique_id; 1020 char z_name[RTE_MEMZONE_NAMESIZE]; 1021 1022 if (cmd == CLSF_ADD) { 1023 tlv_size = sizeof(struct filter) + 1024 sizeof(struct filter_action) + 1025 2*sizeof(struct filter_tlv); 1026 snprintf((char *)z_name, sizeof(z_name), 1027 "vnic_clsf_%d", unique_id++); 1028 tlv_va = vdev->alloc_consistent(vdev->priv, 1029 tlv_size, &tlv_pa, (u8 *)z_name); 1030 if (!tlv_va) 1031 return -ENOMEM; 1032 tlv = tlv_va; 1033 a0 = tlv_pa; 1034 a1 = tlv_size; 1035 memset(tlv, 0, tlv_size); 1036 tlv->type = CLSF_TLV_FILTER; 1037 tlv->length = sizeof(struct filter); 1038 *(struct filter *)&tlv->val = *data; 1039 1040 tlv = (struct filter_tlv *)((char *)tlv + 1041 sizeof(struct filter_tlv) + 1042 sizeof(struct filter)); 1043 1044 tlv->type = CLSF_TLV_ACTION; 1045 tlv->length = sizeof(struct filter_action); 1046 action = (struct filter_action *)&tlv->val; 1047 action->type = FILTER_ACTION_RQ_STEERING; 1048 action->u.rq_idx = *entry; 1049 1050 ret = vnic_dev_cmd(vdev, CMD_ADD_FILTER, &a0, &a1, wait); 1051 *entry = (u16)a0; 1052 vdev->free_consistent(vdev->priv, tlv_size, tlv_va, tlv_pa); 1053 } else if (cmd == CLSF_DEL) { 1054 a0 = *entry; 1055 ret = vnic_dev_cmd(vdev, CMD_DEL_FILTER, &a0, &a1, wait); 1056 } 1057 1058 return ret; 1059 } 1060