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 vport->netdev = netdev; 712 713 return idpf_init_mac_addr(vport, netdev); 714 } 715 716 netdev = alloc_etherdev_mqs(sizeof(struct idpf_netdev_priv), 717 vport_config->max_q.max_txq, 718 vport_config->max_q.max_rxq); 719 if (!netdev) 720 return -ENOMEM; 721 722 vport->netdev = netdev; 723 np = netdev_priv(netdev); 724 np->vport = vport; 725 np->adapter = adapter; 726 np->vport_idx = vport->idx; 727 np->vport_id = vport->vport_id; 728 729 spin_lock_init(&np->stats_lock); 730 731 err = idpf_init_mac_addr(vport, netdev); 732 if (err) { 733 free_netdev(vport->netdev); 734 vport->netdev = NULL; 735 736 return err; 737 } 738 739 /* assign netdev_ops */ 740 netdev->netdev_ops = &idpf_netdev_ops; 741 742 /* setup watchdog timeout value to be 5 second */ 743 netdev->watchdog_timeo = 5 * HZ; 744 745 netdev->dev_port = idx; 746 747 /* configure default MTU size */ 748 netdev->min_mtu = ETH_MIN_MTU; 749 netdev->max_mtu = vport->max_mtu; 750 751 dflt_features = NETIF_F_SG | 752 NETIF_F_HIGHDMA; 753 754 if (idpf_is_cap_ena_all(adapter, IDPF_RSS_CAPS, IDPF_CAP_RSS)) 755 dflt_features |= NETIF_F_RXHASH; 756 if (idpf_is_cap_ena_all(adapter, IDPF_CSUM_CAPS, IDPF_CAP_TX_CSUM_L4V4)) 757 csum_offloads |= NETIF_F_IP_CSUM; 758 if (idpf_is_cap_ena_all(adapter, IDPF_CSUM_CAPS, IDPF_CAP_TX_CSUM_L4V6)) 759 csum_offloads |= NETIF_F_IPV6_CSUM; 760 if (idpf_is_cap_ena(adapter, IDPF_CSUM_CAPS, IDPF_CAP_RX_CSUM)) 761 csum_offloads |= NETIF_F_RXCSUM; 762 if (idpf_is_cap_ena_all(adapter, IDPF_CSUM_CAPS, IDPF_CAP_TX_SCTP_CSUM)) 763 csum_offloads |= NETIF_F_SCTP_CRC; 764 765 if (idpf_is_cap_ena(adapter, IDPF_SEG_CAPS, VIRTCHNL2_CAP_SEG_IPV4_TCP)) 766 tso_offloads |= NETIF_F_TSO; 767 if (idpf_is_cap_ena(adapter, IDPF_SEG_CAPS, VIRTCHNL2_CAP_SEG_IPV6_TCP)) 768 tso_offloads |= NETIF_F_TSO6; 769 if (idpf_is_cap_ena_all(adapter, IDPF_SEG_CAPS, 770 VIRTCHNL2_CAP_SEG_IPV4_UDP | 771 VIRTCHNL2_CAP_SEG_IPV6_UDP)) 772 tso_offloads |= NETIF_F_GSO_UDP_L4; 773 if (idpf_is_cap_ena_all(adapter, IDPF_RSC_CAPS, IDPF_CAP_RSC)) 774 other_offloads |= NETIF_F_GRO_HW; 775 if (idpf_is_cap_ena(adapter, IDPF_OTHER_CAPS, VIRTCHNL2_CAP_LOOPBACK)) 776 other_offloads |= NETIF_F_LOOPBACK; 777 778 netdev->features |= dflt_features | csum_offloads | tso_offloads; 779 netdev->hw_features |= netdev->features | other_offloads; 780 netdev->vlan_features |= netdev->features | other_offloads; 781 netdev->hw_enc_features |= dflt_features | other_offloads; 782 idpf_set_ethtool_ops(netdev); 783 netif_set_affinity_auto(netdev); 784 SET_NETDEV_DEV(netdev, &adapter->pdev->dev); 785 786 /* carrier off on init to avoid Tx hangs */ 787 netif_carrier_off(netdev); 788 789 /* make sure transmit queues start off as stopped */ 790 netif_tx_stop_all_queues(netdev); 791 792 /* The vport can be arbitrarily released so we need to also track 793 * netdevs in the adapter struct 794 */ 795 adapter->netdevs[idx] = netdev; 796 797 return 0; 798 } 799 800 /** 801 * idpf_get_free_slot - get the next non-NULL location index in array 802 * @adapter: adapter in which to look for a free vport slot 803 */ 804 static int idpf_get_free_slot(struct idpf_adapter *adapter) 805 { 806 unsigned int i; 807 808 for (i = 0; i < adapter->max_vports; i++) { 809 if (!adapter->vports[i]) 810 return i; 811 } 812 813 return IDPF_NO_FREE_SLOT; 814 } 815 816 /** 817 * idpf_remove_features - Turn off feature configs 818 * @vport: virtual port structure 819 */ 820 static void idpf_remove_features(struct idpf_vport *vport) 821 { 822 struct idpf_adapter *adapter = vport->adapter; 823 824 if (idpf_is_cap_ena(adapter, IDPF_OTHER_CAPS, VIRTCHNL2_CAP_MACFILTER)) 825 idpf_remove_mac_filters(vport); 826 } 827 828 /** 829 * idpf_vport_stop - Disable a vport 830 * @vport: vport to disable 831 */ 832 static void idpf_vport_stop(struct idpf_vport *vport) 833 { 834 struct idpf_netdev_priv *np = netdev_priv(vport->netdev); 835 836 if (np->state <= __IDPF_VPORT_DOWN) 837 return; 838 839 netif_carrier_off(vport->netdev); 840 netif_tx_disable(vport->netdev); 841 842 idpf_send_disable_vport_msg(vport); 843 idpf_send_disable_queues_msg(vport); 844 idpf_send_map_unmap_queue_vector_msg(vport, false); 845 /* Normally we ask for queues in create_vport, but if the number of 846 * initially requested queues have changed, for example via ethtool 847 * set channels, we do delete queues and then add the queues back 848 * instead of deleting and reallocating the vport. 849 */ 850 if (test_and_clear_bit(IDPF_VPORT_DEL_QUEUES, vport->flags)) 851 idpf_send_delete_queues_msg(vport); 852 853 idpf_remove_features(vport); 854 855 vport->link_up = false; 856 idpf_vport_intr_deinit(vport); 857 idpf_vport_queues_rel(vport); 858 idpf_vport_intr_rel(vport); 859 np->state = __IDPF_VPORT_DOWN; 860 } 861 862 /** 863 * idpf_stop - Disables a network interface 864 * @netdev: network interface device structure 865 * 866 * The stop entry point is called when an interface is de-activated by the OS, 867 * and the netdevice enters the DOWN state. The hardware is still under the 868 * driver's control, but the netdev interface is disabled. 869 * 870 * Returns success only - not allowed to fail 871 */ 872 static int idpf_stop(struct net_device *netdev) 873 { 874 struct idpf_netdev_priv *np = netdev_priv(netdev); 875 struct idpf_vport *vport; 876 877 if (test_bit(IDPF_REMOVE_IN_PROG, np->adapter->flags)) 878 return 0; 879 880 idpf_vport_ctrl_lock(netdev); 881 vport = idpf_netdev_to_vport(netdev); 882 883 idpf_vport_stop(vport); 884 885 idpf_vport_ctrl_unlock(netdev); 886 887 return 0; 888 } 889 890 /** 891 * idpf_decfg_netdev - Unregister the netdev 892 * @vport: vport for which netdev to be unregistered 893 */ 894 static void idpf_decfg_netdev(struct idpf_vport *vport) 895 { 896 struct idpf_adapter *adapter = vport->adapter; 897 u16 idx = vport->idx; 898 899 kfree(vport->rx_ptype_lkup); 900 vport->rx_ptype_lkup = NULL; 901 902 if (test_and_clear_bit(IDPF_VPORT_REG_NETDEV, 903 adapter->vport_config[idx]->flags)) { 904 unregister_netdev(vport->netdev); 905 free_netdev(vport->netdev); 906 } 907 vport->netdev = NULL; 908 909 adapter->netdevs[idx] = NULL; 910 } 911 912 /** 913 * idpf_vport_rel - Delete a vport and free its resources 914 * @vport: the vport being removed 915 */ 916 static void idpf_vport_rel(struct idpf_vport *vport) 917 { 918 struct idpf_adapter *adapter = vport->adapter; 919 struct idpf_vport_config *vport_config; 920 struct idpf_vector_info vec_info; 921 struct idpf_rss_data *rss_data; 922 struct idpf_vport_max_q max_q; 923 u16 idx = vport->idx; 924 925 vport_config = adapter->vport_config[vport->idx]; 926 idpf_deinit_rss(vport); 927 rss_data = &vport_config->user_config.rss_data; 928 kfree(rss_data->rss_key); 929 rss_data->rss_key = NULL; 930 931 idpf_send_destroy_vport_msg(vport); 932 933 /* Release all max queues allocated to the adapter's pool */ 934 max_q.max_rxq = vport_config->max_q.max_rxq; 935 max_q.max_txq = vport_config->max_q.max_txq; 936 max_q.max_bufq = vport_config->max_q.max_bufq; 937 max_q.max_complq = vport_config->max_q.max_complq; 938 idpf_vport_dealloc_max_qs(adapter, &max_q); 939 940 /* Release all the allocated vectors on the stack */ 941 vec_info.num_req_vecs = 0; 942 vec_info.num_curr_vecs = vport->num_q_vectors; 943 vec_info.default_vport = vport->default_vport; 944 945 idpf_req_rel_vector_indexes(adapter, vport->q_vector_idxs, &vec_info); 946 947 kfree(vport->q_vector_idxs); 948 vport->q_vector_idxs = NULL; 949 950 kfree(adapter->vport_params_recvd[idx]); 951 adapter->vport_params_recvd[idx] = NULL; 952 kfree(adapter->vport_params_reqd[idx]); 953 adapter->vport_params_reqd[idx] = NULL; 954 if (adapter->vport_config[idx]) { 955 kfree(adapter->vport_config[idx]->req_qs_chunks); 956 adapter->vport_config[idx]->req_qs_chunks = NULL; 957 } 958 kfree(vport); 959 adapter->num_alloc_vports--; 960 } 961 962 /** 963 * idpf_vport_dealloc - cleanup and release a given vport 964 * @vport: pointer to idpf vport structure 965 * 966 * returns nothing 967 */ 968 static void idpf_vport_dealloc(struct idpf_vport *vport) 969 { 970 struct idpf_adapter *adapter = vport->adapter; 971 unsigned int i = vport->idx; 972 973 idpf_deinit_mac_addr(vport); 974 idpf_vport_stop(vport); 975 976 if (!test_bit(IDPF_HR_RESET_IN_PROG, adapter->flags)) 977 idpf_decfg_netdev(vport); 978 if (test_bit(IDPF_REMOVE_IN_PROG, adapter->flags)) 979 idpf_del_all_mac_filters(vport); 980 981 if (adapter->netdevs[i]) { 982 struct idpf_netdev_priv *np = netdev_priv(adapter->netdevs[i]); 983 984 np->vport = NULL; 985 } 986 987 idpf_vport_rel(vport); 988 989 adapter->vports[i] = NULL; 990 adapter->next_vport = idpf_get_free_slot(adapter); 991 } 992 993 /** 994 * idpf_is_hsplit_supported - check whether the header split is supported 995 * @vport: virtual port to check the capability for 996 * 997 * Return: true if it's supported by the HW/FW, false if not. 998 */ 999 static bool idpf_is_hsplit_supported(const struct idpf_vport *vport) 1000 { 1001 return idpf_is_queue_model_split(vport->rxq_model) && 1002 idpf_is_cap_ena_all(vport->adapter, IDPF_HSPLIT_CAPS, 1003 IDPF_CAP_HSPLIT); 1004 } 1005 1006 /** 1007 * idpf_vport_get_hsplit - get the current header split feature state 1008 * @vport: virtual port to query the state for 1009 * 1010 * Return: ``ETHTOOL_TCP_DATA_SPLIT_UNKNOWN`` if not supported, 1011 * ``ETHTOOL_TCP_DATA_SPLIT_DISABLED`` if disabled, 1012 * ``ETHTOOL_TCP_DATA_SPLIT_ENABLED`` if active. 1013 */ 1014 u8 idpf_vport_get_hsplit(const struct idpf_vport *vport) 1015 { 1016 const struct idpf_vport_user_config_data *config; 1017 1018 if (!idpf_is_hsplit_supported(vport)) 1019 return ETHTOOL_TCP_DATA_SPLIT_UNKNOWN; 1020 1021 config = &vport->adapter->vport_config[vport->idx]->user_config; 1022 1023 return test_bit(__IDPF_USER_FLAG_HSPLIT, config->user_flags) ? 1024 ETHTOOL_TCP_DATA_SPLIT_ENABLED : 1025 ETHTOOL_TCP_DATA_SPLIT_DISABLED; 1026 } 1027 1028 /** 1029 * idpf_vport_set_hsplit - enable or disable header split on a given vport 1030 * @vport: virtual port to configure 1031 * @val: Ethtool flag controlling the header split state 1032 * 1033 * Return: true on success, false if not supported by the HW. 1034 */ 1035 bool idpf_vport_set_hsplit(const struct idpf_vport *vport, u8 val) 1036 { 1037 struct idpf_vport_user_config_data *config; 1038 1039 if (!idpf_is_hsplit_supported(vport)) 1040 return val == ETHTOOL_TCP_DATA_SPLIT_UNKNOWN; 1041 1042 config = &vport->adapter->vport_config[vport->idx]->user_config; 1043 1044 switch (val) { 1045 case ETHTOOL_TCP_DATA_SPLIT_UNKNOWN: 1046 /* Default is to enable */ 1047 case ETHTOOL_TCP_DATA_SPLIT_ENABLED: 1048 __set_bit(__IDPF_USER_FLAG_HSPLIT, config->user_flags); 1049 return true; 1050 case ETHTOOL_TCP_DATA_SPLIT_DISABLED: 1051 __clear_bit(__IDPF_USER_FLAG_HSPLIT, config->user_flags); 1052 return true; 1053 default: 1054 return false; 1055 } 1056 } 1057 1058 /** 1059 * idpf_vport_alloc - Allocates the next available struct vport in the adapter 1060 * @adapter: board private structure 1061 * @max_q: vport max queue info 1062 * 1063 * returns a pointer to a vport on success, NULL on failure. 1064 */ 1065 static struct idpf_vport *idpf_vport_alloc(struct idpf_adapter *adapter, 1066 struct idpf_vport_max_q *max_q) 1067 { 1068 struct idpf_rss_data *rss_data; 1069 u16 idx = adapter->next_vport; 1070 struct idpf_vport *vport; 1071 u16 num_max_q; 1072 1073 if (idx == IDPF_NO_FREE_SLOT) 1074 return NULL; 1075 1076 vport = kzalloc(sizeof(*vport), GFP_KERNEL); 1077 if (!vport) 1078 return vport; 1079 1080 if (!adapter->vport_config[idx]) { 1081 struct idpf_vport_config *vport_config; 1082 1083 vport_config = kzalloc(sizeof(*vport_config), GFP_KERNEL); 1084 if (!vport_config) { 1085 kfree(vport); 1086 1087 return NULL; 1088 } 1089 1090 adapter->vport_config[idx] = vport_config; 1091 } 1092 1093 vport->idx = idx; 1094 vport->adapter = adapter; 1095 vport->compln_clean_budget = IDPF_TX_COMPLQ_CLEAN_BUDGET; 1096 vport->default_vport = adapter->num_alloc_vports < 1097 idpf_get_default_vports(adapter); 1098 1099 num_max_q = max(max_q->max_txq, max_q->max_rxq); 1100 vport->q_vector_idxs = kcalloc(num_max_q, sizeof(u16), GFP_KERNEL); 1101 if (!vport->q_vector_idxs) 1102 goto free_vport; 1103 1104 idpf_vport_init(vport, max_q); 1105 1106 /* This alloc is done separate from the LUT because it's not strictly 1107 * dependent on how many queues we have. If we change number of queues 1108 * and soft reset we'll need a new LUT but the key can remain the same 1109 * for as long as the vport exists. 1110 */ 1111 rss_data = &adapter->vport_config[idx]->user_config.rss_data; 1112 rss_data->rss_key = kzalloc(rss_data->rss_key_size, GFP_KERNEL); 1113 if (!rss_data->rss_key) 1114 goto free_vector_idxs; 1115 1116 /* Initialize default rss key */ 1117 netdev_rss_key_fill((void *)rss_data->rss_key, rss_data->rss_key_size); 1118 1119 /* fill vport slot in the adapter struct */ 1120 adapter->vports[idx] = vport; 1121 adapter->vport_ids[idx] = idpf_get_vport_id(vport); 1122 1123 adapter->num_alloc_vports++; 1124 /* prepare adapter->next_vport for next use */ 1125 adapter->next_vport = idpf_get_free_slot(adapter); 1126 1127 return vport; 1128 1129 free_vector_idxs: 1130 kfree(vport->q_vector_idxs); 1131 free_vport: 1132 kfree(vport); 1133 1134 return NULL; 1135 } 1136 1137 /** 1138 * idpf_get_stats64 - get statistics for network device structure 1139 * @netdev: network interface device structure 1140 * @stats: main device statistics structure 1141 */ 1142 static void idpf_get_stats64(struct net_device *netdev, 1143 struct rtnl_link_stats64 *stats) 1144 { 1145 struct idpf_netdev_priv *np = netdev_priv(netdev); 1146 1147 spin_lock_bh(&np->stats_lock); 1148 *stats = np->netstats; 1149 spin_unlock_bh(&np->stats_lock); 1150 } 1151 1152 /** 1153 * idpf_statistics_task - Delayed task to get statistics over mailbox 1154 * @work: work_struct handle to our data 1155 */ 1156 void idpf_statistics_task(struct work_struct *work) 1157 { 1158 struct idpf_adapter *adapter; 1159 int i; 1160 1161 adapter = container_of(work, struct idpf_adapter, stats_task.work); 1162 1163 for (i = 0; i < adapter->max_vports; i++) { 1164 struct idpf_vport *vport = adapter->vports[i]; 1165 1166 if (vport && !test_bit(IDPF_HR_RESET_IN_PROG, adapter->flags)) 1167 idpf_send_get_stats_msg(vport); 1168 } 1169 1170 queue_delayed_work(adapter->stats_wq, &adapter->stats_task, 1171 msecs_to_jiffies(10000)); 1172 } 1173 1174 /** 1175 * idpf_mbx_task - Delayed task to handle mailbox responses 1176 * @work: work_struct handle 1177 */ 1178 void idpf_mbx_task(struct work_struct *work) 1179 { 1180 struct idpf_adapter *adapter; 1181 1182 adapter = container_of(work, struct idpf_adapter, mbx_task.work); 1183 1184 if (test_bit(IDPF_MB_INTR_MODE, adapter->flags)) 1185 idpf_mb_irq_enable(adapter); 1186 else 1187 queue_delayed_work(adapter->mbx_wq, &adapter->mbx_task, 1188 msecs_to_jiffies(300)); 1189 1190 idpf_recv_mb_msg(adapter); 1191 } 1192 1193 /** 1194 * idpf_service_task - Delayed task for handling mailbox responses 1195 * @work: work_struct handle to our data 1196 * 1197 */ 1198 void idpf_service_task(struct work_struct *work) 1199 { 1200 struct idpf_adapter *adapter; 1201 1202 adapter = container_of(work, struct idpf_adapter, serv_task.work); 1203 1204 if (idpf_is_reset_detected(adapter) && 1205 !idpf_is_reset_in_prog(adapter) && 1206 !test_bit(IDPF_REMOVE_IN_PROG, adapter->flags)) { 1207 dev_info(&adapter->pdev->dev, "HW reset detected\n"); 1208 set_bit(IDPF_HR_FUNC_RESET, adapter->flags); 1209 queue_delayed_work(adapter->vc_event_wq, 1210 &adapter->vc_event_task, 1211 msecs_to_jiffies(10)); 1212 } 1213 1214 queue_delayed_work(adapter->serv_wq, &adapter->serv_task, 1215 msecs_to_jiffies(300)); 1216 } 1217 1218 /** 1219 * idpf_restore_features - Restore feature configs 1220 * @vport: virtual port structure 1221 */ 1222 static void idpf_restore_features(struct idpf_vport *vport) 1223 { 1224 struct idpf_adapter *adapter = vport->adapter; 1225 1226 if (idpf_is_cap_ena(adapter, IDPF_OTHER_CAPS, VIRTCHNL2_CAP_MACFILTER)) 1227 idpf_restore_mac_filters(vport); 1228 } 1229 1230 /** 1231 * idpf_set_real_num_queues - set number of queues for netdev 1232 * @vport: virtual port structure 1233 * 1234 * Returns 0 on success, negative on failure. 1235 */ 1236 static int idpf_set_real_num_queues(struct idpf_vport *vport) 1237 { 1238 int err; 1239 1240 err = netif_set_real_num_rx_queues(vport->netdev, vport->num_rxq); 1241 if (err) 1242 return err; 1243 1244 return netif_set_real_num_tx_queues(vport->netdev, vport->num_txq); 1245 } 1246 1247 /** 1248 * idpf_up_complete - Complete interface up sequence 1249 * @vport: virtual port structure 1250 * 1251 * Returns 0 on success, negative on failure. 1252 */ 1253 static int idpf_up_complete(struct idpf_vport *vport) 1254 { 1255 struct idpf_netdev_priv *np = netdev_priv(vport->netdev); 1256 1257 if (vport->link_up && !netif_carrier_ok(vport->netdev)) { 1258 netif_carrier_on(vport->netdev); 1259 netif_tx_start_all_queues(vport->netdev); 1260 } 1261 1262 np->state = __IDPF_VPORT_UP; 1263 1264 return 0; 1265 } 1266 1267 /** 1268 * idpf_rx_init_buf_tail - Write initial buffer ring tail value 1269 * @vport: virtual port struct 1270 */ 1271 static void idpf_rx_init_buf_tail(struct idpf_vport *vport) 1272 { 1273 int i, j; 1274 1275 for (i = 0; i < vport->num_rxq_grp; i++) { 1276 struct idpf_rxq_group *grp = &vport->rxq_grps[i]; 1277 1278 if (idpf_is_queue_model_split(vport->rxq_model)) { 1279 for (j = 0; j < vport->num_bufqs_per_qgrp; j++) { 1280 const struct idpf_buf_queue *q = 1281 &grp->splitq.bufq_sets[j].bufq; 1282 1283 writel(q->next_to_alloc, q->tail); 1284 } 1285 } else { 1286 for (j = 0; j < grp->singleq.num_rxq; j++) { 1287 const struct idpf_rx_queue *q = 1288 grp->singleq.rxqs[j]; 1289 1290 writel(q->next_to_alloc, q->tail); 1291 } 1292 } 1293 } 1294 } 1295 1296 /** 1297 * idpf_vport_open - Bring up a vport 1298 * @vport: vport to bring up 1299 */ 1300 static int idpf_vport_open(struct idpf_vport *vport) 1301 { 1302 struct idpf_netdev_priv *np = netdev_priv(vport->netdev); 1303 struct idpf_adapter *adapter = vport->adapter; 1304 struct idpf_vport_config *vport_config; 1305 int err; 1306 1307 if (np->state != __IDPF_VPORT_DOWN) 1308 return -EBUSY; 1309 1310 /* we do not allow interface up just yet */ 1311 netif_carrier_off(vport->netdev); 1312 1313 err = idpf_vport_intr_alloc(vport); 1314 if (err) { 1315 dev_err(&adapter->pdev->dev, "Failed to allocate interrupts for vport %u: %d\n", 1316 vport->vport_id, err); 1317 return err; 1318 } 1319 1320 err = idpf_vport_queues_alloc(vport); 1321 if (err) 1322 goto intr_rel; 1323 1324 err = idpf_vport_queue_ids_init(vport); 1325 if (err) { 1326 dev_err(&adapter->pdev->dev, "Failed to initialize queue ids for vport %u: %d\n", 1327 vport->vport_id, err); 1328 goto queues_rel; 1329 } 1330 1331 err = idpf_vport_intr_init(vport); 1332 if (err) { 1333 dev_err(&adapter->pdev->dev, "Failed to initialize interrupts for vport %u: %d\n", 1334 vport->vport_id, err); 1335 goto queues_rel; 1336 } 1337 1338 err = idpf_rx_bufs_init_all(vport); 1339 if (err) { 1340 dev_err(&adapter->pdev->dev, "Failed to initialize RX buffers for vport %u: %d\n", 1341 vport->vport_id, err); 1342 goto queues_rel; 1343 } 1344 1345 err = idpf_queue_reg_init(vport); 1346 if (err) { 1347 dev_err(&adapter->pdev->dev, "Failed to initialize queue registers for vport %u: %d\n", 1348 vport->vport_id, err); 1349 goto queues_rel; 1350 } 1351 1352 idpf_rx_init_buf_tail(vport); 1353 idpf_vport_intr_ena(vport); 1354 1355 err = idpf_send_config_queues_msg(vport); 1356 if (err) { 1357 dev_err(&adapter->pdev->dev, "Failed to configure queues for vport %u, %d\n", 1358 vport->vport_id, err); 1359 goto intr_deinit; 1360 } 1361 1362 err = idpf_send_map_unmap_queue_vector_msg(vport, true); 1363 if (err) { 1364 dev_err(&adapter->pdev->dev, "Failed to map queue vectors for vport %u: %d\n", 1365 vport->vport_id, err); 1366 goto intr_deinit; 1367 } 1368 1369 err = idpf_send_enable_queues_msg(vport); 1370 if (err) { 1371 dev_err(&adapter->pdev->dev, "Failed to enable queues for vport %u: %d\n", 1372 vport->vport_id, err); 1373 goto unmap_queue_vectors; 1374 } 1375 1376 err = idpf_send_enable_vport_msg(vport); 1377 if (err) { 1378 dev_err(&adapter->pdev->dev, "Failed to enable vport %u: %d\n", 1379 vport->vport_id, err); 1380 err = -EAGAIN; 1381 goto disable_queues; 1382 } 1383 1384 idpf_restore_features(vport); 1385 1386 vport_config = adapter->vport_config[vport->idx]; 1387 if (vport_config->user_config.rss_data.rss_lut) 1388 err = idpf_config_rss(vport); 1389 else 1390 err = idpf_init_rss(vport); 1391 if (err) { 1392 dev_err(&adapter->pdev->dev, "Failed to initialize RSS for vport %u: %d\n", 1393 vport->vport_id, err); 1394 goto disable_vport; 1395 } 1396 1397 err = idpf_up_complete(vport); 1398 if (err) { 1399 dev_err(&adapter->pdev->dev, "Failed to complete interface up for vport %u: %d\n", 1400 vport->vport_id, err); 1401 goto deinit_rss; 1402 } 1403 1404 return 0; 1405 1406 deinit_rss: 1407 idpf_deinit_rss(vport); 1408 disable_vport: 1409 idpf_send_disable_vport_msg(vport); 1410 disable_queues: 1411 idpf_send_disable_queues_msg(vport); 1412 unmap_queue_vectors: 1413 idpf_send_map_unmap_queue_vector_msg(vport, false); 1414 intr_deinit: 1415 idpf_vport_intr_deinit(vport); 1416 queues_rel: 1417 idpf_vport_queues_rel(vport); 1418 intr_rel: 1419 idpf_vport_intr_rel(vport); 1420 1421 return err; 1422 } 1423 1424 /** 1425 * idpf_init_task - Delayed initialization task 1426 * @work: work_struct handle to our data 1427 * 1428 * Init task finishes up pending work started in probe. Due to the asynchronous 1429 * nature in which the device communicates with hardware, we may have to wait 1430 * several milliseconds to get a response. Instead of busy polling in probe, 1431 * pulling it out into a delayed work task prevents us from bogging down the 1432 * whole system waiting for a response from hardware. 1433 */ 1434 void idpf_init_task(struct work_struct *work) 1435 { 1436 struct idpf_vport_config *vport_config; 1437 struct idpf_vport_max_q max_q; 1438 struct idpf_adapter *adapter; 1439 struct idpf_netdev_priv *np; 1440 struct idpf_vport *vport; 1441 u16 num_default_vports; 1442 struct pci_dev *pdev; 1443 bool default_vport; 1444 int index, err; 1445 1446 adapter = container_of(work, struct idpf_adapter, init_task.work); 1447 1448 num_default_vports = idpf_get_default_vports(adapter); 1449 if (adapter->num_alloc_vports < num_default_vports) 1450 default_vport = true; 1451 else 1452 default_vport = false; 1453 1454 err = idpf_vport_alloc_max_qs(adapter, &max_q); 1455 if (err) 1456 goto unwind_vports; 1457 1458 err = idpf_send_create_vport_msg(adapter, &max_q); 1459 if (err) { 1460 idpf_vport_dealloc_max_qs(adapter, &max_q); 1461 goto unwind_vports; 1462 } 1463 1464 pdev = adapter->pdev; 1465 vport = idpf_vport_alloc(adapter, &max_q); 1466 if (!vport) { 1467 err = -EFAULT; 1468 dev_err(&pdev->dev, "failed to allocate vport: %d\n", 1469 err); 1470 idpf_vport_dealloc_max_qs(adapter, &max_q); 1471 goto unwind_vports; 1472 } 1473 1474 index = vport->idx; 1475 vport_config = adapter->vport_config[index]; 1476 1477 init_waitqueue_head(&vport->sw_marker_wq); 1478 1479 spin_lock_init(&vport_config->mac_filter_list_lock); 1480 1481 INIT_LIST_HEAD(&vport_config->user_config.mac_filter_list); 1482 1483 err = idpf_check_supported_desc_ids(vport); 1484 if (err) { 1485 dev_err(&pdev->dev, "failed to get required descriptor ids\n"); 1486 goto cfg_netdev_err; 1487 } 1488 1489 if (idpf_cfg_netdev(vport)) 1490 goto cfg_netdev_err; 1491 1492 err = idpf_send_get_rx_ptype_msg(vport); 1493 if (err) 1494 goto handle_err; 1495 1496 /* Once state is put into DOWN, driver is ready for dev_open */ 1497 np = netdev_priv(vport->netdev); 1498 np->state = __IDPF_VPORT_DOWN; 1499 if (test_and_clear_bit(IDPF_VPORT_UP_REQUESTED, vport_config->flags)) 1500 idpf_vport_open(vport); 1501 1502 /* Spawn and return 'idpf_init_task' work queue until all the 1503 * default vports are created 1504 */ 1505 if (adapter->num_alloc_vports < num_default_vports) { 1506 queue_delayed_work(adapter->init_wq, &adapter->init_task, 1507 msecs_to_jiffies(5 * (adapter->pdev->devfn & 0x07))); 1508 1509 return; 1510 } 1511 1512 for (index = 0; index < adapter->max_vports; index++) { 1513 struct net_device *netdev = adapter->netdevs[index]; 1514 struct idpf_vport_config *vport_config; 1515 1516 vport_config = adapter->vport_config[index]; 1517 1518 if (!netdev || 1519 test_bit(IDPF_VPORT_REG_NETDEV, vport_config->flags)) 1520 continue; 1521 1522 err = register_netdev(netdev); 1523 if (err) { 1524 dev_err(&pdev->dev, "failed to register netdev for vport %d: %pe\n", 1525 index, ERR_PTR(err)); 1526 continue; 1527 } 1528 set_bit(IDPF_VPORT_REG_NETDEV, vport_config->flags); 1529 } 1530 1531 /* As all the required vports are created, clear the reset flag 1532 * unconditionally here in case we were in reset and the link was down. 1533 */ 1534 clear_bit(IDPF_HR_RESET_IN_PROG, adapter->flags); 1535 /* Start the statistics task now */ 1536 queue_delayed_work(adapter->stats_wq, &adapter->stats_task, 1537 msecs_to_jiffies(10 * (pdev->devfn & 0x07))); 1538 1539 return; 1540 1541 handle_err: 1542 idpf_decfg_netdev(vport); 1543 cfg_netdev_err: 1544 idpf_vport_rel(vport); 1545 adapter->vports[index] = NULL; 1546 unwind_vports: 1547 if (default_vport) { 1548 for (index = 0; index < adapter->max_vports; index++) { 1549 if (adapter->vports[index]) 1550 idpf_vport_dealloc(adapter->vports[index]); 1551 } 1552 } 1553 clear_bit(IDPF_HR_RESET_IN_PROG, adapter->flags); 1554 } 1555 1556 /** 1557 * idpf_sriov_ena - Enable or change number of VFs 1558 * @adapter: private data struct 1559 * @num_vfs: number of VFs to allocate 1560 */ 1561 static int idpf_sriov_ena(struct idpf_adapter *adapter, int num_vfs) 1562 { 1563 struct device *dev = &adapter->pdev->dev; 1564 int err; 1565 1566 err = idpf_send_set_sriov_vfs_msg(adapter, num_vfs); 1567 if (err) { 1568 dev_err(dev, "Failed to allocate VFs: %d\n", err); 1569 1570 return err; 1571 } 1572 1573 err = pci_enable_sriov(adapter->pdev, num_vfs); 1574 if (err) { 1575 idpf_send_set_sriov_vfs_msg(adapter, 0); 1576 dev_err(dev, "Failed to enable SR-IOV: %d\n", err); 1577 1578 return err; 1579 } 1580 1581 adapter->num_vfs = num_vfs; 1582 1583 return num_vfs; 1584 } 1585 1586 /** 1587 * idpf_sriov_configure - Configure the requested VFs 1588 * @pdev: pointer to a pci_dev structure 1589 * @num_vfs: number of vfs to allocate 1590 * 1591 * Enable or change the number of VFs. Called when the user updates the number 1592 * of VFs in sysfs. 1593 **/ 1594 int idpf_sriov_configure(struct pci_dev *pdev, int num_vfs) 1595 { 1596 struct idpf_adapter *adapter = pci_get_drvdata(pdev); 1597 1598 if (!idpf_is_cap_ena(adapter, IDPF_OTHER_CAPS, VIRTCHNL2_CAP_SRIOV)) { 1599 dev_info(&pdev->dev, "SR-IOV is not supported on this device\n"); 1600 1601 return -EOPNOTSUPP; 1602 } 1603 1604 if (num_vfs) 1605 return idpf_sriov_ena(adapter, num_vfs); 1606 1607 if (pci_vfs_assigned(pdev)) { 1608 dev_warn(&pdev->dev, "Unable to free VFs because some are assigned to VMs\n"); 1609 1610 return -EBUSY; 1611 } 1612 1613 pci_disable_sriov(adapter->pdev); 1614 idpf_send_set_sriov_vfs_msg(adapter, 0); 1615 adapter->num_vfs = 0; 1616 1617 return 0; 1618 } 1619 1620 /** 1621 * idpf_deinit_task - Device deinit routine 1622 * @adapter: Driver specific private structure 1623 * 1624 * Extended remove logic which will be used for 1625 * hard reset as well 1626 */ 1627 void idpf_deinit_task(struct idpf_adapter *adapter) 1628 { 1629 unsigned int i; 1630 1631 /* Wait until the init_task is done else this thread might release 1632 * the resources first and the other thread might end up in a bad state 1633 */ 1634 cancel_delayed_work_sync(&adapter->init_task); 1635 1636 if (!adapter->vports) 1637 return; 1638 1639 cancel_delayed_work_sync(&adapter->stats_task); 1640 1641 for (i = 0; i < adapter->max_vports; i++) { 1642 if (adapter->vports[i]) 1643 idpf_vport_dealloc(adapter->vports[i]); 1644 } 1645 } 1646 1647 /** 1648 * idpf_check_reset_complete - check that reset is complete 1649 * @hw: pointer to hw struct 1650 * @reset_reg: struct with reset registers 1651 * 1652 * Returns 0 if device is ready to use, or -EBUSY if it's in reset. 1653 **/ 1654 static int idpf_check_reset_complete(struct idpf_hw *hw, 1655 struct idpf_reset_reg *reset_reg) 1656 { 1657 struct idpf_adapter *adapter = hw->back; 1658 int i; 1659 1660 for (i = 0; i < 2000; i++) { 1661 u32 reg_val = readl(reset_reg->rstat); 1662 1663 /* 0xFFFFFFFF might be read if other side hasn't cleared the 1664 * register for us yet and 0xFFFFFFFF is not a valid value for 1665 * the register, so treat that as invalid. 1666 */ 1667 if (reg_val != 0xFFFFFFFF && (reg_val & reset_reg->rstat_m)) 1668 return 0; 1669 1670 usleep_range(5000, 10000); 1671 } 1672 1673 dev_warn(&adapter->pdev->dev, "Device reset timeout!\n"); 1674 /* Clear the reset flag unconditionally here since the reset 1675 * technically isn't in progress anymore from the driver's perspective 1676 */ 1677 clear_bit(IDPF_HR_RESET_IN_PROG, adapter->flags); 1678 1679 return -EBUSY; 1680 } 1681 1682 /** 1683 * idpf_set_vport_state - Set the vport state to be after the reset 1684 * @adapter: Driver specific private structure 1685 */ 1686 static void idpf_set_vport_state(struct idpf_adapter *adapter) 1687 { 1688 u16 i; 1689 1690 for (i = 0; i < adapter->max_vports; i++) { 1691 struct idpf_netdev_priv *np; 1692 1693 if (!adapter->netdevs[i]) 1694 continue; 1695 1696 np = netdev_priv(adapter->netdevs[i]); 1697 if (np->state == __IDPF_VPORT_UP) 1698 set_bit(IDPF_VPORT_UP_REQUESTED, 1699 adapter->vport_config[i]->flags); 1700 } 1701 } 1702 1703 /** 1704 * idpf_init_hard_reset - Initiate a hardware reset 1705 * @adapter: Driver specific private structure 1706 * 1707 * Deallocate the vports and all the resources associated with them and 1708 * reallocate. Also reinitialize the mailbox. Return 0 on success, 1709 * negative on failure. 1710 */ 1711 static int idpf_init_hard_reset(struct idpf_adapter *adapter) 1712 { 1713 struct idpf_reg_ops *reg_ops = &adapter->dev_ops.reg_ops; 1714 struct device *dev = &adapter->pdev->dev; 1715 struct net_device *netdev; 1716 int err; 1717 u16 i; 1718 1719 mutex_lock(&adapter->vport_ctrl_lock); 1720 1721 dev_info(dev, "Device HW Reset initiated\n"); 1722 1723 /* Avoid TX hangs on reset */ 1724 for (i = 0; i < adapter->max_vports; i++) { 1725 netdev = adapter->netdevs[i]; 1726 if (!netdev) 1727 continue; 1728 1729 netif_carrier_off(netdev); 1730 netif_tx_disable(netdev); 1731 } 1732 1733 /* Prepare for reset */ 1734 if (test_and_clear_bit(IDPF_HR_DRV_LOAD, adapter->flags)) { 1735 reg_ops->trigger_reset(adapter, IDPF_HR_DRV_LOAD); 1736 } else if (test_and_clear_bit(IDPF_HR_FUNC_RESET, adapter->flags)) { 1737 bool is_reset = idpf_is_reset_detected(adapter); 1738 1739 idpf_set_vport_state(adapter); 1740 idpf_vc_core_deinit(adapter); 1741 if (!is_reset) 1742 reg_ops->trigger_reset(adapter, IDPF_HR_FUNC_RESET); 1743 idpf_deinit_dflt_mbx(adapter); 1744 } else { 1745 dev_err(dev, "Unhandled hard reset cause\n"); 1746 err = -EBADRQC; 1747 goto unlock_mutex; 1748 } 1749 1750 /* Wait for reset to complete */ 1751 err = idpf_check_reset_complete(&adapter->hw, &adapter->reset_reg); 1752 if (err) { 1753 dev_err(dev, "The driver was unable to contact the device's firmware. Check that the FW is running. Driver state= 0x%x\n", 1754 adapter->state); 1755 goto unlock_mutex; 1756 } 1757 1758 /* Reset is complete and so start building the driver resources again */ 1759 err = idpf_init_dflt_mbx(adapter); 1760 if (err) { 1761 dev_err(dev, "Failed to initialize default mailbox: %d\n", err); 1762 goto unlock_mutex; 1763 } 1764 1765 queue_delayed_work(adapter->mbx_wq, &adapter->mbx_task, 0); 1766 1767 /* Initialize the state machine, also allocate memory and request 1768 * resources 1769 */ 1770 err = idpf_vc_core_init(adapter); 1771 if (err) { 1772 cancel_delayed_work_sync(&adapter->mbx_task); 1773 idpf_deinit_dflt_mbx(adapter); 1774 goto unlock_mutex; 1775 } 1776 1777 /* Wait till all the vports are initialized to release the reset lock, 1778 * else user space callbacks may access uninitialized vports 1779 */ 1780 while (test_bit(IDPF_HR_RESET_IN_PROG, adapter->flags)) 1781 msleep(100); 1782 1783 unlock_mutex: 1784 mutex_unlock(&adapter->vport_ctrl_lock); 1785 1786 return err; 1787 } 1788 1789 /** 1790 * idpf_vc_event_task - Handle virtchannel event logic 1791 * @work: work queue struct 1792 */ 1793 void idpf_vc_event_task(struct work_struct *work) 1794 { 1795 struct idpf_adapter *adapter; 1796 1797 adapter = container_of(work, struct idpf_adapter, vc_event_task.work); 1798 1799 if (test_bit(IDPF_REMOVE_IN_PROG, adapter->flags)) 1800 return; 1801 1802 if (test_bit(IDPF_HR_FUNC_RESET, adapter->flags) || 1803 test_bit(IDPF_HR_DRV_LOAD, adapter->flags)) { 1804 set_bit(IDPF_HR_RESET_IN_PROG, adapter->flags); 1805 idpf_init_hard_reset(adapter); 1806 } 1807 } 1808 1809 /** 1810 * idpf_initiate_soft_reset - Initiate a software reset 1811 * @vport: virtual port data struct 1812 * @reset_cause: reason for the soft reset 1813 * 1814 * Soft reset only reallocs vport queue resources. Returns 0 on success, 1815 * negative on failure. 1816 */ 1817 int idpf_initiate_soft_reset(struct idpf_vport *vport, 1818 enum idpf_vport_reset_cause reset_cause) 1819 { 1820 struct idpf_netdev_priv *np = netdev_priv(vport->netdev); 1821 enum idpf_vport_state current_state = np->state; 1822 struct idpf_adapter *adapter = vport->adapter; 1823 struct idpf_vport *new_vport; 1824 int err; 1825 1826 /* If the system is low on memory, we can end up in bad state if we 1827 * free all the memory for queue resources and try to allocate them 1828 * again. Instead, we can pre-allocate the new resources before doing 1829 * anything and bailing if the alloc fails. 1830 * 1831 * Make a clone of the existing vport to mimic its current 1832 * configuration, then modify the new structure with any requested 1833 * changes. Once the allocation of the new resources is done, stop the 1834 * existing vport and copy the configuration to the main vport. If an 1835 * error occurred, the existing vport will be untouched. 1836 * 1837 */ 1838 new_vport = kzalloc(sizeof(*vport), GFP_KERNEL); 1839 if (!new_vport) 1840 return -ENOMEM; 1841 1842 /* This purposely avoids copying the end of the struct because it 1843 * contains wait_queues and mutexes and other stuff we don't want to 1844 * mess with. Nothing below should use those variables from new_vport 1845 * and should instead always refer to them in vport if they need to. 1846 */ 1847 memcpy(new_vport, vport, offsetof(struct idpf_vport, link_up)); 1848 1849 /* Adjust resource parameters prior to reallocating resources */ 1850 switch (reset_cause) { 1851 case IDPF_SR_Q_CHANGE: 1852 err = idpf_vport_adjust_qs(new_vport); 1853 if (err) 1854 goto free_vport; 1855 break; 1856 case IDPF_SR_Q_DESC_CHANGE: 1857 /* Update queue parameters before allocating resources */ 1858 idpf_vport_calc_num_q_desc(new_vport); 1859 break; 1860 case IDPF_SR_MTU_CHANGE: 1861 case IDPF_SR_RSC_CHANGE: 1862 break; 1863 default: 1864 dev_err(&adapter->pdev->dev, "Unhandled soft reset cause\n"); 1865 err = -EINVAL; 1866 goto free_vport; 1867 } 1868 1869 if (current_state <= __IDPF_VPORT_DOWN) { 1870 idpf_send_delete_queues_msg(vport); 1871 } else { 1872 set_bit(IDPF_VPORT_DEL_QUEUES, vport->flags); 1873 idpf_vport_stop(vport); 1874 } 1875 1876 idpf_deinit_rss(vport); 1877 /* We're passing in vport here because we need its wait_queue 1878 * to send a message and it should be getting all the vport 1879 * config data out of the adapter but we need to be careful not 1880 * to add code to add_queues to change the vport config within 1881 * vport itself as it will be wiped with a memcpy later. 1882 */ 1883 err = idpf_send_add_queues_msg(vport, new_vport->num_txq, 1884 new_vport->num_complq, 1885 new_vport->num_rxq, 1886 new_vport->num_bufq); 1887 if (err) 1888 goto err_reset; 1889 1890 /* Same comment as above regarding avoiding copying the wait_queues and 1891 * mutexes applies here. We do not want to mess with those if possible. 1892 */ 1893 memcpy(vport, new_vport, offsetof(struct idpf_vport, link_up)); 1894 1895 if (reset_cause == IDPF_SR_Q_CHANGE) 1896 idpf_vport_alloc_vec_indexes(vport); 1897 1898 err = idpf_set_real_num_queues(vport); 1899 if (err) 1900 goto err_open; 1901 1902 if (current_state == __IDPF_VPORT_UP) 1903 err = idpf_vport_open(vport); 1904 1905 kfree(new_vport); 1906 1907 return err; 1908 1909 err_reset: 1910 idpf_send_add_queues_msg(vport, vport->num_txq, vport->num_complq, 1911 vport->num_rxq, vport->num_bufq); 1912 1913 err_open: 1914 if (current_state == __IDPF_VPORT_UP) 1915 idpf_vport_open(vport); 1916 1917 free_vport: 1918 kfree(new_vport); 1919 1920 return err; 1921 } 1922 1923 /** 1924 * idpf_addr_sync - Callback for dev_(mc|uc)_sync to add address 1925 * @netdev: the netdevice 1926 * @addr: address to add 1927 * 1928 * Called by __dev_(mc|uc)_sync when an address needs to be added. We call 1929 * __dev_(uc|mc)_sync from .set_rx_mode. Kernel takes addr_list_lock spinlock 1930 * meaning we cannot sleep in this context. Due to this, we have to add the 1931 * filter and send the virtchnl message asynchronously without waiting for the 1932 * response from the other side. We won't know whether or not the operation 1933 * actually succeeded until we get the message back. Returns 0 on success, 1934 * negative on failure. 1935 */ 1936 static int idpf_addr_sync(struct net_device *netdev, const u8 *addr) 1937 { 1938 struct idpf_netdev_priv *np = netdev_priv(netdev); 1939 1940 return idpf_add_mac_filter(np->vport, np, addr, true); 1941 } 1942 1943 /** 1944 * idpf_addr_unsync - Callback for dev_(mc|uc)_sync to remove address 1945 * @netdev: the netdevice 1946 * @addr: address to add 1947 * 1948 * Called by __dev_(mc|uc)_sync when an address needs to be added. We call 1949 * __dev_(uc|mc)_sync from .set_rx_mode. Kernel takes addr_list_lock spinlock 1950 * meaning we cannot sleep in this context. Due to this we have to delete the 1951 * filter and send the virtchnl message asynchronously without waiting for the 1952 * return from the other side. We won't know whether or not the operation 1953 * actually succeeded until we get the message back. Returns 0 on success, 1954 * negative on failure. 1955 */ 1956 static int idpf_addr_unsync(struct net_device *netdev, const u8 *addr) 1957 { 1958 struct idpf_netdev_priv *np = netdev_priv(netdev); 1959 1960 /* Under some circumstances, we might receive a request to delete 1961 * our own device address from our uc list. Because we store the 1962 * device address in the VSI's MAC filter list, we need to ignore 1963 * such requests and not delete our device address from this list. 1964 */ 1965 if (ether_addr_equal(addr, netdev->dev_addr)) 1966 return 0; 1967 1968 idpf_del_mac_filter(np->vport, np, addr, true); 1969 1970 return 0; 1971 } 1972 1973 /** 1974 * idpf_set_rx_mode - NDO callback to set the netdev filters 1975 * @netdev: network interface device structure 1976 * 1977 * Stack takes addr_list_lock spinlock before calling our .set_rx_mode. We 1978 * cannot sleep in this context. 1979 */ 1980 static void idpf_set_rx_mode(struct net_device *netdev) 1981 { 1982 struct idpf_netdev_priv *np = netdev_priv(netdev); 1983 struct idpf_vport_user_config_data *config_data; 1984 struct idpf_adapter *adapter; 1985 bool changed = false; 1986 struct device *dev; 1987 int err; 1988 1989 adapter = np->adapter; 1990 dev = &adapter->pdev->dev; 1991 1992 if (idpf_is_cap_ena(adapter, IDPF_OTHER_CAPS, VIRTCHNL2_CAP_MACFILTER)) { 1993 __dev_uc_sync(netdev, idpf_addr_sync, idpf_addr_unsync); 1994 __dev_mc_sync(netdev, idpf_addr_sync, idpf_addr_unsync); 1995 } 1996 1997 if (!idpf_is_cap_ena(adapter, IDPF_OTHER_CAPS, VIRTCHNL2_CAP_PROMISC)) 1998 return; 1999 2000 config_data = &adapter->vport_config[np->vport_idx]->user_config; 2001 /* IFF_PROMISC enables both unicast and multicast promiscuous, 2002 * while IFF_ALLMULTI only enables multicast such that: 2003 * 2004 * promisc + allmulti = unicast | multicast 2005 * promisc + !allmulti = unicast | multicast 2006 * !promisc + allmulti = multicast 2007 */ 2008 if ((netdev->flags & IFF_PROMISC) && 2009 !test_and_set_bit(__IDPF_PROMISC_UC, config_data->user_flags)) { 2010 changed = true; 2011 dev_info(&adapter->pdev->dev, "Entering promiscuous mode\n"); 2012 if (!test_and_set_bit(__IDPF_PROMISC_MC, adapter->flags)) 2013 dev_info(dev, "Entering multicast promiscuous mode\n"); 2014 } 2015 2016 if (!(netdev->flags & IFF_PROMISC) && 2017 test_and_clear_bit(__IDPF_PROMISC_UC, config_data->user_flags)) { 2018 changed = true; 2019 dev_info(dev, "Leaving promiscuous mode\n"); 2020 } 2021 2022 if (netdev->flags & IFF_ALLMULTI && 2023 !test_and_set_bit(__IDPF_PROMISC_MC, config_data->user_flags)) { 2024 changed = true; 2025 dev_info(dev, "Entering multicast promiscuous mode\n"); 2026 } 2027 2028 if (!(netdev->flags & (IFF_ALLMULTI | IFF_PROMISC)) && 2029 test_and_clear_bit(__IDPF_PROMISC_MC, config_data->user_flags)) { 2030 changed = true; 2031 dev_info(dev, "Leaving multicast promiscuous mode\n"); 2032 } 2033 2034 if (!changed) 2035 return; 2036 2037 err = idpf_set_promiscuous(adapter, config_data, np->vport_id); 2038 if (err) 2039 dev_err(dev, "Failed to set promiscuous mode: %d\n", err); 2040 } 2041 2042 /** 2043 * idpf_vport_manage_rss_lut - disable/enable RSS 2044 * @vport: the vport being changed 2045 * 2046 * In the event of disable request for RSS, this function will zero out RSS 2047 * LUT, while in the event of enable request for RSS, it will reconfigure RSS 2048 * LUT with the default LUT configuration. 2049 */ 2050 static int idpf_vport_manage_rss_lut(struct idpf_vport *vport) 2051 { 2052 bool ena = idpf_is_feature_ena(vport, NETIF_F_RXHASH); 2053 struct idpf_rss_data *rss_data; 2054 u16 idx = vport->idx; 2055 int lut_size; 2056 2057 rss_data = &vport->adapter->vport_config[idx]->user_config.rss_data; 2058 lut_size = rss_data->rss_lut_size * sizeof(u32); 2059 2060 if (ena) { 2061 /* This will contain the default or user configured LUT */ 2062 memcpy(rss_data->rss_lut, rss_data->cached_lut, lut_size); 2063 } else { 2064 /* Save a copy of the current LUT to be restored later if 2065 * requested. 2066 */ 2067 memcpy(rss_data->cached_lut, rss_data->rss_lut, lut_size); 2068 2069 /* Zero out the current LUT to disable */ 2070 memset(rss_data->rss_lut, 0, lut_size); 2071 } 2072 2073 return idpf_config_rss(vport); 2074 } 2075 2076 /** 2077 * idpf_set_features - set the netdev feature flags 2078 * @netdev: ptr to the netdev being adjusted 2079 * @features: the feature set that the stack is suggesting 2080 */ 2081 static int idpf_set_features(struct net_device *netdev, 2082 netdev_features_t features) 2083 { 2084 netdev_features_t changed = netdev->features ^ features; 2085 struct idpf_adapter *adapter; 2086 struct idpf_vport *vport; 2087 int err = 0; 2088 2089 idpf_vport_ctrl_lock(netdev); 2090 vport = idpf_netdev_to_vport(netdev); 2091 2092 adapter = vport->adapter; 2093 2094 if (idpf_is_reset_in_prog(adapter)) { 2095 dev_err(&adapter->pdev->dev, "Device is resetting, changing netdev features temporarily unavailable.\n"); 2096 err = -EBUSY; 2097 goto unlock_mutex; 2098 } 2099 2100 if (changed & NETIF_F_RXHASH) { 2101 netdev->features ^= NETIF_F_RXHASH; 2102 err = idpf_vport_manage_rss_lut(vport); 2103 if (err) 2104 goto unlock_mutex; 2105 } 2106 2107 if (changed & NETIF_F_GRO_HW) { 2108 netdev->features ^= NETIF_F_GRO_HW; 2109 err = idpf_initiate_soft_reset(vport, IDPF_SR_RSC_CHANGE); 2110 if (err) 2111 goto unlock_mutex; 2112 } 2113 2114 if (changed & NETIF_F_LOOPBACK) { 2115 netdev->features ^= NETIF_F_LOOPBACK; 2116 err = idpf_send_ena_dis_loopback_msg(vport); 2117 } 2118 2119 unlock_mutex: 2120 idpf_vport_ctrl_unlock(netdev); 2121 2122 return err; 2123 } 2124 2125 /** 2126 * idpf_open - Called when a network interface becomes active 2127 * @netdev: network interface device structure 2128 * 2129 * The open entry point is called when a network interface is made 2130 * active by the system (IFF_UP). At this point all resources needed 2131 * for transmit and receive operations are allocated, the interrupt 2132 * handler is registered with the OS, the netdev watchdog is enabled, 2133 * and the stack is notified that the interface is ready. 2134 * 2135 * Returns 0 on success, negative value on failure 2136 */ 2137 static int idpf_open(struct net_device *netdev) 2138 { 2139 struct idpf_vport *vport; 2140 int err; 2141 2142 idpf_vport_ctrl_lock(netdev); 2143 vport = idpf_netdev_to_vport(netdev); 2144 2145 err = idpf_set_real_num_queues(vport); 2146 if (err) 2147 goto unlock; 2148 2149 err = idpf_vport_open(vport); 2150 2151 unlock: 2152 idpf_vport_ctrl_unlock(netdev); 2153 2154 return err; 2155 } 2156 2157 /** 2158 * idpf_change_mtu - NDO callback to change the MTU 2159 * @netdev: network interface device structure 2160 * @new_mtu: new value for maximum frame size 2161 * 2162 * Returns 0 on success, negative on failure 2163 */ 2164 static int idpf_change_mtu(struct net_device *netdev, int new_mtu) 2165 { 2166 struct idpf_vport *vport; 2167 int err; 2168 2169 idpf_vport_ctrl_lock(netdev); 2170 vport = idpf_netdev_to_vport(netdev); 2171 2172 WRITE_ONCE(netdev->mtu, new_mtu); 2173 2174 err = idpf_initiate_soft_reset(vport, IDPF_SR_MTU_CHANGE); 2175 2176 idpf_vport_ctrl_unlock(netdev); 2177 2178 return err; 2179 } 2180 2181 /** 2182 * idpf_features_check - Validate packet conforms to limits 2183 * @skb: skb buffer 2184 * @netdev: This port's netdev 2185 * @features: Offload features that the stack believes apply 2186 */ 2187 static netdev_features_t idpf_features_check(struct sk_buff *skb, 2188 struct net_device *netdev, 2189 netdev_features_t features) 2190 { 2191 struct idpf_vport *vport = idpf_netdev_to_vport(netdev); 2192 struct idpf_adapter *adapter = vport->adapter; 2193 size_t len; 2194 2195 /* No point in doing any of this if neither checksum nor GSO are 2196 * being requested for this frame. We can rule out both by just 2197 * checking for CHECKSUM_PARTIAL 2198 */ 2199 if (skb->ip_summed != CHECKSUM_PARTIAL) 2200 return features; 2201 2202 /* We cannot support GSO if the MSS is going to be less than 2203 * 88 bytes. If it is then we need to drop support for GSO. 2204 */ 2205 if (skb_is_gso(skb) && 2206 (skb_shinfo(skb)->gso_size < IDPF_TX_TSO_MIN_MSS)) 2207 features &= ~NETIF_F_GSO_MASK; 2208 2209 /* Ensure MACLEN is <= 126 bytes (63 words) and not an odd size */ 2210 len = skb_network_offset(skb); 2211 if (unlikely(len & ~(126))) 2212 goto unsupported; 2213 2214 len = skb_network_header_len(skb); 2215 if (unlikely(len > idpf_get_max_tx_hdr_size(adapter))) 2216 goto unsupported; 2217 2218 if (!skb->encapsulation) 2219 return features; 2220 2221 /* L4TUNLEN can support 127 words */ 2222 len = skb_inner_network_header(skb) - skb_transport_header(skb); 2223 if (unlikely(len & ~(127 * 2))) 2224 goto unsupported; 2225 2226 /* IPLEN can support at most 127 dwords */ 2227 len = skb_inner_network_header_len(skb); 2228 if (unlikely(len > idpf_get_max_tx_hdr_size(adapter))) 2229 goto unsupported; 2230 2231 /* No need to validate L4LEN as TCP is the only protocol with a 2232 * a flexible value and we support all possible values supported 2233 * by TCP, which is at most 15 dwords 2234 */ 2235 2236 return features; 2237 2238 unsupported: 2239 return features & ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK); 2240 } 2241 2242 /** 2243 * idpf_set_mac - NDO callback to set port mac address 2244 * @netdev: network interface device structure 2245 * @p: pointer to an address structure 2246 * 2247 * Returns 0 on success, negative on failure 2248 **/ 2249 static int idpf_set_mac(struct net_device *netdev, void *p) 2250 { 2251 struct idpf_netdev_priv *np = netdev_priv(netdev); 2252 struct idpf_vport_config *vport_config; 2253 struct sockaddr *addr = p; 2254 struct idpf_vport *vport; 2255 int err = 0; 2256 2257 idpf_vport_ctrl_lock(netdev); 2258 vport = idpf_netdev_to_vport(netdev); 2259 2260 if (!idpf_is_cap_ena(vport->adapter, IDPF_OTHER_CAPS, 2261 VIRTCHNL2_CAP_MACFILTER)) { 2262 dev_info(&vport->adapter->pdev->dev, "Setting MAC address is not supported\n"); 2263 err = -EOPNOTSUPP; 2264 goto unlock_mutex; 2265 } 2266 2267 if (!is_valid_ether_addr(addr->sa_data)) { 2268 dev_info(&vport->adapter->pdev->dev, "Invalid MAC address: %pM\n", 2269 addr->sa_data); 2270 err = -EADDRNOTAVAIL; 2271 goto unlock_mutex; 2272 } 2273 2274 if (ether_addr_equal(netdev->dev_addr, addr->sa_data)) 2275 goto unlock_mutex; 2276 2277 vport_config = vport->adapter->vport_config[vport->idx]; 2278 err = idpf_add_mac_filter(vport, np, addr->sa_data, false); 2279 if (err) { 2280 __idpf_del_mac_filter(vport_config, addr->sa_data); 2281 goto unlock_mutex; 2282 } 2283 2284 if (is_valid_ether_addr(vport->default_mac_addr)) 2285 idpf_del_mac_filter(vport, np, vport->default_mac_addr, false); 2286 2287 ether_addr_copy(vport->default_mac_addr, addr->sa_data); 2288 eth_hw_addr_set(netdev, addr->sa_data); 2289 2290 unlock_mutex: 2291 idpf_vport_ctrl_unlock(netdev); 2292 2293 return err; 2294 } 2295 2296 /** 2297 * idpf_alloc_dma_mem - Allocate dma memory 2298 * @hw: pointer to hw struct 2299 * @mem: pointer to dma_mem struct 2300 * @size: size of the memory to allocate 2301 */ 2302 void *idpf_alloc_dma_mem(struct idpf_hw *hw, struct idpf_dma_mem *mem, u64 size) 2303 { 2304 struct idpf_adapter *adapter = hw->back; 2305 size_t sz = ALIGN(size, 4096); 2306 2307 mem->va = dma_alloc_coherent(&adapter->pdev->dev, sz, 2308 &mem->pa, GFP_KERNEL); 2309 mem->size = sz; 2310 2311 return mem->va; 2312 } 2313 2314 /** 2315 * idpf_free_dma_mem - Free the allocated dma memory 2316 * @hw: pointer to hw struct 2317 * @mem: pointer to dma_mem struct 2318 */ 2319 void idpf_free_dma_mem(struct idpf_hw *hw, struct idpf_dma_mem *mem) 2320 { 2321 struct idpf_adapter *adapter = hw->back; 2322 2323 dma_free_coherent(&adapter->pdev->dev, mem->size, 2324 mem->va, mem->pa); 2325 mem->size = 0; 2326 mem->va = NULL; 2327 mem->pa = 0; 2328 } 2329 2330 static int idpf_hwtstamp_set(struct net_device *netdev, 2331 struct kernel_hwtstamp_config *config, 2332 struct netlink_ext_ack *extack) 2333 { 2334 struct idpf_vport *vport; 2335 int err; 2336 2337 idpf_vport_ctrl_lock(netdev); 2338 vport = idpf_netdev_to_vport(netdev); 2339 2340 if (!vport->link_up) { 2341 idpf_vport_ctrl_unlock(netdev); 2342 return -EPERM; 2343 } 2344 2345 if (!idpf_ptp_is_vport_tx_tstamp_ena(vport) && 2346 !idpf_ptp_is_vport_rx_tstamp_ena(vport)) { 2347 idpf_vport_ctrl_unlock(netdev); 2348 return -EOPNOTSUPP; 2349 } 2350 2351 err = idpf_ptp_set_timestamp_mode(vport, config); 2352 2353 idpf_vport_ctrl_unlock(netdev); 2354 2355 return err; 2356 } 2357 2358 static int idpf_hwtstamp_get(struct net_device *netdev, 2359 struct kernel_hwtstamp_config *config) 2360 { 2361 struct idpf_vport *vport; 2362 2363 idpf_vport_ctrl_lock(netdev); 2364 vport = idpf_netdev_to_vport(netdev); 2365 2366 if (!vport->link_up) { 2367 idpf_vport_ctrl_unlock(netdev); 2368 return -EPERM; 2369 } 2370 2371 if (!idpf_ptp_is_vport_tx_tstamp_ena(vport) && 2372 !idpf_ptp_is_vport_rx_tstamp_ena(vport)) { 2373 idpf_vport_ctrl_unlock(netdev); 2374 return 0; 2375 } 2376 2377 *config = vport->tstamp_config; 2378 2379 idpf_vport_ctrl_unlock(netdev); 2380 2381 return 0; 2382 } 2383 2384 static const struct net_device_ops idpf_netdev_ops = { 2385 .ndo_open = idpf_open, 2386 .ndo_stop = idpf_stop, 2387 .ndo_start_xmit = idpf_tx_start, 2388 .ndo_features_check = idpf_features_check, 2389 .ndo_set_rx_mode = idpf_set_rx_mode, 2390 .ndo_validate_addr = eth_validate_addr, 2391 .ndo_set_mac_address = idpf_set_mac, 2392 .ndo_change_mtu = idpf_change_mtu, 2393 .ndo_get_stats64 = idpf_get_stats64, 2394 .ndo_set_features = idpf_set_features, 2395 .ndo_tx_timeout = idpf_tx_timeout, 2396 .ndo_hwtstamp_get = idpf_hwtstamp_get, 2397 .ndo_hwtstamp_set = idpf_hwtstamp_set, 2398 }; 2399