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