1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * Copyright 2008-2010 Cisco Systems, Inc. All rights reserved. 4 * Copyright 2007 Nuova Systems, Inc. All rights reserved. 5 */ 6 7 #include <linux/kernel.h> 8 #include <linux/errno.h> 9 #include <linux/types.h> 10 #include <linux/pci.h> 11 #include <linux/delay.h> 12 #include <linux/if_ether.h> 13 14 #include "vnic_resource.h" 15 #include "vnic_devcmd.h" 16 #include "vnic_dev.h" 17 #include "vnic_wq.h" 18 #include "vnic_stats.h" 19 #include "enic.h" 20 21 #define VNIC_MAX_RES_HDR_SIZE \ 22 (sizeof(struct vnic_resource_header) + \ 23 sizeof(struct vnic_resource) * RES_TYPE_MAX) 24 #define VNIC_RES_STRIDE 128 25 26 void *vnic_dev_priv(struct vnic_dev *vdev) 27 { 28 return vdev->priv; 29 } 30 31 static int vnic_dev_discover_res(struct vnic_dev *vdev, 32 struct vnic_dev_bar *bar, unsigned int num_bars) 33 { 34 struct vnic_resource_header __iomem *rh; 35 struct mgmt_barmap_hdr __iomem *mrh; 36 struct vnic_resource __iomem *r; 37 u8 type; 38 39 if (num_bars == 0) 40 return -EINVAL; 41 42 if (bar->len < VNIC_MAX_RES_HDR_SIZE) { 43 vdev_err(vdev, "vNIC BAR0 res hdr length error\n"); 44 return -EINVAL; 45 } 46 47 rh = bar->vaddr; 48 mrh = bar->vaddr; 49 if (!rh) { 50 vdev_err(vdev, "vNIC BAR0 res hdr not mem-mapped\n"); 51 return -EINVAL; 52 } 53 54 /* Check for mgmt vnic in addition to normal vnic */ 55 if ((ioread32(&rh->magic) != VNIC_RES_MAGIC) || 56 (ioread32(&rh->version) != VNIC_RES_VERSION)) { 57 if ((ioread32(&mrh->magic) != MGMTVNIC_MAGIC) || 58 (ioread32(&mrh->version) != MGMTVNIC_VERSION)) { 59 vdev_err(vdev, "vNIC BAR0 res magic/version error exp (%lx/%lx) or (%lx/%lx), curr (%x/%x)\n", 60 VNIC_RES_MAGIC, VNIC_RES_VERSION, 61 MGMTVNIC_MAGIC, MGMTVNIC_VERSION, 62 ioread32(&rh->magic), ioread32(&rh->version)); 63 return -EINVAL; 64 } 65 } 66 67 if (ioread32(&mrh->magic) == MGMTVNIC_MAGIC) 68 r = (struct vnic_resource __iomem *)(mrh + 1); 69 else 70 r = (struct vnic_resource __iomem *)(rh + 1); 71 72 73 while ((type = ioread8(&r->type)) != RES_TYPE_EOL) { 74 75 u8 bar_num = ioread8(&r->bar); 76 u32 bar_offset = ioread32(&r->bar_offset); 77 u32 count = ioread32(&r->count); 78 u32 len; 79 80 vdev_dbg(vdev, "res type %u bar %u offset 0x%x count %u\n", 81 type, bar_num, bar_offset, count); 82 83 r++; 84 85 if (bar_num >= num_bars) 86 continue; 87 88 if (!bar[bar_num].len || !bar[bar_num].vaddr) 89 continue; 90 91 switch (type) { 92 case RES_TYPE_WQ: 93 case RES_TYPE_RQ: 94 case RES_TYPE_CQ: 95 case RES_TYPE_INTR_CTRL: 96 case RES_TYPE_ADMIN_WQ: 97 case RES_TYPE_ADMIN_RQ: 98 case RES_TYPE_ADMIN_CQ: 99 /* each count is stride bytes long */ 100 len = count * VNIC_RES_STRIDE; 101 if (len + bar_offset > bar[bar_num].len) { 102 vdev_err(vdev, "vNIC BAR0 resource %d out-of-bounds, offset 0x%x + size 0x%x > bar len 0x%lx\n", 103 type, bar_offset, len, 104 bar[bar_num].len); 105 return -EINVAL; 106 } 107 break; 108 case RES_TYPE_INTR_PBA_LEGACY: 109 case RES_TYPE_DEVCMD: 110 case RES_TYPE_DEVCMD2: 111 case RES_TYPE_SRIOV_INTR: 112 len = count; 113 break; 114 default: 115 continue; 116 } 117 118 vdev->res[type].count = count; 119 vdev->res[type].vaddr = (char __iomem *)bar[bar_num].vaddr + 120 bar_offset; 121 vdev->res[type].bus_addr = bar[bar_num].bus_addr + bar_offset; 122 } 123 124 return 0; 125 } 126 127 unsigned int vnic_dev_get_res_count(struct vnic_dev *vdev, 128 enum vnic_res_type type) 129 { 130 return vdev->res[type].count; 131 } 132 EXPORT_SYMBOL(vnic_dev_get_res_count); 133 134 void __iomem *vnic_dev_get_res(struct vnic_dev *vdev, enum vnic_res_type type, 135 unsigned int index) 136 { 137 if (!vdev->res[type].vaddr) 138 return NULL; 139 140 switch (type) { 141 case RES_TYPE_WQ: 142 case RES_TYPE_RQ: 143 case RES_TYPE_CQ: 144 case RES_TYPE_INTR_CTRL: 145 case RES_TYPE_ADMIN_WQ: 146 case RES_TYPE_ADMIN_RQ: 147 case RES_TYPE_ADMIN_CQ: 148 return (char __iomem *)vdev->res[type].vaddr + 149 index * VNIC_RES_STRIDE; 150 default: 151 return (char __iomem *)vdev->res[type].vaddr; 152 } 153 } 154 EXPORT_SYMBOL(vnic_dev_get_res); 155 156 static unsigned int vnic_dev_desc_ring_size(struct vnic_dev_ring *ring, 157 unsigned int desc_count, unsigned int desc_size) 158 { 159 160 /* Descriptor ring base address alignment in bytes*/ 161 ring->base_align = VNIC_DESC_BASE_ALIGN; 162 163 /* A count of 0 means the maximum descriptors */ 164 if (desc_count == 0) 165 desc_count = VNIC_DESC_MAX_COUNT; 166 167 /* Descriptor count aligned in groups of VNIC_DESC_COUNT_ALIGN descriptors */ 168 ring->desc_count = ALIGN(desc_count, VNIC_DESC_COUNT_ALIGN); 169 170 /* Descriptor size alignment in bytes */ 171 ring->desc_size = ALIGN(desc_size, VNIC_DESC_SIZE_ALIGN); 172 173 ring->size = ring->desc_count * ring->desc_size; 174 ring->size_unaligned = ring->size + ring->base_align; 175 176 return ring->size_unaligned; 177 } 178 179 void vnic_dev_clear_desc_ring(struct vnic_dev_ring *ring) 180 { 181 memset(ring->descs, 0, ring->size); 182 } 183 184 int vnic_dev_alloc_desc_ring(struct vnic_dev *vdev, struct vnic_dev_ring *ring, 185 unsigned int desc_count, unsigned int desc_size) 186 { 187 vnic_dev_desc_ring_size(ring, desc_count, desc_size); 188 189 ring->descs_unaligned = dma_alloc_coherent(&vdev->pdev->dev, 190 ring->size_unaligned, 191 &ring->base_addr_unaligned, 192 GFP_KERNEL); 193 194 if (!ring->descs_unaligned) { 195 vdev_err(vdev, "Failed to allocate ring (size=%d), aborting\n", 196 (int)ring->size); 197 return -ENOMEM; 198 } 199 200 ring->base_addr = ALIGN(ring->base_addr_unaligned, 201 ring->base_align); 202 ring->descs = (u8 *)ring->descs_unaligned + 203 (ring->base_addr - ring->base_addr_unaligned); 204 205 vnic_dev_clear_desc_ring(ring); 206 207 ring->desc_avail = ring->desc_count - 1; 208 209 return 0; 210 } 211 212 void vnic_dev_free_desc_ring(struct vnic_dev *vdev, struct vnic_dev_ring *ring) 213 { 214 if (ring->descs) { 215 dma_free_coherent(&vdev->pdev->dev, ring->size_unaligned, 216 ring->descs_unaligned, 217 ring->base_addr_unaligned); 218 ring->descs = NULL; 219 } 220 } 221 222 static int _vnic_dev_cmd(struct vnic_dev *vdev, enum vnic_devcmd_cmd cmd, 223 int wait) 224 { 225 struct vnic_devcmd __iomem *devcmd = vdev->devcmd; 226 unsigned int i; 227 int delay; 228 u32 status; 229 int err; 230 231 status = ioread32(&devcmd->status); 232 if (status == 0xFFFFFFFF) { 233 /* PCI-e target device is gone */ 234 return -ENODEV; 235 } 236 if (status & STAT_BUSY) { 237 vdev_neterr(vdev, "Busy devcmd %d\n", _CMD_N(cmd)); 238 return -EBUSY; 239 } 240 241 if (_CMD_DIR(cmd) & _CMD_DIR_WRITE) { 242 for (i = 0; i < VNIC_DEVCMD_NARGS; i++) 243 writeq(vdev->args[i], &devcmd->args[i]); 244 wmb(); 245 } 246 247 iowrite32(cmd, &devcmd->cmd); 248 249 if ((_CMD_FLAGS(cmd) & _CMD_FLAGS_NOWAIT)) 250 return 0; 251 252 for (delay = 0; delay < wait; delay++) { 253 254 udelay(100); 255 256 status = ioread32(&devcmd->status); 257 if (status == 0xFFFFFFFF) { 258 /* PCI-e target device is gone */ 259 return -ENODEV; 260 } 261 262 if (!(status & STAT_BUSY)) { 263 264 if (status & STAT_ERROR) { 265 err = (int)readq(&devcmd->args[0]); 266 if (err == ERR_EINVAL && 267 cmd == CMD_CAPABILITY) 268 return -err; 269 if (err != ERR_ECMDUNKNOWN || 270 cmd != CMD_CAPABILITY) 271 vdev_neterr(vdev, "Error %d devcmd %d\n", 272 err, _CMD_N(cmd)); 273 return -err; 274 } 275 276 if (_CMD_DIR(cmd) & _CMD_DIR_READ) { 277 rmb(); 278 for (i = 0; i < VNIC_DEVCMD_NARGS; i++) 279 vdev->args[i] = readq(&devcmd->args[i]); 280 } 281 282 return 0; 283 } 284 } 285 286 vdev_neterr(vdev, "Timedout devcmd %d\n", _CMD_N(cmd)); 287 return -ETIMEDOUT; 288 } 289 290 static int _vnic_dev_cmd2(struct vnic_dev *vdev, enum vnic_devcmd_cmd cmd, 291 int wait) 292 { 293 struct devcmd2_controller *dc2c = vdev->devcmd2; 294 struct devcmd2_result *result; 295 u8 color; 296 unsigned int i; 297 int delay, err; 298 u32 fetch_index, new_posted; 299 u32 posted = dc2c->posted; 300 301 fetch_index = ioread32(&dc2c->wq_ctrl->fetch_index); 302 303 if (fetch_index == 0xFFFFFFFF) 304 return -ENODEV; 305 306 new_posted = (posted + 1) % DEVCMD2_RING_SIZE; 307 308 if (new_posted == fetch_index) { 309 vdev_neterr(vdev, "devcmd2 %d: wq is full. fetch index: %u, posted index: %u\n", 310 _CMD_N(cmd), fetch_index, posted); 311 return -EBUSY; 312 } 313 dc2c->cmd_ring[posted].cmd = cmd; 314 dc2c->cmd_ring[posted].flags = 0; 315 316 if ((_CMD_FLAGS(cmd) & _CMD_FLAGS_NOWAIT)) 317 dc2c->cmd_ring[posted].flags |= DEVCMD2_FNORESULT; 318 if (_CMD_DIR(cmd) & _CMD_DIR_WRITE) 319 for (i = 0; i < VNIC_DEVCMD_NARGS; i++) 320 dc2c->cmd_ring[posted].args[i] = vdev->args[i]; 321 322 /* Adding write memory barrier prevents compiler and/or CPU reordering, 323 * thus avoiding descriptor posting before descriptor is initialized. 324 * Otherwise, hardware can read stale descriptor fields. 325 */ 326 wmb(); 327 iowrite32(new_posted, &dc2c->wq_ctrl->posted_index); 328 dc2c->posted = new_posted; 329 330 if (dc2c->cmd_ring[posted].flags & DEVCMD2_FNORESULT) 331 return 0; 332 333 result = dc2c->result + dc2c->next_result; 334 color = dc2c->color; 335 336 dc2c->next_result++; 337 if (dc2c->next_result == dc2c->result_size) { 338 dc2c->next_result = 0; 339 dc2c->color = dc2c->color ? 0 : 1; 340 } 341 342 for (delay = 0; delay < wait; delay++) { 343 if (result->color == color) { 344 if (result->error) { 345 err = result->error; 346 if (err != ERR_ECMDUNKNOWN || 347 cmd != CMD_CAPABILITY) 348 vdev_neterr(vdev, "Error %d devcmd %d\n", 349 err, _CMD_N(cmd)); 350 return -err; 351 } 352 if (_CMD_DIR(cmd) & _CMD_DIR_READ) 353 for (i = 0; i < VNIC_DEVCMD2_NARGS; i++) 354 vdev->args[i] = result->results[i]; 355 356 return 0; 357 } 358 udelay(100); 359 } 360 361 vdev_neterr(vdev, "devcmd %d timed out\n", _CMD_N(cmd)); 362 363 return -ETIMEDOUT; 364 } 365 366 static int vnic_dev_init_devcmd1(struct vnic_dev *vdev) 367 { 368 vdev->devcmd = vnic_dev_get_res(vdev, RES_TYPE_DEVCMD, 0); 369 if (!vdev->devcmd) 370 return -ENODEV; 371 vdev->devcmd_rtn = _vnic_dev_cmd; 372 373 return 0; 374 } 375 376 static int vnic_dev_init_devcmd2(struct vnic_dev *vdev) 377 { 378 int err; 379 unsigned int fetch_index; 380 381 if (vdev->devcmd2) 382 return 0; 383 384 vdev->devcmd2 = kzalloc_obj(*vdev->devcmd2); 385 if (!vdev->devcmd2) 386 return -ENOMEM; 387 388 vdev->devcmd2->color = 1; 389 vdev->devcmd2->result_size = DEVCMD2_RING_SIZE; 390 err = enic_wq_devcmd2_alloc(vdev, &vdev->devcmd2->wq, DEVCMD2_RING_SIZE, 391 DEVCMD2_DESC_SIZE); 392 if (err) 393 goto err_free_devcmd2; 394 395 fetch_index = ioread32(&vdev->devcmd2->wq.ctrl->fetch_index); 396 if (fetch_index == 0xFFFFFFFF) { /* check for hardware gone */ 397 vdev_err(vdev, "Fatal error in devcmd2 init - hardware surprise removal\n"); 398 err = -ENODEV; 399 goto err_free_wq; 400 } 401 402 enic_wq_init_start(&vdev->devcmd2->wq, 0, fetch_index, fetch_index, 0, 403 0); 404 vdev->devcmd2->posted = fetch_index; 405 vnic_wq_enable(&vdev->devcmd2->wq); 406 407 err = vnic_dev_alloc_desc_ring(vdev, &vdev->devcmd2->results_ring, 408 DEVCMD2_RING_SIZE, DEVCMD2_DESC_SIZE); 409 if (err) 410 goto err_disable_wq; 411 412 vdev->devcmd2->result = vdev->devcmd2->results_ring.descs; 413 vdev->devcmd2->cmd_ring = vdev->devcmd2->wq.ring.descs; 414 vdev->devcmd2->wq_ctrl = vdev->devcmd2->wq.ctrl; 415 vdev->args[0] = (u64)vdev->devcmd2->results_ring.base_addr | 416 VNIC_PADDR_TARGET; 417 vdev->args[1] = DEVCMD2_RING_SIZE; 418 419 err = _vnic_dev_cmd2(vdev, CMD_INITIALIZE_DEVCMD2, 1000); 420 if (err) 421 goto err_free_desc_ring; 422 423 vdev->devcmd_rtn = _vnic_dev_cmd2; 424 425 return 0; 426 427 err_free_desc_ring: 428 vnic_dev_free_desc_ring(vdev, &vdev->devcmd2->results_ring); 429 err_disable_wq: 430 vnic_wq_disable(&vdev->devcmd2->wq); 431 err_free_wq: 432 vnic_wq_free(&vdev->devcmd2->wq); 433 err_free_devcmd2: 434 kfree(vdev->devcmd2); 435 vdev->devcmd2 = NULL; 436 437 return err; 438 } 439 440 static void vnic_dev_deinit_devcmd2(struct vnic_dev *vdev) 441 { 442 vnic_dev_free_desc_ring(vdev, &vdev->devcmd2->results_ring); 443 vnic_wq_disable(&vdev->devcmd2->wq); 444 vnic_wq_free(&vdev->devcmd2->wq); 445 kfree(vdev->devcmd2); 446 } 447 448 static int vnic_dev_cmd_proxy(struct vnic_dev *vdev, 449 enum vnic_devcmd_cmd proxy_cmd, enum vnic_devcmd_cmd cmd, 450 u64 *a0, u64 *a1, int wait) 451 { 452 u32 status; 453 int err; 454 455 memset(vdev->args, 0, sizeof(vdev->args)); 456 457 vdev->args[0] = vdev->proxy_index; 458 vdev->args[1] = cmd; 459 vdev->args[2] = *a0; 460 vdev->args[3] = *a1; 461 462 err = vdev->devcmd_rtn(vdev, proxy_cmd, wait); 463 if (err) 464 return err; 465 466 status = (u32)vdev->args[0]; 467 if (status & STAT_ERROR) { 468 err = (int)vdev->args[1]; 469 if (err != ERR_ECMDUNKNOWN || 470 cmd != CMD_CAPABILITY) 471 vdev_neterr(vdev, "Error %d proxy devcmd %d\n", 472 err, _CMD_N(cmd)); 473 return err; 474 } 475 476 *a0 = vdev->args[1]; 477 *a1 = vdev->args[2]; 478 479 return 0; 480 } 481 482 static int vnic_dev_cmd_no_proxy(struct vnic_dev *vdev, 483 enum vnic_devcmd_cmd cmd, u64 *a0, u64 *a1, int wait) 484 { 485 int err; 486 487 vdev->args[0] = *a0; 488 vdev->args[1] = *a1; 489 490 err = vdev->devcmd_rtn(vdev, cmd, wait); 491 492 *a0 = vdev->args[0]; 493 *a1 = vdev->args[1]; 494 495 return err; 496 } 497 498 void vnic_dev_cmd_proxy_by_index_start(struct vnic_dev *vdev, u16 index) 499 { 500 vdev->proxy = PROXY_BY_INDEX; 501 vdev->proxy_index = index; 502 } 503 504 void vnic_dev_cmd_proxy_end(struct vnic_dev *vdev) 505 { 506 vdev->proxy = PROXY_NONE; 507 vdev->proxy_index = 0; 508 } 509 510 int vnic_dev_cmd(struct vnic_dev *vdev, enum vnic_devcmd_cmd cmd, 511 u64 *a0, u64 *a1, int wait) 512 { 513 memset(vdev->args, 0, sizeof(vdev->args)); 514 515 switch (vdev->proxy) { 516 case PROXY_BY_INDEX: 517 return vnic_dev_cmd_proxy(vdev, CMD_PROXY_BY_INDEX, cmd, 518 a0, a1, wait); 519 case PROXY_BY_BDF: 520 return vnic_dev_cmd_proxy(vdev, CMD_PROXY_BY_BDF, cmd, 521 a0, a1, wait); 522 case PROXY_NONE: 523 default: 524 return vnic_dev_cmd_no_proxy(vdev, cmd, a0, a1, wait); 525 } 526 } 527 528 static int vnic_dev_capable(struct vnic_dev *vdev, enum vnic_devcmd_cmd cmd) 529 { 530 u64 a0 = (u32)cmd, a1 = 0; 531 int wait = 1000; 532 int err; 533 534 err = vnic_dev_cmd(vdev, CMD_CAPABILITY, &a0, &a1, wait); 535 536 return !(err || a0); 537 } 538 539 int vnic_dev_fw_info(struct vnic_dev *vdev, 540 struct vnic_devcmd_fw_info **fw_info) 541 { 542 u64 a0, a1 = 0; 543 int wait = 1000; 544 int err = 0; 545 546 if (!vdev->fw_info) { 547 vdev->fw_info = dma_alloc_coherent(&vdev->pdev->dev, 548 sizeof(struct vnic_devcmd_fw_info), 549 &vdev->fw_info_pa, GFP_ATOMIC); 550 if (!vdev->fw_info) 551 return -ENOMEM; 552 553 a0 = vdev->fw_info_pa; 554 a1 = sizeof(struct vnic_devcmd_fw_info); 555 556 /* only get fw_info once and cache it */ 557 if (vnic_dev_capable(vdev, CMD_MCPU_FW_INFO)) 558 err = vnic_dev_cmd(vdev, CMD_MCPU_FW_INFO, 559 &a0, &a1, wait); 560 else 561 err = vnic_dev_cmd(vdev, CMD_MCPU_FW_INFO_OLD, 562 &a0, &a1, wait); 563 } 564 565 *fw_info = vdev->fw_info; 566 567 return err; 568 } 569 570 int vnic_dev_spec(struct vnic_dev *vdev, unsigned int offset, unsigned int size, 571 void *value) 572 { 573 u64 a0, a1; 574 int wait = 1000; 575 int err; 576 577 a0 = offset; 578 a1 = size; 579 580 err = vnic_dev_cmd(vdev, CMD_DEV_SPEC, &a0, &a1, wait); 581 582 switch (size) { 583 case 1: *(u8 *)value = (u8)a0; break; 584 case 2: *(u16 *)value = (u16)a0; break; 585 case 4: *(u32 *)value = (u32)a0; break; 586 case 8: *(u64 *)value = a0; break; 587 default: BUG(); break; 588 } 589 590 return err; 591 } 592 593 int vnic_dev_stats_dump(struct vnic_dev *vdev, struct vnic_stats **stats) 594 { 595 u64 a0, a1; 596 int wait = 1000; 597 598 if (!vdev->stats) { 599 vdev->stats = dma_alloc_coherent(&vdev->pdev->dev, 600 sizeof(struct vnic_stats), 601 &vdev->stats_pa, GFP_ATOMIC); 602 if (!vdev->stats) 603 return -ENOMEM; 604 } 605 606 *stats = vdev->stats; 607 a0 = vdev->stats_pa; 608 a1 = sizeof(struct vnic_stats); 609 610 return vnic_dev_cmd(vdev, CMD_STATS_DUMP, &a0, &a1, wait); 611 } 612 613 int vnic_dev_close(struct vnic_dev *vdev) 614 { 615 u64 a0 = 0, a1 = 0; 616 int wait = 1000; 617 return vnic_dev_cmd(vdev, CMD_CLOSE, &a0, &a1, wait); 618 } 619 620 int vnic_dev_enable_wait(struct vnic_dev *vdev) 621 { 622 u64 a0 = 0, a1 = 0; 623 int wait = 1000; 624 625 if (vnic_dev_capable(vdev, CMD_ENABLE_WAIT)) 626 return vnic_dev_cmd(vdev, CMD_ENABLE_WAIT, &a0, &a1, wait); 627 else 628 return vnic_dev_cmd(vdev, CMD_ENABLE, &a0, &a1, wait); 629 } 630 631 int vnic_dev_disable(struct vnic_dev *vdev) 632 { 633 u64 a0 = 0, a1 = 0; 634 int wait = 1000; 635 return vnic_dev_cmd(vdev, CMD_DISABLE, &a0, &a1, wait); 636 } 637 638 int vnic_dev_open(struct vnic_dev *vdev, int arg) 639 { 640 u64 a0 = (u32)arg, a1 = 0; 641 int wait = 1000; 642 return vnic_dev_cmd(vdev, CMD_OPEN, &a0, &a1, wait); 643 } 644 645 int vnic_dev_open_done(struct vnic_dev *vdev, int *done) 646 { 647 u64 a0 = 0, a1 = 0; 648 int wait = 1000; 649 int err; 650 651 *done = 0; 652 653 err = vnic_dev_cmd(vdev, CMD_OPEN_STATUS, &a0, &a1, wait); 654 if (err) 655 return err; 656 657 *done = (a0 == 0); 658 659 return 0; 660 } 661 662 int vnic_dev_soft_reset(struct vnic_dev *vdev, int arg) 663 { 664 u64 a0 = (u32)arg, a1 = 0; 665 int wait = 1000; 666 return vnic_dev_cmd(vdev, CMD_SOFT_RESET, &a0, &a1, wait); 667 } 668 669 int vnic_dev_soft_reset_done(struct vnic_dev *vdev, int *done) 670 { 671 u64 a0 = 0, a1 = 0; 672 int wait = 1000; 673 int err; 674 675 *done = 0; 676 677 err = vnic_dev_cmd(vdev, CMD_SOFT_RESET_STATUS, &a0, &a1, wait); 678 if (err) 679 return err; 680 681 *done = (a0 == 0); 682 683 return 0; 684 } 685 686 int vnic_dev_hang_reset(struct vnic_dev *vdev, int arg) 687 { 688 u64 a0 = (u32)arg, a1 = 0; 689 int wait = 1000; 690 int err; 691 692 if (vnic_dev_capable(vdev, CMD_HANG_RESET)) { 693 return vnic_dev_cmd(vdev, CMD_HANG_RESET, 694 &a0, &a1, wait); 695 } else { 696 err = vnic_dev_soft_reset(vdev, arg); 697 if (err) 698 return err; 699 return vnic_dev_init(vdev, 0); 700 } 701 } 702 703 int vnic_dev_hang_reset_done(struct vnic_dev *vdev, int *done) 704 { 705 u64 a0 = 0, a1 = 0; 706 int wait = 1000; 707 int err; 708 709 *done = 0; 710 711 if (vnic_dev_capable(vdev, CMD_HANG_RESET_STATUS)) { 712 err = vnic_dev_cmd(vdev, CMD_HANG_RESET_STATUS, 713 &a0, &a1, wait); 714 if (err) 715 return err; 716 } else { 717 return vnic_dev_soft_reset_done(vdev, done); 718 } 719 720 *done = (a0 == 0); 721 722 return 0; 723 } 724 725 int vnic_dev_hang_notify(struct vnic_dev *vdev) 726 { 727 u64 a0, a1; 728 int wait = 1000; 729 return vnic_dev_cmd(vdev, CMD_HANG_NOTIFY, &a0, &a1, wait); 730 } 731 732 int vnic_dev_get_mac_addr(struct vnic_dev *vdev, u8 *mac_addr) 733 { 734 u64 a0, a1; 735 int wait = 1000; 736 int err, i; 737 738 for (i = 0; i < ETH_ALEN; i++) 739 mac_addr[i] = 0; 740 741 err = vnic_dev_cmd(vdev, CMD_GET_MAC_ADDR, &a0, &a1, wait); 742 if (err) 743 return err; 744 745 for (i = 0; i < ETH_ALEN; i++) 746 mac_addr[i] = ((u8 *)&a0)[i]; 747 748 return 0; 749 } 750 751 int vnic_dev_packet_filter(struct vnic_dev *vdev, int directed, int multicast, 752 int broadcast, int promisc, int allmulti) 753 { 754 u64 a0, a1 = 0; 755 int wait = 1000; 756 int err; 757 758 a0 = (directed ? CMD_PFILTER_DIRECTED : 0) | 759 (multicast ? CMD_PFILTER_MULTICAST : 0) | 760 (broadcast ? CMD_PFILTER_BROADCAST : 0) | 761 (promisc ? CMD_PFILTER_PROMISCUOUS : 0) | 762 (allmulti ? CMD_PFILTER_ALL_MULTICAST : 0); 763 764 err = vnic_dev_cmd(vdev, CMD_PACKET_FILTER, &a0, &a1, wait); 765 if (err) 766 vdev_neterr(vdev, "Can't set packet filter\n"); 767 768 return err; 769 } 770 771 int vnic_dev_add_addr(struct vnic_dev *vdev, const u8 *addr) 772 { 773 u64 a0 = 0, a1 = 0; 774 int wait = 1000; 775 int err; 776 int i; 777 778 for (i = 0; i < ETH_ALEN; i++) 779 ((u8 *)&a0)[i] = addr[i]; 780 781 err = vnic_dev_cmd(vdev, CMD_ADDR_ADD, &a0, &a1, wait); 782 if (err) 783 vdev_neterr(vdev, "Can't add addr [%pM], %d\n", addr, err); 784 785 return err; 786 } 787 788 int vnic_dev_del_addr(struct vnic_dev *vdev, const u8 *addr) 789 { 790 u64 a0 = 0, a1 = 0; 791 int wait = 1000; 792 int err; 793 int i; 794 795 for (i = 0; i < ETH_ALEN; i++) 796 ((u8 *)&a0)[i] = addr[i]; 797 798 err = vnic_dev_cmd(vdev, CMD_ADDR_DEL, &a0, &a1, wait); 799 if (err) 800 vdev_neterr(vdev, "Can't del addr [%pM], %d\n", addr, err); 801 802 return err; 803 } 804 805 int vnic_dev_set_ig_vlan_rewrite_mode(struct vnic_dev *vdev, 806 u8 ig_vlan_rewrite_mode) 807 { 808 u64 a0 = ig_vlan_rewrite_mode, a1 = 0; 809 int wait = 1000; 810 811 if (vnic_dev_capable(vdev, CMD_IG_VLAN_REWRITE_MODE)) 812 return vnic_dev_cmd(vdev, CMD_IG_VLAN_REWRITE_MODE, 813 &a0, &a1, wait); 814 else 815 return 0; 816 } 817 818 static int vnic_dev_notify_setcmd(struct vnic_dev *vdev, 819 void *notify_addr, dma_addr_t notify_pa, u16 intr) 820 { 821 u64 a0, a1; 822 int wait = 1000; 823 int r; 824 825 memset(notify_addr, 0, sizeof(struct vnic_devcmd_notify)); 826 vdev->notify = notify_addr; 827 vdev->notify_pa = notify_pa; 828 829 a0 = (u64)notify_pa; 830 a1 = ((u64)intr << 32) & 0x0000ffff00000000ULL; 831 a1 += sizeof(struct vnic_devcmd_notify); 832 833 r = vnic_dev_cmd(vdev, CMD_NOTIFY, &a0, &a1, wait); 834 vdev->notify_sz = (r == 0) ? (u32)a1 : 0; 835 return r; 836 } 837 838 int vnic_dev_notify_set(struct vnic_dev *vdev, u16 intr) 839 { 840 void *notify_addr; 841 dma_addr_t notify_pa; 842 843 if (vdev->notify || vdev->notify_pa) { 844 vdev_neterr(vdev, "notify block %p still allocated\n", 845 vdev->notify); 846 return -EINVAL; 847 } 848 849 notify_addr = dma_alloc_coherent(&vdev->pdev->dev, 850 sizeof(struct vnic_devcmd_notify), 851 ¬ify_pa, GFP_ATOMIC); 852 if (!notify_addr) 853 return -ENOMEM; 854 855 return vnic_dev_notify_setcmd(vdev, notify_addr, notify_pa, intr); 856 } 857 858 static int vnic_dev_notify_unsetcmd(struct vnic_dev *vdev) 859 { 860 u64 a0, a1; 861 int wait = 1000; 862 int err; 863 864 a0 = 0; /* paddr = 0 to unset notify buffer */ 865 a1 = 0x0000ffff00000000ULL; /* intr num = -1 to unreg for intr */ 866 a1 += sizeof(struct vnic_devcmd_notify); 867 868 err = vnic_dev_cmd(vdev, CMD_NOTIFY, &a0, &a1, wait); 869 vdev->notify = NULL; 870 vdev->notify_pa = 0; 871 vdev->notify_sz = 0; 872 873 return err; 874 } 875 876 int vnic_dev_notify_unset(struct vnic_dev *vdev) 877 { 878 if (vdev->notify) { 879 dma_free_coherent(&vdev->pdev->dev, 880 sizeof(struct vnic_devcmd_notify), 881 vdev->notify, vdev->notify_pa); 882 } 883 884 return vnic_dev_notify_unsetcmd(vdev); 885 } 886 887 static int vnic_dev_notify_ready(struct vnic_dev *vdev) 888 { 889 u32 *words; 890 unsigned int nwords = vdev->notify_sz / 4; 891 unsigned int i; 892 u32 csum; 893 894 if (!vdev->notify || !vdev->notify_sz) 895 return 0; 896 897 do { 898 csum = 0; 899 memcpy(&vdev->notify_copy, vdev->notify, vdev->notify_sz); 900 words = (u32 *)&vdev->notify_copy; 901 for (i = 1; i < nwords; i++) 902 csum += words[i]; 903 } while (csum != words[0]); 904 905 return 1; 906 } 907 908 int vnic_dev_init(struct vnic_dev *vdev, int arg) 909 { 910 u64 a0 = (u32)arg, a1 = 0; 911 int wait = 1000; 912 int r = 0; 913 914 if (vnic_dev_capable(vdev, CMD_INIT)) 915 r = vnic_dev_cmd(vdev, CMD_INIT, &a0, &a1, wait); 916 else { 917 vnic_dev_cmd(vdev, CMD_INIT_v1, &a0, &a1, wait); 918 if (a0 & CMD_INITF_DEFAULT_MAC) { 919 /* Emulate these for old CMD_INIT_v1 which 920 * didn't pass a0 so no CMD_INITF_*. 921 */ 922 vnic_dev_cmd(vdev, CMD_GET_MAC_ADDR, &a0, &a1, wait); 923 vnic_dev_cmd(vdev, CMD_ADDR_ADD, &a0, &a1, wait); 924 } 925 } 926 return r; 927 } 928 929 int vnic_dev_deinit(struct vnic_dev *vdev) 930 { 931 u64 a0 = 0, a1 = 0; 932 int wait = 1000; 933 934 return vnic_dev_cmd(vdev, CMD_DEINIT, &a0, &a1, wait); 935 } 936 937 void vnic_dev_intr_coal_timer_info_default(struct vnic_dev *vdev) 938 { 939 /* Default: hardware intr coal timer is in units of 1.5 usecs */ 940 vdev->intr_coal_timer_info.mul = 2; 941 vdev->intr_coal_timer_info.div = 3; 942 vdev->intr_coal_timer_info.max_usec = 943 vnic_dev_intr_coal_timer_hw_to_usec(vdev, 0xffff); 944 } 945 946 int vnic_dev_intr_coal_timer_info(struct vnic_dev *vdev) 947 { 948 int wait = 1000; 949 int err; 950 951 memset(vdev->args, 0, sizeof(vdev->args)); 952 953 if (vnic_dev_capable(vdev, CMD_INTR_COAL_CONVERT)) 954 err = vdev->devcmd_rtn(vdev, CMD_INTR_COAL_CONVERT, wait); 955 else 956 err = ERR_ECMDUNKNOWN; 957 958 /* Use defaults when firmware doesn't support the devcmd at all or 959 * supports it for only specific hardware 960 */ 961 if ((err == ERR_ECMDUNKNOWN) || 962 (!err && !(vdev->args[0] && vdev->args[1] && vdev->args[2]))) { 963 vdev_netwarn(vdev, "Using default conversion factor for interrupt coalesce timer\n"); 964 vnic_dev_intr_coal_timer_info_default(vdev); 965 return 0; 966 } 967 968 if (!err) { 969 vdev->intr_coal_timer_info.mul = (u32) vdev->args[0]; 970 vdev->intr_coal_timer_info.div = (u32) vdev->args[1]; 971 vdev->intr_coal_timer_info.max_usec = (u32) vdev->args[2]; 972 } 973 974 return err; 975 } 976 977 int vnic_dev_link_status(struct vnic_dev *vdev) 978 { 979 if (!vnic_dev_notify_ready(vdev)) 980 return 0; 981 982 return vdev->notify_copy.link_state; 983 } 984 985 u32 vnic_dev_port_speed(struct vnic_dev *vdev) 986 { 987 if (!vnic_dev_notify_ready(vdev)) 988 return 0; 989 990 return vdev->notify_copy.port_speed; 991 } 992 993 u32 vnic_dev_msg_lvl(struct vnic_dev *vdev) 994 { 995 if (!vnic_dev_notify_ready(vdev)) 996 return 0; 997 998 return vdev->notify_copy.msglvl; 999 } 1000 1001 u32 vnic_dev_mtu(struct vnic_dev *vdev) 1002 { 1003 if (!vnic_dev_notify_ready(vdev)) 1004 return 0; 1005 1006 return vdev->notify_copy.mtu; 1007 } 1008 1009 void vnic_dev_set_intr_mode(struct vnic_dev *vdev, 1010 enum vnic_dev_intr_mode intr_mode) 1011 { 1012 vdev->intr_mode = intr_mode; 1013 } 1014 1015 enum vnic_dev_intr_mode vnic_dev_get_intr_mode( 1016 struct vnic_dev *vdev) 1017 { 1018 return vdev->intr_mode; 1019 } 1020 1021 u32 vnic_dev_intr_coal_timer_usec_to_hw(struct vnic_dev *vdev, u32 usec) 1022 { 1023 return (usec * vdev->intr_coal_timer_info.mul) / 1024 vdev->intr_coal_timer_info.div; 1025 } 1026 1027 u32 vnic_dev_intr_coal_timer_hw_to_usec(struct vnic_dev *vdev, u32 hw_cycles) 1028 { 1029 return (hw_cycles * vdev->intr_coal_timer_info.div) / 1030 vdev->intr_coal_timer_info.mul; 1031 } 1032 1033 u32 vnic_dev_get_intr_coal_timer_max(struct vnic_dev *vdev) 1034 { 1035 return vdev->intr_coal_timer_info.max_usec; 1036 } 1037 1038 void vnic_dev_unregister(struct vnic_dev *vdev) 1039 { 1040 if (vdev) { 1041 if (vdev->notify) 1042 dma_free_coherent(&vdev->pdev->dev, 1043 sizeof(struct vnic_devcmd_notify), 1044 vdev->notify, vdev->notify_pa); 1045 if (vdev->stats) 1046 dma_free_coherent(&vdev->pdev->dev, 1047 sizeof(struct vnic_stats), 1048 vdev->stats, vdev->stats_pa); 1049 if (vdev->fw_info) 1050 dma_free_coherent(&vdev->pdev->dev, 1051 sizeof(struct vnic_devcmd_fw_info), 1052 vdev->fw_info, vdev->fw_info_pa); 1053 if (vdev->devcmd2) 1054 vnic_dev_deinit_devcmd2(vdev); 1055 1056 kfree(vdev); 1057 } 1058 } 1059 EXPORT_SYMBOL(vnic_dev_unregister); 1060 1061 struct vnic_dev *vnic_dev_register(struct vnic_dev *vdev, 1062 void *priv, struct pci_dev *pdev, struct vnic_dev_bar *bar, 1063 unsigned int num_bars) 1064 { 1065 if (!vdev) { 1066 vdev = kzalloc_obj(struct vnic_dev); 1067 if (!vdev) 1068 return NULL; 1069 } 1070 1071 vdev->priv = priv; 1072 vdev->pdev = pdev; 1073 1074 if (vnic_dev_discover_res(vdev, bar, num_bars)) 1075 goto err_out; 1076 1077 return vdev; 1078 1079 err_out: 1080 vnic_dev_unregister(vdev); 1081 return NULL; 1082 } 1083 EXPORT_SYMBOL(vnic_dev_register); 1084 1085 struct pci_dev *vnic_dev_get_pdev(struct vnic_dev *vdev) 1086 { 1087 return vdev->pdev; 1088 } 1089 EXPORT_SYMBOL(vnic_dev_get_pdev); 1090 1091 int vnic_devcmd_init(struct vnic_dev *vdev) 1092 { 1093 void __iomem *res; 1094 int err; 1095 1096 res = vnic_dev_get_res(vdev, RES_TYPE_DEVCMD2, 0); 1097 if (res) { 1098 err = vnic_dev_init_devcmd2(vdev); 1099 if (err) 1100 vdev_warn(vdev, "DEVCMD2 init failed: %d, Using DEVCMD1\n", 1101 err); 1102 else 1103 return 0; 1104 } else { 1105 vdev_warn(vdev, "DEVCMD2 resource not found (old firmware?) Using DEVCMD1\n"); 1106 } 1107 err = vnic_dev_init_devcmd1(vdev); 1108 if (err) 1109 vdev_err(vdev, "DEVCMD1 initialization failed: %d\n", err); 1110 1111 return err; 1112 } 1113 1114 int vnic_dev_init_prov2(struct vnic_dev *vdev, u8 *buf, u32 len) 1115 { 1116 u64 a0, a1 = len; 1117 int wait = 1000; 1118 dma_addr_t prov_pa; 1119 void *prov_buf; 1120 int ret; 1121 1122 prov_buf = dma_alloc_coherent(&vdev->pdev->dev, len, &prov_pa, GFP_ATOMIC); 1123 if (!prov_buf) 1124 return -ENOMEM; 1125 1126 memcpy(prov_buf, buf, len); 1127 1128 a0 = prov_pa; 1129 1130 ret = vnic_dev_cmd(vdev, CMD_INIT_PROV_INFO2, &a0, &a1, wait); 1131 1132 dma_free_coherent(&vdev->pdev->dev, len, prov_buf, prov_pa); 1133 1134 return ret; 1135 } 1136 1137 int vnic_dev_enable2(struct vnic_dev *vdev, int active) 1138 { 1139 u64 a0, a1 = 0; 1140 int wait = 1000; 1141 1142 a0 = (active ? CMD_ENABLE2_ACTIVE : 0); 1143 1144 return vnic_dev_cmd(vdev, CMD_ENABLE2, &a0, &a1, wait); 1145 } 1146 1147 static int vnic_dev_cmd_status(struct vnic_dev *vdev, enum vnic_devcmd_cmd cmd, 1148 int *status) 1149 { 1150 u64 a0 = cmd, a1 = 0; 1151 int wait = 1000; 1152 int ret; 1153 1154 ret = vnic_dev_cmd(vdev, CMD_STATUS, &a0, &a1, wait); 1155 if (!ret) 1156 *status = (int)a0; 1157 1158 return ret; 1159 } 1160 1161 int vnic_dev_enable2_done(struct vnic_dev *vdev, int *status) 1162 { 1163 return vnic_dev_cmd_status(vdev, CMD_ENABLE2, status); 1164 } 1165 1166 int vnic_dev_deinit_done(struct vnic_dev *vdev, int *status) 1167 { 1168 return vnic_dev_cmd_status(vdev, CMD_DEINIT, status); 1169 } 1170 1171 int vnic_dev_set_mac_addr(struct vnic_dev *vdev, u8 *mac_addr) 1172 { 1173 u64 a0, a1; 1174 int wait = 1000; 1175 int i; 1176 1177 for (i = 0; i < ETH_ALEN; i++) 1178 ((u8 *)&a0)[i] = mac_addr[i]; 1179 1180 return vnic_dev_cmd(vdev, CMD_SET_MAC_ADDR, &a0, &a1, wait); 1181 } 1182 1183 /* vnic_dev_classifier: Add/Delete classifier entries 1184 * @vdev: vdev of the device 1185 * @cmd: CLSF_ADD for Add filter 1186 * CLSF_DEL for Delete filter 1187 * @entry: In case of ADD filter, the caller passes the RQ number in this 1188 * variable. 1189 * 1190 * This function stores the filter_id returned by the firmware in the 1191 * same variable before return; 1192 * 1193 * In case of DEL filter, the caller passes the RQ number. Return 1194 * value is irrelevant. 1195 * @data: filter data 1196 */ 1197 int vnic_dev_classifier(struct vnic_dev *vdev, u8 cmd, u16 *entry, 1198 struct filter *data) 1199 { 1200 u64 a0, a1; 1201 int wait = 1000; 1202 dma_addr_t tlv_pa; 1203 int ret = -EINVAL; 1204 struct filter_tlv *tlv, *tlv_va; 1205 struct filter_action *action; 1206 u64 tlv_size; 1207 1208 if (cmd == CLSF_ADD) { 1209 tlv_size = sizeof(struct filter) + 1210 sizeof(struct filter_action) + 1211 2 * sizeof(struct filter_tlv); 1212 tlv_va = dma_alloc_coherent(&vdev->pdev->dev, tlv_size, 1213 &tlv_pa, GFP_ATOMIC); 1214 if (!tlv_va) 1215 return -ENOMEM; 1216 tlv = tlv_va; 1217 a0 = tlv_pa; 1218 a1 = tlv_size; 1219 memset(tlv, 0, tlv_size); 1220 tlv->type = CLSF_TLV_FILTER; 1221 tlv->length = sizeof(struct filter); 1222 *(struct filter *)&tlv->val = *data; 1223 1224 tlv = (struct filter_tlv *)((char *)tlv + 1225 sizeof(struct filter_tlv) + 1226 sizeof(struct filter)); 1227 1228 tlv->type = CLSF_TLV_ACTION; 1229 tlv->length = sizeof(struct filter_action); 1230 action = (struct filter_action *)&tlv->val; 1231 action->type = FILTER_ACTION_RQ_STEERING; 1232 action->u.rq_idx = *entry; 1233 1234 ret = vnic_dev_cmd(vdev, CMD_ADD_FILTER, &a0, &a1, wait); 1235 *entry = (u16)a0; 1236 dma_free_coherent(&vdev->pdev->dev, tlv_size, tlv_va, tlv_pa); 1237 } else if (cmd == CLSF_DEL) { 1238 a0 = *entry; 1239 ret = vnic_dev_cmd(vdev, CMD_DEL_FILTER, &a0, &a1, wait); 1240 } 1241 1242 return ret; 1243 } 1244 1245 int vnic_dev_overlay_offload_ctrl(struct vnic_dev *vdev, u8 overlay, u8 config) 1246 { 1247 u64 a0 = overlay; 1248 u64 a1 = config; 1249 int wait = 1000; 1250 1251 return vnic_dev_cmd(vdev, CMD_OVERLAY_OFFLOAD_CTRL, &a0, &a1, wait); 1252 } 1253 1254 int vnic_dev_overlay_offload_cfg(struct vnic_dev *vdev, u8 overlay, 1255 u16 vxlan_udp_port_number) 1256 { 1257 u64 a1 = vxlan_udp_port_number; 1258 u64 a0 = overlay; 1259 int wait = 1000; 1260 1261 return vnic_dev_cmd(vdev, CMD_OVERLAY_OFFLOAD_CFG, &a0, &a1, wait); 1262 } 1263 1264 int vnic_dev_get_supported_feature_ver(struct vnic_dev *vdev, u8 feature, 1265 u64 *supported_versions, u64 *a1) 1266 { 1267 u64 a0 = feature; 1268 int wait = 1000; 1269 int ret; 1270 1271 ret = vnic_dev_cmd(vdev, CMD_GET_SUPP_FEATURE_VER, &a0, a1, wait); 1272 if (!ret) 1273 *supported_versions = a0; 1274 1275 return ret; 1276 } 1277 1278 int vnic_dev_capable_rss_hash_type(struct vnic_dev *vdev, u8 *rss_hash_type) 1279 { 1280 u64 a0 = CMD_NIC_CFG, a1 = 0; 1281 int wait = 1000; 1282 int err; 1283 1284 err = vnic_dev_cmd(vdev, CMD_CAPABILITY, &a0, &a1, wait); 1285 /* rss_hash_type is valid only when a0 is 1. Adapter which does not 1286 * support CMD_CAPABILITY for rss_hash_type has a0 = 0 1287 */ 1288 if (err || (a0 != 1)) 1289 return -EOPNOTSUPP; 1290 1291 a1 = (a1 >> NIC_CFG_RSS_HASH_TYPE_SHIFT) & 1292 NIC_CFG_RSS_HASH_TYPE_MASK_FIELD; 1293 1294 *rss_hash_type = (u8)a1; 1295 1296 return 0; 1297 } 1298