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)(struct rte_pci_device *hwdev, 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)(struct rte_pci_device *hwdev, 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(__attribute__((unused)) struct vnic_dev *vdev, 270 struct vnic_dev_ring *ring, 271 unsigned int desc_count, unsigned int desc_size, unsigned int socket_id, 272 char *z_name) 273 { 274 const struct rte_memzone *rz; 275 276 vnic_dev_desc_ring_size(ring, desc_count, desc_size); 277 278 rz = rte_memzone_reserve_aligned(z_name, 279 ring->size_unaligned, socket_id, 280 0, ENIC_ALIGN); 281 if (!rz) { 282 pr_err("Failed to allocate ring (size=%d), aborting\n", 283 (int)ring->size); 284 return -ENOMEM; 285 } 286 287 ring->descs_unaligned = rz->addr; 288 if (!ring->descs_unaligned) { 289 pr_err("Failed to map allocated ring (size=%d), aborting\n", 290 (int)ring->size); 291 return -ENOMEM; 292 } 293 294 ring->base_addr_unaligned = (dma_addr_t)rz->phys_addr; 295 296 ring->base_addr = VNIC_ALIGN(ring->base_addr_unaligned, 297 ring->base_align); 298 ring->descs = (u8 *)ring->descs_unaligned + 299 (ring->base_addr - ring->base_addr_unaligned); 300 301 vnic_dev_clear_desc_ring(ring); 302 303 ring->desc_avail = ring->desc_count - 1; 304 305 return 0; 306 } 307 308 void vnic_dev_free_desc_ring(__attribute__((unused)) struct vnic_dev *vdev, 309 struct vnic_dev_ring *ring) 310 { 311 if (ring->descs) 312 ring->descs = NULL; 313 } 314 315 static int _vnic_dev_cmd(struct vnic_dev *vdev, enum vnic_devcmd_cmd cmd, 316 int wait) 317 { 318 struct vnic_devcmd __iomem *devcmd = vdev->devcmd; 319 unsigned int i; 320 int delay; 321 u32 status; 322 int err; 323 324 status = ioread32(&devcmd->status); 325 if (status == 0xFFFFFFFF) { 326 /* PCI-e target device is gone */ 327 return -ENODEV; 328 } 329 if (status & STAT_BUSY) { 330 331 pr_err("Busy devcmd %d\n", _CMD_N(cmd)); 332 return -EBUSY; 333 } 334 335 if (_CMD_DIR(cmd) & _CMD_DIR_WRITE) { 336 for (i = 0; i < VNIC_DEVCMD_NARGS; i++) 337 writeq(vdev->args[i], &devcmd->args[i]); 338 wmb(); /* complete all writes initiated till now */ 339 } 340 341 iowrite32(cmd, &devcmd->cmd); 342 343 if ((_CMD_FLAGS(cmd) & _CMD_FLAGS_NOWAIT)) 344 return 0; 345 346 for (delay = 0; delay < wait; delay++) { 347 348 udelay(100); 349 350 status = ioread32(&devcmd->status); 351 if (status == 0xFFFFFFFF) { 352 /* PCI-e target device is gone */ 353 return -ENODEV; 354 } 355 356 if (!(status & STAT_BUSY)) { 357 if (status & STAT_ERROR) { 358 err = -(int)readq(&devcmd->args[0]); 359 if (cmd != CMD_CAPABILITY) 360 pr_err("Devcmd %d failed " \ 361 "with error code %d\n", 362 _CMD_N(cmd), err); 363 return err; 364 } 365 366 if (_CMD_DIR(cmd) & _CMD_DIR_READ) { 367 rmb();/* finish all reads initiated till now */ 368 for (i = 0; i < VNIC_DEVCMD_NARGS; i++) 369 vdev->args[i] = readq(&devcmd->args[i]); 370 } 371 372 return 0; 373 } 374 } 375 376 pr_err("Timedout devcmd %d\n", _CMD_N(cmd)); 377 return -ETIMEDOUT; 378 } 379 380 static int vnic_dev_cmd_proxy(struct vnic_dev *vdev, 381 enum vnic_devcmd_cmd proxy_cmd, enum vnic_devcmd_cmd cmd, 382 u64 *a0, u64 *a1, int wait) 383 { 384 u32 status; 385 int err; 386 387 memset(vdev->args, 0, sizeof(vdev->args)); 388 389 vdev->args[0] = vdev->proxy_index; 390 vdev->args[1] = cmd; 391 vdev->args[2] = *a0; 392 vdev->args[3] = *a1; 393 394 err = _vnic_dev_cmd(vdev, proxy_cmd, wait); 395 if (err) 396 return err; 397 398 status = (u32)vdev->args[0]; 399 if (status & STAT_ERROR) { 400 err = (int)vdev->args[1]; 401 if (err != ERR_ECMDUNKNOWN || 402 cmd != CMD_CAPABILITY) 403 pr_err("Error %d proxy devcmd %d\n", err, _CMD_N(cmd)); 404 return err; 405 } 406 407 *a0 = vdev->args[1]; 408 *a1 = vdev->args[2]; 409 410 return 0; 411 } 412 413 static int vnic_dev_cmd_no_proxy(struct vnic_dev *vdev, 414 enum vnic_devcmd_cmd cmd, u64 *a0, u64 *a1, int wait) 415 { 416 int err; 417 418 vdev->args[0] = *a0; 419 vdev->args[1] = *a1; 420 421 err = _vnic_dev_cmd(vdev, cmd, wait); 422 423 *a0 = vdev->args[0]; 424 *a1 = vdev->args[1]; 425 426 return err; 427 } 428 429 void vnic_dev_cmd_proxy_by_index_start(struct vnic_dev *vdev, u16 index) 430 { 431 vdev->proxy = PROXY_BY_INDEX; 432 vdev->proxy_index = index; 433 } 434 435 void vnic_dev_cmd_proxy_by_bdf_start(struct vnic_dev *vdev, u16 bdf) 436 { 437 vdev->proxy = PROXY_BY_BDF; 438 vdev->proxy_index = bdf; 439 } 440 441 void vnic_dev_cmd_proxy_end(struct vnic_dev *vdev) 442 { 443 vdev->proxy = PROXY_NONE; 444 vdev->proxy_index = 0; 445 } 446 447 int vnic_dev_cmd(struct vnic_dev *vdev, enum vnic_devcmd_cmd cmd, 448 u64 *a0, u64 *a1, int wait) 449 { 450 memset(vdev->args, 0, sizeof(vdev->args)); 451 452 switch (vdev->proxy) { 453 case PROXY_BY_INDEX: 454 return vnic_dev_cmd_proxy(vdev, CMD_PROXY_BY_INDEX, cmd, 455 a0, a1, wait); 456 case PROXY_BY_BDF: 457 return vnic_dev_cmd_proxy(vdev, CMD_PROXY_BY_BDF, cmd, 458 a0, a1, wait); 459 case PROXY_NONE: 460 default: 461 return vnic_dev_cmd_no_proxy(vdev, cmd, a0, a1, wait); 462 } 463 } 464 465 static int vnic_dev_capable(struct vnic_dev *vdev, enum vnic_devcmd_cmd cmd) 466 { 467 u64 a0 = (u32)cmd, a1 = 0; 468 int wait = 1000; 469 int err; 470 471 err = vnic_dev_cmd(vdev, CMD_CAPABILITY, &a0, &a1, wait); 472 473 return !(err || a0); 474 } 475 476 int vnic_dev_spec(struct vnic_dev *vdev, unsigned int offset, size_t size, 477 void *value) 478 { 479 u64 a0, a1; 480 int wait = 1000; 481 int err; 482 483 a0 = offset; 484 a1 = size; 485 486 err = vnic_dev_cmd(vdev, CMD_DEV_SPEC, &a0, &a1, wait); 487 488 switch (size) { 489 case 1: 490 *(u8 *)value = (u8)a0; 491 break; 492 case 2: 493 *(u16 *)value = (u16)a0; 494 break; 495 case 4: 496 *(u32 *)value = (u32)a0; 497 break; 498 case 8: 499 *(u64 *)value = a0; 500 break; 501 default: 502 BUG(); 503 break; 504 } 505 506 return err; 507 } 508 509 int vnic_dev_stats_clear(struct vnic_dev *vdev) 510 { 511 u64 a0 = 0, a1 = 0; 512 int wait = 1000; 513 514 return vnic_dev_cmd(vdev, CMD_STATS_CLEAR, &a0, &a1, wait); 515 } 516 517 int vnic_dev_stats_dump(struct vnic_dev *vdev, struct vnic_stats **stats) 518 { 519 u64 a0, a1; 520 int wait = 1000; 521 static u32 instance; 522 char name[NAME_MAX]; 523 524 if (!vdev->stats) { 525 snprintf((char *)name, sizeof(name), 526 "vnic_stats-%d", instance++); 527 vdev->stats = vdev->alloc_consistent(vdev->priv, 528 sizeof(struct vnic_stats), &vdev->stats_pa, (u8 *)name); 529 if (!vdev->stats) 530 return -ENOMEM; 531 } 532 533 *stats = vdev->stats; 534 a0 = vdev->stats_pa; 535 a1 = sizeof(struct vnic_stats); 536 537 return vnic_dev_cmd(vdev, CMD_STATS_DUMP, &a0, &a1, wait); 538 } 539 540 int vnic_dev_close(struct vnic_dev *vdev) 541 { 542 u64 a0 = 0, a1 = 0; 543 int wait = 1000; 544 545 return vnic_dev_cmd(vdev, CMD_CLOSE, &a0, &a1, wait); 546 } 547 548 /** Deprecated. @see vnic_dev_enable_wait */ 549 int vnic_dev_enable(struct vnic_dev *vdev) 550 { 551 u64 a0 = 0, a1 = 0; 552 int wait = 1000; 553 554 return vnic_dev_cmd(vdev, CMD_ENABLE, &a0, &a1, wait); 555 } 556 557 int vnic_dev_enable_wait(struct vnic_dev *vdev) 558 { 559 u64 a0 = 0, a1 = 0; 560 int wait = 1000; 561 562 if (vnic_dev_capable(vdev, CMD_ENABLE_WAIT)) 563 return vnic_dev_cmd(vdev, CMD_ENABLE_WAIT, &a0, &a1, wait); 564 else 565 return vnic_dev_cmd(vdev, CMD_ENABLE, &a0, &a1, wait); 566 } 567 568 int vnic_dev_disable(struct vnic_dev *vdev) 569 { 570 u64 a0 = 0, a1 = 0; 571 int wait = 1000; 572 573 return vnic_dev_cmd(vdev, CMD_DISABLE, &a0, &a1, wait); 574 } 575 576 int vnic_dev_open(struct vnic_dev *vdev, int arg) 577 { 578 u64 a0 = (u32)arg, a1 = 0; 579 int wait = 1000; 580 581 return vnic_dev_cmd(vdev, CMD_OPEN, &a0, &a1, wait); 582 } 583 584 int vnic_dev_open_done(struct vnic_dev *vdev, int *done) 585 { 586 u64 a0 = 0, a1 = 0; 587 int wait = 1000; 588 int err; 589 590 *done = 0; 591 592 err = vnic_dev_cmd(vdev, CMD_OPEN_STATUS, &a0, &a1, wait); 593 if (err) 594 return err; 595 596 *done = (a0 == 0); 597 598 return 0; 599 } 600 601 int vnic_dev_soft_reset(struct vnic_dev *vdev, int arg) 602 { 603 u64 a0 = (u32)arg, a1 = 0; 604 int wait = 1000; 605 606 return vnic_dev_cmd(vdev, CMD_SOFT_RESET, &a0, &a1, wait); 607 } 608 609 int vnic_dev_soft_reset_done(struct vnic_dev *vdev, int *done) 610 { 611 u64 a0 = 0, a1 = 0; 612 int wait = 1000; 613 int err; 614 615 *done = 0; 616 617 err = vnic_dev_cmd(vdev, CMD_SOFT_RESET_STATUS, &a0, &a1, wait); 618 if (err) 619 return err; 620 621 *done = (a0 == 0); 622 623 return 0; 624 } 625 626 int vnic_dev_get_mac_addr(struct vnic_dev *vdev, u8 *mac_addr) 627 { 628 u64 a0, a1 = 0; 629 int wait = 1000; 630 int err, i; 631 632 for (i = 0; i < ETH_ALEN; i++) 633 mac_addr[i] = 0; 634 635 err = vnic_dev_cmd(vdev, CMD_GET_MAC_ADDR, &a0, &a1, wait); 636 if (err) 637 return err; 638 639 for (i = 0; i < ETH_ALEN; i++) 640 mac_addr[i] = ((u8 *)&a0)[i]; 641 642 return 0; 643 } 644 645 int vnic_dev_packet_filter(struct vnic_dev *vdev, int directed, int multicast, 646 int broadcast, int promisc, int allmulti) 647 { 648 u64 a0, a1 = 0; 649 int wait = 1000; 650 int err; 651 652 a0 = (directed ? CMD_PFILTER_DIRECTED : 0) | 653 (multicast ? CMD_PFILTER_MULTICAST : 0) | 654 (broadcast ? CMD_PFILTER_BROADCAST : 0) | 655 (promisc ? CMD_PFILTER_PROMISCUOUS : 0) | 656 (allmulti ? CMD_PFILTER_ALL_MULTICAST : 0); 657 658 err = vnic_dev_cmd(vdev, CMD_PACKET_FILTER, &a0, &a1, wait); 659 if (err) 660 pr_err("Can't set packet filter\n"); 661 662 return err; 663 } 664 665 int vnic_dev_add_addr(struct vnic_dev *vdev, u8 *addr) 666 { 667 u64 a0 = 0, a1 = 0; 668 int wait = 1000; 669 int err; 670 int i; 671 672 for (i = 0; i < ETH_ALEN; i++) 673 ((u8 *)&a0)[i] = addr[i]; 674 675 err = vnic_dev_cmd(vdev, CMD_ADDR_ADD, &a0, &a1, wait); 676 if (err) 677 pr_err("Can't add addr [%02x:%02x:%02x:%02x:%02x:%02x], %d\n", 678 addr[0], addr[1], addr[2], addr[3], addr[4], addr[5], 679 err); 680 681 return err; 682 } 683 684 int vnic_dev_del_addr(struct vnic_dev *vdev, u8 *addr) 685 { 686 u64 a0 = 0, a1 = 0; 687 int wait = 1000; 688 int err; 689 int i; 690 691 for (i = 0; i < ETH_ALEN; i++) 692 ((u8 *)&a0)[i] = addr[i]; 693 694 err = vnic_dev_cmd(vdev, CMD_ADDR_DEL, &a0, &a1, wait); 695 if (err) 696 pr_err("Can't del addr [%02x:%02x:%02x:%02x:%02x:%02x], %d\n", 697 addr[0], addr[1], addr[2], addr[3], addr[4], addr[5], 698 err); 699 700 return err; 701 } 702 703 int vnic_dev_set_ig_vlan_rewrite_mode(struct vnic_dev *vdev, 704 u8 ig_vlan_rewrite_mode) 705 { 706 u64 a0 = ig_vlan_rewrite_mode, a1 = 0; 707 int wait = 1000; 708 709 if (vnic_dev_capable(vdev, CMD_IG_VLAN_REWRITE_MODE)) 710 return vnic_dev_cmd(vdev, CMD_IG_VLAN_REWRITE_MODE, 711 &a0, &a1, wait); 712 else 713 return 0; 714 } 715 716 int vnic_dev_raise_intr(struct vnic_dev *vdev, u16 intr) 717 { 718 u64 a0 = intr, a1 = 0; 719 int wait = 1000; 720 int err; 721 722 err = vnic_dev_cmd(vdev, CMD_IAR, &a0, &a1, wait); 723 if (err) 724 pr_err("Failed to raise INTR[%d], err %d\n", intr, err); 725 726 return err; 727 } 728 729 void vnic_dev_set_reset_flag(struct vnic_dev *vdev, int state) 730 { 731 vdev->in_reset = state; 732 } 733 734 static inline int vnic_dev_in_reset(struct vnic_dev *vdev) 735 { 736 return vdev->in_reset; 737 } 738 739 int vnic_dev_notify_setcmd(struct vnic_dev *vdev, 740 void *notify_addr, dma_addr_t notify_pa, u16 intr) 741 { 742 u64 a0, a1; 743 int wait = 1000; 744 int r; 745 746 memset(notify_addr, 0, sizeof(struct vnic_devcmd_notify)); 747 if (!vnic_dev_in_reset(vdev)) { 748 vdev->notify = notify_addr; 749 vdev->notify_pa = notify_pa; 750 } 751 752 a0 = (u64)notify_pa; 753 a1 = ((u64)intr << 32) & 0x0000ffff00000000ULL; 754 a1 += sizeof(struct vnic_devcmd_notify); 755 756 r = vnic_dev_cmd(vdev, CMD_NOTIFY, &a0, &a1, wait); 757 if (!vnic_dev_in_reset(vdev)) 758 vdev->notify_sz = (r == 0) ? (u32)a1 : 0; 759 760 return r; 761 } 762 763 int vnic_dev_notify_set(struct vnic_dev *vdev, u16 intr) 764 { 765 void *notify_addr = NULL; 766 dma_addr_t notify_pa = 0; 767 char name[NAME_MAX]; 768 static u32 instance; 769 770 if (vdev->notify || vdev->notify_pa) { 771 return vnic_dev_notify_setcmd(vdev, vdev->notify, 772 vdev->notify_pa, intr); 773 } 774 if (!vnic_dev_in_reset(vdev)) { 775 snprintf((char *)name, sizeof(name), 776 "vnic_notify-%d", instance++); 777 notify_addr = vdev->alloc_consistent(vdev->priv, 778 sizeof(struct vnic_devcmd_notify), 779 ¬ify_pa, (u8 *)name); 780 if (!notify_addr) 781 return -ENOMEM; 782 } 783 784 return vnic_dev_notify_setcmd(vdev, notify_addr, notify_pa, intr); 785 } 786 787 int vnic_dev_notify_unsetcmd(struct vnic_dev *vdev) 788 { 789 u64 a0, a1; 790 int wait = 1000; 791 int err; 792 793 a0 = 0; /* paddr = 0 to unset notify buffer */ 794 a1 = 0x0000ffff00000000ULL; /* intr num = -1 to unreg for intr */ 795 a1 += sizeof(struct vnic_devcmd_notify); 796 797 err = vnic_dev_cmd(vdev, CMD_NOTIFY, &a0, &a1, wait); 798 if (!vnic_dev_in_reset(vdev)) { 799 vdev->notify = NULL; 800 vdev->notify_pa = 0; 801 vdev->notify_sz = 0; 802 } 803 804 return err; 805 } 806 807 int vnic_dev_notify_unset(struct vnic_dev *vdev) 808 { 809 if (vdev->notify && !vnic_dev_in_reset(vdev)) { 810 vdev->free_consistent(vdev->pdev, 811 sizeof(struct vnic_devcmd_notify), 812 vdev->notify, 813 vdev->notify_pa); 814 } 815 816 return vnic_dev_notify_unsetcmd(vdev); 817 } 818 819 static int vnic_dev_notify_ready(struct vnic_dev *vdev) 820 { 821 u32 *words; 822 unsigned int nwords = vdev->notify_sz / 4; 823 unsigned int i; 824 u32 csum; 825 826 if (!vdev->notify || !vdev->notify_sz) 827 return 0; 828 829 do { 830 csum = 0; 831 rte_memcpy(&vdev->notify_copy, vdev->notify, vdev->notify_sz); 832 words = (u32 *)&vdev->notify_copy; 833 for (i = 1; i < nwords; i++) 834 csum += words[i]; 835 } while (csum != words[0]); 836 837 return 1; 838 } 839 840 int vnic_dev_init(struct vnic_dev *vdev, int arg) 841 { 842 u64 a0 = (u32)arg, a1 = 0; 843 int wait = 1000; 844 int r = 0; 845 846 if (vnic_dev_capable(vdev, CMD_INIT)) 847 r = vnic_dev_cmd(vdev, CMD_INIT, &a0, &a1, wait); 848 else { 849 vnic_dev_cmd(vdev, CMD_INIT_v1, &a0, &a1, wait); 850 if (a0 & CMD_INITF_DEFAULT_MAC) { 851 /* Emulate these for old CMD_INIT_v1 which 852 * didn't pass a0 so no CMD_INITF_*. 853 */ 854 vnic_dev_cmd(vdev, CMD_GET_MAC_ADDR, &a0, &a1, wait); 855 vnic_dev_cmd(vdev, CMD_ADDR_ADD, &a0, &a1, wait); 856 } 857 } 858 return r; 859 } 860 861 int vnic_dev_deinit(struct vnic_dev *vdev) 862 { 863 u64 a0 = 0, a1 = 0; 864 int wait = 1000; 865 866 return vnic_dev_cmd(vdev, CMD_DEINIT, &a0, &a1, wait); 867 } 868 869 void vnic_dev_intr_coal_timer_info_default(struct vnic_dev *vdev) 870 { 871 /* Default: hardware intr coal timer is in units of 1.5 usecs */ 872 vdev->intr_coal_timer_info.mul = 2; 873 vdev->intr_coal_timer_info.div = 3; 874 vdev->intr_coal_timer_info.max_usec = 875 vnic_dev_intr_coal_timer_hw_to_usec(vdev, 0xffff); 876 } 877 878 int vnic_dev_link_status(struct vnic_dev *vdev) 879 { 880 if (!vnic_dev_notify_ready(vdev)) 881 return 0; 882 883 return vdev->notify_copy.link_state; 884 } 885 886 u32 vnic_dev_port_speed(struct vnic_dev *vdev) 887 { 888 if (!vnic_dev_notify_ready(vdev)) 889 return 0; 890 891 return vdev->notify_copy.port_speed; 892 } 893 894 void vnic_dev_set_intr_mode(struct vnic_dev *vdev, 895 enum vnic_dev_intr_mode intr_mode) 896 { 897 vdev->intr_mode = intr_mode; 898 } 899 900 enum vnic_dev_intr_mode vnic_dev_get_intr_mode( 901 struct vnic_dev *vdev) 902 { 903 return vdev->intr_mode; 904 } 905 906 u32 vnic_dev_intr_coal_timer_usec_to_hw(struct vnic_dev *vdev, u32 usec) 907 { 908 return (usec * vdev->intr_coal_timer_info.mul) / 909 vdev->intr_coal_timer_info.div; 910 } 911 912 u32 vnic_dev_intr_coal_timer_hw_to_usec(struct vnic_dev *vdev, u32 hw_cycles) 913 { 914 return (hw_cycles * vdev->intr_coal_timer_info.div) / 915 vdev->intr_coal_timer_info.mul; 916 } 917 918 u32 vnic_dev_get_intr_coal_timer_max(struct vnic_dev *vdev) 919 { 920 return vdev->intr_coal_timer_info.max_usec; 921 } 922 923 void vnic_dev_unregister(struct vnic_dev *vdev) 924 { 925 if (vdev) { 926 if (vdev->notify) 927 vdev->free_consistent(vdev->pdev, 928 sizeof(struct vnic_devcmd_notify), 929 vdev->notify, 930 vdev->notify_pa); 931 if (vdev->stats) 932 vdev->free_consistent(vdev->pdev, 933 sizeof(struct vnic_stats), 934 vdev->stats, vdev->stats_pa); 935 if (vdev->fw_info) 936 vdev->free_consistent(vdev->pdev, 937 sizeof(struct vnic_devcmd_fw_info), 938 vdev->fw_info, vdev->fw_info_pa); 939 kfree(vdev); 940 } 941 } 942 943 struct vnic_dev *vnic_dev_register(struct vnic_dev *vdev, 944 void *priv, struct rte_pci_device *pdev, struct vnic_dev_bar *bar, 945 unsigned int num_bars) 946 { 947 if (!vdev) { 948 vdev = kzalloc(sizeof(struct vnic_dev), GFP_ATOMIC); 949 if (!vdev) 950 return NULL; 951 } 952 953 vdev->priv = priv; 954 vdev->pdev = pdev; 955 956 if (vnic_dev_discover_res(vdev, bar, num_bars)) 957 goto err_out; 958 959 vdev->devcmd = vnic_dev_get_res(vdev, RES_TYPE_DEVCMD, 0); 960 if (!vdev->devcmd) 961 goto err_out; 962 963 return vdev; 964 965 err_out: 966 vnic_dev_unregister(vdev); 967 return NULL; 968 } 969 970 struct rte_pci_device *vnic_dev_get_pdev(struct vnic_dev *vdev) 971 { 972 return vdev->pdev; 973 } 974 975 int vnic_dev_set_mac_addr(struct vnic_dev *vdev, u8 *mac_addr) 976 { 977 u64 a0, a1 = 0; 978 int wait = 1000; 979 int i; 980 981 for (i = 0; i < ETH_ALEN; i++) 982 ((u8 *)&a0)[i] = mac_addr[i]; 983 984 return vnic_dev_cmd(vdev, CMD_SET_MAC_ADDR, &a0, &a1, wait); 985 } 986 987 /* 988 * vnic_dev_classifier: Add/Delete classifier entries 989 * @vdev: vdev of the device 990 * @cmd: CLSF_ADD for Add filter 991 * CLSF_DEL for Delete filter 992 * @entry: In case of ADD filter, the caller passes the RQ number in this 993 * variable. 994 * This function stores the filter_id returned by the 995 * firmware in the same variable before return; 996 * 997 * In case of DEL filter, the caller passes the RQ number. Return 998 * value is irrelevant. 999 * @data: filter data 1000 */ 1001 int vnic_dev_classifier(struct vnic_dev *vdev, u8 cmd, u16 *entry, 1002 struct filter *data) 1003 { 1004 u64 a0, a1; 1005 int wait = 1000; 1006 dma_addr_t tlv_pa; 1007 int ret = -EINVAL; 1008 struct filter_tlv *tlv, *tlv_va; 1009 struct filter_action *action; 1010 u64 tlv_size; 1011 static unsigned int unique_id; 1012 char z_name[RTE_MEMZONE_NAMESIZE]; 1013 1014 if (cmd == CLSF_ADD) { 1015 tlv_size = sizeof(struct filter) + 1016 sizeof(struct filter_action) + 1017 2*sizeof(struct filter_tlv); 1018 snprintf((char *)z_name, sizeof(z_name), 1019 "vnic_clsf_%d", unique_id++); 1020 tlv_va = vdev->alloc_consistent(vdev->priv, 1021 tlv_size, &tlv_pa, (u8 *)z_name); 1022 if (!tlv_va) 1023 return -ENOMEM; 1024 tlv = tlv_va; 1025 a0 = tlv_pa; 1026 a1 = tlv_size; 1027 memset(tlv, 0, tlv_size); 1028 tlv->type = CLSF_TLV_FILTER; 1029 tlv->length = sizeof(struct filter); 1030 *(struct filter *)&tlv->val = *data; 1031 1032 tlv = (struct filter_tlv *)((char *)tlv + 1033 sizeof(struct filter_tlv) + 1034 sizeof(struct filter)); 1035 1036 tlv->type = CLSF_TLV_ACTION; 1037 tlv->length = sizeof(struct filter_action); 1038 action = (struct filter_action *)&tlv->val; 1039 action->type = FILTER_ACTION_RQ_STEERING; 1040 action->u.rq_idx = *entry; 1041 1042 ret = vnic_dev_cmd(vdev, CMD_ADD_FILTER, &a0, &a1, wait); 1043 *entry = (u16)a0; 1044 vdev->free_consistent(vdev->pdev, tlv_size, tlv_va, tlv_pa); 1045 } else if (cmd == CLSF_DEL) { 1046 a0 = *entry; 1047 ret = vnic_dev_cmd(vdev, CMD_DEL_FILTER, &a0, &a1, wait); 1048 } 1049 1050 return ret; 1051 } 1052