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 -EINVAL; 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_fdb_add(struct rocker_port *rocker_port, 1629 const struct switchdev_obj_port_fdb *fdb, 1630 struct switchdev_trans *trans) 1631 { 1632 struct rocker_world_ops *wops = rocker_port->rocker->wops; 1633 1634 if (!wops->port_obj_fdb_add) 1635 return -EOPNOTSUPP; 1636 return wops->port_obj_fdb_add(rocker_port, fdb, trans); 1637 } 1638 1639 static int 1640 rocker_world_port_obj_fdb_del(struct rocker_port *rocker_port, 1641 const struct switchdev_obj_port_fdb *fdb) 1642 { 1643 struct rocker_world_ops *wops = rocker_port->rocker->wops; 1644 1645 if (!wops->port_obj_fdb_del) 1646 return -EOPNOTSUPP; 1647 return wops->port_obj_fdb_del(rocker_port, fdb); 1648 } 1649 1650 static int 1651 rocker_world_port_obj_fdb_dump(const struct rocker_port *rocker_port, 1652 struct switchdev_obj_port_fdb *fdb, 1653 switchdev_obj_dump_cb_t *cb) 1654 { 1655 struct rocker_world_ops *wops = rocker_port->rocker->wops; 1656 1657 if (!wops->port_obj_fdb_dump) 1658 return -EOPNOTSUPP; 1659 return wops->port_obj_fdb_dump(rocker_port, fdb, cb); 1660 } 1661 1662 static int rocker_world_port_master_linked(struct rocker_port *rocker_port, 1663 struct net_device *master) 1664 { 1665 struct rocker_world_ops *wops = rocker_port->rocker->wops; 1666 1667 if (!wops->port_master_linked) 1668 return -EOPNOTSUPP; 1669 return wops->port_master_linked(rocker_port, master); 1670 } 1671 1672 static int rocker_world_port_master_unlinked(struct rocker_port *rocker_port, 1673 struct net_device *master) 1674 { 1675 struct rocker_world_ops *wops = rocker_port->rocker->wops; 1676 1677 if (!wops->port_master_unlinked) 1678 return -EOPNOTSUPP; 1679 return wops->port_master_unlinked(rocker_port, master); 1680 } 1681 1682 static int rocker_world_port_neigh_update(struct rocker_port *rocker_port, 1683 struct neighbour *n) 1684 { 1685 struct rocker_world_ops *wops = rocker_port->rocker->wops; 1686 1687 if (!wops->port_neigh_update) 1688 return -EOPNOTSUPP; 1689 return wops->port_neigh_update(rocker_port, n); 1690 } 1691 1692 static int rocker_world_port_neigh_destroy(struct rocker_port *rocker_port, 1693 struct neighbour *n) 1694 { 1695 struct rocker_world_ops *wops = rocker_port->rocker->wops; 1696 1697 if (!wops->port_neigh_destroy) 1698 return -EOPNOTSUPP; 1699 return wops->port_neigh_destroy(rocker_port, n); 1700 } 1701 1702 static int rocker_world_port_ev_mac_vlan_seen(struct rocker_port *rocker_port, 1703 const unsigned char *addr, 1704 __be16 vlan_id) 1705 { 1706 struct rocker_world_ops *wops = rocker_port->rocker->wops; 1707 1708 if (!wops->port_ev_mac_vlan_seen) 1709 return -EOPNOTSUPP; 1710 return wops->port_ev_mac_vlan_seen(rocker_port, addr, vlan_id); 1711 } 1712 1713 static int rocker_world_fib4_add(struct rocker *rocker, 1714 const struct fib_entry_notifier_info *fen_info) 1715 { 1716 struct rocker_world_ops *wops = rocker->wops; 1717 1718 if (!wops->fib4_add) 1719 return 0; 1720 return wops->fib4_add(rocker, fen_info); 1721 } 1722 1723 static int rocker_world_fib4_del(struct rocker *rocker, 1724 const struct fib_entry_notifier_info *fen_info) 1725 { 1726 struct rocker_world_ops *wops = rocker->wops; 1727 1728 if (!wops->fib4_del) 1729 return 0; 1730 return wops->fib4_del(rocker, fen_info); 1731 } 1732 1733 static void rocker_world_fib4_abort(struct rocker *rocker) 1734 { 1735 struct rocker_world_ops *wops = rocker->wops; 1736 1737 if (wops->fib4_abort) 1738 wops->fib4_abort(rocker); 1739 } 1740 1741 /***************** 1742 * Net device ops 1743 *****************/ 1744 1745 static int rocker_port_open(struct net_device *dev) 1746 { 1747 struct rocker_port *rocker_port = netdev_priv(dev); 1748 int err; 1749 1750 err = rocker_port_dma_rings_init(rocker_port); 1751 if (err) 1752 return err; 1753 1754 err = request_irq(rocker_msix_tx_vector(rocker_port), 1755 rocker_tx_irq_handler, 0, 1756 rocker_driver_name, rocker_port); 1757 if (err) { 1758 netdev_err(rocker_port->dev, "cannot assign tx irq\n"); 1759 goto err_request_tx_irq; 1760 } 1761 1762 err = request_irq(rocker_msix_rx_vector(rocker_port), 1763 rocker_rx_irq_handler, 0, 1764 rocker_driver_name, rocker_port); 1765 if (err) { 1766 netdev_err(rocker_port->dev, "cannot assign rx irq\n"); 1767 goto err_request_rx_irq; 1768 } 1769 1770 err = rocker_world_port_open(rocker_port); 1771 if (err) { 1772 netdev_err(rocker_port->dev, "cannot open port in world\n"); 1773 goto err_world_port_open; 1774 } 1775 1776 napi_enable(&rocker_port->napi_tx); 1777 napi_enable(&rocker_port->napi_rx); 1778 if (!dev->proto_down) 1779 rocker_port_set_enable(rocker_port, true); 1780 netif_start_queue(dev); 1781 return 0; 1782 1783 err_world_port_open: 1784 free_irq(rocker_msix_rx_vector(rocker_port), rocker_port); 1785 err_request_rx_irq: 1786 free_irq(rocker_msix_tx_vector(rocker_port), rocker_port); 1787 err_request_tx_irq: 1788 rocker_port_dma_rings_fini(rocker_port); 1789 return err; 1790 } 1791 1792 static int rocker_port_stop(struct net_device *dev) 1793 { 1794 struct rocker_port *rocker_port = netdev_priv(dev); 1795 1796 netif_stop_queue(dev); 1797 rocker_port_set_enable(rocker_port, false); 1798 napi_disable(&rocker_port->napi_rx); 1799 napi_disable(&rocker_port->napi_tx); 1800 rocker_world_port_stop(rocker_port); 1801 free_irq(rocker_msix_rx_vector(rocker_port), rocker_port); 1802 free_irq(rocker_msix_tx_vector(rocker_port), rocker_port); 1803 rocker_port_dma_rings_fini(rocker_port); 1804 1805 return 0; 1806 } 1807 1808 static void rocker_tx_desc_frags_unmap(const struct rocker_port *rocker_port, 1809 const struct rocker_desc_info *desc_info) 1810 { 1811 const struct rocker *rocker = rocker_port->rocker; 1812 struct pci_dev *pdev = rocker->pdev; 1813 const struct rocker_tlv *attrs[ROCKER_TLV_TX_MAX + 1]; 1814 struct rocker_tlv *attr; 1815 int rem; 1816 1817 rocker_tlv_parse_desc(attrs, ROCKER_TLV_TX_MAX, desc_info); 1818 if (!attrs[ROCKER_TLV_TX_FRAGS]) 1819 return; 1820 rocker_tlv_for_each_nested(attr, attrs[ROCKER_TLV_TX_FRAGS], rem) { 1821 const struct rocker_tlv *frag_attrs[ROCKER_TLV_TX_FRAG_ATTR_MAX + 1]; 1822 dma_addr_t dma_handle; 1823 size_t len; 1824 1825 if (rocker_tlv_type(attr) != ROCKER_TLV_TX_FRAG) 1826 continue; 1827 rocker_tlv_parse_nested(frag_attrs, ROCKER_TLV_TX_FRAG_ATTR_MAX, 1828 attr); 1829 if (!frag_attrs[ROCKER_TLV_TX_FRAG_ATTR_ADDR] || 1830 !frag_attrs[ROCKER_TLV_TX_FRAG_ATTR_LEN]) 1831 continue; 1832 dma_handle = rocker_tlv_get_u64(frag_attrs[ROCKER_TLV_TX_FRAG_ATTR_ADDR]); 1833 len = rocker_tlv_get_u16(frag_attrs[ROCKER_TLV_TX_FRAG_ATTR_LEN]); 1834 pci_unmap_single(pdev, dma_handle, len, DMA_TO_DEVICE); 1835 } 1836 } 1837 1838 static int rocker_tx_desc_frag_map_put(const struct rocker_port *rocker_port, 1839 struct rocker_desc_info *desc_info, 1840 char *buf, size_t buf_len) 1841 { 1842 const struct rocker *rocker = rocker_port->rocker; 1843 struct pci_dev *pdev = rocker->pdev; 1844 dma_addr_t dma_handle; 1845 struct rocker_tlv *frag; 1846 1847 dma_handle = pci_map_single(pdev, buf, buf_len, DMA_TO_DEVICE); 1848 if (unlikely(pci_dma_mapping_error(pdev, dma_handle))) { 1849 if (net_ratelimit()) 1850 netdev_err(rocker_port->dev, "failed to dma map tx frag\n"); 1851 return -EIO; 1852 } 1853 frag = rocker_tlv_nest_start(desc_info, ROCKER_TLV_TX_FRAG); 1854 if (!frag) 1855 goto unmap_frag; 1856 if (rocker_tlv_put_u64(desc_info, ROCKER_TLV_TX_FRAG_ATTR_ADDR, 1857 dma_handle)) 1858 goto nest_cancel; 1859 if (rocker_tlv_put_u16(desc_info, ROCKER_TLV_TX_FRAG_ATTR_LEN, 1860 buf_len)) 1861 goto nest_cancel; 1862 rocker_tlv_nest_end(desc_info, frag); 1863 return 0; 1864 1865 nest_cancel: 1866 rocker_tlv_nest_cancel(desc_info, frag); 1867 unmap_frag: 1868 pci_unmap_single(pdev, dma_handle, buf_len, DMA_TO_DEVICE); 1869 return -EMSGSIZE; 1870 } 1871 1872 static netdev_tx_t rocker_port_xmit(struct sk_buff *skb, struct net_device *dev) 1873 { 1874 struct rocker_port *rocker_port = netdev_priv(dev); 1875 struct rocker *rocker = rocker_port->rocker; 1876 struct rocker_desc_info *desc_info; 1877 struct rocker_tlv *frags; 1878 int i; 1879 int err; 1880 1881 desc_info = rocker_desc_head_get(&rocker_port->tx_ring); 1882 if (unlikely(!desc_info)) { 1883 if (net_ratelimit()) 1884 netdev_err(dev, "tx ring full when queue awake\n"); 1885 return NETDEV_TX_BUSY; 1886 } 1887 1888 rocker_desc_cookie_ptr_set(desc_info, skb); 1889 1890 frags = rocker_tlv_nest_start(desc_info, ROCKER_TLV_TX_FRAGS); 1891 if (!frags) 1892 goto out; 1893 err = rocker_tx_desc_frag_map_put(rocker_port, desc_info, 1894 skb->data, skb_headlen(skb)); 1895 if (err) 1896 goto nest_cancel; 1897 if (skb_shinfo(skb)->nr_frags > ROCKER_TX_FRAGS_MAX) { 1898 err = skb_linearize(skb); 1899 if (err) 1900 goto unmap_frags; 1901 } 1902 1903 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { 1904 const skb_frag_t *frag = &skb_shinfo(skb)->frags[i]; 1905 1906 err = rocker_tx_desc_frag_map_put(rocker_port, desc_info, 1907 skb_frag_address(frag), 1908 skb_frag_size(frag)); 1909 if (err) 1910 goto unmap_frags; 1911 } 1912 rocker_tlv_nest_end(desc_info, frags); 1913 1914 rocker_desc_gen_clear(desc_info); 1915 rocker_desc_head_set(rocker, &rocker_port->tx_ring, desc_info); 1916 1917 desc_info = rocker_desc_head_get(&rocker_port->tx_ring); 1918 if (!desc_info) 1919 netif_stop_queue(dev); 1920 1921 return NETDEV_TX_OK; 1922 1923 unmap_frags: 1924 rocker_tx_desc_frags_unmap(rocker_port, desc_info); 1925 nest_cancel: 1926 rocker_tlv_nest_cancel(desc_info, frags); 1927 out: 1928 dev_kfree_skb(skb); 1929 dev->stats.tx_dropped++; 1930 1931 return NETDEV_TX_OK; 1932 } 1933 1934 static int rocker_port_set_mac_address(struct net_device *dev, void *p) 1935 { 1936 struct sockaddr *addr = p; 1937 struct rocker_port *rocker_port = netdev_priv(dev); 1938 int err; 1939 1940 if (!is_valid_ether_addr(addr->sa_data)) 1941 return -EADDRNOTAVAIL; 1942 1943 err = rocker_cmd_set_port_settings_macaddr(rocker_port, addr->sa_data); 1944 if (err) 1945 return err; 1946 memcpy(dev->dev_addr, addr->sa_data, dev->addr_len); 1947 return 0; 1948 } 1949 1950 static int rocker_port_change_mtu(struct net_device *dev, int new_mtu) 1951 { 1952 struct rocker_port *rocker_port = netdev_priv(dev); 1953 int running = netif_running(dev); 1954 int err; 1955 1956 #define ROCKER_PORT_MIN_MTU 68 1957 #define ROCKER_PORT_MAX_MTU 9000 1958 1959 if (new_mtu < ROCKER_PORT_MIN_MTU || new_mtu > ROCKER_PORT_MAX_MTU) 1960 return -EINVAL; 1961 1962 if (running) 1963 rocker_port_stop(dev); 1964 1965 netdev_info(dev, "MTU change from %d to %d\n", dev->mtu, new_mtu); 1966 dev->mtu = new_mtu; 1967 1968 err = rocker_cmd_set_port_settings_mtu(rocker_port, new_mtu); 1969 if (err) 1970 return err; 1971 1972 if (running) 1973 err = rocker_port_open(dev); 1974 1975 return err; 1976 } 1977 1978 static int rocker_port_get_phys_port_name(struct net_device *dev, 1979 char *buf, size_t len) 1980 { 1981 struct rocker_port *rocker_port = netdev_priv(dev); 1982 struct port_name name = { .buf = buf, .len = len }; 1983 int err; 1984 1985 err = rocker_cmd_exec(rocker_port, false, 1986 rocker_cmd_get_port_settings_prep, NULL, 1987 rocker_cmd_get_port_settings_phys_name_proc, 1988 &name); 1989 1990 return err ? -EOPNOTSUPP : 0; 1991 } 1992 1993 static int rocker_port_change_proto_down(struct net_device *dev, 1994 bool proto_down) 1995 { 1996 struct rocker_port *rocker_port = netdev_priv(dev); 1997 1998 if (rocker_port->dev->flags & IFF_UP) 1999 rocker_port_set_enable(rocker_port, !proto_down); 2000 rocker_port->dev->proto_down = proto_down; 2001 return 0; 2002 } 2003 2004 static void rocker_port_neigh_destroy(struct net_device *dev, 2005 struct neighbour *n) 2006 { 2007 struct rocker_port *rocker_port = netdev_priv(n->dev); 2008 int err; 2009 2010 err = rocker_world_port_neigh_destroy(rocker_port, n); 2011 if (err) 2012 netdev_warn(rocker_port->dev, "failed to handle neigh destroy (err %d)\n", 2013 err); 2014 } 2015 2016 static const struct net_device_ops rocker_port_netdev_ops = { 2017 .ndo_open = rocker_port_open, 2018 .ndo_stop = rocker_port_stop, 2019 .ndo_start_xmit = rocker_port_xmit, 2020 .ndo_set_mac_address = rocker_port_set_mac_address, 2021 .ndo_change_mtu = rocker_port_change_mtu, 2022 .ndo_bridge_getlink = switchdev_port_bridge_getlink, 2023 .ndo_bridge_setlink = switchdev_port_bridge_setlink, 2024 .ndo_bridge_dellink = switchdev_port_bridge_dellink, 2025 .ndo_fdb_add = switchdev_port_fdb_add, 2026 .ndo_fdb_del = switchdev_port_fdb_del, 2027 .ndo_fdb_dump = switchdev_port_fdb_dump, 2028 .ndo_get_phys_port_name = rocker_port_get_phys_port_name, 2029 .ndo_change_proto_down = rocker_port_change_proto_down, 2030 .ndo_neigh_destroy = rocker_port_neigh_destroy, 2031 }; 2032 2033 /******************** 2034 * swdev interface 2035 ********************/ 2036 2037 static int rocker_port_attr_get(struct net_device *dev, 2038 struct switchdev_attr *attr) 2039 { 2040 const struct rocker_port *rocker_port = netdev_priv(dev); 2041 const struct rocker *rocker = rocker_port->rocker; 2042 int err = 0; 2043 2044 switch (attr->id) { 2045 case SWITCHDEV_ATTR_ID_PORT_PARENT_ID: 2046 attr->u.ppid.id_len = sizeof(rocker->hw.id); 2047 memcpy(&attr->u.ppid.id, &rocker->hw.id, attr->u.ppid.id_len); 2048 break; 2049 case SWITCHDEV_ATTR_ID_PORT_BRIDGE_FLAGS: 2050 err = rocker_world_port_attr_bridge_flags_get(rocker_port, 2051 &attr->u.brport_flags); 2052 break; 2053 default: 2054 return -EOPNOTSUPP; 2055 } 2056 2057 return err; 2058 } 2059 2060 static int rocker_port_attr_set(struct net_device *dev, 2061 const struct switchdev_attr *attr, 2062 struct switchdev_trans *trans) 2063 { 2064 struct rocker_port *rocker_port = netdev_priv(dev); 2065 int err = 0; 2066 2067 switch (attr->id) { 2068 case SWITCHDEV_ATTR_ID_PORT_STP_STATE: 2069 err = rocker_world_port_attr_stp_state_set(rocker_port, 2070 attr->u.stp_state, 2071 trans); 2072 break; 2073 case SWITCHDEV_ATTR_ID_PORT_BRIDGE_FLAGS: 2074 err = rocker_world_port_attr_bridge_flags_set(rocker_port, 2075 attr->u.brport_flags, 2076 trans); 2077 break; 2078 case SWITCHDEV_ATTR_ID_BRIDGE_AGEING_TIME: 2079 err = rocker_world_port_attr_bridge_ageing_time_set(rocker_port, 2080 attr->u.ageing_time, 2081 trans); 2082 break; 2083 default: 2084 err = -EOPNOTSUPP; 2085 break; 2086 } 2087 2088 return err; 2089 } 2090 2091 static int rocker_port_obj_add(struct net_device *dev, 2092 const struct switchdev_obj *obj, 2093 struct switchdev_trans *trans) 2094 { 2095 struct rocker_port *rocker_port = netdev_priv(dev); 2096 int err = 0; 2097 2098 switch (obj->id) { 2099 case SWITCHDEV_OBJ_ID_PORT_VLAN: 2100 err = rocker_world_port_obj_vlan_add(rocker_port, 2101 SWITCHDEV_OBJ_PORT_VLAN(obj), 2102 trans); 2103 break; 2104 case SWITCHDEV_OBJ_ID_PORT_FDB: 2105 err = rocker_world_port_obj_fdb_add(rocker_port, 2106 SWITCHDEV_OBJ_PORT_FDB(obj), 2107 trans); 2108 break; 2109 default: 2110 err = -EOPNOTSUPP; 2111 break; 2112 } 2113 2114 return err; 2115 } 2116 2117 static int rocker_port_obj_del(struct net_device *dev, 2118 const struct switchdev_obj *obj) 2119 { 2120 struct rocker_port *rocker_port = netdev_priv(dev); 2121 int err = 0; 2122 2123 switch (obj->id) { 2124 case SWITCHDEV_OBJ_ID_PORT_VLAN: 2125 err = rocker_world_port_obj_vlan_del(rocker_port, 2126 SWITCHDEV_OBJ_PORT_VLAN(obj)); 2127 break; 2128 case SWITCHDEV_OBJ_ID_PORT_FDB: 2129 err = rocker_world_port_obj_fdb_del(rocker_port, 2130 SWITCHDEV_OBJ_PORT_FDB(obj)); 2131 break; 2132 default: 2133 err = -EOPNOTSUPP; 2134 break; 2135 } 2136 2137 return err; 2138 } 2139 2140 static int rocker_port_obj_dump(struct net_device *dev, 2141 struct switchdev_obj *obj, 2142 switchdev_obj_dump_cb_t *cb) 2143 { 2144 const struct rocker_port *rocker_port = netdev_priv(dev); 2145 int err = 0; 2146 2147 switch (obj->id) { 2148 case SWITCHDEV_OBJ_ID_PORT_FDB: 2149 err = rocker_world_port_obj_fdb_dump(rocker_port, 2150 SWITCHDEV_OBJ_PORT_FDB(obj), 2151 cb); 2152 break; 2153 case SWITCHDEV_OBJ_ID_PORT_VLAN: 2154 err = rocker_world_port_obj_vlan_dump(rocker_port, 2155 SWITCHDEV_OBJ_PORT_VLAN(obj), 2156 cb); 2157 break; 2158 default: 2159 err = -EOPNOTSUPP; 2160 break; 2161 } 2162 2163 return err; 2164 } 2165 2166 static const struct switchdev_ops rocker_port_switchdev_ops = { 2167 .switchdev_port_attr_get = rocker_port_attr_get, 2168 .switchdev_port_attr_set = rocker_port_attr_set, 2169 .switchdev_port_obj_add = rocker_port_obj_add, 2170 .switchdev_port_obj_del = rocker_port_obj_del, 2171 .switchdev_port_obj_dump = rocker_port_obj_dump, 2172 }; 2173 2174 static int rocker_router_fib_event(struct notifier_block *nb, 2175 unsigned long event, void *ptr) 2176 { 2177 struct rocker *rocker = container_of(nb, struct rocker, fib_nb); 2178 struct fib_entry_notifier_info *fen_info = ptr; 2179 int err; 2180 2181 switch (event) { 2182 case FIB_EVENT_ENTRY_ADD: 2183 err = rocker_world_fib4_add(rocker, fen_info); 2184 if (err) 2185 rocker_world_fib4_abort(rocker); 2186 else 2187 break; 2188 case FIB_EVENT_ENTRY_DEL: 2189 rocker_world_fib4_del(rocker, fen_info); 2190 break; 2191 case FIB_EVENT_RULE_ADD: /* fall through */ 2192 case FIB_EVENT_RULE_DEL: 2193 rocker_world_fib4_abort(rocker); 2194 break; 2195 } 2196 return NOTIFY_DONE; 2197 } 2198 2199 /******************** 2200 * ethtool interface 2201 ********************/ 2202 2203 static int rocker_port_get_settings(struct net_device *dev, 2204 struct ethtool_cmd *ecmd) 2205 { 2206 struct rocker_port *rocker_port = netdev_priv(dev); 2207 2208 return rocker_cmd_get_port_settings_ethtool(rocker_port, ecmd); 2209 } 2210 2211 static int rocker_port_set_settings(struct net_device *dev, 2212 struct ethtool_cmd *ecmd) 2213 { 2214 struct rocker_port *rocker_port = netdev_priv(dev); 2215 2216 return rocker_cmd_set_port_settings_ethtool(rocker_port, ecmd); 2217 } 2218 2219 static void rocker_port_get_drvinfo(struct net_device *dev, 2220 struct ethtool_drvinfo *drvinfo) 2221 { 2222 strlcpy(drvinfo->driver, rocker_driver_name, sizeof(drvinfo->driver)); 2223 strlcpy(drvinfo->version, UTS_RELEASE, sizeof(drvinfo->version)); 2224 } 2225 2226 static struct rocker_port_stats { 2227 char str[ETH_GSTRING_LEN]; 2228 int type; 2229 } rocker_port_stats[] = { 2230 { "rx_packets", ROCKER_TLV_CMD_PORT_STATS_RX_PKTS, }, 2231 { "rx_bytes", ROCKER_TLV_CMD_PORT_STATS_RX_BYTES, }, 2232 { "rx_dropped", ROCKER_TLV_CMD_PORT_STATS_RX_DROPPED, }, 2233 { "rx_errors", ROCKER_TLV_CMD_PORT_STATS_RX_ERRORS, }, 2234 2235 { "tx_packets", ROCKER_TLV_CMD_PORT_STATS_TX_PKTS, }, 2236 { "tx_bytes", ROCKER_TLV_CMD_PORT_STATS_TX_BYTES, }, 2237 { "tx_dropped", ROCKER_TLV_CMD_PORT_STATS_TX_DROPPED, }, 2238 { "tx_errors", ROCKER_TLV_CMD_PORT_STATS_TX_ERRORS, }, 2239 }; 2240 2241 #define ROCKER_PORT_STATS_LEN ARRAY_SIZE(rocker_port_stats) 2242 2243 static void rocker_port_get_strings(struct net_device *netdev, u32 stringset, 2244 u8 *data) 2245 { 2246 u8 *p = data; 2247 int i; 2248 2249 switch (stringset) { 2250 case ETH_SS_STATS: 2251 for (i = 0; i < ARRAY_SIZE(rocker_port_stats); i++) { 2252 memcpy(p, rocker_port_stats[i].str, ETH_GSTRING_LEN); 2253 p += ETH_GSTRING_LEN; 2254 } 2255 break; 2256 } 2257 } 2258 2259 static int 2260 rocker_cmd_get_port_stats_prep(const struct rocker_port *rocker_port, 2261 struct rocker_desc_info *desc_info, 2262 void *priv) 2263 { 2264 struct rocker_tlv *cmd_stats; 2265 2266 if (rocker_tlv_put_u16(desc_info, ROCKER_TLV_CMD_TYPE, 2267 ROCKER_TLV_CMD_TYPE_GET_PORT_STATS)) 2268 return -EMSGSIZE; 2269 2270 cmd_stats = rocker_tlv_nest_start(desc_info, ROCKER_TLV_CMD_INFO); 2271 if (!cmd_stats) 2272 return -EMSGSIZE; 2273 2274 if (rocker_tlv_put_u32(desc_info, ROCKER_TLV_CMD_PORT_STATS_PPORT, 2275 rocker_port->pport)) 2276 return -EMSGSIZE; 2277 2278 rocker_tlv_nest_end(desc_info, cmd_stats); 2279 2280 return 0; 2281 } 2282 2283 static int 2284 rocker_cmd_get_port_stats_ethtool_proc(const struct rocker_port *rocker_port, 2285 const struct rocker_desc_info *desc_info, 2286 void *priv) 2287 { 2288 const struct rocker_tlv *attrs[ROCKER_TLV_CMD_MAX + 1]; 2289 const struct rocker_tlv *stats_attrs[ROCKER_TLV_CMD_PORT_STATS_MAX + 1]; 2290 const struct rocker_tlv *pattr; 2291 u32 pport; 2292 u64 *data = priv; 2293 int i; 2294 2295 rocker_tlv_parse_desc(attrs, ROCKER_TLV_CMD_MAX, desc_info); 2296 2297 if (!attrs[ROCKER_TLV_CMD_INFO]) 2298 return -EIO; 2299 2300 rocker_tlv_parse_nested(stats_attrs, ROCKER_TLV_CMD_PORT_STATS_MAX, 2301 attrs[ROCKER_TLV_CMD_INFO]); 2302 2303 if (!stats_attrs[ROCKER_TLV_CMD_PORT_STATS_PPORT]) 2304 return -EIO; 2305 2306 pport = rocker_tlv_get_u32(stats_attrs[ROCKER_TLV_CMD_PORT_STATS_PPORT]); 2307 if (pport != rocker_port->pport) 2308 return -EIO; 2309 2310 for (i = 0; i < ARRAY_SIZE(rocker_port_stats); i++) { 2311 pattr = stats_attrs[rocker_port_stats[i].type]; 2312 if (!pattr) 2313 continue; 2314 2315 data[i] = rocker_tlv_get_u64(pattr); 2316 } 2317 2318 return 0; 2319 } 2320 2321 static int rocker_cmd_get_port_stats_ethtool(struct rocker_port *rocker_port, 2322 void *priv) 2323 { 2324 return rocker_cmd_exec(rocker_port, false, 2325 rocker_cmd_get_port_stats_prep, NULL, 2326 rocker_cmd_get_port_stats_ethtool_proc, 2327 priv); 2328 } 2329 2330 static void rocker_port_get_stats(struct net_device *dev, 2331 struct ethtool_stats *stats, u64 *data) 2332 { 2333 struct rocker_port *rocker_port = netdev_priv(dev); 2334 2335 if (rocker_cmd_get_port_stats_ethtool(rocker_port, data) != 0) { 2336 int i; 2337 2338 for (i = 0; i < ARRAY_SIZE(rocker_port_stats); ++i) 2339 data[i] = 0; 2340 } 2341 } 2342 2343 static int rocker_port_get_sset_count(struct net_device *netdev, int sset) 2344 { 2345 switch (sset) { 2346 case ETH_SS_STATS: 2347 return ROCKER_PORT_STATS_LEN; 2348 default: 2349 return -EOPNOTSUPP; 2350 } 2351 } 2352 2353 static const struct ethtool_ops rocker_port_ethtool_ops = { 2354 .get_settings = rocker_port_get_settings, 2355 .set_settings = rocker_port_set_settings, 2356 .get_drvinfo = rocker_port_get_drvinfo, 2357 .get_link = ethtool_op_get_link, 2358 .get_strings = rocker_port_get_strings, 2359 .get_ethtool_stats = rocker_port_get_stats, 2360 .get_sset_count = rocker_port_get_sset_count, 2361 }; 2362 2363 /***************** 2364 * NAPI interface 2365 *****************/ 2366 2367 static struct rocker_port *rocker_port_napi_tx_get(struct napi_struct *napi) 2368 { 2369 return container_of(napi, struct rocker_port, napi_tx); 2370 } 2371 2372 static int rocker_port_poll_tx(struct napi_struct *napi, int budget) 2373 { 2374 struct rocker_port *rocker_port = rocker_port_napi_tx_get(napi); 2375 const struct rocker *rocker = rocker_port->rocker; 2376 const struct rocker_desc_info *desc_info; 2377 u32 credits = 0; 2378 int err; 2379 2380 /* Cleanup tx descriptors */ 2381 while ((desc_info = rocker_desc_tail_get(&rocker_port->tx_ring))) { 2382 struct sk_buff *skb; 2383 2384 err = rocker_desc_err(desc_info); 2385 if (err && net_ratelimit()) 2386 netdev_err(rocker_port->dev, "tx desc received with err %d\n", 2387 err); 2388 rocker_tx_desc_frags_unmap(rocker_port, desc_info); 2389 2390 skb = rocker_desc_cookie_ptr_get(desc_info); 2391 if (err == 0) { 2392 rocker_port->dev->stats.tx_packets++; 2393 rocker_port->dev->stats.tx_bytes += skb->len; 2394 } else { 2395 rocker_port->dev->stats.tx_errors++; 2396 } 2397 2398 dev_kfree_skb_any(skb); 2399 credits++; 2400 } 2401 2402 if (credits && netif_queue_stopped(rocker_port->dev)) 2403 netif_wake_queue(rocker_port->dev); 2404 2405 napi_complete(napi); 2406 rocker_dma_ring_credits_set(rocker, &rocker_port->tx_ring, credits); 2407 2408 return 0; 2409 } 2410 2411 static int rocker_port_rx_proc(const struct rocker *rocker, 2412 const struct rocker_port *rocker_port, 2413 struct rocker_desc_info *desc_info) 2414 { 2415 const struct rocker_tlv *attrs[ROCKER_TLV_RX_MAX + 1]; 2416 struct sk_buff *skb = rocker_desc_cookie_ptr_get(desc_info); 2417 size_t rx_len; 2418 u16 rx_flags = 0; 2419 2420 if (!skb) 2421 return -ENOENT; 2422 2423 rocker_tlv_parse_desc(attrs, ROCKER_TLV_RX_MAX, desc_info); 2424 if (!attrs[ROCKER_TLV_RX_FRAG_LEN]) 2425 return -EINVAL; 2426 if (attrs[ROCKER_TLV_RX_FLAGS]) 2427 rx_flags = rocker_tlv_get_u16(attrs[ROCKER_TLV_RX_FLAGS]); 2428 2429 rocker_dma_rx_ring_skb_unmap(rocker, attrs); 2430 2431 rx_len = rocker_tlv_get_u16(attrs[ROCKER_TLV_RX_FRAG_LEN]); 2432 skb_put(skb, rx_len); 2433 skb->protocol = eth_type_trans(skb, rocker_port->dev); 2434 2435 if (rx_flags & ROCKER_RX_FLAGS_FWD_OFFLOAD) 2436 skb->offload_fwd_mark = 1; 2437 2438 rocker_port->dev->stats.rx_packets++; 2439 rocker_port->dev->stats.rx_bytes += skb->len; 2440 2441 netif_receive_skb(skb); 2442 2443 return rocker_dma_rx_ring_skb_alloc(rocker_port, desc_info); 2444 } 2445 2446 static struct rocker_port *rocker_port_napi_rx_get(struct napi_struct *napi) 2447 { 2448 return container_of(napi, struct rocker_port, napi_rx); 2449 } 2450 2451 static int rocker_port_poll_rx(struct napi_struct *napi, int budget) 2452 { 2453 struct rocker_port *rocker_port = rocker_port_napi_rx_get(napi); 2454 const struct rocker *rocker = rocker_port->rocker; 2455 struct rocker_desc_info *desc_info; 2456 u32 credits = 0; 2457 int err; 2458 2459 /* Process rx descriptors */ 2460 while (credits < budget && 2461 (desc_info = rocker_desc_tail_get(&rocker_port->rx_ring))) { 2462 err = rocker_desc_err(desc_info); 2463 if (err) { 2464 if (net_ratelimit()) 2465 netdev_err(rocker_port->dev, "rx desc received with err %d\n", 2466 err); 2467 } else { 2468 err = rocker_port_rx_proc(rocker, rocker_port, 2469 desc_info); 2470 if (err && net_ratelimit()) 2471 netdev_err(rocker_port->dev, "rx processing failed with err %d\n", 2472 err); 2473 } 2474 if (err) 2475 rocker_port->dev->stats.rx_errors++; 2476 2477 rocker_desc_gen_clear(desc_info); 2478 rocker_desc_head_set(rocker, &rocker_port->rx_ring, desc_info); 2479 credits++; 2480 } 2481 2482 if (credits < budget) 2483 napi_complete(napi); 2484 2485 rocker_dma_ring_credits_set(rocker, &rocker_port->rx_ring, credits); 2486 2487 return credits; 2488 } 2489 2490 /***************** 2491 * PCI driver ops 2492 *****************/ 2493 2494 static void rocker_carrier_init(const struct rocker_port *rocker_port) 2495 { 2496 const struct rocker *rocker = rocker_port->rocker; 2497 u64 link_status = rocker_read64(rocker, PORT_PHYS_LINK_STATUS); 2498 bool link_up; 2499 2500 link_up = link_status & (1 << rocker_port->pport); 2501 if (link_up) 2502 netif_carrier_on(rocker_port->dev); 2503 else 2504 netif_carrier_off(rocker_port->dev); 2505 } 2506 2507 static void rocker_remove_ports(struct rocker *rocker) 2508 { 2509 struct rocker_port *rocker_port; 2510 int i; 2511 2512 for (i = 0; i < rocker->port_count; i++) { 2513 rocker_port = rocker->ports[i]; 2514 if (!rocker_port) 2515 continue; 2516 rocker_world_port_fini(rocker_port); 2517 unregister_netdev(rocker_port->dev); 2518 rocker_world_port_post_fini(rocker_port); 2519 free_netdev(rocker_port->dev); 2520 } 2521 rocker_world_fini(rocker); 2522 kfree(rocker->ports); 2523 } 2524 2525 static void rocker_port_dev_addr_init(struct rocker_port *rocker_port) 2526 { 2527 const struct rocker *rocker = rocker_port->rocker; 2528 const struct pci_dev *pdev = rocker->pdev; 2529 int err; 2530 2531 err = rocker_cmd_get_port_settings_macaddr(rocker_port, 2532 rocker_port->dev->dev_addr); 2533 if (err) { 2534 dev_warn(&pdev->dev, "failed to get mac address, using random\n"); 2535 eth_hw_addr_random(rocker_port->dev); 2536 } 2537 } 2538 2539 static int rocker_probe_port(struct rocker *rocker, unsigned int port_number) 2540 { 2541 const struct pci_dev *pdev = rocker->pdev; 2542 struct rocker_port *rocker_port; 2543 struct net_device *dev; 2544 int err; 2545 2546 dev = alloc_etherdev(sizeof(struct rocker_port)); 2547 if (!dev) 2548 return -ENOMEM; 2549 rocker_port = netdev_priv(dev); 2550 rocker_port->dev = dev; 2551 rocker_port->rocker = rocker; 2552 rocker_port->port_number = port_number; 2553 rocker_port->pport = port_number + 1; 2554 2555 err = rocker_world_check_init(rocker_port); 2556 if (err) { 2557 dev_err(&pdev->dev, "world init failed\n"); 2558 goto err_world_check_init; 2559 } 2560 2561 rocker_port_dev_addr_init(rocker_port); 2562 dev->netdev_ops = &rocker_port_netdev_ops; 2563 dev->ethtool_ops = &rocker_port_ethtool_ops; 2564 dev->switchdev_ops = &rocker_port_switchdev_ops; 2565 netif_tx_napi_add(dev, &rocker_port->napi_tx, rocker_port_poll_tx, 2566 NAPI_POLL_WEIGHT); 2567 netif_napi_add(dev, &rocker_port->napi_rx, rocker_port_poll_rx, 2568 NAPI_POLL_WEIGHT); 2569 rocker_carrier_init(rocker_port); 2570 2571 dev->features |= NETIF_F_NETNS_LOCAL | NETIF_F_SG; 2572 2573 err = rocker_world_port_pre_init(rocker_port); 2574 if (err) { 2575 dev_err(&pdev->dev, "port world pre-init failed\n"); 2576 goto err_world_port_pre_init; 2577 } 2578 err = register_netdev(dev); 2579 if (err) { 2580 dev_err(&pdev->dev, "register_netdev failed\n"); 2581 goto err_register_netdev; 2582 } 2583 rocker->ports[port_number] = rocker_port; 2584 2585 err = rocker_world_port_init(rocker_port); 2586 if (err) { 2587 dev_err(&pdev->dev, "port world init failed\n"); 2588 goto err_world_port_init; 2589 } 2590 2591 return 0; 2592 2593 err_world_port_init: 2594 rocker->ports[port_number] = NULL; 2595 unregister_netdev(dev); 2596 err_register_netdev: 2597 rocker_world_port_post_fini(rocker_port); 2598 err_world_port_pre_init: 2599 err_world_check_init: 2600 free_netdev(dev); 2601 return err; 2602 } 2603 2604 static int rocker_probe_ports(struct rocker *rocker) 2605 { 2606 int i; 2607 size_t alloc_size; 2608 int err; 2609 2610 alloc_size = sizeof(struct rocker_port *) * rocker->port_count; 2611 rocker->ports = kzalloc(alloc_size, GFP_KERNEL); 2612 if (!rocker->ports) 2613 return -ENOMEM; 2614 for (i = 0; i < rocker->port_count; i++) { 2615 err = rocker_probe_port(rocker, i); 2616 if (err) 2617 goto remove_ports; 2618 } 2619 return 0; 2620 2621 remove_ports: 2622 rocker_remove_ports(rocker); 2623 return err; 2624 } 2625 2626 static int rocker_msix_init(struct rocker *rocker) 2627 { 2628 struct pci_dev *pdev = rocker->pdev; 2629 int msix_entries; 2630 int i; 2631 int err; 2632 2633 msix_entries = pci_msix_vec_count(pdev); 2634 if (msix_entries < 0) 2635 return msix_entries; 2636 2637 if (msix_entries != ROCKER_MSIX_VEC_COUNT(rocker->port_count)) 2638 return -EINVAL; 2639 2640 rocker->msix_entries = kmalloc_array(msix_entries, 2641 sizeof(struct msix_entry), 2642 GFP_KERNEL); 2643 if (!rocker->msix_entries) 2644 return -ENOMEM; 2645 2646 for (i = 0; i < msix_entries; i++) 2647 rocker->msix_entries[i].entry = i; 2648 2649 err = pci_enable_msix_exact(pdev, rocker->msix_entries, msix_entries); 2650 if (err < 0) 2651 goto err_enable_msix; 2652 2653 return 0; 2654 2655 err_enable_msix: 2656 kfree(rocker->msix_entries); 2657 return err; 2658 } 2659 2660 static void rocker_msix_fini(const struct rocker *rocker) 2661 { 2662 pci_disable_msix(rocker->pdev); 2663 kfree(rocker->msix_entries); 2664 } 2665 2666 static int rocker_probe(struct pci_dev *pdev, const struct pci_device_id *id) 2667 { 2668 struct rocker *rocker; 2669 int err; 2670 2671 rocker = kzalloc(sizeof(*rocker), GFP_KERNEL); 2672 if (!rocker) 2673 return -ENOMEM; 2674 2675 err = pci_enable_device(pdev); 2676 if (err) { 2677 dev_err(&pdev->dev, "pci_enable_device failed\n"); 2678 goto err_pci_enable_device; 2679 } 2680 2681 err = pci_request_regions(pdev, rocker_driver_name); 2682 if (err) { 2683 dev_err(&pdev->dev, "pci_request_regions failed\n"); 2684 goto err_pci_request_regions; 2685 } 2686 2687 err = pci_set_dma_mask(pdev, DMA_BIT_MASK(64)); 2688 if (!err) { 2689 err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64)); 2690 if (err) { 2691 dev_err(&pdev->dev, "pci_set_consistent_dma_mask failed\n"); 2692 goto err_pci_set_dma_mask; 2693 } 2694 } else { 2695 err = pci_set_dma_mask(pdev, DMA_BIT_MASK(32)); 2696 if (err) { 2697 dev_err(&pdev->dev, "pci_set_dma_mask failed\n"); 2698 goto err_pci_set_dma_mask; 2699 } 2700 } 2701 2702 if (pci_resource_len(pdev, 0) < ROCKER_PCI_BAR0_SIZE) { 2703 dev_err(&pdev->dev, "invalid PCI region size\n"); 2704 err = -EINVAL; 2705 goto err_pci_resource_len_check; 2706 } 2707 2708 rocker->hw_addr = ioremap(pci_resource_start(pdev, 0), 2709 pci_resource_len(pdev, 0)); 2710 if (!rocker->hw_addr) { 2711 dev_err(&pdev->dev, "ioremap failed\n"); 2712 err = -EIO; 2713 goto err_ioremap; 2714 } 2715 pci_set_master(pdev); 2716 2717 rocker->pdev = pdev; 2718 pci_set_drvdata(pdev, rocker); 2719 2720 rocker->port_count = rocker_read32(rocker, PORT_PHYS_COUNT); 2721 2722 err = rocker_msix_init(rocker); 2723 if (err) { 2724 dev_err(&pdev->dev, "MSI-X init failed\n"); 2725 goto err_msix_init; 2726 } 2727 2728 err = rocker_basic_hw_test(rocker); 2729 if (err) { 2730 dev_err(&pdev->dev, "basic hw test failed\n"); 2731 goto err_basic_hw_test; 2732 } 2733 2734 rocker_write32(rocker, CONTROL, ROCKER_CONTROL_RESET); 2735 2736 err = rocker_dma_rings_init(rocker); 2737 if (err) 2738 goto err_dma_rings_init; 2739 2740 err = request_irq(rocker_msix_vector(rocker, ROCKER_MSIX_VEC_CMD), 2741 rocker_cmd_irq_handler, 0, 2742 rocker_driver_name, rocker); 2743 if (err) { 2744 dev_err(&pdev->dev, "cannot assign cmd irq\n"); 2745 goto err_request_cmd_irq; 2746 } 2747 2748 err = request_irq(rocker_msix_vector(rocker, ROCKER_MSIX_VEC_EVENT), 2749 rocker_event_irq_handler, 0, 2750 rocker_driver_name, rocker); 2751 if (err) { 2752 dev_err(&pdev->dev, "cannot assign event irq\n"); 2753 goto err_request_event_irq; 2754 } 2755 2756 rocker->hw.id = rocker_read64(rocker, SWITCH_ID); 2757 2758 err = rocker_probe_ports(rocker); 2759 if (err) { 2760 dev_err(&pdev->dev, "failed to probe ports\n"); 2761 goto err_probe_ports; 2762 } 2763 2764 rocker->fib_nb.notifier_call = rocker_router_fib_event; 2765 register_fib_notifier(&rocker->fib_nb); 2766 2767 dev_info(&pdev->dev, "Rocker switch with id %*phN\n", 2768 (int)sizeof(rocker->hw.id), &rocker->hw.id); 2769 2770 return 0; 2771 2772 err_probe_ports: 2773 free_irq(rocker_msix_vector(rocker, ROCKER_MSIX_VEC_EVENT), rocker); 2774 err_request_event_irq: 2775 free_irq(rocker_msix_vector(rocker, ROCKER_MSIX_VEC_CMD), rocker); 2776 err_request_cmd_irq: 2777 rocker_dma_rings_fini(rocker); 2778 err_dma_rings_init: 2779 err_basic_hw_test: 2780 rocker_msix_fini(rocker); 2781 err_msix_init: 2782 iounmap(rocker->hw_addr); 2783 err_ioremap: 2784 err_pci_resource_len_check: 2785 err_pci_set_dma_mask: 2786 pci_release_regions(pdev); 2787 err_pci_request_regions: 2788 pci_disable_device(pdev); 2789 err_pci_enable_device: 2790 kfree(rocker); 2791 return err; 2792 } 2793 2794 static void rocker_remove(struct pci_dev *pdev) 2795 { 2796 struct rocker *rocker = pci_get_drvdata(pdev); 2797 2798 unregister_fib_notifier(&rocker->fib_nb); 2799 rocker_write32(rocker, CONTROL, ROCKER_CONTROL_RESET); 2800 rocker_remove_ports(rocker); 2801 free_irq(rocker_msix_vector(rocker, ROCKER_MSIX_VEC_EVENT), rocker); 2802 free_irq(rocker_msix_vector(rocker, ROCKER_MSIX_VEC_CMD), rocker); 2803 rocker_dma_rings_fini(rocker); 2804 rocker_msix_fini(rocker); 2805 iounmap(rocker->hw_addr); 2806 pci_release_regions(rocker->pdev); 2807 pci_disable_device(rocker->pdev); 2808 kfree(rocker); 2809 } 2810 2811 static struct pci_driver rocker_pci_driver = { 2812 .name = rocker_driver_name, 2813 .id_table = rocker_pci_id_table, 2814 .probe = rocker_probe, 2815 .remove = rocker_remove, 2816 }; 2817 2818 /************************************ 2819 * Net device notifier event handler 2820 ************************************/ 2821 2822 static bool rocker_port_dev_check(const struct net_device *dev) 2823 { 2824 return dev->netdev_ops == &rocker_port_netdev_ops; 2825 } 2826 2827 static bool rocker_port_dev_check_under(const struct net_device *dev, 2828 struct rocker *rocker) 2829 { 2830 struct rocker_port *rocker_port; 2831 2832 if (!rocker_port_dev_check(dev)) 2833 return false; 2834 2835 rocker_port = netdev_priv(dev); 2836 if (rocker_port->rocker != rocker) 2837 return false; 2838 2839 return true; 2840 } 2841 2842 struct rocker_port *rocker_port_dev_lower_find(struct net_device *dev, 2843 struct rocker *rocker) 2844 { 2845 struct net_device *lower_dev; 2846 struct list_head *iter; 2847 2848 if (rocker_port_dev_check_under(dev, rocker)) 2849 return netdev_priv(dev); 2850 2851 netdev_for_each_all_lower_dev(dev, lower_dev, iter) { 2852 if (rocker_port_dev_check_under(lower_dev, rocker)) 2853 return netdev_priv(lower_dev); 2854 } 2855 return NULL; 2856 } 2857 2858 static int rocker_netdevice_event(struct notifier_block *unused, 2859 unsigned long event, void *ptr) 2860 { 2861 struct net_device *dev = netdev_notifier_info_to_dev(ptr); 2862 struct netdev_notifier_changeupper_info *info; 2863 struct rocker_port *rocker_port; 2864 int err; 2865 2866 if (!rocker_port_dev_check(dev)) 2867 return NOTIFY_DONE; 2868 2869 switch (event) { 2870 case NETDEV_CHANGEUPPER: 2871 info = ptr; 2872 if (!info->master) 2873 goto out; 2874 rocker_port = netdev_priv(dev); 2875 if (info->linking) { 2876 err = rocker_world_port_master_linked(rocker_port, 2877 info->upper_dev); 2878 if (err) 2879 netdev_warn(dev, "failed to reflect master linked (err %d)\n", 2880 err); 2881 } else { 2882 err = rocker_world_port_master_unlinked(rocker_port, 2883 info->upper_dev); 2884 if (err) 2885 netdev_warn(dev, "failed to reflect master unlinked (err %d)\n", 2886 err); 2887 } 2888 } 2889 out: 2890 return NOTIFY_DONE; 2891 } 2892 2893 static struct notifier_block rocker_netdevice_nb __read_mostly = { 2894 .notifier_call = rocker_netdevice_event, 2895 }; 2896 2897 /************************************ 2898 * Net event notifier event handler 2899 ************************************/ 2900 2901 static int rocker_netevent_event(struct notifier_block *unused, 2902 unsigned long event, void *ptr) 2903 { 2904 struct rocker_port *rocker_port; 2905 struct net_device *dev; 2906 struct neighbour *n = ptr; 2907 int err; 2908 2909 switch (event) { 2910 case NETEVENT_NEIGH_UPDATE: 2911 if (n->tbl != &arp_tbl) 2912 return NOTIFY_DONE; 2913 dev = n->dev; 2914 if (!rocker_port_dev_check(dev)) 2915 return NOTIFY_DONE; 2916 rocker_port = netdev_priv(dev); 2917 err = rocker_world_port_neigh_update(rocker_port, n); 2918 if (err) 2919 netdev_warn(dev, "failed to handle neigh update (err %d)\n", 2920 err); 2921 break; 2922 } 2923 2924 return NOTIFY_DONE; 2925 } 2926 2927 static struct notifier_block rocker_netevent_nb __read_mostly = { 2928 .notifier_call = rocker_netevent_event, 2929 }; 2930 2931 /*********************** 2932 * Module init and exit 2933 ***********************/ 2934 2935 static int __init rocker_module_init(void) 2936 { 2937 int err; 2938 2939 register_netdevice_notifier(&rocker_netdevice_nb); 2940 register_netevent_notifier(&rocker_netevent_nb); 2941 err = pci_register_driver(&rocker_pci_driver); 2942 if (err) 2943 goto err_pci_register_driver; 2944 return 0; 2945 2946 err_pci_register_driver: 2947 unregister_netevent_notifier(&rocker_netevent_nb); 2948 unregister_netdevice_notifier(&rocker_netdevice_nb); 2949 return err; 2950 } 2951 2952 static void __exit rocker_module_exit(void) 2953 { 2954 unregister_netevent_notifier(&rocker_netevent_nb); 2955 unregister_netdevice_notifier(&rocker_netdevice_nb); 2956 pci_unregister_driver(&rocker_pci_driver); 2957 } 2958 2959 module_init(rocker_module_init); 2960 module_exit(rocker_module_exit); 2961 2962 MODULE_LICENSE("GPL v2"); 2963 MODULE_AUTHOR("Jiri Pirko <jiri@resnulli.us>"); 2964 MODULE_AUTHOR("Scott Feldman <sfeldma@gmail.com>"); 2965 MODULE_DESCRIPTION("Rocker switch device driver"); 2966 MODULE_DEVICE_TABLE(pci, rocker_pci_id_table); 2967