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