2 * drivers/net/ethernet/rocker/rocker.c - Rocker switch device driver
3 * Copyright (c) 2014 Jiri Pirko <jiri@resnulli.us>
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/hashtable.h>
20 #include <linux/crc32.h>
21 #include <linux/sort.h>
22 #include <linux/random.h>
23 #include <linux/netdevice.h>
24 #include <linux/inetdevice.h>
25 #include <linux/skbuff.h>
26 #include <linux/socket.h>
27 #include <linux/etherdevice.h>
28 #include <linux/ethtool.h>
29 #include <linux/if_ether.h>
30 #include <linux/if_vlan.h>
31 #include <linux/if_bridge.h>
32 #include <linux/bitops.h>
33 #include <linux/ctype.h>
34 #include <net/switchdev.h>
35 #include <net/rtnetlink.h>
36 #include <net/ip_fib.h>
37 #include <net/netevent.h>
39 #include <asm-generic/io-64-nonatomic-lo-hi.h>
40 #include <generated/utsrelease.h>
44 static const char rocker_driver_name[] = "rocker";
46 static const struct pci_device_id rocker_pci_id_table[] = {
47 {PCI_VDEVICE(REDHAT, PCI_DEVICE_ID_REDHAT_ROCKER), 0},
51 struct rocker_flow_tbl_key {
53 enum rocker_of_dpa_table_id tbl_id;
58 enum rocker_of_dpa_table_id goto_tbl;
64 enum rocker_of_dpa_table_id goto_tbl;
73 u8 eth_dst_mask[ETH_ALEN];
76 enum rocker_of_dpa_table_id goto_tbl;
83 enum rocker_of_dpa_table_id goto_tbl;
88 u8 eth_dst_mask[ETH_ALEN];
93 enum rocker_of_dpa_table_id goto_tbl;
100 u8 eth_src[ETH_ALEN];
101 u8 eth_src_mask[ETH_ALEN];
102 u8 eth_dst[ETH_ALEN];
103 u8 eth_dst_mask[ETH_ALEN];
116 struct rocker_flow_tbl_entry {
117 struct hlist_node entry;
120 struct rocker_flow_tbl_key key;
122 u32 key_crc32; /* key */
125 struct rocker_group_tbl_entry {
126 struct hlist_node entry;
128 u32 group_id; /* key */
136 u8 eth_src[ETH_ALEN];
137 u8 eth_dst[ETH_ALEN];
142 u8 eth_src[ETH_ALEN];
143 u8 eth_dst[ETH_ALEN];
151 struct rocker_fdb_tbl_entry {
152 struct hlist_node entry;
153 u32 key_crc32; /* key */
155 struct rocker_fdb_tbl_key {
162 struct rocker_internal_vlan_tbl_entry {
163 struct hlist_node entry;
164 int ifindex; /* key */
169 struct rocker_neigh_tbl_entry {
170 struct hlist_node entry;
171 __be32 ip_addr; /* key */
172 struct net_device *dev;
175 u8 eth_dst[ETH_ALEN];
179 struct rocker_desc_info {
180 char *data; /* mapped */
183 struct rocker_desc *desc;
184 DEFINE_DMA_UNMAP_ADDR(mapaddr);
187 struct rocker_dma_ring_info {
191 struct rocker_desc *desc; /* mapped */
193 struct rocker_desc_info *desc_info;
200 ROCKER_CTRL_LINK_LOCAL_MCAST,
201 ROCKER_CTRL_LOCAL_ARP,
202 ROCKER_CTRL_IPV4_MCAST,
203 ROCKER_CTRL_IPV6_MCAST,
204 ROCKER_CTRL_DFLT_BRIDGING,
208 #define ROCKER_INTERNAL_VLAN_ID_BASE 0x0f00
209 #define ROCKER_N_INTERNAL_VLANS 255
210 #define ROCKER_VLAN_BITMAP_LEN BITS_TO_LONGS(VLAN_N_VID)
211 #define ROCKER_INTERNAL_VLAN_BITMAP_LEN BITS_TO_LONGS(ROCKER_N_INTERNAL_VLANS)
214 struct net_device *dev;
215 struct net_device *bridge_dev;
216 struct rocker *rocker;
217 unsigned int port_number;
219 __be16 internal_vlan_id;
222 bool ctrls[ROCKER_CTRL_MAX];
223 unsigned long vlan_bitmap[ROCKER_VLAN_BITMAP_LEN];
224 struct napi_struct napi_tx;
225 struct napi_struct napi_rx;
226 struct rocker_dma_ring_info tx_ring;
227 struct rocker_dma_ring_info rx_ring;
228 struct list_head trans_mem;
232 struct pci_dev *pdev;
234 struct msix_entry *msix_entries;
235 unsigned int port_count;
236 struct rocker_port **ports;
240 spinlock_t cmd_ring_lock;
241 struct rocker_dma_ring_info cmd_ring;
242 struct rocker_dma_ring_info event_ring;
243 DECLARE_HASHTABLE(flow_tbl, 16);
244 spinlock_t flow_tbl_lock;
245 u64 flow_tbl_next_cookie;
246 DECLARE_HASHTABLE(group_tbl, 16);
247 spinlock_t group_tbl_lock;
248 DECLARE_HASHTABLE(fdb_tbl, 16);
249 spinlock_t fdb_tbl_lock;
250 unsigned long internal_vlan_bitmap[ROCKER_INTERNAL_VLAN_BITMAP_LEN];
251 DECLARE_HASHTABLE(internal_vlan_tbl, 8);
252 spinlock_t internal_vlan_tbl_lock;
253 DECLARE_HASHTABLE(neigh_tbl, 16);
254 spinlock_t neigh_tbl_lock;
255 u32 neigh_tbl_next_index;
258 static const u8 zero_mac[ETH_ALEN] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
259 static const u8 ff_mac[ETH_ALEN] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
260 static const u8 ll_mac[ETH_ALEN] = { 0x01, 0x80, 0xc2, 0x00, 0x00, 0x00 };
261 static const u8 ll_mask[ETH_ALEN] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xf0 };
262 static const u8 mcast_mac[ETH_ALEN] = { 0x01, 0x00, 0x00, 0x00, 0x00, 0x00 };
263 static const u8 ipv4_mcast[ETH_ALEN] = { 0x01, 0x00, 0x5e, 0x00, 0x00, 0x00 };
264 static const u8 ipv4_mask[ETH_ALEN] = { 0xff, 0xff, 0xff, 0x80, 0x00, 0x00 };
265 static const u8 ipv6_mcast[ETH_ALEN] = { 0x33, 0x33, 0x00, 0x00, 0x00, 0x00 };
266 static const u8 ipv6_mask[ETH_ALEN] = { 0xff, 0xff, 0x00, 0x00, 0x00, 0x00 };
268 /* Rocker priority levels for flow table entries. Higher
269 * priority match takes precedence over lower priority match.
273 ROCKER_PRIORITY_UNKNOWN = 0,
274 ROCKER_PRIORITY_IG_PORT = 1,
275 ROCKER_PRIORITY_VLAN = 1,
276 ROCKER_PRIORITY_TERM_MAC_UCAST = 0,
277 ROCKER_PRIORITY_TERM_MAC_MCAST = 1,
278 ROCKER_PRIORITY_BRIDGING_VLAN_DFLT_EXACT = 1,
279 ROCKER_PRIORITY_BRIDGING_VLAN_DFLT_WILD = 2,
280 ROCKER_PRIORITY_BRIDGING_VLAN = 3,
281 ROCKER_PRIORITY_BRIDGING_TENANT_DFLT_EXACT = 1,
282 ROCKER_PRIORITY_BRIDGING_TENANT_DFLT_WILD = 2,
283 ROCKER_PRIORITY_BRIDGING_TENANT = 3,
284 ROCKER_PRIORITY_ACL_CTRL = 3,
285 ROCKER_PRIORITY_ACL_NORMAL = 2,
286 ROCKER_PRIORITY_ACL_DFLT = 1,
289 static bool rocker_vlan_id_is_internal(__be16 vlan_id)
291 u16 start = ROCKER_INTERNAL_VLAN_ID_BASE;
293 u16 _vlan_id = ntohs(vlan_id);
295 return (_vlan_id >= start && _vlan_id <= end);
298 static __be16 rocker_port_vid_to_vlan(struct rocker_port *rocker_port,
299 u16 vid, bool *pop_vlan)
305 vlan_id = htons(vid);
307 vlan_id = rocker_port->internal_vlan_id;
315 static u16 rocker_port_vlan_to_vid(struct rocker_port *rocker_port,
318 if (rocker_vlan_id_is_internal(vlan_id))
321 return ntohs(vlan_id);
324 static bool rocker_port_is_bridged(struct rocker_port *rocker_port)
326 return !!rocker_port->bridge_dev;
329 static void *__rocker_port_mem_alloc(struct rocker_port *rocker_port,
330 enum switchdev_trans trans, size_t size)
332 struct list_head *elem = NULL;
334 /* If in transaction prepare phase, allocate the memory
335 * and enqueue it on a per-port list. If in transaction
336 * commit phase, dequeue the memory from the per-port list
337 * rather than re-allocating the memory. The idea is the
338 * driver code paths for prepare and commit are identical
339 * so the memory allocated in the prepare phase is the
340 * memory used in the commit phase.
344 case SWITCHDEV_TRANS_PREPARE:
345 elem = kzalloc(size + sizeof(*elem), GFP_KERNEL);
348 list_add_tail(elem, &rocker_port->trans_mem);
350 case SWITCHDEV_TRANS_COMMIT:
351 BUG_ON(list_empty(&rocker_port->trans_mem));
352 elem = rocker_port->trans_mem.next;
355 case SWITCHDEV_TRANS_NONE:
356 elem = kzalloc(size + sizeof(*elem), GFP_KERNEL);
358 INIT_LIST_HEAD(elem);
364 return elem ? elem + 1 : NULL;
367 static void *rocker_port_kzalloc(struct rocker_port *rocker_port,
368 enum switchdev_trans trans, size_t size)
370 return __rocker_port_mem_alloc(rocker_port, trans, size);
373 static void *rocker_port_kcalloc(struct rocker_port *rocker_port,
374 enum switchdev_trans trans, size_t n,
377 return __rocker_port_mem_alloc(rocker_port, trans, n * size);
380 static void rocker_port_kfree(struct rocker_port *rocker_port,
381 enum switchdev_trans trans, const void *mem)
383 struct list_head *elem;
385 /* Frees are ignored if in transaction prepare phase. The
386 * memory remains on the per-port list until freed in the
390 if (trans == SWITCHDEV_TRANS_PREPARE)
393 elem = (struct list_head *)mem - 1;
394 BUG_ON(!list_empty(elem));
399 wait_queue_head_t wait;
403 static void rocker_wait_reset(struct rocker_wait *wait)
408 static void rocker_wait_init(struct rocker_wait *wait)
410 init_waitqueue_head(&wait->wait);
411 rocker_wait_reset(wait);
414 static struct rocker_wait *rocker_wait_create(struct rocker_port *rocker_port,
415 enum switchdev_trans trans)
417 struct rocker_wait *wait;
419 wait = rocker_port_kzalloc(rocker_port, trans, sizeof(*wait));
422 rocker_wait_init(wait);
426 static void rocker_wait_destroy(struct rocker_port *rocker_port,
427 enum switchdev_trans trans,
428 struct rocker_wait *wait)
430 rocker_port_kfree(rocker_port, trans, wait);
433 static bool rocker_wait_event_timeout(struct rocker_wait *wait,
434 unsigned long timeout)
436 wait_event_timeout(wait->wait, wait->done, HZ / 10);
442 static void rocker_wait_wake_up(struct rocker_wait *wait)
445 wake_up(&wait->wait);
448 static u32 rocker_msix_vector(struct rocker *rocker, unsigned int vector)
450 return rocker->msix_entries[vector].vector;
453 static u32 rocker_msix_tx_vector(struct rocker_port *rocker_port)
455 return rocker_msix_vector(rocker_port->rocker,
456 ROCKER_MSIX_VEC_TX(rocker_port->port_number));
459 static u32 rocker_msix_rx_vector(struct rocker_port *rocker_port)
461 return rocker_msix_vector(rocker_port->rocker,
462 ROCKER_MSIX_VEC_RX(rocker_port->port_number));
465 #define rocker_write32(rocker, reg, val) \
466 writel((val), (rocker)->hw_addr + (ROCKER_ ## reg))
467 #define rocker_read32(rocker, reg) \
468 readl((rocker)->hw_addr + (ROCKER_ ## reg))
469 #define rocker_write64(rocker, reg, val) \
470 writeq((val), (rocker)->hw_addr + (ROCKER_ ## reg))
471 #define rocker_read64(rocker, reg) \
472 readq((rocker)->hw_addr + (ROCKER_ ## reg))
474 /*****************************
475 * HW basic testing functions
476 *****************************/
478 static int rocker_reg_test(struct rocker *rocker)
480 struct pci_dev *pdev = rocker->pdev;
486 rocker_write32(rocker, TEST_REG, rnd);
487 test_reg = rocker_read32(rocker, TEST_REG);
488 if (test_reg != rnd * 2) {
489 dev_err(&pdev->dev, "unexpected 32bit register value %08llx, expected %08llx\n",
496 rnd |= prandom_u32();
497 rocker_write64(rocker, TEST_REG64, rnd);
498 test_reg = rocker_read64(rocker, TEST_REG64);
499 if (test_reg != rnd * 2) {
500 dev_err(&pdev->dev, "unexpected 64bit register value %16llx, expected %16llx\n",
508 static int rocker_dma_test_one(struct rocker *rocker, struct rocker_wait *wait,
509 u32 test_type, dma_addr_t dma_handle,
510 unsigned char *buf, unsigned char *expect,
513 struct pci_dev *pdev = rocker->pdev;
516 rocker_wait_reset(wait);
517 rocker_write32(rocker, TEST_DMA_CTRL, test_type);
519 if (!rocker_wait_event_timeout(wait, HZ / 10)) {
520 dev_err(&pdev->dev, "no interrupt received within a timeout\n");
524 for (i = 0; i < size; i++) {
525 if (buf[i] != expect[i]) {
526 dev_err(&pdev->dev, "unexpected memory content %02x at byte %x\n, %02x expected",
527 buf[i], i, expect[i]);
534 #define ROCKER_TEST_DMA_BUF_SIZE (PAGE_SIZE * 4)
535 #define ROCKER_TEST_DMA_FILL_PATTERN 0x96
537 static int rocker_dma_test_offset(struct rocker *rocker,
538 struct rocker_wait *wait, int offset)
540 struct pci_dev *pdev = rocker->pdev;
541 unsigned char *alloc;
543 unsigned char *expect;
544 dma_addr_t dma_handle;
548 alloc = kzalloc(ROCKER_TEST_DMA_BUF_SIZE * 2 + offset,
549 GFP_KERNEL | GFP_DMA);
552 buf = alloc + offset;
553 expect = buf + ROCKER_TEST_DMA_BUF_SIZE;
555 dma_handle = pci_map_single(pdev, buf, ROCKER_TEST_DMA_BUF_SIZE,
556 PCI_DMA_BIDIRECTIONAL);
557 if (pci_dma_mapping_error(pdev, dma_handle)) {
562 rocker_write64(rocker, TEST_DMA_ADDR, dma_handle);
563 rocker_write32(rocker, TEST_DMA_SIZE, ROCKER_TEST_DMA_BUF_SIZE);
565 memset(expect, ROCKER_TEST_DMA_FILL_PATTERN, ROCKER_TEST_DMA_BUF_SIZE);
566 err = rocker_dma_test_one(rocker, wait, ROCKER_TEST_DMA_CTRL_FILL,
567 dma_handle, buf, expect,
568 ROCKER_TEST_DMA_BUF_SIZE);
572 memset(expect, 0, ROCKER_TEST_DMA_BUF_SIZE);
573 err = rocker_dma_test_one(rocker, wait, ROCKER_TEST_DMA_CTRL_CLEAR,
574 dma_handle, buf, expect,
575 ROCKER_TEST_DMA_BUF_SIZE);
579 prandom_bytes(buf, ROCKER_TEST_DMA_BUF_SIZE);
580 for (i = 0; i < ROCKER_TEST_DMA_BUF_SIZE; i++)
582 err = rocker_dma_test_one(rocker, wait, ROCKER_TEST_DMA_CTRL_INVERT,
583 dma_handle, buf, expect,
584 ROCKER_TEST_DMA_BUF_SIZE);
589 pci_unmap_single(pdev, dma_handle, ROCKER_TEST_DMA_BUF_SIZE,
590 PCI_DMA_BIDIRECTIONAL);
597 static int rocker_dma_test(struct rocker *rocker, struct rocker_wait *wait)
602 for (i = 0; i < 8; i++) {
603 err = rocker_dma_test_offset(rocker, wait, i);
610 static irqreturn_t rocker_test_irq_handler(int irq, void *dev_id)
612 struct rocker_wait *wait = dev_id;
614 rocker_wait_wake_up(wait);
619 static int rocker_basic_hw_test(struct rocker *rocker)
621 struct pci_dev *pdev = rocker->pdev;
622 struct rocker_wait wait;
625 err = rocker_reg_test(rocker);
627 dev_err(&pdev->dev, "reg test failed\n");
631 err = request_irq(rocker_msix_vector(rocker, ROCKER_MSIX_VEC_TEST),
632 rocker_test_irq_handler, 0,
633 rocker_driver_name, &wait);
635 dev_err(&pdev->dev, "cannot assign test irq\n");
639 rocker_wait_init(&wait);
640 rocker_write32(rocker, TEST_IRQ, ROCKER_MSIX_VEC_TEST);
642 if (!rocker_wait_event_timeout(&wait, HZ / 10)) {
643 dev_err(&pdev->dev, "no interrupt received within a timeout\n");
648 err = rocker_dma_test(rocker, &wait);
650 dev_err(&pdev->dev, "dma test failed\n");
653 free_irq(rocker_msix_vector(rocker, ROCKER_MSIX_VEC_TEST), &wait);
661 #define ROCKER_TLV_ALIGNTO 8U
662 #define ROCKER_TLV_ALIGN(len) \
663 (((len) + ROCKER_TLV_ALIGNTO - 1) & ~(ROCKER_TLV_ALIGNTO - 1))
664 #define ROCKER_TLV_HDRLEN ROCKER_TLV_ALIGN(sizeof(struct rocker_tlv))
666 /* <------- ROCKER_TLV_HDRLEN -------> <--- ROCKER_TLV_ALIGN(payload) --->
667 * +-----------------------------+- - -+- - - - - - - - - - - - - - -+- - -+
668 * | Header | Pad | Payload | Pad |
669 * | (struct rocker_tlv) | ing | | ing |
670 * +-----------------------------+- - -+- - - - - - - - - - - - - - -+- - -+
671 * <--------------------------- tlv->len -------------------------->
674 static struct rocker_tlv *rocker_tlv_next(const struct rocker_tlv *tlv,
677 int totlen = ROCKER_TLV_ALIGN(tlv->len);
679 *remaining -= totlen;
680 return (struct rocker_tlv *) ((char *) tlv + totlen);
683 static int rocker_tlv_ok(const struct rocker_tlv *tlv, int remaining)
685 return remaining >= (int) ROCKER_TLV_HDRLEN &&
686 tlv->len >= ROCKER_TLV_HDRLEN &&
687 tlv->len <= remaining;
690 #define rocker_tlv_for_each(pos, head, len, rem) \
691 for (pos = head, rem = len; \
692 rocker_tlv_ok(pos, rem); \
693 pos = rocker_tlv_next(pos, &(rem)))
695 #define rocker_tlv_for_each_nested(pos, tlv, rem) \
696 rocker_tlv_for_each(pos, rocker_tlv_data(tlv), \
697 rocker_tlv_len(tlv), rem)
699 static int rocker_tlv_attr_size(int payload)
701 return ROCKER_TLV_HDRLEN + payload;
704 static int rocker_tlv_total_size(int payload)
706 return ROCKER_TLV_ALIGN(rocker_tlv_attr_size(payload));
709 static int rocker_tlv_padlen(int payload)
711 return rocker_tlv_total_size(payload) - rocker_tlv_attr_size(payload);
714 static int rocker_tlv_type(const struct rocker_tlv *tlv)
719 static void *rocker_tlv_data(const struct rocker_tlv *tlv)
721 return (char *) tlv + ROCKER_TLV_HDRLEN;
724 static int rocker_tlv_len(const struct rocker_tlv *tlv)
726 return tlv->len - ROCKER_TLV_HDRLEN;
729 static u8 rocker_tlv_get_u8(const struct rocker_tlv *tlv)
731 return *(u8 *) rocker_tlv_data(tlv);
734 static u16 rocker_tlv_get_u16(const struct rocker_tlv *tlv)
736 return *(u16 *) rocker_tlv_data(tlv);
739 static __be16 rocker_tlv_get_be16(const struct rocker_tlv *tlv)
741 return *(__be16 *) rocker_tlv_data(tlv);
744 static u32 rocker_tlv_get_u32(const struct rocker_tlv *tlv)
746 return *(u32 *) rocker_tlv_data(tlv);
749 static u64 rocker_tlv_get_u64(const struct rocker_tlv *tlv)
751 return *(u64 *) rocker_tlv_data(tlv);
754 static void rocker_tlv_parse(struct rocker_tlv **tb, int maxtype,
755 const char *buf, int buf_len)
757 const struct rocker_tlv *tlv;
758 const struct rocker_tlv *head = (const struct rocker_tlv *) buf;
761 memset(tb, 0, sizeof(struct rocker_tlv *) * (maxtype + 1));
763 rocker_tlv_for_each(tlv, head, buf_len, rem) {
764 u32 type = rocker_tlv_type(tlv);
766 if (type > 0 && type <= maxtype)
767 tb[type] = (struct rocker_tlv *) tlv;
771 static void rocker_tlv_parse_nested(struct rocker_tlv **tb, int maxtype,
772 const struct rocker_tlv *tlv)
774 rocker_tlv_parse(tb, maxtype, rocker_tlv_data(tlv),
775 rocker_tlv_len(tlv));
778 static void rocker_tlv_parse_desc(struct rocker_tlv **tb, int maxtype,
779 struct rocker_desc_info *desc_info)
781 rocker_tlv_parse(tb, maxtype, desc_info->data,
782 desc_info->desc->tlv_size);
785 static struct rocker_tlv *rocker_tlv_start(struct rocker_desc_info *desc_info)
787 return (struct rocker_tlv *) ((char *) desc_info->data +
788 desc_info->tlv_size);
791 static int rocker_tlv_put(struct rocker_desc_info *desc_info,
792 int attrtype, int attrlen, const void *data)
794 int tail_room = desc_info->data_size - desc_info->tlv_size;
795 int total_size = rocker_tlv_total_size(attrlen);
796 struct rocker_tlv *tlv;
798 if (unlikely(tail_room < total_size))
801 tlv = rocker_tlv_start(desc_info);
802 desc_info->tlv_size += total_size;
803 tlv->type = attrtype;
804 tlv->len = rocker_tlv_attr_size(attrlen);
805 memcpy(rocker_tlv_data(tlv), data, attrlen);
806 memset((char *) tlv + tlv->len, 0, rocker_tlv_padlen(attrlen));
810 static int rocker_tlv_put_u8(struct rocker_desc_info *desc_info,
811 int attrtype, u8 value)
813 return rocker_tlv_put(desc_info, attrtype, sizeof(u8), &value);
816 static int rocker_tlv_put_u16(struct rocker_desc_info *desc_info,
817 int attrtype, u16 value)
819 return rocker_tlv_put(desc_info, attrtype, sizeof(u16), &value);
822 static int rocker_tlv_put_be16(struct rocker_desc_info *desc_info,
823 int attrtype, __be16 value)
825 return rocker_tlv_put(desc_info, attrtype, sizeof(__be16), &value);
828 static int rocker_tlv_put_u32(struct rocker_desc_info *desc_info,
829 int attrtype, u32 value)
831 return rocker_tlv_put(desc_info, attrtype, sizeof(u32), &value);
834 static int rocker_tlv_put_be32(struct rocker_desc_info *desc_info,
835 int attrtype, __be32 value)
837 return rocker_tlv_put(desc_info, attrtype, sizeof(__be32), &value);
840 static int rocker_tlv_put_u64(struct rocker_desc_info *desc_info,
841 int attrtype, u64 value)
843 return rocker_tlv_put(desc_info, attrtype, sizeof(u64), &value);
846 static struct rocker_tlv *
847 rocker_tlv_nest_start(struct rocker_desc_info *desc_info, int attrtype)
849 struct rocker_tlv *start = rocker_tlv_start(desc_info);
851 if (rocker_tlv_put(desc_info, attrtype, 0, NULL) < 0)
857 static void rocker_tlv_nest_end(struct rocker_desc_info *desc_info,
858 struct rocker_tlv *start)
860 start->len = (char *) rocker_tlv_start(desc_info) - (char *) start;
863 static void rocker_tlv_nest_cancel(struct rocker_desc_info *desc_info,
864 struct rocker_tlv *start)
866 desc_info->tlv_size = (char *) start - desc_info->data;
869 /******************************************
870 * DMA rings and descriptors manipulations
871 ******************************************/
873 static u32 __pos_inc(u32 pos, size_t limit)
875 return ++pos == limit ? 0 : pos;
878 static int rocker_desc_err(struct rocker_desc_info *desc_info)
880 int err = desc_info->desc->comp_err & ~ROCKER_DMA_DESC_COMP_ERR_GEN;
895 case -ROCKER_EMSGSIZE:
897 case -ROCKER_ENOTSUP:
899 case -ROCKER_ENOBUFS:
906 static void rocker_desc_gen_clear(struct rocker_desc_info *desc_info)
908 desc_info->desc->comp_err &= ~ROCKER_DMA_DESC_COMP_ERR_GEN;
911 static bool rocker_desc_gen(struct rocker_desc_info *desc_info)
913 u32 comp_err = desc_info->desc->comp_err;
915 return comp_err & ROCKER_DMA_DESC_COMP_ERR_GEN ? true : false;
918 static void *rocker_desc_cookie_ptr_get(struct rocker_desc_info *desc_info)
920 return (void *)(uintptr_t)desc_info->desc->cookie;
923 static void rocker_desc_cookie_ptr_set(struct rocker_desc_info *desc_info,
926 desc_info->desc->cookie = (uintptr_t) ptr;
929 static struct rocker_desc_info *
930 rocker_desc_head_get(struct rocker_dma_ring_info *info)
932 static struct rocker_desc_info *desc_info;
933 u32 head = __pos_inc(info->head, info->size);
935 desc_info = &info->desc_info[info->head];
936 if (head == info->tail)
937 return NULL; /* ring full */
938 desc_info->tlv_size = 0;
942 static void rocker_desc_commit(struct rocker_desc_info *desc_info)
944 desc_info->desc->buf_size = desc_info->data_size;
945 desc_info->desc->tlv_size = desc_info->tlv_size;
948 static void rocker_desc_head_set(struct rocker *rocker,
949 struct rocker_dma_ring_info *info,
950 struct rocker_desc_info *desc_info)
952 u32 head = __pos_inc(info->head, info->size);
954 BUG_ON(head == info->tail);
955 rocker_desc_commit(desc_info);
957 rocker_write32(rocker, DMA_DESC_HEAD(info->type), head);
960 static struct rocker_desc_info *
961 rocker_desc_tail_get(struct rocker_dma_ring_info *info)
963 static struct rocker_desc_info *desc_info;
965 if (info->tail == info->head)
966 return NULL; /* nothing to be done between head and tail */
967 desc_info = &info->desc_info[info->tail];
968 if (!rocker_desc_gen(desc_info))
969 return NULL; /* gen bit not set, desc is not ready yet */
970 info->tail = __pos_inc(info->tail, info->size);
971 desc_info->tlv_size = desc_info->desc->tlv_size;
975 static void rocker_dma_ring_credits_set(struct rocker *rocker,
976 struct rocker_dma_ring_info *info,
980 rocker_write32(rocker, DMA_DESC_CREDITS(info->type), credits);
983 static unsigned long rocker_dma_ring_size_fix(size_t size)
985 return max(ROCKER_DMA_SIZE_MIN,
986 min(roundup_pow_of_two(size), ROCKER_DMA_SIZE_MAX));
989 static int rocker_dma_ring_create(struct rocker *rocker,
992 struct rocker_dma_ring_info *info)
996 BUG_ON(size != rocker_dma_ring_size_fix(size));
1001 info->desc_info = kcalloc(info->size, sizeof(*info->desc_info),
1003 if (!info->desc_info)
1006 info->desc = pci_alloc_consistent(rocker->pdev,
1007 info->size * sizeof(*info->desc),
1010 kfree(info->desc_info);
1014 for (i = 0; i < info->size; i++)
1015 info->desc_info[i].desc = &info->desc[i];
1017 rocker_write32(rocker, DMA_DESC_CTRL(info->type),
1018 ROCKER_DMA_DESC_CTRL_RESET);
1019 rocker_write64(rocker, DMA_DESC_ADDR(info->type), info->mapaddr);
1020 rocker_write32(rocker, DMA_DESC_SIZE(info->type), info->size);
1025 static void rocker_dma_ring_destroy(struct rocker *rocker,
1026 struct rocker_dma_ring_info *info)
1028 rocker_write64(rocker, DMA_DESC_ADDR(info->type), 0);
1030 pci_free_consistent(rocker->pdev,
1031 info->size * sizeof(struct rocker_desc),
1032 info->desc, info->mapaddr);
1033 kfree(info->desc_info);
1036 static void rocker_dma_ring_pass_to_producer(struct rocker *rocker,
1037 struct rocker_dma_ring_info *info)
1041 BUG_ON(info->head || info->tail);
1043 /* When ring is consumer, we need to advance head for each desc.
1044 * That tells hw that the desc is ready to be used by it.
1046 for (i = 0; i < info->size - 1; i++)
1047 rocker_desc_head_set(rocker, info, &info->desc_info[i]);
1048 rocker_desc_commit(&info->desc_info[i]);
1051 static int rocker_dma_ring_bufs_alloc(struct rocker *rocker,
1052 struct rocker_dma_ring_info *info,
1053 int direction, size_t buf_size)
1055 struct pci_dev *pdev = rocker->pdev;
1059 for (i = 0; i < info->size; i++) {
1060 struct rocker_desc_info *desc_info = &info->desc_info[i];
1061 struct rocker_desc *desc = &info->desc[i];
1062 dma_addr_t dma_handle;
1065 buf = kzalloc(buf_size, GFP_KERNEL | GFP_DMA);
1071 dma_handle = pci_map_single(pdev, buf, buf_size, direction);
1072 if (pci_dma_mapping_error(pdev, dma_handle)) {
1078 desc_info->data = buf;
1079 desc_info->data_size = buf_size;
1080 dma_unmap_addr_set(desc_info, mapaddr, dma_handle);
1082 desc->buf_addr = dma_handle;
1083 desc->buf_size = buf_size;
1088 for (i--; i >= 0; i--) {
1089 struct rocker_desc_info *desc_info = &info->desc_info[i];
1091 pci_unmap_single(pdev, dma_unmap_addr(desc_info, mapaddr),
1092 desc_info->data_size, direction);
1093 kfree(desc_info->data);
1098 static void rocker_dma_ring_bufs_free(struct rocker *rocker,
1099 struct rocker_dma_ring_info *info,
1102 struct pci_dev *pdev = rocker->pdev;
1105 for (i = 0; i < info->size; i++) {
1106 struct rocker_desc_info *desc_info = &info->desc_info[i];
1107 struct rocker_desc *desc = &info->desc[i];
1111 pci_unmap_single(pdev, dma_unmap_addr(desc_info, mapaddr),
1112 desc_info->data_size, direction);
1113 kfree(desc_info->data);
1117 static int rocker_dma_rings_init(struct rocker *rocker)
1119 struct pci_dev *pdev = rocker->pdev;
1122 err = rocker_dma_ring_create(rocker, ROCKER_DMA_CMD,
1123 ROCKER_DMA_CMD_DEFAULT_SIZE,
1126 dev_err(&pdev->dev, "failed to create command dma ring\n");
1130 spin_lock_init(&rocker->cmd_ring_lock);
1132 err = rocker_dma_ring_bufs_alloc(rocker, &rocker->cmd_ring,
1133 PCI_DMA_BIDIRECTIONAL, PAGE_SIZE);
1135 dev_err(&pdev->dev, "failed to alloc command dma ring buffers\n");
1136 goto err_dma_cmd_ring_bufs_alloc;
1139 err = rocker_dma_ring_create(rocker, ROCKER_DMA_EVENT,
1140 ROCKER_DMA_EVENT_DEFAULT_SIZE,
1141 &rocker->event_ring);
1143 dev_err(&pdev->dev, "failed to create event dma ring\n");
1144 goto err_dma_event_ring_create;
1147 err = rocker_dma_ring_bufs_alloc(rocker, &rocker->event_ring,
1148 PCI_DMA_FROMDEVICE, PAGE_SIZE);
1150 dev_err(&pdev->dev, "failed to alloc event dma ring buffers\n");
1151 goto err_dma_event_ring_bufs_alloc;
1153 rocker_dma_ring_pass_to_producer(rocker, &rocker->event_ring);
1156 err_dma_event_ring_bufs_alloc:
1157 rocker_dma_ring_destroy(rocker, &rocker->event_ring);
1158 err_dma_event_ring_create:
1159 rocker_dma_ring_bufs_free(rocker, &rocker->cmd_ring,
1160 PCI_DMA_BIDIRECTIONAL);
1161 err_dma_cmd_ring_bufs_alloc:
1162 rocker_dma_ring_destroy(rocker, &rocker->cmd_ring);
1166 static void rocker_dma_rings_fini(struct rocker *rocker)
1168 rocker_dma_ring_bufs_free(rocker, &rocker->event_ring,
1169 PCI_DMA_BIDIRECTIONAL);
1170 rocker_dma_ring_destroy(rocker, &rocker->event_ring);
1171 rocker_dma_ring_bufs_free(rocker, &rocker->cmd_ring,
1172 PCI_DMA_BIDIRECTIONAL);
1173 rocker_dma_ring_destroy(rocker, &rocker->cmd_ring);
1176 static int rocker_dma_rx_ring_skb_map(struct rocker *rocker,
1177 struct rocker_port *rocker_port,
1178 struct rocker_desc_info *desc_info,
1179 struct sk_buff *skb, size_t buf_len)
1181 struct pci_dev *pdev = rocker->pdev;
1182 dma_addr_t dma_handle;
1184 dma_handle = pci_map_single(pdev, skb->data, buf_len,
1185 PCI_DMA_FROMDEVICE);
1186 if (pci_dma_mapping_error(pdev, dma_handle))
1188 if (rocker_tlv_put_u64(desc_info, ROCKER_TLV_RX_FRAG_ADDR, dma_handle))
1189 goto tlv_put_failure;
1190 if (rocker_tlv_put_u16(desc_info, ROCKER_TLV_RX_FRAG_MAX_LEN, buf_len))
1191 goto tlv_put_failure;
1195 pci_unmap_single(pdev, dma_handle, buf_len, PCI_DMA_FROMDEVICE);
1196 desc_info->tlv_size = 0;
1200 static size_t rocker_port_rx_buf_len(struct rocker_port *rocker_port)
1202 return rocker_port->dev->mtu + ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN;
1205 static int rocker_dma_rx_ring_skb_alloc(struct rocker *rocker,
1206 struct rocker_port *rocker_port,
1207 struct rocker_desc_info *desc_info)
1209 struct net_device *dev = rocker_port->dev;
1210 struct sk_buff *skb;
1211 size_t buf_len = rocker_port_rx_buf_len(rocker_port);
1214 /* Ensure that hw will see tlv_size zero in case of an error.
1215 * That tells hw to use another descriptor.
1217 rocker_desc_cookie_ptr_set(desc_info, NULL);
1218 desc_info->tlv_size = 0;
1220 skb = netdev_alloc_skb_ip_align(dev, buf_len);
1223 err = rocker_dma_rx_ring_skb_map(rocker, rocker_port, desc_info,
1226 dev_kfree_skb_any(skb);
1229 rocker_desc_cookie_ptr_set(desc_info, skb);
1233 static void rocker_dma_rx_ring_skb_unmap(struct rocker *rocker,
1234 struct rocker_tlv **attrs)
1236 struct pci_dev *pdev = rocker->pdev;
1237 dma_addr_t dma_handle;
1240 if (!attrs[ROCKER_TLV_RX_FRAG_ADDR] ||
1241 !attrs[ROCKER_TLV_RX_FRAG_MAX_LEN])
1243 dma_handle = rocker_tlv_get_u64(attrs[ROCKER_TLV_RX_FRAG_ADDR]);
1244 len = rocker_tlv_get_u16(attrs[ROCKER_TLV_RX_FRAG_MAX_LEN]);
1245 pci_unmap_single(pdev, dma_handle, len, PCI_DMA_FROMDEVICE);
1248 static void rocker_dma_rx_ring_skb_free(struct rocker *rocker,
1249 struct rocker_desc_info *desc_info)
1251 struct rocker_tlv *attrs[ROCKER_TLV_RX_MAX + 1];
1252 struct sk_buff *skb = rocker_desc_cookie_ptr_get(desc_info);
1256 rocker_tlv_parse_desc(attrs, ROCKER_TLV_RX_MAX, desc_info);
1257 rocker_dma_rx_ring_skb_unmap(rocker, attrs);
1258 dev_kfree_skb_any(skb);
1261 static int rocker_dma_rx_ring_skbs_alloc(struct rocker *rocker,
1262 struct rocker_port *rocker_port)
1264 struct rocker_dma_ring_info *rx_ring = &rocker_port->rx_ring;
1268 for (i = 0; i < rx_ring->size; i++) {
1269 err = rocker_dma_rx_ring_skb_alloc(rocker, rocker_port,
1270 &rx_ring->desc_info[i]);
1277 for (i--; i >= 0; i--)
1278 rocker_dma_rx_ring_skb_free(rocker, &rx_ring->desc_info[i]);
1282 static void rocker_dma_rx_ring_skbs_free(struct rocker *rocker,
1283 struct rocker_port *rocker_port)
1285 struct rocker_dma_ring_info *rx_ring = &rocker_port->rx_ring;
1288 for (i = 0; i < rx_ring->size; i++)
1289 rocker_dma_rx_ring_skb_free(rocker, &rx_ring->desc_info[i]);
1292 static int rocker_port_dma_rings_init(struct rocker_port *rocker_port)
1294 struct rocker *rocker = rocker_port->rocker;
1297 err = rocker_dma_ring_create(rocker,
1298 ROCKER_DMA_TX(rocker_port->port_number),
1299 ROCKER_DMA_TX_DEFAULT_SIZE,
1300 &rocker_port->tx_ring);
1302 netdev_err(rocker_port->dev, "failed to create tx dma ring\n");
1306 err = rocker_dma_ring_bufs_alloc(rocker, &rocker_port->tx_ring,
1308 ROCKER_DMA_TX_DESC_SIZE);
1310 netdev_err(rocker_port->dev, "failed to alloc tx dma ring buffers\n");
1311 goto err_dma_tx_ring_bufs_alloc;
1314 err = rocker_dma_ring_create(rocker,
1315 ROCKER_DMA_RX(rocker_port->port_number),
1316 ROCKER_DMA_RX_DEFAULT_SIZE,
1317 &rocker_port->rx_ring);
1319 netdev_err(rocker_port->dev, "failed to create rx dma ring\n");
1320 goto err_dma_rx_ring_create;
1323 err = rocker_dma_ring_bufs_alloc(rocker, &rocker_port->rx_ring,
1324 PCI_DMA_BIDIRECTIONAL,
1325 ROCKER_DMA_RX_DESC_SIZE);
1327 netdev_err(rocker_port->dev, "failed to alloc rx dma ring buffers\n");
1328 goto err_dma_rx_ring_bufs_alloc;
1331 err = rocker_dma_rx_ring_skbs_alloc(rocker, rocker_port);
1333 netdev_err(rocker_port->dev, "failed to alloc rx dma ring skbs\n");
1334 goto err_dma_rx_ring_skbs_alloc;
1336 rocker_dma_ring_pass_to_producer(rocker, &rocker_port->rx_ring);
1340 err_dma_rx_ring_skbs_alloc:
1341 rocker_dma_ring_bufs_free(rocker, &rocker_port->rx_ring,
1342 PCI_DMA_BIDIRECTIONAL);
1343 err_dma_rx_ring_bufs_alloc:
1344 rocker_dma_ring_destroy(rocker, &rocker_port->rx_ring);
1345 err_dma_rx_ring_create:
1346 rocker_dma_ring_bufs_free(rocker, &rocker_port->tx_ring,
1348 err_dma_tx_ring_bufs_alloc:
1349 rocker_dma_ring_destroy(rocker, &rocker_port->tx_ring);
1353 static void rocker_port_dma_rings_fini(struct rocker_port *rocker_port)
1355 struct rocker *rocker = rocker_port->rocker;
1357 rocker_dma_rx_ring_skbs_free(rocker, rocker_port);
1358 rocker_dma_ring_bufs_free(rocker, &rocker_port->rx_ring,
1359 PCI_DMA_BIDIRECTIONAL);
1360 rocker_dma_ring_destroy(rocker, &rocker_port->rx_ring);
1361 rocker_dma_ring_bufs_free(rocker, &rocker_port->tx_ring,
1363 rocker_dma_ring_destroy(rocker, &rocker_port->tx_ring);
1366 static void rocker_port_set_enable(struct rocker_port *rocker_port, bool enable)
1368 u64 val = rocker_read64(rocker_port->rocker, PORT_PHYS_ENABLE);
1371 val |= 1ULL << rocker_port->pport;
1373 val &= ~(1ULL << rocker_port->pport);
1374 rocker_write64(rocker_port->rocker, PORT_PHYS_ENABLE, val);
1377 /********************************
1378 * Interrupt handler and helpers
1379 ********************************/
1381 static irqreturn_t rocker_cmd_irq_handler(int irq, void *dev_id)
1383 struct rocker *rocker = dev_id;
1384 struct rocker_desc_info *desc_info;
1385 struct rocker_wait *wait;
1388 spin_lock(&rocker->cmd_ring_lock);
1389 while ((desc_info = rocker_desc_tail_get(&rocker->cmd_ring))) {
1390 wait = rocker_desc_cookie_ptr_get(desc_info);
1391 rocker_wait_wake_up(wait);
1394 spin_unlock(&rocker->cmd_ring_lock);
1395 rocker_dma_ring_credits_set(rocker, &rocker->cmd_ring, credits);
1400 static void rocker_port_link_up(struct rocker_port *rocker_port)
1402 netif_carrier_on(rocker_port->dev);
1403 netdev_info(rocker_port->dev, "Link is up\n");
1406 static void rocker_port_link_down(struct rocker_port *rocker_port)
1408 netif_carrier_off(rocker_port->dev);
1409 netdev_info(rocker_port->dev, "Link is down\n");
1412 static int rocker_event_link_change(struct rocker *rocker,
1413 const struct rocker_tlv *info)
1415 struct rocker_tlv *attrs[ROCKER_TLV_EVENT_LINK_CHANGED_MAX + 1];
1416 unsigned int port_number;
1418 struct rocker_port *rocker_port;
1420 rocker_tlv_parse_nested(attrs, ROCKER_TLV_EVENT_LINK_CHANGED_MAX, info);
1421 if (!attrs[ROCKER_TLV_EVENT_LINK_CHANGED_PPORT] ||
1422 !attrs[ROCKER_TLV_EVENT_LINK_CHANGED_LINKUP])
1425 rocker_tlv_get_u32(attrs[ROCKER_TLV_EVENT_LINK_CHANGED_PPORT]) - 1;
1426 link_up = rocker_tlv_get_u8(attrs[ROCKER_TLV_EVENT_LINK_CHANGED_LINKUP]);
1428 if (port_number >= rocker->port_count)
1431 rocker_port = rocker->ports[port_number];
1432 if (netif_carrier_ok(rocker_port->dev) != link_up) {
1434 rocker_port_link_up(rocker_port);
1436 rocker_port_link_down(rocker_port);
1442 #define ROCKER_OP_FLAG_REMOVE BIT(0)
1443 #define ROCKER_OP_FLAG_LEARNED BIT(1)
1444 #define ROCKER_OP_FLAG_REFRESH BIT(2)
1446 static int rocker_port_fdb(struct rocker_port *rocker_port,
1447 enum switchdev_trans trans,
1448 const unsigned char *addr,
1449 __be16 vlan_id, int flags);
1451 struct rocker_mac_vlan_seen_work {
1452 struct work_struct work;
1453 struct rocker_port *rocker_port;
1455 unsigned char addr[ETH_ALEN];
1459 static void rocker_event_mac_vlan_seen_work(struct work_struct *work)
1461 struct rocker_mac_vlan_seen_work *sw =
1462 container_of(work, struct rocker_mac_vlan_seen_work, work);
1465 rocker_port_fdb(sw->rocker_port, SWITCHDEV_TRANS_NONE,
1466 sw->addr, sw->vlan_id, sw->flags);
1472 static int rocker_event_mac_vlan_seen(struct rocker *rocker,
1473 const struct rocker_tlv *info)
1475 struct rocker_mac_vlan_seen_work *sw;
1476 struct rocker_tlv *attrs[ROCKER_TLV_EVENT_MAC_VLAN_MAX + 1];
1477 unsigned int port_number;
1478 struct rocker_port *rocker_port;
1479 unsigned char *addr;
1480 int flags = ROCKER_OP_FLAG_LEARNED;
1483 rocker_tlv_parse_nested(attrs, ROCKER_TLV_EVENT_MAC_VLAN_MAX, info);
1484 if (!attrs[ROCKER_TLV_EVENT_MAC_VLAN_PPORT] ||
1485 !attrs[ROCKER_TLV_EVENT_MAC_VLAN_MAC] ||
1486 !attrs[ROCKER_TLV_EVENT_MAC_VLAN_VLAN_ID])
1489 rocker_tlv_get_u32(attrs[ROCKER_TLV_EVENT_MAC_VLAN_PPORT]) - 1;
1490 addr = rocker_tlv_data(attrs[ROCKER_TLV_EVENT_MAC_VLAN_MAC]);
1491 vlan_id = rocker_tlv_get_be16(attrs[ROCKER_TLV_EVENT_MAC_VLAN_VLAN_ID]);
1493 if (port_number >= rocker->port_count)
1496 rocker_port = rocker->ports[port_number];
1498 if (rocker_port->stp_state != BR_STATE_LEARNING &&
1499 rocker_port->stp_state != BR_STATE_FORWARDING)
1502 sw = kmalloc(sizeof(*sw), GFP_ATOMIC);
1506 INIT_WORK(&sw->work, rocker_event_mac_vlan_seen_work);
1508 sw->rocker_port = rocker_port;
1510 ether_addr_copy(sw->addr, addr);
1511 sw->vlan_id = vlan_id;
1513 schedule_work(&sw->work);
1518 static int rocker_event_process(struct rocker *rocker,
1519 struct rocker_desc_info *desc_info)
1521 struct rocker_tlv *attrs[ROCKER_TLV_EVENT_MAX + 1];
1522 struct rocker_tlv *info;
1525 rocker_tlv_parse_desc(attrs, ROCKER_TLV_EVENT_MAX, desc_info);
1526 if (!attrs[ROCKER_TLV_EVENT_TYPE] ||
1527 !attrs[ROCKER_TLV_EVENT_INFO])
1530 type = rocker_tlv_get_u16(attrs[ROCKER_TLV_EVENT_TYPE]);
1531 info = attrs[ROCKER_TLV_EVENT_INFO];
1534 case ROCKER_TLV_EVENT_TYPE_LINK_CHANGED:
1535 return rocker_event_link_change(rocker, info);
1536 case ROCKER_TLV_EVENT_TYPE_MAC_VLAN_SEEN:
1537 return rocker_event_mac_vlan_seen(rocker, info);
1543 static irqreturn_t rocker_event_irq_handler(int irq, void *dev_id)
1545 struct rocker *rocker = dev_id;
1546 struct pci_dev *pdev = rocker->pdev;
1547 struct rocker_desc_info *desc_info;
1551 while ((desc_info = rocker_desc_tail_get(&rocker->event_ring))) {
1552 err = rocker_desc_err(desc_info);
1554 dev_err(&pdev->dev, "event desc received with err %d\n",
1557 err = rocker_event_process(rocker, desc_info);
1559 dev_err(&pdev->dev, "event processing failed with err %d\n",
1562 rocker_desc_gen_clear(desc_info);
1563 rocker_desc_head_set(rocker, &rocker->event_ring, desc_info);
1566 rocker_dma_ring_credits_set(rocker, &rocker->event_ring, credits);
1571 static irqreturn_t rocker_tx_irq_handler(int irq, void *dev_id)
1573 struct rocker_port *rocker_port = dev_id;
1575 napi_schedule(&rocker_port->napi_tx);
1579 static irqreturn_t rocker_rx_irq_handler(int irq, void *dev_id)
1581 struct rocker_port *rocker_port = dev_id;
1583 napi_schedule(&rocker_port->napi_rx);
1587 /********************
1589 ********************/
1591 typedef int (*rocker_cmd_cb_t)(struct rocker *rocker,
1592 struct rocker_port *rocker_port,
1593 struct rocker_desc_info *desc_info,
1596 static int rocker_cmd_exec(struct rocker *rocker,
1597 struct rocker_port *rocker_port,
1598 enum switchdev_trans trans,
1599 rocker_cmd_cb_t prepare, void *prepare_priv,
1600 rocker_cmd_cb_t process, void *process_priv)
1602 struct rocker_desc_info *desc_info;
1603 struct rocker_wait *wait;
1604 unsigned long flags;
1607 wait = rocker_wait_create(rocker_port, trans);
1611 spin_lock_irqsave(&rocker->cmd_ring_lock, flags);
1613 desc_info = rocker_desc_head_get(&rocker->cmd_ring);
1615 spin_unlock_irqrestore(&rocker->cmd_ring_lock, flags);
1620 err = prepare(rocker, rocker_port, desc_info, prepare_priv);
1622 spin_unlock_irqrestore(&rocker->cmd_ring_lock, flags);
1626 rocker_desc_cookie_ptr_set(desc_info, wait);
1628 if (trans != SWITCHDEV_TRANS_PREPARE)
1629 rocker_desc_head_set(rocker, &rocker->cmd_ring, desc_info);
1631 spin_unlock_irqrestore(&rocker->cmd_ring_lock, flags);
1633 if (trans != SWITCHDEV_TRANS_PREPARE)
1634 if (!rocker_wait_event_timeout(wait, HZ / 10))
1637 err = rocker_desc_err(desc_info);
1642 err = process(rocker, rocker_port, desc_info, process_priv);
1644 rocker_desc_gen_clear(desc_info);
1646 rocker_wait_destroy(rocker_port, trans, wait);
1651 rocker_cmd_get_port_settings_prep(struct rocker *rocker,
1652 struct rocker_port *rocker_port,
1653 struct rocker_desc_info *desc_info,
1656 struct rocker_tlv *cmd_info;
1658 if (rocker_tlv_put_u16(desc_info, ROCKER_TLV_CMD_TYPE,
1659 ROCKER_TLV_CMD_TYPE_GET_PORT_SETTINGS))
1661 cmd_info = rocker_tlv_nest_start(desc_info, ROCKER_TLV_CMD_INFO);
1664 if (rocker_tlv_put_u32(desc_info, ROCKER_TLV_CMD_PORT_SETTINGS_PPORT,
1665 rocker_port->pport))
1667 rocker_tlv_nest_end(desc_info, cmd_info);
1672 rocker_cmd_get_port_settings_ethtool_proc(struct rocker *rocker,
1673 struct rocker_port *rocker_port,
1674 struct rocker_desc_info *desc_info,
1677 struct ethtool_cmd *ecmd = priv;
1678 struct rocker_tlv *attrs[ROCKER_TLV_CMD_MAX + 1];
1679 struct rocker_tlv *info_attrs[ROCKER_TLV_CMD_PORT_SETTINGS_MAX + 1];
1684 rocker_tlv_parse_desc(attrs, ROCKER_TLV_CMD_MAX, desc_info);
1685 if (!attrs[ROCKER_TLV_CMD_INFO])
1688 rocker_tlv_parse_nested(info_attrs, ROCKER_TLV_CMD_PORT_SETTINGS_MAX,
1689 attrs[ROCKER_TLV_CMD_INFO]);
1690 if (!info_attrs[ROCKER_TLV_CMD_PORT_SETTINGS_SPEED] ||
1691 !info_attrs[ROCKER_TLV_CMD_PORT_SETTINGS_DUPLEX] ||
1692 !info_attrs[ROCKER_TLV_CMD_PORT_SETTINGS_AUTONEG])
1695 speed = rocker_tlv_get_u32(info_attrs[ROCKER_TLV_CMD_PORT_SETTINGS_SPEED]);
1696 duplex = rocker_tlv_get_u8(info_attrs[ROCKER_TLV_CMD_PORT_SETTINGS_DUPLEX]);
1697 autoneg = rocker_tlv_get_u8(info_attrs[ROCKER_TLV_CMD_PORT_SETTINGS_AUTONEG]);
1699 ecmd->transceiver = XCVR_INTERNAL;
1700 ecmd->supported = SUPPORTED_TP;
1701 ecmd->phy_address = 0xff;
1702 ecmd->port = PORT_TP;
1703 ethtool_cmd_speed_set(ecmd, speed);
1704 ecmd->duplex = duplex ? DUPLEX_FULL : DUPLEX_HALF;
1705 ecmd->autoneg = autoneg ? AUTONEG_ENABLE : AUTONEG_DISABLE;
1711 rocker_cmd_get_port_settings_macaddr_proc(struct rocker *rocker,
1712 struct rocker_port *rocker_port,
1713 struct rocker_desc_info *desc_info,
1716 unsigned char *macaddr = priv;
1717 struct rocker_tlv *attrs[ROCKER_TLV_CMD_MAX + 1];
1718 struct rocker_tlv *info_attrs[ROCKER_TLV_CMD_PORT_SETTINGS_MAX + 1];
1719 struct rocker_tlv *attr;
1721 rocker_tlv_parse_desc(attrs, ROCKER_TLV_CMD_MAX, desc_info);
1722 if (!attrs[ROCKER_TLV_CMD_INFO])
1725 rocker_tlv_parse_nested(info_attrs, ROCKER_TLV_CMD_PORT_SETTINGS_MAX,
1726 attrs[ROCKER_TLV_CMD_INFO]);
1727 attr = info_attrs[ROCKER_TLV_CMD_PORT_SETTINGS_MACADDR];
1731 if (rocker_tlv_len(attr) != ETH_ALEN)
1734 ether_addr_copy(macaddr, rocker_tlv_data(attr));
1744 rocker_cmd_get_port_settings_phys_name_proc(struct rocker *rocker,
1745 struct rocker_port *rocker_port,
1746 struct rocker_desc_info *desc_info,
1749 struct rocker_tlv *info_attrs[ROCKER_TLV_CMD_PORT_SETTINGS_MAX + 1];
1750 struct rocker_tlv *attrs[ROCKER_TLV_CMD_MAX + 1];
1751 struct port_name *name = priv;
1752 struct rocker_tlv *attr;
1756 rocker_tlv_parse_desc(attrs, ROCKER_TLV_CMD_MAX, desc_info);
1757 if (!attrs[ROCKER_TLV_CMD_INFO])
1760 rocker_tlv_parse_nested(info_attrs, ROCKER_TLV_CMD_PORT_SETTINGS_MAX,
1761 attrs[ROCKER_TLV_CMD_INFO]);
1762 attr = info_attrs[ROCKER_TLV_CMD_PORT_SETTINGS_PHYS_NAME];
1766 len = min_t(size_t, rocker_tlv_len(attr), name->len);
1767 str = rocker_tlv_data(attr);
1769 /* make sure name only contains alphanumeric characters */
1770 for (i = j = 0; i < len; ++i) {
1771 if (isalnum(str[i])) {
1772 name->buf[j] = str[i];
1780 name->buf[j] = '\0';
1786 rocker_cmd_set_port_settings_ethtool_prep(struct rocker *rocker,
1787 struct rocker_port *rocker_port,
1788 struct rocker_desc_info *desc_info,
1791 struct ethtool_cmd *ecmd = priv;
1792 struct rocker_tlv *cmd_info;
1794 if (rocker_tlv_put_u16(desc_info, ROCKER_TLV_CMD_TYPE,
1795 ROCKER_TLV_CMD_TYPE_SET_PORT_SETTINGS))
1797 cmd_info = rocker_tlv_nest_start(desc_info, ROCKER_TLV_CMD_INFO);
1800 if (rocker_tlv_put_u32(desc_info, ROCKER_TLV_CMD_PORT_SETTINGS_PPORT,
1801 rocker_port->pport))
1803 if (rocker_tlv_put_u32(desc_info, ROCKER_TLV_CMD_PORT_SETTINGS_SPEED,
1804 ethtool_cmd_speed(ecmd)))
1806 if (rocker_tlv_put_u8(desc_info, ROCKER_TLV_CMD_PORT_SETTINGS_DUPLEX,
1809 if (rocker_tlv_put_u8(desc_info, ROCKER_TLV_CMD_PORT_SETTINGS_AUTONEG,
1812 rocker_tlv_nest_end(desc_info, cmd_info);
1817 rocker_cmd_set_port_settings_macaddr_prep(struct rocker *rocker,
1818 struct rocker_port *rocker_port,
1819 struct rocker_desc_info *desc_info,
1822 unsigned char *macaddr = priv;
1823 struct rocker_tlv *cmd_info;
1825 if (rocker_tlv_put_u16(desc_info, ROCKER_TLV_CMD_TYPE,
1826 ROCKER_TLV_CMD_TYPE_SET_PORT_SETTINGS))
1828 cmd_info = rocker_tlv_nest_start(desc_info, ROCKER_TLV_CMD_INFO);
1831 if (rocker_tlv_put_u32(desc_info, ROCKER_TLV_CMD_PORT_SETTINGS_PPORT,
1832 rocker_port->pport))
1834 if (rocker_tlv_put(desc_info, ROCKER_TLV_CMD_PORT_SETTINGS_MACADDR,
1837 rocker_tlv_nest_end(desc_info, cmd_info);
1842 rocker_cmd_set_port_learning_prep(struct rocker *rocker,
1843 struct rocker_port *rocker_port,
1844 struct rocker_desc_info *desc_info,
1847 struct rocker_tlv *cmd_info;
1849 if (rocker_tlv_put_u16(desc_info, ROCKER_TLV_CMD_TYPE,
1850 ROCKER_TLV_CMD_TYPE_SET_PORT_SETTINGS))
1852 cmd_info = rocker_tlv_nest_start(desc_info, ROCKER_TLV_CMD_INFO);
1855 if (rocker_tlv_put_u32(desc_info, ROCKER_TLV_CMD_PORT_SETTINGS_PPORT,
1856 rocker_port->pport))
1858 if (rocker_tlv_put_u8(desc_info, ROCKER_TLV_CMD_PORT_SETTINGS_LEARNING,
1859 !!(rocker_port->brport_flags & BR_LEARNING)))
1861 rocker_tlv_nest_end(desc_info, cmd_info);
1865 static int rocker_cmd_get_port_settings_ethtool(struct rocker_port *rocker_port,
1866 struct ethtool_cmd *ecmd)
1868 return rocker_cmd_exec(rocker_port->rocker, rocker_port,
1869 SWITCHDEV_TRANS_NONE,
1870 rocker_cmd_get_port_settings_prep, NULL,
1871 rocker_cmd_get_port_settings_ethtool_proc,
1875 static int rocker_cmd_get_port_settings_macaddr(struct rocker_port *rocker_port,
1876 unsigned char *macaddr)
1878 return rocker_cmd_exec(rocker_port->rocker, rocker_port,
1879 SWITCHDEV_TRANS_NONE,
1880 rocker_cmd_get_port_settings_prep, NULL,
1881 rocker_cmd_get_port_settings_macaddr_proc,
1885 static int rocker_cmd_set_port_settings_ethtool(struct rocker_port *rocker_port,
1886 struct ethtool_cmd *ecmd)
1888 return rocker_cmd_exec(rocker_port->rocker, rocker_port,
1889 SWITCHDEV_TRANS_NONE,
1890 rocker_cmd_set_port_settings_ethtool_prep,
1894 static int rocker_cmd_set_port_settings_macaddr(struct rocker_port *rocker_port,
1895 unsigned char *macaddr)
1897 return rocker_cmd_exec(rocker_port->rocker, rocker_port,
1898 SWITCHDEV_TRANS_NONE,
1899 rocker_cmd_set_port_settings_macaddr_prep,
1900 macaddr, NULL, NULL);
1903 static int rocker_port_set_learning(struct rocker_port *rocker_port,
1904 enum switchdev_trans trans)
1906 return rocker_cmd_exec(rocker_port->rocker, rocker_port, trans,
1907 rocker_cmd_set_port_learning_prep,
1911 static int rocker_cmd_flow_tbl_add_ig_port(struct rocker_desc_info *desc_info,
1912 struct rocker_flow_tbl_entry *entry)
1914 if (rocker_tlv_put_u32(desc_info, ROCKER_TLV_OF_DPA_IN_PPORT,
1915 entry->key.ig_port.in_pport))
1917 if (rocker_tlv_put_u32(desc_info, ROCKER_TLV_OF_DPA_IN_PPORT_MASK,
1918 entry->key.ig_port.in_pport_mask))
1920 if (rocker_tlv_put_u16(desc_info, ROCKER_TLV_OF_DPA_GOTO_TABLE_ID,
1921 entry->key.ig_port.goto_tbl))
1927 static int rocker_cmd_flow_tbl_add_vlan(struct rocker_desc_info *desc_info,
1928 struct rocker_flow_tbl_entry *entry)
1930 if (rocker_tlv_put_u32(desc_info, ROCKER_TLV_OF_DPA_IN_PPORT,
1931 entry->key.vlan.in_pport))
1933 if (rocker_tlv_put_be16(desc_info, ROCKER_TLV_OF_DPA_VLAN_ID,
1934 entry->key.vlan.vlan_id))
1936 if (rocker_tlv_put_be16(desc_info, ROCKER_TLV_OF_DPA_VLAN_ID_MASK,
1937 entry->key.vlan.vlan_id_mask))
1939 if (rocker_tlv_put_u16(desc_info, ROCKER_TLV_OF_DPA_GOTO_TABLE_ID,
1940 entry->key.vlan.goto_tbl))
1942 if (entry->key.vlan.untagged &&
1943 rocker_tlv_put_be16(desc_info, ROCKER_TLV_OF_DPA_NEW_VLAN_ID,
1944 entry->key.vlan.new_vlan_id))
1950 static int rocker_cmd_flow_tbl_add_term_mac(struct rocker_desc_info *desc_info,
1951 struct rocker_flow_tbl_entry *entry)
1953 if (rocker_tlv_put_u32(desc_info, ROCKER_TLV_OF_DPA_IN_PPORT,
1954 entry->key.term_mac.in_pport))
1956 if (rocker_tlv_put_u32(desc_info, ROCKER_TLV_OF_DPA_IN_PPORT_MASK,
1957 entry->key.term_mac.in_pport_mask))
1959 if (rocker_tlv_put_be16(desc_info, ROCKER_TLV_OF_DPA_ETHERTYPE,
1960 entry->key.term_mac.eth_type))
1962 if (rocker_tlv_put(desc_info, ROCKER_TLV_OF_DPA_DST_MAC,
1963 ETH_ALEN, entry->key.term_mac.eth_dst))
1965 if (rocker_tlv_put(desc_info, ROCKER_TLV_OF_DPA_DST_MAC_MASK,
1966 ETH_ALEN, entry->key.term_mac.eth_dst_mask))
1968 if (rocker_tlv_put_be16(desc_info, ROCKER_TLV_OF_DPA_VLAN_ID,
1969 entry->key.term_mac.vlan_id))
1971 if (rocker_tlv_put_be16(desc_info, ROCKER_TLV_OF_DPA_VLAN_ID_MASK,
1972 entry->key.term_mac.vlan_id_mask))
1974 if (rocker_tlv_put_u16(desc_info, ROCKER_TLV_OF_DPA_GOTO_TABLE_ID,
1975 entry->key.term_mac.goto_tbl))
1977 if (entry->key.term_mac.copy_to_cpu &&
1978 rocker_tlv_put_u8(desc_info, ROCKER_TLV_OF_DPA_COPY_CPU_ACTION,
1979 entry->key.term_mac.copy_to_cpu))
1986 rocker_cmd_flow_tbl_add_ucast_routing(struct rocker_desc_info *desc_info,
1987 struct rocker_flow_tbl_entry *entry)
1989 if (rocker_tlv_put_be16(desc_info, ROCKER_TLV_OF_DPA_ETHERTYPE,
1990 entry->key.ucast_routing.eth_type))
1992 if (rocker_tlv_put_be32(desc_info, ROCKER_TLV_OF_DPA_DST_IP,
1993 entry->key.ucast_routing.dst4))
1995 if (rocker_tlv_put_be32(desc_info, ROCKER_TLV_OF_DPA_DST_IP_MASK,
1996 entry->key.ucast_routing.dst4_mask))
1998 if (rocker_tlv_put_u16(desc_info, ROCKER_TLV_OF_DPA_GOTO_TABLE_ID,
1999 entry->key.ucast_routing.goto_tbl))
2001 if (rocker_tlv_put_u32(desc_info, ROCKER_TLV_OF_DPA_GROUP_ID,
2002 entry->key.ucast_routing.group_id))
2008 static int rocker_cmd_flow_tbl_add_bridge(struct rocker_desc_info *desc_info,
2009 struct rocker_flow_tbl_entry *entry)
2011 if (entry->key.bridge.has_eth_dst &&
2012 rocker_tlv_put(desc_info, ROCKER_TLV_OF_DPA_DST_MAC,
2013 ETH_ALEN, entry->key.bridge.eth_dst))
2015 if (entry->key.bridge.has_eth_dst_mask &&
2016 rocker_tlv_put(desc_info, ROCKER_TLV_OF_DPA_DST_MAC_MASK,
2017 ETH_ALEN, entry->key.bridge.eth_dst_mask))
2019 if (entry->key.bridge.vlan_id &&
2020 rocker_tlv_put_be16(desc_info, ROCKER_TLV_OF_DPA_VLAN_ID,
2021 entry->key.bridge.vlan_id))
2023 if (entry->key.bridge.tunnel_id &&
2024 rocker_tlv_put_u32(desc_info, ROCKER_TLV_OF_DPA_TUNNEL_ID,
2025 entry->key.bridge.tunnel_id))
2027 if (rocker_tlv_put_u16(desc_info, ROCKER_TLV_OF_DPA_GOTO_TABLE_ID,
2028 entry->key.bridge.goto_tbl))
2030 if (rocker_tlv_put_u32(desc_info, ROCKER_TLV_OF_DPA_GROUP_ID,
2031 entry->key.bridge.group_id))
2033 if (entry->key.bridge.copy_to_cpu &&
2034 rocker_tlv_put_u8(desc_info, ROCKER_TLV_OF_DPA_COPY_CPU_ACTION,
2035 entry->key.bridge.copy_to_cpu))
2041 static int rocker_cmd_flow_tbl_add_acl(struct rocker_desc_info *desc_info,
2042 struct rocker_flow_tbl_entry *entry)
2044 if (rocker_tlv_put_u32(desc_info, ROCKER_TLV_OF_DPA_IN_PPORT,
2045 entry->key.acl.in_pport))
2047 if (rocker_tlv_put_u32(desc_info, ROCKER_TLV_OF_DPA_IN_PPORT_MASK,
2048 entry->key.acl.in_pport_mask))
2050 if (rocker_tlv_put(desc_info, ROCKER_TLV_OF_DPA_SRC_MAC,
2051 ETH_ALEN, entry->key.acl.eth_src))
2053 if (rocker_tlv_put(desc_info, ROCKER_TLV_OF_DPA_SRC_MAC_MASK,
2054 ETH_ALEN, entry->key.acl.eth_src_mask))
2056 if (rocker_tlv_put(desc_info, ROCKER_TLV_OF_DPA_DST_MAC,
2057 ETH_ALEN, entry->key.acl.eth_dst))
2059 if (rocker_tlv_put(desc_info, ROCKER_TLV_OF_DPA_DST_MAC_MASK,
2060 ETH_ALEN, entry->key.acl.eth_dst_mask))
2062 if (rocker_tlv_put_be16(desc_info, ROCKER_TLV_OF_DPA_ETHERTYPE,
2063 entry->key.acl.eth_type))
2065 if (rocker_tlv_put_be16(desc_info, ROCKER_TLV_OF_DPA_VLAN_ID,
2066 entry->key.acl.vlan_id))
2068 if (rocker_tlv_put_be16(desc_info, ROCKER_TLV_OF_DPA_VLAN_ID_MASK,
2069 entry->key.acl.vlan_id_mask))
2072 switch (ntohs(entry->key.acl.eth_type)) {
2075 if (rocker_tlv_put_u8(desc_info, ROCKER_TLV_OF_DPA_IP_PROTO,
2076 entry->key.acl.ip_proto))
2078 if (rocker_tlv_put_u8(desc_info,
2079 ROCKER_TLV_OF_DPA_IP_PROTO_MASK,
2080 entry->key.acl.ip_proto_mask))
2082 if (rocker_tlv_put_u8(desc_info, ROCKER_TLV_OF_DPA_IP_DSCP,
2083 entry->key.acl.ip_tos & 0x3f))
2085 if (rocker_tlv_put_u8(desc_info,
2086 ROCKER_TLV_OF_DPA_IP_DSCP_MASK,
2087 entry->key.acl.ip_tos_mask & 0x3f))
2089 if (rocker_tlv_put_u8(desc_info, ROCKER_TLV_OF_DPA_IP_ECN,
2090 (entry->key.acl.ip_tos & 0xc0) >> 6))
2092 if (rocker_tlv_put_u8(desc_info,
2093 ROCKER_TLV_OF_DPA_IP_ECN_MASK,
2094 (entry->key.acl.ip_tos_mask & 0xc0) >> 6))
2099 if (entry->key.acl.group_id != ROCKER_GROUP_NONE &&
2100 rocker_tlv_put_u32(desc_info, ROCKER_TLV_OF_DPA_GROUP_ID,
2101 entry->key.acl.group_id))
2107 static int rocker_cmd_flow_tbl_add(struct rocker *rocker,
2108 struct rocker_port *rocker_port,
2109 struct rocker_desc_info *desc_info,
2112 struct rocker_flow_tbl_entry *entry = priv;
2113 struct rocker_tlv *cmd_info;
2116 if (rocker_tlv_put_u16(desc_info, ROCKER_TLV_CMD_TYPE, entry->cmd))
2118 cmd_info = rocker_tlv_nest_start(desc_info, ROCKER_TLV_CMD_INFO);
2121 if (rocker_tlv_put_u16(desc_info, ROCKER_TLV_OF_DPA_TABLE_ID,
2124 if (rocker_tlv_put_u32(desc_info, ROCKER_TLV_OF_DPA_PRIORITY,
2125 entry->key.priority))
2127 if (rocker_tlv_put_u32(desc_info, ROCKER_TLV_OF_DPA_HARDTIME, 0))
2129 if (rocker_tlv_put_u64(desc_info, ROCKER_TLV_OF_DPA_COOKIE,
2133 switch (entry->key.tbl_id) {
2134 case ROCKER_OF_DPA_TABLE_ID_INGRESS_PORT:
2135 err = rocker_cmd_flow_tbl_add_ig_port(desc_info, entry);
2137 case ROCKER_OF_DPA_TABLE_ID_VLAN:
2138 err = rocker_cmd_flow_tbl_add_vlan(desc_info, entry);
2140 case ROCKER_OF_DPA_TABLE_ID_TERMINATION_MAC:
2141 err = rocker_cmd_flow_tbl_add_term_mac(desc_info, entry);
2143 case ROCKER_OF_DPA_TABLE_ID_UNICAST_ROUTING:
2144 err = rocker_cmd_flow_tbl_add_ucast_routing(desc_info, entry);
2146 case ROCKER_OF_DPA_TABLE_ID_BRIDGING:
2147 err = rocker_cmd_flow_tbl_add_bridge(desc_info, entry);
2149 case ROCKER_OF_DPA_TABLE_ID_ACL_POLICY:
2150 err = rocker_cmd_flow_tbl_add_acl(desc_info, entry);
2160 rocker_tlv_nest_end(desc_info, cmd_info);
2165 static int rocker_cmd_flow_tbl_del(struct rocker *rocker,
2166 struct rocker_port *rocker_port,
2167 struct rocker_desc_info *desc_info,
2170 const struct rocker_flow_tbl_entry *entry = priv;
2171 struct rocker_tlv *cmd_info;
2173 if (rocker_tlv_put_u16(desc_info, ROCKER_TLV_CMD_TYPE, entry->cmd))
2175 cmd_info = rocker_tlv_nest_start(desc_info, ROCKER_TLV_CMD_INFO);
2178 if (rocker_tlv_put_u64(desc_info, ROCKER_TLV_OF_DPA_COOKIE,
2181 rocker_tlv_nest_end(desc_info, cmd_info);
2187 rocker_cmd_group_tbl_add_l2_interface(struct rocker_desc_info *desc_info,
2188 struct rocker_group_tbl_entry *entry)
2190 if (rocker_tlv_put_u32(desc_info, ROCKER_TLV_OF_DPA_OUT_PPORT,
2191 ROCKER_GROUP_PORT_GET(entry->group_id)))
2193 if (rocker_tlv_put_u8(desc_info, ROCKER_TLV_OF_DPA_POP_VLAN,
2194 entry->l2_interface.pop_vlan))
2201 rocker_cmd_group_tbl_add_l2_rewrite(struct rocker_desc_info *desc_info,
2202 struct rocker_group_tbl_entry *entry)
2204 if (rocker_tlv_put_u32(desc_info, ROCKER_TLV_OF_DPA_GROUP_ID_LOWER,
2205 entry->l2_rewrite.group_id))
2207 if (!is_zero_ether_addr(entry->l2_rewrite.eth_src) &&
2208 rocker_tlv_put(desc_info, ROCKER_TLV_OF_DPA_SRC_MAC,
2209 ETH_ALEN, entry->l2_rewrite.eth_src))
2211 if (!is_zero_ether_addr(entry->l2_rewrite.eth_dst) &&
2212 rocker_tlv_put(desc_info, ROCKER_TLV_OF_DPA_DST_MAC,
2213 ETH_ALEN, entry->l2_rewrite.eth_dst))
2215 if (entry->l2_rewrite.vlan_id &&
2216 rocker_tlv_put_be16(desc_info, ROCKER_TLV_OF_DPA_VLAN_ID,
2217 entry->l2_rewrite.vlan_id))
2224 rocker_cmd_group_tbl_add_group_ids(struct rocker_desc_info *desc_info,
2225 struct rocker_group_tbl_entry *entry)
2228 struct rocker_tlv *group_ids;
2230 if (rocker_tlv_put_u16(desc_info, ROCKER_TLV_OF_DPA_GROUP_COUNT,
2231 entry->group_count))
2234 group_ids = rocker_tlv_nest_start(desc_info,
2235 ROCKER_TLV_OF_DPA_GROUP_IDS);
2239 for (i = 0; i < entry->group_count; i++)
2240 /* Note TLV array is 1-based */
2241 if (rocker_tlv_put_u32(desc_info, i + 1, entry->group_ids[i]))
2244 rocker_tlv_nest_end(desc_info, group_ids);
2250 rocker_cmd_group_tbl_add_l3_unicast(struct rocker_desc_info *desc_info,
2251 struct rocker_group_tbl_entry *entry)
2253 if (!is_zero_ether_addr(entry->l3_unicast.eth_src) &&
2254 rocker_tlv_put(desc_info, ROCKER_TLV_OF_DPA_SRC_MAC,
2255 ETH_ALEN, entry->l3_unicast.eth_src))
2257 if (!is_zero_ether_addr(entry->l3_unicast.eth_dst) &&
2258 rocker_tlv_put(desc_info, ROCKER_TLV_OF_DPA_DST_MAC,
2259 ETH_ALEN, entry->l3_unicast.eth_dst))
2261 if (entry->l3_unicast.vlan_id &&
2262 rocker_tlv_put_be16(desc_info, ROCKER_TLV_OF_DPA_VLAN_ID,
2263 entry->l3_unicast.vlan_id))
2265 if (rocker_tlv_put_u8(desc_info, ROCKER_TLV_OF_DPA_TTL_CHECK,
2266 entry->l3_unicast.ttl_check))
2268 if (rocker_tlv_put_u32(desc_info, ROCKER_TLV_OF_DPA_GROUP_ID_LOWER,
2269 entry->l3_unicast.group_id))
2275 static int rocker_cmd_group_tbl_add(struct rocker *rocker,
2276 struct rocker_port *rocker_port,
2277 struct rocker_desc_info *desc_info,
2280 struct rocker_group_tbl_entry *entry = priv;
2281 struct rocker_tlv *cmd_info;
2284 if (rocker_tlv_put_u16(desc_info, ROCKER_TLV_CMD_TYPE, entry->cmd))
2286 cmd_info = rocker_tlv_nest_start(desc_info, ROCKER_TLV_CMD_INFO);
2290 if (rocker_tlv_put_u32(desc_info, ROCKER_TLV_OF_DPA_GROUP_ID,
2294 switch (ROCKER_GROUP_TYPE_GET(entry->group_id)) {
2295 case ROCKER_OF_DPA_GROUP_TYPE_L2_INTERFACE:
2296 err = rocker_cmd_group_tbl_add_l2_interface(desc_info, entry);
2298 case ROCKER_OF_DPA_GROUP_TYPE_L2_REWRITE:
2299 err = rocker_cmd_group_tbl_add_l2_rewrite(desc_info, entry);
2301 case ROCKER_OF_DPA_GROUP_TYPE_L2_FLOOD:
2302 case ROCKER_OF_DPA_GROUP_TYPE_L2_MCAST:
2303 err = rocker_cmd_group_tbl_add_group_ids(desc_info, entry);
2305 case ROCKER_OF_DPA_GROUP_TYPE_L3_UCAST:
2306 err = rocker_cmd_group_tbl_add_l3_unicast(desc_info, entry);
2316 rocker_tlv_nest_end(desc_info, cmd_info);
2321 static int rocker_cmd_group_tbl_del(struct rocker *rocker,
2322 struct rocker_port *rocker_port,
2323 struct rocker_desc_info *desc_info,
2326 const struct rocker_group_tbl_entry *entry = priv;
2327 struct rocker_tlv *cmd_info;
2329 if (rocker_tlv_put_u16(desc_info, ROCKER_TLV_CMD_TYPE, entry->cmd))
2331 cmd_info = rocker_tlv_nest_start(desc_info, ROCKER_TLV_CMD_INFO);
2334 if (rocker_tlv_put_u32(desc_info, ROCKER_TLV_OF_DPA_GROUP_ID,
2337 rocker_tlv_nest_end(desc_info, cmd_info);
2342 /***************************************************
2343 * Flow, group, FDB, internal VLAN and neigh tables
2344 ***************************************************/
2346 static int rocker_init_tbls(struct rocker *rocker)
2348 hash_init(rocker->flow_tbl);
2349 spin_lock_init(&rocker->flow_tbl_lock);
2351 hash_init(rocker->group_tbl);
2352 spin_lock_init(&rocker->group_tbl_lock);
2354 hash_init(rocker->fdb_tbl);
2355 spin_lock_init(&rocker->fdb_tbl_lock);
2357 hash_init(rocker->internal_vlan_tbl);
2358 spin_lock_init(&rocker->internal_vlan_tbl_lock);
2360 hash_init(rocker->neigh_tbl);
2361 spin_lock_init(&rocker->neigh_tbl_lock);
2366 static void rocker_free_tbls(struct rocker *rocker)
2368 unsigned long flags;
2369 struct rocker_flow_tbl_entry *flow_entry;
2370 struct rocker_group_tbl_entry *group_entry;
2371 struct rocker_fdb_tbl_entry *fdb_entry;
2372 struct rocker_internal_vlan_tbl_entry *internal_vlan_entry;
2373 struct rocker_neigh_tbl_entry *neigh_entry;
2374 struct hlist_node *tmp;
2377 spin_lock_irqsave(&rocker->flow_tbl_lock, flags);
2378 hash_for_each_safe(rocker->flow_tbl, bkt, tmp, flow_entry, entry)
2379 hash_del(&flow_entry->entry);
2380 spin_unlock_irqrestore(&rocker->flow_tbl_lock, flags);
2382 spin_lock_irqsave(&rocker->group_tbl_lock, flags);
2383 hash_for_each_safe(rocker->group_tbl, bkt, tmp, group_entry, entry)
2384 hash_del(&group_entry->entry);
2385 spin_unlock_irqrestore(&rocker->group_tbl_lock, flags);
2387 spin_lock_irqsave(&rocker->fdb_tbl_lock, flags);
2388 hash_for_each_safe(rocker->fdb_tbl, bkt, tmp, fdb_entry, entry)
2389 hash_del(&fdb_entry->entry);
2390 spin_unlock_irqrestore(&rocker->fdb_tbl_lock, flags);
2392 spin_lock_irqsave(&rocker->internal_vlan_tbl_lock, flags);
2393 hash_for_each_safe(rocker->internal_vlan_tbl, bkt,
2394 tmp, internal_vlan_entry, entry)
2395 hash_del(&internal_vlan_entry->entry);
2396 spin_unlock_irqrestore(&rocker->internal_vlan_tbl_lock, flags);
2398 spin_lock_irqsave(&rocker->neigh_tbl_lock, flags);
2399 hash_for_each_safe(rocker->neigh_tbl, bkt, tmp, neigh_entry, entry)
2400 hash_del(&neigh_entry->entry);
2401 spin_unlock_irqrestore(&rocker->neigh_tbl_lock, flags);
2404 static struct rocker_flow_tbl_entry *
2405 rocker_flow_tbl_find(struct rocker *rocker, struct rocker_flow_tbl_entry *match)
2407 struct rocker_flow_tbl_entry *found;
2408 size_t key_len = match->key_len ? match->key_len : sizeof(found->key);
2410 hash_for_each_possible(rocker->flow_tbl, found,
2411 entry, match->key_crc32) {
2412 if (memcmp(&found->key, &match->key, key_len) == 0)
2419 static int rocker_flow_tbl_add(struct rocker_port *rocker_port,
2420 enum switchdev_trans trans,
2421 struct rocker_flow_tbl_entry *match)
2423 struct rocker *rocker = rocker_port->rocker;
2424 struct rocker_flow_tbl_entry *found;
2425 size_t key_len = match->key_len ? match->key_len : sizeof(found->key);
2426 unsigned long flags;
2428 match->key_crc32 = crc32(~0, &match->key, key_len);
2430 spin_lock_irqsave(&rocker->flow_tbl_lock, flags);
2432 found = rocker_flow_tbl_find(rocker, match);
2435 match->cookie = found->cookie;
2436 if (trans != SWITCHDEV_TRANS_PREPARE)
2437 hash_del(&found->entry);
2438 rocker_port_kfree(rocker_port, trans, found);
2440 found->cmd = ROCKER_TLV_CMD_TYPE_OF_DPA_FLOW_MOD;
2443 found->cookie = rocker->flow_tbl_next_cookie++;
2444 found->cmd = ROCKER_TLV_CMD_TYPE_OF_DPA_FLOW_ADD;
2447 if (trans != SWITCHDEV_TRANS_PREPARE)
2448 hash_add(rocker->flow_tbl, &found->entry, found->key_crc32);
2450 spin_unlock_irqrestore(&rocker->flow_tbl_lock, flags);
2452 return rocker_cmd_exec(rocker, rocker_port, trans,
2453 rocker_cmd_flow_tbl_add,
2457 static int rocker_flow_tbl_del(struct rocker_port *rocker_port,
2458 enum switchdev_trans trans,
2459 struct rocker_flow_tbl_entry *match)
2461 struct rocker *rocker = rocker_port->rocker;
2462 struct rocker_flow_tbl_entry *found;
2463 size_t key_len = match->key_len ? match->key_len : sizeof(found->key);
2464 unsigned long flags;
2467 match->key_crc32 = crc32(~0, &match->key, key_len);
2469 spin_lock_irqsave(&rocker->flow_tbl_lock, flags);
2471 found = rocker_flow_tbl_find(rocker, match);
2474 if (trans != SWITCHDEV_TRANS_PREPARE)
2475 hash_del(&found->entry);
2476 found->cmd = ROCKER_TLV_CMD_TYPE_OF_DPA_FLOW_DEL;
2479 spin_unlock_irqrestore(&rocker->flow_tbl_lock, flags);
2481 rocker_port_kfree(rocker_port, trans, match);
2484 err = rocker_cmd_exec(rocker, rocker_port, trans,
2485 rocker_cmd_flow_tbl_del,
2487 rocker_port_kfree(rocker_port, trans, found);
2493 static int rocker_flow_tbl_do(struct rocker_port *rocker_port,
2494 enum switchdev_trans trans, int flags,
2495 struct rocker_flow_tbl_entry *entry)
2497 if (flags & ROCKER_OP_FLAG_REMOVE)
2498 return rocker_flow_tbl_del(rocker_port, trans, entry);
2500 return rocker_flow_tbl_add(rocker_port, trans, entry);
2503 static int rocker_flow_tbl_ig_port(struct rocker_port *rocker_port,
2504 enum switchdev_trans trans, int flags,
2505 u32 in_pport, u32 in_pport_mask,
2506 enum rocker_of_dpa_table_id goto_tbl)
2508 struct rocker_flow_tbl_entry *entry;
2510 entry = rocker_port_kzalloc(rocker_port, trans, sizeof(*entry));
2514 entry->key.priority = ROCKER_PRIORITY_IG_PORT;
2515 entry->key.tbl_id = ROCKER_OF_DPA_TABLE_ID_INGRESS_PORT;
2516 entry->key.ig_port.in_pport = in_pport;
2517 entry->key.ig_port.in_pport_mask = in_pport_mask;
2518 entry->key.ig_port.goto_tbl = goto_tbl;
2520 return rocker_flow_tbl_do(rocker_port, trans, flags, entry);
2523 static int rocker_flow_tbl_vlan(struct rocker_port *rocker_port,
2524 enum switchdev_trans trans, int flags,
2525 u32 in_pport, __be16 vlan_id,
2526 __be16 vlan_id_mask,
2527 enum rocker_of_dpa_table_id goto_tbl,
2528 bool untagged, __be16 new_vlan_id)
2530 struct rocker_flow_tbl_entry *entry;
2532 entry = rocker_port_kzalloc(rocker_port, trans, sizeof(*entry));
2536 entry->key.priority = ROCKER_PRIORITY_VLAN;
2537 entry->key.tbl_id = ROCKER_OF_DPA_TABLE_ID_VLAN;
2538 entry->key.vlan.in_pport = in_pport;
2539 entry->key.vlan.vlan_id = vlan_id;
2540 entry->key.vlan.vlan_id_mask = vlan_id_mask;
2541 entry->key.vlan.goto_tbl = goto_tbl;
2543 entry->key.vlan.untagged = untagged;
2544 entry->key.vlan.new_vlan_id = new_vlan_id;
2546 return rocker_flow_tbl_do(rocker_port, trans, flags, entry);
2549 static int rocker_flow_tbl_term_mac(struct rocker_port *rocker_port,
2550 enum switchdev_trans trans,
2551 u32 in_pport, u32 in_pport_mask,
2552 __be16 eth_type, const u8 *eth_dst,
2553 const u8 *eth_dst_mask, __be16 vlan_id,
2554 __be16 vlan_id_mask, bool copy_to_cpu,
2557 struct rocker_flow_tbl_entry *entry;
2559 entry = rocker_port_kzalloc(rocker_port, trans, sizeof(*entry));
2563 if (is_multicast_ether_addr(eth_dst)) {
2564 entry->key.priority = ROCKER_PRIORITY_TERM_MAC_MCAST;
2565 entry->key.term_mac.goto_tbl =
2566 ROCKER_OF_DPA_TABLE_ID_MULTICAST_ROUTING;
2568 entry->key.priority = ROCKER_PRIORITY_TERM_MAC_UCAST;
2569 entry->key.term_mac.goto_tbl =
2570 ROCKER_OF_DPA_TABLE_ID_UNICAST_ROUTING;
2573 entry->key.tbl_id = ROCKER_OF_DPA_TABLE_ID_TERMINATION_MAC;
2574 entry->key.term_mac.in_pport = in_pport;
2575 entry->key.term_mac.in_pport_mask = in_pport_mask;
2576 entry->key.term_mac.eth_type = eth_type;
2577 ether_addr_copy(entry->key.term_mac.eth_dst, eth_dst);
2578 ether_addr_copy(entry->key.term_mac.eth_dst_mask, eth_dst_mask);
2579 entry->key.term_mac.vlan_id = vlan_id;
2580 entry->key.term_mac.vlan_id_mask = vlan_id_mask;
2581 entry->key.term_mac.copy_to_cpu = copy_to_cpu;
2583 return rocker_flow_tbl_do(rocker_port, trans, flags, entry);
2586 static int rocker_flow_tbl_bridge(struct rocker_port *rocker_port,
2587 enum switchdev_trans trans, int flags,
2588 const u8 *eth_dst, const u8 *eth_dst_mask,
2589 __be16 vlan_id, u32 tunnel_id,
2590 enum rocker_of_dpa_table_id goto_tbl,
2591 u32 group_id, bool copy_to_cpu)
2593 struct rocker_flow_tbl_entry *entry;
2595 bool vlan_bridging = !!vlan_id;
2596 bool dflt = !eth_dst || (eth_dst && eth_dst_mask);
2599 entry = rocker_port_kzalloc(rocker_port, trans, sizeof(*entry));
2603 entry->key.tbl_id = ROCKER_OF_DPA_TABLE_ID_BRIDGING;
2606 entry->key.bridge.has_eth_dst = 1;
2607 ether_addr_copy(entry->key.bridge.eth_dst, eth_dst);
2610 entry->key.bridge.has_eth_dst_mask = 1;
2611 ether_addr_copy(entry->key.bridge.eth_dst_mask, eth_dst_mask);
2612 if (!ether_addr_equal(eth_dst_mask, ff_mac))
2616 priority = ROCKER_PRIORITY_UNKNOWN;
2617 if (vlan_bridging && dflt && wild)
2618 priority = ROCKER_PRIORITY_BRIDGING_VLAN_DFLT_WILD;
2619 else if (vlan_bridging && dflt && !wild)
2620 priority = ROCKER_PRIORITY_BRIDGING_VLAN_DFLT_EXACT;
2621 else if (vlan_bridging && !dflt)
2622 priority = ROCKER_PRIORITY_BRIDGING_VLAN;
2623 else if (!vlan_bridging && dflt && wild)
2624 priority = ROCKER_PRIORITY_BRIDGING_TENANT_DFLT_WILD;
2625 else if (!vlan_bridging && dflt && !wild)
2626 priority = ROCKER_PRIORITY_BRIDGING_TENANT_DFLT_EXACT;
2627 else if (!vlan_bridging && !dflt)
2628 priority = ROCKER_PRIORITY_BRIDGING_TENANT;
2630 entry->key.priority = priority;
2631 entry->key.bridge.vlan_id = vlan_id;
2632 entry->key.bridge.tunnel_id = tunnel_id;
2633 entry->key.bridge.goto_tbl = goto_tbl;
2634 entry->key.bridge.group_id = group_id;
2635 entry->key.bridge.copy_to_cpu = copy_to_cpu;
2637 return rocker_flow_tbl_do(rocker_port, trans, flags, entry);
2640 static int rocker_flow_tbl_ucast4_routing(struct rocker_port *rocker_port,
2641 enum switchdev_trans trans,
2642 __be16 eth_type, __be32 dst,
2643 __be32 dst_mask, u32 priority,
2644 enum rocker_of_dpa_table_id goto_tbl,
2645 u32 group_id, int flags)
2647 struct rocker_flow_tbl_entry *entry;
2649 entry = rocker_port_kzalloc(rocker_port, trans, sizeof(*entry));
2653 entry->key.tbl_id = ROCKER_OF_DPA_TABLE_ID_UNICAST_ROUTING;
2654 entry->key.priority = priority;
2655 entry->key.ucast_routing.eth_type = eth_type;
2656 entry->key.ucast_routing.dst4 = dst;
2657 entry->key.ucast_routing.dst4_mask = dst_mask;
2658 entry->key.ucast_routing.goto_tbl = goto_tbl;
2659 entry->key.ucast_routing.group_id = group_id;
2660 entry->key_len = offsetof(struct rocker_flow_tbl_key,
2661 ucast_routing.group_id);
2663 return rocker_flow_tbl_do(rocker_port, trans, flags, entry);
2666 static int rocker_flow_tbl_acl(struct rocker_port *rocker_port,
2667 enum switchdev_trans trans, int flags,
2668 u32 in_pport, u32 in_pport_mask,
2669 const u8 *eth_src, const u8 *eth_src_mask,
2670 const u8 *eth_dst, const u8 *eth_dst_mask,
2671 __be16 eth_type, __be16 vlan_id,
2672 __be16 vlan_id_mask, u8 ip_proto,
2673 u8 ip_proto_mask, u8 ip_tos, u8 ip_tos_mask,
2677 struct rocker_flow_tbl_entry *entry;
2679 entry = rocker_port_kzalloc(rocker_port, trans, sizeof(*entry));
2683 priority = ROCKER_PRIORITY_ACL_NORMAL;
2684 if (eth_dst && eth_dst_mask) {
2685 if (ether_addr_equal(eth_dst_mask, mcast_mac))
2686 priority = ROCKER_PRIORITY_ACL_DFLT;
2687 else if (is_link_local_ether_addr(eth_dst))
2688 priority = ROCKER_PRIORITY_ACL_CTRL;
2691 entry->key.priority = priority;
2692 entry->key.tbl_id = ROCKER_OF_DPA_TABLE_ID_ACL_POLICY;
2693 entry->key.acl.in_pport = in_pport;
2694 entry->key.acl.in_pport_mask = in_pport_mask;
2697 ether_addr_copy(entry->key.acl.eth_src, eth_src);
2699 ether_addr_copy(entry->key.acl.eth_src_mask, eth_src_mask);
2701 ether_addr_copy(entry->key.acl.eth_dst, eth_dst);
2703 ether_addr_copy(entry->key.acl.eth_dst_mask, eth_dst_mask);
2705 entry->key.acl.eth_type = eth_type;
2706 entry->key.acl.vlan_id = vlan_id;
2707 entry->key.acl.vlan_id_mask = vlan_id_mask;
2708 entry->key.acl.ip_proto = ip_proto;
2709 entry->key.acl.ip_proto_mask = ip_proto_mask;
2710 entry->key.acl.ip_tos = ip_tos;
2711 entry->key.acl.ip_tos_mask = ip_tos_mask;
2712 entry->key.acl.group_id = group_id;
2714 return rocker_flow_tbl_do(rocker_port, trans, flags, entry);
2717 static struct rocker_group_tbl_entry *
2718 rocker_group_tbl_find(struct rocker *rocker,
2719 struct rocker_group_tbl_entry *match)
2721 struct rocker_group_tbl_entry *found;
2723 hash_for_each_possible(rocker->group_tbl, found,
2724 entry, match->group_id) {
2725 if (found->group_id == match->group_id)
2732 static void rocker_group_tbl_entry_free(struct rocker_port *rocker_port,
2733 enum switchdev_trans trans,
2734 struct rocker_group_tbl_entry *entry)
2736 switch (ROCKER_GROUP_TYPE_GET(entry->group_id)) {
2737 case ROCKER_OF_DPA_GROUP_TYPE_L2_FLOOD:
2738 case ROCKER_OF_DPA_GROUP_TYPE_L2_MCAST:
2739 rocker_port_kfree(rocker_port, trans, entry->group_ids);
2744 rocker_port_kfree(rocker_port, trans, entry);
2747 static int rocker_group_tbl_add(struct rocker_port *rocker_port,
2748 enum switchdev_trans trans,
2749 struct rocker_group_tbl_entry *match)
2751 struct rocker *rocker = rocker_port->rocker;
2752 struct rocker_group_tbl_entry *found;
2753 unsigned long flags;
2755 spin_lock_irqsave(&rocker->group_tbl_lock, flags);
2757 found = rocker_group_tbl_find(rocker, match);
2760 if (trans != SWITCHDEV_TRANS_PREPARE)
2761 hash_del(&found->entry);
2762 rocker_group_tbl_entry_free(rocker_port, trans, found);
2764 found->cmd = ROCKER_TLV_CMD_TYPE_OF_DPA_GROUP_MOD;
2767 found->cmd = ROCKER_TLV_CMD_TYPE_OF_DPA_GROUP_ADD;
2770 if (trans != SWITCHDEV_TRANS_PREPARE)
2771 hash_add(rocker->group_tbl, &found->entry, found->group_id);
2773 spin_unlock_irqrestore(&rocker->group_tbl_lock, flags);
2775 return rocker_cmd_exec(rocker, rocker_port, trans,
2776 rocker_cmd_group_tbl_add,
2780 static int rocker_group_tbl_del(struct rocker_port *rocker_port,
2781 enum switchdev_trans trans,
2782 struct rocker_group_tbl_entry *match)
2784 struct rocker *rocker = rocker_port->rocker;
2785 struct rocker_group_tbl_entry *found;
2786 unsigned long flags;
2789 spin_lock_irqsave(&rocker->group_tbl_lock, flags);
2791 found = rocker_group_tbl_find(rocker, match);
2794 if (trans != SWITCHDEV_TRANS_PREPARE)
2795 hash_del(&found->entry);
2796 found->cmd = ROCKER_TLV_CMD_TYPE_OF_DPA_GROUP_DEL;
2799 spin_unlock_irqrestore(&rocker->group_tbl_lock, flags);
2801 rocker_group_tbl_entry_free(rocker_port, trans, match);
2804 err = rocker_cmd_exec(rocker, rocker_port, trans,
2805 rocker_cmd_group_tbl_del,
2807 rocker_group_tbl_entry_free(rocker_port, trans, found);
2813 static int rocker_group_tbl_do(struct rocker_port *rocker_port,
2814 enum switchdev_trans trans, int flags,
2815 struct rocker_group_tbl_entry *entry)
2817 if (flags & ROCKER_OP_FLAG_REMOVE)
2818 return rocker_group_tbl_del(rocker_port, trans, entry);
2820 return rocker_group_tbl_add(rocker_port, trans, entry);
2823 static int rocker_group_l2_interface(struct rocker_port *rocker_port,
2824 enum switchdev_trans trans, int flags,
2825 __be16 vlan_id, u32 out_pport,
2828 struct rocker_group_tbl_entry *entry;
2830 entry = rocker_port_kzalloc(rocker_port, trans, sizeof(*entry));
2834 entry->group_id = ROCKER_GROUP_L2_INTERFACE(vlan_id, out_pport);
2835 entry->l2_interface.pop_vlan = pop_vlan;
2837 return rocker_group_tbl_do(rocker_port, trans, flags, entry);
2840 static int rocker_group_l2_fan_out(struct rocker_port *rocker_port,
2841 enum switchdev_trans trans,
2842 int flags, u8 group_count,
2843 u32 *group_ids, u32 group_id)
2845 struct rocker_group_tbl_entry *entry;
2847 entry = rocker_port_kzalloc(rocker_port, trans, sizeof(*entry));
2851 entry->group_id = group_id;
2852 entry->group_count = group_count;
2854 entry->group_ids = rocker_port_kcalloc(rocker_port, trans, group_count,
2856 if (!entry->group_ids) {
2857 rocker_port_kfree(rocker_port, trans, entry);
2860 memcpy(entry->group_ids, group_ids, group_count * sizeof(u32));
2862 return rocker_group_tbl_do(rocker_port, trans, flags, entry);
2865 static int rocker_group_l2_flood(struct rocker_port *rocker_port,
2866 enum switchdev_trans trans, int flags,
2867 __be16 vlan_id, u8 group_count,
2868 u32 *group_ids, u32 group_id)
2870 return rocker_group_l2_fan_out(rocker_port, trans, flags,
2871 group_count, group_ids,
2875 static int rocker_group_l3_unicast(struct rocker_port *rocker_port,
2876 enum switchdev_trans trans, int flags,
2877 u32 index, u8 *src_mac, u8 *dst_mac,
2878 __be16 vlan_id, bool ttl_check, u32 pport)
2880 struct rocker_group_tbl_entry *entry;
2882 entry = rocker_port_kzalloc(rocker_port, trans, sizeof(*entry));
2886 entry->group_id = ROCKER_GROUP_L3_UNICAST(index);
2888 ether_addr_copy(entry->l3_unicast.eth_src, src_mac);
2890 ether_addr_copy(entry->l3_unicast.eth_dst, dst_mac);
2891 entry->l3_unicast.vlan_id = vlan_id;
2892 entry->l3_unicast.ttl_check = ttl_check;
2893 entry->l3_unicast.group_id = ROCKER_GROUP_L2_INTERFACE(vlan_id, pport);
2895 return rocker_group_tbl_do(rocker_port, trans, flags, entry);
2898 static struct rocker_neigh_tbl_entry *
2899 rocker_neigh_tbl_find(struct rocker *rocker, __be32 ip_addr)
2901 struct rocker_neigh_tbl_entry *found;
2903 hash_for_each_possible(rocker->neigh_tbl, found,
2904 entry, be32_to_cpu(ip_addr))
2905 if (found->ip_addr == ip_addr)
2911 static void _rocker_neigh_add(struct rocker *rocker,
2912 struct rocker_neigh_tbl_entry *entry)
2914 entry->index = rocker->neigh_tbl_next_index++;
2916 hash_add(rocker->neigh_tbl, &entry->entry,
2917 be32_to_cpu(entry->ip_addr));
2920 static void _rocker_neigh_del(struct rocker_port *rocker_port,
2921 enum switchdev_trans trans,
2922 struct rocker_neigh_tbl_entry *entry)
2924 if (--entry->ref_count == 0) {
2925 hash_del(&entry->entry);
2926 rocker_port_kfree(rocker_port, trans, entry);
2930 static void _rocker_neigh_update(struct rocker_neigh_tbl_entry *entry,
2931 u8 *eth_dst, bool ttl_check)
2934 ether_addr_copy(entry->eth_dst, eth_dst);
2935 entry->ttl_check = ttl_check;
2941 static int rocker_port_ipv4_neigh(struct rocker_port *rocker_port,
2942 enum switchdev_trans trans,
2943 int flags, __be32 ip_addr, u8 *eth_dst)
2945 struct rocker *rocker = rocker_port->rocker;
2946 struct rocker_neigh_tbl_entry *entry;
2947 struct rocker_neigh_tbl_entry *found;
2948 unsigned long lock_flags;
2949 __be16 eth_type = htons(ETH_P_IP);
2950 enum rocker_of_dpa_table_id goto_tbl =
2951 ROCKER_OF_DPA_TABLE_ID_ACL_POLICY;
2954 bool adding = !(flags & ROCKER_OP_FLAG_REMOVE);
2959 entry = rocker_port_kzalloc(rocker_port, trans, sizeof(*entry));
2963 spin_lock_irqsave(&rocker->neigh_tbl_lock, lock_flags);
2965 found = rocker_neigh_tbl_find(rocker, ip_addr);
2967 updating = found && adding;
2968 removing = found && !adding;
2969 adding = !found && adding;
2972 entry->ip_addr = ip_addr;
2973 entry->dev = rocker_port->dev;
2974 ether_addr_copy(entry->eth_dst, eth_dst);
2975 entry->ttl_check = true;
2976 _rocker_neigh_add(rocker, entry);
2977 } else if (removing) {
2978 memcpy(entry, found, sizeof(*entry));
2979 _rocker_neigh_del(rocker_port, trans, found);
2980 } else if (updating) {
2981 _rocker_neigh_update(found, eth_dst, true);
2982 memcpy(entry, found, sizeof(*entry));
2987 spin_unlock_irqrestore(&rocker->neigh_tbl_lock, lock_flags);
2992 /* For each active neighbor, we have an L3 unicast group and
2993 * a /32 route to the neighbor, which uses the L3 unicast
2994 * group. The L3 unicast group can also be referred to by
2995 * other routes' nexthops.
2998 err = rocker_group_l3_unicast(rocker_port, trans, flags,
3000 rocker_port->dev->dev_addr,
3002 rocker_port->internal_vlan_id,
3004 rocker_port->pport);
3006 netdev_err(rocker_port->dev,
3007 "Error (%d) L3 unicast group index %d\n",
3012 if (adding || removing) {
3013 group_id = ROCKER_GROUP_L3_UNICAST(entry->index);
3014 err = rocker_flow_tbl_ucast4_routing(rocker_port, trans,
3021 netdev_err(rocker_port->dev,
3022 "Error (%d) /32 unicast route %pI4 group 0x%08x\n",
3023 err, &entry->ip_addr, group_id);
3028 rocker_port_kfree(rocker_port, trans, entry);
3033 static int rocker_port_ipv4_resolve(struct rocker_port *rocker_port,
3034 enum switchdev_trans trans, __be32 ip_addr)
3036 struct net_device *dev = rocker_port->dev;
3037 struct neighbour *n = __ipv4_neigh_lookup(dev, (__force u32)ip_addr);
3041 n = neigh_create(&arp_tbl, &ip_addr, dev);
3045 /* If the neigh is already resolved, then go ahead and
3046 * install the entry, otherwise start the ARP process to
3047 * resolve the neigh.
3050 if (n->nud_state & NUD_VALID)
3051 err = rocker_port_ipv4_neigh(rocker_port, trans, 0,
3054 neigh_event_send(n, NULL);
3059 static int rocker_port_ipv4_nh(struct rocker_port *rocker_port,
3060 enum switchdev_trans trans, int flags,
3061 __be32 ip_addr, u32 *index)
3063 struct rocker *rocker = rocker_port->rocker;
3064 struct rocker_neigh_tbl_entry *entry;
3065 struct rocker_neigh_tbl_entry *found;
3066 unsigned long lock_flags;
3067 bool adding = !(flags & ROCKER_OP_FLAG_REMOVE);
3070 bool resolved = true;
3073 entry = rocker_port_kzalloc(rocker_port, trans, sizeof(*entry));
3077 spin_lock_irqsave(&rocker->neigh_tbl_lock, lock_flags);
3079 found = rocker_neigh_tbl_find(rocker, ip_addr);
3081 *index = found->index;
3083 updating = found && adding;
3084 removing = found && !adding;
3085 adding = !found && adding;
3088 entry->ip_addr = ip_addr;
3089 entry->dev = rocker_port->dev;
3090 _rocker_neigh_add(rocker, entry);
3091 *index = entry->index;
3093 } else if (removing) {
3094 _rocker_neigh_del(rocker_port, trans, found);
3095 } else if (updating) {
3096 _rocker_neigh_update(found, NULL, false);
3097 resolved = !is_zero_ether_addr(found->eth_dst);
3102 spin_unlock_irqrestore(&rocker->neigh_tbl_lock, lock_flags);
3105 rocker_port_kfree(rocker_port, trans, entry);
3110 /* Resolved means neigh ip_addr is resolved to neigh mac. */
3113 err = rocker_port_ipv4_resolve(rocker_port, trans, ip_addr);
3118 static int rocker_port_vlan_flood_group(struct rocker_port *rocker_port,
3119 enum switchdev_trans trans,
3120 int flags, __be16 vlan_id)
3122 struct rocker_port *p;
3123 struct rocker *rocker = rocker_port->rocker;
3124 u32 group_id = ROCKER_GROUP_L2_FLOOD(vlan_id, 0);
3130 group_ids = rocker_port_kcalloc(rocker_port, trans, rocker->port_count,
3135 /* Adjust the flood group for this VLAN. The flood group
3136 * references an L2 interface group for each port in this
3140 for (i = 0; i < rocker->port_count; i++) {
3141 p = rocker->ports[i];
3142 if (!rocker_port_is_bridged(p))
3144 if (test_bit(ntohs(vlan_id), p->vlan_bitmap)) {
3145 group_ids[group_count++] =
3146 ROCKER_GROUP_L2_INTERFACE(vlan_id, p->pport);
3150 /* If there are no bridged ports in this VLAN, we're done */
3151 if (group_count == 0)
3152 goto no_ports_in_vlan;
3154 err = rocker_group_l2_flood(rocker_port, trans, flags, vlan_id,
3155 group_count, group_ids, group_id);
3157 netdev_err(rocker_port->dev,
3158 "Error (%d) port VLAN l2 flood group\n", err);
3161 rocker_port_kfree(rocker_port, trans, group_ids);
3165 static int rocker_port_vlan_l2_groups(struct rocker_port *rocker_port,
3166 enum switchdev_trans trans, int flags,
3167 __be16 vlan_id, bool pop_vlan)
3169 struct rocker *rocker = rocker_port->rocker;
3170 struct rocker_port *p;
3171 bool adding = !(flags & ROCKER_OP_FLAG_REMOVE);
3177 /* An L2 interface group for this port in this VLAN, but
3178 * only when port STP state is LEARNING|FORWARDING.
3181 if (rocker_port->stp_state == BR_STATE_LEARNING ||
3182 rocker_port->stp_state == BR_STATE_FORWARDING) {
3183 out_pport = rocker_port->pport;
3184 err = rocker_group_l2_interface(rocker_port, trans, flags,
3185 vlan_id, out_pport, pop_vlan);
3187 netdev_err(rocker_port->dev,
3188 "Error (%d) port VLAN l2 group for pport %d\n",
3194 /* An L2 interface group for this VLAN to CPU port.
3195 * Add when first port joins this VLAN and destroy when
3196 * last port leaves this VLAN.
3199 for (i = 0; i < rocker->port_count; i++) {
3200 p = rocker->ports[i];
3201 if (test_bit(ntohs(vlan_id), p->vlan_bitmap))
3205 if ((!adding || ref != 1) && (adding || ref != 0))
3209 err = rocker_group_l2_interface(rocker_port, trans, flags,
3210 vlan_id, out_pport, pop_vlan);
3212 netdev_err(rocker_port->dev,
3213 "Error (%d) port VLAN l2 group for CPU port\n", err);
3220 static struct rocker_ctrl {
3222 const u8 *eth_dst_mask;
3228 } rocker_ctrls[] = {
3229 [ROCKER_CTRL_LINK_LOCAL_MCAST] = {
3230 /* pass link local multicast pkts up to CPU for filtering */
3232 .eth_dst_mask = ll_mask,
3235 [ROCKER_CTRL_LOCAL_ARP] = {
3236 /* pass local ARP pkts up to CPU */
3237 .eth_dst = zero_mac,
3238 .eth_dst_mask = zero_mac,
3239 .eth_type = htons(ETH_P_ARP),
3242 [ROCKER_CTRL_IPV4_MCAST] = {
3243 /* pass IPv4 mcast pkts up to CPU, RFC 1112 */
3244 .eth_dst = ipv4_mcast,
3245 .eth_dst_mask = ipv4_mask,
3246 .eth_type = htons(ETH_P_IP),
3248 .copy_to_cpu = true,
3250 [ROCKER_CTRL_IPV6_MCAST] = {
3251 /* pass IPv6 mcast pkts up to CPU, RFC 2464 */
3252 .eth_dst = ipv6_mcast,
3253 .eth_dst_mask = ipv6_mask,
3254 .eth_type = htons(ETH_P_IPV6),
3256 .copy_to_cpu = true,
3258 [ROCKER_CTRL_DFLT_BRIDGING] = {
3259 /* flood any pkts on vlan */
3261 .copy_to_cpu = true,
3265 static int rocker_port_ctrl_vlan_acl(struct rocker_port *rocker_port,
3266 enum switchdev_trans trans, int flags,
3267 struct rocker_ctrl *ctrl, __be16 vlan_id)
3269 u32 in_pport = rocker_port->pport;
3270 u32 in_pport_mask = 0xffffffff;
3273 u8 *eth_src_mask = NULL;
3274 __be16 vlan_id_mask = htons(0xffff);
3276 u8 ip_proto_mask = 0;
3279 u32 group_id = ROCKER_GROUP_L2_INTERFACE(vlan_id, out_pport);
3282 err = rocker_flow_tbl_acl(rocker_port, trans, flags,
3283 in_pport, in_pport_mask,
3284 eth_src, eth_src_mask,
3285 ctrl->eth_dst, ctrl->eth_dst_mask,
3287 vlan_id, vlan_id_mask,
3288 ip_proto, ip_proto_mask,
3289 ip_tos, ip_tos_mask,
3293 netdev_err(rocker_port->dev, "Error (%d) ctrl ACL\n", err);
3298 static int rocker_port_ctrl_vlan_bridge(struct rocker_port *rocker_port,
3299 enum switchdev_trans trans, int flags,
3300 struct rocker_ctrl *ctrl,
3303 enum rocker_of_dpa_table_id goto_tbl =
3304 ROCKER_OF_DPA_TABLE_ID_ACL_POLICY;
3305 u32 group_id = ROCKER_GROUP_L2_FLOOD(vlan_id, 0);
3309 if (!rocker_port_is_bridged(rocker_port))
3312 err = rocker_flow_tbl_bridge(rocker_port, trans, flags,
3313 ctrl->eth_dst, ctrl->eth_dst_mask,
3315 goto_tbl, group_id, ctrl->copy_to_cpu);
3318 netdev_err(rocker_port->dev, "Error (%d) ctrl FLOOD\n", err);
3323 static int rocker_port_ctrl_vlan_term(struct rocker_port *rocker_port,
3324 enum switchdev_trans trans, int flags,
3325 struct rocker_ctrl *ctrl, __be16 vlan_id)
3327 u32 in_pport_mask = 0xffffffff;
3328 __be16 vlan_id_mask = htons(0xffff);
3331 if (ntohs(vlan_id) == 0)
3332 vlan_id = rocker_port->internal_vlan_id;
3334 err = rocker_flow_tbl_term_mac(rocker_port, trans,
3335 rocker_port->pport, in_pport_mask,
3336 ctrl->eth_type, ctrl->eth_dst,
3337 ctrl->eth_dst_mask, vlan_id,
3338 vlan_id_mask, ctrl->copy_to_cpu,
3342 netdev_err(rocker_port->dev, "Error (%d) ctrl term\n", err);
3347 static int rocker_port_ctrl_vlan(struct rocker_port *rocker_port,
3348 enum switchdev_trans trans, int flags,
3349 struct rocker_ctrl *ctrl, __be16 vlan_id)
3352 return rocker_port_ctrl_vlan_acl(rocker_port, trans, flags,
3355 return rocker_port_ctrl_vlan_bridge(rocker_port, trans, flags,
3359 return rocker_port_ctrl_vlan_term(rocker_port, trans, flags,
3365 static int rocker_port_ctrl_vlan_add(struct rocker_port *rocker_port,
3366 enum switchdev_trans trans, int flags,
3372 for (i = 0; i < ROCKER_CTRL_MAX; i++) {
3373 if (rocker_port->ctrls[i]) {
3374 err = rocker_port_ctrl_vlan(rocker_port, trans, flags,
3375 &rocker_ctrls[i], vlan_id);
3384 static int rocker_port_ctrl(struct rocker_port *rocker_port,
3385 enum switchdev_trans trans, int flags,
3386 struct rocker_ctrl *ctrl)
3391 for (vid = 1; vid < VLAN_N_VID; vid++) {
3392 if (!test_bit(vid, rocker_port->vlan_bitmap))
3394 err = rocker_port_ctrl_vlan(rocker_port, trans, flags,
3403 static int rocker_port_vlan(struct rocker_port *rocker_port,
3404 enum switchdev_trans trans, int flags, u16 vid)
3406 enum rocker_of_dpa_table_id goto_tbl =
3407 ROCKER_OF_DPA_TABLE_ID_TERMINATION_MAC;
3408 u32 in_pport = rocker_port->pport;
3409 __be16 vlan_id = htons(vid);
3410 __be16 vlan_id_mask = htons(0xffff);
3411 __be16 internal_vlan_id;
3413 bool adding = !(flags & ROCKER_OP_FLAG_REMOVE);
3416 internal_vlan_id = rocker_port_vid_to_vlan(rocker_port, vid, &untagged);
3418 if (adding && test_and_set_bit(ntohs(internal_vlan_id),
3419 rocker_port->vlan_bitmap))
3420 return 0; /* already added */
3421 else if (!adding && !test_and_clear_bit(ntohs(internal_vlan_id),
3422 rocker_port->vlan_bitmap))
3423 return 0; /* already removed */
3426 err = rocker_port_ctrl_vlan_add(rocker_port, trans, flags,
3429 netdev_err(rocker_port->dev,
3430 "Error (%d) port ctrl vlan add\n", err);
3435 err = rocker_port_vlan_l2_groups(rocker_port, trans, flags,
3436 internal_vlan_id, untagged);
3438 netdev_err(rocker_port->dev,
3439 "Error (%d) port VLAN l2 groups\n", err);
3443 err = rocker_port_vlan_flood_group(rocker_port, trans, flags,
3446 netdev_err(rocker_port->dev,
3447 "Error (%d) port VLAN l2 flood group\n", err);
3451 err = rocker_flow_tbl_vlan(rocker_port, trans, flags,
3452 in_pport, vlan_id, vlan_id_mask,
3453 goto_tbl, untagged, internal_vlan_id);
3455 netdev_err(rocker_port->dev,
3456 "Error (%d) port VLAN table\n", err);
3461 static int rocker_port_ig_tbl(struct rocker_port *rocker_port,
3462 enum switchdev_trans trans, int flags)
3464 enum rocker_of_dpa_table_id goto_tbl;
3469 /* Normal Ethernet Frames. Matches pkts from any local physical
3470 * ports. Goto VLAN tbl.
3474 in_pport_mask = 0xffff0000;
3475 goto_tbl = ROCKER_OF_DPA_TABLE_ID_VLAN;
3477 err = rocker_flow_tbl_ig_port(rocker_port, trans, flags,
3478 in_pport, in_pport_mask,
3481 netdev_err(rocker_port->dev,
3482 "Error (%d) ingress port table entry\n", err);
3487 struct rocker_fdb_learn_work {
3488 struct work_struct work;
3489 struct rocker_port *rocker_port;
3490 enum switchdev_trans trans;
3496 static void rocker_port_fdb_learn_work(struct work_struct *work)
3498 struct rocker_fdb_learn_work *lw =
3499 container_of(work, struct rocker_fdb_learn_work, work);
3500 bool removing = (lw->flags & ROCKER_OP_FLAG_REMOVE);
3501 bool learned = (lw->flags & ROCKER_OP_FLAG_LEARNED);
3502 struct switchdev_notifier_fdb_info info;
3504 info.addr = lw->addr;
3507 if (learned && removing)
3508 call_switchdev_notifiers(SWITCHDEV_FDB_DEL,
3509 lw->rocker_port->dev, &info.info);
3510 else if (learned && !removing)
3511 call_switchdev_notifiers(SWITCHDEV_FDB_ADD,
3512 lw->rocker_port->dev, &info.info);
3514 rocker_port_kfree(lw->rocker_port, lw->trans, work);
3517 static int rocker_port_fdb_learn(struct rocker_port *rocker_port,
3518 enum switchdev_trans trans, int flags,
3519 const u8 *addr, __be16 vlan_id)
3521 struct rocker_fdb_learn_work *lw;
3522 enum rocker_of_dpa_table_id goto_tbl =
3523 ROCKER_OF_DPA_TABLE_ID_ACL_POLICY;
3524 u32 out_pport = rocker_port->pport;
3526 u32 group_id = ROCKER_GROUP_NONE;
3527 bool syncing = !!(rocker_port->brport_flags & BR_LEARNING_SYNC);
3528 bool copy_to_cpu = false;
3531 if (rocker_port_is_bridged(rocker_port))
3532 group_id = ROCKER_GROUP_L2_INTERFACE(vlan_id, out_pport);
3534 if (!(flags & ROCKER_OP_FLAG_REFRESH)) {
3535 err = rocker_flow_tbl_bridge(rocker_port, trans, flags, addr,
3536 NULL, vlan_id, tunnel_id, goto_tbl,
3537 group_id, copy_to_cpu);
3545 if (!rocker_port_is_bridged(rocker_port))
3548 lw = rocker_port_kzalloc(rocker_port, trans, sizeof(*lw));
3552 INIT_WORK(&lw->work, rocker_port_fdb_learn_work);
3554 lw->rocker_port = rocker_port;
3557 ether_addr_copy(lw->addr, addr);
3558 lw->vid = rocker_port_vlan_to_vid(rocker_port, vlan_id);
3560 if (trans == SWITCHDEV_TRANS_PREPARE)
3561 rocker_port_kfree(rocker_port, trans, lw);
3563 schedule_work(&lw->work);
3568 static struct rocker_fdb_tbl_entry *
3569 rocker_fdb_tbl_find(struct rocker *rocker, struct rocker_fdb_tbl_entry *match)
3571 struct rocker_fdb_tbl_entry *found;
3573 hash_for_each_possible(rocker->fdb_tbl, found, entry, match->key_crc32)
3574 if (memcmp(&found->key, &match->key, sizeof(found->key)) == 0)
3580 static int rocker_port_fdb(struct rocker_port *rocker_port,
3581 enum switchdev_trans trans,
3582 const unsigned char *addr,
3583 __be16 vlan_id, int flags)
3585 struct rocker *rocker = rocker_port->rocker;
3586 struct rocker_fdb_tbl_entry *fdb;
3587 struct rocker_fdb_tbl_entry *found;
3588 bool removing = (flags & ROCKER_OP_FLAG_REMOVE);
3589 unsigned long lock_flags;
3591 fdb = rocker_port_kzalloc(rocker_port, trans, sizeof(*fdb));
3595 fdb->learned = (flags & ROCKER_OP_FLAG_LEARNED);
3596 fdb->key.pport = rocker_port->pport;
3597 ether_addr_copy(fdb->key.addr, addr);
3598 fdb->key.vlan_id = vlan_id;
3599 fdb->key_crc32 = crc32(~0, &fdb->key, sizeof(fdb->key));
3601 spin_lock_irqsave(&rocker->fdb_tbl_lock, lock_flags);
3603 found = rocker_fdb_tbl_find(rocker, fdb);
3605 if (removing && found) {
3606 rocker_port_kfree(rocker_port, trans, fdb);
3607 hash_del(&found->entry);
3608 } else if (!removing && !found) {
3609 hash_add(rocker->fdb_tbl, &fdb->entry, fdb->key_crc32);
3612 spin_unlock_irqrestore(&rocker->fdb_tbl_lock, lock_flags);
3614 /* Check if adding and already exists, or removing and can't find */
3615 if (!found != !removing) {
3616 rocker_port_kfree(rocker_port, trans, fdb);
3617 if (!found && removing)
3619 /* Refreshing existing to update aging timers */
3620 flags |= ROCKER_OP_FLAG_REFRESH;
3623 return rocker_port_fdb_learn(rocker_port, trans, flags, addr, vlan_id);
3626 static int rocker_port_fdb_flush(struct rocker_port *rocker_port,
3627 enum switchdev_trans trans)
3629 struct rocker *rocker = rocker_port->rocker;
3630 struct rocker_fdb_tbl_entry *found;
3631 unsigned long lock_flags;
3632 int flags = ROCKER_OP_FLAG_REMOVE;
3633 struct hlist_node *tmp;
3637 if (rocker_port->stp_state == BR_STATE_LEARNING ||
3638 rocker_port->stp_state == BR_STATE_FORWARDING)
3641 spin_lock_irqsave(&rocker->fdb_tbl_lock, lock_flags);
3643 hash_for_each_safe(rocker->fdb_tbl, bkt, tmp, found, entry) {
3644 if (found->key.pport != rocker_port->pport)
3646 if (!found->learned)
3648 err = rocker_port_fdb_learn(rocker_port, trans, flags,
3650 found->key.vlan_id);
3653 hash_del(&found->entry);
3657 spin_unlock_irqrestore(&rocker->fdb_tbl_lock, lock_flags);
3662 static int rocker_port_router_mac(struct rocker_port *rocker_port,
3663 enum switchdev_trans trans, int flags,
3666 u32 in_pport_mask = 0xffffffff;
3668 const u8 *dst_mac_mask = ff_mac;
3669 __be16 vlan_id_mask = htons(0xffff);
3670 bool copy_to_cpu = false;
3673 if (ntohs(vlan_id) == 0)
3674 vlan_id = rocker_port->internal_vlan_id;
3676 eth_type = htons(ETH_P_IP);
3677 err = rocker_flow_tbl_term_mac(rocker_port, trans,
3678 rocker_port->pport, in_pport_mask,
3679 eth_type, rocker_port->dev->dev_addr,
3680 dst_mac_mask, vlan_id, vlan_id_mask,
3681 copy_to_cpu, flags);
3685 eth_type = htons(ETH_P_IPV6);
3686 err = rocker_flow_tbl_term_mac(rocker_port, trans,
3687 rocker_port->pport, in_pport_mask,
3688 eth_type, rocker_port->dev->dev_addr,
3689 dst_mac_mask, vlan_id, vlan_id_mask,
3690 copy_to_cpu, flags);
3695 static int rocker_port_fwding(struct rocker_port *rocker_port,
3696 enum switchdev_trans trans)
3705 /* Port will be forwarding-enabled if its STP state is LEARNING
3706 * or FORWARDING. Traffic from CPU can still egress, regardless of
3707 * port STP state. Use L2 interface group on port VLANs as a way
3708 * to toggle port forwarding: if forwarding is disabled, L2
3709 * interface group will not exist.
3712 if (rocker_port->stp_state != BR_STATE_LEARNING &&
3713 rocker_port->stp_state != BR_STATE_FORWARDING)
3714 flags |= ROCKER_OP_FLAG_REMOVE;
3716 out_pport = rocker_port->pport;
3717 for (vid = 1; vid < VLAN_N_VID; vid++) {
3718 if (!test_bit(vid, rocker_port->vlan_bitmap))
3720 vlan_id = htons(vid);
3721 pop_vlan = rocker_vlan_id_is_internal(vlan_id);
3722 err = rocker_group_l2_interface(rocker_port, trans, flags,
3723 vlan_id, out_pport, pop_vlan);
3725 netdev_err(rocker_port->dev,
3726 "Error (%d) port VLAN l2 group for pport %d\n",
3735 static int rocker_port_stp_update(struct rocker_port *rocker_port,
3736 enum switchdev_trans trans, u8 state)
3738 bool want[ROCKER_CTRL_MAX] = { 0, };
3739 bool prev_ctrls[ROCKER_CTRL_MAX];
3745 if (trans == SWITCHDEV_TRANS_PREPARE) {
3746 memcpy(prev_ctrls, rocker_port->ctrls, sizeof(prev_ctrls));
3747 prev_state = rocker_port->stp_state;
3750 if (rocker_port->stp_state == state)
3753 rocker_port->stp_state = state;
3756 case BR_STATE_DISABLED:
3757 /* port is completely disabled */
3759 case BR_STATE_LISTENING:
3760 case BR_STATE_BLOCKING:
3761 want[ROCKER_CTRL_LINK_LOCAL_MCAST] = true;
3763 case BR_STATE_LEARNING:
3764 case BR_STATE_FORWARDING:
3765 want[ROCKER_CTRL_LINK_LOCAL_MCAST] = true;
3766 want[ROCKER_CTRL_IPV4_MCAST] = true;
3767 want[ROCKER_CTRL_IPV6_MCAST] = true;
3768 if (rocker_port_is_bridged(rocker_port))
3769 want[ROCKER_CTRL_DFLT_BRIDGING] = true;
3771 want[ROCKER_CTRL_LOCAL_ARP] = true;
3775 for (i = 0; i < ROCKER_CTRL_MAX; i++) {
3776 if (want[i] != rocker_port->ctrls[i]) {
3777 flags = (want[i] ? 0 : ROCKER_OP_FLAG_REMOVE);
3778 err = rocker_port_ctrl(rocker_port, trans, flags,
3782 rocker_port->ctrls[i] = want[i];
3786 err = rocker_port_fdb_flush(rocker_port, trans);
3790 err = rocker_port_fwding(rocker_port, trans);
3793 if (trans == SWITCHDEV_TRANS_PREPARE) {
3794 memcpy(rocker_port->ctrls, prev_ctrls, sizeof(prev_ctrls));
3795 rocker_port->stp_state = prev_state;
3801 static int rocker_port_fwd_enable(struct rocker_port *rocker_port,
3802 enum switchdev_trans trans)
3804 if (rocker_port_is_bridged(rocker_port))
3805 /* bridge STP will enable port */
3808 /* port is not bridged, so simulate going to FORWARDING state */
3809 return rocker_port_stp_update(rocker_port, trans, BR_STATE_FORWARDING);
3812 static int rocker_port_fwd_disable(struct rocker_port *rocker_port,
3813 enum switchdev_trans trans)
3815 if (rocker_port_is_bridged(rocker_port))
3816 /* bridge STP will disable port */
3819 /* port is not bridged, so simulate going to DISABLED state */
3820 return rocker_port_stp_update(rocker_port, trans, BR_STATE_DISABLED);
3823 static struct rocker_internal_vlan_tbl_entry *
3824 rocker_internal_vlan_tbl_find(struct rocker *rocker, int ifindex)
3826 struct rocker_internal_vlan_tbl_entry *found;
3828 hash_for_each_possible(rocker->internal_vlan_tbl, found,
3830 if (found->ifindex == ifindex)
3837 static __be16 rocker_port_internal_vlan_id_get(struct rocker_port *rocker_port,
3838 enum switchdev_trans trans,
3841 struct rocker *rocker = rocker_port->rocker;
3842 struct rocker_internal_vlan_tbl_entry *entry;
3843 struct rocker_internal_vlan_tbl_entry *found;
3844 unsigned long lock_flags;
3847 entry = rocker_port_kzalloc(rocker_port, trans, sizeof(*entry));
3851 entry->ifindex = ifindex;
3853 spin_lock_irqsave(&rocker->internal_vlan_tbl_lock, lock_flags);
3855 found = rocker_internal_vlan_tbl_find(rocker, ifindex);
3857 rocker_port_kfree(rocker_port, trans, entry);
3862 hash_add(rocker->internal_vlan_tbl, &found->entry, found->ifindex);
3864 for (i = 0; i < ROCKER_N_INTERNAL_VLANS; i++) {
3865 if (test_and_set_bit(i, rocker->internal_vlan_bitmap))
3867 found->vlan_id = htons(ROCKER_INTERNAL_VLAN_ID_BASE + i);
3871 netdev_err(rocker_port->dev, "Out of internal VLAN IDs\n");
3875 spin_unlock_irqrestore(&rocker->internal_vlan_tbl_lock, lock_flags);
3877 return found->vlan_id;
3880 static void rocker_port_internal_vlan_id_put(struct rocker_port *rocker_port,
3881 enum switchdev_trans trans,
3884 struct rocker *rocker = rocker_port->rocker;
3885 struct rocker_internal_vlan_tbl_entry *found;
3886 unsigned long lock_flags;
3889 spin_lock_irqsave(&rocker->internal_vlan_tbl_lock, lock_flags);
3891 found = rocker_internal_vlan_tbl_find(rocker, ifindex);
3893 netdev_err(rocker_port->dev,
3894 "ifindex (%d) not found in internal VLAN tbl\n",
3899 if (--found->ref_count <= 0) {
3900 bit = ntohs(found->vlan_id) - ROCKER_INTERNAL_VLAN_ID_BASE;
3901 clear_bit(bit, rocker->internal_vlan_bitmap);
3902 hash_del(&found->entry);
3903 rocker_port_kfree(rocker_port, trans, found);
3907 spin_unlock_irqrestore(&rocker->internal_vlan_tbl_lock, lock_flags);
3910 static int rocker_port_fib_ipv4(struct rocker_port *rocker_port,
3911 enum switchdev_trans trans, __be32 dst,
3912 int dst_len, struct fib_info *fi, u32 tb_id,
3916 __be16 eth_type = htons(ETH_P_IP);
3917 __be32 dst_mask = inet_make_mask(dst_len);
3918 __be16 internal_vlan_id = rocker_port->internal_vlan_id;
3919 u32 priority = fi->fib_priority;
3920 enum rocker_of_dpa_table_id goto_tbl =
3921 ROCKER_OF_DPA_TABLE_ID_ACL_POLICY;
3928 /* XXX support ECMP */
3931 nh_on_port = (fi->fib_dev == rocker_port->dev);
3932 has_gw = !!nh->nh_gw;
3934 if (has_gw && nh_on_port) {
3935 err = rocker_port_ipv4_nh(rocker_port, trans, flags,
3940 group_id = ROCKER_GROUP_L3_UNICAST(index);
3942 /* Send to CPU for processing */
3943 group_id = ROCKER_GROUP_L2_INTERFACE(internal_vlan_id, 0);
3946 err = rocker_flow_tbl_ucast4_routing(rocker_port, trans, eth_type, dst,
3947 dst_mask, priority, goto_tbl,
3950 netdev_err(rocker_port->dev, "Error (%d) IPv4 route %pI4\n",
3960 static int rocker_port_open(struct net_device *dev)
3962 struct rocker_port *rocker_port = netdev_priv(dev);
3965 err = rocker_port_dma_rings_init(rocker_port);
3969 err = request_irq(rocker_msix_tx_vector(rocker_port),
3970 rocker_tx_irq_handler, 0,
3971 rocker_driver_name, rocker_port);
3973 netdev_err(rocker_port->dev, "cannot assign tx irq\n");
3974 goto err_request_tx_irq;
3977 err = request_irq(rocker_msix_rx_vector(rocker_port),
3978 rocker_rx_irq_handler, 0,
3979 rocker_driver_name, rocker_port);
3981 netdev_err(rocker_port->dev, "cannot assign rx irq\n");
3982 goto err_request_rx_irq;
3985 err = rocker_port_fwd_enable(rocker_port, SWITCHDEV_TRANS_NONE);
3987 goto err_fwd_enable;
3989 napi_enable(&rocker_port->napi_tx);
3990 napi_enable(&rocker_port->napi_rx);
3991 rocker_port_set_enable(rocker_port, true);
3992 netif_start_queue(dev);
3996 free_irq(rocker_msix_rx_vector(rocker_port), rocker_port);
3998 free_irq(rocker_msix_tx_vector(rocker_port), rocker_port);
4000 rocker_port_dma_rings_fini(rocker_port);
4004 static int rocker_port_stop(struct net_device *dev)
4006 struct rocker_port *rocker_port = netdev_priv(dev);
4008 netif_stop_queue(dev);
4009 rocker_port_set_enable(rocker_port, false);
4010 napi_disable(&rocker_port->napi_rx);
4011 napi_disable(&rocker_port->napi_tx);
4012 rocker_port_fwd_disable(rocker_port, SWITCHDEV_TRANS_NONE);
4013 free_irq(rocker_msix_rx_vector(rocker_port), rocker_port);
4014 free_irq(rocker_msix_tx_vector(rocker_port), rocker_port);
4015 rocker_port_dma_rings_fini(rocker_port);
4020 static void rocker_tx_desc_frags_unmap(struct rocker_port *rocker_port,
4021 struct rocker_desc_info *desc_info)
4023 struct rocker *rocker = rocker_port->rocker;
4024 struct pci_dev *pdev = rocker->pdev;
4025 struct rocker_tlv *attrs[ROCKER_TLV_TX_MAX + 1];
4026 struct rocker_tlv *attr;
4029 rocker_tlv_parse_desc(attrs, ROCKER_TLV_TX_MAX, desc_info);
4030 if (!attrs[ROCKER_TLV_TX_FRAGS])
4032 rocker_tlv_for_each_nested(attr, attrs[ROCKER_TLV_TX_FRAGS], rem) {
4033 struct rocker_tlv *frag_attrs[ROCKER_TLV_TX_FRAG_ATTR_MAX + 1];
4034 dma_addr_t dma_handle;
4037 if (rocker_tlv_type(attr) != ROCKER_TLV_TX_FRAG)
4039 rocker_tlv_parse_nested(frag_attrs, ROCKER_TLV_TX_FRAG_ATTR_MAX,
4041 if (!frag_attrs[ROCKER_TLV_TX_FRAG_ATTR_ADDR] ||
4042 !frag_attrs[ROCKER_TLV_TX_FRAG_ATTR_LEN])
4044 dma_handle = rocker_tlv_get_u64(frag_attrs[ROCKER_TLV_TX_FRAG_ATTR_ADDR]);
4045 len = rocker_tlv_get_u16(frag_attrs[ROCKER_TLV_TX_FRAG_ATTR_LEN]);
4046 pci_unmap_single(pdev, dma_handle, len, DMA_TO_DEVICE);
4050 static int rocker_tx_desc_frag_map_put(struct rocker_port *rocker_port,
4051 struct rocker_desc_info *desc_info,
4052 char *buf, size_t buf_len)
4054 struct rocker *rocker = rocker_port->rocker;
4055 struct pci_dev *pdev = rocker->pdev;
4056 dma_addr_t dma_handle;
4057 struct rocker_tlv *frag;
4059 dma_handle = pci_map_single(pdev, buf, buf_len, DMA_TO_DEVICE);
4060 if (unlikely(pci_dma_mapping_error(pdev, dma_handle))) {
4061 if (net_ratelimit())
4062 netdev_err(rocker_port->dev, "failed to dma map tx frag\n");
4065 frag = rocker_tlv_nest_start(desc_info, ROCKER_TLV_TX_FRAG);
4068 if (rocker_tlv_put_u64(desc_info, ROCKER_TLV_TX_FRAG_ATTR_ADDR,
4071 if (rocker_tlv_put_u16(desc_info, ROCKER_TLV_TX_FRAG_ATTR_LEN,
4074 rocker_tlv_nest_end(desc_info, frag);
4078 rocker_tlv_nest_cancel(desc_info, frag);
4080 pci_unmap_single(pdev, dma_handle, buf_len, DMA_TO_DEVICE);
4084 static netdev_tx_t rocker_port_xmit(struct sk_buff *skb, struct net_device *dev)
4086 struct rocker_port *rocker_port = netdev_priv(dev);
4087 struct rocker *rocker = rocker_port->rocker;
4088 struct rocker_desc_info *desc_info;
4089 struct rocker_tlv *frags;
4093 desc_info = rocker_desc_head_get(&rocker_port->tx_ring);
4094 if (unlikely(!desc_info)) {
4095 if (net_ratelimit())
4096 netdev_err(dev, "tx ring full when queue awake\n");
4097 return NETDEV_TX_BUSY;
4100 rocker_desc_cookie_ptr_set(desc_info, skb);
4102 frags = rocker_tlv_nest_start(desc_info, ROCKER_TLV_TX_FRAGS);
4105 err = rocker_tx_desc_frag_map_put(rocker_port, desc_info,
4106 skb->data, skb_headlen(skb));
4109 if (skb_shinfo(skb)->nr_frags > ROCKER_TX_FRAGS_MAX)
4112 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
4113 const skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
4115 err = rocker_tx_desc_frag_map_put(rocker_port, desc_info,
4116 skb_frag_address(frag),
4117 skb_frag_size(frag));
4121 rocker_tlv_nest_end(desc_info, frags);
4123 rocker_desc_gen_clear(desc_info);
4124 rocker_desc_head_set(rocker, &rocker_port->tx_ring, desc_info);
4126 desc_info = rocker_desc_head_get(&rocker_port->tx_ring);
4128 netif_stop_queue(dev);
4130 return NETDEV_TX_OK;
4133 rocker_tx_desc_frags_unmap(rocker_port, desc_info);
4135 rocker_tlv_nest_cancel(desc_info, frags);
4138 dev->stats.tx_dropped++;
4140 return NETDEV_TX_OK;
4143 static int rocker_port_set_mac_address(struct net_device *dev, void *p)
4145 struct sockaddr *addr = p;
4146 struct rocker_port *rocker_port = netdev_priv(dev);
4149 if (!is_valid_ether_addr(addr->sa_data))
4150 return -EADDRNOTAVAIL;
4152 err = rocker_cmd_set_port_settings_macaddr(rocker_port, addr->sa_data);
4155 memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
4159 static int rocker_port_vlan_rx_add_vid(struct net_device *dev,
4160 __be16 proto, u16 vid)
4162 struct rocker_port *rocker_port = netdev_priv(dev);
4165 err = rocker_port_vlan(rocker_port, SWITCHDEV_TRANS_NONE, 0, vid);
4169 return rocker_port_router_mac(rocker_port, SWITCHDEV_TRANS_NONE,
4173 static int rocker_port_vlan_rx_kill_vid(struct net_device *dev,
4174 __be16 proto, u16 vid)
4176 struct rocker_port *rocker_port = netdev_priv(dev);
4179 err = rocker_port_router_mac(rocker_port, SWITCHDEV_TRANS_NONE,
4180 ROCKER_OP_FLAG_REMOVE, htons(vid));
4184 return rocker_port_vlan(rocker_port, SWITCHDEV_TRANS_NONE,
4185 ROCKER_OP_FLAG_REMOVE, vid);
4188 static int rocker_port_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
4189 struct net_device *dev,
4190 const unsigned char *addr, u16 vid,
4193 struct rocker_port *rocker_port = netdev_priv(dev);
4194 __be16 vlan_id = rocker_port_vid_to_vlan(rocker_port, vid, NULL);
4197 if (!rocker_port_is_bridged(rocker_port))
4200 return rocker_port_fdb(rocker_port, SWITCHDEV_TRANS_NONE,
4201 addr, vlan_id, flags);
4204 static int rocker_port_fdb_del(struct ndmsg *ndm, struct nlattr *tb[],
4205 struct net_device *dev,
4206 const unsigned char *addr, u16 vid)
4208 struct rocker_port *rocker_port = netdev_priv(dev);
4209 __be16 vlan_id = rocker_port_vid_to_vlan(rocker_port, vid, NULL);
4210 int flags = ROCKER_OP_FLAG_REMOVE;
4212 if (!rocker_port_is_bridged(rocker_port))
4215 return rocker_port_fdb(rocker_port, SWITCHDEV_TRANS_NONE,
4216 addr, vlan_id, flags);
4219 static int rocker_fdb_fill_info(struct sk_buff *skb,
4220 struct rocker_port *rocker_port,
4221 const unsigned char *addr, u16 vid,
4222 u32 portid, u32 seq, int type,
4225 struct nlmsghdr *nlh;
4228 nlh = nlmsg_put(skb, portid, seq, type, sizeof(*ndm), flags);
4232 ndm = nlmsg_data(nlh);
4233 ndm->ndm_family = AF_BRIDGE;
4236 ndm->ndm_flags = NTF_SELF;
4238 ndm->ndm_ifindex = rocker_port->dev->ifindex;
4239 ndm->ndm_state = NUD_REACHABLE;
4241 if (nla_put(skb, NDA_LLADDR, ETH_ALEN, addr))
4242 goto nla_put_failure;
4244 if (vid && nla_put_u16(skb, NDA_VLAN, vid))
4245 goto nla_put_failure;
4247 nlmsg_end(skb, nlh);
4251 nlmsg_cancel(skb, nlh);
4255 static int rocker_port_fdb_dump(struct sk_buff *skb,
4256 struct netlink_callback *cb,
4257 struct net_device *dev,
4258 struct net_device *filter_dev,
4261 struct rocker_port *rocker_port = netdev_priv(dev);
4262 struct rocker *rocker = rocker_port->rocker;
4263 struct rocker_fdb_tbl_entry *found;
4264 struct hlist_node *tmp;
4266 unsigned long lock_flags;
4267 const unsigned char *addr;
4271 spin_lock_irqsave(&rocker->fdb_tbl_lock, lock_flags);
4272 hash_for_each_safe(rocker->fdb_tbl, bkt, tmp, found, entry) {
4273 if (found->key.pport != rocker_port->pport)
4275 if (idx < cb->args[0])
4277 addr = found->key.addr;
4278 vid = rocker_port_vlan_to_vid(rocker_port, found->key.vlan_id);
4279 err = rocker_fdb_fill_info(skb, rocker_port, addr, vid,
4280 NETLINK_CB(cb->skb).portid,
4282 RTM_NEWNEIGH, NLM_F_MULTI);
4288 spin_unlock_irqrestore(&rocker->fdb_tbl_lock, lock_flags);
4292 static int rocker_port_bridge_setlink(struct net_device *dev,
4293 struct nlmsghdr *nlh, u16 flags)
4295 struct rocker_port *rocker_port = netdev_priv(dev);
4296 struct nlattr *protinfo;
4297 struct nlattr *attr;
4300 protinfo = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg),
4303 attr = nla_find_nested(protinfo, IFLA_BRPORT_LEARNING);
4305 if (nla_len(attr) < sizeof(u8))
4308 if (nla_get_u8(attr))
4309 rocker_port->brport_flags |= BR_LEARNING;
4311 rocker_port->brport_flags &= ~BR_LEARNING;
4312 err = rocker_port_set_learning(rocker_port,
4313 SWITCHDEV_TRANS_NONE);
4317 attr = nla_find_nested(protinfo, IFLA_BRPORT_LEARNING_SYNC);
4319 if (nla_len(attr) < sizeof(u8))
4322 if (nla_get_u8(attr))
4323 rocker_port->brport_flags |= BR_LEARNING_SYNC;
4325 rocker_port->brport_flags &= ~BR_LEARNING_SYNC;
4332 static int rocker_port_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq,
4333 struct net_device *dev,
4334 u32 filter_mask, int nlflags)
4336 struct rocker_port *rocker_port = netdev_priv(dev);
4337 u16 mode = BRIDGE_MODE_UNDEF;
4338 u32 mask = BR_LEARNING | BR_LEARNING_SYNC;
4340 return ndo_dflt_bridge_getlink(skb, pid, seq, dev, mode,
4341 rocker_port->brport_flags, mask,
4345 static int rocker_port_get_phys_port_name(struct net_device *dev,
4346 char *buf, size_t len)
4348 struct rocker_port *rocker_port = netdev_priv(dev);
4349 struct port_name name = { .buf = buf, .len = len };
4352 err = rocker_cmd_exec(rocker_port->rocker, rocker_port,
4353 SWITCHDEV_TRANS_NONE,
4354 rocker_cmd_get_port_settings_prep, NULL,
4355 rocker_cmd_get_port_settings_phys_name_proc,
4358 return err ? -EOPNOTSUPP : 0;
4361 static const struct net_device_ops rocker_port_netdev_ops = {
4362 .ndo_open = rocker_port_open,
4363 .ndo_stop = rocker_port_stop,
4364 .ndo_start_xmit = rocker_port_xmit,
4365 .ndo_set_mac_address = rocker_port_set_mac_address,
4366 .ndo_vlan_rx_add_vid = rocker_port_vlan_rx_add_vid,
4367 .ndo_vlan_rx_kill_vid = rocker_port_vlan_rx_kill_vid,
4368 .ndo_fdb_add = rocker_port_fdb_add,
4369 .ndo_fdb_del = rocker_port_fdb_del,
4370 .ndo_fdb_dump = rocker_port_fdb_dump,
4371 .ndo_bridge_setlink = rocker_port_bridge_setlink,
4372 .ndo_bridge_getlink = rocker_port_bridge_getlink,
4373 .ndo_get_phys_port_name = rocker_port_get_phys_port_name,
4376 /********************
4378 ********************/
4380 static int rocker_port_attr_get(struct net_device *dev,
4381 struct switchdev_attr *attr)
4383 struct rocker_port *rocker_port = netdev_priv(dev);
4384 struct rocker *rocker = rocker_port->rocker;
4387 case SWITCHDEV_ATTR_PORT_PARENT_ID:
4388 attr->ppid.id_len = sizeof(rocker->hw.id);
4389 memcpy(&attr->ppid.id, &rocker->hw.id, attr->ppid.id_len);
4398 static void rocker_port_trans_abort(struct rocker_port *rocker_port)
4400 struct list_head *mem, *tmp;
4402 list_for_each_safe(mem, tmp, &rocker_port->trans_mem) {
4408 static int rocker_port_attr_set(struct net_device *dev,
4409 struct switchdev_attr *attr)
4411 struct rocker_port *rocker_port = netdev_priv(dev);
4414 switch (attr->trans) {
4415 case SWITCHDEV_TRANS_PREPARE:
4416 BUG_ON(!list_empty(&rocker_port->trans_mem));
4418 case SWITCHDEV_TRANS_ABORT:
4419 rocker_port_trans_abort(rocker_port);
4426 case SWITCHDEV_ATTR_PORT_STP_STATE:
4427 err = rocker_port_stp_update(rocker_port, attr->trans,
4438 static int rocker_port_switchdev_fib_ipv4_add(struct net_device *dev,
4439 __be32 dst, int dst_len,
4440 struct fib_info *fi,
4442 u32 nlflags, u32 tb_id)
4444 struct rocker_port *rocker_port = netdev_priv(dev);
4447 return rocker_port_fib_ipv4(rocker_port, SWITCHDEV_TRANS_NONE,
4448 dst, dst_len, fi, tb_id, flags);
4451 static int rocker_port_switchdev_fib_ipv4_del(struct net_device *dev,
4452 __be32 dst, int dst_len,
4453 struct fib_info *fi,
4454 u8 tos, u8 type, u32 tb_id)
4456 struct rocker_port *rocker_port = netdev_priv(dev);
4457 int flags = ROCKER_OP_FLAG_REMOVE;
4459 return rocker_port_fib_ipv4(rocker_port, SWITCHDEV_TRANS_NONE,
4460 dst, dst_len, fi, tb_id, flags);
4463 static const struct switchdev_ops rocker_port_switchdev_ops = {
4464 .switchdev_port_attr_get = rocker_port_attr_get,
4465 .switchdev_port_attr_set = rocker_port_attr_set,
4466 .switchdev_fib_ipv4_add = rocker_port_switchdev_fib_ipv4_add,
4467 .switchdev_fib_ipv4_del = rocker_port_switchdev_fib_ipv4_del,
4470 /********************
4472 ********************/
4474 static int rocker_port_get_settings(struct net_device *dev,
4475 struct ethtool_cmd *ecmd)
4477 struct rocker_port *rocker_port = netdev_priv(dev);
4479 return rocker_cmd_get_port_settings_ethtool(rocker_port, ecmd);
4482 static int rocker_port_set_settings(struct net_device *dev,
4483 struct ethtool_cmd *ecmd)
4485 struct rocker_port *rocker_port = netdev_priv(dev);
4487 return rocker_cmd_set_port_settings_ethtool(rocker_port, ecmd);
4490 static void rocker_port_get_drvinfo(struct net_device *dev,
4491 struct ethtool_drvinfo *drvinfo)
4493 strlcpy(drvinfo->driver, rocker_driver_name, sizeof(drvinfo->driver));
4494 strlcpy(drvinfo->version, UTS_RELEASE, sizeof(drvinfo->version));
4497 static struct rocker_port_stats {
4498 char str[ETH_GSTRING_LEN];
4500 } rocker_port_stats[] = {
4501 { "rx_packets", ROCKER_TLV_CMD_PORT_STATS_RX_PKTS, },
4502 { "rx_bytes", ROCKER_TLV_CMD_PORT_STATS_RX_BYTES, },
4503 { "rx_dropped", ROCKER_TLV_CMD_PORT_STATS_RX_DROPPED, },
4504 { "rx_errors", ROCKER_TLV_CMD_PORT_STATS_RX_ERRORS, },
4506 { "tx_packets", ROCKER_TLV_CMD_PORT_STATS_TX_PKTS, },
4507 { "tx_bytes", ROCKER_TLV_CMD_PORT_STATS_TX_BYTES, },
4508 { "tx_dropped", ROCKER_TLV_CMD_PORT_STATS_TX_DROPPED, },
4509 { "tx_errors", ROCKER_TLV_CMD_PORT_STATS_TX_ERRORS, },
4512 #define ROCKER_PORT_STATS_LEN ARRAY_SIZE(rocker_port_stats)
4514 static void rocker_port_get_strings(struct net_device *netdev, u32 stringset,
4520 switch (stringset) {
4522 for (i = 0; i < ARRAY_SIZE(rocker_port_stats); i++) {
4523 memcpy(p, rocker_port_stats[i].str, ETH_GSTRING_LEN);
4524 p += ETH_GSTRING_LEN;
4531 rocker_cmd_get_port_stats_prep(struct rocker *rocker,
4532 struct rocker_port *rocker_port,
4533 struct rocker_desc_info *desc_info,
4536 struct rocker_tlv *cmd_stats;
4538 if (rocker_tlv_put_u16(desc_info, ROCKER_TLV_CMD_TYPE,
4539 ROCKER_TLV_CMD_TYPE_GET_PORT_STATS))
4542 cmd_stats = rocker_tlv_nest_start(desc_info, ROCKER_TLV_CMD_INFO);
4546 if (rocker_tlv_put_u32(desc_info, ROCKER_TLV_CMD_PORT_STATS_PPORT,
4547 rocker_port->pport))
4550 rocker_tlv_nest_end(desc_info, cmd_stats);
4556 rocker_cmd_get_port_stats_ethtool_proc(struct rocker *rocker,
4557 struct rocker_port *rocker_port,
4558 struct rocker_desc_info *desc_info,
4561 struct rocker_tlv *attrs[ROCKER_TLV_CMD_MAX + 1];
4562 struct rocker_tlv *stats_attrs[ROCKER_TLV_CMD_PORT_STATS_MAX + 1];
4563 struct rocker_tlv *pattr;
4568 rocker_tlv_parse_desc(attrs, ROCKER_TLV_CMD_MAX, desc_info);
4570 if (!attrs[ROCKER_TLV_CMD_INFO])
4573 rocker_tlv_parse_nested(stats_attrs, ROCKER_TLV_CMD_PORT_STATS_MAX,
4574 attrs[ROCKER_TLV_CMD_INFO]);
4576 if (!stats_attrs[ROCKER_TLV_CMD_PORT_STATS_PPORT])
4579 pport = rocker_tlv_get_u32(stats_attrs[ROCKER_TLV_CMD_PORT_STATS_PPORT]);
4580 if (pport != rocker_port->pport)
4583 for (i = 0; i < ARRAY_SIZE(rocker_port_stats); i++) {
4584 pattr = stats_attrs[rocker_port_stats[i].type];
4588 data[i] = rocker_tlv_get_u64(pattr);
4594 static int rocker_cmd_get_port_stats_ethtool(struct rocker_port *rocker_port,
4597 return rocker_cmd_exec(rocker_port->rocker, rocker_port,
4598 SWITCHDEV_TRANS_NONE,
4599 rocker_cmd_get_port_stats_prep, NULL,
4600 rocker_cmd_get_port_stats_ethtool_proc,
4604 static void rocker_port_get_stats(struct net_device *dev,
4605 struct ethtool_stats *stats, u64 *data)
4607 struct rocker_port *rocker_port = netdev_priv(dev);
4609 if (rocker_cmd_get_port_stats_ethtool(rocker_port, data) != 0) {
4612 for (i = 0; i < ARRAY_SIZE(rocker_port_stats); ++i)
4619 static int rocker_port_get_sset_count(struct net_device *netdev, int sset)
4623 return ROCKER_PORT_STATS_LEN;
4629 static const struct ethtool_ops rocker_port_ethtool_ops = {
4630 .get_settings = rocker_port_get_settings,
4631 .set_settings = rocker_port_set_settings,
4632 .get_drvinfo = rocker_port_get_drvinfo,
4633 .get_link = ethtool_op_get_link,
4634 .get_strings = rocker_port_get_strings,
4635 .get_ethtool_stats = rocker_port_get_stats,
4636 .get_sset_count = rocker_port_get_sset_count,
4643 static struct rocker_port *rocker_port_napi_tx_get(struct napi_struct *napi)
4645 return container_of(napi, struct rocker_port, napi_tx);
4648 static int rocker_port_poll_tx(struct napi_struct *napi, int budget)
4650 struct rocker_port *rocker_port = rocker_port_napi_tx_get(napi);
4651 struct rocker *rocker = rocker_port->rocker;
4652 struct rocker_desc_info *desc_info;
4656 /* Cleanup tx descriptors */
4657 while ((desc_info = rocker_desc_tail_get(&rocker_port->tx_ring))) {
4658 struct sk_buff *skb;
4660 err = rocker_desc_err(desc_info);
4661 if (err && net_ratelimit())
4662 netdev_err(rocker_port->dev, "tx desc received with err %d\n",
4664 rocker_tx_desc_frags_unmap(rocker_port, desc_info);
4666 skb = rocker_desc_cookie_ptr_get(desc_info);
4668 rocker_port->dev->stats.tx_packets++;
4669 rocker_port->dev->stats.tx_bytes += skb->len;
4671 rocker_port->dev->stats.tx_errors++;
4673 dev_kfree_skb_any(skb);
4677 if (credits && netif_queue_stopped(rocker_port->dev))
4678 netif_wake_queue(rocker_port->dev);
4680 napi_complete(napi);
4681 rocker_dma_ring_credits_set(rocker, &rocker_port->tx_ring, credits);
4686 static int rocker_port_rx_proc(struct rocker *rocker,
4687 struct rocker_port *rocker_port,
4688 struct rocker_desc_info *desc_info)
4690 struct rocker_tlv *attrs[ROCKER_TLV_RX_MAX + 1];
4691 struct sk_buff *skb = rocker_desc_cookie_ptr_get(desc_info);
4697 rocker_tlv_parse_desc(attrs, ROCKER_TLV_RX_MAX, desc_info);
4698 if (!attrs[ROCKER_TLV_RX_FRAG_LEN])
4701 rocker_dma_rx_ring_skb_unmap(rocker, attrs);
4703 rx_len = rocker_tlv_get_u16(attrs[ROCKER_TLV_RX_FRAG_LEN]);
4704 skb_put(skb, rx_len);
4705 skb->protocol = eth_type_trans(skb, rocker_port->dev);
4707 rocker_port->dev->stats.rx_packets++;
4708 rocker_port->dev->stats.rx_bytes += skb->len;
4710 netif_receive_skb(skb);
4712 return rocker_dma_rx_ring_skb_alloc(rocker, rocker_port, desc_info);
4715 static struct rocker_port *rocker_port_napi_rx_get(struct napi_struct *napi)
4717 return container_of(napi, struct rocker_port, napi_rx);
4720 static int rocker_port_poll_rx(struct napi_struct *napi, int budget)
4722 struct rocker_port *rocker_port = rocker_port_napi_rx_get(napi);
4723 struct rocker *rocker = rocker_port->rocker;
4724 struct rocker_desc_info *desc_info;
4728 /* Process rx descriptors */
4729 while (credits < budget &&
4730 (desc_info = rocker_desc_tail_get(&rocker_port->rx_ring))) {
4731 err = rocker_desc_err(desc_info);
4733 if (net_ratelimit())
4734 netdev_err(rocker_port->dev, "rx desc received with err %d\n",
4737 err = rocker_port_rx_proc(rocker, rocker_port,
4739 if (err && net_ratelimit())
4740 netdev_err(rocker_port->dev, "rx processing failed with err %d\n",
4744 rocker_port->dev->stats.rx_errors++;
4746 rocker_desc_gen_clear(desc_info);
4747 rocker_desc_head_set(rocker, &rocker_port->rx_ring, desc_info);
4751 if (credits < budget)
4752 napi_complete(napi);
4754 rocker_dma_ring_credits_set(rocker, &rocker_port->rx_ring, credits);
4763 static void rocker_carrier_init(struct rocker_port *rocker_port)
4765 struct rocker *rocker = rocker_port->rocker;
4766 u64 link_status = rocker_read64(rocker, PORT_PHYS_LINK_STATUS);
4769 link_up = link_status & (1 << rocker_port->pport);
4771 netif_carrier_on(rocker_port->dev);
4773 netif_carrier_off(rocker_port->dev);
4776 static void rocker_remove_ports(struct rocker *rocker)
4778 struct rocker_port *rocker_port;
4781 for (i = 0; i < rocker->port_count; i++) {
4782 rocker_port = rocker->ports[i];
4783 rocker_port_ig_tbl(rocker_port, SWITCHDEV_TRANS_NONE,
4784 ROCKER_OP_FLAG_REMOVE);
4785 unregister_netdev(rocker_port->dev);
4787 kfree(rocker->ports);
4790 static void rocker_port_dev_addr_init(struct rocker *rocker,
4791 struct rocker_port *rocker_port)
4793 struct pci_dev *pdev = rocker->pdev;
4796 err = rocker_cmd_get_port_settings_macaddr(rocker_port,
4797 rocker_port->dev->dev_addr);
4799 dev_warn(&pdev->dev, "failed to get mac address, using random\n");
4800 eth_hw_addr_random(rocker_port->dev);
4804 static int rocker_probe_port(struct rocker *rocker, unsigned int port_number)
4806 struct pci_dev *pdev = rocker->pdev;
4807 struct rocker_port *rocker_port;
4808 struct net_device *dev;
4811 dev = alloc_etherdev(sizeof(struct rocker_port));
4814 rocker_port = netdev_priv(dev);
4815 rocker_port->dev = dev;
4816 rocker_port->rocker = rocker;
4817 rocker_port->port_number = port_number;
4818 rocker_port->pport = port_number + 1;
4819 rocker_port->brport_flags = BR_LEARNING | BR_LEARNING_SYNC;
4820 INIT_LIST_HEAD(&rocker_port->trans_mem);
4822 rocker_port_dev_addr_init(rocker, rocker_port);
4823 dev->netdev_ops = &rocker_port_netdev_ops;
4824 dev->ethtool_ops = &rocker_port_ethtool_ops;
4825 dev->switchdev_ops = &rocker_port_switchdev_ops;
4826 netif_napi_add(dev, &rocker_port->napi_tx, rocker_port_poll_tx,
4828 netif_napi_add(dev, &rocker_port->napi_rx, rocker_port_poll_rx,
4830 rocker_carrier_init(rocker_port);
4832 dev->features |= NETIF_F_NETNS_LOCAL |
4833 NETIF_F_HW_VLAN_CTAG_FILTER |
4834 NETIF_F_HW_SWITCH_OFFLOAD;
4836 err = register_netdev(dev);
4838 dev_err(&pdev->dev, "register_netdev failed\n");
4839 goto err_register_netdev;
4841 rocker->ports[port_number] = rocker_port;
4843 rocker_port_set_learning(rocker_port, SWITCHDEV_TRANS_NONE);
4845 rocker_port->internal_vlan_id =
4846 rocker_port_internal_vlan_id_get(rocker_port,
4847 SWITCHDEV_TRANS_NONE,
4849 err = rocker_port_ig_tbl(rocker_port, SWITCHDEV_TRANS_NONE, 0);
4851 dev_err(&pdev->dev, "install ig port table failed\n");
4852 goto err_port_ig_tbl;
4858 unregister_netdev(dev);
4859 err_register_netdev:
4864 static int rocker_probe_ports(struct rocker *rocker)
4870 alloc_size = sizeof(struct rocker_port *) * rocker->port_count;
4871 rocker->ports = kmalloc(alloc_size, GFP_KERNEL);
4874 for (i = 0; i < rocker->port_count; i++) {
4875 err = rocker_probe_port(rocker, i);
4882 rocker_remove_ports(rocker);
4886 static int rocker_msix_init(struct rocker *rocker)
4888 struct pci_dev *pdev = rocker->pdev;
4893 msix_entries = pci_msix_vec_count(pdev);
4894 if (msix_entries < 0)
4895 return msix_entries;
4897 if (msix_entries != ROCKER_MSIX_VEC_COUNT(rocker->port_count))
4900 rocker->msix_entries = kmalloc_array(msix_entries,
4901 sizeof(struct msix_entry),
4903 if (!rocker->msix_entries)
4906 for (i = 0; i < msix_entries; i++)
4907 rocker->msix_entries[i].entry = i;
4909 err = pci_enable_msix_exact(pdev, rocker->msix_entries, msix_entries);
4911 goto err_enable_msix;
4916 kfree(rocker->msix_entries);
4920 static void rocker_msix_fini(struct rocker *rocker)
4922 pci_disable_msix(rocker->pdev);
4923 kfree(rocker->msix_entries);
4926 static int rocker_probe(struct pci_dev *pdev, const struct pci_device_id *id)
4928 struct rocker *rocker;
4931 rocker = kzalloc(sizeof(*rocker), GFP_KERNEL);
4935 err = pci_enable_device(pdev);
4937 dev_err(&pdev->dev, "pci_enable_device failed\n");
4938 goto err_pci_enable_device;
4941 err = pci_request_regions(pdev, rocker_driver_name);
4943 dev_err(&pdev->dev, "pci_request_regions failed\n");
4944 goto err_pci_request_regions;
4947 err = pci_set_dma_mask(pdev, DMA_BIT_MASK(64));
4949 err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64));
4951 dev_err(&pdev->dev, "pci_set_consistent_dma_mask failed\n");
4952 goto err_pci_set_dma_mask;
4955 err = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
4957 dev_err(&pdev->dev, "pci_set_dma_mask failed\n");
4958 goto err_pci_set_dma_mask;
4962 if (pci_resource_len(pdev, 0) < ROCKER_PCI_BAR0_SIZE) {
4963 dev_err(&pdev->dev, "invalid PCI region size\n");
4965 goto err_pci_resource_len_check;
4968 rocker->hw_addr = ioremap(pci_resource_start(pdev, 0),
4969 pci_resource_len(pdev, 0));
4970 if (!rocker->hw_addr) {
4971 dev_err(&pdev->dev, "ioremap failed\n");
4975 pci_set_master(pdev);
4977 rocker->pdev = pdev;
4978 pci_set_drvdata(pdev, rocker);
4980 rocker->port_count = rocker_read32(rocker, PORT_PHYS_COUNT);
4982 err = rocker_msix_init(rocker);
4984 dev_err(&pdev->dev, "MSI-X init failed\n");
4988 err = rocker_basic_hw_test(rocker);
4990 dev_err(&pdev->dev, "basic hw test failed\n");
4991 goto err_basic_hw_test;
4994 rocker_write32(rocker, CONTROL, ROCKER_CONTROL_RESET);
4996 err = rocker_dma_rings_init(rocker);
4998 goto err_dma_rings_init;
5000 err = request_irq(rocker_msix_vector(rocker, ROCKER_MSIX_VEC_CMD),
5001 rocker_cmd_irq_handler, 0,
5002 rocker_driver_name, rocker);
5004 dev_err(&pdev->dev, "cannot assign cmd irq\n");
5005 goto err_request_cmd_irq;
5008 err = request_irq(rocker_msix_vector(rocker, ROCKER_MSIX_VEC_EVENT),
5009 rocker_event_irq_handler, 0,
5010 rocker_driver_name, rocker);
5012 dev_err(&pdev->dev, "cannot assign event irq\n");
5013 goto err_request_event_irq;
5016 rocker->hw.id = rocker_read64(rocker, SWITCH_ID);
5018 err = rocker_init_tbls(rocker);
5020 dev_err(&pdev->dev, "cannot init rocker tables\n");
5024 err = rocker_probe_ports(rocker);
5026 dev_err(&pdev->dev, "failed to probe ports\n");
5027 goto err_probe_ports;
5030 dev_info(&pdev->dev, "Rocker switch with id %016llx\n", rocker->hw.id);
5035 rocker_free_tbls(rocker);
5037 free_irq(rocker_msix_vector(rocker, ROCKER_MSIX_VEC_EVENT), rocker);
5038 err_request_event_irq:
5039 free_irq(rocker_msix_vector(rocker, ROCKER_MSIX_VEC_CMD), rocker);
5040 err_request_cmd_irq:
5041 rocker_dma_rings_fini(rocker);
5044 rocker_msix_fini(rocker);
5046 iounmap(rocker->hw_addr);
5048 err_pci_resource_len_check:
5049 err_pci_set_dma_mask:
5050 pci_release_regions(pdev);
5051 err_pci_request_regions:
5052 pci_disable_device(pdev);
5053 err_pci_enable_device:
5058 static void rocker_remove(struct pci_dev *pdev)
5060 struct rocker *rocker = pci_get_drvdata(pdev);
5062 rocker_free_tbls(rocker);
5063 rocker_write32(rocker, CONTROL, ROCKER_CONTROL_RESET);
5064 rocker_remove_ports(rocker);
5065 free_irq(rocker_msix_vector(rocker, ROCKER_MSIX_VEC_EVENT), rocker);
5066 free_irq(rocker_msix_vector(rocker, ROCKER_MSIX_VEC_CMD), rocker);
5067 rocker_dma_rings_fini(rocker);
5068 rocker_msix_fini(rocker);
5069 iounmap(rocker->hw_addr);
5070 pci_release_regions(rocker->pdev);
5071 pci_disable_device(rocker->pdev);
5075 static struct pci_driver rocker_pci_driver = {
5076 .name = rocker_driver_name,
5077 .id_table = rocker_pci_id_table,
5078 .probe = rocker_probe,
5079 .remove = rocker_remove,
5082 /************************************
5083 * Net device notifier event handler
5084 ************************************/
5086 static bool rocker_port_dev_check(struct net_device *dev)
5088 return dev->netdev_ops == &rocker_port_netdev_ops;
5091 static int rocker_port_bridge_join(struct rocker_port *rocker_port,
5092 struct net_device *bridge)
5096 rocker_port_internal_vlan_id_put(rocker_port, SWITCHDEV_TRANS_NONE,
5097 rocker_port->dev->ifindex);
5099 rocker_port->bridge_dev = bridge;
5101 /* Use bridge internal VLAN ID for untagged pkts */
5102 err = rocker_port_vlan(rocker_port, SWITCHDEV_TRANS_NONE,
5103 ROCKER_OP_FLAG_REMOVE, 0);
5106 rocker_port->internal_vlan_id =
5107 rocker_port_internal_vlan_id_get(rocker_port,
5108 SWITCHDEV_TRANS_NONE,
5110 return rocker_port_vlan(rocker_port, SWITCHDEV_TRANS_NONE, 0, 0);
5113 static int rocker_port_bridge_leave(struct rocker_port *rocker_port)
5117 rocker_port_internal_vlan_id_put(rocker_port, SWITCHDEV_TRANS_NONE,
5118 rocker_port->bridge_dev->ifindex);
5120 rocker_port->bridge_dev = NULL;
5122 /* Use port internal VLAN ID for untagged pkts */
5123 err = rocker_port_vlan(rocker_port, SWITCHDEV_TRANS_NONE,
5124 ROCKER_OP_FLAG_REMOVE, 0);
5127 rocker_port->internal_vlan_id =
5128 rocker_port_internal_vlan_id_get(rocker_port,
5129 SWITCHDEV_TRANS_NONE,
5130 rocker_port->dev->ifindex);
5131 err = rocker_port_vlan(rocker_port, SWITCHDEV_TRANS_NONE, 0, 0);
5135 if (rocker_port->dev->flags & IFF_UP)
5136 err = rocker_port_fwd_enable(rocker_port, SWITCHDEV_TRANS_NONE);
5141 static int rocker_port_master_changed(struct net_device *dev)
5143 struct rocker_port *rocker_port = netdev_priv(dev);
5144 struct net_device *master = netdev_master_upper_dev_get(dev);
5147 /* There are currently three cases handled here:
5148 * 1. Joining a bridge
5149 * 2. Leaving a previously joined bridge
5150 * 3. Other, e.g. being added to or removed from a bond or openvswitch,
5151 * in which case nothing is done
5153 if (master && master->rtnl_link_ops &&
5154 !strcmp(master->rtnl_link_ops->kind, "bridge"))
5155 err = rocker_port_bridge_join(rocker_port, master);
5156 else if (rocker_port_is_bridged(rocker_port))
5157 err = rocker_port_bridge_leave(rocker_port);
5162 static int rocker_netdevice_event(struct notifier_block *unused,
5163 unsigned long event, void *ptr)
5165 struct net_device *dev;
5169 case NETDEV_CHANGEUPPER:
5170 dev = netdev_notifier_info_to_dev(ptr);
5171 if (!rocker_port_dev_check(dev))
5173 err = rocker_port_master_changed(dev);
5176 "failed to reflect master change (err %d)\n",
5184 static struct notifier_block rocker_netdevice_nb __read_mostly = {
5185 .notifier_call = rocker_netdevice_event,
5188 /************************************
5189 * Net event notifier event handler
5190 ************************************/
5192 static int rocker_neigh_update(struct net_device *dev, struct neighbour *n)
5194 struct rocker_port *rocker_port = netdev_priv(dev);
5195 int flags = (n->nud_state & NUD_VALID) ? 0 : ROCKER_OP_FLAG_REMOVE;
5196 __be32 ip_addr = *(__be32 *)n->primary_key;
5198 return rocker_port_ipv4_neigh(rocker_port, SWITCHDEV_TRANS_NONE,
5199 flags, ip_addr, n->ha);
5202 static int rocker_netevent_event(struct notifier_block *unused,
5203 unsigned long event, void *ptr)
5205 struct net_device *dev;
5206 struct neighbour *n = ptr;
5210 case NETEVENT_NEIGH_UPDATE:
5211 if (n->tbl != &arp_tbl)
5214 if (!rocker_port_dev_check(dev))
5216 err = rocker_neigh_update(dev, n);
5219 "failed to handle neigh update (err %d)\n",
5227 static struct notifier_block rocker_netevent_nb __read_mostly = {
5228 .notifier_call = rocker_netevent_event,
5231 /***********************
5232 * Module init and exit
5233 ***********************/
5235 static int __init rocker_module_init(void)
5239 register_netdevice_notifier(&rocker_netdevice_nb);
5240 register_netevent_notifier(&rocker_netevent_nb);
5241 err = pci_register_driver(&rocker_pci_driver);
5243 goto err_pci_register_driver;
5246 err_pci_register_driver:
5247 unregister_netdevice_notifier(&rocker_netevent_nb);
5248 unregister_netdevice_notifier(&rocker_netdevice_nb);
5252 static void __exit rocker_module_exit(void)
5254 unregister_netevent_notifier(&rocker_netevent_nb);
5255 unregister_netdevice_notifier(&rocker_netdevice_nb);
5256 pci_unregister_driver(&rocker_pci_driver);
5259 module_init(rocker_module_init);
5260 module_exit(rocker_module_exit);
5262 MODULE_LICENSE("GPL v2");
5263 MODULE_AUTHOR("Jiri Pirko <jiri@resnulli.us>");
5264 MODULE_AUTHOR("Scott Feldman <sfeldma@gmail.com>");
5265 MODULE_DESCRIPTION("Rocker switch device driver");
5266 MODULE_DEVICE_TABLE(pci, rocker_pci_id_table);