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