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