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