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