1 // SPDX-License-Identifier: GPL-2.0-only
2 /* Copyright (C) 2023 Intel Corporation */
4 #include <net/libeth/rx.h>
7 #include "idpf_virtchnl.h"
9 #define IDPF_VC_XN_MIN_TIMEOUT_MSEC 2000
10 #define IDPF_VC_XN_DEFAULT_TIMEOUT_MSEC (60 * 1000)
11 #define IDPF_VC_XN_IDX_M GENMASK(7, 0)
12 #define IDPF_VC_XN_SALT_M GENMASK(15, 8)
13 #define IDPF_VC_XN_RING_LEN U8_MAX
16 * enum idpf_vc_xn_state - Virtchnl transaction status
17 * @IDPF_VC_XN_IDLE: not expecting a reply, ready to be used
18 * @IDPF_VC_XN_WAITING: expecting a reply, not yet received
19 * @IDPF_VC_XN_COMPLETED_SUCCESS: a reply was expected and received,
21 * @IDPF_VC_XN_COMPLETED_FAILED: a reply was expected and received, but there
22 * was an error, buffer not updated
23 * @IDPF_VC_XN_SHUTDOWN: transaction object cannot be used, VC torn down
24 * @IDPF_VC_XN_ASYNC: transaction sent asynchronously and doesn't have the
25 * return context; a callback may be provided to handle
28 enum idpf_vc_xn_state {
31 IDPF_VC_XN_COMPLETED_SUCCESS,
32 IDPF_VC_XN_COMPLETED_FAILED,
38 /* Callback for asynchronous messages */
39 typedef int (*async_vc_cb) (struct idpf_adapter *, struct idpf_vc_xn *,
40 const struct idpf_ctlq_msg *);
43 * struct idpf_vc_xn - Data structure representing virtchnl transactions
44 * @completed: virtchnl event loop uses that to signal when a reply is
45 * available, uses kernel completion API
46 * @state: virtchnl event loop stores the data below, protected by the
48 * @reply_sz: Original size of reply, may be > reply_buf.iov_len; it will be
49 * truncated on its way to the receiver thread according to
51 * @reply: Reference to the buffer(s) where the reply data should be written
52 * to. May be 0-length (then NULL address permitted) if the reply data
54 * @async_handler: if sent asynchronously, a callback can be provided to handle
55 * the reply when it's received
56 * @vc_op: corresponding opcode sent with this transaction
57 * @idx: index used as retrieval on reply receive, used for cookie
58 * @salt: changed every message to make unique, used for cookie
61 struct completion completed;
62 enum idpf_vc_xn_state state;
65 async_vc_cb async_handler;
72 * struct idpf_vc_xn_params - Parameters for executing transaction
73 * @send_buf: kvec for send buffer
74 * @recv_buf: kvec for recv buffer, may be NULL, must then have zero length
75 * @timeout_ms: timeout to wait for reply
76 * @async: send message asynchronously, will not wait on completion
77 * @async_handler: If sent asynchronously, optional callback handler. The user
78 * must be careful when using async handlers as the memory for
79 * the recv_buf _cannot_ be on stack if this is async.
80 * @vc_op: virtchnl op to send
82 struct idpf_vc_xn_params {
87 async_vc_cb async_handler;
92 * struct idpf_vc_xn_manager - Manager for tracking transactions
93 * @ring: backing and lookup for transactions
94 * @free_xn_bm: bitmap for free transactions
95 * @xn_bm_lock: make bitmap access synchronous where necessary
96 * @salt: used to make cookie unique every message
98 struct idpf_vc_xn_manager {
99 struct idpf_vc_xn ring[IDPF_VC_XN_RING_LEN];
100 DECLARE_BITMAP(free_xn_bm, IDPF_VC_XN_RING_LEN);
101 spinlock_t xn_bm_lock;
106 * idpf_vid_to_vport - Translate vport id to vport pointer
107 * @adapter: private data struct
108 * @v_id: vport id to translate
110 * Returns vport matching v_id, NULL if not found.
113 struct idpf_vport *idpf_vid_to_vport(struct idpf_adapter *adapter, u32 v_id)
115 u16 num_max_vports = idpf_get_max_vports(adapter);
118 for (i = 0; i < num_max_vports; i++)
119 if (adapter->vport_ids[i] == v_id)
120 return adapter->vports[i];
126 * idpf_handle_event_link - Handle link event message
127 * @adapter: private data struct
128 * @v2e: virtchnl event message
130 static void idpf_handle_event_link(struct idpf_adapter *adapter,
131 const struct virtchnl2_event *v2e)
133 struct idpf_netdev_priv *np;
134 struct idpf_vport *vport;
136 vport = idpf_vid_to_vport(adapter, le32_to_cpu(v2e->vport_id));
138 dev_err_ratelimited(&adapter->pdev->dev, "Failed to find vport_id %d for link event\n",
142 np = netdev_priv(vport->netdev);
144 vport->link_speed_mbps = le32_to_cpu(v2e->link_speed);
146 if (vport->link_up == v2e->link_status)
149 vport->link_up = v2e->link_status;
151 if (np->state != __IDPF_VPORT_UP)
154 if (vport->link_up) {
155 netif_tx_start_all_queues(vport->netdev);
156 netif_carrier_on(vport->netdev);
158 netif_tx_stop_all_queues(vport->netdev);
159 netif_carrier_off(vport->netdev);
164 * idpf_recv_event_msg - Receive virtchnl event message
165 * @adapter: Driver specific private structure
166 * @ctlq_msg: message to copy from
168 * Receive virtchnl event message
170 static void idpf_recv_event_msg(struct idpf_adapter *adapter,
171 struct idpf_ctlq_msg *ctlq_msg)
173 int payload_size = ctlq_msg->ctx.indirect.payload->size;
174 struct virtchnl2_event *v2e;
177 if (payload_size < sizeof(*v2e)) {
178 dev_err_ratelimited(&adapter->pdev->dev, "Failed to receive valid payload for event msg (op %d len %d)\n",
179 ctlq_msg->cookie.mbx.chnl_opcode,
184 v2e = (struct virtchnl2_event *)ctlq_msg->ctx.indirect.payload->va;
185 event = le32_to_cpu(v2e->event);
188 case VIRTCHNL2_EVENT_LINK_CHANGE:
189 idpf_handle_event_link(adapter, v2e);
192 dev_err(&adapter->pdev->dev,
193 "Unknown event %d from PF\n", event);
199 * idpf_mb_clean - Reclaim the send mailbox queue entries
200 * @adapter: Driver specific private structure
202 * Reclaim the send mailbox queue entries to be used to send further messages
204 * Returns 0 on success, negative on failure
206 static int idpf_mb_clean(struct idpf_adapter *adapter)
208 u16 i, num_q_msg = IDPF_DFLT_MBX_Q_LEN;
209 struct idpf_ctlq_msg **q_msg;
210 struct idpf_dma_mem *dma_mem;
213 q_msg = kcalloc(num_q_msg, sizeof(struct idpf_ctlq_msg *), GFP_ATOMIC);
217 err = idpf_ctlq_clean_sq(adapter->hw.asq, &num_q_msg, q_msg);
221 for (i = 0; i < num_q_msg; i++) {
224 dma_mem = q_msg[i]->ctx.indirect.payload;
226 dma_free_coherent(&adapter->pdev->dev, dma_mem->size,
227 dma_mem->va, dma_mem->pa);
239 * idpf_send_mb_msg - Send message over mailbox
240 * @adapter: Driver specific private structure
241 * @op: virtchnl opcode
242 * @msg_size: size of the payload
243 * @msg: pointer to buffer holding the payload
244 * @cookie: unique SW generated cookie per message
246 * Will prepare the control queue message and initiates the send api
248 * Returns 0 on success, negative on failure
250 int idpf_send_mb_msg(struct idpf_adapter *adapter, u32 op,
251 u16 msg_size, u8 *msg, u16 cookie)
253 struct idpf_ctlq_msg *ctlq_msg;
254 struct idpf_dma_mem *dma_mem;
257 /* If we are here and a reset is detected nothing much can be
258 * done. This thread should silently abort and expected to
259 * be corrected with a new run either by user or driver
262 if (idpf_is_reset_detected(adapter))
265 err = idpf_mb_clean(adapter);
269 ctlq_msg = kzalloc(sizeof(*ctlq_msg), GFP_ATOMIC);
273 dma_mem = kzalloc(sizeof(*dma_mem), GFP_ATOMIC);
279 ctlq_msg->opcode = idpf_mbq_opc_send_msg_to_cp;
280 ctlq_msg->func_id = 0;
281 ctlq_msg->data_len = msg_size;
282 ctlq_msg->cookie.mbx.chnl_opcode = op;
283 ctlq_msg->cookie.mbx.chnl_retval = 0;
284 dma_mem->size = IDPF_CTLQ_MAX_BUF_LEN;
285 dma_mem->va = dma_alloc_coherent(&adapter->pdev->dev, dma_mem->size,
286 &dma_mem->pa, GFP_ATOMIC);
289 goto dma_alloc_error;
292 /* It's possible we're just sending an opcode but no buffer */
294 memcpy(dma_mem->va, msg, msg_size);
295 ctlq_msg->ctx.indirect.payload = dma_mem;
296 ctlq_msg->ctx.sw_cookie.data = cookie;
298 err = idpf_ctlq_send(&adapter->hw, adapter->hw.asq, 1, ctlq_msg);
305 dma_free_coherent(&adapter->pdev->dev, dma_mem->size, dma_mem->va,
315 /* API for virtchnl "transaction" support ("xn" for short).
317 * We are reusing the completion lock to serialize the accesses to the
318 * transaction state for simplicity, but it could be its own separate synchro
319 * as well. For now, this API is only used from within a workqueue context;
320 * raw_spin_lock() is enough.
323 * idpf_vc_xn_lock - Request exclusive access to vc transaction
324 * @xn: struct idpf_vc_xn* to access
326 #define idpf_vc_xn_lock(xn) \
327 raw_spin_lock(&(xn)->completed.wait.lock)
330 * idpf_vc_xn_unlock - Release exclusive access to vc transaction
331 * @xn: struct idpf_vc_xn* to access
333 #define idpf_vc_xn_unlock(xn) \
334 raw_spin_unlock(&(xn)->completed.wait.lock)
337 * idpf_vc_xn_release_bufs - Release reference to reply buffer(s) and
338 * reset the transaction state.
339 * @xn: struct idpf_vc_xn to update
341 static void idpf_vc_xn_release_bufs(struct idpf_vc_xn *xn)
343 xn->reply.iov_base = NULL;
344 xn->reply.iov_len = 0;
346 if (xn->state != IDPF_VC_XN_SHUTDOWN)
347 xn->state = IDPF_VC_XN_IDLE;
351 * idpf_vc_xn_init - Initialize virtchnl transaction object
352 * @vcxn_mngr: pointer to vc transaction manager struct
354 static void idpf_vc_xn_init(struct idpf_vc_xn_manager *vcxn_mngr)
358 spin_lock_init(&vcxn_mngr->xn_bm_lock);
360 for (i = 0; i < ARRAY_SIZE(vcxn_mngr->ring); i++) {
361 struct idpf_vc_xn *xn = &vcxn_mngr->ring[i];
363 xn->state = IDPF_VC_XN_IDLE;
365 idpf_vc_xn_release_bufs(xn);
366 init_completion(&xn->completed);
369 bitmap_fill(vcxn_mngr->free_xn_bm, IDPF_VC_XN_RING_LEN);
373 * idpf_vc_xn_shutdown - Uninitialize virtchnl transaction object
374 * @vcxn_mngr: pointer to vc transaction manager struct
376 * All waiting threads will be woken-up and their transaction aborted. Further
377 * operations on that object will fail.
379 static void idpf_vc_xn_shutdown(struct idpf_vc_xn_manager *vcxn_mngr)
383 spin_lock_bh(&vcxn_mngr->xn_bm_lock);
384 bitmap_zero(vcxn_mngr->free_xn_bm, IDPF_VC_XN_RING_LEN);
385 spin_unlock_bh(&vcxn_mngr->xn_bm_lock);
387 for (i = 0; i < ARRAY_SIZE(vcxn_mngr->ring); i++) {
388 struct idpf_vc_xn *xn = &vcxn_mngr->ring[i];
391 xn->state = IDPF_VC_XN_SHUTDOWN;
392 idpf_vc_xn_release_bufs(xn);
393 idpf_vc_xn_unlock(xn);
394 complete_all(&xn->completed);
399 * idpf_vc_xn_pop_free - Pop a free transaction from free list
400 * @vcxn_mngr: transaction manager to pop from
402 * Returns NULL if no free transactions
405 struct idpf_vc_xn *idpf_vc_xn_pop_free(struct idpf_vc_xn_manager *vcxn_mngr)
407 struct idpf_vc_xn *xn = NULL;
408 unsigned long free_idx;
410 spin_lock_bh(&vcxn_mngr->xn_bm_lock);
411 free_idx = find_first_bit(vcxn_mngr->free_xn_bm, IDPF_VC_XN_RING_LEN);
412 if (free_idx == IDPF_VC_XN_RING_LEN)
415 clear_bit(free_idx, vcxn_mngr->free_xn_bm);
416 xn = &vcxn_mngr->ring[free_idx];
417 xn->salt = vcxn_mngr->salt++;
420 spin_unlock_bh(&vcxn_mngr->xn_bm_lock);
426 * idpf_vc_xn_push_free - Push a free transaction to free list
427 * @vcxn_mngr: transaction manager to push to
428 * @xn: transaction to push
430 static void idpf_vc_xn_push_free(struct idpf_vc_xn_manager *vcxn_mngr,
431 struct idpf_vc_xn *xn)
433 idpf_vc_xn_release_bufs(xn);
434 set_bit(xn->idx, vcxn_mngr->free_xn_bm);
438 * idpf_vc_xn_exec - Perform a send/recv virtchnl transaction
439 * @adapter: driver specific private structure with vcxn_mngr
440 * @params: parameters for this particular transaction including
441 * -vc_op: virtchannel operation to send
442 * -send_buf: kvec iov for send buf and len
443 * -recv_buf: kvec iov for recv buf and len (ignored if NULL)
444 * -timeout_ms: timeout waiting for a reply (milliseconds)
445 * -async: don't wait for message reply, will lose caller context
446 * -async_handler: callback to handle async replies
448 * @returns >= 0 for success, the size of the initial reply (may or may not be
449 * >= @recv_buf.iov_len, but we never overflow @@recv_buf_iov_base). < 0 for
452 static ssize_t idpf_vc_xn_exec(struct idpf_adapter *adapter,
453 const struct idpf_vc_xn_params *params)
455 const struct kvec *send_buf = ¶ms->send_buf;
456 struct idpf_vc_xn *xn;
460 xn = idpf_vc_xn_pop_free(adapter->vcxn_mngr);
461 /* no free transactions available */
466 if (xn->state == IDPF_VC_XN_SHUTDOWN) {
469 } else if (xn->state != IDPF_VC_XN_IDLE) {
470 /* We're just going to clobber this transaction even though
471 * it's not IDLE. If we don't reuse it we could theoretically
472 * eventually leak all the free transactions and not be able to
473 * send any messages. At least this way we make an attempt to
474 * remain functional even though something really bad is
475 * happening that's corrupting what was supposed to be free
478 WARN_ONCE(1, "There should only be idle transactions in free list (idx %d op %d)\n",
482 xn->reply = params->recv_buf;
484 xn->state = params->async ? IDPF_VC_XN_ASYNC : IDPF_VC_XN_WAITING;
485 xn->vc_op = params->vc_op;
486 xn->async_handler = params->async_handler;
487 idpf_vc_xn_unlock(xn);
490 reinit_completion(&xn->completed);
491 cookie = FIELD_PREP(IDPF_VC_XN_SALT_M, xn->salt) |
492 FIELD_PREP(IDPF_VC_XN_IDX_M, xn->idx);
494 retval = idpf_send_mb_msg(adapter, params->vc_op,
495 send_buf->iov_len, send_buf->iov_base,
499 goto release_and_unlock;
505 wait_for_completion_timeout(&xn->completed,
506 msecs_to_jiffies(params->timeout_ms));
508 /* No need to check the return value; we check the final state of the
509 * transaction below. It's possible the transaction actually gets more
510 * timeout than specified if we get preempted here but after
511 * wait_for_completion_timeout returns. This should be non-issue
516 case IDPF_VC_XN_SHUTDOWN:
519 case IDPF_VC_XN_WAITING:
520 dev_notice_ratelimited(&adapter->pdev->dev, "Transaction timed-out (op %d, %dms)\n",
521 params->vc_op, params->timeout_ms);
524 case IDPF_VC_XN_COMPLETED_SUCCESS:
525 retval = xn->reply_sz;
527 case IDPF_VC_XN_COMPLETED_FAILED:
528 dev_notice_ratelimited(&adapter->pdev->dev, "Transaction failed (op %d)\n",
540 idpf_vc_xn_push_free(adapter->vcxn_mngr, xn);
541 /* If we receive a VC reply after here, it will be dropped. */
543 idpf_vc_xn_unlock(xn);
549 * idpf_vc_xn_forward_async - Handle async reply receives
550 * @adapter: private data struct
551 * @xn: transaction to handle
552 * @ctlq_msg: corresponding ctlq_msg
554 * For async sends we're going to lose the caller's context so, if an
555 * async_handler was provided, it can deal with the reply, otherwise we'll just
556 * check and report if there is an error.
559 idpf_vc_xn_forward_async(struct idpf_adapter *adapter, struct idpf_vc_xn *xn,
560 const struct idpf_ctlq_msg *ctlq_msg)
564 if (ctlq_msg->cookie.mbx.chnl_opcode != xn->vc_op) {
565 dev_err_ratelimited(&adapter->pdev->dev, "Async message opcode does not match transaction opcode (msg: %d) (xn: %d)\n",
566 ctlq_msg->cookie.mbx.chnl_opcode, xn->vc_op);
572 if (xn->async_handler) {
573 err = xn->async_handler(adapter, xn, ctlq_msg);
577 if (ctlq_msg->cookie.mbx.chnl_retval) {
579 dev_err_ratelimited(&adapter->pdev->dev, "Async message failure (op %d)\n",
580 ctlq_msg->cookie.mbx.chnl_opcode);
585 idpf_vc_xn_push_free(adapter->vcxn_mngr, xn);
591 * idpf_vc_xn_forward_reply - copy a reply back to receiving thread
592 * @adapter: driver specific private structure with vcxn_mngr
593 * @ctlq_msg: controlq message to send back to receiving thread
596 idpf_vc_xn_forward_reply(struct idpf_adapter *adapter,
597 const struct idpf_ctlq_msg *ctlq_msg)
599 const void *payload = NULL;
600 size_t payload_size = 0;
601 struct idpf_vc_xn *xn;
607 msg_info = ctlq_msg->ctx.sw_cookie.data;
608 xn_idx = FIELD_GET(IDPF_VC_XN_IDX_M, msg_info);
609 if (xn_idx >= ARRAY_SIZE(adapter->vcxn_mngr->ring)) {
610 dev_err_ratelimited(&adapter->pdev->dev, "Out of bounds cookie received: %02x\n",
614 xn = &adapter->vcxn_mngr->ring[xn_idx];
615 salt = FIELD_GET(IDPF_VC_XN_SALT_M, msg_info);
616 if (xn->salt != salt) {
617 dev_err_ratelimited(&adapter->pdev->dev, "Transaction salt does not match (%02x != %02x)\n",
624 case IDPF_VC_XN_WAITING:
627 case IDPF_VC_XN_IDLE:
628 dev_err_ratelimited(&adapter->pdev->dev, "Unexpected or belated VC reply (op %d)\n",
629 ctlq_msg->cookie.mbx.chnl_opcode);
632 case IDPF_VC_XN_SHUTDOWN:
633 /* ENXIO is a bit special here as the recv msg loop uses that
634 * know if it should stop trying to clean the ring if we lost
635 * the virtchnl. We need to stop playing with registers and
640 case IDPF_VC_XN_ASYNC:
641 err = idpf_vc_xn_forward_async(adapter, xn, ctlq_msg);
642 idpf_vc_xn_unlock(xn);
645 dev_err_ratelimited(&adapter->pdev->dev, "Overwriting VC reply (op %d)\n",
646 ctlq_msg->cookie.mbx.chnl_opcode);
651 if (ctlq_msg->cookie.mbx.chnl_opcode != xn->vc_op) {
652 dev_err_ratelimited(&adapter->pdev->dev, "Message opcode does not match transaction opcode (msg: %d) (xn: %d)\n",
653 ctlq_msg->cookie.mbx.chnl_opcode, xn->vc_op);
655 xn->state = IDPF_VC_XN_COMPLETED_FAILED;
660 if (ctlq_msg->cookie.mbx.chnl_retval) {
662 xn->state = IDPF_VC_XN_COMPLETED_FAILED;
667 if (ctlq_msg->data_len) {
668 payload = ctlq_msg->ctx.indirect.payload->va;
669 payload_size = ctlq_msg->ctx.indirect.payload->size;
672 xn->reply_sz = payload_size;
673 xn->state = IDPF_VC_XN_COMPLETED_SUCCESS;
675 if (xn->reply.iov_base && xn->reply.iov_len && payload_size)
676 memcpy(xn->reply.iov_base, payload,
677 min_t(size_t, xn->reply.iov_len, payload_size));
680 idpf_vc_xn_unlock(xn);
681 /* we _cannot_ hold lock while calling complete */
682 complete(&xn->completed);
688 * idpf_recv_mb_msg - Receive message over mailbox
689 * @adapter: Driver specific private structure
691 * Will receive control queue message and posts the receive buffer. Returns 0
692 * on success and negative on failure.
694 int idpf_recv_mb_msg(struct idpf_adapter *adapter)
696 struct idpf_ctlq_msg ctlq_msg;
697 struct idpf_dma_mem *dma_mem;
702 /* This will get <= num_recv messages and output how many
703 * actually received on num_recv.
706 err = idpf_ctlq_recv(adapter->hw.arq, &num_recv, &ctlq_msg);
707 if (err || !num_recv)
710 if (ctlq_msg.data_len) {
711 dma_mem = ctlq_msg.ctx.indirect.payload;
717 if (ctlq_msg.cookie.mbx.chnl_opcode == VIRTCHNL2_OP_EVENT)
718 idpf_recv_event_msg(adapter, &ctlq_msg);
720 err = idpf_vc_xn_forward_reply(adapter, &ctlq_msg);
722 post_err = idpf_ctlq_post_rx_buffs(&adapter->hw,
724 &num_recv, &dma_mem);
726 /* If post failed clear the only buffer we supplied */
729 dmam_free_coherent(&adapter->pdev->dev,
730 dma_mem->size, dma_mem->va,
735 /* virtchnl trying to shutdown, stop cleaning */
744 * idpf_wait_for_marker_event - wait for software marker response
745 * @vport: virtual port data structure
747 * Returns 0 success, negative on failure.
749 static int idpf_wait_for_marker_event(struct idpf_vport *vport)
754 for (i = 0; i < vport->num_txq; i++)
755 idpf_queue_set(SW_MARKER, vport->txqs[i]);
757 event = wait_event_timeout(vport->sw_marker_wq,
758 test_and_clear_bit(IDPF_VPORT_SW_MARKER,
760 msecs_to_jiffies(500));
762 for (i = 0; i < vport->num_txq; i++)
763 idpf_queue_clear(POLL_MODE, vport->txqs[i]);
768 dev_warn(&vport->adapter->pdev->dev, "Failed to receive marker packets\n");
774 * idpf_send_ver_msg - send virtchnl version message
775 * @adapter: Driver specific private structure
777 * Send virtchnl version message. Returns 0 on success, negative on failure.
779 static int idpf_send_ver_msg(struct idpf_adapter *adapter)
781 struct idpf_vc_xn_params xn_params = {};
782 struct virtchnl2_version_info vvi;
787 if (adapter->virt_ver_maj) {
788 vvi.major = cpu_to_le32(adapter->virt_ver_maj);
789 vvi.minor = cpu_to_le32(adapter->virt_ver_min);
791 vvi.major = cpu_to_le32(IDPF_VIRTCHNL_VERSION_MAJOR);
792 vvi.minor = cpu_to_le32(IDPF_VIRTCHNL_VERSION_MINOR);
795 xn_params.vc_op = VIRTCHNL2_OP_VERSION;
796 xn_params.send_buf.iov_base = &vvi;
797 xn_params.send_buf.iov_len = sizeof(vvi);
798 xn_params.recv_buf = xn_params.send_buf;
799 xn_params.timeout_ms = IDPF_VC_XN_DEFAULT_TIMEOUT_MSEC;
801 reply_sz = idpf_vc_xn_exec(adapter, &xn_params);
804 if (reply_sz < sizeof(vvi))
807 major = le32_to_cpu(vvi.major);
808 minor = le32_to_cpu(vvi.minor);
810 if (major > IDPF_VIRTCHNL_VERSION_MAJOR) {
811 dev_warn(&adapter->pdev->dev, "Virtchnl major version greater than supported\n");
815 if (major == IDPF_VIRTCHNL_VERSION_MAJOR &&
816 minor > IDPF_VIRTCHNL_VERSION_MINOR)
817 dev_warn(&adapter->pdev->dev, "Virtchnl minor version didn't match\n");
819 /* If we have a mismatch, resend version to update receiver on what
820 * version we will use.
822 if (!adapter->virt_ver_maj &&
823 major != IDPF_VIRTCHNL_VERSION_MAJOR &&
824 minor != IDPF_VIRTCHNL_VERSION_MINOR)
827 adapter->virt_ver_maj = major;
828 adapter->virt_ver_min = minor;
834 * idpf_send_get_caps_msg - Send virtchnl get capabilities message
835 * @adapter: Driver specific private structure
837 * Send virtchl get capabilities message. Returns 0 on success, negative on
840 static int idpf_send_get_caps_msg(struct idpf_adapter *adapter)
842 struct virtchnl2_get_capabilities caps = {};
843 struct idpf_vc_xn_params xn_params = {};
847 cpu_to_le32(VIRTCHNL2_CAP_TX_CSUM_L3_IPV4 |
848 VIRTCHNL2_CAP_TX_CSUM_L4_IPV4_TCP |
849 VIRTCHNL2_CAP_TX_CSUM_L4_IPV4_UDP |
850 VIRTCHNL2_CAP_TX_CSUM_L4_IPV4_SCTP |
851 VIRTCHNL2_CAP_TX_CSUM_L4_IPV6_TCP |
852 VIRTCHNL2_CAP_TX_CSUM_L4_IPV6_UDP |
853 VIRTCHNL2_CAP_TX_CSUM_L4_IPV6_SCTP |
854 VIRTCHNL2_CAP_RX_CSUM_L3_IPV4 |
855 VIRTCHNL2_CAP_RX_CSUM_L4_IPV4_TCP |
856 VIRTCHNL2_CAP_RX_CSUM_L4_IPV4_UDP |
857 VIRTCHNL2_CAP_RX_CSUM_L4_IPV4_SCTP |
858 VIRTCHNL2_CAP_RX_CSUM_L4_IPV6_TCP |
859 VIRTCHNL2_CAP_RX_CSUM_L4_IPV6_UDP |
860 VIRTCHNL2_CAP_RX_CSUM_L4_IPV6_SCTP |
861 VIRTCHNL2_CAP_TX_CSUM_L3_SINGLE_TUNNEL |
862 VIRTCHNL2_CAP_RX_CSUM_L3_SINGLE_TUNNEL |
863 VIRTCHNL2_CAP_TX_CSUM_L4_SINGLE_TUNNEL |
864 VIRTCHNL2_CAP_RX_CSUM_L4_SINGLE_TUNNEL |
865 VIRTCHNL2_CAP_RX_CSUM_GENERIC);
868 cpu_to_le32(VIRTCHNL2_CAP_SEG_IPV4_TCP |
869 VIRTCHNL2_CAP_SEG_IPV4_UDP |
870 VIRTCHNL2_CAP_SEG_IPV4_SCTP |
871 VIRTCHNL2_CAP_SEG_IPV6_TCP |
872 VIRTCHNL2_CAP_SEG_IPV6_UDP |
873 VIRTCHNL2_CAP_SEG_IPV6_SCTP |
874 VIRTCHNL2_CAP_SEG_TX_SINGLE_TUNNEL);
877 cpu_to_le64(VIRTCHNL2_CAP_RSS_IPV4_TCP |
878 VIRTCHNL2_CAP_RSS_IPV4_UDP |
879 VIRTCHNL2_CAP_RSS_IPV4_SCTP |
880 VIRTCHNL2_CAP_RSS_IPV4_OTHER |
881 VIRTCHNL2_CAP_RSS_IPV6_TCP |
882 VIRTCHNL2_CAP_RSS_IPV6_UDP |
883 VIRTCHNL2_CAP_RSS_IPV6_SCTP |
884 VIRTCHNL2_CAP_RSS_IPV6_OTHER);
887 cpu_to_le32(VIRTCHNL2_CAP_RX_HSPLIT_AT_L4V4 |
888 VIRTCHNL2_CAP_RX_HSPLIT_AT_L4V6);
891 cpu_to_le32(VIRTCHNL2_CAP_RSC_IPV4_TCP |
892 VIRTCHNL2_CAP_RSC_IPV6_TCP);
895 cpu_to_le64(VIRTCHNL2_CAP_SRIOV |
896 VIRTCHNL2_CAP_MACFILTER |
897 VIRTCHNL2_CAP_SPLITQ_QSCHED |
898 VIRTCHNL2_CAP_PROMISC |
899 VIRTCHNL2_CAP_LOOPBACK);
901 xn_params.vc_op = VIRTCHNL2_OP_GET_CAPS;
902 xn_params.send_buf.iov_base = ∩︀
903 xn_params.send_buf.iov_len = sizeof(caps);
904 xn_params.recv_buf.iov_base = &adapter->caps;
905 xn_params.recv_buf.iov_len = sizeof(adapter->caps);
906 xn_params.timeout_ms = IDPF_VC_XN_DEFAULT_TIMEOUT_MSEC;
908 reply_sz = idpf_vc_xn_exec(adapter, &xn_params);
911 if (reply_sz < sizeof(adapter->caps))
918 * idpf_vport_alloc_max_qs - Allocate max queues for a vport
919 * @adapter: Driver specific private structure
920 * @max_q: vport max queue structure
922 int idpf_vport_alloc_max_qs(struct idpf_adapter *adapter,
923 struct idpf_vport_max_q *max_q)
925 struct idpf_avail_queue_info *avail_queues = &adapter->avail_queues;
926 struct virtchnl2_get_capabilities *caps = &adapter->caps;
927 u16 default_vports = idpf_get_default_vports(adapter);
928 int max_rx_q, max_tx_q;
930 mutex_lock(&adapter->queue_lock);
932 max_rx_q = le16_to_cpu(caps->max_rx_q) / default_vports;
933 max_tx_q = le16_to_cpu(caps->max_tx_q) / default_vports;
934 if (adapter->num_alloc_vports < default_vports) {
935 max_q->max_rxq = min_t(u16, max_rx_q, IDPF_MAX_Q);
936 max_q->max_txq = min_t(u16, max_tx_q, IDPF_MAX_Q);
938 max_q->max_rxq = IDPF_MIN_Q;
939 max_q->max_txq = IDPF_MIN_Q;
941 max_q->max_bufq = max_q->max_rxq * IDPF_MAX_BUFQS_PER_RXQ_GRP;
942 max_q->max_complq = max_q->max_txq;
944 if (avail_queues->avail_rxq < max_q->max_rxq ||
945 avail_queues->avail_txq < max_q->max_txq ||
946 avail_queues->avail_bufq < max_q->max_bufq ||
947 avail_queues->avail_complq < max_q->max_complq) {
948 mutex_unlock(&adapter->queue_lock);
953 avail_queues->avail_rxq -= max_q->max_rxq;
954 avail_queues->avail_txq -= max_q->max_txq;
955 avail_queues->avail_bufq -= max_q->max_bufq;
956 avail_queues->avail_complq -= max_q->max_complq;
958 mutex_unlock(&adapter->queue_lock);
964 * idpf_vport_dealloc_max_qs - Deallocate max queues of a vport
965 * @adapter: Driver specific private structure
966 * @max_q: vport max queue structure
968 void idpf_vport_dealloc_max_qs(struct idpf_adapter *adapter,
969 struct idpf_vport_max_q *max_q)
971 struct idpf_avail_queue_info *avail_queues;
973 mutex_lock(&adapter->queue_lock);
974 avail_queues = &adapter->avail_queues;
976 avail_queues->avail_rxq += max_q->max_rxq;
977 avail_queues->avail_txq += max_q->max_txq;
978 avail_queues->avail_bufq += max_q->max_bufq;
979 avail_queues->avail_complq += max_q->max_complq;
981 mutex_unlock(&adapter->queue_lock);
985 * idpf_init_avail_queues - Initialize available queues on the device
986 * @adapter: Driver specific private structure
988 static void idpf_init_avail_queues(struct idpf_adapter *adapter)
990 struct idpf_avail_queue_info *avail_queues = &adapter->avail_queues;
991 struct virtchnl2_get_capabilities *caps = &adapter->caps;
993 avail_queues->avail_rxq = le16_to_cpu(caps->max_rx_q);
994 avail_queues->avail_txq = le16_to_cpu(caps->max_tx_q);
995 avail_queues->avail_bufq = le16_to_cpu(caps->max_rx_bufq);
996 avail_queues->avail_complq = le16_to_cpu(caps->max_tx_complq);
1000 * idpf_get_reg_intr_vecs - Get vector queue register offset
1001 * @vport: virtual port structure
1002 * @reg_vals: Register offsets to store in
1004 * Returns number of registers that got populated
1006 int idpf_get_reg_intr_vecs(struct idpf_vport *vport,
1007 struct idpf_vec_regs *reg_vals)
1009 struct virtchnl2_vector_chunks *chunks;
1010 struct idpf_vec_regs reg_val;
1011 u16 num_vchunks, num_vec;
1012 int num_regs = 0, i, j;
1014 chunks = &vport->adapter->req_vec_chunks->vchunks;
1015 num_vchunks = le16_to_cpu(chunks->num_vchunks);
1017 for (j = 0; j < num_vchunks; j++) {
1018 struct virtchnl2_vector_chunk *chunk;
1019 u32 dynctl_reg_spacing;
1020 u32 itrn_reg_spacing;
1022 chunk = &chunks->vchunks[j];
1023 num_vec = le16_to_cpu(chunk->num_vectors);
1024 reg_val.dyn_ctl_reg = le32_to_cpu(chunk->dynctl_reg_start);
1025 reg_val.itrn_reg = le32_to_cpu(chunk->itrn_reg_start);
1026 reg_val.itrn_index_spacing = le32_to_cpu(chunk->itrn_index_spacing);
1028 dynctl_reg_spacing = le32_to_cpu(chunk->dynctl_reg_spacing);
1029 itrn_reg_spacing = le32_to_cpu(chunk->itrn_reg_spacing);
1031 for (i = 0; i < num_vec; i++) {
1032 reg_vals[num_regs].dyn_ctl_reg = reg_val.dyn_ctl_reg;
1033 reg_vals[num_regs].itrn_reg = reg_val.itrn_reg;
1034 reg_vals[num_regs].itrn_index_spacing =
1035 reg_val.itrn_index_spacing;
1037 reg_val.dyn_ctl_reg += dynctl_reg_spacing;
1038 reg_val.itrn_reg += itrn_reg_spacing;
1047 * idpf_vport_get_q_reg - Get the queue registers for the vport
1048 * @reg_vals: register values needing to be set
1049 * @num_regs: amount we expect to fill
1050 * @q_type: queue model
1051 * @chunks: queue regs received over mailbox
1053 * This function parses the queue register offsets from the queue register
1054 * chunk information, with a specific queue type and stores it into the array
1055 * passed as an argument. It returns the actual number of queue registers that
1058 static int idpf_vport_get_q_reg(u32 *reg_vals, int num_regs, u32 q_type,
1059 struct virtchnl2_queue_reg_chunks *chunks)
1061 u16 num_chunks = le16_to_cpu(chunks->num_chunks);
1062 int reg_filled = 0, i;
1065 while (num_chunks--) {
1066 struct virtchnl2_queue_reg_chunk *chunk;
1069 chunk = &chunks->chunks[num_chunks];
1070 if (le32_to_cpu(chunk->type) != q_type)
1073 num_q = le32_to_cpu(chunk->num_queues);
1074 reg_val = le64_to_cpu(chunk->qtail_reg_start);
1075 for (i = 0; i < num_q && reg_filled < num_regs ; i++) {
1076 reg_vals[reg_filled++] = reg_val;
1077 reg_val += le32_to_cpu(chunk->qtail_reg_spacing);
1085 * __idpf_queue_reg_init - initialize queue registers
1086 * @vport: virtual port structure
1087 * @reg_vals: registers we are initializing
1088 * @num_regs: how many registers there are in total
1089 * @q_type: queue model
1091 * Return number of queues that are initialized
1093 static int __idpf_queue_reg_init(struct idpf_vport *vport, u32 *reg_vals,
1094 int num_regs, u32 q_type)
1096 struct idpf_adapter *adapter = vport->adapter;
1100 case VIRTCHNL2_QUEUE_TYPE_TX:
1101 for (i = 0; i < vport->num_txq_grp; i++) {
1102 struct idpf_txq_group *tx_qgrp = &vport->txq_grps[i];
1104 for (j = 0; j < tx_qgrp->num_txq && k < num_regs; j++, k++)
1105 tx_qgrp->txqs[j]->tail =
1106 idpf_get_reg_addr(adapter, reg_vals[k]);
1109 case VIRTCHNL2_QUEUE_TYPE_RX:
1110 for (i = 0; i < vport->num_rxq_grp; i++) {
1111 struct idpf_rxq_group *rx_qgrp = &vport->rxq_grps[i];
1112 u16 num_rxq = rx_qgrp->singleq.num_rxq;
1114 for (j = 0; j < num_rxq && k < num_regs; j++, k++) {
1115 struct idpf_rx_queue *q;
1117 q = rx_qgrp->singleq.rxqs[j];
1118 q->tail = idpf_get_reg_addr(adapter,
1123 case VIRTCHNL2_QUEUE_TYPE_RX_BUFFER:
1124 for (i = 0; i < vport->num_rxq_grp; i++) {
1125 struct idpf_rxq_group *rx_qgrp = &vport->rxq_grps[i];
1126 u8 num_bufqs = vport->num_bufqs_per_qgrp;
1128 for (j = 0; j < num_bufqs && k < num_regs; j++, k++) {
1129 struct idpf_buf_queue *q;
1131 q = &rx_qgrp->splitq.bufq_sets[j].bufq;
1132 q->tail = idpf_get_reg_addr(adapter,
1145 * idpf_queue_reg_init - initialize queue registers
1146 * @vport: virtual port structure
1148 * Return 0 on success, negative on failure
1150 int idpf_queue_reg_init(struct idpf_vport *vport)
1152 struct virtchnl2_create_vport *vport_params;
1153 struct virtchnl2_queue_reg_chunks *chunks;
1154 struct idpf_vport_config *vport_config;
1155 u16 vport_idx = vport->idx;
1156 int num_regs, ret = 0;
1159 /* We may never deal with more than 256 same type of queues */
1160 reg_vals = kzalloc(sizeof(void *) * IDPF_LARGE_MAX_Q, GFP_KERNEL);
1164 vport_config = vport->adapter->vport_config[vport_idx];
1165 if (vport_config->req_qs_chunks) {
1166 struct virtchnl2_add_queues *vc_aq =
1167 (struct virtchnl2_add_queues *)vport_config->req_qs_chunks;
1168 chunks = &vc_aq->chunks;
1170 vport_params = vport->adapter->vport_params_recvd[vport_idx];
1171 chunks = &vport_params->chunks;
1174 /* Initialize Tx queue tail register address */
1175 num_regs = idpf_vport_get_q_reg(reg_vals, IDPF_LARGE_MAX_Q,
1176 VIRTCHNL2_QUEUE_TYPE_TX,
1178 if (num_regs < vport->num_txq) {
1183 num_regs = __idpf_queue_reg_init(vport, reg_vals, num_regs,
1184 VIRTCHNL2_QUEUE_TYPE_TX);
1185 if (num_regs < vport->num_txq) {
1190 /* Initialize Rx/buffer queue tail register address based on Rx queue
1193 if (idpf_is_queue_model_split(vport->rxq_model)) {
1194 num_regs = idpf_vport_get_q_reg(reg_vals, IDPF_LARGE_MAX_Q,
1195 VIRTCHNL2_QUEUE_TYPE_RX_BUFFER,
1197 if (num_regs < vport->num_bufq) {
1202 num_regs = __idpf_queue_reg_init(vport, reg_vals, num_regs,
1203 VIRTCHNL2_QUEUE_TYPE_RX_BUFFER);
1204 if (num_regs < vport->num_bufq) {
1209 num_regs = idpf_vport_get_q_reg(reg_vals, IDPF_LARGE_MAX_Q,
1210 VIRTCHNL2_QUEUE_TYPE_RX,
1212 if (num_regs < vport->num_rxq) {
1217 num_regs = __idpf_queue_reg_init(vport, reg_vals, num_regs,
1218 VIRTCHNL2_QUEUE_TYPE_RX);
1219 if (num_regs < vport->num_rxq) {
1232 * idpf_send_create_vport_msg - Send virtchnl create vport message
1233 * @adapter: Driver specific private structure
1234 * @max_q: vport max queue info
1236 * send virtchnl creae vport message
1238 * Returns 0 on success, negative on failure
1240 int idpf_send_create_vport_msg(struct idpf_adapter *adapter,
1241 struct idpf_vport_max_q *max_q)
1243 struct virtchnl2_create_vport *vport_msg;
1244 struct idpf_vc_xn_params xn_params = {};
1245 u16 idx = adapter->next_vport;
1249 buf_size = sizeof(struct virtchnl2_create_vport);
1250 if (!adapter->vport_params_reqd[idx]) {
1251 adapter->vport_params_reqd[idx] = kzalloc(buf_size,
1253 if (!adapter->vport_params_reqd[idx])
1257 vport_msg = adapter->vport_params_reqd[idx];
1258 vport_msg->vport_type = cpu_to_le16(VIRTCHNL2_VPORT_TYPE_DEFAULT);
1259 vport_msg->vport_index = cpu_to_le16(idx);
1261 if (adapter->req_tx_splitq || !IS_ENABLED(CONFIG_IDPF_SINGLEQ))
1262 vport_msg->txq_model = cpu_to_le16(VIRTCHNL2_QUEUE_MODEL_SPLIT);
1264 vport_msg->txq_model = cpu_to_le16(VIRTCHNL2_QUEUE_MODEL_SINGLE);
1266 if (adapter->req_rx_splitq || !IS_ENABLED(CONFIG_IDPF_SINGLEQ))
1267 vport_msg->rxq_model = cpu_to_le16(VIRTCHNL2_QUEUE_MODEL_SPLIT);
1269 vport_msg->rxq_model = cpu_to_le16(VIRTCHNL2_QUEUE_MODEL_SINGLE);
1271 err = idpf_vport_calc_total_qs(adapter, idx, vport_msg, max_q);
1273 dev_err(&adapter->pdev->dev, "Enough queues are not available");
1278 if (!adapter->vport_params_recvd[idx]) {
1279 adapter->vport_params_recvd[idx] = kzalloc(IDPF_CTLQ_MAX_BUF_LEN,
1281 if (!adapter->vport_params_recvd[idx]) {
1283 goto free_vport_params;
1287 xn_params.vc_op = VIRTCHNL2_OP_CREATE_VPORT;
1288 xn_params.send_buf.iov_base = vport_msg;
1289 xn_params.send_buf.iov_len = buf_size;
1290 xn_params.recv_buf.iov_base = adapter->vport_params_recvd[idx];
1291 xn_params.recv_buf.iov_len = IDPF_CTLQ_MAX_BUF_LEN;
1292 xn_params.timeout_ms = IDPF_VC_XN_DEFAULT_TIMEOUT_MSEC;
1293 reply_sz = idpf_vc_xn_exec(adapter, &xn_params);
1296 goto free_vport_params;
1298 if (reply_sz < IDPF_CTLQ_MAX_BUF_LEN) {
1300 goto free_vport_params;
1306 kfree(adapter->vport_params_recvd[idx]);
1307 adapter->vport_params_recvd[idx] = NULL;
1308 kfree(adapter->vport_params_reqd[idx]);
1309 adapter->vport_params_reqd[idx] = NULL;
1315 * idpf_check_supported_desc_ids - Verify we have required descriptor support
1316 * @vport: virtual port structure
1318 * Return 0 on success, error on failure
1320 int idpf_check_supported_desc_ids(struct idpf_vport *vport)
1322 struct idpf_adapter *adapter = vport->adapter;
1323 struct virtchnl2_create_vport *vport_msg;
1324 u64 rx_desc_ids, tx_desc_ids;
1326 vport_msg = adapter->vport_params_recvd[vport->idx];
1328 if (!IS_ENABLED(CONFIG_IDPF_SINGLEQ) &&
1329 (vport_msg->rxq_model == VIRTCHNL2_QUEUE_MODEL_SINGLE ||
1330 vport_msg->txq_model == VIRTCHNL2_QUEUE_MODEL_SINGLE)) {
1331 pci_err(adapter->pdev, "singleq mode requested, but not compiled-in\n");
1335 rx_desc_ids = le64_to_cpu(vport_msg->rx_desc_ids);
1336 tx_desc_ids = le64_to_cpu(vport_msg->tx_desc_ids);
1338 if (idpf_is_queue_model_split(vport->rxq_model)) {
1339 if (!(rx_desc_ids & VIRTCHNL2_RXDID_2_FLEX_SPLITQ_M)) {
1340 dev_info(&adapter->pdev->dev, "Minimum RX descriptor support not provided, using the default\n");
1341 vport_msg->rx_desc_ids = cpu_to_le64(VIRTCHNL2_RXDID_2_FLEX_SPLITQ_M);
1344 if (!(rx_desc_ids & VIRTCHNL2_RXDID_2_FLEX_SQ_NIC_M))
1345 vport->base_rxd = true;
1348 if (!idpf_is_queue_model_split(vport->txq_model))
1351 if ((tx_desc_ids & MIN_SUPPORT_TXDID) != MIN_SUPPORT_TXDID) {
1352 dev_info(&adapter->pdev->dev, "Minimum TX descriptor support not provided, using the default\n");
1353 vport_msg->tx_desc_ids = cpu_to_le64(MIN_SUPPORT_TXDID);
1360 * idpf_send_destroy_vport_msg - Send virtchnl destroy vport message
1361 * @vport: virtual port data structure
1363 * Send virtchnl destroy vport message. Returns 0 on success, negative on
1366 int idpf_send_destroy_vport_msg(struct idpf_vport *vport)
1368 struct idpf_vc_xn_params xn_params = {};
1369 struct virtchnl2_vport v_id;
1372 v_id.vport_id = cpu_to_le32(vport->vport_id);
1374 xn_params.vc_op = VIRTCHNL2_OP_DESTROY_VPORT;
1375 xn_params.send_buf.iov_base = &v_id;
1376 xn_params.send_buf.iov_len = sizeof(v_id);
1377 xn_params.timeout_ms = IDPF_VC_XN_MIN_TIMEOUT_MSEC;
1378 reply_sz = idpf_vc_xn_exec(vport->adapter, &xn_params);
1380 return reply_sz < 0 ? reply_sz : 0;
1384 * idpf_send_enable_vport_msg - Send virtchnl enable vport message
1385 * @vport: virtual port data structure
1387 * Send enable vport virtchnl message. Returns 0 on success, negative on
1390 int idpf_send_enable_vport_msg(struct idpf_vport *vport)
1392 struct idpf_vc_xn_params xn_params = {};
1393 struct virtchnl2_vport v_id;
1396 v_id.vport_id = cpu_to_le32(vport->vport_id);
1398 xn_params.vc_op = VIRTCHNL2_OP_ENABLE_VPORT;
1399 xn_params.send_buf.iov_base = &v_id;
1400 xn_params.send_buf.iov_len = sizeof(v_id);
1401 xn_params.timeout_ms = IDPF_VC_XN_DEFAULT_TIMEOUT_MSEC;
1402 reply_sz = idpf_vc_xn_exec(vport->adapter, &xn_params);
1404 return reply_sz < 0 ? reply_sz : 0;
1408 * idpf_send_disable_vport_msg - Send virtchnl disable vport message
1409 * @vport: virtual port data structure
1411 * Send disable vport virtchnl message. Returns 0 on success, negative on
1414 int idpf_send_disable_vport_msg(struct idpf_vport *vport)
1416 struct idpf_vc_xn_params xn_params = {};
1417 struct virtchnl2_vport v_id;
1420 v_id.vport_id = cpu_to_le32(vport->vport_id);
1422 xn_params.vc_op = VIRTCHNL2_OP_DISABLE_VPORT;
1423 xn_params.send_buf.iov_base = &v_id;
1424 xn_params.send_buf.iov_len = sizeof(v_id);
1425 xn_params.timeout_ms = IDPF_VC_XN_MIN_TIMEOUT_MSEC;
1426 reply_sz = idpf_vc_xn_exec(vport->adapter, &xn_params);
1428 return reply_sz < 0 ? reply_sz : 0;
1432 * idpf_send_config_tx_queues_msg - Send virtchnl config tx queues message
1433 * @vport: virtual port data structure
1435 * Send config tx queues virtchnl message. Returns 0 on success, negative on
1438 static int idpf_send_config_tx_queues_msg(struct idpf_vport *vport)
1440 struct virtchnl2_config_tx_queues *ctq __free(kfree) = NULL;
1441 struct virtchnl2_txq_info *qi __free(kfree) = NULL;
1442 struct idpf_vc_xn_params xn_params = {};
1443 u32 config_sz, chunk_sz, buf_sz;
1444 int totqs, num_msgs, num_chunks;
1448 totqs = vport->num_txq + vport->num_complq;
1449 qi = kcalloc(totqs, sizeof(struct virtchnl2_txq_info), GFP_KERNEL);
1453 /* Populate the queue info buffer with all queue context info */
1454 for (i = 0; i < vport->num_txq_grp; i++) {
1455 struct idpf_txq_group *tx_qgrp = &vport->txq_grps[i];
1458 for (j = 0; j < tx_qgrp->num_txq; j++, k++) {
1460 cpu_to_le32(tx_qgrp->txqs[j]->q_id);
1462 cpu_to_le16(vport->txq_model);
1464 cpu_to_le32(VIRTCHNL2_QUEUE_TYPE_TX);
1466 cpu_to_le16(tx_qgrp->txqs[j]->desc_count);
1467 qi[k].dma_ring_addr =
1468 cpu_to_le64(tx_qgrp->txqs[j]->dma);
1469 if (idpf_is_queue_model_split(vport->txq_model)) {
1470 struct idpf_tx_queue *q = tx_qgrp->txqs[j];
1472 qi[k].tx_compl_queue_id =
1473 cpu_to_le16(tx_qgrp->complq->q_id);
1474 qi[k].relative_queue_id = cpu_to_le16(j);
1476 if (idpf_queue_has(FLOW_SCH_EN, q))
1478 cpu_to_le16(VIRTCHNL2_TXQ_SCHED_MODE_FLOW);
1481 cpu_to_le16(VIRTCHNL2_TXQ_SCHED_MODE_QUEUE);
1484 cpu_to_le16(VIRTCHNL2_TXQ_SCHED_MODE_QUEUE);
1488 if (!idpf_is_queue_model_split(vport->txq_model))
1491 qi[k].queue_id = cpu_to_le32(tx_qgrp->complq->q_id);
1492 qi[k].model = cpu_to_le16(vport->txq_model);
1493 qi[k].type = cpu_to_le32(VIRTCHNL2_QUEUE_TYPE_TX_COMPLETION);
1494 qi[k].ring_len = cpu_to_le16(tx_qgrp->complq->desc_count);
1495 qi[k].dma_ring_addr = cpu_to_le64(tx_qgrp->complq->dma);
1497 if (idpf_queue_has(FLOW_SCH_EN, tx_qgrp->complq))
1498 sched_mode = VIRTCHNL2_TXQ_SCHED_MODE_FLOW;
1500 sched_mode = VIRTCHNL2_TXQ_SCHED_MODE_QUEUE;
1501 qi[k].sched_mode = cpu_to_le16(sched_mode);
1506 /* Make sure accounting agrees */
1510 /* Chunk up the queue contexts into multiple messages to avoid
1511 * sending a control queue message buffer that is too large
1513 config_sz = sizeof(struct virtchnl2_config_tx_queues);
1514 chunk_sz = sizeof(struct virtchnl2_txq_info);
1516 num_chunks = min_t(u32, IDPF_NUM_CHUNKS_PER_MSG(config_sz, chunk_sz),
1518 num_msgs = DIV_ROUND_UP(totqs, num_chunks);
1520 buf_sz = struct_size(ctq, qinfo, num_chunks);
1521 ctq = kzalloc(buf_sz, GFP_KERNEL);
1525 xn_params.vc_op = VIRTCHNL2_OP_CONFIG_TX_QUEUES;
1526 xn_params.timeout_ms = IDPF_VC_XN_DEFAULT_TIMEOUT_MSEC;
1528 for (i = 0, k = 0; i < num_msgs; i++) {
1529 memset(ctq, 0, buf_sz);
1530 ctq->vport_id = cpu_to_le32(vport->vport_id);
1531 ctq->num_qinfo = cpu_to_le16(num_chunks);
1532 memcpy(ctq->qinfo, &qi[k], chunk_sz * num_chunks);
1534 xn_params.send_buf.iov_base = ctq;
1535 xn_params.send_buf.iov_len = buf_sz;
1536 reply_sz = idpf_vc_xn_exec(vport->adapter, &xn_params);
1541 totqs -= num_chunks;
1542 num_chunks = min(num_chunks, totqs);
1543 /* Recalculate buffer size */
1544 buf_sz = struct_size(ctq, qinfo, num_chunks);
1551 * idpf_send_config_rx_queues_msg - Send virtchnl config rx queues message
1552 * @vport: virtual port data structure
1554 * Send config rx queues virtchnl message. Returns 0 on success, negative on
1557 static int idpf_send_config_rx_queues_msg(struct idpf_vport *vport)
1559 struct virtchnl2_config_rx_queues *crq __free(kfree) = NULL;
1560 struct virtchnl2_rxq_info *qi __free(kfree) = NULL;
1561 struct idpf_vc_xn_params xn_params = {};
1562 u32 config_sz, chunk_sz, buf_sz;
1563 int totqs, num_msgs, num_chunks;
1567 totqs = vport->num_rxq + vport->num_bufq;
1568 qi = kcalloc(totqs, sizeof(struct virtchnl2_rxq_info), GFP_KERNEL);
1572 /* Populate the queue info buffer with all queue context info */
1573 for (i = 0; i < vport->num_rxq_grp; i++) {
1574 struct idpf_rxq_group *rx_qgrp = &vport->rxq_grps[i];
1578 if (!idpf_is_queue_model_split(vport->rxq_model))
1581 for (j = 0; j < vport->num_bufqs_per_qgrp; j++, k++) {
1582 struct idpf_buf_queue *bufq =
1583 &rx_qgrp->splitq.bufq_sets[j].bufq;
1585 qi[k].queue_id = cpu_to_le32(bufq->q_id);
1586 qi[k].model = cpu_to_le16(vport->rxq_model);
1588 cpu_to_le32(VIRTCHNL2_QUEUE_TYPE_RX_BUFFER);
1589 qi[k].desc_ids = cpu_to_le64(VIRTCHNL2_RXDID_2_FLEX_SPLITQ_M);
1590 qi[k].ring_len = cpu_to_le16(bufq->desc_count);
1591 qi[k].dma_ring_addr = cpu_to_le64(bufq->dma);
1592 qi[k].data_buffer_size = cpu_to_le32(bufq->rx_buf_size);
1593 qi[k].buffer_notif_stride = IDPF_RX_BUF_STRIDE;
1594 qi[k].rx_buffer_low_watermark =
1595 cpu_to_le16(bufq->rx_buffer_low_watermark);
1596 if (idpf_is_feature_ena(vport, NETIF_F_GRO_HW))
1597 qi[k].qflags |= cpu_to_le16(VIRTCHNL2_RXQ_RSC);
1601 if (idpf_is_queue_model_split(vport->rxq_model))
1602 num_rxq = rx_qgrp->splitq.num_rxq_sets;
1604 num_rxq = rx_qgrp->singleq.num_rxq;
1606 for (j = 0; j < num_rxq; j++, k++) {
1607 const struct idpf_bufq_set *sets;
1608 struct idpf_rx_queue *rxq;
1610 if (!idpf_is_queue_model_split(vport->rxq_model)) {
1611 rxq = rx_qgrp->singleq.rxqs[j];
1612 goto common_qi_fields;
1615 rxq = &rx_qgrp->splitq.rxq_sets[j]->rxq;
1616 sets = rxq->bufq_sets;
1618 /* In splitq mode, RXQ buffer size should be
1619 * set to that of the first buffer queue
1620 * associated with this RXQ.
1622 rxq->rx_buf_size = sets[0].bufq.rx_buf_size;
1624 qi[k].rx_bufq1_id = cpu_to_le16(sets[0].bufq.q_id);
1625 if (vport->num_bufqs_per_qgrp > IDPF_SINGLE_BUFQ_PER_RXQ_GRP) {
1626 qi[k].bufq2_ena = IDPF_BUFQ2_ENA;
1628 cpu_to_le16(sets[1].bufq.q_id);
1630 qi[k].rx_buffer_low_watermark =
1631 cpu_to_le16(rxq->rx_buffer_low_watermark);
1632 if (idpf_is_feature_ena(vport, NETIF_F_GRO_HW))
1633 qi[k].qflags |= cpu_to_le16(VIRTCHNL2_RXQ_RSC);
1635 rxq->rx_hbuf_size = sets[0].bufq.rx_hbuf_size;
1637 if (idpf_queue_has(HSPLIT_EN, rxq)) {
1639 cpu_to_le16(VIRTCHNL2_RXQ_HDR_SPLIT);
1640 qi[k].hdr_buffer_size =
1641 cpu_to_le16(rxq->rx_hbuf_size);
1645 qi[k].queue_id = cpu_to_le32(rxq->q_id);
1646 qi[k].model = cpu_to_le16(vport->rxq_model);
1647 qi[k].type = cpu_to_le32(VIRTCHNL2_QUEUE_TYPE_RX);
1648 qi[k].ring_len = cpu_to_le16(rxq->desc_count);
1649 qi[k].dma_ring_addr = cpu_to_le64(rxq->dma);
1650 qi[k].max_pkt_size = cpu_to_le32(rxq->rx_max_pkt_size);
1651 qi[k].data_buffer_size = cpu_to_le32(rxq->rx_buf_size);
1653 cpu_to_le16(VIRTCHNL2_RX_DESC_SIZE_32BYTE);
1654 qi[k].desc_ids = cpu_to_le64(rxq->rxdids);
1658 /* Make sure accounting agrees */
1662 /* Chunk up the queue contexts into multiple messages to avoid
1663 * sending a control queue message buffer that is too large
1665 config_sz = sizeof(struct virtchnl2_config_rx_queues);
1666 chunk_sz = sizeof(struct virtchnl2_rxq_info);
1668 num_chunks = min_t(u32, IDPF_NUM_CHUNKS_PER_MSG(config_sz, chunk_sz),
1670 num_msgs = DIV_ROUND_UP(totqs, num_chunks);
1672 buf_sz = struct_size(crq, qinfo, num_chunks);
1673 crq = kzalloc(buf_sz, GFP_KERNEL);
1677 xn_params.vc_op = VIRTCHNL2_OP_CONFIG_RX_QUEUES;
1678 xn_params.timeout_ms = IDPF_VC_XN_DEFAULT_TIMEOUT_MSEC;
1680 for (i = 0, k = 0; i < num_msgs; i++) {
1681 memset(crq, 0, buf_sz);
1682 crq->vport_id = cpu_to_le32(vport->vport_id);
1683 crq->num_qinfo = cpu_to_le16(num_chunks);
1684 memcpy(crq->qinfo, &qi[k], chunk_sz * num_chunks);
1686 xn_params.send_buf.iov_base = crq;
1687 xn_params.send_buf.iov_len = buf_sz;
1688 reply_sz = idpf_vc_xn_exec(vport->adapter, &xn_params);
1693 totqs -= num_chunks;
1694 num_chunks = min(num_chunks, totqs);
1695 /* Recalculate buffer size */
1696 buf_sz = struct_size(crq, qinfo, num_chunks);
1703 * idpf_send_ena_dis_queues_msg - Send virtchnl enable or disable
1705 * @vport: virtual port data structure
1706 * @ena: if true enable, false disable
1708 * Send enable or disable queues virtchnl message. Returns 0 on success,
1709 * negative on failure.
1711 static int idpf_send_ena_dis_queues_msg(struct idpf_vport *vport, bool ena)
1713 struct virtchnl2_del_ena_dis_queues *eq __free(kfree) = NULL;
1714 struct virtchnl2_queue_chunk *qc __free(kfree) = NULL;
1715 u32 num_msgs, num_chunks, num_txq, num_rxq, num_q;
1716 struct idpf_vc_xn_params xn_params = {};
1717 struct virtchnl2_queue_chunks *qcs;
1718 u32 config_sz, chunk_sz, buf_sz;
1722 num_txq = vport->num_txq + vport->num_complq;
1723 num_rxq = vport->num_rxq + vport->num_bufq;
1724 num_q = num_txq + num_rxq;
1725 buf_sz = sizeof(struct virtchnl2_queue_chunk) * num_q;
1726 qc = kzalloc(buf_sz, GFP_KERNEL);
1730 for (i = 0; i < vport->num_txq_grp; i++) {
1731 struct idpf_txq_group *tx_qgrp = &vport->txq_grps[i];
1733 for (j = 0; j < tx_qgrp->num_txq; j++, k++) {
1734 qc[k].type = cpu_to_le32(VIRTCHNL2_QUEUE_TYPE_TX);
1735 qc[k].start_queue_id = cpu_to_le32(tx_qgrp->txqs[j]->q_id);
1736 qc[k].num_queues = cpu_to_le32(IDPF_NUMQ_PER_CHUNK);
1739 if (vport->num_txq != k)
1742 if (!idpf_is_queue_model_split(vport->txq_model))
1745 for (i = 0; i < vport->num_txq_grp; i++, k++) {
1746 struct idpf_txq_group *tx_qgrp = &vport->txq_grps[i];
1748 qc[k].type = cpu_to_le32(VIRTCHNL2_QUEUE_TYPE_TX_COMPLETION);
1749 qc[k].start_queue_id = cpu_to_le32(tx_qgrp->complq->q_id);
1750 qc[k].num_queues = cpu_to_le32(IDPF_NUMQ_PER_CHUNK);
1752 if (vport->num_complq != (k - vport->num_txq))
1756 for (i = 0; i < vport->num_rxq_grp; i++) {
1757 struct idpf_rxq_group *rx_qgrp = &vport->rxq_grps[i];
1759 if (idpf_is_queue_model_split(vport->rxq_model))
1760 num_rxq = rx_qgrp->splitq.num_rxq_sets;
1762 num_rxq = rx_qgrp->singleq.num_rxq;
1764 for (j = 0; j < num_rxq; j++, k++) {
1765 if (idpf_is_queue_model_split(vport->rxq_model)) {
1766 qc[k].start_queue_id =
1767 cpu_to_le32(rx_qgrp->splitq.rxq_sets[j]->rxq.q_id);
1769 cpu_to_le32(VIRTCHNL2_QUEUE_TYPE_RX);
1771 qc[k].start_queue_id =
1772 cpu_to_le32(rx_qgrp->singleq.rxqs[j]->q_id);
1774 cpu_to_le32(VIRTCHNL2_QUEUE_TYPE_RX);
1776 qc[k].num_queues = cpu_to_le32(IDPF_NUMQ_PER_CHUNK);
1779 if (vport->num_rxq != k - (vport->num_txq + vport->num_complq))
1782 if (!idpf_is_queue_model_split(vport->rxq_model))
1785 for (i = 0; i < vport->num_rxq_grp; i++) {
1786 struct idpf_rxq_group *rx_qgrp = &vport->rxq_grps[i];
1788 for (j = 0; j < vport->num_bufqs_per_qgrp; j++, k++) {
1789 const struct idpf_buf_queue *q;
1791 q = &rx_qgrp->splitq.bufq_sets[j].bufq;
1793 cpu_to_le32(VIRTCHNL2_QUEUE_TYPE_RX_BUFFER);
1794 qc[k].start_queue_id = cpu_to_le32(q->q_id);
1795 qc[k].num_queues = cpu_to_le32(IDPF_NUMQ_PER_CHUNK);
1798 if (vport->num_bufq != k - (vport->num_txq +
1804 /* Chunk up the queue info into multiple messages */
1805 config_sz = sizeof(struct virtchnl2_del_ena_dis_queues);
1806 chunk_sz = sizeof(struct virtchnl2_queue_chunk);
1808 num_chunks = min_t(u32, IDPF_NUM_CHUNKS_PER_MSG(config_sz, chunk_sz),
1810 num_msgs = DIV_ROUND_UP(num_q, num_chunks);
1812 buf_sz = struct_size(eq, chunks.chunks, num_chunks);
1813 eq = kzalloc(buf_sz, GFP_KERNEL);
1818 xn_params.vc_op = VIRTCHNL2_OP_ENABLE_QUEUES;
1819 xn_params.timeout_ms = IDPF_VC_XN_DEFAULT_TIMEOUT_MSEC;
1821 xn_params.vc_op = VIRTCHNL2_OP_DISABLE_QUEUES;
1822 xn_params.timeout_ms = IDPF_VC_XN_MIN_TIMEOUT_MSEC;
1825 for (i = 0, k = 0; i < num_msgs; i++) {
1826 memset(eq, 0, buf_sz);
1827 eq->vport_id = cpu_to_le32(vport->vport_id);
1828 eq->chunks.num_chunks = cpu_to_le16(num_chunks);
1830 memcpy(qcs->chunks, &qc[k], chunk_sz * num_chunks);
1832 xn_params.send_buf.iov_base = eq;
1833 xn_params.send_buf.iov_len = buf_sz;
1834 reply_sz = idpf_vc_xn_exec(vport->adapter, &xn_params);
1839 num_q -= num_chunks;
1840 num_chunks = min(num_chunks, num_q);
1841 /* Recalculate buffer size */
1842 buf_sz = struct_size(eq, chunks.chunks, num_chunks);
1849 * idpf_send_map_unmap_queue_vector_msg - Send virtchnl map or unmap queue
1851 * @vport: virtual port data structure
1852 * @map: true for map and false for unmap
1854 * Send map or unmap queue vector virtchnl message. Returns 0 on success,
1855 * negative on failure.
1857 int idpf_send_map_unmap_queue_vector_msg(struct idpf_vport *vport, bool map)
1859 struct virtchnl2_queue_vector_maps *vqvm __free(kfree) = NULL;
1860 struct virtchnl2_queue_vector *vqv __free(kfree) = NULL;
1861 struct idpf_vc_xn_params xn_params = {};
1862 u32 config_sz, chunk_sz, buf_sz;
1863 u32 num_msgs, num_chunks, num_q;
1867 num_q = vport->num_txq + vport->num_rxq;
1869 buf_sz = sizeof(struct virtchnl2_queue_vector) * num_q;
1870 vqv = kzalloc(buf_sz, GFP_KERNEL);
1874 for (i = 0; i < vport->num_txq_grp; i++) {
1875 struct idpf_txq_group *tx_qgrp = &vport->txq_grps[i];
1877 for (j = 0; j < tx_qgrp->num_txq; j++, k++) {
1879 cpu_to_le32(VIRTCHNL2_QUEUE_TYPE_TX);
1880 vqv[k].queue_id = cpu_to_le32(tx_qgrp->txqs[j]->q_id);
1882 if (idpf_is_queue_model_split(vport->txq_model)) {
1884 cpu_to_le16(tx_qgrp->complq->q_vector->v_idx);
1886 cpu_to_le32(tx_qgrp->complq->q_vector->tx_itr_idx);
1889 cpu_to_le16(tx_qgrp->txqs[j]->q_vector->v_idx);
1891 cpu_to_le32(tx_qgrp->txqs[j]->q_vector->tx_itr_idx);
1896 if (vport->num_txq != k)
1899 for (i = 0; i < vport->num_rxq_grp; i++) {
1900 struct idpf_rxq_group *rx_qgrp = &vport->rxq_grps[i];
1903 if (idpf_is_queue_model_split(vport->rxq_model))
1904 num_rxq = rx_qgrp->splitq.num_rxq_sets;
1906 num_rxq = rx_qgrp->singleq.num_rxq;
1908 for (j = 0; j < num_rxq; j++, k++) {
1909 struct idpf_rx_queue *rxq;
1911 if (idpf_is_queue_model_split(vport->rxq_model))
1912 rxq = &rx_qgrp->splitq.rxq_sets[j]->rxq;
1914 rxq = rx_qgrp->singleq.rxqs[j];
1917 cpu_to_le32(VIRTCHNL2_QUEUE_TYPE_RX);
1918 vqv[k].queue_id = cpu_to_le32(rxq->q_id);
1919 vqv[k].vector_id = cpu_to_le16(rxq->q_vector->v_idx);
1920 vqv[k].itr_idx = cpu_to_le32(rxq->q_vector->rx_itr_idx);
1924 if (idpf_is_queue_model_split(vport->txq_model)) {
1925 if (vport->num_rxq != k - vport->num_complq)
1928 if (vport->num_rxq != k - vport->num_txq)
1932 /* Chunk up the vector info into multiple messages */
1933 config_sz = sizeof(struct virtchnl2_queue_vector_maps);
1934 chunk_sz = sizeof(struct virtchnl2_queue_vector);
1936 num_chunks = min_t(u32, IDPF_NUM_CHUNKS_PER_MSG(config_sz, chunk_sz),
1938 num_msgs = DIV_ROUND_UP(num_q, num_chunks);
1940 buf_sz = struct_size(vqvm, qv_maps, num_chunks);
1941 vqvm = kzalloc(buf_sz, GFP_KERNEL);
1946 xn_params.vc_op = VIRTCHNL2_OP_MAP_QUEUE_VECTOR;
1947 xn_params.timeout_ms = IDPF_VC_XN_DEFAULT_TIMEOUT_MSEC;
1949 xn_params.vc_op = VIRTCHNL2_OP_UNMAP_QUEUE_VECTOR;
1950 xn_params.timeout_ms = IDPF_VC_XN_MIN_TIMEOUT_MSEC;
1953 for (i = 0, k = 0; i < num_msgs; i++) {
1954 memset(vqvm, 0, buf_sz);
1955 xn_params.send_buf.iov_base = vqvm;
1956 xn_params.send_buf.iov_len = buf_sz;
1957 vqvm->vport_id = cpu_to_le32(vport->vport_id);
1958 vqvm->num_qv_maps = cpu_to_le16(num_chunks);
1959 memcpy(vqvm->qv_maps, &vqv[k], chunk_sz * num_chunks);
1961 reply_sz = idpf_vc_xn_exec(vport->adapter, &xn_params);
1966 num_q -= num_chunks;
1967 num_chunks = min(num_chunks, num_q);
1968 /* Recalculate buffer size */
1969 buf_sz = struct_size(vqvm, qv_maps, num_chunks);
1976 * idpf_send_enable_queues_msg - send enable queues virtchnl message
1977 * @vport: Virtual port private data structure
1979 * Will send enable queues virtchnl message. Returns 0 on success, negative on
1982 int idpf_send_enable_queues_msg(struct idpf_vport *vport)
1984 return idpf_send_ena_dis_queues_msg(vport, true);
1988 * idpf_send_disable_queues_msg - send disable queues virtchnl message
1989 * @vport: Virtual port private data structure
1991 * Will send disable queues virtchnl message. Returns 0 on success, negative
1994 int idpf_send_disable_queues_msg(struct idpf_vport *vport)
1998 err = idpf_send_ena_dis_queues_msg(vport, false);
2002 /* switch to poll mode as interrupts will be disabled after disable
2003 * queues virtchnl message is sent
2005 for (i = 0; i < vport->num_txq; i++)
2006 idpf_queue_set(POLL_MODE, vport->txqs[i]);
2008 /* schedule the napi to receive all the marker packets */
2010 for (i = 0; i < vport->num_q_vectors; i++)
2011 napi_schedule(&vport->q_vectors[i].napi);
2014 return idpf_wait_for_marker_event(vport);
2018 * idpf_convert_reg_to_queue_chunks - Copy queue chunk information to the right
2020 * @dchunks: Destination chunks to store data to
2021 * @schunks: Source chunks to copy data from
2022 * @num_chunks: number of chunks to copy
2024 static void idpf_convert_reg_to_queue_chunks(struct virtchnl2_queue_chunk *dchunks,
2025 struct virtchnl2_queue_reg_chunk *schunks,
2030 for (i = 0; i < num_chunks; i++) {
2031 dchunks[i].type = schunks[i].type;
2032 dchunks[i].start_queue_id = schunks[i].start_queue_id;
2033 dchunks[i].num_queues = schunks[i].num_queues;
2038 * idpf_send_delete_queues_msg - send delete queues virtchnl message
2039 * @vport: Virtual port private data structure
2041 * Will send delete queues virtchnl message. Return 0 on success, negative on
2044 int idpf_send_delete_queues_msg(struct idpf_vport *vport)
2046 struct virtchnl2_del_ena_dis_queues *eq __free(kfree) = NULL;
2047 struct virtchnl2_create_vport *vport_params;
2048 struct virtchnl2_queue_reg_chunks *chunks;
2049 struct idpf_vc_xn_params xn_params = {};
2050 struct idpf_vport_config *vport_config;
2051 u16 vport_idx = vport->idx;
2056 vport_config = vport->adapter->vport_config[vport_idx];
2057 if (vport_config->req_qs_chunks) {
2058 chunks = &vport_config->req_qs_chunks->chunks;
2060 vport_params = vport->adapter->vport_params_recvd[vport_idx];
2061 chunks = &vport_params->chunks;
2064 num_chunks = le16_to_cpu(chunks->num_chunks);
2065 buf_size = struct_size(eq, chunks.chunks, num_chunks);
2067 eq = kzalloc(buf_size, GFP_KERNEL);
2071 eq->vport_id = cpu_to_le32(vport->vport_id);
2072 eq->chunks.num_chunks = cpu_to_le16(num_chunks);
2074 idpf_convert_reg_to_queue_chunks(eq->chunks.chunks, chunks->chunks,
2077 xn_params.vc_op = VIRTCHNL2_OP_DEL_QUEUES;
2078 xn_params.timeout_ms = IDPF_VC_XN_MIN_TIMEOUT_MSEC;
2079 xn_params.send_buf.iov_base = eq;
2080 xn_params.send_buf.iov_len = buf_size;
2081 reply_sz = idpf_vc_xn_exec(vport->adapter, &xn_params);
2083 return reply_sz < 0 ? reply_sz : 0;
2087 * idpf_send_config_queues_msg - Send config queues virtchnl message
2088 * @vport: Virtual port private data structure
2090 * Will send config queues virtchnl message. Returns 0 on success, negative on
2093 int idpf_send_config_queues_msg(struct idpf_vport *vport)
2097 err = idpf_send_config_tx_queues_msg(vport);
2101 return idpf_send_config_rx_queues_msg(vport);
2105 * idpf_send_add_queues_msg - Send virtchnl add queues message
2106 * @vport: Virtual port private data structure
2107 * @num_tx_q: number of transmit queues
2108 * @num_complq: number of transmit completion queues
2109 * @num_rx_q: number of receive queues
2110 * @num_rx_bufq: number of receive buffer queues
2112 * Returns 0 on success, negative on failure. vport _MUST_ be const here as
2113 * we should not change any fields within vport itself in this function.
2115 int idpf_send_add_queues_msg(const struct idpf_vport *vport, u16 num_tx_q,
2116 u16 num_complq, u16 num_rx_q, u16 num_rx_bufq)
2118 struct virtchnl2_add_queues *vc_msg __free(kfree) = NULL;
2119 struct idpf_vc_xn_params xn_params = {};
2120 struct idpf_vport_config *vport_config;
2121 struct virtchnl2_add_queues aq = {};
2122 u16 vport_idx = vport->idx;
2126 vc_msg = kzalloc(IDPF_CTLQ_MAX_BUF_LEN, GFP_KERNEL);
2130 vport_config = vport->adapter->vport_config[vport_idx];
2131 kfree(vport_config->req_qs_chunks);
2132 vport_config->req_qs_chunks = NULL;
2134 aq.vport_id = cpu_to_le32(vport->vport_id);
2135 aq.num_tx_q = cpu_to_le16(num_tx_q);
2136 aq.num_tx_complq = cpu_to_le16(num_complq);
2137 aq.num_rx_q = cpu_to_le16(num_rx_q);
2138 aq.num_rx_bufq = cpu_to_le16(num_rx_bufq);
2140 xn_params.vc_op = VIRTCHNL2_OP_ADD_QUEUES;
2141 xn_params.timeout_ms = IDPF_VC_XN_DEFAULT_TIMEOUT_MSEC;
2142 xn_params.send_buf.iov_base = &aq;
2143 xn_params.send_buf.iov_len = sizeof(aq);
2144 xn_params.recv_buf.iov_base = vc_msg;
2145 xn_params.recv_buf.iov_len = IDPF_CTLQ_MAX_BUF_LEN;
2146 reply_sz = idpf_vc_xn_exec(vport->adapter, &xn_params);
2150 /* compare vc_msg num queues with vport num queues */
2151 if (le16_to_cpu(vc_msg->num_tx_q) != num_tx_q ||
2152 le16_to_cpu(vc_msg->num_rx_q) != num_rx_q ||
2153 le16_to_cpu(vc_msg->num_tx_complq) != num_complq ||
2154 le16_to_cpu(vc_msg->num_rx_bufq) != num_rx_bufq)
2157 size = struct_size(vc_msg, chunks.chunks,
2158 le16_to_cpu(vc_msg->chunks.num_chunks));
2159 if (reply_sz < size)
2162 vport_config->req_qs_chunks = kmemdup(vc_msg, size, GFP_KERNEL);
2163 if (!vport_config->req_qs_chunks)
2170 * idpf_send_alloc_vectors_msg - Send virtchnl alloc vectors message
2171 * @adapter: Driver specific private structure
2172 * @num_vectors: number of vectors to be allocated
2174 * Returns 0 on success, negative on failure.
2176 int idpf_send_alloc_vectors_msg(struct idpf_adapter *adapter, u16 num_vectors)
2178 struct virtchnl2_alloc_vectors *rcvd_vec __free(kfree) = NULL;
2179 struct idpf_vc_xn_params xn_params = {};
2180 struct virtchnl2_alloc_vectors ac = {};
2185 ac.num_vectors = cpu_to_le16(num_vectors);
2187 rcvd_vec = kzalloc(IDPF_CTLQ_MAX_BUF_LEN, GFP_KERNEL);
2191 xn_params.vc_op = VIRTCHNL2_OP_ALLOC_VECTORS;
2192 xn_params.send_buf.iov_base = ∾
2193 xn_params.send_buf.iov_len = sizeof(ac);
2194 xn_params.recv_buf.iov_base = rcvd_vec;
2195 xn_params.recv_buf.iov_len = IDPF_CTLQ_MAX_BUF_LEN;
2196 xn_params.timeout_ms = IDPF_VC_XN_DEFAULT_TIMEOUT_MSEC;
2197 reply_sz = idpf_vc_xn_exec(adapter, &xn_params);
2201 num_vchunks = le16_to_cpu(rcvd_vec->vchunks.num_vchunks);
2202 size = struct_size(rcvd_vec, vchunks.vchunks, num_vchunks);
2203 if (reply_sz < size)
2206 if (size > IDPF_CTLQ_MAX_BUF_LEN)
2209 kfree(adapter->req_vec_chunks);
2210 adapter->req_vec_chunks = kmemdup(rcvd_vec, size, GFP_KERNEL);
2211 if (!adapter->req_vec_chunks)
2214 if (le16_to_cpu(adapter->req_vec_chunks->num_vectors) < num_vectors) {
2215 kfree(adapter->req_vec_chunks);
2216 adapter->req_vec_chunks = NULL;
2224 * idpf_send_dealloc_vectors_msg - Send virtchnl de allocate vectors message
2225 * @adapter: Driver specific private structure
2227 * Returns 0 on success, negative on failure.
2229 int idpf_send_dealloc_vectors_msg(struct idpf_adapter *adapter)
2231 struct virtchnl2_alloc_vectors *ac = adapter->req_vec_chunks;
2232 struct virtchnl2_vector_chunks *vcs = &ac->vchunks;
2233 struct idpf_vc_xn_params xn_params = {};
2237 buf_size = struct_size(vcs, vchunks, le16_to_cpu(vcs->num_vchunks));
2239 xn_params.vc_op = VIRTCHNL2_OP_DEALLOC_VECTORS;
2240 xn_params.send_buf.iov_base = vcs;
2241 xn_params.send_buf.iov_len = buf_size;
2242 xn_params.timeout_ms = IDPF_VC_XN_MIN_TIMEOUT_MSEC;
2243 reply_sz = idpf_vc_xn_exec(adapter, &xn_params);
2247 kfree(adapter->req_vec_chunks);
2248 adapter->req_vec_chunks = NULL;
2254 * idpf_get_max_vfs - Get max number of vfs supported
2255 * @adapter: Driver specific private structure
2257 * Returns max number of VFs
2259 static int idpf_get_max_vfs(struct idpf_adapter *adapter)
2261 return le16_to_cpu(adapter->caps.max_sriov_vfs);
2265 * idpf_send_set_sriov_vfs_msg - Send virtchnl set sriov vfs message
2266 * @adapter: Driver specific private structure
2267 * @num_vfs: number of virtual functions to be created
2269 * Returns 0 on success, negative on failure.
2271 int idpf_send_set_sriov_vfs_msg(struct idpf_adapter *adapter, u16 num_vfs)
2273 struct virtchnl2_sriov_vfs_info svi = {};
2274 struct idpf_vc_xn_params xn_params = {};
2277 svi.num_vfs = cpu_to_le16(num_vfs);
2278 xn_params.vc_op = VIRTCHNL2_OP_SET_SRIOV_VFS;
2279 xn_params.timeout_ms = IDPF_VC_XN_DEFAULT_TIMEOUT_MSEC;
2280 xn_params.send_buf.iov_base = &svi;
2281 xn_params.send_buf.iov_len = sizeof(svi);
2282 reply_sz = idpf_vc_xn_exec(adapter, &xn_params);
2284 return reply_sz < 0 ? reply_sz : 0;
2288 * idpf_send_get_stats_msg - Send virtchnl get statistics message
2289 * @vport: vport to get stats for
2291 * Returns 0 on success, negative on failure.
2293 int idpf_send_get_stats_msg(struct idpf_vport *vport)
2295 struct idpf_netdev_priv *np = netdev_priv(vport->netdev);
2296 struct rtnl_link_stats64 *netstats = &np->netstats;
2297 struct virtchnl2_vport_stats stats_msg = {};
2298 struct idpf_vc_xn_params xn_params = {};
2302 /* Don't send get_stats message if the link is down */
2303 if (np->state <= __IDPF_VPORT_DOWN)
2306 stats_msg.vport_id = cpu_to_le32(vport->vport_id);
2308 xn_params.vc_op = VIRTCHNL2_OP_GET_STATS;
2309 xn_params.send_buf.iov_base = &stats_msg;
2310 xn_params.send_buf.iov_len = sizeof(stats_msg);
2311 xn_params.recv_buf = xn_params.send_buf;
2312 xn_params.timeout_ms = IDPF_VC_XN_DEFAULT_TIMEOUT_MSEC;
2314 reply_sz = idpf_vc_xn_exec(vport->adapter, &xn_params);
2317 if (reply_sz < sizeof(stats_msg))
2320 spin_lock_bh(&np->stats_lock);
2322 netstats->rx_packets = le64_to_cpu(stats_msg.rx_unicast) +
2323 le64_to_cpu(stats_msg.rx_multicast) +
2324 le64_to_cpu(stats_msg.rx_broadcast);
2325 netstats->tx_packets = le64_to_cpu(stats_msg.tx_unicast) +
2326 le64_to_cpu(stats_msg.tx_multicast) +
2327 le64_to_cpu(stats_msg.tx_broadcast);
2328 netstats->rx_bytes = le64_to_cpu(stats_msg.rx_bytes);
2329 netstats->tx_bytes = le64_to_cpu(stats_msg.tx_bytes);
2330 netstats->rx_errors = le64_to_cpu(stats_msg.rx_errors);
2331 netstats->tx_errors = le64_to_cpu(stats_msg.tx_errors);
2332 netstats->rx_dropped = le64_to_cpu(stats_msg.rx_discards);
2333 netstats->tx_dropped = le64_to_cpu(stats_msg.tx_discards);
2335 vport->port_stats.vport_stats = stats_msg;
2337 spin_unlock_bh(&np->stats_lock);
2343 * idpf_send_get_set_rss_lut_msg - Send virtchnl get or set rss lut message
2344 * @vport: virtual port data structure
2345 * @get: flag to set or get rss look up table
2347 * Returns 0 on success, negative on failure.
2349 int idpf_send_get_set_rss_lut_msg(struct idpf_vport *vport, bool get)
2351 struct virtchnl2_rss_lut *recv_rl __free(kfree) = NULL;
2352 struct virtchnl2_rss_lut *rl __free(kfree) = NULL;
2353 struct idpf_vc_xn_params xn_params = {};
2354 struct idpf_rss_data *rss_data;
2355 int buf_size, lut_buf_size;
2360 &vport->adapter->vport_config[vport->idx]->user_config.rss_data;
2361 buf_size = struct_size(rl, lut, rss_data->rss_lut_size);
2362 rl = kzalloc(buf_size, GFP_KERNEL);
2366 rl->vport_id = cpu_to_le32(vport->vport_id);
2368 xn_params.timeout_ms = IDPF_VC_XN_DEFAULT_TIMEOUT_MSEC;
2369 xn_params.send_buf.iov_base = rl;
2370 xn_params.send_buf.iov_len = buf_size;
2373 recv_rl = kzalloc(IDPF_CTLQ_MAX_BUF_LEN, GFP_KERNEL);
2376 xn_params.vc_op = VIRTCHNL2_OP_GET_RSS_LUT;
2377 xn_params.recv_buf.iov_base = recv_rl;
2378 xn_params.recv_buf.iov_len = IDPF_CTLQ_MAX_BUF_LEN;
2380 rl->lut_entries = cpu_to_le16(rss_data->rss_lut_size);
2381 for (i = 0; i < rss_data->rss_lut_size; i++)
2382 rl->lut[i] = cpu_to_le32(rss_data->rss_lut[i]);
2384 xn_params.vc_op = VIRTCHNL2_OP_SET_RSS_LUT;
2386 reply_sz = idpf_vc_xn_exec(vport->adapter, &xn_params);
2391 if (reply_sz < sizeof(struct virtchnl2_rss_lut))
2394 lut_buf_size = le16_to_cpu(recv_rl->lut_entries) * sizeof(u32);
2395 if (reply_sz < lut_buf_size)
2398 /* size didn't change, we can reuse existing lut buf */
2399 if (rss_data->rss_lut_size == le16_to_cpu(recv_rl->lut_entries))
2402 rss_data->rss_lut_size = le16_to_cpu(recv_rl->lut_entries);
2403 kfree(rss_data->rss_lut);
2405 rss_data->rss_lut = kzalloc(lut_buf_size, GFP_KERNEL);
2406 if (!rss_data->rss_lut) {
2407 rss_data->rss_lut_size = 0;
2412 memcpy(rss_data->rss_lut, recv_rl->lut, rss_data->rss_lut_size);
2418 * idpf_send_get_set_rss_key_msg - Send virtchnl get or set rss key message
2419 * @vport: virtual port data structure
2420 * @get: flag to set or get rss look up table
2422 * Returns 0 on success, negative on failure
2424 int idpf_send_get_set_rss_key_msg(struct idpf_vport *vport, bool get)
2426 struct virtchnl2_rss_key *recv_rk __free(kfree) = NULL;
2427 struct virtchnl2_rss_key *rk __free(kfree) = NULL;
2428 struct idpf_vc_xn_params xn_params = {};
2429 struct idpf_rss_data *rss_data;
2435 &vport->adapter->vport_config[vport->idx]->user_config.rss_data;
2436 buf_size = struct_size(rk, key_flex, rss_data->rss_key_size);
2437 rk = kzalloc(buf_size, GFP_KERNEL);
2441 rk->vport_id = cpu_to_le32(vport->vport_id);
2442 xn_params.send_buf.iov_base = rk;
2443 xn_params.send_buf.iov_len = buf_size;
2444 xn_params.timeout_ms = IDPF_VC_XN_DEFAULT_TIMEOUT_MSEC;
2446 recv_rk = kzalloc(IDPF_CTLQ_MAX_BUF_LEN, GFP_KERNEL);
2450 xn_params.vc_op = VIRTCHNL2_OP_GET_RSS_KEY;
2451 xn_params.recv_buf.iov_base = recv_rk;
2452 xn_params.recv_buf.iov_len = IDPF_CTLQ_MAX_BUF_LEN;
2454 rk->key_len = cpu_to_le16(rss_data->rss_key_size);
2455 for (i = 0; i < rss_data->rss_key_size; i++)
2456 rk->key_flex[i] = rss_data->rss_key[i];
2458 xn_params.vc_op = VIRTCHNL2_OP_SET_RSS_KEY;
2461 reply_sz = idpf_vc_xn_exec(vport->adapter, &xn_params);
2466 if (reply_sz < sizeof(struct virtchnl2_rss_key))
2469 key_size = min_t(u16, NETDEV_RSS_KEY_LEN,
2470 le16_to_cpu(recv_rk->key_len));
2471 if (reply_sz < key_size)
2474 /* key len didn't change, reuse existing buf */
2475 if (rss_data->rss_key_size == key_size)
2478 rss_data->rss_key_size = key_size;
2479 kfree(rss_data->rss_key);
2480 rss_data->rss_key = kzalloc(key_size, GFP_KERNEL);
2481 if (!rss_data->rss_key) {
2482 rss_data->rss_key_size = 0;
2487 memcpy(rss_data->rss_key, recv_rk->key_flex, rss_data->rss_key_size);
2493 * idpf_fill_ptype_lookup - Fill L3 specific fields in ptype lookup table
2494 * @ptype: ptype lookup table
2495 * @pstate: state machine for ptype lookup table
2496 * @ipv4: ipv4 or ipv6
2497 * @frag: fragmentation allowed
2500 static void idpf_fill_ptype_lookup(struct libeth_rx_pt *ptype,
2501 struct idpf_ptype_state *pstate,
2502 bool ipv4, bool frag)
2504 if (!pstate->outer_ip || !pstate->outer_frag) {
2505 pstate->outer_ip = true;
2508 ptype->outer_ip = LIBETH_RX_PT_OUTER_IPV4;
2510 ptype->outer_ip = LIBETH_RX_PT_OUTER_IPV6;
2513 ptype->outer_frag = LIBETH_RX_PT_FRAG;
2514 pstate->outer_frag = true;
2517 ptype->tunnel_type = LIBETH_RX_PT_TUNNEL_IP_IP;
2518 pstate->tunnel_state = IDPF_PTYPE_TUNNEL_IP;
2521 ptype->tunnel_end_prot = LIBETH_RX_PT_TUNNEL_END_IPV4;
2523 ptype->tunnel_end_prot = LIBETH_RX_PT_TUNNEL_END_IPV6;
2526 ptype->tunnel_end_frag = LIBETH_RX_PT_FRAG;
2530 static void idpf_finalize_ptype_lookup(struct libeth_rx_pt *ptype)
2532 if (ptype->payload_layer == LIBETH_RX_PT_PAYLOAD_L2 &&
2534 ptype->payload_layer = LIBETH_RX_PT_PAYLOAD_L4;
2535 else if (ptype->payload_layer == LIBETH_RX_PT_PAYLOAD_L2 &&
2537 ptype->payload_layer = LIBETH_RX_PT_PAYLOAD_L3;
2538 else if (ptype->outer_ip == LIBETH_RX_PT_OUTER_L2)
2539 ptype->payload_layer = LIBETH_RX_PT_PAYLOAD_L2;
2541 ptype->payload_layer = LIBETH_RX_PT_PAYLOAD_NONE;
2543 libeth_rx_pt_gen_hash_type(ptype);
2547 * idpf_send_get_rx_ptype_msg - Send virtchnl for ptype info
2548 * @vport: virtual port data structure
2550 * Returns 0 on success, negative on failure.
2552 int idpf_send_get_rx_ptype_msg(struct idpf_vport *vport)
2554 struct virtchnl2_get_ptype_info *get_ptype_info __free(kfree) = NULL;
2555 struct virtchnl2_get_ptype_info *ptype_info __free(kfree) = NULL;
2556 struct libeth_rx_pt *ptype_lkup __free(kfree) = NULL;
2557 int max_ptype, ptypes_recvd = 0, ptype_offset;
2558 struct idpf_adapter *adapter = vport->adapter;
2559 struct idpf_vc_xn_params xn_params = {};
2560 u16 next_ptype_id = 0;
2564 if (vport->rx_ptype_lkup)
2567 if (idpf_is_queue_model_split(vport->rxq_model))
2568 max_ptype = IDPF_RX_MAX_PTYPE;
2570 max_ptype = IDPF_RX_MAX_BASE_PTYPE;
2572 ptype_lkup = kcalloc(max_ptype, sizeof(*ptype_lkup), GFP_KERNEL);
2576 get_ptype_info = kzalloc(sizeof(*get_ptype_info), GFP_KERNEL);
2577 if (!get_ptype_info)
2580 ptype_info = kzalloc(IDPF_CTLQ_MAX_BUF_LEN, GFP_KERNEL);
2584 xn_params.vc_op = VIRTCHNL2_OP_GET_PTYPE_INFO;
2585 xn_params.send_buf.iov_base = get_ptype_info;
2586 xn_params.send_buf.iov_len = sizeof(*get_ptype_info);
2587 xn_params.recv_buf.iov_base = ptype_info;
2588 xn_params.recv_buf.iov_len = IDPF_CTLQ_MAX_BUF_LEN;
2589 xn_params.timeout_ms = IDPF_VC_XN_DEFAULT_TIMEOUT_MSEC;
2591 while (next_ptype_id < max_ptype) {
2592 get_ptype_info->start_ptype_id = cpu_to_le16(next_ptype_id);
2594 if ((next_ptype_id + IDPF_RX_MAX_PTYPES_PER_BUF) > max_ptype)
2595 get_ptype_info->num_ptypes =
2596 cpu_to_le16(max_ptype - next_ptype_id);
2598 get_ptype_info->num_ptypes =
2599 cpu_to_le16(IDPF_RX_MAX_PTYPES_PER_BUF);
2601 reply_sz = idpf_vc_xn_exec(adapter, &xn_params);
2605 if (reply_sz < IDPF_CTLQ_MAX_BUF_LEN)
2608 ptypes_recvd += le16_to_cpu(ptype_info->num_ptypes);
2609 if (ptypes_recvd > max_ptype)
2612 next_ptype_id = le16_to_cpu(get_ptype_info->start_ptype_id) +
2613 le16_to_cpu(get_ptype_info->num_ptypes);
2615 ptype_offset = IDPF_RX_PTYPE_HDR_SZ;
2617 for (i = 0; i < le16_to_cpu(ptype_info->num_ptypes); i++) {
2618 struct idpf_ptype_state pstate = { };
2619 struct virtchnl2_ptype *ptype;
2622 ptype = (struct virtchnl2_ptype *)
2623 ((u8 *)ptype_info + ptype_offset);
2625 ptype_offset += IDPF_GET_PTYPE_SIZE(ptype);
2626 if (ptype_offset > IDPF_CTLQ_MAX_BUF_LEN)
2629 /* 0xFFFF indicates end of ptypes */
2630 if (le16_to_cpu(ptype->ptype_id_10) ==
2631 IDPF_INVALID_PTYPE_ID)
2634 if (idpf_is_queue_model_split(vport->rxq_model))
2635 k = le16_to_cpu(ptype->ptype_id_10);
2637 k = ptype->ptype_id_8;
2639 for (j = 0; j < ptype->proto_id_count; j++) {
2640 id = le16_to_cpu(ptype->proto_id[j]);
2642 case VIRTCHNL2_PROTO_HDR_GRE:
2643 if (pstate.tunnel_state ==
2644 IDPF_PTYPE_TUNNEL_IP) {
2645 ptype_lkup[k].tunnel_type =
2646 LIBETH_RX_PT_TUNNEL_IP_GRENAT;
2647 pstate.tunnel_state |=
2648 IDPF_PTYPE_TUNNEL_IP_GRENAT;
2651 case VIRTCHNL2_PROTO_HDR_MAC:
2652 ptype_lkup[k].outer_ip =
2653 LIBETH_RX_PT_OUTER_L2;
2654 if (pstate.tunnel_state ==
2656 ptype_lkup[k].tunnel_type =
2657 LIBETH_RX_PT_TUNNEL_IP_GRENAT_MAC;
2658 pstate.tunnel_state |=
2659 IDPF_PTYPE_TUNNEL_IP_GRENAT_MAC;
2662 case VIRTCHNL2_PROTO_HDR_IPV4:
2663 idpf_fill_ptype_lookup(&ptype_lkup[k],
2667 case VIRTCHNL2_PROTO_HDR_IPV6:
2668 idpf_fill_ptype_lookup(&ptype_lkup[k],
2672 case VIRTCHNL2_PROTO_HDR_IPV4_FRAG:
2673 idpf_fill_ptype_lookup(&ptype_lkup[k],
2677 case VIRTCHNL2_PROTO_HDR_IPV6_FRAG:
2678 idpf_fill_ptype_lookup(&ptype_lkup[k],
2682 case VIRTCHNL2_PROTO_HDR_UDP:
2683 ptype_lkup[k].inner_prot =
2684 LIBETH_RX_PT_INNER_UDP;
2686 case VIRTCHNL2_PROTO_HDR_TCP:
2687 ptype_lkup[k].inner_prot =
2688 LIBETH_RX_PT_INNER_TCP;
2690 case VIRTCHNL2_PROTO_HDR_SCTP:
2691 ptype_lkup[k].inner_prot =
2692 LIBETH_RX_PT_INNER_SCTP;
2694 case VIRTCHNL2_PROTO_HDR_ICMP:
2695 ptype_lkup[k].inner_prot =
2696 LIBETH_RX_PT_INNER_ICMP;
2698 case VIRTCHNL2_PROTO_HDR_PAY:
2699 ptype_lkup[k].payload_layer =
2700 LIBETH_RX_PT_PAYLOAD_L2;
2702 case VIRTCHNL2_PROTO_HDR_ICMPV6:
2703 case VIRTCHNL2_PROTO_HDR_IPV6_EH:
2704 case VIRTCHNL2_PROTO_HDR_PRE_MAC:
2705 case VIRTCHNL2_PROTO_HDR_POST_MAC:
2706 case VIRTCHNL2_PROTO_HDR_ETHERTYPE:
2707 case VIRTCHNL2_PROTO_HDR_SVLAN:
2708 case VIRTCHNL2_PROTO_HDR_CVLAN:
2709 case VIRTCHNL2_PROTO_HDR_MPLS:
2710 case VIRTCHNL2_PROTO_HDR_MMPLS:
2711 case VIRTCHNL2_PROTO_HDR_PTP:
2712 case VIRTCHNL2_PROTO_HDR_CTRL:
2713 case VIRTCHNL2_PROTO_HDR_LLDP:
2714 case VIRTCHNL2_PROTO_HDR_ARP:
2715 case VIRTCHNL2_PROTO_HDR_ECP:
2716 case VIRTCHNL2_PROTO_HDR_EAPOL:
2717 case VIRTCHNL2_PROTO_HDR_PPPOD:
2718 case VIRTCHNL2_PROTO_HDR_PPPOE:
2719 case VIRTCHNL2_PROTO_HDR_IGMP:
2720 case VIRTCHNL2_PROTO_HDR_AH:
2721 case VIRTCHNL2_PROTO_HDR_ESP:
2722 case VIRTCHNL2_PROTO_HDR_IKE:
2723 case VIRTCHNL2_PROTO_HDR_NATT_KEEP:
2724 case VIRTCHNL2_PROTO_HDR_L2TPV2:
2725 case VIRTCHNL2_PROTO_HDR_L2TPV2_CONTROL:
2726 case VIRTCHNL2_PROTO_HDR_L2TPV3:
2727 case VIRTCHNL2_PROTO_HDR_GTP:
2728 case VIRTCHNL2_PROTO_HDR_GTP_EH:
2729 case VIRTCHNL2_PROTO_HDR_GTPCV2:
2730 case VIRTCHNL2_PROTO_HDR_GTPC_TEID:
2731 case VIRTCHNL2_PROTO_HDR_GTPU:
2732 case VIRTCHNL2_PROTO_HDR_GTPU_UL:
2733 case VIRTCHNL2_PROTO_HDR_GTPU_DL:
2734 case VIRTCHNL2_PROTO_HDR_ECPRI:
2735 case VIRTCHNL2_PROTO_HDR_VRRP:
2736 case VIRTCHNL2_PROTO_HDR_OSPF:
2737 case VIRTCHNL2_PROTO_HDR_TUN:
2738 case VIRTCHNL2_PROTO_HDR_NVGRE:
2739 case VIRTCHNL2_PROTO_HDR_VXLAN:
2740 case VIRTCHNL2_PROTO_HDR_VXLAN_GPE:
2741 case VIRTCHNL2_PROTO_HDR_GENEVE:
2742 case VIRTCHNL2_PROTO_HDR_NSH:
2743 case VIRTCHNL2_PROTO_HDR_QUIC:
2744 case VIRTCHNL2_PROTO_HDR_PFCP:
2745 case VIRTCHNL2_PROTO_HDR_PFCP_NODE:
2746 case VIRTCHNL2_PROTO_HDR_PFCP_SESSION:
2747 case VIRTCHNL2_PROTO_HDR_RTP:
2748 case VIRTCHNL2_PROTO_HDR_NO_PROTO:
2755 idpf_finalize_ptype_lookup(&ptype_lkup[k]);
2760 vport->rx_ptype_lkup = no_free_ptr(ptype_lkup);
2766 * idpf_send_ena_dis_loopback_msg - Send virtchnl enable/disable loopback
2768 * @vport: virtual port data structure
2770 * Returns 0 on success, negative on failure.
2772 int idpf_send_ena_dis_loopback_msg(struct idpf_vport *vport)
2774 struct idpf_vc_xn_params xn_params = {};
2775 struct virtchnl2_loopback loopback;
2778 loopback.vport_id = cpu_to_le32(vport->vport_id);
2779 loopback.enable = idpf_is_feature_ena(vport, NETIF_F_LOOPBACK);
2781 xn_params.vc_op = VIRTCHNL2_OP_LOOPBACK;
2782 xn_params.timeout_ms = IDPF_VC_XN_DEFAULT_TIMEOUT_MSEC;
2783 xn_params.send_buf.iov_base = &loopback;
2784 xn_params.send_buf.iov_len = sizeof(loopback);
2785 reply_sz = idpf_vc_xn_exec(vport->adapter, &xn_params);
2787 return reply_sz < 0 ? reply_sz : 0;
2791 * idpf_find_ctlq - Given a type and id, find ctlq info
2792 * @hw: hardware struct
2793 * @type: type of ctrlq to find
2794 * @id: ctlq id to find
2796 * Returns pointer to found ctlq info struct, NULL otherwise.
2798 static struct idpf_ctlq_info *idpf_find_ctlq(struct idpf_hw *hw,
2799 enum idpf_ctlq_type type, int id)
2801 struct idpf_ctlq_info *cq, *tmp;
2803 list_for_each_entry_safe(cq, tmp, &hw->cq_list_head, cq_list)
2804 if (cq->q_id == id && cq->cq_type == type)
2811 * idpf_init_dflt_mbx - Setup default mailbox parameters and make request
2812 * @adapter: adapter info struct
2814 * Returns 0 on success, negative otherwise
2816 int idpf_init_dflt_mbx(struct idpf_adapter *adapter)
2818 struct idpf_ctlq_create_info ctlq_info[] = {
2820 .type = IDPF_CTLQ_TYPE_MAILBOX_TX,
2821 .id = IDPF_DFLT_MBX_ID,
2822 .len = IDPF_DFLT_MBX_Q_LEN,
2823 .buf_size = IDPF_CTLQ_MAX_BUF_LEN
2826 .type = IDPF_CTLQ_TYPE_MAILBOX_RX,
2827 .id = IDPF_DFLT_MBX_ID,
2828 .len = IDPF_DFLT_MBX_Q_LEN,
2829 .buf_size = IDPF_CTLQ_MAX_BUF_LEN
2832 struct idpf_hw *hw = &adapter->hw;
2835 adapter->dev_ops.reg_ops.ctlq_reg_init(ctlq_info);
2837 err = idpf_ctlq_init(hw, IDPF_NUM_DFLT_MBX_Q, ctlq_info);
2841 hw->asq = idpf_find_ctlq(hw, IDPF_CTLQ_TYPE_MAILBOX_TX,
2843 hw->arq = idpf_find_ctlq(hw, IDPF_CTLQ_TYPE_MAILBOX_RX,
2846 if (!hw->asq || !hw->arq) {
2847 idpf_ctlq_deinit(hw);
2852 adapter->state = __IDPF_VER_CHECK;
2858 * idpf_deinit_dflt_mbx - Free up ctlqs setup
2859 * @adapter: Driver specific private data structure
2861 void idpf_deinit_dflt_mbx(struct idpf_adapter *adapter)
2863 if (adapter->hw.arq && adapter->hw.asq) {
2864 idpf_mb_clean(adapter);
2865 idpf_ctlq_deinit(&adapter->hw);
2867 adapter->hw.arq = NULL;
2868 adapter->hw.asq = NULL;
2872 * idpf_vport_params_buf_rel - Release memory for MailBox resources
2873 * @adapter: Driver specific private data structure
2875 * Will release memory to hold the vport parameters received on MailBox
2877 static void idpf_vport_params_buf_rel(struct idpf_adapter *adapter)
2879 kfree(adapter->vport_params_recvd);
2880 adapter->vport_params_recvd = NULL;
2881 kfree(adapter->vport_params_reqd);
2882 adapter->vport_params_reqd = NULL;
2883 kfree(adapter->vport_ids);
2884 adapter->vport_ids = NULL;
2888 * idpf_vport_params_buf_alloc - Allocate memory for MailBox resources
2889 * @adapter: Driver specific private data structure
2891 * Will alloc memory to hold the vport parameters received on MailBox
2893 static int idpf_vport_params_buf_alloc(struct idpf_adapter *adapter)
2895 u16 num_max_vports = idpf_get_max_vports(adapter);
2897 adapter->vport_params_reqd = kcalloc(num_max_vports,
2898 sizeof(*adapter->vport_params_reqd),
2900 if (!adapter->vport_params_reqd)
2903 adapter->vport_params_recvd = kcalloc(num_max_vports,
2904 sizeof(*adapter->vport_params_recvd),
2906 if (!adapter->vport_params_recvd)
2909 adapter->vport_ids = kcalloc(num_max_vports, sizeof(u32), GFP_KERNEL);
2910 if (!adapter->vport_ids)
2913 if (adapter->vport_config)
2916 adapter->vport_config = kcalloc(num_max_vports,
2917 sizeof(*adapter->vport_config),
2919 if (!adapter->vport_config)
2925 idpf_vport_params_buf_rel(adapter);
2931 * idpf_vc_core_init - Initialize state machine and get driver specific
2933 * @adapter: Driver specific private structure
2935 * This function will initialize the state machine and request all necessary
2936 * resources required by the device driver. Once the state machine is
2937 * initialized, allocate memory to store vport specific information and also
2938 * requests required interrupts.
2940 * Returns 0 on success, -EAGAIN function will get called again,
2941 * otherwise negative on failure.
2943 int idpf_vc_core_init(struct idpf_adapter *adapter)
2945 int task_delay = 30;
2949 if (!adapter->vcxn_mngr) {
2950 adapter->vcxn_mngr = kzalloc(sizeof(*adapter->vcxn_mngr), GFP_KERNEL);
2951 if (!adapter->vcxn_mngr) {
2956 idpf_vc_xn_init(adapter->vcxn_mngr);
2958 while (adapter->state != __IDPF_INIT_SW) {
2959 switch (adapter->state) {
2960 case __IDPF_VER_CHECK:
2961 err = idpf_send_ver_msg(adapter);
2964 /* success, move state machine forward */
2965 adapter->state = __IDPF_GET_CAPS;
2970 /* Something bad happened, try again but only a
2975 case __IDPF_GET_CAPS:
2976 err = idpf_send_get_caps_msg(adapter);
2979 adapter->state = __IDPF_INIT_SW;
2982 dev_err(&adapter->pdev->dev, "Device is in bad state: %d\n",
2989 /* Give enough time before proceeding further with
2995 pci_sriov_set_totalvfs(adapter->pdev, idpf_get_max_vfs(adapter));
2996 num_max_vports = idpf_get_max_vports(adapter);
2997 adapter->max_vports = num_max_vports;
2998 adapter->vports = kcalloc(num_max_vports, sizeof(*adapter->vports),
3000 if (!adapter->vports)
3003 if (!adapter->netdevs) {
3004 adapter->netdevs = kcalloc(num_max_vports,
3005 sizeof(struct net_device *),
3007 if (!adapter->netdevs) {
3009 goto err_netdev_alloc;
3013 err = idpf_vport_params_buf_alloc(adapter);
3015 dev_err(&adapter->pdev->dev, "Failed to alloc vport params buffer: %d\n",
3017 goto err_netdev_alloc;
3020 /* Start the mailbox task before requesting vectors. This will ensure
3021 * vector information response from mailbox is handled
3023 queue_delayed_work(adapter->mbx_wq, &adapter->mbx_task, 0);
3025 queue_delayed_work(adapter->serv_wq, &adapter->serv_task,
3026 msecs_to_jiffies(5 * (adapter->pdev->devfn & 0x07)));
3028 err = idpf_intr_req(adapter);
3030 dev_err(&adapter->pdev->dev, "failed to enable interrupt vectors: %d\n",
3035 idpf_init_avail_queues(adapter);
3037 /* Skew the delay for init tasks for each function based on fn number
3038 * to prevent every function from making the same call simultaneously.
3040 queue_delayed_work(adapter->init_wq, &adapter->init_task,
3041 msecs_to_jiffies(5 * (adapter->pdev->devfn & 0x07)));
3043 set_bit(IDPF_VC_CORE_INIT, adapter->flags);
3048 cancel_delayed_work_sync(&adapter->serv_task);
3049 cancel_delayed_work_sync(&adapter->mbx_task);
3050 idpf_vport_params_buf_rel(adapter);
3052 kfree(adapter->vports);
3053 adapter->vports = NULL;
3057 /* Don't retry if we're trying to go down, just bail. */
3058 if (test_bit(IDPF_REMOVE_IN_PROG, adapter->flags))
3061 if (++adapter->mb_wait_count > IDPF_MB_MAX_ERR) {
3062 dev_err(&adapter->pdev->dev, "Failed to establish mailbox communications with hardware\n");
3066 /* If it reached here, it is possible that mailbox queue initialization
3067 * register writes might not have taken effect. Retry to initialize
3070 adapter->state = __IDPF_VER_CHECK;
3071 if (adapter->vcxn_mngr)
3072 idpf_vc_xn_shutdown(adapter->vcxn_mngr);
3073 idpf_deinit_dflt_mbx(adapter);
3074 set_bit(IDPF_HR_DRV_LOAD, adapter->flags);
3075 queue_delayed_work(adapter->vc_event_wq, &adapter->vc_event_task,
3076 msecs_to_jiffies(task_delay));
3082 * idpf_vc_core_deinit - Device deinit routine
3083 * @adapter: Driver specific private structure
3086 void idpf_vc_core_deinit(struct idpf_adapter *adapter)
3088 if (!test_bit(IDPF_VC_CORE_INIT, adapter->flags))
3091 idpf_vc_xn_shutdown(adapter->vcxn_mngr);
3092 idpf_deinit_task(adapter);
3093 idpf_intr_rel(adapter);
3095 cancel_delayed_work_sync(&adapter->serv_task);
3096 cancel_delayed_work_sync(&adapter->mbx_task);
3098 idpf_vport_params_buf_rel(adapter);
3100 kfree(adapter->vports);
3101 adapter->vports = NULL;
3103 clear_bit(IDPF_VC_CORE_INIT, adapter->flags);
3107 * idpf_vport_alloc_vec_indexes - Get relative vector indexes
3108 * @vport: virtual port data struct
3110 * This function requests the vector information required for the vport and
3111 * stores the vector indexes received from the 'global vector distribution'
3112 * in the vport's queue vectors array.
3114 * Return 0 on success, error on failure
3116 int idpf_vport_alloc_vec_indexes(struct idpf_vport *vport)
3118 struct idpf_vector_info vec_info;
3121 vec_info.num_curr_vecs = vport->num_q_vectors;
3122 vec_info.num_req_vecs = max(vport->num_txq, vport->num_rxq);
3123 vec_info.default_vport = vport->default_vport;
3124 vec_info.index = vport->idx;
3126 num_alloc_vecs = idpf_req_rel_vector_indexes(vport->adapter,
3127 vport->q_vector_idxs,
3129 if (num_alloc_vecs <= 0) {
3130 dev_err(&vport->adapter->pdev->dev, "Vector distribution failed: %d\n",
3135 vport->num_q_vectors = num_alloc_vecs;
3141 * idpf_vport_init - Initialize virtual port
3142 * @vport: virtual port to be initialized
3143 * @max_q: vport max queue info
3145 * Will initialize vport with the info received through MB earlier
3147 void idpf_vport_init(struct idpf_vport *vport, struct idpf_vport_max_q *max_q)
3149 struct idpf_adapter *adapter = vport->adapter;
3150 struct virtchnl2_create_vport *vport_msg;
3151 struct idpf_vport_config *vport_config;
3152 u16 tx_itr[] = {2, 8, 64, 128, 256};
3153 u16 rx_itr[] = {2, 8, 32, 96, 128};
3154 struct idpf_rss_data *rss_data;
3155 u16 idx = vport->idx;
3157 vport_config = adapter->vport_config[idx];
3158 rss_data = &vport_config->user_config.rss_data;
3159 vport_msg = adapter->vport_params_recvd[idx];
3161 vport_config->max_q.max_txq = max_q->max_txq;
3162 vport_config->max_q.max_rxq = max_q->max_rxq;
3163 vport_config->max_q.max_complq = max_q->max_complq;
3164 vport_config->max_q.max_bufq = max_q->max_bufq;
3166 vport->txq_model = le16_to_cpu(vport_msg->txq_model);
3167 vport->rxq_model = le16_to_cpu(vport_msg->rxq_model);
3168 vport->vport_type = le16_to_cpu(vport_msg->vport_type);
3169 vport->vport_id = le32_to_cpu(vport_msg->vport_id);
3171 rss_data->rss_key_size = min_t(u16, NETDEV_RSS_KEY_LEN,
3172 le16_to_cpu(vport_msg->rss_key_size));
3173 rss_data->rss_lut_size = le16_to_cpu(vport_msg->rss_lut_size);
3175 ether_addr_copy(vport->default_mac_addr, vport_msg->default_mac_addr);
3176 vport->max_mtu = le16_to_cpu(vport_msg->max_mtu) - LIBETH_RX_LL_LEN;
3178 /* Initialize Tx and Rx profiles for Dynamic Interrupt Moderation */
3179 memcpy(vport->rx_itr_profile, rx_itr, IDPF_DIM_PROFILE_SLOTS);
3180 memcpy(vport->tx_itr_profile, tx_itr, IDPF_DIM_PROFILE_SLOTS);
3182 idpf_vport_set_hsplit(vport, ETHTOOL_TCP_DATA_SPLIT_ENABLED);
3184 idpf_vport_init_num_qs(vport, vport_msg);
3185 idpf_vport_calc_num_q_desc(vport);
3186 idpf_vport_calc_num_q_groups(vport);
3187 idpf_vport_alloc_vec_indexes(vport);
3189 vport->crc_enable = adapter->crc_enable;
3193 * idpf_get_vec_ids - Initialize vector id from Mailbox parameters
3194 * @adapter: adapter structure to get the mailbox vector id
3195 * @vecids: Array of vector ids
3196 * @num_vecids: number of vector ids
3197 * @chunks: vector ids received over mailbox
3199 * Will initialize the mailbox vector id which is received from the
3200 * get capabilities and data queue vector ids with ids received as
3201 * mailbox parameters.
3202 * Returns number of ids filled
3204 int idpf_get_vec_ids(struct idpf_adapter *adapter,
3205 u16 *vecids, int num_vecids,
3206 struct virtchnl2_vector_chunks *chunks)
3208 u16 num_chunks = le16_to_cpu(chunks->num_vchunks);
3209 int num_vecid_filled = 0;
3212 vecids[num_vecid_filled] = adapter->mb_vector.v_idx;
3215 for (j = 0; j < num_chunks; j++) {
3216 struct virtchnl2_vector_chunk *chunk;
3217 u16 start_vecid, num_vec;
3219 chunk = &chunks->vchunks[j];
3220 num_vec = le16_to_cpu(chunk->num_vectors);
3221 start_vecid = le16_to_cpu(chunk->start_vector_id);
3223 for (i = 0; i < num_vec; i++) {
3224 if ((num_vecid_filled + i) < num_vecids) {
3225 vecids[num_vecid_filled + i] = start_vecid;
3231 num_vecid_filled = num_vecid_filled + i;
3234 return num_vecid_filled;
3238 * idpf_vport_get_queue_ids - Initialize queue id from Mailbox parameters
3239 * @qids: Array of queue ids
3240 * @num_qids: number of queue ids
3241 * @q_type: queue model
3242 * @chunks: queue ids received over mailbox
3244 * Will initialize all queue ids with ids received as mailbox parameters
3245 * Returns number of ids filled
3247 static int idpf_vport_get_queue_ids(u32 *qids, int num_qids, u16 q_type,
3248 struct virtchnl2_queue_reg_chunks *chunks)
3250 u16 num_chunks = le16_to_cpu(chunks->num_chunks);
3251 u32 num_q_id_filled = 0, i;
3252 u32 start_q_id, num_q;
3254 while (num_chunks--) {
3255 struct virtchnl2_queue_reg_chunk *chunk;
3257 chunk = &chunks->chunks[num_chunks];
3258 if (le32_to_cpu(chunk->type) != q_type)
3261 num_q = le32_to_cpu(chunk->num_queues);
3262 start_q_id = le32_to_cpu(chunk->start_queue_id);
3264 for (i = 0; i < num_q; i++) {
3265 if ((num_q_id_filled + i) < num_qids) {
3266 qids[num_q_id_filled + i] = start_q_id;
3272 num_q_id_filled = num_q_id_filled + i;
3275 return num_q_id_filled;
3279 * __idpf_vport_queue_ids_init - Initialize queue ids from Mailbox parameters
3280 * @vport: virtual port for which the queues ids are initialized
3282 * @num_qids: number of queue ids
3283 * @q_type: type of queue
3285 * Will initialize all queue ids with ids received as mailbox
3286 * parameters. Returns number of queue ids initialized.
3288 static int __idpf_vport_queue_ids_init(struct idpf_vport *vport,
3296 case VIRTCHNL2_QUEUE_TYPE_TX:
3297 for (i = 0; i < vport->num_txq_grp; i++) {
3298 struct idpf_txq_group *tx_qgrp = &vport->txq_grps[i];
3300 for (j = 0; j < tx_qgrp->num_txq && k < num_qids; j++, k++)
3301 tx_qgrp->txqs[j]->q_id = qids[k];
3304 case VIRTCHNL2_QUEUE_TYPE_RX:
3305 for (i = 0; i < vport->num_rxq_grp; i++) {
3306 struct idpf_rxq_group *rx_qgrp = &vport->rxq_grps[i];
3309 if (idpf_is_queue_model_split(vport->rxq_model))
3310 num_rxq = rx_qgrp->splitq.num_rxq_sets;
3312 num_rxq = rx_qgrp->singleq.num_rxq;
3314 for (j = 0; j < num_rxq && k < num_qids; j++, k++) {
3315 struct idpf_rx_queue *q;
3317 if (idpf_is_queue_model_split(vport->rxq_model))
3318 q = &rx_qgrp->splitq.rxq_sets[j]->rxq;
3320 q = rx_qgrp->singleq.rxqs[j];
3325 case VIRTCHNL2_QUEUE_TYPE_TX_COMPLETION:
3326 for (i = 0; i < vport->num_txq_grp && k < num_qids; i++, k++) {
3327 struct idpf_txq_group *tx_qgrp = &vport->txq_grps[i];
3329 tx_qgrp->complq->q_id = qids[k];
3332 case VIRTCHNL2_QUEUE_TYPE_RX_BUFFER:
3333 for (i = 0; i < vport->num_rxq_grp; i++) {
3334 struct idpf_rxq_group *rx_qgrp = &vport->rxq_grps[i];
3335 u8 num_bufqs = vport->num_bufqs_per_qgrp;
3337 for (j = 0; j < num_bufqs && k < num_qids; j++, k++) {
3338 struct idpf_buf_queue *q;
3340 q = &rx_qgrp->splitq.bufq_sets[j].bufq;
3353 * idpf_vport_queue_ids_init - Initialize queue ids from Mailbox parameters
3354 * @vport: virtual port for which the queues ids are initialized
3356 * Will initialize all queue ids with ids received as mailbox parameters.
3357 * Returns 0 on success, negative if all the queues are not initialized.
3359 int idpf_vport_queue_ids_init(struct idpf_vport *vport)
3361 struct virtchnl2_create_vport *vport_params;
3362 struct virtchnl2_queue_reg_chunks *chunks;
3363 struct idpf_vport_config *vport_config;
3364 u16 vport_idx = vport->idx;
3365 int num_ids, err = 0;
3369 vport_config = vport->adapter->vport_config[vport_idx];
3370 if (vport_config->req_qs_chunks) {
3371 struct virtchnl2_add_queues *vc_aq =
3372 (struct virtchnl2_add_queues *)vport_config->req_qs_chunks;
3373 chunks = &vc_aq->chunks;
3375 vport_params = vport->adapter->vport_params_recvd[vport_idx];
3376 chunks = &vport_params->chunks;
3379 qids = kcalloc(IDPF_MAX_QIDS, sizeof(u32), GFP_KERNEL);
3383 num_ids = idpf_vport_get_queue_ids(qids, IDPF_MAX_QIDS,
3384 VIRTCHNL2_QUEUE_TYPE_TX,
3386 if (num_ids < vport->num_txq) {
3390 num_ids = __idpf_vport_queue_ids_init(vport, qids, num_ids,
3391 VIRTCHNL2_QUEUE_TYPE_TX);
3392 if (num_ids < vport->num_txq) {
3397 num_ids = idpf_vport_get_queue_ids(qids, IDPF_MAX_QIDS,
3398 VIRTCHNL2_QUEUE_TYPE_RX,
3400 if (num_ids < vport->num_rxq) {
3404 num_ids = __idpf_vport_queue_ids_init(vport, qids, num_ids,
3405 VIRTCHNL2_QUEUE_TYPE_RX);
3406 if (num_ids < vport->num_rxq) {
3411 if (!idpf_is_queue_model_split(vport->txq_model))
3414 q_type = VIRTCHNL2_QUEUE_TYPE_TX_COMPLETION;
3415 num_ids = idpf_vport_get_queue_ids(qids, IDPF_MAX_QIDS, q_type, chunks);
3416 if (num_ids < vport->num_complq) {
3420 num_ids = __idpf_vport_queue_ids_init(vport, qids, num_ids, q_type);
3421 if (num_ids < vport->num_complq) {
3427 if (!idpf_is_queue_model_split(vport->rxq_model))
3430 q_type = VIRTCHNL2_QUEUE_TYPE_RX_BUFFER;
3431 num_ids = idpf_vport_get_queue_ids(qids, IDPF_MAX_QIDS, q_type, chunks);
3432 if (num_ids < vport->num_bufq) {
3436 num_ids = __idpf_vport_queue_ids_init(vport, qids, num_ids, q_type);
3437 if (num_ids < vport->num_bufq)
3447 * idpf_vport_adjust_qs - Adjust to new requested queues
3448 * @vport: virtual port data struct
3450 * Renegotiate queues. Returns 0 on success, negative on failure.
3452 int idpf_vport_adjust_qs(struct idpf_vport *vport)
3454 struct virtchnl2_create_vport vport_msg;
3457 vport_msg.txq_model = cpu_to_le16(vport->txq_model);
3458 vport_msg.rxq_model = cpu_to_le16(vport->rxq_model);
3459 err = idpf_vport_calc_total_qs(vport->adapter, vport->idx, &vport_msg,
3464 idpf_vport_init_num_qs(vport, &vport_msg);
3465 idpf_vport_calc_num_q_groups(vport);
3471 * idpf_is_capability_ena - Default implementation of capability checking
3472 * @adapter: Private data struct
3473 * @all: all or one flag
3474 * @field: caps field to check for flags
3475 * @flag: flag to check
3477 * Return true if all capabilities are supported, false otherwise
3479 bool idpf_is_capability_ena(struct idpf_adapter *adapter, bool all,
3480 enum idpf_cap_field field, u64 flag)
3482 u8 *caps = (u8 *)&adapter->caps;
3488 if (field == IDPF_BASE_CAPS)
3491 cap_field = (u32 *)(caps + field);
3494 return (*cap_field & flag) == flag;
3496 return !!(*cap_field & flag);
3500 * idpf_get_vport_id: Get vport id
3501 * @vport: virtual port structure
3503 * Return vport id from the adapter persistent data
3505 u32 idpf_get_vport_id(struct idpf_vport *vport)
3507 struct virtchnl2_create_vport *vport_msg;
3509 vport_msg = vport->adapter->vport_params_recvd[vport->idx];
3511 return le32_to_cpu(vport_msg->vport_id);
3515 * idpf_mac_filter_async_handler - Async callback for mac filters
3516 * @adapter: private data struct
3517 * @xn: transaction for message
3518 * @ctlq_msg: received message
3520 * In some scenarios driver can't sleep and wait for a reply (e.g.: stack is
3521 * holding rtnl_lock) when adding a new mac filter. It puts us in a difficult
3522 * situation to deal with errors returned on the reply. The best we can
3523 * ultimately do is remove it from our list of mac filters and report the
3526 static int idpf_mac_filter_async_handler(struct idpf_adapter *adapter,
3527 struct idpf_vc_xn *xn,
3528 const struct idpf_ctlq_msg *ctlq_msg)
3530 struct virtchnl2_mac_addr_list *ma_list;
3531 struct idpf_vport_config *vport_config;
3532 struct virtchnl2_mac_addr *mac_addr;
3533 struct idpf_mac_filter *f, *tmp;
3534 struct list_head *ma_list_head;
3535 struct idpf_vport *vport;
3539 /* if success we're done, we're only here if something bad happened */
3540 if (!ctlq_msg->cookie.mbx.chnl_retval)
3543 /* make sure at least struct is there */
3544 if (xn->reply_sz < sizeof(*ma_list))
3545 goto invalid_payload;
3547 ma_list = ctlq_msg->ctx.indirect.payload->va;
3548 mac_addr = ma_list->mac_addr_list;
3549 num_entries = le16_to_cpu(ma_list->num_mac_addr);
3550 /* we should have received a buffer at least this big */
3551 if (xn->reply_sz < struct_size(ma_list, mac_addr_list, num_entries))
3552 goto invalid_payload;
3554 vport = idpf_vid_to_vport(adapter, le32_to_cpu(ma_list->vport_id));
3556 goto invalid_payload;
3558 vport_config = adapter->vport_config[le32_to_cpu(ma_list->vport_id)];
3559 ma_list_head = &vport_config->user_config.mac_filter_list;
3561 /* We can't do much to reconcile bad filters at this point, however we
3562 * should at least remove them from our list one way or the other so we
3563 * have some idea what good filters we have.
3565 spin_lock_bh(&vport_config->mac_filter_list_lock);
3566 list_for_each_entry_safe(f, tmp, ma_list_head, list)
3567 for (i = 0; i < num_entries; i++)
3568 if (ether_addr_equal(mac_addr[i].addr, f->macaddr))
3570 spin_unlock_bh(&vport_config->mac_filter_list_lock);
3571 dev_err_ratelimited(&adapter->pdev->dev, "Received error sending MAC filter request (op %d)\n",
3577 dev_err_ratelimited(&adapter->pdev->dev, "Received invalid MAC filter payload (op %d) (len %zd)\n",
3578 xn->vc_op, xn->reply_sz);
3584 * idpf_add_del_mac_filters - Add/del mac filters
3585 * @vport: Virtual port data structure
3586 * @np: Netdev private structure
3587 * @add: Add or delete flag
3588 * @async: Don't wait for return message
3590 * Returns 0 on success, error on failure.
3592 int idpf_add_del_mac_filters(struct idpf_vport *vport,
3593 struct idpf_netdev_priv *np,
3594 bool add, bool async)
3596 struct virtchnl2_mac_addr_list *ma_list __free(kfree) = NULL;
3597 struct virtchnl2_mac_addr *mac_addr __free(kfree) = NULL;
3598 struct idpf_adapter *adapter = np->adapter;
3599 struct idpf_vc_xn_params xn_params = {};
3600 struct idpf_vport_config *vport_config;
3601 u32 num_msgs, total_filters = 0;
3602 struct idpf_mac_filter *f;
3606 xn_params.vc_op = add ? VIRTCHNL2_OP_ADD_MAC_ADDR :
3607 VIRTCHNL2_OP_DEL_MAC_ADDR;
3608 xn_params.timeout_ms = IDPF_VC_XN_DEFAULT_TIMEOUT_MSEC;
3609 xn_params.async = async;
3610 xn_params.async_handler = idpf_mac_filter_async_handler;
3612 vport_config = adapter->vport_config[np->vport_idx];
3613 spin_lock_bh(&vport_config->mac_filter_list_lock);
3615 /* Find the number of newly added filters */
3616 list_for_each_entry(f, &vport_config->user_config.mac_filter_list,
3620 else if (!add && f->remove)
3624 if (!total_filters) {
3625 spin_unlock_bh(&vport_config->mac_filter_list_lock);
3630 /* Fill all the new filters into virtchannel message */
3631 mac_addr = kcalloc(total_filters, sizeof(struct virtchnl2_mac_addr),
3634 spin_unlock_bh(&vport_config->mac_filter_list_lock);
3639 list_for_each_entry(f, &vport_config->user_config.mac_filter_list,
3641 if (add && f->add) {
3642 ether_addr_copy(mac_addr[i].addr, f->macaddr);
3645 if (i == total_filters)
3648 if (!add && f->remove) {
3649 ether_addr_copy(mac_addr[i].addr, f->macaddr);
3652 if (i == total_filters)
3657 spin_unlock_bh(&vport_config->mac_filter_list_lock);
3659 /* Chunk up the filters into multiple messages to avoid
3660 * sending a control queue message buffer that is too large
3662 num_msgs = DIV_ROUND_UP(total_filters, IDPF_NUM_FILTERS_PER_MSG);
3664 for (i = 0, k = 0; i < num_msgs; i++) {
3665 u32 entries_size, buf_size, num_entries;
3667 num_entries = min_t(u32, total_filters,
3668 IDPF_NUM_FILTERS_PER_MSG);
3669 entries_size = sizeof(struct virtchnl2_mac_addr) * num_entries;
3670 buf_size = struct_size(ma_list, mac_addr_list, num_entries);
3672 if (!ma_list || num_entries != IDPF_NUM_FILTERS_PER_MSG) {
3674 ma_list = kzalloc(buf_size, GFP_ATOMIC);
3678 memset(ma_list, 0, buf_size);
3681 ma_list->vport_id = cpu_to_le32(np->vport_id);
3682 ma_list->num_mac_addr = cpu_to_le16(num_entries);
3683 memcpy(ma_list->mac_addr_list, &mac_addr[k], entries_size);
3685 xn_params.send_buf.iov_base = ma_list;
3686 xn_params.send_buf.iov_len = buf_size;
3687 reply_sz = idpf_vc_xn_exec(adapter, &xn_params);
3692 total_filters -= num_entries;
3699 * idpf_set_promiscuous - set promiscuous and send message to mailbox
3700 * @adapter: Driver specific private structure
3701 * @config_data: Vport specific config data
3702 * @vport_id: Vport identifier
3704 * Request to enable promiscuous mode for the vport. Message is sent
3705 * asynchronously and won't wait for response. Returns 0 on success, negative
3708 int idpf_set_promiscuous(struct idpf_adapter *adapter,
3709 struct idpf_vport_user_config_data *config_data,
3712 struct idpf_vc_xn_params xn_params = {};
3713 struct virtchnl2_promisc_info vpi;
3717 if (test_bit(__IDPF_PROMISC_UC, config_data->user_flags))
3718 flags |= VIRTCHNL2_UNICAST_PROMISC;
3719 if (test_bit(__IDPF_PROMISC_MC, config_data->user_flags))
3720 flags |= VIRTCHNL2_MULTICAST_PROMISC;
3722 vpi.vport_id = cpu_to_le32(vport_id);
3723 vpi.flags = cpu_to_le16(flags);
3725 xn_params.vc_op = VIRTCHNL2_OP_CONFIG_PROMISCUOUS_MODE;
3726 xn_params.timeout_ms = IDPF_VC_XN_DEFAULT_TIMEOUT_MSEC;
3727 xn_params.send_buf.iov_base = &vpi;
3728 xn_params.send_buf.iov_len = sizeof(vpi);
3729 /* setting promiscuous is only ever done asynchronously */
3730 xn_params.async = true;
3731 reply_sz = idpf_vc_xn_exec(adapter, &xn_params);
3733 return reply_sz < 0 ? reply_sz : 0;