blob: 7d5d92a10284f6e52ec74f440fe18b3bf062a074 [file] [log] [blame]
Jiri Pirko4b8ac962014-11-28 14:34:26 +01001/*
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>
5 *
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.
10 */
11
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>
Scott Feldman9f6bbf72014-11-28 14:34:27 +010019#include <linux/hashtable.h>
Jiri Pirko4b8ac962014-11-28 14:34:26 +010020#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>
Scott Feldman6c707942014-11-28 14:34:28 +010031#include <linux/if_bridge.h>
Scott Feldman9f6bbf72014-11-28 14:34:27 +010032#include <linux/bitops.h>
David Aherndb191702015-03-17 20:23:16 -060033#include <linux/ctype.h>
Jiri Pirko4b8ac962014-11-28 14:34:26 +010034#include <net/switchdev.h>
35#include <net/rtnetlink.h>
Scott Feldmanc1beeef2015-03-05 21:21:20 -080036#include <net/ip_fib.h>
37#include <net/netevent.h>
38#include <net/arp.h>
Jiri Pirko4b8ac962014-11-28 14:34:26 +010039#include <asm-generic/io-64-nonatomic-lo-hi.h>
40#include <generated/utsrelease.h>
41
42#include "rocker.h"
43
44static const char rocker_driver_name[] = "rocker";
45
46static const struct pci_device_id rocker_pci_id_table[] = {
47 {PCI_VDEVICE(REDHAT, PCI_DEVICE_ID_REDHAT_ROCKER), 0},
48 {0, }
49};
50
Scott Feldman9f6bbf72014-11-28 14:34:27 +010051struct rocker_flow_tbl_key {
52 u32 priority;
53 enum rocker_of_dpa_table_id tbl_id;
54 union {
55 struct {
Scott Feldman4a6bb6d2015-02-25 20:15:37 -080056 u32 in_pport;
57 u32 in_pport_mask;
Scott Feldman9f6bbf72014-11-28 14:34:27 +010058 enum rocker_of_dpa_table_id goto_tbl;
59 } ig_port;
60 struct {
Scott Feldman4a6bb6d2015-02-25 20:15:37 -080061 u32 in_pport;
Scott Feldman9f6bbf72014-11-28 14:34:27 +010062 __be16 vlan_id;
63 __be16 vlan_id_mask;
64 enum rocker_of_dpa_table_id goto_tbl;
65 bool untagged;
66 __be16 new_vlan_id;
67 } vlan;
68 struct {
Scott Feldman4a6bb6d2015-02-25 20:15:37 -080069 u32 in_pport;
70 u32 in_pport_mask;
Scott Feldman9f6bbf72014-11-28 14:34:27 +010071 __be16 eth_type;
72 u8 eth_dst[ETH_ALEN];
73 u8 eth_dst_mask[ETH_ALEN];
74 __be16 vlan_id;
75 __be16 vlan_id_mask;
76 enum rocker_of_dpa_table_id goto_tbl;
77 bool copy_to_cpu;
78 } term_mac;
79 struct {
80 __be16 eth_type;
81 __be32 dst4;
82 __be32 dst4_mask;
83 enum rocker_of_dpa_table_id goto_tbl;
84 u32 group_id;
85 } ucast_routing;
86 struct {
87 u8 eth_dst[ETH_ALEN];
88 u8 eth_dst_mask[ETH_ALEN];
89 int has_eth_dst;
90 int has_eth_dst_mask;
91 __be16 vlan_id;
92 u32 tunnel_id;
93 enum rocker_of_dpa_table_id goto_tbl;
94 u32 group_id;
95 bool copy_to_cpu;
96 } bridge;
97 struct {
Scott Feldman4a6bb6d2015-02-25 20:15:37 -080098 u32 in_pport;
99 u32 in_pport_mask;
Scott Feldman9f6bbf72014-11-28 14:34:27 +0100100 u8 eth_src[ETH_ALEN];
101 u8 eth_src_mask[ETH_ALEN];
102 u8 eth_dst[ETH_ALEN];
103 u8 eth_dst_mask[ETH_ALEN];
104 __be16 eth_type;
105 __be16 vlan_id;
106 __be16 vlan_id_mask;
107 u8 ip_proto;
108 u8 ip_proto_mask;
109 u8 ip_tos;
110 u8 ip_tos_mask;
111 u32 group_id;
112 } acl;
113 };
114};
115
116struct rocker_flow_tbl_entry {
117 struct hlist_node entry;
Scott Feldmanc1beeef2015-03-05 21:21:20 -0800118 u32 cmd;
Scott Feldman9f6bbf72014-11-28 14:34:27 +0100119 u64 cookie;
120 struct rocker_flow_tbl_key key;
Scott Feldmanc1beeef2015-03-05 21:21:20 -0800121 size_t key_len;
Scott Feldman9f6bbf72014-11-28 14:34:27 +0100122 u32 key_crc32; /* key */
123};
124
125struct rocker_group_tbl_entry {
126 struct hlist_node entry;
127 u32 cmd;
128 u32 group_id; /* key */
129 u16 group_count;
130 u32 *group_ids;
131 union {
132 struct {
133 u8 pop_vlan;
134 } l2_interface;
135 struct {
136 u8 eth_src[ETH_ALEN];
137 u8 eth_dst[ETH_ALEN];
138 __be16 vlan_id;
139 u32 group_id;
140 } l2_rewrite;
141 struct {
142 u8 eth_src[ETH_ALEN];
143 u8 eth_dst[ETH_ALEN];
144 __be16 vlan_id;
145 bool ttl_check;
146 u32 group_id;
147 } l3_unicast;
148 };
149};
150
151struct rocker_fdb_tbl_entry {
152 struct hlist_node entry;
153 u32 key_crc32; /* key */
154 bool learned;
155 struct rocker_fdb_tbl_key {
Scott Feldman4a6bb6d2015-02-25 20:15:37 -0800156 u32 pport;
Scott Feldman9f6bbf72014-11-28 14:34:27 +0100157 u8 addr[ETH_ALEN];
158 __be16 vlan_id;
159 } key;
160};
161
162struct rocker_internal_vlan_tbl_entry {
163 struct hlist_node entry;
164 int ifindex; /* key */
165 u32 ref_count;
166 __be16 vlan_id;
167};
168
Scott Feldmanc1beeef2015-03-05 21:21:20 -0800169struct rocker_neigh_tbl_entry {
170 struct hlist_node entry;
171 __be32 ip_addr; /* key */
172 struct net_device *dev;
173 u32 ref_count;
174 u32 index;
175 u8 eth_dst[ETH_ALEN];
176 bool ttl_check;
177};
178
Jiri Pirko4b8ac962014-11-28 14:34:26 +0100179struct rocker_desc_info {
180 char *data; /* mapped */
181 size_t data_size;
182 size_t tlv_size;
183 struct rocker_desc *desc;
Scott Feldman4725ceb2015-05-10 09:48:08 -0700184 dma_addr_t mapaddr;
Jiri Pirko4b8ac962014-11-28 14:34:26 +0100185};
186
187struct rocker_dma_ring_info {
188 size_t size;
189 u32 head;
190 u32 tail;
191 struct rocker_desc *desc; /* mapped */
192 dma_addr_t mapaddr;
193 struct rocker_desc_info *desc_info;
194 unsigned int type;
195};
196
197struct rocker;
198
Scott Feldman9f6bbf72014-11-28 14:34:27 +0100199enum {
200 ROCKER_CTRL_LINK_LOCAL_MCAST,
201 ROCKER_CTRL_LOCAL_ARP,
202 ROCKER_CTRL_IPV4_MCAST,
203 ROCKER_CTRL_IPV6_MCAST,
204 ROCKER_CTRL_DFLT_BRIDGING,
205 ROCKER_CTRL_MAX,
206};
207
208#define ROCKER_INTERNAL_VLAN_ID_BASE 0x0f00
209#define ROCKER_N_INTERNAL_VLANS 255
210#define ROCKER_VLAN_BITMAP_LEN BITS_TO_LONGS(VLAN_N_VID)
211#define ROCKER_INTERNAL_VLAN_BITMAP_LEN BITS_TO_LONGS(ROCKER_N_INTERNAL_VLANS)
212
Jiri Pirko4b8ac962014-11-28 14:34:26 +0100213struct rocker_port {
214 struct net_device *dev;
Scott Feldman6c707942014-11-28 14:34:28 +0100215 struct net_device *bridge_dev;
Jiri Pirko4b8ac962014-11-28 14:34:26 +0100216 struct rocker *rocker;
217 unsigned int port_number;
Scott Feldman4a6bb6d2015-02-25 20:15:37 -0800218 u32 pport;
Scott Feldman9f6bbf72014-11-28 14:34:27 +0100219 __be16 internal_vlan_id;
Scott Feldman6c707942014-11-28 14:34:28 +0100220 int stp_state;
Scott Feldman5111f802014-11-28 14:34:30 +0100221 u32 brport_flags;
Scott Feldman9f6bbf72014-11-28 14:34:27 +0100222 bool ctrls[ROCKER_CTRL_MAX];
223 unsigned long vlan_bitmap[ROCKER_VLAN_BITMAP_LEN];
Jiri Pirko4b8ac962014-11-28 14:34:26 +0100224 struct napi_struct napi_tx;
225 struct napi_struct napi_rx;
226 struct rocker_dma_ring_info tx_ring;
227 struct rocker_dma_ring_info rx_ring;
Scott Feldmanc4f20322015-05-10 09:47:50 -0700228 struct list_head trans_mem;
Jiri Pirko4b8ac962014-11-28 14:34:26 +0100229};
230
231struct rocker {
232 struct pci_dev *pdev;
233 u8 __iomem *hw_addr;
234 struct msix_entry *msix_entries;
235 unsigned int port_count;
236 struct rocker_port **ports;
237 struct {
238 u64 id;
239 } hw;
Scott Feldman4725ceb2015-05-10 09:48:08 -0700240 spinlock_t cmd_ring_lock; /* for cmd ring accesses */
Jiri Pirko4b8ac962014-11-28 14:34:26 +0100241 struct rocker_dma_ring_info cmd_ring;
242 struct rocker_dma_ring_info event_ring;
Scott Feldman9f6bbf72014-11-28 14:34:27 +0100243 DECLARE_HASHTABLE(flow_tbl, 16);
Scott Feldman4725ceb2015-05-10 09:48:08 -0700244 spinlock_t flow_tbl_lock; /* for flow tbl accesses */
Scott Feldman9f6bbf72014-11-28 14:34:27 +0100245 u64 flow_tbl_next_cookie;
246 DECLARE_HASHTABLE(group_tbl, 16);
Scott Feldman4725ceb2015-05-10 09:48:08 -0700247 spinlock_t group_tbl_lock; /* for group tbl accesses */
Scott Feldman9f6bbf72014-11-28 14:34:27 +0100248 DECLARE_HASHTABLE(fdb_tbl, 16);
Scott Feldman4725ceb2015-05-10 09:48:08 -0700249 spinlock_t fdb_tbl_lock; /* for fdb tbl accesses */
Scott Feldman9f6bbf72014-11-28 14:34:27 +0100250 unsigned long internal_vlan_bitmap[ROCKER_INTERNAL_VLAN_BITMAP_LEN];
251 DECLARE_HASHTABLE(internal_vlan_tbl, 8);
Scott Feldman4725ceb2015-05-10 09:48:08 -0700252 spinlock_t internal_vlan_tbl_lock; /* for vlan tbl accesses */
Scott Feldmanc1beeef2015-03-05 21:21:20 -0800253 DECLARE_HASHTABLE(neigh_tbl, 16);
Scott Feldman4725ceb2015-05-10 09:48:08 -0700254 spinlock_t neigh_tbl_lock; /* for neigh tbl accesses */
Scott Feldmanc1beeef2015-03-05 21:21:20 -0800255 u32 neigh_tbl_next_index;
Jiri Pirko4b8ac962014-11-28 14:34:26 +0100256};
257
Scott Feldman9f6bbf72014-11-28 14:34:27 +0100258static const u8 zero_mac[ETH_ALEN] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
259static const u8 ff_mac[ETH_ALEN] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
260static const u8 ll_mac[ETH_ALEN] = { 0x01, 0x80, 0xc2, 0x00, 0x00, 0x00 };
261static const u8 ll_mask[ETH_ALEN] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xf0 };
262static const u8 mcast_mac[ETH_ALEN] = { 0x01, 0x00, 0x00, 0x00, 0x00, 0x00 };
263static const u8 ipv4_mcast[ETH_ALEN] = { 0x01, 0x00, 0x5e, 0x00, 0x00, 0x00 };
264static const u8 ipv4_mask[ETH_ALEN] = { 0xff, 0xff, 0xff, 0x80, 0x00, 0x00 };
265static const u8 ipv6_mcast[ETH_ALEN] = { 0x33, 0x33, 0x00, 0x00, 0x00, 0x00 };
266static const u8 ipv6_mask[ETH_ALEN] = { 0xff, 0xff, 0x00, 0x00, 0x00, 0x00 };
267
268/* Rocker priority levels for flow table entries. Higher
269 * priority match takes precedence over lower priority match.
270 */
271
272enum {
273 ROCKER_PRIORITY_UNKNOWN = 0,
274 ROCKER_PRIORITY_IG_PORT = 1,
275 ROCKER_PRIORITY_VLAN = 1,
276 ROCKER_PRIORITY_TERM_MAC_UCAST = 0,
277 ROCKER_PRIORITY_TERM_MAC_MCAST = 1,
Scott Feldman9f6bbf72014-11-28 14:34:27 +0100278 ROCKER_PRIORITY_BRIDGING_VLAN_DFLT_EXACT = 1,
279 ROCKER_PRIORITY_BRIDGING_VLAN_DFLT_WILD = 2,
280 ROCKER_PRIORITY_BRIDGING_VLAN = 3,
281 ROCKER_PRIORITY_BRIDGING_TENANT_DFLT_EXACT = 1,
282 ROCKER_PRIORITY_BRIDGING_TENANT_DFLT_WILD = 2,
283 ROCKER_PRIORITY_BRIDGING_TENANT = 3,
284 ROCKER_PRIORITY_ACL_CTRL = 3,
285 ROCKER_PRIORITY_ACL_NORMAL = 2,
286 ROCKER_PRIORITY_ACL_DFLT = 1,
287};
288
289static bool rocker_vlan_id_is_internal(__be16 vlan_id)
290{
291 u16 start = ROCKER_INTERNAL_VLAN_ID_BASE;
292 u16 end = 0xffe;
293 u16 _vlan_id = ntohs(vlan_id);
294
295 return (_vlan_id >= start && _vlan_id <= end);
296}
297
Simon Hormane5054642015-05-25 14:28:36 +0900298static __be16 rocker_port_vid_to_vlan(const struct rocker_port *rocker_port,
Scott Feldman9f6bbf72014-11-28 14:34:27 +0100299 u16 vid, bool *pop_vlan)
300{
301 __be16 vlan_id;
302
303 if (pop_vlan)
304 *pop_vlan = false;
305 vlan_id = htons(vid);
306 if (!vlan_id) {
307 vlan_id = rocker_port->internal_vlan_id;
308 if (pop_vlan)
309 *pop_vlan = true;
310 }
311
312 return vlan_id;
313}
314
Simon Hormane5054642015-05-25 14:28:36 +0900315static u16 rocker_port_vlan_to_vid(const struct rocker_port *rocker_port,
Scott Feldman6c707942014-11-28 14:34:28 +0100316 __be16 vlan_id)
317{
318 if (rocker_vlan_id_is_internal(vlan_id))
319 return 0;
320
321 return ntohs(vlan_id);
322}
323
Simon Hormane5054642015-05-25 14:28:36 +0900324static bool rocker_port_is_bridged(const struct rocker_port *rocker_port)
Scott Feldman6c707942014-11-28 14:34:28 +0100325{
326 return !!rocker_port->bridge_dev;
327}
328
Scott Feldman179f9a22015-06-12 21:35:46 -0700329#define ROCKER_OP_FLAG_REMOVE BIT(0)
330#define ROCKER_OP_FLAG_NOWAIT BIT(1)
331#define ROCKER_OP_FLAG_LEARNED BIT(2)
332#define ROCKER_OP_FLAG_REFRESH BIT(3)
333
Scott Feldmanc4f20322015-05-10 09:47:50 -0700334static void *__rocker_port_mem_alloc(struct rocker_port *rocker_port,
Scott Feldman179f9a22015-06-12 21:35:46 -0700335 enum switchdev_trans trans, int flags,
336 size_t size)
Scott Feldmanc4f20322015-05-10 09:47:50 -0700337{
338 struct list_head *elem = NULL;
Scott Feldman179f9a22015-06-12 21:35:46 -0700339 gfp_t gfp_flags = (flags & ROCKER_OP_FLAG_NOWAIT) ?
340 GFP_ATOMIC : GFP_KERNEL;
Scott Feldmanc4f20322015-05-10 09:47:50 -0700341
342 /* If in transaction prepare phase, allocate the memory
343 * and enqueue it on a per-port list. If in transaction
344 * commit phase, dequeue the memory from the per-port list
345 * rather than re-allocating the memory. The idea is the
346 * driver code paths for prepare and commit are identical
347 * so the memory allocated in the prepare phase is the
348 * memory used in the commit phase.
349 */
350
351 switch (trans) {
352 case SWITCHDEV_TRANS_PREPARE:
Scott Feldman179f9a22015-06-12 21:35:46 -0700353 elem = kzalloc(size + sizeof(*elem), gfp_flags);
Scott Feldmanc4f20322015-05-10 09:47:50 -0700354 if (!elem)
355 return NULL;
356 list_add_tail(elem, &rocker_port->trans_mem);
357 break;
358 case SWITCHDEV_TRANS_COMMIT:
359 BUG_ON(list_empty(&rocker_port->trans_mem));
360 elem = rocker_port->trans_mem.next;
361 list_del_init(elem);
362 break;
363 case SWITCHDEV_TRANS_NONE:
Scott Feldman179f9a22015-06-12 21:35:46 -0700364 elem = kzalloc(size + sizeof(*elem), gfp_flags);
Scott Feldmanc4f20322015-05-10 09:47:50 -0700365 if (elem)
366 INIT_LIST_HEAD(elem);
367 break;
368 default:
369 break;
370 }
371
372 return elem ? elem + 1 : NULL;
373}
374
375static void *rocker_port_kzalloc(struct rocker_port *rocker_port,
Scott Feldman179f9a22015-06-12 21:35:46 -0700376 enum switchdev_trans trans, int flags,
377 size_t size)
Scott Feldmanc4f20322015-05-10 09:47:50 -0700378{
Scott Feldman179f9a22015-06-12 21:35:46 -0700379 return __rocker_port_mem_alloc(rocker_port, trans, flags, size);
Scott Feldmanc4f20322015-05-10 09:47:50 -0700380}
381
382static void *rocker_port_kcalloc(struct rocker_port *rocker_port,
Scott Feldman179f9a22015-06-12 21:35:46 -0700383 enum switchdev_trans trans, int flags,
384 size_t n, size_t size)
Scott Feldmanc4f20322015-05-10 09:47:50 -0700385{
Scott Feldman179f9a22015-06-12 21:35:46 -0700386 return __rocker_port_mem_alloc(rocker_port, trans, flags, n * size);
Scott Feldmanc4f20322015-05-10 09:47:50 -0700387}
388
Simon Horman0985df72015-05-25 14:28:35 +0900389static void rocker_port_kfree(enum switchdev_trans trans, const void *mem)
Scott Feldmanc4f20322015-05-10 09:47:50 -0700390{
391 struct list_head *elem;
392
393 /* Frees are ignored if in transaction prepare phase. The
394 * memory remains on the per-port list until freed in the
395 * commit phase.
396 */
397
398 if (trans == SWITCHDEV_TRANS_PREPARE)
399 return;
400
401 elem = (struct list_head *)mem - 1;
402 BUG_ON(!list_empty(elem));
403 kfree(elem);
404}
405
Jiri Pirko4b8ac962014-11-28 14:34:26 +0100406struct rocker_wait {
407 wait_queue_head_t wait;
408 bool done;
Scott Feldman179f9a22015-06-12 21:35:46 -0700409 bool nowait;
Jiri Pirko4b8ac962014-11-28 14:34:26 +0100410};
411
412static void rocker_wait_reset(struct rocker_wait *wait)
413{
414 wait->done = false;
Scott Feldman179f9a22015-06-12 21:35:46 -0700415 wait->nowait = false;
Jiri Pirko4b8ac962014-11-28 14:34:26 +0100416}
417
418static void rocker_wait_init(struct rocker_wait *wait)
419{
420 init_waitqueue_head(&wait->wait);
421 rocker_wait_reset(wait);
422}
423
Scott Feldmanc4f20322015-05-10 09:47:50 -0700424static struct rocker_wait *rocker_wait_create(struct rocker_port *rocker_port,
Scott Feldman179f9a22015-06-12 21:35:46 -0700425 enum switchdev_trans trans,
426 int flags)
Jiri Pirko4b8ac962014-11-28 14:34:26 +0100427{
428 struct rocker_wait *wait;
429
Scott Feldman179f9a22015-06-12 21:35:46 -0700430 wait = rocker_port_kzalloc(rocker_port, trans, flags, sizeof(*wait));
Jiri Pirko4b8ac962014-11-28 14:34:26 +0100431 if (!wait)
432 return NULL;
433 rocker_wait_init(wait);
434 return wait;
435}
436
Simon Horman0985df72015-05-25 14:28:35 +0900437static void rocker_wait_destroy(enum switchdev_trans trans,
Scott Feldmanc4f20322015-05-10 09:47:50 -0700438 struct rocker_wait *wait)
Jiri Pirko4b8ac962014-11-28 14:34:26 +0100439{
Simon Horman0985df72015-05-25 14:28:35 +0900440 rocker_port_kfree(trans, wait);
Jiri Pirko4b8ac962014-11-28 14:34:26 +0100441}
442
443static bool rocker_wait_event_timeout(struct rocker_wait *wait,
444 unsigned long timeout)
445{
446 wait_event_timeout(wait->wait, wait->done, HZ / 10);
447 if (!wait->done)
448 return false;
449 return true;
450}
451
452static void rocker_wait_wake_up(struct rocker_wait *wait)
453{
454 wait->done = true;
455 wake_up(&wait->wait);
456}
457
Simon Hormane5054642015-05-25 14:28:36 +0900458static u32 rocker_msix_vector(const struct rocker *rocker, unsigned int vector)
Jiri Pirko4b8ac962014-11-28 14:34:26 +0100459{
460 return rocker->msix_entries[vector].vector;
461}
462
Simon Hormane5054642015-05-25 14:28:36 +0900463static u32 rocker_msix_tx_vector(const struct rocker_port *rocker_port)
Jiri Pirko4b8ac962014-11-28 14:34:26 +0100464{
465 return rocker_msix_vector(rocker_port->rocker,
466 ROCKER_MSIX_VEC_TX(rocker_port->port_number));
467}
468
Simon Hormane5054642015-05-25 14:28:36 +0900469static u32 rocker_msix_rx_vector(const struct rocker_port *rocker_port)
Jiri Pirko4b8ac962014-11-28 14:34:26 +0100470{
471 return rocker_msix_vector(rocker_port->rocker,
472 ROCKER_MSIX_VEC_RX(rocker_port->port_number));
473}
474
475#define rocker_write32(rocker, reg, val) \
476 writel((val), (rocker)->hw_addr + (ROCKER_ ## reg))
477#define rocker_read32(rocker, reg) \
478 readl((rocker)->hw_addr + (ROCKER_ ## reg))
479#define rocker_write64(rocker, reg, val) \
480 writeq((val), (rocker)->hw_addr + (ROCKER_ ## reg))
481#define rocker_read64(rocker, reg) \
482 readq((rocker)->hw_addr + (ROCKER_ ## reg))
483
484/*****************************
485 * HW basic testing functions
486 *****************************/
487
Simon Hormane5054642015-05-25 14:28:36 +0900488static int rocker_reg_test(const struct rocker *rocker)
Jiri Pirko4b8ac962014-11-28 14:34:26 +0100489{
Simon Hormane5054642015-05-25 14:28:36 +0900490 const struct pci_dev *pdev = rocker->pdev;
Jiri Pirko4b8ac962014-11-28 14:34:26 +0100491 u64 test_reg;
492 u64 rnd;
493
494 rnd = prandom_u32();
495 rnd >>= 1;
496 rocker_write32(rocker, TEST_REG, rnd);
497 test_reg = rocker_read32(rocker, TEST_REG);
498 if (test_reg != rnd * 2) {
499 dev_err(&pdev->dev, "unexpected 32bit register value %08llx, expected %08llx\n",
500 test_reg, rnd * 2);
501 return -EIO;
502 }
503
504 rnd = prandom_u32();
505 rnd <<= 31;
506 rnd |= prandom_u32();
507 rocker_write64(rocker, TEST_REG64, rnd);
508 test_reg = rocker_read64(rocker, TEST_REG64);
509 if (test_reg != rnd * 2) {
510 dev_err(&pdev->dev, "unexpected 64bit register value %16llx, expected %16llx\n",
511 test_reg, rnd * 2);
512 return -EIO;
513 }
514
515 return 0;
516}
517
Simon Hormane5054642015-05-25 14:28:36 +0900518static int rocker_dma_test_one(const struct rocker *rocker,
519 struct rocker_wait *wait, u32 test_type,
520 dma_addr_t dma_handle, const unsigned char *buf,
521 const unsigned char *expect, size_t size)
Jiri Pirko4b8ac962014-11-28 14:34:26 +0100522{
Simon Hormane5054642015-05-25 14:28:36 +0900523 const struct pci_dev *pdev = rocker->pdev;
Jiri Pirko4b8ac962014-11-28 14:34:26 +0100524 int i;
525
526 rocker_wait_reset(wait);
527 rocker_write32(rocker, TEST_DMA_CTRL, test_type);
528
529 if (!rocker_wait_event_timeout(wait, HZ / 10)) {
530 dev_err(&pdev->dev, "no interrupt received within a timeout\n");
531 return -EIO;
532 }
533
534 for (i = 0; i < size; i++) {
535 if (buf[i] != expect[i]) {
536 dev_err(&pdev->dev, "unexpected memory content %02x at byte %x\n, %02x expected",
537 buf[i], i, expect[i]);
538 return -EIO;
539 }
540 }
541 return 0;
542}
543
544#define ROCKER_TEST_DMA_BUF_SIZE (PAGE_SIZE * 4)
545#define ROCKER_TEST_DMA_FILL_PATTERN 0x96
546
Simon Hormane5054642015-05-25 14:28:36 +0900547static int rocker_dma_test_offset(const struct rocker *rocker,
Jiri Pirko4b8ac962014-11-28 14:34:26 +0100548 struct rocker_wait *wait, int offset)
549{
550 struct pci_dev *pdev = rocker->pdev;
551 unsigned char *alloc;
552 unsigned char *buf;
553 unsigned char *expect;
554 dma_addr_t dma_handle;
555 int i;
556 int err;
557
558 alloc = kzalloc(ROCKER_TEST_DMA_BUF_SIZE * 2 + offset,
559 GFP_KERNEL | GFP_DMA);
560 if (!alloc)
561 return -ENOMEM;
562 buf = alloc + offset;
563 expect = buf + ROCKER_TEST_DMA_BUF_SIZE;
564
565 dma_handle = pci_map_single(pdev, buf, ROCKER_TEST_DMA_BUF_SIZE,
566 PCI_DMA_BIDIRECTIONAL);
567 if (pci_dma_mapping_error(pdev, dma_handle)) {
568 err = -EIO;
569 goto free_alloc;
570 }
571
572 rocker_write64(rocker, TEST_DMA_ADDR, dma_handle);
573 rocker_write32(rocker, TEST_DMA_SIZE, ROCKER_TEST_DMA_BUF_SIZE);
574
575 memset(expect, ROCKER_TEST_DMA_FILL_PATTERN, ROCKER_TEST_DMA_BUF_SIZE);
576 err = rocker_dma_test_one(rocker, wait, ROCKER_TEST_DMA_CTRL_FILL,
577 dma_handle, buf, expect,
578 ROCKER_TEST_DMA_BUF_SIZE);
579 if (err)
580 goto unmap;
581
582 memset(expect, 0, ROCKER_TEST_DMA_BUF_SIZE);
583 err = rocker_dma_test_one(rocker, wait, ROCKER_TEST_DMA_CTRL_CLEAR,
584 dma_handle, buf, expect,
585 ROCKER_TEST_DMA_BUF_SIZE);
586 if (err)
587 goto unmap;
588
589 prandom_bytes(buf, ROCKER_TEST_DMA_BUF_SIZE);
590 for (i = 0; i < ROCKER_TEST_DMA_BUF_SIZE; i++)
591 expect[i] = ~buf[i];
592 err = rocker_dma_test_one(rocker, wait, ROCKER_TEST_DMA_CTRL_INVERT,
593 dma_handle, buf, expect,
594 ROCKER_TEST_DMA_BUF_SIZE);
595 if (err)
596 goto unmap;
597
598unmap:
599 pci_unmap_single(pdev, dma_handle, ROCKER_TEST_DMA_BUF_SIZE,
600 PCI_DMA_BIDIRECTIONAL);
601free_alloc:
602 kfree(alloc);
603
604 return err;
605}
606
Simon Hormane5054642015-05-25 14:28:36 +0900607static int rocker_dma_test(const struct rocker *rocker,
608 struct rocker_wait *wait)
Jiri Pirko4b8ac962014-11-28 14:34:26 +0100609{
610 int i;
611 int err;
612
613 for (i = 0; i < 8; i++) {
614 err = rocker_dma_test_offset(rocker, wait, i);
615 if (err)
616 return err;
617 }
618 return 0;
619}
620
621static irqreturn_t rocker_test_irq_handler(int irq, void *dev_id)
622{
623 struct rocker_wait *wait = dev_id;
624
625 rocker_wait_wake_up(wait);
626
627 return IRQ_HANDLED;
628}
629
Simon Hormane5054642015-05-25 14:28:36 +0900630static int rocker_basic_hw_test(const struct rocker *rocker)
Jiri Pirko4b8ac962014-11-28 14:34:26 +0100631{
Simon Hormane5054642015-05-25 14:28:36 +0900632 const struct pci_dev *pdev = rocker->pdev;
Jiri Pirko4b8ac962014-11-28 14:34:26 +0100633 struct rocker_wait wait;
634 int err;
635
636 err = rocker_reg_test(rocker);
637 if (err) {
638 dev_err(&pdev->dev, "reg test failed\n");
639 return err;
640 }
641
642 err = request_irq(rocker_msix_vector(rocker, ROCKER_MSIX_VEC_TEST),
643 rocker_test_irq_handler, 0,
644 rocker_driver_name, &wait);
645 if (err) {
646 dev_err(&pdev->dev, "cannot assign test irq\n");
647 return err;
648 }
649
650 rocker_wait_init(&wait);
651 rocker_write32(rocker, TEST_IRQ, ROCKER_MSIX_VEC_TEST);
652
653 if (!rocker_wait_event_timeout(&wait, HZ / 10)) {
654 dev_err(&pdev->dev, "no interrupt received within a timeout\n");
655 err = -EIO;
656 goto free_irq;
657 }
658
659 err = rocker_dma_test(rocker, &wait);
660 if (err)
661 dev_err(&pdev->dev, "dma test failed\n");
662
663free_irq:
664 free_irq(rocker_msix_vector(rocker, ROCKER_MSIX_VEC_TEST), &wait);
665 return err;
666}
667
668/******
669 * TLV
670 ******/
671
672#define ROCKER_TLV_ALIGNTO 8U
673#define ROCKER_TLV_ALIGN(len) \
674 (((len) + ROCKER_TLV_ALIGNTO - 1) & ~(ROCKER_TLV_ALIGNTO - 1))
675#define ROCKER_TLV_HDRLEN ROCKER_TLV_ALIGN(sizeof(struct rocker_tlv))
676
677/* <------- ROCKER_TLV_HDRLEN -------> <--- ROCKER_TLV_ALIGN(payload) --->
678 * +-----------------------------+- - -+- - - - - - - - - - - - - - -+- - -+
679 * | Header | Pad | Payload | Pad |
680 * | (struct rocker_tlv) | ing | | ing |
681 * +-----------------------------+- - -+- - - - - - - - - - - - - - -+- - -+
682 * <--------------------------- tlv->len -------------------------->
683 */
684
685static struct rocker_tlv *rocker_tlv_next(const struct rocker_tlv *tlv,
686 int *remaining)
687{
688 int totlen = ROCKER_TLV_ALIGN(tlv->len);
689
690 *remaining -= totlen;
691 return (struct rocker_tlv *) ((char *) tlv + totlen);
692}
693
694static int rocker_tlv_ok(const struct rocker_tlv *tlv, int remaining)
695{
696 return remaining >= (int) ROCKER_TLV_HDRLEN &&
697 tlv->len >= ROCKER_TLV_HDRLEN &&
698 tlv->len <= remaining;
699}
700
701#define rocker_tlv_for_each(pos, head, len, rem) \
702 for (pos = head, rem = len; \
703 rocker_tlv_ok(pos, rem); \
704 pos = rocker_tlv_next(pos, &(rem)))
705
706#define rocker_tlv_for_each_nested(pos, tlv, rem) \
707 rocker_tlv_for_each(pos, rocker_tlv_data(tlv), \
708 rocker_tlv_len(tlv), rem)
709
710static int rocker_tlv_attr_size(int payload)
711{
712 return ROCKER_TLV_HDRLEN + payload;
713}
714
715static int rocker_tlv_total_size(int payload)
716{
717 return ROCKER_TLV_ALIGN(rocker_tlv_attr_size(payload));
718}
719
720static int rocker_tlv_padlen(int payload)
721{
722 return rocker_tlv_total_size(payload) - rocker_tlv_attr_size(payload);
723}
724
725static int rocker_tlv_type(const struct rocker_tlv *tlv)
726{
727 return tlv->type;
728}
729
730static void *rocker_tlv_data(const struct rocker_tlv *tlv)
731{
732 return (char *) tlv + ROCKER_TLV_HDRLEN;
733}
734
735static int rocker_tlv_len(const struct rocker_tlv *tlv)
736{
737 return tlv->len - ROCKER_TLV_HDRLEN;
738}
739
740static u8 rocker_tlv_get_u8(const struct rocker_tlv *tlv)
741{
742 return *(u8 *) rocker_tlv_data(tlv);
743}
744
745static u16 rocker_tlv_get_u16(const struct rocker_tlv *tlv)
746{
747 return *(u16 *) rocker_tlv_data(tlv);
748}
749
Jiri Pirko9b03c712014-12-03 14:14:53 +0100750static __be16 rocker_tlv_get_be16(const struct rocker_tlv *tlv)
751{
752 return *(__be16 *) rocker_tlv_data(tlv);
753}
754
Jiri Pirko4b8ac962014-11-28 14:34:26 +0100755static u32 rocker_tlv_get_u32(const struct rocker_tlv *tlv)
756{
757 return *(u32 *) rocker_tlv_data(tlv);
758}
759
760static u64 rocker_tlv_get_u64(const struct rocker_tlv *tlv)
761{
762 return *(u64 *) rocker_tlv_data(tlv);
763}
764
Simon Hormane5054642015-05-25 14:28:36 +0900765static void rocker_tlv_parse(const struct rocker_tlv **tb, int maxtype,
Jiri Pirko4b8ac962014-11-28 14:34:26 +0100766 const char *buf, int buf_len)
767{
768 const struct rocker_tlv *tlv;
769 const struct rocker_tlv *head = (const struct rocker_tlv *) buf;
770 int rem;
771
772 memset(tb, 0, sizeof(struct rocker_tlv *) * (maxtype + 1));
773
774 rocker_tlv_for_each(tlv, head, buf_len, rem) {
775 u32 type = rocker_tlv_type(tlv);
776
777 if (type > 0 && type <= maxtype)
Simon Hormane5054642015-05-25 14:28:36 +0900778 tb[type] = tlv;
Jiri Pirko4b8ac962014-11-28 14:34:26 +0100779 }
780}
781
Simon Hormane5054642015-05-25 14:28:36 +0900782static void rocker_tlv_parse_nested(const struct rocker_tlv **tb, int maxtype,
Jiri Pirko4b8ac962014-11-28 14:34:26 +0100783 const struct rocker_tlv *tlv)
784{
785 rocker_tlv_parse(tb, maxtype, rocker_tlv_data(tlv),
786 rocker_tlv_len(tlv));
787}
788
Simon Hormane5054642015-05-25 14:28:36 +0900789static void rocker_tlv_parse_desc(const struct rocker_tlv **tb, int maxtype,
790 const struct rocker_desc_info *desc_info)
Jiri Pirko4b8ac962014-11-28 14:34:26 +0100791{
792 rocker_tlv_parse(tb, maxtype, desc_info->data,
793 desc_info->desc->tlv_size);
794}
795
796static struct rocker_tlv *rocker_tlv_start(struct rocker_desc_info *desc_info)
797{
798 return (struct rocker_tlv *) ((char *) desc_info->data +
799 desc_info->tlv_size);
800}
801
802static int rocker_tlv_put(struct rocker_desc_info *desc_info,
803 int attrtype, int attrlen, const void *data)
804{
805 int tail_room = desc_info->data_size - desc_info->tlv_size;
806 int total_size = rocker_tlv_total_size(attrlen);
807 struct rocker_tlv *tlv;
808
809 if (unlikely(tail_room < total_size))
810 return -EMSGSIZE;
811
812 tlv = rocker_tlv_start(desc_info);
813 desc_info->tlv_size += total_size;
814 tlv->type = attrtype;
815 tlv->len = rocker_tlv_attr_size(attrlen);
816 memcpy(rocker_tlv_data(tlv), data, attrlen);
817 memset((char *) tlv + tlv->len, 0, rocker_tlv_padlen(attrlen));
818 return 0;
819}
820
821static int rocker_tlv_put_u8(struct rocker_desc_info *desc_info,
822 int attrtype, u8 value)
823{
824 return rocker_tlv_put(desc_info, attrtype, sizeof(u8), &value);
825}
826
827static int rocker_tlv_put_u16(struct rocker_desc_info *desc_info,
828 int attrtype, u16 value)
829{
830 return rocker_tlv_put(desc_info, attrtype, sizeof(u16), &value);
831}
832
Jiri Pirko9b03c712014-12-03 14:14:53 +0100833static int rocker_tlv_put_be16(struct rocker_desc_info *desc_info,
834 int attrtype, __be16 value)
835{
836 return rocker_tlv_put(desc_info, attrtype, sizeof(__be16), &value);
837}
838
Jiri Pirko4b8ac962014-11-28 14:34:26 +0100839static int rocker_tlv_put_u32(struct rocker_desc_info *desc_info,
840 int attrtype, u32 value)
841{
842 return rocker_tlv_put(desc_info, attrtype, sizeof(u32), &value);
843}
844
Jiri Pirko9b03c712014-12-03 14:14:53 +0100845static int rocker_tlv_put_be32(struct rocker_desc_info *desc_info,
846 int attrtype, __be32 value)
847{
848 return rocker_tlv_put(desc_info, attrtype, sizeof(__be32), &value);
849}
850
Jiri Pirko4b8ac962014-11-28 14:34:26 +0100851static int rocker_tlv_put_u64(struct rocker_desc_info *desc_info,
852 int attrtype, u64 value)
853{
854 return rocker_tlv_put(desc_info, attrtype, sizeof(u64), &value);
855}
856
857static struct rocker_tlv *
858rocker_tlv_nest_start(struct rocker_desc_info *desc_info, int attrtype)
859{
860 struct rocker_tlv *start = rocker_tlv_start(desc_info);
861
862 if (rocker_tlv_put(desc_info, attrtype, 0, NULL) < 0)
863 return NULL;
864
865 return start;
866}
867
868static void rocker_tlv_nest_end(struct rocker_desc_info *desc_info,
869 struct rocker_tlv *start)
870{
871 start->len = (char *) rocker_tlv_start(desc_info) - (char *) start;
872}
873
874static void rocker_tlv_nest_cancel(struct rocker_desc_info *desc_info,
Simon Hormane5054642015-05-25 14:28:36 +0900875 const struct rocker_tlv *start)
Jiri Pirko4b8ac962014-11-28 14:34:26 +0100876{
Simon Hormane5054642015-05-25 14:28:36 +0900877 desc_info->tlv_size = (const char *) start - desc_info->data;
Jiri Pirko4b8ac962014-11-28 14:34:26 +0100878}
879
880/******************************************
881 * DMA rings and descriptors manipulations
882 ******************************************/
883
884static u32 __pos_inc(u32 pos, size_t limit)
885{
886 return ++pos == limit ? 0 : pos;
887}
888
Simon Hormane5054642015-05-25 14:28:36 +0900889static int rocker_desc_err(const struct rocker_desc_info *desc_info)
Jiri Pirko4b8ac962014-11-28 14:34:26 +0100890{
Scott Feldman7eb344f2015-02-25 20:15:36 -0800891 int err = desc_info->desc->comp_err & ~ROCKER_DMA_DESC_COMP_ERR_GEN;
892
893 switch (err) {
894 case ROCKER_OK:
895 return 0;
896 case -ROCKER_ENOENT:
897 return -ENOENT;
898 case -ROCKER_ENXIO:
899 return -ENXIO;
900 case -ROCKER_ENOMEM:
901 return -ENOMEM;
902 case -ROCKER_EEXIST:
903 return -EEXIST;
904 case -ROCKER_EINVAL:
905 return -EINVAL;
906 case -ROCKER_EMSGSIZE:
907 return -EMSGSIZE;
908 case -ROCKER_ENOTSUP:
909 return -EOPNOTSUPP;
910 case -ROCKER_ENOBUFS:
911 return -ENOBUFS;
912 }
913
914 return -EINVAL;
Jiri Pirko4b8ac962014-11-28 14:34:26 +0100915}
916
Simon Hormane5054642015-05-25 14:28:36 +0900917static void rocker_desc_gen_clear(const struct rocker_desc_info *desc_info)
Jiri Pirko4b8ac962014-11-28 14:34:26 +0100918{
919 desc_info->desc->comp_err &= ~ROCKER_DMA_DESC_COMP_ERR_GEN;
920}
921
Simon Hormane5054642015-05-25 14:28:36 +0900922static bool rocker_desc_gen(const struct rocker_desc_info *desc_info)
Jiri Pirko4b8ac962014-11-28 14:34:26 +0100923{
924 u32 comp_err = desc_info->desc->comp_err;
925
926 return comp_err & ROCKER_DMA_DESC_COMP_ERR_GEN ? true : false;
927}
928
Simon Hormane5054642015-05-25 14:28:36 +0900929static void *rocker_desc_cookie_ptr_get(const struct rocker_desc_info *desc_info)
Jiri Pirko4b8ac962014-11-28 14:34:26 +0100930{
Arnd Bergmannadedf372015-01-13 15:23:52 +0100931 return (void *)(uintptr_t)desc_info->desc->cookie;
Jiri Pirko4b8ac962014-11-28 14:34:26 +0100932}
933
Simon Hormane5054642015-05-25 14:28:36 +0900934static void rocker_desc_cookie_ptr_set(const struct rocker_desc_info *desc_info,
Jiri Pirko4b8ac962014-11-28 14:34:26 +0100935 void *ptr)
936{
Arnd Bergmannadedf372015-01-13 15:23:52 +0100937 desc_info->desc->cookie = (uintptr_t) ptr;
Jiri Pirko4b8ac962014-11-28 14:34:26 +0100938}
939
940static struct rocker_desc_info *
Simon Hormane5054642015-05-25 14:28:36 +0900941rocker_desc_head_get(const struct rocker_dma_ring_info *info)
Jiri Pirko4b8ac962014-11-28 14:34:26 +0100942{
943 static struct rocker_desc_info *desc_info;
944 u32 head = __pos_inc(info->head, info->size);
945
946 desc_info = &info->desc_info[info->head];
947 if (head == info->tail)
948 return NULL; /* ring full */
949 desc_info->tlv_size = 0;
950 return desc_info;
951}
952
Simon Hormane5054642015-05-25 14:28:36 +0900953static void rocker_desc_commit(const struct rocker_desc_info *desc_info)
Jiri Pirko4b8ac962014-11-28 14:34:26 +0100954{
955 desc_info->desc->buf_size = desc_info->data_size;
956 desc_info->desc->tlv_size = desc_info->tlv_size;
957}
958
Simon Hormane5054642015-05-25 14:28:36 +0900959static void rocker_desc_head_set(const struct rocker *rocker,
Jiri Pirko4b8ac962014-11-28 14:34:26 +0100960 struct rocker_dma_ring_info *info,
Simon Hormane5054642015-05-25 14:28:36 +0900961 const struct rocker_desc_info *desc_info)
Jiri Pirko4b8ac962014-11-28 14:34:26 +0100962{
963 u32 head = __pos_inc(info->head, info->size);
964
965 BUG_ON(head == info->tail);
966 rocker_desc_commit(desc_info);
967 info->head = head;
968 rocker_write32(rocker, DMA_DESC_HEAD(info->type), head);
969}
970
971static struct rocker_desc_info *
972rocker_desc_tail_get(struct rocker_dma_ring_info *info)
973{
974 static struct rocker_desc_info *desc_info;
975
976 if (info->tail == info->head)
977 return NULL; /* nothing to be done between head and tail */
978 desc_info = &info->desc_info[info->tail];
979 if (!rocker_desc_gen(desc_info))
980 return NULL; /* gen bit not set, desc is not ready yet */
981 info->tail = __pos_inc(info->tail, info->size);
982 desc_info->tlv_size = desc_info->desc->tlv_size;
983 return desc_info;
984}
985
Simon Hormane5054642015-05-25 14:28:36 +0900986static void rocker_dma_ring_credits_set(const struct rocker *rocker,
987 const struct rocker_dma_ring_info *info,
Jiri Pirko4b8ac962014-11-28 14:34:26 +0100988 u32 credits)
989{
990 if (credits)
991 rocker_write32(rocker, DMA_DESC_CREDITS(info->type), credits);
992}
993
994static unsigned long rocker_dma_ring_size_fix(size_t size)
995{
996 return max(ROCKER_DMA_SIZE_MIN,
997 min(roundup_pow_of_two(size), ROCKER_DMA_SIZE_MAX));
998}
999
Simon Hormane5054642015-05-25 14:28:36 +09001000static int rocker_dma_ring_create(const struct rocker *rocker,
Jiri Pirko4b8ac962014-11-28 14:34:26 +01001001 unsigned int type,
1002 size_t size,
1003 struct rocker_dma_ring_info *info)
1004{
1005 int i;
1006
1007 BUG_ON(size != rocker_dma_ring_size_fix(size));
1008 info->size = size;
1009 info->type = type;
1010 info->head = 0;
1011 info->tail = 0;
1012 info->desc_info = kcalloc(info->size, sizeof(*info->desc_info),
1013 GFP_KERNEL);
1014 if (!info->desc_info)
1015 return -ENOMEM;
1016
1017 info->desc = pci_alloc_consistent(rocker->pdev,
1018 info->size * sizeof(*info->desc),
1019 &info->mapaddr);
1020 if (!info->desc) {
1021 kfree(info->desc_info);
1022 return -ENOMEM;
1023 }
1024
1025 for (i = 0; i < info->size; i++)
1026 info->desc_info[i].desc = &info->desc[i];
1027
1028 rocker_write32(rocker, DMA_DESC_CTRL(info->type),
1029 ROCKER_DMA_DESC_CTRL_RESET);
1030 rocker_write64(rocker, DMA_DESC_ADDR(info->type), info->mapaddr);
1031 rocker_write32(rocker, DMA_DESC_SIZE(info->type), info->size);
1032
1033 return 0;
1034}
1035
Simon Hormane5054642015-05-25 14:28:36 +09001036static void rocker_dma_ring_destroy(const struct rocker *rocker,
1037 const struct rocker_dma_ring_info *info)
Jiri Pirko4b8ac962014-11-28 14:34:26 +01001038{
1039 rocker_write64(rocker, DMA_DESC_ADDR(info->type), 0);
1040
1041 pci_free_consistent(rocker->pdev,
1042 info->size * sizeof(struct rocker_desc),
1043 info->desc, info->mapaddr);
1044 kfree(info->desc_info);
1045}
1046
Simon Hormane5054642015-05-25 14:28:36 +09001047static void rocker_dma_ring_pass_to_producer(const struct rocker *rocker,
Jiri Pirko4b8ac962014-11-28 14:34:26 +01001048 struct rocker_dma_ring_info *info)
1049{
1050 int i;
1051
1052 BUG_ON(info->head || info->tail);
1053
1054 /* When ring is consumer, we need to advance head for each desc.
1055 * That tells hw that the desc is ready to be used by it.
1056 */
1057 for (i = 0; i < info->size - 1; i++)
1058 rocker_desc_head_set(rocker, info, &info->desc_info[i]);
1059 rocker_desc_commit(&info->desc_info[i]);
1060}
1061
Simon Hormane5054642015-05-25 14:28:36 +09001062static int rocker_dma_ring_bufs_alloc(const struct rocker *rocker,
1063 const struct rocker_dma_ring_info *info,
Jiri Pirko4b8ac962014-11-28 14:34:26 +01001064 int direction, size_t buf_size)
1065{
1066 struct pci_dev *pdev = rocker->pdev;
1067 int i;
1068 int err;
1069
1070 for (i = 0; i < info->size; i++) {
1071 struct rocker_desc_info *desc_info = &info->desc_info[i];
1072 struct rocker_desc *desc = &info->desc[i];
1073 dma_addr_t dma_handle;
1074 char *buf;
1075
1076 buf = kzalloc(buf_size, GFP_KERNEL | GFP_DMA);
1077 if (!buf) {
1078 err = -ENOMEM;
1079 goto rollback;
1080 }
1081
1082 dma_handle = pci_map_single(pdev, buf, buf_size, direction);
1083 if (pci_dma_mapping_error(pdev, dma_handle)) {
1084 kfree(buf);
1085 err = -EIO;
1086 goto rollback;
1087 }
1088
1089 desc_info->data = buf;
1090 desc_info->data_size = buf_size;
1091 dma_unmap_addr_set(desc_info, mapaddr, dma_handle);
1092
1093 desc->buf_addr = dma_handle;
1094 desc->buf_size = buf_size;
1095 }
1096 return 0;
1097
1098rollback:
1099 for (i--; i >= 0; i--) {
Simon Hormane5054642015-05-25 14:28:36 +09001100 const struct rocker_desc_info *desc_info = &info->desc_info[i];
Jiri Pirko4b8ac962014-11-28 14:34:26 +01001101
1102 pci_unmap_single(pdev, dma_unmap_addr(desc_info, mapaddr),
1103 desc_info->data_size, direction);
1104 kfree(desc_info->data);
1105 }
1106 return err;
1107}
1108
Simon Hormane5054642015-05-25 14:28:36 +09001109static void rocker_dma_ring_bufs_free(const struct rocker *rocker,
1110 const struct rocker_dma_ring_info *info,
Jiri Pirko4b8ac962014-11-28 14:34:26 +01001111 int direction)
1112{
1113 struct pci_dev *pdev = rocker->pdev;
1114 int i;
1115
1116 for (i = 0; i < info->size; i++) {
Simon Hormane5054642015-05-25 14:28:36 +09001117 const struct rocker_desc_info *desc_info = &info->desc_info[i];
Jiri Pirko4b8ac962014-11-28 14:34:26 +01001118 struct rocker_desc *desc = &info->desc[i];
1119
1120 desc->buf_addr = 0;
1121 desc->buf_size = 0;
1122 pci_unmap_single(pdev, dma_unmap_addr(desc_info, mapaddr),
1123 desc_info->data_size, direction);
1124 kfree(desc_info->data);
1125 }
1126}
1127
1128static int rocker_dma_rings_init(struct rocker *rocker)
1129{
Simon Hormane5054642015-05-25 14:28:36 +09001130 const struct pci_dev *pdev = rocker->pdev;
Jiri Pirko4b8ac962014-11-28 14:34:26 +01001131 int err;
1132
1133 err = rocker_dma_ring_create(rocker, ROCKER_DMA_CMD,
1134 ROCKER_DMA_CMD_DEFAULT_SIZE,
1135 &rocker->cmd_ring);
1136 if (err) {
1137 dev_err(&pdev->dev, "failed to create command dma ring\n");
1138 return err;
1139 }
1140
1141 spin_lock_init(&rocker->cmd_ring_lock);
1142
1143 err = rocker_dma_ring_bufs_alloc(rocker, &rocker->cmd_ring,
1144 PCI_DMA_BIDIRECTIONAL, PAGE_SIZE);
1145 if (err) {
1146 dev_err(&pdev->dev, "failed to alloc command dma ring buffers\n");
1147 goto err_dma_cmd_ring_bufs_alloc;
1148 }
1149
1150 err = rocker_dma_ring_create(rocker, ROCKER_DMA_EVENT,
1151 ROCKER_DMA_EVENT_DEFAULT_SIZE,
1152 &rocker->event_ring);
1153 if (err) {
1154 dev_err(&pdev->dev, "failed to create event dma ring\n");
1155 goto err_dma_event_ring_create;
1156 }
1157
1158 err = rocker_dma_ring_bufs_alloc(rocker, &rocker->event_ring,
1159 PCI_DMA_FROMDEVICE, PAGE_SIZE);
1160 if (err) {
1161 dev_err(&pdev->dev, "failed to alloc event dma ring buffers\n");
1162 goto err_dma_event_ring_bufs_alloc;
1163 }
1164 rocker_dma_ring_pass_to_producer(rocker, &rocker->event_ring);
1165 return 0;
1166
1167err_dma_event_ring_bufs_alloc:
1168 rocker_dma_ring_destroy(rocker, &rocker->event_ring);
1169err_dma_event_ring_create:
1170 rocker_dma_ring_bufs_free(rocker, &rocker->cmd_ring,
1171 PCI_DMA_BIDIRECTIONAL);
1172err_dma_cmd_ring_bufs_alloc:
1173 rocker_dma_ring_destroy(rocker, &rocker->cmd_ring);
1174 return err;
1175}
1176
1177static void rocker_dma_rings_fini(struct rocker *rocker)
1178{
1179 rocker_dma_ring_bufs_free(rocker, &rocker->event_ring,
1180 PCI_DMA_BIDIRECTIONAL);
1181 rocker_dma_ring_destroy(rocker, &rocker->event_ring);
1182 rocker_dma_ring_bufs_free(rocker, &rocker->cmd_ring,
1183 PCI_DMA_BIDIRECTIONAL);
1184 rocker_dma_ring_destroy(rocker, &rocker->cmd_ring);
1185}
1186
Simon Horman534ba6a2015-06-01 13:25:04 +09001187static int rocker_dma_rx_ring_skb_map(const struct rocker_port *rocker_port,
Jiri Pirko4b8ac962014-11-28 14:34:26 +01001188 struct rocker_desc_info *desc_info,
1189 struct sk_buff *skb, size_t buf_len)
1190{
Simon Horman534ba6a2015-06-01 13:25:04 +09001191 const struct rocker *rocker = rocker_port->rocker;
Jiri Pirko4b8ac962014-11-28 14:34:26 +01001192 struct pci_dev *pdev = rocker->pdev;
1193 dma_addr_t dma_handle;
1194
1195 dma_handle = pci_map_single(pdev, skb->data, buf_len,
1196 PCI_DMA_FROMDEVICE);
1197 if (pci_dma_mapping_error(pdev, dma_handle))
1198 return -EIO;
1199 if (rocker_tlv_put_u64(desc_info, ROCKER_TLV_RX_FRAG_ADDR, dma_handle))
1200 goto tlv_put_failure;
1201 if (rocker_tlv_put_u16(desc_info, ROCKER_TLV_RX_FRAG_MAX_LEN, buf_len))
1202 goto tlv_put_failure;
1203 return 0;
1204
1205tlv_put_failure:
1206 pci_unmap_single(pdev, dma_handle, buf_len, PCI_DMA_FROMDEVICE);
1207 desc_info->tlv_size = 0;
1208 return -EMSGSIZE;
1209}
1210
Simon Hormane5054642015-05-25 14:28:36 +09001211static size_t rocker_port_rx_buf_len(const struct rocker_port *rocker_port)
Jiri Pirko4b8ac962014-11-28 14:34:26 +01001212{
1213 return rocker_port->dev->mtu + ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN;
1214}
1215
Simon Horman534ba6a2015-06-01 13:25:04 +09001216static int rocker_dma_rx_ring_skb_alloc(const struct rocker_port *rocker_port,
Jiri Pirko4b8ac962014-11-28 14:34:26 +01001217 struct rocker_desc_info *desc_info)
1218{
1219 struct net_device *dev = rocker_port->dev;
1220 struct sk_buff *skb;
1221 size_t buf_len = rocker_port_rx_buf_len(rocker_port);
1222 int err;
1223
1224 /* Ensure that hw will see tlv_size zero in case of an error.
1225 * That tells hw to use another descriptor.
1226 */
1227 rocker_desc_cookie_ptr_set(desc_info, NULL);
1228 desc_info->tlv_size = 0;
1229
1230 skb = netdev_alloc_skb_ip_align(dev, buf_len);
1231 if (!skb)
1232 return -ENOMEM;
Simon Horman534ba6a2015-06-01 13:25:04 +09001233 err = rocker_dma_rx_ring_skb_map(rocker_port, desc_info, skb, buf_len);
Jiri Pirko4b8ac962014-11-28 14:34:26 +01001234 if (err) {
1235 dev_kfree_skb_any(skb);
1236 return err;
1237 }
1238 rocker_desc_cookie_ptr_set(desc_info, skb);
1239 return 0;
1240}
1241
Simon Hormane5054642015-05-25 14:28:36 +09001242static void rocker_dma_rx_ring_skb_unmap(const struct rocker *rocker,
1243 const struct rocker_tlv **attrs)
Jiri Pirko4b8ac962014-11-28 14:34:26 +01001244{
1245 struct pci_dev *pdev = rocker->pdev;
1246 dma_addr_t dma_handle;
1247 size_t len;
1248
1249 if (!attrs[ROCKER_TLV_RX_FRAG_ADDR] ||
1250 !attrs[ROCKER_TLV_RX_FRAG_MAX_LEN])
1251 return;
1252 dma_handle = rocker_tlv_get_u64(attrs[ROCKER_TLV_RX_FRAG_ADDR]);
1253 len = rocker_tlv_get_u16(attrs[ROCKER_TLV_RX_FRAG_MAX_LEN]);
1254 pci_unmap_single(pdev, dma_handle, len, PCI_DMA_FROMDEVICE);
1255}
1256
Simon Hormane5054642015-05-25 14:28:36 +09001257static void rocker_dma_rx_ring_skb_free(const struct rocker *rocker,
1258 const struct rocker_desc_info *desc_info)
Jiri Pirko4b8ac962014-11-28 14:34:26 +01001259{
Simon Hormane5054642015-05-25 14:28:36 +09001260 const struct rocker_tlv *attrs[ROCKER_TLV_RX_MAX + 1];
Jiri Pirko4b8ac962014-11-28 14:34:26 +01001261 struct sk_buff *skb = rocker_desc_cookie_ptr_get(desc_info);
1262
1263 if (!skb)
1264 return;
1265 rocker_tlv_parse_desc(attrs, ROCKER_TLV_RX_MAX, desc_info);
1266 rocker_dma_rx_ring_skb_unmap(rocker, attrs);
1267 dev_kfree_skb_any(skb);
1268}
1269
Simon Horman534ba6a2015-06-01 13:25:04 +09001270static int rocker_dma_rx_ring_skbs_alloc(const struct rocker_port *rocker_port)
Jiri Pirko4b8ac962014-11-28 14:34:26 +01001271{
Simon Hormane5054642015-05-25 14:28:36 +09001272 const struct rocker_dma_ring_info *rx_ring = &rocker_port->rx_ring;
Simon Horman534ba6a2015-06-01 13:25:04 +09001273 const struct rocker *rocker = rocker_port->rocker;
Jiri Pirko4b8ac962014-11-28 14:34:26 +01001274 int i;
1275 int err;
1276
1277 for (i = 0; i < rx_ring->size; i++) {
Simon Horman534ba6a2015-06-01 13:25:04 +09001278 err = rocker_dma_rx_ring_skb_alloc(rocker_port,
Jiri Pirko4b8ac962014-11-28 14:34:26 +01001279 &rx_ring->desc_info[i]);
1280 if (err)
1281 goto rollback;
1282 }
1283 return 0;
1284
1285rollback:
1286 for (i--; i >= 0; i--)
1287 rocker_dma_rx_ring_skb_free(rocker, &rx_ring->desc_info[i]);
1288 return err;
1289}
1290
Simon Horman534ba6a2015-06-01 13:25:04 +09001291static void rocker_dma_rx_ring_skbs_free(const struct rocker_port *rocker_port)
Jiri Pirko4b8ac962014-11-28 14:34:26 +01001292{
Simon Hormane5054642015-05-25 14:28:36 +09001293 const struct rocker_dma_ring_info *rx_ring = &rocker_port->rx_ring;
Simon Horman534ba6a2015-06-01 13:25:04 +09001294 const struct rocker *rocker = rocker_port->rocker;
Jiri Pirko4b8ac962014-11-28 14:34:26 +01001295 int i;
1296
1297 for (i = 0; i < rx_ring->size; i++)
1298 rocker_dma_rx_ring_skb_free(rocker, &rx_ring->desc_info[i]);
1299}
1300
1301static int rocker_port_dma_rings_init(struct rocker_port *rocker_port)
1302{
1303 struct rocker *rocker = rocker_port->rocker;
1304 int err;
1305
1306 err = rocker_dma_ring_create(rocker,
1307 ROCKER_DMA_TX(rocker_port->port_number),
1308 ROCKER_DMA_TX_DEFAULT_SIZE,
1309 &rocker_port->tx_ring);
1310 if (err) {
1311 netdev_err(rocker_port->dev, "failed to create tx dma ring\n");
1312 return err;
1313 }
1314
1315 err = rocker_dma_ring_bufs_alloc(rocker, &rocker_port->tx_ring,
1316 PCI_DMA_TODEVICE,
1317 ROCKER_DMA_TX_DESC_SIZE);
1318 if (err) {
1319 netdev_err(rocker_port->dev, "failed to alloc tx dma ring buffers\n");
1320 goto err_dma_tx_ring_bufs_alloc;
1321 }
1322
1323 err = rocker_dma_ring_create(rocker,
1324 ROCKER_DMA_RX(rocker_port->port_number),
1325 ROCKER_DMA_RX_DEFAULT_SIZE,
1326 &rocker_port->rx_ring);
1327 if (err) {
1328 netdev_err(rocker_port->dev, "failed to create rx dma ring\n");
1329 goto err_dma_rx_ring_create;
1330 }
1331
1332 err = rocker_dma_ring_bufs_alloc(rocker, &rocker_port->rx_ring,
1333 PCI_DMA_BIDIRECTIONAL,
1334 ROCKER_DMA_RX_DESC_SIZE);
1335 if (err) {
1336 netdev_err(rocker_port->dev, "failed to alloc rx dma ring buffers\n");
1337 goto err_dma_rx_ring_bufs_alloc;
1338 }
1339
Simon Horman534ba6a2015-06-01 13:25:04 +09001340 err = rocker_dma_rx_ring_skbs_alloc(rocker_port);
Jiri Pirko4b8ac962014-11-28 14:34:26 +01001341 if (err) {
1342 netdev_err(rocker_port->dev, "failed to alloc rx dma ring skbs\n");
1343 goto err_dma_rx_ring_skbs_alloc;
1344 }
1345 rocker_dma_ring_pass_to_producer(rocker, &rocker_port->rx_ring);
1346
1347 return 0;
1348
1349err_dma_rx_ring_skbs_alloc:
1350 rocker_dma_ring_bufs_free(rocker, &rocker_port->rx_ring,
1351 PCI_DMA_BIDIRECTIONAL);
1352err_dma_rx_ring_bufs_alloc:
1353 rocker_dma_ring_destroy(rocker, &rocker_port->rx_ring);
1354err_dma_rx_ring_create:
1355 rocker_dma_ring_bufs_free(rocker, &rocker_port->tx_ring,
1356 PCI_DMA_TODEVICE);
1357err_dma_tx_ring_bufs_alloc:
1358 rocker_dma_ring_destroy(rocker, &rocker_port->tx_ring);
1359 return err;
1360}
1361
1362static void rocker_port_dma_rings_fini(struct rocker_port *rocker_port)
1363{
1364 struct rocker *rocker = rocker_port->rocker;
1365
Simon Horman534ba6a2015-06-01 13:25:04 +09001366 rocker_dma_rx_ring_skbs_free(rocker_port);
Jiri Pirko4b8ac962014-11-28 14:34:26 +01001367 rocker_dma_ring_bufs_free(rocker, &rocker_port->rx_ring,
1368 PCI_DMA_BIDIRECTIONAL);
1369 rocker_dma_ring_destroy(rocker, &rocker_port->rx_ring);
1370 rocker_dma_ring_bufs_free(rocker, &rocker_port->tx_ring,
1371 PCI_DMA_TODEVICE);
1372 rocker_dma_ring_destroy(rocker, &rocker_port->tx_ring);
1373}
1374
Simon Hormane5054642015-05-25 14:28:36 +09001375static void rocker_port_set_enable(const struct rocker_port *rocker_port,
1376 bool enable)
Jiri Pirko4b8ac962014-11-28 14:34:26 +01001377{
1378 u64 val = rocker_read64(rocker_port->rocker, PORT_PHYS_ENABLE);
1379
1380 if (enable)
David S. Miller71a83a62015-03-03 21:16:48 -05001381 val |= 1ULL << rocker_port->pport;
Jiri Pirko4b8ac962014-11-28 14:34:26 +01001382 else
David S. Miller71a83a62015-03-03 21:16:48 -05001383 val &= ~(1ULL << rocker_port->pport);
Jiri Pirko4b8ac962014-11-28 14:34:26 +01001384 rocker_write64(rocker_port->rocker, PORT_PHYS_ENABLE, val);
1385}
1386
1387/********************************
1388 * Interrupt handler and helpers
1389 ********************************/
1390
1391static irqreturn_t rocker_cmd_irq_handler(int irq, void *dev_id)
1392{
1393 struct rocker *rocker = dev_id;
Simon Hormane5054642015-05-25 14:28:36 +09001394 const struct rocker_desc_info *desc_info;
Jiri Pirko4b8ac962014-11-28 14:34:26 +01001395 struct rocker_wait *wait;
1396 u32 credits = 0;
1397
1398 spin_lock(&rocker->cmd_ring_lock);
1399 while ((desc_info = rocker_desc_tail_get(&rocker->cmd_ring))) {
1400 wait = rocker_desc_cookie_ptr_get(desc_info);
Scott Feldman179f9a22015-06-12 21:35:46 -07001401 if (wait->nowait) {
1402 rocker_desc_gen_clear(desc_info);
1403 rocker_wait_destroy(SWITCHDEV_TRANS_NONE, wait);
1404 } else {
1405 rocker_wait_wake_up(wait);
1406 }
Jiri Pirko4b8ac962014-11-28 14:34:26 +01001407 credits++;
1408 }
1409 spin_unlock(&rocker->cmd_ring_lock);
1410 rocker_dma_ring_credits_set(rocker, &rocker->cmd_ring, credits);
1411
1412 return IRQ_HANDLED;
1413}
1414
Simon Hormane5054642015-05-25 14:28:36 +09001415static void rocker_port_link_up(const struct rocker_port *rocker_port)
Jiri Pirko4b8ac962014-11-28 14:34:26 +01001416{
1417 netif_carrier_on(rocker_port->dev);
1418 netdev_info(rocker_port->dev, "Link is up\n");
1419}
1420
Simon Hormane5054642015-05-25 14:28:36 +09001421static void rocker_port_link_down(const struct rocker_port *rocker_port)
Jiri Pirko4b8ac962014-11-28 14:34:26 +01001422{
1423 netif_carrier_off(rocker_port->dev);
1424 netdev_info(rocker_port->dev, "Link is down\n");
1425}
1426
Simon Hormane5054642015-05-25 14:28:36 +09001427static int rocker_event_link_change(const struct rocker *rocker,
Jiri Pirko4b8ac962014-11-28 14:34:26 +01001428 const struct rocker_tlv *info)
1429{
Simon Hormane5054642015-05-25 14:28:36 +09001430 const struct rocker_tlv *attrs[ROCKER_TLV_EVENT_LINK_CHANGED_MAX + 1];
Jiri Pirko4b8ac962014-11-28 14:34:26 +01001431 unsigned int port_number;
1432 bool link_up;
1433 struct rocker_port *rocker_port;
1434
1435 rocker_tlv_parse_nested(attrs, ROCKER_TLV_EVENT_LINK_CHANGED_MAX, info);
Scott Feldman4a6bb6d2015-02-25 20:15:37 -08001436 if (!attrs[ROCKER_TLV_EVENT_LINK_CHANGED_PPORT] ||
Jiri Pirko4b8ac962014-11-28 14:34:26 +01001437 !attrs[ROCKER_TLV_EVENT_LINK_CHANGED_LINKUP])
1438 return -EIO;
1439 port_number =
Scott Feldman4a6bb6d2015-02-25 20:15:37 -08001440 rocker_tlv_get_u32(attrs[ROCKER_TLV_EVENT_LINK_CHANGED_PPORT]) - 1;
Jiri Pirko4b8ac962014-11-28 14:34:26 +01001441 link_up = rocker_tlv_get_u8(attrs[ROCKER_TLV_EVENT_LINK_CHANGED_LINKUP]);
1442
1443 if (port_number >= rocker->port_count)
1444 return -EINVAL;
1445
1446 rocker_port = rocker->ports[port_number];
1447 if (netif_carrier_ok(rocker_port->dev) != link_up) {
1448 if (link_up)
1449 rocker_port_link_up(rocker_port);
1450 else
1451 rocker_port_link_down(rocker_port);
1452 }
1453
1454 return 0;
1455}
1456
Scott Feldman6c707942014-11-28 14:34:28 +01001457static int rocker_port_fdb(struct rocker_port *rocker_port,
Scott Feldmanc4f20322015-05-10 09:47:50 -07001458 enum switchdev_trans trans,
Scott Feldman6c707942014-11-28 14:34:28 +01001459 const unsigned char *addr,
1460 __be16 vlan_id, int flags);
1461
Simon Hormane5054642015-05-25 14:28:36 +09001462static int rocker_event_mac_vlan_seen(const struct rocker *rocker,
Scott Feldman6c707942014-11-28 14:34:28 +01001463 const struct rocker_tlv *info)
1464{
Simon Hormane5054642015-05-25 14:28:36 +09001465 const struct rocker_tlv *attrs[ROCKER_TLV_EVENT_MAC_VLAN_MAX + 1];
Scott Feldman6c707942014-11-28 14:34:28 +01001466 unsigned int port_number;
1467 struct rocker_port *rocker_port;
Simon Hormane5054642015-05-25 14:28:36 +09001468 const unsigned char *addr;
Scott Feldman92014b92015-06-12 21:35:49 -07001469 int flags = ROCKER_OP_FLAG_NOWAIT | ROCKER_OP_FLAG_LEARNED;
Scott Feldman6c707942014-11-28 14:34:28 +01001470 __be16 vlan_id;
1471
1472 rocker_tlv_parse_nested(attrs, ROCKER_TLV_EVENT_MAC_VLAN_MAX, info);
Scott Feldman4a6bb6d2015-02-25 20:15:37 -08001473 if (!attrs[ROCKER_TLV_EVENT_MAC_VLAN_PPORT] ||
Scott Feldman6c707942014-11-28 14:34:28 +01001474 !attrs[ROCKER_TLV_EVENT_MAC_VLAN_MAC] ||
1475 !attrs[ROCKER_TLV_EVENT_MAC_VLAN_VLAN_ID])
1476 return -EIO;
1477 port_number =
Scott Feldman4a6bb6d2015-02-25 20:15:37 -08001478 rocker_tlv_get_u32(attrs[ROCKER_TLV_EVENT_MAC_VLAN_PPORT]) - 1;
Scott Feldman6c707942014-11-28 14:34:28 +01001479 addr = rocker_tlv_data(attrs[ROCKER_TLV_EVENT_MAC_VLAN_MAC]);
Jiri Pirko9b03c712014-12-03 14:14:53 +01001480 vlan_id = rocker_tlv_get_be16(attrs[ROCKER_TLV_EVENT_MAC_VLAN_VLAN_ID]);
Scott Feldman6c707942014-11-28 14:34:28 +01001481
1482 if (port_number >= rocker->port_count)
1483 return -EINVAL;
1484
1485 rocker_port = rocker->ports[port_number];
1486
1487 if (rocker_port->stp_state != BR_STATE_LEARNING &&
1488 rocker_port->stp_state != BR_STATE_FORWARDING)
1489 return 0;
1490
Scott Feldman92014b92015-06-12 21:35:49 -07001491 return rocker_port_fdb(rocker_port, SWITCHDEV_TRANS_NONE,
1492 addr, vlan_id, flags);
Scott Feldman6c707942014-11-28 14:34:28 +01001493}
Scott Feldman9f6bbf72014-11-28 14:34:27 +01001494
Simon Hormane5054642015-05-25 14:28:36 +09001495static int rocker_event_process(const struct rocker *rocker,
1496 const struct rocker_desc_info *desc_info)
Jiri Pirko4b8ac962014-11-28 14:34:26 +01001497{
Simon Hormane5054642015-05-25 14:28:36 +09001498 const struct rocker_tlv *attrs[ROCKER_TLV_EVENT_MAX + 1];
1499 const struct rocker_tlv *info;
Jiri Pirko4b8ac962014-11-28 14:34:26 +01001500 u16 type;
1501
1502 rocker_tlv_parse_desc(attrs, ROCKER_TLV_EVENT_MAX, desc_info);
1503 if (!attrs[ROCKER_TLV_EVENT_TYPE] ||
1504 !attrs[ROCKER_TLV_EVENT_INFO])
1505 return -EIO;
1506
1507 type = rocker_tlv_get_u16(attrs[ROCKER_TLV_EVENT_TYPE]);
1508 info = attrs[ROCKER_TLV_EVENT_INFO];
1509
1510 switch (type) {
1511 case ROCKER_TLV_EVENT_TYPE_LINK_CHANGED:
1512 return rocker_event_link_change(rocker, info);
Scott Feldman6c707942014-11-28 14:34:28 +01001513 case ROCKER_TLV_EVENT_TYPE_MAC_VLAN_SEEN:
1514 return rocker_event_mac_vlan_seen(rocker, info);
Jiri Pirko4b8ac962014-11-28 14:34:26 +01001515 }
1516
1517 return -EOPNOTSUPP;
1518}
1519
1520static irqreturn_t rocker_event_irq_handler(int irq, void *dev_id)
1521{
1522 struct rocker *rocker = dev_id;
Simon Hormane5054642015-05-25 14:28:36 +09001523 const struct pci_dev *pdev = rocker->pdev;
1524 const struct rocker_desc_info *desc_info;
Jiri Pirko4b8ac962014-11-28 14:34:26 +01001525 u32 credits = 0;
1526 int err;
1527
1528 while ((desc_info = rocker_desc_tail_get(&rocker->event_ring))) {
1529 err = rocker_desc_err(desc_info);
1530 if (err) {
1531 dev_err(&pdev->dev, "event desc received with err %d\n",
1532 err);
1533 } else {
1534 err = rocker_event_process(rocker, desc_info);
1535 if (err)
1536 dev_err(&pdev->dev, "event processing failed with err %d\n",
1537 err);
1538 }
1539 rocker_desc_gen_clear(desc_info);
1540 rocker_desc_head_set(rocker, &rocker->event_ring, desc_info);
1541 credits++;
1542 }
1543 rocker_dma_ring_credits_set(rocker, &rocker->event_ring, credits);
1544
1545 return IRQ_HANDLED;
1546}
1547
1548static irqreturn_t rocker_tx_irq_handler(int irq, void *dev_id)
1549{
1550 struct rocker_port *rocker_port = dev_id;
1551
1552 napi_schedule(&rocker_port->napi_tx);
1553 return IRQ_HANDLED;
1554}
1555
1556static irqreturn_t rocker_rx_irq_handler(int irq, void *dev_id)
1557{
1558 struct rocker_port *rocker_port = dev_id;
1559
1560 napi_schedule(&rocker_port->napi_rx);
1561 return IRQ_HANDLED;
1562}
1563
1564/********************
1565 * Command interface
1566 ********************/
1567
Simon Horman534ba6a2015-06-01 13:25:04 +09001568typedef int (*rocker_cmd_prep_cb_t)(const struct rocker_port *rocker_port,
Simon Hormane5054642015-05-25 14:28:36 +09001569 struct rocker_desc_info *desc_info,
1570 void *priv);
1571
Simon Horman534ba6a2015-06-01 13:25:04 +09001572typedef int (*rocker_cmd_proc_cb_t)(const struct rocker_port *rocker_port,
Simon Hormane5054642015-05-25 14:28:36 +09001573 const struct rocker_desc_info *desc_info,
1574 void *priv);
Jiri Pirko4b8ac962014-11-28 14:34:26 +01001575
Simon Horman534ba6a2015-06-01 13:25:04 +09001576static int rocker_cmd_exec(struct rocker_port *rocker_port,
Scott Feldman179f9a22015-06-12 21:35:46 -07001577 enum switchdev_trans trans, int flags,
Simon Hormane5054642015-05-25 14:28:36 +09001578 rocker_cmd_prep_cb_t prepare, void *prepare_priv,
1579 rocker_cmd_proc_cb_t process, void *process_priv)
Jiri Pirko4b8ac962014-11-28 14:34:26 +01001580{
Simon Horman534ba6a2015-06-01 13:25:04 +09001581 struct rocker *rocker = rocker_port->rocker;
Jiri Pirko4b8ac962014-11-28 14:34:26 +01001582 struct rocker_desc_info *desc_info;
1583 struct rocker_wait *wait;
Scott Feldman179f9a22015-06-12 21:35:46 -07001584 bool nowait = !!(flags & ROCKER_OP_FLAG_NOWAIT);
1585 unsigned long lock_flags;
Jiri Pirko4b8ac962014-11-28 14:34:26 +01001586 int err;
1587
Scott Feldman179f9a22015-06-12 21:35:46 -07001588 wait = rocker_wait_create(rocker_port, trans, flags);
Jiri Pirko4b8ac962014-11-28 14:34:26 +01001589 if (!wait)
1590 return -ENOMEM;
Scott Feldman179f9a22015-06-12 21:35:46 -07001591 wait->nowait = nowait;
Jiri Pirko4b8ac962014-11-28 14:34:26 +01001592
Scott Feldman179f9a22015-06-12 21:35:46 -07001593 spin_lock_irqsave(&rocker->cmd_ring_lock, lock_flags);
Scott Feldmanc4f20322015-05-10 09:47:50 -07001594
Jiri Pirko4b8ac962014-11-28 14:34:26 +01001595 desc_info = rocker_desc_head_get(&rocker->cmd_ring);
1596 if (!desc_info) {
Scott Feldman179f9a22015-06-12 21:35:46 -07001597 spin_unlock_irqrestore(&rocker->cmd_ring_lock, lock_flags);
Jiri Pirko4b8ac962014-11-28 14:34:26 +01001598 err = -EAGAIN;
1599 goto out;
1600 }
Scott Feldmanc4f20322015-05-10 09:47:50 -07001601
Simon Horman534ba6a2015-06-01 13:25:04 +09001602 err = prepare(rocker_port, desc_info, prepare_priv);
Jiri Pirko4b8ac962014-11-28 14:34:26 +01001603 if (err) {
Scott Feldman179f9a22015-06-12 21:35:46 -07001604 spin_unlock_irqrestore(&rocker->cmd_ring_lock, lock_flags);
Jiri Pirko4b8ac962014-11-28 14:34:26 +01001605 goto out;
1606 }
Scott Feldmanc4f20322015-05-10 09:47:50 -07001607
Jiri Pirko4b8ac962014-11-28 14:34:26 +01001608 rocker_desc_cookie_ptr_set(desc_info, wait);
Scott Feldmanc4f20322015-05-10 09:47:50 -07001609
1610 if (trans != SWITCHDEV_TRANS_PREPARE)
1611 rocker_desc_head_set(rocker, &rocker->cmd_ring, desc_info);
1612
Scott Feldman179f9a22015-06-12 21:35:46 -07001613 spin_unlock_irqrestore(&rocker->cmd_ring_lock, lock_flags);
1614
1615 if (nowait)
1616 return 0;
Jiri Pirko4b8ac962014-11-28 14:34:26 +01001617
Scott Feldmanc4f20322015-05-10 09:47:50 -07001618 if (trans != SWITCHDEV_TRANS_PREPARE)
1619 if (!rocker_wait_event_timeout(wait, HZ / 10))
1620 return -EIO;
Jiri Pirko4b8ac962014-11-28 14:34:26 +01001621
1622 err = rocker_desc_err(desc_info);
1623 if (err)
1624 return err;
1625
1626 if (process)
Simon Horman534ba6a2015-06-01 13:25:04 +09001627 err = process(rocker_port, desc_info, process_priv);
Jiri Pirko4b8ac962014-11-28 14:34:26 +01001628
1629 rocker_desc_gen_clear(desc_info);
1630out:
Simon Horman0985df72015-05-25 14:28:35 +09001631 rocker_wait_destroy(trans, wait);
Jiri Pirko4b8ac962014-11-28 14:34:26 +01001632 return err;
1633}
1634
1635static int
Simon Horman534ba6a2015-06-01 13:25:04 +09001636rocker_cmd_get_port_settings_prep(const struct rocker_port *rocker_port,
Jiri Pirko4b8ac962014-11-28 14:34:26 +01001637 struct rocker_desc_info *desc_info,
1638 void *priv)
1639{
1640 struct rocker_tlv *cmd_info;
1641
1642 if (rocker_tlv_put_u16(desc_info, ROCKER_TLV_CMD_TYPE,
1643 ROCKER_TLV_CMD_TYPE_GET_PORT_SETTINGS))
1644 return -EMSGSIZE;
1645 cmd_info = rocker_tlv_nest_start(desc_info, ROCKER_TLV_CMD_INFO);
1646 if (!cmd_info)
1647 return -EMSGSIZE;
Scott Feldman4a6bb6d2015-02-25 20:15:37 -08001648 if (rocker_tlv_put_u32(desc_info, ROCKER_TLV_CMD_PORT_SETTINGS_PPORT,
1649 rocker_port->pport))
Jiri Pirko4b8ac962014-11-28 14:34:26 +01001650 return -EMSGSIZE;
1651 rocker_tlv_nest_end(desc_info, cmd_info);
1652 return 0;
1653}
1654
1655static int
Simon Horman534ba6a2015-06-01 13:25:04 +09001656rocker_cmd_get_port_settings_ethtool_proc(const struct rocker_port *rocker_port,
Simon Hormane5054642015-05-25 14:28:36 +09001657 const struct rocker_desc_info *desc_info,
Jiri Pirko4b8ac962014-11-28 14:34:26 +01001658 void *priv)
1659{
1660 struct ethtool_cmd *ecmd = priv;
Simon Hormane5054642015-05-25 14:28:36 +09001661 const struct rocker_tlv *attrs[ROCKER_TLV_CMD_MAX + 1];
1662 const struct rocker_tlv *info_attrs[ROCKER_TLV_CMD_PORT_SETTINGS_MAX + 1];
Jiri Pirko4b8ac962014-11-28 14:34:26 +01001663 u32 speed;
1664 u8 duplex;
1665 u8 autoneg;
1666
1667 rocker_tlv_parse_desc(attrs, ROCKER_TLV_CMD_MAX, desc_info);
1668 if (!attrs[ROCKER_TLV_CMD_INFO])
1669 return -EIO;
1670
1671 rocker_tlv_parse_nested(info_attrs, ROCKER_TLV_CMD_PORT_SETTINGS_MAX,
1672 attrs[ROCKER_TLV_CMD_INFO]);
1673 if (!info_attrs[ROCKER_TLV_CMD_PORT_SETTINGS_SPEED] ||
1674 !info_attrs[ROCKER_TLV_CMD_PORT_SETTINGS_DUPLEX] ||
1675 !info_attrs[ROCKER_TLV_CMD_PORT_SETTINGS_AUTONEG])
1676 return -EIO;
1677
1678 speed = rocker_tlv_get_u32(info_attrs[ROCKER_TLV_CMD_PORT_SETTINGS_SPEED]);
1679 duplex = rocker_tlv_get_u8(info_attrs[ROCKER_TLV_CMD_PORT_SETTINGS_DUPLEX]);
1680 autoneg = rocker_tlv_get_u8(info_attrs[ROCKER_TLV_CMD_PORT_SETTINGS_AUTONEG]);
1681
1682 ecmd->transceiver = XCVR_INTERNAL;
1683 ecmd->supported = SUPPORTED_TP;
1684 ecmd->phy_address = 0xff;
1685 ecmd->port = PORT_TP;
1686 ethtool_cmd_speed_set(ecmd, speed);
1687 ecmd->duplex = duplex ? DUPLEX_FULL : DUPLEX_HALF;
1688 ecmd->autoneg = autoneg ? AUTONEG_ENABLE : AUTONEG_DISABLE;
1689
1690 return 0;
1691}
1692
1693static int
Simon Horman534ba6a2015-06-01 13:25:04 +09001694rocker_cmd_get_port_settings_macaddr_proc(const struct rocker_port *rocker_port,
Simon Hormane5054642015-05-25 14:28:36 +09001695 const struct rocker_desc_info *desc_info,
Jiri Pirko4b8ac962014-11-28 14:34:26 +01001696 void *priv)
1697{
1698 unsigned char *macaddr = priv;
Simon Hormane5054642015-05-25 14:28:36 +09001699 const struct rocker_tlv *attrs[ROCKER_TLV_CMD_MAX + 1];
1700 const struct rocker_tlv *info_attrs[ROCKER_TLV_CMD_PORT_SETTINGS_MAX + 1];
1701 const struct rocker_tlv *attr;
Jiri Pirko4b8ac962014-11-28 14:34:26 +01001702
1703 rocker_tlv_parse_desc(attrs, ROCKER_TLV_CMD_MAX, desc_info);
1704 if (!attrs[ROCKER_TLV_CMD_INFO])
1705 return -EIO;
1706
1707 rocker_tlv_parse_nested(info_attrs, ROCKER_TLV_CMD_PORT_SETTINGS_MAX,
1708 attrs[ROCKER_TLV_CMD_INFO]);
1709 attr = info_attrs[ROCKER_TLV_CMD_PORT_SETTINGS_MACADDR];
1710 if (!attr)
1711 return -EIO;
1712
1713 if (rocker_tlv_len(attr) != ETH_ALEN)
1714 return -EINVAL;
1715
1716 ether_addr_copy(macaddr, rocker_tlv_data(attr));
1717 return 0;
1718}
1719
David Aherndb191702015-03-17 20:23:16 -06001720struct port_name {
1721 char *buf;
1722 size_t len;
1723};
1724
1725static int
Simon Horman534ba6a2015-06-01 13:25:04 +09001726rocker_cmd_get_port_settings_phys_name_proc(const struct rocker_port *rocker_port,
Simon Hormane5054642015-05-25 14:28:36 +09001727 const struct rocker_desc_info *desc_info,
David Aherndb191702015-03-17 20:23:16 -06001728 void *priv)
1729{
Simon Hormane5054642015-05-25 14:28:36 +09001730 const struct rocker_tlv *info_attrs[ROCKER_TLV_CMD_PORT_SETTINGS_MAX + 1];
1731 const struct rocker_tlv *attrs[ROCKER_TLV_CMD_MAX + 1];
David Aherndb191702015-03-17 20:23:16 -06001732 struct port_name *name = priv;
Simon Hormane5054642015-05-25 14:28:36 +09001733 const struct rocker_tlv *attr;
David Aherndb191702015-03-17 20:23:16 -06001734 size_t i, j, len;
Simon Hormane5054642015-05-25 14:28:36 +09001735 const char *str;
David Aherndb191702015-03-17 20:23:16 -06001736
1737 rocker_tlv_parse_desc(attrs, ROCKER_TLV_CMD_MAX, desc_info);
1738 if (!attrs[ROCKER_TLV_CMD_INFO])
1739 return -EIO;
1740
1741 rocker_tlv_parse_nested(info_attrs, ROCKER_TLV_CMD_PORT_SETTINGS_MAX,
1742 attrs[ROCKER_TLV_CMD_INFO]);
1743 attr = info_attrs[ROCKER_TLV_CMD_PORT_SETTINGS_PHYS_NAME];
1744 if (!attr)
1745 return -EIO;
1746
1747 len = min_t(size_t, rocker_tlv_len(attr), name->len);
1748 str = rocker_tlv_data(attr);
1749
1750 /* make sure name only contains alphanumeric characters */
1751 for (i = j = 0; i < len; ++i) {
1752 if (isalnum(str[i])) {
1753 name->buf[j] = str[i];
1754 j++;
1755 }
1756 }
1757
1758 if (j == 0)
1759 return -EIO;
1760
1761 name->buf[j] = '\0';
1762
1763 return 0;
1764}
1765
Jiri Pirko4b8ac962014-11-28 14:34:26 +01001766static int
Simon Horman534ba6a2015-06-01 13:25:04 +09001767rocker_cmd_set_port_settings_ethtool_prep(const struct rocker_port *rocker_port,
Jiri Pirko4b8ac962014-11-28 14:34:26 +01001768 struct rocker_desc_info *desc_info,
1769 void *priv)
1770{
1771 struct ethtool_cmd *ecmd = priv;
1772 struct rocker_tlv *cmd_info;
1773
1774 if (rocker_tlv_put_u16(desc_info, ROCKER_TLV_CMD_TYPE,
1775 ROCKER_TLV_CMD_TYPE_SET_PORT_SETTINGS))
1776 return -EMSGSIZE;
1777 cmd_info = rocker_tlv_nest_start(desc_info, ROCKER_TLV_CMD_INFO);
1778 if (!cmd_info)
1779 return -EMSGSIZE;
Scott Feldman4a6bb6d2015-02-25 20:15:37 -08001780 if (rocker_tlv_put_u32(desc_info, ROCKER_TLV_CMD_PORT_SETTINGS_PPORT,
1781 rocker_port->pport))
Jiri Pirko4b8ac962014-11-28 14:34:26 +01001782 return -EMSGSIZE;
1783 if (rocker_tlv_put_u32(desc_info, ROCKER_TLV_CMD_PORT_SETTINGS_SPEED,
1784 ethtool_cmd_speed(ecmd)))
1785 return -EMSGSIZE;
1786 if (rocker_tlv_put_u8(desc_info, ROCKER_TLV_CMD_PORT_SETTINGS_DUPLEX,
1787 ecmd->duplex))
1788 return -EMSGSIZE;
1789 if (rocker_tlv_put_u8(desc_info, ROCKER_TLV_CMD_PORT_SETTINGS_AUTONEG,
1790 ecmd->autoneg))
1791 return -EMSGSIZE;
1792 rocker_tlv_nest_end(desc_info, cmd_info);
1793 return 0;
1794}
1795
1796static int
Simon Horman534ba6a2015-06-01 13:25:04 +09001797rocker_cmd_set_port_settings_macaddr_prep(const struct rocker_port *rocker_port,
Jiri Pirko4b8ac962014-11-28 14:34:26 +01001798 struct rocker_desc_info *desc_info,
1799 void *priv)
1800{
Simon Hormane5054642015-05-25 14:28:36 +09001801 const unsigned char *macaddr = priv;
Jiri Pirko4b8ac962014-11-28 14:34:26 +01001802 struct rocker_tlv *cmd_info;
1803
1804 if (rocker_tlv_put_u16(desc_info, ROCKER_TLV_CMD_TYPE,
1805 ROCKER_TLV_CMD_TYPE_SET_PORT_SETTINGS))
1806 return -EMSGSIZE;
1807 cmd_info = rocker_tlv_nest_start(desc_info, ROCKER_TLV_CMD_INFO);
1808 if (!cmd_info)
1809 return -EMSGSIZE;
Scott Feldman4a6bb6d2015-02-25 20:15:37 -08001810 if (rocker_tlv_put_u32(desc_info, ROCKER_TLV_CMD_PORT_SETTINGS_PPORT,
1811 rocker_port->pport))
Jiri Pirko4b8ac962014-11-28 14:34:26 +01001812 return -EMSGSIZE;
1813 if (rocker_tlv_put(desc_info, ROCKER_TLV_CMD_PORT_SETTINGS_MACADDR,
1814 ETH_ALEN, macaddr))
1815 return -EMSGSIZE;
1816 rocker_tlv_nest_end(desc_info, cmd_info);
1817 return 0;
1818}
1819
Scott Feldman5111f802014-11-28 14:34:30 +01001820static int
Simon Horman534ba6a2015-06-01 13:25:04 +09001821rocker_cmd_set_port_learning_prep(const struct rocker_port *rocker_port,
Scott Feldman5111f802014-11-28 14:34:30 +01001822 struct rocker_desc_info *desc_info,
1823 void *priv)
1824{
1825 struct rocker_tlv *cmd_info;
1826
1827 if (rocker_tlv_put_u16(desc_info, ROCKER_TLV_CMD_TYPE,
1828 ROCKER_TLV_CMD_TYPE_SET_PORT_SETTINGS))
1829 return -EMSGSIZE;
1830 cmd_info = rocker_tlv_nest_start(desc_info, ROCKER_TLV_CMD_INFO);
1831 if (!cmd_info)
1832 return -EMSGSIZE;
Scott Feldman4a6bb6d2015-02-25 20:15:37 -08001833 if (rocker_tlv_put_u32(desc_info, ROCKER_TLV_CMD_PORT_SETTINGS_PPORT,
1834 rocker_port->pport))
Scott Feldman5111f802014-11-28 14:34:30 +01001835 return -EMSGSIZE;
1836 if (rocker_tlv_put_u8(desc_info, ROCKER_TLV_CMD_PORT_SETTINGS_LEARNING,
1837 !!(rocker_port->brport_flags & BR_LEARNING)))
1838 return -EMSGSIZE;
1839 rocker_tlv_nest_end(desc_info, cmd_info);
1840 return 0;
1841}
1842
Jiri Pirko4b8ac962014-11-28 14:34:26 +01001843static int rocker_cmd_get_port_settings_ethtool(struct rocker_port *rocker_port,
1844 struct ethtool_cmd *ecmd)
1845{
Scott Feldman179f9a22015-06-12 21:35:46 -07001846 return rocker_cmd_exec(rocker_port, SWITCHDEV_TRANS_NONE, 0,
Jiri Pirko4b8ac962014-11-28 14:34:26 +01001847 rocker_cmd_get_port_settings_prep, NULL,
1848 rocker_cmd_get_port_settings_ethtool_proc,
Scott Feldmanc4f20322015-05-10 09:47:50 -07001849 ecmd);
Jiri Pirko4b8ac962014-11-28 14:34:26 +01001850}
1851
1852static int rocker_cmd_get_port_settings_macaddr(struct rocker_port *rocker_port,
1853 unsigned char *macaddr)
1854{
Scott Feldman179f9a22015-06-12 21:35:46 -07001855 return rocker_cmd_exec(rocker_port, SWITCHDEV_TRANS_NONE, 0,
Jiri Pirko4b8ac962014-11-28 14:34:26 +01001856 rocker_cmd_get_port_settings_prep, NULL,
1857 rocker_cmd_get_port_settings_macaddr_proc,
Scott Feldmanc4f20322015-05-10 09:47:50 -07001858 macaddr);
Jiri Pirko4b8ac962014-11-28 14:34:26 +01001859}
1860
1861static int rocker_cmd_set_port_settings_ethtool(struct rocker_port *rocker_port,
1862 struct ethtool_cmd *ecmd)
1863{
Scott Feldman179f9a22015-06-12 21:35:46 -07001864 return rocker_cmd_exec(rocker_port, SWITCHDEV_TRANS_NONE, 0,
Jiri Pirko4b8ac962014-11-28 14:34:26 +01001865 rocker_cmd_set_port_settings_ethtool_prep,
Scott Feldmanc4f20322015-05-10 09:47:50 -07001866 ecmd, NULL, NULL);
Jiri Pirko4b8ac962014-11-28 14:34:26 +01001867}
1868
1869static int rocker_cmd_set_port_settings_macaddr(struct rocker_port *rocker_port,
1870 unsigned char *macaddr)
1871{
Scott Feldman179f9a22015-06-12 21:35:46 -07001872 return rocker_cmd_exec(rocker_port, SWITCHDEV_TRANS_NONE, 0,
Jiri Pirko4b8ac962014-11-28 14:34:26 +01001873 rocker_cmd_set_port_settings_macaddr_prep,
Scott Feldmanc4f20322015-05-10 09:47:50 -07001874 macaddr, NULL, NULL);
Jiri Pirko4b8ac962014-11-28 14:34:26 +01001875}
1876
Scott Feldmanc4f20322015-05-10 09:47:50 -07001877static int rocker_port_set_learning(struct rocker_port *rocker_port,
1878 enum switchdev_trans trans)
Scott Feldman5111f802014-11-28 14:34:30 +01001879{
Scott Feldman179f9a22015-06-12 21:35:46 -07001880 return rocker_cmd_exec(rocker_port, trans, 0,
Scott Feldman5111f802014-11-28 14:34:30 +01001881 rocker_cmd_set_port_learning_prep,
Scott Feldmanc4f20322015-05-10 09:47:50 -07001882 NULL, NULL, NULL);
Scott Feldman5111f802014-11-28 14:34:30 +01001883}
1884
Simon Hormane5054642015-05-25 14:28:36 +09001885static int
1886rocker_cmd_flow_tbl_add_ig_port(struct rocker_desc_info *desc_info,
1887 const struct rocker_flow_tbl_entry *entry)
Scott Feldman9f6bbf72014-11-28 14:34:27 +01001888{
Scott Feldman4a6bb6d2015-02-25 20:15:37 -08001889 if (rocker_tlv_put_u32(desc_info, ROCKER_TLV_OF_DPA_IN_PPORT,
1890 entry->key.ig_port.in_pport))
Scott Feldman9f6bbf72014-11-28 14:34:27 +01001891 return -EMSGSIZE;
Scott Feldman4a6bb6d2015-02-25 20:15:37 -08001892 if (rocker_tlv_put_u32(desc_info, ROCKER_TLV_OF_DPA_IN_PPORT_MASK,
1893 entry->key.ig_port.in_pport_mask))
Scott Feldman9f6bbf72014-11-28 14:34:27 +01001894 return -EMSGSIZE;
1895 if (rocker_tlv_put_u16(desc_info, ROCKER_TLV_OF_DPA_GOTO_TABLE_ID,
1896 entry->key.ig_port.goto_tbl))
1897 return -EMSGSIZE;
1898
1899 return 0;
1900}
1901
Simon Hormane5054642015-05-25 14:28:36 +09001902static int
1903rocker_cmd_flow_tbl_add_vlan(struct rocker_desc_info *desc_info,
1904 const struct rocker_flow_tbl_entry *entry)
Scott Feldman9f6bbf72014-11-28 14:34:27 +01001905{
Scott Feldman4a6bb6d2015-02-25 20:15:37 -08001906 if (rocker_tlv_put_u32(desc_info, ROCKER_TLV_OF_DPA_IN_PPORT,
1907 entry->key.vlan.in_pport))
Scott Feldman9f6bbf72014-11-28 14:34:27 +01001908 return -EMSGSIZE;
Jiri Pirko9b03c712014-12-03 14:14:53 +01001909 if (rocker_tlv_put_be16(desc_info, ROCKER_TLV_OF_DPA_VLAN_ID,
1910 entry->key.vlan.vlan_id))
Scott Feldman9f6bbf72014-11-28 14:34:27 +01001911 return -EMSGSIZE;
Jiri Pirko9b03c712014-12-03 14:14:53 +01001912 if (rocker_tlv_put_be16(desc_info, ROCKER_TLV_OF_DPA_VLAN_ID_MASK,
1913 entry->key.vlan.vlan_id_mask))
Scott Feldman9f6bbf72014-11-28 14:34:27 +01001914 return -EMSGSIZE;
1915 if (rocker_tlv_put_u16(desc_info, ROCKER_TLV_OF_DPA_GOTO_TABLE_ID,
1916 entry->key.vlan.goto_tbl))
1917 return -EMSGSIZE;
1918 if (entry->key.vlan.untagged &&
Jiri Pirko9b03c712014-12-03 14:14:53 +01001919 rocker_tlv_put_be16(desc_info, ROCKER_TLV_OF_DPA_NEW_VLAN_ID,
1920 entry->key.vlan.new_vlan_id))
Scott Feldman9f6bbf72014-11-28 14:34:27 +01001921 return -EMSGSIZE;
1922
1923 return 0;
1924}
1925
Simon Hormane5054642015-05-25 14:28:36 +09001926static int
1927rocker_cmd_flow_tbl_add_term_mac(struct rocker_desc_info *desc_info,
1928 const struct rocker_flow_tbl_entry *entry)
Scott Feldman9f6bbf72014-11-28 14:34:27 +01001929{
Scott Feldman4a6bb6d2015-02-25 20:15:37 -08001930 if (rocker_tlv_put_u32(desc_info, ROCKER_TLV_OF_DPA_IN_PPORT,
1931 entry->key.term_mac.in_pport))
Scott Feldman9f6bbf72014-11-28 14:34:27 +01001932 return -EMSGSIZE;
Scott Feldman4a6bb6d2015-02-25 20:15:37 -08001933 if (rocker_tlv_put_u32(desc_info, ROCKER_TLV_OF_DPA_IN_PPORT_MASK,
1934 entry->key.term_mac.in_pport_mask))
Scott Feldman9f6bbf72014-11-28 14:34:27 +01001935 return -EMSGSIZE;
Jiri Pirko9b03c712014-12-03 14:14:53 +01001936 if (rocker_tlv_put_be16(desc_info, ROCKER_TLV_OF_DPA_ETHERTYPE,
1937 entry->key.term_mac.eth_type))
Scott Feldman9f6bbf72014-11-28 14:34:27 +01001938 return -EMSGSIZE;
1939 if (rocker_tlv_put(desc_info, ROCKER_TLV_OF_DPA_DST_MAC,
1940 ETH_ALEN, entry->key.term_mac.eth_dst))
1941 return -EMSGSIZE;
1942 if (rocker_tlv_put(desc_info, ROCKER_TLV_OF_DPA_DST_MAC_MASK,
1943 ETH_ALEN, entry->key.term_mac.eth_dst_mask))
1944 return -EMSGSIZE;
Jiri Pirko9b03c712014-12-03 14:14:53 +01001945 if (rocker_tlv_put_be16(desc_info, ROCKER_TLV_OF_DPA_VLAN_ID,
1946 entry->key.term_mac.vlan_id))
Scott Feldman9f6bbf72014-11-28 14:34:27 +01001947 return -EMSGSIZE;
Jiri Pirko9b03c712014-12-03 14:14:53 +01001948 if (rocker_tlv_put_be16(desc_info, ROCKER_TLV_OF_DPA_VLAN_ID_MASK,
1949 entry->key.term_mac.vlan_id_mask))
Scott Feldman9f6bbf72014-11-28 14:34:27 +01001950 return -EMSGSIZE;
1951 if (rocker_tlv_put_u16(desc_info, ROCKER_TLV_OF_DPA_GOTO_TABLE_ID,
1952 entry->key.term_mac.goto_tbl))
1953 return -EMSGSIZE;
1954 if (entry->key.term_mac.copy_to_cpu &&
1955 rocker_tlv_put_u8(desc_info, ROCKER_TLV_OF_DPA_COPY_CPU_ACTION,
1956 entry->key.term_mac.copy_to_cpu))
1957 return -EMSGSIZE;
1958
1959 return 0;
1960}
1961
1962static int
1963rocker_cmd_flow_tbl_add_ucast_routing(struct rocker_desc_info *desc_info,
Simon Hormane5054642015-05-25 14:28:36 +09001964 const struct rocker_flow_tbl_entry *entry)
Scott Feldman9f6bbf72014-11-28 14:34:27 +01001965{
Jiri Pirko9b03c712014-12-03 14:14:53 +01001966 if (rocker_tlv_put_be16(desc_info, ROCKER_TLV_OF_DPA_ETHERTYPE,
1967 entry->key.ucast_routing.eth_type))
Scott Feldman9f6bbf72014-11-28 14:34:27 +01001968 return -EMSGSIZE;
Jiri Pirko9b03c712014-12-03 14:14:53 +01001969 if (rocker_tlv_put_be32(desc_info, ROCKER_TLV_OF_DPA_DST_IP,
1970 entry->key.ucast_routing.dst4))
Scott Feldman9f6bbf72014-11-28 14:34:27 +01001971 return -EMSGSIZE;
Jiri Pirko9b03c712014-12-03 14:14:53 +01001972 if (rocker_tlv_put_be32(desc_info, ROCKER_TLV_OF_DPA_DST_IP_MASK,
1973 entry->key.ucast_routing.dst4_mask))
Scott Feldman9f6bbf72014-11-28 14:34:27 +01001974 return -EMSGSIZE;
1975 if (rocker_tlv_put_u16(desc_info, ROCKER_TLV_OF_DPA_GOTO_TABLE_ID,
1976 entry->key.ucast_routing.goto_tbl))
1977 return -EMSGSIZE;
1978 if (rocker_tlv_put_u32(desc_info, ROCKER_TLV_OF_DPA_GROUP_ID,
1979 entry->key.ucast_routing.group_id))
1980 return -EMSGSIZE;
1981
1982 return 0;
1983}
1984
Simon Hormane5054642015-05-25 14:28:36 +09001985static int
1986rocker_cmd_flow_tbl_add_bridge(struct rocker_desc_info *desc_info,
1987 const struct rocker_flow_tbl_entry *entry)
Scott Feldman9f6bbf72014-11-28 14:34:27 +01001988{
1989 if (entry->key.bridge.has_eth_dst &&
1990 rocker_tlv_put(desc_info, ROCKER_TLV_OF_DPA_DST_MAC,
1991 ETH_ALEN, entry->key.bridge.eth_dst))
1992 return -EMSGSIZE;
1993 if (entry->key.bridge.has_eth_dst_mask &&
1994 rocker_tlv_put(desc_info, ROCKER_TLV_OF_DPA_DST_MAC_MASK,
1995 ETH_ALEN, entry->key.bridge.eth_dst_mask))
1996 return -EMSGSIZE;
1997 if (entry->key.bridge.vlan_id &&
Jiri Pirko9b03c712014-12-03 14:14:53 +01001998 rocker_tlv_put_be16(desc_info, ROCKER_TLV_OF_DPA_VLAN_ID,
1999 entry->key.bridge.vlan_id))
Scott Feldman9f6bbf72014-11-28 14:34:27 +01002000 return -EMSGSIZE;
2001 if (entry->key.bridge.tunnel_id &&
2002 rocker_tlv_put_u32(desc_info, ROCKER_TLV_OF_DPA_TUNNEL_ID,
2003 entry->key.bridge.tunnel_id))
2004 return -EMSGSIZE;
2005 if (rocker_tlv_put_u16(desc_info, ROCKER_TLV_OF_DPA_GOTO_TABLE_ID,
2006 entry->key.bridge.goto_tbl))
2007 return -EMSGSIZE;
2008 if (rocker_tlv_put_u32(desc_info, ROCKER_TLV_OF_DPA_GROUP_ID,
2009 entry->key.bridge.group_id))
2010 return -EMSGSIZE;
2011 if (entry->key.bridge.copy_to_cpu &&
2012 rocker_tlv_put_u8(desc_info, ROCKER_TLV_OF_DPA_COPY_CPU_ACTION,
2013 entry->key.bridge.copy_to_cpu))
2014 return -EMSGSIZE;
2015
2016 return 0;
2017}
2018
Simon Hormane5054642015-05-25 14:28:36 +09002019static int
2020rocker_cmd_flow_tbl_add_acl(struct rocker_desc_info *desc_info,
2021 const struct rocker_flow_tbl_entry *entry)
Scott Feldman9f6bbf72014-11-28 14:34:27 +01002022{
Scott Feldman4a6bb6d2015-02-25 20:15:37 -08002023 if (rocker_tlv_put_u32(desc_info, ROCKER_TLV_OF_DPA_IN_PPORT,
2024 entry->key.acl.in_pport))
Scott Feldman9f6bbf72014-11-28 14:34:27 +01002025 return -EMSGSIZE;
Scott Feldman4a6bb6d2015-02-25 20:15:37 -08002026 if (rocker_tlv_put_u32(desc_info, ROCKER_TLV_OF_DPA_IN_PPORT_MASK,
2027 entry->key.acl.in_pport_mask))
Scott Feldman9f6bbf72014-11-28 14:34:27 +01002028 return -EMSGSIZE;
2029 if (rocker_tlv_put(desc_info, ROCKER_TLV_OF_DPA_SRC_MAC,
2030 ETH_ALEN, entry->key.acl.eth_src))
2031 return -EMSGSIZE;
2032 if (rocker_tlv_put(desc_info, ROCKER_TLV_OF_DPA_SRC_MAC_MASK,
2033 ETH_ALEN, entry->key.acl.eth_src_mask))
2034 return -EMSGSIZE;
2035 if (rocker_tlv_put(desc_info, ROCKER_TLV_OF_DPA_DST_MAC,
2036 ETH_ALEN, entry->key.acl.eth_dst))
2037 return -EMSGSIZE;
2038 if (rocker_tlv_put(desc_info, ROCKER_TLV_OF_DPA_DST_MAC_MASK,
2039 ETH_ALEN, entry->key.acl.eth_dst_mask))
2040 return -EMSGSIZE;
Jiri Pirko9b03c712014-12-03 14:14:53 +01002041 if (rocker_tlv_put_be16(desc_info, ROCKER_TLV_OF_DPA_ETHERTYPE,
2042 entry->key.acl.eth_type))
Scott Feldman9f6bbf72014-11-28 14:34:27 +01002043 return -EMSGSIZE;
Jiri Pirko9b03c712014-12-03 14:14:53 +01002044 if (rocker_tlv_put_be16(desc_info, ROCKER_TLV_OF_DPA_VLAN_ID,
2045 entry->key.acl.vlan_id))
Scott Feldman9f6bbf72014-11-28 14:34:27 +01002046 return -EMSGSIZE;
Jiri Pirko9b03c712014-12-03 14:14:53 +01002047 if (rocker_tlv_put_be16(desc_info, ROCKER_TLV_OF_DPA_VLAN_ID_MASK,
2048 entry->key.acl.vlan_id_mask))
Scott Feldman9f6bbf72014-11-28 14:34:27 +01002049 return -EMSGSIZE;
2050
2051 switch (ntohs(entry->key.acl.eth_type)) {
2052 case ETH_P_IP:
2053 case ETH_P_IPV6:
2054 if (rocker_tlv_put_u8(desc_info, ROCKER_TLV_OF_DPA_IP_PROTO,
2055 entry->key.acl.ip_proto))
2056 return -EMSGSIZE;
2057 if (rocker_tlv_put_u8(desc_info,
2058 ROCKER_TLV_OF_DPA_IP_PROTO_MASK,
2059 entry->key.acl.ip_proto_mask))
2060 return -EMSGSIZE;
2061 if (rocker_tlv_put_u8(desc_info, ROCKER_TLV_OF_DPA_IP_DSCP,
2062 entry->key.acl.ip_tos & 0x3f))
2063 return -EMSGSIZE;
2064 if (rocker_tlv_put_u8(desc_info,
2065 ROCKER_TLV_OF_DPA_IP_DSCP_MASK,
2066 entry->key.acl.ip_tos_mask & 0x3f))
2067 return -EMSGSIZE;
2068 if (rocker_tlv_put_u8(desc_info, ROCKER_TLV_OF_DPA_IP_ECN,
2069 (entry->key.acl.ip_tos & 0xc0) >> 6))
2070 return -EMSGSIZE;
2071 if (rocker_tlv_put_u8(desc_info,
2072 ROCKER_TLV_OF_DPA_IP_ECN_MASK,
2073 (entry->key.acl.ip_tos_mask & 0xc0) >> 6))
2074 return -EMSGSIZE;
2075 break;
2076 }
2077
2078 if (entry->key.acl.group_id != ROCKER_GROUP_NONE &&
2079 rocker_tlv_put_u32(desc_info, ROCKER_TLV_OF_DPA_GROUP_ID,
2080 entry->key.acl.group_id))
2081 return -EMSGSIZE;
2082
2083 return 0;
2084}
2085
Simon Horman534ba6a2015-06-01 13:25:04 +09002086static int rocker_cmd_flow_tbl_add(const struct rocker_port *rocker_port,
Scott Feldman9f6bbf72014-11-28 14:34:27 +01002087 struct rocker_desc_info *desc_info,
2088 void *priv)
2089{
Simon Hormane5054642015-05-25 14:28:36 +09002090 const struct rocker_flow_tbl_entry *entry = priv;
Scott Feldman9f6bbf72014-11-28 14:34:27 +01002091 struct rocker_tlv *cmd_info;
2092 int err = 0;
2093
Scott Feldmanc1beeef2015-03-05 21:21:20 -08002094 if (rocker_tlv_put_u16(desc_info, ROCKER_TLV_CMD_TYPE, entry->cmd))
Scott Feldman9f6bbf72014-11-28 14:34:27 +01002095 return -EMSGSIZE;
2096 cmd_info = rocker_tlv_nest_start(desc_info, ROCKER_TLV_CMD_INFO);
2097 if (!cmd_info)
2098 return -EMSGSIZE;
2099 if (rocker_tlv_put_u16(desc_info, ROCKER_TLV_OF_DPA_TABLE_ID,
2100 entry->key.tbl_id))
2101 return -EMSGSIZE;
2102 if (rocker_tlv_put_u32(desc_info, ROCKER_TLV_OF_DPA_PRIORITY,
2103 entry->key.priority))
2104 return -EMSGSIZE;
2105 if (rocker_tlv_put_u32(desc_info, ROCKER_TLV_OF_DPA_HARDTIME, 0))
2106 return -EMSGSIZE;
2107 if (rocker_tlv_put_u64(desc_info, ROCKER_TLV_OF_DPA_COOKIE,
2108 entry->cookie))
2109 return -EMSGSIZE;
2110
2111 switch (entry->key.tbl_id) {
2112 case ROCKER_OF_DPA_TABLE_ID_INGRESS_PORT:
2113 err = rocker_cmd_flow_tbl_add_ig_port(desc_info, entry);
2114 break;
2115 case ROCKER_OF_DPA_TABLE_ID_VLAN:
2116 err = rocker_cmd_flow_tbl_add_vlan(desc_info, entry);
2117 break;
2118 case ROCKER_OF_DPA_TABLE_ID_TERMINATION_MAC:
2119 err = rocker_cmd_flow_tbl_add_term_mac(desc_info, entry);
2120 break;
2121 case ROCKER_OF_DPA_TABLE_ID_UNICAST_ROUTING:
2122 err = rocker_cmd_flow_tbl_add_ucast_routing(desc_info, entry);
2123 break;
2124 case ROCKER_OF_DPA_TABLE_ID_BRIDGING:
2125 err = rocker_cmd_flow_tbl_add_bridge(desc_info, entry);
2126 break;
2127 case ROCKER_OF_DPA_TABLE_ID_ACL_POLICY:
2128 err = rocker_cmd_flow_tbl_add_acl(desc_info, entry);
2129 break;
2130 default:
2131 err = -ENOTSUPP;
2132 break;
2133 }
2134
2135 if (err)
2136 return err;
2137
2138 rocker_tlv_nest_end(desc_info, cmd_info);
2139
2140 return 0;
2141}
2142
Simon Horman534ba6a2015-06-01 13:25:04 +09002143static int rocker_cmd_flow_tbl_del(const struct rocker_port *rocker_port,
Scott Feldman9f6bbf72014-11-28 14:34:27 +01002144 struct rocker_desc_info *desc_info,
2145 void *priv)
2146{
2147 const struct rocker_flow_tbl_entry *entry = priv;
2148 struct rocker_tlv *cmd_info;
2149
Scott Feldmanc1beeef2015-03-05 21:21:20 -08002150 if (rocker_tlv_put_u16(desc_info, ROCKER_TLV_CMD_TYPE, entry->cmd))
Scott Feldman9f6bbf72014-11-28 14:34:27 +01002151 return -EMSGSIZE;
2152 cmd_info = rocker_tlv_nest_start(desc_info, ROCKER_TLV_CMD_INFO);
2153 if (!cmd_info)
2154 return -EMSGSIZE;
2155 if (rocker_tlv_put_u64(desc_info, ROCKER_TLV_OF_DPA_COOKIE,
2156 entry->cookie))
2157 return -EMSGSIZE;
2158 rocker_tlv_nest_end(desc_info, cmd_info);
2159
2160 return 0;
2161}
2162
2163static int
2164rocker_cmd_group_tbl_add_l2_interface(struct rocker_desc_info *desc_info,
2165 struct rocker_group_tbl_entry *entry)
2166{
Scott Feldman4a6bb6d2015-02-25 20:15:37 -08002167 if (rocker_tlv_put_u32(desc_info, ROCKER_TLV_OF_DPA_OUT_PPORT,
Scott Feldman9f6bbf72014-11-28 14:34:27 +01002168 ROCKER_GROUP_PORT_GET(entry->group_id)))
2169 return -EMSGSIZE;
2170 if (rocker_tlv_put_u8(desc_info, ROCKER_TLV_OF_DPA_POP_VLAN,
2171 entry->l2_interface.pop_vlan))
2172 return -EMSGSIZE;
2173
2174 return 0;
2175}
2176
2177static int
2178rocker_cmd_group_tbl_add_l2_rewrite(struct rocker_desc_info *desc_info,
Simon Hormane5054642015-05-25 14:28:36 +09002179 const struct rocker_group_tbl_entry *entry)
Scott Feldman9f6bbf72014-11-28 14:34:27 +01002180{
2181 if (rocker_tlv_put_u32(desc_info, ROCKER_TLV_OF_DPA_GROUP_ID_LOWER,
2182 entry->l2_rewrite.group_id))
2183 return -EMSGSIZE;
2184 if (!is_zero_ether_addr(entry->l2_rewrite.eth_src) &&
2185 rocker_tlv_put(desc_info, ROCKER_TLV_OF_DPA_SRC_MAC,
2186 ETH_ALEN, entry->l2_rewrite.eth_src))
2187 return -EMSGSIZE;
2188 if (!is_zero_ether_addr(entry->l2_rewrite.eth_dst) &&
2189 rocker_tlv_put(desc_info, ROCKER_TLV_OF_DPA_DST_MAC,
2190 ETH_ALEN, entry->l2_rewrite.eth_dst))
2191 return -EMSGSIZE;
2192 if (entry->l2_rewrite.vlan_id &&
Jiri Pirko9b03c712014-12-03 14:14:53 +01002193 rocker_tlv_put_be16(desc_info, ROCKER_TLV_OF_DPA_VLAN_ID,
2194 entry->l2_rewrite.vlan_id))
Scott Feldman9f6bbf72014-11-28 14:34:27 +01002195 return -EMSGSIZE;
2196
2197 return 0;
2198}
2199
2200static int
2201rocker_cmd_group_tbl_add_group_ids(struct rocker_desc_info *desc_info,
Simon Hormane5054642015-05-25 14:28:36 +09002202 const struct rocker_group_tbl_entry *entry)
Scott Feldman9f6bbf72014-11-28 14:34:27 +01002203{
2204 int i;
2205 struct rocker_tlv *group_ids;
2206
2207 if (rocker_tlv_put_u16(desc_info, ROCKER_TLV_OF_DPA_GROUP_COUNT,
2208 entry->group_count))
2209 return -EMSGSIZE;
2210
2211 group_ids = rocker_tlv_nest_start(desc_info,
2212 ROCKER_TLV_OF_DPA_GROUP_IDS);
2213 if (!group_ids)
2214 return -EMSGSIZE;
2215
2216 for (i = 0; i < entry->group_count; i++)
2217 /* Note TLV array is 1-based */
2218 if (rocker_tlv_put_u32(desc_info, i + 1, entry->group_ids[i]))
2219 return -EMSGSIZE;
2220
2221 rocker_tlv_nest_end(desc_info, group_ids);
2222
2223 return 0;
2224}
2225
2226static int
2227rocker_cmd_group_tbl_add_l3_unicast(struct rocker_desc_info *desc_info,
Simon Hormane5054642015-05-25 14:28:36 +09002228 const struct rocker_group_tbl_entry *entry)
Scott Feldman9f6bbf72014-11-28 14:34:27 +01002229{
2230 if (!is_zero_ether_addr(entry->l3_unicast.eth_src) &&
2231 rocker_tlv_put(desc_info, ROCKER_TLV_OF_DPA_SRC_MAC,
2232 ETH_ALEN, entry->l3_unicast.eth_src))
2233 return -EMSGSIZE;
2234 if (!is_zero_ether_addr(entry->l3_unicast.eth_dst) &&
2235 rocker_tlv_put(desc_info, ROCKER_TLV_OF_DPA_DST_MAC,
2236 ETH_ALEN, entry->l3_unicast.eth_dst))
2237 return -EMSGSIZE;
2238 if (entry->l3_unicast.vlan_id &&
Jiri Pirko9b03c712014-12-03 14:14:53 +01002239 rocker_tlv_put_be16(desc_info, ROCKER_TLV_OF_DPA_VLAN_ID,
2240 entry->l3_unicast.vlan_id))
Scott Feldman9f6bbf72014-11-28 14:34:27 +01002241 return -EMSGSIZE;
2242 if (rocker_tlv_put_u8(desc_info, ROCKER_TLV_OF_DPA_TTL_CHECK,
2243 entry->l3_unicast.ttl_check))
2244 return -EMSGSIZE;
2245 if (rocker_tlv_put_u32(desc_info, ROCKER_TLV_OF_DPA_GROUP_ID_LOWER,
2246 entry->l3_unicast.group_id))
2247 return -EMSGSIZE;
2248
2249 return 0;
2250}
2251
Simon Horman534ba6a2015-06-01 13:25:04 +09002252static int rocker_cmd_group_tbl_add(const struct rocker_port *rocker_port,
Scott Feldman9f6bbf72014-11-28 14:34:27 +01002253 struct rocker_desc_info *desc_info,
2254 void *priv)
2255{
2256 struct rocker_group_tbl_entry *entry = priv;
2257 struct rocker_tlv *cmd_info;
2258 int err = 0;
2259
2260 if (rocker_tlv_put_u16(desc_info, ROCKER_TLV_CMD_TYPE, entry->cmd))
2261 return -EMSGSIZE;
2262 cmd_info = rocker_tlv_nest_start(desc_info, ROCKER_TLV_CMD_INFO);
2263 if (!cmd_info)
2264 return -EMSGSIZE;
2265
2266 if (rocker_tlv_put_u32(desc_info, ROCKER_TLV_OF_DPA_GROUP_ID,
2267 entry->group_id))
2268 return -EMSGSIZE;
2269
2270 switch (ROCKER_GROUP_TYPE_GET(entry->group_id)) {
2271 case ROCKER_OF_DPA_GROUP_TYPE_L2_INTERFACE:
2272 err = rocker_cmd_group_tbl_add_l2_interface(desc_info, entry);
2273 break;
2274 case ROCKER_OF_DPA_GROUP_TYPE_L2_REWRITE:
2275 err = rocker_cmd_group_tbl_add_l2_rewrite(desc_info, entry);
2276 break;
2277 case ROCKER_OF_DPA_GROUP_TYPE_L2_FLOOD:
2278 case ROCKER_OF_DPA_GROUP_TYPE_L2_MCAST:
2279 err = rocker_cmd_group_tbl_add_group_ids(desc_info, entry);
2280 break;
2281 case ROCKER_OF_DPA_GROUP_TYPE_L3_UCAST:
2282 err = rocker_cmd_group_tbl_add_l3_unicast(desc_info, entry);
2283 break;
2284 default:
2285 err = -ENOTSUPP;
2286 break;
2287 }
2288
2289 if (err)
2290 return err;
2291
2292 rocker_tlv_nest_end(desc_info, cmd_info);
2293
2294 return 0;
2295}
2296
Simon Horman534ba6a2015-06-01 13:25:04 +09002297static int rocker_cmd_group_tbl_del(const struct rocker_port *rocker_port,
Scott Feldman9f6bbf72014-11-28 14:34:27 +01002298 struct rocker_desc_info *desc_info,
2299 void *priv)
2300{
2301 const struct rocker_group_tbl_entry *entry = priv;
2302 struct rocker_tlv *cmd_info;
2303
2304 if (rocker_tlv_put_u16(desc_info, ROCKER_TLV_CMD_TYPE, entry->cmd))
2305 return -EMSGSIZE;
2306 cmd_info = rocker_tlv_nest_start(desc_info, ROCKER_TLV_CMD_INFO);
2307 if (!cmd_info)
2308 return -EMSGSIZE;
2309 if (rocker_tlv_put_u32(desc_info, ROCKER_TLV_OF_DPA_GROUP_ID,
2310 entry->group_id))
2311 return -EMSGSIZE;
2312 rocker_tlv_nest_end(desc_info, cmd_info);
2313
2314 return 0;
2315}
2316
Scott Feldmanc1beeef2015-03-05 21:21:20 -08002317/***************************************************
2318 * Flow, group, FDB, internal VLAN and neigh tables
2319 ***************************************************/
Scott Feldman9f6bbf72014-11-28 14:34:27 +01002320
2321static int rocker_init_tbls(struct rocker *rocker)
2322{
2323 hash_init(rocker->flow_tbl);
2324 spin_lock_init(&rocker->flow_tbl_lock);
2325
2326 hash_init(rocker->group_tbl);
2327 spin_lock_init(&rocker->group_tbl_lock);
2328
2329 hash_init(rocker->fdb_tbl);
2330 spin_lock_init(&rocker->fdb_tbl_lock);
2331
2332 hash_init(rocker->internal_vlan_tbl);
2333 spin_lock_init(&rocker->internal_vlan_tbl_lock);
2334
Scott Feldmanc1beeef2015-03-05 21:21:20 -08002335 hash_init(rocker->neigh_tbl);
2336 spin_lock_init(&rocker->neigh_tbl_lock);
2337
Scott Feldman9f6bbf72014-11-28 14:34:27 +01002338 return 0;
2339}
2340
2341static void rocker_free_tbls(struct rocker *rocker)
2342{
2343 unsigned long flags;
2344 struct rocker_flow_tbl_entry *flow_entry;
2345 struct rocker_group_tbl_entry *group_entry;
2346 struct rocker_fdb_tbl_entry *fdb_entry;
2347 struct rocker_internal_vlan_tbl_entry *internal_vlan_entry;
Scott Feldmanc1beeef2015-03-05 21:21:20 -08002348 struct rocker_neigh_tbl_entry *neigh_entry;
Scott Feldman9f6bbf72014-11-28 14:34:27 +01002349 struct hlist_node *tmp;
2350 int bkt;
2351
2352 spin_lock_irqsave(&rocker->flow_tbl_lock, flags);
2353 hash_for_each_safe(rocker->flow_tbl, bkt, tmp, flow_entry, entry)
2354 hash_del(&flow_entry->entry);
2355 spin_unlock_irqrestore(&rocker->flow_tbl_lock, flags);
2356
2357 spin_lock_irqsave(&rocker->group_tbl_lock, flags);
2358 hash_for_each_safe(rocker->group_tbl, bkt, tmp, group_entry, entry)
2359 hash_del(&group_entry->entry);
2360 spin_unlock_irqrestore(&rocker->group_tbl_lock, flags);
2361
2362 spin_lock_irqsave(&rocker->fdb_tbl_lock, flags);
2363 hash_for_each_safe(rocker->fdb_tbl, bkt, tmp, fdb_entry, entry)
2364 hash_del(&fdb_entry->entry);
2365 spin_unlock_irqrestore(&rocker->fdb_tbl_lock, flags);
2366
2367 spin_lock_irqsave(&rocker->internal_vlan_tbl_lock, flags);
2368 hash_for_each_safe(rocker->internal_vlan_tbl, bkt,
2369 tmp, internal_vlan_entry, entry)
2370 hash_del(&internal_vlan_entry->entry);
2371 spin_unlock_irqrestore(&rocker->internal_vlan_tbl_lock, flags);
Scott Feldmanc1beeef2015-03-05 21:21:20 -08002372
2373 spin_lock_irqsave(&rocker->neigh_tbl_lock, flags);
2374 hash_for_each_safe(rocker->neigh_tbl, bkt, tmp, neigh_entry, entry)
2375 hash_del(&neigh_entry->entry);
2376 spin_unlock_irqrestore(&rocker->neigh_tbl_lock, flags);
Scott Feldman9f6bbf72014-11-28 14:34:27 +01002377}
2378
2379static struct rocker_flow_tbl_entry *
Simon Hormane5054642015-05-25 14:28:36 +09002380rocker_flow_tbl_find(const struct rocker *rocker,
2381 const struct rocker_flow_tbl_entry *match)
Scott Feldman9f6bbf72014-11-28 14:34:27 +01002382{
2383 struct rocker_flow_tbl_entry *found;
Scott Feldmanc1beeef2015-03-05 21:21:20 -08002384 size_t key_len = match->key_len ? match->key_len : sizeof(found->key);
Scott Feldman9f6bbf72014-11-28 14:34:27 +01002385
2386 hash_for_each_possible(rocker->flow_tbl, found,
2387 entry, match->key_crc32) {
Scott Feldmanc1beeef2015-03-05 21:21:20 -08002388 if (memcmp(&found->key, &match->key, key_len) == 0)
Scott Feldman9f6bbf72014-11-28 14:34:27 +01002389 return found;
2390 }
2391
2392 return NULL;
2393}
2394
2395static int rocker_flow_tbl_add(struct rocker_port *rocker_port,
Scott Feldman179f9a22015-06-12 21:35:46 -07002396 enum switchdev_trans trans, int flags,
Scott Feldmanc4f20322015-05-10 09:47:50 -07002397 struct rocker_flow_tbl_entry *match)
Scott Feldman9f6bbf72014-11-28 14:34:27 +01002398{
2399 struct rocker *rocker = rocker_port->rocker;
2400 struct rocker_flow_tbl_entry *found;
Scott Feldmanc1beeef2015-03-05 21:21:20 -08002401 size_t key_len = match->key_len ? match->key_len : sizeof(found->key);
Scott Feldman179f9a22015-06-12 21:35:46 -07002402 unsigned long lock_flags;
Scott Feldman9f6bbf72014-11-28 14:34:27 +01002403
Scott Feldmanc1beeef2015-03-05 21:21:20 -08002404 match->key_crc32 = crc32(~0, &match->key, key_len);
Scott Feldman9f6bbf72014-11-28 14:34:27 +01002405
Scott Feldman179f9a22015-06-12 21:35:46 -07002406 spin_lock_irqsave(&rocker->flow_tbl_lock, lock_flags);
Scott Feldman9f6bbf72014-11-28 14:34:27 +01002407
2408 found = rocker_flow_tbl_find(rocker, match);
2409
2410 if (found) {
Scott Feldmanc1beeef2015-03-05 21:21:20 -08002411 match->cookie = found->cookie;
Scott Feldmanc4f20322015-05-10 09:47:50 -07002412 if (trans != SWITCHDEV_TRANS_PREPARE)
2413 hash_del(&found->entry);
Simon Horman0985df72015-05-25 14:28:35 +09002414 rocker_port_kfree(trans, found);
Scott Feldmanc1beeef2015-03-05 21:21:20 -08002415 found = match;
2416 found->cmd = ROCKER_TLV_CMD_TYPE_OF_DPA_FLOW_MOD;
Scott Feldman9f6bbf72014-11-28 14:34:27 +01002417 } else {
2418 found = match;
2419 found->cookie = rocker->flow_tbl_next_cookie++;
Scott Feldmanc1beeef2015-03-05 21:21:20 -08002420 found->cmd = ROCKER_TLV_CMD_TYPE_OF_DPA_FLOW_ADD;
Scott Feldman9f6bbf72014-11-28 14:34:27 +01002421 }
2422
Scott Feldmanc4f20322015-05-10 09:47:50 -07002423 if (trans != SWITCHDEV_TRANS_PREPARE)
2424 hash_add(rocker->flow_tbl, &found->entry, found->key_crc32);
Scott Feldman9f6bbf72014-11-28 14:34:27 +01002425
Scott Feldman179f9a22015-06-12 21:35:46 -07002426 spin_unlock_irqrestore(&rocker->flow_tbl_lock, lock_flags);
Scott Feldman9f6bbf72014-11-28 14:34:27 +01002427
Scott Feldman179f9a22015-06-12 21:35:46 -07002428 return rocker_cmd_exec(rocker_port, trans, flags,
2429 rocker_cmd_flow_tbl_add, found, NULL, NULL);
Scott Feldman9f6bbf72014-11-28 14:34:27 +01002430}
2431
2432static int rocker_flow_tbl_del(struct rocker_port *rocker_port,
Scott Feldman179f9a22015-06-12 21:35:46 -07002433 enum switchdev_trans trans, int flags,
Scott Feldmanc4f20322015-05-10 09:47:50 -07002434 struct rocker_flow_tbl_entry *match)
Scott Feldman9f6bbf72014-11-28 14:34:27 +01002435{
2436 struct rocker *rocker = rocker_port->rocker;
2437 struct rocker_flow_tbl_entry *found;
Scott Feldmanc1beeef2015-03-05 21:21:20 -08002438 size_t key_len = match->key_len ? match->key_len : sizeof(found->key);
Scott Feldman179f9a22015-06-12 21:35:46 -07002439 unsigned long lock_flags;
Scott Feldman9f6bbf72014-11-28 14:34:27 +01002440 int err = 0;
2441
Scott Feldmanc1beeef2015-03-05 21:21:20 -08002442 match->key_crc32 = crc32(~0, &match->key, key_len);
Scott Feldman9f6bbf72014-11-28 14:34:27 +01002443
Scott Feldman179f9a22015-06-12 21:35:46 -07002444 spin_lock_irqsave(&rocker->flow_tbl_lock, lock_flags);
Scott Feldman9f6bbf72014-11-28 14:34:27 +01002445
2446 found = rocker_flow_tbl_find(rocker, match);
2447
2448 if (found) {
Scott Feldmanc4f20322015-05-10 09:47:50 -07002449 if (trans != SWITCHDEV_TRANS_PREPARE)
2450 hash_del(&found->entry);
Scott Feldmanc1beeef2015-03-05 21:21:20 -08002451 found->cmd = ROCKER_TLV_CMD_TYPE_OF_DPA_FLOW_DEL;
Scott Feldman9f6bbf72014-11-28 14:34:27 +01002452 }
2453
Scott Feldman179f9a22015-06-12 21:35:46 -07002454 spin_unlock_irqrestore(&rocker->flow_tbl_lock, lock_flags);
Scott Feldman9f6bbf72014-11-28 14:34:27 +01002455
Simon Horman0985df72015-05-25 14:28:35 +09002456 rocker_port_kfree(trans, match);
Scott Feldman9f6bbf72014-11-28 14:34:27 +01002457
Scott Feldmanc1beeef2015-03-05 21:21:20 -08002458 if (found) {
Scott Feldman179f9a22015-06-12 21:35:46 -07002459 err = rocker_cmd_exec(rocker_port, trans, flags,
Scott Feldman9f6bbf72014-11-28 14:34:27 +01002460 rocker_cmd_flow_tbl_del,
Scott Feldmanc4f20322015-05-10 09:47:50 -07002461 found, NULL, NULL);
Simon Horman0985df72015-05-25 14:28:35 +09002462 rocker_port_kfree(trans, found);
Scott Feldman9f6bbf72014-11-28 14:34:27 +01002463 }
2464
2465 return err;
2466}
2467
Scott Feldman9f6bbf72014-11-28 14:34:27 +01002468static int rocker_flow_tbl_do(struct rocker_port *rocker_port,
Scott Feldmanc4f20322015-05-10 09:47:50 -07002469 enum switchdev_trans trans, int flags,
2470 struct rocker_flow_tbl_entry *entry)
Scott Feldman9f6bbf72014-11-28 14:34:27 +01002471{
Scott Feldman9f6bbf72014-11-28 14:34:27 +01002472 if (flags & ROCKER_OP_FLAG_REMOVE)
Scott Feldman179f9a22015-06-12 21:35:46 -07002473 return rocker_flow_tbl_del(rocker_port, trans, flags, entry);
Scott Feldman9f6bbf72014-11-28 14:34:27 +01002474 else
Scott Feldman179f9a22015-06-12 21:35:46 -07002475 return rocker_flow_tbl_add(rocker_port, trans, flags, entry);
Scott Feldman9f6bbf72014-11-28 14:34:27 +01002476}
2477
2478static int rocker_flow_tbl_ig_port(struct rocker_port *rocker_port,
Scott Feldmanc4f20322015-05-10 09:47:50 -07002479 enum switchdev_trans trans, int flags,
2480 u32 in_pport, u32 in_pport_mask,
Scott Feldman9f6bbf72014-11-28 14:34:27 +01002481 enum rocker_of_dpa_table_id goto_tbl)
2482{
2483 struct rocker_flow_tbl_entry *entry;
2484
Scott Feldman179f9a22015-06-12 21:35:46 -07002485 entry = rocker_port_kzalloc(rocker_port, trans, flags, sizeof(*entry));
Scott Feldman9f6bbf72014-11-28 14:34:27 +01002486 if (!entry)
2487 return -ENOMEM;
2488
2489 entry->key.priority = ROCKER_PRIORITY_IG_PORT;
2490 entry->key.tbl_id = ROCKER_OF_DPA_TABLE_ID_INGRESS_PORT;
Scott Feldman4a6bb6d2015-02-25 20:15:37 -08002491 entry->key.ig_port.in_pport = in_pport;
2492 entry->key.ig_port.in_pport_mask = in_pport_mask;
Scott Feldman9f6bbf72014-11-28 14:34:27 +01002493 entry->key.ig_port.goto_tbl = goto_tbl;
2494
Scott Feldmanc4f20322015-05-10 09:47:50 -07002495 return rocker_flow_tbl_do(rocker_port, trans, flags, entry);
Scott Feldman9f6bbf72014-11-28 14:34:27 +01002496}
2497
2498static int rocker_flow_tbl_vlan(struct rocker_port *rocker_port,
Scott Feldmanc4f20322015-05-10 09:47:50 -07002499 enum switchdev_trans trans, int flags,
2500 u32 in_pport, __be16 vlan_id,
2501 __be16 vlan_id_mask,
Scott Feldman9f6bbf72014-11-28 14:34:27 +01002502 enum rocker_of_dpa_table_id goto_tbl,
2503 bool untagged, __be16 new_vlan_id)
2504{
2505 struct rocker_flow_tbl_entry *entry;
2506
Scott Feldman179f9a22015-06-12 21:35:46 -07002507 entry = rocker_port_kzalloc(rocker_port, trans, flags, sizeof(*entry));
Scott Feldman9f6bbf72014-11-28 14:34:27 +01002508 if (!entry)
2509 return -ENOMEM;
2510
2511 entry->key.priority = ROCKER_PRIORITY_VLAN;
2512 entry->key.tbl_id = ROCKER_OF_DPA_TABLE_ID_VLAN;
Scott Feldman4a6bb6d2015-02-25 20:15:37 -08002513 entry->key.vlan.in_pport = in_pport;
Scott Feldman9f6bbf72014-11-28 14:34:27 +01002514 entry->key.vlan.vlan_id = vlan_id;
2515 entry->key.vlan.vlan_id_mask = vlan_id_mask;
2516 entry->key.vlan.goto_tbl = goto_tbl;
2517
2518 entry->key.vlan.untagged = untagged;
2519 entry->key.vlan.new_vlan_id = new_vlan_id;
2520
Scott Feldmanc4f20322015-05-10 09:47:50 -07002521 return rocker_flow_tbl_do(rocker_port, trans, flags, entry);
Scott Feldman9f6bbf72014-11-28 14:34:27 +01002522}
2523
2524static int rocker_flow_tbl_term_mac(struct rocker_port *rocker_port,
Scott Feldmanc4f20322015-05-10 09:47:50 -07002525 enum switchdev_trans trans,
Scott Feldman4a6bb6d2015-02-25 20:15:37 -08002526 u32 in_pport, u32 in_pport_mask,
Scott Feldman9f6bbf72014-11-28 14:34:27 +01002527 __be16 eth_type, const u8 *eth_dst,
2528 const u8 *eth_dst_mask, __be16 vlan_id,
2529 __be16 vlan_id_mask, bool copy_to_cpu,
2530 int flags)
2531{
2532 struct rocker_flow_tbl_entry *entry;
2533
Scott Feldman179f9a22015-06-12 21:35:46 -07002534 entry = rocker_port_kzalloc(rocker_port, trans, flags, sizeof(*entry));
Scott Feldman9f6bbf72014-11-28 14:34:27 +01002535 if (!entry)
2536 return -ENOMEM;
2537
2538 if (is_multicast_ether_addr(eth_dst)) {
2539 entry->key.priority = ROCKER_PRIORITY_TERM_MAC_MCAST;
2540 entry->key.term_mac.goto_tbl =
2541 ROCKER_OF_DPA_TABLE_ID_MULTICAST_ROUTING;
2542 } else {
2543 entry->key.priority = ROCKER_PRIORITY_TERM_MAC_UCAST;
2544 entry->key.term_mac.goto_tbl =
2545 ROCKER_OF_DPA_TABLE_ID_UNICAST_ROUTING;
2546 }
2547
2548 entry->key.tbl_id = ROCKER_OF_DPA_TABLE_ID_TERMINATION_MAC;
Scott Feldman4a6bb6d2015-02-25 20:15:37 -08002549 entry->key.term_mac.in_pport = in_pport;
2550 entry->key.term_mac.in_pport_mask = in_pport_mask;
Scott Feldman9f6bbf72014-11-28 14:34:27 +01002551 entry->key.term_mac.eth_type = eth_type;
2552 ether_addr_copy(entry->key.term_mac.eth_dst, eth_dst);
2553 ether_addr_copy(entry->key.term_mac.eth_dst_mask, eth_dst_mask);
2554 entry->key.term_mac.vlan_id = vlan_id;
2555 entry->key.term_mac.vlan_id_mask = vlan_id_mask;
2556 entry->key.term_mac.copy_to_cpu = copy_to_cpu;
2557
Scott Feldmanc4f20322015-05-10 09:47:50 -07002558 return rocker_flow_tbl_do(rocker_port, trans, flags, entry);
Scott Feldman9f6bbf72014-11-28 14:34:27 +01002559}
2560
2561static int rocker_flow_tbl_bridge(struct rocker_port *rocker_port,
Scott Feldmanc4f20322015-05-10 09:47:50 -07002562 enum switchdev_trans trans, int flags,
Scott Feldman9f6bbf72014-11-28 14:34:27 +01002563 const u8 *eth_dst, const u8 *eth_dst_mask,
2564 __be16 vlan_id, u32 tunnel_id,
2565 enum rocker_of_dpa_table_id goto_tbl,
2566 u32 group_id, bool copy_to_cpu)
2567{
2568 struct rocker_flow_tbl_entry *entry;
2569 u32 priority;
2570 bool vlan_bridging = !!vlan_id;
2571 bool dflt = !eth_dst || (eth_dst && eth_dst_mask);
2572 bool wild = false;
2573
Scott Feldman179f9a22015-06-12 21:35:46 -07002574 entry = rocker_port_kzalloc(rocker_port, trans, flags, sizeof(*entry));
Scott Feldman9f6bbf72014-11-28 14:34:27 +01002575 if (!entry)
2576 return -ENOMEM;
2577
2578 entry->key.tbl_id = ROCKER_OF_DPA_TABLE_ID_BRIDGING;
2579
2580 if (eth_dst) {
2581 entry->key.bridge.has_eth_dst = 1;
2582 ether_addr_copy(entry->key.bridge.eth_dst, eth_dst);
2583 }
2584 if (eth_dst_mask) {
2585 entry->key.bridge.has_eth_dst_mask = 1;
2586 ether_addr_copy(entry->key.bridge.eth_dst_mask, eth_dst_mask);
Simon Horman629161f2015-04-30 15:21:29 +09002587 if (!ether_addr_equal(eth_dst_mask, ff_mac))
Scott Feldman9f6bbf72014-11-28 14:34:27 +01002588 wild = true;
2589 }
2590
2591 priority = ROCKER_PRIORITY_UNKNOWN;
Thomas Graf51ace882014-11-28 14:34:32 +01002592 if (vlan_bridging && dflt && wild)
Scott Feldman9f6bbf72014-11-28 14:34:27 +01002593 priority = ROCKER_PRIORITY_BRIDGING_VLAN_DFLT_WILD;
Thomas Graf51ace882014-11-28 14:34:32 +01002594 else if (vlan_bridging && dflt && !wild)
Scott Feldman9f6bbf72014-11-28 14:34:27 +01002595 priority = ROCKER_PRIORITY_BRIDGING_VLAN_DFLT_EXACT;
Thomas Graf51ace882014-11-28 14:34:32 +01002596 else if (vlan_bridging && !dflt)
Scott Feldman9f6bbf72014-11-28 14:34:27 +01002597 priority = ROCKER_PRIORITY_BRIDGING_VLAN;
Thomas Graf51ace882014-11-28 14:34:32 +01002598 else if (!vlan_bridging && dflt && wild)
Scott Feldman9f6bbf72014-11-28 14:34:27 +01002599 priority = ROCKER_PRIORITY_BRIDGING_TENANT_DFLT_WILD;
Thomas Graf51ace882014-11-28 14:34:32 +01002600 else if (!vlan_bridging && dflt && !wild)
Scott Feldman9f6bbf72014-11-28 14:34:27 +01002601 priority = ROCKER_PRIORITY_BRIDGING_TENANT_DFLT_EXACT;
Thomas Graf51ace882014-11-28 14:34:32 +01002602 else if (!vlan_bridging && !dflt)
Scott Feldman9f6bbf72014-11-28 14:34:27 +01002603 priority = ROCKER_PRIORITY_BRIDGING_TENANT;
2604
2605 entry->key.priority = priority;
2606 entry->key.bridge.vlan_id = vlan_id;
2607 entry->key.bridge.tunnel_id = tunnel_id;
2608 entry->key.bridge.goto_tbl = goto_tbl;
2609 entry->key.bridge.group_id = group_id;
2610 entry->key.bridge.copy_to_cpu = copy_to_cpu;
2611
Scott Feldmanc4f20322015-05-10 09:47:50 -07002612 return rocker_flow_tbl_do(rocker_port, trans, flags, entry);
Scott Feldman9f6bbf72014-11-28 14:34:27 +01002613}
2614
Scott Feldmanc1beeef2015-03-05 21:21:20 -08002615static int rocker_flow_tbl_ucast4_routing(struct rocker_port *rocker_port,
Scott Feldmanc4f20322015-05-10 09:47:50 -07002616 enum switchdev_trans trans,
Scott Feldmanc1beeef2015-03-05 21:21:20 -08002617 __be16 eth_type, __be32 dst,
2618 __be32 dst_mask, u32 priority,
2619 enum rocker_of_dpa_table_id goto_tbl,
2620 u32 group_id, int flags)
2621{
2622 struct rocker_flow_tbl_entry *entry;
2623
Scott Feldman179f9a22015-06-12 21:35:46 -07002624 entry = rocker_port_kzalloc(rocker_port, trans, flags, sizeof(*entry));
Scott Feldmanc1beeef2015-03-05 21:21:20 -08002625 if (!entry)
2626 return -ENOMEM;
2627
2628 entry->key.tbl_id = ROCKER_OF_DPA_TABLE_ID_UNICAST_ROUTING;
2629 entry->key.priority = priority;
2630 entry->key.ucast_routing.eth_type = eth_type;
2631 entry->key.ucast_routing.dst4 = dst;
2632 entry->key.ucast_routing.dst4_mask = dst_mask;
2633 entry->key.ucast_routing.goto_tbl = goto_tbl;
2634 entry->key.ucast_routing.group_id = group_id;
2635 entry->key_len = offsetof(struct rocker_flow_tbl_key,
2636 ucast_routing.group_id);
2637
Scott Feldmanc4f20322015-05-10 09:47:50 -07002638 return rocker_flow_tbl_do(rocker_port, trans, flags, entry);
Scott Feldmanc1beeef2015-03-05 21:21:20 -08002639}
2640
Scott Feldman9f6bbf72014-11-28 14:34:27 +01002641static int rocker_flow_tbl_acl(struct rocker_port *rocker_port,
Scott Feldmanc4f20322015-05-10 09:47:50 -07002642 enum switchdev_trans trans, int flags,
2643 u32 in_pport, u32 in_pport_mask,
Scott Feldman9f6bbf72014-11-28 14:34:27 +01002644 const u8 *eth_src, const u8 *eth_src_mask,
2645 const u8 *eth_dst, const u8 *eth_dst_mask,
Scott Feldmanc4f20322015-05-10 09:47:50 -07002646 __be16 eth_type, __be16 vlan_id,
2647 __be16 vlan_id_mask, u8 ip_proto,
2648 u8 ip_proto_mask, u8 ip_tos, u8 ip_tos_mask,
Scott Feldman9f6bbf72014-11-28 14:34:27 +01002649 u32 group_id)
2650{
2651 u32 priority;
2652 struct rocker_flow_tbl_entry *entry;
2653
Scott Feldman179f9a22015-06-12 21:35:46 -07002654 entry = rocker_port_kzalloc(rocker_port, trans, flags, sizeof(*entry));
Scott Feldman9f6bbf72014-11-28 14:34:27 +01002655 if (!entry)
2656 return -ENOMEM;
2657
2658 priority = ROCKER_PRIORITY_ACL_NORMAL;
2659 if (eth_dst && eth_dst_mask) {
Simon Horman629161f2015-04-30 15:21:29 +09002660 if (ether_addr_equal(eth_dst_mask, mcast_mac))
Scott Feldman9f6bbf72014-11-28 14:34:27 +01002661 priority = ROCKER_PRIORITY_ACL_DFLT;
2662 else if (is_link_local_ether_addr(eth_dst))
2663 priority = ROCKER_PRIORITY_ACL_CTRL;
2664 }
2665
2666 entry->key.priority = priority;
2667 entry->key.tbl_id = ROCKER_OF_DPA_TABLE_ID_ACL_POLICY;
Scott Feldman4a6bb6d2015-02-25 20:15:37 -08002668 entry->key.acl.in_pport = in_pport;
2669 entry->key.acl.in_pport_mask = in_pport_mask;
Scott Feldman9f6bbf72014-11-28 14:34:27 +01002670
2671 if (eth_src)
2672 ether_addr_copy(entry->key.acl.eth_src, eth_src);
2673 if (eth_src_mask)
2674 ether_addr_copy(entry->key.acl.eth_src_mask, eth_src_mask);
2675 if (eth_dst)
2676 ether_addr_copy(entry->key.acl.eth_dst, eth_dst);
2677 if (eth_dst_mask)
2678 ether_addr_copy(entry->key.acl.eth_dst_mask, eth_dst_mask);
2679
2680 entry->key.acl.eth_type = eth_type;
2681 entry->key.acl.vlan_id = vlan_id;
2682 entry->key.acl.vlan_id_mask = vlan_id_mask;
2683 entry->key.acl.ip_proto = ip_proto;
2684 entry->key.acl.ip_proto_mask = ip_proto_mask;
2685 entry->key.acl.ip_tos = ip_tos;
2686 entry->key.acl.ip_tos_mask = ip_tos_mask;
2687 entry->key.acl.group_id = group_id;
2688
Scott Feldmanc4f20322015-05-10 09:47:50 -07002689 return rocker_flow_tbl_do(rocker_port, trans, flags, entry);
Scott Feldman9f6bbf72014-11-28 14:34:27 +01002690}
2691
2692static struct rocker_group_tbl_entry *
Simon Hormane5054642015-05-25 14:28:36 +09002693rocker_group_tbl_find(const struct rocker *rocker,
2694 const struct rocker_group_tbl_entry *match)
Scott Feldman9f6bbf72014-11-28 14:34:27 +01002695{
2696 struct rocker_group_tbl_entry *found;
2697
2698 hash_for_each_possible(rocker->group_tbl, found,
2699 entry, match->group_id) {
2700 if (found->group_id == match->group_id)
2701 return found;
2702 }
2703
2704 return NULL;
2705}
2706
Simon Horman0985df72015-05-25 14:28:35 +09002707static void rocker_group_tbl_entry_free(enum switchdev_trans trans,
Scott Feldmanc4f20322015-05-10 09:47:50 -07002708 struct rocker_group_tbl_entry *entry)
Scott Feldman9f6bbf72014-11-28 14:34:27 +01002709{
2710 switch (ROCKER_GROUP_TYPE_GET(entry->group_id)) {
2711 case ROCKER_OF_DPA_GROUP_TYPE_L2_FLOOD:
2712 case ROCKER_OF_DPA_GROUP_TYPE_L2_MCAST:
Simon Horman0985df72015-05-25 14:28:35 +09002713 rocker_port_kfree(trans, entry->group_ids);
Scott Feldman9f6bbf72014-11-28 14:34:27 +01002714 break;
2715 default:
2716 break;
2717 }
Simon Horman0985df72015-05-25 14:28:35 +09002718 rocker_port_kfree(trans, entry);
Scott Feldman9f6bbf72014-11-28 14:34:27 +01002719}
2720
2721static int rocker_group_tbl_add(struct rocker_port *rocker_port,
Scott Feldman179f9a22015-06-12 21:35:46 -07002722 enum switchdev_trans trans, int flags,
Scott Feldmanc4f20322015-05-10 09:47:50 -07002723 struct rocker_group_tbl_entry *match)
Scott Feldman9f6bbf72014-11-28 14:34:27 +01002724{
2725 struct rocker *rocker = rocker_port->rocker;
2726 struct rocker_group_tbl_entry *found;
Scott Feldman179f9a22015-06-12 21:35:46 -07002727 unsigned long lock_flags;
Scott Feldman9f6bbf72014-11-28 14:34:27 +01002728
Scott Feldman179f9a22015-06-12 21:35:46 -07002729 spin_lock_irqsave(&rocker->group_tbl_lock, lock_flags);
Scott Feldman9f6bbf72014-11-28 14:34:27 +01002730
2731 found = rocker_group_tbl_find(rocker, match);
2732
2733 if (found) {
Scott Feldmanc4f20322015-05-10 09:47:50 -07002734 if (trans != SWITCHDEV_TRANS_PREPARE)
2735 hash_del(&found->entry);
Simon Horman0985df72015-05-25 14:28:35 +09002736 rocker_group_tbl_entry_free(trans, found);
Scott Feldman9f6bbf72014-11-28 14:34:27 +01002737 found = match;
2738 found->cmd = ROCKER_TLV_CMD_TYPE_OF_DPA_GROUP_MOD;
2739 } else {
2740 found = match;
2741 found->cmd = ROCKER_TLV_CMD_TYPE_OF_DPA_GROUP_ADD;
2742 }
2743
Scott Feldmanc4f20322015-05-10 09:47:50 -07002744 if (trans != SWITCHDEV_TRANS_PREPARE)
2745 hash_add(rocker->group_tbl, &found->entry, found->group_id);
Scott Feldman9f6bbf72014-11-28 14:34:27 +01002746
Scott Feldman179f9a22015-06-12 21:35:46 -07002747 spin_unlock_irqrestore(&rocker->group_tbl_lock, lock_flags);
Scott Feldman9f6bbf72014-11-28 14:34:27 +01002748
Scott Feldman179f9a22015-06-12 21:35:46 -07002749 return rocker_cmd_exec(rocker_port, trans, flags,
2750 rocker_cmd_group_tbl_add, found, NULL, NULL);
Scott Feldman9f6bbf72014-11-28 14:34:27 +01002751}
2752
2753static int rocker_group_tbl_del(struct rocker_port *rocker_port,
Scott Feldman179f9a22015-06-12 21:35:46 -07002754 enum switchdev_trans trans, int flags,
Scott Feldmanc4f20322015-05-10 09:47:50 -07002755 struct rocker_group_tbl_entry *match)
Scott Feldman9f6bbf72014-11-28 14:34:27 +01002756{
2757 struct rocker *rocker = rocker_port->rocker;
2758 struct rocker_group_tbl_entry *found;
Scott Feldman179f9a22015-06-12 21:35:46 -07002759 unsigned long lock_flags;
Scott Feldman9f6bbf72014-11-28 14:34:27 +01002760 int err = 0;
2761
Scott Feldman179f9a22015-06-12 21:35:46 -07002762 spin_lock_irqsave(&rocker->group_tbl_lock, lock_flags);
Scott Feldman9f6bbf72014-11-28 14:34:27 +01002763
2764 found = rocker_group_tbl_find(rocker, match);
2765
2766 if (found) {
Scott Feldmanc4f20322015-05-10 09:47:50 -07002767 if (trans != SWITCHDEV_TRANS_PREPARE)
2768 hash_del(&found->entry);
Scott Feldman9f6bbf72014-11-28 14:34:27 +01002769 found->cmd = ROCKER_TLV_CMD_TYPE_OF_DPA_GROUP_DEL;
2770 }
2771
Scott Feldman179f9a22015-06-12 21:35:46 -07002772 spin_unlock_irqrestore(&rocker->group_tbl_lock, lock_flags);
Scott Feldman9f6bbf72014-11-28 14:34:27 +01002773
Simon Horman0985df72015-05-25 14:28:35 +09002774 rocker_group_tbl_entry_free(trans, match);
Scott Feldman9f6bbf72014-11-28 14:34:27 +01002775
2776 if (found) {
Scott Feldman179f9a22015-06-12 21:35:46 -07002777 err = rocker_cmd_exec(rocker_port, trans, flags,
Scott Feldman9f6bbf72014-11-28 14:34:27 +01002778 rocker_cmd_group_tbl_del,
Scott Feldmanc4f20322015-05-10 09:47:50 -07002779 found, NULL, NULL);
Simon Horman0985df72015-05-25 14:28:35 +09002780 rocker_group_tbl_entry_free(trans, found);
Scott Feldman9f6bbf72014-11-28 14:34:27 +01002781 }
2782
2783 return err;
2784}
2785
2786static int rocker_group_tbl_do(struct rocker_port *rocker_port,
Scott Feldmanc4f20322015-05-10 09:47:50 -07002787 enum switchdev_trans trans, int flags,
2788 struct rocker_group_tbl_entry *entry)
Scott Feldman9f6bbf72014-11-28 14:34:27 +01002789{
Scott Feldman9f6bbf72014-11-28 14:34:27 +01002790 if (flags & ROCKER_OP_FLAG_REMOVE)
Scott Feldman179f9a22015-06-12 21:35:46 -07002791 return rocker_group_tbl_del(rocker_port, trans, flags, entry);
Scott Feldman9f6bbf72014-11-28 14:34:27 +01002792 else
Scott Feldman179f9a22015-06-12 21:35:46 -07002793 return rocker_group_tbl_add(rocker_port, trans, flags, entry);
Scott Feldman9f6bbf72014-11-28 14:34:27 +01002794}
2795
2796static int rocker_group_l2_interface(struct rocker_port *rocker_port,
Scott Feldmanc4f20322015-05-10 09:47:50 -07002797 enum switchdev_trans trans, int flags,
2798 __be16 vlan_id, u32 out_pport,
2799 int pop_vlan)
Scott Feldman9f6bbf72014-11-28 14:34:27 +01002800{
2801 struct rocker_group_tbl_entry *entry;
2802
Scott Feldman179f9a22015-06-12 21:35:46 -07002803 entry = rocker_port_kzalloc(rocker_port, trans, flags, sizeof(*entry));
Scott Feldman9f6bbf72014-11-28 14:34:27 +01002804 if (!entry)
2805 return -ENOMEM;
2806
Scott Feldman4a6bb6d2015-02-25 20:15:37 -08002807 entry->group_id = ROCKER_GROUP_L2_INTERFACE(vlan_id, out_pport);
Scott Feldman9f6bbf72014-11-28 14:34:27 +01002808 entry->l2_interface.pop_vlan = pop_vlan;
2809
Scott Feldmanc4f20322015-05-10 09:47:50 -07002810 return rocker_group_tbl_do(rocker_port, trans, flags, entry);
Scott Feldman9f6bbf72014-11-28 14:34:27 +01002811}
2812
2813static int rocker_group_l2_fan_out(struct rocker_port *rocker_port,
Scott Feldmanc4f20322015-05-10 09:47:50 -07002814 enum switchdev_trans trans,
Scott Feldman9f6bbf72014-11-28 14:34:27 +01002815 int flags, u8 group_count,
Simon Hormane5054642015-05-25 14:28:36 +09002816 const u32 *group_ids, u32 group_id)
Scott Feldman9f6bbf72014-11-28 14:34:27 +01002817{
2818 struct rocker_group_tbl_entry *entry;
2819
Scott Feldman179f9a22015-06-12 21:35:46 -07002820 entry = rocker_port_kzalloc(rocker_port, trans, flags, sizeof(*entry));
Scott Feldman9f6bbf72014-11-28 14:34:27 +01002821 if (!entry)
2822 return -ENOMEM;
2823
2824 entry->group_id = group_id;
2825 entry->group_count = group_count;
2826
Scott Feldman179f9a22015-06-12 21:35:46 -07002827 entry->group_ids = rocker_port_kcalloc(rocker_port, trans, flags,
2828 group_count, sizeof(u32));
Scott Feldman9f6bbf72014-11-28 14:34:27 +01002829 if (!entry->group_ids) {
Simon Horman0985df72015-05-25 14:28:35 +09002830 rocker_port_kfree(trans, entry);
Scott Feldman9f6bbf72014-11-28 14:34:27 +01002831 return -ENOMEM;
2832 }
2833 memcpy(entry->group_ids, group_ids, group_count * sizeof(u32));
2834
Scott Feldmanc4f20322015-05-10 09:47:50 -07002835 return rocker_group_tbl_do(rocker_port, trans, flags, entry);
Scott Feldman9f6bbf72014-11-28 14:34:27 +01002836}
2837
2838static int rocker_group_l2_flood(struct rocker_port *rocker_port,
Scott Feldmanc4f20322015-05-10 09:47:50 -07002839 enum switchdev_trans trans, int flags,
2840 __be16 vlan_id, u8 group_count,
Simon Hormane5054642015-05-25 14:28:36 +09002841 const u32 *group_ids, u32 group_id)
Scott Feldman9f6bbf72014-11-28 14:34:27 +01002842{
Scott Feldmanc4f20322015-05-10 09:47:50 -07002843 return rocker_group_l2_fan_out(rocker_port, trans, flags,
Scott Feldman9f6bbf72014-11-28 14:34:27 +01002844 group_count, group_ids,
2845 group_id);
2846}
2847
Scott Feldmanc1beeef2015-03-05 21:21:20 -08002848static int rocker_group_l3_unicast(struct rocker_port *rocker_port,
Scott Feldmanc4f20322015-05-10 09:47:50 -07002849 enum switchdev_trans trans, int flags,
Simon Hormane5054642015-05-25 14:28:36 +09002850 u32 index, const u8 *src_mac, const u8 *dst_mac,
Scott Feldmanc4f20322015-05-10 09:47:50 -07002851 __be16 vlan_id, bool ttl_check, u32 pport)
Scott Feldmanc1beeef2015-03-05 21:21:20 -08002852{
2853 struct rocker_group_tbl_entry *entry;
2854
Scott Feldman179f9a22015-06-12 21:35:46 -07002855 entry = rocker_port_kzalloc(rocker_port, trans, flags, sizeof(*entry));
Scott Feldmanc1beeef2015-03-05 21:21:20 -08002856 if (!entry)
2857 return -ENOMEM;
2858
2859 entry->group_id = ROCKER_GROUP_L3_UNICAST(index);
2860 if (src_mac)
2861 ether_addr_copy(entry->l3_unicast.eth_src, src_mac);
2862 if (dst_mac)
2863 ether_addr_copy(entry->l3_unicast.eth_dst, dst_mac);
2864 entry->l3_unicast.vlan_id = vlan_id;
2865 entry->l3_unicast.ttl_check = ttl_check;
2866 entry->l3_unicast.group_id = ROCKER_GROUP_L2_INTERFACE(vlan_id, pport);
2867
Scott Feldmanc4f20322015-05-10 09:47:50 -07002868 return rocker_group_tbl_do(rocker_port, trans, flags, entry);
Scott Feldmanc1beeef2015-03-05 21:21:20 -08002869}
2870
2871static struct rocker_neigh_tbl_entry *
Simon Hormane5054642015-05-25 14:28:36 +09002872rocker_neigh_tbl_find(const struct rocker *rocker, __be32 ip_addr)
Scott Feldmanc1beeef2015-03-05 21:21:20 -08002873{
2874 struct rocker_neigh_tbl_entry *found;
2875
Scott Feldman0f43deb2015-03-06 15:54:51 -08002876 hash_for_each_possible(rocker->neigh_tbl, found,
2877 entry, be32_to_cpu(ip_addr))
Scott Feldmanc1beeef2015-03-05 21:21:20 -08002878 if (found->ip_addr == ip_addr)
2879 return found;
2880
2881 return NULL;
2882}
2883
2884static void _rocker_neigh_add(struct rocker *rocker,
Simon Horman550ecc92015-05-21 12:40:16 +09002885 enum switchdev_trans trans,
Scott Feldmanc1beeef2015-03-05 21:21:20 -08002886 struct rocker_neigh_tbl_entry *entry)
2887{
Scott Feldman4d81db42015-06-12 21:24:40 -07002888 if (trans != SWITCHDEV_TRANS_COMMIT)
2889 entry->index = rocker->neigh_tbl_next_index++;
Simon Horman550ecc92015-05-21 12:40:16 +09002890 if (trans == SWITCHDEV_TRANS_PREPARE)
2891 return;
Scott Feldmanc1beeef2015-03-05 21:21:20 -08002892 entry->ref_count++;
Scott Feldman0f43deb2015-03-06 15:54:51 -08002893 hash_add(rocker->neigh_tbl, &entry->entry,
2894 be32_to_cpu(entry->ip_addr));
Scott Feldmanc1beeef2015-03-05 21:21:20 -08002895}
2896
Simon Horman0985df72015-05-25 14:28:35 +09002897static void _rocker_neigh_del(enum switchdev_trans trans,
Scott Feldmanc1beeef2015-03-05 21:21:20 -08002898 struct rocker_neigh_tbl_entry *entry)
2899{
Simon Horman550ecc92015-05-21 12:40:16 +09002900 if (trans == SWITCHDEV_TRANS_PREPARE)
2901 return;
Scott Feldmanc1beeef2015-03-05 21:21:20 -08002902 if (--entry->ref_count == 0) {
2903 hash_del(&entry->entry);
Simon Horman0985df72015-05-25 14:28:35 +09002904 rocker_port_kfree(trans, entry);
Scott Feldmanc1beeef2015-03-05 21:21:20 -08002905 }
2906}
2907
Scott Feldmanc4f20322015-05-10 09:47:50 -07002908static void _rocker_neigh_update(struct rocker_neigh_tbl_entry *entry,
Simon Horman550ecc92015-05-21 12:40:16 +09002909 enum switchdev_trans trans,
Simon Hormane5054642015-05-25 14:28:36 +09002910 const u8 *eth_dst, bool ttl_check)
Scott Feldmanc1beeef2015-03-05 21:21:20 -08002911{
2912 if (eth_dst) {
2913 ether_addr_copy(entry->eth_dst, eth_dst);
2914 entry->ttl_check = ttl_check;
Simon Horman550ecc92015-05-21 12:40:16 +09002915 } else if (trans != SWITCHDEV_TRANS_PREPARE) {
Scott Feldmanc1beeef2015-03-05 21:21:20 -08002916 entry->ref_count++;
2917 }
2918}
2919
2920static int rocker_port_ipv4_neigh(struct rocker_port *rocker_port,
Scott Feldmanc4f20322015-05-10 09:47:50 -07002921 enum switchdev_trans trans,
Simon Hormane5054642015-05-25 14:28:36 +09002922 int flags, __be32 ip_addr, const u8 *eth_dst)
Scott Feldmanc1beeef2015-03-05 21:21:20 -08002923{
2924 struct rocker *rocker = rocker_port->rocker;
2925 struct rocker_neigh_tbl_entry *entry;
2926 struct rocker_neigh_tbl_entry *found;
2927 unsigned long lock_flags;
2928 __be16 eth_type = htons(ETH_P_IP);
2929 enum rocker_of_dpa_table_id goto_tbl =
2930 ROCKER_OF_DPA_TABLE_ID_ACL_POLICY;
2931 u32 group_id;
2932 u32 priority = 0;
2933 bool adding = !(flags & ROCKER_OP_FLAG_REMOVE);
2934 bool updating;
2935 bool removing;
2936 int err = 0;
2937
Scott Feldman179f9a22015-06-12 21:35:46 -07002938 entry = rocker_port_kzalloc(rocker_port, trans, flags, sizeof(*entry));
Scott Feldmanc1beeef2015-03-05 21:21:20 -08002939 if (!entry)
2940 return -ENOMEM;
2941
2942 spin_lock_irqsave(&rocker->neigh_tbl_lock, lock_flags);
2943
2944 found = rocker_neigh_tbl_find(rocker, ip_addr);
2945
2946 updating = found && adding;
2947 removing = found && !adding;
2948 adding = !found && adding;
2949
2950 if (adding) {
2951 entry->ip_addr = ip_addr;
2952 entry->dev = rocker_port->dev;
2953 ether_addr_copy(entry->eth_dst, eth_dst);
2954 entry->ttl_check = true;
Simon Horman550ecc92015-05-21 12:40:16 +09002955 _rocker_neigh_add(rocker, trans, entry);
Scott Feldmanc1beeef2015-03-05 21:21:20 -08002956 } else if (removing) {
2957 memcpy(entry, found, sizeof(*entry));
Simon Horman0985df72015-05-25 14:28:35 +09002958 _rocker_neigh_del(trans, found);
Scott Feldmanc1beeef2015-03-05 21:21:20 -08002959 } else if (updating) {
Simon Horman550ecc92015-05-21 12:40:16 +09002960 _rocker_neigh_update(found, trans, eth_dst, true);
Scott Feldmanc1beeef2015-03-05 21:21:20 -08002961 memcpy(entry, found, sizeof(*entry));
2962 } else {
2963 err = -ENOENT;
2964 }
2965
2966 spin_unlock_irqrestore(&rocker->neigh_tbl_lock, lock_flags);
2967
2968 if (err)
2969 goto err_out;
2970
2971 /* For each active neighbor, we have an L3 unicast group and
2972 * a /32 route to the neighbor, which uses the L3 unicast
2973 * group. The L3 unicast group can also be referred to by
2974 * other routes' nexthops.
2975 */
2976
Scott Feldmanc4f20322015-05-10 09:47:50 -07002977 err = rocker_group_l3_unicast(rocker_port, trans, flags,
Scott Feldmanc1beeef2015-03-05 21:21:20 -08002978 entry->index,
2979 rocker_port->dev->dev_addr,
2980 entry->eth_dst,
2981 rocker_port->internal_vlan_id,
2982 entry->ttl_check,
2983 rocker_port->pport);
2984 if (err) {
2985 netdev_err(rocker_port->dev,
2986 "Error (%d) L3 unicast group index %d\n",
2987 err, entry->index);
2988 goto err_out;
2989 }
2990
2991 if (adding || removing) {
2992 group_id = ROCKER_GROUP_L3_UNICAST(entry->index);
Scott Feldmanc4f20322015-05-10 09:47:50 -07002993 err = rocker_flow_tbl_ucast4_routing(rocker_port, trans,
Scott Feldmanc1beeef2015-03-05 21:21:20 -08002994 eth_type, ip_addr,
2995 inet_make_mask(32),
2996 priority, goto_tbl,
2997 group_id, flags);
2998
2999 if (err)
3000 netdev_err(rocker_port->dev,
3001 "Error (%d) /32 unicast route %pI4 group 0x%08x\n",
3002 err, &entry->ip_addr, group_id);
3003 }
3004
3005err_out:
3006 if (!adding)
Simon Horman0985df72015-05-25 14:28:35 +09003007 rocker_port_kfree(trans, entry);
Scott Feldmanc1beeef2015-03-05 21:21:20 -08003008
3009 return err;
3010}
3011
3012static int rocker_port_ipv4_resolve(struct rocker_port *rocker_port,
Scott Feldmanc4f20322015-05-10 09:47:50 -07003013 enum switchdev_trans trans, __be32 ip_addr)
Scott Feldmanc1beeef2015-03-05 21:21:20 -08003014{
3015 struct net_device *dev = rocker_port->dev;
Scott Feldman0f43deb2015-03-06 15:54:51 -08003016 struct neighbour *n = __ipv4_neigh_lookup(dev, (__force u32)ip_addr);
Scott Feldmanc1beeef2015-03-05 21:21:20 -08003017 int err = 0;
3018
Ying Xue4133fc02015-05-15 12:53:21 +08003019 if (!n) {
Scott Feldmanc1beeef2015-03-05 21:21:20 -08003020 n = neigh_create(&arp_tbl, &ip_addr, dev);
Ying Xue4133fc02015-05-15 12:53:21 +08003021 if (IS_ERR(n))
3022 return IS_ERR(n);
3023 }
Scott Feldmanc1beeef2015-03-05 21:21:20 -08003024
3025 /* If the neigh is already resolved, then go ahead and
3026 * install the entry, otherwise start the ARP process to
3027 * resolve the neigh.
3028 */
3029
3030 if (n->nud_state & NUD_VALID)
Scott Feldmanc4f20322015-05-10 09:47:50 -07003031 err = rocker_port_ipv4_neigh(rocker_port, trans, 0,
3032 ip_addr, n->ha);
Scott Feldmanc1beeef2015-03-05 21:21:20 -08003033 else
3034 neigh_event_send(n, NULL);
3035
Ying Xue4133fc02015-05-15 12:53:21 +08003036 neigh_release(n);
Scott Feldmanc1beeef2015-03-05 21:21:20 -08003037 return err;
3038}
3039
Scott Feldmanc4f20322015-05-10 09:47:50 -07003040static int rocker_port_ipv4_nh(struct rocker_port *rocker_port,
3041 enum switchdev_trans trans, int flags,
Scott Feldmanc1beeef2015-03-05 21:21:20 -08003042 __be32 ip_addr, u32 *index)
3043{
3044 struct rocker *rocker = rocker_port->rocker;
3045 struct rocker_neigh_tbl_entry *entry;
3046 struct rocker_neigh_tbl_entry *found;
3047 unsigned long lock_flags;
3048 bool adding = !(flags & ROCKER_OP_FLAG_REMOVE);
3049 bool updating;
3050 bool removing;
3051 bool resolved = true;
3052 int err = 0;
3053
Scott Feldman179f9a22015-06-12 21:35:46 -07003054 entry = rocker_port_kzalloc(rocker_port, trans, flags, sizeof(*entry));
Scott Feldmanc1beeef2015-03-05 21:21:20 -08003055 if (!entry)
3056 return -ENOMEM;
3057
3058 spin_lock_irqsave(&rocker->neigh_tbl_lock, lock_flags);
3059
3060 found = rocker_neigh_tbl_find(rocker, ip_addr);
3061 if (found)
3062 *index = found->index;
3063
3064 updating = found && adding;
3065 removing = found && !adding;
3066 adding = !found && adding;
3067
3068 if (adding) {
3069 entry->ip_addr = ip_addr;
3070 entry->dev = rocker_port->dev;
Simon Horman550ecc92015-05-21 12:40:16 +09003071 _rocker_neigh_add(rocker, trans, entry);
Scott Feldmanc1beeef2015-03-05 21:21:20 -08003072 *index = entry->index;
3073 resolved = false;
3074 } else if (removing) {
Simon Horman0985df72015-05-25 14:28:35 +09003075 _rocker_neigh_del(trans, found);
Scott Feldmanc1beeef2015-03-05 21:21:20 -08003076 } else if (updating) {
Simon Horman550ecc92015-05-21 12:40:16 +09003077 _rocker_neigh_update(found, trans, NULL, false);
Scott Feldmanc1beeef2015-03-05 21:21:20 -08003078 resolved = !is_zero_ether_addr(found->eth_dst);
3079 } else {
3080 err = -ENOENT;
3081 }
3082
3083 spin_unlock_irqrestore(&rocker->neigh_tbl_lock, lock_flags);
3084
3085 if (!adding)
Simon Horman0985df72015-05-25 14:28:35 +09003086 rocker_port_kfree(trans, entry);
Scott Feldmanc1beeef2015-03-05 21:21:20 -08003087
3088 if (err)
3089 return err;
3090
3091 /* Resolved means neigh ip_addr is resolved to neigh mac. */
3092
3093 if (!resolved)
Scott Feldmanc4f20322015-05-10 09:47:50 -07003094 err = rocker_port_ipv4_resolve(rocker_port, trans, ip_addr);
Scott Feldmanc1beeef2015-03-05 21:21:20 -08003095
3096 return err;
3097}
3098
Scott Feldman6c707942014-11-28 14:34:28 +01003099static int rocker_port_vlan_flood_group(struct rocker_port *rocker_port,
Scott Feldmanc4f20322015-05-10 09:47:50 -07003100 enum switchdev_trans trans,
Scott Feldman6c707942014-11-28 14:34:28 +01003101 int flags, __be16 vlan_id)
3102{
3103 struct rocker_port *p;
Simon Hormane5054642015-05-25 14:28:36 +09003104 const struct rocker *rocker = rocker_port->rocker;
Scott Feldman6c707942014-11-28 14:34:28 +01003105 u32 group_id = ROCKER_GROUP_L2_FLOOD(vlan_id, 0);
Scott Feldman04f49fa2015-03-15 23:04:46 -07003106 u32 *group_ids;
Scott Feldman6c707942014-11-28 14:34:28 +01003107 u8 group_count = 0;
Scott Feldman04f49fa2015-03-15 23:04:46 -07003108 int err = 0;
Scott Feldman6c707942014-11-28 14:34:28 +01003109 int i;
3110
Scott Feldman179f9a22015-06-12 21:35:46 -07003111 group_ids = rocker_port_kcalloc(rocker_port, trans, flags,
3112 rocker->port_count, sizeof(u32));
Scott Feldman04f49fa2015-03-15 23:04:46 -07003113 if (!group_ids)
3114 return -ENOMEM;
3115
Scott Feldman6c707942014-11-28 14:34:28 +01003116 /* Adjust the flood group for this VLAN. The flood group
3117 * references an L2 interface group for each port in this
3118 * VLAN.
3119 */
3120
3121 for (i = 0; i < rocker->port_count; i++) {
3122 p = rocker->ports[i];
Scott Feldmanbcfd7802015-06-01 11:39:04 -07003123 if (!p)
3124 continue;
Scott Feldman6c707942014-11-28 14:34:28 +01003125 if (!rocker_port_is_bridged(p))
3126 continue;
3127 if (test_bit(ntohs(vlan_id), p->vlan_bitmap)) {
3128 group_ids[group_count++] =
Scott Feldman4a6bb6d2015-02-25 20:15:37 -08003129 ROCKER_GROUP_L2_INTERFACE(vlan_id, p->pport);
Scott Feldman6c707942014-11-28 14:34:28 +01003130 }
3131 }
3132
3133 /* If there are no bridged ports in this VLAN, we're done */
3134 if (group_count == 0)
Scott Feldman04f49fa2015-03-15 23:04:46 -07003135 goto no_ports_in_vlan;
Scott Feldman6c707942014-11-28 14:34:28 +01003136
Scott Feldmanc4f20322015-05-10 09:47:50 -07003137 err = rocker_group_l2_flood(rocker_port, trans, flags, vlan_id,
3138 group_count, group_ids, group_id);
Scott Feldman6c707942014-11-28 14:34:28 +01003139 if (err)
3140 netdev_err(rocker_port->dev,
3141 "Error (%d) port VLAN l2 flood group\n", err);
3142
Scott Feldman04f49fa2015-03-15 23:04:46 -07003143no_ports_in_vlan:
Simon Horman0985df72015-05-25 14:28:35 +09003144 rocker_port_kfree(trans, group_ids);
Scott Feldman6c707942014-11-28 14:34:28 +01003145 return err;
3146}
3147
3148static int rocker_port_vlan_l2_groups(struct rocker_port *rocker_port,
Scott Feldmanc4f20322015-05-10 09:47:50 -07003149 enum switchdev_trans trans, int flags,
3150 __be16 vlan_id, bool pop_vlan)
Scott Feldman6c707942014-11-28 14:34:28 +01003151{
Simon Hormane5054642015-05-25 14:28:36 +09003152 const struct rocker *rocker = rocker_port->rocker;
Scott Feldman6c707942014-11-28 14:34:28 +01003153 struct rocker_port *p;
3154 bool adding = !(flags & ROCKER_OP_FLAG_REMOVE);
Scott Feldman4a6bb6d2015-02-25 20:15:37 -08003155 u32 out_pport;
Scott Feldman6c707942014-11-28 14:34:28 +01003156 int ref = 0;
3157 int err;
3158 int i;
3159
3160 /* An L2 interface group for this port in this VLAN, but
3161 * only when port STP state is LEARNING|FORWARDING.
3162 */
3163
3164 if (rocker_port->stp_state == BR_STATE_LEARNING ||
3165 rocker_port->stp_state == BR_STATE_FORWARDING) {
Scott Feldman4a6bb6d2015-02-25 20:15:37 -08003166 out_pport = rocker_port->pport;
Scott Feldmanc4f20322015-05-10 09:47:50 -07003167 err = rocker_group_l2_interface(rocker_port, trans, flags,
3168 vlan_id, out_pport, pop_vlan);
Scott Feldman6c707942014-11-28 14:34:28 +01003169 if (err) {
3170 netdev_err(rocker_port->dev,
Scott Feldman4a6bb6d2015-02-25 20:15:37 -08003171 "Error (%d) port VLAN l2 group for pport %d\n",
3172 err, out_pport);
Scott Feldman6c707942014-11-28 14:34:28 +01003173 return err;
3174 }
3175 }
3176
3177 /* An L2 interface group for this VLAN to CPU port.
3178 * Add when first port joins this VLAN and destroy when
3179 * last port leaves this VLAN.
3180 */
3181
3182 for (i = 0; i < rocker->port_count; i++) {
3183 p = rocker->ports[i];
Scott Feldmanbcfd7802015-06-01 11:39:04 -07003184 if (p && test_bit(ntohs(vlan_id), p->vlan_bitmap))
Scott Feldman6c707942014-11-28 14:34:28 +01003185 ref++;
3186 }
3187
3188 if ((!adding || ref != 1) && (adding || ref != 0))
3189 return 0;
3190
Scott Feldman4a6bb6d2015-02-25 20:15:37 -08003191 out_pport = 0;
Scott Feldmanc4f20322015-05-10 09:47:50 -07003192 err = rocker_group_l2_interface(rocker_port, trans, flags,
3193 vlan_id, out_pport, pop_vlan);
Scott Feldman6c707942014-11-28 14:34:28 +01003194 if (err) {
3195 netdev_err(rocker_port->dev,
3196 "Error (%d) port VLAN l2 group for CPU port\n", err);
3197 return err;
3198 }
3199
3200 return 0;
3201}
3202
Scott Feldman9f6bbf72014-11-28 14:34:27 +01003203static struct rocker_ctrl {
3204 const u8 *eth_dst;
3205 const u8 *eth_dst_mask;
Jiri Pirko11e6c652014-12-03 14:14:54 +01003206 __be16 eth_type;
Scott Feldman9f6bbf72014-11-28 14:34:27 +01003207 bool acl;
3208 bool bridge;
3209 bool term;
3210 bool copy_to_cpu;
3211} rocker_ctrls[] = {
3212 [ROCKER_CTRL_LINK_LOCAL_MCAST] = {
3213 /* pass link local multicast pkts up to CPU for filtering */
3214 .eth_dst = ll_mac,
3215 .eth_dst_mask = ll_mask,
3216 .acl = true,
3217 },
3218 [ROCKER_CTRL_LOCAL_ARP] = {
3219 /* pass local ARP pkts up to CPU */
3220 .eth_dst = zero_mac,
3221 .eth_dst_mask = zero_mac,
3222 .eth_type = htons(ETH_P_ARP),
3223 .acl = true,
3224 },
3225 [ROCKER_CTRL_IPV4_MCAST] = {
3226 /* pass IPv4 mcast pkts up to CPU, RFC 1112 */
3227 .eth_dst = ipv4_mcast,
3228 .eth_dst_mask = ipv4_mask,
3229 .eth_type = htons(ETH_P_IP),
3230 .term = true,
3231 .copy_to_cpu = true,
3232 },
3233 [ROCKER_CTRL_IPV6_MCAST] = {
3234 /* pass IPv6 mcast pkts up to CPU, RFC 2464 */
3235 .eth_dst = ipv6_mcast,
3236 .eth_dst_mask = ipv6_mask,
3237 .eth_type = htons(ETH_P_IPV6),
3238 .term = true,
3239 .copy_to_cpu = true,
3240 },
3241 [ROCKER_CTRL_DFLT_BRIDGING] = {
3242 /* flood any pkts on vlan */
3243 .bridge = true,
3244 .copy_to_cpu = true,
3245 },
3246};
3247
3248static int rocker_port_ctrl_vlan_acl(struct rocker_port *rocker_port,
Scott Feldmanc4f20322015-05-10 09:47:50 -07003249 enum switchdev_trans trans, int flags,
Simon Hormane5054642015-05-25 14:28:36 +09003250 const struct rocker_ctrl *ctrl, __be16 vlan_id)
Scott Feldman9f6bbf72014-11-28 14:34:27 +01003251{
Scott Feldman4a6bb6d2015-02-25 20:15:37 -08003252 u32 in_pport = rocker_port->pport;
3253 u32 in_pport_mask = 0xffffffff;
3254 u32 out_pport = 0;
Simon Hormane5054642015-05-25 14:28:36 +09003255 const u8 *eth_src = NULL;
3256 const u8 *eth_src_mask = NULL;
Scott Feldman9f6bbf72014-11-28 14:34:27 +01003257 __be16 vlan_id_mask = htons(0xffff);
3258 u8 ip_proto = 0;
3259 u8 ip_proto_mask = 0;
3260 u8 ip_tos = 0;
3261 u8 ip_tos_mask = 0;
Scott Feldman4a6bb6d2015-02-25 20:15:37 -08003262 u32 group_id = ROCKER_GROUP_L2_INTERFACE(vlan_id, out_pport);
Scott Feldman9f6bbf72014-11-28 14:34:27 +01003263 int err;
3264
Scott Feldmanc4f20322015-05-10 09:47:50 -07003265 err = rocker_flow_tbl_acl(rocker_port, trans, flags,
Scott Feldman4a6bb6d2015-02-25 20:15:37 -08003266 in_pport, in_pport_mask,
Scott Feldman9f6bbf72014-11-28 14:34:27 +01003267 eth_src, eth_src_mask,
3268 ctrl->eth_dst, ctrl->eth_dst_mask,
3269 ctrl->eth_type,
3270 vlan_id, vlan_id_mask,
3271 ip_proto, ip_proto_mask,
3272 ip_tos, ip_tos_mask,
3273 group_id);
3274
3275 if (err)
3276 netdev_err(rocker_port->dev, "Error (%d) ctrl ACL\n", err);
3277
3278 return err;
3279}
3280
Scott Feldman6c707942014-11-28 14:34:28 +01003281static int rocker_port_ctrl_vlan_bridge(struct rocker_port *rocker_port,
Scott Feldmanc4f20322015-05-10 09:47:50 -07003282 enum switchdev_trans trans, int flags,
Simon Hormane5054642015-05-25 14:28:36 +09003283 const struct rocker_ctrl *ctrl,
Scott Feldman6c707942014-11-28 14:34:28 +01003284 __be16 vlan_id)
3285{
3286 enum rocker_of_dpa_table_id goto_tbl =
3287 ROCKER_OF_DPA_TABLE_ID_ACL_POLICY;
3288 u32 group_id = ROCKER_GROUP_L2_FLOOD(vlan_id, 0);
3289 u32 tunnel_id = 0;
3290 int err;
3291
3292 if (!rocker_port_is_bridged(rocker_port))
3293 return 0;
3294
Scott Feldmanc4f20322015-05-10 09:47:50 -07003295 err = rocker_flow_tbl_bridge(rocker_port, trans, flags,
Scott Feldman6c707942014-11-28 14:34:28 +01003296 ctrl->eth_dst, ctrl->eth_dst_mask,
3297 vlan_id, tunnel_id,
3298 goto_tbl, group_id, ctrl->copy_to_cpu);
3299
3300 if (err)
3301 netdev_err(rocker_port->dev, "Error (%d) ctrl FLOOD\n", err);
3302
3303 return err;
3304}
3305
Scott Feldman9f6bbf72014-11-28 14:34:27 +01003306static int rocker_port_ctrl_vlan_term(struct rocker_port *rocker_port,
Scott Feldmanc4f20322015-05-10 09:47:50 -07003307 enum switchdev_trans trans, int flags,
Simon Hormane5054642015-05-25 14:28:36 +09003308 const struct rocker_ctrl *ctrl, __be16 vlan_id)
Scott Feldman9f6bbf72014-11-28 14:34:27 +01003309{
Scott Feldman4a6bb6d2015-02-25 20:15:37 -08003310 u32 in_pport_mask = 0xffffffff;
Scott Feldman9f6bbf72014-11-28 14:34:27 +01003311 __be16 vlan_id_mask = htons(0xffff);
3312 int err;
3313
3314 if (ntohs(vlan_id) == 0)
3315 vlan_id = rocker_port->internal_vlan_id;
3316
Scott Feldmanc4f20322015-05-10 09:47:50 -07003317 err = rocker_flow_tbl_term_mac(rocker_port, trans,
Scott Feldman4a6bb6d2015-02-25 20:15:37 -08003318 rocker_port->pport, in_pport_mask,
Scott Feldman9f6bbf72014-11-28 14:34:27 +01003319 ctrl->eth_type, ctrl->eth_dst,
3320 ctrl->eth_dst_mask, vlan_id,
3321 vlan_id_mask, ctrl->copy_to_cpu,
3322 flags);
3323
3324 if (err)
3325 netdev_err(rocker_port->dev, "Error (%d) ctrl term\n", err);
3326
3327 return err;
3328}
3329
Scott Feldmanc4f20322015-05-10 09:47:50 -07003330static int rocker_port_ctrl_vlan(struct rocker_port *rocker_port,
3331 enum switchdev_trans trans, int flags,
Simon Hormane5054642015-05-25 14:28:36 +09003332 const struct rocker_ctrl *ctrl, __be16 vlan_id)
Scott Feldman9f6bbf72014-11-28 14:34:27 +01003333{
3334 if (ctrl->acl)
Scott Feldmanc4f20322015-05-10 09:47:50 -07003335 return rocker_port_ctrl_vlan_acl(rocker_port, trans, flags,
Scott Feldman9f6bbf72014-11-28 14:34:27 +01003336 ctrl, vlan_id);
Scott Feldman6c707942014-11-28 14:34:28 +01003337 if (ctrl->bridge)
Scott Feldmanc4f20322015-05-10 09:47:50 -07003338 return rocker_port_ctrl_vlan_bridge(rocker_port, trans, flags,
Scott Feldman6c707942014-11-28 14:34:28 +01003339 ctrl, vlan_id);
Scott Feldman9f6bbf72014-11-28 14:34:27 +01003340
3341 if (ctrl->term)
Scott Feldmanc4f20322015-05-10 09:47:50 -07003342 return rocker_port_ctrl_vlan_term(rocker_port, trans, flags,
Scott Feldman9f6bbf72014-11-28 14:34:27 +01003343 ctrl, vlan_id);
3344
3345 return -EOPNOTSUPP;
3346}
3347
3348static int rocker_port_ctrl_vlan_add(struct rocker_port *rocker_port,
Scott Feldmanc4f20322015-05-10 09:47:50 -07003349 enum switchdev_trans trans, int flags,
3350 __be16 vlan_id)
Scott Feldman9f6bbf72014-11-28 14:34:27 +01003351{
3352 int err = 0;
3353 int i;
3354
3355 for (i = 0; i < ROCKER_CTRL_MAX; i++) {
3356 if (rocker_port->ctrls[i]) {
Scott Feldmanc4f20322015-05-10 09:47:50 -07003357 err = rocker_port_ctrl_vlan(rocker_port, trans, flags,
Scott Feldman9f6bbf72014-11-28 14:34:27 +01003358 &rocker_ctrls[i], vlan_id);
3359 if (err)
3360 return err;
3361 }
3362 }
3363
3364 return err;
3365}
3366
Scott Feldmanc4f20322015-05-10 09:47:50 -07003367static int rocker_port_ctrl(struct rocker_port *rocker_port,
3368 enum switchdev_trans trans, int flags,
Simon Hormane5054642015-05-25 14:28:36 +09003369 const struct rocker_ctrl *ctrl)
Scott Feldman9f6bbf72014-11-28 14:34:27 +01003370{
3371 u16 vid;
3372 int err = 0;
3373
3374 for (vid = 1; vid < VLAN_N_VID; vid++) {
3375 if (!test_bit(vid, rocker_port->vlan_bitmap))
3376 continue;
Scott Feldmanc4f20322015-05-10 09:47:50 -07003377 err = rocker_port_ctrl_vlan(rocker_port, trans, flags,
Scott Feldman9f6bbf72014-11-28 14:34:27 +01003378 ctrl, htons(vid));
3379 if (err)
3380 break;
3381 }
3382
3383 return err;
3384}
3385
Scott Feldmanc4f20322015-05-10 09:47:50 -07003386static int rocker_port_vlan(struct rocker_port *rocker_port,
3387 enum switchdev_trans trans, int flags, u16 vid)
Scott Feldman6c707942014-11-28 14:34:28 +01003388{
3389 enum rocker_of_dpa_table_id goto_tbl =
3390 ROCKER_OF_DPA_TABLE_ID_TERMINATION_MAC;
Scott Feldman4a6bb6d2015-02-25 20:15:37 -08003391 u32 in_pport = rocker_port->pport;
Scott Feldman6c707942014-11-28 14:34:28 +01003392 __be16 vlan_id = htons(vid);
3393 __be16 vlan_id_mask = htons(0xffff);
3394 __be16 internal_vlan_id;
3395 bool untagged;
3396 bool adding = !(flags & ROCKER_OP_FLAG_REMOVE);
3397 int err;
3398
3399 internal_vlan_id = rocker_port_vid_to_vlan(rocker_port, vid, &untagged);
3400
Scott Feldman9228ad22015-05-10 09:47:54 -07003401 if (adding && test_bit(ntohs(internal_vlan_id),
3402 rocker_port->vlan_bitmap))
Scott Feldman6c707942014-11-28 14:34:28 +01003403 return 0; /* already added */
Scott Feldman9228ad22015-05-10 09:47:54 -07003404 else if (!adding && !test_bit(ntohs(internal_vlan_id),
3405 rocker_port->vlan_bitmap))
Scott Feldman6c707942014-11-28 14:34:28 +01003406 return 0; /* already removed */
3407
Scott Feldman9228ad22015-05-10 09:47:54 -07003408 change_bit(ntohs(internal_vlan_id), rocker_port->vlan_bitmap);
3409
Scott Feldman6c707942014-11-28 14:34:28 +01003410 if (adding) {
Scott Feldmanc4f20322015-05-10 09:47:50 -07003411 err = rocker_port_ctrl_vlan_add(rocker_port, trans, flags,
Scott Feldman6c707942014-11-28 14:34:28 +01003412 internal_vlan_id);
3413 if (err) {
3414 netdev_err(rocker_port->dev,
3415 "Error (%d) port ctrl vlan add\n", err);
Scott Feldman9228ad22015-05-10 09:47:54 -07003416 goto err_out;
Scott Feldman6c707942014-11-28 14:34:28 +01003417 }
3418 }
3419
Scott Feldmanc4f20322015-05-10 09:47:50 -07003420 err = rocker_port_vlan_l2_groups(rocker_port, trans, flags,
Scott Feldman6c707942014-11-28 14:34:28 +01003421 internal_vlan_id, untagged);
3422 if (err) {
3423 netdev_err(rocker_port->dev,
3424 "Error (%d) port VLAN l2 groups\n", err);
Scott Feldman9228ad22015-05-10 09:47:54 -07003425 goto err_out;
Scott Feldman6c707942014-11-28 14:34:28 +01003426 }
3427
Scott Feldmanc4f20322015-05-10 09:47:50 -07003428 err = rocker_port_vlan_flood_group(rocker_port, trans, flags,
Scott Feldman6c707942014-11-28 14:34:28 +01003429 internal_vlan_id);
3430 if (err) {
3431 netdev_err(rocker_port->dev,
3432 "Error (%d) port VLAN l2 flood group\n", err);
Scott Feldman9228ad22015-05-10 09:47:54 -07003433 goto err_out;
Scott Feldman6c707942014-11-28 14:34:28 +01003434 }
3435
Scott Feldmanc4f20322015-05-10 09:47:50 -07003436 err = rocker_flow_tbl_vlan(rocker_port, trans, flags,
Scott Feldman4a6bb6d2015-02-25 20:15:37 -08003437 in_pport, vlan_id, vlan_id_mask,
Scott Feldman6c707942014-11-28 14:34:28 +01003438 goto_tbl, untagged, internal_vlan_id);
3439 if (err)
3440 netdev_err(rocker_port->dev,
3441 "Error (%d) port VLAN table\n", err);
3442
Scott Feldman9228ad22015-05-10 09:47:54 -07003443err_out:
3444 if (trans == SWITCHDEV_TRANS_PREPARE)
3445 change_bit(ntohs(internal_vlan_id), rocker_port->vlan_bitmap);
3446
Scott Feldman6c707942014-11-28 14:34:28 +01003447 return err;
3448}
3449
Scott Feldmanc4f20322015-05-10 09:47:50 -07003450static int rocker_port_ig_tbl(struct rocker_port *rocker_port,
3451 enum switchdev_trans trans, int flags)
Scott Feldman9f6bbf72014-11-28 14:34:27 +01003452{
3453 enum rocker_of_dpa_table_id goto_tbl;
Scott Feldman4a6bb6d2015-02-25 20:15:37 -08003454 u32 in_pport;
3455 u32 in_pport_mask;
Scott Feldman9f6bbf72014-11-28 14:34:27 +01003456 int err;
3457
3458 /* Normal Ethernet Frames. Matches pkts from any local physical
3459 * ports. Goto VLAN tbl.
3460 */
3461
Scott Feldman4a6bb6d2015-02-25 20:15:37 -08003462 in_pport = 0;
3463 in_pport_mask = 0xffff0000;
Scott Feldman9f6bbf72014-11-28 14:34:27 +01003464 goto_tbl = ROCKER_OF_DPA_TABLE_ID_VLAN;
3465
Scott Feldmanc4f20322015-05-10 09:47:50 -07003466 err = rocker_flow_tbl_ig_port(rocker_port, trans, flags,
Scott Feldman4a6bb6d2015-02-25 20:15:37 -08003467 in_pport, in_pport_mask,
Scott Feldman9f6bbf72014-11-28 14:34:27 +01003468 goto_tbl);
3469 if (err)
3470 netdev_err(rocker_port->dev,
3471 "Error (%d) ingress port table entry\n", err);
3472
3473 return err;
3474}
3475
Scott Feldman6c707942014-11-28 14:34:28 +01003476struct rocker_fdb_learn_work {
3477 struct work_struct work;
Scott Feldmanc4f20322015-05-10 09:47:50 -07003478 struct rocker_port *rocker_port;
3479 enum switchdev_trans trans;
Scott Feldman6c707942014-11-28 14:34:28 +01003480 int flags;
3481 u8 addr[ETH_ALEN];
3482 u16 vid;
3483};
3484
3485static void rocker_port_fdb_learn_work(struct work_struct *work)
3486{
Simon Hormane5054642015-05-25 14:28:36 +09003487 const struct rocker_fdb_learn_work *lw =
Scott Feldman6c707942014-11-28 14:34:28 +01003488 container_of(work, struct rocker_fdb_learn_work, work);
3489 bool removing = (lw->flags & ROCKER_OP_FLAG_REMOVE);
3490 bool learned = (lw->flags & ROCKER_OP_FLAG_LEARNED);
Jiri Pirkoebb9a032015-05-10 09:47:46 -07003491 struct switchdev_notifier_fdb_info info;
Jiri Pirko3aeb6612015-01-15 23:49:37 +01003492
3493 info.addr = lw->addr;
3494 info.vid = lw->vid;
Scott Feldman6c707942014-11-28 14:34:28 +01003495
Thomas Graf51ace882014-11-28 14:34:32 +01003496 if (learned && removing)
Jiri Pirkoebb9a032015-05-10 09:47:46 -07003497 call_switchdev_notifiers(SWITCHDEV_FDB_DEL,
Scott Feldmanc4f20322015-05-10 09:47:50 -07003498 lw->rocker_port->dev, &info.info);
Thomas Graf51ace882014-11-28 14:34:32 +01003499 else if (learned && !removing)
Jiri Pirkoebb9a032015-05-10 09:47:46 -07003500 call_switchdev_notifiers(SWITCHDEV_FDB_ADD,
Scott Feldmanc4f20322015-05-10 09:47:50 -07003501 lw->rocker_port->dev, &info.info);
Scott Feldman6c707942014-11-28 14:34:28 +01003502
Simon Horman0985df72015-05-25 14:28:35 +09003503 rocker_port_kfree(lw->trans, work);
Scott Feldman6c707942014-11-28 14:34:28 +01003504}
3505
3506static int rocker_port_fdb_learn(struct rocker_port *rocker_port,
Scott Feldmanc4f20322015-05-10 09:47:50 -07003507 enum switchdev_trans trans, int flags,
3508 const u8 *addr, __be16 vlan_id)
Scott Feldman6c707942014-11-28 14:34:28 +01003509{
3510 struct rocker_fdb_learn_work *lw;
3511 enum rocker_of_dpa_table_id goto_tbl =
3512 ROCKER_OF_DPA_TABLE_ID_ACL_POLICY;
Scott Feldman4a6bb6d2015-02-25 20:15:37 -08003513 u32 out_pport = rocker_port->pport;
Scott Feldman6c707942014-11-28 14:34:28 +01003514 u32 tunnel_id = 0;
3515 u32 group_id = ROCKER_GROUP_NONE;
Scott Feldman5111f802014-11-28 14:34:30 +01003516 bool syncing = !!(rocker_port->brport_flags & BR_LEARNING_SYNC);
Scott Feldman6c707942014-11-28 14:34:28 +01003517 bool copy_to_cpu = false;
3518 int err;
3519
3520 if (rocker_port_is_bridged(rocker_port))
Scott Feldman4a6bb6d2015-02-25 20:15:37 -08003521 group_id = ROCKER_GROUP_L2_INTERFACE(vlan_id, out_pport);
Scott Feldman6c707942014-11-28 14:34:28 +01003522
3523 if (!(flags & ROCKER_OP_FLAG_REFRESH)) {
Scott Feldmanc4f20322015-05-10 09:47:50 -07003524 err = rocker_flow_tbl_bridge(rocker_port, trans, flags, addr,
3525 NULL, vlan_id, tunnel_id, goto_tbl,
Scott Feldman6c707942014-11-28 14:34:28 +01003526 group_id, copy_to_cpu);
3527 if (err)
3528 return err;
3529 }
3530
Scott Feldman5111f802014-11-28 14:34:30 +01003531 if (!syncing)
3532 return 0;
3533
Scott Feldman6c707942014-11-28 14:34:28 +01003534 if (!rocker_port_is_bridged(rocker_port))
3535 return 0;
3536
Scott Feldman179f9a22015-06-12 21:35:46 -07003537 lw = rocker_port_kzalloc(rocker_port, trans, flags, sizeof(*lw));
Scott Feldman6c707942014-11-28 14:34:28 +01003538 if (!lw)
3539 return -ENOMEM;
3540
3541 INIT_WORK(&lw->work, rocker_port_fdb_learn_work);
3542
Scott Feldmanc4f20322015-05-10 09:47:50 -07003543 lw->rocker_port = rocker_port;
3544 lw->trans = trans;
Scott Feldman6c707942014-11-28 14:34:28 +01003545 lw->flags = flags;
3546 ether_addr_copy(lw->addr, addr);
3547 lw->vid = rocker_port_vlan_to_vid(rocker_port, vlan_id);
3548
Scott Feldmanc4f20322015-05-10 09:47:50 -07003549 if (trans == SWITCHDEV_TRANS_PREPARE)
Simon Horman0985df72015-05-25 14:28:35 +09003550 rocker_port_kfree(trans, lw);
Scott Feldmanc4f20322015-05-10 09:47:50 -07003551 else
3552 schedule_work(&lw->work);
Scott Feldman6c707942014-11-28 14:34:28 +01003553
3554 return 0;
3555}
3556
3557static struct rocker_fdb_tbl_entry *
Simon Hormane5054642015-05-25 14:28:36 +09003558rocker_fdb_tbl_find(const struct rocker *rocker,
3559 const struct rocker_fdb_tbl_entry *match)
Scott Feldman6c707942014-11-28 14:34:28 +01003560{
3561 struct rocker_fdb_tbl_entry *found;
3562
3563 hash_for_each_possible(rocker->fdb_tbl, found, entry, match->key_crc32)
3564 if (memcmp(&found->key, &match->key, sizeof(found->key)) == 0)
3565 return found;
3566
3567 return NULL;
3568}
3569
3570static int rocker_port_fdb(struct rocker_port *rocker_port,
Scott Feldmanc4f20322015-05-10 09:47:50 -07003571 enum switchdev_trans trans,
Scott Feldman6c707942014-11-28 14:34:28 +01003572 const unsigned char *addr,
3573 __be16 vlan_id, int flags)
3574{
3575 struct rocker *rocker = rocker_port->rocker;
3576 struct rocker_fdb_tbl_entry *fdb;
3577 struct rocker_fdb_tbl_entry *found;
3578 bool removing = (flags & ROCKER_OP_FLAG_REMOVE);
3579 unsigned long lock_flags;
3580
Scott Feldman179f9a22015-06-12 21:35:46 -07003581 fdb = rocker_port_kzalloc(rocker_port, trans, flags, sizeof(*fdb));
Scott Feldman6c707942014-11-28 14:34:28 +01003582 if (!fdb)
3583 return -ENOMEM;
3584
3585 fdb->learned = (flags & ROCKER_OP_FLAG_LEARNED);
Scott Feldman4a6bb6d2015-02-25 20:15:37 -08003586 fdb->key.pport = rocker_port->pport;
Scott Feldman6c707942014-11-28 14:34:28 +01003587 ether_addr_copy(fdb->key.addr, addr);
3588 fdb->key.vlan_id = vlan_id;
3589 fdb->key_crc32 = crc32(~0, &fdb->key, sizeof(fdb->key));
3590
3591 spin_lock_irqsave(&rocker->fdb_tbl_lock, lock_flags);
3592
3593 found = rocker_fdb_tbl_find(rocker, fdb);
3594
3595 if (removing && found) {
Simon Horman0985df72015-05-25 14:28:35 +09003596 rocker_port_kfree(trans, fdb);
Simon Horman42e94882015-05-21 12:40:15 +09003597 if (trans != SWITCHDEV_TRANS_PREPARE)
3598 hash_del(&found->entry);
Scott Feldman6c707942014-11-28 14:34:28 +01003599 } else if (!removing && !found) {
Simon Horman42e94882015-05-21 12:40:15 +09003600 if (trans != SWITCHDEV_TRANS_PREPARE)
3601 hash_add(rocker->fdb_tbl, &fdb->entry, fdb->key_crc32);
Scott Feldman6c707942014-11-28 14:34:28 +01003602 }
3603
3604 spin_unlock_irqrestore(&rocker->fdb_tbl_lock, lock_flags);
3605
3606 /* Check if adding and already exists, or removing and can't find */
3607 if (!found != !removing) {
Simon Horman0985df72015-05-25 14:28:35 +09003608 rocker_port_kfree(trans, fdb);
Scott Feldman6c707942014-11-28 14:34:28 +01003609 if (!found && removing)
3610 return 0;
3611 /* Refreshing existing to update aging timers */
3612 flags |= ROCKER_OP_FLAG_REFRESH;
3613 }
3614
Scott Feldmanc4f20322015-05-10 09:47:50 -07003615 return rocker_port_fdb_learn(rocker_port, trans, flags, addr, vlan_id);
Scott Feldman6c707942014-11-28 14:34:28 +01003616}
3617
Scott Feldmanc4f20322015-05-10 09:47:50 -07003618static int rocker_port_fdb_flush(struct rocker_port *rocker_port,
Scott Feldman179f9a22015-06-12 21:35:46 -07003619 enum switchdev_trans trans, int flags)
Scott Feldman6c707942014-11-28 14:34:28 +01003620{
3621 struct rocker *rocker = rocker_port->rocker;
3622 struct rocker_fdb_tbl_entry *found;
3623 unsigned long lock_flags;
Scott Feldman6c707942014-11-28 14:34:28 +01003624 struct hlist_node *tmp;
3625 int bkt;
3626 int err = 0;
3627
3628 if (rocker_port->stp_state == BR_STATE_LEARNING ||
3629 rocker_port->stp_state == BR_STATE_FORWARDING)
3630 return 0;
3631
Scott Feldman179f9a22015-06-12 21:35:46 -07003632 flags |= ROCKER_OP_FLAG_REMOVE;
3633
Scott Feldman6c707942014-11-28 14:34:28 +01003634 spin_lock_irqsave(&rocker->fdb_tbl_lock, lock_flags);
3635
3636 hash_for_each_safe(rocker->fdb_tbl, bkt, tmp, found, entry) {
Scott Feldman4a6bb6d2015-02-25 20:15:37 -08003637 if (found->key.pport != rocker_port->pport)
Scott Feldman6c707942014-11-28 14:34:28 +01003638 continue;
3639 if (!found->learned)
3640 continue;
Scott Feldmanc4f20322015-05-10 09:47:50 -07003641 err = rocker_port_fdb_learn(rocker_port, trans, flags,
Scott Feldman6c707942014-11-28 14:34:28 +01003642 found->key.addr,
3643 found->key.vlan_id);
3644 if (err)
3645 goto err_out;
Simon Horman3098ac32015-05-21 12:40:14 +09003646 if (trans != SWITCHDEV_TRANS_PREPARE)
3647 hash_del(&found->entry);
Scott Feldman6c707942014-11-28 14:34:28 +01003648 }
3649
3650err_out:
3651 spin_unlock_irqrestore(&rocker->fdb_tbl_lock, lock_flags);
3652
3653 return err;
3654}
3655
Scott Feldman9f6bbf72014-11-28 14:34:27 +01003656static int rocker_port_router_mac(struct rocker_port *rocker_port,
Scott Feldmanc4f20322015-05-10 09:47:50 -07003657 enum switchdev_trans trans, int flags,
3658 __be16 vlan_id)
Scott Feldman9f6bbf72014-11-28 14:34:27 +01003659{
Scott Feldman4a6bb6d2015-02-25 20:15:37 -08003660 u32 in_pport_mask = 0xffffffff;
Scott Feldman9f6bbf72014-11-28 14:34:27 +01003661 __be16 eth_type;
3662 const u8 *dst_mac_mask = ff_mac;
3663 __be16 vlan_id_mask = htons(0xffff);
3664 bool copy_to_cpu = false;
3665 int err;
3666
3667 if (ntohs(vlan_id) == 0)
3668 vlan_id = rocker_port->internal_vlan_id;
3669
3670 eth_type = htons(ETH_P_IP);
Scott Feldmanc4f20322015-05-10 09:47:50 -07003671 err = rocker_flow_tbl_term_mac(rocker_port, trans,
Scott Feldman4a6bb6d2015-02-25 20:15:37 -08003672 rocker_port->pport, in_pport_mask,
Scott Feldman9f6bbf72014-11-28 14:34:27 +01003673 eth_type, rocker_port->dev->dev_addr,
3674 dst_mac_mask, vlan_id, vlan_id_mask,
3675 copy_to_cpu, flags);
3676 if (err)
3677 return err;
3678
3679 eth_type = htons(ETH_P_IPV6);
Scott Feldmanc4f20322015-05-10 09:47:50 -07003680 err = rocker_flow_tbl_term_mac(rocker_port, trans,
Scott Feldman4a6bb6d2015-02-25 20:15:37 -08003681 rocker_port->pport, in_pport_mask,
Scott Feldman9f6bbf72014-11-28 14:34:27 +01003682 eth_type, rocker_port->dev->dev_addr,
3683 dst_mac_mask, vlan_id, vlan_id_mask,
3684 copy_to_cpu, flags);
3685
3686 return err;
3687}
3688
Scott Feldmanc4f20322015-05-10 09:47:50 -07003689static int rocker_port_fwding(struct rocker_port *rocker_port,
Scott Feldman179f9a22015-06-12 21:35:46 -07003690 enum switchdev_trans trans, int flags)
Scott Feldman6c707942014-11-28 14:34:28 +01003691{
3692 bool pop_vlan;
Scott Feldman4a6bb6d2015-02-25 20:15:37 -08003693 u32 out_pport;
Scott Feldman6c707942014-11-28 14:34:28 +01003694 __be16 vlan_id;
3695 u16 vid;
Scott Feldman6c707942014-11-28 14:34:28 +01003696 int err;
3697
3698 /* Port will be forwarding-enabled if its STP state is LEARNING
3699 * or FORWARDING. Traffic from CPU can still egress, regardless of
3700 * port STP state. Use L2 interface group on port VLANs as a way
3701 * to toggle port forwarding: if forwarding is disabled, L2
3702 * interface group will not exist.
3703 */
3704
3705 if (rocker_port->stp_state != BR_STATE_LEARNING &&
3706 rocker_port->stp_state != BR_STATE_FORWARDING)
3707 flags |= ROCKER_OP_FLAG_REMOVE;
3708
Scott Feldman4a6bb6d2015-02-25 20:15:37 -08003709 out_pport = rocker_port->pport;
Scott Feldman6c707942014-11-28 14:34:28 +01003710 for (vid = 1; vid < VLAN_N_VID; vid++) {
3711 if (!test_bit(vid, rocker_port->vlan_bitmap))
3712 continue;
3713 vlan_id = htons(vid);
3714 pop_vlan = rocker_vlan_id_is_internal(vlan_id);
Scott Feldmanc4f20322015-05-10 09:47:50 -07003715 err = rocker_group_l2_interface(rocker_port, trans, flags,
3716 vlan_id, out_pport, pop_vlan);
Scott Feldman6c707942014-11-28 14:34:28 +01003717 if (err) {
3718 netdev_err(rocker_port->dev,
Scott Feldman4a6bb6d2015-02-25 20:15:37 -08003719 "Error (%d) port VLAN l2 group for pport %d\n",
3720 err, out_pport);
Scott Feldman6c707942014-11-28 14:34:28 +01003721 return err;
3722 }
3723 }
3724
3725 return 0;
3726}
3727
Scott Feldmanc4f20322015-05-10 09:47:50 -07003728static int rocker_port_stp_update(struct rocker_port *rocker_port,
Scott Feldman179f9a22015-06-12 21:35:46 -07003729 enum switchdev_trans trans, int flags,
3730 u8 state)
Scott Feldman6c707942014-11-28 14:34:28 +01003731{
3732 bool want[ROCKER_CTRL_MAX] = { 0, };
Scott Feldmanc4f20322015-05-10 09:47:50 -07003733 bool prev_ctrls[ROCKER_CTRL_MAX];
3734 u8 prev_state;
Scott Feldman6c707942014-11-28 14:34:28 +01003735 int err;
3736 int i;
3737
Scott Feldmanc4f20322015-05-10 09:47:50 -07003738 if (trans == SWITCHDEV_TRANS_PREPARE) {
3739 memcpy(prev_ctrls, rocker_port->ctrls, sizeof(prev_ctrls));
3740 prev_state = rocker_port->stp_state;
3741 }
3742
Scott Feldman6c707942014-11-28 14:34:28 +01003743 if (rocker_port->stp_state == state)
3744 return 0;
3745
3746 rocker_port->stp_state = state;
3747
3748 switch (state) {
3749 case BR_STATE_DISABLED:
3750 /* port is completely disabled */
3751 break;
3752 case BR_STATE_LISTENING:
3753 case BR_STATE_BLOCKING:
3754 want[ROCKER_CTRL_LINK_LOCAL_MCAST] = true;
3755 break;
3756 case BR_STATE_LEARNING:
3757 case BR_STATE_FORWARDING:
3758 want[ROCKER_CTRL_LINK_LOCAL_MCAST] = true;
3759 want[ROCKER_CTRL_IPV4_MCAST] = true;
3760 want[ROCKER_CTRL_IPV6_MCAST] = true;
3761 if (rocker_port_is_bridged(rocker_port))
3762 want[ROCKER_CTRL_DFLT_BRIDGING] = true;
3763 else
3764 want[ROCKER_CTRL_LOCAL_ARP] = true;
3765 break;
3766 }
3767
3768 for (i = 0; i < ROCKER_CTRL_MAX; i++) {
3769 if (want[i] != rocker_port->ctrls[i]) {
Scott Feldman179f9a22015-06-12 21:35:46 -07003770 int ctrl_flags = flags |
3771 (want[i] ? 0 : ROCKER_OP_FLAG_REMOVE);
3772 err = rocker_port_ctrl(rocker_port, trans, ctrl_flags,
Scott Feldman6c707942014-11-28 14:34:28 +01003773 &rocker_ctrls[i]);
3774 if (err)
Scott Feldmanc4f20322015-05-10 09:47:50 -07003775 goto err_out;
Scott Feldman6c707942014-11-28 14:34:28 +01003776 rocker_port->ctrls[i] = want[i];
3777 }
3778 }
3779
Scott Feldman179f9a22015-06-12 21:35:46 -07003780 err = rocker_port_fdb_flush(rocker_port, trans, flags);
Scott Feldman6c707942014-11-28 14:34:28 +01003781 if (err)
Scott Feldmanc4f20322015-05-10 09:47:50 -07003782 goto err_out;
Scott Feldman6c707942014-11-28 14:34:28 +01003783
Scott Feldman179f9a22015-06-12 21:35:46 -07003784 err = rocker_port_fwding(rocker_port, trans, flags);
Scott Feldmanc4f20322015-05-10 09:47:50 -07003785
3786err_out:
3787 if (trans == SWITCHDEV_TRANS_PREPARE) {
3788 memcpy(rocker_port->ctrls, prev_ctrls, sizeof(prev_ctrls));
3789 rocker_port->stp_state = prev_state;
3790 }
3791
3792 return err;
Scott Feldman6c707942014-11-28 14:34:28 +01003793}
3794
Scott Feldmanc4f20322015-05-10 09:47:50 -07003795static int rocker_port_fwd_enable(struct rocker_port *rocker_port,
Scott Feldman179f9a22015-06-12 21:35:46 -07003796 enum switchdev_trans trans, int flags)
Scott Feldmane47172a2015-02-25 20:15:38 -08003797{
3798 if (rocker_port_is_bridged(rocker_port))
3799 /* bridge STP will enable port */
3800 return 0;
3801
3802 /* port is not bridged, so simulate going to FORWARDING state */
Scott Feldman179f9a22015-06-12 21:35:46 -07003803 return rocker_port_stp_update(rocker_port, trans, flags,
3804 BR_STATE_FORWARDING);
Scott Feldmane47172a2015-02-25 20:15:38 -08003805}
3806
Scott Feldmanc4f20322015-05-10 09:47:50 -07003807static int rocker_port_fwd_disable(struct rocker_port *rocker_port,
Scott Feldman179f9a22015-06-12 21:35:46 -07003808 enum switchdev_trans trans, int flags)
Scott Feldmane47172a2015-02-25 20:15:38 -08003809{
3810 if (rocker_port_is_bridged(rocker_port))
3811 /* bridge STP will disable port */
3812 return 0;
3813
3814 /* port is not bridged, so simulate going to DISABLED state */
Scott Feldman179f9a22015-06-12 21:35:46 -07003815 return rocker_port_stp_update(rocker_port, trans, flags,
3816 BR_STATE_DISABLED);
Scott Feldmane47172a2015-02-25 20:15:38 -08003817}
3818
Scott Feldman9f6bbf72014-11-28 14:34:27 +01003819static struct rocker_internal_vlan_tbl_entry *
Simon Hormane5054642015-05-25 14:28:36 +09003820rocker_internal_vlan_tbl_find(const struct rocker *rocker, int ifindex)
Scott Feldman9f6bbf72014-11-28 14:34:27 +01003821{
3822 struct rocker_internal_vlan_tbl_entry *found;
3823
3824 hash_for_each_possible(rocker->internal_vlan_tbl, found,
3825 entry, ifindex) {
3826 if (found->ifindex == ifindex)
3827 return found;
3828 }
3829
3830 return NULL;
3831}
3832
3833static __be16 rocker_port_internal_vlan_id_get(struct rocker_port *rocker_port,
3834 int ifindex)
3835{
3836 struct rocker *rocker = rocker_port->rocker;
3837 struct rocker_internal_vlan_tbl_entry *entry;
3838 struct rocker_internal_vlan_tbl_entry *found;
3839 unsigned long lock_flags;
3840 int i;
3841
Simon Hormandf6a2062015-05-21 12:40:17 +09003842 entry = kzalloc(sizeof(*entry), GFP_KERNEL);
Scott Feldman9f6bbf72014-11-28 14:34:27 +01003843 if (!entry)
3844 return 0;
3845
3846 entry->ifindex = ifindex;
3847
3848 spin_lock_irqsave(&rocker->internal_vlan_tbl_lock, lock_flags);
3849
3850 found = rocker_internal_vlan_tbl_find(rocker, ifindex);
3851 if (found) {
Simon Hormandf6a2062015-05-21 12:40:17 +09003852 kfree(entry);
Scott Feldman9f6bbf72014-11-28 14:34:27 +01003853 goto found;
3854 }
3855
3856 found = entry;
3857 hash_add(rocker->internal_vlan_tbl, &found->entry, found->ifindex);
3858
3859 for (i = 0; i < ROCKER_N_INTERNAL_VLANS; i++) {
3860 if (test_and_set_bit(i, rocker->internal_vlan_bitmap))
3861 continue;
3862 found->vlan_id = htons(ROCKER_INTERNAL_VLAN_ID_BASE + i);
3863 goto found;
3864 }
3865
3866 netdev_err(rocker_port->dev, "Out of internal VLAN IDs\n");
3867
3868found:
3869 found->ref_count++;
3870 spin_unlock_irqrestore(&rocker->internal_vlan_tbl_lock, lock_flags);
3871
3872 return found->vlan_id;
3873}
3874
Simon Hormane5054642015-05-25 14:28:36 +09003875static void
3876rocker_port_internal_vlan_id_put(const struct rocker_port *rocker_port,
3877 int ifindex)
Scott Feldman9f6bbf72014-11-28 14:34:27 +01003878{
3879 struct rocker *rocker = rocker_port->rocker;
3880 struct rocker_internal_vlan_tbl_entry *found;
3881 unsigned long lock_flags;
3882 unsigned long bit;
3883
3884 spin_lock_irqsave(&rocker->internal_vlan_tbl_lock, lock_flags);
3885
3886 found = rocker_internal_vlan_tbl_find(rocker, ifindex);
3887 if (!found) {
3888 netdev_err(rocker_port->dev,
3889 "ifindex (%d) not found in internal VLAN tbl\n",
3890 ifindex);
3891 goto not_found;
3892 }
3893
3894 if (--found->ref_count <= 0) {
3895 bit = ntohs(found->vlan_id) - ROCKER_INTERNAL_VLAN_ID_BASE;
3896 clear_bit(bit, rocker->internal_vlan_bitmap);
3897 hash_del(&found->entry);
Simon Hormandf6a2062015-05-21 12:40:17 +09003898 kfree(found);
Scott Feldman9f6bbf72014-11-28 14:34:27 +01003899 }
3900
3901not_found:
3902 spin_unlock_irqrestore(&rocker->internal_vlan_tbl_lock, lock_flags);
3903}
3904
Scott Feldmanc4f20322015-05-10 09:47:50 -07003905static int rocker_port_fib_ipv4(struct rocker_port *rocker_port,
3906 enum switchdev_trans trans, __be32 dst,
Simon Hormane5054642015-05-25 14:28:36 +09003907 int dst_len, const struct fib_info *fi,
3908 u32 tb_id, int flags)
Scott Feldmanc1beeef2015-03-05 21:21:20 -08003909{
Simon Hormane5054642015-05-25 14:28:36 +09003910 const struct fib_nh *nh;
Scott Feldmanc1beeef2015-03-05 21:21:20 -08003911 __be16 eth_type = htons(ETH_P_IP);
3912 __be32 dst_mask = inet_make_mask(dst_len);
3913 __be16 internal_vlan_id = rocker_port->internal_vlan_id;
3914 u32 priority = fi->fib_priority;
3915 enum rocker_of_dpa_table_id goto_tbl =
3916 ROCKER_OF_DPA_TABLE_ID_ACL_POLICY;
3917 u32 group_id;
3918 bool nh_on_port;
3919 bool has_gw;
3920 u32 index;
3921 int err;
3922
3923 /* XXX support ECMP */
3924
3925 nh = fi->fib_nh;
3926 nh_on_port = (fi->fib_dev == rocker_port->dev);
3927 has_gw = !!nh->nh_gw;
3928
3929 if (has_gw && nh_on_port) {
Scott Feldmanc4f20322015-05-10 09:47:50 -07003930 err = rocker_port_ipv4_nh(rocker_port, trans, flags,
Scott Feldmanc1beeef2015-03-05 21:21:20 -08003931 nh->nh_gw, &index);
3932 if (err)
3933 return err;
3934
3935 group_id = ROCKER_GROUP_L3_UNICAST(index);
3936 } else {
3937 /* Send to CPU for processing */
3938 group_id = ROCKER_GROUP_L2_INTERFACE(internal_vlan_id, 0);
3939 }
3940
Scott Feldmanc4f20322015-05-10 09:47:50 -07003941 err = rocker_flow_tbl_ucast4_routing(rocker_port, trans, eth_type, dst,
Scott Feldmanc1beeef2015-03-05 21:21:20 -08003942 dst_mask, priority, goto_tbl,
3943 group_id, flags);
3944 if (err)
3945 netdev_err(rocker_port->dev, "Error (%d) IPv4 route %pI4\n",
3946 err, &dst);
3947
3948 return err;
3949}
3950
Jiri Pirko4b8ac962014-11-28 14:34:26 +01003951/*****************
3952 * Net device ops
3953 *****************/
3954
3955static int rocker_port_open(struct net_device *dev)
3956{
3957 struct rocker_port *rocker_port = netdev_priv(dev);
3958 int err;
3959
3960 err = rocker_port_dma_rings_init(rocker_port);
3961 if (err)
3962 return err;
3963
3964 err = request_irq(rocker_msix_tx_vector(rocker_port),
3965 rocker_tx_irq_handler, 0,
3966 rocker_driver_name, rocker_port);
3967 if (err) {
3968 netdev_err(rocker_port->dev, "cannot assign tx irq\n");
3969 goto err_request_tx_irq;
3970 }
3971
3972 err = request_irq(rocker_msix_rx_vector(rocker_port),
3973 rocker_rx_irq_handler, 0,
3974 rocker_driver_name, rocker_port);
3975 if (err) {
3976 netdev_err(rocker_port->dev, "cannot assign rx irq\n");
3977 goto err_request_rx_irq;
3978 }
3979
Scott Feldman179f9a22015-06-12 21:35:46 -07003980 err = rocker_port_fwd_enable(rocker_port, SWITCHDEV_TRANS_NONE, 0);
Scott Feldman6c707942014-11-28 14:34:28 +01003981 if (err)
Scott Feldmane47172a2015-02-25 20:15:38 -08003982 goto err_fwd_enable;
Scott Feldman6c707942014-11-28 14:34:28 +01003983
Jiri Pirko4b8ac962014-11-28 14:34:26 +01003984 napi_enable(&rocker_port->napi_tx);
3985 napi_enable(&rocker_port->napi_rx);
3986 rocker_port_set_enable(rocker_port, true);
3987 netif_start_queue(dev);
3988 return 0;
3989
Scott Feldmane47172a2015-02-25 20:15:38 -08003990err_fwd_enable:
Scott Feldman6c707942014-11-28 14:34:28 +01003991 free_irq(rocker_msix_rx_vector(rocker_port), rocker_port);
Jiri Pirko4b8ac962014-11-28 14:34:26 +01003992err_request_rx_irq:
3993 free_irq(rocker_msix_tx_vector(rocker_port), rocker_port);
3994err_request_tx_irq:
3995 rocker_port_dma_rings_fini(rocker_port);
3996 return err;
3997}
3998
3999static int rocker_port_stop(struct net_device *dev)
4000{
4001 struct rocker_port *rocker_port = netdev_priv(dev);
4002
4003 netif_stop_queue(dev);
4004 rocker_port_set_enable(rocker_port, false);
4005 napi_disable(&rocker_port->napi_rx);
4006 napi_disable(&rocker_port->napi_tx);
Scott Feldmanf66feaa2015-06-12 21:35:50 -07004007 rocker_port_fwd_disable(rocker_port, SWITCHDEV_TRANS_NONE,
4008 ROCKER_OP_FLAG_NOWAIT);
Jiri Pirko4b8ac962014-11-28 14:34:26 +01004009 free_irq(rocker_msix_rx_vector(rocker_port), rocker_port);
4010 free_irq(rocker_msix_tx_vector(rocker_port), rocker_port);
4011 rocker_port_dma_rings_fini(rocker_port);
4012
4013 return 0;
4014}
4015
Simon Hormane5054642015-05-25 14:28:36 +09004016static void rocker_tx_desc_frags_unmap(const struct rocker_port *rocker_port,
4017 const struct rocker_desc_info *desc_info)
Jiri Pirko4b8ac962014-11-28 14:34:26 +01004018{
Simon Hormane5054642015-05-25 14:28:36 +09004019 const struct rocker *rocker = rocker_port->rocker;
Jiri Pirko4b8ac962014-11-28 14:34:26 +01004020 struct pci_dev *pdev = rocker->pdev;
Simon Hormane5054642015-05-25 14:28:36 +09004021 const struct rocker_tlv *attrs[ROCKER_TLV_TX_MAX + 1];
Jiri Pirko4b8ac962014-11-28 14:34:26 +01004022 struct rocker_tlv *attr;
4023 int rem;
4024
4025 rocker_tlv_parse_desc(attrs, ROCKER_TLV_TX_MAX, desc_info);
4026 if (!attrs[ROCKER_TLV_TX_FRAGS])
4027 return;
4028 rocker_tlv_for_each_nested(attr, attrs[ROCKER_TLV_TX_FRAGS], rem) {
Simon Hormane5054642015-05-25 14:28:36 +09004029 const struct rocker_tlv *frag_attrs[ROCKER_TLV_TX_FRAG_ATTR_MAX + 1];
Jiri Pirko4b8ac962014-11-28 14:34:26 +01004030 dma_addr_t dma_handle;
4031 size_t len;
4032
4033 if (rocker_tlv_type(attr) != ROCKER_TLV_TX_FRAG)
4034 continue;
4035 rocker_tlv_parse_nested(frag_attrs, ROCKER_TLV_TX_FRAG_ATTR_MAX,
4036 attr);
4037 if (!frag_attrs[ROCKER_TLV_TX_FRAG_ATTR_ADDR] ||
4038 !frag_attrs[ROCKER_TLV_TX_FRAG_ATTR_LEN])
4039 continue;
4040 dma_handle = rocker_tlv_get_u64(frag_attrs[ROCKER_TLV_TX_FRAG_ATTR_ADDR]);
4041 len = rocker_tlv_get_u16(frag_attrs[ROCKER_TLV_TX_FRAG_ATTR_LEN]);
4042 pci_unmap_single(pdev, dma_handle, len, DMA_TO_DEVICE);
4043 }
4044}
4045
Simon Hormane5054642015-05-25 14:28:36 +09004046static int rocker_tx_desc_frag_map_put(const struct rocker_port *rocker_port,
Jiri Pirko4b8ac962014-11-28 14:34:26 +01004047 struct rocker_desc_info *desc_info,
4048 char *buf, size_t buf_len)
4049{
Simon Hormane5054642015-05-25 14:28:36 +09004050 const struct rocker *rocker = rocker_port->rocker;
Jiri Pirko4b8ac962014-11-28 14:34:26 +01004051 struct pci_dev *pdev = rocker->pdev;
4052 dma_addr_t dma_handle;
4053 struct rocker_tlv *frag;
4054
4055 dma_handle = pci_map_single(pdev, buf, buf_len, DMA_TO_DEVICE);
4056 if (unlikely(pci_dma_mapping_error(pdev, dma_handle))) {
4057 if (net_ratelimit())
4058 netdev_err(rocker_port->dev, "failed to dma map tx frag\n");
4059 return -EIO;
4060 }
4061 frag = rocker_tlv_nest_start(desc_info, ROCKER_TLV_TX_FRAG);
4062 if (!frag)
4063 goto unmap_frag;
4064 if (rocker_tlv_put_u64(desc_info, ROCKER_TLV_TX_FRAG_ATTR_ADDR,
4065 dma_handle))
4066 goto nest_cancel;
4067 if (rocker_tlv_put_u16(desc_info, ROCKER_TLV_TX_FRAG_ATTR_LEN,
4068 buf_len))
4069 goto nest_cancel;
4070 rocker_tlv_nest_end(desc_info, frag);
4071 return 0;
4072
4073nest_cancel:
4074 rocker_tlv_nest_cancel(desc_info, frag);
4075unmap_frag:
4076 pci_unmap_single(pdev, dma_handle, buf_len, DMA_TO_DEVICE);
4077 return -EMSGSIZE;
4078}
4079
4080static netdev_tx_t rocker_port_xmit(struct sk_buff *skb, struct net_device *dev)
4081{
4082 struct rocker_port *rocker_port = netdev_priv(dev);
4083 struct rocker *rocker = rocker_port->rocker;
4084 struct rocker_desc_info *desc_info;
4085 struct rocker_tlv *frags;
4086 int i;
4087 int err;
4088
4089 desc_info = rocker_desc_head_get(&rocker_port->tx_ring);
4090 if (unlikely(!desc_info)) {
4091 if (net_ratelimit())
4092 netdev_err(dev, "tx ring full when queue awake\n");
4093 return NETDEV_TX_BUSY;
4094 }
4095
4096 rocker_desc_cookie_ptr_set(desc_info, skb);
4097
4098 frags = rocker_tlv_nest_start(desc_info, ROCKER_TLV_TX_FRAGS);
4099 if (!frags)
4100 goto out;
4101 err = rocker_tx_desc_frag_map_put(rocker_port, desc_info,
4102 skb->data, skb_headlen(skb));
4103 if (err)
4104 goto nest_cancel;
4105 if (skb_shinfo(skb)->nr_frags > ROCKER_TX_FRAGS_MAX)
4106 goto nest_cancel;
4107
4108 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
4109 const skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
4110
4111 err = rocker_tx_desc_frag_map_put(rocker_port, desc_info,
4112 skb_frag_address(frag),
4113 skb_frag_size(frag));
4114 if (err)
4115 goto unmap_frags;
4116 }
4117 rocker_tlv_nest_end(desc_info, frags);
4118
4119 rocker_desc_gen_clear(desc_info);
4120 rocker_desc_head_set(rocker, &rocker_port->tx_ring, desc_info);
4121
4122 desc_info = rocker_desc_head_get(&rocker_port->tx_ring);
4123 if (!desc_info)
4124 netif_stop_queue(dev);
4125
4126 return NETDEV_TX_OK;
4127
4128unmap_frags:
4129 rocker_tx_desc_frags_unmap(rocker_port, desc_info);
4130nest_cancel:
4131 rocker_tlv_nest_cancel(desc_info, frags);
4132out:
4133 dev_kfree_skb(skb);
David Ahernf2bbca52015-01-16 14:22:29 -07004134 dev->stats.tx_dropped++;
4135
Jiri Pirko4b8ac962014-11-28 14:34:26 +01004136 return NETDEV_TX_OK;
4137}
4138
4139static int rocker_port_set_mac_address(struct net_device *dev, void *p)
4140{
4141 struct sockaddr *addr = p;
4142 struct rocker_port *rocker_port = netdev_priv(dev);
4143 int err;
4144
4145 if (!is_valid_ether_addr(addr->sa_data))
4146 return -EADDRNOTAVAIL;
4147
4148 err = rocker_cmd_set_port_settings_macaddr(rocker_port, addr->sa_data);
4149 if (err)
4150 return err;
4151 memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
4152 return 0;
4153}
4154
David Aherndb191702015-03-17 20:23:16 -06004155static int rocker_port_get_phys_port_name(struct net_device *dev,
4156 char *buf, size_t len)
4157{
4158 struct rocker_port *rocker_port = netdev_priv(dev);
4159 struct port_name name = { .buf = buf, .len = len };
4160 int err;
4161
Scott Feldman179f9a22015-06-12 21:35:46 -07004162 err = rocker_cmd_exec(rocker_port, SWITCHDEV_TRANS_NONE, 0,
David Aherndb191702015-03-17 20:23:16 -06004163 rocker_cmd_get_port_settings_prep, NULL,
4164 rocker_cmd_get_port_settings_phys_name_proc,
Scott Feldmanc4f20322015-05-10 09:47:50 -07004165 &name);
David Aherndb191702015-03-17 20:23:16 -06004166
4167 return err ? -EOPNOTSUPP : 0;
4168}
4169
Jiri Pirko4b8ac962014-11-28 14:34:26 +01004170static const struct net_device_ops rocker_port_netdev_ops = {
4171 .ndo_open = rocker_port_open,
4172 .ndo_stop = rocker_port_stop,
4173 .ndo_start_xmit = rocker_port_xmit,
4174 .ndo_set_mac_address = rocker_port_set_mac_address,
Scott Feldman85fdb952015-05-10 09:48:05 -07004175 .ndo_bridge_getlink = switchdev_port_bridge_getlink,
Scott Feldmanfc8f40d2015-05-10 09:47:57 -07004176 .ndo_bridge_setlink = switchdev_port_bridge_setlink,
Scott Feldman54ba5a02015-05-10 09:48:01 -07004177 .ndo_bridge_dellink = switchdev_port_bridge_dellink,
Samudrala, Sridhar45d41222015-05-13 21:55:43 -07004178 .ndo_fdb_add = switchdev_port_fdb_add,
4179 .ndo_fdb_del = switchdev_port_fdb_del,
4180 .ndo_fdb_dump = switchdev_port_fdb_dump,
David Aherndb191702015-03-17 20:23:16 -06004181 .ndo_get_phys_port_name = rocker_port_get_phys_port_name,
Scott Feldman98237d42015-03-15 21:07:15 -07004182};
4183
4184/********************
4185 * swdev interface
4186 ********************/
4187
Scott Feldmanf8e20a92015-05-10 09:47:49 -07004188static int rocker_port_attr_get(struct net_device *dev,
4189 struct switchdev_attr *attr)
Scott Feldman98237d42015-03-15 21:07:15 -07004190{
Simon Hormane5054642015-05-25 14:28:36 +09004191 const struct rocker_port *rocker_port = netdev_priv(dev);
4192 const struct rocker *rocker = rocker_port->rocker;
Scott Feldman98237d42015-03-15 21:07:15 -07004193
Scott Feldmanf8e20a92015-05-10 09:47:49 -07004194 switch (attr->id) {
4195 case SWITCHDEV_ATTR_PORT_PARENT_ID:
Scott Feldman42275bd2015-05-13 11:16:50 -07004196 attr->u.ppid.id_len = sizeof(rocker->hw.id);
4197 memcpy(&attr->u.ppid.id, &rocker->hw.id, attr->u.ppid.id_len);
Scott Feldmanf8e20a92015-05-10 09:47:49 -07004198 break;
Scott Feldman6004c862015-05-10 09:47:55 -07004199 case SWITCHDEV_ATTR_PORT_BRIDGE_FLAGS:
Scott Feldman42275bd2015-05-13 11:16:50 -07004200 attr->u.brport_flags = rocker_port->brport_flags;
Scott Feldman6004c862015-05-10 09:47:55 -07004201 break;
Scott Feldmanf8e20a92015-05-10 09:47:49 -07004202 default:
4203 return -EOPNOTSUPP;
4204 }
4205
Scott Feldman98237d42015-03-15 21:07:15 -07004206 return 0;
4207}
4208
Simon Hormane5054642015-05-25 14:28:36 +09004209static void rocker_port_trans_abort(const struct rocker_port *rocker_port)
Scott Feldmanc4f20322015-05-10 09:47:50 -07004210{
4211 struct list_head *mem, *tmp;
4212
4213 list_for_each_safe(mem, tmp, &rocker_port->trans_mem) {
4214 list_del(mem);
4215 kfree(mem);
4216 }
4217}
4218
Scott Feldman6004c862015-05-10 09:47:55 -07004219static int rocker_port_brport_flags_set(struct rocker_port *rocker_port,
4220 enum switchdev_trans trans,
4221 unsigned long brport_flags)
4222{
4223 unsigned long orig_flags;
4224 int err = 0;
4225
4226 orig_flags = rocker_port->brport_flags;
4227 rocker_port->brport_flags = brport_flags;
4228 if ((orig_flags ^ rocker_port->brport_flags) & BR_LEARNING)
4229 err = rocker_port_set_learning(rocker_port, trans);
4230
4231 if (trans == SWITCHDEV_TRANS_PREPARE)
4232 rocker_port->brport_flags = orig_flags;
4233
4234 return err;
4235}
4236
Scott Feldmanc4f20322015-05-10 09:47:50 -07004237static int rocker_port_attr_set(struct net_device *dev,
4238 struct switchdev_attr *attr)
4239{
4240 struct rocker_port *rocker_port = netdev_priv(dev);
4241 int err = 0;
4242
4243 switch (attr->trans) {
4244 case SWITCHDEV_TRANS_PREPARE:
4245 BUG_ON(!list_empty(&rocker_port->trans_mem));
4246 break;
4247 case SWITCHDEV_TRANS_ABORT:
4248 rocker_port_trans_abort(rocker_port);
4249 return 0;
4250 default:
4251 break;
4252 }
4253
4254 switch (attr->id) {
Scott Feldman35636062015-05-10 09:47:51 -07004255 case SWITCHDEV_ATTR_PORT_STP_STATE:
Scott Feldmanac283932015-06-12 21:35:48 -07004256 err = rocker_port_stp_update(rocker_port, attr->trans,
4257 ROCKER_OP_FLAG_NOWAIT,
Scott Feldman42275bd2015-05-13 11:16:50 -07004258 attr->u.stp_state);
Scott Feldman35636062015-05-10 09:47:51 -07004259 break;
Scott Feldman6004c862015-05-10 09:47:55 -07004260 case SWITCHDEV_ATTR_PORT_BRIDGE_FLAGS:
4261 err = rocker_port_brport_flags_set(rocker_port, attr->trans,
Scott Feldman42275bd2015-05-13 11:16:50 -07004262 attr->u.brport_flags);
Scott Feldman6004c862015-05-10 09:47:55 -07004263 break;
Scott Feldmanc4f20322015-05-10 09:47:50 -07004264 default:
4265 err = -EOPNOTSUPP;
4266 break;
4267 }
4268
4269 return err;
Scott Feldman98237d42015-03-15 21:07:15 -07004270}
4271
Scott Feldman9228ad22015-05-10 09:47:54 -07004272static int rocker_port_vlan_add(struct rocker_port *rocker_port,
4273 enum switchdev_trans trans, u16 vid, u16 flags)
4274{
4275 int err;
4276
4277 /* XXX deal with flags for PVID and untagged */
4278
4279 err = rocker_port_vlan(rocker_port, trans, 0, vid);
4280 if (err)
4281 return err;
4282
Scott Feldmancec04a62015-06-01 11:39:03 -07004283 err = rocker_port_router_mac(rocker_port, trans, 0, htons(vid));
4284 if (err)
4285 rocker_port_vlan(rocker_port, trans,
4286 ROCKER_OP_FLAG_REMOVE, vid);
4287
4288 return err;
Scott Feldman9228ad22015-05-10 09:47:54 -07004289}
4290
4291static int rocker_port_vlans_add(struct rocker_port *rocker_port,
4292 enum switchdev_trans trans,
Simon Hormane5054642015-05-25 14:28:36 +09004293 const struct switchdev_obj_vlan *vlan)
Scott Feldman9228ad22015-05-10 09:47:54 -07004294{
4295 u16 vid;
4296 int err;
4297
Scott Feldman3e3a78b2015-06-22 00:27:16 -07004298 for (vid = vlan->vid_begin; vid <= vlan->vid_end; vid++) {
Scott Feldman9228ad22015-05-10 09:47:54 -07004299 err = rocker_port_vlan_add(rocker_port, trans,
4300 vid, vlan->flags);
4301 if (err)
4302 return err;
4303 }
4304
4305 return 0;
4306}
4307
Samudrala, Sridhar45d41222015-05-13 21:55:43 -07004308static int rocker_port_fdb_add(struct rocker_port *rocker_port,
4309 enum switchdev_trans trans,
Simon Hormane5054642015-05-25 14:28:36 +09004310 const struct switchdev_obj_fdb *fdb)
Samudrala, Sridhar45d41222015-05-13 21:55:43 -07004311{
4312 __be16 vlan_id = rocker_port_vid_to_vlan(rocker_port, fdb->vid, NULL);
4313 int flags = 0;
4314
4315 if (!rocker_port_is_bridged(rocker_port))
4316 return -EINVAL;
4317
4318 return rocker_port_fdb(rocker_port, trans, fdb->addr, vlan_id, flags);
4319}
4320
Scott Feldman9228ad22015-05-10 09:47:54 -07004321static int rocker_port_obj_add(struct net_device *dev,
4322 struct switchdev_obj *obj)
4323{
4324 struct rocker_port *rocker_port = netdev_priv(dev);
Simon Hormane5054642015-05-25 14:28:36 +09004325 const struct switchdev_obj_ipv4_fib *fib4;
Scott Feldman9228ad22015-05-10 09:47:54 -07004326 int err = 0;
4327
4328 switch (obj->trans) {
4329 case SWITCHDEV_TRANS_PREPARE:
4330 BUG_ON(!list_empty(&rocker_port->trans_mem));
4331 break;
4332 case SWITCHDEV_TRANS_ABORT:
4333 rocker_port_trans_abort(rocker_port);
4334 return 0;
4335 default:
4336 break;
4337 }
4338
4339 switch (obj->id) {
4340 case SWITCHDEV_OBJ_PORT_VLAN:
4341 err = rocker_port_vlans_add(rocker_port, obj->trans,
Scott Feldman42275bd2015-05-13 11:16:50 -07004342 &obj->u.vlan);
Scott Feldman9228ad22015-05-10 09:47:54 -07004343 break;
Scott Feldman58c2cb12015-05-10 09:48:06 -07004344 case SWITCHDEV_OBJ_IPV4_FIB:
Scott Feldman42275bd2015-05-13 11:16:50 -07004345 fib4 = &obj->u.ipv4_fib;
Scott Feldman58c2cb12015-05-10 09:48:06 -07004346 err = rocker_port_fib_ipv4(rocker_port, obj->trans,
Scott Feldman7a7ee532015-05-12 23:03:52 -07004347 htonl(fib4->dst), fib4->dst_len,
Scott Feldman58c2cb12015-05-10 09:48:06 -07004348 fib4->fi, fib4->tb_id, 0);
4349 break;
Samudrala, Sridhar45d41222015-05-13 21:55:43 -07004350 case SWITCHDEV_OBJ_PORT_FDB:
4351 err = rocker_port_fdb_add(rocker_port, obj->trans, &obj->u.fdb);
4352 break;
Scott Feldman9228ad22015-05-10 09:47:54 -07004353 default:
4354 err = -EOPNOTSUPP;
4355 break;
4356 }
4357
4358 return err;
4359}
4360
4361static int rocker_port_vlan_del(struct rocker_port *rocker_port,
4362 u16 vid, u16 flags)
4363{
4364 int err;
4365
4366 err = rocker_port_router_mac(rocker_port, SWITCHDEV_TRANS_NONE,
4367 ROCKER_OP_FLAG_REMOVE, htons(vid));
4368 if (err)
4369 return err;
4370
4371 return rocker_port_vlan(rocker_port, SWITCHDEV_TRANS_NONE,
4372 ROCKER_OP_FLAG_REMOVE, vid);
4373}
4374
4375static int rocker_port_vlans_del(struct rocker_port *rocker_port,
Simon Hormane5054642015-05-25 14:28:36 +09004376 const struct switchdev_obj_vlan *vlan)
Scott Feldman9228ad22015-05-10 09:47:54 -07004377{
4378 u16 vid;
4379 int err;
4380
Scott Feldman3e3a78b2015-06-22 00:27:16 -07004381 for (vid = vlan->vid_begin; vid <= vlan->vid_end; vid++) {
Scott Feldman9228ad22015-05-10 09:47:54 -07004382 err = rocker_port_vlan_del(rocker_port, vid, vlan->flags);
4383 if (err)
4384 return err;
4385 }
4386
4387 return 0;
4388}
4389
Samudrala, Sridhar45d41222015-05-13 21:55:43 -07004390static int rocker_port_fdb_del(struct rocker_port *rocker_port,
4391 enum switchdev_trans trans,
Simon Hormane5054642015-05-25 14:28:36 +09004392 const struct switchdev_obj_fdb *fdb)
Samudrala, Sridhar45d41222015-05-13 21:55:43 -07004393{
4394 __be16 vlan_id = rocker_port_vid_to_vlan(rocker_port, fdb->vid, NULL);
Scott Feldmanb4ad7ba2015-06-14 11:33:11 -07004395 int flags = ROCKER_OP_FLAG_NOWAIT | ROCKER_OP_FLAG_REMOVE;
Samudrala, Sridhar45d41222015-05-13 21:55:43 -07004396
4397 if (!rocker_port_is_bridged(rocker_port))
4398 return -EINVAL;
4399
4400 return rocker_port_fdb(rocker_port, trans, fdb->addr, vlan_id, flags);
4401}
4402
Scott Feldman9228ad22015-05-10 09:47:54 -07004403static int rocker_port_obj_del(struct net_device *dev,
4404 struct switchdev_obj *obj)
4405{
4406 struct rocker_port *rocker_port = netdev_priv(dev);
Simon Hormane5054642015-05-25 14:28:36 +09004407 const struct switchdev_obj_ipv4_fib *fib4;
Scott Feldman9228ad22015-05-10 09:47:54 -07004408 int err = 0;
4409
4410 switch (obj->id) {
4411 case SWITCHDEV_OBJ_PORT_VLAN:
Scott Feldman42275bd2015-05-13 11:16:50 -07004412 err = rocker_port_vlans_del(rocker_port, &obj->u.vlan);
Scott Feldman9228ad22015-05-10 09:47:54 -07004413 break;
Scott Feldman58c2cb12015-05-10 09:48:06 -07004414 case SWITCHDEV_OBJ_IPV4_FIB:
Scott Feldman42275bd2015-05-13 11:16:50 -07004415 fib4 = &obj->u.ipv4_fib;
Scott Feldman58c2cb12015-05-10 09:48:06 -07004416 err = rocker_port_fib_ipv4(rocker_port, SWITCHDEV_TRANS_NONE,
Scott Feldman7a7ee532015-05-12 23:03:52 -07004417 htonl(fib4->dst), fib4->dst_len,
4418 fib4->fi, fib4->tb_id,
4419 ROCKER_OP_FLAG_REMOVE);
Scott Feldman58c2cb12015-05-10 09:48:06 -07004420 break;
Samudrala, Sridhar45d41222015-05-13 21:55:43 -07004421 case SWITCHDEV_OBJ_PORT_FDB:
4422 err = rocker_port_fdb_del(rocker_port, obj->trans, &obj->u.fdb);
4423 break;
4424 default:
4425 err = -EOPNOTSUPP;
4426 break;
4427 }
4428
4429 return err;
4430}
4431
Simon Hormane5054642015-05-25 14:28:36 +09004432static int rocker_port_fdb_dump(const struct rocker_port *rocker_port,
Samudrala, Sridhar45d41222015-05-13 21:55:43 -07004433 struct switchdev_obj *obj)
4434{
4435 struct rocker *rocker = rocker_port->rocker;
4436 struct switchdev_obj_fdb *fdb = &obj->u.fdb;
4437 struct rocker_fdb_tbl_entry *found;
4438 struct hlist_node *tmp;
4439 unsigned long lock_flags;
4440 int bkt;
4441 int err = 0;
4442
4443 spin_lock_irqsave(&rocker->fdb_tbl_lock, lock_flags);
4444 hash_for_each_safe(rocker->fdb_tbl, bkt, tmp, found, entry) {
4445 if (found->key.pport != rocker_port->pport)
4446 continue;
4447 fdb->addr = found->key.addr;
4448 fdb->vid = rocker_port_vlan_to_vid(rocker_port,
4449 found->key.vlan_id);
4450 err = obj->cb(rocker_port->dev, obj);
4451 if (err)
4452 break;
4453 }
4454 spin_unlock_irqrestore(&rocker->fdb_tbl_lock, lock_flags);
4455
4456 return err;
4457}
4458
4459static int rocker_port_obj_dump(struct net_device *dev,
4460 struct switchdev_obj *obj)
4461{
Simon Hormane5054642015-05-25 14:28:36 +09004462 const struct rocker_port *rocker_port = netdev_priv(dev);
Samudrala, Sridhar45d41222015-05-13 21:55:43 -07004463 int err = 0;
4464
4465 switch (obj->id) {
4466 case SWITCHDEV_OBJ_PORT_FDB:
4467 err = rocker_port_fdb_dump(rocker_port, obj);
4468 break;
Scott Feldman9228ad22015-05-10 09:47:54 -07004469 default:
4470 err = -EOPNOTSUPP;
4471 break;
4472 }
4473
4474 return err;
4475}
4476
Jiri Pirko9d47c0a2015-05-10 09:47:47 -07004477static const struct switchdev_ops rocker_port_switchdev_ops = {
Scott Feldmanf8e20a92015-05-10 09:47:49 -07004478 .switchdev_port_attr_get = rocker_port_attr_get,
Scott Feldman35636062015-05-10 09:47:51 -07004479 .switchdev_port_attr_set = rocker_port_attr_set,
Scott Feldman9228ad22015-05-10 09:47:54 -07004480 .switchdev_port_obj_add = rocker_port_obj_add,
4481 .switchdev_port_obj_del = rocker_port_obj_del,
Samudrala, Sridhar45d41222015-05-13 21:55:43 -07004482 .switchdev_port_obj_dump = rocker_port_obj_dump,
Jiri Pirko4b8ac962014-11-28 14:34:26 +01004483};
4484
4485/********************
4486 * ethtool interface
4487 ********************/
4488
4489static int rocker_port_get_settings(struct net_device *dev,
4490 struct ethtool_cmd *ecmd)
4491{
4492 struct rocker_port *rocker_port = netdev_priv(dev);
4493
4494 return rocker_cmd_get_port_settings_ethtool(rocker_port, ecmd);
4495}
4496
4497static int rocker_port_set_settings(struct net_device *dev,
4498 struct ethtool_cmd *ecmd)
4499{
4500 struct rocker_port *rocker_port = netdev_priv(dev);
4501
4502 return rocker_cmd_set_port_settings_ethtool(rocker_port, ecmd);
4503}
4504
4505static void rocker_port_get_drvinfo(struct net_device *dev,
4506 struct ethtool_drvinfo *drvinfo)
4507{
4508 strlcpy(drvinfo->driver, rocker_driver_name, sizeof(drvinfo->driver));
4509 strlcpy(drvinfo->version, UTS_RELEASE, sizeof(drvinfo->version));
4510}
4511
David Ahern9766e972015-01-29 20:59:33 -07004512static struct rocker_port_stats {
4513 char str[ETH_GSTRING_LEN];
4514 int type;
4515} rocker_port_stats[] = {
4516 { "rx_packets", ROCKER_TLV_CMD_PORT_STATS_RX_PKTS, },
4517 { "rx_bytes", ROCKER_TLV_CMD_PORT_STATS_RX_BYTES, },
4518 { "rx_dropped", ROCKER_TLV_CMD_PORT_STATS_RX_DROPPED, },
4519 { "rx_errors", ROCKER_TLV_CMD_PORT_STATS_RX_ERRORS, },
4520
4521 { "tx_packets", ROCKER_TLV_CMD_PORT_STATS_TX_PKTS, },
4522 { "tx_bytes", ROCKER_TLV_CMD_PORT_STATS_TX_BYTES, },
4523 { "tx_dropped", ROCKER_TLV_CMD_PORT_STATS_TX_DROPPED, },
4524 { "tx_errors", ROCKER_TLV_CMD_PORT_STATS_TX_ERRORS, },
4525};
4526
4527#define ROCKER_PORT_STATS_LEN ARRAY_SIZE(rocker_port_stats)
4528
4529static void rocker_port_get_strings(struct net_device *netdev, u32 stringset,
4530 u8 *data)
4531{
4532 u8 *p = data;
4533 int i;
4534
4535 switch (stringset) {
4536 case ETH_SS_STATS:
4537 for (i = 0; i < ARRAY_SIZE(rocker_port_stats); i++) {
4538 memcpy(p, rocker_port_stats[i].str, ETH_GSTRING_LEN);
4539 p += ETH_GSTRING_LEN;
4540 }
4541 break;
4542 }
4543}
4544
4545static int
Simon Horman534ba6a2015-06-01 13:25:04 +09004546rocker_cmd_get_port_stats_prep(const struct rocker_port *rocker_port,
David Ahern9766e972015-01-29 20:59:33 -07004547 struct rocker_desc_info *desc_info,
4548 void *priv)
4549{
4550 struct rocker_tlv *cmd_stats;
4551
4552 if (rocker_tlv_put_u16(desc_info, ROCKER_TLV_CMD_TYPE,
4553 ROCKER_TLV_CMD_TYPE_GET_PORT_STATS))
4554 return -EMSGSIZE;
4555
4556 cmd_stats = rocker_tlv_nest_start(desc_info, ROCKER_TLV_CMD_INFO);
4557 if (!cmd_stats)
4558 return -EMSGSIZE;
4559
Scott Feldman4a6bb6d2015-02-25 20:15:37 -08004560 if (rocker_tlv_put_u32(desc_info, ROCKER_TLV_CMD_PORT_STATS_PPORT,
4561 rocker_port->pport))
David Ahern9766e972015-01-29 20:59:33 -07004562 return -EMSGSIZE;
4563
4564 rocker_tlv_nest_end(desc_info, cmd_stats);
4565
4566 return 0;
4567}
4568
4569static int
Simon Horman534ba6a2015-06-01 13:25:04 +09004570rocker_cmd_get_port_stats_ethtool_proc(const struct rocker_port *rocker_port,
Simon Hormane5054642015-05-25 14:28:36 +09004571 const struct rocker_desc_info *desc_info,
David Ahern9766e972015-01-29 20:59:33 -07004572 void *priv)
4573{
Simon Hormane5054642015-05-25 14:28:36 +09004574 const struct rocker_tlv *attrs[ROCKER_TLV_CMD_MAX + 1];
4575 const struct rocker_tlv *stats_attrs[ROCKER_TLV_CMD_PORT_STATS_MAX + 1];
4576 const struct rocker_tlv *pattr;
Scott Feldman4a6bb6d2015-02-25 20:15:37 -08004577 u32 pport;
David Ahern9766e972015-01-29 20:59:33 -07004578 u64 *data = priv;
4579 int i;
4580
4581 rocker_tlv_parse_desc(attrs, ROCKER_TLV_CMD_MAX, desc_info);
4582
4583 if (!attrs[ROCKER_TLV_CMD_INFO])
4584 return -EIO;
4585
4586 rocker_tlv_parse_nested(stats_attrs, ROCKER_TLV_CMD_PORT_STATS_MAX,
4587 attrs[ROCKER_TLV_CMD_INFO]);
4588
Scott Feldman4a6bb6d2015-02-25 20:15:37 -08004589 if (!stats_attrs[ROCKER_TLV_CMD_PORT_STATS_PPORT])
David Ahern9766e972015-01-29 20:59:33 -07004590 return -EIO;
4591
Scott Feldman4a6bb6d2015-02-25 20:15:37 -08004592 pport = rocker_tlv_get_u32(stats_attrs[ROCKER_TLV_CMD_PORT_STATS_PPORT]);
4593 if (pport != rocker_port->pport)
David Ahern9766e972015-01-29 20:59:33 -07004594 return -EIO;
4595
4596 for (i = 0; i < ARRAY_SIZE(rocker_port_stats); i++) {
4597 pattr = stats_attrs[rocker_port_stats[i].type];
4598 if (!pattr)
4599 continue;
4600
4601 data[i] = rocker_tlv_get_u64(pattr);
4602 }
4603
4604 return 0;
4605}
4606
4607static int rocker_cmd_get_port_stats_ethtool(struct rocker_port *rocker_port,
4608 void *priv)
4609{
Scott Feldman179f9a22015-06-12 21:35:46 -07004610 return rocker_cmd_exec(rocker_port, SWITCHDEV_TRANS_NONE, 0,
David Ahern9766e972015-01-29 20:59:33 -07004611 rocker_cmd_get_port_stats_prep, NULL,
4612 rocker_cmd_get_port_stats_ethtool_proc,
Scott Feldmanc4f20322015-05-10 09:47:50 -07004613 priv);
David Ahern9766e972015-01-29 20:59:33 -07004614}
4615
4616static void rocker_port_get_stats(struct net_device *dev,
4617 struct ethtool_stats *stats, u64 *data)
4618{
4619 struct rocker_port *rocker_port = netdev_priv(dev);
4620
4621 if (rocker_cmd_get_port_stats_ethtool(rocker_port, data) != 0) {
4622 int i;
4623
4624 for (i = 0; i < ARRAY_SIZE(rocker_port_stats); ++i)
4625 data[i] = 0;
4626 }
David Ahern9766e972015-01-29 20:59:33 -07004627}
4628
4629static int rocker_port_get_sset_count(struct net_device *netdev, int sset)
4630{
4631 switch (sset) {
4632 case ETH_SS_STATS:
4633 return ROCKER_PORT_STATS_LEN;
4634 default:
4635 return -EOPNOTSUPP;
4636 }
4637}
4638
Jiri Pirko4b8ac962014-11-28 14:34:26 +01004639static const struct ethtool_ops rocker_port_ethtool_ops = {
4640 .get_settings = rocker_port_get_settings,
4641 .set_settings = rocker_port_set_settings,
4642 .get_drvinfo = rocker_port_get_drvinfo,
4643 .get_link = ethtool_op_get_link,
David Ahern9766e972015-01-29 20:59:33 -07004644 .get_strings = rocker_port_get_strings,
4645 .get_ethtool_stats = rocker_port_get_stats,
4646 .get_sset_count = rocker_port_get_sset_count,
Jiri Pirko4b8ac962014-11-28 14:34:26 +01004647};
4648
4649/*****************
4650 * NAPI interface
4651 *****************/
4652
4653static struct rocker_port *rocker_port_napi_tx_get(struct napi_struct *napi)
4654{
4655 return container_of(napi, struct rocker_port, napi_tx);
4656}
4657
4658static int rocker_port_poll_tx(struct napi_struct *napi, int budget)
4659{
4660 struct rocker_port *rocker_port = rocker_port_napi_tx_get(napi);
Simon Hormane5054642015-05-25 14:28:36 +09004661 const struct rocker *rocker = rocker_port->rocker;
4662 const struct rocker_desc_info *desc_info;
Jiri Pirko4b8ac962014-11-28 14:34:26 +01004663 u32 credits = 0;
4664 int err;
4665
4666 /* Cleanup tx descriptors */
4667 while ((desc_info = rocker_desc_tail_get(&rocker_port->tx_ring))) {
David Ahernf2bbca52015-01-16 14:22:29 -07004668 struct sk_buff *skb;
4669
Jiri Pirko4b8ac962014-11-28 14:34:26 +01004670 err = rocker_desc_err(desc_info);
4671 if (err && net_ratelimit())
4672 netdev_err(rocker_port->dev, "tx desc received with err %d\n",
4673 err);
4674 rocker_tx_desc_frags_unmap(rocker_port, desc_info);
David Ahernf2bbca52015-01-16 14:22:29 -07004675
4676 skb = rocker_desc_cookie_ptr_get(desc_info);
4677 if (err == 0) {
4678 rocker_port->dev->stats.tx_packets++;
4679 rocker_port->dev->stats.tx_bytes += skb->len;
Scott Feldman4725ceb2015-05-10 09:48:08 -07004680 } else {
David Ahernf2bbca52015-01-16 14:22:29 -07004681 rocker_port->dev->stats.tx_errors++;
Scott Feldman4725ceb2015-05-10 09:48:08 -07004682 }
David Ahernf2bbca52015-01-16 14:22:29 -07004683
4684 dev_kfree_skb_any(skb);
Jiri Pirko4b8ac962014-11-28 14:34:26 +01004685 credits++;
4686 }
4687
4688 if (credits && netif_queue_stopped(rocker_port->dev))
4689 netif_wake_queue(rocker_port->dev);
4690
4691 napi_complete(napi);
4692 rocker_dma_ring_credits_set(rocker, &rocker_port->tx_ring, credits);
4693
4694 return 0;
4695}
4696
Simon Hormane5054642015-05-25 14:28:36 +09004697static int rocker_port_rx_proc(const struct rocker *rocker,
4698 const struct rocker_port *rocker_port,
Jiri Pirko4b8ac962014-11-28 14:34:26 +01004699 struct rocker_desc_info *desc_info)
4700{
Simon Hormane5054642015-05-25 14:28:36 +09004701 const struct rocker_tlv *attrs[ROCKER_TLV_RX_MAX + 1];
Jiri Pirko4b8ac962014-11-28 14:34:26 +01004702 struct sk_buff *skb = rocker_desc_cookie_ptr_get(desc_info);
4703 size_t rx_len;
4704
4705 if (!skb)
4706 return -ENOENT;
4707
4708 rocker_tlv_parse_desc(attrs, ROCKER_TLV_RX_MAX, desc_info);
4709 if (!attrs[ROCKER_TLV_RX_FRAG_LEN])
4710 return -EINVAL;
4711
4712 rocker_dma_rx_ring_skb_unmap(rocker, attrs);
4713
4714 rx_len = rocker_tlv_get_u16(attrs[ROCKER_TLV_RX_FRAG_LEN]);
4715 skb_put(skb, rx_len);
4716 skb->protocol = eth_type_trans(skb, rocker_port->dev);
David Ahernf2bbca52015-01-16 14:22:29 -07004717
4718 rocker_port->dev->stats.rx_packets++;
4719 rocker_port->dev->stats.rx_bytes += skb->len;
4720
Jiri Pirko4b8ac962014-11-28 14:34:26 +01004721 netif_receive_skb(skb);
4722
Simon Horman534ba6a2015-06-01 13:25:04 +09004723 return rocker_dma_rx_ring_skb_alloc(rocker_port, desc_info);
Jiri Pirko4b8ac962014-11-28 14:34:26 +01004724}
4725
4726static struct rocker_port *rocker_port_napi_rx_get(struct napi_struct *napi)
4727{
4728 return container_of(napi, struct rocker_port, napi_rx);
4729}
4730
4731static int rocker_port_poll_rx(struct napi_struct *napi, int budget)
4732{
4733 struct rocker_port *rocker_port = rocker_port_napi_rx_get(napi);
Simon Hormane5054642015-05-25 14:28:36 +09004734 const struct rocker *rocker = rocker_port->rocker;
Jiri Pirko4b8ac962014-11-28 14:34:26 +01004735 struct rocker_desc_info *desc_info;
4736 u32 credits = 0;
4737 int err;
4738
4739 /* Process rx descriptors */
4740 while (credits < budget &&
4741 (desc_info = rocker_desc_tail_get(&rocker_port->rx_ring))) {
4742 err = rocker_desc_err(desc_info);
4743 if (err) {
4744 if (net_ratelimit())
4745 netdev_err(rocker_port->dev, "rx desc received with err %d\n",
4746 err);
4747 } else {
4748 err = rocker_port_rx_proc(rocker, rocker_port,
4749 desc_info);
4750 if (err && net_ratelimit())
4751 netdev_err(rocker_port->dev, "rx processing failed with err %d\n",
4752 err);
4753 }
David Ahernf2bbca52015-01-16 14:22:29 -07004754 if (err)
4755 rocker_port->dev->stats.rx_errors++;
4756
Jiri Pirko4b8ac962014-11-28 14:34:26 +01004757 rocker_desc_gen_clear(desc_info);
4758 rocker_desc_head_set(rocker, &rocker_port->rx_ring, desc_info);
4759 credits++;
4760 }
4761
4762 if (credits < budget)
4763 napi_complete(napi);
4764
4765 rocker_dma_ring_credits_set(rocker, &rocker_port->rx_ring, credits);
4766
4767 return credits;
4768}
4769
4770/*****************
4771 * PCI driver ops
4772 *****************/
4773
Simon Hormane5054642015-05-25 14:28:36 +09004774static void rocker_carrier_init(const struct rocker_port *rocker_port)
Jiri Pirko4b8ac962014-11-28 14:34:26 +01004775{
Simon Hormane5054642015-05-25 14:28:36 +09004776 const struct rocker *rocker = rocker_port->rocker;
Jiri Pirko4b8ac962014-11-28 14:34:26 +01004777 u64 link_status = rocker_read64(rocker, PORT_PHYS_LINK_STATUS);
4778 bool link_up;
4779
Scott Feldman4a6bb6d2015-02-25 20:15:37 -08004780 link_up = link_status & (1 << rocker_port->pport);
Jiri Pirko4b8ac962014-11-28 14:34:26 +01004781 if (link_up)
4782 netif_carrier_on(rocker_port->dev);
4783 else
4784 netif_carrier_off(rocker_port->dev);
4785}
4786
Simon Hormane5054642015-05-25 14:28:36 +09004787static void rocker_remove_ports(const struct rocker *rocker)
Jiri Pirko4b8ac962014-11-28 14:34:26 +01004788{
Scott Feldman9f6bbf72014-11-28 14:34:27 +01004789 struct rocker_port *rocker_port;
Jiri Pirko4b8ac962014-11-28 14:34:26 +01004790 int i;
4791
Scott Feldman9f6bbf72014-11-28 14:34:27 +01004792 for (i = 0; i < rocker->port_count; i++) {
4793 rocker_port = rocker->ports[i];
Scott Feldmana0720312015-06-12 21:09:44 -07004794 if (!rocker_port)
4795 continue;
Scott Feldmanc4f20322015-05-10 09:47:50 -07004796 rocker_port_ig_tbl(rocker_port, SWITCHDEV_TRANS_NONE,
4797 ROCKER_OP_FLAG_REMOVE);
Scott Feldman9f6bbf72014-11-28 14:34:27 +01004798 unregister_netdev(rocker_port->dev);
4799 }
Jiri Pirko4b8ac962014-11-28 14:34:26 +01004800 kfree(rocker->ports);
4801}
4802
Simon Horman534ba6a2015-06-01 13:25:04 +09004803static void rocker_port_dev_addr_init(struct rocker_port *rocker_port)
Jiri Pirko4b8ac962014-11-28 14:34:26 +01004804{
Simon Horman534ba6a2015-06-01 13:25:04 +09004805 const struct rocker *rocker = rocker_port->rocker;
Simon Hormane5054642015-05-25 14:28:36 +09004806 const struct pci_dev *pdev = rocker->pdev;
Jiri Pirko4b8ac962014-11-28 14:34:26 +01004807 int err;
4808
4809 err = rocker_cmd_get_port_settings_macaddr(rocker_port,
4810 rocker_port->dev->dev_addr);
4811 if (err) {
4812 dev_warn(&pdev->dev, "failed to get mac address, using random\n");
4813 eth_hw_addr_random(rocker_port->dev);
4814 }
4815}
4816
4817static int rocker_probe_port(struct rocker *rocker, unsigned int port_number)
4818{
Simon Hormane5054642015-05-25 14:28:36 +09004819 const struct pci_dev *pdev = rocker->pdev;
Jiri Pirko4b8ac962014-11-28 14:34:26 +01004820 struct rocker_port *rocker_port;
4821 struct net_device *dev;
Scott Feldmanbcfd7802015-06-01 11:39:04 -07004822 u16 untagged_vid = 0;
Jiri Pirko4b8ac962014-11-28 14:34:26 +01004823 int err;
4824
4825 dev = alloc_etherdev(sizeof(struct rocker_port));
4826 if (!dev)
4827 return -ENOMEM;
4828 rocker_port = netdev_priv(dev);
4829 rocker_port->dev = dev;
4830 rocker_port->rocker = rocker;
4831 rocker_port->port_number = port_number;
Scott Feldman4a6bb6d2015-02-25 20:15:37 -08004832 rocker_port->pport = port_number + 1;
Scott Feldman5111f802014-11-28 14:34:30 +01004833 rocker_port->brport_flags = BR_LEARNING | BR_LEARNING_SYNC;
Scott Feldmanc4f20322015-05-10 09:47:50 -07004834 INIT_LIST_HEAD(&rocker_port->trans_mem);
Jiri Pirko4b8ac962014-11-28 14:34:26 +01004835
Simon Horman534ba6a2015-06-01 13:25:04 +09004836 rocker_port_dev_addr_init(rocker_port);
Jiri Pirko4b8ac962014-11-28 14:34:26 +01004837 dev->netdev_ops = &rocker_port_netdev_ops;
4838 dev->ethtool_ops = &rocker_port_ethtool_ops;
Jiri Pirko9d47c0a2015-05-10 09:47:47 -07004839 dev->switchdev_ops = &rocker_port_switchdev_ops;
Jiri Pirko4b8ac962014-11-28 14:34:26 +01004840 netif_napi_add(dev, &rocker_port->napi_tx, rocker_port_poll_tx,
4841 NAPI_POLL_WEIGHT);
4842 netif_napi_add(dev, &rocker_port->napi_rx, rocker_port_poll_rx,
4843 NAPI_POLL_WEIGHT);
4844 rocker_carrier_init(rocker_port);
4845
Scott Feldman2aa2ed02015-06-01 11:39:06 -07004846 dev->features |= NETIF_F_NETNS_LOCAL;
Jiri Pirko4b8ac962014-11-28 14:34:26 +01004847
4848 err = register_netdev(dev);
4849 if (err) {
4850 dev_err(&pdev->dev, "register_netdev failed\n");
4851 goto err_register_netdev;
4852 }
4853 rocker->ports[port_number] = rocker_port;
4854
Scott Feldmanc4f20322015-05-10 09:47:50 -07004855 rocker_port_set_learning(rocker_port, SWITCHDEV_TRANS_NONE);
Scott Feldman5111f802014-11-28 14:34:30 +01004856
Scott Feldmanc4f20322015-05-10 09:47:50 -07004857 err = rocker_port_ig_tbl(rocker_port, SWITCHDEV_TRANS_NONE, 0);
Scott Feldman9f6bbf72014-11-28 14:34:27 +01004858 if (err) {
4859 dev_err(&pdev->dev, "install ig port table failed\n");
4860 goto err_port_ig_tbl;
4861 }
4862
Scott Feldmanbcfd7802015-06-01 11:39:04 -07004863 rocker_port->internal_vlan_id =
4864 rocker_port_internal_vlan_id_get(rocker_port, dev->ifindex);
4865
4866 err = rocker_port_vlan_add(rocker_port, SWITCHDEV_TRANS_NONE,
4867 untagged_vid, 0);
4868 if (err) {
4869 netdev_err(rocker_port->dev, "install untagged VLAN failed\n");
4870 goto err_untagged_vlan;
4871 }
4872
Jiri Pirko4b8ac962014-11-28 14:34:26 +01004873 return 0;
4874
Scott Feldmanbcfd7802015-06-01 11:39:04 -07004875err_untagged_vlan:
4876 rocker_port_ig_tbl(rocker_port, SWITCHDEV_TRANS_NONE,
4877 ROCKER_OP_FLAG_REMOVE);
Scott Feldman9f6bbf72014-11-28 14:34:27 +01004878err_port_ig_tbl:
4879 unregister_netdev(dev);
Jiri Pirko4b8ac962014-11-28 14:34:26 +01004880err_register_netdev:
4881 free_netdev(dev);
4882 return err;
4883}
4884
4885static int rocker_probe_ports(struct rocker *rocker)
4886{
4887 int i;
4888 size_t alloc_size;
4889 int err;
4890
4891 alloc_size = sizeof(struct rocker_port *) * rocker->port_count;
Scott Feldman27b808c2015-06-01 11:39:02 -07004892 rocker->ports = kzalloc(alloc_size, GFP_KERNEL);
Dan Carpentere65ad3b2015-02-25 16:35:32 +03004893 if (!rocker->ports)
4894 return -ENOMEM;
Jiri Pirko4b8ac962014-11-28 14:34:26 +01004895 for (i = 0; i < rocker->port_count; i++) {
4896 err = rocker_probe_port(rocker, i);
4897 if (err)
4898 goto remove_ports;
4899 }
4900 return 0;
4901
4902remove_ports:
4903 rocker_remove_ports(rocker);
4904 return err;
4905}
4906
4907static int rocker_msix_init(struct rocker *rocker)
4908{
4909 struct pci_dev *pdev = rocker->pdev;
4910 int msix_entries;
4911 int i;
4912 int err;
4913
4914 msix_entries = pci_msix_vec_count(pdev);
4915 if (msix_entries < 0)
4916 return msix_entries;
4917
4918 if (msix_entries != ROCKER_MSIX_VEC_COUNT(rocker->port_count))
4919 return -EINVAL;
4920
4921 rocker->msix_entries = kmalloc_array(msix_entries,
4922 sizeof(struct msix_entry),
4923 GFP_KERNEL);
4924 if (!rocker->msix_entries)
4925 return -ENOMEM;
4926
4927 for (i = 0; i < msix_entries; i++)
4928 rocker->msix_entries[i].entry = i;
4929
4930 err = pci_enable_msix_exact(pdev, rocker->msix_entries, msix_entries);
4931 if (err < 0)
4932 goto err_enable_msix;
4933
4934 return 0;
4935
4936err_enable_msix:
4937 kfree(rocker->msix_entries);
4938 return err;
4939}
4940
Simon Hormane5054642015-05-25 14:28:36 +09004941static void rocker_msix_fini(const struct rocker *rocker)
Jiri Pirko4b8ac962014-11-28 14:34:26 +01004942{
4943 pci_disable_msix(rocker->pdev);
4944 kfree(rocker->msix_entries);
4945}
4946
4947static int rocker_probe(struct pci_dev *pdev, const struct pci_device_id *id)
4948{
4949 struct rocker *rocker;
4950 int err;
4951
4952 rocker = kzalloc(sizeof(*rocker), GFP_KERNEL);
4953 if (!rocker)
4954 return -ENOMEM;
4955
4956 err = pci_enable_device(pdev);
4957 if (err) {
4958 dev_err(&pdev->dev, "pci_enable_device failed\n");
4959 goto err_pci_enable_device;
4960 }
4961
4962 err = pci_request_regions(pdev, rocker_driver_name);
4963 if (err) {
4964 dev_err(&pdev->dev, "pci_request_regions failed\n");
4965 goto err_pci_request_regions;
4966 }
4967
4968 err = pci_set_dma_mask(pdev, DMA_BIT_MASK(64));
4969 if (!err) {
4970 err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64));
4971 if (err) {
4972 dev_err(&pdev->dev, "pci_set_consistent_dma_mask failed\n");
4973 goto err_pci_set_dma_mask;
4974 }
4975 } else {
4976 err = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
4977 if (err) {
4978 dev_err(&pdev->dev, "pci_set_dma_mask failed\n");
4979 goto err_pci_set_dma_mask;
4980 }
4981 }
4982
4983 if (pci_resource_len(pdev, 0) < ROCKER_PCI_BAR0_SIZE) {
4984 dev_err(&pdev->dev, "invalid PCI region size\n");
Wei Yongjun3122a922015-04-16 20:21:02 +08004985 err = -EINVAL;
Jiri Pirko4b8ac962014-11-28 14:34:26 +01004986 goto err_pci_resource_len_check;
4987 }
4988
4989 rocker->hw_addr = ioremap(pci_resource_start(pdev, 0),
4990 pci_resource_len(pdev, 0));
4991 if (!rocker->hw_addr) {
4992 dev_err(&pdev->dev, "ioremap failed\n");
4993 err = -EIO;
4994 goto err_ioremap;
4995 }
4996 pci_set_master(pdev);
4997
4998 rocker->pdev = pdev;
4999 pci_set_drvdata(pdev, rocker);
5000
5001 rocker->port_count = rocker_read32(rocker, PORT_PHYS_COUNT);
5002
5003 err = rocker_msix_init(rocker);
5004 if (err) {
5005 dev_err(&pdev->dev, "MSI-X init failed\n");
5006 goto err_msix_init;
5007 }
5008
5009 err = rocker_basic_hw_test(rocker);
5010 if (err) {
5011 dev_err(&pdev->dev, "basic hw test failed\n");
5012 goto err_basic_hw_test;
5013 }
5014
5015 rocker_write32(rocker, CONTROL, ROCKER_CONTROL_RESET);
5016
5017 err = rocker_dma_rings_init(rocker);
5018 if (err)
5019 goto err_dma_rings_init;
5020
5021 err = request_irq(rocker_msix_vector(rocker, ROCKER_MSIX_VEC_CMD),
5022 rocker_cmd_irq_handler, 0,
5023 rocker_driver_name, rocker);
5024 if (err) {
5025 dev_err(&pdev->dev, "cannot assign cmd irq\n");
5026 goto err_request_cmd_irq;
5027 }
5028
5029 err = request_irq(rocker_msix_vector(rocker, ROCKER_MSIX_VEC_EVENT),
5030 rocker_event_irq_handler, 0,
5031 rocker_driver_name, rocker);
5032 if (err) {
5033 dev_err(&pdev->dev, "cannot assign event irq\n");
5034 goto err_request_event_irq;
5035 }
5036
5037 rocker->hw.id = rocker_read64(rocker, SWITCH_ID);
5038
Scott Feldman9f6bbf72014-11-28 14:34:27 +01005039 err = rocker_init_tbls(rocker);
5040 if (err) {
5041 dev_err(&pdev->dev, "cannot init rocker tables\n");
5042 goto err_init_tbls;
5043 }
5044
Jiri Pirko4b8ac962014-11-28 14:34:26 +01005045 err = rocker_probe_ports(rocker);
5046 if (err) {
5047 dev_err(&pdev->dev, "failed to probe ports\n");
5048 goto err_probe_ports;
5049 }
5050
5051 dev_info(&pdev->dev, "Rocker switch with id %016llx\n", rocker->hw.id);
5052
5053 return 0;
5054
5055err_probe_ports:
Scott Feldman9f6bbf72014-11-28 14:34:27 +01005056 rocker_free_tbls(rocker);
5057err_init_tbls:
Jiri Pirko4b8ac962014-11-28 14:34:26 +01005058 free_irq(rocker_msix_vector(rocker, ROCKER_MSIX_VEC_EVENT), rocker);
5059err_request_event_irq:
5060 free_irq(rocker_msix_vector(rocker, ROCKER_MSIX_VEC_CMD), rocker);
5061err_request_cmd_irq:
5062 rocker_dma_rings_fini(rocker);
5063err_dma_rings_init:
5064err_basic_hw_test:
5065 rocker_msix_fini(rocker);
5066err_msix_init:
5067 iounmap(rocker->hw_addr);
5068err_ioremap:
5069err_pci_resource_len_check:
5070err_pci_set_dma_mask:
5071 pci_release_regions(pdev);
5072err_pci_request_regions:
5073 pci_disable_device(pdev);
5074err_pci_enable_device:
5075 kfree(rocker);
5076 return err;
5077}
5078
5079static void rocker_remove(struct pci_dev *pdev)
5080{
5081 struct rocker *rocker = pci_get_drvdata(pdev);
5082
Scott Feldman9f6bbf72014-11-28 14:34:27 +01005083 rocker_free_tbls(rocker);
Jiri Pirko4b8ac962014-11-28 14:34:26 +01005084 rocker_write32(rocker, CONTROL, ROCKER_CONTROL_RESET);
5085 rocker_remove_ports(rocker);
5086 free_irq(rocker_msix_vector(rocker, ROCKER_MSIX_VEC_EVENT), rocker);
5087 free_irq(rocker_msix_vector(rocker, ROCKER_MSIX_VEC_CMD), rocker);
5088 rocker_dma_rings_fini(rocker);
5089 rocker_msix_fini(rocker);
5090 iounmap(rocker->hw_addr);
5091 pci_release_regions(rocker->pdev);
5092 pci_disable_device(rocker->pdev);
5093 kfree(rocker);
5094}
5095
5096static struct pci_driver rocker_pci_driver = {
5097 .name = rocker_driver_name,
5098 .id_table = rocker_pci_id_table,
5099 .probe = rocker_probe,
5100 .remove = rocker_remove,
5101};
5102
Scott Feldman6c707942014-11-28 14:34:28 +01005103/************************************
5104 * Net device notifier event handler
5105 ************************************/
5106
Simon Hormane5054642015-05-25 14:28:36 +09005107static bool rocker_port_dev_check(const struct net_device *dev)
Scott Feldman6c707942014-11-28 14:34:28 +01005108{
5109 return dev->netdev_ops == &rocker_port_netdev_ops;
5110}
5111
5112static int rocker_port_bridge_join(struct rocker_port *rocker_port,
5113 struct net_device *bridge)
5114{
Scott Feldman027e00d2015-06-01 11:39:05 -07005115 u16 untagged_vid = 0;
Scott Feldman6c707942014-11-28 14:34:28 +01005116 int err;
5117
Scott Feldman027e00d2015-06-01 11:39:05 -07005118 /* Port is joining bridge, so the internal VLAN for the
5119 * port is going to change to the bridge internal VLAN.
5120 * Let's remove untagged VLAN (vid=0) from port and
5121 * re-add once internal VLAN has changed.
5122 */
5123
5124 err = rocker_port_vlan_del(rocker_port, untagged_vid, 0);
5125 if (err)
5126 return err;
5127
Simon Hormandf6a2062015-05-21 12:40:17 +09005128 rocker_port_internal_vlan_id_put(rocker_port,
Scott Feldman6c707942014-11-28 14:34:28 +01005129 rocker_port->dev->ifindex);
Scott Feldman027e00d2015-06-01 11:39:05 -07005130 rocker_port->internal_vlan_id =
5131 rocker_port_internal_vlan_id_get(rocker_port, bridge->ifindex);
Scott Feldman6c707942014-11-28 14:34:28 +01005132
5133 rocker_port->bridge_dev = bridge;
5134
Scott Feldman027e00d2015-06-01 11:39:05 -07005135 return rocker_port_vlan_add(rocker_port, SWITCHDEV_TRANS_NONE,
5136 untagged_vid, 0);
Scott Feldman6c707942014-11-28 14:34:28 +01005137}
5138
5139static int rocker_port_bridge_leave(struct rocker_port *rocker_port)
5140{
Scott Feldman027e00d2015-06-01 11:39:05 -07005141 u16 untagged_vid = 0;
Scott Feldman6c707942014-11-28 14:34:28 +01005142 int err;
5143
Scott Feldman027e00d2015-06-01 11:39:05 -07005144 err = rocker_port_vlan_del(rocker_port, untagged_vid, 0);
5145 if (err)
5146 return err;
5147
Simon Hormandf6a2062015-05-21 12:40:17 +09005148 rocker_port_internal_vlan_id_put(rocker_port,
Scott Feldman6c707942014-11-28 14:34:28 +01005149 rocker_port->bridge_dev->ifindex);
Scott Feldman6c707942014-11-28 14:34:28 +01005150 rocker_port->internal_vlan_id =
5151 rocker_port_internal_vlan_id_get(rocker_port,
5152 rocker_port->dev->ifindex);
Scott Feldman027e00d2015-06-01 11:39:05 -07005153
5154 rocker_port->bridge_dev = NULL;
5155
5156 err = rocker_port_vlan_add(rocker_port, SWITCHDEV_TRANS_NONE,
5157 untagged_vid, 0);
Scott Feldmane47172a2015-02-25 20:15:38 -08005158 if (err)
5159 return err;
5160
5161 if (rocker_port->dev->flags & IFF_UP)
Scott Feldman179f9a22015-06-12 21:35:46 -07005162 err = rocker_port_fwd_enable(rocker_port,
5163 SWITCHDEV_TRANS_NONE, 0);
Scott Feldman6c707942014-11-28 14:34:28 +01005164
5165 return err;
5166}
5167
5168static int rocker_port_master_changed(struct net_device *dev)
5169{
5170 struct rocker_port *rocker_port = netdev_priv(dev);
5171 struct net_device *master = netdev_master_upper_dev_get(dev);
5172 int err = 0;
5173
Simon Hormana6e95cc2015-03-24 09:31:40 +09005174 /* There are currently three cases handled here:
5175 * 1. Joining a bridge
5176 * 2. Leaving a previously joined bridge
5177 * 3. Other, e.g. being added to or removed from a bond or openvswitch,
5178 * in which case nothing is done
5179 */
Scott Feldman6c707942014-11-28 14:34:28 +01005180 if (master && master->rtnl_link_ops &&
5181 !strcmp(master->rtnl_link_ops->kind, "bridge"))
5182 err = rocker_port_bridge_join(rocker_port, master);
Simon Hormana6e95cc2015-03-24 09:31:40 +09005183 else if (rocker_port_is_bridged(rocker_port))
Scott Feldman6c707942014-11-28 14:34:28 +01005184 err = rocker_port_bridge_leave(rocker_port);
5185
5186 return err;
5187}
5188
5189static int rocker_netdevice_event(struct notifier_block *unused,
5190 unsigned long event, void *ptr)
5191{
5192 struct net_device *dev;
5193 int err;
5194
5195 switch (event) {
5196 case NETDEV_CHANGEUPPER:
5197 dev = netdev_notifier_info_to_dev(ptr);
5198 if (!rocker_port_dev_check(dev))
5199 return NOTIFY_DONE;
5200 err = rocker_port_master_changed(dev);
5201 if (err)
5202 netdev_warn(dev,
5203 "failed to reflect master change (err %d)\n",
5204 err);
5205 break;
5206 }
5207
5208 return NOTIFY_DONE;
5209}
5210
5211static struct notifier_block rocker_netdevice_nb __read_mostly = {
5212 .notifier_call = rocker_netdevice_event,
5213};
5214
Scott Feldmanc1beeef2015-03-05 21:21:20 -08005215/************************************
5216 * Net event notifier event handler
5217 ************************************/
5218
5219static int rocker_neigh_update(struct net_device *dev, struct neighbour *n)
5220{
5221 struct rocker_port *rocker_port = netdev_priv(dev);
Scott Feldman02a9fbf2015-06-12 21:35:47 -07005222 int flags = (n->nud_state & NUD_VALID ? 0 : ROCKER_OP_FLAG_REMOVE) |
5223 ROCKER_OP_FLAG_NOWAIT;
Scott Feldmanc1beeef2015-03-05 21:21:20 -08005224 __be32 ip_addr = *(__be32 *)n->primary_key;
5225
Scott Feldmanc4f20322015-05-10 09:47:50 -07005226 return rocker_port_ipv4_neigh(rocker_port, SWITCHDEV_TRANS_NONE,
5227 flags, ip_addr, n->ha);
Scott Feldmanc1beeef2015-03-05 21:21:20 -08005228}
5229
5230static int rocker_netevent_event(struct notifier_block *unused,
5231 unsigned long event, void *ptr)
5232{
5233 struct net_device *dev;
5234 struct neighbour *n = ptr;
5235 int err;
5236
5237 switch (event) {
5238 case NETEVENT_NEIGH_UPDATE:
5239 if (n->tbl != &arp_tbl)
5240 return NOTIFY_DONE;
5241 dev = n->dev;
5242 if (!rocker_port_dev_check(dev))
5243 return NOTIFY_DONE;
5244 err = rocker_neigh_update(dev, n);
5245 if (err)
5246 netdev_warn(dev,
5247 "failed to handle neigh update (err %d)\n",
5248 err);
5249 break;
5250 }
5251
5252 return NOTIFY_DONE;
5253}
5254
5255static struct notifier_block rocker_netevent_nb __read_mostly = {
5256 .notifier_call = rocker_netevent_event,
5257};
5258
Jiri Pirko4b8ac962014-11-28 14:34:26 +01005259/***********************
5260 * Module init and exit
5261 ***********************/
5262
5263static int __init rocker_module_init(void)
5264{
Scott Feldman6c707942014-11-28 14:34:28 +01005265 int err;
5266
5267 register_netdevice_notifier(&rocker_netdevice_nb);
Scott Feldmanc1beeef2015-03-05 21:21:20 -08005268 register_netevent_notifier(&rocker_netevent_nb);
Scott Feldman6c707942014-11-28 14:34:28 +01005269 err = pci_register_driver(&rocker_pci_driver);
5270 if (err)
5271 goto err_pci_register_driver;
5272 return 0;
5273
5274err_pci_register_driver:
Scott Feldmanc1beeef2015-03-05 21:21:20 -08005275 unregister_netdevice_notifier(&rocker_netevent_nb);
Scott Feldman6c707942014-11-28 14:34:28 +01005276 unregister_netdevice_notifier(&rocker_netdevice_nb);
5277 return err;
Jiri Pirko4b8ac962014-11-28 14:34:26 +01005278}
5279
5280static void __exit rocker_module_exit(void)
5281{
Scott Feldmanc1beeef2015-03-05 21:21:20 -08005282 unregister_netevent_notifier(&rocker_netevent_nb);
Scott Feldman6c707942014-11-28 14:34:28 +01005283 unregister_netdevice_notifier(&rocker_netdevice_nb);
Jiri Pirko4b8ac962014-11-28 14:34:26 +01005284 pci_unregister_driver(&rocker_pci_driver);
5285}
5286
5287module_init(rocker_module_init);
5288module_exit(rocker_module_exit);
5289
5290MODULE_LICENSE("GPL v2");
5291MODULE_AUTHOR("Jiri Pirko <jiri@resnulli.us>");
5292MODULE_AUTHOR("Scott Feldman <sfeldma@gmail.com>");
5293MODULE_DESCRIPTION("Rocker switch device driver");
5294MODULE_DEVICE_TABLE(pci, rocker_pci_id_table);