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; |
| 184 | DEFINE_DMA_UNMAP_ADDR(mapaddr); |
| 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; |
| 228 | }; |
| 229 | |
| 230 | struct rocker { |
| 231 | struct pci_dev *pdev; |
| 232 | u8 __iomem *hw_addr; |
| 233 | struct msix_entry *msix_entries; |
| 234 | unsigned int port_count; |
| 235 | struct rocker_port **ports; |
| 236 | struct { |
| 237 | u64 id; |
| 238 | } hw; |
| 239 | spinlock_t cmd_ring_lock; |
| 240 | struct rocker_dma_ring_info cmd_ring; |
| 241 | struct rocker_dma_ring_info event_ring; |
Scott Feldman | 9f6bbf7 | 2014-11-28 14:34:27 +0100 | [diff] [blame] | 242 | DECLARE_HASHTABLE(flow_tbl, 16); |
| 243 | spinlock_t flow_tbl_lock; |
| 244 | u64 flow_tbl_next_cookie; |
| 245 | DECLARE_HASHTABLE(group_tbl, 16); |
| 246 | spinlock_t group_tbl_lock; |
| 247 | DECLARE_HASHTABLE(fdb_tbl, 16); |
| 248 | spinlock_t fdb_tbl_lock; |
| 249 | unsigned long internal_vlan_bitmap[ROCKER_INTERNAL_VLAN_BITMAP_LEN]; |
| 250 | DECLARE_HASHTABLE(internal_vlan_tbl, 8); |
| 251 | spinlock_t internal_vlan_tbl_lock; |
Scott Feldman | c1beeef | 2015-03-05 21:21:20 -0800 | [diff] [blame] | 252 | DECLARE_HASHTABLE(neigh_tbl, 16); |
| 253 | spinlock_t neigh_tbl_lock; |
| 254 | u32 neigh_tbl_next_index; |
Jiri Pirko | 4b8ac96 | 2014-11-28 14:34:26 +0100 | [diff] [blame] | 255 | }; |
| 256 | |
Scott Feldman | 9f6bbf7 | 2014-11-28 14:34:27 +0100 | [diff] [blame] | 257 | static const u8 zero_mac[ETH_ALEN] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; |
| 258 | static const u8 ff_mac[ETH_ALEN] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }; |
| 259 | static const u8 ll_mac[ETH_ALEN] = { 0x01, 0x80, 0xc2, 0x00, 0x00, 0x00 }; |
| 260 | static const u8 ll_mask[ETH_ALEN] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xf0 }; |
| 261 | static const u8 mcast_mac[ETH_ALEN] = { 0x01, 0x00, 0x00, 0x00, 0x00, 0x00 }; |
| 262 | static const u8 ipv4_mcast[ETH_ALEN] = { 0x01, 0x00, 0x5e, 0x00, 0x00, 0x00 }; |
| 263 | static const u8 ipv4_mask[ETH_ALEN] = { 0xff, 0xff, 0xff, 0x80, 0x00, 0x00 }; |
| 264 | static const u8 ipv6_mcast[ETH_ALEN] = { 0x33, 0x33, 0x00, 0x00, 0x00, 0x00 }; |
| 265 | static const u8 ipv6_mask[ETH_ALEN] = { 0xff, 0xff, 0x00, 0x00, 0x00, 0x00 }; |
| 266 | |
| 267 | /* Rocker priority levels for flow table entries. Higher |
| 268 | * priority match takes precedence over lower priority match. |
| 269 | */ |
| 270 | |
| 271 | enum { |
| 272 | ROCKER_PRIORITY_UNKNOWN = 0, |
| 273 | ROCKER_PRIORITY_IG_PORT = 1, |
| 274 | ROCKER_PRIORITY_VLAN = 1, |
| 275 | ROCKER_PRIORITY_TERM_MAC_UCAST = 0, |
| 276 | ROCKER_PRIORITY_TERM_MAC_MCAST = 1, |
Scott Feldman | 9f6bbf7 | 2014-11-28 14:34:27 +0100 | [diff] [blame] | 277 | ROCKER_PRIORITY_BRIDGING_VLAN_DFLT_EXACT = 1, |
| 278 | ROCKER_PRIORITY_BRIDGING_VLAN_DFLT_WILD = 2, |
| 279 | ROCKER_PRIORITY_BRIDGING_VLAN = 3, |
| 280 | ROCKER_PRIORITY_BRIDGING_TENANT_DFLT_EXACT = 1, |
| 281 | ROCKER_PRIORITY_BRIDGING_TENANT_DFLT_WILD = 2, |
| 282 | ROCKER_PRIORITY_BRIDGING_TENANT = 3, |
| 283 | ROCKER_PRIORITY_ACL_CTRL = 3, |
| 284 | ROCKER_PRIORITY_ACL_NORMAL = 2, |
| 285 | ROCKER_PRIORITY_ACL_DFLT = 1, |
| 286 | }; |
| 287 | |
| 288 | static bool rocker_vlan_id_is_internal(__be16 vlan_id) |
| 289 | { |
| 290 | u16 start = ROCKER_INTERNAL_VLAN_ID_BASE; |
| 291 | u16 end = 0xffe; |
| 292 | u16 _vlan_id = ntohs(vlan_id); |
| 293 | |
| 294 | return (_vlan_id >= start && _vlan_id <= end); |
| 295 | } |
| 296 | |
| 297 | static __be16 rocker_port_vid_to_vlan(struct rocker_port *rocker_port, |
| 298 | u16 vid, bool *pop_vlan) |
| 299 | { |
| 300 | __be16 vlan_id; |
| 301 | |
| 302 | if (pop_vlan) |
| 303 | *pop_vlan = false; |
| 304 | vlan_id = htons(vid); |
| 305 | if (!vlan_id) { |
| 306 | vlan_id = rocker_port->internal_vlan_id; |
| 307 | if (pop_vlan) |
| 308 | *pop_vlan = true; |
| 309 | } |
| 310 | |
| 311 | return vlan_id; |
| 312 | } |
| 313 | |
Scott Feldman | 6c70794 | 2014-11-28 14:34:28 +0100 | [diff] [blame] | 314 | static u16 rocker_port_vlan_to_vid(struct rocker_port *rocker_port, |
| 315 | __be16 vlan_id) |
| 316 | { |
| 317 | if (rocker_vlan_id_is_internal(vlan_id)) |
| 318 | return 0; |
| 319 | |
| 320 | return ntohs(vlan_id); |
| 321 | } |
| 322 | |
| 323 | static bool rocker_port_is_bridged(struct rocker_port *rocker_port) |
| 324 | { |
| 325 | return !!rocker_port->bridge_dev; |
| 326 | } |
| 327 | |
Jiri Pirko | 4b8ac96 | 2014-11-28 14:34:26 +0100 | [diff] [blame] | 328 | struct rocker_wait { |
| 329 | wait_queue_head_t wait; |
| 330 | bool done; |
| 331 | bool nowait; |
| 332 | }; |
| 333 | |
| 334 | static void rocker_wait_reset(struct rocker_wait *wait) |
| 335 | { |
| 336 | wait->done = false; |
| 337 | wait->nowait = false; |
| 338 | } |
| 339 | |
| 340 | static void rocker_wait_init(struct rocker_wait *wait) |
| 341 | { |
| 342 | init_waitqueue_head(&wait->wait); |
| 343 | rocker_wait_reset(wait); |
| 344 | } |
| 345 | |
| 346 | static struct rocker_wait *rocker_wait_create(gfp_t gfp) |
| 347 | { |
| 348 | struct rocker_wait *wait; |
| 349 | |
| 350 | wait = kmalloc(sizeof(*wait), gfp); |
| 351 | if (!wait) |
| 352 | return NULL; |
| 353 | rocker_wait_init(wait); |
| 354 | return wait; |
| 355 | } |
| 356 | |
| 357 | static void rocker_wait_destroy(struct rocker_wait *work) |
| 358 | { |
| 359 | kfree(work); |
| 360 | } |
| 361 | |
| 362 | static bool rocker_wait_event_timeout(struct rocker_wait *wait, |
| 363 | unsigned long timeout) |
| 364 | { |
| 365 | wait_event_timeout(wait->wait, wait->done, HZ / 10); |
| 366 | if (!wait->done) |
| 367 | return false; |
| 368 | return true; |
| 369 | } |
| 370 | |
| 371 | static void rocker_wait_wake_up(struct rocker_wait *wait) |
| 372 | { |
| 373 | wait->done = true; |
| 374 | wake_up(&wait->wait); |
| 375 | } |
| 376 | |
| 377 | static u32 rocker_msix_vector(struct rocker *rocker, unsigned int vector) |
| 378 | { |
| 379 | return rocker->msix_entries[vector].vector; |
| 380 | } |
| 381 | |
| 382 | static u32 rocker_msix_tx_vector(struct rocker_port *rocker_port) |
| 383 | { |
| 384 | return rocker_msix_vector(rocker_port->rocker, |
| 385 | ROCKER_MSIX_VEC_TX(rocker_port->port_number)); |
| 386 | } |
| 387 | |
| 388 | static u32 rocker_msix_rx_vector(struct rocker_port *rocker_port) |
| 389 | { |
| 390 | return rocker_msix_vector(rocker_port->rocker, |
| 391 | ROCKER_MSIX_VEC_RX(rocker_port->port_number)); |
| 392 | } |
| 393 | |
| 394 | #define rocker_write32(rocker, reg, val) \ |
| 395 | writel((val), (rocker)->hw_addr + (ROCKER_ ## reg)) |
| 396 | #define rocker_read32(rocker, reg) \ |
| 397 | readl((rocker)->hw_addr + (ROCKER_ ## reg)) |
| 398 | #define rocker_write64(rocker, reg, val) \ |
| 399 | writeq((val), (rocker)->hw_addr + (ROCKER_ ## reg)) |
| 400 | #define rocker_read64(rocker, reg) \ |
| 401 | readq((rocker)->hw_addr + (ROCKER_ ## reg)) |
| 402 | |
| 403 | /***************************** |
| 404 | * HW basic testing functions |
| 405 | *****************************/ |
| 406 | |
| 407 | static int rocker_reg_test(struct rocker *rocker) |
| 408 | { |
| 409 | struct pci_dev *pdev = rocker->pdev; |
| 410 | u64 test_reg; |
| 411 | u64 rnd; |
| 412 | |
| 413 | rnd = prandom_u32(); |
| 414 | rnd >>= 1; |
| 415 | rocker_write32(rocker, TEST_REG, rnd); |
| 416 | test_reg = rocker_read32(rocker, TEST_REG); |
| 417 | if (test_reg != rnd * 2) { |
| 418 | dev_err(&pdev->dev, "unexpected 32bit register value %08llx, expected %08llx\n", |
| 419 | test_reg, rnd * 2); |
| 420 | return -EIO; |
| 421 | } |
| 422 | |
| 423 | rnd = prandom_u32(); |
| 424 | rnd <<= 31; |
| 425 | rnd |= prandom_u32(); |
| 426 | rocker_write64(rocker, TEST_REG64, rnd); |
| 427 | test_reg = rocker_read64(rocker, TEST_REG64); |
| 428 | if (test_reg != rnd * 2) { |
| 429 | dev_err(&pdev->dev, "unexpected 64bit register value %16llx, expected %16llx\n", |
| 430 | test_reg, rnd * 2); |
| 431 | return -EIO; |
| 432 | } |
| 433 | |
| 434 | return 0; |
| 435 | } |
| 436 | |
| 437 | static int rocker_dma_test_one(struct rocker *rocker, struct rocker_wait *wait, |
| 438 | u32 test_type, dma_addr_t dma_handle, |
| 439 | unsigned char *buf, unsigned char *expect, |
| 440 | size_t size) |
| 441 | { |
| 442 | struct pci_dev *pdev = rocker->pdev; |
| 443 | int i; |
| 444 | |
| 445 | rocker_wait_reset(wait); |
| 446 | rocker_write32(rocker, TEST_DMA_CTRL, test_type); |
| 447 | |
| 448 | if (!rocker_wait_event_timeout(wait, HZ / 10)) { |
| 449 | dev_err(&pdev->dev, "no interrupt received within a timeout\n"); |
| 450 | return -EIO; |
| 451 | } |
| 452 | |
| 453 | for (i = 0; i < size; i++) { |
| 454 | if (buf[i] != expect[i]) { |
| 455 | dev_err(&pdev->dev, "unexpected memory content %02x at byte %x\n, %02x expected", |
| 456 | buf[i], i, expect[i]); |
| 457 | return -EIO; |
| 458 | } |
| 459 | } |
| 460 | return 0; |
| 461 | } |
| 462 | |
| 463 | #define ROCKER_TEST_DMA_BUF_SIZE (PAGE_SIZE * 4) |
| 464 | #define ROCKER_TEST_DMA_FILL_PATTERN 0x96 |
| 465 | |
| 466 | static int rocker_dma_test_offset(struct rocker *rocker, |
| 467 | struct rocker_wait *wait, int offset) |
| 468 | { |
| 469 | struct pci_dev *pdev = rocker->pdev; |
| 470 | unsigned char *alloc; |
| 471 | unsigned char *buf; |
| 472 | unsigned char *expect; |
| 473 | dma_addr_t dma_handle; |
| 474 | int i; |
| 475 | int err; |
| 476 | |
| 477 | alloc = kzalloc(ROCKER_TEST_DMA_BUF_SIZE * 2 + offset, |
| 478 | GFP_KERNEL | GFP_DMA); |
| 479 | if (!alloc) |
| 480 | return -ENOMEM; |
| 481 | buf = alloc + offset; |
| 482 | expect = buf + ROCKER_TEST_DMA_BUF_SIZE; |
| 483 | |
| 484 | dma_handle = pci_map_single(pdev, buf, ROCKER_TEST_DMA_BUF_SIZE, |
| 485 | PCI_DMA_BIDIRECTIONAL); |
| 486 | if (pci_dma_mapping_error(pdev, dma_handle)) { |
| 487 | err = -EIO; |
| 488 | goto free_alloc; |
| 489 | } |
| 490 | |
| 491 | rocker_write64(rocker, TEST_DMA_ADDR, dma_handle); |
| 492 | rocker_write32(rocker, TEST_DMA_SIZE, ROCKER_TEST_DMA_BUF_SIZE); |
| 493 | |
| 494 | memset(expect, ROCKER_TEST_DMA_FILL_PATTERN, ROCKER_TEST_DMA_BUF_SIZE); |
| 495 | err = rocker_dma_test_one(rocker, wait, ROCKER_TEST_DMA_CTRL_FILL, |
| 496 | dma_handle, buf, expect, |
| 497 | ROCKER_TEST_DMA_BUF_SIZE); |
| 498 | if (err) |
| 499 | goto unmap; |
| 500 | |
| 501 | memset(expect, 0, ROCKER_TEST_DMA_BUF_SIZE); |
| 502 | err = rocker_dma_test_one(rocker, wait, ROCKER_TEST_DMA_CTRL_CLEAR, |
| 503 | dma_handle, buf, expect, |
| 504 | ROCKER_TEST_DMA_BUF_SIZE); |
| 505 | if (err) |
| 506 | goto unmap; |
| 507 | |
| 508 | prandom_bytes(buf, ROCKER_TEST_DMA_BUF_SIZE); |
| 509 | for (i = 0; i < ROCKER_TEST_DMA_BUF_SIZE; i++) |
| 510 | expect[i] = ~buf[i]; |
| 511 | err = rocker_dma_test_one(rocker, wait, ROCKER_TEST_DMA_CTRL_INVERT, |
| 512 | dma_handle, buf, expect, |
| 513 | ROCKER_TEST_DMA_BUF_SIZE); |
| 514 | if (err) |
| 515 | goto unmap; |
| 516 | |
| 517 | unmap: |
| 518 | pci_unmap_single(pdev, dma_handle, ROCKER_TEST_DMA_BUF_SIZE, |
| 519 | PCI_DMA_BIDIRECTIONAL); |
| 520 | free_alloc: |
| 521 | kfree(alloc); |
| 522 | |
| 523 | return err; |
| 524 | } |
| 525 | |
| 526 | static int rocker_dma_test(struct rocker *rocker, struct rocker_wait *wait) |
| 527 | { |
| 528 | int i; |
| 529 | int err; |
| 530 | |
| 531 | for (i = 0; i < 8; i++) { |
| 532 | err = rocker_dma_test_offset(rocker, wait, i); |
| 533 | if (err) |
| 534 | return err; |
| 535 | } |
| 536 | return 0; |
| 537 | } |
| 538 | |
| 539 | static irqreturn_t rocker_test_irq_handler(int irq, void *dev_id) |
| 540 | { |
| 541 | struct rocker_wait *wait = dev_id; |
| 542 | |
| 543 | rocker_wait_wake_up(wait); |
| 544 | |
| 545 | return IRQ_HANDLED; |
| 546 | } |
| 547 | |
| 548 | static int rocker_basic_hw_test(struct rocker *rocker) |
| 549 | { |
| 550 | struct pci_dev *pdev = rocker->pdev; |
| 551 | struct rocker_wait wait; |
| 552 | int err; |
| 553 | |
| 554 | err = rocker_reg_test(rocker); |
| 555 | if (err) { |
| 556 | dev_err(&pdev->dev, "reg test failed\n"); |
| 557 | return err; |
| 558 | } |
| 559 | |
| 560 | err = request_irq(rocker_msix_vector(rocker, ROCKER_MSIX_VEC_TEST), |
| 561 | rocker_test_irq_handler, 0, |
| 562 | rocker_driver_name, &wait); |
| 563 | if (err) { |
| 564 | dev_err(&pdev->dev, "cannot assign test irq\n"); |
| 565 | return err; |
| 566 | } |
| 567 | |
| 568 | rocker_wait_init(&wait); |
| 569 | rocker_write32(rocker, TEST_IRQ, ROCKER_MSIX_VEC_TEST); |
| 570 | |
| 571 | if (!rocker_wait_event_timeout(&wait, HZ / 10)) { |
| 572 | dev_err(&pdev->dev, "no interrupt received within a timeout\n"); |
| 573 | err = -EIO; |
| 574 | goto free_irq; |
| 575 | } |
| 576 | |
| 577 | err = rocker_dma_test(rocker, &wait); |
| 578 | if (err) |
| 579 | dev_err(&pdev->dev, "dma test failed\n"); |
| 580 | |
| 581 | free_irq: |
| 582 | free_irq(rocker_msix_vector(rocker, ROCKER_MSIX_VEC_TEST), &wait); |
| 583 | return err; |
| 584 | } |
| 585 | |
| 586 | /****** |
| 587 | * TLV |
| 588 | ******/ |
| 589 | |
| 590 | #define ROCKER_TLV_ALIGNTO 8U |
| 591 | #define ROCKER_TLV_ALIGN(len) \ |
| 592 | (((len) + ROCKER_TLV_ALIGNTO - 1) & ~(ROCKER_TLV_ALIGNTO - 1)) |
| 593 | #define ROCKER_TLV_HDRLEN ROCKER_TLV_ALIGN(sizeof(struct rocker_tlv)) |
| 594 | |
| 595 | /* <------- ROCKER_TLV_HDRLEN -------> <--- ROCKER_TLV_ALIGN(payload) ---> |
| 596 | * +-----------------------------+- - -+- - - - - - - - - - - - - - -+- - -+ |
| 597 | * | Header | Pad | Payload | Pad | |
| 598 | * | (struct rocker_tlv) | ing | | ing | |
| 599 | * +-----------------------------+- - -+- - - - - - - - - - - - - - -+- - -+ |
| 600 | * <--------------------------- tlv->len --------------------------> |
| 601 | */ |
| 602 | |
| 603 | static struct rocker_tlv *rocker_tlv_next(const struct rocker_tlv *tlv, |
| 604 | int *remaining) |
| 605 | { |
| 606 | int totlen = ROCKER_TLV_ALIGN(tlv->len); |
| 607 | |
| 608 | *remaining -= totlen; |
| 609 | return (struct rocker_tlv *) ((char *) tlv + totlen); |
| 610 | } |
| 611 | |
| 612 | static int rocker_tlv_ok(const struct rocker_tlv *tlv, int remaining) |
| 613 | { |
| 614 | return remaining >= (int) ROCKER_TLV_HDRLEN && |
| 615 | tlv->len >= ROCKER_TLV_HDRLEN && |
| 616 | tlv->len <= remaining; |
| 617 | } |
| 618 | |
| 619 | #define rocker_tlv_for_each(pos, head, len, rem) \ |
| 620 | for (pos = head, rem = len; \ |
| 621 | rocker_tlv_ok(pos, rem); \ |
| 622 | pos = rocker_tlv_next(pos, &(rem))) |
| 623 | |
| 624 | #define rocker_tlv_for_each_nested(pos, tlv, rem) \ |
| 625 | rocker_tlv_for_each(pos, rocker_tlv_data(tlv), \ |
| 626 | rocker_tlv_len(tlv), rem) |
| 627 | |
| 628 | static int rocker_tlv_attr_size(int payload) |
| 629 | { |
| 630 | return ROCKER_TLV_HDRLEN + payload; |
| 631 | } |
| 632 | |
| 633 | static int rocker_tlv_total_size(int payload) |
| 634 | { |
| 635 | return ROCKER_TLV_ALIGN(rocker_tlv_attr_size(payload)); |
| 636 | } |
| 637 | |
| 638 | static int rocker_tlv_padlen(int payload) |
| 639 | { |
| 640 | return rocker_tlv_total_size(payload) - rocker_tlv_attr_size(payload); |
| 641 | } |
| 642 | |
| 643 | static int rocker_tlv_type(const struct rocker_tlv *tlv) |
| 644 | { |
| 645 | return tlv->type; |
| 646 | } |
| 647 | |
| 648 | static void *rocker_tlv_data(const struct rocker_tlv *tlv) |
| 649 | { |
| 650 | return (char *) tlv + ROCKER_TLV_HDRLEN; |
| 651 | } |
| 652 | |
| 653 | static int rocker_tlv_len(const struct rocker_tlv *tlv) |
| 654 | { |
| 655 | return tlv->len - ROCKER_TLV_HDRLEN; |
| 656 | } |
| 657 | |
| 658 | static u8 rocker_tlv_get_u8(const struct rocker_tlv *tlv) |
| 659 | { |
| 660 | return *(u8 *) rocker_tlv_data(tlv); |
| 661 | } |
| 662 | |
| 663 | static u16 rocker_tlv_get_u16(const struct rocker_tlv *tlv) |
| 664 | { |
| 665 | return *(u16 *) rocker_tlv_data(tlv); |
| 666 | } |
| 667 | |
Jiri Pirko | 9b03c71 | 2014-12-03 14:14:53 +0100 | [diff] [blame] | 668 | static __be16 rocker_tlv_get_be16(const struct rocker_tlv *tlv) |
| 669 | { |
| 670 | return *(__be16 *) rocker_tlv_data(tlv); |
| 671 | } |
| 672 | |
Jiri Pirko | 4b8ac96 | 2014-11-28 14:34:26 +0100 | [diff] [blame] | 673 | static u32 rocker_tlv_get_u32(const struct rocker_tlv *tlv) |
| 674 | { |
| 675 | return *(u32 *) rocker_tlv_data(tlv); |
| 676 | } |
| 677 | |
| 678 | static u64 rocker_tlv_get_u64(const struct rocker_tlv *tlv) |
| 679 | { |
| 680 | return *(u64 *) rocker_tlv_data(tlv); |
| 681 | } |
| 682 | |
| 683 | static void rocker_tlv_parse(struct rocker_tlv **tb, int maxtype, |
| 684 | const char *buf, int buf_len) |
| 685 | { |
| 686 | const struct rocker_tlv *tlv; |
| 687 | const struct rocker_tlv *head = (const struct rocker_tlv *) buf; |
| 688 | int rem; |
| 689 | |
| 690 | memset(tb, 0, sizeof(struct rocker_tlv *) * (maxtype + 1)); |
| 691 | |
| 692 | rocker_tlv_for_each(tlv, head, buf_len, rem) { |
| 693 | u32 type = rocker_tlv_type(tlv); |
| 694 | |
| 695 | if (type > 0 && type <= maxtype) |
| 696 | tb[type] = (struct rocker_tlv *) tlv; |
| 697 | } |
| 698 | } |
| 699 | |
| 700 | static void rocker_tlv_parse_nested(struct rocker_tlv **tb, int maxtype, |
| 701 | const struct rocker_tlv *tlv) |
| 702 | { |
| 703 | rocker_tlv_parse(tb, maxtype, rocker_tlv_data(tlv), |
| 704 | rocker_tlv_len(tlv)); |
| 705 | } |
| 706 | |
| 707 | static void rocker_tlv_parse_desc(struct rocker_tlv **tb, int maxtype, |
| 708 | struct rocker_desc_info *desc_info) |
| 709 | { |
| 710 | rocker_tlv_parse(tb, maxtype, desc_info->data, |
| 711 | desc_info->desc->tlv_size); |
| 712 | } |
| 713 | |
| 714 | static struct rocker_tlv *rocker_tlv_start(struct rocker_desc_info *desc_info) |
| 715 | { |
| 716 | return (struct rocker_tlv *) ((char *) desc_info->data + |
| 717 | desc_info->tlv_size); |
| 718 | } |
| 719 | |
| 720 | static int rocker_tlv_put(struct rocker_desc_info *desc_info, |
| 721 | int attrtype, int attrlen, const void *data) |
| 722 | { |
| 723 | int tail_room = desc_info->data_size - desc_info->tlv_size; |
| 724 | int total_size = rocker_tlv_total_size(attrlen); |
| 725 | struct rocker_tlv *tlv; |
| 726 | |
| 727 | if (unlikely(tail_room < total_size)) |
| 728 | return -EMSGSIZE; |
| 729 | |
| 730 | tlv = rocker_tlv_start(desc_info); |
| 731 | desc_info->tlv_size += total_size; |
| 732 | tlv->type = attrtype; |
| 733 | tlv->len = rocker_tlv_attr_size(attrlen); |
| 734 | memcpy(rocker_tlv_data(tlv), data, attrlen); |
| 735 | memset((char *) tlv + tlv->len, 0, rocker_tlv_padlen(attrlen)); |
| 736 | return 0; |
| 737 | } |
| 738 | |
| 739 | static int rocker_tlv_put_u8(struct rocker_desc_info *desc_info, |
| 740 | int attrtype, u8 value) |
| 741 | { |
| 742 | return rocker_tlv_put(desc_info, attrtype, sizeof(u8), &value); |
| 743 | } |
| 744 | |
| 745 | static int rocker_tlv_put_u16(struct rocker_desc_info *desc_info, |
| 746 | int attrtype, u16 value) |
| 747 | { |
| 748 | return rocker_tlv_put(desc_info, attrtype, sizeof(u16), &value); |
| 749 | } |
| 750 | |
Jiri Pirko | 9b03c71 | 2014-12-03 14:14:53 +0100 | [diff] [blame] | 751 | static int rocker_tlv_put_be16(struct rocker_desc_info *desc_info, |
| 752 | int attrtype, __be16 value) |
| 753 | { |
| 754 | return rocker_tlv_put(desc_info, attrtype, sizeof(__be16), &value); |
| 755 | } |
| 756 | |
Jiri Pirko | 4b8ac96 | 2014-11-28 14:34:26 +0100 | [diff] [blame] | 757 | static int rocker_tlv_put_u32(struct rocker_desc_info *desc_info, |
| 758 | int attrtype, u32 value) |
| 759 | { |
| 760 | return rocker_tlv_put(desc_info, attrtype, sizeof(u32), &value); |
| 761 | } |
| 762 | |
Jiri Pirko | 9b03c71 | 2014-12-03 14:14:53 +0100 | [diff] [blame] | 763 | static int rocker_tlv_put_be32(struct rocker_desc_info *desc_info, |
| 764 | int attrtype, __be32 value) |
| 765 | { |
| 766 | return rocker_tlv_put(desc_info, attrtype, sizeof(__be32), &value); |
| 767 | } |
| 768 | |
Jiri Pirko | 4b8ac96 | 2014-11-28 14:34:26 +0100 | [diff] [blame] | 769 | static int rocker_tlv_put_u64(struct rocker_desc_info *desc_info, |
| 770 | int attrtype, u64 value) |
| 771 | { |
| 772 | return rocker_tlv_put(desc_info, attrtype, sizeof(u64), &value); |
| 773 | } |
| 774 | |
| 775 | static struct rocker_tlv * |
| 776 | rocker_tlv_nest_start(struct rocker_desc_info *desc_info, int attrtype) |
| 777 | { |
| 778 | struct rocker_tlv *start = rocker_tlv_start(desc_info); |
| 779 | |
| 780 | if (rocker_tlv_put(desc_info, attrtype, 0, NULL) < 0) |
| 781 | return NULL; |
| 782 | |
| 783 | return start; |
| 784 | } |
| 785 | |
| 786 | static void rocker_tlv_nest_end(struct rocker_desc_info *desc_info, |
| 787 | struct rocker_tlv *start) |
| 788 | { |
| 789 | start->len = (char *) rocker_tlv_start(desc_info) - (char *) start; |
| 790 | } |
| 791 | |
| 792 | static void rocker_tlv_nest_cancel(struct rocker_desc_info *desc_info, |
| 793 | struct rocker_tlv *start) |
| 794 | { |
| 795 | desc_info->tlv_size = (char *) start - desc_info->data; |
| 796 | } |
| 797 | |
| 798 | /****************************************** |
| 799 | * DMA rings and descriptors manipulations |
| 800 | ******************************************/ |
| 801 | |
| 802 | static u32 __pos_inc(u32 pos, size_t limit) |
| 803 | { |
| 804 | return ++pos == limit ? 0 : pos; |
| 805 | } |
| 806 | |
| 807 | static int rocker_desc_err(struct rocker_desc_info *desc_info) |
| 808 | { |
Scott Feldman | 7eb344f | 2015-02-25 20:15:36 -0800 | [diff] [blame] | 809 | int err = desc_info->desc->comp_err & ~ROCKER_DMA_DESC_COMP_ERR_GEN; |
| 810 | |
| 811 | switch (err) { |
| 812 | case ROCKER_OK: |
| 813 | return 0; |
| 814 | case -ROCKER_ENOENT: |
| 815 | return -ENOENT; |
| 816 | case -ROCKER_ENXIO: |
| 817 | return -ENXIO; |
| 818 | case -ROCKER_ENOMEM: |
| 819 | return -ENOMEM; |
| 820 | case -ROCKER_EEXIST: |
| 821 | return -EEXIST; |
| 822 | case -ROCKER_EINVAL: |
| 823 | return -EINVAL; |
| 824 | case -ROCKER_EMSGSIZE: |
| 825 | return -EMSGSIZE; |
| 826 | case -ROCKER_ENOTSUP: |
| 827 | return -EOPNOTSUPP; |
| 828 | case -ROCKER_ENOBUFS: |
| 829 | return -ENOBUFS; |
| 830 | } |
| 831 | |
| 832 | return -EINVAL; |
Jiri Pirko | 4b8ac96 | 2014-11-28 14:34:26 +0100 | [diff] [blame] | 833 | } |
| 834 | |
| 835 | static void rocker_desc_gen_clear(struct rocker_desc_info *desc_info) |
| 836 | { |
| 837 | desc_info->desc->comp_err &= ~ROCKER_DMA_DESC_COMP_ERR_GEN; |
| 838 | } |
| 839 | |
| 840 | static bool rocker_desc_gen(struct rocker_desc_info *desc_info) |
| 841 | { |
| 842 | u32 comp_err = desc_info->desc->comp_err; |
| 843 | |
| 844 | return comp_err & ROCKER_DMA_DESC_COMP_ERR_GEN ? true : false; |
| 845 | } |
| 846 | |
| 847 | static void *rocker_desc_cookie_ptr_get(struct rocker_desc_info *desc_info) |
| 848 | { |
Arnd Bergmann | adedf37 | 2015-01-13 15:23:52 +0100 | [diff] [blame] | 849 | return (void *)(uintptr_t)desc_info->desc->cookie; |
Jiri Pirko | 4b8ac96 | 2014-11-28 14:34:26 +0100 | [diff] [blame] | 850 | } |
| 851 | |
| 852 | static void rocker_desc_cookie_ptr_set(struct rocker_desc_info *desc_info, |
| 853 | void *ptr) |
| 854 | { |
Arnd Bergmann | adedf37 | 2015-01-13 15:23:52 +0100 | [diff] [blame] | 855 | desc_info->desc->cookie = (uintptr_t) ptr; |
Jiri Pirko | 4b8ac96 | 2014-11-28 14:34:26 +0100 | [diff] [blame] | 856 | } |
| 857 | |
| 858 | static struct rocker_desc_info * |
| 859 | rocker_desc_head_get(struct rocker_dma_ring_info *info) |
| 860 | { |
| 861 | static struct rocker_desc_info *desc_info; |
| 862 | u32 head = __pos_inc(info->head, info->size); |
| 863 | |
| 864 | desc_info = &info->desc_info[info->head]; |
| 865 | if (head == info->tail) |
| 866 | return NULL; /* ring full */ |
| 867 | desc_info->tlv_size = 0; |
| 868 | return desc_info; |
| 869 | } |
| 870 | |
| 871 | static void rocker_desc_commit(struct rocker_desc_info *desc_info) |
| 872 | { |
| 873 | desc_info->desc->buf_size = desc_info->data_size; |
| 874 | desc_info->desc->tlv_size = desc_info->tlv_size; |
| 875 | } |
| 876 | |
| 877 | static void rocker_desc_head_set(struct rocker *rocker, |
| 878 | struct rocker_dma_ring_info *info, |
| 879 | struct rocker_desc_info *desc_info) |
| 880 | { |
| 881 | u32 head = __pos_inc(info->head, info->size); |
| 882 | |
| 883 | BUG_ON(head == info->tail); |
| 884 | rocker_desc_commit(desc_info); |
| 885 | info->head = head; |
| 886 | rocker_write32(rocker, DMA_DESC_HEAD(info->type), head); |
| 887 | } |
| 888 | |
| 889 | static struct rocker_desc_info * |
| 890 | rocker_desc_tail_get(struct rocker_dma_ring_info *info) |
| 891 | { |
| 892 | static struct rocker_desc_info *desc_info; |
| 893 | |
| 894 | if (info->tail == info->head) |
| 895 | return NULL; /* nothing to be done between head and tail */ |
| 896 | desc_info = &info->desc_info[info->tail]; |
| 897 | if (!rocker_desc_gen(desc_info)) |
| 898 | return NULL; /* gen bit not set, desc is not ready yet */ |
| 899 | info->tail = __pos_inc(info->tail, info->size); |
| 900 | desc_info->tlv_size = desc_info->desc->tlv_size; |
| 901 | return desc_info; |
| 902 | } |
| 903 | |
| 904 | static void rocker_dma_ring_credits_set(struct rocker *rocker, |
| 905 | struct rocker_dma_ring_info *info, |
| 906 | u32 credits) |
| 907 | { |
| 908 | if (credits) |
| 909 | rocker_write32(rocker, DMA_DESC_CREDITS(info->type), credits); |
| 910 | } |
| 911 | |
| 912 | static unsigned long rocker_dma_ring_size_fix(size_t size) |
| 913 | { |
| 914 | return max(ROCKER_DMA_SIZE_MIN, |
| 915 | min(roundup_pow_of_two(size), ROCKER_DMA_SIZE_MAX)); |
| 916 | } |
| 917 | |
| 918 | static int rocker_dma_ring_create(struct rocker *rocker, |
| 919 | unsigned int type, |
| 920 | size_t size, |
| 921 | struct rocker_dma_ring_info *info) |
| 922 | { |
| 923 | int i; |
| 924 | |
| 925 | BUG_ON(size != rocker_dma_ring_size_fix(size)); |
| 926 | info->size = size; |
| 927 | info->type = type; |
| 928 | info->head = 0; |
| 929 | info->tail = 0; |
| 930 | info->desc_info = kcalloc(info->size, sizeof(*info->desc_info), |
| 931 | GFP_KERNEL); |
| 932 | if (!info->desc_info) |
| 933 | return -ENOMEM; |
| 934 | |
| 935 | info->desc = pci_alloc_consistent(rocker->pdev, |
| 936 | info->size * sizeof(*info->desc), |
| 937 | &info->mapaddr); |
| 938 | if (!info->desc) { |
| 939 | kfree(info->desc_info); |
| 940 | return -ENOMEM; |
| 941 | } |
| 942 | |
| 943 | for (i = 0; i < info->size; i++) |
| 944 | info->desc_info[i].desc = &info->desc[i]; |
| 945 | |
| 946 | rocker_write32(rocker, DMA_DESC_CTRL(info->type), |
| 947 | ROCKER_DMA_DESC_CTRL_RESET); |
| 948 | rocker_write64(rocker, DMA_DESC_ADDR(info->type), info->mapaddr); |
| 949 | rocker_write32(rocker, DMA_DESC_SIZE(info->type), info->size); |
| 950 | |
| 951 | return 0; |
| 952 | } |
| 953 | |
| 954 | static void rocker_dma_ring_destroy(struct rocker *rocker, |
| 955 | struct rocker_dma_ring_info *info) |
| 956 | { |
| 957 | rocker_write64(rocker, DMA_DESC_ADDR(info->type), 0); |
| 958 | |
| 959 | pci_free_consistent(rocker->pdev, |
| 960 | info->size * sizeof(struct rocker_desc), |
| 961 | info->desc, info->mapaddr); |
| 962 | kfree(info->desc_info); |
| 963 | } |
| 964 | |
| 965 | static void rocker_dma_ring_pass_to_producer(struct rocker *rocker, |
| 966 | struct rocker_dma_ring_info *info) |
| 967 | { |
| 968 | int i; |
| 969 | |
| 970 | BUG_ON(info->head || info->tail); |
| 971 | |
| 972 | /* When ring is consumer, we need to advance head for each desc. |
| 973 | * That tells hw that the desc is ready to be used by it. |
| 974 | */ |
| 975 | for (i = 0; i < info->size - 1; i++) |
| 976 | rocker_desc_head_set(rocker, info, &info->desc_info[i]); |
| 977 | rocker_desc_commit(&info->desc_info[i]); |
| 978 | } |
| 979 | |
| 980 | static int rocker_dma_ring_bufs_alloc(struct rocker *rocker, |
| 981 | struct rocker_dma_ring_info *info, |
| 982 | int direction, size_t buf_size) |
| 983 | { |
| 984 | struct pci_dev *pdev = rocker->pdev; |
| 985 | int i; |
| 986 | int err; |
| 987 | |
| 988 | for (i = 0; i < info->size; i++) { |
| 989 | struct rocker_desc_info *desc_info = &info->desc_info[i]; |
| 990 | struct rocker_desc *desc = &info->desc[i]; |
| 991 | dma_addr_t dma_handle; |
| 992 | char *buf; |
| 993 | |
| 994 | buf = kzalloc(buf_size, GFP_KERNEL | GFP_DMA); |
| 995 | if (!buf) { |
| 996 | err = -ENOMEM; |
| 997 | goto rollback; |
| 998 | } |
| 999 | |
| 1000 | dma_handle = pci_map_single(pdev, buf, buf_size, direction); |
| 1001 | if (pci_dma_mapping_error(pdev, dma_handle)) { |
| 1002 | kfree(buf); |
| 1003 | err = -EIO; |
| 1004 | goto rollback; |
| 1005 | } |
| 1006 | |
| 1007 | desc_info->data = buf; |
| 1008 | desc_info->data_size = buf_size; |
| 1009 | dma_unmap_addr_set(desc_info, mapaddr, dma_handle); |
| 1010 | |
| 1011 | desc->buf_addr = dma_handle; |
| 1012 | desc->buf_size = buf_size; |
| 1013 | } |
| 1014 | return 0; |
| 1015 | |
| 1016 | rollback: |
| 1017 | for (i--; i >= 0; i--) { |
| 1018 | struct rocker_desc_info *desc_info = &info->desc_info[i]; |
| 1019 | |
| 1020 | pci_unmap_single(pdev, dma_unmap_addr(desc_info, mapaddr), |
| 1021 | desc_info->data_size, direction); |
| 1022 | kfree(desc_info->data); |
| 1023 | } |
| 1024 | return err; |
| 1025 | } |
| 1026 | |
| 1027 | static void rocker_dma_ring_bufs_free(struct rocker *rocker, |
| 1028 | struct rocker_dma_ring_info *info, |
| 1029 | int direction) |
| 1030 | { |
| 1031 | struct pci_dev *pdev = rocker->pdev; |
| 1032 | int i; |
| 1033 | |
| 1034 | for (i = 0; i < info->size; i++) { |
| 1035 | struct rocker_desc_info *desc_info = &info->desc_info[i]; |
| 1036 | struct rocker_desc *desc = &info->desc[i]; |
| 1037 | |
| 1038 | desc->buf_addr = 0; |
| 1039 | desc->buf_size = 0; |
| 1040 | pci_unmap_single(pdev, dma_unmap_addr(desc_info, mapaddr), |
| 1041 | desc_info->data_size, direction); |
| 1042 | kfree(desc_info->data); |
| 1043 | } |
| 1044 | } |
| 1045 | |
| 1046 | static int rocker_dma_rings_init(struct rocker *rocker) |
| 1047 | { |
| 1048 | struct pci_dev *pdev = rocker->pdev; |
| 1049 | int err; |
| 1050 | |
| 1051 | err = rocker_dma_ring_create(rocker, ROCKER_DMA_CMD, |
| 1052 | ROCKER_DMA_CMD_DEFAULT_SIZE, |
| 1053 | &rocker->cmd_ring); |
| 1054 | if (err) { |
| 1055 | dev_err(&pdev->dev, "failed to create command dma ring\n"); |
| 1056 | return err; |
| 1057 | } |
| 1058 | |
| 1059 | spin_lock_init(&rocker->cmd_ring_lock); |
| 1060 | |
| 1061 | err = rocker_dma_ring_bufs_alloc(rocker, &rocker->cmd_ring, |
| 1062 | PCI_DMA_BIDIRECTIONAL, PAGE_SIZE); |
| 1063 | if (err) { |
| 1064 | dev_err(&pdev->dev, "failed to alloc command dma ring buffers\n"); |
| 1065 | goto err_dma_cmd_ring_bufs_alloc; |
| 1066 | } |
| 1067 | |
| 1068 | err = rocker_dma_ring_create(rocker, ROCKER_DMA_EVENT, |
| 1069 | ROCKER_DMA_EVENT_DEFAULT_SIZE, |
| 1070 | &rocker->event_ring); |
| 1071 | if (err) { |
| 1072 | dev_err(&pdev->dev, "failed to create event dma ring\n"); |
| 1073 | goto err_dma_event_ring_create; |
| 1074 | } |
| 1075 | |
| 1076 | err = rocker_dma_ring_bufs_alloc(rocker, &rocker->event_ring, |
| 1077 | PCI_DMA_FROMDEVICE, PAGE_SIZE); |
| 1078 | if (err) { |
| 1079 | dev_err(&pdev->dev, "failed to alloc event dma ring buffers\n"); |
| 1080 | goto err_dma_event_ring_bufs_alloc; |
| 1081 | } |
| 1082 | rocker_dma_ring_pass_to_producer(rocker, &rocker->event_ring); |
| 1083 | return 0; |
| 1084 | |
| 1085 | err_dma_event_ring_bufs_alloc: |
| 1086 | rocker_dma_ring_destroy(rocker, &rocker->event_ring); |
| 1087 | err_dma_event_ring_create: |
| 1088 | rocker_dma_ring_bufs_free(rocker, &rocker->cmd_ring, |
| 1089 | PCI_DMA_BIDIRECTIONAL); |
| 1090 | err_dma_cmd_ring_bufs_alloc: |
| 1091 | rocker_dma_ring_destroy(rocker, &rocker->cmd_ring); |
| 1092 | return err; |
| 1093 | } |
| 1094 | |
| 1095 | static void rocker_dma_rings_fini(struct rocker *rocker) |
| 1096 | { |
| 1097 | rocker_dma_ring_bufs_free(rocker, &rocker->event_ring, |
| 1098 | PCI_DMA_BIDIRECTIONAL); |
| 1099 | rocker_dma_ring_destroy(rocker, &rocker->event_ring); |
| 1100 | rocker_dma_ring_bufs_free(rocker, &rocker->cmd_ring, |
| 1101 | PCI_DMA_BIDIRECTIONAL); |
| 1102 | rocker_dma_ring_destroy(rocker, &rocker->cmd_ring); |
| 1103 | } |
| 1104 | |
| 1105 | static int rocker_dma_rx_ring_skb_map(struct rocker *rocker, |
| 1106 | struct rocker_port *rocker_port, |
| 1107 | struct rocker_desc_info *desc_info, |
| 1108 | struct sk_buff *skb, size_t buf_len) |
| 1109 | { |
| 1110 | struct pci_dev *pdev = rocker->pdev; |
| 1111 | dma_addr_t dma_handle; |
| 1112 | |
| 1113 | dma_handle = pci_map_single(pdev, skb->data, buf_len, |
| 1114 | PCI_DMA_FROMDEVICE); |
| 1115 | if (pci_dma_mapping_error(pdev, dma_handle)) |
| 1116 | return -EIO; |
| 1117 | if (rocker_tlv_put_u64(desc_info, ROCKER_TLV_RX_FRAG_ADDR, dma_handle)) |
| 1118 | goto tlv_put_failure; |
| 1119 | if (rocker_tlv_put_u16(desc_info, ROCKER_TLV_RX_FRAG_MAX_LEN, buf_len)) |
| 1120 | goto tlv_put_failure; |
| 1121 | return 0; |
| 1122 | |
| 1123 | tlv_put_failure: |
| 1124 | pci_unmap_single(pdev, dma_handle, buf_len, PCI_DMA_FROMDEVICE); |
| 1125 | desc_info->tlv_size = 0; |
| 1126 | return -EMSGSIZE; |
| 1127 | } |
| 1128 | |
| 1129 | static size_t rocker_port_rx_buf_len(struct rocker_port *rocker_port) |
| 1130 | { |
| 1131 | return rocker_port->dev->mtu + ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN; |
| 1132 | } |
| 1133 | |
| 1134 | static int rocker_dma_rx_ring_skb_alloc(struct rocker *rocker, |
| 1135 | struct rocker_port *rocker_port, |
| 1136 | struct rocker_desc_info *desc_info) |
| 1137 | { |
| 1138 | struct net_device *dev = rocker_port->dev; |
| 1139 | struct sk_buff *skb; |
| 1140 | size_t buf_len = rocker_port_rx_buf_len(rocker_port); |
| 1141 | int err; |
| 1142 | |
| 1143 | /* Ensure that hw will see tlv_size zero in case of an error. |
| 1144 | * That tells hw to use another descriptor. |
| 1145 | */ |
| 1146 | rocker_desc_cookie_ptr_set(desc_info, NULL); |
| 1147 | desc_info->tlv_size = 0; |
| 1148 | |
| 1149 | skb = netdev_alloc_skb_ip_align(dev, buf_len); |
| 1150 | if (!skb) |
| 1151 | return -ENOMEM; |
| 1152 | err = rocker_dma_rx_ring_skb_map(rocker, rocker_port, desc_info, |
| 1153 | skb, buf_len); |
| 1154 | if (err) { |
| 1155 | dev_kfree_skb_any(skb); |
| 1156 | return err; |
| 1157 | } |
| 1158 | rocker_desc_cookie_ptr_set(desc_info, skb); |
| 1159 | return 0; |
| 1160 | } |
| 1161 | |
| 1162 | static void rocker_dma_rx_ring_skb_unmap(struct rocker *rocker, |
| 1163 | struct rocker_tlv **attrs) |
| 1164 | { |
| 1165 | struct pci_dev *pdev = rocker->pdev; |
| 1166 | dma_addr_t dma_handle; |
| 1167 | size_t len; |
| 1168 | |
| 1169 | if (!attrs[ROCKER_TLV_RX_FRAG_ADDR] || |
| 1170 | !attrs[ROCKER_TLV_RX_FRAG_MAX_LEN]) |
| 1171 | return; |
| 1172 | dma_handle = rocker_tlv_get_u64(attrs[ROCKER_TLV_RX_FRAG_ADDR]); |
| 1173 | len = rocker_tlv_get_u16(attrs[ROCKER_TLV_RX_FRAG_MAX_LEN]); |
| 1174 | pci_unmap_single(pdev, dma_handle, len, PCI_DMA_FROMDEVICE); |
| 1175 | } |
| 1176 | |
| 1177 | static void rocker_dma_rx_ring_skb_free(struct rocker *rocker, |
| 1178 | struct rocker_desc_info *desc_info) |
| 1179 | { |
| 1180 | struct rocker_tlv *attrs[ROCKER_TLV_RX_MAX + 1]; |
| 1181 | struct sk_buff *skb = rocker_desc_cookie_ptr_get(desc_info); |
| 1182 | |
| 1183 | if (!skb) |
| 1184 | return; |
| 1185 | rocker_tlv_parse_desc(attrs, ROCKER_TLV_RX_MAX, desc_info); |
| 1186 | rocker_dma_rx_ring_skb_unmap(rocker, attrs); |
| 1187 | dev_kfree_skb_any(skb); |
| 1188 | } |
| 1189 | |
| 1190 | static int rocker_dma_rx_ring_skbs_alloc(struct rocker *rocker, |
| 1191 | struct rocker_port *rocker_port) |
| 1192 | { |
| 1193 | struct rocker_dma_ring_info *rx_ring = &rocker_port->rx_ring; |
| 1194 | int i; |
| 1195 | int err; |
| 1196 | |
| 1197 | for (i = 0; i < rx_ring->size; i++) { |
| 1198 | err = rocker_dma_rx_ring_skb_alloc(rocker, rocker_port, |
| 1199 | &rx_ring->desc_info[i]); |
| 1200 | if (err) |
| 1201 | goto rollback; |
| 1202 | } |
| 1203 | return 0; |
| 1204 | |
| 1205 | rollback: |
| 1206 | for (i--; i >= 0; i--) |
| 1207 | rocker_dma_rx_ring_skb_free(rocker, &rx_ring->desc_info[i]); |
| 1208 | return err; |
| 1209 | } |
| 1210 | |
| 1211 | static void rocker_dma_rx_ring_skbs_free(struct rocker *rocker, |
| 1212 | struct rocker_port *rocker_port) |
| 1213 | { |
| 1214 | struct rocker_dma_ring_info *rx_ring = &rocker_port->rx_ring; |
| 1215 | int i; |
| 1216 | |
| 1217 | for (i = 0; i < rx_ring->size; i++) |
| 1218 | rocker_dma_rx_ring_skb_free(rocker, &rx_ring->desc_info[i]); |
| 1219 | } |
| 1220 | |
| 1221 | static int rocker_port_dma_rings_init(struct rocker_port *rocker_port) |
| 1222 | { |
| 1223 | struct rocker *rocker = rocker_port->rocker; |
| 1224 | int err; |
| 1225 | |
| 1226 | err = rocker_dma_ring_create(rocker, |
| 1227 | ROCKER_DMA_TX(rocker_port->port_number), |
| 1228 | ROCKER_DMA_TX_DEFAULT_SIZE, |
| 1229 | &rocker_port->tx_ring); |
| 1230 | if (err) { |
| 1231 | netdev_err(rocker_port->dev, "failed to create tx dma ring\n"); |
| 1232 | return err; |
| 1233 | } |
| 1234 | |
| 1235 | err = rocker_dma_ring_bufs_alloc(rocker, &rocker_port->tx_ring, |
| 1236 | PCI_DMA_TODEVICE, |
| 1237 | ROCKER_DMA_TX_DESC_SIZE); |
| 1238 | if (err) { |
| 1239 | netdev_err(rocker_port->dev, "failed to alloc tx dma ring buffers\n"); |
| 1240 | goto err_dma_tx_ring_bufs_alloc; |
| 1241 | } |
| 1242 | |
| 1243 | err = rocker_dma_ring_create(rocker, |
| 1244 | ROCKER_DMA_RX(rocker_port->port_number), |
| 1245 | ROCKER_DMA_RX_DEFAULT_SIZE, |
| 1246 | &rocker_port->rx_ring); |
| 1247 | if (err) { |
| 1248 | netdev_err(rocker_port->dev, "failed to create rx dma ring\n"); |
| 1249 | goto err_dma_rx_ring_create; |
| 1250 | } |
| 1251 | |
| 1252 | err = rocker_dma_ring_bufs_alloc(rocker, &rocker_port->rx_ring, |
| 1253 | PCI_DMA_BIDIRECTIONAL, |
| 1254 | ROCKER_DMA_RX_DESC_SIZE); |
| 1255 | if (err) { |
| 1256 | netdev_err(rocker_port->dev, "failed to alloc rx dma ring buffers\n"); |
| 1257 | goto err_dma_rx_ring_bufs_alloc; |
| 1258 | } |
| 1259 | |
| 1260 | err = rocker_dma_rx_ring_skbs_alloc(rocker, rocker_port); |
| 1261 | if (err) { |
| 1262 | netdev_err(rocker_port->dev, "failed to alloc rx dma ring skbs\n"); |
| 1263 | goto err_dma_rx_ring_skbs_alloc; |
| 1264 | } |
| 1265 | rocker_dma_ring_pass_to_producer(rocker, &rocker_port->rx_ring); |
| 1266 | |
| 1267 | return 0; |
| 1268 | |
| 1269 | err_dma_rx_ring_skbs_alloc: |
| 1270 | rocker_dma_ring_bufs_free(rocker, &rocker_port->rx_ring, |
| 1271 | PCI_DMA_BIDIRECTIONAL); |
| 1272 | err_dma_rx_ring_bufs_alloc: |
| 1273 | rocker_dma_ring_destroy(rocker, &rocker_port->rx_ring); |
| 1274 | err_dma_rx_ring_create: |
| 1275 | rocker_dma_ring_bufs_free(rocker, &rocker_port->tx_ring, |
| 1276 | PCI_DMA_TODEVICE); |
| 1277 | err_dma_tx_ring_bufs_alloc: |
| 1278 | rocker_dma_ring_destroy(rocker, &rocker_port->tx_ring); |
| 1279 | return err; |
| 1280 | } |
| 1281 | |
| 1282 | static void rocker_port_dma_rings_fini(struct rocker_port *rocker_port) |
| 1283 | { |
| 1284 | struct rocker *rocker = rocker_port->rocker; |
| 1285 | |
| 1286 | rocker_dma_rx_ring_skbs_free(rocker, rocker_port); |
| 1287 | rocker_dma_ring_bufs_free(rocker, &rocker_port->rx_ring, |
| 1288 | PCI_DMA_BIDIRECTIONAL); |
| 1289 | rocker_dma_ring_destroy(rocker, &rocker_port->rx_ring); |
| 1290 | rocker_dma_ring_bufs_free(rocker, &rocker_port->tx_ring, |
| 1291 | PCI_DMA_TODEVICE); |
| 1292 | rocker_dma_ring_destroy(rocker, &rocker_port->tx_ring); |
| 1293 | } |
| 1294 | |
| 1295 | static void rocker_port_set_enable(struct rocker_port *rocker_port, bool enable) |
| 1296 | { |
| 1297 | u64 val = rocker_read64(rocker_port->rocker, PORT_PHYS_ENABLE); |
| 1298 | |
| 1299 | if (enable) |
David S. Miller | 71a83a6 | 2015-03-03 21:16:48 -0500 | [diff] [blame] | 1300 | val |= 1ULL << rocker_port->pport; |
Jiri Pirko | 4b8ac96 | 2014-11-28 14:34:26 +0100 | [diff] [blame] | 1301 | else |
David S. Miller | 71a83a6 | 2015-03-03 21:16:48 -0500 | [diff] [blame] | 1302 | val &= ~(1ULL << rocker_port->pport); |
Jiri Pirko | 4b8ac96 | 2014-11-28 14:34:26 +0100 | [diff] [blame] | 1303 | rocker_write64(rocker_port->rocker, PORT_PHYS_ENABLE, val); |
| 1304 | } |
| 1305 | |
| 1306 | /******************************** |
| 1307 | * Interrupt handler and helpers |
| 1308 | ********************************/ |
| 1309 | |
| 1310 | static irqreturn_t rocker_cmd_irq_handler(int irq, void *dev_id) |
| 1311 | { |
| 1312 | struct rocker *rocker = dev_id; |
| 1313 | struct rocker_desc_info *desc_info; |
| 1314 | struct rocker_wait *wait; |
| 1315 | u32 credits = 0; |
| 1316 | |
| 1317 | spin_lock(&rocker->cmd_ring_lock); |
| 1318 | while ((desc_info = rocker_desc_tail_get(&rocker->cmd_ring))) { |
| 1319 | wait = rocker_desc_cookie_ptr_get(desc_info); |
| 1320 | if (wait->nowait) { |
| 1321 | rocker_desc_gen_clear(desc_info); |
| 1322 | rocker_wait_destroy(wait); |
| 1323 | } else { |
| 1324 | rocker_wait_wake_up(wait); |
| 1325 | } |
| 1326 | credits++; |
| 1327 | } |
| 1328 | spin_unlock(&rocker->cmd_ring_lock); |
| 1329 | rocker_dma_ring_credits_set(rocker, &rocker->cmd_ring, credits); |
| 1330 | |
| 1331 | return IRQ_HANDLED; |
| 1332 | } |
| 1333 | |
| 1334 | static void rocker_port_link_up(struct rocker_port *rocker_port) |
| 1335 | { |
| 1336 | netif_carrier_on(rocker_port->dev); |
| 1337 | netdev_info(rocker_port->dev, "Link is up\n"); |
| 1338 | } |
| 1339 | |
| 1340 | static void rocker_port_link_down(struct rocker_port *rocker_port) |
| 1341 | { |
| 1342 | netif_carrier_off(rocker_port->dev); |
| 1343 | netdev_info(rocker_port->dev, "Link is down\n"); |
| 1344 | } |
| 1345 | |
| 1346 | static int rocker_event_link_change(struct rocker *rocker, |
| 1347 | const struct rocker_tlv *info) |
| 1348 | { |
| 1349 | struct rocker_tlv *attrs[ROCKER_TLV_EVENT_LINK_CHANGED_MAX + 1]; |
| 1350 | unsigned int port_number; |
| 1351 | bool link_up; |
| 1352 | struct rocker_port *rocker_port; |
| 1353 | |
| 1354 | rocker_tlv_parse_nested(attrs, ROCKER_TLV_EVENT_LINK_CHANGED_MAX, info); |
Scott Feldman | 4a6bb6d | 2015-02-25 20:15:37 -0800 | [diff] [blame] | 1355 | if (!attrs[ROCKER_TLV_EVENT_LINK_CHANGED_PPORT] || |
Jiri Pirko | 4b8ac96 | 2014-11-28 14:34:26 +0100 | [diff] [blame] | 1356 | !attrs[ROCKER_TLV_EVENT_LINK_CHANGED_LINKUP]) |
| 1357 | return -EIO; |
| 1358 | port_number = |
Scott Feldman | 4a6bb6d | 2015-02-25 20:15:37 -0800 | [diff] [blame] | 1359 | rocker_tlv_get_u32(attrs[ROCKER_TLV_EVENT_LINK_CHANGED_PPORT]) - 1; |
Jiri Pirko | 4b8ac96 | 2014-11-28 14:34:26 +0100 | [diff] [blame] | 1360 | link_up = rocker_tlv_get_u8(attrs[ROCKER_TLV_EVENT_LINK_CHANGED_LINKUP]); |
| 1361 | |
| 1362 | if (port_number >= rocker->port_count) |
| 1363 | return -EINVAL; |
| 1364 | |
| 1365 | rocker_port = rocker->ports[port_number]; |
| 1366 | if (netif_carrier_ok(rocker_port->dev) != link_up) { |
| 1367 | if (link_up) |
| 1368 | rocker_port_link_up(rocker_port); |
| 1369 | else |
| 1370 | rocker_port_link_down(rocker_port); |
| 1371 | } |
| 1372 | |
| 1373 | return 0; |
| 1374 | } |
| 1375 | |
Scott Feldman | 9f6bbf7 | 2014-11-28 14:34:27 +0100 | [diff] [blame] | 1376 | #define ROCKER_OP_FLAG_REMOVE BIT(0) |
| 1377 | #define ROCKER_OP_FLAG_NOWAIT BIT(1) |
| 1378 | #define ROCKER_OP_FLAG_LEARNED BIT(2) |
Scott Feldman | 6c70794 | 2014-11-28 14:34:28 +0100 | [diff] [blame] | 1379 | #define ROCKER_OP_FLAG_REFRESH BIT(3) |
| 1380 | |
| 1381 | static int rocker_port_fdb(struct rocker_port *rocker_port, |
| 1382 | const unsigned char *addr, |
| 1383 | __be16 vlan_id, int flags); |
| 1384 | |
| 1385 | static int rocker_event_mac_vlan_seen(struct rocker *rocker, |
| 1386 | const struct rocker_tlv *info) |
| 1387 | { |
| 1388 | struct rocker_tlv *attrs[ROCKER_TLV_EVENT_MAC_VLAN_MAX + 1]; |
| 1389 | unsigned int port_number; |
| 1390 | struct rocker_port *rocker_port; |
| 1391 | unsigned char *addr; |
| 1392 | int flags = ROCKER_OP_FLAG_NOWAIT | ROCKER_OP_FLAG_LEARNED; |
| 1393 | __be16 vlan_id; |
| 1394 | |
| 1395 | rocker_tlv_parse_nested(attrs, ROCKER_TLV_EVENT_MAC_VLAN_MAX, info); |
Scott Feldman | 4a6bb6d | 2015-02-25 20:15:37 -0800 | [diff] [blame] | 1396 | if (!attrs[ROCKER_TLV_EVENT_MAC_VLAN_PPORT] || |
Scott Feldman | 6c70794 | 2014-11-28 14:34:28 +0100 | [diff] [blame] | 1397 | !attrs[ROCKER_TLV_EVENT_MAC_VLAN_MAC] || |
| 1398 | !attrs[ROCKER_TLV_EVENT_MAC_VLAN_VLAN_ID]) |
| 1399 | return -EIO; |
| 1400 | port_number = |
Scott Feldman | 4a6bb6d | 2015-02-25 20:15:37 -0800 | [diff] [blame] | 1401 | rocker_tlv_get_u32(attrs[ROCKER_TLV_EVENT_MAC_VLAN_PPORT]) - 1; |
Scott Feldman | 6c70794 | 2014-11-28 14:34:28 +0100 | [diff] [blame] | 1402 | addr = rocker_tlv_data(attrs[ROCKER_TLV_EVENT_MAC_VLAN_MAC]); |
Jiri Pirko | 9b03c71 | 2014-12-03 14:14:53 +0100 | [diff] [blame] | 1403 | 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] | 1404 | |
| 1405 | if (port_number >= rocker->port_count) |
| 1406 | return -EINVAL; |
| 1407 | |
| 1408 | rocker_port = rocker->ports[port_number]; |
| 1409 | |
| 1410 | if (rocker_port->stp_state != BR_STATE_LEARNING && |
| 1411 | rocker_port->stp_state != BR_STATE_FORWARDING) |
| 1412 | return 0; |
| 1413 | |
| 1414 | return rocker_port_fdb(rocker_port, addr, vlan_id, flags); |
| 1415 | } |
Scott Feldman | 9f6bbf7 | 2014-11-28 14:34:27 +0100 | [diff] [blame] | 1416 | |
Jiri Pirko | 4b8ac96 | 2014-11-28 14:34:26 +0100 | [diff] [blame] | 1417 | static int rocker_event_process(struct rocker *rocker, |
| 1418 | struct rocker_desc_info *desc_info) |
| 1419 | { |
| 1420 | struct rocker_tlv *attrs[ROCKER_TLV_EVENT_MAX + 1]; |
| 1421 | struct rocker_tlv *info; |
| 1422 | u16 type; |
| 1423 | |
| 1424 | rocker_tlv_parse_desc(attrs, ROCKER_TLV_EVENT_MAX, desc_info); |
| 1425 | if (!attrs[ROCKER_TLV_EVENT_TYPE] || |
| 1426 | !attrs[ROCKER_TLV_EVENT_INFO]) |
| 1427 | return -EIO; |
| 1428 | |
| 1429 | type = rocker_tlv_get_u16(attrs[ROCKER_TLV_EVENT_TYPE]); |
| 1430 | info = attrs[ROCKER_TLV_EVENT_INFO]; |
| 1431 | |
| 1432 | switch (type) { |
| 1433 | case ROCKER_TLV_EVENT_TYPE_LINK_CHANGED: |
| 1434 | return rocker_event_link_change(rocker, info); |
Scott Feldman | 6c70794 | 2014-11-28 14:34:28 +0100 | [diff] [blame] | 1435 | case ROCKER_TLV_EVENT_TYPE_MAC_VLAN_SEEN: |
| 1436 | return rocker_event_mac_vlan_seen(rocker, info); |
Jiri Pirko | 4b8ac96 | 2014-11-28 14:34:26 +0100 | [diff] [blame] | 1437 | } |
| 1438 | |
| 1439 | return -EOPNOTSUPP; |
| 1440 | } |
| 1441 | |
| 1442 | static irqreturn_t rocker_event_irq_handler(int irq, void *dev_id) |
| 1443 | { |
| 1444 | struct rocker *rocker = dev_id; |
| 1445 | struct pci_dev *pdev = rocker->pdev; |
| 1446 | struct rocker_desc_info *desc_info; |
| 1447 | u32 credits = 0; |
| 1448 | int err; |
| 1449 | |
| 1450 | while ((desc_info = rocker_desc_tail_get(&rocker->event_ring))) { |
| 1451 | err = rocker_desc_err(desc_info); |
| 1452 | if (err) { |
| 1453 | dev_err(&pdev->dev, "event desc received with err %d\n", |
| 1454 | err); |
| 1455 | } else { |
| 1456 | err = rocker_event_process(rocker, desc_info); |
| 1457 | if (err) |
| 1458 | dev_err(&pdev->dev, "event processing failed with err %d\n", |
| 1459 | err); |
| 1460 | } |
| 1461 | rocker_desc_gen_clear(desc_info); |
| 1462 | rocker_desc_head_set(rocker, &rocker->event_ring, desc_info); |
| 1463 | credits++; |
| 1464 | } |
| 1465 | rocker_dma_ring_credits_set(rocker, &rocker->event_ring, credits); |
| 1466 | |
| 1467 | return IRQ_HANDLED; |
| 1468 | } |
| 1469 | |
| 1470 | static irqreturn_t rocker_tx_irq_handler(int irq, void *dev_id) |
| 1471 | { |
| 1472 | struct rocker_port *rocker_port = dev_id; |
| 1473 | |
| 1474 | napi_schedule(&rocker_port->napi_tx); |
| 1475 | return IRQ_HANDLED; |
| 1476 | } |
| 1477 | |
| 1478 | static irqreturn_t rocker_rx_irq_handler(int irq, void *dev_id) |
| 1479 | { |
| 1480 | struct rocker_port *rocker_port = dev_id; |
| 1481 | |
| 1482 | napi_schedule(&rocker_port->napi_rx); |
| 1483 | return IRQ_HANDLED; |
| 1484 | } |
| 1485 | |
| 1486 | /******************** |
| 1487 | * Command interface |
| 1488 | ********************/ |
| 1489 | |
| 1490 | typedef int (*rocker_cmd_cb_t)(struct rocker *rocker, |
| 1491 | struct rocker_port *rocker_port, |
| 1492 | struct rocker_desc_info *desc_info, |
| 1493 | void *priv); |
| 1494 | |
| 1495 | static int rocker_cmd_exec(struct rocker *rocker, |
| 1496 | struct rocker_port *rocker_port, |
| 1497 | rocker_cmd_cb_t prepare, void *prepare_priv, |
| 1498 | rocker_cmd_cb_t process, void *process_priv, |
| 1499 | bool nowait) |
| 1500 | { |
| 1501 | struct rocker_desc_info *desc_info; |
| 1502 | struct rocker_wait *wait; |
| 1503 | unsigned long flags; |
| 1504 | int err; |
| 1505 | |
| 1506 | wait = rocker_wait_create(nowait ? GFP_ATOMIC : GFP_KERNEL); |
| 1507 | if (!wait) |
| 1508 | return -ENOMEM; |
| 1509 | wait->nowait = nowait; |
| 1510 | |
| 1511 | spin_lock_irqsave(&rocker->cmd_ring_lock, flags); |
| 1512 | desc_info = rocker_desc_head_get(&rocker->cmd_ring); |
| 1513 | if (!desc_info) { |
| 1514 | spin_unlock_irqrestore(&rocker->cmd_ring_lock, flags); |
| 1515 | err = -EAGAIN; |
| 1516 | goto out; |
| 1517 | } |
| 1518 | err = prepare(rocker, rocker_port, desc_info, prepare_priv); |
| 1519 | if (err) { |
| 1520 | spin_unlock_irqrestore(&rocker->cmd_ring_lock, flags); |
| 1521 | goto out; |
| 1522 | } |
| 1523 | rocker_desc_cookie_ptr_set(desc_info, wait); |
| 1524 | rocker_desc_head_set(rocker, &rocker->cmd_ring, desc_info); |
| 1525 | spin_unlock_irqrestore(&rocker->cmd_ring_lock, flags); |
| 1526 | |
| 1527 | if (nowait) |
| 1528 | return 0; |
| 1529 | |
| 1530 | if (!rocker_wait_event_timeout(wait, HZ / 10)) |
| 1531 | return -EIO; |
| 1532 | |
| 1533 | err = rocker_desc_err(desc_info); |
| 1534 | if (err) |
| 1535 | return err; |
| 1536 | |
| 1537 | if (process) |
| 1538 | err = process(rocker, rocker_port, desc_info, process_priv); |
| 1539 | |
| 1540 | rocker_desc_gen_clear(desc_info); |
| 1541 | out: |
| 1542 | rocker_wait_destroy(wait); |
| 1543 | return err; |
| 1544 | } |
| 1545 | |
| 1546 | static int |
| 1547 | rocker_cmd_get_port_settings_prep(struct rocker *rocker, |
| 1548 | struct rocker_port *rocker_port, |
| 1549 | struct rocker_desc_info *desc_info, |
| 1550 | void *priv) |
| 1551 | { |
| 1552 | struct rocker_tlv *cmd_info; |
| 1553 | |
| 1554 | if (rocker_tlv_put_u16(desc_info, ROCKER_TLV_CMD_TYPE, |
| 1555 | ROCKER_TLV_CMD_TYPE_GET_PORT_SETTINGS)) |
| 1556 | return -EMSGSIZE; |
| 1557 | cmd_info = rocker_tlv_nest_start(desc_info, ROCKER_TLV_CMD_INFO); |
| 1558 | if (!cmd_info) |
| 1559 | return -EMSGSIZE; |
Scott Feldman | 4a6bb6d | 2015-02-25 20:15:37 -0800 | [diff] [blame] | 1560 | if (rocker_tlv_put_u32(desc_info, ROCKER_TLV_CMD_PORT_SETTINGS_PPORT, |
| 1561 | rocker_port->pport)) |
Jiri Pirko | 4b8ac96 | 2014-11-28 14:34:26 +0100 | [diff] [blame] | 1562 | return -EMSGSIZE; |
| 1563 | rocker_tlv_nest_end(desc_info, cmd_info); |
| 1564 | return 0; |
| 1565 | } |
| 1566 | |
| 1567 | static int |
| 1568 | rocker_cmd_get_port_settings_ethtool_proc(struct rocker *rocker, |
| 1569 | struct rocker_port *rocker_port, |
| 1570 | struct rocker_desc_info *desc_info, |
| 1571 | void *priv) |
| 1572 | { |
| 1573 | struct ethtool_cmd *ecmd = priv; |
| 1574 | struct rocker_tlv *attrs[ROCKER_TLV_CMD_MAX + 1]; |
| 1575 | struct rocker_tlv *info_attrs[ROCKER_TLV_CMD_PORT_SETTINGS_MAX + 1]; |
| 1576 | u32 speed; |
| 1577 | u8 duplex; |
| 1578 | u8 autoneg; |
| 1579 | |
| 1580 | rocker_tlv_parse_desc(attrs, ROCKER_TLV_CMD_MAX, desc_info); |
| 1581 | if (!attrs[ROCKER_TLV_CMD_INFO]) |
| 1582 | return -EIO; |
| 1583 | |
| 1584 | rocker_tlv_parse_nested(info_attrs, ROCKER_TLV_CMD_PORT_SETTINGS_MAX, |
| 1585 | attrs[ROCKER_TLV_CMD_INFO]); |
| 1586 | if (!info_attrs[ROCKER_TLV_CMD_PORT_SETTINGS_SPEED] || |
| 1587 | !info_attrs[ROCKER_TLV_CMD_PORT_SETTINGS_DUPLEX] || |
| 1588 | !info_attrs[ROCKER_TLV_CMD_PORT_SETTINGS_AUTONEG]) |
| 1589 | return -EIO; |
| 1590 | |
| 1591 | speed = rocker_tlv_get_u32(info_attrs[ROCKER_TLV_CMD_PORT_SETTINGS_SPEED]); |
| 1592 | duplex = rocker_tlv_get_u8(info_attrs[ROCKER_TLV_CMD_PORT_SETTINGS_DUPLEX]); |
| 1593 | autoneg = rocker_tlv_get_u8(info_attrs[ROCKER_TLV_CMD_PORT_SETTINGS_AUTONEG]); |
| 1594 | |
| 1595 | ecmd->transceiver = XCVR_INTERNAL; |
| 1596 | ecmd->supported = SUPPORTED_TP; |
| 1597 | ecmd->phy_address = 0xff; |
| 1598 | ecmd->port = PORT_TP; |
| 1599 | ethtool_cmd_speed_set(ecmd, speed); |
| 1600 | ecmd->duplex = duplex ? DUPLEX_FULL : DUPLEX_HALF; |
| 1601 | ecmd->autoneg = autoneg ? AUTONEG_ENABLE : AUTONEG_DISABLE; |
| 1602 | |
| 1603 | return 0; |
| 1604 | } |
| 1605 | |
| 1606 | static int |
| 1607 | rocker_cmd_get_port_settings_macaddr_proc(struct rocker *rocker, |
| 1608 | struct rocker_port *rocker_port, |
| 1609 | struct rocker_desc_info *desc_info, |
| 1610 | void *priv) |
| 1611 | { |
| 1612 | unsigned char *macaddr = priv; |
| 1613 | struct rocker_tlv *attrs[ROCKER_TLV_CMD_MAX + 1]; |
| 1614 | struct rocker_tlv *info_attrs[ROCKER_TLV_CMD_PORT_SETTINGS_MAX + 1]; |
| 1615 | struct rocker_tlv *attr; |
| 1616 | |
| 1617 | rocker_tlv_parse_desc(attrs, ROCKER_TLV_CMD_MAX, desc_info); |
| 1618 | if (!attrs[ROCKER_TLV_CMD_INFO]) |
| 1619 | return -EIO; |
| 1620 | |
| 1621 | rocker_tlv_parse_nested(info_attrs, ROCKER_TLV_CMD_PORT_SETTINGS_MAX, |
| 1622 | attrs[ROCKER_TLV_CMD_INFO]); |
| 1623 | attr = info_attrs[ROCKER_TLV_CMD_PORT_SETTINGS_MACADDR]; |
| 1624 | if (!attr) |
| 1625 | return -EIO; |
| 1626 | |
| 1627 | if (rocker_tlv_len(attr) != ETH_ALEN) |
| 1628 | return -EINVAL; |
| 1629 | |
| 1630 | ether_addr_copy(macaddr, rocker_tlv_data(attr)); |
| 1631 | return 0; |
| 1632 | } |
| 1633 | |
David Ahern | db19170 | 2015-03-17 20:23:16 -0600 | [diff] [blame] | 1634 | struct port_name { |
| 1635 | char *buf; |
| 1636 | size_t len; |
| 1637 | }; |
| 1638 | |
| 1639 | static int |
| 1640 | rocker_cmd_get_port_settings_phys_name_proc(struct rocker *rocker, |
| 1641 | struct rocker_port *rocker_port, |
| 1642 | struct rocker_desc_info *desc_info, |
| 1643 | void *priv) |
| 1644 | { |
| 1645 | struct rocker_tlv *info_attrs[ROCKER_TLV_CMD_PORT_SETTINGS_MAX + 1]; |
| 1646 | struct rocker_tlv *attrs[ROCKER_TLV_CMD_MAX + 1]; |
| 1647 | struct port_name *name = priv; |
| 1648 | struct rocker_tlv *attr; |
| 1649 | size_t i, j, len; |
| 1650 | char *str; |
| 1651 | |
| 1652 | rocker_tlv_parse_desc(attrs, ROCKER_TLV_CMD_MAX, desc_info); |
| 1653 | if (!attrs[ROCKER_TLV_CMD_INFO]) |
| 1654 | return -EIO; |
| 1655 | |
| 1656 | rocker_tlv_parse_nested(info_attrs, ROCKER_TLV_CMD_PORT_SETTINGS_MAX, |
| 1657 | attrs[ROCKER_TLV_CMD_INFO]); |
| 1658 | attr = info_attrs[ROCKER_TLV_CMD_PORT_SETTINGS_PHYS_NAME]; |
| 1659 | if (!attr) |
| 1660 | return -EIO; |
| 1661 | |
| 1662 | len = min_t(size_t, rocker_tlv_len(attr), name->len); |
| 1663 | str = rocker_tlv_data(attr); |
| 1664 | |
| 1665 | /* make sure name only contains alphanumeric characters */ |
| 1666 | for (i = j = 0; i < len; ++i) { |
| 1667 | if (isalnum(str[i])) { |
| 1668 | name->buf[j] = str[i]; |
| 1669 | j++; |
| 1670 | } |
| 1671 | } |
| 1672 | |
| 1673 | if (j == 0) |
| 1674 | return -EIO; |
| 1675 | |
| 1676 | name->buf[j] = '\0'; |
| 1677 | |
| 1678 | return 0; |
| 1679 | } |
| 1680 | |
Jiri Pirko | 4b8ac96 | 2014-11-28 14:34:26 +0100 | [diff] [blame] | 1681 | static int |
| 1682 | rocker_cmd_set_port_settings_ethtool_prep(struct rocker *rocker, |
| 1683 | struct rocker_port *rocker_port, |
| 1684 | struct rocker_desc_info *desc_info, |
| 1685 | void *priv) |
| 1686 | { |
| 1687 | struct ethtool_cmd *ecmd = priv; |
| 1688 | struct rocker_tlv *cmd_info; |
| 1689 | |
| 1690 | if (rocker_tlv_put_u16(desc_info, ROCKER_TLV_CMD_TYPE, |
| 1691 | ROCKER_TLV_CMD_TYPE_SET_PORT_SETTINGS)) |
| 1692 | return -EMSGSIZE; |
| 1693 | cmd_info = rocker_tlv_nest_start(desc_info, ROCKER_TLV_CMD_INFO); |
| 1694 | if (!cmd_info) |
| 1695 | return -EMSGSIZE; |
Scott Feldman | 4a6bb6d | 2015-02-25 20:15:37 -0800 | [diff] [blame] | 1696 | if (rocker_tlv_put_u32(desc_info, ROCKER_TLV_CMD_PORT_SETTINGS_PPORT, |
| 1697 | rocker_port->pport)) |
Jiri Pirko | 4b8ac96 | 2014-11-28 14:34:26 +0100 | [diff] [blame] | 1698 | return -EMSGSIZE; |
| 1699 | if (rocker_tlv_put_u32(desc_info, ROCKER_TLV_CMD_PORT_SETTINGS_SPEED, |
| 1700 | ethtool_cmd_speed(ecmd))) |
| 1701 | return -EMSGSIZE; |
| 1702 | if (rocker_tlv_put_u8(desc_info, ROCKER_TLV_CMD_PORT_SETTINGS_DUPLEX, |
| 1703 | ecmd->duplex)) |
| 1704 | return -EMSGSIZE; |
| 1705 | if (rocker_tlv_put_u8(desc_info, ROCKER_TLV_CMD_PORT_SETTINGS_AUTONEG, |
| 1706 | ecmd->autoneg)) |
| 1707 | return -EMSGSIZE; |
| 1708 | rocker_tlv_nest_end(desc_info, cmd_info); |
| 1709 | return 0; |
| 1710 | } |
| 1711 | |
| 1712 | static int |
| 1713 | rocker_cmd_set_port_settings_macaddr_prep(struct rocker *rocker, |
| 1714 | struct rocker_port *rocker_port, |
| 1715 | struct rocker_desc_info *desc_info, |
| 1716 | void *priv) |
| 1717 | { |
| 1718 | unsigned char *macaddr = priv; |
| 1719 | struct rocker_tlv *cmd_info; |
| 1720 | |
| 1721 | if (rocker_tlv_put_u16(desc_info, ROCKER_TLV_CMD_TYPE, |
| 1722 | ROCKER_TLV_CMD_TYPE_SET_PORT_SETTINGS)) |
| 1723 | return -EMSGSIZE; |
| 1724 | cmd_info = rocker_tlv_nest_start(desc_info, ROCKER_TLV_CMD_INFO); |
| 1725 | if (!cmd_info) |
| 1726 | return -EMSGSIZE; |
Scott Feldman | 4a6bb6d | 2015-02-25 20:15:37 -0800 | [diff] [blame] | 1727 | if (rocker_tlv_put_u32(desc_info, ROCKER_TLV_CMD_PORT_SETTINGS_PPORT, |
| 1728 | rocker_port->pport)) |
Jiri Pirko | 4b8ac96 | 2014-11-28 14:34:26 +0100 | [diff] [blame] | 1729 | return -EMSGSIZE; |
| 1730 | if (rocker_tlv_put(desc_info, ROCKER_TLV_CMD_PORT_SETTINGS_MACADDR, |
| 1731 | ETH_ALEN, macaddr)) |
| 1732 | return -EMSGSIZE; |
| 1733 | rocker_tlv_nest_end(desc_info, cmd_info); |
| 1734 | return 0; |
| 1735 | } |
| 1736 | |
Scott Feldman | 5111f80 | 2014-11-28 14:34:30 +0100 | [diff] [blame] | 1737 | static int |
| 1738 | rocker_cmd_set_port_learning_prep(struct rocker *rocker, |
| 1739 | struct rocker_port *rocker_port, |
| 1740 | struct rocker_desc_info *desc_info, |
| 1741 | void *priv) |
| 1742 | { |
| 1743 | struct rocker_tlv *cmd_info; |
| 1744 | |
| 1745 | if (rocker_tlv_put_u16(desc_info, ROCKER_TLV_CMD_TYPE, |
| 1746 | ROCKER_TLV_CMD_TYPE_SET_PORT_SETTINGS)) |
| 1747 | return -EMSGSIZE; |
| 1748 | cmd_info = rocker_tlv_nest_start(desc_info, ROCKER_TLV_CMD_INFO); |
| 1749 | if (!cmd_info) |
| 1750 | return -EMSGSIZE; |
Scott Feldman | 4a6bb6d | 2015-02-25 20:15:37 -0800 | [diff] [blame] | 1751 | if (rocker_tlv_put_u32(desc_info, ROCKER_TLV_CMD_PORT_SETTINGS_PPORT, |
| 1752 | rocker_port->pport)) |
Scott Feldman | 5111f80 | 2014-11-28 14:34:30 +0100 | [diff] [blame] | 1753 | return -EMSGSIZE; |
| 1754 | if (rocker_tlv_put_u8(desc_info, ROCKER_TLV_CMD_PORT_SETTINGS_LEARNING, |
| 1755 | !!(rocker_port->brport_flags & BR_LEARNING))) |
| 1756 | return -EMSGSIZE; |
| 1757 | rocker_tlv_nest_end(desc_info, cmd_info); |
| 1758 | return 0; |
| 1759 | } |
| 1760 | |
Jiri Pirko | 4b8ac96 | 2014-11-28 14:34:26 +0100 | [diff] [blame] | 1761 | static int rocker_cmd_get_port_settings_ethtool(struct rocker_port *rocker_port, |
| 1762 | struct ethtool_cmd *ecmd) |
| 1763 | { |
| 1764 | return rocker_cmd_exec(rocker_port->rocker, rocker_port, |
| 1765 | rocker_cmd_get_port_settings_prep, NULL, |
| 1766 | rocker_cmd_get_port_settings_ethtool_proc, |
| 1767 | ecmd, false); |
| 1768 | } |
| 1769 | |
| 1770 | static int rocker_cmd_get_port_settings_macaddr(struct rocker_port *rocker_port, |
| 1771 | unsigned char *macaddr) |
| 1772 | { |
| 1773 | return rocker_cmd_exec(rocker_port->rocker, rocker_port, |
| 1774 | rocker_cmd_get_port_settings_prep, NULL, |
| 1775 | rocker_cmd_get_port_settings_macaddr_proc, |
| 1776 | macaddr, false); |
| 1777 | } |
| 1778 | |
| 1779 | static int rocker_cmd_set_port_settings_ethtool(struct rocker_port *rocker_port, |
| 1780 | struct ethtool_cmd *ecmd) |
| 1781 | { |
| 1782 | return rocker_cmd_exec(rocker_port->rocker, rocker_port, |
| 1783 | rocker_cmd_set_port_settings_ethtool_prep, |
| 1784 | ecmd, NULL, NULL, false); |
| 1785 | } |
| 1786 | |
| 1787 | static int rocker_cmd_set_port_settings_macaddr(struct rocker_port *rocker_port, |
| 1788 | unsigned char *macaddr) |
| 1789 | { |
| 1790 | return rocker_cmd_exec(rocker_port->rocker, rocker_port, |
| 1791 | rocker_cmd_set_port_settings_macaddr_prep, |
| 1792 | macaddr, NULL, NULL, false); |
| 1793 | } |
| 1794 | |
Scott Feldman | 5111f80 | 2014-11-28 14:34:30 +0100 | [diff] [blame] | 1795 | static int rocker_port_set_learning(struct rocker_port *rocker_port) |
| 1796 | { |
| 1797 | return rocker_cmd_exec(rocker_port->rocker, rocker_port, |
| 1798 | rocker_cmd_set_port_learning_prep, |
| 1799 | NULL, NULL, NULL, false); |
| 1800 | } |
| 1801 | |
Scott Feldman | 9f6bbf7 | 2014-11-28 14:34:27 +0100 | [diff] [blame] | 1802 | static int rocker_cmd_flow_tbl_add_ig_port(struct rocker_desc_info *desc_info, |
| 1803 | struct rocker_flow_tbl_entry *entry) |
| 1804 | { |
Scott Feldman | 4a6bb6d | 2015-02-25 20:15:37 -0800 | [diff] [blame] | 1805 | if (rocker_tlv_put_u32(desc_info, ROCKER_TLV_OF_DPA_IN_PPORT, |
| 1806 | entry->key.ig_port.in_pport)) |
Scott Feldman | 9f6bbf7 | 2014-11-28 14:34:27 +0100 | [diff] [blame] | 1807 | return -EMSGSIZE; |
Scott Feldman | 4a6bb6d | 2015-02-25 20:15:37 -0800 | [diff] [blame] | 1808 | if (rocker_tlv_put_u32(desc_info, ROCKER_TLV_OF_DPA_IN_PPORT_MASK, |
| 1809 | entry->key.ig_port.in_pport_mask)) |
Scott Feldman | 9f6bbf7 | 2014-11-28 14:34:27 +0100 | [diff] [blame] | 1810 | return -EMSGSIZE; |
| 1811 | if (rocker_tlv_put_u16(desc_info, ROCKER_TLV_OF_DPA_GOTO_TABLE_ID, |
| 1812 | entry->key.ig_port.goto_tbl)) |
| 1813 | return -EMSGSIZE; |
| 1814 | |
| 1815 | return 0; |
| 1816 | } |
| 1817 | |
| 1818 | static int rocker_cmd_flow_tbl_add_vlan(struct rocker_desc_info *desc_info, |
| 1819 | struct rocker_flow_tbl_entry *entry) |
| 1820 | { |
Scott Feldman | 4a6bb6d | 2015-02-25 20:15:37 -0800 | [diff] [blame] | 1821 | if (rocker_tlv_put_u32(desc_info, ROCKER_TLV_OF_DPA_IN_PPORT, |
| 1822 | entry->key.vlan.in_pport)) |
Scott Feldman | 9f6bbf7 | 2014-11-28 14:34:27 +0100 | [diff] [blame] | 1823 | return -EMSGSIZE; |
Jiri Pirko | 9b03c71 | 2014-12-03 14:14:53 +0100 | [diff] [blame] | 1824 | if (rocker_tlv_put_be16(desc_info, ROCKER_TLV_OF_DPA_VLAN_ID, |
| 1825 | entry->key.vlan.vlan_id)) |
Scott Feldman | 9f6bbf7 | 2014-11-28 14:34:27 +0100 | [diff] [blame] | 1826 | return -EMSGSIZE; |
Jiri Pirko | 9b03c71 | 2014-12-03 14:14:53 +0100 | [diff] [blame] | 1827 | if (rocker_tlv_put_be16(desc_info, ROCKER_TLV_OF_DPA_VLAN_ID_MASK, |
| 1828 | entry->key.vlan.vlan_id_mask)) |
Scott Feldman | 9f6bbf7 | 2014-11-28 14:34:27 +0100 | [diff] [blame] | 1829 | return -EMSGSIZE; |
| 1830 | if (rocker_tlv_put_u16(desc_info, ROCKER_TLV_OF_DPA_GOTO_TABLE_ID, |
| 1831 | entry->key.vlan.goto_tbl)) |
| 1832 | return -EMSGSIZE; |
| 1833 | if (entry->key.vlan.untagged && |
Jiri Pirko | 9b03c71 | 2014-12-03 14:14:53 +0100 | [diff] [blame] | 1834 | rocker_tlv_put_be16(desc_info, ROCKER_TLV_OF_DPA_NEW_VLAN_ID, |
| 1835 | entry->key.vlan.new_vlan_id)) |
Scott Feldman | 9f6bbf7 | 2014-11-28 14:34:27 +0100 | [diff] [blame] | 1836 | return -EMSGSIZE; |
| 1837 | |
| 1838 | return 0; |
| 1839 | } |
| 1840 | |
| 1841 | static int rocker_cmd_flow_tbl_add_term_mac(struct rocker_desc_info *desc_info, |
| 1842 | struct rocker_flow_tbl_entry *entry) |
| 1843 | { |
Scott Feldman | 4a6bb6d | 2015-02-25 20:15:37 -0800 | [diff] [blame] | 1844 | if (rocker_tlv_put_u32(desc_info, ROCKER_TLV_OF_DPA_IN_PPORT, |
| 1845 | entry->key.term_mac.in_pport)) |
Scott Feldman | 9f6bbf7 | 2014-11-28 14:34:27 +0100 | [diff] [blame] | 1846 | return -EMSGSIZE; |
Scott Feldman | 4a6bb6d | 2015-02-25 20:15:37 -0800 | [diff] [blame] | 1847 | if (rocker_tlv_put_u32(desc_info, ROCKER_TLV_OF_DPA_IN_PPORT_MASK, |
| 1848 | entry->key.term_mac.in_pport_mask)) |
Scott Feldman | 9f6bbf7 | 2014-11-28 14:34:27 +0100 | [diff] [blame] | 1849 | return -EMSGSIZE; |
Jiri Pirko | 9b03c71 | 2014-12-03 14:14:53 +0100 | [diff] [blame] | 1850 | if (rocker_tlv_put_be16(desc_info, ROCKER_TLV_OF_DPA_ETHERTYPE, |
| 1851 | entry->key.term_mac.eth_type)) |
Scott Feldman | 9f6bbf7 | 2014-11-28 14:34:27 +0100 | [diff] [blame] | 1852 | return -EMSGSIZE; |
| 1853 | if (rocker_tlv_put(desc_info, ROCKER_TLV_OF_DPA_DST_MAC, |
| 1854 | ETH_ALEN, entry->key.term_mac.eth_dst)) |
| 1855 | return -EMSGSIZE; |
| 1856 | if (rocker_tlv_put(desc_info, ROCKER_TLV_OF_DPA_DST_MAC_MASK, |
| 1857 | ETH_ALEN, entry->key.term_mac.eth_dst_mask)) |
| 1858 | return -EMSGSIZE; |
Jiri Pirko | 9b03c71 | 2014-12-03 14:14:53 +0100 | [diff] [blame] | 1859 | if (rocker_tlv_put_be16(desc_info, ROCKER_TLV_OF_DPA_VLAN_ID, |
| 1860 | entry->key.term_mac.vlan_id)) |
Scott Feldman | 9f6bbf7 | 2014-11-28 14:34:27 +0100 | [diff] [blame] | 1861 | return -EMSGSIZE; |
Jiri Pirko | 9b03c71 | 2014-12-03 14:14:53 +0100 | [diff] [blame] | 1862 | if (rocker_tlv_put_be16(desc_info, ROCKER_TLV_OF_DPA_VLAN_ID_MASK, |
| 1863 | entry->key.term_mac.vlan_id_mask)) |
Scott Feldman | 9f6bbf7 | 2014-11-28 14:34:27 +0100 | [diff] [blame] | 1864 | return -EMSGSIZE; |
| 1865 | if (rocker_tlv_put_u16(desc_info, ROCKER_TLV_OF_DPA_GOTO_TABLE_ID, |
| 1866 | entry->key.term_mac.goto_tbl)) |
| 1867 | return -EMSGSIZE; |
| 1868 | if (entry->key.term_mac.copy_to_cpu && |
| 1869 | rocker_tlv_put_u8(desc_info, ROCKER_TLV_OF_DPA_COPY_CPU_ACTION, |
| 1870 | entry->key.term_mac.copy_to_cpu)) |
| 1871 | return -EMSGSIZE; |
| 1872 | |
| 1873 | return 0; |
| 1874 | } |
| 1875 | |
| 1876 | static int |
| 1877 | rocker_cmd_flow_tbl_add_ucast_routing(struct rocker_desc_info *desc_info, |
| 1878 | struct rocker_flow_tbl_entry *entry) |
| 1879 | { |
Jiri Pirko | 9b03c71 | 2014-12-03 14:14:53 +0100 | [diff] [blame] | 1880 | if (rocker_tlv_put_be16(desc_info, ROCKER_TLV_OF_DPA_ETHERTYPE, |
| 1881 | entry->key.ucast_routing.eth_type)) |
Scott Feldman | 9f6bbf7 | 2014-11-28 14:34:27 +0100 | [diff] [blame] | 1882 | return -EMSGSIZE; |
Jiri Pirko | 9b03c71 | 2014-12-03 14:14:53 +0100 | [diff] [blame] | 1883 | if (rocker_tlv_put_be32(desc_info, ROCKER_TLV_OF_DPA_DST_IP, |
| 1884 | entry->key.ucast_routing.dst4)) |
Scott Feldman | 9f6bbf7 | 2014-11-28 14:34:27 +0100 | [diff] [blame] | 1885 | return -EMSGSIZE; |
Jiri Pirko | 9b03c71 | 2014-12-03 14:14:53 +0100 | [diff] [blame] | 1886 | if (rocker_tlv_put_be32(desc_info, ROCKER_TLV_OF_DPA_DST_IP_MASK, |
| 1887 | entry->key.ucast_routing.dst4_mask)) |
Scott Feldman | 9f6bbf7 | 2014-11-28 14:34:27 +0100 | [diff] [blame] | 1888 | return -EMSGSIZE; |
| 1889 | if (rocker_tlv_put_u16(desc_info, ROCKER_TLV_OF_DPA_GOTO_TABLE_ID, |
| 1890 | entry->key.ucast_routing.goto_tbl)) |
| 1891 | return -EMSGSIZE; |
| 1892 | if (rocker_tlv_put_u32(desc_info, ROCKER_TLV_OF_DPA_GROUP_ID, |
| 1893 | entry->key.ucast_routing.group_id)) |
| 1894 | return -EMSGSIZE; |
| 1895 | |
| 1896 | return 0; |
| 1897 | } |
| 1898 | |
| 1899 | static int rocker_cmd_flow_tbl_add_bridge(struct rocker_desc_info *desc_info, |
| 1900 | struct rocker_flow_tbl_entry *entry) |
| 1901 | { |
| 1902 | if (entry->key.bridge.has_eth_dst && |
| 1903 | rocker_tlv_put(desc_info, ROCKER_TLV_OF_DPA_DST_MAC, |
| 1904 | ETH_ALEN, entry->key.bridge.eth_dst)) |
| 1905 | return -EMSGSIZE; |
| 1906 | if (entry->key.bridge.has_eth_dst_mask && |
| 1907 | rocker_tlv_put(desc_info, ROCKER_TLV_OF_DPA_DST_MAC_MASK, |
| 1908 | ETH_ALEN, entry->key.bridge.eth_dst_mask)) |
| 1909 | return -EMSGSIZE; |
| 1910 | if (entry->key.bridge.vlan_id && |
Jiri Pirko | 9b03c71 | 2014-12-03 14:14:53 +0100 | [diff] [blame] | 1911 | rocker_tlv_put_be16(desc_info, ROCKER_TLV_OF_DPA_VLAN_ID, |
| 1912 | entry->key.bridge.vlan_id)) |
Scott Feldman | 9f6bbf7 | 2014-11-28 14:34:27 +0100 | [diff] [blame] | 1913 | return -EMSGSIZE; |
| 1914 | if (entry->key.bridge.tunnel_id && |
| 1915 | rocker_tlv_put_u32(desc_info, ROCKER_TLV_OF_DPA_TUNNEL_ID, |
| 1916 | entry->key.bridge.tunnel_id)) |
| 1917 | return -EMSGSIZE; |
| 1918 | if (rocker_tlv_put_u16(desc_info, ROCKER_TLV_OF_DPA_GOTO_TABLE_ID, |
| 1919 | entry->key.bridge.goto_tbl)) |
| 1920 | return -EMSGSIZE; |
| 1921 | if (rocker_tlv_put_u32(desc_info, ROCKER_TLV_OF_DPA_GROUP_ID, |
| 1922 | entry->key.bridge.group_id)) |
| 1923 | return -EMSGSIZE; |
| 1924 | if (entry->key.bridge.copy_to_cpu && |
| 1925 | rocker_tlv_put_u8(desc_info, ROCKER_TLV_OF_DPA_COPY_CPU_ACTION, |
| 1926 | entry->key.bridge.copy_to_cpu)) |
| 1927 | return -EMSGSIZE; |
| 1928 | |
| 1929 | return 0; |
| 1930 | } |
| 1931 | |
| 1932 | static int rocker_cmd_flow_tbl_add_acl(struct rocker_desc_info *desc_info, |
| 1933 | struct rocker_flow_tbl_entry *entry) |
| 1934 | { |
Scott Feldman | 4a6bb6d | 2015-02-25 20:15:37 -0800 | [diff] [blame] | 1935 | if (rocker_tlv_put_u32(desc_info, ROCKER_TLV_OF_DPA_IN_PPORT, |
| 1936 | entry->key.acl.in_pport)) |
Scott Feldman | 9f6bbf7 | 2014-11-28 14:34:27 +0100 | [diff] [blame] | 1937 | return -EMSGSIZE; |
Scott Feldman | 4a6bb6d | 2015-02-25 20:15:37 -0800 | [diff] [blame] | 1938 | if (rocker_tlv_put_u32(desc_info, ROCKER_TLV_OF_DPA_IN_PPORT_MASK, |
| 1939 | entry->key.acl.in_pport_mask)) |
Scott Feldman | 9f6bbf7 | 2014-11-28 14:34:27 +0100 | [diff] [blame] | 1940 | return -EMSGSIZE; |
| 1941 | if (rocker_tlv_put(desc_info, ROCKER_TLV_OF_DPA_SRC_MAC, |
| 1942 | ETH_ALEN, entry->key.acl.eth_src)) |
| 1943 | return -EMSGSIZE; |
| 1944 | if (rocker_tlv_put(desc_info, ROCKER_TLV_OF_DPA_SRC_MAC_MASK, |
| 1945 | ETH_ALEN, entry->key.acl.eth_src_mask)) |
| 1946 | return -EMSGSIZE; |
| 1947 | if (rocker_tlv_put(desc_info, ROCKER_TLV_OF_DPA_DST_MAC, |
| 1948 | ETH_ALEN, entry->key.acl.eth_dst)) |
| 1949 | return -EMSGSIZE; |
| 1950 | if (rocker_tlv_put(desc_info, ROCKER_TLV_OF_DPA_DST_MAC_MASK, |
| 1951 | ETH_ALEN, entry->key.acl.eth_dst_mask)) |
| 1952 | return -EMSGSIZE; |
Jiri Pirko | 9b03c71 | 2014-12-03 14:14:53 +0100 | [diff] [blame] | 1953 | if (rocker_tlv_put_be16(desc_info, ROCKER_TLV_OF_DPA_ETHERTYPE, |
| 1954 | entry->key.acl.eth_type)) |
Scott Feldman | 9f6bbf7 | 2014-11-28 14:34:27 +0100 | [diff] [blame] | 1955 | return -EMSGSIZE; |
Jiri Pirko | 9b03c71 | 2014-12-03 14:14:53 +0100 | [diff] [blame] | 1956 | if (rocker_tlv_put_be16(desc_info, ROCKER_TLV_OF_DPA_VLAN_ID, |
| 1957 | entry->key.acl.vlan_id)) |
Scott Feldman | 9f6bbf7 | 2014-11-28 14:34:27 +0100 | [diff] [blame] | 1958 | return -EMSGSIZE; |
Jiri Pirko | 9b03c71 | 2014-12-03 14:14:53 +0100 | [diff] [blame] | 1959 | if (rocker_tlv_put_be16(desc_info, ROCKER_TLV_OF_DPA_VLAN_ID_MASK, |
| 1960 | entry->key.acl.vlan_id_mask)) |
Scott Feldman | 9f6bbf7 | 2014-11-28 14:34:27 +0100 | [diff] [blame] | 1961 | return -EMSGSIZE; |
| 1962 | |
| 1963 | switch (ntohs(entry->key.acl.eth_type)) { |
| 1964 | case ETH_P_IP: |
| 1965 | case ETH_P_IPV6: |
| 1966 | if (rocker_tlv_put_u8(desc_info, ROCKER_TLV_OF_DPA_IP_PROTO, |
| 1967 | entry->key.acl.ip_proto)) |
| 1968 | return -EMSGSIZE; |
| 1969 | if (rocker_tlv_put_u8(desc_info, |
| 1970 | ROCKER_TLV_OF_DPA_IP_PROTO_MASK, |
| 1971 | entry->key.acl.ip_proto_mask)) |
| 1972 | return -EMSGSIZE; |
| 1973 | if (rocker_tlv_put_u8(desc_info, ROCKER_TLV_OF_DPA_IP_DSCP, |
| 1974 | entry->key.acl.ip_tos & 0x3f)) |
| 1975 | return -EMSGSIZE; |
| 1976 | if (rocker_tlv_put_u8(desc_info, |
| 1977 | ROCKER_TLV_OF_DPA_IP_DSCP_MASK, |
| 1978 | entry->key.acl.ip_tos_mask & 0x3f)) |
| 1979 | return -EMSGSIZE; |
| 1980 | if (rocker_tlv_put_u8(desc_info, ROCKER_TLV_OF_DPA_IP_ECN, |
| 1981 | (entry->key.acl.ip_tos & 0xc0) >> 6)) |
| 1982 | return -EMSGSIZE; |
| 1983 | if (rocker_tlv_put_u8(desc_info, |
| 1984 | ROCKER_TLV_OF_DPA_IP_ECN_MASK, |
| 1985 | (entry->key.acl.ip_tos_mask & 0xc0) >> 6)) |
| 1986 | return -EMSGSIZE; |
| 1987 | break; |
| 1988 | } |
| 1989 | |
| 1990 | if (entry->key.acl.group_id != ROCKER_GROUP_NONE && |
| 1991 | rocker_tlv_put_u32(desc_info, ROCKER_TLV_OF_DPA_GROUP_ID, |
| 1992 | entry->key.acl.group_id)) |
| 1993 | return -EMSGSIZE; |
| 1994 | |
| 1995 | return 0; |
| 1996 | } |
| 1997 | |
| 1998 | static int rocker_cmd_flow_tbl_add(struct rocker *rocker, |
| 1999 | struct rocker_port *rocker_port, |
| 2000 | struct rocker_desc_info *desc_info, |
| 2001 | void *priv) |
| 2002 | { |
| 2003 | struct rocker_flow_tbl_entry *entry = priv; |
| 2004 | struct rocker_tlv *cmd_info; |
| 2005 | int err = 0; |
| 2006 | |
Scott Feldman | c1beeef | 2015-03-05 21:21:20 -0800 | [diff] [blame] | 2007 | 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] | 2008 | return -EMSGSIZE; |
| 2009 | cmd_info = rocker_tlv_nest_start(desc_info, ROCKER_TLV_CMD_INFO); |
| 2010 | if (!cmd_info) |
| 2011 | return -EMSGSIZE; |
| 2012 | if (rocker_tlv_put_u16(desc_info, ROCKER_TLV_OF_DPA_TABLE_ID, |
| 2013 | entry->key.tbl_id)) |
| 2014 | return -EMSGSIZE; |
| 2015 | if (rocker_tlv_put_u32(desc_info, ROCKER_TLV_OF_DPA_PRIORITY, |
| 2016 | entry->key.priority)) |
| 2017 | return -EMSGSIZE; |
| 2018 | if (rocker_tlv_put_u32(desc_info, ROCKER_TLV_OF_DPA_HARDTIME, 0)) |
| 2019 | return -EMSGSIZE; |
| 2020 | if (rocker_tlv_put_u64(desc_info, ROCKER_TLV_OF_DPA_COOKIE, |
| 2021 | entry->cookie)) |
| 2022 | return -EMSGSIZE; |
| 2023 | |
| 2024 | switch (entry->key.tbl_id) { |
| 2025 | case ROCKER_OF_DPA_TABLE_ID_INGRESS_PORT: |
| 2026 | err = rocker_cmd_flow_tbl_add_ig_port(desc_info, entry); |
| 2027 | break; |
| 2028 | case ROCKER_OF_DPA_TABLE_ID_VLAN: |
| 2029 | err = rocker_cmd_flow_tbl_add_vlan(desc_info, entry); |
| 2030 | break; |
| 2031 | case ROCKER_OF_DPA_TABLE_ID_TERMINATION_MAC: |
| 2032 | err = rocker_cmd_flow_tbl_add_term_mac(desc_info, entry); |
| 2033 | break; |
| 2034 | case ROCKER_OF_DPA_TABLE_ID_UNICAST_ROUTING: |
| 2035 | err = rocker_cmd_flow_tbl_add_ucast_routing(desc_info, entry); |
| 2036 | break; |
| 2037 | case ROCKER_OF_DPA_TABLE_ID_BRIDGING: |
| 2038 | err = rocker_cmd_flow_tbl_add_bridge(desc_info, entry); |
| 2039 | break; |
| 2040 | case ROCKER_OF_DPA_TABLE_ID_ACL_POLICY: |
| 2041 | err = rocker_cmd_flow_tbl_add_acl(desc_info, entry); |
| 2042 | break; |
| 2043 | default: |
| 2044 | err = -ENOTSUPP; |
| 2045 | break; |
| 2046 | } |
| 2047 | |
| 2048 | if (err) |
| 2049 | return err; |
| 2050 | |
| 2051 | rocker_tlv_nest_end(desc_info, cmd_info); |
| 2052 | |
| 2053 | return 0; |
| 2054 | } |
| 2055 | |
| 2056 | static int rocker_cmd_flow_tbl_del(struct rocker *rocker, |
| 2057 | struct rocker_port *rocker_port, |
| 2058 | struct rocker_desc_info *desc_info, |
| 2059 | void *priv) |
| 2060 | { |
| 2061 | const struct rocker_flow_tbl_entry *entry = priv; |
| 2062 | struct rocker_tlv *cmd_info; |
| 2063 | |
Scott Feldman | c1beeef | 2015-03-05 21:21:20 -0800 | [diff] [blame] | 2064 | 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] | 2065 | return -EMSGSIZE; |
| 2066 | cmd_info = rocker_tlv_nest_start(desc_info, ROCKER_TLV_CMD_INFO); |
| 2067 | if (!cmd_info) |
| 2068 | return -EMSGSIZE; |
| 2069 | if (rocker_tlv_put_u64(desc_info, ROCKER_TLV_OF_DPA_COOKIE, |
| 2070 | entry->cookie)) |
| 2071 | return -EMSGSIZE; |
| 2072 | rocker_tlv_nest_end(desc_info, cmd_info); |
| 2073 | |
| 2074 | return 0; |
| 2075 | } |
| 2076 | |
| 2077 | static int |
| 2078 | rocker_cmd_group_tbl_add_l2_interface(struct rocker_desc_info *desc_info, |
| 2079 | struct rocker_group_tbl_entry *entry) |
| 2080 | { |
Scott Feldman | 4a6bb6d | 2015-02-25 20:15:37 -0800 | [diff] [blame] | 2081 | 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] | 2082 | ROCKER_GROUP_PORT_GET(entry->group_id))) |
| 2083 | return -EMSGSIZE; |
| 2084 | if (rocker_tlv_put_u8(desc_info, ROCKER_TLV_OF_DPA_POP_VLAN, |
| 2085 | entry->l2_interface.pop_vlan)) |
| 2086 | return -EMSGSIZE; |
| 2087 | |
| 2088 | return 0; |
| 2089 | } |
| 2090 | |
| 2091 | static int |
| 2092 | rocker_cmd_group_tbl_add_l2_rewrite(struct rocker_desc_info *desc_info, |
| 2093 | struct rocker_group_tbl_entry *entry) |
| 2094 | { |
| 2095 | if (rocker_tlv_put_u32(desc_info, ROCKER_TLV_OF_DPA_GROUP_ID_LOWER, |
| 2096 | entry->l2_rewrite.group_id)) |
| 2097 | return -EMSGSIZE; |
| 2098 | if (!is_zero_ether_addr(entry->l2_rewrite.eth_src) && |
| 2099 | rocker_tlv_put(desc_info, ROCKER_TLV_OF_DPA_SRC_MAC, |
| 2100 | ETH_ALEN, entry->l2_rewrite.eth_src)) |
| 2101 | return -EMSGSIZE; |
| 2102 | if (!is_zero_ether_addr(entry->l2_rewrite.eth_dst) && |
| 2103 | rocker_tlv_put(desc_info, ROCKER_TLV_OF_DPA_DST_MAC, |
| 2104 | ETH_ALEN, entry->l2_rewrite.eth_dst)) |
| 2105 | return -EMSGSIZE; |
| 2106 | if (entry->l2_rewrite.vlan_id && |
Jiri Pirko | 9b03c71 | 2014-12-03 14:14:53 +0100 | [diff] [blame] | 2107 | rocker_tlv_put_be16(desc_info, ROCKER_TLV_OF_DPA_VLAN_ID, |
| 2108 | entry->l2_rewrite.vlan_id)) |
Scott Feldman | 9f6bbf7 | 2014-11-28 14:34:27 +0100 | [diff] [blame] | 2109 | return -EMSGSIZE; |
| 2110 | |
| 2111 | return 0; |
| 2112 | } |
| 2113 | |
| 2114 | static int |
| 2115 | rocker_cmd_group_tbl_add_group_ids(struct rocker_desc_info *desc_info, |
| 2116 | struct rocker_group_tbl_entry *entry) |
| 2117 | { |
| 2118 | int i; |
| 2119 | struct rocker_tlv *group_ids; |
| 2120 | |
| 2121 | if (rocker_tlv_put_u16(desc_info, ROCKER_TLV_OF_DPA_GROUP_COUNT, |
| 2122 | entry->group_count)) |
| 2123 | return -EMSGSIZE; |
| 2124 | |
| 2125 | group_ids = rocker_tlv_nest_start(desc_info, |
| 2126 | ROCKER_TLV_OF_DPA_GROUP_IDS); |
| 2127 | if (!group_ids) |
| 2128 | return -EMSGSIZE; |
| 2129 | |
| 2130 | for (i = 0; i < entry->group_count; i++) |
| 2131 | /* Note TLV array is 1-based */ |
| 2132 | if (rocker_tlv_put_u32(desc_info, i + 1, entry->group_ids[i])) |
| 2133 | return -EMSGSIZE; |
| 2134 | |
| 2135 | rocker_tlv_nest_end(desc_info, group_ids); |
| 2136 | |
| 2137 | return 0; |
| 2138 | } |
| 2139 | |
| 2140 | static int |
| 2141 | rocker_cmd_group_tbl_add_l3_unicast(struct rocker_desc_info *desc_info, |
| 2142 | struct rocker_group_tbl_entry *entry) |
| 2143 | { |
| 2144 | if (!is_zero_ether_addr(entry->l3_unicast.eth_src) && |
| 2145 | rocker_tlv_put(desc_info, ROCKER_TLV_OF_DPA_SRC_MAC, |
| 2146 | ETH_ALEN, entry->l3_unicast.eth_src)) |
| 2147 | return -EMSGSIZE; |
| 2148 | if (!is_zero_ether_addr(entry->l3_unicast.eth_dst) && |
| 2149 | rocker_tlv_put(desc_info, ROCKER_TLV_OF_DPA_DST_MAC, |
| 2150 | ETH_ALEN, entry->l3_unicast.eth_dst)) |
| 2151 | return -EMSGSIZE; |
| 2152 | if (entry->l3_unicast.vlan_id && |
Jiri Pirko | 9b03c71 | 2014-12-03 14:14:53 +0100 | [diff] [blame] | 2153 | rocker_tlv_put_be16(desc_info, ROCKER_TLV_OF_DPA_VLAN_ID, |
| 2154 | entry->l3_unicast.vlan_id)) |
Scott Feldman | 9f6bbf7 | 2014-11-28 14:34:27 +0100 | [diff] [blame] | 2155 | return -EMSGSIZE; |
| 2156 | if (rocker_tlv_put_u8(desc_info, ROCKER_TLV_OF_DPA_TTL_CHECK, |
| 2157 | entry->l3_unicast.ttl_check)) |
| 2158 | return -EMSGSIZE; |
| 2159 | if (rocker_tlv_put_u32(desc_info, ROCKER_TLV_OF_DPA_GROUP_ID_LOWER, |
| 2160 | entry->l3_unicast.group_id)) |
| 2161 | return -EMSGSIZE; |
| 2162 | |
| 2163 | return 0; |
| 2164 | } |
| 2165 | |
| 2166 | static int rocker_cmd_group_tbl_add(struct rocker *rocker, |
| 2167 | struct rocker_port *rocker_port, |
| 2168 | struct rocker_desc_info *desc_info, |
| 2169 | void *priv) |
| 2170 | { |
| 2171 | struct rocker_group_tbl_entry *entry = priv; |
| 2172 | struct rocker_tlv *cmd_info; |
| 2173 | int err = 0; |
| 2174 | |
| 2175 | if (rocker_tlv_put_u16(desc_info, ROCKER_TLV_CMD_TYPE, entry->cmd)) |
| 2176 | return -EMSGSIZE; |
| 2177 | cmd_info = rocker_tlv_nest_start(desc_info, ROCKER_TLV_CMD_INFO); |
| 2178 | if (!cmd_info) |
| 2179 | return -EMSGSIZE; |
| 2180 | |
| 2181 | if (rocker_tlv_put_u32(desc_info, ROCKER_TLV_OF_DPA_GROUP_ID, |
| 2182 | entry->group_id)) |
| 2183 | return -EMSGSIZE; |
| 2184 | |
| 2185 | switch (ROCKER_GROUP_TYPE_GET(entry->group_id)) { |
| 2186 | case ROCKER_OF_DPA_GROUP_TYPE_L2_INTERFACE: |
| 2187 | err = rocker_cmd_group_tbl_add_l2_interface(desc_info, entry); |
| 2188 | break; |
| 2189 | case ROCKER_OF_DPA_GROUP_TYPE_L2_REWRITE: |
| 2190 | err = rocker_cmd_group_tbl_add_l2_rewrite(desc_info, entry); |
| 2191 | break; |
| 2192 | case ROCKER_OF_DPA_GROUP_TYPE_L2_FLOOD: |
| 2193 | case ROCKER_OF_DPA_GROUP_TYPE_L2_MCAST: |
| 2194 | err = rocker_cmd_group_tbl_add_group_ids(desc_info, entry); |
| 2195 | break; |
| 2196 | case ROCKER_OF_DPA_GROUP_TYPE_L3_UCAST: |
| 2197 | err = rocker_cmd_group_tbl_add_l3_unicast(desc_info, entry); |
| 2198 | break; |
| 2199 | default: |
| 2200 | err = -ENOTSUPP; |
| 2201 | break; |
| 2202 | } |
| 2203 | |
| 2204 | if (err) |
| 2205 | return err; |
| 2206 | |
| 2207 | rocker_tlv_nest_end(desc_info, cmd_info); |
| 2208 | |
| 2209 | return 0; |
| 2210 | } |
| 2211 | |
| 2212 | static int rocker_cmd_group_tbl_del(struct rocker *rocker, |
| 2213 | struct rocker_port *rocker_port, |
| 2214 | struct rocker_desc_info *desc_info, |
| 2215 | void *priv) |
| 2216 | { |
| 2217 | const struct rocker_group_tbl_entry *entry = priv; |
| 2218 | struct rocker_tlv *cmd_info; |
| 2219 | |
| 2220 | if (rocker_tlv_put_u16(desc_info, ROCKER_TLV_CMD_TYPE, entry->cmd)) |
| 2221 | return -EMSGSIZE; |
| 2222 | cmd_info = rocker_tlv_nest_start(desc_info, ROCKER_TLV_CMD_INFO); |
| 2223 | if (!cmd_info) |
| 2224 | return -EMSGSIZE; |
| 2225 | if (rocker_tlv_put_u32(desc_info, ROCKER_TLV_OF_DPA_GROUP_ID, |
| 2226 | entry->group_id)) |
| 2227 | return -EMSGSIZE; |
| 2228 | rocker_tlv_nest_end(desc_info, cmd_info); |
| 2229 | |
| 2230 | return 0; |
| 2231 | } |
| 2232 | |
Scott Feldman | c1beeef | 2015-03-05 21:21:20 -0800 | [diff] [blame] | 2233 | /*************************************************** |
| 2234 | * Flow, group, FDB, internal VLAN and neigh tables |
| 2235 | ***************************************************/ |
Scott Feldman | 9f6bbf7 | 2014-11-28 14:34:27 +0100 | [diff] [blame] | 2236 | |
| 2237 | static int rocker_init_tbls(struct rocker *rocker) |
| 2238 | { |
| 2239 | hash_init(rocker->flow_tbl); |
| 2240 | spin_lock_init(&rocker->flow_tbl_lock); |
| 2241 | |
| 2242 | hash_init(rocker->group_tbl); |
| 2243 | spin_lock_init(&rocker->group_tbl_lock); |
| 2244 | |
| 2245 | hash_init(rocker->fdb_tbl); |
| 2246 | spin_lock_init(&rocker->fdb_tbl_lock); |
| 2247 | |
| 2248 | hash_init(rocker->internal_vlan_tbl); |
| 2249 | spin_lock_init(&rocker->internal_vlan_tbl_lock); |
| 2250 | |
Scott Feldman | c1beeef | 2015-03-05 21:21:20 -0800 | [diff] [blame] | 2251 | hash_init(rocker->neigh_tbl); |
| 2252 | spin_lock_init(&rocker->neigh_tbl_lock); |
| 2253 | |
Scott Feldman | 9f6bbf7 | 2014-11-28 14:34:27 +0100 | [diff] [blame] | 2254 | return 0; |
| 2255 | } |
| 2256 | |
| 2257 | static void rocker_free_tbls(struct rocker *rocker) |
| 2258 | { |
| 2259 | unsigned long flags; |
| 2260 | struct rocker_flow_tbl_entry *flow_entry; |
| 2261 | struct rocker_group_tbl_entry *group_entry; |
| 2262 | struct rocker_fdb_tbl_entry *fdb_entry; |
| 2263 | struct rocker_internal_vlan_tbl_entry *internal_vlan_entry; |
Scott Feldman | c1beeef | 2015-03-05 21:21:20 -0800 | [diff] [blame] | 2264 | struct rocker_neigh_tbl_entry *neigh_entry; |
Scott Feldman | 9f6bbf7 | 2014-11-28 14:34:27 +0100 | [diff] [blame] | 2265 | struct hlist_node *tmp; |
| 2266 | int bkt; |
| 2267 | |
| 2268 | spin_lock_irqsave(&rocker->flow_tbl_lock, flags); |
| 2269 | hash_for_each_safe(rocker->flow_tbl, bkt, tmp, flow_entry, entry) |
| 2270 | hash_del(&flow_entry->entry); |
| 2271 | spin_unlock_irqrestore(&rocker->flow_tbl_lock, flags); |
| 2272 | |
| 2273 | spin_lock_irqsave(&rocker->group_tbl_lock, flags); |
| 2274 | hash_for_each_safe(rocker->group_tbl, bkt, tmp, group_entry, entry) |
| 2275 | hash_del(&group_entry->entry); |
| 2276 | spin_unlock_irqrestore(&rocker->group_tbl_lock, flags); |
| 2277 | |
| 2278 | spin_lock_irqsave(&rocker->fdb_tbl_lock, flags); |
| 2279 | hash_for_each_safe(rocker->fdb_tbl, bkt, tmp, fdb_entry, entry) |
| 2280 | hash_del(&fdb_entry->entry); |
| 2281 | spin_unlock_irqrestore(&rocker->fdb_tbl_lock, flags); |
| 2282 | |
| 2283 | spin_lock_irqsave(&rocker->internal_vlan_tbl_lock, flags); |
| 2284 | hash_for_each_safe(rocker->internal_vlan_tbl, bkt, |
| 2285 | tmp, internal_vlan_entry, entry) |
| 2286 | hash_del(&internal_vlan_entry->entry); |
| 2287 | spin_unlock_irqrestore(&rocker->internal_vlan_tbl_lock, flags); |
Scott Feldman | c1beeef | 2015-03-05 21:21:20 -0800 | [diff] [blame] | 2288 | |
| 2289 | spin_lock_irqsave(&rocker->neigh_tbl_lock, flags); |
| 2290 | hash_for_each_safe(rocker->neigh_tbl, bkt, tmp, neigh_entry, entry) |
| 2291 | hash_del(&neigh_entry->entry); |
| 2292 | spin_unlock_irqrestore(&rocker->neigh_tbl_lock, flags); |
Scott Feldman | 9f6bbf7 | 2014-11-28 14:34:27 +0100 | [diff] [blame] | 2293 | } |
| 2294 | |
| 2295 | static struct rocker_flow_tbl_entry * |
| 2296 | rocker_flow_tbl_find(struct rocker *rocker, struct rocker_flow_tbl_entry *match) |
| 2297 | { |
| 2298 | struct rocker_flow_tbl_entry *found; |
Scott Feldman | c1beeef | 2015-03-05 21:21:20 -0800 | [diff] [blame] | 2299 | 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] | 2300 | |
| 2301 | hash_for_each_possible(rocker->flow_tbl, found, |
| 2302 | entry, match->key_crc32) { |
Scott Feldman | c1beeef | 2015-03-05 21:21:20 -0800 | [diff] [blame] | 2303 | if (memcmp(&found->key, &match->key, key_len) == 0) |
Scott Feldman | 9f6bbf7 | 2014-11-28 14:34:27 +0100 | [diff] [blame] | 2304 | return found; |
| 2305 | } |
| 2306 | |
| 2307 | return NULL; |
| 2308 | } |
| 2309 | |
| 2310 | static int rocker_flow_tbl_add(struct rocker_port *rocker_port, |
| 2311 | struct rocker_flow_tbl_entry *match, |
| 2312 | bool nowait) |
| 2313 | { |
| 2314 | struct rocker *rocker = rocker_port->rocker; |
| 2315 | struct rocker_flow_tbl_entry *found; |
Scott Feldman | c1beeef | 2015-03-05 21:21:20 -0800 | [diff] [blame] | 2316 | 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] | 2317 | unsigned long flags; |
Scott Feldman | 9f6bbf7 | 2014-11-28 14:34:27 +0100 | [diff] [blame] | 2318 | |
Scott Feldman | c1beeef | 2015-03-05 21:21:20 -0800 | [diff] [blame] | 2319 | match->key_crc32 = crc32(~0, &match->key, key_len); |
Scott Feldman | 9f6bbf7 | 2014-11-28 14:34:27 +0100 | [diff] [blame] | 2320 | |
| 2321 | spin_lock_irqsave(&rocker->flow_tbl_lock, flags); |
| 2322 | |
| 2323 | found = rocker_flow_tbl_find(rocker, match); |
| 2324 | |
| 2325 | if (found) { |
Scott Feldman | c1beeef | 2015-03-05 21:21:20 -0800 | [diff] [blame] | 2326 | match->cookie = found->cookie; |
| 2327 | hash_del(&found->entry); |
| 2328 | kfree(found); |
| 2329 | found = match; |
| 2330 | found->cmd = ROCKER_TLV_CMD_TYPE_OF_DPA_FLOW_MOD; |
Scott Feldman | 9f6bbf7 | 2014-11-28 14:34:27 +0100 | [diff] [blame] | 2331 | } else { |
| 2332 | found = match; |
| 2333 | found->cookie = rocker->flow_tbl_next_cookie++; |
Scott Feldman | c1beeef | 2015-03-05 21:21:20 -0800 | [diff] [blame] | 2334 | found->cmd = ROCKER_TLV_CMD_TYPE_OF_DPA_FLOW_ADD; |
Scott Feldman | 9f6bbf7 | 2014-11-28 14:34:27 +0100 | [diff] [blame] | 2335 | } |
| 2336 | |
Scott Feldman | c1beeef | 2015-03-05 21:21:20 -0800 | [diff] [blame] | 2337 | hash_add(rocker->flow_tbl, &found->entry, found->key_crc32); |
Scott Feldman | 9f6bbf7 | 2014-11-28 14:34:27 +0100 | [diff] [blame] | 2338 | |
| 2339 | spin_unlock_irqrestore(&rocker->flow_tbl_lock, flags); |
| 2340 | |
Scott Feldman | c1beeef | 2015-03-05 21:21:20 -0800 | [diff] [blame] | 2341 | return rocker_cmd_exec(rocker, rocker_port, |
| 2342 | rocker_cmd_flow_tbl_add, |
| 2343 | found, NULL, NULL, nowait); |
Scott Feldman | 9f6bbf7 | 2014-11-28 14:34:27 +0100 | [diff] [blame] | 2344 | } |
| 2345 | |
| 2346 | static int rocker_flow_tbl_del(struct rocker_port *rocker_port, |
| 2347 | struct rocker_flow_tbl_entry *match, |
| 2348 | bool nowait) |
| 2349 | { |
| 2350 | struct rocker *rocker = rocker_port->rocker; |
| 2351 | struct rocker_flow_tbl_entry *found; |
Scott Feldman | c1beeef | 2015-03-05 21:21:20 -0800 | [diff] [blame] | 2352 | 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] | 2353 | unsigned long flags; |
Scott Feldman | 9f6bbf7 | 2014-11-28 14:34:27 +0100 | [diff] [blame] | 2354 | int err = 0; |
| 2355 | |
Scott Feldman | c1beeef | 2015-03-05 21:21:20 -0800 | [diff] [blame] | 2356 | match->key_crc32 = crc32(~0, &match->key, key_len); |
Scott Feldman | 9f6bbf7 | 2014-11-28 14:34:27 +0100 | [diff] [blame] | 2357 | |
| 2358 | spin_lock_irqsave(&rocker->flow_tbl_lock, flags); |
| 2359 | |
| 2360 | found = rocker_flow_tbl_find(rocker, match); |
| 2361 | |
| 2362 | if (found) { |
Scott Feldman | c1beeef | 2015-03-05 21:21:20 -0800 | [diff] [blame] | 2363 | hash_del(&found->entry); |
| 2364 | found->cmd = ROCKER_TLV_CMD_TYPE_OF_DPA_FLOW_DEL; |
Scott Feldman | 9f6bbf7 | 2014-11-28 14:34:27 +0100 | [diff] [blame] | 2365 | } |
| 2366 | |
| 2367 | spin_unlock_irqrestore(&rocker->flow_tbl_lock, flags); |
| 2368 | |
| 2369 | kfree(match); |
| 2370 | |
Scott Feldman | c1beeef | 2015-03-05 21:21:20 -0800 | [diff] [blame] | 2371 | if (found) { |
Scott Feldman | 9f6bbf7 | 2014-11-28 14:34:27 +0100 | [diff] [blame] | 2372 | err = rocker_cmd_exec(rocker, rocker_port, |
| 2373 | rocker_cmd_flow_tbl_del, |
| 2374 | found, NULL, NULL, nowait); |
| 2375 | kfree(found); |
| 2376 | } |
| 2377 | |
| 2378 | return err; |
| 2379 | } |
| 2380 | |
| 2381 | static gfp_t rocker_op_flags_gfp(int flags) |
| 2382 | { |
| 2383 | return flags & ROCKER_OP_FLAG_NOWAIT ? GFP_ATOMIC : GFP_KERNEL; |
| 2384 | } |
| 2385 | |
| 2386 | static int rocker_flow_tbl_do(struct rocker_port *rocker_port, |
| 2387 | int flags, struct rocker_flow_tbl_entry *entry) |
| 2388 | { |
| 2389 | bool nowait = flags & ROCKER_OP_FLAG_NOWAIT; |
| 2390 | |
| 2391 | if (flags & ROCKER_OP_FLAG_REMOVE) |
| 2392 | return rocker_flow_tbl_del(rocker_port, entry, nowait); |
| 2393 | else |
| 2394 | return rocker_flow_tbl_add(rocker_port, entry, nowait); |
| 2395 | } |
| 2396 | |
| 2397 | static int rocker_flow_tbl_ig_port(struct rocker_port *rocker_port, |
Scott Feldman | 4a6bb6d | 2015-02-25 20:15:37 -0800 | [diff] [blame] | 2398 | int flags, u32 in_pport, u32 in_pport_mask, |
Scott Feldman | 9f6bbf7 | 2014-11-28 14:34:27 +0100 | [diff] [blame] | 2399 | enum rocker_of_dpa_table_id goto_tbl) |
| 2400 | { |
| 2401 | struct rocker_flow_tbl_entry *entry; |
| 2402 | |
| 2403 | entry = kzalloc(sizeof(*entry), rocker_op_flags_gfp(flags)); |
| 2404 | if (!entry) |
| 2405 | return -ENOMEM; |
| 2406 | |
| 2407 | entry->key.priority = ROCKER_PRIORITY_IG_PORT; |
| 2408 | entry->key.tbl_id = ROCKER_OF_DPA_TABLE_ID_INGRESS_PORT; |
Scott Feldman | 4a6bb6d | 2015-02-25 20:15:37 -0800 | [diff] [blame] | 2409 | entry->key.ig_port.in_pport = in_pport; |
| 2410 | entry->key.ig_port.in_pport_mask = in_pport_mask; |
Scott Feldman | 9f6bbf7 | 2014-11-28 14:34:27 +0100 | [diff] [blame] | 2411 | entry->key.ig_port.goto_tbl = goto_tbl; |
| 2412 | |
| 2413 | return rocker_flow_tbl_do(rocker_port, flags, entry); |
| 2414 | } |
| 2415 | |
| 2416 | static int rocker_flow_tbl_vlan(struct rocker_port *rocker_port, |
Scott Feldman | 4a6bb6d | 2015-02-25 20:15:37 -0800 | [diff] [blame] | 2417 | int flags, u32 in_pport, |
Scott Feldman | 9f6bbf7 | 2014-11-28 14:34:27 +0100 | [diff] [blame] | 2418 | __be16 vlan_id, __be16 vlan_id_mask, |
| 2419 | enum rocker_of_dpa_table_id goto_tbl, |
| 2420 | bool untagged, __be16 new_vlan_id) |
| 2421 | { |
| 2422 | struct rocker_flow_tbl_entry *entry; |
| 2423 | |
| 2424 | entry = kzalloc(sizeof(*entry), rocker_op_flags_gfp(flags)); |
| 2425 | if (!entry) |
| 2426 | return -ENOMEM; |
| 2427 | |
| 2428 | entry->key.priority = ROCKER_PRIORITY_VLAN; |
| 2429 | entry->key.tbl_id = ROCKER_OF_DPA_TABLE_ID_VLAN; |
Scott Feldman | 4a6bb6d | 2015-02-25 20:15:37 -0800 | [diff] [blame] | 2430 | entry->key.vlan.in_pport = in_pport; |
Scott Feldman | 9f6bbf7 | 2014-11-28 14:34:27 +0100 | [diff] [blame] | 2431 | entry->key.vlan.vlan_id = vlan_id; |
| 2432 | entry->key.vlan.vlan_id_mask = vlan_id_mask; |
| 2433 | entry->key.vlan.goto_tbl = goto_tbl; |
| 2434 | |
| 2435 | entry->key.vlan.untagged = untagged; |
| 2436 | entry->key.vlan.new_vlan_id = new_vlan_id; |
| 2437 | |
| 2438 | return rocker_flow_tbl_do(rocker_port, flags, entry); |
| 2439 | } |
| 2440 | |
| 2441 | static int rocker_flow_tbl_term_mac(struct rocker_port *rocker_port, |
Scott Feldman | 4a6bb6d | 2015-02-25 20:15:37 -0800 | [diff] [blame] | 2442 | u32 in_pport, u32 in_pport_mask, |
Scott Feldman | 9f6bbf7 | 2014-11-28 14:34:27 +0100 | [diff] [blame] | 2443 | __be16 eth_type, const u8 *eth_dst, |
| 2444 | const u8 *eth_dst_mask, __be16 vlan_id, |
| 2445 | __be16 vlan_id_mask, bool copy_to_cpu, |
| 2446 | int flags) |
| 2447 | { |
| 2448 | struct rocker_flow_tbl_entry *entry; |
| 2449 | |
| 2450 | entry = kzalloc(sizeof(*entry), rocker_op_flags_gfp(flags)); |
| 2451 | if (!entry) |
| 2452 | return -ENOMEM; |
| 2453 | |
| 2454 | if (is_multicast_ether_addr(eth_dst)) { |
| 2455 | entry->key.priority = ROCKER_PRIORITY_TERM_MAC_MCAST; |
| 2456 | entry->key.term_mac.goto_tbl = |
| 2457 | ROCKER_OF_DPA_TABLE_ID_MULTICAST_ROUTING; |
| 2458 | } else { |
| 2459 | entry->key.priority = ROCKER_PRIORITY_TERM_MAC_UCAST; |
| 2460 | entry->key.term_mac.goto_tbl = |
| 2461 | ROCKER_OF_DPA_TABLE_ID_UNICAST_ROUTING; |
| 2462 | } |
| 2463 | |
| 2464 | entry->key.tbl_id = ROCKER_OF_DPA_TABLE_ID_TERMINATION_MAC; |
Scott Feldman | 4a6bb6d | 2015-02-25 20:15:37 -0800 | [diff] [blame] | 2465 | entry->key.term_mac.in_pport = in_pport; |
| 2466 | entry->key.term_mac.in_pport_mask = in_pport_mask; |
Scott Feldman | 9f6bbf7 | 2014-11-28 14:34:27 +0100 | [diff] [blame] | 2467 | entry->key.term_mac.eth_type = eth_type; |
| 2468 | ether_addr_copy(entry->key.term_mac.eth_dst, eth_dst); |
| 2469 | ether_addr_copy(entry->key.term_mac.eth_dst_mask, eth_dst_mask); |
| 2470 | entry->key.term_mac.vlan_id = vlan_id; |
| 2471 | entry->key.term_mac.vlan_id_mask = vlan_id_mask; |
| 2472 | entry->key.term_mac.copy_to_cpu = copy_to_cpu; |
| 2473 | |
| 2474 | return rocker_flow_tbl_do(rocker_port, flags, entry); |
| 2475 | } |
| 2476 | |
| 2477 | static int rocker_flow_tbl_bridge(struct rocker_port *rocker_port, |
| 2478 | int flags, |
| 2479 | const u8 *eth_dst, const u8 *eth_dst_mask, |
| 2480 | __be16 vlan_id, u32 tunnel_id, |
| 2481 | enum rocker_of_dpa_table_id goto_tbl, |
| 2482 | u32 group_id, bool copy_to_cpu) |
| 2483 | { |
| 2484 | struct rocker_flow_tbl_entry *entry; |
| 2485 | u32 priority; |
| 2486 | bool vlan_bridging = !!vlan_id; |
| 2487 | bool dflt = !eth_dst || (eth_dst && eth_dst_mask); |
| 2488 | bool wild = false; |
| 2489 | |
| 2490 | entry = kzalloc(sizeof(*entry), rocker_op_flags_gfp(flags)); |
| 2491 | if (!entry) |
| 2492 | return -ENOMEM; |
| 2493 | |
| 2494 | entry->key.tbl_id = ROCKER_OF_DPA_TABLE_ID_BRIDGING; |
| 2495 | |
| 2496 | if (eth_dst) { |
| 2497 | entry->key.bridge.has_eth_dst = 1; |
| 2498 | ether_addr_copy(entry->key.bridge.eth_dst, eth_dst); |
| 2499 | } |
| 2500 | if (eth_dst_mask) { |
| 2501 | entry->key.bridge.has_eth_dst_mask = 1; |
| 2502 | ether_addr_copy(entry->key.bridge.eth_dst_mask, eth_dst_mask); |
| 2503 | if (memcmp(eth_dst_mask, ff_mac, ETH_ALEN)) |
| 2504 | wild = true; |
| 2505 | } |
| 2506 | |
| 2507 | priority = ROCKER_PRIORITY_UNKNOWN; |
Thomas Graf | 51ace88 | 2014-11-28 14:34:32 +0100 | [diff] [blame] | 2508 | if (vlan_bridging && dflt && wild) |
Scott Feldman | 9f6bbf7 | 2014-11-28 14:34:27 +0100 | [diff] [blame] | 2509 | priority = ROCKER_PRIORITY_BRIDGING_VLAN_DFLT_WILD; |
Thomas Graf | 51ace88 | 2014-11-28 14:34:32 +0100 | [diff] [blame] | 2510 | else if (vlan_bridging && dflt && !wild) |
Scott Feldman | 9f6bbf7 | 2014-11-28 14:34:27 +0100 | [diff] [blame] | 2511 | priority = ROCKER_PRIORITY_BRIDGING_VLAN_DFLT_EXACT; |
Thomas Graf | 51ace88 | 2014-11-28 14:34:32 +0100 | [diff] [blame] | 2512 | else if (vlan_bridging && !dflt) |
Scott Feldman | 9f6bbf7 | 2014-11-28 14:34:27 +0100 | [diff] [blame] | 2513 | priority = ROCKER_PRIORITY_BRIDGING_VLAN; |
Thomas Graf | 51ace88 | 2014-11-28 14:34:32 +0100 | [diff] [blame] | 2514 | else if (!vlan_bridging && dflt && wild) |
Scott Feldman | 9f6bbf7 | 2014-11-28 14:34:27 +0100 | [diff] [blame] | 2515 | priority = ROCKER_PRIORITY_BRIDGING_TENANT_DFLT_WILD; |
Thomas Graf | 51ace88 | 2014-11-28 14:34:32 +0100 | [diff] [blame] | 2516 | else if (!vlan_bridging && dflt && !wild) |
Scott Feldman | 9f6bbf7 | 2014-11-28 14:34:27 +0100 | [diff] [blame] | 2517 | priority = ROCKER_PRIORITY_BRIDGING_TENANT_DFLT_EXACT; |
Thomas Graf | 51ace88 | 2014-11-28 14:34:32 +0100 | [diff] [blame] | 2518 | else if (!vlan_bridging && !dflt) |
Scott Feldman | 9f6bbf7 | 2014-11-28 14:34:27 +0100 | [diff] [blame] | 2519 | priority = ROCKER_PRIORITY_BRIDGING_TENANT; |
| 2520 | |
| 2521 | entry->key.priority = priority; |
| 2522 | entry->key.bridge.vlan_id = vlan_id; |
| 2523 | entry->key.bridge.tunnel_id = tunnel_id; |
| 2524 | entry->key.bridge.goto_tbl = goto_tbl; |
| 2525 | entry->key.bridge.group_id = group_id; |
| 2526 | entry->key.bridge.copy_to_cpu = copy_to_cpu; |
| 2527 | |
| 2528 | return rocker_flow_tbl_do(rocker_port, flags, entry); |
| 2529 | } |
| 2530 | |
Scott Feldman | c1beeef | 2015-03-05 21:21:20 -0800 | [diff] [blame] | 2531 | static int rocker_flow_tbl_ucast4_routing(struct rocker_port *rocker_port, |
| 2532 | __be16 eth_type, __be32 dst, |
| 2533 | __be32 dst_mask, u32 priority, |
| 2534 | enum rocker_of_dpa_table_id goto_tbl, |
| 2535 | u32 group_id, int flags) |
| 2536 | { |
| 2537 | struct rocker_flow_tbl_entry *entry; |
| 2538 | |
| 2539 | entry = kzalloc(sizeof(*entry), rocker_op_flags_gfp(flags)); |
| 2540 | if (!entry) |
| 2541 | return -ENOMEM; |
| 2542 | |
| 2543 | entry->key.tbl_id = ROCKER_OF_DPA_TABLE_ID_UNICAST_ROUTING; |
| 2544 | entry->key.priority = priority; |
| 2545 | entry->key.ucast_routing.eth_type = eth_type; |
| 2546 | entry->key.ucast_routing.dst4 = dst; |
| 2547 | entry->key.ucast_routing.dst4_mask = dst_mask; |
| 2548 | entry->key.ucast_routing.goto_tbl = goto_tbl; |
| 2549 | entry->key.ucast_routing.group_id = group_id; |
| 2550 | entry->key_len = offsetof(struct rocker_flow_tbl_key, |
| 2551 | ucast_routing.group_id); |
| 2552 | |
| 2553 | return rocker_flow_tbl_do(rocker_port, flags, entry); |
| 2554 | } |
| 2555 | |
Scott Feldman | 9f6bbf7 | 2014-11-28 14:34:27 +0100 | [diff] [blame] | 2556 | static int rocker_flow_tbl_acl(struct rocker_port *rocker_port, |
Scott Feldman | 4a6bb6d | 2015-02-25 20:15:37 -0800 | [diff] [blame] | 2557 | int flags, u32 in_pport, |
| 2558 | u32 in_pport_mask, |
Scott Feldman | 9f6bbf7 | 2014-11-28 14:34:27 +0100 | [diff] [blame] | 2559 | const u8 *eth_src, const u8 *eth_src_mask, |
| 2560 | const u8 *eth_dst, const u8 *eth_dst_mask, |
| 2561 | __be16 eth_type, |
| 2562 | __be16 vlan_id, __be16 vlan_id_mask, |
| 2563 | u8 ip_proto, u8 ip_proto_mask, |
| 2564 | u8 ip_tos, u8 ip_tos_mask, |
| 2565 | u32 group_id) |
| 2566 | { |
| 2567 | u32 priority; |
| 2568 | struct rocker_flow_tbl_entry *entry; |
| 2569 | |
| 2570 | entry = kzalloc(sizeof(*entry), rocker_op_flags_gfp(flags)); |
| 2571 | if (!entry) |
| 2572 | return -ENOMEM; |
| 2573 | |
| 2574 | priority = ROCKER_PRIORITY_ACL_NORMAL; |
| 2575 | if (eth_dst && eth_dst_mask) { |
| 2576 | if (memcmp(eth_dst_mask, mcast_mac, ETH_ALEN) == 0) |
| 2577 | priority = ROCKER_PRIORITY_ACL_DFLT; |
| 2578 | else if (is_link_local_ether_addr(eth_dst)) |
| 2579 | priority = ROCKER_PRIORITY_ACL_CTRL; |
| 2580 | } |
| 2581 | |
| 2582 | entry->key.priority = priority; |
| 2583 | entry->key.tbl_id = ROCKER_OF_DPA_TABLE_ID_ACL_POLICY; |
Scott Feldman | 4a6bb6d | 2015-02-25 20:15:37 -0800 | [diff] [blame] | 2584 | entry->key.acl.in_pport = in_pport; |
| 2585 | entry->key.acl.in_pport_mask = in_pport_mask; |
Scott Feldman | 9f6bbf7 | 2014-11-28 14:34:27 +0100 | [diff] [blame] | 2586 | |
| 2587 | if (eth_src) |
| 2588 | ether_addr_copy(entry->key.acl.eth_src, eth_src); |
| 2589 | if (eth_src_mask) |
| 2590 | ether_addr_copy(entry->key.acl.eth_src_mask, eth_src_mask); |
| 2591 | if (eth_dst) |
| 2592 | ether_addr_copy(entry->key.acl.eth_dst, eth_dst); |
| 2593 | if (eth_dst_mask) |
| 2594 | ether_addr_copy(entry->key.acl.eth_dst_mask, eth_dst_mask); |
| 2595 | |
| 2596 | entry->key.acl.eth_type = eth_type; |
| 2597 | entry->key.acl.vlan_id = vlan_id; |
| 2598 | entry->key.acl.vlan_id_mask = vlan_id_mask; |
| 2599 | entry->key.acl.ip_proto = ip_proto; |
| 2600 | entry->key.acl.ip_proto_mask = ip_proto_mask; |
| 2601 | entry->key.acl.ip_tos = ip_tos; |
| 2602 | entry->key.acl.ip_tos_mask = ip_tos_mask; |
| 2603 | entry->key.acl.group_id = group_id; |
| 2604 | |
| 2605 | return rocker_flow_tbl_do(rocker_port, flags, entry); |
| 2606 | } |
| 2607 | |
| 2608 | static struct rocker_group_tbl_entry * |
| 2609 | rocker_group_tbl_find(struct rocker *rocker, |
| 2610 | struct rocker_group_tbl_entry *match) |
| 2611 | { |
| 2612 | struct rocker_group_tbl_entry *found; |
| 2613 | |
| 2614 | hash_for_each_possible(rocker->group_tbl, found, |
| 2615 | entry, match->group_id) { |
| 2616 | if (found->group_id == match->group_id) |
| 2617 | return found; |
| 2618 | } |
| 2619 | |
| 2620 | return NULL; |
| 2621 | } |
| 2622 | |
| 2623 | static void rocker_group_tbl_entry_free(struct rocker_group_tbl_entry *entry) |
| 2624 | { |
| 2625 | switch (ROCKER_GROUP_TYPE_GET(entry->group_id)) { |
| 2626 | case ROCKER_OF_DPA_GROUP_TYPE_L2_FLOOD: |
| 2627 | case ROCKER_OF_DPA_GROUP_TYPE_L2_MCAST: |
| 2628 | kfree(entry->group_ids); |
| 2629 | break; |
| 2630 | default: |
| 2631 | break; |
| 2632 | } |
| 2633 | kfree(entry); |
| 2634 | } |
| 2635 | |
| 2636 | static int rocker_group_tbl_add(struct rocker_port *rocker_port, |
| 2637 | struct rocker_group_tbl_entry *match, |
| 2638 | bool nowait) |
| 2639 | { |
| 2640 | struct rocker *rocker = rocker_port->rocker; |
| 2641 | struct rocker_group_tbl_entry *found; |
| 2642 | unsigned long flags; |
Scott Feldman | 9f6bbf7 | 2014-11-28 14:34:27 +0100 | [diff] [blame] | 2643 | |
| 2644 | spin_lock_irqsave(&rocker->group_tbl_lock, flags); |
| 2645 | |
| 2646 | found = rocker_group_tbl_find(rocker, match); |
| 2647 | |
| 2648 | if (found) { |
| 2649 | hash_del(&found->entry); |
| 2650 | rocker_group_tbl_entry_free(found); |
| 2651 | found = match; |
| 2652 | found->cmd = ROCKER_TLV_CMD_TYPE_OF_DPA_GROUP_MOD; |
| 2653 | } else { |
| 2654 | found = match; |
| 2655 | found->cmd = ROCKER_TLV_CMD_TYPE_OF_DPA_GROUP_ADD; |
| 2656 | } |
| 2657 | |
| 2658 | hash_add(rocker->group_tbl, &found->entry, found->group_id); |
| 2659 | |
| 2660 | spin_unlock_irqrestore(&rocker->group_tbl_lock, flags); |
| 2661 | |
Scott Feldman | c1beeef | 2015-03-05 21:21:20 -0800 | [diff] [blame] | 2662 | return rocker_cmd_exec(rocker, rocker_port, |
| 2663 | rocker_cmd_group_tbl_add, |
| 2664 | found, NULL, NULL, nowait); |
Scott Feldman | 9f6bbf7 | 2014-11-28 14:34:27 +0100 | [diff] [blame] | 2665 | } |
| 2666 | |
| 2667 | static int rocker_group_tbl_del(struct rocker_port *rocker_port, |
| 2668 | struct rocker_group_tbl_entry *match, |
| 2669 | bool nowait) |
| 2670 | { |
| 2671 | struct rocker *rocker = rocker_port->rocker; |
| 2672 | struct rocker_group_tbl_entry *found; |
| 2673 | unsigned long flags; |
| 2674 | int err = 0; |
| 2675 | |
| 2676 | spin_lock_irqsave(&rocker->group_tbl_lock, flags); |
| 2677 | |
| 2678 | found = rocker_group_tbl_find(rocker, match); |
| 2679 | |
| 2680 | if (found) { |
| 2681 | hash_del(&found->entry); |
| 2682 | found->cmd = ROCKER_TLV_CMD_TYPE_OF_DPA_GROUP_DEL; |
| 2683 | } |
| 2684 | |
| 2685 | spin_unlock_irqrestore(&rocker->group_tbl_lock, flags); |
| 2686 | |
| 2687 | rocker_group_tbl_entry_free(match); |
| 2688 | |
| 2689 | if (found) { |
| 2690 | err = rocker_cmd_exec(rocker, rocker_port, |
| 2691 | rocker_cmd_group_tbl_del, |
| 2692 | found, NULL, NULL, nowait); |
| 2693 | rocker_group_tbl_entry_free(found); |
| 2694 | } |
| 2695 | |
| 2696 | return err; |
| 2697 | } |
| 2698 | |
| 2699 | static int rocker_group_tbl_do(struct rocker_port *rocker_port, |
| 2700 | int flags, struct rocker_group_tbl_entry *entry) |
| 2701 | { |
| 2702 | bool nowait = flags & ROCKER_OP_FLAG_NOWAIT; |
| 2703 | |
| 2704 | if (flags & ROCKER_OP_FLAG_REMOVE) |
| 2705 | return rocker_group_tbl_del(rocker_port, entry, nowait); |
| 2706 | else |
| 2707 | return rocker_group_tbl_add(rocker_port, entry, nowait); |
| 2708 | } |
| 2709 | |
| 2710 | static int rocker_group_l2_interface(struct rocker_port *rocker_port, |
| 2711 | int flags, __be16 vlan_id, |
Scott Feldman | 4a6bb6d | 2015-02-25 20:15:37 -0800 | [diff] [blame] | 2712 | u32 out_pport, int pop_vlan) |
Scott Feldman | 9f6bbf7 | 2014-11-28 14:34:27 +0100 | [diff] [blame] | 2713 | { |
| 2714 | struct rocker_group_tbl_entry *entry; |
| 2715 | |
| 2716 | entry = kzalloc(sizeof(*entry), rocker_op_flags_gfp(flags)); |
| 2717 | if (!entry) |
| 2718 | return -ENOMEM; |
| 2719 | |
Scott Feldman | 4a6bb6d | 2015-02-25 20:15:37 -0800 | [diff] [blame] | 2720 | entry->group_id = ROCKER_GROUP_L2_INTERFACE(vlan_id, out_pport); |
Scott Feldman | 9f6bbf7 | 2014-11-28 14:34:27 +0100 | [diff] [blame] | 2721 | entry->l2_interface.pop_vlan = pop_vlan; |
| 2722 | |
| 2723 | return rocker_group_tbl_do(rocker_port, flags, entry); |
| 2724 | } |
| 2725 | |
| 2726 | static int rocker_group_l2_fan_out(struct rocker_port *rocker_port, |
| 2727 | int flags, u8 group_count, |
| 2728 | u32 *group_ids, u32 group_id) |
| 2729 | { |
| 2730 | struct rocker_group_tbl_entry *entry; |
| 2731 | |
| 2732 | entry = kzalloc(sizeof(*entry), rocker_op_flags_gfp(flags)); |
| 2733 | if (!entry) |
| 2734 | return -ENOMEM; |
| 2735 | |
| 2736 | entry->group_id = group_id; |
| 2737 | entry->group_count = group_count; |
| 2738 | |
| 2739 | entry->group_ids = kcalloc(group_count, sizeof(u32), |
| 2740 | rocker_op_flags_gfp(flags)); |
| 2741 | if (!entry->group_ids) { |
| 2742 | kfree(entry); |
| 2743 | return -ENOMEM; |
| 2744 | } |
| 2745 | memcpy(entry->group_ids, group_ids, group_count * sizeof(u32)); |
| 2746 | |
| 2747 | return rocker_group_tbl_do(rocker_port, flags, entry); |
| 2748 | } |
| 2749 | |
| 2750 | static int rocker_group_l2_flood(struct rocker_port *rocker_port, |
| 2751 | int flags, __be16 vlan_id, |
| 2752 | u8 group_count, u32 *group_ids, |
| 2753 | u32 group_id) |
| 2754 | { |
| 2755 | return rocker_group_l2_fan_out(rocker_port, flags, |
| 2756 | group_count, group_ids, |
| 2757 | group_id); |
| 2758 | } |
| 2759 | |
Scott Feldman | c1beeef | 2015-03-05 21:21:20 -0800 | [diff] [blame] | 2760 | static int rocker_group_l3_unicast(struct rocker_port *rocker_port, |
| 2761 | int flags, u32 index, u8 *src_mac, |
| 2762 | u8 *dst_mac, __be16 vlan_id, |
| 2763 | bool ttl_check, u32 pport) |
| 2764 | { |
| 2765 | struct rocker_group_tbl_entry *entry; |
| 2766 | |
| 2767 | entry = kzalloc(sizeof(*entry), rocker_op_flags_gfp(flags)); |
| 2768 | if (!entry) |
| 2769 | return -ENOMEM; |
| 2770 | |
| 2771 | entry->group_id = ROCKER_GROUP_L3_UNICAST(index); |
| 2772 | if (src_mac) |
| 2773 | ether_addr_copy(entry->l3_unicast.eth_src, src_mac); |
| 2774 | if (dst_mac) |
| 2775 | ether_addr_copy(entry->l3_unicast.eth_dst, dst_mac); |
| 2776 | entry->l3_unicast.vlan_id = vlan_id; |
| 2777 | entry->l3_unicast.ttl_check = ttl_check; |
| 2778 | entry->l3_unicast.group_id = ROCKER_GROUP_L2_INTERFACE(vlan_id, pport); |
| 2779 | |
| 2780 | return rocker_group_tbl_do(rocker_port, flags, entry); |
| 2781 | } |
| 2782 | |
| 2783 | static struct rocker_neigh_tbl_entry * |
| 2784 | rocker_neigh_tbl_find(struct rocker *rocker, __be32 ip_addr) |
| 2785 | { |
| 2786 | struct rocker_neigh_tbl_entry *found; |
| 2787 | |
Scott Feldman | 0f43deb | 2015-03-06 15:54:51 -0800 | [diff] [blame] | 2788 | hash_for_each_possible(rocker->neigh_tbl, found, |
| 2789 | entry, be32_to_cpu(ip_addr)) |
Scott Feldman | c1beeef | 2015-03-05 21:21:20 -0800 | [diff] [blame] | 2790 | if (found->ip_addr == ip_addr) |
| 2791 | return found; |
| 2792 | |
| 2793 | return NULL; |
| 2794 | } |
| 2795 | |
| 2796 | static void _rocker_neigh_add(struct rocker *rocker, |
| 2797 | struct rocker_neigh_tbl_entry *entry) |
| 2798 | { |
| 2799 | entry->index = rocker->neigh_tbl_next_index++; |
| 2800 | entry->ref_count++; |
Scott Feldman | 0f43deb | 2015-03-06 15:54:51 -0800 | [diff] [blame] | 2801 | hash_add(rocker->neigh_tbl, &entry->entry, |
| 2802 | be32_to_cpu(entry->ip_addr)); |
Scott Feldman | c1beeef | 2015-03-05 21:21:20 -0800 | [diff] [blame] | 2803 | } |
| 2804 | |
| 2805 | static void _rocker_neigh_del(struct rocker *rocker, |
| 2806 | struct rocker_neigh_tbl_entry *entry) |
| 2807 | { |
| 2808 | if (--entry->ref_count == 0) { |
| 2809 | hash_del(&entry->entry); |
| 2810 | kfree(entry); |
| 2811 | } |
| 2812 | } |
| 2813 | |
| 2814 | static void _rocker_neigh_update(struct rocker *rocker, |
| 2815 | struct rocker_neigh_tbl_entry *entry, |
| 2816 | u8 *eth_dst, bool ttl_check) |
| 2817 | { |
| 2818 | if (eth_dst) { |
| 2819 | ether_addr_copy(entry->eth_dst, eth_dst); |
| 2820 | entry->ttl_check = ttl_check; |
| 2821 | } else { |
| 2822 | entry->ref_count++; |
| 2823 | } |
| 2824 | } |
| 2825 | |
| 2826 | static int rocker_port_ipv4_neigh(struct rocker_port *rocker_port, |
| 2827 | int flags, __be32 ip_addr, u8 *eth_dst) |
| 2828 | { |
| 2829 | struct rocker *rocker = rocker_port->rocker; |
| 2830 | struct rocker_neigh_tbl_entry *entry; |
| 2831 | struct rocker_neigh_tbl_entry *found; |
| 2832 | unsigned long lock_flags; |
| 2833 | __be16 eth_type = htons(ETH_P_IP); |
| 2834 | enum rocker_of_dpa_table_id goto_tbl = |
| 2835 | ROCKER_OF_DPA_TABLE_ID_ACL_POLICY; |
| 2836 | u32 group_id; |
| 2837 | u32 priority = 0; |
| 2838 | bool adding = !(flags & ROCKER_OP_FLAG_REMOVE); |
| 2839 | bool updating; |
| 2840 | bool removing; |
| 2841 | int err = 0; |
| 2842 | |
| 2843 | entry = kzalloc(sizeof(*entry), rocker_op_flags_gfp(flags)); |
| 2844 | if (!entry) |
| 2845 | return -ENOMEM; |
| 2846 | |
| 2847 | spin_lock_irqsave(&rocker->neigh_tbl_lock, lock_flags); |
| 2848 | |
| 2849 | found = rocker_neigh_tbl_find(rocker, ip_addr); |
| 2850 | |
| 2851 | updating = found && adding; |
| 2852 | removing = found && !adding; |
| 2853 | adding = !found && adding; |
| 2854 | |
| 2855 | if (adding) { |
| 2856 | entry->ip_addr = ip_addr; |
| 2857 | entry->dev = rocker_port->dev; |
| 2858 | ether_addr_copy(entry->eth_dst, eth_dst); |
| 2859 | entry->ttl_check = true; |
| 2860 | _rocker_neigh_add(rocker, entry); |
| 2861 | } else if (removing) { |
| 2862 | memcpy(entry, found, sizeof(*entry)); |
| 2863 | _rocker_neigh_del(rocker, found); |
| 2864 | } else if (updating) { |
| 2865 | _rocker_neigh_update(rocker, found, eth_dst, true); |
| 2866 | memcpy(entry, found, sizeof(*entry)); |
| 2867 | } else { |
| 2868 | err = -ENOENT; |
| 2869 | } |
| 2870 | |
| 2871 | spin_unlock_irqrestore(&rocker->neigh_tbl_lock, lock_flags); |
| 2872 | |
| 2873 | if (err) |
| 2874 | goto err_out; |
| 2875 | |
| 2876 | /* For each active neighbor, we have an L3 unicast group and |
| 2877 | * a /32 route to the neighbor, which uses the L3 unicast |
| 2878 | * group. The L3 unicast group can also be referred to by |
| 2879 | * other routes' nexthops. |
| 2880 | */ |
| 2881 | |
| 2882 | err = rocker_group_l3_unicast(rocker_port, flags, |
| 2883 | entry->index, |
| 2884 | rocker_port->dev->dev_addr, |
| 2885 | entry->eth_dst, |
| 2886 | rocker_port->internal_vlan_id, |
| 2887 | entry->ttl_check, |
| 2888 | rocker_port->pport); |
| 2889 | if (err) { |
| 2890 | netdev_err(rocker_port->dev, |
| 2891 | "Error (%d) L3 unicast group index %d\n", |
| 2892 | err, entry->index); |
| 2893 | goto err_out; |
| 2894 | } |
| 2895 | |
| 2896 | if (adding || removing) { |
| 2897 | group_id = ROCKER_GROUP_L3_UNICAST(entry->index); |
| 2898 | err = rocker_flow_tbl_ucast4_routing(rocker_port, |
| 2899 | eth_type, ip_addr, |
| 2900 | inet_make_mask(32), |
| 2901 | priority, goto_tbl, |
| 2902 | group_id, flags); |
| 2903 | |
| 2904 | if (err) |
| 2905 | netdev_err(rocker_port->dev, |
| 2906 | "Error (%d) /32 unicast route %pI4 group 0x%08x\n", |
| 2907 | err, &entry->ip_addr, group_id); |
| 2908 | } |
| 2909 | |
| 2910 | err_out: |
| 2911 | if (!adding) |
| 2912 | kfree(entry); |
| 2913 | |
| 2914 | return err; |
| 2915 | } |
| 2916 | |
| 2917 | static int rocker_port_ipv4_resolve(struct rocker_port *rocker_port, |
| 2918 | __be32 ip_addr) |
| 2919 | { |
| 2920 | struct net_device *dev = rocker_port->dev; |
Scott Feldman | 0f43deb | 2015-03-06 15:54:51 -0800 | [diff] [blame] | 2921 | struct neighbour *n = __ipv4_neigh_lookup(dev, (__force u32)ip_addr); |
Scott Feldman | c1beeef | 2015-03-05 21:21:20 -0800 | [diff] [blame] | 2922 | int err = 0; |
| 2923 | |
| 2924 | if (!n) |
| 2925 | n = neigh_create(&arp_tbl, &ip_addr, dev); |
| 2926 | if (!n) |
| 2927 | return -ENOMEM; |
| 2928 | |
| 2929 | /* If the neigh is already resolved, then go ahead and |
| 2930 | * install the entry, otherwise start the ARP process to |
| 2931 | * resolve the neigh. |
| 2932 | */ |
| 2933 | |
| 2934 | if (n->nud_state & NUD_VALID) |
| 2935 | err = rocker_port_ipv4_neigh(rocker_port, 0, ip_addr, n->ha); |
| 2936 | else |
| 2937 | neigh_event_send(n, NULL); |
| 2938 | |
| 2939 | return err; |
| 2940 | } |
| 2941 | |
| 2942 | static int rocker_port_ipv4_nh(struct rocker_port *rocker_port, int flags, |
| 2943 | __be32 ip_addr, u32 *index) |
| 2944 | { |
| 2945 | struct rocker *rocker = rocker_port->rocker; |
| 2946 | struct rocker_neigh_tbl_entry *entry; |
| 2947 | struct rocker_neigh_tbl_entry *found; |
| 2948 | unsigned long lock_flags; |
| 2949 | bool adding = !(flags & ROCKER_OP_FLAG_REMOVE); |
| 2950 | bool updating; |
| 2951 | bool removing; |
| 2952 | bool resolved = true; |
| 2953 | int err = 0; |
| 2954 | |
| 2955 | entry = kzalloc(sizeof(*entry), rocker_op_flags_gfp(flags)); |
| 2956 | if (!entry) |
| 2957 | return -ENOMEM; |
| 2958 | |
| 2959 | spin_lock_irqsave(&rocker->neigh_tbl_lock, lock_flags); |
| 2960 | |
| 2961 | found = rocker_neigh_tbl_find(rocker, ip_addr); |
| 2962 | if (found) |
| 2963 | *index = found->index; |
| 2964 | |
| 2965 | updating = found && adding; |
| 2966 | removing = found && !adding; |
| 2967 | adding = !found && adding; |
| 2968 | |
| 2969 | if (adding) { |
| 2970 | entry->ip_addr = ip_addr; |
| 2971 | entry->dev = rocker_port->dev; |
| 2972 | _rocker_neigh_add(rocker, entry); |
| 2973 | *index = entry->index; |
| 2974 | resolved = false; |
| 2975 | } else if (removing) { |
| 2976 | _rocker_neigh_del(rocker, found); |
| 2977 | } else if (updating) { |
| 2978 | _rocker_neigh_update(rocker, found, NULL, false); |
| 2979 | resolved = !is_zero_ether_addr(found->eth_dst); |
| 2980 | } else { |
| 2981 | err = -ENOENT; |
| 2982 | } |
| 2983 | |
| 2984 | spin_unlock_irqrestore(&rocker->neigh_tbl_lock, lock_flags); |
| 2985 | |
| 2986 | if (!adding) |
| 2987 | kfree(entry); |
| 2988 | |
| 2989 | if (err) |
| 2990 | return err; |
| 2991 | |
| 2992 | /* Resolved means neigh ip_addr is resolved to neigh mac. */ |
| 2993 | |
| 2994 | if (!resolved) |
| 2995 | err = rocker_port_ipv4_resolve(rocker_port, ip_addr); |
| 2996 | |
| 2997 | return err; |
| 2998 | } |
| 2999 | |
Scott Feldman | 6c70794 | 2014-11-28 14:34:28 +0100 | [diff] [blame] | 3000 | static int rocker_port_vlan_flood_group(struct rocker_port *rocker_port, |
| 3001 | int flags, __be16 vlan_id) |
| 3002 | { |
| 3003 | struct rocker_port *p; |
| 3004 | struct rocker *rocker = rocker_port->rocker; |
| 3005 | u32 group_id = ROCKER_GROUP_L2_FLOOD(vlan_id, 0); |
Scott Feldman | 04f49fa | 2015-03-15 23:04:46 -0700 | [diff] [blame] | 3006 | u32 *group_ids; |
Scott Feldman | 6c70794 | 2014-11-28 14:34:28 +0100 | [diff] [blame] | 3007 | u8 group_count = 0; |
Scott Feldman | 04f49fa | 2015-03-15 23:04:46 -0700 | [diff] [blame] | 3008 | int err = 0; |
Scott Feldman | 6c70794 | 2014-11-28 14:34:28 +0100 | [diff] [blame] | 3009 | int i; |
| 3010 | |
Scott Feldman | 04f49fa | 2015-03-15 23:04:46 -0700 | [diff] [blame] | 3011 | group_ids = kcalloc(rocker->port_count, sizeof(u32), |
| 3012 | rocker_op_flags_gfp(flags)); |
| 3013 | if (!group_ids) |
| 3014 | return -ENOMEM; |
| 3015 | |
Scott Feldman | 6c70794 | 2014-11-28 14:34:28 +0100 | [diff] [blame] | 3016 | /* Adjust the flood group for this VLAN. The flood group |
| 3017 | * references an L2 interface group for each port in this |
| 3018 | * VLAN. |
| 3019 | */ |
| 3020 | |
| 3021 | for (i = 0; i < rocker->port_count; i++) { |
| 3022 | p = rocker->ports[i]; |
| 3023 | if (!rocker_port_is_bridged(p)) |
| 3024 | continue; |
| 3025 | if (test_bit(ntohs(vlan_id), p->vlan_bitmap)) { |
| 3026 | group_ids[group_count++] = |
Scott Feldman | 4a6bb6d | 2015-02-25 20:15:37 -0800 | [diff] [blame] | 3027 | ROCKER_GROUP_L2_INTERFACE(vlan_id, p->pport); |
Scott Feldman | 6c70794 | 2014-11-28 14:34:28 +0100 | [diff] [blame] | 3028 | } |
| 3029 | } |
| 3030 | |
| 3031 | /* If there are no bridged ports in this VLAN, we're done */ |
| 3032 | if (group_count == 0) |
Scott Feldman | 04f49fa | 2015-03-15 23:04:46 -0700 | [diff] [blame] | 3033 | goto no_ports_in_vlan; |
Scott Feldman | 6c70794 | 2014-11-28 14:34:28 +0100 | [diff] [blame] | 3034 | |
| 3035 | err = rocker_group_l2_flood(rocker_port, flags, vlan_id, |
| 3036 | group_count, group_ids, |
| 3037 | group_id); |
| 3038 | if (err) |
| 3039 | netdev_err(rocker_port->dev, |
| 3040 | "Error (%d) port VLAN l2 flood group\n", err); |
| 3041 | |
Scott Feldman | 04f49fa | 2015-03-15 23:04:46 -0700 | [diff] [blame] | 3042 | no_ports_in_vlan: |
| 3043 | kfree(group_ids); |
Scott Feldman | 6c70794 | 2014-11-28 14:34:28 +0100 | [diff] [blame] | 3044 | return err; |
| 3045 | } |
| 3046 | |
| 3047 | static int rocker_port_vlan_l2_groups(struct rocker_port *rocker_port, |
| 3048 | int flags, __be16 vlan_id, |
| 3049 | bool pop_vlan) |
| 3050 | { |
| 3051 | struct rocker *rocker = rocker_port->rocker; |
| 3052 | struct rocker_port *p; |
| 3053 | bool adding = !(flags & ROCKER_OP_FLAG_REMOVE); |
Scott Feldman | 4a6bb6d | 2015-02-25 20:15:37 -0800 | [diff] [blame] | 3054 | u32 out_pport; |
Scott Feldman | 6c70794 | 2014-11-28 14:34:28 +0100 | [diff] [blame] | 3055 | int ref = 0; |
| 3056 | int err; |
| 3057 | int i; |
| 3058 | |
| 3059 | /* An L2 interface group for this port in this VLAN, but |
| 3060 | * only when port STP state is LEARNING|FORWARDING. |
| 3061 | */ |
| 3062 | |
| 3063 | if (rocker_port->stp_state == BR_STATE_LEARNING || |
| 3064 | rocker_port->stp_state == BR_STATE_FORWARDING) { |
Scott Feldman | 4a6bb6d | 2015-02-25 20:15:37 -0800 | [diff] [blame] | 3065 | out_pport = rocker_port->pport; |
Scott Feldman | 6c70794 | 2014-11-28 14:34:28 +0100 | [diff] [blame] | 3066 | err = rocker_group_l2_interface(rocker_port, flags, |
Scott Feldman | 4a6bb6d | 2015-02-25 20:15:37 -0800 | [diff] [blame] | 3067 | vlan_id, out_pport, |
Scott Feldman | 6c70794 | 2014-11-28 14:34:28 +0100 | [diff] [blame] | 3068 | pop_vlan); |
| 3069 | if (err) { |
| 3070 | netdev_err(rocker_port->dev, |
Scott Feldman | 4a6bb6d | 2015-02-25 20:15:37 -0800 | [diff] [blame] | 3071 | "Error (%d) port VLAN l2 group for pport %d\n", |
| 3072 | err, out_pport); |
Scott Feldman | 6c70794 | 2014-11-28 14:34:28 +0100 | [diff] [blame] | 3073 | return err; |
| 3074 | } |
| 3075 | } |
| 3076 | |
| 3077 | /* An L2 interface group for this VLAN to CPU port. |
| 3078 | * Add when first port joins this VLAN and destroy when |
| 3079 | * last port leaves this VLAN. |
| 3080 | */ |
| 3081 | |
| 3082 | for (i = 0; i < rocker->port_count; i++) { |
| 3083 | p = rocker->ports[i]; |
| 3084 | if (test_bit(ntohs(vlan_id), p->vlan_bitmap)) |
| 3085 | ref++; |
| 3086 | } |
| 3087 | |
| 3088 | if ((!adding || ref != 1) && (adding || ref != 0)) |
| 3089 | return 0; |
| 3090 | |
Scott Feldman | 4a6bb6d | 2015-02-25 20:15:37 -0800 | [diff] [blame] | 3091 | out_pport = 0; |
Scott Feldman | 6c70794 | 2014-11-28 14:34:28 +0100 | [diff] [blame] | 3092 | err = rocker_group_l2_interface(rocker_port, flags, |
Scott Feldman | 4a6bb6d | 2015-02-25 20:15:37 -0800 | [diff] [blame] | 3093 | vlan_id, out_pport, |
Scott Feldman | 6c70794 | 2014-11-28 14:34:28 +0100 | [diff] [blame] | 3094 | pop_vlan); |
| 3095 | if (err) { |
| 3096 | netdev_err(rocker_port->dev, |
| 3097 | "Error (%d) port VLAN l2 group for CPU port\n", err); |
| 3098 | return err; |
| 3099 | } |
| 3100 | |
| 3101 | return 0; |
| 3102 | } |
| 3103 | |
Scott Feldman | 9f6bbf7 | 2014-11-28 14:34:27 +0100 | [diff] [blame] | 3104 | static struct rocker_ctrl { |
| 3105 | const u8 *eth_dst; |
| 3106 | const u8 *eth_dst_mask; |
Jiri Pirko | 11e6c65 | 2014-12-03 14:14:54 +0100 | [diff] [blame] | 3107 | __be16 eth_type; |
Scott Feldman | 9f6bbf7 | 2014-11-28 14:34:27 +0100 | [diff] [blame] | 3108 | bool acl; |
| 3109 | bool bridge; |
| 3110 | bool term; |
| 3111 | bool copy_to_cpu; |
| 3112 | } rocker_ctrls[] = { |
| 3113 | [ROCKER_CTRL_LINK_LOCAL_MCAST] = { |
| 3114 | /* pass link local multicast pkts up to CPU for filtering */ |
| 3115 | .eth_dst = ll_mac, |
| 3116 | .eth_dst_mask = ll_mask, |
| 3117 | .acl = true, |
| 3118 | }, |
| 3119 | [ROCKER_CTRL_LOCAL_ARP] = { |
| 3120 | /* pass local ARP pkts up to CPU */ |
| 3121 | .eth_dst = zero_mac, |
| 3122 | .eth_dst_mask = zero_mac, |
| 3123 | .eth_type = htons(ETH_P_ARP), |
| 3124 | .acl = true, |
| 3125 | }, |
| 3126 | [ROCKER_CTRL_IPV4_MCAST] = { |
| 3127 | /* pass IPv4 mcast pkts up to CPU, RFC 1112 */ |
| 3128 | .eth_dst = ipv4_mcast, |
| 3129 | .eth_dst_mask = ipv4_mask, |
| 3130 | .eth_type = htons(ETH_P_IP), |
| 3131 | .term = true, |
| 3132 | .copy_to_cpu = true, |
| 3133 | }, |
| 3134 | [ROCKER_CTRL_IPV6_MCAST] = { |
| 3135 | /* pass IPv6 mcast pkts up to CPU, RFC 2464 */ |
| 3136 | .eth_dst = ipv6_mcast, |
| 3137 | .eth_dst_mask = ipv6_mask, |
| 3138 | .eth_type = htons(ETH_P_IPV6), |
| 3139 | .term = true, |
| 3140 | .copy_to_cpu = true, |
| 3141 | }, |
| 3142 | [ROCKER_CTRL_DFLT_BRIDGING] = { |
| 3143 | /* flood any pkts on vlan */ |
| 3144 | .bridge = true, |
| 3145 | .copy_to_cpu = true, |
| 3146 | }, |
| 3147 | }; |
| 3148 | |
| 3149 | static int rocker_port_ctrl_vlan_acl(struct rocker_port *rocker_port, |
| 3150 | int flags, struct rocker_ctrl *ctrl, |
| 3151 | __be16 vlan_id) |
| 3152 | { |
Scott Feldman | 4a6bb6d | 2015-02-25 20:15:37 -0800 | [diff] [blame] | 3153 | u32 in_pport = rocker_port->pport; |
| 3154 | u32 in_pport_mask = 0xffffffff; |
| 3155 | u32 out_pport = 0; |
Scott Feldman | 9f6bbf7 | 2014-11-28 14:34:27 +0100 | [diff] [blame] | 3156 | u8 *eth_src = NULL; |
| 3157 | u8 *eth_src_mask = NULL; |
| 3158 | __be16 vlan_id_mask = htons(0xffff); |
| 3159 | u8 ip_proto = 0; |
| 3160 | u8 ip_proto_mask = 0; |
| 3161 | u8 ip_tos = 0; |
| 3162 | u8 ip_tos_mask = 0; |
Scott Feldman | 4a6bb6d | 2015-02-25 20:15:37 -0800 | [diff] [blame] | 3163 | u32 group_id = ROCKER_GROUP_L2_INTERFACE(vlan_id, out_pport); |
Scott Feldman | 9f6bbf7 | 2014-11-28 14:34:27 +0100 | [diff] [blame] | 3164 | int err; |
| 3165 | |
| 3166 | err = rocker_flow_tbl_acl(rocker_port, flags, |
Scott Feldman | 4a6bb6d | 2015-02-25 20:15:37 -0800 | [diff] [blame] | 3167 | in_pport, in_pport_mask, |
Scott Feldman | 9f6bbf7 | 2014-11-28 14:34:27 +0100 | [diff] [blame] | 3168 | eth_src, eth_src_mask, |
| 3169 | ctrl->eth_dst, ctrl->eth_dst_mask, |
| 3170 | ctrl->eth_type, |
| 3171 | vlan_id, vlan_id_mask, |
| 3172 | ip_proto, ip_proto_mask, |
| 3173 | ip_tos, ip_tos_mask, |
| 3174 | group_id); |
| 3175 | |
| 3176 | if (err) |
| 3177 | netdev_err(rocker_port->dev, "Error (%d) ctrl ACL\n", err); |
| 3178 | |
| 3179 | return err; |
| 3180 | } |
| 3181 | |
Scott Feldman | 6c70794 | 2014-11-28 14:34:28 +0100 | [diff] [blame] | 3182 | static int rocker_port_ctrl_vlan_bridge(struct rocker_port *rocker_port, |
| 3183 | int flags, struct rocker_ctrl *ctrl, |
| 3184 | __be16 vlan_id) |
| 3185 | { |
| 3186 | enum rocker_of_dpa_table_id goto_tbl = |
| 3187 | ROCKER_OF_DPA_TABLE_ID_ACL_POLICY; |
| 3188 | u32 group_id = ROCKER_GROUP_L2_FLOOD(vlan_id, 0); |
| 3189 | u32 tunnel_id = 0; |
| 3190 | int err; |
| 3191 | |
| 3192 | if (!rocker_port_is_bridged(rocker_port)) |
| 3193 | return 0; |
| 3194 | |
| 3195 | err = rocker_flow_tbl_bridge(rocker_port, flags, |
| 3196 | ctrl->eth_dst, ctrl->eth_dst_mask, |
| 3197 | vlan_id, tunnel_id, |
| 3198 | goto_tbl, group_id, ctrl->copy_to_cpu); |
| 3199 | |
| 3200 | if (err) |
| 3201 | netdev_err(rocker_port->dev, "Error (%d) ctrl FLOOD\n", err); |
| 3202 | |
| 3203 | return err; |
| 3204 | } |
| 3205 | |
Scott Feldman | 9f6bbf7 | 2014-11-28 14:34:27 +0100 | [diff] [blame] | 3206 | static int rocker_port_ctrl_vlan_term(struct rocker_port *rocker_port, |
| 3207 | int flags, struct rocker_ctrl *ctrl, |
| 3208 | __be16 vlan_id) |
| 3209 | { |
Scott Feldman | 4a6bb6d | 2015-02-25 20:15:37 -0800 | [diff] [blame] | 3210 | u32 in_pport_mask = 0xffffffff; |
Scott Feldman | 9f6bbf7 | 2014-11-28 14:34:27 +0100 | [diff] [blame] | 3211 | __be16 vlan_id_mask = htons(0xffff); |
| 3212 | int err; |
| 3213 | |
| 3214 | if (ntohs(vlan_id) == 0) |
| 3215 | vlan_id = rocker_port->internal_vlan_id; |
| 3216 | |
| 3217 | err = rocker_flow_tbl_term_mac(rocker_port, |
Scott Feldman | 4a6bb6d | 2015-02-25 20:15:37 -0800 | [diff] [blame] | 3218 | rocker_port->pport, in_pport_mask, |
Scott Feldman | 9f6bbf7 | 2014-11-28 14:34:27 +0100 | [diff] [blame] | 3219 | ctrl->eth_type, ctrl->eth_dst, |
| 3220 | ctrl->eth_dst_mask, vlan_id, |
| 3221 | vlan_id_mask, ctrl->copy_to_cpu, |
| 3222 | flags); |
| 3223 | |
| 3224 | if (err) |
| 3225 | netdev_err(rocker_port->dev, "Error (%d) ctrl term\n", err); |
| 3226 | |
| 3227 | return err; |
| 3228 | } |
| 3229 | |
| 3230 | static int rocker_port_ctrl_vlan(struct rocker_port *rocker_port, int flags, |
| 3231 | struct rocker_ctrl *ctrl, __be16 vlan_id) |
| 3232 | { |
| 3233 | if (ctrl->acl) |
| 3234 | return rocker_port_ctrl_vlan_acl(rocker_port, flags, |
| 3235 | ctrl, vlan_id); |
Scott Feldman | 6c70794 | 2014-11-28 14:34:28 +0100 | [diff] [blame] | 3236 | if (ctrl->bridge) |
| 3237 | return rocker_port_ctrl_vlan_bridge(rocker_port, flags, |
| 3238 | ctrl, vlan_id); |
Scott Feldman | 9f6bbf7 | 2014-11-28 14:34:27 +0100 | [diff] [blame] | 3239 | |
| 3240 | if (ctrl->term) |
| 3241 | return rocker_port_ctrl_vlan_term(rocker_port, flags, |
| 3242 | ctrl, vlan_id); |
| 3243 | |
| 3244 | return -EOPNOTSUPP; |
| 3245 | } |
| 3246 | |
| 3247 | static int rocker_port_ctrl_vlan_add(struct rocker_port *rocker_port, |
| 3248 | int flags, __be16 vlan_id) |
| 3249 | { |
| 3250 | int err = 0; |
| 3251 | int i; |
| 3252 | |
| 3253 | for (i = 0; i < ROCKER_CTRL_MAX; i++) { |
| 3254 | if (rocker_port->ctrls[i]) { |
| 3255 | err = rocker_port_ctrl_vlan(rocker_port, flags, |
| 3256 | &rocker_ctrls[i], vlan_id); |
| 3257 | if (err) |
| 3258 | return err; |
| 3259 | } |
| 3260 | } |
| 3261 | |
| 3262 | return err; |
| 3263 | } |
| 3264 | |
| 3265 | static int rocker_port_ctrl(struct rocker_port *rocker_port, int flags, |
| 3266 | struct rocker_ctrl *ctrl) |
| 3267 | { |
| 3268 | u16 vid; |
| 3269 | int err = 0; |
| 3270 | |
| 3271 | for (vid = 1; vid < VLAN_N_VID; vid++) { |
| 3272 | if (!test_bit(vid, rocker_port->vlan_bitmap)) |
| 3273 | continue; |
| 3274 | err = rocker_port_ctrl_vlan(rocker_port, flags, |
| 3275 | ctrl, htons(vid)); |
| 3276 | if (err) |
| 3277 | break; |
| 3278 | } |
| 3279 | |
| 3280 | return err; |
| 3281 | } |
| 3282 | |
Scott Feldman | 6c70794 | 2014-11-28 14:34:28 +0100 | [diff] [blame] | 3283 | static int rocker_port_vlan(struct rocker_port *rocker_port, int flags, |
| 3284 | u16 vid) |
| 3285 | { |
| 3286 | enum rocker_of_dpa_table_id goto_tbl = |
| 3287 | ROCKER_OF_DPA_TABLE_ID_TERMINATION_MAC; |
Scott Feldman | 4a6bb6d | 2015-02-25 20:15:37 -0800 | [diff] [blame] | 3288 | u32 in_pport = rocker_port->pport; |
Scott Feldman | 6c70794 | 2014-11-28 14:34:28 +0100 | [diff] [blame] | 3289 | __be16 vlan_id = htons(vid); |
| 3290 | __be16 vlan_id_mask = htons(0xffff); |
| 3291 | __be16 internal_vlan_id; |
| 3292 | bool untagged; |
| 3293 | bool adding = !(flags & ROCKER_OP_FLAG_REMOVE); |
| 3294 | int err; |
| 3295 | |
| 3296 | internal_vlan_id = rocker_port_vid_to_vlan(rocker_port, vid, &untagged); |
| 3297 | |
| 3298 | if (adding && test_and_set_bit(ntohs(internal_vlan_id), |
| 3299 | rocker_port->vlan_bitmap)) |
| 3300 | return 0; /* already added */ |
| 3301 | else if (!adding && !test_and_clear_bit(ntohs(internal_vlan_id), |
| 3302 | rocker_port->vlan_bitmap)) |
| 3303 | return 0; /* already removed */ |
| 3304 | |
| 3305 | if (adding) { |
| 3306 | err = rocker_port_ctrl_vlan_add(rocker_port, flags, |
| 3307 | internal_vlan_id); |
| 3308 | if (err) { |
| 3309 | netdev_err(rocker_port->dev, |
| 3310 | "Error (%d) port ctrl vlan add\n", err); |
| 3311 | return err; |
| 3312 | } |
| 3313 | } |
| 3314 | |
| 3315 | err = rocker_port_vlan_l2_groups(rocker_port, flags, |
| 3316 | internal_vlan_id, untagged); |
| 3317 | if (err) { |
| 3318 | netdev_err(rocker_port->dev, |
| 3319 | "Error (%d) port VLAN l2 groups\n", err); |
| 3320 | return err; |
| 3321 | } |
| 3322 | |
| 3323 | err = rocker_port_vlan_flood_group(rocker_port, flags, |
| 3324 | internal_vlan_id); |
| 3325 | if (err) { |
| 3326 | netdev_err(rocker_port->dev, |
| 3327 | "Error (%d) port VLAN l2 flood group\n", err); |
| 3328 | return err; |
| 3329 | } |
| 3330 | |
| 3331 | err = rocker_flow_tbl_vlan(rocker_port, flags, |
Scott Feldman | 4a6bb6d | 2015-02-25 20:15:37 -0800 | [diff] [blame] | 3332 | in_pport, vlan_id, vlan_id_mask, |
Scott Feldman | 6c70794 | 2014-11-28 14:34:28 +0100 | [diff] [blame] | 3333 | goto_tbl, untagged, internal_vlan_id); |
| 3334 | if (err) |
| 3335 | netdev_err(rocker_port->dev, |
| 3336 | "Error (%d) port VLAN table\n", err); |
| 3337 | |
| 3338 | return err; |
| 3339 | } |
| 3340 | |
Scott Feldman | 9f6bbf7 | 2014-11-28 14:34:27 +0100 | [diff] [blame] | 3341 | static int rocker_port_ig_tbl(struct rocker_port *rocker_port, int flags) |
| 3342 | { |
| 3343 | enum rocker_of_dpa_table_id goto_tbl; |
Scott Feldman | 4a6bb6d | 2015-02-25 20:15:37 -0800 | [diff] [blame] | 3344 | u32 in_pport; |
| 3345 | u32 in_pport_mask; |
Scott Feldman | 9f6bbf7 | 2014-11-28 14:34:27 +0100 | [diff] [blame] | 3346 | int err; |
| 3347 | |
| 3348 | /* Normal Ethernet Frames. Matches pkts from any local physical |
| 3349 | * ports. Goto VLAN tbl. |
| 3350 | */ |
| 3351 | |
Scott Feldman | 4a6bb6d | 2015-02-25 20:15:37 -0800 | [diff] [blame] | 3352 | in_pport = 0; |
| 3353 | in_pport_mask = 0xffff0000; |
Scott Feldman | 9f6bbf7 | 2014-11-28 14:34:27 +0100 | [diff] [blame] | 3354 | goto_tbl = ROCKER_OF_DPA_TABLE_ID_VLAN; |
| 3355 | |
| 3356 | err = rocker_flow_tbl_ig_port(rocker_port, flags, |
Scott Feldman | 4a6bb6d | 2015-02-25 20:15:37 -0800 | [diff] [blame] | 3357 | in_pport, in_pport_mask, |
Scott Feldman | 9f6bbf7 | 2014-11-28 14:34:27 +0100 | [diff] [blame] | 3358 | goto_tbl); |
| 3359 | if (err) |
| 3360 | netdev_err(rocker_port->dev, |
| 3361 | "Error (%d) ingress port table entry\n", err); |
| 3362 | |
| 3363 | return err; |
| 3364 | } |
| 3365 | |
Scott Feldman | 6c70794 | 2014-11-28 14:34:28 +0100 | [diff] [blame] | 3366 | struct rocker_fdb_learn_work { |
| 3367 | struct work_struct work; |
| 3368 | struct net_device *dev; |
| 3369 | int flags; |
| 3370 | u8 addr[ETH_ALEN]; |
| 3371 | u16 vid; |
| 3372 | }; |
| 3373 | |
| 3374 | static void rocker_port_fdb_learn_work(struct work_struct *work) |
| 3375 | { |
| 3376 | struct rocker_fdb_learn_work *lw = |
| 3377 | container_of(work, struct rocker_fdb_learn_work, work); |
| 3378 | bool removing = (lw->flags & ROCKER_OP_FLAG_REMOVE); |
| 3379 | bool learned = (lw->flags & ROCKER_OP_FLAG_LEARNED); |
Jiri Pirko | 3aeb661 | 2015-01-15 23:49:37 +0100 | [diff] [blame] | 3380 | struct netdev_switch_notifier_fdb_info info; |
| 3381 | |
| 3382 | info.addr = lw->addr; |
| 3383 | info.vid = lw->vid; |
Scott Feldman | 6c70794 | 2014-11-28 14:34:28 +0100 | [diff] [blame] | 3384 | |
Thomas Graf | 51ace88 | 2014-11-28 14:34:32 +0100 | [diff] [blame] | 3385 | if (learned && removing) |
Jiri Pirko | 3aeb661 | 2015-01-15 23:49:37 +0100 | [diff] [blame] | 3386 | call_netdev_switch_notifiers(NETDEV_SWITCH_FDB_DEL, |
| 3387 | lw->dev, &info.info); |
Thomas Graf | 51ace88 | 2014-11-28 14:34:32 +0100 | [diff] [blame] | 3388 | else if (learned && !removing) |
Jiri Pirko | 3aeb661 | 2015-01-15 23:49:37 +0100 | [diff] [blame] | 3389 | call_netdev_switch_notifiers(NETDEV_SWITCH_FDB_ADD, |
| 3390 | lw->dev, &info.info); |
Scott Feldman | 6c70794 | 2014-11-28 14:34:28 +0100 | [diff] [blame] | 3391 | |
| 3392 | kfree(work); |
| 3393 | } |
| 3394 | |
| 3395 | static int rocker_port_fdb_learn(struct rocker_port *rocker_port, |
| 3396 | int flags, const u8 *addr, __be16 vlan_id) |
| 3397 | { |
| 3398 | struct rocker_fdb_learn_work *lw; |
| 3399 | enum rocker_of_dpa_table_id goto_tbl = |
| 3400 | ROCKER_OF_DPA_TABLE_ID_ACL_POLICY; |
Scott Feldman | 4a6bb6d | 2015-02-25 20:15:37 -0800 | [diff] [blame] | 3401 | u32 out_pport = rocker_port->pport; |
Scott Feldman | 6c70794 | 2014-11-28 14:34:28 +0100 | [diff] [blame] | 3402 | u32 tunnel_id = 0; |
| 3403 | u32 group_id = ROCKER_GROUP_NONE; |
Scott Feldman | 5111f80 | 2014-11-28 14:34:30 +0100 | [diff] [blame] | 3404 | bool syncing = !!(rocker_port->brport_flags & BR_LEARNING_SYNC); |
Scott Feldman | 6c70794 | 2014-11-28 14:34:28 +0100 | [diff] [blame] | 3405 | bool copy_to_cpu = false; |
| 3406 | int err; |
| 3407 | |
| 3408 | if (rocker_port_is_bridged(rocker_port)) |
Scott Feldman | 4a6bb6d | 2015-02-25 20:15:37 -0800 | [diff] [blame] | 3409 | group_id = ROCKER_GROUP_L2_INTERFACE(vlan_id, out_pport); |
Scott Feldman | 6c70794 | 2014-11-28 14:34:28 +0100 | [diff] [blame] | 3410 | |
| 3411 | if (!(flags & ROCKER_OP_FLAG_REFRESH)) { |
| 3412 | err = rocker_flow_tbl_bridge(rocker_port, flags, addr, NULL, |
| 3413 | vlan_id, tunnel_id, goto_tbl, |
| 3414 | group_id, copy_to_cpu); |
| 3415 | if (err) |
| 3416 | return err; |
| 3417 | } |
| 3418 | |
Scott Feldman | 5111f80 | 2014-11-28 14:34:30 +0100 | [diff] [blame] | 3419 | if (!syncing) |
| 3420 | return 0; |
| 3421 | |
Scott Feldman | 6c70794 | 2014-11-28 14:34:28 +0100 | [diff] [blame] | 3422 | if (!rocker_port_is_bridged(rocker_port)) |
| 3423 | return 0; |
| 3424 | |
| 3425 | lw = kmalloc(sizeof(*lw), rocker_op_flags_gfp(flags)); |
| 3426 | if (!lw) |
| 3427 | return -ENOMEM; |
| 3428 | |
| 3429 | INIT_WORK(&lw->work, rocker_port_fdb_learn_work); |
| 3430 | |
| 3431 | lw->dev = rocker_port->dev; |
| 3432 | lw->flags = flags; |
| 3433 | ether_addr_copy(lw->addr, addr); |
| 3434 | lw->vid = rocker_port_vlan_to_vid(rocker_port, vlan_id); |
| 3435 | |
| 3436 | schedule_work(&lw->work); |
| 3437 | |
| 3438 | return 0; |
| 3439 | } |
| 3440 | |
| 3441 | static struct rocker_fdb_tbl_entry * |
| 3442 | rocker_fdb_tbl_find(struct rocker *rocker, struct rocker_fdb_tbl_entry *match) |
| 3443 | { |
| 3444 | struct rocker_fdb_tbl_entry *found; |
| 3445 | |
| 3446 | hash_for_each_possible(rocker->fdb_tbl, found, entry, match->key_crc32) |
| 3447 | if (memcmp(&found->key, &match->key, sizeof(found->key)) == 0) |
| 3448 | return found; |
| 3449 | |
| 3450 | return NULL; |
| 3451 | } |
| 3452 | |
| 3453 | static int rocker_port_fdb(struct rocker_port *rocker_port, |
| 3454 | const unsigned char *addr, |
| 3455 | __be16 vlan_id, int flags) |
| 3456 | { |
| 3457 | struct rocker *rocker = rocker_port->rocker; |
| 3458 | struct rocker_fdb_tbl_entry *fdb; |
| 3459 | struct rocker_fdb_tbl_entry *found; |
| 3460 | bool removing = (flags & ROCKER_OP_FLAG_REMOVE); |
| 3461 | unsigned long lock_flags; |
| 3462 | |
| 3463 | fdb = kzalloc(sizeof(*fdb), rocker_op_flags_gfp(flags)); |
| 3464 | if (!fdb) |
| 3465 | return -ENOMEM; |
| 3466 | |
| 3467 | fdb->learned = (flags & ROCKER_OP_FLAG_LEARNED); |
Scott Feldman | 4a6bb6d | 2015-02-25 20:15:37 -0800 | [diff] [blame] | 3468 | fdb->key.pport = rocker_port->pport; |
Scott Feldman | 6c70794 | 2014-11-28 14:34:28 +0100 | [diff] [blame] | 3469 | ether_addr_copy(fdb->key.addr, addr); |
| 3470 | fdb->key.vlan_id = vlan_id; |
| 3471 | fdb->key_crc32 = crc32(~0, &fdb->key, sizeof(fdb->key)); |
| 3472 | |
| 3473 | spin_lock_irqsave(&rocker->fdb_tbl_lock, lock_flags); |
| 3474 | |
| 3475 | found = rocker_fdb_tbl_find(rocker, fdb); |
| 3476 | |
| 3477 | if (removing && found) { |
| 3478 | kfree(fdb); |
| 3479 | hash_del(&found->entry); |
| 3480 | } else if (!removing && !found) { |
| 3481 | hash_add(rocker->fdb_tbl, &fdb->entry, fdb->key_crc32); |
| 3482 | } |
| 3483 | |
| 3484 | spin_unlock_irqrestore(&rocker->fdb_tbl_lock, lock_flags); |
| 3485 | |
| 3486 | /* Check if adding and already exists, or removing and can't find */ |
| 3487 | if (!found != !removing) { |
| 3488 | kfree(fdb); |
| 3489 | if (!found && removing) |
| 3490 | return 0; |
| 3491 | /* Refreshing existing to update aging timers */ |
| 3492 | flags |= ROCKER_OP_FLAG_REFRESH; |
| 3493 | } |
| 3494 | |
| 3495 | return rocker_port_fdb_learn(rocker_port, flags, addr, vlan_id); |
| 3496 | } |
| 3497 | |
| 3498 | static int rocker_port_fdb_flush(struct rocker_port *rocker_port) |
| 3499 | { |
| 3500 | struct rocker *rocker = rocker_port->rocker; |
| 3501 | struct rocker_fdb_tbl_entry *found; |
| 3502 | unsigned long lock_flags; |
| 3503 | int flags = ROCKER_OP_FLAG_NOWAIT | ROCKER_OP_FLAG_REMOVE; |
| 3504 | struct hlist_node *tmp; |
| 3505 | int bkt; |
| 3506 | int err = 0; |
| 3507 | |
| 3508 | if (rocker_port->stp_state == BR_STATE_LEARNING || |
| 3509 | rocker_port->stp_state == BR_STATE_FORWARDING) |
| 3510 | return 0; |
| 3511 | |
| 3512 | spin_lock_irqsave(&rocker->fdb_tbl_lock, lock_flags); |
| 3513 | |
| 3514 | hash_for_each_safe(rocker->fdb_tbl, bkt, tmp, found, entry) { |
Scott Feldman | 4a6bb6d | 2015-02-25 20:15:37 -0800 | [diff] [blame] | 3515 | if (found->key.pport != rocker_port->pport) |
Scott Feldman | 6c70794 | 2014-11-28 14:34:28 +0100 | [diff] [blame] | 3516 | continue; |
| 3517 | if (!found->learned) |
| 3518 | continue; |
| 3519 | err = rocker_port_fdb_learn(rocker_port, flags, |
| 3520 | found->key.addr, |
| 3521 | found->key.vlan_id); |
| 3522 | if (err) |
| 3523 | goto err_out; |
| 3524 | hash_del(&found->entry); |
| 3525 | } |
| 3526 | |
| 3527 | err_out: |
| 3528 | spin_unlock_irqrestore(&rocker->fdb_tbl_lock, lock_flags); |
| 3529 | |
| 3530 | return err; |
| 3531 | } |
| 3532 | |
Scott Feldman | 9f6bbf7 | 2014-11-28 14:34:27 +0100 | [diff] [blame] | 3533 | static int rocker_port_router_mac(struct rocker_port *rocker_port, |
| 3534 | int flags, __be16 vlan_id) |
| 3535 | { |
Scott Feldman | 4a6bb6d | 2015-02-25 20:15:37 -0800 | [diff] [blame] | 3536 | u32 in_pport_mask = 0xffffffff; |
Scott Feldman | 9f6bbf7 | 2014-11-28 14:34:27 +0100 | [diff] [blame] | 3537 | __be16 eth_type; |
| 3538 | const u8 *dst_mac_mask = ff_mac; |
| 3539 | __be16 vlan_id_mask = htons(0xffff); |
| 3540 | bool copy_to_cpu = false; |
| 3541 | int err; |
| 3542 | |
| 3543 | if (ntohs(vlan_id) == 0) |
| 3544 | vlan_id = rocker_port->internal_vlan_id; |
| 3545 | |
| 3546 | eth_type = htons(ETH_P_IP); |
| 3547 | err = rocker_flow_tbl_term_mac(rocker_port, |
Scott Feldman | 4a6bb6d | 2015-02-25 20:15:37 -0800 | [diff] [blame] | 3548 | rocker_port->pport, in_pport_mask, |
Scott Feldman | 9f6bbf7 | 2014-11-28 14:34:27 +0100 | [diff] [blame] | 3549 | eth_type, rocker_port->dev->dev_addr, |
| 3550 | dst_mac_mask, vlan_id, vlan_id_mask, |
| 3551 | copy_to_cpu, flags); |
| 3552 | if (err) |
| 3553 | return err; |
| 3554 | |
| 3555 | eth_type = htons(ETH_P_IPV6); |
| 3556 | err = rocker_flow_tbl_term_mac(rocker_port, |
Scott Feldman | 4a6bb6d | 2015-02-25 20:15:37 -0800 | [diff] [blame] | 3557 | rocker_port->pport, in_pport_mask, |
Scott Feldman | 9f6bbf7 | 2014-11-28 14:34:27 +0100 | [diff] [blame] | 3558 | eth_type, rocker_port->dev->dev_addr, |
| 3559 | dst_mac_mask, vlan_id, vlan_id_mask, |
| 3560 | copy_to_cpu, flags); |
| 3561 | |
| 3562 | return err; |
| 3563 | } |
| 3564 | |
Scott Feldman | 6c70794 | 2014-11-28 14:34:28 +0100 | [diff] [blame] | 3565 | static int rocker_port_fwding(struct rocker_port *rocker_port) |
| 3566 | { |
| 3567 | bool pop_vlan; |
Scott Feldman | 4a6bb6d | 2015-02-25 20:15:37 -0800 | [diff] [blame] | 3568 | u32 out_pport; |
Scott Feldman | 6c70794 | 2014-11-28 14:34:28 +0100 | [diff] [blame] | 3569 | __be16 vlan_id; |
| 3570 | u16 vid; |
| 3571 | int flags = ROCKER_OP_FLAG_NOWAIT; |
| 3572 | int err; |
| 3573 | |
| 3574 | /* Port will be forwarding-enabled if its STP state is LEARNING |
| 3575 | * or FORWARDING. Traffic from CPU can still egress, regardless of |
| 3576 | * port STP state. Use L2 interface group on port VLANs as a way |
| 3577 | * to toggle port forwarding: if forwarding is disabled, L2 |
| 3578 | * interface group will not exist. |
| 3579 | */ |
| 3580 | |
| 3581 | if (rocker_port->stp_state != BR_STATE_LEARNING && |
| 3582 | rocker_port->stp_state != BR_STATE_FORWARDING) |
| 3583 | flags |= ROCKER_OP_FLAG_REMOVE; |
| 3584 | |
Scott Feldman | 4a6bb6d | 2015-02-25 20:15:37 -0800 | [diff] [blame] | 3585 | out_pport = rocker_port->pport; |
Scott Feldman | 6c70794 | 2014-11-28 14:34:28 +0100 | [diff] [blame] | 3586 | for (vid = 1; vid < VLAN_N_VID; vid++) { |
| 3587 | if (!test_bit(vid, rocker_port->vlan_bitmap)) |
| 3588 | continue; |
| 3589 | vlan_id = htons(vid); |
| 3590 | pop_vlan = rocker_vlan_id_is_internal(vlan_id); |
| 3591 | err = rocker_group_l2_interface(rocker_port, flags, |
Scott Feldman | 4a6bb6d | 2015-02-25 20:15:37 -0800 | [diff] [blame] | 3592 | vlan_id, out_pport, |
Scott Feldman | 6c70794 | 2014-11-28 14:34:28 +0100 | [diff] [blame] | 3593 | pop_vlan); |
| 3594 | if (err) { |
| 3595 | netdev_err(rocker_port->dev, |
Scott Feldman | 4a6bb6d | 2015-02-25 20:15:37 -0800 | [diff] [blame] | 3596 | "Error (%d) port VLAN l2 group for pport %d\n", |
| 3597 | err, out_pport); |
Scott Feldman | 6c70794 | 2014-11-28 14:34:28 +0100 | [diff] [blame] | 3598 | return err; |
| 3599 | } |
| 3600 | } |
| 3601 | |
| 3602 | return 0; |
| 3603 | } |
| 3604 | |
| 3605 | static int rocker_port_stp_update(struct rocker_port *rocker_port, u8 state) |
| 3606 | { |
| 3607 | bool want[ROCKER_CTRL_MAX] = { 0, }; |
| 3608 | int flags; |
| 3609 | int err; |
| 3610 | int i; |
| 3611 | |
| 3612 | if (rocker_port->stp_state == state) |
| 3613 | return 0; |
| 3614 | |
| 3615 | rocker_port->stp_state = state; |
| 3616 | |
| 3617 | switch (state) { |
| 3618 | case BR_STATE_DISABLED: |
| 3619 | /* port is completely disabled */ |
| 3620 | break; |
| 3621 | case BR_STATE_LISTENING: |
| 3622 | case BR_STATE_BLOCKING: |
| 3623 | want[ROCKER_CTRL_LINK_LOCAL_MCAST] = true; |
| 3624 | break; |
| 3625 | case BR_STATE_LEARNING: |
| 3626 | case BR_STATE_FORWARDING: |
| 3627 | want[ROCKER_CTRL_LINK_LOCAL_MCAST] = true; |
| 3628 | want[ROCKER_CTRL_IPV4_MCAST] = true; |
| 3629 | want[ROCKER_CTRL_IPV6_MCAST] = true; |
| 3630 | if (rocker_port_is_bridged(rocker_port)) |
| 3631 | want[ROCKER_CTRL_DFLT_BRIDGING] = true; |
| 3632 | else |
| 3633 | want[ROCKER_CTRL_LOCAL_ARP] = true; |
| 3634 | break; |
| 3635 | } |
| 3636 | |
| 3637 | for (i = 0; i < ROCKER_CTRL_MAX; i++) { |
| 3638 | if (want[i] != rocker_port->ctrls[i]) { |
| 3639 | flags = ROCKER_OP_FLAG_NOWAIT | |
| 3640 | (want[i] ? 0 : ROCKER_OP_FLAG_REMOVE); |
| 3641 | err = rocker_port_ctrl(rocker_port, flags, |
| 3642 | &rocker_ctrls[i]); |
| 3643 | if (err) |
| 3644 | return err; |
| 3645 | rocker_port->ctrls[i] = want[i]; |
| 3646 | } |
| 3647 | } |
| 3648 | |
| 3649 | err = rocker_port_fdb_flush(rocker_port); |
| 3650 | if (err) |
| 3651 | return err; |
| 3652 | |
| 3653 | return rocker_port_fwding(rocker_port); |
| 3654 | } |
| 3655 | |
Scott Feldman | e47172a | 2015-02-25 20:15:38 -0800 | [diff] [blame] | 3656 | static int rocker_port_fwd_enable(struct rocker_port *rocker_port) |
| 3657 | { |
| 3658 | if (rocker_port_is_bridged(rocker_port)) |
| 3659 | /* bridge STP will enable port */ |
| 3660 | return 0; |
| 3661 | |
| 3662 | /* port is not bridged, so simulate going to FORWARDING state */ |
| 3663 | return rocker_port_stp_update(rocker_port, BR_STATE_FORWARDING); |
| 3664 | } |
| 3665 | |
| 3666 | static int rocker_port_fwd_disable(struct rocker_port *rocker_port) |
| 3667 | { |
| 3668 | if (rocker_port_is_bridged(rocker_port)) |
| 3669 | /* bridge STP will disable port */ |
| 3670 | return 0; |
| 3671 | |
| 3672 | /* port is not bridged, so simulate going to DISABLED state */ |
| 3673 | return rocker_port_stp_update(rocker_port, BR_STATE_DISABLED); |
| 3674 | } |
| 3675 | |
Scott Feldman | 9f6bbf7 | 2014-11-28 14:34:27 +0100 | [diff] [blame] | 3676 | static struct rocker_internal_vlan_tbl_entry * |
| 3677 | rocker_internal_vlan_tbl_find(struct rocker *rocker, int ifindex) |
| 3678 | { |
| 3679 | struct rocker_internal_vlan_tbl_entry *found; |
| 3680 | |
| 3681 | hash_for_each_possible(rocker->internal_vlan_tbl, found, |
| 3682 | entry, ifindex) { |
| 3683 | if (found->ifindex == ifindex) |
| 3684 | return found; |
| 3685 | } |
| 3686 | |
| 3687 | return NULL; |
| 3688 | } |
| 3689 | |
| 3690 | static __be16 rocker_port_internal_vlan_id_get(struct rocker_port *rocker_port, |
| 3691 | int ifindex) |
| 3692 | { |
| 3693 | struct rocker *rocker = rocker_port->rocker; |
| 3694 | struct rocker_internal_vlan_tbl_entry *entry; |
| 3695 | struct rocker_internal_vlan_tbl_entry *found; |
| 3696 | unsigned long lock_flags; |
| 3697 | int i; |
| 3698 | |
| 3699 | entry = kzalloc(sizeof(*entry), GFP_KERNEL); |
| 3700 | if (!entry) |
| 3701 | return 0; |
| 3702 | |
| 3703 | entry->ifindex = ifindex; |
| 3704 | |
| 3705 | spin_lock_irqsave(&rocker->internal_vlan_tbl_lock, lock_flags); |
| 3706 | |
| 3707 | found = rocker_internal_vlan_tbl_find(rocker, ifindex); |
| 3708 | if (found) { |
| 3709 | kfree(entry); |
| 3710 | goto found; |
| 3711 | } |
| 3712 | |
| 3713 | found = entry; |
| 3714 | hash_add(rocker->internal_vlan_tbl, &found->entry, found->ifindex); |
| 3715 | |
| 3716 | for (i = 0; i < ROCKER_N_INTERNAL_VLANS; i++) { |
| 3717 | if (test_and_set_bit(i, rocker->internal_vlan_bitmap)) |
| 3718 | continue; |
| 3719 | found->vlan_id = htons(ROCKER_INTERNAL_VLAN_ID_BASE + i); |
| 3720 | goto found; |
| 3721 | } |
| 3722 | |
| 3723 | netdev_err(rocker_port->dev, "Out of internal VLAN IDs\n"); |
| 3724 | |
| 3725 | found: |
| 3726 | found->ref_count++; |
| 3727 | spin_unlock_irqrestore(&rocker->internal_vlan_tbl_lock, lock_flags); |
| 3728 | |
| 3729 | return found->vlan_id; |
| 3730 | } |
| 3731 | |
| 3732 | static void rocker_port_internal_vlan_id_put(struct rocker_port *rocker_port, |
| 3733 | int ifindex) |
| 3734 | { |
| 3735 | struct rocker *rocker = rocker_port->rocker; |
| 3736 | struct rocker_internal_vlan_tbl_entry *found; |
| 3737 | unsigned long lock_flags; |
| 3738 | unsigned long bit; |
| 3739 | |
| 3740 | spin_lock_irqsave(&rocker->internal_vlan_tbl_lock, lock_flags); |
| 3741 | |
| 3742 | found = rocker_internal_vlan_tbl_find(rocker, ifindex); |
| 3743 | if (!found) { |
| 3744 | netdev_err(rocker_port->dev, |
| 3745 | "ifindex (%d) not found in internal VLAN tbl\n", |
| 3746 | ifindex); |
| 3747 | goto not_found; |
| 3748 | } |
| 3749 | |
| 3750 | if (--found->ref_count <= 0) { |
| 3751 | bit = ntohs(found->vlan_id) - ROCKER_INTERNAL_VLAN_ID_BASE; |
| 3752 | clear_bit(bit, rocker->internal_vlan_bitmap); |
| 3753 | hash_del(&found->entry); |
| 3754 | kfree(found); |
| 3755 | } |
| 3756 | |
| 3757 | not_found: |
| 3758 | spin_unlock_irqrestore(&rocker->internal_vlan_tbl_lock, lock_flags); |
| 3759 | } |
| 3760 | |
Scott Feldman | c1beeef | 2015-03-05 21:21:20 -0800 | [diff] [blame] | 3761 | static int rocker_port_fib_ipv4(struct rocker_port *rocker_port, __be32 dst, |
| 3762 | int dst_len, struct fib_info *fi, u32 tb_id, |
| 3763 | int flags) |
| 3764 | { |
| 3765 | struct fib_nh *nh; |
| 3766 | __be16 eth_type = htons(ETH_P_IP); |
| 3767 | __be32 dst_mask = inet_make_mask(dst_len); |
| 3768 | __be16 internal_vlan_id = rocker_port->internal_vlan_id; |
| 3769 | u32 priority = fi->fib_priority; |
| 3770 | enum rocker_of_dpa_table_id goto_tbl = |
| 3771 | ROCKER_OF_DPA_TABLE_ID_ACL_POLICY; |
| 3772 | u32 group_id; |
| 3773 | bool nh_on_port; |
| 3774 | bool has_gw; |
| 3775 | u32 index; |
| 3776 | int err; |
| 3777 | |
| 3778 | /* XXX support ECMP */ |
| 3779 | |
| 3780 | nh = fi->fib_nh; |
| 3781 | nh_on_port = (fi->fib_dev == rocker_port->dev); |
| 3782 | has_gw = !!nh->nh_gw; |
| 3783 | |
| 3784 | if (has_gw && nh_on_port) { |
| 3785 | err = rocker_port_ipv4_nh(rocker_port, flags, |
| 3786 | nh->nh_gw, &index); |
| 3787 | if (err) |
| 3788 | return err; |
| 3789 | |
| 3790 | group_id = ROCKER_GROUP_L3_UNICAST(index); |
| 3791 | } else { |
| 3792 | /* Send to CPU for processing */ |
| 3793 | group_id = ROCKER_GROUP_L2_INTERFACE(internal_vlan_id, 0); |
| 3794 | } |
| 3795 | |
| 3796 | err = rocker_flow_tbl_ucast4_routing(rocker_port, eth_type, dst, |
| 3797 | dst_mask, priority, goto_tbl, |
| 3798 | group_id, flags); |
| 3799 | if (err) |
| 3800 | netdev_err(rocker_port->dev, "Error (%d) IPv4 route %pI4\n", |
| 3801 | err, &dst); |
| 3802 | |
| 3803 | return err; |
| 3804 | } |
| 3805 | |
Jiri Pirko | 4b8ac96 | 2014-11-28 14:34:26 +0100 | [diff] [blame] | 3806 | /***************** |
| 3807 | * Net device ops |
| 3808 | *****************/ |
| 3809 | |
| 3810 | static int rocker_port_open(struct net_device *dev) |
| 3811 | { |
| 3812 | struct rocker_port *rocker_port = netdev_priv(dev); |
| 3813 | int err; |
| 3814 | |
| 3815 | err = rocker_port_dma_rings_init(rocker_port); |
| 3816 | if (err) |
| 3817 | return err; |
| 3818 | |
| 3819 | err = request_irq(rocker_msix_tx_vector(rocker_port), |
| 3820 | rocker_tx_irq_handler, 0, |
| 3821 | rocker_driver_name, rocker_port); |
| 3822 | if (err) { |
| 3823 | netdev_err(rocker_port->dev, "cannot assign tx irq\n"); |
| 3824 | goto err_request_tx_irq; |
| 3825 | } |
| 3826 | |
| 3827 | err = request_irq(rocker_msix_rx_vector(rocker_port), |
| 3828 | rocker_rx_irq_handler, 0, |
| 3829 | rocker_driver_name, rocker_port); |
| 3830 | if (err) { |
| 3831 | netdev_err(rocker_port->dev, "cannot assign rx irq\n"); |
| 3832 | goto err_request_rx_irq; |
| 3833 | } |
| 3834 | |
Scott Feldman | e47172a | 2015-02-25 20:15:38 -0800 | [diff] [blame] | 3835 | err = rocker_port_fwd_enable(rocker_port); |
Scott Feldman | 6c70794 | 2014-11-28 14:34:28 +0100 | [diff] [blame] | 3836 | if (err) |
Scott Feldman | e47172a | 2015-02-25 20:15:38 -0800 | [diff] [blame] | 3837 | goto err_fwd_enable; |
Scott Feldman | 6c70794 | 2014-11-28 14:34:28 +0100 | [diff] [blame] | 3838 | |
Jiri Pirko | 4b8ac96 | 2014-11-28 14:34:26 +0100 | [diff] [blame] | 3839 | napi_enable(&rocker_port->napi_tx); |
| 3840 | napi_enable(&rocker_port->napi_rx); |
| 3841 | rocker_port_set_enable(rocker_port, true); |
| 3842 | netif_start_queue(dev); |
| 3843 | return 0; |
| 3844 | |
Scott Feldman | e47172a | 2015-02-25 20:15:38 -0800 | [diff] [blame] | 3845 | err_fwd_enable: |
Scott Feldman | 6c70794 | 2014-11-28 14:34:28 +0100 | [diff] [blame] | 3846 | free_irq(rocker_msix_rx_vector(rocker_port), rocker_port); |
Jiri Pirko | 4b8ac96 | 2014-11-28 14:34:26 +0100 | [diff] [blame] | 3847 | err_request_rx_irq: |
| 3848 | free_irq(rocker_msix_tx_vector(rocker_port), rocker_port); |
| 3849 | err_request_tx_irq: |
| 3850 | rocker_port_dma_rings_fini(rocker_port); |
| 3851 | return err; |
| 3852 | } |
| 3853 | |
| 3854 | static int rocker_port_stop(struct net_device *dev) |
| 3855 | { |
| 3856 | struct rocker_port *rocker_port = netdev_priv(dev); |
| 3857 | |
| 3858 | netif_stop_queue(dev); |
| 3859 | rocker_port_set_enable(rocker_port, false); |
| 3860 | napi_disable(&rocker_port->napi_rx); |
| 3861 | napi_disable(&rocker_port->napi_tx); |
Scott Feldman | e47172a | 2015-02-25 20:15:38 -0800 | [diff] [blame] | 3862 | rocker_port_fwd_disable(rocker_port); |
Jiri Pirko | 4b8ac96 | 2014-11-28 14:34:26 +0100 | [diff] [blame] | 3863 | free_irq(rocker_msix_rx_vector(rocker_port), rocker_port); |
| 3864 | free_irq(rocker_msix_tx_vector(rocker_port), rocker_port); |
| 3865 | rocker_port_dma_rings_fini(rocker_port); |
| 3866 | |
| 3867 | return 0; |
| 3868 | } |
| 3869 | |
| 3870 | static void rocker_tx_desc_frags_unmap(struct rocker_port *rocker_port, |
| 3871 | struct rocker_desc_info *desc_info) |
| 3872 | { |
| 3873 | struct rocker *rocker = rocker_port->rocker; |
| 3874 | struct pci_dev *pdev = rocker->pdev; |
| 3875 | struct rocker_tlv *attrs[ROCKER_TLV_TX_MAX + 1]; |
| 3876 | struct rocker_tlv *attr; |
| 3877 | int rem; |
| 3878 | |
| 3879 | rocker_tlv_parse_desc(attrs, ROCKER_TLV_TX_MAX, desc_info); |
| 3880 | if (!attrs[ROCKER_TLV_TX_FRAGS]) |
| 3881 | return; |
| 3882 | rocker_tlv_for_each_nested(attr, attrs[ROCKER_TLV_TX_FRAGS], rem) { |
| 3883 | struct rocker_tlv *frag_attrs[ROCKER_TLV_TX_FRAG_ATTR_MAX + 1]; |
| 3884 | dma_addr_t dma_handle; |
| 3885 | size_t len; |
| 3886 | |
| 3887 | if (rocker_tlv_type(attr) != ROCKER_TLV_TX_FRAG) |
| 3888 | continue; |
| 3889 | rocker_tlv_parse_nested(frag_attrs, ROCKER_TLV_TX_FRAG_ATTR_MAX, |
| 3890 | attr); |
| 3891 | if (!frag_attrs[ROCKER_TLV_TX_FRAG_ATTR_ADDR] || |
| 3892 | !frag_attrs[ROCKER_TLV_TX_FRAG_ATTR_LEN]) |
| 3893 | continue; |
| 3894 | dma_handle = rocker_tlv_get_u64(frag_attrs[ROCKER_TLV_TX_FRAG_ATTR_ADDR]); |
| 3895 | len = rocker_tlv_get_u16(frag_attrs[ROCKER_TLV_TX_FRAG_ATTR_LEN]); |
| 3896 | pci_unmap_single(pdev, dma_handle, len, DMA_TO_DEVICE); |
| 3897 | } |
| 3898 | } |
| 3899 | |
| 3900 | static int rocker_tx_desc_frag_map_put(struct rocker_port *rocker_port, |
| 3901 | struct rocker_desc_info *desc_info, |
| 3902 | char *buf, size_t buf_len) |
| 3903 | { |
| 3904 | struct rocker *rocker = rocker_port->rocker; |
| 3905 | struct pci_dev *pdev = rocker->pdev; |
| 3906 | dma_addr_t dma_handle; |
| 3907 | struct rocker_tlv *frag; |
| 3908 | |
| 3909 | dma_handle = pci_map_single(pdev, buf, buf_len, DMA_TO_DEVICE); |
| 3910 | if (unlikely(pci_dma_mapping_error(pdev, dma_handle))) { |
| 3911 | if (net_ratelimit()) |
| 3912 | netdev_err(rocker_port->dev, "failed to dma map tx frag\n"); |
| 3913 | return -EIO; |
| 3914 | } |
| 3915 | frag = rocker_tlv_nest_start(desc_info, ROCKER_TLV_TX_FRAG); |
| 3916 | if (!frag) |
| 3917 | goto unmap_frag; |
| 3918 | if (rocker_tlv_put_u64(desc_info, ROCKER_TLV_TX_FRAG_ATTR_ADDR, |
| 3919 | dma_handle)) |
| 3920 | goto nest_cancel; |
| 3921 | if (rocker_tlv_put_u16(desc_info, ROCKER_TLV_TX_FRAG_ATTR_LEN, |
| 3922 | buf_len)) |
| 3923 | goto nest_cancel; |
| 3924 | rocker_tlv_nest_end(desc_info, frag); |
| 3925 | return 0; |
| 3926 | |
| 3927 | nest_cancel: |
| 3928 | rocker_tlv_nest_cancel(desc_info, frag); |
| 3929 | unmap_frag: |
| 3930 | pci_unmap_single(pdev, dma_handle, buf_len, DMA_TO_DEVICE); |
| 3931 | return -EMSGSIZE; |
| 3932 | } |
| 3933 | |
| 3934 | static netdev_tx_t rocker_port_xmit(struct sk_buff *skb, struct net_device *dev) |
| 3935 | { |
| 3936 | struct rocker_port *rocker_port = netdev_priv(dev); |
| 3937 | struct rocker *rocker = rocker_port->rocker; |
| 3938 | struct rocker_desc_info *desc_info; |
| 3939 | struct rocker_tlv *frags; |
| 3940 | int i; |
| 3941 | int err; |
| 3942 | |
| 3943 | desc_info = rocker_desc_head_get(&rocker_port->tx_ring); |
| 3944 | if (unlikely(!desc_info)) { |
| 3945 | if (net_ratelimit()) |
| 3946 | netdev_err(dev, "tx ring full when queue awake\n"); |
| 3947 | return NETDEV_TX_BUSY; |
| 3948 | } |
| 3949 | |
| 3950 | rocker_desc_cookie_ptr_set(desc_info, skb); |
| 3951 | |
| 3952 | frags = rocker_tlv_nest_start(desc_info, ROCKER_TLV_TX_FRAGS); |
| 3953 | if (!frags) |
| 3954 | goto out; |
| 3955 | err = rocker_tx_desc_frag_map_put(rocker_port, desc_info, |
| 3956 | skb->data, skb_headlen(skb)); |
| 3957 | if (err) |
| 3958 | goto nest_cancel; |
| 3959 | if (skb_shinfo(skb)->nr_frags > ROCKER_TX_FRAGS_MAX) |
| 3960 | goto nest_cancel; |
| 3961 | |
| 3962 | for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { |
| 3963 | const skb_frag_t *frag = &skb_shinfo(skb)->frags[i]; |
| 3964 | |
| 3965 | err = rocker_tx_desc_frag_map_put(rocker_port, desc_info, |
| 3966 | skb_frag_address(frag), |
| 3967 | skb_frag_size(frag)); |
| 3968 | if (err) |
| 3969 | goto unmap_frags; |
| 3970 | } |
| 3971 | rocker_tlv_nest_end(desc_info, frags); |
| 3972 | |
| 3973 | rocker_desc_gen_clear(desc_info); |
| 3974 | rocker_desc_head_set(rocker, &rocker_port->tx_ring, desc_info); |
| 3975 | |
| 3976 | desc_info = rocker_desc_head_get(&rocker_port->tx_ring); |
| 3977 | if (!desc_info) |
| 3978 | netif_stop_queue(dev); |
| 3979 | |
| 3980 | return NETDEV_TX_OK; |
| 3981 | |
| 3982 | unmap_frags: |
| 3983 | rocker_tx_desc_frags_unmap(rocker_port, desc_info); |
| 3984 | nest_cancel: |
| 3985 | rocker_tlv_nest_cancel(desc_info, frags); |
| 3986 | out: |
| 3987 | dev_kfree_skb(skb); |
David Ahern | f2bbca5 | 2015-01-16 14:22:29 -0700 | [diff] [blame] | 3988 | dev->stats.tx_dropped++; |
| 3989 | |
Jiri Pirko | 4b8ac96 | 2014-11-28 14:34:26 +0100 | [diff] [blame] | 3990 | return NETDEV_TX_OK; |
| 3991 | } |
| 3992 | |
| 3993 | static int rocker_port_set_mac_address(struct net_device *dev, void *p) |
| 3994 | { |
| 3995 | struct sockaddr *addr = p; |
| 3996 | struct rocker_port *rocker_port = netdev_priv(dev); |
| 3997 | int err; |
| 3998 | |
| 3999 | if (!is_valid_ether_addr(addr->sa_data)) |
| 4000 | return -EADDRNOTAVAIL; |
| 4001 | |
| 4002 | err = rocker_cmd_set_port_settings_macaddr(rocker_port, addr->sa_data); |
| 4003 | if (err) |
| 4004 | return err; |
| 4005 | memcpy(dev->dev_addr, addr->sa_data, dev->addr_len); |
| 4006 | return 0; |
| 4007 | } |
| 4008 | |
Scott Feldman | 6c70794 | 2014-11-28 14:34:28 +0100 | [diff] [blame] | 4009 | static int rocker_port_vlan_rx_add_vid(struct net_device *dev, |
| 4010 | __be16 proto, u16 vid) |
| 4011 | { |
| 4012 | struct rocker_port *rocker_port = netdev_priv(dev); |
| 4013 | int err; |
| 4014 | |
| 4015 | err = rocker_port_vlan(rocker_port, 0, vid); |
| 4016 | if (err) |
| 4017 | return err; |
| 4018 | |
| 4019 | return rocker_port_router_mac(rocker_port, 0, htons(vid)); |
| 4020 | } |
| 4021 | |
| 4022 | static int rocker_port_vlan_rx_kill_vid(struct net_device *dev, |
| 4023 | __be16 proto, u16 vid) |
| 4024 | { |
| 4025 | struct rocker_port *rocker_port = netdev_priv(dev); |
| 4026 | int err; |
| 4027 | |
| 4028 | err = rocker_port_router_mac(rocker_port, ROCKER_OP_FLAG_REMOVE, |
| 4029 | htons(vid)); |
| 4030 | if (err) |
| 4031 | return err; |
| 4032 | |
| 4033 | return rocker_port_vlan(rocker_port, ROCKER_OP_FLAG_REMOVE, vid); |
| 4034 | } |
| 4035 | |
| 4036 | static int rocker_port_fdb_add(struct ndmsg *ndm, struct nlattr *tb[], |
| 4037 | struct net_device *dev, |
| 4038 | const unsigned char *addr, u16 vid, |
| 4039 | u16 nlm_flags) |
| 4040 | { |
| 4041 | struct rocker_port *rocker_port = netdev_priv(dev); |
| 4042 | __be16 vlan_id = rocker_port_vid_to_vlan(rocker_port, vid, NULL); |
| 4043 | int flags = 0; |
| 4044 | |
| 4045 | if (!rocker_port_is_bridged(rocker_port)) |
| 4046 | return -EINVAL; |
| 4047 | |
| 4048 | return rocker_port_fdb(rocker_port, addr, vlan_id, flags); |
| 4049 | } |
| 4050 | |
| 4051 | static int rocker_port_fdb_del(struct ndmsg *ndm, struct nlattr *tb[], |
| 4052 | struct net_device *dev, |
| 4053 | const unsigned char *addr, u16 vid) |
| 4054 | { |
| 4055 | struct rocker_port *rocker_port = netdev_priv(dev); |
| 4056 | __be16 vlan_id = rocker_port_vid_to_vlan(rocker_port, vid, NULL); |
| 4057 | int flags = ROCKER_OP_FLAG_REMOVE; |
| 4058 | |
| 4059 | if (!rocker_port_is_bridged(rocker_port)) |
| 4060 | return -EINVAL; |
| 4061 | |
| 4062 | return rocker_port_fdb(rocker_port, addr, vlan_id, flags); |
| 4063 | } |
| 4064 | |
Jiri Pirko | ce76ca6 | 2014-11-28 14:34:29 +0100 | [diff] [blame] | 4065 | static int rocker_fdb_fill_info(struct sk_buff *skb, |
| 4066 | struct rocker_port *rocker_port, |
| 4067 | const unsigned char *addr, u16 vid, |
| 4068 | u32 portid, u32 seq, int type, |
| 4069 | unsigned int flags) |
| 4070 | { |
| 4071 | struct nlmsghdr *nlh; |
| 4072 | struct ndmsg *ndm; |
| 4073 | |
| 4074 | nlh = nlmsg_put(skb, portid, seq, type, sizeof(*ndm), flags); |
| 4075 | if (!nlh) |
| 4076 | return -EMSGSIZE; |
| 4077 | |
| 4078 | ndm = nlmsg_data(nlh); |
| 4079 | ndm->ndm_family = AF_BRIDGE; |
| 4080 | ndm->ndm_pad1 = 0; |
| 4081 | ndm->ndm_pad2 = 0; |
| 4082 | ndm->ndm_flags = NTF_SELF; |
| 4083 | ndm->ndm_type = 0; |
| 4084 | ndm->ndm_ifindex = rocker_port->dev->ifindex; |
| 4085 | ndm->ndm_state = NUD_REACHABLE; |
| 4086 | |
| 4087 | if (nla_put(skb, NDA_LLADDR, ETH_ALEN, addr)) |
| 4088 | goto nla_put_failure; |
| 4089 | |
| 4090 | if (vid && nla_put_u16(skb, NDA_VLAN, vid)) |
| 4091 | goto nla_put_failure; |
| 4092 | |
Johannes Berg | 053c095 | 2015-01-16 22:09:00 +0100 | [diff] [blame] | 4093 | nlmsg_end(skb, nlh); |
| 4094 | return 0; |
Jiri Pirko | ce76ca6 | 2014-11-28 14:34:29 +0100 | [diff] [blame] | 4095 | |
| 4096 | nla_put_failure: |
| 4097 | nlmsg_cancel(skb, nlh); |
| 4098 | return -EMSGSIZE; |
| 4099 | } |
| 4100 | |
| 4101 | static int rocker_port_fdb_dump(struct sk_buff *skb, |
| 4102 | struct netlink_callback *cb, |
| 4103 | struct net_device *dev, |
| 4104 | struct net_device *filter_dev, |
| 4105 | int idx) |
| 4106 | { |
| 4107 | struct rocker_port *rocker_port = netdev_priv(dev); |
| 4108 | struct rocker *rocker = rocker_port->rocker; |
| 4109 | struct rocker_fdb_tbl_entry *found; |
| 4110 | struct hlist_node *tmp; |
| 4111 | int bkt; |
| 4112 | unsigned long lock_flags; |
| 4113 | const unsigned char *addr; |
| 4114 | u16 vid; |
| 4115 | int err; |
| 4116 | |
| 4117 | spin_lock_irqsave(&rocker->fdb_tbl_lock, lock_flags); |
| 4118 | hash_for_each_safe(rocker->fdb_tbl, bkt, tmp, found, entry) { |
Scott Feldman | 4a6bb6d | 2015-02-25 20:15:37 -0800 | [diff] [blame] | 4119 | if (found->key.pport != rocker_port->pport) |
Jiri Pirko | ce76ca6 | 2014-11-28 14:34:29 +0100 | [diff] [blame] | 4120 | continue; |
| 4121 | if (idx < cb->args[0]) |
| 4122 | goto skip; |
| 4123 | addr = found->key.addr; |
| 4124 | vid = rocker_port_vlan_to_vid(rocker_port, found->key.vlan_id); |
| 4125 | err = rocker_fdb_fill_info(skb, rocker_port, addr, vid, |
| 4126 | NETLINK_CB(cb->skb).portid, |
| 4127 | cb->nlh->nlmsg_seq, |
| 4128 | RTM_NEWNEIGH, NLM_F_MULTI); |
| 4129 | if (err < 0) |
| 4130 | break; |
| 4131 | skip: |
| 4132 | ++idx; |
| 4133 | } |
| 4134 | spin_unlock_irqrestore(&rocker->fdb_tbl_lock, lock_flags); |
| 4135 | return idx; |
| 4136 | } |
| 4137 | |
Scott Feldman | 5111f80 | 2014-11-28 14:34:30 +0100 | [diff] [blame] | 4138 | static int rocker_port_bridge_setlink(struct net_device *dev, |
Roopa Prabhu | add511b | 2015-01-29 22:40:12 -0800 | [diff] [blame] | 4139 | struct nlmsghdr *nlh, u16 flags) |
Scott Feldman | 5111f80 | 2014-11-28 14:34:30 +0100 | [diff] [blame] | 4140 | { |
| 4141 | struct rocker_port *rocker_port = netdev_priv(dev); |
| 4142 | struct nlattr *protinfo; |
Scott Feldman | 5111f80 | 2014-11-28 14:34:30 +0100 | [diff] [blame] | 4143 | struct nlattr *attr; |
Scott Feldman | 5111f80 | 2014-11-28 14:34:30 +0100 | [diff] [blame] | 4144 | int err; |
| 4145 | |
| 4146 | protinfo = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), |
| 4147 | IFLA_PROTINFO); |
Scott Feldman | 5111f80 | 2014-11-28 14:34:30 +0100 | [diff] [blame] | 4148 | if (protinfo) { |
| 4149 | attr = nla_find_nested(protinfo, IFLA_BRPORT_LEARNING); |
| 4150 | if (attr) { |
Thomas Graf | e756058 | 2014-11-28 14:34:31 +0100 | [diff] [blame] | 4151 | if (nla_len(attr) < sizeof(u8)) |
| 4152 | return -EINVAL; |
| 4153 | |
Scott Feldman | 5111f80 | 2014-11-28 14:34:30 +0100 | [diff] [blame] | 4154 | if (nla_get_u8(attr)) |
| 4155 | rocker_port->brport_flags |= BR_LEARNING; |
| 4156 | else |
| 4157 | rocker_port->brport_flags &= ~BR_LEARNING; |
| 4158 | err = rocker_port_set_learning(rocker_port); |
| 4159 | if (err) |
| 4160 | return err; |
| 4161 | } |
| 4162 | attr = nla_find_nested(protinfo, IFLA_BRPORT_LEARNING_SYNC); |
| 4163 | if (attr) { |
Thomas Graf | e756058 | 2014-11-28 14:34:31 +0100 | [diff] [blame] | 4164 | if (nla_len(attr) < sizeof(u8)) |
| 4165 | return -EINVAL; |
| 4166 | |
Scott Feldman | 5111f80 | 2014-11-28 14:34:30 +0100 | [diff] [blame] | 4167 | if (nla_get_u8(attr)) |
| 4168 | rocker_port->brport_flags |= BR_LEARNING_SYNC; |
| 4169 | else |
| 4170 | rocker_port->brport_flags &= ~BR_LEARNING_SYNC; |
| 4171 | } |
| 4172 | } |
| 4173 | |
| 4174 | return 0; |
| 4175 | } |
| 4176 | |
| 4177 | static int rocker_port_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq, |
| 4178 | struct net_device *dev, |
Nicolas Dichtel | 46c264d | 2015-04-28 18:33:49 +0200 | [diff] [blame] | 4179 | u32 filter_mask, int nlflags) |
Scott Feldman | 5111f80 | 2014-11-28 14:34:30 +0100 | [diff] [blame] | 4180 | { |
| 4181 | struct rocker_port *rocker_port = netdev_priv(dev); |
Roopa Prabhu | 1d460b9 | 2014-12-08 14:04:20 -0800 | [diff] [blame] | 4182 | u16 mode = BRIDGE_MODE_UNDEF; |
Scott Feldman | 5111f80 | 2014-11-28 14:34:30 +0100 | [diff] [blame] | 4183 | u32 mask = BR_LEARNING | BR_LEARNING_SYNC; |
| 4184 | |
| 4185 | return ndo_dflt_bridge_getlink(skb, pid, seq, dev, mode, |
Nicolas Dichtel | 46c264d | 2015-04-28 18:33:49 +0200 | [diff] [blame] | 4186 | rocker_port->brport_flags, mask, |
| 4187 | nlflags); |
Scott Feldman | 5111f80 | 2014-11-28 14:34:30 +0100 | [diff] [blame] | 4188 | } |
| 4189 | |
David Ahern | db19170 | 2015-03-17 20:23:16 -0600 | [diff] [blame] | 4190 | static int rocker_port_get_phys_port_name(struct net_device *dev, |
| 4191 | char *buf, size_t len) |
| 4192 | { |
| 4193 | struct rocker_port *rocker_port = netdev_priv(dev); |
| 4194 | struct port_name name = { .buf = buf, .len = len }; |
| 4195 | int err; |
| 4196 | |
| 4197 | err = rocker_cmd_exec(rocker_port->rocker, rocker_port, |
| 4198 | rocker_cmd_get_port_settings_prep, NULL, |
| 4199 | rocker_cmd_get_port_settings_phys_name_proc, |
| 4200 | &name, false); |
| 4201 | |
| 4202 | return err ? -EOPNOTSUPP : 0; |
| 4203 | } |
| 4204 | |
Jiri Pirko | 4b8ac96 | 2014-11-28 14:34:26 +0100 | [diff] [blame] | 4205 | static const struct net_device_ops rocker_port_netdev_ops = { |
| 4206 | .ndo_open = rocker_port_open, |
| 4207 | .ndo_stop = rocker_port_stop, |
| 4208 | .ndo_start_xmit = rocker_port_xmit, |
| 4209 | .ndo_set_mac_address = rocker_port_set_mac_address, |
Scott Feldman | 6c70794 | 2014-11-28 14:34:28 +0100 | [diff] [blame] | 4210 | .ndo_vlan_rx_add_vid = rocker_port_vlan_rx_add_vid, |
| 4211 | .ndo_vlan_rx_kill_vid = rocker_port_vlan_rx_kill_vid, |
| 4212 | .ndo_fdb_add = rocker_port_fdb_add, |
| 4213 | .ndo_fdb_del = rocker_port_fdb_del, |
Jiri Pirko | ce76ca6 | 2014-11-28 14:34:29 +0100 | [diff] [blame] | 4214 | .ndo_fdb_dump = rocker_port_fdb_dump, |
Scott Feldman | 5111f80 | 2014-11-28 14:34:30 +0100 | [diff] [blame] | 4215 | .ndo_bridge_setlink = rocker_port_bridge_setlink, |
| 4216 | .ndo_bridge_getlink = rocker_port_bridge_getlink, |
David Ahern | db19170 | 2015-03-17 20:23:16 -0600 | [diff] [blame] | 4217 | .ndo_get_phys_port_name = rocker_port_get_phys_port_name, |
Scott Feldman | 98237d4 | 2015-03-15 21:07:15 -0700 | [diff] [blame] | 4218 | }; |
| 4219 | |
| 4220 | /******************** |
| 4221 | * swdev interface |
| 4222 | ********************/ |
| 4223 | |
| 4224 | static int rocker_port_swdev_parent_id_get(struct net_device *dev, |
| 4225 | struct netdev_phys_item_id *psid) |
| 4226 | { |
| 4227 | struct rocker_port *rocker_port = netdev_priv(dev); |
| 4228 | struct rocker *rocker = rocker_port->rocker; |
| 4229 | |
| 4230 | psid->id_len = sizeof(rocker->hw.id); |
| 4231 | memcpy(&psid->id, &rocker->hw.id, psid->id_len); |
| 4232 | return 0; |
| 4233 | } |
| 4234 | |
| 4235 | static int rocker_port_swdev_port_stp_update(struct net_device *dev, u8 state) |
| 4236 | { |
| 4237 | struct rocker_port *rocker_port = netdev_priv(dev); |
| 4238 | |
| 4239 | return rocker_port_stp_update(rocker_port, state); |
| 4240 | } |
| 4241 | |
| 4242 | static int rocker_port_swdev_fib_ipv4_add(struct net_device *dev, |
| 4243 | __be32 dst, int dst_len, |
| 4244 | struct fib_info *fi, |
| 4245 | u8 tos, u8 type, |
| 4246 | u32 nlflags, u32 tb_id) |
| 4247 | { |
| 4248 | struct rocker_port *rocker_port = netdev_priv(dev); |
| 4249 | int flags = 0; |
| 4250 | |
| 4251 | return rocker_port_fib_ipv4(rocker_port, dst, dst_len, |
| 4252 | fi, tb_id, flags); |
| 4253 | } |
| 4254 | |
| 4255 | static int rocker_port_swdev_fib_ipv4_del(struct net_device *dev, |
| 4256 | __be32 dst, int dst_len, |
| 4257 | struct fib_info *fi, |
| 4258 | u8 tos, u8 type, u32 tb_id) |
| 4259 | { |
| 4260 | struct rocker_port *rocker_port = netdev_priv(dev); |
| 4261 | int flags = ROCKER_OP_FLAG_REMOVE; |
| 4262 | |
| 4263 | return rocker_port_fib_ipv4(rocker_port, dst, dst_len, |
| 4264 | fi, tb_id, flags); |
| 4265 | } |
| 4266 | |
| 4267 | static const struct swdev_ops rocker_port_swdev_ops = { |
| 4268 | .swdev_parent_id_get = rocker_port_swdev_parent_id_get, |
| 4269 | .swdev_port_stp_update = rocker_port_swdev_port_stp_update, |
| 4270 | .swdev_fib_ipv4_add = rocker_port_swdev_fib_ipv4_add, |
| 4271 | .swdev_fib_ipv4_del = rocker_port_swdev_fib_ipv4_del, |
Jiri Pirko | 4b8ac96 | 2014-11-28 14:34:26 +0100 | [diff] [blame] | 4272 | }; |
| 4273 | |
| 4274 | /******************** |
| 4275 | * ethtool interface |
| 4276 | ********************/ |
| 4277 | |
| 4278 | static int rocker_port_get_settings(struct net_device *dev, |
| 4279 | struct ethtool_cmd *ecmd) |
| 4280 | { |
| 4281 | struct rocker_port *rocker_port = netdev_priv(dev); |
| 4282 | |
| 4283 | return rocker_cmd_get_port_settings_ethtool(rocker_port, ecmd); |
| 4284 | } |
| 4285 | |
| 4286 | static int rocker_port_set_settings(struct net_device *dev, |
| 4287 | struct ethtool_cmd *ecmd) |
| 4288 | { |
| 4289 | struct rocker_port *rocker_port = netdev_priv(dev); |
| 4290 | |
| 4291 | return rocker_cmd_set_port_settings_ethtool(rocker_port, ecmd); |
| 4292 | } |
| 4293 | |
| 4294 | static void rocker_port_get_drvinfo(struct net_device *dev, |
| 4295 | struct ethtool_drvinfo *drvinfo) |
| 4296 | { |
| 4297 | strlcpy(drvinfo->driver, rocker_driver_name, sizeof(drvinfo->driver)); |
| 4298 | strlcpy(drvinfo->version, UTS_RELEASE, sizeof(drvinfo->version)); |
| 4299 | } |
| 4300 | |
David Ahern | 9766e97 | 2015-01-29 20:59:33 -0700 | [diff] [blame] | 4301 | static struct rocker_port_stats { |
| 4302 | char str[ETH_GSTRING_LEN]; |
| 4303 | int type; |
| 4304 | } rocker_port_stats[] = { |
| 4305 | { "rx_packets", ROCKER_TLV_CMD_PORT_STATS_RX_PKTS, }, |
| 4306 | { "rx_bytes", ROCKER_TLV_CMD_PORT_STATS_RX_BYTES, }, |
| 4307 | { "rx_dropped", ROCKER_TLV_CMD_PORT_STATS_RX_DROPPED, }, |
| 4308 | { "rx_errors", ROCKER_TLV_CMD_PORT_STATS_RX_ERRORS, }, |
| 4309 | |
| 4310 | { "tx_packets", ROCKER_TLV_CMD_PORT_STATS_TX_PKTS, }, |
| 4311 | { "tx_bytes", ROCKER_TLV_CMD_PORT_STATS_TX_BYTES, }, |
| 4312 | { "tx_dropped", ROCKER_TLV_CMD_PORT_STATS_TX_DROPPED, }, |
| 4313 | { "tx_errors", ROCKER_TLV_CMD_PORT_STATS_TX_ERRORS, }, |
| 4314 | }; |
| 4315 | |
| 4316 | #define ROCKER_PORT_STATS_LEN ARRAY_SIZE(rocker_port_stats) |
| 4317 | |
| 4318 | static void rocker_port_get_strings(struct net_device *netdev, u32 stringset, |
| 4319 | u8 *data) |
| 4320 | { |
| 4321 | u8 *p = data; |
| 4322 | int i; |
| 4323 | |
| 4324 | switch (stringset) { |
| 4325 | case ETH_SS_STATS: |
| 4326 | for (i = 0; i < ARRAY_SIZE(rocker_port_stats); i++) { |
| 4327 | memcpy(p, rocker_port_stats[i].str, ETH_GSTRING_LEN); |
| 4328 | p += ETH_GSTRING_LEN; |
| 4329 | } |
| 4330 | break; |
| 4331 | } |
| 4332 | } |
| 4333 | |
| 4334 | static int |
| 4335 | rocker_cmd_get_port_stats_prep(struct rocker *rocker, |
| 4336 | struct rocker_port *rocker_port, |
| 4337 | struct rocker_desc_info *desc_info, |
| 4338 | void *priv) |
| 4339 | { |
| 4340 | struct rocker_tlv *cmd_stats; |
| 4341 | |
| 4342 | if (rocker_tlv_put_u16(desc_info, ROCKER_TLV_CMD_TYPE, |
| 4343 | ROCKER_TLV_CMD_TYPE_GET_PORT_STATS)) |
| 4344 | return -EMSGSIZE; |
| 4345 | |
| 4346 | cmd_stats = rocker_tlv_nest_start(desc_info, ROCKER_TLV_CMD_INFO); |
| 4347 | if (!cmd_stats) |
| 4348 | return -EMSGSIZE; |
| 4349 | |
Scott Feldman | 4a6bb6d | 2015-02-25 20:15:37 -0800 | [diff] [blame] | 4350 | if (rocker_tlv_put_u32(desc_info, ROCKER_TLV_CMD_PORT_STATS_PPORT, |
| 4351 | rocker_port->pport)) |
David Ahern | 9766e97 | 2015-01-29 20:59:33 -0700 | [diff] [blame] | 4352 | return -EMSGSIZE; |
| 4353 | |
| 4354 | rocker_tlv_nest_end(desc_info, cmd_stats); |
| 4355 | |
| 4356 | return 0; |
| 4357 | } |
| 4358 | |
| 4359 | static int |
| 4360 | rocker_cmd_get_port_stats_ethtool_proc(struct rocker *rocker, |
| 4361 | struct rocker_port *rocker_port, |
| 4362 | struct rocker_desc_info *desc_info, |
| 4363 | void *priv) |
| 4364 | { |
| 4365 | struct rocker_tlv *attrs[ROCKER_TLV_CMD_MAX + 1]; |
| 4366 | struct rocker_tlv *stats_attrs[ROCKER_TLV_CMD_PORT_STATS_MAX + 1]; |
| 4367 | struct rocker_tlv *pattr; |
Scott Feldman | 4a6bb6d | 2015-02-25 20:15:37 -0800 | [diff] [blame] | 4368 | u32 pport; |
David Ahern | 9766e97 | 2015-01-29 20:59:33 -0700 | [diff] [blame] | 4369 | u64 *data = priv; |
| 4370 | int i; |
| 4371 | |
| 4372 | rocker_tlv_parse_desc(attrs, ROCKER_TLV_CMD_MAX, desc_info); |
| 4373 | |
| 4374 | if (!attrs[ROCKER_TLV_CMD_INFO]) |
| 4375 | return -EIO; |
| 4376 | |
| 4377 | rocker_tlv_parse_nested(stats_attrs, ROCKER_TLV_CMD_PORT_STATS_MAX, |
| 4378 | attrs[ROCKER_TLV_CMD_INFO]); |
| 4379 | |
Scott Feldman | 4a6bb6d | 2015-02-25 20:15:37 -0800 | [diff] [blame] | 4380 | if (!stats_attrs[ROCKER_TLV_CMD_PORT_STATS_PPORT]) |
David Ahern | 9766e97 | 2015-01-29 20:59:33 -0700 | [diff] [blame] | 4381 | return -EIO; |
| 4382 | |
Scott Feldman | 4a6bb6d | 2015-02-25 20:15:37 -0800 | [diff] [blame] | 4383 | pport = rocker_tlv_get_u32(stats_attrs[ROCKER_TLV_CMD_PORT_STATS_PPORT]); |
| 4384 | if (pport != rocker_port->pport) |
David Ahern | 9766e97 | 2015-01-29 20:59:33 -0700 | [diff] [blame] | 4385 | return -EIO; |
| 4386 | |
| 4387 | for (i = 0; i < ARRAY_SIZE(rocker_port_stats); i++) { |
| 4388 | pattr = stats_attrs[rocker_port_stats[i].type]; |
| 4389 | if (!pattr) |
| 4390 | continue; |
| 4391 | |
| 4392 | data[i] = rocker_tlv_get_u64(pattr); |
| 4393 | } |
| 4394 | |
| 4395 | return 0; |
| 4396 | } |
| 4397 | |
| 4398 | static int rocker_cmd_get_port_stats_ethtool(struct rocker_port *rocker_port, |
| 4399 | void *priv) |
| 4400 | { |
| 4401 | return rocker_cmd_exec(rocker_port->rocker, rocker_port, |
| 4402 | rocker_cmd_get_port_stats_prep, NULL, |
| 4403 | rocker_cmd_get_port_stats_ethtool_proc, |
| 4404 | priv, false); |
| 4405 | } |
| 4406 | |
| 4407 | static void rocker_port_get_stats(struct net_device *dev, |
| 4408 | struct ethtool_stats *stats, u64 *data) |
| 4409 | { |
| 4410 | struct rocker_port *rocker_port = netdev_priv(dev); |
| 4411 | |
| 4412 | if (rocker_cmd_get_port_stats_ethtool(rocker_port, data) != 0) { |
| 4413 | int i; |
| 4414 | |
| 4415 | for (i = 0; i < ARRAY_SIZE(rocker_port_stats); ++i) |
| 4416 | data[i] = 0; |
| 4417 | } |
| 4418 | |
| 4419 | return; |
| 4420 | } |
| 4421 | |
| 4422 | static int rocker_port_get_sset_count(struct net_device *netdev, int sset) |
| 4423 | { |
| 4424 | switch (sset) { |
| 4425 | case ETH_SS_STATS: |
| 4426 | return ROCKER_PORT_STATS_LEN; |
| 4427 | default: |
| 4428 | return -EOPNOTSUPP; |
| 4429 | } |
| 4430 | } |
| 4431 | |
Jiri Pirko | 4b8ac96 | 2014-11-28 14:34:26 +0100 | [diff] [blame] | 4432 | static const struct ethtool_ops rocker_port_ethtool_ops = { |
| 4433 | .get_settings = rocker_port_get_settings, |
| 4434 | .set_settings = rocker_port_set_settings, |
| 4435 | .get_drvinfo = rocker_port_get_drvinfo, |
| 4436 | .get_link = ethtool_op_get_link, |
David Ahern | 9766e97 | 2015-01-29 20:59:33 -0700 | [diff] [blame] | 4437 | .get_strings = rocker_port_get_strings, |
| 4438 | .get_ethtool_stats = rocker_port_get_stats, |
| 4439 | .get_sset_count = rocker_port_get_sset_count, |
Jiri Pirko | 4b8ac96 | 2014-11-28 14:34:26 +0100 | [diff] [blame] | 4440 | }; |
| 4441 | |
| 4442 | /***************** |
| 4443 | * NAPI interface |
| 4444 | *****************/ |
| 4445 | |
| 4446 | static struct rocker_port *rocker_port_napi_tx_get(struct napi_struct *napi) |
| 4447 | { |
| 4448 | return container_of(napi, struct rocker_port, napi_tx); |
| 4449 | } |
| 4450 | |
| 4451 | static int rocker_port_poll_tx(struct napi_struct *napi, int budget) |
| 4452 | { |
| 4453 | struct rocker_port *rocker_port = rocker_port_napi_tx_get(napi); |
| 4454 | struct rocker *rocker = rocker_port->rocker; |
| 4455 | struct rocker_desc_info *desc_info; |
| 4456 | u32 credits = 0; |
| 4457 | int err; |
| 4458 | |
| 4459 | /* Cleanup tx descriptors */ |
| 4460 | while ((desc_info = rocker_desc_tail_get(&rocker_port->tx_ring))) { |
David Ahern | f2bbca5 | 2015-01-16 14:22:29 -0700 | [diff] [blame] | 4461 | struct sk_buff *skb; |
| 4462 | |
Jiri Pirko | 4b8ac96 | 2014-11-28 14:34:26 +0100 | [diff] [blame] | 4463 | err = rocker_desc_err(desc_info); |
| 4464 | if (err && net_ratelimit()) |
| 4465 | netdev_err(rocker_port->dev, "tx desc received with err %d\n", |
| 4466 | err); |
| 4467 | rocker_tx_desc_frags_unmap(rocker_port, desc_info); |
David Ahern | f2bbca5 | 2015-01-16 14:22:29 -0700 | [diff] [blame] | 4468 | |
| 4469 | skb = rocker_desc_cookie_ptr_get(desc_info); |
| 4470 | if (err == 0) { |
| 4471 | rocker_port->dev->stats.tx_packets++; |
| 4472 | rocker_port->dev->stats.tx_bytes += skb->len; |
| 4473 | } else |
| 4474 | rocker_port->dev->stats.tx_errors++; |
| 4475 | |
| 4476 | dev_kfree_skb_any(skb); |
Jiri Pirko | 4b8ac96 | 2014-11-28 14:34:26 +0100 | [diff] [blame] | 4477 | credits++; |
| 4478 | } |
| 4479 | |
| 4480 | if (credits && netif_queue_stopped(rocker_port->dev)) |
| 4481 | netif_wake_queue(rocker_port->dev); |
| 4482 | |
| 4483 | napi_complete(napi); |
| 4484 | rocker_dma_ring_credits_set(rocker, &rocker_port->tx_ring, credits); |
| 4485 | |
| 4486 | return 0; |
| 4487 | } |
| 4488 | |
| 4489 | static int rocker_port_rx_proc(struct rocker *rocker, |
| 4490 | struct rocker_port *rocker_port, |
| 4491 | struct rocker_desc_info *desc_info) |
| 4492 | { |
| 4493 | struct rocker_tlv *attrs[ROCKER_TLV_RX_MAX + 1]; |
| 4494 | struct sk_buff *skb = rocker_desc_cookie_ptr_get(desc_info); |
| 4495 | size_t rx_len; |
| 4496 | |
| 4497 | if (!skb) |
| 4498 | return -ENOENT; |
| 4499 | |
| 4500 | rocker_tlv_parse_desc(attrs, ROCKER_TLV_RX_MAX, desc_info); |
| 4501 | if (!attrs[ROCKER_TLV_RX_FRAG_LEN]) |
| 4502 | return -EINVAL; |
| 4503 | |
| 4504 | rocker_dma_rx_ring_skb_unmap(rocker, attrs); |
| 4505 | |
| 4506 | rx_len = rocker_tlv_get_u16(attrs[ROCKER_TLV_RX_FRAG_LEN]); |
| 4507 | skb_put(skb, rx_len); |
| 4508 | skb->protocol = eth_type_trans(skb, rocker_port->dev); |
David Ahern | f2bbca5 | 2015-01-16 14:22:29 -0700 | [diff] [blame] | 4509 | |
| 4510 | rocker_port->dev->stats.rx_packets++; |
| 4511 | rocker_port->dev->stats.rx_bytes += skb->len; |
| 4512 | |
Jiri Pirko | 4b8ac96 | 2014-11-28 14:34:26 +0100 | [diff] [blame] | 4513 | netif_receive_skb(skb); |
| 4514 | |
| 4515 | return rocker_dma_rx_ring_skb_alloc(rocker, rocker_port, desc_info); |
| 4516 | } |
| 4517 | |
| 4518 | static struct rocker_port *rocker_port_napi_rx_get(struct napi_struct *napi) |
| 4519 | { |
| 4520 | return container_of(napi, struct rocker_port, napi_rx); |
| 4521 | } |
| 4522 | |
| 4523 | static int rocker_port_poll_rx(struct napi_struct *napi, int budget) |
| 4524 | { |
| 4525 | struct rocker_port *rocker_port = rocker_port_napi_rx_get(napi); |
| 4526 | struct rocker *rocker = rocker_port->rocker; |
| 4527 | struct rocker_desc_info *desc_info; |
| 4528 | u32 credits = 0; |
| 4529 | int err; |
| 4530 | |
| 4531 | /* Process rx descriptors */ |
| 4532 | while (credits < budget && |
| 4533 | (desc_info = rocker_desc_tail_get(&rocker_port->rx_ring))) { |
| 4534 | err = rocker_desc_err(desc_info); |
| 4535 | if (err) { |
| 4536 | if (net_ratelimit()) |
| 4537 | netdev_err(rocker_port->dev, "rx desc received with err %d\n", |
| 4538 | err); |
| 4539 | } else { |
| 4540 | err = rocker_port_rx_proc(rocker, rocker_port, |
| 4541 | desc_info); |
| 4542 | if (err && net_ratelimit()) |
| 4543 | netdev_err(rocker_port->dev, "rx processing failed with err %d\n", |
| 4544 | err); |
| 4545 | } |
David Ahern | f2bbca5 | 2015-01-16 14:22:29 -0700 | [diff] [blame] | 4546 | if (err) |
| 4547 | rocker_port->dev->stats.rx_errors++; |
| 4548 | |
Jiri Pirko | 4b8ac96 | 2014-11-28 14:34:26 +0100 | [diff] [blame] | 4549 | rocker_desc_gen_clear(desc_info); |
| 4550 | rocker_desc_head_set(rocker, &rocker_port->rx_ring, desc_info); |
| 4551 | credits++; |
| 4552 | } |
| 4553 | |
| 4554 | if (credits < budget) |
| 4555 | napi_complete(napi); |
| 4556 | |
| 4557 | rocker_dma_ring_credits_set(rocker, &rocker_port->rx_ring, credits); |
| 4558 | |
| 4559 | return credits; |
| 4560 | } |
| 4561 | |
| 4562 | /***************** |
| 4563 | * PCI driver ops |
| 4564 | *****************/ |
| 4565 | |
| 4566 | static void rocker_carrier_init(struct rocker_port *rocker_port) |
| 4567 | { |
| 4568 | struct rocker *rocker = rocker_port->rocker; |
| 4569 | u64 link_status = rocker_read64(rocker, PORT_PHYS_LINK_STATUS); |
| 4570 | bool link_up; |
| 4571 | |
Scott Feldman | 4a6bb6d | 2015-02-25 20:15:37 -0800 | [diff] [blame] | 4572 | link_up = link_status & (1 << rocker_port->pport); |
Jiri Pirko | 4b8ac96 | 2014-11-28 14:34:26 +0100 | [diff] [blame] | 4573 | if (link_up) |
| 4574 | netif_carrier_on(rocker_port->dev); |
| 4575 | else |
| 4576 | netif_carrier_off(rocker_port->dev); |
| 4577 | } |
| 4578 | |
| 4579 | static void rocker_remove_ports(struct rocker *rocker) |
| 4580 | { |
Scott Feldman | 9f6bbf7 | 2014-11-28 14:34:27 +0100 | [diff] [blame] | 4581 | struct rocker_port *rocker_port; |
Jiri Pirko | 4b8ac96 | 2014-11-28 14:34:26 +0100 | [diff] [blame] | 4582 | int i; |
| 4583 | |
Scott Feldman | 9f6bbf7 | 2014-11-28 14:34:27 +0100 | [diff] [blame] | 4584 | for (i = 0; i < rocker->port_count; i++) { |
| 4585 | rocker_port = rocker->ports[i]; |
| 4586 | rocker_port_ig_tbl(rocker_port, ROCKER_OP_FLAG_REMOVE); |
| 4587 | unregister_netdev(rocker_port->dev); |
| 4588 | } |
Jiri Pirko | 4b8ac96 | 2014-11-28 14:34:26 +0100 | [diff] [blame] | 4589 | kfree(rocker->ports); |
| 4590 | } |
| 4591 | |
| 4592 | static void rocker_port_dev_addr_init(struct rocker *rocker, |
| 4593 | struct rocker_port *rocker_port) |
| 4594 | { |
| 4595 | struct pci_dev *pdev = rocker->pdev; |
| 4596 | int err; |
| 4597 | |
| 4598 | err = rocker_cmd_get_port_settings_macaddr(rocker_port, |
| 4599 | rocker_port->dev->dev_addr); |
| 4600 | if (err) { |
| 4601 | dev_warn(&pdev->dev, "failed to get mac address, using random\n"); |
| 4602 | eth_hw_addr_random(rocker_port->dev); |
| 4603 | } |
| 4604 | } |
| 4605 | |
| 4606 | static int rocker_probe_port(struct rocker *rocker, unsigned int port_number) |
| 4607 | { |
| 4608 | struct pci_dev *pdev = rocker->pdev; |
| 4609 | struct rocker_port *rocker_port; |
| 4610 | struct net_device *dev; |
| 4611 | int err; |
| 4612 | |
| 4613 | dev = alloc_etherdev(sizeof(struct rocker_port)); |
| 4614 | if (!dev) |
| 4615 | return -ENOMEM; |
| 4616 | rocker_port = netdev_priv(dev); |
| 4617 | rocker_port->dev = dev; |
| 4618 | rocker_port->rocker = rocker; |
| 4619 | rocker_port->port_number = port_number; |
Scott Feldman | 4a6bb6d | 2015-02-25 20:15:37 -0800 | [diff] [blame] | 4620 | rocker_port->pport = port_number + 1; |
Scott Feldman | 5111f80 | 2014-11-28 14:34:30 +0100 | [diff] [blame] | 4621 | rocker_port->brport_flags = BR_LEARNING | BR_LEARNING_SYNC; |
Jiri Pirko | 4b8ac96 | 2014-11-28 14:34:26 +0100 | [diff] [blame] | 4622 | |
| 4623 | rocker_port_dev_addr_init(rocker, rocker_port); |
| 4624 | dev->netdev_ops = &rocker_port_netdev_ops; |
| 4625 | dev->ethtool_ops = &rocker_port_ethtool_ops; |
Scott Feldman | 98237d4 | 2015-03-15 21:07:15 -0700 | [diff] [blame] | 4626 | dev->swdev_ops = &rocker_port_swdev_ops; |
Jiri Pirko | 4b8ac96 | 2014-11-28 14:34:26 +0100 | [diff] [blame] | 4627 | netif_napi_add(dev, &rocker_port->napi_tx, rocker_port_poll_tx, |
| 4628 | NAPI_POLL_WEIGHT); |
| 4629 | netif_napi_add(dev, &rocker_port->napi_rx, rocker_port_poll_rx, |
| 4630 | NAPI_POLL_WEIGHT); |
| 4631 | rocker_carrier_init(rocker_port); |
| 4632 | |
Scott Feldman | c1beeef | 2015-03-05 21:21:20 -0800 | [diff] [blame] | 4633 | dev->features |= NETIF_F_NETNS_LOCAL | |
| 4634 | NETIF_F_HW_VLAN_CTAG_FILTER | |
| 4635 | NETIF_F_HW_SWITCH_OFFLOAD; |
Jiri Pirko | 4b8ac96 | 2014-11-28 14:34:26 +0100 | [diff] [blame] | 4636 | |
| 4637 | err = register_netdev(dev); |
| 4638 | if (err) { |
| 4639 | dev_err(&pdev->dev, "register_netdev failed\n"); |
| 4640 | goto err_register_netdev; |
| 4641 | } |
| 4642 | rocker->ports[port_number] = rocker_port; |
| 4643 | |
Scott Feldman | 5111f80 | 2014-11-28 14:34:30 +0100 | [diff] [blame] | 4644 | rocker_port_set_learning(rocker_port); |
| 4645 | |
Scott Feldman | 9f6bbf7 | 2014-11-28 14:34:27 +0100 | [diff] [blame] | 4646 | rocker_port->internal_vlan_id = |
| 4647 | rocker_port_internal_vlan_id_get(rocker_port, dev->ifindex); |
| 4648 | err = rocker_port_ig_tbl(rocker_port, 0); |
| 4649 | if (err) { |
| 4650 | dev_err(&pdev->dev, "install ig port table failed\n"); |
| 4651 | goto err_port_ig_tbl; |
| 4652 | } |
| 4653 | |
Jiri Pirko | 4b8ac96 | 2014-11-28 14:34:26 +0100 | [diff] [blame] | 4654 | return 0; |
| 4655 | |
Scott Feldman | 9f6bbf7 | 2014-11-28 14:34:27 +0100 | [diff] [blame] | 4656 | err_port_ig_tbl: |
| 4657 | unregister_netdev(dev); |
Jiri Pirko | 4b8ac96 | 2014-11-28 14:34:26 +0100 | [diff] [blame] | 4658 | err_register_netdev: |
| 4659 | free_netdev(dev); |
| 4660 | return err; |
| 4661 | } |
| 4662 | |
| 4663 | static int rocker_probe_ports(struct rocker *rocker) |
| 4664 | { |
| 4665 | int i; |
| 4666 | size_t alloc_size; |
| 4667 | int err; |
| 4668 | |
| 4669 | alloc_size = sizeof(struct rocker_port *) * rocker->port_count; |
| 4670 | rocker->ports = kmalloc(alloc_size, GFP_KERNEL); |
Dan Carpenter | e65ad3b | 2015-02-25 16:35:32 +0300 | [diff] [blame] | 4671 | if (!rocker->ports) |
| 4672 | return -ENOMEM; |
Jiri Pirko | 4b8ac96 | 2014-11-28 14:34:26 +0100 | [diff] [blame] | 4673 | for (i = 0; i < rocker->port_count; i++) { |
| 4674 | err = rocker_probe_port(rocker, i); |
| 4675 | if (err) |
| 4676 | goto remove_ports; |
| 4677 | } |
| 4678 | return 0; |
| 4679 | |
| 4680 | remove_ports: |
| 4681 | rocker_remove_ports(rocker); |
| 4682 | return err; |
| 4683 | } |
| 4684 | |
| 4685 | static int rocker_msix_init(struct rocker *rocker) |
| 4686 | { |
| 4687 | struct pci_dev *pdev = rocker->pdev; |
| 4688 | int msix_entries; |
| 4689 | int i; |
| 4690 | int err; |
| 4691 | |
| 4692 | msix_entries = pci_msix_vec_count(pdev); |
| 4693 | if (msix_entries < 0) |
| 4694 | return msix_entries; |
| 4695 | |
| 4696 | if (msix_entries != ROCKER_MSIX_VEC_COUNT(rocker->port_count)) |
| 4697 | return -EINVAL; |
| 4698 | |
| 4699 | rocker->msix_entries = kmalloc_array(msix_entries, |
| 4700 | sizeof(struct msix_entry), |
| 4701 | GFP_KERNEL); |
| 4702 | if (!rocker->msix_entries) |
| 4703 | return -ENOMEM; |
| 4704 | |
| 4705 | for (i = 0; i < msix_entries; i++) |
| 4706 | rocker->msix_entries[i].entry = i; |
| 4707 | |
| 4708 | err = pci_enable_msix_exact(pdev, rocker->msix_entries, msix_entries); |
| 4709 | if (err < 0) |
| 4710 | goto err_enable_msix; |
| 4711 | |
| 4712 | return 0; |
| 4713 | |
| 4714 | err_enable_msix: |
| 4715 | kfree(rocker->msix_entries); |
| 4716 | return err; |
| 4717 | } |
| 4718 | |
| 4719 | static void rocker_msix_fini(struct rocker *rocker) |
| 4720 | { |
| 4721 | pci_disable_msix(rocker->pdev); |
| 4722 | kfree(rocker->msix_entries); |
| 4723 | } |
| 4724 | |
| 4725 | static int rocker_probe(struct pci_dev *pdev, const struct pci_device_id *id) |
| 4726 | { |
| 4727 | struct rocker *rocker; |
| 4728 | int err; |
| 4729 | |
| 4730 | rocker = kzalloc(sizeof(*rocker), GFP_KERNEL); |
| 4731 | if (!rocker) |
| 4732 | return -ENOMEM; |
| 4733 | |
| 4734 | err = pci_enable_device(pdev); |
| 4735 | if (err) { |
| 4736 | dev_err(&pdev->dev, "pci_enable_device failed\n"); |
| 4737 | goto err_pci_enable_device; |
| 4738 | } |
| 4739 | |
| 4740 | err = pci_request_regions(pdev, rocker_driver_name); |
| 4741 | if (err) { |
| 4742 | dev_err(&pdev->dev, "pci_request_regions failed\n"); |
| 4743 | goto err_pci_request_regions; |
| 4744 | } |
| 4745 | |
| 4746 | err = pci_set_dma_mask(pdev, DMA_BIT_MASK(64)); |
| 4747 | if (!err) { |
| 4748 | err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64)); |
| 4749 | if (err) { |
| 4750 | dev_err(&pdev->dev, "pci_set_consistent_dma_mask failed\n"); |
| 4751 | goto err_pci_set_dma_mask; |
| 4752 | } |
| 4753 | } else { |
| 4754 | err = pci_set_dma_mask(pdev, DMA_BIT_MASK(32)); |
| 4755 | if (err) { |
| 4756 | dev_err(&pdev->dev, "pci_set_dma_mask failed\n"); |
| 4757 | goto err_pci_set_dma_mask; |
| 4758 | } |
| 4759 | } |
| 4760 | |
| 4761 | if (pci_resource_len(pdev, 0) < ROCKER_PCI_BAR0_SIZE) { |
| 4762 | dev_err(&pdev->dev, "invalid PCI region size\n"); |
Wei Yongjun | 3122a92 | 2015-04-16 20:21:02 +0800 | [diff] [blame] | 4763 | err = -EINVAL; |
Jiri Pirko | 4b8ac96 | 2014-11-28 14:34:26 +0100 | [diff] [blame] | 4764 | goto err_pci_resource_len_check; |
| 4765 | } |
| 4766 | |
| 4767 | rocker->hw_addr = ioremap(pci_resource_start(pdev, 0), |
| 4768 | pci_resource_len(pdev, 0)); |
| 4769 | if (!rocker->hw_addr) { |
| 4770 | dev_err(&pdev->dev, "ioremap failed\n"); |
| 4771 | err = -EIO; |
| 4772 | goto err_ioremap; |
| 4773 | } |
| 4774 | pci_set_master(pdev); |
| 4775 | |
| 4776 | rocker->pdev = pdev; |
| 4777 | pci_set_drvdata(pdev, rocker); |
| 4778 | |
| 4779 | rocker->port_count = rocker_read32(rocker, PORT_PHYS_COUNT); |
| 4780 | |
| 4781 | err = rocker_msix_init(rocker); |
| 4782 | if (err) { |
| 4783 | dev_err(&pdev->dev, "MSI-X init failed\n"); |
| 4784 | goto err_msix_init; |
| 4785 | } |
| 4786 | |
| 4787 | err = rocker_basic_hw_test(rocker); |
| 4788 | if (err) { |
| 4789 | dev_err(&pdev->dev, "basic hw test failed\n"); |
| 4790 | goto err_basic_hw_test; |
| 4791 | } |
| 4792 | |
| 4793 | rocker_write32(rocker, CONTROL, ROCKER_CONTROL_RESET); |
| 4794 | |
| 4795 | err = rocker_dma_rings_init(rocker); |
| 4796 | if (err) |
| 4797 | goto err_dma_rings_init; |
| 4798 | |
| 4799 | err = request_irq(rocker_msix_vector(rocker, ROCKER_MSIX_VEC_CMD), |
| 4800 | rocker_cmd_irq_handler, 0, |
| 4801 | rocker_driver_name, rocker); |
| 4802 | if (err) { |
| 4803 | dev_err(&pdev->dev, "cannot assign cmd irq\n"); |
| 4804 | goto err_request_cmd_irq; |
| 4805 | } |
| 4806 | |
| 4807 | err = request_irq(rocker_msix_vector(rocker, ROCKER_MSIX_VEC_EVENT), |
| 4808 | rocker_event_irq_handler, 0, |
| 4809 | rocker_driver_name, rocker); |
| 4810 | if (err) { |
| 4811 | dev_err(&pdev->dev, "cannot assign event irq\n"); |
| 4812 | goto err_request_event_irq; |
| 4813 | } |
| 4814 | |
| 4815 | rocker->hw.id = rocker_read64(rocker, SWITCH_ID); |
| 4816 | |
Scott Feldman | 9f6bbf7 | 2014-11-28 14:34:27 +0100 | [diff] [blame] | 4817 | err = rocker_init_tbls(rocker); |
| 4818 | if (err) { |
| 4819 | dev_err(&pdev->dev, "cannot init rocker tables\n"); |
| 4820 | goto err_init_tbls; |
| 4821 | } |
| 4822 | |
Jiri Pirko | 4b8ac96 | 2014-11-28 14:34:26 +0100 | [diff] [blame] | 4823 | err = rocker_probe_ports(rocker); |
| 4824 | if (err) { |
| 4825 | dev_err(&pdev->dev, "failed to probe ports\n"); |
| 4826 | goto err_probe_ports; |
| 4827 | } |
| 4828 | |
| 4829 | dev_info(&pdev->dev, "Rocker switch with id %016llx\n", rocker->hw.id); |
| 4830 | |
| 4831 | return 0; |
| 4832 | |
| 4833 | err_probe_ports: |
Scott Feldman | 9f6bbf7 | 2014-11-28 14:34:27 +0100 | [diff] [blame] | 4834 | rocker_free_tbls(rocker); |
| 4835 | err_init_tbls: |
Jiri Pirko | 4b8ac96 | 2014-11-28 14:34:26 +0100 | [diff] [blame] | 4836 | free_irq(rocker_msix_vector(rocker, ROCKER_MSIX_VEC_EVENT), rocker); |
| 4837 | err_request_event_irq: |
| 4838 | free_irq(rocker_msix_vector(rocker, ROCKER_MSIX_VEC_CMD), rocker); |
| 4839 | err_request_cmd_irq: |
| 4840 | rocker_dma_rings_fini(rocker); |
| 4841 | err_dma_rings_init: |
| 4842 | err_basic_hw_test: |
| 4843 | rocker_msix_fini(rocker); |
| 4844 | err_msix_init: |
| 4845 | iounmap(rocker->hw_addr); |
| 4846 | err_ioremap: |
| 4847 | err_pci_resource_len_check: |
| 4848 | err_pci_set_dma_mask: |
| 4849 | pci_release_regions(pdev); |
| 4850 | err_pci_request_regions: |
| 4851 | pci_disable_device(pdev); |
| 4852 | err_pci_enable_device: |
| 4853 | kfree(rocker); |
| 4854 | return err; |
| 4855 | } |
| 4856 | |
| 4857 | static void rocker_remove(struct pci_dev *pdev) |
| 4858 | { |
| 4859 | struct rocker *rocker = pci_get_drvdata(pdev); |
| 4860 | |
Scott Feldman | 9f6bbf7 | 2014-11-28 14:34:27 +0100 | [diff] [blame] | 4861 | rocker_free_tbls(rocker); |
Jiri Pirko | 4b8ac96 | 2014-11-28 14:34:26 +0100 | [diff] [blame] | 4862 | rocker_write32(rocker, CONTROL, ROCKER_CONTROL_RESET); |
| 4863 | rocker_remove_ports(rocker); |
| 4864 | free_irq(rocker_msix_vector(rocker, ROCKER_MSIX_VEC_EVENT), rocker); |
| 4865 | free_irq(rocker_msix_vector(rocker, ROCKER_MSIX_VEC_CMD), rocker); |
| 4866 | rocker_dma_rings_fini(rocker); |
| 4867 | rocker_msix_fini(rocker); |
| 4868 | iounmap(rocker->hw_addr); |
| 4869 | pci_release_regions(rocker->pdev); |
| 4870 | pci_disable_device(rocker->pdev); |
| 4871 | kfree(rocker); |
| 4872 | } |
| 4873 | |
| 4874 | static struct pci_driver rocker_pci_driver = { |
| 4875 | .name = rocker_driver_name, |
| 4876 | .id_table = rocker_pci_id_table, |
| 4877 | .probe = rocker_probe, |
| 4878 | .remove = rocker_remove, |
| 4879 | }; |
| 4880 | |
Scott Feldman | 6c70794 | 2014-11-28 14:34:28 +0100 | [diff] [blame] | 4881 | /************************************ |
| 4882 | * Net device notifier event handler |
| 4883 | ************************************/ |
| 4884 | |
| 4885 | static bool rocker_port_dev_check(struct net_device *dev) |
| 4886 | { |
| 4887 | return dev->netdev_ops == &rocker_port_netdev_ops; |
| 4888 | } |
| 4889 | |
| 4890 | static int rocker_port_bridge_join(struct rocker_port *rocker_port, |
| 4891 | struct net_device *bridge) |
| 4892 | { |
| 4893 | int err; |
| 4894 | |
| 4895 | rocker_port_internal_vlan_id_put(rocker_port, |
| 4896 | rocker_port->dev->ifindex); |
| 4897 | |
| 4898 | rocker_port->bridge_dev = bridge; |
| 4899 | |
| 4900 | /* Use bridge internal VLAN ID for untagged pkts */ |
| 4901 | err = rocker_port_vlan(rocker_port, ROCKER_OP_FLAG_REMOVE, 0); |
| 4902 | if (err) |
| 4903 | return err; |
| 4904 | rocker_port->internal_vlan_id = |
| 4905 | rocker_port_internal_vlan_id_get(rocker_port, |
| 4906 | bridge->ifindex); |
Scott Feldman | e47172a | 2015-02-25 20:15:38 -0800 | [diff] [blame] | 4907 | return rocker_port_vlan(rocker_port, 0, 0); |
Scott Feldman | 6c70794 | 2014-11-28 14:34:28 +0100 | [diff] [blame] | 4908 | } |
| 4909 | |
| 4910 | static int rocker_port_bridge_leave(struct rocker_port *rocker_port) |
| 4911 | { |
| 4912 | int err; |
| 4913 | |
| 4914 | rocker_port_internal_vlan_id_put(rocker_port, |
| 4915 | rocker_port->bridge_dev->ifindex); |
| 4916 | |
| 4917 | rocker_port->bridge_dev = NULL; |
| 4918 | |
| 4919 | /* Use port internal VLAN ID for untagged pkts */ |
| 4920 | err = rocker_port_vlan(rocker_port, ROCKER_OP_FLAG_REMOVE, 0); |
| 4921 | if (err) |
| 4922 | return err; |
| 4923 | rocker_port->internal_vlan_id = |
| 4924 | rocker_port_internal_vlan_id_get(rocker_port, |
| 4925 | rocker_port->dev->ifindex); |
| 4926 | err = rocker_port_vlan(rocker_port, 0, 0); |
Scott Feldman | e47172a | 2015-02-25 20:15:38 -0800 | [diff] [blame] | 4927 | if (err) |
| 4928 | return err; |
| 4929 | |
| 4930 | if (rocker_port->dev->flags & IFF_UP) |
| 4931 | err = rocker_port_fwd_enable(rocker_port); |
Scott Feldman | 6c70794 | 2014-11-28 14:34:28 +0100 | [diff] [blame] | 4932 | |
| 4933 | return err; |
| 4934 | } |
| 4935 | |
| 4936 | static int rocker_port_master_changed(struct net_device *dev) |
| 4937 | { |
| 4938 | struct rocker_port *rocker_port = netdev_priv(dev); |
| 4939 | struct net_device *master = netdev_master_upper_dev_get(dev); |
| 4940 | int err = 0; |
| 4941 | |
Simon Horman | a6e95cc | 2015-03-24 09:31:40 +0900 | [diff] [blame] | 4942 | /* There are currently three cases handled here: |
| 4943 | * 1. Joining a bridge |
| 4944 | * 2. Leaving a previously joined bridge |
| 4945 | * 3. Other, e.g. being added to or removed from a bond or openvswitch, |
| 4946 | * in which case nothing is done |
| 4947 | */ |
Scott Feldman | 6c70794 | 2014-11-28 14:34:28 +0100 | [diff] [blame] | 4948 | if (master && master->rtnl_link_ops && |
| 4949 | !strcmp(master->rtnl_link_ops->kind, "bridge")) |
| 4950 | err = rocker_port_bridge_join(rocker_port, master); |
Simon Horman | a6e95cc | 2015-03-24 09:31:40 +0900 | [diff] [blame] | 4951 | else if (rocker_port_is_bridged(rocker_port)) |
Scott Feldman | 6c70794 | 2014-11-28 14:34:28 +0100 | [diff] [blame] | 4952 | err = rocker_port_bridge_leave(rocker_port); |
| 4953 | |
| 4954 | return err; |
| 4955 | } |
| 4956 | |
| 4957 | static int rocker_netdevice_event(struct notifier_block *unused, |
| 4958 | unsigned long event, void *ptr) |
| 4959 | { |
| 4960 | struct net_device *dev; |
| 4961 | int err; |
| 4962 | |
| 4963 | switch (event) { |
| 4964 | case NETDEV_CHANGEUPPER: |
| 4965 | dev = netdev_notifier_info_to_dev(ptr); |
| 4966 | if (!rocker_port_dev_check(dev)) |
| 4967 | return NOTIFY_DONE; |
| 4968 | err = rocker_port_master_changed(dev); |
| 4969 | if (err) |
| 4970 | netdev_warn(dev, |
| 4971 | "failed to reflect master change (err %d)\n", |
| 4972 | err); |
| 4973 | break; |
| 4974 | } |
| 4975 | |
| 4976 | return NOTIFY_DONE; |
| 4977 | } |
| 4978 | |
| 4979 | static struct notifier_block rocker_netdevice_nb __read_mostly = { |
| 4980 | .notifier_call = rocker_netdevice_event, |
| 4981 | }; |
| 4982 | |
Scott Feldman | c1beeef | 2015-03-05 21:21:20 -0800 | [diff] [blame] | 4983 | /************************************ |
| 4984 | * Net event notifier event handler |
| 4985 | ************************************/ |
| 4986 | |
| 4987 | static int rocker_neigh_update(struct net_device *dev, struct neighbour *n) |
| 4988 | { |
| 4989 | struct rocker_port *rocker_port = netdev_priv(dev); |
| 4990 | int flags = (n->nud_state & NUD_VALID) ? 0 : ROCKER_OP_FLAG_REMOVE; |
| 4991 | __be32 ip_addr = *(__be32 *)n->primary_key; |
| 4992 | |
| 4993 | return rocker_port_ipv4_neigh(rocker_port, flags, ip_addr, n->ha); |
| 4994 | } |
| 4995 | |
| 4996 | static int rocker_netevent_event(struct notifier_block *unused, |
| 4997 | unsigned long event, void *ptr) |
| 4998 | { |
| 4999 | struct net_device *dev; |
| 5000 | struct neighbour *n = ptr; |
| 5001 | int err; |
| 5002 | |
| 5003 | switch (event) { |
| 5004 | case NETEVENT_NEIGH_UPDATE: |
| 5005 | if (n->tbl != &arp_tbl) |
| 5006 | return NOTIFY_DONE; |
| 5007 | dev = n->dev; |
| 5008 | if (!rocker_port_dev_check(dev)) |
| 5009 | return NOTIFY_DONE; |
| 5010 | err = rocker_neigh_update(dev, n); |
| 5011 | if (err) |
| 5012 | netdev_warn(dev, |
| 5013 | "failed to handle neigh update (err %d)\n", |
| 5014 | err); |
| 5015 | break; |
| 5016 | } |
| 5017 | |
| 5018 | return NOTIFY_DONE; |
| 5019 | } |
| 5020 | |
| 5021 | static struct notifier_block rocker_netevent_nb __read_mostly = { |
| 5022 | .notifier_call = rocker_netevent_event, |
| 5023 | }; |
| 5024 | |
Jiri Pirko | 4b8ac96 | 2014-11-28 14:34:26 +0100 | [diff] [blame] | 5025 | /*********************** |
| 5026 | * Module init and exit |
| 5027 | ***********************/ |
| 5028 | |
| 5029 | static int __init rocker_module_init(void) |
| 5030 | { |
Scott Feldman | 6c70794 | 2014-11-28 14:34:28 +0100 | [diff] [blame] | 5031 | int err; |
| 5032 | |
| 5033 | register_netdevice_notifier(&rocker_netdevice_nb); |
Scott Feldman | c1beeef | 2015-03-05 21:21:20 -0800 | [diff] [blame] | 5034 | register_netevent_notifier(&rocker_netevent_nb); |
Scott Feldman | 6c70794 | 2014-11-28 14:34:28 +0100 | [diff] [blame] | 5035 | err = pci_register_driver(&rocker_pci_driver); |
| 5036 | if (err) |
| 5037 | goto err_pci_register_driver; |
| 5038 | return 0; |
| 5039 | |
| 5040 | err_pci_register_driver: |
Scott Feldman | c1beeef | 2015-03-05 21:21:20 -0800 | [diff] [blame] | 5041 | unregister_netdevice_notifier(&rocker_netevent_nb); |
Scott Feldman | 6c70794 | 2014-11-28 14:34:28 +0100 | [diff] [blame] | 5042 | unregister_netdevice_notifier(&rocker_netdevice_nb); |
| 5043 | return err; |
Jiri Pirko | 4b8ac96 | 2014-11-28 14:34:26 +0100 | [diff] [blame] | 5044 | } |
| 5045 | |
| 5046 | static void __exit rocker_module_exit(void) |
| 5047 | { |
Scott Feldman | c1beeef | 2015-03-05 21:21:20 -0800 | [diff] [blame] | 5048 | unregister_netevent_notifier(&rocker_netevent_nb); |
Scott Feldman | 6c70794 | 2014-11-28 14:34:28 +0100 | [diff] [blame] | 5049 | unregister_netdevice_notifier(&rocker_netdevice_nb); |
Jiri Pirko | 4b8ac96 | 2014-11-28 14:34:26 +0100 | [diff] [blame] | 5050 | pci_unregister_driver(&rocker_pci_driver); |
| 5051 | } |
| 5052 | |
| 5053 | module_init(rocker_module_init); |
| 5054 | module_exit(rocker_module_exit); |
| 5055 | |
| 5056 | MODULE_LICENSE("GPL v2"); |
| 5057 | MODULE_AUTHOR("Jiri Pirko <jiri@resnulli.us>"); |
| 5058 | MODULE_AUTHOR("Scott Feldman <sfeldma@gmail.com>"); |
| 5059 | MODULE_DESCRIPTION("Rocker switch device driver"); |
| 5060 | MODULE_DEVICE_TABLE(pci, rocker_pci_id_table); |