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