gpio: rcar: Fix runtime PM imbalance on error
[linux/fpc-iii.git] / drivers / net / ethernet / rocker / rocker_main.c
blob7585cd2270ba49c1bcbbad37e94dc7b716784212
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * drivers/net/ethernet/rocker/rocker.c - Rocker switch device driver
4 * Copyright (c) 2014-2016 Jiri Pirko <jiri@mellanox.com>
5 * Copyright (c) 2014 Scott Feldman <sfeldma@gmail.com>
6 */
8 #include <linux/kernel.h>
9 #include <linux/module.h>
10 #include <linux/pci.h>
11 #include <linux/interrupt.h>
12 #include <linux/sched.h>
13 #include <linux/wait.h>
14 #include <linux/spinlock.h>
15 #include <linux/sort.h>
16 #include <linux/random.h>
17 #include <linux/netdevice.h>
18 #include <linux/skbuff.h>
19 #include <linux/socket.h>
20 #include <linux/etherdevice.h>
21 #include <linux/ethtool.h>
22 #include <linux/if_ether.h>
23 #include <linux/if_vlan.h>
24 #include <linux/if_bridge.h>
25 #include <linux/bitops.h>
26 #include <linux/ctype.h>
27 #include <linux/workqueue.h>
28 #include <net/switchdev.h>
29 #include <net/rtnetlink.h>
30 #include <net/netevent.h>
31 #include <net/arp.h>
32 #include <net/fib_rules.h>
33 #include <net/fib_notifier.h>
34 #include <linux/io-64-nonatomic-lo-hi.h>
35 #include <generated/utsrelease.h>
37 #include "rocker_hw.h"
38 #include "rocker.h"
39 #include "rocker_tlv.h"
41 static const char rocker_driver_name[] = "rocker";
43 static const struct pci_device_id rocker_pci_id_table[] = {
44 {PCI_VDEVICE(REDHAT, PCI_DEVICE_ID_REDHAT_ROCKER), 0},
45 {0, }
48 struct rocker_wait {
49 wait_queue_head_t wait;
50 bool done;
51 bool nowait;
54 static void rocker_wait_reset(struct rocker_wait *wait)
56 wait->done = false;
57 wait->nowait = false;
60 static void rocker_wait_init(struct rocker_wait *wait)
62 init_waitqueue_head(&wait->wait);
63 rocker_wait_reset(wait);
66 static struct rocker_wait *rocker_wait_create(void)
68 struct rocker_wait *wait;
70 wait = kzalloc(sizeof(*wait), GFP_KERNEL);
71 if (!wait)
72 return NULL;
73 return wait;
76 static void rocker_wait_destroy(struct rocker_wait *wait)
78 kfree(wait);
81 static bool rocker_wait_event_timeout(struct rocker_wait *wait,
82 unsigned long timeout)
84 wait_event_timeout(wait->wait, wait->done, HZ / 10);
85 if (!wait->done)
86 return false;
87 return true;
90 static void rocker_wait_wake_up(struct rocker_wait *wait)
92 wait->done = true;
93 wake_up(&wait->wait);
96 static u32 rocker_msix_vector(const struct rocker *rocker, unsigned int vector)
98 return rocker->msix_entries[vector].vector;
101 static u32 rocker_msix_tx_vector(const struct rocker_port *rocker_port)
103 return rocker_msix_vector(rocker_port->rocker,
104 ROCKER_MSIX_VEC_TX(rocker_port->port_number));
107 static u32 rocker_msix_rx_vector(const struct rocker_port *rocker_port)
109 return rocker_msix_vector(rocker_port->rocker,
110 ROCKER_MSIX_VEC_RX(rocker_port->port_number));
113 #define rocker_write32(rocker, reg, val) \
114 writel((val), (rocker)->hw_addr + (ROCKER_ ## reg))
115 #define rocker_read32(rocker, reg) \
116 readl((rocker)->hw_addr + (ROCKER_ ## reg))
117 #define rocker_write64(rocker, reg, val) \
118 writeq((val), (rocker)->hw_addr + (ROCKER_ ## reg))
119 #define rocker_read64(rocker, reg) \
120 readq((rocker)->hw_addr + (ROCKER_ ## reg))
122 /*****************************
123 * HW basic testing functions
124 *****************************/
126 static int rocker_reg_test(const struct rocker *rocker)
128 const struct pci_dev *pdev = rocker->pdev;
129 u64 test_reg;
130 u64 rnd;
132 rnd = prandom_u32();
133 rnd >>= 1;
134 rocker_write32(rocker, TEST_REG, rnd);
135 test_reg = rocker_read32(rocker, TEST_REG);
136 if (test_reg != rnd * 2) {
137 dev_err(&pdev->dev, "unexpected 32bit register value %08llx, expected %08llx\n",
138 test_reg, rnd * 2);
139 return -EIO;
142 rnd = prandom_u32();
143 rnd <<= 31;
144 rnd |= prandom_u32();
145 rocker_write64(rocker, TEST_REG64, rnd);
146 test_reg = rocker_read64(rocker, TEST_REG64);
147 if (test_reg != rnd * 2) {
148 dev_err(&pdev->dev, "unexpected 64bit register value %16llx, expected %16llx\n",
149 test_reg, rnd * 2);
150 return -EIO;
153 return 0;
156 static int rocker_dma_test_one(const struct rocker *rocker,
157 struct rocker_wait *wait, u32 test_type,
158 dma_addr_t dma_handle, const unsigned char *buf,
159 const unsigned char *expect, size_t size)
161 const struct pci_dev *pdev = rocker->pdev;
162 int i;
164 rocker_wait_reset(wait);
165 rocker_write32(rocker, TEST_DMA_CTRL, test_type);
167 if (!rocker_wait_event_timeout(wait, HZ / 10)) {
168 dev_err(&pdev->dev, "no interrupt received within a timeout\n");
169 return -EIO;
172 for (i = 0; i < size; i++) {
173 if (buf[i] != expect[i]) {
174 dev_err(&pdev->dev, "unexpected memory content %02x at byte %x\n, %02x expected",
175 buf[i], i, expect[i]);
176 return -EIO;
179 return 0;
182 #define ROCKER_TEST_DMA_BUF_SIZE (PAGE_SIZE * 4)
183 #define ROCKER_TEST_DMA_FILL_PATTERN 0x96
185 static int rocker_dma_test_offset(const struct rocker *rocker,
186 struct rocker_wait *wait, int offset)
188 struct pci_dev *pdev = rocker->pdev;
189 unsigned char *alloc;
190 unsigned char *buf;
191 unsigned char *expect;
192 dma_addr_t dma_handle;
193 int i;
194 int err;
196 alloc = kzalloc(ROCKER_TEST_DMA_BUF_SIZE * 2 + offset,
197 GFP_KERNEL | GFP_DMA);
198 if (!alloc)
199 return -ENOMEM;
200 buf = alloc + offset;
201 expect = buf + ROCKER_TEST_DMA_BUF_SIZE;
203 dma_handle = pci_map_single(pdev, buf, ROCKER_TEST_DMA_BUF_SIZE,
204 PCI_DMA_BIDIRECTIONAL);
205 if (pci_dma_mapping_error(pdev, dma_handle)) {
206 err = -EIO;
207 goto free_alloc;
210 rocker_write64(rocker, TEST_DMA_ADDR, dma_handle);
211 rocker_write32(rocker, TEST_DMA_SIZE, ROCKER_TEST_DMA_BUF_SIZE);
213 memset(expect, ROCKER_TEST_DMA_FILL_PATTERN, ROCKER_TEST_DMA_BUF_SIZE);
214 err = rocker_dma_test_one(rocker, wait, ROCKER_TEST_DMA_CTRL_FILL,
215 dma_handle, buf, expect,
216 ROCKER_TEST_DMA_BUF_SIZE);
217 if (err)
218 goto unmap;
220 memset(expect, 0, ROCKER_TEST_DMA_BUF_SIZE);
221 err = rocker_dma_test_one(rocker, wait, ROCKER_TEST_DMA_CTRL_CLEAR,
222 dma_handle, buf, expect,
223 ROCKER_TEST_DMA_BUF_SIZE);
224 if (err)
225 goto unmap;
227 prandom_bytes(buf, ROCKER_TEST_DMA_BUF_SIZE);
228 for (i = 0; i < ROCKER_TEST_DMA_BUF_SIZE; i++)
229 expect[i] = ~buf[i];
230 err = rocker_dma_test_one(rocker, wait, ROCKER_TEST_DMA_CTRL_INVERT,
231 dma_handle, buf, expect,
232 ROCKER_TEST_DMA_BUF_SIZE);
233 if (err)
234 goto unmap;
236 unmap:
237 pci_unmap_single(pdev, dma_handle, ROCKER_TEST_DMA_BUF_SIZE,
238 PCI_DMA_BIDIRECTIONAL);
239 free_alloc:
240 kfree(alloc);
242 return err;
245 static int rocker_dma_test(const struct rocker *rocker,
246 struct rocker_wait *wait)
248 int i;
249 int err;
251 for (i = 0; i < 8; i++) {
252 err = rocker_dma_test_offset(rocker, wait, i);
253 if (err)
254 return err;
256 return 0;
259 static irqreturn_t rocker_test_irq_handler(int irq, void *dev_id)
261 struct rocker_wait *wait = dev_id;
263 rocker_wait_wake_up(wait);
265 return IRQ_HANDLED;
268 static int rocker_basic_hw_test(const struct rocker *rocker)
270 const struct pci_dev *pdev = rocker->pdev;
271 struct rocker_wait wait;
272 int err;
274 err = rocker_reg_test(rocker);
275 if (err) {
276 dev_err(&pdev->dev, "reg test failed\n");
277 return err;
280 err = request_irq(rocker_msix_vector(rocker, ROCKER_MSIX_VEC_TEST),
281 rocker_test_irq_handler, 0,
282 rocker_driver_name, &wait);
283 if (err) {
284 dev_err(&pdev->dev, "cannot assign test irq\n");
285 return err;
288 rocker_wait_init(&wait);
289 rocker_write32(rocker, TEST_IRQ, ROCKER_MSIX_VEC_TEST);
291 if (!rocker_wait_event_timeout(&wait, HZ / 10)) {
292 dev_err(&pdev->dev, "no interrupt received within a timeout\n");
293 err = -EIO;
294 goto free_irq;
297 err = rocker_dma_test(rocker, &wait);
298 if (err)
299 dev_err(&pdev->dev, "dma test failed\n");
301 free_irq:
302 free_irq(rocker_msix_vector(rocker, ROCKER_MSIX_VEC_TEST), &wait);
303 return err;
306 /******************************************
307 * DMA rings and descriptors manipulations
308 ******************************************/
310 static u32 __pos_inc(u32 pos, size_t limit)
312 return ++pos == limit ? 0 : pos;
315 static int rocker_desc_err(const struct rocker_desc_info *desc_info)
317 int err = desc_info->desc->comp_err & ~ROCKER_DMA_DESC_COMP_ERR_GEN;
319 switch (err) {
320 case ROCKER_OK:
321 return 0;
322 case -ROCKER_ENOENT:
323 return -ENOENT;
324 case -ROCKER_ENXIO:
325 return -ENXIO;
326 case -ROCKER_ENOMEM:
327 return -ENOMEM;
328 case -ROCKER_EEXIST:
329 return -EEXIST;
330 case -ROCKER_EINVAL:
331 return -EINVAL;
332 case -ROCKER_EMSGSIZE:
333 return -EMSGSIZE;
334 case -ROCKER_ENOTSUP:
335 return -EOPNOTSUPP;
336 case -ROCKER_ENOBUFS:
337 return -ENOBUFS;
340 return -EINVAL;
343 static void rocker_desc_gen_clear(const struct rocker_desc_info *desc_info)
345 desc_info->desc->comp_err &= ~ROCKER_DMA_DESC_COMP_ERR_GEN;
348 static bool rocker_desc_gen(const struct rocker_desc_info *desc_info)
350 u32 comp_err = desc_info->desc->comp_err;
352 return comp_err & ROCKER_DMA_DESC_COMP_ERR_GEN ? true : false;
355 static void *
356 rocker_desc_cookie_ptr_get(const struct rocker_desc_info *desc_info)
358 return (void *)(uintptr_t)desc_info->desc->cookie;
361 static void rocker_desc_cookie_ptr_set(const struct rocker_desc_info *desc_info,
362 void *ptr)
364 desc_info->desc->cookie = (uintptr_t) ptr;
367 static struct rocker_desc_info *
368 rocker_desc_head_get(const struct rocker_dma_ring_info *info)
370 struct rocker_desc_info *desc_info;
371 u32 head = __pos_inc(info->head, info->size);
373 desc_info = &info->desc_info[info->head];
374 if (head == info->tail)
375 return NULL; /* ring full */
376 desc_info->tlv_size = 0;
377 return desc_info;
380 static void rocker_desc_commit(const struct rocker_desc_info *desc_info)
382 desc_info->desc->buf_size = desc_info->data_size;
383 desc_info->desc->tlv_size = desc_info->tlv_size;
386 static void rocker_desc_head_set(const struct rocker *rocker,
387 struct rocker_dma_ring_info *info,
388 const struct rocker_desc_info *desc_info)
390 u32 head = __pos_inc(info->head, info->size);
392 BUG_ON(head == info->tail);
393 rocker_desc_commit(desc_info);
394 info->head = head;
395 rocker_write32(rocker, DMA_DESC_HEAD(info->type), head);
398 static struct rocker_desc_info *
399 rocker_desc_tail_get(struct rocker_dma_ring_info *info)
401 struct rocker_desc_info *desc_info;
403 if (info->tail == info->head)
404 return NULL; /* nothing to be done between head and tail */
405 desc_info = &info->desc_info[info->tail];
406 if (!rocker_desc_gen(desc_info))
407 return NULL; /* gen bit not set, desc is not ready yet */
408 info->tail = __pos_inc(info->tail, info->size);
409 desc_info->tlv_size = desc_info->desc->tlv_size;
410 return desc_info;
413 static void rocker_dma_ring_credits_set(const struct rocker *rocker,
414 const struct rocker_dma_ring_info *info,
415 u32 credits)
417 if (credits)
418 rocker_write32(rocker, DMA_DESC_CREDITS(info->type), credits);
421 static unsigned long rocker_dma_ring_size_fix(size_t size)
423 return max(ROCKER_DMA_SIZE_MIN,
424 min(roundup_pow_of_two(size), ROCKER_DMA_SIZE_MAX));
427 static int rocker_dma_ring_create(const struct rocker *rocker,
428 unsigned int type,
429 size_t size,
430 struct rocker_dma_ring_info *info)
432 int i;
434 BUG_ON(size != rocker_dma_ring_size_fix(size));
435 info->size = size;
436 info->type = type;
437 info->head = 0;
438 info->tail = 0;
439 info->desc_info = kcalloc(info->size, sizeof(*info->desc_info),
440 GFP_KERNEL);
441 if (!info->desc_info)
442 return -ENOMEM;
444 info->desc = pci_alloc_consistent(rocker->pdev,
445 info->size * sizeof(*info->desc),
446 &info->mapaddr);
447 if (!info->desc) {
448 kfree(info->desc_info);
449 return -ENOMEM;
452 for (i = 0; i < info->size; i++)
453 info->desc_info[i].desc = &info->desc[i];
455 rocker_write32(rocker, DMA_DESC_CTRL(info->type),
456 ROCKER_DMA_DESC_CTRL_RESET);
457 rocker_write64(rocker, DMA_DESC_ADDR(info->type), info->mapaddr);
458 rocker_write32(rocker, DMA_DESC_SIZE(info->type), info->size);
460 return 0;
463 static void rocker_dma_ring_destroy(const struct rocker *rocker,
464 const struct rocker_dma_ring_info *info)
466 rocker_write64(rocker, DMA_DESC_ADDR(info->type), 0);
468 pci_free_consistent(rocker->pdev,
469 info->size * sizeof(struct rocker_desc),
470 info->desc, info->mapaddr);
471 kfree(info->desc_info);
474 static void rocker_dma_ring_pass_to_producer(const struct rocker *rocker,
475 struct rocker_dma_ring_info *info)
477 int i;
479 BUG_ON(info->head || info->tail);
481 /* When ring is consumer, we need to advance head for each desc.
482 * That tells hw that the desc is ready to be used by it.
484 for (i = 0; i < info->size - 1; i++)
485 rocker_desc_head_set(rocker, info, &info->desc_info[i]);
486 rocker_desc_commit(&info->desc_info[i]);
489 static int rocker_dma_ring_bufs_alloc(const struct rocker *rocker,
490 const struct rocker_dma_ring_info *info,
491 int direction, size_t buf_size)
493 struct pci_dev *pdev = rocker->pdev;
494 int i;
495 int err;
497 for (i = 0; i < info->size; i++) {
498 struct rocker_desc_info *desc_info = &info->desc_info[i];
499 struct rocker_desc *desc = &info->desc[i];
500 dma_addr_t dma_handle;
501 char *buf;
503 buf = kzalloc(buf_size, GFP_KERNEL | GFP_DMA);
504 if (!buf) {
505 err = -ENOMEM;
506 goto rollback;
509 dma_handle = pci_map_single(pdev, buf, buf_size, direction);
510 if (pci_dma_mapping_error(pdev, dma_handle)) {
511 kfree(buf);
512 err = -EIO;
513 goto rollback;
516 desc_info->data = buf;
517 desc_info->data_size = buf_size;
518 dma_unmap_addr_set(desc_info, mapaddr, dma_handle);
520 desc->buf_addr = dma_handle;
521 desc->buf_size = buf_size;
523 return 0;
525 rollback:
526 for (i--; i >= 0; i--) {
527 const struct rocker_desc_info *desc_info = &info->desc_info[i];
529 pci_unmap_single(pdev, dma_unmap_addr(desc_info, mapaddr),
530 desc_info->data_size, direction);
531 kfree(desc_info->data);
533 return err;
536 static void rocker_dma_ring_bufs_free(const struct rocker *rocker,
537 const struct rocker_dma_ring_info *info,
538 int direction)
540 struct pci_dev *pdev = rocker->pdev;
541 int i;
543 for (i = 0; i < info->size; i++) {
544 const struct rocker_desc_info *desc_info = &info->desc_info[i];
545 struct rocker_desc *desc = &info->desc[i];
547 desc->buf_addr = 0;
548 desc->buf_size = 0;
549 pci_unmap_single(pdev, dma_unmap_addr(desc_info, mapaddr),
550 desc_info->data_size, direction);
551 kfree(desc_info->data);
555 static int rocker_dma_cmd_ring_wait_alloc(struct rocker_desc_info *desc_info)
557 struct rocker_wait *wait;
559 wait = rocker_wait_create();
560 if (!wait)
561 return -ENOMEM;
562 rocker_desc_cookie_ptr_set(desc_info, wait);
563 return 0;
566 static void
567 rocker_dma_cmd_ring_wait_free(const struct rocker_desc_info *desc_info)
569 struct rocker_wait *wait = rocker_desc_cookie_ptr_get(desc_info);
571 rocker_wait_destroy(wait);
574 static int rocker_dma_cmd_ring_waits_alloc(const struct rocker *rocker)
576 const struct rocker_dma_ring_info *cmd_ring = &rocker->cmd_ring;
577 int i;
578 int err;
580 for (i = 0; i < cmd_ring->size; i++) {
581 err = rocker_dma_cmd_ring_wait_alloc(&cmd_ring->desc_info[i]);
582 if (err)
583 goto rollback;
585 return 0;
587 rollback:
588 for (i--; i >= 0; i--)
589 rocker_dma_cmd_ring_wait_free(&cmd_ring->desc_info[i]);
590 return err;
593 static void rocker_dma_cmd_ring_waits_free(const struct rocker *rocker)
595 const struct rocker_dma_ring_info *cmd_ring = &rocker->cmd_ring;
596 int i;
598 for (i = 0; i < cmd_ring->size; i++)
599 rocker_dma_cmd_ring_wait_free(&cmd_ring->desc_info[i]);
602 static int rocker_dma_rings_init(struct rocker *rocker)
604 const struct pci_dev *pdev = rocker->pdev;
605 int err;
607 err = rocker_dma_ring_create(rocker, ROCKER_DMA_CMD,
608 ROCKER_DMA_CMD_DEFAULT_SIZE,
609 &rocker->cmd_ring);
610 if (err) {
611 dev_err(&pdev->dev, "failed to create command dma ring\n");
612 return err;
615 spin_lock_init(&rocker->cmd_ring_lock);
617 err = rocker_dma_ring_bufs_alloc(rocker, &rocker->cmd_ring,
618 PCI_DMA_BIDIRECTIONAL, PAGE_SIZE);
619 if (err) {
620 dev_err(&pdev->dev, "failed to alloc command dma ring buffers\n");
621 goto err_dma_cmd_ring_bufs_alloc;
624 err = rocker_dma_cmd_ring_waits_alloc(rocker);
625 if (err) {
626 dev_err(&pdev->dev, "failed to alloc command dma ring waits\n");
627 goto err_dma_cmd_ring_waits_alloc;
630 err = rocker_dma_ring_create(rocker, ROCKER_DMA_EVENT,
631 ROCKER_DMA_EVENT_DEFAULT_SIZE,
632 &rocker->event_ring);
633 if (err) {
634 dev_err(&pdev->dev, "failed to create event dma ring\n");
635 goto err_dma_event_ring_create;
638 err = rocker_dma_ring_bufs_alloc(rocker, &rocker->event_ring,
639 PCI_DMA_FROMDEVICE, PAGE_SIZE);
640 if (err) {
641 dev_err(&pdev->dev, "failed to alloc event dma ring buffers\n");
642 goto err_dma_event_ring_bufs_alloc;
644 rocker_dma_ring_pass_to_producer(rocker, &rocker->event_ring);
645 return 0;
647 err_dma_event_ring_bufs_alloc:
648 rocker_dma_ring_destroy(rocker, &rocker->event_ring);
649 err_dma_event_ring_create:
650 rocker_dma_ring_bufs_free(rocker, &rocker->cmd_ring,
651 PCI_DMA_BIDIRECTIONAL);
652 err_dma_cmd_ring_waits_alloc:
653 rocker_dma_cmd_ring_waits_free(rocker);
654 err_dma_cmd_ring_bufs_alloc:
655 rocker_dma_ring_destroy(rocker, &rocker->cmd_ring);
656 return err;
659 static void rocker_dma_rings_fini(struct rocker *rocker)
661 rocker_dma_ring_bufs_free(rocker, &rocker->event_ring,
662 PCI_DMA_BIDIRECTIONAL);
663 rocker_dma_ring_destroy(rocker, &rocker->event_ring);
664 rocker_dma_cmd_ring_waits_free(rocker);
665 rocker_dma_ring_bufs_free(rocker, &rocker->cmd_ring,
666 PCI_DMA_BIDIRECTIONAL);
667 rocker_dma_ring_destroy(rocker, &rocker->cmd_ring);
670 static int rocker_dma_rx_ring_skb_map(const struct rocker_port *rocker_port,
671 struct rocker_desc_info *desc_info,
672 struct sk_buff *skb, size_t buf_len)
674 const struct rocker *rocker = rocker_port->rocker;
675 struct pci_dev *pdev = rocker->pdev;
676 dma_addr_t dma_handle;
678 dma_handle = pci_map_single(pdev, skb->data, buf_len,
679 PCI_DMA_FROMDEVICE);
680 if (pci_dma_mapping_error(pdev, dma_handle))
681 return -EIO;
682 if (rocker_tlv_put_u64(desc_info, ROCKER_TLV_RX_FRAG_ADDR, dma_handle))
683 goto tlv_put_failure;
684 if (rocker_tlv_put_u16(desc_info, ROCKER_TLV_RX_FRAG_MAX_LEN, buf_len))
685 goto tlv_put_failure;
686 return 0;
688 tlv_put_failure:
689 pci_unmap_single(pdev, dma_handle, buf_len, PCI_DMA_FROMDEVICE);
690 desc_info->tlv_size = 0;
691 return -EMSGSIZE;
694 static size_t rocker_port_rx_buf_len(const struct rocker_port *rocker_port)
696 return rocker_port->dev->mtu + ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN;
699 static int rocker_dma_rx_ring_skb_alloc(const struct rocker_port *rocker_port,
700 struct rocker_desc_info *desc_info)
702 struct net_device *dev = rocker_port->dev;
703 struct sk_buff *skb;
704 size_t buf_len = rocker_port_rx_buf_len(rocker_port);
705 int err;
707 /* Ensure that hw will see tlv_size zero in case of an error.
708 * That tells hw to use another descriptor.
710 rocker_desc_cookie_ptr_set(desc_info, NULL);
711 desc_info->tlv_size = 0;
713 skb = netdev_alloc_skb_ip_align(dev, buf_len);
714 if (!skb)
715 return -ENOMEM;
716 err = rocker_dma_rx_ring_skb_map(rocker_port, desc_info, skb, buf_len);
717 if (err) {
718 dev_kfree_skb_any(skb);
719 return err;
721 rocker_desc_cookie_ptr_set(desc_info, skb);
722 return 0;
725 static void rocker_dma_rx_ring_skb_unmap(const struct rocker *rocker,
726 const struct rocker_tlv **attrs)
728 struct pci_dev *pdev = rocker->pdev;
729 dma_addr_t dma_handle;
730 size_t len;
732 if (!attrs[ROCKER_TLV_RX_FRAG_ADDR] ||
733 !attrs[ROCKER_TLV_RX_FRAG_MAX_LEN])
734 return;
735 dma_handle = rocker_tlv_get_u64(attrs[ROCKER_TLV_RX_FRAG_ADDR]);
736 len = rocker_tlv_get_u16(attrs[ROCKER_TLV_RX_FRAG_MAX_LEN]);
737 pci_unmap_single(pdev, dma_handle, len, PCI_DMA_FROMDEVICE);
740 static void rocker_dma_rx_ring_skb_free(const struct rocker *rocker,
741 const struct rocker_desc_info *desc_info)
743 const struct rocker_tlv *attrs[ROCKER_TLV_RX_MAX + 1];
744 struct sk_buff *skb = rocker_desc_cookie_ptr_get(desc_info);
746 if (!skb)
747 return;
748 rocker_tlv_parse_desc(attrs, ROCKER_TLV_RX_MAX, desc_info);
749 rocker_dma_rx_ring_skb_unmap(rocker, attrs);
750 dev_kfree_skb_any(skb);
753 static int rocker_dma_rx_ring_skbs_alloc(const struct rocker_port *rocker_port)
755 const struct rocker_dma_ring_info *rx_ring = &rocker_port->rx_ring;
756 const struct rocker *rocker = rocker_port->rocker;
757 int i;
758 int err;
760 for (i = 0; i < rx_ring->size; i++) {
761 err = rocker_dma_rx_ring_skb_alloc(rocker_port,
762 &rx_ring->desc_info[i]);
763 if (err)
764 goto rollback;
766 return 0;
768 rollback:
769 for (i--; i >= 0; i--)
770 rocker_dma_rx_ring_skb_free(rocker, &rx_ring->desc_info[i]);
771 return err;
774 static void rocker_dma_rx_ring_skbs_free(const struct rocker_port *rocker_port)
776 const struct rocker_dma_ring_info *rx_ring = &rocker_port->rx_ring;
777 const struct rocker *rocker = rocker_port->rocker;
778 int i;
780 for (i = 0; i < rx_ring->size; i++)
781 rocker_dma_rx_ring_skb_free(rocker, &rx_ring->desc_info[i]);
784 static int rocker_port_dma_rings_init(struct rocker_port *rocker_port)
786 struct rocker *rocker = rocker_port->rocker;
787 int err;
789 err = rocker_dma_ring_create(rocker,
790 ROCKER_DMA_TX(rocker_port->port_number),
791 ROCKER_DMA_TX_DEFAULT_SIZE,
792 &rocker_port->tx_ring);
793 if (err) {
794 netdev_err(rocker_port->dev, "failed to create tx dma ring\n");
795 return err;
798 err = rocker_dma_ring_bufs_alloc(rocker, &rocker_port->tx_ring,
799 PCI_DMA_TODEVICE,
800 ROCKER_DMA_TX_DESC_SIZE);
801 if (err) {
802 netdev_err(rocker_port->dev, "failed to alloc tx dma ring buffers\n");
803 goto err_dma_tx_ring_bufs_alloc;
806 err = rocker_dma_ring_create(rocker,
807 ROCKER_DMA_RX(rocker_port->port_number),
808 ROCKER_DMA_RX_DEFAULT_SIZE,
809 &rocker_port->rx_ring);
810 if (err) {
811 netdev_err(rocker_port->dev, "failed to create rx dma ring\n");
812 goto err_dma_rx_ring_create;
815 err = rocker_dma_ring_bufs_alloc(rocker, &rocker_port->rx_ring,
816 PCI_DMA_BIDIRECTIONAL,
817 ROCKER_DMA_RX_DESC_SIZE);
818 if (err) {
819 netdev_err(rocker_port->dev, "failed to alloc rx dma ring buffers\n");
820 goto err_dma_rx_ring_bufs_alloc;
823 err = rocker_dma_rx_ring_skbs_alloc(rocker_port);
824 if (err) {
825 netdev_err(rocker_port->dev, "failed to alloc rx dma ring skbs\n");
826 goto err_dma_rx_ring_skbs_alloc;
828 rocker_dma_ring_pass_to_producer(rocker, &rocker_port->rx_ring);
830 return 0;
832 err_dma_rx_ring_skbs_alloc:
833 rocker_dma_ring_bufs_free(rocker, &rocker_port->rx_ring,
834 PCI_DMA_BIDIRECTIONAL);
835 err_dma_rx_ring_bufs_alloc:
836 rocker_dma_ring_destroy(rocker, &rocker_port->rx_ring);
837 err_dma_rx_ring_create:
838 rocker_dma_ring_bufs_free(rocker, &rocker_port->tx_ring,
839 PCI_DMA_TODEVICE);
840 err_dma_tx_ring_bufs_alloc:
841 rocker_dma_ring_destroy(rocker, &rocker_port->tx_ring);
842 return err;
845 static void rocker_port_dma_rings_fini(struct rocker_port *rocker_port)
847 struct rocker *rocker = rocker_port->rocker;
849 rocker_dma_rx_ring_skbs_free(rocker_port);
850 rocker_dma_ring_bufs_free(rocker, &rocker_port->rx_ring,
851 PCI_DMA_BIDIRECTIONAL);
852 rocker_dma_ring_destroy(rocker, &rocker_port->rx_ring);
853 rocker_dma_ring_bufs_free(rocker, &rocker_port->tx_ring,
854 PCI_DMA_TODEVICE);
855 rocker_dma_ring_destroy(rocker, &rocker_port->tx_ring);
858 static void rocker_port_set_enable(const struct rocker_port *rocker_port,
859 bool enable)
861 u64 val = rocker_read64(rocker_port->rocker, PORT_PHYS_ENABLE);
863 if (enable)
864 val |= 1ULL << rocker_port->pport;
865 else
866 val &= ~(1ULL << rocker_port->pport);
867 rocker_write64(rocker_port->rocker, PORT_PHYS_ENABLE, val);
870 /********************************
871 * Interrupt handler and helpers
872 ********************************/
874 static irqreturn_t rocker_cmd_irq_handler(int irq, void *dev_id)
876 struct rocker *rocker = dev_id;
877 const struct rocker_desc_info *desc_info;
878 struct rocker_wait *wait;
879 u32 credits = 0;
881 spin_lock(&rocker->cmd_ring_lock);
882 while ((desc_info = rocker_desc_tail_get(&rocker->cmd_ring))) {
883 wait = rocker_desc_cookie_ptr_get(desc_info);
884 if (wait->nowait) {
885 rocker_desc_gen_clear(desc_info);
886 } else {
887 rocker_wait_wake_up(wait);
889 credits++;
891 spin_unlock(&rocker->cmd_ring_lock);
892 rocker_dma_ring_credits_set(rocker, &rocker->cmd_ring, credits);
894 return IRQ_HANDLED;
897 static void rocker_port_link_up(const struct rocker_port *rocker_port)
899 netif_carrier_on(rocker_port->dev);
900 netdev_info(rocker_port->dev, "Link is up\n");
903 static void rocker_port_link_down(const struct rocker_port *rocker_port)
905 netif_carrier_off(rocker_port->dev);
906 netdev_info(rocker_port->dev, "Link is down\n");
909 static int rocker_event_link_change(const struct rocker *rocker,
910 const struct rocker_tlv *info)
912 const struct rocker_tlv *attrs[ROCKER_TLV_EVENT_LINK_CHANGED_MAX + 1];
913 unsigned int port_number;
914 bool link_up;
915 struct rocker_port *rocker_port;
917 rocker_tlv_parse_nested(attrs, ROCKER_TLV_EVENT_LINK_CHANGED_MAX, info);
918 if (!attrs[ROCKER_TLV_EVENT_LINK_CHANGED_PPORT] ||
919 !attrs[ROCKER_TLV_EVENT_LINK_CHANGED_LINKUP])
920 return -EIO;
921 port_number =
922 rocker_tlv_get_u32(attrs[ROCKER_TLV_EVENT_LINK_CHANGED_PPORT]) - 1;
923 link_up = rocker_tlv_get_u8(attrs[ROCKER_TLV_EVENT_LINK_CHANGED_LINKUP]);
925 if (port_number >= rocker->port_count)
926 return -EINVAL;
928 rocker_port = rocker->ports[port_number];
929 if (netif_carrier_ok(rocker_port->dev) != link_up) {
930 if (link_up)
931 rocker_port_link_up(rocker_port);
932 else
933 rocker_port_link_down(rocker_port);
936 return 0;
939 static int rocker_world_port_ev_mac_vlan_seen(struct rocker_port *rocker_port,
940 const unsigned char *addr,
941 __be16 vlan_id);
943 static int rocker_event_mac_vlan_seen(const struct rocker *rocker,
944 const struct rocker_tlv *info)
946 const struct rocker_tlv *attrs[ROCKER_TLV_EVENT_MAC_VLAN_MAX + 1];
947 unsigned int port_number;
948 struct rocker_port *rocker_port;
949 const unsigned char *addr;
950 __be16 vlan_id;
952 rocker_tlv_parse_nested(attrs, ROCKER_TLV_EVENT_MAC_VLAN_MAX, info);
953 if (!attrs[ROCKER_TLV_EVENT_MAC_VLAN_PPORT] ||
954 !attrs[ROCKER_TLV_EVENT_MAC_VLAN_MAC] ||
955 !attrs[ROCKER_TLV_EVENT_MAC_VLAN_VLAN_ID])
956 return -EIO;
957 port_number =
958 rocker_tlv_get_u32(attrs[ROCKER_TLV_EVENT_MAC_VLAN_PPORT]) - 1;
959 addr = rocker_tlv_data(attrs[ROCKER_TLV_EVENT_MAC_VLAN_MAC]);
960 vlan_id = rocker_tlv_get_be16(attrs[ROCKER_TLV_EVENT_MAC_VLAN_VLAN_ID]);
962 if (port_number >= rocker->port_count)
963 return -EINVAL;
965 rocker_port = rocker->ports[port_number];
966 return rocker_world_port_ev_mac_vlan_seen(rocker_port, addr, vlan_id);
969 static int rocker_event_process(const struct rocker *rocker,
970 const struct rocker_desc_info *desc_info)
972 const struct rocker_tlv *attrs[ROCKER_TLV_EVENT_MAX + 1];
973 const struct rocker_tlv *info;
974 u16 type;
976 rocker_tlv_parse_desc(attrs, ROCKER_TLV_EVENT_MAX, desc_info);
977 if (!attrs[ROCKER_TLV_EVENT_TYPE] ||
978 !attrs[ROCKER_TLV_EVENT_INFO])
979 return -EIO;
981 type = rocker_tlv_get_u16(attrs[ROCKER_TLV_EVENT_TYPE]);
982 info = attrs[ROCKER_TLV_EVENT_INFO];
984 switch (type) {
985 case ROCKER_TLV_EVENT_TYPE_LINK_CHANGED:
986 return rocker_event_link_change(rocker, info);
987 case ROCKER_TLV_EVENT_TYPE_MAC_VLAN_SEEN:
988 return rocker_event_mac_vlan_seen(rocker, info);
991 return -EOPNOTSUPP;
994 static irqreturn_t rocker_event_irq_handler(int irq, void *dev_id)
996 struct rocker *rocker = dev_id;
997 const struct pci_dev *pdev = rocker->pdev;
998 const struct rocker_desc_info *desc_info;
999 u32 credits = 0;
1000 int err;
1002 while ((desc_info = rocker_desc_tail_get(&rocker->event_ring))) {
1003 err = rocker_desc_err(desc_info);
1004 if (err) {
1005 dev_err(&pdev->dev, "event desc received with err %d\n",
1006 err);
1007 } else {
1008 err = rocker_event_process(rocker, desc_info);
1009 if (err)
1010 dev_err(&pdev->dev, "event processing failed with err %d\n",
1011 err);
1013 rocker_desc_gen_clear(desc_info);
1014 rocker_desc_head_set(rocker, &rocker->event_ring, desc_info);
1015 credits++;
1017 rocker_dma_ring_credits_set(rocker, &rocker->event_ring, credits);
1019 return IRQ_HANDLED;
1022 static irqreturn_t rocker_tx_irq_handler(int irq, void *dev_id)
1024 struct rocker_port *rocker_port = dev_id;
1026 napi_schedule(&rocker_port->napi_tx);
1027 return IRQ_HANDLED;
1030 static irqreturn_t rocker_rx_irq_handler(int irq, void *dev_id)
1032 struct rocker_port *rocker_port = dev_id;
1034 napi_schedule(&rocker_port->napi_rx);
1035 return IRQ_HANDLED;
1038 /********************
1039 * Command interface
1040 ********************/
1042 int rocker_cmd_exec(struct rocker_port *rocker_port, bool nowait,
1043 rocker_cmd_prep_cb_t prepare, void *prepare_priv,
1044 rocker_cmd_proc_cb_t process, void *process_priv)
1046 struct rocker *rocker = rocker_port->rocker;
1047 struct rocker_desc_info *desc_info;
1048 struct rocker_wait *wait;
1049 unsigned long lock_flags;
1050 int err;
1052 spin_lock_irqsave(&rocker->cmd_ring_lock, lock_flags);
1054 desc_info = rocker_desc_head_get(&rocker->cmd_ring);
1055 if (!desc_info) {
1056 spin_unlock_irqrestore(&rocker->cmd_ring_lock, lock_flags);
1057 return -EAGAIN;
1060 wait = rocker_desc_cookie_ptr_get(desc_info);
1061 rocker_wait_init(wait);
1062 wait->nowait = nowait;
1064 err = prepare(rocker_port, desc_info, prepare_priv);
1065 if (err) {
1066 spin_unlock_irqrestore(&rocker->cmd_ring_lock, lock_flags);
1067 return err;
1070 rocker_desc_head_set(rocker, &rocker->cmd_ring, desc_info);
1072 spin_unlock_irqrestore(&rocker->cmd_ring_lock, lock_flags);
1074 if (nowait)
1075 return 0;
1077 if (!rocker_wait_event_timeout(wait, HZ / 10))
1078 return -EIO;
1080 err = rocker_desc_err(desc_info);
1081 if (err)
1082 return err;
1084 if (process)
1085 err = process(rocker_port, desc_info, process_priv);
1087 rocker_desc_gen_clear(desc_info);
1088 return err;
1091 static int
1092 rocker_cmd_get_port_settings_prep(const struct rocker_port *rocker_port,
1093 struct rocker_desc_info *desc_info,
1094 void *priv)
1096 struct rocker_tlv *cmd_info;
1098 if (rocker_tlv_put_u16(desc_info, ROCKER_TLV_CMD_TYPE,
1099 ROCKER_TLV_CMD_TYPE_GET_PORT_SETTINGS))
1100 return -EMSGSIZE;
1101 cmd_info = rocker_tlv_nest_start(desc_info, ROCKER_TLV_CMD_INFO);
1102 if (!cmd_info)
1103 return -EMSGSIZE;
1104 if (rocker_tlv_put_u32(desc_info, ROCKER_TLV_CMD_PORT_SETTINGS_PPORT,
1105 rocker_port->pport))
1106 return -EMSGSIZE;
1107 rocker_tlv_nest_end(desc_info, cmd_info);
1108 return 0;
1111 static int
1112 rocker_cmd_get_port_settings_ethtool_proc(const struct rocker_port *rocker_port,
1113 const struct rocker_desc_info *desc_info,
1114 void *priv)
1116 struct ethtool_link_ksettings *ecmd = priv;
1117 const struct rocker_tlv *attrs[ROCKER_TLV_CMD_MAX + 1];
1118 const struct rocker_tlv *info_attrs[ROCKER_TLV_CMD_PORT_SETTINGS_MAX + 1];
1119 u32 speed;
1120 u8 duplex;
1121 u8 autoneg;
1123 rocker_tlv_parse_desc(attrs, ROCKER_TLV_CMD_MAX, desc_info);
1124 if (!attrs[ROCKER_TLV_CMD_INFO])
1125 return -EIO;
1127 rocker_tlv_parse_nested(info_attrs, ROCKER_TLV_CMD_PORT_SETTINGS_MAX,
1128 attrs[ROCKER_TLV_CMD_INFO]);
1129 if (!info_attrs[ROCKER_TLV_CMD_PORT_SETTINGS_SPEED] ||
1130 !info_attrs[ROCKER_TLV_CMD_PORT_SETTINGS_DUPLEX] ||
1131 !info_attrs[ROCKER_TLV_CMD_PORT_SETTINGS_AUTONEG])
1132 return -EIO;
1134 speed = rocker_tlv_get_u32(info_attrs[ROCKER_TLV_CMD_PORT_SETTINGS_SPEED]);
1135 duplex = rocker_tlv_get_u8(info_attrs[ROCKER_TLV_CMD_PORT_SETTINGS_DUPLEX]);
1136 autoneg = rocker_tlv_get_u8(info_attrs[ROCKER_TLV_CMD_PORT_SETTINGS_AUTONEG]);
1138 ethtool_link_ksettings_zero_link_mode(ecmd, supported);
1139 ethtool_link_ksettings_add_link_mode(ecmd, supported, TP);
1141 ecmd->base.phy_address = 0xff;
1142 ecmd->base.port = PORT_TP;
1143 ecmd->base.speed = speed;
1144 ecmd->base.duplex = duplex ? DUPLEX_FULL : DUPLEX_HALF;
1145 ecmd->base.autoneg = autoneg ? AUTONEG_ENABLE : AUTONEG_DISABLE;
1147 return 0;
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)
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;
1160 rocker_tlv_parse_desc(attrs, ROCKER_TLV_CMD_MAX, desc_info);
1161 if (!attrs[ROCKER_TLV_CMD_INFO])
1162 return -EIO;
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;
1170 if (rocker_tlv_len(attr) != ETH_ALEN)
1171 return -EINVAL;
1173 ether_addr_copy(macaddr, rocker_tlv_data(attr));
1174 return 0;
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)
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;
1187 rocker_tlv_parse_desc(attrs, ROCKER_TLV_CMD_MAX, desc_info);
1188 if (!attrs[ROCKER_TLV_CMD_INFO])
1189 return -EIO;
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;
1197 *p_mode = rocker_tlv_get_u8(info_attrs[ROCKER_TLV_CMD_PORT_SETTINGS_MODE]);
1198 return 0;
1201 struct port_name {
1202 char *buf;
1203 size_t len;
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)
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;
1218 rocker_tlv_parse_desc(attrs, ROCKER_TLV_CMD_MAX, desc_info);
1219 if (!attrs[ROCKER_TLV_CMD_INFO])
1220 return -EIO;
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;
1228 len = min_t(size_t, rocker_tlv_len(attr), name->len);
1229 str = rocker_tlv_data(attr);
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++;
1239 if (j == 0)
1240 return -EIO;
1242 name->buf[j] = '\0';
1244 return 0;
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)
1252 struct ethtool_link_ksettings *ecmd = priv;
1253 struct rocker_tlv *cmd_info;
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 ecmd->base.speed))
1266 return -EMSGSIZE;
1267 if (rocker_tlv_put_u8(desc_info, ROCKER_TLV_CMD_PORT_SETTINGS_DUPLEX,
1268 ecmd->base.duplex))
1269 return -EMSGSIZE;
1270 if (rocker_tlv_put_u8(desc_info, ROCKER_TLV_CMD_PORT_SETTINGS_AUTONEG,
1271 ecmd->base.autoneg))
1272 return -EMSGSIZE;
1273 rocker_tlv_nest_end(desc_info, cmd_info);
1274 return 0;
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)
1282 const unsigned char *macaddr = priv;
1283 struct rocker_tlv *cmd_info;
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;
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)
1306 int mtu = *(int *)priv;
1307 struct rocker_tlv *cmd_info;
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;
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)
1330 bool learning = *(bool *)priv;
1331 struct rocker_tlv *cmd_info;
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;
1349 static int
1350 rocker_cmd_get_port_settings_ethtool(struct rocker_port *rocker_port,
1351 struct ethtool_link_ksettings *ecmd)
1353 return rocker_cmd_exec(rocker_port, false,
1354 rocker_cmd_get_port_settings_prep, NULL,
1355 rocker_cmd_get_port_settings_ethtool_proc,
1356 ecmd);
1359 static int rocker_cmd_get_port_settings_macaddr(struct rocker_port *rocker_port,
1360 unsigned char *macaddr)
1362 return rocker_cmd_exec(rocker_port, false,
1363 rocker_cmd_get_port_settings_prep, NULL,
1364 rocker_cmd_get_port_settings_macaddr_proc,
1365 macaddr);
1368 static int rocker_cmd_get_port_settings_mode(struct rocker_port *rocker_port,
1369 u8 *p_mode)
1371 return rocker_cmd_exec(rocker_port, false,
1372 rocker_cmd_get_port_settings_prep, NULL,
1373 rocker_cmd_get_port_settings_mode_proc, p_mode);
1376 static int
1377 rocker_cmd_set_port_settings_ethtool(struct rocker_port *rocker_port,
1378 const struct ethtool_link_ksettings *ecmd)
1380 struct ethtool_link_ksettings copy_ecmd;
1382 memcpy(&copy_ecmd, ecmd, sizeof(copy_ecmd));
1384 return rocker_cmd_exec(rocker_port, false,
1385 rocker_cmd_set_port_settings_ethtool_prep,
1386 &copy_ecmd, NULL, NULL);
1389 static int rocker_cmd_set_port_settings_macaddr(struct rocker_port *rocker_port,
1390 unsigned char *macaddr)
1392 return rocker_cmd_exec(rocker_port, false,
1393 rocker_cmd_set_port_settings_macaddr_prep,
1394 macaddr, NULL, NULL);
1397 static int rocker_cmd_set_port_settings_mtu(struct rocker_port *rocker_port,
1398 int mtu)
1400 return rocker_cmd_exec(rocker_port, false,
1401 rocker_cmd_set_port_settings_mtu_prep,
1402 &mtu, NULL, NULL);
1405 int rocker_port_set_learning(struct rocker_port *rocker_port,
1406 bool learning)
1408 return rocker_cmd_exec(rocker_port, false,
1409 rocker_cmd_set_port_learning_prep,
1410 &learning, NULL, NULL);
1413 /**********************
1414 * Worlds manipulation
1415 **********************/
1417 static struct rocker_world_ops *rocker_world_ops[] = {
1418 &rocker_ofdpa_ops,
1421 #define ROCKER_WORLD_OPS_LEN ARRAY_SIZE(rocker_world_ops)
1423 static struct rocker_world_ops *rocker_world_ops_find(u8 mode)
1425 int i;
1427 for (i = 0; i < ROCKER_WORLD_OPS_LEN; i++)
1428 if (rocker_world_ops[i]->mode == mode)
1429 return rocker_world_ops[i];
1430 return NULL;
1433 static int rocker_world_init(struct rocker *rocker, u8 mode)
1435 struct rocker_world_ops *wops;
1436 int err;
1438 wops = rocker_world_ops_find(mode);
1439 if (!wops) {
1440 dev_err(&rocker->pdev->dev, "port mode \"%d\" is not supported\n",
1441 mode);
1442 return -EINVAL;
1444 rocker->wops = wops;
1445 rocker->wpriv = kzalloc(wops->priv_size, GFP_KERNEL);
1446 if (!rocker->wpriv)
1447 return -ENOMEM;
1448 if (!wops->init)
1449 return 0;
1450 err = wops->init(rocker);
1451 if (err)
1452 kfree(rocker->wpriv);
1453 return err;
1456 static void rocker_world_fini(struct rocker *rocker)
1458 struct rocker_world_ops *wops = rocker->wops;
1460 if (!wops || !wops->fini)
1461 return;
1462 wops->fini(rocker);
1463 kfree(rocker->wpriv);
1466 static int rocker_world_check_init(struct rocker_port *rocker_port)
1468 struct rocker *rocker = rocker_port->rocker;
1469 u8 mode;
1470 int err;
1472 err = rocker_cmd_get_port_settings_mode(rocker_port, &mode);
1473 if (err) {
1474 dev_err(&rocker->pdev->dev, "failed to get port mode\n");
1475 return err;
1477 if (rocker->wops) {
1478 if (rocker->wops->mode != mode) {
1479 dev_err(&rocker->pdev->dev, "hardware has ports in different worlds, which is not supported\n");
1480 return -EINVAL;
1482 return 0;
1484 return rocker_world_init(rocker, mode);
1487 static int rocker_world_port_pre_init(struct rocker_port *rocker_port)
1489 struct rocker_world_ops *wops = rocker_port->rocker->wops;
1490 int err;
1492 rocker_port->wpriv = kzalloc(wops->port_priv_size, GFP_KERNEL);
1493 if (!rocker_port->wpriv)
1494 return -ENOMEM;
1495 if (!wops->port_pre_init)
1496 return 0;
1497 err = wops->port_pre_init(rocker_port);
1498 if (err)
1499 kfree(rocker_port->wpriv);
1500 return 0;
1503 static int rocker_world_port_init(struct rocker_port *rocker_port)
1505 struct rocker_world_ops *wops = rocker_port->rocker->wops;
1507 if (!wops->port_init)
1508 return 0;
1509 return wops->port_init(rocker_port);
1512 static void rocker_world_port_fini(struct rocker_port *rocker_port)
1514 struct rocker_world_ops *wops = rocker_port->rocker->wops;
1516 if (!wops->port_fini)
1517 return;
1518 wops->port_fini(rocker_port);
1521 static void rocker_world_port_post_fini(struct rocker_port *rocker_port)
1523 struct rocker_world_ops *wops = rocker_port->rocker->wops;
1525 if (!wops->port_post_fini)
1526 return;
1527 wops->port_post_fini(rocker_port);
1528 kfree(rocker_port->wpriv);
1531 static int rocker_world_port_open(struct rocker_port *rocker_port)
1533 struct rocker_world_ops *wops = rocker_port->rocker->wops;
1535 if (!wops->port_open)
1536 return 0;
1537 return wops->port_open(rocker_port);
1540 static void rocker_world_port_stop(struct rocker_port *rocker_port)
1542 struct rocker_world_ops *wops = rocker_port->rocker->wops;
1544 if (!wops->port_stop)
1545 return;
1546 wops->port_stop(rocker_port);
1549 static int rocker_world_port_attr_stp_state_set(struct rocker_port *rocker_port,
1550 u8 state,
1551 struct switchdev_trans *trans)
1553 struct rocker_world_ops *wops = rocker_port->rocker->wops;
1555 if (!wops->port_attr_stp_state_set)
1556 return -EOPNOTSUPP;
1558 if (switchdev_trans_ph_prepare(trans))
1559 return 0;
1561 return wops->port_attr_stp_state_set(rocker_port, state);
1564 static int
1565 rocker_world_port_attr_bridge_flags_support_get(const struct rocker_port *
1566 rocker_port,
1567 unsigned long *
1568 p_brport_flags_support)
1570 struct rocker_world_ops *wops = rocker_port->rocker->wops;
1572 if (!wops->port_attr_bridge_flags_support_get)
1573 return -EOPNOTSUPP;
1574 return wops->port_attr_bridge_flags_support_get(rocker_port,
1575 p_brport_flags_support);
1578 static int
1579 rocker_world_port_attr_pre_bridge_flags_set(struct rocker_port *rocker_port,
1580 unsigned long brport_flags,
1581 struct switchdev_trans *trans)
1583 struct rocker_world_ops *wops = rocker_port->rocker->wops;
1584 unsigned long brport_flags_s;
1585 int err;
1587 if (!wops->port_attr_bridge_flags_set)
1588 return -EOPNOTSUPP;
1590 err = rocker_world_port_attr_bridge_flags_support_get(rocker_port,
1591 &brport_flags_s);
1592 if (err)
1593 return err;
1595 if (brport_flags & ~brport_flags_s)
1596 return -EINVAL;
1598 return 0;
1601 static int
1602 rocker_world_port_attr_bridge_flags_set(struct rocker_port *rocker_port,
1603 unsigned long brport_flags,
1604 struct switchdev_trans *trans)
1606 struct rocker_world_ops *wops = rocker_port->rocker->wops;
1608 if (!wops->port_attr_bridge_flags_set)
1609 return -EOPNOTSUPP;
1611 if (switchdev_trans_ph_prepare(trans))
1612 return 0;
1614 return wops->port_attr_bridge_flags_set(rocker_port, brport_flags,
1615 trans);
1618 static int
1619 rocker_world_port_attr_bridge_ageing_time_set(struct rocker_port *rocker_port,
1620 u32 ageing_time,
1621 struct switchdev_trans *trans)
1624 struct rocker_world_ops *wops = rocker_port->rocker->wops;
1626 if (!wops->port_attr_bridge_ageing_time_set)
1627 return -EOPNOTSUPP;
1629 if (switchdev_trans_ph_prepare(trans))
1630 return 0;
1632 return wops->port_attr_bridge_ageing_time_set(rocker_port, ageing_time,
1633 trans);
1636 static int
1637 rocker_world_port_obj_vlan_add(struct rocker_port *rocker_port,
1638 const struct switchdev_obj_port_vlan *vlan,
1639 struct switchdev_trans *trans)
1641 struct rocker_world_ops *wops = rocker_port->rocker->wops;
1643 if (!wops->port_obj_vlan_add)
1644 return -EOPNOTSUPP;
1646 if (switchdev_trans_ph_prepare(trans))
1647 return 0;
1649 return wops->port_obj_vlan_add(rocker_port, vlan);
1652 static int
1653 rocker_world_port_obj_vlan_del(struct rocker_port *rocker_port,
1654 const struct switchdev_obj_port_vlan *vlan)
1656 struct rocker_world_ops *wops = rocker_port->rocker->wops;
1658 if (netif_is_bridge_master(vlan->obj.orig_dev))
1659 return -EOPNOTSUPP;
1661 if (!wops->port_obj_vlan_del)
1662 return -EOPNOTSUPP;
1663 return wops->port_obj_vlan_del(rocker_port, vlan);
1666 static int
1667 rocker_world_port_fdb_add(struct rocker_port *rocker_port,
1668 struct switchdev_notifier_fdb_info *info)
1670 struct rocker_world_ops *wops = rocker_port->rocker->wops;
1672 if (!wops->port_obj_fdb_add)
1673 return -EOPNOTSUPP;
1675 return wops->port_obj_fdb_add(rocker_port, info->vid, info->addr);
1678 static int
1679 rocker_world_port_fdb_del(struct rocker_port *rocker_port,
1680 struct switchdev_notifier_fdb_info *info)
1682 struct rocker_world_ops *wops = rocker_port->rocker->wops;
1684 if (!wops->port_obj_fdb_del)
1685 return -EOPNOTSUPP;
1686 return wops->port_obj_fdb_del(rocker_port, info->vid, info->addr);
1689 static int rocker_world_port_master_linked(struct rocker_port *rocker_port,
1690 struct net_device *master)
1692 struct rocker_world_ops *wops = rocker_port->rocker->wops;
1694 if (!wops->port_master_linked)
1695 return -EOPNOTSUPP;
1696 return wops->port_master_linked(rocker_port, master);
1699 static int rocker_world_port_master_unlinked(struct rocker_port *rocker_port,
1700 struct net_device *master)
1702 struct rocker_world_ops *wops = rocker_port->rocker->wops;
1704 if (!wops->port_master_unlinked)
1705 return -EOPNOTSUPP;
1706 return wops->port_master_unlinked(rocker_port, master);
1709 static int rocker_world_port_neigh_update(struct rocker_port *rocker_port,
1710 struct neighbour *n)
1712 struct rocker_world_ops *wops = rocker_port->rocker->wops;
1714 if (!wops->port_neigh_update)
1715 return -EOPNOTSUPP;
1716 return wops->port_neigh_update(rocker_port, n);
1719 static int rocker_world_port_neigh_destroy(struct rocker_port *rocker_port,
1720 struct neighbour *n)
1722 struct rocker_world_ops *wops = rocker_port->rocker->wops;
1724 if (!wops->port_neigh_destroy)
1725 return -EOPNOTSUPP;
1726 return wops->port_neigh_destroy(rocker_port, n);
1729 static int rocker_world_port_ev_mac_vlan_seen(struct rocker_port *rocker_port,
1730 const unsigned char *addr,
1731 __be16 vlan_id)
1733 struct rocker_world_ops *wops = rocker_port->rocker->wops;
1735 if (!wops->port_ev_mac_vlan_seen)
1736 return -EOPNOTSUPP;
1737 return wops->port_ev_mac_vlan_seen(rocker_port, addr, vlan_id);
1740 static int rocker_world_fib4_add(struct rocker *rocker,
1741 const struct fib_entry_notifier_info *fen_info)
1743 struct rocker_world_ops *wops = rocker->wops;
1745 if (!wops->fib4_add)
1746 return 0;
1747 return wops->fib4_add(rocker, fen_info);
1750 static int rocker_world_fib4_del(struct rocker *rocker,
1751 const struct fib_entry_notifier_info *fen_info)
1753 struct rocker_world_ops *wops = rocker->wops;
1755 if (!wops->fib4_del)
1756 return 0;
1757 return wops->fib4_del(rocker, fen_info);
1760 static void rocker_world_fib4_abort(struct rocker *rocker)
1762 struct rocker_world_ops *wops = rocker->wops;
1764 if (wops->fib4_abort)
1765 wops->fib4_abort(rocker);
1768 /*****************
1769 * Net device ops
1770 *****************/
1772 static int rocker_port_open(struct net_device *dev)
1774 struct rocker_port *rocker_port = netdev_priv(dev);
1775 int err;
1777 err = rocker_port_dma_rings_init(rocker_port);
1778 if (err)
1779 return err;
1781 err = request_irq(rocker_msix_tx_vector(rocker_port),
1782 rocker_tx_irq_handler, 0,
1783 rocker_driver_name, rocker_port);
1784 if (err) {
1785 netdev_err(rocker_port->dev, "cannot assign tx irq\n");
1786 goto err_request_tx_irq;
1789 err = request_irq(rocker_msix_rx_vector(rocker_port),
1790 rocker_rx_irq_handler, 0,
1791 rocker_driver_name, rocker_port);
1792 if (err) {
1793 netdev_err(rocker_port->dev, "cannot assign rx irq\n");
1794 goto err_request_rx_irq;
1797 err = rocker_world_port_open(rocker_port);
1798 if (err) {
1799 netdev_err(rocker_port->dev, "cannot open port in world\n");
1800 goto err_world_port_open;
1803 napi_enable(&rocker_port->napi_tx);
1804 napi_enable(&rocker_port->napi_rx);
1805 if (!dev->proto_down)
1806 rocker_port_set_enable(rocker_port, true);
1807 netif_start_queue(dev);
1808 return 0;
1810 err_world_port_open:
1811 free_irq(rocker_msix_rx_vector(rocker_port), rocker_port);
1812 err_request_rx_irq:
1813 free_irq(rocker_msix_tx_vector(rocker_port), rocker_port);
1814 err_request_tx_irq:
1815 rocker_port_dma_rings_fini(rocker_port);
1816 return err;
1819 static int rocker_port_stop(struct net_device *dev)
1821 struct rocker_port *rocker_port = netdev_priv(dev);
1823 netif_stop_queue(dev);
1824 rocker_port_set_enable(rocker_port, false);
1825 napi_disable(&rocker_port->napi_rx);
1826 napi_disable(&rocker_port->napi_tx);
1827 rocker_world_port_stop(rocker_port);
1828 free_irq(rocker_msix_rx_vector(rocker_port), rocker_port);
1829 free_irq(rocker_msix_tx_vector(rocker_port), rocker_port);
1830 rocker_port_dma_rings_fini(rocker_port);
1832 return 0;
1835 static void rocker_tx_desc_frags_unmap(const struct rocker_port *rocker_port,
1836 const struct rocker_desc_info *desc_info)
1838 const struct rocker *rocker = rocker_port->rocker;
1839 struct pci_dev *pdev = rocker->pdev;
1840 const struct rocker_tlv *attrs[ROCKER_TLV_TX_MAX + 1];
1841 struct rocker_tlv *attr;
1842 int rem;
1844 rocker_tlv_parse_desc(attrs, ROCKER_TLV_TX_MAX, desc_info);
1845 if (!attrs[ROCKER_TLV_TX_FRAGS])
1846 return;
1847 rocker_tlv_for_each_nested(attr, attrs[ROCKER_TLV_TX_FRAGS], rem) {
1848 const struct rocker_tlv *frag_attrs[ROCKER_TLV_TX_FRAG_ATTR_MAX + 1];
1849 dma_addr_t dma_handle;
1850 size_t len;
1852 if (rocker_tlv_type(attr) != ROCKER_TLV_TX_FRAG)
1853 continue;
1854 rocker_tlv_parse_nested(frag_attrs, ROCKER_TLV_TX_FRAG_ATTR_MAX,
1855 attr);
1856 if (!frag_attrs[ROCKER_TLV_TX_FRAG_ATTR_ADDR] ||
1857 !frag_attrs[ROCKER_TLV_TX_FRAG_ATTR_LEN])
1858 continue;
1859 dma_handle = rocker_tlv_get_u64(frag_attrs[ROCKER_TLV_TX_FRAG_ATTR_ADDR]);
1860 len = rocker_tlv_get_u16(frag_attrs[ROCKER_TLV_TX_FRAG_ATTR_LEN]);
1861 pci_unmap_single(pdev, dma_handle, len, DMA_TO_DEVICE);
1865 static int rocker_tx_desc_frag_map_put(const struct rocker_port *rocker_port,
1866 struct rocker_desc_info *desc_info,
1867 char *buf, size_t buf_len)
1869 const struct rocker *rocker = rocker_port->rocker;
1870 struct pci_dev *pdev = rocker->pdev;
1871 dma_addr_t dma_handle;
1872 struct rocker_tlv *frag;
1874 dma_handle = pci_map_single(pdev, buf, buf_len, DMA_TO_DEVICE);
1875 if (unlikely(pci_dma_mapping_error(pdev, dma_handle))) {
1876 if (net_ratelimit())
1877 netdev_err(rocker_port->dev, "failed to dma map tx frag\n");
1878 return -EIO;
1880 frag = rocker_tlv_nest_start(desc_info, ROCKER_TLV_TX_FRAG);
1881 if (!frag)
1882 goto unmap_frag;
1883 if (rocker_tlv_put_u64(desc_info, ROCKER_TLV_TX_FRAG_ATTR_ADDR,
1884 dma_handle))
1885 goto nest_cancel;
1886 if (rocker_tlv_put_u16(desc_info, ROCKER_TLV_TX_FRAG_ATTR_LEN,
1887 buf_len))
1888 goto nest_cancel;
1889 rocker_tlv_nest_end(desc_info, frag);
1890 return 0;
1892 nest_cancel:
1893 rocker_tlv_nest_cancel(desc_info, frag);
1894 unmap_frag:
1895 pci_unmap_single(pdev, dma_handle, buf_len, DMA_TO_DEVICE);
1896 return -EMSGSIZE;
1899 static netdev_tx_t rocker_port_xmit(struct sk_buff *skb, struct net_device *dev)
1901 struct rocker_port *rocker_port = netdev_priv(dev);
1902 struct rocker *rocker = rocker_port->rocker;
1903 struct rocker_desc_info *desc_info;
1904 struct rocker_tlv *frags;
1905 int i;
1906 int err;
1908 desc_info = rocker_desc_head_get(&rocker_port->tx_ring);
1909 if (unlikely(!desc_info)) {
1910 if (net_ratelimit())
1911 netdev_err(dev, "tx ring full when queue awake\n");
1912 return NETDEV_TX_BUSY;
1915 rocker_desc_cookie_ptr_set(desc_info, skb);
1917 frags = rocker_tlv_nest_start(desc_info, ROCKER_TLV_TX_FRAGS);
1918 if (!frags)
1919 goto out;
1920 err = rocker_tx_desc_frag_map_put(rocker_port, desc_info,
1921 skb->data, skb_headlen(skb));
1922 if (err)
1923 goto nest_cancel;
1924 if (skb_shinfo(skb)->nr_frags > ROCKER_TX_FRAGS_MAX) {
1925 err = skb_linearize(skb);
1926 if (err)
1927 goto unmap_frags;
1930 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1931 const skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
1933 err = rocker_tx_desc_frag_map_put(rocker_port, desc_info,
1934 skb_frag_address(frag),
1935 skb_frag_size(frag));
1936 if (err)
1937 goto unmap_frags;
1939 rocker_tlv_nest_end(desc_info, frags);
1941 rocker_desc_gen_clear(desc_info);
1942 rocker_desc_head_set(rocker, &rocker_port->tx_ring, desc_info);
1944 desc_info = rocker_desc_head_get(&rocker_port->tx_ring);
1945 if (!desc_info)
1946 netif_stop_queue(dev);
1948 return NETDEV_TX_OK;
1950 unmap_frags:
1951 rocker_tx_desc_frags_unmap(rocker_port, desc_info);
1952 nest_cancel:
1953 rocker_tlv_nest_cancel(desc_info, frags);
1954 out:
1955 dev_kfree_skb(skb);
1956 dev->stats.tx_dropped++;
1958 return NETDEV_TX_OK;
1961 static int rocker_port_set_mac_address(struct net_device *dev, void *p)
1963 struct sockaddr *addr = p;
1964 struct rocker_port *rocker_port = netdev_priv(dev);
1965 int err;
1967 if (!is_valid_ether_addr(addr->sa_data))
1968 return -EADDRNOTAVAIL;
1970 err = rocker_cmd_set_port_settings_macaddr(rocker_port, addr->sa_data);
1971 if (err)
1972 return err;
1973 memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
1974 return 0;
1977 static int rocker_port_change_mtu(struct net_device *dev, int new_mtu)
1979 struct rocker_port *rocker_port = netdev_priv(dev);
1980 int running = netif_running(dev);
1981 int err;
1983 if (running)
1984 rocker_port_stop(dev);
1986 netdev_info(dev, "MTU change from %d to %d\n", dev->mtu, new_mtu);
1987 dev->mtu = new_mtu;
1989 err = rocker_cmd_set_port_settings_mtu(rocker_port, new_mtu);
1990 if (err)
1991 return err;
1993 if (running)
1994 err = rocker_port_open(dev);
1996 return err;
1999 static int rocker_port_get_phys_port_name(struct net_device *dev,
2000 char *buf, size_t len)
2002 struct rocker_port *rocker_port = netdev_priv(dev);
2003 struct port_name name = { .buf = buf, .len = len };
2004 int err;
2006 err = rocker_cmd_exec(rocker_port, false,
2007 rocker_cmd_get_port_settings_prep, NULL,
2008 rocker_cmd_get_port_settings_phys_name_proc,
2009 &name);
2011 return err ? -EOPNOTSUPP : 0;
2014 static int rocker_port_change_proto_down(struct net_device *dev,
2015 bool proto_down)
2017 struct rocker_port *rocker_port = netdev_priv(dev);
2019 if (rocker_port->dev->flags & IFF_UP)
2020 rocker_port_set_enable(rocker_port, !proto_down);
2021 rocker_port->dev->proto_down = proto_down;
2022 return 0;
2025 static void rocker_port_neigh_destroy(struct net_device *dev,
2026 struct neighbour *n)
2028 struct rocker_port *rocker_port = netdev_priv(n->dev);
2029 int err;
2031 err = rocker_world_port_neigh_destroy(rocker_port, n);
2032 if (err)
2033 netdev_warn(rocker_port->dev, "failed to handle neigh destroy (err %d)\n",
2034 err);
2037 static int rocker_port_get_port_parent_id(struct net_device *dev,
2038 struct netdev_phys_item_id *ppid)
2040 const struct rocker_port *rocker_port = netdev_priv(dev);
2041 const struct rocker *rocker = rocker_port->rocker;
2043 ppid->id_len = sizeof(rocker->hw.id);
2044 memcpy(&ppid->id, &rocker->hw.id, ppid->id_len);
2046 return 0;
2049 static const struct net_device_ops rocker_port_netdev_ops = {
2050 .ndo_open = rocker_port_open,
2051 .ndo_stop = rocker_port_stop,
2052 .ndo_start_xmit = rocker_port_xmit,
2053 .ndo_set_mac_address = rocker_port_set_mac_address,
2054 .ndo_change_mtu = rocker_port_change_mtu,
2055 .ndo_get_phys_port_name = rocker_port_get_phys_port_name,
2056 .ndo_change_proto_down = rocker_port_change_proto_down,
2057 .ndo_neigh_destroy = rocker_port_neigh_destroy,
2058 .ndo_get_port_parent_id = rocker_port_get_port_parent_id,
2061 /********************
2062 * swdev interface
2063 ********************/
2065 static int rocker_port_attr_set(struct net_device *dev,
2066 const struct switchdev_attr *attr,
2067 struct switchdev_trans *trans)
2069 struct rocker_port *rocker_port = netdev_priv(dev);
2070 int err = 0;
2072 switch (attr->id) {
2073 case SWITCHDEV_ATTR_ID_PORT_STP_STATE:
2074 err = rocker_world_port_attr_stp_state_set(rocker_port,
2075 attr->u.stp_state,
2076 trans);
2077 break;
2078 case SWITCHDEV_ATTR_ID_PORT_PRE_BRIDGE_FLAGS:
2079 err = rocker_world_port_attr_pre_bridge_flags_set(rocker_port,
2080 attr->u.brport_flags,
2081 trans);
2082 break;
2083 case SWITCHDEV_ATTR_ID_PORT_BRIDGE_FLAGS:
2084 err = rocker_world_port_attr_bridge_flags_set(rocker_port,
2085 attr->u.brport_flags,
2086 trans);
2087 break;
2088 case SWITCHDEV_ATTR_ID_BRIDGE_AGEING_TIME:
2089 err = rocker_world_port_attr_bridge_ageing_time_set(rocker_port,
2090 attr->u.ageing_time,
2091 trans);
2092 break;
2093 default:
2094 err = -EOPNOTSUPP;
2095 break;
2098 return err;
2101 static int rocker_port_obj_add(struct net_device *dev,
2102 const struct switchdev_obj *obj,
2103 struct switchdev_trans *trans)
2105 struct rocker_port *rocker_port = netdev_priv(dev);
2106 int err = 0;
2108 switch (obj->id) {
2109 case SWITCHDEV_OBJ_ID_PORT_VLAN:
2110 err = rocker_world_port_obj_vlan_add(rocker_port,
2111 SWITCHDEV_OBJ_PORT_VLAN(obj),
2112 trans);
2113 break;
2114 default:
2115 err = -EOPNOTSUPP;
2116 break;
2119 return err;
2122 static int rocker_port_obj_del(struct net_device *dev,
2123 const struct switchdev_obj *obj)
2125 struct rocker_port *rocker_port = netdev_priv(dev);
2126 int err = 0;
2128 switch (obj->id) {
2129 case SWITCHDEV_OBJ_ID_PORT_VLAN:
2130 err = rocker_world_port_obj_vlan_del(rocker_port,
2131 SWITCHDEV_OBJ_PORT_VLAN(obj));
2132 break;
2133 default:
2134 err = -EOPNOTSUPP;
2135 break;
2138 return err;
2141 struct rocker_fib_event_work {
2142 struct work_struct work;
2143 union {
2144 struct fib_entry_notifier_info fen_info;
2145 struct fib_rule_notifier_info fr_info;
2147 struct rocker *rocker;
2148 unsigned long event;
2151 static void rocker_router_fib_event_work(struct work_struct *work)
2153 struct rocker_fib_event_work *fib_work =
2154 container_of(work, struct rocker_fib_event_work, work);
2155 struct rocker *rocker = fib_work->rocker;
2156 struct fib_rule *rule;
2157 int err;
2159 /* Protect internal structures from changes */
2160 rtnl_lock();
2161 switch (fib_work->event) {
2162 case FIB_EVENT_ENTRY_REPLACE:
2163 err = rocker_world_fib4_add(rocker, &fib_work->fen_info);
2164 if (err)
2165 rocker_world_fib4_abort(rocker);
2166 fib_info_put(fib_work->fen_info.fi);
2167 break;
2168 case FIB_EVENT_ENTRY_DEL:
2169 rocker_world_fib4_del(rocker, &fib_work->fen_info);
2170 fib_info_put(fib_work->fen_info.fi);
2171 break;
2172 case FIB_EVENT_RULE_ADD: /* fall through */
2173 case FIB_EVENT_RULE_DEL:
2174 rule = fib_work->fr_info.rule;
2175 if (!fib4_rule_default(rule))
2176 rocker_world_fib4_abort(rocker);
2177 fib_rule_put(rule);
2178 break;
2180 rtnl_unlock();
2181 kfree(fib_work);
2184 /* Called with rcu_read_lock() */
2185 static int rocker_router_fib_event(struct notifier_block *nb,
2186 unsigned long event, void *ptr)
2188 struct rocker *rocker = container_of(nb, struct rocker, fib_nb);
2189 struct rocker_fib_event_work *fib_work;
2190 struct fib_notifier_info *info = ptr;
2192 if (info->family != AF_INET)
2193 return NOTIFY_DONE;
2195 fib_work = kzalloc(sizeof(*fib_work), GFP_ATOMIC);
2196 if (WARN_ON(!fib_work))
2197 return NOTIFY_BAD;
2199 INIT_WORK(&fib_work->work, rocker_router_fib_event_work);
2200 fib_work->rocker = rocker;
2201 fib_work->event = event;
2203 switch (event) {
2204 case FIB_EVENT_ENTRY_REPLACE: /* fall through */
2205 case FIB_EVENT_ENTRY_DEL:
2206 if (info->family == AF_INET) {
2207 struct fib_entry_notifier_info *fen_info = ptr;
2209 if (fen_info->fi->fib_nh_is_v6) {
2210 NL_SET_ERR_MSG_MOD(info->extack, "IPv6 gateway with IPv4 route is not supported");
2211 kfree(fib_work);
2212 return notifier_from_errno(-EINVAL);
2214 if (fen_info->fi->nh) {
2215 NL_SET_ERR_MSG_MOD(info->extack, "IPv4 route with nexthop objects is not supported");
2216 kfree(fib_work);
2217 return notifier_from_errno(-EINVAL);
2221 memcpy(&fib_work->fen_info, ptr, sizeof(fib_work->fen_info));
2222 /* Take referece on fib_info to prevent it from being
2223 * freed while work is queued. Release it afterwards.
2225 fib_info_hold(fib_work->fen_info.fi);
2226 break;
2227 case FIB_EVENT_RULE_ADD: /* fall through */
2228 case FIB_EVENT_RULE_DEL:
2229 memcpy(&fib_work->fr_info, ptr, sizeof(fib_work->fr_info));
2230 fib_rule_get(fib_work->fr_info.rule);
2231 break;
2234 queue_work(rocker->rocker_owq, &fib_work->work);
2236 return NOTIFY_DONE;
2239 /********************
2240 * ethtool interface
2241 ********************/
2243 static int
2244 rocker_port_get_link_ksettings(struct net_device *dev,
2245 struct ethtool_link_ksettings *ecmd)
2247 struct rocker_port *rocker_port = netdev_priv(dev);
2249 return rocker_cmd_get_port_settings_ethtool(rocker_port, ecmd);
2252 static int
2253 rocker_port_set_link_ksettings(struct net_device *dev,
2254 const struct ethtool_link_ksettings *ecmd)
2256 struct rocker_port *rocker_port = netdev_priv(dev);
2258 return rocker_cmd_set_port_settings_ethtool(rocker_port, ecmd);
2261 static void rocker_port_get_drvinfo(struct net_device *dev,
2262 struct ethtool_drvinfo *drvinfo)
2264 strlcpy(drvinfo->driver, rocker_driver_name, sizeof(drvinfo->driver));
2265 strlcpy(drvinfo->version, UTS_RELEASE, sizeof(drvinfo->version));
2268 static struct rocker_port_stats {
2269 char str[ETH_GSTRING_LEN];
2270 int type;
2271 } rocker_port_stats[] = {
2272 { "rx_packets", ROCKER_TLV_CMD_PORT_STATS_RX_PKTS, },
2273 { "rx_bytes", ROCKER_TLV_CMD_PORT_STATS_RX_BYTES, },
2274 { "rx_dropped", ROCKER_TLV_CMD_PORT_STATS_RX_DROPPED, },
2275 { "rx_errors", ROCKER_TLV_CMD_PORT_STATS_RX_ERRORS, },
2277 { "tx_packets", ROCKER_TLV_CMD_PORT_STATS_TX_PKTS, },
2278 { "tx_bytes", ROCKER_TLV_CMD_PORT_STATS_TX_BYTES, },
2279 { "tx_dropped", ROCKER_TLV_CMD_PORT_STATS_TX_DROPPED, },
2280 { "tx_errors", ROCKER_TLV_CMD_PORT_STATS_TX_ERRORS, },
2283 #define ROCKER_PORT_STATS_LEN ARRAY_SIZE(rocker_port_stats)
2285 static void rocker_port_get_strings(struct net_device *netdev, u32 stringset,
2286 u8 *data)
2288 u8 *p = data;
2289 int i;
2291 switch (stringset) {
2292 case ETH_SS_STATS:
2293 for (i = 0; i < ARRAY_SIZE(rocker_port_stats); i++) {
2294 memcpy(p, rocker_port_stats[i].str, ETH_GSTRING_LEN);
2295 p += ETH_GSTRING_LEN;
2297 break;
2301 static int
2302 rocker_cmd_get_port_stats_prep(const struct rocker_port *rocker_port,
2303 struct rocker_desc_info *desc_info,
2304 void *priv)
2306 struct rocker_tlv *cmd_stats;
2308 if (rocker_tlv_put_u16(desc_info, ROCKER_TLV_CMD_TYPE,
2309 ROCKER_TLV_CMD_TYPE_GET_PORT_STATS))
2310 return -EMSGSIZE;
2312 cmd_stats = rocker_tlv_nest_start(desc_info, ROCKER_TLV_CMD_INFO);
2313 if (!cmd_stats)
2314 return -EMSGSIZE;
2316 if (rocker_tlv_put_u32(desc_info, ROCKER_TLV_CMD_PORT_STATS_PPORT,
2317 rocker_port->pport))
2318 return -EMSGSIZE;
2320 rocker_tlv_nest_end(desc_info, cmd_stats);
2322 return 0;
2325 static int
2326 rocker_cmd_get_port_stats_ethtool_proc(const struct rocker_port *rocker_port,
2327 const struct rocker_desc_info *desc_info,
2328 void *priv)
2330 const struct rocker_tlv *attrs[ROCKER_TLV_CMD_MAX + 1];
2331 const struct rocker_tlv *stats_attrs[ROCKER_TLV_CMD_PORT_STATS_MAX + 1];
2332 const struct rocker_tlv *pattr;
2333 u32 pport;
2334 u64 *data = priv;
2335 int i;
2337 rocker_tlv_parse_desc(attrs, ROCKER_TLV_CMD_MAX, desc_info);
2339 if (!attrs[ROCKER_TLV_CMD_INFO])
2340 return -EIO;
2342 rocker_tlv_parse_nested(stats_attrs, ROCKER_TLV_CMD_PORT_STATS_MAX,
2343 attrs[ROCKER_TLV_CMD_INFO]);
2345 if (!stats_attrs[ROCKER_TLV_CMD_PORT_STATS_PPORT])
2346 return -EIO;
2348 pport = rocker_tlv_get_u32(stats_attrs[ROCKER_TLV_CMD_PORT_STATS_PPORT]);
2349 if (pport != rocker_port->pport)
2350 return -EIO;
2352 for (i = 0; i < ARRAY_SIZE(rocker_port_stats); i++) {
2353 pattr = stats_attrs[rocker_port_stats[i].type];
2354 if (!pattr)
2355 continue;
2357 data[i] = rocker_tlv_get_u64(pattr);
2360 return 0;
2363 static int rocker_cmd_get_port_stats_ethtool(struct rocker_port *rocker_port,
2364 void *priv)
2366 return rocker_cmd_exec(rocker_port, false,
2367 rocker_cmd_get_port_stats_prep, NULL,
2368 rocker_cmd_get_port_stats_ethtool_proc,
2369 priv);
2372 static void rocker_port_get_stats(struct net_device *dev,
2373 struct ethtool_stats *stats, u64 *data)
2375 struct rocker_port *rocker_port = netdev_priv(dev);
2377 if (rocker_cmd_get_port_stats_ethtool(rocker_port, data) != 0) {
2378 int i;
2380 for (i = 0; i < ARRAY_SIZE(rocker_port_stats); ++i)
2381 data[i] = 0;
2385 static int rocker_port_get_sset_count(struct net_device *netdev, int sset)
2387 switch (sset) {
2388 case ETH_SS_STATS:
2389 return ROCKER_PORT_STATS_LEN;
2390 default:
2391 return -EOPNOTSUPP;
2395 static const struct ethtool_ops rocker_port_ethtool_ops = {
2396 .get_drvinfo = rocker_port_get_drvinfo,
2397 .get_link = ethtool_op_get_link,
2398 .get_strings = rocker_port_get_strings,
2399 .get_ethtool_stats = rocker_port_get_stats,
2400 .get_sset_count = rocker_port_get_sset_count,
2401 .get_link_ksettings = rocker_port_get_link_ksettings,
2402 .set_link_ksettings = rocker_port_set_link_ksettings,
2405 /*****************
2406 * NAPI interface
2407 *****************/
2409 static struct rocker_port *rocker_port_napi_tx_get(struct napi_struct *napi)
2411 return container_of(napi, struct rocker_port, napi_tx);
2414 static int rocker_port_poll_tx(struct napi_struct *napi, int budget)
2416 struct rocker_port *rocker_port = rocker_port_napi_tx_get(napi);
2417 const struct rocker *rocker = rocker_port->rocker;
2418 const struct rocker_desc_info *desc_info;
2419 u32 credits = 0;
2420 int err;
2422 /* Cleanup tx descriptors */
2423 while ((desc_info = rocker_desc_tail_get(&rocker_port->tx_ring))) {
2424 struct sk_buff *skb;
2426 err = rocker_desc_err(desc_info);
2427 if (err && net_ratelimit())
2428 netdev_err(rocker_port->dev, "tx desc received with err %d\n",
2429 err);
2430 rocker_tx_desc_frags_unmap(rocker_port, desc_info);
2432 skb = rocker_desc_cookie_ptr_get(desc_info);
2433 if (err == 0) {
2434 rocker_port->dev->stats.tx_packets++;
2435 rocker_port->dev->stats.tx_bytes += skb->len;
2436 } else {
2437 rocker_port->dev->stats.tx_errors++;
2440 dev_kfree_skb_any(skb);
2441 credits++;
2444 if (credits && netif_queue_stopped(rocker_port->dev))
2445 netif_wake_queue(rocker_port->dev);
2447 napi_complete(napi);
2448 rocker_dma_ring_credits_set(rocker, &rocker_port->tx_ring, credits);
2450 return 0;
2453 static int rocker_port_rx_proc(const struct rocker *rocker,
2454 const struct rocker_port *rocker_port,
2455 struct rocker_desc_info *desc_info)
2457 const struct rocker_tlv *attrs[ROCKER_TLV_RX_MAX + 1];
2458 struct sk_buff *skb = rocker_desc_cookie_ptr_get(desc_info);
2459 size_t rx_len;
2460 u16 rx_flags = 0;
2462 if (!skb)
2463 return -ENOENT;
2465 rocker_tlv_parse_desc(attrs, ROCKER_TLV_RX_MAX, desc_info);
2466 if (!attrs[ROCKER_TLV_RX_FRAG_LEN])
2467 return -EINVAL;
2468 if (attrs[ROCKER_TLV_RX_FLAGS])
2469 rx_flags = rocker_tlv_get_u16(attrs[ROCKER_TLV_RX_FLAGS]);
2471 rocker_dma_rx_ring_skb_unmap(rocker, attrs);
2473 rx_len = rocker_tlv_get_u16(attrs[ROCKER_TLV_RX_FRAG_LEN]);
2474 skb_put(skb, rx_len);
2475 skb->protocol = eth_type_trans(skb, rocker_port->dev);
2477 if (rx_flags & ROCKER_RX_FLAGS_FWD_OFFLOAD)
2478 skb->offload_fwd_mark = 1;
2480 rocker_port->dev->stats.rx_packets++;
2481 rocker_port->dev->stats.rx_bytes += skb->len;
2483 netif_receive_skb(skb);
2485 return rocker_dma_rx_ring_skb_alloc(rocker_port, desc_info);
2488 static struct rocker_port *rocker_port_napi_rx_get(struct napi_struct *napi)
2490 return container_of(napi, struct rocker_port, napi_rx);
2493 static int rocker_port_poll_rx(struct napi_struct *napi, int budget)
2495 struct rocker_port *rocker_port = rocker_port_napi_rx_get(napi);
2496 const struct rocker *rocker = rocker_port->rocker;
2497 struct rocker_desc_info *desc_info;
2498 u32 credits = 0;
2499 int err;
2501 /* Process rx descriptors */
2502 while (credits < budget &&
2503 (desc_info = rocker_desc_tail_get(&rocker_port->rx_ring))) {
2504 err = rocker_desc_err(desc_info);
2505 if (err) {
2506 if (net_ratelimit())
2507 netdev_err(rocker_port->dev, "rx desc received with err %d\n",
2508 err);
2509 } else {
2510 err = rocker_port_rx_proc(rocker, rocker_port,
2511 desc_info);
2512 if (err && net_ratelimit())
2513 netdev_err(rocker_port->dev, "rx processing failed with err %d\n",
2514 err);
2516 if (err)
2517 rocker_port->dev->stats.rx_errors++;
2519 rocker_desc_gen_clear(desc_info);
2520 rocker_desc_head_set(rocker, &rocker_port->rx_ring, desc_info);
2521 credits++;
2524 if (credits < budget)
2525 napi_complete_done(napi, credits);
2527 rocker_dma_ring_credits_set(rocker, &rocker_port->rx_ring, credits);
2529 return credits;
2532 /*****************
2533 * PCI driver ops
2534 *****************/
2536 static void rocker_carrier_init(const struct rocker_port *rocker_port)
2538 const struct rocker *rocker = rocker_port->rocker;
2539 u64 link_status = rocker_read64(rocker, PORT_PHYS_LINK_STATUS);
2540 bool link_up;
2542 link_up = link_status & (1 << rocker_port->pport);
2543 if (link_up)
2544 netif_carrier_on(rocker_port->dev);
2545 else
2546 netif_carrier_off(rocker_port->dev);
2549 static void rocker_remove_ports(struct rocker *rocker)
2551 struct rocker_port *rocker_port;
2552 int i;
2554 for (i = 0; i < rocker->port_count; i++) {
2555 rocker_port = rocker->ports[i];
2556 if (!rocker_port)
2557 continue;
2558 rocker_world_port_fini(rocker_port);
2559 unregister_netdev(rocker_port->dev);
2560 rocker_world_port_post_fini(rocker_port);
2561 free_netdev(rocker_port->dev);
2563 rocker_world_fini(rocker);
2564 kfree(rocker->ports);
2567 static void rocker_port_dev_addr_init(struct rocker_port *rocker_port)
2569 const struct rocker *rocker = rocker_port->rocker;
2570 const struct pci_dev *pdev = rocker->pdev;
2571 int err;
2573 err = rocker_cmd_get_port_settings_macaddr(rocker_port,
2574 rocker_port->dev->dev_addr);
2575 if (err) {
2576 dev_warn(&pdev->dev, "failed to get mac address, using random\n");
2577 eth_hw_addr_random(rocker_port->dev);
2581 #define ROCKER_PORT_MIN_MTU ETH_MIN_MTU
2582 #define ROCKER_PORT_MAX_MTU 9000
2583 static int rocker_probe_port(struct rocker *rocker, unsigned int port_number)
2585 struct pci_dev *pdev = rocker->pdev;
2586 struct rocker_port *rocker_port;
2587 struct net_device *dev;
2588 int err;
2590 dev = alloc_etherdev(sizeof(struct rocker_port));
2591 if (!dev)
2592 return -ENOMEM;
2593 SET_NETDEV_DEV(dev, &pdev->dev);
2594 rocker_port = netdev_priv(dev);
2595 rocker_port->dev = dev;
2596 rocker_port->rocker = rocker;
2597 rocker_port->port_number = port_number;
2598 rocker_port->pport = port_number + 1;
2600 err = rocker_world_check_init(rocker_port);
2601 if (err) {
2602 dev_err(&pdev->dev, "world init failed\n");
2603 goto err_world_check_init;
2606 rocker_port_dev_addr_init(rocker_port);
2607 dev->netdev_ops = &rocker_port_netdev_ops;
2608 dev->ethtool_ops = &rocker_port_ethtool_ops;
2609 netif_tx_napi_add(dev, &rocker_port->napi_tx, rocker_port_poll_tx,
2610 NAPI_POLL_WEIGHT);
2611 netif_napi_add(dev, &rocker_port->napi_rx, rocker_port_poll_rx,
2612 NAPI_POLL_WEIGHT);
2613 rocker_carrier_init(rocker_port);
2615 dev->features |= NETIF_F_NETNS_LOCAL | NETIF_F_SG;
2617 /* MTU range: 68 - 9000 */
2618 dev->min_mtu = ROCKER_PORT_MIN_MTU;
2619 dev->max_mtu = ROCKER_PORT_MAX_MTU;
2621 err = rocker_world_port_pre_init(rocker_port);
2622 if (err) {
2623 dev_err(&pdev->dev, "port world pre-init failed\n");
2624 goto err_world_port_pre_init;
2626 err = register_netdev(dev);
2627 if (err) {
2628 dev_err(&pdev->dev, "register_netdev failed\n");
2629 goto err_register_netdev;
2631 rocker->ports[port_number] = rocker_port;
2633 err = rocker_world_port_init(rocker_port);
2634 if (err) {
2635 dev_err(&pdev->dev, "port world init failed\n");
2636 goto err_world_port_init;
2639 return 0;
2641 err_world_port_init:
2642 rocker->ports[port_number] = NULL;
2643 unregister_netdev(dev);
2644 err_register_netdev:
2645 rocker_world_port_post_fini(rocker_port);
2646 err_world_port_pre_init:
2647 err_world_check_init:
2648 free_netdev(dev);
2649 return err;
2652 static int rocker_probe_ports(struct rocker *rocker)
2654 int i;
2655 size_t alloc_size;
2656 int err;
2658 alloc_size = sizeof(struct rocker_port *) * rocker->port_count;
2659 rocker->ports = kzalloc(alloc_size, GFP_KERNEL);
2660 if (!rocker->ports)
2661 return -ENOMEM;
2662 for (i = 0; i < rocker->port_count; i++) {
2663 err = rocker_probe_port(rocker, i);
2664 if (err)
2665 goto remove_ports;
2667 return 0;
2669 remove_ports:
2670 rocker_remove_ports(rocker);
2671 return err;
2674 static int rocker_msix_init(struct rocker *rocker)
2676 struct pci_dev *pdev = rocker->pdev;
2677 int msix_entries;
2678 int i;
2679 int err;
2681 msix_entries = pci_msix_vec_count(pdev);
2682 if (msix_entries < 0)
2683 return msix_entries;
2685 if (msix_entries != ROCKER_MSIX_VEC_COUNT(rocker->port_count))
2686 return -EINVAL;
2688 rocker->msix_entries = kmalloc_array(msix_entries,
2689 sizeof(struct msix_entry),
2690 GFP_KERNEL);
2691 if (!rocker->msix_entries)
2692 return -ENOMEM;
2694 for (i = 0; i < msix_entries; i++)
2695 rocker->msix_entries[i].entry = i;
2697 err = pci_enable_msix_exact(pdev, rocker->msix_entries, msix_entries);
2698 if (err < 0)
2699 goto err_enable_msix;
2701 return 0;
2703 err_enable_msix:
2704 kfree(rocker->msix_entries);
2705 return err;
2708 static void rocker_msix_fini(const struct rocker *rocker)
2710 pci_disable_msix(rocker->pdev);
2711 kfree(rocker->msix_entries);
2714 static bool rocker_port_dev_check(const struct net_device *dev)
2716 return dev->netdev_ops == &rocker_port_netdev_ops;
2719 static int
2720 rocker_switchdev_port_attr_set_event(struct net_device *netdev,
2721 struct switchdev_notifier_port_attr_info *port_attr_info)
2723 int err;
2725 err = rocker_port_attr_set(netdev, port_attr_info->attr,
2726 port_attr_info->trans);
2728 port_attr_info->handled = true;
2729 return notifier_from_errno(err);
2732 struct rocker_switchdev_event_work {
2733 struct work_struct work;
2734 struct switchdev_notifier_fdb_info fdb_info;
2735 struct rocker_port *rocker_port;
2736 unsigned long event;
2739 static void
2740 rocker_fdb_offload_notify(struct rocker_port *rocker_port,
2741 struct switchdev_notifier_fdb_info *recv_info)
2743 struct switchdev_notifier_fdb_info info;
2745 info.addr = recv_info->addr;
2746 info.vid = recv_info->vid;
2747 info.offloaded = true;
2748 call_switchdev_notifiers(SWITCHDEV_FDB_OFFLOADED,
2749 rocker_port->dev, &info.info, NULL);
2752 static void rocker_switchdev_event_work(struct work_struct *work)
2754 struct rocker_switchdev_event_work *switchdev_work =
2755 container_of(work, struct rocker_switchdev_event_work, work);
2756 struct rocker_port *rocker_port = switchdev_work->rocker_port;
2757 struct switchdev_notifier_fdb_info *fdb_info;
2758 int err;
2760 rtnl_lock();
2761 switch (switchdev_work->event) {
2762 case SWITCHDEV_FDB_ADD_TO_DEVICE:
2763 fdb_info = &switchdev_work->fdb_info;
2764 if (!fdb_info->added_by_user)
2765 break;
2766 err = rocker_world_port_fdb_add(rocker_port, fdb_info);
2767 if (err) {
2768 netdev_dbg(rocker_port->dev, "fdb add failed err=%d\n", err);
2769 break;
2771 rocker_fdb_offload_notify(rocker_port, fdb_info);
2772 break;
2773 case SWITCHDEV_FDB_DEL_TO_DEVICE:
2774 fdb_info = &switchdev_work->fdb_info;
2775 if (!fdb_info->added_by_user)
2776 break;
2777 err = rocker_world_port_fdb_del(rocker_port, fdb_info);
2778 if (err)
2779 netdev_dbg(rocker_port->dev, "fdb add failed err=%d\n", err);
2780 break;
2782 rtnl_unlock();
2784 kfree(switchdev_work->fdb_info.addr);
2785 kfree(switchdev_work);
2786 dev_put(rocker_port->dev);
2789 /* called under rcu_read_lock() */
2790 static int rocker_switchdev_event(struct notifier_block *unused,
2791 unsigned long event, void *ptr)
2793 struct net_device *dev = switchdev_notifier_info_to_dev(ptr);
2794 struct rocker_switchdev_event_work *switchdev_work;
2795 struct switchdev_notifier_fdb_info *fdb_info = ptr;
2796 struct rocker_port *rocker_port;
2798 if (!rocker_port_dev_check(dev))
2799 return NOTIFY_DONE;
2801 if (event == SWITCHDEV_PORT_ATTR_SET)
2802 return rocker_switchdev_port_attr_set_event(dev, ptr);
2804 rocker_port = netdev_priv(dev);
2805 switchdev_work = kzalloc(sizeof(*switchdev_work), GFP_ATOMIC);
2806 if (WARN_ON(!switchdev_work))
2807 return NOTIFY_BAD;
2809 INIT_WORK(&switchdev_work->work, rocker_switchdev_event_work);
2810 switchdev_work->rocker_port = rocker_port;
2811 switchdev_work->event = event;
2813 switch (event) {
2814 case SWITCHDEV_FDB_ADD_TO_DEVICE: /* fall through */
2815 case SWITCHDEV_FDB_DEL_TO_DEVICE:
2816 memcpy(&switchdev_work->fdb_info, ptr,
2817 sizeof(switchdev_work->fdb_info));
2818 switchdev_work->fdb_info.addr = kzalloc(ETH_ALEN, GFP_ATOMIC);
2819 if (unlikely(!switchdev_work->fdb_info.addr)) {
2820 kfree(switchdev_work);
2821 return NOTIFY_BAD;
2824 ether_addr_copy((u8 *)switchdev_work->fdb_info.addr,
2825 fdb_info->addr);
2826 /* Take a reference on the rocker device */
2827 dev_hold(dev);
2828 break;
2829 default:
2830 kfree(switchdev_work);
2831 return NOTIFY_DONE;
2834 queue_work(rocker_port->rocker->rocker_owq,
2835 &switchdev_work->work);
2836 return NOTIFY_DONE;
2839 static int
2840 rocker_switchdev_port_obj_event(unsigned long event, struct net_device *netdev,
2841 struct switchdev_notifier_port_obj_info *port_obj_info)
2843 int err = -EOPNOTSUPP;
2845 switch (event) {
2846 case SWITCHDEV_PORT_OBJ_ADD:
2847 err = rocker_port_obj_add(netdev, port_obj_info->obj,
2848 port_obj_info->trans);
2849 break;
2850 case SWITCHDEV_PORT_OBJ_DEL:
2851 err = rocker_port_obj_del(netdev, port_obj_info->obj);
2852 break;
2855 port_obj_info->handled = true;
2856 return notifier_from_errno(err);
2859 static int rocker_switchdev_blocking_event(struct notifier_block *unused,
2860 unsigned long event, void *ptr)
2862 struct net_device *dev = switchdev_notifier_info_to_dev(ptr);
2864 if (!rocker_port_dev_check(dev))
2865 return NOTIFY_DONE;
2867 switch (event) {
2868 case SWITCHDEV_PORT_OBJ_ADD:
2869 case SWITCHDEV_PORT_OBJ_DEL:
2870 return rocker_switchdev_port_obj_event(event, dev, ptr);
2871 case SWITCHDEV_PORT_ATTR_SET:
2872 return rocker_switchdev_port_attr_set_event(dev, ptr);
2875 return NOTIFY_DONE;
2878 static struct notifier_block rocker_switchdev_notifier = {
2879 .notifier_call = rocker_switchdev_event,
2882 static struct notifier_block rocker_switchdev_blocking_notifier = {
2883 .notifier_call = rocker_switchdev_blocking_event,
2886 static int rocker_probe(struct pci_dev *pdev, const struct pci_device_id *id)
2888 struct notifier_block *nb;
2889 struct rocker *rocker;
2890 int err;
2892 rocker = kzalloc(sizeof(*rocker), GFP_KERNEL);
2893 if (!rocker)
2894 return -ENOMEM;
2896 err = pci_enable_device(pdev);
2897 if (err) {
2898 dev_err(&pdev->dev, "pci_enable_device failed\n");
2899 goto err_pci_enable_device;
2902 err = pci_request_regions(pdev, rocker_driver_name);
2903 if (err) {
2904 dev_err(&pdev->dev, "pci_request_regions failed\n");
2905 goto err_pci_request_regions;
2908 err = pci_set_dma_mask(pdev, DMA_BIT_MASK(64));
2909 if (!err) {
2910 err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64));
2911 if (err) {
2912 dev_err(&pdev->dev, "pci_set_consistent_dma_mask failed\n");
2913 goto err_pci_set_dma_mask;
2915 } else {
2916 err = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
2917 if (err) {
2918 dev_err(&pdev->dev, "pci_set_dma_mask failed\n");
2919 goto err_pci_set_dma_mask;
2923 if (pci_resource_len(pdev, 0) < ROCKER_PCI_BAR0_SIZE) {
2924 dev_err(&pdev->dev, "invalid PCI region size\n");
2925 err = -EINVAL;
2926 goto err_pci_resource_len_check;
2929 rocker->hw_addr = ioremap(pci_resource_start(pdev, 0),
2930 pci_resource_len(pdev, 0));
2931 if (!rocker->hw_addr) {
2932 dev_err(&pdev->dev, "ioremap failed\n");
2933 err = -EIO;
2934 goto err_ioremap;
2936 pci_set_master(pdev);
2938 rocker->pdev = pdev;
2939 pci_set_drvdata(pdev, rocker);
2941 rocker->port_count = rocker_read32(rocker, PORT_PHYS_COUNT);
2943 err = rocker_msix_init(rocker);
2944 if (err) {
2945 dev_err(&pdev->dev, "MSI-X init failed\n");
2946 goto err_msix_init;
2949 err = rocker_basic_hw_test(rocker);
2950 if (err) {
2951 dev_err(&pdev->dev, "basic hw test failed\n");
2952 goto err_basic_hw_test;
2955 rocker_write32(rocker, CONTROL, ROCKER_CONTROL_RESET);
2957 err = rocker_dma_rings_init(rocker);
2958 if (err)
2959 goto err_dma_rings_init;
2961 err = request_irq(rocker_msix_vector(rocker, ROCKER_MSIX_VEC_CMD),
2962 rocker_cmd_irq_handler, 0,
2963 rocker_driver_name, rocker);
2964 if (err) {
2965 dev_err(&pdev->dev, "cannot assign cmd irq\n");
2966 goto err_request_cmd_irq;
2969 err = request_irq(rocker_msix_vector(rocker, ROCKER_MSIX_VEC_EVENT),
2970 rocker_event_irq_handler, 0,
2971 rocker_driver_name, rocker);
2972 if (err) {
2973 dev_err(&pdev->dev, "cannot assign event irq\n");
2974 goto err_request_event_irq;
2977 rocker->rocker_owq = alloc_ordered_workqueue(rocker_driver_name,
2978 WQ_MEM_RECLAIM);
2979 if (!rocker->rocker_owq) {
2980 err = -ENOMEM;
2981 goto err_alloc_ordered_workqueue;
2984 err = rocker_probe_ports(rocker);
2985 if (err) {
2986 dev_err(&pdev->dev, "failed to probe ports\n");
2987 goto err_probe_ports;
2990 /* Only FIBs pointing to our own netdevs are programmed into
2991 * the device, so no need to pass a callback.
2993 rocker->fib_nb.notifier_call = rocker_router_fib_event;
2994 err = register_fib_notifier(&init_net, &rocker->fib_nb, NULL, NULL);
2995 if (err)
2996 goto err_register_fib_notifier;
2998 err = register_switchdev_notifier(&rocker_switchdev_notifier);
2999 if (err) {
3000 dev_err(&pdev->dev, "Failed to register switchdev notifier\n");
3001 goto err_register_switchdev_notifier;
3004 nb = &rocker_switchdev_blocking_notifier;
3005 err = register_switchdev_blocking_notifier(nb);
3006 if (err) {
3007 dev_err(&pdev->dev, "Failed to register switchdev blocking notifier\n");
3008 goto err_register_switchdev_blocking_notifier;
3011 rocker->hw.id = rocker_read64(rocker, SWITCH_ID);
3013 dev_info(&pdev->dev, "Rocker switch with id %*phN\n",
3014 (int)sizeof(rocker->hw.id), &rocker->hw.id);
3016 return 0;
3018 err_register_switchdev_blocking_notifier:
3019 unregister_switchdev_notifier(&rocker_switchdev_notifier);
3020 err_register_switchdev_notifier:
3021 unregister_fib_notifier(&init_net, &rocker->fib_nb);
3022 err_register_fib_notifier:
3023 rocker_remove_ports(rocker);
3024 err_probe_ports:
3025 destroy_workqueue(rocker->rocker_owq);
3026 err_alloc_ordered_workqueue:
3027 free_irq(rocker_msix_vector(rocker, ROCKER_MSIX_VEC_EVENT), rocker);
3028 err_request_event_irq:
3029 free_irq(rocker_msix_vector(rocker, ROCKER_MSIX_VEC_CMD), rocker);
3030 err_request_cmd_irq:
3031 rocker_dma_rings_fini(rocker);
3032 err_dma_rings_init:
3033 err_basic_hw_test:
3034 rocker_msix_fini(rocker);
3035 err_msix_init:
3036 iounmap(rocker->hw_addr);
3037 err_ioremap:
3038 err_pci_resource_len_check:
3039 err_pci_set_dma_mask:
3040 pci_release_regions(pdev);
3041 err_pci_request_regions:
3042 pci_disable_device(pdev);
3043 err_pci_enable_device:
3044 kfree(rocker);
3045 return err;
3048 static void rocker_remove(struct pci_dev *pdev)
3050 struct rocker *rocker = pci_get_drvdata(pdev);
3051 struct notifier_block *nb;
3053 nb = &rocker_switchdev_blocking_notifier;
3054 unregister_switchdev_blocking_notifier(nb);
3056 unregister_switchdev_notifier(&rocker_switchdev_notifier);
3057 unregister_fib_notifier(&init_net, &rocker->fib_nb);
3058 rocker_remove_ports(rocker);
3059 rocker_write32(rocker, CONTROL, ROCKER_CONTROL_RESET);
3060 destroy_workqueue(rocker->rocker_owq);
3061 free_irq(rocker_msix_vector(rocker, ROCKER_MSIX_VEC_EVENT), rocker);
3062 free_irq(rocker_msix_vector(rocker, ROCKER_MSIX_VEC_CMD), rocker);
3063 rocker_dma_rings_fini(rocker);
3064 rocker_msix_fini(rocker);
3065 iounmap(rocker->hw_addr);
3066 pci_release_regions(rocker->pdev);
3067 pci_disable_device(rocker->pdev);
3068 kfree(rocker);
3071 static struct pci_driver rocker_pci_driver = {
3072 .name = rocker_driver_name,
3073 .id_table = rocker_pci_id_table,
3074 .probe = rocker_probe,
3075 .remove = rocker_remove,
3078 /************************************
3079 * Net device notifier event handler
3080 ************************************/
3082 static bool rocker_port_dev_check_under(const struct net_device *dev,
3083 struct rocker *rocker)
3085 struct rocker_port *rocker_port;
3087 if (!rocker_port_dev_check(dev))
3088 return false;
3090 rocker_port = netdev_priv(dev);
3091 if (rocker_port->rocker != rocker)
3092 return false;
3094 return true;
3097 struct rocker_walk_data {
3098 struct rocker *rocker;
3099 struct rocker_port *port;
3102 static int rocker_lower_dev_walk(struct net_device *lower_dev, void *_data)
3104 struct rocker_walk_data *data = _data;
3105 int ret = 0;
3107 if (rocker_port_dev_check_under(lower_dev, data->rocker)) {
3108 data->port = netdev_priv(lower_dev);
3109 ret = 1;
3112 return ret;
3115 struct rocker_port *rocker_port_dev_lower_find(struct net_device *dev,
3116 struct rocker *rocker)
3118 struct rocker_walk_data data;
3120 if (rocker_port_dev_check_under(dev, rocker))
3121 return netdev_priv(dev);
3123 data.rocker = rocker;
3124 data.port = NULL;
3125 netdev_walk_all_lower_dev(dev, rocker_lower_dev_walk, &data);
3127 return data.port;
3130 static int rocker_netdevice_event(struct notifier_block *unused,
3131 unsigned long event, void *ptr)
3133 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
3134 struct netdev_notifier_changeupper_info *info;
3135 struct rocker_port *rocker_port;
3136 int err;
3138 if (!rocker_port_dev_check(dev))
3139 return NOTIFY_DONE;
3141 switch (event) {
3142 case NETDEV_CHANGEUPPER:
3143 info = ptr;
3144 if (!info->master)
3145 goto out;
3146 rocker_port = netdev_priv(dev);
3147 if (info->linking) {
3148 err = rocker_world_port_master_linked(rocker_port,
3149 info->upper_dev);
3150 if (err)
3151 netdev_warn(dev, "failed to reflect master linked (err %d)\n",
3152 err);
3153 } else {
3154 err = rocker_world_port_master_unlinked(rocker_port,
3155 info->upper_dev);
3156 if (err)
3157 netdev_warn(dev, "failed to reflect master unlinked (err %d)\n",
3158 err);
3161 out:
3162 return NOTIFY_DONE;
3165 static struct notifier_block rocker_netdevice_nb __read_mostly = {
3166 .notifier_call = rocker_netdevice_event,
3169 /************************************
3170 * Net event notifier event handler
3171 ************************************/
3173 static int rocker_netevent_event(struct notifier_block *unused,
3174 unsigned long event, void *ptr)
3176 struct rocker_port *rocker_port;
3177 struct net_device *dev;
3178 struct neighbour *n = ptr;
3179 int err;
3181 switch (event) {
3182 case NETEVENT_NEIGH_UPDATE:
3183 if (n->tbl != &arp_tbl)
3184 return NOTIFY_DONE;
3185 dev = n->dev;
3186 if (!rocker_port_dev_check(dev))
3187 return NOTIFY_DONE;
3188 rocker_port = netdev_priv(dev);
3189 err = rocker_world_port_neigh_update(rocker_port, n);
3190 if (err)
3191 netdev_warn(dev, "failed to handle neigh update (err %d)\n",
3192 err);
3193 break;
3196 return NOTIFY_DONE;
3199 static struct notifier_block rocker_netevent_nb __read_mostly = {
3200 .notifier_call = rocker_netevent_event,
3203 /***********************
3204 * Module init and exit
3205 ***********************/
3207 static int __init rocker_module_init(void)
3209 int err;
3211 register_netdevice_notifier(&rocker_netdevice_nb);
3212 register_netevent_notifier(&rocker_netevent_nb);
3213 err = pci_register_driver(&rocker_pci_driver);
3214 if (err)
3215 goto err_pci_register_driver;
3216 return 0;
3218 err_pci_register_driver:
3219 unregister_netevent_notifier(&rocker_netevent_nb);
3220 unregister_netdevice_notifier(&rocker_netdevice_nb);
3221 return err;
3224 static void __exit rocker_module_exit(void)
3226 unregister_netevent_notifier(&rocker_netevent_nb);
3227 unregister_netdevice_notifier(&rocker_netdevice_nb);
3228 pci_unregister_driver(&rocker_pci_driver);
3231 module_init(rocker_module_init);
3232 module_exit(rocker_module_exit);
3234 MODULE_LICENSE("GPL v2");
3235 MODULE_AUTHOR("Jiri Pirko <jiri@resnulli.us>");
3236 MODULE_AUTHOR("Scott Feldman <sfeldma@gmail.com>");
3237 MODULE_DESCRIPTION("Rocker switch device driver");
3238 MODULE_DEVICE_TABLE(pci, rocker_pci_id_table);