1 // SPDX-License-Identifier: GPL-2.0-or-later
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>
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>
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"
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},
49 wait_queue_head_t wait
;
54 static void rocker_wait_reset(struct rocker_wait
*wait
)
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
);
76 static void rocker_wait_destroy(struct rocker_wait
*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);
90 static void rocker_wait_wake_up(struct rocker_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
;
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",
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",
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
;
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");
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
]);
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
;
191 unsigned char *expect
;
192 dma_addr_t dma_handle
;
196 alloc
= kzalloc(ROCKER_TEST_DMA_BUF_SIZE
* 2 + offset
,
197 GFP_KERNEL
| GFP_DMA
);
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
)) {
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
);
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
);
227 prandom_bytes(buf
, ROCKER_TEST_DMA_BUF_SIZE
);
228 for (i
= 0; i
< ROCKER_TEST_DMA_BUF_SIZE
; 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
);
237 pci_unmap_single(pdev
, dma_handle
, ROCKER_TEST_DMA_BUF_SIZE
,
238 PCI_DMA_BIDIRECTIONAL
);
245 static int rocker_dma_test(const struct rocker
*rocker
,
246 struct rocker_wait
*wait
)
251 for (i
= 0; i
< 8; i
++) {
252 err
= rocker_dma_test_offset(rocker
, wait
, i
);
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
);
268 static int rocker_basic_hw_test(const struct rocker
*rocker
)
270 const struct pci_dev
*pdev
= rocker
->pdev
;
271 struct rocker_wait wait
;
274 err
= rocker_reg_test(rocker
);
276 dev_err(&pdev
->dev
, "reg test failed\n");
280 err
= request_irq(rocker_msix_vector(rocker
, ROCKER_MSIX_VEC_TEST
),
281 rocker_test_irq_handler
, 0,
282 rocker_driver_name
, &wait
);
284 dev_err(&pdev
->dev
, "cannot assign test irq\n");
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");
297 err
= rocker_dma_test(rocker
, &wait
);
299 dev_err(&pdev
->dev
, "dma test failed\n");
302 free_irq(rocker_msix_vector(rocker
, ROCKER_MSIX_VEC_TEST
), &wait
);
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
;
332 case -ROCKER_EMSGSIZE
:
334 case -ROCKER_ENOTSUP
:
336 case -ROCKER_ENOBUFS
:
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;
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
,
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;
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
);
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
;
413 static void rocker_dma_ring_credits_set(const struct rocker
*rocker
,
414 const struct rocker_dma_ring_info
*info
,
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
,
430 struct rocker_dma_ring_info
*info
)
434 BUG_ON(size
!= rocker_dma_ring_size_fix(size
));
439 info
->desc_info
= kcalloc(info
->size
, sizeof(*info
->desc_info
),
441 if (!info
->desc_info
)
444 info
->desc
= pci_alloc_consistent(rocker
->pdev
,
445 info
->size
* sizeof(*info
->desc
),
448 kfree(info
->desc_info
);
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
);
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
)
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
;
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
;
503 buf
= kzalloc(buf_size
, GFP_KERNEL
| GFP_DMA
);
509 dma_handle
= pci_map_single(pdev
, buf
, buf_size
, direction
);
510 if (pci_dma_mapping_error(pdev
, dma_handle
)) {
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
;
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
);
536 static void rocker_dma_ring_bufs_free(const struct rocker
*rocker
,
537 const struct rocker_dma_ring_info
*info
,
540 struct pci_dev
*pdev
= rocker
->pdev
;
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
];
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();
562 rocker_desc_cookie_ptr_set(desc_info
, wait
);
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
;
580 for (i
= 0; i
< cmd_ring
->size
; i
++) {
581 err
= rocker_dma_cmd_ring_wait_alloc(&cmd_ring
->desc_info
[i
]);
588 for (i
--; i
>= 0; i
--)
589 rocker_dma_cmd_ring_wait_free(&cmd_ring
->desc_info
[i
]);
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
;
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
;
607 err
= rocker_dma_ring_create(rocker
, ROCKER_DMA_CMD
,
608 ROCKER_DMA_CMD_DEFAULT_SIZE
,
611 dev_err(&pdev
->dev
, "failed to create command dma ring\n");
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
);
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
);
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
);
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
);
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
);
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
);
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
,
680 if (pci_dma_mapping_error(pdev
, dma_handle
))
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
;
689 pci_unmap_single(pdev
, dma_handle
, buf_len
, PCI_DMA_FROMDEVICE
);
690 desc_info
->tlv_size
= 0;
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
;
704 size_t buf_len
= rocker_port_rx_buf_len(rocker_port
);
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
);
716 err
= rocker_dma_rx_ring_skb_map(rocker_port
, desc_info
, skb
, buf_len
);
718 dev_kfree_skb_any(skb
);
721 rocker_desc_cookie_ptr_set(desc_info
, skb
);
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
;
732 if (!attrs
[ROCKER_TLV_RX_FRAG_ADDR
] ||
733 !attrs
[ROCKER_TLV_RX_FRAG_MAX_LEN
])
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
);
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
;
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
]);
769 for (i
--; i
>= 0; i
--)
770 rocker_dma_rx_ring_skb_free(rocker
, &rx_ring
->desc_info
[i
]);
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
;
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
;
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
);
794 netdev_err(rocker_port
->dev
, "failed to create tx dma ring\n");
798 err
= rocker_dma_ring_bufs_alloc(rocker
, &rocker_port
->tx_ring
,
800 ROCKER_DMA_TX_DESC_SIZE
);
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
);
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
);
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
);
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
);
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
,
840 err_dma_tx_ring_bufs_alloc
:
841 rocker_dma_ring_destroy(rocker
, &rocker_port
->tx_ring
);
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
,
855 rocker_dma_ring_destroy(rocker
, &rocker_port
->tx_ring
);
858 static void rocker_port_set_enable(const struct rocker_port
*rocker_port
,
861 u64 val
= rocker_read64(rocker_port
->rocker
, PORT_PHYS_ENABLE
);
864 val
|= 1ULL << rocker_port
->pport
;
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
;
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
);
885 rocker_desc_gen_clear(desc_info
);
887 rocker_wait_wake_up(wait
);
891 spin_unlock(&rocker
->cmd_ring_lock
);
892 rocker_dma_ring_credits_set(rocker
, &rocker
->cmd_ring
, credits
);
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
;
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
])
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
)
928 rocker_port
= rocker
->ports
[port_number
];
929 if (netif_carrier_ok(rocker_port
->dev
) != link_up
) {
931 rocker_port_link_up(rocker_port
);
933 rocker_port_link_down(rocker_port
);
939 static int rocker_world_port_ev_mac_vlan_seen(struct rocker_port
*rocker_port
,
940 const unsigned char *addr
,
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
;
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
])
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
)
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
;
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
])
981 type
= rocker_tlv_get_u16(attrs
[ROCKER_TLV_EVENT_TYPE
]);
982 info
= attrs
[ROCKER_TLV_EVENT_INFO
];
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
);
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
;
1002 while ((desc_info
= rocker_desc_tail_get(&rocker
->event_ring
))) {
1003 err
= rocker_desc_err(desc_info
);
1005 dev_err(&pdev
->dev
, "event desc received with err %d\n",
1008 err
= rocker_event_process(rocker
, desc_info
);
1010 dev_err(&pdev
->dev
, "event processing failed with err %d\n",
1013 rocker_desc_gen_clear(desc_info
);
1014 rocker_desc_head_set(rocker
, &rocker
->event_ring
, desc_info
);
1017 rocker_dma_ring_credits_set(rocker
, &rocker
->event_ring
, credits
);
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
);
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
);
1038 /********************
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
;
1052 spin_lock_irqsave(&rocker
->cmd_ring_lock
, lock_flags
);
1054 desc_info
= rocker_desc_head_get(&rocker
->cmd_ring
);
1056 spin_unlock_irqrestore(&rocker
->cmd_ring_lock
, lock_flags
);
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
);
1066 spin_unlock_irqrestore(&rocker
->cmd_ring_lock
, lock_flags
);
1070 rocker_desc_head_set(rocker
, &rocker
->cmd_ring
, desc_info
);
1072 spin_unlock_irqrestore(&rocker
->cmd_ring_lock
, lock_flags
);
1077 if (!rocker_wait_event_timeout(wait
, HZ
/ 10))
1080 err
= rocker_desc_err(desc_info
);
1085 err
= process(rocker_port
, desc_info
, process_priv
);
1087 rocker_desc_gen_clear(desc_info
);
1092 rocker_cmd_get_port_settings_prep(const struct rocker_port
*rocker_port
,
1093 struct rocker_desc_info
*desc_info
,
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
))
1101 cmd_info
= rocker_tlv_nest_start(desc_info
, ROCKER_TLV_CMD_INFO
);
1104 if (rocker_tlv_put_u32(desc_info
, ROCKER_TLV_CMD_PORT_SETTINGS_PPORT
,
1105 rocker_port
->pport
))
1107 rocker_tlv_nest_end(desc_info
, cmd_info
);
1112 rocker_cmd_get_port_settings_ethtool_proc(const struct rocker_port
*rocker_port
,
1113 const struct rocker_desc_info
*desc_info
,
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];
1123 rocker_tlv_parse_desc(attrs
, ROCKER_TLV_CMD_MAX
, desc_info
);
1124 if (!attrs
[ROCKER_TLV_CMD_INFO
])
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
])
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
;
1151 rocker_cmd_get_port_settings_macaddr_proc(const struct rocker_port
*rocker_port
,
1152 const struct rocker_desc_info
*desc_info
,
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
])
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
];
1170 if (rocker_tlv_len(attr
) != ETH_ALEN
)
1173 ether_addr_copy(macaddr
, rocker_tlv_data(attr
));
1178 rocker_cmd_get_port_settings_mode_proc(const struct rocker_port
*rocker_port
,
1179 const struct rocker_desc_info
*desc_info
,
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
])
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
];
1197 *p_mode
= rocker_tlv_get_u8(info_attrs
[ROCKER_TLV_CMD_PORT_SETTINGS_MODE
]);
1207 rocker_cmd_get_port_settings_phys_name_proc(const struct rocker_port
*rocker_port
,
1208 const struct rocker_desc_info
*desc_info
,
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
;
1218 rocker_tlv_parse_desc(attrs
, ROCKER_TLV_CMD_MAX
, desc_info
);
1219 if (!attrs
[ROCKER_TLV_CMD_INFO
])
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
];
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
];
1242 name
->buf
[j
] = '\0';
1248 rocker_cmd_set_port_settings_ethtool_prep(const struct rocker_port
*rocker_port
,
1249 struct rocker_desc_info
*desc_info
,
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
))
1258 cmd_info
= rocker_tlv_nest_start(desc_info
, ROCKER_TLV_CMD_INFO
);
1261 if (rocker_tlv_put_u32(desc_info
, ROCKER_TLV_CMD_PORT_SETTINGS_PPORT
,
1262 rocker_port
->pport
))
1264 if (rocker_tlv_put_u32(desc_info
, ROCKER_TLV_CMD_PORT_SETTINGS_SPEED
,
1267 if (rocker_tlv_put_u8(desc_info
, ROCKER_TLV_CMD_PORT_SETTINGS_DUPLEX
,
1270 if (rocker_tlv_put_u8(desc_info
, ROCKER_TLV_CMD_PORT_SETTINGS_AUTONEG
,
1271 ecmd
->base
.autoneg
))
1273 rocker_tlv_nest_end(desc_info
, cmd_info
);
1278 rocker_cmd_set_port_settings_macaddr_prep(const struct rocker_port
*rocker_port
,
1279 struct rocker_desc_info
*desc_info
,
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
))
1288 cmd_info
= rocker_tlv_nest_start(desc_info
, ROCKER_TLV_CMD_INFO
);
1291 if (rocker_tlv_put_u32(desc_info
, ROCKER_TLV_CMD_PORT_SETTINGS_PPORT
,
1292 rocker_port
->pport
))
1294 if (rocker_tlv_put(desc_info
, ROCKER_TLV_CMD_PORT_SETTINGS_MACADDR
,
1297 rocker_tlv_nest_end(desc_info
, cmd_info
);
1302 rocker_cmd_set_port_settings_mtu_prep(const struct rocker_port
*rocker_port
,
1303 struct rocker_desc_info
*desc_info
,
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
))
1312 cmd_info
= rocker_tlv_nest_start(desc_info
, ROCKER_TLV_CMD_INFO
);
1315 if (rocker_tlv_put_u32(desc_info
, ROCKER_TLV_CMD_PORT_SETTINGS_PPORT
,
1316 rocker_port
->pport
))
1318 if (rocker_tlv_put_u16(desc_info
, ROCKER_TLV_CMD_PORT_SETTINGS_MTU
,
1321 rocker_tlv_nest_end(desc_info
, cmd_info
);
1326 rocker_cmd_set_port_learning_prep(const struct rocker_port
*rocker_port
,
1327 struct rocker_desc_info
*desc_info
,
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
))
1336 cmd_info
= rocker_tlv_nest_start(desc_info
, ROCKER_TLV_CMD_INFO
);
1339 if (rocker_tlv_put_u32(desc_info
, ROCKER_TLV_CMD_PORT_SETTINGS_PPORT
,
1340 rocker_port
->pport
))
1342 if (rocker_tlv_put_u8(desc_info
, ROCKER_TLV_CMD_PORT_SETTINGS_LEARNING
,
1345 rocker_tlv_nest_end(desc_info
, cmd_info
);
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
,
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
,
1368 static int rocker_cmd_get_port_settings_mode(struct rocker_port
*rocker_port
,
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
);
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(©_ecmd
, ecmd
, sizeof(copy_ecmd
));
1384 return rocker_cmd_exec(rocker_port
, false,
1385 rocker_cmd_set_port_settings_ethtool_prep
,
1386 ©_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
,
1400 return rocker_cmd_exec(rocker_port
, false,
1401 rocker_cmd_set_port_settings_mtu_prep
,
1405 int rocker_port_set_learning(struct rocker_port
*rocker_port
,
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
[] = {
1421 #define ROCKER_WORLD_OPS_LEN ARRAY_SIZE(rocker_world_ops)
1423 static struct rocker_world_ops
*rocker_world_ops_find(u8 mode
)
1427 for (i
= 0; i
< ROCKER_WORLD_OPS_LEN
; i
++)
1428 if (rocker_world_ops
[i
]->mode
== mode
)
1429 return rocker_world_ops
[i
];
1433 static int rocker_world_init(struct rocker
*rocker
, u8 mode
)
1435 struct rocker_world_ops
*wops
;
1438 wops
= rocker_world_ops_find(mode
);
1440 dev_err(&rocker
->pdev
->dev
, "port mode \"%d\" is not supported\n",
1444 rocker
->wops
= wops
;
1445 rocker
->wpriv
= kzalloc(wops
->priv_size
, GFP_KERNEL
);
1450 err
= wops
->init(rocker
);
1452 kfree(rocker
->wpriv
);
1456 static void rocker_world_fini(struct rocker
*rocker
)
1458 struct rocker_world_ops
*wops
= rocker
->wops
;
1460 if (!wops
|| !wops
->fini
)
1463 kfree(rocker
->wpriv
);
1466 static int rocker_world_check_init(struct rocker_port
*rocker_port
)
1468 struct rocker
*rocker
= rocker_port
->rocker
;
1472 err
= rocker_cmd_get_port_settings_mode(rocker_port
, &mode
);
1474 dev_err(&rocker
->pdev
->dev
, "failed to get port mode\n");
1478 if (rocker
->wops
->mode
!= mode
) {
1479 dev_err(&rocker
->pdev
->dev
, "hardware has ports in different worlds, which is not supported\n");
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
;
1492 rocker_port
->wpriv
= kzalloc(wops
->port_priv_size
, GFP_KERNEL
);
1493 if (!rocker_port
->wpriv
)
1495 if (!wops
->port_pre_init
)
1497 err
= wops
->port_pre_init(rocker_port
);
1499 kfree(rocker_port
->wpriv
);
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
)
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
)
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
)
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
)
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
)
1546 wops
->port_stop(rocker_port
);
1549 static int rocker_world_port_attr_stp_state_set(struct rocker_port
*rocker_port
,
1551 struct switchdev_trans
*trans
)
1553 struct rocker_world_ops
*wops
= rocker_port
->rocker
->wops
;
1555 if (!wops
->port_attr_stp_state_set
)
1558 if (switchdev_trans_ph_prepare(trans
))
1561 return wops
->port_attr_stp_state_set(rocker_port
, state
);
1565 rocker_world_port_attr_bridge_flags_support_get(const struct rocker_port
*
1568 p_brport_flags_support
)
1570 struct rocker_world_ops
*wops
= rocker_port
->rocker
->wops
;
1572 if (!wops
->port_attr_bridge_flags_support_get
)
1574 return wops
->port_attr_bridge_flags_support_get(rocker_port
,
1575 p_brport_flags_support
);
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
;
1587 if (!wops
->port_attr_bridge_flags_set
)
1590 err
= rocker_world_port_attr_bridge_flags_support_get(rocker_port
,
1595 if (brport_flags
& ~brport_flags_s
)
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
)
1611 if (switchdev_trans_ph_prepare(trans
))
1614 return wops
->port_attr_bridge_flags_set(rocker_port
, brport_flags
,
1619 rocker_world_port_attr_bridge_ageing_time_set(struct rocker_port
*rocker_port
,
1621 struct switchdev_trans
*trans
)
1624 struct rocker_world_ops
*wops
= rocker_port
->rocker
->wops
;
1626 if (!wops
->port_attr_bridge_ageing_time_set
)
1629 if (switchdev_trans_ph_prepare(trans
))
1632 return wops
->port_attr_bridge_ageing_time_set(rocker_port
, ageing_time
,
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
)
1646 if (switchdev_trans_ph_prepare(trans
))
1649 return wops
->port_obj_vlan_add(rocker_port
, vlan
);
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
))
1661 if (!wops
->port_obj_vlan_del
)
1663 return wops
->port_obj_vlan_del(rocker_port
, vlan
);
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
)
1675 return wops
->port_obj_fdb_add(rocker_port
, info
->vid
, info
->addr
);
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
)
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
)
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
)
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
)
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
)
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
,
1733 struct rocker_world_ops
*wops
= rocker_port
->rocker
->wops
;
1735 if (!wops
->port_ev_mac_vlan_seen
)
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
)
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
)
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
);
1772 static int rocker_port_open(struct net_device
*dev
)
1774 struct rocker_port
*rocker_port
= netdev_priv(dev
);
1777 err
= rocker_port_dma_rings_init(rocker_port
);
1781 err
= request_irq(rocker_msix_tx_vector(rocker_port
),
1782 rocker_tx_irq_handler
, 0,
1783 rocker_driver_name
, rocker_port
);
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
);
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
);
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
);
1810 err_world_port_open
:
1811 free_irq(rocker_msix_rx_vector(rocker_port
), rocker_port
);
1813 free_irq(rocker_msix_tx_vector(rocker_port
), rocker_port
);
1815 rocker_port_dma_rings_fini(rocker_port
);
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
);
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
;
1844 rocker_tlv_parse_desc(attrs
, ROCKER_TLV_TX_MAX
, desc_info
);
1845 if (!attrs
[ROCKER_TLV_TX_FRAGS
])
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
;
1852 if (rocker_tlv_type(attr
) != ROCKER_TLV_TX_FRAG
)
1854 rocker_tlv_parse_nested(frag_attrs
, ROCKER_TLV_TX_FRAG_ATTR_MAX
,
1856 if (!frag_attrs
[ROCKER_TLV_TX_FRAG_ATTR_ADDR
] ||
1857 !frag_attrs
[ROCKER_TLV_TX_FRAG_ATTR_LEN
])
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");
1880 frag
= rocker_tlv_nest_start(desc_info
, ROCKER_TLV_TX_FRAG
);
1883 if (rocker_tlv_put_u64(desc_info
, ROCKER_TLV_TX_FRAG_ATTR_ADDR
,
1886 if (rocker_tlv_put_u16(desc_info
, ROCKER_TLV_TX_FRAG_ATTR_LEN
,
1889 rocker_tlv_nest_end(desc_info
, frag
);
1893 rocker_tlv_nest_cancel(desc_info
, frag
);
1895 pci_unmap_single(pdev
, dma_handle
, buf_len
, DMA_TO_DEVICE
);
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
;
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
);
1920 err
= rocker_tx_desc_frag_map_put(rocker_port
, desc_info
,
1921 skb
->data
, skb_headlen(skb
));
1924 if (skb_shinfo(skb
)->nr_frags
> ROCKER_TX_FRAGS_MAX
) {
1925 err
= skb_linearize(skb
);
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
));
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
);
1946 netif_stop_queue(dev
);
1948 return NETDEV_TX_OK
;
1951 rocker_tx_desc_frags_unmap(rocker_port
, desc_info
);
1953 rocker_tlv_nest_cancel(desc_info
, frags
);
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
);
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
);
1973 memcpy(dev
->dev_addr
, addr
->sa_data
, dev
->addr_len
);
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
);
1984 rocker_port_stop(dev
);
1986 netdev_info(dev
, "MTU change from %d to %d\n", dev
->mtu
, new_mtu
);
1989 err
= rocker_cmd_set_port_settings_mtu(rocker_port
, new_mtu
);
1994 err
= rocker_port_open(dev
);
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
};
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
,
2011 return err
? -EOPNOTSUPP
: 0;
2014 static int rocker_port_change_proto_down(struct net_device
*dev
,
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
;
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
);
2031 err
= rocker_world_port_neigh_destroy(rocker_port
, n
);
2033 netdev_warn(rocker_port
->dev
, "failed to handle neigh destroy (err %d)\n",
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
);
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 /********************
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
);
2073 case SWITCHDEV_ATTR_ID_PORT_STP_STATE
:
2074 err
= rocker_world_port_attr_stp_state_set(rocker_port
,
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
,
2083 case SWITCHDEV_ATTR_ID_PORT_BRIDGE_FLAGS
:
2084 err
= rocker_world_port_attr_bridge_flags_set(rocker_port
,
2085 attr
->u
.brport_flags
,
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
,
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
);
2109 case SWITCHDEV_OBJ_ID_PORT_VLAN
:
2110 err
= rocker_world_port_obj_vlan_add(rocker_port
,
2111 SWITCHDEV_OBJ_PORT_VLAN(obj
),
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
);
2129 case SWITCHDEV_OBJ_ID_PORT_VLAN
:
2130 err
= rocker_world_port_obj_vlan_del(rocker_port
,
2131 SWITCHDEV_OBJ_PORT_VLAN(obj
));
2141 struct rocker_fib_event_work
{
2142 struct work_struct work
;
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
;
2159 /* Protect internal structures from changes */
2161 switch (fib_work
->event
) {
2162 case FIB_EVENT_ENTRY_REPLACE
:
2163 err
= rocker_world_fib4_add(rocker
, &fib_work
->fen_info
);
2165 rocker_world_fib4_abort(rocker
);
2166 fib_info_put(fib_work
->fen_info
.fi
);
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
);
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
);
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
)
2195 fib_work
= kzalloc(sizeof(*fib_work
), GFP_ATOMIC
);
2196 if (WARN_ON(!fib_work
))
2199 INIT_WORK(&fib_work
->work
, rocker_router_fib_event_work
);
2200 fib_work
->rocker
= rocker
;
2201 fib_work
->event
= 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");
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");
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
);
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
);
2234 queue_work(rocker
->rocker_owq
, &fib_work
->work
);
2239 /********************
2241 ********************/
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
);
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
];
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
,
2291 switch (stringset
) {
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
;
2302 rocker_cmd_get_port_stats_prep(const struct rocker_port
*rocker_port
,
2303 struct rocker_desc_info
*desc_info
,
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
))
2312 cmd_stats
= rocker_tlv_nest_start(desc_info
, ROCKER_TLV_CMD_INFO
);
2316 if (rocker_tlv_put_u32(desc_info
, ROCKER_TLV_CMD_PORT_STATS_PPORT
,
2317 rocker_port
->pport
))
2320 rocker_tlv_nest_end(desc_info
, cmd_stats
);
2326 rocker_cmd_get_port_stats_ethtool_proc(const struct rocker_port
*rocker_port
,
2327 const struct rocker_desc_info
*desc_info
,
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
;
2337 rocker_tlv_parse_desc(attrs
, ROCKER_TLV_CMD_MAX
, desc_info
);
2339 if (!attrs
[ROCKER_TLV_CMD_INFO
])
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
])
2348 pport
= rocker_tlv_get_u32(stats_attrs
[ROCKER_TLV_CMD_PORT_STATS_PPORT
]);
2349 if (pport
!= rocker_port
->pport
)
2352 for (i
= 0; i
< ARRAY_SIZE(rocker_port_stats
); i
++) {
2353 pattr
= stats_attrs
[rocker_port_stats
[i
].type
];
2357 data
[i
] = rocker_tlv_get_u64(pattr
);
2363 static int rocker_cmd_get_port_stats_ethtool(struct rocker_port
*rocker_port
,
2366 return rocker_cmd_exec(rocker_port
, false,
2367 rocker_cmd_get_port_stats_prep
, NULL
,
2368 rocker_cmd_get_port_stats_ethtool_proc
,
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) {
2380 for (i
= 0; i
< ARRAY_SIZE(rocker_port_stats
); ++i
)
2385 static int rocker_port_get_sset_count(struct net_device
*netdev
, int sset
)
2389 return ROCKER_PORT_STATS_LEN
;
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
,
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
;
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",
2430 rocker_tx_desc_frags_unmap(rocker_port
, desc_info
);
2432 skb
= rocker_desc_cookie_ptr_get(desc_info
);
2434 rocker_port
->dev
->stats
.tx_packets
++;
2435 rocker_port
->dev
->stats
.tx_bytes
+= skb
->len
;
2437 rocker_port
->dev
->stats
.tx_errors
++;
2440 dev_kfree_skb_any(skb
);
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
);
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
);
2465 rocker_tlv_parse_desc(attrs
, ROCKER_TLV_RX_MAX
, desc_info
);
2466 if (!attrs
[ROCKER_TLV_RX_FRAG_LEN
])
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
;
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
);
2506 if (net_ratelimit())
2507 netdev_err(rocker_port
->dev
, "rx desc received with err %d\n",
2510 err
= rocker_port_rx_proc(rocker
, rocker_port
,
2512 if (err
&& net_ratelimit())
2513 netdev_err(rocker_port
->dev
, "rx processing failed with err %d\n",
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
);
2524 if (credits
< budget
)
2525 napi_complete_done(napi
, credits
);
2527 rocker_dma_ring_credits_set(rocker
, &rocker_port
->rx_ring
, credits
);
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
);
2542 link_up
= link_status
& (1 << rocker_port
->pport
);
2544 netif_carrier_on(rocker_port
->dev
);
2546 netif_carrier_off(rocker_port
->dev
);
2549 static void rocker_remove_ports(struct rocker
*rocker
)
2551 struct rocker_port
*rocker_port
;
2554 for (i
= 0; i
< rocker
->port_count
; i
++) {
2555 rocker_port
= rocker
->ports
[i
];
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
;
2573 err
= rocker_cmd_get_port_settings_macaddr(rocker_port
,
2574 rocker_port
->dev
->dev_addr
);
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
;
2590 dev
= alloc_etherdev(sizeof(struct rocker_port
));
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
);
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
,
2611 netif_napi_add(dev
, &rocker_port
->napi_rx
, rocker_port_poll_rx
,
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
);
2623 dev_err(&pdev
->dev
, "port world pre-init failed\n");
2624 goto err_world_port_pre_init
;
2626 err
= register_netdev(dev
);
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
);
2635 dev_err(&pdev
->dev
, "port world init failed\n");
2636 goto err_world_port_init
;
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
:
2652 static int rocker_probe_ports(struct rocker
*rocker
)
2658 alloc_size
= sizeof(struct rocker_port
*) * rocker
->port_count
;
2659 rocker
->ports
= kzalloc(alloc_size
, GFP_KERNEL
);
2662 for (i
= 0; i
< rocker
->port_count
; i
++) {
2663 err
= rocker_probe_port(rocker
, i
);
2670 rocker_remove_ports(rocker
);
2674 static int rocker_msix_init(struct rocker
*rocker
)
2676 struct pci_dev
*pdev
= rocker
->pdev
;
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
))
2688 rocker
->msix_entries
= kmalloc_array(msix_entries
,
2689 sizeof(struct msix_entry
),
2691 if (!rocker
->msix_entries
)
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
);
2699 goto err_enable_msix
;
2704 kfree(rocker
->msix_entries
);
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
;
2720 rocker_switchdev_port_attr_set_event(struct net_device
*netdev
,
2721 struct switchdev_notifier_port_attr_info
*port_attr_info
)
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
;
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
;
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
)
2766 err
= rocker_world_port_fdb_add(rocker_port
, fdb_info
);
2768 netdev_dbg(rocker_port
->dev
, "fdb add failed err=%d\n", err
);
2771 rocker_fdb_offload_notify(rocker_port
, fdb_info
);
2773 case SWITCHDEV_FDB_DEL_TO_DEVICE
:
2774 fdb_info
= &switchdev_work
->fdb_info
;
2775 if (!fdb_info
->added_by_user
)
2777 err
= rocker_world_port_fdb_del(rocker_port
, fdb_info
);
2779 netdev_dbg(rocker_port
->dev
, "fdb add failed err=%d\n", err
);
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
))
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
))
2809 INIT_WORK(&switchdev_work
->work
, rocker_switchdev_event_work
);
2810 switchdev_work
->rocker_port
= rocker_port
;
2811 switchdev_work
->event
= 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
);
2824 ether_addr_copy((u8
*)switchdev_work
->fdb_info
.addr
,
2826 /* Take a reference on the rocker device */
2830 kfree(switchdev_work
);
2834 queue_work(rocker_port
->rocker
->rocker_owq
,
2835 &switchdev_work
->work
);
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
;
2846 case SWITCHDEV_PORT_OBJ_ADD
:
2847 err
= rocker_port_obj_add(netdev
, port_obj_info
->obj
,
2848 port_obj_info
->trans
);
2850 case SWITCHDEV_PORT_OBJ_DEL
:
2851 err
= rocker_port_obj_del(netdev
, port_obj_info
->obj
);
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
))
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
);
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
;
2892 rocker
= kzalloc(sizeof(*rocker
), GFP_KERNEL
);
2896 err
= pci_enable_device(pdev
);
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
);
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));
2910 err
= pci_set_consistent_dma_mask(pdev
, DMA_BIT_MASK(64));
2912 dev_err(&pdev
->dev
, "pci_set_consistent_dma_mask failed\n");
2913 goto err_pci_set_dma_mask
;
2916 err
= pci_set_dma_mask(pdev
, DMA_BIT_MASK(32));
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");
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");
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
);
2945 dev_err(&pdev
->dev
, "MSI-X init failed\n");
2949 err
= rocker_basic_hw_test(rocker
);
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
);
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
);
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
);
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
,
2979 if (!rocker
->rocker_owq
) {
2981 goto err_alloc_ordered_workqueue
;
2984 err
= rocker_probe_ports(rocker
);
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
);
2996 goto err_register_fib_notifier
;
2998 err
= register_switchdev_notifier(&rocker_switchdev_notifier
);
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
);
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
);
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
);
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
);
3034 rocker_msix_fini(rocker
);
3036 iounmap(rocker
->hw_addr
);
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
:
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
);
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
))
3090 rocker_port
= netdev_priv(dev
);
3091 if (rocker_port
->rocker
!= rocker
)
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
;
3107 if (rocker_port_dev_check_under(lower_dev
, data
->rocker
)) {
3108 data
->port
= netdev_priv(lower_dev
);
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
;
3125 netdev_walk_all_lower_dev(dev
, rocker_lower_dev_walk
, &data
);
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
;
3138 if (!rocker_port_dev_check(dev
))
3142 case NETDEV_CHANGEUPPER
:
3146 rocker_port
= netdev_priv(dev
);
3147 if (info
->linking
) {
3148 err
= rocker_world_port_master_linked(rocker_port
,
3151 netdev_warn(dev
, "failed to reflect master linked (err %d)\n",
3154 err
= rocker_world_port_master_unlinked(rocker_port
,
3157 netdev_warn(dev
, "failed to reflect master unlinked (err %d)\n",
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
;
3182 case NETEVENT_NEIGH_UPDATE
:
3183 if (n
->tbl
!= &arp_tbl
)
3186 if (!rocker_port_dev_check(dev
))
3188 rocker_port
= netdev_priv(dev
);
3189 err
= rocker_world_port_neigh_update(rocker_port
, n
);
3191 netdev_warn(dev
, "failed to handle neigh update (err %d)\n",
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)
3211 register_netdevice_notifier(&rocker_netdevice_nb
);
3212 register_netevent_notifier(&rocker_netevent_nb
);
3213 err
= pci_register_driver(&rocker_pci_driver
);
3215 goto err_pci_register_driver
;
3218 err_pci_register_driver
:
3219 unregister_netevent_notifier(&rocker_netevent_nb
);
3220 unregister_netdevice_notifier(&rocker_netdevice_nb
);
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
);