1 /* Broadcom NetXtreme-C/E network driver. 2 * 3 * Copyright (c) 2016-2018 Broadcom Limited 4 * 5 * This program is free software; you can redistribute it and/or modify 6 * it under the terms of the GNU General Public License as published by 7 * the Free Software Foundation. 8 */ 9 10 #include <linux/module.h> 11 12 #include <linux/kernel.h> 13 #include <linux/errno.h> 14 #include <linux/interrupt.h> 15 #include <linux/pci.h> 16 #include <linux/netdevice.h> 17 #include <linux/rtnetlink.h> 18 #include <linux/bitops.h> 19 #include <linux/irq.h> 20 #include <asm/byteorder.h> 21 #include <linux/bitmap.h> 22 #include <linux/auxiliary_bus.h> 23 #include <net/netdev_lock.h> 24 #include <linux/bnxt/hsi.h> 25 #include <linux/bnxt/ulp.h> 26 27 #include "bnxt.h" 28 #include "bnxt_hwrm.h" 29 30 static DEFINE_IDA(bnxt_aux_dev_ids); 31 32 struct bnxt_aux_device { 33 const char *name; 34 }; 35 36 static void bnxt_auxdev_set_state(struct bnxt *bp, int idx, int state) 37 { 38 bp->auxdev_state[idx] = state; 39 } 40 41 static bool bnxt_auxdev_is_init(struct bnxt *bp, int idx) 42 { 43 return (bp->auxdev_state[idx] == BNXT_ADEV_STATE_INIT); 44 } 45 46 static bool bnxt_auxdev_is_active(struct bnxt *bp, int idx) 47 { 48 return (bp->auxdev_state[idx] == BNXT_ADEV_STATE_ADD); 49 } 50 51 static struct bnxt_aux_device bnxt_aux_devices[__BNXT_AUXDEV_MAX] = {{ 52 .name = "rdma", 53 }, { 54 .name = "fwctl", 55 }}; 56 57 static void bnxt_fill_msix_vecs(struct bnxt *bp, struct bnxt_msix_entry *ent) 58 { 59 struct bnxt_en_dev *edev = bp->edev[BNXT_AUXDEV_RDMA]; 60 int num_msix, i; 61 62 if (!edev->ulp_tbl->msix_requested) { 63 netdev_warn(bp->dev, "Requested MSI-X vectors insufficient\n"); 64 return; 65 } 66 num_msix = edev->ulp_tbl->msix_requested; 67 for (i = 0; i < num_msix; i++) { 68 ent[i].vector = bp->irq_tbl[i].vector; 69 ent[i].ring_idx = i; 70 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) 71 ent[i].db_offset = bp->db_offset; 72 else 73 ent[i].db_offset = i * 0x80; 74 } 75 } 76 77 int bnxt_get_ulp_msix_num(struct bnxt *bp) 78 { 79 struct bnxt_en_dev *edev = bp->edev[BNXT_AUXDEV_RDMA]; 80 81 if (edev) 82 return edev->ulp_num_msix_vec; 83 return 0; 84 } 85 86 void bnxt_set_ulp_msix_num(struct bnxt *bp, int num) 87 { 88 struct bnxt_en_dev *edev = bp->edev[BNXT_AUXDEV_RDMA]; 89 90 if (edev) 91 edev->ulp_num_msix_vec = num; 92 } 93 94 int bnxt_get_ulp_msix_num_in_use(struct bnxt *bp) 95 { 96 struct bnxt_en_dev *edev = bp->edev[BNXT_AUXDEV_RDMA]; 97 98 if (bnxt_ulp_registered(edev)) 99 return edev->ulp_num_msix_vec; 100 return 0; 101 } 102 103 int bnxt_get_ulp_stat_ctxs(struct bnxt *bp) 104 { 105 struct bnxt_en_dev *edev = bp->edev[BNXT_AUXDEV_RDMA]; 106 107 if (edev) 108 return edev->ulp_num_ctxs; 109 return 0; 110 } 111 112 void bnxt_set_ulp_stat_ctxs(struct bnxt *bp, int num_ulp_ctx) 113 { 114 struct bnxt_en_dev *edev = bp->edev[BNXT_AUXDEV_RDMA]; 115 116 if (edev) 117 edev->ulp_num_ctxs = num_ulp_ctx; 118 } 119 120 int bnxt_get_ulp_stat_ctxs_in_use(struct bnxt *bp) 121 { 122 struct bnxt_en_dev *edev = bp->edev[BNXT_AUXDEV_RDMA]; 123 124 if (bnxt_ulp_registered(edev)) 125 return edev->ulp_num_ctxs; 126 return 0; 127 } 128 129 void bnxt_set_dflt_ulp_stat_ctxs(struct bnxt *bp) 130 { 131 struct bnxt_en_dev *edev = bp->edev[BNXT_AUXDEV_RDMA]; 132 133 if (edev) { 134 edev->ulp_num_ctxs = BNXT_MIN_ROCE_STAT_CTXS; 135 /* Reserve one additional stat_ctx for PF0 (except 136 * on 1-port NICs) as it also creates one stat_ctx 137 * for PF1 in case of RoCE bonding. 138 */ 139 if (BNXT_PF(bp) && !bp->pf.port_id && 140 bp->port_count > 1) 141 edev->ulp_num_ctxs++; 142 143 /* Reserve one additional stat_ctx when the device is capable 144 * of supporting port mirroring on RDMA device. 145 */ 146 if (BNXT_MIRROR_ON_ROCE_CAP(bp)) 147 edev->ulp_num_ctxs++; 148 } 149 } 150 151 int bnxt_register_dev(struct bnxt_en_dev *edev, 152 struct bnxt_ulp_ops *ulp_ops, 153 void *handle) 154 { 155 struct net_device *dev = edev->net; 156 struct bnxt *bp = netdev_priv(dev); 157 unsigned int max_stat_ctxs; 158 struct bnxt_ulp *ulp; 159 int rc = 0; 160 161 netdev_lock(dev); 162 mutex_lock(&edev->en_dev_lock); 163 if (!bp->irq_tbl) { 164 rc = -ENODEV; 165 goto exit; 166 } 167 max_stat_ctxs = bnxt_get_max_func_stat_ctxs(bp); 168 if (max_stat_ctxs <= BNXT_MIN_ROCE_STAT_CTXS || 169 bp->cp_nr_rings == max_stat_ctxs) { 170 rc = -ENOMEM; 171 goto exit; 172 } 173 174 ulp = edev->ulp_tbl; 175 ulp->handle = handle; 176 rcu_assign_pointer(ulp->ulp_ops, ulp_ops); 177 178 if (test_bit(BNXT_STATE_OPEN, &bp->state)) 179 bnxt_hwrm_vnic_cfg(bp, &bp->vnic_info[BNXT_VNIC_DEFAULT]); 180 181 edev->ulp_tbl->msix_requested = bnxt_get_ulp_msix_num(bp); 182 183 bnxt_fill_msix_vecs(bp, edev->msix_entries); 184 exit: 185 mutex_unlock(&edev->en_dev_lock); 186 netdev_unlock(dev); 187 return rc; 188 } 189 EXPORT_SYMBOL(bnxt_register_dev); 190 191 void bnxt_unregister_dev(struct bnxt_en_dev *edev) 192 { 193 struct net_device *dev = edev->net; 194 struct bnxt *bp = netdev_priv(dev); 195 struct bnxt_ulp *ulp; 196 197 ulp = edev->ulp_tbl; 198 netdev_lock(dev); 199 mutex_lock(&edev->en_dev_lock); 200 edev->ulp_tbl->msix_requested = 0; 201 202 if (ulp->max_async_event_id) 203 bnxt_hwrm_func_drv_rgtr(bp, NULL, 0, true); 204 205 RCU_INIT_POINTER(ulp->ulp_ops, NULL); 206 synchronize_rcu(); 207 ulp->max_async_event_id = 0; 208 ulp->async_events_bmap = NULL; 209 mutex_unlock(&edev->en_dev_lock); 210 netdev_unlock(dev); 211 return; 212 } 213 EXPORT_SYMBOL(bnxt_unregister_dev); 214 215 static int bnxt_set_dflt_ulp_msix(struct bnxt *bp) 216 { 217 int roce_msix = BNXT_MAX_ROCE_MSIX; 218 219 if (BNXT_VF(bp)) 220 roce_msix = BNXT_MAX_ROCE_MSIX_VF; 221 else if (bp->port_partition_type) 222 roce_msix = BNXT_MAX_ROCE_MSIX_NPAR_PF; 223 224 /* NQ MSIX vectors should match the number of CPUs plus 1 more for 225 * the CREQ MSIX, up to the default. 226 */ 227 return min_t(int, roce_msix, num_online_cpus() + 1); 228 } 229 230 int bnxt_send_msg(struct bnxt_en_dev *edev, 231 struct bnxt_fw_msg *fw_msg) 232 { 233 struct net_device *dev = edev->net; 234 struct bnxt *bp = netdev_priv(dev); 235 struct output *resp; 236 struct input *req; 237 u32 resp_len; 238 int rc; 239 240 if (bp->fw_reset_state) 241 return -EBUSY; 242 243 rc = hwrm_req_init(bp, req, 0 /* don't care */); 244 if (rc) 245 return rc; 246 247 rc = hwrm_req_replace(bp, req, fw_msg->msg, fw_msg->msg_len); 248 if (rc) 249 goto drop_req; 250 251 hwrm_req_timeout(bp, req, fw_msg->timeout); 252 resp = hwrm_req_hold(bp, req); 253 rc = hwrm_req_send(bp, req); 254 resp_len = le16_to_cpu(resp->resp_len); 255 if (resp_len) { 256 if (fw_msg->resp_max_len < resp_len) 257 resp_len = fw_msg->resp_max_len; 258 259 memcpy(fw_msg->resp, resp, resp_len); 260 } 261 drop_req: 262 hwrm_req_drop(bp, req); 263 return rc; 264 } 265 EXPORT_SYMBOL(bnxt_send_msg); 266 267 void bnxt_ulp_stop(struct bnxt *bp) 268 { 269 int i; 270 271 mutex_lock(&bp->auxdev_lock); 272 for (i = 0; i < __BNXT_AUXDEV_MAX; i++) { 273 struct bnxt_aux_priv *aux_priv; 274 struct auxiliary_device *adev; 275 struct bnxt_en_dev *edev; 276 277 if (!bnxt_auxdev_is_active(bp, i)) 278 continue; 279 280 aux_priv = bp->aux_priv[i]; 281 edev = bp->edev[i]; 282 mutex_lock(&edev->en_dev_lock); 283 if (i == BNXT_AUXDEV_FWCTL) { 284 edev->flags |= BNXT_EN_FLAG_ULP_STOPPED; 285 mutex_unlock(&edev->en_dev_lock); 286 continue; 287 } 288 if (!bnxt_ulp_registered(edev) || 289 (edev->flags & BNXT_EN_FLAG_ULP_STOPPED)) { 290 mutex_unlock(&edev->en_dev_lock); 291 continue; 292 } 293 294 edev->flags |= BNXT_EN_FLAG_ULP_STOPPED; 295 296 adev = &aux_priv->aux_dev; 297 if (adev->dev.driver) { 298 const struct auxiliary_driver *adrv; 299 pm_message_t pm = {}; 300 301 adrv = to_auxiliary_drv(adev->dev.driver); 302 edev->en_state = bp->state; 303 adrv->suspend(adev, pm); 304 } 305 mutex_unlock(&edev->en_dev_lock); 306 } 307 mutex_unlock(&bp->auxdev_lock); 308 } 309 310 void bnxt_ulp_start(struct bnxt *bp) 311 { 312 int i; 313 314 mutex_lock(&bp->auxdev_lock); 315 for (i = 0; i < __BNXT_AUXDEV_MAX; i++) { 316 struct bnxt_aux_priv *aux_priv; 317 struct auxiliary_device *adev; 318 struct bnxt_en_dev *edev; 319 320 if (!bnxt_auxdev_is_active(bp, i)) 321 continue; 322 323 aux_priv = bp->aux_priv[i]; 324 edev = bp->edev[i]; 325 mutex_lock(&edev->en_dev_lock); 326 if (i == BNXT_AUXDEV_FWCTL || !bnxt_ulp_registered(edev) || 327 !(edev->flags & BNXT_EN_FLAG_ULP_STOPPED)) { 328 goto clear_flag_continue; 329 } 330 331 if (edev->ulp_tbl->msix_requested) 332 bnxt_fill_msix_vecs(bp, edev->msix_entries); 333 334 335 adev = &aux_priv->aux_dev; 336 if (adev->dev.driver) { 337 const struct auxiliary_driver *adrv; 338 339 adrv = to_auxiliary_drv(adev->dev.driver); 340 edev->en_state = bp->state; 341 adrv->resume(adev); 342 } 343 clear_flag_continue: 344 edev->flags &= ~BNXT_EN_FLAG_ULP_STOPPED; 345 mutex_unlock(&edev->en_dev_lock); 346 } 347 mutex_unlock(&bp->auxdev_lock); 348 } 349 350 void bnxt_ulp_irq_stop(struct bnxt *bp) 351 { 352 struct bnxt_en_dev *edev = bp->edev[BNXT_AUXDEV_RDMA]; 353 struct bnxt_ulp_ops *ops; 354 bool reset = false; 355 356 if (!edev) 357 return; 358 359 if (bnxt_ulp_registered(edev)) { 360 struct bnxt_ulp *ulp = edev->ulp_tbl; 361 362 if (!ulp->msix_requested) 363 return; 364 365 ops = netdev_lock_dereference(ulp->ulp_ops, bp->dev); 366 if (!ops || !ops->ulp_irq_stop) 367 return; 368 if (test_bit(BNXT_STATE_FW_RESET_DET, &bp->state)) 369 reset = true; 370 ops->ulp_irq_stop(ulp->handle, reset); 371 } 372 } 373 374 void bnxt_ulp_irq_restart(struct bnxt *bp, int err) 375 { 376 struct bnxt_en_dev *edev = bp->edev[BNXT_AUXDEV_RDMA]; 377 struct bnxt_ulp_ops *ops; 378 379 if (!edev) 380 return; 381 382 if (bnxt_ulp_registered(edev)) { 383 struct bnxt_ulp *ulp = edev->ulp_tbl; 384 struct bnxt_msix_entry *ent = NULL; 385 386 if (!ulp->msix_requested) 387 return; 388 389 ops = netdev_lock_dereference(ulp->ulp_ops, bp->dev); 390 if (!ops || !ops->ulp_irq_restart) 391 return; 392 393 if (!err) { 394 ent = kzalloc_objs(*ent, ulp->msix_requested); 395 if (!ent) 396 return; 397 bnxt_fill_msix_vecs(bp, ent); 398 } 399 ops->ulp_irq_restart(ulp->handle, ent); 400 kfree(ent); 401 } 402 } 403 404 void bnxt_ulp_async_events(struct bnxt *bp, struct hwrm_async_event_cmpl *cmpl) 405 { 406 u16 event_id = le16_to_cpu(cmpl->event_id); 407 struct bnxt_en_dev *edev = bp->edev[BNXT_AUXDEV_RDMA]; 408 struct bnxt_ulp_ops *ops; 409 struct bnxt_ulp *ulp; 410 411 if (!bnxt_ulp_registered(edev)) 412 return; 413 ulp = edev->ulp_tbl; 414 415 rcu_read_lock(); 416 417 ops = rcu_dereference(ulp->ulp_ops); 418 if (!ops || !ops->ulp_async_notifier) 419 goto exit_unlock_rcu; 420 if (!ulp->async_events_bmap || event_id > ulp->max_async_event_id) 421 goto exit_unlock_rcu; 422 423 /* Read max_async_event_id first before testing the bitmap. */ 424 smp_rmb(); 425 426 if (test_bit(event_id, ulp->async_events_bmap)) 427 ops->ulp_async_notifier(ulp->handle, cmpl); 428 exit_unlock_rcu: 429 rcu_read_unlock(); 430 } 431 432 void bnxt_register_async_events(struct bnxt_en_dev *edev, 433 unsigned long *events_bmap, u16 max_id) 434 { 435 struct net_device *dev = edev->net; 436 struct bnxt *bp = netdev_priv(dev); 437 struct bnxt_ulp *ulp; 438 439 ulp = edev->ulp_tbl; 440 ulp->async_events_bmap = events_bmap; 441 /* Make sure bnxt_ulp_async_events() sees this order */ 442 smp_wmb(); 443 ulp->max_async_event_id = max_id; 444 bnxt_hwrm_func_drv_rgtr(bp, events_bmap, max_id + 1, true); 445 } 446 EXPORT_SYMBOL(bnxt_register_async_events); 447 448 void bnxt_aux_devices_uninit(struct bnxt *bp) 449 { 450 struct bnxt_aux_priv *aux_priv; 451 struct auxiliary_device *adev; 452 int idx; 453 454 mutex_lock(&bp->auxdev_lock); 455 for (idx = 0; idx < __BNXT_AUXDEV_MAX; idx++) { 456 if (bnxt_auxdev_is_init(bp, idx)) { 457 aux_priv = bp->aux_priv[idx]; 458 adev = &aux_priv->aux_dev; 459 auxiliary_device_uninit(adev); 460 } 461 } 462 mutex_unlock(&bp->auxdev_lock); 463 } 464 465 static void bnxt_aux_dev_release(struct device *dev) 466 { 467 struct bnxt_aux_priv *aux_priv = 468 container_of(dev, struct bnxt_aux_priv, aux_dev.dev); 469 struct bnxt *bp = netdev_priv(aux_priv->edev->net); 470 471 kfree(aux_priv->edev->ulp_tbl); 472 bp->edev[aux_priv->id] = NULL; 473 kfree(aux_priv->edev); 474 bp->aux_priv[aux_priv->id] = NULL; 475 kfree(aux_priv); 476 } 477 478 void bnxt_aux_devices_del(struct bnxt *bp) 479 { 480 int idx; 481 482 mutex_lock(&bp->auxdev_lock); 483 for (idx = 0; idx < __BNXT_AUXDEV_MAX; idx++) { 484 if (bnxt_auxdev_is_active(bp, idx)) { 485 auxiliary_device_delete(&bp->aux_priv[idx]->aux_dev); 486 bnxt_auxdev_set_state(bp, idx, BNXT_ADEV_STATE_INIT); 487 } 488 } 489 mutex_unlock(&bp->auxdev_lock); 490 } 491 492 static void bnxt_set_edev_info(struct bnxt_en_dev *edev, struct bnxt *bp) 493 { 494 edev->net = bp->dev; 495 edev->pdev = bp->pdev; 496 edev->l2_db_size = bp->db_size; 497 edev->l2_db_size_nc = bp->db_size; 498 edev->l2_db_offset = bp->db_offset; 499 mutex_init(&edev->en_dev_lock); 500 501 if (bp->flags & BNXT_FLAG_ROCEV1_CAP) 502 edev->flags |= BNXT_EN_FLAG_ROCEV1_CAP; 503 if (bp->flags & BNXT_FLAG_ROCEV2_CAP) 504 edev->flags |= BNXT_EN_FLAG_ROCEV2_CAP; 505 if (bp->flags & BNXT_FLAG_VF) 506 edev->flags |= BNXT_EN_FLAG_VF; 507 if (BNXT_ROCE_VF_RESC_CAP(bp)) 508 edev->flags |= BNXT_EN_FLAG_ROCE_VF_RES_MGMT; 509 if (BNXT_SW_RES_LMT(bp)) 510 edev->flags |= BNXT_EN_FLAG_SW_RES_LMT; 511 512 edev->chip_num = bp->chip_num; 513 edev->hw_ring_stats_size = bp->hw_ring_stats_size; 514 edev->pf_port_id = bp->pf.port_id; 515 edev->en_state = bp->state; 516 edev->bar0 = bp->bar0; 517 } 518 519 void bnxt_aux_devices_add(struct bnxt *bp) 520 { 521 struct auxiliary_device *aux_dev; 522 int rc, idx; 523 524 mutex_lock(&bp->auxdev_lock); 525 for (idx = 0; idx < __BNXT_AUXDEV_MAX; idx++) { 526 if (bnxt_auxdev_is_init(bp, idx)) { 527 aux_dev = &bp->aux_priv[idx]->aux_dev; 528 rc = auxiliary_device_add(aux_dev); 529 if (rc) { 530 netdev_warn(bp->dev, "Failed to add auxiliary device for auxdev type %d\n", 531 idx); 532 auxiliary_device_uninit(aux_dev); 533 if (idx == BNXT_AUXDEV_RDMA) 534 bp->flags &= ~BNXT_FLAG_ROCE_CAP; 535 continue; 536 } 537 bnxt_auxdev_set_state(bp, idx, BNXT_ADEV_STATE_ADD); 538 } 539 } 540 mutex_unlock(&bp->auxdev_lock); 541 } 542 543 void bnxt_aux_devices_init(struct bnxt *bp) 544 { 545 struct auxiliary_device *aux_dev; 546 struct bnxt_aux_priv *aux_priv; 547 struct bnxt_en_dev *edev; 548 struct bnxt_ulp *ulp; 549 int rc, idx; 550 551 mutex_lock(&bp->auxdev_lock); 552 for (idx = 0; idx < __BNXT_AUXDEV_MAX; idx++) { 553 bnxt_auxdev_set_state(bp, idx, BNXT_ADEV_STATE_NONE); 554 555 if (idx == BNXT_AUXDEV_RDMA && 556 !(bp->flags & BNXT_FLAG_ROCE_CAP)) 557 continue; 558 559 aux_priv = kzalloc_obj(*aux_priv); 560 if (!aux_priv) 561 goto next_auxdev; 562 563 aux_dev = &aux_priv->aux_dev; 564 aux_dev->id = bp->auxdev_id; 565 aux_dev->name = bnxt_aux_devices[idx].name; 566 aux_dev->dev.parent = &bp->pdev->dev; 567 aux_dev->dev.release = bnxt_aux_dev_release; 568 569 rc = auxiliary_device_init(aux_dev); 570 if (rc) { 571 kfree(aux_priv); 572 goto next_auxdev; 573 } 574 bp->aux_priv[idx] = aux_priv; 575 576 /* From this point, all cleanup will happen via the .release 577 * callback & any error unwinding will need to include a call 578 * to auxiliary_device_uninit. 579 */ 580 edev = kzalloc_obj(*edev); 581 if (!edev) 582 goto aux_dev_uninit; 583 584 aux_priv->edev = edev; 585 bnxt_set_edev_info(edev, bp); 586 587 ulp = kzalloc_obj(*ulp); 588 if (!ulp) 589 goto aux_dev_uninit; 590 591 edev->ulp_tbl = ulp; 592 bp->edev[idx] = edev; 593 if (idx == BNXT_AUXDEV_RDMA) 594 bp->ulp_num_msix_want = bnxt_set_dflt_ulp_msix(bp); 595 aux_priv->id = idx; 596 bnxt_auxdev_set_state(bp, idx, BNXT_ADEV_STATE_INIT); 597 598 continue; 599 aux_dev_uninit: 600 auxiliary_device_uninit(aux_dev); 601 next_auxdev: 602 if (idx == BNXT_AUXDEV_RDMA) 603 bp->flags &= ~BNXT_FLAG_ROCE_CAP; 604 } 605 mutex_unlock(&bp->auxdev_lock); 606 } 607 608 int bnxt_auxdev_id_alloc(struct bnxt *bp) 609 { 610 bp->auxdev_id = ida_alloc(&bnxt_aux_dev_ids, GFP_KERNEL); 611 if (bp->auxdev_id < 0) 612 return bp->auxdev_id; 613 614 return 0; 615 } 616 617 void bnxt_auxdev_id_free(struct bnxt *bp, int id) 618 { 619 if (bp->auxdev_id >= 0) 620 ida_free(&bnxt_aux_dev_ids, id); 621 } 622