File Index
Files and directories
Sign in
FILE

idpf_virtchnl.c

File preview with syntax highlighting.

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