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