xref: /linux/drivers/net/ethernet/intel/idpf/idpf_virtchnl.c (revision 4e887471e8e3a513607495d18333c44f59a82c5a)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /* Copyright (C) 2023 Intel Corporation */
3 
4 #include "idpf.h"
5 #include "idpf_virtchnl.h"
6 
7 #define IDPF_VC_XN_MIN_TIMEOUT_MSEC	2000
8 #define IDPF_VC_XN_DEFAULT_TIMEOUT_MSEC	(60 * 1000)
9 #define IDPF_VC_XN_IDX_M		GENMASK(7, 0)
10 #define IDPF_VC_XN_SALT_M		GENMASK(15, 8)
11 #define IDPF_VC_XN_RING_LEN		U8_MAX
12 
13 /**
14  * enum idpf_vc_xn_state - Virtchnl transaction status
15  * @IDPF_VC_XN_IDLE: not expecting a reply, ready to be used
16  * @IDPF_VC_XN_WAITING: expecting a reply, not yet received
17  * @IDPF_VC_XN_COMPLETED_SUCCESS: a reply was expected and received,
18  *				  buffer updated
19  * @IDPF_VC_XN_COMPLETED_FAILED: a reply was expected and received, but there
20  *				 was an error, buffer not updated
21  * @IDPF_VC_XN_SHUTDOWN: transaction object cannot be used, VC torn down
22  * @IDPF_VC_XN_ASYNC: transaction sent asynchronously and doesn't have the
23  *		      return context; a callback may be provided to handle
24  *		      return
25  */
26 enum idpf_vc_xn_state {
27 	IDPF_VC_XN_IDLE = 1,
28 	IDPF_VC_XN_WAITING,
29 	IDPF_VC_XN_COMPLETED_SUCCESS,
30 	IDPF_VC_XN_COMPLETED_FAILED,
31 	IDPF_VC_XN_SHUTDOWN,
32 	IDPF_VC_XN_ASYNC,
33 };
34 
35 struct idpf_vc_xn;
36 /* Callback for asynchronous messages */
37 typedef int (*async_vc_cb) (struct idpf_adapter *, struct idpf_vc_xn *,
38 			    const struct idpf_ctlq_msg *);
39 
40 /**
41  * struct idpf_vc_xn - Data structure representing virtchnl transactions
42  * @completed: virtchnl event loop uses that to signal when a reply is
43  *	       available, uses kernel completion API
44  * @state: virtchnl event loop stores the data below, protected by the
45  *	   completion's lock.
46  * @reply_sz: Original size of reply, may be > reply_buf.iov_len; it will be
47  *	      truncated on its way to the receiver thread according to
48  *	      reply_buf.iov_len.
49  * @reply: Reference to the buffer(s) where the reply data should be written
50  *	   to. May be 0-length (then NULL address permitted) if the reply data
51  *	   should be ignored.
52  * @async_handler: if sent asynchronously, a callback can be provided to handle
53  *		   the reply when it's received
54  * @vc_op: corresponding opcode sent with this transaction
55  * @idx: index used as retrieval on reply receive, used for cookie
56  * @salt: changed every message to make unique, used for cookie
57  */
58 struct idpf_vc_xn {
59 	struct completion completed;
60 	enum idpf_vc_xn_state state;
61 	size_t reply_sz;
62 	struct kvec reply;
63 	async_vc_cb async_handler;
64 	u32 vc_op;
65 	u8 idx;
66 	u8 salt;
67 };
68 
69 /**
70  * struct idpf_vc_xn_params - Parameters for executing transaction
71  * @send_buf: kvec for send buffer
72  * @recv_buf: kvec for recv buffer, may be NULL, must then have zero length
73  * @timeout_ms: timeout to wait for reply
74  * @async: send message asynchronously, will not wait on completion
75  * @async_handler: If sent asynchronously, optional callback handler. The user
76  *		   must be careful when using async handlers as the memory for
77  *		   the recv_buf _cannot_ be on stack if this is async.
78  * @vc_op: virtchnl op to send
79  */
80 struct idpf_vc_xn_params {
81 	struct kvec send_buf;
82 	struct kvec recv_buf;
83 	int timeout_ms;
84 	bool async;
85 	async_vc_cb async_handler;
86 	u32 vc_op;
87 };
88 
89 /**
90  * struct idpf_vc_xn_manager - Manager for tracking transactions
91  * @ring: backing and lookup for transactions
92  * @free_xn_bm: bitmap for free transactions
93  * @xn_bm_lock: make bitmap access synchronous where necessary
94  * @salt: used to make cookie unique every message
95  */
96 struct idpf_vc_xn_manager {
97 	struct idpf_vc_xn ring[IDPF_VC_XN_RING_LEN];
98 	DECLARE_BITMAP(free_xn_bm, IDPF_VC_XN_RING_LEN);
99 	spinlock_t xn_bm_lock;
100 	u8 salt;
101 };
102 
103 /**
104  * idpf_vid_to_vport - Translate vport id to vport pointer
105  * @adapter: private data struct
106  * @v_id: vport id to translate
107  *
108  * Returns vport matching v_id, NULL if not found.
109  */
110 static
111 struct idpf_vport *idpf_vid_to_vport(struct idpf_adapter *adapter, u32 v_id)
112 {
113 	u16 num_max_vports = idpf_get_max_vports(adapter);
114 	int i;
115 
116 	for (i = 0; i < num_max_vports; i++)
117 		if (adapter->vport_ids[i] == v_id)
118 			return adapter->vports[i];
119 
120 	return NULL;
121 }
122 
123 /**
124  * idpf_handle_event_link - Handle link event message
125  * @adapter: private data struct
126  * @v2e: virtchnl event message
127  */
128 static void idpf_handle_event_link(struct idpf_adapter *adapter,
129 				   const struct virtchnl2_event *v2e)
130 {
131 	struct idpf_netdev_priv *np;
132 	struct idpf_vport *vport;
133 
134 	vport = idpf_vid_to_vport(adapter, le32_to_cpu(v2e->vport_id));
135 	if (!vport) {
136 		dev_err_ratelimited(&adapter->pdev->dev, "Failed to find vport_id %d for link event\n",
137 				    v2e->vport_id);
138 		return;
139 	}
140 	np = netdev_priv(vport->netdev);
141 
142 	vport->link_speed_mbps = le32_to_cpu(v2e->link_speed);
143 
144 	if (vport->link_up == v2e->link_status)
145 		return;
146 
147 	vport->link_up = v2e->link_status;
148 
149 	if (np->state != __IDPF_VPORT_UP)
150 		return;
151 
152 	if (vport->link_up) {
153 		netif_tx_start_all_queues(vport->netdev);
154 		netif_carrier_on(vport->netdev);
155 	} else {
156 		netif_tx_stop_all_queues(vport->netdev);
157 		netif_carrier_off(vport->netdev);
158 	}
159 }
160 
161 /**
162  * idpf_recv_event_msg - Receive virtchnl event message
163  * @adapter: Driver specific private structure
164  * @ctlq_msg: message to copy from
165  *
166  * Receive virtchnl event message
167  */
168 static void idpf_recv_event_msg(struct idpf_adapter *adapter,
169 				struct idpf_ctlq_msg *ctlq_msg)
170 {
171 	int payload_size = ctlq_msg->ctx.indirect.payload->size;
172 	struct virtchnl2_event *v2e;
173 	u32 event;
174 
175 	if (payload_size < sizeof(*v2e)) {
176 		dev_err_ratelimited(&adapter->pdev->dev, "Failed to receive valid payload for event msg (op %d len %d)\n",
177 				    ctlq_msg->cookie.mbx.chnl_opcode,
178 				    payload_size);
179 		return;
180 	}
181 
182 	v2e = (struct virtchnl2_event *)ctlq_msg->ctx.indirect.payload->va;
183 	event = le32_to_cpu(v2e->event);
184 
185 	switch (event) {
186 	case VIRTCHNL2_EVENT_LINK_CHANGE:
187 		idpf_handle_event_link(adapter, v2e);
188 		return;
189 	default:
190 		dev_err(&adapter->pdev->dev,
191 			"Unknown event %d from PF\n", event);
192 		break;
193 	}
194 }
195 
196 /**
197  * idpf_mb_clean - Reclaim the send mailbox queue entries
198  * @adapter: Driver specific private structure
199  *
200  * Reclaim the send mailbox queue entries to be used to send further messages
201  *
202  * Returns 0 on success, negative on failure
203  */
204 static int idpf_mb_clean(struct idpf_adapter *adapter)
205 {
206 	u16 i, num_q_msg = IDPF_DFLT_MBX_Q_LEN;
207 	struct idpf_ctlq_msg **q_msg;
208 	struct idpf_dma_mem *dma_mem;
209 	int err;
210 
211 	q_msg = kcalloc(num_q_msg, sizeof(struct idpf_ctlq_msg *), GFP_ATOMIC);
212 	if (!q_msg)
213 		return -ENOMEM;
214 
215 	err = idpf_ctlq_clean_sq(adapter->hw.asq, &num_q_msg, q_msg);
216 	if (err)
217 		goto err_kfree;
218 
219 	for (i = 0; i < num_q_msg; i++) {
220 		if (!q_msg[i])
221 			continue;
222 		dma_mem = q_msg[i]->ctx.indirect.payload;
223 		if (dma_mem)
224 			dma_free_coherent(&adapter->pdev->dev, dma_mem->size,
225 					  dma_mem->va, dma_mem->pa);
226 		kfree(q_msg[i]);
227 		kfree(dma_mem);
228 	}
229 
230 err_kfree:
231 	kfree(q_msg);
232 
233 	return err;
234 }
235 
236 /**
237  * idpf_send_mb_msg - Send message over mailbox
238  * @adapter: Driver specific private structure
239  * @op: virtchnl opcode
240  * @msg_size: size of the payload
241  * @msg: pointer to buffer holding the payload
242  * @cookie: unique SW generated cookie per message
243  *
244  * Will prepare the control queue message and initiates the send api
245  *
246  * Returns 0 on success, negative on failure
247  */
248 int idpf_send_mb_msg(struct idpf_adapter *adapter, u32 op,
249 		     u16 msg_size, u8 *msg, u16 cookie)
250 {
251 	struct idpf_ctlq_msg *ctlq_msg;
252 	struct idpf_dma_mem *dma_mem;
253 	int err;
254 
255 	/* If we are here and a reset is detected nothing much can be
256 	 * done. This thread should silently abort and expected to
257 	 * be corrected with a new run either by user or driver
258 	 * flows after reset
259 	 */
260 	if (idpf_is_reset_detected(adapter))
261 		return 0;
262 
263 	err = idpf_mb_clean(adapter);
264 	if (err)
265 		return err;
266 
267 	ctlq_msg = kzalloc(sizeof(*ctlq_msg), GFP_ATOMIC);
268 	if (!ctlq_msg)
269 		return -ENOMEM;
270 
271 	dma_mem = kzalloc(sizeof(*dma_mem), GFP_ATOMIC);
272 	if (!dma_mem) {
273 		err = -ENOMEM;
274 		goto dma_mem_error;
275 	}
276 
277 	ctlq_msg->opcode = idpf_mbq_opc_send_msg_to_cp;
278 	ctlq_msg->func_id = 0;
279 	ctlq_msg->data_len = msg_size;
280 	ctlq_msg->cookie.mbx.chnl_opcode = op;
281 	ctlq_msg->cookie.mbx.chnl_retval = 0;
282 	dma_mem->size = IDPF_CTLQ_MAX_BUF_LEN;
283 	dma_mem->va = dma_alloc_coherent(&adapter->pdev->dev, dma_mem->size,
284 					 &dma_mem->pa, GFP_ATOMIC);
285 	if (!dma_mem->va) {
286 		err = -ENOMEM;
287 		goto dma_alloc_error;
288 	}
289 
290 	/* It's possible we're just sending an opcode but no buffer */
291 	if (msg && msg_size)
292 		memcpy(dma_mem->va, msg, msg_size);
293 	ctlq_msg->ctx.indirect.payload = dma_mem;
294 	ctlq_msg->ctx.sw_cookie.data = cookie;
295 
296 	err = idpf_ctlq_send(&adapter->hw, adapter->hw.asq, 1, ctlq_msg);
297 	if (err)
298 		goto send_error;
299 
300 	return 0;
301 
302 send_error:
303 	dma_free_coherent(&adapter->pdev->dev, dma_mem->size, dma_mem->va,
304 			  dma_mem->pa);
305 dma_alloc_error:
306 	kfree(dma_mem);
307 dma_mem_error:
308 	kfree(ctlq_msg);
309 
310 	return err;
311 }
312 
313 /* API for virtchnl "transaction" support ("xn" for short).
314  *
315  * We are reusing the completion lock to serialize the accesses to the
316  * transaction state for simplicity, but it could be its own separate synchro
317  * as well. For now, this API is only used from within a workqueue context;
318  * raw_spin_lock() is enough.
319  */
320 /**
321  * idpf_vc_xn_lock - Request exclusive access to vc transaction
322  * @xn: struct idpf_vc_xn* to access
323  */
324 #define idpf_vc_xn_lock(xn)			\
325 	raw_spin_lock(&(xn)->completed.wait.lock)
326 
327 /**
328  * idpf_vc_xn_unlock - Release exclusive access to vc transaction
329  * @xn: struct idpf_vc_xn* to access
330  */
331 #define idpf_vc_xn_unlock(xn)		\
332 	raw_spin_unlock(&(xn)->completed.wait.lock)
333 
334 /**
335  * idpf_vc_xn_release_bufs - Release reference to reply buffer(s) and
336  * reset the transaction state.
337  * @xn: struct idpf_vc_xn to update
338  */
339 static void idpf_vc_xn_release_bufs(struct idpf_vc_xn *xn)
340 {
341 	xn->reply.iov_base = NULL;
342 	xn->reply.iov_len = 0;
343 
344 	if (xn->state != IDPF_VC_XN_SHUTDOWN)
345 		xn->state = IDPF_VC_XN_IDLE;
346 }
347 
348 /**
349  * idpf_vc_xn_init - Initialize virtchnl transaction object
350  * @vcxn_mngr: pointer to vc transaction manager struct
351  */
352 static void idpf_vc_xn_init(struct idpf_vc_xn_manager *vcxn_mngr)
353 {
354 	int i;
355 
356 	spin_lock_init(&vcxn_mngr->xn_bm_lock);
357 
358 	for (i = 0; i < ARRAY_SIZE(vcxn_mngr->ring); i++) {
359 		struct idpf_vc_xn *xn = &vcxn_mngr->ring[i];
360 
361 		xn->state = IDPF_VC_XN_IDLE;
362 		xn->idx = i;
363 		idpf_vc_xn_release_bufs(xn);
364 		init_completion(&xn->completed);
365 	}
366 
367 	bitmap_fill(vcxn_mngr->free_xn_bm, IDPF_VC_XN_RING_LEN);
368 }
369 
370 /**
371  * idpf_vc_xn_shutdown - Uninitialize virtchnl transaction object
372  * @vcxn_mngr: pointer to vc transaction manager struct
373  *
374  * All waiting threads will be woken-up and their transaction aborted. Further
375  * operations on that object will fail.
376  */
377 static void idpf_vc_xn_shutdown(struct idpf_vc_xn_manager *vcxn_mngr)
378 {
379 	int i;
380 
381 	spin_lock_bh(&vcxn_mngr->xn_bm_lock);
382 	bitmap_zero(vcxn_mngr->free_xn_bm, IDPF_VC_XN_RING_LEN);
383 	spin_unlock_bh(&vcxn_mngr->xn_bm_lock);
384 
385 	for (i = 0; i < ARRAY_SIZE(vcxn_mngr->ring); i++) {
386 		struct idpf_vc_xn *xn = &vcxn_mngr->ring[i];
387 
388 		idpf_vc_xn_lock(xn);
389 		xn->state = IDPF_VC_XN_SHUTDOWN;
390 		idpf_vc_xn_release_bufs(xn);
391 		idpf_vc_xn_unlock(xn);
392 		complete_all(&xn->completed);
393 	}
394 }
395 
396 /**
397  * idpf_vc_xn_pop_free - Pop a free transaction from free list
398  * @vcxn_mngr: transaction manager to pop from
399  *
400  * Returns NULL if no free transactions
401  */
402 static
403 struct idpf_vc_xn *idpf_vc_xn_pop_free(struct idpf_vc_xn_manager *vcxn_mngr)
404 {
405 	struct idpf_vc_xn *xn = NULL;
406 	unsigned long free_idx;
407 
408 	spin_lock_bh(&vcxn_mngr->xn_bm_lock);
409 	free_idx = find_first_bit(vcxn_mngr->free_xn_bm, IDPF_VC_XN_RING_LEN);
410 	if (free_idx == IDPF_VC_XN_RING_LEN)
411 		goto do_unlock;
412 
413 	clear_bit(free_idx, vcxn_mngr->free_xn_bm);
414 	xn = &vcxn_mngr->ring[free_idx];
415 	xn->salt = vcxn_mngr->salt++;
416 
417 do_unlock:
418 	spin_unlock_bh(&vcxn_mngr->xn_bm_lock);
419 
420 	return xn;
421 }
422 
423 /**
424  * idpf_vc_xn_push_free - Push a free transaction to free list
425  * @vcxn_mngr: transaction manager to push to
426  * @xn: transaction to push
427  */
428 static void idpf_vc_xn_push_free(struct idpf_vc_xn_manager *vcxn_mngr,
429 				 struct idpf_vc_xn *xn)
430 {
431 	idpf_vc_xn_release_bufs(xn);
432 	set_bit(xn->idx, vcxn_mngr->free_xn_bm);
433 }
434 
435 /**
436  * idpf_vc_xn_exec - Perform a send/recv virtchnl transaction
437  * @adapter: driver specific private structure with vcxn_mngr
438  * @params: parameters for this particular transaction including
439  *   -vc_op: virtchannel operation to send
440  *   -send_buf: kvec iov for send buf and len
441  *   -recv_buf: kvec iov for recv buf and len (ignored if NULL)
442  *   -timeout_ms: timeout waiting for a reply (milliseconds)
443  *   -async: don't wait for message reply, will lose caller context
444  *   -async_handler: callback to handle async replies
445  *
446  * @returns >= 0 for success, the size of the initial reply (may or may not be
447  * >= @recv_buf.iov_len, but we never overflow @@recv_buf_iov_base). < 0 for
448  * error.
449  */
450 static ssize_t idpf_vc_xn_exec(struct idpf_adapter *adapter,
451 			       const struct idpf_vc_xn_params *params)
452 {
453 	const struct kvec *send_buf = &params->send_buf;
454 	struct idpf_vc_xn *xn;
455 	ssize_t retval;
456 	u16 cookie;
457 
458 	xn = idpf_vc_xn_pop_free(adapter->vcxn_mngr);
459 	/* no free transactions available */
460 	if (!xn)
461 		return -ENOSPC;
462 
463 	idpf_vc_xn_lock(xn);
464 	if (xn->state == IDPF_VC_XN_SHUTDOWN) {
465 		retval = -ENXIO;
466 		goto only_unlock;
467 	} else if (xn->state != IDPF_VC_XN_IDLE) {
468 		/* We're just going to clobber this transaction even though
469 		 * it's not IDLE. If we don't reuse it we could theoretically
470 		 * eventually leak all the free transactions and not be able to
471 		 * send any messages. At least this way we make an attempt to
472 		 * remain functional even though something really bad is
473 		 * happening that's corrupting what was supposed to be free
474 		 * transactions.
475 		 */
476 		WARN_ONCE(1, "There should only be idle transactions in free list (idx %d op %d)\n",
477 			  xn->idx, xn->vc_op);
478 	}
479 
480 	xn->reply = params->recv_buf;
481 	xn->reply_sz = 0;
482 	xn->state = params->async ? IDPF_VC_XN_ASYNC : IDPF_VC_XN_WAITING;
483 	xn->vc_op = params->vc_op;
484 	xn->async_handler = params->async_handler;
485 	idpf_vc_xn_unlock(xn);
486 
487 	if (!params->async)
488 		reinit_completion(&xn->completed);
489 	cookie = FIELD_PREP(IDPF_VC_XN_SALT_M, xn->salt) |
490 		 FIELD_PREP(IDPF_VC_XN_IDX_M, xn->idx);
491 
492 	retval = idpf_send_mb_msg(adapter, params->vc_op,
493 				  send_buf->iov_len, send_buf->iov_base,
494 				  cookie);
495 	if (retval) {
496 		idpf_vc_xn_lock(xn);
497 		goto release_and_unlock;
498 	}
499 
500 	if (params->async)
501 		return 0;
502 
503 	wait_for_completion_timeout(&xn->completed,
504 				    msecs_to_jiffies(params->timeout_ms));
505 
506 	/* No need to check the return value; we check the final state of the
507 	 * transaction below. It's possible the transaction actually gets more
508 	 * timeout than specified if we get preempted here but after
509 	 * wait_for_completion_timeout returns. This should be non-issue
510 	 * however.
511 	 */
512 	idpf_vc_xn_lock(xn);
513 	switch (xn->state) {
514 	case IDPF_VC_XN_SHUTDOWN:
515 		retval = -ENXIO;
516 		goto only_unlock;
517 	case IDPF_VC_XN_WAITING:
518 		dev_notice_ratelimited(&adapter->pdev->dev, "Transaction timed-out (op %d, %dms)\n",
519 				       params->vc_op, params->timeout_ms);
520 		retval = -ETIME;
521 		break;
522 	case IDPF_VC_XN_COMPLETED_SUCCESS:
523 		retval = xn->reply_sz;
524 		break;
525 	case IDPF_VC_XN_COMPLETED_FAILED:
526 		dev_notice_ratelimited(&adapter->pdev->dev, "Transaction failed (op %d)\n",
527 				       params->vc_op);
528 		retval = -EIO;
529 		break;
530 	default:
531 		/* Invalid state. */
532 		WARN_ON_ONCE(1);
533 		retval = -EIO;
534 		break;
535 	}
536 
537 release_and_unlock:
538 	idpf_vc_xn_push_free(adapter->vcxn_mngr, xn);
539 	/* If we receive a VC reply after here, it will be dropped. */
540 only_unlock:
541 	idpf_vc_xn_unlock(xn);
542 
543 	return retval;
544 }
545 
546 /**
547  * idpf_vc_xn_forward_async - Handle async reply receives
548  * @adapter: private data struct
549  * @xn: transaction to handle
550  * @ctlq_msg: corresponding ctlq_msg
551  *
552  * For async sends we're going to lose the caller's context so, if an
553  * async_handler was provided, it can deal with the reply, otherwise we'll just
554  * check and report if there is an error.
555  */
556 static int
557 idpf_vc_xn_forward_async(struct idpf_adapter *adapter, struct idpf_vc_xn *xn,
558 			 const struct idpf_ctlq_msg *ctlq_msg)
559 {
560 	int err = 0;
561 
562 	if (ctlq_msg->cookie.mbx.chnl_opcode != xn->vc_op) {
563 		dev_err_ratelimited(&adapter->pdev->dev, "Async message opcode does not match transaction opcode (msg: %d) (xn: %d)\n",
564 				    ctlq_msg->cookie.mbx.chnl_opcode, xn->vc_op);
565 		xn->reply_sz = 0;
566 		err = -EINVAL;
567 		goto release_bufs;
568 	}
569 
570 	if (xn->async_handler) {
571 		err = xn->async_handler(adapter, xn, ctlq_msg);
572 		goto release_bufs;
573 	}
574 
575 	if (ctlq_msg->cookie.mbx.chnl_retval) {
576 		xn->reply_sz = 0;
577 		dev_err_ratelimited(&adapter->pdev->dev, "Async message failure (op %d)\n",
578 				    ctlq_msg->cookie.mbx.chnl_opcode);
579 		err = -EINVAL;
580 	}
581 
582 release_bufs:
583 	idpf_vc_xn_push_free(adapter->vcxn_mngr, xn);
584 
585 	return err;
586 }
587 
588 /**
589  * idpf_vc_xn_forward_reply - copy a reply back to receiving thread
590  * @adapter: driver specific private structure with vcxn_mngr
591  * @ctlq_msg: controlq message to send back to receiving thread
592  */
593 static int
594 idpf_vc_xn_forward_reply(struct idpf_adapter *adapter,
595 			 const struct idpf_ctlq_msg *ctlq_msg)
596 {
597 	const void *payload = NULL;
598 	size_t payload_size = 0;
599 	struct idpf_vc_xn *xn;
600 	u16 msg_info;
601 	int err = 0;
602 	u16 xn_idx;
603 	u16 salt;
604 
605 	msg_info = ctlq_msg->ctx.sw_cookie.data;
606 	xn_idx = FIELD_GET(IDPF_VC_XN_IDX_M, msg_info);
607 	if (xn_idx >= ARRAY_SIZE(adapter->vcxn_mngr->ring)) {
608 		dev_err_ratelimited(&adapter->pdev->dev, "Out of bounds cookie received: %02x\n",
609 				    xn_idx);
610 		return -EINVAL;
611 	}
612 	xn = &adapter->vcxn_mngr->ring[xn_idx];
613 	salt = FIELD_GET(IDPF_VC_XN_SALT_M, msg_info);
614 	if (xn->salt != salt) {
615 		dev_err_ratelimited(&adapter->pdev->dev, "Transaction salt does not match (%02x != %02x)\n",
616 				    xn->salt, salt);
617 		return -EINVAL;
618 	}
619 
620 	idpf_vc_xn_lock(xn);
621 	switch (xn->state) {
622 	case IDPF_VC_XN_WAITING:
623 		/* success */
624 		break;
625 	case IDPF_VC_XN_IDLE:
626 		dev_err_ratelimited(&adapter->pdev->dev, "Unexpected or belated VC reply (op %d)\n",
627 				    ctlq_msg->cookie.mbx.chnl_opcode);
628 		err = -EINVAL;
629 		goto out_unlock;
630 	case IDPF_VC_XN_SHUTDOWN:
631 		/* ENXIO is a bit special here as the recv msg loop uses that
632 		 * know if it should stop trying to clean the ring if we lost
633 		 * the virtchnl. We need to stop playing with registers and
634 		 * yield.
635 		 */
636 		err = -ENXIO;
637 		goto out_unlock;
638 	case IDPF_VC_XN_ASYNC:
639 		err = idpf_vc_xn_forward_async(adapter, xn, ctlq_msg);
640 		idpf_vc_xn_unlock(xn);
641 		return err;
642 	default:
643 		dev_err_ratelimited(&adapter->pdev->dev, "Overwriting VC reply (op %d)\n",
644 				    ctlq_msg->cookie.mbx.chnl_opcode);
645 		err = -EBUSY;
646 		goto out_unlock;
647 	}
648 
649 	if (ctlq_msg->cookie.mbx.chnl_opcode != xn->vc_op) {
650 		dev_err_ratelimited(&adapter->pdev->dev, "Message opcode does not match transaction opcode (msg: %d) (xn: %d)\n",
651 				    ctlq_msg->cookie.mbx.chnl_opcode, xn->vc_op);
652 		xn->reply_sz = 0;
653 		xn->state = IDPF_VC_XN_COMPLETED_FAILED;
654 		err = -EINVAL;
655 		goto out_unlock;
656 	}
657 
658 	if (ctlq_msg->cookie.mbx.chnl_retval) {
659 		xn->reply_sz = 0;
660 		xn->state = IDPF_VC_XN_COMPLETED_FAILED;
661 		err = -EINVAL;
662 		goto out_unlock;
663 	}
664 
665 	if (ctlq_msg->data_len) {
666 		payload = ctlq_msg->ctx.indirect.payload->va;
667 		payload_size = ctlq_msg->ctx.indirect.payload->size;
668 	}
669 
670 	xn->reply_sz = payload_size;
671 	xn->state = IDPF_VC_XN_COMPLETED_SUCCESS;
672 
673 	if (xn->reply.iov_base && xn->reply.iov_len && payload_size)
674 		memcpy(xn->reply.iov_base, payload,
675 		       min_t(size_t, xn->reply.iov_len, payload_size));
676 
677 out_unlock:
678 	idpf_vc_xn_unlock(xn);
679 	/* we _cannot_ hold lock while calling complete */
680 	complete(&xn->completed);
681 
682 	return err;
683 }
684 
685 /**
686  * idpf_recv_mb_msg - Receive message over mailbox
687  * @adapter: Driver specific private structure
688  *
689  * Will receive control queue message and posts the receive buffer. Returns 0
690  * on success and negative on failure.
691  */
692 int idpf_recv_mb_msg(struct idpf_adapter *adapter)
693 {
694 	struct idpf_ctlq_msg ctlq_msg;
695 	struct idpf_dma_mem *dma_mem;
696 	int post_err, err;
697 	u16 num_recv;
698 
699 	while (1) {
700 		/* This will get <= num_recv messages and output how many
701 		 * actually received on num_recv.
702 		 */
703 		num_recv = 1;
704 		err = idpf_ctlq_recv(adapter->hw.arq, &num_recv, &ctlq_msg);
705 		if (err || !num_recv)
706 			break;
707 
708 		if (ctlq_msg.data_len) {
709 			dma_mem = ctlq_msg.ctx.indirect.payload;
710 		} else {
711 			dma_mem = NULL;
712 			num_recv = 0;
713 		}
714 
715 		if (ctlq_msg.cookie.mbx.chnl_opcode == VIRTCHNL2_OP_EVENT)
716 			idpf_recv_event_msg(adapter, &ctlq_msg);
717 		else
718 			err = idpf_vc_xn_forward_reply(adapter, &ctlq_msg);
719 
720 		post_err = idpf_ctlq_post_rx_buffs(&adapter->hw,
721 						   adapter->hw.arq,
722 						   &num_recv, &dma_mem);
723 
724 		/* If post failed clear the only buffer we supplied */
725 		if (post_err) {
726 			if (dma_mem)
727 				dmam_free_coherent(&adapter->pdev->dev,
728 						   dma_mem->size, dma_mem->va,
729 						   dma_mem->pa);
730 			break;
731 		}
732 
733 		/* virtchnl trying to shutdown, stop cleaning */
734 		if (err == -ENXIO)
735 			break;
736 	}
737 
738 	return err;
739 }
740 
741 /**
742  * idpf_wait_for_marker_event - wait for software marker response
743  * @vport: virtual port data structure
744  *
745  * Returns 0 success, negative on failure.
746  **/
747 static int idpf_wait_for_marker_event(struct idpf_vport *vport)
748 {
749 	int event;
750 	int i;
751 
752 	for (i = 0; i < vport->num_txq; i++)
753 		set_bit(__IDPF_Q_SW_MARKER, vport->txqs[i]->flags);
754 
755 	event = wait_event_timeout(vport->sw_marker_wq,
756 				   test_and_clear_bit(IDPF_VPORT_SW_MARKER,
757 						      vport->flags),
758 				   msecs_to_jiffies(500));
759 
760 	for (i = 0; i < vport->num_txq; i++)
761 		clear_bit(__IDPF_Q_POLL_MODE, vport->txqs[i]->flags);
762 
763 	if (event)
764 		return 0;
765 
766 	dev_warn(&vport->adapter->pdev->dev, "Failed to receive marker packets\n");
767 
768 	return -ETIMEDOUT;
769 }
770 
771 /**
772  * idpf_send_ver_msg - send virtchnl version message
773  * @adapter: Driver specific private structure
774  *
775  * Send virtchnl version message.  Returns 0 on success, negative on failure.
776  */
777 static int idpf_send_ver_msg(struct idpf_adapter *adapter)
778 {
779 	struct idpf_vc_xn_params xn_params = {};
780 	struct virtchnl2_version_info vvi;
781 	ssize_t reply_sz;
782 	u32 major, minor;
783 	int err = 0;
784 
785 	if (adapter->virt_ver_maj) {
786 		vvi.major = cpu_to_le32(adapter->virt_ver_maj);
787 		vvi.minor = cpu_to_le32(adapter->virt_ver_min);
788 	} else {
789 		vvi.major = cpu_to_le32(IDPF_VIRTCHNL_VERSION_MAJOR);
790 		vvi.minor = cpu_to_le32(IDPF_VIRTCHNL_VERSION_MINOR);
791 	}
792 
793 	xn_params.vc_op = VIRTCHNL2_OP_VERSION;
794 	xn_params.send_buf.iov_base = &vvi;
795 	xn_params.send_buf.iov_len = sizeof(vvi);
796 	xn_params.recv_buf = xn_params.send_buf;
797 	xn_params.timeout_ms = IDPF_VC_XN_DEFAULT_TIMEOUT_MSEC;
798 
799 	reply_sz = idpf_vc_xn_exec(adapter, &xn_params);
800 	if (reply_sz < 0)
801 		return reply_sz;
802 	if (reply_sz < sizeof(vvi))
803 		return -EIO;
804 
805 	major = le32_to_cpu(vvi.major);
806 	minor = le32_to_cpu(vvi.minor);
807 
808 	if (major > IDPF_VIRTCHNL_VERSION_MAJOR) {
809 		dev_warn(&adapter->pdev->dev, "Virtchnl major version greater than supported\n");
810 		return -EINVAL;
811 	}
812 
813 	if (major == IDPF_VIRTCHNL_VERSION_MAJOR &&
814 	    minor > IDPF_VIRTCHNL_VERSION_MINOR)
815 		dev_warn(&adapter->pdev->dev, "Virtchnl minor version didn't match\n");
816 
817 	/* If we have a mismatch, resend version to update receiver on what
818 	 * version we will use.
819 	 */
820 	if (!adapter->virt_ver_maj &&
821 	    major != IDPF_VIRTCHNL_VERSION_MAJOR &&
822 	    minor != IDPF_VIRTCHNL_VERSION_MINOR)
823 		err = -EAGAIN;
824 
825 	adapter->virt_ver_maj = major;
826 	adapter->virt_ver_min = minor;
827 
828 	return err;
829 }
830 
831 /**
832  * idpf_send_get_caps_msg - Send virtchnl get capabilities message
833  * @adapter: Driver specific private structure
834  *
835  * Send virtchl get capabilities message. Returns 0 on success, negative on
836  * failure.
837  */
838 static int idpf_send_get_caps_msg(struct idpf_adapter *adapter)
839 {
840 	struct virtchnl2_get_capabilities caps = {};
841 	struct idpf_vc_xn_params xn_params = {};
842 	ssize_t reply_sz;
843 
844 	caps.csum_caps =
845 		cpu_to_le32(VIRTCHNL2_CAP_TX_CSUM_L3_IPV4	|
846 			    VIRTCHNL2_CAP_TX_CSUM_L4_IPV4_TCP	|
847 			    VIRTCHNL2_CAP_TX_CSUM_L4_IPV4_UDP	|
848 			    VIRTCHNL2_CAP_TX_CSUM_L4_IPV4_SCTP	|
849 			    VIRTCHNL2_CAP_TX_CSUM_L4_IPV6_TCP	|
850 			    VIRTCHNL2_CAP_TX_CSUM_L4_IPV6_UDP	|
851 			    VIRTCHNL2_CAP_TX_CSUM_L4_IPV6_SCTP	|
852 			    VIRTCHNL2_CAP_RX_CSUM_L3_IPV4	|
853 			    VIRTCHNL2_CAP_RX_CSUM_L4_IPV4_TCP	|
854 			    VIRTCHNL2_CAP_RX_CSUM_L4_IPV4_UDP	|
855 			    VIRTCHNL2_CAP_RX_CSUM_L4_IPV4_SCTP	|
856 			    VIRTCHNL2_CAP_RX_CSUM_L4_IPV6_TCP	|
857 			    VIRTCHNL2_CAP_RX_CSUM_L4_IPV6_UDP	|
858 			    VIRTCHNL2_CAP_RX_CSUM_L4_IPV6_SCTP	|
859 			    VIRTCHNL2_CAP_TX_CSUM_L3_SINGLE_TUNNEL |
860 			    VIRTCHNL2_CAP_RX_CSUM_L3_SINGLE_TUNNEL |
861 			    VIRTCHNL2_CAP_TX_CSUM_L4_SINGLE_TUNNEL |
862 			    VIRTCHNL2_CAP_RX_CSUM_L4_SINGLE_TUNNEL |
863 			    VIRTCHNL2_CAP_RX_CSUM_GENERIC);
864 
865 	caps.seg_caps =
866 		cpu_to_le32(VIRTCHNL2_CAP_SEG_IPV4_TCP		|
867 			    VIRTCHNL2_CAP_SEG_IPV4_UDP		|
868 			    VIRTCHNL2_CAP_SEG_IPV4_SCTP		|
869 			    VIRTCHNL2_CAP_SEG_IPV6_TCP		|
870 			    VIRTCHNL2_CAP_SEG_IPV6_UDP		|
871 			    VIRTCHNL2_CAP_SEG_IPV6_SCTP		|
872 			    VIRTCHNL2_CAP_SEG_TX_SINGLE_TUNNEL);
873 
874 	caps.rss_caps =
875 		cpu_to_le64(VIRTCHNL2_CAP_RSS_IPV4_TCP		|
876 			    VIRTCHNL2_CAP_RSS_IPV4_UDP		|
877 			    VIRTCHNL2_CAP_RSS_IPV4_SCTP		|
878 			    VIRTCHNL2_CAP_RSS_IPV4_OTHER	|
879 			    VIRTCHNL2_CAP_RSS_IPV6_TCP		|
880 			    VIRTCHNL2_CAP_RSS_IPV6_UDP		|
881 			    VIRTCHNL2_CAP_RSS_IPV6_SCTP		|
882 			    VIRTCHNL2_CAP_RSS_IPV6_OTHER);
883 
884 	caps.hsplit_caps =
885 		cpu_to_le32(VIRTCHNL2_CAP_RX_HSPLIT_AT_L4V4	|
886 			    VIRTCHNL2_CAP_RX_HSPLIT_AT_L4V6);
887 
888 	caps.rsc_caps =
889 		cpu_to_le32(VIRTCHNL2_CAP_RSC_IPV4_TCP		|
890 			    VIRTCHNL2_CAP_RSC_IPV6_TCP);
891 
892 	caps.other_caps =
893 		cpu_to_le64(VIRTCHNL2_CAP_SRIOV			|
894 			    VIRTCHNL2_CAP_MACFILTER		|
895 			    VIRTCHNL2_CAP_SPLITQ_QSCHED		|
896 			    VIRTCHNL2_CAP_PROMISC		|
897 			    VIRTCHNL2_CAP_LOOPBACK);
898 
899 	xn_params.vc_op = VIRTCHNL2_OP_GET_CAPS;
900 	xn_params.send_buf.iov_base = &caps;
901 	xn_params.send_buf.iov_len = sizeof(caps);
902 	xn_params.recv_buf.iov_base = &adapter->caps;
903 	xn_params.recv_buf.iov_len = sizeof(adapter->caps);
904 	xn_params.timeout_ms = IDPF_VC_XN_DEFAULT_TIMEOUT_MSEC;
905 
906 	reply_sz = idpf_vc_xn_exec(adapter, &xn_params);
907 	if (reply_sz < 0)
908 		return reply_sz;
909 	if (reply_sz < sizeof(adapter->caps))
910 		return -EIO;
911 
912 	return 0;
913 }
914 
915 /**
916  * idpf_vport_alloc_max_qs - Allocate max queues for a vport
917  * @adapter: Driver specific private structure
918  * @max_q: vport max queue structure
919  */
920 int idpf_vport_alloc_max_qs(struct idpf_adapter *adapter,
921 			    struct idpf_vport_max_q *max_q)
922 {
923 	struct idpf_avail_queue_info *avail_queues = &adapter->avail_queues;
924 	struct virtchnl2_get_capabilities *caps = &adapter->caps;
925 	u16 default_vports = idpf_get_default_vports(adapter);
926 	int max_rx_q, max_tx_q;
927 
928 	mutex_lock(&adapter->queue_lock);
929 
930 	max_rx_q = le16_to_cpu(caps->max_rx_q) / default_vports;
931 	max_tx_q = le16_to_cpu(caps->max_tx_q) / default_vports;
932 	if (adapter->num_alloc_vports < default_vports) {
933 		max_q->max_rxq = min_t(u16, max_rx_q, IDPF_MAX_Q);
934 		max_q->max_txq = min_t(u16, max_tx_q, IDPF_MAX_Q);
935 	} else {
936 		max_q->max_rxq = IDPF_MIN_Q;
937 		max_q->max_txq = IDPF_MIN_Q;
938 	}
939 	max_q->max_bufq = max_q->max_rxq * IDPF_MAX_BUFQS_PER_RXQ_GRP;
940 	max_q->max_complq = max_q->max_txq;
941 
942 	if (avail_queues->avail_rxq < max_q->max_rxq ||
943 	    avail_queues->avail_txq < max_q->max_txq ||
944 	    avail_queues->avail_bufq < max_q->max_bufq ||
945 	    avail_queues->avail_complq < max_q->max_complq) {
946 		mutex_unlock(&adapter->queue_lock);
947 
948 		return -EINVAL;
949 	}
950 
951 	avail_queues->avail_rxq -= max_q->max_rxq;
952 	avail_queues->avail_txq -= max_q->max_txq;
953 	avail_queues->avail_bufq -= max_q->max_bufq;
954 	avail_queues->avail_complq -= max_q->max_complq;
955 
956 	mutex_unlock(&adapter->queue_lock);
957 
958 	return 0;
959 }
960 
961 /**
962  * idpf_vport_dealloc_max_qs - Deallocate max queues of a vport
963  * @adapter: Driver specific private structure
964  * @max_q: vport max queue structure
965  */
966 void idpf_vport_dealloc_max_qs(struct idpf_adapter *adapter,
967 			       struct idpf_vport_max_q *max_q)
968 {
969 	struct idpf_avail_queue_info *avail_queues;
970 
971 	mutex_lock(&adapter->queue_lock);
972 	avail_queues = &adapter->avail_queues;
973 
974 	avail_queues->avail_rxq += max_q->max_rxq;
975 	avail_queues->avail_txq += max_q->max_txq;
976 	avail_queues->avail_bufq += max_q->max_bufq;
977 	avail_queues->avail_complq += max_q->max_complq;
978 
979 	mutex_unlock(&adapter->queue_lock);
980 }
981 
982 /**
983  * idpf_init_avail_queues - Initialize available queues on the device
984  * @adapter: Driver specific private structure
985  */
986 static void idpf_init_avail_queues(struct idpf_adapter *adapter)
987 {
988 	struct idpf_avail_queue_info *avail_queues = &adapter->avail_queues;
989 	struct virtchnl2_get_capabilities *caps = &adapter->caps;
990 
991 	avail_queues->avail_rxq = le16_to_cpu(caps->max_rx_q);
992 	avail_queues->avail_txq = le16_to_cpu(caps->max_tx_q);
993 	avail_queues->avail_bufq = le16_to_cpu(caps->max_rx_bufq);
994 	avail_queues->avail_complq = le16_to_cpu(caps->max_tx_complq);
995 }
996 
997 /**
998  * idpf_get_reg_intr_vecs - Get vector queue register offset
999  * @vport: virtual port structure
1000  * @reg_vals: Register offsets to store in
1001  *
1002  * Returns number of registers that got populated
1003  */
1004 int idpf_get_reg_intr_vecs(struct idpf_vport *vport,
1005 			   struct idpf_vec_regs *reg_vals)
1006 {
1007 	struct virtchnl2_vector_chunks *chunks;
1008 	struct idpf_vec_regs reg_val;
1009 	u16 num_vchunks, num_vec;
1010 	int num_regs = 0, i, j;
1011 
1012 	chunks = &vport->adapter->req_vec_chunks->vchunks;
1013 	num_vchunks = le16_to_cpu(chunks->num_vchunks);
1014 
1015 	for (j = 0; j < num_vchunks; j++) {
1016 		struct virtchnl2_vector_chunk *chunk;
1017 		u32 dynctl_reg_spacing;
1018 		u32 itrn_reg_spacing;
1019 
1020 		chunk = &chunks->vchunks[j];
1021 		num_vec = le16_to_cpu(chunk->num_vectors);
1022 		reg_val.dyn_ctl_reg = le32_to_cpu(chunk->dynctl_reg_start);
1023 		reg_val.itrn_reg = le32_to_cpu(chunk->itrn_reg_start);
1024 		reg_val.itrn_index_spacing = le32_to_cpu(chunk->itrn_index_spacing);
1025 
1026 		dynctl_reg_spacing = le32_to_cpu(chunk->dynctl_reg_spacing);
1027 		itrn_reg_spacing = le32_to_cpu(chunk->itrn_reg_spacing);
1028 
1029 		for (i = 0; i < num_vec; i++) {
1030 			reg_vals[num_regs].dyn_ctl_reg = reg_val.dyn_ctl_reg;
1031 			reg_vals[num_regs].itrn_reg = reg_val.itrn_reg;
1032 			reg_vals[num_regs].itrn_index_spacing =
1033 						reg_val.itrn_index_spacing;
1034 
1035 			reg_val.dyn_ctl_reg += dynctl_reg_spacing;
1036 			reg_val.itrn_reg += itrn_reg_spacing;
1037 			num_regs++;
1038 		}
1039 	}
1040 
1041 	return num_regs;
1042 }
1043 
1044 /**
1045  * idpf_vport_get_q_reg - Get the queue registers for the vport
1046  * @reg_vals: register values needing to be set
1047  * @num_regs: amount we expect to fill
1048  * @q_type: queue model
1049  * @chunks: queue regs received over mailbox
1050  *
1051  * This function parses the queue register offsets from the queue register
1052  * chunk information, with a specific queue type and stores it into the array
1053  * passed as an argument. It returns the actual number of queue registers that
1054  * are filled.
1055  */
1056 static int idpf_vport_get_q_reg(u32 *reg_vals, int num_regs, u32 q_type,
1057 				struct virtchnl2_queue_reg_chunks *chunks)
1058 {
1059 	u16 num_chunks = le16_to_cpu(chunks->num_chunks);
1060 	int reg_filled = 0, i;
1061 	u32 reg_val;
1062 
1063 	while (num_chunks--) {
1064 		struct virtchnl2_queue_reg_chunk *chunk;
1065 		u16 num_q;
1066 
1067 		chunk = &chunks->chunks[num_chunks];
1068 		if (le32_to_cpu(chunk->type) != q_type)
1069 			continue;
1070 
1071 		num_q = le32_to_cpu(chunk->num_queues);
1072 		reg_val = le64_to_cpu(chunk->qtail_reg_start);
1073 		for (i = 0; i < num_q && reg_filled < num_regs ; i++) {
1074 			reg_vals[reg_filled++] = reg_val;
1075 			reg_val += le32_to_cpu(chunk->qtail_reg_spacing);
1076 		}
1077 	}
1078 
1079 	return reg_filled;
1080 }
1081 
1082 /**
1083  * __idpf_queue_reg_init - initialize queue registers
1084  * @vport: virtual port structure
1085  * @reg_vals: registers we are initializing
1086  * @num_regs: how many registers there are in total
1087  * @q_type: queue model
1088  *
1089  * Return number of queues that are initialized
1090  */
1091 static int __idpf_queue_reg_init(struct idpf_vport *vport, u32 *reg_vals,
1092 				 int num_regs, u32 q_type)
1093 {
1094 	struct idpf_adapter *adapter = vport->adapter;
1095 	struct idpf_queue *q;
1096 	int i, j, k = 0;
1097 
1098 	switch (q_type) {
1099 	case VIRTCHNL2_QUEUE_TYPE_TX:
1100 		for (i = 0; i < vport->num_txq_grp; i++) {
1101 			struct idpf_txq_group *tx_qgrp = &vport->txq_grps[i];
1102 
1103 			for (j = 0; j < tx_qgrp->num_txq && k < num_regs; j++, k++)
1104 				tx_qgrp->txqs[j]->tail =
1105 					idpf_get_reg_addr(adapter, reg_vals[k]);
1106 		}
1107 		break;
1108 	case VIRTCHNL2_QUEUE_TYPE_RX:
1109 		for (i = 0; i < vport->num_rxq_grp; i++) {
1110 			struct idpf_rxq_group *rx_qgrp = &vport->rxq_grps[i];
1111 			u16 num_rxq = rx_qgrp->singleq.num_rxq;
1112 
1113 			for (j = 0; j < num_rxq && k < num_regs; j++, k++) {
1114 				q = rx_qgrp->singleq.rxqs[j];
1115 				q->tail = idpf_get_reg_addr(adapter,
1116 							    reg_vals[k]);
1117 			}
1118 		}
1119 		break;
1120 	case VIRTCHNL2_QUEUE_TYPE_RX_BUFFER:
1121 		for (i = 0; i < vport->num_rxq_grp; i++) {
1122 			struct idpf_rxq_group *rx_qgrp = &vport->rxq_grps[i];
1123 			u8 num_bufqs = vport->num_bufqs_per_qgrp;
1124 
1125 			for (j = 0; j < num_bufqs && k < num_regs; j++, k++) {
1126 				q = &rx_qgrp->splitq.bufq_sets[j].bufq;
1127 				q->tail = idpf_get_reg_addr(adapter,
1128 							    reg_vals[k]);
1129 			}
1130 		}
1131 		break;
1132 	default:
1133 		break;
1134 	}
1135 
1136 	return k;
1137 }
1138 
1139 /**
1140  * idpf_queue_reg_init - initialize queue registers
1141  * @vport: virtual port structure
1142  *
1143  * Return 0 on success, negative on failure
1144  */
1145 int idpf_queue_reg_init(struct idpf_vport *vport)
1146 {
1147 	struct virtchnl2_create_vport *vport_params;
1148 	struct virtchnl2_queue_reg_chunks *chunks;
1149 	struct idpf_vport_config *vport_config;
1150 	u16 vport_idx = vport->idx;
1151 	int num_regs, ret = 0;
1152 	u32 *reg_vals;
1153 
1154 	/* We may never deal with more than 256 same type of queues */
1155 	reg_vals = kzalloc(sizeof(void *) * IDPF_LARGE_MAX_Q, GFP_KERNEL);
1156 	if (!reg_vals)
1157 		return -ENOMEM;
1158 
1159 	vport_config = vport->adapter->vport_config[vport_idx];
1160 	if (vport_config->req_qs_chunks) {
1161 		struct virtchnl2_add_queues *vc_aq =
1162 		  (struct virtchnl2_add_queues *)vport_config->req_qs_chunks;
1163 		chunks = &vc_aq->chunks;
1164 	} else {
1165 		vport_params = vport->adapter->vport_params_recvd[vport_idx];
1166 		chunks = &vport_params->chunks;
1167 	}
1168 
1169 	/* Initialize Tx queue tail register address */
1170 	num_regs = idpf_vport_get_q_reg(reg_vals, IDPF_LARGE_MAX_Q,
1171 					VIRTCHNL2_QUEUE_TYPE_TX,
1172 					chunks);
1173 	if (num_regs < vport->num_txq) {
1174 		ret = -EINVAL;
1175 		goto free_reg_vals;
1176 	}
1177 
1178 	num_regs = __idpf_queue_reg_init(vport, reg_vals, num_regs,
1179 					 VIRTCHNL2_QUEUE_TYPE_TX);
1180 	if (num_regs < vport->num_txq) {
1181 		ret = -EINVAL;
1182 		goto free_reg_vals;
1183 	}
1184 
1185 	/* Initialize Rx/buffer queue tail register address based on Rx queue
1186 	 * model
1187 	 */
1188 	if (idpf_is_queue_model_split(vport->rxq_model)) {
1189 		num_regs = idpf_vport_get_q_reg(reg_vals, IDPF_LARGE_MAX_Q,
1190 						VIRTCHNL2_QUEUE_TYPE_RX_BUFFER,
1191 						chunks);
1192 		if (num_regs < vport->num_bufq) {
1193 			ret = -EINVAL;
1194 			goto free_reg_vals;
1195 		}
1196 
1197 		num_regs = __idpf_queue_reg_init(vport, reg_vals, num_regs,
1198 						 VIRTCHNL2_QUEUE_TYPE_RX_BUFFER);
1199 		if (num_regs < vport->num_bufq) {
1200 			ret = -EINVAL;
1201 			goto free_reg_vals;
1202 		}
1203 	} else {
1204 		num_regs = idpf_vport_get_q_reg(reg_vals, IDPF_LARGE_MAX_Q,
1205 						VIRTCHNL2_QUEUE_TYPE_RX,
1206 						chunks);
1207 		if (num_regs < vport->num_rxq) {
1208 			ret = -EINVAL;
1209 			goto free_reg_vals;
1210 		}
1211 
1212 		num_regs = __idpf_queue_reg_init(vport, reg_vals, num_regs,
1213 						 VIRTCHNL2_QUEUE_TYPE_RX);
1214 		if (num_regs < vport->num_rxq) {
1215 			ret = -EINVAL;
1216 			goto free_reg_vals;
1217 		}
1218 	}
1219 
1220 free_reg_vals:
1221 	kfree(reg_vals);
1222 
1223 	return ret;
1224 }
1225 
1226 /**
1227  * idpf_send_create_vport_msg - Send virtchnl create vport message
1228  * @adapter: Driver specific private structure
1229  * @max_q: vport max queue info
1230  *
1231  * send virtchnl creae vport message
1232  *
1233  * Returns 0 on success, negative on failure
1234  */
1235 int idpf_send_create_vport_msg(struct idpf_adapter *adapter,
1236 			       struct idpf_vport_max_q *max_q)
1237 {
1238 	struct virtchnl2_create_vport *vport_msg;
1239 	struct idpf_vc_xn_params xn_params = {};
1240 	u16 idx = adapter->next_vport;
1241 	int err, buf_size;
1242 	ssize_t reply_sz;
1243 
1244 	buf_size = sizeof(struct virtchnl2_create_vport);
1245 	if (!adapter->vport_params_reqd[idx]) {
1246 		adapter->vport_params_reqd[idx] = kzalloc(buf_size,
1247 							  GFP_KERNEL);
1248 		if (!adapter->vport_params_reqd[idx])
1249 			return -ENOMEM;
1250 	}
1251 
1252 	vport_msg = adapter->vport_params_reqd[idx];
1253 	vport_msg->vport_type = cpu_to_le16(VIRTCHNL2_VPORT_TYPE_DEFAULT);
1254 	vport_msg->vport_index = cpu_to_le16(idx);
1255 
1256 	if (adapter->req_tx_splitq)
1257 		vport_msg->txq_model = cpu_to_le16(VIRTCHNL2_QUEUE_MODEL_SPLIT);
1258 	else
1259 		vport_msg->txq_model = cpu_to_le16(VIRTCHNL2_QUEUE_MODEL_SINGLE);
1260 
1261 	if (adapter->req_rx_splitq)
1262 		vport_msg->rxq_model = cpu_to_le16(VIRTCHNL2_QUEUE_MODEL_SPLIT);
1263 	else
1264 		vport_msg->rxq_model = cpu_to_le16(VIRTCHNL2_QUEUE_MODEL_SINGLE);
1265 
1266 	err = idpf_vport_calc_total_qs(adapter, idx, vport_msg, max_q);
1267 	if (err) {
1268 		dev_err(&adapter->pdev->dev, "Enough queues are not available");
1269 
1270 		return err;
1271 	}
1272 
1273 	if (!adapter->vport_params_recvd[idx]) {
1274 		adapter->vport_params_recvd[idx] = kzalloc(IDPF_CTLQ_MAX_BUF_LEN,
1275 							   GFP_KERNEL);
1276 		if (!adapter->vport_params_recvd[idx]) {
1277 			err = -ENOMEM;
1278 			goto free_vport_params;
1279 		}
1280 	}
1281 
1282 	xn_params.vc_op = VIRTCHNL2_OP_CREATE_VPORT;
1283 	xn_params.send_buf.iov_base = vport_msg;
1284 	xn_params.send_buf.iov_len = buf_size;
1285 	xn_params.recv_buf.iov_base = adapter->vport_params_recvd[idx];
1286 	xn_params.recv_buf.iov_len = IDPF_CTLQ_MAX_BUF_LEN;
1287 	xn_params.timeout_ms = IDPF_VC_XN_DEFAULT_TIMEOUT_MSEC;
1288 	reply_sz = idpf_vc_xn_exec(adapter, &xn_params);
1289 	if (reply_sz < 0) {
1290 		err = reply_sz;
1291 		goto free_vport_params;
1292 	}
1293 	if (reply_sz < IDPF_CTLQ_MAX_BUF_LEN) {
1294 		err = -EIO;
1295 		goto free_vport_params;
1296 	}
1297 
1298 	return 0;
1299 
1300 free_vport_params:
1301 	kfree(adapter->vport_params_recvd[idx]);
1302 	adapter->vport_params_recvd[idx] = NULL;
1303 	kfree(adapter->vport_params_reqd[idx]);
1304 	adapter->vport_params_reqd[idx] = NULL;
1305 
1306 	return err;
1307 }
1308 
1309 /**
1310  * idpf_check_supported_desc_ids - Verify we have required descriptor support
1311  * @vport: virtual port structure
1312  *
1313  * Return 0 on success, error on failure
1314  */
1315 int idpf_check_supported_desc_ids(struct idpf_vport *vport)
1316 {
1317 	struct idpf_adapter *adapter = vport->adapter;
1318 	struct virtchnl2_create_vport *vport_msg;
1319 	u64 rx_desc_ids, tx_desc_ids;
1320 
1321 	vport_msg = adapter->vport_params_recvd[vport->idx];
1322 
1323 	rx_desc_ids = le64_to_cpu(vport_msg->rx_desc_ids);
1324 	tx_desc_ids = le64_to_cpu(vport_msg->tx_desc_ids);
1325 
1326 	if (vport->rxq_model == VIRTCHNL2_QUEUE_MODEL_SPLIT) {
1327 		if (!(rx_desc_ids & VIRTCHNL2_RXDID_2_FLEX_SPLITQ_M)) {
1328 			dev_info(&adapter->pdev->dev, "Minimum RX descriptor support not provided, using the default\n");
1329 			vport_msg->rx_desc_ids = cpu_to_le64(VIRTCHNL2_RXDID_2_FLEX_SPLITQ_M);
1330 		}
1331 	} else {
1332 		if (!(rx_desc_ids & VIRTCHNL2_RXDID_2_FLEX_SQ_NIC_M))
1333 			vport->base_rxd = true;
1334 	}
1335 
1336 	if (vport->txq_model != VIRTCHNL2_QUEUE_MODEL_SPLIT)
1337 		return 0;
1338 
1339 	if ((tx_desc_ids & MIN_SUPPORT_TXDID) != MIN_SUPPORT_TXDID) {
1340 		dev_info(&adapter->pdev->dev, "Minimum TX descriptor support not provided, using the default\n");
1341 		vport_msg->tx_desc_ids = cpu_to_le64(MIN_SUPPORT_TXDID);
1342 	}
1343 
1344 	return 0;
1345 }
1346 
1347 /**
1348  * idpf_send_destroy_vport_msg - Send virtchnl destroy vport message
1349  * @vport: virtual port data structure
1350  *
1351  * Send virtchnl destroy vport message.  Returns 0 on success, negative on
1352  * failure.
1353  */
1354 int idpf_send_destroy_vport_msg(struct idpf_vport *vport)
1355 {
1356 	struct idpf_vc_xn_params xn_params = {};
1357 	struct virtchnl2_vport v_id;
1358 	ssize_t reply_sz;
1359 
1360 	v_id.vport_id = cpu_to_le32(vport->vport_id);
1361 
1362 	xn_params.vc_op = VIRTCHNL2_OP_DESTROY_VPORT;
1363 	xn_params.send_buf.iov_base = &v_id;
1364 	xn_params.send_buf.iov_len = sizeof(v_id);
1365 	xn_params.timeout_ms = IDPF_VC_XN_MIN_TIMEOUT_MSEC;
1366 	reply_sz = idpf_vc_xn_exec(vport->adapter, &xn_params);
1367 
1368 	return reply_sz < 0 ? reply_sz : 0;
1369 }
1370 
1371 /**
1372  * idpf_send_enable_vport_msg - Send virtchnl enable vport message
1373  * @vport: virtual port data structure
1374  *
1375  * Send enable vport virtchnl message.  Returns 0 on success, negative on
1376  * failure.
1377  */
1378 int idpf_send_enable_vport_msg(struct idpf_vport *vport)
1379 {
1380 	struct idpf_vc_xn_params xn_params = {};
1381 	struct virtchnl2_vport v_id;
1382 	ssize_t reply_sz;
1383 
1384 	v_id.vport_id = cpu_to_le32(vport->vport_id);
1385 
1386 	xn_params.vc_op = VIRTCHNL2_OP_ENABLE_VPORT;
1387 	xn_params.send_buf.iov_base = &v_id;
1388 	xn_params.send_buf.iov_len = sizeof(v_id);
1389 	xn_params.timeout_ms = IDPF_VC_XN_DEFAULT_TIMEOUT_MSEC;
1390 	reply_sz = idpf_vc_xn_exec(vport->adapter, &xn_params);
1391 
1392 	return reply_sz < 0 ? reply_sz : 0;
1393 }
1394 
1395 /**
1396  * idpf_send_disable_vport_msg - Send virtchnl disable vport message
1397  * @vport: virtual port data structure
1398  *
1399  * Send disable vport virtchnl message.  Returns 0 on success, negative on
1400  * failure.
1401  */
1402 int idpf_send_disable_vport_msg(struct idpf_vport *vport)
1403 {
1404 	struct idpf_vc_xn_params xn_params = {};
1405 	struct virtchnl2_vport v_id;
1406 	ssize_t reply_sz;
1407 
1408 	v_id.vport_id = cpu_to_le32(vport->vport_id);
1409 
1410 	xn_params.vc_op = VIRTCHNL2_OP_DISABLE_VPORT;
1411 	xn_params.send_buf.iov_base = &v_id;
1412 	xn_params.send_buf.iov_len = sizeof(v_id);
1413 	xn_params.timeout_ms = IDPF_VC_XN_MIN_TIMEOUT_MSEC;
1414 	reply_sz = idpf_vc_xn_exec(vport->adapter, &xn_params);
1415 
1416 	return reply_sz < 0 ? reply_sz : 0;
1417 }
1418 
1419 /**
1420  * idpf_send_config_tx_queues_msg - Send virtchnl config tx queues message
1421  * @vport: virtual port data structure
1422  *
1423  * Send config tx queues virtchnl message. Returns 0 on success, negative on
1424  * failure.
1425  */
1426 static int idpf_send_config_tx_queues_msg(struct idpf_vport *vport)
1427 {
1428 	struct virtchnl2_config_tx_queues *ctq __free(kfree) = NULL;
1429 	struct virtchnl2_txq_info *qi __free(kfree) = NULL;
1430 	struct idpf_vc_xn_params xn_params = {};
1431 	u32 config_sz, chunk_sz, buf_sz;
1432 	int totqs, num_msgs, num_chunks;
1433 	ssize_t reply_sz;
1434 	int i, k = 0;
1435 
1436 	totqs = vport->num_txq + vport->num_complq;
1437 	qi = kcalloc(totqs, sizeof(struct virtchnl2_txq_info), GFP_KERNEL);
1438 	if (!qi)
1439 		return -ENOMEM;
1440 
1441 	/* Populate the queue info buffer with all queue context info */
1442 	for (i = 0; i < vport->num_txq_grp; i++) {
1443 		struct idpf_txq_group *tx_qgrp = &vport->txq_grps[i];
1444 		int j, sched_mode;
1445 
1446 		for (j = 0; j < tx_qgrp->num_txq; j++, k++) {
1447 			qi[k].queue_id =
1448 				cpu_to_le32(tx_qgrp->txqs[j]->q_id);
1449 			qi[k].model =
1450 				cpu_to_le16(vport->txq_model);
1451 			qi[k].type =
1452 				cpu_to_le32(tx_qgrp->txqs[j]->q_type);
1453 			qi[k].ring_len =
1454 				cpu_to_le16(tx_qgrp->txqs[j]->desc_count);
1455 			qi[k].dma_ring_addr =
1456 				cpu_to_le64(tx_qgrp->txqs[j]->dma);
1457 			if (idpf_is_queue_model_split(vport->txq_model)) {
1458 				struct idpf_queue *q = tx_qgrp->txqs[j];
1459 
1460 				qi[k].tx_compl_queue_id =
1461 					cpu_to_le16(tx_qgrp->complq->q_id);
1462 				qi[k].relative_queue_id = cpu_to_le16(j);
1463 
1464 				if (test_bit(__IDPF_Q_FLOW_SCH_EN, q->flags))
1465 					qi[k].sched_mode =
1466 					cpu_to_le16(VIRTCHNL2_TXQ_SCHED_MODE_FLOW);
1467 				else
1468 					qi[k].sched_mode =
1469 					cpu_to_le16(VIRTCHNL2_TXQ_SCHED_MODE_QUEUE);
1470 			} else {
1471 				qi[k].sched_mode =
1472 					cpu_to_le16(VIRTCHNL2_TXQ_SCHED_MODE_QUEUE);
1473 			}
1474 		}
1475 
1476 		if (!idpf_is_queue_model_split(vport->txq_model))
1477 			continue;
1478 
1479 		qi[k].queue_id = cpu_to_le32(tx_qgrp->complq->q_id);
1480 		qi[k].model = cpu_to_le16(vport->txq_model);
1481 		qi[k].type = cpu_to_le32(tx_qgrp->complq->q_type);
1482 		qi[k].ring_len = cpu_to_le16(tx_qgrp->complq->desc_count);
1483 		qi[k].dma_ring_addr = cpu_to_le64(tx_qgrp->complq->dma);
1484 
1485 		if (test_bit(__IDPF_Q_FLOW_SCH_EN, tx_qgrp->complq->flags))
1486 			sched_mode = VIRTCHNL2_TXQ_SCHED_MODE_FLOW;
1487 		else
1488 			sched_mode = VIRTCHNL2_TXQ_SCHED_MODE_QUEUE;
1489 		qi[k].sched_mode = cpu_to_le16(sched_mode);
1490 
1491 		k++;
1492 	}
1493 
1494 	/* Make sure accounting agrees */
1495 	if (k != totqs)
1496 		return -EINVAL;
1497 
1498 	/* Chunk up the queue contexts into multiple messages to avoid
1499 	 * sending a control queue message buffer that is too large
1500 	 */
1501 	config_sz = sizeof(struct virtchnl2_config_tx_queues);
1502 	chunk_sz = sizeof(struct virtchnl2_txq_info);
1503 
1504 	num_chunks = min_t(u32, IDPF_NUM_CHUNKS_PER_MSG(config_sz, chunk_sz),
1505 			   totqs);
1506 	num_msgs = DIV_ROUND_UP(totqs, num_chunks);
1507 
1508 	buf_sz = struct_size(ctq, qinfo, num_chunks);
1509 	ctq = kzalloc(buf_sz, GFP_KERNEL);
1510 	if (!ctq)
1511 		return -ENOMEM;
1512 
1513 	xn_params.vc_op = VIRTCHNL2_OP_CONFIG_TX_QUEUES;
1514 	xn_params.timeout_ms = IDPF_VC_XN_DEFAULT_TIMEOUT_MSEC;
1515 
1516 	for (i = 0, k = 0; i < num_msgs; i++) {
1517 		memset(ctq, 0, buf_sz);
1518 		ctq->vport_id = cpu_to_le32(vport->vport_id);
1519 		ctq->num_qinfo = cpu_to_le16(num_chunks);
1520 		memcpy(ctq->qinfo, &qi[k], chunk_sz * num_chunks);
1521 
1522 		xn_params.send_buf.iov_base = ctq;
1523 		xn_params.send_buf.iov_len = buf_sz;
1524 		reply_sz = idpf_vc_xn_exec(vport->adapter, &xn_params);
1525 		if (reply_sz < 0)
1526 			return reply_sz;
1527 
1528 		k += num_chunks;
1529 		totqs -= num_chunks;
1530 		num_chunks = min(num_chunks, totqs);
1531 		/* Recalculate buffer size */
1532 		buf_sz = struct_size(ctq, qinfo, num_chunks);
1533 	}
1534 
1535 	return 0;
1536 }
1537 
1538 /**
1539  * idpf_send_config_rx_queues_msg - Send virtchnl config rx queues message
1540  * @vport: virtual port data structure
1541  *
1542  * Send config rx queues virtchnl message.  Returns 0 on success, negative on
1543  * failure.
1544  */
1545 static int idpf_send_config_rx_queues_msg(struct idpf_vport *vport)
1546 {
1547 	struct virtchnl2_config_rx_queues *crq __free(kfree) = NULL;
1548 	struct virtchnl2_rxq_info *qi __free(kfree) = NULL;
1549 	struct idpf_vc_xn_params xn_params = {};
1550 	u32 config_sz, chunk_sz, buf_sz;
1551 	int totqs, num_msgs, num_chunks;
1552 	ssize_t reply_sz;
1553 	int i, k = 0;
1554 
1555 	totqs = vport->num_rxq + vport->num_bufq;
1556 	qi = kcalloc(totqs, sizeof(struct virtchnl2_rxq_info), GFP_KERNEL);
1557 	if (!qi)
1558 		return -ENOMEM;
1559 
1560 	/* Populate the queue info buffer with all queue context info */
1561 	for (i = 0; i < vport->num_rxq_grp; i++) {
1562 		struct idpf_rxq_group *rx_qgrp = &vport->rxq_grps[i];
1563 		u16 num_rxq;
1564 		int j;
1565 
1566 		if (!idpf_is_queue_model_split(vport->rxq_model))
1567 			goto setup_rxqs;
1568 
1569 		for (j = 0; j < vport->num_bufqs_per_qgrp; j++, k++) {
1570 			struct idpf_queue *bufq =
1571 				&rx_qgrp->splitq.bufq_sets[j].bufq;
1572 
1573 			qi[k].queue_id = cpu_to_le32(bufq->q_id);
1574 			qi[k].model = cpu_to_le16(vport->rxq_model);
1575 			qi[k].type = cpu_to_le32(bufq->q_type);
1576 			qi[k].desc_ids = cpu_to_le64(VIRTCHNL2_RXDID_2_FLEX_SPLITQ_M);
1577 			qi[k].ring_len = cpu_to_le16(bufq->desc_count);
1578 			qi[k].dma_ring_addr = cpu_to_le64(bufq->dma);
1579 			qi[k].data_buffer_size = cpu_to_le32(bufq->rx_buf_size);
1580 			qi[k].buffer_notif_stride = bufq->rx_buf_stride;
1581 			qi[k].rx_buffer_low_watermark =
1582 				cpu_to_le16(bufq->rx_buffer_low_watermark);
1583 			if (idpf_is_feature_ena(vport, NETIF_F_GRO_HW))
1584 				qi[k].qflags |= cpu_to_le16(VIRTCHNL2_RXQ_RSC);
1585 		}
1586 
1587 setup_rxqs:
1588 		if (idpf_is_queue_model_split(vport->rxq_model))
1589 			num_rxq = rx_qgrp->splitq.num_rxq_sets;
1590 		else
1591 			num_rxq = rx_qgrp->singleq.num_rxq;
1592 
1593 		for (j = 0; j < num_rxq; j++, k++) {
1594 			struct idpf_queue *rxq;
1595 
1596 			if (!idpf_is_queue_model_split(vport->rxq_model)) {
1597 				rxq = rx_qgrp->singleq.rxqs[j];
1598 				goto common_qi_fields;
1599 			}
1600 			rxq = &rx_qgrp->splitq.rxq_sets[j]->rxq;
1601 			qi[k].rx_bufq1_id =
1602 			  cpu_to_le16(rxq->rxq_grp->splitq.bufq_sets[0].bufq.q_id);
1603 			if (vport->num_bufqs_per_qgrp > IDPF_SINGLE_BUFQ_PER_RXQ_GRP) {
1604 				qi[k].bufq2_ena = IDPF_BUFQ2_ENA;
1605 				qi[k].rx_bufq2_id =
1606 				  cpu_to_le16(rxq->rxq_grp->splitq.bufq_sets[1].bufq.q_id);
1607 			}
1608 			qi[k].rx_buffer_low_watermark =
1609 				cpu_to_le16(rxq->rx_buffer_low_watermark);
1610 			if (idpf_is_feature_ena(vport, NETIF_F_GRO_HW))
1611 				qi[k].qflags |= cpu_to_le16(VIRTCHNL2_RXQ_RSC);
1612 
1613 common_qi_fields:
1614 			if (rxq->rx_hsplit_en) {
1615 				qi[k].qflags |=
1616 					cpu_to_le16(VIRTCHNL2_RXQ_HDR_SPLIT);
1617 				qi[k].hdr_buffer_size =
1618 					cpu_to_le16(rxq->rx_hbuf_size);
1619 			}
1620 			qi[k].queue_id = cpu_to_le32(rxq->q_id);
1621 			qi[k].model = cpu_to_le16(vport->rxq_model);
1622 			qi[k].type = cpu_to_le32(rxq->q_type);
1623 			qi[k].ring_len = cpu_to_le16(rxq->desc_count);
1624 			qi[k].dma_ring_addr = cpu_to_le64(rxq->dma);
1625 			qi[k].max_pkt_size = cpu_to_le32(rxq->rx_max_pkt_size);
1626 			qi[k].data_buffer_size = cpu_to_le32(rxq->rx_buf_size);
1627 			qi[k].qflags |=
1628 				cpu_to_le16(VIRTCHNL2_RX_DESC_SIZE_32BYTE);
1629 			qi[k].desc_ids = cpu_to_le64(rxq->rxdids);
1630 		}
1631 	}
1632 
1633 	/* Make sure accounting agrees */
1634 	if (k != totqs)
1635 		return -EINVAL;
1636 
1637 	/* Chunk up the queue contexts into multiple messages to avoid
1638 	 * sending a control queue message buffer that is too large
1639 	 */
1640 	config_sz = sizeof(struct virtchnl2_config_rx_queues);
1641 	chunk_sz = sizeof(struct virtchnl2_rxq_info);
1642 
1643 	num_chunks = min_t(u32, IDPF_NUM_CHUNKS_PER_MSG(config_sz, chunk_sz),
1644 			   totqs);
1645 	num_msgs = DIV_ROUND_UP(totqs, num_chunks);
1646 
1647 	buf_sz = struct_size(crq, qinfo, num_chunks);
1648 	crq = kzalloc(buf_sz, GFP_KERNEL);
1649 	if (!crq)
1650 		return -ENOMEM;
1651 
1652 	xn_params.vc_op = VIRTCHNL2_OP_CONFIG_RX_QUEUES;
1653 	xn_params.timeout_ms = IDPF_VC_XN_DEFAULT_TIMEOUT_MSEC;
1654 
1655 	for (i = 0, k = 0; i < num_msgs; i++) {
1656 		memset(crq, 0, buf_sz);
1657 		crq->vport_id = cpu_to_le32(vport->vport_id);
1658 		crq->num_qinfo = cpu_to_le16(num_chunks);
1659 		memcpy(crq->qinfo, &qi[k], chunk_sz * num_chunks);
1660 
1661 		xn_params.send_buf.iov_base = crq;
1662 		xn_params.send_buf.iov_len = buf_sz;
1663 		reply_sz = idpf_vc_xn_exec(vport->adapter, &xn_params);
1664 		if (reply_sz < 0)
1665 			return reply_sz;
1666 
1667 		k += num_chunks;
1668 		totqs -= num_chunks;
1669 		num_chunks = min(num_chunks, totqs);
1670 		/* Recalculate buffer size */
1671 		buf_sz = struct_size(crq, qinfo, num_chunks);
1672 	}
1673 
1674 	return 0;
1675 }
1676 
1677 /**
1678  * idpf_send_ena_dis_queues_msg - Send virtchnl enable or disable
1679  * queues message
1680  * @vport: virtual port data structure
1681  * @ena: if true enable, false disable
1682  *
1683  * Send enable or disable queues virtchnl message. Returns 0 on success,
1684  * negative on failure.
1685  */
1686 static int idpf_send_ena_dis_queues_msg(struct idpf_vport *vport, bool ena)
1687 {
1688 	struct virtchnl2_del_ena_dis_queues *eq __free(kfree) = NULL;
1689 	struct virtchnl2_queue_chunk *qc __free(kfree) = NULL;
1690 	u32 num_msgs, num_chunks, num_txq, num_rxq, num_q;
1691 	struct idpf_vc_xn_params xn_params = {};
1692 	struct virtchnl2_queue_chunks *qcs;
1693 	u32 config_sz, chunk_sz, buf_sz;
1694 	ssize_t reply_sz;
1695 	int i, j, k = 0;
1696 
1697 	num_txq = vport->num_txq + vport->num_complq;
1698 	num_rxq = vport->num_rxq + vport->num_bufq;
1699 	num_q = num_txq + num_rxq;
1700 	buf_sz = sizeof(struct virtchnl2_queue_chunk) * num_q;
1701 	qc = kzalloc(buf_sz, GFP_KERNEL);
1702 	if (!qc)
1703 		return -ENOMEM;
1704 
1705 	for (i = 0; i < vport->num_txq_grp; i++) {
1706 		struct idpf_txq_group *tx_qgrp = &vport->txq_grps[i];
1707 
1708 		for (j = 0; j < tx_qgrp->num_txq; j++, k++) {
1709 			qc[k].type = cpu_to_le32(tx_qgrp->txqs[j]->q_type);
1710 			qc[k].start_queue_id = cpu_to_le32(tx_qgrp->txqs[j]->q_id);
1711 			qc[k].num_queues = cpu_to_le32(IDPF_NUMQ_PER_CHUNK);
1712 		}
1713 	}
1714 	if (vport->num_txq != k)
1715 		return -EINVAL;
1716 
1717 	if (!idpf_is_queue_model_split(vport->txq_model))
1718 		goto setup_rx;
1719 
1720 	for (i = 0; i < vport->num_txq_grp; i++, k++) {
1721 		struct idpf_txq_group *tx_qgrp = &vport->txq_grps[i];
1722 
1723 		qc[k].type = cpu_to_le32(tx_qgrp->complq->q_type);
1724 		qc[k].start_queue_id = cpu_to_le32(tx_qgrp->complq->q_id);
1725 		qc[k].num_queues = cpu_to_le32(IDPF_NUMQ_PER_CHUNK);
1726 	}
1727 	if (vport->num_complq != (k - vport->num_txq))
1728 		return -EINVAL;
1729 
1730 setup_rx:
1731 	for (i = 0; i < vport->num_rxq_grp; i++) {
1732 		struct idpf_rxq_group *rx_qgrp = &vport->rxq_grps[i];
1733 
1734 		if (idpf_is_queue_model_split(vport->rxq_model))
1735 			num_rxq = rx_qgrp->splitq.num_rxq_sets;
1736 		else
1737 			num_rxq = rx_qgrp->singleq.num_rxq;
1738 
1739 		for (j = 0; j < num_rxq; j++, k++) {
1740 			if (idpf_is_queue_model_split(vport->rxq_model)) {
1741 				qc[k].start_queue_id =
1742 				cpu_to_le32(rx_qgrp->splitq.rxq_sets[j]->rxq.q_id);
1743 				qc[k].type =
1744 				cpu_to_le32(rx_qgrp->splitq.rxq_sets[j]->rxq.q_type);
1745 			} else {
1746 				qc[k].start_queue_id =
1747 				cpu_to_le32(rx_qgrp->singleq.rxqs[j]->q_id);
1748 				qc[k].type =
1749 				cpu_to_le32(rx_qgrp->singleq.rxqs[j]->q_type);
1750 			}
1751 			qc[k].num_queues = cpu_to_le32(IDPF_NUMQ_PER_CHUNK);
1752 		}
1753 	}
1754 	if (vport->num_rxq != k - (vport->num_txq + vport->num_complq))
1755 		return -EINVAL;
1756 
1757 	if (!idpf_is_queue_model_split(vport->rxq_model))
1758 		goto send_msg;
1759 
1760 	for (i = 0; i < vport->num_rxq_grp; i++) {
1761 		struct idpf_rxq_group *rx_qgrp = &vport->rxq_grps[i];
1762 
1763 		for (j = 0; j < vport->num_bufqs_per_qgrp; j++, k++) {
1764 			struct idpf_queue *q;
1765 
1766 			q = &rx_qgrp->splitq.bufq_sets[j].bufq;
1767 			qc[k].type = cpu_to_le32(q->q_type);
1768 			qc[k].start_queue_id = cpu_to_le32(q->q_id);
1769 			qc[k].num_queues = cpu_to_le32(IDPF_NUMQ_PER_CHUNK);
1770 		}
1771 	}
1772 	if (vport->num_bufq != k - (vport->num_txq +
1773 				    vport->num_complq +
1774 				    vport->num_rxq))
1775 		return -EINVAL;
1776 
1777 send_msg:
1778 	/* Chunk up the queue info into multiple messages */
1779 	config_sz = sizeof(struct virtchnl2_del_ena_dis_queues);
1780 	chunk_sz = sizeof(struct virtchnl2_queue_chunk);
1781 
1782 	num_chunks = min_t(u32, IDPF_NUM_CHUNKS_PER_MSG(config_sz, chunk_sz),
1783 			   num_q);
1784 	num_msgs = DIV_ROUND_UP(num_q, num_chunks);
1785 
1786 	buf_sz = struct_size(eq, chunks.chunks, num_chunks);
1787 	eq = kzalloc(buf_sz, GFP_KERNEL);
1788 	if (!eq)
1789 		return -ENOMEM;
1790 
1791 	if (ena) {
1792 		xn_params.vc_op = VIRTCHNL2_OP_ENABLE_QUEUES;
1793 		xn_params.timeout_ms = IDPF_VC_XN_DEFAULT_TIMEOUT_MSEC;
1794 	} else {
1795 		xn_params.vc_op = VIRTCHNL2_OP_DISABLE_QUEUES;
1796 		xn_params.timeout_ms = IDPF_VC_XN_MIN_TIMEOUT_MSEC;
1797 	}
1798 
1799 	for (i = 0, k = 0; i < num_msgs; i++) {
1800 		memset(eq, 0, buf_sz);
1801 		eq->vport_id = cpu_to_le32(vport->vport_id);
1802 		eq->chunks.num_chunks = cpu_to_le16(num_chunks);
1803 		qcs = &eq->chunks;
1804 		memcpy(qcs->chunks, &qc[k], chunk_sz * num_chunks);
1805 
1806 		xn_params.send_buf.iov_base = eq;
1807 		xn_params.send_buf.iov_len = buf_sz;
1808 		reply_sz = idpf_vc_xn_exec(vport->adapter, &xn_params);
1809 		if (reply_sz < 0)
1810 			return reply_sz;
1811 
1812 		k += num_chunks;
1813 		num_q -= num_chunks;
1814 		num_chunks = min(num_chunks, num_q);
1815 		/* Recalculate buffer size */
1816 		buf_sz = struct_size(eq, chunks.chunks, num_chunks);
1817 	}
1818 
1819 	return 0;
1820 }
1821 
1822 /**
1823  * idpf_send_map_unmap_queue_vector_msg - Send virtchnl map or unmap queue
1824  * vector message
1825  * @vport: virtual port data structure
1826  * @map: true for map and false for unmap
1827  *
1828  * Send map or unmap queue vector virtchnl message.  Returns 0 on success,
1829  * negative on failure.
1830  */
1831 int idpf_send_map_unmap_queue_vector_msg(struct idpf_vport *vport, bool map)
1832 {
1833 	struct virtchnl2_queue_vector_maps *vqvm __free(kfree) = NULL;
1834 	struct virtchnl2_queue_vector *vqv __free(kfree) = NULL;
1835 	struct idpf_vc_xn_params xn_params = {};
1836 	u32 config_sz, chunk_sz, buf_sz;
1837 	u32 num_msgs, num_chunks, num_q;
1838 	ssize_t reply_sz;
1839 	int i, j, k = 0;
1840 
1841 	num_q = vport->num_txq + vport->num_rxq;
1842 
1843 	buf_sz = sizeof(struct virtchnl2_queue_vector) * num_q;
1844 	vqv = kzalloc(buf_sz, GFP_KERNEL);
1845 	if (!vqv)
1846 		return -ENOMEM;
1847 
1848 	for (i = 0; i < vport->num_txq_grp; i++) {
1849 		struct idpf_txq_group *tx_qgrp = &vport->txq_grps[i];
1850 
1851 		for (j = 0; j < tx_qgrp->num_txq; j++, k++) {
1852 			vqv[k].queue_type = cpu_to_le32(tx_qgrp->txqs[j]->q_type);
1853 			vqv[k].queue_id = cpu_to_le32(tx_qgrp->txqs[j]->q_id);
1854 
1855 			if (idpf_is_queue_model_split(vport->txq_model)) {
1856 				vqv[k].vector_id =
1857 				cpu_to_le16(tx_qgrp->complq->q_vector->v_idx);
1858 				vqv[k].itr_idx =
1859 				cpu_to_le32(tx_qgrp->complq->q_vector->tx_itr_idx);
1860 			} else {
1861 				vqv[k].vector_id =
1862 				cpu_to_le16(tx_qgrp->txqs[j]->q_vector->v_idx);
1863 				vqv[k].itr_idx =
1864 				cpu_to_le32(tx_qgrp->txqs[j]->q_vector->tx_itr_idx);
1865 			}
1866 		}
1867 	}
1868 
1869 	if (vport->num_txq != k)
1870 		return -EINVAL;
1871 
1872 	for (i = 0; i < vport->num_rxq_grp; i++) {
1873 		struct idpf_rxq_group *rx_qgrp = &vport->rxq_grps[i];
1874 		u16 num_rxq;
1875 
1876 		if (idpf_is_queue_model_split(vport->rxq_model))
1877 			num_rxq = rx_qgrp->splitq.num_rxq_sets;
1878 		else
1879 			num_rxq = rx_qgrp->singleq.num_rxq;
1880 
1881 		for (j = 0; j < num_rxq; j++, k++) {
1882 			struct idpf_queue *rxq;
1883 
1884 			if (idpf_is_queue_model_split(vport->rxq_model))
1885 				rxq = &rx_qgrp->splitq.rxq_sets[j]->rxq;
1886 			else
1887 				rxq = rx_qgrp->singleq.rxqs[j];
1888 
1889 			vqv[k].queue_type = cpu_to_le32(rxq->q_type);
1890 			vqv[k].queue_id = cpu_to_le32(rxq->q_id);
1891 			vqv[k].vector_id = cpu_to_le16(rxq->q_vector->v_idx);
1892 			vqv[k].itr_idx = cpu_to_le32(rxq->q_vector->rx_itr_idx);
1893 		}
1894 	}
1895 
1896 	if (idpf_is_queue_model_split(vport->txq_model)) {
1897 		if (vport->num_rxq != k - vport->num_complq)
1898 			return -EINVAL;
1899 	} else {
1900 		if (vport->num_rxq != k - vport->num_txq)
1901 			return -EINVAL;
1902 	}
1903 
1904 	/* Chunk up the vector info into multiple messages */
1905 	config_sz = sizeof(struct virtchnl2_queue_vector_maps);
1906 	chunk_sz = sizeof(struct virtchnl2_queue_vector);
1907 
1908 	num_chunks = min_t(u32, IDPF_NUM_CHUNKS_PER_MSG(config_sz, chunk_sz),
1909 			   num_q);
1910 	num_msgs = DIV_ROUND_UP(num_q, num_chunks);
1911 
1912 	buf_sz = struct_size(vqvm, qv_maps, num_chunks);
1913 	vqvm = kzalloc(buf_sz, GFP_KERNEL);
1914 	if (!vqvm)
1915 		return -ENOMEM;
1916 
1917 	if (map) {
1918 		xn_params.vc_op = VIRTCHNL2_OP_MAP_QUEUE_VECTOR;
1919 		xn_params.timeout_ms = IDPF_VC_XN_DEFAULT_TIMEOUT_MSEC;
1920 	} else {
1921 		xn_params.vc_op = VIRTCHNL2_OP_UNMAP_QUEUE_VECTOR;
1922 		xn_params.timeout_ms = IDPF_VC_XN_MIN_TIMEOUT_MSEC;
1923 	}
1924 
1925 	for (i = 0, k = 0; i < num_msgs; i++) {
1926 		memset(vqvm, 0, buf_sz);
1927 		xn_params.send_buf.iov_base = vqvm;
1928 		xn_params.send_buf.iov_len = buf_sz;
1929 		vqvm->vport_id = cpu_to_le32(vport->vport_id);
1930 		vqvm->num_qv_maps = cpu_to_le16(num_chunks);
1931 		memcpy(vqvm->qv_maps, &vqv[k], chunk_sz * num_chunks);
1932 
1933 		reply_sz = idpf_vc_xn_exec(vport->adapter, &xn_params);
1934 		if (reply_sz < 0)
1935 			return reply_sz;
1936 
1937 		k += num_chunks;
1938 		num_q -= num_chunks;
1939 		num_chunks = min(num_chunks, num_q);
1940 		/* Recalculate buffer size */
1941 		buf_sz = struct_size(vqvm, qv_maps, num_chunks);
1942 	}
1943 
1944 	return 0;
1945 }
1946 
1947 /**
1948  * idpf_send_enable_queues_msg - send enable queues virtchnl message
1949  * @vport: Virtual port private data structure
1950  *
1951  * Will send enable queues virtchnl message.  Returns 0 on success, negative on
1952  * failure.
1953  */
1954 int idpf_send_enable_queues_msg(struct idpf_vport *vport)
1955 {
1956 	return idpf_send_ena_dis_queues_msg(vport, true);
1957 }
1958 
1959 /**
1960  * idpf_send_disable_queues_msg - send disable queues virtchnl message
1961  * @vport: Virtual port private data structure
1962  *
1963  * Will send disable queues virtchnl message.  Returns 0 on success, negative
1964  * on failure.
1965  */
1966 int idpf_send_disable_queues_msg(struct idpf_vport *vport)
1967 {
1968 	int err, i;
1969 
1970 	err = idpf_send_ena_dis_queues_msg(vport, false);
1971 	if (err)
1972 		return err;
1973 
1974 	/* switch to poll mode as interrupts will be disabled after disable
1975 	 * queues virtchnl message is sent
1976 	 */
1977 	for (i = 0; i < vport->num_txq; i++)
1978 		set_bit(__IDPF_Q_POLL_MODE, vport->txqs[i]->flags);
1979 
1980 	/* schedule the napi to receive all the marker packets */
1981 	for (i = 0; i < vport->num_q_vectors; i++)
1982 		napi_schedule(&vport->q_vectors[i].napi);
1983 
1984 	return idpf_wait_for_marker_event(vport);
1985 }
1986 
1987 /**
1988  * idpf_convert_reg_to_queue_chunks - Copy queue chunk information to the right
1989  * structure
1990  * @dchunks: Destination chunks to store data to
1991  * @schunks: Source chunks to copy data from
1992  * @num_chunks: number of chunks to copy
1993  */
1994 static void idpf_convert_reg_to_queue_chunks(struct virtchnl2_queue_chunk *dchunks,
1995 					     struct virtchnl2_queue_reg_chunk *schunks,
1996 					     u16 num_chunks)
1997 {
1998 	u16 i;
1999 
2000 	for (i = 0; i < num_chunks; i++) {
2001 		dchunks[i].type = schunks[i].type;
2002 		dchunks[i].start_queue_id = schunks[i].start_queue_id;
2003 		dchunks[i].num_queues = schunks[i].num_queues;
2004 	}
2005 }
2006 
2007 /**
2008  * idpf_send_delete_queues_msg - send delete queues virtchnl message
2009  * @vport: Virtual port private data structure
2010  *
2011  * Will send delete queues virtchnl message. Return 0 on success, negative on
2012  * failure.
2013  */
2014 int idpf_send_delete_queues_msg(struct idpf_vport *vport)
2015 {
2016 	struct virtchnl2_del_ena_dis_queues *eq __free(kfree) = NULL;
2017 	struct virtchnl2_create_vport *vport_params;
2018 	struct virtchnl2_queue_reg_chunks *chunks;
2019 	struct idpf_vc_xn_params xn_params = {};
2020 	struct idpf_vport_config *vport_config;
2021 	u16 vport_idx = vport->idx;
2022 	ssize_t reply_sz;
2023 	u16 num_chunks;
2024 	int buf_size;
2025 
2026 	vport_config = vport->adapter->vport_config[vport_idx];
2027 	if (vport_config->req_qs_chunks) {
2028 		chunks = &vport_config->req_qs_chunks->chunks;
2029 	} else {
2030 		vport_params = vport->adapter->vport_params_recvd[vport_idx];
2031 		chunks = &vport_params->chunks;
2032 	}
2033 
2034 	num_chunks = le16_to_cpu(chunks->num_chunks);
2035 	buf_size = struct_size(eq, chunks.chunks, num_chunks);
2036 
2037 	eq = kzalloc(buf_size, GFP_KERNEL);
2038 	if (!eq)
2039 		return -ENOMEM;
2040 
2041 	eq->vport_id = cpu_to_le32(vport->vport_id);
2042 	eq->chunks.num_chunks = cpu_to_le16(num_chunks);
2043 
2044 	idpf_convert_reg_to_queue_chunks(eq->chunks.chunks, chunks->chunks,
2045 					 num_chunks);
2046 
2047 	xn_params.vc_op = VIRTCHNL2_OP_DEL_QUEUES;
2048 	xn_params.timeout_ms = IDPF_VC_XN_MIN_TIMEOUT_MSEC;
2049 	xn_params.send_buf.iov_base = eq;
2050 	xn_params.send_buf.iov_len = buf_size;
2051 	reply_sz = idpf_vc_xn_exec(vport->adapter, &xn_params);
2052 
2053 	return reply_sz < 0 ? reply_sz : 0;
2054 }
2055 
2056 /**
2057  * idpf_send_config_queues_msg - Send config queues virtchnl message
2058  * @vport: Virtual port private data structure
2059  *
2060  * Will send config queues virtchnl message. Returns 0 on success, negative on
2061  * failure.
2062  */
2063 int idpf_send_config_queues_msg(struct idpf_vport *vport)
2064 {
2065 	int err;
2066 
2067 	err = idpf_send_config_tx_queues_msg(vport);
2068 	if (err)
2069 		return err;
2070 
2071 	return idpf_send_config_rx_queues_msg(vport);
2072 }
2073 
2074 /**
2075  * idpf_send_add_queues_msg - Send virtchnl add queues message
2076  * @vport: Virtual port private data structure
2077  * @num_tx_q: number of transmit queues
2078  * @num_complq: number of transmit completion queues
2079  * @num_rx_q: number of receive queues
2080  * @num_rx_bufq: number of receive buffer queues
2081  *
2082  * Returns 0 on success, negative on failure. vport _MUST_ be const here as
2083  * we should not change any fields within vport itself in this function.
2084  */
2085 int idpf_send_add_queues_msg(const struct idpf_vport *vport, u16 num_tx_q,
2086 			     u16 num_complq, u16 num_rx_q, u16 num_rx_bufq)
2087 {
2088 	struct virtchnl2_add_queues *vc_msg __free(kfree) = NULL;
2089 	struct idpf_vc_xn_params xn_params = {};
2090 	struct idpf_vport_config *vport_config;
2091 	struct virtchnl2_add_queues aq = {};
2092 	u16 vport_idx = vport->idx;
2093 	ssize_t reply_sz;
2094 	int size;
2095 
2096 	vc_msg = kzalloc(IDPF_CTLQ_MAX_BUF_LEN, GFP_KERNEL);
2097 	if (!vc_msg)
2098 		return -ENOMEM;
2099 
2100 	vport_config = vport->adapter->vport_config[vport_idx];
2101 	kfree(vport_config->req_qs_chunks);
2102 	vport_config->req_qs_chunks = NULL;
2103 
2104 	aq.vport_id = cpu_to_le32(vport->vport_id);
2105 	aq.num_tx_q = cpu_to_le16(num_tx_q);
2106 	aq.num_tx_complq = cpu_to_le16(num_complq);
2107 	aq.num_rx_q = cpu_to_le16(num_rx_q);
2108 	aq.num_rx_bufq = cpu_to_le16(num_rx_bufq);
2109 
2110 	xn_params.vc_op = VIRTCHNL2_OP_ADD_QUEUES;
2111 	xn_params.timeout_ms = IDPF_VC_XN_DEFAULT_TIMEOUT_MSEC;
2112 	xn_params.send_buf.iov_base = &aq;
2113 	xn_params.send_buf.iov_len = sizeof(aq);
2114 	xn_params.recv_buf.iov_base = vc_msg;
2115 	xn_params.recv_buf.iov_len = IDPF_CTLQ_MAX_BUF_LEN;
2116 	reply_sz = idpf_vc_xn_exec(vport->adapter, &xn_params);
2117 	if (reply_sz < 0)
2118 		return reply_sz;
2119 
2120 	/* compare vc_msg num queues with vport num queues */
2121 	if (le16_to_cpu(vc_msg->num_tx_q) != num_tx_q ||
2122 	    le16_to_cpu(vc_msg->num_rx_q) != num_rx_q ||
2123 	    le16_to_cpu(vc_msg->num_tx_complq) != num_complq ||
2124 	    le16_to_cpu(vc_msg->num_rx_bufq) != num_rx_bufq)
2125 		return -EINVAL;
2126 
2127 	size = struct_size(vc_msg, chunks.chunks,
2128 			   le16_to_cpu(vc_msg->chunks.num_chunks));
2129 	if (reply_sz < size)
2130 		return -EIO;
2131 
2132 	vport_config->req_qs_chunks = kmemdup(vc_msg, size, GFP_KERNEL);
2133 	if (!vport_config->req_qs_chunks)
2134 		return -ENOMEM;
2135 
2136 	return 0;
2137 }
2138 
2139 /**
2140  * idpf_send_alloc_vectors_msg - Send virtchnl alloc vectors message
2141  * @adapter: Driver specific private structure
2142  * @num_vectors: number of vectors to be allocated
2143  *
2144  * Returns 0 on success, negative on failure.
2145  */
2146 int idpf_send_alloc_vectors_msg(struct idpf_adapter *adapter, u16 num_vectors)
2147 {
2148 	struct virtchnl2_alloc_vectors *rcvd_vec __free(kfree) = NULL;
2149 	struct idpf_vc_xn_params xn_params = {};
2150 	struct virtchnl2_alloc_vectors ac = {};
2151 	ssize_t reply_sz;
2152 	u16 num_vchunks;
2153 	int size;
2154 
2155 	ac.num_vectors = cpu_to_le16(num_vectors);
2156 
2157 	rcvd_vec = kzalloc(IDPF_CTLQ_MAX_BUF_LEN, GFP_KERNEL);
2158 	if (!rcvd_vec)
2159 		return -ENOMEM;
2160 
2161 	xn_params.vc_op = VIRTCHNL2_OP_ALLOC_VECTORS;
2162 	xn_params.send_buf.iov_base = &ac;
2163 	xn_params.send_buf.iov_len = sizeof(ac);
2164 	xn_params.recv_buf.iov_base = rcvd_vec;
2165 	xn_params.recv_buf.iov_len = IDPF_CTLQ_MAX_BUF_LEN;
2166 	xn_params.timeout_ms = IDPF_VC_XN_DEFAULT_TIMEOUT_MSEC;
2167 	reply_sz = idpf_vc_xn_exec(adapter, &xn_params);
2168 	if (reply_sz < 0)
2169 		return reply_sz;
2170 
2171 	num_vchunks = le16_to_cpu(rcvd_vec->vchunks.num_vchunks);
2172 	size = struct_size(rcvd_vec, vchunks.vchunks, num_vchunks);
2173 	if (reply_sz < size)
2174 		return -EIO;
2175 
2176 	if (size > IDPF_CTLQ_MAX_BUF_LEN)
2177 		return -EINVAL;
2178 
2179 	kfree(adapter->req_vec_chunks);
2180 	adapter->req_vec_chunks = kmemdup(rcvd_vec, size, GFP_KERNEL);
2181 	if (!adapter->req_vec_chunks)
2182 		return -ENOMEM;
2183 
2184 	if (le16_to_cpu(adapter->req_vec_chunks->num_vectors) < num_vectors) {
2185 		kfree(adapter->req_vec_chunks);
2186 		adapter->req_vec_chunks = NULL;
2187 		return -EINVAL;
2188 	}
2189 
2190 	return 0;
2191 }
2192 
2193 /**
2194  * idpf_send_dealloc_vectors_msg - Send virtchnl de allocate vectors message
2195  * @adapter: Driver specific private structure
2196  *
2197  * Returns 0 on success, negative on failure.
2198  */
2199 int idpf_send_dealloc_vectors_msg(struct idpf_adapter *adapter)
2200 {
2201 	struct virtchnl2_alloc_vectors *ac = adapter->req_vec_chunks;
2202 	struct virtchnl2_vector_chunks *vcs = &ac->vchunks;
2203 	struct idpf_vc_xn_params xn_params = {};
2204 	ssize_t reply_sz;
2205 	int buf_size;
2206 
2207 	buf_size = struct_size(vcs, vchunks, le16_to_cpu(vcs->num_vchunks));
2208 
2209 	xn_params.vc_op = VIRTCHNL2_OP_DEALLOC_VECTORS;
2210 	xn_params.send_buf.iov_base = vcs;
2211 	xn_params.send_buf.iov_len = buf_size;
2212 	xn_params.timeout_ms = IDPF_VC_XN_MIN_TIMEOUT_MSEC;
2213 	reply_sz = idpf_vc_xn_exec(adapter, &xn_params);
2214 	if (reply_sz < 0)
2215 		return reply_sz;
2216 
2217 	kfree(adapter->req_vec_chunks);
2218 	adapter->req_vec_chunks = NULL;
2219 
2220 	return 0;
2221 }
2222 
2223 /**
2224  * idpf_get_max_vfs - Get max number of vfs supported
2225  * @adapter: Driver specific private structure
2226  *
2227  * Returns max number of VFs
2228  */
2229 static int idpf_get_max_vfs(struct idpf_adapter *adapter)
2230 {
2231 	return le16_to_cpu(adapter->caps.max_sriov_vfs);
2232 }
2233 
2234 /**
2235  * idpf_send_set_sriov_vfs_msg - Send virtchnl set sriov vfs message
2236  * @adapter: Driver specific private structure
2237  * @num_vfs: number of virtual functions to be created
2238  *
2239  * Returns 0 on success, negative on failure.
2240  */
2241 int idpf_send_set_sriov_vfs_msg(struct idpf_adapter *adapter, u16 num_vfs)
2242 {
2243 	struct virtchnl2_sriov_vfs_info svi = {};
2244 	struct idpf_vc_xn_params xn_params = {};
2245 	ssize_t reply_sz;
2246 
2247 	svi.num_vfs = cpu_to_le16(num_vfs);
2248 	xn_params.vc_op = VIRTCHNL2_OP_SET_SRIOV_VFS;
2249 	xn_params.timeout_ms = IDPF_VC_XN_DEFAULT_TIMEOUT_MSEC;
2250 	xn_params.send_buf.iov_base = &svi;
2251 	xn_params.send_buf.iov_len = sizeof(svi);
2252 	reply_sz = idpf_vc_xn_exec(adapter, &xn_params);
2253 
2254 	return reply_sz < 0 ? reply_sz : 0;
2255 }
2256 
2257 /**
2258  * idpf_send_get_stats_msg - Send virtchnl get statistics message
2259  * @vport: vport to get stats for
2260  *
2261  * Returns 0 on success, negative on failure.
2262  */
2263 int idpf_send_get_stats_msg(struct idpf_vport *vport)
2264 {
2265 	struct idpf_netdev_priv *np = netdev_priv(vport->netdev);
2266 	struct rtnl_link_stats64 *netstats = &np->netstats;
2267 	struct virtchnl2_vport_stats stats_msg = {};
2268 	struct idpf_vc_xn_params xn_params = {};
2269 	ssize_t reply_sz;
2270 
2271 
2272 	/* Don't send get_stats message if the link is down */
2273 	if (np->state <= __IDPF_VPORT_DOWN)
2274 		return 0;
2275 
2276 	stats_msg.vport_id = cpu_to_le32(vport->vport_id);
2277 
2278 	xn_params.vc_op = VIRTCHNL2_OP_GET_STATS;
2279 	xn_params.send_buf.iov_base = &stats_msg;
2280 	xn_params.send_buf.iov_len = sizeof(stats_msg);
2281 	xn_params.recv_buf = xn_params.send_buf;
2282 	xn_params.timeout_ms = IDPF_VC_XN_DEFAULT_TIMEOUT_MSEC;
2283 
2284 	reply_sz = idpf_vc_xn_exec(vport->adapter, &xn_params);
2285 	if (reply_sz < 0)
2286 		return reply_sz;
2287 	if (reply_sz < sizeof(stats_msg))
2288 		return -EIO;
2289 
2290 	spin_lock_bh(&np->stats_lock);
2291 
2292 	netstats->rx_packets = le64_to_cpu(stats_msg.rx_unicast) +
2293 			       le64_to_cpu(stats_msg.rx_multicast) +
2294 			       le64_to_cpu(stats_msg.rx_broadcast);
2295 	netstats->tx_packets = le64_to_cpu(stats_msg.tx_unicast) +
2296 			       le64_to_cpu(stats_msg.tx_multicast) +
2297 			       le64_to_cpu(stats_msg.tx_broadcast);
2298 	netstats->rx_bytes = le64_to_cpu(stats_msg.rx_bytes);
2299 	netstats->tx_bytes = le64_to_cpu(stats_msg.tx_bytes);
2300 	netstats->rx_errors = le64_to_cpu(stats_msg.rx_errors);
2301 	netstats->tx_errors = le64_to_cpu(stats_msg.tx_errors);
2302 	netstats->rx_dropped = le64_to_cpu(stats_msg.rx_discards);
2303 	netstats->tx_dropped = le64_to_cpu(stats_msg.tx_discards);
2304 
2305 	vport->port_stats.vport_stats = stats_msg;
2306 
2307 	spin_unlock_bh(&np->stats_lock);
2308 
2309 	return 0;
2310 }
2311 
2312 /**
2313  * idpf_send_get_set_rss_lut_msg - Send virtchnl get or set rss lut message
2314  * @vport: virtual port data structure
2315  * @get: flag to set or get rss look up table
2316  *
2317  * Returns 0 on success, negative on failure.
2318  */
2319 int idpf_send_get_set_rss_lut_msg(struct idpf_vport *vport, bool get)
2320 {
2321 	struct virtchnl2_rss_lut *recv_rl __free(kfree) = NULL;
2322 	struct virtchnl2_rss_lut *rl __free(kfree) = NULL;
2323 	struct idpf_vc_xn_params xn_params = {};
2324 	struct idpf_rss_data *rss_data;
2325 	int buf_size, lut_buf_size;
2326 	ssize_t reply_sz;
2327 	int i;
2328 
2329 	rss_data =
2330 		&vport->adapter->vport_config[vport->idx]->user_config.rss_data;
2331 	buf_size = struct_size(rl, lut, rss_data->rss_lut_size);
2332 	rl = kzalloc(buf_size, GFP_KERNEL);
2333 	if (!rl)
2334 		return -ENOMEM;
2335 
2336 	rl->vport_id = cpu_to_le32(vport->vport_id);
2337 
2338 	xn_params.timeout_ms = IDPF_VC_XN_DEFAULT_TIMEOUT_MSEC;
2339 	xn_params.send_buf.iov_base = rl;
2340 	xn_params.send_buf.iov_len = buf_size;
2341 
2342 	if (get) {
2343 		recv_rl = kzalloc(IDPF_CTLQ_MAX_BUF_LEN, GFP_KERNEL);
2344 		if (!recv_rl)
2345 			return -ENOMEM;
2346 		xn_params.vc_op = VIRTCHNL2_OP_GET_RSS_LUT;
2347 		xn_params.recv_buf.iov_base = recv_rl;
2348 		xn_params.recv_buf.iov_len = IDPF_CTLQ_MAX_BUF_LEN;
2349 	} else {
2350 		rl->lut_entries = cpu_to_le16(rss_data->rss_lut_size);
2351 		for (i = 0; i < rss_data->rss_lut_size; i++)
2352 			rl->lut[i] = cpu_to_le32(rss_data->rss_lut[i]);
2353 
2354 		xn_params.vc_op = VIRTCHNL2_OP_SET_RSS_LUT;
2355 	}
2356 	reply_sz = idpf_vc_xn_exec(vport->adapter, &xn_params);
2357 	if (reply_sz < 0)
2358 		return reply_sz;
2359 	if (!get)
2360 		return 0;
2361 	if (reply_sz < sizeof(struct virtchnl2_rss_lut))
2362 		return -EIO;
2363 
2364 	lut_buf_size = le16_to_cpu(recv_rl->lut_entries) * sizeof(u32);
2365 	if (reply_sz < lut_buf_size)
2366 		return -EIO;
2367 
2368 	/* size didn't change, we can reuse existing lut buf */
2369 	if (rss_data->rss_lut_size == le16_to_cpu(recv_rl->lut_entries))
2370 		goto do_memcpy;
2371 
2372 	rss_data->rss_lut_size = le16_to_cpu(recv_rl->lut_entries);
2373 	kfree(rss_data->rss_lut);
2374 
2375 	rss_data->rss_lut = kzalloc(lut_buf_size, GFP_KERNEL);
2376 	if (!rss_data->rss_lut) {
2377 		rss_data->rss_lut_size = 0;
2378 		return -ENOMEM;
2379 	}
2380 
2381 do_memcpy:
2382 	memcpy(rss_data->rss_lut, recv_rl->lut, rss_data->rss_lut_size);
2383 
2384 	return 0;
2385 }
2386 
2387 /**
2388  * idpf_send_get_set_rss_key_msg - Send virtchnl get or set rss key message
2389  * @vport: virtual port data structure
2390  * @get: flag to set or get rss look up table
2391  *
2392  * Returns 0 on success, negative on failure
2393  */
2394 int idpf_send_get_set_rss_key_msg(struct idpf_vport *vport, bool get)
2395 {
2396 	struct virtchnl2_rss_key *recv_rk __free(kfree) = NULL;
2397 	struct virtchnl2_rss_key *rk __free(kfree) = NULL;
2398 	struct idpf_vc_xn_params xn_params = {};
2399 	struct idpf_rss_data *rss_data;
2400 	ssize_t reply_sz;
2401 	int i, buf_size;
2402 	u16 key_size;
2403 
2404 	rss_data =
2405 		&vport->adapter->vport_config[vport->idx]->user_config.rss_data;
2406 	buf_size = struct_size(rk, key_flex, rss_data->rss_key_size);
2407 	rk = kzalloc(buf_size, GFP_KERNEL);
2408 	if (!rk)
2409 		return -ENOMEM;
2410 
2411 	rk->vport_id = cpu_to_le32(vport->vport_id);
2412 	xn_params.send_buf.iov_base = rk;
2413 	xn_params.send_buf.iov_len = buf_size;
2414 	xn_params.timeout_ms = IDPF_VC_XN_DEFAULT_TIMEOUT_MSEC;
2415 	if (get) {
2416 		recv_rk = kzalloc(IDPF_CTLQ_MAX_BUF_LEN, GFP_KERNEL);
2417 		if (!recv_rk)
2418 			return -ENOMEM;
2419 
2420 		xn_params.vc_op = VIRTCHNL2_OP_GET_RSS_KEY;
2421 		xn_params.recv_buf.iov_base = recv_rk;
2422 		xn_params.recv_buf.iov_len = IDPF_CTLQ_MAX_BUF_LEN;
2423 	} else {
2424 		rk->key_len = cpu_to_le16(rss_data->rss_key_size);
2425 		for (i = 0; i < rss_data->rss_key_size; i++)
2426 			rk->key_flex[i] = rss_data->rss_key[i];
2427 
2428 		xn_params.vc_op = VIRTCHNL2_OP_SET_RSS_KEY;
2429 	}
2430 
2431 	reply_sz = idpf_vc_xn_exec(vport->adapter, &xn_params);
2432 	if (reply_sz < 0)
2433 		return reply_sz;
2434 	if (!get)
2435 		return 0;
2436 	if (reply_sz < sizeof(struct virtchnl2_rss_key))
2437 		return -EIO;
2438 
2439 	key_size = min_t(u16, NETDEV_RSS_KEY_LEN,
2440 			 le16_to_cpu(recv_rk->key_len));
2441 	if (reply_sz < key_size)
2442 		return -EIO;
2443 
2444 	/* key len didn't change, reuse existing buf */
2445 	if (rss_data->rss_key_size == key_size)
2446 		goto do_memcpy;
2447 
2448 	rss_data->rss_key_size = key_size;
2449 	kfree(rss_data->rss_key);
2450 	rss_data->rss_key = kzalloc(key_size, GFP_KERNEL);
2451 	if (!rss_data->rss_key) {
2452 		rss_data->rss_key_size = 0;
2453 		return -ENOMEM;
2454 	}
2455 
2456 do_memcpy:
2457 	memcpy(rss_data->rss_key, recv_rk->key_flex, rss_data->rss_key_size);
2458 
2459 	return 0;
2460 }
2461 
2462 /**
2463  * idpf_fill_ptype_lookup - Fill L3 specific fields in ptype lookup table
2464  * @ptype: ptype lookup table
2465  * @pstate: state machine for ptype lookup table
2466  * @ipv4: ipv4 or ipv6
2467  * @frag: fragmentation allowed
2468  *
2469  */
2470 static void idpf_fill_ptype_lookup(struct idpf_rx_ptype_decoded *ptype,
2471 				   struct idpf_ptype_state *pstate,
2472 				   bool ipv4, bool frag)
2473 {
2474 	if (!pstate->outer_ip || !pstate->outer_frag) {
2475 		ptype->outer_ip = IDPF_RX_PTYPE_OUTER_IP;
2476 		pstate->outer_ip = true;
2477 
2478 		if (ipv4)
2479 			ptype->outer_ip_ver = IDPF_RX_PTYPE_OUTER_IPV4;
2480 		else
2481 			ptype->outer_ip_ver = IDPF_RX_PTYPE_OUTER_IPV6;
2482 
2483 		if (frag) {
2484 			ptype->outer_frag = IDPF_RX_PTYPE_FRAG;
2485 			pstate->outer_frag = true;
2486 		}
2487 	} else {
2488 		ptype->tunnel_type = IDPF_RX_PTYPE_TUNNEL_IP_IP;
2489 		pstate->tunnel_state = IDPF_PTYPE_TUNNEL_IP;
2490 
2491 		if (ipv4)
2492 			ptype->tunnel_end_prot =
2493 					IDPF_RX_PTYPE_TUNNEL_END_IPV4;
2494 		else
2495 			ptype->tunnel_end_prot =
2496 					IDPF_RX_PTYPE_TUNNEL_END_IPV6;
2497 
2498 		if (frag)
2499 			ptype->tunnel_end_frag = IDPF_RX_PTYPE_FRAG;
2500 	}
2501 }
2502 
2503 /**
2504  * idpf_send_get_rx_ptype_msg - Send virtchnl for ptype info
2505  * @vport: virtual port data structure
2506  *
2507  * Returns 0 on success, negative on failure.
2508  */
2509 int idpf_send_get_rx_ptype_msg(struct idpf_vport *vport)
2510 {
2511 	struct virtchnl2_get_ptype_info *get_ptype_info __free(kfree) = NULL;
2512 	struct virtchnl2_get_ptype_info *ptype_info __free(kfree) = NULL;
2513 	struct idpf_rx_ptype_decoded *ptype_lkup = vport->rx_ptype_lkup;
2514 	int max_ptype, ptypes_recvd = 0, ptype_offset;
2515 	struct idpf_adapter *adapter = vport->adapter;
2516 	struct idpf_vc_xn_params xn_params = {};
2517 	u16 next_ptype_id = 0;
2518 	ssize_t reply_sz;
2519 	int i, j, k;
2520 
2521 	if (idpf_is_queue_model_split(vport->rxq_model))
2522 		max_ptype = IDPF_RX_MAX_PTYPE;
2523 	else
2524 		max_ptype = IDPF_RX_MAX_BASE_PTYPE;
2525 
2526 	memset(vport->rx_ptype_lkup, 0, sizeof(vport->rx_ptype_lkup));
2527 
2528 	get_ptype_info = kzalloc(sizeof(*get_ptype_info), GFP_KERNEL);
2529 	if (!get_ptype_info)
2530 		return -ENOMEM;
2531 
2532 	ptype_info = kzalloc(IDPF_CTLQ_MAX_BUF_LEN, GFP_KERNEL);
2533 	if (!ptype_info)
2534 		return -ENOMEM;
2535 
2536 	xn_params.vc_op = VIRTCHNL2_OP_GET_PTYPE_INFO;
2537 	xn_params.send_buf.iov_base = get_ptype_info;
2538 	xn_params.send_buf.iov_len = sizeof(*get_ptype_info);
2539 	xn_params.recv_buf.iov_base = ptype_info;
2540 	xn_params.recv_buf.iov_len = IDPF_CTLQ_MAX_BUF_LEN;
2541 	xn_params.timeout_ms = IDPF_VC_XN_DEFAULT_TIMEOUT_MSEC;
2542 
2543 	while (next_ptype_id < max_ptype) {
2544 		get_ptype_info->start_ptype_id = cpu_to_le16(next_ptype_id);
2545 
2546 		if ((next_ptype_id + IDPF_RX_MAX_PTYPES_PER_BUF) > max_ptype)
2547 			get_ptype_info->num_ptypes =
2548 				cpu_to_le16(max_ptype - next_ptype_id);
2549 		else
2550 			get_ptype_info->num_ptypes =
2551 				cpu_to_le16(IDPF_RX_MAX_PTYPES_PER_BUF);
2552 
2553 		reply_sz = idpf_vc_xn_exec(adapter, &xn_params);
2554 		if (reply_sz < 0)
2555 			return reply_sz;
2556 
2557 		if (reply_sz < IDPF_CTLQ_MAX_BUF_LEN)
2558 			return -EIO;
2559 
2560 		ptypes_recvd += le16_to_cpu(ptype_info->num_ptypes);
2561 		if (ptypes_recvd > max_ptype)
2562 			return -EINVAL;
2563 
2564 		next_ptype_id = le16_to_cpu(get_ptype_info->start_ptype_id) +
2565 				le16_to_cpu(get_ptype_info->num_ptypes);
2566 
2567 		ptype_offset = IDPF_RX_PTYPE_HDR_SZ;
2568 
2569 		for (i = 0; i < le16_to_cpu(ptype_info->num_ptypes); i++) {
2570 			struct idpf_ptype_state pstate = { };
2571 			struct virtchnl2_ptype *ptype;
2572 			u16 id;
2573 
2574 			ptype = (struct virtchnl2_ptype *)
2575 					((u8 *)ptype_info + ptype_offset);
2576 
2577 			ptype_offset += IDPF_GET_PTYPE_SIZE(ptype);
2578 			if (ptype_offset > IDPF_CTLQ_MAX_BUF_LEN)
2579 				return -EINVAL;
2580 
2581 			/* 0xFFFF indicates end of ptypes */
2582 			if (le16_to_cpu(ptype->ptype_id_10) ==
2583 							IDPF_INVALID_PTYPE_ID)
2584 				return 0;
2585 
2586 			if (idpf_is_queue_model_split(vport->rxq_model))
2587 				k = le16_to_cpu(ptype->ptype_id_10);
2588 			else
2589 				k = ptype->ptype_id_8;
2590 
2591 			if (ptype->proto_id_count)
2592 				ptype_lkup[k].known = 1;
2593 
2594 			for (j = 0; j < ptype->proto_id_count; j++) {
2595 				id = le16_to_cpu(ptype->proto_id[j]);
2596 				switch (id) {
2597 				case VIRTCHNL2_PROTO_HDR_GRE:
2598 					if (pstate.tunnel_state ==
2599 							IDPF_PTYPE_TUNNEL_IP) {
2600 						ptype_lkup[k].tunnel_type =
2601 						IDPF_RX_PTYPE_TUNNEL_IP_GRENAT;
2602 						pstate.tunnel_state |=
2603 						IDPF_PTYPE_TUNNEL_IP_GRENAT;
2604 					}
2605 					break;
2606 				case VIRTCHNL2_PROTO_HDR_MAC:
2607 					ptype_lkup[k].outer_ip =
2608 						IDPF_RX_PTYPE_OUTER_L2;
2609 					if (pstate.tunnel_state ==
2610 							IDPF_TUN_IP_GRE) {
2611 						ptype_lkup[k].tunnel_type =
2612 						IDPF_RX_PTYPE_TUNNEL_IP_GRENAT_MAC;
2613 						pstate.tunnel_state |=
2614 						IDPF_PTYPE_TUNNEL_IP_GRENAT_MAC;
2615 					}
2616 					break;
2617 				case VIRTCHNL2_PROTO_HDR_IPV4:
2618 					idpf_fill_ptype_lookup(&ptype_lkup[k],
2619 							       &pstate, true,
2620 							       false);
2621 					break;
2622 				case VIRTCHNL2_PROTO_HDR_IPV6:
2623 					idpf_fill_ptype_lookup(&ptype_lkup[k],
2624 							       &pstate, false,
2625 							       false);
2626 					break;
2627 				case VIRTCHNL2_PROTO_HDR_IPV4_FRAG:
2628 					idpf_fill_ptype_lookup(&ptype_lkup[k],
2629 							       &pstate, true,
2630 							       true);
2631 					break;
2632 				case VIRTCHNL2_PROTO_HDR_IPV6_FRAG:
2633 					idpf_fill_ptype_lookup(&ptype_lkup[k],
2634 							       &pstate, false,
2635 							       true);
2636 					break;
2637 				case VIRTCHNL2_PROTO_HDR_UDP:
2638 					ptype_lkup[k].inner_prot =
2639 					IDPF_RX_PTYPE_INNER_PROT_UDP;
2640 					break;
2641 				case VIRTCHNL2_PROTO_HDR_TCP:
2642 					ptype_lkup[k].inner_prot =
2643 					IDPF_RX_PTYPE_INNER_PROT_TCP;
2644 					break;
2645 				case VIRTCHNL2_PROTO_HDR_SCTP:
2646 					ptype_lkup[k].inner_prot =
2647 					IDPF_RX_PTYPE_INNER_PROT_SCTP;
2648 					break;
2649 				case VIRTCHNL2_PROTO_HDR_ICMP:
2650 					ptype_lkup[k].inner_prot =
2651 					IDPF_RX_PTYPE_INNER_PROT_ICMP;
2652 					break;
2653 				case VIRTCHNL2_PROTO_HDR_PAY:
2654 					ptype_lkup[k].payload_layer =
2655 						IDPF_RX_PTYPE_PAYLOAD_LAYER_PAY2;
2656 					break;
2657 				case VIRTCHNL2_PROTO_HDR_ICMPV6:
2658 				case VIRTCHNL2_PROTO_HDR_IPV6_EH:
2659 				case VIRTCHNL2_PROTO_HDR_PRE_MAC:
2660 				case VIRTCHNL2_PROTO_HDR_POST_MAC:
2661 				case VIRTCHNL2_PROTO_HDR_ETHERTYPE:
2662 				case VIRTCHNL2_PROTO_HDR_SVLAN:
2663 				case VIRTCHNL2_PROTO_HDR_CVLAN:
2664 				case VIRTCHNL2_PROTO_HDR_MPLS:
2665 				case VIRTCHNL2_PROTO_HDR_MMPLS:
2666 				case VIRTCHNL2_PROTO_HDR_PTP:
2667 				case VIRTCHNL2_PROTO_HDR_CTRL:
2668 				case VIRTCHNL2_PROTO_HDR_LLDP:
2669 				case VIRTCHNL2_PROTO_HDR_ARP:
2670 				case VIRTCHNL2_PROTO_HDR_ECP:
2671 				case VIRTCHNL2_PROTO_HDR_EAPOL:
2672 				case VIRTCHNL2_PROTO_HDR_PPPOD:
2673 				case VIRTCHNL2_PROTO_HDR_PPPOE:
2674 				case VIRTCHNL2_PROTO_HDR_IGMP:
2675 				case VIRTCHNL2_PROTO_HDR_AH:
2676 				case VIRTCHNL2_PROTO_HDR_ESP:
2677 				case VIRTCHNL2_PROTO_HDR_IKE:
2678 				case VIRTCHNL2_PROTO_HDR_NATT_KEEP:
2679 				case VIRTCHNL2_PROTO_HDR_L2TPV2:
2680 				case VIRTCHNL2_PROTO_HDR_L2TPV2_CONTROL:
2681 				case VIRTCHNL2_PROTO_HDR_L2TPV3:
2682 				case VIRTCHNL2_PROTO_HDR_GTP:
2683 				case VIRTCHNL2_PROTO_HDR_GTP_EH:
2684 				case VIRTCHNL2_PROTO_HDR_GTPCV2:
2685 				case VIRTCHNL2_PROTO_HDR_GTPC_TEID:
2686 				case VIRTCHNL2_PROTO_HDR_GTPU:
2687 				case VIRTCHNL2_PROTO_HDR_GTPU_UL:
2688 				case VIRTCHNL2_PROTO_HDR_GTPU_DL:
2689 				case VIRTCHNL2_PROTO_HDR_ECPRI:
2690 				case VIRTCHNL2_PROTO_HDR_VRRP:
2691 				case VIRTCHNL2_PROTO_HDR_OSPF:
2692 				case VIRTCHNL2_PROTO_HDR_TUN:
2693 				case VIRTCHNL2_PROTO_HDR_NVGRE:
2694 				case VIRTCHNL2_PROTO_HDR_VXLAN:
2695 				case VIRTCHNL2_PROTO_HDR_VXLAN_GPE:
2696 				case VIRTCHNL2_PROTO_HDR_GENEVE:
2697 				case VIRTCHNL2_PROTO_HDR_NSH:
2698 				case VIRTCHNL2_PROTO_HDR_QUIC:
2699 				case VIRTCHNL2_PROTO_HDR_PFCP:
2700 				case VIRTCHNL2_PROTO_HDR_PFCP_NODE:
2701 				case VIRTCHNL2_PROTO_HDR_PFCP_SESSION:
2702 				case VIRTCHNL2_PROTO_HDR_RTP:
2703 				case VIRTCHNL2_PROTO_HDR_NO_PROTO:
2704 					break;
2705 				default:
2706 					break;
2707 				}
2708 			}
2709 		}
2710 	}
2711 
2712 	return 0;
2713 }
2714 
2715 /**
2716  * idpf_send_ena_dis_loopback_msg - Send virtchnl enable/disable loopback
2717  *				    message
2718  * @vport: virtual port data structure
2719  *
2720  * Returns 0 on success, negative on failure.
2721  */
2722 int idpf_send_ena_dis_loopback_msg(struct idpf_vport *vport)
2723 {
2724 	struct idpf_vc_xn_params xn_params = {};
2725 	struct virtchnl2_loopback loopback;
2726 	ssize_t reply_sz;
2727 
2728 	loopback.vport_id = cpu_to_le32(vport->vport_id);
2729 	loopback.enable = idpf_is_feature_ena(vport, NETIF_F_LOOPBACK);
2730 
2731 	xn_params.vc_op = VIRTCHNL2_OP_LOOPBACK;
2732 	xn_params.timeout_ms = IDPF_VC_XN_DEFAULT_TIMEOUT_MSEC;
2733 	xn_params.send_buf.iov_base = &loopback;
2734 	xn_params.send_buf.iov_len = sizeof(loopback);
2735 	reply_sz = idpf_vc_xn_exec(vport->adapter, &xn_params);
2736 
2737 	return reply_sz < 0 ? reply_sz : 0;
2738 }
2739 
2740 /**
2741  * idpf_find_ctlq - Given a type and id, find ctlq info
2742  * @hw: hardware struct
2743  * @type: type of ctrlq to find
2744  * @id: ctlq id to find
2745  *
2746  * Returns pointer to found ctlq info struct, NULL otherwise.
2747  */
2748 static struct idpf_ctlq_info *idpf_find_ctlq(struct idpf_hw *hw,
2749 					     enum idpf_ctlq_type type, int id)
2750 {
2751 	struct idpf_ctlq_info *cq, *tmp;
2752 
2753 	list_for_each_entry_safe(cq, tmp, &hw->cq_list_head, cq_list)
2754 		if (cq->q_id == id && cq->cq_type == type)
2755 			return cq;
2756 
2757 	return NULL;
2758 }
2759 
2760 /**
2761  * idpf_init_dflt_mbx - Setup default mailbox parameters and make request
2762  * @adapter: adapter info struct
2763  *
2764  * Returns 0 on success, negative otherwise
2765  */
2766 int idpf_init_dflt_mbx(struct idpf_adapter *adapter)
2767 {
2768 	struct idpf_ctlq_create_info ctlq_info[] = {
2769 		{
2770 			.type = IDPF_CTLQ_TYPE_MAILBOX_TX,
2771 			.id = IDPF_DFLT_MBX_ID,
2772 			.len = IDPF_DFLT_MBX_Q_LEN,
2773 			.buf_size = IDPF_CTLQ_MAX_BUF_LEN
2774 		},
2775 		{
2776 			.type = IDPF_CTLQ_TYPE_MAILBOX_RX,
2777 			.id = IDPF_DFLT_MBX_ID,
2778 			.len = IDPF_DFLT_MBX_Q_LEN,
2779 			.buf_size = IDPF_CTLQ_MAX_BUF_LEN
2780 		}
2781 	};
2782 	struct idpf_hw *hw = &adapter->hw;
2783 	int err;
2784 
2785 	adapter->dev_ops.reg_ops.ctlq_reg_init(ctlq_info);
2786 
2787 	err = idpf_ctlq_init(hw, IDPF_NUM_DFLT_MBX_Q, ctlq_info);
2788 	if (err)
2789 		return err;
2790 
2791 	hw->asq = idpf_find_ctlq(hw, IDPF_CTLQ_TYPE_MAILBOX_TX,
2792 				 IDPF_DFLT_MBX_ID);
2793 	hw->arq = idpf_find_ctlq(hw, IDPF_CTLQ_TYPE_MAILBOX_RX,
2794 				 IDPF_DFLT_MBX_ID);
2795 
2796 	if (!hw->asq || !hw->arq) {
2797 		idpf_ctlq_deinit(hw);
2798 
2799 		return -ENOENT;
2800 	}
2801 
2802 	adapter->state = __IDPF_VER_CHECK;
2803 
2804 	return 0;
2805 }
2806 
2807 /**
2808  * idpf_deinit_dflt_mbx - Free up ctlqs setup
2809  * @adapter: Driver specific private data structure
2810  */
2811 void idpf_deinit_dflt_mbx(struct idpf_adapter *adapter)
2812 {
2813 	if (adapter->hw.arq && adapter->hw.asq) {
2814 		idpf_mb_clean(adapter);
2815 		idpf_ctlq_deinit(&adapter->hw);
2816 	}
2817 	adapter->hw.arq = NULL;
2818 	adapter->hw.asq = NULL;
2819 }
2820 
2821 /**
2822  * idpf_vport_params_buf_rel - Release memory for MailBox resources
2823  * @adapter: Driver specific private data structure
2824  *
2825  * Will release memory to hold the vport parameters received on MailBox
2826  */
2827 static void idpf_vport_params_buf_rel(struct idpf_adapter *adapter)
2828 {
2829 	kfree(adapter->vport_params_recvd);
2830 	adapter->vport_params_recvd = NULL;
2831 	kfree(adapter->vport_params_reqd);
2832 	adapter->vport_params_reqd = NULL;
2833 	kfree(adapter->vport_ids);
2834 	adapter->vport_ids = NULL;
2835 }
2836 
2837 /**
2838  * idpf_vport_params_buf_alloc - Allocate memory for MailBox resources
2839  * @adapter: Driver specific private data structure
2840  *
2841  * Will alloc memory to hold the vport parameters received on MailBox
2842  */
2843 static int idpf_vport_params_buf_alloc(struct idpf_adapter *adapter)
2844 {
2845 	u16 num_max_vports = idpf_get_max_vports(adapter);
2846 
2847 	adapter->vport_params_reqd = kcalloc(num_max_vports,
2848 					     sizeof(*adapter->vport_params_reqd),
2849 					     GFP_KERNEL);
2850 	if (!adapter->vport_params_reqd)
2851 		return -ENOMEM;
2852 
2853 	adapter->vport_params_recvd = kcalloc(num_max_vports,
2854 					      sizeof(*adapter->vport_params_recvd),
2855 					      GFP_KERNEL);
2856 	if (!adapter->vport_params_recvd)
2857 		goto err_mem;
2858 
2859 	adapter->vport_ids = kcalloc(num_max_vports, sizeof(u32), GFP_KERNEL);
2860 	if (!adapter->vport_ids)
2861 		goto err_mem;
2862 
2863 	if (adapter->vport_config)
2864 		return 0;
2865 
2866 	adapter->vport_config = kcalloc(num_max_vports,
2867 					sizeof(*adapter->vport_config),
2868 					GFP_KERNEL);
2869 	if (!adapter->vport_config)
2870 		goto err_mem;
2871 
2872 	return 0;
2873 
2874 err_mem:
2875 	idpf_vport_params_buf_rel(adapter);
2876 
2877 	return -ENOMEM;
2878 }
2879 
2880 /**
2881  * idpf_vc_core_init - Initialize state machine and get driver specific
2882  * resources
2883  * @adapter: Driver specific private structure
2884  *
2885  * This function will initialize the state machine and request all necessary
2886  * resources required by the device driver. Once the state machine is
2887  * initialized, allocate memory to store vport specific information and also
2888  * requests required interrupts.
2889  *
2890  * Returns 0 on success, -EAGAIN function will get called again,
2891  * otherwise negative on failure.
2892  */
2893 int idpf_vc_core_init(struct idpf_adapter *adapter)
2894 {
2895 	int task_delay = 30;
2896 	u16 num_max_vports;
2897 	int err = 0;
2898 
2899 	if (!adapter->vcxn_mngr) {
2900 		adapter->vcxn_mngr = kzalloc(sizeof(*adapter->vcxn_mngr), GFP_KERNEL);
2901 		if (!adapter->vcxn_mngr) {
2902 			err = -ENOMEM;
2903 			goto init_failed;
2904 		}
2905 	}
2906 	idpf_vc_xn_init(adapter->vcxn_mngr);
2907 
2908 	while (adapter->state != __IDPF_INIT_SW) {
2909 		switch (adapter->state) {
2910 		case __IDPF_VER_CHECK:
2911 			err = idpf_send_ver_msg(adapter);
2912 			switch (err) {
2913 			case 0:
2914 				/* success, move state machine forward */
2915 				adapter->state = __IDPF_GET_CAPS;
2916 				fallthrough;
2917 			case -EAGAIN:
2918 				goto restart;
2919 			default:
2920 				/* Something bad happened, try again but only a
2921 				 * few times.
2922 				 */
2923 				goto init_failed;
2924 			}
2925 		case __IDPF_GET_CAPS:
2926 			err = idpf_send_get_caps_msg(adapter);
2927 			if (err)
2928 				goto init_failed;
2929 			adapter->state = __IDPF_INIT_SW;
2930 			break;
2931 		default:
2932 			dev_err(&adapter->pdev->dev, "Device is in bad state: %d\n",
2933 				adapter->state);
2934 			err = -EINVAL;
2935 			goto init_failed;
2936 		}
2937 		break;
2938 restart:
2939 		/* Give enough time before proceeding further with
2940 		 * state machine
2941 		 */
2942 		msleep(task_delay);
2943 	}
2944 
2945 	pci_sriov_set_totalvfs(adapter->pdev, idpf_get_max_vfs(adapter));
2946 	num_max_vports = idpf_get_max_vports(adapter);
2947 	adapter->max_vports = num_max_vports;
2948 	adapter->vports = kcalloc(num_max_vports, sizeof(*adapter->vports),
2949 				  GFP_KERNEL);
2950 	if (!adapter->vports)
2951 		return -ENOMEM;
2952 
2953 	if (!adapter->netdevs) {
2954 		adapter->netdevs = kcalloc(num_max_vports,
2955 					   sizeof(struct net_device *),
2956 					   GFP_KERNEL);
2957 		if (!adapter->netdevs) {
2958 			err = -ENOMEM;
2959 			goto err_netdev_alloc;
2960 		}
2961 	}
2962 
2963 	err = idpf_vport_params_buf_alloc(adapter);
2964 	if (err) {
2965 		dev_err(&adapter->pdev->dev, "Failed to alloc vport params buffer: %d\n",
2966 			err);
2967 		goto err_netdev_alloc;
2968 	}
2969 
2970 	/* Start the mailbox task before requesting vectors. This will ensure
2971 	 * vector information response from mailbox is handled
2972 	 */
2973 	queue_delayed_work(adapter->mbx_wq, &adapter->mbx_task, 0);
2974 
2975 	queue_delayed_work(adapter->serv_wq, &adapter->serv_task,
2976 			   msecs_to_jiffies(5 * (adapter->pdev->devfn & 0x07)));
2977 
2978 	err = idpf_intr_req(adapter);
2979 	if (err) {
2980 		dev_err(&adapter->pdev->dev, "failed to enable interrupt vectors: %d\n",
2981 			err);
2982 		goto err_intr_req;
2983 	}
2984 
2985 	idpf_init_avail_queues(adapter);
2986 
2987 	/* Skew the delay for init tasks for each function based on fn number
2988 	 * to prevent every function from making the same call simultaneously.
2989 	 */
2990 	queue_delayed_work(adapter->init_wq, &adapter->init_task,
2991 			   msecs_to_jiffies(5 * (adapter->pdev->devfn & 0x07)));
2992 
2993 	set_bit(IDPF_VC_CORE_INIT, adapter->flags);
2994 
2995 	return 0;
2996 
2997 err_intr_req:
2998 	cancel_delayed_work_sync(&adapter->serv_task);
2999 	cancel_delayed_work_sync(&adapter->mbx_task);
3000 	idpf_vport_params_buf_rel(adapter);
3001 err_netdev_alloc:
3002 	kfree(adapter->vports);
3003 	adapter->vports = NULL;
3004 	return err;
3005 
3006 init_failed:
3007 	/* Don't retry if we're trying to go down, just bail. */
3008 	if (test_bit(IDPF_REMOVE_IN_PROG, adapter->flags))
3009 		return err;
3010 
3011 	if (++adapter->mb_wait_count > IDPF_MB_MAX_ERR) {
3012 		dev_err(&adapter->pdev->dev, "Failed to establish mailbox communications with hardware\n");
3013 
3014 		return -EFAULT;
3015 	}
3016 	/* If it reached here, it is possible that mailbox queue initialization
3017 	 * register writes might not have taken effect. Retry to initialize
3018 	 * the mailbox again
3019 	 */
3020 	adapter->state = __IDPF_VER_CHECK;
3021 	if (adapter->vcxn_mngr)
3022 		idpf_vc_xn_shutdown(adapter->vcxn_mngr);
3023 	idpf_deinit_dflt_mbx(adapter);
3024 	set_bit(IDPF_HR_DRV_LOAD, adapter->flags);
3025 	queue_delayed_work(adapter->vc_event_wq, &adapter->vc_event_task,
3026 			   msecs_to_jiffies(task_delay));
3027 
3028 	return -EAGAIN;
3029 }
3030 
3031 /**
3032  * idpf_vc_core_deinit - Device deinit routine
3033  * @adapter: Driver specific private structure
3034  *
3035  */
3036 void idpf_vc_core_deinit(struct idpf_adapter *adapter)
3037 {
3038 	if (!test_bit(IDPF_VC_CORE_INIT, adapter->flags))
3039 		return;
3040 
3041 	idpf_vc_xn_shutdown(adapter->vcxn_mngr);
3042 	idpf_deinit_task(adapter);
3043 	idpf_intr_rel(adapter);
3044 
3045 	cancel_delayed_work_sync(&adapter->serv_task);
3046 	cancel_delayed_work_sync(&adapter->mbx_task);
3047 
3048 	idpf_vport_params_buf_rel(adapter);
3049 
3050 	kfree(adapter->vports);
3051 	adapter->vports = NULL;
3052 
3053 	clear_bit(IDPF_VC_CORE_INIT, adapter->flags);
3054 }
3055 
3056 /**
3057  * idpf_vport_alloc_vec_indexes - Get relative vector indexes
3058  * @vport: virtual port data struct
3059  *
3060  * This function requests the vector information required for the vport and
3061  * stores the vector indexes received from the 'global vector distribution'
3062  * in the vport's queue vectors array.
3063  *
3064  * Return 0 on success, error on failure
3065  */
3066 int idpf_vport_alloc_vec_indexes(struct idpf_vport *vport)
3067 {
3068 	struct idpf_vector_info vec_info;
3069 	int num_alloc_vecs;
3070 
3071 	vec_info.num_curr_vecs = vport->num_q_vectors;
3072 	vec_info.num_req_vecs = max(vport->num_txq, vport->num_rxq);
3073 	vec_info.default_vport = vport->default_vport;
3074 	vec_info.index = vport->idx;
3075 
3076 	num_alloc_vecs = idpf_req_rel_vector_indexes(vport->adapter,
3077 						     vport->q_vector_idxs,
3078 						     &vec_info);
3079 	if (num_alloc_vecs <= 0) {
3080 		dev_err(&vport->adapter->pdev->dev, "Vector distribution failed: %d\n",
3081 			num_alloc_vecs);
3082 		return -EINVAL;
3083 	}
3084 
3085 	vport->num_q_vectors = num_alloc_vecs;
3086 
3087 	return 0;
3088 }
3089 
3090 /**
3091  * idpf_vport_init - Initialize virtual port
3092  * @vport: virtual port to be initialized
3093  * @max_q: vport max queue info
3094  *
3095  * Will initialize vport with the info received through MB earlier
3096  */
3097 void idpf_vport_init(struct idpf_vport *vport, struct idpf_vport_max_q *max_q)
3098 {
3099 	struct idpf_adapter *adapter = vport->adapter;
3100 	struct virtchnl2_create_vport *vport_msg;
3101 	struct idpf_vport_config *vport_config;
3102 	u16 tx_itr[] = {2, 8, 64, 128, 256};
3103 	u16 rx_itr[] = {2, 8, 32, 96, 128};
3104 	struct idpf_rss_data *rss_data;
3105 	u16 idx = vport->idx;
3106 
3107 	vport_config = adapter->vport_config[idx];
3108 	rss_data = &vport_config->user_config.rss_data;
3109 	vport_msg = adapter->vport_params_recvd[idx];
3110 
3111 	vport_config->max_q.max_txq = max_q->max_txq;
3112 	vport_config->max_q.max_rxq = max_q->max_rxq;
3113 	vport_config->max_q.max_complq = max_q->max_complq;
3114 	vport_config->max_q.max_bufq = max_q->max_bufq;
3115 
3116 	vport->txq_model = le16_to_cpu(vport_msg->txq_model);
3117 	vport->rxq_model = le16_to_cpu(vport_msg->rxq_model);
3118 	vport->vport_type = le16_to_cpu(vport_msg->vport_type);
3119 	vport->vport_id = le32_to_cpu(vport_msg->vport_id);
3120 
3121 	rss_data->rss_key_size = min_t(u16, NETDEV_RSS_KEY_LEN,
3122 				       le16_to_cpu(vport_msg->rss_key_size));
3123 	rss_data->rss_lut_size = le16_to_cpu(vport_msg->rss_lut_size);
3124 
3125 	ether_addr_copy(vport->default_mac_addr, vport_msg->default_mac_addr);
3126 	vport->max_mtu = le16_to_cpu(vport_msg->max_mtu) - IDPF_PACKET_HDR_PAD;
3127 
3128 	/* Initialize Tx and Rx profiles for Dynamic Interrupt Moderation */
3129 	memcpy(vport->rx_itr_profile, rx_itr, IDPF_DIM_PROFILE_SLOTS);
3130 	memcpy(vport->tx_itr_profile, tx_itr, IDPF_DIM_PROFILE_SLOTS);
3131 
3132 	idpf_vport_set_hsplit(vport, ETHTOOL_TCP_DATA_SPLIT_ENABLED);
3133 
3134 	idpf_vport_init_num_qs(vport, vport_msg);
3135 	idpf_vport_calc_num_q_desc(vport);
3136 	idpf_vport_calc_num_q_groups(vport);
3137 	idpf_vport_alloc_vec_indexes(vport);
3138 
3139 	vport->crc_enable = adapter->crc_enable;
3140 }
3141 
3142 /**
3143  * idpf_get_vec_ids - Initialize vector id from Mailbox parameters
3144  * @adapter: adapter structure to get the mailbox vector id
3145  * @vecids: Array of vector ids
3146  * @num_vecids: number of vector ids
3147  * @chunks: vector ids received over mailbox
3148  *
3149  * Will initialize the mailbox vector id which is received from the
3150  * get capabilities and data queue vector ids with ids received as
3151  * mailbox parameters.
3152  * Returns number of ids filled
3153  */
3154 int idpf_get_vec_ids(struct idpf_adapter *adapter,
3155 		     u16 *vecids, int num_vecids,
3156 		     struct virtchnl2_vector_chunks *chunks)
3157 {
3158 	u16 num_chunks = le16_to_cpu(chunks->num_vchunks);
3159 	int num_vecid_filled = 0;
3160 	int i, j;
3161 
3162 	vecids[num_vecid_filled] = adapter->mb_vector.v_idx;
3163 	num_vecid_filled++;
3164 
3165 	for (j = 0; j < num_chunks; j++) {
3166 		struct virtchnl2_vector_chunk *chunk;
3167 		u16 start_vecid, num_vec;
3168 
3169 		chunk = &chunks->vchunks[j];
3170 		num_vec = le16_to_cpu(chunk->num_vectors);
3171 		start_vecid = le16_to_cpu(chunk->start_vector_id);
3172 
3173 		for (i = 0; i < num_vec; i++) {
3174 			if ((num_vecid_filled + i) < num_vecids) {
3175 				vecids[num_vecid_filled + i] = start_vecid;
3176 				start_vecid++;
3177 			} else {
3178 				break;
3179 			}
3180 		}
3181 		num_vecid_filled = num_vecid_filled + i;
3182 	}
3183 
3184 	return num_vecid_filled;
3185 }
3186 
3187 /**
3188  * idpf_vport_get_queue_ids - Initialize queue id from Mailbox parameters
3189  * @qids: Array of queue ids
3190  * @num_qids: number of queue ids
3191  * @q_type: queue model
3192  * @chunks: queue ids received over mailbox
3193  *
3194  * Will initialize all queue ids with ids received as mailbox parameters
3195  * Returns number of ids filled
3196  */
3197 static int idpf_vport_get_queue_ids(u32 *qids, int num_qids, u16 q_type,
3198 				    struct virtchnl2_queue_reg_chunks *chunks)
3199 {
3200 	u16 num_chunks = le16_to_cpu(chunks->num_chunks);
3201 	u32 num_q_id_filled = 0, i;
3202 	u32 start_q_id, num_q;
3203 
3204 	while (num_chunks--) {
3205 		struct virtchnl2_queue_reg_chunk *chunk;
3206 
3207 		chunk = &chunks->chunks[num_chunks];
3208 		if (le32_to_cpu(chunk->type) != q_type)
3209 			continue;
3210 
3211 		num_q = le32_to_cpu(chunk->num_queues);
3212 		start_q_id = le32_to_cpu(chunk->start_queue_id);
3213 
3214 		for (i = 0; i < num_q; i++) {
3215 			if ((num_q_id_filled + i) < num_qids) {
3216 				qids[num_q_id_filled + i] = start_q_id;
3217 				start_q_id++;
3218 			} else {
3219 				break;
3220 			}
3221 		}
3222 		num_q_id_filled = num_q_id_filled + i;
3223 	}
3224 
3225 	return num_q_id_filled;
3226 }
3227 
3228 /**
3229  * __idpf_vport_queue_ids_init - Initialize queue ids from Mailbox parameters
3230  * @vport: virtual port for which the queues ids are initialized
3231  * @qids: queue ids
3232  * @num_qids: number of queue ids
3233  * @q_type: type of queue
3234  *
3235  * Will initialize all queue ids with ids received as mailbox
3236  * parameters. Returns number of queue ids initialized.
3237  */
3238 static int __idpf_vport_queue_ids_init(struct idpf_vport *vport,
3239 				       const u32 *qids,
3240 				       int num_qids,
3241 				       u32 q_type)
3242 {
3243 	struct idpf_queue *q;
3244 	int i, j, k = 0;
3245 
3246 	switch (q_type) {
3247 	case VIRTCHNL2_QUEUE_TYPE_TX:
3248 		for (i = 0; i < vport->num_txq_grp; i++) {
3249 			struct idpf_txq_group *tx_qgrp = &vport->txq_grps[i];
3250 
3251 			for (j = 0; j < tx_qgrp->num_txq && k < num_qids; j++, k++) {
3252 				tx_qgrp->txqs[j]->q_id = qids[k];
3253 				tx_qgrp->txqs[j]->q_type =
3254 					VIRTCHNL2_QUEUE_TYPE_TX;
3255 			}
3256 		}
3257 		break;
3258 	case VIRTCHNL2_QUEUE_TYPE_RX:
3259 		for (i = 0; i < vport->num_rxq_grp; i++) {
3260 			struct idpf_rxq_group *rx_qgrp = &vport->rxq_grps[i];
3261 			u16 num_rxq;
3262 
3263 			if (idpf_is_queue_model_split(vport->rxq_model))
3264 				num_rxq = rx_qgrp->splitq.num_rxq_sets;
3265 			else
3266 				num_rxq = rx_qgrp->singleq.num_rxq;
3267 
3268 			for (j = 0; j < num_rxq && k < num_qids; j++, k++) {
3269 				if (idpf_is_queue_model_split(vport->rxq_model))
3270 					q = &rx_qgrp->splitq.rxq_sets[j]->rxq;
3271 				else
3272 					q = rx_qgrp->singleq.rxqs[j];
3273 				q->q_id = qids[k];
3274 				q->q_type = VIRTCHNL2_QUEUE_TYPE_RX;
3275 			}
3276 		}
3277 		break;
3278 	case VIRTCHNL2_QUEUE_TYPE_TX_COMPLETION:
3279 		for (i = 0; i < vport->num_txq_grp && k < num_qids; i++, k++) {
3280 			struct idpf_txq_group *tx_qgrp = &vport->txq_grps[i];
3281 
3282 			tx_qgrp->complq->q_id = qids[k];
3283 			tx_qgrp->complq->q_type =
3284 				VIRTCHNL2_QUEUE_TYPE_TX_COMPLETION;
3285 		}
3286 		break;
3287 	case VIRTCHNL2_QUEUE_TYPE_RX_BUFFER:
3288 		for (i = 0; i < vport->num_rxq_grp; i++) {
3289 			struct idpf_rxq_group *rx_qgrp = &vport->rxq_grps[i];
3290 			u8 num_bufqs = vport->num_bufqs_per_qgrp;
3291 
3292 			for (j = 0; j < num_bufqs && k < num_qids; j++, k++) {
3293 				q = &rx_qgrp->splitq.bufq_sets[j].bufq;
3294 				q->q_id = qids[k];
3295 				q->q_type = VIRTCHNL2_QUEUE_TYPE_RX_BUFFER;
3296 			}
3297 		}
3298 		break;
3299 	default:
3300 		break;
3301 	}
3302 
3303 	return k;
3304 }
3305 
3306 /**
3307  * idpf_vport_queue_ids_init - Initialize queue ids from Mailbox parameters
3308  * @vport: virtual port for which the queues ids are initialized
3309  *
3310  * Will initialize all queue ids with ids received as mailbox parameters.
3311  * Returns 0 on success, negative if all the queues are not initialized.
3312  */
3313 int idpf_vport_queue_ids_init(struct idpf_vport *vport)
3314 {
3315 	struct virtchnl2_create_vport *vport_params;
3316 	struct virtchnl2_queue_reg_chunks *chunks;
3317 	struct idpf_vport_config *vport_config;
3318 	u16 vport_idx = vport->idx;
3319 	int num_ids, err = 0;
3320 	u16 q_type;
3321 	u32 *qids;
3322 
3323 	vport_config = vport->adapter->vport_config[vport_idx];
3324 	if (vport_config->req_qs_chunks) {
3325 		struct virtchnl2_add_queues *vc_aq =
3326 			(struct virtchnl2_add_queues *)vport_config->req_qs_chunks;
3327 		chunks = &vc_aq->chunks;
3328 	} else {
3329 		vport_params = vport->adapter->vport_params_recvd[vport_idx];
3330 		chunks = &vport_params->chunks;
3331 	}
3332 
3333 	qids = kcalloc(IDPF_MAX_QIDS, sizeof(u32), GFP_KERNEL);
3334 	if (!qids)
3335 		return -ENOMEM;
3336 
3337 	num_ids = idpf_vport_get_queue_ids(qids, IDPF_MAX_QIDS,
3338 					   VIRTCHNL2_QUEUE_TYPE_TX,
3339 					   chunks);
3340 	if (num_ids < vport->num_txq) {
3341 		err = -EINVAL;
3342 		goto mem_rel;
3343 	}
3344 	num_ids = __idpf_vport_queue_ids_init(vport, qids, num_ids,
3345 					      VIRTCHNL2_QUEUE_TYPE_TX);
3346 	if (num_ids < vport->num_txq) {
3347 		err = -EINVAL;
3348 		goto mem_rel;
3349 	}
3350 
3351 	num_ids = idpf_vport_get_queue_ids(qids, IDPF_MAX_QIDS,
3352 					   VIRTCHNL2_QUEUE_TYPE_RX,
3353 					   chunks);
3354 	if (num_ids < vport->num_rxq) {
3355 		err = -EINVAL;
3356 		goto mem_rel;
3357 	}
3358 	num_ids = __idpf_vport_queue_ids_init(vport, qids, num_ids,
3359 					      VIRTCHNL2_QUEUE_TYPE_RX);
3360 	if (num_ids < vport->num_rxq) {
3361 		err = -EINVAL;
3362 		goto mem_rel;
3363 	}
3364 
3365 	if (!idpf_is_queue_model_split(vport->txq_model))
3366 		goto check_rxq;
3367 
3368 	q_type = VIRTCHNL2_QUEUE_TYPE_TX_COMPLETION;
3369 	num_ids = idpf_vport_get_queue_ids(qids, IDPF_MAX_QIDS, q_type, chunks);
3370 	if (num_ids < vport->num_complq) {
3371 		err = -EINVAL;
3372 		goto mem_rel;
3373 	}
3374 	num_ids = __idpf_vport_queue_ids_init(vport, qids, num_ids, q_type);
3375 	if (num_ids < vport->num_complq) {
3376 		err = -EINVAL;
3377 		goto mem_rel;
3378 	}
3379 
3380 check_rxq:
3381 	if (!idpf_is_queue_model_split(vport->rxq_model))
3382 		goto mem_rel;
3383 
3384 	q_type = VIRTCHNL2_QUEUE_TYPE_RX_BUFFER;
3385 	num_ids = idpf_vport_get_queue_ids(qids, IDPF_MAX_QIDS, q_type, chunks);
3386 	if (num_ids < vport->num_bufq) {
3387 		err = -EINVAL;
3388 		goto mem_rel;
3389 	}
3390 	num_ids = __idpf_vport_queue_ids_init(vport, qids, num_ids, q_type);
3391 	if (num_ids < vport->num_bufq)
3392 		err = -EINVAL;
3393 
3394 mem_rel:
3395 	kfree(qids);
3396 
3397 	return err;
3398 }
3399 
3400 /**
3401  * idpf_vport_adjust_qs - Adjust to new requested queues
3402  * @vport: virtual port data struct
3403  *
3404  * Renegotiate queues.  Returns 0 on success, negative on failure.
3405  */
3406 int idpf_vport_adjust_qs(struct idpf_vport *vport)
3407 {
3408 	struct virtchnl2_create_vport vport_msg;
3409 	int err;
3410 
3411 	vport_msg.txq_model = cpu_to_le16(vport->txq_model);
3412 	vport_msg.rxq_model = cpu_to_le16(vport->rxq_model);
3413 	err = idpf_vport_calc_total_qs(vport->adapter, vport->idx, &vport_msg,
3414 				       NULL);
3415 	if (err)
3416 		return err;
3417 
3418 	idpf_vport_init_num_qs(vport, &vport_msg);
3419 	idpf_vport_calc_num_q_groups(vport);
3420 
3421 	return 0;
3422 }
3423 
3424 /**
3425  * idpf_is_capability_ena - Default implementation of capability checking
3426  * @adapter: Private data struct
3427  * @all: all or one flag
3428  * @field: caps field to check for flags
3429  * @flag: flag to check
3430  *
3431  * Return true if all capabilities are supported, false otherwise
3432  */
3433 bool idpf_is_capability_ena(struct idpf_adapter *adapter, bool all,
3434 			    enum idpf_cap_field field, u64 flag)
3435 {
3436 	u8 *caps = (u8 *)&adapter->caps;
3437 	u32 *cap_field;
3438 
3439 	if (!caps)
3440 		return false;
3441 
3442 	if (field == IDPF_BASE_CAPS)
3443 		return false;
3444 
3445 	cap_field = (u32 *)(caps + field);
3446 
3447 	if (all)
3448 		return (*cap_field & flag) == flag;
3449 	else
3450 		return !!(*cap_field & flag);
3451 }
3452 
3453 /**
3454  * idpf_get_vport_id: Get vport id
3455  * @vport: virtual port structure
3456  *
3457  * Return vport id from the adapter persistent data
3458  */
3459 u32 idpf_get_vport_id(struct idpf_vport *vport)
3460 {
3461 	struct virtchnl2_create_vport *vport_msg;
3462 
3463 	vport_msg = vport->adapter->vport_params_recvd[vport->idx];
3464 
3465 	return le32_to_cpu(vport_msg->vport_id);
3466 }
3467 
3468 /**
3469  * idpf_mac_filter_async_handler - Async callback for mac filters
3470  * @adapter: private data struct
3471  * @xn: transaction for message
3472  * @ctlq_msg: received message
3473  *
3474  * In some scenarios driver can't sleep and wait for a reply (e.g.: stack is
3475  * holding rtnl_lock) when adding a new mac filter. It puts us in a difficult
3476  * situation to deal with errors returned on the reply. The best we can
3477  * ultimately do is remove it from our list of mac filters and report the
3478  * error.
3479  */
3480 static int idpf_mac_filter_async_handler(struct idpf_adapter *adapter,
3481 					 struct idpf_vc_xn *xn,
3482 					 const struct idpf_ctlq_msg *ctlq_msg)
3483 {
3484 	struct virtchnl2_mac_addr_list *ma_list;
3485 	struct idpf_vport_config *vport_config;
3486 	struct virtchnl2_mac_addr *mac_addr;
3487 	struct idpf_mac_filter *f, *tmp;
3488 	struct list_head *ma_list_head;
3489 	struct idpf_vport *vport;
3490 	u16 num_entries;
3491 	int i;
3492 
3493 	/* if success we're done, we're only here if something bad happened */
3494 	if (!ctlq_msg->cookie.mbx.chnl_retval)
3495 		return 0;
3496 
3497 	/* make sure at least struct is there */
3498 	if (xn->reply_sz < sizeof(*ma_list))
3499 		goto invalid_payload;
3500 
3501 	ma_list = ctlq_msg->ctx.indirect.payload->va;
3502 	mac_addr = ma_list->mac_addr_list;
3503 	num_entries = le16_to_cpu(ma_list->num_mac_addr);
3504 	/* we should have received a buffer at least this big */
3505 	if (xn->reply_sz < struct_size(ma_list, mac_addr_list, num_entries))
3506 		goto invalid_payload;
3507 
3508 	vport = idpf_vid_to_vport(adapter, le32_to_cpu(ma_list->vport_id));
3509 	if (!vport)
3510 		goto invalid_payload;
3511 
3512 	vport_config = adapter->vport_config[le32_to_cpu(ma_list->vport_id)];
3513 	ma_list_head = &vport_config->user_config.mac_filter_list;
3514 
3515 	/* We can't do much to reconcile bad filters at this point, however we
3516 	 * should at least remove them from our list one way or the other so we
3517 	 * have some idea what good filters we have.
3518 	 */
3519 	spin_lock_bh(&vport_config->mac_filter_list_lock);
3520 	list_for_each_entry_safe(f, tmp, ma_list_head, list)
3521 		for (i = 0; i < num_entries; i++)
3522 			if (ether_addr_equal(mac_addr[i].addr, f->macaddr))
3523 				list_del(&f->list);
3524 	spin_unlock_bh(&vport_config->mac_filter_list_lock);
3525 	dev_err_ratelimited(&adapter->pdev->dev, "Received error sending MAC filter request (op %d)\n",
3526 			    xn->vc_op);
3527 
3528 	return 0;
3529 
3530 invalid_payload:
3531 	dev_err_ratelimited(&adapter->pdev->dev, "Received invalid MAC filter payload (op %d) (len %zd)\n",
3532 			    xn->vc_op, xn->reply_sz);
3533 
3534 	return -EINVAL;
3535 }
3536 
3537 /**
3538  * idpf_add_del_mac_filters - Add/del mac filters
3539  * @vport: Virtual port data structure
3540  * @np: Netdev private structure
3541  * @add: Add or delete flag
3542  * @async: Don't wait for return message
3543  *
3544  * Returns 0 on success, error on failure.
3545  **/
3546 int idpf_add_del_mac_filters(struct idpf_vport *vport,
3547 			     struct idpf_netdev_priv *np,
3548 			     bool add, bool async)
3549 {
3550 	struct virtchnl2_mac_addr_list *ma_list __free(kfree) = NULL;
3551 	struct virtchnl2_mac_addr *mac_addr __free(kfree) = NULL;
3552 	struct idpf_adapter *adapter = np->adapter;
3553 	struct idpf_vc_xn_params xn_params = {};
3554 	struct idpf_vport_config *vport_config;
3555 	u32 num_msgs, total_filters = 0;
3556 	struct idpf_mac_filter *f;
3557 	ssize_t reply_sz;
3558 	int i = 0, k;
3559 
3560 	xn_params.vc_op = add ? VIRTCHNL2_OP_ADD_MAC_ADDR :
3561 				VIRTCHNL2_OP_DEL_MAC_ADDR;
3562 	xn_params.timeout_ms = IDPF_VC_XN_DEFAULT_TIMEOUT_MSEC;
3563 	xn_params.async = async;
3564 	xn_params.async_handler = idpf_mac_filter_async_handler;
3565 
3566 	vport_config = adapter->vport_config[np->vport_idx];
3567 	spin_lock_bh(&vport_config->mac_filter_list_lock);
3568 
3569 	/* Find the number of newly added filters */
3570 	list_for_each_entry(f, &vport_config->user_config.mac_filter_list,
3571 			    list) {
3572 		if (add && f->add)
3573 			total_filters++;
3574 		else if (!add && f->remove)
3575 			total_filters++;
3576 	}
3577 
3578 	if (!total_filters) {
3579 		spin_unlock_bh(&vport_config->mac_filter_list_lock);
3580 
3581 		return 0;
3582 	}
3583 
3584 	/* Fill all the new filters into virtchannel message */
3585 	mac_addr = kcalloc(total_filters, sizeof(struct virtchnl2_mac_addr),
3586 			   GFP_ATOMIC);
3587 	if (!mac_addr) {
3588 		spin_unlock_bh(&vport_config->mac_filter_list_lock);
3589 
3590 		return -ENOMEM;
3591 	}
3592 
3593 	list_for_each_entry(f, &vport_config->user_config.mac_filter_list,
3594 			    list) {
3595 		if (add && f->add) {
3596 			ether_addr_copy(mac_addr[i].addr, f->macaddr);
3597 			i++;
3598 			f->add = false;
3599 			if (i == total_filters)
3600 				break;
3601 		}
3602 		if (!add && f->remove) {
3603 			ether_addr_copy(mac_addr[i].addr, f->macaddr);
3604 			i++;
3605 			f->remove = false;
3606 			if (i == total_filters)
3607 				break;
3608 		}
3609 	}
3610 
3611 	spin_unlock_bh(&vport_config->mac_filter_list_lock);
3612 
3613 	/* Chunk up the filters into multiple messages to avoid
3614 	 * sending a control queue message buffer that is too large
3615 	 */
3616 	num_msgs = DIV_ROUND_UP(total_filters, IDPF_NUM_FILTERS_PER_MSG);
3617 
3618 	for (i = 0, k = 0; i < num_msgs; i++) {
3619 		u32 entries_size, buf_size, num_entries;
3620 
3621 		num_entries = min_t(u32, total_filters,
3622 				    IDPF_NUM_FILTERS_PER_MSG);
3623 		entries_size = sizeof(struct virtchnl2_mac_addr) * num_entries;
3624 		buf_size = struct_size(ma_list, mac_addr_list, num_entries);
3625 
3626 		if (!ma_list || num_entries != IDPF_NUM_FILTERS_PER_MSG) {
3627 			kfree(ma_list);
3628 			ma_list = kzalloc(buf_size, GFP_ATOMIC);
3629 			if (!ma_list)
3630 				return -ENOMEM;
3631 		} else {
3632 			memset(ma_list, 0, buf_size);
3633 		}
3634 
3635 		ma_list->vport_id = cpu_to_le32(np->vport_id);
3636 		ma_list->num_mac_addr = cpu_to_le16(num_entries);
3637 		memcpy(ma_list->mac_addr_list, &mac_addr[k], entries_size);
3638 
3639 		xn_params.send_buf.iov_base = ma_list;
3640 		xn_params.send_buf.iov_len = buf_size;
3641 		reply_sz = idpf_vc_xn_exec(adapter, &xn_params);
3642 		if (reply_sz < 0)
3643 			return reply_sz;
3644 
3645 		k += num_entries;
3646 		total_filters -= num_entries;
3647 	}
3648 
3649 	return 0;
3650 }
3651 
3652 /**
3653  * idpf_set_promiscuous - set promiscuous and send message to mailbox
3654  * @adapter: Driver specific private structure
3655  * @config_data: Vport specific config data
3656  * @vport_id: Vport identifier
3657  *
3658  * Request to enable promiscuous mode for the vport. Message is sent
3659  * asynchronously and won't wait for response.  Returns 0 on success, negative
3660  * on failure;
3661  */
3662 int idpf_set_promiscuous(struct idpf_adapter *adapter,
3663 			 struct idpf_vport_user_config_data *config_data,
3664 			 u32 vport_id)
3665 {
3666 	struct idpf_vc_xn_params xn_params = {};
3667 	struct virtchnl2_promisc_info vpi;
3668 	ssize_t reply_sz;
3669 	u16 flags = 0;
3670 
3671 	if (test_bit(__IDPF_PROMISC_UC, config_data->user_flags))
3672 		flags |= VIRTCHNL2_UNICAST_PROMISC;
3673 	if (test_bit(__IDPF_PROMISC_MC, config_data->user_flags))
3674 		flags |= VIRTCHNL2_MULTICAST_PROMISC;
3675 
3676 	vpi.vport_id = cpu_to_le32(vport_id);
3677 	vpi.flags = cpu_to_le16(flags);
3678 
3679 	xn_params.vc_op = VIRTCHNL2_OP_CONFIG_PROMISCUOUS_MODE;
3680 	xn_params.timeout_ms = IDPF_VC_XN_DEFAULT_TIMEOUT_MSEC;
3681 	xn_params.send_buf.iov_base = &vpi;
3682 	xn_params.send_buf.iov_len = sizeof(vpi);
3683 	/* setting promiscuous is only ever done asynchronously */
3684 	xn_params.async = true;
3685 	reply_sz = idpf_vc_xn_exec(adapter, &xn_params);
3686 
3687 	return reply_sz < 0 ? reply_sz : 0;
3688 }
3689