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