]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blob - drivers/net/ethernet/intel/iavf/iavf_main.c
181d17945bbe003e46738eef516b3f2b7ecbbc9d
[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 switch (adapter->state) {
1982 case __IAVF_COMM_FAILED:
1983 reg_val = rd32(hw, IAVF_VFGEN_RSTAT) &
1984 IAVF_VFGEN_RSTAT_VFR_STATE_MASK;
1985 if (reg_val == VIRTCHNL_VFR_VFACTIVE ||
1986 reg_val == VIRTCHNL_VFR_COMPLETED) {
1987 /* A chance for redemption! */
1988 dev_err(&adapter->pdev->dev,
1989 "Hardware came out of reset. Attempting reinit.\n");
1990 iavf_change_state(adapter, __IAVF_STARTUP);
1991 adapter->flags &= ~IAVF_FLAG_PF_COMMS_FAILED;
1992 queue_delayed_work(iavf_wq, &adapter->init_task, 10);
1993 mutex_unlock(&adapter->crit_lock);
1994 /* Don't reschedule the watchdog, since we've restarted
1995 * the init task. When init_task contacts the PF and
1996 * gets everything set up again, it'll restart the
1997 * watchdog for us. Down, boy. Sit. Stay. Woof.
1998 */
1999 return;
2000 }
2001 adapter->aq_required = 0;
2002 adapter->current_op = VIRTCHNL_OP_UNKNOWN;
2003 queue_delayed_work(iavf_wq,
2004 &adapter->watchdog_task,
2005 msecs_to_jiffies(10));
2006 goto watchdog_done;
2007 case __IAVF_RESETTING:
2008 mutex_unlock(&adapter->crit_lock);
2009 queue_delayed_work(iavf_wq, &adapter->watchdog_task, HZ * 2);
2010 return;
2011 case __IAVF_DOWN:
2012 case __IAVF_DOWN_PENDING:
2013 case __IAVF_TESTING:
2014 case __IAVF_RUNNING:
2015 if (adapter->current_op) {
2016 if (!iavf_asq_done(hw)) {
2017 dev_dbg(&adapter->pdev->dev,
2018 "Admin queue timeout\n");
2019 iavf_send_api_ver(adapter);
2020 }
2021 } else {
2022 /* An error will be returned if no commands were
2023 * processed; use this opportunity to update stats
2024 */
2025 if (iavf_process_aq_command(adapter) &&
2026 adapter->state == __IAVF_RUNNING)
2027 iavf_request_stats(adapter);
2028 }
2029 break;
2030 case __IAVF_REMOVE:
2031 mutex_unlock(&adapter->crit_lock);
2032 return;
2033 default:
2034 goto restart_watchdog;
2035 }
2036
2037 /* check for hw reset */
2038 reg_val = rd32(hw, IAVF_VF_ARQLEN1) & IAVF_VF_ARQLEN1_ARQENABLE_MASK;
2039 if (!reg_val) {
2040 iavf_change_state(adapter, __IAVF_RESETTING);
2041 adapter->flags |= IAVF_FLAG_RESET_PENDING;
2042 adapter->aq_required = 0;
2043 adapter->current_op = VIRTCHNL_OP_UNKNOWN;
2044 dev_err(&adapter->pdev->dev, "Hardware reset detected\n");
2045 queue_work(iavf_wq, &adapter->reset_task);
2046 goto watchdog_done;
2047 }
2048
2049 schedule_delayed_work(&adapter->client_task, msecs_to_jiffies(5));
2050 watchdog_done:
2051 if (adapter->state == __IAVF_RUNNING ||
2052 adapter->state == __IAVF_COMM_FAILED)
2053 iavf_detect_recover_hung(&adapter->vsi);
2054 mutex_unlock(&adapter->crit_lock);
2055 restart_watchdog:
2056 if (adapter->aq_required)
2057 queue_delayed_work(iavf_wq, &adapter->watchdog_task,
2058 msecs_to_jiffies(20));
2059 else
2060 queue_delayed_work(iavf_wq, &adapter->watchdog_task, HZ * 2);
2061 queue_work(iavf_wq, &adapter->adminq_task);
2062 }
2063
2064 static void iavf_disable_vf(struct iavf_adapter *adapter)
2065 {
2066 struct iavf_mac_filter *f, *ftmp;
2067 struct iavf_vlan_filter *fv, *fvtmp;
2068 struct iavf_cloud_filter *cf, *cftmp;
2069
2070 adapter->flags |= IAVF_FLAG_PF_COMMS_FAILED;
2071
2072 /* We don't use netif_running() because it may be true prior to
2073 * ndo_open() returning, so we can't assume it means all our open
2074 * tasks have finished, since we're not holding the rtnl_lock here.
2075 */
2076 if (adapter->state == __IAVF_RUNNING) {
2077 set_bit(__IAVF_VSI_DOWN, adapter->vsi.state);
2078 netif_carrier_off(adapter->netdev);
2079 netif_tx_disable(adapter->netdev);
2080 adapter->link_up = false;
2081 iavf_napi_disable_all(adapter);
2082 iavf_irq_disable(adapter);
2083 iavf_free_traffic_irqs(adapter);
2084 iavf_free_all_tx_resources(adapter);
2085 iavf_free_all_rx_resources(adapter);
2086 }
2087
2088 spin_lock_bh(&adapter->mac_vlan_list_lock);
2089
2090 /* Delete all of the filters */
2091 list_for_each_entry_safe(f, ftmp, &adapter->mac_filter_list, list) {
2092 list_del(&f->list);
2093 kfree(f);
2094 }
2095
2096 list_for_each_entry_safe(fv, fvtmp, &adapter->vlan_filter_list, list) {
2097 list_del(&fv->list);
2098 kfree(fv);
2099 }
2100
2101 spin_unlock_bh(&adapter->mac_vlan_list_lock);
2102
2103 spin_lock_bh(&adapter->cloud_filter_list_lock);
2104 list_for_each_entry_safe(cf, cftmp, &adapter->cloud_filter_list, list) {
2105 list_del(&cf->list);
2106 kfree(cf);
2107 adapter->num_cloud_filters--;
2108 }
2109 spin_unlock_bh(&adapter->cloud_filter_list_lock);
2110
2111 iavf_free_misc_irq(adapter);
2112 iavf_reset_interrupt_capability(adapter);
2113 iavf_free_q_vectors(adapter);
2114 iavf_free_queues(adapter);
2115 memset(adapter->vf_res, 0, IAVF_VIRTCHNL_VF_RESOURCE_SIZE);
2116 iavf_shutdown_adminq(&adapter->hw);
2117 adapter->netdev->flags &= ~IFF_UP;
2118 mutex_unlock(&adapter->crit_lock);
2119 adapter->flags &= ~IAVF_FLAG_RESET_PENDING;
2120 iavf_change_state(adapter, __IAVF_DOWN);
2121 wake_up(&adapter->down_waitqueue);
2122 dev_info(&adapter->pdev->dev, "Reset task did not complete, VF disabled\n");
2123 }
2124
2125 /**
2126 * iavf_reset_task - Call-back task to handle hardware reset
2127 * @work: pointer to work_struct
2128 *
2129 * During reset we need to shut down and reinitialize the admin queue
2130 * before we can use it to communicate with the PF again. We also clear
2131 * and reinit the rings because that context is lost as well.
2132 **/
2133 static void iavf_reset_task(struct work_struct *work)
2134 {
2135 struct iavf_adapter *adapter = container_of(work,
2136 struct iavf_adapter,
2137 reset_task);
2138 struct virtchnl_vf_resource *vfres = adapter->vf_res;
2139 struct net_device *netdev = adapter->netdev;
2140 struct iavf_hw *hw = &adapter->hw;
2141 struct iavf_mac_filter *f, *ftmp;
2142 struct iavf_cloud_filter *cf;
2143 u32 reg_val;
2144 int i = 0, err;
2145 bool running;
2146
2147 /* When device is being removed it doesn't make sense to run the reset
2148 * task, just return in such a case.
2149 */
2150 if (mutex_is_locked(&adapter->remove_lock))
2151 return;
2152
2153 if (iavf_lock_timeout(&adapter->crit_lock, 200)) {
2154 schedule_work(&adapter->reset_task);
2155 return;
2156 }
2157 while (!mutex_trylock(&adapter->client_lock))
2158 usleep_range(500, 1000);
2159 if (CLIENT_ENABLED(adapter)) {
2160 adapter->flags &= ~(IAVF_FLAG_CLIENT_NEEDS_OPEN |
2161 IAVF_FLAG_CLIENT_NEEDS_CLOSE |
2162 IAVF_FLAG_CLIENT_NEEDS_L2_PARAMS |
2163 IAVF_FLAG_SERVICE_CLIENT_REQUESTED);
2164 cancel_delayed_work_sync(&adapter->client_task);
2165 iavf_notify_client_close(&adapter->vsi, true);
2166 }
2167 iavf_misc_irq_disable(adapter);
2168 if (adapter->flags & IAVF_FLAG_RESET_NEEDED) {
2169 adapter->flags &= ~IAVF_FLAG_RESET_NEEDED;
2170 /* Restart the AQ here. If we have been reset but didn't
2171 * detect it, or if the PF had to reinit, our AQ will be hosed.
2172 */
2173 iavf_shutdown_adminq(hw);
2174 iavf_init_adminq(hw);
2175 iavf_request_reset(adapter);
2176 }
2177 adapter->flags |= IAVF_FLAG_RESET_PENDING;
2178
2179 /* poll until we see the reset actually happen */
2180 for (i = 0; i < IAVF_RESET_WAIT_DETECTED_COUNT; i++) {
2181 reg_val = rd32(hw, IAVF_VF_ARQLEN1) &
2182 IAVF_VF_ARQLEN1_ARQENABLE_MASK;
2183 if (!reg_val)
2184 break;
2185 usleep_range(5000, 10000);
2186 }
2187 if (i == IAVF_RESET_WAIT_DETECTED_COUNT) {
2188 dev_info(&adapter->pdev->dev, "Never saw reset\n");
2189 goto continue_reset; /* act like the reset happened */
2190 }
2191
2192 /* wait until the reset is complete and the PF is responding to us */
2193 for (i = 0; i < IAVF_RESET_WAIT_COMPLETE_COUNT; i++) {
2194 /* sleep first to make sure a minimum wait time is met */
2195 msleep(IAVF_RESET_WAIT_MS);
2196
2197 reg_val = rd32(hw, IAVF_VFGEN_RSTAT) &
2198 IAVF_VFGEN_RSTAT_VFR_STATE_MASK;
2199 if (reg_val == VIRTCHNL_VFR_VFACTIVE)
2200 break;
2201 }
2202
2203 pci_set_master(adapter->pdev);
2204 pci_restore_msi_state(adapter->pdev);
2205
2206 if (i == IAVF_RESET_WAIT_COMPLETE_COUNT) {
2207 dev_err(&adapter->pdev->dev, "Reset never finished (%x)\n",
2208 reg_val);
2209 iavf_disable_vf(adapter);
2210 mutex_unlock(&adapter->client_lock);
2211 mutex_unlock(&adapter->crit_lock);
2212 return; /* Do not attempt to reinit. It's dead, Jim. */
2213 }
2214
2215 continue_reset:
2216 /* We don't use netif_running() because it may be true prior to
2217 * ndo_open() returning, so we can't assume it means all our open
2218 * tasks have finished, since we're not holding the rtnl_lock here.
2219 */
2220 running = ((adapter->state == __IAVF_RUNNING) ||
2221 (adapter->state == __IAVF_RESETTING));
2222
2223 if (running) {
2224 netif_carrier_off(netdev);
2225 netif_tx_stop_all_queues(netdev);
2226 adapter->link_up = false;
2227 iavf_napi_disable_all(adapter);
2228 }
2229 iavf_irq_disable(adapter);
2230
2231 iavf_change_state(adapter, __IAVF_RESETTING);
2232 adapter->flags &= ~IAVF_FLAG_RESET_PENDING;
2233
2234 /* free the Tx/Rx rings and descriptors, might be better to just
2235 * re-use them sometime in the future
2236 */
2237 iavf_free_all_rx_resources(adapter);
2238 iavf_free_all_tx_resources(adapter);
2239
2240 adapter->flags |= IAVF_FLAG_QUEUES_DISABLED;
2241 /* kill and reinit the admin queue */
2242 iavf_shutdown_adminq(hw);
2243 adapter->current_op = VIRTCHNL_OP_UNKNOWN;
2244 err = iavf_init_adminq(hw);
2245 if (err)
2246 dev_info(&adapter->pdev->dev, "Failed to init adminq: %d\n",
2247 err);
2248 adapter->aq_required = 0;
2249
2250 if (adapter->flags & IAVF_FLAG_REINIT_ITR_NEEDED) {
2251 err = iavf_reinit_interrupt_scheme(adapter);
2252 if (err)
2253 goto reset_err;
2254 }
2255
2256 if (RSS_AQ(adapter)) {
2257 adapter->aq_required |= IAVF_FLAG_AQ_CONFIGURE_RSS;
2258 } else {
2259 err = iavf_init_rss(adapter);
2260 if (err)
2261 goto reset_err;
2262 }
2263
2264 adapter->aq_required |= IAVF_FLAG_AQ_GET_CONFIG;
2265 adapter->aq_required |= IAVF_FLAG_AQ_MAP_VECTORS;
2266
2267 spin_lock_bh(&adapter->mac_vlan_list_lock);
2268
2269 /* Delete filter for the current MAC address, it could have
2270 * been changed by the PF via administratively set MAC.
2271 * Will be re-added via VIRTCHNL_OP_GET_VF_RESOURCES.
2272 */
2273 list_for_each_entry_safe(f, ftmp, &adapter->mac_filter_list, list) {
2274 if (ether_addr_equal(f->macaddr, adapter->hw.mac.addr)) {
2275 list_del(&f->list);
2276 kfree(f);
2277 }
2278 }
2279 /* re-add all MAC filters */
2280 list_for_each_entry(f, &adapter->mac_filter_list, list) {
2281 f->add = true;
2282 }
2283 spin_unlock_bh(&adapter->mac_vlan_list_lock);
2284
2285 /* check if TCs are running and re-add all cloud filters */
2286 spin_lock_bh(&adapter->cloud_filter_list_lock);
2287 if ((vfres->vf_cap_flags & VIRTCHNL_VF_OFFLOAD_ADQ) &&
2288 adapter->num_tc) {
2289 list_for_each_entry(cf, &adapter->cloud_filter_list, list) {
2290 cf->add = true;
2291 }
2292 }
2293 spin_unlock_bh(&adapter->cloud_filter_list_lock);
2294
2295 adapter->aq_required |= IAVF_FLAG_AQ_ADD_MAC_FILTER;
2296 adapter->aq_required |= IAVF_FLAG_AQ_ADD_CLOUD_FILTER;
2297 iavf_misc_irq_enable(adapter);
2298
2299 mod_delayed_work(iavf_wq, &adapter->watchdog_task, 2);
2300
2301 /* We were running when the reset started, so we need to restore some
2302 * state here.
2303 */
2304 if (running) {
2305 /* allocate transmit descriptors */
2306 err = iavf_setup_all_tx_resources(adapter);
2307 if (err)
2308 goto reset_err;
2309
2310 /* allocate receive descriptors */
2311 err = iavf_setup_all_rx_resources(adapter);
2312 if (err)
2313 goto reset_err;
2314
2315 if (adapter->flags & IAVF_FLAG_REINIT_ITR_NEEDED) {
2316 err = iavf_request_traffic_irqs(adapter, netdev->name);
2317 if (err)
2318 goto reset_err;
2319
2320 adapter->flags &= ~IAVF_FLAG_REINIT_ITR_NEEDED;
2321 }
2322
2323 iavf_configure(adapter);
2324
2325 /* iavf_up_complete() will switch device back
2326 * to __IAVF_RUNNING
2327 */
2328 iavf_up_complete(adapter);
2329
2330 iavf_irq_enable(adapter, true);
2331 } else {
2332 iavf_change_state(adapter, __IAVF_DOWN);
2333 wake_up(&adapter->down_waitqueue);
2334 }
2335 mutex_unlock(&adapter->client_lock);
2336 mutex_unlock(&adapter->crit_lock);
2337
2338 return;
2339 reset_err:
2340 mutex_unlock(&adapter->client_lock);
2341 mutex_unlock(&adapter->crit_lock);
2342 dev_err(&adapter->pdev->dev, "failed to allocate resources during reinit\n");
2343 iavf_close(netdev);
2344 }
2345
2346 /**
2347 * iavf_adminq_task - worker thread to clean the admin queue
2348 * @work: pointer to work_struct containing our data
2349 **/
2350 static void iavf_adminq_task(struct work_struct *work)
2351 {
2352 struct iavf_adapter *adapter =
2353 container_of(work, struct iavf_adapter, adminq_task);
2354 struct iavf_hw *hw = &adapter->hw;
2355 struct iavf_arq_event_info event;
2356 enum virtchnl_ops v_op;
2357 enum iavf_status ret, v_ret;
2358 u32 val, oldval;
2359 u16 pending;
2360
2361 if (adapter->flags & IAVF_FLAG_PF_COMMS_FAILED)
2362 goto out;
2363
2364 event.buf_len = IAVF_MAX_AQ_BUF_SIZE;
2365 event.msg_buf = kzalloc(event.buf_len, GFP_KERNEL);
2366 if (!event.msg_buf)
2367 goto out;
2368
2369 if (iavf_lock_timeout(&adapter->crit_lock, 200))
2370 goto freedom;
2371 do {
2372 ret = iavf_clean_arq_element(hw, &event, &pending);
2373 v_op = (enum virtchnl_ops)le32_to_cpu(event.desc.cookie_high);
2374 v_ret = (enum iavf_status)le32_to_cpu(event.desc.cookie_low);
2375
2376 if (ret || !v_op)
2377 break; /* No event to process or error cleaning ARQ */
2378
2379 iavf_virtchnl_completion(adapter, v_op, v_ret, event.msg_buf,
2380 event.msg_len);
2381 if (pending != 0)
2382 memset(event.msg_buf, 0, IAVF_MAX_AQ_BUF_SIZE);
2383 } while (pending);
2384 mutex_unlock(&adapter->crit_lock);
2385
2386 if ((adapter->flags &
2387 (IAVF_FLAG_RESET_PENDING | IAVF_FLAG_RESET_NEEDED)) ||
2388 adapter->state == __IAVF_RESETTING)
2389 goto freedom;
2390
2391 /* check for error indications */
2392 val = rd32(hw, hw->aq.arq.len);
2393 if (val == 0xdeadbeef || val == 0xffffffff) /* device in reset */
2394 goto freedom;
2395 oldval = val;
2396 if (val & IAVF_VF_ARQLEN1_ARQVFE_MASK) {
2397 dev_info(&adapter->pdev->dev, "ARQ VF Error detected\n");
2398 val &= ~IAVF_VF_ARQLEN1_ARQVFE_MASK;
2399 }
2400 if (val & IAVF_VF_ARQLEN1_ARQOVFL_MASK) {
2401 dev_info(&adapter->pdev->dev, "ARQ Overflow Error detected\n");
2402 val &= ~IAVF_VF_ARQLEN1_ARQOVFL_MASK;
2403 }
2404 if (val & IAVF_VF_ARQLEN1_ARQCRIT_MASK) {
2405 dev_info(&adapter->pdev->dev, "ARQ Critical Error detected\n");
2406 val &= ~IAVF_VF_ARQLEN1_ARQCRIT_MASK;
2407 }
2408 if (oldval != val)
2409 wr32(hw, hw->aq.arq.len, val);
2410
2411 val = rd32(hw, hw->aq.asq.len);
2412 oldval = val;
2413 if (val & IAVF_VF_ATQLEN1_ATQVFE_MASK) {
2414 dev_info(&adapter->pdev->dev, "ASQ VF Error detected\n");
2415 val &= ~IAVF_VF_ATQLEN1_ATQVFE_MASK;
2416 }
2417 if (val & IAVF_VF_ATQLEN1_ATQOVFL_MASK) {
2418 dev_info(&adapter->pdev->dev, "ASQ Overflow Error detected\n");
2419 val &= ~IAVF_VF_ATQLEN1_ATQOVFL_MASK;
2420 }
2421 if (val & IAVF_VF_ATQLEN1_ATQCRIT_MASK) {
2422 dev_info(&adapter->pdev->dev, "ASQ Critical Error detected\n");
2423 val &= ~IAVF_VF_ATQLEN1_ATQCRIT_MASK;
2424 }
2425 if (oldval != val)
2426 wr32(hw, hw->aq.asq.len, val);
2427
2428 freedom:
2429 kfree(event.msg_buf);
2430 out:
2431 /* re-enable Admin queue interrupt cause */
2432 iavf_misc_irq_enable(adapter);
2433 }
2434
2435 /**
2436 * iavf_client_task - worker thread to perform client work
2437 * @work: pointer to work_struct containing our data
2438 *
2439 * This task handles client interactions. Because client calls can be
2440 * reentrant, we can't handle them in the watchdog.
2441 **/
2442 static void iavf_client_task(struct work_struct *work)
2443 {
2444 struct iavf_adapter *adapter =
2445 container_of(work, struct iavf_adapter, client_task.work);
2446
2447 /* If we can't get the client bit, just give up. We'll be rescheduled
2448 * later.
2449 */
2450
2451 if (!mutex_trylock(&adapter->client_lock))
2452 return;
2453
2454 if (adapter->flags & IAVF_FLAG_SERVICE_CLIENT_REQUESTED) {
2455 iavf_client_subtask(adapter);
2456 adapter->flags &= ~IAVF_FLAG_SERVICE_CLIENT_REQUESTED;
2457 goto out;
2458 }
2459 if (adapter->flags & IAVF_FLAG_CLIENT_NEEDS_L2_PARAMS) {
2460 iavf_notify_client_l2_params(&adapter->vsi);
2461 adapter->flags &= ~IAVF_FLAG_CLIENT_NEEDS_L2_PARAMS;
2462 goto out;
2463 }
2464 if (adapter->flags & IAVF_FLAG_CLIENT_NEEDS_CLOSE) {
2465 iavf_notify_client_close(&adapter->vsi, false);
2466 adapter->flags &= ~IAVF_FLAG_CLIENT_NEEDS_CLOSE;
2467 goto out;
2468 }
2469 if (adapter->flags & IAVF_FLAG_CLIENT_NEEDS_OPEN) {
2470 iavf_notify_client_open(&adapter->vsi);
2471 adapter->flags &= ~IAVF_FLAG_CLIENT_NEEDS_OPEN;
2472 }
2473 out:
2474 mutex_unlock(&adapter->client_lock);
2475 }
2476
2477 /**
2478 * iavf_free_all_tx_resources - Free Tx Resources for All Queues
2479 * @adapter: board private structure
2480 *
2481 * Free all transmit software resources
2482 **/
2483 void iavf_free_all_tx_resources(struct iavf_adapter *adapter)
2484 {
2485 int i;
2486
2487 if (!adapter->tx_rings)
2488 return;
2489
2490 for (i = 0; i < adapter->num_active_queues; i++)
2491 if (adapter->tx_rings[i].desc)
2492 iavf_free_tx_resources(&adapter->tx_rings[i]);
2493 }
2494
2495 /**
2496 * iavf_setup_all_tx_resources - allocate all queues Tx resources
2497 * @adapter: board private structure
2498 *
2499 * If this function returns with an error, then it's possible one or
2500 * more of the rings is populated (while the rest are not). It is the
2501 * callers duty to clean those orphaned rings.
2502 *
2503 * Return 0 on success, negative on failure
2504 **/
2505 static int iavf_setup_all_tx_resources(struct iavf_adapter *adapter)
2506 {
2507 int i, err = 0;
2508
2509 for (i = 0; i < adapter->num_active_queues; i++) {
2510 adapter->tx_rings[i].count = adapter->tx_desc_count;
2511 err = iavf_setup_tx_descriptors(&adapter->tx_rings[i]);
2512 if (!err)
2513 continue;
2514 dev_err(&adapter->pdev->dev,
2515 "Allocation for Tx Queue %u failed\n", i);
2516 break;
2517 }
2518
2519 return err;
2520 }
2521
2522 /**
2523 * iavf_setup_all_rx_resources - allocate all queues Rx resources
2524 * @adapter: board private structure
2525 *
2526 * If this function returns with an error, then it's possible one or
2527 * more of the rings is populated (while the rest are not). It is the
2528 * callers duty to clean those orphaned rings.
2529 *
2530 * Return 0 on success, negative on failure
2531 **/
2532 static int iavf_setup_all_rx_resources(struct iavf_adapter *adapter)
2533 {
2534 int i, err = 0;
2535
2536 for (i = 0; i < adapter->num_active_queues; i++) {
2537 adapter->rx_rings[i].count = adapter->rx_desc_count;
2538 err = iavf_setup_rx_descriptors(&adapter->rx_rings[i]);
2539 if (!err)
2540 continue;
2541 dev_err(&adapter->pdev->dev,
2542 "Allocation for Rx Queue %u failed\n", i);
2543 break;
2544 }
2545 return err;
2546 }
2547
2548 /**
2549 * iavf_free_all_rx_resources - Free Rx Resources for All Queues
2550 * @adapter: board private structure
2551 *
2552 * Free all receive software resources
2553 **/
2554 void iavf_free_all_rx_resources(struct iavf_adapter *adapter)
2555 {
2556 int i;
2557
2558 if (!adapter->rx_rings)
2559 return;
2560
2561 for (i = 0; i < adapter->num_active_queues; i++)
2562 if (adapter->rx_rings[i].desc)
2563 iavf_free_rx_resources(&adapter->rx_rings[i]);
2564 }
2565
2566 /**
2567 * iavf_validate_tx_bandwidth - validate the max Tx bandwidth
2568 * @adapter: board private structure
2569 * @max_tx_rate: max Tx bw for a tc
2570 **/
2571 static int iavf_validate_tx_bandwidth(struct iavf_adapter *adapter,
2572 u64 max_tx_rate)
2573 {
2574 int speed = 0, ret = 0;
2575
2576 if (ADV_LINK_SUPPORT(adapter)) {
2577 if (adapter->link_speed_mbps < U32_MAX) {
2578 speed = adapter->link_speed_mbps;
2579 goto validate_bw;
2580 } else {
2581 dev_err(&adapter->pdev->dev, "Unknown link speed\n");
2582 return -EINVAL;
2583 }
2584 }
2585
2586 switch (adapter->link_speed) {
2587 case VIRTCHNL_LINK_SPEED_40GB:
2588 speed = SPEED_40000;
2589 break;
2590 case VIRTCHNL_LINK_SPEED_25GB:
2591 speed = SPEED_25000;
2592 break;
2593 case VIRTCHNL_LINK_SPEED_20GB:
2594 speed = SPEED_20000;
2595 break;
2596 case VIRTCHNL_LINK_SPEED_10GB:
2597 speed = SPEED_10000;
2598 break;
2599 case VIRTCHNL_LINK_SPEED_5GB:
2600 speed = SPEED_5000;
2601 break;
2602 case VIRTCHNL_LINK_SPEED_2_5GB:
2603 speed = SPEED_2500;
2604 break;
2605 case VIRTCHNL_LINK_SPEED_1GB:
2606 speed = SPEED_1000;
2607 break;
2608 case VIRTCHNL_LINK_SPEED_100MB:
2609 speed = SPEED_100;
2610 break;
2611 default:
2612 break;
2613 }
2614
2615 validate_bw:
2616 if (max_tx_rate > speed) {
2617 dev_err(&adapter->pdev->dev,
2618 "Invalid tx rate specified\n");
2619 ret = -EINVAL;
2620 }
2621
2622 return ret;
2623 }
2624
2625 /**
2626 * iavf_validate_ch_config - validate queue mapping info
2627 * @adapter: board private structure
2628 * @mqprio_qopt: queue parameters
2629 *
2630 * This function validates if the config provided by the user to
2631 * configure queue channels is valid or not. Returns 0 on a valid
2632 * config.
2633 **/
2634 static int iavf_validate_ch_config(struct iavf_adapter *adapter,
2635 struct tc_mqprio_qopt_offload *mqprio_qopt)
2636 {
2637 u64 total_max_rate = 0;
2638 int i, num_qps = 0;
2639 u64 tx_rate = 0;
2640 int ret = 0;
2641
2642 if (mqprio_qopt->qopt.num_tc > IAVF_MAX_TRAFFIC_CLASS ||
2643 mqprio_qopt->qopt.num_tc < 1)
2644 return -EINVAL;
2645
2646 for (i = 0; i <= mqprio_qopt->qopt.num_tc - 1; i++) {
2647 if (!mqprio_qopt->qopt.count[i] ||
2648 mqprio_qopt->qopt.offset[i] != num_qps)
2649 return -EINVAL;
2650 if (mqprio_qopt->min_rate[i]) {
2651 dev_err(&adapter->pdev->dev,
2652 "Invalid min tx rate (greater than 0) specified\n");
2653 return -EINVAL;
2654 }
2655 /*convert to Mbps */
2656 tx_rate = div_u64(mqprio_qopt->max_rate[i],
2657 IAVF_MBPS_DIVISOR);
2658 total_max_rate += tx_rate;
2659 num_qps += mqprio_qopt->qopt.count[i];
2660 }
2661 if (num_qps > adapter->num_active_queues) {
2662 dev_err(&adapter->pdev->dev,
2663 "Cannot support requested number of queues\n");
2664 return -EINVAL;
2665 }
2666
2667 ret = iavf_validate_tx_bandwidth(adapter, total_max_rate);
2668 return ret;
2669 }
2670
2671 /**
2672 * iavf_del_all_cloud_filters - delete all cloud filters on the traffic classes
2673 * @adapter: board private structure
2674 **/
2675 static void iavf_del_all_cloud_filters(struct iavf_adapter *adapter)
2676 {
2677 struct iavf_cloud_filter *cf, *cftmp;
2678
2679 spin_lock_bh(&adapter->cloud_filter_list_lock);
2680 list_for_each_entry_safe(cf, cftmp, &adapter->cloud_filter_list,
2681 list) {
2682 list_del(&cf->list);
2683 kfree(cf);
2684 adapter->num_cloud_filters--;
2685 }
2686 spin_unlock_bh(&adapter->cloud_filter_list_lock);
2687 }
2688
2689 /**
2690 * __iavf_setup_tc - configure multiple traffic classes
2691 * @netdev: network interface device structure
2692 * @type_data: tc offload data
2693 *
2694 * This function processes the config information provided by the
2695 * user to configure traffic classes/queue channels and packages the
2696 * information to request the PF to setup traffic classes.
2697 *
2698 * Returns 0 on success.
2699 **/
2700 static int __iavf_setup_tc(struct net_device *netdev, void *type_data)
2701 {
2702 struct tc_mqprio_qopt_offload *mqprio_qopt = type_data;
2703 struct iavf_adapter *adapter = netdev_priv(netdev);
2704 struct virtchnl_vf_resource *vfres = adapter->vf_res;
2705 u8 num_tc = 0, total_qps = 0;
2706 int ret = 0, netdev_tc = 0;
2707 u64 max_tx_rate;
2708 u16 mode;
2709 int i;
2710
2711 num_tc = mqprio_qopt->qopt.num_tc;
2712 mode = mqprio_qopt->mode;
2713
2714 /* delete queue_channel */
2715 if (!mqprio_qopt->qopt.hw) {
2716 if (adapter->ch_config.state == __IAVF_TC_RUNNING) {
2717 /* reset the tc configuration */
2718 netdev_reset_tc(netdev);
2719 adapter->num_tc = 0;
2720 netif_tx_stop_all_queues(netdev);
2721 netif_tx_disable(netdev);
2722 iavf_del_all_cloud_filters(adapter);
2723 adapter->aq_required = IAVF_FLAG_AQ_DISABLE_CHANNELS;
2724 goto exit;
2725 } else {
2726 return -EINVAL;
2727 }
2728 }
2729
2730 /* add queue channel */
2731 if (mode == TC_MQPRIO_MODE_CHANNEL) {
2732 if (!(vfres->vf_cap_flags & VIRTCHNL_VF_OFFLOAD_ADQ)) {
2733 dev_err(&adapter->pdev->dev, "ADq not supported\n");
2734 return -EOPNOTSUPP;
2735 }
2736 if (adapter->ch_config.state != __IAVF_TC_INVALID) {
2737 dev_err(&adapter->pdev->dev, "TC configuration already exists\n");
2738 return -EINVAL;
2739 }
2740
2741 ret = iavf_validate_ch_config(adapter, mqprio_qopt);
2742 if (ret)
2743 return ret;
2744 /* Return if same TC config is requested */
2745 if (adapter->num_tc == num_tc)
2746 return 0;
2747 adapter->num_tc = num_tc;
2748
2749 for (i = 0; i < IAVF_MAX_TRAFFIC_CLASS; i++) {
2750 if (i < num_tc) {
2751 adapter->ch_config.ch_info[i].count =
2752 mqprio_qopt->qopt.count[i];
2753 adapter->ch_config.ch_info[i].offset =
2754 mqprio_qopt->qopt.offset[i];
2755 total_qps += mqprio_qopt->qopt.count[i];
2756 max_tx_rate = mqprio_qopt->max_rate[i];
2757 /* convert to Mbps */
2758 max_tx_rate = div_u64(max_tx_rate,
2759 IAVF_MBPS_DIVISOR);
2760 adapter->ch_config.ch_info[i].max_tx_rate =
2761 max_tx_rate;
2762 } else {
2763 adapter->ch_config.ch_info[i].count = 1;
2764 adapter->ch_config.ch_info[i].offset = 0;
2765 }
2766 }
2767 adapter->ch_config.total_qps = total_qps;
2768 netif_tx_stop_all_queues(netdev);
2769 netif_tx_disable(netdev);
2770 adapter->aq_required |= IAVF_FLAG_AQ_ENABLE_CHANNELS;
2771 netdev_reset_tc(netdev);
2772 /* Report the tc mapping up the stack */
2773 netdev_set_num_tc(adapter->netdev, num_tc);
2774 for (i = 0; i < IAVF_MAX_TRAFFIC_CLASS; i++) {
2775 u16 qcount = mqprio_qopt->qopt.count[i];
2776 u16 qoffset = mqprio_qopt->qopt.offset[i];
2777
2778 if (i < num_tc)
2779 netdev_set_tc_queue(netdev, netdev_tc++, qcount,
2780 qoffset);
2781 }
2782 }
2783 exit:
2784 return ret;
2785 }
2786
2787 /**
2788 * iavf_parse_cls_flower - Parse tc flower filters provided by kernel
2789 * @adapter: board private structure
2790 * @f: pointer to struct flow_cls_offload
2791 * @filter: pointer to cloud filter structure
2792 */
2793 static int iavf_parse_cls_flower(struct iavf_adapter *adapter,
2794 struct flow_cls_offload *f,
2795 struct iavf_cloud_filter *filter)
2796 {
2797 struct flow_rule *rule = flow_cls_offload_flow_rule(f);
2798 struct flow_dissector *dissector = rule->match.dissector;
2799 u16 n_proto_mask = 0;
2800 u16 n_proto_key = 0;
2801 u8 field_flags = 0;
2802 u16 addr_type = 0;
2803 u16 n_proto = 0;
2804 int i = 0;
2805 struct virtchnl_filter *vf = &filter->f;
2806
2807 if (dissector->used_keys &
2808 ~(BIT(FLOW_DISSECTOR_KEY_CONTROL) |
2809 BIT(FLOW_DISSECTOR_KEY_BASIC) |
2810 BIT(FLOW_DISSECTOR_KEY_ETH_ADDRS) |
2811 BIT(FLOW_DISSECTOR_KEY_VLAN) |
2812 BIT(FLOW_DISSECTOR_KEY_IPV4_ADDRS) |
2813 BIT(FLOW_DISSECTOR_KEY_IPV6_ADDRS) |
2814 BIT(FLOW_DISSECTOR_KEY_PORTS) |
2815 BIT(FLOW_DISSECTOR_KEY_ENC_KEYID))) {
2816 dev_err(&adapter->pdev->dev, "Unsupported key used: 0x%x\n",
2817 dissector->used_keys);
2818 return -EOPNOTSUPP;
2819 }
2820
2821 if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_ENC_KEYID)) {
2822 struct flow_match_enc_keyid match;
2823
2824 flow_rule_match_enc_keyid(rule, &match);
2825 if (match.mask->keyid != 0)
2826 field_flags |= IAVF_CLOUD_FIELD_TEN_ID;
2827 }
2828
2829 if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_BASIC)) {
2830 struct flow_match_basic match;
2831
2832 flow_rule_match_basic(rule, &match);
2833 n_proto_key = ntohs(match.key->n_proto);
2834 n_proto_mask = ntohs(match.mask->n_proto);
2835
2836 if (n_proto_key == ETH_P_ALL) {
2837 n_proto_key = 0;
2838 n_proto_mask = 0;
2839 }
2840 n_proto = n_proto_key & n_proto_mask;
2841 if (n_proto != ETH_P_IP && n_proto != ETH_P_IPV6)
2842 return -EINVAL;
2843 if (n_proto == ETH_P_IPV6) {
2844 /* specify flow type as TCP IPv6 */
2845 vf->flow_type = VIRTCHNL_TCP_V6_FLOW;
2846 }
2847
2848 if (match.key->ip_proto != IPPROTO_TCP) {
2849 dev_info(&adapter->pdev->dev, "Only TCP transport is supported\n");
2850 return -EINVAL;
2851 }
2852 }
2853
2854 if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_ETH_ADDRS)) {
2855 struct flow_match_eth_addrs match;
2856
2857 flow_rule_match_eth_addrs(rule, &match);
2858
2859 /* use is_broadcast and is_zero to check for all 0xf or 0 */
2860 if (!is_zero_ether_addr(match.mask->dst)) {
2861 if (is_broadcast_ether_addr(match.mask->dst)) {
2862 field_flags |= IAVF_CLOUD_FIELD_OMAC;
2863 } else {
2864 dev_err(&adapter->pdev->dev, "Bad ether dest mask %pM\n",
2865 match.mask->dst);
2866 return IAVF_ERR_CONFIG;
2867 }
2868 }
2869
2870 if (!is_zero_ether_addr(match.mask->src)) {
2871 if (is_broadcast_ether_addr(match.mask->src)) {
2872 field_flags |= IAVF_CLOUD_FIELD_IMAC;
2873 } else {
2874 dev_err(&adapter->pdev->dev, "Bad ether src mask %pM\n",
2875 match.mask->src);
2876 return IAVF_ERR_CONFIG;
2877 }
2878 }
2879
2880 if (!is_zero_ether_addr(match.key->dst))
2881 if (is_valid_ether_addr(match.key->dst) ||
2882 is_multicast_ether_addr(match.key->dst)) {
2883 /* set the mask if a valid dst_mac address */
2884 for (i = 0; i < ETH_ALEN; i++)
2885 vf->mask.tcp_spec.dst_mac[i] |= 0xff;
2886 ether_addr_copy(vf->data.tcp_spec.dst_mac,
2887 match.key->dst);
2888 }
2889
2890 if (!is_zero_ether_addr(match.key->src))
2891 if (is_valid_ether_addr(match.key->src) ||
2892 is_multicast_ether_addr(match.key->src)) {
2893 /* set the mask if a valid dst_mac address */
2894 for (i = 0; i < ETH_ALEN; i++)
2895 vf->mask.tcp_spec.src_mac[i] |= 0xff;
2896 ether_addr_copy(vf->data.tcp_spec.src_mac,
2897 match.key->src);
2898 }
2899 }
2900
2901 if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_VLAN)) {
2902 struct flow_match_vlan match;
2903
2904 flow_rule_match_vlan(rule, &match);
2905 if (match.mask->vlan_id) {
2906 if (match.mask->vlan_id == VLAN_VID_MASK) {
2907 field_flags |= IAVF_CLOUD_FIELD_IVLAN;
2908 } else {
2909 dev_err(&adapter->pdev->dev, "Bad vlan mask %u\n",
2910 match.mask->vlan_id);
2911 return IAVF_ERR_CONFIG;
2912 }
2913 }
2914 vf->mask.tcp_spec.vlan_id |= cpu_to_be16(0xffff);
2915 vf->data.tcp_spec.vlan_id = cpu_to_be16(match.key->vlan_id);
2916 }
2917
2918 if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_CONTROL)) {
2919 struct flow_match_control match;
2920
2921 flow_rule_match_control(rule, &match);
2922 addr_type = match.key->addr_type;
2923 }
2924
2925 if (addr_type == FLOW_DISSECTOR_KEY_IPV4_ADDRS) {
2926 struct flow_match_ipv4_addrs match;
2927
2928 flow_rule_match_ipv4_addrs(rule, &match);
2929 if (match.mask->dst) {
2930 if (match.mask->dst == cpu_to_be32(0xffffffff)) {
2931 field_flags |= IAVF_CLOUD_FIELD_IIP;
2932 } else {
2933 dev_err(&adapter->pdev->dev, "Bad ip dst mask 0x%08x\n",
2934 be32_to_cpu(match.mask->dst));
2935 return IAVF_ERR_CONFIG;
2936 }
2937 }
2938
2939 if (match.mask->src) {
2940 if (match.mask->src == cpu_to_be32(0xffffffff)) {
2941 field_flags |= IAVF_CLOUD_FIELD_IIP;
2942 } else {
2943 dev_err(&adapter->pdev->dev, "Bad ip src mask 0x%08x\n",
2944 be32_to_cpu(match.mask->dst));
2945 return IAVF_ERR_CONFIG;
2946 }
2947 }
2948
2949 if (field_flags & IAVF_CLOUD_FIELD_TEN_ID) {
2950 dev_info(&adapter->pdev->dev, "Tenant id not allowed for ip filter\n");
2951 return IAVF_ERR_CONFIG;
2952 }
2953 if (match.key->dst) {
2954 vf->mask.tcp_spec.dst_ip[0] |= cpu_to_be32(0xffffffff);
2955 vf->data.tcp_spec.dst_ip[0] = match.key->dst;
2956 }
2957 if (match.key->src) {
2958 vf->mask.tcp_spec.src_ip[0] |= cpu_to_be32(0xffffffff);
2959 vf->data.tcp_spec.src_ip[0] = match.key->src;
2960 }
2961 }
2962
2963 if (addr_type == FLOW_DISSECTOR_KEY_IPV6_ADDRS) {
2964 struct flow_match_ipv6_addrs match;
2965
2966 flow_rule_match_ipv6_addrs(rule, &match);
2967
2968 /* validate mask, make sure it is not IPV6_ADDR_ANY */
2969 if (ipv6_addr_any(&match.mask->dst)) {
2970 dev_err(&adapter->pdev->dev, "Bad ipv6 dst mask 0x%02x\n",
2971 IPV6_ADDR_ANY);
2972 return IAVF_ERR_CONFIG;
2973 }
2974
2975 /* src and dest IPv6 address should not be LOOPBACK
2976 * (0:0:0:0:0:0:0:1) which can be represented as ::1
2977 */
2978 if (ipv6_addr_loopback(&match.key->dst) ||
2979 ipv6_addr_loopback(&match.key->src)) {
2980 dev_err(&adapter->pdev->dev,
2981 "ipv6 addr should not be loopback\n");
2982 return IAVF_ERR_CONFIG;
2983 }
2984 if (!ipv6_addr_any(&match.mask->dst) ||
2985 !ipv6_addr_any(&match.mask->src))
2986 field_flags |= IAVF_CLOUD_FIELD_IIP;
2987
2988 for (i = 0; i < 4; i++)
2989 vf->mask.tcp_spec.dst_ip[i] |= cpu_to_be32(0xffffffff);
2990 memcpy(&vf->data.tcp_spec.dst_ip, &match.key->dst.s6_addr32,
2991 sizeof(vf->data.tcp_spec.dst_ip));
2992 for (i = 0; i < 4; i++)
2993 vf->mask.tcp_spec.src_ip[i] |= cpu_to_be32(0xffffffff);
2994 memcpy(&vf->data.tcp_spec.src_ip, &match.key->src.s6_addr32,
2995 sizeof(vf->data.tcp_spec.src_ip));
2996 }
2997 if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_PORTS)) {
2998 struct flow_match_ports match;
2999
3000 flow_rule_match_ports(rule, &match);
3001 if (match.mask->src) {
3002 if (match.mask->src == cpu_to_be16(0xffff)) {
3003 field_flags |= IAVF_CLOUD_FIELD_IIP;
3004 } else {
3005 dev_err(&adapter->pdev->dev, "Bad src port mask %u\n",
3006 be16_to_cpu(match.mask->src));
3007 return IAVF_ERR_CONFIG;
3008 }
3009 }
3010
3011 if (match.mask->dst) {
3012 if (match.mask->dst == cpu_to_be16(0xffff)) {
3013 field_flags |= IAVF_CLOUD_FIELD_IIP;
3014 } else {
3015 dev_err(&adapter->pdev->dev, "Bad dst port mask %u\n",
3016 be16_to_cpu(match.mask->dst));
3017 return IAVF_ERR_CONFIG;
3018 }
3019 }
3020 if (match.key->dst) {
3021 vf->mask.tcp_spec.dst_port |= cpu_to_be16(0xffff);
3022 vf->data.tcp_spec.dst_port = match.key->dst;
3023 }
3024
3025 if (match.key->src) {
3026 vf->mask.tcp_spec.src_port |= cpu_to_be16(0xffff);
3027 vf->data.tcp_spec.src_port = match.key->src;
3028 }
3029 }
3030 vf->field_flags = field_flags;
3031
3032 return 0;
3033 }
3034
3035 /**
3036 * iavf_handle_tclass - Forward to a traffic class on the device
3037 * @adapter: board private structure
3038 * @tc: traffic class index on the device
3039 * @filter: pointer to cloud filter structure
3040 */
3041 static int iavf_handle_tclass(struct iavf_adapter *adapter, u32 tc,
3042 struct iavf_cloud_filter *filter)
3043 {
3044 if (tc == 0)
3045 return 0;
3046 if (tc < adapter->num_tc) {
3047 if (!filter->f.data.tcp_spec.dst_port) {
3048 dev_err(&adapter->pdev->dev,
3049 "Specify destination port to redirect to traffic class other than TC0\n");
3050 return -EINVAL;
3051 }
3052 }
3053 /* redirect to a traffic class on the same device */
3054 filter->f.action = VIRTCHNL_ACTION_TC_REDIRECT;
3055 filter->f.action_meta = tc;
3056 return 0;
3057 }
3058
3059 /**
3060 * iavf_configure_clsflower - Add tc flower filters
3061 * @adapter: board private structure
3062 * @cls_flower: Pointer to struct flow_cls_offload
3063 */
3064 static int iavf_configure_clsflower(struct iavf_adapter *adapter,
3065 struct flow_cls_offload *cls_flower)
3066 {
3067 int tc = tc_classid_to_hwtc(adapter->netdev, cls_flower->classid);
3068 struct iavf_cloud_filter *filter = NULL;
3069 int err = -EINVAL, count = 50;
3070
3071 if (tc < 0) {
3072 dev_err(&adapter->pdev->dev, "Invalid traffic class\n");
3073 return -EINVAL;
3074 }
3075
3076 filter = kzalloc(sizeof(*filter), GFP_KERNEL);
3077 if (!filter)
3078 return -ENOMEM;
3079
3080 while (!mutex_trylock(&adapter->crit_lock)) {
3081 if (--count == 0) {
3082 kfree(filter);
3083 return err;
3084 }
3085 udelay(1);
3086 }
3087
3088 filter->cookie = cls_flower->cookie;
3089
3090 /* set the mask to all zeroes to begin with */
3091 memset(&filter->f.mask.tcp_spec, 0, sizeof(struct virtchnl_l4_spec));
3092 /* start out with flow type and eth type IPv4 to begin with */
3093 filter->f.flow_type = VIRTCHNL_TCP_V4_FLOW;
3094 err = iavf_parse_cls_flower(adapter, cls_flower, filter);
3095 if (err)
3096 goto err;
3097
3098 err = iavf_handle_tclass(adapter, tc, filter);
3099 if (err)
3100 goto err;
3101
3102 /* add filter to the list */
3103 spin_lock_bh(&adapter->cloud_filter_list_lock);
3104 list_add_tail(&filter->list, &adapter->cloud_filter_list);
3105 adapter->num_cloud_filters++;
3106 filter->add = true;
3107 adapter->aq_required |= IAVF_FLAG_AQ_ADD_CLOUD_FILTER;
3108 spin_unlock_bh(&adapter->cloud_filter_list_lock);
3109 err:
3110 if (err)
3111 kfree(filter);
3112
3113 mutex_unlock(&adapter->crit_lock);
3114 return err;
3115 }
3116
3117 /* iavf_find_cf - Find the cloud filter in the list
3118 * @adapter: Board private structure
3119 * @cookie: filter specific cookie
3120 *
3121 * Returns ptr to the filter object or NULL. Must be called while holding the
3122 * cloud_filter_list_lock.
3123 */
3124 static struct iavf_cloud_filter *iavf_find_cf(struct iavf_adapter *adapter,
3125 unsigned long *cookie)
3126 {
3127 struct iavf_cloud_filter *filter = NULL;
3128
3129 if (!cookie)
3130 return NULL;
3131
3132 list_for_each_entry(filter, &adapter->cloud_filter_list, list) {
3133 if (!memcmp(cookie, &filter->cookie, sizeof(filter->cookie)))
3134 return filter;
3135 }
3136 return NULL;
3137 }
3138
3139 /**
3140 * iavf_delete_clsflower - Remove tc flower filters
3141 * @adapter: board private structure
3142 * @cls_flower: Pointer to struct flow_cls_offload
3143 */
3144 static int iavf_delete_clsflower(struct iavf_adapter *adapter,
3145 struct flow_cls_offload *cls_flower)
3146 {
3147 struct iavf_cloud_filter *filter = NULL;
3148 int err = 0;
3149
3150 spin_lock_bh(&adapter->cloud_filter_list_lock);
3151 filter = iavf_find_cf(adapter, &cls_flower->cookie);
3152 if (filter) {
3153 filter->del = true;
3154 adapter->aq_required |= IAVF_FLAG_AQ_DEL_CLOUD_FILTER;
3155 } else {
3156 err = -EINVAL;
3157 }
3158 spin_unlock_bh(&adapter->cloud_filter_list_lock);
3159
3160 return err;
3161 }
3162
3163 /**
3164 * iavf_setup_tc_cls_flower - flower classifier offloads
3165 * @adapter: board private structure
3166 * @cls_flower: pointer to flow_cls_offload struct with flow info
3167 */
3168 static int iavf_setup_tc_cls_flower(struct iavf_adapter *adapter,
3169 struct flow_cls_offload *cls_flower)
3170 {
3171 switch (cls_flower->command) {
3172 case FLOW_CLS_REPLACE:
3173 return iavf_configure_clsflower(adapter, cls_flower);
3174 case FLOW_CLS_DESTROY:
3175 return iavf_delete_clsflower(adapter, cls_flower);
3176 case FLOW_CLS_STATS:
3177 return -EOPNOTSUPP;
3178 default:
3179 return -EOPNOTSUPP;
3180 }
3181 }
3182
3183 /**
3184 * iavf_setup_tc_block_cb - block callback for tc
3185 * @type: type of offload
3186 * @type_data: offload data
3187 * @cb_priv:
3188 *
3189 * This function is the block callback for traffic classes
3190 **/
3191 static int iavf_setup_tc_block_cb(enum tc_setup_type type, void *type_data,
3192 void *cb_priv)
3193 {
3194 struct iavf_adapter *adapter = cb_priv;
3195
3196 if (!tc_cls_can_offload_and_chain0(adapter->netdev, type_data))
3197 return -EOPNOTSUPP;
3198
3199 switch (type) {
3200 case TC_SETUP_CLSFLOWER:
3201 return iavf_setup_tc_cls_flower(cb_priv, type_data);
3202 default:
3203 return -EOPNOTSUPP;
3204 }
3205 }
3206
3207 static LIST_HEAD(iavf_block_cb_list);
3208
3209 /**
3210 * iavf_setup_tc - configure multiple traffic classes
3211 * @netdev: network interface device structure
3212 * @type: type of offload
3213 * @type_data: tc offload data
3214 *
3215 * This function is the callback to ndo_setup_tc in the
3216 * netdev_ops.
3217 *
3218 * Returns 0 on success
3219 **/
3220 static int iavf_setup_tc(struct net_device *netdev, enum tc_setup_type type,
3221 void *type_data)
3222 {
3223 struct iavf_adapter *adapter = netdev_priv(netdev);
3224
3225 switch (type) {
3226 case TC_SETUP_QDISC_MQPRIO:
3227 return __iavf_setup_tc(netdev, type_data);
3228 case TC_SETUP_BLOCK:
3229 return flow_block_cb_setup_simple(type_data,
3230 &iavf_block_cb_list,
3231 iavf_setup_tc_block_cb,
3232 adapter, adapter, true);
3233 default:
3234 return -EOPNOTSUPP;
3235 }
3236 }
3237
3238 /**
3239 * iavf_open - Called when a network interface is made active
3240 * @netdev: network interface device structure
3241 *
3242 * Returns 0 on success, negative value on failure
3243 *
3244 * The open entry point is called when a network interface is made
3245 * active by the system (IFF_UP). At this point all resources needed
3246 * for transmit and receive operations are allocated, the interrupt
3247 * handler is registered with the OS, the watchdog is started,
3248 * and the stack is notified that the interface is ready.
3249 **/
3250 static int iavf_open(struct net_device *netdev)
3251 {
3252 struct iavf_adapter *adapter = netdev_priv(netdev);
3253 int err;
3254
3255 if (adapter->flags & IAVF_FLAG_PF_COMMS_FAILED) {
3256 dev_err(&adapter->pdev->dev, "Unable to open device due to PF driver failure.\n");
3257 return -EIO;
3258 }
3259
3260 while (!mutex_trylock(&adapter->crit_lock))
3261 usleep_range(500, 1000);
3262
3263 if (adapter->state != __IAVF_DOWN) {
3264 err = -EBUSY;
3265 goto err_unlock;
3266 }
3267
3268 /* allocate transmit descriptors */
3269 err = iavf_setup_all_tx_resources(adapter);
3270 if (err)
3271 goto err_setup_tx;
3272
3273 /* allocate receive descriptors */
3274 err = iavf_setup_all_rx_resources(adapter);
3275 if (err)
3276 goto err_setup_rx;
3277
3278 /* clear any pending interrupts, may auto mask */
3279 err = iavf_request_traffic_irqs(adapter, netdev->name);
3280 if (err)
3281 goto err_req_irq;
3282
3283 spin_lock_bh(&adapter->mac_vlan_list_lock);
3284
3285 iavf_add_filter(adapter, adapter->hw.mac.addr);
3286
3287 spin_unlock_bh(&adapter->mac_vlan_list_lock);
3288
3289 /* Restore VLAN filters that were removed with IFF_DOWN */
3290 iavf_restore_filters(adapter);
3291
3292 iavf_configure(adapter);
3293
3294 iavf_up_complete(adapter);
3295
3296 iavf_irq_enable(adapter, true);
3297
3298 mutex_unlock(&adapter->crit_lock);
3299
3300 return 0;
3301
3302 err_req_irq:
3303 iavf_down(adapter);
3304 iavf_free_traffic_irqs(adapter);
3305 err_setup_rx:
3306 iavf_free_all_rx_resources(adapter);
3307 err_setup_tx:
3308 iavf_free_all_tx_resources(adapter);
3309 err_unlock:
3310 mutex_unlock(&adapter->crit_lock);
3311
3312 return err;
3313 }
3314
3315 /**
3316 * iavf_close - Disables a network interface
3317 * @netdev: network interface device structure
3318 *
3319 * Returns 0, this is not allowed to fail
3320 *
3321 * The close entry point is called when an interface is de-activated
3322 * by the OS. The hardware is still under the drivers control, but
3323 * needs to be disabled. All IRQs except vector 0 (reserved for admin queue)
3324 * are freed, along with all transmit and receive resources.
3325 **/
3326 static int iavf_close(struct net_device *netdev)
3327 {
3328 struct iavf_adapter *adapter = netdev_priv(netdev);
3329 int status;
3330
3331 if (adapter->state <= __IAVF_DOWN_PENDING)
3332 return 0;
3333
3334 while (!mutex_trylock(&adapter->crit_lock))
3335 usleep_range(500, 1000);
3336
3337 set_bit(__IAVF_VSI_DOWN, adapter->vsi.state);
3338 if (CLIENT_ENABLED(adapter))
3339 adapter->flags |= IAVF_FLAG_CLIENT_NEEDS_CLOSE;
3340
3341 iavf_down(adapter);
3342 iavf_change_state(adapter, __IAVF_DOWN_PENDING);
3343 iavf_free_traffic_irqs(adapter);
3344
3345 mutex_unlock(&adapter->crit_lock);
3346
3347 /* We explicitly don't free resources here because the hardware is
3348 * still active and can DMA into memory. Resources are cleared in
3349 * iavf_virtchnl_completion() after we get confirmation from the PF
3350 * driver that the rings have been stopped.
3351 *
3352 * Also, we wait for state to transition to __IAVF_DOWN before
3353 * returning. State change occurs in iavf_virtchnl_completion() after
3354 * VF resources are released (which occurs after PF driver processes and
3355 * responds to admin queue commands).
3356 */
3357
3358 status = wait_event_timeout(adapter->down_waitqueue,
3359 adapter->state == __IAVF_DOWN,
3360 msecs_to_jiffies(500));
3361 if (!status)
3362 netdev_warn(netdev, "Device resources not yet released\n");
3363 return 0;
3364 }
3365
3366 /**
3367 * iavf_change_mtu - Change the Maximum Transfer Unit
3368 * @netdev: network interface device structure
3369 * @new_mtu: new value for maximum frame size
3370 *
3371 * Returns 0 on success, negative on failure
3372 **/
3373 static int iavf_change_mtu(struct net_device *netdev, int new_mtu)
3374 {
3375 struct iavf_adapter *adapter = netdev_priv(netdev);
3376
3377 netdev->mtu = new_mtu;
3378 if (CLIENT_ENABLED(adapter)) {
3379 iavf_notify_client_l2_params(&adapter->vsi);
3380 adapter->flags |= IAVF_FLAG_SERVICE_CLIENT_REQUESTED;
3381 }
3382
3383 if (netif_running(netdev)) {
3384 adapter->flags |= IAVF_FLAG_RESET_NEEDED;
3385 queue_work(iavf_wq, &adapter->reset_task);
3386 }
3387
3388 return 0;
3389 }
3390
3391 /**
3392 * iavf_set_features - set the netdev feature flags
3393 * @netdev: ptr to the netdev being adjusted
3394 * @features: the feature set that the stack is suggesting
3395 * Note: expects to be called while under rtnl_lock()
3396 **/
3397 static int iavf_set_features(struct net_device *netdev,
3398 netdev_features_t features)
3399 {
3400 struct iavf_adapter *adapter = netdev_priv(netdev);
3401
3402 /* Don't allow enabling VLAN features when adapter is not capable
3403 * of VLAN offload/filtering
3404 */
3405 if (!VLAN_ALLOWED(adapter)) {
3406 netdev->hw_features &= ~(NETIF_F_HW_VLAN_CTAG_RX |
3407 NETIF_F_HW_VLAN_CTAG_TX |
3408 NETIF_F_HW_VLAN_CTAG_FILTER);
3409 if (features & (NETIF_F_HW_VLAN_CTAG_RX |
3410 NETIF_F_HW_VLAN_CTAG_TX |
3411 NETIF_F_HW_VLAN_CTAG_FILTER))
3412 return -EINVAL;
3413 } else if ((netdev->features ^ features) & NETIF_F_HW_VLAN_CTAG_RX) {
3414 if (features & NETIF_F_HW_VLAN_CTAG_RX)
3415 adapter->aq_required |=
3416 IAVF_FLAG_AQ_ENABLE_VLAN_STRIPPING;
3417 else
3418 adapter->aq_required |=
3419 IAVF_FLAG_AQ_DISABLE_VLAN_STRIPPING;
3420 }
3421
3422 return 0;
3423 }
3424
3425 /**
3426 * iavf_features_check - Validate encapsulated packet conforms to limits
3427 * @skb: skb buff
3428 * @dev: This physical port's netdev
3429 * @features: Offload features that the stack believes apply
3430 **/
3431 static netdev_features_t iavf_features_check(struct sk_buff *skb,
3432 struct net_device *dev,
3433 netdev_features_t features)
3434 {
3435 size_t len;
3436
3437 /* No point in doing any of this if neither checksum nor GSO are
3438 * being requested for this frame. We can rule out both by just
3439 * checking for CHECKSUM_PARTIAL
3440 */
3441 if (skb->ip_summed != CHECKSUM_PARTIAL)
3442 return features;
3443
3444 /* We cannot support GSO if the MSS is going to be less than
3445 * 64 bytes. If it is then we need to drop support for GSO.
3446 */
3447 if (skb_is_gso(skb) && (skb_shinfo(skb)->gso_size < 64))
3448 features &= ~NETIF_F_GSO_MASK;
3449
3450 /* MACLEN can support at most 63 words */
3451 len = skb_network_header(skb) - skb->data;
3452 if (len & ~(63 * 2))
3453 goto out_err;
3454
3455 /* IPLEN and EIPLEN can support at most 127 dwords */
3456 len = skb_transport_header(skb) - skb_network_header(skb);
3457 if (len & ~(127 * 4))
3458 goto out_err;
3459
3460 if (skb->encapsulation) {
3461 /* L4TUNLEN can support 127 words */
3462 len = skb_inner_network_header(skb) - skb_transport_header(skb);
3463 if (len & ~(127 * 2))
3464 goto out_err;
3465
3466 /* IPLEN can support at most 127 dwords */
3467 len = skb_inner_transport_header(skb) -
3468 skb_inner_network_header(skb);
3469 if (len & ~(127 * 4))
3470 goto out_err;
3471 }
3472
3473 /* No need to validate L4LEN as TCP is the only protocol with a
3474 * a flexible value and we support all possible values supported
3475 * by TCP, which is at most 15 dwords
3476 */
3477
3478 return features;
3479 out_err:
3480 return features & ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK);
3481 }
3482
3483 /**
3484 * iavf_fix_features - fix up the netdev feature bits
3485 * @netdev: our net device
3486 * @features: desired feature bits
3487 *
3488 * Returns fixed-up features bits
3489 **/
3490 static netdev_features_t iavf_fix_features(struct net_device *netdev,
3491 netdev_features_t features)
3492 {
3493 struct iavf_adapter *adapter = netdev_priv(netdev);
3494
3495 if (adapter->vf_res &&
3496 !(adapter->vf_res->vf_cap_flags & VIRTCHNL_VF_OFFLOAD_VLAN))
3497 features &= ~(NETIF_F_HW_VLAN_CTAG_TX |
3498 NETIF_F_HW_VLAN_CTAG_RX |
3499 NETIF_F_HW_VLAN_CTAG_FILTER);
3500
3501 return features;
3502 }
3503
3504 static const struct net_device_ops iavf_netdev_ops = {
3505 .ndo_open = iavf_open,
3506 .ndo_stop = iavf_close,
3507 .ndo_start_xmit = iavf_xmit_frame,
3508 .ndo_set_rx_mode = iavf_set_rx_mode,
3509 .ndo_validate_addr = eth_validate_addr,
3510 .ndo_set_mac_address = iavf_set_mac,
3511 .ndo_change_mtu = iavf_change_mtu,
3512 .ndo_tx_timeout = iavf_tx_timeout,
3513 .ndo_vlan_rx_add_vid = iavf_vlan_rx_add_vid,
3514 .ndo_vlan_rx_kill_vid = iavf_vlan_rx_kill_vid,
3515 .ndo_features_check = iavf_features_check,
3516 .ndo_fix_features = iavf_fix_features,
3517 .ndo_set_features = iavf_set_features,
3518 .ndo_setup_tc = iavf_setup_tc,
3519 };
3520
3521 /**
3522 * iavf_check_reset_complete - check that VF reset is complete
3523 * @hw: pointer to hw struct
3524 *
3525 * Returns 0 if device is ready to use, or -EBUSY if it's in reset.
3526 **/
3527 static int iavf_check_reset_complete(struct iavf_hw *hw)
3528 {
3529 u32 rstat;
3530 int i;
3531
3532 for (i = 0; i < IAVF_RESET_WAIT_COMPLETE_COUNT; i++) {
3533 rstat = rd32(hw, IAVF_VFGEN_RSTAT) &
3534 IAVF_VFGEN_RSTAT_VFR_STATE_MASK;
3535 if ((rstat == VIRTCHNL_VFR_VFACTIVE) ||
3536 (rstat == VIRTCHNL_VFR_COMPLETED))
3537 return 0;
3538 usleep_range(10, 20);
3539 }
3540 return -EBUSY;
3541 }
3542
3543 /**
3544 * iavf_process_config - Process the config information we got from the PF
3545 * @adapter: board private structure
3546 *
3547 * Verify that we have a valid config struct, and set up our netdev features
3548 * and our VSI struct.
3549 **/
3550 int iavf_process_config(struct iavf_adapter *adapter)
3551 {
3552 struct virtchnl_vf_resource *vfres = adapter->vf_res;
3553 int i, num_req_queues = adapter->num_req_queues;
3554 struct net_device *netdev = adapter->netdev;
3555 struct iavf_vsi *vsi = &adapter->vsi;
3556 netdev_features_t hw_enc_features;
3557 netdev_features_t hw_features;
3558
3559 /* got VF config message back from PF, now we can parse it */
3560 for (i = 0; i < vfres->num_vsis; i++) {
3561 if (vfres->vsi_res[i].vsi_type == VIRTCHNL_VSI_SRIOV)
3562 adapter->vsi_res = &vfres->vsi_res[i];
3563 }
3564 if (!adapter->vsi_res) {
3565 dev_err(&adapter->pdev->dev, "No LAN VSI found\n");
3566 return -ENODEV;
3567 }
3568
3569 if (num_req_queues &&
3570 num_req_queues > adapter->vsi_res->num_queue_pairs) {
3571 /* Problem. The PF gave us fewer queues than what we had
3572 * negotiated in our request. Need a reset to see if we can't
3573 * get back to a working state.
3574 */
3575 dev_err(&adapter->pdev->dev,
3576 "Requested %d queues, but PF only gave us %d.\n",
3577 num_req_queues,
3578 adapter->vsi_res->num_queue_pairs);
3579 adapter->flags |= IAVF_FLAG_REINIT_ITR_NEEDED;
3580 adapter->num_req_queues = adapter->vsi_res->num_queue_pairs;
3581 iavf_schedule_reset(adapter);
3582 return -ENODEV;
3583 }
3584 adapter->num_req_queues = 0;
3585
3586 hw_enc_features = NETIF_F_SG |
3587 NETIF_F_IP_CSUM |
3588 NETIF_F_IPV6_CSUM |
3589 NETIF_F_HIGHDMA |
3590 NETIF_F_SOFT_FEATURES |
3591 NETIF_F_TSO |
3592 NETIF_F_TSO_ECN |
3593 NETIF_F_TSO6 |
3594 NETIF_F_SCTP_CRC |
3595 NETIF_F_RXHASH |
3596 NETIF_F_RXCSUM |
3597 0;
3598
3599 /* advertise to stack only if offloads for encapsulated packets is
3600 * supported
3601 */
3602 if (vfres->vf_cap_flags & VIRTCHNL_VF_OFFLOAD_ENCAP) {
3603 hw_enc_features |= NETIF_F_GSO_UDP_TUNNEL |
3604 NETIF_F_GSO_GRE |
3605 NETIF_F_GSO_GRE_CSUM |
3606 NETIF_F_GSO_IPXIP4 |
3607 NETIF_F_GSO_IPXIP6 |
3608 NETIF_F_GSO_UDP_TUNNEL_CSUM |
3609 NETIF_F_GSO_PARTIAL |
3610 0;
3611
3612 if (!(vfres->vf_cap_flags &
3613 VIRTCHNL_VF_OFFLOAD_ENCAP_CSUM))
3614 netdev->gso_partial_features |=
3615 NETIF_F_GSO_UDP_TUNNEL_CSUM;
3616
3617 netdev->gso_partial_features |= NETIF_F_GSO_GRE_CSUM;
3618 netdev->hw_enc_features |= NETIF_F_TSO_MANGLEID;
3619 netdev->hw_enc_features |= hw_enc_features;
3620 }
3621 /* record features VLANs can make use of */
3622 netdev->vlan_features |= hw_enc_features | NETIF_F_TSO_MANGLEID;
3623
3624 /* Write features and hw_features separately to avoid polluting
3625 * with, or dropping, features that are set when we registered.
3626 */
3627 hw_features = hw_enc_features;
3628
3629 /* Enable VLAN features if supported */
3630 if (vfres->vf_cap_flags & VIRTCHNL_VF_OFFLOAD_VLAN)
3631 hw_features |= (NETIF_F_HW_VLAN_CTAG_TX |
3632 NETIF_F_HW_VLAN_CTAG_RX);
3633 /* Enable cloud filter if ADQ is supported */
3634 if (vfres->vf_cap_flags & VIRTCHNL_VF_OFFLOAD_ADQ)
3635 hw_features |= NETIF_F_HW_TC;
3636 if (vfres->vf_cap_flags & VIRTCHNL_VF_OFFLOAD_USO)
3637 hw_features |= NETIF_F_GSO_UDP_L4;
3638
3639 netdev->hw_features |= hw_features;
3640
3641 netdev->features |= hw_features;
3642
3643 if (vfres->vf_cap_flags & VIRTCHNL_VF_OFFLOAD_VLAN)
3644 netdev->features |= NETIF_F_HW_VLAN_CTAG_FILTER;
3645
3646 netdev->priv_flags |= IFF_UNICAST_FLT;
3647
3648 /* Do not turn on offloads when they are requested to be turned off.
3649 * TSO needs minimum 576 bytes to work correctly.
3650 */
3651 if (netdev->wanted_features) {
3652 if (!(netdev->wanted_features & NETIF_F_TSO) ||
3653 netdev->mtu < 576)
3654 netdev->features &= ~NETIF_F_TSO;
3655 if (!(netdev->wanted_features & NETIF_F_TSO6) ||
3656 netdev->mtu < 576)
3657 netdev->features &= ~NETIF_F_TSO6;
3658 if (!(netdev->wanted_features & NETIF_F_TSO_ECN))
3659 netdev->features &= ~NETIF_F_TSO_ECN;
3660 if (!(netdev->wanted_features & NETIF_F_GRO))
3661 netdev->features &= ~NETIF_F_GRO;
3662 if (!(netdev->wanted_features & NETIF_F_GSO))
3663 netdev->features &= ~NETIF_F_GSO;
3664 }
3665
3666 adapter->vsi.id = adapter->vsi_res->vsi_id;
3667
3668 adapter->vsi.back = adapter;
3669 adapter->vsi.base_vector = 1;
3670 adapter->vsi.work_limit = IAVF_DEFAULT_IRQ_WORK;
3671 vsi->netdev = adapter->netdev;
3672 vsi->qs_handle = adapter->vsi_res->qset_handle;
3673 if (vfres->vf_cap_flags & VIRTCHNL_VF_OFFLOAD_RSS_PF) {
3674 adapter->rss_key_size = vfres->rss_key_size;
3675 adapter->rss_lut_size = vfres->rss_lut_size;
3676 } else {
3677 adapter->rss_key_size = IAVF_HKEY_ARRAY_SIZE;
3678 adapter->rss_lut_size = IAVF_HLUT_ARRAY_SIZE;
3679 }
3680
3681 return 0;
3682 }
3683
3684 /**
3685 * iavf_init_task - worker thread to perform delayed initialization
3686 * @work: pointer to work_struct containing our data
3687 *
3688 * This task completes the work that was begun in probe. Due to the nature
3689 * of VF-PF communications, we may need to wait tens of milliseconds to get
3690 * responses back from the PF. Rather than busy-wait in probe and bog down the
3691 * whole system, we'll do it in a task so we can sleep.
3692 * This task only runs during driver init. Once we've established
3693 * communications with the PF driver and set up our netdev, the watchdog
3694 * takes over.
3695 **/
3696 static void iavf_init_task(struct work_struct *work)
3697 {
3698 struct iavf_adapter *adapter = container_of(work,
3699 struct iavf_adapter,
3700 init_task.work);
3701 struct iavf_hw *hw = &adapter->hw;
3702
3703 if (iavf_lock_timeout(&adapter->crit_lock, 5000)) {
3704 dev_warn(&adapter->pdev->dev, "failed to acquire crit_lock in %s\n", __FUNCTION__);
3705 return;
3706 }
3707 switch (adapter->state) {
3708 case __IAVF_STARTUP:
3709 iavf_startup(adapter);
3710 if (adapter->state == __IAVF_INIT_FAILED)
3711 goto init_failed;
3712 break;
3713 case __IAVF_INIT_VERSION_CHECK:
3714 iavf_init_version_check(adapter);
3715 if (adapter->state == __IAVF_INIT_FAILED)
3716 goto init_failed;
3717 break;
3718 case __IAVF_INIT_GET_RESOURCES:
3719 iavf_init_get_resources(adapter);
3720 if (adapter->state == __IAVF_INIT_FAILED)
3721 goto init_failed;
3722 goto out;
3723 default:
3724 goto init_failed;
3725 }
3726
3727 queue_delayed_work(iavf_wq, &adapter->init_task,
3728 msecs_to_jiffies(30));
3729 goto out;
3730 init_failed:
3731 if (++adapter->aq_wait_count > IAVF_AQ_MAX_ERR) {
3732 dev_err(&adapter->pdev->dev,
3733 "Failed to communicate with PF; waiting before retry\n");
3734 adapter->flags |= IAVF_FLAG_PF_COMMS_FAILED;
3735 iavf_shutdown_adminq(hw);
3736 iavf_change_state(adapter, __IAVF_STARTUP);
3737 queue_delayed_work(iavf_wq, &adapter->init_task, HZ * 5);
3738 goto out;
3739 }
3740 queue_delayed_work(iavf_wq, &adapter->init_task, HZ);
3741 out:
3742 mutex_unlock(&adapter->crit_lock);
3743 }
3744
3745 /**
3746 * iavf_shutdown - Shutdown the device in preparation for a reboot
3747 * @pdev: pci device structure
3748 **/
3749 static void iavf_shutdown(struct pci_dev *pdev)
3750 {
3751 struct net_device *netdev = pci_get_drvdata(pdev);
3752 struct iavf_adapter *adapter = netdev_priv(netdev);
3753
3754 netif_device_detach(netdev);
3755
3756 if (netif_running(netdev))
3757 iavf_close(netdev);
3758
3759 if (iavf_lock_timeout(&adapter->crit_lock, 5000))
3760 dev_warn(&adapter->pdev->dev, "failed to acquire crit_lock in %s\n", __FUNCTION__);
3761 /* Prevent the watchdog from running. */
3762 iavf_change_state(adapter, __IAVF_REMOVE);
3763 adapter->aq_required = 0;
3764 mutex_unlock(&adapter->crit_lock);
3765
3766 #ifdef CONFIG_PM
3767 pci_save_state(pdev);
3768
3769 #endif
3770 pci_disable_device(pdev);
3771 }
3772
3773 /**
3774 * iavf_probe - Device Initialization Routine
3775 * @pdev: PCI device information struct
3776 * @ent: entry in iavf_pci_tbl
3777 *
3778 * Returns 0 on success, negative on failure
3779 *
3780 * iavf_probe initializes an adapter identified by a pci_dev structure.
3781 * The OS initialization, configuring of the adapter private structure,
3782 * and a hardware reset occur.
3783 **/
3784 static int iavf_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
3785 {
3786 struct net_device *netdev;
3787 struct iavf_adapter *adapter = NULL;
3788 struct iavf_hw *hw = NULL;
3789 int err;
3790
3791 err = pci_enable_device(pdev);
3792 if (err)
3793 return err;
3794
3795 err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
3796 if (err) {
3797 err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
3798 if (err) {
3799 dev_err(&pdev->dev,
3800 "DMA configuration failed: 0x%x\n", err);
3801 goto err_dma;
3802 }
3803 }
3804
3805 err = pci_request_regions(pdev, iavf_driver_name);
3806 if (err) {
3807 dev_err(&pdev->dev,
3808 "pci_request_regions failed 0x%x\n", err);
3809 goto err_pci_reg;
3810 }
3811
3812 pci_enable_pcie_error_reporting(pdev);
3813
3814 pci_set_master(pdev);
3815
3816 netdev = alloc_etherdev_mq(sizeof(struct iavf_adapter),
3817 IAVF_MAX_REQ_QUEUES);
3818 if (!netdev) {
3819 err = -ENOMEM;
3820 goto err_alloc_etherdev;
3821 }
3822
3823 SET_NETDEV_DEV(netdev, &pdev->dev);
3824
3825 pci_set_drvdata(pdev, netdev);
3826 adapter = netdev_priv(netdev);
3827
3828 adapter->netdev = netdev;
3829 adapter->pdev = pdev;
3830
3831 hw = &adapter->hw;
3832 hw->back = adapter;
3833
3834 adapter->msg_enable = BIT(DEFAULT_DEBUG_LEVEL_SHIFT) - 1;
3835 iavf_change_state(adapter, __IAVF_STARTUP);
3836
3837 /* Call save state here because it relies on the adapter struct. */
3838 pci_save_state(pdev);
3839
3840 hw->hw_addr = ioremap(pci_resource_start(pdev, 0),
3841 pci_resource_len(pdev, 0));
3842 if (!hw->hw_addr) {
3843 err = -EIO;
3844 goto err_ioremap;
3845 }
3846 hw->vendor_id = pdev->vendor;
3847 hw->device_id = pdev->device;
3848 pci_read_config_byte(pdev, PCI_REVISION_ID, &hw->revision_id);
3849 hw->subsystem_vendor_id = pdev->subsystem_vendor;
3850 hw->subsystem_device_id = pdev->subsystem_device;
3851 hw->bus.device = PCI_SLOT(pdev->devfn);
3852 hw->bus.func = PCI_FUNC(pdev->devfn);
3853 hw->bus.bus_id = pdev->bus->number;
3854
3855 /* set up the locks for the AQ, do this only once in probe
3856 * and destroy them only once in remove
3857 */
3858 mutex_init(&adapter->crit_lock);
3859 mutex_init(&adapter->client_lock);
3860 mutex_init(&adapter->remove_lock);
3861 mutex_init(&hw->aq.asq_mutex);
3862 mutex_init(&hw->aq.arq_mutex);
3863
3864 spin_lock_init(&adapter->mac_vlan_list_lock);
3865 spin_lock_init(&adapter->cloud_filter_list_lock);
3866 spin_lock_init(&adapter->fdir_fltr_lock);
3867 spin_lock_init(&adapter->adv_rss_lock);
3868
3869 INIT_LIST_HEAD(&adapter->mac_filter_list);
3870 INIT_LIST_HEAD(&adapter->vlan_filter_list);
3871 INIT_LIST_HEAD(&adapter->cloud_filter_list);
3872 INIT_LIST_HEAD(&adapter->fdir_list_head);
3873 INIT_LIST_HEAD(&adapter->adv_rss_list_head);
3874
3875 INIT_WORK(&adapter->reset_task, iavf_reset_task);
3876 INIT_WORK(&adapter->adminq_task, iavf_adminq_task);
3877 INIT_DELAYED_WORK(&adapter->watchdog_task, iavf_watchdog_task);
3878 INIT_DELAYED_WORK(&adapter->client_task, iavf_client_task);
3879 INIT_DELAYED_WORK(&adapter->init_task, iavf_init_task);
3880 queue_delayed_work(iavf_wq, &adapter->init_task,
3881 msecs_to_jiffies(5 * (pdev->devfn & 0x07)));
3882
3883 /* Setup the wait queue for indicating transition to down status */
3884 init_waitqueue_head(&adapter->down_waitqueue);
3885
3886 return 0;
3887
3888 err_ioremap:
3889 free_netdev(netdev);
3890 err_alloc_etherdev:
3891 pci_disable_pcie_error_reporting(pdev);
3892 pci_release_regions(pdev);
3893 err_pci_reg:
3894 err_dma:
3895 pci_disable_device(pdev);
3896 return err;
3897 }
3898
3899 /**
3900 * iavf_suspend - Power management suspend routine
3901 * @dev_d: device info pointer
3902 *
3903 * Called when the system (VM) is entering sleep/suspend.
3904 **/
3905 static int __maybe_unused iavf_suspend(struct device *dev_d)
3906 {
3907 struct net_device *netdev = dev_get_drvdata(dev_d);
3908 struct iavf_adapter *adapter = netdev_priv(netdev);
3909
3910 netif_device_detach(netdev);
3911
3912 while (!mutex_trylock(&adapter->crit_lock))
3913 usleep_range(500, 1000);
3914
3915 if (netif_running(netdev)) {
3916 rtnl_lock();
3917 iavf_down(adapter);
3918 rtnl_unlock();
3919 }
3920 iavf_free_misc_irq(adapter);
3921 iavf_reset_interrupt_capability(adapter);
3922
3923 mutex_unlock(&adapter->crit_lock);
3924
3925 return 0;
3926 }
3927
3928 /**
3929 * iavf_resume - Power management resume routine
3930 * @dev_d: device info pointer
3931 *
3932 * Called when the system (VM) is resumed from sleep/suspend.
3933 **/
3934 static int __maybe_unused iavf_resume(struct device *dev_d)
3935 {
3936 struct pci_dev *pdev = to_pci_dev(dev_d);
3937 struct net_device *netdev = pci_get_drvdata(pdev);
3938 struct iavf_adapter *adapter = netdev_priv(netdev);
3939 u32 err;
3940
3941 pci_set_master(pdev);
3942
3943 rtnl_lock();
3944 err = iavf_set_interrupt_capability(adapter);
3945 if (err) {
3946 rtnl_unlock();
3947 dev_err(&pdev->dev, "Cannot enable MSI-X interrupts.\n");
3948 return err;
3949 }
3950 err = iavf_request_misc_irq(adapter);
3951 rtnl_unlock();
3952 if (err) {
3953 dev_err(&pdev->dev, "Cannot get interrupt vector.\n");
3954 return err;
3955 }
3956
3957 queue_work(iavf_wq, &adapter->reset_task);
3958
3959 netif_device_attach(netdev);
3960
3961 return err;
3962 }
3963
3964 /**
3965 * iavf_remove - Device Removal Routine
3966 * @pdev: PCI device information struct
3967 *
3968 * iavf_remove is called by the PCI subsystem to alert the driver
3969 * that it should release a PCI device. The could be caused by a
3970 * Hot-Plug event, or because the driver is going to be removed from
3971 * memory.
3972 **/
3973 static void iavf_remove(struct pci_dev *pdev)
3974 {
3975 struct net_device *netdev = pci_get_drvdata(pdev);
3976 struct iavf_adapter *adapter = netdev_priv(netdev);
3977 struct iavf_fdir_fltr *fdir, *fdirtmp;
3978 struct iavf_vlan_filter *vlf, *vlftmp;
3979 struct iavf_adv_rss *rss, *rsstmp;
3980 struct iavf_mac_filter *f, *ftmp;
3981 struct iavf_cloud_filter *cf, *cftmp;
3982 struct iavf_hw *hw = &adapter->hw;
3983 int err;
3984 /* Indicate we are in remove and not to run reset_task */
3985 mutex_lock(&adapter->remove_lock);
3986 cancel_delayed_work_sync(&adapter->init_task);
3987 cancel_work_sync(&adapter->reset_task);
3988 cancel_delayed_work_sync(&adapter->client_task);
3989 if (adapter->netdev_registered) {
3990 unregister_netdev(netdev);
3991 adapter->netdev_registered = false;
3992 }
3993 if (CLIENT_ALLOWED(adapter)) {
3994 err = iavf_lan_del_device(adapter);
3995 if (err)
3996 dev_warn(&pdev->dev, "Failed to delete client device: %d\n",
3997 err);
3998 }
3999
4000 iavf_request_reset(adapter);
4001 msleep(50);
4002 /* If the FW isn't responding, kick it once, but only once. */
4003 if (!iavf_asq_done(hw)) {
4004 iavf_request_reset(adapter);
4005 msleep(50);
4006 }
4007 if (iavf_lock_timeout(&adapter->crit_lock, 5000))
4008 dev_warn(&adapter->pdev->dev, "failed to acquire crit_lock in %s\n", __FUNCTION__);
4009
4010 /* Shut down all the garbage mashers on the detention level */
4011 iavf_change_state(adapter, __IAVF_REMOVE);
4012 adapter->aq_required = 0;
4013 adapter->flags &= ~IAVF_FLAG_REINIT_ITR_NEEDED;
4014 iavf_free_all_tx_resources(adapter);
4015 iavf_free_all_rx_resources(adapter);
4016 iavf_misc_irq_disable(adapter);
4017 iavf_free_misc_irq(adapter);
4018 iavf_reset_interrupt_capability(adapter);
4019 iavf_free_q_vectors(adapter);
4020
4021 cancel_delayed_work_sync(&adapter->watchdog_task);
4022
4023 cancel_work_sync(&adapter->adminq_task);
4024
4025 iavf_free_rss(adapter);
4026
4027 if (hw->aq.asq.count)
4028 iavf_shutdown_adminq(hw);
4029
4030 /* destroy the locks only once, here */
4031 mutex_destroy(&hw->aq.arq_mutex);
4032 mutex_destroy(&hw->aq.asq_mutex);
4033 mutex_destroy(&adapter->client_lock);
4034 mutex_unlock(&adapter->crit_lock);
4035 mutex_destroy(&adapter->crit_lock);
4036 mutex_unlock(&adapter->remove_lock);
4037 mutex_destroy(&adapter->remove_lock);
4038
4039 iounmap(hw->hw_addr);
4040 pci_release_regions(pdev);
4041 iavf_free_queues(adapter);
4042 kfree(adapter->vf_res);
4043 spin_lock_bh(&adapter->mac_vlan_list_lock);
4044 /* If we got removed before an up/down sequence, we've got a filter
4045 * hanging out there that we need to get rid of.
4046 */
4047 list_for_each_entry_safe(f, ftmp, &adapter->mac_filter_list, list) {
4048 list_del(&f->list);
4049 kfree(f);
4050 }
4051 list_for_each_entry_safe(vlf, vlftmp, &adapter->vlan_filter_list,
4052 list) {
4053 list_del(&vlf->list);
4054 kfree(vlf);
4055 }
4056
4057 spin_unlock_bh(&adapter->mac_vlan_list_lock);
4058
4059 spin_lock_bh(&adapter->cloud_filter_list_lock);
4060 list_for_each_entry_safe(cf, cftmp, &adapter->cloud_filter_list, list) {
4061 list_del(&cf->list);
4062 kfree(cf);
4063 }
4064 spin_unlock_bh(&adapter->cloud_filter_list_lock);
4065
4066 spin_lock_bh(&adapter->fdir_fltr_lock);
4067 list_for_each_entry_safe(fdir, fdirtmp, &adapter->fdir_list_head, list) {
4068 list_del(&fdir->list);
4069 kfree(fdir);
4070 }
4071 spin_unlock_bh(&adapter->fdir_fltr_lock);
4072
4073 spin_lock_bh(&adapter->adv_rss_lock);
4074 list_for_each_entry_safe(rss, rsstmp, &adapter->adv_rss_list_head,
4075 list) {
4076 list_del(&rss->list);
4077 kfree(rss);
4078 }
4079 spin_unlock_bh(&adapter->adv_rss_lock);
4080
4081 free_netdev(netdev);
4082
4083 pci_disable_pcie_error_reporting(pdev);
4084
4085 pci_disable_device(pdev);
4086 }
4087
4088 static SIMPLE_DEV_PM_OPS(iavf_pm_ops, iavf_suspend, iavf_resume);
4089
4090 static struct pci_driver iavf_driver = {
4091 .name = iavf_driver_name,
4092 .id_table = iavf_pci_tbl,
4093 .probe = iavf_probe,
4094 .remove = iavf_remove,
4095 .driver.pm = &iavf_pm_ops,
4096 .shutdown = iavf_shutdown,
4097 };
4098
4099 /**
4100 * iavf_init_module - Driver Registration Routine
4101 *
4102 * iavf_init_module is the first routine called when the driver is
4103 * loaded. All it does is register with the PCI subsystem.
4104 **/
4105 static int __init iavf_init_module(void)
4106 {
4107 int ret;
4108
4109 pr_info("iavf: %s\n", iavf_driver_string);
4110
4111 pr_info("%s\n", iavf_copyright);
4112
4113 iavf_wq = alloc_workqueue("%s", WQ_UNBOUND | WQ_MEM_RECLAIM, 1,
4114 iavf_driver_name);
4115 if (!iavf_wq) {
4116 pr_err("%s: Failed to create workqueue\n", iavf_driver_name);
4117 return -ENOMEM;
4118 }
4119 ret = pci_register_driver(&iavf_driver);
4120 return ret;
4121 }
4122
4123 module_init(iavf_init_module);
4124
4125 /**
4126 * iavf_exit_module - Driver Exit Cleanup Routine
4127 *
4128 * iavf_exit_module is called just before the driver is removed
4129 * from memory.
4130 **/
4131 static void __exit iavf_exit_module(void)
4132 {
4133 pci_unregister_driver(&iavf_driver);
4134 destroy_workqueue(iavf_wq);
4135 }
4136
4137 module_exit(iavf_exit_module);
4138
4139 /* iavf_main.c */