1 // SPDX-License-Identifier: GPL-2.0-only 2 /* Copyright (C) 2023 Intel Corporation */ 3 4 #include "idpf.h" 5 #include "idpf_virtchnl.h" 6 #include "idpf_ptp.h" 7 8 static const struct net_device_ops idpf_netdev_ops; 9 10 /** 11 * idpf_init_vector_stack - Fill the MSIX vector stack with vector index 12 * @adapter: private data struct 13 * 14 * Return 0 on success, error on failure 15 */ 16 static int idpf_init_vector_stack(struct idpf_adapter *adapter) 17 { 18 struct idpf_vector_lifo *stack; 19 u16 min_vec; 20 u32 i; 21 22 mutex_lock(&adapter->vector_lock); 23 min_vec = adapter->num_msix_entries - adapter->num_avail_msix; 24 stack = &adapter->vector_stack; 25 stack->size = adapter->num_msix_entries; 26 /* set the base and top to point at start of the 'free pool' to 27 * distribute the unused vectors on-demand basis 28 */ 29 stack->base = min_vec; 30 stack->top = min_vec; 31 32 stack->vec_idx = kcalloc(stack->size, sizeof(u16), GFP_KERNEL); 33 if (!stack->vec_idx) { 34 mutex_unlock(&adapter->vector_lock); 35 36 return -ENOMEM; 37 } 38 39 for (i = 0; i < stack->size; i++) 40 stack->vec_idx[i] = i; 41 42 mutex_unlock(&adapter->vector_lock); 43 44 return 0; 45 } 46 47 /** 48 * idpf_deinit_vector_stack - zero out the MSIX vector stack 49 * @adapter: private data struct 50 */ 51 static void idpf_deinit_vector_stack(struct idpf_adapter *adapter) 52 { 53 struct idpf_vector_lifo *stack; 54 55 mutex_lock(&adapter->vector_lock); 56 stack = &adapter->vector_stack; 57 kfree(stack->vec_idx); 58 stack->vec_idx = NULL; 59 mutex_unlock(&adapter->vector_lock); 60 } 61 62 /** 63 * idpf_mb_intr_rel_irq - Free the IRQ association with the OS 64 * @adapter: adapter structure 65 * 66 * This will also disable interrupt mode and queue up mailbox task. Mailbox 67 * task will reschedule itself if not in interrupt mode. 68 */ 69 static void idpf_mb_intr_rel_irq(struct idpf_adapter *adapter) 70 { 71 clear_bit(IDPF_MB_INTR_MODE, adapter->flags); 72 kfree(free_irq(adapter->msix_entries[0].vector, adapter)); 73 queue_delayed_work(adapter->mbx_wq, &adapter->mbx_task, 0); 74 } 75 76 /** 77 * idpf_intr_rel - Release interrupt capabilities and free memory 78 * @adapter: adapter to disable interrupts on 79 */ 80 void idpf_intr_rel(struct idpf_adapter *adapter) 81 { 82 if (!adapter->msix_entries) 83 return; 84 85 idpf_mb_intr_rel_irq(adapter); 86 pci_free_irq_vectors(adapter->pdev); 87 idpf_send_dealloc_vectors_msg(adapter); 88 idpf_deinit_vector_stack(adapter); 89 kfree(adapter->msix_entries); 90 adapter->msix_entries = NULL; 91 kfree(adapter->rdma_msix_entries); 92 adapter->rdma_msix_entries = NULL; 93 } 94 95 /** 96 * idpf_mb_intr_clean - Interrupt handler for the mailbox 97 * @irq: interrupt number 98 * @data: pointer to the adapter structure 99 */ 100 static irqreturn_t idpf_mb_intr_clean(int __always_unused irq, void *data) 101 { 102 struct idpf_adapter *adapter = (struct idpf_adapter *)data; 103 104 queue_delayed_work(adapter->mbx_wq, &adapter->mbx_task, 0); 105 106 return IRQ_HANDLED; 107 } 108 109 /** 110 * idpf_mb_irq_enable - Enable MSIX interrupt for the mailbox 111 * @adapter: adapter to get the hardware address for register write 112 */ 113 static void idpf_mb_irq_enable(struct idpf_adapter *adapter) 114 { 115 struct idpf_intr_reg *intr = &adapter->mb_vector.intr_reg; 116 u32 val; 117 118 val = intr->dyn_ctl_intena_m | intr->dyn_ctl_itridx_m; 119 writel(val, intr->dyn_ctl); 120 writel(intr->icr_ena_ctlq_m, intr->icr_ena); 121 } 122 123 /** 124 * idpf_mb_intr_req_irq - Request irq for the mailbox interrupt 125 * @adapter: adapter structure to pass to the mailbox irq handler 126 */ 127 static int idpf_mb_intr_req_irq(struct idpf_adapter *adapter) 128 { 129 int irq_num, mb_vidx = 0, err; 130 char *name; 131 132 irq_num = adapter->msix_entries[mb_vidx].vector; 133 name = kasprintf(GFP_KERNEL, "%s-%s-%d", 134 dev_driver_string(&adapter->pdev->dev), 135 "Mailbox", mb_vidx); 136 err = request_irq(irq_num, adapter->irq_mb_handler, 0, name, adapter); 137 if (err) { 138 dev_err(&adapter->pdev->dev, 139 "IRQ request for mailbox failed, error: %d\n", err); 140 141 return err; 142 } 143 144 set_bit(IDPF_MB_INTR_MODE, adapter->flags); 145 146 return 0; 147 } 148 149 /** 150 * idpf_mb_intr_init - Initialize the mailbox interrupt 151 * @adapter: adapter structure to store the mailbox vector 152 */ 153 static int idpf_mb_intr_init(struct idpf_adapter *adapter) 154 { 155 adapter->dev_ops.reg_ops.mb_intr_reg_init(adapter); 156 adapter->irq_mb_handler = idpf_mb_intr_clean; 157 158 return idpf_mb_intr_req_irq(adapter); 159 } 160 161 /** 162 * idpf_vector_lifo_push - push MSIX vector index onto stack 163 * @adapter: private data struct 164 * @vec_idx: vector index to store 165 */ 166 static int idpf_vector_lifo_push(struct idpf_adapter *adapter, u16 vec_idx) 167 { 168 struct idpf_vector_lifo *stack = &adapter->vector_stack; 169 170 lockdep_assert_held(&adapter->vector_lock); 171 172 if (stack->top == stack->base) { 173 dev_err(&adapter->pdev->dev, "Exceeded the vector stack limit: %d\n", 174 stack->top); 175 return -EINVAL; 176 } 177 178 stack->vec_idx[--stack->top] = vec_idx; 179 180 return 0; 181 } 182 183 /** 184 * idpf_vector_lifo_pop - pop MSIX vector index from stack 185 * @adapter: private data struct 186 */ 187 static int idpf_vector_lifo_pop(struct idpf_adapter *adapter) 188 { 189 struct idpf_vector_lifo *stack = &adapter->vector_stack; 190 191 lockdep_assert_held(&adapter->vector_lock); 192 193 if (stack->top == stack->size) { 194 dev_err(&adapter->pdev->dev, "No interrupt vectors are available to distribute!\n"); 195 196 return -EINVAL; 197 } 198 199 return stack->vec_idx[stack->top++]; 200 } 201 202 /** 203 * idpf_vector_stash - Store the vector indexes onto the stack 204 * @adapter: private data struct 205 * @q_vector_idxs: vector index array 206 * @vec_info: info related to the number of vectors 207 * 208 * This function is a no-op if there are no vectors indexes to be stashed 209 */ 210 static void idpf_vector_stash(struct idpf_adapter *adapter, u16 *q_vector_idxs, 211 struct idpf_vector_info *vec_info) 212 { 213 int i, base = 0; 214 u16 vec_idx; 215 216 lockdep_assert_held(&adapter->vector_lock); 217 218 if (!vec_info->num_curr_vecs) 219 return; 220 221 /* For default vports, no need to stash vector allocated from the 222 * default pool onto the stack 223 */ 224 if (vec_info->default_vport) 225 base = IDPF_MIN_Q_VEC; 226 227 for (i = vec_info->num_curr_vecs - 1; i >= base ; i--) { 228 vec_idx = q_vector_idxs[i]; 229 idpf_vector_lifo_push(adapter, vec_idx); 230 adapter->num_avail_msix++; 231 } 232 } 233 234 /** 235 * idpf_req_rel_vector_indexes - Request or release MSIX vector indexes 236 * @adapter: driver specific private structure 237 * @q_vector_idxs: vector index array 238 * @vec_info: info related to the number of vectors 239 * 240 * This is the core function to distribute the MSIX vectors acquired from the 241 * OS. It expects the caller to pass the number of vectors required and 242 * also previously allocated. First, it stashes previously allocated vector 243 * indexes on to the stack and then figures out if it can allocate requested 244 * vectors. It can wait on acquiring the mutex lock. If the caller passes 0 as 245 * requested vectors, then this function just stashes the already allocated 246 * vectors and returns 0. 247 * 248 * Returns actual number of vectors allocated on success, error value on failure 249 * If 0 is returned, implies the stack has no vectors to allocate which is also 250 * a failure case for the caller 251 */ 252 int idpf_req_rel_vector_indexes(struct idpf_adapter *adapter, 253 u16 *q_vector_idxs, 254 struct idpf_vector_info *vec_info) 255 { 256 u16 num_req_vecs, num_alloc_vecs = 0, max_vecs; 257 struct idpf_vector_lifo *stack; 258 int i, j, vecid; 259 260 mutex_lock(&adapter->vector_lock); 261 stack = &adapter->vector_stack; 262 num_req_vecs = vec_info->num_req_vecs; 263 264 /* Stash interrupt vector indexes onto the stack if required */ 265 idpf_vector_stash(adapter, q_vector_idxs, vec_info); 266 267 if (!num_req_vecs) 268 goto rel_lock; 269 270 if (vec_info->default_vport) { 271 /* As IDPF_MIN_Q_VEC per default vport is put aside in the 272 * default pool of the stack, use them for default vports 273 */ 274 j = vec_info->index * IDPF_MIN_Q_VEC + IDPF_MBX_Q_VEC; 275 for (i = 0; i < IDPF_MIN_Q_VEC; i++) { 276 q_vector_idxs[num_alloc_vecs++] = stack->vec_idx[j++]; 277 num_req_vecs--; 278 } 279 } 280 281 /* Find if stack has enough vector to allocate */ 282 max_vecs = min(adapter->num_avail_msix, num_req_vecs); 283 284 for (j = 0; j < max_vecs; j++) { 285 vecid = idpf_vector_lifo_pop(adapter); 286 q_vector_idxs[num_alloc_vecs++] = vecid; 287 } 288 adapter->num_avail_msix -= max_vecs; 289 290 rel_lock: 291 mutex_unlock(&adapter->vector_lock); 292 293 return num_alloc_vecs; 294 } 295 296 /** 297 * idpf_intr_req - Request interrupt capabilities 298 * @adapter: adapter to enable interrupts on 299 * 300 * Returns 0 on success, negative on failure 301 */ 302 int idpf_intr_req(struct idpf_adapter *adapter) 303 { 304 u16 num_lan_vecs, min_lan_vecs, num_rdma_vecs = 0, min_rdma_vecs = 0; 305 u16 default_vports = idpf_get_default_vports(adapter); 306 int num_q_vecs, total_vecs, num_vec_ids; 307 int min_vectors, actual_vecs, err; 308 unsigned int vector; 309 u16 *vecids; 310 int i; 311 312 total_vecs = idpf_get_reserved_vecs(adapter); 313 num_lan_vecs = total_vecs; 314 if (idpf_is_rdma_cap_ena(adapter)) { 315 num_rdma_vecs = idpf_get_reserved_rdma_vecs(adapter); 316 min_rdma_vecs = IDPF_MIN_RDMA_VEC; 317 318 if (!num_rdma_vecs) { 319 /* If idpf_get_reserved_rdma_vecs is 0, vectors are 320 * pulled from the LAN pool. 321 */ 322 num_rdma_vecs = min_rdma_vecs; 323 } else if (num_rdma_vecs < min_rdma_vecs) { 324 dev_err(&adapter->pdev->dev, 325 "Not enough vectors reserved for RDMA (min: %u, current: %u)\n", 326 min_rdma_vecs, num_rdma_vecs); 327 return -EINVAL; 328 } 329 } 330 331 num_q_vecs = total_vecs - IDPF_MBX_Q_VEC; 332 333 err = idpf_send_alloc_vectors_msg(adapter, num_q_vecs); 334 if (err) { 335 dev_err(&adapter->pdev->dev, 336 "Failed to allocate %d vectors: %d\n", num_q_vecs, err); 337 338 return -EAGAIN; 339 } 340 341 min_lan_vecs = IDPF_MBX_Q_VEC + IDPF_MIN_Q_VEC * default_vports; 342 min_vectors = min_lan_vecs + min_rdma_vecs; 343 actual_vecs = pci_alloc_irq_vectors(adapter->pdev, min_vectors, 344 total_vecs, PCI_IRQ_MSIX); 345 if (actual_vecs < 0) { 346 dev_err(&adapter->pdev->dev, "Failed to allocate minimum MSIX vectors required: %d\n", 347 min_vectors); 348 err = actual_vecs; 349 goto send_dealloc_vecs; 350 } 351 352 if (idpf_is_rdma_cap_ena(adapter)) { 353 if (actual_vecs < total_vecs) { 354 dev_warn(&adapter->pdev->dev, 355 "Warning: %d vectors requested, only %d available. Defaulting to minimum (%d) for RDMA and remaining for LAN.\n", 356 total_vecs, actual_vecs, IDPF_MIN_RDMA_VEC); 357 num_rdma_vecs = IDPF_MIN_RDMA_VEC; 358 } 359 360 adapter->rdma_msix_entries = kcalloc(num_rdma_vecs, 361 sizeof(struct msix_entry), 362 GFP_KERNEL); 363 if (!adapter->rdma_msix_entries) { 364 err = -ENOMEM; 365 goto free_irq; 366 } 367 } 368 369 num_lan_vecs = actual_vecs - num_rdma_vecs; 370 adapter->msix_entries = kcalloc(num_lan_vecs, sizeof(struct msix_entry), 371 GFP_KERNEL); 372 if (!adapter->msix_entries) { 373 err = -ENOMEM; 374 goto free_rdma_msix; 375 } 376 377 adapter->mb_vector.v_idx = le16_to_cpu(adapter->caps.mailbox_vector_id); 378 379 vecids = kcalloc(actual_vecs, sizeof(u16), GFP_KERNEL); 380 if (!vecids) { 381 err = -ENOMEM; 382 goto free_msix; 383 } 384 385 num_vec_ids = idpf_get_vec_ids(adapter, vecids, actual_vecs, 386 &adapter->req_vec_chunks->vchunks); 387 if (num_vec_ids < actual_vecs) { 388 err = -EINVAL; 389 goto free_vecids; 390 } 391 392 for (vector = 0; vector < num_lan_vecs; vector++) { 393 adapter->msix_entries[vector].entry = vecids[vector]; 394 adapter->msix_entries[vector].vector = 395 pci_irq_vector(adapter->pdev, vector); 396 } 397 for (i = 0; i < num_rdma_vecs; vector++, i++) { 398 adapter->rdma_msix_entries[i].entry = vecids[vector]; 399 adapter->rdma_msix_entries[i].vector = 400 pci_irq_vector(adapter->pdev, vector); 401 } 402 403 /* 'num_avail_msix' is used to distribute excess vectors to the vports 404 * after considering the minimum vectors required per each default 405 * vport 406 */ 407 adapter->num_avail_msix = num_lan_vecs - min_lan_vecs; 408 adapter->num_msix_entries = num_lan_vecs; 409 if (idpf_is_rdma_cap_ena(adapter)) 410 adapter->num_rdma_msix_entries = num_rdma_vecs; 411 412 /* Fill MSIX vector lifo stack with vector indexes */ 413 err = idpf_init_vector_stack(adapter); 414 if (err) 415 goto free_vecids; 416 417 err = idpf_mb_intr_init(adapter); 418 if (err) 419 goto deinit_vec_stack; 420 idpf_mb_irq_enable(adapter); 421 kfree(vecids); 422 423 return 0; 424 425 deinit_vec_stack: 426 idpf_deinit_vector_stack(adapter); 427 free_vecids: 428 kfree(vecids); 429 free_msix: 430 kfree(adapter->msix_entries); 431 adapter->msix_entries = NULL; 432 free_rdma_msix: 433 kfree(adapter->rdma_msix_entries); 434 adapter->rdma_msix_entries = NULL; 435 free_irq: 436 pci_free_irq_vectors(adapter->pdev); 437 send_dealloc_vecs: 438 idpf_send_dealloc_vectors_msg(adapter); 439 440 return err; 441 } 442 443 /** 444 * idpf_find_mac_filter - Search filter list for specific mac filter 445 * @vconfig: Vport config structure 446 * @macaddr: The MAC address 447 * 448 * Returns ptr to the filter object or NULL. Must be called while holding the 449 * mac_filter_list_lock. 450 **/ 451 static struct idpf_mac_filter *idpf_find_mac_filter(struct idpf_vport_config *vconfig, 452 const u8 *macaddr) 453 { 454 struct idpf_mac_filter *f; 455 456 if (!macaddr) 457 return NULL; 458 459 list_for_each_entry(f, &vconfig->user_config.mac_filter_list, list) { 460 if (ether_addr_equal(macaddr, f->macaddr)) 461 return f; 462 } 463 464 return NULL; 465 } 466 467 /** 468 * __idpf_del_mac_filter - Delete a MAC filter from the filter list 469 * @vport_config: Vport config structure 470 * @macaddr: The MAC address 471 * 472 * Returns 0 on success, error value on failure 473 **/ 474 static int __idpf_del_mac_filter(struct idpf_vport_config *vport_config, 475 const u8 *macaddr) 476 { 477 struct idpf_mac_filter *f; 478 479 spin_lock_bh(&vport_config->mac_filter_list_lock); 480 f = idpf_find_mac_filter(vport_config, macaddr); 481 if (f) { 482 list_del(&f->list); 483 kfree(f); 484 } 485 spin_unlock_bh(&vport_config->mac_filter_list_lock); 486 487 return 0; 488 } 489 490 /** 491 * idpf_del_mac_filter - Delete a MAC filter from the filter list 492 * @vport: Main vport structure 493 * @np: Netdev private structure 494 * @macaddr: The MAC address 495 * @async: Don't wait for return message 496 * 497 * Removes filter from list and if interface is up, tells hardware about the 498 * removed filter. 499 **/ 500 static int idpf_del_mac_filter(struct idpf_vport *vport, 501 struct idpf_netdev_priv *np, 502 const u8 *macaddr, bool async) 503 { 504 struct idpf_vport_config *vport_config; 505 struct idpf_mac_filter *f; 506 507 vport_config = np->adapter->vport_config[np->vport_idx]; 508 509 spin_lock_bh(&vport_config->mac_filter_list_lock); 510 f = idpf_find_mac_filter(vport_config, macaddr); 511 if (f) { 512 f->remove = true; 513 } else { 514 spin_unlock_bh(&vport_config->mac_filter_list_lock); 515 516 return -EINVAL; 517 } 518 spin_unlock_bh(&vport_config->mac_filter_list_lock); 519 520 if (np->state == __IDPF_VPORT_UP) { 521 int err; 522 523 err = idpf_add_del_mac_filters(vport, np, false, async); 524 if (err) 525 return err; 526 } 527 528 return __idpf_del_mac_filter(vport_config, macaddr); 529 } 530 531 /** 532 * __idpf_add_mac_filter - Add mac filter helper function 533 * @vport_config: Vport config structure 534 * @macaddr: Address to add 535 * 536 * Takes mac_filter_list_lock spinlock to add new filter to list. 537 */ 538 static int __idpf_add_mac_filter(struct idpf_vport_config *vport_config, 539 const u8 *macaddr) 540 { 541 struct idpf_mac_filter *f; 542 543 spin_lock_bh(&vport_config->mac_filter_list_lock); 544 545 f = idpf_find_mac_filter(vport_config, macaddr); 546 if (f) { 547 f->remove = false; 548 spin_unlock_bh(&vport_config->mac_filter_list_lock); 549 550 return 0; 551 } 552 553 f = kzalloc(sizeof(*f), GFP_ATOMIC); 554 if (!f) { 555 spin_unlock_bh(&vport_config->mac_filter_list_lock); 556 557 return -ENOMEM; 558 } 559 560 ether_addr_copy(f->macaddr, macaddr); 561 list_add_tail(&f->list, &vport_config->user_config.mac_filter_list); 562 f->add = true; 563 564 spin_unlock_bh(&vport_config->mac_filter_list_lock); 565 566 return 0; 567 } 568 569 /** 570 * idpf_add_mac_filter - Add a mac filter to the filter list 571 * @vport: Main vport structure 572 * @np: Netdev private structure 573 * @macaddr: The MAC address 574 * @async: Don't wait for return message 575 * 576 * Returns 0 on success or error on failure. If interface is up, we'll also 577 * send the virtchnl message to tell hardware about the filter. 578 **/ 579 static int idpf_add_mac_filter(struct idpf_vport *vport, 580 struct idpf_netdev_priv *np, 581 const u8 *macaddr, bool async) 582 { 583 struct idpf_vport_config *vport_config; 584 int err; 585 586 vport_config = np->adapter->vport_config[np->vport_idx]; 587 err = __idpf_add_mac_filter(vport_config, macaddr); 588 if (err) 589 return err; 590 591 if (np->state == __IDPF_VPORT_UP) 592 err = idpf_add_del_mac_filters(vport, np, true, async); 593 594 return err; 595 } 596 597 /** 598 * idpf_del_all_mac_filters - Delete all MAC filters in list 599 * @vport: main vport struct 600 * 601 * Takes mac_filter_list_lock spinlock. Deletes all filters 602 */ 603 static void idpf_del_all_mac_filters(struct idpf_vport *vport) 604 { 605 struct idpf_vport_config *vport_config; 606 struct idpf_mac_filter *f, *ftmp; 607 608 vport_config = vport->adapter->vport_config[vport->idx]; 609 spin_lock_bh(&vport_config->mac_filter_list_lock); 610 611 list_for_each_entry_safe(f, ftmp, &vport_config->user_config.mac_filter_list, 612 list) { 613 list_del(&f->list); 614 kfree(f); 615 } 616 617 spin_unlock_bh(&vport_config->mac_filter_list_lock); 618 } 619 620 /** 621 * idpf_restore_mac_filters - Re-add all MAC filters in list 622 * @vport: main vport struct 623 * 624 * Takes mac_filter_list_lock spinlock. Sets add field to true for filters to 625 * resync filters back to HW. 626 */ 627 static void idpf_restore_mac_filters(struct idpf_vport *vport) 628 { 629 struct idpf_vport_config *vport_config; 630 struct idpf_mac_filter *f; 631 632 vport_config = vport->adapter->vport_config[vport->idx]; 633 spin_lock_bh(&vport_config->mac_filter_list_lock); 634 635 list_for_each_entry(f, &vport_config->user_config.mac_filter_list, list) 636 f->add = true; 637 638 spin_unlock_bh(&vport_config->mac_filter_list_lock); 639 640 idpf_add_del_mac_filters(vport, netdev_priv(vport->netdev), 641 true, false); 642 } 643 644 /** 645 * idpf_remove_mac_filters - Remove all MAC filters in list 646 * @vport: main vport struct 647 * 648 * Takes mac_filter_list_lock spinlock. Sets remove field to true for filters 649 * to remove filters in HW. 650 */ 651 static void idpf_remove_mac_filters(struct idpf_vport *vport) 652 { 653 struct idpf_vport_config *vport_config; 654 struct idpf_mac_filter *f; 655 656 vport_config = vport->adapter->vport_config[vport->idx]; 657 spin_lock_bh(&vport_config->mac_filter_list_lock); 658 659 list_for_each_entry(f, &vport_config->user_config.mac_filter_list, list) 660 f->remove = true; 661 662 spin_unlock_bh(&vport_config->mac_filter_list_lock); 663 664 idpf_add_del_mac_filters(vport, netdev_priv(vport->netdev), 665 false, false); 666 } 667 668 /** 669 * idpf_deinit_mac_addr - deinitialize mac address for vport 670 * @vport: main vport structure 671 */ 672 static void idpf_deinit_mac_addr(struct idpf_vport *vport) 673 { 674 struct idpf_vport_config *vport_config; 675 struct idpf_mac_filter *f; 676 677 vport_config = vport->adapter->vport_config[vport->idx]; 678 679 spin_lock_bh(&vport_config->mac_filter_list_lock); 680 681 f = idpf_find_mac_filter(vport_config, vport->default_mac_addr); 682 if (f) { 683 list_del(&f->list); 684 kfree(f); 685 } 686 687 spin_unlock_bh(&vport_config->mac_filter_list_lock); 688 } 689 690 /** 691 * idpf_init_mac_addr - initialize mac address for vport 692 * @vport: main vport structure 693 * @netdev: pointer to netdev struct associated with this vport 694 */ 695 static int idpf_init_mac_addr(struct idpf_vport *vport, 696 struct net_device *netdev) 697 { 698 struct idpf_netdev_priv *np = netdev_priv(netdev); 699 struct idpf_adapter *adapter = vport->adapter; 700 int err; 701 702 if (is_valid_ether_addr(vport->default_mac_addr)) { 703 eth_hw_addr_set(netdev, vport->default_mac_addr); 704 ether_addr_copy(netdev->perm_addr, vport->default_mac_addr); 705 706 return idpf_add_mac_filter(vport, np, vport->default_mac_addr, 707 false); 708 } 709 710 if (!idpf_is_cap_ena(adapter, IDPF_OTHER_CAPS, 711 VIRTCHNL2_CAP_MACFILTER)) { 712 dev_err(&adapter->pdev->dev, 713 "MAC address is not provided and capability is not set\n"); 714 715 return -EINVAL; 716 } 717 718 eth_hw_addr_random(netdev); 719 err = idpf_add_mac_filter(vport, np, netdev->dev_addr, false); 720 if (err) 721 return err; 722 723 dev_info(&adapter->pdev->dev, "Invalid MAC address %pM, using random %pM\n", 724 vport->default_mac_addr, netdev->dev_addr); 725 ether_addr_copy(vport->default_mac_addr, netdev->dev_addr); 726 727 return 0; 728 } 729 730 /** 731 * idpf_cfg_netdev - Allocate, configure and register a netdev 732 * @vport: main vport structure 733 * 734 * Returns 0 on success, negative value on failure. 735 */ 736 static int idpf_cfg_netdev(struct idpf_vport *vport) 737 { 738 struct idpf_adapter *adapter = vport->adapter; 739 struct idpf_vport_config *vport_config; 740 netdev_features_t other_offloads = 0; 741 netdev_features_t csum_offloads = 0; 742 netdev_features_t tso_offloads = 0; 743 netdev_features_t dflt_features; 744 struct idpf_netdev_priv *np; 745 struct net_device *netdev; 746 u16 idx = vport->idx; 747 int err; 748 749 vport_config = adapter->vport_config[idx]; 750 751 /* It's possible we already have a netdev allocated and registered for 752 * this vport 753 */ 754 if (test_bit(IDPF_VPORT_REG_NETDEV, vport_config->flags)) { 755 netdev = adapter->netdevs[idx]; 756 np = netdev_priv(netdev); 757 np->vport = vport; 758 np->vport_idx = vport->idx; 759 np->vport_id = vport->vport_id; 760 np->max_tx_hdr_size = idpf_get_max_tx_hdr_size(adapter); 761 vport->netdev = netdev; 762 763 return idpf_init_mac_addr(vport, netdev); 764 } 765 766 netdev = alloc_etherdev_mqs(sizeof(struct idpf_netdev_priv), 767 vport_config->max_q.max_txq, 768 vport_config->max_q.max_rxq); 769 if (!netdev) 770 return -ENOMEM; 771 772 vport->netdev = netdev; 773 np = netdev_priv(netdev); 774 np->vport = vport; 775 np->adapter = adapter; 776 np->vport_idx = vport->idx; 777 np->vport_id = vport->vport_id; 778 np->max_tx_hdr_size = idpf_get_max_tx_hdr_size(adapter); 779 780 spin_lock_init(&np->stats_lock); 781 782 err = idpf_init_mac_addr(vport, netdev); 783 if (err) { 784 free_netdev(vport->netdev); 785 vport->netdev = NULL; 786 787 return err; 788 } 789 790 /* assign netdev_ops */ 791 netdev->netdev_ops = &idpf_netdev_ops; 792 793 /* setup watchdog timeout value to be 5 second */ 794 netdev->watchdog_timeo = 5 * HZ; 795 796 netdev->dev_port = idx; 797 798 /* configure default MTU size */ 799 netdev->min_mtu = ETH_MIN_MTU; 800 netdev->max_mtu = vport->max_mtu; 801 802 dflt_features = NETIF_F_SG | 803 NETIF_F_HIGHDMA; 804 805 if (idpf_is_cap_ena_all(adapter, IDPF_RSS_CAPS, IDPF_CAP_RSS)) 806 dflt_features |= NETIF_F_RXHASH; 807 if (idpf_is_cap_ena_all(adapter, IDPF_CSUM_CAPS, IDPF_CAP_TX_CSUM_L4V4)) 808 csum_offloads |= NETIF_F_IP_CSUM; 809 if (idpf_is_cap_ena_all(adapter, IDPF_CSUM_CAPS, IDPF_CAP_TX_CSUM_L4V6)) 810 csum_offloads |= NETIF_F_IPV6_CSUM; 811 if (idpf_is_cap_ena(adapter, IDPF_CSUM_CAPS, IDPF_CAP_RX_CSUM)) 812 csum_offloads |= NETIF_F_RXCSUM; 813 if (idpf_is_cap_ena_all(adapter, IDPF_CSUM_CAPS, IDPF_CAP_TX_SCTP_CSUM)) 814 csum_offloads |= NETIF_F_SCTP_CRC; 815 816 if (idpf_is_cap_ena(adapter, IDPF_SEG_CAPS, VIRTCHNL2_CAP_SEG_IPV4_TCP)) 817 tso_offloads |= NETIF_F_TSO; 818 if (idpf_is_cap_ena(adapter, IDPF_SEG_CAPS, VIRTCHNL2_CAP_SEG_IPV6_TCP)) 819 tso_offloads |= NETIF_F_TSO6; 820 if (idpf_is_cap_ena_all(adapter, IDPF_SEG_CAPS, 821 VIRTCHNL2_CAP_SEG_IPV4_UDP | 822 VIRTCHNL2_CAP_SEG_IPV6_UDP)) 823 tso_offloads |= NETIF_F_GSO_UDP_L4; 824 if (idpf_is_cap_ena_all(adapter, IDPF_RSC_CAPS, IDPF_CAP_RSC)) 825 other_offloads |= NETIF_F_GRO_HW; 826 if (idpf_is_cap_ena(adapter, IDPF_OTHER_CAPS, VIRTCHNL2_CAP_LOOPBACK)) 827 other_offloads |= NETIF_F_LOOPBACK; 828 829 netdev->features |= dflt_features | csum_offloads | tso_offloads; 830 netdev->hw_features |= netdev->features | other_offloads; 831 netdev->vlan_features |= netdev->features | other_offloads; 832 netdev->hw_enc_features |= dflt_features | other_offloads; 833 idpf_set_ethtool_ops(netdev); 834 netif_set_affinity_auto(netdev); 835 SET_NETDEV_DEV(netdev, &adapter->pdev->dev); 836 837 /* carrier off on init to avoid Tx hangs */ 838 netif_carrier_off(netdev); 839 840 /* make sure transmit queues start off as stopped */ 841 netif_tx_stop_all_queues(netdev); 842 843 /* The vport can be arbitrarily released so we need to also track 844 * netdevs in the adapter struct 845 */ 846 adapter->netdevs[idx] = netdev; 847 848 return 0; 849 } 850 851 /** 852 * idpf_get_free_slot - get the next non-NULL location index in array 853 * @adapter: adapter in which to look for a free vport slot 854 */ 855 static int idpf_get_free_slot(struct idpf_adapter *adapter) 856 { 857 unsigned int i; 858 859 for (i = 0; i < adapter->max_vports; i++) { 860 if (!adapter->vports[i]) 861 return i; 862 } 863 864 return IDPF_NO_FREE_SLOT; 865 } 866 867 /** 868 * idpf_remove_features - Turn off feature configs 869 * @vport: virtual port structure 870 */ 871 static void idpf_remove_features(struct idpf_vport *vport) 872 { 873 struct idpf_adapter *adapter = vport->adapter; 874 875 if (idpf_is_cap_ena(adapter, IDPF_OTHER_CAPS, VIRTCHNL2_CAP_MACFILTER)) 876 idpf_remove_mac_filters(vport); 877 } 878 879 /** 880 * idpf_vport_stop - Disable a vport 881 * @vport: vport to disable 882 */ 883 static void idpf_vport_stop(struct idpf_vport *vport) 884 { 885 struct idpf_netdev_priv *np = netdev_priv(vport->netdev); 886 887 if (np->state <= __IDPF_VPORT_DOWN) 888 return; 889 890 netif_carrier_off(vport->netdev); 891 netif_tx_disable(vport->netdev); 892 893 idpf_send_disable_vport_msg(vport); 894 idpf_send_disable_queues_msg(vport); 895 idpf_send_map_unmap_queue_vector_msg(vport, false); 896 /* Normally we ask for queues in create_vport, but if the number of 897 * initially requested queues have changed, for example via ethtool 898 * set channels, we do delete queues and then add the queues back 899 * instead of deleting and reallocating the vport. 900 */ 901 if (test_and_clear_bit(IDPF_VPORT_DEL_QUEUES, vport->flags)) 902 idpf_send_delete_queues_msg(vport); 903 904 idpf_remove_features(vport); 905 906 vport->link_up = false; 907 idpf_vport_intr_deinit(vport); 908 idpf_vport_queues_rel(vport); 909 idpf_vport_intr_rel(vport); 910 np->state = __IDPF_VPORT_DOWN; 911 } 912 913 /** 914 * idpf_stop - Disables a network interface 915 * @netdev: network interface device structure 916 * 917 * The stop entry point is called when an interface is de-activated by the OS, 918 * and the netdevice enters the DOWN state. The hardware is still under the 919 * driver's control, but the netdev interface is disabled. 920 * 921 * Returns success only - not allowed to fail 922 */ 923 static int idpf_stop(struct net_device *netdev) 924 { 925 struct idpf_netdev_priv *np = netdev_priv(netdev); 926 struct idpf_vport *vport; 927 928 if (test_bit(IDPF_REMOVE_IN_PROG, np->adapter->flags)) 929 return 0; 930 931 idpf_vport_ctrl_lock(netdev); 932 vport = idpf_netdev_to_vport(netdev); 933 934 idpf_vport_stop(vport); 935 936 idpf_vport_ctrl_unlock(netdev); 937 938 return 0; 939 } 940 941 /** 942 * idpf_decfg_netdev - Unregister the netdev 943 * @vport: vport for which netdev to be unregistered 944 */ 945 static void idpf_decfg_netdev(struct idpf_vport *vport) 946 { 947 struct idpf_adapter *adapter = vport->adapter; 948 u16 idx = vport->idx; 949 950 kfree(vport->rx_ptype_lkup); 951 vport->rx_ptype_lkup = NULL; 952 953 if (test_and_clear_bit(IDPF_VPORT_REG_NETDEV, 954 adapter->vport_config[idx]->flags)) { 955 unregister_netdev(vport->netdev); 956 free_netdev(vport->netdev); 957 } 958 vport->netdev = NULL; 959 960 adapter->netdevs[idx] = NULL; 961 } 962 963 /** 964 * idpf_vport_rel - Delete a vport and free its resources 965 * @vport: the vport being removed 966 */ 967 static void idpf_vport_rel(struct idpf_vport *vport) 968 { 969 struct idpf_adapter *adapter = vport->adapter; 970 struct idpf_vport_config *vport_config; 971 struct idpf_vector_info vec_info; 972 struct idpf_rss_data *rss_data; 973 struct idpf_vport_max_q max_q; 974 u16 idx = vport->idx; 975 976 vport_config = adapter->vport_config[vport->idx]; 977 idpf_deinit_rss(vport); 978 rss_data = &vport_config->user_config.rss_data; 979 kfree(rss_data->rss_key); 980 rss_data->rss_key = NULL; 981 982 idpf_send_destroy_vport_msg(vport); 983 984 /* Release all max queues allocated to the adapter's pool */ 985 max_q.max_rxq = vport_config->max_q.max_rxq; 986 max_q.max_txq = vport_config->max_q.max_txq; 987 max_q.max_bufq = vport_config->max_q.max_bufq; 988 max_q.max_complq = vport_config->max_q.max_complq; 989 idpf_vport_dealloc_max_qs(adapter, &max_q); 990 991 /* Release all the allocated vectors on the stack */ 992 vec_info.num_req_vecs = 0; 993 vec_info.num_curr_vecs = vport->num_q_vectors; 994 vec_info.default_vport = vport->default_vport; 995 996 idpf_req_rel_vector_indexes(adapter, vport->q_vector_idxs, &vec_info); 997 998 kfree(vport->q_vector_idxs); 999 vport->q_vector_idxs = NULL; 1000 1001 kfree(adapter->vport_params_recvd[idx]); 1002 adapter->vport_params_recvd[idx] = NULL; 1003 kfree(adapter->vport_params_reqd[idx]); 1004 adapter->vport_params_reqd[idx] = NULL; 1005 if (adapter->vport_config[idx]) { 1006 kfree(adapter->vport_config[idx]->req_qs_chunks); 1007 adapter->vport_config[idx]->req_qs_chunks = NULL; 1008 } 1009 kfree(vport); 1010 adapter->num_alloc_vports--; 1011 } 1012 1013 /** 1014 * idpf_vport_dealloc - cleanup and release a given vport 1015 * @vport: pointer to idpf vport structure 1016 * 1017 * returns nothing 1018 */ 1019 static void idpf_vport_dealloc(struct idpf_vport *vport) 1020 { 1021 struct idpf_adapter *adapter = vport->adapter; 1022 unsigned int i = vport->idx; 1023 1024 idpf_idc_deinit_vport_aux_device(vport->vdev_info); 1025 1026 idpf_deinit_mac_addr(vport); 1027 idpf_vport_stop(vport); 1028 1029 if (!test_bit(IDPF_HR_RESET_IN_PROG, adapter->flags)) 1030 idpf_decfg_netdev(vport); 1031 if (test_bit(IDPF_REMOVE_IN_PROG, adapter->flags)) 1032 idpf_del_all_mac_filters(vport); 1033 1034 if (adapter->netdevs[i]) { 1035 struct idpf_netdev_priv *np = netdev_priv(adapter->netdevs[i]); 1036 1037 np->vport = NULL; 1038 } 1039 1040 idpf_vport_rel(vport); 1041 1042 adapter->vports[i] = NULL; 1043 adapter->next_vport = idpf_get_free_slot(adapter); 1044 } 1045 1046 /** 1047 * idpf_is_hsplit_supported - check whether the header split is supported 1048 * @vport: virtual port to check the capability for 1049 * 1050 * Return: true if it's supported by the HW/FW, false if not. 1051 */ 1052 static bool idpf_is_hsplit_supported(const struct idpf_vport *vport) 1053 { 1054 return idpf_is_queue_model_split(vport->rxq_model) && 1055 idpf_is_cap_ena_all(vport->adapter, IDPF_HSPLIT_CAPS, 1056 IDPF_CAP_HSPLIT); 1057 } 1058 1059 /** 1060 * idpf_vport_get_hsplit - get the current header split feature state 1061 * @vport: virtual port to query the state for 1062 * 1063 * Return: ``ETHTOOL_TCP_DATA_SPLIT_UNKNOWN`` if not supported, 1064 * ``ETHTOOL_TCP_DATA_SPLIT_DISABLED`` if disabled, 1065 * ``ETHTOOL_TCP_DATA_SPLIT_ENABLED`` if active. 1066 */ 1067 u8 idpf_vport_get_hsplit(const struct idpf_vport *vport) 1068 { 1069 const struct idpf_vport_user_config_data *config; 1070 1071 if (!idpf_is_hsplit_supported(vport)) 1072 return ETHTOOL_TCP_DATA_SPLIT_UNKNOWN; 1073 1074 config = &vport->adapter->vport_config[vport->idx]->user_config; 1075 1076 return test_bit(__IDPF_USER_FLAG_HSPLIT, config->user_flags) ? 1077 ETHTOOL_TCP_DATA_SPLIT_ENABLED : 1078 ETHTOOL_TCP_DATA_SPLIT_DISABLED; 1079 } 1080 1081 /** 1082 * idpf_vport_set_hsplit - enable or disable header split on a given vport 1083 * @vport: virtual port to configure 1084 * @val: Ethtool flag controlling the header split state 1085 * 1086 * Return: true on success, false if not supported by the HW. 1087 */ 1088 bool idpf_vport_set_hsplit(const struct idpf_vport *vport, u8 val) 1089 { 1090 struct idpf_vport_user_config_data *config; 1091 1092 if (!idpf_is_hsplit_supported(vport)) 1093 return val == ETHTOOL_TCP_DATA_SPLIT_UNKNOWN; 1094 1095 config = &vport->adapter->vport_config[vport->idx]->user_config; 1096 1097 switch (val) { 1098 case ETHTOOL_TCP_DATA_SPLIT_UNKNOWN: 1099 /* Default is to enable */ 1100 case ETHTOOL_TCP_DATA_SPLIT_ENABLED: 1101 __set_bit(__IDPF_USER_FLAG_HSPLIT, config->user_flags); 1102 return true; 1103 case ETHTOOL_TCP_DATA_SPLIT_DISABLED: 1104 __clear_bit(__IDPF_USER_FLAG_HSPLIT, config->user_flags); 1105 return true; 1106 default: 1107 return false; 1108 } 1109 } 1110 1111 /** 1112 * idpf_vport_alloc - Allocates the next available struct vport in the adapter 1113 * @adapter: board private structure 1114 * @max_q: vport max queue info 1115 * 1116 * returns a pointer to a vport on success, NULL on failure. 1117 */ 1118 static struct idpf_vport *idpf_vport_alloc(struct idpf_adapter *adapter, 1119 struct idpf_vport_max_q *max_q) 1120 { 1121 struct idpf_rss_data *rss_data; 1122 u16 idx = adapter->next_vport; 1123 struct idpf_vport *vport; 1124 u16 num_max_q; 1125 1126 if (idx == IDPF_NO_FREE_SLOT) 1127 return NULL; 1128 1129 vport = kzalloc(sizeof(*vport), GFP_KERNEL); 1130 if (!vport) 1131 return vport; 1132 1133 if (!adapter->vport_config[idx]) { 1134 struct idpf_vport_config *vport_config; 1135 1136 vport_config = kzalloc(sizeof(*vport_config), GFP_KERNEL); 1137 if (!vport_config) { 1138 kfree(vport); 1139 1140 return NULL; 1141 } 1142 1143 adapter->vport_config[idx] = vport_config; 1144 } 1145 1146 vport->idx = idx; 1147 vport->adapter = adapter; 1148 vport->compln_clean_budget = IDPF_TX_COMPLQ_CLEAN_BUDGET; 1149 vport->default_vport = adapter->num_alloc_vports < 1150 idpf_get_default_vports(adapter); 1151 1152 num_max_q = max(max_q->max_txq, max_q->max_rxq); 1153 vport->q_vector_idxs = kcalloc(num_max_q, sizeof(u16), GFP_KERNEL); 1154 if (!vport->q_vector_idxs) 1155 goto free_vport; 1156 1157 idpf_vport_init(vport, max_q); 1158 1159 /* This alloc is done separate from the LUT because it's not strictly 1160 * dependent on how many queues we have. If we change number of queues 1161 * and soft reset we'll need a new LUT but the key can remain the same 1162 * for as long as the vport exists. 1163 */ 1164 rss_data = &adapter->vport_config[idx]->user_config.rss_data; 1165 rss_data->rss_key = kzalloc(rss_data->rss_key_size, GFP_KERNEL); 1166 if (!rss_data->rss_key) 1167 goto free_vector_idxs; 1168 1169 /* Initialize default rss key */ 1170 netdev_rss_key_fill((void *)rss_data->rss_key, rss_data->rss_key_size); 1171 1172 /* fill vport slot in the adapter struct */ 1173 adapter->vports[idx] = vport; 1174 adapter->vport_ids[idx] = idpf_get_vport_id(vport); 1175 1176 adapter->num_alloc_vports++; 1177 /* prepare adapter->next_vport for next use */ 1178 adapter->next_vport = idpf_get_free_slot(adapter); 1179 1180 return vport; 1181 1182 free_vector_idxs: 1183 kfree(vport->q_vector_idxs); 1184 free_vport: 1185 kfree(vport); 1186 1187 return NULL; 1188 } 1189 1190 /** 1191 * idpf_get_stats64 - get statistics for network device structure 1192 * @netdev: network interface device structure 1193 * @stats: main device statistics structure 1194 */ 1195 static void idpf_get_stats64(struct net_device *netdev, 1196 struct rtnl_link_stats64 *stats) 1197 { 1198 struct idpf_netdev_priv *np = netdev_priv(netdev); 1199 1200 spin_lock_bh(&np->stats_lock); 1201 *stats = np->netstats; 1202 spin_unlock_bh(&np->stats_lock); 1203 } 1204 1205 /** 1206 * idpf_statistics_task - Delayed task to get statistics over mailbox 1207 * @work: work_struct handle to our data 1208 */ 1209 void idpf_statistics_task(struct work_struct *work) 1210 { 1211 struct idpf_adapter *adapter; 1212 int i; 1213 1214 adapter = container_of(work, struct idpf_adapter, stats_task.work); 1215 1216 for (i = 0; i < adapter->max_vports; i++) { 1217 struct idpf_vport *vport = adapter->vports[i]; 1218 1219 if (vport && !test_bit(IDPF_HR_RESET_IN_PROG, adapter->flags)) 1220 idpf_send_get_stats_msg(vport); 1221 } 1222 1223 queue_delayed_work(adapter->stats_wq, &adapter->stats_task, 1224 msecs_to_jiffies(10000)); 1225 } 1226 1227 /** 1228 * idpf_mbx_task - Delayed task to handle mailbox responses 1229 * @work: work_struct handle 1230 */ 1231 void idpf_mbx_task(struct work_struct *work) 1232 { 1233 struct idpf_adapter *adapter; 1234 1235 adapter = container_of(work, struct idpf_adapter, mbx_task.work); 1236 1237 if (test_bit(IDPF_MB_INTR_MODE, adapter->flags)) 1238 idpf_mb_irq_enable(adapter); 1239 else 1240 queue_delayed_work(adapter->mbx_wq, &adapter->mbx_task, 1241 msecs_to_jiffies(300)); 1242 1243 idpf_recv_mb_msg(adapter); 1244 } 1245 1246 /** 1247 * idpf_service_task - Delayed task for handling mailbox responses 1248 * @work: work_struct handle to our data 1249 * 1250 */ 1251 void idpf_service_task(struct work_struct *work) 1252 { 1253 struct idpf_adapter *adapter; 1254 1255 adapter = container_of(work, struct idpf_adapter, serv_task.work); 1256 1257 if (idpf_is_reset_detected(adapter) && 1258 !idpf_is_reset_in_prog(adapter) && 1259 !test_bit(IDPF_REMOVE_IN_PROG, adapter->flags)) { 1260 dev_info(&adapter->pdev->dev, "HW reset detected\n"); 1261 set_bit(IDPF_HR_FUNC_RESET, adapter->flags); 1262 queue_delayed_work(adapter->vc_event_wq, 1263 &adapter->vc_event_task, 1264 msecs_to_jiffies(10)); 1265 } 1266 1267 queue_delayed_work(adapter->serv_wq, &adapter->serv_task, 1268 msecs_to_jiffies(300)); 1269 } 1270 1271 /** 1272 * idpf_restore_features - Restore feature configs 1273 * @vport: virtual port structure 1274 */ 1275 static void idpf_restore_features(struct idpf_vport *vport) 1276 { 1277 struct idpf_adapter *adapter = vport->adapter; 1278 1279 if (idpf_is_cap_ena(adapter, IDPF_OTHER_CAPS, VIRTCHNL2_CAP_MACFILTER)) 1280 idpf_restore_mac_filters(vport); 1281 } 1282 1283 /** 1284 * idpf_set_real_num_queues - set number of queues for netdev 1285 * @vport: virtual port structure 1286 * 1287 * Returns 0 on success, negative on failure. 1288 */ 1289 static int idpf_set_real_num_queues(struct idpf_vport *vport) 1290 { 1291 int err; 1292 1293 err = netif_set_real_num_rx_queues(vport->netdev, vport->num_rxq); 1294 if (err) 1295 return err; 1296 1297 return netif_set_real_num_tx_queues(vport->netdev, vport->num_txq); 1298 } 1299 1300 /** 1301 * idpf_up_complete - Complete interface up sequence 1302 * @vport: virtual port structure 1303 * 1304 * Returns 0 on success, negative on failure. 1305 */ 1306 static int idpf_up_complete(struct idpf_vport *vport) 1307 { 1308 struct idpf_netdev_priv *np = netdev_priv(vport->netdev); 1309 1310 if (vport->link_up && !netif_carrier_ok(vport->netdev)) { 1311 netif_carrier_on(vport->netdev); 1312 netif_tx_start_all_queues(vport->netdev); 1313 } 1314 1315 np->state = __IDPF_VPORT_UP; 1316 1317 return 0; 1318 } 1319 1320 /** 1321 * idpf_rx_init_buf_tail - Write initial buffer ring tail value 1322 * @vport: virtual port struct 1323 */ 1324 static void idpf_rx_init_buf_tail(struct idpf_vport *vport) 1325 { 1326 int i, j; 1327 1328 for (i = 0; i < vport->num_rxq_grp; i++) { 1329 struct idpf_rxq_group *grp = &vport->rxq_grps[i]; 1330 1331 if (idpf_is_queue_model_split(vport->rxq_model)) { 1332 for (j = 0; j < vport->num_bufqs_per_qgrp; j++) { 1333 const struct idpf_buf_queue *q = 1334 &grp->splitq.bufq_sets[j].bufq; 1335 1336 writel(q->next_to_alloc, q->tail); 1337 } 1338 } else { 1339 for (j = 0; j < grp->singleq.num_rxq; j++) { 1340 const struct idpf_rx_queue *q = 1341 grp->singleq.rxqs[j]; 1342 1343 writel(q->next_to_alloc, q->tail); 1344 } 1345 } 1346 } 1347 } 1348 1349 /** 1350 * idpf_vport_open - Bring up a vport 1351 * @vport: vport to bring up 1352 */ 1353 static int idpf_vport_open(struct idpf_vport *vport) 1354 { 1355 struct idpf_netdev_priv *np = netdev_priv(vport->netdev); 1356 struct idpf_adapter *adapter = vport->adapter; 1357 struct idpf_vport_config *vport_config; 1358 int err; 1359 1360 if (np->state != __IDPF_VPORT_DOWN) 1361 return -EBUSY; 1362 1363 /* we do not allow interface up just yet */ 1364 netif_carrier_off(vport->netdev); 1365 1366 err = idpf_vport_intr_alloc(vport); 1367 if (err) { 1368 dev_err(&adapter->pdev->dev, "Failed to allocate interrupts for vport %u: %d\n", 1369 vport->vport_id, err); 1370 return err; 1371 } 1372 1373 err = idpf_vport_queues_alloc(vport); 1374 if (err) 1375 goto intr_rel; 1376 1377 err = idpf_vport_queue_ids_init(vport); 1378 if (err) { 1379 dev_err(&adapter->pdev->dev, "Failed to initialize queue ids for vport %u: %d\n", 1380 vport->vport_id, err); 1381 goto queues_rel; 1382 } 1383 1384 err = idpf_vport_intr_init(vport); 1385 if (err) { 1386 dev_err(&adapter->pdev->dev, "Failed to initialize interrupts for vport %u: %d\n", 1387 vport->vport_id, err); 1388 goto queues_rel; 1389 } 1390 1391 err = idpf_rx_bufs_init_all(vport); 1392 if (err) { 1393 dev_err(&adapter->pdev->dev, "Failed to initialize RX buffers for vport %u: %d\n", 1394 vport->vport_id, err); 1395 goto queues_rel; 1396 } 1397 1398 err = idpf_queue_reg_init(vport); 1399 if (err) { 1400 dev_err(&adapter->pdev->dev, "Failed to initialize queue registers for vport %u: %d\n", 1401 vport->vport_id, err); 1402 goto queues_rel; 1403 } 1404 1405 idpf_rx_init_buf_tail(vport); 1406 idpf_vport_intr_ena(vport); 1407 1408 err = idpf_send_config_queues_msg(vport); 1409 if (err) { 1410 dev_err(&adapter->pdev->dev, "Failed to configure queues for vport %u, %d\n", 1411 vport->vport_id, err); 1412 goto intr_deinit; 1413 } 1414 1415 err = idpf_send_map_unmap_queue_vector_msg(vport, true); 1416 if (err) { 1417 dev_err(&adapter->pdev->dev, "Failed to map queue vectors for vport %u: %d\n", 1418 vport->vport_id, err); 1419 goto intr_deinit; 1420 } 1421 1422 err = idpf_send_enable_queues_msg(vport); 1423 if (err) { 1424 dev_err(&adapter->pdev->dev, "Failed to enable queues for vport %u: %d\n", 1425 vport->vport_id, err); 1426 goto unmap_queue_vectors; 1427 } 1428 1429 err = idpf_send_enable_vport_msg(vport); 1430 if (err) { 1431 dev_err(&adapter->pdev->dev, "Failed to enable vport %u: %d\n", 1432 vport->vport_id, err); 1433 err = -EAGAIN; 1434 goto disable_queues; 1435 } 1436 1437 idpf_restore_features(vport); 1438 1439 vport_config = adapter->vport_config[vport->idx]; 1440 if (vport_config->user_config.rss_data.rss_lut) 1441 err = idpf_config_rss(vport); 1442 else 1443 err = idpf_init_rss(vport); 1444 if (err) { 1445 dev_err(&adapter->pdev->dev, "Failed to initialize RSS for vport %u: %d\n", 1446 vport->vport_id, err); 1447 goto disable_vport; 1448 } 1449 1450 err = idpf_up_complete(vport); 1451 if (err) { 1452 dev_err(&adapter->pdev->dev, "Failed to complete interface up for vport %u: %d\n", 1453 vport->vport_id, err); 1454 goto deinit_rss; 1455 } 1456 1457 return 0; 1458 1459 deinit_rss: 1460 idpf_deinit_rss(vport); 1461 disable_vport: 1462 idpf_send_disable_vport_msg(vport); 1463 disable_queues: 1464 idpf_send_disable_queues_msg(vport); 1465 unmap_queue_vectors: 1466 idpf_send_map_unmap_queue_vector_msg(vport, false); 1467 intr_deinit: 1468 idpf_vport_intr_deinit(vport); 1469 queues_rel: 1470 idpf_vport_queues_rel(vport); 1471 intr_rel: 1472 idpf_vport_intr_rel(vport); 1473 1474 return err; 1475 } 1476 1477 /** 1478 * idpf_init_task - Delayed initialization task 1479 * @work: work_struct handle to our data 1480 * 1481 * Init task finishes up pending work started in probe. Due to the asynchronous 1482 * nature in which the device communicates with hardware, we may have to wait 1483 * several milliseconds to get a response. Instead of busy polling in probe, 1484 * pulling it out into a delayed work task prevents us from bogging down the 1485 * whole system waiting for a response from hardware. 1486 */ 1487 void idpf_init_task(struct work_struct *work) 1488 { 1489 struct idpf_vport_config *vport_config; 1490 struct idpf_vport_max_q max_q; 1491 struct idpf_adapter *adapter; 1492 struct idpf_netdev_priv *np; 1493 struct idpf_vport *vport; 1494 u16 num_default_vports; 1495 struct pci_dev *pdev; 1496 bool default_vport; 1497 int index, err; 1498 1499 adapter = container_of(work, struct idpf_adapter, init_task.work); 1500 1501 num_default_vports = idpf_get_default_vports(adapter); 1502 if (adapter->num_alloc_vports < num_default_vports) 1503 default_vport = true; 1504 else 1505 default_vport = false; 1506 1507 err = idpf_vport_alloc_max_qs(adapter, &max_q); 1508 if (err) 1509 goto unwind_vports; 1510 1511 err = idpf_send_create_vport_msg(adapter, &max_q); 1512 if (err) { 1513 idpf_vport_dealloc_max_qs(adapter, &max_q); 1514 goto unwind_vports; 1515 } 1516 1517 pdev = adapter->pdev; 1518 vport = idpf_vport_alloc(adapter, &max_q); 1519 if (!vport) { 1520 err = -EFAULT; 1521 dev_err(&pdev->dev, "failed to allocate vport: %d\n", 1522 err); 1523 idpf_vport_dealloc_max_qs(adapter, &max_q); 1524 goto unwind_vports; 1525 } 1526 1527 index = vport->idx; 1528 vport_config = adapter->vport_config[index]; 1529 1530 init_waitqueue_head(&vport->sw_marker_wq); 1531 1532 spin_lock_init(&vport_config->mac_filter_list_lock); 1533 1534 INIT_LIST_HEAD(&vport_config->user_config.mac_filter_list); 1535 1536 err = idpf_check_supported_desc_ids(vport); 1537 if (err) { 1538 dev_err(&pdev->dev, "failed to get required descriptor ids\n"); 1539 goto cfg_netdev_err; 1540 } 1541 1542 if (idpf_cfg_netdev(vport)) 1543 goto cfg_netdev_err; 1544 1545 err = idpf_send_get_rx_ptype_msg(vport); 1546 if (err) 1547 goto handle_err; 1548 1549 /* Once state is put into DOWN, driver is ready for dev_open */ 1550 np = netdev_priv(vport->netdev); 1551 np->state = __IDPF_VPORT_DOWN; 1552 if (test_and_clear_bit(IDPF_VPORT_UP_REQUESTED, vport_config->flags)) 1553 idpf_vport_open(vport); 1554 1555 /* Spawn and return 'idpf_init_task' work queue until all the 1556 * default vports are created 1557 */ 1558 if (adapter->num_alloc_vports < num_default_vports) { 1559 queue_delayed_work(adapter->init_wq, &adapter->init_task, 1560 msecs_to_jiffies(5 * (adapter->pdev->devfn & 0x07))); 1561 1562 return; 1563 } 1564 1565 for (index = 0; index < adapter->max_vports; index++) { 1566 struct net_device *netdev = adapter->netdevs[index]; 1567 struct idpf_vport_config *vport_config; 1568 1569 vport_config = adapter->vport_config[index]; 1570 1571 if (!netdev || 1572 test_bit(IDPF_VPORT_REG_NETDEV, vport_config->flags)) 1573 continue; 1574 1575 err = register_netdev(netdev); 1576 if (err) { 1577 dev_err(&pdev->dev, "failed to register netdev for vport %d: %pe\n", 1578 index, ERR_PTR(err)); 1579 continue; 1580 } 1581 set_bit(IDPF_VPORT_REG_NETDEV, vport_config->flags); 1582 } 1583 1584 /* As all the required vports are created, clear the reset flag 1585 * unconditionally here in case we were in reset and the link was down. 1586 */ 1587 clear_bit(IDPF_HR_RESET_IN_PROG, adapter->flags); 1588 /* Start the statistics task now */ 1589 queue_delayed_work(adapter->stats_wq, &adapter->stats_task, 1590 msecs_to_jiffies(10 * (pdev->devfn & 0x07))); 1591 1592 return; 1593 1594 handle_err: 1595 idpf_decfg_netdev(vport); 1596 cfg_netdev_err: 1597 idpf_vport_rel(vport); 1598 adapter->vports[index] = NULL; 1599 unwind_vports: 1600 if (default_vport) { 1601 for (index = 0; index < adapter->max_vports; index++) { 1602 if (adapter->vports[index]) 1603 idpf_vport_dealloc(adapter->vports[index]); 1604 } 1605 } 1606 clear_bit(IDPF_HR_RESET_IN_PROG, adapter->flags); 1607 } 1608 1609 /** 1610 * idpf_sriov_ena - Enable or change number of VFs 1611 * @adapter: private data struct 1612 * @num_vfs: number of VFs to allocate 1613 */ 1614 static int idpf_sriov_ena(struct idpf_adapter *adapter, int num_vfs) 1615 { 1616 struct device *dev = &adapter->pdev->dev; 1617 int err; 1618 1619 err = idpf_send_set_sriov_vfs_msg(adapter, num_vfs); 1620 if (err) { 1621 dev_err(dev, "Failed to allocate VFs: %d\n", err); 1622 1623 return err; 1624 } 1625 1626 err = pci_enable_sriov(adapter->pdev, num_vfs); 1627 if (err) { 1628 idpf_send_set_sriov_vfs_msg(adapter, 0); 1629 dev_err(dev, "Failed to enable SR-IOV: %d\n", err); 1630 1631 return err; 1632 } 1633 1634 adapter->num_vfs = num_vfs; 1635 1636 return num_vfs; 1637 } 1638 1639 /** 1640 * idpf_sriov_configure - Configure the requested VFs 1641 * @pdev: pointer to a pci_dev structure 1642 * @num_vfs: number of vfs to allocate 1643 * 1644 * Enable or change the number of VFs. Called when the user updates the number 1645 * of VFs in sysfs. 1646 **/ 1647 int idpf_sriov_configure(struct pci_dev *pdev, int num_vfs) 1648 { 1649 struct idpf_adapter *adapter = pci_get_drvdata(pdev); 1650 1651 if (!idpf_is_cap_ena(adapter, IDPF_OTHER_CAPS, VIRTCHNL2_CAP_SRIOV)) { 1652 dev_info(&pdev->dev, "SR-IOV is not supported on this device\n"); 1653 1654 return -EOPNOTSUPP; 1655 } 1656 1657 if (num_vfs) 1658 return idpf_sriov_ena(adapter, num_vfs); 1659 1660 if (pci_vfs_assigned(pdev)) { 1661 dev_warn(&pdev->dev, "Unable to free VFs because some are assigned to VMs\n"); 1662 1663 return -EBUSY; 1664 } 1665 1666 pci_disable_sriov(adapter->pdev); 1667 idpf_send_set_sriov_vfs_msg(adapter, 0); 1668 adapter->num_vfs = 0; 1669 1670 return 0; 1671 } 1672 1673 /** 1674 * idpf_deinit_task - Device deinit routine 1675 * @adapter: Driver specific private structure 1676 * 1677 * Extended remove logic which will be used for 1678 * hard reset as well 1679 */ 1680 void idpf_deinit_task(struct idpf_adapter *adapter) 1681 { 1682 unsigned int i; 1683 1684 /* Wait until the init_task is done else this thread might release 1685 * the resources first and the other thread might end up in a bad state 1686 */ 1687 cancel_delayed_work_sync(&adapter->init_task); 1688 1689 if (!adapter->vports) 1690 return; 1691 1692 cancel_delayed_work_sync(&adapter->stats_task); 1693 1694 for (i = 0; i < adapter->max_vports; i++) { 1695 if (adapter->vports[i]) 1696 idpf_vport_dealloc(adapter->vports[i]); 1697 } 1698 } 1699 1700 /** 1701 * idpf_check_reset_complete - check that reset is complete 1702 * @hw: pointer to hw struct 1703 * @reset_reg: struct with reset registers 1704 * 1705 * Returns 0 if device is ready to use, or -EBUSY if it's in reset. 1706 **/ 1707 static int idpf_check_reset_complete(struct idpf_hw *hw, 1708 struct idpf_reset_reg *reset_reg) 1709 { 1710 struct idpf_adapter *adapter = hw->back; 1711 int i; 1712 1713 for (i = 0; i < 2000; i++) { 1714 u32 reg_val = readl(reset_reg->rstat); 1715 1716 /* 0xFFFFFFFF might be read if other side hasn't cleared the 1717 * register for us yet and 0xFFFFFFFF is not a valid value for 1718 * the register, so treat that as invalid. 1719 */ 1720 if (reg_val != 0xFFFFFFFF && (reg_val & reset_reg->rstat_m)) 1721 return 0; 1722 1723 usleep_range(5000, 10000); 1724 } 1725 1726 dev_warn(&adapter->pdev->dev, "Device reset timeout!\n"); 1727 /* Clear the reset flag unconditionally here since the reset 1728 * technically isn't in progress anymore from the driver's perspective 1729 */ 1730 clear_bit(IDPF_HR_RESET_IN_PROG, adapter->flags); 1731 1732 return -EBUSY; 1733 } 1734 1735 /** 1736 * idpf_set_vport_state - Set the vport state to be after the reset 1737 * @adapter: Driver specific private structure 1738 */ 1739 static void idpf_set_vport_state(struct idpf_adapter *adapter) 1740 { 1741 u16 i; 1742 1743 for (i = 0; i < adapter->max_vports; i++) { 1744 struct idpf_netdev_priv *np; 1745 1746 if (!adapter->netdevs[i]) 1747 continue; 1748 1749 np = netdev_priv(adapter->netdevs[i]); 1750 if (np->state == __IDPF_VPORT_UP) 1751 set_bit(IDPF_VPORT_UP_REQUESTED, 1752 adapter->vport_config[i]->flags); 1753 } 1754 } 1755 1756 /** 1757 * idpf_init_hard_reset - Initiate a hardware reset 1758 * @adapter: Driver specific private structure 1759 * 1760 * Deallocate the vports and all the resources associated with them and 1761 * reallocate. Also reinitialize the mailbox. Return 0 on success, 1762 * negative on failure. 1763 */ 1764 static int idpf_init_hard_reset(struct idpf_adapter *adapter) 1765 { 1766 struct idpf_reg_ops *reg_ops = &adapter->dev_ops.reg_ops; 1767 struct device *dev = &adapter->pdev->dev; 1768 struct net_device *netdev; 1769 int err; 1770 u16 i; 1771 1772 mutex_lock(&adapter->vport_ctrl_lock); 1773 1774 dev_info(dev, "Device HW Reset initiated\n"); 1775 1776 /* Avoid TX hangs on reset */ 1777 for (i = 0; i < adapter->max_vports; i++) { 1778 netdev = adapter->netdevs[i]; 1779 if (!netdev) 1780 continue; 1781 1782 netif_carrier_off(netdev); 1783 netif_tx_disable(netdev); 1784 } 1785 1786 /* Prepare for reset */ 1787 if (test_and_clear_bit(IDPF_HR_DRV_LOAD, adapter->flags)) { 1788 reg_ops->trigger_reset(adapter, IDPF_HR_DRV_LOAD); 1789 } else if (test_and_clear_bit(IDPF_HR_FUNC_RESET, adapter->flags)) { 1790 bool is_reset = idpf_is_reset_detected(adapter); 1791 1792 idpf_idc_issue_reset_event(adapter->cdev_info); 1793 1794 idpf_set_vport_state(adapter); 1795 idpf_vc_core_deinit(adapter); 1796 if (!is_reset) 1797 reg_ops->trigger_reset(adapter, IDPF_HR_FUNC_RESET); 1798 idpf_deinit_dflt_mbx(adapter); 1799 } else { 1800 dev_err(dev, "Unhandled hard reset cause\n"); 1801 err = -EBADRQC; 1802 goto unlock_mutex; 1803 } 1804 1805 /* Wait for reset to complete */ 1806 err = idpf_check_reset_complete(&adapter->hw, &adapter->reset_reg); 1807 if (err) { 1808 dev_err(dev, "The driver was unable to contact the device's firmware. Check that the FW is running. Driver state= 0x%x\n", 1809 adapter->state); 1810 goto unlock_mutex; 1811 } 1812 1813 /* Reset is complete and so start building the driver resources again */ 1814 err = idpf_init_dflt_mbx(adapter); 1815 if (err) { 1816 dev_err(dev, "Failed to initialize default mailbox: %d\n", err); 1817 goto unlock_mutex; 1818 } 1819 1820 queue_delayed_work(adapter->mbx_wq, &adapter->mbx_task, 0); 1821 1822 /* Initialize the state machine, also allocate memory and request 1823 * resources 1824 */ 1825 err = idpf_vc_core_init(adapter); 1826 if (err) { 1827 cancel_delayed_work_sync(&adapter->mbx_task); 1828 idpf_deinit_dflt_mbx(adapter); 1829 goto unlock_mutex; 1830 } 1831 1832 /* Wait till all the vports are initialized to release the reset lock, 1833 * else user space callbacks may access uninitialized vports 1834 */ 1835 while (test_bit(IDPF_HR_RESET_IN_PROG, adapter->flags)) 1836 msleep(100); 1837 1838 unlock_mutex: 1839 mutex_unlock(&adapter->vport_ctrl_lock); 1840 1841 /* Wait until all vports are created to init RDMA CORE AUX */ 1842 if (!err) 1843 err = idpf_idc_init(adapter); 1844 1845 return err; 1846 } 1847 1848 /** 1849 * idpf_vc_event_task - Handle virtchannel event logic 1850 * @work: work queue struct 1851 */ 1852 void idpf_vc_event_task(struct work_struct *work) 1853 { 1854 struct idpf_adapter *adapter; 1855 1856 adapter = container_of(work, struct idpf_adapter, vc_event_task.work); 1857 1858 if (test_bit(IDPF_REMOVE_IN_PROG, adapter->flags)) 1859 return; 1860 1861 if (test_bit(IDPF_HR_FUNC_RESET, adapter->flags)) 1862 goto func_reset; 1863 1864 if (test_bit(IDPF_HR_DRV_LOAD, adapter->flags)) 1865 goto drv_load; 1866 1867 return; 1868 1869 func_reset: 1870 idpf_vc_xn_shutdown(adapter->vcxn_mngr); 1871 drv_load: 1872 set_bit(IDPF_HR_RESET_IN_PROG, adapter->flags); 1873 idpf_init_hard_reset(adapter); 1874 } 1875 1876 /** 1877 * idpf_initiate_soft_reset - Initiate a software reset 1878 * @vport: virtual port data struct 1879 * @reset_cause: reason for the soft reset 1880 * 1881 * Soft reset only reallocs vport queue resources. Returns 0 on success, 1882 * negative on failure. 1883 */ 1884 int idpf_initiate_soft_reset(struct idpf_vport *vport, 1885 enum idpf_vport_reset_cause reset_cause) 1886 { 1887 struct idpf_netdev_priv *np = netdev_priv(vport->netdev); 1888 enum idpf_vport_state current_state = np->state; 1889 struct idpf_adapter *adapter = vport->adapter; 1890 struct idpf_vport *new_vport; 1891 int err; 1892 1893 /* If the system is low on memory, we can end up in bad state if we 1894 * free all the memory for queue resources and try to allocate them 1895 * again. Instead, we can pre-allocate the new resources before doing 1896 * anything and bailing if the alloc fails. 1897 * 1898 * Make a clone of the existing vport to mimic its current 1899 * configuration, then modify the new structure with any requested 1900 * changes. Once the allocation of the new resources is done, stop the 1901 * existing vport and copy the configuration to the main vport. If an 1902 * error occurred, the existing vport will be untouched. 1903 * 1904 */ 1905 new_vport = kzalloc(sizeof(*vport), GFP_KERNEL); 1906 if (!new_vport) 1907 return -ENOMEM; 1908 1909 /* This purposely avoids copying the end of the struct because it 1910 * contains wait_queues and mutexes and other stuff we don't want to 1911 * mess with. Nothing below should use those variables from new_vport 1912 * and should instead always refer to them in vport if they need to. 1913 */ 1914 memcpy(new_vport, vport, offsetof(struct idpf_vport, link_up)); 1915 1916 /* Adjust resource parameters prior to reallocating resources */ 1917 switch (reset_cause) { 1918 case IDPF_SR_Q_CHANGE: 1919 err = idpf_vport_adjust_qs(new_vport); 1920 if (err) 1921 goto free_vport; 1922 break; 1923 case IDPF_SR_Q_DESC_CHANGE: 1924 /* Update queue parameters before allocating resources */ 1925 idpf_vport_calc_num_q_desc(new_vport); 1926 break; 1927 case IDPF_SR_MTU_CHANGE: 1928 idpf_idc_vdev_mtu_event(vport->vdev_info, 1929 IIDC_RDMA_EVENT_BEFORE_MTU_CHANGE); 1930 break; 1931 case IDPF_SR_RSC_CHANGE: 1932 break; 1933 default: 1934 dev_err(&adapter->pdev->dev, "Unhandled soft reset cause\n"); 1935 err = -EINVAL; 1936 goto free_vport; 1937 } 1938 1939 if (current_state <= __IDPF_VPORT_DOWN) { 1940 idpf_send_delete_queues_msg(vport); 1941 } else { 1942 set_bit(IDPF_VPORT_DEL_QUEUES, vport->flags); 1943 idpf_vport_stop(vport); 1944 } 1945 1946 idpf_deinit_rss(vport); 1947 /* We're passing in vport here because we need its wait_queue 1948 * to send a message and it should be getting all the vport 1949 * config data out of the adapter but we need to be careful not 1950 * to add code to add_queues to change the vport config within 1951 * vport itself as it will be wiped with a memcpy later. 1952 */ 1953 err = idpf_send_add_queues_msg(vport, new_vport->num_txq, 1954 new_vport->num_complq, 1955 new_vport->num_rxq, 1956 new_vport->num_bufq); 1957 if (err) 1958 goto err_reset; 1959 1960 /* Same comment as above regarding avoiding copying the wait_queues and 1961 * mutexes applies here. We do not want to mess with those if possible. 1962 */ 1963 memcpy(vport, new_vport, offsetof(struct idpf_vport, link_up)); 1964 1965 if (reset_cause == IDPF_SR_Q_CHANGE) 1966 idpf_vport_alloc_vec_indexes(vport); 1967 1968 err = idpf_set_real_num_queues(vport); 1969 if (err) 1970 goto err_open; 1971 1972 if (current_state == __IDPF_VPORT_UP) 1973 err = idpf_vport_open(vport); 1974 1975 goto free_vport; 1976 1977 err_reset: 1978 idpf_send_add_queues_msg(vport, vport->num_txq, vport->num_complq, 1979 vport->num_rxq, vport->num_bufq); 1980 1981 err_open: 1982 if (current_state == __IDPF_VPORT_UP) 1983 idpf_vport_open(vport); 1984 1985 free_vport: 1986 kfree(new_vport); 1987 1988 if (reset_cause == IDPF_SR_MTU_CHANGE) 1989 idpf_idc_vdev_mtu_event(vport->vdev_info, 1990 IIDC_RDMA_EVENT_AFTER_MTU_CHANGE); 1991 1992 return err; 1993 } 1994 1995 /** 1996 * idpf_addr_sync - Callback for dev_(mc|uc)_sync to add address 1997 * @netdev: the netdevice 1998 * @addr: address to add 1999 * 2000 * Called by __dev_(mc|uc)_sync when an address needs to be added. We call 2001 * __dev_(uc|mc)_sync from .set_rx_mode. Kernel takes addr_list_lock spinlock 2002 * meaning we cannot sleep in this context. Due to this, we have to add the 2003 * filter and send the virtchnl message asynchronously without waiting for the 2004 * response from the other side. We won't know whether or not the operation 2005 * actually succeeded until we get the message back. Returns 0 on success, 2006 * negative on failure. 2007 */ 2008 static int idpf_addr_sync(struct net_device *netdev, const u8 *addr) 2009 { 2010 struct idpf_netdev_priv *np = netdev_priv(netdev); 2011 2012 return idpf_add_mac_filter(np->vport, np, addr, true); 2013 } 2014 2015 /** 2016 * idpf_addr_unsync - Callback for dev_(mc|uc)_sync to remove address 2017 * @netdev: the netdevice 2018 * @addr: address to add 2019 * 2020 * Called by __dev_(mc|uc)_sync when an address needs to be added. We call 2021 * __dev_(uc|mc)_sync from .set_rx_mode. Kernel takes addr_list_lock spinlock 2022 * meaning we cannot sleep in this context. Due to this we have to delete the 2023 * filter and send the virtchnl message asynchronously without waiting for the 2024 * return from the other side. We won't know whether or not the operation 2025 * actually succeeded until we get the message back. Returns 0 on success, 2026 * negative on failure. 2027 */ 2028 static int idpf_addr_unsync(struct net_device *netdev, const u8 *addr) 2029 { 2030 struct idpf_netdev_priv *np = netdev_priv(netdev); 2031 2032 /* Under some circumstances, we might receive a request to delete 2033 * our own device address from our uc list. Because we store the 2034 * device address in the VSI's MAC filter list, we need to ignore 2035 * such requests and not delete our device address from this list. 2036 */ 2037 if (ether_addr_equal(addr, netdev->dev_addr)) 2038 return 0; 2039 2040 idpf_del_mac_filter(np->vport, np, addr, true); 2041 2042 return 0; 2043 } 2044 2045 /** 2046 * idpf_set_rx_mode - NDO callback to set the netdev filters 2047 * @netdev: network interface device structure 2048 * 2049 * Stack takes addr_list_lock spinlock before calling our .set_rx_mode. We 2050 * cannot sleep in this context. 2051 */ 2052 static void idpf_set_rx_mode(struct net_device *netdev) 2053 { 2054 struct idpf_netdev_priv *np = netdev_priv(netdev); 2055 struct idpf_vport_user_config_data *config_data; 2056 struct idpf_adapter *adapter; 2057 bool changed = false; 2058 struct device *dev; 2059 int err; 2060 2061 adapter = np->adapter; 2062 dev = &adapter->pdev->dev; 2063 2064 if (idpf_is_cap_ena(adapter, IDPF_OTHER_CAPS, VIRTCHNL2_CAP_MACFILTER)) { 2065 __dev_uc_sync(netdev, idpf_addr_sync, idpf_addr_unsync); 2066 __dev_mc_sync(netdev, idpf_addr_sync, idpf_addr_unsync); 2067 } 2068 2069 if (!idpf_is_cap_ena(adapter, IDPF_OTHER_CAPS, VIRTCHNL2_CAP_PROMISC)) 2070 return; 2071 2072 config_data = &adapter->vport_config[np->vport_idx]->user_config; 2073 /* IFF_PROMISC enables both unicast and multicast promiscuous, 2074 * while IFF_ALLMULTI only enables multicast such that: 2075 * 2076 * promisc + allmulti = unicast | multicast 2077 * promisc + !allmulti = unicast | multicast 2078 * !promisc + allmulti = multicast 2079 */ 2080 if ((netdev->flags & IFF_PROMISC) && 2081 !test_and_set_bit(__IDPF_PROMISC_UC, config_data->user_flags)) { 2082 changed = true; 2083 dev_info(&adapter->pdev->dev, "Entering promiscuous mode\n"); 2084 if (!test_and_set_bit(__IDPF_PROMISC_MC, adapter->flags)) 2085 dev_info(dev, "Entering multicast promiscuous mode\n"); 2086 } 2087 2088 if (!(netdev->flags & IFF_PROMISC) && 2089 test_and_clear_bit(__IDPF_PROMISC_UC, config_data->user_flags)) { 2090 changed = true; 2091 dev_info(dev, "Leaving promiscuous mode\n"); 2092 } 2093 2094 if (netdev->flags & IFF_ALLMULTI && 2095 !test_and_set_bit(__IDPF_PROMISC_MC, config_data->user_flags)) { 2096 changed = true; 2097 dev_info(dev, "Entering multicast promiscuous mode\n"); 2098 } 2099 2100 if (!(netdev->flags & (IFF_ALLMULTI | IFF_PROMISC)) && 2101 test_and_clear_bit(__IDPF_PROMISC_MC, config_data->user_flags)) { 2102 changed = true; 2103 dev_info(dev, "Leaving multicast promiscuous mode\n"); 2104 } 2105 2106 if (!changed) 2107 return; 2108 2109 err = idpf_set_promiscuous(adapter, config_data, np->vport_id); 2110 if (err) 2111 dev_err(dev, "Failed to set promiscuous mode: %d\n", err); 2112 } 2113 2114 /** 2115 * idpf_vport_manage_rss_lut - disable/enable RSS 2116 * @vport: the vport being changed 2117 * 2118 * In the event of disable request for RSS, this function will zero out RSS 2119 * LUT, while in the event of enable request for RSS, it will reconfigure RSS 2120 * LUT with the default LUT configuration. 2121 */ 2122 static int idpf_vport_manage_rss_lut(struct idpf_vport *vport) 2123 { 2124 bool ena = idpf_is_feature_ena(vport, NETIF_F_RXHASH); 2125 struct idpf_rss_data *rss_data; 2126 u16 idx = vport->idx; 2127 int lut_size; 2128 2129 rss_data = &vport->adapter->vport_config[idx]->user_config.rss_data; 2130 lut_size = rss_data->rss_lut_size * sizeof(u32); 2131 2132 if (ena) { 2133 /* This will contain the default or user configured LUT */ 2134 memcpy(rss_data->rss_lut, rss_data->cached_lut, lut_size); 2135 } else { 2136 /* Save a copy of the current LUT to be restored later if 2137 * requested. 2138 */ 2139 memcpy(rss_data->cached_lut, rss_data->rss_lut, lut_size); 2140 2141 /* Zero out the current LUT to disable */ 2142 memset(rss_data->rss_lut, 0, lut_size); 2143 } 2144 2145 return idpf_config_rss(vport); 2146 } 2147 2148 /** 2149 * idpf_set_features - set the netdev feature flags 2150 * @netdev: ptr to the netdev being adjusted 2151 * @features: the feature set that the stack is suggesting 2152 */ 2153 static int idpf_set_features(struct net_device *netdev, 2154 netdev_features_t features) 2155 { 2156 netdev_features_t changed = netdev->features ^ features; 2157 struct idpf_adapter *adapter; 2158 struct idpf_vport *vport; 2159 int err = 0; 2160 2161 idpf_vport_ctrl_lock(netdev); 2162 vport = idpf_netdev_to_vport(netdev); 2163 2164 adapter = vport->adapter; 2165 2166 if (idpf_is_reset_in_prog(adapter)) { 2167 dev_err(&adapter->pdev->dev, "Device is resetting, changing netdev features temporarily unavailable.\n"); 2168 err = -EBUSY; 2169 goto unlock_mutex; 2170 } 2171 2172 if (changed & NETIF_F_RXHASH) { 2173 netdev->features ^= NETIF_F_RXHASH; 2174 err = idpf_vport_manage_rss_lut(vport); 2175 if (err) 2176 goto unlock_mutex; 2177 } 2178 2179 if (changed & NETIF_F_GRO_HW) { 2180 netdev->features ^= NETIF_F_GRO_HW; 2181 err = idpf_initiate_soft_reset(vport, IDPF_SR_RSC_CHANGE); 2182 if (err) 2183 goto unlock_mutex; 2184 } 2185 2186 if (changed & NETIF_F_LOOPBACK) { 2187 netdev->features ^= NETIF_F_LOOPBACK; 2188 err = idpf_send_ena_dis_loopback_msg(vport); 2189 } 2190 2191 unlock_mutex: 2192 idpf_vport_ctrl_unlock(netdev); 2193 2194 return err; 2195 } 2196 2197 /** 2198 * idpf_open - Called when a network interface becomes active 2199 * @netdev: network interface device structure 2200 * 2201 * The open entry point is called when a network interface is made 2202 * active by the system (IFF_UP). At this point all resources needed 2203 * for transmit and receive operations are allocated, the interrupt 2204 * handler is registered with the OS, the netdev watchdog is enabled, 2205 * and the stack is notified that the interface is ready. 2206 * 2207 * Returns 0 on success, negative value on failure 2208 */ 2209 static int idpf_open(struct net_device *netdev) 2210 { 2211 struct idpf_vport *vport; 2212 int err; 2213 2214 idpf_vport_ctrl_lock(netdev); 2215 vport = idpf_netdev_to_vport(netdev); 2216 2217 err = idpf_set_real_num_queues(vport); 2218 if (err) 2219 goto unlock; 2220 2221 err = idpf_vport_open(vport); 2222 2223 unlock: 2224 idpf_vport_ctrl_unlock(netdev); 2225 2226 return err; 2227 } 2228 2229 /** 2230 * idpf_change_mtu - NDO callback to change the MTU 2231 * @netdev: network interface device structure 2232 * @new_mtu: new value for maximum frame size 2233 * 2234 * Returns 0 on success, negative on failure 2235 */ 2236 static int idpf_change_mtu(struct net_device *netdev, int new_mtu) 2237 { 2238 struct idpf_vport *vport; 2239 int err; 2240 2241 idpf_vport_ctrl_lock(netdev); 2242 vport = idpf_netdev_to_vport(netdev); 2243 2244 WRITE_ONCE(netdev->mtu, new_mtu); 2245 2246 err = idpf_initiate_soft_reset(vport, IDPF_SR_MTU_CHANGE); 2247 2248 idpf_vport_ctrl_unlock(netdev); 2249 2250 return err; 2251 } 2252 2253 /** 2254 * idpf_features_check - Validate packet conforms to limits 2255 * @skb: skb buffer 2256 * @netdev: This port's netdev 2257 * @features: Offload features that the stack believes apply 2258 */ 2259 static netdev_features_t idpf_features_check(struct sk_buff *skb, 2260 struct net_device *netdev, 2261 netdev_features_t features) 2262 { 2263 struct idpf_netdev_priv *np = netdev_priv(netdev); 2264 u16 max_tx_hdr_size = np->max_tx_hdr_size; 2265 size_t len; 2266 2267 /* No point in doing any of this if neither checksum nor GSO are 2268 * being requested for this frame. We can rule out both by just 2269 * checking for CHECKSUM_PARTIAL 2270 */ 2271 if (skb->ip_summed != CHECKSUM_PARTIAL) 2272 return features; 2273 2274 /* We cannot support GSO if the MSS is going to be less than 2275 * 88 bytes. If it is then we need to drop support for GSO. 2276 */ 2277 if (skb_is_gso(skb) && 2278 (skb_shinfo(skb)->gso_size < IDPF_TX_TSO_MIN_MSS)) 2279 features &= ~NETIF_F_GSO_MASK; 2280 2281 /* Ensure MACLEN is <= 126 bytes (63 words) and not an odd size */ 2282 len = skb_network_offset(skb); 2283 if (unlikely(len & ~(126))) 2284 goto unsupported; 2285 2286 len = skb_network_header_len(skb); 2287 if (unlikely(len > max_tx_hdr_size)) 2288 goto unsupported; 2289 2290 if (!skb->encapsulation) 2291 return features; 2292 2293 /* L4TUNLEN can support 127 words */ 2294 len = skb_inner_network_header(skb) - skb_transport_header(skb); 2295 if (unlikely(len & ~(127 * 2))) 2296 goto unsupported; 2297 2298 /* IPLEN can support at most 127 dwords */ 2299 len = skb_inner_network_header_len(skb); 2300 if (unlikely(len > max_tx_hdr_size)) 2301 goto unsupported; 2302 2303 /* No need to validate L4LEN as TCP is the only protocol with a 2304 * a flexible value and we support all possible values supported 2305 * by TCP, which is at most 15 dwords 2306 */ 2307 2308 return features; 2309 2310 unsupported: 2311 return features & ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK); 2312 } 2313 2314 /** 2315 * idpf_set_mac - NDO callback to set port mac address 2316 * @netdev: network interface device structure 2317 * @p: pointer to an address structure 2318 * 2319 * Returns 0 on success, negative on failure 2320 **/ 2321 static int idpf_set_mac(struct net_device *netdev, void *p) 2322 { 2323 struct idpf_netdev_priv *np = netdev_priv(netdev); 2324 struct idpf_vport_config *vport_config; 2325 struct sockaddr *addr = p; 2326 struct idpf_vport *vport; 2327 int err = 0; 2328 2329 idpf_vport_ctrl_lock(netdev); 2330 vport = idpf_netdev_to_vport(netdev); 2331 2332 if (!idpf_is_cap_ena(vport->adapter, IDPF_OTHER_CAPS, 2333 VIRTCHNL2_CAP_MACFILTER)) { 2334 dev_info(&vport->adapter->pdev->dev, "Setting MAC address is not supported\n"); 2335 err = -EOPNOTSUPP; 2336 goto unlock_mutex; 2337 } 2338 2339 if (!is_valid_ether_addr(addr->sa_data)) { 2340 dev_info(&vport->adapter->pdev->dev, "Invalid MAC address: %pM\n", 2341 addr->sa_data); 2342 err = -EADDRNOTAVAIL; 2343 goto unlock_mutex; 2344 } 2345 2346 if (ether_addr_equal(netdev->dev_addr, addr->sa_data)) 2347 goto unlock_mutex; 2348 2349 vport_config = vport->adapter->vport_config[vport->idx]; 2350 err = idpf_add_mac_filter(vport, np, addr->sa_data, false); 2351 if (err) { 2352 __idpf_del_mac_filter(vport_config, addr->sa_data); 2353 goto unlock_mutex; 2354 } 2355 2356 if (is_valid_ether_addr(vport->default_mac_addr)) 2357 idpf_del_mac_filter(vport, np, vport->default_mac_addr, false); 2358 2359 ether_addr_copy(vport->default_mac_addr, addr->sa_data); 2360 eth_hw_addr_set(netdev, addr->sa_data); 2361 2362 unlock_mutex: 2363 idpf_vport_ctrl_unlock(netdev); 2364 2365 return err; 2366 } 2367 2368 /** 2369 * idpf_alloc_dma_mem - Allocate dma memory 2370 * @hw: pointer to hw struct 2371 * @mem: pointer to dma_mem struct 2372 * @size: size of the memory to allocate 2373 */ 2374 void *idpf_alloc_dma_mem(struct idpf_hw *hw, struct idpf_dma_mem *mem, u64 size) 2375 { 2376 struct idpf_adapter *adapter = hw->back; 2377 size_t sz = ALIGN(size, 4096); 2378 2379 /* The control queue resources are freed under a spinlock, contiguous 2380 * pages will avoid IOMMU remapping and the use vmap (and vunmap in 2381 * dma_free_*() path. 2382 */ 2383 mem->va = dma_alloc_attrs(&adapter->pdev->dev, sz, &mem->pa, 2384 GFP_KERNEL, DMA_ATTR_FORCE_CONTIGUOUS); 2385 mem->size = sz; 2386 2387 return mem->va; 2388 } 2389 2390 /** 2391 * idpf_free_dma_mem - Free the allocated dma memory 2392 * @hw: pointer to hw struct 2393 * @mem: pointer to dma_mem struct 2394 */ 2395 void idpf_free_dma_mem(struct idpf_hw *hw, struct idpf_dma_mem *mem) 2396 { 2397 struct idpf_adapter *adapter = hw->back; 2398 2399 dma_free_attrs(&adapter->pdev->dev, mem->size, 2400 mem->va, mem->pa, DMA_ATTR_FORCE_CONTIGUOUS); 2401 mem->size = 0; 2402 mem->va = NULL; 2403 mem->pa = 0; 2404 } 2405 2406 static int idpf_hwtstamp_set(struct net_device *netdev, 2407 struct kernel_hwtstamp_config *config, 2408 struct netlink_ext_ack *extack) 2409 { 2410 struct idpf_vport *vport; 2411 int err; 2412 2413 idpf_vport_ctrl_lock(netdev); 2414 vport = idpf_netdev_to_vport(netdev); 2415 2416 if (!vport->link_up) { 2417 idpf_vport_ctrl_unlock(netdev); 2418 return -EPERM; 2419 } 2420 2421 if (!idpf_ptp_is_vport_tx_tstamp_ena(vport) && 2422 !idpf_ptp_is_vport_rx_tstamp_ena(vport)) { 2423 idpf_vport_ctrl_unlock(netdev); 2424 return -EOPNOTSUPP; 2425 } 2426 2427 err = idpf_ptp_set_timestamp_mode(vport, config); 2428 2429 idpf_vport_ctrl_unlock(netdev); 2430 2431 return err; 2432 } 2433 2434 static int idpf_hwtstamp_get(struct net_device *netdev, 2435 struct kernel_hwtstamp_config *config) 2436 { 2437 struct idpf_vport *vport; 2438 2439 idpf_vport_ctrl_lock(netdev); 2440 vport = idpf_netdev_to_vport(netdev); 2441 2442 if (!vport->link_up) { 2443 idpf_vport_ctrl_unlock(netdev); 2444 return -EPERM; 2445 } 2446 2447 if (!idpf_ptp_is_vport_tx_tstamp_ena(vport) && 2448 !idpf_ptp_is_vport_rx_tstamp_ena(vport)) { 2449 idpf_vport_ctrl_unlock(netdev); 2450 return 0; 2451 } 2452 2453 *config = vport->tstamp_config; 2454 2455 idpf_vport_ctrl_unlock(netdev); 2456 2457 return 0; 2458 } 2459 2460 static const struct net_device_ops idpf_netdev_ops = { 2461 .ndo_open = idpf_open, 2462 .ndo_stop = idpf_stop, 2463 .ndo_start_xmit = idpf_tx_start, 2464 .ndo_features_check = idpf_features_check, 2465 .ndo_set_rx_mode = idpf_set_rx_mode, 2466 .ndo_validate_addr = eth_validate_addr, 2467 .ndo_set_mac_address = idpf_set_mac, 2468 .ndo_change_mtu = idpf_change_mtu, 2469 .ndo_get_stats64 = idpf_get_stats64, 2470 .ndo_set_features = idpf_set_features, 2471 .ndo_tx_timeout = idpf_tx_timeout, 2472 .ndo_hwtstamp_get = idpf_hwtstamp_get, 2473 .ndo_hwtstamp_set = idpf_hwtstamp_set, 2474 }; 2475