Alexander Duyck | 0e7b364 | 2014-09-20 19:48:10 -0400 | [diff] [blame] | 1 | /* Intel Ethernet Switch Host Interface Driver |
| 2 | * Copyright(c) 2013 - 2014 Intel Corporation. |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or modify it |
| 5 | * under the terms and conditions of the GNU General Public License, |
| 6 | * version 2, as published by the Free Software Foundation. |
| 7 | * |
| 8 | * This program is distributed in the hope it will be useful, but WITHOUT |
| 9 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 10 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for |
| 11 | * more details. |
| 12 | * |
| 13 | * The full GNU General Public License is included in this distribution in |
| 14 | * the file called "COPYING". |
| 15 | * |
| 16 | * Contact Information: |
| 17 | * e1000-devel Mailing List <e1000-devel@lists.sourceforge.net> |
| 18 | * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497 |
| 19 | */ |
| 20 | |
| 21 | #include "fm10k.h" |
| 22 | |
| 23 | static netdev_tx_t fm10k_xmit_frame(struct sk_buff *skb, struct net_device *dev) |
| 24 | { |
| 25 | dev_kfree_skb_any(skb); |
| 26 | return NETDEV_TX_OK; |
| 27 | } |
| 28 | |
| 29 | static int fm10k_change_mtu(struct net_device *dev, int new_mtu) |
| 30 | { |
| 31 | if (new_mtu < 68 || new_mtu > FM10K_MAX_JUMBO_FRAME_SIZE) |
| 32 | return -EINVAL; |
| 33 | |
| 34 | dev->mtu = new_mtu; |
| 35 | |
| 36 | return 0; |
| 37 | } |
| 38 | |
Alexander Duyck | 8f5e20d | 2014-09-20 19:48:20 -0400 | [diff] [blame^] | 39 | static int fm10k_uc_vlan_unsync(struct net_device *netdev, |
| 40 | const unsigned char *uc_addr) |
| 41 | { |
| 42 | struct fm10k_intfc *interface = netdev_priv(netdev); |
| 43 | struct fm10k_hw *hw = &interface->hw; |
| 44 | u16 glort = interface->glort; |
| 45 | u16 vid = interface->vid; |
| 46 | bool set = !!(vid / VLAN_N_VID); |
| 47 | int err; |
| 48 | |
| 49 | /* drop any leading bits on the VLAN ID */ |
| 50 | vid &= VLAN_N_VID - 1; |
| 51 | |
| 52 | err = hw->mac.ops.update_uc_addr(hw, glort, uc_addr, vid, set, 0); |
| 53 | if (err) |
| 54 | return err; |
| 55 | |
| 56 | /* return non-zero value as we are only doing a partial sync/unsync */ |
| 57 | return 1; |
| 58 | } |
| 59 | |
| 60 | static int fm10k_mc_vlan_unsync(struct net_device *netdev, |
| 61 | const unsigned char *mc_addr) |
| 62 | { |
| 63 | struct fm10k_intfc *interface = netdev_priv(netdev); |
| 64 | struct fm10k_hw *hw = &interface->hw; |
| 65 | u16 glort = interface->glort; |
| 66 | u16 vid = interface->vid; |
| 67 | bool set = !!(vid / VLAN_N_VID); |
| 68 | int err; |
| 69 | |
| 70 | /* drop any leading bits on the VLAN ID */ |
| 71 | vid &= VLAN_N_VID - 1; |
| 72 | |
| 73 | err = hw->mac.ops.update_mc_addr(hw, glort, mc_addr, vid, set); |
| 74 | if (err) |
| 75 | return err; |
| 76 | |
| 77 | /* return non-zero value as we are only doing a partial sync/unsync */ |
| 78 | return 1; |
| 79 | } |
| 80 | |
| 81 | static int fm10k_update_vid(struct net_device *netdev, u16 vid, bool set) |
| 82 | { |
| 83 | struct fm10k_intfc *interface = netdev_priv(netdev); |
| 84 | struct fm10k_hw *hw = &interface->hw; |
| 85 | s32 err; |
| 86 | |
| 87 | /* updates do not apply to VLAN 0 */ |
| 88 | if (!vid) |
| 89 | return 0; |
| 90 | |
| 91 | if (vid >= VLAN_N_VID) |
| 92 | return -EINVAL; |
| 93 | |
| 94 | /* Verify we have permission to add VLANs */ |
| 95 | if (hw->mac.vlan_override) |
| 96 | return -EACCES; |
| 97 | |
| 98 | /* if default VLAN is already present do nothing */ |
| 99 | if (vid == hw->mac.default_vid) |
| 100 | return -EBUSY; |
| 101 | |
| 102 | /* update active_vlans bitmask */ |
| 103 | set_bit(vid, interface->active_vlans); |
| 104 | if (!set) |
| 105 | clear_bit(vid, interface->active_vlans); |
| 106 | |
| 107 | fm10k_mbx_lock(interface); |
| 108 | |
| 109 | /* only need to update the VLAN if not in promiscous mode */ |
| 110 | if (!(netdev->flags & IFF_PROMISC)) { |
| 111 | err = hw->mac.ops.update_vlan(hw, vid, 0, set); |
| 112 | if (err) |
| 113 | return err; |
| 114 | } |
| 115 | |
| 116 | /* update our base MAC address */ |
| 117 | err = hw->mac.ops.update_uc_addr(hw, interface->glort, hw->mac.addr, |
| 118 | vid, set, 0); |
| 119 | if (err) |
| 120 | return err; |
| 121 | |
| 122 | /* set vid prior to syncing/unsyncing the VLAN */ |
| 123 | interface->vid = vid + (set ? VLAN_N_VID : 0); |
| 124 | |
| 125 | /* Update the unicast and multicast address list to add/drop VLAN */ |
| 126 | __dev_uc_unsync(netdev, fm10k_uc_vlan_unsync); |
| 127 | __dev_mc_unsync(netdev, fm10k_mc_vlan_unsync); |
| 128 | |
| 129 | fm10k_mbx_unlock(interface); |
| 130 | |
| 131 | return 0; |
| 132 | } |
| 133 | |
| 134 | static int fm10k_vlan_rx_add_vid(struct net_device *netdev, |
| 135 | __always_unused __be16 proto, u16 vid) |
| 136 | { |
| 137 | /* update VLAN and address table based on changes */ |
| 138 | return fm10k_update_vid(netdev, vid, true); |
| 139 | } |
| 140 | |
| 141 | static int fm10k_vlan_rx_kill_vid(struct net_device *netdev, |
| 142 | __always_unused __be16 proto, u16 vid) |
| 143 | { |
| 144 | /* update VLAN and address table based on changes */ |
| 145 | return fm10k_update_vid(netdev, vid, false); |
| 146 | } |
| 147 | |
| 148 | static u16 fm10k_find_next_vlan(struct fm10k_intfc *interface, u16 vid) |
| 149 | { |
| 150 | struct fm10k_hw *hw = &interface->hw; |
| 151 | u16 default_vid = hw->mac.default_vid; |
| 152 | u16 vid_limit = vid < default_vid ? default_vid : VLAN_N_VID; |
| 153 | |
| 154 | vid = find_next_bit(interface->active_vlans, vid_limit, ++vid); |
| 155 | |
| 156 | return vid; |
| 157 | } |
| 158 | |
| 159 | static void fm10k_clear_unused_vlans(struct fm10k_intfc *interface) |
| 160 | { |
| 161 | struct fm10k_hw *hw = &interface->hw; |
| 162 | u32 vid, prev_vid; |
| 163 | |
| 164 | /* loop through and find any gaps in the table */ |
| 165 | for (vid = 0, prev_vid = 0; |
| 166 | prev_vid < VLAN_N_VID; |
| 167 | prev_vid = vid + 1, vid = fm10k_find_next_vlan(interface, vid)) { |
| 168 | if (prev_vid == vid) |
| 169 | continue; |
| 170 | |
| 171 | /* send request to clear multiple bits at a time */ |
| 172 | prev_vid += (vid - prev_vid - 1) << FM10K_VLAN_LENGTH_SHIFT; |
| 173 | hw->mac.ops.update_vlan(hw, prev_vid, 0, false); |
| 174 | } |
| 175 | } |
| 176 | |
| 177 | static int __fm10k_uc_sync(struct net_device *dev, |
| 178 | const unsigned char *addr, bool sync) |
| 179 | { |
| 180 | struct fm10k_intfc *interface = netdev_priv(dev); |
| 181 | struct fm10k_hw *hw = &interface->hw; |
| 182 | u16 vid, glort = interface->glort; |
| 183 | s32 err; |
| 184 | |
| 185 | if (!is_valid_ether_addr(addr)) |
| 186 | return -EADDRNOTAVAIL; |
| 187 | |
| 188 | /* update table with current entries */ |
| 189 | for (vid = hw->mac.default_vid ? fm10k_find_next_vlan(interface, 0) : 0; |
| 190 | vid < VLAN_N_VID; |
| 191 | vid = fm10k_find_next_vlan(interface, vid)) { |
| 192 | err = hw->mac.ops.update_uc_addr(hw, glort, addr, |
| 193 | vid, sync, 0); |
| 194 | if (err) |
| 195 | return err; |
| 196 | } |
| 197 | |
| 198 | return 0; |
| 199 | } |
| 200 | |
| 201 | static int fm10k_uc_sync(struct net_device *dev, |
| 202 | const unsigned char *addr) |
| 203 | { |
| 204 | return __fm10k_uc_sync(dev, addr, true); |
| 205 | } |
| 206 | |
| 207 | static int fm10k_uc_unsync(struct net_device *dev, |
| 208 | const unsigned char *addr) |
| 209 | { |
| 210 | return __fm10k_uc_sync(dev, addr, false); |
| 211 | } |
| 212 | |
Alexander Duyck | 0e7b364 | 2014-09-20 19:48:10 -0400 | [diff] [blame] | 213 | static int fm10k_set_mac(struct net_device *dev, void *p) |
| 214 | { |
Alexander Duyck | 8f5e20d | 2014-09-20 19:48:20 -0400 | [diff] [blame^] | 215 | struct fm10k_intfc *interface = netdev_priv(dev); |
| 216 | struct fm10k_hw *hw = &interface->hw; |
Alexander Duyck | 0e7b364 | 2014-09-20 19:48:10 -0400 | [diff] [blame] | 217 | struct sockaddr *addr = p; |
| 218 | s32 err = 0; |
| 219 | |
| 220 | if (!is_valid_ether_addr(addr->sa_data)) |
| 221 | return -EADDRNOTAVAIL; |
| 222 | |
Alexander Duyck | 8f5e20d | 2014-09-20 19:48:20 -0400 | [diff] [blame^] | 223 | if (dev->flags & IFF_UP) { |
| 224 | /* setting MAC address requires mailbox */ |
| 225 | fm10k_mbx_lock(interface); |
| 226 | |
| 227 | err = fm10k_uc_sync(dev, addr->sa_data); |
| 228 | if (!err) |
| 229 | fm10k_uc_unsync(dev, hw->mac.addr); |
| 230 | |
| 231 | fm10k_mbx_unlock(interface); |
| 232 | } |
| 233 | |
Alexander Duyck | 0e7b364 | 2014-09-20 19:48:10 -0400 | [diff] [blame] | 234 | if (!err) { |
| 235 | ether_addr_copy(dev->dev_addr, addr->sa_data); |
Alexander Duyck | 8f5e20d | 2014-09-20 19:48:20 -0400 | [diff] [blame^] | 236 | ether_addr_copy(hw->mac.addr, addr->sa_data); |
Alexander Duyck | 0e7b364 | 2014-09-20 19:48:10 -0400 | [diff] [blame] | 237 | dev->addr_assign_type &= ~NET_ADDR_RANDOM; |
| 238 | } |
| 239 | |
Alexander Duyck | 8f5e20d | 2014-09-20 19:48:20 -0400 | [diff] [blame^] | 240 | /* if we had a mailbox error suggest trying again */ |
| 241 | return err ? -EAGAIN : 0; |
| 242 | } |
| 243 | |
| 244 | static int __fm10k_mc_sync(struct net_device *dev, |
| 245 | const unsigned char *addr, bool sync) |
| 246 | { |
| 247 | struct fm10k_intfc *interface = netdev_priv(dev); |
| 248 | struct fm10k_hw *hw = &interface->hw; |
| 249 | u16 vid, glort = interface->glort; |
| 250 | s32 err; |
| 251 | |
| 252 | if (!is_multicast_ether_addr(addr)) |
| 253 | return -EADDRNOTAVAIL; |
| 254 | |
| 255 | /* update table with current entries */ |
| 256 | for (vid = hw->mac.default_vid ? fm10k_find_next_vlan(interface, 0) : 0; |
| 257 | vid < VLAN_N_VID; |
| 258 | vid = fm10k_find_next_vlan(interface, vid)) { |
| 259 | err = hw->mac.ops.update_mc_addr(hw, glort, addr, vid, sync); |
| 260 | if (err) |
| 261 | return err; |
| 262 | } |
| 263 | |
| 264 | return 0; |
| 265 | } |
| 266 | |
| 267 | static int fm10k_mc_sync(struct net_device *dev, |
| 268 | const unsigned char *addr) |
| 269 | { |
| 270 | return __fm10k_mc_sync(dev, addr, true); |
| 271 | } |
| 272 | |
| 273 | static int fm10k_mc_unsync(struct net_device *dev, |
| 274 | const unsigned char *addr) |
| 275 | { |
| 276 | return __fm10k_mc_sync(dev, addr, false); |
Alexander Duyck | 0e7b364 | 2014-09-20 19:48:10 -0400 | [diff] [blame] | 277 | } |
| 278 | |
| 279 | static void fm10k_set_rx_mode(struct net_device *dev) |
| 280 | { |
Alexander Duyck | 8f5e20d | 2014-09-20 19:48:20 -0400 | [diff] [blame^] | 281 | struct fm10k_intfc *interface = netdev_priv(dev); |
| 282 | struct fm10k_hw *hw = &interface->hw; |
| 283 | int xcast_mode; |
| 284 | |
| 285 | /* no need to update the harwdare if we are not running */ |
| 286 | if (!(dev->flags & IFF_UP)) |
| 287 | return; |
| 288 | |
| 289 | /* determine new mode based on flags */ |
| 290 | xcast_mode = (dev->flags & IFF_PROMISC) ? FM10K_XCAST_MODE_PROMISC : |
| 291 | (dev->flags & IFF_ALLMULTI) ? FM10K_XCAST_MODE_ALLMULTI : |
| 292 | (dev->flags & (IFF_BROADCAST | IFF_MULTICAST)) ? |
| 293 | FM10K_XCAST_MODE_MULTI : FM10K_XCAST_MODE_NONE; |
| 294 | |
| 295 | fm10k_mbx_lock(interface); |
| 296 | |
| 297 | /* syncronize all of the addresses */ |
| 298 | if (xcast_mode != FM10K_XCAST_MODE_PROMISC) { |
| 299 | __dev_uc_sync(dev, fm10k_uc_sync, fm10k_uc_unsync); |
| 300 | if (xcast_mode != FM10K_XCAST_MODE_ALLMULTI) |
| 301 | __dev_mc_sync(dev, fm10k_mc_sync, fm10k_mc_unsync); |
| 302 | } |
| 303 | |
| 304 | /* if we aren't changing modes there is nothing to do */ |
| 305 | if (interface->xcast_mode != xcast_mode) { |
| 306 | /* update VLAN table */ |
| 307 | if (xcast_mode == FM10K_XCAST_MODE_PROMISC) |
| 308 | hw->mac.ops.update_vlan(hw, FM10K_VLAN_ALL, 0, true); |
| 309 | if (interface->xcast_mode == FM10K_XCAST_MODE_PROMISC) |
| 310 | fm10k_clear_unused_vlans(interface); |
| 311 | |
| 312 | /* update xcast mode */ |
| 313 | hw->mac.ops.update_xcast_mode(hw, interface->glort, xcast_mode); |
| 314 | |
| 315 | /* record updated xcast mode state */ |
| 316 | interface->xcast_mode = xcast_mode; |
| 317 | } |
| 318 | |
| 319 | fm10k_mbx_unlock(interface); |
| 320 | } |
| 321 | |
| 322 | void fm10k_restore_rx_state(struct fm10k_intfc *interface) |
| 323 | { |
| 324 | struct net_device *netdev = interface->netdev; |
| 325 | struct fm10k_hw *hw = &interface->hw; |
| 326 | int xcast_mode; |
| 327 | u16 vid, glort; |
| 328 | |
| 329 | /* record glort for this interface */ |
| 330 | glort = interface->glort; |
| 331 | |
| 332 | /* convert interface flags to xcast mode */ |
| 333 | if (netdev->flags & IFF_PROMISC) |
| 334 | xcast_mode = FM10K_XCAST_MODE_PROMISC; |
| 335 | else if (netdev->flags & IFF_ALLMULTI) |
| 336 | xcast_mode = FM10K_XCAST_MODE_ALLMULTI; |
| 337 | else if (netdev->flags & (IFF_BROADCAST | IFF_MULTICAST)) |
| 338 | xcast_mode = FM10K_XCAST_MODE_MULTI; |
| 339 | else |
| 340 | xcast_mode = FM10K_XCAST_MODE_NONE; |
| 341 | |
| 342 | fm10k_mbx_lock(interface); |
| 343 | |
| 344 | /* Enable logical port */ |
| 345 | hw->mac.ops.update_lport_state(hw, glort, interface->glort_count, true); |
| 346 | |
| 347 | /* update VLAN table */ |
| 348 | hw->mac.ops.update_vlan(hw, FM10K_VLAN_ALL, 0, |
| 349 | xcast_mode == FM10K_XCAST_MODE_PROMISC); |
| 350 | |
| 351 | /* Add filter for VLAN 0 */ |
| 352 | hw->mac.ops.update_vlan(hw, 0, 0, true); |
| 353 | |
| 354 | /* update table with current entries */ |
| 355 | for (vid = hw->mac.default_vid ? fm10k_find_next_vlan(interface, 0) : 0; |
| 356 | vid < VLAN_N_VID; |
| 357 | vid = fm10k_find_next_vlan(interface, vid)) { |
| 358 | hw->mac.ops.update_vlan(hw, vid, 0, true); |
| 359 | hw->mac.ops.update_uc_addr(hw, glort, hw->mac.addr, |
| 360 | vid, true, 0); |
| 361 | } |
| 362 | |
| 363 | /* syncronize all of the addresses */ |
| 364 | if (xcast_mode != FM10K_XCAST_MODE_PROMISC) { |
| 365 | __dev_uc_sync(netdev, fm10k_uc_sync, fm10k_uc_unsync); |
| 366 | if (xcast_mode != FM10K_XCAST_MODE_ALLMULTI) |
| 367 | __dev_mc_sync(netdev, fm10k_mc_sync, fm10k_mc_unsync); |
| 368 | } |
| 369 | |
| 370 | /* update xcast mode */ |
| 371 | hw->mac.ops.update_xcast_mode(hw, glort, xcast_mode); |
| 372 | |
| 373 | fm10k_mbx_unlock(interface); |
| 374 | |
| 375 | /* record updated xcast mode state */ |
| 376 | interface->xcast_mode = xcast_mode; |
| 377 | } |
| 378 | |
| 379 | void fm10k_reset_rx_state(struct fm10k_intfc *interface) |
| 380 | { |
| 381 | struct net_device *netdev = interface->netdev; |
| 382 | struct fm10k_hw *hw = &interface->hw; |
| 383 | |
| 384 | fm10k_mbx_lock(interface); |
| 385 | |
| 386 | /* clear the logical port state on lower device */ |
| 387 | hw->mac.ops.update_lport_state(hw, interface->glort, |
| 388 | interface->glort_count, false); |
| 389 | |
| 390 | fm10k_mbx_unlock(interface); |
| 391 | |
| 392 | /* reset flags to default state */ |
| 393 | interface->xcast_mode = FM10K_XCAST_MODE_NONE; |
| 394 | |
| 395 | /* clear the sync flag since the lport has been dropped */ |
| 396 | __dev_uc_unsync(netdev, NULL); |
| 397 | __dev_mc_unsync(netdev, NULL); |
Alexander Duyck | 0e7b364 | 2014-09-20 19:48:10 -0400 | [diff] [blame] | 398 | } |
| 399 | |
| 400 | static const struct net_device_ops fm10k_netdev_ops = { |
| 401 | .ndo_validate_addr = eth_validate_addr, |
| 402 | .ndo_start_xmit = fm10k_xmit_frame, |
| 403 | .ndo_set_mac_address = fm10k_set_mac, |
| 404 | .ndo_change_mtu = fm10k_change_mtu, |
Alexander Duyck | 8f5e20d | 2014-09-20 19:48:20 -0400 | [diff] [blame^] | 405 | .ndo_vlan_rx_add_vid = fm10k_vlan_rx_add_vid, |
| 406 | .ndo_vlan_rx_kill_vid = fm10k_vlan_rx_kill_vid, |
Alexander Duyck | 0e7b364 | 2014-09-20 19:48:10 -0400 | [diff] [blame] | 407 | .ndo_set_rx_mode = fm10k_set_rx_mode, |
| 408 | }; |
| 409 | |
| 410 | #define DEFAULT_DEBUG_LEVEL_SHIFT 3 |
| 411 | |
| 412 | struct net_device *fm10k_alloc_netdev(void) |
| 413 | { |
| 414 | struct fm10k_intfc *interface; |
| 415 | struct net_device *dev; |
| 416 | |
| 417 | dev = alloc_etherdev(sizeof(struct fm10k_intfc)); |
| 418 | if (!dev) |
| 419 | return NULL; |
| 420 | |
| 421 | /* set net device and ethtool ops */ |
| 422 | dev->netdev_ops = &fm10k_netdev_ops; |
| 423 | |
| 424 | /* configure default debug level */ |
| 425 | interface = netdev_priv(dev); |
| 426 | interface->msg_enable = (1 << DEFAULT_DEBUG_LEVEL_SHIFT) - 1; |
| 427 | |
| 428 | /* configure default features */ |
| 429 | dev->features |= NETIF_F_SG; |
| 430 | |
| 431 | /* all features defined to this point should be changeable */ |
| 432 | dev->hw_features |= dev->features; |
| 433 | |
| 434 | /* configure VLAN features */ |
| 435 | dev->vlan_features |= dev->features; |
| 436 | |
| 437 | /* configure tunnel offloads */ |
| 438 | dev->hw_enc_features = NETIF_F_SG; |
| 439 | |
Alexander Duyck | 8f5e20d | 2014-09-20 19:48:20 -0400 | [diff] [blame^] | 440 | /* we want to leave these both on as we cannot disable VLAN tag |
| 441 | * insertion or stripping on the hardware since it is contained |
| 442 | * in the FTAG and not in the frame itself. |
| 443 | */ |
| 444 | dev->features |= NETIF_F_HW_VLAN_CTAG_TX | |
| 445 | NETIF_F_HW_VLAN_CTAG_RX | |
| 446 | NETIF_F_HW_VLAN_CTAG_FILTER; |
| 447 | |
| 448 | dev->priv_flags |= IFF_UNICAST_FLT; |
| 449 | |
Alexander Duyck | 0e7b364 | 2014-09-20 19:48:10 -0400 | [diff] [blame] | 450 | return dev; |
| 451 | } |