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