blob: d00cb193da9a561f65ff6c8ccf1f4d694c46b841 [file] [log] [blame]
Jacob Keller86641092016-04-07 08:21:21 -07001/* Intel(R) Ethernet Switch Host Interface Driver
Jacob Keller9de6a1a2016-04-01 16:17:31 -07002 * Copyright(c) 2013 - 2016 Intel Corporation.
Alexander Duyck0e7b3642014-09-20 19:48:10 -04003 *
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 Duyck3abaae42014-09-20 19:49:43 -040022#include <linux/vmalloc.h>
Alexander Duyckf174cdb2016-06-16 12:21:57 -070023#include <net/udp_tunnel.h>
Alexander Duyck3abaae42014-09-20 19:49:43 -040024
25/**
26 * fm10k_setup_tx_resources - allocate Tx resources (Descriptors)
27 * @tx_ring: tx descriptor ring (for a specific queue) to setup
28 *
29 * Return 0 on success, negative on failure
30 **/
31int fm10k_setup_tx_resources(struct fm10k_ring *tx_ring)
32{
33 struct device *dev = tx_ring->dev;
34 int size;
35
36 size = sizeof(struct fm10k_tx_buffer) * tx_ring->count;
37
38 tx_ring->tx_buffer = vzalloc(size);
39 if (!tx_ring->tx_buffer)
40 goto err;
41
42 u64_stats_init(&tx_ring->syncp);
43
44 /* round up to nearest 4K */
45 tx_ring->size = tx_ring->count * sizeof(struct fm10k_tx_desc);
46 tx_ring->size = ALIGN(tx_ring->size, 4096);
47
48 tx_ring->desc = dma_alloc_coherent(dev, tx_ring->size,
49 &tx_ring->dma, GFP_KERNEL);
50 if (!tx_ring->desc)
51 goto err;
52
53 return 0;
54
55err:
56 vfree(tx_ring->tx_buffer);
57 tx_ring->tx_buffer = NULL;
58 return -ENOMEM;
59}
60
61/**
62 * fm10k_setup_all_tx_resources - allocate all queues Tx resources
63 * @interface: board private structure
64 *
65 * If this function returns with an error, then it's possible one or
66 * more of the rings is populated (while the rest are not). It is the
67 * callers duty to clean those orphaned rings.
68 *
69 * Return 0 on success, negative on failure
70 **/
71static int fm10k_setup_all_tx_resources(struct fm10k_intfc *interface)
72{
73 int i, err = 0;
74
75 for (i = 0; i < interface->num_tx_queues; i++) {
76 err = fm10k_setup_tx_resources(interface->tx_ring[i]);
77 if (!err)
78 continue;
79
80 netif_err(interface, probe, interface->netdev,
81 "Allocation for Tx Queue %u failed\n", i);
82 goto err_setup_tx;
83 }
84
85 return 0;
86err_setup_tx:
87 /* rewind the index freeing the rings as we go */
88 while (i--)
89 fm10k_free_tx_resources(interface->tx_ring[i]);
90 return err;
91}
92
93/**
94 * fm10k_setup_rx_resources - allocate Rx resources (Descriptors)
95 * @rx_ring: rx descriptor ring (for a specific queue) to setup
96 *
97 * Returns 0 on success, negative on failure
98 **/
99int fm10k_setup_rx_resources(struct fm10k_ring *rx_ring)
100{
101 struct device *dev = rx_ring->dev;
102 int size;
103
104 size = sizeof(struct fm10k_rx_buffer) * rx_ring->count;
105
106 rx_ring->rx_buffer = vzalloc(size);
107 if (!rx_ring->rx_buffer)
108 goto err;
109
110 u64_stats_init(&rx_ring->syncp);
111
112 /* Round up to nearest 4K */
113 rx_ring->size = rx_ring->count * sizeof(union fm10k_rx_desc);
114 rx_ring->size = ALIGN(rx_ring->size, 4096);
115
116 rx_ring->desc = dma_alloc_coherent(dev, rx_ring->size,
117 &rx_ring->dma, GFP_KERNEL);
118 if (!rx_ring->desc)
119 goto err;
120
121 return 0;
122err:
123 vfree(rx_ring->rx_buffer);
124 rx_ring->rx_buffer = NULL;
125 return -ENOMEM;
126}
127
128/**
129 * fm10k_setup_all_rx_resources - allocate all queues Rx resources
130 * @interface: board private structure
131 *
132 * If this function returns with an error, then it's possible one or
133 * more of the rings is populated (while the rest are not). It is the
134 * callers duty to clean those orphaned rings.
135 *
136 * Return 0 on success, negative on failure
137 **/
138static int fm10k_setup_all_rx_resources(struct fm10k_intfc *interface)
139{
140 int i, err = 0;
141
142 for (i = 0; i < interface->num_rx_queues; i++) {
143 err = fm10k_setup_rx_resources(interface->rx_ring[i]);
144 if (!err)
145 continue;
146
147 netif_err(interface, probe, interface->netdev,
148 "Allocation for Rx Queue %u failed\n", i);
149 goto err_setup_rx;
150 }
151
152 return 0;
153err_setup_rx:
154 /* rewind the index freeing the rings as we go */
155 while (i--)
156 fm10k_free_rx_resources(interface->rx_ring[i]);
157 return err;
158}
159
160void fm10k_unmap_and_free_tx_resource(struct fm10k_ring *ring,
161 struct fm10k_tx_buffer *tx_buffer)
162{
163 if (tx_buffer->skb) {
164 dev_kfree_skb_any(tx_buffer->skb);
165 if (dma_unmap_len(tx_buffer, len))
166 dma_unmap_single(ring->dev,
167 dma_unmap_addr(tx_buffer, dma),
168 dma_unmap_len(tx_buffer, len),
169 DMA_TO_DEVICE);
170 } else if (dma_unmap_len(tx_buffer, len)) {
171 dma_unmap_page(ring->dev,
172 dma_unmap_addr(tx_buffer, dma),
173 dma_unmap_len(tx_buffer, len),
174 DMA_TO_DEVICE);
175 }
176 tx_buffer->next_to_watch = NULL;
177 tx_buffer->skb = NULL;
178 dma_unmap_len_set(tx_buffer, len, 0);
179 /* tx_buffer must be completely set up in the transmit path */
180}
181
182/**
183 * fm10k_clean_tx_ring - Free Tx Buffers
184 * @tx_ring: ring to be cleaned
185 **/
186static void fm10k_clean_tx_ring(struct fm10k_ring *tx_ring)
187{
188 struct fm10k_tx_buffer *tx_buffer;
189 unsigned long size;
190 u16 i;
191
192 /* ring already cleared, nothing to do */
193 if (!tx_ring->tx_buffer)
194 return;
195
196 /* Free all the Tx ring sk_buffs */
197 for (i = 0; i < tx_ring->count; i++) {
198 tx_buffer = &tx_ring->tx_buffer[i];
199 fm10k_unmap_and_free_tx_resource(tx_ring, tx_buffer);
200 }
201
202 /* reset BQL values */
203 netdev_tx_reset_queue(txring_txq(tx_ring));
204
205 size = sizeof(struct fm10k_tx_buffer) * tx_ring->count;
206 memset(tx_ring->tx_buffer, 0, size);
207
208 /* Zero out the descriptor ring */
209 memset(tx_ring->desc, 0, tx_ring->size);
210}
211
212/**
213 * fm10k_free_tx_resources - Free Tx Resources per Queue
214 * @tx_ring: Tx descriptor ring for a specific queue
215 *
216 * Free all transmit software resources
217 **/
218void fm10k_free_tx_resources(struct fm10k_ring *tx_ring)
219{
220 fm10k_clean_tx_ring(tx_ring);
221
222 vfree(tx_ring->tx_buffer);
223 tx_ring->tx_buffer = NULL;
224
225 /* if not set, then don't free */
226 if (!tx_ring->desc)
227 return;
228
229 dma_free_coherent(tx_ring->dev, tx_ring->size,
230 tx_ring->desc, tx_ring->dma);
231 tx_ring->desc = NULL;
232}
233
234/**
235 * fm10k_clean_all_tx_rings - Free Tx Buffers for all queues
236 * @interface: board private structure
237 **/
238void fm10k_clean_all_tx_rings(struct fm10k_intfc *interface)
239{
240 int i;
241
242 for (i = 0; i < interface->num_tx_queues; i++)
243 fm10k_clean_tx_ring(interface->tx_ring[i]);
244}
245
246/**
247 * fm10k_free_all_tx_resources - Free Tx Resources for All Queues
248 * @interface: board private structure
249 *
250 * Free all transmit software resources
251 **/
252static void fm10k_free_all_tx_resources(struct fm10k_intfc *interface)
253{
254 int i = interface->num_tx_queues;
255
256 while (i--)
257 fm10k_free_tx_resources(interface->tx_ring[i]);
258}
259
260/**
261 * fm10k_clean_rx_ring - Free Rx Buffers per Queue
262 * @rx_ring: ring to free buffers from
263 **/
264static void fm10k_clean_rx_ring(struct fm10k_ring *rx_ring)
265{
266 unsigned long size;
267 u16 i;
268
269 if (!rx_ring->rx_buffer)
270 return;
271
272 if (rx_ring->skb)
273 dev_kfree_skb(rx_ring->skb);
274 rx_ring->skb = NULL;
275
276 /* Free all the Rx ring sk_buffs */
277 for (i = 0; i < rx_ring->count; i++) {
278 struct fm10k_rx_buffer *buffer = &rx_ring->rx_buffer[i];
279 /* clean-up will only set page pointer to NULL */
280 if (!buffer->page)
281 continue;
282
283 dma_unmap_page(rx_ring->dev, buffer->dma,
284 PAGE_SIZE, DMA_FROM_DEVICE);
285 __free_page(buffer->page);
286
287 buffer->page = NULL;
288 }
289
290 size = sizeof(struct fm10k_rx_buffer) * rx_ring->count;
291 memset(rx_ring->rx_buffer, 0, size);
292
293 /* Zero out the descriptor ring */
294 memset(rx_ring->desc, 0, rx_ring->size);
295
296 rx_ring->next_to_alloc = 0;
297 rx_ring->next_to_clean = 0;
298 rx_ring->next_to_use = 0;
299}
300
301/**
302 * fm10k_free_rx_resources - Free Rx Resources
303 * @rx_ring: ring to clean the resources from
304 *
305 * Free all receive software resources
306 **/
307void fm10k_free_rx_resources(struct fm10k_ring *rx_ring)
308{
309 fm10k_clean_rx_ring(rx_ring);
310
311 vfree(rx_ring->rx_buffer);
312 rx_ring->rx_buffer = NULL;
313
314 /* if not set, then don't free */
315 if (!rx_ring->desc)
316 return;
317
318 dma_free_coherent(rx_ring->dev, rx_ring->size,
319 rx_ring->desc, rx_ring->dma);
320
321 rx_ring->desc = NULL;
322}
323
324/**
325 * fm10k_clean_all_rx_rings - Free Rx Buffers for all queues
326 * @interface: board private structure
327 **/
328void fm10k_clean_all_rx_rings(struct fm10k_intfc *interface)
329{
330 int i;
331
332 for (i = 0; i < interface->num_rx_queues; i++)
333 fm10k_clean_rx_ring(interface->rx_ring[i]);
334}
335
336/**
337 * fm10k_free_all_rx_resources - Free Rx Resources for All Queues
338 * @interface: board private structure
339 *
340 * Free all receive software resources
341 **/
342static void fm10k_free_all_rx_resources(struct fm10k_intfc *interface)
343{
344 int i = interface->num_rx_queues;
345
346 while (i--)
347 fm10k_free_rx_resources(interface->rx_ring[i]);
348}
Alexander Duyck0e7b3642014-09-20 19:48:10 -0400349
Alexander Duyck504c5ea2014-09-20 19:48:29 -0400350/**
351 * fm10k_request_glort_range - Request GLORTs for use in configuring rules
352 * @interface: board private structure
353 *
Matthew Vickeca32042015-01-31 02:23:05 +0000354 * This function allocates a range of glorts for this interface to use.
Alexander Duyck504c5ea2014-09-20 19:48:29 -0400355 **/
356static void fm10k_request_glort_range(struct fm10k_intfc *interface)
357{
358 struct fm10k_hw *hw = &interface->hw;
359 u16 mask = (~hw->mac.dglort_map) >> FM10K_DGLORTMAP_MASK_SHIFT;
360
361 /* establish GLORT base */
362 interface->glort = hw->mac.dglort_map & FM10K_DGLORTMAP_NONE;
363 interface->glort_count = 0;
364
365 /* nothing we can do until mask is allocated */
366 if (hw->mac.dglort_map == FM10K_DGLORTMAP_NONE)
367 return;
368
Alexander Duyck883a9cc2014-09-20 19:52:09 -0400369 /* we support 3 possible GLORT configurations.
370 * 1: VFs consume all but the last 1
371 * 2: VFs and PF split glorts with possible gap between
372 * 3: VFs allocated first 64, all others belong to PF
373 */
374 if (mask <= hw->iov.total_vfs) {
375 interface->glort_count = 1;
376 interface->glort += mask;
377 } else if (mask < 64) {
378 interface->glort_count = (mask + 1) / 2;
379 interface->glort += interface->glort_count;
380 } else {
381 interface->glort_count = mask - 63;
382 interface->glort += 64;
383 }
Alexander Duyck504c5ea2014-09-20 19:48:29 -0400384}
385
386/**
Alexander Duyck76a540d2014-09-20 19:51:02 -0400387 * fm10k_del_vxlan_port_all
388 * @interface: board private structure
389 *
390 * This function frees the entire vxlan_port list
391 **/
392static void fm10k_del_vxlan_port_all(struct fm10k_intfc *interface)
393{
394 struct fm10k_vxlan_port *vxlan_port;
395
396 /* flush all entries from list */
397 vxlan_port = list_first_entry_or_null(&interface->vxlan_port,
398 struct fm10k_vxlan_port, list);
399 while (vxlan_port) {
400 list_del(&vxlan_port->list);
401 kfree(vxlan_port);
402 vxlan_port = list_first_entry_or_null(&interface->vxlan_port,
403 struct fm10k_vxlan_port,
404 list);
405 }
406}
407
408/**
409 * fm10k_restore_vxlan_port
410 * @interface: board private structure
411 *
412 * This function restores the value in the tunnel_cfg register after reset
413 **/
414static void fm10k_restore_vxlan_port(struct fm10k_intfc *interface)
415{
416 struct fm10k_hw *hw = &interface->hw;
417 struct fm10k_vxlan_port *vxlan_port;
418
419 /* only the PF supports configuring tunnels */
420 if (hw->mac.type != fm10k_mac_pf)
421 return;
422
423 vxlan_port = list_first_entry_or_null(&interface->vxlan_port,
424 struct fm10k_vxlan_port, list);
425
426 /* restore tunnel configuration register */
427 fm10k_write_reg(hw, FM10K_TUNNEL_CFG,
428 (vxlan_port ? ntohs(vxlan_port->port) : 0) |
429 (ETH_P_TEB << FM10K_TUNNEL_CFG_NVGRE_SHIFT));
430}
431
432/**
433 * fm10k_add_vxlan_port
434 * @netdev: network interface device structure
435 * @sa_family: Address family of new port
436 * @port: port number used for VXLAN
Alexander Duyckf174cdb2016-06-16 12:21:57 -0700437 * @type: Enumerated value specifying udp encapsulation type
Alexander Duyck76a540d2014-09-20 19:51:02 -0400438 *
Jacob Kellerd8ec92f2016-02-10 14:45:51 -0800439 * This function is called when a new VXLAN interface has added a new port
Alexander Duyck76a540d2014-09-20 19:51:02 -0400440 * number to the range that is currently in use for VXLAN. The new port
441 * number is always added to the tail so that the port number list should
442 * match the order in which the ports were allocated. The head of the list
443 * is always used as the VXLAN port number for offloads.
444 **/
445static void fm10k_add_vxlan_port(struct net_device *dev,
Alexander Duyckf174cdb2016-06-16 12:21:57 -0700446 struct udp_tunnel_info *ti)
447{
Alexander Duyck76a540d2014-09-20 19:51:02 -0400448 struct fm10k_intfc *interface = netdev_priv(dev);
449 struct fm10k_vxlan_port *vxlan_port;
450
Alexander Duyckf174cdb2016-06-16 12:21:57 -0700451 if (ti->type != UDP_TUNNEL_TYPE_VXLAN)
452 return;
Alexander Duyck76a540d2014-09-20 19:51:02 -0400453 /* only the PF supports configuring tunnels */
454 if (interface->hw.mac.type != fm10k_mac_pf)
455 return;
456
457 /* existing ports are pulled out so our new entry is always last */
458 fm10k_vxlan_port_for_each(vxlan_port, interface) {
Alexander Duyckf174cdb2016-06-16 12:21:57 -0700459 if ((vxlan_port->port == ti->port) &&
460 (vxlan_port->sa_family == ti->sa_family)) {
Alexander Duyck76a540d2014-09-20 19:51:02 -0400461 list_del(&vxlan_port->list);
462 goto insert_tail;
463 }
464 }
465
466 /* allocate memory to track ports */
467 vxlan_port = kmalloc(sizeof(*vxlan_port), GFP_ATOMIC);
468 if (!vxlan_port)
469 return;
Alexander Duyckf174cdb2016-06-16 12:21:57 -0700470 vxlan_port->port = ti->port;
471 vxlan_port->sa_family = ti->sa_family;
Alexander Duyck76a540d2014-09-20 19:51:02 -0400472
473insert_tail:
474 /* add new port value to list */
475 list_add_tail(&vxlan_port->list, &interface->vxlan_port);
476
477 fm10k_restore_vxlan_port(interface);
478}
479
480/**
481 * fm10k_del_vxlan_port
482 * @netdev: network interface device structure
483 * @sa_family: Address family of freed port
484 * @port: port number used for VXLAN
Alexander Duyckf174cdb2016-06-16 12:21:57 -0700485 * @type: Enumerated value specifying udp encapsulation type
Alexander Duyck76a540d2014-09-20 19:51:02 -0400486 *
Jacob Kellerd8ec92f2016-02-10 14:45:51 -0800487 * This function is called when a new VXLAN interface has freed a port
Alexander Duyck76a540d2014-09-20 19:51:02 -0400488 * number from the range that is currently in use for VXLAN. The freed
489 * port is removed from the list and the new head is used to determine
490 * the port number for offloads.
491 **/
492static void fm10k_del_vxlan_port(struct net_device *dev,
Alexander Duyckf174cdb2016-06-16 12:21:57 -0700493 struct udp_tunnel_info *ti)
494{
Alexander Duyck76a540d2014-09-20 19:51:02 -0400495 struct fm10k_intfc *interface = netdev_priv(dev);
496 struct fm10k_vxlan_port *vxlan_port;
497
Alexander Duyckf174cdb2016-06-16 12:21:57 -0700498 if (ti->type != UDP_TUNNEL_TYPE_VXLAN)
499 return;
Alexander Duyck76a540d2014-09-20 19:51:02 -0400500 if (interface->hw.mac.type != fm10k_mac_pf)
501 return;
502
503 /* find the port in the list and free it */
504 fm10k_vxlan_port_for_each(vxlan_port, interface) {
Alexander Duyckf174cdb2016-06-16 12:21:57 -0700505 if ((vxlan_port->port == ti->port) &&
506 (vxlan_port->sa_family == ti->sa_family)) {
Alexander Duyck76a540d2014-09-20 19:51:02 -0400507 list_del(&vxlan_port->list);
508 kfree(vxlan_port);
509 break;
510 }
511 }
512
513 fm10k_restore_vxlan_port(interface);
514}
515
516/**
Alexander Duyck504c5ea2014-09-20 19:48:29 -0400517 * fm10k_open - Called when a network interface is made active
518 * @netdev: network interface device structure
519 *
520 * Returns 0 on success, negative value on failure
521 *
522 * The open entry point is called when a network interface is made
523 * active by the system (IFF_UP). At this point all resources needed
524 * for transmit and receive operations are allocated, the interrupt
525 * handler is registered with the OS, the watchdog timer is started,
526 * and the stack is notified that the interface is ready.
527 **/
528int fm10k_open(struct net_device *netdev)
529{
530 struct fm10k_intfc *interface = netdev_priv(netdev);
Alexander Duyck18283ca2014-09-20 19:48:51 -0400531 int err;
532
Alexander Duyck3abaae42014-09-20 19:49:43 -0400533 /* allocate transmit descriptors */
534 err = fm10k_setup_all_tx_resources(interface);
535 if (err)
536 goto err_setup_tx;
537
538 /* allocate receive descriptors */
539 err = fm10k_setup_all_rx_resources(interface);
540 if (err)
541 goto err_setup_rx;
542
Alexander Duyck18283ca2014-09-20 19:48:51 -0400543 /* allocate interrupt resources */
544 err = fm10k_qv_request_irq(interface);
545 if (err)
546 goto err_req_irq;
Alexander Duyck504c5ea2014-09-20 19:48:29 -0400547
548 /* setup GLORT assignment for this port */
549 fm10k_request_glort_range(interface);
550
Alexander Duycke27ef592014-09-20 19:49:03 -0400551 /* Notify the stack of the actual queue counts */
Alexander Duyckc9d49942014-09-30 22:49:22 +0000552 err = netif_set_real_num_tx_queues(netdev,
553 interface->num_tx_queues);
554 if (err)
555 goto err_set_queues;
Alexander Duycke27ef592014-09-20 19:49:03 -0400556
557 err = netif_set_real_num_rx_queues(netdev,
558 interface->num_rx_queues);
559 if (err)
560 goto err_set_queues;
561
Alexander Duyck76a540d2014-09-20 19:51:02 -0400562 /* update VXLAN port configuration */
Alexander Duyckf174cdb2016-06-16 12:21:57 -0700563 udp_tunnel_get_rx_info(netdev);
Bruce Allan0d722ec2015-12-08 17:20:49 -0800564
Alexander Duyck504c5ea2014-09-20 19:48:29 -0400565 fm10k_up(interface);
566
567 return 0;
Alexander Duyck18283ca2014-09-20 19:48:51 -0400568
Alexander Duycke27ef592014-09-20 19:49:03 -0400569err_set_queues:
570 fm10k_qv_free_irq(interface);
Alexander Duyck18283ca2014-09-20 19:48:51 -0400571err_req_irq:
Alexander Duyck3abaae42014-09-20 19:49:43 -0400572 fm10k_free_all_rx_resources(interface);
573err_setup_rx:
574 fm10k_free_all_tx_resources(interface);
575err_setup_tx:
Alexander Duyck18283ca2014-09-20 19:48:51 -0400576 return err;
Alexander Duyck504c5ea2014-09-20 19:48:29 -0400577}
578
579/**
580 * fm10k_close - Disables a network interface
581 * @netdev: network interface device structure
582 *
583 * Returns 0, this is not allowed to fail
584 *
585 * The close entry point is called when an interface is de-activated
586 * by the OS. The hardware is still under the drivers control, but
587 * needs to be disabled. A global MAC reset is issued to stop the
588 * hardware, and all transmit and receive resources are freed.
589 **/
590int fm10k_close(struct net_device *netdev)
591{
592 struct fm10k_intfc *interface = netdev_priv(netdev);
593
594 fm10k_down(interface);
595
Alexander Duyck18283ca2014-09-20 19:48:51 -0400596 fm10k_qv_free_irq(interface);
597
Alexander Duyck76a540d2014-09-20 19:51:02 -0400598 fm10k_del_vxlan_port_all(interface);
599
Alexander Duyck3abaae42014-09-20 19:49:43 -0400600 fm10k_free_all_tx_resources(interface);
601 fm10k_free_all_rx_resources(interface);
602
Alexander Duyck504c5ea2014-09-20 19:48:29 -0400603 return 0;
604}
605
Alexander Duyck0e7b3642014-09-20 19:48:10 -0400606static netdev_tx_t fm10k_xmit_frame(struct sk_buff *skb, struct net_device *dev)
607{
Alexander Duyckb101c962014-09-20 19:50:03 -0400608 struct fm10k_intfc *interface = netdev_priv(dev);
Alexander Duyckc9d49942014-09-30 22:49:22 +0000609 unsigned int r_idx = skb->queue_mapping;
Alexander Duyckb101c962014-09-20 19:50:03 -0400610 int err;
611
Bruce Allana4fcad62015-10-28 17:19:40 -0700612 if ((skb->protocol == htons(ETH_P_8021Q)) &&
Jiri Pirkodf8a39d2015-01-13 17:13:44 +0100613 !skb_vlan_tag_present(skb)) {
Alexander Duyckb101c962014-09-20 19:50:03 -0400614 /* FM10K only supports hardware tagging, any tags in frame
615 * are considered 2nd level or "outer" tags
616 */
617 struct vlan_hdr *vhdr;
618 __be16 proto;
619
620 /* make sure skb is not shared */
621 skb = skb_share_check(skb, GFP_ATOMIC);
622 if (!skb)
623 return NETDEV_TX_OK;
624
625 /* make sure there is enough room to move the ethernet header */
626 if (unlikely(!pskb_may_pull(skb, VLAN_ETH_HLEN)))
627 return NETDEV_TX_OK;
628
629 /* verify the skb head is not shared */
630 err = skb_cow_head(skb, 0);
stephen hemminger6f975322015-11-17 14:24:27 -0800631 if (err) {
632 dev_kfree_skb(skb);
Alexander Duyckb101c962014-09-20 19:50:03 -0400633 return NETDEV_TX_OK;
stephen hemminger6f975322015-11-17 14:24:27 -0800634 }
Alexander Duyckb101c962014-09-20 19:50:03 -0400635
Jacob Kelleraa502b42015-11-02 12:10:22 -0800636 /* locate VLAN header */
Alexander Duyckb101c962014-09-20 19:50:03 -0400637 vhdr = (struct vlan_hdr *)(skb->data + ETH_HLEN);
638
639 /* pull the 2 key pieces of data out of it */
640 __vlan_hwaccel_put_tag(skb,
641 htons(ETH_P_8021Q),
642 ntohs(vhdr->h_vlan_TCI));
643 proto = vhdr->h_vlan_encapsulated_proto;
644 skb->protocol = (ntohs(proto) >= 1536) ? proto :
645 htons(ETH_P_802_2);
646
647 /* squash it by moving the ethernet addresses up 4 bytes */
648 memmove(skb->data + VLAN_HLEN, skb->data, 12);
649 __skb_pull(skb, VLAN_HLEN);
650 skb_reset_mac_header(skb);
651 }
652
653 /* The minimum packet size for a single buffer is 17B so pad the skb
654 * in order to meet this minimum size requirement.
655 */
656 if (unlikely(skb->len < 17)) {
657 int pad_len = 17 - skb->len;
658
659 if (skb_pad(skb, pad_len))
660 return NETDEV_TX_OK;
661 __skb_put(skb, pad_len);
662 }
663
664 if (r_idx >= interface->num_tx_queues)
665 r_idx %= interface->num_tx_queues;
666
667 err = fm10k_xmit_frame_ring(skb, interface->tx_ring[r_idx]);
668
669 return err;
Alexander Duyck0e7b3642014-09-20 19:48:10 -0400670}
671
672static int fm10k_change_mtu(struct net_device *dev, int new_mtu)
673{
674 if (new_mtu < 68 || new_mtu > FM10K_MAX_JUMBO_FRAME_SIZE)
675 return -EINVAL;
676
677 dev->mtu = new_mtu;
678
679 return 0;
680}
681
Alexander Duyckb101c962014-09-20 19:50:03 -0400682/**
683 * fm10k_tx_timeout - Respond to a Tx Hang
684 * @netdev: network interface device structure
685 **/
686static void fm10k_tx_timeout(struct net_device *netdev)
687{
688 struct fm10k_intfc *interface = netdev_priv(netdev);
689 bool real_tx_hang = false;
690 int i;
691
692#define TX_TIMEO_LIMIT 16000
693 for (i = 0; i < interface->num_tx_queues; i++) {
694 struct fm10k_ring *tx_ring = interface->tx_ring[i];
695
696 if (check_for_tx_hang(tx_ring) && fm10k_check_tx_hang(tx_ring))
697 real_tx_hang = true;
698 }
699
700 if (real_tx_hang) {
701 fm10k_tx_timeout_reset(interface);
702 } else {
703 netif_info(interface, drv, netdev,
704 "Fake Tx hang detected with timeout of %d seconds\n",
Bruce Allana4fcad62015-10-28 17:19:40 -0700705 netdev->watchdog_timeo / HZ);
Alexander Duyckb101c962014-09-20 19:50:03 -0400706
707 /* fake Tx hang - increase the kernel timeout */
708 if (netdev->watchdog_timeo < TX_TIMEO_LIMIT)
709 netdev->watchdog_timeo *= 2;
710 }
711}
712
Alexander Duyck8f5e20d2014-09-20 19:48:20 -0400713static int fm10k_uc_vlan_unsync(struct net_device *netdev,
714 const unsigned char *uc_addr)
715{
716 struct fm10k_intfc *interface = netdev_priv(netdev);
717 struct fm10k_hw *hw = &interface->hw;
718 u16 glort = interface->glort;
719 u16 vid = interface->vid;
720 bool set = !!(vid / VLAN_N_VID);
721 int err;
722
723 /* drop any leading bits on the VLAN ID */
724 vid &= VLAN_N_VID - 1;
725
726 err = hw->mac.ops.update_uc_addr(hw, glort, uc_addr, vid, set, 0);
727 if (err)
728 return err;
729
730 /* return non-zero value as we are only doing a partial sync/unsync */
731 return 1;
732}
733
734static int fm10k_mc_vlan_unsync(struct net_device *netdev,
735 const unsigned char *mc_addr)
736{
737 struct fm10k_intfc *interface = netdev_priv(netdev);
738 struct fm10k_hw *hw = &interface->hw;
739 u16 glort = interface->glort;
740 u16 vid = interface->vid;
741 bool set = !!(vid / VLAN_N_VID);
742 int err;
743
744 /* drop any leading bits on the VLAN ID */
745 vid &= VLAN_N_VID - 1;
746
747 err = hw->mac.ops.update_mc_addr(hw, glort, mc_addr, vid, set);
748 if (err)
749 return err;
750
751 /* return non-zero value as we are only doing a partial sync/unsync */
752 return 1;
753}
754
755static int fm10k_update_vid(struct net_device *netdev, u16 vid, bool set)
756{
757 struct fm10k_intfc *interface = netdev_priv(netdev);
758 struct fm10k_hw *hw = &interface->hw;
759 s32 err;
Jacob Kellere71c9312015-06-24 13:34:46 -0700760 int i;
Alexander Duyck8f5e20d2014-09-20 19:48:20 -0400761
762 /* updates do not apply to VLAN 0 */
763 if (!vid)
764 return 0;
765
766 if (vid >= VLAN_N_VID)
767 return -EINVAL;
768
769 /* Verify we have permission to add VLANs */
770 if (hw->mac.vlan_override)
771 return -EACCES;
772
Alexander Duyck8f5e20d2014-09-20 19:48:20 -0400773 /* update active_vlans bitmask */
774 set_bit(vid, interface->active_vlans);
775 if (!set)
776 clear_bit(vid, interface->active_vlans);
777
Jacob Kelleraa502b42015-11-02 12:10:22 -0800778 /* disable the default VLAN ID on ring if we have an active VLAN */
Jacob Kellere71c9312015-06-24 13:34:46 -0700779 for (i = 0; i < interface->num_rx_queues; i++) {
780 struct fm10k_ring *rx_ring = interface->rx_ring[i];
781 u16 rx_vid = rx_ring->vid & (VLAN_N_VID - 1);
782
783 if (test_bit(rx_vid, interface->active_vlans))
784 rx_ring->vid |= FM10K_VLAN_CLEAR;
785 else
786 rx_ring->vid &= ~FM10K_VLAN_CLEAR;
787 }
788
Bruce Allan3d02b3d2015-10-28 17:19:56 -0700789 /* Do not remove default VLAN ID related entries from VLAN and MAC
790 * tables
791 */
Jacob Keller56f05692015-06-15 15:00:53 -0700792 if (!set && vid == hw->mac.default_vid)
Jeff Kirsher661b2062015-04-10 16:48:19 -0700793 return 0;
794
Jacob Keller3f0bdb22015-06-19 10:56:09 -0700795 /* Do not throw an error if the interface is down. We will sync once
796 * we come up
797 */
798 if (test_bit(__FM10K_DOWN, &interface->state))
799 return 0;
800
Alexander Duyck8f5e20d2014-09-20 19:48:20 -0400801 fm10k_mbx_lock(interface);
802
Matthew Vickeca32042015-01-31 02:23:05 +0000803 /* only need to update the VLAN if not in promiscuous mode */
Alexander Duyck8f5e20d2014-09-20 19:48:20 -0400804 if (!(netdev->flags & IFF_PROMISC)) {
805 err = hw->mac.ops.update_vlan(hw, vid, 0, set);
806 if (err)
Matthew Vick13cb2da2014-10-03 00:43:35 +0000807 goto err_out;
Alexander Duyck8f5e20d2014-09-20 19:48:20 -0400808 }
809
810 /* update our base MAC address */
811 err = hw->mac.ops.update_uc_addr(hw, interface->glort, hw->mac.addr,
812 vid, set, 0);
813 if (err)
Matthew Vick13cb2da2014-10-03 00:43:35 +0000814 goto err_out;
Alexander Duyck8f5e20d2014-09-20 19:48:20 -0400815
Jacob Kelleraa502b42015-11-02 12:10:22 -0800816 /* set VLAN ID prior to syncing/unsyncing the VLAN */
Alexander Duyck8f5e20d2014-09-20 19:48:20 -0400817 interface->vid = vid + (set ? VLAN_N_VID : 0);
818
819 /* Update the unicast and multicast address list to add/drop VLAN */
820 __dev_uc_unsync(netdev, fm10k_uc_vlan_unsync);
821 __dev_mc_unsync(netdev, fm10k_mc_vlan_unsync);
822
Matthew Vick13cb2da2014-10-03 00:43:35 +0000823err_out:
Alexander Duyck8f5e20d2014-09-20 19:48:20 -0400824 fm10k_mbx_unlock(interface);
825
Matthew Vick13cb2da2014-10-03 00:43:35 +0000826 return err;
Alexander Duyck8f5e20d2014-09-20 19:48:20 -0400827}
828
829static int fm10k_vlan_rx_add_vid(struct net_device *netdev,
830 __always_unused __be16 proto, u16 vid)
831{
832 /* update VLAN and address table based on changes */
833 return fm10k_update_vid(netdev, vid, true);
834}
835
836static int fm10k_vlan_rx_kill_vid(struct net_device *netdev,
837 __always_unused __be16 proto, u16 vid)
838{
839 /* update VLAN and address table based on changes */
840 return fm10k_update_vid(netdev, vid, false);
841}
842
843static u16 fm10k_find_next_vlan(struct fm10k_intfc *interface, u16 vid)
844{
845 struct fm10k_hw *hw = &interface->hw;
846 u16 default_vid = hw->mac.default_vid;
847 u16 vid_limit = vid < default_vid ? default_vid : VLAN_N_VID;
848
849 vid = find_next_bit(interface->active_vlans, vid_limit, ++vid);
850
851 return vid;
852}
853
854static void fm10k_clear_unused_vlans(struct fm10k_intfc *interface)
855{
856 struct fm10k_hw *hw = &interface->hw;
857 u32 vid, prev_vid;
858
859 /* loop through and find any gaps in the table */
860 for (vid = 0, prev_vid = 0;
861 prev_vid < VLAN_N_VID;
862 prev_vid = vid + 1, vid = fm10k_find_next_vlan(interface, vid)) {
863 if (prev_vid == vid)
864 continue;
865
866 /* send request to clear multiple bits at a time */
867 prev_vid += (vid - prev_vid - 1) << FM10K_VLAN_LENGTH_SHIFT;
868 hw->mac.ops.update_vlan(hw, prev_vid, 0, false);
869 }
870}
871
872static int __fm10k_uc_sync(struct net_device *dev,
873 const unsigned char *addr, bool sync)
874{
875 struct fm10k_intfc *interface = netdev_priv(dev);
876 struct fm10k_hw *hw = &interface->hw;
877 u16 vid, glort = interface->glort;
878 s32 err;
879
880 if (!is_valid_ether_addr(addr))
881 return -EADDRNOTAVAIL;
882
883 /* update table with current entries */
Ngai-Mint Kwan89987632016-04-01 16:17:32 -0700884 for (vid = hw->mac.default_vid ? fm10k_find_next_vlan(interface, 0) : 1;
Alexander Duyck8f5e20d2014-09-20 19:48:20 -0400885 vid < VLAN_N_VID;
886 vid = fm10k_find_next_vlan(interface, vid)) {
887 err = hw->mac.ops.update_uc_addr(hw, glort, addr,
888 vid, sync, 0);
889 if (err)
890 return err;
891 }
892
893 return 0;
894}
895
896static int fm10k_uc_sync(struct net_device *dev,
897 const unsigned char *addr)
898{
899 return __fm10k_uc_sync(dev, addr, true);
900}
901
902static int fm10k_uc_unsync(struct net_device *dev,
903 const unsigned char *addr)
904{
905 return __fm10k_uc_sync(dev, addr, false);
906}
907
Alexander Duyck0e7b3642014-09-20 19:48:10 -0400908static int fm10k_set_mac(struct net_device *dev, void *p)
909{
Alexander Duyck8f5e20d2014-09-20 19:48:20 -0400910 struct fm10k_intfc *interface = netdev_priv(dev);
911 struct fm10k_hw *hw = &interface->hw;
Alexander Duyck0e7b3642014-09-20 19:48:10 -0400912 struct sockaddr *addr = p;
913 s32 err = 0;
914
915 if (!is_valid_ether_addr(addr->sa_data))
916 return -EADDRNOTAVAIL;
917
Alexander Duyck8f5e20d2014-09-20 19:48:20 -0400918 if (dev->flags & IFF_UP) {
919 /* setting MAC address requires mailbox */
920 fm10k_mbx_lock(interface);
921
922 err = fm10k_uc_sync(dev, addr->sa_data);
923 if (!err)
924 fm10k_uc_unsync(dev, hw->mac.addr);
925
926 fm10k_mbx_unlock(interface);
927 }
928
Alexander Duyck0e7b3642014-09-20 19:48:10 -0400929 if (!err) {
930 ether_addr_copy(dev->dev_addr, addr->sa_data);
Alexander Duyck8f5e20d2014-09-20 19:48:20 -0400931 ether_addr_copy(hw->mac.addr, addr->sa_data);
Alexander Duyck0e7b3642014-09-20 19:48:10 -0400932 dev->addr_assign_type &= ~NET_ADDR_RANDOM;
933 }
934
Alexander Duyck8f5e20d2014-09-20 19:48:20 -0400935 /* if we had a mailbox error suggest trying again */
936 return err ? -EAGAIN : 0;
937}
938
939static int __fm10k_mc_sync(struct net_device *dev,
940 const unsigned char *addr, bool sync)
941{
942 struct fm10k_intfc *interface = netdev_priv(dev);
943 struct fm10k_hw *hw = &interface->hw;
944 u16 vid, glort = interface->glort;
Alexander Duyck8f5e20d2014-09-20 19:48:20 -0400945
946 /* update table with current entries */
Ngai-Mint Kwan89987632016-04-01 16:17:32 -0700947 for (vid = hw->mac.default_vid ? fm10k_find_next_vlan(interface, 0) : 1;
Alexander Duyck8f5e20d2014-09-20 19:48:20 -0400948 vid < VLAN_N_VID;
949 vid = fm10k_find_next_vlan(interface, vid)) {
Jacob Keller745136a2015-06-03 16:30:58 -0700950 hw->mac.ops.update_mc_addr(hw, glort, addr, vid, sync);
Alexander Duyck8f5e20d2014-09-20 19:48:20 -0400951 }
952
953 return 0;
954}
955
956static int fm10k_mc_sync(struct net_device *dev,
957 const unsigned char *addr)
958{
959 return __fm10k_mc_sync(dev, addr, true);
960}
961
962static int fm10k_mc_unsync(struct net_device *dev,
963 const unsigned char *addr)
964{
965 return __fm10k_mc_sync(dev, addr, false);
Alexander Duyck0e7b3642014-09-20 19:48:10 -0400966}
967
968static void fm10k_set_rx_mode(struct net_device *dev)
969{
Alexander Duyck8f5e20d2014-09-20 19:48:20 -0400970 struct fm10k_intfc *interface = netdev_priv(dev);
971 struct fm10k_hw *hw = &interface->hw;
972 int xcast_mode;
973
974 /* no need to update the harwdare if we are not running */
975 if (!(dev->flags & IFF_UP))
976 return;
977
978 /* determine new mode based on flags */
979 xcast_mode = (dev->flags & IFF_PROMISC) ? FM10K_XCAST_MODE_PROMISC :
980 (dev->flags & IFF_ALLMULTI) ? FM10K_XCAST_MODE_ALLMULTI :
981 (dev->flags & (IFF_BROADCAST | IFF_MULTICAST)) ?
982 FM10K_XCAST_MODE_MULTI : FM10K_XCAST_MODE_NONE;
983
984 fm10k_mbx_lock(interface);
985
Jeff Kirshera7731cc2015-04-03 13:27:11 -0700986 /* update xcast mode first, but only if it changed */
Alexander Duyck8f5e20d2014-09-20 19:48:20 -0400987 if (interface->xcast_mode != xcast_mode) {
988 /* update VLAN table */
989 if (xcast_mode == FM10K_XCAST_MODE_PROMISC)
990 hw->mac.ops.update_vlan(hw, FM10K_VLAN_ALL, 0, true);
991 if (interface->xcast_mode == FM10K_XCAST_MODE_PROMISC)
992 fm10k_clear_unused_vlans(interface);
993
994 /* update xcast mode */
995 hw->mac.ops.update_xcast_mode(hw, interface->glort, xcast_mode);
996
997 /* record updated xcast mode state */
998 interface->xcast_mode = xcast_mode;
999 }
1000
Jeff Kirshera7731cc2015-04-03 13:27:11 -07001001 /* synchronize all of the addresses */
Ngai-Mint Kwan89987632016-04-01 16:17:32 -07001002 __dev_uc_sync(dev, fm10k_uc_sync, fm10k_uc_unsync);
1003 __dev_mc_sync(dev, fm10k_mc_sync, fm10k_mc_unsync);
Jeff Kirshera7731cc2015-04-03 13:27:11 -07001004
Alexander Duyck8f5e20d2014-09-20 19:48:20 -04001005 fm10k_mbx_unlock(interface);
1006}
1007
1008void fm10k_restore_rx_state(struct fm10k_intfc *interface)
1009{
1010 struct net_device *netdev = interface->netdev;
1011 struct fm10k_hw *hw = &interface->hw;
1012 int xcast_mode;
1013 u16 vid, glort;
1014
1015 /* record glort for this interface */
1016 glort = interface->glort;
1017
1018 /* convert interface flags to xcast mode */
1019 if (netdev->flags & IFF_PROMISC)
1020 xcast_mode = FM10K_XCAST_MODE_PROMISC;
1021 else if (netdev->flags & IFF_ALLMULTI)
1022 xcast_mode = FM10K_XCAST_MODE_ALLMULTI;
1023 else if (netdev->flags & (IFF_BROADCAST | IFF_MULTICAST))
1024 xcast_mode = FM10K_XCAST_MODE_MULTI;
1025 else
1026 xcast_mode = FM10K_XCAST_MODE_NONE;
1027
1028 fm10k_mbx_lock(interface);
1029
1030 /* Enable logical port */
1031 hw->mac.ops.update_lport_state(hw, glort, interface->glort_count, true);
1032
1033 /* update VLAN table */
1034 hw->mac.ops.update_vlan(hw, FM10K_VLAN_ALL, 0,
1035 xcast_mode == FM10K_XCAST_MODE_PROMISC);
1036
1037 /* Add filter for VLAN 0 */
1038 hw->mac.ops.update_vlan(hw, 0, 0, true);
1039
1040 /* update table with current entries */
Ngai-Mint Kwan89987632016-04-01 16:17:32 -07001041 for (vid = hw->mac.default_vid ? fm10k_find_next_vlan(interface, 0) : 1;
Alexander Duyck8f5e20d2014-09-20 19:48:20 -04001042 vid < VLAN_N_VID;
1043 vid = fm10k_find_next_vlan(interface, vid)) {
1044 hw->mac.ops.update_vlan(hw, vid, 0, true);
1045 hw->mac.ops.update_uc_addr(hw, glort, hw->mac.addr,
1046 vid, true, 0);
1047 }
1048
Jacob Keller5c2d6422015-06-24 13:34:47 -07001049 /* update xcast mode before synchronizing addresses */
Jeff Kirshera7731cc2015-04-03 13:27:11 -07001050 hw->mac.ops.update_xcast_mode(hw, glort, xcast_mode);
1051
Matthew Vickeca32042015-01-31 02:23:05 +00001052 /* synchronize all of the addresses */
Ngai-Mint Kwan89987632016-04-01 16:17:32 -07001053 __dev_uc_sync(netdev, fm10k_uc_sync, fm10k_uc_unsync);
1054 __dev_mc_sync(netdev, fm10k_mc_sync, fm10k_mc_unsync);
Alexander Duyck8f5e20d2014-09-20 19:48:20 -04001055
Alexander Duyck8f5e20d2014-09-20 19:48:20 -04001056 fm10k_mbx_unlock(interface);
1057
1058 /* record updated xcast mode state */
1059 interface->xcast_mode = xcast_mode;
Alexander Duyck76a540d2014-09-20 19:51:02 -04001060
1061 /* Restore tunnel configuration */
1062 fm10k_restore_vxlan_port(interface);
Alexander Duyck8f5e20d2014-09-20 19:48:20 -04001063}
1064
1065void fm10k_reset_rx_state(struct fm10k_intfc *interface)
1066{
1067 struct net_device *netdev = interface->netdev;
1068 struct fm10k_hw *hw = &interface->hw;
1069
1070 fm10k_mbx_lock(interface);
1071
1072 /* clear the logical port state on lower device */
1073 hw->mac.ops.update_lport_state(hw, interface->glort,
1074 interface->glort_count, false);
1075
1076 fm10k_mbx_unlock(interface);
1077
1078 /* reset flags to default state */
1079 interface->xcast_mode = FM10K_XCAST_MODE_NONE;
1080
1081 /* clear the sync flag since the lport has been dropped */
1082 __dev_uc_unsync(netdev, NULL);
1083 __dev_mc_unsync(netdev, NULL);
Alexander Duyck0e7b3642014-09-20 19:48:10 -04001084}
1085
Alexander Duycke27ef592014-09-20 19:49:03 -04001086/**
1087 * fm10k_get_stats64 - Get System Network Statistics
1088 * @netdev: network interface device structure
1089 * @stats: storage space for 64bit statistics
1090 *
1091 * Returns 64bit statistics, for use in the ndo_get_stats64 callback. This
1092 * function replaces fm10k_get_stats for kernels which support it.
1093 */
1094static struct rtnl_link_stats64 *fm10k_get_stats64(struct net_device *netdev,
1095 struct rtnl_link_stats64 *stats)
1096{
1097 struct fm10k_intfc *interface = netdev_priv(netdev);
1098 struct fm10k_ring *ring;
1099 unsigned int start, i;
1100 u64 bytes, packets;
1101
1102 rcu_read_lock();
1103
1104 for (i = 0; i < interface->num_rx_queues; i++) {
1105 ring = ACCESS_ONCE(interface->rx_ring[i]);
1106
1107 if (!ring)
1108 continue;
1109
1110 do {
1111 start = u64_stats_fetch_begin_irq(&ring->syncp);
1112 packets = ring->stats.packets;
1113 bytes = ring->stats.bytes;
1114 } while (u64_stats_fetch_retry_irq(&ring->syncp, start));
1115
1116 stats->rx_packets += packets;
1117 stats->rx_bytes += bytes;
1118 }
1119
1120 for (i = 0; i < interface->num_tx_queues; i++) {
Jeff Kirsherf4e25f62015-04-03 13:26:51 -07001121 ring = ACCESS_ONCE(interface->tx_ring[i]);
Alexander Duycke27ef592014-09-20 19:49:03 -04001122
1123 if (!ring)
1124 continue;
1125
1126 do {
1127 start = u64_stats_fetch_begin_irq(&ring->syncp);
1128 packets = ring->stats.packets;
1129 bytes = ring->stats.bytes;
1130 } while (u64_stats_fetch_retry_irq(&ring->syncp, start));
1131
1132 stats->tx_packets += packets;
1133 stats->tx_bytes += bytes;
1134 }
1135
1136 rcu_read_unlock();
1137
1138 /* following stats updated by fm10k_service_task() */
1139 stats->rx_missed_errors = netdev->stats.rx_missed_errors;
1140
1141 return stats;
1142}
1143
Alexander Duyckaa3ac822014-09-20 19:50:42 -04001144int fm10k_setup_tc(struct net_device *dev, u8 tc)
1145{
1146 struct fm10k_intfc *interface = netdev_priv(dev);
Alexander Duyck09f8a822015-11-10 09:40:30 -08001147 int err;
Alexander Duyckaa3ac822014-09-20 19:50:42 -04001148
1149 /* Currently only the PF supports priority classes */
1150 if (tc && (interface->hw.mac.type != fm10k_mac_pf))
1151 return -EINVAL;
1152
1153 /* Hardware supports up to 8 traffic classes */
1154 if (tc > 8)
1155 return -EINVAL;
1156
1157 /* Hardware has to reinitialize queues to match packet
1158 * buffer alignment. Unfortunately, the hardware is not
1159 * flexible enough to do this dynamically.
1160 */
1161 if (netif_running(dev))
1162 fm10k_close(dev);
1163
1164 fm10k_mbx_free_irq(interface);
1165
1166 fm10k_clear_queueing_scheme(interface);
1167
1168 /* we expect the prio_tc map to be repopulated later */
1169 netdev_reset_tc(dev);
1170 netdev_set_num_tc(dev, tc);
1171
Alexander Duyck09f8a822015-11-10 09:40:30 -08001172 err = fm10k_init_queueing_scheme(interface);
1173 if (err)
1174 goto err_queueing_scheme;
Alexander Duyckaa3ac822014-09-20 19:50:42 -04001175
Alexander Duyck09f8a822015-11-10 09:40:30 -08001176 err = fm10k_mbx_request_irq(interface);
1177 if (err)
1178 goto err_mbx_irq;
Alexander Duyckaa3ac822014-09-20 19:50:42 -04001179
Alexander Duyck09f8a822015-11-10 09:40:30 -08001180 err = netif_running(dev) ? fm10k_open(dev) : 0;
1181 if (err)
1182 goto err_open;
Alexander Duyckaa3ac822014-09-20 19:50:42 -04001183
1184 /* flag to indicate SWPRI has yet to be updated */
1185 interface->flags |= FM10K_FLAG_SWPRI_CONFIG;
1186
1187 return 0;
Alexander Duyck09f8a822015-11-10 09:40:30 -08001188err_open:
1189 fm10k_mbx_free_irq(interface);
1190err_mbx_irq:
1191 fm10k_clear_queueing_scheme(interface);
1192err_queueing_scheme:
1193 netif_device_detach(dev);
1194
1195 return err;
Alexander Duyckaa3ac822014-09-20 19:50:42 -04001196}
1197
John Fastabend16e5cc62016-02-16 21:16:43 -08001198static int __fm10k_setup_tc(struct net_device *dev, u32 handle, __be16 proto,
1199 struct tc_to_netdev *tc)
John Fastabende4c67342016-02-16 21:16:15 -08001200{
John Fastabend5eb4dce2016-02-29 11:26:13 -08001201 if (tc->type != TC_SETUP_MQPRIO)
John Fastabende4c67342016-02-16 21:16:15 -08001202 return -EINVAL;
1203
John Fastabend16e5cc62016-02-16 21:16:43 -08001204 return fm10k_setup_tc(dev, tc->tc);
John Fastabende4c67342016-02-16 21:16:15 -08001205}
1206
Alexander Duyck5cd5e2e2014-09-20 19:51:15 -04001207static void fm10k_assign_l2_accel(struct fm10k_intfc *interface,
1208 struct fm10k_l2_accel *l2_accel)
1209{
1210 struct fm10k_ring *ring;
1211 int i;
1212
1213 for (i = 0; i < interface->num_rx_queues; i++) {
1214 ring = interface->rx_ring[i];
1215 rcu_assign_pointer(ring->l2_accel, l2_accel);
1216 }
1217
1218 interface->l2_accel = l2_accel;
1219}
1220
1221static void *fm10k_dfwd_add_station(struct net_device *dev,
1222 struct net_device *sdev)
1223{
1224 struct fm10k_intfc *interface = netdev_priv(dev);
1225 struct fm10k_l2_accel *l2_accel = interface->l2_accel;
1226 struct fm10k_l2_accel *old_l2_accel = NULL;
1227 struct fm10k_dglort_cfg dglort = { 0 };
1228 struct fm10k_hw *hw = &interface->hw;
1229 int size = 0, i;
1230 u16 glort;
1231
1232 /* allocate l2 accel structure if it is not available */
1233 if (!l2_accel) {
1234 /* verify there is enough free GLORTs to support l2_accel */
1235 if (interface->glort_count < 7)
1236 return ERR_PTR(-EBUSY);
1237
1238 size = offsetof(struct fm10k_l2_accel, macvlan[7]);
1239 l2_accel = kzalloc(size, GFP_KERNEL);
1240 if (!l2_accel)
1241 return ERR_PTR(-ENOMEM);
1242
1243 l2_accel->size = 7;
1244 l2_accel->dglort = interface->glort;
1245
1246 /* update pointers */
1247 fm10k_assign_l2_accel(interface, l2_accel);
1248 /* do not expand if we are at our limit */
1249 } else if ((l2_accel->count == FM10K_MAX_STATIONS) ||
1250 (l2_accel->count == (interface->glort_count - 1))) {
1251 return ERR_PTR(-EBUSY);
1252 /* expand if we have hit the size limit */
1253 } else if (l2_accel->count == l2_accel->size) {
1254 old_l2_accel = l2_accel;
1255 size = offsetof(struct fm10k_l2_accel,
1256 macvlan[(l2_accel->size * 2) + 1]);
1257 l2_accel = kzalloc(size, GFP_KERNEL);
1258 if (!l2_accel)
1259 return ERR_PTR(-ENOMEM);
1260
1261 memcpy(l2_accel, old_l2_accel,
1262 offsetof(struct fm10k_l2_accel,
1263 macvlan[old_l2_accel->size]));
1264
1265 l2_accel->size = (old_l2_accel->size * 2) + 1;
1266
1267 /* update pointers */
1268 fm10k_assign_l2_accel(interface, l2_accel);
1269 kfree_rcu(old_l2_accel, rcu);
1270 }
1271
1272 /* add macvlan to accel table, and record GLORT for position */
1273 for (i = 0; i < l2_accel->size; i++) {
1274 if (!l2_accel->macvlan[i])
1275 break;
1276 }
1277
1278 /* record station */
1279 l2_accel->macvlan[i] = sdev;
1280 l2_accel->count++;
1281
1282 /* configure default DGLORT mapping for RSS/DCB */
1283 dglort.idx = fm10k_dglort_pf_rss;
1284 dglort.inner_rss = 1;
1285 dglort.rss_l = fls(interface->ring_feature[RING_F_RSS].mask);
1286 dglort.pc_l = fls(interface->ring_feature[RING_F_QOS].mask);
1287 dglort.glort = interface->glort;
1288 dglort.shared_l = fls(l2_accel->size);
1289 hw->mac.ops.configure_dglort_map(hw, &dglort);
1290
1291 /* Add rules for this specific dglort to the switch */
1292 fm10k_mbx_lock(interface);
1293
1294 glort = l2_accel->dglort + 1 + i;
1295 hw->mac.ops.update_xcast_mode(hw, glort, FM10K_XCAST_MODE_MULTI);
1296 hw->mac.ops.update_uc_addr(hw, glort, sdev->dev_addr, 0, true, 0);
1297
1298 fm10k_mbx_unlock(interface);
1299
1300 return sdev;
1301}
1302
1303static void fm10k_dfwd_del_station(struct net_device *dev, void *priv)
1304{
1305 struct fm10k_intfc *interface = netdev_priv(dev);
1306 struct fm10k_l2_accel *l2_accel = ACCESS_ONCE(interface->l2_accel);
1307 struct fm10k_dglort_cfg dglort = { 0 };
1308 struct fm10k_hw *hw = &interface->hw;
1309 struct net_device *sdev = priv;
1310 int i;
1311 u16 glort;
1312
1313 if (!l2_accel)
1314 return;
1315
1316 /* search table for matching interface */
1317 for (i = 0; i < l2_accel->size; i++) {
1318 if (l2_accel->macvlan[i] == sdev)
1319 break;
1320 }
1321
1322 /* exit if macvlan not found */
1323 if (i == l2_accel->size)
1324 return;
1325
1326 /* Remove any rules specific to this dglort */
1327 fm10k_mbx_lock(interface);
1328
1329 glort = l2_accel->dglort + 1 + i;
1330 hw->mac.ops.update_xcast_mode(hw, glort, FM10K_XCAST_MODE_NONE);
1331 hw->mac.ops.update_uc_addr(hw, glort, sdev->dev_addr, 0, false, 0);
1332
1333 fm10k_mbx_unlock(interface);
1334
1335 /* record removal */
1336 l2_accel->macvlan[i] = NULL;
1337 l2_accel->count--;
1338
1339 /* configure default DGLORT mapping for RSS/DCB */
1340 dglort.idx = fm10k_dglort_pf_rss;
1341 dglort.inner_rss = 1;
1342 dglort.rss_l = fls(interface->ring_feature[RING_F_RSS].mask);
1343 dglort.pc_l = fls(interface->ring_feature[RING_F_QOS].mask);
1344 dglort.glort = interface->glort;
Jacob Kellerf1f33222015-06-03 16:31:04 -07001345 dglort.shared_l = fls(l2_accel->size);
Alexander Duyck5cd5e2e2014-09-20 19:51:15 -04001346 hw->mac.ops.configure_dglort_map(hw, &dglort);
1347
1348 /* If table is empty remove it */
1349 if (l2_accel->count == 0) {
1350 fm10k_assign_l2_accel(interface, NULL);
1351 kfree_rcu(l2_accel, rcu);
1352 }
1353}
1354
Matthew Vick5bf33dc2015-01-29 07:17:27 +00001355static netdev_features_t fm10k_features_check(struct sk_buff *skb,
1356 struct net_device *dev,
1357 netdev_features_t features)
1358{
1359 if (!skb->encapsulation || fm10k_tx_encap_offload(skb))
1360 return features;
1361
Tom Herberta1882222015-12-14 11:19:43 -08001362 return features & ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK);
Matthew Vick5bf33dc2015-01-29 07:17:27 +00001363}
1364
Alexander Duyck0e7b3642014-09-20 19:48:10 -04001365static const struct net_device_ops fm10k_netdev_ops = {
Alexander Duyck504c5ea2014-09-20 19:48:29 -04001366 .ndo_open = fm10k_open,
1367 .ndo_stop = fm10k_close,
Alexander Duyck0e7b3642014-09-20 19:48:10 -04001368 .ndo_validate_addr = eth_validate_addr,
1369 .ndo_start_xmit = fm10k_xmit_frame,
1370 .ndo_set_mac_address = fm10k_set_mac,
1371 .ndo_change_mtu = fm10k_change_mtu,
Alexander Duyckb101c962014-09-20 19:50:03 -04001372 .ndo_tx_timeout = fm10k_tx_timeout,
Alexander Duyck8f5e20d2014-09-20 19:48:20 -04001373 .ndo_vlan_rx_add_vid = fm10k_vlan_rx_add_vid,
1374 .ndo_vlan_rx_kill_vid = fm10k_vlan_rx_kill_vid,
Alexander Duyck0e7b3642014-09-20 19:48:10 -04001375 .ndo_set_rx_mode = fm10k_set_rx_mode,
Alexander Duycke27ef592014-09-20 19:49:03 -04001376 .ndo_get_stats64 = fm10k_get_stats64,
John Fastabende4c67342016-02-16 21:16:15 -08001377 .ndo_setup_tc = __fm10k_setup_tc,
Alexander Duyck883a9cc2014-09-20 19:52:09 -04001378 .ndo_set_vf_mac = fm10k_ndo_set_vf_mac,
1379 .ndo_set_vf_vlan = fm10k_ndo_set_vf_vlan,
1380 .ndo_set_vf_rate = fm10k_ndo_set_vf_bw,
1381 .ndo_get_vf_config = fm10k_ndo_get_vf_config,
Alexander Duyckf174cdb2016-06-16 12:21:57 -07001382 .ndo_udp_tunnel_add = fm10k_add_vxlan_port,
1383 .ndo_udp_tunnel_del = fm10k_del_vxlan_port,
Alexander Duyck5cd5e2e2014-09-20 19:51:15 -04001384 .ndo_dfwd_add_station = fm10k_dfwd_add_station,
1385 .ndo_dfwd_del_station = fm10k_dfwd_del_station,
Jeff Kirsher8b4a98c2015-04-10 19:14:31 -07001386#ifdef CONFIG_NET_POLL_CONTROLLER
1387 .ndo_poll_controller = fm10k_netpoll,
1388#endif
Matthew Vick5bf33dc2015-01-29 07:17:27 +00001389 .ndo_features_check = fm10k_features_check,
Alexander Duyck0e7b3642014-09-20 19:48:10 -04001390};
1391
1392#define DEFAULT_DEBUG_LEVEL_SHIFT 3
1393
Jacob Kellere0244902015-10-16 10:56:56 -07001394struct net_device *fm10k_alloc_netdev(const struct fm10k_info *info)
Alexander Duyck0e7b3642014-09-20 19:48:10 -04001395{
Jacob Kellere0244902015-10-16 10:56:56 -07001396 netdev_features_t hw_features;
Alexander Duyck0e7b3642014-09-20 19:48:10 -04001397 struct fm10k_intfc *interface;
1398 struct net_device *dev;
1399
Alexander Duycke27ef592014-09-20 19:49:03 -04001400 dev = alloc_etherdev_mq(sizeof(struct fm10k_intfc), MAX_QUEUES);
Alexander Duyck0e7b3642014-09-20 19:48:10 -04001401 if (!dev)
1402 return NULL;
1403
1404 /* set net device and ethtool ops */
1405 dev->netdev_ops = &fm10k_netdev_ops;
Alexander Duyck82dd0f72014-09-20 19:50:15 -04001406 fm10k_set_ethtool_ops(dev);
Alexander Duyck0e7b3642014-09-20 19:48:10 -04001407
1408 /* configure default debug level */
1409 interface = netdev_priv(dev);
Bruce Allanfcdb0a92015-12-22 13:43:49 -08001410 interface->msg_enable = BIT(DEFAULT_DEBUG_LEVEL_SHIFT) - 1;
Alexander Duyck0e7b3642014-09-20 19:48:10 -04001411
1412 /* configure default features */
Alexander Duyck76a540d2014-09-20 19:51:02 -04001413 dev->features |= NETIF_F_IP_CSUM |
1414 NETIF_F_IPV6_CSUM |
1415 NETIF_F_SG |
1416 NETIF_F_TSO |
1417 NETIF_F_TSO6 |
1418 NETIF_F_TSO_ECN |
Alexander Duyck76a540d2014-09-20 19:51:02 -04001419 NETIF_F_RXHASH |
1420 NETIF_F_RXCSUM;
Alexander Duyck0e7b3642014-09-20 19:48:10 -04001421
Jacob Kellere0244902015-10-16 10:56:56 -07001422 /* Only the PF can support VXLAN and NVGRE tunnel offloads */
1423 if (info->mac == fm10k_mac_pf) {
1424 dev->hw_enc_features = NETIF_F_IP_CSUM |
1425 NETIF_F_TSO |
1426 NETIF_F_TSO6 |
1427 NETIF_F_TSO_ECN |
1428 NETIF_F_GSO_UDP_TUNNEL |
1429 NETIF_F_IPV6_CSUM |
1430 NETIF_F_SG;
1431
1432 dev->features |= NETIF_F_GSO_UDP_TUNNEL;
1433 }
1434
Alexander Duyck0e7b3642014-09-20 19:48:10 -04001435 /* all features defined to this point should be changeable */
Jacob Kellere0244902015-10-16 10:56:56 -07001436 hw_features = dev->features;
Alexander Duyck0e7b3642014-09-20 19:48:10 -04001437
Alexander Duyck5cd5e2e2014-09-20 19:51:15 -04001438 /* allow user to enable L2 forwarding acceleration */
Jacob Kellere0244902015-10-16 10:56:56 -07001439 hw_features |= NETIF_F_HW_L2FW_DOFFLOAD;
Alexander Duyck5cd5e2e2014-09-20 19:51:15 -04001440
Alexander Duyck0e7b3642014-09-20 19:48:10 -04001441 /* configure VLAN features */
1442 dev->vlan_features |= dev->features;
1443
Alexander Duyck8f5e20d2014-09-20 19:48:20 -04001444 /* we want to leave these both on as we cannot disable VLAN tag
1445 * insertion or stripping on the hardware since it is contained
1446 * in the FTAG and not in the frame itself.
1447 */
1448 dev->features |= NETIF_F_HW_VLAN_CTAG_TX |
1449 NETIF_F_HW_VLAN_CTAG_RX |
1450 NETIF_F_HW_VLAN_CTAG_FILTER;
1451
1452 dev->priv_flags |= IFF_UNICAST_FLT;
1453
Jacob Kellere0244902015-10-16 10:56:56 -07001454 dev->hw_features |= hw_features;
1455
Alexander Duyck0e7b3642014-09-20 19:48:10 -04001456 return dev;
1457}