]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blob - drivers/net/ethernet/intel/iavf/iavf_main.c
7bc8b646e37c893eae30a10044b6d07052dca6af
[mirror_ubuntu-jammy-kernel.git] / drivers / net / ethernet / intel / iavf / iavf_main.c
1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright(c) 2013 - 2018 Intel Corporation. */
3
4 #include "iavf.h"
5 #include "iavf_prototype.h"
6 #include "iavf_client.h"
7 /* All iavf tracepoints are defined by the include below, which must
8 * be included exactly once across the whole kernel with
9 * CREATE_TRACE_POINTS defined
10 */
11 #define CREATE_TRACE_POINTS
12 #include "iavf_trace.h"
13
14 static int iavf_setup_all_tx_resources(struct iavf_adapter *adapter);
15 static int iavf_setup_all_rx_resources(struct iavf_adapter *adapter);
16 static int iavf_close(struct net_device *netdev);
17 static void iavf_init_get_resources(struct iavf_adapter *adapter);
18 static int iavf_check_reset_complete(struct iavf_hw *hw);
19
20 char iavf_driver_name[] = "iavf";
21 static const char iavf_driver_string[] =
22 "Intel(R) Ethernet Adaptive Virtual Function Network Driver";
23
24 static const char iavf_copyright[] =
25 "Copyright (c) 2013 - 2018 Intel Corporation.";
26
27 /* iavf_pci_tbl - PCI Device ID Table
28 *
29 * Wildcard entries (PCI_ANY_ID) should come last
30 * Last entry must be all 0s
31 *
32 * { Vendor ID, Device ID, SubVendor ID, SubDevice ID,
33 * Class, Class Mask, private data (not used) }
34 */
35 static const struct pci_device_id iavf_pci_tbl[] = {
36 {PCI_VDEVICE(INTEL, IAVF_DEV_ID_VF), 0},
37 {PCI_VDEVICE(INTEL, IAVF_DEV_ID_VF_HV), 0},
38 {PCI_VDEVICE(INTEL, IAVF_DEV_ID_X722_VF), 0},
39 {PCI_VDEVICE(INTEL, IAVF_DEV_ID_ADAPTIVE_VF), 0},
40 /* required last entry */
41 {0, }
42 };
43
44 MODULE_DEVICE_TABLE(pci, iavf_pci_tbl);
45
46 MODULE_ALIAS("i40evf");
47 MODULE_AUTHOR("Intel Corporation, <linux.nics@intel.com>");
48 MODULE_DESCRIPTION("Intel(R) Ethernet Adaptive Virtual Function Network Driver");
49 MODULE_LICENSE("GPL v2");
50
51 static const struct net_device_ops iavf_netdev_ops;
52 struct workqueue_struct *iavf_wq;
53
54 /**
55 * iavf_allocate_dma_mem_d - OS specific memory alloc for shared code
56 * @hw: pointer to the HW structure
57 * @mem: ptr to mem struct to fill out
58 * @size: size of memory requested
59 * @alignment: what to align the allocation to
60 **/
61 enum iavf_status iavf_allocate_dma_mem_d(struct iavf_hw *hw,
62 struct iavf_dma_mem *mem,
63 u64 size, u32 alignment)
64 {
65 struct iavf_adapter *adapter = (struct iavf_adapter *)hw->back;
66
67 if (!mem)
68 return IAVF_ERR_PARAM;
69
70 mem->size = ALIGN(size, alignment);
71 mem->va = dma_alloc_coherent(&adapter->pdev->dev, mem->size,
72 (dma_addr_t *)&mem->pa, GFP_KERNEL);
73 if (mem->va)
74 return 0;
75 else
76 return IAVF_ERR_NO_MEMORY;
77 }
78
79 /**
80 * iavf_free_dma_mem_d - OS specific memory free for shared code
81 * @hw: pointer to the HW structure
82 * @mem: ptr to mem struct to free
83 **/
84 enum iavf_status iavf_free_dma_mem_d(struct iavf_hw *hw,
85 struct iavf_dma_mem *mem)
86 {
87 struct iavf_adapter *adapter = (struct iavf_adapter *)hw->back;
88
89 if (!mem || !mem->va)
90 return IAVF_ERR_PARAM;
91 dma_free_coherent(&adapter->pdev->dev, mem->size,
92 mem->va, (dma_addr_t)mem->pa);
93 return 0;
94 }
95
96 /**
97 * iavf_allocate_virt_mem_d - OS specific memory alloc for shared code
98 * @hw: pointer to the HW structure
99 * @mem: ptr to mem struct to fill out
100 * @size: size of memory requested
101 **/
102 enum iavf_status iavf_allocate_virt_mem_d(struct iavf_hw *hw,
103 struct iavf_virt_mem *mem, u32 size)
104 {
105 if (!mem)
106 return IAVF_ERR_PARAM;
107
108 mem->size = size;
109 mem->va = kzalloc(size, GFP_KERNEL);
110
111 if (mem->va)
112 return 0;
113 else
114 return IAVF_ERR_NO_MEMORY;
115 }
116
117 /**
118 * iavf_free_virt_mem_d - OS specific memory free for shared code
119 * @hw: pointer to the HW structure
120 * @mem: ptr to mem struct to free
121 **/
122 enum iavf_status iavf_free_virt_mem_d(struct iavf_hw *hw,
123 struct iavf_virt_mem *mem)
124 {
125 if (!mem)
126 return IAVF_ERR_PARAM;
127
128 /* it's ok to kfree a NULL pointer */
129 kfree(mem->va);
130
131 return 0;
132 }
133
134 /**
135 * iavf_lock_timeout - try to lock mutex but give up after timeout
136 * @lock: mutex that should be locked
137 * @msecs: timeout in msecs
138 *
139 * Returns 0 on success, negative on failure
140 **/
141 int iavf_lock_timeout(struct mutex *lock, unsigned int msecs)
142 {
143 unsigned int wait, delay = 10;
144
145 for (wait = 0; wait < msecs; wait += delay) {
146 if (mutex_trylock(lock))
147 return 0;
148
149 msleep(delay);
150 }
151
152 return -1;
153 }
154
155 /**
156 * iavf_schedule_reset - Set the flags and schedule a reset event
157 * @adapter: board private structure
158 **/
159 void iavf_schedule_reset(struct iavf_adapter *adapter)
160 {
161 if (!(adapter->flags &
162 (IAVF_FLAG_RESET_PENDING | IAVF_FLAG_RESET_NEEDED))) {
163 adapter->flags |= IAVF_FLAG_RESET_NEEDED;
164 queue_work(iavf_wq, &adapter->reset_task);
165 }
166 }
167
168 /**
169 * iavf_schedule_request_stats - Set the flags and schedule statistics request
170 * @adapter: board private structure
171 *
172 * Sets IAVF_FLAG_AQ_REQUEST_STATS flag so iavf_watchdog_task() will explicitly
173 * request and refresh ethtool stats
174 **/
175 void iavf_schedule_request_stats(struct iavf_adapter *adapter)
176 {
177 adapter->aq_required |= IAVF_FLAG_AQ_REQUEST_STATS;
178 mod_delayed_work(iavf_wq, &adapter->watchdog_task, 0);
179 }
180
181 /**
182 * iavf_tx_timeout - Respond to a Tx Hang
183 * @netdev: network interface device structure
184 * @txqueue: queue number that is timing out
185 **/
186 static void iavf_tx_timeout(struct net_device *netdev, unsigned int txqueue)
187 {
188 struct iavf_adapter *adapter = netdev_priv(netdev);
189
190 adapter->tx_timeout_count++;
191 iavf_schedule_reset(adapter);
192 }
193
194 /**
195 * iavf_misc_irq_disable - Mask off interrupt generation on the NIC
196 * @adapter: board private structure
197 **/
198 static void iavf_misc_irq_disable(struct iavf_adapter *adapter)
199 {
200 struct iavf_hw *hw = &adapter->hw;
201
202 if (!adapter->msix_entries)
203 return;
204
205 wr32(hw, IAVF_VFINT_DYN_CTL01, 0);
206
207 iavf_flush(hw);
208
209 synchronize_irq(adapter->msix_entries[0].vector);
210 }
211
212 /**
213 * iavf_misc_irq_enable - Enable default interrupt generation settings
214 * @adapter: board private structure
215 **/
216 static void iavf_misc_irq_enable(struct iavf_adapter *adapter)
217 {
218 struct iavf_hw *hw = &adapter->hw;
219
220 wr32(hw, IAVF_VFINT_DYN_CTL01, IAVF_VFINT_DYN_CTL01_INTENA_MASK |
221 IAVF_VFINT_DYN_CTL01_ITR_INDX_MASK);
222 wr32(hw, IAVF_VFINT_ICR0_ENA1, IAVF_VFINT_ICR0_ENA1_ADMINQ_MASK);
223
224 iavf_flush(hw);
225 }
226
227 /**
228 * iavf_irq_disable - Mask off interrupt generation on the NIC
229 * @adapter: board private structure
230 **/
231 static void iavf_irq_disable(struct iavf_adapter *adapter)
232 {
233 int i;
234 struct iavf_hw *hw = &adapter->hw;
235
236 if (!adapter->msix_entries)
237 return;
238
239 for (i = 1; i < adapter->num_msix_vectors; i++) {
240 wr32(hw, IAVF_VFINT_DYN_CTLN1(i - 1), 0);
241 synchronize_irq(adapter->msix_entries[i].vector);
242 }
243 iavf_flush(hw);
244 }
245
246 /**
247 * iavf_irq_enable_queues - Enable interrupt for specified queues
248 * @adapter: board private structure
249 * @mask: bitmap of queues to enable
250 **/
251 void iavf_irq_enable_queues(struct iavf_adapter *adapter, u32 mask)
252 {
253 struct iavf_hw *hw = &adapter->hw;
254 int i;
255
256 for (i = 1; i < adapter->num_msix_vectors; i++) {
257 if (mask & BIT(i - 1)) {
258 wr32(hw, IAVF_VFINT_DYN_CTLN1(i - 1),
259 IAVF_VFINT_DYN_CTLN1_INTENA_MASK |
260 IAVF_VFINT_DYN_CTLN1_ITR_INDX_MASK);
261 }
262 }
263 }
264
265 /**
266 * iavf_irq_enable - Enable default interrupt generation settings
267 * @adapter: board private structure
268 * @flush: boolean value whether to run rd32()
269 **/
270 void iavf_irq_enable(struct iavf_adapter *adapter, bool flush)
271 {
272 struct iavf_hw *hw = &adapter->hw;
273
274 iavf_misc_irq_enable(adapter);
275 iavf_irq_enable_queues(adapter, ~0);
276
277 if (flush)
278 iavf_flush(hw);
279 }
280
281 /**
282 * iavf_msix_aq - Interrupt handler for vector 0
283 * @irq: interrupt number
284 * @data: pointer to netdev
285 **/
286 static irqreturn_t iavf_msix_aq(int irq, void *data)
287 {
288 struct net_device *netdev = data;
289 struct iavf_adapter *adapter = netdev_priv(netdev);
290 struct iavf_hw *hw = &adapter->hw;
291
292 /* handle non-queue interrupts, these reads clear the registers */
293 rd32(hw, IAVF_VFINT_ICR01);
294 rd32(hw, IAVF_VFINT_ICR0_ENA1);
295
296 /* schedule work on the private workqueue */
297 queue_work(iavf_wq, &adapter->adminq_task);
298
299 return IRQ_HANDLED;
300 }
301
302 /**
303 * iavf_msix_clean_rings - MSIX mode Interrupt Handler
304 * @irq: interrupt number
305 * @data: pointer to a q_vector
306 **/
307 static irqreturn_t iavf_msix_clean_rings(int irq, void *data)
308 {
309 struct iavf_q_vector *q_vector = data;
310
311 if (!q_vector->tx.ring && !q_vector->rx.ring)
312 return IRQ_HANDLED;
313
314 napi_schedule_irqoff(&q_vector->napi);
315
316 return IRQ_HANDLED;
317 }
318
319 /**
320 * iavf_map_vector_to_rxq - associate irqs with rx queues
321 * @adapter: board private structure
322 * @v_idx: interrupt number
323 * @r_idx: queue number
324 **/
325 static void
326 iavf_map_vector_to_rxq(struct iavf_adapter *adapter, int v_idx, int r_idx)
327 {
328 struct iavf_q_vector *q_vector = &adapter->q_vectors[v_idx];
329 struct iavf_ring *rx_ring = &adapter->rx_rings[r_idx];
330 struct iavf_hw *hw = &adapter->hw;
331
332 rx_ring->q_vector = q_vector;
333 rx_ring->next = q_vector->rx.ring;
334 rx_ring->vsi = &adapter->vsi;
335 q_vector->rx.ring = rx_ring;
336 q_vector->rx.count++;
337 q_vector->rx.next_update = jiffies + 1;
338 q_vector->rx.target_itr = ITR_TO_REG(rx_ring->itr_setting);
339 q_vector->ring_mask |= BIT(r_idx);
340 wr32(hw, IAVF_VFINT_ITRN1(IAVF_RX_ITR, q_vector->reg_idx),
341 q_vector->rx.current_itr >> 1);
342 q_vector->rx.current_itr = q_vector->rx.target_itr;
343 }
344
345 /**
346 * iavf_map_vector_to_txq - associate irqs with tx queues
347 * @adapter: board private structure
348 * @v_idx: interrupt number
349 * @t_idx: queue number
350 **/
351 static void
352 iavf_map_vector_to_txq(struct iavf_adapter *adapter, int v_idx, int t_idx)
353 {
354 struct iavf_q_vector *q_vector = &adapter->q_vectors[v_idx];
355 struct iavf_ring *tx_ring = &adapter->tx_rings[t_idx];
356 struct iavf_hw *hw = &adapter->hw;
357
358 tx_ring->q_vector = q_vector;
359 tx_ring->next = q_vector->tx.ring;
360 tx_ring->vsi = &adapter->vsi;
361 q_vector->tx.ring = tx_ring;
362 q_vector->tx.count++;
363 q_vector->tx.next_update = jiffies + 1;
364 q_vector->tx.target_itr = ITR_TO_REG(tx_ring->itr_setting);
365 q_vector->num_ringpairs++;
366 wr32(hw, IAVF_VFINT_ITRN1(IAVF_TX_ITR, q_vector->reg_idx),
367 q_vector->tx.target_itr >> 1);
368 q_vector->tx.current_itr = q_vector->tx.target_itr;
369 }
370
371 /**
372 * iavf_map_rings_to_vectors - Maps descriptor rings to vectors
373 * @adapter: board private structure to initialize
374 *
375 * This function maps descriptor rings to the queue-specific vectors
376 * we were allotted through the MSI-X enabling code. Ideally, we'd have
377 * one vector per ring/queue, but on a constrained vector budget, we
378 * group the rings as "efficiently" as possible. You would add new
379 * mapping configurations in here.
380 **/
381 static void iavf_map_rings_to_vectors(struct iavf_adapter *adapter)
382 {
383 int rings_remaining = adapter->num_active_queues;
384 int ridx = 0, vidx = 0;
385 int q_vectors;
386
387 q_vectors = adapter->num_msix_vectors - NONQ_VECS;
388
389 for (; ridx < rings_remaining; ridx++) {
390 iavf_map_vector_to_rxq(adapter, vidx, ridx);
391 iavf_map_vector_to_txq(adapter, vidx, ridx);
392
393 /* In the case where we have more queues than vectors, continue
394 * round-robin on vectors until all queues are mapped.
395 */
396 if (++vidx >= q_vectors)
397 vidx = 0;
398 }
399
400 adapter->aq_required |= IAVF_FLAG_AQ_MAP_VECTORS;
401 }
402
403 /**
404 * iavf_irq_affinity_notify - Callback for affinity changes
405 * @notify: context as to what irq was changed
406 * @mask: the new affinity mask
407 *
408 * This is a callback function used by the irq_set_affinity_notifier function
409 * so that we may register to receive changes to the irq affinity masks.
410 **/
411 static void iavf_irq_affinity_notify(struct irq_affinity_notify *notify,
412 const cpumask_t *mask)
413 {
414 struct iavf_q_vector *q_vector =
415 container_of(notify, struct iavf_q_vector, affinity_notify);
416
417 cpumask_copy(&q_vector->affinity_mask, mask);
418 }
419
420 /**
421 * iavf_irq_affinity_release - Callback for affinity notifier release
422 * @ref: internal core kernel usage
423 *
424 * This is a callback function used by the irq_set_affinity_notifier function
425 * to inform the current notification subscriber that they will no longer
426 * receive notifications.
427 **/
428 static void iavf_irq_affinity_release(struct kref *ref) {}
429
430 /**
431 * iavf_request_traffic_irqs - Initialize MSI-X interrupts
432 * @adapter: board private structure
433 * @basename: device basename
434 *
435 * Allocates MSI-X vectors for tx and rx handling, and requests
436 * interrupts from the kernel.
437 **/
438 static int
439 iavf_request_traffic_irqs(struct iavf_adapter *adapter, char *basename)
440 {
441 unsigned int vector, q_vectors;
442 unsigned int rx_int_idx = 0, tx_int_idx = 0;
443 int irq_num, err;
444 int cpu;
445
446 iavf_irq_disable(adapter);
447 /* Decrement for Other and TCP Timer vectors */
448 q_vectors = adapter->num_msix_vectors - NONQ_VECS;
449
450 for (vector = 0; vector < q_vectors; vector++) {
451 struct iavf_q_vector *q_vector = &adapter->q_vectors[vector];
452
453 irq_num = adapter->msix_entries[vector + NONQ_VECS].vector;
454
455 if (q_vector->tx.ring && q_vector->rx.ring) {
456 snprintf(q_vector->name, sizeof(q_vector->name),
457 "iavf-%s-TxRx-%d", basename, rx_int_idx++);
458 tx_int_idx++;
459 } else if (q_vector->rx.ring) {
460 snprintf(q_vector->name, sizeof(q_vector->name),
461 "iavf-%s-rx-%d", basename, rx_int_idx++);
462 } else if (q_vector->tx.ring) {
463 snprintf(q_vector->name, sizeof(q_vector->name),
464 "iavf-%s-tx-%d", basename, tx_int_idx++);
465 } else {
466 /* skip this unused q_vector */
467 continue;
468 }
469 err = request_irq(irq_num,
470 iavf_msix_clean_rings,
471 0,
472 q_vector->name,
473 q_vector);
474 if (err) {
475 dev_info(&adapter->pdev->dev,
476 "Request_irq failed, error: %d\n", err);
477 goto free_queue_irqs;
478 }
479 /* register for affinity change notifications */
480 q_vector->affinity_notify.notify = iavf_irq_affinity_notify;
481 q_vector->affinity_notify.release =
482 iavf_irq_affinity_release;
483 irq_set_affinity_notifier(irq_num, &q_vector->affinity_notify);
484 /* Spread the IRQ affinity hints across online CPUs. Note that
485 * get_cpu_mask returns a mask with a permanent lifetime so
486 * it's safe to use as a hint for irq_set_affinity_hint.
487 */
488 cpu = cpumask_local_spread(q_vector->v_idx, -1);
489 irq_set_affinity_hint(irq_num, get_cpu_mask(cpu));
490 }
491
492 return 0;
493
494 free_queue_irqs:
495 while (vector) {
496 vector--;
497 irq_num = adapter->msix_entries[vector + NONQ_VECS].vector;
498 irq_set_affinity_notifier(irq_num, NULL);
499 irq_set_affinity_hint(irq_num, NULL);
500 free_irq(irq_num, &adapter->q_vectors[vector]);
501 }
502 return err;
503 }
504
505 /**
506 * iavf_request_misc_irq - Initialize MSI-X interrupts
507 * @adapter: board private structure
508 *
509 * Allocates MSI-X vector 0 and requests interrupts from the kernel. This
510 * vector is only for the admin queue, and stays active even when the netdev
511 * is closed.
512 **/
513 static int iavf_request_misc_irq(struct iavf_adapter *adapter)
514 {
515 struct net_device *netdev = adapter->netdev;
516 int err;
517
518 snprintf(adapter->misc_vector_name,
519 sizeof(adapter->misc_vector_name) - 1, "iavf-%s:mbx",
520 dev_name(&adapter->pdev->dev));
521 err = request_irq(adapter->msix_entries[0].vector,
522 &iavf_msix_aq, 0,
523 adapter->misc_vector_name, netdev);
524 if (err) {
525 dev_err(&adapter->pdev->dev,
526 "request_irq for %s failed: %d\n",
527 adapter->misc_vector_name, err);
528 free_irq(adapter->msix_entries[0].vector, netdev);
529 }
530 return err;
531 }
532
533 /**
534 * iavf_free_traffic_irqs - Free MSI-X interrupts
535 * @adapter: board private structure
536 *
537 * Frees all MSI-X vectors other than 0.
538 **/
539 static void iavf_free_traffic_irqs(struct iavf_adapter *adapter)
540 {
541 int vector, irq_num, q_vectors;
542
543 if (!adapter->msix_entries)
544 return;
545
546 q_vectors = adapter->num_msix_vectors - NONQ_VECS;
547
548 for (vector = 0; vector < q_vectors; vector++) {
549 irq_num = adapter->msix_entries[vector + NONQ_VECS].vector;
550 irq_set_affinity_notifier(irq_num, NULL);
551 irq_set_affinity_hint(irq_num, NULL);
552 free_irq(irq_num, &adapter->q_vectors[vector]);
553 }
554 }
555
556 /**
557 * iavf_free_misc_irq - Free MSI-X miscellaneous vector
558 * @adapter: board private structure
559 *
560 * Frees MSI-X vector 0.
561 **/
562 static void iavf_free_misc_irq(struct iavf_adapter *adapter)
563 {
564 struct net_device *netdev = adapter->netdev;
565
566 if (!adapter->msix_entries)
567 return;
568
569 free_irq(adapter->msix_entries[0].vector, netdev);
570 }
571
572 /**
573 * iavf_configure_tx - Configure Transmit Unit after Reset
574 * @adapter: board private structure
575 *
576 * Configure the Tx unit of the MAC after a reset.
577 **/
578 static void iavf_configure_tx(struct iavf_adapter *adapter)
579 {
580 struct iavf_hw *hw = &adapter->hw;
581 int i;
582
583 for (i = 0; i < adapter->num_active_queues; i++)
584 adapter->tx_rings[i].tail = hw->hw_addr + IAVF_QTX_TAIL1(i);
585 }
586
587 /**
588 * iavf_configure_rx - Configure Receive Unit after Reset
589 * @adapter: board private structure
590 *
591 * Configure the Rx unit of the MAC after a reset.
592 **/
593 static void iavf_configure_rx(struct iavf_adapter *adapter)
594 {
595 unsigned int rx_buf_len = IAVF_RXBUFFER_2048;
596 struct iavf_hw *hw = &adapter->hw;
597 int i;
598
599 /* Legacy Rx will always default to a 2048 buffer size. */
600 #if (PAGE_SIZE < 8192)
601 if (!(adapter->flags & IAVF_FLAG_LEGACY_RX)) {
602 struct net_device *netdev = adapter->netdev;
603
604 /* For jumbo frames on systems with 4K pages we have to use
605 * an order 1 page, so we might as well increase the size
606 * of our Rx buffer to make better use of the available space
607 */
608 rx_buf_len = IAVF_RXBUFFER_3072;
609
610 /* We use a 1536 buffer size for configurations with
611 * standard Ethernet mtu. On x86 this gives us enough room
612 * for shared info and 192 bytes of padding.
613 */
614 if (!IAVF_2K_TOO_SMALL_WITH_PADDING &&
615 (netdev->mtu <= ETH_DATA_LEN))
616 rx_buf_len = IAVF_RXBUFFER_1536 - NET_IP_ALIGN;
617 }
618 #endif
619
620 for (i = 0; i < adapter->num_active_queues; i++) {
621 adapter->rx_rings[i].tail = hw->hw_addr + IAVF_QRX_TAIL1(i);
622 adapter->rx_rings[i].rx_buf_len = rx_buf_len;
623
624 if (adapter->flags & IAVF_FLAG_LEGACY_RX)
625 clear_ring_build_skb_enabled(&adapter->rx_rings[i]);
626 else
627 set_ring_build_skb_enabled(&adapter->rx_rings[i]);
628 }
629 }
630
631 /**
632 * iavf_find_vlan - Search filter list for specific vlan filter
633 * @adapter: board private structure
634 * @vlan: vlan tag
635 *
636 * Returns ptr to the filter object or NULL. Must be called while holding the
637 * mac_vlan_list_lock.
638 **/
639 static struct
640 iavf_vlan_filter *iavf_find_vlan(struct iavf_adapter *adapter, u16 vlan)
641 {
642 struct iavf_vlan_filter *f;
643
644 list_for_each_entry(f, &adapter->vlan_filter_list, list) {
645 if (vlan == f->vlan)
646 return f;
647 }
648 return NULL;
649 }
650
651 /**
652 * iavf_add_vlan - Add a vlan filter to the list
653 * @adapter: board private structure
654 * @vlan: VLAN tag
655 *
656 * Returns ptr to the filter object or NULL when no memory available.
657 **/
658 static struct
659 iavf_vlan_filter *iavf_add_vlan(struct iavf_adapter *adapter, u16 vlan)
660 {
661 struct iavf_vlan_filter *f = NULL;
662
663 spin_lock_bh(&adapter->mac_vlan_list_lock);
664
665 f = iavf_find_vlan(adapter, vlan);
666 if (!f) {
667 f = kzalloc(sizeof(*f), GFP_ATOMIC);
668 if (!f)
669 goto clearout;
670
671 f->vlan = vlan;
672
673 list_add_tail(&f->list, &adapter->vlan_filter_list);
674 f->add = true;
675 adapter->aq_required |= IAVF_FLAG_AQ_ADD_VLAN_FILTER;
676 }
677
678 clearout:
679 spin_unlock_bh(&adapter->mac_vlan_list_lock);
680 return f;
681 }
682
683 /**
684 * iavf_del_vlan - Remove a vlan filter from the list
685 * @adapter: board private structure
686 * @vlan: VLAN tag
687 **/
688 static void iavf_del_vlan(struct iavf_adapter *adapter, u16 vlan)
689 {
690 struct iavf_vlan_filter *f;
691
692 spin_lock_bh(&adapter->mac_vlan_list_lock);
693
694 f = iavf_find_vlan(adapter, vlan);
695 if (f) {
696 f->remove = true;
697 adapter->aq_required |= IAVF_FLAG_AQ_DEL_VLAN_FILTER;
698 }
699
700 spin_unlock_bh(&adapter->mac_vlan_list_lock);
701 }
702
703 /**
704 * iavf_restore_filters
705 * @adapter: board private structure
706 *
707 * Restore existing non MAC filters when VF netdev comes back up
708 **/
709 static void iavf_restore_filters(struct iavf_adapter *adapter)
710 {
711 u16 vid;
712
713 /* re-add all VLAN filters */
714 for_each_set_bit(vid, adapter->vsi.active_vlans, VLAN_N_VID)
715 iavf_add_vlan(adapter, vid);
716 }
717
718 /**
719 * iavf_vlan_rx_add_vid - Add a VLAN filter to a device
720 * @netdev: network device struct
721 * @proto: unused protocol data
722 * @vid: VLAN tag
723 **/
724 static int iavf_vlan_rx_add_vid(struct net_device *netdev,
725 __always_unused __be16 proto, u16 vid)
726 {
727 struct iavf_adapter *adapter = netdev_priv(netdev);
728
729 if (!VLAN_ALLOWED(adapter))
730 return -EIO;
731
732 if (iavf_add_vlan(adapter, vid) == NULL)
733 return -ENOMEM;
734
735 set_bit(vid, adapter->vsi.active_vlans);
736 return 0;
737 }
738
739 /**
740 * iavf_vlan_rx_kill_vid - Remove a VLAN filter from a device
741 * @netdev: network device struct
742 * @proto: unused protocol data
743 * @vid: VLAN tag
744 **/
745 static int iavf_vlan_rx_kill_vid(struct net_device *netdev,
746 __always_unused __be16 proto, u16 vid)
747 {
748 struct iavf_adapter *adapter = netdev_priv(netdev);
749
750 iavf_del_vlan(adapter, vid);
751 clear_bit(vid, adapter->vsi.active_vlans);
752
753 return 0;
754 }
755
756 /**
757 * iavf_find_filter - Search filter list for specific mac filter
758 * @adapter: board private structure
759 * @macaddr: the MAC address
760 *
761 * Returns ptr to the filter object or NULL. Must be called while holding the
762 * mac_vlan_list_lock.
763 **/
764 static struct
765 iavf_mac_filter *iavf_find_filter(struct iavf_adapter *adapter,
766 const u8 *macaddr)
767 {
768 struct iavf_mac_filter *f;
769
770 if (!macaddr)
771 return NULL;
772
773 list_for_each_entry(f, &adapter->mac_filter_list, list) {
774 if (ether_addr_equal(macaddr, f->macaddr))
775 return f;
776 }
777 return NULL;
778 }
779
780 /**
781 * iavf_add_filter - Add a mac filter to the filter list
782 * @adapter: board private structure
783 * @macaddr: the MAC address
784 *
785 * Returns ptr to the filter object or NULL when no memory available.
786 **/
787 struct iavf_mac_filter *iavf_add_filter(struct iavf_adapter *adapter,
788 const u8 *macaddr)
789 {
790 struct iavf_mac_filter *f;
791
792 if (!macaddr)
793 return NULL;
794
795 f = iavf_find_filter(adapter, macaddr);
796 if (!f) {
797 f = kzalloc(sizeof(*f), GFP_ATOMIC);
798 if (!f)
799 return f;
800
801 ether_addr_copy(f->macaddr, macaddr);
802
803 list_add_tail(&f->list, &adapter->mac_filter_list);
804 f->add = true;
805 f->is_new_mac = true;
806 adapter->aq_required |= IAVF_FLAG_AQ_ADD_MAC_FILTER;
807 } else {
808 f->remove = false;
809 }
810
811 return f;
812 }
813
814 /**
815 * iavf_set_mac - NDO callback to set port mac address
816 * @netdev: network interface device structure
817 * @p: pointer to an address structure
818 *
819 * Returns 0 on success, negative on failure
820 **/
821 static int iavf_set_mac(struct net_device *netdev, void *p)
822 {
823 struct iavf_adapter *adapter = netdev_priv(netdev);
824 struct iavf_hw *hw = &adapter->hw;
825 struct iavf_mac_filter *f;
826 struct sockaddr *addr = p;
827
828 if (!is_valid_ether_addr(addr->sa_data))
829 return -EADDRNOTAVAIL;
830
831 if (ether_addr_equal(netdev->dev_addr, addr->sa_data))
832 return 0;
833
834 spin_lock_bh(&adapter->mac_vlan_list_lock);
835
836 f = iavf_find_filter(adapter, hw->mac.addr);
837 if (f) {
838 f->remove = true;
839 adapter->aq_required |= IAVF_FLAG_AQ_DEL_MAC_FILTER;
840 }
841
842 f = iavf_add_filter(adapter, addr->sa_data);
843
844 spin_unlock_bh(&adapter->mac_vlan_list_lock);
845
846 if (f) {
847 ether_addr_copy(hw->mac.addr, addr->sa_data);
848 }
849
850 return (f == NULL) ? -ENOMEM : 0;
851 }
852
853 /**
854 * iavf_addr_sync - Callback for dev_(mc|uc)_sync to add address
855 * @netdev: the netdevice
856 * @addr: address to add
857 *
858 * Called by __dev_(mc|uc)_sync when an address needs to be added. We call
859 * __dev_(uc|mc)_sync from .set_rx_mode and guarantee to hold the hash lock.
860 */
861 static int iavf_addr_sync(struct net_device *netdev, const u8 *addr)
862 {
863 struct iavf_adapter *adapter = netdev_priv(netdev);
864
865 if (iavf_add_filter(adapter, addr))
866 return 0;
867 else
868 return -ENOMEM;
869 }
870
871 /**
872 * iavf_addr_unsync - Callback for dev_(mc|uc)_sync to remove address
873 * @netdev: the netdevice
874 * @addr: address to add
875 *
876 * Called by __dev_(mc|uc)_sync when an address needs to be removed. We call
877 * __dev_(uc|mc)_sync from .set_rx_mode and guarantee to hold the hash lock.
878 */
879 static int iavf_addr_unsync(struct net_device *netdev, const u8 *addr)
880 {
881 struct iavf_adapter *adapter = netdev_priv(netdev);
882 struct iavf_mac_filter *f;
883
884 /* Under some circumstances, we might receive a request to delete
885 * our own device address from our uc list. Because we store the
886 * device address in the VSI's MAC/VLAN filter list, we need to ignore
887 * such requests and not delete our device address from this list.
888 */
889 if (ether_addr_equal(addr, netdev->dev_addr))
890 return 0;
891
892 f = iavf_find_filter(adapter, addr);
893 if (f) {
894 f->remove = true;
895 adapter->aq_required |= IAVF_FLAG_AQ_DEL_MAC_FILTER;
896 }
897 return 0;
898 }
899
900 /**
901 * iavf_set_rx_mode - NDO callback to set the netdev filters
902 * @netdev: network interface device structure
903 **/
904 static void iavf_set_rx_mode(struct net_device *netdev)
905 {
906 struct iavf_adapter *adapter = netdev_priv(netdev);
907
908 spin_lock_bh(&adapter->mac_vlan_list_lock);
909 __dev_uc_sync(netdev, iavf_addr_sync, iavf_addr_unsync);
910 __dev_mc_sync(netdev, iavf_addr_sync, iavf_addr_unsync);
911 spin_unlock_bh(&adapter->mac_vlan_list_lock);
912
913 if (netdev->flags & IFF_PROMISC &&
914 !(adapter->flags & IAVF_FLAG_PROMISC_ON))
915 adapter->aq_required |= IAVF_FLAG_AQ_REQUEST_PROMISC;
916 else if (!(netdev->flags & IFF_PROMISC) &&
917 adapter->flags & IAVF_FLAG_PROMISC_ON)
918 adapter->aq_required |= IAVF_FLAG_AQ_RELEASE_PROMISC;
919
920 if (netdev->flags & IFF_ALLMULTI &&
921 !(adapter->flags & IAVF_FLAG_ALLMULTI_ON))
922 adapter->aq_required |= IAVF_FLAG_AQ_REQUEST_ALLMULTI;
923 else if (!(netdev->flags & IFF_ALLMULTI) &&
924 adapter->flags & IAVF_FLAG_ALLMULTI_ON)
925 adapter->aq_required |= IAVF_FLAG_AQ_RELEASE_ALLMULTI;
926 }
927
928 /**
929 * iavf_napi_enable_all - enable NAPI on all queue vectors
930 * @adapter: board private structure
931 **/
932 static void iavf_napi_enable_all(struct iavf_adapter *adapter)
933 {
934 int q_idx;
935 struct iavf_q_vector *q_vector;
936 int q_vectors = adapter->num_msix_vectors - NONQ_VECS;
937
938 for (q_idx = 0; q_idx < q_vectors; q_idx++) {
939 struct napi_struct *napi;
940
941 q_vector = &adapter->q_vectors[q_idx];
942 napi = &q_vector->napi;
943 napi_enable(napi);
944 }
945 }
946
947 /**
948 * iavf_napi_disable_all - disable NAPI on all queue vectors
949 * @adapter: board private structure
950 **/
951 static void iavf_napi_disable_all(struct iavf_adapter *adapter)
952 {
953 int q_idx;
954 struct iavf_q_vector *q_vector;
955 int q_vectors = adapter->num_msix_vectors - NONQ_VECS;
956
957 for (q_idx = 0; q_idx < q_vectors; q_idx++) {
958 q_vector = &adapter->q_vectors[q_idx];
959 napi_disable(&q_vector->napi);
960 }
961 }
962
963 /**
964 * iavf_configure - set up transmit and receive data structures
965 * @adapter: board private structure
966 **/
967 static void iavf_configure(struct iavf_adapter *adapter)
968 {
969 struct net_device *netdev = adapter->netdev;
970 int i;
971
972 iavf_set_rx_mode(netdev);
973
974 iavf_configure_tx(adapter);
975 iavf_configure_rx(adapter);
976 adapter->aq_required |= IAVF_FLAG_AQ_CONFIGURE_QUEUES;
977
978 for (i = 0; i < adapter->num_active_queues; i++) {
979 struct iavf_ring *ring = &adapter->rx_rings[i];
980
981 iavf_alloc_rx_buffers(ring, IAVF_DESC_UNUSED(ring));
982 }
983 }
984
985 /**
986 * iavf_up_complete - Finish the last steps of bringing up a connection
987 * @adapter: board private structure
988 *
989 * Expects to be called while holding the __IAVF_IN_CRITICAL_TASK bit lock.
990 **/
991 static void iavf_up_complete(struct iavf_adapter *adapter)
992 {
993 iavf_change_state(adapter, __IAVF_RUNNING);
994 clear_bit(__IAVF_VSI_DOWN, adapter->vsi.state);
995
996 iavf_napi_enable_all(adapter);
997
998 adapter->aq_required |= IAVF_FLAG_AQ_ENABLE_QUEUES;
999 if (CLIENT_ENABLED(adapter))
1000 adapter->flags |= IAVF_FLAG_CLIENT_NEEDS_OPEN;
1001 mod_delayed_work(iavf_wq, &adapter->watchdog_task, 0);
1002 }
1003
1004 /**
1005 * iavf_down - Shutdown the connection processing
1006 * @adapter: board private structure
1007 *
1008 * Expects to be called while holding the __IAVF_IN_CRITICAL_TASK bit lock.
1009 **/
1010 void iavf_down(struct iavf_adapter *adapter)
1011 {
1012 struct net_device *netdev = adapter->netdev;
1013 struct iavf_vlan_filter *vlf;
1014 struct iavf_cloud_filter *cf;
1015 struct iavf_fdir_fltr *fdir;
1016 struct iavf_mac_filter *f;
1017 struct iavf_adv_rss *rss;
1018
1019 if (adapter->state <= __IAVF_DOWN_PENDING)
1020 return;
1021
1022 netif_carrier_off(netdev);
1023 netif_tx_disable(netdev);
1024 adapter->link_up = false;
1025 iavf_napi_disable_all(adapter);
1026 iavf_irq_disable(adapter);
1027
1028 spin_lock_bh(&adapter->mac_vlan_list_lock);
1029
1030 /* clear the sync flag on all filters */
1031 __dev_uc_unsync(adapter->netdev, NULL);
1032 __dev_mc_unsync(adapter->netdev, NULL);
1033
1034 /* remove all MAC filters */
1035 list_for_each_entry(f, &adapter->mac_filter_list, list) {
1036 f->remove = true;
1037 }
1038
1039 /* remove all VLAN filters */
1040 list_for_each_entry(vlf, &adapter->vlan_filter_list, list) {
1041 vlf->remove = true;
1042 }
1043
1044 spin_unlock_bh(&adapter->mac_vlan_list_lock);
1045
1046 /* remove all cloud filters */
1047 spin_lock_bh(&adapter->cloud_filter_list_lock);
1048 list_for_each_entry(cf, &adapter->cloud_filter_list, list) {
1049 cf->del = true;
1050 }
1051 spin_unlock_bh(&adapter->cloud_filter_list_lock);
1052
1053 /* remove all Flow Director filters */
1054 spin_lock_bh(&adapter->fdir_fltr_lock);
1055 list_for_each_entry(fdir, &adapter->fdir_list_head, list) {
1056 fdir->state = IAVF_FDIR_FLTR_DEL_REQUEST;
1057 }
1058 spin_unlock_bh(&adapter->fdir_fltr_lock);
1059
1060 /* remove all advance RSS configuration */
1061 spin_lock_bh(&adapter->adv_rss_lock);
1062 list_for_each_entry(rss, &adapter->adv_rss_list_head, list)
1063 rss->state = IAVF_ADV_RSS_DEL_REQUEST;
1064 spin_unlock_bh(&adapter->adv_rss_lock);
1065
1066 if (!(adapter->flags & IAVF_FLAG_PF_COMMS_FAILED) &&
1067 adapter->state != __IAVF_RESETTING) {
1068 /* cancel any current operation */
1069 adapter->current_op = VIRTCHNL_OP_UNKNOWN;
1070 /* Schedule operations to close down the HW. Don't wait
1071 * here for this to complete. The watchdog is still running
1072 * and it will take care of this.
1073 */
1074 adapter->aq_required = IAVF_FLAG_AQ_DEL_MAC_FILTER;
1075 adapter->aq_required |= IAVF_FLAG_AQ_DEL_VLAN_FILTER;
1076 adapter->aq_required |= IAVF_FLAG_AQ_DEL_CLOUD_FILTER;
1077 adapter->aq_required |= IAVF_FLAG_AQ_DEL_FDIR_FILTER;
1078 adapter->aq_required |= IAVF_FLAG_AQ_DEL_ADV_RSS_CFG;
1079 adapter->aq_required |= IAVF_FLAG_AQ_DISABLE_QUEUES;
1080 }
1081
1082 mod_delayed_work(iavf_wq, &adapter->watchdog_task, 0);
1083 }
1084
1085 /**
1086 * iavf_acquire_msix_vectors - Setup the MSIX capability
1087 * @adapter: board private structure
1088 * @vectors: number of vectors to request
1089 *
1090 * Work with the OS to set up the MSIX vectors needed.
1091 *
1092 * Returns 0 on success, negative on failure
1093 **/
1094 static int
1095 iavf_acquire_msix_vectors(struct iavf_adapter *adapter, int vectors)
1096 {
1097 int err, vector_threshold;
1098
1099 /* We'll want at least 3 (vector_threshold):
1100 * 0) Other (Admin Queue and link, mostly)
1101 * 1) TxQ[0] Cleanup
1102 * 2) RxQ[0] Cleanup
1103 */
1104 vector_threshold = MIN_MSIX_COUNT;
1105
1106 /* The more we get, the more we will assign to Tx/Rx Cleanup
1107 * for the separate queues...where Rx Cleanup >= Tx Cleanup.
1108 * Right now, we simply care about how many we'll get; we'll
1109 * set them up later while requesting irq's.
1110 */
1111 err = pci_enable_msix_range(adapter->pdev, adapter->msix_entries,
1112 vector_threshold, vectors);
1113 if (err < 0) {
1114 dev_err(&adapter->pdev->dev, "Unable to allocate MSI-X interrupts\n");
1115 kfree(adapter->msix_entries);
1116 adapter->msix_entries = NULL;
1117 return err;
1118 }
1119
1120 /* Adjust for only the vectors we'll use, which is minimum
1121 * of max_msix_q_vectors + NONQ_VECS, or the number of
1122 * vectors we were allocated.
1123 */
1124 adapter->num_msix_vectors = err;
1125 return 0;
1126 }
1127
1128 /**
1129 * iavf_free_queues - Free memory for all rings
1130 * @adapter: board private structure to initialize
1131 *
1132 * Free all of the memory associated with queue pairs.
1133 **/
1134 static void iavf_free_queues(struct iavf_adapter *adapter)
1135 {
1136 if (!adapter->vsi_res)
1137 return;
1138 adapter->num_active_queues = 0;
1139 kfree(adapter->tx_rings);
1140 adapter->tx_rings = NULL;
1141 kfree(adapter->rx_rings);
1142 adapter->rx_rings = NULL;
1143 }
1144
1145 /**
1146 * iavf_alloc_queues - Allocate memory for all rings
1147 * @adapter: board private structure to initialize
1148 *
1149 * We allocate one ring per queue at run-time since we don't know the
1150 * number of queues at compile-time. The polling_netdev array is
1151 * intended for Multiqueue, but should work fine with a single queue.
1152 **/
1153 static int iavf_alloc_queues(struct iavf_adapter *adapter)
1154 {
1155 int i, num_active_queues;
1156
1157 /* If we're in reset reallocating queues we don't actually know yet for
1158 * certain the PF gave us the number of queues we asked for but we'll
1159 * assume it did. Once basic reset is finished we'll confirm once we
1160 * start negotiating config with PF.
1161 */
1162 if (adapter->num_req_queues)
1163 num_active_queues = adapter->num_req_queues;
1164 else if ((adapter->vf_res->vf_cap_flags & VIRTCHNL_VF_OFFLOAD_ADQ) &&
1165 adapter->num_tc)
1166 num_active_queues = adapter->ch_config.total_qps;
1167 else
1168 num_active_queues = min_t(int,
1169 adapter->vsi_res->num_queue_pairs,
1170 (int)(num_online_cpus()));
1171
1172
1173 adapter->tx_rings = kcalloc(num_active_queues,
1174 sizeof(struct iavf_ring), GFP_KERNEL);
1175 if (!adapter->tx_rings)
1176 goto err_out;
1177 adapter->rx_rings = kcalloc(num_active_queues,
1178 sizeof(struct iavf_ring), GFP_KERNEL);
1179 if (!adapter->rx_rings)
1180 goto err_out;
1181
1182 for (i = 0; i < num_active_queues; i++) {
1183 struct iavf_ring *tx_ring;
1184 struct iavf_ring *rx_ring;
1185
1186 tx_ring = &adapter->tx_rings[i];
1187
1188 tx_ring->queue_index = i;
1189 tx_ring->netdev = adapter->netdev;
1190 tx_ring->dev = &adapter->pdev->dev;
1191 tx_ring->count = adapter->tx_desc_count;
1192 tx_ring->itr_setting = IAVF_ITR_TX_DEF;
1193 if (adapter->flags & IAVF_FLAG_WB_ON_ITR_CAPABLE)
1194 tx_ring->flags |= IAVF_TXR_FLAGS_WB_ON_ITR;
1195
1196 rx_ring = &adapter->rx_rings[i];
1197 rx_ring->queue_index = i;
1198 rx_ring->netdev = adapter->netdev;
1199 rx_ring->dev = &adapter->pdev->dev;
1200 rx_ring->count = adapter->rx_desc_count;
1201 rx_ring->itr_setting = IAVF_ITR_RX_DEF;
1202 }
1203
1204 adapter->num_active_queues = num_active_queues;
1205
1206 return 0;
1207
1208 err_out:
1209 iavf_free_queues(adapter);
1210 return -ENOMEM;
1211 }
1212
1213 /**
1214 * iavf_set_interrupt_capability - set MSI-X or FAIL if not supported
1215 * @adapter: board private structure to initialize
1216 *
1217 * Attempt to configure the interrupts using the best available
1218 * capabilities of the hardware and the kernel.
1219 **/
1220 static int iavf_set_interrupt_capability(struct iavf_adapter *adapter)
1221 {
1222 int vector, v_budget;
1223 int pairs = 0;
1224 int err = 0;
1225
1226 if (!adapter->vsi_res) {
1227 err = -EIO;
1228 goto out;
1229 }
1230 pairs = adapter->num_active_queues;
1231
1232 /* It's easy to be greedy for MSI-X vectors, but it really doesn't do
1233 * us much good if we have more vectors than CPUs. However, we already
1234 * limit the total number of queues by the number of CPUs so we do not
1235 * need any further limiting here.
1236 */
1237 v_budget = min_t(int, pairs + NONQ_VECS,
1238 (int)adapter->vf_res->max_vectors);
1239
1240 adapter->msix_entries = kcalloc(v_budget,
1241 sizeof(struct msix_entry), GFP_KERNEL);
1242 if (!adapter->msix_entries) {
1243 err = -ENOMEM;
1244 goto out;
1245 }
1246
1247 for (vector = 0; vector < v_budget; vector++)
1248 adapter->msix_entries[vector].entry = vector;
1249
1250 err = iavf_acquire_msix_vectors(adapter, v_budget);
1251
1252 out:
1253 netif_set_real_num_rx_queues(adapter->netdev, pairs);
1254 netif_set_real_num_tx_queues(adapter->netdev, pairs);
1255 return err;
1256 }
1257
1258 /**
1259 * iavf_config_rss_aq - Configure RSS keys and lut by using AQ commands
1260 * @adapter: board private structure
1261 *
1262 * Return 0 on success, negative on failure
1263 **/
1264 static int iavf_config_rss_aq(struct iavf_adapter *adapter)
1265 {
1266 struct iavf_aqc_get_set_rss_key_data *rss_key =
1267 (struct iavf_aqc_get_set_rss_key_data *)adapter->rss_key;
1268 struct iavf_hw *hw = &adapter->hw;
1269 int ret = 0;
1270
1271 if (adapter->current_op != VIRTCHNL_OP_UNKNOWN) {
1272 /* bail because we already have a command pending */
1273 dev_err(&adapter->pdev->dev, "Cannot configure RSS, command %d pending\n",
1274 adapter->current_op);
1275 return -EBUSY;
1276 }
1277
1278 ret = iavf_aq_set_rss_key(hw, adapter->vsi.id, rss_key);
1279 if (ret) {
1280 dev_err(&adapter->pdev->dev, "Cannot set RSS key, err %s aq_err %s\n",
1281 iavf_stat_str(hw, ret),
1282 iavf_aq_str(hw, hw->aq.asq_last_status));
1283 return ret;
1284
1285 }
1286
1287 ret = iavf_aq_set_rss_lut(hw, adapter->vsi.id, false,
1288 adapter->rss_lut, adapter->rss_lut_size);
1289 if (ret) {
1290 dev_err(&adapter->pdev->dev, "Cannot set RSS lut, err %s aq_err %s\n",
1291 iavf_stat_str(hw, ret),
1292 iavf_aq_str(hw, hw->aq.asq_last_status));
1293 }
1294
1295 return ret;
1296
1297 }
1298
1299 /**
1300 * iavf_config_rss_reg - Configure RSS keys and lut by writing registers
1301 * @adapter: board private structure
1302 *
1303 * Returns 0 on success, negative on failure
1304 **/
1305 static int iavf_config_rss_reg(struct iavf_adapter *adapter)
1306 {
1307 struct iavf_hw *hw = &adapter->hw;
1308 u32 *dw;
1309 u16 i;
1310
1311 dw = (u32 *)adapter->rss_key;
1312 for (i = 0; i <= adapter->rss_key_size / 4; i++)
1313 wr32(hw, IAVF_VFQF_HKEY(i), dw[i]);
1314
1315 dw = (u32 *)adapter->rss_lut;
1316 for (i = 0; i <= adapter->rss_lut_size / 4; i++)
1317 wr32(hw, IAVF_VFQF_HLUT(i), dw[i]);
1318
1319 iavf_flush(hw);
1320
1321 return 0;
1322 }
1323
1324 /**
1325 * iavf_config_rss - Configure RSS keys and lut
1326 * @adapter: board private structure
1327 *
1328 * Returns 0 on success, negative on failure
1329 **/
1330 int iavf_config_rss(struct iavf_adapter *adapter)
1331 {
1332
1333 if (RSS_PF(adapter)) {
1334 adapter->aq_required |= IAVF_FLAG_AQ_SET_RSS_LUT |
1335 IAVF_FLAG_AQ_SET_RSS_KEY;
1336 return 0;
1337 } else if (RSS_AQ(adapter)) {
1338 return iavf_config_rss_aq(adapter);
1339 } else {
1340 return iavf_config_rss_reg(adapter);
1341 }
1342 }
1343
1344 /**
1345 * iavf_fill_rss_lut - Fill the lut with default values
1346 * @adapter: board private structure
1347 **/
1348 static void iavf_fill_rss_lut(struct iavf_adapter *adapter)
1349 {
1350 u16 i;
1351
1352 for (i = 0; i < adapter->rss_lut_size; i++)
1353 adapter->rss_lut[i] = i % adapter->num_active_queues;
1354 }
1355
1356 /**
1357 * iavf_init_rss - Prepare for RSS
1358 * @adapter: board private structure
1359 *
1360 * Return 0 on success, negative on failure
1361 **/
1362 static int iavf_init_rss(struct iavf_adapter *adapter)
1363 {
1364 struct iavf_hw *hw = &adapter->hw;
1365 int ret;
1366
1367 if (!RSS_PF(adapter)) {
1368 /* Enable PCTYPES for RSS, TCP/UDP with IPv4/IPv6 */
1369 if (adapter->vf_res->vf_cap_flags &
1370 VIRTCHNL_VF_OFFLOAD_RSS_PCTYPE_V2)
1371 adapter->hena = IAVF_DEFAULT_RSS_HENA_EXPANDED;
1372 else
1373 adapter->hena = IAVF_DEFAULT_RSS_HENA;
1374
1375 wr32(hw, IAVF_VFQF_HENA(0), (u32)adapter->hena);
1376 wr32(hw, IAVF_VFQF_HENA(1), (u32)(adapter->hena >> 32));
1377 }
1378
1379 iavf_fill_rss_lut(adapter);
1380 netdev_rss_key_fill((void *)adapter->rss_key, adapter->rss_key_size);
1381 ret = iavf_config_rss(adapter);
1382
1383 return ret;
1384 }
1385
1386 /**
1387 * iavf_alloc_q_vectors - Allocate memory for interrupt vectors
1388 * @adapter: board private structure to initialize
1389 *
1390 * We allocate one q_vector per queue interrupt. If allocation fails we
1391 * return -ENOMEM.
1392 **/
1393 static int iavf_alloc_q_vectors(struct iavf_adapter *adapter)
1394 {
1395 int q_idx = 0, num_q_vectors;
1396 struct iavf_q_vector *q_vector;
1397
1398 num_q_vectors = adapter->num_msix_vectors - NONQ_VECS;
1399 adapter->q_vectors = kcalloc(num_q_vectors, sizeof(*q_vector),
1400 GFP_KERNEL);
1401 if (!adapter->q_vectors)
1402 return -ENOMEM;
1403
1404 for (q_idx = 0; q_idx < num_q_vectors; q_idx++) {
1405 q_vector = &adapter->q_vectors[q_idx];
1406 q_vector->adapter = adapter;
1407 q_vector->vsi = &adapter->vsi;
1408 q_vector->v_idx = q_idx;
1409 q_vector->reg_idx = q_idx;
1410 cpumask_copy(&q_vector->affinity_mask, cpu_possible_mask);
1411 netif_napi_add(adapter->netdev, &q_vector->napi,
1412 iavf_napi_poll, NAPI_POLL_WEIGHT);
1413 }
1414
1415 return 0;
1416 }
1417
1418 /**
1419 * iavf_free_q_vectors - Free memory allocated for interrupt vectors
1420 * @adapter: board private structure to initialize
1421 *
1422 * This function frees the memory allocated to the q_vectors. In addition if
1423 * NAPI is enabled it will delete any references to the NAPI struct prior
1424 * to freeing the q_vector.
1425 **/
1426 static void iavf_free_q_vectors(struct iavf_adapter *adapter)
1427 {
1428 int q_idx, num_q_vectors;
1429 int napi_vectors;
1430
1431 if (!adapter->q_vectors)
1432 return;
1433
1434 num_q_vectors = adapter->num_msix_vectors - NONQ_VECS;
1435 napi_vectors = adapter->num_active_queues;
1436
1437 for (q_idx = 0; q_idx < num_q_vectors; q_idx++) {
1438 struct iavf_q_vector *q_vector = &adapter->q_vectors[q_idx];
1439
1440 if (q_idx < napi_vectors)
1441 netif_napi_del(&q_vector->napi);
1442 }
1443 kfree(adapter->q_vectors);
1444 adapter->q_vectors = NULL;
1445 }
1446
1447 /**
1448 * iavf_reset_interrupt_capability - Reset MSIX setup
1449 * @adapter: board private structure
1450 *
1451 **/
1452 void iavf_reset_interrupt_capability(struct iavf_adapter *adapter)
1453 {
1454 if (!adapter->msix_entries)
1455 return;
1456
1457 pci_disable_msix(adapter->pdev);
1458 kfree(adapter->msix_entries);
1459 adapter->msix_entries = NULL;
1460 }
1461
1462 /**
1463 * iavf_init_interrupt_scheme - Determine if MSIX is supported and init
1464 * @adapter: board private structure to initialize
1465 *
1466 **/
1467 int iavf_init_interrupt_scheme(struct iavf_adapter *adapter)
1468 {
1469 int err;
1470
1471 err = iavf_alloc_queues(adapter);
1472 if (err) {
1473 dev_err(&adapter->pdev->dev,
1474 "Unable to allocate memory for queues\n");
1475 goto err_alloc_queues;
1476 }
1477
1478 rtnl_lock();
1479 err = iavf_set_interrupt_capability(adapter);
1480 rtnl_unlock();
1481 if (err) {
1482 dev_err(&adapter->pdev->dev,
1483 "Unable to setup interrupt capabilities\n");
1484 goto err_set_interrupt;
1485 }
1486
1487 err = iavf_alloc_q_vectors(adapter);
1488 if (err) {
1489 dev_err(&adapter->pdev->dev,
1490 "Unable to allocate memory for queue vectors\n");
1491 goto err_alloc_q_vectors;
1492 }
1493
1494 /* If we've made it so far while ADq flag being ON, then we haven't
1495 * bailed out anywhere in middle. And ADq isn't just enabled but actual
1496 * resources have been allocated in the reset path.
1497 * Now we can truly claim that ADq is enabled.
1498 */
1499 if ((adapter->vf_res->vf_cap_flags & VIRTCHNL_VF_OFFLOAD_ADQ) &&
1500 adapter->num_tc)
1501 dev_info(&adapter->pdev->dev, "ADq Enabled, %u TCs created",
1502 adapter->num_tc);
1503
1504 dev_info(&adapter->pdev->dev, "Multiqueue %s: Queue pair count = %u",
1505 (adapter->num_active_queues > 1) ? "Enabled" : "Disabled",
1506 adapter->num_active_queues);
1507
1508 return 0;
1509 err_alloc_q_vectors:
1510 iavf_reset_interrupt_capability(adapter);
1511 err_set_interrupt:
1512 iavf_free_queues(adapter);
1513 err_alloc_queues:
1514 return err;
1515 }
1516
1517 /**
1518 * iavf_free_rss - Free memory used by RSS structs
1519 * @adapter: board private structure
1520 **/
1521 static void iavf_free_rss(struct iavf_adapter *adapter)
1522 {
1523 kfree(adapter->rss_key);
1524 adapter->rss_key = NULL;
1525
1526 kfree(adapter->rss_lut);
1527 adapter->rss_lut = NULL;
1528 }
1529
1530 /**
1531 * iavf_reinit_interrupt_scheme - Reallocate queues and vectors
1532 * @adapter: board private structure
1533 *
1534 * Returns 0 on success, negative on failure
1535 **/
1536 static int iavf_reinit_interrupt_scheme(struct iavf_adapter *adapter)
1537 {
1538 struct net_device *netdev = adapter->netdev;
1539 int err;
1540
1541 if (netif_running(netdev))
1542 iavf_free_traffic_irqs(adapter);
1543 iavf_free_misc_irq(adapter);
1544 iavf_reset_interrupt_capability(adapter);
1545 iavf_free_q_vectors(adapter);
1546 iavf_free_queues(adapter);
1547
1548 err = iavf_init_interrupt_scheme(adapter);
1549 if (err)
1550 goto err;
1551
1552 netif_tx_stop_all_queues(netdev);
1553
1554 err = iavf_request_misc_irq(adapter);
1555 if (err)
1556 goto err;
1557
1558 set_bit(__IAVF_VSI_DOWN, adapter->vsi.state);
1559
1560 iavf_map_rings_to_vectors(adapter);
1561 err:
1562 return err;
1563 }
1564
1565 /**
1566 * iavf_process_aq_command - process aq_required flags
1567 * and sends aq command
1568 * @adapter: pointer to iavf adapter structure
1569 *
1570 * Returns 0 on success
1571 * Returns error code if no command was sent
1572 * or error code if the command failed.
1573 **/
1574 static int iavf_process_aq_command(struct iavf_adapter *adapter)
1575 {
1576 if (adapter->aq_required & IAVF_FLAG_AQ_GET_CONFIG)
1577 return iavf_send_vf_config_msg(adapter);
1578 if (adapter->aq_required & IAVF_FLAG_AQ_DISABLE_QUEUES) {
1579 iavf_disable_queues(adapter);
1580 return 0;
1581 }
1582
1583 if (adapter->aq_required & IAVF_FLAG_AQ_MAP_VECTORS) {
1584 iavf_map_queues(adapter);
1585 return 0;
1586 }
1587
1588 if (adapter->aq_required & IAVF_FLAG_AQ_ADD_MAC_FILTER) {
1589 iavf_add_ether_addrs(adapter);
1590 return 0;
1591 }
1592
1593 if (adapter->aq_required & IAVF_FLAG_AQ_ADD_VLAN_FILTER) {
1594 iavf_add_vlans(adapter);
1595 return 0;
1596 }
1597
1598 if (adapter->aq_required & IAVF_FLAG_AQ_DEL_MAC_FILTER) {
1599 iavf_del_ether_addrs(adapter);
1600 return 0;
1601 }
1602
1603 if (adapter->aq_required & IAVF_FLAG_AQ_DEL_VLAN_FILTER) {
1604 iavf_del_vlans(adapter);
1605 return 0;
1606 }
1607
1608 if (adapter->aq_required & IAVF_FLAG_AQ_ENABLE_VLAN_STRIPPING) {
1609 iavf_enable_vlan_stripping(adapter);
1610 return 0;
1611 }
1612
1613 if (adapter->aq_required & IAVF_FLAG_AQ_DISABLE_VLAN_STRIPPING) {
1614 iavf_disable_vlan_stripping(adapter);
1615 return 0;
1616 }
1617
1618 if (adapter->aq_required & IAVF_FLAG_AQ_CONFIGURE_QUEUES) {
1619 iavf_configure_queues(adapter);
1620 return 0;
1621 }
1622
1623 if (adapter->aq_required & IAVF_FLAG_AQ_ENABLE_QUEUES) {
1624 iavf_enable_queues(adapter);
1625 return 0;
1626 }
1627
1628 if (adapter->aq_required & IAVF_FLAG_AQ_CONFIGURE_RSS) {
1629 /* This message goes straight to the firmware, not the
1630 * PF, so we don't have to set current_op as we will
1631 * not get a response through the ARQ.
1632 */
1633 adapter->aq_required &= ~IAVF_FLAG_AQ_CONFIGURE_RSS;
1634 return 0;
1635 }
1636 if (adapter->aq_required & IAVF_FLAG_AQ_GET_HENA) {
1637 iavf_get_hena(adapter);
1638 return 0;
1639 }
1640 if (adapter->aq_required & IAVF_FLAG_AQ_SET_HENA) {
1641 iavf_set_hena(adapter);
1642 return 0;
1643 }
1644 if (adapter->aq_required & IAVF_FLAG_AQ_SET_RSS_KEY) {
1645 iavf_set_rss_key(adapter);
1646 return 0;
1647 }
1648 if (adapter->aq_required & IAVF_FLAG_AQ_SET_RSS_LUT) {
1649 iavf_set_rss_lut(adapter);
1650 return 0;
1651 }
1652
1653 if (adapter->aq_required & IAVF_FLAG_AQ_REQUEST_PROMISC) {
1654 iavf_set_promiscuous(adapter, FLAG_VF_UNICAST_PROMISC |
1655 FLAG_VF_MULTICAST_PROMISC);
1656 return 0;
1657 }
1658
1659 if (adapter->aq_required & IAVF_FLAG_AQ_REQUEST_ALLMULTI) {
1660 iavf_set_promiscuous(adapter, FLAG_VF_MULTICAST_PROMISC);
1661 return 0;
1662 }
1663 if ((adapter->aq_required & IAVF_FLAG_AQ_RELEASE_PROMISC) ||
1664 (adapter->aq_required & IAVF_FLAG_AQ_RELEASE_ALLMULTI)) {
1665 iavf_set_promiscuous(adapter, 0);
1666 return 0;
1667 }
1668
1669 if (adapter->aq_required & IAVF_FLAG_AQ_ENABLE_CHANNELS) {
1670 iavf_enable_channels(adapter);
1671 return 0;
1672 }
1673
1674 if (adapter->aq_required & IAVF_FLAG_AQ_DISABLE_CHANNELS) {
1675 iavf_disable_channels(adapter);
1676 return 0;
1677 }
1678 if (adapter->aq_required & IAVF_FLAG_AQ_ADD_CLOUD_FILTER) {
1679 iavf_add_cloud_filter(adapter);
1680 return 0;
1681 }
1682
1683 if (adapter->aq_required & IAVF_FLAG_AQ_DEL_CLOUD_FILTER) {
1684 iavf_del_cloud_filter(adapter);
1685 return 0;
1686 }
1687 if (adapter->aq_required & IAVF_FLAG_AQ_DEL_CLOUD_FILTER) {
1688 iavf_del_cloud_filter(adapter);
1689 return 0;
1690 }
1691 if (adapter->aq_required & IAVF_FLAG_AQ_ADD_CLOUD_FILTER) {
1692 iavf_add_cloud_filter(adapter);
1693 return 0;
1694 }
1695 if (adapter->aq_required & IAVF_FLAG_AQ_ADD_FDIR_FILTER) {
1696 iavf_add_fdir_filter(adapter);
1697 return IAVF_SUCCESS;
1698 }
1699 if (adapter->aq_required & IAVF_FLAG_AQ_DEL_FDIR_FILTER) {
1700 iavf_del_fdir_filter(adapter);
1701 return IAVF_SUCCESS;
1702 }
1703 if (adapter->aq_required & IAVF_FLAG_AQ_ADD_ADV_RSS_CFG) {
1704 iavf_add_adv_rss_cfg(adapter);
1705 return 0;
1706 }
1707 if (adapter->aq_required & IAVF_FLAG_AQ_DEL_ADV_RSS_CFG) {
1708 iavf_del_adv_rss_cfg(adapter);
1709 return 0;
1710 }
1711 if (adapter->aq_required & IAVF_FLAG_AQ_REQUEST_STATS) {
1712 iavf_request_stats(adapter);
1713 return 0;
1714 }
1715
1716 return -EAGAIN;
1717 }
1718
1719 /**
1720 * iavf_startup - first step of driver startup
1721 * @adapter: board private structure
1722 *
1723 * Function process __IAVF_STARTUP driver state.
1724 * When success the state is changed to __IAVF_INIT_VERSION_CHECK
1725 * when fails the state is changed to __IAVF_INIT_FAILED
1726 **/
1727 static void iavf_startup(struct iavf_adapter *adapter)
1728 {
1729 struct pci_dev *pdev = adapter->pdev;
1730 struct iavf_hw *hw = &adapter->hw;
1731 int err;
1732
1733 WARN_ON(adapter->state != __IAVF_STARTUP);
1734
1735 /* driver loaded, probe complete */
1736 adapter->flags &= ~IAVF_FLAG_PF_COMMS_FAILED;
1737 adapter->flags &= ~IAVF_FLAG_RESET_PENDING;
1738 err = iavf_set_mac_type(hw);
1739 if (err) {
1740 dev_err(&pdev->dev, "Failed to set MAC type (%d)\n", err);
1741 goto err;
1742 }
1743
1744 err = iavf_check_reset_complete(hw);
1745 if (err) {
1746 dev_info(&pdev->dev, "Device is still in reset (%d), retrying\n",
1747 err);
1748 goto err;
1749 }
1750 hw->aq.num_arq_entries = IAVF_AQ_LEN;
1751 hw->aq.num_asq_entries = IAVF_AQ_LEN;
1752 hw->aq.arq_buf_size = IAVF_MAX_AQ_BUF_SIZE;
1753 hw->aq.asq_buf_size = IAVF_MAX_AQ_BUF_SIZE;
1754
1755 err = iavf_init_adminq(hw);
1756 if (err) {
1757 dev_err(&pdev->dev, "Failed to init Admin Queue (%d)\n", err);
1758 goto err;
1759 }
1760 err = iavf_send_api_ver(adapter);
1761 if (err) {
1762 dev_err(&pdev->dev, "Unable to send to PF (%d)\n", err);
1763 iavf_shutdown_adminq(hw);
1764 goto err;
1765 }
1766 iavf_change_state(adapter, __IAVF_INIT_VERSION_CHECK);
1767 return;
1768 err:
1769 iavf_change_state(adapter, __IAVF_INIT_FAILED);
1770 }
1771
1772 /**
1773 * iavf_init_version_check - second step of driver startup
1774 * @adapter: board private structure
1775 *
1776 * Function process __IAVF_INIT_VERSION_CHECK driver state.
1777 * When success the state is changed to __IAVF_INIT_GET_RESOURCES
1778 * when fails the state is changed to __IAVF_INIT_FAILED
1779 **/
1780 static void iavf_init_version_check(struct iavf_adapter *adapter)
1781 {
1782 struct pci_dev *pdev = adapter->pdev;
1783 struct iavf_hw *hw = &adapter->hw;
1784 int err = -EAGAIN;
1785
1786 WARN_ON(adapter->state != __IAVF_INIT_VERSION_CHECK);
1787
1788 if (!iavf_asq_done(hw)) {
1789 dev_err(&pdev->dev, "Admin queue command never completed\n");
1790 iavf_shutdown_adminq(hw);
1791 iavf_change_state(adapter, __IAVF_STARTUP);
1792 goto err;
1793 }
1794
1795 /* aq msg sent, awaiting reply */
1796 err = iavf_verify_api_ver(adapter);
1797 if (err) {
1798 if (err == IAVF_ERR_ADMIN_QUEUE_NO_WORK)
1799 err = iavf_send_api_ver(adapter);
1800 else
1801 dev_err(&pdev->dev, "Unsupported PF API version %d.%d, expected %d.%d\n",
1802 adapter->pf_version.major,
1803 adapter->pf_version.minor,
1804 VIRTCHNL_VERSION_MAJOR,
1805 VIRTCHNL_VERSION_MINOR);
1806 goto err;
1807 }
1808 err = iavf_send_vf_config_msg(adapter);
1809 if (err) {
1810 dev_err(&pdev->dev, "Unable to send config request (%d)\n",
1811 err);
1812 goto err;
1813 }
1814 iavf_change_state(adapter, __IAVF_INIT_GET_RESOURCES);
1815 return;
1816 err:
1817 iavf_change_state(adapter, __IAVF_INIT_FAILED);
1818 }
1819
1820 /**
1821 * iavf_init_get_resources - third step of driver startup
1822 * @adapter: board private structure
1823 *
1824 * Function process __IAVF_INIT_GET_RESOURCES driver state and
1825 * finishes driver initialization procedure.
1826 * When success the state is changed to __IAVF_DOWN
1827 * when fails the state is changed to __IAVF_INIT_FAILED
1828 **/
1829 static void iavf_init_get_resources(struct iavf_adapter *adapter)
1830 {
1831 struct net_device *netdev = adapter->netdev;
1832 struct pci_dev *pdev = adapter->pdev;
1833 struct iavf_hw *hw = &adapter->hw;
1834 int err;
1835
1836 WARN_ON(adapter->state != __IAVF_INIT_GET_RESOURCES);
1837 /* aq msg sent, awaiting reply */
1838 if (!adapter->vf_res) {
1839 adapter->vf_res = kzalloc(IAVF_VIRTCHNL_VF_RESOURCE_SIZE,
1840 GFP_KERNEL);
1841 if (!adapter->vf_res) {
1842 err = -ENOMEM;
1843 goto err;
1844 }
1845 }
1846 err = iavf_get_vf_config(adapter);
1847 if (err == IAVF_ERR_ADMIN_QUEUE_NO_WORK) {
1848 err = iavf_send_vf_config_msg(adapter);
1849 goto err;
1850 } else if (err == IAVF_ERR_PARAM) {
1851 /* We only get ERR_PARAM if the device is in a very bad
1852 * state or if we've been disabled for previous bad
1853 * behavior. Either way, we're done now.
1854 */
1855 iavf_shutdown_adminq(hw);
1856 dev_err(&pdev->dev, "Unable to get VF config due to PF error condition, not retrying\n");
1857 return;
1858 }
1859 if (err) {
1860 dev_err(&pdev->dev, "Unable to get VF config (%d)\n", err);
1861 goto err_alloc;
1862 }
1863
1864 err = iavf_process_config(adapter);
1865 if (err)
1866 goto err_alloc;
1867 adapter->current_op = VIRTCHNL_OP_UNKNOWN;
1868
1869 adapter->flags |= IAVF_FLAG_RX_CSUM_ENABLED;
1870
1871 netdev->netdev_ops = &iavf_netdev_ops;
1872 iavf_set_ethtool_ops(netdev);
1873 netdev->watchdog_timeo = 5 * HZ;
1874
1875 /* MTU range: 68 - 9710 */
1876 netdev->min_mtu = ETH_MIN_MTU;
1877 netdev->max_mtu = IAVF_MAX_RXBUFFER - IAVF_PACKET_HDR_PAD;
1878
1879 if (!is_valid_ether_addr(adapter->hw.mac.addr)) {
1880 dev_info(&pdev->dev, "Invalid MAC address %pM, using random\n",
1881 adapter->hw.mac.addr);
1882 eth_hw_addr_random(netdev);
1883 ether_addr_copy(adapter->hw.mac.addr, netdev->dev_addr);
1884 } else {
1885 ether_addr_copy(netdev->dev_addr, adapter->hw.mac.addr);
1886 ether_addr_copy(netdev->perm_addr, adapter->hw.mac.addr);
1887 }
1888
1889 adapter->tx_desc_count = IAVF_DEFAULT_TXD;
1890 adapter->rx_desc_count = IAVF_DEFAULT_RXD;
1891 err = iavf_init_interrupt_scheme(adapter);
1892 if (err)
1893 goto err_sw_init;
1894 iavf_map_rings_to_vectors(adapter);
1895 if (adapter->vf_res->vf_cap_flags &
1896 VIRTCHNL_VF_OFFLOAD_WB_ON_ITR)
1897 adapter->flags |= IAVF_FLAG_WB_ON_ITR_CAPABLE;
1898
1899 err = iavf_request_misc_irq(adapter);
1900 if (err)
1901 goto err_sw_init;
1902
1903 netif_carrier_off(netdev);
1904 adapter->link_up = false;
1905
1906 /* set the semaphore to prevent any callbacks after device registration
1907 * up to time when state of driver will be set to __IAVF_DOWN
1908 */
1909 rtnl_lock();
1910 if (!adapter->netdev_registered) {
1911 err = register_netdevice(netdev);
1912 if (err) {
1913 rtnl_unlock();
1914 goto err_register;
1915 }
1916 }
1917
1918 adapter->netdev_registered = true;
1919
1920 netif_tx_stop_all_queues(netdev);
1921 if (CLIENT_ALLOWED(adapter)) {
1922 err = iavf_lan_add_device(adapter);
1923 if (err)
1924 dev_info(&pdev->dev, "Failed to add VF to client API service list: %d\n",
1925 err);
1926 }
1927 dev_info(&pdev->dev, "MAC address: %pM\n", adapter->hw.mac.addr);
1928 if (netdev->features & NETIF_F_GRO)
1929 dev_info(&pdev->dev, "GRO is enabled\n");
1930
1931 iavf_change_state(adapter, __IAVF_DOWN);
1932 set_bit(__IAVF_VSI_DOWN, adapter->vsi.state);
1933 rtnl_unlock();
1934
1935 iavf_misc_irq_enable(adapter);
1936 wake_up(&adapter->down_waitqueue);
1937
1938 adapter->rss_key = kzalloc(adapter->rss_key_size, GFP_KERNEL);
1939 adapter->rss_lut = kzalloc(adapter->rss_lut_size, GFP_KERNEL);
1940 if (!adapter->rss_key || !adapter->rss_lut) {
1941 err = -ENOMEM;
1942 goto err_mem;
1943 }
1944 if (RSS_AQ(adapter))
1945 adapter->aq_required |= IAVF_FLAG_AQ_CONFIGURE_RSS;
1946 else
1947 iavf_init_rss(adapter);
1948
1949 return;
1950 err_mem:
1951 iavf_free_rss(adapter);
1952 err_register:
1953 iavf_free_misc_irq(adapter);
1954 err_sw_init:
1955 iavf_reset_interrupt_capability(adapter);
1956 err_alloc:
1957 kfree(adapter->vf_res);
1958 adapter->vf_res = NULL;
1959 err:
1960 iavf_change_state(adapter, __IAVF_INIT_FAILED);
1961 }
1962
1963 /**
1964 * iavf_watchdog_task - Periodic call-back task
1965 * @work: pointer to work_struct
1966 **/
1967 static void iavf_watchdog_task(struct work_struct *work)
1968 {
1969 struct iavf_adapter *adapter = container_of(work,
1970 struct iavf_adapter,
1971 watchdog_task.work);
1972 struct iavf_hw *hw = &adapter->hw;
1973 u32 reg_val;
1974
1975 if (!mutex_trylock(&adapter->crit_lock))
1976 goto restart_watchdog;
1977
1978 if (adapter->flags & IAVF_FLAG_PF_COMMS_FAILED)
1979 iavf_change_state(adapter, __IAVF_COMM_FAILED);
1980
1981 if (adapter->flags & IAVF_FLAG_RESET_NEEDED &&
1982 adapter->state != __IAVF_RESETTING) {
1983 iavf_change_state(adapter, __IAVF_RESETTING);
1984 adapter->aq_required = 0;
1985 adapter->current_op = VIRTCHNL_OP_UNKNOWN;
1986 }
1987
1988 switch (adapter->state) {
1989 case __IAVF_STARTUP:
1990 iavf_startup(adapter);
1991 mutex_unlock(&adapter->crit_lock);
1992 queue_delayed_work(iavf_wq, &adapter->watchdog_task,
1993 msecs_to_jiffies(30));
1994 return;
1995 case __IAVF_INIT_VERSION_CHECK:
1996 iavf_init_version_check(adapter);
1997 mutex_unlock(&adapter->crit_lock);
1998 queue_delayed_work(iavf_wq, &adapter->watchdog_task,
1999 msecs_to_jiffies(30));
2000 return;
2001 case __IAVF_INIT_GET_RESOURCES:
2002 iavf_init_get_resources(adapter);
2003 mutex_unlock(&adapter->crit_lock);
2004 queue_delayed_work(iavf_wq, &adapter->watchdog_task,
2005 msecs_to_jiffies(1));
2006 return;
2007 case __IAVF_INIT_FAILED:
2008 if (++adapter->aq_wait_count > IAVF_AQ_MAX_ERR) {
2009 dev_err(&adapter->pdev->dev,
2010 "Failed to communicate with PF; waiting before retry\n");
2011 adapter->flags |= IAVF_FLAG_PF_COMMS_FAILED;
2012 iavf_shutdown_adminq(hw);
2013 mutex_unlock(&adapter->crit_lock);
2014 queue_delayed_work(iavf_wq,
2015 &adapter->watchdog_task, (5 * HZ));
2016 return;
2017 }
2018 /* Try again from failed step*/
2019 iavf_change_state(adapter, adapter->last_state);
2020 mutex_unlock(&adapter->crit_lock);
2021 queue_delayed_work(iavf_wq, &adapter->watchdog_task, HZ);
2022 return;
2023 case __IAVF_COMM_FAILED:
2024 reg_val = rd32(hw, IAVF_VFGEN_RSTAT) &
2025 IAVF_VFGEN_RSTAT_VFR_STATE_MASK;
2026 if (reg_val == VIRTCHNL_VFR_VFACTIVE ||
2027 reg_val == VIRTCHNL_VFR_COMPLETED) {
2028 /* A chance for redemption! */
2029 dev_err(&adapter->pdev->dev,
2030 "Hardware came out of reset. Attempting reinit.\n");
2031 /* When init task contacts the PF and
2032 * gets everything set up again, it'll restart the
2033 * watchdog for us. Down, boy. Sit. Stay. Woof.
2034 */
2035 iavf_change_state(adapter, __IAVF_STARTUP);
2036 adapter->flags &= ~IAVF_FLAG_PF_COMMS_FAILED;
2037 }
2038 adapter->aq_required = 0;
2039 adapter->current_op = VIRTCHNL_OP_UNKNOWN;
2040 queue_delayed_work(iavf_wq,
2041 &adapter->watchdog_task,
2042 msecs_to_jiffies(10));
2043 return;
2044 case __IAVF_RESETTING:
2045 mutex_unlock(&adapter->crit_lock);
2046 queue_delayed_work(iavf_wq, &adapter->watchdog_task, HZ * 2);
2047 return;
2048 case __IAVF_DOWN:
2049 case __IAVF_DOWN_PENDING:
2050 case __IAVF_TESTING:
2051 case __IAVF_RUNNING:
2052 if (adapter->current_op) {
2053 if (!iavf_asq_done(hw)) {
2054 dev_dbg(&adapter->pdev->dev,
2055 "Admin queue timeout\n");
2056 iavf_send_api_ver(adapter);
2057 }
2058 } else {
2059 /* An error will be returned if no commands were
2060 * processed; use this opportunity to update stats
2061 */
2062 if (iavf_process_aq_command(adapter) &&
2063 adapter->state == __IAVF_RUNNING)
2064 iavf_request_stats(adapter);
2065 }
2066 if (adapter->state == __IAVF_RUNNING)
2067 iavf_detect_recover_hung(&adapter->vsi);
2068 break;
2069 case __IAVF_REMOVE:
2070 mutex_unlock(&adapter->crit_lock);
2071 return;
2072 default:
2073 return;
2074 }
2075
2076 /* check for hw reset */
2077 reg_val = rd32(hw, IAVF_VF_ARQLEN1) & IAVF_VF_ARQLEN1_ARQENABLE_MASK;
2078 if (!reg_val) {
2079 iavf_change_state(adapter, __IAVF_RESETTING);
2080 adapter->flags |= IAVF_FLAG_RESET_PENDING;
2081 adapter->aq_required = 0;
2082 adapter->current_op = VIRTCHNL_OP_UNKNOWN;
2083 dev_err(&adapter->pdev->dev, "Hardware reset detected\n");
2084 queue_work(iavf_wq, &adapter->reset_task);
2085 mutex_unlock(&adapter->crit_lock);
2086 queue_delayed_work(iavf_wq,
2087 &adapter->watchdog_task, HZ * 2);
2088 return;
2089 }
2090
2091 schedule_delayed_work(&adapter->client_task, msecs_to_jiffies(5));
2092 mutex_unlock(&adapter->crit_lock);
2093 restart_watchdog:
2094 queue_work(iavf_wq, &adapter->adminq_task);
2095 if (adapter->aq_required)
2096 queue_delayed_work(iavf_wq, &adapter->watchdog_task,
2097 msecs_to_jiffies(20));
2098 else
2099 queue_delayed_work(iavf_wq, &adapter->watchdog_task, HZ * 2);
2100 }
2101
2102 static void iavf_disable_vf(struct iavf_adapter *adapter)
2103 {
2104 struct iavf_mac_filter *f, *ftmp;
2105 struct iavf_vlan_filter *fv, *fvtmp;
2106 struct iavf_cloud_filter *cf, *cftmp;
2107
2108 adapter->flags |= IAVF_FLAG_PF_COMMS_FAILED;
2109
2110 /* We don't use netif_running() because it may be true prior to
2111 * ndo_open() returning, so we can't assume it means all our open
2112 * tasks have finished, since we're not holding the rtnl_lock here.
2113 */
2114 if (adapter->state == __IAVF_RUNNING) {
2115 set_bit(__IAVF_VSI_DOWN, adapter->vsi.state);
2116 netif_carrier_off(adapter->netdev);
2117 netif_tx_disable(adapter->netdev);
2118 adapter->link_up = false;
2119 iavf_napi_disable_all(adapter);
2120 iavf_irq_disable(adapter);
2121 iavf_free_traffic_irqs(adapter);
2122 iavf_free_all_tx_resources(adapter);
2123 iavf_free_all_rx_resources(adapter);
2124 }
2125
2126 spin_lock_bh(&adapter->mac_vlan_list_lock);
2127
2128 /* Delete all of the filters */
2129 list_for_each_entry_safe(f, ftmp, &adapter->mac_filter_list, list) {
2130 list_del(&f->list);
2131 kfree(f);
2132 }
2133
2134 list_for_each_entry_safe(fv, fvtmp, &adapter->vlan_filter_list, list) {
2135 list_del(&fv->list);
2136 kfree(fv);
2137 }
2138
2139 spin_unlock_bh(&adapter->mac_vlan_list_lock);
2140
2141 spin_lock_bh(&adapter->cloud_filter_list_lock);
2142 list_for_each_entry_safe(cf, cftmp, &adapter->cloud_filter_list, list) {
2143 list_del(&cf->list);
2144 kfree(cf);
2145 adapter->num_cloud_filters--;
2146 }
2147 spin_unlock_bh(&adapter->cloud_filter_list_lock);
2148
2149 iavf_free_misc_irq(adapter);
2150 iavf_reset_interrupt_capability(adapter);
2151 iavf_free_q_vectors(adapter);
2152 iavf_free_queues(adapter);
2153 memset(adapter->vf_res, 0, IAVF_VIRTCHNL_VF_RESOURCE_SIZE);
2154 iavf_shutdown_adminq(&adapter->hw);
2155 adapter->netdev->flags &= ~IFF_UP;
2156 mutex_unlock(&adapter->crit_lock);
2157 adapter->flags &= ~IAVF_FLAG_RESET_PENDING;
2158 iavf_change_state(adapter, __IAVF_DOWN);
2159 wake_up(&adapter->down_waitqueue);
2160 dev_info(&adapter->pdev->dev, "Reset task did not complete, VF disabled\n");
2161 }
2162
2163 /**
2164 * iavf_reset_task - Call-back task to handle hardware reset
2165 * @work: pointer to work_struct
2166 *
2167 * During reset we need to shut down and reinitialize the admin queue
2168 * before we can use it to communicate with the PF again. We also clear
2169 * and reinit the rings because that context is lost as well.
2170 **/
2171 static void iavf_reset_task(struct work_struct *work)
2172 {
2173 struct iavf_adapter *adapter = container_of(work,
2174 struct iavf_adapter,
2175 reset_task);
2176 struct virtchnl_vf_resource *vfres = adapter->vf_res;
2177 struct net_device *netdev = adapter->netdev;
2178 struct iavf_hw *hw = &adapter->hw;
2179 struct iavf_mac_filter *f, *ftmp;
2180 struct iavf_cloud_filter *cf;
2181 u32 reg_val;
2182 int i = 0, err;
2183 bool running;
2184
2185 /* When device is being removed it doesn't make sense to run the reset
2186 * task, just return in such a case.
2187 */
2188 if (mutex_is_locked(&adapter->remove_lock))
2189 return;
2190
2191 if (iavf_lock_timeout(&adapter->crit_lock, 200)) {
2192 schedule_work(&adapter->reset_task);
2193 return;
2194 }
2195 while (!mutex_trylock(&adapter->client_lock))
2196 usleep_range(500, 1000);
2197 if (CLIENT_ENABLED(adapter)) {
2198 adapter->flags &= ~(IAVF_FLAG_CLIENT_NEEDS_OPEN |
2199 IAVF_FLAG_CLIENT_NEEDS_CLOSE |
2200 IAVF_FLAG_CLIENT_NEEDS_L2_PARAMS |
2201 IAVF_FLAG_SERVICE_CLIENT_REQUESTED);
2202 cancel_delayed_work_sync(&adapter->client_task);
2203 iavf_notify_client_close(&adapter->vsi, true);
2204 }
2205 iavf_misc_irq_disable(adapter);
2206 if (adapter->flags & IAVF_FLAG_RESET_NEEDED) {
2207 adapter->flags &= ~IAVF_FLAG_RESET_NEEDED;
2208 /* Restart the AQ here. If we have been reset but didn't
2209 * detect it, or if the PF had to reinit, our AQ will be hosed.
2210 */
2211 iavf_shutdown_adminq(hw);
2212 iavf_init_adminq(hw);
2213 iavf_request_reset(adapter);
2214 }
2215 adapter->flags |= IAVF_FLAG_RESET_PENDING;
2216
2217 /* poll until we see the reset actually happen */
2218 for (i = 0; i < IAVF_RESET_WAIT_DETECTED_COUNT; i++) {
2219 reg_val = rd32(hw, IAVF_VF_ARQLEN1) &
2220 IAVF_VF_ARQLEN1_ARQENABLE_MASK;
2221 if (!reg_val)
2222 break;
2223 usleep_range(5000, 10000);
2224 }
2225 if (i == IAVF_RESET_WAIT_DETECTED_COUNT) {
2226 dev_info(&adapter->pdev->dev, "Never saw reset\n");
2227 goto continue_reset; /* act like the reset happened */
2228 }
2229
2230 /* wait until the reset is complete and the PF is responding to us */
2231 for (i = 0; i < IAVF_RESET_WAIT_COMPLETE_COUNT; i++) {
2232 /* sleep first to make sure a minimum wait time is met */
2233 msleep(IAVF_RESET_WAIT_MS);
2234
2235 reg_val = rd32(hw, IAVF_VFGEN_RSTAT) &
2236 IAVF_VFGEN_RSTAT_VFR_STATE_MASK;
2237 if (reg_val == VIRTCHNL_VFR_VFACTIVE)
2238 break;
2239 }
2240
2241 pci_set_master(adapter->pdev);
2242 pci_restore_msi_state(adapter->pdev);
2243
2244 if (i == IAVF_RESET_WAIT_COMPLETE_COUNT) {
2245 dev_err(&adapter->pdev->dev, "Reset never finished (%x)\n",
2246 reg_val);
2247 iavf_disable_vf(adapter);
2248 mutex_unlock(&adapter->client_lock);
2249 mutex_unlock(&adapter->crit_lock);
2250 return; /* Do not attempt to reinit. It's dead, Jim. */
2251 }
2252
2253 continue_reset:
2254 /* We don't use netif_running() because it may be true prior to
2255 * ndo_open() returning, so we can't assume it means all our open
2256 * tasks have finished, since we're not holding the rtnl_lock here.
2257 */
2258 running = ((adapter->state == __IAVF_RUNNING) ||
2259 (adapter->state == __IAVF_RESETTING));
2260
2261 if (running) {
2262 netif_carrier_off(netdev);
2263 netif_tx_stop_all_queues(netdev);
2264 adapter->link_up = false;
2265 iavf_napi_disable_all(adapter);
2266 }
2267 iavf_irq_disable(adapter);
2268
2269 iavf_change_state(adapter, __IAVF_RESETTING);
2270 adapter->flags &= ~IAVF_FLAG_RESET_PENDING;
2271
2272 /* free the Tx/Rx rings and descriptors, might be better to just
2273 * re-use them sometime in the future
2274 */
2275 iavf_free_all_rx_resources(adapter);
2276 iavf_free_all_tx_resources(adapter);
2277
2278 adapter->flags |= IAVF_FLAG_QUEUES_DISABLED;
2279 /* kill and reinit the admin queue */
2280 iavf_shutdown_adminq(hw);
2281 adapter->current_op = VIRTCHNL_OP_UNKNOWN;
2282 err = iavf_init_adminq(hw);
2283 if (err)
2284 dev_info(&adapter->pdev->dev, "Failed to init adminq: %d\n",
2285 err);
2286 adapter->aq_required = 0;
2287
2288 if (adapter->flags & IAVF_FLAG_REINIT_ITR_NEEDED) {
2289 err = iavf_reinit_interrupt_scheme(adapter);
2290 if (err)
2291 goto reset_err;
2292 }
2293
2294 if (RSS_AQ(adapter)) {
2295 adapter->aq_required |= IAVF_FLAG_AQ_CONFIGURE_RSS;
2296 } else {
2297 err = iavf_init_rss(adapter);
2298 if (err)
2299 goto reset_err;
2300 }
2301
2302 adapter->aq_required |= IAVF_FLAG_AQ_GET_CONFIG;
2303 adapter->aq_required |= IAVF_FLAG_AQ_MAP_VECTORS;
2304
2305 spin_lock_bh(&adapter->mac_vlan_list_lock);
2306
2307 /* Delete filter for the current MAC address, it could have
2308 * been changed by the PF via administratively set MAC.
2309 * Will be re-added via VIRTCHNL_OP_GET_VF_RESOURCES.
2310 */
2311 list_for_each_entry_safe(f, ftmp, &adapter->mac_filter_list, list) {
2312 if (ether_addr_equal(f->macaddr, adapter->hw.mac.addr)) {
2313 list_del(&f->list);
2314 kfree(f);
2315 }
2316 }
2317 /* re-add all MAC filters */
2318 list_for_each_entry(f, &adapter->mac_filter_list, list) {
2319 f->add = true;
2320 }
2321 spin_unlock_bh(&adapter->mac_vlan_list_lock);
2322
2323 /* check if TCs are running and re-add all cloud filters */
2324 spin_lock_bh(&adapter->cloud_filter_list_lock);
2325 if ((vfres->vf_cap_flags & VIRTCHNL_VF_OFFLOAD_ADQ) &&
2326 adapter->num_tc) {
2327 list_for_each_entry(cf, &adapter->cloud_filter_list, list) {
2328 cf->add = true;
2329 }
2330 }
2331 spin_unlock_bh(&adapter->cloud_filter_list_lock);
2332
2333 adapter->aq_required |= IAVF_FLAG_AQ_ADD_MAC_FILTER;
2334 adapter->aq_required |= IAVF_FLAG_AQ_ADD_CLOUD_FILTER;
2335 iavf_misc_irq_enable(adapter);
2336
2337 mod_delayed_work(iavf_wq, &adapter->watchdog_task, 2);
2338
2339 /* We were running when the reset started, so we need to restore some
2340 * state here.
2341 */
2342 if (running) {
2343 /* allocate transmit descriptors */
2344 err = iavf_setup_all_tx_resources(adapter);
2345 if (err)
2346 goto reset_err;
2347
2348 /* allocate receive descriptors */
2349 err = iavf_setup_all_rx_resources(adapter);
2350 if (err)
2351 goto reset_err;
2352
2353 if (adapter->flags & IAVF_FLAG_REINIT_ITR_NEEDED) {
2354 err = iavf_request_traffic_irqs(adapter, netdev->name);
2355 if (err)
2356 goto reset_err;
2357
2358 adapter->flags &= ~IAVF_FLAG_REINIT_ITR_NEEDED;
2359 }
2360
2361 iavf_configure(adapter);
2362
2363 /* iavf_up_complete() will switch device back
2364 * to __IAVF_RUNNING
2365 */
2366 iavf_up_complete(adapter);
2367
2368 iavf_irq_enable(adapter, true);
2369 } else {
2370 iavf_change_state(adapter, __IAVF_DOWN);
2371 wake_up(&adapter->down_waitqueue);
2372 }
2373 mutex_unlock(&adapter->client_lock);
2374 mutex_unlock(&adapter->crit_lock);
2375
2376 return;
2377 reset_err:
2378 mutex_unlock(&adapter->client_lock);
2379 mutex_unlock(&adapter->crit_lock);
2380 if (running)
2381 iavf_change_state(adapter, __IAVF_RUNNING);
2382 dev_err(&adapter->pdev->dev, "failed to allocate resources during reinit\n");
2383 iavf_close(netdev);
2384 }
2385
2386 /**
2387 * iavf_adminq_task - worker thread to clean the admin queue
2388 * @work: pointer to work_struct containing our data
2389 **/
2390 static void iavf_adminq_task(struct work_struct *work)
2391 {
2392 struct iavf_adapter *adapter =
2393 container_of(work, struct iavf_adapter, adminq_task);
2394 struct iavf_hw *hw = &adapter->hw;
2395 struct iavf_arq_event_info event;
2396 enum virtchnl_ops v_op;
2397 enum iavf_status ret, v_ret;
2398 u32 val, oldval;
2399 u16 pending;
2400
2401 if (adapter->flags & IAVF_FLAG_PF_COMMS_FAILED)
2402 goto out;
2403
2404 event.buf_len = IAVF_MAX_AQ_BUF_SIZE;
2405 event.msg_buf = kzalloc(event.buf_len, GFP_KERNEL);
2406 if (!event.msg_buf)
2407 goto out;
2408
2409 if (iavf_lock_timeout(&adapter->crit_lock, 200))
2410 goto freedom;
2411 do {
2412 ret = iavf_clean_arq_element(hw, &event, &pending);
2413 v_op = (enum virtchnl_ops)le32_to_cpu(event.desc.cookie_high);
2414 v_ret = (enum iavf_status)le32_to_cpu(event.desc.cookie_low);
2415
2416 if (ret || !v_op)
2417 break; /* No event to process or error cleaning ARQ */
2418
2419 iavf_virtchnl_completion(adapter, v_op, v_ret, event.msg_buf,
2420 event.msg_len);
2421 if (pending != 0)
2422 memset(event.msg_buf, 0, IAVF_MAX_AQ_BUF_SIZE);
2423 } while (pending);
2424 mutex_unlock(&adapter->crit_lock);
2425
2426 if ((adapter->flags &
2427 (IAVF_FLAG_RESET_PENDING | IAVF_FLAG_RESET_NEEDED)) ||
2428 adapter->state == __IAVF_RESETTING)
2429 goto freedom;
2430
2431 /* check for error indications */
2432 val = rd32(hw, hw->aq.arq.len);
2433 if (val == 0xdeadbeef || val == 0xffffffff) /* device in reset */
2434 goto freedom;
2435 oldval = val;
2436 if (val & IAVF_VF_ARQLEN1_ARQVFE_MASK) {
2437 dev_info(&adapter->pdev->dev, "ARQ VF Error detected\n");
2438 val &= ~IAVF_VF_ARQLEN1_ARQVFE_MASK;
2439 }
2440 if (val & IAVF_VF_ARQLEN1_ARQOVFL_MASK) {
2441 dev_info(&adapter->pdev->dev, "ARQ Overflow Error detected\n");
2442 val &= ~IAVF_VF_ARQLEN1_ARQOVFL_MASK;
2443 }
2444 if (val & IAVF_VF_ARQLEN1_ARQCRIT_MASK) {
2445 dev_info(&adapter->pdev->dev, "ARQ Critical Error detected\n");
2446 val &= ~IAVF_VF_ARQLEN1_ARQCRIT_MASK;
2447 }
2448 if (oldval != val)
2449 wr32(hw, hw->aq.arq.len, val);
2450
2451 val = rd32(hw, hw->aq.asq.len);
2452 oldval = val;
2453 if (val & IAVF_VF_ATQLEN1_ATQVFE_MASK) {
2454 dev_info(&adapter->pdev->dev, "ASQ VF Error detected\n");
2455 val &= ~IAVF_VF_ATQLEN1_ATQVFE_MASK;
2456 }
2457 if (val & IAVF_VF_ATQLEN1_ATQOVFL_MASK) {
2458 dev_info(&adapter->pdev->dev, "ASQ Overflow Error detected\n");
2459 val &= ~IAVF_VF_ATQLEN1_ATQOVFL_MASK;
2460 }
2461 if (val & IAVF_VF_ATQLEN1_ATQCRIT_MASK) {
2462 dev_info(&adapter->pdev->dev, "ASQ Critical Error detected\n");
2463 val &= ~IAVF_VF_ATQLEN1_ATQCRIT_MASK;
2464 }
2465 if (oldval != val)
2466 wr32(hw, hw->aq.asq.len, val);
2467
2468 freedom:
2469 kfree(event.msg_buf);
2470 out:
2471 /* re-enable Admin queue interrupt cause */
2472 iavf_misc_irq_enable(adapter);
2473 }
2474
2475 /**
2476 * iavf_client_task - worker thread to perform client work
2477 * @work: pointer to work_struct containing our data
2478 *
2479 * This task handles client interactions. Because client calls can be
2480 * reentrant, we can't handle them in the watchdog.
2481 **/
2482 static void iavf_client_task(struct work_struct *work)
2483 {
2484 struct iavf_adapter *adapter =
2485 container_of(work, struct iavf_adapter, client_task.work);
2486
2487 /* If we can't get the client bit, just give up. We'll be rescheduled
2488 * later.
2489 */
2490
2491 if (!mutex_trylock(&adapter->client_lock))
2492 return;
2493
2494 if (adapter->flags & IAVF_FLAG_SERVICE_CLIENT_REQUESTED) {
2495 iavf_client_subtask(adapter);
2496 adapter->flags &= ~IAVF_FLAG_SERVICE_CLIENT_REQUESTED;
2497 goto out;
2498 }
2499 if (adapter->flags & IAVF_FLAG_CLIENT_NEEDS_L2_PARAMS) {
2500 iavf_notify_client_l2_params(&adapter->vsi);
2501 adapter->flags &= ~IAVF_FLAG_CLIENT_NEEDS_L2_PARAMS;
2502 goto out;
2503 }
2504 if (adapter->flags & IAVF_FLAG_CLIENT_NEEDS_CLOSE) {
2505 iavf_notify_client_close(&adapter->vsi, false);
2506 adapter->flags &= ~IAVF_FLAG_CLIENT_NEEDS_CLOSE;
2507 goto out;
2508 }
2509 if (adapter->flags & IAVF_FLAG_CLIENT_NEEDS_OPEN) {
2510 iavf_notify_client_open(&adapter->vsi);
2511 adapter->flags &= ~IAVF_FLAG_CLIENT_NEEDS_OPEN;
2512 }
2513 out:
2514 mutex_unlock(&adapter->client_lock);
2515 }
2516
2517 /**
2518 * iavf_free_all_tx_resources - Free Tx Resources for All Queues
2519 * @adapter: board private structure
2520 *
2521 * Free all transmit software resources
2522 **/
2523 void iavf_free_all_tx_resources(struct iavf_adapter *adapter)
2524 {
2525 int i;
2526
2527 if (!adapter->tx_rings)
2528 return;
2529
2530 for (i = 0; i < adapter->num_active_queues; i++)
2531 if (adapter->tx_rings[i].desc)
2532 iavf_free_tx_resources(&adapter->tx_rings[i]);
2533 }
2534
2535 /**
2536 * iavf_setup_all_tx_resources - allocate all queues Tx resources
2537 * @adapter: board private structure
2538 *
2539 * If this function returns with an error, then it's possible one or
2540 * more of the rings is populated (while the rest are not). It is the
2541 * callers duty to clean those orphaned rings.
2542 *
2543 * Return 0 on success, negative on failure
2544 **/
2545 static int iavf_setup_all_tx_resources(struct iavf_adapter *adapter)
2546 {
2547 int i, err = 0;
2548
2549 for (i = 0; i < adapter->num_active_queues; i++) {
2550 adapter->tx_rings[i].count = adapter->tx_desc_count;
2551 err = iavf_setup_tx_descriptors(&adapter->tx_rings[i]);
2552 if (!err)
2553 continue;
2554 dev_err(&adapter->pdev->dev,
2555 "Allocation for Tx Queue %u failed\n", i);
2556 break;
2557 }
2558
2559 return err;
2560 }
2561
2562 /**
2563 * iavf_setup_all_rx_resources - allocate all queues Rx resources
2564 * @adapter: board private structure
2565 *
2566 * If this function returns with an error, then it's possible one or
2567 * more of the rings is populated (while the rest are not). It is the
2568 * callers duty to clean those orphaned rings.
2569 *
2570 * Return 0 on success, negative on failure
2571 **/
2572 static int iavf_setup_all_rx_resources(struct iavf_adapter *adapter)
2573 {
2574 int i, err = 0;
2575
2576 for (i = 0; i < adapter->num_active_queues; i++) {
2577 adapter->rx_rings[i].count = adapter->rx_desc_count;
2578 err = iavf_setup_rx_descriptors(&adapter->rx_rings[i]);
2579 if (!err)
2580 continue;
2581 dev_err(&adapter->pdev->dev,
2582 "Allocation for Rx Queue %u failed\n", i);
2583 break;
2584 }
2585 return err;
2586 }
2587
2588 /**
2589 * iavf_free_all_rx_resources - Free Rx Resources for All Queues
2590 * @adapter: board private structure
2591 *
2592 * Free all receive software resources
2593 **/
2594 void iavf_free_all_rx_resources(struct iavf_adapter *adapter)
2595 {
2596 int i;
2597
2598 if (!adapter->rx_rings)
2599 return;
2600
2601 for (i = 0; i < adapter->num_active_queues; i++)
2602 if (adapter->rx_rings[i].desc)
2603 iavf_free_rx_resources(&adapter->rx_rings[i]);
2604 }
2605
2606 /**
2607 * iavf_validate_tx_bandwidth - validate the max Tx bandwidth
2608 * @adapter: board private structure
2609 * @max_tx_rate: max Tx bw for a tc
2610 **/
2611 static int iavf_validate_tx_bandwidth(struct iavf_adapter *adapter,
2612 u64 max_tx_rate)
2613 {
2614 int speed = 0, ret = 0;
2615
2616 if (ADV_LINK_SUPPORT(adapter)) {
2617 if (adapter->link_speed_mbps < U32_MAX) {
2618 speed = adapter->link_speed_mbps;
2619 goto validate_bw;
2620 } else {
2621 dev_err(&adapter->pdev->dev, "Unknown link speed\n");
2622 return -EINVAL;
2623 }
2624 }
2625
2626 switch (adapter->link_speed) {
2627 case VIRTCHNL_LINK_SPEED_40GB:
2628 speed = SPEED_40000;
2629 break;
2630 case VIRTCHNL_LINK_SPEED_25GB:
2631 speed = SPEED_25000;
2632 break;
2633 case VIRTCHNL_LINK_SPEED_20GB:
2634 speed = SPEED_20000;
2635 break;
2636 case VIRTCHNL_LINK_SPEED_10GB:
2637 speed = SPEED_10000;
2638 break;
2639 case VIRTCHNL_LINK_SPEED_5GB:
2640 speed = SPEED_5000;
2641 break;
2642 case VIRTCHNL_LINK_SPEED_2_5GB:
2643 speed = SPEED_2500;
2644 break;
2645 case VIRTCHNL_LINK_SPEED_1GB:
2646 speed = SPEED_1000;
2647 break;
2648 case VIRTCHNL_LINK_SPEED_100MB:
2649 speed = SPEED_100;
2650 break;
2651 default:
2652 break;
2653 }
2654
2655 validate_bw:
2656 if (max_tx_rate > speed) {
2657 dev_err(&adapter->pdev->dev,
2658 "Invalid tx rate specified\n");
2659 ret = -EINVAL;
2660 }
2661
2662 return ret;
2663 }
2664
2665 /**
2666 * iavf_validate_ch_config - validate queue mapping info
2667 * @adapter: board private structure
2668 * @mqprio_qopt: queue parameters
2669 *
2670 * This function validates if the config provided by the user to
2671 * configure queue channels is valid or not. Returns 0 on a valid
2672 * config.
2673 **/
2674 static int iavf_validate_ch_config(struct iavf_adapter *adapter,
2675 struct tc_mqprio_qopt_offload *mqprio_qopt)
2676 {
2677 u64 total_max_rate = 0;
2678 int i, num_qps = 0;
2679 u64 tx_rate = 0;
2680 int ret = 0;
2681
2682 if (mqprio_qopt->qopt.num_tc > IAVF_MAX_TRAFFIC_CLASS ||
2683 mqprio_qopt->qopt.num_tc < 1)
2684 return -EINVAL;
2685
2686 for (i = 0; i <= mqprio_qopt->qopt.num_tc - 1; i++) {
2687 if (!mqprio_qopt->qopt.count[i] ||
2688 mqprio_qopt->qopt.offset[i] != num_qps)
2689 return -EINVAL;
2690 if (mqprio_qopt->min_rate[i]) {
2691 dev_err(&adapter->pdev->dev,
2692 "Invalid min tx rate (greater than 0) specified\n");
2693 return -EINVAL;
2694 }
2695 /*convert to Mbps */
2696 tx_rate = div_u64(mqprio_qopt->max_rate[i],
2697 IAVF_MBPS_DIVISOR);
2698 total_max_rate += tx_rate;
2699 num_qps += mqprio_qopt->qopt.count[i];
2700 }
2701 if (num_qps > adapter->num_active_queues) {
2702 dev_err(&adapter->pdev->dev,
2703 "Cannot support requested number of queues\n");
2704 return -EINVAL;
2705 }
2706
2707 ret = iavf_validate_tx_bandwidth(adapter, total_max_rate);
2708 return ret;
2709 }
2710
2711 /**
2712 * iavf_del_all_cloud_filters - delete all cloud filters on the traffic classes
2713 * @adapter: board private structure
2714 **/
2715 static void iavf_del_all_cloud_filters(struct iavf_adapter *adapter)
2716 {
2717 struct iavf_cloud_filter *cf, *cftmp;
2718
2719 spin_lock_bh(&adapter->cloud_filter_list_lock);
2720 list_for_each_entry_safe(cf, cftmp, &adapter->cloud_filter_list,
2721 list) {
2722 list_del(&cf->list);
2723 kfree(cf);
2724 adapter->num_cloud_filters--;
2725 }
2726 spin_unlock_bh(&adapter->cloud_filter_list_lock);
2727 }
2728
2729 /**
2730 * __iavf_setup_tc - configure multiple traffic classes
2731 * @netdev: network interface device structure
2732 * @type_data: tc offload data
2733 *
2734 * This function processes the config information provided by the
2735 * user to configure traffic classes/queue channels and packages the
2736 * information to request the PF to setup traffic classes.
2737 *
2738 * Returns 0 on success.
2739 **/
2740 static int __iavf_setup_tc(struct net_device *netdev, void *type_data)
2741 {
2742 struct tc_mqprio_qopt_offload *mqprio_qopt = type_data;
2743 struct iavf_adapter *adapter = netdev_priv(netdev);
2744 struct virtchnl_vf_resource *vfres = adapter->vf_res;
2745 u8 num_tc = 0, total_qps = 0;
2746 int ret = 0, netdev_tc = 0;
2747 u64 max_tx_rate;
2748 u16 mode;
2749 int i;
2750
2751 num_tc = mqprio_qopt->qopt.num_tc;
2752 mode = mqprio_qopt->mode;
2753
2754 /* delete queue_channel */
2755 if (!mqprio_qopt->qopt.hw) {
2756 if (adapter->ch_config.state == __IAVF_TC_RUNNING) {
2757 /* reset the tc configuration */
2758 netdev_reset_tc(netdev);
2759 adapter->num_tc = 0;
2760 netif_tx_stop_all_queues(netdev);
2761 netif_tx_disable(netdev);
2762 iavf_del_all_cloud_filters(adapter);
2763 adapter->aq_required = IAVF_FLAG_AQ_DISABLE_CHANNELS;
2764 goto exit;
2765 } else {
2766 return -EINVAL;
2767 }
2768 }
2769
2770 /* add queue channel */
2771 if (mode == TC_MQPRIO_MODE_CHANNEL) {
2772 if (!(vfres->vf_cap_flags & VIRTCHNL_VF_OFFLOAD_ADQ)) {
2773 dev_err(&adapter->pdev->dev, "ADq not supported\n");
2774 return -EOPNOTSUPP;
2775 }
2776 if (adapter->ch_config.state != __IAVF_TC_INVALID) {
2777 dev_err(&adapter->pdev->dev, "TC configuration already exists\n");
2778 return -EINVAL;
2779 }
2780
2781 ret = iavf_validate_ch_config(adapter, mqprio_qopt);
2782 if (ret)
2783 return ret;
2784 /* Return if same TC config is requested */
2785 if (adapter->num_tc == num_tc)
2786 return 0;
2787 adapter->num_tc = num_tc;
2788
2789 for (i = 0; i < IAVF_MAX_TRAFFIC_CLASS; i++) {
2790 if (i < num_tc) {
2791 adapter->ch_config.ch_info[i].count =
2792 mqprio_qopt->qopt.count[i];
2793 adapter->ch_config.ch_info[i].offset =
2794 mqprio_qopt->qopt.offset[i];
2795 total_qps += mqprio_qopt->qopt.count[i];
2796 max_tx_rate = mqprio_qopt->max_rate[i];
2797 /* convert to Mbps */
2798 max_tx_rate = div_u64(max_tx_rate,
2799 IAVF_MBPS_DIVISOR);
2800 adapter->ch_config.ch_info[i].max_tx_rate =
2801 max_tx_rate;
2802 } else {
2803 adapter->ch_config.ch_info[i].count = 1;
2804 adapter->ch_config.ch_info[i].offset = 0;
2805 }
2806 }
2807 adapter->ch_config.total_qps = total_qps;
2808 netif_tx_stop_all_queues(netdev);
2809 netif_tx_disable(netdev);
2810 adapter->aq_required |= IAVF_FLAG_AQ_ENABLE_CHANNELS;
2811 netdev_reset_tc(netdev);
2812 /* Report the tc mapping up the stack */
2813 netdev_set_num_tc(adapter->netdev, num_tc);
2814 for (i = 0; i < IAVF_MAX_TRAFFIC_CLASS; i++) {
2815 u16 qcount = mqprio_qopt->qopt.count[i];
2816 u16 qoffset = mqprio_qopt->qopt.offset[i];
2817
2818 if (i < num_tc)
2819 netdev_set_tc_queue(netdev, netdev_tc++, qcount,
2820 qoffset);
2821 }
2822 }
2823 exit:
2824 return ret;
2825 }
2826
2827 /**
2828 * iavf_parse_cls_flower - Parse tc flower filters provided by kernel
2829 * @adapter: board private structure
2830 * @f: pointer to struct flow_cls_offload
2831 * @filter: pointer to cloud filter structure
2832 */
2833 static int iavf_parse_cls_flower(struct iavf_adapter *adapter,
2834 struct flow_cls_offload *f,
2835 struct iavf_cloud_filter *filter)
2836 {
2837 struct flow_rule *rule = flow_cls_offload_flow_rule(f);
2838 struct flow_dissector *dissector = rule->match.dissector;
2839 u16 n_proto_mask = 0;
2840 u16 n_proto_key = 0;
2841 u8 field_flags = 0;
2842 u16 addr_type = 0;
2843 u16 n_proto = 0;
2844 int i = 0;
2845 struct virtchnl_filter *vf = &filter->f;
2846
2847 if (dissector->used_keys &
2848 ~(BIT(FLOW_DISSECTOR_KEY_CONTROL) |
2849 BIT(FLOW_DISSECTOR_KEY_BASIC) |
2850 BIT(FLOW_DISSECTOR_KEY_ETH_ADDRS) |
2851 BIT(FLOW_DISSECTOR_KEY_VLAN) |
2852 BIT(FLOW_DISSECTOR_KEY_IPV4_ADDRS) |
2853 BIT(FLOW_DISSECTOR_KEY_IPV6_ADDRS) |
2854 BIT(FLOW_DISSECTOR_KEY_PORTS) |
2855 BIT(FLOW_DISSECTOR_KEY_ENC_KEYID))) {
2856 dev_err(&adapter->pdev->dev, "Unsupported key used: 0x%x\n",
2857 dissector->used_keys);
2858 return -EOPNOTSUPP;
2859 }
2860
2861 if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_ENC_KEYID)) {
2862 struct flow_match_enc_keyid match;
2863
2864 flow_rule_match_enc_keyid(rule, &match);
2865 if (match.mask->keyid != 0)
2866 field_flags |= IAVF_CLOUD_FIELD_TEN_ID;
2867 }
2868
2869 if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_BASIC)) {
2870 struct flow_match_basic match;
2871
2872 flow_rule_match_basic(rule, &match);
2873 n_proto_key = ntohs(match.key->n_proto);
2874 n_proto_mask = ntohs(match.mask->n_proto);
2875
2876 if (n_proto_key == ETH_P_ALL) {
2877 n_proto_key = 0;
2878 n_proto_mask = 0;
2879 }
2880 n_proto = n_proto_key & n_proto_mask;
2881 if (n_proto != ETH_P_IP && n_proto != ETH_P_IPV6)
2882 return -EINVAL;
2883 if (n_proto == ETH_P_IPV6) {
2884 /* specify flow type as TCP IPv6 */
2885 vf->flow_type = VIRTCHNL_TCP_V6_FLOW;
2886 }
2887
2888 if (match.key->ip_proto != IPPROTO_TCP) {
2889 dev_info(&adapter->pdev->dev, "Only TCP transport is supported\n");
2890 return -EINVAL;
2891 }
2892 }
2893
2894 if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_ETH_ADDRS)) {
2895 struct flow_match_eth_addrs match;
2896
2897 flow_rule_match_eth_addrs(rule, &match);
2898
2899 /* use is_broadcast and is_zero to check for all 0xf or 0 */
2900 if (!is_zero_ether_addr(match.mask->dst)) {
2901 if (is_broadcast_ether_addr(match.mask->dst)) {
2902 field_flags |= IAVF_CLOUD_FIELD_OMAC;
2903 } else {
2904 dev_err(&adapter->pdev->dev, "Bad ether dest mask %pM\n",
2905 match.mask->dst);
2906 return IAVF_ERR_CONFIG;
2907 }
2908 }
2909
2910 if (!is_zero_ether_addr(match.mask->src)) {
2911 if (is_broadcast_ether_addr(match.mask->src)) {
2912 field_flags |= IAVF_CLOUD_FIELD_IMAC;
2913 } else {
2914 dev_err(&adapter->pdev->dev, "Bad ether src mask %pM\n",
2915 match.mask->src);
2916 return IAVF_ERR_CONFIG;
2917 }
2918 }
2919
2920 if (!is_zero_ether_addr(match.key->dst))
2921 if (is_valid_ether_addr(match.key->dst) ||
2922 is_multicast_ether_addr(match.key->dst)) {
2923 /* set the mask if a valid dst_mac address */
2924 for (i = 0; i < ETH_ALEN; i++)
2925 vf->mask.tcp_spec.dst_mac[i] |= 0xff;
2926 ether_addr_copy(vf->data.tcp_spec.dst_mac,
2927 match.key->dst);
2928 }
2929
2930 if (!is_zero_ether_addr(match.key->src))
2931 if (is_valid_ether_addr(match.key->src) ||
2932 is_multicast_ether_addr(match.key->src)) {
2933 /* set the mask if a valid dst_mac address */
2934 for (i = 0; i < ETH_ALEN; i++)
2935 vf->mask.tcp_spec.src_mac[i] |= 0xff;
2936 ether_addr_copy(vf->data.tcp_spec.src_mac,
2937 match.key->src);
2938 }
2939 }
2940
2941 if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_VLAN)) {
2942 struct flow_match_vlan match;
2943
2944 flow_rule_match_vlan(rule, &match);
2945 if (match.mask->vlan_id) {
2946 if (match.mask->vlan_id == VLAN_VID_MASK) {
2947 field_flags |= IAVF_CLOUD_FIELD_IVLAN;
2948 } else {
2949 dev_err(&adapter->pdev->dev, "Bad vlan mask %u\n",
2950 match.mask->vlan_id);
2951 return IAVF_ERR_CONFIG;
2952 }
2953 }
2954 vf->mask.tcp_spec.vlan_id |= cpu_to_be16(0xffff);
2955 vf->data.tcp_spec.vlan_id = cpu_to_be16(match.key->vlan_id);
2956 }
2957
2958 if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_CONTROL)) {
2959 struct flow_match_control match;
2960
2961 flow_rule_match_control(rule, &match);
2962 addr_type = match.key->addr_type;
2963 }
2964
2965 if (addr_type == FLOW_DISSECTOR_KEY_IPV4_ADDRS) {
2966 struct flow_match_ipv4_addrs match;
2967
2968 flow_rule_match_ipv4_addrs(rule, &match);
2969 if (match.mask->dst) {
2970 if (match.mask->dst == cpu_to_be32(0xffffffff)) {
2971 field_flags |= IAVF_CLOUD_FIELD_IIP;
2972 } else {
2973 dev_err(&adapter->pdev->dev, "Bad ip dst mask 0x%08x\n",
2974 be32_to_cpu(match.mask->dst));
2975 return IAVF_ERR_CONFIG;
2976 }
2977 }
2978
2979 if (match.mask->src) {
2980 if (match.mask->src == cpu_to_be32(0xffffffff)) {
2981 field_flags |= IAVF_CLOUD_FIELD_IIP;
2982 } else {
2983 dev_err(&adapter->pdev->dev, "Bad ip src mask 0x%08x\n",
2984 be32_to_cpu(match.mask->dst));
2985 return IAVF_ERR_CONFIG;
2986 }
2987 }
2988
2989 if (field_flags & IAVF_CLOUD_FIELD_TEN_ID) {
2990 dev_info(&adapter->pdev->dev, "Tenant id not allowed for ip filter\n");
2991 return IAVF_ERR_CONFIG;
2992 }
2993 if (match.key->dst) {
2994 vf->mask.tcp_spec.dst_ip[0] |= cpu_to_be32(0xffffffff);
2995 vf->data.tcp_spec.dst_ip[0] = match.key->dst;
2996 }
2997 if (match.key->src) {
2998 vf->mask.tcp_spec.src_ip[0] |= cpu_to_be32(0xffffffff);
2999 vf->data.tcp_spec.src_ip[0] = match.key->src;
3000 }
3001 }
3002
3003 if (addr_type == FLOW_DISSECTOR_KEY_IPV6_ADDRS) {
3004 struct flow_match_ipv6_addrs match;
3005
3006 flow_rule_match_ipv6_addrs(rule, &match);
3007
3008 /* validate mask, make sure it is not IPV6_ADDR_ANY */
3009 if (ipv6_addr_any(&match.mask->dst)) {
3010 dev_err(&adapter->pdev->dev, "Bad ipv6 dst mask 0x%02x\n",
3011 IPV6_ADDR_ANY);
3012 return IAVF_ERR_CONFIG;
3013 }
3014
3015 /* src and dest IPv6 address should not be LOOPBACK
3016 * (0:0:0:0:0:0:0:1) which can be represented as ::1
3017 */
3018 if (ipv6_addr_loopback(&match.key->dst) ||
3019 ipv6_addr_loopback(&match.key->src)) {
3020 dev_err(&adapter->pdev->dev,
3021 "ipv6 addr should not be loopback\n");
3022 return IAVF_ERR_CONFIG;
3023 }
3024 if (!ipv6_addr_any(&match.mask->dst) ||
3025 !ipv6_addr_any(&match.mask->src))
3026 field_flags |= IAVF_CLOUD_FIELD_IIP;
3027
3028 for (i = 0; i < 4; i++)
3029 vf->mask.tcp_spec.dst_ip[i] |= cpu_to_be32(0xffffffff);
3030 memcpy(&vf->data.tcp_spec.dst_ip, &match.key->dst.s6_addr32,
3031 sizeof(vf->data.tcp_spec.dst_ip));
3032 for (i = 0; i < 4; i++)
3033 vf->mask.tcp_spec.src_ip[i] |= cpu_to_be32(0xffffffff);
3034 memcpy(&vf->data.tcp_spec.src_ip, &match.key->src.s6_addr32,
3035 sizeof(vf->data.tcp_spec.src_ip));
3036 }
3037 if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_PORTS)) {
3038 struct flow_match_ports match;
3039
3040 flow_rule_match_ports(rule, &match);
3041 if (match.mask->src) {
3042 if (match.mask->src == cpu_to_be16(0xffff)) {
3043 field_flags |= IAVF_CLOUD_FIELD_IIP;
3044 } else {
3045 dev_err(&adapter->pdev->dev, "Bad src port mask %u\n",
3046 be16_to_cpu(match.mask->src));
3047 return IAVF_ERR_CONFIG;
3048 }
3049 }
3050
3051 if (match.mask->dst) {
3052 if (match.mask->dst == cpu_to_be16(0xffff)) {
3053 field_flags |= IAVF_CLOUD_FIELD_IIP;
3054 } else {
3055 dev_err(&adapter->pdev->dev, "Bad dst port mask %u\n",
3056 be16_to_cpu(match.mask->dst));
3057 return IAVF_ERR_CONFIG;
3058 }
3059 }
3060 if (match.key->dst) {
3061 vf->mask.tcp_spec.dst_port |= cpu_to_be16(0xffff);
3062 vf->data.tcp_spec.dst_port = match.key->dst;
3063 }
3064
3065 if (match.key->src) {
3066 vf->mask.tcp_spec.src_port |= cpu_to_be16(0xffff);
3067 vf->data.tcp_spec.src_port = match.key->src;
3068 }
3069 }
3070 vf->field_flags = field_flags;
3071
3072 return 0;
3073 }
3074
3075 /**
3076 * iavf_handle_tclass - Forward to a traffic class on the device
3077 * @adapter: board private structure
3078 * @tc: traffic class index on the device
3079 * @filter: pointer to cloud filter structure
3080 */
3081 static int iavf_handle_tclass(struct iavf_adapter *adapter, u32 tc,
3082 struct iavf_cloud_filter *filter)
3083 {
3084 if (tc == 0)
3085 return 0;
3086 if (tc < adapter->num_tc) {
3087 if (!filter->f.data.tcp_spec.dst_port) {
3088 dev_err(&adapter->pdev->dev,
3089 "Specify destination port to redirect to traffic class other than TC0\n");
3090 return -EINVAL;
3091 }
3092 }
3093 /* redirect to a traffic class on the same device */
3094 filter->f.action = VIRTCHNL_ACTION_TC_REDIRECT;
3095 filter->f.action_meta = tc;
3096 return 0;
3097 }
3098
3099 /**
3100 * iavf_configure_clsflower - Add tc flower filters
3101 * @adapter: board private structure
3102 * @cls_flower: Pointer to struct flow_cls_offload
3103 */
3104 static int iavf_configure_clsflower(struct iavf_adapter *adapter,
3105 struct flow_cls_offload *cls_flower)
3106 {
3107 int tc = tc_classid_to_hwtc(adapter->netdev, cls_flower->classid);
3108 struct iavf_cloud_filter *filter = NULL;
3109 int err = -EINVAL, count = 50;
3110
3111 if (tc < 0) {
3112 dev_err(&adapter->pdev->dev, "Invalid traffic class\n");
3113 return -EINVAL;
3114 }
3115
3116 filter = kzalloc(sizeof(*filter), GFP_KERNEL);
3117 if (!filter)
3118 return -ENOMEM;
3119
3120 while (!mutex_trylock(&adapter->crit_lock)) {
3121 if (--count == 0) {
3122 kfree(filter);
3123 return err;
3124 }
3125 udelay(1);
3126 }
3127
3128 filter->cookie = cls_flower->cookie;
3129
3130 /* set the mask to all zeroes to begin with */
3131 memset(&filter->f.mask.tcp_spec, 0, sizeof(struct virtchnl_l4_spec));
3132 /* start out with flow type and eth type IPv4 to begin with */
3133 filter->f.flow_type = VIRTCHNL_TCP_V4_FLOW;
3134 err = iavf_parse_cls_flower(adapter, cls_flower, filter);
3135 if (err)
3136 goto err;
3137
3138 err = iavf_handle_tclass(adapter, tc, filter);
3139 if (err)
3140 goto err;
3141
3142 /* add filter to the list */
3143 spin_lock_bh(&adapter->cloud_filter_list_lock);
3144 list_add_tail(&filter->list, &adapter->cloud_filter_list);
3145 adapter->num_cloud_filters++;
3146 filter->add = true;
3147 adapter->aq_required |= IAVF_FLAG_AQ_ADD_CLOUD_FILTER;
3148 spin_unlock_bh(&adapter->cloud_filter_list_lock);
3149 err:
3150 if (err)
3151 kfree(filter);
3152
3153 mutex_unlock(&adapter->crit_lock);
3154 return err;
3155 }
3156
3157 /* iavf_find_cf - Find the cloud filter in the list
3158 * @adapter: Board private structure
3159 * @cookie: filter specific cookie
3160 *
3161 * Returns ptr to the filter object or NULL. Must be called while holding the
3162 * cloud_filter_list_lock.
3163 */
3164 static struct iavf_cloud_filter *iavf_find_cf(struct iavf_adapter *adapter,
3165 unsigned long *cookie)
3166 {
3167 struct iavf_cloud_filter *filter = NULL;
3168
3169 if (!cookie)
3170 return NULL;
3171
3172 list_for_each_entry(filter, &adapter->cloud_filter_list, list) {
3173 if (!memcmp(cookie, &filter->cookie, sizeof(filter->cookie)))
3174 return filter;
3175 }
3176 return NULL;
3177 }
3178
3179 /**
3180 * iavf_delete_clsflower - Remove tc flower filters
3181 * @adapter: board private structure
3182 * @cls_flower: Pointer to struct flow_cls_offload
3183 */
3184 static int iavf_delete_clsflower(struct iavf_adapter *adapter,
3185 struct flow_cls_offload *cls_flower)
3186 {
3187 struct iavf_cloud_filter *filter = NULL;
3188 int err = 0;
3189
3190 spin_lock_bh(&adapter->cloud_filter_list_lock);
3191 filter = iavf_find_cf(adapter, &cls_flower->cookie);
3192 if (filter) {
3193 filter->del = true;
3194 adapter->aq_required |= IAVF_FLAG_AQ_DEL_CLOUD_FILTER;
3195 } else {
3196 err = -EINVAL;
3197 }
3198 spin_unlock_bh(&adapter->cloud_filter_list_lock);
3199
3200 return err;
3201 }
3202
3203 /**
3204 * iavf_setup_tc_cls_flower - flower classifier offloads
3205 * @adapter: board private structure
3206 * @cls_flower: pointer to flow_cls_offload struct with flow info
3207 */
3208 static int iavf_setup_tc_cls_flower(struct iavf_adapter *adapter,
3209 struct flow_cls_offload *cls_flower)
3210 {
3211 switch (cls_flower->command) {
3212 case FLOW_CLS_REPLACE:
3213 return iavf_configure_clsflower(adapter, cls_flower);
3214 case FLOW_CLS_DESTROY:
3215 return iavf_delete_clsflower(adapter, cls_flower);
3216 case FLOW_CLS_STATS:
3217 return -EOPNOTSUPP;
3218 default:
3219 return -EOPNOTSUPP;
3220 }
3221 }
3222
3223 /**
3224 * iavf_setup_tc_block_cb - block callback for tc
3225 * @type: type of offload
3226 * @type_data: offload data
3227 * @cb_priv:
3228 *
3229 * This function is the block callback for traffic classes
3230 **/
3231 static int iavf_setup_tc_block_cb(enum tc_setup_type type, void *type_data,
3232 void *cb_priv)
3233 {
3234 struct iavf_adapter *adapter = cb_priv;
3235
3236 if (!tc_cls_can_offload_and_chain0(adapter->netdev, type_data))
3237 return -EOPNOTSUPP;
3238
3239 switch (type) {
3240 case TC_SETUP_CLSFLOWER:
3241 return iavf_setup_tc_cls_flower(cb_priv, type_data);
3242 default:
3243 return -EOPNOTSUPP;
3244 }
3245 }
3246
3247 static LIST_HEAD(iavf_block_cb_list);
3248
3249 /**
3250 * iavf_setup_tc - configure multiple traffic classes
3251 * @netdev: network interface device structure
3252 * @type: type of offload
3253 * @type_data: tc offload data
3254 *
3255 * This function is the callback to ndo_setup_tc in the
3256 * netdev_ops.
3257 *
3258 * Returns 0 on success
3259 **/
3260 static int iavf_setup_tc(struct net_device *netdev, enum tc_setup_type type,
3261 void *type_data)
3262 {
3263 struct iavf_adapter *adapter = netdev_priv(netdev);
3264
3265 switch (type) {
3266 case TC_SETUP_QDISC_MQPRIO:
3267 return __iavf_setup_tc(netdev, type_data);
3268 case TC_SETUP_BLOCK:
3269 return flow_block_cb_setup_simple(type_data,
3270 &iavf_block_cb_list,
3271 iavf_setup_tc_block_cb,
3272 adapter, adapter, true);
3273 default:
3274 return -EOPNOTSUPP;
3275 }
3276 }
3277
3278 /**
3279 * iavf_open - Called when a network interface is made active
3280 * @netdev: network interface device structure
3281 *
3282 * Returns 0 on success, negative value on failure
3283 *
3284 * The open entry point is called when a network interface is made
3285 * active by the system (IFF_UP). At this point all resources needed
3286 * for transmit and receive operations are allocated, the interrupt
3287 * handler is registered with the OS, the watchdog is started,
3288 * and the stack is notified that the interface is ready.
3289 **/
3290 static int iavf_open(struct net_device *netdev)
3291 {
3292 struct iavf_adapter *adapter = netdev_priv(netdev);
3293 int err;
3294
3295 if (adapter->flags & IAVF_FLAG_PF_COMMS_FAILED) {
3296 dev_err(&adapter->pdev->dev, "Unable to open device due to PF driver failure.\n");
3297 return -EIO;
3298 }
3299
3300 while (!mutex_trylock(&adapter->crit_lock))
3301 usleep_range(500, 1000);
3302
3303 if (adapter->state != __IAVF_DOWN) {
3304 err = -EBUSY;
3305 goto err_unlock;
3306 }
3307
3308 /* allocate transmit descriptors */
3309 err = iavf_setup_all_tx_resources(adapter);
3310 if (err)
3311 goto err_setup_tx;
3312
3313 /* allocate receive descriptors */
3314 err = iavf_setup_all_rx_resources(adapter);
3315 if (err)
3316 goto err_setup_rx;
3317
3318 /* clear any pending interrupts, may auto mask */
3319 err = iavf_request_traffic_irqs(adapter, netdev->name);
3320 if (err)
3321 goto err_req_irq;
3322
3323 spin_lock_bh(&adapter->mac_vlan_list_lock);
3324
3325 iavf_add_filter(adapter, adapter->hw.mac.addr);
3326
3327 spin_unlock_bh(&adapter->mac_vlan_list_lock);
3328
3329 /* Restore VLAN filters that were removed with IFF_DOWN */
3330 iavf_restore_filters(adapter);
3331
3332 iavf_configure(adapter);
3333
3334 iavf_up_complete(adapter);
3335
3336 iavf_irq_enable(adapter, true);
3337
3338 mutex_unlock(&adapter->crit_lock);
3339
3340 return 0;
3341
3342 err_req_irq:
3343 iavf_down(adapter);
3344 iavf_free_traffic_irqs(adapter);
3345 err_setup_rx:
3346 iavf_free_all_rx_resources(adapter);
3347 err_setup_tx:
3348 iavf_free_all_tx_resources(adapter);
3349 err_unlock:
3350 mutex_unlock(&adapter->crit_lock);
3351
3352 return err;
3353 }
3354
3355 /**
3356 * iavf_close - Disables a network interface
3357 * @netdev: network interface device structure
3358 *
3359 * Returns 0, this is not allowed to fail
3360 *
3361 * The close entry point is called when an interface is de-activated
3362 * by the OS. The hardware is still under the drivers control, but
3363 * needs to be disabled. All IRQs except vector 0 (reserved for admin queue)
3364 * are freed, along with all transmit and receive resources.
3365 **/
3366 static int iavf_close(struct net_device *netdev)
3367 {
3368 struct iavf_adapter *adapter = netdev_priv(netdev);
3369 int status;
3370
3371 if (adapter->state <= __IAVF_DOWN_PENDING)
3372 return 0;
3373
3374 while (!mutex_trylock(&adapter->crit_lock))
3375 usleep_range(500, 1000);
3376
3377 set_bit(__IAVF_VSI_DOWN, adapter->vsi.state);
3378 if (CLIENT_ENABLED(adapter))
3379 adapter->flags |= IAVF_FLAG_CLIENT_NEEDS_CLOSE;
3380
3381 iavf_down(adapter);
3382 iavf_change_state(adapter, __IAVF_DOWN_PENDING);
3383 iavf_free_traffic_irqs(adapter);
3384
3385 mutex_unlock(&adapter->crit_lock);
3386
3387 /* We explicitly don't free resources here because the hardware is
3388 * still active and can DMA into memory. Resources are cleared in
3389 * iavf_virtchnl_completion() after we get confirmation from the PF
3390 * driver that the rings have been stopped.
3391 *
3392 * Also, we wait for state to transition to __IAVF_DOWN before
3393 * returning. State change occurs in iavf_virtchnl_completion() after
3394 * VF resources are released (which occurs after PF driver processes and
3395 * responds to admin queue commands).
3396 */
3397
3398 status = wait_event_timeout(adapter->down_waitqueue,
3399 adapter->state == __IAVF_DOWN,
3400 msecs_to_jiffies(500));
3401 if (!status)
3402 netdev_warn(netdev, "Device resources not yet released\n");
3403 return 0;
3404 }
3405
3406 /**
3407 * iavf_change_mtu - Change the Maximum Transfer Unit
3408 * @netdev: network interface device structure
3409 * @new_mtu: new value for maximum frame size
3410 *
3411 * Returns 0 on success, negative on failure
3412 **/
3413 static int iavf_change_mtu(struct net_device *netdev, int new_mtu)
3414 {
3415 struct iavf_adapter *adapter = netdev_priv(netdev);
3416
3417 netdev->mtu = new_mtu;
3418 if (CLIENT_ENABLED(adapter)) {
3419 iavf_notify_client_l2_params(&adapter->vsi);
3420 adapter->flags |= IAVF_FLAG_SERVICE_CLIENT_REQUESTED;
3421 }
3422
3423 if (netif_running(netdev)) {
3424 adapter->flags |= IAVF_FLAG_RESET_NEEDED;
3425 queue_work(iavf_wq, &adapter->reset_task);
3426 }
3427
3428 return 0;
3429 }
3430
3431 /**
3432 * iavf_set_features - set the netdev feature flags
3433 * @netdev: ptr to the netdev being adjusted
3434 * @features: the feature set that the stack is suggesting
3435 * Note: expects to be called while under rtnl_lock()
3436 **/
3437 static int iavf_set_features(struct net_device *netdev,
3438 netdev_features_t features)
3439 {
3440 struct iavf_adapter *adapter = netdev_priv(netdev);
3441
3442 /* Don't allow enabling VLAN features when adapter is not capable
3443 * of VLAN offload/filtering
3444 */
3445 if (!VLAN_ALLOWED(adapter)) {
3446 netdev->hw_features &= ~(NETIF_F_HW_VLAN_CTAG_RX |
3447 NETIF_F_HW_VLAN_CTAG_TX |
3448 NETIF_F_HW_VLAN_CTAG_FILTER);
3449 if (features & (NETIF_F_HW_VLAN_CTAG_RX |
3450 NETIF_F_HW_VLAN_CTAG_TX |
3451 NETIF_F_HW_VLAN_CTAG_FILTER))
3452 return -EINVAL;
3453 } else if ((netdev->features ^ features) & NETIF_F_HW_VLAN_CTAG_RX) {
3454 if (features & NETIF_F_HW_VLAN_CTAG_RX)
3455 adapter->aq_required |=
3456 IAVF_FLAG_AQ_ENABLE_VLAN_STRIPPING;
3457 else
3458 adapter->aq_required |=
3459 IAVF_FLAG_AQ_DISABLE_VLAN_STRIPPING;
3460 }
3461
3462 return 0;
3463 }
3464
3465 /**
3466 * iavf_features_check - Validate encapsulated packet conforms to limits
3467 * @skb: skb buff
3468 * @dev: This physical port's netdev
3469 * @features: Offload features that the stack believes apply
3470 **/
3471 static netdev_features_t iavf_features_check(struct sk_buff *skb,
3472 struct net_device *dev,
3473 netdev_features_t features)
3474 {
3475 size_t len;
3476
3477 /* No point in doing any of this if neither checksum nor GSO are
3478 * being requested for this frame. We can rule out both by just
3479 * checking for CHECKSUM_PARTIAL
3480 */
3481 if (skb->ip_summed != CHECKSUM_PARTIAL)
3482 return features;
3483
3484 /* We cannot support GSO if the MSS is going to be less than
3485 * 64 bytes. If it is then we need to drop support for GSO.
3486 */
3487 if (skb_is_gso(skb) && (skb_shinfo(skb)->gso_size < 64))
3488 features &= ~NETIF_F_GSO_MASK;
3489
3490 /* MACLEN can support at most 63 words */
3491 len = skb_network_header(skb) - skb->data;
3492 if (len & ~(63 * 2))
3493 goto out_err;
3494
3495 /* IPLEN and EIPLEN can support at most 127 dwords */
3496 len = skb_transport_header(skb) - skb_network_header(skb);
3497 if (len & ~(127 * 4))
3498 goto out_err;
3499
3500 if (skb->encapsulation) {
3501 /* L4TUNLEN can support 127 words */
3502 len = skb_inner_network_header(skb) - skb_transport_header(skb);
3503 if (len & ~(127 * 2))
3504 goto out_err;
3505
3506 /* IPLEN can support at most 127 dwords */
3507 len = skb_inner_transport_header(skb) -
3508 skb_inner_network_header(skb);
3509 if (len & ~(127 * 4))
3510 goto out_err;
3511 }
3512
3513 /* No need to validate L4LEN as TCP is the only protocol with a
3514 * a flexible value and we support all possible values supported
3515 * by TCP, which is at most 15 dwords
3516 */
3517
3518 return features;
3519 out_err:
3520 return features & ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK);
3521 }
3522
3523 /**
3524 * iavf_fix_features - fix up the netdev feature bits
3525 * @netdev: our net device
3526 * @features: desired feature bits
3527 *
3528 * Returns fixed-up features bits
3529 **/
3530 static netdev_features_t iavf_fix_features(struct net_device *netdev,
3531 netdev_features_t features)
3532 {
3533 struct iavf_adapter *adapter = netdev_priv(netdev);
3534
3535 if (adapter->vf_res &&
3536 !(adapter->vf_res->vf_cap_flags & VIRTCHNL_VF_OFFLOAD_VLAN))
3537 features &= ~(NETIF_F_HW_VLAN_CTAG_TX |
3538 NETIF_F_HW_VLAN_CTAG_RX |
3539 NETIF_F_HW_VLAN_CTAG_FILTER);
3540
3541 return features;
3542 }
3543
3544 static const struct net_device_ops iavf_netdev_ops = {
3545 .ndo_open = iavf_open,
3546 .ndo_stop = iavf_close,
3547 .ndo_start_xmit = iavf_xmit_frame,
3548 .ndo_set_rx_mode = iavf_set_rx_mode,
3549 .ndo_validate_addr = eth_validate_addr,
3550 .ndo_set_mac_address = iavf_set_mac,
3551 .ndo_change_mtu = iavf_change_mtu,
3552 .ndo_tx_timeout = iavf_tx_timeout,
3553 .ndo_vlan_rx_add_vid = iavf_vlan_rx_add_vid,
3554 .ndo_vlan_rx_kill_vid = iavf_vlan_rx_kill_vid,
3555 .ndo_features_check = iavf_features_check,
3556 .ndo_fix_features = iavf_fix_features,
3557 .ndo_set_features = iavf_set_features,
3558 .ndo_setup_tc = iavf_setup_tc,
3559 };
3560
3561 /**
3562 * iavf_check_reset_complete - check that VF reset is complete
3563 * @hw: pointer to hw struct
3564 *
3565 * Returns 0 if device is ready to use, or -EBUSY if it's in reset.
3566 **/
3567 static int iavf_check_reset_complete(struct iavf_hw *hw)
3568 {
3569 u32 rstat;
3570 int i;
3571
3572 for (i = 0; i < IAVF_RESET_WAIT_COMPLETE_COUNT; i++) {
3573 rstat = rd32(hw, IAVF_VFGEN_RSTAT) &
3574 IAVF_VFGEN_RSTAT_VFR_STATE_MASK;
3575 if ((rstat == VIRTCHNL_VFR_VFACTIVE) ||
3576 (rstat == VIRTCHNL_VFR_COMPLETED))
3577 return 0;
3578 usleep_range(10, 20);
3579 }
3580 return -EBUSY;
3581 }
3582
3583 /**
3584 * iavf_process_config - Process the config information we got from the PF
3585 * @adapter: board private structure
3586 *
3587 * Verify that we have a valid config struct, and set up our netdev features
3588 * and our VSI struct.
3589 **/
3590 int iavf_process_config(struct iavf_adapter *adapter)
3591 {
3592 struct virtchnl_vf_resource *vfres = adapter->vf_res;
3593 int i, num_req_queues = adapter->num_req_queues;
3594 struct net_device *netdev = adapter->netdev;
3595 struct iavf_vsi *vsi = &adapter->vsi;
3596 netdev_features_t hw_enc_features;
3597 netdev_features_t hw_features;
3598
3599 /* got VF config message back from PF, now we can parse it */
3600 for (i = 0; i < vfres->num_vsis; i++) {
3601 if (vfres->vsi_res[i].vsi_type == VIRTCHNL_VSI_SRIOV)
3602 adapter->vsi_res = &vfres->vsi_res[i];
3603 }
3604 if (!adapter->vsi_res) {
3605 dev_err(&adapter->pdev->dev, "No LAN VSI found\n");
3606 return -ENODEV;
3607 }
3608
3609 if (num_req_queues &&
3610 num_req_queues > adapter->vsi_res->num_queue_pairs) {
3611 /* Problem. The PF gave us fewer queues than what we had
3612 * negotiated in our request. Need a reset to see if we can't
3613 * get back to a working state.
3614 */
3615 dev_err(&adapter->pdev->dev,
3616 "Requested %d queues, but PF only gave us %d.\n",
3617 num_req_queues,
3618 adapter->vsi_res->num_queue_pairs);
3619 adapter->flags |= IAVF_FLAG_REINIT_ITR_NEEDED;
3620 adapter->num_req_queues = adapter->vsi_res->num_queue_pairs;
3621 iavf_schedule_reset(adapter);
3622 return -ENODEV;
3623 }
3624 adapter->num_req_queues = 0;
3625
3626 hw_enc_features = NETIF_F_SG |
3627 NETIF_F_IP_CSUM |
3628 NETIF_F_IPV6_CSUM |
3629 NETIF_F_HIGHDMA |
3630 NETIF_F_SOFT_FEATURES |
3631 NETIF_F_TSO |
3632 NETIF_F_TSO_ECN |
3633 NETIF_F_TSO6 |
3634 NETIF_F_SCTP_CRC |
3635 NETIF_F_RXHASH |
3636 NETIF_F_RXCSUM |
3637 0;
3638
3639 /* advertise to stack only if offloads for encapsulated packets is
3640 * supported
3641 */
3642 if (vfres->vf_cap_flags & VIRTCHNL_VF_OFFLOAD_ENCAP) {
3643 hw_enc_features |= NETIF_F_GSO_UDP_TUNNEL |
3644 NETIF_F_GSO_GRE |
3645 NETIF_F_GSO_GRE_CSUM |
3646 NETIF_F_GSO_IPXIP4 |
3647 NETIF_F_GSO_IPXIP6 |
3648 NETIF_F_GSO_UDP_TUNNEL_CSUM |
3649 NETIF_F_GSO_PARTIAL |
3650 0;
3651
3652 if (!(vfres->vf_cap_flags &
3653 VIRTCHNL_VF_OFFLOAD_ENCAP_CSUM))
3654 netdev->gso_partial_features |=
3655 NETIF_F_GSO_UDP_TUNNEL_CSUM;
3656
3657 netdev->gso_partial_features |= NETIF_F_GSO_GRE_CSUM;
3658 netdev->hw_enc_features |= NETIF_F_TSO_MANGLEID;
3659 netdev->hw_enc_features |= hw_enc_features;
3660 }
3661 /* record features VLANs can make use of */
3662 netdev->vlan_features |= hw_enc_features | NETIF_F_TSO_MANGLEID;
3663
3664 /* Write features and hw_features separately to avoid polluting
3665 * with, or dropping, features that are set when we registered.
3666 */
3667 hw_features = hw_enc_features;
3668
3669 /* Enable VLAN features if supported */
3670 if (vfres->vf_cap_flags & VIRTCHNL_VF_OFFLOAD_VLAN)
3671 hw_features |= (NETIF_F_HW_VLAN_CTAG_TX |
3672 NETIF_F_HW_VLAN_CTAG_RX);
3673 /* Enable cloud filter if ADQ is supported */
3674 if (vfres->vf_cap_flags & VIRTCHNL_VF_OFFLOAD_ADQ)
3675 hw_features |= NETIF_F_HW_TC;
3676 if (vfres->vf_cap_flags & VIRTCHNL_VF_OFFLOAD_USO)
3677 hw_features |= NETIF_F_GSO_UDP_L4;
3678
3679 netdev->hw_features |= hw_features;
3680
3681 netdev->features |= hw_features;
3682
3683 if (vfres->vf_cap_flags & VIRTCHNL_VF_OFFLOAD_VLAN)
3684 netdev->features |= NETIF_F_HW_VLAN_CTAG_FILTER;
3685
3686 netdev->priv_flags |= IFF_UNICAST_FLT;
3687
3688 /* Do not turn on offloads when they are requested to be turned off.
3689 * TSO needs minimum 576 bytes to work correctly.
3690 */
3691 if (netdev->wanted_features) {
3692 if (!(netdev->wanted_features & NETIF_F_TSO) ||
3693 netdev->mtu < 576)
3694 netdev->features &= ~NETIF_F_TSO;
3695 if (!(netdev->wanted_features & NETIF_F_TSO6) ||
3696 netdev->mtu < 576)
3697 netdev->features &= ~NETIF_F_TSO6;
3698 if (!(netdev->wanted_features & NETIF_F_TSO_ECN))
3699 netdev->features &= ~NETIF_F_TSO_ECN;
3700 if (!(netdev->wanted_features & NETIF_F_GRO))
3701 netdev->features &= ~NETIF_F_GRO;
3702 if (!(netdev->wanted_features & NETIF_F_GSO))
3703 netdev->features &= ~NETIF_F_GSO;
3704 }
3705
3706 adapter->vsi.id = adapter->vsi_res->vsi_id;
3707
3708 adapter->vsi.back = adapter;
3709 adapter->vsi.base_vector = 1;
3710 adapter->vsi.work_limit = IAVF_DEFAULT_IRQ_WORK;
3711 vsi->netdev = adapter->netdev;
3712 vsi->qs_handle = adapter->vsi_res->qset_handle;
3713 if (vfres->vf_cap_flags & VIRTCHNL_VF_OFFLOAD_RSS_PF) {
3714 adapter->rss_key_size = vfres->rss_key_size;
3715 adapter->rss_lut_size = vfres->rss_lut_size;
3716 } else {
3717 adapter->rss_key_size = IAVF_HKEY_ARRAY_SIZE;
3718 adapter->rss_lut_size = IAVF_HLUT_ARRAY_SIZE;
3719 }
3720
3721 return 0;
3722 }
3723
3724 /**
3725 * iavf_shutdown - Shutdown the device in preparation for a reboot
3726 * @pdev: pci device structure
3727 **/
3728 static void iavf_shutdown(struct pci_dev *pdev)
3729 {
3730 struct net_device *netdev = pci_get_drvdata(pdev);
3731 struct iavf_adapter *adapter = netdev_priv(netdev);
3732
3733 netif_device_detach(netdev);
3734
3735 if (netif_running(netdev))
3736 iavf_close(netdev);
3737
3738 if (iavf_lock_timeout(&adapter->crit_lock, 5000))
3739 dev_warn(&adapter->pdev->dev, "failed to acquire crit_lock in %s\n", __FUNCTION__);
3740 /* Prevent the watchdog from running. */
3741 iavf_change_state(adapter, __IAVF_REMOVE);
3742 adapter->aq_required = 0;
3743 mutex_unlock(&adapter->crit_lock);
3744
3745 #ifdef CONFIG_PM
3746 pci_save_state(pdev);
3747
3748 #endif
3749 pci_disable_device(pdev);
3750 }
3751
3752 /**
3753 * iavf_probe - Device Initialization Routine
3754 * @pdev: PCI device information struct
3755 * @ent: entry in iavf_pci_tbl
3756 *
3757 * Returns 0 on success, negative on failure
3758 *
3759 * iavf_probe initializes an adapter identified by a pci_dev structure.
3760 * The OS initialization, configuring of the adapter private structure,
3761 * and a hardware reset occur.
3762 **/
3763 static int iavf_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
3764 {
3765 struct net_device *netdev;
3766 struct iavf_adapter *adapter = NULL;
3767 struct iavf_hw *hw = NULL;
3768 int err;
3769
3770 err = pci_enable_device(pdev);
3771 if (err)
3772 return err;
3773
3774 err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
3775 if (err) {
3776 err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
3777 if (err) {
3778 dev_err(&pdev->dev,
3779 "DMA configuration failed: 0x%x\n", err);
3780 goto err_dma;
3781 }
3782 }
3783
3784 err = pci_request_regions(pdev, iavf_driver_name);
3785 if (err) {
3786 dev_err(&pdev->dev,
3787 "pci_request_regions failed 0x%x\n", err);
3788 goto err_pci_reg;
3789 }
3790
3791 pci_enable_pcie_error_reporting(pdev);
3792
3793 pci_set_master(pdev);
3794
3795 netdev = alloc_etherdev_mq(sizeof(struct iavf_adapter),
3796 IAVF_MAX_REQ_QUEUES);
3797 if (!netdev) {
3798 err = -ENOMEM;
3799 goto err_alloc_etherdev;
3800 }
3801
3802 SET_NETDEV_DEV(netdev, &pdev->dev);
3803
3804 pci_set_drvdata(pdev, netdev);
3805 adapter = netdev_priv(netdev);
3806
3807 adapter->netdev = netdev;
3808 adapter->pdev = pdev;
3809
3810 hw = &adapter->hw;
3811 hw->back = adapter;
3812
3813 adapter->msg_enable = BIT(DEFAULT_DEBUG_LEVEL_SHIFT) - 1;
3814 iavf_change_state(adapter, __IAVF_STARTUP);
3815
3816 /* Call save state here because it relies on the adapter struct. */
3817 pci_save_state(pdev);
3818
3819 hw->hw_addr = ioremap(pci_resource_start(pdev, 0),
3820 pci_resource_len(pdev, 0));
3821 if (!hw->hw_addr) {
3822 err = -EIO;
3823 goto err_ioremap;
3824 }
3825 hw->vendor_id = pdev->vendor;
3826 hw->device_id = pdev->device;
3827 pci_read_config_byte(pdev, PCI_REVISION_ID, &hw->revision_id);
3828 hw->subsystem_vendor_id = pdev->subsystem_vendor;
3829 hw->subsystem_device_id = pdev->subsystem_device;
3830 hw->bus.device = PCI_SLOT(pdev->devfn);
3831 hw->bus.func = PCI_FUNC(pdev->devfn);
3832 hw->bus.bus_id = pdev->bus->number;
3833
3834 /* set up the locks for the AQ, do this only once in probe
3835 * and destroy them only once in remove
3836 */
3837 mutex_init(&adapter->crit_lock);
3838 mutex_init(&adapter->client_lock);
3839 mutex_init(&adapter->remove_lock);
3840 mutex_init(&hw->aq.asq_mutex);
3841 mutex_init(&hw->aq.arq_mutex);
3842
3843 spin_lock_init(&adapter->mac_vlan_list_lock);
3844 spin_lock_init(&adapter->cloud_filter_list_lock);
3845 spin_lock_init(&adapter->fdir_fltr_lock);
3846 spin_lock_init(&adapter->adv_rss_lock);
3847
3848 INIT_LIST_HEAD(&adapter->mac_filter_list);
3849 INIT_LIST_HEAD(&adapter->vlan_filter_list);
3850 INIT_LIST_HEAD(&adapter->cloud_filter_list);
3851 INIT_LIST_HEAD(&adapter->fdir_list_head);
3852 INIT_LIST_HEAD(&adapter->adv_rss_list_head);
3853
3854 INIT_WORK(&adapter->reset_task, iavf_reset_task);
3855 INIT_WORK(&adapter->adminq_task, iavf_adminq_task);
3856 INIT_DELAYED_WORK(&adapter->watchdog_task, iavf_watchdog_task);
3857 INIT_DELAYED_WORK(&adapter->client_task, iavf_client_task);
3858 queue_delayed_work(iavf_wq, &adapter->watchdog_task,
3859 msecs_to_jiffies(5 * (pdev->devfn & 0x07)));
3860
3861 /* Setup the wait queue for indicating transition to down status */
3862 init_waitqueue_head(&adapter->down_waitqueue);
3863
3864 return 0;
3865
3866 err_ioremap:
3867 free_netdev(netdev);
3868 err_alloc_etherdev:
3869 pci_disable_pcie_error_reporting(pdev);
3870 pci_release_regions(pdev);
3871 err_pci_reg:
3872 err_dma:
3873 pci_disable_device(pdev);
3874 return err;
3875 }
3876
3877 /**
3878 * iavf_suspend - Power management suspend routine
3879 * @dev_d: device info pointer
3880 *
3881 * Called when the system (VM) is entering sleep/suspend.
3882 **/
3883 static int __maybe_unused iavf_suspend(struct device *dev_d)
3884 {
3885 struct net_device *netdev = dev_get_drvdata(dev_d);
3886 struct iavf_adapter *adapter = netdev_priv(netdev);
3887
3888 netif_device_detach(netdev);
3889
3890 while (!mutex_trylock(&adapter->crit_lock))
3891 usleep_range(500, 1000);
3892
3893 if (netif_running(netdev)) {
3894 rtnl_lock();
3895 iavf_down(adapter);
3896 rtnl_unlock();
3897 }
3898 iavf_free_misc_irq(adapter);
3899 iavf_reset_interrupt_capability(adapter);
3900
3901 mutex_unlock(&adapter->crit_lock);
3902
3903 return 0;
3904 }
3905
3906 /**
3907 * iavf_resume - Power management resume routine
3908 * @dev_d: device info pointer
3909 *
3910 * Called when the system (VM) is resumed from sleep/suspend.
3911 **/
3912 static int __maybe_unused iavf_resume(struct device *dev_d)
3913 {
3914 struct pci_dev *pdev = to_pci_dev(dev_d);
3915 struct net_device *netdev = pci_get_drvdata(pdev);
3916 struct iavf_adapter *adapter = netdev_priv(netdev);
3917 u32 err;
3918
3919 pci_set_master(pdev);
3920
3921 rtnl_lock();
3922 err = iavf_set_interrupt_capability(adapter);
3923 if (err) {
3924 rtnl_unlock();
3925 dev_err(&pdev->dev, "Cannot enable MSI-X interrupts.\n");
3926 return err;
3927 }
3928 err = iavf_request_misc_irq(adapter);
3929 rtnl_unlock();
3930 if (err) {
3931 dev_err(&pdev->dev, "Cannot get interrupt vector.\n");
3932 return err;
3933 }
3934
3935 queue_work(iavf_wq, &adapter->reset_task);
3936
3937 netif_device_attach(netdev);
3938
3939 return err;
3940 }
3941
3942 /**
3943 * iavf_remove - Device Removal Routine
3944 * @pdev: PCI device information struct
3945 *
3946 * iavf_remove is called by the PCI subsystem to alert the driver
3947 * that it should release a PCI device. The could be caused by a
3948 * Hot-Plug event, or because the driver is going to be removed from
3949 * memory.
3950 **/
3951 static void iavf_remove(struct pci_dev *pdev)
3952 {
3953 struct net_device *netdev = pci_get_drvdata(pdev);
3954 struct iavf_adapter *adapter = netdev_priv(netdev);
3955 struct iavf_fdir_fltr *fdir, *fdirtmp;
3956 struct iavf_vlan_filter *vlf, *vlftmp;
3957 struct iavf_adv_rss *rss, *rsstmp;
3958 struct iavf_mac_filter *f, *ftmp;
3959 struct iavf_cloud_filter *cf, *cftmp;
3960 struct iavf_hw *hw = &adapter->hw;
3961 int err;
3962 /* Indicate we are in remove and not to run reset_task */
3963 mutex_lock(&adapter->remove_lock);
3964 cancel_work_sync(&adapter->reset_task);
3965 cancel_delayed_work_sync(&adapter->watchdog_task);
3966 cancel_delayed_work_sync(&adapter->client_task);
3967 if (adapter->netdev_registered) {
3968 unregister_netdev(netdev);
3969 adapter->netdev_registered = false;
3970 }
3971 if (CLIENT_ALLOWED(adapter)) {
3972 err = iavf_lan_del_device(adapter);
3973 if (err)
3974 dev_warn(&pdev->dev, "Failed to delete client device: %d\n",
3975 err);
3976 }
3977
3978 iavf_request_reset(adapter);
3979 msleep(50);
3980 /* If the FW isn't responding, kick it once, but only once. */
3981 if (!iavf_asq_done(hw)) {
3982 iavf_request_reset(adapter);
3983 msleep(50);
3984 }
3985 if (iavf_lock_timeout(&adapter->crit_lock, 5000))
3986 dev_warn(&adapter->pdev->dev, "failed to acquire crit_lock in %s\n", __FUNCTION__);
3987
3988 /* Shut down all the garbage mashers on the detention level */
3989 iavf_change_state(adapter, __IAVF_REMOVE);
3990 adapter->aq_required = 0;
3991 adapter->flags &= ~IAVF_FLAG_REINIT_ITR_NEEDED;
3992 iavf_free_all_tx_resources(adapter);
3993 iavf_free_all_rx_resources(adapter);
3994 iavf_misc_irq_disable(adapter);
3995 iavf_free_misc_irq(adapter);
3996 iavf_reset_interrupt_capability(adapter);
3997 iavf_free_q_vectors(adapter);
3998
3999 cancel_delayed_work_sync(&adapter->watchdog_task);
4000
4001 cancel_work_sync(&adapter->adminq_task);
4002
4003 iavf_free_rss(adapter);
4004
4005 if (hw->aq.asq.count)
4006 iavf_shutdown_adminq(hw);
4007
4008 /* destroy the locks only once, here */
4009 mutex_destroy(&hw->aq.arq_mutex);
4010 mutex_destroy(&hw->aq.asq_mutex);
4011 mutex_destroy(&adapter->client_lock);
4012 mutex_unlock(&adapter->crit_lock);
4013 mutex_destroy(&adapter->crit_lock);
4014 mutex_unlock(&adapter->remove_lock);
4015 mutex_destroy(&adapter->remove_lock);
4016
4017 iounmap(hw->hw_addr);
4018 pci_release_regions(pdev);
4019 iavf_free_queues(adapter);
4020 kfree(adapter->vf_res);
4021 spin_lock_bh(&adapter->mac_vlan_list_lock);
4022 /* If we got removed before an up/down sequence, we've got a filter
4023 * hanging out there that we need to get rid of.
4024 */
4025 list_for_each_entry_safe(f, ftmp, &adapter->mac_filter_list, list) {
4026 list_del(&f->list);
4027 kfree(f);
4028 }
4029 list_for_each_entry_safe(vlf, vlftmp, &adapter->vlan_filter_list,
4030 list) {
4031 list_del(&vlf->list);
4032 kfree(vlf);
4033 }
4034
4035 spin_unlock_bh(&adapter->mac_vlan_list_lock);
4036
4037 spin_lock_bh(&adapter->cloud_filter_list_lock);
4038 list_for_each_entry_safe(cf, cftmp, &adapter->cloud_filter_list, list) {
4039 list_del(&cf->list);
4040 kfree(cf);
4041 }
4042 spin_unlock_bh(&adapter->cloud_filter_list_lock);
4043
4044 spin_lock_bh(&adapter->fdir_fltr_lock);
4045 list_for_each_entry_safe(fdir, fdirtmp, &adapter->fdir_list_head, list) {
4046 list_del(&fdir->list);
4047 kfree(fdir);
4048 }
4049 spin_unlock_bh(&adapter->fdir_fltr_lock);
4050
4051 spin_lock_bh(&adapter->adv_rss_lock);
4052 list_for_each_entry_safe(rss, rsstmp, &adapter->adv_rss_list_head,
4053 list) {
4054 list_del(&rss->list);
4055 kfree(rss);
4056 }
4057 spin_unlock_bh(&adapter->adv_rss_lock);
4058
4059 free_netdev(netdev);
4060
4061 pci_disable_pcie_error_reporting(pdev);
4062
4063 pci_disable_device(pdev);
4064 }
4065
4066 static SIMPLE_DEV_PM_OPS(iavf_pm_ops, iavf_suspend, iavf_resume);
4067
4068 static struct pci_driver iavf_driver = {
4069 .name = iavf_driver_name,
4070 .id_table = iavf_pci_tbl,
4071 .probe = iavf_probe,
4072 .remove = iavf_remove,
4073 .driver.pm = &iavf_pm_ops,
4074 .shutdown = iavf_shutdown,
4075 };
4076
4077 /**
4078 * iavf_init_module - Driver Registration Routine
4079 *
4080 * iavf_init_module is the first routine called when the driver is
4081 * loaded. All it does is register with the PCI subsystem.
4082 **/
4083 static int __init iavf_init_module(void)
4084 {
4085 int ret;
4086
4087 pr_info("iavf: %s\n", iavf_driver_string);
4088
4089 pr_info("%s\n", iavf_copyright);
4090
4091 iavf_wq = alloc_workqueue("%s", WQ_UNBOUND | WQ_MEM_RECLAIM, 1,
4092 iavf_driver_name);
4093 if (!iavf_wq) {
4094 pr_err("%s: Failed to create workqueue\n", iavf_driver_name);
4095 return -ENOMEM;
4096 }
4097 ret = pci_register_driver(&iavf_driver);
4098 return ret;
4099 }
4100
4101 module_init(iavf_init_module);
4102
4103 /**
4104 * iavf_exit_module - Driver Exit Cleanup Routine
4105 *
4106 * iavf_exit_module is called just before the driver is removed
4107 * from memory.
4108 **/
4109 static void __exit iavf_exit_module(void)
4110 {
4111 pci_unregister_driver(&iavf_driver);
4112 destroy_workqueue(iavf_wq);
4113 }
4114
4115 module_exit(iavf_exit_module);
4116
4117 /* iavf_main.c */