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 <net/switchdev.h>
34 #include <net/rtnetlink.h>
35 #include <net/ip_fib.h>
36 #include <net/netevent.h>
38 #include <asm-generic/io-64-nonatomic-lo-hi.h>
39 #include <generated/utsrelease.h>
43 static const char rocker_driver_name[] = "rocker";
45 static const struct pci_device_id rocker_pci_id_table[] = {
46 {PCI_VDEVICE(REDHAT, PCI_DEVICE_ID_REDHAT_ROCKER), 0},
50 struct rocker_flow_tbl_key {
52 enum rocker_of_dpa_table_id tbl_id;
57 enum rocker_of_dpa_table_id goto_tbl;
63 enum rocker_of_dpa_table_id goto_tbl;
72 u8 eth_dst_mask[ETH_ALEN];
75 enum rocker_of_dpa_table_id goto_tbl;
82 enum rocker_of_dpa_table_id goto_tbl;
87 u8 eth_dst_mask[ETH_ALEN];
92 enum rocker_of_dpa_table_id goto_tbl;
100 u8 eth_src_mask[ETH_ALEN];
101 u8 eth_dst[ETH_ALEN];
102 u8 eth_dst_mask[ETH_ALEN];
115 struct rocker_flow_tbl_entry {
116 struct hlist_node entry;
119 struct rocker_flow_tbl_key key;
121 u32 key_crc32; /* key */
124 struct rocker_group_tbl_entry {
125 struct hlist_node entry;
127 u32 group_id; /* key */
135 u8 eth_src[ETH_ALEN];
136 u8 eth_dst[ETH_ALEN];
141 u8 eth_src[ETH_ALEN];
142 u8 eth_dst[ETH_ALEN];
150 struct rocker_fdb_tbl_entry {
151 struct hlist_node entry;
152 u32 key_crc32; /* key */
154 struct rocker_fdb_tbl_key {
161 struct rocker_internal_vlan_tbl_entry {
162 struct hlist_node entry;
163 int ifindex; /* key */
168 struct rocker_neigh_tbl_entry {
169 struct hlist_node entry;
170 __be32 ip_addr; /* key */
171 struct net_device *dev;
174 u8 eth_dst[ETH_ALEN];
178 struct rocker_desc_info {
179 char *data; /* mapped */
182 struct rocker_desc *desc;
183 DEFINE_DMA_UNMAP_ADDR(mapaddr);
186 struct rocker_dma_ring_info {
190 struct rocker_desc *desc; /* mapped */
192 struct rocker_desc_info *desc_info;
199 ROCKER_CTRL_LINK_LOCAL_MCAST,
200 ROCKER_CTRL_LOCAL_ARP,
201 ROCKER_CTRL_IPV4_MCAST,
202 ROCKER_CTRL_IPV6_MCAST,
203 ROCKER_CTRL_DFLT_BRIDGING,
207 #define ROCKER_INTERNAL_VLAN_ID_BASE 0x0f00
208 #define ROCKER_N_INTERNAL_VLANS 255
209 #define ROCKER_VLAN_BITMAP_LEN BITS_TO_LONGS(VLAN_N_VID)
210 #define ROCKER_INTERNAL_VLAN_BITMAP_LEN BITS_TO_LONGS(ROCKER_N_INTERNAL_VLANS)
213 struct net_device *dev;
214 struct net_device *bridge_dev;
215 struct rocker *rocker;
216 unsigned int port_number;
218 __be16 internal_vlan_id;
221 bool ctrls[ROCKER_CTRL_MAX];
222 unsigned long vlan_bitmap[ROCKER_VLAN_BITMAP_LEN];
223 struct napi_struct napi_tx;
224 struct napi_struct napi_rx;
225 struct rocker_dma_ring_info tx_ring;
226 struct rocker_dma_ring_info rx_ring;
230 struct pci_dev *pdev;
232 struct msix_entry *msix_entries;
233 unsigned int port_count;
234 struct rocker_port **ports;
238 spinlock_t cmd_ring_lock;
239 struct rocker_dma_ring_info cmd_ring;
240 struct rocker_dma_ring_info event_ring;
241 DECLARE_HASHTABLE(flow_tbl, 16);
242 spinlock_t flow_tbl_lock;
243 u64 flow_tbl_next_cookie;
244 DECLARE_HASHTABLE(group_tbl, 16);
245 spinlock_t group_tbl_lock;
246 DECLARE_HASHTABLE(fdb_tbl, 16);
247 spinlock_t fdb_tbl_lock;
248 unsigned long internal_vlan_bitmap[ROCKER_INTERNAL_VLAN_BITMAP_LEN];
249 DECLARE_HASHTABLE(internal_vlan_tbl, 8);
250 spinlock_t internal_vlan_tbl_lock;
251 DECLARE_HASHTABLE(neigh_tbl, 16);
252 spinlock_t neigh_tbl_lock;
253 u32 neigh_tbl_next_index;
256 static const u8 zero_mac[ETH_ALEN] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
257 static const u8 ff_mac[ETH_ALEN] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
258 static const u8 ll_mac[ETH_ALEN] = { 0x01, 0x80, 0xc2, 0x00, 0x00, 0x00 };
259 static const u8 ll_mask[ETH_ALEN] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xf0 };
260 static const u8 mcast_mac[ETH_ALEN] = { 0x01, 0x00, 0x00, 0x00, 0x00, 0x00 };
261 static const u8 ipv4_mcast[ETH_ALEN] = { 0x01, 0x00, 0x5e, 0x00, 0x00, 0x00 };
262 static const u8 ipv4_mask[ETH_ALEN] = { 0xff, 0xff, 0xff, 0x80, 0x00, 0x00 };
263 static const u8 ipv6_mcast[ETH_ALEN] = { 0x33, 0x33, 0x00, 0x00, 0x00, 0x00 };
264 static const u8 ipv6_mask[ETH_ALEN] = { 0xff, 0xff, 0x00, 0x00, 0x00, 0x00 };
266 /* Rocker priority levels for flow table entries. Higher
267 * priority match takes precedence over lower priority match.
271 ROCKER_PRIORITY_UNKNOWN = 0,
272 ROCKER_PRIORITY_IG_PORT = 1,
273 ROCKER_PRIORITY_VLAN = 1,
274 ROCKER_PRIORITY_TERM_MAC_UCAST = 0,
275 ROCKER_PRIORITY_TERM_MAC_MCAST = 1,
276 ROCKER_PRIORITY_BRIDGING_VLAN_DFLT_EXACT = 1,
277 ROCKER_PRIORITY_BRIDGING_VLAN_DFLT_WILD = 2,
278 ROCKER_PRIORITY_BRIDGING_VLAN = 3,
279 ROCKER_PRIORITY_BRIDGING_TENANT_DFLT_EXACT = 1,
280 ROCKER_PRIORITY_BRIDGING_TENANT_DFLT_WILD = 2,
281 ROCKER_PRIORITY_BRIDGING_TENANT = 3,
282 ROCKER_PRIORITY_ACL_CTRL = 3,
283 ROCKER_PRIORITY_ACL_NORMAL = 2,
284 ROCKER_PRIORITY_ACL_DFLT = 1,
287 static bool rocker_vlan_id_is_internal(__be16 vlan_id)
289 u16 start = ROCKER_INTERNAL_VLAN_ID_BASE;
291 u16 _vlan_id = ntohs(vlan_id);
293 return (_vlan_id >= start && _vlan_id <= end);
296 static __be16 rocker_port_vid_to_vlan(struct rocker_port *rocker_port,
297 u16 vid, bool *pop_vlan)
303 vlan_id = htons(vid);
305 vlan_id = rocker_port->internal_vlan_id;
313 static u16 rocker_port_vlan_to_vid(struct rocker_port *rocker_port,
316 if (rocker_vlan_id_is_internal(vlan_id))
319 return ntohs(vlan_id);
322 static bool rocker_port_is_bridged(struct rocker_port *rocker_port)
324 return !!rocker_port->bridge_dev;
328 wait_queue_head_t wait;
333 static void rocker_wait_reset(struct rocker_wait *wait)
336 wait->nowait = false;
339 static void rocker_wait_init(struct rocker_wait *wait)
341 init_waitqueue_head(&wait->wait);
342 rocker_wait_reset(wait);
345 static struct rocker_wait *rocker_wait_create(gfp_t gfp)
347 struct rocker_wait *wait;
349 wait = kmalloc(sizeof(*wait), gfp);
352 rocker_wait_init(wait);
356 static void rocker_wait_destroy(struct rocker_wait *work)
361 static bool rocker_wait_event_timeout(struct rocker_wait *wait,
362 unsigned long timeout)
364 wait_event_timeout(wait->wait, wait->done, HZ / 10);
370 static void rocker_wait_wake_up(struct rocker_wait *wait)
373 wake_up(&wait->wait);
376 static u32 rocker_msix_vector(struct rocker *rocker, unsigned int vector)
378 return rocker->msix_entries[vector].vector;
381 static u32 rocker_msix_tx_vector(struct rocker_port *rocker_port)
383 return rocker_msix_vector(rocker_port->rocker,
384 ROCKER_MSIX_VEC_TX(rocker_port->port_number));
387 static u32 rocker_msix_rx_vector(struct rocker_port *rocker_port)
389 return rocker_msix_vector(rocker_port->rocker,
390 ROCKER_MSIX_VEC_RX(rocker_port->port_number));
393 #define rocker_write32(rocker, reg, val) \
394 writel((val), (rocker)->hw_addr + (ROCKER_ ## reg))
395 #define rocker_read32(rocker, reg) \
396 readl((rocker)->hw_addr + (ROCKER_ ## reg))
397 #define rocker_write64(rocker, reg, val) \
398 writeq((val), (rocker)->hw_addr + (ROCKER_ ## reg))
399 #define rocker_read64(rocker, reg) \
400 readq((rocker)->hw_addr + (ROCKER_ ## reg))
402 /*****************************
403 * HW basic testing functions
404 *****************************/
406 static int rocker_reg_test(struct rocker *rocker)
408 struct pci_dev *pdev = rocker->pdev;
414 rocker_write32(rocker, TEST_REG, rnd);
415 test_reg = rocker_read32(rocker, TEST_REG);
416 if (test_reg != rnd * 2) {
417 dev_err(&pdev->dev, "unexpected 32bit register value %08llx, expected %08llx\n",
424 rnd |= prandom_u32();
425 rocker_write64(rocker, TEST_REG64, rnd);
426 test_reg = rocker_read64(rocker, TEST_REG64);
427 if (test_reg != rnd * 2) {
428 dev_err(&pdev->dev, "unexpected 64bit register value %16llx, expected %16llx\n",
436 static int rocker_dma_test_one(struct rocker *rocker, struct rocker_wait *wait,
437 u32 test_type, dma_addr_t dma_handle,
438 unsigned char *buf, unsigned char *expect,
441 struct pci_dev *pdev = rocker->pdev;
444 rocker_wait_reset(wait);
445 rocker_write32(rocker, TEST_DMA_CTRL, test_type);
447 if (!rocker_wait_event_timeout(wait, HZ / 10)) {
448 dev_err(&pdev->dev, "no interrupt received within a timeout\n");
452 for (i = 0; i < size; i++) {
453 if (buf[i] != expect[i]) {
454 dev_err(&pdev->dev, "unexpected memory content %02x at byte %x\n, %02x expected",
455 buf[i], i, expect[i]);
462 #define ROCKER_TEST_DMA_BUF_SIZE (PAGE_SIZE * 4)
463 #define ROCKER_TEST_DMA_FILL_PATTERN 0x96
465 static int rocker_dma_test_offset(struct rocker *rocker,
466 struct rocker_wait *wait, int offset)
468 struct pci_dev *pdev = rocker->pdev;
469 unsigned char *alloc;
471 unsigned char *expect;
472 dma_addr_t dma_handle;
476 alloc = kzalloc(ROCKER_TEST_DMA_BUF_SIZE * 2 + offset,
477 GFP_KERNEL | GFP_DMA);
480 buf = alloc + offset;
481 expect = buf + ROCKER_TEST_DMA_BUF_SIZE;
483 dma_handle = pci_map_single(pdev, buf, ROCKER_TEST_DMA_BUF_SIZE,
484 PCI_DMA_BIDIRECTIONAL);
485 if (pci_dma_mapping_error(pdev, dma_handle)) {
490 rocker_write64(rocker, TEST_DMA_ADDR, dma_handle);
491 rocker_write32(rocker, TEST_DMA_SIZE, ROCKER_TEST_DMA_BUF_SIZE);
493 memset(expect, ROCKER_TEST_DMA_FILL_PATTERN, ROCKER_TEST_DMA_BUF_SIZE);
494 err = rocker_dma_test_one(rocker, wait, ROCKER_TEST_DMA_CTRL_FILL,
495 dma_handle, buf, expect,
496 ROCKER_TEST_DMA_BUF_SIZE);
500 memset(expect, 0, ROCKER_TEST_DMA_BUF_SIZE);
501 err = rocker_dma_test_one(rocker, wait, ROCKER_TEST_DMA_CTRL_CLEAR,
502 dma_handle, buf, expect,
503 ROCKER_TEST_DMA_BUF_SIZE);
507 prandom_bytes(buf, ROCKER_TEST_DMA_BUF_SIZE);
508 for (i = 0; i < ROCKER_TEST_DMA_BUF_SIZE; i++)
510 err = rocker_dma_test_one(rocker, wait, ROCKER_TEST_DMA_CTRL_INVERT,
511 dma_handle, buf, expect,
512 ROCKER_TEST_DMA_BUF_SIZE);
517 pci_unmap_single(pdev, dma_handle, ROCKER_TEST_DMA_BUF_SIZE,
518 PCI_DMA_BIDIRECTIONAL);
525 static int rocker_dma_test(struct rocker *rocker, struct rocker_wait *wait)
530 for (i = 0; i < 8; i++) {
531 err = rocker_dma_test_offset(rocker, wait, i);
538 static irqreturn_t rocker_test_irq_handler(int irq, void *dev_id)
540 struct rocker_wait *wait = dev_id;
542 rocker_wait_wake_up(wait);
547 static int rocker_basic_hw_test(struct rocker *rocker)
549 struct pci_dev *pdev = rocker->pdev;
550 struct rocker_wait wait;
553 err = rocker_reg_test(rocker);
555 dev_err(&pdev->dev, "reg test failed\n");
559 err = request_irq(rocker_msix_vector(rocker, ROCKER_MSIX_VEC_TEST),
560 rocker_test_irq_handler, 0,
561 rocker_driver_name, &wait);
563 dev_err(&pdev->dev, "cannot assign test irq\n");
567 rocker_wait_init(&wait);
568 rocker_write32(rocker, TEST_IRQ, ROCKER_MSIX_VEC_TEST);
570 if (!rocker_wait_event_timeout(&wait, HZ / 10)) {
571 dev_err(&pdev->dev, "no interrupt received within a timeout\n");
576 err = rocker_dma_test(rocker, &wait);
578 dev_err(&pdev->dev, "dma test failed\n");
581 free_irq(rocker_msix_vector(rocker, ROCKER_MSIX_VEC_TEST), &wait);
589 #define ROCKER_TLV_ALIGNTO 8U
590 #define ROCKER_TLV_ALIGN(len) \
591 (((len) + ROCKER_TLV_ALIGNTO - 1) & ~(ROCKER_TLV_ALIGNTO - 1))
592 #define ROCKER_TLV_HDRLEN ROCKER_TLV_ALIGN(sizeof(struct rocker_tlv))
594 /* <------- ROCKER_TLV_HDRLEN -------> <--- ROCKER_TLV_ALIGN(payload) --->
595 * +-----------------------------+- - -+- - - - - - - - - - - - - - -+- - -+
596 * | Header | Pad | Payload | Pad |
597 * | (struct rocker_tlv) | ing | | ing |
598 * +-----------------------------+- - -+- - - - - - - - - - - - - - -+- - -+
599 * <--------------------------- tlv->len -------------------------->
602 static struct rocker_tlv *rocker_tlv_next(const struct rocker_tlv *tlv,
605 int totlen = ROCKER_TLV_ALIGN(tlv->len);
607 *remaining -= totlen;
608 return (struct rocker_tlv *) ((char *) tlv + totlen);
611 static int rocker_tlv_ok(const struct rocker_tlv *tlv, int remaining)
613 return remaining >= (int) ROCKER_TLV_HDRLEN &&
614 tlv->len >= ROCKER_TLV_HDRLEN &&
615 tlv->len <= remaining;
618 #define rocker_tlv_for_each(pos, head, len, rem) \
619 for (pos = head, rem = len; \
620 rocker_tlv_ok(pos, rem); \
621 pos = rocker_tlv_next(pos, &(rem)))
623 #define rocker_tlv_for_each_nested(pos, tlv, rem) \
624 rocker_tlv_for_each(pos, rocker_tlv_data(tlv), \
625 rocker_tlv_len(tlv), rem)
627 static int rocker_tlv_attr_size(int payload)
629 return ROCKER_TLV_HDRLEN + payload;
632 static int rocker_tlv_total_size(int payload)
634 return ROCKER_TLV_ALIGN(rocker_tlv_attr_size(payload));
637 static int rocker_tlv_padlen(int payload)
639 return rocker_tlv_total_size(payload) - rocker_tlv_attr_size(payload);
642 static int rocker_tlv_type(const struct rocker_tlv *tlv)
647 static void *rocker_tlv_data(const struct rocker_tlv *tlv)
649 return (char *) tlv + ROCKER_TLV_HDRLEN;
652 static int rocker_tlv_len(const struct rocker_tlv *tlv)
654 return tlv->len - ROCKER_TLV_HDRLEN;
657 static u8 rocker_tlv_get_u8(const struct rocker_tlv *tlv)
659 return *(u8 *) rocker_tlv_data(tlv);
662 static u16 rocker_tlv_get_u16(const struct rocker_tlv *tlv)
664 return *(u16 *) rocker_tlv_data(tlv);
667 static __be16 rocker_tlv_get_be16(const struct rocker_tlv *tlv)
669 return *(__be16 *) rocker_tlv_data(tlv);
672 static u32 rocker_tlv_get_u32(const struct rocker_tlv *tlv)
674 return *(u32 *) rocker_tlv_data(tlv);
677 static u64 rocker_tlv_get_u64(const struct rocker_tlv *tlv)
679 return *(u64 *) rocker_tlv_data(tlv);
682 static void rocker_tlv_parse(struct rocker_tlv **tb, int maxtype,
683 const char *buf, int buf_len)
685 const struct rocker_tlv *tlv;
686 const struct rocker_tlv *head = (const struct rocker_tlv *) buf;
689 memset(tb, 0, sizeof(struct rocker_tlv *) * (maxtype + 1));
691 rocker_tlv_for_each(tlv, head, buf_len, rem) {
692 u32 type = rocker_tlv_type(tlv);
694 if (type > 0 && type <= maxtype)
695 tb[type] = (struct rocker_tlv *) tlv;
699 static void rocker_tlv_parse_nested(struct rocker_tlv **tb, int maxtype,
700 const struct rocker_tlv *tlv)
702 rocker_tlv_parse(tb, maxtype, rocker_tlv_data(tlv),
703 rocker_tlv_len(tlv));
706 static void rocker_tlv_parse_desc(struct rocker_tlv **tb, int maxtype,
707 struct rocker_desc_info *desc_info)
709 rocker_tlv_parse(tb, maxtype, desc_info->data,
710 desc_info->desc->tlv_size);
713 static struct rocker_tlv *rocker_tlv_start(struct rocker_desc_info *desc_info)
715 return (struct rocker_tlv *) ((char *) desc_info->data +
716 desc_info->tlv_size);
719 static int rocker_tlv_put(struct rocker_desc_info *desc_info,
720 int attrtype, int attrlen, const void *data)
722 int tail_room = desc_info->data_size - desc_info->tlv_size;
723 int total_size = rocker_tlv_total_size(attrlen);
724 struct rocker_tlv *tlv;
726 if (unlikely(tail_room < total_size))
729 tlv = rocker_tlv_start(desc_info);
730 desc_info->tlv_size += total_size;
731 tlv->type = attrtype;
732 tlv->len = rocker_tlv_attr_size(attrlen);
733 memcpy(rocker_tlv_data(tlv), data, attrlen);
734 memset((char *) tlv + tlv->len, 0, rocker_tlv_padlen(attrlen));
738 static int rocker_tlv_put_u8(struct rocker_desc_info *desc_info,
739 int attrtype, u8 value)
741 return rocker_tlv_put(desc_info, attrtype, sizeof(u8), &value);
744 static int rocker_tlv_put_u16(struct rocker_desc_info *desc_info,
745 int attrtype, u16 value)
747 return rocker_tlv_put(desc_info, attrtype, sizeof(u16), &value);
750 static int rocker_tlv_put_be16(struct rocker_desc_info *desc_info,
751 int attrtype, __be16 value)
753 return rocker_tlv_put(desc_info, attrtype, sizeof(__be16), &value);
756 static int rocker_tlv_put_u32(struct rocker_desc_info *desc_info,
757 int attrtype, u32 value)
759 return rocker_tlv_put(desc_info, attrtype, sizeof(u32), &value);
762 static int rocker_tlv_put_be32(struct rocker_desc_info *desc_info,
763 int attrtype, __be32 value)
765 return rocker_tlv_put(desc_info, attrtype, sizeof(__be32), &value);
768 static int rocker_tlv_put_u64(struct rocker_desc_info *desc_info,
769 int attrtype, u64 value)
771 return rocker_tlv_put(desc_info, attrtype, sizeof(u64), &value);
774 static struct rocker_tlv *
775 rocker_tlv_nest_start(struct rocker_desc_info *desc_info, int attrtype)
777 struct rocker_tlv *start = rocker_tlv_start(desc_info);
779 if (rocker_tlv_put(desc_info, attrtype, 0, NULL) < 0)
785 static void rocker_tlv_nest_end(struct rocker_desc_info *desc_info,
786 struct rocker_tlv *start)
788 start->len = (char *) rocker_tlv_start(desc_info) - (char *) start;
791 static void rocker_tlv_nest_cancel(struct rocker_desc_info *desc_info,
792 struct rocker_tlv *start)
794 desc_info->tlv_size = (char *) start - desc_info->data;
797 /******************************************
798 * DMA rings and descriptors manipulations
799 ******************************************/
801 static u32 __pos_inc(u32 pos, size_t limit)
803 return ++pos == limit ? 0 : pos;
806 static int rocker_desc_err(struct rocker_desc_info *desc_info)
808 int err = desc_info->desc->comp_err & ~ROCKER_DMA_DESC_COMP_ERR_GEN;
823 case -ROCKER_EMSGSIZE:
825 case -ROCKER_ENOTSUP:
827 case -ROCKER_ENOBUFS:
834 static void rocker_desc_gen_clear(struct rocker_desc_info *desc_info)
836 desc_info->desc->comp_err &= ~ROCKER_DMA_DESC_COMP_ERR_GEN;
839 static bool rocker_desc_gen(struct rocker_desc_info *desc_info)
841 u32 comp_err = desc_info->desc->comp_err;
843 return comp_err & ROCKER_DMA_DESC_COMP_ERR_GEN ? true : false;
846 static void *rocker_desc_cookie_ptr_get(struct rocker_desc_info *desc_info)
848 return (void *)(uintptr_t)desc_info->desc->cookie;
851 static void rocker_desc_cookie_ptr_set(struct rocker_desc_info *desc_info,
854 desc_info->desc->cookie = (uintptr_t) ptr;
857 static struct rocker_desc_info *
858 rocker_desc_head_get(struct rocker_dma_ring_info *info)
860 static struct rocker_desc_info *desc_info;
861 u32 head = __pos_inc(info->head, info->size);
863 desc_info = &info->desc_info[info->head];
864 if (head == info->tail)
865 return NULL; /* ring full */
866 desc_info->tlv_size = 0;
870 static void rocker_desc_commit(struct rocker_desc_info *desc_info)
872 desc_info->desc->buf_size = desc_info->data_size;
873 desc_info->desc->tlv_size = desc_info->tlv_size;
876 static void rocker_desc_head_set(struct rocker *rocker,
877 struct rocker_dma_ring_info *info,
878 struct rocker_desc_info *desc_info)
880 u32 head = __pos_inc(info->head, info->size);
882 BUG_ON(head == info->tail);
883 rocker_desc_commit(desc_info);
885 rocker_write32(rocker, DMA_DESC_HEAD(info->type), head);
888 static struct rocker_desc_info *
889 rocker_desc_tail_get(struct rocker_dma_ring_info *info)
891 static struct rocker_desc_info *desc_info;
893 if (info->tail == info->head)
894 return NULL; /* nothing to be done between head and tail */
895 desc_info = &info->desc_info[info->tail];
896 if (!rocker_desc_gen(desc_info))
897 return NULL; /* gen bit not set, desc is not ready yet */
898 info->tail = __pos_inc(info->tail, info->size);
899 desc_info->tlv_size = desc_info->desc->tlv_size;
903 static void rocker_dma_ring_credits_set(struct rocker *rocker,
904 struct rocker_dma_ring_info *info,
908 rocker_write32(rocker, DMA_DESC_CREDITS(info->type), credits);
911 static unsigned long rocker_dma_ring_size_fix(size_t size)
913 return max(ROCKER_DMA_SIZE_MIN,
914 min(roundup_pow_of_two(size), ROCKER_DMA_SIZE_MAX));
917 static int rocker_dma_ring_create(struct rocker *rocker,
920 struct rocker_dma_ring_info *info)
924 BUG_ON(size != rocker_dma_ring_size_fix(size));
929 info->desc_info = kcalloc(info->size, sizeof(*info->desc_info),
931 if (!info->desc_info)
934 info->desc = pci_alloc_consistent(rocker->pdev,
935 info->size * sizeof(*info->desc),
938 kfree(info->desc_info);
942 for (i = 0; i < info->size; i++)
943 info->desc_info[i].desc = &info->desc[i];
945 rocker_write32(rocker, DMA_DESC_CTRL(info->type),
946 ROCKER_DMA_DESC_CTRL_RESET);
947 rocker_write64(rocker, DMA_DESC_ADDR(info->type), info->mapaddr);
948 rocker_write32(rocker, DMA_DESC_SIZE(info->type), info->size);
953 static void rocker_dma_ring_destroy(struct rocker *rocker,
954 struct rocker_dma_ring_info *info)
956 rocker_write64(rocker, DMA_DESC_ADDR(info->type), 0);
958 pci_free_consistent(rocker->pdev,
959 info->size * sizeof(struct rocker_desc),
960 info->desc, info->mapaddr);
961 kfree(info->desc_info);
964 static void rocker_dma_ring_pass_to_producer(struct rocker *rocker,
965 struct rocker_dma_ring_info *info)
969 BUG_ON(info->head || info->tail);
971 /* When ring is consumer, we need to advance head for each desc.
972 * That tells hw that the desc is ready to be used by it.
974 for (i = 0; i < info->size - 1; i++)
975 rocker_desc_head_set(rocker, info, &info->desc_info[i]);
976 rocker_desc_commit(&info->desc_info[i]);
979 static int rocker_dma_ring_bufs_alloc(struct rocker *rocker,
980 struct rocker_dma_ring_info *info,
981 int direction, size_t buf_size)
983 struct pci_dev *pdev = rocker->pdev;
987 for (i = 0; i < info->size; i++) {
988 struct rocker_desc_info *desc_info = &info->desc_info[i];
989 struct rocker_desc *desc = &info->desc[i];
990 dma_addr_t dma_handle;
993 buf = kzalloc(buf_size, GFP_KERNEL | GFP_DMA);
999 dma_handle = pci_map_single(pdev, buf, buf_size, direction);
1000 if (pci_dma_mapping_error(pdev, dma_handle)) {
1006 desc_info->data = buf;
1007 desc_info->data_size = buf_size;
1008 dma_unmap_addr_set(desc_info, mapaddr, dma_handle);
1010 desc->buf_addr = dma_handle;
1011 desc->buf_size = buf_size;
1016 for (i--; i >= 0; i--) {
1017 struct rocker_desc_info *desc_info = &info->desc_info[i];
1019 pci_unmap_single(pdev, dma_unmap_addr(desc_info, mapaddr),
1020 desc_info->data_size, direction);
1021 kfree(desc_info->data);
1026 static void rocker_dma_ring_bufs_free(struct rocker *rocker,
1027 struct rocker_dma_ring_info *info,
1030 struct pci_dev *pdev = rocker->pdev;
1033 for (i = 0; i < info->size; i++) {
1034 struct rocker_desc_info *desc_info = &info->desc_info[i];
1035 struct rocker_desc *desc = &info->desc[i];
1039 pci_unmap_single(pdev, dma_unmap_addr(desc_info, mapaddr),
1040 desc_info->data_size, direction);
1041 kfree(desc_info->data);
1045 static int rocker_dma_rings_init(struct rocker *rocker)
1047 struct pci_dev *pdev = rocker->pdev;
1050 err = rocker_dma_ring_create(rocker, ROCKER_DMA_CMD,
1051 ROCKER_DMA_CMD_DEFAULT_SIZE,
1054 dev_err(&pdev->dev, "failed to create command dma ring\n");
1058 spin_lock_init(&rocker->cmd_ring_lock);
1060 err = rocker_dma_ring_bufs_alloc(rocker, &rocker->cmd_ring,
1061 PCI_DMA_BIDIRECTIONAL, PAGE_SIZE);
1063 dev_err(&pdev->dev, "failed to alloc command dma ring buffers\n");
1064 goto err_dma_cmd_ring_bufs_alloc;
1067 err = rocker_dma_ring_create(rocker, ROCKER_DMA_EVENT,
1068 ROCKER_DMA_EVENT_DEFAULT_SIZE,
1069 &rocker->event_ring);
1071 dev_err(&pdev->dev, "failed to create event dma ring\n");
1072 goto err_dma_event_ring_create;
1075 err = rocker_dma_ring_bufs_alloc(rocker, &rocker->event_ring,
1076 PCI_DMA_FROMDEVICE, PAGE_SIZE);
1078 dev_err(&pdev->dev, "failed to alloc event dma ring buffers\n");
1079 goto err_dma_event_ring_bufs_alloc;
1081 rocker_dma_ring_pass_to_producer(rocker, &rocker->event_ring);
1084 err_dma_event_ring_bufs_alloc:
1085 rocker_dma_ring_destroy(rocker, &rocker->event_ring);
1086 err_dma_event_ring_create:
1087 rocker_dma_ring_bufs_free(rocker, &rocker->cmd_ring,
1088 PCI_DMA_BIDIRECTIONAL);
1089 err_dma_cmd_ring_bufs_alloc:
1090 rocker_dma_ring_destroy(rocker, &rocker->cmd_ring);
1094 static void rocker_dma_rings_fini(struct rocker *rocker)
1096 rocker_dma_ring_bufs_free(rocker, &rocker->event_ring,
1097 PCI_DMA_BIDIRECTIONAL);
1098 rocker_dma_ring_destroy(rocker, &rocker->event_ring);
1099 rocker_dma_ring_bufs_free(rocker, &rocker->cmd_ring,
1100 PCI_DMA_BIDIRECTIONAL);
1101 rocker_dma_ring_destroy(rocker, &rocker->cmd_ring);
1104 static int rocker_dma_rx_ring_skb_map(struct rocker *rocker,
1105 struct rocker_port *rocker_port,
1106 struct rocker_desc_info *desc_info,
1107 struct sk_buff *skb, size_t buf_len)
1109 struct pci_dev *pdev = rocker->pdev;
1110 dma_addr_t dma_handle;
1112 dma_handle = pci_map_single(pdev, skb->data, buf_len,
1113 PCI_DMA_FROMDEVICE);
1114 if (pci_dma_mapping_error(pdev, dma_handle))
1116 if (rocker_tlv_put_u64(desc_info, ROCKER_TLV_RX_FRAG_ADDR, dma_handle))
1117 goto tlv_put_failure;
1118 if (rocker_tlv_put_u16(desc_info, ROCKER_TLV_RX_FRAG_MAX_LEN, buf_len))
1119 goto tlv_put_failure;
1123 pci_unmap_single(pdev, dma_handle, buf_len, PCI_DMA_FROMDEVICE);
1124 desc_info->tlv_size = 0;
1128 static size_t rocker_port_rx_buf_len(struct rocker_port *rocker_port)
1130 return rocker_port->dev->mtu + ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN;
1133 static int rocker_dma_rx_ring_skb_alloc(struct rocker *rocker,
1134 struct rocker_port *rocker_port,
1135 struct rocker_desc_info *desc_info)
1137 struct net_device *dev = rocker_port->dev;
1138 struct sk_buff *skb;
1139 size_t buf_len = rocker_port_rx_buf_len(rocker_port);
1142 /* Ensure that hw will see tlv_size zero in case of an error.
1143 * That tells hw to use another descriptor.
1145 rocker_desc_cookie_ptr_set(desc_info, NULL);
1146 desc_info->tlv_size = 0;
1148 skb = netdev_alloc_skb_ip_align(dev, buf_len);
1151 err = rocker_dma_rx_ring_skb_map(rocker, rocker_port, desc_info,
1154 dev_kfree_skb_any(skb);
1157 rocker_desc_cookie_ptr_set(desc_info, skb);
1161 static void rocker_dma_rx_ring_skb_unmap(struct rocker *rocker,
1162 struct rocker_tlv **attrs)
1164 struct pci_dev *pdev = rocker->pdev;
1165 dma_addr_t dma_handle;
1168 if (!attrs[ROCKER_TLV_RX_FRAG_ADDR] ||
1169 !attrs[ROCKER_TLV_RX_FRAG_MAX_LEN])
1171 dma_handle = rocker_tlv_get_u64(attrs[ROCKER_TLV_RX_FRAG_ADDR]);
1172 len = rocker_tlv_get_u16(attrs[ROCKER_TLV_RX_FRAG_MAX_LEN]);
1173 pci_unmap_single(pdev, dma_handle, len, PCI_DMA_FROMDEVICE);
1176 static void rocker_dma_rx_ring_skb_free(struct rocker *rocker,
1177 struct rocker_desc_info *desc_info)
1179 struct rocker_tlv *attrs[ROCKER_TLV_RX_MAX + 1];
1180 struct sk_buff *skb = rocker_desc_cookie_ptr_get(desc_info);
1184 rocker_tlv_parse_desc(attrs, ROCKER_TLV_RX_MAX, desc_info);
1185 rocker_dma_rx_ring_skb_unmap(rocker, attrs);
1186 dev_kfree_skb_any(skb);
1189 static int rocker_dma_rx_ring_skbs_alloc(struct rocker *rocker,
1190 struct rocker_port *rocker_port)
1192 struct rocker_dma_ring_info *rx_ring = &rocker_port->rx_ring;
1196 for (i = 0; i < rx_ring->size; i++) {
1197 err = rocker_dma_rx_ring_skb_alloc(rocker, rocker_port,
1198 &rx_ring->desc_info[i]);
1205 for (i--; i >= 0; i--)
1206 rocker_dma_rx_ring_skb_free(rocker, &rx_ring->desc_info[i]);
1210 static void rocker_dma_rx_ring_skbs_free(struct rocker *rocker,
1211 struct rocker_port *rocker_port)
1213 struct rocker_dma_ring_info *rx_ring = &rocker_port->rx_ring;
1216 for (i = 0; i < rx_ring->size; i++)
1217 rocker_dma_rx_ring_skb_free(rocker, &rx_ring->desc_info[i]);
1220 static int rocker_port_dma_rings_init(struct rocker_port *rocker_port)
1222 struct rocker *rocker = rocker_port->rocker;
1225 err = rocker_dma_ring_create(rocker,
1226 ROCKER_DMA_TX(rocker_port->port_number),
1227 ROCKER_DMA_TX_DEFAULT_SIZE,
1228 &rocker_port->tx_ring);
1230 netdev_err(rocker_port->dev, "failed to create tx dma ring\n");
1234 err = rocker_dma_ring_bufs_alloc(rocker, &rocker_port->tx_ring,
1236 ROCKER_DMA_TX_DESC_SIZE);
1238 netdev_err(rocker_port->dev, "failed to alloc tx dma ring buffers\n");
1239 goto err_dma_tx_ring_bufs_alloc;
1242 err = rocker_dma_ring_create(rocker,
1243 ROCKER_DMA_RX(rocker_port->port_number),
1244 ROCKER_DMA_RX_DEFAULT_SIZE,
1245 &rocker_port->rx_ring);
1247 netdev_err(rocker_port->dev, "failed to create rx dma ring\n");
1248 goto err_dma_rx_ring_create;
1251 err = rocker_dma_ring_bufs_alloc(rocker, &rocker_port->rx_ring,
1252 PCI_DMA_BIDIRECTIONAL,
1253 ROCKER_DMA_RX_DESC_SIZE);
1255 netdev_err(rocker_port->dev, "failed to alloc rx dma ring buffers\n");
1256 goto err_dma_rx_ring_bufs_alloc;
1259 err = rocker_dma_rx_ring_skbs_alloc(rocker, rocker_port);
1261 netdev_err(rocker_port->dev, "failed to alloc rx dma ring skbs\n");
1262 goto err_dma_rx_ring_skbs_alloc;
1264 rocker_dma_ring_pass_to_producer(rocker, &rocker_port->rx_ring);
1268 err_dma_rx_ring_skbs_alloc:
1269 rocker_dma_ring_bufs_free(rocker, &rocker_port->rx_ring,
1270 PCI_DMA_BIDIRECTIONAL);
1271 err_dma_rx_ring_bufs_alloc:
1272 rocker_dma_ring_destroy(rocker, &rocker_port->rx_ring);
1273 err_dma_rx_ring_create:
1274 rocker_dma_ring_bufs_free(rocker, &rocker_port->tx_ring,
1276 err_dma_tx_ring_bufs_alloc:
1277 rocker_dma_ring_destroy(rocker, &rocker_port->tx_ring);
1281 static void rocker_port_dma_rings_fini(struct rocker_port *rocker_port)
1283 struct rocker *rocker = rocker_port->rocker;
1285 rocker_dma_rx_ring_skbs_free(rocker, rocker_port);
1286 rocker_dma_ring_bufs_free(rocker, &rocker_port->rx_ring,
1287 PCI_DMA_BIDIRECTIONAL);
1288 rocker_dma_ring_destroy(rocker, &rocker_port->rx_ring);
1289 rocker_dma_ring_bufs_free(rocker, &rocker_port->tx_ring,
1291 rocker_dma_ring_destroy(rocker, &rocker_port->tx_ring);
1294 static void rocker_port_set_enable(struct rocker_port *rocker_port, bool enable)
1296 u64 val = rocker_read64(rocker_port->rocker, PORT_PHYS_ENABLE);
1299 val |= 1ULL << rocker_port->pport;
1301 val &= ~(1ULL << rocker_port->pport);
1302 rocker_write64(rocker_port->rocker, PORT_PHYS_ENABLE, val);
1305 /********************************
1306 * Interrupt handler and helpers
1307 ********************************/
1309 static irqreturn_t rocker_cmd_irq_handler(int irq, void *dev_id)
1311 struct rocker *rocker = dev_id;
1312 struct rocker_desc_info *desc_info;
1313 struct rocker_wait *wait;
1316 spin_lock(&rocker->cmd_ring_lock);
1317 while ((desc_info = rocker_desc_tail_get(&rocker->cmd_ring))) {
1318 wait = rocker_desc_cookie_ptr_get(desc_info);
1320 rocker_desc_gen_clear(desc_info);
1321 rocker_wait_destroy(wait);
1323 rocker_wait_wake_up(wait);
1327 spin_unlock(&rocker->cmd_ring_lock);
1328 rocker_dma_ring_credits_set(rocker, &rocker->cmd_ring, credits);
1333 static void rocker_port_link_up(struct rocker_port *rocker_port)
1335 netif_carrier_on(rocker_port->dev);
1336 netdev_info(rocker_port->dev, "Link is up\n");
1339 static void rocker_port_link_down(struct rocker_port *rocker_port)
1341 netif_carrier_off(rocker_port->dev);
1342 netdev_info(rocker_port->dev, "Link is down\n");
1345 static int rocker_event_link_change(struct rocker *rocker,
1346 const struct rocker_tlv *info)
1348 struct rocker_tlv *attrs[ROCKER_TLV_EVENT_LINK_CHANGED_MAX + 1];
1349 unsigned int port_number;
1351 struct rocker_port *rocker_port;
1353 rocker_tlv_parse_nested(attrs, ROCKER_TLV_EVENT_LINK_CHANGED_MAX, info);
1354 if (!attrs[ROCKER_TLV_EVENT_LINK_CHANGED_PPORT] ||
1355 !attrs[ROCKER_TLV_EVENT_LINK_CHANGED_LINKUP])
1358 rocker_tlv_get_u32(attrs[ROCKER_TLV_EVENT_LINK_CHANGED_PPORT]) - 1;
1359 link_up = rocker_tlv_get_u8(attrs[ROCKER_TLV_EVENT_LINK_CHANGED_LINKUP]);
1361 if (port_number >= rocker->port_count)
1364 rocker_port = rocker->ports[port_number];
1365 if (netif_carrier_ok(rocker_port->dev) != link_up) {
1367 rocker_port_link_up(rocker_port);
1369 rocker_port_link_down(rocker_port);
1375 #define ROCKER_OP_FLAG_REMOVE BIT(0)
1376 #define ROCKER_OP_FLAG_NOWAIT BIT(1)
1377 #define ROCKER_OP_FLAG_LEARNED BIT(2)
1378 #define ROCKER_OP_FLAG_REFRESH BIT(3)
1380 static int rocker_port_fdb(struct rocker_port *rocker_port,
1381 const unsigned char *addr,
1382 __be16 vlan_id, int flags);
1384 static int rocker_event_mac_vlan_seen(struct rocker *rocker,
1385 const struct rocker_tlv *info)
1387 struct rocker_tlv *attrs[ROCKER_TLV_EVENT_MAC_VLAN_MAX + 1];
1388 unsigned int port_number;
1389 struct rocker_port *rocker_port;
1390 unsigned char *addr;
1391 int flags = ROCKER_OP_FLAG_NOWAIT | ROCKER_OP_FLAG_LEARNED;
1394 rocker_tlv_parse_nested(attrs, ROCKER_TLV_EVENT_MAC_VLAN_MAX, info);
1395 if (!attrs[ROCKER_TLV_EVENT_MAC_VLAN_PPORT] ||
1396 !attrs[ROCKER_TLV_EVENT_MAC_VLAN_MAC] ||
1397 !attrs[ROCKER_TLV_EVENT_MAC_VLAN_VLAN_ID])
1400 rocker_tlv_get_u32(attrs[ROCKER_TLV_EVENT_MAC_VLAN_PPORT]) - 1;
1401 addr = rocker_tlv_data(attrs[ROCKER_TLV_EVENT_MAC_VLAN_MAC]);
1402 vlan_id = rocker_tlv_get_be16(attrs[ROCKER_TLV_EVENT_MAC_VLAN_VLAN_ID]);
1404 if (port_number >= rocker->port_count)
1407 rocker_port = rocker->ports[port_number];
1409 if (rocker_port->stp_state != BR_STATE_LEARNING &&
1410 rocker_port->stp_state != BR_STATE_FORWARDING)
1413 return rocker_port_fdb(rocker_port, addr, vlan_id, flags);
1416 static int rocker_event_process(struct rocker *rocker,
1417 struct rocker_desc_info *desc_info)
1419 struct rocker_tlv *attrs[ROCKER_TLV_EVENT_MAX + 1];
1420 struct rocker_tlv *info;
1423 rocker_tlv_parse_desc(attrs, ROCKER_TLV_EVENT_MAX, desc_info);
1424 if (!attrs[ROCKER_TLV_EVENT_TYPE] ||
1425 !attrs[ROCKER_TLV_EVENT_INFO])
1428 type = rocker_tlv_get_u16(attrs[ROCKER_TLV_EVENT_TYPE]);
1429 info = attrs[ROCKER_TLV_EVENT_INFO];
1432 case ROCKER_TLV_EVENT_TYPE_LINK_CHANGED:
1433 return rocker_event_link_change(rocker, info);
1434 case ROCKER_TLV_EVENT_TYPE_MAC_VLAN_SEEN:
1435 return rocker_event_mac_vlan_seen(rocker, info);
1441 static irqreturn_t rocker_event_irq_handler(int irq, void *dev_id)
1443 struct rocker *rocker = dev_id;
1444 struct pci_dev *pdev = rocker->pdev;
1445 struct rocker_desc_info *desc_info;
1449 while ((desc_info = rocker_desc_tail_get(&rocker->event_ring))) {
1450 err = rocker_desc_err(desc_info);
1452 dev_err(&pdev->dev, "event desc received with err %d\n",
1455 err = rocker_event_process(rocker, desc_info);
1457 dev_err(&pdev->dev, "event processing failed with err %d\n",
1460 rocker_desc_gen_clear(desc_info);
1461 rocker_desc_head_set(rocker, &rocker->event_ring, desc_info);
1464 rocker_dma_ring_credits_set(rocker, &rocker->event_ring, credits);
1469 static irqreturn_t rocker_tx_irq_handler(int irq, void *dev_id)
1471 struct rocker_port *rocker_port = dev_id;
1473 napi_schedule(&rocker_port->napi_tx);
1477 static irqreturn_t rocker_rx_irq_handler(int irq, void *dev_id)
1479 struct rocker_port *rocker_port = dev_id;
1481 napi_schedule(&rocker_port->napi_rx);
1485 /********************
1487 ********************/
1489 typedef int (*rocker_cmd_cb_t)(struct rocker *rocker,
1490 struct rocker_port *rocker_port,
1491 struct rocker_desc_info *desc_info,
1494 static int rocker_cmd_exec(struct rocker *rocker,
1495 struct rocker_port *rocker_port,
1496 rocker_cmd_cb_t prepare, void *prepare_priv,
1497 rocker_cmd_cb_t process, void *process_priv,
1500 struct rocker_desc_info *desc_info;
1501 struct rocker_wait *wait;
1502 unsigned long flags;
1505 wait = rocker_wait_create(nowait ? GFP_ATOMIC : GFP_KERNEL);
1508 wait->nowait = nowait;
1510 spin_lock_irqsave(&rocker->cmd_ring_lock, flags);
1511 desc_info = rocker_desc_head_get(&rocker->cmd_ring);
1513 spin_unlock_irqrestore(&rocker->cmd_ring_lock, flags);
1517 err = prepare(rocker, rocker_port, desc_info, prepare_priv);
1519 spin_unlock_irqrestore(&rocker->cmd_ring_lock, flags);
1522 rocker_desc_cookie_ptr_set(desc_info, wait);
1523 rocker_desc_head_set(rocker, &rocker->cmd_ring, desc_info);
1524 spin_unlock_irqrestore(&rocker->cmd_ring_lock, flags);
1529 if (!rocker_wait_event_timeout(wait, HZ / 10))
1532 err = rocker_desc_err(desc_info);
1537 err = process(rocker, rocker_port, desc_info, process_priv);
1539 rocker_desc_gen_clear(desc_info);
1541 rocker_wait_destroy(wait);
1546 rocker_cmd_get_port_settings_prep(struct rocker *rocker,
1547 struct rocker_port *rocker_port,
1548 struct rocker_desc_info *desc_info,
1551 struct rocker_tlv *cmd_info;
1553 if (rocker_tlv_put_u16(desc_info, ROCKER_TLV_CMD_TYPE,
1554 ROCKER_TLV_CMD_TYPE_GET_PORT_SETTINGS))
1556 cmd_info = rocker_tlv_nest_start(desc_info, ROCKER_TLV_CMD_INFO);
1559 if (rocker_tlv_put_u32(desc_info, ROCKER_TLV_CMD_PORT_SETTINGS_PPORT,
1560 rocker_port->pport))
1562 rocker_tlv_nest_end(desc_info, cmd_info);
1567 rocker_cmd_get_port_settings_ethtool_proc(struct rocker *rocker,
1568 struct rocker_port *rocker_port,
1569 struct rocker_desc_info *desc_info,
1572 struct ethtool_cmd *ecmd = priv;
1573 struct rocker_tlv *attrs[ROCKER_TLV_CMD_MAX + 1];
1574 struct rocker_tlv *info_attrs[ROCKER_TLV_CMD_PORT_SETTINGS_MAX + 1];
1579 rocker_tlv_parse_desc(attrs, ROCKER_TLV_CMD_MAX, desc_info);
1580 if (!attrs[ROCKER_TLV_CMD_INFO])
1583 rocker_tlv_parse_nested(info_attrs, ROCKER_TLV_CMD_PORT_SETTINGS_MAX,
1584 attrs[ROCKER_TLV_CMD_INFO]);
1585 if (!info_attrs[ROCKER_TLV_CMD_PORT_SETTINGS_SPEED] ||
1586 !info_attrs[ROCKER_TLV_CMD_PORT_SETTINGS_DUPLEX] ||
1587 !info_attrs[ROCKER_TLV_CMD_PORT_SETTINGS_AUTONEG])
1590 speed = rocker_tlv_get_u32(info_attrs[ROCKER_TLV_CMD_PORT_SETTINGS_SPEED]);
1591 duplex = rocker_tlv_get_u8(info_attrs[ROCKER_TLV_CMD_PORT_SETTINGS_DUPLEX]);
1592 autoneg = rocker_tlv_get_u8(info_attrs[ROCKER_TLV_CMD_PORT_SETTINGS_AUTONEG]);
1594 ecmd->transceiver = XCVR_INTERNAL;
1595 ecmd->supported = SUPPORTED_TP;
1596 ecmd->phy_address = 0xff;
1597 ecmd->port = PORT_TP;
1598 ethtool_cmd_speed_set(ecmd, speed);
1599 ecmd->duplex = duplex ? DUPLEX_FULL : DUPLEX_HALF;
1600 ecmd->autoneg = autoneg ? AUTONEG_ENABLE : AUTONEG_DISABLE;
1606 rocker_cmd_get_port_settings_macaddr_proc(struct rocker *rocker,
1607 struct rocker_port *rocker_port,
1608 struct rocker_desc_info *desc_info,
1611 unsigned char *macaddr = priv;
1612 struct rocker_tlv *attrs[ROCKER_TLV_CMD_MAX + 1];
1613 struct rocker_tlv *info_attrs[ROCKER_TLV_CMD_PORT_SETTINGS_MAX + 1];
1614 struct rocker_tlv *attr;
1616 rocker_tlv_parse_desc(attrs, ROCKER_TLV_CMD_MAX, desc_info);
1617 if (!attrs[ROCKER_TLV_CMD_INFO])
1620 rocker_tlv_parse_nested(info_attrs, ROCKER_TLV_CMD_PORT_SETTINGS_MAX,
1621 attrs[ROCKER_TLV_CMD_INFO]);
1622 attr = info_attrs[ROCKER_TLV_CMD_PORT_SETTINGS_MACADDR];
1626 if (rocker_tlv_len(attr) != ETH_ALEN)
1629 ether_addr_copy(macaddr, rocker_tlv_data(attr));
1634 rocker_cmd_set_port_settings_ethtool_prep(struct rocker *rocker,
1635 struct rocker_port *rocker_port,
1636 struct rocker_desc_info *desc_info,
1639 struct ethtool_cmd *ecmd = priv;
1640 struct rocker_tlv *cmd_info;
1642 if (rocker_tlv_put_u16(desc_info, ROCKER_TLV_CMD_TYPE,
1643 ROCKER_TLV_CMD_TYPE_SET_PORT_SETTINGS))
1645 cmd_info = rocker_tlv_nest_start(desc_info, ROCKER_TLV_CMD_INFO);
1648 if (rocker_tlv_put_u32(desc_info, ROCKER_TLV_CMD_PORT_SETTINGS_PPORT,
1649 rocker_port->pport))
1651 if (rocker_tlv_put_u32(desc_info, ROCKER_TLV_CMD_PORT_SETTINGS_SPEED,
1652 ethtool_cmd_speed(ecmd)))
1654 if (rocker_tlv_put_u8(desc_info, ROCKER_TLV_CMD_PORT_SETTINGS_DUPLEX,
1657 if (rocker_tlv_put_u8(desc_info, ROCKER_TLV_CMD_PORT_SETTINGS_AUTONEG,
1660 rocker_tlv_nest_end(desc_info, cmd_info);
1665 rocker_cmd_set_port_settings_macaddr_prep(struct rocker *rocker,
1666 struct rocker_port *rocker_port,
1667 struct rocker_desc_info *desc_info,
1670 unsigned char *macaddr = priv;
1671 struct rocker_tlv *cmd_info;
1673 if (rocker_tlv_put_u16(desc_info, ROCKER_TLV_CMD_TYPE,
1674 ROCKER_TLV_CMD_TYPE_SET_PORT_SETTINGS))
1676 cmd_info = rocker_tlv_nest_start(desc_info, ROCKER_TLV_CMD_INFO);
1679 if (rocker_tlv_put_u32(desc_info, ROCKER_TLV_CMD_PORT_SETTINGS_PPORT,
1680 rocker_port->pport))
1682 if (rocker_tlv_put(desc_info, ROCKER_TLV_CMD_PORT_SETTINGS_MACADDR,
1685 rocker_tlv_nest_end(desc_info, cmd_info);
1690 rocker_cmd_set_port_learning_prep(struct rocker *rocker,
1691 struct rocker_port *rocker_port,
1692 struct rocker_desc_info *desc_info,
1695 struct rocker_tlv *cmd_info;
1697 if (rocker_tlv_put_u16(desc_info, ROCKER_TLV_CMD_TYPE,
1698 ROCKER_TLV_CMD_TYPE_SET_PORT_SETTINGS))
1700 cmd_info = rocker_tlv_nest_start(desc_info, ROCKER_TLV_CMD_INFO);
1703 if (rocker_tlv_put_u32(desc_info, ROCKER_TLV_CMD_PORT_SETTINGS_PPORT,
1704 rocker_port->pport))
1706 if (rocker_tlv_put_u8(desc_info, ROCKER_TLV_CMD_PORT_SETTINGS_LEARNING,
1707 !!(rocker_port->brport_flags & BR_LEARNING)))
1709 rocker_tlv_nest_end(desc_info, cmd_info);
1713 static int rocker_cmd_get_port_settings_ethtool(struct rocker_port *rocker_port,
1714 struct ethtool_cmd *ecmd)
1716 return rocker_cmd_exec(rocker_port->rocker, rocker_port,
1717 rocker_cmd_get_port_settings_prep, NULL,
1718 rocker_cmd_get_port_settings_ethtool_proc,
1722 static int rocker_cmd_get_port_settings_macaddr(struct rocker_port *rocker_port,
1723 unsigned char *macaddr)
1725 return rocker_cmd_exec(rocker_port->rocker, rocker_port,
1726 rocker_cmd_get_port_settings_prep, NULL,
1727 rocker_cmd_get_port_settings_macaddr_proc,
1731 static int rocker_cmd_set_port_settings_ethtool(struct rocker_port *rocker_port,
1732 struct ethtool_cmd *ecmd)
1734 return rocker_cmd_exec(rocker_port->rocker, rocker_port,
1735 rocker_cmd_set_port_settings_ethtool_prep,
1736 ecmd, NULL, NULL, false);
1739 static int rocker_cmd_set_port_settings_macaddr(struct rocker_port *rocker_port,
1740 unsigned char *macaddr)
1742 return rocker_cmd_exec(rocker_port->rocker, rocker_port,
1743 rocker_cmd_set_port_settings_macaddr_prep,
1744 macaddr, NULL, NULL, false);
1747 static int rocker_port_set_learning(struct rocker_port *rocker_port)
1749 return rocker_cmd_exec(rocker_port->rocker, rocker_port,
1750 rocker_cmd_set_port_learning_prep,
1751 NULL, NULL, NULL, false);
1754 static int rocker_cmd_flow_tbl_add_ig_port(struct rocker_desc_info *desc_info,
1755 struct rocker_flow_tbl_entry *entry)
1757 if (rocker_tlv_put_u32(desc_info, ROCKER_TLV_OF_DPA_IN_PPORT,
1758 entry->key.ig_port.in_pport))
1760 if (rocker_tlv_put_u32(desc_info, ROCKER_TLV_OF_DPA_IN_PPORT_MASK,
1761 entry->key.ig_port.in_pport_mask))
1763 if (rocker_tlv_put_u16(desc_info, ROCKER_TLV_OF_DPA_GOTO_TABLE_ID,
1764 entry->key.ig_port.goto_tbl))
1770 static int rocker_cmd_flow_tbl_add_vlan(struct rocker_desc_info *desc_info,
1771 struct rocker_flow_tbl_entry *entry)
1773 if (rocker_tlv_put_u32(desc_info, ROCKER_TLV_OF_DPA_IN_PPORT,
1774 entry->key.vlan.in_pport))
1776 if (rocker_tlv_put_be16(desc_info, ROCKER_TLV_OF_DPA_VLAN_ID,
1777 entry->key.vlan.vlan_id))
1779 if (rocker_tlv_put_be16(desc_info, ROCKER_TLV_OF_DPA_VLAN_ID_MASK,
1780 entry->key.vlan.vlan_id_mask))
1782 if (rocker_tlv_put_u16(desc_info, ROCKER_TLV_OF_DPA_GOTO_TABLE_ID,
1783 entry->key.vlan.goto_tbl))
1785 if (entry->key.vlan.untagged &&
1786 rocker_tlv_put_be16(desc_info, ROCKER_TLV_OF_DPA_NEW_VLAN_ID,
1787 entry->key.vlan.new_vlan_id))
1793 static int rocker_cmd_flow_tbl_add_term_mac(struct rocker_desc_info *desc_info,
1794 struct rocker_flow_tbl_entry *entry)
1796 if (rocker_tlv_put_u32(desc_info, ROCKER_TLV_OF_DPA_IN_PPORT,
1797 entry->key.term_mac.in_pport))
1799 if (rocker_tlv_put_u32(desc_info, ROCKER_TLV_OF_DPA_IN_PPORT_MASK,
1800 entry->key.term_mac.in_pport_mask))
1802 if (rocker_tlv_put_be16(desc_info, ROCKER_TLV_OF_DPA_ETHERTYPE,
1803 entry->key.term_mac.eth_type))
1805 if (rocker_tlv_put(desc_info, ROCKER_TLV_OF_DPA_DST_MAC,
1806 ETH_ALEN, entry->key.term_mac.eth_dst))
1808 if (rocker_tlv_put(desc_info, ROCKER_TLV_OF_DPA_DST_MAC_MASK,
1809 ETH_ALEN, entry->key.term_mac.eth_dst_mask))
1811 if (rocker_tlv_put_be16(desc_info, ROCKER_TLV_OF_DPA_VLAN_ID,
1812 entry->key.term_mac.vlan_id))
1814 if (rocker_tlv_put_be16(desc_info, ROCKER_TLV_OF_DPA_VLAN_ID_MASK,
1815 entry->key.term_mac.vlan_id_mask))
1817 if (rocker_tlv_put_u16(desc_info, ROCKER_TLV_OF_DPA_GOTO_TABLE_ID,
1818 entry->key.term_mac.goto_tbl))
1820 if (entry->key.term_mac.copy_to_cpu &&
1821 rocker_tlv_put_u8(desc_info, ROCKER_TLV_OF_DPA_COPY_CPU_ACTION,
1822 entry->key.term_mac.copy_to_cpu))
1829 rocker_cmd_flow_tbl_add_ucast_routing(struct rocker_desc_info *desc_info,
1830 struct rocker_flow_tbl_entry *entry)
1832 if (rocker_tlv_put_be16(desc_info, ROCKER_TLV_OF_DPA_ETHERTYPE,
1833 entry->key.ucast_routing.eth_type))
1835 if (rocker_tlv_put_be32(desc_info, ROCKER_TLV_OF_DPA_DST_IP,
1836 entry->key.ucast_routing.dst4))
1838 if (rocker_tlv_put_be32(desc_info, ROCKER_TLV_OF_DPA_DST_IP_MASK,
1839 entry->key.ucast_routing.dst4_mask))
1841 if (rocker_tlv_put_u16(desc_info, ROCKER_TLV_OF_DPA_GOTO_TABLE_ID,
1842 entry->key.ucast_routing.goto_tbl))
1844 if (rocker_tlv_put_u32(desc_info, ROCKER_TLV_OF_DPA_GROUP_ID,
1845 entry->key.ucast_routing.group_id))
1851 static int rocker_cmd_flow_tbl_add_bridge(struct rocker_desc_info *desc_info,
1852 struct rocker_flow_tbl_entry *entry)
1854 if (entry->key.bridge.has_eth_dst &&
1855 rocker_tlv_put(desc_info, ROCKER_TLV_OF_DPA_DST_MAC,
1856 ETH_ALEN, entry->key.bridge.eth_dst))
1858 if (entry->key.bridge.has_eth_dst_mask &&
1859 rocker_tlv_put(desc_info, ROCKER_TLV_OF_DPA_DST_MAC_MASK,
1860 ETH_ALEN, entry->key.bridge.eth_dst_mask))
1862 if (entry->key.bridge.vlan_id &&
1863 rocker_tlv_put_be16(desc_info, ROCKER_TLV_OF_DPA_VLAN_ID,
1864 entry->key.bridge.vlan_id))
1866 if (entry->key.bridge.tunnel_id &&
1867 rocker_tlv_put_u32(desc_info, ROCKER_TLV_OF_DPA_TUNNEL_ID,
1868 entry->key.bridge.tunnel_id))
1870 if (rocker_tlv_put_u16(desc_info, ROCKER_TLV_OF_DPA_GOTO_TABLE_ID,
1871 entry->key.bridge.goto_tbl))
1873 if (rocker_tlv_put_u32(desc_info, ROCKER_TLV_OF_DPA_GROUP_ID,
1874 entry->key.bridge.group_id))
1876 if (entry->key.bridge.copy_to_cpu &&
1877 rocker_tlv_put_u8(desc_info, ROCKER_TLV_OF_DPA_COPY_CPU_ACTION,
1878 entry->key.bridge.copy_to_cpu))
1884 static int rocker_cmd_flow_tbl_add_acl(struct rocker_desc_info *desc_info,
1885 struct rocker_flow_tbl_entry *entry)
1887 if (rocker_tlv_put_u32(desc_info, ROCKER_TLV_OF_DPA_IN_PPORT,
1888 entry->key.acl.in_pport))
1890 if (rocker_tlv_put_u32(desc_info, ROCKER_TLV_OF_DPA_IN_PPORT_MASK,
1891 entry->key.acl.in_pport_mask))
1893 if (rocker_tlv_put(desc_info, ROCKER_TLV_OF_DPA_SRC_MAC,
1894 ETH_ALEN, entry->key.acl.eth_src))
1896 if (rocker_tlv_put(desc_info, ROCKER_TLV_OF_DPA_SRC_MAC_MASK,
1897 ETH_ALEN, entry->key.acl.eth_src_mask))
1899 if (rocker_tlv_put(desc_info, ROCKER_TLV_OF_DPA_DST_MAC,
1900 ETH_ALEN, entry->key.acl.eth_dst))
1902 if (rocker_tlv_put(desc_info, ROCKER_TLV_OF_DPA_DST_MAC_MASK,
1903 ETH_ALEN, entry->key.acl.eth_dst_mask))
1905 if (rocker_tlv_put_be16(desc_info, ROCKER_TLV_OF_DPA_ETHERTYPE,
1906 entry->key.acl.eth_type))
1908 if (rocker_tlv_put_be16(desc_info, ROCKER_TLV_OF_DPA_VLAN_ID,
1909 entry->key.acl.vlan_id))
1911 if (rocker_tlv_put_be16(desc_info, ROCKER_TLV_OF_DPA_VLAN_ID_MASK,
1912 entry->key.acl.vlan_id_mask))
1915 switch (ntohs(entry->key.acl.eth_type)) {
1918 if (rocker_tlv_put_u8(desc_info, ROCKER_TLV_OF_DPA_IP_PROTO,
1919 entry->key.acl.ip_proto))
1921 if (rocker_tlv_put_u8(desc_info,
1922 ROCKER_TLV_OF_DPA_IP_PROTO_MASK,
1923 entry->key.acl.ip_proto_mask))
1925 if (rocker_tlv_put_u8(desc_info, ROCKER_TLV_OF_DPA_IP_DSCP,
1926 entry->key.acl.ip_tos & 0x3f))
1928 if (rocker_tlv_put_u8(desc_info,
1929 ROCKER_TLV_OF_DPA_IP_DSCP_MASK,
1930 entry->key.acl.ip_tos_mask & 0x3f))
1932 if (rocker_tlv_put_u8(desc_info, ROCKER_TLV_OF_DPA_IP_ECN,
1933 (entry->key.acl.ip_tos & 0xc0) >> 6))
1935 if (rocker_tlv_put_u8(desc_info,
1936 ROCKER_TLV_OF_DPA_IP_ECN_MASK,
1937 (entry->key.acl.ip_tos_mask & 0xc0) >> 6))
1942 if (entry->key.acl.group_id != ROCKER_GROUP_NONE &&
1943 rocker_tlv_put_u32(desc_info, ROCKER_TLV_OF_DPA_GROUP_ID,
1944 entry->key.acl.group_id))
1950 static int rocker_cmd_flow_tbl_add(struct rocker *rocker,
1951 struct rocker_port *rocker_port,
1952 struct rocker_desc_info *desc_info,
1955 struct rocker_flow_tbl_entry *entry = priv;
1956 struct rocker_tlv *cmd_info;
1959 if (rocker_tlv_put_u16(desc_info, ROCKER_TLV_CMD_TYPE, entry->cmd))
1961 cmd_info = rocker_tlv_nest_start(desc_info, ROCKER_TLV_CMD_INFO);
1964 if (rocker_tlv_put_u16(desc_info, ROCKER_TLV_OF_DPA_TABLE_ID,
1967 if (rocker_tlv_put_u32(desc_info, ROCKER_TLV_OF_DPA_PRIORITY,
1968 entry->key.priority))
1970 if (rocker_tlv_put_u32(desc_info, ROCKER_TLV_OF_DPA_HARDTIME, 0))
1972 if (rocker_tlv_put_u64(desc_info, ROCKER_TLV_OF_DPA_COOKIE,
1976 switch (entry->key.tbl_id) {
1977 case ROCKER_OF_DPA_TABLE_ID_INGRESS_PORT:
1978 err = rocker_cmd_flow_tbl_add_ig_port(desc_info, entry);
1980 case ROCKER_OF_DPA_TABLE_ID_VLAN:
1981 err = rocker_cmd_flow_tbl_add_vlan(desc_info, entry);
1983 case ROCKER_OF_DPA_TABLE_ID_TERMINATION_MAC:
1984 err = rocker_cmd_flow_tbl_add_term_mac(desc_info, entry);
1986 case ROCKER_OF_DPA_TABLE_ID_UNICAST_ROUTING:
1987 err = rocker_cmd_flow_tbl_add_ucast_routing(desc_info, entry);
1989 case ROCKER_OF_DPA_TABLE_ID_BRIDGING:
1990 err = rocker_cmd_flow_tbl_add_bridge(desc_info, entry);
1992 case ROCKER_OF_DPA_TABLE_ID_ACL_POLICY:
1993 err = rocker_cmd_flow_tbl_add_acl(desc_info, entry);
2003 rocker_tlv_nest_end(desc_info, cmd_info);
2008 static int rocker_cmd_flow_tbl_del(struct rocker *rocker,
2009 struct rocker_port *rocker_port,
2010 struct rocker_desc_info *desc_info,
2013 const struct rocker_flow_tbl_entry *entry = priv;
2014 struct rocker_tlv *cmd_info;
2016 if (rocker_tlv_put_u16(desc_info, ROCKER_TLV_CMD_TYPE, entry->cmd))
2018 cmd_info = rocker_tlv_nest_start(desc_info, ROCKER_TLV_CMD_INFO);
2021 if (rocker_tlv_put_u64(desc_info, ROCKER_TLV_OF_DPA_COOKIE,
2024 rocker_tlv_nest_end(desc_info, cmd_info);
2030 rocker_cmd_group_tbl_add_l2_interface(struct rocker_desc_info *desc_info,
2031 struct rocker_group_tbl_entry *entry)
2033 if (rocker_tlv_put_u32(desc_info, ROCKER_TLV_OF_DPA_OUT_PPORT,
2034 ROCKER_GROUP_PORT_GET(entry->group_id)))
2036 if (rocker_tlv_put_u8(desc_info, ROCKER_TLV_OF_DPA_POP_VLAN,
2037 entry->l2_interface.pop_vlan))
2044 rocker_cmd_group_tbl_add_l2_rewrite(struct rocker_desc_info *desc_info,
2045 struct rocker_group_tbl_entry *entry)
2047 if (rocker_tlv_put_u32(desc_info, ROCKER_TLV_OF_DPA_GROUP_ID_LOWER,
2048 entry->l2_rewrite.group_id))
2050 if (!is_zero_ether_addr(entry->l2_rewrite.eth_src) &&
2051 rocker_tlv_put(desc_info, ROCKER_TLV_OF_DPA_SRC_MAC,
2052 ETH_ALEN, entry->l2_rewrite.eth_src))
2054 if (!is_zero_ether_addr(entry->l2_rewrite.eth_dst) &&
2055 rocker_tlv_put(desc_info, ROCKER_TLV_OF_DPA_DST_MAC,
2056 ETH_ALEN, entry->l2_rewrite.eth_dst))
2058 if (entry->l2_rewrite.vlan_id &&
2059 rocker_tlv_put_be16(desc_info, ROCKER_TLV_OF_DPA_VLAN_ID,
2060 entry->l2_rewrite.vlan_id))
2067 rocker_cmd_group_tbl_add_group_ids(struct rocker_desc_info *desc_info,
2068 struct rocker_group_tbl_entry *entry)
2071 struct rocker_tlv *group_ids;
2073 if (rocker_tlv_put_u16(desc_info, ROCKER_TLV_OF_DPA_GROUP_COUNT,
2074 entry->group_count))
2077 group_ids = rocker_tlv_nest_start(desc_info,
2078 ROCKER_TLV_OF_DPA_GROUP_IDS);
2082 for (i = 0; i < entry->group_count; i++)
2083 /* Note TLV array is 1-based */
2084 if (rocker_tlv_put_u32(desc_info, i + 1, entry->group_ids[i]))
2087 rocker_tlv_nest_end(desc_info, group_ids);
2093 rocker_cmd_group_tbl_add_l3_unicast(struct rocker_desc_info *desc_info,
2094 struct rocker_group_tbl_entry *entry)
2096 if (!is_zero_ether_addr(entry->l3_unicast.eth_src) &&
2097 rocker_tlv_put(desc_info, ROCKER_TLV_OF_DPA_SRC_MAC,
2098 ETH_ALEN, entry->l3_unicast.eth_src))
2100 if (!is_zero_ether_addr(entry->l3_unicast.eth_dst) &&
2101 rocker_tlv_put(desc_info, ROCKER_TLV_OF_DPA_DST_MAC,
2102 ETH_ALEN, entry->l3_unicast.eth_dst))
2104 if (entry->l3_unicast.vlan_id &&
2105 rocker_tlv_put_be16(desc_info, ROCKER_TLV_OF_DPA_VLAN_ID,
2106 entry->l3_unicast.vlan_id))
2108 if (rocker_tlv_put_u8(desc_info, ROCKER_TLV_OF_DPA_TTL_CHECK,
2109 entry->l3_unicast.ttl_check))
2111 if (rocker_tlv_put_u32(desc_info, ROCKER_TLV_OF_DPA_GROUP_ID_LOWER,
2112 entry->l3_unicast.group_id))
2118 static int rocker_cmd_group_tbl_add(struct rocker *rocker,
2119 struct rocker_port *rocker_port,
2120 struct rocker_desc_info *desc_info,
2123 struct rocker_group_tbl_entry *entry = priv;
2124 struct rocker_tlv *cmd_info;
2127 if (rocker_tlv_put_u16(desc_info, ROCKER_TLV_CMD_TYPE, entry->cmd))
2129 cmd_info = rocker_tlv_nest_start(desc_info, ROCKER_TLV_CMD_INFO);
2133 if (rocker_tlv_put_u32(desc_info, ROCKER_TLV_OF_DPA_GROUP_ID,
2137 switch (ROCKER_GROUP_TYPE_GET(entry->group_id)) {
2138 case ROCKER_OF_DPA_GROUP_TYPE_L2_INTERFACE:
2139 err = rocker_cmd_group_tbl_add_l2_interface(desc_info, entry);
2141 case ROCKER_OF_DPA_GROUP_TYPE_L2_REWRITE:
2142 err = rocker_cmd_group_tbl_add_l2_rewrite(desc_info, entry);
2144 case ROCKER_OF_DPA_GROUP_TYPE_L2_FLOOD:
2145 case ROCKER_OF_DPA_GROUP_TYPE_L2_MCAST:
2146 err = rocker_cmd_group_tbl_add_group_ids(desc_info, entry);
2148 case ROCKER_OF_DPA_GROUP_TYPE_L3_UCAST:
2149 err = rocker_cmd_group_tbl_add_l3_unicast(desc_info, entry);
2159 rocker_tlv_nest_end(desc_info, cmd_info);
2164 static int rocker_cmd_group_tbl_del(struct rocker *rocker,
2165 struct rocker_port *rocker_port,
2166 struct rocker_desc_info *desc_info,
2169 const struct rocker_group_tbl_entry *entry = priv;
2170 struct rocker_tlv *cmd_info;
2172 if (rocker_tlv_put_u16(desc_info, ROCKER_TLV_CMD_TYPE, entry->cmd))
2174 cmd_info = rocker_tlv_nest_start(desc_info, ROCKER_TLV_CMD_INFO);
2177 if (rocker_tlv_put_u32(desc_info, ROCKER_TLV_OF_DPA_GROUP_ID,
2180 rocker_tlv_nest_end(desc_info, cmd_info);
2185 /***************************************************
2186 * Flow, group, FDB, internal VLAN and neigh tables
2187 ***************************************************/
2189 static int rocker_init_tbls(struct rocker *rocker)
2191 hash_init(rocker->flow_tbl);
2192 spin_lock_init(&rocker->flow_tbl_lock);
2194 hash_init(rocker->group_tbl);
2195 spin_lock_init(&rocker->group_tbl_lock);
2197 hash_init(rocker->fdb_tbl);
2198 spin_lock_init(&rocker->fdb_tbl_lock);
2200 hash_init(rocker->internal_vlan_tbl);
2201 spin_lock_init(&rocker->internal_vlan_tbl_lock);
2203 hash_init(rocker->neigh_tbl);
2204 spin_lock_init(&rocker->neigh_tbl_lock);
2209 static void rocker_free_tbls(struct rocker *rocker)
2211 unsigned long flags;
2212 struct rocker_flow_tbl_entry *flow_entry;
2213 struct rocker_group_tbl_entry *group_entry;
2214 struct rocker_fdb_tbl_entry *fdb_entry;
2215 struct rocker_internal_vlan_tbl_entry *internal_vlan_entry;
2216 struct rocker_neigh_tbl_entry *neigh_entry;
2217 struct hlist_node *tmp;
2220 spin_lock_irqsave(&rocker->flow_tbl_lock, flags);
2221 hash_for_each_safe(rocker->flow_tbl, bkt, tmp, flow_entry, entry)
2222 hash_del(&flow_entry->entry);
2223 spin_unlock_irqrestore(&rocker->flow_tbl_lock, flags);
2225 spin_lock_irqsave(&rocker->group_tbl_lock, flags);
2226 hash_for_each_safe(rocker->group_tbl, bkt, tmp, group_entry, entry)
2227 hash_del(&group_entry->entry);
2228 spin_unlock_irqrestore(&rocker->group_tbl_lock, flags);
2230 spin_lock_irqsave(&rocker->fdb_tbl_lock, flags);
2231 hash_for_each_safe(rocker->fdb_tbl, bkt, tmp, fdb_entry, entry)
2232 hash_del(&fdb_entry->entry);
2233 spin_unlock_irqrestore(&rocker->fdb_tbl_lock, flags);
2235 spin_lock_irqsave(&rocker->internal_vlan_tbl_lock, flags);
2236 hash_for_each_safe(rocker->internal_vlan_tbl, bkt,
2237 tmp, internal_vlan_entry, entry)
2238 hash_del(&internal_vlan_entry->entry);
2239 spin_unlock_irqrestore(&rocker->internal_vlan_tbl_lock, flags);
2241 spin_lock_irqsave(&rocker->neigh_tbl_lock, flags);
2242 hash_for_each_safe(rocker->neigh_tbl, bkt, tmp, neigh_entry, entry)
2243 hash_del(&neigh_entry->entry);
2244 spin_unlock_irqrestore(&rocker->neigh_tbl_lock, flags);
2247 static struct rocker_flow_tbl_entry *
2248 rocker_flow_tbl_find(struct rocker *rocker, struct rocker_flow_tbl_entry *match)
2250 struct rocker_flow_tbl_entry *found;
2251 size_t key_len = match->key_len ? match->key_len : sizeof(found->key);
2253 hash_for_each_possible(rocker->flow_tbl, found,
2254 entry, match->key_crc32) {
2255 if (memcmp(&found->key, &match->key, key_len) == 0)
2262 static int rocker_flow_tbl_add(struct rocker_port *rocker_port,
2263 struct rocker_flow_tbl_entry *match,
2266 struct rocker *rocker = rocker_port->rocker;
2267 struct rocker_flow_tbl_entry *found;
2268 size_t key_len = match->key_len ? match->key_len : sizeof(found->key);
2269 unsigned long flags;
2271 match->key_crc32 = crc32(~0, &match->key, key_len);
2273 spin_lock_irqsave(&rocker->flow_tbl_lock, flags);
2275 found = rocker_flow_tbl_find(rocker, match);
2278 match->cookie = found->cookie;
2279 hash_del(&found->entry);
2282 found->cmd = ROCKER_TLV_CMD_TYPE_OF_DPA_FLOW_MOD;
2285 found->cookie = rocker->flow_tbl_next_cookie++;
2286 found->cmd = ROCKER_TLV_CMD_TYPE_OF_DPA_FLOW_ADD;
2289 hash_add(rocker->flow_tbl, &found->entry, found->key_crc32);
2291 spin_unlock_irqrestore(&rocker->flow_tbl_lock, flags);
2293 return rocker_cmd_exec(rocker, rocker_port,
2294 rocker_cmd_flow_tbl_add,
2295 found, NULL, NULL, nowait);
2298 static int rocker_flow_tbl_del(struct rocker_port *rocker_port,
2299 struct rocker_flow_tbl_entry *match,
2302 struct rocker *rocker = rocker_port->rocker;
2303 struct rocker_flow_tbl_entry *found;
2304 size_t key_len = match->key_len ? match->key_len : sizeof(found->key);
2305 unsigned long flags;
2308 match->key_crc32 = crc32(~0, &match->key, key_len);
2310 spin_lock_irqsave(&rocker->flow_tbl_lock, flags);
2312 found = rocker_flow_tbl_find(rocker, match);
2315 hash_del(&found->entry);
2316 found->cmd = ROCKER_TLV_CMD_TYPE_OF_DPA_FLOW_DEL;
2319 spin_unlock_irqrestore(&rocker->flow_tbl_lock, flags);
2324 err = rocker_cmd_exec(rocker, rocker_port,
2325 rocker_cmd_flow_tbl_del,
2326 found, NULL, NULL, nowait);
2333 static gfp_t rocker_op_flags_gfp(int flags)
2335 return flags & ROCKER_OP_FLAG_NOWAIT ? GFP_ATOMIC : GFP_KERNEL;
2338 static int rocker_flow_tbl_do(struct rocker_port *rocker_port,
2339 int flags, struct rocker_flow_tbl_entry *entry)
2341 bool nowait = flags & ROCKER_OP_FLAG_NOWAIT;
2343 if (flags & ROCKER_OP_FLAG_REMOVE)
2344 return rocker_flow_tbl_del(rocker_port, entry, nowait);
2346 return rocker_flow_tbl_add(rocker_port, entry, nowait);
2349 static int rocker_flow_tbl_ig_port(struct rocker_port *rocker_port,
2350 int flags, u32 in_pport, u32 in_pport_mask,
2351 enum rocker_of_dpa_table_id goto_tbl)
2353 struct rocker_flow_tbl_entry *entry;
2355 entry = kzalloc(sizeof(*entry), rocker_op_flags_gfp(flags));
2359 entry->key.priority = ROCKER_PRIORITY_IG_PORT;
2360 entry->key.tbl_id = ROCKER_OF_DPA_TABLE_ID_INGRESS_PORT;
2361 entry->key.ig_port.in_pport = in_pport;
2362 entry->key.ig_port.in_pport_mask = in_pport_mask;
2363 entry->key.ig_port.goto_tbl = goto_tbl;
2365 return rocker_flow_tbl_do(rocker_port, flags, entry);
2368 static int rocker_flow_tbl_vlan(struct rocker_port *rocker_port,
2369 int flags, u32 in_pport,
2370 __be16 vlan_id, __be16 vlan_id_mask,
2371 enum rocker_of_dpa_table_id goto_tbl,
2372 bool untagged, __be16 new_vlan_id)
2374 struct rocker_flow_tbl_entry *entry;
2376 entry = kzalloc(sizeof(*entry), rocker_op_flags_gfp(flags));
2380 entry->key.priority = ROCKER_PRIORITY_VLAN;
2381 entry->key.tbl_id = ROCKER_OF_DPA_TABLE_ID_VLAN;
2382 entry->key.vlan.in_pport = in_pport;
2383 entry->key.vlan.vlan_id = vlan_id;
2384 entry->key.vlan.vlan_id_mask = vlan_id_mask;
2385 entry->key.vlan.goto_tbl = goto_tbl;
2387 entry->key.vlan.untagged = untagged;
2388 entry->key.vlan.new_vlan_id = new_vlan_id;
2390 return rocker_flow_tbl_do(rocker_port, flags, entry);
2393 static int rocker_flow_tbl_term_mac(struct rocker_port *rocker_port,
2394 u32 in_pport, u32 in_pport_mask,
2395 __be16 eth_type, const u8 *eth_dst,
2396 const u8 *eth_dst_mask, __be16 vlan_id,
2397 __be16 vlan_id_mask, bool copy_to_cpu,
2400 struct rocker_flow_tbl_entry *entry;
2402 entry = kzalloc(sizeof(*entry), rocker_op_flags_gfp(flags));
2406 if (is_multicast_ether_addr(eth_dst)) {
2407 entry->key.priority = ROCKER_PRIORITY_TERM_MAC_MCAST;
2408 entry->key.term_mac.goto_tbl =
2409 ROCKER_OF_DPA_TABLE_ID_MULTICAST_ROUTING;
2411 entry->key.priority = ROCKER_PRIORITY_TERM_MAC_UCAST;
2412 entry->key.term_mac.goto_tbl =
2413 ROCKER_OF_DPA_TABLE_ID_UNICAST_ROUTING;
2416 entry->key.tbl_id = ROCKER_OF_DPA_TABLE_ID_TERMINATION_MAC;
2417 entry->key.term_mac.in_pport = in_pport;
2418 entry->key.term_mac.in_pport_mask = in_pport_mask;
2419 entry->key.term_mac.eth_type = eth_type;
2420 ether_addr_copy(entry->key.term_mac.eth_dst, eth_dst);
2421 ether_addr_copy(entry->key.term_mac.eth_dst_mask, eth_dst_mask);
2422 entry->key.term_mac.vlan_id = vlan_id;
2423 entry->key.term_mac.vlan_id_mask = vlan_id_mask;
2424 entry->key.term_mac.copy_to_cpu = copy_to_cpu;
2426 return rocker_flow_tbl_do(rocker_port, flags, entry);
2429 static int rocker_flow_tbl_bridge(struct rocker_port *rocker_port,
2431 const u8 *eth_dst, const u8 *eth_dst_mask,
2432 __be16 vlan_id, u32 tunnel_id,
2433 enum rocker_of_dpa_table_id goto_tbl,
2434 u32 group_id, bool copy_to_cpu)
2436 struct rocker_flow_tbl_entry *entry;
2438 bool vlan_bridging = !!vlan_id;
2439 bool dflt = !eth_dst || (eth_dst && eth_dst_mask);
2442 entry = kzalloc(sizeof(*entry), rocker_op_flags_gfp(flags));
2446 entry->key.tbl_id = ROCKER_OF_DPA_TABLE_ID_BRIDGING;
2449 entry->key.bridge.has_eth_dst = 1;
2450 ether_addr_copy(entry->key.bridge.eth_dst, eth_dst);
2453 entry->key.bridge.has_eth_dst_mask = 1;
2454 ether_addr_copy(entry->key.bridge.eth_dst_mask, eth_dst_mask);
2455 if (memcmp(eth_dst_mask, ff_mac, ETH_ALEN))
2459 priority = ROCKER_PRIORITY_UNKNOWN;
2460 if (vlan_bridging && dflt && wild)
2461 priority = ROCKER_PRIORITY_BRIDGING_VLAN_DFLT_WILD;
2462 else if (vlan_bridging && dflt && !wild)
2463 priority = ROCKER_PRIORITY_BRIDGING_VLAN_DFLT_EXACT;
2464 else if (vlan_bridging && !dflt)
2465 priority = ROCKER_PRIORITY_BRIDGING_VLAN;
2466 else if (!vlan_bridging && dflt && wild)
2467 priority = ROCKER_PRIORITY_BRIDGING_TENANT_DFLT_WILD;
2468 else if (!vlan_bridging && dflt && !wild)
2469 priority = ROCKER_PRIORITY_BRIDGING_TENANT_DFLT_EXACT;
2470 else if (!vlan_bridging && !dflt)
2471 priority = ROCKER_PRIORITY_BRIDGING_TENANT;
2473 entry->key.priority = priority;
2474 entry->key.bridge.vlan_id = vlan_id;
2475 entry->key.bridge.tunnel_id = tunnel_id;
2476 entry->key.bridge.goto_tbl = goto_tbl;
2477 entry->key.bridge.group_id = group_id;
2478 entry->key.bridge.copy_to_cpu = copy_to_cpu;
2480 return rocker_flow_tbl_do(rocker_port, flags, entry);
2483 static int rocker_flow_tbl_ucast4_routing(struct rocker_port *rocker_port,
2484 __be16 eth_type, __be32 dst,
2485 __be32 dst_mask, u32 priority,
2486 enum rocker_of_dpa_table_id goto_tbl,
2487 u32 group_id, int flags)
2489 struct rocker_flow_tbl_entry *entry;
2491 entry = kzalloc(sizeof(*entry), rocker_op_flags_gfp(flags));
2495 entry->key.tbl_id = ROCKER_OF_DPA_TABLE_ID_UNICAST_ROUTING;
2496 entry->key.priority = priority;
2497 entry->key.ucast_routing.eth_type = eth_type;
2498 entry->key.ucast_routing.dst4 = dst;
2499 entry->key.ucast_routing.dst4_mask = dst_mask;
2500 entry->key.ucast_routing.goto_tbl = goto_tbl;
2501 entry->key.ucast_routing.group_id = group_id;
2502 entry->key_len = offsetof(struct rocker_flow_tbl_key,
2503 ucast_routing.group_id);
2505 return rocker_flow_tbl_do(rocker_port, flags, entry);
2508 static int rocker_flow_tbl_acl(struct rocker_port *rocker_port,
2509 int flags, u32 in_pport,
2511 const u8 *eth_src, const u8 *eth_src_mask,
2512 const u8 *eth_dst, const u8 *eth_dst_mask,
2514 __be16 vlan_id, __be16 vlan_id_mask,
2515 u8 ip_proto, u8 ip_proto_mask,
2516 u8 ip_tos, u8 ip_tos_mask,
2520 struct rocker_flow_tbl_entry *entry;
2522 entry = kzalloc(sizeof(*entry), rocker_op_flags_gfp(flags));
2526 priority = ROCKER_PRIORITY_ACL_NORMAL;
2527 if (eth_dst && eth_dst_mask) {
2528 if (memcmp(eth_dst_mask, mcast_mac, ETH_ALEN) == 0)
2529 priority = ROCKER_PRIORITY_ACL_DFLT;
2530 else if (is_link_local_ether_addr(eth_dst))
2531 priority = ROCKER_PRIORITY_ACL_CTRL;
2534 entry->key.priority = priority;
2535 entry->key.tbl_id = ROCKER_OF_DPA_TABLE_ID_ACL_POLICY;
2536 entry->key.acl.in_pport = in_pport;
2537 entry->key.acl.in_pport_mask = in_pport_mask;
2540 ether_addr_copy(entry->key.acl.eth_src, eth_src);
2542 ether_addr_copy(entry->key.acl.eth_src_mask, eth_src_mask);
2544 ether_addr_copy(entry->key.acl.eth_dst, eth_dst);
2546 ether_addr_copy(entry->key.acl.eth_dst_mask, eth_dst_mask);
2548 entry->key.acl.eth_type = eth_type;
2549 entry->key.acl.vlan_id = vlan_id;
2550 entry->key.acl.vlan_id_mask = vlan_id_mask;
2551 entry->key.acl.ip_proto = ip_proto;
2552 entry->key.acl.ip_proto_mask = ip_proto_mask;
2553 entry->key.acl.ip_tos = ip_tos;
2554 entry->key.acl.ip_tos_mask = ip_tos_mask;
2555 entry->key.acl.group_id = group_id;
2557 return rocker_flow_tbl_do(rocker_port, flags, entry);
2560 static struct rocker_group_tbl_entry *
2561 rocker_group_tbl_find(struct rocker *rocker,
2562 struct rocker_group_tbl_entry *match)
2564 struct rocker_group_tbl_entry *found;
2566 hash_for_each_possible(rocker->group_tbl, found,
2567 entry, match->group_id) {
2568 if (found->group_id == match->group_id)
2575 static void rocker_group_tbl_entry_free(struct rocker_group_tbl_entry *entry)
2577 switch (ROCKER_GROUP_TYPE_GET(entry->group_id)) {
2578 case ROCKER_OF_DPA_GROUP_TYPE_L2_FLOOD:
2579 case ROCKER_OF_DPA_GROUP_TYPE_L2_MCAST:
2580 kfree(entry->group_ids);
2588 static int rocker_group_tbl_add(struct rocker_port *rocker_port,
2589 struct rocker_group_tbl_entry *match,
2592 struct rocker *rocker = rocker_port->rocker;
2593 struct rocker_group_tbl_entry *found;
2594 unsigned long flags;
2596 spin_lock_irqsave(&rocker->group_tbl_lock, flags);
2598 found = rocker_group_tbl_find(rocker, match);
2601 hash_del(&found->entry);
2602 rocker_group_tbl_entry_free(found);
2604 found->cmd = ROCKER_TLV_CMD_TYPE_OF_DPA_GROUP_MOD;
2607 found->cmd = ROCKER_TLV_CMD_TYPE_OF_DPA_GROUP_ADD;
2610 hash_add(rocker->group_tbl, &found->entry, found->group_id);
2612 spin_unlock_irqrestore(&rocker->group_tbl_lock, flags);
2614 return rocker_cmd_exec(rocker, rocker_port,
2615 rocker_cmd_group_tbl_add,
2616 found, NULL, NULL, nowait);
2619 static int rocker_group_tbl_del(struct rocker_port *rocker_port,
2620 struct rocker_group_tbl_entry *match,
2623 struct rocker *rocker = rocker_port->rocker;
2624 struct rocker_group_tbl_entry *found;
2625 unsigned long flags;
2628 spin_lock_irqsave(&rocker->group_tbl_lock, flags);
2630 found = rocker_group_tbl_find(rocker, match);
2633 hash_del(&found->entry);
2634 found->cmd = ROCKER_TLV_CMD_TYPE_OF_DPA_GROUP_DEL;
2637 spin_unlock_irqrestore(&rocker->group_tbl_lock, flags);
2639 rocker_group_tbl_entry_free(match);
2642 err = rocker_cmd_exec(rocker, rocker_port,
2643 rocker_cmd_group_tbl_del,
2644 found, NULL, NULL, nowait);
2645 rocker_group_tbl_entry_free(found);
2651 static int rocker_group_tbl_do(struct rocker_port *rocker_port,
2652 int flags, struct rocker_group_tbl_entry *entry)
2654 bool nowait = flags & ROCKER_OP_FLAG_NOWAIT;
2656 if (flags & ROCKER_OP_FLAG_REMOVE)
2657 return rocker_group_tbl_del(rocker_port, entry, nowait);
2659 return rocker_group_tbl_add(rocker_port, entry, nowait);
2662 static int rocker_group_l2_interface(struct rocker_port *rocker_port,
2663 int flags, __be16 vlan_id,
2664 u32 out_pport, int pop_vlan)
2666 struct rocker_group_tbl_entry *entry;
2668 entry = kzalloc(sizeof(*entry), rocker_op_flags_gfp(flags));
2672 entry->group_id = ROCKER_GROUP_L2_INTERFACE(vlan_id, out_pport);
2673 entry->l2_interface.pop_vlan = pop_vlan;
2675 return rocker_group_tbl_do(rocker_port, flags, entry);
2678 static int rocker_group_l2_fan_out(struct rocker_port *rocker_port,
2679 int flags, u8 group_count,
2680 u32 *group_ids, u32 group_id)
2682 struct rocker_group_tbl_entry *entry;
2684 entry = kzalloc(sizeof(*entry), rocker_op_flags_gfp(flags));
2688 entry->group_id = group_id;
2689 entry->group_count = group_count;
2691 entry->group_ids = kcalloc(group_count, sizeof(u32),
2692 rocker_op_flags_gfp(flags));
2693 if (!entry->group_ids) {
2697 memcpy(entry->group_ids, group_ids, group_count * sizeof(u32));
2699 return rocker_group_tbl_do(rocker_port, flags, entry);
2702 static int rocker_group_l2_flood(struct rocker_port *rocker_port,
2703 int flags, __be16 vlan_id,
2704 u8 group_count, u32 *group_ids,
2707 return rocker_group_l2_fan_out(rocker_port, flags,
2708 group_count, group_ids,
2712 static int rocker_group_l3_unicast(struct rocker_port *rocker_port,
2713 int flags, u32 index, u8 *src_mac,
2714 u8 *dst_mac, __be16 vlan_id,
2715 bool ttl_check, u32 pport)
2717 struct rocker_group_tbl_entry *entry;
2719 entry = kzalloc(sizeof(*entry), rocker_op_flags_gfp(flags));
2723 entry->group_id = ROCKER_GROUP_L3_UNICAST(index);
2725 ether_addr_copy(entry->l3_unicast.eth_src, src_mac);
2727 ether_addr_copy(entry->l3_unicast.eth_dst, dst_mac);
2728 entry->l3_unicast.vlan_id = vlan_id;
2729 entry->l3_unicast.ttl_check = ttl_check;
2730 entry->l3_unicast.group_id = ROCKER_GROUP_L2_INTERFACE(vlan_id, pport);
2732 return rocker_group_tbl_do(rocker_port, flags, entry);
2735 static struct rocker_neigh_tbl_entry *
2736 rocker_neigh_tbl_find(struct rocker *rocker, __be32 ip_addr)
2738 struct rocker_neigh_tbl_entry *found;
2740 hash_for_each_possible(rocker->neigh_tbl, found,
2741 entry, be32_to_cpu(ip_addr))
2742 if (found->ip_addr == ip_addr)
2748 static void _rocker_neigh_add(struct rocker *rocker,
2749 struct rocker_neigh_tbl_entry *entry)
2751 entry->index = rocker->neigh_tbl_next_index++;
2753 hash_add(rocker->neigh_tbl, &entry->entry,
2754 be32_to_cpu(entry->ip_addr));
2757 static void _rocker_neigh_del(struct rocker *rocker,
2758 struct rocker_neigh_tbl_entry *entry)
2760 if (--entry->ref_count == 0) {
2761 hash_del(&entry->entry);
2766 static void _rocker_neigh_update(struct rocker *rocker,
2767 struct rocker_neigh_tbl_entry *entry,
2768 u8 *eth_dst, bool ttl_check)
2771 ether_addr_copy(entry->eth_dst, eth_dst);
2772 entry->ttl_check = ttl_check;
2778 static int rocker_port_ipv4_neigh(struct rocker_port *rocker_port,
2779 int flags, __be32 ip_addr, u8 *eth_dst)
2781 struct rocker *rocker = rocker_port->rocker;
2782 struct rocker_neigh_tbl_entry *entry;
2783 struct rocker_neigh_tbl_entry *found;
2784 unsigned long lock_flags;
2785 __be16 eth_type = htons(ETH_P_IP);
2786 enum rocker_of_dpa_table_id goto_tbl =
2787 ROCKER_OF_DPA_TABLE_ID_ACL_POLICY;
2790 bool adding = !(flags & ROCKER_OP_FLAG_REMOVE);
2795 entry = kzalloc(sizeof(*entry), rocker_op_flags_gfp(flags));
2799 spin_lock_irqsave(&rocker->neigh_tbl_lock, lock_flags);
2801 found = rocker_neigh_tbl_find(rocker, ip_addr);
2803 updating = found && adding;
2804 removing = found && !adding;
2805 adding = !found && adding;
2808 entry->ip_addr = ip_addr;
2809 entry->dev = rocker_port->dev;
2810 ether_addr_copy(entry->eth_dst, eth_dst);
2811 entry->ttl_check = true;
2812 _rocker_neigh_add(rocker, entry);
2813 } else if (removing) {
2814 memcpy(entry, found, sizeof(*entry));
2815 _rocker_neigh_del(rocker, found);
2816 } else if (updating) {
2817 _rocker_neigh_update(rocker, found, eth_dst, true);
2818 memcpy(entry, found, sizeof(*entry));
2823 spin_unlock_irqrestore(&rocker->neigh_tbl_lock, lock_flags);
2828 /* For each active neighbor, we have an L3 unicast group and
2829 * a /32 route to the neighbor, which uses the L3 unicast
2830 * group. The L3 unicast group can also be referred to by
2831 * other routes' nexthops.
2834 err = rocker_group_l3_unicast(rocker_port, flags,
2836 rocker_port->dev->dev_addr,
2838 rocker_port->internal_vlan_id,
2840 rocker_port->pport);
2842 netdev_err(rocker_port->dev,
2843 "Error (%d) L3 unicast group index %d\n",
2848 if (adding || removing) {
2849 group_id = ROCKER_GROUP_L3_UNICAST(entry->index);
2850 err = rocker_flow_tbl_ucast4_routing(rocker_port,
2857 netdev_err(rocker_port->dev,
2858 "Error (%d) /32 unicast route %pI4 group 0x%08x\n",
2859 err, &entry->ip_addr, group_id);
2869 static int rocker_port_ipv4_resolve(struct rocker_port *rocker_port,
2872 struct net_device *dev = rocker_port->dev;
2873 struct neighbour *n = __ipv4_neigh_lookup(dev, (__force u32)ip_addr);
2877 n = neigh_create(&arp_tbl, &ip_addr, dev);
2881 /* If the neigh is already resolved, then go ahead and
2882 * install the entry, otherwise start the ARP process to
2883 * resolve the neigh.
2886 if (n->nud_state & NUD_VALID)
2887 err = rocker_port_ipv4_neigh(rocker_port, 0, ip_addr, n->ha);
2889 neigh_event_send(n, NULL);
2894 static int rocker_port_ipv4_nh(struct rocker_port *rocker_port, int flags,
2895 __be32 ip_addr, u32 *index)
2897 struct rocker *rocker = rocker_port->rocker;
2898 struct rocker_neigh_tbl_entry *entry;
2899 struct rocker_neigh_tbl_entry *found;
2900 unsigned long lock_flags;
2901 bool adding = !(flags & ROCKER_OP_FLAG_REMOVE);
2904 bool resolved = true;
2907 entry = kzalloc(sizeof(*entry), rocker_op_flags_gfp(flags));
2911 spin_lock_irqsave(&rocker->neigh_tbl_lock, lock_flags);
2913 found = rocker_neigh_tbl_find(rocker, ip_addr);
2915 *index = found->index;
2917 updating = found && adding;
2918 removing = found && !adding;
2919 adding = !found && adding;
2922 entry->ip_addr = ip_addr;
2923 entry->dev = rocker_port->dev;
2924 _rocker_neigh_add(rocker, entry);
2925 *index = entry->index;
2927 } else if (removing) {
2928 _rocker_neigh_del(rocker, found);
2929 } else if (updating) {
2930 _rocker_neigh_update(rocker, found, NULL, false);
2931 resolved = !is_zero_ether_addr(found->eth_dst);
2936 spin_unlock_irqrestore(&rocker->neigh_tbl_lock, lock_flags);
2944 /* Resolved means neigh ip_addr is resolved to neigh mac. */
2947 err = rocker_port_ipv4_resolve(rocker_port, ip_addr);
2952 static int rocker_port_vlan_flood_group(struct rocker_port *rocker_port,
2953 int flags, __be16 vlan_id)
2955 struct rocker_port *p;
2956 struct rocker *rocker = rocker_port->rocker;
2957 u32 group_id = ROCKER_GROUP_L2_FLOOD(vlan_id, 0);
2958 u32 group_ids[ROCKER_FP_PORTS_MAX];
2963 /* Adjust the flood group for this VLAN. The flood group
2964 * references an L2 interface group for each port in this
2968 for (i = 0; i < rocker->port_count; i++) {
2969 p = rocker->ports[i];
2970 if (!rocker_port_is_bridged(p))
2972 if (test_bit(ntohs(vlan_id), p->vlan_bitmap)) {
2973 group_ids[group_count++] =
2974 ROCKER_GROUP_L2_INTERFACE(vlan_id, p->pport);
2978 /* If there are no bridged ports in this VLAN, we're done */
2979 if (group_count == 0)
2982 err = rocker_group_l2_flood(rocker_port, flags, vlan_id,
2983 group_count, group_ids,
2986 netdev_err(rocker_port->dev,
2987 "Error (%d) port VLAN l2 flood group\n", err);
2992 static int rocker_port_vlan_l2_groups(struct rocker_port *rocker_port,
2993 int flags, __be16 vlan_id,
2996 struct rocker *rocker = rocker_port->rocker;
2997 struct rocker_port *p;
2998 bool adding = !(flags & ROCKER_OP_FLAG_REMOVE);
3004 /* An L2 interface group for this port in this VLAN, but
3005 * only when port STP state is LEARNING|FORWARDING.
3008 if (rocker_port->stp_state == BR_STATE_LEARNING ||
3009 rocker_port->stp_state == BR_STATE_FORWARDING) {
3010 out_pport = rocker_port->pport;
3011 err = rocker_group_l2_interface(rocker_port, flags,
3015 netdev_err(rocker_port->dev,
3016 "Error (%d) port VLAN l2 group for pport %d\n",
3022 /* An L2 interface group for this VLAN to CPU port.
3023 * Add when first port joins this VLAN and destroy when
3024 * last port leaves this VLAN.
3027 for (i = 0; i < rocker->port_count; i++) {
3028 p = rocker->ports[i];
3029 if (test_bit(ntohs(vlan_id), p->vlan_bitmap))
3033 if ((!adding || ref != 1) && (adding || ref != 0))
3037 err = rocker_group_l2_interface(rocker_port, flags,
3041 netdev_err(rocker_port->dev,
3042 "Error (%d) port VLAN l2 group for CPU port\n", err);
3049 static struct rocker_ctrl {
3051 const u8 *eth_dst_mask;
3057 } rocker_ctrls[] = {
3058 [ROCKER_CTRL_LINK_LOCAL_MCAST] = {
3059 /* pass link local multicast pkts up to CPU for filtering */
3061 .eth_dst_mask = ll_mask,
3064 [ROCKER_CTRL_LOCAL_ARP] = {
3065 /* pass local ARP pkts up to CPU */
3066 .eth_dst = zero_mac,
3067 .eth_dst_mask = zero_mac,
3068 .eth_type = htons(ETH_P_ARP),
3071 [ROCKER_CTRL_IPV4_MCAST] = {
3072 /* pass IPv4 mcast pkts up to CPU, RFC 1112 */
3073 .eth_dst = ipv4_mcast,
3074 .eth_dst_mask = ipv4_mask,
3075 .eth_type = htons(ETH_P_IP),
3077 .copy_to_cpu = true,
3079 [ROCKER_CTRL_IPV6_MCAST] = {
3080 /* pass IPv6 mcast pkts up to CPU, RFC 2464 */
3081 .eth_dst = ipv6_mcast,
3082 .eth_dst_mask = ipv6_mask,
3083 .eth_type = htons(ETH_P_IPV6),
3085 .copy_to_cpu = true,
3087 [ROCKER_CTRL_DFLT_BRIDGING] = {
3088 /* flood any pkts on vlan */
3090 .copy_to_cpu = true,
3094 static int rocker_port_ctrl_vlan_acl(struct rocker_port *rocker_port,
3095 int flags, struct rocker_ctrl *ctrl,
3098 u32 in_pport = rocker_port->pport;
3099 u32 in_pport_mask = 0xffffffff;
3102 u8 *eth_src_mask = NULL;
3103 __be16 vlan_id_mask = htons(0xffff);
3105 u8 ip_proto_mask = 0;
3108 u32 group_id = ROCKER_GROUP_L2_INTERFACE(vlan_id, out_pport);
3111 err = rocker_flow_tbl_acl(rocker_port, flags,
3112 in_pport, in_pport_mask,
3113 eth_src, eth_src_mask,
3114 ctrl->eth_dst, ctrl->eth_dst_mask,
3116 vlan_id, vlan_id_mask,
3117 ip_proto, ip_proto_mask,
3118 ip_tos, ip_tos_mask,
3122 netdev_err(rocker_port->dev, "Error (%d) ctrl ACL\n", err);
3127 static int rocker_port_ctrl_vlan_bridge(struct rocker_port *rocker_port,
3128 int flags, struct rocker_ctrl *ctrl,
3131 enum rocker_of_dpa_table_id goto_tbl =
3132 ROCKER_OF_DPA_TABLE_ID_ACL_POLICY;
3133 u32 group_id = ROCKER_GROUP_L2_FLOOD(vlan_id, 0);
3137 if (!rocker_port_is_bridged(rocker_port))
3140 err = rocker_flow_tbl_bridge(rocker_port, flags,
3141 ctrl->eth_dst, ctrl->eth_dst_mask,
3143 goto_tbl, group_id, ctrl->copy_to_cpu);
3146 netdev_err(rocker_port->dev, "Error (%d) ctrl FLOOD\n", err);
3151 static int rocker_port_ctrl_vlan_term(struct rocker_port *rocker_port,
3152 int flags, struct rocker_ctrl *ctrl,
3155 u32 in_pport_mask = 0xffffffff;
3156 __be16 vlan_id_mask = htons(0xffff);
3159 if (ntohs(vlan_id) == 0)
3160 vlan_id = rocker_port->internal_vlan_id;
3162 err = rocker_flow_tbl_term_mac(rocker_port,
3163 rocker_port->pport, in_pport_mask,
3164 ctrl->eth_type, ctrl->eth_dst,
3165 ctrl->eth_dst_mask, vlan_id,
3166 vlan_id_mask, ctrl->copy_to_cpu,
3170 netdev_err(rocker_port->dev, "Error (%d) ctrl term\n", err);
3175 static int rocker_port_ctrl_vlan(struct rocker_port *rocker_port, int flags,
3176 struct rocker_ctrl *ctrl, __be16 vlan_id)
3179 return rocker_port_ctrl_vlan_acl(rocker_port, flags,
3182 return rocker_port_ctrl_vlan_bridge(rocker_port, flags,
3186 return rocker_port_ctrl_vlan_term(rocker_port, flags,
3192 static int rocker_port_ctrl_vlan_add(struct rocker_port *rocker_port,
3193 int flags, __be16 vlan_id)
3198 for (i = 0; i < ROCKER_CTRL_MAX; i++) {
3199 if (rocker_port->ctrls[i]) {
3200 err = rocker_port_ctrl_vlan(rocker_port, flags,
3201 &rocker_ctrls[i], vlan_id);
3210 static int rocker_port_ctrl(struct rocker_port *rocker_port, int flags,
3211 struct rocker_ctrl *ctrl)
3216 for (vid = 1; vid < VLAN_N_VID; vid++) {
3217 if (!test_bit(vid, rocker_port->vlan_bitmap))
3219 err = rocker_port_ctrl_vlan(rocker_port, flags,
3228 static int rocker_port_vlan(struct rocker_port *rocker_port, int flags,
3231 enum rocker_of_dpa_table_id goto_tbl =
3232 ROCKER_OF_DPA_TABLE_ID_TERMINATION_MAC;
3233 u32 in_pport = rocker_port->pport;
3234 __be16 vlan_id = htons(vid);
3235 __be16 vlan_id_mask = htons(0xffff);
3236 __be16 internal_vlan_id;
3238 bool adding = !(flags & ROCKER_OP_FLAG_REMOVE);
3241 internal_vlan_id = rocker_port_vid_to_vlan(rocker_port, vid, &untagged);
3243 if (adding && test_and_set_bit(ntohs(internal_vlan_id),
3244 rocker_port->vlan_bitmap))
3245 return 0; /* already added */
3246 else if (!adding && !test_and_clear_bit(ntohs(internal_vlan_id),
3247 rocker_port->vlan_bitmap))
3248 return 0; /* already removed */
3251 err = rocker_port_ctrl_vlan_add(rocker_port, flags,
3254 netdev_err(rocker_port->dev,
3255 "Error (%d) port ctrl vlan add\n", err);
3260 err = rocker_port_vlan_l2_groups(rocker_port, flags,
3261 internal_vlan_id, untagged);
3263 netdev_err(rocker_port->dev,
3264 "Error (%d) port VLAN l2 groups\n", err);
3268 err = rocker_port_vlan_flood_group(rocker_port, flags,
3271 netdev_err(rocker_port->dev,
3272 "Error (%d) port VLAN l2 flood group\n", err);
3276 err = rocker_flow_tbl_vlan(rocker_port, flags,
3277 in_pport, vlan_id, vlan_id_mask,
3278 goto_tbl, untagged, internal_vlan_id);
3280 netdev_err(rocker_port->dev,
3281 "Error (%d) port VLAN table\n", err);
3286 static int rocker_port_ig_tbl(struct rocker_port *rocker_port, int flags)
3288 enum rocker_of_dpa_table_id goto_tbl;
3293 /* Normal Ethernet Frames. Matches pkts from any local physical
3294 * ports. Goto VLAN tbl.
3298 in_pport_mask = 0xffff0000;
3299 goto_tbl = ROCKER_OF_DPA_TABLE_ID_VLAN;
3301 err = rocker_flow_tbl_ig_port(rocker_port, flags,
3302 in_pport, in_pport_mask,
3305 netdev_err(rocker_port->dev,
3306 "Error (%d) ingress port table entry\n", err);
3311 struct rocker_fdb_learn_work {
3312 struct work_struct work;
3313 struct net_device *dev;
3319 static void rocker_port_fdb_learn_work(struct work_struct *work)
3321 struct rocker_fdb_learn_work *lw =
3322 container_of(work, struct rocker_fdb_learn_work, work);
3323 bool removing = (lw->flags & ROCKER_OP_FLAG_REMOVE);
3324 bool learned = (lw->flags & ROCKER_OP_FLAG_LEARNED);
3325 struct netdev_switch_notifier_fdb_info info;
3327 info.addr = lw->addr;
3330 if (learned && removing)
3331 call_netdev_switch_notifiers(NETDEV_SWITCH_FDB_DEL,
3332 lw->dev, &info.info);
3333 else if (learned && !removing)
3334 call_netdev_switch_notifiers(NETDEV_SWITCH_FDB_ADD,
3335 lw->dev, &info.info);
3340 static int rocker_port_fdb_learn(struct rocker_port *rocker_port,
3341 int flags, const u8 *addr, __be16 vlan_id)
3343 struct rocker_fdb_learn_work *lw;
3344 enum rocker_of_dpa_table_id goto_tbl =
3345 ROCKER_OF_DPA_TABLE_ID_ACL_POLICY;
3346 u32 out_pport = rocker_port->pport;
3348 u32 group_id = ROCKER_GROUP_NONE;
3349 bool syncing = !!(rocker_port->brport_flags & BR_LEARNING_SYNC);
3350 bool copy_to_cpu = false;
3353 if (rocker_port_is_bridged(rocker_port))
3354 group_id = ROCKER_GROUP_L2_INTERFACE(vlan_id, out_pport);
3356 if (!(flags & ROCKER_OP_FLAG_REFRESH)) {
3357 err = rocker_flow_tbl_bridge(rocker_port, flags, addr, NULL,
3358 vlan_id, tunnel_id, goto_tbl,
3359 group_id, copy_to_cpu);
3367 if (!rocker_port_is_bridged(rocker_port))
3370 lw = kmalloc(sizeof(*lw), rocker_op_flags_gfp(flags));
3374 INIT_WORK(&lw->work, rocker_port_fdb_learn_work);
3376 lw->dev = rocker_port->dev;
3378 ether_addr_copy(lw->addr, addr);
3379 lw->vid = rocker_port_vlan_to_vid(rocker_port, vlan_id);
3381 schedule_work(&lw->work);
3386 static struct rocker_fdb_tbl_entry *
3387 rocker_fdb_tbl_find(struct rocker *rocker, struct rocker_fdb_tbl_entry *match)
3389 struct rocker_fdb_tbl_entry *found;
3391 hash_for_each_possible(rocker->fdb_tbl, found, entry, match->key_crc32)
3392 if (memcmp(&found->key, &match->key, sizeof(found->key)) == 0)
3398 static int rocker_port_fdb(struct rocker_port *rocker_port,
3399 const unsigned char *addr,
3400 __be16 vlan_id, int flags)
3402 struct rocker *rocker = rocker_port->rocker;
3403 struct rocker_fdb_tbl_entry *fdb;
3404 struct rocker_fdb_tbl_entry *found;
3405 bool removing = (flags & ROCKER_OP_FLAG_REMOVE);
3406 unsigned long lock_flags;
3408 fdb = kzalloc(sizeof(*fdb), rocker_op_flags_gfp(flags));
3412 fdb->learned = (flags & ROCKER_OP_FLAG_LEARNED);
3413 fdb->key.pport = rocker_port->pport;
3414 ether_addr_copy(fdb->key.addr, addr);
3415 fdb->key.vlan_id = vlan_id;
3416 fdb->key_crc32 = crc32(~0, &fdb->key, sizeof(fdb->key));
3418 spin_lock_irqsave(&rocker->fdb_tbl_lock, lock_flags);
3420 found = rocker_fdb_tbl_find(rocker, fdb);
3422 if (removing && found) {
3424 hash_del(&found->entry);
3425 } else if (!removing && !found) {
3426 hash_add(rocker->fdb_tbl, &fdb->entry, fdb->key_crc32);
3429 spin_unlock_irqrestore(&rocker->fdb_tbl_lock, lock_flags);
3431 /* Check if adding and already exists, or removing and can't find */
3432 if (!found != !removing) {
3434 if (!found && removing)
3436 /* Refreshing existing to update aging timers */
3437 flags |= ROCKER_OP_FLAG_REFRESH;
3440 return rocker_port_fdb_learn(rocker_port, flags, addr, vlan_id);
3443 static int rocker_port_fdb_flush(struct rocker_port *rocker_port)
3445 struct rocker *rocker = rocker_port->rocker;
3446 struct rocker_fdb_tbl_entry *found;
3447 unsigned long lock_flags;
3448 int flags = ROCKER_OP_FLAG_NOWAIT | ROCKER_OP_FLAG_REMOVE;
3449 struct hlist_node *tmp;
3453 if (rocker_port->stp_state == BR_STATE_LEARNING ||
3454 rocker_port->stp_state == BR_STATE_FORWARDING)
3457 spin_lock_irqsave(&rocker->fdb_tbl_lock, lock_flags);
3459 hash_for_each_safe(rocker->fdb_tbl, bkt, tmp, found, entry) {
3460 if (found->key.pport != rocker_port->pport)
3462 if (!found->learned)
3464 err = rocker_port_fdb_learn(rocker_port, flags,
3466 found->key.vlan_id);
3469 hash_del(&found->entry);
3473 spin_unlock_irqrestore(&rocker->fdb_tbl_lock, lock_flags);
3478 static int rocker_port_router_mac(struct rocker_port *rocker_port,
3479 int flags, __be16 vlan_id)
3481 u32 in_pport_mask = 0xffffffff;
3483 const u8 *dst_mac_mask = ff_mac;
3484 __be16 vlan_id_mask = htons(0xffff);
3485 bool copy_to_cpu = false;
3488 if (ntohs(vlan_id) == 0)
3489 vlan_id = rocker_port->internal_vlan_id;
3491 eth_type = htons(ETH_P_IP);
3492 err = rocker_flow_tbl_term_mac(rocker_port,
3493 rocker_port->pport, in_pport_mask,
3494 eth_type, rocker_port->dev->dev_addr,
3495 dst_mac_mask, vlan_id, vlan_id_mask,
3496 copy_to_cpu, flags);
3500 eth_type = htons(ETH_P_IPV6);
3501 err = rocker_flow_tbl_term_mac(rocker_port,
3502 rocker_port->pport, in_pport_mask,
3503 eth_type, rocker_port->dev->dev_addr,
3504 dst_mac_mask, vlan_id, vlan_id_mask,
3505 copy_to_cpu, flags);
3510 static int rocker_port_fwding(struct rocker_port *rocker_port)
3516 int flags = ROCKER_OP_FLAG_NOWAIT;
3519 /* Port will be forwarding-enabled if its STP state is LEARNING
3520 * or FORWARDING. Traffic from CPU can still egress, regardless of
3521 * port STP state. Use L2 interface group on port VLANs as a way
3522 * to toggle port forwarding: if forwarding is disabled, L2
3523 * interface group will not exist.
3526 if (rocker_port->stp_state != BR_STATE_LEARNING &&
3527 rocker_port->stp_state != BR_STATE_FORWARDING)
3528 flags |= ROCKER_OP_FLAG_REMOVE;
3530 out_pport = rocker_port->pport;
3531 for (vid = 1; vid < VLAN_N_VID; vid++) {
3532 if (!test_bit(vid, rocker_port->vlan_bitmap))
3534 vlan_id = htons(vid);
3535 pop_vlan = rocker_vlan_id_is_internal(vlan_id);
3536 err = rocker_group_l2_interface(rocker_port, flags,
3540 netdev_err(rocker_port->dev,
3541 "Error (%d) port VLAN l2 group for pport %d\n",
3550 static int rocker_port_stp_update(struct rocker_port *rocker_port, u8 state)
3552 bool want[ROCKER_CTRL_MAX] = { 0, };
3557 if (rocker_port->stp_state == state)
3560 rocker_port->stp_state = state;
3563 case BR_STATE_DISABLED:
3564 /* port is completely disabled */
3566 case BR_STATE_LISTENING:
3567 case BR_STATE_BLOCKING:
3568 want[ROCKER_CTRL_LINK_LOCAL_MCAST] = true;
3570 case BR_STATE_LEARNING:
3571 case BR_STATE_FORWARDING:
3572 want[ROCKER_CTRL_LINK_LOCAL_MCAST] = true;
3573 want[ROCKER_CTRL_IPV4_MCAST] = true;
3574 want[ROCKER_CTRL_IPV6_MCAST] = true;
3575 if (rocker_port_is_bridged(rocker_port))
3576 want[ROCKER_CTRL_DFLT_BRIDGING] = true;
3578 want[ROCKER_CTRL_LOCAL_ARP] = true;
3582 for (i = 0; i < ROCKER_CTRL_MAX; i++) {
3583 if (want[i] != rocker_port->ctrls[i]) {
3584 flags = ROCKER_OP_FLAG_NOWAIT |
3585 (want[i] ? 0 : ROCKER_OP_FLAG_REMOVE);
3586 err = rocker_port_ctrl(rocker_port, flags,
3590 rocker_port->ctrls[i] = want[i];
3594 err = rocker_port_fdb_flush(rocker_port);
3598 return rocker_port_fwding(rocker_port);
3601 static int rocker_port_fwd_enable(struct rocker_port *rocker_port)
3603 if (rocker_port_is_bridged(rocker_port))
3604 /* bridge STP will enable port */
3607 /* port is not bridged, so simulate going to FORWARDING state */
3608 return rocker_port_stp_update(rocker_port, BR_STATE_FORWARDING);
3611 static int rocker_port_fwd_disable(struct rocker_port *rocker_port)
3613 if (rocker_port_is_bridged(rocker_port))
3614 /* bridge STP will disable port */
3617 /* port is not bridged, so simulate going to DISABLED state */
3618 return rocker_port_stp_update(rocker_port, BR_STATE_DISABLED);
3621 static struct rocker_internal_vlan_tbl_entry *
3622 rocker_internal_vlan_tbl_find(struct rocker *rocker, int ifindex)
3624 struct rocker_internal_vlan_tbl_entry *found;
3626 hash_for_each_possible(rocker->internal_vlan_tbl, found,
3628 if (found->ifindex == ifindex)
3635 static __be16 rocker_port_internal_vlan_id_get(struct rocker_port *rocker_port,
3638 struct rocker *rocker = rocker_port->rocker;
3639 struct rocker_internal_vlan_tbl_entry *entry;
3640 struct rocker_internal_vlan_tbl_entry *found;
3641 unsigned long lock_flags;
3644 entry = kzalloc(sizeof(*entry), GFP_KERNEL);
3648 entry->ifindex = ifindex;
3650 spin_lock_irqsave(&rocker->internal_vlan_tbl_lock, lock_flags);
3652 found = rocker_internal_vlan_tbl_find(rocker, ifindex);
3659 hash_add(rocker->internal_vlan_tbl, &found->entry, found->ifindex);
3661 for (i = 0; i < ROCKER_N_INTERNAL_VLANS; i++) {
3662 if (test_and_set_bit(i, rocker->internal_vlan_bitmap))
3664 found->vlan_id = htons(ROCKER_INTERNAL_VLAN_ID_BASE + i);
3668 netdev_err(rocker_port->dev, "Out of internal VLAN IDs\n");
3672 spin_unlock_irqrestore(&rocker->internal_vlan_tbl_lock, lock_flags);
3674 return found->vlan_id;
3677 static void rocker_port_internal_vlan_id_put(struct rocker_port *rocker_port,
3680 struct rocker *rocker = rocker_port->rocker;
3681 struct rocker_internal_vlan_tbl_entry *found;
3682 unsigned long lock_flags;
3685 spin_lock_irqsave(&rocker->internal_vlan_tbl_lock, lock_flags);
3687 found = rocker_internal_vlan_tbl_find(rocker, ifindex);
3689 netdev_err(rocker_port->dev,
3690 "ifindex (%d) not found in internal VLAN tbl\n",
3695 if (--found->ref_count <= 0) {
3696 bit = ntohs(found->vlan_id) - ROCKER_INTERNAL_VLAN_ID_BASE;
3697 clear_bit(bit, rocker->internal_vlan_bitmap);
3698 hash_del(&found->entry);
3703 spin_unlock_irqrestore(&rocker->internal_vlan_tbl_lock, lock_flags);
3706 static int rocker_port_fib_ipv4(struct rocker_port *rocker_port, __be32 dst,
3707 int dst_len, struct fib_info *fi, u32 tb_id,
3711 __be16 eth_type = htons(ETH_P_IP);
3712 __be32 dst_mask = inet_make_mask(dst_len);
3713 __be16 internal_vlan_id = rocker_port->internal_vlan_id;
3714 u32 priority = fi->fib_priority;
3715 enum rocker_of_dpa_table_id goto_tbl =
3716 ROCKER_OF_DPA_TABLE_ID_ACL_POLICY;
3723 /* XXX support ECMP */
3726 nh_on_port = (fi->fib_dev == rocker_port->dev);
3727 has_gw = !!nh->nh_gw;
3729 if (has_gw && nh_on_port) {
3730 err = rocker_port_ipv4_nh(rocker_port, flags,
3735 group_id = ROCKER_GROUP_L3_UNICAST(index);
3737 /* Send to CPU for processing */
3738 group_id = ROCKER_GROUP_L2_INTERFACE(internal_vlan_id, 0);
3741 err = rocker_flow_tbl_ucast4_routing(rocker_port, eth_type, dst,
3742 dst_mask, priority, goto_tbl,
3745 netdev_err(rocker_port->dev, "Error (%d) IPv4 route %pI4\n",
3755 static int rocker_port_open(struct net_device *dev)
3757 struct rocker_port *rocker_port = netdev_priv(dev);
3760 err = rocker_port_dma_rings_init(rocker_port);
3764 err = request_irq(rocker_msix_tx_vector(rocker_port),
3765 rocker_tx_irq_handler, 0,
3766 rocker_driver_name, rocker_port);
3768 netdev_err(rocker_port->dev, "cannot assign tx irq\n");
3769 goto err_request_tx_irq;
3772 err = request_irq(rocker_msix_rx_vector(rocker_port),
3773 rocker_rx_irq_handler, 0,
3774 rocker_driver_name, rocker_port);
3776 netdev_err(rocker_port->dev, "cannot assign rx irq\n");
3777 goto err_request_rx_irq;
3780 err = rocker_port_fwd_enable(rocker_port);
3782 goto err_fwd_enable;
3784 napi_enable(&rocker_port->napi_tx);
3785 napi_enable(&rocker_port->napi_rx);
3786 rocker_port_set_enable(rocker_port, true);
3787 netif_start_queue(dev);
3791 free_irq(rocker_msix_rx_vector(rocker_port), rocker_port);
3793 free_irq(rocker_msix_tx_vector(rocker_port), rocker_port);
3795 rocker_port_dma_rings_fini(rocker_port);
3799 static int rocker_port_stop(struct net_device *dev)
3801 struct rocker_port *rocker_port = netdev_priv(dev);
3803 netif_stop_queue(dev);
3804 rocker_port_set_enable(rocker_port, false);
3805 napi_disable(&rocker_port->napi_rx);
3806 napi_disable(&rocker_port->napi_tx);
3807 rocker_port_fwd_disable(rocker_port);
3808 free_irq(rocker_msix_rx_vector(rocker_port), rocker_port);
3809 free_irq(rocker_msix_tx_vector(rocker_port), rocker_port);
3810 rocker_port_dma_rings_fini(rocker_port);
3815 static void rocker_tx_desc_frags_unmap(struct rocker_port *rocker_port,
3816 struct rocker_desc_info *desc_info)
3818 struct rocker *rocker = rocker_port->rocker;
3819 struct pci_dev *pdev = rocker->pdev;
3820 struct rocker_tlv *attrs[ROCKER_TLV_TX_MAX + 1];
3821 struct rocker_tlv *attr;
3824 rocker_tlv_parse_desc(attrs, ROCKER_TLV_TX_MAX, desc_info);
3825 if (!attrs[ROCKER_TLV_TX_FRAGS])
3827 rocker_tlv_for_each_nested(attr, attrs[ROCKER_TLV_TX_FRAGS], rem) {
3828 struct rocker_tlv *frag_attrs[ROCKER_TLV_TX_FRAG_ATTR_MAX + 1];
3829 dma_addr_t dma_handle;
3832 if (rocker_tlv_type(attr) != ROCKER_TLV_TX_FRAG)
3834 rocker_tlv_parse_nested(frag_attrs, ROCKER_TLV_TX_FRAG_ATTR_MAX,
3836 if (!frag_attrs[ROCKER_TLV_TX_FRAG_ATTR_ADDR] ||
3837 !frag_attrs[ROCKER_TLV_TX_FRAG_ATTR_LEN])
3839 dma_handle = rocker_tlv_get_u64(frag_attrs[ROCKER_TLV_TX_FRAG_ATTR_ADDR]);
3840 len = rocker_tlv_get_u16(frag_attrs[ROCKER_TLV_TX_FRAG_ATTR_LEN]);
3841 pci_unmap_single(pdev, dma_handle, len, DMA_TO_DEVICE);
3845 static int rocker_tx_desc_frag_map_put(struct rocker_port *rocker_port,
3846 struct rocker_desc_info *desc_info,
3847 char *buf, size_t buf_len)
3849 struct rocker *rocker = rocker_port->rocker;
3850 struct pci_dev *pdev = rocker->pdev;
3851 dma_addr_t dma_handle;
3852 struct rocker_tlv *frag;
3854 dma_handle = pci_map_single(pdev, buf, buf_len, DMA_TO_DEVICE);
3855 if (unlikely(pci_dma_mapping_error(pdev, dma_handle))) {
3856 if (net_ratelimit())
3857 netdev_err(rocker_port->dev, "failed to dma map tx frag\n");
3860 frag = rocker_tlv_nest_start(desc_info, ROCKER_TLV_TX_FRAG);
3863 if (rocker_tlv_put_u64(desc_info, ROCKER_TLV_TX_FRAG_ATTR_ADDR,
3866 if (rocker_tlv_put_u16(desc_info, ROCKER_TLV_TX_FRAG_ATTR_LEN,
3869 rocker_tlv_nest_end(desc_info, frag);
3873 rocker_tlv_nest_cancel(desc_info, frag);
3875 pci_unmap_single(pdev, dma_handle, buf_len, DMA_TO_DEVICE);
3879 static netdev_tx_t rocker_port_xmit(struct sk_buff *skb, struct net_device *dev)
3881 struct rocker_port *rocker_port = netdev_priv(dev);
3882 struct rocker *rocker = rocker_port->rocker;
3883 struct rocker_desc_info *desc_info;
3884 struct rocker_tlv *frags;
3888 desc_info = rocker_desc_head_get(&rocker_port->tx_ring);
3889 if (unlikely(!desc_info)) {
3890 if (net_ratelimit())
3891 netdev_err(dev, "tx ring full when queue awake\n");
3892 return NETDEV_TX_BUSY;
3895 rocker_desc_cookie_ptr_set(desc_info, skb);
3897 frags = rocker_tlv_nest_start(desc_info, ROCKER_TLV_TX_FRAGS);
3900 err = rocker_tx_desc_frag_map_put(rocker_port, desc_info,
3901 skb->data, skb_headlen(skb));
3904 if (skb_shinfo(skb)->nr_frags > ROCKER_TX_FRAGS_MAX)
3907 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
3908 const skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
3910 err = rocker_tx_desc_frag_map_put(rocker_port, desc_info,
3911 skb_frag_address(frag),
3912 skb_frag_size(frag));
3916 rocker_tlv_nest_end(desc_info, frags);
3918 rocker_desc_gen_clear(desc_info);
3919 rocker_desc_head_set(rocker, &rocker_port->tx_ring, desc_info);
3921 desc_info = rocker_desc_head_get(&rocker_port->tx_ring);
3923 netif_stop_queue(dev);
3925 return NETDEV_TX_OK;
3928 rocker_tx_desc_frags_unmap(rocker_port, desc_info);
3930 rocker_tlv_nest_cancel(desc_info, frags);
3933 dev->stats.tx_dropped++;
3935 return NETDEV_TX_OK;
3938 static int rocker_port_set_mac_address(struct net_device *dev, void *p)
3940 struct sockaddr *addr = p;
3941 struct rocker_port *rocker_port = netdev_priv(dev);
3944 if (!is_valid_ether_addr(addr->sa_data))
3945 return -EADDRNOTAVAIL;
3947 err = rocker_cmd_set_port_settings_macaddr(rocker_port, addr->sa_data);
3950 memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
3954 static int rocker_port_vlan_rx_add_vid(struct net_device *dev,
3955 __be16 proto, u16 vid)
3957 struct rocker_port *rocker_port = netdev_priv(dev);
3960 err = rocker_port_vlan(rocker_port, 0, vid);
3964 return rocker_port_router_mac(rocker_port, 0, htons(vid));
3967 static int rocker_port_vlan_rx_kill_vid(struct net_device *dev,
3968 __be16 proto, u16 vid)
3970 struct rocker_port *rocker_port = netdev_priv(dev);
3973 err = rocker_port_router_mac(rocker_port, ROCKER_OP_FLAG_REMOVE,
3978 return rocker_port_vlan(rocker_port, ROCKER_OP_FLAG_REMOVE, vid);
3981 static int rocker_port_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
3982 struct net_device *dev,
3983 const unsigned char *addr, u16 vid,
3986 struct rocker_port *rocker_port = netdev_priv(dev);
3987 __be16 vlan_id = rocker_port_vid_to_vlan(rocker_port, vid, NULL);
3990 if (!rocker_port_is_bridged(rocker_port))
3993 return rocker_port_fdb(rocker_port, addr, vlan_id, flags);
3996 static int rocker_port_fdb_del(struct ndmsg *ndm, struct nlattr *tb[],
3997 struct net_device *dev,
3998 const unsigned char *addr, u16 vid)
4000 struct rocker_port *rocker_port = netdev_priv(dev);
4001 __be16 vlan_id = rocker_port_vid_to_vlan(rocker_port, vid, NULL);
4002 int flags = ROCKER_OP_FLAG_REMOVE;
4004 if (!rocker_port_is_bridged(rocker_port))
4007 return rocker_port_fdb(rocker_port, addr, vlan_id, flags);
4010 static int rocker_fdb_fill_info(struct sk_buff *skb,
4011 struct rocker_port *rocker_port,
4012 const unsigned char *addr, u16 vid,
4013 u32 portid, u32 seq, int type,
4016 struct nlmsghdr *nlh;
4019 nlh = nlmsg_put(skb, portid, seq, type, sizeof(*ndm), flags);
4023 ndm = nlmsg_data(nlh);
4024 ndm->ndm_family = AF_BRIDGE;
4027 ndm->ndm_flags = NTF_SELF;
4029 ndm->ndm_ifindex = rocker_port->dev->ifindex;
4030 ndm->ndm_state = NUD_REACHABLE;
4032 if (nla_put(skb, NDA_LLADDR, ETH_ALEN, addr))
4033 goto nla_put_failure;
4035 if (vid && nla_put_u16(skb, NDA_VLAN, vid))
4036 goto nla_put_failure;
4038 nlmsg_end(skb, nlh);
4042 nlmsg_cancel(skb, nlh);
4046 static int rocker_port_fdb_dump(struct sk_buff *skb,
4047 struct netlink_callback *cb,
4048 struct net_device *dev,
4049 struct net_device *filter_dev,
4052 struct rocker_port *rocker_port = netdev_priv(dev);
4053 struct rocker *rocker = rocker_port->rocker;
4054 struct rocker_fdb_tbl_entry *found;
4055 struct hlist_node *tmp;
4057 unsigned long lock_flags;
4058 const unsigned char *addr;
4062 spin_lock_irqsave(&rocker->fdb_tbl_lock, lock_flags);
4063 hash_for_each_safe(rocker->fdb_tbl, bkt, tmp, found, entry) {
4064 if (found->key.pport != rocker_port->pport)
4066 if (idx < cb->args[0])
4068 addr = found->key.addr;
4069 vid = rocker_port_vlan_to_vid(rocker_port, found->key.vlan_id);
4070 err = rocker_fdb_fill_info(skb, rocker_port, addr, vid,
4071 NETLINK_CB(cb->skb).portid,
4073 RTM_NEWNEIGH, NLM_F_MULTI);
4079 spin_unlock_irqrestore(&rocker->fdb_tbl_lock, lock_flags);
4083 static int rocker_port_bridge_setlink(struct net_device *dev,
4084 struct nlmsghdr *nlh, u16 flags)
4086 struct rocker_port *rocker_port = netdev_priv(dev);
4087 struct nlattr *protinfo;
4088 struct nlattr *attr;
4091 protinfo = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg),
4094 attr = nla_find_nested(protinfo, IFLA_BRPORT_LEARNING);
4096 if (nla_len(attr) < sizeof(u8))
4099 if (nla_get_u8(attr))
4100 rocker_port->brport_flags |= BR_LEARNING;
4102 rocker_port->brport_flags &= ~BR_LEARNING;
4103 err = rocker_port_set_learning(rocker_port);
4107 attr = nla_find_nested(protinfo, IFLA_BRPORT_LEARNING_SYNC);
4109 if (nla_len(attr) < sizeof(u8))
4112 if (nla_get_u8(attr))
4113 rocker_port->brport_flags |= BR_LEARNING_SYNC;
4115 rocker_port->brport_flags &= ~BR_LEARNING_SYNC;
4122 static int rocker_port_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq,
4123 struct net_device *dev,
4126 struct rocker_port *rocker_port = netdev_priv(dev);
4127 u16 mode = BRIDGE_MODE_UNDEF;
4128 u32 mask = BR_LEARNING | BR_LEARNING_SYNC;
4130 return ndo_dflt_bridge_getlink(skb, pid, seq, dev, mode,
4131 rocker_port->brport_flags, mask);
4134 static int rocker_port_switch_parent_id_get(struct net_device *dev,
4135 struct netdev_phys_item_id *psid)
4137 struct rocker_port *rocker_port = netdev_priv(dev);
4138 struct rocker *rocker = rocker_port->rocker;
4140 psid->id_len = sizeof(rocker->hw.id);
4141 memcpy(&psid->id, &rocker->hw.id, psid->id_len);
4145 static int rocker_port_switch_port_stp_update(struct net_device *dev, u8 state)
4147 struct rocker_port *rocker_port = netdev_priv(dev);
4149 return rocker_port_stp_update(rocker_port, state);
4152 static int rocker_port_switch_fib_ipv4_add(struct net_device *dev,
4153 __be32 dst, int dst_len,
4154 struct fib_info *fi,
4156 u32 nlflags, u32 tb_id)
4158 struct rocker_port *rocker_port = netdev_priv(dev);
4161 return rocker_port_fib_ipv4(rocker_port, dst, dst_len,
4165 static int rocker_port_switch_fib_ipv4_del(struct net_device *dev,
4166 __be32 dst, int dst_len,
4167 struct fib_info *fi,
4168 u8 tos, u8 type, u32 tb_id)
4170 struct rocker_port *rocker_port = netdev_priv(dev);
4171 int flags = ROCKER_OP_FLAG_REMOVE;
4173 return rocker_port_fib_ipv4(rocker_port, dst, dst_len,
4177 static const struct net_device_ops rocker_port_netdev_ops = {
4178 .ndo_open = rocker_port_open,
4179 .ndo_stop = rocker_port_stop,
4180 .ndo_start_xmit = rocker_port_xmit,
4181 .ndo_set_mac_address = rocker_port_set_mac_address,
4182 .ndo_vlan_rx_add_vid = rocker_port_vlan_rx_add_vid,
4183 .ndo_vlan_rx_kill_vid = rocker_port_vlan_rx_kill_vid,
4184 .ndo_fdb_add = rocker_port_fdb_add,
4185 .ndo_fdb_del = rocker_port_fdb_del,
4186 .ndo_fdb_dump = rocker_port_fdb_dump,
4187 .ndo_bridge_setlink = rocker_port_bridge_setlink,
4188 .ndo_bridge_getlink = rocker_port_bridge_getlink,
4189 .ndo_switch_parent_id_get = rocker_port_switch_parent_id_get,
4190 .ndo_switch_port_stp_update = rocker_port_switch_port_stp_update,
4191 .ndo_switch_fib_ipv4_add = rocker_port_switch_fib_ipv4_add,
4192 .ndo_switch_fib_ipv4_del = rocker_port_switch_fib_ipv4_del,
4195 /********************
4197 ********************/
4199 static int rocker_port_get_settings(struct net_device *dev,
4200 struct ethtool_cmd *ecmd)
4202 struct rocker_port *rocker_port = netdev_priv(dev);
4204 return rocker_cmd_get_port_settings_ethtool(rocker_port, ecmd);
4207 static int rocker_port_set_settings(struct net_device *dev,
4208 struct ethtool_cmd *ecmd)
4210 struct rocker_port *rocker_port = netdev_priv(dev);
4212 return rocker_cmd_set_port_settings_ethtool(rocker_port, ecmd);
4215 static void rocker_port_get_drvinfo(struct net_device *dev,
4216 struct ethtool_drvinfo *drvinfo)
4218 strlcpy(drvinfo->driver, rocker_driver_name, sizeof(drvinfo->driver));
4219 strlcpy(drvinfo->version, UTS_RELEASE, sizeof(drvinfo->version));
4222 static struct rocker_port_stats {
4223 char str[ETH_GSTRING_LEN];
4225 } rocker_port_stats[] = {
4226 { "rx_packets", ROCKER_TLV_CMD_PORT_STATS_RX_PKTS, },
4227 { "rx_bytes", ROCKER_TLV_CMD_PORT_STATS_RX_BYTES, },
4228 { "rx_dropped", ROCKER_TLV_CMD_PORT_STATS_RX_DROPPED, },
4229 { "rx_errors", ROCKER_TLV_CMD_PORT_STATS_RX_ERRORS, },
4231 { "tx_packets", ROCKER_TLV_CMD_PORT_STATS_TX_PKTS, },
4232 { "tx_bytes", ROCKER_TLV_CMD_PORT_STATS_TX_BYTES, },
4233 { "tx_dropped", ROCKER_TLV_CMD_PORT_STATS_TX_DROPPED, },
4234 { "tx_errors", ROCKER_TLV_CMD_PORT_STATS_TX_ERRORS, },
4237 #define ROCKER_PORT_STATS_LEN ARRAY_SIZE(rocker_port_stats)
4239 static void rocker_port_get_strings(struct net_device *netdev, u32 stringset,
4245 switch (stringset) {
4247 for (i = 0; i < ARRAY_SIZE(rocker_port_stats); i++) {
4248 memcpy(p, rocker_port_stats[i].str, ETH_GSTRING_LEN);
4249 p += ETH_GSTRING_LEN;
4256 rocker_cmd_get_port_stats_prep(struct rocker *rocker,
4257 struct rocker_port *rocker_port,
4258 struct rocker_desc_info *desc_info,
4261 struct rocker_tlv *cmd_stats;
4263 if (rocker_tlv_put_u16(desc_info, ROCKER_TLV_CMD_TYPE,
4264 ROCKER_TLV_CMD_TYPE_GET_PORT_STATS))
4267 cmd_stats = rocker_tlv_nest_start(desc_info, ROCKER_TLV_CMD_INFO);
4271 if (rocker_tlv_put_u32(desc_info, ROCKER_TLV_CMD_PORT_STATS_PPORT,
4272 rocker_port->pport))
4275 rocker_tlv_nest_end(desc_info, cmd_stats);
4281 rocker_cmd_get_port_stats_ethtool_proc(struct rocker *rocker,
4282 struct rocker_port *rocker_port,
4283 struct rocker_desc_info *desc_info,
4286 struct rocker_tlv *attrs[ROCKER_TLV_CMD_MAX + 1];
4287 struct rocker_tlv *stats_attrs[ROCKER_TLV_CMD_PORT_STATS_MAX + 1];
4288 struct rocker_tlv *pattr;
4293 rocker_tlv_parse_desc(attrs, ROCKER_TLV_CMD_MAX, desc_info);
4295 if (!attrs[ROCKER_TLV_CMD_INFO])
4298 rocker_tlv_parse_nested(stats_attrs, ROCKER_TLV_CMD_PORT_STATS_MAX,
4299 attrs[ROCKER_TLV_CMD_INFO]);
4301 if (!stats_attrs[ROCKER_TLV_CMD_PORT_STATS_PPORT])
4304 pport = rocker_tlv_get_u32(stats_attrs[ROCKER_TLV_CMD_PORT_STATS_PPORT]);
4305 if (pport != rocker_port->pport)
4308 for (i = 0; i < ARRAY_SIZE(rocker_port_stats); i++) {
4309 pattr = stats_attrs[rocker_port_stats[i].type];
4313 data[i] = rocker_tlv_get_u64(pattr);
4319 static int rocker_cmd_get_port_stats_ethtool(struct rocker_port *rocker_port,
4322 return rocker_cmd_exec(rocker_port->rocker, rocker_port,
4323 rocker_cmd_get_port_stats_prep, NULL,
4324 rocker_cmd_get_port_stats_ethtool_proc,
4328 static void rocker_port_get_stats(struct net_device *dev,
4329 struct ethtool_stats *stats, u64 *data)
4331 struct rocker_port *rocker_port = netdev_priv(dev);
4333 if (rocker_cmd_get_port_stats_ethtool(rocker_port, data) != 0) {
4336 for (i = 0; i < ARRAY_SIZE(rocker_port_stats); ++i)
4343 static int rocker_port_get_sset_count(struct net_device *netdev, int sset)
4347 return ROCKER_PORT_STATS_LEN;
4353 static const struct ethtool_ops rocker_port_ethtool_ops = {
4354 .get_settings = rocker_port_get_settings,
4355 .set_settings = rocker_port_set_settings,
4356 .get_drvinfo = rocker_port_get_drvinfo,
4357 .get_link = ethtool_op_get_link,
4358 .get_strings = rocker_port_get_strings,
4359 .get_ethtool_stats = rocker_port_get_stats,
4360 .get_sset_count = rocker_port_get_sset_count,
4367 static struct rocker_port *rocker_port_napi_tx_get(struct napi_struct *napi)
4369 return container_of(napi, struct rocker_port, napi_tx);
4372 static int rocker_port_poll_tx(struct napi_struct *napi, int budget)
4374 struct rocker_port *rocker_port = rocker_port_napi_tx_get(napi);
4375 struct rocker *rocker = rocker_port->rocker;
4376 struct rocker_desc_info *desc_info;
4380 /* Cleanup tx descriptors */
4381 while ((desc_info = rocker_desc_tail_get(&rocker_port->tx_ring))) {
4382 struct sk_buff *skb;
4384 err = rocker_desc_err(desc_info);
4385 if (err && net_ratelimit())
4386 netdev_err(rocker_port->dev, "tx desc received with err %d\n",
4388 rocker_tx_desc_frags_unmap(rocker_port, desc_info);
4390 skb = rocker_desc_cookie_ptr_get(desc_info);
4392 rocker_port->dev->stats.tx_packets++;
4393 rocker_port->dev->stats.tx_bytes += skb->len;
4395 rocker_port->dev->stats.tx_errors++;
4397 dev_kfree_skb_any(skb);
4401 if (credits && netif_queue_stopped(rocker_port->dev))
4402 netif_wake_queue(rocker_port->dev);
4404 napi_complete(napi);
4405 rocker_dma_ring_credits_set(rocker, &rocker_port->tx_ring, credits);
4410 static int rocker_port_rx_proc(struct rocker *rocker,
4411 struct rocker_port *rocker_port,
4412 struct rocker_desc_info *desc_info)
4414 struct rocker_tlv *attrs[ROCKER_TLV_RX_MAX + 1];
4415 struct sk_buff *skb = rocker_desc_cookie_ptr_get(desc_info);
4421 rocker_tlv_parse_desc(attrs, ROCKER_TLV_RX_MAX, desc_info);
4422 if (!attrs[ROCKER_TLV_RX_FRAG_LEN])
4425 rocker_dma_rx_ring_skb_unmap(rocker, attrs);
4427 rx_len = rocker_tlv_get_u16(attrs[ROCKER_TLV_RX_FRAG_LEN]);
4428 skb_put(skb, rx_len);
4429 skb->protocol = eth_type_trans(skb, rocker_port->dev);
4431 rocker_port->dev->stats.rx_packets++;
4432 rocker_port->dev->stats.rx_bytes += skb->len;
4434 netif_receive_skb(skb);
4436 return rocker_dma_rx_ring_skb_alloc(rocker, rocker_port, desc_info);
4439 static struct rocker_port *rocker_port_napi_rx_get(struct napi_struct *napi)
4441 return container_of(napi, struct rocker_port, napi_rx);
4444 static int rocker_port_poll_rx(struct napi_struct *napi, int budget)
4446 struct rocker_port *rocker_port = rocker_port_napi_rx_get(napi);
4447 struct rocker *rocker = rocker_port->rocker;
4448 struct rocker_desc_info *desc_info;
4452 /* Process rx descriptors */
4453 while (credits < budget &&
4454 (desc_info = rocker_desc_tail_get(&rocker_port->rx_ring))) {
4455 err = rocker_desc_err(desc_info);
4457 if (net_ratelimit())
4458 netdev_err(rocker_port->dev, "rx desc received with err %d\n",
4461 err = rocker_port_rx_proc(rocker, rocker_port,
4463 if (err && net_ratelimit())
4464 netdev_err(rocker_port->dev, "rx processing failed with err %d\n",
4468 rocker_port->dev->stats.rx_errors++;
4470 rocker_desc_gen_clear(desc_info);
4471 rocker_desc_head_set(rocker, &rocker_port->rx_ring, desc_info);
4475 if (credits < budget)
4476 napi_complete(napi);
4478 rocker_dma_ring_credits_set(rocker, &rocker_port->rx_ring, credits);
4487 static void rocker_carrier_init(struct rocker_port *rocker_port)
4489 struct rocker *rocker = rocker_port->rocker;
4490 u64 link_status = rocker_read64(rocker, PORT_PHYS_LINK_STATUS);
4493 link_up = link_status & (1 << rocker_port->pport);
4495 netif_carrier_on(rocker_port->dev);
4497 netif_carrier_off(rocker_port->dev);
4500 static void rocker_remove_ports(struct rocker *rocker)
4502 struct rocker_port *rocker_port;
4505 for (i = 0; i < rocker->port_count; i++) {
4506 rocker_port = rocker->ports[i];
4507 rocker_port_ig_tbl(rocker_port, ROCKER_OP_FLAG_REMOVE);
4508 unregister_netdev(rocker_port->dev);
4510 kfree(rocker->ports);
4513 static void rocker_port_dev_addr_init(struct rocker *rocker,
4514 struct rocker_port *rocker_port)
4516 struct pci_dev *pdev = rocker->pdev;
4519 err = rocker_cmd_get_port_settings_macaddr(rocker_port,
4520 rocker_port->dev->dev_addr);
4522 dev_warn(&pdev->dev, "failed to get mac address, using random\n");
4523 eth_hw_addr_random(rocker_port->dev);
4527 static int rocker_probe_port(struct rocker *rocker, unsigned int port_number)
4529 struct pci_dev *pdev = rocker->pdev;
4530 struct rocker_port *rocker_port;
4531 struct net_device *dev;
4534 dev = alloc_etherdev(sizeof(struct rocker_port));
4537 rocker_port = netdev_priv(dev);
4538 rocker_port->dev = dev;
4539 rocker_port->rocker = rocker;
4540 rocker_port->port_number = port_number;
4541 rocker_port->pport = port_number + 1;
4542 rocker_port->brport_flags = BR_LEARNING | BR_LEARNING_SYNC;
4544 rocker_port_dev_addr_init(rocker, rocker_port);
4545 dev->netdev_ops = &rocker_port_netdev_ops;
4546 dev->ethtool_ops = &rocker_port_ethtool_ops;
4547 netif_napi_add(dev, &rocker_port->napi_tx, rocker_port_poll_tx,
4549 netif_napi_add(dev, &rocker_port->napi_rx, rocker_port_poll_rx,
4551 rocker_carrier_init(rocker_port);
4553 dev->features |= NETIF_F_NETNS_LOCAL |
4554 NETIF_F_HW_VLAN_CTAG_FILTER |
4555 NETIF_F_HW_SWITCH_OFFLOAD;
4557 err = register_netdev(dev);
4559 dev_err(&pdev->dev, "register_netdev failed\n");
4560 goto err_register_netdev;
4562 rocker->ports[port_number] = rocker_port;
4564 rocker_port_set_learning(rocker_port);
4566 rocker_port->internal_vlan_id =
4567 rocker_port_internal_vlan_id_get(rocker_port, dev->ifindex);
4568 err = rocker_port_ig_tbl(rocker_port, 0);
4570 dev_err(&pdev->dev, "install ig port table failed\n");
4571 goto err_port_ig_tbl;
4577 unregister_netdev(dev);
4578 err_register_netdev:
4583 static int rocker_probe_ports(struct rocker *rocker)
4589 alloc_size = sizeof(struct rocker_port *) * rocker->port_count;
4590 rocker->ports = kmalloc(alloc_size, GFP_KERNEL);
4593 for (i = 0; i < rocker->port_count; i++) {
4594 err = rocker_probe_port(rocker, i);
4601 rocker_remove_ports(rocker);
4605 static int rocker_msix_init(struct rocker *rocker)
4607 struct pci_dev *pdev = rocker->pdev;
4612 msix_entries = pci_msix_vec_count(pdev);
4613 if (msix_entries < 0)
4614 return msix_entries;
4616 if (msix_entries != ROCKER_MSIX_VEC_COUNT(rocker->port_count))
4619 rocker->msix_entries = kmalloc_array(msix_entries,
4620 sizeof(struct msix_entry),
4622 if (!rocker->msix_entries)
4625 for (i = 0; i < msix_entries; i++)
4626 rocker->msix_entries[i].entry = i;
4628 err = pci_enable_msix_exact(pdev, rocker->msix_entries, msix_entries);
4630 goto err_enable_msix;
4635 kfree(rocker->msix_entries);
4639 static void rocker_msix_fini(struct rocker *rocker)
4641 pci_disable_msix(rocker->pdev);
4642 kfree(rocker->msix_entries);
4645 static int rocker_probe(struct pci_dev *pdev, const struct pci_device_id *id)
4647 struct rocker *rocker;
4650 rocker = kzalloc(sizeof(*rocker), GFP_KERNEL);
4654 err = pci_enable_device(pdev);
4656 dev_err(&pdev->dev, "pci_enable_device failed\n");
4657 goto err_pci_enable_device;
4660 err = pci_request_regions(pdev, rocker_driver_name);
4662 dev_err(&pdev->dev, "pci_request_regions failed\n");
4663 goto err_pci_request_regions;
4666 err = pci_set_dma_mask(pdev, DMA_BIT_MASK(64));
4668 err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64));
4670 dev_err(&pdev->dev, "pci_set_consistent_dma_mask failed\n");
4671 goto err_pci_set_dma_mask;
4674 err = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
4676 dev_err(&pdev->dev, "pci_set_dma_mask failed\n");
4677 goto err_pci_set_dma_mask;
4681 if (pci_resource_len(pdev, 0) < ROCKER_PCI_BAR0_SIZE) {
4682 dev_err(&pdev->dev, "invalid PCI region size\n");
4683 goto err_pci_resource_len_check;
4686 rocker->hw_addr = ioremap(pci_resource_start(pdev, 0),
4687 pci_resource_len(pdev, 0));
4688 if (!rocker->hw_addr) {
4689 dev_err(&pdev->dev, "ioremap failed\n");
4693 pci_set_master(pdev);
4695 rocker->pdev = pdev;
4696 pci_set_drvdata(pdev, rocker);
4698 rocker->port_count = rocker_read32(rocker, PORT_PHYS_COUNT);
4700 err = rocker_msix_init(rocker);
4702 dev_err(&pdev->dev, "MSI-X init failed\n");
4706 err = rocker_basic_hw_test(rocker);
4708 dev_err(&pdev->dev, "basic hw test failed\n");
4709 goto err_basic_hw_test;
4712 rocker_write32(rocker, CONTROL, ROCKER_CONTROL_RESET);
4714 err = rocker_dma_rings_init(rocker);
4716 goto err_dma_rings_init;
4718 err = request_irq(rocker_msix_vector(rocker, ROCKER_MSIX_VEC_CMD),
4719 rocker_cmd_irq_handler, 0,
4720 rocker_driver_name, rocker);
4722 dev_err(&pdev->dev, "cannot assign cmd irq\n");
4723 goto err_request_cmd_irq;
4726 err = request_irq(rocker_msix_vector(rocker, ROCKER_MSIX_VEC_EVENT),
4727 rocker_event_irq_handler, 0,
4728 rocker_driver_name, rocker);
4730 dev_err(&pdev->dev, "cannot assign event irq\n");
4731 goto err_request_event_irq;
4734 rocker->hw.id = rocker_read64(rocker, SWITCH_ID);
4736 err = rocker_init_tbls(rocker);
4738 dev_err(&pdev->dev, "cannot init rocker tables\n");
4742 err = rocker_probe_ports(rocker);
4744 dev_err(&pdev->dev, "failed to probe ports\n");
4745 goto err_probe_ports;
4748 dev_info(&pdev->dev, "Rocker switch with id %016llx\n", rocker->hw.id);
4753 rocker_free_tbls(rocker);
4755 free_irq(rocker_msix_vector(rocker, ROCKER_MSIX_VEC_EVENT), rocker);
4756 err_request_event_irq:
4757 free_irq(rocker_msix_vector(rocker, ROCKER_MSIX_VEC_CMD), rocker);
4758 err_request_cmd_irq:
4759 rocker_dma_rings_fini(rocker);
4762 rocker_msix_fini(rocker);
4764 iounmap(rocker->hw_addr);
4766 err_pci_resource_len_check:
4767 err_pci_set_dma_mask:
4768 pci_release_regions(pdev);
4769 err_pci_request_regions:
4770 pci_disable_device(pdev);
4771 err_pci_enable_device:
4776 static void rocker_remove(struct pci_dev *pdev)
4778 struct rocker *rocker = pci_get_drvdata(pdev);
4780 rocker_free_tbls(rocker);
4781 rocker_write32(rocker, CONTROL, ROCKER_CONTROL_RESET);
4782 rocker_remove_ports(rocker);
4783 free_irq(rocker_msix_vector(rocker, ROCKER_MSIX_VEC_EVENT), rocker);
4784 free_irq(rocker_msix_vector(rocker, ROCKER_MSIX_VEC_CMD), rocker);
4785 rocker_dma_rings_fini(rocker);
4786 rocker_msix_fini(rocker);
4787 iounmap(rocker->hw_addr);
4788 pci_release_regions(rocker->pdev);
4789 pci_disable_device(rocker->pdev);
4793 static struct pci_driver rocker_pci_driver = {
4794 .name = rocker_driver_name,
4795 .id_table = rocker_pci_id_table,
4796 .probe = rocker_probe,
4797 .remove = rocker_remove,
4800 /************************************
4801 * Net device notifier event handler
4802 ************************************/
4804 static bool rocker_port_dev_check(struct net_device *dev)
4806 return dev->netdev_ops == &rocker_port_netdev_ops;
4809 static int rocker_port_bridge_join(struct rocker_port *rocker_port,
4810 struct net_device *bridge)
4814 rocker_port_internal_vlan_id_put(rocker_port,
4815 rocker_port->dev->ifindex);
4817 rocker_port->bridge_dev = bridge;
4819 /* Use bridge internal VLAN ID for untagged pkts */
4820 err = rocker_port_vlan(rocker_port, ROCKER_OP_FLAG_REMOVE, 0);
4823 rocker_port->internal_vlan_id =
4824 rocker_port_internal_vlan_id_get(rocker_port,
4826 return rocker_port_vlan(rocker_port, 0, 0);
4829 static int rocker_port_bridge_leave(struct rocker_port *rocker_port)
4833 rocker_port_internal_vlan_id_put(rocker_port,
4834 rocker_port->bridge_dev->ifindex);
4836 rocker_port->bridge_dev = NULL;
4838 /* Use port internal VLAN ID for untagged pkts */
4839 err = rocker_port_vlan(rocker_port, ROCKER_OP_FLAG_REMOVE, 0);
4842 rocker_port->internal_vlan_id =
4843 rocker_port_internal_vlan_id_get(rocker_port,
4844 rocker_port->dev->ifindex);
4845 err = rocker_port_vlan(rocker_port, 0, 0);
4849 if (rocker_port->dev->flags & IFF_UP)
4850 err = rocker_port_fwd_enable(rocker_port);
4855 static int rocker_port_master_changed(struct net_device *dev)
4857 struct rocker_port *rocker_port = netdev_priv(dev);
4858 struct net_device *master = netdev_master_upper_dev_get(dev);
4861 if (master && master->rtnl_link_ops &&
4862 !strcmp(master->rtnl_link_ops->kind, "bridge"))
4863 err = rocker_port_bridge_join(rocker_port, master);
4865 err = rocker_port_bridge_leave(rocker_port);
4870 static int rocker_netdevice_event(struct notifier_block *unused,
4871 unsigned long event, void *ptr)
4873 struct net_device *dev;
4877 case NETDEV_CHANGEUPPER:
4878 dev = netdev_notifier_info_to_dev(ptr);
4879 if (!rocker_port_dev_check(dev))
4881 err = rocker_port_master_changed(dev);
4884 "failed to reflect master change (err %d)\n",
4892 static struct notifier_block rocker_netdevice_nb __read_mostly = {
4893 .notifier_call = rocker_netdevice_event,
4896 /************************************
4897 * Net event notifier event handler
4898 ************************************/
4900 static int rocker_neigh_update(struct net_device *dev, struct neighbour *n)
4902 struct rocker_port *rocker_port = netdev_priv(dev);
4903 int flags = (n->nud_state & NUD_VALID) ? 0 : ROCKER_OP_FLAG_REMOVE;
4904 __be32 ip_addr = *(__be32 *)n->primary_key;
4906 return rocker_port_ipv4_neigh(rocker_port, flags, ip_addr, n->ha);
4909 static int rocker_netevent_event(struct notifier_block *unused,
4910 unsigned long event, void *ptr)
4912 struct net_device *dev;
4913 struct neighbour *n = ptr;
4917 case NETEVENT_NEIGH_UPDATE:
4918 if (n->tbl != &arp_tbl)
4921 if (!rocker_port_dev_check(dev))
4923 err = rocker_neigh_update(dev, n);
4926 "failed to handle neigh update (err %d)\n",
4934 static struct notifier_block rocker_netevent_nb __read_mostly = {
4935 .notifier_call = rocker_netevent_event,
4938 /***********************
4939 * Module init and exit
4940 ***********************/
4942 static int __init rocker_module_init(void)
4946 register_netdevice_notifier(&rocker_netdevice_nb);
4947 register_netevent_notifier(&rocker_netevent_nb);
4948 err = pci_register_driver(&rocker_pci_driver);
4950 goto err_pci_register_driver;
4953 err_pci_register_driver:
4954 unregister_netdevice_notifier(&rocker_netevent_nb);
4955 unregister_netdevice_notifier(&rocker_netdevice_nb);
4959 static void __exit rocker_module_exit(void)
4961 unregister_netevent_notifier(&rocker_netevent_nb);
4962 unregister_netdevice_notifier(&rocker_netdevice_nb);
4963 pci_unregister_driver(&rocker_pci_driver);
4966 module_init(rocker_module_init);
4967 module_exit(rocker_module_exit);
4969 MODULE_LICENSE("GPL v2");
4970 MODULE_AUTHOR("Jiri Pirko <jiri@resnulli.us>");
4971 MODULE_AUTHOR("Scott Feldman <sfeldma@gmail.com>");
4972 MODULE_DESCRIPTION("Rocker switch device driver");
4973 MODULE_DEVICE_TABLE(pci, rocker_pci_id_table);