xref: /linux/drivers/net/ethernet/intel/idpf/idpf_lib.c (revision 07fdad3a93756b872da7b53647715c48d0f4a2d0)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /* Copyright (C) 2023 Intel Corporation */
3 
4 #include "idpf.h"
5 #include "idpf_virtchnl.h"
6 #include "idpf_ptp.h"
7 #include "xdp.h"
8 #include "xsk.h"
9 
10 static const struct net_device_ops idpf_netdev_ops;
11 
12 /**
13  * idpf_init_vector_stack - Fill the MSIX vector stack with vector index
14  * @adapter: private data struct
15  *
16  * Return 0 on success, error on failure
17  */
18 static int idpf_init_vector_stack(struct idpf_adapter *adapter)
19 {
20 	struct idpf_vector_lifo *stack;
21 	u16 min_vec;
22 	u32 i;
23 
24 	mutex_lock(&adapter->vector_lock);
25 	min_vec = adapter->num_msix_entries - adapter->num_avail_msix;
26 	stack = &adapter->vector_stack;
27 	stack->size = adapter->num_msix_entries;
28 	/* set the base and top to point at start of the 'free pool' to
29 	 * distribute the unused vectors on-demand basis
30 	 */
31 	stack->base = min_vec;
32 	stack->top = min_vec;
33 
34 	stack->vec_idx = kcalloc(stack->size, sizeof(u16), GFP_KERNEL);
35 	if (!stack->vec_idx) {
36 		mutex_unlock(&adapter->vector_lock);
37 
38 		return -ENOMEM;
39 	}
40 
41 	for (i = 0; i < stack->size; i++)
42 		stack->vec_idx[i] = i;
43 
44 	mutex_unlock(&adapter->vector_lock);
45 
46 	return 0;
47 }
48 
49 /**
50  * idpf_deinit_vector_stack - zero out the MSIX vector stack
51  * @adapter: private data struct
52  */
53 static void idpf_deinit_vector_stack(struct idpf_adapter *adapter)
54 {
55 	struct idpf_vector_lifo *stack;
56 
57 	mutex_lock(&adapter->vector_lock);
58 	stack = &adapter->vector_stack;
59 	kfree(stack->vec_idx);
60 	stack->vec_idx = NULL;
61 	mutex_unlock(&adapter->vector_lock);
62 }
63 
64 /**
65  * idpf_mb_intr_rel_irq - Free the IRQ association with the OS
66  * @adapter: adapter structure
67  *
68  * This will also disable interrupt mode and queue up mailbox task. Mailbox
69  * task will reschedule itself if not in interrupt mode.
70  */
71 static void idpf_mb_intr_rel_irq(struct idpf_adapter *adapter)
72 {
73 	clear_bit(IDPF_MB_INTR_MODE, adapter->flags);
74 	kfree(free_irq(adapter->msix_entries[0].vector, adapter));
75 	queue_delayed_work(adapter->mbx_wq, &adapter->mbx_task, 0);
76 }
77 
78 /**
79  * idpf_intr_rel - Release interrupt capabilities and free memory
80  * @adapter: adapter to disable interrupts on
81  */
82 void idpf_intr_rel(struct idpf_adapter *adapter)
83 {
84 	if (!adapter->msix_entries)
85 		return;
86 
87 	idpf_mb_intr_rel_irq(adapter);
88 	pci_free_irq_vectors(adapter->pdev);
89 	idpf_send_dealloc_vectors_msg(adapter);
90 	idpf_deinit_vector_stack(adapter);
91 	kfree(adapter->msix_entries);
92 	adapter->msix_entries = NULL;
93 	kfree(adapter->rdma_msix_entries);
94 	adapter->rdma_msix_entries = NULL;
95 }
96 
97 /**
98  * idpf_mb_intr_clean - Interrupt handler for the mailbox
99  * @irq: interrupt number
100  * @data: pointer to the adapter structure
101  */
102 static irqreturn_t idpf_mb_intr_clean(int __always_unused irq, void *data)
103 {
104 	struct idpf_adapter *adapter = (struct idpf_adapter *)data;
105 
106 	queue_delayed_work(adapter->mbx_wq, &adapter->mbx_task, 0);
107 
108 	return IRQ_HANDLED;
109 }
110 
111 /**
112  * idpf_mb_irq_enable - Enable MSIX interrupt for the mailbox
113  * @adapter: adapter to get the hardware address for register write
114  */
115 static void idpf_mb_irq_enable(struct idpf_adapter *adapter)
116 {
117 	struct idpf_intr_reg *intr = &adapter->mb_vector.intr_reg;
118 	u32 val;
119 
120 	val = intr->dyn_ctl_intena_m | intr->dyn_ctl_itridx_m;
121 	writel(val, intr->dyn_ctl);
122 	writel(intr->icr_ena_ctlq_m, intr->icr_ena);
123 }
124 
125 /**
126  * idpf_mb_intr_req_irq - Request irq for the mailbox interrupt
127  * @adapter: adapter structure to pass to the mailbox irq handler
128  */
129 static int idpf_mb_intr_req_irq(struct idpf_adapter *adapter)
130 {
131 	int irq_num, mb_vidx = 0, err;
132 	char *name;
133 
134 	irq_num = adapter->msix_entries[mb_vidx].vector;
135 	name = kasprintf(GFP_KERNEL, "%s-%s-%d",
136 			 dev_driver_string(&adapter->pdev->dev),
137 			 "Mailbox", mb_vidx);
138 	err = request_irq(irq_num, adapter->irq_mb_handler, 0, name, adapter);
139 	if (err) {
140 		dev_err(&adapter->pdev->dev,
141 			"IRQ request for mailbox failed, error: %d\n", err);
142 
143 		return err;
144 	}
145 
146 	set_bit(IDPF_MB_INTR_MODE, adapter->flags);
147 
148 	return 0;
149 }
150 
151 /**
152  * idpf_mb_intr_init - Initialize the mailbox interrupt
153  * @adapter: adapter structure to store the mailbox vector
154  */
155 static int idpf_mb_intr_init(struct idpf_adapter *adapter)
156 {
157 	adapter->dev_ops.reg_ops.mb_intr_reg_init(adapter);
158 	adapter->irq_mb_handler = idpf_mb_intr_clean;
159 
160 	return idpf_mb_intr_req_irq(adapter);
161 }
162 
163 /**
164  * idpf_vector_lifo_push - push MSIX vector index onto stack
165  * @adapter: private data struct
166  * @vec_idx: vector index to store
167  */
168 static int idpf_vector_lifo_push(struct idpf_adapter *adapter, u16 vec_idx)
169 {
170 	struct idpf_vector_lifo *stack = &adapter->vector_stack;
171 
172 	lockdep_assert_held(&adapter->vector_lock);
173 
174 	if (stack->top == stack->base) {
175 		dev_err(&adapter->pdev->dev, "Exceeded the vector stack limit: %d\n",
176 			stack->top);
177 		return -EINVAL;
178 	}
179 
180 	stack->vec_idx[--stack->top] = vec_idx;
181 
182 	return 0;
183 }
184 
185 /**
186  * idpf_vector_lifo_pop - pop MSIX vector index from stack
187  * @adapter: private data struct
188  */
189 static int idpf_vector_lifo_pop(struct idpf_adapter *adapter)
190 {
191 	struct idpf_vector_lifo *stack = &adapter->vector_stack;
192 
193 	lockdep_assert_held(&adapter->vector_lock);
194 
195 	if (stack->top == stack->size) {
196 		dev_err(&adapter->pdev->dev, "No interrupt vectors are available to distribute!\n");
197 
198 		return -EINVAL;
199 	}
200 
201 	return stack->vec_idx[stack->top++];
202 }
203 
204 /**
205  * idpf_vector_stash - Store the vector indexes onto the stack
206  * @adapter: private data struct
207  * @q_vector_idxs: vector index array
208  * @vec_info: info related to the number of vectors
209  *
210  * This function is a no-op if there are no vectors indexes to be stashed
211  */
212 static void idpf_vector_stash(struct idpf_adapter *adapter, u16 *q_vector_idxs,
213 			      struct idpf_vector_info *vec_info)
214 {
215 	int i, base = 0;
216 	u16 vec_idx;
217 
218 	lockdep_assert_held(&adapter->vector_lock);
219 
220 	if (!vec_info->num_curr_vecs)
221 		return;
222 
223 	/* For default vports, no need to stash vector allocated from the
224 	 * default pool onto the stack
225 	 */
226 	if (vec_info->default_vport)
227 		base = IDPF_MIN_Q_VEC;
228 
229 	for (i = vec_info->num_curr_vecs - 1; i >= base ; i--) {
230 		vec_idx = q_vector_idxs[i];
231 		idpf_vector_lifo_push(adapter, vec_idx);
232 		adapter->num_avail_msix++;
233 	}
234 }
235 
236 /**
237  * idpf_req_rel_vector_indexes - Request or release MSIX vector indexes
238  * @adapter: driver specific private structure
239  * @q_vector_idxs: vector index array
240  * @vec_info: info related to the number of vectors
241  *
242  * This is the core function to distribute the MSIX vectors acquired from the
243  * OS. It expects the caller to pass the number of vectors required and
244  * also previously allocated. First, it stashes previously allocated vector
245  * indexes on to the stack and then figures out if it can allocate requested
246  * vectors. It can wait on acquiring the mutex lock. If the caller passes 0 as
247  * requested vectors, then this function just stashes the already allocated
248  * vectors and returns 0.
249  *
250  * Returns actual number of vectors allocated on success, error value on failure
251  * If 0 is returned, implies the stack has no vectors to allocate which is also
252  * a failure case for the caller
253  */
254 int idpf_req_rel_vector_indexes(struct idpf_adapter *adapter,
255 				u16 *q_vector_idxs,
256 				struct idpf_vector_info *vec_info)
257 {
258 	u16 num_req_vecs, num_alloc_vecs = 0, max_vecs;
259 	struct idpf_vector_lifo *stack;
260 	int i, j, vecid;
261 
262 	mutex_lock(&adapter->vector_lock);
263 	stack = &adapter->vector_stack;
264 	num_req_vecs = vec_info->num_req_vecs;
265 
266 	/* Stash interrupt vector indexes onto the stack if required */
267 	idpf_vector_stash(adapter, q_vector_idxs, vec_info);
268 
269 	if (!num_req_vecs)
270 		goto rel_lock;
271 
272 	if (vec_info->default_vport) {
273 		/* As IDPF_MIN_Q_VEC per default vport is put aside in the
274 		 * default pool of the stack, use them for default vports
275 		 */
276 		j = vec_info->index * IDPF_MIN_Q_VEC + IDPF_MBX_Q_VEC;
277 		for (i = 0; i < IDPF_MIN_Q_VEC; i++) {
278 			q_vector_idxs[num_alloc_vecs++] = stack->vec_idx[j++];
279 			num_req_vecs--;
280 		}
281 	}
282 
283 	/* Find if stack has enough vector to allocate */
284 	max_vecs = min(adapter->num_avail_msix, num_req_vecs);
285 
286 	for (j = 0; j < max_vecs; j++) {
287 		vecid = idpf_vector_lifo_pop(adapter);
288 		q_vector_idxs[num_alloc_vecs++] = vecid;
289 	}
290 	adapter->num_avail_msix -= max_vecs;
291 
292 rel_lock:
293 	mutex_unlock(&adapter->vector_lock);
294 
295 	return num_alloc_vecs;
296 }
297 
298 /**
299  * idpf_intr_req - Request interrupt capabilities
300  * @adapter: adapter to enable interrupts on
301  *
302  * Returns 0 on success, negative on failure
303  */
304 int idpf_intr_req(struct idpf_adapter *adapter)
305 {
306 	u16 num_lan_vecs, min_lan_vecs, num_rdma_vecs = 0, min_rdma_vecs = 0;
307 	u16 default_vports = idpf_get_default_vports(adapter);
308 	int num_q_vecs, total_vecs, num_vec_ids;
309 	int min_vectors, actual_vecs, err;
310 	unsigned int vector;
311 	u16 *vecids;
312 	int i;
313 
314 	total_vecs = idpf_get_reserved_vecs(adapter);
315 	num_lan_vecs = total_vecs;
316 	if (idpf_is_rdma_cap_ena(adapter)) {
317 		num_rdma_vecs = idpf_get_reserved_rdma_vecs(adapter);
318 		min_rdma_vecs = IDPF_MIN_RDMA_VEC;
319 
320 		if (!num_rdma_vecs) {
321 			/* If idpf_get_reserved_rdma_vecs is 0, vectors are
322 			 * pulled from the LAN pool.
323 			 */
324 			num_rdma_vecs = min_rdma_vecs;
325 		} else if (num_rdma_vecs < min_rdma_vecs) {
326 			dev_err(&adapter->pdev->dev,
327 				"Not enough vectors reserved for RDMA (min: %u, current: %u)\n",
328 				min_rdma_vecs, num_rdma_vecs);
329 			return -EINVAL;
330 		}
331 	}
332 
333 	num_q_vecs = total_vecs - IDPF_MBX_Q_VEC;
334 
335 	err = idpf_send_alloc_vectors_msg(adapter, num_q_vecs);
336 	if (err) {
337 		dev_err(&adapter->pdev->dev,
338 			"Failed to allocate %d vectors: %d\n", num_q_vecs, err);
339 
340 		return -EAGAIN;
341 	}
342 
343 	min_lan_vecs = IDPF_MBX_Q_VEC + IDPF_MIN_Q_VEC * default_vports;
344 	min_vectors = min_lan_vecs + min_rdma_vecs;
345 	actual_vecs = pci_alloc_irq_vectors(adapter->pdev, min_vectors,
346 					    total_vecs, PCI_IRQ_MSIX);
347 	if (actual_vecs < 0) {
348 		dev_err(&adapter->pdev->dev, "Failed to allocate minimum MSIX vectors required: %d\n",
349 			min_vectors);
350 		err = actual_vecs;
351 		goto send_dealloc_vecs;
352 	}
353 
354 	if (idpf_is_rdma_cap_ena(adapter)) {
355 		if (actual_vecs < total_vecs) {
356 			dev_warn(&adapter->pdev->dev,
357 				 "Warning: %d vectors requested, only %d available. Defaulting to minimum (%d) for RDMA and remaining for LAN.\n",
358 				 total_vecs, actual_vecs, IDPF_MIN_RDMA_VEC);
359 			num_rdma_vecs = IDPF_MIN_RDMA_VEC;
360 		}
361 
362 		adapter->rdma_msix_entries = kcalloc(num_rdma_vecs,
363 						     sizeof(struct msix_entry),
364 						     GFP_KERNEL);
365 		if (!adapter->rdma_msix_entries) {
366 			err = -ENOMEM;
367 			goto free_irq;
368 		}
369 	}
370 
371 	num_lan_vecs = actual_vecs - num_rdma_vecs;
372 	adapter->msix_entries = kcalloc(num_lan_vecs, sizeof(struct msix_entry),
373 					GFP_KERNEL);
374 	if (!adapter->msix_entries) {
375 		err = -ENOMEM;
376 		goto free_rdma_msix;
377 	}
378 
379 	adapter->mb_vector.v_idx = le16_to_cpu(adapter->caps.mailbox_vector_id);
380 
381 	vecids = kcalloc(actual_vecs, sizeof(u16), GFP_KERNEL);
382 	if (!vecids) {
383 		err = -ENOMEM;
384 		goto free_msix;
385 	}
386 
387 	num_vec_ids = idpf_get_vec_ids(adapter, vecids, actual_vecs,
388 				       &adapter->req_vec_chunks->vchunks);
389 	if (num_vec_ids < actual_vecs) {
390 		err = -EINVAL;
391 		goto free_vecids;
392 	}
393 
394 	for (vector = 0; vector < num_lan_vecs; vector++) {
395 		adapter->msix_entries[vector].entry = vecids[vector];
396 		adapter->msix_entries[vector].vector =
397 			pci_irq_vector(adapter->pdev, vector);
398 	}
399 	for (i = 0; i < num_rdma_vecs; vector++, i++) {
400 		adapter->rdma_msix_entries[i].entry = vecids[vector];
401 		adapter->rdma_msix_entries[i].vector =
402 			pci_irq_vector(adapter->pdev, vector);
403 	}
404 
405 	/* 'num_avail_msix' is used to distribute excess vectors to the vports
406 	 * after considering the minimum vectors required per each default
407 	 * vport
408 	 */
409 	adapter->num_avail_msix = num_lan_vecs - min_lan_vecs;
410 	adapter->num_msix_entries = num_lan_vecs;
411 	if (idpf_is_rdma_cap_ena(adapter))
412 		adapter->num_rdma_msix_entries = num_rdma_vecs;
413 
414 	/* Fill MSIX vector lifo stack with vector indexes */
415 	err = idpf_init_vector_stack(adapter);
416 	if (err)
417 		goto free_vecids;
418 
419 	err = idpf_mb_intr_init(adapter);
420 	if (err)
421 		goto deinit_vec_stack;
422 	idpf_mb_irq_enable(adapter);
423 	kfree(vecids);
424 
425 	return 0;
426 
427 deinit_vec_stack:
428 	idpf_deinit_vector_stack(adapter);
429 free_vecids:
430 	kfree(vecids);
431 free_msix:
432 	kfree(adapter->msix_entries);
433 	adapter->msix_entries = NULL;
434 free_rdma_msix:
435 	kfree(adapter->rdma_msix_entries);
436 	adapter->rdma_msix_entries = NULL;
437 free_irq:
438 	pci_free_irq_vectors(adapter->pdev);
439 send_dealloc_vecs:
440 	idpf_send_dealloc_vectors_msg(adapter);
441 
442 	return err;
443 }
444 
445 /**
446  * idpf_find_mac_filter - Search filter list for specific mac filter
447  * @vconfig: Vport config structure
448  * @macaddr: The MAC address
449  *
450  * Returns ptr to the filter object or NULL. Must be called while holding the
451  * mac_filter_list_lock.
452  **/
453 static struct idpf_mac_filter *idpf_find_mac_filter(struct idpf_vport_config *vconfig,
454 						    const u8 *macaddr)
455 {
456 	struct idpf_mac_filter *f;
457 
458 	if (!macaddr)
459 		return NULL;
460 
461 	list_for_each_entry(f, &vconfig->user_config.mac_filter_list, list) {
462 		if (ether_addr_equal(macaddr, f->macaddr))
463 			return f;
464 	}
465 
466 	return NULL;
467 }
468 
469 /**
470  * __idpf_del_mac_filter - Delete a MAC filter from the filter list
471  * @vport_config: Vport config structure
472  * @macaddr: The MAC address
473  *
474  * Returns 0 on success, error value on failure
475  **/
476 static int __idpf_del_mac_filter(struct idpf_vport_config *vport_config,
477 				 const u8 *macaddr)
478 {
479 	struct idpf_mac_filter *f;
480 
481 	spin_lock_bh(&vport_config->mac_filter_list_lock);
482 	f = idpf_find_mac_filter(vport_config, macaddr);
483 	if (f) {
484 		list_del(&f->list);
485 		kfree(f);
486 	}
487 	spin_unlock_bh(&vport_config->mac_filter_list_lock);
488 
489 	return 0;
490 }
491 
492 /**
493  * idpf_del_mac_filter - Delete a MAC filter from the filter list
494  * @vport: Main vport structure
495  * @np: Netdev private structure
496  * @macaddr: The MAC address
497  * @async: Don't wait for return message
498  *
499  * Removes filter from list and if interface is up, tells hardware about the
500  * removed filter.
501  **/
502 static int idpf_del_mac_filter(struct idpf_vport *vport,
503 			       struct idpf_netdev_priv *np,
504 			       const u8 *macaddr, bool async)
505 {
506 	struct idpf_vport_config *vport_config;
507 	struct idpf_mac_filter *f;
508 
509 	vport_config = np->adapter->vport_config[np->vport_idx];
510 
511 	spin_lock_bh(&vport_config->mac_filter_list_lock);
512 	f = idpf_find_mac_filter(vport_config, macaddr);
513 	if (f) {
514 		f->remove = true;
515 	} else {
516 		spin_unlock_bh(&vport_config->mac_filter_list_lock);
517 
518 		return -EINVAL;
519 	}
520 	spin_unlock_bh(&vport_config->mac_filter_list_lock);
521 
522 	if (np->state == __IDPF_VPORT_UP) {
523 		int err;
524 
525 		err = idpf_add_del_mac_filters(vport, np, false, async);
526 		if (err)
527 			return err;
528 	}
529 
530 	return  __idpf_del_mac_filter(vport_config, macaddr);
531 }
532 
533 /**
534  * __idpf_add_mac_filter - Add mac filter helper function
535  * @vport_config: Vport config structure
536  * @macaddr: Address to add
537  *
538  * Takes mac_filter_list_lock spinlock to add new filter to list.
539  */
540 static int __idpf_add_mac_filter(struct idpf_vport_config *vport_config,
541 				 const u8 *macaddr)
542 {
543 	struct idpf_mac_filter *f;
544 
545 	spin_lock_bh(&vport_config->mac_filter_list_lock);
546 
547 	f = idpf_find_mac_filter(vport_config, macaddr);
548 	if (f) {
549 		f->remove = false;
550 		spin_unlock_bh(&vport_config->mac_filter_list_lock);
551 
552 		return 0;
553 	}
554 
555 	f = kzalloc(sizeof(*f), GFP_ATOMIC);
556 	if (!f) {
557 		spin_unlock_bh(&vport_config->mac_filter_list_lock);
558 
559 		return -ENOMEM;
560 	}
561 
562 	ether_addr_copy(f->macaddr, macaddr);
563 	list_add_tail(&f->list, &vport_config->user_config.mac_filter_list);
564 	f->add = true;
565 
566 	spin_unlock_bh(&vport_config->mac_filter_list_lock);
567 
568 	return 0;
569 }
570 
571 /**
572  * idpf_add_mac_filter - Add a mac filter to the filter list
573  * @vport: Main vport structure
574  * @np: Netdev private structure
575  * @macaddr: The MAC address
576  * @async: Don't wait for return message
577  *
578  * Returns 0 on success or error on failure. If interface is up, we'll also
579  * send the virtchnl message to tell hardware about the filter.
580  **/
581 static int idpf_add_mac_filter(struct idpf_vport *vport,
582 			       struct idpf_netdev_priv *np,
583 			       const u8 *macaddr, bool async)
584 {
585 	struct idpf_vport_config *vport_config;
586 	int err;
587 
588 	vport_config = np->adapter->vport_config[np->vport_idx];
589 	err = __idpf_add_mac_filter(vport_config, macaddr);
590 	if (err)
591 		return err;
592 
593 	if (np->state == __IDPF_VPORT_UP)
594 		err = idpf_add_del_mac_filters(vport, np, true, async);
595 
596 	return err;
597 }
598 
599 /**
600  * idpf_del_all_mac_filters - Delete all MAC filters in list
601  * @vport: main vport struct
602  *
603  * Takes mac_filter_list_lock spinlock.  Deletes all filters
604  */
605 static void idpf_del_all_mac_filters(struct idpf_vport *vport)
606 {
607 	struct idpf_vport_config *vport_config;
608 	struct idpf_mac_filter *f, *ftmp;
609 
610 	vport_config = vport->adapter->vport_config[vport->idx];
611 	spin_lock_bh(&vport_config->mac_filter_list_lock);
612 
613 	list_for_each_entry_safe(f, ftmp, &vport_config->user_config.mac_filter_list,
614 				 list) {
615 		list_del(&f->list);
616 		kfree(f);
617 	}
618 
619 	spin_unlock_bh(&vport_config->mac_filter_list_lock);
620 }
621 
622 /**
623  * idpf_restore_mac_filters - Re-add all MAC filters in list
624  * @vport: main vport struct
625  *
626  * Takes mac_filter_list_lock spinlock.  Sets add field to true for filters to
627  * resync filters back to HW.
628  */
629 static void idpf_restore_mac_filters(struct idpf_vport *vport)
630 {
631 	struct idpf_vport_config *vport_config;
632 	struct idpf_mac_filter *f;
633 
634 	vport_config = vport->adapter->vport_config[vport->idx];
635 	spin_lock_bh(&vport_config->mac_filter_list_lock);
636 
637 	list_for_each_entry(f, &vport_config->user_config.mac_filter_list, list)
638 		f->add = true;
639 
640 	spin_unlock_bh(&vport_config->mac_filter_list_lock);
641 
642 	idpf_add_del_mac_filters(vport, netdev_priv(vport->netdev),
643 				 true, false);
644 }
645 
646 /**
647  * idpf_remove_mac_filters - Remove all MAC filters in list
648  * @vport: main vport struct
649  *
650  * Takes mac_filter_list_lock spinlock. Sets remove field to true for filters
651  * to remove filters in HW.
652  */
653 static void idpf_remove_mac_filters(struct idpf_vport *vport)
654 {
655 	struct idpf_vport_config *vport_config;
656 	struct idpf_mac_filter *f;
657 
658 	vport_config = vport->adapter->vport_config[vport->idx];
659 	spin_lock_bh(&vport_config->mac_filter_list_lock);
660 
661 	list_for_each_entry(f, &vport_config->user_config.mac_filter_list, list)
662 		f->remove = true;
663 
664 	spin_unlock_bh(&vport_config->mac_filter_list_lock);
665 
666 	idpf_add_del_mac_filters(vport, netdev_priv(vport->netdev),
667 				 false, false);
668 }
669 
670 /**
671  * idpf_deinit_mac_addr - deinitialize mac address for vport
672  * @vport: main vport structure
673  */
674 static void idpf_deinit_mac_addr(struct idpf_vport *vport)
675 {
676 	struct idpf_vport_config *vport_config;
677 	struct idpf_mac_filter *f;
678 
679 	vport_config = vport->adapter->vport_config[vport->idx];
680 
681 	spin_lock_bh(&vport_config->mac_filter_list_lock);
682 
683 	f = idpf_find_mac_filter(vport_config, vport->default_mac_addr);
684 	if (f) {
685 		list_del(&f->list);
686 		kfree(f);
687 	}
688 
689 	spin_unlock_bh(&vport_config->mac_filter_list_lock);
690 }
691 
692 /**
693  * idpf_init_mac_addr - initialize mac address for vport
694  * @vport: main vport structure
695  * @netdev: pointer to netdev struct associated with this vport
696  */
697 static int idpf_init_mac_addr(struct idpf_vport *vport,
698 			      struct net_device *netdev)
699 {
700 	struct idpf_netdev_priv *np = netdev_priv(netdev);
701 	struct idpf_adapter *adapter = vport->adapter;
702 	int err;
703 
704 	if (is_valid_ether_addr(vport->default_mac_addr)) {
705 		eth_hw_addr_set(netdev, vport->default_mac_addr);
706 		ether_addr_copy(netdev->perm_addr, vport->default_mac_addr);
707 
708 		return idpf_add_mac_filter(vport, np, vport->default_mac_addr,
709 					   false);
710 	}
711 
712 	if (!idpf_is_cap_ena(adapter, IDPF_OTHER_CAPS,
713 			     VIRTCHNL2_CAP_MACFILTER)) {
714 		dev_err(&adapter->pdev->dev,
715 			"MAC address is not provided and capability is not set\n");
716 
717 		return -EINVAL;
718 	}
719 
720 	eth_hw_addr_random(netdev);
721 	err = idpf_add_mac_filter(vport, np, netdev->dev_addr, false);
722 	if (err)
723 		return err;
724 
725 	dev_info(&adapter->pdev->dev, "Invalid MAC address %pM, using random %pM\n",
726 		 vport->default_mac_addr, netdev->dev_addr);
727 	ether_addr_copy(vport->default_mac_addr, netdev->dev_addr);
728 
729 	return 0;
730 }
731 
732 /**
733  * idpf_cfg_netdev - Allocate, configure and register a netdev
734  * @vport: main vport structure
735  *
736  * Returns 0 on success, negative value on failure.
737  */
738 static int idpf_cfg_netdev(struct idpf_vport *vport)
739 {
740 	struct idpf_adapter *adapter = vport->adapter;
741 	struct idpf_vport_config *vport_config;
742 	netdev_features_t other_offloads = 0;
743 	netdev_features_t csum_offloads = 0;
744 	netdev_features_t tso_offloads = 0;
745 	netdev_features_t dflt_features;
746 	struct idpf_netdev_priv *np;
747 	struct net_device *netdev;
748 	u16 idx = vport->idx;
749 	int err;
750 
751 	vport_config = adapter->vport_config[idx];
752 
753 	/* It's possible we already have a netdev allocated and registered for
754 	 * this vport
755 	 */
756 	if (test_bit(IDPF_VPORT_REG_NETDEV, vport_config->flags)) {
757 		netdev = adapter->netdevs[idx];
758 		np = netdev_priv(netdev);
759 		np->vport = vport;
760 		np->vport_idx = vport->idx;
761 		np->vport_id = vport->vport_id;
762 		np->max_tx_hdr_size = idpf_get_max_tx_hdr_size(adapter);
763 		vport->netdev = netdev;
764 
765 		return idpf_init_mac_addr(vport, netdev);
766 	}
767 
768 	netdev = alloc_etherdev_mqs(sizeof(struct idpf_netdev_priv),
769 				    vport_config->max_q.max_txq,
770 				    vport_config->max_q.max_rxq);
771 	if (!netdev)
772 		return -ENOMEM;
773 
774 	vport->netdev = netdev;
775 	np = netdev_priv(netdev);
776 	np->vport = vport;
777 	np->adapter = adapter;
778 	np->vport_idx = vport->idx;
779 	np->vport_id = vport->vport_id;
780 	np->max_tx_hdr_size = idpf_get_max_tx_hdr_size(adapter);
781 	np->tx_max_bufs = idpf_get_max_tx_bufs(adapter);
782 
783 	spin_lock_init(&np->stats_lock);
784 
785 	err = idpf_init_mac_addr(vport, netdev);
786 	if (err) {
787 		free_netdev(vport->netdev);
788 		vport->netdev = NULL;
789 
790 		return err;
791 	}
792 
793 	/* assign netdev_ops */
794 	netdev->netdev_ops = &idpf_netdev_ops;
795 
796 	/* setup watchdog timeout value to be 5 second */
797 	netdev->watchdog_timeo = 5 * HZ;
798 
799 	netdev->dev_port = idx;
800 
801 	/* configure default MTU size */
802 	netdev->min_mtu = ETH_MIN_MTU;
803 	netdev->max_mtu = vport->max_mtu;
804 
805 	dflt_features = NETIF_F_SG	|
806 			NETIF_F_HIGHDMA;
807 
808 	if (idpf_is_cap_ena_all(adapter, IDPF_RSS_CAPS, IDPF_CAP_RSS))
809 		dflt_features |= NETIF_F_RXHASH;
810 	if (idpf_is_cap_ena(adapter, IDPF_OTHER_CAPS,
811 			    VIRTCHNL2_CAP_FLOW_STEER) &&
812 	    idpf_vport_is_cap_ena(vport, VIRTCHNL2_VPORT_SIDEBAND_FLOW_STEER))
813 		dflt_features |= NETIF_F_NTUPLE;
814 	if (idpf_is_cap_ena_all(adapter, IDPF_CSUM_CAPS, IDPF_CAP_TX_CSUM_L4V4))
815 		csum_offloads |= NETIF_F_IP_CSUM;
816 	if (idpf_is_cap_ena_all(adapter, IDPF_CSUM_CAPS, IDPF_CAP_TX_CSUM_L4V6))
817 		csum_offloads |= NETIF_F_IPV6_CSUM;
818 	if (idpf_is_cap_ena(adapter, IDPF_CSUM_CAPS, IDPF_CAP_RX_CSUM))
819 		csum_offloads |= NETIF_F_RXCSUM;
820 	if (idpf_is_cap_ena_all(adapter, IDPF_CSUM_CAPS, IDPF_CAP_TX_SCTP_CSUM))
821 		csum_offloads |= NETIF_F_SCTP_CRC;
822 
823 	if (idpf_is_cap_ena(adapter, IDPF_SEG_CAPS, VIRTCHNL2_CAP_SEG_IPV4_TCP))
824 		tso_offloads |= NETIF_F_TSO;
825 	if (idpf_is_cap_ena(adapter, IDPF_SEG_CAPS, VIRTCHNL2_CAP_SEG_IPV6_TCP))
826 		tso_offloads |= NETIF_F_TSO6;
827 	if (idpf_is_cap_ena_all(adapter, IDPF_SEG_CAPS,
828 				VIRTCHNL2_CAP_SEG_IPV4_UDP |
829 				VIRTCHNL2_CAP_SEG_IPV6_UDP))
830 		tso_offloads |= NETIF_F_GSO_UDP_L4;
831 	if (idpf_is_cap_ena_all(adapter, IDPF_RSC_CAPS, IDPF_CAP_RSC))
832 		other_offloads |= NETIF_F_GRO_HW;
833 	if (idpf_is_cap_ena(adapter, IDPF_OTHER_CAPS, VIRTCHNL2_CAP_LOOPBACK))
834 		other_offloads |= NETIF_F_LOOPBACK;
835 
836 	netdev->features |= dflt_features | csum_offloads | tso_offloads;
837 	netdev->hw_features |=  netdev->features | other_offloads;
838 	netdev->vlan_features |= netdev->features | other_offloads;
839 	netdev->hw_enc_features |= dflt_features | other_offloads;
840 	idpf_xdp_set_features(vport);
841 
842 	idpf_set_ethtool_ops(netdev);
843 	netif_set_affinity_auto(netdev);
844 	SET_NETDEV_DEV(netdev, &adapter->pdev->dev);
845 
846 	/* carrier off on init to avoid Tx hangs */
847 	netif_carrier_off(netdev);
848 
849 	/* make sure transmit queues start off as stopped */
850 	netif_tx_stop_all_queues(netdev);
851 
852 	/* The vport can be arbitrarily released so we need to also track
853 	 * netdevs in the adapter struct
854 	 */
855 	adapter->netdevs[idx] = netdev;
856 
857 	return 0;
858 }
859 
860 /**
861  * idpf_get_free_slot - get the next non-NULL location index in array
862  * @adapter: adapter in which to look for a free vport slot
863  */
864 static int idpf_get_free_slot(struct idpf_adapter *adapter)
865 {
866 	unsigned int i;
867 
868 	for (i = 0; i < adapter->max_vports; i++) {
869 		if (!adapter->vports[i])
870 			return i;
871 	}
872 
873 	return IDPF_NO_FREE_SLOT;
874 }
875 
876 /**
877  * idpf_remove_features - Turn off feature configs
878  * @vport: virtual port structure
879  */
880 static void idpf_remove_features(struct idpf_vport *vport)
881 {
882 	struct idpf_adapter *adapter = vport->adapter;
883 
884 	if (idpf_is_cap_ena(adapter, IDPF_OTHER_CAPS, VIRTCHNL2_CAP_MACFILTER))
885 		idpf_remove_mac_filters(vport);
886 }
887 
888 /**
889  * idpf_vport_stop - Disable a vport
890  * @vport: vport to disable
891  * @rtnl: whether to take RTNL lock
892  */
893 static void idpf_vport_stop(struct idpf_vport *vport, bool rtnl)
894 {
895 	struct idpf_netdev_priv *np = netdev_priv(vport->netdev);
896 
897 	if (np->state <= __IDPF_VPORT_DOWN)
898 		return;
899 
900 	if (rtnl)
901 		rtnl_lock();
902 
903 	netif_carrier_off(vport->netdev);
904 	netif_tx_disable(vport->netdev);
905 
906 	idpf_send_disable_vport_msg(vport);
907 	idpf_send_disable_queues_msg(vport);
908 	idpf_send_map_unmap_queue_vector_msg(vport, false);
909 	/* Normally we ask for queues in create_vport, but if the number of
910 	 * initially requested queues have changed, for example via ethtool
911 	 * set channels, we do delete queues and then add the queues back
912 	 * instead of deleting and reallocating the vport.
913 	 */
914 	if (test_and_clear_bit(IDPF_VPORT_DEL_QUEUES, vport->flags))
915 		idpf_send_delete_queues_msg(vport);
916 
917 	idpf_remove_features(vport);
918 
919 	vport->link_up = false;
920 	idpf_vport_intr_deinit(vport);
921 	idpf_xdp_rxq_info_deinit_all(vport);
922 	idpf_vport_queues_rel(vport);
923 	idpf_vport_intr_rel(vport);
924 	np->state = __IDPF_VPORT_DOWN;
925 
926 	if (rtnl)
927 		rtnl_unlock();
928 }
929 
930 /**
931  * idpf_stop - Disables a network interface
932  * @netdev: network interface device structure
933  *
934  * The stop entry point is called when an interface is de-activated by the OS,
935  * and the netdevice enters the DOWN state.  The hardware is still under the
936  * driver's control, but the netdev interface is disabled.
937  *
938  * Returns success only - not allowed to fail
939  */
940 static int idpf_stop(struct net_device *netdev)
941 {
942 	struct idpf_netdev_priv *np = netdev_priv(netdev);
943 	struct idpf_vport *vport;
944 
945 	if (test_bit(IDPF_REMOVE_IN_PROG, np->adapter->flags))
946 		return 0;
947 
948 	idpf_vport_ctrl_lock(netdev);
949 	vport = idpf_netdev_to_vport(netdev);
950 
951 	idpf_vport_stop(vport, false);
952 
953 	idpf_vport_ctrl_unlock(netdev);
954 
955 	return 0;
956 }
957 
958 /**
959  * idpf_decfg_netdev - Unregister the netdev
960  * @vport: vport for which netdev to be unregistered
961  */
962 static void idpf_decfg_netdev(struct idpf_vport *vport)
963 {
964 	struct idpf_adapter *adapter = vport->adapter;
965 	u16 idx = vport->idx;
966 
967 	kfree(vport->rx_ptype_lkup);
968 	vport->rx_ptype_lkup = NULL;
969 
970 	if (test_and_clear_bit(IDPF_VPORT_REG_NETDEV,
971 			       adapter->vport_config[idx]->flags)) {
972 		unregister_netdev(vport->netdev);
973 		free_netdev(vport->netdev);
974 	}
975 	vport->netdev = NULL;
976 
977 	adapter->netdevs[idx] = NULL;
978 }
979 
980 /**
981  * idpf_vport_rel - Delete a vport and free its resources
982  * @vport: the vport being removed
983  */
984 static void idpf_vport_rel(struct idpf_vport *vport)
985 {
986 	struct idpf_adapter *adapter = vport->adapter;
987 	struct idpf_vport_config *vport_config;
988 	struct idpf_vector_info vec_info;
989 	struct idpf_rss_data *rss_data;
990 	struct idpf_vport_max_q max_q;
991 	u16 idx = vport->idx;
992 
993 	vport_config = adapter->vport_config[vport->idx];
994 	idpf_deinit_rss(vport);
995 	rss_data = &vport_config->user_config.rss_data;
996 	kfree(rss_data->rss_key);
997 	rss_data->rss_key = NULL;
998 
999 	idpf_send_destroy_vport_msg(vport);
1000 
1001 	/* Release all max queues allocated to the adapter's pool */
1002 	max_q.max_rxq = vport_config->max_q.max_rxq;
1003 	max_q.max_txq = vport_config->max_q.max_txq;
1004 	max_q.max_bufq = vport_config->max_q.max_bufq;
1005 	max_q.max_complq = vport_config->max_q.max_complq;
1006 	idpf_vport_dealloc_max_qs(adapter, &max_q);
1007 
1008 	/* Release all the allocated vectors on the stack */
1009 	vec_info.num_req_vecs = 0;
1010 	vec_info.num_curr_vecs = vport->num_q_vectors;
1011 	vec_info.default_vport = vport->default_vport;
1012 
1013 	idpf_req_rel_vector_indexes(adapter, vport->q_vector_idxs, &vec_info);
1014 
1015 	kfree(vport->q_vector_idxs);
1016 	vport->q_vector_idxs = NULL;
1017 
1018 	kfree(adapter->vport_params_recvd[idx]);
1019 	adapter->vport_params_recvd[idx] = NULL;
1020 	kfree(adapter->vport_params_reqd[idx]);
1021 	adapter->vport_params_reqd[idx] = NULL;
1022 	if (adapter->vport_config[idx]) {
1023 		kfree(adapter->vport_config[idx]->req_qs_chunks);
1024 		adapter->vport_config[idx]->req_qs_chunks = NULL;
1025 	}
1026 	kfree(vport);
1027 	adapter->num_alloc_vports--;
1028 }
1029 
1030 /**
1031  * idpf_vport_dealloc - cleanup and release a given vport
1032  * @vport: pointer to idpf vport structure
1033  *
1034  * returns nothing
1035  */
1036 static void idpf_vport_dealloc(struct idpf_vport *vport)
1037 {
1038 	struct idpf_adapter *adapter = vport->adapter;
1039 	unsigned int i = vport->idx;
1040 
1041 	idpf_idc_deinit_vport_aux_device(vport->vdev_info);
1042 
1043 	idpf_deinit_mac_addr(vport);
1044 	idpf_vport_stop(vport, true);
1045 
1046 	if (!test_bit(IDPF_HR_RESET_IN_PROG, adapter->flags))
1047 		idpf_decfg_netdev(vport);
1048 	if (test_bit(IDPF_REMOVE_IN_PROG, adapter->flags))
1049 		idpf_del_all_mac_filters(vport);
1050 
1051 	if (adapter->netdevs[i]) {
1052 		struct idpf_netdev_priv *np = netdev_priv(adapter->netdevs[i]);
1053 
1054 		np->vport = NULL;
1055 	}
1056 
1057 	idpf_vport_rel(vport);
1058 
1059 	adapter->vports[i] = NULL;
1060 	adapter->next_vport = idpf_get_free_slot(adapter);
1061 }
1062 
1063 /**
1064  * idpf_is_hsplit_supported - check whether the header split is supported
1065  * @vport: virtual port to check the capability for
1066  *
1067  * Return: true if it's supported by the HW/FW, false if not.
1068  */
1069 static bool idpf_is_hsplit_supported(const struct idpf_vport *vport)
1070 {
1071 	return idpf_is_queue_model_split(vport->rxq_model) &&
1072 	       idpf_is_cap_ena_all(vport->adapter, IDPF_HSPLIT_CAPS,
1073 				   IDPF_CAP_HSPLIT);
1074 }
1075 
1076 /**
1077  * idpf_vport_get_hsplit - get the current header split feature state
1078  * @vport: virtual port to query the state for
1079  *
1080  * Return: ``ETHTOOL_TCP_DATA_SPLIT_UNKNOWN`` if not supported,
1081  *         ``ETHTOOL_TCP_DATA_SPLIT_DISABLED`` if disabled,
1082  *         ``ETHTOOL_TCP_DATA_SPLIT_ENABLED`` if active.
1083  */
1084 u8 idpf_vport_get_hsplit(const struct idpf_vport *vport)
1085 {
1086 	const struct idpf_vport_user_config_data *config;
1087 
1088 	if (!idpf_is_hsplit_supported(vport))
1089 		return ETHTOOL_TCP_DATA_SPLIT_UNKNOWN;
1090 
1091 	config = &vport->adapter->vport_config[vport->idx]->user_config;
1092 
1093 	return test_bit(__IDPF_USER_FLAG_HSPLIT, config->user_flags) ?
1094 	       ETHTOOL_TCP_DATA_SPLIT_ENABLED :
1095 	       ETHTOOL_TCP_DATA_SPLIT_DISABLED;
1096 }
1097 
1098 /**
1099  * idpf_vport_set_hsplit - enable or disable header split on a given vport
1100  * @vport: virtual port to configure
1101  * @val: Ethtool flag controlling the header split state
1102  *
1103  * Return: true on success, false if not supported by the HW.
1104  */
1105 bool idpf_vport_set_hsplit(const struct idpf_vport *vport, u8 val)
1106 {
1107 	struct idpf_vport_user_config_data *config;
1108 
1109 	if (!idpf_is_hsplit_supported(vport))
1110 		return val == ETHTOOL_TCP_DATA_SPLIT_UNKNOWN;
1111 
1112 	config = &vport->adapter->vport_config[vport->idx]->user_config;
1113 
1114 	switch (val) {
1115 	case ETHTOOL_TCP_DATA_SPLIT_UNKNOWN:
1116 		/* Default is to enable */
1117 	case ETHTOOL_TCP_DATA_SPLIT_ENABLED:
1118 		__set_bit(__IDPF_USER_FLAG_HSPLIT, config->user_flags);
1119 		return true;
1120 	case ETHTOOL_TCP_DATA_SPLIT_DISABLED:
1121 		__clear_bit(__IDPF_USER_FLAG_HSPLIT, config->user_flags);
1122 		return true;
1123 	default:
1124 		return false;
1125 	}
1126 }
1127 
1128 /**
1129  * idpf_vport_alloc - Allocates the next available struct vport in the adapter
1130  * @adapter: board private structure
1131  * @max_q: vport max queue info
1132  *
1133  * returns a pointer to a vport on success, NULL on failure.
1134  */
1135 static struct idpf_vport *idpf_vport_alloc(struct idpf_adapter *adapter,
1136 					   struct idpf_vport_max_q *max_q)
1137 {
1138 	struct idpf_rss_data *rss_data;
1139 	u16 idx = adapter->next_vport;
1140 	struct idpf_vport *vport;
1141 	u16 num_max_q;
1142 
1143 	if (idx == IDPF_NO_FREE_SLOT)
1144 		return NULL;
1145 
1146 	vport = kzalloc(sizeof(*vport), GFP_KERNEL);
1147 	if (!vport)
1148 		return vport;
1149 
1150 	num_max_q = max(max_q->max_txq, max_q->max_rxq) + IDPF_RESERVED_VECS;
1151 	if (!adapter->vport_config[idx]) {
1152 		struct idpf_vport_config *vport_config;
1153 		struct idpf_q_coalesce *q_coal;
1154 
1155 		vport_config = kzalloc(sizeof(*vport_config), GFP_KERNEL);
1156 		if (!vport_config) {
1157 			kfree(vport);
1158 
1159 			return NULL;
1160 		}
1161 
1162 		q_coal = kcalloc(num_max_q, sizeof(*q_coal), GFP_KERNEL);
1163 		if (!q_coal) {
1164 			kfree(vport_config);
1165 			kfree(vport);
1166 
1167 			return NULL;
1168 		}
1169 		for (int i = 0; i < num_max_q; i++) {
1170 			q_coal[i].tx_intr_mode = IDPF_ITR_DYNAMIC;
1171 			q_coal[i].tx_coalesce_usecs = IDPF_ITR_TX_DEF;
1172 			q_coal[i].rx_intr_mode = IDPF_ITR_DYNAMIC;
1173 			q_coal[i].rx_coalesce_usecs = IDPF_ITR_RX_DEF;
1174 		}
1175 		vport_config->user_config.q_coalesce = q_coal;
1176 
1177 		adapter->vport_config[idx] = vport_config;
1178 	}
1179 
1180 	vport->idx = idx;
1181 	vport->adapter = adapter;
1182 	vport->compln_clean_budget = IDPF_TX_COMPLQ_CLEAN_BUDGET;
1183 	vport->default_vport = adapter->num_alloc_vports <
1184 			       idpf_get_default_vports(adapter);
1185 
1186 	vport->q_vector_idxs = kcalloc(num_max_q, sizeof(u16), GFP_KERNEL);
1187 	if (!vport->q_vector_idxs)
1188 		goto free_vport;
1189 
1190 	idpf_vport_init(vport, max_q);
1191 
1192 	/* This alloc is done separate from the LUT because it's not strictly
1193 	 * dependent on how many queues we have. If we change number of queues
1194 	 * and soft reset we'll need a new LUT but the key can remain the same
1195 	 * for as long as the vport exists.
1196 	 */
1197 	rss_data = &adapter->vport_config[idx]->user_config.rss_data;
1198 	rss_data->rss_key = kzalloc(rss_data->rss_key_size, GFP_KERNEL);
1199 	if (!rss_data->rss_key)
1200 		goto free_vector_idxs;
1201 
1202 	/* Initialize default rss key */
1203 	netdev_rss_key_fill((void *)rss_data->rss_key, rss_data->rss_key_size);
1204 
1205 	/* fill vport slot in the adapter struct */
1206 	adapter->vports[idx] = vport;
1207 	adapter->vport_ids[idx] = idpf_get_vport_id(vport);
1208 
1209 	adapter->num_alloc_vports++;
1210 	/* prepare adapter->next_vport for next use */
1211 	adapter->next_vport = idpf_get_free_slot(adapter);
1212 
1213 	return vport;
1214 
1215 free_vector_idxs:
1216 	kfree(vport->q_vector_idxs);
1217 free_vport:
1218 	kfree(vport);
1219 
1220 	return NULL;
1221 }
1222 
1223 /**
1224  * idpf_get_stats64 - get statistics for network device structure
1225  * @netdev: network interface device structure
1226  * @stats: main device statistics structure
1227  */
1228 static void idpf_get_stats64(struct net_device *netdev,
1229 			     struct rtnl_link_stats64 *stats)
1230 {
1231 	struct idpf_netdev_priv *np = netdev_priv(netdev);
1232 
1233 	spin_lock_bh(&np->stats_lock);
1234 	*stats = np->netstats;
1235 	spin_unlock_bh(&np->stats_lock);
1236 }
1237 
1238 /**
1239  * idpf_statistics_task - Delayed task to get statistics over mailbox
1240  * @work: work_struct handle to our data
1241  */
1242 void idpf_statistics_task(struct work_struct *work)
1243 {
1244 	struct idpf_adapter *adapter;
1245 	int i;
1246 
1247 	adapter = container_of(work, struct idpf_adapter, stats_task.work);
1248 
1249 	for (i = 0; i < adapter->max_vports; i++) {
1250 		struct idpf_vport *vport = adapter->vports[i];
1251 
1252 		if (vport && !test_bit(IDPF_HR_RESET_IN_PROG, adapter->flags))
1253 			idpf_send_get_stats_msg(vport);
1254 	}
1255 
1256 	queue_delayed_work(adapter->stats_wq, &adapter->stats_task,
1257 			   msecs_to_jiffies(10000));
1258 }
1259 
1260 /**
1261  * idpf_mbx_task - Delayed task to handle mailbox responses
1262  * @work: work_struct handle
1263  */
1264 void idpf_mbx_task(struct work_struct *work)
1265 {
1266 	struct idpf_adapter *adapter;
1267 
1268 	adapter = container_of(work, struct idpf_adapter, mbx_task.work);
1269 
1270 	if (test_bit(IDPF_MB_INTR_MODE, adapter->flags))
1271 		idpf_mb_irq_enable(adapter);
1272 	else
1273 		queue_delayed_work(adapter->mbx_wq, &adapter->mbx_task,
1274 				   msecs_to_jiffies(300));
1275 
1276 	idpf_recv_mb_msg(adapter);
1277 }
1278 
1279 /**
1280  * idpf_service_task - Delayed task for handling mailbox responses
1281  * @work: work_struct handle to our data
1282  *
1283  */
1284 void idpf_service_task(struct work_struct *work)
1285 {
1286 	struct idpf_adapter *adapter;
1287 
1288 	adapter = container_of(work, struct idpf_adapter, serv_task.work);
1289 
1290 	if (idpf_is_reset_detected(adapter) &&
1291 	    !idpf_is_reset_in_prog(adapter) &&
1292 	    !test_bit(IDPF_REMOVE_IN_PROG, adapter->flags)) {
1293 		dev_info(&adapter->pdev->dev, "HW reset detected\n");
1294 		set_bit(IDPF_HR_FUNC_RESET, adapter->flags);
1295 		queue_delayed_work(adapter->vc_event_wq,
1296 				   &adapter->vc_event_task,
1297 				   msecs_to_jiffies(10));
1298 	}
1299 
1300 	queue_delayed_work(adapter->serv_wq, &adapter->serv_task,
1301 			   msecs_to_jiffies(300));
1302 }
1303 
1304 /**
1305  * idpf_restore_features - Restore feature configs
1306  * @vport: virtual port structure
1307  */
1308 static void idpf_restore_features(struct idpf_vport *vport)
1309 {
1310 	struct idpf_adapter *adapter = vport->adapter;
1311 
1312 	if (idpf_is_cap_ena(adapter, IDPF_OTHER_CAPS, VIRTCHNL2_CAP_MACFILTER))
1313 		idpf_restore_mac_filters(vport);
1314 }
1315 
1316 /**
1317  * idpf_set_real_num_queues - set number of queues for netdev
1318  * @vport: virtual port structure
1319  *
1320  * Returns 0 on success, negative on failure.
1321  */
1322 static int idpf_set_real_num_queues(struct idpf_vport *vport)
1323 {
1324 	int err, txq = vport->num_txq - vport->num_xdp_txq;
1325 
1326 	err = netif_set_real_num_rx_queues(vport->netdev, vport->num_rxq);
1327 	if (err)
1328 		return err;
1329 
1330 	return netif_set_real_num_tx_queues(vport->netdev, txq);
1331 }
1332 
1333 /**
1334  * idpf_up_complete - Complete interface up sequence
1335  * @vport: virtual port structure
1336  *
1337  * Returns 0 on success, negative on failure.
1338  */
1339 static int idpf_up_complete(struct idpf_vport *vport)
1340 {
1341 	struct idpf_netdev_priv *np = netdev_priv(vport->netdev);
1342 
1343 	if (vport->link_up && !netif_carrier_ok(vport->netdev)) {
1344 		netif_carrier_on(vport->netdev);
1345 		netif_tx_start_all_queues(vport->netdev);
1346 	}
1347 
1348 	np->state = __IDPF_VPORT_UP;
1349 
1350 	return 0;
1351 }
1352 
1353 /**
1354  * idpf_rx_init_buf_tail - Write initial buffer ring tail value
1355  * @vport: virtual port struct
1356  */
1357 static void idpf_rx_init_buf_tail(struct idpf_vport *vport)
1358 {
1359 	int i, j;
1360 
1361 	for (i = 0; i < vport->num_rxq_grp; i++) {
1362 		struct idpf_rxq_group *grp = &vport->rxq_grps[i];
1363 
1364 		if (idpf_is_queue_model_split(vport->rxq_model)) {
1365 			for (j = 0; j < vport->num_bufqs_per_qgrp; j++) {
1366 				const struct idpf_buf_queue *q =
1367 					&grp->splitq.bufq_sets[j].bufq;
1368 
1369 				writel(q->next_to_alloc, q->tail);
1370 			}
1371 		} else {
1372 			for (j = 0; j < grp->singleq.num_rxq; j++) {
1373 				const struct idpf_rx_queue *q =
1374 					grp->singleq.rxqs[j];
1375 
1376 				writel(q->next_to_alloc, q->tail);
1377 			}
1378 		}
1379 	}
1380 }
1381 
1382 /**
1383  * idpf_vport_open - Bring up a vport
1384  * @vport: vport to bring up
1385  * @rtnl: whether to take RTNL lock
1386  */
1387 static int idpf_vport_open(struct idpf_vport *vport, bool rtnl)
1388 {
1389 	struct idpf_netdev_priv *np = netdev_priv(vport->netdev);
1390 	struct idpf_adapter *adapter = vport->adapter;
1391 	struct idpf_vport_config *vport_config;
1392 	int err;
1393 
1394 	if (np->state != __IDPF_VPORT_DOWN)
1395 		return -EBUSY;
1396 
1397 	if (rtnl)
1398 		rtnl_lock();
1399 
1400 	/* we do not allow interface up just yet */
1401 	netif_carrier_off(vport->netdev);
1402 
1403 	err = idpf_vport_intr_alloc(vport);
1404 	if (err) {
1405 		dev_err(&adapter->pdev->dev, "Failed to allocate interrupts for vport %u: %d\n",
1406 			vport->vport_id, err);
1407 		goto err_rtnl_unlock;
1408 	}
1409 
1410 	err = idpf_vport_queues_alloc(vport);
1411 	if (err)
1412 		goto intr_rel;
1413 
1414 	err = idpf_vport_queue_ids_init(vport);
1415 	if (err) {
1416 		dev_err(&adapter->pdev->dev, "Failed to initialize queue ids for vport %u: %d\n",
1417 			vport->vport_id, err);
1418 		goto queues_rel;
1419 	}
1420 
1421 	err = idpf_vport_intr_init(vport);
1422 	if (err) {
1423 		dev_err(&adapter->pdev->dev, "Failed to initialize interrupts for vport %u: %d\n",
1424 			vport->vport_id, err);
1425 		goto queues_rel;
1426 	}
1427 
1428 	err = idpf_queue_reg_init(vport);
1429 	if (err) {
1430 		dev_err(&adapter->pdev->dev, "Failed to initialize queue registers for vport %u: %d\n",
1431 			vport->vport_id, err);
1432 		goto queues_rel;
1433 	}
1434 
1435 	err = idpf_rx_bufs_init_all(vport);
1436 	if (err) {
1437 		dev_err(&adapter->pdev->dev, "Failed to initialize RX buffers for vport %u: %d\n",
1438 			vport->vport_id, err);
1439 		goto queues_rel;
1440 	}
1441 
1442 	idpf_rx_init_buf_tail(vport);
1443 
1444 	err = idpf_xdp_rxq_info_init_all(vport);
1445 	if (err) {
1446 		netdev_err(vport->netdev,
1447 			   "Failed to initialize XDP RxQ info for vport %u: %pe\n",
1448 			   vport->vport_id, ERR_PTR(err));
1449 		goto intr_deinit;
1450 	}
1451 
1452 	idpf_vport_intr_ena(vport);
1453 
1454 	err = idpf_send_config_queues_msg(vport);
1455 	if (err) {
1456 		dev_err(&adapter->pdev->dev, "Failed to configure queues for vport %u, %d\n",
1457 			vport->vport_id, err);
1458 		goto rxq_deinit;
1459 	}
1460 
1461 	err = idpf_send_map_unmap_queue_vector_msg(vport, true);
1462 	if (err) {
1463 		dev_err(&adapter->pdev->dev, "Failed to map queue vectors for vport %u: %d\n",
1464 			vport->vport_id, err);
1465 		goto rxq_deinit;
1466 	}
1467 
1468 	err = idpf_send_enable_queues_msg(vport);
1469 	if (err) {
1470 		dev_err(&adapter->pdev->dev, "Failed to enable queues for vport %u: %d\n",
1471 			vport->vport_id, err);
1472 		goto unmap_queue_vectors;
1473 	}
1474 
1475 	err = idpf_send_enable_vport_msg(vport);
1476 	if (err) {
1477 		dev_err(&adapter->pdev->dev, "Failed to enable vport %u: %d\n",
1478 			vport->vport_id, err);
1479 		err = -EAGAIN;
1480 		goto disable_queues;
1481 	}
1482 
1483 	idpf_restore_features(vport);
1484 
1485 	vport_config = adapter->vport_config[vport->idx];
1486 	if (vport_config->user_config.rss_data.rss_lut)
1487 		err = idpf_config_rss(vport);
1488 	else
1489 		err = idpf_init_rss(vport);
1490 	if (err) {
1491 		dev_err(&adapter->pdev->dev, "Failed to initialize RSS for vport %u: %d\n",
1492 			vport->vport_id, err);
1493 		goto disable_vport;
1494 	}
1495 
1496 	err = idpf_up_complete(vport);
1497 	if (err) {
1498 		dev_err(&adapter->pdev->dev, "Failed to complete interface up for vport %u: %d\n",
1499 			vport->vport_id, err);
1500 		goto deinit_rss;
1501 	}
1502 
1503 	if (rtnl)
1504 		rtnl_unlock();
1505 
1506 	return 0;
1507 
1508 deinit_rss:
1509 	idpf_deinit_rss(vport);
1510 disable_vport:
1511 	idpf_send_disable_vport_msg(vport);
1512 disable_queues:
1513 	idpf_send_disable_queues_msg(vport);
1514 unmap_queue_vectors:
1515 	idpf_send_map_unmap_queue_vector_msg(vport, false);
1516 rxq_deinit:
1517 	idpf_xdp_rxq_info_deinit_all(vport);
1518 intr_deinit:
1519 	idpf_vport_intr_deinit(vport);
1520 queues_rel:
1521 	idpf_vport_queues_rel(vport);
1522 intr_rel:
1523 	idpf_vport_intr_rel(vport);
1524 
1525 err_rtnl_unlock:
1526 	if (rtnl)
1527 		rtnl_unlock();
1528 
1529 	return err;
1530 }
1531 
1532 /**
1533  * idpf_init_task - Delayed initialization task
1534  * @work: work_struct handle to our data
1535  *
1536  * Init task finishes up pending work started in probe. Due to the asynchronous
1537  * nature in which the device communicates with hardware, we may have to wait
1538  * several milliseconds to get a response.  Instead of busy polling in probe,
1539  * pulling it out into a delayed work task prevents us from bogging down the
1540  * whole system waiting for a response from hardware.
1541  */
1542 void idpf_init_task(struct work_struct *work)
1543 {
1544 	struct idpf_vport_config *vport_config;
1545 	struct idpf_vport_max_q max_q;
1546 	struct idpf_adapter *adapter;
1547 	struct idpf_netdev_priv *np;
1548 	struct idpf_vport *vport;
1549 	u16 num_default_vports;
1550 	struct pci_dev *pdev;
1551 	bool default_vport;
1552 	int index, err;
1553 
1554 	adapter = container_of(work, struct idpf_adapter, init_task.work);
1555 
1556 	num_default_vports = idpf_get_default_vports(adapter);
1557 	if (adapter->num_alloc_vports < num_default_vports)
1558 		default_vport = true;
1559 	else
1560 		default_vport = false;
1561 
1562 	err = idpf_vport_alloc_max_qs(adapter, &max_q);
1563 	if (err)
1564 		goto unwind_vports;
1565 
1566 	err = idpf_send_create_vport_msg(adapter, &max_q);
1567 	if (err) {
1568 		idpf_vport_dealloc_max_qs(adapter, &max_q);
1569 		goto unwind_vports;
1570 	}
1571 
1572 	pdev = adapter->pdev;
1573 	vport = idpf_vport_alloc(adapter, &max_q);
1574 	if (!vport) {
1575 		err = -EFAULT;
1576 		dev_err(&pdev->dev, "failed to allocate vport: %d\n",
1577 			err);
1578 		idpf_vport_dealloc_max_qs(adapter, &max_q);
1579 		goto unwind_vports;
1580 	}
1581 
1582 	index = vport->idx;
1583 	vport_config = adapter->vport_config[index];
1584 
1585 	spin_lock_init(&vport_config->mac_filter_list_lock);
1586 
1587 	INIT_LIST_HEAD(&vport_config->user_config.mac_filter_list);
1588 	INIT_LIST_HEAD(&vport_config->user_config.flow_steer_list);
1589 
1590 	err = idpf_check_supported_desc_ids(vport);
1591 	if (err) {
1592 		dev_err(&pdev->dev, "failed to get required descriptor ids\n");
1593 		goto cfg_netdev_err;
1594 	}
1595 
1596 	if (idpf_cfg_netdev(vport))
1597 		goto cfg_netdev_err;
1598 
1599 	err = idpf_send_get_rx_ptype_msg(vport);
1600 	if (err)
1601 		goto handle_err;
1602 
1603 	/* Once state is put into DOWN, driver is ready for dev_open */
1604 	np = netdev_priv(vport->netdev);
1605 	np->state = __IDPF_VPORT_DOWN;
1606 	if (test_and_clear_bit(IDPF_VPORT_UP_REQUESTED, vport_config->flags))
1607 		idpf_vport_open(vport, true);
1608 
1609 	/* Spawn and return 'idpf_init_task' work queue until all the
1610 	 * default vports are created
1611 	 */
1612 	if (adapter->num_alloc_vports < num_default_vports) {
1613 		queue_delayed_work(adapter->init_wq, &adapter->init_task,
1614 				   msecs_to_jiffies(5 * (adapter->pdev->devfn & 0x07)));
1615 
1616 		return;
1617 	}
1618 
1619 	for (index = 0; index < adapter->max_vports; index++) {
1620 		struct net_device *netdev = adapter->netdevs[index];
1621 		struct idpf_vport_config *vport_config;
1622 
1623 		vport_config = adapter->vport_config[index];
1624 
1625 		if (!netdev ||
1626 		    test_bit(IDPF_VPORT_REG_NETDEV, vport_config->flags))
1627 			continue;
1628 
1629 		err = register_netdev(netdev);
1630 		if (err) {
1631 			dev_err(&pdev->dev, "failed to register netdev for vport %d: %pe\n",
1632 				index, ERR_PTR(err));
1633 			continue;
1634 		}
1635 		set_bit(IDPF_VPORT_REG_NETDEV, vport_config->flags);
1636 	}
1637 
1638 	/* As all the required vports are created, clear the reset flag
1639 	 * unconditionally here in case we were in reset and the link was down.
1640 	 */
1641 	clear_bit(IDPF_HR_RESET_IN_PROG, adapter->flags);
1642 	/* Start the statistics task now */
1643 	queue_delayed_work(adapter->stats_wq, &adapter->stats_task,
1644 			   msecs_to_jiffies(10 * (pdev->devfn & 0x07)));
1645 
1646 	return;
1647 
1648 handle_err:
1649 	idpf_decfg_netdev(vport);
1650 cfg_netdev_err:
1651 	idpf_vport_rel(vport);
1652 	adapter->vports[index] = NULL;
1653 unwind_vports:
1654 	if (default_vport) {
1655 		for (index = 0; index < adapter->max_vports; index++) {
1656 			if (adapter->vports[index])
1657 				idpf_vport_dealloc(adapter->vports[index]);
1658 		}
1659 	}
1660 	clear_bit(IDPF_HR_RESET_IN_PROG, adapter->flags);
1661 }
1662 
1663 /**
1664  * idpf_sriov_ena - Enable or change number of VFs
1665  * @adapter: private data struct
1666  * @num_vfs: number of VFs to allocate
1667  */
1668 static int idpf_sriov_ena(struct idpf_adapter *adapter, int num_vfs)
1669 {
1670 	struct device *dev = &adapter->pdev->dev;
1671 	int err;
1672 
1673 	err = idpf_send_set_sriov_vfs_msg(adapter, num_vfs);
1674 	if (err) {
1675 		dev_err(dev, "Failed to allocate VFs: %d\n", err);
1676 
1677 		return err;
1678 	}
1679 
1680 	err = pci_enable_sriov(adapter->pdev, num_vfs);
1681 	if (err) {
1682 		idpf_send_set_sriov_vfs_msg(adapter, 0);
1683 		dev_err(dev, "Failed to enable SR-IOV: %d\n", err);
1684 
1685 		return err;
1686 	}
1687 
1688 	adapter->num_vfs = num_vfs;
1689 
1690 	return num_vfs;
1691 }
1692 
1693 /**
1694  * idpf_sriov_configure - Configure the requested VFs
1695  * @pdev: pointer to a pci_dev structure
1696  * @num_vfs: number of vfs to allocate
1697  *
1698  * Enable or change the number of VFs. Called when the user updates the number
1699  * of VFs in sysfs.
1700  **/
1701 int idpf_sriov_configure(struct pci_dev *pdev, int num_vfs)
1702 {
1703 	struct idpf_adapter *adapter = pci_get_drvdata(pdev);
1704 
1705 	if (!idpf_is_cap_ena(adapter, IDPF_OTHER_CAPS, VIRTCHNL2_CAP_SRIOV)) {
1706 		dev_info(&pdev->dev, "SR-IOV is not supported on this device\n");
1707 
1708 		return -EOPNOTSUPP;
1709 	}
1710 
1711 	if (num_vfs)
1712 		return idpf_sriov_ena(adapter, num_vfs);
1713 
1714 	if (pci_vfs_assigned(pdev)) {
1715 		dev_warn(&pdev->dev, "Unable to free VFs because some are assigned to VMs\n");
1716 
1717 		return -EBUSY;
1718 	}
1719 
1720 	pci_disable_sriov(adapter->pdev);
1721 	idpf_send_set_sriov_vfs_msg(adapter, 0);
1722 	adapter->num_vfs = 0;
1723 
1724 	return 0;
1725 }
1726 
1727 /**
1728  * idpf_deinit_task - Device deinit routine
1729  * @adapter: Driver specific private structure
1730  *
1731  * Extended remove logic which will be used for
1732  * hard reset as well
1733  */
1734 void idpf_deinit_task(struct idpf_adapter *adapter)
1735 {
1736 	unsigned int i;
1737 
1738 	/* Wait until the init_task is done else this thread might release
1739 	 * the resources first and the other thread might end up in a bad state
1740 	 */
1741 	cancel_delayed_work_sync(&adapter->init_task);
1742 
1743 	if (!adapter->vports)
1744 		return;
1745 
1746 	cancel_delayed_work_sync(&adapter->stats_task);
1747 
1748 	for (i = 0; i < adapter->max_vports; i++) {
1749 		if (adapter->vports[i])
1750 			idpf_vport_dealloc(adapter->vports[i]);
1751 	}
1752 }
1753 
1754 /**
1755  * idpf_check_reset_complete - check that reset is complete
1756  * @hw: pointer to hw struct
1757  * @reset_reg: struct with reset registers
1758  *
1759  * Returns 0 if device is ready to use, or -EBUSY if it's in reset.
1760  **/
1761 static int idpf_check_reset_complete(struct idpf_hw *hw,
1762 				     struct idpf_reset_reg *reset_reg)
1763 {
1764 	struct idpf_adapter *adapter = hw->back;
1765 	int i;
1766 
1767 	for (i = 0; i < 2000; i++) {
1768 		u32 reg_val = readl(reset_reg->rstat);
1769 
1770 		/* 0xFFFFFFFF might be read if other side hasn't cleared the
1771 		 * register for us yet and 0xFFFFFFFF is not a valid value for
1772 		 * the register, so treat that as invalid.
1773 		 */
1774 		if (reg_val != 0xFFFFFFFF && (reg_val & reset_reg->rstat_m))
1775 			return 0;
1776 
1777 		usleep_range(5000, 10000);
1778 	}
1779 
1780 	dev_warn(&adapter->pdev->dev, "Device reset timeout!\n");
1781 	/* Clear the reset flag unconditionally here since the reset
1782 	 * technically isn't in progress anymore from the driver's perspective
1783 	 */
1784 	clear_bit(IDPF_HR_RESET_IN_PROG, adapter->flags);
1785 
1786 	return -EBUSY;
1787 }
1788 
1789 /**
1790  * idpf_set_vport_state - Set the vport state to be after the reset
1791  * @adapter: Driver specific private structure
1792  */
1793 static void idpf_set_vport_state(struct idpf_adapter *adapter)
1794 {
1795 	u16 i;
1796 
1797 	for (i = 0; i < adapter->max_vports; i++) {
1798 		struct idpf_netdev_priv *np;
1799 
1800 		if (!adapter->netdevs[i])
1801 			continue;
1802 
1803 		np = netdev_priv(adapter->netdevs[i]);
1804 		if (np->state == __IDPF_VPORT_UP)
1805 			set_bit(IDPF_VPORT_UP_REQUESTED,
1806 				adapter->vport_config[i]->flags);
1807 	}
1808 }
1809 
1810 /**
1811  * idpf_init_hard_reset - Initiate a hardware reset
1812  * @adapter: Driver specific private structure
1813  *
1814  * Deallocate the vports and all the resources associated with them and
1815  * reallocate. Also reinitialize the mailbox. Return 0 on success,
1816  * negative on failure.
1817  */
1818 static int idpf_init_hard_reset(struct idpf_adapter *adapter)
1819 {
1820 	struct idpf_reg_ops *reg_ops = &adapter->dev_ops.reg_ops;
1821 	struct device *dev = &adapter->pdev->dev;
1822 	struct net_device *netdev;
1823 	int err;
1824 	u16 i;
1825 
1826 	mutex_lock(&adapter->vport_ctrl_lock);
1827 
1828 	dev_info(dev, "Device HW Reset initiated\n");
1829 
1830 	/* Avoid TX hangs on reset */
1831 	for (i = 0; i < adapter->max_vports; i++) {
1832 		netdev = adapter->netdevs[i];
1833 		if (!netdev)
1834 			continue;
1835 
1836 		netif_carrier_off(netdev);
1837 		netif_tx_disable(netdev);
1838 	}
1839 
1840 	/* Prepare for reset */
1841 	if (test_and_clear_bit(IDPF_HR_DRV_LOAD, adapter->flags)) {
1842 		reg_ops->trigger_reset(adapter, IDPF_HR_DRV_LOAD);
1843 	} else if (test_and_clear_bit(IDPF_HR_FUNC_RESET, adapter->flags)) {
1844 		bool is_reset = idpf_is_reset_detected(adapter);
1845 
1846 		idpf_idc_issue_reset_event(adapter->cdev_info);
1847 
1848 		idpf_set_vport_state(adapter);
1849 		idpf_vc_core_deinit(adapter);
1850 		if (!is_reset)
1851 			reg_ops->trigger_reset(adapter, IDPF_HR_FUNC_RESET);
1852 		idpf_deinit_dflt_mbx(adapter);
1853 	} else {
1854 		dev_err(dev, "Unhandled hard reset cause\n");
1855 		err = -EBADRQC;
1856 		goto unlock_mutex;
1857 	}
1858 
1859 	/* Wait for reset to complete */
1860 	err = idpf_check_reset_complete(&adapter->hw, &adapter->reset_reg);
1861 	if (err) {
1862 		dev_err(dev, "The driver was unable to contact the device's firmware. Check that the FW is running. Driver state= 0x%x\n",
1863 			adapter->state);
1864 		goto unlock_mutex;
1865 	}
1866 
1867 	/* Reset is complete and so start building the driver resources again */
1868 	err = idpf_init_dflt_mbx(adapter);
1869 	if (err) {
1870 		dev_err(dev, "Failed to initialize default mailbox: %d\n", err);
1871 		goto unlock_mutex;
1872 	}
1873 
1874 	queue_delayed_work(adapter->mbx_wq, &adapter->mbx_task, 0);
1875 
1876 	/* Initialize the state machine, also allocate memory and request
1877 	 * resources
1878 	 */
1879 	err = idpf_vc_core_init(adapter);
1880 	if (err) {
1881 		cancel_delayed_work_sync(&adapter->mbx_task);
1882 		idpf_deinit_dflt_mbx(adapter);
1883 		goto unlock_mutex;
1884 	}
1885 
1886 	/* Wait till all the vports are initialized to release the reset lock,
1887 	 * else user space callbacks may access uninitialized vports
1888 	 */
1889 	while (test_bit(IDPF_HR_RESET_IN_PROG, adapter->flags))
1890 		msleep(100);
1891 
1892 unlock_mutex:
1893 	mutex_unlock(&adapter->vport_ctrl_lock);
1894 
1895 	/* Wait until all vports are created to init RDMA CORE AUX */
1896 	if (!err)
1897 		err = idpf_idc_init(adapter);
1898 
1899 	return err;
1900 }
1901 
1902 /**
1903  * idpf_vc_event_task - Handle virtchannel event logic
1904  * @work: work queue struct
1905  */
1906 void idpf_vc_event_task(struct work_struct *work)
1907 {
1908 	struct idpf_adapter *adapter;
1909 
1910 	adapter = container_of(work, struct idpf_adapter, vc_event_task.work);
1911 
1912 	if (test_bit(IDPF_REMOVE_IN_PROG, adapter->flags))
1913 		return;
1914 
1915 	if (test_bit(IDPF_HR_FUNC_RESET, adapter->flags))
1916 		goto func_reset;
1917 
1918 	if (test_bit(IDPF_HR_DRV_LOAD, adapter->flags))
1919 		goto drv_load;
1920 
1921 	return;
1922 
1923 func_reset:
1924 	idpf_vc_xn_shutdown(adapter->vcxn_mngr);
1925 drv_load:
1926 	set_bit(IDPF_HR_RESET_IN_PROG, adapter->flags);
1927 	idpf_init_hard_reset(adapter);
1928 }
1929 
1930 /**
1931  * idpf_initiate_soft_reset - Initiate a software reset
1932  * @vport: virtual port data struct
1933  * @reset_cause: reason for the soft reset
1934  *
1935  * Soft reset only reallocs vport queue resources. Returns 0 on success,
1936  * negative on failure.
1937  */
1938 int idpf_initiate_soft_reset(struct idpf_vport *vport,
1939 			     enum idpf_vport_reset_cause reset_cause)
1940 {
1941 	struct idpf_netdev_priv *np = netdev_priv(vport->netdev);
1942 	enum idpf_vport_state current_state = np->state;
1943 	struct idpf_adapter *adapter = vport->adapter;
1944 	struct idpf_vport *new_vport;
1945 	int err;
1946 
1947 	/* If the system is low on memory, we can end up in bad state if we
1948 	 * free all the memory for queue resources and try to allocate them
1949 	 * again. Instead, we can pre-allocate the new resources before doing
1950 	 * anything and bailing if the alloc fails.
1951 	 *
1952 	 * Make a clone of the existing vport to mimic its current
1953 	 * configuration, then modify the new structure with any requested
1954 	 * changes. Once the allocation of the new resources is done, stop the
1955 	 * existing vport and copy the configuration to the main vport. If an
1956 	 * error occurred, the existing vport will be untouched.
1957 	 *
1958 	 */
1959 	new_vport = kzalloc(sizeof(*vport), GFP_KERNEL);
1960 	if (!new_vport)
1961 		return -ENOMEM;
1962 
1963 	/* This purposely avoids copying the end of the struct because it
1964 	 * contains wait_queues and mutexes and other stuff we don't want to
1965 	 * mess with. Nothing below should use those variables from new_vport
1966 	 * and should instead always refer to them in vport if they need to.
1967 	 */
1968 	memcpy(new_vport, vport, offsetof(struct idpf_vport, link_up));
1969 
1970 	/* Adjust resource parameters prior to reallocating resources */
1971 	switch (reset_cause) {
1972 	case IDPF_SR_Q_CHANGE:
1973 		err = idpf_vport_adjust_qs(new_vport);
1974 		if (err)
1975 			goto free_vport;
1976 		break;
1977 	case IDPF_SR_Q_DESC_CHANGE:
1978 		/* Update queue parameters before allocating resources */
1979 		idpf_vport_calc_num_q_desc(new_vport);
1980 		break;
1981 	case IDPF_SR_MTU_CHANGE:
1982 		idpf_idc_vdev_mtu_event(vport->vdev_info,
1983 					IIDC_RDMA_EVENT_BEFORE_MTU_CHANGE);
1984 		break;
1985 	case IDPF_SR_RSC_CHANGE:
1986 		break;
1987 	default:
1988 		dev_err(&adapter->pdev->dev, "Unhandled soft reset cause\n");
1989 		err = -EINVAL;
1990 		goto free_vport;
1991 	}
1992 
1993 	if (current_state <= __IDPF_VPORT_DOWN) {
1994 		idpf_send_delete_queues_msg(vport);
1995 	} else {
1996 		set_bit(IDPF_VPORT_DEL_QUEUES, vport->flags);
1997 		idpf_vport_stop(vport, false);
1998 	}
1999 
2000 	idpf_deinit_rss(vport);
2001 	/* We're passing in vport here because we need its wait_queue
2002 	 * to send a message and it should be getting all the vport
2003 	 * config data out of the adapter but we need to be careful not
2004 	 * to add code to add_queues to change the vport config within
2005 	 * vport itself as it will be wiped with a memcpy later.
2006 	 */
2007 	err = idpf_send_add_queues_msg(vport, new_vport->num_txq,
2008 				       new_vport->num_complq,
2009 				       new_vport->num_rxq,
2010 				       new_vport->num_bufq);
2011 	if (err)
2012 		goto err_reset;
2013 
2014 	/* Same comment as above regarding avoiding copying the wait_queues and
2015 	 * mutexes applies here. We do not want to mess with those if possible.
2016 	 */
2017 	memcpy(vport, new_vport, offsetof(struct idpf_vport, link_up));
2018 
2019 	if (reset_cause == IDPF_SR_Q_CHANGE)
2020 		idpf_vport_alloc_vec_indexes(vport);
2021 
2022 	err = idpf_set_real_num_queues(vport);
2023 	if (err)
2024 		goto err_open;
2025 
2026 	if (current_state == __IDPF_VPORT_UP)
2027 		err = idpf_vport_open(vport, false);
2028 
2029 	goto free_vport;
2030 
2031 err_reset:
2032 	idpf_send_add_queues_msg(vport, vport->num_txq, vport->num_complq,
2033 				 vport->num_rxq, vport->num_bufq);
2034 
2035 err_open:
2036 	if (current_state == __IDPF_VPORT_UP)
2037 		idpf_vport_open(vport, false);
2038 
2039 free_vport:
2040 	kfree(new_vport);
2041 
2042 	if (reset_cause == IDPF_SR_MTU_CHANGE)
2043 		idpf_idc_vdev_mtu_event(vport->vdev_info,
2044 					IIDC_RDMA_EVENT_AFTER_MTU_CHANGE);
2045 
2046 	return err;
2047 }
2048 
2049 /**
2050  * idpf_addr_sync - Callback for dev_(mc|uc)_sync to add address
2051  * @netdev: the netdevice
2052  * @addr: address to add
2053  *
2054  * Called by __dev_(mc|uc)_sync when an address needs to be added. We call
2055  * __dev_(uc|mc)_sync from .set_rx_mode. Kernel takes addr_list_lock spinlock
2056  * meaning we cannot sleep in this context. Due to this, we have to add the
2057  * filter and send the virtchnl message asynchronously without waiting for the
2058  * response from the other side. We won't know whether or not the operation
2059  * actually succeeded until we get the message back.  Returns 0 on success,
2060  * negative on failure.
2061  */
2062 static int idpf_addr_sync(struct net_device *netdev, const u8 *addr)
2063 {
2064 	struct idpf_netdev_priv *np = netdev_priv(netdev);
2065 
2066 	return idpf_add_mac_filter(np->vport, np, addr, true);
2067 }
2068 
2069 /**
2070  * idpf_addr_unsync - Callback for dev_(mc|uc)_sync to remove address
2071  * @netdev: the netdevice
2072  * @addr: address to add
2073  *
2074  * Called by __dev_(mc|uc)_sync when an address needs to be added. We call
2075  * __dev_(uc|mc)_sync from .set_rx_mode. Kernel takes addr_list_lock spinlock
2076  * meaning we cannot sleep in this context. Due to this we have to delete the
2077  * filter and send the virtchnl message asynchronously without waiting for the
2078  * return from the other side.  We won't know whether or not the operation
2079  * actually succeeded until we get the message back. Returns 0 on success,
2080  * negative on failure.
2081  */
2082 static int idpf_addr_unsync(struct net_device *netdev, const u8 *addr)
2083 {
2084 	struct idpf_netdev_priv *np = netdev_priv(netdev);
2085 
2086 	/* Under some circumstances, we might receive a request to delete
2087 	 * our own device address from our uc list. Because we store the
2088 	 * device address in the VSI's MAC filter list, we need to ignore
2089 	 * such requests and not delete our device address from this list.
2090 	 */
2091 	if (ether_addr_equal(addr, netdev->dev_addr))
2092 		return 0;
2093 
2094 	idpf_del_mac_filter(np->vport, np, addr, true);
2095 
2096 	return 0;
2097 }
2098 
2099 /**
2100  * idpf_set_rx_mode - NDO callback to set the netdev filters
2101  * @netdev: network interface device structure
2102  *
2103  * Stack takes addr_list_lock spinlock before calling our .set_rx_mode.  We
2104  * cannot sleep in this context.
2105  */
2106 static void idpf_set_rx_mode(struct net_device *netdev)
2107 {
2108 	struct idpf_netdev_priv *np = netdev_priv(netdev);
2109 	struct idpf_vport_user_config_data *config_data;
2110 	struct idpf_adapter *adapter;
2111 	bool changed = false;
2112 	struct device *dev;
2113 	int err;
2114 
2115 	adapter = np->adapter;
2116 	dev = &adapter->pdev->dev;
2117 
2118 	if (idpf_is_cap_ena(adapter, IDPF_OTHER_CAPS, VIRTCHNL2_CAP_MACFILTER)) {
2119 		__dev_uc_sync(netdev, idpf_addr_sync, idpf_addr_unsync);
2120 		__dev_mc_sync(netdev, idpf_addr_sync, idpf_addr_unsync);
2121 	}
2122 
2123 	if (!idpf_is_cap_ena(adapter, IDPF_OTHER_CAPS, VIRTCHNL2_CAP_PROMISC))
2124 		return;
2125 
2126 	config_data = &adapter->vport_config[np->vport_idx]->user_config;
2127 	/* IFF_PROMISC enables both unicast and multicast promiscuous,
2128 	 * while IFF_ALLMULTI only enables multicast such that:
2129 	 *
2130 	 * promisc  + allmulti		= unicast | multicast
2131 	 * promisc  + !allmulti		= unicast | multicast
2132 	 * !promisc + allmulti		= multicast
2133 	 */
2134 	if ((netdev->flags & IFF_PROMISC) &&
2135 	    !test_and_set_bit(__IDPF_PROMISC_UC, config_data->user_flags)) {
2136 		changed = true;
2137 		dev_info(&adapter->pdev->dev, "Entering promiscuous mode\n");
2138 		if (!test_and_set_bit(__IDPF_PROMISC_MC, adapter->flags))
2139 			dev_info(dev, "Entering multicast promiscuous mode\n");
2140 	}
2141 
2142 	if (!(netdev->flags & IFF_PROMISC) &&
2143 	    test_and_clear_bit(__IDPF_PROMISC_UC, config_data->user_flags)) {
2144 		changed = true;
2145 		dev_info(dev, "Leaving promiscuous mode\n");
2146 	}
2147 
2148 	if (netdev->flags & IFF_ALLMULTI &&
2149 	    !test_and_set_bit(__IDPF_PROMISC_MC, config_data->user_flags)) {
2150 		changed = true;
2151 		dev_info(dev, "Entering multicast promiscuous mode\n");
2152 	}
2153 
2154 	if (!(netdev->flags & (IFF_ALLMULTI | IFF_PROMISC)) &&
2155 	    test_and_clear_bit(__IDPF_PROMISC_MC, config_data->user_flags)) {
2156 		changed = true;
2157 		dev_info(dev, "Leaving multicast promiscuous mode\n");
2158 	}
2159 
2160 	if (!changed)
2161 		return;
2162 
2163 	err = idpf_set_promiscuous(adapter, config_data, np->vport_id);
2164 	if (err)
2165 		dev_err(dev, "Failed to set promiscuous mode: %d\n", err);
2166 }
2167 
2168 /**
2169  * idpf_vport_manage_rss_lut - disable/enable RSS
2170  * @vport: the vport being changed
2171  *
2172  * In the event of disable request for RSS, this function will zero out RSS
2173  * LUT, while in the event of enable request for RSS, it will reconfigure RSS
2174  * LUT with the default LUT configuration.
2175  */
2176 static int idpf_vport_manage_rss_lut(struct idpf_vport *vport)
2177 {
2178 	bool ena = idpf_is_feature_ena(vport, NETIF_F_RXHASH);
2179 	struct idpf_rss_data *rss_data;
2180 	u16 idx = vport->idx;
2181 	int lut_size;
2182 
2183 	rss_data = &vport->adapter->vport_config[idx]->user_config.rss_data;
2184 	lut_size = rss_data->rss_lut_size * sizeof(u32);
2185 
2186 	if (ena) {
2187 		/* This will contain the default or user configured LUT */
2188 		memcpy(rss_data->rss_lut, rss_data->cached_lut, lut_size);
2189 	} else {
2190 		/* Save a copy of the current LUT to be restored later if
2191 		 * requested.
2192 		 */
2193 		memcpy(rss_data->cached_lut, rss_data->rss_lut, lut_size);
2194 
2195 		/* Zero out the current LUT to disable */
2196 		memset(rss_data->rss_lut, 0, lut_size);
2197 	}
2198 
2199 	return idpf_config_rss(vport);
2200 }
2201 
2202 /**
2203  * idpf_set_features - set the netdev feature flags
2204  * @netdev: ptr to the netdev being adjusted
2205  * @features: the feature set that the stack is suggesting
2206  */
2207 static int idpf_set_features(struct net_device *netdev,
2208 			     netdev_features_t features)
2209 {
2210 	netdev_features_t changed = netdev->features ^ features;
2211 	struct idpf_adapter *adapter;
2212 	struct idpf_vport *vport;
2213 	int err = 0;
2214 
2215 	idpf_vport_ctrl_lock(netdev);
2216 	vport = idpf_netdev_to_vport(netdev);
2217 
2218 	adapter = vport->adapter;
2219 
2220 	if (idpf_is_reset_in_prog(adapter)) {
2221 		dev_err(&adapter->pdev->dev, "Device is resetting, changing netdev features temporarily unavailable.\n");
2222 		err = -EBUSY;
2223 		goto unlock_mutex;
2224 	}
2225 
2226 	if (changed & NETIF_F_RXHASH) {
2227 		netdev->features ^= NETIF_F_RXHASH;
2228 		err = idpf_vport_manage_rss_lut(vport);
2229 		if (err)
2230 			goto unlock_mutex;
2231 	}
2232 
2233 	if (changed & NETIF_F_GRO_HW) {
2234 		netdev->features ^= NETIF_F_GRO_HW;
2235 		err = idpf_initiate_soft_reset(vport, IDPF_SR_RSC_CHANGE);
2236 		if (err)
2237 			goto unlock_mutex;
2238 	}
2239 
2240 	if (changed & NETIF_F_LOOPBACK) {
2241 		netdev->features ^= NETIF_F_LOOPBACK;
2242 		err = idpf_send_ena_dis_loopback_msg(vport);
2243 	}
2244 
2245 unlock_mutex:
2246 	idpf_vport_ctrl_unlock(netdev);
2247 
2248 	return err;
2249 }
2250 
2251 /**
2252  * idpf_open - Called when a network interface becomes active
2253  * @netdev: network interface device structure
2254  *
2255  * The open entry point is called when a network interface is made
2256  * active by the system (IFF_UP).  At this point all resources needed
2257  * for transmit and receive operations are allocated, the interrupt
2258  * handler is registered with the OS, the netdev watchdog is enabled,
2259  * and the stack is notified that the interface is ready.
2260  *
2261  * Returns 0 on success, negative value on failure
2262  */
2263 static int idpf_open(struct net_device *netdev)
2264 {
2265 	struct idpf_vport *vport;
2266 	int err;
2267 
2268 	idpf_vport_ctrl_lock(netdev);
2269 	vport = idpf_netdev_to_vport(netdev);
2270 
2271 	err = idpf_set_real_num_queues(vport);
2272 	if (err)
2273 		goto unlock;
2274 
2275 	err = idpf_vport_open(vport, false);
2276 
2277 unlock:
2278 	idpf_vport_ctrl_unlock(netdev);
2279 
2280 	return err;
2281 }
2282 
2283 /**
2284  * idpf_change_mtu - NDO callback to change the MTU
2285  * @netdev: network interface device structure
2286  * @new_mtu: new value for maximum frame size
2287  *
2288  * Returns 0 on success, negative on failure
2289  */
2290 static int idpf_change_mtu(struct net_device *netdev, int new_mtu)
2291 {
2292 	struct idpf_vport *vport;
2293 	int err;
2294 
2295 	idpf_vport_ctrl_lock(netdev);
2296 	vport = idpf_netdev_to_vport(netdev);
2297 
2298 	WRITE_ONCE(netdev->mtu, new_mtu);
2299 
2300 	err = idpf_initiate_soft_reset(vport, IDPF_SR_MTU_CHANGE);
2301 
2302 	idpf_vport_ctrl_unlock(netdev);
2303 
2304 	return err;
2305 }
2306 
2307 /**
2308  * idpf_chk_tso_segment - Check skb is not using too many buffers
2309  * @skb: send buffer
2310  * @max_bufs: maximum number of buffers
2311  *
2312  * For TSO we need to count the TSO header and segment payload separately.  As
2313  * such we need to check cases where we have max_bufs-1 fragments or more as we
2314  * can potentially require max_bufs+1 DMA transactions, 1 for the TSO header, 1
2315  * for the segment payload in the first descriptor, and another max_buf-1 for
2316  * the fragments.
2317  *
2318  * Returns true if the packet needs to be software segmented by core stack.
2319  */
2320 static bool idpf_chk_tso_segment(const struct sk_buff *skb,
2321 				 unsigned int max_bufs)
2322 {
2323 	const struct skb_shared_info *shinfo = skb_shinfo(skb);
2324 	const skb_frag_t *frag, *stale;
2325 	int nr_frags, sum;
2326 
2327 	/* no need to check if number of frags is less than max_bufs - 1 */
2328 	nr_frags = shinfo->nr_frags;
2329 	if (nr_frags < (max_bufs - 1))
2330 		return false;
2331 
2332 	/* We need to walk through the list and validate that each group
2333 	 * of max_bufs-2 fragments totals at least gso_size.
2334 	 */
2335 	nr_frags -= max_bufs - 2;
2336 	frag = &shinfo->frags[0];
2337 
2338 	/* Initialize size to the negative value of gso_size minus 1.  We use
2339 	 * this as the worst case scenario in which the frag ahead of us only
2340 	 * provides one byte which is why we are limited to max_bufs-2
2341 	 * descriptors for a single transmit as the header and previous
2342 	 * fragment are already consuming 2 descriptors.
2343 	 */
2344 	sum = 1 - shinfo->gso_size;
2345 
2346 	/* Add size of frags 0 through 4 to create our initial sum */
2347 	sum += skb_frag_size(frag++);
2348 	sum += skb_frag_size(frag++);
2349 	sum += skb_frag_size(frag++);
2350 	sum += skb_frag_size(frag++);
2351 	sum += skb_frag_size(frag++);
2352 
2353 	/* Walk through fragments adding latest fragment, testing it, and
2354 	 * then removing stale fragments from the sum.
2355 	 */
2356 	for (stale = &shinfo->frags[0];; stale++) {
2357 		int stale_size = skb_frag_size(stale);
2358 
2359 		sum += skb_frag_size(frag++);
2360 
2361 		/* The stale fragment may present us with a smaller
2362 		 * descriptor than the actual fragment size. To account
2363 		 * for that we need to remove all the data on the front and
2364 		 * figure out what the remainder would be in the last
2365 		 * descriptor associated with the fragment.
2366 		 */
2367 		if (stale_size > IDPF_TX_MAX_DESC_DATA) {
2368 			int align_pad = -(skb_frag_off(stale)) &
2369 					(IDPF_TX_MAX_READ_REQ_SIZE - 1);
2370 
2371 			sum -= align_pad;
2372 			stale_size -= align_pad;
2373 
2374 			do {
2375 				sum -= IDPF_TX_MAX_DESC_DATA_ALIGNED;
2376 				stale_size -= IDPF_TX_MAX_DESC_DATA_ALIGNED;
2377 			} while (stale_size > IDPF_TX_MAX_DESC_DATA);
2378 		}
2379 
2380 		/* if sum is negative we failed to make sufficient progress */
2381 		if (sum < 0)
2382 			return true;
2383 
2384 		if (!nr_frags--)
2385 			break;
2386 
2387 		sum -= stale_size;
2388 	}
2389 
2390 	return false;
2391 }
2392 
2393 /**
2394  * idpf_features_check - Validate packet conforms to limits
2395  * @skb: skb buffer
2396  * @netdev: This port's netdev
2397  * @features: Offload features that the stack believes apply
2398  */
2399 static netdev_features_t idpf_features_check(struct sk_buff *skb,
2400 					     struct net_device *netdev,
2401 					     netdev_features_t features)
2402 {
2403 	struct idpf_netdev_priv *np = netdev_priv(netdev);
2404 	u16 max_tx_hdr_size = np->max_tx_hdr_size;
2405 	size_t len;
2406 
2407 	/* No point in doing any of this if neither checksum nor GSO are
2408 	 * being requested for this frame.  We can rule out both by just
2409 	 * checking for CHECKSUM_PARTIAL
2410 	 */
2411 	if (skb->ip_summed != CHECKSUM_PARTIAL)
2412 		return features;
2413 
2414 	if (skb_is_gso(skb)) {
2415 		/* We cannot support GSO if the MSS is going to be less than
2416 		 * 88 bytes. If it is then we need to drop support for GSO.
2417 		 */
2418 		if (skb_shinfo(skb)->gso_size < IDPF_TX_TSO_MIN_MSS)
2419 			features &= ~NETIF_F_GSO_MASK;
2420 		else if (idpf_chk_tso_segment(skb, np->tx_max_bufs))
2421 			features &= ~NETIF_F_GSO_MASK;
2422 	}
2423 
2424 	/* Ensure MACLEN is <= 126 bytes (63 words) and not an odd size */
2425 	len = skb_network_offset(skb);
2426 	if (unlikely(len & ~(126)))
2427 		goto unsupported;
2428 
2429 	len = skb_network_header_len(skb);
2430 	if (unlikely(len > max_tx_hdr_size))
2431 		goto unsupported;
2432 
2433 	if (!skb->encapsulation)
2434 		return features;
2435 
2436 	/* L4TUNLEN can support 127 words */
2437 	len = skb_inner_network_header(skb) - skb_transport_header(skb);
2438 	if (unlikely(len & ~(127 * 2)))
2439 		goto unsupported;
2440 
2441 	/* IPLEN can support at most 127 dwords */
2442 	len = skb_inner_network_header_len(skb);
2443 	if (unlikely(len > max_tx_hdr_size))
2444 		goto unsupported;
2445 
2446 	/* No need to validate L4LEN as TCP is the only protocol with a
2447 	 * a flexible value and we support all possible values supported
2448 	 * by TCP, which is at most 15 dwords
2449 	 */
2450 
2451 	return features;
2452 
2453 unsupported:
2454 	return features & ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK);
2455 }
2456 
2457 /**
2458  * idpf_set_mac - NDO callback to set port mac address
2459  * @netdev: network interface device structure
2460  * @p: pointer to an address structure
2461  *
2462  * Returns 0 on success, negative on failure
2463  **/
2464 static int idpf_set_mac(struct net_device *netdev, void *p)
2465 {
2466 	struct idpf_netdev_priv *np = netdev_priv(netdev);
2467 	struct idpf_vport_config *vport_config;
2468 	struct sockaddr *addr = p;
2469 	u8 old_mac_addr[ETH_ALEN];
2470 	struct idpf_vport *vport;
2471 	int err = 0;
2472 
2473 	idpf_vport_ctrl_lock(netdev);
2474 	vport = idpf_netdev_to_vport(netdev);
2475 
2476 	if (!idpf_is_cap_ena(vport->adapter, IDPF_OTHER_CAPS,
2477 			     VIRTCHNL2_CAP_MACFILTER)) {
2478 		dev_info(&vport->adapter->pdev->dev, "Setting MAC address is not supported\n");
2479 		err = -EOPNOTSUPP;
2480 		goto unlock_mutex;
2481 	}
2482 
2483 	if (!is_valid_ether_addr(addr->sa_data)) {
2484 		dev_info(&vport->adapter->pdev->dev, "Invalid MAC address: %pM\n",
2485 			 addr->sa_data);
2486 		err = -EADDRNOTAVAIL;
2487 		goto unlock_mutex;
2488 	}
2489 
2490 	if (ether_addr_equal(netdev->dev_addr, addr->sa_data))
2491 		goto unlock_mutex;
2492 
2493 	ether_addr_copy(old_mac_addr, vport->default_mac_addr);
2494 	ether_addr_copy(vport->default_mac_addr, addr->sa_data);
2495 	vport_config = vport->adapter->vport_config[vport->idx];
2496 	err = idpf_add_mac_filter(vport, np, addr->sa_data, false);
2497 	if (err) {
2498 		__idpf_del_mac_filter(vport_config, addr->sa_data);
2499 		ether_addr_copy(vport->default_mac_addr, netdev->dev_addr);
2500 		goto unlock_mutex;
2501 	}
2502 
2503 	if (is_valid_ether_addr(old_mac_addr))
2504 		__idpf_del_mac_filter(vport_config, old_mac_addr);
2505 
2506 	eth_hw_addr_set(netdev, addr->sa_data);
2507 
2508 unlock_mutex:
2509 	idpf_vport_ctrl_unlock(netdev);
2510 
2511 	return err;
2512 }
2513 
2514 /**
2515  * idpf_alloc_dma_mem - Allocate dma memory
2516  * @hw: pointer to hw struct
2517  * @mem: pointer to dma_mem struct
2518  * @size: size of the memory to allocate
2519  */
2520 void *idpf_alloc_dma_mem(struct idpf_hw *hw, struct idpf_dma_mem *mem, u64 size)
2521 {
2522 	struct idpf_adapter *adapter = hw->back;
2523 	size_t sz = ALIGN(size, 4096);
2524 
2525 	/* The control queue resources are freed under a spinlock, contiguous
2526 	 * pages will avoid IOMMU remapping and the use vmap (and vunmap in
2527 	 * dma_free_*() path.
2528 	 */
2529 	mem->va = dma_alloc_attrs(&adapter->pdev->dev, sz, &mem->pa,
2530 				  GFP_KERNEL, DMA_ATTR_FORCE_CONTIGUOUS);
2531 	mem->size = sz;
2532 
2533 	return mem->va;
2534 }
2535 
2536 /**
2537  * idpf_free_dma_mem - Free the allocated dma memory
2538  * @hw: pointer to hw struct
2539  * @mem: pointer to dma_mem struct
2540  */
2541 void idpf_free_dma_mem(struct idpf_hw *hw, struct idpf_dma_mem *mem)
2542 {
2543 	struct idpf_adapter *adapter = hw->back;
2544 
2545 	dma_free_attrs(&adapter->pdev->dev, mem->size,
2546 		       mem->va, mem->pa, DMA_ATTR_FORCE_CONTIGUOUS);
2547 	mem->size = 0;
2548 	mem->va = NULL;
2549 	mem->pa = 0;
2550 }
2551 
2552 static int idpf_hwtstamp_set(struct net_device *netdev,
2553 			     struct kernel_hwtstamp_config *config,
2554 			     struct netlink_ext_ack *extack)
2555 {
2556 	struct idpf_vport *vport;
2557 	int err;
2558 
2559 	idpf_vport_ctrl_lock(netdev);
2560 	vport = idpf_netdev_to_vport(netdev);
2561 
2562 	if (!vport->link_up) {
2563 		idpf_vport_ctrl_unlock(netdev);
2564 		return -EPERM;
2565 	}
2566 
2567 	if (!idpf_ptp_is_vport_tx_tstamp_ena(vport) &&
2568 	    !idpf_ptp_is_vport_rx_tstamp_ena(vport)) {
2569 		idpf_vport_ctrl_unlock(netdev);
2570 		return -EOPNOTSUPP;
2571 	}
2572 
2573 	err = idpf_ptp_set_timestamp_mode(vport, config);
2574 
2575 	idpf_vport_ctrl_unlock(netdev);
2576 
2577 	return err;
2578 }
2579 
2580 static int idpf_hwtstamp_get(struct net_device *netdev,
2581 			     struct kernel_hwtstamp_config *config)
2582 {
2583 	struct idpf_vport *vport;
2584 
2585 	idpf_vport_ctrl_lock(netdev);
2586 	vport = idpf_netdev_to_vport(netdev);
2587 
2588 	if (!vport->link_up) {
2589 		idpf_vport_ctrl_unlock(netdev);
2590 		return -EPERM;
2591 	}
2592 
2593 	if (!idpf_ptp_is_vport_tx_tstamp_ena(vport) &&
2594 	    !idpf_ptp_is_vport_rx_tstamp_ena(vport)) {
2595 		idpf_vport_ctrl_unlock(netdev);
2596 		return 0;
2597 	}
2598 
2599 	*config = vport->tstamp_config;
2600 
2601 	idpf_vport_ctrl_unlock(netdev);
2602 
2603 	return 0;
2604 }
2605 
2606 static const struct net_device_ops idpf_netdev_ops = {
2607 	.ndo_open = idpf_open,
2608 	.ndo_stop = idpf_stop,
2609 	.ndo_start_xmit = idpf_tx_start,
2610 	.ndo_features_check = idpf_features_check,
2611 	.ndo_set_rx_mode = idpf_set_rx_mode,
2612 	.ndo_validate_addr = eth_validate_addr,
2613 	.ndo_set_mac_address = idpf_set_mac,
2614 	.ndo_change_mtu = idpf_change_mtu,
2615 	.ndo_get_stats64 = idpf_get_stats64,
2616 	.ndo_set_features = idpf_set_features,
2617 	.ndo_tx_timeout = idpf_tx_timeout,
2618 	.ndo_hwtstamp_get = idpf_hwtstamp_get,
2619 	.ndo_hwtstamp_set = idpf_hwtstamp_set,
2620 	.ndo_bpf = idpf_xdp,
2621 	.ndo_xdp_xmit = idpf_xdp_xmit,
2622 	.ndo_xsk_wakeup = idpf_xsk_wakeup,
2623 };
2624