1 // SPDX-License-Identifier: GPL-2.0-only
2 /* Copyright (C) 2023 Intel Corporation */
5 #include "idpf_virtchnl.h"
7 static const struct net_device_ops idpf_netdev_ops;
10 * idpf_init_vector_stack - Fill the MSIX vector stack with vector index
11 * @adapter: private data struct
13 * Return 0 on success, error on failure
15 static int idpf_init_vector_stack(struct idpf_adapter *adapter)
17 struct idpf_vector_lifo *stack;
21 mutex_lock(&adapter->vector_lock);
22 min_vec = adapter->num_msix_entries - adapter->num_avail_msix;
23 stack = &adapter->vector_stack;
24 stack->size = adapter->num_msix_entries;
25 /* set the base and top to point at start of the 'free pool' to
26 * distribute the unused vectors on-demand basis
28 stack->base = min_vec;
31 stack->vec_idx = kcalloc(stack->size, sizeof(u16), GFP_KERNEL);
32 if (!stack->vec_idx) {
33 mutex_unlock(&adapter->vector_lock);
38 for (i = 0; i < stack->size; i++)
39 stack->vec_idx[i] = i;
41 mutex_unlock(&adapter->vector_lock);
47 * idpf_deinit_vector_stack - zero out the MSIX vector stack
48 * @adapter: private data struct
50 static void idpf_deinit_vector_stack(struct idpf_adapter *adapter)
52 struct idpf_vector_lifo *stack;
54 mutex_lock(&adapter->vector_lock);
55 stack = &adapter->vector_stack;
56 kfree(stack->vec_idx);
57 stack->vec_idx = NULL;
58 mutex_unlock(&adapter->vector_lock);
62 * idpf_mb_intr_rel_irq - Free the IRQ association with the OS
63 * @adapter: adapter structure
65 * This will also disable interrupt mode and queue up mailbox task. Mailbox
66 * task will reschedule itself if not in interrupt mode.
68 static void idpf_mb_intr_rel_irq(struct idpf_adapter *adapter)
70 clear_bit(IDPF_MB_INTR_MODE, adapter->flags);
71 kfree(free_irq(adapter->msix_entries[0].vector, adapter));
72 queue_delayed_work(adapter->mbx_wq, &adapter->mbx_task, 0);
76 * idpf_intr_rel - Release interrupt capabilities and free memory
77 * @adapter: adapter to disable interrupts on
79 void idpf_intr_rel(struct idpf_adapter *adapter)
81 if (!adapter->msix_entries)
84 idpf_mb_intr_rel_irq(adapter);
85 pci_free_irq_vectors(adapter->pdev);
86 idpf_send_dealloc_vectors_msg(adapter);
87 idpf_deinit_vector_stack(adapter);
88 kfree(adapter->msix_entries);
89 adapter->msix_entries = NULL;
93 * idpf_mb_intr_clean - Interrupt handler for the mailbox
94 * @irq: interrupt number
95 * @data: pointer to the adapter structure
97 static irqreturn_t idpf_mb_intr_clean(int __always_unused irq, void *data)
99 struct idpf_adapter *adapter = (struct idpf_adapter *)data;
101 queue_delayed_work(adapter->mbx_wq, &adapter->mbx_task, 0);
107 * idpf_mb_irq_enable - Enable MSIX interrupt for the mailbox
108 * @adapter: adapter to get the hardware address for register write
110 static void idpf_mb_irq_enable(struct idpf_adapter *adapter)
112 struct idpf_intr_reg *intr = &adapter->mb_vector.intr_reg;
115 val = intr->dyn_ctl_intena_m | intr->dyn_ctl_itridx_m;
116 writel(val, intr->dyn_ctl);
117 writel(intr->icr_ena_ctlq_m, intr->icr_ena);
121 * idpf_mb_intr_req_irq - Request irq for the mailbox interrupt
122 * @adapter: adapter structure to pass to the mailbox irq handler
124 static int idpf_mb_intr_req_irq(struct idpf_adapter *adapter)
126 int irq_num, mb_vidx = 0, err;
129 irq_num = adapter->msix_entries[mb_vidx].vector;
130 name = kasprintf(GFP_KERNEL, "%s-%s-%d",
131 dev_driver_string(&adapter->pdev->dev),
133 err = request_irq(irq_num, adapter->irq_mb_handler, 0, name, adapter);
135 dev_err(&adapter->pdev->dev,
136 "IRQ request for mailbox failed, error: %d\n", err);
141 set_bit(IDPF_MB_INTR_MODE, adapter->flags);
147 * idpf_set_mb_vec_id - Set vector index for mailbox
148 * @adapter: adapter structure to access the vector chunks
150 * The first vector id in the requested vector chunks from the CP is for
153 static void idpf_set_mb_vec_id(struct idpf_adapter *adapter)
155 if (adapter->req_vec_chunks)
156 adapter->mb_vector.v_idx =
157 le16_to_cpu(adapter->caps.mailbox_vector_id);
159 adapter->mb_vector.v_idx = 0;
163 * idpf_mb_intr_init - Initialize the mailbox interrupt
164 * @adapter: adapter structure to store the mailbox vector
166 static int idpf_mb_intr_init(struct idpf_adapter *adapter)
168 adapter->dev_ops.reg_ops.mb_intr_reg_init(adapter);
169 adapter->irq_mb_handler = idpf_mb_intr_clean;
171 return idpf_mb_intr_req_irq(adapter);
175 * idpf_vector_lifo_push - push MSIX vector index onto stack
176 * @adapter: private data struct
177 * @vec_idx: vector index to store
179 static int idpf_vector_lifo_push(struct idpf_adapter *adapter, u16 vec_idx)
181 struct idpf_vector_lifo *stack = &adapter->vector_stack;
183 lockdep_assert_held(&adapter->vector_lock);
185 if (stack->top == stack->base) {
186 dev_err(&adapter->pdev->dev, "Exceeded the vector stack limit: %d\n",
191 stack->vec_idx[--stack->top] = vec_idx;
197 * idpf_vector_lifo_pop - pop MSIX vector index from stack
198 * @adapter: private data struct
200 static int idpf_vector_lifo_pop(struct idpf_adapter *adapter)
202 struct idpf_vector_lifo *stack = &adapter->vector_stack;
204 lockdep_assert_held(&adapter->vector_lock);
206 if (stack->top == stack->size) {
207 dev_err(&adapter->pdev->dev, "No interrupt vectors are available to distribute!\n");
212 return stack->vec_idx[stack->top++];
216 * idpf_vector_stash - Store the vector indexes onto the stack
217 * @adapter: private data struct
218 * @q_vector_idxs: vector index array
219 * @vec_info: info related to the number of vectors
221 * This function is a no-op if there are no vectors indexes to be stashed
223 static void idpf_vector_stash(struct idpf_adapter *adapter, u16 *q_vector_idxs,
224 struct idpf_vector_info *vec_info)
229 lockdep_assert_held(&adapter->vector_lock);
231 if (!vec_info->num_curr_vecs)
234 /* For default vports, no need to stash vector allocated from the
235 * default pool onto the stack
237 if (vec_info->default_vport)
238 base = IDPF_MIN_Q_VEC;
240 for (i = vec_info->num_curr_vecs - 1; i >= base ; i--) {
241 vec_idx = q_vector_idxs[i];
242 idpf_vector_lifo_push(adapter, vec_idx);
243 adapter->num_avail_msix++;
248 * idpf_req_rel_vector_indexes - Request or release MSIX vector indexes
249 * @adapter: driver specific private structure
250 * @q_vector_idxs: vector index array
251 * @vec_info: info related to the number of vectors
253 * This is the core function to distribute the MSIX vectors acquired from the
254 * OS. It expects the caller to pass the number of vectors required and
255 * also previously allocated. First, it stashes previously allocated vector
256 * indexes on to the stack and then figures out if it can allocate requested
257 * vectors. It can wait on acquiring the mutex lock. If the caller passes 0 as
258 * requested vectors, then this function just stashes the already allocated
259 * vectors and returns 0.
261 * Returns actual number of vectors allocated on success, error value on failure
262 * If 0 is returned, implies the stack has no vectors to allocate which is also
263 * a failure case for the caller
265 int idpf_req_rel_vector_indexes(struct idpf_adapter *adapter,
267 struct idpf_vector_info *vec_info)
269 u16 num_req_vecs, num_alloc_vecs = 0, max_vecs;
270 struct idpf_vector_lifo *stack;
273 mutex_lock(&adapter->vector_lock);
274 stack = &adapter->vector_stack;
275 num_req_vecs = vec_info->num_req_vecs;
277 /* Stash interrupt vector indexes onto the stack if required */
278 idpf_vector_stash(adapter, q_vector_idxs, vec_info);
283 if (vec_info->default_vport) {
284 /* As IDPF_MIN_Q_VEC per default vport is put aside in the
285 * default pool of the stack, use them for default vports
287 j = vec_info->index * IDPF_MIN_Q_VEC + IDPF_MBX_Q_VEC;
288 for (i = 0; i < IDPF_MIN_Q_VEC; i++) {
289 q_vector_idxs[num_alloc_vecs++] = stack->vec_idx[j++];
294 /* Find if stack has enough vector to allocate */
295 max_vecs = min(adapter->num_avail_msix, num_req_vecs);
297 for (j = 0; j < max_vecs; j++) {
298 vecid = idpf_vector_lifo_pop(adapter);
299 q_vector_idxs[num_alloc_vecs++] = vecid;
301 adapter->num_avail_msix -= max_vecs;
304 mutex_unlock(&adapter->vector_lock);
306 return num_alloc_vecs;
310 * idpf_intr_req - Request interrupt capabilities
311 * @adapter: adapter to enable interrupts on
313 * Returns 0 on success, negative on failure
315 int idpf_intr_req(struct idpf_adapter *adapter)
317 u16 default_vports = idpf_get_default_vports(adapter);
318 int num_q_vecs, total_vecs, num_vec_ids;
319 int min_vectors, v_actual, err;
323 total_vecs = idpf_get_reserved_vecs(adapter);
324 num_q_vecs = total_vecs - IDPF_MBX_Q_VEC;
326 err = idpf_send_alloc_vectors_msg(adapter, num_q_vecs);
328 dev_err(&adapter->pdev->dev,
329 "Failed to allocate %d vectors: %d\n", num_q_vecs, err);
334 min_vectors = IDPF_MBX_Q_VEC + IDPF_MIN_Q_VEC * default_vports;
335 v_actual = pci_alloc_irq_vectors(adapter->pdev, min_vectors,
336 total_vecs, PCI_IRQ_MSIX);
337 if (v_actual < min_vectors) {
338 dev_err(&adapter->pdev->dev, "Failed to allocate MSIX vectors: %d\n",
341 goto send_dealloc_vecs;
344 adapter->msix_entries = kcalloc(v_actual, sizeof(struct msix_entry),
347 if (!adapter->msix_entries) {
352 idpf_set_mb_vec_id(adapter);
354 vecids = kcalloc(total_vecs, sizeof(u16), GFP_KERNEL);
360 num_vec_ids = idpf_get_vec_ids(adapter, vecids, total_vecs,
361 &adapter->req_vec_chunks->vchunks);
362 if (num_vec_ids < v_actual) {
367 for (vector = 0; vector < v_actual; vector++) {
368 adapter->msix_entries[vector].entry = vecids[vector];
369 adapter->msix_entries[vector].vector =
370 pci_irq_vector(adapter->pdev, vector);
373 adapter->num_req_msix = total_vecs;
374 adapter->num_msix_entries = v_actual;
375 /* 'num_avail_msix' is used to distribute excess vectors to the vports
376 * after considering the minimum vectors required per each default
379 adapter->num_avail_msix = v_actual - min_vectors;
381 /* Fill MSIX vector lifo stack with vector indexes */
382 err = idpf_init_vector_stack(adapter);
386 err = idpf_mb_intr_init(adapter);
388 goto deinit_vec_stack;
389 idpf_mb_irq_enable(adapter);
395 idpf_deinit_vector_stack(adapter);
399 kfree(adapter->msix_entries);
400 adapter->msix_entries = NULL;
402 pci_free_irq_vectors(adapter->pdev);
404 idpf_send_dealloc_vectors_msg(adapter);
410 * idpf_find_mac_filter - Search filter list for specific mac filter
411 * @vconfig: Vport config structure
412 * @macaddr: The MAC address
414 * Returns ptr to the filter object or NULL. Must be called while holding the
415 * mac_filter_list_lock.
417 static struct idpf_mac_filter *idpf_find_mac_filter(struct idpf_vport_config *vconfig,
420 struct idpf_mac_filter *f;
425 list_for_each_entry(f, &vconfig->user_config.mac_filter_list, list) {
426 if (ether_addr_equal(macaddr, f->macaddr))
434 * __idpf_del_mac_filter - Delete a MAC filter from the filter list
435 * @vport_config: Vport config structure
436 * @macaddr: The MAC address
438 * Returns 0 on success, error value on failure
440 static int __idpf_del_mac_filter(struct idpf_vport_config *vport_config,
443 struct idpf_mac_filter *f;
445 spin_lock_bh(&vport_config->mac_filter_list_lock);
446 f = idpf_find_mac_filter(vport_config, macaddr);
451 spin_unlock_bh(&vport_config->mac_filter_list_lock);
457 * idpf_del_mac_filter - Delete a MAC filter from the filter list
458 * @vport: Main vport structure
459 * @np: Netdev private structure
460 * @macaddr: The MAC address
461 * @async: Don't wait for return message
463 * Removes filter from list and if interface is up, tells hardware about the
466 static int idpf_del_mac_filter(struct idpf_vport *vport,
467 struct idpf_netdev_priv *np,
468 const u8 *macaddr, bool async)
470 struct idpf_vport_config *vport_config;
471 struct idpf_mac_filter *f;
473 vport_config = np->adapter->vport_config[np->vport_idx];
475 spin_lock_bh(&vport_config->mac_filter_list_lock);
476 f = idpf_find_mac_filter(vport_config, macaddr);
480 spin_unlock_bh(&vport_config->mac_filter_list_lock);
484 spin_unlock_bh(&vport_config->mac_filter_list_lock);
486 if (np->state == __IDPF_VPORT_UP) {
489 err = idpf_add_del_mac_filters(vport, np, false, async);
494 return __idpf_del_mac_filter(vport_config, macaddr);
498 * __idpf_add_mac_filter - Add mac filter helper function
499 * @vport_config: Vport config structure
500 * @macaddr: Address to add
502 * Takes mac_filter_list_lock spinlock to add new filter to list.
504 static int __idpf_add_mac_filter(struct idpf_vport_config *vport_config,
507 struct idpf_mac_filter *f;
509 spin_lock_bh(&vport_config->mac_filter_list_lock);
511 f = idpf_find_mac_filter(vport_config, macaddr);
514 spin_unlock_bh(&vport_config->mac_filter_list_lock);
519 f = kzalloc(sizeof(*f), GFP_ATOMIC);
521 spin_unlock_bh(&vport_config->mac_filter_list_lock);
526 ether_addr_copy(f->macaddr, macaddr);
527 list_add_tail(&f->list, &vport_config->user_config.mac_filter_list);
530 spin_unlock_bh(&vport_config->mac_filter_list_lock);
536 * idpf_add_mac_filter - Add a mac filter to the filter list
537 * @vport: Main vport structure
538 * @np: Netdev private structure
539 * @macaddr: The MAC address
540 * @async: Don't wait for return message
542 * Returns 0 on success or error on failure. If interface is up, we'll also
543 * send the virtchnl message to tell hardware about the filter.
545 static int idpf_add_mac_filter(struct idpf_vport *vport,
546 struct idpf_netdev_priv *np,
547 const u8 *macaddr, bool async)
549 struct idpf_vport_config *vport_config;
552 vport_config = np->adapter->vport_config[np->vport_idx];
553 err = __idpf_add_mac_filter(vport_config, macaddr);
557 if (np->state == __IDPF_VPORT_UP)
558 err = idpf_add_del_mac_filters(vport, np, true, async);
564 * idpf_del_all_mac_filters - Delete all MAC filters in list
565 * @vport: main vport struct
567 * Takes mac_filter_list_lock spinlock. Deletes all filters
569 static void idpf_del_all_mac_filters(struct idpf_vport *vport)
571 struct idpf_vport_config *vport_config;
572 struct idpf_mac_filter *f, *ftmp;
574 vport_config = vport->adapter->vport_config[vport->idx];
575 spin_lock_bh(&vport_config->mac_filter_list_lock);
577 list_for_each_entry_safe(f, ftmp, &vport_config->user_config.mac_filter_list,
583 spin_unlock_bh(&vport_config->mac_filter_list_lock);
587 * idpf_restore_mac_filters - Re-add all MAC filters in list
588 * @vport: main vport struct
590 * Takes mac_filter_list_lock spinlock. Sets add field to true for filters to
591 * resync filters back to HW.
593 static void idpf_restore_mac_filters(struct idpf_vport *vport)
595 struct idpf_vport_config *vport_config;
596 struct idpf_mac_filter *f;
598 vport_config = vport->adapter->vport_config[vport->idx];
599 spin_lock_bh(&vport_config->mac_filter_list_lock);
601 list_for_each_entry(f, &vport_config->user_config.mac_filter_list, list)
604 spin_unlock_bh(&vport_config->mac_filter_list_lock);
606 idpf_add_del_mac_filters(vport, netdev_priv(vport->netdev),
611 * idpf_remove_mac_filters - Remove all MAC filters in list
612 * @vport: main vport struct
614 * Takes mac_filter_list_lock spinlock. Sets remove field to true for filters
615 * to remove filters in HW.
617 static void idpf_remove_mac_filters(struct idpf_vport *vport)
619 struct idpf_vport_config *vport_config;
620 struct idpf_mac_filter *f;
622 vport_config = vport->adapter->vport_config[vport->idx];
623 spin_lock_bh(&vport_config->mac_filter_list_lock);
625 list_for_each_entry(f, &vport_config->user_config.mac_filter_list, list)
628 spin_unlock_bh(&vport_config->mac_filter_list_lock);
630 idpf_add_del_mac_filters(vport, netdev_priv(vport->netdev),
635 * idpf_deinit_mac_addr - deinitialize mac address for vport
636 * @vport: main vport structure
638 static void idpf_deinit_mac_addr(struct idpf_vport *vport)
640 struct idpf_vport_config *vport_config;
641 struct idpf_mac_filter *f;
643 vport_config = vport->adapter->vport_config[vport->idx];
645 spin_lock_bh(&vport_config->mac_filter_list_lock);
647 f = idpf_find_mac_filter(vport_config, vport->default_mac_addr);
653 spin_unlock_bh(&vport_config->mac_filter_list_lock);
657 * idpf_init_mac_addr - initialize mac address for vport
658 * @vport: main vport structure
659 * @netdev: pointer to netdev struct associated with this vport
661 static int idpf_init_mac_addr(struct idpf_vport *vport,
662 struct net_device *netdev)
664 struct idpf_netdev_priv *np = netdev_priv(netdev);
665 struct idpf_adapter *adapter = vport->adapter;
668 if (is_valid_ether_addr(vport->default_mac_addr)) {
669 eth_hw_addr_set(netdev, vport->default_mac_addr);
670 ether_addr_copy(netdev->perm_addr, vport->default_mac_addr);
672 return idpf_add_mac_filter(vport, np, vport->default_mac_addr,
676 if (!idpf_is_cap_ena(adapter, IDPF_OTHER_CAPS,
677 VIRTCHNL2_CAP_MACFILTER)) {
678 dev_err(&adapter->pdev->dev,
679 "MAC address is not provided and capability is not set\n");
684 eth_hw_addr_random(netdev);
685 err = idpf_add_mac_filter(vport, np, netdev->dev_addr, false);
689 dev_info(&adapter->pdev->dev, "Invalid MAC address %pM, using random %pM\n",
690 vport->default_mac_addr, netdev->dev_addr);
691 ether_addr_copy(vport->default_mac_addr, netdev->dev_addr);
697 * idpf_cfg_netdev - Allocate, configure and register a netdev
698 * @vport: main vport structure
700 * Returns 0 on success, negative value on failure.
702 static int idpf_cfg_netdev(struct idpf_vport *vport)
704 struct idpf_adapter *adapter = vport->adapter;
705 struct idpf_vport_config *vport_config;
706 netdev_features_t dflt_features;
707 netdev_features_t offloads = 0;
708 struct idpf_netdev_priv *np;
709 struct net_device *netdev;
710 u16 idx = vport->idx;
713 vport_config = adapter->vport_config[idx];
715 /* It's possible we already have a netdev allocated and registered for
718 if (test_bit(IDPF_VPORT_REG_NETDEV, vport_config->flags)) {
719 netdev = adapter->netdevs[idx];
720 np = netdev_priv(netdev);
722 np->vport_idx = vport->idx;
723 np->vport_id = vport->vport_id;
724 vport->netdev = netdev;
726 return idpf_init_mac_addr(vport, netdev);
729 netdev = alloc_etherdev_mqs(sizeof(struct idpf_netdev_priv),
730 vport_config->max_q.max_txq,
731 vport_config->max_q.max_rxq);
735 vport->netdev = netdev;
736 np = netdev_priv(netdev);
738 np->adapter = adapter;
739 np->vport_idx = vport->idx;
740 np->vport_id = vport->vport_id;
742 spin_lock_init(&np->stats_lock);
744 err = idpf_init_mac_addr(vport, netdev);
746 free_netdev(vport->netdev);
747 vport->netdev = NULL;
752 /* assign netdev_ops */
753 netdev->netdev_ops = &idpf_netdev_ops;
755 /* setup watchdog timeout value to be 5 second */
756 netdev->watchdog_timeo = 5 * HZ;
758 netdev->dev_port = idx;
760 /* configure default MTU size */
761 netdev->min_mtu = ETH_MIN_MTU;
762 netdev->max_mtu = vport->max_mtu;
764 dflt_features = NETIF_F_SG |
767 if (idpf_is_cap_ena_all(adapter, IDPF_RSS_CAPS, IDPF_CAP_RSS))
768 dflt_features |= NETIF_F_RXHASH;
769 if (idpf_is_cap_ena_all(adapter, IDPF_CSUM_CAPS, IDPF_CAP_RX_CSUM_L4V4))
770 dflt_features |= NETIF_F_IP_CSUM;
771 if (idpf_is_cap_ena_all(adapter, IDPF_CSUM_CAPS, IDPF_CAP_RX_CSUM_L4V6))
772 dflt_features |= NETIF_F_IPV6_CSUM;
773 if (idpf_is_cap_ena(adapter, IDPF_CSUM_CAPS, IDPF_CAP_RX_CSUM))
774 dflt_features |= NETIF_F_RXCSUM;
775 if (idpf_is_cap_ena_all(adapter, IDPF_CSUM_CAPS, IDPF_CAP_SCTP_CSUM))
776 dflt_features |= NETIF_F_SCTP_CRC;
778 if (idpf_is_cap_ena(adapter, IDPF_SEG_CAPS, VIRTCHNL2_CAP_SEG_IPV4_TCP))
779 dflt_features |= NETIF_F_TSO;
780 if (idpf_is_cap_ena(adapter, IDPF_SEG_CAPS, VIRTCHNL2_CAP_SEG_IPV6_TCP))
781 dflt_features |= NETIF_F_TSO6;
782 if (idpf_is_cap_ena_all(adapter, IDPF_SEG_CAPS,
783 VIRTCHNL2_CAP_SEG_IPV4_UDP |
784 VIRTCHNL2_CAP_SEG_IPV6_UDP))
785 dflt_features |= NETIF_F_GSO_UDP_L4;
786 if (idpf_is_cap_ena_all(adapter, IDPF_RSC_CAPS, IDPF_CAP_RSC))
787 offloads |= NETIF_F_GRO_HW;
788 /* advertise to stack only if offloads for encapsulated packets is
791 if (idpf_is_cap_ena(vport->adapter, IDPF_SEG_CAPS,
792 VIRTCHNL2_CAP_SEG_TX_SINGLE_TUNNEL)) {
793 offloads |= NETIF_F_GSO_UDP_TUNNEL |
795 NETIF_F_GSO_GRE_CSUM |
796 NETIF_F_GSO_PARTIAL |
797 NETIF_F_GSO_UDP_TUNNEL_CSUM |
802 if (!idpf_is_cap_ena_all(vport->adapter, IDPF_CSUM_CAPS,
803 IDPF_CAP_TUNNEL_TX_CSUM))
804 netdev->gso_partial_features |=
805 NETIF_F_GSO_UDP_TUNNEL_CSUM;
807 netdev->gso_partial_features |= NETIF_F_GSO_GRE_CSUM;
808 offloads |= NETIF_F_TSO_MANGLEID;
810 if (idpf_is_cap_ena(adapter, IDPF_OTHER_CAPS, VIRTCHNL2_CAP_LOOPBACK))
811 offloads |= NETIF_F_LOOPBACK;
813 netdev->features |= dflt_features;
814 netdev->hw_features |= dflt_features | offloads;
815 netdev->hw_enc_features |= dflt_features | offloads;
816 idpf_set_ethtool_ops(netdev);
817 SET_NETDEV_DEV(netdev, &adapter->pdev->dev);
819 /* carrier off on init to avoid Tx hangs */
820 netif_carrier_off(netdev);
822 /* make sure transmit queues start off as stopped */
823 netif_tx_stop_all_queues(netdev);
825 /* The vport can be arbitrarily released so we need to also track
826 * netdevs in the adapter struct
828 adapter->netdevs[idx] = netdev;
834 * idpf_get_free_slot - get the next non-NULL location index in array
835 * @adapter: adapter in which to look for a free vport slot
837 static int idpf_get_free_slot(struct idpf_adapter *adapter)
841 for (i = 0; i < adapter->max_vports; i++) {
842 if (!adapter->vports[i])
846 return IDPF_NO_FREE_SLOT;
850 * idpf_remove_features - Turn off feature configs
851 * @vport: virtual port structure
853 static void idpf_remove_features(struct idpf_vport *vport)
855 struct idpf_adapter *adapter = vport->adapter;
857 if (idpf_is_cap_ena(adapter, IDPF_OTHER_CAPS, VIRTCHNL2_CAP_MACFILTER))
858 idpf_remove_mac_filters(vport);
862 * idpf_vport_stop - Disable a vport
863 * @vport: vport to disable
865 static void idpf_vport_stop(struct idpf_vport *vport)
867 struct idpf_netdev_priv *np = netdev_priv(vport->netdev);
869 if (np->state <= __IDPF_VPORT_DOWN)
872 netif_carrier_off(vport->netdev);
873 netif_tx_disable(vport->netdev);
875 idpf_send_disable_vport_msg(vport);
876 idpf_send_disable_queues_msg(vport);
877 idpf_send_map_unmap_queue_vector_msg(vport, false);
878 /* Normally we ask for queues in create_vport, but if the number of
879 * initially requested queues have changed, for example via ethtool
880 * set channels, we do delete queues and then add the queues back
881 * instead of deleting and reallocating the vport.
883 if (test_and_clear_bit(IDPF_VPORT_DEL_QUEUES, vport->flags))
884 idpf_send_delete_queues_msg(vport);
886 idpf_remove_features(vport);
888 vport->link_up = false;
889 idpf_vport_intr_deinit(vport);
890 idpf_vport_queues_rel(vport);
891 idpf_vport_intr_rel(vport);
892 np->state = __IDPF_VPORT_DOWN;
896 * idpf_stop - Disables a network interface
897 * @netdev: network interface device structure
899 * The stop entry point is called when an interface is de-activated by the OS,
900 * and the netdevice enters the DOWN state. The hardware is still under the
901 * driver's control, but the netdev interface is disabled.
903 * Returns success only - not allowed to fail
905 static int idpf_stop(struct net_device *netdev)
907 struct idpf_netdev_priv *np = netdev_priv(netdev);
908 struct idpf_vport *vport;
910 if (test_bit(IDPF_REMOVE_IN_PROG, np->adapter->flags))
913 idpf_vport_ctrl_lock(netdev);
914 vport = idpf_netdev_to_vport(netdev);
916 idpf_vport_stop(vport);
918 idpf_vport_ctrl_unlock(netdev);
924 * idpf_decfg_netdev - Unregister the netdev
925 * @vport: vport for which netdev to be unregistered
927 static void idpf_decfg_netdev(struct idpf_vport *vport)
929 struct idpf_adapter *adapter = vport->adapter;
931 kfree(vport->rx_ptype_lkup);
932 vport->rx_ptype_lkup = NULL;
934 unregister_netdev(vport->netdev);
935 free_netdev(vport->netdev);
936 vport->netdev = NULL;
938 adapter->netdevs[vport->idx] = NULL;
942 * idpf_vport_rel - Delete a vport and free its resources
943 * @vport: the vport being removed
945 static void idpf_vport_rel(struct idpf_vport *vport)
947 struct idpf_adapter *adapter = vport->adapter;
948 struct idpf_vport_config *vport_config;
949 struct idpf_vector_info vec_info;
950 struct idpf_rss_data *rss_data;
951 struct idpf_vport_max_q max_q;
952 u16 idx = vport->idx;
954 vport_config = adapter->vport_config[vport->idx];
955 idpf_deinit_rss(vport);
956 rss_data = &vport_config->user_config.rss_data;
957 kfree(rss_data->rss_key);
958 rss_data->rss_key = NULL;
960 idpf_send_destroy_vport_msg(vport);
962 /* Release all max queues allocated to the adapter's pool */
963 max_q.max_rxq = vport_config->max_q.max_rxq;
964 max_q.max_txq = vport_config->max_q.max_txq;
965 max_q.max_bufq = vport_config->max_q.max_bufq;
966 max_q.max_complq = vport_config->max_q.max_complq;
967 idpf_vport_dealloc_max_qs(adapter, &max_q);
969 /* Release all the allocated vectors on the stack */
970 vec_info.num_req_vecs = 0;
971 vec_info.num_curr_vecs = vport->num_q_vectors;
972 vec_info.default_vport = vport->default_vport;
974 idpf_req_rel_vector_indexes(adapter, vport->q_vector_idxs, &vec_info);
976 kfree(vport->q_vector_idxs);
977 vport->q_vector_idxs = NULL;
979 kfree(adapter->vport_params_recvd[idx]);
980 adapter->vport_params_recvd[idx] = NULL;
981 kfree(adapter->vport_params_reqd[idx]);
982 adapter->vport_params_reqd[idx] = NULL;
983 if (adapter->vport_config[idx]) {
984 kfree(adapter->vport_config[idx]->req_qs_chunks);
985 adapter->vport_config[idx]->req_qs_chunks = NULL;
988 adapter->num_alloc_vports--;
992 * idpf_vport_dealloc - cleanup and release a given vport
993 * @vport: pointer to idpf vport structure
997 static void idpf_vport_dealloc(struct idpf_vport *vport)
999 struct idpf_adapter *adapter = vport->adapter;
1000 unsigned int i = vport->idx;
1002 idpf_deinit_mac_addr(vport);
1003 idpf_vport_stop(vport);
1005 if (!test_bit(IDPF_HR_RESET_IN_PROG, adapter->flags))
1006 idpf_decfg_netdev(vport);
1007 if (test_bit(IDPF_REMOVE_IN_PROG, adapter->flags))
1008 idpf_del_all_mac_filters(vport);
1010 if (adapter->netdevs[i]) {
1011 struct idpf_netdev_priv *np = netdev_priv(adapter->netdevs[i]);
1016 idpf_vport_rel(vport);
1018 adapter->vports[i] = NULL;
1019 adapter->next_vport = idpf_get_free_slot(adapter);
1023 * idpf_is_hsplit_supported - check whether the header split is supported
1024 * @vport: virtual port to check the capability for
1026 * Return: true if it's supported by the HW/FW, false if not.
1028 static bool idpf_is_hsplit_supported(const struct idpf_vport *vport)
1030 return idpf_is_queue_model_split(vport->rxq_model) &&
1031 idpf_is_cap_ena_all(vport->adapter, IDPF_HSPLIT_CAPS,
1036 * idpf_vport_get_hsplit - get the current header split feature state
1037 * @vport: virtual port to query the state for
1039 * Return: ``ETHTOOL_TCP_DATA_SPLIT_UNKNOWN`` if not supported,
1040 * ``ETHTOOL_TCP_DATA_SPLIT_DISABLED`` if disabled,
1041 * ``ETHTOOL_TCP_DATA_SPLIT_ENABLED`` if active.
1043 u8 idpf_vport_get_hsplit(const struct idpf_vport *vport)
1045 const struct idpf_vport_user_config_data *config;
1047 if (!idpf_is_hsplit_supported(vport))
1048 return ETHTOOL_TCP_DATA_SPLIT_UNKNOWN;
1050 config = &vport->adapter->vport_config[vport->idx]->user_config;
1052 return test_bit(__IDPF_USER_FLAG_HSPLIT, config->user_flags) ?
1053 ETHTOOL_TCP_DATA_SPLIT_ENABLED :
1054 ETHTOOL_TCP_DATA_SPLIT_DISABLED;
1058 * idpf_vport_set_hsplit - enable or disable header split on a given vport
1059 * @vport: virtual port to configure
1060 * @val: Ethtool flag controlling the header split state
1062 * Return: true on success, false if not supported by the HW.
1064 bool idpf_vport_set_hsplit(const struct idpf_vport *vport, u8 val)
1066 struct idpf_vport_user_config_data *config;
1068 if (!idpf_is_hsplit_supported(vport))
1069 return val == ETHTOOL_TCP_DATA_SPLIT_UNKNOWN;
1071 config = &vport->adapter->vport_config[vport->idx]->user_config;
1074 case ETHTOOL_TCP_DATA_SPLIT_UNKNOWN:
1075 /* Default is to enable */
1076 case ETHTOOL_TCP_DATA_SPLIT_ENABLED:
1077 __set_bit(__IDPF_USER_FLAG_HSPLIT, config->user_flags);
1079 case ETHTOOL_TCP_DATA_SPLIT_DISABLED:
1080 __clear_bit(__IDPF_USER_FLAG_HSPLIT, config->user_flags);
1088 * idpf_vport_alloc - Allocates the next available struct vport in the adapter
1089 * @adapter: board private structure
1090 * @max_q: vport max queue info
1092 * returns a pointer to a vport on success, NULL on failure.
1094 static struct idpf_vport *idpf_vport_alloc(struct idpf_adapter *adapter,
1095 struct idpf_vport_max_q *max_q)
1097 struct idpf_rss_data *rss_data;
1098 u16 idx = adapter->next_vport;
1099 struct idpf_vport *vport;
1102 if (idx == IDPF_NO_FREE_SLOT)
1105 vport = kzalloc(sizeof(*vport), GFP_KERNEL);
1109 if (!adapter->vport_config[idx]) {
1110 struct idpf_vport_config *vport_config;
1112 vport_config = kzalloc(sizeof(*vport_config), GFP_KERNEL);
1113 if (!vport_config) {
1119 adapter->vport_config[idx] = vport_config;
1123 vport->adapter = adapter;
1124 vport->compln_clean_budget = IDPF_TX_COMPLQ_CLEAN_BUDGET;
1125 vport->default_vport = adapter->num_alloc_vports <
1126 idpf_get_default_vports(adapter);
1128 num_max_q = max(max_q->max_txq, max_q->max_rxq);
1129 vport->q_vector_idxs = kcalloc(num_max_q, sizeof(u16), GFP_KERNEL);
1130 if (!vport->q_vector_idxs) {
1135 idpf_vport_init(vport, max_q);
1137 /* This alloc is done separate from the LUT because it's not strictly
1138 * dependent on how many queues we have. If we change number of queues
1139 * and soft reset we'll need a new LUT but the key can remain the same
1140 * for as long as the vport exists.
1142 rss_data = &adapter->vport_config[idx]->user_config.rss_data;
1143 rss_data->rss_key = kzalloc(rss_data->rss_key_size, GFP_KERNEL);
1144 if (!rss_data->rss_key) {
1149 /* Initialize default rss key */
1150 netdev_rss_key_fill((void *)rss_data->rss_key, rss_data->rss_key_size);
1152 /* fill vport slot in the adapter struct */
1153 adapter->vports[idx] = vport;
1154 adapter->vport_ids[idx] = idpf_get_vport_id(vport);
1156 adapter->num_alloc_vports++;
1157 /* prepare adapter->next_vport for next use */
1158 adapter->next_vport = idpf_get_free_slot(adapter);
1164 * idpf_get_stats64 - get statistics for network device structure
1165 * @netdev: network interface device structure
1166 * @stats: main device statistics structure
1168 static void idpf_get_stats64(struct net_device *netdev,
1169 struct rtnl_link_stats64 *stats)
1171 struct idpf_netdev_priv *np = netdev_priv(netdev);
1173 spin_lock_bh(&np->stats_lock);
1174 *stats = np->netstats;
1175 spin_unlock_bh(&np->stats_lock);
1179 * idpf_statistics_task - Delayed task to get statistics over mailbox
1180 * @work: work_struct handle to our data
1182 void idpf_statistics_task(struct work_struct *work)
1184 struct idpf_adapter *adapter;
1187 adapter = container_of(work, struct idpf_adapter, stats_task.work);
1189 for (i = 0; i < adapter->max_vports; i++) {
1190 struct idpf_vport *vport = adapter->vports[i];
1192 if (vport && !test_bit(IDPF_HR_RESET_IN_PROG, adapter->flags))
1193 idpf_send_get_stats_msg(vport);
1196 queue_delayed_work(adapter->stats_wq, &adapter->stats_task,
1197 msecs_to_jiffies(10000));
1201 * idpf_mbx_task - Delayed task to handle mailbox responses
1202 * @work: work_struct handle
1204 void idpf_mbx_task(struct work_struct *work)
1206 struct idpf_adapter *adapter;
1208 adapter = container_of(work, struct idpf_adapter, mbx_task.work);
1210 if (test_bit(IDPF_MB_INTR_MODE, adapter->flags))
1211 idpf_mb_irq_enable(adapter);
1213 queue_delayed_work(adapter->mbx_wq, &adapter->mbx_task,
1214 msecs_to_jiffies(300));
1216 idpf_recv_mb_msg(adapter);
1220 * idpf_service_task - Delayed task for handling mailbox responses
1221 * @work: work_struct handle to our data
1224 void idpf_service_task(struct work_struct *work)
1226 struct idpf_adapter *adapter;
1228 adapter = container_of(work, struct idpf_adapter, serv_task.work);
1230 if (idpf_is_reset_detected(adapter) &&
1231 !idpf_is_reset_in_prog(adapter) &&
1232 !test_bit(IDPF_REMOVE_IN_PROG, adapter->flags)) {
1233 dev_info(&adapter->pdev->dev, "HW reset detected\n");
1234 set_bit(IDPF_HR_FUNC_RESET, adapter->flags);
1235 queue_delayed_work(adapter->vc_event_wq,
1236 &adapter->vc_event_task,
1237 msecs_to_jiffies(10));
1240 queue_delayed_work(adapter->serv_wq, &adapter->serv_task,
1241 msecs_to_jiffies(300));
1245 * idpf_restore_features - Restore feature configs
1246 * @vport: virtual port structure
1248 static void idpf_restore_features(struct idpf_vport *vport)
1250 struct idpf_adapter *adapter = vport->adapter;
1252 if (idpf_is_cap_ena(adapter, IDPF_OTHER_CAPS, VIRTCHNL2_CAP_MACFILTER))
1253 idpf_restore_mac_filters(vport);
1257 * idpf_set_real_num_queues - set number of queues for netdev
1258 * @vport: virtual port structure
1260 * Returns 0 on success, negative on failure.
1262 static int idpf_set_real_num_queues(struct idpf_vport *vport)
1266 err = netif_set_real_num_rx_queues(vport->netdev, vport->num_rxq);
1270 return netif_set_real_num_tx_queues(vport->netdev, vport->num_txq);
1274 * idpf_up_complete - Complete interface up sequence
1275 * @vport: virtual port structure
1277 * Returns 0 on success, negative on failure.
1279 static int idpf_up_complete(struct idpf_vport *vport)
1281 struct idpf_netdev_priv *np = netdev_priv(vport->netdev);
1283 if (vport->link_up && !netif_carrier_ok(vport->netdev)) {
1284 netif_carrier_on(vport->netdev);
1285 netif_tx_start_all_queues(vport->netdev);
1288 np->state = __IDPF_VPORT_UP;
1294 * idpf_rx_init_buf_tail - Write initial buffer ring tail value
1295 * @vport: virtual port struct
1297 static void idpf_rx_init_buf_tail(struct idpf_vport *vport)
1301 for (i = 0; i < vport->num_rxq_grp; i++) {
1302 struct idpf_rxq_group *grp = &vport->rxq_grps[i];
1304 if (idpf_is_queue_model_split(vport->rxq_model)) {
1305 for (j = 0; j < vport->num_bufqs_per_qgrp; j++) {
1306 const struct idpf_buf_queue *q =
1307 &grp->splitq.bufq_sets[j].bufq;
1309 writel(q->next_to_alloc, q->tail);
1312 for (j = 0; j < grp->singleq.num_rxq; j++) {
1313 const struct idpf_rx_queue *q =
1314 grp->singleq.rxqs[j];
1316 writel(q->next_to_alloc, q->tail);
1323 * idpf_vport_open - Bring up a vport
1324 * @vport: vport to bring up
1326 static int idpf_vport_open(struct idpf_vport *vport)
1328 struct idpf_netdev_priv *np = netdev_priv(vport->netdev);
1329 struct idpf_adapter *adapter = vport->adapter;
1330 struct idpf_vport_config *vport_config;
1333 if (np->state != __IDPF_VPORT_DOWN)
1336 /* we do not allow interface up just yet */
1337 netif_carrier_off(vport->netdev);
1339 err = idpf_vport_intr_alloc(vport);
1341 dev_err(&adapter->pdev->dev, "Failed to allocate interrupts for vport %u: %d\n",
1342 vport->vport_id, err);
1346 err = idpf_vport_queues_alloc(vport);
1350 err = idpf_vport_queue_ids_init(vport);
1352 dev_err(&adapter->pdev->dev, "Failed to initialize queue ids for vport %u: %d\n",
1353 vport->vport_id, err);
1357 err = idpf_vport_intr_init(vport);
1359 dev_err(&adapter->pdev->dev, "Failed to initialize interrupts for vport %u: %d\n",
1360 vport->vport_id, err);
1364 err = idpf_rx_bufs_init_all(vport);
1366 dev_err(&adapter->pdev->dev, "Failed to initialize RX buffers for vport %u: %d\n",
1367 vport->vport_id, err);
1371 err = idpf_queue_reg_init(vport);
1373 dev_err(&adapter->pdev->dev, "Failed to initialize queue registers for vport %u: %d\n",
1374 vport->vport_id, err);
1378 idpf_rx_init_buf_tail(vport);
1379 idpf_vport_intr_ena(vport);
1381 err = idpf_send_config_queues_msg(vport);
1383 dev_err(&adapter->pdev->dev, "Failed to configure queues for vport %u, %d\n",
1384 vport->vport_id, err);
1388 err = idpf_send_map_unmap_queue_vector_msg(vport, true);
1390 dev_err(&adapter->pdev->dev, "Failed to map queue vectors for vport %u: %d\n",
1391 vport->vport_id, err);
1395 err = idpf_send_enable_queues_msg(vport);
1397 dev_err(&adapter->pdev->dev, "Failed to enable queues for vport %u: %d\n",
1398 vport->vport_id, err);
1399 goto unmap_queue_vectors;
1402 err = idpf_send_enable_vport_msg(vport);
1404 dev_err(&adapter->pdev->dev, "Failed to enable vport %u: %d\n",
1405 vport->vport_id, err);
1407 goto disable_queues;
1410 idpf_restore_features(vport);
1412 vport_config = adapter->vport_config[vport->idx];
1413 if (vport_config->user_config.rss_data.rss_lut)
1414 err = idpf_config_rss(vport);
1416 err = idpf_init_rss(vport);
1418 dev_err(&adapter->pdev->dev, "Failed to initialize RSS for vport %u: %d\n",
1419 vport->vport_id, err);
1423 err = idpf_up_complete(vport);
1425 dev_err(&adapter->pdev->dev, "Failed to complete interface up for vport %u: %d\n",
1426 vport->vport_id, err);
1433 idpf_deinit_rss(vport);
1435 idpf_send_disable_vport_msg(vport);
1437 idpf_send_disable_queues_msg(vport);
1438 unmap_queue_vectors:
1439 idpf_send_map_unmap_queue_vector_msg(vport, false);
1441 idpf_vport_intr_deinit(vport);
1443 idpf_vport_queues_rel(vport);
1445 idpf_vport_intr_rel(vport);
1451 * idpf_init_task - Delayed initialization task
1452 * @work: work_struct handle to our data
1454 * Init task finishes up pending work started in probe. Due to the asynchronous
1455 * nature in which the device communicates with hardware, we may have to wait
1456 * several milliseconds to get a response. Instead of busy polling in probe,
1457 * pulling it out into a delayed work task prevents us from bogging down the
1458 * whole system waiting for a response from hardware.
1460 void idpf_init_task(struct work_struct *work)
1462 struct idpf_vport_config *vport_config;
1463 struct idpf_vport_max_q max_q;
1464 struct idpf_adapter *adapter;
1465 struct idpf_netdev_priv *np;
1466 struct idpf_vport *vport;
1467 u16 num_default_vports;
1468 struct pci_dev *pdev;
1472 adapter = container_of(work, struct idpf_adapter, init_task.work);
1474 num_default_vports = idpf_get_default_vports(adapter);
1475 if (adapter->num_alloc_vports < num_default_vports)
1476 default_vport = true;
1478 default_vport = false;
1480 err = idpf_vport_alloc_max_qs(adapter, &max_q);
1484 err = idpf_send_create_vport_msg(adapter, &max_q);
1486 idpf_vport_dealloc_max_qs(adapter, &max_q);
1490 pdev = adapter->pdev;
1491 vport = idpf_vport_alloc(adapter, &max_q);
1494 dev_err(&pdev->dev, "failed to allocate vport: %d\n",
1496 idpf_vport_dealloc_max_qs(adapter, &max_q);
1501 vport_config = adapter->vport_config[index];
1503 init_waitqueue_head(&vport->sw_marker_wq);
1505 spin_lock_init(&vport_config->mac_filter_list_lock);
1507 INIT_LIST_HEAD(&vport_config->user_config.mac_filter_list);
1509 err = idpf_check_supported_desc_ids(vport);
1511 dev_err(&pdev->dev, "failed to get required descriptor ids\n");
1512 goto cfg_netdev_err;
1515 if (idpf_cfg_netdev(vport))
1516 goto cfg_netdev_err;
1518 err = idpf_send_get_rx_ptype_msg(vport);
1522 /* Once state is put into DOWN, driver is ready for dev_open */
1523 np = netdev_priv(vport->netdev);
1524 np->state = __IDPF_VPORT_DOWN;
1525 if (test_and_clear_bit(IDPF_VPORT_UP_REQUESTED, vport_config->flags))
1526 idpf_vport_open(vport);
1528 /* Spawn and return 'idpf_init_task' work queue until all the
1529 * default vports are created
1531 if (adapter->num_alloc_vports < num_default_vports) {
1532 queue_delayed_work(adapter->init_wq, &adapter->init_task,
1533 msecs_to_jiffies(5 * (adapter->pdev->devfn & 0x07)));
1538 for (index = 0; index < adapter->max_vports; index++) {
1539 if (adapter->netdevs[index] &&
1540 !test_bit(IDPF_VPORT_REG_NETDEV,
1541 adapter->vport_config[index]->flags)) {
1542 register_netdev(adapter->netdevs[index]);
1543 set_bit(IDPF_VPORT_REG_NETDEV,
1544 adapter->vport_config[index]->flags);
1548 /* As all the required vports are created, clear the reset flag
1549 * unconditionally here in case we were in reset and the link was down.
1551 clear_bit(IDPF_HR_RESET_IN_PROG, adapter->flags);
1552 /* Start the statistics task now */
1553 queue_delayed_work(adapter->stats_wq, &adapter->stats_task,
1554 msecs_to_jiffies(10 * (pdev->devfn & 0x07)));
1559 idpf_decfg_netdev(vport);
1561 idpf_vport_rel(vport);
1562 adapter->vports[index] = NULL;
1564 if (default_vport) {
1565 for (index = 0; index < adapter->max_vports; index++) {
1566 if (adapter->vports[index])
1567 idpf_vport_dealloc(adapter->vports[index]);
1570 clear_bit(IDPF_HR_RESET_IN_PROG, adapter->flags);
1574 * idpf_sriov_ena - Enable or change number of VFs
1575 * @adapter: private data struct
1576 * @num_vfs: number of VFs to allocate
1578 static int idpf_sriov_ena(struct idpf_adapter *adapter, int num_vfs)
1580 struct device *dev = &adapter->pdev->dev;
1583 err = idpf_send_set_sriov_vfs_msg(adapter, num_vfs);
1585 dev_err(dev, "Failed to allocate VFs: %d\n", err);
1590 err = pci_enable_sriov(adapter->pdev, num_vfs);
1592 idpf_send_set_sriov_vfs_msg(adapter, 0);
1593 dev_err(dev, "Failed to enable SR-IOV: %d\n", err);
1598 adapter->num_vfs = num_vfs;
1604 * idpf_sriov_configure - Configure the requested VFs
1605 * @pdev: pointer to a pci_dev structure
1606 * @num_vfs: number of vfs to allocate
1608 * Enable or change the number of VFs. Called when the user updates the number
1611 int idpf_sriov_configure(struct pci_dev *pdev, int num_vfs)
1613 struct idpf_adapter *adapter = pci_get_drvdata(pdev);
1615 if (!idpf_is_cap_ena(adapter, IDPF_OTHER_CAPS, VIRTCHNL2_CAP_SRIOV)) {
1616 dev_info(&pdev->dev, "SR-IOV is not supported on this device\n");
1622 return idpf_sriov_ena(adapter, num_vfs);
1624 if (pci_vfs_assigned(pdev)) {
1625 dev_warn(&pdev->dev, "Unable to free VFs because some are assigned to VMs\n");
1630 pci_disable_sriov(adapter->pdev);
1631 idpf_send_set_sriov_vfs_msg(adapter, 0);
1632 adapter->num_vfs = 0;
1638 * idpf_deinit_task - Device deinit routine
1639 * @adapter: Driver specific private structure
1641 * Extended remove logic which will be used for
1642 * hard reset as well
1644 void idpf_deinit_task(struct idpf_adapter *adapter)
1648 /* Wait until the init_task is done else this thread might release
1649 * the resources first and the other thread might end up in a bad state
1651 cancel_delayed_work_sync(&adapter->init_task);
1653 if (!adapter->vports)
1656 cancel_delayed_work_sync(&adapter->stats_task);
1658 for (i = 0; i < adapter->max_vports; i++) {
1659 if (adapter->vports[i])
1660 idpf_vport_dealloc(adapter->vports[i]);
1665 * idpf_check_reset_complete - check that reset is complete
1666 * @hw: pointer to hw struct
1667 * @reset_reg: struct with reset registers
1669 * Returns 0 if device is ready to use, or -EBUSY if it's in reset.
1671 static int idpf_check_reset_complete(struct idpf_hw *hw,
1672 struct idpf_reset_reg *reset_reg)
1674 struct idpf_adapter *adapter = hw->back;
1677 for (i = 0; i < 2000; i++) {
1678 u32 reg_val = readl(reset_reg->rstat);
1680 /* 0xFFFFFFFF might be read if other side hasn't cleared the
1681 * register for us yet and 0xFFFFFFFF is not a valid value for
1682 * the register, so treat that as invalid.
1684 if (reg_val != 0xFFFFFFFF && (reg_val & reset_reg->rstat_m))
1687 usleep_range(5000, 10000);
1690 dev_warn(&adapter->pdev->dev, "Device reset timeout!\n");
1691 /* Clear the reset flag unconditionally here since the reset
1692 * technically isn't in progress anymore from the driver's perspective
1694 clear_bit(IDPF_HR_RESET_IN_PROG, adapter->flags);
1700 * idpf_set_vport_state - Set the vport state to be after the reset
1701 * @adapter: Driver specific private structure
1703 static void idpf_set_vport_state(struct idpf_adapter *adapter)
1707 for (i = 0; i < adapter->max_vports; i++) {
1708 struct idpf_netdev_priv *np;
1710 if (!adapter->netdevs[i])
1713 np = netdev_priv(adapter->netdevs[i]);
1714 if (np->state == __IDPF_VPORT_UP)
1715 set_bit(IDPF_VPORT_UP_REQUESTED,
1716 adapter->vport_config[i]->flags);
1721 * idpf_init_hard_reset - Initiate a hardware reset
1722 * @adapter: Driver specific private structure
1724 * Deallocate the vports and all the resources associated with them and
1725 * reallocate. Also reinitialize the mailbox. Return 0 on success,
1726 * negative on failure.
1728 static int idpf_init_hard_reset(struct idpf_adapter *adapter)
1730 struct idpf_reg_ops *reg_ops = &adapter->dev_ops.reg_ops;
1731 struct device *dev = &adapter->pdev->dev;
1732 struct net_device *netdev;
1736 mutex_lock(&adapter->vport_ctrl_lock);
1738 dev_info(dev, "Device HW Reset initiated\n");
1740 /* Avoid TX hangs on reset */
1741 for (i = 0; i < adapter->max_vports; i++) {
1742 netdev = adapter->netdevs[i];
1746 netif_carrier_off(netdev);
1747 netif_tx_disable(netdev);
1750 /* Prepare for reset */
1751 if (test_and_clear_bit(IDPF_HR_DRV_LOAD, adapter->flags)) {
1752 reg_ops->trigger_reset(adapter, IDPF_HR_DRV_LOAD);
1753 } else if (test_and_clear_bit(IDPF_HR_FUNC_RESET, adapter->flags)) {
1754 bool is_reset = idpf_is_reset_detected(adapter);
1756 idpf_set_vport_state(adapter);
1757 idpf_vc_core_deinit(adapter);
1759 reg_ops->trigger_reset(adapter, IDPF_HR_FUNC_RESET);
1760 idpf_deinit_dflt_mbx(adapter);
1762 dev_err(dev, "Unhandled hard reset cause\n");
1767 /* Wait for reset to complete */
1768 err = idpf_check_reset_complete(&adapter->hw, &adapter->reset_reg);
1770 dev_err(dev, "The driver was unable to contact the device's firmware. Check that the FW is running. Driver state= 0x%x\n",
1775 /* Reset is complete and so start building the driver resources again */
1776 err = idpf_init_dflt_mbx(adapter);
1778 dev_err(dev, "Failed to initialize default mailbox: %d\n", err);
1782 queue_delayed_work(adapter->mbx_wq, &adapter->mbx_task, 0);
1784 /* Initialize the state machine, also allocate memory and request
1787 err = idpf_vc_core_init(adapter);
1789 cancel_delayed_work_sync(&adapter->mbx_task);
1790 idpf_deinit_dflt_mbx(adapter);
1794 /* Wait till all the vports are initialized to release the reset lock,
1795 * else user space callbacks may access uninitialized vports
1797 while (test_bit(IDPF_HR_RESET_IN_PROG, adapter->flags))
1801 mutex_unlock(&adapter->vport_ctrl_lock);
1807 * idpf_vc_event_task - Handle virtchannel event logic
1808 * @work: work queue struct
1810 void idpf_vc_event_task(struct work_struct *work)
1812 struct idpf_adapter *adapter;
1814 adapter = container_of(work, struct idpf_adapter, vc_event_task.work);
1816 if (test_bit(IDPF_REMOVE_IN_PROG, adapter->flags))
1819 if (test_bit(IDPF_HR_FUNC_RESET, adapter->flags) ||
1820 test_bit(IDPF_HR_DRV_LOAD, adapter->flags)) {
1821 set_bit(IDPF_HR_RESET_IN_PROG, adapter->flags);
1822 idpf_init_hard_reset(adapter);
1827 * idpf_initiate_soft_reset - Initiate a software reset
1828 * @vport: virtual port data struct
1829 * @reset_cause: reason for the soft reset
1831 * Soft reset only reallocs vport queue resources. Returns 0 on success,
1832 * negative on failure.
1834 int idpf_initiate_soft_reset(struct idpf_vport *vport,
1835 enum idpf_vport_reset_cause reset_cause)
1837 struct idpf_netdev_priv *np = netdev_priv(vport->netdev);
1838 enum idpf_vport_state current_state = np->state;
1839 struct idpf_adapter *adapter = vport->adapter;
1840 struct idpf_vport *new_vport;
1843 /* If the system is low on memory, we can end up in bad state if we
1844 * free all the memory for queue resources and try to allocate them
1845 * again. Instead, we can pre-allocate the new resources before doing
1846 * anything and bailing if the alloc fails.
1848 * Make a clone of the existing vport to mimic its current
1849 * configuration, then modify the new structure with any requested
1850 * changes. Once the allocation of the new resources is done, stop the
1851 * existing vport and copy the configuration to the main vport. If an
1852 * error occurred, the existing vport will be untouched.
1855 new_vport = kzalloc(sizeof(*vport), GFP_KERNEL);
1859 /* This purposely avoids copying the end of the struct because it
1860 * contains wait_queues and mutexes and other stuff we don't want to
1861 * mess with. Nothing below should use those variables from new_vport
1862 * and should instead always refer to them in vport if they need to.
1864 memcpy(new_vport, vport, offsetof(struct idpf_vport, link_up));
1866 /* Adjust resource parameters prior to reallocating resources */
1867 switch (reset_cause) {
1868 case IDPF_SR_Q_CHANGE:
1869 err = idpf_vport_adjust_qs(new_vport);
1873 case IDPF_SR_Q_DESC_CHANGE:
1874 /* Update queue parameters before allocating resources */
1875 idpf_vport_calc_num_q_desc(new_vport);
1877 case IDPF_SR_MTU_CHANGE:
1878 case IDPF_SR_RSC_CHANGE:
1881 dev_err(&adapter->pdev->dev, "Unhandled soft reset cause\n");
1886 if (current_state <= __IDPF_VPORT_DOWN) {
1887 idpf_send_delete_queues_msg(vport);
1889 set_bit(IDPF_VPORT_DEL_QUEUES, vport->flags);
1890 idpf_vport_stop(vport);
1893 idpf_deinit_rss(vport);
1894 /* We're passing in vport here because we need its wait_queue
1895 * to send a message and it should be getting all the vport
1896 * config data out of the adapter but we need to be careful not
1897 * to add code to add_queues to change the vport config within
1898 * vport itself as it will be wiped with a memcpy later.
1900 err = idpf_send_add_queues_msg(vport, new_vport->num_txq,
1901 new_vport->num_complq,
1903 new_vport->num_bufq);
1907 /* Same comment as above regarding avoiding copying the wait_queues and
1908 * mutexes applies here. We do not want to mess with those if possible.
1910 memcpy(vport, new_vport, offsetof(struct idpf_vport, link_up));
1912 if (reset_cause == IDPF_SR_Q_CHANGE)
1913 idpf_vport_alloc_vec_indexes(vport);
1915 err = idpf_set_real_num_queues(vport);
1919 if (current_state == __IDPF_VPORT_UP)
1920 err = idpf_vport_open(vport);
1927 idpf_send_add_queues_msg(vport, vport->num_txq, vport->num_complq,
1928 vport->num_rxq, vport->num_bufq);
1931 if (current_state == __IDPF_VPORT_UP)
1932 idpf_vport_open(vport);
1941 * idpf_addr_sync - Callback for dev_(mc|uc)_sync to add address
1942 * @netdev: the netdevice
1943 * @addr: address to add
1945 * Called by __dev_(mc|uc)_sync when an address needs to be added. We call
1946 * __dev_(uc|mc)_sync from .set_rx_mode. Kernel takes addr_list_lock spinlock
1947 * meaning we cannot sleep in this context. Due to this, we have to add the
1948 * filter and send the virtchnl message asynchronously without waiting for the
1949 * response from the other side. We won't know whether or not the operation
1950 * actually succeeded until we get the message back. Returns 0 on success,
1951 * negative on failure.
1953 static int idpf_addr_sync(struct net_device *netdev, const u8 *addr)
1955 struct idpf_netdev_priv *np = netdev_priv(netdev);
1957 return idpf_add_mac_filter(np->vport, np, addr, true);
1961 * idpf_addr_unsync - Callback for dev_(mc|uc)_sync to remove address
1962 * @netdev: the netdevice
1963 * @addr: address to add
1965 * Called by __dev_(mc|uc)_sync when an address needs to be added. We call
1966 * __dev_(uc|mc)_sync from .set_rx_mode. Kernel takes addr_list_lock spinlock
1967 * meaning we cannot sleep in this context. Due to this we have to delete the
1968 * filter and send the virtchnl message asynchronously without waiting for the
1969 * return from the other side. We won't know whether or not the operation
1970 * actually succeeded until we get the message back. Returns 0 on success,
1971 * negative on failure.
1973 static int idpf_addr_unsync(struct net_device *netdev, const u8 *addr)
1975 struct idpf_netdev_priv *np = netdev_priv(netdev);
1977 /* Under some circumstances, we might receive a request to delete
1978 * our own device address from our uc list. Because we store the
1979 * device address in the VSI's MAC filter list, we need to ignore
1980 * such requests and not delete our device address from this list.
1982 if (ether_addr_equal(addr, netdev->dev_addr))
1985 idpf_del_mac_filter(np->vport, np, addr, true);
1991 * idpf_set_rx_mode - NDO callback to set the netdev filters
1992 * @netdev: network interface device structure
1994 * Stack takes addr_list_lock spinlock before calling our .set_rx_mode. We
1995 * cannot sleep in this context.
1997 static void idpf_set_rx_mode(struct net_device *netdev)
1999 struct idpf_netdev_priv *np = netdev_priv(netdev);
2000 struct idpf_vport_user_config_data *config_data;
2001 struct idpf_adapter *adapter;
2002 bool changed = false;
2006 adapter = np->adapter;
2007 dev = &adapter->pdev->dev;
2009 if (idpf_is_cap_ena(adapter, IDPF_OTHER_CAPS, VIRTCHNL2_CAP_MACFILTER)) {
2010 __dev_uc_sync(netdev, idpf_addr_sync, idpf_addr_unsync);
2011 __dev_mc_sync(netdev, idpf_addr_sync, idpf_addr_unsync);
2014 if (!idpf_is_cap_ena(adapter, IDPF_OTHER_CAPS, VIRTCHNL2_CAP_PROMISC))
2017 config_data = &adapter->vport_config[np->vport_idx]->user_config;
2018 /* IFF_PROMISC enables both unicast and multicast promiscuous,
2019 * while IFF_ALLMULTI only enables multicast such that:
2021 * promisc + allmulti = unicast | multicast
2022 * promisc + !allmulti = unicast | multicast
2023 * !promisc + allmulti = multicast
2025 if ((netdev->flags & IFF_PROMISC) &&
2026 !test_and_set_bit(__IDPF_PROMISC_UC, config_data->user_flags)) {
2028 dev_info(&adapter->pdev->dev, "Entering promiscuous mode\n");
2029 if (!test_and_set_bit(__IDPF_PROMISC_MC, adapter->flags))
2030 dev_info(dev, "Entering multicast promiscuous mode\n");
2033 if (!(netdev->flags & IFF_PROMISC) &&
2034 test_and_clear_bit(__IDPF_PROMISC_UC, config_data->user_flags)) {
2036 dev_info(dev, "Leaving promiscuous mode\n");
2039 if (netdev->flags & IFF_ALLMULTI &&
2040 !test_and_set_bit(__IDPF_PROMISC_MC, config_data->user_flags)) {
2042 dev_info(dev, "Entering multicast promiscuous mode\n");
2045 if (!(netdev->flags & (IFF_ALLMULTI | IFF_PROMISC)) &&
2046 test_and_clear_bit(__IDPF_PROMISC_MC, config_data->user_flags)) {
2048 dev_info(dev, "Leaving multicast promiscuous mode\n");
2054 err = idpf_set_promiscuous(adapter, config_data, np->vport_id);
2056 dev_err(dev, "Failed to set promiscuous mode: %d\n", err);
2060 * idpf_vport_manage_rss_lut - disable/enable RSS
2061 * @vport: the vport being changed
2063 * In the event of disable request for RSS, this function will zero out RSS
2064 * LUT, while in the event of enable request for RSS, it will reconfigure RSS
2065 * LUT with the default LUT configuration.
2067 static int idpf_vport_manage_rss_lut(struct idpf_vport *vport)
2069 bool ena = idpf_is_feature_ena(vport, NETIF_F_RXHASH);
2070 struct idpf_rss_data *rss_data;
2071 u16 idx = vport->idx;
2074 rss_data = &vport->adapter->vport_config[idx]->user_config.rss_data;
2075 lut_size = rss_data->rss_lut_size * sizeof(u32);
2078 /* This will contain the default or user configured LUT */
2079 memcpy(rss_data->rss_lut, rss_data->cached_lut, lut_size);
2081 /* Save a copy of the current LUT to be restored later if
2084 memcpy(rss_data->cached_lut, rss_data->rss_lut, lut_size);
2086 /* Zero out the current LUT to disable */
2087 memset(rss_data->rss_lut, 0, lut_size);
2090 return idpf_config_rss(vport);
2094 * idpf_set_features - set the netdev feature flags
2095 * @netdev: ptr to the netdev being adjusted
2096 * @features: the feature set that the stack is suggesting
2098 static int idpf_set_features(struct net_device *netdev,
2099 netdev_features_t features)
2101 netdev_features_t changed = netdev->features ^ features;
2102 struct idpf_adapter *adapter;
2103 struct idpf_vport *vport;
2106 idpf_vport_ctrl_lock(netdev);
2107 vport = idpf_netdev_to_vport(netdev);
2109 adapter = vport->adapter;
2111 if (idpf_is_reset_in_prog(adapter)) {
2112 dev_err(&adapter->pdev->dev, "Device is resetting, changing netdev features temporarily unavailable.\n");
2117 if (changed & NETIF_F_RXHASH) {
2118 netdev->features ^= NETIF_F_RXHASH;
2119 err = idpf_vport_manage_rss_lut(vport);
2124 if (changed & NETIF_F_GRO_HW) {
2125 netdev->features ^= NETIF_F_GRO_HW;
2126 err = idpf_initiate_soft_reset(vport, IDPF_SR_RSC_CHANGE);
2131 if (changed & NETIF_F_LOOPBACK) {
2132 netdev->features ^= NETIF_F_LOOPBACK;
2133 err = idpf_send_ena_dis_loopback_msg(vport);
2137 idpf_vport_ctrl_unlock(netdev);
2143 * idpf_open - Called when a network interface becomes active
2144 * @netdev: network interface device structure
2146 * The open entry point is called when a network interface is made
2147 * active by the system (IFF_UP). At this point all resources needed
2148 * for transmit and receive operations are allocated, the interrupt
2149 * handler is registered with the OS, the netdev watchdog is enabled,
2150 * and the stack is notified that the interface is ready.
2152 * Returns 0 on success, negative value on failure
2154 static int idpf_open(struct net_device *netdev)
2156 struct idpf_vport *vport;
2159 idpf_vport_ctrl_lock(netdev);
2160 vport = idpf_netdev_to_vport(netdev);
2162 err = idpf_set_real_num_queues(vport);
2166 err = idpf_vport_open(vport);
2169 idpf_vport_ctrl_unlock(netdev);
2175 * idpf_change_mtu - NDO callback to change the MTU
2176 * @netdev: network interface device structure
2177 * @new_mtu: new value for maximum frame size
2179 * Returns 0 on success, negative on failure
2181 static int idpf_change_mtu(struct net_device *netdev, int new_mtu)
2183 struct idpf_vport *vport;
2186 idpf_vport_ctrl_lock(netdev);
2187 vport = idpf_netdev_to_vport(netdev);
2189 WRITE_ONCE(netdev->mtu, new_mtu);
2191 err = idpf_initiate_soft_reset(vport, IDPF_SR_MTU_CHANGE);
2193 idpf_vport_ctrl_unlock(netdev);
2199 * idpf_features_check - Validate packet conforms to limits
2201 * @netdev: This port's netdev
2202 * @features: Offload features that the stack believes apply
2204 static netdev_features_t idpf_features_check(struct sk_buff *skb,
2205 struct net_device *netdev,
2206 netdev_features_t features)
2208 struct idpf_vport *vport = idpf_netdev_to_vport(netdev);
2209 struct idpf_adapter *adapter = vport->adapter;
2212 /* No point in doing any of this if neither checksum nor GSO are
2213 * being requested for this frame. We can rule out both by just
2214 * checking for CHECKSUM_PARTIAL
2216 if (skb->ip_summed != CHECKSUM_PARTIAL)
2219 /* We cannot support GSO if the MSS is going to be less than
2220 * 88 bytes. If it is then we need to drop support for GSO.
2222 if (skb_is_gso(skb) &&
2223 (skb_shinfo(skb)->gso_size < IDPF_TX_TSO_MIN_MSS))
2224 features &= ~NETIF_F_GSO_MASK;
2226 /* Ensure MACLEN is <= 126 bytes (63 words) and not an odd size */
2227 len = skb_network_offset(skb);
2228 if (unlikely(len & ~(126)))
2231 len = skb_network_header_len(skb);
2232 if (unlikely(len > idpf_get_max_tx_hdr_size(adapter)))
2235 if (!skb->encapsulation)
2238 /* L4TUNLEN can support 127 words */
2239 len = skb_inner_network_header(skb) - skb_transport_header(skb);
2240 if (unlikely(len & ~(127 * 2)))
2243 /* IPLEN can support at most 127 dwords */
2244 len = skb_inner_network_header_len(skb);
2245 if (unlikely(len > idpf_get_max_tx_hdr_size(adapter)))
2248 /* No need to validate L4LEN as TCP is the only protocol with a
2249 * a flexible value and we support all possible values supported
2250 * by TCP, which is at most 15 dwords
2256 return features & ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK);
2260 * idpf_set_mac - NDO callback to set port mac address
2261 * @netdev: network interface device structure
2262 * @p: pointer to an address structure
2264 * Returns 0 on success, negative on failure
2266 static int idpf_set_mac(struct net_device *netdev, void *p)
2268 struct idpf_netdev_priv *np = netdev_priv(netdev);
2269 struct idpf_vport_config *vport_config;
2270 struct sockaddr *addr = p;
2271 struct idpf_vport *vport;
2274 idpf_vport_ctrl_lock(netdev);
2275 vport = idpf_netdev_to_vport(netdev);
2277 if (!idpf_is_cap_ena(vport->adapter, IDPF_OTHER_CAPS,
2278 VIRTCHNL2_CAP_MACFILTER)) {
2279 dev_info(&vport->adapter->pdev->dev, "Setting MAC address is not supported\n");
2284 if (!is_valid_ether_addr(addr->sa_data)) {
2285 dev_info(&vport->adapter->pdev->dev, "Invalid MAC address: %pM\n",
2287 err = -EADDRNOTAVAIL;
2291 if (ether_addr_equal(netdev->dev_addr, addr->sa_data))
2294 vport_config = vport->adapter->vport_config[vport->idx];
2295 err = idpf_add_mac_filter(vport, np, addr->sa_data, false);
2297 __idpf_del_mac_filter(vport_config, addr->sa_data);
2301 if (is_valid_ether_addr(vport->default_mac_addr))
2302 idpf_del_mac_filter(vport, np, vport->default_mac_addr, false);
2304 ether_addr_copy(vport->default_mac_addr, addr->sa_data);
2305 eth_hw_addr_set(netdev, addr->sa_data);
2308 idpf_vport_ctrl_unlock(netdev);
2314 * idpf_alloc_dma_mem - Allocate dma memory
2315 * @hw: pointer to hw struct
2316 * @mem: pointer to dma_mem struct
2317 * @size: size of the memory to allocate
2319 void *idpf_alloc_dma_mem(struct idpf_hw *hw, struct idpf_dma_mem *mem, u64 size)
2321 struct idpf_adapter *adapter = hw->back;
2322 size_t sz = ALIGN(size, 4096);
2324 mem->va = dma_alloc_coherent(&adapter->pdev->dev, sz,
2325 &mem->pa, GFP_KERNEL);
2332 * idpf_free_dma_mem - Free the allocated dma memory
2333 * @hw: pointer to hw struct
2334 * @mem: pointer to dma_mem struct
2336 void idpf_free_dma_mem(struct idpf_hw *hw, struct idpf_dma_mem *mem)
2338 struct idpf_adapter *adapter = hw->back;
2340 dma_free_coherent(&adapter->pdev->dev, mem->size,
2347 static const struct net_device_ops idpf_netdev_ops = {
2348 .ndo_open = idpf_open,
2349 .ndo_stop = idpf_stop,
2350 .ndo_start_xmit = idpf_tx_start,
2351 .ndo_features_check = idpf_features_check,
2352 .ndo_set_rx_mode = idpf_set_rx_mode,
2353 .ndo_validate_addr = eth_validate_addr,
2354 .ndo_set_mac_address = idpf_set_mac,
2355 .ndo_change_mtu = idpf_change_mtu,
2356 .ndo_get_stats64 = idpf_get_stats64,
2357 .ndo_set_features = idpf_set_features,
2358 .ndo_tx_timeout = idpf_tx_timeout,