Jiri Pirko | 4b8ac96 | 2014-11-28 14:34:26 +0100 | [diff] [blame] | 1 | /* |
| 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 Feldman | 9f6bbf7 | 2014-11-28 14:34:27 +0100 | [diff] [blame] | 19 | #include <linux/hashtable.h> |
Jiri Pirko | 4b8ac96 | 2014-11-28 14:34:26 +0100 | [diff] [blame] | 20 | #include <linux/crc32.h> |
| 21 | #include <linux/sort.h> |
| 22 | #include <linux/random.h> |
| 23 | #include <linux/netdevice.h> |
| 24 | #include <linux/inetdevice.h> |
| 25 | #include <linux/skbuff.h> |
| 26 | #include <linux/socket.h> |
| 27 | #include <linux/etherdevice.h> |
| 28 | #include <linux/ethtool.h> |
| 29 | #include <linux/if_ether.h> |
| 30 | #include <linux/if_vlan.h> |
Scott Feldman | 6c70794 | 2014-11-28 14:34:28 +0100 | [diff] [blame] | 31 | #include <linux/if_bridge.h> |
Scott Feldman | 9f6bbf7 | 2014-11-28 14:34:27 +0100 | [diff] [blame] | 32 | #include <linux/bitops.h> |
David Ahern | db19170 | 2015-03-17 20:23:16 -0600 | [diff] [blame] | 33 | #include <linux/ctype.h> |
Jiri Pirko | 4b8ac96 | 2014-11-28 14:34:26 +0100 | [diff] [blame] | 34 | #include <net/switchdev.h> |
| 35 | #include <net/rtnetlink.h> |
Scott Feldman | c1beeef | 2015-03-05 21:21:20 -0800 | [diff] [blame] | 36 | #include <net/ip_fib.h> |
| 37 | #include <net/netevent.h> |
| 38 | #include <net/arp.h> |
Jiri Pirko | 4b8ac96 | 2014-11-28 14:34:26 +0100 | [diff] [blame] | 39 | #include <asm-generic/io-64-nonatomic-lo-hi.h> |
| 40 | #include <generated/utsrelease.h> |
| 41 | |
| 42 | #include "rocker.h" |
| 43 | |
| 44 | static const char rocker_driver_name[] = "rocker"; |
| 45 | |
| 46 | static const struct pci_device_id rocker_pci_id_table[] = { |
| 47 | {PCI_VDEVICE(REDHAT, PCI_DEVICE_ID_REDHAT_ROCKER), 0}, |
| 48 | {0, } |
| 49 | }; |
| 50 | |
Scott Feldman | 9f6bbf7 | 2014-11-28 14:34:27 +0100 | [diff] [blame] | 51 | struct rocker_flow_tbl_key { |
| 52 | u32 priority; |
| 53 | enum rocker_of_dpa_table_id tbl_id; |
| 54 | union { |
| 55 | struct { |
Scott Feldman | 4a6bb6d | 2015-02-25 20:15:37 -0800 | [diff] [blame] | 56 | u32 in_pport; |
| 57 | u32 in_pport_mask; |
Scott Feldman | 9f6bbf7 | 2014-11-28 14:34:27 +0100 | [diff] [blame] | 58 | enum rocker_of_dpa_table_id goto_tbl; |
| 59 | } ig_port; |
| 60 | struct { |
Scott Feldman | 4a6bb6d | 2015-02-25 20:15:37 -0800 | [diff] [blame] | 61 | u32 in_pport; |
Scott Feldman | 9f6bbf7 | 2014-11-28 14:34:27 +0100 | [diff] [blame] | 62 | __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 Feldman | 4a6bb6d | 2015-02-25 20:15:37 -0800 | [diff] [blame] | 69 | u32 in_pport; |
| 70 | u32 in_pport_mask; |
Scott Feldman | 9f6bbf7 | 2014-11-28 14:34:27 +0100 | [diff] [blame] | 71 | __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 Feldman | 4a6bb6d | 2015-02-25 20:15:37 -0800 | [diff] [blame] | 98 | u32 in_pport; |
| 99 | u32 in_pport_mask; |
Scott Feldman | 9f6bbf7 | 2014-11-28 14:34:27 +0100 | [diff] [blame] | 100 | u8 eth_src[ETH_ALEN]; |
| 101 | u8 eth_src_mask[ETH_ALEN]; |
| 102 | u8 eth_dst[ETH_ALEN]; |
| 103 | u8 eth_dst_mask[ETH_ALEN]; |
| 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 | |
| 116 | struct rocker_flow_tbl_entry { |
| 117 | struct hlist_node entry; |
Scott Feldman | c1beeef | 2015-03-05 21:21:20 -0800 | [diff] [blame] | 118 | u32 cmd; |
Scott Feldman | 9f6bbf7 | 2014-11-28 14:34:27 +0100 | [diff] [blame] | 119 | u64 cookie; |
| 120 | struct rocker_flow_tbl_key key; |
Scott Feldman | c1beeef | 2015-03-05 21:21:20 -0800 | [diff] [blame] | 121 | size_t key_len; |
Scott Feldman | 9f6bbf7 | 2014-11-28 14:34:27 +0100 | [diff] [blame] | 122 | u32 key_crc32; /* key */ |
| 123 | }; |
| 124 | |
| 125 | struct 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 | |
| 151 | struct rocker_fdb_tbl_entry { |
| 152 | struct hlist_node entry; |
| 153 | u32 key_crc32; /* key */ |
| 154 | bool learned; |
| 155 | struct rocker_fdb_tbl_key { |
Scott Feldman | 4a6bb6d | 2015-02-25 20:15:37 -0800 | [diff] [blame] | 156 | u32 pport; |
Scott Feldman | 9f6bbf7 | 2014-11-28 14:34:27 +0100 | [diff] [blame] | 157 | u8 addr[ETH_ALEN]; |
| 158 | __be16 vlan_id; |
| 159 | } key; |
| 160 | }; |
| 161 | |
| 162 | struct 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 Feldman | c1beeef | 2015-03-05 21:21:20 -0800 | [diff] [blame] | 169 | struct 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 Pirko | 4b8ac96 | 2014-11-28 14:34:26 +0100 | [diff] [blame] | 179 | struct rocker_desc_info { |
| 180 | char *data; /* mapped */ |
| 181 | size_t data_size; |
| 182 | size_t tlv_size; |
| 183 | struct rocker_desc *desc; |
Scott Feldman | 4725ceb | 2015-05-10 09:48:08 -0700 | [diff] [blame] | 184 | dma_addr_t mapaddr; |
Jiri Pirko | 4b8ac96 | 2014-11-28 14:34:26 +0100 | [diff] [blame] | 185 | }; |
| 186 | |
| 187 | struct 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 | |
| 197 | struct rocker; |
| 198 | |
Scott Feldman | 9f6bbf7 | 2014-11-28 14:34:27 +0100 | [diff] [blame] | 199 | enum { |
| 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 Pirko | 4b8ac96 | 2014-11-28 14:34:26 +0100 | [diff] [blame] | 213 | struct rocker_port { |
| 214 | struct net_device *dev; |
Scott Feldman | 6c70794 | 2014-11-28 14:34:28 +0100 | [diff] [blame] | 215 | struct net_device *bridge_dev; |
Jiri Pirko | 4b8ac96 | 2014-11-28 14:34:26 +0100 | [diff] [blame] | 216 | struct rocker *rocker; |
| 217 | unsigned int port_number; |
Scott Feldman | 4a6bb6d | 2015-02-25 20:15:37 -0800 | [diff] [blame] | 218 | u32 pport; |
Scott Feldman | 9f6bbf7 | 2014-11-28 14:34:27 +0100 | [diff] [blame] | 219 | __be16 internal_vlan_id; |
Scott Feldman | 6c70794 | 2014-11-28 14:34:28 +0100 | [diff] [blame] | 220 | int stp_state; |
Scott Feldman | 5111f80 | 2014-11-28 14:34:30 +0100 | [diff] [blame] | 221 | u32 brport_flags; |
Scott Feldman | 9f6bbf7 | 2014-11-28 14:34:27 +0100 | [diff] [blame] | 222 | bool ctrls[ROCKER_CTRL_MAX]; |
| 223 | unsigned long vlan_bitmap[ROCKER_VLAN_BITMAP_LEN]; |
Jiri Pirko | 4b8ac96 | 2014-11-28 14:34:26 +0100 | [diff] [blame] | 224 | struct napi_struct napi_tx; |
| 225 | struct napi_struct napi_rx; |
| 226 | struct rocker_dma_ring_info tx_ring; |
| 227 | struct rocker_dma_ring_info rx_ring; |
Scott Feldman | c4f2032 | 2015-05-10 09:47:50 -0700 | [diff] [blame] | 228 | struct list_head trans_mem; |
Jiri Pirko | 4b8ac96 | 2014-11-28 14:34:26 +0100 | [diff] [blame] | 229 | }; |
| 230 | |
| 231 | struct 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 Feldman | 4725ceb | 2015-05-10 09:48:08 -0700 | [diff] [blame] | 240 | spinlock_t cmd_ring_lock; /* for cmd ring accesses */ |
Jiri Pirko | 4b8ac96 | 2014-11-28 14:34:26 +0100 | [diff] [blame] | 241 | struct rocker_dma_ring_info cmd_ring; |
| 242 | struct rocker_dma_ring_info event_ring; |
Scott Feldman | 9f6bbf7 | 2014-11-28 14:34:27 +0100 | [diff] [blame] | 243 | DECLARE_HASHTABLE(flow_tbl, 16); |
Scott Feldman | 4725ceb | 2015-05-10 09:48:08 -0700 | [diff] [blame] | 244 | spinlock_t flow_tbl_lock; /* for flow tbl accesses */ |
Scott Feldman | 9f6bbf7 | 2014-11-28 14:34:27 +0100 | [diff] [blame] | 245 | u64 flow_tbl_next_cookie; |
| 246 | DECLARE_HASHTABLE(group_tbl, 16); |
Scott Feldman | 4725ceb | 2015-05-10 09:48:08 -0700 | [diff] [blame] | 247 | spinlock_t group_tbl_lock; /* for group tbl accesses */ |
Scott Feldman | 9f6bbf7 | 2014-11-28 14:34:27 +0100 | [diff] [blame] | 248 | DECLARE_HASHTABLE(fdb_tbl, 16); |
Scott Feldman | 4725ceb | 2015-05-10 09:48:08 -0700 | [diff] [blame] | 249 | spinlock_t fdb_tbl_lock; /* for fdb tbl accesses */ |
Scott Feldman | 9f6bbf7 | 2014-11-28 14:34:27 +0100 | [diff] [blame] | 250 | unsigned long internal_vlan_bitmap[ROCKER_INTERNAL_VLAN_BITMAP_LEN]; |
| 251 | DECLARE_HASHTABLE(internal_vlan_tbl, 8); |
Scott Feldman | 4725ceb | 2015-05-10 09:48:08 -0700 | [diff] [blame] | 252 | spinlock_t internal_vlan_tbl_lock; /* for vlan tbl accesses */ |
Scott Feldman | c1beeef | 2015-03-05 21:21:20 -0800 | [diff] [blame] | 253 | DECLARE_HASHTABLE(neigh_tbl, 16); |
Scott Feldman | 4725ceb | 2015-05-10 09:48:08 -0700 | [diff] [blame] | 254 | spinlock_t neigh_tbl_lock; /* for neigh tbl accesses */ |
Scott Feldman | c1beeef | 2015-03-05 21:21:20 -0800 | [diff] [blame] | 255 | u32 neigh_tbl_next_index; |
Jiri Pirko | 4b8ac96 | 2014-11-28 14:34:26 +0100 | [diff] [blame] | 256 | }; |
| 257 | |
Scott Feldman | 9f6bbf7 | 2014-11-28 14:34:27 +0100 | [diff] [blame] | 258 | static const u8 zero_mac[ETH_ALEN] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; |
| 259 | static const u8 ff_mac[ETH_ALEN] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }; |
| 260 | static const u8 ll_mac[ETH_ALEN] = { 0x01, 0x80, 0xc2, 0x00, 0x00, 0x00 }; |
| 261 | static const u8 ll_mask[ETH_ALEN] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xf0 }; |
| 262 | static const u8 mcast_mac[ETH_ALEN] = { 0x01, 0x00, 0x00, 0x00, 0x00, 0x00 }; |
| 263 | static const u8 ipv4_mcast[ETH_ALEN] = { 0x01, 0x00, 0x5e, 0x00, 0x00, 0x00 }; |
| 264 | static const u8 ipv4_mask[ETH_ALEN] = { 0xff, 0xff, 0xff, 0x80, 0x00, 0x00 }; |
| 265 | static const u8 ipv6_mcast[ETH_ALEN] = { 0x33, 0x33, 0x00, 0x00, 0x00, 0x00 }; |
| 266 | static const u8 ipv6_mask[ETH_ALEN] = { 0xff, 0xff, 0x00, 0x00, 0x00, 0x00 }; |
| 267 | |
| 268 | /* Rocker priority levels for flow table entries. Higher |
| 269 | * priority match takes precedence over lower priority match. |
| 270 | */ |
| 271 | |
| 272 | enum { |
| 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 Feldman | 9f6bbf7 | 2014-11-28 14:34:27 +0100 | [diff] [blame] | 278 | ROCKER_PRIORITY_BRIDGING_VLAN_DFLT_EXACT = 1, |
| 279 | ROCKER_PRIORITY_BRIDGING_VLAN_DFLT_WILD = 2, |
| 280 | ROCKER_PRIORITY_BRIDGING_VLAN = 3, |
| 281 | ROCKER_PRIORITY_BRIDGING_TENANT_DFLT_EXACT = 1, |
| 282 | ROCKER_PRIORITY_BRIDGING_TENANT_DFLT_WILD = 2, |
| 283 | ROCKER_PRIORITY_BRIDGING_TENANT = 3, |
| 284 | ROCKER_PRIORITY_ACL_CTRL = 3, |
| 285 | ROCKER_PRIORITY_ACL_NORMAL = 2, |
| 286 | ROCKER_PRIORITY_ACL_DFLT = 1, |
| 287 | }; |
| 288 | |
| 289 | static 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 Horman | e505464 | 2015-05-25 14:28:36 +0900 | [diff] [blame] | 298 | static __be16 rocker_port_vid_to_vlan(const struct rocker_port *rocker_port, |
Scott Feldman | 9f6bbf7 | 2014-11-28 14:34:27 +0100 | [diff] [blame] | 299 | 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 Horman | e505464 | 2015-05-25 14:28:36 +0900 | [diff] [blame] | 315 | static u16 rocker_port_vlan_to_vid(const struct rocker_port *rocker_port, |
Scott Feldman | 6c70794 | 2014-11-28 14:34:28 +0100 | [diff] [blame] | 316 | __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 Horman | e505464 | 2015-05-25 14:28:36 +0900 | [diff] [blame] | 324 | static bool rocker_port_is_bridged(const struct rocker_port *rocker_port) |
Scott Feldman | 6c70794 | 2014-11-28 14:34:28 +0100 | [diff] [blame] | 325 | { |
| 326 | return !!rocker_port->bridge_dev; |
| 327 | } |
| 328 | |
Scott Feldman | 179f9a2 | 2015-06-12 21:35:46 -0700 | [diff] [blame] | 329 | #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 Feldman | c4f2032 | 2015-05-10 09:47:50 -0700 | [diff] [blame] | 334 | static void *__rocker_port_mem_alloc(struct rocker_port *rocker_port, |
Scott Feldman | 179f9a2 | 2015-06-12 21:35:46 -0700 | [diff] [blame] | 335 | enum switchdev_trans trans, int flags, |
| 336 | size_t size) |
Scott Feldman | c4f2032 | 2015-05-10 09:47:50 -0700 | [diff] [blame] | 337 | { |
| 338 | struct list_head *elem = NULL; |
Scott Feldman | 179f9a2 | 2015-06-12 21:35:46 -0700 | [diff] [blame] | 339 | gfp_t gfp_flags = (flags & ROCKER_OP_FLAG_NOWAIT) ? |
| 340 | GFP_ATOMIC : GFP_KERNEL; |
Scott Feldman | c4f2032 | 2015-05-10 09:47:50 -0700 | [diff] [blame] | 341 | |
| 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 Feldman | 179f9a2 | 2015-06-12 21:35:46 -0700 | [diff] [blame] | 353 | elem = kzalloc(size + sizeof(*elem), gfp_flags); |
Scott Feldman | c4f2032 | 2015-05-10 09:47:50 -0700 | [diff] [blame] | 354 | 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 Feldman | 179f9a2 | 2015-06-12 21:35:46 -0700 | [diff] [blame] | 364 | elem = kzalloc(size + sizeof(*elem), gfp_flags); |
Scott Feldman | c4f2032 | 2015-05-10 09:47:50 -0700 | [diff] [blame] | 365 | if (elem) |
| 366 | INIT_LIST_HEAD(elem); |
| 367 | break; |
| 368 | default: |
| 369 | break; |
| 370 | } |
| 371 | |
| 372 | return elem ? elem + 1 : NULL; |
| 373 | } |
| 374 | |
| 375 | static void *rocker_port_kzalloc(struct rocker_port *rocker_port, |
Scott Feldman | 179f9a2 | 2015-06-12 21:35:46 -0700 | [diff] [blame] | 376 | enum switchdev_trans trans, int flags, |
| 377 | size_t size) |
Scott Feldman | c4f2032 | 2015-05-10 09:47:50 -0700 | [diff] [blame] | 378 | { |
Scott Feldman | 179f9a2 | 2015-06-12 21:35:46 -0700 | [diff] [blame] | 379 | return __rocker_port_mem_alloc(rocker_port, trans, flags, size); |
Scott Feldman | c4f2032 | 2015-05-10 09:47:50 -0700 | [diff] [blame] | 380 | } |
| 381 | |
| 382 | static void *rocker_port_kcalloc(struct rocker_port *rocker_port, |
Scott Feldman | 179f9a2 | 2015-06-12 21:35:46 -0700 | [diff] [blame] | 383 | enum switchdev_trans trans, int flags, |
| 384 | size_t n, size_t size) |
Scott Feldman | c4f2032 | 2015-05-10 09:47:50 -0700 | [diff] [blame] | 385 | { |
Scott Feldman | 179f9a2 | 2015-06-12 21:35:46 -0700 | [diff] [blame] | 386 | return __rocker_port_mem_alloc(rocker_port, trans, flags, n * size); |
Scott Feldman | c4f2032 | 2015-05-10 09:47:50 -0700 | [diff] [blame] | 387 | } |
| 388 | |
Simon Horman | 0985df7 | 2015-05-25 14:28:35 +0900 | [diff] [blame] | 389 | static void rocker_port_kfree(enum switchdev_trans trans, const void *mem) |
Scott Feldman | c4f2032 | 2015-05-10 09:47:50 -0700 | [diff] [blame] | 390 | { |
| 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 Pirko | 4b8ac96 | 2014-11-28 14:34:26 +0100 | [diff] [blame] | 406 | struct rocker_wait { |
| 407 | wait_queue_head_t wait; |
| 408 | bool done; |
Scott Feldman | 179f9a2 | 2015-06-12 21:35:46 -0700 | [diff] [blame] | 409 | bool nowait; |
Jiri Pirko | 4b8ac96 | 2014-11-28 14:34:26 +0100 | [diff] [blame] | 410 | }; |
| 411 | |
| 412 | static void rocker_wait_reset(struct rocker_wait *wait) |
| 413 | { |
| 414 | wait->done = false; |
Scott Feldman | 179f9a2 | 2015-06-12 21:35:46 -0700 | [diff] [blame] | 415 | wait->nowait = false; |
Jiri Pirko | 4b8ac96 | 2014-11-28 14:34:26 +0100 | [diff] [blame] | 416 | } |
| 417 | |
| 418 | static void rocker_wait_init(struct rocker_wait *wait) |
| 419 | { |
| 420 | init_waitqueue_head(&wait->wait); |
| 421 | rocker_wait_reset(wait); |
| 422 | } |
| 423 | |
Scott Feldman | c4f2032 | 2015-05-10 09:47:50 -0700 | [diff] [blame] | 424 | static struct rocker_wait *rocker_wait_create(struct rocker_port *rocker_port, |
Scott Feldman | 179f9a2 | 2015-06-12 21:35:46 -0700 | [diff] [blame] | 425 | enum switchdev_trans trans, |
| 426 | int flags) |
Jiri Pirko | 4b8ac96 | 2014-11-28 14:34:26 +0100 | [diff] [blame] | 427 | { |
| 428 | struct rocker_wait *wait; |
| 429 | |
Scott Feldman | 179f9a2 | 2015-06-12 21:35:46 -0700 | [diff] [blame] | 430 | wait = rocker_port_kzalloc(rocker_port, trans, flags, sizeof(*wait)); |
Jiri Pirko | 4b8ac96 | 2014-11-28 14:34:26 +0100 | [diff] [blame] | 431 | if (!wait) |
| 432 | return NULL; |
| 433 | rocker_wait_init(wait); |
| 434 | return wait; |
| 435 | } |
| 436 | |
Simon Horman | 0985df7 | 2015-05-25 14:28:35 +0900 | [diff] [blame] | 437 | static void rocker_wait_destroy(enum switchdev_trans trans, |
Scott Feldman | c4f2032 | 2015-05-10 09:47:50 -0700 | [diff] [blame] | 438 | struct rocker_wait *wait) |
Jiri Pirko | 4b8ac96 | 2014-11-28 14:34:26 +0100 | [diff] [blame] | 439 | { |
Simon Horman | 0985df7 | 2015-05-25 14:28:35 +0900 | [diff] [blame] | 440 | rocker_port_kfree(trans, wait); |
Jiri Pirko | 4b8ac96 | 2014-11-28 14:34:26 +0100 | [diff] [blame] | 441 | } |
| 442 | |
| 443 | static 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 | |
| 452 | static void rocker_wait_wake_up(struct rocker_wait *wait) |
| 453 | { |
| 454 | wait->done = true; |
| 455 | wake_up(&wait->wait); |
| 456 | } |
| 457 | |
Simon Horman | e505464 | 2015-05-25 14:28:36 +0900 | [diff] [blame] | 458 | static u32 rocker_msix_vector(const struct rocker *rocker, unsigned int vector) |
Jiri Pirko | 4b8ac96 | 2014-11-28 14:34:26 +0100 | [diff] [blame] | 459 | { |
| 460 | return rocker->msix_entries[vector].vector; |
| 461 | } |
| 462 | |
Simon Horman | e505464 | 2015-05-25 14:28:36 +0900 | [diff] [blame] | 463 | static u32 rocker_msix_tx_vector(const struct rocker_port *rocker_port) |
Jiri Pirko | 4b8ac96 | 2014-11-28 14:34:26 +0100 | [diff] [blame] | 464 | { |
| 465 | return rocker_msix_vector(rocker_port->rocker, |
| 466 | ROCKER_MSIX_VEC_TX(rocker_port->port_number)); |
| 467 | } |
| 468 | |
Simon Horman | e505464 | 2015-05-25 14:28:36 +0900 | [diff] [blame] | 469 | static u32 rocker_msix_rx_vector(const struct rocker_port *rocker_port) |
Jiri Pirko | 4b8ac96 | 2014-11-28 14:34:26 +0100 | [diff] [blame] | 470 | { |
| 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 Horman | e505464 | 2015-05-25 14:28:36 +0900 | [diff] [blame] | 488 | static int rocker_reg_test(const struct rocker *rocker) |
Jiri Pirko | 4b8ac96 | 2014-11-28 14:34:26 +0100 | [diff] [blame] | 489 | { |
Simon Horman | e505464 | 2015-05-25 14:28:36 +0900 | [diff] [blame] | 490 | const struct pci_dev *pdev = rocker->pdev; |
Jiri Pirko | 4b8ac96 | 2014-11-28 14:34:26 +0100 | [diff] [blame] | 491 | 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 Horman | e505464 | 2015-05-25 14:28:36 +0900 | [diff] [blame] | 518 | static 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 Pirko | 4b8ac96 | 2014-11-28 14:34:26 +0100 | [diff] [blame] | 522 | { |
Simon Horman | e505464 | 2015-05-25 14:28:36 +0900 | [diff] [blame] | 523 | const struct pci_dev *pdev = rocker->pdev; |
Jiri Pirko | 4b8ac96 | 2014-11-28 14:34:26 +0100 | [diff] [blame] | 524 | 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 Horman | e505464 | 2015-05-25 14:28:36 +0900 | [diff] [blame] | 547 | static int rocker_dma_test_offset(const struct rocker *rocker, |
Jiri Pirko | 4b8ac96 | 2014-11-28 14:34:26 +0100 | [diff] [blame] | 548 | 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 | |
| 598 | unmap: |
| 599 | pci_unmap_single(pdev, dma_handle, ROCKER_TEST_DMA_BUF_SIZE, |
| 600 | PCI_DMA_BIDIRECTIONAL); |
| 601 | free_alloc: |
| 602 | kfree(alloc); |
| 603 | |
| 604 | return err; |
| 605 | } |
| 606 | |
Simon Horman | e505464 | 2015-05-25 14:28:36 +0900 | [diff] [blame] | 607 | static int rocker_dma_test(const struct rocker *rocker, |
| 608 | struct rocker_wait *wait) |
Jiri Pirko | 4b8ac96 | 2014-11-28 14:34:26 +0100 | [diff] [blame] | 609 | { |
| 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 | |
| 621 | static 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 Horman | e505464 | 2015-05-25 14:28:36 +0900 | [diff] [blame] | 630 | static int rocker_basic_hw_test(const struct rocker *rocker) |
Jiri Pirko | 4b8ac96 | 2014-11-28 14:34:26 +0100 | [diff] [blame] | 631 | { |
Simon Horman | e505464 | 2015-05-25 14:28:36 +0900 | [diff] [blame] | 632 | const struct pci_dev *pdev = rocker->pdev; |
Jiri Pirko | 4b8ac96 | 2014-11-28 14:34:26 +0100 | [diff] [blame] | 633 | 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 | |
| 663 | free_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 | |
| 685 | static 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 | |
| 694 | static 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 | |
| 710 | static int rocker_tlv_attr_size(int payload) |
| 711 | { |
| 712 | return ROCKER_TLV_HDRLEN + payload; |
| 713 | } |
| 714 | |
| 715 | static int rocker_tlv_total_size(int payload) |
| 716 | { |
| 717 | return ROCKER_TLV_ALIGN(rocker_tlv_attr_size(payload)); |
| 718 | } |
| 719 | |
| 720 | static int rocker_tlv_padlen(int payload) |
| 721 | { |
| 722 | return rocker_tlv_total_size(payload) - rocker_tlv_attr_size(payload); |
| 723 | } |
| 724 | |
| 725 | static int rocker_tlv_type(const struct rocker_tlv *tlv) |
| 726 | { |
| 727 | return tlv->type; |
| 728 | } |
| 729 | |
| 730 | static void *rocker_tlv_data(const struct rocker_tlv *tlv) |
| 731 | { |
| 732 | return (char *) tlv + ROCKER_TLV_HDRLEN; |
| 733 | } |
| 734 | |
| 735 | static int rocker_tlv_len(const struct rocker_tlv *tlv) |
| 736 | { |
| 737 | return tlv->len - ROCKER_TLV_HDRLEN; |
| 738 | } |
| 739 | |
| 740 | static u8 rocker_tlv_get_u8(const struct rocker_tlv *tlv) |
| 741 | { |
| 742 | return *(u8 *) rocker_tlv_data(tlv); |
| 743 | } |
| 744 | |
| 745 | static u16 rocker_tlv_get_u16(const struct rocker_tlv *tlv) |
| 746 | { |
| 747 | return *(u16 *) rocker_tlv_data(tlv); |
| 748 | } |
| 749 | |
Jiri Pirko | 9b03c71 | 2014-12-03 14:14:53 +0100 | [diff] [blame] | 750 | static __be16 rocker_tlv_get_be16(const struct rocker_tlv *tlv) |
| 751 | { |
| 752 | return *(__be16 *) rocker_tlv_data(tlv); |
| 753 | } |
| 754 | |
Jiri Pirko | 4b8ac96 | 2014-11-28 14:34:26 +0100 | [diff] [blame] | 755 | static u32 rocker_tlv_get_u32(const struct rocker_tlv *tlv) |
| 756 | { |
| 757 | return *(u32 *) rocker_tlv_data(tlv); |
| 758 | } |
| 759 | |
| 760 | static u64 rocker_tlv_get_u64(const struct rocker_tlv *tlv) |
| 761 | { |
| 762 | return *(u64 *) rocker_tlv_data(tlv); |
| 763 | } |
| 764 | |
Simon Horman | e505464 | 2015-05-25 14:28:36 +0900 | [diff] [blame] | 765 | static void rocker_tlv_parse(const struct rocker_tlv **tb, int maxtype, |
Jiri Pirko | 4b8ac96 | 2014-11-28 14:34:26 +0100 | [diff] [blame] | 766 | 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 Horman | e505464 | 2015-05-25 14:28:36 +0900 | [diff] [blame] | 778 | tb[type] = tlv; |
Jiri Pirko | 4b8ac96 | 2014-11-28 14:34:26 +0100 | [diff] [blame] | 779 | } |
| 780 | } |
| 781 | |
Simon Horman | e505464 | 2015-05-25 14:28:36 +0900 | [diff] [blame] | 782 | static void rocker_tlv_parse_nested(const struct rocker_tlv **tb, int maxtype, |
Jiri Pirko | 4b8ac96 | 2014-11-28 14:34:26 +0100 | [diff] [blame] | 783 | const struct rocker_tlv *tlv) |
| 784 | { |
| 785 | rocker_tlv_parse(tb, maxtype, rocker_tlv_data(tlv), |
| 786 | rocker_tlv_len(tlv)); |
| 787 | } |
| 788 | |
Simon Horman | e505464 | 2015-05-25 14:28:36 +0900 | [diff] [blame] | 789 | static void rocker_tlv_parse_desc(const struct rocker_tlv **tb, int maxtype, |
| 790 | const struct rocker_desc_info *desc_info) |
Jiri Pirko | 4b8ac96 | 2014-11-28 14:34:26 +0100 | [diff] [blame] | 791 | { |
| 792 | rocker_tlv_parse(tb, maxtype, desc_info->data, |
| 793 | desc_info->desc->tlv_size); |
| 794 | } |
| 795 | |
| 796 | static 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 | |
| 802 | static 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 | |
| 821 | static 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 | |
| 827 | static 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 Pirko | 9b03c71 | 2014-12-03 14:14:53 +0100 | [diff] [blame] | 833 | static 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 Pirko | 4b8ac96 | 2014-11-28 14:34:26 +0100 | [diff] [blame] | 839 | static 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 Pirko | 9b03c71 | 2014-12-03 14:14:53 +0100 | [diff] [blame] | 845 | static 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 Pirko | 4b8ac96 | 2014-11-28 14:34:26 +0100 | [diff] [blame] | 851 | static 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 | |
| 857 | static struct rocker_tlv * |
| 858 | rocker_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 | |
| 868 | static 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 | |
| 874 | static void rocker_tlv_nest_cancel(struct rocker_desc_info *desc_info, |
Simon Horman | e505464 | 2015-05-25 14:28:36 +0900 | [diff] [blame] | 875 | const struct rocker_tlv *start) |
Jiri Pirko | 4b8ac96 | 2014-11-28 14:34:26 +0100 | [diff] [blame] | 876 | { |
Simon Horman | e505464 | 2015-05-25 14:28:36 +0900 | [diff] [blame] | 877 | desc_info->tlv_size = (const char *) start - desc_info->data; |
Jiri Pirko | 4b8ac96 | 2014-11-28 14:34:26 +0100 | [diff] [blame] | 878 | } |
| 879 | |
| 880 | /****************************************** |
| 881 | * DMA rings and descriptors manipulations |
| 882 | ******************************************/ |
| 883 | |
| 884 | static u32 __pos_inc(u32 pos, size_t limit) |
| 885 | { |
| 886 | return ++pos == limit ? 0 : pos; |
| 887 | } |
| 888 | |
Simon Horman | e505464 | 2015-05-25 14:28:36 +0900 | [diff] [blame] | 889 | static int rocker_desc_err(const struct rocker_desc_info *desc_info) |
Jiri Pirko | 4b8ac96 | 2014-11-28 14:34:26 +0100 | [diff] [blame] | 890 | { |
Scott Feldman | 7eb344f | 2015-02-25 20:15:36 -0800 | [diff] [blame] | 891 | 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 Pirko | 4b8ac96 | 2014-11-28 14:34:26 +0100 | [diff] [blame] | 915 | } |
| 916 | |
Simon Horman | e505464 | 2015-05-25 14:28:36 +0900 | [diff] [blame] | 917 | static void rocker_desc_gen_clear(const struct rocker_desc_info *desc_info) |
Jiri Pirko | 4b8ac96 | 2014-11-28 14:34:26 +0100 | [diff] [blame] | 918 | { |
| 919 | desc_info->desc->comp_err &= ~ROCKER_DMA_DESC_COMP_ERR_GEN; |
| 920 | } |
| 921 | |
Simon Horman | e505464 | 2015-05-25 14:28:36 +0900 | [diff] [blame] | 922 | static bool rocker_desc_gen(const struct rocker_desc_info *desc_info) |
Jiri Pirko | 4b8ac96 | 2014-11-28 14:34:26 +0100 | [diff] [blame] | 923 | { |
| 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 Horman | e505464 | 2015-05-25 14:28:36 +0900 | [diff] [blame] | 929 | static void *rocker_desc_cookie_ptr_get(const struct rocker_desc_info *desc_info) |
Jiri Pirko | 4b8ac96 | 2014-11-28 14:34:26 +0100 | [diff] [blame] | 930 | { |
Arnd Bergmann | adedf37 | 2015-01-13 15:23:52 +0100 | [diff] [blame] | 931 | return (void *)(uintptr_t)desc_info->desc->cookie; |
Jiri Pirko | 4b8ac96 | 2014-11-28 14:34:26 +0100 | [diff] [blame] | 932 | } |
| 933 | |
Simon Horman | e505464 | 2015-05-25 14:28:36 +0900 | [diff] [blame] | 934 | static void rocker_desc_cookie_ptr_set(const struct rocker_desc_info *desc_info, |
Jiri Pirko | 4b8ac96 | 2014-11-28 14:34:26 +0100 | [diff] [blame] | 935 | void *ptr) |
| 936 | { |
Arnd Bergmann | adedf37 | 2015-01-13 15:23:52 +0100 | [diff] [blame] | 937 | desc_info->desc->cookie = (uintptr_t) ptr; |
Jiri Pirko | 4b8ac96 | 2014-11-28 14:34:26 +0100 | [diff] [blame] | 938 | } |
| 939 | |
| 940 | static struct rocker_desc_info * |
Simon Horman | e505464 | 2015-05-25 14:28:36 +0900 | [diff] [blame] | 941 | rocker_desc_head_get(const struct rocker_dma_ring_info *info) |
Jiri Pirko | 4b8ac96 | 2014-11-28 14:34:26 +0100 | [diff] [blame] | 942 | { |
| 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 Horman | e505464 | 2015-05-25 14:28:36 +0900 | [diff] [blame] | 953 | static void rocker_desc_commit(const struct rocker_desc_info *desc_info) |
Jiri Pirko | 4b8ac96 | 2014-11-28 14:34:26 +0100 | [diff] [blame] | 954 | { |
| 955 | desc_info->desc->buf_size = desc_info->data_size; |
| 956 | desc_info->desc->tlv_size = desc_info->tlv_size; |
| 957 | } |
| 958 | |
Simon Horman | e505464 | 2015-05-25 14:28:36 +0900 | [diff] [blame] | 959 | static void rocker_desc_head_set(const struct rocker *rocker, |
Jiri Pirko | 4b8ac96 | 2014-11-28 14:34:26 +0100 | [diff] [blame] | 960 | struct rocker_dma_ring_info *info, |
Simon Horman | e505464 | 2015-05-25 14:28:36 +0900 | [diff] [blame] | 961 | const struct rocker_desc_info *desc_info) |
Jiri Pirko | 4b8ac96 | 2014-11-28 14:34:26 +0100 | [diff] [blame] | 962 | { |
| 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 | |
| 971 | static struct rocker_desc_info * |
| 972 | rocker_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 Horman | e505464 | 2015-05-25 14:28:36 +0900 | [diff] [blame] | 986 | static void rocker_dma_ring_credits_set(const struct rocker *rocker, |
| 987 | const struct rocker_dma_ring_info *info, |
Jiri Pirko | 4b8ac96 | 2014-11-28 14:34:26 +0100 | [diff] [blame] | 988 | u32 credits) |
| 989 | { |
| 990 | if (credits) |
| 991 | rocker_write32(rocker, DMA_DESC_CREDITS(info->type), credits); |
| 992 | } |
| 993 | |
| 994 | static 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 Horman | e505464 | 2015-05-25 14:28:36 +0900 | [diff] [blame] | 1000 | static int rocker_dma_ring_create(const struct rocker *rocker, |
Jiri Pirko | 4b8ac96 | 2014-11-28 14:34:26 +0100 | [diff] [blame] | 1001 | 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 Horman | e505464 | 2015-05-25 14:28:36 +0900 | [diff] [blame] | 1036 | static void rocker_dma_ring_destroy(const struct rocker *rocker, |
| 1037 | const struct rocker_dma_ring_info *info) |
Jiri Pirko | 4b8ac96 | 2014-11-28 14:34:26 +0100 | [diff] [blame] | 1038 | { |
| 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 Horman | e505464 | 2015-05-25 14:28:36 +0900 | [diff] [blame] | 1047 | static void rocker_dma_ring_pass_to_producer(const struct rocker *rocker, |
Jiri Pirko | 4b8ac96 | 2014-11-28 14:34:26 +0100 | [diff] [blame] | 1048 | 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 Horman | e505464 | 2015-05-25 14:28:36 +0900 | [diff] [blame] | 1062 | static int rocker_dma_ring_bufs_alloc(const struct rocker *rocker, |
| 1063 | const struct rocker_dma_ring_info *info, |
Jiri Pirko | 4b8ac96 | 2014-11-28 14:34:26 +0100 | [diff] [blame] | 1064 | 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 | |
| 1098 | rollback: |
| 1099 | for (i--; i >= 0; i--) { |
Simon Horman | e505464 | 2015-05-25 14:28:36 +0900 | [diff] [blame] | 1100 | const struct rocker_desc_info *desc_info = &info->desc_info[i]; |
Jiri Pirko | 4b8ac96 | 2014-11-28 14:34:26 +0100 | [diff] [blame] | 1101 | |
| 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 Horman | e505464 | 2015-05-25 14:28:36 +0900 | [diff] [blame] | 1109 | static void rocker_dma_ring_bufs_free(const struct rocker *rocker, |
| 1110 | const struct rocker_dma_ring_info *info, |
Jiri Pirko | 4b8ac96 | 2014-11-28 14:34:26 +0100 | [diff] [blame] | 1111 | int direction) |
| 1112 | { |
| 1113 | struct pci_dev *pdev = rocker->pdev; |
| 1114 | int i; |
| 1115 | |
| 1116 | for (i = 0; i < info->size; i++) { |
Simon Horman | e505464 | 2015-05-25 14:28:36 +0900 | [diff] [blame] | 1117 | const struct rocker_desc_info *desc_info = &info->desc_info[i]; |
Jiri Pirko | 4b8ac96 | 2014-11-28 14:34:26 +0100 | [diff] [blame] | 1118 | 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 | |
| 1128 | static int rocker_dma_rings_init(struct rocker *rocker) |
| 1129 | { |
Simon Horman | e505464 | 2015-05-25 14:28:36 +0900 | [diff] [blame] | 1130 | const struct pci_dev *pdev = rocker->pdev; |
Jiri Pirko | 4b8ac96 | 2014-11-28 14:34:26 +0100 | [diff] [blame] | 1131 | 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 | |
| 1167 | err_dma_event_ring_bufs_alloc: |
| 1168 | rocker_dma_ring_destroy(rocker, &rocker->event_ring); |
| 1169 | err_dma_event_ring_create: |
| 1170 | rocker_dma_ring_bufs_free(rocker, &rocker->cmd_ring, |
| 1171 | PCI_DMA_BIDIRECTIONAL); |
| 1172 | err_dma_cmd_ring_bufs_alloc: |
| 1173 | rocker_dma_ring_destroy(rocker, &rocker->cmd_ring); |
| 1174 | return err; |
| 1175 | } |
| 1176 | |
| 1177 | static 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 Horman | 534ba6a | 2015-06-01 13:25:04 +0900 | [diff] [blame] | 1187 | static int rocker_dma_rx_ring_skb_map(const struct rocker_port *rocker_port, |
Jiri Pirko | 4b8ac96 | 2014-11-28 14:34:26 +0100 | [diff] [blame] | 1188 | struct rocker_desc_info *desc_info, |
| 1189 | struct sk_buff *skb, size_t buf_len) |
| 1190 | { |
Simon Horman | 534ba6a | 2015-06-01 13:25:04 +0900 | [diff] [blame] | 1191 | const struct rocker *rocker = rocker_port->rocker; |
Jiri Pirko | 4b8ac96 | 2014-11-28 14:34:26 +0100 | [diff] [blame] | 1192 | 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 | |
| 1205 | tlv_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 Horman | e505464 | 2015-05-25 14:28:36 +0900 | [diff] [blame] | 1211 | static size_t rocker_port_rx_buf_len(const struct rocker_port *rocker_port) |
Jiri Pirko | 4b8ac96 | 2014-11-28 14:34:26 +0100 | [diff] [blame] | 1212 | { |
| 1213 | return rocker_port->dev->mtu + ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN; |
| 1214 | } |
| 1215 | |
Simon Horman | 534ba6a | 2015-06-01 13:25:04 +0900 | [diff] [blame] | 1216 | static int rocker_dma_rx_ring_skb_alloc(const struct rocker_port *rocker_port, |
Jiri Pirko | 4b8ac96 | 2014-11-28 14:34:26 +0100 | [diff] [blame] | 1217 | 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 Horman | 534ba6a | 2015-06-01 13:25:04 +0900 | [diff] [blame] | 1233 | err = rocker_dma_rx_ring_skb_map(rocker_port, desc_info, skb, buf_len); |
Jiri Pirko | 4b8ac96 | 2014-11-28 14:34:26 +0100 | [diff] [blame] | 1234 | 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 Horman | e505464 | 2015-05-25 14:28:36 +0900 | [diff] [blame] | 1242 | static void rocker_dma_rx_ring_skb_unmap(const struct rocker *rocker, |
| 1243 | const struct rocker_tlv **attrs) |
Jiri Pirko | 4b8ac96 | 2014-11-28 14:34:26 +0100 | [diff] [blame] | 1244 | { |
| 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 Horman | e505464 | 2015-05-25 14:28:36 +0900 | [diff] [blame] | 1257 | static void rocker_dma_rx_ring_skb_free(const struct rocker *rocker, |
| 1258 | const struct rocker_desc_info *desc_info) |
Jiri Pirko | 4b8ac96 | 2014-11-28 14:34:26 +0100 | [diff] [blame] | 1259 | { |
Simon Horman | e505464 | 2015-05-25 14:28:36 +0900 | [diff] [blame] | 1260 | const struct rocker_tlv *attrs[ROCKER_TLV_RX_MAX + 1]; |
Jiri Pirko | 4b8ac96 | 2014-11-28 14:34:26 +0100 | [diff] [blame] | 1261 | 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 Horman | 534ba6a | 2015-06-01 13:25:04 +0900 | [diff] [blame] | 1270 | static int rocker_dma_rx_ring_skbs_alloc(const struct rocker_port *rocker_port) |
Jiri Pirko | 4b8ac96 | 2014-11-28 14:34:26 +0100 | [diff] [blame] | 1271 | { |
Simon Horman | e505464 | 2015-05-25 14:28:36 +0900 | [diff] [blame] | 1272 | const struct rocker_dma_ring_info *rx_ring = &rocker_port->rx_ring; |
Simon Horman | 534ba6a | 2015-06-01 13:25:04 +0900 | [diff] [blame] | 1273 | const struct rocker *rocker = rocker_port->rocker; |
Jiri Pirko | 4b8ac96 | 2014-11-28 14:34:26 +0100 | [diff] [blame] | 1274 | int i; |
| 1275 | int err; |
| 1276 | |
| 1277 | for (i = 0; i < rx_ring->size; i++) { |
Simon Horman | 534ba6a | 2015-06-01 13:25:04 +0900 | [diff] [blame] | 1278 | err = rocker_dma_rx_ring_skb_alloc(rocker_port, |
Jiri Pirko | 4b8ac96 | 2014-11-28 14:34:26 +0100 | [diff] [blame] | 1279 | &rx_ring->desc_info[i]); |
| 1280 | if (err) |
| 1281 | goto rollback; |
| 1282 | } |
| 1283 | return 0; |
| 1284 | |
| 1285 | rollback: |
| 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 Horman | 534ba6a | 2015-06-01 13:25:04 +0900 | [diff] [blame] | 1291 | static void rocker_dma_rx_ring_skbs_free(const struct rocker_port *rocker_port) |
Jiri Pirko | 4b8ac96 | 2014-11-28 14:34:26 +0100 | [diff] [blame] | 1292 | { |
Simon Horman | e505464 | 2015-05-25 14:28:36 +0900 | [diff] [blame] | 1293 | const struct rocker_dma_ring_info *rx_ring = &rocker_port->rx_ring; |
Simon Horman | 534ba6a | 2015-06-01 13:25:04 +0900 | [diff] [blame] | 1294 | const struct rocker *rocker = rocker_port->rocker; |
Jiri Pirko | 4b8ac96 | 2014-11-28 14:34:26 +0100 | [diff] [blame] | 1295 | 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 | |
| 1301 | static 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 Horman | 534ba6a | 2015-06-01 13:25:04 +0900 | [diff] [blame] | 1340 | err = rocker_dma_rx_ring_skbs_alloc(rocker_port); |
Jiri Pirko | 4b8ac96 | 2014-11-28 14:34:26 +0100 | [diff] [blame] | 1341 | 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 | |
| 1349 | err_dma_rx_ring_skbs_alloc: |
| 1350 | rocker_dma_ring_bufs_free(rocker, &rocker_port->rx_ring, |
| 1351 | PCI_DMA_BIDIRECTIONAL); |
| 1352 | err_dma_rx_ring_bufs_alloc: |
| 1353 | rocker_dma_ring_destroy(rocker, &rocker_port->rx_ring); |
| 1354 | err_dma_rx_ring_create: |
| 1355 | rocker_dma_ring_bufs_free(rocker, &rocker_port->tx_ring, |
| 1356 | PCI_DMA_TODEVICE); |
| 1357 | err_dma_tx_ring_bufs_alloc: |
| 1358 | rocker_dma_ring_destroy(rocker, &rocker_port->tx_ring); |
| 1359 | return err; |
| 1360 | } |
| 1361 | |
| 1362 | static void rocker_port_dma_rings_fini(struct rocker_port *rocker_port) |
| 1363 | { |
| 1364 | struct rocker *rocker = rocker_port->rocker; |
| 1365 | |
Simon Horman | 534ba6a | 2015-06-01 13:25:04 +0900 | [diff] [blame] | 1366 | rocker_dma_rx_ring_skbs_free(rocker_port); |
Jiri Pirko | 4b8ac96 | 2014-11-28 14:34:26 +0100 | [diff] [blame] | 1367 | 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 Horman | e505464 | 2015-05-25 14:28:36 +0900 | [diff] [blame] | 1375 | static void rocker_port_set_enable(const struct rocker_port *rocker_port, |
| 1376 | bool enable) |
Jiri Pirko | 4b8ac96 | 2014-11-28 14:34:26 +0100 | [diff] [blame] | 1377 | { |
| 1378 | u64 val = rocker_read64(rocker_port->rocker, PORT_PHYS_ENABLE); |
| 1379 | |
| 1380 | if (enable) |
David S. Miller | 71a83a6 | 2015-03-03 21:16:48 -0500 | [diff] [blame] | 1381 | val |= 1ULL << rocker_port->pport; |
Jiri Pirko | 4b8ac96 | 2014-11-28 14:34:26 +0100 | [diff] [blame] | 1382 | else |
David S. Miller | 71a83a6 | 2015-03-03 21:16:48 -0500 | [diff] [blame] | 1383 | val &= ~(1ULL << rocker_port->pport); |
Jiri Pirko | 4b8ac96 | 2014-11-28 14:34:26 +0100 | [diff] [blame] | 1384 | rocker_write64(rocker_port->rocker, PORT_PHYS_ENABLE, val); |
| 1385 | } |
| 1386 | |
| 1387 | /******************************** |
| 1388 | * Interrupt handler and helpers |
| 1389 | ********************************/ |
| 1390 | |
| 1391 | static irqreturn_t rocker_cmd_irq_handler(int irq, void *dev_id) |
| 1392 | { |
| 1393 | struct rocker *rocker = dev_id; |
Simon Horman | e505464 | 2015-05-25 14:28:36 +0900 | [diff] [blame] | 1394 | const struct rocker_desc_info *desc_info; |
Jiri Pirko | 4b8ac96 | 2014-11-28 14:34:26 +0100 | [diff] [blame] | 1395 | 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 Feldman | 179f9a2 | 2015-06-12 21:35:46 -0700 | [diff] [blame] | 1401 | 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 Pirko | 4b8ac96 | 2014-11-28 14:34:26 +0100 | [diff] [blame] | 1407 | 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 Horman | e505464 | 2015-05-25 14:28:36 +0900 | [diff] [blame] | 1415 | static void rocker_port_link_up(const struct rocker_port *rocker_port) |
Jiri Pirko | 4b8ac96 | 2014-11-28 14:34:26 +0100 | [diff] [blame] | 1416 | { |
| 1417 | netif_carrier_on(rocker_port->dev); |
| 1418 | netdev_info(rocker_port->dev, "Link is up\n"); |
| 1419 | } |
| 1420 | |
Simon Horman | e505464 | 2015-05-25 14:28:36 +0900 | [diff] [blame] | 1421 | static void rocker_port_link_down(const struct rocker_port *rocker_port) |
Jiri Pirko | 4b8ac96 | 2014-11-28 14:34:26 +0100 | [diff] [blame] | 1422 | { |
| 1423 | netif_carrier_off(rocker_port->dev); |
| 1424 | netdev_info(rocker_port->dev, "Link is down\n"); |
| 1425 | } |
| 1426 | |
Simon Horman | e505464 | 2015-05-25 14:28:36 +0900 | [diff] [blame] | 1427 | static int rocker_event_link_change(const struct rocker *rocker, |
Jiri Pirko | 4b8ac96 | 2014-11-28 14:34:26 +0100 | [diff] [blame] | 1428 | const struct rocker_tlv *info) |
| 1429 | { |
Simon Horman | e505464 | 2015-05-25 14:28:36 +0900 | [diff] [blame] | 1430 | const struct rocker_tlv *attrs[ROCKER_TLV_EVENT_LINK_CHANGED_MAX + 1]; |
Jiri Pirko | 4b8ac96 | 2014-11-28 14:34:26 +0100 | [diff] [blame] | 1431 | 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 Feldman | 4a6bb6d | 2015-02-25 20:15:37 -0800 | [diff] [blame] | 1436 | if (!attrs[ROCKER_TLV_EVENT_LINK_CHANGED_PPORT] || |
Jiri Pirko | 4b8ac96 | 2014-11-28 14:34:26 +0100 | [diff] [blame] | 1437 | !attrs[ROCKER_TLV_EVENT_LINK_CHANGED_LINKUP]) |
| 1438 | return -EIO; |
| 1439 | port_number = |
Scott Feldman | 4a6bb6d | 2015-02-25 20:15:37 -0800 | [diff] [blame] | 1440 | rocker_tlv_get_u32(attrs[ROCKER_TLV_EVENT_LINK_CHANGED_PPORT]) - 1; |
Jiri Pirko | 4b8ac96 | 2014-11-28 14:34:26 +0100 | [diff] [blame] | 1441 | 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 Feldman | 6c70794 | 2014-11-28 14:34:28 +0100 | [diff] [blame] | 1457 | static int rocker_port_fdb(struct rocker_port *rocker_port, |
Scott Feldman | c4f2032 | 2015-05-10 09:47:50 -0700 | [diff] [blame] | 1458 | enum switchdev_trans trans, |
Scott Feldman | 6c70794 | 2014-11-28 14:34:28 +0100 | [diff] [blame] | 1459 | const unsigned char *addr, |
| 1460 | __be16 vlan_id, int flags); |
| 1461 | |
Simon Horman | e505464 | 2015-05-25 14:28:36 +0900 | [diff] [blame] | 1462 | static int rocker_event_mac_vlan_seen(const struct rocker *rocker, |
Scott Feldman | 6c70794 | 2014-11-28 14:34:28 +0100 | [diff] [blame] | 1463 | const struct rocker_tlv *info) |
| 1464 | { |
Simon Horman | e505464 | 2015-05-25 14:28:36 +0900 | [diff] [blame] | 1465 | const struct rocker_tlv *attrs[ROCKER_TLV_EVENT_MAC_VLAN_MAX + 1]; |
Scott Feldman | 6c70794 | 2014-11-28 14:34:28 +0100 | [diff] [blame] | 1466 | unsigned int port_number; |
| 1467 | struct rocker_port *rocker_port; |
Simon Horman | e505464 | 2015-05-25 14:28:36 +0900 | [diff] [blame] | 1468 | const unsigned char *addr; |
Scott Feldman | 92014b9 | 2015-06-12 21:35:49 -0700 | [diff] [blame] | 1469 | int flags = ROCKER_OP_FLAG_NOWAIT | ROCKER_OP_FLAG_LEARNED; |
Scott Feldman | 6c70794 | 2014-11-28 14:34:28 +0100 | [diff] [blame] | 1470 | __be16 vlan_id; |
| 1471 | |
| 1472 | rocker_tlv_parse_nested(attrs, ROCKER_TLV_EVENT_MAC_VLAN_MAX, info); |
Scott Feldman | 4a6bb6d | 2015-02-25 20:15:37 -0800 | [diff] [blame] | 1473 | if (!attrs[ROCKER_TLV_EVENT_MAC_VLAN_PPORT] || |
Scott Feldman | 6c70794 | 2014-11-28 14:34:28 +0100 | [diff] [blame] | 1474 | !attrs[ROCKER_TLV_EVENT_MAC_VLAN_MAC] || |
| 1475 | !attrs[ROCKER_TLV_EVENT_MAC_VLAN_VLAN_ID]) |
| 1476 | return -EIO; |
| 1477 | port_number = |
Scott Feldman | 4a6bb6d | 2015-02-25 20:15:37 -0800 | [diff] [blame] | 1478 | rocker_tlv_get_u32(attrs[ROCKER_TLV_EVENT_MAC_VLAN_PPORT]) - 1; |
Scott Feldman | 6c70794 | 2014-11-28 14:34:28 +0100 | [diff] [blame] | 1479 | addr = rocker_tlv_data(attrs[ROCKER_TLV_EVENT_MAC_VLAN_MAC]); |
Jiri Pirko | 9b03c71 | 2014-12-03 14:14:53 +0100 | [diff] [blame] | 1480 | vlan_id = rocker_tlv_get_be16(attrs[ROCKER_TLV_EVENT_MAC_VLAN_VLAN_ID]); |
Scott Feldman | 6c70794 | 2014-11-28 14:34:28 +0100 | [diff] [blame] | 1481 | |
| 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 Feldman | 92014b9 | 2015-06-12 21:35:49 -0700 | [diff] [blame] | 1491 | return rocker_port_fdb(rocker_port, SWITCHDEV_TRANS_NONE, |
| 1492 | addr, vlan_id, flags); |
Scott Feldman | 6c70794 | 2014-11-28 14:34:28 +0100 | [diff] [blame] | 1493 | } |
Scott Feldman | 9f6bbf7 | 2014-11-28 14:34:27 +0100 | [diff] [blame] | 1494 | |
Simon Horman | e505464 | 2015-05-25 14:28:36 +0900 | [diff] [blame] | 1495 | static int rocker_event_process(const struct rocker *rocker, |
| 1496 | const struct rocker_desc_info *desc_info) |
Jiri Pirko | 4b8ac96 | 2014-11-28 14:34:26 +0100 | [diff] [blame] | 1497 | { |
Simon Horman | e505464 | 2015-05-25 14:28:36 +0900 | [diff] [blame] | 1498 | const struct rocker_tlv *attrs[ROCKER_TLV_EVENT_MAX + 1]; |
| 1499 | const struct rocker_tlv *info; |
Jiri Pirko | 4b8ac96 | 2014-11-28 14:34:26 +0100 | [diff] [blame] | 1500 | 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 Feldman | 6c70794 | 2014-11-28 14:34:28 +0100 | [diff] [blame] | 1513 | case ROCKER_TLV_EVENT_TYPE_MAC_VLAN_SEEN: |
| 1514 | return rocker_event_mac_vlan_seen(rocker, info); |
Jiri Pirko | 4b8ac96 | 2014-11-28 14:34:26 +0100 | [diff] [blame] | 1515 | } |
| 1516 | |
| 1517 | return -EOPNOTSUPP; |
| 1518 | } |
| 1519 | |
| 1520 | static irqreturn_t rocker_event_irq_handler(int irq, void *dev_id) |
| 1521 | { |
| 1522 | struct rocker *rocker = dev_id; |
Simon Horman | e505464 | 2015-05-25 14:28:36 +0900 | [diff] [blame] | 1523 | const struct pci_dev *pdev = rocker->pdev; |
| 1524 | const struct rocker_desc_info *desc_info; |
Jiri Pirko | 4b8ac96 | 2014-11-28 14:34:26 +0100 | [diff] [blame] | 1525 | 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 | |
| 1548 | static 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 | |
| 1556 | static 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 Horman | 534ba6a | 2015-06-01 13:25:04 +0900 | [diff] [blame] | 1568 | typedef int (*rocker_cmd_prep_cb_t)(const struct rocker_port *rocker_port, |
Simon Horman | e505464 | 2015-05-25 14:28:36 +0900 | [diff] [blame] | 1569 | struct rocker_desc_info *desc_info, |
| 1570 | void *priv); |
| 1571 | |
Simon Horman | 534ba6a | 2015-06-01 13:25:04 +0900 | [diff] [blame] | 1572 | typedef int (*rocker_cmd_proc_cb_t)(const struct rocker_port *rocker_port, |
Simon Horman | e505464 | 2015-05-25 14:28:36 +0900 | [diff] [blame] | 1573 | const struct rocker_desc_info *desc_info, |
| 1574 | void *priv); |
Jiri Pirko | 4b8ac96 | 2014-11-28 14:34:26 +0100 | [diff] [blame] | 1575 | |
Simon Horman | 534ba6a | 2015-06-01 13:25:04 +0900 | [diff] [blame] | 1576 | static int rocker_cmd_exec(struct rocker_port *rocker_port, |
Scott Feldman | 179f9a2 | 2015-06-12 21:35:46 -0700 | [diff] [blame] | 1577 | enum switchdev_trans trans, int flags, |
Simon Horman | e505464 | 2015-05-25 14:28:36 +0900 | [diff] [blame] | 1578 | rocker_cmd_prep_cb_t prepare, void *prepare_priv, |
| 1579 | rocker_cmd_proc_cb_t process, void *process_priv) |
Jiri Pirko | 4b8ac96 | 2014-11-28 14:34:26 +0100 | [diff] [blame] | 1580 | { |
Simon Horman | 534ba6a | 2015-06-01 13:25:04 +0900 | [diff] [blame] | 1581 | struct rocker *rocker = rocker_port->rocker; |
Jiri Pirko | 4b8ac96 | 2014-11-28 14:34:26 +0100 | [diff] [blame] | 1582 | struct rocker_desc_info *desc_info; |
| 1583 | struct rocker_wait *wait; |
Scott Feldman | 179f9a2 | 2015-06-12 21:35:46 -0700 | [diff] [blame] | 1584 | bool nowait = !!(flags & ROCKER_OP_FLAG_NOWAIT); |
| 1585 | unsigned long lock_flags; |
Jiri Pirko | 4b8ac96 | 2014-11-28 14:34:26 +0100 | [diff] [blame] | 1586 | int err; |
| 1587 | |
Scott Feldman | 179f9a2 | 2015-06-12 21:35:46 -0700 | [diff] [blame] | 1588 | wait = rocker_wait_create(rocker_port, trans, flags); |
Jiri Pirko | 4b8ac96 | 2014-11-28 14:34:26 +0100 | [diff] [blame] | 1589 | if (!wait) |
| 1590 | return -ENOMEM; |
Scott Feldman | 179f9a2 | 2015-06-12 21:35:46 -0700 | [diff] [blame] | 1591 | wait->nowait = nowait; |
Jiri Pirko | 4b8ac96 | 2014-11-28 14:34:26 +0100 | [diff] [blame] | 1592 | |
Scott Feldman | 179f9a2 | 2015-06-12 21:35:46 -0700 | [diff] [blame] | 1593 | spin_lock_irqsave(&rocker->cmd_ring_lock, lock_flags); |
Scott Feldman | c4f2032 | 2015-05-10 09:47:50 -0700 | [diff] [blame] | 1594 | |
Jiri Pirko | 4b8ac96 | 2014-11-28 14:34:26 +0100 | [diff] [blame] | 1595 | desc_info = rocker_desc_head_get(&rocker->cmd_ring); |
| 1596 | if (!desc_info) { |
Scott Feldman | 179f9a2 | 2015-06-12 21:35:46 -0700 | [diff] [blame] | 1597 | spin_unlock_irqrestore(&rocker->cmd_ring_lock, lock_flags); |
Jiri Pirko | 4b8ac96 | 2014-11-28 14:34:26 +0100 | [diff] [blame] | 1598 | err = -EAGAIN; |
| 1599 | goto out; |
| 1600 | } |
Scott Feldman | c4f2032 | 2015-05-10 09:47:50 -0700 | [diff] [blame] | 1601 | |
Simon Horman | 534ba6a | 2015-06-01 13:25:04 +0900 | [diff] [blame] | 1602 | err = prepare(rocker_port, desc_info, prepare_priv); |
Jiri Pirko | 4b8ac96 | 2014-11-28 14:34:26 +0100 | [diff] [blame] | 1603 | if (err) { |
Scott Feldman | 179f9a2 | 2015-06-12 21:35:46 -0700 | [diff] [blame] | 1604 | spin_unlock_irqrestore(&rocker->cmd_ring_lock, lock_flags); |
Jiri Pirko | 4b8ac96 | 2014-11-28 14:34:26 +0100 | [diff] [blame] | 1605 | goto out; |
| 1606 | } |
Scott Feldman | c4f2032 | 2015-05-10 09:47:50 -0700 | [diff] [blame] | 1607 | |
Jiri Pirko | 4b8ac96 | 2014-11-28 14:34:26 +0100 | [diff] [blame] | 1608 | rocker_desc_cookie_ptr_set(desc_info, wait); |
Scott Feldman | c4f2032 | 2015-05-10 09:47:50 -0700 | [diff] [blame] | 1609 | |
| 1610 | if (trans != SWITCHDEV_TRANS_PREPARE) |
| 1611 | rocker_desc_head_set(rocker, &rocker->cmd_ring, desc_info); |
| 1612 | |
Scott Feldman | 179f9a2 | 2015-06-12 21:35:46 -0700 | [diff] [blame] | 1613 | spin_unlock_irqrestore(&rocker->cmd_ring_lock, lock_flags); |
| 1614 | |
| 1615 | if (nowait) |
| 1616 | return 0; |
Jiri Pirko | 4b8ac96 | 2014-11-28 14:34:26 +0100 | [diff] [blame] | 1617 | |
Scott Feldman | c4f2032 | 2015-05-10 09:47:50 -0700 | [diff] [blame] | 1618 | if (trans != SWITCHDEV_TRANS_PREPARE) |
| 1619 | if (!rocker_wait_event_timeout(wait, HZ / 10)) |
| 1620 | return -EIO; |
Jiri Pirko | 4b8ac96 | 2014-11-28 14:34:26 +0100 | [diff] [blame] | 1621 | |
| 1622 | err = rocker_desc_err(desc_info); |
| 1623 | if (err) |
| 1624 | return err; |
| 1625 | |
| 1626 | if (process) |
Simon Horman | 534ba6a | 2015-06-01 13:25:04 +0900 | [diff] [blame] | 1627 | err = process(rocker_port, desc_info, process_priv); |
Jiri Pirko | 4b8ac96 | 2014-11-28 14:34:26 +0100 | [diff] [blame] | 1628 | |
| 1629 | rocker_desc_gen_clear(desc_info); |
| 1630 | out: |
Simon Horman | 0985df7 | 2015-05-25 14:28:35 +0900 | [diff] [blame] | 1631 | rocker_wait_destroy(trans, wait); |
Jiri Pirko | 4b8ac96 | 2014-11-28 14:34:26 +0100 | [diff] [blame] | 1632 | return err; |
| 1633 | } |
| 1634 | |
| 1635 | static int |
Simon Horman | 534ba6a | 2015-06-01 13:25:04 +0900 | [diff] [blame] | 1636 | rocker_cmd_get_port_settings_prep(const struct rocker_port *rocker_port, |
Jiri Pirko | 4b8ac96 | 2014-11-28 14:34:26 +0100 | [diff] [blame] | 1637 | 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 Feldman | 4a6bb6d | 2015-02-25 20:15:37 -0800 | [diff] [blame] | 1648 | if (rocker_tlv_put_u32(desc_info, ROCKER_TLV_CMD_PORT_SETTINGS_PPORT, |
| 1649 | rocker_port->pport)) |
Jiri Pirko | 4b8ac96 | 2014-11-28 14:34:26 +0100 | [diff] [blame] | 1650 | return -EMSGSIZE; |
| 1651 | rocker_tlv_nest_end(desc_info, cmd_info); |
| 1652 | return 0; |
| 1653 | } |
| 1654 | |
| 1655 | static int |
Simon Horman | 534ba6a | 2015-06-01 13:25:04 +0900 | [diff] [blame] | 1656 | rocker_cmd_get_port_settings_ethtool_proc(const struct rocker_port *rocker_port, |
Simon Horman | e505464 | 2015-05-25 14:28:36 +0900 | [diff] [blame] | 1657 | const struct rocker_desc_info *desc_info, |
Jiri Pirko | 4b8ac96 | 2014-11-28 14:34:26 +0100 | [diff] [blame] | 1658 | void *priv) |
| 1659 | { |
| 1660 | struct ethtool_cmd *ecmd = priv; |
Simon Horman | e505464 | 2015-05-25 14:28:36 +0900 | [diff] [blame] | 1661 | 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 Pirko | 4b8ac96 | 2014-11-28 14:34:26 +0100 | [diff] [blame] | 1663 | 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 | |
| 1693 | static int |
Simon Horman | 534ba6a | 2015-06-01 13:25:04 +0900 | [diff] [blame] | 1694 | rocker_cmd_get_port_settings_macaddr_proc(const struct rocker_port *rocker_port, |
Simon Horman | e505464 | 2015-05-25 14:28:36 +0900 | [diff] [blame] | 1695 | const struct rocker_desc_info *desc_info, |
Jiri Pirko | 4b8ac96 | 2014-11-28 14:34:26 +0100 | [diff] [blame] | 1696 | void *priv) |
| 1697 | { |
| 1698 | unsigned char *macaddr = priv; |
Simon Horman | e505464 | 2015-05-25 14:28:36 +0900 | [diff] [blame] | 1699 | 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 Pirko | 4b8ac96 | 2014-11-28 14:34:26 +0100 | [diff] [blame] | 1702 | |
| 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 Ahern | db19170 | 2015-03-17 20:23:16 -0600 | [diff] [blame] | 1720 | struct port_name { |
| 1721 | char *buf; |
| 1722 | size_t len; |
| 1723 | }; |
| 1724 | |
| 1725 | static int |
Simon Horman | 534ba6a | 2015-06-01 13:25:04 +0900 | [diff] [blame] | 1726 | rocker_cmd_get_port_settings_phys_name_proc(const struct rocker_port *rocker_port, |
Simon Horman | e505464 | 2015-05-25 14:28:36 +0900 | [diff] [blame] | 1727 | const struct rocker_desc_info *desc_info, |
David Ahern | db19170 | 2015-03-17 20:23:16 -0600 | [diff] [blame] | 1728 | void *priv) |
| 1729 | { |
Simon Horman | e505464 | 2015-05-25 14:28:36 +0900 | [diff] [blame] | 1730 | 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 Ahern | db19170 | 2015-03-17 20:23:16 -0600 | [diff] [blame] | 1732 | struct port_name *name = priv; |
Simon Horman | e505464 | 2015-05-25 14:28:36 +0900 | [diff] [blame] | 1733 | const struct rocker_tlv *attr; |
David Ahern | db19170 | 2015-03-17 20:23:16 -0600 | [diff] [blame] | 1734 | size_t i, j, len; |
Simon Horman | e505464 | 2015-05-25 14:28:36 +0900 | [diff] [blame] | 1735 | const char *str; |
David Ahern | db19170 | 2015-03-17 20:23:16 -0600 | [diff] [blame] | 1736 | |
| 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 Pirko | 4b8ac96 | 2014-11-28 14:34:26 +0100 | [diff] [blame] | 1766 | static int |
Simon Horman | 534ba6a | 2015-06-01 13:25:04 +0900 | [diff] [blame] | 1767 | rocker_cmd_set_port_settings_ethtool_prep(const struct rocker_port *rocker_port, |
Jiri Pirko | 4b8ac96 | 2014-11-28 14:34:26 +0100 | [diff] [blame] | 1768 | 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 Feldman | 4a6bb6d | 2015-02-25 20:15:37 -0800 | [diff] [blame] | 1780 | if (rocker_tlv_put_u32(desc_info, ROCKER_TLV_CMD_PORT_SETTINGS_PPORT, |
| 1781 | rocker_port->pport)) |
Jiri Pirko | 4b8ac96 | 2014-11-28 14:34:26 +0100 | [diff] [blame] | 1782 | 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 | |
| 1796 | static int |
Simon Horman | 534ba6a | 2015-06-01 13:25:04 +0900 | [diff] [blame] | 1797 | rocker_cmd_set_port_settings_macaddr_prep(const struct rocker_port *rocker_port, |
Jiri Pirko | 4b8ac96 | 2014-11-28 14:34:26 +0100 | [diff] [blame] | 1798 | struct rocker_desc_info *desc_info, |
| 1799 | void *priv) |
| 1800 | { |
Simon Horman | e505464 | 2015-05-25 14:28:36 +0900 | [diff] [blame] | 1801 | const unsigned char *macaddr = priv; |
Jiri Pirko | 4b8ac96 | 2014-11-28 14:34:26 +0100 | [diff] [blame] | 1802 | 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 Feldman | 4a6bb6d | 2015-02-25 20:15:37 -0800 | [diff] [blame] | 1810 | if (rocker_tlv_put_u32(desc_info, ROCKER_TLV_CMD_PORT_SETTINGS_PPORT, |
| 1811 | rocker_port->pport)) |
Jiri Pirko | 4b8ac96 | 2014-11-28 14:34:26 +0100 | [diff] [blame] | 1812 | 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 Feldman | 5111f80 | 2014-11-28 14:34:30 +0100 | [diff] [blame] | 1820 | static int |
Simon Horman | 534ba6a | 2015-06-01 13:25:04 +0900 | [diff] [blame] | 1821 | rocker_cmd_set_port_learning_prep(const struct rocker_port *rocker_port, |
Scott Feldman | 5111f80 | 2014-11-28 14:34:30 +0100 | [diff] [blame] | 1822 | 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 Feldman | 4a6bb6d | 2015-02-25 20:15:37 -0800 | [diff] [blame] | 1833 | if (rocker_tlv_put_u32(desc_info, ROCKER_TLV_CMD_PORT_SETTINGS_PPORT, |
| 1834 | rocker_port->pport)) |
Scott Feldman | 5111f80 | 2014-11-28 14:34:30 +0100 | [diff] [blame] | 1835 | 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 Pirko | 4b8ac96 | 2014-11-28 14:34:26 +0100 | [diff] [blame] | 1843 | static int rocker_cmd_get_port_settings_ethtool(struct rocker_port *rocker_port, |
| 1844 | struct ethtool_cmd *ecmd) |
| 1845 | { |
Scott Feldman | 179f9a2 | 2015-06-12 21:35:46 -0700 | [diff] [blame] | 1846 | return rocker_cmd_exec(rocker_port, SWITCHDEV_TRANS_NONE, 0, |
Jiri Pirko | 4b8ac96 | 2014-11-28 14:34:26 +0100 | [diff] [blame] | 1847 | rocker_cmd_get_port_settings_prep, NULL, |
| 1848 | rocker_cmd_get_port_settings_ethtool_proc, |
Scott Feldman | c4f2032 | 2015-05-10 09:47:50 -0700 | [diff] [blame] | 1849 | ecmd); |
Jiri Pirko | 4b8ac96 | 2014-11-28 14:34:26 +0100 | [diff] [blame] | 1850 | } |
| 1851 | |
| 1852 | static int rocker_cmd_get_port_settings_macaddr(struct rocker_port *rocker_port, |
| 1853 | unsigned char *macaddr) |
| 1854 | { |
Scott Feldman | 179f9a2 | 2015-06-12 21:35:46 -0700 | [diff] [blame] | 1855 | return rocker_cmd_exec(rocker_port, SWITCHDEV_TRANS_NONE, 0, |
Jiri Pirko | 4b8ac96 | 2014-11-28 14:34:26 +0100 | [diff] [blame] | 1856 | rocker_cmd_get_port_settings_prep, NULL, |
| 1857 | rocker_cmd_get_port_settings_macaddr_proc, |
Scott Feldman | c4f2032 | 2015-05-10 09:47:50 -0700 | [diff] [blame] | 1858 | macaddr); |
Jiri Pirko | 4b8ac96 | 2014-11-28 14:34:26 +0100 | [diff] [blame] | 1859 | } |
| 1860 | |
| 1861 | static int rocker_cmd_set_port_settings_ethtool(struct rocker_port *rocker_port, |
| 1862 | struct ethtool_cmd *ecmd) |
| 1863 | { |
Scott Feldman | 179f9a2 | 2015-06-12 21:35:46 -0700 | [diff] [blame] | 1864 | return rocker_cmd_exec(rocker_port, SWITCHDEV_TRANS_NONE, 0, |
Jiri Pirko | 4b8ac96 | 2014-11-28 14:34:26 +0100 | [diff] [blame] | 1865 | rocker_cmd_set_port_settings_ethtool_prep, |
Scott Feldman | c4f2032 | 2015-05-10 09:47:50 -0700 | [diff] [blame] | 1866 | ecmd, NULL, NULL); |
Jiri Pirko | 4b8ac96 | 2014-11-28 14:34:26 +0100 | [diff] [blame] | 1867 | } |
| 1868 | |
| 1869 | static int rocker_cmd_set_port_settings_macaddr(struct rocker_port *rocker_port, |
| 1870 | unsigned char *macaddr) |
| 1871 | { |
Scott Feldman | 179f9a2 | 2015-06-12 21:35:46 -0700 | [diff] [blame] | 1872 | return rocker_cmd_exec(rocker_port, SWITCHDEV_TRANS_NONE, 0, |
Jiri Pirko | 4b8ac96 | 2014-11-28 14:34:26 +0100 | [diff] [blame] | 1873 | rocker_cmd_set_port_settings_macaddr_prep, |
Scott Feldman | c4f2032 | 2015-05-10 09:47:50 -0700 | [diff] [blame] | 1874 | macaddr, NULL, NULL); |
Jiri Pirko | 4b8ac96 | 2014-11-28 14:34:26 +0100 | [diff] [blame] | 1875 | } |
| 1876 | |
Scott Feldman | c4f2032 | 2015-05-10 09:47:50 -0700 | [diff] [blame] | 1877 | static int rocker_port_set_learning(struct rocker_port *rocker_port, |
| 1878 | enum switchdev_trans trans) |
Scott Feldman | 5111f80 | 2014-11-28 14:34:30 +0100 | [diff] [blame] | 1879 | { |
Scott Feldman | 179f9a2 | 2015-06-12 21:35:46 -0700 | [diff] [blame] | 1880 | return rocker_cmd_exec(rocker_port, trans, 0, |
Scott Feldman | 5111f80 | 2014-11-28 14:34:30 +0100 | [diff] [blame] | 1881 | rocker_cmd_set_port_learning_prep, |
Scott Feldman | c4f2032 | 2015-05-10 09:47:50 -0700 | [diff] [blame] | 1882 | NULL, NULL, NULL); |
Scott Feldman | 5111f80 | 2014-11-28 14:34:30 +0100 | [diff] [blame] | 1883 | } |
| 1884 | |
Simon Horman | e505464 | 2015-05-25 14:28:36 +0900 | [diff] [blame] | 1885 | static int |
| 1886 | rocker_cmd_flow_tbl_add_ig_port(struct rocker_desc_info *desc_info, |
| 1887 | const struct rocker_flow_tbl_entry *entry) |
Scott Feldman | 9f6bbf7 | 2014-11-28 14:34:27 +0100 | [diff] [blame] | 1888 | { |
Scott Feldman | 4a6bb6d | 2015-02-25 20:15:37 -0800 | [diff] [blame] | 1889 | if (rocker_tlv_put_u32(desc_info, ROCKER_TLV_OF_DPA_IN_PPORT, |
| 1890 | entry->key.ig_port.in_pport)) |
Scott Feldman | 9f6bbf7 | 2014-11-28 14:34:27 +0100 | [diff] [blame] | 1891 | return -EMSGSIZE; |
Scott Feldman | 4a6bb6d | 2015-02-25 20:15:37 -0800 | [diff] [blame] | 1892 | if (rocker_tlv_put_u32(desc_info, ROCKER_TLV_OF_DPA_IN_PPORT_MASK, |
| 1893 | entry->key.ig_port.in_pport_mask)) |
Scott Feldman | 9f6bbf7 | 2014-11-28 14:34:27 +0100 | [diff] [blame] | 1894 | 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 Horman | e505464 | 2015-05-25 14:28:36 +0900 | [diff] [blame] | 1902 | static int |
| 1903 | rocker_cmd_flow_tbl_add_vlan(struct rocker_desc_info *desc_info, |
| 1904 | const struct rocker_flow_tbl_entry *entry) |
Scott Feldman | 9f6bbf7 | 2014-11-28 14:34:27 +0100 | [diff] [blame] | 1905 | { |
Scott Feldman | 4a6bb6d | 2015-02-25 20:15:37 -0800 | [diff] [blame] | 1906 | if (rocker_tlv_put_u32(desc_info, ROCKER_TLV_OF_DPA_IN_PPORT, |
| 1907 | entry->key.vlan.in_pport)) |
Scott Feldman | 9f6bbf7 | 2014-11-28 14:34:27 +0100 | [diff] [blame] | 1908 | return -EMSGSIZE; |
Jiri Pirko | 9b03c71 | 2014-12-03 14:14:53 +0100 | [diff] [blame] | 1909 | if (rocker_tlv_put_be16(desc_info, ROCKER_TLV_OF_DPA_VLAN_ID, |
| 1910 | entry->key.vlan.vlan_id)) |
Scott Feldman | 9f6bbf7 | 2014-11-28 14:34:27 +0100 | [diff] [blame] | 1911 | return -EMSGSIZE; |
Jiri Pirko | 9b03c71 | 2014-12-03 14:14:53 +0100 | [diff] [blame] | 1912 | if (rocker_tlv_put_be16(desc_info, ROCKER_TLV_OF_DPA_VLAN_ID_MASK, |
| 1913 | entry->key.vlan.vlan_id_mask)) |
Scott Feldman | 9f6bbf7 | 2014-11-28 14:34:27 +0100 | [diff] [blame] | 1914 | 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 Pirko | 9b03c71 | 2014-12-03 14:14:53 +0100 | [diff] [blame] | 1919 | rocker_tlv_put_be16(desc_info, ROCKER_TLV_OF_DPA_NEW_VLAN_ID, |
| 1920 | entry->key.vlan.new_vlan_id)) |
Scott Feldman | 9f6bbf7 | 2014-11-28 14:34:27 +0100 | [diff] [blame] | 1921 | return -EMSGSIZE; |
| 1922 | |
| 1923 | return 0; |
| 1924 | } |
| 1925 | |
Simon Horman | e505464 | 2015-05-25 14:28:36 +0900 | [diff] [blame] | 1926 | static int |
| 1927 | rocker_cmd_flow_tbl_add_term_mac(struct rocker_desc_info *desc_info, |
| 1928 | const struct rocker_flow_tbl_entry *entry) |
Scott Feldman | 9f6bbf7 | 2014-11-28 14:34:27 +0100 | [diff] [blame] | 1929 | { |
Scott Feldman | 4a6bb6d | 2015-02-25 20:15:37 -0800 | [diff] [blame] | 1930 | if (rocker_tlv_put_u32(desc_info, ROCKER_TLV_OF_DPA_IN_PPORT, |
| 1931 | entry->key.term_mac.in_pport)) |
Scott Feldman | 9f6bbf7 | 2014-11-28 14:34:27 +0100 | [diff] [blame] | 1932 | return -EMSGSIZE; |
Scott Feldman | 4a6bb6d | 2015-02-25 20:15:37 -0800 | [diff] [blame] | 1933 | if (rocker_tlv_put_u32(desc_info, ROCKER_TLV_OF_DPA_IN_PPORT_MASK, |
| 1934 | entry->key.term_mac.in_pport_mask)) |
Scott Feldman | 9f6bbf7 | 2014-11-28 14:34:27 +0100 | [diff] [blame] | 1935 | return -EMSGSIZE; |
Jiri Pirko | 9b03c71 | 2014-12-03 14:14:53 +0100 | [diff] [blame] | 1936 | if (rocker_tlv_put_be16(desc_info, ROCKER_TLV_OF_DPA_ETHERTYPE, |
| 1937 | entry->key.term_mac.eth_type)) |
Scott Feldman | 9f6bbf7 | 2014-11-28 14:34:27 +0100 | [diff] [blame] | 1938 | 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 Pirko | 9b03c71 | 2014-12-03 14:14:53 +0100 | [diff] [blame] | 1945 | if (rocker_tlv_put_be16(desc_info, ROCKER_TLV_OF_DPA_VLAN_ID, |
| 1946 | entry->key.term_mac.vlan_id)) |
Scott Feldman | 9f6bbf7 | 2014-11-28 14:34:27 +0100 | [diff] [blame] | 1947 | return -EMSGSIZE; |
Jiri Pirko | 9b03c71 | 2014-12-03 14:14:53 +0100 | [diff] [blame] | 1948 | if (rocker_tlv_put_be16(desc_info, ROCKER_TLV_OF_DPA_VLAN_ID_MASK, |
| 1949 | entry->key.term_mac.vlan_id_mask)) |
Scott Feldman | 9f6bbf7 | 2014-11-28 14:34:27 +0100 | [diff] [blame] | 1950 | 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 | |
| 1962 | static int |
| 1963 | rocker_cmd_flow_tbl_add_ucast_routing(struct rocker_desc_info *desc_info, |
Simon Horman | e505464 | 2015-05-25 14:28:36 +0900 | [diff] [blame] | 1964 | const struct rocker_flow_tbl_entry *entry) |
Scott Feldman | 9f6bbf7 | 2014-11-28 14:34:27 +0100 | [diff] [blame] | 1965 | { |
Jiri Pirko | 9b03c71 | 2014-12-03 14:14:53 +0100 | [diff] [blame] | 1966 | if (rocker_tlv_put_be16(desc_info, ROCKER_TLV_OF_DPA_ETHERTYPE, |
| 1967 | entry->key.ucast_routing.eth_type)) |
Scott Feldman | 9f6bbf7 | 2014-11-28 14:34:27 +0100 | [diff] [blame] | 1968 | return -EMSGSIZE; |
Jiri Pirko | 9b03c71 | 2014-12-03 14:14:53 +0100 | [diff] [blame] | 1969 | if (rocker_tlv_put_be32(desc_info, ROCKER_TLV_OF_DPA_DST_IP, |
| 1970 | entry->key.ucast_routing.dst4)) |
Scott Feldman | 9f6bbf7 | 2014-11-28 14:34:27 +0100 | [diff] [blame] | 1971 | return -EMSGSIZE; |
Jiri Pirko | 9b03c71 | 2014-12-03 14:14:53 +0100 | [diff] [blame] | 1972 | if (rocker_tlv_put_be32(desc_info, ROCKER_TLV_OF_DPA_DST_IP_MASK, |
| 1973 | entry->key.ucast_routing.dst4_mask)) |
Scott Feldman | 9f6bbf7 | 2014-11-28 14:34:27 +0100 | [diff] [blame] | 1974 | 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 Horman | e505464 | 2015-05-25 14:28:36 +0900 | [diff] [blame] | 1985 | static int |
| 1986 | rocker_cmd_flow_tbl_add_bridge(struct rocker_desc_info *desc_info, |
| 1987 | const struct rocker_flow_tbl_entry *entry) |
Scott Feldman | 9f6bbf7 | 2014-11-28 14:34:27 +0100 | [diff] [blame] | 1988 | { |
| 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 Pirko | 9b03c71 | 2014-12-03 14:14:53 +0100 | [diff] [blame] | 1998 | rocker_tlv_put_be16(desc_info, ROCKER_TLV_OF_DPA_VLAN_ID, |
| 1999 | entry->key.bridge.vlan_id)) |
Scott Feldman | 9f6bbf7 | 2014-11-28 14:34:27 +0100 | [diff] [blame] | 2000 | 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 Horman | e505464 | 2015-05-25 14:28:36 +0900 | [diff] [blame] | 2019 | static int |
| 2020 | rocker_cmd_flow_tbl_add_acl(struct rocker_desc_info *desc_info, |
| 2021 | const struct rocker_flow_tbl_entry *entry) |
Scott Feldman | 9f6bbf7 | 2014-11-28 14:34:27 +0100 | [diff] [blame] | 2022 | { |
Scott Feldman | 4a6bb6d | 2015-02-25 20:15:37 -0800 | [diff] [blame] | 2023 | if (rocker_tlv_put_u32(desc_info, ROCKER_TLV_OF_DPA_IN_PPORT, |
| 2024 | entry->key.acl.in_pport)) |
Scott Feldman | 9f6bbf7 | 2014-11-28 14:34:27 +0100 | [diff] [blame] | 2025 | return -EMSGSIZE; |
Scott Feldman | 4a6bb6d | 2015-02-25 20:15:37 -0800 | [diff] [blame] | 2026 | if (rocker_tlv_put_u32(desc_info, ROCKER_TLV_OF_DPA_IN_PPORT_MASK, |
| 2027 | entry->key.acl.in_pport_mask)) |
Scott Feldman | 9f6bbf7 | 2014-11-28 14:34:27 +0100 | [diff] [blame] | 2028 | 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 Pirko | 9b03c71 | 2014-12-03 14:14:53 +0100 | [diff] [blame] | 2041 | if (rocker_tlv_put_be16(desc_info, ROCKER_TLV_OF_DPA_ETHERTYPE, |
| 2042 | entry->key.acl.eth_type)) |
Scott Feldman | 9f6bbf7 | 2014-11-28 14:34:27 +0100 | [diff] [blame] | 2043 | return -EMSGSIZE; |
Jiri Pirko | 9b03c71 | 2014-12-03 14:14:53 +0100 | [diff] [blame] | 2044 | if (rocker_tlv_put_be16(desc_info, ROCKER_TLV_OF_DPA_VLAN_ID, |
| 2045 | entry->key.acl.vlan_id)) |
Scott Feldman | 9f6bbf7 | 2014-11-28 14:34:27 +0100 | [diff] [blame] | 2046 | return -EMSGSIZE; |
Jiri Pirko | 9b03c71 | 2014-12-03 14:14:53 +0100 | [diff] [blame] | 2047 | if (rocker_tlv_put_be16(desc_info, ROCKER_TLV_OF_DPA_VLAN_ID_MASK, |
| 2048 | entry->key.acl.vlan_id_mask)) |
Scott Feldman | 9f6bbf7 | 2014-11-28 14:34:27 +0100 | [diff] [blame] | 2049 | 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 Horman | 534ba6a | 2015-06-01 13:25:04 +0900 | [diff] [blame] | 2086 | static int rocker_cmd_flow_tbl_add(const struct rocker_port *rocker_port, |
Scott Feldman | 9f6bbf7 | 2014-11-28 14:34:27 +0100 | [diff] [blame] | 2087 | struct rocker_desc_info *desc_info, |
| 2088 | void *priv) |
| 2089 | { |
Simon Horman | e505464 | 2015-05-25 14:28:36 +0900 | [diff] [blame] | 2090 | const struct rocker_flow_tbl_entry *entry = priv; |
Scott Feldman | 9f6bbf7 | 2014-11-28 14:34:27 +0100 | [diff] [blame] | 2091 | struct rocker_tlv *cmd_info; |
| 2092 | int err = 0; |
| 2093 | |
Scott Feldman | c1beeef | 2015-03-05 21:21:20 -0800 | [diff] [blame] | 2094 | if (rocker_tlv_put_u16(desc_info, ROCKER_TLV_CMD_TYPE, entry->cmd)) |
Scott Feldman | 9f6bbf7 | 2014-11-28 14:34:27 +0100 | [diff] [blame] | 2095 | 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 Horman | 534ba6a | 2015-06-01 13:25:04 +0900 | [diff] [blame] | 2143 | static int rocker_cmd_flow_tbl_del(const struct rocker_port *rocker_port, |
Scott Feldman | 9f6bbf7 | 2014-11-28 14:34:27 +0100 | [diff] [blame] | 2144 | 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 Feldman | c1beeef | 2015-03-05 21:21:20 -0800 | [diff] [blame] | 2150 | if (rocker_tlv_put_u16(desc_info, ROCKER_TLV_CMD_TYPE, entry->cmd)) |
Scott Feldman | 9f6bbf7 | 2014-11-28 14:34:27 +0100 | [diff] [blame] | 2151 | 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 | |
| 2163 | static int |
| 2164 | rocker_cmd_group_tbl_add_l2_interface(struct rocker_desc_info *desc_info, |
| 2165 | struct rocker_group_tbl_entry *entry) |
| 2166 | { |
Scott Feldman | 4a6bb6d | 2015-02-25 20:15:37 -0800 | [diff] [blame] | 2167 | if (rocker_tlv_put_u32(desc_info, ROCKER_TLV_OF_DPA_OUT_PPORT, |
Scott Feldman | 9f6bbf7 | 2014-11-28 14:34:27 +0100 | [diff] [blame] | 2168 | 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 | |
| 2177 | static int |
| 2178 | rocker_cmd_group_tbl_add_l2_rewrite(struct rocker_desc_info *desc_info, |
Simon Horman | e505464 | 2015-05-25 14:28:36 +0900 | [diff] [blame] | 2179 | const struct rocker_group_tbl_entry *entry) |
Scott Feldman | 9f6bbf7 | 2014-11-28 14:34:27 +0100 | [diff] [blame] | 2180 | { |
| 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 Pirko | 9b03c71 | 2014-12-03 14:14:53 +0100 | [diff] [blame] | 2193 | rocker_tlv_put_be16(desc_info, ROCKER_TLV_OF_DPA_VLAN_ID, |
| 2194 | entry->l2_rewrite.vlan_id)) |
Scott Feldman | 9f6bbf7 | 2014-11-28 14:34:27 +0100 | [diff] [blame] | 2195 | return -EMSGSIZE; |
| 2196 | |
| 2197 | return 0; |
| 2198 | } |
| 2199 | |
| 2200 | static int |
| 2201 | rocker_cmd_group_tbl_add_group_ids(struct rocker_desc_info *desc_info, |
Simon Horman | e505464 | 2015-05-25 14:28:36 +0900 | [diff] [blame] | 2202 | const struct rocker_group_tbl_entry *entry) |
Scott Feldman | 9f6bbf7 | 2014-11-28 14:34:27 +0100 | [diff] [blame] | 2203 | { |
| 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 | |
| 2226 | static int |
| 2227 | rocker_cmd_group_tbl_add_l3_unicast(struct rocker_desc_info *desc_info, |
Simon Horman | e505464 | 2015-05-25 14:28:36 +0900 | [diff] [blame] | 2228 | const struct rocker_group_tbl_entry *entry) |
Scott Feldman | 9f6bbf7 | 2014-11-28 14:34:27 +0100 | [diff] [blame] | 2229 | { |
| 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 Pirko | 9b03c71 | 2014-12-03 14:14:53 +0100 | [diff] [blame] | 2239 | rocker_tlv_put_be16(desc_info, ROCKER_TLV_OF_DPA_VLAN_ID, |
| 2240 | entry->l3_unicast.vlan_id)) |
Scott Feldman | 9f6bbf7 | 2014-11-28 14:34:27 +0100 | [diff] [blame] | 2241 | 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 Horman | 534ba6a | 2015-06-01 13:25:04 +0900 | [diff] [blame] | 2252 | static int rocker_cmd_group_tbl_add(const struct rocker_port *rocker_port, |
Scott Feldman | 9f6bbf7 | 2014-11-28 14:34:27 +0100 | [diff] [blame] | 2253 | 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 Horman | 534ba6a | 2015-06-01 13:25:04 +0900 | [diff] [blame] | 2297 | static int rocker_cmd_group_tbl_del(const struct rocker_port *rocker_port, |
Scott Feldman | 9f6bbf7 | 2014-11-28 14:34:27 +0100 | [diff] [blame] | 2298 | 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 Feldman | c1beeef | 2015-03-05 21:21:20 -0800 | [diff] [blame] | 2317 | /*************************************************** |
| 2318 | * Flow, group, FDB, internal VLAN and neigh tables |
| 2319 | ***************************************************/ |
Scott Feldman | 9f6bbf7 | 2014-11-28 14:34:27 +0100 | [diff] [blame] | 2320 | |
| 2321 | static 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 Feldman | c1beeef | 2015-03-05 21:21:20 -0800 | [diff] [blame] | 2335 | hash_init(rocker->neigh_tbl); |
| 2336 | spin_lock_init(&rocker->neigh_tbl_lock); |
| 2337 | |
Scott Feldman | 9f6bbf7 | 2014-11-28 14:34:27 +0100 | [diff] [blame] | 2338 | return 0; |
| 2339 | } |
| 2340 | |
| 2341 | static 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 Feldman | c1beeef | 2015-03-05 21:21:20 -0800 | [diff] [blame] | 2348 | struct rocker_neigh_tbl_entry *neigh_entry; |
Scott Feldman | 9f6bbf7 | 2014-11-28 14:34:27 +0100 | [diff] [blame] | 2349 | 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 Feldman | c1beeef | 2015-03-05 21:21:20 -0800 | [diff] [blame] | 2372 | |
| 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 Feldman | 9f6bbf7 | 2014-11-28 14:34:27 +0100 | [diff] [blame] | 2377 | } |
| 2378 | |
| 2379 | static struct rocker_flow_tbl_entry * |
Simon Horman | e505464 | 2015-05-25 14:28:36 +0900 | [diff] [blame] | 2380 | rocker_flow_tbl_find(const struct rocker *rocker, |
| 2381 | const struct rocker_flow_tbl_entry *match) |
Scott Feldman | 9f6bbf7 | 2014-11-28 14:34:27 +0100 | [diff] [blame] | 2382 | { |
| 2383 | struct rocker_flow_tbl_entry *found; |
Scott Feldman | c1beeef | 2015-03-05 21:21:20 -0800 | [diff] [blame] | 2384 | size_t key_len = match->key_len ? match->key_len : sizeof(found->key); |
Scott Feldman | 9f6bbf7 | 2014-11-28 14:34:27 +0100 | [diff] [blame] | 2385 | |
| 2386 | hash_for_each_possible(rocker->flow_tbl, found, |
| 2387 | entry, match->key_crc32) { |
Scott Feldman | c1beeef | 2015-03-05 21:21:20 -0800 | [diff] [blame] | 2388 | if (memcmp(&found->key, &match->key, key_len) == 0) |
Scott Feldman | 9f6bbf7 | 2014-11-28 14:34:27 +0100 | [diff] [blame] | 2389 | return found; |
| 2390 | } |
| 2391 | |
| 2392 | return NULL; |
| 2393 | } |
| 2394 | |
| 2395 | static int rocker_flow_tbl_add(struct rocker_port *rocker_port, |
Scott Feldman | 179f9a2 | 2015-06-12 21:35:46 -0700 | [diff] [blame] | 2396 | enum switchdev_trans trans, int flags, |
Scott Feldman | c4f2032 | 2015-05-10 09:47:50 -0700 | [diff] [blame] | 2397 | struct rocker_flow_tbl_entry *match) |
Scott Feldman | 9f6bbf7 | 2014-11-28 14:34:27 +0100 | [diff] [blame] | 2398 | { |
| 2399 | struct rocker *rocker = rocker_port->rocker; |
| 2400 | struct rocker_flow_tbl_entry *found; |
Scott Feldman | c1beeef | 2015-03-05 21:21:20 -0800 | [diff] [blame] | 2401 | size_t key_len = match->key_len ? match->key_len : sizeof(found->key); |
Scott Feldman | 179f9a2 | 2015-06-12 21:35:46 -0700 | [diff] [blame] | 2402 | unsigned long lock_flags; |
Scott Feldman | 9f6bbf7 | 2014-11-28 14:34:27 +0100 | [diff] [blame] | 2403 | |
Scott Feldman | c1beeef | 2015-03-05 21:21:20 -0800 | [diff] [blame] | 2404 | match->key_crc32 = crc32(~0, &match->key, key_len); |
Scott Feldman | 9f6bbf7 | 2014-11-28 14:34:27 +0100 | [diff] [blame] | 2405 | |
Scott Feldman | 179f9a2 | 2015-06-12 21:35:46 -0700 | [diff] [blame] | 2406 | spin_lock_irqsave(&rocker->flow_tbl_lock, lock_flags); |
Scott Feldman | 9f6bbf7 | 2014-11-28 14:34:27 +0100 | [diff] [blame] | 2407 | |
| 2408 | found = rocker_flow_tbl_find(rocker, match); |
| 2409 | |
| 2410 | if (found) { |
Scott Feldman | c1beeef | 2015-03-05 21:21:20 -0800 | [diff] [blame] | 2411 | match->cookie = found->cookie; |
Scott Feldman | c4f2032 | 2015-05-10 09:47:50 -0700 | [diff] [blame] | 2412 | if (trans != SWITCHDEV_TRANS_PREPARE) |
| 2413 | hash_del(&found->entry); |
Simon Horman | 0985df7 | 2015-05-25 14:28:35 +0900 | [diff] [blame] | 2414 | rocker_port_kfree(trans, found); |
Scott Feldman | c1beeef | 2015-03-05 21:21:20 -0800 | [diff] [blame] | 2415 | found = match; |
| 2416 | found->cmd = ROCKER_TLV_CMD_TYPE_OF_DPA_FLOW_MOD; |
Scott Feldman | 9f6bbf7 | 2014-11-28 14:34:27 +0100 | [diff] [blame] | 2417 | } else { |
| 2418 | found = match; |
| 2419 | found->cookie = rocker->flow_tbl_next_cookie++; |
Scott Feldman | c1beeef | 2015-03-05 21:21:20 -0800 | [diff] [blame] | 2420 | found->cmd = ROCKER_TLV_CMD_TYPE_OF_DPA_FLOW_ADD; |
Scott Feldman | 9f6bbf7 | 2014-11-28 14:34:27 +0100 | [diff] [blame] | 2421 | } |
| 2422 | |
Scott Feldman | c4f2032 | 2015-05-10 09:47:50 -0700 | [diff] [blame] | 2423 | if (trans != SWITCHDEV_TRANS_PREPARE) |
| 2424 | hash_add(rocker->flow_tbl, &found->entry, found->key_crc32); |
Scott Feldman | 9f6bbf7 | 2014-11-28 14:34:27 +0100 | [diff] [blame] | 2425 | |
Scott Feldman | 179f9a2 | 2015-06-12 21:35:46 -0700 | [diff] [blame] | 2426 | spin_unlock_irqrestore(&rocker->flow_tbl_lock, lock_flags); |
Scott Feldman | 9f6bbf7 | 2014-11-28 14:34:27 +0100 | [diff] [blame] | 2427 | |
Scott Feldman | 179f9a2 | 2015-06-12 21:35:46 -0700 | [diff] [blame] | 2428 | return rocker_cmd_exec(rocker_port, trans, flags, |
| 2429 | rocker_cmd_flow_tbl_add, found, NULL, NULL); |
Scott Feldman | 9f6bbf7 | 2014-11-28 14:34:27 +0100 | [diff] [blame] | 2430 | } |
| 2431 | |
| 2432 | static int rocker_flow_tbl_del(struct rocker_port *rocker_port, |
Scott Feldman | 179f9a2 | 2015-06-12 21:35:46 -0700 | [diff] [blame] | 2433 | enum switchdev_trans trans, int flags, |
Scott Feldman | c4f2032 | 2015-05-10 09:47:50 -0700 | [diff] [blame] | 2434 | struct rocker_flow_tbl_entry *match) |
Scott Feldman | 9f6bbf7 | 2014-11-28 14:34:27 +0100 | [diff] [blame] | 2435 | { |
| 2436 | struct rocker *rocker = rocker_port->rocker; |
| 2437 | struct rocker_flow_tbl_entry *found; |
Scott Feldman | c1beeef | 2015-03-05 21:21:20 -0800 | [diff] [blame] | 2438 | size_t key_len = match->key_len ? match->key_len : sizeof(found->key); |
Scott Feldman | 179f9a2 | 2015-06-12 21:35:46 -0700 | [diff] [blame] | 2439 | unsigned long lock_flags; |
Scott Feldman | 9f6bbf7 | 2014-11-28 14:34:27 +0100 | [diff] [blame] | 2440 | int err = 0; |
| 2441 | |
Scott Feldman | c1beeef | 2015-03-05 21:21:20 -0800 | [diff] [blame] | 2442 | match->key_crc32 = crc32(~0, &match->key, key_len); |
Scott Feldman | 9f6bbf7 | 2014-11-28 14:34:27 +0100 | [diff] [blame] | 2443 | |
Scott Feldman | 179f9a2 | 2015-06-12 21:35:46 -0700 | [diff] [blame] | 2444 | spin_lock_irqsave(&rocker->flow_tbl_lock, lock_flags); |
Scott Feldman | 9f6bbf7 | 2014-11-28 14:34:27 +0100 | [diff] [blame] | 2445 | |
| 2446 | found = rocker_flow_tbl_find(rocker, match); |
| 2447 | |
| 2448 | if (found) { |
Scott Feldman | c4f2032 | 2015-05-10 09:47:50 -0700 | [diff] [blame] | 2449 | if (trans != SWITCHDEV_TRANS_PREPARE) |
| 2450 | hash_del(&found->entry); |
Scott Feldman | c1beeef | 2015-03-05 21:21:20 -0800 | [diff] [blame] | 2451 | found->cmd = ROCKER_TLV_CMD_TYPE_OF_DPA_FLOW_DEL; |
Scott Feldman | 9f6bbf7 | 2014-11-28 14:34:27 +0100 | [diff] [blame] | 2452 | } |
| 2453 | |
Scott Feldman | 179f9a2 | 2015-06-12 21:35:46 -0700 | [diff] [blame] | 2454 | spin_unlock_irqrestore(&rocker->flow_tbl_lock, lock_flags); |
Scott Feldman | 9f6bbf7 | 2014-11-28 14:34:27 +0100 | [diff] [blame] | 2455 | |
Simon Horman | 0985df7 | 2015-05-25 14:28:35 +0900 | [diff] [blame] | 2456 | rocker_port_kfree(trans, match); |
Scott Feldman | 9f6bbf7 | 2014-11-28 14:34:27 +0100 | [diff] [blame] | 2457 | |
Scott Feldman | c1beeef | 2015-03-05 21:21:20 -0800 | [diff] [blame] | 2458 | if (found) { |
Scott Feldman | 179f9a2 | 2015-06-12 21:35:46 -0700 | [diff] [blame] | 2459 | err = rocker_cmd_exec(rocker_port, trans, flags, |
Scott Feldman | 9f6bbf7 | 2014-11-28 14:34:27 +0100 | [diff] [blame] | 2460 | rocker_cmd_flow_tbl_del, |
Scott Feldman | c4f2032 | 2015-05-10 09:47:50 -0700 | [diff] [blame] | 2461 | found, NULL, NULL); |
Simon Horman | 0985df7 | 2015-05-25 14:28:35 +0900 | [diff] [blame] | 2462 | rocker_port_kfree(trans, found); |
Scott Feldman | 9f6bbf7 | 2014-11-28 14:34:27 +0100 | [diff] [blame] | 2463 | } |
| 2464 | |
| 2465 | return err; |
| 2466 | } |
| 2467 | |
Scott Feldman | 9f6bbf7 | 2014-11-28 14:34:27 +0100 | [diff] [blame] | 2468 | static int rocker_flow_tbl_do(struct rocker_port *rocker_port, |
Scott Feldman | c4f2032 | 2015-05-10 09:47:50 -0700 | [diff] [blame] | 2469 | enum switchdev_trans trans, int flags, |
| 2470 | struct rocker_flow_tbl_entry *entry) |
Scott Feldman | 9f6bbf7 | 2014-11-28 14:34:27 +0100 | [diff] [blame] | 2471 | { |
Scott Feldman | 9f6bbf7 | 2014-11-28 14:34:27 +0100 | [diff] [blame] | 2472 | if (flags & ROCKER_OP_FLAG_REMOVE) |
Scott Feldman | 179f9a2 | 2015-06-12 21:35:46 -0700 | [diff] [blame] | 2473 | return rocker_flow_tbl_del(rocker_port, trans, flags, entry); |
Scott Feldman | 9f6bbf7 | 2014-11-28 14:34:27 +0100 | [diff] [blame] | 2474 | else |
Scott Feldman | 179f9a2 | 2015-06-12 21:35:46 -0700 | [diff] [blame] | 2475 | return rocker_flow_tbl_add(rocker_port, trans, flags, entry); |
Scott Feldman | 9f6bbf7 | 2014-11-28 14:34:27 +0100 | [diff] [blame] | 2476 | } |
| 2477 | |
| 2478 | static int rocker_flow_tbl_ig_port(struct rocker_port *rocker_port, |
Scott Feldman | c4f2032 | 2015-05-10 09:47:50 -0700 | [diff] [blame] | 2479 | enum switchdev_trans trans, int flags, |
| 2480 | u32 in_pport, u32 in_pport_mask, |
Scott Feldman | 9f6bbf7 | 2014-11-28 14:34:27 +0100 | [diff] [blame] | 2481 | enum rocker_of_dpa_table_id goto_tbl) |
| 2482 | { |
| 2483 | struct rocker_flow_tbl_entry *entry; |
| 2484 | |
Scott Feldman | 179f9a2 | 2015-06-12 21:35:46 -0700 | [diff] [blame] | 2485 | entry = rocker_port_kzalloc(rocker_port, trans, flags, sizeof(*entry)); |
Scott Feldman | 9f6bbf7 | 2014-11-28 14:34:27 +0100 | [diff] [blame] | 2486 | 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 Feldman | 4a6bb6d | 2015-02-25 20:15:37 -0800 | [diff] [blame] | 2491 | entry->key.ig_port.in_pport = in_pport; |
| 2492 | entry->key.ig_port.in_pport_mask = in_pport_mask; |
Scott Feldman | 9f6bbf7 | 2014-11-28 14:34:27 +0100 | [diff] [blame] | 2493 | entry->key.ig_port.goto_tbl = goto_tbl; |
| 2494 | |
Scott Feldman | c4f2032 | 2015-05-10 09:47:50 -0700 | [diff] [blame] | 2495 | return rocker_flow_tbl_do(rocker_port, trans, flags, entry); |
Scott Feldman | 9f6bbf7 | 2014-11-28 14:34:27 +0100 | [diff] [blame] | 2496 | } |
| 2497 | |
| 2498 | static int rocker_flow_tbl_vlan(struct rocker_port *rocker_port, |
Scott Feldman | c4f2032 | 2015-05-10 09:47:50 -0700 | [diff] [blame] | 2499 | enum switchdev_trans trans, int flags, |
| 2500 | u32 in_pport, __be16 vlan_id, |
| 2501 | __be16 vlan_id_mask, |
Scott Feldman | 9f6bbf7 | 2014-11-28 14:34:27 +0100 | [diff] [blame] | 2502 | 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 Feldman | 179f9a2 | 2015-06-12 21:35:46 -0700 | [diff] [blame] | 2507 | entry = rocker_port_kzalloc(rocker_port, trans, flags, sizeof(*entry)); |
Scott Feldman | 9f6bbf7 | 2014-11-28 14:34:27 +0100 | [diff] [blame] | 2508 | 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 Feldman | 4a6bb6d | 2015-02-25 20:15:37 -0800 | [diff] [blame] | 2513 | entry->key.vlan.in_pport = in_pport; |
Scott Feldman | 9f6bbf7 | 2014-11-28 14:34:27 +0100 | [diff] [blame] | 2514 | 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 Feldman | c4f2032 | 2015-05-10 09:47:50 -0700 | [diff] [blame] | 2521 | return rocker_flow_tbl_do(rocker_port, trans, flags, entry); |
Scott Feldman | 9f6bbf7 | 2014-11-28 14:34:27 +0100 | [diff] [blame] | 2522 | } |
| 2523 | |
| 2524 | static int rocker_flow_tbl_term_mac(struct rocker_port *rocker_port, |
Scott Feldman | c4f2032 | 2015-05-10 09:47:50 -0700 | [diff] [blame] | 2525 | enum switchdev_trans trans, |
Scott Feldman | 4a6bb6d | 2015-02-25 20:15:37 -0800 | [diff] [blame] | 2526 | u32 in_pport, u32 in_pport_mask, |
Scott Feldman | 9f6bbf7 | 2014-11-28 14:34:27 +0100 | [diff] [blame] | 2527 | __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 Feldman | 179f9a2 | 2015-06-12 21:35:46 -0700 | [diff] [blame] | 2534 | entry = rocker_port_kzalloc(rocker_port, trans, flags, sizeof(*entry)); |
Scott Feldman | 9f6bbf7 | 2014-11-28 14:34:27 +0100 | [diff] [blame] | 2535 | 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 Feldman | 4a6bb6d | 2015-02-25 20:15:37 -0800 | [diff] [blame] | 2549 | entry->key.term_mac.in_pport = in_pport; |
| 2550 | entry->key.term_mac.in_pport_mask = in_pport_mask; |
Scott Feldman | 9f6bbf7 | 2014-11-28 14:34:27 +0100 | [diff] [blame] | 2551 | 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 Feldman | c4f2032 | 2015-05-10 09:47:50 -0700 | [diff] [blame] | 2558 | return rocker_flow_tbl_do(rocker_port, trans, flags, entry); |
Scott Feldman | 9f6bbf7 | 2014-11-28 14:34:27 +0100 | [diff] [blame] | 2559 | } |
| 2560 | |
| 2561 | static int rocker_flow_tbl_bridge(struct rocker_port *rocker_port, |
Scott Feldman | c4f2032 | 2015-05-10 09:47:50 -0700 | [diff] [blame] | 2562 | enum switchdev_trans trans, int flags, |
Scott Feldman | 9f6bbf7 | 2014-11-28 14:34:27 +0100 | [diff] [blame] | 2563 | 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 Feldman | 179f9a2 | 2015-06-12 21:35:46 -0700 | [diff] [blame] | 2574 | entry = rocker_port_kzalloc(rocker_port, trans, flags, sizeof(*entry)); |
Scott Feldman | 9f6bbf7 | 2014-11-28 14:34:27 +0100 | [diff] [blame] | 2575 | 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 Horman | 629161f | 2015-04-30 15:21:29 +0900 | [diff] [blame] | 2587 | if (!ether_addr_equal(eth_dst_mask, ff_mac)) |
Scott Feldman | 9f6bbf7 | 2014-11-28 14:34:27 +0100 | [diff] [blame] | 2588 | wild = true; |
| 2589 | } |
| 2590 | |
| 2591 | priority = ROCKER_PRIORITY_UNKNOWN; |
Thomas Graf | 51ace88 | 2014-11-28 14:34:32 +0100 | [diff] [blame] | 2592 | if (vlan_bridging && dflt && wild) |
Scott Feldman | 9f6bbf7 | 2014-11-28 14:34:27 +0100 | [diff] [blame] | 2593 | priority = ROCKER_PRIORITY_BRIDGING_VLAN_DFLT_WILD; |
Thomas Graf | 51ace88 | 2014-11-28 14:34:32 +0100 | [diff] [blame] | 2594 | else if (vlan_bridging && dflt && !wild) |
Scott Feldman | 9f6bbf7 | 2014-11-28 14:34:27 +0100 | [diff] [blame] | 2595 | priority = ROCKER_PRIORITY_BRIDGING_VLAN_DFLT_EXACT; |
Thomas Graf | 51ace88 | 2014-11-28 14:34:32 +0100 | [diff] [blame] | 2596 | else if (vlan_bridging && !dflt) |
Scott Feldman | 9f6bbf7 | 2014-11-28 14:34:27 +0100 | [diff] [blame] | 2597 | priority = ROCKER_PRIORITY_BRIDGING_VLAN; |
Thomas Graf | 51ace88 | 2014-11-28 14:34:32 +0100 | [diff] [blame] | 2598 | else if (!vlan_bridging && dflt && wild) |
Scott Feldman | 9f6bbf7 | 2014-11-28 14:34:27 +0100 | [diff] [blame] | 2599 | priority = ROCKER_PRIORITY_BRIDGING_TENANT_DFLT_WILD; |
Thomas Graf | 51ace88 | 2014-11-28 14:34:32 +0100 | [diff] [blame] | 2600 | else if (!vlan_bridging && dflt && !wild) |
Scott Feldman | 9f6bbf7 | 2014-11-28 14:34:27 +0100 | [diff] [blame] | 2601 | priority = ROCKER_PRIORITY_BRIDGING_TENANT_DFLT_EXACT; |
Thomas Graf | 51ace88 | 2014-11-28 14:34:32 +0100 | [diff] [blame] | 2602 | else if (!vlan_bridging && !dflt) |
Scott Feldman | 9f6bbf7 | 2014-11-28 14:34:27 +0100 | [diff] [blame] | 2603 | 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 Feldman | c4f2032 | 2015-05-10 09:47:50 -0700 | [diff] [blame] | 2612 | return rocker_flow_tbl_do(rocker_port, trans, flags, entry); |
Scott Feldman | 9f6bbf7 | 2014-11-28 14:34:27 +0100 | [diff] [blame] | 2613 | } |
| 2614 | |
Scott Feldman | c1beeef | 2015-03-05 21:21:20 -0800 | [diff] [blame] | 2615 | static int rocker_flow_tbl_ucast4_routing(struct rocker_port *rocker_port, |
Scott Feldman | c4f2032 | 2015-05-10 09:47:50 -0700 | [diff] [blame] | 2616 | enum switchdev_trans trans, |
Scott Feldman | c1beeef | 2015-03-05 21:21:20 -0800 | [diff] [blame] | 2617 | __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 Feldman | 179f9a2 | 2015-06-12 21:35:46 -0700 | [diff] [blame] | 2624 | entry = rocker_port_kzalloc(rocker_port, trans, flags, sizeof(*entry)); |
Scott Feldman | c1beeef | 2015-03-05 21:21:20 -0800 | [diff] [blame] | 2625 | 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 Feldman | c4f2032 | 2015-05-10 09:47:50 -0700 | [diff] [blame] | 2638 | return rocker_flow_tbl_do(rocker_port, trans, flags, entry); |
Scott Feldman | c1beeef | 2015-03-05 21:21:20 -0800 | [diff] [blame] | 2639 | } |
| 2640 | |
Scott Feldman | 9f6bbf7 | 2014-11-28 14:34:27 +0100 | [diff] [blame] | 2641 | static int rocker_flow_tbl_acl(struct rocker_port *rocker_port, |
Scott Feldman | c4f2032 | 2015-05-10 09:47:50 -0700 | [diff] [blame] | 2642 | enum switchdev_trans trans, int flags, |
| 2643 | u32 in_pport, u32 in_pport_mask, |
Scott Feldman | 9f6bbf7 | 2014-11-28 14:34:27 +0100 | [diff] [blame] | 2644 | const u8 *eth_src, const u8 *eth_src_mask, |
| 2645 | const u8 *eth_dst, const u8 *eth_dst_mask, |
Scott Feldman | c4f2032 | 2015-05-10 09:47:50 -0700 | [diff] [blame] | 2646 | __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 Feldman | 9f6bbf7 | 2014-11-28 14:34:27 +0100 | [diff] [blame] | 2649 | u32 group_id) |
| 2650 | { |
| 2651 | u32 priority; |
| 2652 | struct rocker_flow_tbl_entry *entry; |
| 2653 | |
Scott Feldman | 179f9a2 | 2015-06-12 21:35:46 -0700 | [diff] [blame] | 2654 | entry = rocker_port_kzalloc(rocker_port, trans, flags, sizeof(*entry)); |
Scott Feldman | 9f6bbf7 | 2014-11-28 14:34:27 +0100 | [diff] [blame] | 2655 | if (!entry) |
| 2656 | return -ENOMEM; |
| 2657 | |
| 2658 | priority = ROCKER_PRIORITY_ACL_NORMAL; |
| 2659 | if (eth_dst && eth_dst_mask) { |
Simon Horman | 629161f | 2015-04-30 15:21:29 +0900 | [diff] [blame] | 2660 | if (ether_addr_equal(eth_dst_mask, mcast_mac)) |
Scott Feldman | 9f6bbf7 | 2014-11-28 14:34:27 +0100 | [diff] [blame] | 2661 | 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 Feldman | 4a6bb6d | 2015-02-25 20:15:37 -0800 | [diff] [blame] | 2668 | entry->key.acl.in_pport = in_pport; |
| 2669 | entry->key.acl.in_pport_mask = in_pport_mask; |
Scott Feldman | 9f6bbf7 | 2014-11-28 14:34:27 +0100 | [diff] [blame] | 2670 | |
| 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 Feldman | c4f2032 | 2015-05-10 09:47:50 -0700 | [diff] [blame] | 2689 | return rocker_flow_tbl_do(rocker_port, trans, flags, entry); |
Scott Feldman | 9f6bbf7 | 2014-11-28 14:34:27 +0100 | [diff] [blame] | 2690 | } |
| 2691 | |
| 2692 | static struct rocker_group_tbl_entry * |
Simon Horman | e505464 | 2015-05-25 14:28:36 +0900 | [diff] [blame] | 2693 | rocker_group_tbl_find(const struct rocker *rocker, |
| 2694 | const struct rocker_group_tbl_entry *match) |
Scott Feldman | 9f6bbf7 | 2014-11-28 14:34:27 +0100 | [diff] [blame] | 2695 | { |
| 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 Horman | 0985df7 | 2015-05-25 14:28:35 +0900 | [diff] [blame] | 2707 | static void rocker_group_tbl_entry_free(enum switchdev_trans trans, |
Scott Feldman | c4f2032 | 2015-05-10 09:47:50 -0700 | [diff] [blame] | 2708 | struct rocker_group_tbl_entry *entry) |
Scott Feldman | 9f6bbf7 | 2014-11-28 14:34:27 +0100 | [diff] [blame] | 2709 | { |
| 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 Horman | 0985df7 | 2015-05-25 14:28:35 +0900 | [diff] [blame] | 2713 | rocker_port_kfree(trans, entry->group_ids); |
Scott Feldman | 9f6bbf7 | 2014-11-28 14:34:27 +0100 | [diff] [blame] | 2714 | break; |
| 2715 | default: |
| 2716 | break; |
| 2717 | } |
Simon Horman | 0985df7 | 2015-05-25 14:28:35 +0900 | [diff] [blame] | 2718 | rocker_port_kfree(trans, entry); |
Scott Feldman | 9f6bbf7 | 2014-11-28 14:34:27 +0100 | [diff] [blame] | 2719 | } |
| 2720 | |
| 2721 | static int rocker_group_tbl_add(struct rocker_port *rocker_port, |
Scott Feldman | 179f9a2 | 2015-06-12 21:35:46 -0700 | [diff] [blame] | 2722 | enum switchdev_trans trans, int flags, |
Scott Feldman | c4f2032 | 2015-05-10 09:47:50 -0700 | [diff] [blame] | 2723 | struct rocker_group_tbl_entry *match) |
Scott Feldman | 9f6bbf7 | 2014-11-28 14:34:27 +0100 | [diff] [blame] | 2724 | { |
| 2725 | struct rocker *rocker = rocker_port->rocker; |
| 2726 | struct rocker_group_tbl_entry *found; |
Scott Feldman | 179f9a2 | 2015-06-12 21:35:46 -0700 | [diff] [blame] | 2727 | unsigned long lock_flags; |
Scott Feldman | 9f6bbf7 | 2014-11-28 14:34:27 +0100 | [diff] [blame] | 2728 | |
Scott Feldman | 179f9a2 | 2015-06-12 21:35:46 -0700 | [diff] [blame] | 2729 | spin_lock_irqsave(&rocker->group_tbl_lock, lock_flags); |
Scott Feldman | 9f6bbf7 | 2014-11-28 14:34:27 +0100 | [diff] [blame] | 2730 | |
| 2731 | found = rocker_group_tbl_find(rocker, match); |
| 2732 | |
| 2733 | if (found) { |
Scott Feldman | c4f2032 | 2015-05-10 09:47:50 -0700 | [diff] [blame] | 2734 | if (trans != SWITCHDEV_TRANS_PREPARE) |
| 2735 | hash_del(&found->entry); |
Simon Horman | 0985df7 | 2015-05-25 14:28:35 +0900 | [diff] [blame] | 2736 | rocker_group_tbl_entry_free(trans, found); |
Scott Feldman | 9f6bbf7 | 2014-11-28 14:34:27 +0100 | [diff] [blame] | 2737 | 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 Feldman | c4f2032 | 2015-05-10 09:47:50 -0700 | [diff] [blame] | 2744 | if (trans != SWITCHDEV_TRANS_PREPARE) |
| 2745 | hash_add(rocker->group_tbl, &found->entry, found->group_id); |
Scott Feldman | 9f6bbf7 | 2014-11-28 14:34:27 +0100 | [diff] [blame] | 2746 | |
Scott Feldman | 179f9a2 | 2015-06-12 21:35:46 -0700 | [diff] [blame] | 2747 | spin_unlock_irqrestore(&rocker->group_tbl_lock, lock_flags); |
Scott Feldman | 9f6bbf7 | 2014-11-28 14:34:27 +0100 | [diff] [blame] | 2748 | |
Scott Feldman | 179f9a2 | 2015-06-12 21:35:46 -0700 | [diff] [blame] | 2749 | return rocker_cmd_exec(rocker_port, trans, flags, |
| 2750 | rocker_cmd_group_tbl_add, found, NULL, NULL); |
Scott Feldman | 9f6bbf7 | 2014-11-28 14:34:27 +0100 | [diff] [blame] | 2751 | } |
| 2752 | |
| 2753 | static int rocker_group_tbl_del(struct rocker_port *rocker_port, |
Scott Feldman | 179f9a2 | 2015-06-12 21:35:46 -0700 | [diff] [blame] | 2754 | enum switchdev_trans trans, int flags, |
Scott Feldman | c4f2032 | 2015-05-10 09:47:50 -0700 | [diff] [blame] | 2755 | struct rocker_group_tbl_entry *match) |
Scott Feldman | 9f6bbf7 | 2014-11-28 14:34:27 +0100 | [diff] [blame] | 2756 | { |
| 2757 | struct rocker *rocker = rocker_port->rocker; |
| 2758 | struct rocker_group_tbl_entry *found; |
Scott Feldman | 179f9a2 | 2015-06-12 21:35:46 -0700 | [diff] [blame] | 2759 | unsigned long lock_flags; |
Scott Feldman | 9f6bbf7 | 2014-11-28 14:34:27 +0100 | [diff] [blame] | 2760 | int err = 0; |
| 2761 | |
Scott Feldman | 179f9a2 | 2015-06-12 21:35:46 -0700 | [diff] [blame] | 2762 | spin_lock_irqsave(&rocker->group_tbl_lock, lock_flags); |
Scott Feldman | 9f6bbf7 | 2014-11-28 14:34:27 +0100 | [diff] [blame] | 2763 | |
| 2764 | found = rocker_group_tbl_find(rocker, match); |
| 2765 | |
| 2766 | if (found) { |
Scott Feldman | c4f2032 | 2015-05-10 09:47:50 -0700 | [diff] [blame] | 2767 | if (trans != SWITCHDEV_TRANS_PREPARE) |
| 2768 | hash_del(&found->entry); |
Scott Feldman | 9f6bbf7 | 2014-11-28 14:34:27 +0100 | [diff] [blame] | 2769 | found->cmd = ROCKER_TLV_CMD_TYPE_OF_DPA_GROUP_DEL; |
| 2770 | } |
| 2771 | |
Scott Feldman | 179f9a2 | 2015-06-12 21:35:46 -0700 | [diff] [blame] | 2772 | spin_unlock_irqrestore(&rocker->group_tbl_lock, lock_flags); |
Scott Feldman | 9f6bbf7 | 2014-11-28 14:34:27 +0100 | [diff] [blame] | 2773 | |
Simon Horman | 0985df7 | 2015-05-25 14:28:35 +0900 | [diff] [blame] | 2774 | rocker_group_tbl_entry_free(trans, match); |
Scott Feldman | 9f6bbf7 | 2014-11-28 14:34:27 +0100 | [diff] [blame] | 2775 | |
| 2776 | if (found) { |
Scott Feldman | 179f9a2 | 2015-06-12 21:35:46 -0700 | [diff] [blame] | 2777 | err = rocker_cmd_exec(rocker_port, trans, flags, |
Scott Feldman | 9f6bbf7 | 2014-11-28 14:34:27 +0100 | [diff] [blame] | 2778 | rocker_cmd_group_tbl_del, |
Scott Feldman | c4f2032 | 2015-05-10 09:47:50 -0700 | [diff] [blame] | 2779 | found, NULL, NULL); |
Simon Horman | 0985df7 | 2015-05-25 14:28:35 +0900 | [diff] [blame] | 2780 | rocker_group_tbl_entry_free(trans, found); |
Scott Feldman | 9f6bbf7 | 2014-11-28 14:34:27 +0100 | [diff] [blame] | 2781 | } |
| 2782 | |
| 2783 | return err; |
| 2784 | } |
| 2785 | |
| 2786 | static int rocker_group_tbl_do(struct rocker_port *rocker_port, |
Scott Feldman | c4f2032 | 2015-05-10 09:47:50 -0700 | [diff] [blame] | 2787 | enum switchdev_trans trans, int flags, |
| 2788 | struct rocker_group_tbl_entry *entry) |
Scott Feldman | 9f6bbf7 | 2014-11-28 14:34:27 +0100 | [diff] [blame] | 2789 | { |
Scott Feldman | 9f6bbf7 | 2014-11-28 14:34:27 +0100 | [diff] [blame] | 2790 | if (flags & ROCKER_OP_FLAG_REMOVE) |
Scott Feldman | 179f9a2 | 2015-06-12 21:35:46 -0700 | [diff] [blame] | 2791 | return rocker_group_tbl_del(rocker_port, trans, flags, entry); |
Scott Feldman | 9f6bbf7 | 2014-11-28 14:34:27 +0100 | [diff] [blame] | 2792 | else |
Scott Feldman | 179f9a2 | 2015-06-12 21:35:46 -0700 | [diff] [blame] | 2793 | return rocker_group_tbl_add(rocker_port, trans, flags, entry); |
Scott Feldman | 9f6bbf7 | 2014-11-28 14:34:27 +0100 | [diff] [blame] | 2794 | } |
| 2795 | |
| 2796 | static int rocker_group_l2_interface(struct rocker_port *rocker_port, |
Scott Feldman | c4f2032 | 2015-05-10 09:47:50 -0700 | [diff] [blame] | 2797 | enum switchdev_trans trans, int flags, |
| 2798 | __be16 vlan_id, u32 out_pport, |
| 2799 | int pop_vlan) |
Scott Feldman | 9f6bbf7 | 2014-11-28 14:34:27 +0100 | [diff] [blame] | 2800 | { |
| 2801 | struct rocker_group_tbl_entry *entry; |
| 2802 | |
Scott Feldman | 179f9a2 | 2015-06-12 21:35:46 -0700 | [diff] [blame] | 2803 | entry = rocker_port_kzalloc(rocker_port, trans, flags, sizeof(*entry)); |
Scott Feldman | 9f6bbf7 | 2014-11-28 14:34:27 +0100 | [diff] [blame] | 2804 | if (!entry) |
| 2805 | return -ENOMEM; |
| 2806 | |
Scott Feldman | 4a6bb6d | 2015-02-25 20:15:37 -0800 | [diff] [blame] | 2807 | entry->group_id = ROCKER_GROUP_L2_INTERFACE(vlan_id, out_pport); |
Scott Feldman | 9f6bbf7 | 2014-11-28 14:34:27 +0100 | [diff] [blame] | 2808 | entry->l2_interface.pop_vlan = pop_vlan; |
| 2809 | |
Scott Feldman | c4f2032 | 2015-05-10 09:47:50 -0700 | [diff] [blame] | 2810 | return rocker_group_tbl_do(rocker_port, trans, flags, entry); |
Scott Feldman | 9f6bbf7 | 2014-11-28 14:34:27 +0100 | [diff] [blame] | 2811 | } |
| 2812 | |
| 2813 | static int rocker_group_l2_fan_out(struct rocker_port *rocker_port, |
Scott Feldman | c4f2032 | 2015-05-10 09:47:50 -0700 | [diff] [blame] | 2814 | enum switchdev_trans trans, |
Scott Feldman | 9f6bbf7 | 2014-11-28 14:34:27 +0100 | [diff] [blame] | 2815 | int flags, u8 group_count, |
Simon Horman | e505464 | 2015-05-25 14:28:36 +0900 | [diff] [blame] | 2816 | const u32 *group_ids, u32 group_id) |
Scott Feldman | 9f6bbf7 | 2014-11-28 14:34:27 +0100 | [diff] [blame] | 2817 | { |
| 2818 | struct rocker_group_tbl_entry *entry; |
| 2819 | |
Scott Feldman | 179f9a2 | 2015-06-12 21:35:46 -0700 | [diff] [blame] | 2820 | entry = rocker_port_kzalloc(rocker_port, trans, flags, sizeof(*entry)); |
Scott Feldman | 9f6bbf7 | 2014-11-28 14:34:27 +0100 | [diff] [blame] | 2821 | if (!entry) |
| 2822 | return -ENOMEM; |
| 2823 | |
| 2824 | entry->group_id = group_id; |
| 2825 | entry->group_count = group_count; |
| 2826 | |
Scott Feldman | 179f9a2 | 2015-06-12 21:35:46 -0700 | [diff] [blame] | 2827 | entry->group_ids = rocker_port_kcalloc(rocker_port, trans, flags, |
| 2828 | group_count, sizeof(u32)); |
Scott Feldman | 9f6bbf7 | 2014-11-28 14:34:27 +0100 | [diff] [blame] | 2829 | if (!entry->group_ids) { |
Simon Horman | 0985df7 | 2015-05-25 14:28:35 +0900 | [diff] [blame] | 2830 | rocker_port_kfree(trans, entry); |
Scott Feldman | 9f6bbf7 | 2014-11-28 14:34:27 +0100 | [diff] [blame] | 2831 | return -ENOMEM; |
| 2832 | } |
| 2833 | memcpy(entry->group_ids, group_ids, group_count * sizeof(u32)); |
| 2834 | |
Scott Feldman | c4f2032 | 2015-05-10 09:47:50 -0700 | [diff] [blame] | 2835 | return rocker_group_tbl_do(rocker_port, trans, flags, entry); |
Scott Feldman | 9f6bbf7 | 2014-11-28 14:34:27 +0100 | [diff] [blame] | 2836 | } |
| 2837 | |
| 2838 | static int rocker_group_l2_flood(struct rocker_port *rocker_port, |
Scott Feldman | c4f2032 | 2015-05-10 09:47:50 -0700 | [diff] [blame] | 2839 | enum switchdev_trans trans, int flags, |
| 2840 | __be16 vlan_id, u8 group_count, |
Simon Horman | e505464 | 2015-05-25 14:28:36 +0900 | [diff] [blame] | 2841 | const u32 *group_ids, u32 group_id) |
Scott Feldman | 9f6bbf7 | 2014-11-28 14:34:27 +0100 | [diff] [blame] | 2842 | { |
Scott Feldman | c4f2032 | 2015-05-10 09:47:50 -0700 | [diff] [blame] | 2843 | return rocker_group_l2_fan_out(rocker_port, trans, flags, |
Scott Feldman | 9f6bbf7 | 2014-11-28 14:34:27 +0100 | [diff] [blame] | 2844 | group_count, group_ids, |
| 2845 | group_id); |
| 2846 | } |
| 2847 | |
Scott Feldman | c1beeef | 2015-03-05 21:21:20 -0800 | [diff] [blame] | 2848 | static int rocker_group_l3_unicast(struct rocker_port *rocker_port, |
Scott Feldman | c4f2032 | 2015-05-10 09:47:50 -0700 | [diff] [blame] | 2849 | enum switchdev_trans trans, int flags, |
Simon Horman | e505464 | 2015-05-25 14:28:36 +0900 | [diff] [blame] | 2850 | u32 index, const u8 *src_mac, const u8 *dst_mac, |
Scott Feldman | c4f2032 | 2015-05-10 09:47:50 -0700 | [diff] [blame] | 2851 | __be16 vlan_id, bool ttl_check, u32 pport) |
Scott Feldman | c1beeef | 2015-03-05 21:21:20 -0800 | [diff] [blame] | 2852 | { |
| 2853 | struct rocker_group_tbl_entry *entry; |
| 2854 | |
Scott Feldman | 179f9a2 | 2015-06-12 21:35:46 -0700 | [diff] [blame] | 2855 | entry = rocker_port_kzalloc(rocker_port, trans, flags, sizeof(*entry)); |
Scott Feldman | c1beeef | 2015-03-05 21:21:20 -0800 | [diff] [blame] | 2856 | 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 Feldman | c4f2032 | 2015-05-10 09:47:50 -0700 | [diff] [blame] | 2868 | return rocker_group_tbl_do(rocker_port, trans, flags, entry); |
Scott Feldman | c1beeef | 2015-03-05 21:21:20 -0800 | [diff] [blame] | 2869 | } |
| 2870 | |
| 2871 | static struct rocker_neigh_tbl_entry * |
Simon Horman | e505464 | 2015-05-25 14:28:36 +0900 | [diff] [blame] | 2872 | rocker_neigh_tbl_find(const struct rocker *rocker, __be32 ip_addr) |
Scott Feldman | c1beeef | 2015-03-05 21:21:20 -0800 | [diff] [blame] | 2873 | { |
| 2874 | struct rocker_neigh_tbl_entry *found; |
| 2875 | |
Scott Feldman | 0f43deb | 2015-03-06 15:54:51 -0800 | [diff] [blame] | 2876 | hash_for_each_possible(rocker->neigh_tbl, found, |
| 2877 | entry, be32_to_cpu(ip_addr)) |
Scott Feldman | c1beeef | 2015-03-05 21:21:20 -0800 | [diff] [blame] | 2878 | if (found->ip_addr == ip_addr) |
| 2879 | return found; |
| 2880 | |
| 2881 | return NULL; |
| 2882 | } |
| 2883 | |
| 2884 | static void _rocker_neigh_add(struct rocker *rocker, |
Simon Horman | 550ecc9 | 2015-05-21 12:40:16 +0900 | [diff] [blame] | 2885 | enum switchdev_trans trans, |
Scott Feldman | c1beeef | 2015-03-05 21:21:20 -0800 | [diff] [blame] | 2886 | struct rocker_neigh_tbl_entry *entry) |
| 2887 | { |
Scott Feldman | 4d81db4 | 2015-06-12 21:24:40 -0700 | [diff] [blame] | 2888 | if (trans != SWITCHDEV_TRANS_COMMIT) |
| 2889 | entry->index = rocker->neigh_tbl_next_index++; |
Simon Horman | 550ecc9 | 2015-05-21 12:40:16 +0900 | [diff] [blame] | 2890 | if (trans == SWITCHDEV_TRANS_PREPARE) |
| 2891 | return; |
Scott Feldman | c1beeef | 2015-03-05 21:21:20 -0800 | [diff] [blame] | 2892 | entry->ref_count++; |
Scott Feldman | 0f43deb | 2015-03-06 15:54:51 -0800 | [diff] [blame] | 2893 | hash_add(rocker->neigh_tbl, &entry->entry, |
| 2894 | be32_to_cpu(entry->ip_addr)); |
Scott Feldman | c1beeef | 2015-03-05 21:21:20 -0800 | [diff] [blame] | 2895 | } |
| 2896 | |
Simon Horman | 0985df7 | 2015-05-25 14:28:35 +0900 | [diff] [blame] | 2897 | static void _rocker_neigh_del(enum switchdev_trans trans, |
Scott Feldman | c1beeef | 2015-03-05 21:21:20 -0800 | [diff] [blame] | 2898 | struct rocker_neigh_tbl_entry *entry) |
| 2899 | { |
Simon Horman | 550ecc9 | 2015-05-21 12:40:16 +0900 | [diff] [blame] | 2900 | if (trans == SWITCHDEV_TRANS_PREPARE) |
| 2901 | return; |
Scott Feldman | c1beeef | 2015-03-05 21:21:20 -0800 | [diff] [blame] | 2902 | if (--entry->ref_count == 0) { |
| 2903 | hash_del(&entry->entry); |
Simon Horman | 0985df7 | 2015-05-25 14:28:35 +0900 | [diff] [blame] | 2904 | rocker_port_kfree(trans, entry); |
Scott Feldman | c1beeef | 2015-03-05 21:21:20 -0800 | [diff] [blame] | 2905 | } |
| 2906 | } |
| 2907 | |
Scott Feldman | c4f2032 | 2015-05-10 09:47:50 -0700 | [diff] [blame] | 2908 | static void _rocker_neigh_update(struct rocker_neigh_tbl_entry *entry, |
Simon Horman | 550ecc9 | 2015-05-21 12:40:16 +0900 | [diff] [blame] | 2909 | enum switchdev_trans trans, |
Simon Horman | e505464 | 2015-05-25 14:28:36 +0900 | [diff] [blame] | 2910 | const u8 *eth_dst, bool ttl_check) |
Scott Feldman | c1beeef | 2015-03-05 21:21:20 -0800 | [diff] [blame] | 2911 | { |
| 2912 | if (eth_dst) { |
| 2913 | ether_addr_copy(entry->eth_dst, eth_dst); |
| 2914 | entry->ttl_check = ttl_check; |
Simon Horman | 550ecc9 | 2015-05-21 12:40:16 +0900 | [diff] [blame] | 2915 | } else if (trans != SWITCHDEV_TRANS_PREPARE) { |
Scott Feldman | c1beeef | 2015-03-05 21:21:20 -0800 | [diff] [blame] | 2916 | entry->ref_count++; |
| 2917 | } |
| 2918 | } |
| 2919 | |
| 2920 | static int rocker_port_ipv4_neigh(struct rocker_port *rocker_port, |
Scott Feldman | c4f2032 | 2015-05-10 09:47:50 -0700 | [diff] [blame] | 2921 | enum switchdev_trans trans, |
Simon Horman | e505464 | 2015-05-25 14:28:36 +0900 | [diff] [blame] | 2922 | int flags, __be32 ip_addr, const u8 *eth_dst) |
Scott Feldman | c1beeef | 2015-03-05 21:21:20 -0800 | [diff] [blame] | 2923 | { |
| 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 Feldman | 179f9a2 | 2015-06-12 21:35:46 -0700 | [diff] [blame] | 2938 | entry = rocker_port_kzalloc(rocker_port, trans, flags, sizeof(*entry)); |
Scott Feldman | c1beeef | 2015-03-05 21:21:20 -0800 | [diff] [blame] | 2939 | 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 Horman | 550ecc9 | 2015-05-21 12:40:16 +0900 | [diff] [blame] | 2955 | _rocker_neigh_add(rocker, trans, entry); |
Scott Feldman | c1beeef | 2015-03-05 21:21:20 -0800 | [diff] [blame] | 2956 | } else if (removing) { |
| 2957 | memcpy(entry, found, sizeof(*entry)); |
Simon Horman | 0985df7 | 2015-05-25 14:28:35 +0900 | [diff] [blame] | 2958 | _rocker_neigh_del(trans, found); |
Scott Feldman | c1beeef | 2015-03-05 21:21:20 -0800 | [diff] [blame] | 2959 | } else if (updating) { |
Simon Horman | 550ecc9 | 2015-05-21 12:40:16 +0900 | [diff] [blame] | 2960 | _rocker_neigh_update(found, trans, eth_dst, true); |
Scott Feldman | c1beeef | 2015-03-05 21:21:20 -0800 | [diff] [blame] | 2961 | 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 Feldman | c4f2032 | 2015-05-10 09:47:50 -0700 | [diff] [blame] | 2977 | err = rocker_group_l3_unicast(rocker_port, trans, flags, |
Scott Feldman | c1beeef | 2015-03-05 21:21:20 -0800 | [diff] [blame] | 2978 | 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 Feldman | c4f2032 | 2015-05-10 09:47:50 -0700 | [diff] [blame] | 2993 | err = rocker_flow_tbl_ucast4_routing(rocker_port, trans, |
Scott Feldman | c1beeef | 2015-03-05 21:21:20 -0800 | [diff] [blame] | 2994 | 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 | |
| 3005 | err_out: |
| 3006 | if (!adding) |
Simon Horman | 0985df7 | 2015-05-25 14:28:35 +0900 | [diff] [blame] | 3007 | rocker_port_kfree(trans, entry); |
Scott Feldman | c1beeef | 2015-03-05 21:21:20 -0800 | [diff] [blame] | 3008 | |
| 3009 | return err; |
| 3010 | } |
| 3011 | |
| 3012 | static int rocker_port_ipv4_resolve(struct rocker_port *rocker_port, |
Scott Feldman | c4f2032 | 2015-05-10 09:47:50 -0700 | [diff] [blame] | 3013 | enum switchdev_trans trans, __be32 ip_addr) |
Scott Feldman | c1beeef | 2015-03-05 21:21:20 -0800 | [diff] [blame] | 3014 | { |
| 3015 | struct net_device *dev = rocker_port->dev; |
Scott Feldman | 0f43deb | 2015-03-06 15:54:51 -0800 | [diff] [blame] | 3016 | struct neighbour *n = __ipv4_neigh_lookup(dev, (__force u32)ip_addr); |
Scott Feldman | c1beeef | 2015-03-05 21:21:20 -0800 | [diff] [blame] | 3017 | int err = 0; |
| 3018 | |
Ying Xue | 4133fc0 | 2015-05-15 12:53:21 +0800 | [diff] [blame] | 3019 | if (!n) { |
Scott Feldman | c1beeef | 2015-03-05 21:21:20 -0800 | [diff] [blame] | 3020 | n = neigh_create(&arp_tbl, &ip_addr, dev); |
Ying Xue | 4133fc0 | 2015-05-15 12:53:21 +0800 | [diff] [blame] | 3021 | if (IS_ERR(n)) |
| 3022 | return IS_ERR(n); |
| 3023 | } |
Scott Feldman | c1beeef | 2015-03-05 21:21:20 -0800 | [diff] [blame] | 3024 | |
| 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 Feldman | c4f2032 | 2015-05-10 09:47:50 -0700 | [diff] [blame] | 3031 | err = rocker_port_ipv4_neigh(rocker_port, trans, 0, |
| 3032 | ip_addr, n->ha); |
Scott Feldman | c1beeef | 2015-03-05 21:21:20 -0800 | [diff] [blame] | 3033 | else |
| 3034 | neigh_event_send(n, NULL); |
| 3035 | |
Ying Xue | 4133fc0 | 2015-05-15 12:53:21 +0800 | [diff] [blame] | 3036 | neigh_release(n); |
Scott Feldman | c1beeef | 2015-03-05 21:21:20 -0800 | [diff] [blame] | 3037 | return err; |
| 3038 | } |
| 3039 | |
Scott Feldman | c4f2032 | 2015-05-10 09:47:50 -0700 | [diff] [blame] | 3040 | static int rocker_port_ipv4_nh(struct rocker_port *rocker_port, |
| 3041 | enum switchdev_trans trans, int flags, |
Scott Feldman | c1beeef | 2015-03-05 21:21:20 -0800 | [diff] [blame] | 3042 | __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 Feldman | 179f9a2 | 2015-06-12 21:35:46 -0700 | [diff] [blame] | 3054 | entry = rocker_port_kzalloc(rocker_port, trans, flags, sizeof(*entry)); |
Scott Feldman | c1beeef | 2015-03-05 21:21:20 -0800 | [diff] [blame] | 3055 | 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 Horman | 550ecc9 | 2015-05-21 12:40:16 +0900 | [diff] [blame] | 3071 | _rocker_neigh_add(rocker, trans, entry); |
Scott Feldman | c1beeef | 2015-03-05 21:21:20 -0800 | [diff] [blame] | 3072 | *index = entry->index; |
| 3073 | resolved = false; |
| 3074 | } else if (removing) { |
Simon Horman | 0985df7 | 2015-05-25 14:28:35 +0900 | [diff] [blame] | 3075 | _rocker_neigh_del(trans, found); |
Scott Feldman | c1beeef | 2015-03-05 21:21:20 -0800 | [diff] [blame] | 3076 | } else if (updating) { |
Simon Horman | 550ecc9 | 2015-05-21 12:40:16 +0900 | [diff] [blame] | 3077 | _rocker_neigh_update(found, trans, NULL, false); |
Scott Feldman | c1beeef | 2015-03-05 21:21:20 -0800 | [diff] [blame] | 3078 | 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 Horman | 0985df7 | 2015-05-25 14:28:35 +0900 | [diff] [blame] | 3086 | rocker_port_kfree(trans, entry); |
Scott Feldman | c1beeef | 2015-03-05 21:21:20 -0800 | [diff] [blame] | 3087 | |
| 3088 | if (err) |
| 3089 | return err; |
| 3090 | |
| 3091 | /* Resolved means neigh ip_addr is resolved to neigh mac. */ |
| 3092 | |
| 3093 | if (!resolved) |
Scott Feldman | c4f2032 | 2015-05-10 09:47:50 -0700 | [diff] [blame] | 3094 | err = rocker_port_ipv4_resolve(rocker_port, trans, ip_addr); |
Scott Feldman | c1beeef | 2015-03-05 21:21:20 -0800 | [diff] [blame] | 3095 | |
| 3096 | return err; |
| 3097 | } |
| 3098 | |
Scott Feldman | 6c70794 | 2014-11-28 14:34:28 +0100 | [diff] [blame] | 3099 | static int rocker_port_vlan_flood_group(struct rocker_port *rocker_port, |
Scott Feldman | c4f2032 | 2015-05-10 09:47:50 -0700 | [diff] [blame] | 3100 | enum switchdev_trans trans, |
Scott Feldman | 6c70794 | 2014-11-28 14:34:28 +0100 | [diff] [blame] | 3101 | int flags, __be16 vlan_id) |
| 3102 | { |
| 3103 | struct rocker_port *p; |
Simon Horman | e505464 | 2015-05-25 14:28:36 +0900 | [diff] [blame] | 3104 | const struct rocker *rocker = rocker_port->rocker; |
Scott Feldman | 6c70794 | 2014-11-28 14:34:28 +0100 | [diff] [blame] | 3105 | u32 group_id = ROCKER_GROUP_L2_FLOOD(vlan_id, 0); |
Scott Feldman | 04f49fa | 2015-03-15 23:04:46 -0700 | [diff] [blame] | 3106 | u32 *group_ids; |
Scott Feldman | 6c70794 | 2014-11-28 14:34:28 +0100 | [diff] [blame] | 3107 | u8 group_count = 0; |
Scott Feldman | 04f49fa | 2015-03-15 23:04:46 -0700 | [diff] [blame] | 3108 | int err = 0; |
Scott Feldman | 6c70794 | 2014-11-28 14:34:28 +0100 | [diff] [blame] | 3109 | int i; |
| 3110 | |
Scott Feldman | 179f9a2 | 2015-06-12 21:35:46 -0700 | [diff] [blame] | 3111 | group_ids = rocker_port_kcalloc(rocker_port, trans, flags, |
| 3112 | rocker->port_count, sizeof(u32)); |
Scott Feldman | 04f49fa | 2015-03-15 23:04:46 -0700 | [diff] [blame] | 3113 | if (!group_ids) |
| 3114 | return -ENOMEM; |
| 3115 | |
Scott Feldman | 6c70794 | 2014-11-28 14:34:28 +0100 | [diff] [blame] | 3116 | /* 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 Feldman | bcfd780 | 2015-06-01 11:39:04 -0700 | [diff] [blame] | 3123 | if (!p) |
| 3124 | continue; |
Scott Feldman | 6c70794 | 2014-11-28 14:34:28 +0100 | [diff] [blame] | 3125 | if (!rocker_port_is_bridged(p)) |
| 3126 | continue; |
| 3127 | if (test_bit(ntohs(vlan_id), p->vlan_bitmap)) { |
| 3128 | group_ids[group_count++] = |
Scott Feldman | 4a6bb6d | 2015-02-25 20:15:37 -0800 | [diff] [blame] | 3129 | ROCKER_GROUP_L2_INTERFACE(vlan_id, p->pport); |
Scott Feldman | 6c70794 | 2014-11-28 14:34:28 +0100 | [diff] [blame] | 3130 | } |
| 3131 | } |
| 3132 | |
| 3133 | /* If there are no bridged ports in this VLAN, we're done */ |
| 3134 | if (group_count == 0) |
Scott Feldman | 04f49fa | 2015-03-15 23:04:46 -0700 | [diff] [blame] | 3135 | goto no_ports_in_vlan; |
Scott Feldman | 6c70794 | 2014-11-28 14:34:28 +0100 | [diff] [blame] | 3136 | |
Scott Feldman | c4f2032 | 2015-05-10 09:47:50 -0700 | [diff] [blame] | 3137 | err = rocker_group_l2_flood(rocker_port, trans, flags, vlan_id, |
| 3138 | group_count, group_ids, group_id); |
Scott Feldman | 6c70794 | 2014-11-28 14:34:28 +0100 | [diff] [blame] | 3139 | if (err) |
| 3140 | netdev_err(rocker_port->dev, |
| 3141 | "Error (%d) port VLAN l2 flood group\n", err); |
| 3142 | |
Scott Feldman | 04f49fa | 2015-03-15 23:04:46 -0700 | [diff] [blame] | 3143 | no_ports_in_vlan: |
Simon Horman | 0985df7 | 2015-05-25 14:28:35 +0900 | [diff] [blame] | 3144 | rocker_port_kfree(trans, group_ids); |
Scott Feldman | 6c70794 | 2014-11-28 14:34:28 +0100 | [diff] [blame] | 3145 | return err; |
| 3146 | } |
| 3147 | |
| 3148 | static int rocker_port_vlan_l2_groups(struct rocker_port *rocker_port, |
Scott Feldman | c4f2032 | 2015-05-10 09:47:50 -0700 | [diff] [blame] | 3149 | enum switchdev_trans trans, int flags, |
| 3150 | __be16 vlan_id, bool pop_vlan) |
Scott Feldman | 6c70794 | 2014-11-28 14:34:28 +0100 | [diff] [blame] | 3151 | { |
Simon Horman | e505464 | 2015-05-25 14:28:36 +0900 | [diff] [blame] | 3152 | const struct rocker *rocker = rocker_port->rocker; |
Scott Feldman | 6c70794 | 2014-11-28 14:34:28 +0100 | [diff] [blame] | 3153 | struct rocker_port *p; |
| 3154 | bool adding = !(flags & ROCKER_OP_FLAG_REMOVE); |
Scott Feldman | 4a6bb6d | 2015-02-25 20:15:37 -0800 | [diff] [blame] | 3155 | u32 out_pport; |
Scott Feldman | 6c70794 | 2014-11-28 14:34:28 +0100 | [diff] [blame] | 3156 | 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 Feldman | 4a6bb6d | 2015-02-25 20:15:37 -0800 | [diff] [blame] | 3166 | out_pport = rocker_port->pport; |
Scott Feldman | c4f2032 | 2015-05-10 09:47:50 -0700 | [diff] [blame] | 3167 | err = rocker_group_l2_interface(rocker_port, trans, flags, |
| 3168 | vlan_id, out_pport, pop_vlan); |
Scott Feldman | 6c70794 | 2014-11-28 14:34:28 +0100 | [diff] [blame] | 3169 | if (err) { |
| 3170 | netdev_err(rocker_port->dev, |
Scott Feldman | 4a6bb6d | 2015-02-25 20:15:37 -0800 | [diff] [blame] | 3171 | "Error (%d) port VLAN l2 group for pport %d\n", |
| 3172 | err, out_pport); |
Scott Feldman | 6c70794 | 2014-11-28 14:34:28 +0100 | [diff] [blame] | 3173 | 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 Feldman | bcfd780 | 2015-06-01 11:39:04 -0700 | [diff] [blame] | 3184 | if (p && test_bit(ntohs(vlan_id), p->vlan_bitmap)) |
Scott Feldman | 6c70794 | 2014-11-28 14:34:28 +0100 | [diff] [blame] | 3185 | ref++; |
| 3186 | } |
| 3187 | |
| 3188 | if ((!adding || ref != 1) && (adding || ref != 0)) |
| 3189 | return 0; |
| 3190 | |
Scott Feldman | 4a6bb6d | 2015-02-25 20:15:37 -0800 | [diff] [blame] | 3191 | out_pport = 0; |
Scott Feldman | c4f2032 | 2015-05-10 09:47:50 -0700 | [diff] [blame] | 3192 | err = rocker_group_l2_interface(rocker_port, trans, flags, |
| 3193 | vlan_id, out_pport, pop_vlan); |
Scott Feldman | 6c70794 | 2014-11-28 14:34:28 +0100 | [diff] [blame] | 3194 | 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 Feldman | 9f6bbf7 | 2014-11-28 14:34:27 +0100 | [diff] [blame] | 3203 | static struct rocker_ctrl { |
| 3204 | const u8 *eth_dst; |
| 3205 | const u8 *eth_dst_mask; |
Jiri Pirko | 11e6c65 | 2014-12-03 14:14:54 +0100 | [diff] [blame] | 3206 | __be16 eth_type; |
Scott Feldman | 9f6bbf7 | 2014-11-28 14:34:27 +0100 | [diff] [blame] | 3207 | 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 | |
| 3248 | static int rocker_port_ctrl_vlan_acl(struct rocker_port *rocker_port, |
Scott Feldman | c4f2032 | 2015-05-10 09:47:50 -0700 | [diff] [blame] | 3249 | enum switchdev_trans trans, int flags, |
Simon Horman | e505464 | 2015-05-25 14:28:36 +0900 | [diff] [blame] | 3250 | const struct rocker_ctrl *ctrl, __be16 vlan_id) |
Scott Feldman | 9f6bbf7 | 2014-11-28 14:34:27 +0100 | [diff] [blame] | 3251 | { |
Scott Feldman | 4a6bb6d | 2015-02-25 20:15:37 -0800 | [diff] [blame] | 3252 | u32 in_pport = rocker_port->pport; |
| 3253 | u32 in_pport_mask = 0xffffffff; |
| 3254 | u32 out_pport = 0; |
Simon Horman | e505464 | 2015-05-25 14:28:36 +0900 | [diff] [blame] | 3255 | const u8 *eth_src = NULL; |
| 3256 | const u8 *eth_src_mask = NULL; |
Scott Feldman | 9f6bbf7 | 2014-11-28 14:34:27 +0100 | [diff] [blame] | 3257 | __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 Feldman | 4a6bb6d | 2015-02-25 20:15:37 -0800 | [diff] [blame] | 3262 | u32 group_id = ROCKER_GROUP_L2_INTERFACE(vlan_id, out_pport); |
Scott Feldman | 9f6bbf7 | 2014-11-28 14:34:27 +0100 | [diff] [blame] | 3263 | int err; |
| 3264 | |
Scott Feldman | c4f2032 | 2015-05-10 09:47:50 -0700 | [diff] [blame] | 3265 | err = rocker_flow_tbl_acl(rocker_port, trans, flags, |
Scott Feldman | 4a6bb6d | 2015-02-25 20:15:37 -0800 | [diff] [blame] | 3266 | in_pport, in_pport_mask, |
Scott Feldman | 9f6bbf7 | 2014-11-28 14:34:27 +0100 | [diff] [blame] | 3267 | 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 Feldman | 6c70794 | 2014-11-28 14:34:28 +0100 | [diff] [blame] | 3281 | static int rocker_port_ctrl_vlan_bridge(struct rocker_port *rocker_port, |
Scott Feldman | c4f2032 | 2015-05-10 09:47:50 -0700 | [diff] [blame] | 3282 | enum switchdev_trans trans, int flags, |
Simon Horman | e505464 | 2015-05-25 14:28:36 +0900 | [diff] [blame] | 3283 | const struct rocker_ctrl *ctrl, |
Scott Feldman | 6c70794 | 2014-11-28 14:34:28 +0100 | [diff] [blame] | 3284 | __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 Feldman | c4f2032 | 2015-05-10 09:47:50 -0700 | [diff] [blame] | 3295 | err = rocker_flow_tbl_bridge(rocker_port, trans, flags, |
Scott Feldman | 6c70794 | 2014-11-28 14:34:28 +0100 | [diff] [blame] | 3296 | 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 Feldman | 9f6bbf7 | 2014-11-28 14:34:27 +0100 | [diff] [blame] | 3306 | static int rocker_port_ctrl_vlan_term(struct rocker_port *rocker_port, |
Scott Feldman | c4f2032 | 2015-05-10 09:47:50 -0700 | [diff] [blame] | 3307 | enum switchdev_trans trans, int flags, |
Simon Horman | e505464 | 2015-05-25 14:28:36 +0900 | [diff] [blame] | 3308 | const struct rocker_ctrl *ctrl, __be16 vlan_id) |
Scott Feldman | 9f6bbf7 | 2014-11-28 14:34:27 +0100 | [diff] [blame] | 3309 | { |
Scott Feldman | 4a6bb6d | 2015-02-25 20:15:37 -0800 | [diff] [blame] | 3310 | u32 in_pport_mask = 0xffffffff; |
Scott Feldman | 9f6bbf7 | 2014-11-28 14:34:27 +0100 | [diff] [blame] | 3311 | __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 Feldman | c4f2032 | 2015-05-10 09:47:50 -0700 | [diff] [blame] | 3317 | err = rocker_flow_tbl_term_mac(rocker_port, trans, |
Scott Feldman | 4a6bb6d | 2015-02-25 20:15:37 -0800 | [diff] [blame] | 3318 | rocker_port->pport, in_pport_mask, |
Scott Feldman | 9f6bbf7 | 2014-11-28 14:34:27 +0100 | [diff] [blame] | 3319 | 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 Feldman | c4f2032 | 2015-05-10 09:47:50 -0700 | [diff] [blame] | 3330 | static int rocker_port_ctrl_vlan(struct rocker_port *rocker_port, |
| 3331 | enum switchdev_trans trans, int flags, |
Simon Horman | e505464 | 2015-05-25 14:28:36 +0900 | [diff] [blame] | 3332 | const struct rocker_ctrl *ctrl, __be16 vlan_id) |
Scott Feldman | 9f6bbf7 | 2014-11-28 14:34:27 +0100 | [diff] [blame] | 3333 | { |
| 3334 | if (ctrl->acl) |
Scott Feldman | c4f2032 | 2015-05-10 09:47:50 -0700 | [diff] [blame] | 3335 | return rocker_port_ctrl_vlan_acl(rocker_port, trans, flags, |
Scott Feldman | 9f6bbf7 | 2014-11-28 14:34:27 +0100 | [diff] [blame] | 3336 | ctrl, vlan_id); |
Scott Feldman | 6c70794 | 2014-11-28 14:34:28 +0100 | [diff] [blame] | 3337 | if (ctrl->bridge) |
Scott Feldman | c4f2032 | 2015-05-10 09:47:50 -0700 | [diff] [blame] | 3338 | return rocker_port_ctrl_vlan_bridge(rocker_port, trans, flags, |
Scott Feldman | 6c70794 | 2014-11-28 14:34:28 +0100 | [diff] [blame] | 3339 | ctrl, vlan_id); |
Scott Feldman | 9f6bbf7 | 2014-11-28 14:34:27 +0100 | [diff] [blame] | 3340 | |
| 3341 | if (ctrl->term) |
Scott Feldman | c4f2032 | 2015-05-10 09:47:50 -0700 | [diff] [blame] | 3342 | return rocker_port_ctrl_vlan_term(rocker_port, trans, flags, |
Scott Feldman | 9f6bbf7 | 2014-11-28 14:34:27 +0100 | [diff] [blame] | 3343 | ctrl, vlan_id); |
| 3344 | |
| 3345 | return -EOPNOTSUPP; |
| 3346 | } |
| 3347 | |
| 3348 | static int rocker_port_ctrl_vlan_add(struct rocker_port *rocker_port, |
Scott Feldman | c4f2032 | 2015-05-10 09:47:50 -0700 | [diff] [blame] | 3349 | enum switchdev_trans trans, int flags, |
| 3350 | __be16 vlan_id) |
Scott Feldman | 9f6bbf7 | 2014-11-28 14:34:27 +0100 | [diff] [blame] | 3351 | { |
| 3352 | int err = 0; |
| 3353 | int i; |
| 3354 | |
| 3355 | for (i = 0; i < ROCKER_CTRL_MAX; i++) { |
| 3356 | if (rocker_port->ctrls[i]) { |
Scott Feldman | c4f2032 | 2015-05-10 09:47:50 -0700 | [diff] [blame] | 3357 | err = rocker_port_ctrl_vlan(rocker_port, trans, flags, |
Scott Feldman | 9f6bbf7 | 2014-11-28 14:34:27 +0100 | [diff] [blame] | 3358 | &rocker_ctrls[i], vlan_id); |
| 3359 | if (err) |
| 3360 | return err; |
| 3361 | } |
| 3362 | } |
| 3363 | |
| 3364 | return err; |
| 3365 | } |
| 3366 | |
Scott Feldman | c4f2032 | 2015-05-10 09:47:50 -0700 | [diff] [blame] | 3367 | static int rocker_port_ctrl(struct rocker_port *rocker_port, |
| 3368 | enum switchdev_trans trans, int flags, |
Simon Horman | e505464 | 2015-05-25 14:28:36 +0900 | [diff] [blame] | 3369 | const struct rocker_ctrl *ctrl) |
Scott Feldman | 9f6bbf7 | 2014-11-28 14:34:27 +0100 | [diff] [blame] | 3370 | { |
| 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 Feldman | c4f2032 | 2015-05-10 09:47:50 -0700 | [diff] [blame] | 3377 | err = rocker_port_ctrl_vlan(rocker_port, trans, flags, |
Scott Feldman | 9f6bbf7 | 2014-11-28 14:34:27 +0100 | [diff] [blame] | 3378 | ctrl, htons(vid)); |
| 3379 | if (err) |
| 3380 | break; |
| 3381 | } |
| 3382 | |
| 3383 | return err; |
| 3384 | } |
| 3385 | |
Scott Feldman | c4f2032 | 2015-05-10 09:47:50 -0700 | [diff] [blame] | 3386 | static int rocker_port_vlan(struct rocker_port *rocker_port, |
| 3387 | enum switchdev_trans trans, int flags, u16 vid) |
Scott Feldman | 6c70794 | 2014-11-28 14:34:28 +0100 | [diff] [blame] | 3388 | { |
| 3389 | enum rocker_of_dpa_table_id goto_tbl = |
| 3390 | ROCKER_OF_DPA_TABLE_ID_TERMINATION_MAC; |
Scott Feldman | 4a6bb6d | 2015-02-25 20:15:37 -0800 | [diff] [blame] | 3391 | u32 in_pport = rocker_port->pport; |
Scott Feldman | 6c70794 | 2014-11-28 14:34:28 +0100 | [diff] [blame] | 3392 | __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 Feldman | 9228ad2 | 2015-05-10 09:47:54 -0700 | [diff] [blame] | 3401 | if (adding && test_bit(ntohs(internal_vlan_id), |
| 3402 | rocker_port->vlan_bitmap)) |
Scott Feldman | 6c70794 | 2014-11-28 14:34:28 +0100 | [diff] [blame] | 3403 | return 0; /* already added */ |
Scott Feldman | 9228ad2 | 2015-05-10 09:47:54 -0700 | [diff] [blame] | 3404 | else if (!adding && !test_bit(ntohs(internal_vlan_id), |
| 3405 | rocker_port->vlan_bitmap)) |
Scott Feldman | 6c70794 | 2014-11-28 14:34:28 +0100 | [diff] [blame] | 3406 | return 0; /* already removed */ |
| 3407 | |
Scott Feldman | 9228ad2 | 2015-05-10 09:47:54 -0700 | [diff] [blame] | 3408 | change_bit(ntohs(internal_vlan_id), rocker_port->vlan_bitmap); |
| 3409 | |
Scott Feldman | 6c70794 | 2014-11-28 14:34:28 +0100 | [diff] [blame] | 3410 | if (adding) { |
Scott Feldman | c4f2032 | 2015-05-10 09:47:50 -0700 | [diff] [blame] | 3411 | err = rocker_port_ctrl_vlan_add(rocker_port, trans, flags, |
Scott Feldman | 6c70794 | 2014-11-28 14:34:28 +0100 | [diff] [blame] | 3412 | internal_vlan_id); |
| 3413 | if (err) { |
| 3414 | netdev_err(rocker_port->dev, |
| 3415 | "Error (%d) port ctrl vlan add\n", err); |
Scott Feldman | 9228ad2 | 2015-05-10 09:47:54 -0700 | [diff] [blame] | 3416 | goto err_out; |
Scott Feldman | 6c70794 | 2014-11-28 14:34:28 +0100 | [diff] [blame] | 3417 | } |
| 3418 | } |
| 3419 | |
Scott Feldman | c4f2032 | 2015-05-10 09:47:50 -0700 | [diff] [blame] | 3420 | err = rocker_port_vlan_l2_groups(rocker_port, trans, flags, |
Scott Feldman | 6c70794 | 2014-11-28 14:34:28 +0100 | [diff] [blame] | 3421 | internal_vlan_id, untagged); |
| 3422 | if (err) { |
| 3423 | netdev_err(rocker_port->dev, |
| 3424 | "Error (%d) port VLAN l2 groups\n", err); |
Scott Feldman | 9228ad2 | 2015-05-10 09:47:54 -0700 | [diff] [blame] | 3425 | goto err_out; |
Scott Feldman | 6c70794 | 2014-11-28 14:34:28 +0100 | [diff] [blame] | 3426 | } |
| 3427 | |
Scott Feldman | c4f2032 | 2015-05-10 09:47:50 -0700 | [diff] [blame] | 3428 | err = rocker_port_vlan_flood_group(rocker_port, trans, flags, |
Scott Feldman | 6c70794 | 2014-11-28 14:34:28 +0100 | [diff] [blame] | 3429 | internal_vlan_id); |
| 3430 | if (err) { |
| 3431 | netdev_err(rocker_port->dev, |
| 3432 | "Error (%d) port VLAN l2 flood group\n", err); |
Scott Feldman | 9228ad2 | 2015-05-10 09:47:54 -0700 | [diff] [blame] | 3433 | goto err_out; |
Scott Feldman | 6c70794 | 2014-11-28 14:34:28 +0100 | [diff] [blame] | 3434 | } |
| 3435 | |
Scott Feldman | c4f2032 | 2015-05-10 09:47:50 -0700 | [diff] [blame] | 3436 | err = rocker_flow_tbl_vlan(rocker_port, trans, flags, |
Scott Feldman | 4a6bb6d | 2015-02-25 20:15:37 -0800 | [diff] [blame] | 3437 | in_pport, vlan_id, vlan_id_mask, |
Scott Feldman | 6c70794 | 2014-11-28 14:34:28 +0100 | [diff] [blame] | 3438 | 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 Feldman | 9228ad2 | 2015-05-10 09:47:54 -0700 | [diff] [blame] | 3443 | err_out: |
| 3444 | if (trans == SWITCHDEV_TRANS_PREPARE) |
| 3445 | change_bit(ntohs(internal_vlan_id), rocker_port->vlan_bitmap); |
| 3446 | |
Scott Feldman | 6c70794 | 2014-11-28 14:34:28 +0100 | [diff] [blame] | 3447 | return err; |
| 3448 | } |
| 3449 | |
Scott Feldman | c4f2032 | 2015-05-10 09:47:50 -0700 | [diff] [blame] | 3450 | static int rocker_port_ig_tbl(struct rocker_port *rocker_port, |
| 3451 | enum switchdev_trans trans, int flags) |
Scott Feldman | 9f6bbf7 | 2014-11-28 14:34:27 +0100 | [diff] [blame] | 3452 | { |
| 3453 | enum rocker_of_dpa_table_id goto_tbl; |
Scott Feldman | 4a6bb6d | 2015-02-25 20:15:37 -0800 | [diff] [blame] | 3454 | u32 in_pport; |
| 3455 | u32 in_pport_mask; |
Scott Feldman | 9f6bbf7 | 2014-11-28 14:34:27 +0100 | [diff] [blame] | 3456 | int err; |
| 3457 | |
| 3458 | /* Normal Ethernet Frames. Matches pkts from any local physical |
| 3459 | * ports. Goto VLAN tbl. |
| 3460 | */ |
| 3461 | |
Scott Feldman | 4a6bb6d | 2015-02-25 20:15:37 -0800 | [diff] [blame] | 3462 | in_pport = 0; |
| 3463 | in_pport_mask = 0xffff0000; |
Scott Feldman | 9f6bbf7 | 2014-11-28 14:34:27 +0100 | [diff] [blame] | 3464 | goto_tbl = ROCKER_OF_DPA_TABLE_ID_VLAN; |
| 3465 | |
Scott Feldman | c4f2032 | 2015-05-10 09:47:50 -0700 | [diff] [blame] | 3466 | err = rocker_flow_tbl_ig_port(rocker_port, trans, flags, |
Scott Feldman | 4a6bb6d | 2015-02-25 20:15:37 -0800 | [diff] [blame] | 3467 | in_pport, in_pport_mask, |
Scott Feldman | 9f6bbf7 | 2014-11-28 14:34:27 +0100 | [diff] [blame] | 3468 | 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 Feldman | 6c70794 | 2014-11-28 14:34:28 +0100 | [diff] [blame] | 3476 | struct rocker_fdb_learn_work { |
| 3477 | struct work_struct work; |
Scott Feldman | c4f2032 | 2015-05-10 09:47:50 -0700 | [diff] [blame] | 3478 | struct rocker_port *rocker_port; |
| 3479 | enum switchdev_trans trans; |
Scott Feldman | 6c70794 | 2014-11-28 14:34:28 +0100 | [diff] [blame] | 3480 | int flags; |
| 3481 | u8 addr[ETH_ALEN]; |
| 3482 | u16 vid; |
| 3483 | }; |
| 3484 | |
| 3485 | static void rocker_port_fdb_learn_work(struct work_struct *work) |
| 3486 | { |
Simon Horman | e505464 | 2015-05-25 14:28:36 +0900 | [diff] [blame] | 3487 | const struct rocker_fdb_learn_work *lw = |
Scott Feldman | 6c70794 | 2014-11-28 14:34:28 +0100 | [diff] [blame] | 3488 | 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 Pirko | ebb9a03 | 2015-05-10 09:47:46 -0700 | [diff] [blame] | 3491 | struct switchdev_notifier_fdb_info info; |
Jiri Pirko | 3aeb661 | 2015-01-15 23:49:37 +0100 | [diff] [blame] | 3492 | |
| 3493 | info.addr = lw->addr; |
| 3494 | info.vid = lw->vid; |
Scott Feldman | 6c70794 | 2014-11-28 14:34:28 +0100 | [diff] [blame] | 3495 | |
Thomas Graf | 51ace88 | 2014-11-28 14:34:32 +0100 | [diff] [blame] | 3496 | if (learned && removing) |
Jiri Pirko | ebb9a03 | 2015-05-10 09:47:46 -0700 | [diff] [blame] | 3497 | call_switchdev_notifiers(SWITCHDEV_FDB_DEL, |
Scott Feldman | c4f2032 | 2015-05-10 09:47:50 -0700 | [diff] [blame] | 3498 | lw->rocker_port->dev, &info.info); |
Thomas Graf | 51ace88 | 2014-11-28 14:34:32 +0100 | [diff] [blame] | 3499 | else if (learned && !removing) |
Jiri Pirko | ebb9a03 | 2015-05-10 09:47:46 -0700 | [diff] [blame] | 3500 | call_switchdev_notifiers(SWITCHDEV_FDB_ADD, |
Scott Feldman | c4f2032 | 2015-05-10 09:47:50 -0700 | [diff] [blame] | 3501 | lw->rocker_port->dev, &info.info); |
Scott Feldman | 6c70794 | 2014-11-28 14:34:28 +0100 | [diff] [blame] | 3502 | |
Simon Horman | 0985df7 | 2015-05-25 14:28:35 +0900 | [diff] [blame] | 3503 | rocker_port_kfree(lw->trans, work); |
Scott Feldman | 6c70794 | 2014-11-28 14:34:28 +0100 | [diff] [blame] | 3504 | } |
| 3505 | |
| 3506 | static int rocker_port_fdb_learn(struct rocker_port *rocker_port, |
Scott Feldman | c4f2032 | 2015-05-10 09:47:50 -0700 | [diff] [blame] | 3507 | enum switchdev_trans trans, int flags, |
| 3508 | const u8 *addr, __be16 vlan_id) |
Scott Feldman | 6c70794 | 2014-11-28 14:34:28 +0100 | [diff] [blame] | 3509 | { |
| 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 Feldman | 4a6bb6d | 2015-02-25 20:15:37 -0800 | [diff] [blame] | 3513 | u32 out_pport = rocker_port->pport; |
Scott Feldman | 6c70794 | 2014-11-28 14:34:28 +0100 | [diff] [blame] | 3514 | u32 tunnel_id = 0; |
| 3515 | u32 group_id = ROCKER_GROUP_NONE; |
Scott Feldman | 5111f80 | 2014-11-28 14:34:30 +0100 | [diff] [blame] | 3516 | bool syncing = !!(rocker_port->brport_flags & BR_LEARNING_SYNC); |
Scott Feldman | 6c70794 | 2014-11-28 14:34:28 +0100 | [diff] [blame] | 3517 | bool copy_to_cpu = false; |
| 3518 | int err; |
| 3519 | |
| 3520 | if (rocker_port_is_bridged(rocker_port)) |
Scott Feldman | 4a6bb6d | 2015-02-25 20:15:37 -0800 | [diff] [blame] | 3521 | group_id = ROCKER_GROUP_L2_INTERFACE(vlan_id, out_pport); |
Scott Feldman | 6c70794 | 2014-11-28 14:34:28 +0100 | [diff] [blame] | 3522 | |
| 3523 | if (!(flags & ROCKER_OP_FLAG_REFRESH)) { |
Scott Feldman | c4f2032 | 2015-05-10 09:47:50 -0700 | [diff] [blame] | 3524 | err = rocker_flow_tbl_bridge(rocker_port, trans, flags, addr, |
| 3525 | NULL, vlan_id, tunnel_id, goto_tbl, |
Scott Feldman | 6c70794 | 2014-11-28 14:34:28 +0100 | [diff] [blame] | 3526 | group_id, copy_to_cpu); |
| 3527 | if (err) |
| 3528 | return err; |
| 3529 | } |
| 3530 | |
Scott Feldman | 5111f80 | 2014-11-28 14:34:30 +0100 | [diff] [blame] | 3531 | if (!syncing) |
| 3532 | return 0; |
| 3533 | |
Scott Feldman | 6c70794 | 2014-11-28 14:34:28 +0100 | [diff] [blame] | 3534 | if (!rocker_port_is_bridged(rocker_port)) |
| 3535 | return 0; |
| 3536 | |
Scott Feldman | 179f9a2 | 2015-06-12 21:35:46 -0700 | [diff] [blame] | 3537 | lw = rocker_port_kzalloc(rocker_port, trans, flags, sizeof(*lw)); |
Scott Feldman | 6c70794 | 2014-11-28 14:34:28 +0100 | [diff] [blame] | 3538 | if (!lw) |
| 3539 | return -ENOMEM; |
| 3540 | |
| 3541 | INIT_WORK(&lw->work, rocker_port_fdb_learn_work); |
| 3542 | |
Scott Feldman | c4f2032 | 2015-05-10 09:47:50 -0700 | [diff] [blame] | 3543 | lw->rocker_port = rocker_port; |
| 3544 | lw->trans = trans; |
Scott Feldman | 6c70794 | 2014-11-28 14:34:28 +0100 | [diff] [blame] | 3545 | lw->flags = flags; |
| 3546 | ether_addr_copy(lw->addr, addr); |
| 3547 | lw->vid = rocker_port_vlan_to_vid(rocker_port, vlan_id); |
| 3548 | |
Scott Feldman | c4f2032 | 2015-05-10 09:47:50 -0700 | [diff] [blame] | 3549 | if (trans == SWITCHDEV_TRANS_PREPARE) |
Simon Horman | 0985df7 | 2015-05-25 14:28:35 +0900 | [diff] [blame] | 3550 | rocker_port_kfree(trans, lw); |
Scott Feldman | c4f2032 | 2015-05-10 09:47:50 -0700 | [diff] [blame] | 3551 | else |
| 3552 | schedule_work(&lw->work); |
Scott Feldman | 6c70794 | 2014-11-28 14:34:28 +0100 | [diff] [blame] | 3553 | |
| 3554 | return 0; |
| 3555 | } |
| 3556 | |
| 3557 | static struct rocker_fdb_tbl_entry * |
Simon Horman | e505464 | 2015-05-25 14:28:36 +0900 | [diff] [blame] | 3558 | rocker_fdb_tbl_find(const struct rocker *rocker, |
| 3559 | const struct rocker_fdb_tbl_entry *match) |
Scott Feldman | 6c70794 | 2014-11-28 14:34:28 +0100 | [diff] [blame] | 3560 | { |
| 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 | |
| 3570 | static int rocker_port_fdb(struct rocker_port *rocker_port, |
Scott Feldman | c4f2032 | 2015-05-10 09:47:50 -0700 | [diff] [blame] | 3571 | enum switchdev_trans trans, |
Scott Feldman | 6c70794 | 2014-11-28 14:34:28 +0100 | [diff] [blame] | 3572 | 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 Feldman | 179f9a2 | 2015-06-12 21:35:46 -0700 | [diff] [blame] | 3581 | fdb = rocker_port_kzalloc(rocker_port, trans, flags, sizeof(*fdb)); |
Scott Feldman | 6c70794 | 2014-11-28 14:34:28 +0100 | [diff] [blame] | 3582 | if (!fdb) |
| 3583 | return -ENOMEM; |
| 3584 | |
| 3585 | fdb->learned = (flags & ROCKER_OP_FLAG_LEARNED); |
Scott Feldman | 4a6bb6d | 2015-02-25 20:15:37 -0800 | [diff] [blame] | 3586 | fdb->key.pport = rocker_port->pport; |
Scott Feldman | 6c70794 | 2014-11-28 14:34:28 +0100 | [diff] [blame] | 3587 | 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 Horman | 0985df7 | 2015-05-25 14:28:35 +0900 | [diff] [blame] | 3596 | rocker_port_kfree(trans, fdb); |
Simon Horman | 42e9488 | 2015-05-21 12:40:15 +0900 | [diff] [blame] | 3597 | if (trans != SWITCHDEV_TRANS_PREPARE) |
| 3598 | hash_del(&found->entry); |
Scott Feldman | 6c70794 | 2014-11-28 14:34:28 +0100 | [diff] [blame] | 3599 | } else if (!removing && !found) { |
Simon Horman | 42e9488 | 2015-05-21 12:40:15 +0900 | [diff] [blame] | 3600 | if (trans != SWITCHDEV_TRANS_PREPARE) |
| 3601 | hash_add(rocker->fdb_tbl, &fdb->entry, fdb->key_crc32); |
Scott Feldman | 6c70794 | 2014-11-28 14:34:28 +0100 | [diff] [blame] | 3602 | } |
| 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 Horman | 0985df7 | 2015-05-25 14:28:35 +0900 | [diff] [blame] | 3608 | rocker_port_kfree(trans, fdb); |
Scott Feldman | 6c70794 | 2014-11-28 14:34:28 +0100 | [diff] [blame] | 3609 | if (!found && removing) |
| 3610 | return 0; |
| 3611 | /* Refreshing existing to update aging timers */ |
| 3612 | flags |= ROCKER_OP_FLAG_REFRESH; |
| 3613 | } |
| 3614 | |
Scott Feldman | c4f2032 | 2015-05-10 09:47:50 -0700 | [diff] [blame] | 3615 | return rocker_port_fdb_learn(rocker_port, trans, flags, addr, vlan_id); |
Scott Feldman | 6c70794 | 2014-11-28 14:34:28 +0100 | [diff] [blame] | 3616 | } |
| 3617 | |
Scott Feldman | c4f2032 | 2015-05-10 09:47:50 -0700 | [diff] [blame] | 3618 | static int rocker_port_fdb_flush(struct rocker_port *rocker_port, |
Scott Feldman | 179f9a2 | 2015-06-12 21:35:46 -0700 | [diff] [blame] | 3619 | enum switchdev_trans trans, int flags) |
Scott Feldman | 6c70794 | 2014-11-28 14:34:28 +0100 | [diff] [blame] | 3620 | { |
| 3621 | struct rocker *rocker = rocker_port->rocker; |
| 3622 | struct rocker_fdb_tbl_entry *found; |
| 3623 | unsigned long lock_flags; |
Scott Feldman | 6c70794 | 2014-11-28 14:34:28 +0100 | [diff] [blame] | 3624 | 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 Feldman | 179f9a2 | 2015-06-12 21:35:46 -0700 | [diff] [blame] | 3632 | flags |= ROCKER_OP_FLAG_REMOVE; |
| 3633 | |
Scott Feldman | 6c70794 | 2014-11-28 14:34:28 +0100 | [diff] [blame] | 3634 | spin_lock_irqsave(&rocker->fdb_tbl_lock, lock_flags); |
| 3635 | |
| 3636 | hash_for_each_safe(rocker->fdb_tbl, bkt, tmp, found, entry) { |
Scott Feldman | 4a6bb6d | 2015-02-25 20:15:37 -0800 | [diff] [blame] | 3637 | if (found->key.pport != rocker_port->pport) |
Scott Feldman | 6c70794 | 2014-11-28 14:34:28 +0100 | [diff] [blame] | 3638 | continue; |
| 3639 | if (!found->learned) |
| 3640 | continue; |
Scott Feldman | c4f2032 | 2015-05-10 09:47:50 -0700 | [diff] [blame] | 3641 | err = rocker_port_fdb_learn(rocker_port, trans, flags, |
Scott Feldman | 6c70794 | 2014-11-28 14:34:28 +0100 | [diff] [blame] | 3642 | found->key.addr, |
| 3643 | found->key.vlan_id); |
| 3644 | if (err) |
| 3645 | goto err_out; |
Simon Horman | 3098ac3 | 2015-05-21 12:40:14 +0900 | [diff] [blame] | 3646 | if (trans != SWITCHDEV_TRANS_PREPARE) |
| 3647 | hash_del(&found->entry); |
Scott Feldman | 6c70794 | 2014-11-28 14:34:28 +0100 | [diff] [blame] | 3648 | } |
| 3649 | |
| 3650 | err_out: |
| 3651 | spin_unlock_irqrestore(&rocker->fdb_tbl_lock, lock_flags); |
| 3652 | |
| 3653 | return err; |
| 3654 | } |
| 3655 | |
Scott Feldman | 9f6bbf7 | 2014-11-28 14:34:27 +0100 | [diff] [blame] | 3656 | static int rocker_port_router_mac(struct rocker_port *rocker_port, |
Scott Feldman | c4f2032 | 2015-05-10 09:47:50 -0700 | [diff] [blame] | 3657 | enum switchdev_trans trans, int flags, |
| 3658 | __be16 vlan_id) |
Scott Feldman | 9f6bbf7 | 2014-11-28 14:34:27 +0100 | [diff] [blame] | 3659 | { |
Scott Feldman | 4a6bb6d | 2015-02-25 20:15:37 -0800 | [diff] [blame] | 3660 | u32 in_pport_mask = 0xffffffff; |
Scott Feldman | 9f6bbf7 | 2014-11-28 14:34:27 +0100 | [diff] [blame] | 3661 | __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 Feldman | c4f2032 | 2015-05-10 09:47:50 -0700 | [diff] [blame] | 3671 | err = rocker_flow_tbl_term_mac(rocker_port, trans, |
Scott Feldman | 4a6bb6d | 2015-02-25 20:15:37 -0800 | [diff] [blame] | 3672 | rocker_port->pport, in_pport_mask, |
Scott Feldman | 9f6bbf7 | 2014-11-28 14:34:27 +0100 | [diff] [blame] | 3673 | 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 Feldman | c4f2032 | 2015-05-10 09:47:50 -0700 | [diff] [blame] | 3680 | err = rocker_flow_tbl_term_mac(rocker_port, trans, |
Scott Feldman | 4a6bb6d | 2015-02-25 20:15:37 -0800 | [diff] [blame] | 3681 | rocker_port->pport, in_pport_mask, |
Scott Feldman | 9f6bbf7 | 2014-11-28 14:34:27 +0100 | [diff] [blame] | 3682 | 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 Feldman | c4f2032 | 2015-05-10 09:47:50 -0700 | [diff] [blame] | 3689 | static int rocker_port_fwding(struct rocker_port *rocker_port, |
Scott Feldman | 179f9a2 | 2015-06-12 21:35:46 -0700 | [diff] [blame] | 3690 | enum switchdev_trans trans, int flags) |
Scott Feldman | 6c70794 | 2014-11-28 14:34:28 +0100 | [diff] [blame] | 3691 | { |
| 3692 | bool pop_vlan; |
Scott Feldman | 4a6bb6d | 2015-02-25 20:15:37 -0800 | [diff] [blame] | 3693 | u32 out_pport; |
Scott Feldman | 6c70794 | 2014-11-28 14:34:28 +0100 | [diff] [blame] | 3694 | __be16 vlan_id; |
| 3695 | u16 vid; |
Scott Feldman | 6c70794 | 2014-11-28 14:34:28 +0100 | [diff] [blame] | 3696 | 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 Feldman | 4a6bb6d | 2015-02-25 20:15:37 -0800 | [diff] [blame] | 3709 | out_pport = rocker_port->pport; |
Scott Feldman | 6c70794 | 2014-11-28 14:34:28 +0100 | [diff] [blame] | 3710 | 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 Feldman | c4f2032 | 2015-05-10 09:47:50 -0700 | [diff] [blame] | 3715 | err = rocker_group_l2_interface(rocker_port, trans, flags, |
| 3716 | vlan_id, out_pport, pop_vlan); |
Scott Feldman | 6c70794 | 2014-11-28 14:34:28 +0100 | [diff] [blame] | 3717 | if (err) { |
| 3718 | netdev_err(rocker_port->dev, |
Scott Feldman | 4a6bb6d | 2015-02-25 20:15:37 -0800 | [diff] [blame] | 3719 | "Error (%d) port VLAN l2 group for pport %d\n", |
| 3720 | err, out_pport); |
Scott Feldman | 6c70794 | 2014-11-28 14:34:28 +0100 | [diff] [blame] | 3721 | return err; |
| 3722 | } |
| 3723 | } |
| 3724 | |
| 3725 | return 0; |
| 3726 | } |
| 3727 | |
Scott Feldman | c4f2032 | 2015-05-10 09:47:50 -0700 | [diff] [blame] | 3728 | static int rocker_port_stp_update(struct rocker_port *rocker_port, |
Scott Feldman | 179f9a2 | 2015-06-12 21:35:46 -0700 | [diff] [blame] | 3729 | enum switchdev_trans trans, int flags, |
| 3730 | u8 state) |
Scott Feldman | 6c70794 | 2014-11-28 14:34:28 +0100 | [diff] [blame] | 3731 | { |
| 3732 | bool want[ROCKER_CTRL_MAX] = { 0, }; |
Scott Feldman | c4f2032 | 2015-05-10 09:47:50 -0700 | [diff] [blame] | 3733 | bool prev_ctrls[ROCKER_CTRL_MAX]; |
| 3734 | u8 prev_state; |
Scott Feldman | 6c70794 | 2014-11-28 14:34:28 +0100 | [diff] [blame] | 3735 | int err; |
| 3736 | int i; |
| 3737 | |
Scott Feldman | c4f2032 | 2015-05-10 09:47:50 -0700 | [diff] [blame] | 3738 | 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 Feldman | 6c70794 | 2014-11-28 14:34:28 +0100 | [diff] [blame] | 3743 | 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 Feldman | 179f9a2 | 2015-06-12 21:35:46 -0700 | [diff] [blame] | 3770 | int ctrl_flags = flags | |
| 3771 | (want[i] ? 0 : ROCKER_OP_FLAG_REMOVE); |
| 3772 | err = rocker_port_ctrl(rocker_port, trans, ctrl_flags, |
Scott Feldman | 6c70794 | 2014-11-28 14:34:28 +0100 | [diff] [blame] | 3773 | &rocker_ctrls[i]); |
| 3774 | if (err) |
Scott Feldman | c4f2032 | 2015-05-10 09:47:50 -0700 | [diff] [blame] | 3775 | goto err_out; |
Scott Feldman | 6c70794 | 2014-11-28 14:34:28 +0100 | [diff] [blame] | 3776 | rocker_port->ctrls[i] = want[i]; |
| 3777 | } |
| 3778 | } |
| 3779 | |
Scott Feldman | 179f9a2 | 2015-06-12 21:35:46 -0700 | [diff] [blame] | 3780 | err = rocker_port_fdb_flush(rocker_port, trans, flags); |
Scott Feldman | 6c70794 | 2014-11-28 14:34:28 +0100 | [diff] [blame] | 3781 | if (err) |
Scott Feldman | c4f2032 | 2015-05-10 09:47:50 -0700 | [diff] [blame] | 3782 | goto err_out; |
Scott Feldman | 6c70794 | 2014-11-28 14:34:28 +0100 | [diff] [blame] | 3783 | |
Scott Feldman | 179f9a2 | 2015-06-12 21:35:46 -0700 | [diff] [blame] | 3784 | err = rocker_port_fwding(rocker_port, trans, flags); |
Scott Feldman | c4f2032 | 2015-05-10 09:47:50 -0700 | [diff] [blame] | 3785 | |
| 3786 | err_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 Feldman | 6c70794 | 2014-11-28 14:34:28 +0100 | [diff] [blame] | 3793 | } |
| 3794 | |
Scott Feldman | c4f2032 | 2015-05-10 09:47:50 -0700 | [diff] [blame] | 3795 | static int rocker_port_fwd_enable(struct rocker_port *rocker_port, |
Scott Feldman | 179f9a2 | 2015-06-12 21:35:46 -0700 | [diff] [blame] | 3796 | enum switchdev_trans trans, int flags) |
Scott Feldman | e47172a | 2015-02-25 20:15:38 -0800 | [diff] [blame] | 3797 | { |
| 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 Feldman | 179f9a2 | 2015-06-12 21:35:46 -0700 | [diff] [blame] | 3803 | return rocker_port_stp_update(rocker_port, trans, flags, |
| 3804 | BR_STATE_FORWARDING); |
Scott Feldman | e47172a | 2015-02-25 20:15:38 -0800 | [diff] [blame] | 3805 | } |
| 3806 | |
Scott Feldman | c4f2032 | 2015-05-10 09:47:50 -0700 | [diff] [blame] | 3807 | static int rocker_port_fwd_disable(struct rocker_port *rocker_port, |
Scott Feldman | 179f9a2 | 2015-06-12 21:35:46 -0700 | [diff] [blame] | 3808 | enum switchdev_trans trans, int flags) |
Scott Feldman | e47172a | 2015-02-25 20:15:38 -0800 | [diff] [blame] | 3809 | { |
| 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 Feldman | 179f9a2 | 2015-06-12 21:35:46 -0700 | [diff] [blame] | 3815 | return rocker_port_stp_update(rocker_port, trans, flags, |
| 3816 | BR_STATE_DISABLED); |
Scott Feldman | e47172a | 2015-02-25 20:15:38 -0800 | [diff] [blame] | 3817 | } |
| 3818 | |
Scott Feldman | 9f6bbf7 | 2014-11-28 14:34:27 +0100 | [diff] [blame] | 3819 | static struct rocker_internal_vlan_tbl_entry * |
Simon Horman | e505464 | 2015-05-25 14:28:36 +0900 | [diff] [blame] | 3820 | rocker_internal_vlan_tbl_find(const struct rocker *rocker, int ifindex) |
Scott Feldman | 9f6bbf7 | 2014-11-28 14:34:27 +0100 | [diff] [blame] | 3821 | { |
| 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 | |
| 3833 | static __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 Horman | df6a206 | 2015-05-21 12:40:17 +0900 | [diff] [blame] | 3842 | entry = kzalloc(sizeof(*entry), GFP_KERNEL); |
Scott Feldman | 9f6bbf7 | 2014-11-28 14:34:27 +0100 | [diff] [blame] | 3843 | 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 Horman | df6a206 | 2015-05-21 12:40:17 +0900 | [diff] [blame] | 3852 | kfree(entry); |
Scott Feldman | 9f6bbf7 | 2014-11-28 14:34:27 +0100 | [diff] [blame] | 3853 | 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 | |
| 3868 | found: |
| 3869 | found->ref_count++; |
| 3870 | spin_unlock_irqrestore(&rocker->internal_vlan_tbl_lock, lock_flags); |
| 3871 | |
| 3872 | return found->vlan_id; |
| 3873 | } |
| 3874 | |
Simon Horman | e505464 | 2015-05-25 14:28:36 +0900 | [diff] [blame] | 3875 | static void |
| 3876 | rocker_port_internal_vlan_id_put(const struct rocker_port *rocker_port, |
| 3877 | int ifindex) |
Scott Feldman | 9f6bbf7 | 2014-11-28 14:34:27 +0100 | [diff] [blame] | 3878 | { |
| 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 Horman | df6a206 | 2015-05-21 12:40:17 +0900 | [diff] [blame] | 3898 | kfree(found); |
Scott Feldman | 9f6bbf7 | 2014-11-28 14:34:27 +0100 | [diff] [blame] | 3899 | } |
| 3900 | |
| 3901 | not_found: |
| 3902 | spin_unlock_irqrestore(&rocker->internal_vlan_tbl_lock, lock_flags); |
| 3903 | } |
| 3904 | |
Scott Feldman | c4f2032 | 2015-05-10 09:47:50 -0700 | [diff] [blame] | 3905 | static int rocker_port_fib_ipv4(struct rocker_port *rocker_port, |
| 3906 | enum switchdev_trans trans, __be32 dst, |
Simon Horman | e505464 | 2015-05-25 14:28:36 +0900 | [diff] [blame] | 3907 | int dst_len, const struct fib_info *fi, |
| 3908 | u32 tb_id, int flags) |
Scott Feldman | c1beeef | 2015-03-05 21:21:20 -0800 | [diff] [blame] | 3909 | { |
Simon Horman | e505464 | 2015-05-25 14:28:36 +0900 | [diff] [blame] | 3910 | const struct fib_nh *nh; |
Scott Feldman | c1beeef | 2015-03-05 21:21:20 -0800 | [diff] [blame] | 3911 | __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 Feldman | c4f2032 | 2015-05-10 09:47:50 -0700 | [diff] [blame] | 3930 | err = rocker_port_ipv4_nh(rocker_port, trans, flags, |
Scott Feldman | c1beeef | 2015-03-05 21:21:20 -0800 | [diff] [blame] | 3931 | 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 Feldman | c4f2032 | 2015-05-10 09:47:50 -0700 | [diff] [blame] | 3941 | err = rocker_flow_tbl_ucast4_routing(rocker_port, trans, eth_type, dst, |
Scott Feldman | c1beeef | 2015-03-05 21:21:20 -0800 | [diff] [blame] | 3942 | 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 Pirko | 4b8ac96 | 2014-11-28 14:34:26 +0100 | [diff] [blame] | 3951 | /***************** |
| 3952 | * Net device ops |
| 3953 | *****************/ |
| 3954 | |
| 3955 | static 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 Feldman | 179f9a2 | 2015-06-12 21:35:46 -0700 | [diff] [blame] | 3980 | err = rocker_port_fwd_enable(rocker_port, SWITCHDEV_TRANS_NONE, 0); |
Scott Feldman | 6c70794 | 2014-11-28 14:34:28 +0100 | [diff] [blame] | 3981 | if (err) |
Scott Feldman | e47172a | 2015-02-25 20:15:38 -0800 | [diff] [blame] | 3982 | goto err_fwd_enable; |
Scott Feldman | 6c70794 | 2014-11-28 14:34:28 +0100 | [diff] [blame] | 3983 | |
Jiri Pirko | 4b8ac96 | 2014-11-28 14:34:26 +0100 | [diff] [blame] | 3984 | 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 Feldman | e47172a | 2015-02-25 20:15:38 -0800 | [diff] [blame] | 3990 | err_fwd_enable: |
Scott Feldman | 6c70794 | 2014-11-28 14:34:28 +0100 | [diff] [blame] | 3991 | free_irq(rocker_msix_rx_vector(rocker_port), rocker_port); |
Jiri Pirko | 4b8ac96 | 2014-11-28 14:34:26 +0100 | [diff] [blame] | 3992 | err_request_rx_irq: |
| 3993 | free_irq(rocker_msix_tx_vector(rocker_port), rocker_port); |
| 3994 | err_request_tx_irq: |
| 3995 | rocker_port_dma_rings_fini(rocker_port); |
| 3996 | return err; |
| 3997 | } |
| 3998 | |
| 3999 | static 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 Feldman | f66feaa | 2015-06-12 21:35:50 -0700 | [diff] [blame] | 4007 | rocker_port_fwd_disable(rocker_port, SWITCHDEV_TRANS_NONE, |
| 4008 | ROCKER_OP_FLAG_NOWAIT); |
Jiri Pirko | 4b8ac96 | 2014-11-28 14:34:26 +0100 | [diff] [blame] | 4009 | 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 Horman | e505464 | 2015-05-25 14:28:36 +0900 | [diff] [blame] | 4016 | static void rocker_tx_desc_frags_unmap(const struct rocker_port *rocker_port, |
| 4017 | const struct rocker_desc_info *desc_info) |
Jiri Pirko | 4b8ac96 | 2014-11-28 14:34:26 +0100 | [diff] [blame] | 4018 | { |
Simon Horman | e505464 | 2015-05-25 14:28:36 +0900 | [diff] [blame] | 4019 | const struct rocker *rocker = rocker_port->rocker; |
Jiri Pirko | 4b8ac96 | 2014-11-28 14:34:26 +0100 | [diff] [blame] | 4020 | struct pci_dev *pdev = rocker->pdev; |
Simon Horman | e505464 | 2015-05-25 14:28:36 +0900 | [diff] [blame] | 4021 | const struct rocker_tlv *attrs[ROCKER_TLV_TX_MAX + 1]; |
Jiri Pirko | 4b8ac96 | 2014-11-28 14:34:26 +0100 | [diff] [blame] | 4022 | 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 Horman | e505464 | 2015-05-25 14:28:36 +0900 | [diff] [blame] | 4029 | const struct rocker_tlv *frag_attrs[ROCKER_TLV_TX_FRAG_ATTR_MAX + 1]; |
Jiri Pirko | 4b8ac96 | 2014-11-28 14:34:26 +0100 | [diff] [blame] | 4030 | 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 Horman | e505464 | 2015-05-25 14:28:36 +0900 | [diff] [blame] | 4046 | static int rocker_tx_desc_frag_map_put(const struct rocker_port *rocker_port, |
Jiri Pirko | 4b8ac96 | 2014-11-28 14:34:26 +0100 | [diff] [blame] | 4047 | struct rocker_desc_info *desc_info, |
| 4048 | char *buf, size_t buf_len) |
| 4049 | { |
Simon Horman | e505464 | 2015-05-25 14:28:36 +0900 | [diff] [blame] | 4050 | const struct rocker *rocker = rocker_port->rocker; |
Jiri Pirko | 4b8ac96 | 2014-11-28 14:34:26 +0100 | [diff] [blame] | 4051 | 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 | |
| 4073 | nest_cancel: |
| 4074 | rocker_tlv_nest_cancel(desc_info, frag); |
| 4075 | unmap_frag: |
| 4076 | pci_unmap_single(pdev, dma_handle, buf_len, DMA_TO_DEVICE); |
| 4077 | return -EMSGSIZE; |
| 4078 | } |
| 4079 | |
| 4080 | static 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 | |
| 4128 | unmap_frags: |
| 4129 | rocker_tx_desc_frags_unmap(rocker_port, desc_info); |
| 4130 | nest_cancel: |
| 4131 | rocker_tlv_nest_cancel(desc_info, frags); |
| 4132 | out: |
| 4133 | dev_kfree_skb(skb); |
David Ahern | f2bbca5 | 2015-01-16 14:22:29 -0700 | [diff] [blame] | 4134 | dev->stats.tx_dropped++; |
| 4135 | |
Jiri Pirko | 4b8ac96 | 2014-11-28 14:34:26 +0100 | [diff] [blame] | 4136 | return NETDEV_TX_OK; |
| 4137 | } |
| 4138 | |
| 4139 | static 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 Ahern | db19170 | 2015-03-17 20:23:16 -0600 | [diff] [blame] | 4155 | static 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 Feldman | 179f9a2 | 2015-06-12 21:35:46 -0700 | [diff] [blame] | 4162 | err = rocker_cmd_exec(rocker_port, SWITCHDEV_TRANS_NONE, 0, |
David Ahern | db19170 | 2015-03-17 20:23:16 -0600 | [diff] [blame] | 4163 | rocker_cmd_get_port_settings_prep, NULL, |
| 4164 | rocker_cmd_get_port_settings_phys_name_proc, |
Scott Feldman | c4f2032 | 2015-05-10 09:47:50 -0700 | [diff] [blame] | 4165 | &name); |
David Ahern | db19170 | 2015-03-17 20:23:16 -0600 | [diff] [blame] | 4166 | |
| 4167 | return err ? -EOPNOTSUPP : 0; |
| 4168 | } |
| 4169 | |
Jiri Pirko | 4b8ac96 | 2014-11-28 14:34:26 +0100 | [diff] [blame] | 4170 | static 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 Feldman | 85fdb95 | 2015-05-10 09:48:05 -0700 | [diff] [blame] | 4175 | .ndo_bridge_getlink = switchdev_port_bridge_getlink, |
Scott Feldman | fc8f40d | 2015-05-10 09:47:57 -0700 | [diff] [blame] | 4176 | .ndo_bridge_setlink = switchdev_port_bridge_setlink, |
Scott Feldman | 54ba5a0 | 2015-05-10 09:48:01 -0700 | [diff] [blame] | 4177 | .ndo_bridge_dellink = switchdev_port_bridge_dellink, |
Samudrala, Sridhar | 45d4122 | 2015-05-13 21:55:43 -0700 | [diff] [blame] | 4178 | .ndo_fdb_add = switchdev_port_fdb_add, |
| 4179 | .ndo_fdb_del = switchdev_port_fdb_del, |
| 4180 | .ndo_fdb_dump = switchdev_port_fdb_dump, |
David Ahern | db19170 | 2015-03-17 20:23:16 -0600 | [diff] [blame] | 4181 | .ndo_get_phys_port_name = rocker_port_get_phys_port_name, |
Scott Feldman | 98237d4 | 2015-03-15 21:07:15 -0700 | [diff] [blame] | 4182 | }; |
| 4183 | |
| 4184 | /******************** |
| 4185 | * swdev interface |
| 4186 | ********************/ |
| 4187 | |
Scott Feldman | f8e20a9 | 2015-05-10 09:47:49 -0700 | [diff] [blame] | 4188 | static int rocker_port_attr_get(struct net_device *dev, |
| 4189 | struct switchdev_attr *attr) |
Scott Feldman | 98237d4 | 2015-03-15 21:07:15 -0700 | [diff] [blame] | 4190 | { |
Simon Horman | e505464 | 2015-05-25 14:28:36 +0900 | [diff] [blame] | 4191 | const struct rocker_port *rocker_port = netdev_priv(dev); |
| 4192 | const struct rocker *rocker = rocker_port->rocker; |
Scott Feldman | 98237d4 | 2015-03-15 21:07:15 -0700 | [diff] [blame] | 4193 | |
Scott Feldman | f8e20a9 | 2015-05-10 09:47:49 -0700 | [diff] [blame] | 4194 | switch (attr->id) { |
| 4195 | case SWITCHDEV_ATTR_PORT_PARENT_ID: |
Scott Feldman | 42275bd | 2015-05-13 11:16:50 -0700 | [diff] [blame] | 4196 | attr->u.ppid.id_len = sizeof(rocker->hw.id); |
| 4197 | memcpy(&attr->u.ppid.id, &rocker->hw.id, attr->u.ppid.id_len); |
Scott Feldman | f8e20a9 | 2015-05-10 09:47:49 -0700 | [diff] [blame] | 4198 | break; |
Scott Feldman | 6004c86 | 2015-05-10 09:47:55 -0700 | [diff] [blame] | 4199 | case SWITCHDEV_ATTR_PORT_BRIDGE_FLAGS: |
Scott Feldman | 42275bd | 2015-05-13 11:16:50 -0700 | [diff] [blame] | 4200 | attr->u.brport_flags = rocker_port->brport_flags; |
Scott Feldman | 6004c86 | 2015-05-10 09:47:55 -0700 | [diff] [blame] | 4201 | break; |
Scott Feldman | f8e20a9 | 2015-05-10 09:47:49 -0700 | [diff] [blame] | 4202 | default: |
| 4203 | return -EOPNOTSUPP; |
| 4204 | } |
| 4205 | |
Scott Feldman | 98237d4 | 2015-03-15 21:07:15 -0700 | [diff] [blame] | 4206 | return 0; |
| 4207 | } |
| 4208 | |
Simon Horman | e505464 | 2015-05-25 14:28:36 +0900 | [diff] [blame] | 4209 | static void rocker_port_trans_abort(const struct rocker_port *rocker_port) |
Scott Feldman | c4f2032 | 2015-05-10 09:47:50 -0700 | [diff] [blame] | 4210 | { |
| 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 Feldman | 6004c86 | 2015-05-10 09:47:55 -0700 | [diff] [blame] | 4219 | static 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 Feldman | c4f2032 | 2015-05-10 09:47:50 -0700 | [diff] [blame] | 4237 | static 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 Feldman | 3563606 | 2015-05-10 09:47:51 -0700 | [diff] [blame] | 4255 | case SWITCHDEV_ATTR_PORT_STP_STATE: |
Scott Feldman | ac28393 | 2015-06-12 21:35:48 -0700 | [diff] [blame] | 4256 | err = rocker_port_stp_update(rocker_port, attr->trans, |
| 4257 | ROCKER_OP_FLAG_NOWAIT, |
Scott Feldman | 42275bd | 2015-05-13 11:16:50 -0700 | [diff] [blame] | 4258 | attr->u.stp_state); |
Scott Feldman | 3563606 | 2015-05-10 09:47:51 -0700 | [diff] [blame] | 4259 | break; |
Scott Feldman | 6004c86 | 2015-05-10 09:47:55 -0700 | [diff] [blame] | 4260 | case SWITCHDEV_ATTR_PORT_BRIDGE_FLAGS: |
| 4261 | err = rocker_port_brport_flags_set(rocker_port, attr->trans, |
Scott Feldman | 42275bd | 2015-05-13 11:16:50 -0700 | [diff] [blame] | 4262 | attr->u.brport_flags); |
Scott Feldman | 6004c86 | 2015-05-10 09:47:55 -0700 | [diff] [blame] | 4263 | break; |
Scott Feldman | c4f2032 | 2015-05-10 09:47:50 -0700 | [diff] [blame] | 4264 | default: |
| 4265 | err = -EOPNOTSUPP; |
| 4266 | break; |
| 4267 | } |
| 4268 | |
| 4269 | return err; |
Scott Feldman | 98237d4 | 2015-03-15 21:07:15 -0700 | [diff] [blame] | 4270 | } |
| 4271 | |
Scott Feldman | 9228ad2 | 2015-05-10 09:47:54 -0700 | [diff] [blame] | 4272 | static 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 Feldman | cec04a6 | 2015-06-01 11:39:03 -0700 | [diff] [blame] | 4283 | 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 Feldman | 9228ad2 | 2015-05-10 09:47:54 -0700 | [diff] [blame] | 4289 | } |
| 4290 | |
| 4291 | static int rocker_port_vlans_add(struct rocker_port *rocker_port, |
| 4292 | enum switchdev_trans trans, |
Simon Horman | e505464 | 2015-05-25 14:28:36 +0900 | [diff] [blame] | 4293 | const struct switchdev_obj_vlan *vlan) |
Scott Feldman | 9228ad2 | 2015-05-10 09:47:54 -0700 | [diff] [blame] | 4294 | { |
| 4295 | u16 vid; |
| 4296 | int err; |
| 4297 | |
Scott Feldman | 3e3a78b | 2015-06-22 00:27:16 -0700 | [diff] [blame^] | 4298 | for (vid = vlan->vid_begin; vid <= vlan->vid_end; vid++) { |
Scott Feldman | 9228ad2 | 2015-05-10 09:47:54 -0700 | [diff] [blame] | 4299 | 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, Sridhar | 45d4122 | 2015-05-13 21:55:43 -0700 | [diff] [blame] | 4308 | static int rocker_port_fdb_add(struct rocker_port *rocker_port, |
| 4309 | enum switchdev_trans trans, |
Simon Horman | e505464 | 2015-05-25 14:28:36 +0900 | [diff] [blame] | 4310 | const struct switchdev_obj_fdb *fdb) |
Samudrala, Sridhar | 45d4122 | 2015-05-13 21:55:43 -0700 | [diff] [blame] | 4311 | { |
| 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 Feldman | 9228ad2 | 2015-05-10 09:47:54 -0700 | [diff] [blame] | 4321 | static 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 Horman | e505464 | 2015-05-25 14:28:36 +0900 | [diff] [blame] | 4325 | const struct switchdev_obj_ipv4_fib *fib4; |
Scott Feldman | 9228ad2 | 2015-05-10 09:47:54 -0700 | [diff] [blame] | 4326 | 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 Feldman | 42275bd | 2015-05-13 11:16:50 -0700 | [diff] [blame] | 4342 | &obj->u.vlan); |
Scott Feldman | 9228ad2 | 2015-05-10 09:47:54 -0700 | [diff] [blame] | 4343 | break; |
Scott Feldman | 58c2cb1 | 2015-05-10 09:48:06 -0700 | [diff] [blame] | 4344 | case SWITCHDEV_OBJ_IPV4_FIB: |
Scott Feldman | 42275bd | 2015-05-13 11:16:50 -0700 | [diff] [blame] | 4345 | fib4 = &obj->u.ipv4_fib; |
Scott Feldman | 58c2cb1 | 2015-05-10 09:48:06 -0700 | [diff] [blame] | 4346 | err = rocker_port_fib_ipv4(rocker_port, obj->trans, |
Scott Feldman | 7a7ee53 | 2015-05-12 23:03:52 -0700 | [diff] [blame] | 4347 | htonl(fib4->dst), fib4->dst_len, |
Scott Feldman | 58c2cb1 | 2015-05-10 09:48:06 -0700 | [diff] [blame] | 4348 | fib4->fi, fib4->tb_id, 0); |
| 4349 | break; |
Samudrala, Sridhar | 45d4122 | 2015-05-13 21:55:43 -0700 | [diff] [blame] | 4350 | case SWITCHDEV_OBJ_PORT_FDB: |
| 4351 | err = rocker_port_fdb_add(rocker_port, obj->trans, &obj->u.fdb); |
| 4352 | break; |
Scott Feldman | 9228ad2 | 2015-05-10 09:47:54 -0700 | [diff] [blame] | 4353 | default: |
| 4354 | err = -EOPNOTSUPP; |
| 4355 | break; |
| 4356 | } |
| 4357 | |
| 4358 | return err; |
| 4359 | } |
| 4360 | |
| 4361 | static 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 | |
| 4375 | static int rocker_port_vlans_del(struct rocker_port *rocker_port, |
Simon Horman | e505464 | 2015-05-25 14:28:36 +0900 | [diff] [blame] | 4376 | const struct switchdev_obj_vlan *vlan) |
Scott Feldman | 9228ad2 | 2015-05-10 09:47:54 -0700 | [diff] [blame] | 4377 | { |
| 4378 | u16 vid; |
| 4379 | int err; |
| 4380 | |
Scott Feldman | 3e3a78b | 2015-06-22 00:27:16 -0700 | [diff] [blame^] | 4381 | for (vid = vlan->vid_begin; vid <= vlan->vid_end; vid++) { |
Scott Feldman | 9228ad2 | 2015-05-10 09:47:54 -0700 | [diff] [blame] | 4382 | err = rocker_port_vlan_del(rocker_port, vid, vlan->flags); |
| 4383 | if (err) |
| 4384 | return err; |
| 4385 | } |
| 4386 | |
| 4387 | return 0; |
| 4388 | } |
| 4389 | |
Samudrala, Sridhar | 45d4122 | 2015-05-13 21:55:43 -0700 | [diff] [blame] | 4390 | static int rocker_port_fdb_del(struct rocker_port *rocker_port, |
| 4391 | enum switchdev_trans trans, |
Simon Horman | e505464 | 2015-05-25 14:28:36 +0900 | [diff] [blame] | 4392 | const struct switchdev_obj_fdb *fdb) |
Samudrala, Sridhar | 45d4122 | 2015-05-13 21:55:43 -0700 | [diff] [blame] | 4393 | { |
| 4394 | __be16 vlan_id = rocker_port_vid_to_vlan(rocker_port, fdb->vid, NULL); |
Scott Feldman | b4ad7ba | 2015-06-14 11:33:11 -0700 | [diff] [blame] | 4395 | int flags = ROCKER_OP_FLAG_NOWAIT | ROCKER_OP_FLAG_REMOVE; |
Samudrala, Sridhar | 45d4122 | 2015-05-13 21:55:43 -0700 | [diff] [blame] | 4396 | |
| 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 Feldman | 9228ad2 | 2015-05-10 09:47:54 -0700 | [diff] [blame] | 4403 | static 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 Horman | e505464 | 2015-05-25 14:28:36 +0900 | [diff] [blame] | 4407 | const struct switchdev_obj_ipv4_fib *fib4; |
Scott Feldman | 9228ad2 | 2015-05-10 09:47:54 -0700 | [diff] [blame] | 4408 | int err = 0; |
| 4409 | |
| 4410 | switch (obj->id) { |
| 4411 | case SWITCHDEV_OBJ_PORT_VLAN: |
Scott Feldman | 42275bd | 2015-05-13 11:16:50 -0700 | [diff] [blame] | 4412 | err = rocker_port_vlans_del(rocker_port, &obj->u.vlan); |
Scott Feldman | 9228ad2 | 2015-05-10 09:47:54 -0700 | [diff] [blame] | 4413 | break; |
Scott Feldman | 58c2cb1 | 2015-05-10 09:48:06 -0700 | [diff] [blame] | 4414 | case SWITCHDEV_OBJ_IPV4_FIB: |
Scott Feldman | 42275bd | 2015-05-13 11:16:50 -0700 | [diff] [blame] | 4415 | fib4 = &obj->u.ipv4_fib; |
Scott Feldman | 58c2cb1 | 2015-05-10 09:48:06 -0700 | [diff] [blame] | 4416 | err = rocker_port_fib_ipv4(rocker_port, SWITCHDEV_TRANS_NONE, |
Scott Feldman | 7a7ee53 | 2015-05-12 23:03:52 -0700 | [diff] [blame] | 4417 | htonl(fib4->dst), fib4->dst_len, |
| 4418 | fib4->fi, fib4->tb_id, |
| 4419 | ROCKER_OP_FLAG_REMOVE); |
Scott Feldman | 58c2cb1 | 2015-05-10 09:48:06 -0700 | [diff] [blame] | 4420 | break; |
Samudrala, Sridhar | 45d4122 | 2015-05-13 21:55:43 -0700 | [diff] [blame] | 4421 | 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 Horman | e505464 | 2015-05-25 14:28:36 +0900 | [diff] [blame] | 4432 | static int rocker_port_fdb_dump(const struct rocker_port *rocker_port, |
Samudrala, Sridhar | 45d4122 | 2015-05-13 21:55:43 -0700 | [diff] [blame] | 4433 | 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 | |
| 4459 | static int rocker_port_obj_dump(struct net_device *dev, |
| 4460 | struct switchdev_obj *obj) |
| 4461 | { |
Simon Horman | e505464 | 2015-05-25 14:28:36 +0900 | [diff] [blame] | 4462 | const struct rocker_port *rocker_port = netdev_priv(dev); |
Samudrala, Sridhar | 45d4122 | 2015-05-13 21:55:43 -0700 | [diff] [blame] | 4463 | 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 Feldman | 9228ad2 | 2015-05-10 09:47:54 -0700 | [diff] [blame] | 4469 | default: |
| 4470 | err = -EOPNOTSUPP; |
| 4471 | break; |
| 4472 | } |
| 4473 | |
| 4474 | return err; |
| 4475 | } |
| 4476 | |
Jiri Pirko | 9d47c0a | 2015-05-10 09:47:47 -0700 | [diff] [blame] | 4477 | static const struct switchdev_ops rocker_port_switchdev_ops = { |
Scott Feldman | f8e20a9 | 2015-05-10 09:47:49 -0700 | [diff] [blame] | 4478 | .switchdev_port_attr_get = rocker_port_attr_get, |
Scott Feldman | 3563606 | 2015-05-10 09:47:51 -0700 | [diff] [blame] | 4479 | .switchdev_port_attr_set = rocker_port_attr_set, |
Scott Feldman | 9228ad2 | 2015-05-10 09:47:54 -0700 | [diff] [blame] | 4480 | .switchdev_port_obj_add = rocker_port_obj_add, |
| 4481 | .switchdev_port_obj_del = rocker_port_obj_del, |
Samudrala, Sridhar | 45d4122 | 2015-05-13 21:55:43 -0700 | [diff] [blame] | 4482 | .switchdev_port_obj_dump = rocker_port_obj_dump, |
Jiri Pirko | 4b8ac96 | 2014-11-28 14:34:26 +0100 | [diff] [blame] | 4483 | }; |
| 4484 | |
| 4485 | /******************** |
| 4486 | * ethtool interface |
| 4487 | ********************/ |
| 4488 | |
| 4489 | static 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 | |
| 4497 | static 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 | |
| 4505 | static 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 Ahern | 9766e97 | 2015-01-29 20:59:33 -0700 | [diff] [blame] | 4512 | static 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 | |
| 4529 | static 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 | |
| 4545 | static int |
Simon Horman | 534ba6a | 2015-06-01 13:25:04 +0900 | [diff] [blame] | 4546 | rocker_cmd_get_port_stats_prep(const struct rocker_port *rocker_port, |
David Ahern | 9766e97 | 2015-01-29 20:59:33 -0700 | [diff] [blame] | 4547 | 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 Feldman | 4a6bb6d | 2015-02-25 20:15:37 -0800 | [diff] [blame] | 4560 | if (rocker_tlv_put_u32(desc_info, ROCKER_TLV_CMD_PORT_STATS_PPORT, |
| 4561 | rocker_port->pport)) |
David Ahern | 9766e97 | 2015-01-29 20:59:33 -0700 | [diff] [blame] | 4562 | return -EMSGSIZE; |
| 4563 | |
| 4564 | rocker_tlv_nest_end(desc_info, cmd_stats); |
| 4565 | |
| 4566 | return 0; |
| 4567 | } |
| 4568 | |
| 4569 | static int |
Simon Horman | 534ba6a | 2015-06-01 13:25:04 +0900 | [diff] [blame] | 4570 | rocker_cmd_get_port_stats_ethtool_proc(const struct rocker_port *rocker_port, |
Simon Horman | e505464 | 2015-05-25 14:28:36 +0900 | [diff] [blame] | 4571 | const struct rocker_desc_info *desc_info, |
David Ahern | 9766e97 | 2015-01-29 20:59:33 -0700 | [diff] [blame] | 4572 | void *priv) |
| 4573 | { |
Simon Horman | e505464 | 2015-05-25 14:28:36 +0900 | [diff] [blame] | 4574 | 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 Feldman | 4a6bb6d | 2015-02-25 20:15:37 -0800 | [diff] [blame] | 4577 | u32 pport; |
David Ahern | 9766e97 | 2015-01-29 20:59:33 -0700 | [diff] [blame] | 4578 | 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 Feldman | 4a6bb6d | 2015-02-25 20:15:37 -0800 | [diff] [blame] | 4589 | if (!stats_attrs[ROCKER_TLV_CMD_PORT_STATS_PPORT]) |
David Ahern | 9766e97 | 2015-01-29 20:59:33 -0700 | [diff] [blame] | 4590 | return -EIO; |
| 4591 | |
Scott Feldman | 4a6bb6d | 2015-02-25 20:15:37 -0800 | [diff] [blame] | 4592 | pport = rocker_tlv_get_u32(stats_attrs[ROCKER_TLV_CMD_PORT_STATS_PPORT]); |
| 4593 | if (pport != rocker_port->pport) |
David Ahern | 9766e97 | 2015-01-29 20:59:33 -0700 | [diff] [blame] | 4594 | 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 | |
| 4607 | static int rocker_cmd_get_port_stats_ethtool(struct rocker_port *rocker_port, |
| 4608 | void *priv) |
| 4609 | { |
Scott Feldman | 179f9a2 | 2015-06-12 21:35:46 -0700 | [diff] [blame] | 4610 | return rocker_cmd_exec(rocker_port, SWITCHDEV_TRANS_NONE, 0, |
David Ahern | 9766e97 | 2015-01-29 20:59:33 -0700 | [diff] [blame] | 4611 | rocker_cmd_get_port_stats_prep, NULL, |
| 4612 | rocker_cmd_get_port_stats_ethtool_proc, |
Scott Feldman | c4f2032 | 2015-05-10 09:47:50 -0700 | [diff] [blame] | 4613 | priv); |
David Ahern | 9766e97 | 2015-01-29 20:59:33 -0700 | [diff] [blame] | 4614 | } |
| 4615 | |
| 4616 | static 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 Ahern | 9766e97 | 2015-01-29 20:59:33 -0700 | [diff] [blame] | 4627 | } |
| 4628 | |
| 4629 | static 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 Pirko | 4b8ac96 | 2014-11-28 14:34:26 +0100 | [diff] [blame] | 4639 | static 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 Ahern | 9766e97 | 2015-01-29 20:59:33 -0700 | [diff] [blame] | 4644 | .get_strings = rocker_port_get_strings, |
| 4645 | .get_ethtool_stats = rocker_port_get_stats, |
| 4646 | .get_sset_count = rocker_port_get_sset_count, |
Jiri Pirko | 4b8ac96 | 2014-11-28 14:34:26 +0100 | [diff] [blame] | 4647 | }; |
| 4648 | |
| 4649 | /***************** |
| 4650 | * NAPI interface |
| 4651 | *****************/ |
| 4652 | |
| 4653 | static 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 | |
| 4658 | static 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 Horman | e505464 | 2015-05-25 14:28:36 +0900 | [diff] [blame] | 4661 | const struct rocker *rocker = rocker_port->rocker; |
| 4662 | const struct rocker_desc_info *desc_info; |
Jiri Pirko | 4b8ac96 | 2014-11-28 14:34:26 +0100 | [diff] [blame] | 4663 | u32 credits = 0; |
| 4664 | int err; |
| 4665 | |
| 4666 | /* Cleanup tx descriptors */ |
| 4667 | while ((desc_info = rocker_desc_tail_get(&rocker_port->tx_ring))) { |
David Ahern | f2bbca5 | 2015-01-16 14:22:29 -0700 | [diff] [blame] | 4668 | struct sk_buff *skb; |
| 4669 | |
Jiri Pirko | 4b8ac96 | 2014-11-28 14:34:26 +0100 | [diff] [blame] | 4670 | 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 Ahern | f2bbca5 | 2015-01-16 14:22:29 -0700 | [diff] [blame] | 4675 | |
| 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 Feldman | 4725ceb | 2015-05-10 09:48:08 -0700 | [diff] [blame] | 4680 | } else { |
David Ahern | f2bbca5 | 2015-01-16 14:22:29 -0700 | [diff] [blame] | 4681 | rocker_port->dev->stats.tx_errors++; |
Scott Feldman | 4725ceb | 2015-05-10 09:48:08 -0700 | [diff] [blame] | 4682 | } |
David Ahern | f2bbca5 | 2015-01-16 14:22:29 -0700 | [diff] [blame] | 4683 | |
| 4684 | dev_kfree_skb_any(skb); |
Jiri Pirko | 4b8ac96 | 2014-11-28 14:34:26 +0100 | [diff] [blame] | 4685 | 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 Horman | e505464 | 2015-05-25 14:28:36 +0900 | [diff] [blame] | 4697 | static int rocker_port_rx_proc(const struct rocker *rocker, |
| 4698 | const struct rocker_port *rocker_port, |
Jiri Pirko | 4b8ac96 | 2014-11-28 14:34:26 +0100 | [diff] [blame] | 4699 | struct rocker_desc_info *desc_info) |
| 4700 | { |
Simon Horman | e505464 | 2015-05-25 14:28:36 +0900 | [diff] [blame] | 4701 | const struct rocker_tlv *attrs[ROCKER_TLV_RX_MAX + 1]; |
Jiri Pirko | 4b8ac96 | 2014-11-28 14:34:26 +0100 | [diff] [blame] | 4702 | 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 Ahern | f2bbca5 | 2015-01-16 14:22:29 -0700 | [diff] [blame] | 4717 | |
| 4718 | rocker_port->dev->stats.rx_packets++; |
| 4719 | rocker_port->dev->stats.rx_bytes += skb->len; |
| 4720 | |
Jiri Pirko | 4b8ac96 | 2014-11-28 14:34:26 +0100 | [diff] [blame] | 4721 | netif_receive_skb(skb); |
| 4722 | |
Simon Horman | 534ba6a | 2015-06-01 13:25:04 +0900 | [diff] [blame] | 4723 | return rocker_dma_rx_ring_skb_alloc(rocker_port, desc_info); |
Jiri Pirko | 4b8ac96 | 2014-11-28 14:34:26 +0100 | [diff] [blame] | 4724 | } |
| 4725 | |
| 4726 | static 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 | |
| 4731 | static 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 Horman | e505464 | 2015-05-25 14:28:36 +0900 | [diff] [blame] | 4734 | const struct rocker *rocker = rocker_port->rocker; |
Jiri Pirko | 4b8ac96 | 2014-11-28 14:34:26 +0100 | [diff] [blame] | 4735 | 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 Ahern | f2bbca5 | 2015-01-16 14:22:29 -0700 | [diff] [blame] | 4754 | if (err) |
| 4755 | rocker_port->dev->stats.rx_errors++; |
| 4756 | |
Jiri Pirko | 4b8ac96 | 2014-11-28 14:34:26 +0100 | [diff] [blame] | 4757 | 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 Horman | e505464 | 2015-05-25 14:28:36 +0900 | [diff] [blame] | 4774 | static void rocker_carrier_init(const struct rocker_port *rocker_port) |
Jiri Pirko | 4b8ac96 | 2014-11-28 14:34:26 +0100 | [diff] [blame] | 4775 | { |
Simon Horman | e505464 | 2015-05-25 14:28:36 +0900 | [diff] [blame] | 4776 | const struct rocker *rocker = rocker_port->rocker; |
Jiri Pirko | 4b8ac96 | 2014-11-28 14:34:26 +0100 | [diff] [blame] | 4777 | u64 link_status = rocker_read64(rocker, PORT_PHYS_LINK_STATUS); |
| 4778 | bool link_up; |
| 4779 | |
Scott Feldman | 4a6bb6d | 2015-02-25 20:15:37 -0800 | [diff] [blame] | 4780 | link_up = link_status & (1 << rocker_port->pport); |
Jiri Pirko | 4b8ac96 | 2014-11-28 14:34:26 +0100 | [diff] [blame] | 4781 | if (link_up) |
| 4782 | netif_carrier_on(rocker_port->dev); |
| 4783 | else |
| 4784 | netif_carrier_off(rocker_port->dev); |
| 4785 | } |
| 4786 | |
Simon Horman | e505464 | 2015-05-25 14:28:36 +0900 | [diff] [blame] | 4787 | static void rocker_remove_ports(const struct rocker *rocker) |
Jiri Pirko | 4b8ac96 | 2014-11-28 14:34:26 +0100 | [diff] [blame] | 4788 | { |
Scott Feldman | 9f6bbf7 | 2014-11-28 14:34:27 +0100 | [diff] [blame] | 4789 | struct rocker_port *rocker_port; |
Jiri Pirko | 4b8ac96 | 2014-11-28 14:34:26 +0100 | [diff] [blame] | 4790 | int i; |
| 4791 | |
Scott Feldman | 9f6bbf7 | 2014-11-28 14:34:27 +0100 | [diff] [blame] | 4792 | for (i = 0; i < rocker->port_count; i++) { |
| 4793 | rocker_port = rocker->ports[i]; |
Scott Feldman | a072031 | 2015-06-12 21:09:44 -0700 | [diff] [blame] | 4794 | if (!rocker_port) |
| 4795 | continue; |
Scott Feldman | c4f2032 | 2015-05-10 09:47:50 -0700 | [diff] [blame] | 4796 | rocker_port_ig_tbl(rocker_port, SWITCHDEV_TRANS_NONE, |
| 4797 | ROCKER_OP_FLAG_REMOVE); |
Scott Feldman | 9f6bbf7 | 2014-11-28 14:34:27 +0100 | [diff] [blame] | 4798 | unregister_netdev(rocker_port->dev); |
| 4799 | } |
Jiri Pirko | 4b8ac96 | 2014-11-28 14:34:26 +0100 | [diff] [blame] | 4800 | kfree(rocker->ports); |
| 4801 | } |
| 4802 | |
Simon Horman | 534ba6a | 2015-06-01 13:25:04 +0900 | [diff] [blame] | 4803 | static void rocker_port_dev_addr_init(struct rocker_port *rocker_port) |
Jiri Pirko | 4b8ac96 | 2014-11-28 14:34:26 +0100 | [diff] [blame] | 4804 | { |
Simon Horman | 534ba6a | 2015-06-01 13:25:04 +0900 | [diff] [blame] | 4805 | const struct rocker *rocker = rocker_port->rocker; |
Simon Horman | e505464 | 2015-05-25 14:28:36 +0900 | [diff] [blame] | 4806 | const struct pci_dev *pdev = rocker->pdev; |
Jiri Pirko | 4b8ac96 | 2014-11-28 14:34:26 +0100 | [diff] [blame] | 4807 | 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 | |
| 4817 | static int rocker_probe_port(struct rocker *rocker, unsigned int port_number) |
| 4818 | { |
Simon Horman | e505464 | 2015-05-25 14:28:36 +0900 | [diff] [blame] | 4819 | const struct pci_dev *pdev = rocker->pdev; |
Jiri Pirko | 4b8ac96 | 2014-11-28 14:34:26 +0100 | [diff] [blame] | 4820 | struct rocker_port *rocker_port; |
| 4821 | struct net_device *dev; |
Scott Feldman | bcfd780 | 2015-06-01 11:39:04 -0700 | [diff] [blame] | 4822 | u16 untagged_vid = 0; |
Jiri Pirko | 4b8ac96 | 2014-11-28 14:34:26 +0100 | [diff] [blame] | 4823 | 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 Feldman | 4a6bb6d | 2015-02-25 20:15:37 -0800 | [diff] [blame] | 4832 | rocker_port->pport = port_number + 1; |
Scott Feldman | 5111f80 | 2014-11-28 14:34:30 +0100 | [diff] [blame] | 4833 | rocker_port->brport_flags = BR_LEARNING | BR_LEARNING_SYNC; |
Scott Feldman | c4f2032 | 2015-05-10 09:47:50 -0700 | [diff] [blame] | 4834 | INIT_LIST_HEAD(&rocker_port->trans_mem); |
Jiri Pirko | 4b8ac96 | 2014-11-28 14:34:26 +0100 | [diff] [blame] | 4835 | |
Simon Horman | 534ba6a | 2015-06-01 13:25:04 +0900 | [diff] [blame] | 4836 | rocker_port_dev_addr_init(rocker_port); |
Jiri Pirko | 4b8ac96 | 2014-11-28 14:34:26 +0100 | [diff] [blame] | 4837 | dev->netdev_ops = &rocker_port_netdev_ops; |
| 4838 | dev->ethtool_ops = &rocker_port_ethtool_ops; |
Jiri Pirko | 9d47c0a | 2015-05-10 09:47:47 -0700 | [diff] [blame] | 4839 | dev->switchdev_ops = &rocker_port_switchdev_ops; |
Jiri Pirko | 4b8ac96 | 2014-11-28 14:34:26 +0100 | [diff] [blame] | 4840 | 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 Feldman | 2aa2ed0 | 2015-06-01 11:39:06 -0700 | [diff] [blame] | 4846 | dev->features |= NETIF_F_NETNS_LOCAL; |
Jiri Pirko | 4b8ac96 | 2014-11-28 14:34:26 +0100 | [diff] [blame] | 4847 | |
| 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 Feldman | c4f2032 | 2015-05-10 09:47:50 -0700 | [diff] [blame] | 4855 | rocker_port_set_learning(rocker_port, SWITCHDEV_TRANS_NONE); |
Scott Feldman | 5111f80 | 2014-11-28 14:34:30 +0100 | [diff] [blame] | 4856 | |
Scott Feldman | c4f2032 | 2015-05-10 09:47:50 -0700 | [diff] [blame] | 4857 | err = rocker_port_ig_tbl(rocker_port, SWITCHDEV_TRANS_NONE, 0); |
Scott Feldman | 9f6bbf7 | 2014-11-28 14:34:27 +0100 | [diff] [blame] | 4858 | if (err) { |
| 4859 | dev_err(&pdev->dev, "install ig port table failed\n"); |
| 4860 | goto err_port_ig_tbl; |
| 4861 | } |
| 4862 | |
Scott Feldman | bcfd780 | 2015-06-01 11:39:04 -0700 | [diff] [blame] | 4863 | 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 Pirko | 4b8ac96 | 2014-11-28 14:34:26 +0100 | [diff] [blame] | 4873 | return 0; |
| 4874 | |
Scott Feldman | bcfd780 | 2015-06-01 11:39:04 -0700 | [diff] [blame] | 4875 | err_untagged_vlan: |
| 4876 | rocker_port_ig_tbl(rocker_port, SWITCHDEV_TRANS_NONE, |
| 4877 | ROCKER_OP_FLAG_REMOVE); |
Scott Feldman | 9f6bbf7 | 2014-11-28 14:34:27 +0100 | [diff] [blame] | 4878 | err_port_ig_tbl: |
| 4879 | unregister_netdev(dev); |
Jiri Pirko | 4b8ac96 | 2014-11-28 14:34:26 +0100 | [diff] [blame] | 4880 | err_register_netdev: |
| 4881 | free_netdev(dev); |
| 4882 | return err; |
| 4883 | } |
| 4884 | |
| 4885 | static 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 Feldman | 27b808c | 2015-06-01 11:39:02 -0700 | [diff] [blame] | 4892 | rocker->ports = kzalloc(alloc_size, GFP_KERNEL); |
Dan Carpenter | e65ad3b | 2015-02-25 16:35:32 +0300 | [diff] [blame] | 4893 | if (!rocker->ports) |
| 4894 | return -ENOMEM; |
Jiri Pirko | 4b8ac96 | 2014-11-28 14:34:26 +0100 | [diff] [blame] | 4895 | 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 | |
| 4902 | remove_ports: |
| 4903 | rocker_remove_ports(rocker); |
| 4904 | return err; |
| 4905 | } |
| 4906 | |
| 4907 | static 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 | |
| 4936 | err_enable_msix: |
| 4937 | kfree(rocker->msix_entries); |
| 4938 | return err; |
| 4939 | } |
| 4940 | |
Simon Horman | e505464 | 2015-05-25 14:28:36 +0900 | [diff] [blame] | 4941 | static void rocker_msix_fini(const struct rocker *rocker) |
Jiri Pirko | 4b8ac96 | 2014-11-28 14:34:26 +0100 | [diff] [blame] | 4942 | { |
| 4943 | pci_disable_msix(rocker->pdev); |
| 4944 | kfree(rocker->msix_entries); |
| 4945 | } |
| 4946 | |
| 4947 | static 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 Yongjun | 3122a92 | 2015-04-16 20:21:02 +0800 | [diff] [blame] | 4985 | err = -EINVAL; |
Jiri Pirko | 4b8ac96 | 2014-11-28 14:34:26 +0100 | [diff] [blame] | 4986 | 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 Feldman | 9f6bbf7 | 2014-11-28 14:34:27 +0100 | [diff] [blame] | 5039 | 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 Pirko | 4b8ac96 | 2014-11-28 14:34:26 +0100 | [diff] [blame] | 5045 | 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 | |
| 5055 | err_probe_ports: |
Scott Feldman | 9f6bbf7 | 2014-11-28 14:34:27 +0100 | [diff] [blame] | 5056 | rocker_free_tbls(rocker); |
| 5057 | err_init_tbls: |
Jiri Pirko | 4b8ac96 | 2014-11-28 14:34:26 +0100 | [diff] [blame] | 5058 | free_irq(rocker_msix_vector(rocker, ROCKER_MSIX_VEC_EVENT), rocker); |
| 5059 | err_request_event_irq: |
| 5060 | free_irq(rocker_msix_vector(rocker, ROCKER_MSIX_VEC_CMD), rocker); |
| 5061 | err_request_cmd_irq: |
| 5062 | rocker_dma_rings_fini(rocker); |
| 5063 | err_dma_rings_init: |
| 5064 | err_basic_hw_test: |
| 5065 | rocker_msix_fini(rocker); |
| 5066 | err_msix_init: |
| 5067 | iounmap(rocker->hw_addr); |
| 5068 | err_ioremap: |
| 5069 | err_pci_resource_len_check: |
| 5070 | err_pci_set_dma_mask: |
| 5071 | pci_release_regions(pdev); |
| 5072 | err_pci_request_regions: |
| 5073 | pci_disable_device(pdev); |
| 5074 | err_pci_enable_device: |
| 5075 | kfree(rocker); |
| 5076 | return err; |
| 5077 | } |
| 5078 | |
| 5079 | static void rocker_remove(struct pci_dev *pdev) |
| 5080 | { |
| 5081 | struct rocker *rocker = pci_get_drvdata(pdev); |
| 5082 | |
Scott Feldman | 9f6bbf7 | 2014-11-28 14:34:27 +0100 | [diff] [blame] | 5083 | rocker_free_tbls(rocker); |
Jiri Pirko | 4b8ac96 | 2014-11-28 14:34:26 +0100 | [diff] [blame] | 5084 | 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 | |
| 5096 | static 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 Feldman | 6c70794 | 2014-11-28 14:34:28 +0100 | [diff] [blame] | 5103 | /************************************ |
| 5104 | * Net device notifier event handler |
| 5105 | ************************************/ |
| 5106 | |
Simon Horman | e505464 | 2015-05-25 14:28:36 +0900 | [diff] [blame] | 5107 | static bool rocker_port_dev_check(const struct net_device *dev) |
Scott Feldman | 6c70794 | 2014-11-28 14:34:28 +0100 | [diff] [blame] | 5108 | { |
| 5109 | return dev->netdev_ops == &rocker_port_netdev_ops; |
| 5110 | } |
| 5111 | |
| 5112 | static int rocker_port_bridge_join(struct rocker_port *rocker_port, |
| 5113 | struct net_device *bridge) |
| 5114 | { |
Scott Feldman | 027e00d | 2015-06-01 11:39:05 -0700 | [diff] [blame] | 5115 | u16 untagged_vid = 0; |
Scott Feldman | 6c70794 | 2014-11-28 14:34:28 +0100 | [diff] [blame] | 5116 | int err; |
| 5117 | |
Scott Feldman | 027e00d | 2015-06-01 11:39:05 -0700 | [diff] [blame] | 5118 | /* 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 Horman | df6a206 | 2015-05-21 12:40:17 +0900 | [diff] [blame] | 5128 | rocker_port_internal_vlan_id_put(rocker_port, |
Scott Feldman | 6c70794 | 2014-11-28 14:34:28 +0100 | [diff] [blame] | 5129 | rocker_port->dev->ifindex); |
Scott Feldman | 027e00d | 2015-06-01 11:39:05 -0700 | [diff] [blame] | 5130 | rocker_port->internal_vlan_id = |
| 5131 | rocker_port_internal_vlan_id_get(rocker_port, bridge->ifindex); |
Scott Feldman | 6c70794 | 2014-11-28 14:34:28 +0100 | [diff] [blame] | 5132 | |
| 5133 | rocker_port->bridge_dev = bridge; |
| 5134 | |
Scott Feldman | 027e00d | 2015-06-01 11:39:05 -0700 | [diff] [blame] | 5135 | return rocker_port_vlan_add(rocker_port, SWITCHDEV_TRANS_NONE, |
| 5136 | untagged_vid, 0); |
Scott Feldman | 6c70794 | 2014-11-28 14:34:28 +0100 | [diff] [blame] | 5137 | } |
| 5138 | |
| 5139 | static int rocker_port_bridge_leave(struct rocker_port *rocker_port) |
| 5140 | { |
Scott Feldman | 027e00d | 2015-06-01 11:39:05 -0700 | [diff] [blame] | 5141 | u16 untagged_vid = 0; |
Scott Feldman | 6c70794 | 2014-11-28 14:34:28 +0100 | [diff] [blame] | 5142 | int err; |
| 5143 | |
Scott Feldman | 027e00d | 2015-06-01 11:39:05 -0700 | [diff] [blame] | 5144 | err = rocker_port_vlan_del(rocker_port, untagged_vid, 0); |
| 5145 | if (err) |
| 5146 | return err; |
| 5147 | |
Simon Horman | df6a206 | 2015-05-21 12:40:17 +0900 | [diff] [blame] | 5148 | rocker_port_internal_vlan_id_put(rocker_port, |
Scott Feldman | 6c70794 | 2014-11-28 14:34:28 +0100 | [diff] [blame] | 5149 | rocker_port->bridge_dev->ifindex); |
Scott Feldman | 6c70794 | 2014-11-28 14:34:28 +0100 | [diff] [blame] | 5150 | rocker_port->internal_vlan_id = |
| 5151 | rocker_port_internal_vlan_id_get(rocker_port, |
| 5152 | rocker_port->dev->ifindex); |
Scott Feldman | 027e00d | 2015-06-01 11:39:05 -0700 | [diff] [blame] | 5153 | |
| 5154 | rocker_port->bridge_dev = NULL; |
| 5155 | |
| 5156 | err = rocker_port_vlan_add(rocker_port, SWITCHDEV_TRANS_NONE, |
| 5157 | untagged_vid, 0); |
Scott Feldman | e47172a | 2015-02-25 20:15:38 -0800 | [diff] [blame] | 5158 | if (err) |
| 5159 | return err; |
| 5160 | |
| 5161 | if (rocker_port->dev->flags & IFF_UP) |
Scott Feldman | 179f9a2 | 2015-06-12 21:35:46 -0700 | [diff] [blame] | 5162 | err = rocker_port_fwd_enable(rocker_port, |
| 5163 | SWITCHDEV_TRANS_NONE, 0); |
Scott Feldman | 6c70794 | 2014-11-28 14:34:28 +0100 | [diff] [blame] | 5164 | |
| 5165 | return err; |
| 5166 | } |
| 5167 | |
| 5168 | static 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 Horman | a6e95cc | 2015-03-24 09:31:40 +0900 | [diff] [blame] | 5174 | /* 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 Feldman | 6c70794 | 2014-11-28 14:34:28 +0100 | [diff] [blame] | 5180 | if (master && master->rtnl_link_ops && |
| 5181 | !strcmp(master->rtnl_link_ops->kind, "bridge")) |
| 5182 | err = rocker_port_bridge_join(rocker_port, master); |
Simon Horman | a6e95cc | 2015-03-24 09:31:40 +0900 | [diff] [blame] | 5183 | else if (rocker_port_is_bridged(rocker_port)) |
Scott Feldman | 6c70794 | 2014-11-28 14:34:28 +0100 | [diff] [blame] | 5184 | err = rocker_port_bridge_leave(rocker_port); |
| 5185 | |
| 5186 | return err; |
| 5187 | } |
| 5188 | |
| 5189 | static 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 | |
| 5211 | static struct notifier_block rocker_netdevice_nb __read_mostly = { |
| 5212 | .notifier_call = rocker_netdevice_event, |
| 5213 | }; |
| 5214 | |
Scott Feldman | c1beeef | 2015-03-05 21:21:20 -0800 | [diff] [blame] | 5215 | /************************************ |
| 5216 | * Net event notifier event handler |
| 5217 | ************************************/ |
| 5218 | |
| 5219 | static int rocker_neigh_update(struct net_device *dev, struct neighbour *n) |
| 5220 | { |
| 5221 | struct rocker_port *rocker_port = netdev_priv(dev); |
Scott Feldman | 02a9fbf | 2015-06-12 21:35:47 -0700 | [diff] [blame] | 5222 | int flags = (n->nud_state & NUD_VALID ? 0 : ROCKER_OP_FLAG_REMOVE) | |
| 5223 | ROCKER_OP_FLAG_NOWAIT; |
Scott Feldman | c1beeef | 2015-03-05 21:21:20 -0800 | [diff] [blame] | 5224 | __be32 ip_addr = *(__be32 *)n->primary_key; |
| 5225 | |
Scott Feldman | c4f2032 | 2015-05-10 09:47:50 -0700 | [diff] [blame] | 5226 | return rocker_port_ipv4_neigh(rocker_port, SWITCHDEV_TRANS_NONE, |
| 5227 | flags, ip_addr, n->ha); |
Scott Feldman | c1beeef | 2015-03-05 21:21:20 -0800 | [diff] [blame] | 5228 | } |
| 5229 | |
| 5230 | static 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 | |
| 5255 | static struct notifier_block rocker_netevent_nb __read_mostly = { |
| 5256 | .notifier_call = rocker_netevent_event, |
| 5257 | }; |
| 5258 | |
Jiri Pirko | 4b8ac96 | 2014-11-28 14:34:26 +0100 | [diff] [blame] | 5259 | /*********************** |
| 5260 | * Module init and exit |
| 5261 | ***********************/ |
| 5262 | |
| 5263 | static int __init rocker_module_init(void) |
| 5264 | { |
Scott Feldman | 6c70794 | 2014-11-28 14:34:28 +0100 | [diff] [blame] | 5265 | int err; |
| 5266 | |
| 5267 | register_netdevice_notifier(&rocker_netdevice_nb); |
Scott Feldman | c1beeef | 2015-03-05 21:21:20 -0800 | [diff] [blame] | 5268 | register_netevent_notifier(&rocker_netevent_nb); |
Scott Feldman | 6c70794 | 2014-11-28 14:34:28 +0100 | [diff] [blame] | 5269 | err = pci_register_driver(&rocker_pci_driver); |
| 5270 | if (err) |
| 5271 | goto err_pci_register_driver; |
| 5272 | return 0; |
| 5273 | |
| 5274 | err_pci_register_driver: |
Scott Feldman | c1beeef | 2015-03-05 21:21:20 -0800 | [diff] [blame] | 5275 | unregister_netdevice_notifier(&rocker_netevent_nb); |
Scott Feldman | 6c70794 | 2014-11-28 14:34:28 +0100 | [diff] [blame] | 5276 | unregister_netdevice_notifier(&rocker_netdevice_nb); |
| 5277 | return err; |
Jiri Pirko | 4b8ac96 | 2014-11-28 14:34:26 +0100 | [diff] [blame] | 5278 | } |
| 5279 | |
| 5280 | static void __exit rocker_module_exit(void) |
| 5281 | { |
Scott Feldman | c1beeef | 2015-03-05 21:21:20 -0800 | [diff] [blame] | 5282 | unregister_netevent_notifier(&rocker_netevent_nb); |
Scott Feldman | 6c70794 | 2014-11-28 14:34:28 +0100 | [diff] [blame] | 5283 | unregister_netdevice_notifier(&rocker_netdevice_nb); |
Jiri Pirko | 4b8ac96 | 2014-11-28 14:34:26 +0100 | [diff] [blame] | 5284 | pci_unregister_driver(&rocker_pci_driver); |
| 5285 | } |
| 5286 | |
| 5287 | module_init(rocker_module_init); |
| 5288 | module_exit(rocker_module_exit); |
| 5289 | |
| 5290 | MODULE_LICENSE("GPL v2"); |
| 5291 | MODULE_AUTHOR("Jiri Pirko <jiri@resnulli.us>"); |
| 5292 | MODULE_AUTHOR("Scott Feldman <sfeldma@gmail.com>"); |
| 5293 | MODULE_DESCRIPTION("Rocker switch device driver"); |
| 5294 | MODULE_DEVICE_TABLE(pci, rocker_pci_id_table); |