1 /* 2 * drivers/net/ethernet/rocker/rocker.c - Rocker switch device driver 3 * Copyright (c) 2014-2016 Jiri Pirko <jiri@mellanox.com> 4 * Copyright (c) 2014 Scott Feldman <sfeldma@gmail.com> 5 * 6 * This program is free software; you can redistribute it and/or modify 7 * it under the terms of the GNU General Public License as published by 8 * the Free Software Foundation; either version 2 of the License, or 9 * (at your option) any later version. 10 */ 11 12 #include <linux/kernel.h> 13 #include <linux/module.h> 14 #include <linux/pci.h> 15 #include <linux/interrupt.h> 16 #include <linux/sched.h> 17 #include <linux/wait.h> 18 #include <linux/spinlock.h> 19 #include <linux/sort.h> 20 #include <linux/random.h> 21 #include <linux/netdevice.h> 22 #include <linux/skbuff.h> 23 #include <linux/socket.h> 24 #include <linux/etherdevice.h> 25 #include <linux/ethtool.h> 26 #include <linux/if_ether.h> 27 #include <linux/if_vlan.h> 28 #include <linux/if_bridge.h> 29 #include <linux/bitops.h> 30 #include <linux/ctype.h> 31 #include <net/switchdev.h> 32 #include <net/rtnetlink.h> 33 #include <net/netevent.h> 34 #include <net/arp.h> 35 #include <linux/io-64-nonatomic-lo-hi.h> 36 #include <generated/utsrelease.h> 37 38 #include "rocker_hw.h" 39 #include "rocker.h" 40 #include "rocker_tlv.h" 41 42 static const char rocker_driver_name[] = "rocker"; 43 44 static const struct pci_device_id rocker_pci_id_table[] = { 45 {PCI_VDEVICE(REDHAT, PCI_DEVICE_ID_REDHAT_ROCKER), 0}, 46 {0, } 47 }; 48 49 struct rocker_wait { 50 wait_queue_head_t wait; 51 bool done; 52 bool nowait; 53 }; 54 55 static void rocker_wait_reset(struct rocker_wait *wait) 56 { 57 wait->done = false; 58 wait->nowait = false; 59 } 60 61 static void rocker_wait_init(struct rocker_wait *wait) 62 { 63 init_waitqueue_head(&wait->wait); 64 rocker_wait_reset(wait); 65 } 66 67 static struct rocker_wait *rocker_wait_create(void) 68 { 69 struct rocker_wait *wait; 70 71 wait = kzalloc(sizeof(*wait), GFP_KERNEL); 72 if (!wait) 73 return NULL; 74 return wait; 75 } 76 77 static void rocker_wait_destroy(struct rocker_wait *wait) 78 { 79 kfree(wait); 80 } 81 82 static bool rocker_wait_event_timeout(struct rocker_wait *wait, 83 unsigned long timeout) 84 { 85 wait_event_timeout(wait->wait, wait->done, HZ / 10); 86 if (!wait->done) 87 return false; 88 return true; 89 } 90 91 static void rocker_wait_wake_up(struct rocker_wait *wait) 92 { 93 wait->done = true; 94 wake_up(&wait->wait); 95 } 96 97 static u32 rocker_msix_vector(const struct rocker *rocker, unsigned int vector) 98 { 99 return rocker->msix_entries[vector].vector; 100 } 101 102 static u32 rocker_msix_tx_vector(const struct rocker_port *rocker_port) 103 { 104 return rocker_msix_vector(rocker_port->rocker, 105 ROCKER_MSIX_VEC_TX(rocker_port->port_number)); 106 } 107 108 static u32 rocker_msix_rx_vector(const struct rocker_port *rocker_port) 109 { 110 return rocker_msix_vector(rocker_port->rocker, 111 ROCKER_MSIX_VEC_RX(rocker_port->port_number)); 112 } 113 114 #define rocker_write32(rocker, reg, val) \ 115 writel((val), (rocker)->hw_addr + (ROCKER_ ## reg)) 116 #define rocker_read32(rocker, reg) \ 117 readl((rocker)->hw_addr + (ROCKER_ ## reg)) 118 #define rocker_write64(rocker, reg, val) \ 119 writeq((val), (rocker)->hw_addr + (ROCKER_ ## reg)) 120 #define rocker_read64(rocker, reg) \ 121 readq((rocker)->hw_addr + (ROCKER_ ## reg)) 122 123 /***************************** 124 * HW basic testing functions 125 *****************************/ 126 127 static int rocker_reg_test(const struct rocker *rocker) 128 { 129 const struct pci_dev *pdev = rocker->pdev; 130 u64 test_reg; 131 u64 rnd; 132 133 rnd = prandom_u32(); 134 rnd >>= 1; 135 rocker_write32(rocker, TEST_REG, rnd); 136 test_reg = rocker_read32(rocker, TEST_REG); 137 if (test_reg != rnd * 2) { 138 dev_err(&pdev->dev, "unexpected 32bit register value %08llx, expected %08llx\n", 139 test_reg, rnd * 2); 140 return -EIO; 141 } 142 143 rnd = prandom_u32(); 144 rnd <<= 31; 145 rnd |= prandom_u32(); 146 rocker_write64(rocker, TEST_REG64, rnd); 147 test_reg = rocker_read64(rocker, TEST_REG64); 148 if (test_reg != rnd * 2) { 149 dev_err(&pdev->dev, "unexpected 64bit register value %16llx, expected %16llx\n", 150 test_reg, rnd * 2); 151 return -EIO; 152 } 153 154 return 0; 155 } 156 157 static int rocker_dma_test_one(const struct rocker *rocker, 158 struct rocker_wait *wait, u32 test_type, 159 dma_addr_t dma_handle, const unsigned char *buf, 160 const unsigned char *expect, size_t size) 161 { 162 const struct pci_dev *pdev = rocker->pdev; 163 int i; 164 165 rocker_wait_reset(wait); 166 rocker_write32(rocker, TEST_DMA_CTRL, test_type); 167 168 if (!rocker_wait_event_timeout(wait, HZ / 10)) { 169 dev_err(&pdev->dev, "no interrupt received within a timeout\n"); 170 return -EIO; 171 } 172 173 for (i = 0; i < size; i++) { 174 if (buf[i] != expect[i]) { 175 dev_err(&pdev->dev, "unexpected memory content %02x at byte %x\n, %02x expected", 176 buf[i], i, expect[i]); 177 return -EIO; 178 } 179 } 180 return 0; 181 } 182 183 #define ROCKER_TEST_DMA_BUF_SIZE (PAGE_SIZE * 4) 184 #define ROCKER_TEST_DMA_FILL_PATTERN 0x96 185 186 static int rocker_dma_test_offset(const struct rocker *rocker, 187 struct rocker_wait *wait, int offset) 188 { 189 struct pci_dev *pdev = rocker->pdev; 190 unsigned char *alloc; 191 unsigned char *buf; 192 unsigned char *expect; 193 dma_addr_t dma_handle; 194 int i; 195 int err; 196 197 alloc = kzalloc(ROCKER_TEST_DMA_BUF_SIZE * 2 + offset, 198 GFP_KERNEL | GFP_DMA); 199 if (!alloc) 200 return -ENOMEM; 201 buf = alloc + offset; 202 expect = buf + ROCKER_TEST_DMA_BUF_SIZE; 203 204 dma_handle = pci_map_single(pdev, buf, ROCKER_TEST_DMA_BUF_SIZE, 205 PCI_DMA_BIDIRECTIONAL); 206 if (pci_dma_mapping_error(pdev, dma_handle)) { 207 err = -EIO; 208 goto free_alloc; 209 } 210 211 rocker_write64(rocker, TEST_DMA_ADDR, dma_handle); 212 rocker_write32(rocker, TEST_DMA_SIZE, ROCKER_TEST_DMA_BUF_SIZE); 213 214 memset(expect, ROCKER_TEST_DMA_FILL_PATTERN, ROCKER_TEST_DMA_BUF_SIZE); 215 err = rocker_dma_test_one(rocker, wait, ROCKER_TEST_DMA_CTRL_FILL, 216 dma_handle, buf, expect, 217 ROCKER_TEST_DMA_BUF_SIZE); 218 if (err) 219 goto unmap; 220 221 memset(expect, 0, ROCKER_TEST_DMA_BUF_SIZE); 222 err = rocker_dma_test_one(rocker, wait, ROCKER_TEST_DMA_CTRL_CLEAR, 223 dma_handle, buf, expect, 224 ROCKER_TEST_DMA_BUF_SIZE); 225 if (err) 226 goto unmap; 227 228 prandom_bytes(buf, ROCKER_TEST_DMA_BUF_SIZE); 229 for (i = 0; i < ROCKER_TEST_DMA_BUF_SIZE; i++) 230 expect[i] = ~buf[i]; 231 err = rocker_dma_test_one(rocker, wait, ROCKER_TEST_DMA_CTRL_INVERT, 232 dma_handle, buf, expect, 233 ROCKER_TEST_DMA_BUF_SIZE); 234 if (err) 235 goto unmap; 236 237 unmap: 238 pci_unmap_single(pdev, dma_handle, ROCKER_TEST_DMA_BUF_SIZE, 239 PCI_DMA_BIDIRECTIONAL); 240 free_alloc: 241 kfree(alloc); 242 243 return err; 244 } 245 246 static int rocker_dma_test(const struct rocker *rocker, 247 struct rocker_wait *wait) 248 { 249 int i; 250 int err; 251 252 for (i = 0; i < 8; i++) { 253 err = rocker_dma_test_offset(rocker, wait, i); 254 if (err) 255 return err; 256 } 257 return 0; 258 } 259 260 static irqreturn_t rocker_test_irq_handler(int irq, void *dev_id) 261 { 262 struct rocker_wait *wait = dev_id; 263 264 rocker_wait_wake_up(wait); 265 266 return IRQ_HANDLED; 267 } 268 269 static int rocker_basic_hw_test(const struct rocker *rocker) 270 { 271 const struct pci_dev *pdev = rocker->pdev; 272 struct rocker_wait wait; 273 int err; 274 275 err = rocker_reg_test(rocker); 276 if (err) { 277 dev_err(&pdev->dev, "reg test failed\n"); 278 return err; 279 } 280 281 err = request_irq(rocker_msix_vector(rocker, ROCKER_MSIX_VEC_TEST), 282 rocker_test_irq_handler, 0, 283 rocker_driver_name, &wait); 284 if (err) { 285 dev_err(&pdev->dev, "cannot assign test irq\n"); 286 return err; 287 } 288 289 rocker_wait_init(&wait); 290 rocker_write32(rocker, TEST_IRQ, ROCKER_MSIX_VEC_TEST); 291 292 if (!rocker_wait_event_timeout(&wait, HZ / 10)) { 293 dev_err(&pdev->dev, "no interrupt received within a timeout\n"); 294 err = -EIO; 295 goto free_irq; 296 } 297 298 err = rocker_dma_test(rocker, &wait); 299 if (err) 300 dev_err(&pdev->dev, "dma test failed\n"); 301 302 free_irq: 303 free_irq(rocker_msix_vector(rocker, ROCKER_MSIX_VEC_TEST), &wait); 304 return err; 305 } 306 307 /****************************************** 308 * DMA rings and descriptors manipulations 309 ******************************************/ 310 311 static u32 __pos_inc(u32 pos, size_t limit) 312 { 313 return ++pos == limit ? 0 : pos; 314 } 315 316 static int rocker_desc_err(const struct rocker_desc_info *desc_info) 317 { 318 int err = desc_info->desc->comp_err & ~ROCKER_DMA_DESC_COMP_ERR_GEN; 319 320 switch (err) { 321 case ROCKER_OK: 322 return 0; 323 case -ROCKER_ENOENT: 324 return -ENOENT; 325 case -ROCKER_ENXIO: 326 return -ENXIO; 327 case -ROCKER_ENOMEM: 328 return -ENOMEM; 329 case -ROCKER_EEXIST: 330 return -EEXIST; 331 case -ROCKER_EINVAL: 332 return -EINVAL; 333 case -ROCKER_EMSGSIZE: 334 return -EMSGSIZE; 335 case -ROCKER_ENOTSUP: 336 return -EOPNOTSUPP; 337 case -ROCKER_ENOBUFS: 338 return -ENOBUFS; 339 } 340 341 return -EINVAL; 342 } 343 344 static void rocker_desc_gen_clear(const struct rocker_desc_info *desc_info) 345 { 346 desc_info->desc->comp_err &= ~ROCKER_DMA_DESC_COMP_ERR_GEN; 347 } 348 349 static bool rocker_desc_gen(const struct rocker_desc_info *desc_info) 350 { 351 u32 comp_err = desc_info->desc->comp_err; 352 353 return comp_err & ROCKER_DMA_DESC_COMP_ERR_GEN ? true : false; 354 } 355 356 static void * 357 rocker_desc_cookie_ptr_get(const struct rocker_desc_info *desc_info) 358 { 359 return (void *)(uintptr_t)desc_info->desc->cookie; 360 } 361 362 static void rocker_desc_cookie_ptr_set(const struct rocker_desc_info *desc_info, 363 void *ptr) 364 { 365 desc_info->desc->cookie = (uintptr_t) ptr; 366 } 367 368 static struct rocker_desc_info * 369 rocker_desc_head_get(const struct rocker_dma_ring_info *info) 370 { 371 static struct rocker_desc_info *desc_info; 372 u32 head = __pos_inc(info->head, info->size); 373 374 desc_info = &info->desc_info[info->head]; 375 if (head == info->tail) 376 return NULL; /* ring full */ 377 desc_info->tlv_size = 0; 378 return desc_info; 379 } 380 381 static void rocker_desc_commit(const struct rocker_desc_info *desc_info) 382 { 383 desc_info->desc->buf_size = desc_info->data_size; 384 desc_info->desc->tlv_size = desc_info->tlv_size; 385 } 386 387 static void rocker_desc_head_set(const struct rocker *rocker, 388 struct rocker_dma_ring_info *info, 389 const struct rocker_desc_info *desc_info) 390 { 391 u32 head = __pos_inc(info->head, info->size); 392 393 BUG_ON(head == info->tail); 394 rocker_desc_commit(desc_info); 395 info->head = head; 396 rocker_write32(rocker, DMA_DESC_HEAD(info->type), head); 397 } 398 399 static struct rocker_desc_info * 400 rocker_desc_tail_get(struct rocker_dma_ring_info *info) 401 { 402 static struct rocker_desc_info *desc_info; 403 404 if (info->tail == info->head) 405 return NULL; /* nothing to be done between head and tail */ 406 desc_info = &info->desc_info[info->tail]; 407 if (!rocker_desc_gen(desc_info)) 408 return NULL; /* gen bit not set, desc is not ready yet */ 409 info->tail = __pos_inc(info->tail, info->size); 410 desc_info->tlv_size = desc_info->desc->tlv_size; 411 return desc_info; 412 } 413 414 static void rocker_dma_ring_credits_set(const struct rocker *rocker, 415 const struct rocker_dma_ring_info *info, 416 u32 credits) 417 { 418 if (credits) 419 rocker_write32(rocker, DMA_DESC_CREDITS(info->type), credits); 420 } 421 422 static unsigned long rocker_dma_ring_size_fix(size_t size) 423 { 424 return max(ROCKER_DMA_SIZE_MIN, 425 min(roundup_pow_of_two(size), ROCKER_DMA_SIZE_MAX)); 426 } 427 428 static int rocker_dma_ring_create(const struct rocker *rocker, 429 unsigned int type, 430 size_t size, 431 struct rocker_dma_ring_info *info) 432 { 433 int i; 434 435 BUG_ON(size != rocker_dma_ring_size_fix(size)); 436 info->size = size; 437 info->type = type; 438 info->head = 0; 439 info->tail = 0; 440 info->desc_info = kcalloc(info->size, sizeof(*info->desc_info), 441 GFP_KERNEL); 442 if (!info->desc_info) 443 return -ENOMEM; 444 445 info->desc = pci_alloc_consistent(rocker->pdev, 446 info->size * sizeof(*info->desc), 447 &info->mapaddr); 448 if (!info->desc) { 449 kfree(info->desc_info); 450 return -ENOMEM; 451 } 452 453 for (i = 0; i < info->size; i++) 454 info->desc_info[i].desc = &info->desc[i]; 455 456 rocker_write32(rocker, DMA_DESC_CTRL(info->type), 457 ROCKER_DMA_DESC_CTRL_RESET); 458 rocker_write64(rocker, DMA_DESC_ADDR(info->type), info->mapaddr); 459 rocker_write32(rocker, DMA_DESC_SIZE(info->type), info->size); 460 461 return 0; 462 } 463 464 static void rocker_dma_ring_destroy(const struct rocker *rocker, 465 const struct rocker_dma_ring_info *info) 466 { 467 rocker_write64(rocker, DMA_DESC_ADDR(info->type), 0); 468 469 pci_free_consistent(rocker->pdev, 470 info->size * sizeof(struct rocker_desc), 471 info->desc, info->mapaddr); 472 kfree(info->desc_info); 473 } 474 475 static void rocker_dma_ring_pass_to_producer(const struct rocker *rocker, 476 struct rocker_dma_ring_info *info) 477 { 478 int i; 479 480 BUG_ON(info->head || info->tail); 481 482 /* When ring is consumer, we need to advance head for each desc. 483 * That tells hw that the desc is ready to be used by it. 484 */ 485 for (i = 0; i < info->size - 1; i++) 486 rocker_desc_head_set(rocker, info, &info->desc_info[i]); 487 rocker_desc_commit(&info->desc_info[i]); 488 } 489 490 static int rocker_dma_ring_bufs_alloc(const struct rocker *rocker, 491 const struct rocker_dma_ring_info *info, 492 int direction, size_t buf_size) 493 { 494 struct pci_dev *pdev = rocker->pdev; 495 int i; 496 int err; 497 498 for (i = 0; i < info->size; i++) { 499 struct rocker_desc_info *desc_info = &info->desc_info[i]; 500 struct rocker_desc *desc = &info->desc[i]; 501 dma_addr_t dma_handle; 502 char *buf; 503 504 buf = kzalloc(buf_size, GFP_KERNEL | GFP_DMA); 505 if (!buf) { 506 err = -ENOMEM; 507 goto rollback; 508 } 509 510 dma_handle = pci_map_single(pdev, buf, buf_size, direction); 511 if (pci_dma_mapping_error(pdev, dma_handle)) { 512 kfree(buf); 513 err = -EIO; 514 goto rollback; 515 } 516 517 desc_info->data = buf; 518 desc_info->data_size = buf_size; 519 dma_unmap_addr_set(desc_info, mapaddr, dma_handle); 520 521 desc->buf_addr = dma_handle; 522 desc->buf_size = buf_size; 523 } 524 return 0; 525 526 rollback: 527 for (i--; i >= 0; i--) { 528 const struct rocker_desc_info *desc_info = &info->desc_info[i]; 529 530 pci_unmap_single(pdev, dma_unmap_addr(desc_info, mapaddr), 531 desc_info->data_size, direction); 532 kfree(desc_info->data); 533 } 534 return err; 535 } 536 537 static void rocker_dma_ring_bufs_free(const struct rocker *rocker, 538 const struct rocker_dma_ring_info *info, 539 int direction) 540 { 541 struct pci_dev *pdev = rocker->pdev; 542 int i; 543 544 for (i = 0; i < info->size; i++) { 545 const struct rocker_desc_info *desc_info = &info->desc_info[i]; 546 struct rocker_desc *desc = &info->desc[i]; 547 548 desc->buf_addr = 0; 549 desc->buf_size = 0; 550 pci_unmap_single(pdev, dma_unmap_addr(desc_info, mapaddr), 551 desc_info->data_size, direction); 552 kfree(desc_info->data); 553 } 554 } 555 556 static int rocker_dma_cmd_ring_wait_alloc(struct rocker_desc_info *desc_info) 557 { 558 struct rocker_wait *wait; 559 560 wait = rocker_wait_create(); 561 if (!wait) 562 return -ENOMEM; 563 rocker_desc_cookie_ptr_set(desc_info, wait); 564 return 0; 565 } 566 567 static void 568 rocker_dma_cmd_ring_wait_free(const struct rocker_desc_info *desc_info) 569 { 570 struct rocker_wait *wait = rocker_desc_cookie_ptr_get(desc_info); 571 572 rocker_wait_destroy(wait); 573 } 574 575 static int rocker_dma_cmd_ring_waits_alloc(const struct rocker *rocker) 576 { 577 const struct rocker_dma_ring_info *cmd_ring = &rocker->cmd_ring; 578 int i; 579 int err; 580 581 for (i = 0; i < cmd_ring->size; i++) { 582 err = rocker_dma_cmd_ring_wait_alloc(&cmd_ring->desc_info[i]); 583 if (err) 584 goto rollback; 585 } 586 return 0; 587 588 rollback: 589 for (i--; i >= 0; i--) 590 rocker_dma_cmd_ring_wait_free(&cmd_ring->desc_info[i]); 591 return err; 592 } 593 594 static void rocker_dma_cmd_ring_waits_free(const struct rocker *rocker) 595 { 596 const struct rocker_dma_ring_info *cmd_ring = &rocker->cmd_ring; 597 int i; 598 599 for (i = 0; i < cmd_ring->size; i++) 600 rocker_dma_cmd_ring_wait_free(&cmd_ring->desc_info[i]); 601 } 602 603 static int rocker_dma_rings_init(struct rocker *rocker) 604 { 605 const struct pci_dev *pdev = rocker->pdev; 606 int err; 607 608 err = rocker_dma_ring_create(rocker, ROCKER_DMA_CMD, 609 ROCKER_DMA_CMD_DEFAULT_SIZE, 610 &rocker->cmd_ring); 611 if (err) { 612 dev_err(&pdev->dev, "failed to create command dma ring\n"); 613 return err; 614 } 615 616 spin_lock_init(&rocker->cmd_ring_lock); 617 618 err = rocker_dma_ring_bufs_alloc(rocker, &rocker->cmd_ring, 619 PCI_DMA_BIDIRECTIONAL, PAGE_SIZE); 620 if (err) { 621 dev_err(&pdev->dev, "failed to alloc command dma ring buffers\n"); 622 goto err_dma_cmd_ring_bufs_alloc; 623 } 624 625 err = rocker_dma_cmd_ring_waits_alloc(rocker); 626 if (err) { 627 dev_err(&pdev->dev, "failed to alloc command dma ring waits\n"); 628 goto err_dma_cmd_ring_waits_alloc; 629 } 630 631 err = rocker_dma_ring_create(rocker, ROCKER_DMA_EVENT, 632 ROCKER_DMA_EVENT_DEFAULT_SIZE, 633 &rocker->event_ring); 634 if (err) { 635 dev_err(&pdev->dev, "failed to create event dma ring\n"); 636 goto err_dma_event_ring_create; 637 } 638 639 err = rocker_dma_ring_bufs_alloc(rocker, &rocker->event_ring, 640 PCI_DMA_FROMDEVICE, PAGE_SIZE); 641 if (err) { 642 dev_err(&pdev->dev, "failed to alloc event dma ring buffers\n"); 643 goto err_dma_event_ring_bufs_alloc; 644 } 645 rocker_dma_ring_pass_to_producer(rocker, &rocker->event_ring); 646 return 0; 647 648 err_dma_event_ring_bufs_alloc: 649 rocker_dma_ring_destroy(rocker, &rocker->event_ring); 650 err_dma_event_ring_create: 651 rocker_dma_ring_bufs_free(rocker, &rocker->cmd_ring, 652 PCI_DMA_BIDIRECTIONAL); 653 err_dma_cmd_ring_waits_alloc: 654 rocker_dma_cmd_ring_waits_free(rocker); 655 err_dma_cmd_ring_bufs_alloc: 656 rocker_dma_ring_destroy(rocker, &rocker->cmd_ring); 657 return err; 658 } 659 660 static void rocker_dma_rings_fini(struct rocker *rocker) 661 { 662 rocker_dma_ring_bufs_free(rocker, &rocker->event_ring, 663 PCI_DMA_BIDIRECTIONAL); 664 rocker_dma_ring_destroy(rocker, &rocker->event_ring); 665 rocker_dma_cmd_ring_waits_free(rocker); 666 rocker_dma_ring_bufs_free(rocker, &rocker->cmd_ring, 667 PCI_DMA_BIDIRECTIONAL); 668 rocker_dma_ring_destroy(rocker, &rocker->cmd_ring); 669 } 670 671 static int rocker_dma_rx_ring_skb_map(const struct rocker_port *rocker_port, 672 struct rocker_desc_info *desc_info, 673 struct sk_buff *skb, size_t buf_len) 674 { 675 const struct rocker *rocker = rocker_port->rocker; 676 struct pci_dev *pdev = rocker->pdev; 677 dma_addr_t dma_handle; 678 679 dma_handle = pci_map_single(pdev, skb->data, buf_len, 680 PCI_DMA_FROMDEVICE); 681 if (pci_dma_mapping_error(pdev, dma_handle)) 682 return -EIO; 683 if (rocker_tlv_put_u64(desc_info, ROCKER_TLV_RX_FRAG_ADDR, dma_handle)) 684 goto tlv_put_failure; 685 if (rocker_tlv_put_u16(desc_info, ROCKER_TLV_RX_FRAG_MAX_LEN, buf_len)) 686 goto tlv_put_failure; 687 return 0; 688 689 tlv_put_failure: 690 pci_unmap_single(pdev, dma_handle, buf_len, PCI_DMA_FROMDEVICE); 691 desc_info->tlv_size = 0; 692 return -EMSGSIZE; 693 } 694 695 static size_t rocker_port_rx_buf_len(const struct rocker_port *rocker_port) 696 { 697 return rocker_port->dev->mtu + ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN; 698 } 699 700 static int rocker_dma_rx_ring_skb_alloc(const struct rocker_port *rocker_port, 701 struct rocker_desc_info *desc_info) 702 { 703 struct net_device *dev = rocker_port->dev; 704 struct sk_buff *skb; 705 size_t buf_len = rocker_port_rx_buf_len(rocker_port); 706 int err; 707 708 /* Ensure that hw will see tlv_size zero in case of an error. 709 * That tells hw to use another descriptor. 710 */ 711 rocker_desc_cookie_ptr_set(desc_info, NULL); 712 desc_info->tlv_size = 0; 713 714 skb = netdev_alloc_skb_ip_align(dev, buf_len); 715 if (!skb) 716 return -ENOMEM; 717 err = rocker_dma_rx_ring_skb_map(rocker_port, desc_info, skb, buf_len); 718 if (err) { 719 dev_kfree_skb_any(skb); 720 return err; 721 } 722 rocker_desc_cookie_ptr_set(desc_info, skb); 723 return 0; 724 } 725 726 static void rocker_dma_rx_ring_skb_unmap(const struct rocker *rocker, 727 const struct rocker_tlv **attrs) 728 { 729 struct pci_dev *pdev = rocker->pdev; 730 dma_addr_t dma_handle; 731 size_t len; 732 733 if (!attrs[ROCKER_TLV_RX_FRAG_ADDR] || 734 !attrs[ROCKER_TLV_RX_FRAG_MAX_LEN]) 735 return; 736 dma_handle = rocker_tlv_get_u64(attrs[ROCKER_TLV_RX_FRAG_ADDR]); 737 len = rocker_tlv_get_u16(attrs[ROCKER_TLV_RX_FRAG_MAX_LEN]); 738 pci_unmap_single(pdev, dma_handle, len, PCI_DMA_FROMDEVICE); 739 } 740 741 static void rocker_dma_rx_ring_skb_free(const struct rocker *rocker, 742 const struct rocker_desc_info *desc_info) 743 { 744 const struct rocker_tlv *attrs[ROCKER_TLV_RX_MAX + 1]; 745 struct sk_buff *skb = rocker_desc_cookie_ptr_get(desc_info); 746 747 if (!skb) 748 return; 749 rocker_tlv_parse_desc(attrs, ROCKER_TLV_RX_MAX, desc_info); 750 rocker_dma_rx_ring_skb_unmap(rocker, attrs); 751 dev_kfree_skb_any(skb); 752 } 753 754 static int rocker_dma_rx_ring_skbs_alloc(const struct rocker_port *rocker_port) 755 { 756 const struct rocker_dma_ring_info *rx_ring = &rocker_port->rx_ring; 757 const struct rocker *rocker = rocker_port->rocker; 758 int i; 759 int err; 760 761 for (i = 0; i < rx_ring->size; i++) { 762 err = rocker_dma_rx_ring_skb_alloc(rocker_port, 763 &rx_ring->desc_info[i]); 764 if (err) 765 goto rollback; 766 } 767 return 0; 768 769 rollback: 770 for (i--; i >= 0; i--) 771 rocker_dma_rx_ring_skb_free(rocker, &rx_ring->desc_info[i]); 772 return err; 773 } 774 775 static void rocker_dma_rx_ring_skbs_free(const struct rocker_port *rocker_port) 776 { 777 const struct rocker_dma_ring_info *rx_ring = &rocker_port->rx_ring; 778 const struct rocker *rocker = rocker_port->rocker; 779 int i; 780 781 for (i = 0; i < rx_ring->size; i++) 782 rocker_dma_rx_ring_skb_free(rocker, &rx_ring->desc_info[i]); 783 } 784 785 static int rocker_port_dma_rings_init(struct rocker_port *rocker_port) 786 { 787 struct rocker *rocker = rocker_port->rocker; 788 int err; 789 790 err = rocker_dma_ring_create(rocker, 791 ROCKER_DMA_TX(rocker_port->port_number), 792 ROCKER_DMA_TX_DEFAULT_SIZE, 793 &rocker_port->tx_ring); 794 if (err) { 795 netdev_err(rocker_port->dev, "failed to create tx dma ring\n"); 796 return err; 797 } 798 799 err = rocker_dma_ring_bufs_alloc(rocker, &rocker_port->tx_ring, 800 PCI_DMA_TODEVICE, 801 ROCKER_DMA_TX_DESC_SIZE); 802 if (err) { 803 netdev_err(rocker_port->dev, "failed to alloc tx dma ring buffers\n"); 804 goto err_dma_tx_ring_bufs_alloc; 805 } 806 807 err = rocker_dma_ring_create(rocker, 808 ROCKER_DMA_RX(rocker_port->port_number), 809 ROCKER_DMA_RX_DEFAULT_SIZE, 810 &rocker_port->rx_ring); 811 if (err) { 812 netdev_err(rocker_port->dev, "failed to create rx dma ring\n"); 813 goto err_dma_rx_ring_create; 814 } 815 816 err = rocker_dma_ring_bufs_alloc(rocker, &rocker_port->rx_ring, 817 PCI_DMA_BIDIRECTIONAL, 818 ROCKER_DMA_RX_DESC_SIZE); 819 if (err) { 820 netdev_err(rocker_port->dev, "failed to alloc rx dma ring buffers\n"); 821 goto err_dma_rx_ring_bufs_alloc; 822 } 823 824 err = rocker_dma_rx_ring_skbs_alloc(rocker_port); 825 if (err) { 826 netdev_err(rocker_port->dev, "failed to alloc rx dma ring skbs\n"); 827 goto err_dma_rx_ring_skbs_alloc; 828 } 829 rocker_dma_ring_pass_to_producer(rocker, &rocker_port->rx_ring); 830 831 return 0; 832 833 err_dma_rx_ring_skbs_alloc: 834 rocker_dma_ring_bufs_free(rocker, &rocker_port->rx_ring, 835 PCI_DMA_BIDIRECTIONAL); 836 err_dma_rx_ring_bufs_alloc: 837 rocker_dma_ring_destroy(rocker, &rocker_port->rx_ring); 838 err_dma_rx_ring_create: 839 rocker_dma_ring_bufs_free(rocker, &rocker_port->tx_ring, 840 PCI_DMA_TODEVICE); 841 err_dma_tx_ring_bufs_alloc: 842 rocker_dma_ring_destroy(rocker, &rocker_port->tx_ring); 843 return err; 844 } 845 846 static void rocker_port_dma_rings_fini(struct rocker_port *rocker_port) 847 { 848 struct rocker *rocker = rocker_port->rocker; 849 850 rocker_dma_rx_ring_skbs_free(rocker_port); 851 rocker_dma_ring_bufs_free(rocker, &rocker_port->rx_ring, 852 PCI_DMA_BIDIRECTIONAL); 853 rocker_dma_ring_destroy(rocker, &rocker_port->rx_ring); 854 rocker_dma_ring_bufs_free(rocker, &rocker_port->tx_ring, 855 PCI_DMA_TODEVICE); 856 rocker_dma_ring_destroy(rocker, &rocker_port->tx_ring); 857 } 858 859 static void rocker_port_set_enable(const struct rocker_port *rocker_port, 860 bool enable) 861 { 862 u64 val = rocker_read64(rocker_port->rocker, PORT_PHYS_ENABLE); 863 864 if (enable) 865 val |= 1ULL << rocker_port->pport; 866 else 867 val &= ~(1ULL << rocker_port->pport); 868 rocker_write64(rocker_port->rocker, PORT_PHYS_ENABLE, val); 869 } 870 871 /******************************** 872 * Interrupt handler and helpers 873 ********************************/ 874 875 static irqreturn_t rocker_cmd_irq_handler(int irq, void *dev_id) 876 { 877 struct rocker *rocker = dev_id; 878 const struct rocker_desc_info *desc_info; 879 struct rocker_wait *wait; 880 u32 credits = 0; 881 882 spin_lock(&rocker->cmd_ring_lock); 883 while ((desc_info = rocker_desc_tail_get(&rocker->cmd_ring))) { 884 wait = rocker_desc_cookie_ptr_get(desc_info); 885 if (wait->nowait) { 886 rocker_desc_gen_clear(desc_info); 887 } else { 888 rocker_wait_wake_up(wait); 889 } 890 credits++; 891 } 892 spin_unlock(&rocker->cmd_ring_lock); 893 rocker_dma_ring_credits_set(rocker, &rocker->cmd_ring, credits); 894 895 return IRQ_HANDLED; 896 } 897 898 static void rocker_port_link_up(const struct rocker_port *rocker_port) 899 { 900 netif_carrier_on(rocker_port->dev); 901 netdev_info(rocker_port->dev, "Link is up\n"); 902 } 903 904 static void rocker_port_link_down(const struct rocker_port *rocker_port) 905 { 906 netif_carrier_off(rocker_port->dev); 907 netdev_info(rocker_port->dev, "Link is down\n"); 908 } 909 910 static int rocker_event_link_change(const struct rocker *rocker, 911 const struct rocker_tlv *info) 912 { 913 const struct rocker_tlv *attrs[ROCKER_TLV_EVENT_LINK_CHANGED_MAX + 1]; 914 unsigned int port_number; 915 bool link_up; 916 struct rocker_port *rocker_port; 917 918 rocker_tlv_parse_nested(attrs, ROCKER_TLV_EVENT_LINK_CHANGED_MAX, info); 919 if (!attrs[ROCKER_TLV_EVENT_LINK_CHANGED_PPORT] || 920 !attrs[ROCKER_TLV_EVENT_LINK_CHANGED_LINKUP]) 921 return -EIO; 922 port_number = 923 rocker_tlv_get_u32(attrs[ROCKER_TLV_EVENT_LINK_CHANGED_PPORT]) - 1; 924 link_up = rocker_tlv_get_u8(attrs[ROCKER_TLV_EVENT_LINK_CHANGED_LINKUP]); 925 926 if (port_number >= rocker->port_count) 927 return -EINVAL; 928 929 rocker_port = rocker->ports[port_number]; 930 if (netif_carrier_ok(rocker_port->dev) != link_up) { 931 if (link_up) 932 rocker_port_link_up(rocker_port); 933 else 934 rocker_port_link_down(rocker_port); 935 } 936 937 return 0; 938 } 939 940 static int rocker_world_port_ev_mac_vlan_seen(struct rocker_port *rocker_port, 941 const unsigned char *addr, 942 __be16 vlan_id); 943 944 static int rocker_event_mac_vlan_seen(const struct rocker *rocker, 945 const struct rocker_tlv *info) 946 { 947 const struct rocker_tlv *attrs[ROCKER_TLV_EVENT_MAC_VLAN_MAX + 1]; 948 unsigned int port_number; 949 struct rocker_port *rocker_port; 950 const unsigned char *addr; 951 __be16 vlan_id; 952 953 rocker_tlv_parse_nested(attrs, ROCKER_TLV_EVENT_MAC_VLAN_MAX, info); 954 if (!attrs[ROCKER_TLV_EVENT_MAC_VLAN_PPORT] || 955 !attrs[ROCKER_TLV_EVENT_MAC_VLAN_MAC] || 956 !attrs[ROCKER_TLV_EVENT_MAC_VLAN_VLAN_ID]) 957 return -EIO; 958 port_number = 959 rocker_tlv_get_u32(attrs[ROCKER_TLV_EVENT_MAC_VLAN_PPORT]) - 1; 960 addr = rocker_tlv_data(attrs[ROCKER_TLV_EVENT_MAC_VLAN_MAC]); 961 vlan_id = rocker_tlv_get_be16(attrs[ROCKER_TLV_EVENT_MAC_VLAN_VLAN_ID]); 962 963 if (port_number >= rocker->port_count) 964 return -EINVAL; 965 966 rocker_port = rocker->ports[port_number]; 967 return rocker_world_port_ev_mac_vlan_seen(rocker_port, addr, vlan_id); 968 } 969 970 static int rocker_event_process(const struct rocker *rocker, 971 const struct rocker_desc_info *desc_info) 972 { 973 const struct rocker_tlv *attrs[ROCKER_TLV_EVENT_MAX + 1]; 974 const struct rocker_tlv *info; 975 u16 type; 976 977 rocker_tlv_parse_desc(attrs, ROCKER_TLV_EVENT_MAX, desc_info); 978 if (!attrs[ROCKER_TLV_EVENT_TYPE] || 979 !attrs[ROCKER_TLV_EVENT_INFO]) 980 return -EIO; 981 982 type = rocker_tlv_get_u16(attrs[ROCKER_TLV_EVENT_TYPE]); 983 info = attrs[ROCKER_TLV_EVENT_INFO]; 984 985 switch (type) { 986 case ROCKER_TLV_EVENT_TYPE_LINK_CHANGED: 987 return rocker_event_link_change(rocker, info); 988 case ROCKER_TLV_EVENT_TYPE_MAC_VLAN_SEEN: 989 return rocker_event_mac_vlan_seen(rocker, info); 990 } 991 992 return -EOPNOTSUPP; 993 } 994 995 static irqreturn_t rocker_event_irq_handler(int irq, void *dev_id) 996 { 997 struct rocker *rocker = dev_id; 998 const struct pci_dev *pdev = rocker->pdev; 999 const struct rocker_desc_info *desc_info; 1000 u32 credits = 0; 1001 int err; 1002 1003 while ((desc_info = rocker_desc_tail_get(&rocker->event_ring))) { 1004 err = rocker_desc_err(desc_info); 1005 if (err) { 1006 dev_err(&pdev->dev, "event desc received with err %d\n", 1007 err); 1008 } else { 1009 err = rocker_event_process(rocker, desc_info); 1010 if (err) 1011 dev_err(&pdev->dev, "event processing failed with err %d\n", 1012 err); 1013 } 1014 rocker_desc_gen_clear(desc_info); 1015 rocker_desc_head_set(rocker, &rocker->event_ring, desc_info); 1016 credits++; 1017 } 1018 rocker_dma_ring_credits_set(rocker, &rocker->event_ring, credits); 1019 1020 return IRQ_HANDLED; 1021 } 1022 1023 static irqreturn_t rocker_tx_irq_handler(int irq, void *dev_id) 1024 { 1025 struct rocker_port *rocker_port = dev_id; 1026 1027 napi_schedule(&rocker_port->napi_tx); 1028 return IRQ_HANDLED; 1029 } 1030 1031 static irqreturn_t rocker_rx_irq_handler(int irq, void *dev_id) 1032 { 1033 struct rocker_port *rocker_port = dev_id; 1034 1035 napi_schedule(&rocker_port->napi_rx); 1036 return IRQ_HANDLED; 1037 } 1038 1039 /******************** 1040 * Command interface 1041 ********************/ 1042 1043 int rocker_cmd_exec(struct rocker_port *rocker_port, bool nowait, 1044 rocker_cmd_prep_cb_t prepare, void *prepare_priv, 1045 rocker_cmd_proc_cb_t process, void *process_priv) 1046 { 1047 struct rocker *rocker = rocker_port->rocker; 1048 struct rocker_desc_info *desc_info; 1049 struct rocker_wait *wait; 1050 unsigned long lock_flags; 1051 int err; 1052 1053 spin_lock_irqsave(&rocker->cmd_ring_lock, lock_flags); 1054 1055 desc_info = rocker_desc_head_get(&rocker->cmd_ring); 1056 if (!desc_info) { 1057 spin_unlock_irqrestore(&rocker->cmd_ring_lock, lock_flags); 1058 return -EAGAIN; 1059 } 1060 1061 wait = rocker_desc_cookie_ptr_get(desc_info); 1062 rocker_wait_init(wait); 1063 wait->nowait = nowait; 1064 1065 err = prepare(rocker_port, desc_info, prepare_priv); 1066 if (err) { 1067 spin_unlock_irqrestore(&rocker->cmd_ring_lock, lock_flags); 1068 return err; 1069 } 1070 1071 rocker_desc_head_set(rocker, &rocker->cmd_ring, desc_info); 1072 1073 spin_unlock_irqrestore(&rocker->cmd_ring_lock, lock_flags); 1074 1075 if (nowait) 1076 return 0; 1077 1078 if (!rocker_wait_event_timeout(wait, HZ / 10)) 1079 return -EIO; 1080 1081 err = rocker_desc_err(desc_info); 1082 if (err) 1083 return err; 1084 1085 if (process) 1086 err = process(rocker_port, desc_info, process_priv); 1087 1088 rocker_desc_gen_clear(desc_info); 1089 return err; 1090 } 1091 1092 static int 1093 rocker_cmd_get_port_settings_prep(const struct rocker_port *rocker_port, 1094 struct rocker_desc_info *desc_info, 1095 void *priv) 1096 { 1097 struct rocker_tlv *cmd_info; 1098 1099 if (rocker_tlv_put_u16(desc_info, ROCKER_TLV_CMD_TYPE, 1100 ROCKER_TLV_CMD_TYPE_GET_PORT_SETTINGS)) 1101 return -EMSGSIZE; 1102 cmd_info = rocker_tlv_nest_start(desc_info, ROCKER_TLV_CMD_INFO); 1103 if (!cmd_info) 1104 return -EMSGSIZE; 1105 if (rocker_tlv_put_u32(desc_info, ROCKER_TLV_CMD_PORT_SETTINGS_PPORT, 1106 rocker_port->pport)) 1107 return -EMSGSIZE; 1108 rocker_tlv_nest_end(desc_info, cmd_info); 1109 return 0; 1110 } 1111 1112 static int 1113 rocker_cmd_get_port_settings_ethtool_proc(const struct rocker_port *rocker_port, 1114 const struct rocker_desc_info *desc_info, 1115 void *priv) 1116 { 1117 struct ethtool_cmd *ecmd = priv; 1118 const struct rocker_tlv *attrs[ROCKER_TLV_CMD_MAX + 1]; 1119 const struct rocker_tlv *info_attrs[ROCKER_TLV_CMD_PORT_SETTINGS_MAX + 1]; 1120 u32 speed; 1121 u8 duplex; 1122 u8 autoneg; 1123 1124 rocker_tlv_parse_desc(attrs, ROCKER_TLV_CMD_MAX, desc_info); 1125 if (!attrs[ROCKER_TLV_CMD_INFO]) 1126 return -EIO; 1127 1128 rocker_tlv_parse_nested(info_attrs, ROCKER_TLV_CMD_PORT_SETTINGS_MAX, 1129 attrs[ROCKER_TLV_CMD_INFO]); 1130 if (!info_attrs[ROCKER_TLV_CMD_PORT_SETTINGS_SPEED] || 1131 !info_attrs[ROCKER_TLV_CMD_PORT_SETTINGS_DUPLEX] || 1132 !info_attrs[ROCKER_TLV_CMD_PORT_SETTINGS_AUTONEG]) 1133 return -EIO; 1134 1135 speed = rocker_tlv_get_u32(info_attrs[ROCKER_TLV_CMD_PORT_SETTINGS_SPEED]); 1136 duplex = rocker_tlv_get_u8(info_attrs[ROCKER_TLV_CMD_PORT_SETTINGS_DUPLEX]); 1137 autoneg = rocker_tlv_get_u8(info_attrs[ROCKER_TLV_CMD_PORT_SETTINGS_AUTONEG]); 1138 1139 ecmd->transceiver = XCVR_INTERNAL; 1140 ecmd->supported = SUPPORTED_TP; 1141 ecmd->phy_address = 0xff; 1142 ecmd->port = PORT_TP; 1143 ethtool_cmd_speed_set(ecmd, speed); 1144 ecmd->duplex = duplex ? DUPLEX_FULL : DUPLEX_HALF; 1145 ecmd->autoneg = autoneg ? AUTONEG_ENABLE : AUTONEG_DISABLE; 1146 1147 return 0; 1148 } 1149 1150 static int 1151 rocker_cmd_get_port_settings_macaddr_proc(const struct rocker_port *rocker_port, 1152 const struct rocker_desc_info *desc_info, 1153 void *priv) 1154 { 1155 unsigned char *macaddr = priv; 1156 const struct rocker_tlv *attrs[ROCKER_TLV_CMD_MAX + 1]; 1157 const struct rocker_tlv *info_attrs[ROCKER_TLV_CMD_PORT_SETTINGS_MAX + 1]; 1158 const struct rocker_tlv *attr; 1159 1160 rocker_tlv_parse_desc(attrs, ROCKER_TLV_CMD_MAX, desc_info); 1161 if (!attrs[ROCKER_TLV_CMD_INFO]) 1162 return -EIO; 1163 1164 rocker_tlv_parse_nested(info_attrs, ROCKER_TLV_CMD_PORT_SETTINGS_MAX, 1165 attrs[ROCKER_TLV_CMD_INFO]); 1166 attr = info_attrs[ROCKER_TLV_CMD_PORT_SETTINGS_MACADDR]; 1167 if (!attr) 1168 return -EIO; 1169 1170 if (rocker_tlv_len(attr) != ETH_ALEN) 1171 return -EINVAL; 1172 1173 ether_addr_copy(macaddr, rocker_tlv_data(attr)); 1174 return 0; 1175 } 1176 1177 static int 1178 rocker_cmd_get_port_settings_mode_proc(const struct rocker_port *rocker_port, 1179 const struct rocker_desc_info *desc_info, 1180 void *priv) 1181 { 1182 u8 *p_mode = priv; 1183 const struct rocker_tlv *attrs[ROCKER_TLV_CMD_MAX + 1]; 1184 const struct rocker_tlv *info_attrs[ROCKER_TLV_CMD_PORT_SETTINGS_MAX + 1]; 1185 const struct rocker_tlv *attr; 1186 1187 rocker_tlv_parse_desc(attrs, ROCKER_TLV_CMD_MAX, desc_info); 1188 if (!attrs[ROCKER_TLV_CMD_INFO]) 1189 return -EIO; 1190 1191 rocker_tlv_parse_nested(info_attrs, ROCKER_TLV_CMD_PORT_SETTINGS_MAX, 1192 attrs[ROCKER_TLV_CMD_INFO]); 1193 attr = info_attrs[ROCKER_TLV_CMD_PORT_SETTINGS_MODE]; 1194 if (!attr) 1195 return -EIO; 1196 1197 *p_mode = rocker_tlv_get_u8(info_attrs[ROCKER_TLV_CMD_PORT_SETTINGS_MODE]); 1198 return 0; 1199 } 1200 1201 struct port_name { 1202 char *buf; 1203 size_t len; 1204 }; 1205 1206 static int 1207 rocker_cmd_get_port_settings_phys_name_proc(const struct rocker_port *rocker_port, 1208 const struct rocker_desc_info *desc_info, 1209 void *priv) 1210 { 1211 const struct rocker_tlv *info_attrs[ROCKER_TLV_CMD_PORT_SETTINGS_MAX + 1]; 1212 const struct rocker_tlv *attrs[ROCKER_TLV_CMD_MAX + 1]; 1213 struct port_name *name = priv; 1214 const struct rocker_tlv *attr; 1215 size_t i, j, len; 1216 const char *str; 1217 1218 rocker_tlv_parse_desc(attrs, ROCKER_TLV_CMD_MAX, desc_info); 1219 if (!attrs[ROCKER_TLV_CMD_INFO]) 1220 return -EIO; 1221 1222 rocker_tlv_parse_nested(info_attrs, ROCKER_TLV_CMD_PORT_SETTINGS_MAX, 1223 attrs[ROCKER_TLV_CMD_INFO]); 1224 attr = info_attrs[ROCKER_TLV_CMD_PORT_SETTINGS_PHYS_NAME]; 1225 if (!attr) 1226 return -EIO; 1227 1228 len = min_t(size_t, rocker_tlv_len(attr), name->len); 1229 str = rocker_tlv_data(attr); 1230 1231 /* make sure name only contains alphanumeric characters */ 1232 for (i = j = 0; i < len; ++i) { 1233 if (isalnum(str[i])) { 1234 name->buf[j] = str[i]; 1235 j++; 1236 } 1237 } 1238 1239 if (j == 0) 1240 return -EIO; 1241 1242 name->buf[j] = '\0'; 1243 1244 return 0; 1245 } 1246 1247 static int 1248 rocker_cmd_set_port_settings_ethtool_prep(const struct rocker_port *rocker_port, 1249 struct rocker_desc_info *desc_info, 1250 void *priv) 1251 { 1252 struct ethtool_cmd *ecmd = priv; 1253 struct rocker_tlv *cmd_info; 1254 1255 if (rocker_tlv_put_u16(desc_info, ROCKER_TLV_CMD_TYPE, 1256 ROCKER_TLV_CMD_TYPE_SET_PORT_SETTINGS)) 1257 return -EMSGSIZE; 1258 cmd_info = rocker_tlv_nest_start(desc_info, ROCKER_TLV_CMD_INFO); 1259 if (!cmd_info) 1260 return -EMSGSIZE; 1261 if (rocker_tlv_put_u32(desc_info, ROCKER_TLV_CMD_PORT_SETTINGS_PPORT, 1262 rocker_port->pport)) 1263 return -EMSGSIZE; 1264 if (rocker_tlv_put_u32(desc_info, ROCKER_TLV_CMD_PORT_SETTINGS_SPEED, 1265 ethtool_cmd_speed(ecmd))) 1266 return -EMSGSIZE; 1267 if (rocker_tlv_put_u8(desc_info, ROCKER_TLV_CMD_PORT_SETTINGS_DUPLEX, 1268 ecmd->duplex)) 1269 return -EMSGSIZE; 1270 if (rocker_tlv_put_u8(desc_info, ROCKER_TLV_CMD_PORT_SETTINGS_AUTONEG, 1271 ecmd->autoneg)) 1272 return -EMSGSIZE; 1273 rocker_tlv_nest_end(desc_info, cmd_info); 1274 return 0; 1275 } 1276 1277 static int 1278 rocker_cmd_set_port_settings_macaddr_prep(const struct rocker_port *rocker_port, 1279 struct rocker_desc_info *desc_info, 1280 void *priv) 1281 { 1282 const unsigned char *macaddr = priv; 1283 struct rocker_tlv *cmd_info; 1284 1285 if (rocker_tlv_put_u16(desc_info, ROCKER_TLV_CMD_TYPE, 1286 ROCKER_TLV_CMD_TYPE_SET_PORT_SETTINGS)) 1287 return -EMSGSIZE; 1288 cmd_info = rocker_tlv_nest_start(desc_info, ROCKER_TLV_CMD_INFO); 1289 if (!cmd_info) 1290 return -EMSGSIZE; 1291 if (rocker_tlv_put_u32(desc_info, ROCKER_TLV_CMD_PORT_SETTINGS_PPORT, 1292 rocker_port->pport)) 1293 return -EMSGSIZE; 1294 if (rocker_tlv_put(desc_info, ROCKER_TLV_CMD_PORT_SETTINGS_MACADDR, 1295 ETH_ALEN, macaddr)) 1296 return -EMSGSIZE; 1297 rocker_tlv_nest_end(desc_info, cmd_info); 1298 return 0; 1299 } 1300 1301 static int 1302 rocker_cmd_set_port_settings_mtu_prep(const struct rocker_port *rocker_port, 1303 struct rocker_desc_info *desc_info, 1304 void *priv) 1305 { 1306 int mtu = *(int *)priv; 1307 struct rocker_tlv *cmd_info; 1308 1309 if (rocker_tlv_put_u16(desc_info, ROCKER_TLV_CMD_TYPE, 1310 ROCKER_TLV_CMD_TYPE_SET_PORT_SETTINGS)) 1311 return -EMSGSIZE; 1312 cmd_info = rocker_tlv_nest_start(desc_info, ROCKER_TLV_CMD_INFO); 1313 if (!cmd_info) 1314 return -EMSGSIZE; 1315 if (rocker_tlv_put_u32(desc_info, ROCKER_TLV_CMD_PORT_SETTINGS_PPORT, 1316 rocker_port->pport)) 1317 return -EMSGSIZE; 1318 if (rocker_tlv_put_u16(desc_info, ROCKER_TLV_CMD_PORT_SETTINGS_MTU, 1319 mtu)) 1320 return -EMSGSIZE; 1321 rocker_tlv_nest_end(desc_info, cmd_info); 1322 return 0; 1323 } 1324 1325 static int 1326 rocker_cmd_set_port_learning_prep(const struct rocker_port *rocker_port, 1327 struct rocker_desc_info *desc_info, 1328 void *priv) 1329 { 1330 bool learning = *(bool *)priv; 1331 struct rocker_tlv *cmd_info; 1332 1333 if (rocker_tlv_put_u16(desc_info, ROCKER_TLV_CMD_TYPE, 1334 ROCKER_TLV_CMD_TYPE_SET_PORT_SETTINGS)) 1335 return -EMSGSIZE; 1336 cmd_info = rocker_tlv_nest_start(desc_info, ROCKER_TLV_CMD_INFO); 1337 if (!cmd_info) 1338 return -EMSGSIZE; 1339 if (rocker_tlv_put_u32(desc_info, ROCKER_TLV_CMD_PORT_SETTINGS_PPORT, 1340 rocker_port->pport)) 1341 return -EMSGSIZE; 1342 if (rocker_tlv_put_u8(desc_info, ROCKER_TLV_CMD_PORT_SETTINGS_LEARNING, 1343 learning)) 1344 return -EMSGSIZE; 1345 rocker_tlv_nest_end(desc_info, cmd_info); 1346 return 0; 1347 } 1348 1349 static int rocker_cmd_get_port_settings_ethtool(struct rocker_port *rocker_port, 1350 struct ethtool_cmd *ecmd) 1351 { 1352 return rocker_cmd_exec(rocker_port, false, 1353 rocker_cmd_get_port_settings_prep, NULL, 1354 rocker_cmd_get_port_settings_ethtool_proc, 1355 ecmd); 1356 } 1357 1358 static int rocker_cmd_get_port_settings_macaddr(struct rocker_port *rocker_port, 1359 unsigned char *macaddr) 1360 { 1361 return rocker_cmd_exec(rocker_port, false, 1362 rocker_cmd_get_port_settings_prep, NULL, 1363 rocker_cmd_get_port_settings_macaddr_proc, 1364 macaddr); 1365 } 1366 1367 static int rocker_cmd_get_port_settings_mode(struct rocker_port *rocker_port, 1368 u8 *p_mode) 1369 { 1370 return rocker_cmd_exec(rocker_port, false, 1371 rocker_cmd_get_port_settings_prep, NULL, 1372 rocker_cmd_get_port_settings_mode_proc, p_mode); 1373 } 1374 1375 static int rocker_cmd_set_port_settings_ethtool(struct rocker_port *rocker_port, 1376 struct ethtool_cmd *ecmd) 1377 { 1378 return rocker_cmd_exec(rocker_port, false, 1379 rocker_cmd_set_port_settings_ethtool_prep, 1380 ecmd, NULL, NULL); 1381 } 1382 1383 static int rocker_cmd_set_port_settings_macaddr(struct rocker_port *rocker_port, 1384 unsigned char *macaddr) 1385 { 1386 return rocker_cmd_exec(rocker_port, false, 1387 rocker_cmd_set_port_settings_macaddr_prep, 1388 macaddr, NULL, NULL); 1389 } 1390 1391 static int rocker_cmd_set_port_settings_mtu(struct rocker_port *rocker_port, 1392 int mtu) 1393 { 1394 return rocker_cmd_exec(rocker_port, false, 1395 rocker_cmd_set_port_settings_mtu_prep, 1396 &mtu, NULL, NULL); 1397 } 1398 1399 int rocker_port_set_learning(struct rocker_port *rocker_port, 1400 bool learning) 1401 { 1402 return rocker_cmd_exec(rocker_port, false, 1403 rocker_cmd_set_port_learning_prep, 1404 &learning, NULL, NULL); 1405 } 1406 1407 /********************** 1408 * Worlds manipulation 1409 **********************/ 1410 1411 static struct rocker_world_ops *rocker_world_ops[] = { 1412 &rocker_ofdpa_ops, 1413 }; 1414 1415 #define ROCKER_WORLD_OPS_LEN ARRAY_SIZE(rocker_world_ops) 1416 1417 static struct rocker_world_ops *rocker_world_ops_find(u8 mode) 1418 { 1419 int i; 1420 1421 for (i = 0; i < ROCKER_WORLD_OPS_LEN; i++) 1422 if (rocker_world_ops[i]->mode == mode) 1423 return rocker_world_ops[i]; 1424 return NULL; 1425 } 1426 1427 static int rocker_world_init(struct rocker *rocker, u8 mode) 1428 { 1429 struct rocker_world_ops *wops; 1430 int err; 1431 1432 wops = rocker_world_ops_find(mode); 1433 if (!wops) { 1434 dev_err(&rocker->pdev->dev, "port mode \"%d\" is not supported\n", 1435 mode); 1436 return -EINVAL; 1437 } 1438 rocker->wops = wops; 1439 rocker->wpriv = kzalloc(wops->priv_size, GFP_KERNEL); 1440 if (!rocker->wpriv) 1441 return -ENOMEM; 1442 if (!wops->init) 1443 return 0; 1444 err = wops->init(rocker); 1445 if (err) 1446 kfree(rocker->wpriv); 1447 return err; 1448 } 1449 1450 static void rocker_world_fini(struct rocker *rocker) 1451 { 1452 struct rocker_world_ops *wops = rocker->wops; 1453 1454 if (!wops || !wops->fini) 1455 return; 1456 wops->fini(rocker); 1457 kfree(rocker->wpriv); 1458 } 1459 1460 static int rocker_world_check_init(struct rocker_port *rocker_port) 1461 { 1462 struct rocker *rocker = rocker_port->rocker; 1463 u8 mode; 1464 int err; 1465 1466 err = rocker_cmd_get_port_settings_mode(rocker_port, &mode); 1467 if (err) { 1468 dev_err(&rocker->pdev->dev, "failed to get port mode\n"); 1469 return err; 1470 } 1471 if (rocker->wops) { 1472 if (rocker->wops->mode != mode) { 1473 dev_err(&rocker->pdev->dev, "hardware has ports in different worlds, which is not supported\n"); 1474 return err; 1475 } 1476 return 0; 1477 } 1478 return rocker_world_init(rocker, mode); 1479 } 1480 1481 static int rocker_world_port_pre_init(struct rocker_port *rocker_port) 1482 { 1483 struct rocker_world_ops *wops = rocker_port->rocker->wops; 1484 int err; 1485 1486 rocker_port->wpriv = kzalloc(wops->port_priv_size, GFP_KERNEL); 1487 if (!rocker_port->wpriv) 1488 return -ENOMEM; 1489 if (!wops->port_pre_init) 1490 return 0; 1491 err = wops->port_pre_init(rocker_port); 1492 if (err) 1493 kfree(rocker_port->wpriv); 1494 return 0; 1495 } 1496 1497 static int rocker_world_port_init(struct rocker_port *rocker_port) 1498 { 1499 struct rocker_world_ops *wops = rocker_port->rocker->wops; 1500 1501 if (!wops->port_init) 1502 return 0; 1503 return wops->port_init(rocker_port); 1504 } 1505 1506 static void rocker_world_port_fini(struct rocker_port *rocker_port) 1507 { 1508 struct rocker_world_ops *wops = rocker_port->rocker->wops; 1509 1510 if (!wops->port_fini) 1511 return; 1512 wops->port_fini(rocker_port); 1513 } 1514 1515 static void rocker_world_port_post_fini(struct rocker_port *rocker_port) 1516 { 1517 struct rocker_world_ops *wops = rocker_port->rocker->wops; 1518 1519 if (!wops->port_post_fini) 1520 return; 1521 wops->port_post_fini(rocker_port); 1522 kfree(rocker_port->wpriv); 1523 } 1524 1525 static int rocker_world_port_open(struct rocker_port *rocker_port) 1526 { 1527 struct rocker_world_ops *wops = rocker_port->rocker->wops; 1528 1529 if (!wops->port_open) 1530 return 0; 1531 return wops->port_open(rocker_port); 1532 } 1533 1534 static void rocker_world_port_stop(struct rocker_port *rocker_port) 1535 { 1536 struct rocker_world_ops *wops = rocker_port->rocker->wops; 1537 1538 if (!wops->port_stop) 1539 return; 1540 wops->port_stop(rocker_port); 1541 } 1542 1543 static int rocker_world_port_attr_stp_state_set(struct rocker_port *rocker_port, 1544 u8 state, 1545 struct switchdev_trans *trans) 1546 { 1547 struct rocker_world_ops *wops = rocker_port->rocker->wops; 1548 1549 if (!wops->port_attr_stp_state_set) 1550 return -EOPNOTSUPP; 1551 return wops->port_attr_stp_state_set(rocker_port, state, trans); 1552 } 1553 1554 static int 1555 rocker_world_port_attr_bridge_flags_set(struct rocker_port *rocker_port, 1556 unsigned long brport_flags, 1557 struct switchdev_trans *trans) 1558 { 1559 struct rocker_world_ops *wops = rocker_port->rocker->wops; 1560 1561 if (!wops->port_attr_bridge_flags_set) 1562 return -EOPNOTSUPP; 1563 return wops->port_attr_bridge_flags_set(rocker_port, brport_flags, 1564 trans); 1565 } 1566 1567 static int 1568 rocker_world_port_attr_bridge_flags_get(const struct rocker_port *rocker_port, 1569 unsigned long *p_brport_flags) 1570 { 1571 struct rocker_world_ops *wops = rocker_port->rocker->wops; 1572 1573 if (!wops->port_attr_bridge_flags_get) 1574 return -EOPNOTSUPP; 1575 return wops->port_attr_bridge_flags_get(rocker_port, p_brport_flags); 1576 } 1577 1578 static int 1579 rocker_world_port_attr_bridge_ageing_time_set(struct rocker_port *rocker_port, 1580 u32 ageing_time, 1581 struct switchdev_trans *trans) 1582 1583 { 1584 struct rocker_world_ops *wops = rocker_port->rocker->wops; 1585 1586 if (!wops->port_attr_bridge_ageing_time_set) 1587 return -EOPNOTSUPP; 1588 return wops->port_attr_bridge_ageing_time_set(rocker_port, ageing_time, 1589 trans); 1590 } 1591 1592 static int 1593 rocker_world_port_obj_vlan_add(struct rocker_port *rocker_port, 1594 const struct switchdev_obj_port_vlan *vlan, 1595 struct switchdev_trans *trans) 1596 { 1597 struct rocker_world_ops *wops = rocker_port->rocker->wops; 1598 1599 if (!wops->port_obj_vlan_add) 1600 return -EOPNOTSUPP; 1601 return wops->port_obj_vlan_add(rocker_port, vlan, trans); 1602 } 1603 1604 static int 1605 rocker_world_port_obj_vlan_del(struct rocker_port *rocker_port, 1606 const struct switchdev_obj_port_vlan *vlan) 1607 { 1608 struct rocker_world_ops *wops = rocker_port->rocker->wops; 1609 1610 if (!wops->port_obj_vlan_del) 1611 return -EOPNOTSUPP; 1612 return wops->port_obj_vlan_del(rocker_port, vlan); 1613 } 1614 1615 static int 1616 rocker_world_port_obj_vlan_dump(const struct rocker_port *rocker_port, 1617 struct switchdev_obj_port_vlan *vlan, 1618 switchdev_obj_dump_cb_t *cb) 1619 { 1620 struct rocker_world_ops *wops = rocker_port->rocker->wops; 1621 1622 if (!wops->port_obj_vlan_dump) 1623 return -EOPNOTSUPP; 1624 return wops->port_obj_vlan_dump(rocker_port, vlan, cb); 1625 } 1626 1627 static int 1628 rocker_world_port_obj_fib4_add(struct rocker_port *rocker_port, 1629 const struct switchdev_obj_ipv4_fib *fib4, 1630 struct switchdev_trans *trans) 1631 { 1632 struct rocker_world_ops *wops = rocker_port->rocker->wops; 1633 1634 if (!wops->port_obj_fib4_add) 1635 return -EOPNOTSUPP; 1636 return wops->port_obj_fib4_add(rocker_port, fib4, trans); 1637 } 1638 1639 static int 1640 rocker_world_port_obj_fib4_del(struct rocker_port *rocker_port, 1641 const struct switchdev_obj_ipv4_fib *fib4) 1642 { 1643 struct rocker_world_ops *wops = rocker_port->rocker->wops; 1644 1645 if (!wops->port_obj_fib4_del) 1646 return -EOPNOTSUPP; 1647 return wops->port_obj_fib4_del(rocker_port, fib4); 1648 } 1649 1650 static int 1651 rocker_world_port_obj_fdb_add(struct rocker_port *rocker_port, 1652 const struct switchdev_obj_port_fdb *fdb, 1653 struct switchdev_trans *trans) 1654 { 1655 struct rocker_world_ops *wops = rocker_port->rocker->wops; 1656 1657 if (!wops->port_obj_fdb_add) 1658 return -EOPNOTSUPP; 1659 return wops->port_obj_fdb_add(rocker_port, fdb, trans); 1660 } 1661 1662 static int 1663 rocker_world_port_obj_fdb_del(struct rocker_port *rocker_port, 1664 const struct switchdev_obj_port_fdb *fdb) 1665 { 1666 struct rocker_world_ops *wops = rocker_port->rocker->wops; 1667 1668 if (!wops->port_obj_fdb_del) 1669 return -EOPNOTSUPP; 1670 return wops->port_obj_fdb_del(rocker_port, fdb); 1671 } 1672 1673 static int 1674 rocker_world_port_obj_fdb_dump(const struct rocker_port *rocker_port, 1675 struct switchdev_obj_port_fdb *fdb, 1676 switchdev_obj_dump_cb_t *cb) 1677 { 1678 struct rocker_world_ops *wops = rocker_port->rocker->wops; 1679 1680 if (!wops->port_obj_fdb_dump) 1681 return -EOPNOTSUPP; 1682 return wops->port_obj_fdb_dump(rocker_port, fdb, cb); 1683 } 1684 1685 static int rocker_world_port_master_linked(struct rocker_port *rocker_port, 1686 struct net_device *master) 1687 { 1688 struct rocker_world_ops *wops = rocker_port->rocker->wops; 1689 1690 if (!wops->port_master_linked) 1691 return -EOPNOTSUPP; 1692 return wops->port_master_linked(rocker_port, master); 1693 } 1694 1695 static int rocker_world_port_master_unlinked(struct rocker_port *rocker_port, 1696 struct net_device *master) 1697 { 1698 struct rocker_world_ops *wops = rocker_port->rocker->wops; 1699 1700 if (!wops->port_master_unlinked) 1701 return -EOPNOTSUPP; 1702 return wops->port_master_unlinked(rocker_port, master); 1703 } 1704 1705 static int rocker_world_port_neigh_update(struct rocker_port *rocker_port, 1706 struct neighbour *n) 1707 { 1708 struct rocker_world_ops *wops = rocker_port->rocker->wops; 1709 1710 if (!wops->port_neigh_update) 1711 return -EOPNOTSUPP; 1712 return wops->port_neigh_update(rocker_port, n); 1713 } 1714 1715 static int rocker_world_port_neigh_destroy(struct rocker_port *rocker_port, 1716 struct neighbour *n) 1717 { 1718 struct rocker_world_ops *wops = rocker_port->rocker->wops; 1719 1720 if (!wops->port_neigh_destroy) 1721 return -EOPNOTSUPP; 1722 return wops->port_neigh_destroy(rocker_port, n); 1723 } 1724 1725 static int rocker_world_port_ev_mac_vlan_seen(struct rocker_port *rocker_port, 1726 const unsigned char *addr, 1727 __be16 vlan_id) 1728 { 1729 struct rocker_world_ops *wops = rocker_port->rocker->wops; 1730 1731 if (!wops->port_ev_mac_vlan_seen) 1732 return -EOPNOTSUPP; 1733 return wops->port_ev_mac_vlan_seen(rocker_port, addr, vlan_id); 1734 } 1735 1736 /***************** 1737 * Net device ops 1738 *****************/ 1739 1740 static int rocker_port_open(struct net_device *dev) 1741 { 1742 struct rocker_port *rocker_port = netdev_priv(dev); 1743 int err; 1744 1745 err = rocker_port_dma_rings_init(rocker_port); 1746 if (err) 1747 return err; 1748 1749 err = request_irq(rocker_msix_tx_vector(rocker_port), 1750 rocker_tx_irq_handler, 0, 1751 rocker_driver_name, rocker_port); 1752 if (err) { 1753 netdev_err(rocker_port->dev, "cannot assign tx irq\n"); 1754 goto err_request_tx_irq; 1755 } 1756 1757 err = request_irq(rocker_msix_rx_vector(rocker_port), 1758 rocker_rx_irq_handler, 0, 1759 rocker_driver_name, rocker_port); 1760 if (err) { 1761 netdev_err(rocker_port->dev, "cannot assign rx irq\n"); 1762 goto err_request_rx_irq; 1763 } 1764 1765 err = rocker_world_port_open(rocker_port); 1766 if (err) { 1767 netdev_err(rocker_port->dev, "cannot open port in world\n"); 1768 goto err_world_port_open; 1769 } 1770 1771 napi_enable(&rocker_port->napi_tx); 1772 napi_enable(&rocker_port->napi_rx); 1773 if (!dev->proto_down) 1774 rocker_port_set_enable(rocker_port, true); 1775 netif_start_queue(dev); 1776 return 0; 1777 1778 err_world_port_open: 1779 free_irq(rocker_msix_rx_vector(rocker_port), rocker_port); 1780 err_request_rx_irq: 1781 free_irq(rocker_msix_tx_vector(rocker_port), rocker_port); 1782 err_request_tx_irq: 1783 rocker_port_dma_rings_fini(rocker_port); 1784 return err; 1785 } 1786 1787 static int rocker_port_stop(struct net_device *dev) 1788 { 1789 struct rocker_port *rocker_port = netdev_priv(dev); 1790 1791 netif_stop_queue(dev); 1792 rocker_port_set_enable(rocker_port, false); 1793 napi_disable(&rocker_port->napi_rx); 1794 napi_disable(&rocker_port->napi_tx); 1795 rocker_world_port_stop(rocker_port); 1796 free_irq(rocker_msix_rx_vector(rocker_port), rocker_port); 1797 free_irq(rocker_msix_tx_vector(rocker_port), rocker_port); 1798 rocker_port_dma_rings_fini(rocker_port); 1799 1800 return 0; 1801 } 1802 1803 static void rocker_tx_desc_frags_unmap(const struct rocker_port *rocker_port, 1804 const struct rocker_desc_info *desc_info) 1805 { 1806 const struct rocker *rocker = rocker_port->rocker; 1807 struct pci_dev *pdev = rocker->pdev; 1808 const struct rocker_tlv *attrs[ROCKER_TLV_TX_MAX + 1]; 1809 struct rocker_tlv *attr; 1810 int rem; 1811 1812 rocker_tlv_parse_desc(attrs, ROCKER_TLV_TX_MAX, desc_info); 1813 if (!attrs[ROCKER_TLV_TX_FRAGS]) 1814 return; 1815 rocker_tlv_for_each_nested(attr, attrs[ROCKER_TLV_TX_FRAGS], rem) { 1816 const struct rocker_tlv *frag_attrs[ROCKER_TLV_TX_FRAG_ATTR_MAX + 1]; 1817 dma_addr_t dma_handle; 1818 size_t len; 1819 1820 if (rocker_tlv_type(attr) != ROCKER_TLV_TX_FRAG) 1821 continue; 1822 rocker_tlv_parse_nested(frag_attrs, ROCKER_TLV_TX_FRAG_ATTR_MAX, 1823 attr); 1824 if (!frag_attrs[ROCKER_TLV_TX_FRAG_ATTR_ADDR] || 1825 !frag_attrs[ROCKER_TLV_TX_FRAG_ATTR_LEN]) 1826 continue; 1827 dma_handle = rocker_tlv_get_u64(frag_attrs[ROCKER_TLV_TX_FRAG_ATTR_ADDR]); 1828 len = rocker_tlv_get_u16(frag_attrs[ROCKER_TLV_TX_FRAG_ATTR_LEN]); 1829 pci_unmap_single(pdev, dma_handle, len, DMA_TO_DEVICE); 1830 } 1831 } 1832 1833 static int rocker_tx_desc_frag_map_put(const struct rocker_port *rocker_port, 1834 struct rocker_desc_info *desc_info, 1835 char *buf, size_t buf_len) 1836 { 1837 const struct rocker *rocker = rocker_port->rocker; 1838 struct pci_dev *pdev = rocker->pdev; 1839 dma_addr_t dma_handle; 1840 struct rocker_tlv *frag; 1841 1842 dma_handle = pci_map_single(pdev, buf, buf_len, DMA_TO_DEVICE); 1843 if (unlikely(pci_dma_mapping_error(pdev, dma_handle))) { 1844 if (net_ratelimit()) 1845 netdev_err(rocker_port->dev, "failed to dma map tx frag\n"); 1846 return -EIO; 1847 } 1848 frag = rocker_tlv_nest_start(desc_info, ROCKER_TLV_TX_FRAG); 1849 if (!frag) 1850 goto unmap_frag; 1851 if (rocker_tlv_put_u64(desc_info, ROCKER_TLV_TX_FRAG_ATTR_ADDR, 1852 dma_handle)) 1853 goto nest_cancel; 1854 if (rocker_tlv_put_u16(desc_info, ROCKER_TLV_TX_FRAG_ATTR_LEN, 1855 buf_len)) 1856 goto nest_cancel; 1857 rocker_tlv_nest_end(desc_info, frag); 1858 return 0; 1859 1860 nest_cancel: 1861 rocker_tlv_nest_cancel(desc_info, frag); 1862 unmap_frag: 1863 pci_unmap_single(pdev, dma_handle, buf_len, DMA_TO_DEVICE); 1864 return -EMSGSIZE; 1865 } 1866 1867 static netdev_tx_t rocker_port_xmit(struct sk_buff *skb, struct net_device *dev) 1868 { 1869 struct rocker_port *rocker_port = netdev_priv(dev); 1870 struct rocker *rocker = rocker_port->rocker; 1871 struct rocker_desc_info *desc_info; 1872 struct rocker_tlv *frags; 1873 int i; 1874 int err; 1875 1876 desc_info = rocker_desc_head_get(&rocker_port->tx_ring); 1877 if (unlikely(!desc_info)) { 1878 if (net_ratelimit()) 1879 netdev_err(dev, "tx ring full when queue awake\n"); 1880 return NETDEV_TX_BUSY; 1881 } 1882 1883 rocker_desc_cookie_ptr_set(desc_info, skb); 1884 1885 frags = rocker_tlv_nest_start(desc_info, ROCKER_TLV_TX_FRAGS); 1886 if (!frags) 1887 goto out; 1888 err = rocker_tx_desc_frag_map_put(rocker_port, desc_info, 1889 skb->data, skb_headlen(skb)); 1890 if (err) 1891 goto nest_cancel; 1892 if (skb_shinfo(skb)->nr_frags > ROCKER_TX_FRAGS_MAX) { 1893 err = skb_linearize(skb); 1894 if (err) 1895 goto unmap_frags; 1896 } 1897 1898 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { 1899 const skb_frag_t *frag = &skb_shinfo(skb)->frags[i]; 1900 1901 err = rocker_tx_desc_frag_map_put(rocker_port, desc_info, 1902 skb_frag_address(frag), 1903 skb_frag_size(frag)); 1904 if (err) 1905 goto unmap_frags; 1906 } 1907 rocker_tlv_nest_end(desc_info, frags); 1908 1909 rocker_desc_gen_clear(desc_info); 1910 rocker_desc_head_set(rocker, &rocker_port->tx_ring, desc_info); 1911 1912 desc_info = rocker_desc_head_get(&rocker_port->tx_ring); 1913 if (!desc_info) 1914 netif_stop_queue(dev); 1915 1916 return NETDEV_TX_OK; 1917 1918 unmap_frags: 1919 rocker_tx_desc_frags_unmap(rocker_port, desc_info); 1920 nest_cancel: 1921 rocker_tlv_nest_cancel(desc_info, frags); 1922 out: 1923 dev_kfree_skb(skb); 1924 dev->stats.tx_dropped++; 1925 1926 return NETDEV_TX_OK; 1927 } 1928 1929 static int rocker_port_set_mac_address(struct net_device *dev, void *p) 1930 { 1931 struct sockaddr *addr = p; 1932 struct rocker_port *rocker_port = netdev_priv(dev); 1933 int err; 1934 1935 if (!is_valid_ether_addr(addr->sa_data)) 1936 return -EADDRNOTAVAIL; 1937 1938 err = rocker_cmd_set_port_settings_macaddr(rocker_port, addr->sa_data); 1939 if (err) 1940 return err; 1941 memcpy(dev->dev_addr, addr->sa_data, dev->addr_len); 1942 return 0; 1943 } 1944 1945 static int rocker_port_change_mtu(struct net_device *dev, int new_mtu) 1946 { 1947 struct rocker_port *rocker_port = netdev_priv(dev); 1948 int running = netif_running(dev); 1949 int err; 1950 1951 #define ROCKER_PORT_MIN_MTU 68 1952 #define ROCKER_PORT_MAX_MTU 9000 1953 1954 if (new_mtu < ROCKER_PORT_MIN_MTU || new_mtu > ROCKER_PORT_MAX_MTU) 1955 return -EINVAL; 1956 1957 if (running) 1958 rocker_port_stop(dev); 1959 1960 netdev_info(dev, "MTU change from %d to %d\n", dev->mtu, new_mtu); 1961 dev->mtu = new_mtu; 1962 1963 err = rocker_cmd_set_port_settings_mtu(rocker_port, new_mtu); 1964 if (err) 1965 return err; 1966 1967 if (running) 1968 err = rocker_port_open(dev); 1969 1970 return err; 1971 } 1972 1973 static int rocker_port_get_phys_port_name(struct net_device *dev, 1974 char *buf, size_t len) 1975 { 1976 struct rocker_port *rocker_port = netdev_priv(dev); 1977 struct port_name name = { .buf = buf, .len = len }; 1978 int err; 1979 1980 err = rocker_cmd_exec(rocker_port, false, 1981 rocker_cmd_get_port_settings_prep, NULL, 1982 rocker_cmd_get_port_settings_phys_name_proc, 1983 &name); 1984 1985 return err ? -EOPNOTSUPP : 0; 1986 } 1987 1988 static int rocker_port_change_proto_down(struct net_device *dev, 1989 bool proto_down) 1990 { 1991 struct rocker_port *rocker_port = netdev_priv(dev); 1992 1993 if (rocker_port->dev->flags & IFF_UP) 1994 rocker_port_set_enable(rocker_port, !proto_down); 1995 rocker_port->dev->proto_down = proto_down; 1996 return 0; 1997 } 1998 1999 static void rocker_port_neigh_destroy(struct neighbour *n) 2000 { 2001 struct rocker_port *rocker_port = netdev_priv(n->dev); 2002 int err; 2003 2004 err = rocker_world_port_neigh_destroy(rocker_port, n); 2005 if (err) 2006 netdev_warn(rocker_port->dev, "failed to handle neigh destroy (err %d)\n", 2007 err); 2008 } 2009 2010 static const struct net_device_ops rocker_port_netdev_ops = { 2011 .ndo_open = rocker_port_open, 2012 .ndo_stop = rocker_port_stop, 2013 .ndo_start_xmit = rocker_port_xmit, 2014 .ndo_set_mac_address = rocker_port_set_mac_address, 2015 .ndo_change_mtu = rocker_port_change_mtu, 2016 .ndo_bridge_getlink = switchdev_port_bridge_getlink, 2017 .ndo_bridge_setlink = switchdev_port_bridge_setlink, 2018 .ndo_bridge_dellink = switchdev_port_bridge_dellink, 2019 .ndo_fdb_add = switchdev_port_fdb_add, 2020 .ndo_fdb_del = switchdev_port_fdb_del, 2021 .ndo_fdb_dump = switchdev_port_fdb_dump, 2022 .ndo_get_phys_port_name = rocker_port_get_phys_port_name, 2023 .ndo_change_proto_down = rocker_port_change_proto_down, 2024 .ndo_neigh_destroy = rocker_port_neigh_destroy, 2025 }; 2026 2027 /******************** 2028 * swdev interface 2029 ********************/ 2030 2031 static int rocker_port_attr_get(struct net_device *dev, 2032 struct switchdev_attr *attr) 2033 { 2034 const struct rocker_port *rocker_port = netdev_priv(dev); 2035 const struct rocker *rocker = rocker_port->rocker; 2036 int err = 0; 2037 2038 switch (attr->id) { 2039 case SWITCHDEV_ATTR_ID_PORT_PARENT_ID: 2040 attr->u.ppid.id_len = sizeof(rocker->hw.id); 2041 memcpy(&attr->u.ppid.id, &rocker->hw.id, attr->u.ppid.id_len); 2042 break; 2043 case SWITCHDEV_ATTR_ID_PORT_BRIDGE_FLAGS: 2044 err = rocker_world_port_attr_bridge_flags_get(rocker_port, 2045 &attr->u.brport_flags); 2046 break; 2047 default: 2048 return -EOPNOTSUPP; 2049 } 2050 2051 return err; 2052 } 2053 2054 static int rocker_port_attr_set(struct net_device *dev, 2055 const struct switchdev_attr *attr, 2056 struct switchdev_trans *trans) 2057 { 2058 struct rocker_port *rocker_port = netdev_priv(dev); 2059 int err = 0; 2060 2061 switch (attr->id) { 2062 case SWITCHDEV_ATTR_ID_PORT_STP_STATE: 2063 err = rocker_world_port_attr_stp_state_set(rocker_port, 2064 attr->u.stp_state, 2065 trans); 2066 break; 2067 case SWITCHDEV_ATTR_ID_PORT_BRIDGE_FLAGS: 2068 err = rocker_world_port_attr_bridge_flags_set(rocker_port, 2069 attr->u.brport_flags, 2070 trans); 2071 break; 2072 case SWITCHDEV_ATTR_ID_BRIDGE_AGEING_TIME: 2073 err = rocker_world_port_attr_bridge_ageing_time_set(rocker_port, 2074 attr->u.ageing_time, 2075 trans); 2076 break; 2077 default: 2078 err = -EOPNOTSUPP; 2079 break; 2080 } 2081 2082 return err; 2083 } 2084 2085 static int rocker_port_obj_add(struct net_device *dev, 2086 const struct switchdev_obj *obj, 2087 struct switchdev_trans *trans) 2088 { 2089 struct rocker_port *rocker_port = netdev_priv(dev); 2090 int err = 0; 2091 2092 switch (obj->id) { 2093 case SWITCHDEV_OBJ_ID_PORT_VLAN: 2094 err = rocker_world_port_obj_vlan_add(rocker_port, 2095 SWITCHDEV_OBJ_PORT_VLAN(obj), 2096 trans); 2097 break; 2098 case SWITCHDEV_OBJ_ID_IPV4_FIB: 2099 err = rocker_world_port_obj_fib4_add(rocker_port, 2100 SWITCHDEV_OBJ_IPV4_FIB(obj), 2101 trans); 2102 break; 2103 case SWITCHDEV_OBJ_ID_PORT_FDB: 2104 err = rocker_world_port_obj_fdb_add(rocker_port, 2105 SWITCHDEV_OBJ_PORT_FDB(obj), 2106 trans); 2107 break; 2108 default: 2109 err = -EOPNOTSUPP; 2110 break; 2111 } 2112 2113 return err; 2114 } 2115 2116 static int rocker_port_obj_del(struct net_device *dev, 2117 const struct switchdev_obj *obj) 2118 { 2119 struct rocker_port *rocker_port = netdev_priv(dev); 2120 int err = 0; 2121 2122 switch (obj->id) { 2123 case SWITCHDEV_OBJ_ID_PORT_VLAN: 2124 err = rocker_world_port_obj_vlan_del(rocker_port, 2125 SWITCHDEV_OBJ_PORT_VLAN(obj)); 2126 break; 2127 case SWITCHDEV_OBJ_ID_IPV4_FIB: 2128 err = rocker_world_port_obj_fib4_del(rocker_port, 2129 SWITCHDEV_OBJ_IPV4_FIB(obj)); 2130 break; 2131 case SWITCHDEV_OBJ_ID_PORT_FDB: 2132 err = rocker_world_port_obj_fdb_del(rocker_port, 2133 SWITCHDEV_OBJ_PORT_FDB(obj)); 2134 break; 2135 default: 2136 err = -EOPNOTSUPP; 2137 break; 2138 } 2139 2140 return err; 2141 } 2142 2143 static int rocker_port_obj_dump(struct net_device *dev, 2144 struct switchdev_obj *obj, 2145 switchdev_obj_dump_cb_t *cb) 2146 { 2147 const struct rocker_port *rocker_port = netdev_priv(dev); 2148 int err = 0; 2149 2150 switch (obj->id) { 2151 case SWITCHDEV_OBJ_ID_PORT_FDB: 2152 err = rocker_world_port_obj_fdb_dump(rocker_port, 2153 SWITCHDEV_OBJ_PORT_FDB(obj), 2154 cb); 2155 break; 2156 case SWITCHDEV_OBJ_ID_PORT_VLAN: 2157 err = rocker_world_port_obj_vlan_dump(rocker_port, 2158 SWITCHDEV_OBJ_PORT_VLAN(obj), 2159 cb); 2160 break; 2161 default: 2162 err = -EOPNOTSUPP; 2163 break; 2164 } 2165 2166 return err; 2167 } 2168 2169 static const struct switchdev_ops rocker_port_switchdev_ops = { 2170 .switchdev_port_attr_get = rocker_port_attr_get, 2171 .switchdev_port_attr_set = rocker_port_attr_set, 2172 .switchdev_port_obj_add = rocker_port_obj_add, 2173 .switchdev_port_obj_del = rocker_port_obj_del, 2174 .switchdev_port_obj_dump = rocker_port_obj_dump, 2175 }; 2176 2177 /******************** 2178 * ethtool interface 2179 ********************/ 2180 2181 static int rocker_port_get_settings(struct net_device *dev, 2182 struct ethtool_cmd *ecmd) 2183 { 2184 struct rocker_port *rocker_port = netdev_priv(dev); 2185 2186 return rocker_cmd_get_port_settings_ethtool(rocker_port, ecmd); 2187 } 2188 2189 static int rocker_port_set_settings(struct net_device *dev, 2190 struct ethtool_cmd *ecmd) 2191 { 2192 struct rocker_port *rocker_port = netdev_priv(dev); 2193 2194 return rocker_cmd_set_port_settings_ethtool(rocker_port, ecmd); 2195 } 2196 2197 static void rocker_port_get_drvinfo(struct net_device *dev, 2198 struct ethtool_drvinfo *drvinfo) 2199 { 2200 strlcpy(drvinfo->driver, rocker_driver_name, sizeof(drvinfo->driver)); 2201 strlcpy(drvinfo->version, UTS_RELEASE, sizeof(drvinfo->version)); 2202 } 2203 2204 static struct rocker_port_stats { 2205 char str[ETH_GSTRING_LEN]; 2206 int type; 2207 } rocker_port_stats[] = { 2208 { "rx_packets", ROCKER_TLV_CMD_PORT_STATS_RX_PKTS, }, 2209 { "rx_bytes", ROCKER_TLV_CMD_PORT_STATS_RX_BYTES, }, 2210 { "rx_dropped", ROCKER_TLV_CMD_PORT_STATS_RX_DROPPED, }, 2211 { "rx_errors", ROCKER_TLV_CMD_PORT_STATS_RX_ERRORS, }, 2212 2213 { "tx_packets", ROCKER_TLV_CMD_PORT_STATS_TX_PKTS, }, 2214 { "tx_bytes", ROCKER_TLV_CMD_PORT_STATS_TX_BYTES, }, 2215 { "tx_dropped", ROCKER_TLV_CMD_PORT_STATS_TX_DROPPED, }, 2216 { "tx_errors", ROCKER_TLV_CMD_PORT_STATS_TX_ERRORS, }, 2217 }; 2218 2219 #define ROCKER_PORT_STATS_LEN ARRAY_SIZE(rocker_port_stats) 2220 2221 static void rocker_port_get_strings(struct net_device *netdev, u32 stringset, 2222 u8 *data) 2223 { 2224 u8 *p = data; 2225 int i; 2226 2227 switch (stringset) { 2228 case ETH_SS_STATS: 2229 for (i = 0; i < ARRAY_SIZE(rocker_port_stats); i++) { 2230 memcpy(p, rocker_port_stats[i].str, ETH_GSTRING_LEN); 2231 p += ETH_GSTRING_LEN; 2232 } 2233 break; 2234 } 2235 } 2236 2237 static int 2238 rocker_cmd_get_port_stats_prep(const struct rocker_port *rocker_port, 2239 struct rocker_desc_info *desc_info, 2240 void *priv) 2241 { 2242 struct rocker_tlv *cmd_stats; 2243 2244 if (rocker_tlv_put_u16(desc_info, ROCKER_TLV_CMD_TYPE, 2245 ROCKER_TLV_CMD_TYPE_GET_PORT_STATS)) 2246 return -EMSGSIZE; 2247 2248 cmd_stats = rocker_tlv_nest_start(desc_info, ROCKER_TLV_CMD_INFO); 2249 if (!cmd_stats) 2250 return -EMSGSIZE; 2251 2252 if (rocker_tlv_put_u32(desc_info, ROCKER_TLV_CMD_PORT_STATS_PPORT, 2253 rocker_port->pport)) 2254 return -EMSGSIZE; 2255 2256 rocker_tlv_nest_end(desc_info, cmd_stats); 2257 2258 return 0; 2259 } 2260 2261 static int 2262 rocker_cmd_get_port_stats_ethtool_proc(const struct rocker_port *rocker_port, 2263 const struct rocker_desc_info *desc_info, 2264 void *priv) 2265 { 2266 const struct rocker_tlv *attrs[ROCKER_TLV_CMD_MAX + 1]; 2267 const struct rocker_tlv *stats_attrs[ROCKER_TLV_CMD_PORT_STATS_MAX + 1]; 2268 const struct rocker_tlv *pattr; 2269 u32 pport; 2270 u64 *data = priv; 2271 int i; 2272 2273 rocker_tlv_parse_desc(attrs, ROCKER_TLV_CMD_MAX, desc_info); 2274 2275 if (!attrs[ROCKER_TLV_CMD_INFO]) 2276 return -EIO; 2277 2278 rocker_tlv_parse_nested(stats_attrs, ROCKER_TLV_CMD_PORT_STATS_MAX, 2279 attrs[ROCKER_TLV_CMD_INFO]); 2280 2281 if (!stats_attrs[ROCKER_TLV_CMD_PORT_STATS_PPORT]) 2282 return -EIO; 2283 2284 pport = rocker_tlv_get_u32(stats_attrs[ROCKER_TLV_CMD_PORT_STATS_PPORT]); 2285 if (pport != rocker_port->pport) 2286 return -EIO; 2287 2288 for (i = 0; i < ARRAY_SIZE(rocker_port_stats); i++) { 2289 pattr = stats_attrs[rocker_port_stats[i].type]; 2290 if (!pattr) 2291 continue; 2292 2293 data[i] = rocker_tlv_get_u64(pattr); 2294 } 2295 2296 return 0; 2297 } 2298 2299 static int rocker_cmd_get_port_stats_ethtool(struct rocker_port *rocker_port, 2300 void *priv) 2301 { 2302 return rocker_cmd_exec(rocker_port, false, 2303 rocker_cmd_get_port_stats_prep, NULL, 2304 rocker_cmd_get_port_stats_ethtool_proc, 2305 priv); 2306 } 2307 2308 static void rocker_port_get_stats(struct net_device *dev, 2309 struct ethtool_stats *stats, u64 *data) 2310 { 2311 struct rocker_port *rocker_port = netdev_priv(dev); 2312 2313 if (rocker_cmd_get_port_stats_ethtool(rocker_port, data) != 0) { 2314 int i; 2315 2316 for (i = 0; i < ARRAY_SIZE(rocker_port_stats); ++i) 2317 data[i] = 0; 2318 } 2319 } 2320 2321 static int rocker_port_get_sset_count(struct net_device *netdev, int sset) 2322 { 2323 switch (sset) { 2324 case ETH_SS_STATS: 2325 return ROCKER_PORT_STATS_LEN; 2326 default: 2327 return -EOPNOTSUPP; 2328 } 2329 } 2330 2331 static const struct ethtool_ops rocker_port_ethtool_ops = { 2332 .get_settings = rocker_port_get_settings, 2333 .set_settings = rocker_port_set_settings, 2334 .get_drvinfo = rocker_port_get_drvinfo, 2335 .get_link = ethtool_op_get_link, 2336 .get_strings = rocker_port_get_strings, 2337 .get_ethtool_stats = rocker_port_get_stats, 2338 .get_sset_count = rocker_port_get_sset_count, 2339 }; 2340 2341 /***************** 2342 * NAPI interface 2343 *****************/ 2344 2345 static struct rocker_port *rocker_port_napi_tx_get(struct napi_struct *napi) 2346 { 2347 return container_of(napi, struct rocker_port, napi_tx); 2348 } 2349 2350 static int rocker_port_poll_tx(struct napi_struct *napi, int budget) 2351 { 2352 struct rocker_port *rocker_port = rocker_port_napi_tx_get(napi); 2353 const struct rocker *rocker = rocker_port->rocker; 2354 const struct rocker_desc_info *desc_info; 2355 u32 credits = 0; 2356 int err; 2357 2358 /* Cleanup tx descriptors */ 2359 while ((desc_info = rocker_desc_tail_get(&rocker_port->tx_ring))) { 2360 struct sk_buff *skb; 2361 2362 err = rocker_desc_err(desc_info); 2363 if (err && net_ratelimit()) 2364 netdev_err(rocker_port->dev, "tx desc received with err %d\n", 2365 err); 2366 rocker_tx_desc_frags_unmap(rocker_port, desc_info); 2367 2368 skb = rocker_desc_cookie_ptr_get(desc_info); 2369 if (err == 0) { 2370 rocker_port->dev->stats.tx_packets++; 2371 rocker_port->dev->stats.tx_bytes += skb->len; 2372 } else { 2373 rocker_port->dev->stats.tx_errors++; 2374 } 2375 2376 dev_kfree_skb_any(skb); 2377 credits++; 2378 } 2379 2380 if (credits && netif_queue_stopped(rocker_port->dev)) 2381 netif_wake_queue(rocker_port->dev); 2382 2383 napi_complete(napi); 2384 rocker_dma_ring_credits_set(rocker, &rocker_port->tx_ring, credits); 2385 2386 return 0; 2387 } 2388 2389 static int rocker_port_rx_proc(const struct rocker *rocker, 2390 const struct rocker_port *rocker_port, 2391 struct rocker_desc_info *desc_info) 2392 { 2393 const struct rocker_tlv *attrs[ROCKER_TLV_RX_MAX + 1]; 2394 struct sk_buff *skb = rocker_desc_cookie_ptr_get(desc_info); 2395 size_t rx_len; 2396 u16 rx_flags = 0; 2397 2398 if (!skb) 2399 return -ENOENT; 2400 2401 rocker_tlv_parse_desc(attrs, ROCKER_TLV_RX_MAX, desc_info); 2402 if (!attrs[ROCKER_TLV_RX_FRAG_LEN]) 2403 return -EINVAL; 2404 if (attrs[ROCKER_TLV_RX_FLAGS]) 2405 rx_flags = rocker_tlv_get_u16(attrs[ROCKER_TLV_RX_FLAGS]); 2406 2407 rocker_dma_rx_ring_skb_unmap(rocker, attrs); 2408 2409 rx_len = rocker_tlv_get_u16(attrs[ROCKER_TLV_RX_FRAG_LEN]); 2410 skb_put(skb, rx_len); 2411 skb->protocol = eth_type_trans(skb, rocker_port->dev); 2412 2413 if (rx_flags & ROCKER_RX_FLAGS_FWD_OFFLOAD) 2414 skb->offload_fwd_mark = rocker_port->dev->offload_fwd_mark; 2415 2416 rocker_port->dev->stats.rx_packets++; 2417 rocker_port->dev->stats.rx_bytes += skb->len; 2418 2419 netif_receive_skb(skb); 2420 2421 return rocker_dma_rx_ring_skb_alloc(rocker_port, desc_info); 2422 } 2423 2424 static struct rocker_port *rocker_port_napi_rx_get(struct napi_struct *napi) 2425 { 2426 return container_of(napi, struct rocker_port, napi_rx); 2427 } 2428 2429 static int rocker_port_poll_rx(struct napi_struct *napi, int budget) 2430 { 2431 struct rocker_port *rocker_port = rocker_port_napi_rx_get(napi); 2432 const struct rocker *rocker = rocker_port->rocker; 2433 struct rocker_desc_info *desc_info; 2434 u32 credits = 0; 2435 int err; 2436 2437 /* Process rx descriptors */ 2438 while (credits < budget && 2439 (desc_info = rocker_desc_tail_get(&rocker_port->rx_ring))) { 2440 err = rocker_desc_err(desc_info); 2441 if (err) { 2442 if (net_ratelimit()) 2443 netdev_err(rocker_port->dev, "rx desc received with err %d\n", 2444 err); 2445 } else { 2446 err = rocker_port_rx_proc(rocker, rocker_port, 2447 desc_info); 2448 if (err && net_ratelimit()) 2449 netdev_err(rocker_port->dev, "rx processing failed with err %d\n", 2450 err); 2451 } 2452 if (err) 2453 rocker_port->dev->stats.rx_errors++; 2454 2455 rocker_desc_gen_clear(desc_info); 2456 rocker_desc_head_set(rocker, &rocker_port->rx_ring, desc_info); 2457 credits++; 2458 } 2459 2460 if (credits < budget) 2461 napi_complete(napi); 2462 2463 rocker_dma_ring_credits_set(rocker, &rocker_port->rx_ring, credits); 2464 2465 return credits; 2466 } 2467 2468 /***************** 2469 * PCI driver ops 2470 *****************/ 2471 2472 static void rocker_carrier_init(const struct rocker_port *rocker_port) 2473 { 2474 const struct rocker *rocker = rocker_port->rocker; 2475 u64 link_status = rocker_read64(rocker, PORT_PHYS_LINK_STATUS); 2476 bool link_up; 2477 2478 link_up = link_status & (1 << rocker_port->pport); 2479 if (link_up) 2480 netif_carrier_on(rocker_port->dev); 2481 else 2482 netif_carrier_off(rocker_port->dev); 2483 } 2484 2485 static void rocker_remove_ports(struct rocker *rocker) 2486 { 2487 struct rocker_port *rocker_port; 2488 int i; 2489 2490 for (i = 0; i < rocker->port_count; i++) { 2491 rocker_port = rocker->ports[i]; 2492 if (!rocker_port) 2493 continue; 2494 rocker_world_port_fini(rocker_port); 2495 unregister_netdev(rocker_port->dev); 2496 rocker_world_port_post_fini(rocker_port); 2497 free_netdev(rocker_port->dev); 2498 } 2499 rocker_world_fini(rocker); 2500 kfree(rocker->ports); 2501 } 2502 2503 static void rocker_port_dev_addr_init(struct rocker_port *rocker_port) 2504 { 2505 const struct rocker *rocker = rocker_port->rocker; 2506 const struct pci_dev *pdev = rocker->pdev; 2507 int err; 2508 2509 err = rocker_cmd_get_port_settings_macaddr(rocker_port, 2510 rocker_port->dev->dev_addr); 2511 if (err) { 2512 dev_warn(&pdev->dev, "failed to get mac address, using random\n"); 2513 eth_hw_addr_random(rocker_port->dev); 2514 } 2515 } 2516 2517 static int rocker_probe_port(struct rocker *rocker, unsigned int port_number) 2518 { 2519 const struct pci_dev *pdev = rocker->pdev; 2520 struct rocker_port *rocker_port; 2521 struct net_device *dev; 2522 int err; 2523 2524 dev = alloc_etherdev(sizeof(struct rocker_port)); 2525 if (!dev) 2526 return -ENOMEM; 2527 rocker_port = netdev_priv(dev); 2528 rocker_port->dev = dev; 2529 rocker_port->rocker = rocker; 2530 rocker_port->port_number = port_number; 2531 rocker_port->pport = port_number + 1; 2532 2533 err = rocker_world_check_init(rocker_port); 2534 if (err) { 2535 dev_err(&pdev->dev, "world init failed\n"); 2536 goto err_world_check_init; 2537 } 2538 2539 rocker_port_dev_addr_init(rocker_port); 2540 dev->netdev_ops = &rocker_port_netdev_ops; 2541 dev->ethtool_ops = &rocker_port_ethtool_ops; 2542 dev->switchdev_ops = &rocker_port_switchdev_ops; 2543 netif_tx_napi_add(dev, &rocker_port->napi_tx, rocker_port_poll_tx, 2544 NAPI_POLL_WEIGHT); 2545 netif_napi_add(dev, &rocker_port->napi_rx, rocker_port_poll_rx, 2546 NAPI_POLL_WEIGHT); 2547 rocker_carrier_init(rocker_port); 2548 2549 dev->features |= NETIF_F_NETNS_LOCAL | NETIF_F_SG; 2550 2551 err = rocker_world_port_pre_init(rocker_port); 2552 if (err) { 2553 dev_err(&pdev->dev, "port world pre-init failed\n"); 2554 goto err_world_port_pre_init; 2555 } 2556 err = register_netdev(dev); 2557 if (err) { 2558 dev_err(&pdev->dev, "register_netdev failed\n"); 2559 goto err_register_netdev; 2560 } 2561 rocker->ports[port_number] = rocker_port; 2562 2563 err = rocker_world_port_init(rocker_port); 2564 if (err) { 2565 dev_err(&pdev->dev, "port world init failed\n"); 2566 goto err_world_port_init; 2567 } 2568 2569 return 0; 2570 2571 err_world_port_init: 2572 rocker->ports[port_number] = NULL; 2573 unregister_netdev(dev); 2574 err_register_netdev: 2575 rocker_world_port_post_fini(rocker_port); 2576 err_world_port_pre_init: 2577 err_world_check_init: 2578 free_netdev(dev); 2579 return err; 2580 } 2581 2582 static int rocker_probe_ports(struct rocker *rocker) 2583 { 2584 int i; 2585 size_t alloc_size; 2586 int err; 2587 2588 alloc_size = sizeof(struct rocker_port *) * rocker->port_count; 2589 rocker->ports = kzalloc(alloc_size, GFP_KERNEL); 2590 if (!rocker->ports) 2591 return -ENOMEM; 2592 for (i = 0; i < rocker->port_count; i++) { 2593 err = rocker_probe_port(rocker, i); 2594 if (err) 2595 goto remove_ports; 2596 } 2597 return 0; 2598 2599 remove_ports: 2600 rocker_remove_ports(rocker); 2601 return err; 2602 } 2603 2604 static int rocker_msix_init(struct rocker *rocker) 2605 { 2606 struct pci_dev *pdev = rocker->pdev; 2607 int msix_entries; 2608 int i; 2609 int err; 2610 2611 msix_entries = pci_msix_vec_count(pdev); 2612 if (msix_entries < 0) 2613 return msix_entries; 2614 2615 if (msix_entries != ROCKER_MSIX_VEC_COUNT(rocker->port_count)) 2616 return -EINVAL; 2617 2618 rocker->msix_entries = kmalloc_array(msix_entries, 2619 sizeof(struct msix_entry), 2620 GFP_KERNEL); 2621 if (!rocker->msix_entries) 2622 return -ENOMEM; 2623 2624 for (i = 0; i < msix_entries; i++) 2625 rocker->msix_entries[i].entry = i; 2626 2627 err = pci_enable_msix_exact(pdev, rocker->msix_entries, msix_entries); 2628 if (err < 0) 2629 goto err_enable_msix; 2630 2631 return 0; 2632 2633 err_enable_msix: 2634 kfree(rocker->msix_entries); 2635 return err; 2636 } 2637 2638 static void rocker_msix_fini(const struct rocker *rocker) 2639 { 2640 pci_disable_msix(rocker->pdev); 2641 kfree(rocker->msix_entries); 2642 } 2643 2644 static int rocker_probe(struct pci_dev *pdev, const struct pci_device_id *id) 2645 { 2646 struct rocker *rocker; 2647 int err; 2648 2649 rocker = kzalloc(sizeof(*rocker), GFP_KERNEL); 2650 if (!rocker) 2651 return -ENOMEM; 2652 2653 err = pci_enable_device(pdev); 2654 if (err) { 2655 dev_err(&pdev->dev, "pci_enable_device failed\n"); 2656 goto err_pci_enable_device; 2657 } 2658 2659 err = pci_request_regions(pdev, rocker_driver_name); 2660 if (err) { 2661 dev_err(&pdev->dev, "pci_request_regions failed\n"); 2662 goto err_pci_request_regions; 2663 } 2664 2665 err = pci_set_dma_mask(pdev, DMA_BIT_MASK(64)); 2666 if (!err) { 2667 err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64)); 2668 if (err) { 2669 dev_err(&pdev->dev, "pci_set_consistent_dma_mask failed\n"); 2670 goto err_pci_set_dma_mask; 2671 } 2672 } else { 2673 err = pci_set_dma_mask(pdev, DMA_BIT_MASK(32)); 2674 if (err) { 2675 dev_err(&pdev->dev, "pci_set_dma_mask failed\n"); 2676 goto err_pci_set_dma_mask; 2677 } 2678 } 2679 2680 if (pci_resource_len(pdev, 0) < ROCKER_PCI_BAR0_SIZE) { 2681 dev_err(&pdev->dev, "invalid PCI region size\n"); 2682 err = -EINVAL; 2683 goto err_pci_resource_len_check; 2684 } 2685 2686 rocker->hw_addr = ioremap(pci_resource_start(pdev, 0), 2687 pci_resource_len(pdev, 0)); 2688 if (!rocker->hw_addr) { 2689 dev_err(&pdev->dev, "ioremap failed\n"); 2690 err = -EIO; 2691 goto err_ioremap; 2692 } 2693 pci_set_master(pdev); 2694 2695 rocker->pdev = pdev; 2696 pci_set_drvdata(pdev, rocker); 2697 2698 rocker->port_count = rocker_read32(rocker, PORT_PHYS_COUNT); 2699 2700 err = rocker_msix_init(rocker); 2701 if (err) { 2702 dev_err(&pdev->dev, "MSI-X init failed\n"); 2703 goto err_msix_init; 2704 } 2705 2706 err = rocker_basic_hw_test(rocker); 2707 if (err) { 2708 dev_err(&pdev->dev, "basic hw test failed\n"); 2709 goto err_basic_hw_test; 2710 } 2711 2712 rocker_write32(rocker, CONTROL, ROCKER_CONTROL_RESET); 2713 2714 err = rocker_dma_rings_init(rocker); 2715 if (err) 2716 goto err_dma_rings_init; 2717 2718 err = request_irq(rocker_msix_vector(rocker, ROCKER_MSIX_VEC_CMD), 2719 rocker_cmd_irq_handler, 0, 2720 rocker_driver_name, rocker); 2721 if (err) { 2722 dev_err(&pdev->dev, "cannot assign cmd irq\n"); 2723 goto err_request_cmd_irq; 2724 } 2725 2726 err = request_irq(rocker_msix_vector(rocker, ROCKER_MSIX_VEC_EVENT), 2727 rocker_event_irq_handler, 0, 2728 rocker_driver_name, rocker); 2729 if (err) { 2730 dev_err(&pdev->dev, "cannot assign event irq\n"); 2731 goto err_request_event_irq; 2732 } 2733 2734 rocker->hw.id = rocker_read64(rocker, SWITCH_ID); 2735 2736 err = rocker_probe_ports(rocker); 2737 if (err) { 2738 dev_err(&pdev->dev, "failed to probe ports\n"); 2739 goto err_probe_ports; 2740 } 2741 2742 dev_info(&pdev->dev, "Rocker switch with id %*phN\n", 2743 (int)sizeof(rocker->hw.id), &rocker->hw.id); 2744 2745 return 0; 2746 2747 err_probe_ports: 2748 free_irq(rocker_msix_vector(rocker, ROCKER_MSIX_VEC_EVENT), rocker); 2749 err_request_event_irq: 2750 free_irq(rocker_msix_vector(rocker, ROCKER_MSIX_VEC_CMD), rocker); 2751 err_request_cmd_irq: 2752 rocker_dma_rings_fini(rocker); 2753 err_dma_rings_init: 2754 err_basic_hw_test: 2755 rocker_msix_fini(rocker); 2756 err_msix_init: 2757 iounmap(rocker->hw_addr); 2758 err_ioremap: 2759 err_pci_resource_len_check: 2760 err_pci_set_dma_mask: 2761 pci_release_regions(pdev); 2762 err_pci_request_regions: 2763 pci_disable_device(pdev); 2764 err_pci_enable_device: 2765 kfree(rocker); 2766 return err; 2767 } 2768 2769 static void rocker_remove(struct pci_dev *pdev) 2770 { 2771 struct rocker *rocker = pci_get_drvdata(pdev); 2772 2773 rocker_write32(rocker, CONTROL, ROCKER_CONTROL_RESET); 2774 rocker_remove_ports(rocker); 2775 free_irq(rocker_msix_vector(rocker, ROCKER_MSIX_VEC_EVENT), rocker); 2776 free_irq(rocker_msix_vector(rocker, ROCKER_MSIX_VEC_CMD), rocker); 2777 rocker_dma_rings_fini(rocker); 2778 rocker_msix_fini(rocker); 2779 iounmap(rocker->hw_addr); 2780 pci_release_regions(rocker->pdev); 2781 pci_disable_device(rocker->pdev); 2782 kfree(rocker); 2783 } 2784 2785 static struct pci_driver rocker_pci_driver = { 2786 .name = rocker_driver_name, 2787 .id_table = rocker_pci_id_table, 2788 .probe = rocker_probe, 2789 .remove = rocker_remove, 2790 }; 2791 2792 /************************************ 2793 * Net device notifier event handler 2794 ************************************/ 2795 2796 static bool rocker_port_dev_check(const struct net_device *dev) 2797 { 2798 return dev->netdev_ops == &rocker_port_netdev_ops; 2799 } 2800 2801 static int rocker_netdevice_event(struct notifier_block *unused, 2802 unsigned long event, void *ptr) 2803 { 2804 struct net_device *dev = netdev_notifier_info_to_dev(ptr); 2805 struct netdev_notifier_changeupper_info *info; 2806 struct rocker_port *rocker_port; 2807 int err; 2808 2809 if (!rocker_port_dev_check(dev)) 2810 return NOTIFY_DONE; 2811 2812 switch (event) { 2813 case NETDEV_CHANGEUPPER: 2814 info = ptr; 2815 if (!info->master) 2816 goto out; 2817 rocker_port = netdev_priv(dev); 2818 if (info->linking) { 2819 err = rocker_world_port_master_linked(rocker_port, 2820 info->upper_dev); 2821 if (err) 2822 netdev_warn(dev, "failed to reflect master linked (err %d)\n", 2823 err); 2824 } else { 2825 err = rocker_world_port_master_unlinked(rocker_port, 2826 info->upper_dev); 2827 if (err) 2828 netdev_warn(dev, "failed to reflect master unlinked (err %d)\n", 2829 err); 2830 } 2831 } 2832 out: 2833 return NOTIFY_DONE; 2834 } 2835 2836 static struct notifier_block rocker_netdevice_nb __read_mostly = { 2837 .notifier_call = rocker_netdevice_event, 2838 }; 2839 2840 /************************************ 2841 * Net event notifier event handler 2842 ************************************/ 2843 2844 static int rocker_netevent_event(struct notifier_block *unused, 2845 unsigned long event, void *ptr) 2846 { 2847 struct rocker_port *rocker_port; 2848 struct net_device *dev; 2849 struct neighbour *n = ptr; 2850 int err; 2851 2852 switch (event) { 2853 case NETEVENT_NEIGH_UPDATE: 2854 if (n->tbl != &arp_tbl) 2855 return NOTIFY_DONE; 2856 dev = n->dev; 2857 if (!rocker_port_dev_check(dev)) 2858 return NOTIFY_DONE; 2859 rocker_port = netdev_priv(dev); 2860 err = rocker_world_port_neigh_update(rocker_port, n); 2861 if (err) 2862 netdev_warn(dev, "failed to handle neigh update (err %d)\n", 2863 err); 2864 break; 2865 } 2866 2867 return NOTIFY_DONE; 2868 } 2869 2870 static struct notifier_block rocker_netevent_nb __read_mostly = { 2871 .notifier_call = rocker_netevent_event, 2872 }; 2873 2874 /*********************** 2875 * Module init and exit 2876 ***********************/ 2877 2878 static int __init rocker_module_init(void) 2879 { 2880 int err; 2881 2882 register_netdevice_notifier(&rocker_netdevice_nb); 2883 register_netevent_notifier(&rocker_netevent_nb); 2884 err = pci_register_driver(&rocker_pci_driver); 2885 if (err) 2886 goto err_pci_register_driver; 2887 return 0; 2888 2889 err_pci_register_driver: 2890 unregister_netevent_notifier(&rocker_netevent_nb); 2891 unregister_netdevice_notifier(&rocker_netdevice_nb); 2892 return err; 2893 } 2894 2895 static void __exit rocker_module_exit(void) 2896 { 2897 unregister_netevent_notifier(&rocker_netevent_nb); 2898 unregister_netdevice_notifier(&rocker_netdevice_nb); 2899 pci_unregister_driver(&rocker_pci_driver); 2900 } 2901 2902 module_init(rocker_module_init); 2903 module_exit(rocker_module_exit); 2904 2905 MODULE_LICENSE("GPL v2"); 2906 MODULE_AUTHOR("Jiri Pirko <jiri@resnulli.us>"); 2907 MODULE_AUTHOR("Scott Feldman <sfeldma@gmail.com>"); 2908 MODULE_DESCRIPTION("Rocker switch device driver"); 2909 MODULE_DEVICE_TABLE(pci, rocker_pci_id_table); 2910