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