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