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" |
Alexander Duyck | 3abaae4 | 2014-09-20 19:49:43 -0400 | [diff] [blame^] | 22 | #include <linux/vmalloc.h> |
| 23 | |
| 24 | /** |
| 25 | * fm10k_setup_tx_resources - allocate Tx resources (Descriptors) |
| 26 | * @tx_ring: tx descriptor ring (for a specific queue) to setup |
| 27 | * |
| 28 | * Return 0 on success, negative on failure |
| 29 | **/ |
| 30 | int fm10k_setup_tx_resources(struct fm10k_ring *tx_ring) |
| 31 | { |
| 32 | struct device *dev = tx_ring->dev; |
| 33 | int size; |
| 34 | |
| 35 | size = sizeof(struct fm10k_tx_buffer) * tx_ring->count; |
| 36 | |
| 37 | tx_ring->tx_buffer = vzalloc(size); |
| 38 | if (!tx_ring->tx_buffer) |
| 39 | goto err; |
| 40 | |
| 41 | u64_stats_init(&tx_ring->syncp); |
| 42 | |
| 43 | /* round up to nearest 4K */ |
| 44 | tx_ring->size = tx_ring->count * sizeof(struct fm10k_tx_desc); |
| 45 | tx_ring->size = ALIGN(tx_ring->size, 4096); |
| 46 | |
| 47 | tx_ring->desc = dma_alloc_coherent(dev, tx_ring->size, |
| 48 | &tx_ring->dma, GFP_KERNEL); |
| 49 | if (!tx_ring->desc) |
| 50 | goto err; |
| 51 | |
| 52 | return 0; |
| 53 | |
| 54 | err: |
| 55 | vfree(tx_ring->tx_buffer); |
| 56 | tx_ring->tx_buffer = NULL; |
| 57 | return -ENOMEM; |
| 58 | } |
| 59 | |
| 60 | /** |
| 61 | * fm10k_setup_all_tx_resources - allocate all queues Tx resources |
| 62 | * @interface: board private structure |
| 63 | * |
| 64 | * If this function returns with an error, then it's possible one or |
| 65 | * more of the rings is populated (while the rest are not). It is the |
| 66 | * callers duty to clean those orphaned rings. |
| 67 | * |
| 68 | * Return 0 on success, negative on failure |
| 69 | **/ |
| 70 | static int fm10k_setup_all_tx_resources(struct fm10k_intfc *interface) |
| 71 | { |
| 72 | int i, err = 0; |
| 73 | |
| 74 | for (i = 0; i < interface->num_tx_queues; i++) { |
| 75 | err = fm10k_setup_tx_resources(interface->tx_ring[i]); |
| 76 | if (!err) |
| 77 | continue; |
| 78 | |
| 79 | netif_err(interface, probe, interface->netdev, |
| 80 | "Allocation for Tx Queue %u failed\n", i); |
| 81 | goto err_setup_tx; |
| 82 | } |
| 83 | |
| 84 | return 0; |
| 85 | err_setup_tx: |
| 86 | /* rewind the index freeing the rings as we go */ |
| 87 | while (i--) |
| 88 | fm10k_free_tx_resources(interface->tx_ring[i]); |
| 89 | return err; |
| 90 | } |
| 91 | |
| 92 | /** |
| 93 | * fm10k_setup_rx_resources - allocate Rx resources (Descriptors) |
| 94 | * @rx_ring: rx descriptor ring (for a specific queue) to setup |
| 95 | * |
| 96 | * Returns 0 on success, negative on failure |
| 97 | **/ |
| 98 | int fm10k_setup_rx_resources(struct fm10k_ring *rx_ring) |
| 99 | { |
| 100 | struct device *dev = rx_ring->dev; |
| 101 | int size; |
| 102 | |
| 103 | size = sizeof(struct fm10k_rx_buffer) * rx_ring->count; |
| 104 | |
| 105 | rx_ring->rx_buffer = vzalloc(size); |
| 106 | if (!rx_ring->rx_buffer) |
| 107 | goto err; |
| 108 | |
| 109 | u64_stats_init(&rx_ring->syncp); |
| 110 | |
| 111 | /* Round up to nearest 4K */ |
| 112 | rx_ring->size = rx_ring->count * sizeof(union fm10k_rx_desc); |
| 113 | rx_ring->size = ALIGN(rx_ring->size, 4096); |
| 114 | |
| 115 | rx_ring->desc = dma_alloc_coherent(dev, rx_ring->size, |
| 116 | &rx_ring->dma, GFP_KERNEL); |
| 117 | if (!rx_ring->desc) |
| 118 | goto err; |
| 119 | |
| 120 | return 0; |
| 121 | err: |
| 122 | vfree(rx_ring->rx_buffer); |
| 123 | rx_ring->rx_buffer = NULL; |
| 124 | return -ENOMEM; |
| 125 | } |
| 126 | |
| 127 | /** |
| 128 | * fm10k_setup_all_rx_resources - allocate all queues Rx resources |
| 129 | * @interface: board private structure |
| 130 | * |
| 131 | * If this function returns with an error, then it's possible one or |
| 132 | * more of the rings is populated (while the rest are not). It is the |
| 133 | * callers duty to clean those orphaned rings. |
| 134 | * |
| 135 | * Return 0 on success, negative on failure |
| 136 | **/ |
| 137 | static int fm10k_setup_all_rx_resources(struct fm10k_intfc *interface) |
| 138 | { |
| 139 | int i, err = 0; |
| 140 | |
| 141 | for (i = 0; i < interface->num_rx_queues; i++) { |
| 142 | err = fm10k_setup_rx_resources(interface->rx_ring[i]); |
| 143 | if (!err) |
| 144 | continue; |
| 145 | |
| 146 | netif_err(interface, probe, interface->netdev, |
| 147 | "Allocation for Rx Queue %u failed\n", i); |
| 148 | goto err_setup_rx; |
| 149 | } |
| 150 | |
| 151 | return 0; |
| 152 | err_setup_rx: |
| 153 | /* rewind the index freeing the rings as we go */ |
| 154 | while (i--) |
| 155 | fm10k_free_rx_resources(interface->rx_ring[i]); |
| 156 | return err; |
| 157 | } |
| 158 | |
| 159 | void fm10k_unmap_and_free_tx_resource(struct fm10k_ring *ring, |
| 160 | struct fm10k_tx_buffer *tx_buffer) |
| 161 | { |
| 162 | if (tx_buffer->skb) { |
| 163 | dev_kfree_skb_any(tx_buffer->skb); |
| 164 | if (dma_unmap_len(tx_buffer, len)) |
| 165 | dma_unmap_single(ring->dev, |
| 166 | dma_unmap_addr(tx_buffer, dma), |
| 167 | dma_unmap_len(tx_buffer, len), |
| 168 | DMA_TO_DEVICE); |
| 169 | } else if (dma_unmap_len(tx_buffer, len)) { |
| 170 | dma_unmap_page(ring->dev, |
| 171 | dma_unmap_addr(tx_buffer, dma), |
| 172 | dma_unmap_len(tx_buffer, len), |
| 173 | DMA_TO_DEVICE); |
| 174 | } |
| 175 | tx_buffer->next_to_watch = NULL; |
| 176 | tx_buffer->skb = NULL; |
| 177 | dma_unmap_len_set(tx_buffer, len, 0); |
| 178 | /* tx_buffer must be completely set up in the transmit path */ |
| 179 | } |
| 180 | |
| 181 | /** |
| 182 | * fm10k_clean_tx_ring - Free Tx Buffers |
| 183 | * @tx_ring: ring to be cleaned |
| 184 | **/ |
| 185 | static void fm10k_clean_tx_ring(struct fm10k_ring *tx_ring) |
| 186 | { |
| 187 | struct fm10k_tx_buffer *tx_buffer; |
| 188 | unsigned long size; |
| 189 | u16 i; |
| 190 | |
| 191 | /* ring already cleared, nothing to do */ |
| 192 | if (!tx_ring->tx_buffer) |
| 193 | return; |
| 194 | |
| 195 | /* Free all the Tx ring sk_buffs */ |
| 196 | for (i = 0; i < tx_ring->count; i++) { |
| 197 | tx_buffer = &tx_ring->tx_buffer[i]; |
| 198 | fm10k_unmap_and_free_tx_resource(tx_ring, tx_buffer); |
| 199 | } |
| 200 | |
| 201 | /* reset BQL values */ |
| 202 | netdev_tx_reset_queue(txring_txq(tx_ring)); |
| 203 | |
| 204 | size = sizeof(struct fm10k_tx_buffer) * tx_ring->count; |
| 205 | memset(tx_ring->tx_buffer, 0, size); |
| 206 | |
| 207 | /* Zero out the descriptor ring */ |
| 208 | memset(tx_ring->desc, 0, tx_ring->size); |
| 209 | } |
| 210 | |
| 211 | /** |
| 212 | * fm10k_free_tx_resources - Free Tx Resources per Queue |
| 213 | * @tx_ring: Tx descriptor ring for a specific queue |
| 214 | * |
| 215 | * Free all transmit software resources |
| 216 | **/ |
| 217 | void fm10k_free_tx_resources(struct fm10k_ring *tx_ring) |
| 218 | { |
| 219 | fm10k_clean_tx_ring(tx_ring); |
| 220 | |
| 221 | vfree(tx_ring->tx_buffer); |
| 222 | tx_ring->tx_buffer = NULL; |
| 223 | |
| 224 | /* if not set, then don't free */ |
| 225 | if (!tx_ring->desc) |
| 226 | return; |
| 227 | |
| 228 | dma_free_coherent(tx_ring->dev, tx_ring->size, |
| 229 | tx_ring->desc, tx_ring->dma); |
| 230 | tx_ring->desc = NULL; |
| 231 | } |
| 232 | |
| 233 | /** |
| 234 | * fm10k_clean_all_tx_rings - Free Tx Buffers for all queues |
| 235 | * @interface: board private structure |
| 236 | **/ |
| 237 | void fm10k_clean_all_tx_rings(struct fm10k_intfc *interface) |
| 238 | { |
| 239 | int i; |
| 240 | |
| 241 | for (i = 0; i < interface->num_tx_queues; i++) |
| 242 | fm10k_clean_tx_ring(interface->tx_ring[i]); |
| 243 | } |
| 244 | |
| 245 | /** |
| 246 | * fm10k_free_all_tx_resources - Free Tx Resources for All Queues |
| 247 | * @interface: board private structure |
| 248 | * |
| 249 | * Free all transmit software resources |
| 250 | **/ |
| 251 | static void fm10k_free_all_tx_resources(struct fm10k_intfc *interface) |
| 252 | { |
| 253 | int i = interface->num_tx_queues; |
| 254 | |
| 255 | while (i--) |
| 256 | fm10k_free_tx_resources(interface->tx_ring[i]); |
| 257 | } |
| 258 | |
| 259 | /** |
| 260 | * fm10k_clean_rx_ring - Free Rx Buffers per Queue |
| 261 | * @rx_ring: ring to free buffers from |
| 262 | **/ |
| 263 | static void fm10k_clean_rx_ring(struct fm10k_ring *rx_ring) |
| 264 | { |
| 265 | unsigned long size; |
| 266 | u16 i; |
| 267 | |
| 268 | if (!rx_ring->rx_buffer) |
| 269 | return; |
| 270 | |
| 271 | if (rx_ring->skb) |
| 272 | dev_kfree_skb(rx_ring->skb); |
| 273 | rx_ring->skb = NULL; |
| 274 | |
| 275 | /* Free all the Rx ring sk_buffs */ |
| 276 | for (i = 0; i < rx_ring->count; i++) { |
| 277 | struct fm10k_rx_buffer *buffer = &rx_ring->rx_buffer[i]; |
| 278 | /* clean-up will only set page pointer to NULL */ |
| 279 | if (!buffer->page) |
| 280 | continue; |
| 281 | |
| 282 | dma_unmap_page(rx_ring->dev, buffer->dma, |
| 283 | PAGE_SIZE, DMA_FROM_DEVICE); |
| 284 | __free_page(buffer->page); |
| 285 | |
| 286 | buffer->page = NULL; |
| 287 | } |
| 288 | |
| 289 | size = sizeof(struct fm10k_rx_buffer) * rx_ring->count; |
| 290 | memset(rx_ring->rx_buffer, 0, size); |
| 291 | |
| 292 | /* Zero out the descriptor ring */ |
| 293 | memset(rx_ring->desc, 0, rx_ring->size); |
| 294 | |
| 295 | rx_ring->next_to_alloc = 0; |
| 296 | rx_ring->next_to_clean = 0; |
| 297 | rx_ring->next_to_use = 0; |
| 298 | } |
| 299 | |
| 300 | /** |
| 301 | * fm10k_free_rx_resources - Free Rx Resources |
| 302 | * @rx_ring: ring to clean the resources from |
| 303 | * |
| 304 | * Free all receive software resources |
| 305 | **/ |
| 306 | void fm10k_free_rx_resources(struct fm10k_ring *rx_ring) |
| 307 | { |
| 308 | fm10k_clean_rx_ring(rx_ring); |
| 309 | |
| 310 | vfree(rx_ring->rx_buffer); |
| 311 | rx_ring->rx_buffer = NULL; |
| 312 | |
| 313 | /* if not set, then don't free */ |
| 314 | if (!rx_ring->desc) |
| 315 | return; |
| 316 | |
| 317 | dma_free_coherent(rx_ring->dev, rx_ring->size, |
| 318 | rx_ring->desc, rx_ring->dma); |
| 319 | |
| 320 | rx_ring->desc = NULL; |
| 321 | } |
| 322 | |
| 323 | /** |
| 324 | * fm10k_clean_all_rx_rings - Free Rx Buffers for all queues |
| 325 | * @interface: board private structure |
| 326 | **/ |
| 327 | void fm10k_clean_all_rx_rings(struct fm10k_intfc *interface) |
| 328 | { |
| 329 | int i; |
| 330 | |
| 331 | for (i = 0; i < interface->num_rx_queues; i++) |
| 332 | fm10k_clean_rx_ring(interface->rx_ring[i]); |
| 333 | } |
| 334 | |
| 335 | /** |
| 336 | * fm10k_free_all_rx_resources - Free Rx Resources for All Queues |
| 337 | * @interface: board private structure |
| 338 | * |
| 339 | * Free all receive software resources |
| 340 | **/ |
| 341 | static void fm10k_free_all_rx_resources(struct fm10k_intfc *interface) |
| 342 | { |
| 343 | int i = interface->num_rx_queues; |
| 344 | |
| 345 | while (i--) |
| 346 | fm10k_free_rx_resources(interface->rx_ring[i]); |
| 347 | } |
Alexander Duyck | 0e7b364 | 2014-09-20 19:48:10 -0400 | [diff] [blame] | 348 | |
Alexander Duyck | 504c5ea | 2014-09-20 19:48:29 -0400 | [diff] [blame] | 349 | /** |
| 350 | * fm10k_request_glort_range - Request GLORTs for use in configuring rules |
| 351 | * @interface: board private structure |
| 352 | * |
| 353 | * This function allocates a range of glorts for this inteface to use. |
| 354 | **/ |
| 355 | static void fm10k_request_glort_range(struct fm10k_intfc *interface) |
| 356 | { |
| 357 | struct fm10k_hw *hw = &interface->hw; |
| 358 | u16 mask = (~hw->mac.dglort_map) >> FM10K_DGLORTMAP_MASK_SHIFT; |
| 359 | |
| 360 | /* establish GLORT base */ |
| 361 | interface->glort = hw->mac.dglort_map & FM10K_DGLORTMAP_NONE; |
| 362 | interface->glort_count = 0; |
| 363 | |
| 364 | /* nothing we can do until mask is allocated */ |
| 365 | if (hw->mac.dglort_map == FM10K_DGLORTMAP_NONE) |
| 366 | return; |
| 367 | |
| 368 | interface->glort_count = mask + 1; |
| 369 | } |
| 370 | |
| 371 | /** |
| 372 | * fm10k_open - Called when a network interface is made active |
| 373 | * @netdev: network interface device structure |
| 374 | * |
| 375 | * Returns 0 on success, negative value on failure |
| 376 | * |
| 377 | * The open entry point is called when a network interface is made |
| 378 | * active by the system (IFF_UP). At this point all resources needed |
| 379 | * for transmit and receive operations are allocated, the interrupt |
| 380 | * handler is registered with the OS, the watchdog timer is started, |
| 381 | * and the stack is notified that the interface is ready. |
| 382 | **/ |
| 383 | int fm10k_open(struct net_device *netdev) |
| 384 | { |
| 385 | struct fm10k_intfc *interface = netdev_priv(netdev); |
Alexander Duyck | 18283ca | 2014-09-20 19:48:51 -0400 | [diff] [blame] | 386 | int err; |
| 387 | |
Alexander Duyck | 3abaae4 | 2014-09-20 19:49:43 -0400 | [diff] [blame^] | 388 | /* allocate transmit descriptors */ |
| 389 | err = fm10k_setup_all_tx_resources(interface); |
| 390 | if (err) |
| 391 | goto err_setup_tx; |
| 392 | |
| 393 | /* allocate receive descriptors */ |
| 394 | err = fm10k_setup_all_rx_resources(interface); |
| 395 | if (err) |
| 396 | goto err_setup_rx; |
| 397 | |
Alexander Duyck | 18283ca | 2014-09-20 19:48:51 -0400 | [diff] [blame] | 398 | /* allocate interrupt resources */ |
| 399 | err = fm10k_qv_request_irq(interface); |
| 400 | if (err) |
| 401 | goto err_req_irq; |
Alexander Duyck | 504c5ea | 2014-09-20 19:48:29 -0400 | [diff] [blame] | 402 | |
| 403 | /* setup GLORT assignment for this port */ |
| 404 | fm10k_request_glort_range(interface); |
| 405 | |
Alexander Duyck | e27ef59 | 2014-09-20 19:49:03 -0400 | [diff] [blame] | 406 | /* Notify the stack of the actual queue counts */ |
| 407 | |
| 408 | err = netif_set_real_num_rx_queues(netdev, |
| 409 | interface->num_rx_queues); |
| 410 | if (err) |
| 411 | goto err_set_queues; |
| 412 | |
Alexander Duyck | 504c5ea | 2014-09-20 19:48:29 -0400 | [diff] [blame] | 413 | fm10k_up(interface); |
| 414 | |
| 415 | return 0; |
Alexander Duyck | 18283ca | 2014-09-20 19:48:51 -0400 | [diff] [blame] | 416 | |
Alexander Duyck | e27ef59 | 2014-09-20 19:49:03 -0400 | [diff] [blame] | 417 | err_set_queues: |
| 418 | fm10k_qv_free_irq(interface); |
Alexander Duyck | 18283ca | 2014-09-20 19:48:51 -0400 | [diff] [blame] | 419 | err_req_irq: |
Alexander Duyck | 3abaae4 | 2014-09-20 19:49:43 -0400 | [diff] [blame^] | 420 | fm10k_free_all_rx_resources(interface); |
| 421 | err_setup_rx: |
| 422 | fm10k_free_all_tx_resources(interface); |
| 423 | err_setup_tx: |
Alexander Duyck | 18283ca | 2014-09-20 19:48:51 -0400 | [diff] [blame] | 424 | return err; |
Alexander Duyck | 504c5ea | 2014-09-20 19:48:29 -0400 | [diff] [blame] | 425 | } |
| 426 | |
| 427 | /** |
| 428 | * fm10k_close - Disables a network interface |
| 429 | * @netdev: network interface device structure |
| 430 | * |
| 431 | * Returns 0, this is not allowed to fail |
| 432 | * |
| 433 | * The close entry point is called when an interface is de-activated |
| 434 | * by the OS. The hardware is still under the drivers control, but |
| 435 | * needs to be disabled. A global MAC reset is issued to stop the |
| 436 | * hardware, and all transmit and receive resources are freed. |
| 437 | **/ |
| 438 | int fm10k_close(struct net_device *netdev) |
| 439 | { |
| 440 | struct fm10k_intfc *interface = netdev_priv(netdev); |
| 441 | |
| 442 | fm10k_down(interface); |
| 443 | |
Alexander Duyck | 18283ca | 2014-09-20 19:48:51 -0400 | [diff] [blame] | 444 | fm10k_qv_free_irq(interface); |
| 445 | |
Alexander Duyck | 3abaae4 | 2014-09-20 19:49:43 -0400 | [diff] [blame^] | 446 | fm10k_free_all_tx_resources(interface); |
| 447 | fm10k_free_all_rx_resources(interface); |
| 448 | |
Alexander Duyck | 504c5ea | 2014-09-20 19:48:29 -0400 | [diff] [blame] | 449 | return 0; |
| 450 | } |
| 451 | |
Alexander Duyck | 0e7b364 | 2014-09-20 19:48:10 -0400 | [diff] [blame] | 452 | static netdev_tx_t fm10k_xmit_frame(struct sk_buff *skb, struct net_device *dev) |
| 453 | { |
| 454 | dev_kfree_skb_any(skb); |
| 455 | return NETDEV_TX_OK; |
| 456 | } |
| 457 | |
| 458 | static int fm10k_change_mtu(struct net_device *dev, int new_mtu) |
| 459 | { |
| 460 | if (new_mtu < 68 || new_mtu > FM10K_MAX_JUMBO_FRAME_SIZE) |
| 461 | return -EINVAL; |
| 462 | |
| 463 | dev->mtu = new_mtu; |
| 464 | |
| 465 | return 0; |
| 466 | } |
| 467 | |
Alexander Duyck | 8f5e20d | 2014-09-20 19:48:20 -0400 | [diff] [blame] | 468 | static int fm10k_uc_vlan_unsync(struct net_device *netdev, |
| 469 | const unsigned char *uc_addr) |
| 470 | { |
| 471 | struct fm10k_intfc *interface = netdev_priv(netdev); |
| 472 | struct fm10k_hw *hw = &interface->hw; |
| 473 | u16 glort = interface->glort; |
| 474 | u16 vid = interface->vid; |
| 475 | bool set = !!(vid / VLAN_N_VID); |
| 476 | int err; |
| 477 | |
| 478 | /* drop any leading bits on the VLAN ID */ |
| 479 | vid &= VLAN_N_VID - 1; |
| 480 | |
| 481 | err = hw->mac.ops.update_uc_addr(hw, glort, uc_addr, vid, set, 0); |
| 482 | if (err) |
| 483 | return err; |
| 484 | |
| 485 | /* return non-zero value as we are only doing a partial sync/unsync */ |
| 486 | return 1; |
| 487 | } |
| 488 | |
| 489 | static int fm10k_mc_vlan_unsync(struct net_device *netdev, |
| 490 | const unsigned char *mc_addr) |
| 491 | { |
| 492 | struct fm10k_intfc *interface = netdev_priv(netdev); |
| 493 | struct fm10k_hw *hw = &interface->hw; |
| 494 | u16 glort = interface->glort; |
| 495 | u16 vid = interface->vid; |
| 496 | bool set = !!(vid / VLAN_N_VID); |
| 497 | int err; |
| 498 | |
| 499 | /* drop any leading bits on the VLAN ID */ |
| 500 | vid &= VLAN_N_VID - 1; |
| 501 | |
| 502 | err = hw->mac.ops.update_mc_addr(hw, glort, mc_addr, vid, set); |
| 503 | if (err) |
| 504 | return err; |
| 505 | |
| 506 | /* return non-zero value as we are only doing a partial sync/unsync */ |
| 507 | return 1; |
| 508 | } |
| 509 | |
| 510 | static int fm10k_update_vid(struct net_device *netdev, u16 vid, bool set) |
| 511 | { |
| 512 | struct fm10k_intfc *interface = netdev_priv(netdev); |
| 513 | struct fm10k_hw *hw = &interface->hw; |
| 514 | s32 err; |
| 515 | |
| 516 | /* updates do not apply to VLAN 0 */ |
| 517 | if (!vid) |
| 518 | return 0; |
| 519 | |
| 520 | if (vid >= VLAN_N_VID) |
| 521 | return -EINVAL; |
| 522 | |
| 523 | /* Verify we have permission to add VLANs */ |
| 524 | if (hw->mac.vlan_override) |
| 525 | return -EACCES; |
| 526 | |
| 527 | /* if default VLAN is already present do nothing */ |
| 528 | if (vid == hw->mac.default_vid) |
| 529 | return -EBUSY; |
| 530 | |
| 531 | /* update active_vlans bitmask */ |
| 532 | set_bit(vid, interface->active_vlans); |
| 533 | if (!set) |
| 534 | clear_bit(vid, interface->active_vlans); |
| 535 | |
| 536 | fm10k_mbx_lock(interface); |
| 537 | |
| 538 | /* only need to update the VLAN if not in promiscous mode */ |
| 539 | if (!(netdev->flags & IFF_PROMISC)) { |
| 540 | err = hw->mac.ops.update_vlan(hw, vid, 0, set); |
| 541 | if (err) |
| 542 | return err; |
| 543 | } |
| 544 | |
| 545 | /* update our base MAC address */ |
| 546 | err = hw->mac.ops.update_uc_addr(hw, interface->glort, hw->mac.addr, |
| 547 | vid, set, 0); |
| 548 | if (err) |
| 549 | return err; |
| 550 | |
| 551 | /* set vid prior to syncing/unsyncing the VLAN */ |
| 552 | interface->vid = vid + (set ? VLAN_N_VID : 0); |
| 553 | |
| 554 | /* Update the unicast and multicast address list to add/drop VLAN */ |
| 555 | __dev_uc_unsync(netdev, fm10k_uc_vlan_unsync); |
| 556 | __dev_mc_unsync(netdev, fm10k_mc_vlan_unsync); |
| 557 | |
| 558 | fm10k_mbx_unlock(interface); |
| 559 | |
| 560 | return 0; |
| 561 | } |
| 562 | |
| 563 | static int fm10k_vlan_rx_add_vid(struct net_device *netdev, |
| 564 | __always_unused __be16 proto, u16 vid) |
| 565 | { |
| 566 | /* update VLAN and address table based on changes */ |
| 567 | return fm10k_update_vid(netdev, vid, true); |
| 568 | } |
| 569 | |
| 570 | static int fm10k_vlan_rx_kill_vid(struct net_device *netdev, |
| 571 | __always_unused __be16 proto, u16 vid) |
| 572 | { |
| 573 | /* update VLAN and address table based on changes */ |
| 574 | return fm10k_update_vid(netdev, vid, false); |
| 575 | } |
| 576 | |
| 577 | static u16 fm10k_find_next_vlan(struct fm10k_intfc *interface, u16 vid) |
| 578 | { |
| 579 | struct fm10k_hw *hw = &interface->hw; |
| 580 | u16 default_vid = hw->mac.default_vid; |
| 581 | u16 vid_limit = vid < default_vid ? default_vid : VLAN_N_VID; |
| 582 | |
| 583 | vid = find_next_bit(interface->active_vlans, vid_limit, ++vid); |
| 584 | |
| 585 | return vid; |
| 586 | } |
| 587 | |
| 588 | static void fm10k_clear_unused_vlans(struct fm10k_intfc *interface) |
| 589 | { |
| 590 | struct fm10k_hw *hw = &interface->hw; |
| 591 | u32 vid, prev_vid; |
| 592 | |
| 593 | /* loop through and find any gaps in the table */ |
| 594 | for (vid = 0, prev_vid = 0; |
| 595 | prev_vid < VLAN_N_VID; |
| 596 | prev_vid = vid + 1, vid = fm10k_find_next_vlan(interface, vid)) { |
| 597 | if (prev_vid == vid) |
| 598 | continue; |
| 599 | |
| 600 | /* send request to clear multiple bits at a time */ |
| 601 | prev_vid += (vid - prev_vid - 1) << FM10K_VLAN_LENGTH_SHIFT; |
| 602 | hw->mac.ops.update_vlan(hw, prev_vid, 0, false); |
| 603 | } |
| 604 | } |
| 605 | |
| 606 | static int __fm10k_uc_sync(struct net_device *dev, |
| 607 | const unsigned char *addr, bool sync) |
| 608 | { |
| 609 | struct fm10k_intfc *interface = netdev_priv(dev); |
| 610 | struct fm10k_hw *hw = &interface->hw; |
| 611 | u16 vid, glort = interface->glort; |
| 612 | s32 err; |
| 613 | |
| 614 | if (!is_valid_ether_addr(addr)) |
| 615 | return -EADDRNOTAVAIL; |
| 616 | |
| 617 | /* update table with current entries */ |
| 618 | for (vid = hw->mac.default_vid ? fm10k_find_next_vlan(interface, 0) : 0; |
| 619 | vid < VLAN_N_VID; |
| 620 | vid = fm10k_find_next_vlan(interface, vid)) { |
| 621 | err = hw->mac.ops.update_uc_addr(hw, glort, addr, |
| 622 | vid, sync, 0); |
| 623 | if (err) |
| 624 | return err; |
| 625 | } |
| 626 | |
| 627 | return 0; |
| 628 | } |
| 629 | |
| 630 | static int fm10k_uc_sync(struct net_device *dev, |
| 631 | const unsigned char *addr) |
| 632 | { |
| 633 | return __fm10k_uc_sync(dev, addr, true); |
| 634 | } |
| 635 | |
| 636 | static int fm10k_uc_unsync(struct net_device *dev, |
| 637 | const unsigned char *addr) |
| 638 | { |
| 639 | return __fm10k_uc_sync(dev, addr, false); |
| 640 | } |
| 641 | |
Alexander Duyck | 0e7b364 | 2014-09-20 19:48:10 -0400 | [diff] [blame] | 642 | static int fm10k_set_mac(struct net_device *dev, void *p) |
| 643 | { |
Alexander Duyck | 8f5e20d | 2014-09-20 19:48:20 -0400 | [diff] [blame] | 644 | struct fm10k_intfc *interface = netdev_priv(dev); |
| 645 | struct fm10k_hw *hw = &interface->hw; |
Alexander Duyck | 0e7b364 | 2014-09-20 19:48:10 -0400 | [diff] [blame] | 646 | struct sockaddr *addr = p; |
| 647 | s32 err = 0; |
| 648 | |
| 649 | if (!is_valid_ether_addr(addr->sa_data)) |
| 650 | return -EADDRNOTAVAIL; |
| 651 | |
Alexander Duyck | 8f5e20d | 2014-09-20 19:48:20 -0400 | [diff] [blame] | 652 | if (dev->flags & IFF_UP) { |
| 653 | /* setting MAC address requires mailbox */ |
| 654 | fm10k_mbx_lock(interface); |
| 655 | |
| 656 | err = fm10k_uc_sync(dev, addr->sa_data); |
| 657 | if (!err) |
| 658 | fm10k_uc_unsync(dev, hw->mac.addr); |
| 659 | |
| 660 | fm10k_mbx_unlock(interface); |
| 661 | } |
| 662 | |
Alexander Duyck | 0e7b364 | 2014-09-20 19:48:10 -0400 | [diff] [blame] | 663 | if (!err) { |
| 664 | ether_addr_copy(dev->dev_addr, addr->sa_data); |
Alexander Duyck | 8f5e20d | 2014-09-20 19:48:20 -0400 | [diff] [blame] | 665 | ether_addr_copy(hw->mac.addr, addr->sa_data); |
Alexander Duyck | 0e7b364 | 2014-09-20 19:48:10 -0400 | [diff] [blame] | 666 | dev->addr_assign_type &= ~NET_ADDR_RANDOM; |
| 667 | } |
| 668 | |
Alexander Duyck | 8f5e20d | 2014-09-20 19:48:20 -0400 | [diff] [blame] | 669 | /* if we had a mailbox error suggest trying again */ |
| 670 | return err ? -EAGAIN : 0; |
| 671 | } |
| 672 | |
| 673 | static int __fm10k_mc_sync(struct net_device *dev, |
| 674 | const unsigned char *addr, bool sync) |
| 675 | { |
| 676 | struct fm10k_intfc *interface = netdev_priv(dev); |
| 677 | struct fm10k_hw *hw = &interface->hw; |
| 678 | u16 vid, glort = interface->glort; |
| 679 | s32 err; |
| 680 | |
| 681 | if (!is_multicast_ether_addr(addr)) |
| 682 | return -EADDRNOTAVAIL; |
| 683 | |
| 684 | /* update table with current entries */ |
| 685 | for (vid = hw->mac.default_vid ? fm10k_find_next_vlan(interface, 0) : 0; |
| 686 | vid < VLAN_N_VID; |
| 687 | vid = fm10k_find_next_vlan(interface, vid)) { |
| 688 | err = hw->mac.ops.update_mc_addr(hw, glort, addr, vid, sync); |
| 689 | if (err) |
| 690 | return err; |
| 691 | } |
| 692 | |
| 693 | return 0; |
| 694 | } |
| 695 | |
| 696 | static int fm10k_mc_sync(struct net_device *dev, |
| 697 | const unsigned char *addr) |
| 698 | { |
| 699 | return __fm10k_mc_sync(dev, addr, true); |
| 700 | } |
| 701 | |
| 702 | static int fm10k_mc_unsync(struct net_device *dev, |
| 703 | const unsigned char *addr) |
| 704 | { |
| 705 | return __fm10k_mc_sync(dev, addr, false); |
Alexander Duyck | 0e7b364 | 2014-09-20 19:48:10 -0400 | [diff] [blame] | 706 | } |
| 707 | |
| 708 | static void fm10k_set_rx_mode(struct net_device *dev) |
| 709 | { |
Alexander Duyck | 8f5e20d | 2014-09-20 19:48:20 -0400 | [diff] [blame] | 710 | struct fm10k_intfc *interface = netdev_priv(dev); |
| 711 | struct fm10k_hw *hw = &interface->hw; |
| 712 | int xcast_mode; |
| 713 | |
| 714 | /* no need to update the harwdare if we are not running */ |
| 715 | if (!(dev->flags & IFF_UP)) |
| 716 | return; |
| 717 | |
| 718 | /* determine new mode based on flags */ |
| 719 | xcast_mode = (dev->flags & IFF_PROMISC) ? FM10K_XCAST_MODE_PROMISC : |
| 720 | (dev->flags & IFF_ALLMULTI) ? FM10K_XCAST_MODE_ALLMULTI : |
| 721 | (dev->flags & (IFF_BROADCAST | IFF_MULTICAST)) ? |
| 722 | FM10K_XCAST_MODE_MULTI : FM10K_XCAST_MODE_NONE; |
| 723 | |
| 724 | fm10k_mbx_lock(interface); |
| 725 | |
| 726 | /* syncronize all of the addresses */ |
| 727 | if (xcast_mode != FM10K_XCAST_MODE_PROMISC) { |
| 728 | __dev_uc_sync(dev, fm10k_uc_sync, fm10k_uc_unsync); |
| 729 | if (xcast_mode != FM10K_XCAST_MODE_ALLMULTI) |
| 730 | __dev_mc_sync(dev, fm10k_mc_sync, fm10k_mc_unsync); |
| 731 | } |
| 732 | |
| 733 | /* if we aren't changing modes there is nothing to do */ |
| 734 | if (interface->xcast_mode != xcast_mode) { |
| 735 | /* update VLAN table */ |
| 736 | if (xcast_mode == FM10K_XCAST_MODE_PROMISC) |
| 737 | hw->mac.ops.update_vlan(hw, FM10K_VLAN_ALL, 0, true); |
| 738 | if (interface->xcast_mode == FM10K_XCAST_MODE_PROMISC) |
| 739 | fm10k_clear_unused_vlans(interface); |
| 740 | |
| 741 | /* update xcast mode */ |
| 742 | hw->mac.ops.update_xcast_mode(hw, interface->glort, xcast_mode); |
| 743 | |
| 744 | /* record updated xcast mode state */ |
| 745 | interface->xcast_mode = xcast_mode; |
| 746 | } |
| 747 | |
| 748 | fm10k_mbx_unlock(interface); |
| 749 | } |
| 750 | |
| 751 | void fm10k_restore_rx_state(struct fm10k_intfc *interface) |
| 752 | { |
| 753 | struct net_device *netdev = interface->netdev; |
| 754 | struct fm10k_hw *hw = &interface->hw; |
| 755 | int xcast_mode; |
| 756 | u16 vid, glort; |
| 757 | |
| 758 | /* record glort for this interface */ |
| 759 | glort = interface->glort; |
| 760 | |
| 761 | /* convert interface flags to xcast mode */ |
| 762 | if (netdev->flags & IFF_PROMISC) |
| 763 | xcast_mode = FM10K_XCAST_MODE_PROMISC; |
| 764 | else if (netdev->flags & IFF_ALLMULTI) |
| 765 | xcast_mode = FM10K_XCAST_MODE_ALLMULTI; |
| 766 | else if (netdev->flags & (IFF_BROADCAST | IFF_MULTICAST)) |
| 767 | xcast_mode = FM10K_XCAST_MODE_MULTI; |
| 768 | else |
| 769 | xcast_mode = FM10K_XCAST_MODE_NONE; |
| 770 | |
| 771 | fm10k_mbx_lock(interface); |
| 772 | |
| 773 | /* Enable logical port */ |
| 774 | hw->mac.ops.update_lport_state(hw, glort, interface->glort_count, true); |
| 775 | |
| 776 | /* update VLAN table */ |
| 777 | hw->mac.ops.update_vlan(hw, FM10K_VLAN_ALL, 0, |
| 778 | xcast_mode == FM10K_XCAST_MODE_PROMISC); |
| 779 | |
| 780 | /* Add filter for VLAN 0 */ |
| 781 | hw->mac.ops.update_vlan(hw, 0, 0, true); |
| 782 | |
| 783 | /* update table with current entries */ |
| 784 | for (vid = hw->mac.default_vid ? fm10k_find_next_vlan(interface, 0) : 0; |
| 785 | vid < VLAN_N_VID; |
| 786 | vid = fm10k_find_next_vlan(interface, vid)) { |
| 787 | hw->mac.ops.update_vlan(hw, vid, 0, true); |
| 788 | hw->mac.ops.update_uc_addr(hw, glort, hw->mac.addr, |
| 789 | vid, true, 0); |
| 790 | } |
| 791 | |
| 792 | /* syncronize all of the addresses */ |
| 793 | if (xcast_mode != FM10K_XCAST_MODE_PROMISC) { |
| 794 | __dev_uc_sync(netdev, fm10k_uc_sync, fm10k_uc_unsync); |
| 795 | if (xcast_mode != FM10K_XCAST_MODE_ALLMULTI) |
| 796 | __dev_mc_sync(netdev, fm10k_mc_sync, fm10k_mc_unsync); |
| 797 | } |
| 798 | |
| 799 | /* update xcast mode */ |
| 800 | hw->mac.ops.update_xcast_mode(hw, glort, xcast_mode); |
| 801 | |
| 802 | fm10k_mbx_unlock(interface); |
| 803 | |
| 804 | /* record updated xcast mode state */ |
| 805 | interface->xcast_mode = xcast_mode; |
| 806 | } |
| 807 | |
| 808 | void fm10k_reset_rx_state(struct fm10k_intfc *interface) |
| 809 | { |
| 810 | struct net_device *netdev = interface->netdev; |
| 811 | struct fm10k_hw *hw = &interface->hw; |
| 812 | |
| 813 | fm10k_mbx_lock(interface); |
| 814 | |
| 815 | /* clear the logical port state on lower device */ |
| 816 | hw->mac.ops.update_lport_state(hw, interface->glort, |
| 817 | interface->glort_count, false); |
| 818 | |
| 819 | fm10k_mbx_unlock(interface); |
| 820 | |
| 821 | /* reset flags to default state */ |
| 822 | interface->xcast_mode = FM10K_XCAST_MODE_NONE; |
| 823 | |
| 824 | /* clear the sync flag since the lport has been dropped */ |
| 825 | __dev_uc_unsync(netdev, NULL); |
| 826 | __dev_mc_unsync(netdev, NULL); |
Alexander Duyck | 0e7b364 | 2014-09-20 19:48:10 -0400 | [diff] [blame] | 827 | } |
| 828 | |
Alexander Duyck | e27ef59 | 2014-09-20 19:49:03 -0400 | [diff] [blame] | 829 | /** |
| 830 | * fm10k_get_stats64 - Get System Network Statistics |
| 831 | * @netdev: network interface device structure |
| 832 | * @stats: storage space for 64bit statistics |
| 833 | * |
| 834 | * Returns 64bit statistics, for use in the ndo_get_stats64 callback. This |
| 835 | * function replaces fm10k_get_stats for kernels which support it. |
| 836 | */ |
| 837 | static struct rtnl_link_stats64 *fm10k_get_stats64(struct net_device *netdev, |
| 838 | struct rtnl_link_stats64 *stats) |
| 839 | { |
| 840 | struct fm10k_intfc *interface = netdev_priv(netdev); |
| 841 | struct fm10k_ring *ring; |
| 842 | unsigned int start, i; |
| 843 | u64 bytes, packets; |
| 844 | |
| 845 | rcu_read_lock(); |
| 846 | |
| 847 | for (i = 0; i < interface->num_rx_queues; i++) { |
| 848 | ring = ACCESS_ONCE(interface->rx_ring[i]); |
| 849 | |
| 850 | if (!ring) |
| 851 | continue; |
| 852 | |
| 853 | do { |
| 854 | start = u64_stats_fetch_begin_irq(&ring->syncp); |
| 855 | packets = ring->stats.packets; |
| 856 | bytes = ring->stats.bytes; |
| 857 | } while (u64_stats_fetch_retry_irq(&ring->syncp, start)); |
| 858 | |
| 859 | stats->rx_packets += packets; |
| 860 | stats->rx_bytes += bytes; |
| 861 | } |
| 862 | |
| 863 | for (i = 0; i < interface->num_tx_queues; i++) { |
| 864 | ring = ACCESS_ONCE(interface->rx_ring[i]); |
| 865 | |
| 866 | if (!ring) |
| 867 | continue; |
| 868 | |
| 869 | do { |
| 870 | start = u64_stats_fetch_begin_irq(&ring->syncp); |
| 871 | packets = ring->stats.packets; |
| 872 | bytes = ring->stats.bytes; |
| 873 | } while (u64_stats_fetch_retry_irq(&ring->syncp, start)); |
| 874 | |
| 875 | stats->tx_packets += packets; |
| 876 | stats->tx_bytes += bytes; |
| 877 | } |
| 878 | |
| 879 | rcu_read_unlock(); |
| 880 | |
| 881 | /* following stats updated by fm10k_service_task() */ |
| 882 | stats->rx_missed_errors = netdev->stats.rx_missed_errors; |
| 883 | |
| 884 | return stats; |
| 885 | } |
| 886 | |
Alexander Duyck | 0e7b364 | 2014-09-20 19:48:10 -0400 | [diff] [blame] | 887 | static const struct net_device_ops fm10k_netdev_ops = { |
Alexander Duyck | 504c5ea | 2014-09-20 19:48:29 -0400 | [diff] [blame] | 888 | .ndo_open = fm10k_open, |
| 889 | .ndo_stop = fm10k_close, |
Alexander Duyck | 0e7b364 | 2014-09-20 19:48:10 -0400 | [diff] [blame] | 890 | .ndo_validate_addr = eth_validate_addr, |
| 891 | .ndo_start_xmit = fm10k_xmit_frame, |
| 892 | .ndo_set_mac_address = fm10k_set_mac, |
| 893 | .ndo_change_mtu = fm10k_change_mtu, |
Alexander Duyck | 8f5e20d | 2014-09-20 19:48:20 -0400 | [diff] [blame] | 894 | .ndo_vlan_rx_add_vid = fm10k_vlan_rx_add_vid, |
| 895 | .ndo_vlan_rx_kill_vid = fm10k_vlan_rx_kill_vid, |
Alexander Duyck | 0e7b364 | 2014-09-20 19:48:10 -0400 | [diff] [blame] | 896 | .ndo_set_rx_mode = fm10k_set_rx_mode, |
Alexander Duyck | e27ef59 | 2014-09-20 19:49:03 -0400 | [diff] [blame] | 897 | .ndo_get_stats64 = fm10k_get_stats64, |
Alexander Duyck | 0e7b364 | 2014-09-20 19:48:10 -0400 | [diff] [blame] | 898 | }; |
| 899 | |
| 900 | #define DEFAULT_DEBUG_LEVEL_SHIFT 3 |
| 901 | |
| 902 | struct net_device *fm10k_alloc_netdev(void) |
| 903 | { |
| 904 | struct fm10k_intfc *interface; |
| 905 | struct net_device *dev; |
| 906 | |
Alexander Duyck | e27ef59 | 2014-09-20 19:49:03 -0400 | [diff] [blame] | 907 | dev = alloc_etherdev_mq(sizeof(struct fm10k_intfc), MAX_QUEUES); |
Alexander Duyck | 0e7b364 | 2014-09-20 19:48:10 -0400 | [diff] [blame] | 908 | if (!dev) |
| 909 | return NULL; |
| 910 | |
| 911 | /* set net device and ethtool ops */ |
| 912 | dev->netdev_ops = &fm10k_netdev_ops; |
| 913 | |
| 914 | /* configure default debug level */ |
| 915 | interface = netdev_priv(dev); |
| 916 | interface->msg_enable = (1 << DEFAULT_DEBUG_LEVEL_SHIFT) - 1; |
| 917 | |
| 918 | /* configure default features */ |
| 919 | dev->features |= NETIF_F_SG; |
| 920 | |
| 921 | /* all features defined to this point should be changeable */ |
| 922 | dev->hw_features |= dev->features; |
| 923 | |
| 924 | /* configure VLAN features */ |
| 925 | dev->vlan_features |= dev->features; |
| 926 | |
| 927 | /* configure tunnel offloads */ |
| 928 | dev->hw_enc_features = NETIF_F_SG; |
| 929 | |
Alexander Duyck | 8f5e20d | 2014-09-20 19:48:20 -0400 | [diff] [blame] | 930 | /* we want to leave these both on as we cannot disable VLAN tag |
| 931 | * insertion or stripping on the hardware since it is contained |
| 932 | * in the FTAG and not in the frame itself. |
| 933 | */ |
| 934 | dev->features |= NETIF_F_HW_VLAN_CTAG_TX | |
| 935 | NETIF_F_HW_VLAN_CTAG_RX | |
| 936 | NETIF_F_HW_VLAN_CTAG_FILTER; |
| 937 | |
| 938 | dev->priv_flags |= IFF_UNICAST_FLT; |
| 939 | |
Alexander Duyck | 0e7b364 | 2014-09-20 19:48:10 -0400 | [diff] [blame] | 940 | return dev; |
| 941 | } |