2 * drivers/net/ethernet/rocker/rocker.c - Rocker switch device driver
3 * Copyright (c) 2014-2016 Jiri Pirko <jiri@mellanox.com>
4 * Copyright (c) 2014 Scott Feldman <sfeldma@gmail.com>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
12 #include <linux/kernel.h>
13 #include <linux/module.h>
14 #include <linux/pci.h>
15 #include <linux/interrupt.h>
16 #include <linux/sched.h>
17 #include <linux/wait.h>
18 #include <linux/spinlock.h>
19 #include <linux/sort.h>
20 #include <linux/random.h>
21 #include <linux/netdevice.h>
22 #include <linux/skbuff.h>
23 #include <linux/socket.h>
24 #include <linux/etherdevice.h>
25 #include <linux/ethtool.h>
26 #include <linux/if_ether.h>
27 #include <linux/if_vlan.h>
28 #include <linux/if_bridge.h>
29 #include <linux/bitops.h>
30 #include <linux/ctype.h>
31 #include <linux/workqueue.h>
32 #include <net/switchdev.h>
33 #include <net/rtnetlink.h>
34 #include <net/netevent.h>
36 #include <net/fib_rules.h>
37 #include <net/fib_notifier.h>
38 #include <linux/io-64-nonatomic-lo-hi.h>
39 #include <generated/utsrelease.h>
41 #include "rocker_hw.h"
43 #include "rocker_tlv.h"
45 static const char rocker_driver_name
[] = "rocker";
47 static const struct pci_device_id rocker_pci_id_table
[] = {
48 {PCI_VDEVICE(REDHAT
, PCI_DEVICE_ID_REDHAT_ROCKER
), 0},
53 wait_queue_head_t wait
;
58 static void rocker_wait_reset(struct rocker_wait
*wait
)
64 static void rocker_wait_init(struct rocker_wait
*wait
)
66 init_waitqueue_head(&wait
->wait
);
67 rocker_wait_reset(wait
);
70 static struct rocker_wait
*rocker_wait_create(void)
72 struct rocker_wait
*wait
;
74 wait
= kzalloc(sizeof(*wait
), GFP_KERNEL
);
80 static void rocker_wait_destroy(struct rocker_wait
*wait
)
85 static bool rocker_wait_event_timeout(struct rocker_wait
*wait
,
86 unsigned long timeout
)
88 wait_event_timeout(wait
->wait
, wait
->done
, HZ
/ 10);
94 static void rocker_wait_wake_up(struct rocker_wait
*wait
)
100 static u32
rocker_msix_vector(const struct rocker
*rocker
, unsigned int vector
)
102 return rocker
->msix_entries
[vector
].vector
;
105 static u32
rocker_msix_tx_vector(const struct rocker_port
*rocker_port
)
107 return rocker_msix_vector(rocker_port
->rocker
,
108 ROCKER_MSIX_VEC_TX(rocker_port
->port_number
));
111 static u32
rocker_msix_rx_vector(const struct rocker_port
*rocker_port
)
113 return rocker_msix_vector(rocker_port
->rocker
,
114 ROCKER_MSIX_VEC_RX(rocker_port
->port_number
));
117 #define rocker_write32(rocker, reg, val) \
118 writel((val), (rocker)->hw_addr + (ROCKER_ ## reg))
119 #define rocker_read32(rocker, reg) \
120 readl((rocker)->hw_addr + (ROCKER_ ## reg))
121 #define rocker_write64(rocker, reg, val) \
122 writeq((val), (rocker)->hw_addr + (ROCKER_ ## reg))
123 #define rocker_read64(rocker, reg) \
124 readq((rocker)->hw_addr + (ROCKER_ ## reg))
126 /*****************************
127 * HW basic testing functions
128 *****************************/
130 static int rocker_reg_test(const struct rocker
*rocker
)
132 const struct pci_dev
*pdev
= rocker
->pdev
;
138 rocker_write32(rocker
, TEST_REG
, rnd
);
139 test_reg
= rocker_read32(rocker
, TEST_REG
);
140 if (test_reg
!= rnd
* 2) {
141 dev_err(&pdev
->dev
, "unexpected 32bit register value %08llx, expected %08llx\n",
148 rnd
|= prandom_u32();
149 rocker_write64(rocker
, TEST_REG64
, rnd
);
150 test_reg
= rocker_read64(rocker
, TEST_REG64
);
151 if (test_reg
!= rnd
* 2) {
152 dev_err(&pdev
->dev
, "unexpected 64bit register value %16llx, expected %16llx\n",
160 static int rocker_dma_test_one(const struct rocker
*rocker
,
161 struct rocker_wait
*wait
, u32 test_type
,
162 dma_addr_t dma_handle
, const unsigned char *buf
,
163 const unsigned char *expect
, size_t size
)
165 const struct pci_dev
*pdev
= rocker
->pdev
;
168 rocker_wait_reset(wait
);
169 rocker_write32(rocker
, TEST_DMA_CTRL
, test_type
);
171 if (!rocker_wait_event_timeout(wait
, HZ
/ 10)) {
172 dev_err(&pdev
->dev
, "no interrupt received within a timeout\n");
176 for (i
= 0; i
< size
; i
++) {
177 if (buf
[i
] != expect
[i
]) {
178 dev_err(&pdev
->dev
, "unexpected memory content %02x at byte %x\n, %02x expected",
179 buf
[i
], i
, expect
[i
]);
186 #define ROCKER_TEST_DMA_BUF_SIZE (PAGE_SIZE * 4)
187 #define ROCKER_TEST_DMA_FILL_PATTERN 0x96
189 static int rocker_dma_test_offset(const struct rocker
*rocker
,
190 struct rocker_wait
*wait
, int offset
)
192 struct pci_dev
*pdev
= rocker
->pdev
;
193 unsigned char *alloc
;
195 unsigned char *expect
;
196 dma_addr_t dma_handle
;
200 alloc
= kzalloc(ROCKER_TEST_DMA_BUF_SIZE
* 2 + offset
,
201 GFP_KERNEL
| GFP_DMA
);
204 buf
= alloc
+ offset
;
205 expect
= buf
+ ROCKER_TEST_DMA_BUF_SIZE
;
207 dma_handle
= pci_map_single(pdev
, buf
, ROCKER_TEST_DMA_BUF_SIZE
,
208 PCI_DMA_BIDIRECTIONAL
);
209 if (pci_dma_mapping_error(pdev
, dma_handle
)) {
214 rocker_write64(rocker
, TEST_DMA_ADDR
, dma_handle
);
215 rocker_write32(rocker
, TEST_DMA_SIZE
, ROCKER_TEST_DMA_BUF_SIZE
);
217 memset(expect
, ROCKER_TEST_DMA_FILL_PATTERN
, ROCKER_TEST_DMA_BUF_SIZE
);
218 err
= rocker_dma_test_one(rocker
, wait
, ROCKER_TEST_DMA_CTRL_FILL
,
219 dma_handle
, buf
, expect
,
220 ROCKER_TEST_DMA_BUF_SIZE
);
224 memset(expect
, 0, ROCKER_TEST_DMA_BUF_SIZE
);
225 err
= rocker_dma_test_one(rocker
, wait
, ROCKER_TEST_DMA_CTRL_CLEAR
,
226 dma_handle
, buf
, expect
,
227 ROCKER_TEST_DMA_BUF_SIZE
);
231 prandom_bytes(buf
, ROCKER_TEST_DMA_BUF_SIZE
);
232 for (i
= 0; i
< ROCKER_TEST_DMA_BUF_SIZE
; i
++)
234 err
= rocker_dma_test_one(rocker
, wait
, ROCKER_TEST_DMA_CTRL_INVERT
,
235 dma_handle
, buf
, expect
,
236 ROCKER_TEST_DMA_BUF_SIZE
);
241 pci_unmap_single(pdev
, dma_handle
, ROCKER_TEST_DMA_BUF_SIZE
,
242 PCI_DMA_BIDIRECTIONAL
);
249 static int rocker_dma_test(const struct rocker
*rocker
,
250 struct rocker_wait
*wait
)
255 for (i
= 0; i
< 8; i
++) {
256 err
= rocker_dma_test_offset(rocker
, wait
, i
);
263 static irqreturn_t
rocker_test_irq_handler(int irq
, void *dev_id
)
265 struct rocker_wait
*wait
= dev_id
;
267 rocker_wait_wake_up(wait
);
272 static int rocker_basic_hw_test(const struct rocker
*rocker
)
274 const struct pci_dev
*pdev
= rocker
->pdev
;
275 struct rocker_wait wait
;
278 err
= rocker_reg_test(rocker
);
280 dev_err(&pdev
->dev
, "reg test failed\n");
284 err
= request_irq(rocker_msix_vector(rocker
, ROCKER_MSIX_VEC_TEST
),
285 rocker_test_irq_handler
, 0,
286 rocker_driver_name
, &wait
);
288 dev_err(&pdev
->dev
, "cannot assign test irq\n");
292 rocker_wait_init(&wait
);
293 rocker_write32(rocker
, TEST_IRQ
, ROCKER_MSIX_VEC_TEST
);
295 if (!rocker_wait_event_timeout(&wait
, HZ
/ 10)) {
296 dev_err(&pdev
->dev
, "no interrupt received within a timeout\n");
301 err
= rocker_dma_test(rocker
, &wait
);
303 dev_err(&pdev
->dev
, "dma test failed\n");
306 free_irq(rocker_msix_vector(rocker
, ROCKER_MSIX_VEC_TEST
), &wait
);
310 /******************************************
311 * DMA rings and descriptors manipulations
312 ******************************************/
314 static u32
__pos_inc(u32 pos
, size_t limit
)
316 return ++pos
== limit
? 0 : pos
;
319 static int rocker_desc_err(const struct rocker_desc_info
*desc_info
)
321 int err
= desc_info
->desc
->comp_err
& ~ROCKER_DMA_DESC_COMP_ERR_GEN
;
336 case -ROCKER_EMSGSIZE
:
338 case -ROCKER_ENOTSUP
:
340 case -ROCKER_ENOBUFS
:
347 static void rocker_desc_gen_clear(const struct rocker_desc_info
*desc_info
)
349 desc_info
->desc
->comp_err
&= ~ROCKER_DMA_DESC_COMP_ERR_GEN
;
352 static bool rocker_desc_gen(const struct rocker_desc_info
*desc_info
)
354 u32 comp_err
= desc_info
->desc
->comp_err
;
356 return comp_err
& ROCKER_DMA_DESC_COMP_ERR_GEN
? true : false;
360 rocker_desc_cookie_ptr_get(const struct rocker_desc_info
*desc_info
)
362 return (void *)(uintptr_t)desc_info
->desc
->cookie
;
365 static void rocker_desc_cookie_ptr_set(const struct rocker_desc_info
*desc_info
,
368 desc_info
->desc
->cookie
= (uintptr_t) ptr
;
371 static struct rocker_desc_info
*
372 rocker_desc_head_get(const struct rocker_dma_ring_info
*info
)
374 static struct rocker_desc_info
*desc_info
;
375 u32 head
= __pos_inc(info
->head
, info
->size
);
377 desc_info
= &info
->desc_info
[info
->head
];
378 if (head
== info
->tail
)
379 return NULL
; /* ring full */
380 desc_info
->tlv_size
= 0;
384 static void rocker_desc_commit(const struct rocker_desc_info
*desc_info
)
386 desc_info
->desc
->buf_size
= desc_info
->data_size
;
387 desc_info
->desc
->tlv_size
= desc_info
->tlv_size
;
390 static void rocker_desc_head_set(const struct rocker
*rocker
,
391 struct rocker_dma_ring_info
*info
,
392 const struct rocker_desc_info
*desc_info
)
394 u32 head
= __pos_inc(info
->head
, info
->size
);
396 BUG_ON(head
== info
->tail
);
397 rocker_desc_commit(desc_info
);
399 rocker_write32(rocker
, DMA_DESC_HEAD(info
->type
), head
);
402 static struct rocker_desc_info
*
403 rocker_desc_tail_get(struct rocker_dma_ring_info
*info
)
405 static struct rocker_desc_info
*desc_info
;
407 if (info
->tail
== info
->head
)
408 return NULL
; /* nothing to be done between head and tail */
409 desc_info
= &info
->desc_info
[info
->tail
];
410 if (!rocker_desc_gen(desc_info
))
411 return NULL
; /* gen bit not set, desc is not ready yet */
412 info
->tail
= __pos_inc(info
->tail
, info
->size
);
413 desc_info
->tlv_size
= desc_info
->desc
->tlv_size
;
417 static void rocker_dma_ring_credits_set(const struct rocker
*rocker
,
418 const struct rocker_dma_ring_info
*info
,
422 rocker_write32(rocker
, DMA_DESC_CREDITS(info
->type
), credits
);
425 static unsigned long rocker_dma_ring_size_fix(size_t size
)
427 return max(ROCKER_DMA_SIZE_MIN
,
428 min(roundup_pow_of_two(size
), ROCKER_DMA_SIZE_MAX
));
431 static int rocker_dma_ring_create(const struct rocker
*rocker
,
434 struct rocker_dma_ring_info
*info
)
438 BUG_ON(size
!= rocker_dma_ring_size_fix(size
));
443 info
->desc_info
= kcalloc(info
->size
, sizeof(*info
->desc_info
),
445 if (!info
->desc_info
)
448 info
->desc
= pci_alloc_consistent(rocker
->pdev
,
449 info
->size
* sizeof(*info
->desc
),
452 kfree(info
->desc_info
);
456 for (i
= 0; i
< info
->size
; i
++)
457 info
->desc_info
[i
].desc
= &info
->desc
[i
];
459 rocker_write32(rocker
, DMA_DESC_CTRL(info
->type
),
460 ROCKER_DMA_DESC_CTRL_RESET
);
461 rocker_write64(rocker
, DMA_DESC_ADDR(info
->type
), info
->mapaddr
);
462 rocker_write32(rocker
, DMA_DESC_SIZE(info
->type
), info
->size
);
467 static void rocker_dma_ring_destroy(const struct rocker
*rocker
,
468 const struct rocker_dma_ring_info
*info
)
470 rocker_write64(rocker
, DMA_DESC_ADDR(info
->type
), 0);
472 pci_free_consistent(rocker
->pdev
,
473 info
->size
* sizeof(struct rocker_desc
),
474 info
->desc
, info
->mapaddr
);
475 kfree(info
->desc_info
);
478 static void rocker_dma_ring_pass_to_producer(const struct rocker
*rocker
,
479 struct rocker_dma_ring_info
*info
)
483 BUG_ON(info
->head
|| info
->tail
);
485 /* When ring is consumer, we need to advance head for each desc.
486 * That tells hw that the desc is ready to be used by it.
488 for (i
= 0; i
< info
->size
- 1; i
++)
489 rocker_desc_head_set(rocker
, info
, &info
->desc_info
[i
]);
490 rocker_desc_commit(&info
->desc_info
[i
]);
493 static int rocker_dma_ring_bufs_alloc(const struct rocker
*rocker
,
494 const struct rocker_dma_ring_info
*info
,
495 int direction
, size_t buf_size
)
497 struct pci_dev
*pdev
= rocker
->pdev
;
501 for (i
= 0; i
< info
->size
; i
++) {
502 struct rocker_desc_info
*desc_info
= &info
->desc_info
[i
];
503 struct rocker_desc
*desc
= &info
->desc
[i
];
504 dma_addr_t dma_handle
;
507 buf
= kzalloc(buf_size
, GFP_KERNEL
| GFP_DMA
);
513 dma_handle
= pci_map_single(pdev
, buf
, buf_size
, direction
);
514 if (pci_dma_mapping_error(pdev
, dma_handle
)) {
520 desc_info
->data
= buf
;
521 desc_info
->data_size
= buf_size
;
522 dma_unmap_addr_set(desc_info
, mapaddr
, dma_handle
);
524 desc
->buf_addr
= dma_handle
;
525 desc
->buf_size
= buf_size
;
530 for (i
--; i
>= 0; i
--) {
531 const struct rocker_desc_info
*desc_info
= &info
->desc_info
[i
];
533 pci_unmap_single(pdev
, dma_unmap_addr(desc_info
, mapaddr
),
534 desc_info
->data_size
, direction
);
535 kfree(desc_info
->data
);
540 static void rocker_dma_ring_bufs_free(const struct rocker
*rocker
,
541 const struct rocker_dma_ring_info
*info
,
544 struct pci_dev
*pdev
= rocker
->pdev
;
547 for (i
= 0; i
< info
->size
; i
++) {
548 const struct rocker_desc_info
*desc_info
= &info
->desc_info
[i
];
549 struct rocker_desc
*desc
= &info
->desc
[i
];
553 pci_unmap_single(pdev
, dma_unmap_addr(desc_info
, mapaddr
),
554 desc_info
->data_size
, direction
);
555 kfree(desc_info
->data
);
559 static int rocker_dma_cmd_ring_wait_alloc(struct rocker_desc_info
*desc_info
)
561 struct rocker_wait
*wait
;
563 wait
= rocker_wait_create();
566 rocker_desc_cookie_ptr_set(desc_info
, wait
);
571 rocker_dma_cmd_ring_wait_free(const struct rocker_desc_info
*desc_info
)
573 struct rocker_wait
*wait
= rocker_desc_cookie_ptr_get(desc_info
);
575 rocker_wait_destroy(wait
);
578 static int rocker_dma_cmd_ring_waits_alloc(const struct rocker
*rocker
)
580 const struct rocker_dma_ring_info
*cmd_ring
= &rocker
->cmd_ring
;
584 for (i
= 0; i
< cmd_ring
->size
; i
++) {
585 err
= rocker_dma_cmd_ring_wait_alloc(&cmd_ring
->desc_info
[i
]);
592 for (i
--; i
>= 0; i
--)
593 rocker_dma_cmd_ring_wait_free(&cmd_ring
->desc_info
[i
]);
597 static void rocker_dma_cmd_ring_waits_free(const struct rocker
*rocker
)
599 const struct rocker_dma_ring_info
*cmd_ring
= &rocker
->cmd_ring
;
602 for (i
= 0; i
< cmd_ring
->size
; i
++)
603 rocker_dma_cmd_ring_wait_free(&cmd_ring
->desc_info
[i
]);
606 static int rocker_dma_rings_init(struct rocker
*rocker
)
608 const struct pci_dev
*pdev
= rocker
->pdev
;
611 err
= rocker_dma_ring_create(rocker
, ROCKER_DMA_CMD
,
612 ROCKER_DMA_CMD_DEFAULT_SIZE
,
615 dev_err(&pdev
->dev
, "failed to create command dma ring\n");
619 spin_lock_init(&rocker
->cmd_ring_lock
);
621 err
= rocker_dma_ring_bufs_alloc(rocker
, &rocker
->cmd_ring
,
622 PCI_DMA_BIDIRECTIONAL
, PAGE_SIZE
);
624 dev_err(&pdev
->dev
, "failed to alloc command dma ring buffers\n");
625 goto err_dma_cmd_ring_bufs_alloc
;
628 err
= rocker_dma_cmd_ring_waits_alloc(rocker
);
630 dev_err(&pdev
->dev
, "failed to alloc command dma ring waits\n");
631 goto err_dma_cmd_ring_waits_alloc
;
634 err
= rocker_dma_ring_create(rocker
, ROCKER_DMA_EVENT
,
635 ROCKER_DMA_EVENT_DEFAULT_SIZE
,
636 &rocker
->event_ring
);
638 dev_err(&pdev
->dev
, "failed to create event dma ring\n");
639 goto err_dma_event_ring_create
;
642 err
= rocker_dma_ring_bufs_alloc(rocker
, &rocker
->event_ring
,
643 PCI_DMA_FROMDEVICE
, PAGE_SIZE
);
645 dev_err(&pdev
->dev
, "failed to alloc event dma ring buffers\n");
646 goto err_dma_event_ring_bufs_alloc
;
648 rocker_dma_ring_pass_to_producer(rocker
, &rocker
->event_ring
);
651 err_dma_event_ring_bufs_alloc
:
652 rocker_dma_ring_destroy(rocker
, &rocker
->event_ring
);
653 err_dma_event_ring_create
:
654 rocker_dma_ring_bufs_free(rocker
, &rocker
->cmd_ring
,
655 PCI_DMA_BIDIRECTIONAL
);
656 err_dma_cmd_ring_waits_alloc
:
657 rocker_dma_cmd_ring_waits_free(rocker
);
658 err_dma_cmd_ring_bufs_alloc
:
659 rocker_dma_ring_destroy(rocker
, &rocker
->cmd_ring
);
663 static void rocker_dma_rings_fini(struct rocker
*rocker
)
665 rocker_dma_ring_bufs_free(rocker
, &rocker
->event_ring
,
666 PCI_DMA_BIDIRECTIONAL
);
667 rocker_dma_ring_destroy(rocker
, &rocker
->event_ring
);
668 rocker_dma_cmd_ring_waits_free(rocker
);
669 rocker_dma_ring_bufs_free(rocker
, &rocker
->cmd_ring
,
670 PCI_DMA_BIDIRECTIONAL
);
671 rocker_dma_ring_destroy(rocker
, &rocker
->cmd_ring
);
674 static int rocker_dma_rx_ring_skb_map(const struct rocker_port
*rocker_port
,
675 struct rocker_desc_info
*desc_info
,
676 struct sk_buff
*skb
, size_t buf_len
)
678 const struct rocker
*rocker
= rocker_port
->rocker
;
679 struct pci_dev
*pdev
= rocker
->pdev
;
680 dma_addr_t dma_handle
;
682 dma_handle
= pci_map_single(pdev
, skb
->data
, buf_len
,
684 if (pci_dma_mapping_error(pdev
, dma_handle
))
686 if (rocker_tlv_put_u64(desc_info
, ROCKER_TLV_RX_FRAG_ADDR
, dma_handle
))
687 goto tlv_put_failure
;
688 if (rocker_tlv_put_u16(desc_info
, ROCKER_TLV_RX_FRAG_MAX_LEN
, buf_len
))
689 goto tlv_put_failure
;
693 pci_unmap_single(pdev
, dma_handle
, buf_len
, PCI_DMA_FROMDEVICE
);
694 desc_info
->tlv_size
= 0;
698 static size_t rocker_port_rx_buf_len(const struct rocker_port
*rocker_port
)
700 return rocker_port
->dev
->mtu
+ ETH_HLEN
+ ETH_FCS_LEN
+ VLAN_HLEN
;
703 static int rocker_dma_rx_ring_skb_alloc(const struct rocker_port
*rocker_port
,
704 struct rocker_desc_info
*desc_info
)
706 struct net_device
*dev
= rocker_port
->dev
;
708 size_t buf_len
= rocker_port_rx_buf_len(rocker_port
);
711 /* Ensure that hw will see tlv_size zero in case of an error.
712 * That tells hw to use another descriptor.
714 rocker_desc_cookie_ptr_set(desc_info
, NULL
);
715 desc_info
->tlv_size
= 0;
717 skb
= netdev_alloc_skb_ip_align(dev
, buf_len
);
720 err
= rocker_dma_rx_ring_skb_map(rocker_port
, desc_info
, skb
, buf_len
);
722 dev_kfree_skb_any(skb
);
725 rocker_desc_cookie_ptr_set(desc_info
, skb
);
729 static void rocker_dma_rx_ring_skb_unmap(const struct rocker
*rocker
,
730 const struct rocker_tlv
**attrs
)
732 struct pci_dev
*pdev
= rocker
->pdev
;
733 dma_addr_t dma_handle
;
736 if (!attrs
[ROCKER_TLV_RX_FRAG_ADDR
] ||
737 !attrs
[ROCKER_TLV_RX_FRAG_MAX_LEN
])
739 dma_handle
= rocker_tlv_get_u64(attrs
[ROCKER_TLV_RX_FRAG_ADDR
]);
740 len
= rocker_tlv_get_u16(attrs
[ROCKER_TLV_RX_FRAG_MAX_LEN
]);
741 pci_unmap_single(pdev
, dma_handle
, len
, PCI_DMA_FROMDEVICE
);
744 static void rocker_dma_rx_ring_skb_free(const struct rocker
*rocker
,
745 const struct rocker_desc_info
*desc_info
)
747 const struct rocker_tlv
*attrs
[ROCKER_TLV_RX_MAX
+ 1];
748 struct sk_buff
*skb
= rocker_desc_cookie_ptr_get(desc_info
);
752 rocker_tlv_parse_desc(attrs
, ROCKER_TLV_RX_MAX
, desc_info
);
753 rocker_dma_rx_ring_skb_unmap(rocker
, attrs
);
754 dev_kfree_skb_any(skb
);
757 static int rocker_dma_rx_ring_skbs_alloc(const struct rocker_port
*rocker_port
)
759 const struct rocker_dma_ring_info
*rx_ring
= &rocker_port
->rx_ring
;
760 const struct rocker
*rocker
= rocker_port
->rocker
;
764 for (i
= 0; i
< rx_ring
->size
; i
++) {
765 err
= rocker_dma_rx_ring_skb_alloc(rocker_port
,
766 &rx_ring
->desc_info
[i
]);
773 for (i
--; i
>= 0; i
--)
774 rocker_dma_rx_ring_skb_free(rocker
, &rx_ring
->desc_info
[i
]);
778 static void rocker_dma_rx_ring_skbs_free(const struct rocker_port
*rocker_port
)
780 const struct rocker_dma_ring_info
*rx_ring
= &rocker_port
->rx_ring
;
781 const struct rocker
*rocker
= rocker_port
->rocker
;
784 for (i
= 0; i
< rx_ring
->size
; i
++)
785 rocker_dma_rx_ring_skb_free(rocker
, &rx_ring
->desc_info
[i
]);
788 static int rocker_port_dma_rings_init(struct rocker_port
*rocker_port
)
790 struct rocker
*rocker
= rocker_port
->rocker
;
793 err
= rocker_dma_ring_create(rocker
,
794 ROCKER_DMA_TX(rocker_port
->port_number
),
795 ROCKER_DMA_TX_DEFAULT_SIZE
,
796 &rocker_port
->tx_ring
);
798 netdev_err(rocker_port
->dev
, "failed to create tx dma ring\n");
802 err
= rocker_dma_ring_bufs_alloc(rocker
, &rocker_port
->tx_ring
,
804 ROCKER_DMA_TX_DESC_SIZE
);
806 netdev_err(rocker_port
->dev
, "failed to alloc tx dma ring buffers\n");
807 goto err_dma_tx_ring_bufs_alloc
;
810 err
= rocker_dma_ring_create(rocker
,
811 ROCKER_DMA_RX(rocker_port
->port_number
),
812 ROCKER_DMA_RX_DEFAULT_SIZE
,
813 &rocker_port
->rx_ring
);
815 netdev_err(rocker_port
->dev
, "failed to create rx dma ring\n");
816 goto err_dma_rx_ring_create
;
819 err
= rocker_dma_ring_bufs_alloc(rocker
, &rocker_port
->rx_ring
,
820 PCI_DMA_BIDIRECTIONAL
,
821 ROCKER_DMA_RX_DESC_SIZE
);
823 netdev_err(rocker_port
->dev
, "failed to alloc rx dma ring buffers\n");
824 goto err_dma_rx_ring_bufs_alloc
;
827 err
= rocker_dma_rx_ring_skbs_alloc(rocker_port
);
829 netdev_err(rocker_port
->dev
, "failed to alloc rx dma ring skbs\n");
830 goto err_dma_rx_ring_skbs_alloc
;
832 rocker_dma_ring_pass_to_producer(rocker
, &rocker_port
->rx_ring
);
836 err_dma_rx_ring_skbs_alloc
:
837 rocker_dma_ring_bufs_free(rocker
, &rocker_port
->rx_ring
,
838 PCI_DMA_BIDIRECTIONAL
);
839 err_dma_rx_ring_bufs_alloc
:
840 rocker_dma_ring_destroy(rocker
, &rocker_port
->rx_ring
);
841 err_dma_rx_ring_create
:
842 rocker_dma_ring_bufs_free(rocker
, &rocker_port
->tx_ring
,
844 err_dma_tx_ring_bufs_alloc
:
845 rocker_dma_ring_destroy(rocker
, &rocker_port
->tx_ring
);
849 static void rocker_port_dma_rings_fini(struct rocker_port
*rocker_port
)
851 struct rocker
*rocker
= rocker_port
->rocker
;
853 rocker_dma_rx_ring_skbs_free(rocker_port
);
854 rocker_dma_ring_bufs_free(rocker
, &rocker_port
->rx_ring
,
855 PCI_DMA_BIDIRECTIONAL
);
856 rocker_dma_ring_destroy(rocker
, &rocker_port
->rx_ring
);
857 rocker_dma_ring_bufs_free(rocker
, &rocker_port
->tx_ring
,
859 rocker_dma_ring_destroy(rocker
, &rocker_port
->tx_ring
);
862 static void rocker_port_set_enable(const struct rocker_port
*rocker_port
,
865 u64 val
= rocker_read64(rocker_port
->rocker
, PORT_PHYS_ENABLE
);
868 val
|= 1ULL << rocker_port
->pport
;
870 val
&= ~(1ULL << rocker_port
->pport
);
871 rocker_write64(rocker_port
->rocker
, PORT_PHYS_ENABLE
, val
);
874 /********************************
875 * Interrupt handler and helpers
876 ********************************/
878 static irqreturn_t
rocker_cmd_irq_handler(int irq
, void *dev_id
)
880 struct rocker
*rocker
= dev_id
;
881 const struct rocker_desc_info
*desc_info
;
882 struct rocker_wait
*wait
;
885 spin_lock(&rocker
->cmd_ring_lock
);
886 while ((desc_info
= rocker_desc_tail_get(&rocker
->cmd_ring
))) {
887 wait
= rocker_desc_cookie_ptr_get(desc_info
);
889 rocker_desc_gen_clear(desc_info
);
891 rocker_wait_wake_up(wait
);
895 spin_unlock(&rocker
->cmd_ring_lock
);
896 rocker_dma_ring_credits_set(rocker
, &rocker
->cmd_ring
, credits
);
901 static void rocker_port_link_up(const struct rocker_port
*rocker_port
)
903 netif_carrier_on(rocker_port
->dev
);
904 netdev_info(rocker_port
->dev
, "Link is up\n");
907 static void rocker_port_link_down(const struct rocker_port
*rocker_port
)
909 netif_carrier_off(rocker_port
->dev
);
910 netdev_info(rocker_port
->dev
, "Link is down\n");
913 static int rocker_event_link_change(const struct rocker
*rocker
,
914 const struct rocker_tlv
*info
)
916 const struct rocker_tlv
*attrs
[ROCKER_TLV_EVENT_LINK_CHANGED_MAX
+ 1];
917 unsigned int port_number
;
919 struct rocker_port
*rocker_port
;
921 rocker_tlv_parse_nested(attrs
, ROCKER_TLV_EVENT_LINK_CHANGED_MAX
, info
);
922 if (!attrs
[ROCKER_TLV_EVENT_LINK_CHANGED_PPORT
] ||
923 !attrs
[ROCKER_TLV_EVENT_LINK_CHANGED_LINKUP
])
926 rocker_tlv_get_u32(attrs
[ROCKER_TLV_EVENT_LINK_CHANGED_PPORT
]) - 1;
927 link_up
= rocker_tlv_get_u8(attrs
[ROCKER_TLV_EVENT_LINK_CHANGED_LINKUP
]);
929 if (port_number
>= rocker
->port_count
)
932 rocker_port
= rocker
->ports
[port_number
];
933 if (netif_carrier_ok(rocker_port
->dev
) != link_up
) {
935 rocker_port_link_up(rocker_port
);
937 rocker_port_link_down(rocker_port
);
943 static int rocker_world_port_ev_mac_vlan_seen(struct rocker_port
*rocker_port
,
944 const unsigned char *addr
,
947 static int rocker_event_mac_vlan_seen(const struct rocker
*rocker
,
948 const struct rocker_tlv
*info
)
950 const struct rocker_tlv
*attrs
[ROCKER_TLV_EVENT_MAC_VLAN_MAX
+ 1];
951 unsigned int port_number
;
952 struct rocker_port
*rocker_port
;
953 const unsigned char *addr
;
956 rocker_tlv_parse_nested(attrs
, ROCKER_TLV_EVENT_MAC_VLAN_MAX
, info
);
957 if (!attrs
[ROCKER_TLV_EVENT_MAC_VLAN_PPORT
] ||
958 !attrs
[ROCKER_TLV_EVENT_MAC_VLAN_MAC
] ||
959 !attrs
[ROCKER_TLV_EVENT_MAC_VLAN_VLAN_ID
])
962 rocker_tlv_get_u32(attrs
[ROCKER_TLV_EVENT_MAC_VLAN_PPORT
]) - 1;
963 addr
= rocker_tlv_data(attrs
[ROCKER_TLV_EVENT_MAC_VLAN_MAC
]);
964 vlan_id
= rocker_tlv_get_be16(attrs
[ROCKER_TLV_EVENT_MAC_VLAN_VLAN_ID
]);
966 if (port_number
>= rocker
->port_count
)
969 rocker_port
= rocker
->ports
[port_number
];
970 return rocker_world_port_ev_mac_vlan_seen(rocker_port
, addr
, vlan_id
);
973 static int rocker_event_process(const struct rocker
*rocker
,
974 const struct rocker_desc_info
*desc_info
)
976 const struct rocker_tlv
*attrs
[ROCKER_TLV_EVENT_MAX
+ 1];
977 const struct rocker_tlv
*info
;
980 rocker_tlv_parse_desc(attrs
, ROCKER_TLV_EVENT_MAX
, desc_info
);
981 if (!attrs
[ROCKER_TLV_EVENT_TYPE
] ||
982 !attrs
[ROCKER_TLV_EVENT_INFO
])
985 type
= rocker_tlv_get_u16(attrs
[ROCKER_TLV_EVENT_TYPE
]);
986 info
= attrs
[ROCKER_TLV_EVENT_INFO
];
989 case ROCKER_TLV_EVENT_TYPE_LINK_CHANGED
:
990 return rocker_event_link_change(rocker
, info
);
991 case ROCKER_TLV_EVENT_TYPE_MAC_VLAN_SEEN
:
992 return rocker_event_mac_vlan_seen(rocker
, info
);
998 static irqreturn_t
rocker_event_irq_handler(int irq
, void *dev_id
)
1000 struct rocker
*rocker
= dev_id
;
1001 const struct pci_dev
*pdev
= rocker
->pdev
;
1002 const struct rocker_desc_info
*desc_info
;
1006 while ((desc_info
= rocker_desc_tail_get(&rocker
->event_ring
))) {
1007 err
= rocker_desc_err(desc_info
);
1009 dev_err(&pdev
->dev
, "event desc received with err %d\n",
1012 err
= rocker_event_process(rocker
, desc_info
);
1014 dev_err(&pdev
->dev
, "event processing failed with err %d\n",
1017 rocker_desc_gen_clear(desc_info
);
1018 rocker_desc_head_set(rocker
, &rocker
->event_ring
, desc_info
);
1021 rocker_dma_ring_credits_set(rocker
, &rocker
->event_ring
, credits
);
1026 static irqreturn_t
rocker_tx_irq_handler(int irq
, void *dev_id
)
1028 struct rocker_port
*rocker_port
= dev_id
;
1030 napi_schedule(&rocker_port
->napi_tx
);
1034 static irqreturn_t
rocker_rx_irq_handler(int irq
, void *dev_id
)
1036 struct rocker_port
*rocker_port
= dev_id
;
1038 napi_schedule(&rocker_port
->napi_rx
);
1042 /********************
1044 ********************/
1046 int rocker_cmd_exec(struct rocker_port
*rocker_port
, bool nowait
,
1047 rocker_cmd_prep_cb_t prepare
, void *prepare_priv
,
1048 rocker_cmd_proc_cb_t process
, void *process_priv
)
1050 struct rocker
*rocker
= rocker_port
->rocker
;
1051 struct rocker_desc_info
*desc_info
;
1052 struct rocker_wait
*wait
;
1053 unsigned long lock_flags
;
1056 spin_lock_irqsave(&rocker
->cmd_ring_lock
, lock_flags
);
1058 desc_info
= rocker_desc_head_get(&rocker
->cmd_ring
);
1060 spin_unlock_irqrestore(&rocker
->cmd_ring_lock
, lock_flags
);
1064 wait
= rocker_desc_cookie_ptr_get(desc_info
);
1065 rocker_wait_init(wait
);
1066 wait
->nowait
= nowait
;
1068 err
= prepare(rocker_port
, desc_info
, prepare_priv
);
1070 spin_unlock_irqrestore(&rocker
->cmd_ring_lock
, lock_flags
);
1074 rocker_desc_head_set(rocker
, &rocker
->cmd_ring
, desc_info
);
1076 spin_unlock_irqrestore(&rocker
->cmd_ring_lock
, lock_flags
);
1081 if (!rocker_wait_event_timeout(wait
, HZ
/ 10))
1084 err
= rocker_desc_err(desc_info
);
1089 err
= process(rocker_port
, desc_info
, process_priv
);
1091 rocker_desc_gen_clear(desc_info
);
1096 rocker_cmd_get_port_settings_prep(const struct rocker_port
*rocker_port
,
1097 struct rocker_desc_info
*desc_info
,
1100 struct rocker_tlv
*cmd_info
;
1102 if (rocker_tlv_put_u16(desc_info
, ROCKER_TLV_CMD_TYPE
,
1103 ROCKER_TLV_CMD_TYPE_GET_PORT_SETTINGS
))
1105 cmd_info
= rocker_tlv_nest_start(desc_info
, ROCKER_TLV_CMD_INFO
);
1108 if (rocker_tlv_put_u32(desc_info
, ROCKER_TLV_CMD_PORT_SETTINGS_PPORT
,
1109 rocker_port
->pport
))
1111 rocker_tlv_nest_end(desc_info
, cmd_info
);
1116 rocker_cmd_get_port_settings_ethtool_proc(const struct rocker_port
*rocker_port
,
1117 const struct rocker_desc_info
*desc_info
,
1120 struct ethtool_link_ksettings
*ecmd
= priv
;
1121 const struct rocker_tlv
*attrs
[ROCKER_TLV_CMD_MAX
+ 1];
1122 const struct rocker_tlv
*info_attrs
[ROCKER_TLV_CMD_PORT_SETTINGS_MAX
+ 1];
1127 rocker_tlv_parse_desc(attrs
, ROCKER_TLV_CMD_MAX
, desc_info
);
1128 if (!attrs
[ROCKER_TLV_CMD_INFO
])
1131 rocker_tlv_parse_nested(info_attrs
, ROCKER_TLV_CMD_PORT_SETTINGS_MAX
,
1132 attrs
[ROCKER_TLV_CMD_INFO
]);
1133 if (!info_attrs
[ROCKER_TLV_CMD_PORT_SETTINGS_SPEED
] ||
1134 !info_attrs
[ROCKER_TLV_CMD_PORT_SETTINGS_DUPLEX
] ||
1135 !info_attrs
[ROCKER_TLV_CMD_PORT_SETTINGS_AUTONEG
])
1138 speed
= rocker_tlv_get_u32(info_attrs
[ROCKER_TLV_CMD_PORT_SETTINGS_SPEED
]);
1139 duplex
= rocker_tlv_get_u8(info_attrs
[ROCKER_TLV_CMD_PORT_SETTINGS_DUPLEX
]);
1140 autoneg
= rocker_tlv_get_u8(info_attrs
[ROCKER_TLV_CMD_PORT_SETTINGS_AUTONEG
]);
1142 ethtool_link_ksettings_zero_link_mode(ecmd
, supported
);
1143 ethtool_link_ksettings_add_link_mode(ecmd
, supported
, TP
);
1145 ecmd
->base
.phy_address
= 0xff;
1146 ecmd
->base
.port
= PORT_TP
;
1147 ecmd
->base
.speed
= speed
;
1148 ecmd
->base
.duplex
= duplex
? DUPLEX_FULL
: DUPLEX_HALF
;
1149 ecmd
->base
.autoneg
= autoneg
? AUTONEG_ENABLE
: AUTONEG_DISABLE
;
1155 rocker_cmd_get_port_settings_macaddr_proc(const struct rocker_port
*rocker_port
,
1156 const struct rocker_desc_info
*desc_info
,
1159 unsigned char *macaddr
= priv
;
1160 const struct rocker_tlv
*attrs
[ROCKER_TLV_CMD_MAX
+ 1];
1161 const struct rocker_tlv
*info_attrs
[ROCKER_TLV_CMD_PORT_SETTINGS_MAX
+ 1];
1162 const struct rocker_tlv
*attr
;
1164 rocker_tlv_parse_desc(attrs
, ROCKER_TLV_CMD_MAX
, desc_info
);
1165 if (!attrs
[ROCKER_TLV_CMD_INFO
])
1168 rocker_tlv_parse_nested(info_attrs
, ROCKER_TLV_CMD_PORT_SETTINGS_MAX
,
1169 attrs
[ROCKER_TLV_CMD_INFO
]);
1170 attr
= info_attrs
[ROCKER_TLV_CMD_PORT_SETTINGS_MACADDR
];
1174 if (rocker_tlv_len(attr
) != ETH_ALEN
)
1177 ether_addr_copy(macaddr
, rocker_tlv_data(attr
));
1182 rocker_cmd_get_port_settings_mode_proc(const struct rocker_port
*rocker_port
,
1183 const struct rocker_desc_info
*desc_info
,
1187 const struct rocker_tlv
*attrs
[ROCKER_TLV_CMD_MAX
+ 1];
1188 const struct rocker_tlv
*info_attrs
[ROCKER_TLV_CMD_PORT_SETTINGS_MAX
+ 1];
1189 const struct rocker_tlv
*attr
;
1191 rocker_tlv_parse_desc(attrs
, ROCKER_TLV_CMD_MAX
, desc_info
);
1192 if (!attrs
[ROCKER_TLV_CMD_INFO
])
1195 rocker_tlv_parse_nested(info_attrs
, ROCKER_TLV_CMD_PORT_SETTINGS_MAX
,
1196 attrs
[ROCKER_TLV_CMD_INFO
]);
1197 attr
= info_attrs
[ROCKER_TLV_CMD_PORT_SETTINGS_MODE
];
1201 *p_mode
= rocker_tlv_get_u8(info_attrs
[ROCKER_TLV_CMD_PORT_SETTINGS_MODE
]);
1211 rocker_cmd_get_port_settings_phys_name_proc(const struct rocker_port
*rocker_port
,
1212 const struct rocker_desc_info
*desc_info
,
1215 const struct rocker_tlv
*info_attrs
[ROCKER_TLV_CMD_PORT_SETTINGS_MAX
+ 1];
1216 const struct rocker_tlv
*attrs
[ROCKER_TLV_CMD_MAX
+ 1];
1217 struct port_name
*name
= priv
;
1218 const struct rocker_tlv
*attr
;
1222 rocker_tlv_parse_desc(attrs
, ROCKER_TLV_CMD_MAX
, desc_info
);
1223 if (!attrs
[ROCKER_TLV_CMD_INFO
])
1226 rocker_tlv_parse_nested(info_attrs
, ROCKER_TLV_CMD_PORT_SETTINGS_MAX
,
1227 attrs
[ROCKER_TLV_CMD_INFO
]);
1228 attr
= info_attrs
[ROCKER_TLV_CMD_PORT_SETTINGS_PHYS_NAME
];
1232 len
= min_t(size_t, rocker_tlv_len(attr
), name
->len
);
1233 str
= rocker_tlv_data(attr
);
1235 /* make sure name only contains alphanumeric characters */
1236 for (i
= j
= 0; i
< len
; ++i
) {
1237 if (isalnum(str
[i
])) {
1238 name
->buf
[j
] = str
[i
];
1246 name
->buf
[j
] = '\0';
1252 rocker_cmd_set_port_settings_ethtool_prep(const struct rocker_port
*rocker_port
,
1253 struct rocker_desc_info
*desc_info
,
1256 struct ethtool_link_ksettings
*ecmd
= priv
;
1257 struct rocker_tlv
*cmd_info
;
1259 if (rocker_tlv_put_u16(desc_info
, ROCKER_TLV_CMD_TYPE
,
1260 ROCKER_TLV_CMD_TYPE_SET_PORT_SETTINGS
))
1262 cmd_info
= rocker_tlv_nest_start(desc_info
, ROCKER_TLV_CMD_INFO
);
1265 if (rocker_tlv_put_u32(desc_info
, ROCKER_TLV_CMD_PORT_SETTINGS_PPORT
,
1266 rocker_port
->pport
))
1268 if (rocker_tlv_put_u32(desc_info
, ROCKER_TLV_CMD_PORT_SETTINGS_SPEED
,
1271 if (rocker_tlv_put_u8(desc_info
, ROCKER_TLV_CMD_PORT_SETTINGS_DUPLEX
,
1274 if (rocker_tlv_put_u8(desc_info
, ROCKER_TLV_CMD_PORT_SETTINGS_AUTONEG
,
1275 ecmd
->base
.autoneg
))
1277 rocker_tlv_nest_end(desc_info
, cmd_info
);
1282 rocker_cmd_set_port_settings_macaddr_prep(const struct rocker_port
*rocker_port
,
1283 struct rocker_desc_info
*desc_info
,
1286 const unsigned char *macaddr
= priv
;
1287 struct rocker_tlv
*cmd_info
;
1289 if (rocker_tlv_put_u16(desc_info
, ROCKER_TLV_CMD_TYPE
,
1290 ROCKER_TLV_CMD_TYPE_SET_PORT_SETTINGS
))
1292 cmd_info
= rocker_tlv_nest_start(desc_info
, ROCKER_TLV_CMD_INFO
);
1295 if (rocker_tlv_put_u32(desc_info
, ROCKER_TLV_CMD_PORT_SETTINGS_PPORT
,
1296 rocker_port
->pport
))
1298 if (rocker_tlv_put(desc_info
, ROCKER_TLV_CMD_PORT_SETTINGS_MACADDR
,
1301 rocker_tlv_nest_end(desc_info
, cmd_info
);
1306 rocker_cmd_set_port_settings_mtu_prep(const struct rocker_port
*rocker_port
,
1307 struct rocker_desc_info
*desc_info
,
1310 int mtu
= *(int *)priv
;
1311 struct rocker_tlv
*cmd_info
;
1313 if (rocker_tlv_put_u16(desc_info
, ROCKER_TLV_CMD_TYPE
,
1314 ROCKER_TLV_CMD_TYPE_SET_PORT_SETTINGS
))
1316 cmd_info
= rocker_tlv_nest_start(desc_info
, ROCKER_TLV_CMD_INFO
);
1319 if (rocker_tlv_put_u32(desc_info
, ROCKER_TLV_CMD_PORT_SETTINGS_PPORT
,
1320 rocker_port
->pport
))
1322 if (rocker_tlv_put_u16(desc_info
, ROCKER_TLV_CMD_PORT_SETTINGS_MTU
,
1325 rocker_tlv_nest_end(desc_info
, cmd_info
);
1330 rocker_cmd_set_port_learning_prep(const struct rocker_port
*rocker_port
,
1331 struct rocker_desc_info
*desc_info
,
1334 bool learning
= *(bool *)priv
;
1335 struct rocker_tlv
*cmd_info
;
1337 if (rocker_tlv_put_u16(desc_info
, ROCKER_TLV_CMD_TYPE
,
1338 ROCKER_TLV_CMD_TYPE_SET_PORT_SETTINGS
))
1340 cmd_info
= rocker_tlv_nest_start(desc_info
, ROCKER_TLV_CMD_INFO
);
1343 if (rocker_tlv_put_u32(desc_info
, ROCKER_TLV_CMD_PORT_SETTINGS_PPORT
,
1344 rocker_port
->pport
))
1346 if (rocker_tlv_put_u8(desc_info
, ROCKER_TLV_CMD_PORT_SETTINGS_LEARNING
,
1349 rocker_tlv_nest_end(desc_info
, cmd_info
);
1354 rocker_cmd_get_port_settings_ethtool(struct rocker_port
*rocker_port
,
1355 struct ethtool_link_ksettings
*ecmd
)
1357 return rocker_cmd_exec(rocker_port
, false,
1358 rocker_cmd_get_port_settings_prep
, NULL
,
1359 rocker_cmd_get_port_settings_ethtool_proc
,
1363 static int rocker_cmd_get_port_settings_macaddr(struct rocker_port
*rocker_port
,
1364 unsigned char *macaddr
)
1366 return rocker_cmd_exec(rocker_port
, false,
1367 rocker_cmd_get_port_settings_prep
, NULL
,
1368 rocker_cmd_get_port_settings_macaddr_proc
,
1372 static int rocker_cmd_get_port_settings_mode(struct rocker_port
*rocker_port
,
1375 return rocker_cmd_exec(rocker_port
, false,
1376 rocker_cmd_get_port_settings_prep
, NULL
,
1377 rocker_cmd_get_port_settings_mode_proc
, p_mode
);
1381 rocker_cmd_set_port_settings_ethtool(struct rocker_port
*rocker_port
,
1382 const struct ethtool_link_ksettings
*ecmd
)
1384 struct ethtool_link_ksettings copy_ecmd
;
1386 memcpy(©_ecmd
, ecmd
, sizeof(copy_ecmd
));
1388 return rocker_cmd_exec(rocker_port
, false,
1389 rocker_cmd_set_port_settings_ethtool_prep
,
1390 ©_ecmd
, NULL
, NULL
);
1393 static int rocker_cmd_set_port_settings_macaddr(struct rocker_port
*rocker_port
,
1394 unsigned char *macaddr
)
1396 return rocker_cmd_exec(rocker_port
, false,
1397 rocker_cmd_set_port_settings_macaddr_prep
,
1398 macaddr
, NULL
, NULL
);
1401 static int rocker_cmd_set_port_settings_mtu(struct rocker_port
*rocker_port
,
1404 return rocker_cmd_exec(rocker_port
, false,
1405 rocker_cmd_set_port_settings_mtu_prep
,
1409 int rocker_port_set_learning(struct rocker_port
*rocker_port
,
1412 return rocker_cmd_exec(rocker_port
, false,
1413 rocker_cmd_set_port_learning_prep
,
1414 &learning
, NULL
, NULL
);
1417 /**********************
1418 * Worlds manipulation
1419 **********************/
1421 static struct rocker_world_ops
*rocker_world_ops
[] = {
1425 #define ROCKER_WORLD_OPS_LEN ARRAY_SIZE(rocker_world_ops)
1427 static struct rocker_world_ops
*rocker_world_ops_find(u8 mode
)
1431 for (i
= 0; i
< ROCKER_WORLD_OPS_LEN
; i
++)
1432 if (rocker_world_ops
[i
]->mode
== mode
)
1433 return rocker_world_ops
[i
];
1437 static int rocker_world_init(struct rocker
*rocker
, u8 mode
)
1439 struct rocker_world_ops
*wops
;
1442 wops
= rocker_world_ops_find(mode
);
1444 dev_err(&rocker
->pdev
->dev
, "port mode \"%d\" is not supported\n",
1448 rocker
->wops
= wops
;
1449 rocker
->wpriv
= kzalloc(wops
->priv_size
, GFP_KERNEL
);
1454 err
= wops
->init(rocker
);
1456 kfree(rocker
->wpriv
);
1460 static void rocker_world_fini(struct rocker
*rocker
)
1462 struct rocker_world_ops
*wops
= rocker
->wops
;
1464 if (!wops
|| !wops
->fini
)
1467 kfree(rocker
->wpriv
);
1470 static int rocker_world_check_init(struct rocker_port
*rocker_port
)
1472 struct rocker
*rocker
= rocker_port
->rocker
;
1476 err
= rocker_cmd_get_port_settings_mode(rocker_port
, &mode
);
1478 dev_err(&rocker
->pdev
->dev
, "failed to get port mode\n");
1482 if (rocker
->wops
->mode
!= mode
) {
1483 dev_err(&rocker
->pdev
->dev
, "hardware has ports in different worlds, which is not supported\n");
1488 return rocker_world_init(rocker
, mode
);
1491 static int rocker_world_port_pre_init(struct rocker_port
*rocker_port
)
1493 struct rocker_world_ops
*wops
= rocker_port
->rocker
->wops
;
1496 rocker_port
->wpriv
= kzalloc(wops
->port_priv_size
, GFP_KERNEL
);
1497 if (!rocker_port
->wpriv
)
1499 if (!wops
->port_pre_init
)
1501 err
= wops
->port_pre_init(rocker_port
);
1503 kfree(rocker_port
->wpriv
);
1507 static int rocker_world_port_init(struct rocker_port
*rocker_port
)
1509 struct rocker_world_ops
*wops
= rocker_port
->rocker
->wops
;
1511 if (!wops
->port_init
)
1513 return wops
->port_init(rocker_port
);
1516 static void rocker_world_port_fini(struct rocker_port
*rocker_port
)
1518 struct rocker_world_ops
*wops
= rocker_port
->rocker
->wops
;
1520 if (!wops
->port_fini
)
1522 wops
->port_fini(rocker_port
);
1525 static void rocker_world_port_post_fini(struct rocker_port
*rocker_port
)
1527 struct rocker_world_ops
*wops
= rocker_port
->rocker
->wops
;
1529 if (!wops
->port_post_fini
)
1531 wops
->port_post_fini(rocker_port
);
1532 kfree(rocker_port
->wpriv
);
1535 static int rocker_world_port_open(struct rocker_port
*rocker_port
)
1537 struct rocker_world_ops
*wops
= rocker_port
->rocker
->wops
;
1539 if (!wops
->port_open
)
1541 return wops
->port_open(rocker_port
);
1544 static void rocker_world_port_stop(struct rocker_port
*rocker_port
)
1546 struct rocker_world_ops
*wops
= rocker_port
->rocker
->wops
;
1548 if (!wops
->port_stop
)
1550 wops
->port_stop(rocker_port
);
1553 static int rocker_world_port_attr_stp_state_set(struct rocker_port
*rocker_port
,
1555 struct switchdev_trans
*trans
)
1557 struct rocker_world_ops
*wops
= rocker_port
->rocker
->wops
;
1559 if (!wops
->port_attr_stp_state_set
)
1562 if (switchdev_trans_ph_prepare(trans
))
1565 return wops
->port_attr_stp_state_set(rocker_port
, state
);
1569 rocker_world_port_attr_bridge_flags_set(struct rocker_port
*rocker_port
,
1570 unsigned long brport_flags
,
1571 struct switchdev_trans
*trans
)
1573 struct rocker_world_ops
*wops
= rocker_port
->rocker
->wops
;
1575 if (!wops
->port_attr_bridge_flags_set
)
1578 if (switchdev_trans_ph_prepare(trans
))
1581 return wops
->port_attr_bridge_flags_set(rocker_port
, brport_flags
,
1586 rocker_world_port_attr_bridge_flags_get(const struct rocker_port
*rocker_port
,
1587 unsigned long *p_brport_flags
)
1589 struct rocker_world_ops
*wops
= rocker_port
->rocker
->wops
;
1591 if (!wops
->port_attr_bridge_flags_get
)
1593 return wops
->port_attr_bridge_flags_get(rocker_port
, p_brport_flags
);
1597 rocker_world_port_attr_bridge_flags_support_get(const struct rocker_port
*
1600 p_brport_flags_support
)
1602 struct rocker_world_ops
*wops
= rocker_port
->rocker
->wops
;
1604 if (!wops
->port_attr_bridge_flags_support_get
)
1606 return wops
->port_attr_bridge_flags_support_get(rocker_port
,
1607 p_brport_flags_support
);
1611 rocker_world_port_attr_bridge_ageing_time_set(struct rocker_port
*rocker_port
,
1613 struct switchdev_trans
*trans
)
1616 struct rocker_world_ops
*wops
= rocker_port
->rocker
->wops
;
1618 if (!wops
->port_attr_bridge_ageing_time_set
)
1621 if (switchdev_trans_ph_prepare(trans
))
1624 return wops
->port_attr_bridge_ageing_time_set(rocker_port
, ageing_time
,
1629 rocker_world_port_obj_vlan_add(struct rocker_port
*rocker_port
,
1630 const struct switchdev_obj_port_vlan
*vlan
,
1631 struct switchdev_trans
*trans
)
1633 struct rocker_world_ops
*wops
= rocker_port
->rocker
->wops
;
1635 if (!wops
->port_obj_vlan_add
)
1638 if (switchdev_trans_ph_prepare(trans
))
1641 return wops
->port_obj_vlan_add(rocker_port
, vlan
);
1645 rocker_world_port_obj_vlan_del(struct rocker_port
*rocker_port
,
1646 const struct switchdev_obj_port_vlan
*vlan
)
1648 struct rocker_world_ops
*wops
= rocker_port
->rocker
->wops
;
1650 if (!wops
->port_obj_vlan_del
)
1652 return wops
->port_obj_vlan_del(rocker_port
, vlan
);
1656 rocker_world_port_fdb_add(struct rocker_port
*rocker_port
,
1657 struct switchdev_notifier_fdb_info
*info
)
1659 struct rocker_world_ops
*wops
= rocker_port
->rocker
->wops
;
1661 if (!wops
->port_obj_fdb_add
)
1664 return wops
->port_obj_fdb_add(rocker_port
, info
->vid
, info
->addr
);
1668 rocker_world_port_fdb_del(struct rocker_port
*rocker_port
,
1669 struct switchdev_notifier_fdb_info
*info
)
1671 struct rocker_world_ops
*wops
= rocker_port
->rocker
->wops
;
1673 if (!wops
->port_obj_fdb_del
)
1675 return wops
->port_obj_fdb_del(rocker_port
, info
->vid
, info
->addr
);
1678 static int rocker_world_port_master_linked(struct rocker_port
*rocker_port
,
1679 struct net_device
*master
)
1681 struct rocker_world_ops
*wops
= rocker_port
->rocker
->wops
;
1683 if (!wops
->port_master_linked
)
1685 return wops
->port_master_linked(rocker_port
, master
);
1688 static int rocker_world_port_master_unlinked(struct rocker_port
*rocker_port
,
1689 struct net_device
*master
)
1691 struct rocker_world_ops
*wops
= rocker_port
->rocker
->wops
;
1693 if (!wops
->port_master_unlinked
)
1695 return wops
->port_master_unlinked(rocker_port
, master
);
1698 static int rocker_world_port_neigh_update(struct rocker_port
*rocker_port
,
1699 struct neighbour
*n
)
1701 struct rocker_world_ops
*wops
= rocker_port
->rocker
->wops
;
1703 if (!wops
->port_neigh_update
)
1705 return wops
->port_neigh_update(rocker_port
, n
);
1708 static int rocker_world_port_neigh_destroy(struct rocker_port
*rocker_port
,
1709 struct neighbour
*n
)
1711 struct rocker_world_ops
*wops
= rocker_port
->rocker
->wops
;
1713 if (!wops
->port_neigh_destroy
)
1715 return wops
->port_neigh_destroy(rocker_port
, n
);
1718 static int rocker_world_port_ev_mac_vlan_seen(struct rocker_port
*rocker_port
,
1719 const unsigned char *addr
,
1722 struct rocker_world_ops
*wops
= rocker_port
->rocker
->wops
;
1724 if (!wops
->port_ev_mac_vlan_seen
)
1726 return wops
->port_ev_mac_vlan_seen(rocker_port
, addr
, vlan_id
);
1729 static int rocker_world_fib4_add(struct rocker
*rocker
,
1730 const struct fib_entry_notifier_info
*fen_info
)
1732 struct rocker_world_ops
*wops
= rocker
->wops
;
1734 if (!wops
->fib4_add
)
1736 return wops
->fib4_add(rocker
, fen_info
);
1739 static int rocker_world_fib4_del(struct rocker
*rocker
,
1740 const struct fib_entry_notifier_info
*fen_info
)
1742 struct rocker_world_ops
*wops
= rocker
->wops
;
1744 if (!wops
->fib4_del
)
1746 return wops
->fib4_del(rocker
, fen_info
);
1749 static void rocker_world_fib4_abort(struct rocker
*rocker
)
1751 struct rocker_world_ops
*wops
= rocker
->wops
;
1753 if (wops
->fib4_abort
)
1754 wops
->fib4_abort(rocker
);
1761 static int rocker_port_open(struct net_device
*dev
)
1763 struct rocker_port
*rocker_port
= netdev_priv(dev
);
1766 err
= rocker_port_dma_rings_init(rocker_port
);
1770 err
= request_irq(rocker_msix_tx_vector(rocker_port
),
1771 rocker_tx_irq_handler
, 0,
1772 rocker_driver_name
, rocker_port
);
1774 netdev_err(rocker_port
->dev
, "cannot assign tx irq\n");
1775 goto err_request_tx_irq
;
1778 err
= request_irq(rocker_msix_rx_vector(rocker_port
),
1779 rocker_rx_irq_handler
, 0,
1780 rocker_driver_name
, rocker_port
);
1782 netdev_err(rocker_port
->dev
, "cannot assign rx irq\n");
1783 goto err_request_rx_irq
;
1786 err
= rocker_world_port_open(rocker_port
);
1788 netdev_err(rocker_port
->dev
, "cannot open port in world\n");
1789 goto err_world_port_open
;
1792 napi_enable(&rocker_port
->napi_tx
);
1793 napi_enable(&rocker_port
->napi_rx
);
1794 if (!dev
->proto_down
)
1795 rocker_port_set_enable(rocker_port
, true);
1796 netif_start_queue(dev
);
1799 err_world_port_open
:
1800 free_irq(rocker_msix_rx_vector(rocker_port
), rocker_port
);
1802 free_irq(rocker_msix_tx_vector(rocker_port
), rocker_port
);
1804 rocker_port_dma_rings_fini(rocker_port
);
1808 static int rocker_port_stop(struct net_device
*dev
)
1810 struct rocker_port
*rocker_port
= netdev_priv(dev
);
1812 netif_stop_queue(dev
);
1813 rocker_port_set_enable(rocker_port
, false);
1814 napi_disable(&rocker_port
->napi_rx
);
1815 napi_disable(&rocker_port
->napi_tx
);
1816 rocker_world_port_stop(rocker_port
);
1817 free_irq(rocker_msix_rx_vector(rocker_port
), rocker_port
);
1818 free_irq(rocker_msix_tx_vector(rocker_port
), rocker_port
);
1819 rocker_port_dma_rings_fini(rocker_port
);
1824 static void rocker_tx_desc_frags_unmap(const struct rocker_port
*rocker_port
,
1825 const struct rocker_desc_info
*desc_info
)
1827 const struct rocker
*rocker
= rocker_port
->rocker
;
1828 struct pci_dev
*pdev
= rocker
->pdev
;
1829 const struct rocker_tlv
*attrs
[ROCKER_TLV_TX_MAX
+ 1];
1830 struct rocker_tlv
*attr
;
1833 rocker_tlv_parse_desc(attrs
, ROCKER_TLV_TX_MAX
, desc_info
);
1834 if (!attrs
[ROCKER_TLV_TX_FRAGS
])
1836 rocker_tlv_for_each_nested(attr
, attrs
[ROCKER_TLV_TX_FRAGS
], rem
) {
1837 const struct rocker_tlv
*frag_attrs
[ROCKER_TLV_TX_FRAG_ATTR_MAX
+ 1];
1838 dma_addr_t dma_handle
;
1841 if (rocker_tlv_type(attr
) != ROCKER_TLV_TX_FRAG
)
1843 rocker_tlv_parse_nested(frag_attrs
, ROCKER_TLV_TX_FRAG_ATTR_MAX
,
1845 if (!frag_attrs
[ROCKER_TLV_TX_FRAG_ATTR_ADDR
] ||
1846 !frag_attrs
[ROCKER_TLV_TX_FRAG_ATTR_LEN
])
1848 dma_handle
= rocker_tlv_get_u64(frag_attrs
[ROCKER_TLV_TX_FRAG_ATTR_ADDR
]);
1849 len
= rocker_tlv_get_u16(frag_attrs
[ROCKER_TLV_TX_FRAG_ATTR_LEN
]);
1850 pci_unmap_single(pdev
, dma_handle
, len
, DMA_TO_DEVICE
);
1854 static int rocker_tx_desc_frag_map_put(const struct rocker_port
*rocker_port
,
1855 struct rocker_desc_info
*desc_info
,
1856 char *buf
, size_t buf_len
)
1858 const struct rocker
*rocker
= rocker_port
->rocker
;
1859 struct pci_dev
*pdev
= rocker
->pdev
;
1860 dma_addr_t dma_handle
;
1861 struct rocker_tlv
*frag
;
1863 dma_handle
= pci_map_single(pdev
, buf
, buf_len
, DMA_TO_DEVICE
);
1864 if (unlikely(pci_dma_mapping_error(pdev
, dma_handle
))) {
1865 if (net_ratelimit())
1866 netdev_err(rocker_port
->dev
, "failed to dma map tx frag\n");
1869 frag
= rocker_tlv_nest_start(desc_info
, ROCKER_TLV_TX_FRAG
);
1872 if (rocker_tlv_put_u64(desc_info
, ROCKER_TLV_TX_FRAG_ATTR_ADDR
,
1875 if (rocker_tlv_put_u16(desc_info
, ROCKER_TLV_TX_FRAG_ATTR_LEN
,
1878 rocker_tlv_nest_end(desc_info
, frag
);
1882 rocker_tlv_nest_cancel(desc_info
, frag
);
1884 pci_unmap_single(pdev
, dma_handle
, buf_len
, DMA_TO_DEVICE
);
1888 static netdev_tx_t
rocker_port_xmit(struct sk_buff
*skb
, struct net_device
*dev
)
1890 struct rocker_port
*rocker_port
= netdev_priv(dev
);
1891 struct rocker
*rocker
= rocker_port
->rocker
;
1892 struct rocker_desc_info
*desc_info
;
1893 struct rocker_tlv
*frags
;
1897 desc_info
= rocker_desc_head_get(&rocker_port
->tx_ring
);
1898 if (unlikely(!desc_info
)) {
1899 if (net_ratelimit())
1900 netdev_err(dev
, "tx ring full when queue awake\n");
1901 return NETDEV_TX_BUSY
;
1904 rocker_desc_cookie_ptr_set(desc_info
, skb
);
1906 frags
= rocker_tlv_nest_start(desc_info
, ROCKER_TLV_TX_FRAGS
);
1909 err
= rocker_tx_desc_frag_map_put(rocker_port
, desc_info
,
1910 skb
->data
, skb_headlen(skb
));
1913 if (skb_shinfo(skb
)->nr_frags
> ROCKER_TX_FRAGS_MAX
) {
1914 err
= skb_linearize(skb
);
1919 for (i
= 0; i
< skb_shinfo(skb
)->nr_frags
; i
++) {
1920 const skb_frag_t
*frag
= &skb_shinfo(skb
)->frags
[i
];
1922 err
= rocker_tx_desc_frag_map_put(rocker_port
, desc_info
,
1923 skb_frag_address(frag
),
1924 skb_frag_size(frag
));
1928 rocker_tlv_nest_end(desc_info
, frags
);
1930 rocker_desc_gen_clear(desc_info
);
1931 rocker_desc_head_set(rocker
, &rocker_port
->tx_ring
, desc_info
);
1933 desc_info
= rocker_desc_head_get(&rocker_port
->tx_ring
);
1935 netif_stop_queue(dev
);
1937 return NETDEV_TX_OK
;
1940 rocker_tx_desc_frags_unmap(rocker_port
, desc_info
);
1942 rocker_tlv_nest_cancel(desc_info
, frags
);
1945 dev
->stats
.tx_dropped
++;
1947 return NETDEV_TX_OK
;
1950 static int rocker_port_set_mac_address(struct net_device
*dev
, void *p
)
1952 struct sockaddr
*addr
= p
;
1953 struct rocker_port
*rocker_port
= netdev_priv(dev
);
1956 if (!is_valid_ether_addr(addr
->sa_data
))
1957 return -EADDRNOTAVAIL
;
1959 err
= rocker_cmd_set_port_settings_macaddr(rocker_port
, addr
->sa_data
);
1962 memcpy(dev
->dev_addr
, addr
->sa_data
, dev
->addr_len
);
1966 static int rocker_port_change_mtu(struct net_device
*dev
, int new_mtu
)
1968 struct rocker_port
*rocker_port
= netdev_priv(dev
);
1969 int running
= netif_running(dev
);
1973 rocker_port_stop(dev
);
1975 netdev_info(dev
, "MTU change from %d to %d\n", dev
->mtu
, new_mtu
);
1978 err
= rocker_cmd_set_port_settings_mtu(rocker_port
, new_mtu
);
1983 err
= rocker_port_open(dev
);
1988 static int rocker_port_get_phys_port_name(struct net_device
*dev
,
1989 char *buf
, size_t len
)
1991 struct rocker_port
*rocker_port
= netdev_priv(dev
);
1992 struct port_name name
= { .buf
= buf
, .len
= len
};
1995 err
= rocker_cmd_exec(rocker_port
, false,
1996 rocker_cmd_get_port_settings_prep
, NULL
,
1997 rocker_cmd_get_port_settings_phys_name_proc
,
2000 return err
? -EOPNOTSUPP
: 0;
2003 static int rocker_port_change_proto_down(struct net_device
*dev
,
2006 struct rocker_port
*rocker_port
= netdev_priv(dev
);
2008 if (rocker_port
->dev
->flags
& IFF_UP
)
2009 rocker_port_set_enable(rocker_port
, !proto_down
);
2010 rocker_port
->dev
->proto_down
= proto_down
;
2014 static void rocker_port_neigh_destroy(struct net_device
*dev
,
2015 struct neighbour
*n
)
2017 struct rocker_port
*rocker_port
= netdev_priv(n
->dev
);
2020 err
= rocker_world_port_neigh_destroy(rocker_port
, n
);
2022 netdev_warn(rocker_port
->dev
, "failed to handle neigh destroy (err %d)\n",
2026 static const struct net_device_ops rocker_port_netdev_ops
= {
2027 .ndo_open
= rocker_port_open
,
2028 .ndo_stop
= rocker_port_stop
,
2029 .ndo_start_xmit
= rocker_port_xmit
,
2030 .ndo_set_mac_address
= rocker_port_set_mac_address
,
2031 .ndo_change_mtu
= rocker_port_change_mtu
,
2032 .ndo_get_phys_port_name
= rocker_port_get_phys_port_name
,
2033 .ndo_change_proto_down
= rocker_port_change_proto_down
,
2034 .ndo_neigh_destroy
= rocker_port_neigh_destroy
,
2037 /********************
2039 ********************/
2041 static int rocker_port_attr_get(struct net_device
*dev
,
2042 struct switchdev_attr
*attr
)
2044 const struct rocker_port
*rocker_port
= netdev_priv(dev
);
2045 const struct rocker
*rocker
= rocker_port
->rocker
;
2049 case SWITCHDEV_ATTR_ID_PORT_PARENT_ID
:
2050 attr
->u
.ppid
.id_len
= sizeof(rocker
->hw
.id
);
2051 memcpy(&attr
->u
.ppid
.id
, &rocker
->hw
.id
, attr
->u
.ppid
.id_len
);
2053 case SWITCHDEV_ATTR_ID_PORT_BRIDGE_FLAGS
:
2054 err
= rocker_world_port_attr_bridge_flags_get(rocker_port
,
2055 &attr
->u
.brport_flags
);
2057 case SWITCHDEV_ATTR_ID_PORT_BRIDGE_FLAGS_SUPPORT
:
2058 err
= rocker_world_port_attr_bridge_flags_support_get(rocker_port
,
2059 &attr
->u
.brport_flags_support
);
2068 static int rocker_port_attr_set(struct net_device
*dev
,
2069 const struct switchdev_attr
*attr
,
2070 struct switchdev_trans
*trans
)
2072 struct rocker_port
*rocker_port
= netdev_priv(dev
);
2076 case SWITCHDEV_ATTR_ID_PORT_STP_STATE
:
2077 err
= rocker_world_port_attr_stp_state_set(rocker_port
,
2081 case SWITCHDEV_ATTR_ID_PORT_BRIDGE_FLAGS
:
2082 err
= rocker_world_port_attr_bridge_flags_set(rocker_port
,
2083 attr
->u
.brport_flags
,
2086 case SWITCHDEV_ATTR_ID_BRIDGE_AGEING_TIME
:
2087 err
= rocker_world_port_attr_bridge_ageing_time_set(rocker_port
,
2088 attr
->u
.ageing_time
,
2099 static int rocker_port_obj_add(struct net_device
*dev
,
2100 const struct switchdev_obj
*obj
,
2101 struct switchdev_trans
*trans
)
2103 struct rocker_port
*rocker_port
= netdev_priv(dev
);
2107 case SWITCHDEV_OBJ_ID_PORT_VLAN
:
2108 err
= rocker_world_port_obj_vlan_add(rocker_port
,
2109 SWITCHDEV_OBJ_PORT_VLAN(obj
),
2120 static int rocker_port_obj_del(struct net_device
*dev
,
2121 const struct switchdev_obj
*obj
)
2123 struct rocker_port
*rocker_port
= netdev_priv(dev
);
2127 case SWITCHDEV_OBJ_ID_PORT_VLAN
:
2128 err
= rocker_world_port_obj_vlan_del(rocker_port
,
2129 SWITCHDEV_OBJ_PORT_VLAN(obj
));
2139 static const struct switchdev_ops rocker_port_switchdev_ops
= {
2140 .switchdev_port_attr_get
= rocker_port_attr_get
,
2141 .switchdev_port_attr_set
= rocker_port_attr_set
,
2142 .switchdev_port_obj_add
= rocker_port_obj_add
,
2143 .switchdev_port_obj_del
= rocker_port_obj_del
,
2146 struct rocker_fib_event_work
{
2147 struct work_struct work
;
2149 struct fib_entry_notifier_info fen_info
;
2150 struct fib_rule_notifier_info fr_info
;
2152 struct rocker
*rocker
;
2153 unsigned long event
;
2156 static void rocker_router_fib_event_work(struct work_struct
*work
)
2158 struct rocker_fib_event_work
*fib_work
=
2159 container_of(work
, struct rocker_fib_event_work
, work
);
2160 struct rocker
*rocker
= fib_work
->rocker
;
2161 struct fib_rule
*rule
;
2164 /* Protect internal structures from changes */
2166 switch (fib_work
->event
) {
2167 case FIB_EVENT_ENTRY_ADD
:
2168 err
= rocker_world_fib4_add(rocker
, &fib_work
->fen_info
);
2170 rocker_world_fib4_abort(rocker
);
2171 fib_info_put(fib_work
->fen_info
.fi
);
2173 case FIB_EVENT_ENTRY_DEL
:
2174 rocker_world_fib4_del(rocker
, &fib_work
->fen_info
);
2175 fib_info_put(fib_work
->fen_info
.fi
);
2177 case FIB_EVENT_RULE_ADD
: /* fall through */
2178 case FIB_EVENT_RULE_DEL
:
2179 rule
= fib_work
->fr_info
.rule
;
2180 if (!fib4_rule_default(rule
))
2181 rocker_world_fib4_abort(rocker
);
2189 /* Called with rcu_read_lock() */
2190 static int rocker_router_fib_event(struct notifier_block
*nb
,
2191 unsigned long event
, void *ptr
)
2193 struct rocker
*rocker
= container_of(nb
, struct rocker
, fib_nb
);
2194 struct rocker_fib_event_work
*fib_work
;
2195 struct fib_notifier_info
*info
= ptr
;
2197 if (info
->family
!= AF_INET
)
2200 fib_work
= kzalloc(sizeof(*fib_work
), GFP_ATOMIC
);
2201 if (WARN_ON(!fib_work
))
2204 INIT_WORK(&fib_work
->work
, rocker_router_fib_event_work
);
2205 fib_work
->rocker
= rocker
;
2206 fib_work
->event
= event
;
2209 case FIB_EVENT_ENTRY_ADD
: /* fall through */
2210 case FIB_EVENT_ENTRY_DEL
:
2211 memcpy(&fib_work
->fen_info
, ptr
, sizeof(fib_work
->fen_info
));
2212 /* Take referece on fib_info to prevent it from being
2213 * freed while work is queued. Release it afterwards.
2215 fib_info_hold(fib_work
->fen_info
.fi
);
2217 case FIB_EVENT_RULE_ADD
: /* fall through */
2218 case FIB_EVENT_RULE_DEL
:
2219 memcpy(&fib_work
->fr_info
, ptr
, sizeof(fib_work
->fr_info
));
2220 fib_rule_get(fib_work
->fr_info
.rule
);
2224 queue_work(rocker
->rocker_owq
, &fib_work
->work
);
2229 /********************
2231 ********************/
2234 rocker_port_get_link_ksettings(struct net_device
*dev
,
2235 struct ethtool_link_ksettings
*ecmd
)
2237 struct rocker_port
*rocker_port
= netdev_priv(dev
);
2239 return rocker_cmd_get_port_settings_ethtool(rocker_port
, ecmd
);
2243 rocker_port_set_link_ksettings(struct net_device
*dev
,
2244 const struct ethtool_link_ksettings
*ecmd
)
2246 struct rocker_port
*rocker_port
= netdev_priv(dev
);
2248 return rocker_cmd_set_port_settings_ethtool(rocker_port
, ecmd
);
2251 static void rocker_port_get_drvinfo(struct net_device
*dev
,
2252 struct ethtool_drvinfo
*drvinfo
)
2254 strlcpy(drvinfo
->driver
, rocker_driver_name
, sizeof(drvinfo
->driver
));
2255 strlcpy(drvinfo
->version
, UTS_RELEASE
, sizeof(drvinfo
->version
));
2258 static struct rocker_port_stats
{
2259 char str
[ETH_GSTRING_LEN
];
2261 } rocker_port_stats
[] = {
2262 { "rx_packets", ROCKER_TLV_CMD_PORT_STATS_RX_PKTS
, },
2263 { "rx_bytes", ROCKER_TLV_CMD_PORT_STATS_RX_BYTES
, },
2264 { "rx_dropped", ROCKER_TLV_CMD_PORT_STATS_RX_DROPPED
, },
2265 { "rx_errors", ROCKER_TLV_CMD_PORT_STATS_RX_ERRORS
, },
2267 { "tx_packets", ROCKER_TLV_CMD_PORT_STATS_TX_PKTS
, },
2268 { "tx_bytes", ROCKER_TLV_CMD_PORT_STATS_TX_BYTES
, },
2269 { "tx_dropped", ROCKER_TLV_CMD_PORT_STATS_TX_DROPPED
, },
2270 { "tx_errors", ROCKER_TLV_CMD_PORT_STATS_TX_ERRORS
, },
2273 #define ROCKER_PORT_STATS_LEN ARRAY_SIZE(rocker_port_stats)
2275 static void rocker_port_get_strings(struct net_device
*netdev
, u32 stringset
,
2281 switch (stringset
) {
2283 for (i
= 0; i
< ARRAY_SIZE(rocker_port_stats
); i
++) {
2284 memcpy(p
, rocker_port_stats
[i
].str
, ETH_GSTRING_LEN
);
2285 p
+= ETH_GSTRING_LEN
;
2292 rocker_cmd_get_port_stats_prep(const struct rocker_port
*rocker_port
,
2293 struct rocker_desc_info
*desc_info
,
2296 struct rocker_tlv
*cmd_stats
;
2298 if (rocker_tlv_put_u16(desc_info
, ROCKER_TLV_CMD_TYPE
,
2299 ROCKER_TLV_CMD_TYPE_GET_PORT_STATS
))
2302 cmd_stats
= rocker_tlv_nest_start(desc_info
, ROCKER_TLV_CMD_INFO
);
2306 if (rocker_tlv_put_u32(desc_info
, ROCKER_TLV_CMD_PORT_STATS_PPORT
,
2307 rocker_port
->pport
))
2310 rocker_tlv_nest_end(desc_info
, cmd_stats
);
2316 rocker_cmd_get_port_stats_ethtool_proc(const struct rocker_port
*rocker_port
,
2317 const struct rocker_desc_info
*desc_info
,
2320 const struct rocker_tlv
*attrs
[ROCKER_TLV_CMD_MAX
+ 1];
2321 const struct rocker_tlv
*stats_attrs
[ROCKER_TLV_CMD_PORT_STATS_MAX
+ 1];
2322 const struct rocker_tlv
*pattr
;
2327 rocker_tlv_parse_desc(attrs
, ROCKER_TLV_CMD_MAX
, desc_info
);
2329 if (!attrs
[ROCKER_TLV_CMD_INFO
])
2332 rocker_tlv_parse_nested(stats_attrs
, ROCKER_TLV_CMD_PORT_STATS_MAX
,
2333 attrs
[ROCKER_TLV_CMD_INFO
]);
2335 if (!stats_attrs
[ROCKER_TLV_CMD_PORT_STATS_PPORT
])
2338 pport
= rocker_tlv_get_u32(stats_attrs
[ROCKER_TLV_CMD_PORT_STATS_PPORT
]);
2339 if (pport
!= rocker_port
->pport
)
2342 for (i
= 0; i
< ARRAY_SIZE(rocker_port_stats
); i
++) {
2343 pattr
= stats_attrs
[rocker_port_stats
[i
].type
];
2347 data
[i
] = rocker_tlv_get_u64(pattr
);
2353 static int rocker_cmd_get_port_stats_ethtool(struct rocker_port
*rocker_port
,
2356 return rocker_cmd_exec(rocker_port
, false,
2357 rocker_cmd_get_port_stats_prep
, NULL
,
2358 rocker_cmd_get_port_stats_ethtool_proc
,
2362 static void rocker_port_get_stats(struct net_device
*dev
,
2363 struct ethtool_stats
*stats
, u64
*data
)
2365 struct rocker_port
*rocker_port
= netdev_priv(dev
);
2367 if (rocker_cmd_get_port_stats_ethtool(rocker_port
, data
) != 0) {
2370 for (i
= 0; i
< ARRAY_SIZE(rocker_port_stats
); ++i
)
2375 static int rocker_port_get_sset_count(struct net_device
*netdev
, int sset
)
2379 return ROCKER_PORT_STATS_LEN
;
2385 static const struct ethtool_ops rocker_port_ethtool_ops
= {
2386 .get_drvinfo
= rocker_port_get_drvinfo
,
2387 .get_link
= ethtool_op_get_link
,
2388 .get_strings
= rocker_port_get_strings
,
2389 .get_ethtool_stats
= rocker_port_get_stats
,
2390 .get_sset_count
= rocker_port_get_sset_count
,
2391 .get_link_ksettings
= rocker_port_get_link_ksettings
,
2392 .set_link_ksettings
= rocker_port_set_link_ksettings
,
2399 static struct rocker_port
*rocker_port_napi_tx_get(struct napi_struct
*napi
)
2401 return container_of(napi
, struct rocker_port
, napi_tx
);
2404 static int rocker_port_poll_tx(struct napi_struct
*napi
, int budget
)
2406 struct rocker_port
*rocker_port
= rocker_port_napi_tx_get(napi
);
2407 const struct rocker
*rocker
= rocker_port
->rocker
;
2408 const struct rocker_desc_info
*desc_info
;
2412 /* Cleanup tx descriptors */
2413 while ((desc_info
= rocker_desc_tail_get(&rocker_port
->tx_ring
))) {
2414 struct sk_buff
*skb
;
2416 err
= rocker_desc_err(desc_info
);
2417 if (err
&& net_ratelimit())
2418 netdev_err(rocker_port
->dev
, "tx desc received with err %d\n",
2420 rocker_tx_desc_frags_unmap(rocker_port
, desc_info
);
2422 skb
= rocker_desc_cookie_ptr_get(desc_info
);
2424 rocker_port
->dev
->stats
.tx_packets
++;
2425 rocker_port
->dev
->stats
.tx_bytes
+= skb
->len
;
2427 rocker_port
->dev
->stats
.tx_errors
++;
2430 dev_kfree_skb_any(skb
);
2434 if (credits
&& netif_queue_stopped(rocker_port
->dev
))
2435 netif_wake_queue(rocker_port
->dev
);
2437 napi_complete(napi
);
2438 rocker_dma_ring_credits_set(rocker
, &rocker_port
->tx_ring
, credits
);
2443 static int rocker_port_rx_proc(const struct rocker
*rocker
,
2444 const struct rocker_port
*rocker_port
,
2445 struct rocker_desc_info
*desc_info
)
2447 const struct rocker_tlv
*attrs
[ROCKER_TLV_RX_MAX
+ 1];
2448 struct sk_buff
*skb
= rocker_desc_cookie_ptr_get(desc_info
);
2455 rocker_tlv_parse_desc(attrs
, ROCKER_TLV_RX_MAX
, desc_info
);
2456 if (!attrs
[ROCKER_TLV_RX_FRAG_LEN
])
2458 if (attrs
[ROCKER_TLV_RX_FLAGS
])
2459 rx_flags
= rocker_tlv_get_u16(attrs
[ROCKER_TLV_RX_FLAGS
]);
2461 rocker_dma_rx_ring_skb_unmap(rocker
, attrs
);
2463 rx_len
= rocker_tlv_get_u16(attrs
[ROCKER_TLV_RX_FRAG_LEN
]);
2464 skb_put(skb
, rx_len
);
2465 skb
->protocol
= eth_type_trans(skb
, rocker_port
->dev
);
2467 if (rx_flags
& ROCKER_RX_FLAGS_FWD_OFFLOAD
)
2468 skb
->offload_fwd_mark
= 1;
2470 rocker_port
->dev
->stats
.rx_packets
++;
2471 rocker_port
->dev
->stats
.rx_bytes
+= skb
->len
;
2473 netif_receive_skb(skb
);
2475 return rocker_dma_rx_ring_skb_alloc(rocker_port
, desc_info
);
2478 static struct rocker_port
*rocker_port_napi_rx_get(struct napi_struct
*napi
)
2480 return container_of(napi
, struct rocker_port
, napi_rx
);
2483 static int rocker_port_poll_rx(struct napi_struct
*napi
, int budget
)
2485 struct rocker_port
*rocker_port
= rocker_port_napi_rx_get(napi
);
2486 const struct rocker
*rocker
= rocker_port
->rocker
;
2487 struct rocker_desc_info
*desc_info
;
2491 /* Process rx descriptors */
2492 while (credits
< budget
&&
2493 (desc_info
= rocker_desc_tail_get(&rocker_port
->rx_ring
))) {
2494 err
= rocker_desc_err(desc_info
);
2496 if (net_ratelimit())
2497 netdev_err(rocker_port
->dev
, "rx desc received with err %d\n",
2500 err
= rocker_port_rx_proc(rocker
, rocker_port
,
2502 if (err
&& net_ratelimit())
2503 netdev_err(rocker_port
->dev
, "rx processing failed with err %d\n",
2507 rocker_port
->dev
->stats
.rx_errors
++;
2509 rocker_desc_gen_clear(desc_info
);
2510 rocker_desc_head_set(rocker
, &rocker_port
->rx_ring
, desc_info
);
2514 if (credits
< budget
)
2515 napi_complete_done(napi
, credits
);
2517 rocker_dma_ring_credits_set(rocker
, &rocker_port
->rx_ring
, credits
);
2526 static void rocker_carrier_init(const struct rocker_port
*rocker_port
)
2528 const struct rocker
*rocker
= rocker_port
->rocker
;
2529 u64 link_status
= rocker_read64(rocker
, PORT_PHYS_LINK_STATUS
);
2532 link_up
= link_status
& (1 << rocker_port
->pport
);
2534 netif_carrier_on(rocker_port
->dev
);
2536 netif_carrier_off(rocker_port
->dev
);
2539 static void rocker_remove_ports(struct rocker
*rocker
)
2541 struct rocker_port
*rocker_port
;
2544 for (i
= 0; i
< rocker
->port_count
; i
++) {
2545 rocker_port
= rocker
->ports
[i
];
2548 rocker_world_port_fini(rocker_port
);
2549 unregister_netdev(rocker_port
->dev
);
2550 rocker_world_port_post_fini(rocker_port
);
2551 free_netdev(rocker_port
->dev
);
2553 rocker_world_fini(rocker
);
2554 kfree(rocker
->ports
);
2557 static void rocker_port_dev_addr_init(struct rocker_port
*rocker_port
)
2559 const struct rocker
*rocker
= rocker_port
->rocker
;
2560 const struct pci_dev
*pdev
= rocker
->pdev
;
2563 err
= rocker_cmd_get_port_settings_macaddr(rocker_port
,
2564 rocker_port
->dev
->dev_addr
);
2566 dev_warn(&pdev
->dev
, "failed to get mac address, using random\n");
2567 eth_hw_addr_random(rocker_port
->dev
);
2571 #define ROCKER_PORT_MIN_MTU ETH_MIN_MTU
2572 #define ROCKER_PORT_MAX_MTU 9000
2573 static int rocker_probe_port(struct rocker
*rocker
, unsigned int port_number
)
2575 struct pci_dev
*pdev
= rocker
->pdev
;
2576 struct rocker_port
*rocker_port
;
2577 struct net_device
*dev
;
2580 dev
= alloc_etherdev(sizeof(struct rocker_port
));
2583 SET_NETDEV_DEV(dev
, &pdev
->dev
);
2584 rocker_port
= netdev_priv(dev
);
2585 rocker_port
->dev
= dev
;
2586 rocker_port
->rocker
= rocker
;
2587 rocker_port
->port_number
= port_number
;
2588 rocker_port
->pport
= port_number
+ 1;
2590 err
= rocker_world_check_init(rocker_port
);
2592 dev_err(&pdev
->dev
, "world init failed\n");
2593 goto err_world_check_init
;
2596 rocker_port_dev_addr_init(rocker_port
);
2597 dev
->netdev_ops
= &rocker_port_netdev_ops
;
2598 dev
->ethtool_ops
= &rocker_port_ethtool_ops
;
2599 dev
->switchdev_ops
= &rocker_port_switchdev_ops
;
2600 netif_tx_napi_add(dev
, &rocker_port
->napi_tx
, rocker_port_poll_tx
,
2602 netif_napi_add(dev
, &rocker_port
->napi_rx
, rocker_port_poll_rx
,
2604 rocker_carrier_init(rocker_port
);
2606 dev
->features
|= NETIF_F_NETNS_LOCAL
| NETIF_F_SG
;
2608 /* MTU range: 68 - 9000 */
2609 dev
->min_mtu
= ROCKER_PORT_MIN_MTU
;
2610 dev
->max_mtu
= ROCKER_PORT_MAX_MTU
;
2612 err
= rocker_world_port_pre_init(rocker_port
);
2614 dev_err(&pdev
->dev
, "port world pre-init failed\n");
2615 goto err_world_port_pre_init
;
2617 err
= register_netdev(dev
);
2619 dev_err(&pdev
->dev
, "register_netdev failed\n");
2620 goto err_register_netdev
;
2622 rocker
->ports
[port_number
] = rocker_port
;
2624 err
= rocker_world_port_init(rocker_port
);
2626 dev_err(&pdev
->dev
, "port world init failed\n");
2627 goto err_world_port_init
;
2632 err_world_port_init
:
2633 rocker
->ports
[port_number
] = NULL
;
2634 unregister_netdev(dev
);
2635 err_register_netdev
:
2636 rocker_world_port_post_fini(rocker_port
);
2637 err_world_port_pre_init
:
2638 err_world_check_init
:
2643 static int rocker_probe_ports(struct rocker
*rocker
)
2649 alloc_size
= sizeof(struct rocker_port
*) * rocker
->port_count
;
2650 rocker
->ports
= kzalloc(alloc_size
, GFP_KERNEL
);
2653 for (i
= 0; i
< rocker
->port_count
; i
++) {
2654 err
= rocker_probe_port(rocker
, i
);
2661 rocker_remove_ports(rocker
);
2665 static int rocker_msix_init(struct rocker
*rocker
)
2667 struct pci_dev
*pdev
= rocker
->pdev
;
2672 msix_entries
= pci_msix_vec_count(pdev
);
2673 if (msix_entries
< 0)
2674 return msix_entries
;
2676 if (msix_entries
!= ROCKER_MSIX_VEC_COUNT(rocker
->port_count
))
2679 rocker
->msix_entries
= kmalloc_array(msix_entries
,
2680 sizeof(struct msix_entry
),
2682 if (!rocker
->msix_entries
)
2685 for (i
= 0; i
< msix_entries
; i
++)
2686 rocker
->msix_entries
[i
].entry
= i
;
2688 err
= pci_enable_msix_exact(pdev
, rocker
->msix_entries
, msix_entries
);
2690 goto err_enable_msix
;
2695 kfree(rocker
->msix_entries
);
2699 static void rocker_msix_fini(const struct rocker
*rocker
)
2701 pci_disable_msix(rocker
->pdev
);
2702 kfree(rocker
->msix_entries
);
2705 static bool rocker_port_dev_check(const struct net_device
*dev
)
2707 return dev
->netdev_ops
== &rocker_port_netdev_ops
;
2710 struct rocker_switchdev_event_work
{
2711 struct work_struct work
;
2712 struct switchdev_notifier_fdb_info fdb_info
;
2713 struct rocker_port
*rocker_port
;
2714 unsigned long event
;
2718 rocker_fdb_offload_notify(struct rocker_port
*rocker_port
,
2719 struct switchdev_notifier_fdb_info
*recv_info
)
2721 struct switchdev_notifier_fdb_info info
;
2723 info
.addr
= recv_info
->addr
;
2724 info
.vid
= recv_info
->vid
;
2725 call_switchdev_notifiers(SWITCHDEV_FDB_OFFLOADED
,
2726 rocker_port
->dev
, &info
.info
);
2729 static void rocker_switchdev_event_work(struct work_struct
*work
)
2731 struct rocker_switchdev_event_work
*switchdev_work
=
2732 container_of(work
, struct rocker_switchdev_event_work
, work
);
2733 struct rocker_port
*rocker_port
= switchdev_work
->rocker_port
;
2734 struct switchdev_notifier_fdb_info
*fdb_info
;
2738 switch (switchdev_work
->event
) {
2739 case SWITCHDEV_FDB_ADD_TO_DEVICE
:
2740 fdb_info
= &switchdev_work
->fdb_info
;
2741 err
= rocker_world_port_fdb_add(rocker_port
, fdb_info
);
2743 netdev_dbg(rocker_port
->dev
, "fdb add failed err=%d\n", err
);
2746 rocker_fdb_offload_notify(rocker_port
, fdb_info
);
2748 case SWITCHDEV_FDB_DEL_TO_DEVICE
:
2749 fdb_info
= &switchdev_work
->fdb_info
;
2750 err
= rocker_world_port_fdb_del(rocker_port
, fdb_info
);
2752 netdev_dbg(rocker_port
->dev
, "fdb add failed err=%d\n", err
);
2757 kfree(switchdev_work
->fdb_info
.addr
);
2758 kfree(switchdev_work
);
2759 dev_put(rocker_port
->dev
);
2762 /* called under rcu_read_lock() */
2763 static int rocker_switchdev_event(struct notifier_block
*unused
,
2764 unsigned long event
, void *ptr
)
2766 struct net_device
*dev
= switchdev_notifier_info_to_dev(ptr
);
2767 struct rocker_switchdev_event_work
*switchdev_work
;
2768 struct switchdev_notifier_fdb_info
*fdb_info
= ptr
;
2769 struct rocker_port
*rocker_port
;
2771 if (!rocker_port_dev_check(dev
))
2774 rocker_port
= netdev_priv(dev
);
2775 switchdev_work
= kzalloc(sizeof(*switchdev_work
), GFP_ATOMIC
);
2776 if (WARN_ON(!switchdev_work
))
2779 INIT_WORK(&switchdev_work
->work
, rocker_switchdev_event_work
);
2780 switchdev_work
->rocker_port
= rocker_port
;
2781 switchdev_work
->event
= event
;
2784 case SWITCHDEV_FDB_ADD_TO_DEVICE
: /* fall through */
2785 case SWITCHDEV_FDB_DEL_TO_DEVICE
:
2786 memcpy(&switchdev_work
->fdb_info
, ptr
,
2787 sizeof(switchdev_work
->fdb_info
));
2788 switchdev_work
->fdb_info
.addr
= kzalloc(ETH_ALEN
, GFP_ATOMIC
);
2789 ether_addr_copy((u8
*)switchdev_work
->fdb_info
.addr
,
2791 /* Take a reference on the rocker device */
2795 kfree(switchdev_work
);
2799 queue_work(rocker_port
->rocker
->rocker_owq
,
2800 &switchdev_work
->work
);
2804 static struct notifier_block rocker_switchdev_notifier
= {
2805 .notifier_call
= rocker_switchdev_event
,
2808 static int rocker_probe(struct pci_dev
*pdev
, const struct pci_device_id
*id
)
2810 struct rocker
*rocker
;
2813 rocker
= kzalloc(sizeof(*rocker
), GFP_KERNEL
);
2817 err
= pci_enable_device(pdev
);
2819 dev_err(&pdev
->dev
, "pci_enable_device failed\n");
2820 goto err_pci_enable_device
;
2823 err
= pci_request_regions(pdev
, rocker_driver_name
);
2825 dev_err(&pdev
->dev
, "pci_request_regions failed\n");
2826 goto err_pci_request_regions
;
2829 err
= pci_set_dma_mask(pdev
, DMA_BIT_MASK(64));
2831 err
= pci_set_consistent_dma_mask(pdev
, DMA_BIT_MASK(64));
2833 dev_err(&pdev
->dev
, "pci_set_consistent_dma_mask failed\n");
2834 goto err_pci_set_dma_mask
;
2837 err
= pci_set_dma_mask(pdev
, DMA_BIT_MASK(32));
2839 dev_err(&pdev
->dev
, "pci_set_dma_mask failed\n");
2840 goto err_pci_set_dma_mask
;
2844 if (pci_resource_len(pdev
, 0) < ROCKER_PCI_BAR0_SIZE
) {
2845 dev_err(&pdev
->dev
, "invalid PCI region size\n");
2847 goto err_pci_resource_len_check
;
2850 rocker
->hw_addr
= ioremap(pci_resource_start(pdev
, 0),
2851 pci_resource_len(pdev
, 0));
2852 if (!rocker
->hw_addr
) {
2853 dev_err(&pdev
->dev
, "ioremap failed\n");
2857 pci_set_master(pdev
);
2859 rocker
->pdev
= pdev
;
2860 pci_set_drvdata(pdev
, rocker
);
2862 rocker
->port_count
= rocker_read32(rocker
, PORT_PHYS_COUNT
);
2864 err
= rocker_msix_init(rocker
);
2866 dev_err(&pdev
->dev
, "MSI-X init failed\n");
2870 err
= rocker_basic_hw_test(rocker
);
2872 dev_err(&pdev
->dev
, "basic hw test failed\n");
2873 goto err_basic_hw_test
;
2876 rocker_write32(rocker
, CONTROL
, ROCKER_CONTROL_RESET
);
2878 err
= rocker_dma_rings_init(rocker
);
2880 goto err_dma_rings_init
;
2882 err
= request_irq(rocker_msix_vector(rocker
, ROCKER_MSIX_VEC_CMD
),
2883 rocker_cmd_irq_handler
, 0,
2884 rocker_driver_name
, rocker
);
2886 dev_err(&pdev
->dev
, "cannot assign cmd irq\n");
2887 goto err_request_cmd_irq
;
2890 err
= request_irq(rocker_msix_vector(rocker
, ROCKER_MSIX_VEC_EVENT
),
2891 rocker_event_irq_handler
, 0,
2892 rocker_driver_name
, rocker
);
2894 dev_err(&pdev
->dev
, "cannot assign event irq\n");
2895 goto err_request_event_irq
;
2898 rocker
->rocker_owq
= alloc_ordered_workqueue(rocker_driver_name
,
2900 if (!rocker
->rocker_owq
) {
2902 goto err_alloc_ordered_workqueue
;
2905 err
= rocker_probe_ports(rocker
);
2907 dev_err(&pdev
->dev
, "failed to probe ports\n");
2908 goto err_probe_ports
;
2911 /* Only FIBs pointing to our own netdevs are programmed into
2912 * the device, so no need to pass a callback.
2914 rocker
->fib_nb
.notifier_call
= rocker_router_fib_event
;
2915 err
= register_fib_notifier(&rocker
->fib_nb
, NULL
);
2917 goto err_register_fib_notifier
;
2919 err
= register_switchdev_notifier(&rocker_switchdev_notifier
);
2921 dev_err(&pdev
->dev
, "Failed to register switchdev notifier\n");
2922 goto err_register_switchdev_notifier
;
2925 rocker
->hw
.id
= rocker_read64(rocker
, SWITCH_ID
);
2927 dev_info(&pdev
->dev
, "Rocker switch with id %*phN\n",
2928 (int)sizeof(rocker
->hw
.id
), &rocker
->hw
.id
);
2932 err_register_switchdev_notifier
:
2933 unregister_fib_notifier(&rocker
->fib_nb
);
2934 err_register_fib_notifier
:
2935 rocker_remove_ports(rocker
);
2937 destroy_workqueue(rocker
->rocker_owq
);
2938 err_alloc_ordered_workqueue
:
2939 free_irq(rocker_msix_vector(rocker
, ROCKER_MSIX_VEC_EVENT
), rocker
);
2940 err_request_event_irq
:
2941 free_irq(rocker_msix_vector(rocker
, ROCKER_MSIX_VEC_CMD
), rocker
);
2942 err_request_cmd_irq
:
2943 rocker_dma_rings_fini(rocker
);
2946 rocker_msix_fini(rocker
);
2948 iounmap(rocker
->hw_addr
);
2950 err_pci_resource_len_check
:
2951 err_pci_set_dma_mask
:
2952 pci_release_regions(pdev
);
2953 err_pci_request_regions
:
2954 pci_disable_device(pdev
);
2955 err_pci_enable_device
:
2960 static void rocker_remove(struct pci_dev
*pdev
)
2962 struct rocker
*rocker
= pci_get_drvdata(pdev
);
2964 unregister_switchdev_notifier(&rocker_switchdev_notifier
);
2965 unregister_fib_notifier(&rocker
->fib_nb
);
2966 rocker_remove_ports(rocker
);
2967 rocker_write32(rocker
, CONTROL
, ROCKER_CONTROL_RESET
);
2968 destroy_workqueue(rocker
->rocker_owq
);
2969 free_irq(rocker_msix_vector(rocker
, ROCKER_MSIX_VEC_EVENT
), rocker
);
2970 free_irq(rocker_msix_vector(rocker
, ROCKER_MSIX_VEC_CMD
), rocker
);
2971 rocker_dma_rings_fini(rocker
);
2972 rocker_msix_fini(rocker
);
2973 iounmap(rocker
->hw_addr
);
2974 pci_release_regions(rocker
->pdev
);
2975 pci_disable_device(rocker
->pdev
);
2979 static struct pci_driver rocker_pci_driver
= {
2980 .name
= rocker_driver_name
,
2981 .id_table
= rocker_pci_id_table
,
2982 .probe
= rocker_probe
,
2983 .remove
= rocker_remove
,
2986 /************************************
2987 * Net device notifier event handler
2988 ************************************/
2990 static bool rocker_port_dev_check_under(const struct net_device
*dev
,
2991 struct rocker
*rocker
)
2993 struct rocker_port
*rocker_port
;
2995 if (!rocker_port_dev_check(dev
))
2998 rocker_port
= netdev_priv(dev
);
2999 if (rocker_port
->rocker
!= rocker
)
3005 struct rocker_walk_data
{
3006 struct rocker
*rocker
;
3007 struct rocker_port
*port
;
3010 static int rocker_lower_dev_walk(struct net_device
*lower_dev
, void *_data
)
3012 struct rocker_walk_data
*data
= _data
;
3015 if (rocker_port_dev_check_under(lower_dev
, data
->rocker
)) {
3016 data
->port
= netdev_priv(lower_dev
);
3023 struct rocker_port
*rocker_port_dev_lower_find(struct net_device
*dev
,
3024 struct rocker
*rocker
)
3026 struct rocker_walk_data data
;
3028 if (rocker_port_dev_check_under(dev
, rocker
))
3029 return netdev_priv(dev
);
3031 data
.rocker
= rocker
;
3033 netdev_walk_all_lower_dev(dev
, rocker_lower_dev_walk
, &data
);
3038 static int rocker_netdevice_event(struct notifier_block
*unused
,
3039 unsigned long event
, void *ptr
)
3041 struct net_device
*dev
= netdev_notifier_info_to_dev(ptr
);
3042 struct netdev_notifier_changeupper_info
*info
;
3043 struct rocker_port
*rocker_port
;
3046 if (!rocker_port_dev_check(dev
))
3050 case NETDEV_CHANGEUPPER
:
3054 rocker_port
= netdev_priv(dev
);
3055 if (info
->linking
) {
3056 err
= rocker_world_port_master_linked(rocker_port
,
3059 netdev_warn(dev
, "failed to reflect master linked (err %d)\n",
3062 err
= rocker_world_port_master_unlinked(rocker_port
,
3065 netdev_warn(dev
, "failed to reflect master unlinked (err %d)\n",
3073 static struct notifier_block rocker_netdevice_nb __read_mostly
= {
3074 .notifier_call
= rocker_netdevice_event
,
3077 /************************************
3078 * Net event notifier event handler
3079 ************************************/
3081 static int rocker_netevent_event(struct notifier_block
*unused
,
3082 unsigned long event
, void *ptr
)
3084 struct rocker_port
*rocker_port
;
3085 struct net_device
*dev
;
3086 struct neighbour
*n
= ptr
;
3090 case NETEVENT_NEIGH_UPDATE
:
3091 if (n
->tbl
!= &arp_tbl
)
3094 if (!rocker_port_dev_check(dev
))
3096 rocker_port
= netdev_priv(dev
);
3097 err
= rocker_world_port_neigh_update(rocker_port
, n
);
3099 netdev_warn(dev
, "failed to handle neigh update (err %d)\n",
3107 static struct notifier_block rocker_netevent_nb __read_mostly
= {
3108 .notifier_call
= rocker_netevent_event
,
3111 /***********************
3112 * Module init and exit
3113 ***********************/
3115 static int __init
rocker_module_init(void)
3119 register_netdevice_notifier(&rocker_netdevice_nb
);
3120 register_netevent_notifier(&rocker_netevent_nb
);
3121 err
= pci_register_driver(&rocker_pci_driver
);
3123 goto err_pci_register_driver
;
3126 err_pci_register_driver
:
3127 unregister_netevent_notifier(&rocker_netevent_nb
);
3128 unregister_netdevice_notifier(&rocker_netdevice_nb
);
3132 static void __exit
rocker_module_exit(void)
3134 unregister_netevent_notifier(&rocker_netevent_nb
);
3135 unregister_netdevice_notifier(&rocker_netdevice_nb
);
3136 pci_unregister_driver(&rocker_pci_driver
);
3139 module_init(rocker_module_init
);
3140 module_exit(rocker_module_exit
);
3142 MODULE_LICENSE("GPL v2");
3143 MODULE_AUTHOR("Jiri Pirko <jiri@resnulli.us>");
3144 MODULE_AUTHOR("Scott Feldman <sfeldma@gmail.com>");
3145 MODULE_DESCRIPTION("Rocker switch device driver");
3146 MODULE_DEVICE_TABLE(pci
, rocker_pci_id_table
);