Debian Codegraph @

drivers/net/ethernet/intel/iavf/iavf_main.c

raw
1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright(c) 2013 - 2018 Intel Corporation. */
3
4 #include <linux/net/intel/libie/rx.h>
5 #include <net/netdev_lock.h>
6
7 #include "iavf.h"
8 #include "iavf_ptp.h"
9 #include "iavf_prototype.h"
10 /* All iavf tracepoints are defined by the include below, which must
11 * be included exactly once across the whole kernel with
12 * CREATE_TRACE_POINTS defined
13 */
14 #define CREATE_TRACE_POINTS
15 #include "iavf_trace.h"
16
17 static int iavf_setup_all_tx_resources(struct iavf_adapter *adapter);
18 static int iavf_setup_all_rx_resources(struct iavf_adapter *adapter);
19 static int iavf_close(struct net_device *netdev);
20 static void iavf_init_get_resources(struct iavf_adapter *adapter);
21 static int iavf_check_reset_complete(struct iavf_hw *hw);
22
23 char iavf_driver_name[] = "iavf";
24 static const char iavf_driver_string[] =
25 "Intel(R) Ethernet Adaptive Virtual Function Network Driver";
26
27 static const char iavf_copyright[] =
28 "Copyright (c) 2013 - 2018 Intel Corporation.";
29
30 /* iavf_pci_tbl - PCI Device ID Table
31 *
32 * Wildcard entries (PCI_ANY_ID) should come last
33 * Last entry must be all 0s
34 *
35 * { Vendor ID, Device ID, SubVendor ID, SubDevice ID,
36 * Class, Class Mask, private data (not used) }
37 */
38 static const struct pci_device_id iavf_pci_tbl[] = {
39 {PCI_VDEVICE(INTEL, IAVF_DEV_ID_VF), 0},
40 {PCI_VDEVICE(INTEL, IAVF_DEV_ID_VF_HV), 0},
41 {PCI_VDEVICE(INTEL, IAVF_DEV_ID_X722_VF), 0},
42 {PCI_VDEVICE(INTEL, IAVF_DEV_ID_ADAPTIVE_VF), 0},
43 /* required last entry */
44 {0, }
45 };
46
47 MODULE_DEVICE_TABLE(pci, iavf_pci_tbl);
48
49 MODULE_ALIAS("i40evf");
50 MODULE_DESCRIPTION("Intel(R) Ethernet Adaptive Virtual Function Network Driver");
51 MODULE_IMPORT_NS("LIBETH");
52 MODULE_IMPORT_NS("LIBIE");
53 MODULE_IMPORT_NS("LIBIE_ADMINQ");
54 MODULE_LICENSE("GPL v2");
55
56 static const struct net_device_ops iavf_netdev_ops;
57
58 int iavf_status_to_errno(enum iavf_status status)
59 {
60 switch (status) {
61 case IAVF_SUCCESS:
62 return 0;
63 case IAVF_ERR_PARAM:
64 case IAVF_ERR_MAC_TYPE:
65 case IAVF_ERR_INVALID_MAC_ADDR:
66 case IAVF_ERR_INVALID_LINK_SETTINGS:
67 case IAVF_ERR_INVALID_PD_ID:
68 case IAVF_ERR_INVALID_QP_ID:
69 case IAVF_ERR_INVALID_CQ_ID:
70 case IAVF_ERR_INVALID_CEQ_ID:
71 case IAVF_ERR_INVALID_AEQ_ID:
72 case IAVF_ERR_INVALID_SIZE:
73 case IAVF_ERR_INVALID_ARP_INDEX:
74 case IAVF_ERR_INVALID_FPM_FUNC_ID:
75 case IAVF_ERR_QP_INVALID_MSG_SIZE:
76 case IAVF_ERR_INVALID_FRAG_COUNT:
77 case IAVF_ERR_INVALID_ALIGNMENT:
78 case IAVF_ERR_INVALID_PUSH_PAGE_INDEX:
79 case IAVF_ERR_INVALID_IMM_DATA_SIZE:
80 case IAVF_ERR_INVALID_VF_ID:
81 case IAVF_ERR_INVALID_HMCFN_ID:
82 case IAVF_ERR_INVALID_PBLE_INDEX:
83 case IAVF_ERR_INVALID_SD_INDEX:
84 case IAVF_ERR_INVALID_PAGE_DESC_INDEX:
85 case IAVF_ERR_INVALID_SD_TYPE:
86 case IAVF_ERR_INVALID_HMC_OBJ_INDEX:
87 case IAVF_ERR_INVALID_HMC_OBJ_COUNT:
88 case IAVF_ERR_INVALID_SRQ_ARM_LIMIT:
89 return -EINVAL;
90 case IAVF_ERR_NVM:
91 case IAVF_ERR_NVM_CHECKSUM:
92 case IAVF_ERR_PHY:
93 case IAVF_ERR_CONFIG:
94 case IAVF_ERR_UNKNOWN_PHY:
95 case IAVF_ERR_LINK_SETUP:
96 case IAVF_ERR_ADAPTER_STOPPED:
97 case IAVF_ERR_PRIMARY_REQUESTS_PENDING:
98 case IAVF_ERR_AUTONEG_NOT_COMPLETE:
99 case IAVF_ERR_RESET_FAILED:
100 case IAVF_ERR_BAD_PTR:
101 case IAVF_ERR_SWFW_SYNC:
102 case IAVF_ERR_QP_TOOMANY_WRS_POSTED:
103 case IAVF_ERR_QUEUE_EMPTY:
104 case IAVF_ERR_FLUSHED_QUEUE:
105 case IAVF_ERR_OPCODE_MISMATCH:
106 case IAVF_ERR_CQP_COMPL_ERROR:
107 case IAVF_ERR_BACKING_PAGE_ERROR:
108 case IAVF_ERR_NO_PBLCHUNKS_AVAILABLE:
109 case IAVF_ERR_MEMCPY_FAILED:
110 case IAVF_ERR_SRQ_ENABLED:
111 case IAVF_ERR_ADMIN_QUEUE_ERROR:
112 case IAVF_ERR_ADMIN_QUEUE_FULL:
113 case IAVF_ERR_BAD_RDMA_CQE:
114 case IAVF_ERR_NVM_BLANK_MODE:
115 case IAVF_ERR_PE_DOORBELL_NOT_ENABLED:
116 case IAVF_ERR_DIAG_TEST_FAILED:
117 case IAVF_ERR_FIRMWARE_API_VERSION:
118 case IAVF_ERR_ADMIN_QUEUE_CRITICAL_ERROR:
119 return -EIO;
120 case IAVF_ERR_DEVICE_NOT_SUPPORTED:
121 return -ENODEV;
122 case IAVF_ERR_NO_AVAILABLE_VSI:
123 case IAVF_ERR_RING_FULL:
124 return -ENOSPC;
125 case IAVF_ERR_NO_MEMORY:
126 return -ENOMEM;
127 case IAVF_ERR_TIMEOUT:
128 case IAVF_ERR_ADMIN_QUEUE_TIMEOUT:
129 return -ETIMEDOUT;
130 case IAVF_ERR_NOT_IMPLEMENTED:
131 case IAVF_NOT_SUPPORTED:
132 return -EOPNOTSUPP;
133 case IAVF_ERR_ADMIN_QUEUE_NO_WORK:
134 return -EALREADY;
135 case IAVF_ERR_NOT_READY:
136 return -EBUSY;
137 case IAVF_ERR_BUF_TOO_SHORT:
138 return -EMSGSIZE;
139 }
140
141 return -EIO;
142 }
143
144 int virtchnl_status_to_errno(enum virtchnl_status_code v_status)
145 {
146 switch (v_status) {
147 case VIRTCHNL_STATUS_SUCCESS:
148 return 0;
149 case VIRTCHNL_STATUS_ERR_PARAM:
150 case VIRTCHNL_STATUS_ERR_INVALID_VF_ID:
151 return -EINVAL;
152 case VIRTCHNL_STATUS_ERR_NO_MEMORY:
153 return -ENOMEM;
154 case VIRTCHNL_STATUS_ERR_OPCODE_MISMATCH:
155 case VIRTCHNL_STATUS_ERR_CQP_COMPL_ERROR:
156 case VIRTCHNL_STATUS_ERR_ADMIN_QUEUE_ERROR:
157 return -EIO;
158 case VIRTCHNL_STATUS_ERR_NOT_SUPPORTED:
159 return -EOPNOTSUPP;
160 }
161
162 return -EIO;
163 }
164
165 /**
166 * iavf_pdev_to_adapter - go from pci_dev to adapter
167 * @pdev: pci_dev pointer
168 */
169 static struct iavf_adapter *iavf_pdev_to_adapter(struct pci_dev *pdev)
170 {
171 return netdev_priv(pci_get_drvdata(pdev));
172 }
173
174 /**
175 * iavf_is_reset_in_progress - Check if a reset is in progress
176 * @adapter: board private structure
177 */
178 static bool iavf_is_reset_in_progress(struct iavf_adapter *adapter)
179 {
180 if (adapter->state == __IAVF_RESETTING ||
181 adapter->flags & (IAVF_FLAG_RESET_PENDING |
182 IAVF_FLAG_RESET_NEEDED))
183 return true;
184
185 return false;
186 }
187
188 /**
189 * iavf_allocate_dma_mem_d - OS specific memory alloc for shared code
190 * @hw: pointer to the HW structure
191 * @mem: ptr to mem struct to fill out
192 * @size: size of memory requested
193 * @alignment: what to align the allocation to
194 **/
195 enum iavf_status iavf_allocate_dma_mem_d(struct iavf_hw *hw,
196 struct iavf_dma_mem *mem,
197 u64 size, u32 alignment)
198 {
199 struct iavf_adapter *adapter = (struct iavf_adapter *)hw->back;
200
201 if (!mem)
202 return IAVF_ERR_PARAM;
203
204 mem->size = ALIGN(size, alignment);
205 mem->va = dma_alloc_coherent(&adapter->pdev->dev, mem->size,
206 (dma_addr_t *)&mem->pa, GFP_KERNEL);
207 if (mem->va)
208 return 0;
209 else
210 return IAVF_ERR_NO_MEMORY;
211 }
212
213 /**
214 * iavf_free_dma_mem - wrapper for DMA memory freeing
215 * @hw: pointer to the HW structure
216 * @mem: ptr to mem struct to free
217 **/
218 enum iavf_status iavf_free_dma_mem(struct iavf_hw *hw, struct iavf_dma_mem *mem)
219 {
220 struct iavf_adapter *adapter = (struct iavf_adapter *)hw->back;
221
222 if (!mem || !mem->va)
223 return IAVF_ERR_PARAM;
224 dma_free_coherent(&adapter->pdev->dev, mem->size,
225 mem->va, (dma_addr_t)mem->pa);
226 return 0;
227 }
228
229 /**
230 * iavf_allocate_virt_mem - virt memory alloc wrapper
231 * @hw: pointer to the HW structure
232 * @mem: ptr to mem struct to fill out
233 * @size: size of memory requested
234 **/
235 enum iavf_status iavf_allocate_virt_mem(struct iavf_hw *hw,
236 struct iavf_virt_mem *mem, u32 size)
237 {
238 if (!mem)
239 return IAVF_ERR_PARAM;
240
241 mem->size = size;
242 mem->va = kzalloc(size, GFP_KERNEL);
243
244 if (mem->va)
245 return 0;
246 else
247 return IAVF_ERR_NO_MEMORY;
248 }
249
250 /**
251 * iavf_free_virt_mem - virt memory free wrapper
252 * @hw: pointer to the HW structure
253 * @mem: ptr to mem struct to free
254 **/
255 void iavf_free_virt_mem(struct iavf_hw *hw, struct iavf_virt_mem *mem)
256 {
257 kfree(mem->va);
258 }
259
260 /**
261 * iavf_schedule_reset - Set the flags and schedule a reset event
262 * @adapter: board private structure
263 * @flags: IAVF_FLAG_RESET_PENDING or IAVF_FLAG_RESET_NEEDED
264 **/
265 void iavf_schedule_reset(struct iavf_adapter *adapter, u64 flags)
266 {
267 if (!test_bit(__IAVF_IN_REMOVE_TASK, &adapter->crit_section) &&
268 !(adapter->flags &
269 (IAVF_FLAG_RESET_PENDING | IAVF_FLAG_RESET_NEEDED))) {
270 adapter->flags |= flags;
271 queue_work(adapter->wq, &adapter->reset_task);
272 }
273 }
274
275 /**
276 * iavf_schedule_aq_request - Set the flags and schedule aq request
277 * @adapter: board private structure
278 * @flags: requested aq flags
279 **/
280 void iavf_schedule_aq_request(struct iavf_adapter *adapter, u64 flags)
281 {
282 adapter->aq_required |= flags;
283 mod_delayed_work(adapter->wq, &adapter->watchdog_task, 0);
284 }
285
286 /**
287 * iavf_tx_timeout - Respond to a Tx Hang
288 * @netdev: network interface device structure
289 * @txqueue: queue number that is timing out
290 **/
291 static void iavf_tx_timeout(struct net_device *netdev, unsigned int txqueue)
292 {
293 struct iavf_adapter *adapter = netdev_priv(netdev);
294
295 adapter->tx_timeout_count++;
296 iavf_schedule_reset(adapter, IAVF_FLAG_RESET_NEEDED);
297 }
298
299 /**
300 * iavf_misc_irq_disable - Mask off interrupt generation on the NIC
301 * @adapter: board private structure
302 **/
303 static void iavf_misc_irq_disable(struct iavf_adapter *adapter)
304 {
305 struct iavf_hw *hw = &adapter->hw;
306
307 if (!adapter->msix_entries)
308 return;
309
310 wr32(hw, IAVF_VFINT_DYN_CTL01, 0);
311
312 iavf_flush(hw);
313
314 synchronize_irq(adapter->msix_entries[0].vector);
315 }
316
317 /**
318 * iavf_misc_irq_enable - Enable default interrupt generation settings
319 * @adapter: board private structure
320 **/
321 static void iavf_misc_irq_enable(struct iavf_adapter *adapter)
322 {
323 struct iavf_hw *hw = &adapter->hw;
324
325 wr32(hw, IAVF_VFINT_DYN_CTL01, IAVF_VFINT_DYN_CTL01_INTENA_MASK |
326 IAVF_VFINT_DYN_CTL01_ITR_INDX_MASK);
327 wr32(hw, IAVF_VFINT_ICR0_ENA1, IAVF_VFINT_ICR0_ENA1_ADMINQ_MASK);
328
329 iavf_flush(hw);
330 }
331
332 /**
333 * iavf_irq_disable - Mask off interrupt generation on the NIC
334 * @adapter: board private structure
335 **/
336 static void iavf_irq_disable(struct iavf_adapter *adapter)
337 {
338 int i;
339 struct iavf_hw *hw = &adapter->hw;
340
341 if (!adapter->msix_entries)
342 return;
343
344 for (i = 1; i < adapter->num_msix_vectors; i++) {
345 wr32(hw, IAVF_VFINT_DYN_CTLN1(i - 1), 0);
346 synchronize_irq(adapter->msix_entries[i].vector);
347 }
348 iavf_flush(hw);
349 }
350
351 /**
352 * iavf_irq_enable_queues - Enable interrupt for all queues
353 * @adapter: board private structure
354 **/
355 static void iavf_irq_enable_queues(struct iavf_adapter *adapter)
356 {
357 struct iavf_hw *hw = &adapter->hw;
358 int i;
359
360 for (i = 1; i < adapter->num_msix_vectors; i++) {
361 wr32(hw, IAVF_VFINT_DYN_CTLN1(i - 1),
362 IAVF_VFINT_DYN_CTLN1_INTENA_MASK |
363 IAVF_VFINT_DYN_CTLN1_ITR_INDX_MASK);
364 }
365 }
366
367 /**
368 * iavf_irq_enable - Enable default interrupt generation settings
369 * @adapter: board private structure
370 * @flush: boolean value whether to run rd32()
371 **/
372 void iavf_irq_enable(struct iavf_adapter *adapter, bool flush)
373 {
374 struct iavf_hw *hw = &adapter->hw;
375
376 iavf_misc_irq_enable(adapter);
377 iavf_irq_enable_queues(adapter);
378
379 if (flush)
380 iavf_flush(hw);
381 }
382
383 /**
384 * iavf_msix_aq - Interrupt handler for vector 0
385 * @irq: interrupt number
386 * @data: pointer to netdev
387 **/
388 static irqreturn_t iavf_msix_aq(int irq, void *data)
389 {
390 struct net_device *netdev = data;
391 struct iavf_adapter *adapter = netdev_priv(netdev);
392 struct iavf_hw *hw = &adapter->hw;
393
394 /* handle non-queue interrupts, these reads clear the registers */
395 rd32(hw, IAVF_VFINT_ICR01);
396 rd32(hw, IAVF_VFINT_ICR0_ENA1);
397
398 if (adapter->state != __IAVF_REMOVE)
399 /* schedule work on the private workqueue */
400 queue_work(adapter->wq, &adapter->adminq_task);
401
402 return IRQ_HANDLED;
403 }
404
405 /**
406 * iavf_msix_clean_rings - MSIX mode Interrupt Handler
407 * @irq: interrupt number
408 * @data: pointer to a q_vector
409 **/
410 static irqreturn_t iavf_msix_clean_rings(int irq, void *data)
411 {
412 struct iavf_q_vector *q_vector = data;
413
414 if (!q_vector->tx.ring && !q_vector->rx.ring)
415 return IRQ_HANDLED;
416
417 napi_schedule_irqoff(&q_vector->napi);
418
419 return IRQ_HANDLED;
420 }
421
422 /**
423 * iavf_map_vector_to_rxq - associate irqs with rx queues
424 * @adapter: board private structure
425 * @v_idx: interrupt number
426 * @r_idx: queue number
427 **/
428 static void
429 iavf_map_vector_to_rxq(struct iavf_adapter *adapter, int v_idx, int r_idx)
430 {
431 struct iavf_q_vector *q_vector = &adapter->q_vectors[v_idx];
432 struct iavf_ring *rx_ring = &adapter->rx_rings[r_idx];
433 struct iavf_hw *hw = &adapter->hw;
434
435 rx_ring->q_vector = q_vector;
436 rx_ring->next = q_vector->rx.ring;
437 rx_ring->vsi = &adapter->vsi;
438 q_vector->rx.ring = rx_ring;
439 q_vector->rx.count++;
440 q_vector->rx.next_update = jiffies + 1;
441 q_vector->rx.target_itr = ITR_TO_REG(rx_ring->itr_setting);
442 q_vector->ring_mask |= BIT(r_idx);
443 wr32(hw, IAVF_VFINT_ITRN1(IAVF_RX_ITR, q_vector->reg_idx),
444 q_vector->rx.current_itr >> 1);
445 q_vector->rx.current_itr = q_vector->rx.target_itr;
446 }
447
448 /**
449 * iavf_map_vector_to_txq - associate irqs with tx queues
450 * @adapter: board private structure
451 * @v_idx: interrupt number
452 * @t_idx: queue number
453 **/
454 static void
455 iavf_map_vector_to_txq(struct iavf_adapter *adapter, int v_idx, int t_idx)
456 {
457 struct iavf_q_vector *q_vector = &adapter->q_vectors[v_idx];
458 struct iavf_ring *tx_ring = &adapter->tx_rings[t_idx];
459 struct iavf_hw *hw = &adapter->hw;
460
461 tx_ring->q_vector = q_vector;
462 tx_ring->next = q_vector->tx.ring;
463 tx_ring->vsi = &adapter->vsi;
464 q_vector->tx.ring = tx_ring;
465 q_vector->tx.count++;
466 q_vector->tx.next_update = jiffies + 1;
467 q_vector->tx.target_itr = ITR_TO_REG(tx_ring->itr_setting);
468 q_vector->num_ringpairs++;
469 wr32(hw, IAVF_VFINT_ITRN1(IAVF_TX_ITR, q_vector->reg_idx),
470 q_vector->tx.target_itr >> 1);
471 q_vector->tx.current_itr = q_vector->tx.target_itr;
472 }
473
474 /**
475 * iavf_map_rings_to_vectors - Maps descriptor rings to vectors
476 * @adapter: board private structure to initialize
477 *
478 * This function maps descriptor rings to the queue-specific vectors
479 * we were allotted through the MSI-X enabling code. Ideally, we'd have
480 * one vector per ring/queue, but on a constrained vector budget, we
481 * group the rings as "efficiently" as possible. You would add new
482 * mapping configurations in here.
483 **/
484 static void iavf_map_rings_to_vectors(struct iavf_adapter *adapter)
485 {
486 int rings_remaining = adapter->num_active_queues;
487 int ridx = 0, vidx = 0;
488 int q_vectors;
489
490 q_vectors = adapter->num_msix_vectors - NONQ_VECS;
491
492 for (; ridx < rings_remaining; ridx++) {
493 iavf_map_vector_to_rxq(adapter, vidx, ridx);
494 iavf_map_vector_to_txq(adapter, vidx, ridx);
495
496 /* In the case where we have more queues than vectors, continue
497 * round-robin on vectors until all queues are mapped.
498 */
499 if (++vidx >= q_vectors)
500 vidx = 0;
501 }
502
503 adapter->aq_required |= IAVF_FLAG_AQ_MAP_VECTORS;
504 }
505
506 /**
507 * iavf_request_traffic_irqs - Initialize MSI-X interrupts
508 * @adapter: board private structure
509 * @basename: device basename
510 *
511 * Allocates MSI-X vectors for tx and rx handling, and requests
512 * interrupts from the kernel.
513 **/
514 static int
515 iavf_request_traffic_irqs(struct iavf_adapter *adapter, char *basename)
516 {
517 unsigned int vector, q_vectors;
518 unsigned int rx_int_idx = 0, tx_int_idx = 0;
519 int irq_num, err;
520
521 iavf_irq_disable(adapter);
522 /* Decrement for Other and TCP Timer vectors */
523 q_vectors = adapter->num_msix_vectors - NONQ_VECS;
524
525 for (vector = 0; vector < q_vectors; vector++) {
526 struct iavf_q_vector *q_vector = &adapter->q_vectors[vector];
527
528 irq_num = adapter->msix_entries[vector + NONQ_VECS].vector;
529
530 if (q_vector->tx.ring && q_vector->rx.ring) {
531 snprintf(q_vector->name, sizeof(q_vector->name),
532 "iavf-%s-TxRx-%u", basename, rx_int_idx++);
533 tx_int_idx++;
534 } else if (q_vector->rx.ring) {
535 snprintf(q_vector->name, sizeof(q_vector->name),
536 "iavf-%s-rx-%u", basename, rx_int_idx++);
537 } else if (q_vector->tx.ring) {
538 snprintf(q_vector->name, sizeof(q_vector->name),
539 "iavf-%s-tx-%u", basename, tx_int_idx++);
540 } else {
541 /* skip this unused q_vector */
542 continue;
543 }
544 err = request_irq(irq_num,
545 iavf_msix_clean_rings,
546 0,
547 q_vector->name,
548 q_vector);
549 if (err) {
550 dev_info(&adapter->pdev->dev,
551 "Request_irq failed, error: %d\n", err);
552 goto free_queue_irqs;
553 }
554 }
555
556 return 0;
557
558 free_queue_irqs:
559 while (vector) {
560 vector--;
561 irq_num = adapter->msix_entries[vector + NONQ_VECS].vector;
562 free_irq(irq_num, &adapter->q_vectors[vector]);
563 }
564 return err;
565 }
566
567 /**
568 * iavf_request_misc_irq - Initialize MSI-X interrupts
569 * @adapter: board private structure
570 *
571 * Allocates MSI-X vector 0 and requests interrupts from the kernel. This
572 * vector is only for the admin queue, and stays active even when the netdev
573 * is closed.
574 **/
575 static int iavf_request_misc_irq(struct iavf_adapter *adapter)
576 {
577 struct net_device *netdev = adapter->netdev;
578 int err;
579
580 snprintf(adapter->misc_vector_name,
581 sizeof(adapter->misc_vector_name) - 1, "iavf-%s:mbx",
582 dev_name(&adapter->pdev->dev));
583 err = request_irq(adapter->msix_entries[0].vector,
584 &iavf_msix_aq, 0,
585 adapter->misc_vector_name, netdev);
586 if (err) {
587 dev_err(&adapter->pdev->dev,
588 "request_irq for %s failed: %d\n",
589 adapter->misc_vector_name, err);
590 free_irq(adapter->msix_entries[0].vector, netdev);
591 }
592 return err;
593 }
594
595 /**
596 * iavf_free_traffic_irqs - Free MSI-X interrupts
597 * @adapter: board private structure
598 *
599 * Frees all MSI-X vectors other than 0.
600 **/
601 static void iavf_free_traffic_irqs(struct iavf_adapter *adapter)
602 {
603 struct iavf_q_vector *q_vector;
604 int vector, irq_num, q_vectors;
605
606 if (!adapter->msix_entries)
607 return;
608
609 q_vectors = adapter->num_msix_vectors - NONQ_VECS;
610
611 for (vector = 0; vector < q_vectors; vector++) {
612 q_vector = &adapter->q_vectors[vector];
613 netif_napi_set_irq_locked(&q_vector->napi, -1);
614 irq_num = adapter->msix_entries[vector + NONQ_VECS].vector;
615 free_irq(irq_num, q_vector);
616 }
617 }
618
619 /**
620 * iavf_free_misc_irq - Free MSI-X miscellaneous vector
621 * @adapter: board private structure
622 *
623 * Frees MSI-X vector 0.
624 **/
625 static void iavf_free_misc_irq(struct iavf_adapter *adapter)
626 {
627 struct net_device *netdev = adapter->netdev;
628
629 if (!adapter->msix_entries)
630 return;
631
632 free_irq(adapter->msix_entries[0].vector, netdev);
633 }
634
635 /**
636 * iavf_configure_tx - Configure Transmit Unit after Reset
637 * @adapter: board private structure
638 *
639 * Configure the Tx unit of the MAC after a reset.
640 **/
641 static void iavf_configure_tx(struct iavf_adapter *adapter)
642 {
643 struct iavf_hw *hw = &adapter->hw;
644 int i;
645
646 for (i = 0; i < adapter->num_active_queues; i++)
647 adapter->tx_rings[i].tail = hw->hw_addr + IAVF_QTX_TAIL1(i);
648 }
649
650 /**
651 * iavf_select_rx_desc_format - Select Rx descriptor format
652 * @adapter: adapter private structure
653 *
654 * Select what Rx descriptor format based on availability and enabled
655 * features.
656 *
657 * Return: the desired RXDID to select for a given Rx queue, as defined by
658 * enum virtchnl_rxdid_format.
659 */
660 static u8 iavf_select_rx_desc_format(const struct iavf_adapter *adapter)
661 {
662 u64 rxdids = adapter->supp_rxdids;
663
664 /* If we did not negotiate VIRTCHNL_VF_OFFLOAD_RX_FLEX_DESC, we must
665 * stick with the default value of the legacy 32 byte format.
666 */
667 if (!IAVF_RXDID_ALLOWED(adapter))
668 return VIRTCHNL_RXDID_1_32B_BASE;
669
670 /* Rx timestamping requires the use of flexible NIC descriptors */
671 if (iavf_ptp_cap_supported(adapter, VIRTCHNL_1588_PTP_CAP_RX_TSTAMP)) {
672 if (rxdids & BIT(VIRTCHNL_RXDID_2_FLEX_SQ_NIC))
673 return VIRTCHNL_RXDID_2_FLEX_SQ_NIC;
674
675 pci_warn(adapter->pdev,
676 "Unable to negotiate flexible descriptor format\n");
677 }
678
679 /* Warn if the PF does not list support for the default legacy
680 * descriptor format. This shouldn't happen, as this is the format
681 * used if VIRTCHNL_VF_OFFLOAD_RX_FLEX_DESC is not supported. It is
682 * likely caused by a bug in the PF implementation failing to indicate
683 * support for the format.
684 */
685 if (!(rxdids & VIRTCHNL_RXDID_1_32B_BASE_M))
686 netdev_warn(adapter->netdev, "PF does not list support for default Rx descriptor format\n");
687
688 return VIRTCHNL_RXDID_1_32B_BASE;
689 }
690
691 /**
692 * iavf_configure_rx - Configure Receive Unit after Reset
693 * @adapter: board private structure
694 *
695 * Configure the Rx unit of the MAC after a reset.
696 **/
697 static void iavf_configure_rx(struct iavf_adapter *adapter)
698 {
699 struct iavf_hw *hw = &adapter->hw;
700
701 adapter->rxdid = iavf_select_rx_desc_format(adapter);
702
703 for (u32 i = 0; i < adapter->num_active_queues; i++) {
704 adapter->rx_rings[i].tail = hw->hw_addr + IAVF_QRX_TAIL1(i);
705 adapter->rx_rings[i].rxdid = adapter->rxdid;
706 }
707 }
708
709 /**
710 * iavf_find_vlan - Search filter list for specific vlan filter
711 * @adapter: board private structure
712 * @vlan: vlan tag
713 *
714 * Returns ptr to the filter object or NULL. Must be called while holding the
715 * mac_vlan_list_lock.
716 **/
717 static struct
718 iavf_vlan_filter *iavf_find_vlan(struct iavf_adapter *adapter,
719 struct iavf_vlan vlan)
720 {
721 struct iavf_vlan_filter *f;
722
723 list_for_each_entry(f, &adapter->vlan_filter_list, list) {
724 if (f->vlan.vid == vlan.vid &&
725 f->vlan.tpid == vlan.tpid)
726 return f;
727 }
728
729 return NULL;
730 }
731
732 /**
733 * iavf_add_vlan - Add a vlan filter to the list
734 * @adapter: board private structure
735 * @vlan: VLAN tag
736 *
737 * Returns ptr to the filter object or NULL when no memory available.
738 **/
739 static struct
740 iavf_vlan_filter *iavf_add_vlan(struct iavf_adapter *adapter,
741 struct iavf_vlan vlan)
742 {
743 struct iavf_vlan_filter *f = NULL;
744
745 spin_lock_bh(&adapter->mac_vlan_list_lock);
746
747 f = iavf_find_vlan(adapter, vlan);
748 if (!f) {
749 f = kzalloc_obj(*f, GFP_ATOMIC);
750 if (!f)
751 goto clearout;
752
753 f->vlan = vlan;
754
755 list_add_tail(&f->list, &adapter->vlan_filter_list);
756 f->state = IAVF_VLAN_ADD;
757 adapter->num_vlan_filters++;
758 iavf_schedule_aq_request(adapter, IAVF_FLAG_AQ_ADD_VLAN_FILTER);
759 } else if (f->state == IAVF_VLAN_REMOVE) {
760 /* DEL not yet sent to PF, cancel it */
761 f->state = IAVF_VLAN_ACTIVE;
762 } else if (f->state == IAVF_VLAN_REMOVING) {
763 /* DEL already sent to PF, re-add after completion */
764 f->state = IAVF_VLAN_ADD;
765 iavf_schedule_aq_request(adapter,
766 IAVF_FLAG_AQ_ADD_VLAN_FILTER);
767 }
768
769 clearout:
770 spin_unlock_bh(&adapter->mac_vlan_list_lock);
771 return f;
772 }
773
774 /**
775 * iavf_del_vlan - Remove a vlan filter from the list
776 * @adapter: board private structure
777 * @vlan: VLAN tag
778 **/
779 static void iavf_del_vlan(struct iavf_adapter *adapter, struct iavf_vlan vlan)
780 {
781 struct iavf_vlan_filter *f;
782
783 spin_lock_bh(&adapter->mac_vlan_list_lock);
784
785 f = iavf_find_vlan(adapter, vlan);
786 if (f) {
787 /* IAVF_ADD_VLAN means that VLAN wasn't even added yet.
788 * Remove it from the list.
789 */
790 if (f->state == IAVF_VLAN_ADD) {
791 list_del(&f->list);
792 kfree(f);
793 adapter->num_vlan_filters--;
794 } else if (f->state != IAVF_VLAN_REMOVING) {
795 f->state = IAVF_VLAN_REMOVE;
796 iavf_schedule_aq_request(adapter,
797 IAVF_FLAG_AQ_DEL_VLAN_FILTER);
798 }
799 /* If REMOVING, DEL is already sent to PF; completion
800 * handler will free the filter when PF confirms.
801 */
802 }
803
804 spin_unlock_bh(&adapter->mac_vlan_list_lock);
805 }
806
807
808 /**
809 * iavf_get_num_vlans_added - get number of VLANs added
810 * @adapter: board private structure
811 */
812 u16 iavf_get_num_vlans_added(struct iavf_adapter *adapter)
813 {
814 return adapter->num_vlan_filters;
815 }
816
817 /**
818 * iavf_get_max_vlans_allowed - get maximum VLANs allowed for this VF
819 * @adapter: board private structure
820 *
821 * This depends on the negotiated VLAN capability. For VIRTCHNL_VF_OFFLOAD_VLAN,
822 * do not impose a limit as that maintains current behavior and for
823 * VIRTCHNL_VF_OFFLOAD_VLAN_V2, use the maximum allowed sent from the PF.
824 **/
825 static u16 iavf_get_max_vlans_allowed(struct iavf_adapter *adapter)
826 {
827 /* don't impose any limit for VIRTCHNL_VF_OFFLOAD_VLAN since there has
828 * never been a limit on the VF driver side
829 */
830 if (VLAN_ALLOWED(adapter))
831 return VLAN_N_VID;
832 else if (VLAN_V2_ALLOWED(adapter))
833 return adapter->vlan_v2_caps.filtering.max_filters;
834
835 return 0;
836 }
837
838 /**
839 * iavf_max_vlans_added - check if maximum VLANs allowed already exist
840 * @adapter: board private structure
841 **/
842 static bool iavf_max_vlans_added(struct iavf_adapter *adapter)
843 {
844 if (iavf_get_num_vlans_added(adapter) <
845 iavf_get_max_vlans_allowed(adapter))
846 return false;
847
848 return true;
849 }
850
851 /**
852 * iavf_vlan_rx_add_vid - Add a VLAN filter to a device
853 * @netdev: network device struct
854 * @proto: unused protocol data
855 * @vid: VLAN tag
856 **/
857 static int iavf_vlan_rx_add_vid(struct net_device *netdev,
858 __always_unused __be16 proto, u16 vid)
859 {
860 struct iavf_adapter *adapter = netdev_priv(netdev);
861
862 /* Do not track VLAN 0 filter, always added by the PF on VF init */
863 if (!vid)
864 return 0;
865
866 if (!VLAN_FILTERING_ALLOWED(adapter))
867 return -EIO;
868
869 if (iavf_max_vlans_added(adapter)) {
870 netdev_err(netdev, "Max allowed VLAN filters %u. Remove existing VLANs or disable filtering via Ethtool if supported.\n",
871 iavf_get_max_vlans_allowed(adapter));
872 return -EIO;
873 }
874
875 if (!iavf_add_vlan(adapter, IAVF_VLAN(vid, be16_to_cpu(proto))))
876 return -ENOMEM;
877
878 return 0;
879 }
880
881 /**
882 * iavf_vlan_rx_kill_vid - Remove a VLAN filter from a device
883 * @netdev: network device struct
884 * @proto: unused protocol data
885 * @vid: VLAN tag
886 **/
887 static int iavf_vlan_rx_kill_vid(struct net_device *netdev,
888 __always_unused __be16 proto, u16 vid)
889 {
890 struct iavf_adapter *adapter = netdev_priv(netdev);
891
892 /* We do not track VLAN 0 filter */
893 if (!vid)
894 return 0;
895
896 iavf_del_vlan(adapter, IAVF_VLAN(vid, be16_to_cpu(proto)));
897 return 0;
898 }
899
900 /**
901 * iavf_find_filter - Search filter list for specific mac filter
902 * @adapter: board private structure
903 * @macaddr: the MAC address
904 *
905 * Returns ptr to the filter object or NULL. Must be called while holding the
906 * mac_vlan_list_lock.
907 **/
908 static struct
909 iavf_mac_filter *iavf_find_filter(struct iavf_adapter *adapter,
910 const u8 *macaddr)
911 {
912 struct iavf_mac_filter *f;
913
914 if (!macaddr)
915 return NULL;
916
917 list_for_each_entry(f, &adapter->mac_filter_list, list) {
918 if (ether_addr_equal(macaddr, f->macaddr))
919 return f;
920 }
921 return NULL;
922 }
923
924 /**
925 * iavf_add_filter - Add a mac filter to the filter list
926 * @adapter: board private structure
927 * @macaddr: the MAC address
928 *
929 * Returns ptr to the filter object or NULL when no memory available.
930 **/
931 struct iavf_mac_filter *iavf_add_filter(struct iavf_adapter *adapter,
932 const u8 *macaddr)
933 {
934 struct iavf_mac_filter *f;
935
936 if (!macaddr)
937 return NULL;
938
939 f = iavf_find_filter(adapter, macaddr);
940 if (!f) {
941 f = kzalloc_obj(*f, GFP_ATOMIC);
942 if (!f)
943 return f;
944
945 ether_addr_copy(f->macaddr, macaddr);
946
947 list_add_tail(&f->list, &adapter->mac_filter_list);
948 f->add = true;
949 f->add_handled = false;
950 f->is_new_mac = true;
951 f->is_primary = ether_addr_equal(macaddr, adapter->hw.mac.addr);
952 adapter->aq_required |= IAVF_FLAG_AQ_ADD_MAC_FILTER;
953 } else {
954 f->remove = false;
955 }
956
957 return f;
958 }
959
960 /**
961 * iavf_replace_primary_mac - Replace current primary address
962 * @adapter: board private structure
963 * @new_mac: new MAC address to be applied
964 *
965 * Replace current dev_addr and send request to PF for removal of previous
966 * primary MAC address filter and addition of new primary MAC filter.
967 * Return 0 for success, -ENOMEM for failure.
968 *
969 * Do not call this with mac_vlan_list_lock!
970 **/
971 static int iavf_replace_primary_mac(struct iavf_adapter *adapter,
972 const u8 *new_mac)
973 {
974 struct iavf_hw *hw = &adapter->hw;
975 struct iavf_mac_filter *new_f;
976 struct iavf_mac_filter *old_f;
977
978 spin_lock_bh(&adapter->mac_vlan_list_lock);
979
980 new_f = iavf_add_filter(adapter, new_mac);
981 if (!new_f) {
982 spin_unlock_bh(&adapter->mac_vlan_list_lock);
983 return -ENOMEM;
984 }
985
986 old_f = iavf_find_filter(adapter, hw->mac.addr);
987 if (old_f) {
988 old_f->is_primary = false;
989 old_f->remove = true;
990 adapter->aq_required |= IAVF_FLAG_AQ_DEL_MAC_FILTER;
991 }
992 /* Always send the request to add if changing primary MAC,
993 * even if filter is already present on the list
994 */
995 new_f->is_primary = true;
996 new_f->add = true;
997 ether_addr_copy(hw->mac.addr, new_mac);
998
999 spin_unlock_bh(&adapter->mac_vlan_list_lock);
1000
1001 /* schedule the watchdog task to immediately process the request */
1002 iavf_schedule_aq_request(adapter, IAVF_FLAG_AQ_ADD_MAC_FILTER);
1003 return 0;
1004 }
1005
1006 /**
1007 * iavf_is_mac_set_handled - wait for a response to set MAC from PF
1008 * @netdev: network interface device structure
1009 * @macaddr: MAC address to set
1010 *
1011 * Returns true on success, false on failure
1012 */
1013 static bool iavf_is_mac_set_handled(struct net_device *netdev,
1014 const u8 *macaddr)
1015 {
1016 struct iavf_adapter *adapter = netdev_priv(netdev);
1017 struct iavf_mac_filter *f;
1018 bool ret = false;
1019
1020 spin_lock_bh(&adapter->mac_vlan_list_lock);
1021
1022 f = iavf_find_filter(adapter, macaddr);
1023
1024 if (!f || (!f->add && f->add_handled))
1025 ret = true;
1026
1027 spin_unlock_bh(&adapter->mac_vlan_list_lock);
1028
1029 return ret;
1030 }
1031
1032 /**
1033 * iavf_set_mac - NDO callback to set port MAC address
1034 * @netdev: network interface device structure
1035 * @p: pointer to an address structure
1036 *
1037 * Returns 0 on success, negative on failure
1038 */
1039 static int iavf_set_mac(struct net_device *netdev, void *p)
1040 {
1041 struct iavf_adapter *adapter = netdev_priv(netdev);
1042 struct sockaddr *addr = p;
1043 int ret;
1044
1045 if (!is_valid_ether_addr(addr->sa_data))
1046 return -EADDRNOTAVAIL;
1047
1048 ret = iavf_replace_primary_mac(adapter, addr->sa_data);
1049
1050 if (ret)
1051 return ret;
1052
1053 ret = wait_event_interruptible_timeout(adapter->vc_waitqueue,
1054 iavf_is_mac_set_handled(netdev, addr->sa_data),
1055 msecs_to_jiffies(2500));
1056
1057 /* If ret < 0 then it means wait was interrupted.
1058 * If ret == 0 then it means we got a timeout.
1059 * else it means we got response for set MAC from PF,
1060 * check if netdev MAC was updated to requested MAC,
1061 * if yes then set MAC succeeded otherwise it failed return -EACCES
1062 */
1063 if (ret < 0)
1064 return ret;
1065
1066 if (!ret)
1067 return -EAGAIN;
1068
1069 if (!ether_addr_equal(netdev->dev_addr, addr->sa_data))
1070 return -EACCES;
1071
1072 return 0;
1073 }
1074
1075 /**
1076 * iavf_addr_sync - Callback for dev_(mc|uc)_sync to add address
1077 * @netdev: the netdevice
1078 * @addr: address to add
1079 *
1080 * Called by __dev_(mc|uc)_sync when an address needs to be added. We call
1081 * __dev_(uc|mc)_sync from .set_rx_mode and guarantee to hold the hash lock.
1082 */
1083 static int iavf_addr_sync(struct net_device *netdev, const u8 *addr)
1084 {
1085 struct iavf_adapter *adapter = netdev_priv(netdev);
1086
1087 if (iavf_add_filter(adapter, addr))
1088 return 0;
1089 else
1090 return -ENOMEM;
1091 }
1092
1093 /**
1094 * iavf_addr_unsync - Callback for dev_(mc|uc)_sync to remove address
1095 * @netdev: the netdevice
1096 * @addr: address to add
1097 *
1098 * Called by __dev_(mc|uc)_sync when an address needs to be removed. We call
1099 * __dev_(uc|mc)_sync from .set_rx_mode and guarantee to hold the hash lock.
1100 */
1101 static int iavf_addr_unsync(struct net_device *netdev, const u8 *addr)
1102 {
1103 struct iavf_adapter *adapter = netdev_priv(netdev);
1104 struct iavf_mac_filter *f;
1105
1106 /* Under some circumstances, we might receive a request to delete
1107 * our own device address from our uc list. Because we store the
1108 * device address in the VSI's MAC/VLAN filter list, we need to ignore
1109 * such requests and not delete our device address from this list.
1110 */
1111 if (ether_addr_equal(addr, netdev->dev_addr))
1112 return 0;
1113
1114 f = iavf_find_filter(adapter, addr);
1115 if (f) {
1116 f->remove = true;
1117 adapter->aq_required |= IAVF_FLAG_AQ_DEL_MAC_FILTER;
1118 }
1119 return 0;
1120 }
1121
1122 /**
1123 * iavf_promiscuous_mode_changed - check if promiscuous mode bits changed
1124 * @adapter: device specific adapter
1125 */
1126 bool iavf_promiscuous_mode_changed(struct iavf_adapter *adapter)
1127 {
1128 return (adapter->current_netdev_promisc_flags ^ adapter->netdev->flags) &
1129 (IFF_PROMISC | IFF_ALLMULTI);
1130 }
1131
1132 /**
1133 * iavf_set_rx_mode - NDO callback to set the netdev filters
1134 * @netdev: network interface device structure
1135 **/
1136 static void iavf_set_rx_mode(struct net_device *netdev)
1137 {
1138 struct iavf_adapter *adapter = netdev_priv(netdev);
1139
1140 spin_lock_bh(&adapter->mac_vlan_list_lock);
1141 __dev_uc_sync(netdev, iavf_addr_sync, iavf_addr_unsync);
1142 __dev_mc_sync(netdev, iavf_addr_sync, iavf_addr_unsync);
1143 spin_unlock_bh(&adapter->mac_vlan_list_lock);
1144
1145 spin_lock_bh(&adapter->current_netdev_promisc_flags_lock);
1146 if (iavf_promiscuous_mode_changed(adapter))
1147 adapter->aq_required |= IAVF_FLAG_AQ_CONFIGURE_PROMISC_MODE;
1148 spin_unlock_bh(&adapter->current_netdev_promisc_flags_lock);
1149 }
1150
1151 /**
1152 * iavf_napi_enable_all - enable NAPI on all queue vectors
1153 * @adapter: board private structure
1154 **/
1155 static void iavf_napi_enable_all(struct iavf_adapter *adapter)
1156 {
1157 int q_idx;
1158 struct iavf_q_vector *q_vector;
1159 int q_vectors = adapter->num_msix_vectors - NONQ_VECS;
1160
1161 for (q_idx = 0; q_idx < q_vectors; q_idx++) {
1162 struct napi_struct *napi;
1163
1164 q_vector = &adapter->q_vectors[q_idx];
1165 napi = &q_vector->napi;
1166 napi_enable_locked(napi);
1167 }
1168 }
1169
1170 /**
1171 * iavf_napi_disable_all - disable NAPI on all queue vectors
1172 * @adapter: board private structure
1173 **/
1174 static void iavf_napi_disable_all(struct iavf_adapter *adapter)
1175 {
1176 int q_idx;
1177 struct iavf_q_vector *q_vector;
1178 int q_vectors = adapter->num_msix_vectors - NONQ_VECS;
1179
1180 for (q_idx = 0; q_idx < q_vectors; q_idx++) {
1181 q_vector = &adapter->q_vectors[q_idx];
1182 napi_disable_locked(&q_vector->napi);
1183 }
1184 }
1185
1186 /**
1187 * iavf_configure - set up transmit and receive data structures
1188 * @adapter: board private structure
1189 **/
1190 static void iavf_configure(struct iavf_adapter *adapter)
1191 {
1192 struct net_device *netdev = adapter->netdev;
1193 int i;
1194
1195 iavf_set_rx_mode(netdev);
1196
1197 iavf_configure_tx(adapter);
1198 iavf_configure_rx(adapter);
1199 adapter->aq_required |= IAVF_FLAG_AQ_CONFIGURE_QUEUES;
1200
1201 for (i = 0; i < adapter->num_active_queues; i++) {
1202 struct iavf_ring *ring = &adapter->rx_rings[i];
1203
1204 iavf_alloc_rx_buffers(ring, IAVF_DESC_UNUSED(ring));
1205 }
1206 }
1207
1208 /**
1209 * iavf_up_complete - Finish the last steps of bringing up a connection
1210 * @adapter: board private structure
1211 */
1212 static void iavf_up_complete(struct iavf_adapter *adapter)
1213 {
1214 netdev_assert_locked(adapter->netdev);
1215
1216 iavf_change_state(adapter, __IAVF_RUNNING);
1217 clear_bit(__IAVF_VSI_DOWN, adapter->vsi.state);
1218
1219 iavf_napi_enable_all(adapter);
1220
1221 iavf_schedule_aq_request(adapter, IAVF_FLAG_AQ_ENABLE_QUEUES);
1222 }
1223
1224 /**
1225 * iavf_clear_mac_filters - Remove MAC filters not sent to PF yet and mark
1226 * others to be removed.
1227 * @adapter: board private structure
1228 **/
1229 static void iavf_clear_mac_filters(struct iavf_adapter *adapter)
1230 {
1231 struct iavf_mac_filter *f, *ftmp;
1232
1233 spin_lock_bh(&adapter->mac_vlan_list_lock);
1234 /* clear the sync flag on all filters */
1235 __dev_uc_unsync(adapter->netdev, NULL);
1236 __dev_mc_unsync(adapter->netdev, NULL);
1237
1238 /* remove all MAC filters */
1239 list_for_each_entry_safe(f, ftmp, &adapter->mac_filter_list,
1240 list) {
1241 if (f->add) {
1242 list_del(&f->list);
1243 kfree(f);
1244 } else {
1245 f->remove = true;
1246 }
1247 }
1248
1249 spin_unlock_bh(&adapter->mac_vlan_list_lock);
1250 }
1251
1252 /**
1253 * iavf_clear_cloud_filters - Remove cloud filters not sent to PF yet and
1254 * mark other to be removed.
1255 * @adapter: board private structure
1256 **/
1257 static void iavf_clear_cloud_filters(struct iavf_adapter *adapter)
1258 {
1259 struct iavf_cloud_filter *cf, *cftmp;
1260
1261 /* remove all cloud filters */
1262 spin_lock_bh(&adapter->cloud_filter_list_lock);
1263 list_for_each_entry_safe(cf, cftmp, &adapter->cloud_filter_list,
1264 list) {
1265 if (cf->add) {
1266 list_del(&cf->list);
1267 kfree(cf);
1268 adapter->num_cloud_filters--;
1269 } else {
1270 cf->del = true;
1271 }
1272 }
1273 spin_unlock_bh(&adapter->cloud_filter_list_lock);
1274 }
1275
1276 /**
1277 * iavf_clear_fdir_filters - Remove fdir filters not sent to PF yet and mark
1278 * other to be removed.
1279 * @adapter: board private structure
1280 **/
1281 static void iavf_clear_fdir_filters(struct iavf_adapter *adapter)
1282 {
1283 struct iavf_fdir_fltr *fdir;
1284
1285 /* remove all Flow Director filters */
1286 spin_lock_bh(&adapter->fdir_fltr_lock);
1287 list_for_each_entry(fdir, &adapter->fdir_list_head, list) {
1288 if (fdir->state == IAVF_FDIR_FLTR_ADD_REQUEST) {
1289 /* Cancel a request, keep filter as inactive */
1290 fdir->state = IAVF_FDIR_FLTR_INACTIVE;
1291 } else if (fdir->state == IAVF_FDIR_FLTR_ADD_PENDING ||
1292 fdir->state == IAVF_FDIR_FLTR_ACTIVE) {
1293 /* Disable filters which are active or have a pending
1294 * request to PF to be added
1295 */
1296 fdir->state = IAVF_FDIR_FLTR_DIS_REQUEST;
1297 }
1298 }
1299 spin_unlock_bh(&adapter->fdir_fltr_lock);
1300 }
1301
1302 /**
1303 * iavf_clear_adv_rss_conf - Remove adv rss conf not sent to PF yet and mark
1304 * other to be removed.
1305 * @adapter: board private structure
1306 **/
1307 static void iavf_clear_adv_rss_conf(struct iavf_adapter *adapter)
1308 {
1309 struct iavf_adv_rss *rss, *rsstmp;
1310
1311 /* remove all advance RSS configuration */
1312 spin_lock_bh(&adapter->adv_rss_lock);
1313 list_for_each_entry_safe(rss, rsstmp, &adapter->adv_rss_list_head,
1314 list) {
1315 if (rss->state == IAVF_ADV_RSS_ADD_REQUEST) {
1316 list_del(&rss->list);
1317 kfree(rss);
1318 } else {
1319 rss->state = IAVF_ADV_RSS_DEL_REQUEST;
1320 }
1321 }
1322 spin_unlock_bh(&adapter->adv_rss_lock);
1323 }
1324
1325 /**
1326 * iavf_down - Shutdown the connection processing
1327 * @adapter: board private structure
1328 */
1329 void iavf_down(struct iavf_adapter *adapter)
1330 {
1331 struct net_device *netdev = adapter->netdev;
1332
1333 netdev_assert_locked(netdev);
1334
1335 if (adapter->state <= __IAVF_DOWN_PENDING)
1336 return;
1337
1338 netif_carrier_off(netdev);
1339 netif_tx_disable(netdev);
1340 adapter->link_up = false;
1341 iavf_napi_disable_all(adapter);
1342 iavf_irq_disable(adapter);
1343
1344 iavf_clear_mac_filters(adapter);
1345 iavf_clear_cloud_filters(adapter);
1346 iavf_clear_fdir_filters(adapter);
1347 iavf_clear_adv_rss_conf(adapter);
1348
1349 if (adapter->flags & IAVF_FLAG_PF_COMMS_FAILED)
1350 return;
1351
1352 if (!test_bit(__IAVF_IN_REMOVE_TASK, &adapter->crit_section)) {
1353 /* cancel any current operation */
1354 adapter->current_op = VIRTCHNL_OP_UNKNOWN;
1355 /* Schedule operations to close down the HW. Don't wait
1356 * here for this to complete. The watchdog is still running
1357 * and it will take care of this.
1358 */
1359 if (!list_empty(&adapter->mac_filter_list))
1360 adapter->aq_required |= IAVF_FLAG_AQ_DEL_MAC_FILTER;
1361 if (!list_empty(&adapter->cloud_filter_list))
1362 adapter->aq_required |= IAVF_FLAG_AQ_DEL_CLOUD_FILTER;
1363 if (!list_empty(&adapter->fdir_list_head))
1364 adapter->aq_required |= IAVF_FLAG_AQ_DEL_FDIR_FILTER;
1365 if (!list_empty(&adapter->adv_rss_list_head))
1366 adapter->aq_required |= IAVF_FLAG_AQ_DEL_ADV_RSS_CFG;
1367 }
1368
1369 iavf_schedule_aq_request(adapter, IAVF_FLAG_AQ_DISABLE_QUEUES);
1370 }
1371
1372 /**
1373 * iavf_acquire_msix_vectors - Setup the MSIX capability
1374 * @adapter: board private structure
1375 * @vectors: number of vectors to request
1376 *
1377 * Work with the OS to set up the MSIX vectors needed.
1378 *
1379 * Returns 0 on success, negative on failure
1380 **/
1381 static int
1382 iavf_acquire_msix_vectors(struct iavf_adapter *adapter, int vectors)
1383 {
1384 int err, vector_threshold;
1385
1386 /* We'll want at least 3 (vector_threshold):
1387 * 0) Other (Admin Queue and link, mostly)
1388 * 1) TxQ[0] Cleanup
1389 * 2) RxQ[0] Cleanup
1390 */
1391 vector_threshold = MIN_MSIX_COUNT;
1392
1393 /* The more we get, the more we will assign to Tx/Rx Cleanup
1394 * for the separate queues...where Rx Cleanup >= Tx Cleanup.
1395 * Right now, we simply care about how many we'll get; we'll
1396 * set them up later while requesting irq's.
1397 */
1398 err = pci_enable_msix_range(adapter->pdev, adapter->msix_entries,
1399 vector_threshold, vectors);
1400 if (err < 0) {
1401 dev_err(&adapter->pdev->dev, "Unable to allocate MSI-X interrupts\n");
1402 kfree(adapter->msix_entries);
1403 adapter->msix_entries = NULL;
1404 return err;
1405 }
1406
1407 /* Adjust for only the vectors we'll use, which is minimum
1408 * of max_msix_q_vectors + NONQ_VECS, or the number of
1409 * vectors we were allocated.
1410 */
1411 adapter->num_msix_vectors = err;
1412 return 0;
1413 }
1414
1415 /**
1416 * iavf_free_queues - Free memory for all rings
1417 * @adapter: board private structure to initialize
1418 *
1419 * Free all of the memory associated with queue pairs.
1420 **/
1421 static void iavf_free_queues(struct iavf_adapter *adapter)
1422 {
1423 if (!adapter->vsi_res)
1424 return;
1425 adapter->num_active_queues = 0;
1426 kfree(adapter->tx_rings);
1427 adapter->tx_rings = NULL;
1428 kfree(adapter->rx_rings);
1429 adapter->rx_rings = NULL;
1430 }
1431
1432 /**
1433 * iavf_set_queue_vlan_tag_loc - set location for VLAN tag offload
1434 * @adapter: board private structure
1435 *
1436 * Based on negotiated capabilities, the VLAN tag needs to be inserted and/or
1437 * stripped in certain descriptor fields. Instead of checking the offload
1438 * capability bits in the hot path, cache the location the ring specific
1439 * flags.
1440 */
1441 void iavf_set_queue_vlan_tag_loc(struct iavf_adapter *adapter)
1442 {
1443 int i;
1444
1445 for (i = 0; i < adapter->num_active_queues; i++) {
1446 struct iavf_ring *tx_ring = &adapter->tx_rings[i];
1447 struct iavf_ring *rx_ring = &adapter->rx_rings[i];
1448
1449 /* prevent multiple L2TAG bits being set after VFR */
1450 tx_ring->flags &=
1451 ~(IAVF_TXRX_FLAGS_VLAN_TAG_LOC_L2TAG1 |
1452 IAVF_TXR_FLAGS_VLAN_TAG_LOC_L2TAG2);
1453 rx_ring->flags &=
1454 ~(IAVF_TXRX_FLAGS_VLAN_TAG_LOC_L2TAG1 |
1455 IAVF_RXR_FLAGS_VLAN_TAG_LOC_L2TAG2_2);
1456
1457 if (VLAN_ALLOWED(adapter)) {
1458 tx_ring->flags |= IAVF_TXRX_FLAGS_VLAN_TAG_LOC_L2TAG1;
1459 rx_ring->flags |= IAVF_TXRX_FLAGS_VLAN_TAG_LOC_L2TAG1;
1460 } else if (VLAN_V2_ALLOWED(adapter)) {
1461 struct virtchnl_vlan_supported_caps *stripping_support;
1462 struct virtchnl_vlan_supported_caps *insertion_support;
1463
1464 stripping_support =
1465 &adapter->vlan_v2_caps.offloads.stripping_support;
1466 insertion_support =
1467 &adapter->vlan_v2_caps.offloads.insertion_support;
1468
1469 if (stripping_support->outer) {
1470 if (stripping_support->outer &
1471 VIRTCHNL_VLAN_TAG_LOCATION_L2TAG1)
1472 rx_ring->flags |=
1473 IAVF_TXRX_FLAGS_VLAN_TAG_LOC_L2TAG1;
1474 else if (stripping_support->outer &
1475 VIRTCHNL_VLAN_TAG_LOCATION_L2TAG2_2)
1476 rx_ring->flags |=
1477 IAVF_RXR_FLAGS_VLAN_TAG_LOC_L2TAG2_2;
1478 } else if (stripping_support->inner) {
1479 if (stripping_support->inner &
1480 VIRTCHNL_VLAN_TAG_LOCATION_L2TAG1)
1481 rx_ring->flags |=
1482 IAVF_TXRX_FLAGS_VLAN_TAG_LOC_L2TAG1;
1483 else if (stripping_support->inner &
1484 VIRTCHNL_VLAN_TAG_LOCATION_L2TAG2_2)
1485 rx_ring->flags |=
1486 IAVF_RXR_FLAGS_VLAN_TAG_LOC_L2TAG2_2;
1487 }
1488
1489 if (insertion_support->outer) {
1490 if (insertion_support->outer &
1491 VIRTCHNL_VLAN_TAG_LOCATION_L2TAG1)
1492 tx_ring->flags |=
1493 IAVF_TXRX_FLAGS_VLAN_TAG_LOC_L2TAG1;
1494 else if (insertion_support->outer &
1495 VIRTCHNL_VLAN_TAG_LOCATION_L2TAG2)
1496 tx_ring->flags |=
1497 IAVF_TXR_FLAGS_VLAN_TAG_LOC_L2TAG2;
1498 } else if (insertion_support->inner) {
1499 if (insertion_support->inner &
1500 VIRTCHNL_VLAN_TAG_LOCATION_L2TAG1)
1501 tx_ring->flags |=
1502 IAVF_TXRX_FLAGS_VLAN_TAG_LOC_L2TAG1;
1503 else if (insertion_support->inner &
1504 VIRTCHNL_VLAN_TAG_LOCATION_L2TAG2)
1505 tx_ring->flags |=
1506 IAVF_TXR_FLAGS_VLAN_TAG_LOC_L2TAG2;
1507 }
1508 }
1509 }
1510 }
1511
1512 /**
1513 * iavf_alloc_queues - Allocate memory for all rings
1514 * @adapter: board private structure to initialize
1515 *
1516 * We allocate one ring per queue at run-time since we don't know the
1517 * number of queues at compile-time. The polling_netdev array is
1518 * intended for Multiqueue, but should work fine with a single queue.
1519 **/
1520 static int iavf_alloc_queues(struct iavf_adapter *adapter)
1521 {
1522 int i, num_active_queues;
1523
1524 /* If we're in reset reallocating queues we don't actually know yet for
1525 * certain the PF gave us the number of queues we asked for but we'll
1526 * assume it did. Once basic reset is finished we'll confirm once we
1527 * start negotiating config with PF.
1528 */
1529 if (adapter->num_req_queues)
1530 num_active_queues = adapter->num_req_queues;
1531 else if ((adapter->vf_res->vf_cap_flags & VIRTCHNL_VF_OFFLOAD_ADQ) &&
1532 adapter->num_tc)
1533 num_active_queues = adapter->ch_config.total_qps;
1534 else
1535 num_active_queues = min_t(int,
1536 adapter->vsi_res->num_queue_pairs,
1537 (int)(num_online_cpus()));
1538
1539
1540 adapter->tx_rings = kzalloc_objs(struct iavf_ring, num_active_queues);
1541 if (!adapter->tx_rings)
1542 goto err_out;
1543 adapter->rx_rings = kzalloc_objs(struct iavf_ring, num_active_queues);
1544 if (!adapter->rx_rings)
1545 goto err_out;
1546
1547 for (i = 0; i < num_active_queues; i++) {
1548 struct iavf_ring *tx_ring;
1549 struct iavf_ring *rx_ring;
1550
1551 tx_ring = &adapter->tx_rings[i];
1552
1553 tx_ring->queue_index = i;
1554 tx_ring->netdev = adapter->netdev;
1555 tx_ring->dev = &adapter->pdev->dev;
1556 tx_ring->count = adapter->tx_desc_count;
1557 tx_ring->itr_setting = IAVF_ITR_TX_DEF;
1558 if (adapter->flags & IAVF_FLAG_WB_ON_ITR_CAPABLE)
1559 tx_ring->flags |= IAVF_TXR_FLAGS_WB_ON_ITR;
1560
1561 rx_ring = &adapter->rx_rings[i];
1562 rx_ring->queue_index = i;
1563 rx_ring->netdev = adapter->netdev;
1564 rx_ring->count = adapter->rx_desc_count;
1565 rx_ring->itr_setting = IAVF_ITR_RX_DEF;
1566 }
1567
1568 adapter->num_active_queues = num_active_queues;
1569
1570 iavf_set_queue_vlan_tag_loc(adapter);
1571
1572 return 0;
1573
1574 err_out:
1575 iavf_free_queues(adapter);
1576 return -ENOMEM;
1577 }
1578
1579 /**
1580 * iavf_set_interrupt_capability - set MSI-X or FAIL if not supported
1581 * @adapter: board private structure to initialize
1582 *
1583 * Attempt to configure the interrupts using the best available
1584 * capabilities of the hardware and the kernel.
1585 **/
1586 static int iavf_set_interrupt_capability(struct iavf_adapter *adapter)
1587 {
1588 int vector, v_budget;
1589 int pairs = 0;
1590 int err = 0;
1591
1592 if (!adapter->vsi_res) {
1593 err = -EIO;
1594 goto out;
1595 }
1596 pairs = adapter->num_active_queues;
1597
1598 /* It's easy to be greedy for MSI-X vectors, but it really doesn't do
1599 * us much good if we have more vectors than CPUs. However, we already
1600 * limit the total number of queues by the number of CPUs so we do not
1601 * need any further limiting here.
1602 */
1603 v_budget = min_t(int, pairs + NONQ_VECS,
1604 (int)adapter->vf_res->max_vectors);
1605
1606 adapter->msix_entries = kzalloc_objs(struct msix_entry, v_budget);
1607 if (!adapter->msix_entries) {
1608 err = -ENOMEM;
1609 goto out;
1610 }
1611
1612 for (vector = 0; vector < v_budget; vector++)
1613 adapter->msix_entries[vector].entry = vector;
1614
1615 err = iavf_acquire_msix_vectors(adapter, v_budget);
1616 if (!err)
1617 iavf_schedule_finish_config(adapter);
1618
1619 out:
1620 return err;
1621 }
1622
1623 /**
1624 * iavf_config_rss_aq - Configure RSS keys and lut by using AQ commands
1625 * @adapter: board private structure
1626 *
1627 * Return 0 on success, negative on failure
1628 **/
1629 static int iavf_config_rss_aq(struct iavf_adapter *adapter)
1630 {
1631 struct iavf_aqc_get_set_rss_key_data *rss_key =
1632 (struct iavf_aqc_get_set_rss_key_data *)adapter->rss_key;
1633 struct iavf_hw *hw = &adapter->hw;
1634 enum iavf_status status;
1635
1636 if (adapter->current_op != VIRTCHNL_OP_UNKNOWN) {
1637 /* bail because we already have a command pending */
1638 dev_err(&adapter->pdev->dev, "Cannot configure RSS, command %d pending\n",
1639 adapter->current_op);
1640 return -EBUSY;
1641 }
1642
1643 status = iavf_aq_set_rss_key(hw, adapter->vsi.id, rss_key);
1644 if (status) {
1645 dev_err(&adapter->pdev->dev, "Cannot set RSS key, err %s aq_err %s\n",
1646 iavf_stat_str(hw, status),
1647 libie_aq_str(hw->aq.asq_last_status));
1648 return iavf_status_to_errno(status);
1649
1650 }
1651
1652 status = iavf_aq_set_rss_lut(hw, adapter->vsi.id, false,
1653 adapter->rss_lut, adapter->rss_lut_size);
1654 if (status) {
1655 dev_err(&adapter->pdev->dev, "Cannot set RSS lut, err %s aq_err %s\n",
1656 iavf_stat_str(hw, status),
1657 libie_aq_str(hw->aq.asq_last_status));
1658 return iavf_status_to_errno(status);
1659 }
1660
1661 return 0;
1662
1663 }
1664
1665 /**
1666 * iavf_config_rss_reg - Configure RSS keys and lut by writing registers
1667 * @adapter: board private structure
1668 *
1669 * Returns 0 on success, negative on failure
1670 **/
1671 static int iavf_config_rss_reg(struct iavf_adapter *adapter)
1672 {
1673 struct iavf_hw *hw = &adapter->hw;
1674 u32 *dw;
1675 u16 i;
1676
1677 dw = (u32 *)adapter->rss_key;
1678 for (i = 0; i < adapter->rss_key_size / 4; i++)
1679 wr32(hw, IAVF_VFQF_HKEY(i), dw[i]);
1680
1681 dw = (u32 *)adapter->rss_lut;
1682 for (i = 0; i < adapter->rss_lut_size / 4; i++)
1683 wr32(hw, IAVF_VFQF_HLUT(i), dw[i]);
1684
1685 iavf_flush(hw);
1686
1687 return 0;
1688 }
1689
1690 /**
1691 * iavf_config_rss - Configure RSS keys and lut
1692 * @adapter: board private structure
1693 *
1694 * Returns 0 on success, negative on failure
1695 **/
1696 int iavf_config_rss(struct iavf_adapter *adapter)
1697 {
1698
1699 if (RSS_PF(adapter)) {
1700 adapter->aq_required |= IAVF_FLAG_AQ_SET_RSS_LUT |
1701 IAVF_FLAG_AQ_SET_RSS_KEY;
1702 return 0;
1703 } else if (RSS_AQ(adapter)) {
1704 return iavf_config_rss_aq(adapter);
1705 } else {
1706 return iavf_config_rss_reg(adapter);
1707 }
1708 }
1709
1710 /**
1711 * iavf_fill_rss_lut - Fill the lut with default values
1712 * @adapter: board private structure
1713 **/
1714 static void iavf_fill_rss_lut(struct iavf_adapter *adapter)
1715 {
1716 u16 i;
1717
1718 for (i = 0; i < adapter->rss_lut_size; i++)
1719 adapter->rss_lut[i] = i % adapter->num_active_queues;
1720 }
1721
1722 /**
1723 * iavf_init_rss - Prepare for RSS
1724 * @adapter: board private structure
1725 *
1726 * Return 0 on success, negative on failure
1727 **/
1728 static int iavf_init_rss(struct iavf_adapter *adapter)
1729 {
1730 struct iavf_hw *hw = &adapter->hw;
1731
1732 if (!RSS_PF(adapter)) {
1733 /* Enable PCTYPES for RSS, TCP/UDP with IPv4/IPv6 */
1734 if (adapter->vf_res->vf_cap_flags &
1735 VIRTCHNL_VF_OFFLOAD_RSS_PCTYPE_V2)
1736 adapter->rss_hashcfg =
1737 IAVF_DEFAULT_RSS_HASHCFG_EXPANDED;
1738 else
1739 adapter->rss_hashcfg = IAVF_DEFAULT_RSS_HASHCFG;
1740
1741 wr32(hw, IAVF_VFQF_HENA(0), (u32)adapter->rss_hashcfg);
1742 wr32(hw, IAVF_VFQF_HENA(1), (u32)(adapter->rss_hashcfg >> 32));
1743 }
1744
1745 iavf_fill_rss_lut(adapter);
1746 netdev_rss_key_fill((void *)adapter->rss_key, adapter->rss_key_size);
1747
1748 return iavf_config_rss(adapter);
1749 }
1750
1751 /**
1752 * iavf_alloc_q_vectors - Allocate memory for interrupt vectors
1753 * @adapter: board private structure to initialize
1754 *
1755 * We allocate one q_vector per queue interrupt. If allocation fails we
1756 * return -ENOMEM.
1757 **/
1758 static int iavf_alloc_q_vectors(struct iavf_adapter *adapter)
1759 {
1760 int q_idx = 0, num_q_vectors, irq_num;
1761 struct iavf_q_vector *q_vector;
1762
1763 num_q_vectors = adapter->num_msix_vectors - NONQ_VECS;
1764 adapter->q_vectors = kzalloc_objs(*q_vector, num_q_vectors);
1765 if (!adapter->q_vectors)
1766 return -ENOMEM;
1767
1768 for (q_idx = 0; q_idx < num_q_vectors; q_idx++) {
1769 irq_num = adapter->msix_entries[q_idx + NONQ_VECS].vector;
1770 q_vector = &adapter->q_vectors[q_idx];
1771 q_vector->adapter = adapter;
1772 q_vector->vsi = &adapter->vsi;
1773 q_vector->v_idx = q_idx;
1774 q_vector->reg_idx = q_idx;
1775 netif_napi_add_config_locked(adapter->netdev, &q_vector->napi,
1776 iavf_napi_poll, q_idx);
1777 netif_napi_set_irq_locked(&q_vector->napi, irq_num);
1778 }
1779
1780 return 0;
1781 }
1782
1783 /**
1784 * iavf_free_q_vectors - Free memory allocated for interrupt vectors
1785 * @adapter: board private structure to initialize
1786 *
1787 * This function frees the memory allocated to the q_vectors. In addition if
1788 * NAPI is enabled it will delete any references to the NAPI struct prior
1789 * to freeing the q_vector.
1790 **/
1791 static void iavf_free_q_vectors(struct iavf_adapter *adapter)
1792 {
1793 int q_idx, num_q_vectors;
1794
1795 if (!adapter->q_vectors)
1796 return;
1797
1798 num_q_vectors = adapter->num_msix_vectors - NONQ_VECS;
1799
1800 for (q_idx = 0; q_idx < num_q_vectors; q_idx++) {
1801 struct iavf_q_vector *q_vector = &adapter->q_vectors[q_idx];
1802
1803 netif_napi_del_locked(&q_vector->napi);
1804 }
1805 kfree(adapter->q_vectors);
1806 adapter->q_vectors = NULL;
1807 }
1808
1809 /**
1810 * iavf_reset_interrupt_capability - Reset MSIX setup
1811 * @adapter: board private structure
1812 *
1813 **/
1814 static void iavf_reset_interrupt_capability(struct iavf_adapter *adapter)
1815 {
1816 if (!adapter->msix_entries)
1817 return;
1818
1819 pci_disable_msix(adapter->pdev);
1820 kfree(adapter->msix_entries);
1821 adapter->msix_entries = NULL;
1822 }
1823
1824 /**
1825 * iavf_init_interrupt_scheme - Determine if MSIX is supported and init
1826 * @adapter: board private structure to initialize
1827 *
1828 **/
1829 static int iavf_init_interrupt_scheme(struct iavf_adapter *adapter)
1830 {
1831 int err;
1832
1833 err = iavf_alloc_queues(adapter);
1834 if (err) {
1835 dev_err(&adapter->pdev->dev,
1836 "Unable to allocate memory for queues\n");
1837 goto err_alloc_queues;
1838 }
1839
1840 err = iavf_set_interrupt_capability(adapter);
1841 if (err) {
1842 dev_err(&adapter->pdev->dev,
1843 "Unable to setup interrupt capabilities\n");
1844 goto err_set_interrupt;
1845 }
1846
1847 err = iavf_alloc_q_vectors(adapter);
1848 if (err) {
1849 dev_err(&adapter->pdev->dev,
1850 "Unable to allocate memory for queue vectors\n");
1851 goto err_alloc_q_vectors;
1852 }
1853
1854 /* If we've made it so far while ADq flag being ON, then we haven't
1855 * bailed out anywhere in middle. And ADq isn't just enabled but actual
1856 * resources have been allocated in the reset path.
1857 * Now we can truly claim that ADq is enabled.
1858 */
1859 if ((adapter->vf_res->vf_cap_flags & VIRTCHNL_VF_OFFLOAD_ADQ) &&
1860 adapter->num_tc)
1861 dev_info(&adapter->pdev->dev, "ADq Enabled, %u TCs created",
1862 adapter->num_tc);
1863
1864 dev_info(&adapter->pdev->dev, "Multiqueue %s: Queue pair count = %u",
1865 (adapter->num_active_queues > 1) ? "Enabled" : "Disabled",
1866 adapter->num_active_queues);
1867
1868 return 0;
1869 err_alloc_q_vectors:
1870 iavf_reset_interrupt_capability(adapter);
1871 err_set_interrupt:
1872 iavf_free_queues(adapter);
1873 err_alloc_queues:
1874 return err;
1875 }
1876
1877 /**
1878 * iavf_free_interrupt_scheme - Undo what iavf_init_interrupt_scheme does
1879 * @adapter: board private structure
1880 **/
1881 static void iavf_free_interrupt_scheme(struct iavf_adapter *adapter)
1882 {
1883 iavf_free_q_vectors(adapter);
1884 iavf_reset_interrupt_capability(adapter);
1885 iavf_free_queues(adapter);
1886 }
1887
1888 /**
1889 * iavf_free_rss - Free memory used by RSS structs
1890 * @adapter: board private structure
1891 **/
1892 static void iavf_free_rss(struct iavf_adapter *adapter)
1893 {
1894 kfree(adapter->rss_key);
1895 adapter->rss_key = NULL;
1896
1897 kfree(adapter->rss_lut);
1898 adapter->rss_lut = NULL;
1899 }
1900
1901 /**
1902 * iavf_reinit_interrupt_scheme - Reallocate queues and vectors
1903 * @adapter: board private structure
1904 * @running: true if adapter->state == __IAVF_RUNNING
1905 *
1906 * Returns 0 on success, negative on failure
1907 **/
1908 static int iavf_reinit_interrupt_scheme(struct iavf_adapter *adapter, bool running)
1909 {
1910 struct net_device *netdev = adapter->netdev;
1911 int err;
1912
1913 if (running)
1914 iavf_free_traffic_irqs(adapter);
1915 iavf_free_misc_irq(adapter);
1916 iavf_free_interrupt_scheme(adapter);
1917
1918 err = iavf_init_interrupt_scheme(adapter);
1919 if (err)
1920 goto err;
1921
1922 netif_tx_stop_all_queues(netdev);
1923
1924 err = iavf_request_misc_irq(adapter);
1925 if (err)
1926 goto err;
1927
1928 set_bit(__IAVF_VSI_DOWN, adapter->vsi.state);
1929
1930 iavf_map_rings_to_vectors(adapter);
1931 err:
1932 return err;
1933 }
1934
1935 /**
1936 * iavf_finish_config - do all netdev work that needs RTNL
1937 * @work: our work_struct
1938 *
1939 * Do work that needs RTNL.
1940 */
1941 static void iavf_finish_config(struct work_struct *work)
1942 {
1943 struct iavf_adapter *adapter;
1944 bool netdev_released = false;
1945 int pairs, err;
1946
1947 adapter = container_of(work, struct iavf_adapter, finish_config);
1948
1949 /* Always take RTNL first to prevent circular lock dependency;
1950 * the dev->lock (== netdev lock) is needed to update the queue number.
1951 */
1952 rtnl_lock();
1953 netdev_lock(adapter->netdev);
1954
1955 if ((adapter->flags & IAVF_FLAG_SETUP_NETDEV_FEATURES) &&
1956 adapter->netdev->reg_state == NETREG_REGISTERED &&
1957 !test_bit(__IAVF_IN_REMOVE_TASK, &adapter->crit_section)) {
1958 netdev_update_features(adapter->netdev);
1959 adapter->flags &= ~IAVF_FLAG_SETUP_NETDEV_FEATURES;
1960 }
1961
1962 switch (adapter->state) {
1963 case __IAVF_DOWN:
1964 /* Set the real number of queues when reset occurs while
1965 * state == __IAVF_DOWN
1966 */
1967 pairs = adapter->num_active_queues;
1968 netif_set_real_num_rx_queues(adapter->netdev, pairs);
1969 netif_set_real_num_tx_queues(adapter->netdev, pairs);
1970
1971 if (adapter->netdev->reg_state != NETREG_REGISTERED) {
1972 netdev_unlock(adapter->netdev);
1973 netdev_released = true;
1974 err = register_netdevice(adapter->netdev);
1975 if (err) {
1976 dev_err(&adapter->pdev->dev, "Unable to register netdev (%d)\n",
1977 err);
1978
1979 /* go back and try again.*/
1980 netdev_lock(adapter->netdev);
1981 iavf_free_rss(adapter);
1982 iavf_free_misc_irq(adapter);
1983 iavf_reset_interrupt_capability(adapter);
1984 iavf_change_state(adapter,
1985 __IAVF_INIT_CONFIG_ADAPTER);
1986 netdev_unlock(adapter->netdev);
1987 goto out;
1988 }
1989 }
1990 break;
1991 case __IAVF_RUNNING:
1992 pairs = adapter->num_active_queues;
1993 netif_set_real_num_rx_queues(adapter->netdev, pairs);
1994 netif_set_real_num_tx_queues(adapter->netdev, pairs);
1995 break;
1996
1997 default:
1998 break;
1999 }
2000
2001 out:
2002 if (!netdev_released)
2003 netdev_unlock(adapter->netdev);
2004 rtnl_unlock();
2005 }
2006
2007 /**
2008 * iavf_schedule_finish_config - Set the flags and schedule a reset event
2009 * @adapter: board private structure
2010 **/
2011 void iavf_schedule_finish_config(struct iavf_adapter *adapter)
2012 {
2013 if (!test_bit(__IAVF_IN_REMOVE_TASK, &adapter->crit_section))
2014 queue_work(adapter->wq, &adapter->finish_config);
2015 }
2016
2017 /**
2018 * iavf_process_aq_command - process aq_required flags
2019 * and sends aq command
2020 * @adapter: pointer to iavf adapter structure
2021 *
2022 * Returns 0 on success
2023 * Returns error code if no command was sent
2024 * or error code if the command failed.
2025 **/
2026 static int iavf_process_aq_command(struct iavf_adapter *adapter)
2027 {
2028 if (adapter->aq_required & IAVF_FLAG_AQ_GET_CONFIG)
2029 return iavf_send_vf_config_msg(adapter);
2030 if (adapter->aq_required & IAVF_FLAG_AQ_GET_OFFLOAD_VLAN_V2_CAPS)
2031 return iavf_send_vf_offload_vlan_v2_msg(adapter);
2032 if (adapter->aq_required & IAVF_FLAG_AQ_GET_SUPPORTED_RXDIDS)
2033 return iavf_send_vf_supported_rxdids_msg(adapter);
2034 if (adapter->aq_required & IAVF_FLAG_AQ_GET_PTP_CAPS)
2035 return iavf_send_vf_ptp_caps_msg(adapter);
2036 if (adapter->aq_required & IAVF_FLAG_AQ_DISABLE_QUEUES) {
2037 iavf_disable_queues(adapter);
2038 return 0;
2039 }
2040
2041 if (adapter->aq_required & IAVF_FLAG_AQ_MAP_VECTORS) {
2042 iavf_map_queues(adapter);
2043 return 0;
2044 }
2045
2046 if (adapter->aq_required & IAVF_FLAG_AQ_ADD_MAC_FILTER) {
2047 iavf_add_ether_addrs(adapter);
2048 return 0;
2049 }
2050
2051 if (adapter->aq_required & IAVF_FLAG_AQ_ADD_VLAN_FILTER) {
2052 iavf_add_vlans(adapter);
2053 return 0;
2054 }
2055
2056 if (adapter->aq_required & IAVF_FLAG_AQ_DEL_MAC_FILTER) {
2057 iavf_del_ether_addrs(adapter);
2058 return 0;
2059 }
2060
2061 if (adapter->aq_required & IAVF_FLAG_AQ_DEL_VLAN_FILTER) {
2062 iavf_del_vlans(adapter);
2063 return 0;
2064 }
2065
2066 if (adapter->aq_required & IAVF_FLAG_AQ_ENABLE_VLAN_STRIPPING) {
2067 iavf_enable_vlan_stripping(adapter);
2068 return 0;
2069 }
2070
2071 if (adapter->aq_required & IAVF_FLAG_AQ_DISABLE_VLAN_STRIPPING) {
2072 iavf_disable_vlan_stripping(adapter);
2073 return 0;
2074 }
2075
2076 if (adapter->aq_required & IAVF_FLAG_AQ_CONFIGURE_QUEUES_BW) {
2077 iavf_cfg_queues_bw(adapter);
2078 return 0;
2079 }
2080
2081 if (adapter->aq_required & IAVF_FLAG_AQ_GET_QOS_CAPS) {
2082 iavf_get_qos_caps(adapter);
2083 return 0;
2084 }
2085
2086 if (adapter->aq_required & IAVF_FLAG_AQ_CFG_QUEUES_QUANTA_SIZE) {
2087 iavf_cfg_queues_quanta_size(adapter);
2088 return 0;
2089 }
2090
2091 if (adapter->aq_required & IAVF_FLAG_AQ_CONFIGURE_QUEUES) {
2092 iavf_configure_queues(adapter);
2093 return 0;
2094 }
2095
2096 if (adapter->aq_required & IAVF_FLAG_AQ_ENABLE_QUEUES) {
2097 iavf_enable_queues(adapter);
2098 return 0;
2099 }
2100
2101 if (adapter->aq_required & IAVF_FLAG_AQ_CONFIGURE_RSS) {
2102 /* This message goes straight to the firmware, not the
2103 * PF, so we don't have to set current_op as we will
2104 * not get a response through the ARQ.
2105 */
2106 adapter->aq_required &= ~IAVF_FLAG_AQ_CONFIGURE_RSS;
2107 return 0;
2108 }
2109 if (adapter->aq_required & IAVF_FLAG_AQ_GET_RSS_HASHCFG) {
2110 iavf_get_rss_hashcfg(adapter);
2111 return 0;
2112 }
2113 if (adapter->aq_required & IAVF_FLAG_AQ_SET_RSS_HASHCFG) {
2114 iavf_set_rss_hashcfg(adapter);
2115 return 0;
2116 }
2117 if (adapter->aq_required & IAVF_FLAG_AQ_SET_RSS_KEY) {
2118 iavf_set_rss_key(adapter);
2119 return 0;
2120 }
2121 if (adapter->aq_required & IAVF_FLAG_AQ_SET_RSS_LUT) {
2122 iavf_set_rss_lut(adapter);
2123 return 0;
2124 }
2125 if (adapter->aq_required & IAVF_FLAG_AQ_SET_RSS_HFUNC) {
2126 iavf_set_rss_hfunc(adapter);
2127 return 0;
2128 }
2129
2130 if (adapter->aq_required & IAVF_FLAG_AQ_CONFIGURE_PROMISC_MODE) {
2131 iavf_set_promiscuous(adapter);
2132 return 0;
2133 }
2134
2135 if (adapter->aq_required & IAVF_FLAG_AQ_ENABLE_CHANNELS) {
2136 iavf_enable_channels(adapter);
2137 return 0;
2138 }
2139
2140 if (adapter->aq_required & IAVF_FLAG_AQ_DISABLE_CHANNELS) {
2141 iavf_disable_channels(adapter);
2142 return 0;
2143 }
2144 if (adapter->aq_required & IAVF_FLAG_AQ_ADD_CLOUD_FILTER) {
2145 iavf_add_cloud_filter(adapter);
2146 return 0;
2147 }
2148 if (adapter->aq_required & IAVF_FLAG_AQ_DEL_CLOUD_FILTER) {
2149 iavf_del_cloud_filter(adapter);
2150 return 0;
2151 }
2152 if (adapter->aq_required & IAVF_FLAG_AQ_ADD_FDIR_FILTER) {
2153 iavf_add_fdir_filter(adapter);
2154 return IAVF_SUCCESS;
2155 }
2156 if (adapter->aq_required & IAVF_FLAG_AQ_DEL_FDIR_FILTER) {
2157 iavf_del_fdir_filter(adapter);
2158 return IAVF_SUCCESS;
2159 }
2160 if (adapter->aq_required & IAVF_FLAG_AQ_ADD_ADV_RSS_CFG) {
2161 iavf_add_adv_rss_cfg(adapter);
2162 return 0;
2163 }
2164 if (adapter->aq_required & IAVF_FLAG_AQ_DEL_ADV_RSS_CFG) {
2165 iavf_del_adv_rss_cfg(adapter);
2166 return 0;
2167 }
2168 if (adapter->aq_required & IAVF_FLAG_AQ_DISABLE_CTAG_VLAN_STRIPPING) {
2169 iavf_disable_vlan_stripping_v2(adapter, ETH_P_8021Q);
2170 return 0;
2171 }
2172 if (adapter->aq_required & IAVF_FLAG_AQ_DISABLE_STAG_VLAN_STRIPPING) {
2173 iavf_disable_vlan_stripping_v2(adapter, ETH_P_8021AD);
2174 return 0;
2175 }
2176 if (adapter->aq_required & IAVF_FLAG_AQ_ENABLE_CTAG_VLAN_STRIPPING) {
2177 iavf_enable_vlan_stripping_v2(adapter, ETH_P_8021Q);
2178 return 0;
2179 }
2180 if (adapter->aq_required & IAVF_FLAG_AQ_ENABLE_STAG_VLAN_STRIPPING) {
2181 iavf_enable_vlan_stripping_v2(adapter, ETH_P_8021AD);
2182 return 0;
2183 }
2184 if (adapter->aq_required & IAVF_FLAG_AQ_DISABLE_CTAG_VLAN_INSERTION) {
2185 iavf_disable_vlan_insertion_v2(adapter, ETH_P_8021Q);
2186 return 0;
2187 }
2188 if (adapter->aq_required & IAVF_FLAG_AQ_DISABLE_STAG_VLAN_INSERTION) {
2189 iavf_disable_vlan_insertion_v2(adapter, ETH_P_8021AD);
2190 return 0;
2191 }
2192 if (adapter->aq_required & IAVF_FLAG_AQ_ENABLE_CTAG_VLAN_INSERTION) {
2193 iavf_enable_vlan_insertion_v2(adapter, ETH_P_8021Q);
2194 return 0;
2195 }
2196 if (adapter->aq_required & IAVF_FLAG_AQ_ENABLE_STAG_VLAN_INSERTION) {
2197 iavf_enable_vlan_insertion_v2(adapter, ETH_P_8021AD);
2198 return 0;
2199 }
2200 if (adapter->aq_required & IAVF_FLAG_AQ_SEND_PTP_CMD) {
2201 iavf_virtchnl_send_ptp_cmd(adapter);
2202 return IAVF_SUCCESS;
2203 }
2204 if (adapter->aq_required & IAVF_FLAG_AQ_REQUEST_STATS) {
2205 iavf_request_stats(adapter);
2206 return 0;
2207 }
2208
2209 return -EAGAIN;
2210 }
2211
2212 /**
2213 * iavf_set_vlan_offload_features - set VLAN offload configuration
2214 * @adapter: board private structure
2215 * @prev_features: previous features used for comparison
2216 * @features: updated features used for configuration
2217 *
2218 * Set the aq_required bit(s) based on the requested features passed in to
2219 * configure VLAN stripping and/or VLAN insertion if supported. Also, schedule
2220 * the watchdog if any changes are requested to expedite the request via
2221 * virtchnl.
2222 **/
2223 static void
2224 iavf_set_vlan_offload_features(struct iavf_adapter *adapter,
2225 netdev_features_t prev_features,
2226 netdev_features_t features)
2227 {
2228 bool enable_stripping = true, enable_insertion = true;
2229 u16 vlan_ethertype = 0;
2230 u64 aq_required = 0;
2231
2232 /* keep cases separate because one ethertype for offloads can be
2233 * disabled at the same time as another is disabled, so check for an
2234 * enabled ethertype first, then check for disabled. Default to
2235 * ETH_P_8021Q so an ethertype is specified if disabling insertion and
2236 * stripping.
2237 */
2238 if (features & (NETIF_F_HW_VLAN_STAG_RX | NETIF_F_HW_VLAN_STAG_TX))
2239 vlan_ethertype = ETH_P_8021AD;
2240 else if (features & (NETIF_F_HW_VLAN_CTAG_RX | NETIF_F_HW_VLAN_CTAG_TX))
2241 vlan_ethertype = ETH_P_8021Q;
2242 else if (prev_features & (NETIF_F_HW_VLAN_STAG_RX | NETIF_F_HW_VLAN_STAG_TX))
2243 vlan_ethertype = ETH_P_8021AD;
2244 else if (prev_features & (NETIF_F_HW_VLAN_CTAG_RX | NETIF_F_HW_VLAN_CTAG_TX))
2245 vlan_ethertype = ETH_P_8021Q;
2246 else
2247 vlan_ethertype = ETH_P_8021Q;
2248
2249 if (!(features & (NETIF_F_HW_VLAN_STAG_RX | NETIF_F_HW_VLAN_CTAG_RX)))
2250 enable_stripping = false;
2251 if (!(features & (NETIF_F_HW_VLAN_STAG_TX | NETIF_F_HW_VLAN_CTAG_TX)))
2252 enable_insertion = false;
2253
2254 if (VLAN_ALLOWED(adapter)) {
2255 /* VIRTCHNL_VF_OFFLOAD_VLAN only has support for toggling VLAN
2256 * stripping via virtchnl. VLAN insertion can be toggled on the
2257 * netdev, but it doesn't require a virtchnl message
2258 */
2259 if (enable_stripping)
2260 aq_required |= IAVF_FLAG_AQ_ENABLE_VLAN_STRIPPING;
2261 else
2262 aq_required |= IAVF_FLAG_AQ_DISABLE_VLAN_STRIPPING;
2263
2264 } else if (VLAN_V2_ALLOWED(adapter)) {
2265 switch (vlan_ethertype) {
2266 case ETH_P_8021Q:
2267 if (enable_stripping)
2268 aq_required |= IAVF_FLAG_AQ_ENABLE_CTAG_VLAN_STRIPPING;
2269 else
2270 aq_required |= IAVF_FLAG_AQ_DISABLE_CTAG_VLAN_STRIPPING;
2271
2272 if (enable_insertion)
2273 aq_required |= IAVF_FLAG_AQ_ENABLE_CTAG_VLAN_INSERTION;
2274 else
2275 aq_required |= IAVF_FLAG_AQ_DISABLE_CTAG_VLAN_INSERTION;
2276 break;
2277 case ETH_P_8021AD:
2278 if (enable_stripping)
2279 aq_required |= IAVF_FLAG_AQ_ENABLE_STAG_VLAN_STRIPPING;
2280 else
2281 aq_required |= IAVF_FLAG_AQ_DISABLE_STAG_VLAN_STRIPPING;
2282
2283 if (enable_insertion)
2284 aq_required |= IAVF_FLAG_AQ_ENABLE_STAG_VLAN_INSERTION;
2285 else
2286 aq_required |= IAVF_FLAG_AQ_DISABLE_STAG_VLAN_INSERTION;
2287 break;
2288 }
2289 }
2290
2291 if (aq_required)
2292 iavf_schedule_aq_request(adapter, aq_required);
2293 }
2294
2295 /**
2296 * iavf_startup - first step of driver startup
2297 * @adapter: board private structure
2298 *
2299 * Function process __IAVF_STARTUP driver state.
2300 * When success the state is changed to __IAVF_INIT_VERSION_CHECK
2301 * when fails the state is changed to __IAVF_INIT_FAILED
2302 **/
2303 static void iavf_startup(struct iavf_adapter *adapter)
2304 {
2305 struct pci_dev *pdev = adapter->pdev;
2306 struct iavf_hw *hw = &adapter->hw;
2307 enum iavf_status status;
2308 int ret;
2309
2310 WARN_ON(adapter->state != __IAVF_STARTUP);
2311
2312 /* driver loaded, probe complete */
2313 adapter->flags &= ~IAVF_FLAG_PF_COMMS_FAILED;
2314 adapter->flags &= ~IAVF_FLAG_RESET_PENDING;
2315
2316 ret = iavf_check_reset_complete(hw);
2317 if (ret) {
2318 dev_info(&pdev->dev, "Device is still in reset (%d), retrying\n",
2319 ret);
2320 goto err;
2321 }
2322 hw->aq.num_arq_entries = IAVF_AQ_LEN;
2323 hw->aq.num_asq_entries = IAVF_AQ_LEN;
2324 hw->aq.arq_buf_size = IAVF_MAX_AQ_BUF_SIZE;
2325 hw->aq.asq_buf_size = IAVF_MAX_AQ_BUF_SIZE;
2326
2327 status = iavf_init_adminq(hw);
2328 if (status) {
2329 dev_err(&pdev->dev, "Failed to init Admin Queue (%d)\n",
2330 status);
2331 goto err;
2332 }
2333 ret = iavf_send_api_ver(adapter);
2334 if (ret) {
2335 dev_err(&pdev->dev, "Unable to send to PF (%d)\n", ret);
2336 iavf_shutdown_adminq(hw);
2337 goto err;
2338 }
2339 iavf_change_state(adapter, __IAVF_INIT_VERSION_CHECK);
2340 return;
2341 err:
2342 iavf_change_state(adapter, __IAVF_INIT_FAILED);
2343 }
2344
2345 /**
2346 * iavf_init_version_check - second step of driver startup
2347 * @adapter: board private structure
2348 *
2349 * Function process __IAVF_INIT_VERSION_CHECK driver state.
2350 * When success the state is changed to __IAVF_INIT_GET_RESOURCES
2351 * when fails the state is changed to __IAVF_INIT_FAILED
2352 **/
2353 static void iavf_init_version_check(struct iavf_adapter *adapter)
2354 {
2355 struct pci_dev *pdev = adapter->pdev;
2356 struct iavf_hw *hw = &adapter->hw;
2357 int err = -EAGAIN;
2358
2359 WARN_ON(adapter->state != __IAVF_INIT_VERSION_CHECK);
2360
2361 if (!iavf_asq_done(hw)) {
2362 dev_err(&pdev->dev, "Admin queue command never completed\n");
2363 iavf_shutdown_adminq(hw);
2364 iavf_change_state(adapter, __IAVF_STARTUP);
2365 goto err;
2366 }
2367
2368 /* aq msg sent, awaiting reply */
2369 err = iavf_verify_api_ver(adapter);
2370 if (err) {
2371 if (err == -EALREADY)
2372 err = iavf_send_api_ver(adapter);
2373 else
2374 dev_err(&pdev->dev, "Unsupported PF API version %d.%d, expected %d.%d\n",
2375 adapter->pf_version.major,
2376 adapter->pf_version.minor,
2377 VIRTCHNL_VERSION_MAJOR,
2378 VIRTCHNL_VERSION_MINOR);
2379 goto err;
2380 }
2381 err = iavf_send_vf_config_msg(adapter);
2382 if (err) {
2383 dev_err(&pdev->dev, "Unable to send config request (%d)\n",
2384 err);
2385 goto err;
2386 }
2387 iavf_change_state(adapter, __IAVF_INIT_GET_RESOURCES);
2388 return;
2389 err:
2390 iavf_change_state(adapter, __IAVF_INIT_FAILED);
2391 }
2392
2393 /**
2394 * iavf_parse_vf_resource_msg - parse response from VIRTCHNL_OP_GET_VF_RESOURCES
2395 * @adapter: board private structure
2396 */
2397 int iavf_parse_vf_resource_msg(struct iavf_adapter *adapter)
2398 {
2399 int i, num_req_queues = adapter->num_req_queues;
2400 struct iavf_vsi *vsi = &adapter->vsi;
2401
2402 for (i = 0; i < adapter->vf_res->num_vsis; i++) {
2403 if (adapter->vf_res->vsi_res[i].vsi_type == VIRTCHNL_VSI_SRIOV)
2404 adapter->vsi_res = &adapter->vf_res->vsi_res[i];
2405 }
2406 if (!adapter->vsi_res) {
2407 dev_err(&adapter->pdev->dev, "No LAN VSI found\n");
2408 return -ENODEV;
2409 }
2410
2411 if (num_req_queues &&
2412 num_req_queues > adapter->vsi_res->num_queue_pairs) {
2413 /* Problem. The PF gave us fewer queues than what we had
2414 * negotiated in our request. Need a reset to see if we can't
2415 * get back to a working state.
2416 */
2417 dev_err(&adapter->pdev->dev,
2418 "Requested %d queues, but PF only gave us %d.\n",
2419 num_req_queues,
2420 adapter->vsi_res->num_queue_pairs);
2421 adapter->flags |= IAVF_FLAG_REINIT_MSIX_NEEDED;
2422 adapter->num_req_queues = adapter->vsi_res->num_queue_pairs;
2423 iavf_schedule_reset(adapter, IAVF_FLAG_RESET_NEEDED);
2424
2425 return -EAGAIN;
2426 }
2427 adapter->num_req_queues = 0;
2428 adapter->vsi.id = adapter->vsi_res->vsi_id;
2429
2430 adapter->vsi.back = adapter;
2431 adapter->vsi.base_vector = 1;
2432 vsi->netdev = adapter->netdev;
2433 vsi->qs_handle = adapter->vsi_res->qset_handle;
2434 if (adapter->vf_res->vf_cap_flags & VIRTCHNL_VF_OFFLOAD_RSS_PF) {
2435 adapter->rss_key_size = adapter->vf_res->rss_key_size;
2436 adapter->rss_lut_size = adapter->vf_res->rss_lut_size;
2437 } else {
2438 adapter->rss_key_size = IAVF_HKEY_ARRAY_SIZE;
2439 adapter->rss_lut_size = IAVF_HLUT_ARRAY_SIZE;
2440 }
2441
2442 return 0;
2443 }
2444
2445 /**
2446 * iavf_init_get_resources - third step of driver startup
2447 * @adapter: board private structure
2448 *
2449 * Function process __IAVF_INIT_GET_RESOURCES driver state and
2450 * finishes driver initialization procedure.
2451 * When success the state is changed to __IAVF_DOWN
2452 * when fails the state is changed to __IAVF_INIT_FAILED
2453 **/
2454 static void iavf_init_get_resources(struct iavf_adapter *adapter)
2455 {
2456 struct pci_dev *pdev = adapter->pdev;
2457 struct iavf_hw *hw = &adapter->hw;
2458 int err;
2459
2460 WARN_ON(adapter->state != __IAVF_INIT_GET_RESOURCES);
2461 /* aq msg sent, awaiting reply */
2462 if (!adapter->vf_res) {
2463 adapter->vf_res = kzalloc(IAVF_VIRTCHNL_VF_RESOURCE_SIZE,
2464 GFP_KERNEL);
2465 if (!adapter->vf_res) {
2466 err = -ENOMEM;
2467 goto err;
2468 }
2469 }
2470 err = iavf_get_vf_config(adapter);
2471 if (err == -EALREADY) {
2472 err = iavf_send_vf_config_msg(adapter);
2473 goto err;
2474 } else if (err == -EINVAL) {
2475 /* We only get -EINVAL if the device is in a very bad
2476 * state or if we've been disabled for previous bad
2477 * behavior. Either way, we're done now.
2478 */
2479 iavf_shutdown_adminq(hw);
2480 dev_err(&pdev->dev, "Unable to get VF config due to PF error condition, not retrying\n");
2481 return;
2482 }
2483 if (err) {
2484 dev_err(&pdev->dev, "Unable to get VF config (%d)\n", err);
2485 goto err_alloc;
2486 }
2487
2488 err = iavf_parse_vf_resource_msg(adapter);
2489 if (err) {
2490 dev_err(&pdev->dev, "Failed to parse VF resource message from PF (%d)\n",
2491 err);
2492 goto err_alloc;
2493 }
2494 /* Some features require additional messages to negotiate extended
2495 * capabilities. These are processed in sequence by the
2496 * __IAVF_INIT_EXTENDED_CAPS driver state.
2497 */
2498 adapter->extended_caps = IAVF_EXTENDED_CAPS;
2499
2500 iavf_change_state(adapter, __IAVF_INIT_EXTENDED_CAPS);
2501 return;
2502
2503 err_alloc:
2504 kfree(adapter->vf_res);
2505 adapter->vf_res = NULL;
2506 err:
2507 iavf_change_state(adapter, __IAVF_INIT_FAILED);
2508 }
2509
2510 /**
2511 * iavf_init_send_offload_vlan_v2_caps - part of initializing VLAN V2 caps
2512 * @adapter: board private structure
2513 *
2514 * Function processes send of the extended VLAN V2 capability message to the
2515 * PF. Must clear IAVF_EXTENDED_CAP_RECV_VLAN_V2 if the message is not sent,
2516 * e.g. due to PF not negotiating VIRTCHNL_VF_OFFLOAD_VLAN_V2.
2517 */
2518 static void iavf_init_send_offload_vlan_v2_caps(struct iavf_adapter *adapter)
2519 {
2520 int ret;
2521
2522 WARN_ON(!(adapter->extended_caps & IAVF_EXTENDED_CAP_SEND_VLAN_V2));
2523
2524 ret = iavf_send_vf_offload_vlan_v2_msg(adapter);
2525 if (ret && ret == -EOPNOTSUPP) {
2526 /* PF does not support VIRTCHNL_VF_OFFLOAD_V2. In this case,
2527 * we did not send the capability exchange message and do not
2528 * expect a response.
2529 */
2530 adapter->extended_caps &= ~IAVF_EXTENDED_CAP_RECV_VLAN_V2;
2531 }
2532
2533 /* We sent the message, so move on to the next step */
2534 adapter->extended_caps &= ~IAVF_EXTENDED_CAP_SEND_VLAN_V2;
2535 }
2536
2537 /**
2538 * iavf_init_recv_offload_vlan_v2_caps - part of initializing VLAN V2 caps
2539 * @adapter: board private structure
2540 *
2541 * Function processes receipt of the extended VLAN V2 capability message from
2542 * the PF.
2543 **/
2544 static void iavf_init_recv_offload_vlan_v2_caps(struct iavf_adapter *adapter)
2545 {
2546 int ret;
2547
2548 WARN_ON(!(adapter->extended_caps & IAVF_EXTENDED_CAP_RECV_VLAN_V2));
2549
2550 memset(&adapter->vlan_v2_caps, 0, sizeof(adapter->vlan_v2_caps));
2551
2552 ret = iavf_get_vf_vlan_v2_caps(adapter);
2553 if (ret)
2554 goto err;
2555
2556 /* We've processed receipt of the VLAN V2 caps message */
2557 adapter->extended_caps &= ~IAVF_EXTENDED_CAP_RECV_VLAN_V2;
2558 return;
2559 err:
2560 /* We didn't receive a reply. Make sure we try sending again when
2561 * __IAVF_INIT_FAILED attempts to recover.
2562 */
2563 adapter->extended_caps |= IAVF_EXTENDED_CAP_SEND_VLAN_V2;
2564 iavf_change_state(adapter, __IAVF_INIT_FAILED);
2565 }
2566
2567 /**
2568 * iavf_init_send_supported_rxdids - part of querying for supported RXDID
2569 * formats
2570 * @adapter: board private structure
2571 *
2572 * Function processes send of the request for supported RXDIDs to the PF.
2573 * Must clear IAVF_EXTENDED_CAP_RECV_RXDID if the message is not sent, e.g.
2574 * due to the PF not negotiating VIRTCHNL_VF_OFFLOAD_RX_FLEX_DESC.
2575 */
2576 static void iavf_init_send_supported_rxdids(struct iavf_adapter *adapter)
2577 {
2578 int ret;
2579
2580 ret = iavf_send_vf_supported_rxdids_msg(adapter);
2581 if (ret == -EOPNOTSUPP) {
2582 /* PF does not support VIRTCHNL_VF_OFFLOAD_RX_FLEX_DESC. In this
2583 * case, we did not send the capability exchange message and
2584 * do not expect a response.
2585 */
2586 adapter->extended_caps &= ~IAVF_EXTENDED_CAP_RECV_RXDID;
2587 }
2588
2589 /* We sent the message, so move on to the next step */
2590 adapter->extended_caps &= ~IAVF_EXTENDED_CAP_SEND_RXDID;
2591 }
2592
2593 /**
2594 * iavf_init_recv_supported_rxdids - part of querying for supported RXDID
2595 * formats
2596 * @adapter: board private structure
2597 *
2598 * Function processes receipt of the supported RXDIDs message from the PF.
2599 **/
2600 static void iavf_init_recv_supported_rxdids(struct iavf_adapter *adapter)
2601 {
2602 int ret;
2603
2604 memset(&adapter->supp_rxdids, 0, sizeof(adapter->supp_rxdids));
2605
2606 ret = iavf_get_vf_supported_rxdids(adapter);
2607 if (ret)
2608 goto err;
2609
2610 /* We've processed the PF response to the
2611 * VIRTCHNL_OP_GET_SUPPORTED_RXDIDS message we sent previously.
2612 */
2613 adapter->extended_caps &= ~IAVF_EXTENDED_CAP_RECV_RXDID;
2614 return;
2615
2616 err:
2617 /* We didn't receive a reply. Make sure we try sending again when
2618 * __IAVF_INIT_FAILED attempts to recover.
2619 */
2620 adapter->extended_caps |= IAVF_EXTENDED_CAP_SEND_RXDID;
2621 iavf_change_state(adapter, __IAVF_INIT_FAILED);
2622 }
2623
2624 /**
2625 * iavf_init_send_ptp_caps - part of querying for extended PTP capabilities
2626 * @adapter: board private structure
2627 *
2628 * Function processes send of the request for 1588 PTP capabilities to the PF.
2629 * Must clear IAVF_EXTENDED_CAP_SEND_PTP if the message is not sent, e.g.
2630 * due to the PF not negotiating VIRTCHNL_VF_PTP_CAP
2631 */
2632 static void iavf_init_send_ptp_caps(struct iavf_adapter *adapter)
2633 {
2634 if (iavf_send_vf_ptp_caps_msg(adapter) == -EOPNOTSUPP) {
2635 /* PF does not support VIRTCHNL_VF_PTP_CAP. In this case, we
2636 * did not send the capability exchange message and do not
2637 * expect a response.
2638 */
2639 adapter->extended_caps &= ~IAVF_EXTENDED_CAP_RECV_PTP;
2640 }
2641
2642 /* We sent the message, so move on to the next step */
2643 adapter->extended_caps &= ~IAVF_EXTENDED_CAP_SEND_PTP;
2644 }
2645
2646 /**
2647 * iavf_init_recv_ptp_caps - part of querying for supported PTP capabilities
2648 * @adapter: board private structure
2649 *
2650 * Function processes receipt of the PTP capabilities supported on this VF.
2651 **/
2652 static void iavf_init_recv_ptp_caps(struct iavf_adapter *adapter)
2653 {
2654 memset(&adapter->ptp.hw_caps, 0, sizeof(adapter->ptp.hw_caps));
2655
2656 if (iavf_get_vf_ptp_caps(adapter))
2657 goto err;
2658
2659 /* We've processed the PF response to the VIRTCHNL_OP_1588_PTP_GET_CAPS
2660 * message we sent previously.
2661 */
2662 adapter->extended_caps &= ~IAVF_EXTENDED_CAP_RECV_PTP;
2663 return;
2664
2665 err:
2666 /* We didn't receive a reply. Make sure we try sending again when
2667 * __IAVF_INIT_FAILED attempts to recover.
2668 */
2669 adapter->extended_caps |= IAVF_EXTENDED_CAP_SEND_PTP;
2670 iavf_change_state(adapter, __IAVF_INIT_FAILED);
2671 }
2672
2673 /**
2674 * iavf_init_process_extended_caps - Part of driver startup
2675 * @adapter: board private structure
2676 *
2677 * Function processes __IAVF_INIT_EXTENDED_CAPS driver state. This state
2678 * handles negotiating capabilities for features which require an additional
2679 * message.
2680 *
2681 * Once all extended capabilities exchanges are finished, the driver will
2682 * transition into __IAVF_INIT_CONFIG_ADAPTER.
2683 */
2684 static void iavf_init_process_extended_caps(struct iavf_adapter *adapter)
2685 {
2686 WARN_ON(adapter->state != __IAVF_INIT_EXTENDED_CAPS);
2687
2688 /* Process capability exchange for VLAN V2 */
2689 if (adapter->extended_caps & IAVF_EXTENDED_CAP_SEND_VLAN_V2) {
2690 iavf_init_send_offload_vlan_v2_caps(adapter);
2691 return;
2692 } else if (adapter->extended_caps & IAVF_EXTENDED_CAP_RECV_VLAN_V2) {
2693 iavf_init_recv_offload_vlan_v2_caps(adapter);
2694 return;
2695 }
2696
2697 /* Process capability exchange for RXDID formats */
2698 if (adapter->extended_caps & IAVF_EXTENDED_CAP_SEND_RXDID) {
2699 iavf_init_send_supported_rxdids(adapter);
2700 return;
2701 } else if (adapter->extended_caps & IAVF_EXTENDED_CAP_RECV_RXDID) {
2702 iavf_init_recv_supported_rxdids(adapter);
2703 return;
2704 }
2705
2706 /* Process capability exchange for PTP features */
2707 if (adapter->extended_caps & IAVF_EXTENDED_CAP_SEND_PTP) {
2708 iavf_init_send_ptp_caps(adapter);
2709 return;
2710 } else if (adapter->extended_caps & IAVF_EXTENDED_CAP_RECV_PTP) {
2711 iavf_init_recv_ptp_caps(adapter);
2712 return;
2713 }
2714
2715 /* When we reach here, no further extended capabilities exchanges are
2716 * necessary, so we finally transition into __IAVF_INIT_CONFIG_ADAPTER
2717 */
2718 iavf_change_state(adapter, __IAVF_INIT_CONFIG_ADAPTER);
2719 }
2720
2721 /**
2722 * iavf_init_config_adapter - last part of driver startup
2723 * @adapter: board private structure
2724 *
2725 * After all the supported capabilities are negotiated, then the
2726 * __IAVF_INIT_CONFIG_ADAPTER state will finish driver initialization.
2727 */
2728 static void iavf_init_config_adapter(struct iavf_adapter *adapter)
2729 {
2730 struct net_device *netdev = adapter->netdev;
2731 struct pci_dev *pdev = adapter->pdev;
2732 int err;
2733
2734 WARN_ON(adapter->state != __IAVF_INIT_CONFIG_ADAPTER);
2735
2736 if (iavf_process_config(adapter))
2737 goto err;
2738
2739 adapter->current_op = VIRTCHNL_OP_UNKNOWN;
2740
2741 adapter->flags |= IAVF_FLAG_RX_CSUM_ENABLED;
2742
2743 netdev->netdev_ops = &iavf_netdev_ops;
2744 iavf_set_ethtool_ops(netdev);
2745 netdev->watchdog_timeo = 5 * HZ;
2746
2747 netdev->min_mtu = ETH_MIN_MTU;
2748
2749 /* PF/VF API: vf_res->max_mtu is max frame size (not MTU).
2750 * Convert to MTU.
2751 */
2752 if (!adapter->vf_res->max_mtu) {
2753 netdev->max_mtu = LIBIE_MAX_MTU;
2754 } else if (adapter->vf_res->max_mtu < LIBETH_RX_LL_LEN + ETH_MIN_MTU ||
2755 adapter->vf_res->max_mtu >
2756 LIBETH_RX_LL_LEN + LIBIE_MAX_MTU) {
2757 netdev_warn_once(adapter->netdev,
2758 "invalid max frame size %d from PF, using default MTU %d",
2759 adapter->vf_res->max_mtu, LIBIE_MAX_MTU);
2760 netdev->max_mtu = LIBIE_MAX_MTU;
2761 } else {
2762 netdev->max_mtu = adapter->vf_res->max_mtu - LIBETH_RX_LL_LEN;
2763 }
2764
2765 if (!is_valid_ether_addr(adapter->hw.mac.addr)) {
2766 dev_info(&pdev->dev, "Invalid MAC address %pM, using random\n",
2767 adapter->hw.mac.addr);
2768 eth_hw_addr_random(netdev);
2769 ether_addr_copy(adapter->hw.mac.addr, netdev->dev_addr);
2770 } else {
2771 eth_hw_addr_set(netdev, adapter->hw.mac.addr);
2772 ether_addr_copy(netdev->perm_addr, adapter->hw.mac.addr);
2773 }
2774
2775 adapter->tx_desc_count = IAVF_DEFAULT_TXD;
2776 adapter->rx_desc_count = IAVF_DEFAULT_RXD;
2777 err = iavf_init_interrupt_scheme(adapter);
2778 if (err)
2779 goto err_sw_init;
2780 iavf_map_rings_to_vectors(adapter);
2781 if (adapter->vf_res->vf_cap_flags &
2782 VIRTCHNL_VF_OFFLOAD_WB_ON_ITR)
2783 adapter->flags |= IAVF_FLAG_WB_ON_ITR_CAPABLE;
2784
2785 err = iavf_request_misc_irq(adapter);
2786 if (err)
2787 goto err_sw_init;
2788
2789 netif_carrier_off(netdev);
2790 adapter->link_up = false;
2791 netif_tx_stop_all_queues(netdev);
2792
2793 dev_info(&pdev->dev, "MAC address: %pM\n", adapter->hw.mac.addr);
2794 if (netdev->features & NETIF_F_GRO)
2795 dev_info(&pdev->dev, "GRO is enabled\n");
2796
2797 iavf_change_state(adapter, __IAVF_DOWN);
2798 set_bit(__IAVF_VSI_DOWN, adapter->vsi.state);
2799
2800 iavf_misc_irq_enable(adapter);
2801 wake_up(&adapter->down_waitqueue);
2802
2803 adapter->rss_key = kzalloc(adapter->rss_key_size, GFP_KERNEL);
2804 adapter->rss_lut = kzalloc(adapter->rss_lut_size, GFP_KERNEL);
2805 if (!adapter->rss_key || !adapter->rss_lut) {
2806 err = -ENOMEM;
2807 goto err_mem;
2808 }
2809 if (RSS_AQ(adapter))
2810 adapter->aq_required |= IAVF_FLAG_AQ_CONFIGURE_RSS;
2811 else
2812 iavf_init_rss(adapter);
2813
2814 if (VLAN_V2_ALLOWED(adapter))
2815 /* request initial VLAN offload settings */
2816 iavf_set_vlan_offload_features(adapter, 0, netdev->features);
2817
2818 if (QOS_ALLOWED(adapter))
2819 adapter->aq_required |= IAVF_FLAG_AQ_GET_QOS_CAPS;
2820
2821 /* Setup initial PTP configuration */
2822 iavf_ptp_init(adapter);
2823
2824 iavf_schedule_finish_config(adapter);
2825 return;
2826
2827 err_mem:
2828 iavf_free_rss(adapter);
2829 iavf_free_misc_irq(adapter);
2830 err_sw_init:
2831 iavf_reset_interrupt_capability(adapter);
2832 err:
2833 iavf_change_state(adapter, __IAVF_INIT_FAILED);
2834 }
2835
2836 static const int IAVF_NO_RESCHED = -1;
2837
2838 /* return: msec delay for requeueing itself */
2839 static int iavf_watchdog_step(struct iavf_adapter *adapter)
2840 {
2841 struct iavf_hw *hw = &adapter->hw;
2842 u32 reg_val;
2843
2844 netdev_assert_locked(adapter->netdev);
2845
2846 if (adapter->flags & IAVF_FLAG_PF_COMMS_FAILED)
2847 iavf_change_state(adapter, __IAVF_COMM_FAILED);
2848
2849 switch (adapter->state) {
2850 case __IAVF_STARTUP:
2851 iavf_startup(adapter);
2852 return 30;
2853 case __IAVF_INIT_VERSION_CHECK:
2854 iavf_init_version_check(adapter);
2855 return 30;
2856 case __IAVF_INIT_GET_RESOURCES:
2857 iavf_init_get_resources(adapter);
2858 return 1;
2859 case __IAVF_INIT_EXTENDED_CAPS:
2860 iavf_init_process_extended_caps(adapter);
2861 return 1;
2862 case __IAVF_INIT_CONFIG_ADAPTER:
2863 iavf_init_config_adapter(adapter);
2864 return 1;
2865 case __IAVF_INIT_FAILED:
2866 if (test_bit(__IAVF_IN_REMOVE_TASK,
2867 &adapter->crit_section)) {
2868 /* Do not update the state and do not reschedule
2869 * watchdog task, iavf_remove should handle this state
2870 * as it can loop forever
2871 */
2872 return IAVF_NO_RESCHED;
2873 }
2874 if (++adapter->aq_wait_count > IAVF_AQ_MAX_ERR) {
2875 dev_err(&adapter->pdev->dev,
2876 "Failed to communicate with PF; waiting before retry\n");
2877 adapter->flags |= IAVF_FLAG_PF_COMMS_FAILED;
2878 iavf_shutdown_adminq(hw);
2879 return 5000;
2880 }
2881 /* Try again from failed step*/
2882 iavf_change_state(adapter, adapter->last_state);
2883 return 1000;
2884 case __IAVF_COMM_FAILED:
2885 if (test_bit(__IAVF_IN_REMOVE_TASK,
2886 &adapter->crit_section)) {
2887 /* Set state to __IAVF_INIT_FAILED and perform remove
2888 * steps. Remove IAVF_FLAG_PF_COMMS_FAILED so the task
2889 * doesn't bring the state back to __IAVF_COMM_FAILED.
2890 */
2891 iavf_change_state(adapter, __IAVF_INIT_FAILED);
2892 adapter->flags &= ~IAVF_FLAG_PF_COMMS_FAILED;
2893 return IAVF_NO_RESCHED;
2894 }
2895 reg_val = rd32(hw, IAVF_VFGEN_RSTAT) &
2896 IAVF_VFGEN_RSTAT_VFR_STATE_MASK;
2897 if (reg_val == VIRTCHNL_VFR_VFACTIVE ||
2898 reg_val == VIRTCHNL_VFR_COMPLETED) {
2899 /* A chance for redemption! */
2900 dev_err(&adapter->pdev->dev,
2901 "Hardware came out of reset. Attempting reinit.\n");
2902 /* When init task contacts the PF and
2903 * gets everything set up again, it'll restart the
2904 * watchdog for us. Down, boy. Sit. Stay. Woof.
2905 */
2906 iavf_change_state(adapter, __IAVF_STARTUP);
2907 adapter->flags &= ~IAVF_FLAG_PF_COMMS_FAILED;
2908 }
2909 adapter->aq_required = 0;
2910 adapter->current_op = VIRTCHNL_OP_UNKNOWN;
2911 return 10;
2912 case __IAVF_RESETTING:
2913 return 2000;
2914 case __IAVF_DOWN:
2915 case __IAVF_DOWN_PENDING:
2916 case __IAVF_TESTING:
2917 case __IAVF_RUNNING:
2918 if (adapter->current_op) {
2919 if (!iavf_asq_done(hw)) {
2920 dev_dbg(&adapter->pdev->dev,
2921 "Admin queue timeout\n");
2922 iavf_send_api_ver(adapter);
2923 }
2924 } else {
2925 int ret = iavf_process_aq_command(adapter);
2926
2927 /* An error will be returned if no commands were
2928 * processed; use this opportunity to update stats
2929 * if the error isn't -ENOTSUPP
2930 */
2931 if (ret && ret != -EOPNOTSUPP &&
2932 adapter->state == __IAVF_RUNNING)
2933 iavf_request_stats(adapter);
2934 }
2935 if (adapter->state == __IAVF_RUNNING)
2936 iavf_detect_recover_hung(&adapter->vsi);
2937 break;
2938 case __IAVF_REMOVE:
2939 default:
2940 return IAVF_NO_RESCHED;
2941 }
2942
2943 /* check for hw reset */
2944 reg_val = rd32(hw, IAVF_VF_ARQLEN1) & IAVF_VF_ARQLEN1_ARQENABLE_MASK;
2945 if (!reg_val) {
2946 adapter->aq_required = 0;
2947 adapter->current_op = VIRTCHNL_OP_UNKNOWN;
2948 dev_err(&adapter->pdev->dev, "Hardware reset detected\n");
2949 iavf_schedule_reset(adapter, IAVF_FLAG_RESET_PENDING);
2950 }
2951
2952 return adapter->aq_required ? 20 : 2000;
2953 }
2954
2955 static void iavf_watchdog_task(struct work_struct *work)
2956 {
2957 struct iavf_adapter *adapter = container_of(work,
2958 struct iavf_adapter,
2959 watchdog_task.work);
2960 struct net_device *netdev = adapter->netdev;
2961 int msec_delay;
2962
2963 netdev_lock(netdev);
2964 msec_delay = iavf_watchdog_step(adapter);
2965 /* note that we schedule a different task */
2966 if (adapter->state >= __IAVF_DOWN)
2967 queue_work(adapter->wq, &adapter->adminq_task);
2968
2969 if (msec_delay != IAVF_NO_RESCHED)
2970 queue_delayed_work(adapter->wq, &adapter->watchdog_task,
2971 msecs_to_jiffies(msec_delay));
2972 netdev_unlock(netdev);
2973 }
2974
2975 /**
2976 * iavf_disable_vf - disable VF
2977 * @adapter: board private structure
2978 *
2979 * Set communication failed flag and free all resources.
2980 */
2981 static void iavf_disable_vf(struct iavf_adapter *adapter)
2982 {
2983 struct iavf_mac_filter *f, *ftmp;
2984 struct iavf_vlan_filter *fv, *fvtmp;
2985 struct iavf_cloud_filter *cf, *cftmp;
2986
2987 netdev_assert_locked(adapter->netdev);
2988
2989 adapter->flags |= IAVF_FLAG_PF_COMMS_FAILED;
2990
2991 iavf_ptp_release(adapter);
2992
2993 /* We don't use netif_running() because it may be true prior to
2994 * ndo_open() returning, so we can't assume it means all our open
2995 * tasks have finished, since we're not holding the rtnl_lock here.
2996 */
2997 if (adapter->state == __IAVF_RUNNING) {
2998 set_bit(__IAVF_VSI_DOWN, adapter->vsi.state);
2999 netif_carrier_off(adapter->netdev);
3000 netif_tx_disable(adapter->netdev);
3001 adapter->link_up = false;
3002 iavf_napi_disable_all(adapter);
3003 iavf_irq_disable(adapter);
3004 iavf_free_traffic_irqs(adapter);
3005 iavf_free_all_tx_resources(adapter);
3006 iavf_free_all_rx_resources(adapter);
3007 }
3008
3009 spin_lock_bh(&adapter->mac_vlan_list_lock);
3010
3011 /* Delete all of the filters */
3012 list_for_each_entry_safe(f, ftmp, &adapter->mac_filter_list, list) {
3013 list_del(&f->list);
3014 kfree(f);
3015 }
3016
3017 list_for_each_entry_safe(fv, fvtmp, &adapter->vlan_filter_list, list) {
3018 list_del(&fv->list);
3019 kfree(fv);
3020 }
3021 adapter->num_vlan_filters = 0;
3022
3023 spin_unlock_bh(&adapter->mac_vlan_list_lock);
3024
3025 spin_lock_bh(&adapter->cloud_filter_list_lock);
3026 list_for_each_entry_safe(cf, cftmp, &adapter->cloud_filter_list, list) {
3027 list_del(&cf->list);
3028 kfree(cf);
3029 adapter->num_cloud_filters--;
3030 }
3031 spin_unlock_bh(&adapter->cloud_filter_list_lock);
3032
3033 iavf_free_misc_irq(adapter);
3034 iavf_free_interrupt_scheme(adapter);
3035 memset(adapter->vf_res, 0, IAVF_VIRTCHNL_VF_RESOURCE_SIZE);
3036 iavf_shutdown_adminq(&adapter->hw);
3037 adapter->flags &= ~IAVF_FLAG_RESET_PENDING;
3038 iavf_change_state(adapter, __IAVF_DOWN);
3039 wake_up(&adapter->down_waitqueue);
3040 dev_info(&adapter->pdev->dev, "Reset task did not complete, VF disabled\n");
3041 }
3042
3043 /**
3044 * iavf_reconfig_qs_bw - Call-back task to handle hardware reset
3045 * @adapter: board private structure
3046 *
3047 * After a reset, the shaper parameters of queues need to be replayed again.
3048 * Since the net_shaper object inside TX rings persists across reset,
3049 * set the update flag for all queues so that the virtchnl message is triggered
3050 * for all queues.
3051 **/
3052 static void iavf_reconfig_qs_bw(struct iavf_adapter *adapter)
3053 {
3054 int i, num = 0;
3055
3056 for (i = 0; i < adapter->num_active_queues; i++)
3057 if (adapter->tx_rings[i].q_shaper.bw_min ||
3058 adapter->tx_rings[i].q_shaper.bw_max) {
3059 adapter->tx_rings[i].q_shaper_update = true;
3060 num++;
3061 }
3062
3063 if (num)
3064 adapter->aq_required |= IAVF_FLAG_AQ_CONFIGURE_QUEUES_BW;
3065 }
3066
3067 /**
3068 * iavf_reset_step - Perform the VF reset sequence
3069 * @adapter: board private structure
3070 *
3071 * Requests a reset from PF, polls for completion, and reconfigures
3072 * the driver. Caller must hold the netdev instance lock.
3073 *
3074 * This can sleep for several seconds while polling HW registers.
3075 */
3076 void iavf_reset_step(struct iavf_adapter *adapter)
3077 {
3078 struct virtchnl_vf_resource *vfres = adapter->vf_res;
3079 struct net_device *netdev = adapter->netdev;
3080 struct iavf_hw *hw = &adapter->hw;
3081 struct iavf_mac_filter *f, *ftmp;
3082 struct iavf_cloud_filter *cf;
3083 enum iavf_status status;
3084 u32 reg_val;
3085 int i = 0, err;
3086 bool running;
3087
3088 netdev_assert_locked(netdev);
3089
3090 iavf_misc_irq_disable(adapter);
3091 if (adapter->flags & IAVF_FLAG_RESET_NEEDED) {
3092 adapter->flags &= ~IAVF_FLAG_RESET_NEEDED;
3093 /* Restart the AQ here. If we have been reset but didn't
3094 * detect it, or if the PF had to reinit, our AQ will be hosed.
3095 */
3096 iavf_shutdown_adminq(hw);
3097 iavf_init_adminq(hw);
3098 iavf_request_reset(adapter);
3099 }
3100 adapter->flags |= IAVF_FLAG_RESET_PENDING;
3101
3102 /* poll until we see the reset actually happen */
3103 for (i = 0; i < IAVF_RESET_WAIT_DETECTED_COUNT; i++) {
3104 reg_val = rd32(hw, IAVF_VF_ARQLEN1) &
3105 IAVF_VF_ARQLEN1_ARQENABLE_MASK;
3106 if (!reg_val)
3107 break;
3108 usleep_range(5000, 10000);
3109 }
3110 if (i == IAVF_RESET_WAIT_DETECTED_COUNT) {
3111 dev_info(&adapter->pdev->dev, "Never saw reset\n");
3112 goto continue_reset; /* act like the reset happened */
3113 }
3114
3115 /* wait until the reset is complete and the PF is responding to us */
3116 for (i = 0; i < IAVF_RESET_WAIT_COMPLETE_COUNT; i++) {
3117 /* sleep first to make sure a minimum wait time is met */
3118 msleep(IAVF_RESET_WAIT_MS);
3119
3120 reg_val = rd32(hw, IAVF_VFGEN_RSTAT) &
3121 IAVF_VFGEN_RSTAT_VFR_STATE_MASK;
3122 if (reg_val == VIRTCHNL_VFR_VFACTIVE)
3123 break;
3124 }
3125
3126 pci_set_master(adapter->pdev);
3127 pci_restore_msi_state(adapter->pdev);
3128
3129 if (i == IAVF_RESET_WAIT_COMPLETE_COUNT) {
3130 dev_err(&adapter->pdev->dev, "Reset never finished (%x)\n",
3131 reg_val);
3132 iavf_disable_vf(adapter);
3133 return; /* Do not attempt to reinit. It's dead, Jim. */
3134 }
3135
3136 continue_reset:
3137 /* If we are still early in the state machine, just restart. */
3138 if (adapter->state <= __IAVF_INIT_FAILED) {
3139 iavf_shutdown_adminq(hw);
3140 iavf_change_state(adapter, __IAVF_STARTUP);
3141 iavf_startup(adapter);
3142 queue_delayed_work(adapter->wq, &adapter->watchdog_task,
3143 msecs_to_jiffies(30));
3144 return;
3145 }
3146
3147 /* We don't use netif_running() because it may be true prior to
3148 * ndo_open() returning, so we can't assume it means all our open
3149 * tasks have finished, since we're not holding the rtnl_lock here.
3150 */
3151 running = adapter->state == __IAVF_RUNNING;
3152
3153 if (running) {
3154 netif_carrier_off(netdev);
3155 netif_tx_stop_all_queues(netdev);
3156 adapter->link_up = false;
3157 iavf_napi_disable_all(adapter);
3158 }
3159 iavf_irq_disable(adapter);
3160
3161 iavf_change_state(adapter, __IAVF_RESETTING);
3162 adapter->flags &= ~IAVF_FLAG_RESET_PENDING;
3163
3164 iavf_ptp_release(adapter);
3165
3166 /* free the Tx/Rx rings and descriptors, might be better to just
3167 * re-use them sometime in the future
3168 */
3169 iavf_free_all_rx_resources(adapter);
3170 iavf_free_all_tx_resources(adapter);
3171
3172 adapter->flags |= IAVF_FLAG_QUEUES_DISABLED;
3173 /* kill and reinit the admin queue */
3174 iavf_shutdown_adminq(hw);
3175 adapter->current_op = VIRTCHNL_OP_UNKNOWN;
3176 status = iavf_init_adminq(hw);
3177 if (status) {
3178 dev_info(&adapter->pdev->dev, "Failed to init adminq: %d\n",
3179 status);
3180 goto reset_err;
3181 }
3182 adapter->aq_required = 0;
3183
3184 if ((adapter->flags & IAVF_FLAG_REINIT_MSIX_NEEDED) ||
3185 (adapter->flags & IAVF_FLAG_REINIT_ITR_NEEDED)) {
3186 err = iavf_reinit_interrupt_scheme(adapter, running);
3187 if (err)
3188 goto reset_err;
3189 }
3190
3191 if (RSS_AQ(adapter)) {
3192 adapter->aq_required |= IAVF_FLAG_AQ_CONFIGURE_RSS;
3193 } else {
3194 err = iavf_init_rss(adapter);
3195 if (err)
3196 goto reset_err;
3197 }
3198
3199 adapter->aq_required |= IAVF_FLAG_AQ_GET_CONFIG;
3200 adapter->aq_required |= IAVF_FLAG_AQ_MAP_VECTORS;
3201
3202 /* Certain capabilities require an extended negotiation process using
3203 * extra messages that must be processed after getting the VF
3204 * configuration. The related checks such as VLAN_V2_ALLOWED() are not
3205 * reliable here, since the configuration has not yet been negotiated.
3206 *
3207 * Always set these flags, since them related VIRTCHNL messages won't
3208 * be sent until after VIRTCHNL_OP_GET_VF_RESOURCES.
3209 */
3210 adapter->aq_required |= IAVF_FLAG_AQ_EXTENDED_CAPS;
3211
3212 spin_lock_bh(&adapter->mac_vlan_list_lock);
3213
3214 /* Delete filter for the current MAC address, it could have
3215 * been changed by the PF via administratively set MAC.
3216 * Will be re-added via VIRTCHNL_OP_GET_VF_RESOURCES.
3217 */
3218 list_for_each_entry_safe(f, ftmp, &adapter->mac_filter_list, list) {
3219 if (ether_addr_equal(f->macaddr, adapter->hw.mac.addr)) {
3220 list_del(&f->list);
3221 kfree(f);
3222 }
3223 }
3224 /* re-add all MAC filters */
3225 list_for_each_entry(f, &adapter->mac_filter_list, list) {
3226 f->add = true;
3227 }
3228 spin_unlock_bh(&adapter->mac_vlan_list_lock);
3229
3230 /* check if TCs are running and re-add all cloud filters */
3231 spin_lock_bh(&adapter->cloud_filter_list_lock);
3232 if ((vfres->vf_cap_flags & VIRTCHNL_VF_OFFLOAD_ADQ) &&
3233 adapter->num_tc) {
3234 list_for_each_entry(cf, &adapter->cloud_filter_list, list) {
3235 cf->add = true;
3236 }
3237 }
3238 spin_unlock_bh(&adapter->cloud_filter_list_lock);
3239
3240 adapter->aq_required |= IAVF_FLAG_AQ_ADD_MAC_FILTER;
3241 adapter->aq_required |= IAVF_FLAG_AQ_ADD_CLOUD_FILTER;
3242 iavf_misc_irq_enable(adapter);
3243
3244 mod_delayed_work(adapter->wq, &adapter->watchdog_task, 2);
3245
3246 /* We were running when the reset started, so we need to restore some
3247 * state here.
3248 */
3249 if (running) {
3250 /* allocate transmit descriptors */
3251 err = iavf_setup_all_tx_resources(adapter);
3252 if (err)
3253 goto reset_err;
3254
3255 /* allocate receive descriptors */
3256 err = iavf_setup_all_rx_resources(adapter);
3257 if (err)
3258 goto reset_err;
3259
3260 if ((adapter->flags & IAVF_FLAG_REINIT_MSIX_NEEDED) ||
3261 (adapter->flags & IAVF_FLAG_REINIT_ITR_NEEDED)) {
3262 err = iavf_request_traffic_irqs(adapter, netdev->name);
3263 if (err)
3264 goto reset_err;
3265
3266 adapter->flags &= ~IAVF_FLAG_REINIT_MSIX_NEEDED;
3267 }
3268
3269 iavf_configure(adapter);
3270
3271 /* iavf_up_complete() will switch device back
3272 * to __IAVF_RUNNING
3273 */
3274 iavf_up_complete(adapter);
3275
3276 iavf_irq_enable(adapter, true);
3277
3278 iavf_reconfig_qs_bw(adapter);
3279 } else {
3280 iavf_change_state(adapter, __IAVF_DOWN);
3281 wake_up(&adapter->down_waitqueue);
3282 }
3283
3284 adapter->flags &= ~IAVF_FLAG_REINIT_ITR_NEEDED;
3285
3286 return;
3287 reset_err:
3288 if (running) {
3289 set_bit(__IAVF_VSI_DOWN, adapter->vsi.state);
3290 iavf_free_traffic_irqs(adapter);
3291 }
3292 iavf_disable_vf(adapter);
3293
3294 dev_err(&adapter->pdev->dev, "failed to allocate resources during reinit\n");
3295 }
3296
3297 static void iavf_reset_task(struct work_struct *work)
3298 {
3299 struct iavf_adapter *adapter = container_of(work,
3300 struct iavf_adapter,
3301 reset_task);
3302 struct net_device *netdev = adapter->netdev;
3303
3304 netdev_lock(netdev);
3305 iavf_reset_step(adapter);
3306 netdev_unlock(netdev);
3307 }
3308
3309 /**
3310 * iavf_adminq_task - worker thread to clean the admin queue
3311 * @work: pointer to work_struct containing our data
3312 **/
3313 static void iavf_adminq_task(struct work_struct *work)
3314 {
3315 struct iavf_adapter *adapter =
3316 container_of(work, struct iavf_adapter, adminq_task);
3317 struct net_device *netdev = adapter->netdev;
3318 struct iavf_hw *hw = &adapter->hw;
3319 struct iavf_arq_event_info event;
3320 enum virtchnl_ops v_op;
3321 enum iavf_status ret, v_ret;
3322 u32 val, oldval;
3323 u16 pending;
3324
3325 netdev_lock(netdev);
3326
3327 if (adapter->flags & IAVF_FLAG_PF_COMMS_FAILED)
3328 goto unlock;
3329
3330 event.buf_len = IAVF_MAX_AQ_BUF_SIZE;
3331 event.msg_buf = kzalloc(event.buf_len, GFP_KERNEL);
3332 if (!event.msg_buf)
3333 goto unlock;
3334
3335 do {
3336 ret = iavf_clean_arq_element(hw, &event, &pending);
3337 v_op = (enum virtchnl_ops)le32_to_cpu(event.desc.cookie_high);
3338 v_ret = (enum iavf_status)le32_to_cpu(event.desc.cookie_low);
3339
3340 if (ret || !v_op)
3341 break; /* No event to process or error cleaning ARQ */
3342
3343 iavf_virtchnl_completion(adapter, v_op, v_ret, event.msg_buf,
3344 event.msg_len);
3345 if (pending != 0)
3346 memset(event.msg_buf, 0, IAVF_MAX_AQ_BUF_SIZE);
3347 } while (pending);
3348
3349 if (iavf_is_reset_in_progress(adapter))
3350 goto freedom;
3351
3352 /* check for error indications */
3353 val = rd32(hw, IAVF_VF_ARQLEN1);
3354 if (val == 0xdeadbeef || val == 0xffffffff) /* device in reset */
3355 goto freedom;
3356 oldval = val;
3357 if (val & IAVF_VF_ARQLEN1_ARQVFE_MASK) {
3358 dev_info(&adapter->pdev->dev, "ARQ VF Error detected\n");
3359 val &= ~IAVF_VF_ARQLEN1_ARQVFE_MASK;
3360 }
3361 if (val & IAVF_VF_ARQLEN1_ARQOVFL_MASK) {
3362 dev_info(&adapter->pdev->dev, "ARQ Overflow Error detected\n");
3363 val &= ~IAVF_VF_ARQLEN1_ARQOVFL_MASK;
3364 }
3365 if (val & IAVF_VF_ARQLEN1_ARQCRIT_MASK) {
3366 dev_info(&adapter->pdev->dev, "ARQ Critical Error detected\n");
3367 val &= ~IAVF_VF_ARQLEN1_ARQCRIT_MASK;
3368 }
3369 if (oldval != val)
3370 wr32(hw, IAVF_VF_ARQLEN1, val);
3371
3372 val = rd32(hw, IAVF_VF_ATQLEN1);
3373 oldval = val;
3374 if (val & IAVF_VF_ATQLEN1_ATQVFE_MASK) {
3375 dev_info(&adapter->pdev->dev, "ASQ VF Error detected\n");
3376 val &= ~IAVF_VF_ATQLEN1_ATQVFE_MASK;
3377 }
3378 if (val & IAVF_VF_ATQLEN1_ATQOVFL_MASK) {
3379 dev_info(&adapter->pdev->dev, "ASQ Overflow Error detected\n");
3380 val &= ~IAVF_VF_ATQLEN1_ATQOVFL_MASK;
3381 }
3382 if (val & IAVF_VF_ATQLEN1_ATQCRIT_MASK) {
3383 dev_info(&adapter->pdev->dev, "ASQ Critical Error detected\n");
3384 val &= ~IAVF_VF_ATQLEN1_ATQCRIT_MASK;
3385 }
3386 if (oldval != val)
3387 wr32(hw, IAVF_VF_ATQLEN1, val);
3388
3389 freedom:
3390 kfree(event.msg_buf);
3391 unlock:
3392 netdev_unlock(netdev);
3393 /* re-enable Admin queue interrupt cause */
3394 iavf_misc_irq_enable(adapter);
3395 }
3396
3397 /**
3398 * iavf_free_all_tx_resources - Free Tx Resources for All Queues
3399 * @adapter: board private structure
3400 *
3401 * Free all transmit software resources
3402 **/
3403 void iavf_free_all_tx_resources(struct iavf_adapter *adapter)
3404 {
3405 int i;
3406
3407 if (!adapter->tx_rings)
3408 return;
3409
3410 for (i = 0; i < adapter->num_active_queues; i++)
3411 if (adapter->tx_rings[i].desc)
3412 iavf_free_tx_resources(&adapter->tx_rings[i]);
3413 }
3414
3415 /**
3416 * iavf_setup_all_tx_resources - allocate all queues Tx resources
3417 * @adapter: board private structure
3418 *
3419 * If this function returns with an error, then it's possible one or
3420 * more of the rings is populated (while the rest are not). It is the
3421 * callers duty to clean those orphaned rings.
3422 *
3423 * Return 0 on success, negative on failure
3424 **/
3425 static int iavf_setup_all_tx_resources(struct iavf_adapter *adapter)
3426 {
3427 int i, err = 0;
3428
3429 for (i = 0; i < adapter->num_active_queues; i++) {
3430 adapter->tx_rings[i].count = adapter->tx_desc_count;
3431 err = iavf_setup_tx_descriptors(&adapter->tx_rings[i]);
3432 if (!err)
3433 continue;
3434 dev_err(&adapter->pdev->dev,
3435 "Allocation for Tx Queue %u failed\n", i);
3436 break;
3437 }
3438
3439 return err;
3440 }
3441
3442 /**
3443 * iavf_setup_all_rx_resources - allocate all queues Rx resources
3444 * @adapter: board private structure
3445 *
3446 * If this function returns with an error, then it's possible one or
3447 * more of the rings is populated (while the rest are not). It is the
3448 * callers duty to clean those orphaned rings.
3449 *
3450 * Return 0 on success, negative on failure
3451 **/
3452 static int iavf_setup_all_rx_resources(struct iavf_adapter *adapter)
3453 {
3454 int i, err = 0;
3455
3456 for (i = 0; i < adapter->num_active_queues; i++) {
3457 adapter->rx_rings[i].count = adapter->rx_desc_count;
3458 err = iavf_setup_rx_descriptors(&adapter->rx_rings[i]);
3459 if (!err)
3460 continue;
3461 dev_err(&adapter->pdev->dev,
3462 "Allocation for Rx Queue %u failed\n", i);
3463 break;
3464 }
3465 return err;
3466 }
3467
3468 /**
3469 * iavf_free_all_rx_resources - Free Rx Resources for All Queues
3470 * @adapter: board private structure
3471 *
3472 * Free all receive software resources
3473 **/
3474 void iavf_free_all_rx_resources(struct iavf_adapter *adapter)
3475 {
3476 int i;
3477
3478 if (!adapter->rx_rings)
3479 return;
3480
3481 for (i = 0; i < adapter->num_active_queues; i++)
3482 if (adapter->rx_rings[i].desc)
3483 iavf_free_rx_resources(&adapter->rx_rings[i]);
3484 }
3485
3486 /**
3487 * iavf_validate_tx_bandwidth - validate the max Tx bandwidth
3488 * @adapter: board private structure
3489 * @max_tx_rate: max Tx bw for a tc
3490 **/
3491 static int iavf_validate_tx_bandwidth(struct iavf_adapter *adapter,
3492 u64 max_tx_rate)
3493 {
3494 int speed = 0, ret = 0;
3495
3496 if (ADV_LINK_SUPPORT(adapter)) {
3497 if (adapter->link_speed_mbps < U32_MAX) {
3498 speed = adapter->link_speed_mbps;
3499 goto validate_bw;
3500 } else {
3501 dev_err(&adapter->pdev->dev, "Unknown link speed\n");
3502 return -EINVAL;
3503 }
3504 }
3505
3506 switch (adapter->link_speed) {
3507 case VIRTCHNL_LINK_SPEED_40GB:
3508 speed = SPEED_40000;
3509 break;
3510 case VIRTCHNL_LINK_SPEED_25GB:
3511 speed = SPEED_25000;
3512 break;
3513 case VIRTCHNL_LINK_SPEED_20GB:
3514 speed = SPEED_20000;
3515 break;
3516 case VIRTCHNL_LINK_SPEED_10GB:
3517 speed = SPEED_10000;
3518 break;
3519 case VIRTCHNL_LINK_SPEED_5GB:
3520 speed = SPEED_5000;
3521 break;
3522 case VIRTCHNL_LINK_SPEED_2_5GB:
3523 speed = SPEED_2500;
3524 break;
3525 case VIRTCHNL_LINK_SPEED_1GB:
3526 speed = SPEED_1000;
3527 break;
3528 case VIRTCHNL_LINK_SPEED_100MB:
3529 speed = SPEED_100;
3530 break;
3531 default:
3532 break;
3533 }
3534
3535 validate_bw:
3536 if (max_tx_rate > speed) {
3537 dev_err(&adapter->pdev->dev,
3538 "Invalid tx rate specified\n");
3539 ret = -EINVAL;
3540 }
3541
3542 return ret;
3543 }
3544
3545 /**
3546 * iavf_validate_ch_config - validate queue mapping info
3547 * @adapter: board private structure
3548 * @mqprio_qopt: queue parameters
3549 *
3550 * This function validates if the config provided by the user to
3551 * configure queue channels is valid or not. Returns 0 on a valid
3552 * config.
3553 **/
3554 static int iavf_validate_ch_config(struct iavf_adapter *adapter,
3555 struct tc_mqprio_qopt_offload *mqprio_qopt)
3556 {
3557 u64 total_max_rate = 0;
3558 u32 tx_rate_rem = 0;
3559 int i, num_qps = 0;
3560 u64 tx_rate = 0;
3561 int ret = 0;
3562
3563 if (mqprio_qopt->qopt.num_tc > IAVF_MAX_TRAFFIC_CLASS ||
3564 mqprio_qopt->qopt.num_tc < 1)
3565 return -EINVAL;
3566
3567 for (i = 0; i <= mqprio_qopt->qopt.num_tc - 1; i++) {
3568 if (!mqprio_qopt->qopt.count[i] ||
3569 mqprio_qopt->qopt.offset[i] != num_qps)
3570 return -EINVAL;
3571 if (mqprio_qopt->min_rate[i]) {
3572 dev_err(&adapter->pdev->dev,
3573 "Invalid min tx rate (greater than 0) specified for TC%d\n",
3574 i);
3575 return -EINVAL;
3576 }
3577
3578 /* convert to Mbps */
3579 tx_rate = div_u64(mqprio_qopt->max_rate[i],
3580 IAVF_MBPS_DIVISOR);
3581
3582 if (mqprio_qopt->max_rate[i] &&
3583 tx_rate < IAVF_MBPS_QUANTA) {
3584 dev_err(&adapter->pdev->dev,
3585 "Invalid max tx rate for TC%d, minimum %dMbps\n",
3586 i, IAVF_MBPS_QUANTA);
3587 return -EINVAL;
3588 }
3589
3590 (void)div_u64_rem(tx_rate, IAVF_MBPS_QUANTA, &tx_rate_rem);
3591
3592 if (tx_rate_rem != 0) {
3593 dev_err(&adapter->pdev->dev,
3594 "Invalid max tx rate for TC%d, not divisible by %d\n",
3595 i, IAVF_MBPS_QUANTA);
3596 return -EINVAL;
3597 }
3598
3599 total_max_rate += tx_rate;
3600 num_qps += mqprio_qopt->qopt.count[i];
3601 }
3602 if (num_qps > adapter->num_active_queues) {
3603 dev_err(&adapter->pdev->dev,
3604 "Cannot support requested number of queues\n");
3605 return -EINVAL;
3606 }
3607
3608 ret = iavf_validate_tx_bandwidth(adapter, total_max_rate);
3609 return ret;
3610 }
3611
3612 /**
3613 * iavf_del_all_cloud_filters - delete all cloud filters on the traffic classes
3614 * @adapter: board private structure
3615 **/
3616 static void iavf_del_all_cloud_filters(struct iavf_adapter *adapter)
3617 {
3618 struct iavf_cloud_filter *cf, *cftmp;
3619
3620 spin_lock_bh(&adapter->cloud_filter_list_lock);
3621 list_for_each_entry_safe(cf, cftmp, &adapter->cloud_filter_list,
3622 list) {
3623 list_del(&cf->list);
3624 kfree(cf);
3625 adapter->num_cloud_filters--;
3626 }
3627 spin_unlock_bh(&adapter->cloud_filter_list_lock);
3628 }
3629
3630 /**
3631 * iavf_is_tc_config_same - Compare the mqprio TC config with the
3632 * TC config already configured on this adapter.
3633 * @adapter: board private structure
3634 * @mqprio_qopt: TC config received from kernel.
3635 *
3636 * This function compares the TC config received from the kernel
3637 * with the config already configured on the adapter.
3638 *
3639 * Return: True if configuration is same, false otherwise.
3640 **/
3641 static bool iavf_is_tc_config_same(struct iavf_adapter *adapter,
3642 struct tc_mqprio_qopt *mqprio_qopt)
3643 {
3644 struct virtchnl_channel_info *ch = &adapter->ch_config.ch_info[0];
3645 int i;
3646
3647 if (adapter->num_tc != mqprio_qopt->num_tc)
3648 return false;
3649
3650 for (i = 0; i < adapter->num_tc; i++) {
3651 if (ch[i].count != mqprio_qopt->count[i] ||
3652 ch[i].offset != mqprio_qopt->offset[i])
3653 return false;
3654 }
3655 return true;
3656 }
3657
3658 /**
3659 * __iavf_setup_tc - configure multiple traffic classes
3660 * @netdev: network interface device structure
3661 * @type_data: tc offload data
3662 *
3663 * This function processes the config information provided by the
3664 * user to configure traffic classes/queue channels and packages the
3665 * information to request the PF to setup traffic classes.
3666 *
3667 * Returns 0 on success.
3668 **/
3669 static int __iavf_setup_tc(struct net_device *netdev, void *type_data)
3670 {
3671 struct tc_mqprio_qopt_offload *mqprio_qopt = type_data;
3672 struct iavf_adapter *adapter = netdev_priv(netdev);
3673 struct virtchnl_vf_resource *vfres = adapter->vf_res;
3674 u8 num_tc = 0, total_qps = 0;
3675 int ret = 0, netdev_tc = 0;
3676 u64 max_tx_rate;
3677 u16 mode;
3678 int i;
3679
3680 num_tc = mqprio_qopt->qopt.num_tc;
3681 mode = mqprio_qopt->mode;
3682
3683 /* delete queue_channel */
3684 if (!mqprio_qopt->qopt.hw) {
3685 if (adapter->ch_config.state == __IAVF_TC_RUNNING) {
3686 /* reset the tc configuration */
3687 netdev_reset_tc(netdev);
3688 adapter->num_tc = 0;
3689 netif_tx_stop_all_queues(netdev);
3690 netif_tx_disable(netdev);
3691 iavf_del_all_cloud_filters(adapter);
3692 adapter->aq_required = IAVF_FLAG_AQ_DISABLE_CHANNELS;
3693 total_qps = adapter->orig_num_active_queues;
3694 goto exit;
3695 } else {
3696 return -EINVAL;
3697 }
3698 }
3699
3700 /* add queue channel */
3701 if (mode == TC_MQPRIO_MODE_CHANNEL) {
3702 if (!(vfres->vf_cap_flags & VIRTCHNL_VF_OFFLOAD_ADQ)) {
3703 dev_err(&adapter->pdev->dev, "ADq not supported\n");
3704 return -EOPNOTSUPP;
3705 }
3706 if (adapter->ch_config.state != __IAVF_TC_INVALID) {
3707 dev_err(&adapter->pdev->dev, "TC configuration already exists\n");
3708 return -EINVAL;
3709 }
3710
3711 ret = iavf_validate_ch_config(adapter, mqprio_qopt);
3712 if (ret)
3713 return ret;
3714 /* Return if same TC config is requested */
3715 if (iavf_is_tc_config_same(adapter, &mqprio_qopt->qopt))
3716 return 0;
3717 adapter->num_tc = num_tc;
3718
3719 for (i = 0; i < IAVF_MAX_TRAFFIC_CLASS; i++) {
3720 if (i < num_tc) {
3721 adapter->ch_config.ch_info[i].count =
3722 mqprio_qopt->qopt.count[i];
3723 adapter->ch_config.ch_info[i].offset =
3724 mqprio_qopt->qopt.offset[i];
3725 total_qps += mqprio_qopt->qopt.count[i];
3726 max_tx_rate = mqprio_qopt->max_rate[i];
3727 /* convert to Mbps */
3728 max_tx_rate = div_u64(max_tx_rate,
3729 IAVF_MBPS_DIVISOR);
3730 adapter->ch_config.ch_info[i].max_tx_rate =
3731 max_tx_rate;
3732 } else {
3733 adapter->ch_config.ch_info[i].count = 1;
3734 adapter->ch_config.ch_info[i].offset = 0;
3735 }
3736 }
3737
3738 /* Take snapshot of original config such as "num_active_queues"
3739 * It is used later when delete ADQ flow is exercised, so that
3740 * once delete ADQ flow completes, VF shall go back to its
3741 * original queue configuration
3742 */
3743
3744 adapter->orig_num_active_queues = adapter->num_active_queues;
3745
3746 /* Store queue info based on TC so that VF gets configured
3747 * with correct number of queues when VF completes ADQ config
3748 * flow
3749 */
3750 adapter->ch_config.total_qps = total_qps;
3751
3752 netif_tx_stop_all_queues(netdev);
3753 netif_tx_disable(netdev);
3754 adapter->aq_required |= IAVF_FLAG_AQ_ENABLE_CHANNELS;
3755 netdev_reset_tc(netdev);
3756 /* Report the tc mapping up the stack */
3757 netdev_set_num_tc(adapter->netdev, num_tc);
3758 for (i = 0; i < IAVF_MAX_TRAFFIC_CLASS; i++) {
3759 u16 qcount = mqprio_qopt->qopt.count[i];
3760 u16 qoffset = mqprio_qopt->qopt.offset[i];
3761
3762 if (i < num_tc)
3763 netdev_set_tc_queue(netdev, netdev_tc++, qcount,
3764 qoffset);
3765 }
3766 }
3767 exit:
3768 if (test_bit(__IAVF_IN_REMOVE_TASK, &adapter->crit_section))
3769 return 0;
3770
3771 netif_set_real_num_rx_queues(netdev, total_qps);
3772 netif_set_real_num_tx_queues(netdev, total_qps);
3773
3774 return ret;
3775 }
3776
3777 /**
3778 * iavf_parse_cls_flower - Parse tc flower filters provided by kernel
3779 * @adapter: board private structure
3780 * @f: pointer to struct flow_cls_offload
3781 * @filter: pointer to cloud filter structure
3782 */
3783 static int iavf_parse_cls_flower(struct iavf_adapter *adapter,
3784 struct flow_cls_offload *f,
3785 struct iavf_cloud_filter *filter)
3786 {
3787 struct flow_rule *rule = flow_cls_offload_flow_rule(f);
3788 struct flow_dissector *dissector = rule->match.dissector;
3789 u16 n_proto_mask = 0;
3790 u16 n_proto_key = 0;
3791 u8 field_flags = 0;
3792 u16 addr_type = 0;
3793 u16 n_proto = 0;
3794 int i = 0;
3795 struct virtchnl_filter *vf = &filter->f;
3796
3797 if (dissector->used_keys &
3798 ~(BIT_ULL(FLOW_DISSECTOR_KEY_CONTROL) |
3799 BIT_ULL(FLOW_DISSECTOR_KEY_BASIC) |
3800 BIT_ULL(FLOW_DISSECTOR_KEY_ETH_ADDRS) |
3801 BIT_ULL(FLOW_DISSECTOR_KEY_VLAN) |
3802 BIT_ULL(FLOW_DISSECTOR_KEY_IPV4_ADDRS) |
3803 BIT_ULL(FLOW_DISSECTOR_KEY_IPV6_ADDRS) |
3804 BIT_ULL(FLOW_DISSECTOR_KEY_PORTS) |
3805 BIT_ULL(FLOW_DISSECTOR_KEY_ENC_KEYID))) {
3806 dev_err(&adapter->pdev->dev, "Unsupported key used: 0x%llx\n",
3807 dissector->used_keys);
3808 return -EOPNOTSUPP;
3809 }
3810
3811 if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_ENC_KEYID)) {
3812 struct flow_match_enc_keyid match;
3813
3814 flow_rule_match_enc_keyid(rule, &match);
3815 if (match.mask->keyid != 0)
3816 field_flags |= IAVF_CLOUD_FIELD_TEN_ID;
3817 }
3818
3819 if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_BASIC)) {
3820 struct flow_match_basic match;
3821
3822 flow_rule_match_basic(rule, &match);
3823 n_proto_key = ntohs(match.key->n_proto);
3824 n_proto_mask = ntohs(match.mask->n_proto);
3825
3826 if (n_proto_key == ETH_P_ALL) {
3827 n_proto_key = 0;
3828 n_proto_mask = 0;
3829 }
3830 n_proto = n_proto_key & n_proto_mask;
3831 if (n_proto != ETH_P_IP && n_proto != ETH_P_IPV6)
3832 return -EINVAL;
3833 if (n_proto == ETH_P_IPV6) {
3834 /* specify flow type as TCP IPv6 */
3835 vf->flow_type = VIRTCHNL_TCP_V6_FLOW;
3836 }
3837
3838 if (match.key->ip_proto != IPPROTO_TCP) {
3839 dev_info(&adapter->pdev->dev, "Only TCP transport is supported\n");
3840 return -EINVAL;
3841 }
3842 }
3843
3844 if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_ETH_ADDRS)) {
3845 struct flow_match_eth_addrs match;
3846
3847 flow_rule_match_eth_addrs(rule, &match);
3848
3849 /* use is_broadcast and is_zero to check for all 0xf or 0 */
3850 if (!is_zero_ether_addr(match.mask->dst)) {
3851 if (is_broadcast_ether_addr(match.mask->dst)) {
3852 field_flags |= IAVF_CLOUD_FIELD_OMAC;
3853 } else {
3854 dev_err(&adapter->pdev->dev, "Bad ether dest mask %pM\n",
3855 match.mask->dst);
3856 return -EINVAL;
3857 }
3858 }
3859
3860 if (!is_zero_ether_addr(match.mask->src)) {
3861 if (is_broadcast_ether_addr(match.mask->src)) {
3862 field_flags |= IAVF_CLOUD_FIELD_IMAC;
3863 } else {
3864 dev_err(&adapter->pdev->dev, "Bad ether src mask %pM\n",
3865 match.mask->src);
3866 return -EINVAL;
3867 }
3868 }
3869
3870 if (!is_zero_ether_addr(match.key->dst))
3871 if (is_valid_ether_addr(match.key->dst) ||
3872 is_multicast_ether_addr(match.key->dst)) {
3873 /* set the mask if a valid dst_mac address */
3874 for (i = 0; i < ETH_ALEN; i++)
3875 vf->mask.tcp_spec.dst_mac[i] |= 0xff;
3876 ether_addr_copy(vf->data.tcp_spec.dst_mac,
3877 match.key->dst);
3878 }
3879
3880 if (!is_zero_ether_addr(match.key->src))
3881 if (is_valid_ether_addr(match.key->src) ||
3882 is_multicast_ether_addr(match.key->src)) {
3883 /* set the mask if a valid dst_mac address */
3884 for (i = 0; i < ETH_ALEN; i++)
3885 vf->mask.tcp_spec.src_mac[i] |= 0xff;
3886 ether_addr_copy(vf->data.tcp_spec.src_mac,
3887 match.key->src);
3888 }
3889 }
3890
3891 if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_VLAN)) {
3892 struct flow_match_vlan match;
3893
3894 flow_rule_match_vlan(rule, &match);
3895 if (match.mask->vlan_id) {
3896 if (match.mask->vlan_id == VLAN_VID_MASK) {
3897 field_flags |= IAVF_CLOUD_FIELD_IVLAN;
3898 } else {
3899 dev_err(&adapter->pdev->dev, "Bad vlan mask %u\n",
3900 match.mask->vlan_id);
3901 return -EINVAL;
3902 }
3903 }
3904 vf->mask.tcp_spec.vlan_id |= cpu_to_be16(0xffff);
3905 vf->data.tcp_spec.vlan_id = cpu_to_be16(match.key->vlan_id);
3906 }
3907
3908 if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_CONTROL)) {
3909 struct flow_match_control match;
3910
3911 flow_rule_match_control(rule, &match);
3912 addr_type = match.key->addr_type;
3913
3914 if (flow_rule_has_control_flags(match.mask->flags,
3915 f->common.extack))
3916 return -EOPNOTSUPP;
3917 }
3918
3919 if (addr_type == FLOW_DISSECTOR_KEY_IPV4_ADDRS) {
3920 struct flow_match_ipv4_addrs match;
3921
3922 flow_rule_match_ipv4_addrs(rule, &match);
3923 if (match.mask->dst) {
3924 if (match.mask->dst == cpu_to_be32(0xffffffff)) {
3925 field_flags |= IAVF_CLOUD_FIELD_IIP;
3926 } else {
3927 dev_err(&adapter->pdev->dev, "Bad ip dst mask 0x%08x\n",
3928 be32_to_cpu(match.mask->dst));
3929 return -EINVAL;
3930 }
3931 }
3932
3933 if (match.mask->src) {
3934 if (match.mask->src == cpu_to_be32(0xffffffff)) {
3935 field_flags |= IAVF_CLOUD_FIELD_IIP;
3936 } else {
3937 dev_err(&adapter->pdev->dev, "Bad ip src mask 0x%08x\n",
3938 be32_to_cpu(match.mask->src));
3939 return -EINVAL;
3940 }
3941 }
3942
3943 if (field_flags & IAVF_CLOUD_FIELD_TEN_ID) {
3944 dev_info(&adapter->pdev->dev, "Tenant id not allowed for ip filter\n");
3945 return -EINVAL;
3946 }
3947 if (match.key->dst) {
3948 vf->mask.tcp_spec.dst_ip[0] |= cpu_to_be32(0xffffffff);
3949 vf->data.tcp_spec.dst_ip[0] = match.key->dst;
3950 }
3951 if (match.key->src) {
3952 vf->mask.tcp_spec.src_ip[0] |= cpu_to_be32(0xffffffff);
3953 vf->data.tcp_spec.src_ip[0] = match.key->src;
3954 }
3955 }
3956
3957 if (addr_type == FLOW_DISSECTOR_KEY_IPV6_ADDRS) {
3958 struct flow_match_ipv6_addrs match;
3959
3960 flow_rule_match_ipv6_addrs(rule, &match);
3961
3962 /* validate mask, make sure it is not IPV6_ADDR_ANY */
3963 if (ipv6_addr_any(&match.mask->dst)) {
3964 dev_err(&adapter->pdev->dev, "Bad ipv6 dst mask 0x%02x\n",
3965 IPV6_ADDR_ANY);
3966 return -EINVAL;
3967 }
3968
3969 /* src and dest IPv6 address should not be LOOPBACK
3970 * (0:0:0:0:0:0:0:1) which can be represented as ::1
3971 */
3972 if (ipv6_addr_loopback(&match.key->dst) ||
3973 ipv6_addr_loopback(&match.key->src)) {
3974 dev_err(&adapter->pdev->dev,
3975 "ipv6 addr should not be loopback\n");
3976 return -EINVAL;
3977 }
3978 if (!ipv6_addr_any(&match.mask->dst) ||
3979 !ipv6_addr_any(&match.mask->src))
3980 field_flags |= IAVF_CLOUD_FIELD_IIP;
3981
3982 for (i = 0; i < 4; i++)
3983 vf->mask.tcp_spec.dst_ip[i] |= cpu_to_be32(0xffffffff);
3984 memcpy(&vf->data.tcp_spec.dst_ip, &match.key->dst.s6_addr32,
3985 sizeof(vf->data.tcp_spec.dst_ip));
3986 for (i = 0; i < 4; i++)
3987 vf->mask.tcp_spec.src_ip[i] |= cpu_to_be32(0xffffffff);
3988 memcpy(&vf->data.tcp_spec.src_ip, &match.key->src.s6_addr32,
3989 sizeof(vf->data.tcp_spec.src_ip));
3990 }
3991 if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_PORTS)) {
3992 struct flow_match_ports match;
3993
3994 flow_rule_match_ports(rule, &match);
3995 if (match.mask->src) {
3996 if (match.mask->src == cpu_to_be16(0xffff)) {
3997 field_flags |= IAVF_CLOUD_FIELD_IIP;
3998 } else {
3999 dev_err(&adapter->pdev->dev, "Bad src port mask %u\n",
4000 be16_to_cpu(match.mask->src));
4001 return -EINVAL;
4002 }
4003 }
4004
4005 if (match.mask->dst) {
4006 if (match.mask->dst == cpu_to_be16(0xffff)) {
4007 field_flags |= IAVF_CLOUD_FIELD_IIP;
4008 } else {
4009 dev_err(&adapter->pdev->dev, "Bad dst port mask %u\n",
4010 be16_to_cpu(match.mask->dst));
4011 return -EINVAL;
4012 }
4013 }
4014 if (match.key->dst) {
4015 vf->mask.tcp_spec.dst_port |= cpu_to_be16(0xffff);
4016 vf->data.tcp_spec.dst_port = match.key->dst;
4017 }
4018
4019 if (match.key->src) {
4020 vf->mask.tcp_spec.src_port |= cpu_to_be16(0xffff);
4021 vf->data.tcp_spec.src_port = match.key->src;
4022 }
4023 }
4024 vf->field_flags = field_flags;
4025
4026 return 0;
4027 }
4028
4029 /**
4030 * iavf_handle_tclass - Forward to a traffic class on the device
4031 * @adapter: board private structure
4032 * @tc: traffic class index on the device
4033 * @filter: pointer to cloud filter structure
4034 */
4035 static int iavf_handle_tclass(struct iavf_adapter *adapter, u32 tc,
4036 struct iavf_cloud_filter *filter)
4037 {
4038 if (tc == 0)
4039 return 0;
4040 if (tc < adapter->num_tc) {
4041 if (!filter->f.data.tcp_spec.dst_port) {
4042 dev_err(&adapter->pdev->dev,
4043 "Specify destination port to redirect to traffic class other than TC0\n");
4044 return -EINVAL;
4045 }
4046 }
4047 /* redirect to a traffic class on the same device */
4048 filter->f.action = VIRTCHNL_ACTION_TC_REDIRECT;
4049 filter->f.action_meta = tc;
4050 return 0;
4051 }
4052
4053 /**
4054 * iavf_find_cf - Find the cloud filter in the list
4055 * @adapter: Board private structure
4056 * @cookie: filter specific cookie
4057 *
4058 * Returns ptr to the filter object or NULL. Must be called while holding the
4059 * cloud_filter_list_lock.
4060 */
4061 static struct iavf_cloud_filter *iavf_find_cf(struct iavf_adapter *adapter,
4062 unsigned long *cookie)
4063 {
4064 struct iavf_cloud_filter *filter = NULL;
4065
4066 if (!cookie)
4067 return NULL;
4068
4069 list_for_each_entry(filter, &adapter->cloud_filter_list, list) {
4070 if (!memcmp(cookie, &filter->cookie, sizeof(filter->cookie)))
4071 return filter;
4072 }
4073 return NULL;
4074 }
4075
4076 /**
4077 * iavf_configure_clsflower - Add tc flower filters
4078 * @adapter: board private structure
4079 * @cls_flower: Pointer to struct flow_cls_offload
4080 */
4081 static int iavf_configure_clsflower(struct iavf_adapter *adapter,
4082 struct flow_cls_offload *cls_flower)
4083 {
4084 int tc = tc_classid_to_hwtc(adapter->netdev, cls_flower->classid);
4085 struct iavf_cloud_filter *filter;
4086 int err;
4087
4088 if (tc < 0) {
4089 dev_err(&adapter->pdev->dev, "Invalid traffic class\n");
4090 return -EINVAL;
4091 }
4092
4093 filter = kzalloc_obj(*filter);
4094 if (!filter)
4095 return -ENOMEM;
4096 filter->cookie = cls_flower->cookie;
4097
4098 netdev_lock(adapter->netdev);
4099
4100 /* bail out here if filter already exists */
4101 spin_lock_bh(&adapter->cloud_filter_list_lock);
4102 if (iavf_find_cf(adapter, &cls_flower->cookie)) {
4103 dev_err(&adapter->pdev->dev, "Failed to add TC Flower filter, it already exists\n");
4104 err = -EEXIST;
4105 goto spin_unlock;
4106 }
4107 spin_unlock_bh(&adapter->cloud_filter_list_lock);
4108
4109 /* set the mask to all zeroes to begin with */
4110 memset(&filter->f.mask.tcp_spec, 0, sizeof(struct virtchnl_l4_spec));
4111 /* start out with flow type and eth type IPv4 to begin with */
4112 filter->f.flow_type = VIRTCHNL_TCP_V4_FLOW;
4113 err = iavf_parse_cls_flower(adapter, cls_flower, filter);
4114 if (err)
4115 goto err;
4116
4117 err = iavf_handle_tclass(adapter, tc, filter);
4118 if (err)
4119 goto err;
4120
4121 /* add filter to the list */
4122 spin_lock_bh(&adapter->cloud_filter_list_lock);
4123 list_add_tail(&filter->list, &adapter->cloud_filter_list);
4124 adapter->num_cloud_filters++;
4125 filter->add = true;
4126 adapter->aq_required |= IAVF_FLAG_AQ_ADD_CLOUD_FILTER;
4127 spin_unlock:
4128 spin_unlock_bh(&adapter->cloud_filter_list_lock);
4129 err:
4130 if (err)
4131 kfree(filter);
4132
4133 netdev_unlock(adapter->netdev);
4134 return err;
4135 }
4136
4137 /**
4138 * iavf_delete_clsflower - Remove tc flower filters
4139 * @adapter: board private structure
4140 * @cls_flower: Pointer to struct flow_cls_offload
4141 */
4142 static int iavf_delete_clsflower(struct iavf_adapter *adapter,
4143 struct flow_cls_offload *cls_flower)
4144 {
4145 struct iavf_cloud_filter *filter = NULL;
4146 int err = 0;
4147
4148 spin_lock_bh(&adapter->cloud_filter_list_lock);
4149 filter = iavf_find_cf(adapter, &cls_flower->cookie);
4150 if (filter) {
4151 filter->del = true;
4152 adapter->aq_required |= IAVF_FLAG_AQ_DEL_CLOUD_FILTER;
4153 } else {
4154 err = -EINVAL;
4155 }
4156 spin_unlock_bh(&adapter->cloud_filter_list_lock);
4157
4158 return err;
4159 }
4160
4161 /**
4162 * iavf_setup_tc_cls_flower - flower classifier offloads
4163 * @adapter: pointer to iavf adapter structure
4164 * @cls_flower: pointer to flow_cls_offload struct with flow info
4165 */
4166 static int iavf_setup_tc_cls_flower(struct iavf_adapter *adapter,
4167 struct flow_cls_offload *cls_flower)
4168 {
4169 switch (cls_flower->command) {
4170 case FLOW_CLS_REPLACE:
4171 return iavf_configure_clsflower(adapter, cls_flower);
4172 case FLOW_CLS_DESTROY:
4173 return iavf_delete_clsflower(adapter, cls_flower);
4174 case FLOW_CLS_STATS:
4175 return -EOPNOTSUPP;
4176 default:
4177 return -EOPNOTSUPP;
4178 }
4179 }
4180
4181 /**
4182 * iavf_add_cls_u32 - Add U32 classifier offloads
4183 * @adapter: pointer to iavf adapter structure
4184 * @cls_u32: pointer to tc_cls_u32_offload struct with flow info
4185 *
4186 * Return: 0 on success or negative errno on failure.
4187 */
4188 static int iavf_add_cls_u32(struct iavf_adapter *adapter,
4189 struct tc_cls_u32_offload *cls_u32)
4190 {
4191 struct netlink_ext_ack *extack = cls_u32->common.extack;
4192 struct virtchnl_fdir_rule *rule_cfg;
4193 struct virtchnl_filter_action *vact;
4194 struct virtchnl_proto_hdrs *hdrs;
4195 struct ethhdr *spec_h, *mask_h;
4196 const struct tc_action *act;
4197 struct iavf_fdir_fltr *fltr;
4198 struct tcf_exts *exts;
4199 unsigned int q_index;
4200 int i, status = 0;
4201 int off_base = 0;
4202
4203 if (cls_u32->knode.link_handle) {
4204 NL_SET_ERR_MSG_MOD(extack, "Linking not supported");
4205 return -EOPNOTSUPP;
4206 }
4207
4208 fltr = kzalloc_obj(*fltr);
4209 if (!fltr)
4210 return -ENOMEM;
4211
4212 rule_cfg = &fltr->vc_add_msg.rule_cfg;
4213 hdrs = &rule_cfg->proto_hdrs;
4214 hdrs->count = 0;
4215
4216 /* The parser lib at the PF expects the packet starting with MAC hdr */
4217 switch (ntohs(cls_u32->common.protocol)) {
4218 case ETH_P_802_3:
4219 break;
4220 case ETH_P_IP:
4221 spec_h = (struct ethhdr *)hdrs->raw.spec;
4222 mask_h = (struct ethhdr *)hdrs->raw.mask;
4223 spec_h->h_proto = htons(ETH_P_IP);
4224 mask_h->h_proto = htons(0xFFFF);
4225 off_base += ETH_HLEN;
4226 break;
4227 default:
4228 NL_SET_ERR_MSG_MOD(extack, "Only 802_3 and ip filter protocols are supported");
4229 status = -EOPNOTSUPP;
4230 goto free_alloc;
4231 }
4232
4233 for (i = 0; i < cls_u32->knode.sel->nkeys; i++) {
4234 __be32 val, mask;
4235 int off;
4236
4237 off = off_base + cls_u32->knode.sel->keys[i].off;
4238 val = cls_u32->knode.sel->keys[i].val;
4239 mask = cls_u32->knode.sel->keys[i].mask;
4240
4241 if (off >= sizeof(hdrs->raw.spec)) {
4242 NL_SET_ERR_MSG_MOD(extack, "Input exceeds maximum allowed.");
4243 status = -EINVAL;
4244 goto free_alloc;
4245 }
4246
4247 memcpy(&hdrs->raw.spec[off], &val, sizeof(val));
4248 memcpy(&hdrs->raw.mask[off], &mask, sizeof(mask));
4249 hdrs->raw.pkt_len = off + sizeof(val);
4250 }
4251
4252 /* Only one action is allowed */
4253 rule_cfg->action_set.count = 1;
4254 vact = &rule_cfg->action_set.actions[0];
4255 exts = cls_u32->knode.exts;
4256
4257 tcf_exts_for_each_action(i, act, exts) {
4258 /* FDIR queue */
4259 if (is_tcf_skbedit_rx_queue_mapping(act)) {
4260 q_index = tcf_skbedit_rx_queue_mapping(act);
4261 if (q_index >= adapter->num_active_queues) {
4262 status = -EINVAL;
4263 goto free_alloc;
4264 }
4265
4266 vact->type = VIRTCHNL_ACTION_QUEUE;
4267 vact->act_conf.queue.index = q_index;
4268 break;
4269 }
4270
4271 /* Drop */
4272 if (is_tcf_gact_shot(act)) {
4273 vact->type = VIRTCHNL_ACTION_DROP;
4274 break;
4275 }
4276
4277 /* Unsupported action */
4278 NL_SET_ERR_MSG_MOD(extack, "Unsupported action.");
4279 status = -EOPNOTSUPP;
4280 goto free_alloc;
4281 }
4282
4283 fltr->vc_add_msg.vsi_id = adapter->vsi.id;
4284 fltr->cls_u32_handle = cls_u32->knode.handle;
4285 return iavf_fdir_add_fltr(adapter, fltr);
4286
4287 free_alloc:
4288 kfree(fltr);
4289 return status;
4290 }
4291
4292 /**
4293 * iavf_del_cls_u32 - Delete U32 classifier offloads
4294 * @adapter: pointer to iavf adapter structure
4295 * @cls_u32: pointer to tc_cls_u32_offload struct with flow info
4296 *
4297 * Return: 0 on success or negative errno on failure.
4298 */
4299 static int iavf_del_cls_u32(struct iavf_adapter *adapter,
4300 struct tc_cls_u32_offload *cls_u32)
4301 {
4302 return iavf_fdir_del_fltr(adapter, true, cls_u32->knode.handle);
4303 }
4304
4305 /**
4306 * iavf_setup_tc_cls_u32 - U32 filter offloads
4307 * @adapter: pointer to iavf adapter structure
4308 * @cls_u32: pointer to tc_cls_u32_offload struct with flow info
4309 *
4310 * Return: 0 on success or negative errno on failure.
4311 */
4312 static int iavf_setup_tc_cls_u32(struct iavf_adapter *adapter,
4313 struct tc_cls_u32_offload *cls_u32)
4314 {
4315 if (!TC_U32_SUPPORT(adapter) || !FDIR_FLTR_SUPPORT(adapter))
4316 return -EOPNOTSUPP;
4317
4318 switch (cls_u32->command) {
4319 case TC_CLSU32_NEW_KNODE:
4320 case TC_CLSU32_REPLACE_KNODE:
4321 return iavf_add_cls_u32(adapter, cls_u32);
4322 case TC_CLSU32_DELETE_KNODE:
4323 return iavf_del_cls_u32(adapter, cls_u32);
4324 default:
4325 return -EOPNOTSUPP;
4326 }
4327 }
4328
4329 /**
4330 * iavf_setup_tc_block_cb - block callback for tc
4331 * @type: type of offload
4332 * @type_data: offload data
4333 * @cb_priv:
4334 *
4335 * This function is the block callback for traffic classes
4336 **/
4337 static int iavf_setup_tc_block_cb(enum tc_setup_type type, void *type_data,
4338 void *cb_priv)
4339 {
4340 struct iavf_adapter *adapter = cb_priv;
4341
4342 if (!tc_cls_can_offload_and_chain0(adapter->netdev, type_data))
4343 return -EOPNOTSUPP;
4344
4345 switch (type) {
4346 case TC_SETUP_CLSFLOWER:
4347 return iavf_setup_tc_cls_flower(cb_priv, type_data);
4348 case TC_SETUP_CLSU32:
4349 return iavf_setup_tc_cls_u32(cb_priv, type_data);
4350 default:
4351 return -EOPNOTSUPP;
4352 }
4353 }
4354
4355 static LIST_HEAD(iavf_block_cb_list);
4356
4357 /**
4358 * iavf_setup_tc - configure multiple traffic classes
4359 * @netdev: network interface device structure
4360 * @type: type of offload
4361 * @type_data: tc offload data
4362 *
4363 * This function is the callback to ndo_setup_tc in the
4364 * netdev_ops.
4365 *
4366 * Returns 0 on success
4367 **/
4368 static int iavf_setup_tc(struct net_device *netdev, enum tc_setup_type type,
4369 void *type_data)
4370 {
4371 struct iavf_adapter *adapter = netdev_priv(netdev);
4372
4373 switch (type) {
4374 case TC_SETUP_QDISC_MQPRIO:
4375 return __iavf_setup_tc(netdev, type_data);
4376 case TC_SETUP_BLOCK:
4377 return flow_block_cb_setup_simple(type_data,
4378 &iavf_block_cb_list,
4379 iavf_setup_tc_block_cb,
4380 adapter, adapter, true);
4381 default:
4382 return -EOPNOTSUPP;
4383 }
4384 }
4385
4386 /**
4387 * iavf_restore_fdir_filters
4388 * @adapter: board private structure
4389 *
4390 * Restore existing FDIR filters when VF netdev comes back up.
4391 **/
4392 static void iavf_restore_fdir_filters(struct iavf_adapter *adapter)
4393 {
4394 struct iavf_fdir_fltr *f;
4395
4396 spin_lock_bh(&adapter->fdir_fltr_lock);
4397 list_for_each_entry(f, &adapter->fdir_list_head, list) {
4398 if (f->state == IAVF_FDIR_FLTR_DIS_REQUEST) {
4399 /* Cancel a request, keep filter as active */
4400 f->state = IAVF_FDIR_FLTR_ACTIVE;
4401 } else if (f->state == IAVF_FDIR_FLTR_DIS_PENDING ||
4402 f->state == IAVF_FDIR_FLTR_INACTIVE) {
4403 /* Add filters which are inactive or have a pending
4404 * request to PF to be deleted
4405 */
4406 f->state = IAVF_FDIR_FLTR_ADD_REQUEST;
4407 adapter->aq_required |= IAVF_FLAG_AQ_ADD_FDIR_FILTER;
4408 }
4409 }
4410 spin_unlock_bh(&adapter->fdir_fltr_lock);
4411 }
4412
4413 /**
4414 * iavf_open - Called when a network interface is made active
4415 * @netdev: network interface device structure
4416 *
4417 * Returns 0 on success, negative value on failure
4418 *
4419 * The open entry point is called when a network interface is made
4420 * active by the system (IFF_UP). At this point all resources needed
4421 * for transmit and receive operations are allocated, the interrupt
4422 * handler is registered with the OS, the watchdog is started,
4423 * and the stack is notified that the interface is ready.
4424 **/
4425 static int iavf_open(struct net_device *netdev)
4426 {
4427 struct iavf_adapter *adapter = netdev_priv(netdev);
4428 int err;
4429
4430 netdev_assert_locked(netdev);
4431
4432 if (adapter->flags & IAVF_FLAG_PF_COMMS_FAILED) {
4433 dev_err(&adapter->pdev->dev, "Unable to open device due to PF driver failure.\n");
4434 return -EIO;
4435 }
4436
4437 if (adapter->state != __IAVF_DOWN)
4438 return -EBUSY;
4439
4440 if (adapter->state == __IAVF_RUNNING &&
4441 !test_bit(__IAVF_VSI_DOWN, adapter->vsi.state)) {
4442 dev_dbg(&adapter->pdev->dev, "VF is already open.\n");
4443 return 0;
4444 }
4445
4446 /* allocate transmit descriptors */
4447 err = iavf_setup_all_tx_resources(adapter);
4448 if (err)
4449 goto err_setup_tx;
4450
4451 /* allocate receive descriptors */
4452 err = iavf_setup_all_rx_resources(adapter);
4453 if (err)
4454 goto err_setup_rx;
4455
4456 /* clear any pending interrupts, may auto mask */
4457 err = iavf_request_traffic_irqs(adapter, netdev->name);
4458 if (err)
4459 goto err_req_irq;
4460
4461 spin_lock_bh(&adapter->mac_vlan_list_lock);
4462 iavf_add_filter(adapter, adapter->hw.mac.addr);
4463 spin_unlock_bh(&adapter->mac_vlan_list_lock);
4464
4465 iavf_restore_fdir_filters(adapter);
4466
4467 iavf_configure(adapter);
4468
4469 iavf_up_complete(adapter);
4470
4471 iavf_irq_enable(adapter, true);
4472
4473 return 0;
4474
4475 err_req_irq:
4476 iavf_down(adapter);
4477 iavf_free_traffic_irqs(adapter);
4478 err_setup_rx:
4479 iavf_free_all_rx_resources(adapter);
4480 err_setup_tx:
4481 iavf_free_all_tx_resources(adapter);
4482
4483 return err;
4484 }
4485
4486 /**
4487 * iavf_close - Disables a network interface
4488 * @netdev: network interface device structure
4489 *
4490 * Returns 0, this is not allowed to fail
4491 *
4492 * The close entry point is called when an interface is de-activated
4493 * by the OS. The hardware is still under the drivers control, but
4494 * needs to be disabled. All IRQs except vector 0 (reserved for admin queue)
4495 * are freed, along with all transmit and receive resources.
4496 **/
4497 static int iavf_close(struct net_device *netdev)
4498 {
4499 struct iavf_adapter *adapter = netdev_priv(netdev);
4500 u64 aq_to_restore;
4501 int status;
4502
4503 netdev_assert_locked(netdev);
4504
4505 if (adapter->state <= __IAVF_DOWN_PENDING)
4506 return 0;
4507
4508 set_bit(__IAVF_VSI_DOWN, adapter->vsi.state);
4509 /* We cannot send IAVF_FLAG_AQ_GET_OFFLOAD_VLAN_V2_CAPS before
4510 * IAVF_FLAG_AQ_DISABLE_QUEUES because in such case there is rtnl
4511 * deadlock with adminq_task() until iavf_close timeouts. We must send
4512 * IAVF_FLAG_AQ_GET_CONFIG before IAVF_FLAG_AQ_DISABLE_QUEUES to make
4513 * disable queues possible for vf. Give only necessary flags to
4514 * iavf_down and save other to set them right before iavf_close()
4515 * returns, when IAVF_FLAG_AQ_DISABLE_QUEUES will be already sent and
4516 * iavf will be in DOWN state.
4517 */
4518 aq_to_restore = adapter->aq_required;
4519 adapter->aq_required &= IAVF_FLAG_AQ_GET_CONFIG;
4520
4521 /* Remove flags which we do not want to send after close or we want to
4522 * send before disable queues.
4523 */
4524 aq_to_restore &= ~(IAVF_FLAG_AQ_GET_CONFIG |
4525 IAVF_FLAG_AQ_ENABLE_QUEUES |
4526 IAVF_FLAG_AQ_CONFIGURE_QUEUES |
4527 IAVF_FLAG_AQ_ADD_VLAN_FILTER |
4528 IAVF_FLAG_AQ_ADD_MAC_FILTER |
4529 IAVF_FLAG_AQ_ADD_CLOUD_FILTER |
4530 IAVF_FLAG_AQ_ADD_FDIR_FILTER |
4531 IAVF_FLAG_AQ_ADD_ADV_RSS_CFG);
4532
4533 iavf_down(adapter);
4534 iavf_change_state(adapter, __IAVF_DOWN_PENDING);
4535 iavf_free_traffic_irqs(adapter);
4536
4537 netdev_unlock(netdev);
4538
4539 /* We explicitly don't free resources here because the hardware is
4540 * still active and can DMA into memory. Resources are cleared in
4541 * iavf_virtchnl_completion() after we get confirmation from the PF
4542 * driver that the rings have been stopped.
4543 *
4544 * Also, we wait for state to transition to __IAVF_DOWN before
4545 * returning. State change occurs in iavf_virtchnl_completion() after
4546 * VF resources are released (which occurs after PF driver processes and
4547 * responds to admin queue commands).
4548 */
4549
4550 status = wait_event_timeout(adapter->down_waitqueue,
4551 adapter->state == __IAVF_DOWN,
4552 msecs_to_jiffies(500));
4553 if (!status)
4554 netdev_warn(netdev, "Device resources not yet released\n");
4555 netdev_lock(netdev);
4556
4557 adapter->aq_required |= aq_to_restore;
4558
4559 return 0;
4560 }
4561
4562 /**
4563 * iavf_change_mtu - Change the Maximum Transfer Unit
4564 * @netdev: network interface device structure
4565 * @new_mtu: new value for maximum frame size
4566 *
4567 * Returns 0 on success, negative on failure
4568 **/
4569 static int iavf_change_mtu(struct net_device *netdev, int new_mtu)
4570 {
4571 struct iavf_adapter *adapter = netdev_priv(netdev);
4572
4573 netdev_dbg(netdev, "changing MTU from %d to %d\n",
4574 netdev->mtu, new_mtu);
4575 WRITE_ONCE(netdev->mtu, new_mtu);
4576
4577 if (netif_running(netdev)) {
4578 adapter->flags |= IAVF_FLAG_RESET_NEEDED;
4579 iavf_reset_step(adapter);
4580 }
4581
4582 return 0;
4583 }
4584
4585 /**
4586 * iavf_disable_fdir - disable Flow Director and clear existing filters
4587 * @adapter: board private structure
4588 **/
4589 static void iavf_disable_fdir(struct iavf_adapter *adapter)
4590 {
4591 struct iavf_fdir_fltr *fdir, *fdirtmp;
4592 bool del_filters = false;
4593
4594 adapter->flags &= ~IAVF_FLAG_FDIR_ENABLED;
4595
4596 /* remove all Flow Director filters */
4597 spin_lock_bh(&adapter->fdir_fltr_lock);
4598 list_for_each_entry_safe(fdir, fdirtmp, &adapter->fdir_list_head,
4599 list) {
4600 if (fdir->state == IAVF_FDIR_FLTR_ADD_REQUEST ||
4601 fdir->state == IAVF_FDIR_FLTR_INACTIVE) {
4602 /* Delete filters not registered in PF */
4603 list_del(&fdir->list);
4604 iavf_dec_fdir_active_fltr(adapter, fdir);
4605 kfree(fdir);
4606 } else if (fdir->state == IAVF_FDIR_FLTR_ADD_PENDING ||
4607 fdir->state == IAVF_FDIR_FLTR_DIS_REQUEST ||
4608 fdir->state == IAVF_FDIR_FLTR_ACTIVE) {
4609 /* Filters registered in PF, schedule their deletion */
4610 fdir->state = IAVF_FDIR_FLTR_DEL_REQUEST;
4611 del_filters = true;
4612 } else if (fdir->state == IAVF_FDIR_FLTR_DIS_PENDING) {
4613 /* Request to delete filter already sent to PF, change
4614 * state to DEL_PENDING to delete filter after PF's
4615 * response, not set as INACTIVE
4616 */
4617 fdir->state = IAVF_FDIR_FLTR_DEL_PENDING;
4618 }
4619 }
4620 spin_unlock_bh(&adapter->fdir_fltr_lock);
4621
4622 if (del_filters) {
4623 adapter->aq_required |= IAVF_FLAG_AQ_DEL_FDIR_FILTER;
4624 mod_delayed_work(adapter->wq, &adapter->watchdog_task, 0);
4625 }
4626 }
4627
4628 #define NETIF_VLAN_OFFLOAD_FEATURES (NETIF_F_HW_VLAN_CTAG_RX | \
4629 NETIF_F_HW_VLAN_CTAG_TX | \
4630 NETIF_F_HW_VLAN_STAG_RX | \
4631 NETIF_F_HW_VLAN_STAG_TX)
4632
4633 /**
4634 * iavf_set_features - set the netdev feature flags
4635 * @netdev: ptr to the netdev being adjusted
4636 * @features: the feature set that the stack is suggesting
4637 * Note: expects to be called while under rtnl_lock()
4638 **/
4639 static int iavf_set_features(struct net_device *netdev,
4640 netdev_features_t features)
4641 {
4642 struct iavf_adapter *adapter = netdev_priv(netdev);
4643
4644 /* trigger update on any VLAN feature change */
4645 if ((netdev->features & NETIF_VLAN_OFFLOAD_FEATURES) ^
4646 (features & NETIF_VLAN_OFFLOAD_FEATURES))
4647 iavf_set_vlan_offload_features(adapter, netdev->features,
4648 features);
4649 if (CRC_OFFLOAD_ALLOWED(adapter) &&
4650 ((netdev->features & NETIF_F_RXFCS) ^ (features & NETIF_F_RXFCS)))
4651 iavf_schedule_reset(adapter, IAVF_FLAG_RESET_NEEDED);
4652
4653 if ((netdev->features & NETIF_F_NTUPLE) ^ (features & NETIF_F_NTUPLE)) {
4654 if (features & NETIF_F_NTUPLE)
4655 adapter->flags |= IAVF_FLAG_FDIR_ENABLED;
4656 else
4657 iavf_disable_fdir(adapter);
4658 }
4659
4660 return 0;
4661 }
4662
4663 /**
4664 * iavf_features_check - Validate encapsulated packet conforms to limits
4665 * @skb: skb buff
4666 * @dev: This physical port's netdev
4667 * @features: Offload features that the stack believes apply
4668 **/
4669 static netdev_features_t iavf_features_check(struct sk_buff *skb,
4670 struct net_device *dev,
4671 netdev_features_t features)
4672 {
4673 size_t len;
4674
4675 /* No point in doing any of this if neither checksum nor GSO are
4676 * being requested for this frame. We can rule out both by just
4677 * checking for CHECKSUM_PARTIAL
4678 */
4679 if (skb->ip_summed != CHECKSUM_PARTIAL)
4680 return features;
4681
4682 /* We cannot support GSO if the MSS is going to be less than
4683 * 64 bytes. If it is then we need to drop support for GSO.
4684 */
4685 if (skb_is_gso(skb) && (skb_shinfo(skb)->gso_size < 64))
4686 features &= ~NETIF_F_GSO_MASK;
4687
4688 /* MACLEN can support at most 63 words */
4689 len = skb_network_offset(skb);
4690 if (len & ~(63 * 2))
4691 goto out_err;
4692
4693 /* IPLEN and EIPLEN can support at most 127 dwords */
4694 len = skb_network_header_len(skb);
4695 if (len & ~(127 * 4))
4696 goto out_err;
4697
4698 if (skb->encapsulation) {
4699 /* L4TUNLEN can support 127 words */
4700 len = skb_inner_network_header(skb) - skb_transport_header(skb);
4701 if (len & ~(127 * 2))
4702 goto out_err;
4703
4704 /* IPLEN can support at most 127 dwords */
4705 len = skb_inner_transport_header(skb) -
4706 skb_inner_network_header(skb);
4707 if (len & ~(127 * 4))
4708 goto out_err;
4709 }
4710
4711 /* No need to validate L4LEN as TCP is the only protocol with a
4712 * flexible value and we support all possible values supported
4713 * by TCP, which is at most 15 dwords
4714 */
4715
4716 return features;
4717 out_err:
4718 return features & ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK);
4719 }
4720
4721 /**
4722 * iavf_get_netdev_vlan_hw_features - get NETDEV VLAN features that can toggle on/off
4723 * @adapter: board private structure
4724 *
4725 * Depending on whether VIRTHCNL_VF_OFFLOAD_VLAN or VIRTCHNL_VF_OFFLOAD_VLAN_V2
4726 * were negotiated determine the VLAN features that can be toggled on and off.
4727 **/
4728 static netdev_features_t
4729 iavf_get_netdev_vlan_hw_features(struct iavf_adapter *adapter)
4730 {
4731 netdev_features_t hw_features = 0;
4732
4733 if (!adapter->vf_res || !adapter->vf_res->vf_cap_flags)
4734 return hw_features;
4735
4736 /* Enable VLAN features if supported */
4737 if (VLAN_ALLOWED(adapter)) {
4738 hw_features |= (NETIF_F_HW_VLAN_CTAG_TX |
4739 NETIF_F_HW_VLAN_CTAG_RX);
4740 } else if (VLAN_V2_ALLOWED(adapter)) {
4741 struct virtchnl_vlan_caps *vlan_v2_caps =
4742 &adapter->vlan_v2_caps;
4743 struct virtchnl_vlan_supported_caps *stripping_support =
4744 &vlan_v2_caps->offloads.stripping_support;
4745 struct virtchnl_vlan_supported_caps *insertion_support =
4746 &vlan_v2_caps->offloads.insertion_support;
4747
4748 if (stripping_support->outer != VIRTCHNL_VLAN_UNSUPPORTED &&
4749 stripping_support->outer & VIRTCHNL_VLAN_TOGGLE) {
4750 if (stripping_support->outer &
4751 VIRTCHNL_VLAN_ETHERTYPE_8100)
4752 hw_features |= NETIF_F_HW_VLAN_CTAG_RX;
4753 if (stripping_support->outer &
4754 VIRTCHNL_VLAN_ETHERTYPE_88A8)
4755 hw_features |= NETIF_F_HW_VLAN_STAG_RX;
4756 } else if (stripping_support->inner !=
4757 VIRTCHNL_VLAN_UNSUPPORTED &&
4758 stripping_support->inner & VIRTCHNL_VLAN_TOGGLE) {
4759 if (stripping_support->inner &
4760 VIRTCHNL_VLAN_ETHERTYPE_8100)
4761 hw_features |= NETIF_F_HW_VLAN_CTAG_RX;
4762 }
4763
4764 if (insertion_support->outer != VIRTCHNL_VLAN_UNSUPPORTED &&
4765 insertion_support->outer & VIRTCHNL_VLAN_TOGGLE) {
4766 if (insertion_support->outer &
4767 VIRTCHNL_VLAN_ETHERTYPE_8100)
4768 hw_features |= NETIF_F_HW_VLAN_CTAG_TX;
4769 if (insertion_support->outer &
4770 VIRTCHNL_VLAN_ETHERTYPE_88A8)
4771 hw_features |= NETIF_F_HW_VLAN_STAG_TX;
4772 } else if (insertion_support->inner &&
4773 insertion_support->inner & VIRTCHNL_VLAN_TOGGLE) {
4774 if (insertion_support->inner &
4775 VIRTCHNL_VLAN_ETHERTYPE_8100)
4776 hw_features |= NETIF_F_HW_VLAN_CTAG_TX;
4777 }
4778 }
4779
4780 if (CRC_OFFLOAD_ALLOWED(adapter))
4781 hw_features |= NETIF_F_RXFCS;
4782
4783 return hw_features;
4784 }
4785
4786 /**
4787 * iavf_get_netdev_vlan_features - get the enabled NETDEV VLAN fetures
4788 * @adapter: board private structure
4789 *
4790 * Depending on whether VIRTHCNL_VF_OFFLOAD_VLAN or VIRTCHNL_VF_OFFLOAD_VLAN_V2
4791 * were negotiated determine the VLAN features that are enabled by default.
4792 **/
4793 static netdev_features_t
4794 iavf_get_netdev_vlan_features(struct iavf_adapter *adapter)
4795 {
4796 netdev_features_t features = 0;
4797
4798 if (!adapter->vf_res || !adapter->vf_res->vf_cap_flags)
4799 return features;
4800
4801 if (VLAN_ALLOWED(adapter)) {
4802 features |= NETIF_F_HW_VLAN_CTAG_FILTER |
4803 NETIF_F_HW_VLAN_CTAG_RX | NETIF_F_HW_VLAN_CTAG_TX;
4804 } else if (VLAN_V2_ALLOWED(adapter)) {
4805 struct virtchnl_vlan_caps *vlan_v2_caps =
4806 &adapter->vlan_v2_caps;
4807 struct virtchnl_vlan_supported_caps *filtering_support =
4808 &vlan_v2_caps->filtering.filtering_support;
4809 struct virtchnl_vlan_supported_caps *stripping_support =
4810 &vlan_v2_caps->offloads.stripping_support;
4811 struct virtchnl_vlan_supported_caps *insertion_support =
4812 &vlan_v2_caps->offloads.insertion_support;
4813 u32 ethertype_init;
4814
4815 /* give priority to outer stripping and don't support both outer
4816 * and inner stripping
4817 */
4818 ethertype_init = vlan_v2_caps->offloads.ethertype_init;
4819 if (stripping_support->outer != VIRTCHNL_VLAN_UNSUPPORTED) {
4820 if (stripping_support->outer &
4821 VIRTCHNL_VLAN_ETHERTYPE_8100 &&
4822 ethertype_init & VIRTCHNL_VLAN_ETHERTYPE_8100)
4823 features |= NETIF_F_HW_VLAN_CTAG_RX;
4824 else if (stripping_support->outer &
4825 VIRTCHNL_VLAN_ETHERTYPE_88A8 &&
4826 ethertype_init & VIRTCHNL_VLAN_ETHERTYPE_88A8)
4827 features |= NETIF_F_HW_VLAN_STAG_RX;
4828 } else if (stripping_support->inner !=
4829 VIRTCHNL_VLAN_UNSUPPORTED) {
4830 if (stripping_support->inner &
4831 VIRTCHNL_VLAN_ETHERTYPE_8100 &&
4832 ethertype_init & VIRTCHNL_VLAN_ETHERTYPE_8100)
4833 features |= NETIF_F_HW_VLAN_CTAG_RX;
4834 }
4835
4836 /* give priority to outer insertion and don't support both outer
4837 * and inner insertion
4838 */
4839 if (insertion_support->outer != VIRTCHNL_VLAN_UNSUPPORTED) {
4840 if (insertion_support->outer &
4841 VIRTCHNL_VLAN_ETHERTYPE_8100 &&
4842 ethertype_init & VIRTCHNL_VLAN_ETHERTYPE_8100)
4843 features |= NETIF_F_HW_VLAN_CTAG_TX;
4844 else if (insertion_support->outer &
4845 VIRTCHNL_VLAN_ETHERTYPE_88A8 &&
4846 ethertype_init & VIRTCHNL_VLAN_ETHERTYPE_88A8)
4847 features |= NETIF_F_HW_VLAN_STAG_TX;
4848 } else if (insertion_support->inner !=
4849 VIRTCHNL_VLAN_UNSUPPORTED) {
4850 if (insertion_support->inner &
4851 VIRTCHNL_VLAN_ETHERTYPE_8100 &&
4852 ethertype_init & VIRTCHNL_VLAN_ETHERTYPE_8100)
4853 features |= NETIF_F_HW_VLAN_CTAG_TX;
4854 }
4855
4856 /* give priority to outer filtering and don't bother if both
4857 * outer and inner filtering are enabled
4858 */
4859 ethertype_init = vlan_v2_caps->filtering.ethertype_init;
4860 if (filtering_support->outer != VIRTCHNL_VLAN_UNSUPPORTED) {
4861 if (filtering_support->outer &
4862 VIRTCHNL_VLAN_ETHERTYPE_8100 &&
4863 ethertype_init & VIRTCHNL_VLAN_ETHERTYPE_8100)
4864 features |= NETIF_F_HW_VLAN_CTAG_FILTER;
4865 if (filtering_support->outer &
4866 VIRTCHNL_VLAN_ETHERTYPE_88A8 &&
4867 ethertype_init & VIRTCHNL_VLAN_ETHERTYPE_88A8)
4868 features |= NETIF_F_HW_VLAN_STAG_FILTER;
4869 } else if (filtering_support->inner !=
4870 VIRTCHNL_VLAN_UNSUPPORTED) {
4871 if (filtering_support->inner &
4872 VIRTCHNL_VLAN_ETHERTYPE_8100 &&
4873 ethertype_init & VIRTCHNL_VLAN_ETHERTYPE_8100)
4874 features |= NETIF_F_HW_VLAN_CTAG_FILTER;
4875 if (filtering_support->inner &
4876 VIRTCHNL_VLAN_ETHERTYPE_88A8 &&
4877 ethertype_init & VIRTCHNL_VLAN_ETHERTYPE_88A8)
4878 features |= NETIF_F_HW_VLAN_STAG_FILTER;
4879 }
4880 }
4881
4882 return features;
4883 }
4884
4885 #define IAVF_NETDEV_VLAN_FEATURE_ALLOWED(requested, allowed, feature_bit) \
4886 (!(((requested) & (feature_bit)) && \
4887 !((allowed) & (feature_bit))))
4888
4889 /**
4890 * iavf_fix_netdev_vlan_features - fix NETDEV VLAN features based on support
4891 * @adapter: board private structure
4892 * @requested_features: stack requested NETDEV features
4893 **/
4894 static netdev_features_t
4895 iavf_fix_netdev_vlan_features(struct iavf_adapter *adapter,
4896 netdev_features_t requested_features)
4897 {
4898 netdev_features_t allowed_features;
4899
4900 allowed_features = iavf_get_netdev_vlan_hw_features(adapter) |
4901 iavf_get_netdev_vlan_features(adapter);
4902
4903 if (!IAVF_NETDEV_VLAN_FEATURE_ALLOWED(requested_features,
4904 allowed_features,
4905 NETIF_F_HW_VLAN_CTAG_TX))
4906 requested_features &= ~NETIF_F_HW_VLAN_CTAG_TX;
4907
4908 if (!IAVF_NETDEV_VLAN_FEATURE_ALLOWED(requested_features,
4909 allowed_features,
4910 NETIF_F_HW_VLAN_CTAG_RX))
4911 requested_features &= ~NETIF_F_HW_VLAN_CTAG_RX;
4912
4913 if (!IAVF_NETDEV_VLAN_FEATURE_ALLOWED(requested_features,
4914 allowed_features,
4915 NETIF_F_HW_VLAN_STAG_TX))
4916 requested_features &= ~NETIF_F_HW_VLAN_STAG_TX;
4917 if (!IAVF_NETDEV_VLAN_FEATURE_ALLOWED(requested_features,
4918 allowed_features,
4919 NETIF_F_HW_VLAN_STAG_RX))
4920 requested_features &= ~NETIF_F_HW_VLAN_STAG_RX;
4921
4922 if (!IAVF_NETDEV_VLAN_FEATURE_ALLOWED(requested_features,
4923 allowed_features,
4924 NETIF_F_HW_VLAN_CTAG_FILTER))
4925 requested_features &= ~NETIF_F_HW_VLAN_CTAG_FILTER;
4926
4927 if (!IAVF_NETDEV_VLAN_FEATURE_ALLOWED(requested_features,
4928 allowed_features,
4929 NETIF_F_HW_VLAN_STAG_FILTER))
4930 requested_features &= ~NETIF_F_HW_VLAN_STAG_FILTER;
4931
4932 if ((requested_features &
4933 (NETIF_F_HW_VLAN_CTAG_RX | NETIF_F_HW_VLAN_CTAG_TX)) &&
4934 (requested_features &
4935 (NETIF_F_HW_VLAN_STAG_RX | NETIF_F_HW_VLAN_STAG_TX)) &&
4936 adapter->vlan_v2_caps.offloads.ethertype_match ==
4937 VIRTCHNL_ETHERTYPE_STRIPPING_MATCHES_INSERTION) {
4938 netdev_warn(adapter->netdev, "cannot support CTAG and STAG VLAN stripping and/or insertion simultaneously since CTAG and STAG offloads are mutually exclusive, clearing STAG offload settings\n");
4939 requested_features &= ~(NETIF_F_HW_VLAN_STAG_RX |
4940 NETIF_F_HW_VLAN_STAG_TX);
4941 }
4942
4943 return requested_features;
4944 }
4945
4946 /**
4947 * iavf_fix_strip_features - fix NETDEV CRC and VLAN strip features
4948 * @adapter: board private structure
4949 * @requested_features: stack requested NETDEV features
4950 *
4951 * Returns fixed-up features bits
4952 **/
4953 static netdev_features_t
4954 iavf_fix_strip_features(struct iavf_adapter *adapter,
4955 netdev_features_t requested_features)
4956 {
4957 struct net_device *netdev = adapter->netdev;
4958 bool crc_offload_req, is_vlan_strip;
4959 netdev_features_t vlan_strip;
4960 int num_non_zero_vlan;
4961
4962 crc_offload_req = CRC_OFFLOAD_ALLOWED(adapter) &&
4963 (requested_features & NETIF_F_RXFCS);
4964 num_non_zero_vlan = iavf_get_num_vlans_added(adapter);
4965 vlan_strip = (NETIF_F_HW_VLAN_CTAG_RX | NETIF_F_HW_VLAN_STAG_RX);
4966 is_vlan_strip = requested_features & vlan_strip;
4967
4968 if (!crc_offload_req)
4969 return requested_features;
4970
4971 if (!num_non_zero_vlan && (netdev->features & vlan_strip) &&
4972 !(netdev->features & NETIF_F_RXFCS) && is_vlan_strip) {
4973 requested_features &= ~vlan_strip;
4974 netdev_info(netdev, "Disabling VLAN stripping as FCS/CRC stripping is also disabled and there is no VLAN configured\n");
4975 return requested_features;
4976 }
4977
4978 if ((netdev->features & NETIF_F_RXFCS) && is_vlan_strip) {
4979 requested_features &= ~vlan_strip;
4980 if (!(netdev->features & vlan_strip))
4981 netdev_info(netdev, "To enable VLAN stripping, first need to enable FCS/CRC stripping");
4982
4983 return requested_features;
4984 }
4985
4986 if (num_non_zero_vlan && is_vlan_strip &&
4987 !(netdev->features & NETIF_F_RXFCS)) {
4988 requested_features &= ~NETIF_F_RXFCS;
4989 netdev_info(netdev, "To disable FCS/CRC stripping, first need to disable VLAN stripping");
4990 }
4991
4992 return requested_features;
4993 }
4994
4995 /**
4996 * iavf_fix_features - fix up the netdev feature bits
4997 * @netdev: our net device
4998 * @features: desired feature bits
4999 *
5000 * Returns fixed-up features bits
5001 **/
5002 static netdev_features_t iavf_fix_features(struct net_device *netdev,
5003 netdev_features_t features)
5004 {
5005 struct iavf_adapter *adapter = netdev_priv(netdev);
5006
5007 features = iavf_fix_netdev_vlan_features(adapter, features);
5008
5009 if (!FDIR_FLTR_SUPPORT(adapter))
5010 features &= ~NETIF_F_NTUPLE;
5011
5012 return iavf_fix_strip_features(adapter, features);
5013 }
5014
5015 static int iavf_hwstamp_get(struct net_device *netdev,
5016 struct kernel_hwtstamp_config *config)
5017 {
5018 struct iavf_adapter *adapter = netdev_priv(netdev);
5019
5020 *config = adapter->ptp.hwtstamp_config;
5021
5022 return 0;
5023 }
5024
5025 static int iavf_hwstamp_set(struct net_device *netdev,
5026 struct kernel_hwtstamp_config *config,
5027 struct netlink_ext_ack *extack)
5028 {
5029 struct iavf_adapter *adapter = netdev_priv(netdev);
5030
5031 return iavf_ptp_set_ts_config(adapter, config, extack);
5032 }
5033
5034 static int
5035 iavf_verify_shaper(struct net_shaper_binding *binding,
5036 const struct net_shaper *shaper,
5037 struct netlink_ext_ack *extack)
5038 {
5039 struct iavf_adapter *adapter = netdev_priv(binding->netdev);
5040 u64 vf_max;
5041
5042 if (shaper->handle.scope == NET_SHAPER_SCOPE_QUEUE) {
5043 vf_max = adapter->qos_caps->cap[0].shaper.peak;
5044 if (vf_max && shaper->bw_max > vf_max) {
5045 NL_SET_ERR_MSG_FMT(extack, "Max rate (%llu) of queue %d can't exceed max TX rate of VF (%llu kbps)",
5046 shaper->bw_max, shaper->handle.id,
5047 vf_max);
5048 return -EINVAL;
5049 }
5050 }
5051 return 0;
5052 }
5053
5054 static int
5055 iavf_shaper_set(struct net_shaper_binding *binding,
5056 const struct net_shaper *shaper,
5057 struct netlink_ext_ack *extack)
5058 {
5059 struct iavf_adapter *adapter = netdev_priv(binding->netdev);
5060 const struct net_shaper_handle *handle = &shaper->handle;
5061 struct iavf_ring *tx_ring;
5062 int ret;
5063
5064 netdev_assert_locked(adapter->netdev);
5065
5066 if (handle->id >= adapter->num_active_queues)
5067 return 0;
5068
5069 ret = iavf_verify_shaper(binding, shaper, extack);
5070 if (ret)
5071 return ret;
5072
5073 tx_ring = &adapter->tx_rings[handle->id];
5074
5075 tx_ring->q_shaper.bw_min = div_u64(shaper->bw_min, 1000);
5076 tx_ring->q_shaper.bw_max = div_u64(shaper->bw_max, 1000);
5077 tx_ring->q_shaper_update = true;
5078
5079 adapter->aq_required |= IAVF_FLAG_AQ_CONFIGURE_QUEUES_BW;
5080
5081 return 0;
5082 }
5083
5084 static int iavf_shaper_del(struct net_shaper_binding *binding,
5085 const struct net_shaper_handle *handle,
5086 struct netlink_ext_ack *extack)
5087 {
5088 struct iavf_adapter *adapter = netdev_priv(binding->netdev);
5089 struct iavf_ring *tx_ring;
5090
5091 netdev_assert_locked(adapter->netdev);
5092
5093 if (handle->id >= adapter->num_active_queues)
5094 return 0;
5095
5096 tx_ring = &adapter->tx_rings[handle->id];
5097 tx_ring->q_shaper.bw_min = 0;
5098 tx_ring->q_shaper.bw_max = 0;
5099 tx_ring->q_shaper_update = true;
5100
5101 adapter->aq_required |= IAVF_FLAG_AQ_CONFIGURE_QUEUES_BW;
5102
5103 return 0;
5104 }
5105
5106 static void iavf_shaper_cap(struct net_shaper_binding *binding,
5107 enum net_shaper_scope scope,
5108 unsigned long *flags)
5109 {
5110 if (scope != NET_SHAPER_SCOPE_QUEUE)
5111 return;
5112
5113 *flags = BIT(NET_SHAPER_A_CAPS_SUPPORT_BW_MIN) |
5114 BIT(NET_SHAPER_A_CAPS_SUPPORT_BW_MAX) |
5115 BIT(NET_SHAPER_A_CAPS_SUPPORT_METRIC_BPS);
5116 }
5117
5118 static const struct net_shaper_ops iavf_shaper_ops = {
5119 .set = iavf_shaper_set,
5120 .delete = iavf_shaper_del,
5121 .capabilities = iavf_shaper_cap,
5122 };
5123
5124 static const struct net_device_ops iavf_netdev_ops = {
5125 .ndo_open = iavf_open,
5126 .ndo_stop = iavf_close,
5127 .ndo_start_xmit = iavf_xmit_frame,
5128 .ndo_set_rx_mode = iavf_set_rx_mode,
5129 .ndo_validate_addr = eth_validate_addr,
5130 .ndo_set_mac_address = iavf_set_mac,
5131 .ndo_change_mtu = iavf_change_mtu,
5132 .ndo_tx_timeout = iavf_tx_timeout,
5133 .ndo_vlan_rx_add_vid = iavf_vlan_rx_add_vid,
5134 .ndo_vlan_rx_kill_vid = iavf_vlan_rx_kill_vid,
5135 .ndo_features_check = iavf_features_check,
5136 .ndo_fix_features = iavf_fix_features,
5137 .ndo_set_features = iavf_set_features,
5138 .ndo_setup_tc = iavf_setup_tc,
5139 .net_shaper_ops = &iavf_shaper_ops,
5140 .ndo_hwtstamp_get = iavf_hwstamp_get,
5141 .ndo_hwtstamp_set = iavf_hwstamp_set,
5142 };
5143
5144 /**
5145 * iavf_check_reset_complete - check that VF reset is complete
5146 * @hw: pointer to hw struct
5147 *
5148 * Returns 0 if device is ready to use, or -EBUSY if it's in reset.
5149 **/
5150 static int iavf_check_reset_complete(struct iavf_hw *hw)
5151 {
5152 u32 rstat;
5153 int i;
5154
5155 for (i = 0; i < IAVF_RESET_WAIT_COMPLETE_COUNT; i++) {
5156 rstat = rd32(hw, IAVF_VFGEN_RSTAT) &
5157 IAVF_VFGEN_RSTAT_VFR_STATE_MASK;
5158 if ((rstat == VIRTCHNL_VFR_VFACTIVE) ||
5159 (rstat == VIRTCHNL_VFR_COMPLETED))
5160 return 0;
5161 msleep(IAVF_RESET_WAIT_MS);
5162 }
5163 return -EBUSY;
5164 }
5165
5166 /**
5167 * iavf_process_config - Process the config information we got from the PF
5168 * @adapter: board private structure
5169 *
5170 * Verify that we have a valid config struct, and set up our netdev features
5171 * and our VSI struct.
5172 **/
5173 int iavf_process_config(struct iavf_adapter *adapter)
5174 {
5175 struct virtchnl_vf_resource *vfres = adapter->vf_res;
5176 netdev_features_t hw_vlan_features, vlan_features;
5177 struct net_device *netdev = adapter->netdev;
5178 netdev_features_t hw_enc_features;
5179 netdev_features_t hw_features;
5180
5181 hw_enc_features = NETIF_F_SG |
5182 NETIF_F_IP_CSUM |
5183 NETIF_F_IPV6_CSUM |
5184 NETIF_F_HIGHDMA |
5185 NETIF_F_SOFT_FEATURES |
5186 NETIF_F_TSO |
5187 NETIF_F_TSO_ECN |
5188 NETIF_F_TSO6 |
5189 NETIF_F_SCTP_CRC |
5190 NETIF_F_RXHASH |
5191 NETIF_F_RXCSUM |
5192 0;
5193
5194 /* advertise to stack only if offloads for encapsulated packets is
5195 * supported
5196 */
5197 if (vfres->vf_cap_flags & VIRTCHNL_VF_OFFLOAD_ENCAP) {
5198 hw_enc_features |= NETIF_F_GSO_UDP_TUNNEL |
5199 NETIF_F_GSO_GRE |
5200 NETIF_F_GSO_GRE_CSUM |
5201 NETIF_F_GSO_IPXIP4 |
5202 NETIF_F_GSO_IPXIP6 |
5203 NETIF_F_GSO_UDP_TUNNEL_CSUM |
5204 NETIF_F_GSO_PARTIAL |
5205 0;
5206
5207 if (!(vfres->vf_cap_flags &
5208 VIRTCHNL_VF_OFFLOAD_ENCAP_CSUM))
5209 netdev->gso_partial_features |=
5210 NETIF_F_GSO_UDP_TUNNEL_CSUM;
5211
5212 netdev->gso_partial_features |= NETIF_F_GSO_GRE_CSUM;
5213 netdev->hw_enc_features |= NETIF_F_TSO_MANGLEID;
5214 netdev->hw_enc_features |= hw_enc_features;
5215 }
5216 /* record features VLANs can make use of */
5217 netdev->vlan_features |= hw_enc_features | NETIF_F_TSO_MANGLEID;
5218
5219 /* Write features and hw_features separately to avoid polluting
5220 * with, or dropping, features that are set when we registered.
5221 */
5222 hw_features = hw_enc_features;
5223
5224 /* get HW VLAN features that can be toggled */
5225 hw_vlan_features = iavf_get_netdev_vlan_hw_features(adapter);
5226
5227 /* Enable HW TC offload if ADQ or tc U32 is supported */
5228 if (vfres->vf_cap_flags & VIRTCHNL_VF_OFFLOAD_ADQ ||
5229 TC_U32_SUPPORT(adapter))
5230 hw_features |= NETIF_F_HW_TC;
5231
5232 if (vfres->vf_cap_flags & VIRTCHNL_VF_OFFLOAD_USO)
5233 hw_features |= NETIF_F_GSO_UDP_L4;
5234
5235 netdev->hw_features |= hw_features | hw_vlan_features;
5236 vlan_features = iavf_get_netdev_vlan_features(adapter);
5237
5238 netdev->features |= hw_features | vlan_features;
5239
5240 if (vfres->vf_cap_flags & VIRTCHNL_VF_OFFLOAD_VLAN)
5241 netdev->features |= NETIF_F_HW_VLAN_CTAG_FILTER;
5242
5243 if (FDIR_FLTR_SUPPORT(adapter)) {
5244 netdev->hw_features |= NETIF_F_NTUPLE;
5245 netdev->features |= NETIF_F_NTUPLE;
5246 adapter->flags |= IAVF_FLAG_FDIR_ENABLED;
5247 }
5248
5249 netdev->priv_flags |= IFF_UNICAST_FLT;
5250
5251 /* Do not turn on offloads when they are requested to be turned off.
5252 * TSO needs minimum 576 bytes to work correctly.
5253 */
5254 if (netdev->wanted_features) {
5255 if (!(netdev->wanted_features & NETIF_F_TSO) ||
5256 netdev->mtu < 576)
5257 netdev->features &= ~NETIF_F_TSO;
5258 if (!(netdev->wanted_features & NETIF_F_TSO6) ||
5259 netdev->mtu < 576)
5260 netdev->features &= ~NETIF_F_TSO6;
5261 if (!(netdev->wanted_features & NETIF_F_TSO_ECN))
5262 netdev->features &= ~NETIF_F_TSO_ECN;
5263 if (!(netdev->wanted_features & NETIF_F_GRO))
5264 netdev->features &= ~NETIF_F_GRO;
5265 if (!(netdev->wanted_features & NETIF_F_GSO))
5266 netdev->features &= ~NETIF_F_GSO;
5267 }
5268
5269 return 0;
5270 }
5271
5272 /**
5273 * iavf_probe - Device Initialization Routine
5274 * @pdev: PCI device information struct
5275 * @ent: entry in iavf_pci_tbl
5276 *
5277 * Returns 0 on success, negative on failure
5278 *
5279 * iavf_probe initializes an adapter identified by a pci_dev structure.
5280 * The OS initialization, configuring of the adapter private structure,
5281 * and a hardware reset occur.
5282 **/
5283 static int iavf_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
5284 {
5285 struct net_device *netdev;
5286 struct iavf_adapter *adapter = NULL;
5287 struct iavf_hw *hw = NULL;
5288 int err, len;
5289
5290 err = pci_enable_device(pdev);
5291 if (err)
5292 return err;
5293
5294 err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
5295 if (err) {
5296 dev_err(&pdev->dev,
5297 "DMA configuration failed: 0x%x\n", err);
5298 goto err_dma;
5299 }
5300
5301 err = pci_request_regions(pdev, iavf_driver_name);
5302 if (err) {
5303 dev_err(&pdev->dev,
5304 "pci_request_regions failed 0x%x\n", err);
5305 goto err_pci_reg;
5306 }
5307
5308 pci_set_master(pdev);
5309
5310 netdev = alloc_etherdev_mq(sizeof(struct iavf_adapter),
5311 IAVF_MAX_REQ_QUEUES);
5312 if (!netdev) {
5313 err = -ENOMEM;
5314 goto err_alloc_etherdev;
5315 }
5316
5317 netif_set_affinity_auto(netdev);
5318 SET_NETDEV_DEV(netdev, &pdev->dev);
5319
5320 pci_set_drvdata(pdev, netdev);
5321 adapter = netdev_priv(netdev);
5322
5323 adapter->netdev = netdev;
5324 adapter->pdev = pdev;
5325
5326 hw = &adapter->hw;
5327 hw->back = adapter;
5328
5329 adapter->wq = alloc_ordered_workqueue("%s", WQ_MEM_RECLAIM,
5330 iavf_driver_name);
5331 if (!adapter->wq) {
5332 err = -ENOMEM;
5333 goto err_alloc_wq;
5334 }
5335
5336 adapter->msg_enable = BIT(DEFAULT_DEBUG_LEVEL_SHIFT) - 1;
5337 iavf_change_state(adapter, __IAVF_STARTUP);
5338
5339 /* Call save state here because it relies on the adapter struct. */
5340 pci_save_state(pdev);
5341
5342 hw->hw_addr = ioremap(pci_resource_start(pdev, 0),
5343 pci_resource_len(pdev, 0));
5344 if (!hw->hw_addr) {
5345 err = -EIO;
5346 goto err_ioremap;
5347 }
5348 hw->vendor_id = pdev->vendor;
5349 hw->device_id = pdev->device;
5350 pci_read_config_byte(pdev, PCI_REVISION_ID, &hw->revision_id);
5351 hw->subsystem_vendor_id = pdev->subsystem_vendor;
5352 hw->subsystem_device_id = pdev->subsystem_device;
5353 hw->bus.device = PCI_SLOT(pdev->devfn);
5354 hw->bus.func = PCI_FUNC(pdev->devfn);
5355 hw->bus.bus_id = pdev->bus->number;
5356
5357 len = struct_size(adapter->qos_caps, cap, IAVF_MAX_QOS_TC_NUM);
5358 adapter->qos_caps = kzalloc(len, GFP_KERNEL);
5359 if (!adapter->qos_caps) {
5360 err = -ENOMEM;
5361 goto err_alloc_qos_cap;
5362 }
5363
5364 mutex_init(&hw->aq.asq_mutex);
5365 mutex_init(&hw->aq.arq_mutex);
5366
5367 spin_lock_init(&adapter->mac_vlan_list_lock);
5368 spin_lock_init(&adapter->cloud_filter_list_lock);
5369 spin_lock_init(&adapter->fdir_fltr_lock);
5370 spin_lock_init(&adapter->adv_rss_lock);
5371 spin_lock_init(&adapter->current_netdev_promisc_flags_lock);
5372
5373 INIT_LIST_HEAD(&adapter->mac_filter_list);
5374 INIT_LIST_HEAD(&adapter->vlan_filter_list);
5375 INIT_LIST_HEAD(&adapter->cloud_filter_list);
5376 INIT_LIST_HEAD(&adapter->fdir_list_head);
5377 INIT_LIST_HEAD(&adapter->adv_rss_list_head);
5378
5379 INIT_WORK(&adapter->reset_task, iavf_reset_task);
5380 INIT_WORK(&adapter->adminq_task, iavf_adminq_task);
5381 INIT_WORK(&adapter->finish_config, iavf_finish_config);
5382 INIT_DELAYED_WORK(&adapter->watchdog_task, iavf_watchdog_task);
5383
5384 /* Setup the wait queue for indicating transition to down status */
5385 init_waitqueue_head(&adapter->down_waitqueue);
5386
5387 /* Setup the wait queue for indicating virtchannel events */
5388 init_waitqueue_head(&adapter->vc_waitqueue);
5389
5390 INIT_LIST_HEAD(&adapter->ptp.aq_cmds);
5391 init_waitqueue_head(&adapter->ptp.phc_time_waitqueue);
5392 mutex_init(&adapter->ptp.aq_cmd_lock);
5393
5394 queue_delayed_work(adapter->wq, &adapter->watchdog_task,
5395 msecs_to_jiffies(5 * (pdev->devfn & 0x07)));
5396 /* Initialization goes on in the work. Do not add more of it below. */
5397 return 0;
5398
5399 err_alloc_qos_cap:
5400 iounmap(hw->hw_addr);
5401 err_ioremap:
5402 destroy_workqueue(adapter->wq);
5403 err_alloc_wq:
5404 free_netdev(netdev);
5405 err_alloc_etherdev:
5406 pci_release_regions(pdev);
5407 err_pci_reg:
5408 err_dma:
5409 pci_disable_device(pdev);
5410 return err;
5411 }
5412
5413 /**
5414 * iavf_suspend - Power management suspend routine
5415 * @dev_d: device info pointer
5416 *
5417 * Called when the system (VM) is entering sleep/suspend.
5418 **/
5419 static int iavf_suspend(struct device *dev_d)
5420 {
5421 struct net_device *netdev = dev_get_drvdata(dev_d);
5422 struct iavf_adapter *adapter = netdev_priv(netdev);
5423 bool running;
5424
5425 netif_device_detach(netdev);
5426
5427 running = netif_running(netdev);
5428 if (running)
5429 rtnl_lock();
5430 netdev_lock(netdev);
5431
5432 if (running)
5433 iavf_down(adapter);
5434
5435 iavf_free_misc_irq(adapter);
5436 iavf_reset_interrupt_capability(adapter);
5437
5438 netdev_unlock(netdev);
5439 if (running)
5440 rtnl_unlock();
5441
5442 return 0;
5443 }
5444
5445 /**
5446 * iavf_resume - Power management resume routine
5447 * @dev_d: device info pointer
5448 *
5449 * Called when the system (VM) is resumed from sleep/suspend.
5450 **/
5451 static int iavf_resume(struct device *dev_d)
5452 {
5453 struct pci_dev *pdev = to_pci_dev(dev_d);
5454 struct iavf_adapter *adapter;
5455 int err;
5456
5457 adapter = iavf_pdev_to_adapter(pdev);
5458
5459 pci_set_master(pdev);
5460
5461 rtnl_lock();
5462 err = iavf_set_interrupt_capability(adapter);
5463 if (err) {
5464 rtnl_unlock();
5465 dev_err(&pdev->dev, "Cannot enable MSI-X interrupts.\n");
5466 return err;
5467 }
5468 err = iavf_request_misc_irq(adapter);
5469 rtnl_unlock();
5470 if (err) {
5471 dev_err(&pdev->dev, "Cannot get interrupt vector.\n");
5472 return err;
5473 }
5474
5475 queue_work(adapter->wq, &adapter->reset_task);
5476
5477 netif_device_attach(adapter->netdev);
5478
5479 return err;
5480 }
5481
5482 /**
5483 * iavf_remove - Device Removal Routine
5484 * @pdev: PCI device information struct
5485 *
5486 * iavf_remove is called by the PCI subsystem to alert the driver
5487 * that it should release a PCI device. The could be caused by a
5488 * Hot-Plug event, or because the driver is going to be removed from
5489 * memory.
5490 **/
5491 static void iavf_remove(struct pci_dev *pdev)
5492 {
5493 struct iavf_fdir_fltr *fdir, *fdirtmp;
5494 struct iavf_vlan_filter *vlf, *vlftmp;
5495 struct iavf_cloud_filter *cf, *cftmp;
5496 struct iavf_adv_rss *rss, *rsstmp;
5497 struct iavf_mac_filter *f, *ftmp;
5498 struct iavf_adapter *adapter;
5499 struct net_device *netdev;
5500 struct iavf_hw *hw;
5501
5502 /* Don't proceed with remove if netdev is already freed */
5503 netdev = pci_get_drvdata(pdev);
5504 if (!netdev)
5505 return;
5506
5507 adapter = iavf_pdev_to_adapter(pdev);
5508 hw = &adapter->hw;
5509
5510 if (test_and_set_bit(__IAVF_IN_REMOVE_TASK, &adapter->crit_section))
5511 return;
5512
5513 /* Wait until port initialization is complete.
5514 * There are flows where register/unregister netdev may race.
5515 */
5516 while (1) {
5517 netdev_lock(netdev);
5518 if (adapter->state == __IAVF_RUNNING ||
5519 adapter->state == __IAVF_DOWN ||
5520 adapter->state == __IAVF_INIT_FAILED) {
5521 netdev_unlock(netdev);
5522 break;
5523 }
5524 /* Simply return if we already went through iavf_shutdown */
5525 if (adapter->state == __IAVF_REMOVE) {
5526 netdev_unlock(netdev);
5527 return;
5528 }
5529
5530 netdev_unlock(netdev);
5531 usleep_range(500, 1000);
5532 }
5533 cancel_delayed_work_sync(&adapter->watchdog_task);
5534 cancel_work_sync(&adapter->finish_config);
5535
5536 if (netdev->reg_state == NETREG_REGISTERED)
5537 unregister_netdev(netdev);
5538
5539 netdev_lock(netdev);
5540 dev_info(&adapter->pdev->dev, "Removing device\n");
5541 iavf_change_state(adapter, __IAVF_REMOVE);
5542
5543 iavf_request_reset(adapter);
5544 msleep(50);
5545 /* If the FW isn't responding, kick it once, but only once. */
5546 if (!iavf_asq_done(hw)) {
5547 iavf_request_reset(adapter);
5548 msleep(50);
5549 }
5550
5551 iavf_ptp_release(adapter);
5552
5553 iavf_misc_irq_disable(adapter);
5554 /* Shut down all the garbage mashers on the detention level */
5555 netdev_unlock(netdev);
5556 cancel_work_sync(&adapter->reset_task);
5557 cancel_delayed_work_sync(&adapter->watchdog_task);
5558 cancel_work_sync(&adapter->adminq_task);
5559 netdev_lock(netdev);
5560
5561 adapter->aq_required = 0;
5562 adapter->flags &= ~IAVF_FLAG_REINIT_ITR_NEEDED;
5563
5564 iavf_free_all_tx_resources(adapter);
5565 iavf_free_all_rx_resources(adapter);
5566 iavf_free_misc_irq(adapter);
5567 iavf_free_interrupt_scheme(adapter);
5568
5569 iavf_free_rss(adapter);
5570
5571 if (hw->aq.asq.count)
5572 iavf_shutdown_adminq(hw);
5573
5574 /* destroy the locks only once, here */
5575 mutex_destroy(&hw->aq.arq_mutex);
5576 mutex_destroy(&hw->aq.asq_mutex);
5577 netdev_unlock(netdev);
5578
5579 iounmap(hw->hw_addr);
5580 pci_release_regions(pdev);
5581 kfree(adapter->vf_res);
5582 spin_lock_bh(&adapter->mac_vlan_list_lock);
5583 /* If we got removed before an up/down sequence, we've got a filter
5584 * hanging out there that we need to get rid of.
5585 */
5586 list_for_each_entry_safe(f, ftmp, &adapter->mac_filter_list, list) {
5587 list_del(&f->list);
5588 kfree(f);
5589 }
5590 list_for_each_entry_safe(vlf, vlftmp, &adapter->vlan_filter_list,
5591 list) {
5592 list_del(&vlf->list);
5593 kfree(vlf);
5594 }
5595
5596 spin_unlock_bh(&adapter->mac_vlan_list_lock);
5597
5598 spin_lock_bh(&adapter->cloud_filter_list_lock);
5599 list_for_each_entry_safe(cf, cftmp, &adapter->cloud_filter_list, list) {
5600 list_del(&cf->list);
5601 kfree(cf);
5602 }
5603 spin_unlock_bh(&adapter->cloud_filter_list_lock);
5604
5605 spin_lock_bh(&adapter->fdir_fltr_lock);
5606 list_for_each_entry_safe(fdir, fdirtmp, &adapter->fdir_list_head, list) {
5607 list_del(&fdir->list);
5608 kfree(fdir);
5609 }
5610 spin_unlock_bh(&adapter->fdir_fltr_lock);
5611
5612 spin_lock_bh(&adapter->adv_rss_lock);
5613 list_for_each_entry_safe(rss, rsstmp, &adapter->adv_rss_list_head,
5614 list) {
5615 list_del(&rss->list);
5616 kfree(rss);
5617 }
5618 spin_unlock_bh(&adapter->adv_rss_lock);
5619
5620 destroy_workqueue(adapter->wq);
5621
5622 pci_set_drvdata(pdev, NULL);
5623
5624 free_netdev(netdev);
5625
5626 pci_disable_device(pdev);
5627 }
5628
5629 /**
5630 * iavf_shutdown - Shutdown the device in preparation for a reboot
5631 * @pdev: pci device structure
5632 **/
5633 static void iavf_shutdown(struct pci_dev *pdev)
5634 {
5635 iavf_remove(pdev);
5636
5637 if (system_state == SYSTEM_POWER_OFF)
5638 pci_set_power_state(pdev, PCI_D3hot);
5639 }
5640
5641 static DEFINE_SIMPLE_DEV_PM_OPS(iavf_pm_ops, iavf_suspend, iavf_resume);
5642
5643 static struct pci_driver iavf_driver = {
5644 .name = iavf_driver_name,
5645 .id_table = iavf_pci_tbl,
5646 .probe = iavf_probe,
5647 .remove = iavf_remove,
5648 .driver.pm = pm_sleep_ptr(&iavf_pm_ops),
5649 .shutdown = iavf_shutdown,
5650 };
5651
5652 /**
5653 * iavf_init_module - Driver Registration Routine
5654 *
5655 * iavf_init_module is the first routine called when the driver is
5656 * loaded. All it does is register with the PCI subsystem.
5657 **/
5658 static int __init iavf_init_module(void)
5659 {
5660 pr_info("iavf: %s\n", iavf_driver_string);
5661
5662 pr_info("%s\n", iavf_copyright);
5663
5664 return pci_register_driver(&iavf_driver);
5665 }
5666
5667 module_init(iavf_init_module);
5668
5669 /**
5670 * iavf_exit_module - Driver Exit Cleanup Routine
5671 *
5672 * iavf_exit_module is called just before the driver is removed
5673 * from memory.
5674 **/
5675 static void __exit iavf_exit_module(void)
5676 {
5677 pci_unregister_driver(&iavf_driver);
5678 }
5679
5680 module_exit(iavf_exit_module);
5681
5682 /* iavf_main.c */