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 25 #include "bnxt_hsi.h" 26 #include "bnxt.h" 27 #include "bnxt_hwrm.h" 28 #include "bnxt_ulp.h" 29 30 static DEFINE_IDA(bnxt_aux_dev_ids); 31 32 static void bnxt_fill_msix_vecs(struct bnxt *bp, struct bnxt_msix_entry *ent) 33 { 34 struct bnxt_en_dev *edev = bp->edev; 35 int num_msix, i; 36 37 if (!edev->ulp_tbl->msix_requested) { 38 netdev_warn(bp->dev, "Requested MSI-X vectors insufficient\n"); 39 return; 40 } 41 num_msix = edev->ulp_tbl->msix_requested; 42 for (i = 0; i < num_msix; i++) { 43 ent[i].vector = bp->irq_tbl[i].vector; 44 ent[i].ring_idx = i; 45 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) 46 ent[i].db_offset = bp->db_offset; 47 else 48 ent[i].db_offset = i * 0x80; 49 } 50 } 51 52 int bnxt_get_ulp_msix_num(struct bnxt *bp) 53 { 54 if (bp->edev) 55 return bp->edev->ulp_num_msix_vec; 56 return 0; 57 } 58 59 void bnxt_set_ulp_msix_num(struct bnxt *bp, int num) 60 { 61 if (bp->edev) 62 bp->edev->ulp_num_msix_vec = num; 63 } 64 65 int bnxt_get_ulp_msix_num_in_use(struct bnxt *bp) 66 { 67 if (bnxt_ulp_registered(bp->edev)) 68 return bp->edev->ulp_num_msix_vec; 69 return 0; 70 } 71 72 int bnxt_get_ulp_stat_ctxs(struct bnxt *bp) 73 { 74 if (bp->edev) 75 return bp->edev->ulp_num_ctxs; 76 return 0; 77 } 78 79 void bnxt_set_ulp_stat_ctxs(struct bnxt *bp, int num_ulp_ctx) 80 { 81 if (bp->edev) 82 bp->edev->ulp_num_ctxs = num_ulp_ctx; 83 } 84 85 int bnxt_get_ulp_stat_ctxs_in_use(struct bnxt *bp) 86 { 87 if (bnxt_ulp_registered(bp->edev)) 88 return bp->edev->ulp_num_ctxs; 89 return 0; 90 } 91 92 void bnxt_set_dflt_ulp_stat_ctxs(struct bnxt *bp) 93 { 94 if (bp->edev) { 95 bp->edev->ulp_num_ctxs = BNXT_MIN_ROCE_STAT_CTXS; 96 /* Reserve one additional stat_ctx for PF0 (except 97 * on 1-port NICs) as it also creates one stat_ctx 98 * for PF1 in case of RoCE bonding. 99 */ 100 if (BNXT_PF(bp) && !bp->pf.port_id && 101 bp->port_count > 1) 102 bp->edev->ulp_num_ctxs++; 103 } 104 } 105 106 int bnxt_register_dev(struct bnxt_en_dev *edev, 107 struct bnxt_ulp_ops *ulp_ops, 108 void *handle) 109 { 110 struct net_device *dev = edev->net; 111 struct bnxt *bp = netdev_priv(dev); 112 unsigned int max_stat_ctxs; 113 struct bnxt_ulp *ulp; 114 int rc = 0; 115 116 netdev_lock(dev); 117 mutex_lock(&edev->en_dev_lock); 118 if (!bp->irq_tbl) { 119 rc = -ENODEV; 120 goto exit; 121 } 122 max_stat_ctxs = bnxt_get_max_func_stat_ctxs(bp); 123 if (max_stat_ctxs <= BNXT_MIN_ROCE_STAT_CTXS || 124 bp->cp_nr_rings == max_stat_ctxs) { 125 rc = -ENOMEM; 126 goto exit; 127 } 128 129 ulp = edev->ulp_tbl; 130 ulp->handle = handle; 131 rcu_assign_pointer(ulp->ulp_ops, ulp_ops); 132 133 if (test_bit(BNXT_STATE_OPEN, &bp->state)) 134 bnxt_hwrm_vnic_cfg(bp, &bp->vnic_info[BNXT_VNIC_DEFAULT]); 135 136 edev->ulp_tbl->msix_requested = bnxt_get_ulp_msix_num(bp); 137 138 bnxt_fill_msix_vecs(bp, bp->edev->msix_entries); 139 edev->flags |= BNXT_EN_FLAG_MSIX_REQUESTED; 140 exit: 141 mutex_unlock(&edev->en_dev_lock); 142 netdev_unlock(dev); 143 return rc; 144 } 145 EXPORT_SYMBOL(bnxt_register_dev); 146 147 void bnxt_unregister_dev(struct bnxt_en_dev *edev) 148 { 149 struct net_device *dev = edev->net; 150 struct bnxt *bp = netdev_priv(dev); 151 struct bnxt_ulp *ulp; 152 int i = 0; 153 154 ulp = edev->ulp_tbl; 155 netdev_lock(dev); 156 mutex_lock(&edev->en_dev_lock); 157 if (ulp->msix_requested) 158 edev->flags &= ~BNXT_EN_FLAG_MSIX_REQUESTED; 159 edev->ulp_tbl->msix_requested = 0; 160 161 if (ulp->max_async_event_id) 162 bnxt_hwrm_func_drv_rgtr(bp, NULL, 0, true); 163 164 RCU_INIT_POINTER(ulp->ulp_ops, NULL); 165 synchronize_rcu(); 166 ulp->max_async_event_id = 0; 167 ulp->async_events_bmap = NULL; 168 while (atomic_read(&ulp->ref_count) != 0 && i < 10) { 169 msleep(100); 170 i++; 171 } 172 mutex_unlock(&edev->en_dev_lock); 173 netdev_unlock(dev); 174 return; 175 } 176 EXPORT_SYMBOL(bnxt_unregister_dev); 177 178 static int bnxt_set_dflt_ulp_msix(struct bnxt *bp) 179 { 180 int roce_msix = BNXT_MAX_ROCE_MSIX; 181 182 if (BNXT_VF(bp)) 183 roce_msix = BNXT_MAX_ROCE_MSIX_VF; 184 else if (bp->port_partition_type) 185 roce_msix = BNXT_MAX_ROCE_MSIX_NPAR_PF; 186 187 /* NQ MSIX vectors should match the number of CPUs plus 1 more for 188 * the CREQ MSIX, up to the default. 189 */ 190 return min_t(int, roce_msix, num_online_cpus() + 1); 191 } 192 193 int bnxt_send_msg(struct bnxt_en_dev *edev, 194 struct bnxt_fw_msg *fw_msg) 195 { 196 struct net_device *dev = edev->net; 197 struct bnxt *bp = netdev_priv(dev); 198 struct output *resp; 199 struct input *req; 200 u32 resp_len; 201 int rc; 202 203 if (bp->fw_reset_state) 204 return -EBUSY; 205 206 rc = hwrm_req_init(bp, req, 0 /* don't care */); 207 if (rc) 208 return rc; 209 210 rc = hwrm_req_replace(bp, req, fw_msg->msg, fw_msg->msg_len); 211 if (rc) 212 goto drop_req; 213 214 hwrm_req_timeout(bp, req, fw_msg->timeout); 215 resp = hwrm_req_hold(bp, req); 216 rc = hwrm_req_send(bp, req); 217 resp_len = le16_to_cpu(resp->resp_len); 218 if (resp_len) { 219 if (fw_msg->resp_max_len < resp_len) 220 resp_len = fw_msg->resp_max_len; 221 222 memcpy(fw_msg->resp, resp, resp_len); 223 } 224 drop_req: 225 hwrm_req_drop(bp, req); 226 return rc; 227 } 228 EXPORT_SYMBOL(bnxt_send_msg); 229 230 void bnxt_ulp_stop(struct bnxt *bp) 231 { 232 struct bnxt_aux_priv *aux_priv = bp->aux_priv; 233 struct bnxt_en_dev *edev = bp->edev; 234 235 if (!edev) 236 return; 237 238 mutex_lock(&edev->en_dev_lock); 239 if (!bnxt_ulp_registered(edev)) { 240 mutex_unlock(&edev->en_dev_lock); 241 return; 242 } 243 244 edev->flags |= BNXT_EN_FLAG_ULP_STOPPED; 245 if (aux_priv) { 246 struct auxiliary_device *adev; 247 248 adev = &aux_priv->aux_dev; 249 if (adev->dev.driver) { 250 const struct auxiliary_driver *adrv; 251 pm_message_t pm = {}; 252 253 adrv = to_auxiliary_drv(adev->dev.driver); 254 edev->en_state = bp->state; 255 adrv->suspend(adev, pm); 256 } 257 } 258 mutex_unlock(&edev->en_dev_lock); 259 } 260 261 void bnxt_ulp_start(struct bnxt *bp, int err) 262 { 263 struct bnxt_aux_priv *aux_priv = bp->aux_priv; 264 struct bnxt_en_dev *edev = bp->edev; 265 266 if (!edev) 267 return; 268 269 edev->flags &= ~BNXT_EN_FLAG_ULP_STOPPED; 270 271 if (err) 272 return; 273 274 mutex_lock(&edev->en_dev_lock); 275 if (!bnxt_ulp_registered(edev)) { 276 mutex_unlock(&edev->en_dev_lock); 277 return; 278 } 279 280 if (edev->ulp_tbl->msix_requested) 281 bnxt_fill_msix_vecs(bp, edev->msix_entries); 282 283 if (aux_priv) { 284 struct auxiliary_device *adev; 285 286 adev = &aux_priv->aux_dev; 287 if (adev->dev.driver) { 288 const struct auxiliary_driver *adrv; 289 290 adrv = to_auxiliary_drv(adev->dev.driver); 291 edev->en_state = bp->state; 292 adrv->resume(adev); 293 } 294 } 295 mutex_unlock(&edev->en_dev_lock); 296 } 297 298 void bnxt_ulp_irq_stop(struct bnxt *bp) 299 { 300 struct bnxt_en_dev *edev = bp->edev; 301 struct bnxt_ulp_ops *ops; 302 bool reset = false; 303 304 if (!edev || !(edev->flags & BNXT_EN_FLAG_MSIX_REQUESTED)) 305 return; 306 307 if (bnxt_ulp_registered(bp->edev)) { 308 struct bnxt_ulp *ulp = edev->ulp_tbl; 309 310 if (!ulp->msix_requested) 311 return; 312 313 ops = netdev_lock_dereference(ulp->ulp_ops, bp->dev); 314 if (!ops || !ops->ulp_irq_stop) 315 return; 316 if (test_bit(BNXT_STATE_FW_RESET_DET, &bp->state)) 317 reset = true; 318 ops->ulp_irq_stop(ulp->handle, reset); 319 } 320 } 321 322 void bnxt_ulp_irq_restart(struct bnxt *bp, int err) 323 { 324 struct bnxt_en_dev *edev = bp->edev; 325 struct bnxt_ulp_ops *ops; 326 327 if (!edev || !(edev->flags & BNXT_EN_FLAG_MSIX_REQUESTED)) 328 return; 329 330 if (bnxt_ulp_registered(bp->edev)) { 331 struct bnxt_ulp *ulp = edev->ulp_tbl; 332 struct bnxt_msix_entry *ent = NULL; 333 334 if (!ulp->msix_requested) 335 return; 336 337 ops = netdev_lock_dereference(ulp->ulp_ops, bp->dev); 338 if (!ops || !ops->ulp_irq_restart) 339 return; 340 341 if (!err) { 342 ent = kcalloc(ulp->msix_requested, sizeof(*ent), 343 GFP_KERNEL); 344 if (!ent) 345 return; 346 bnxt_fill_msix_vecs(bp, ent); 347 } 348 ops->ulp_irq_restart(ulp->handle, ent); 349 kfree(ent); 350 } 351 } 352 353 void bnxt_ulp_async_events(struct bnxt *bp, struct hwrm_async_event_cmpl *cmpl) 354 { 355 u16 event_id = le16_to_cpu(cmpl->event_id); 356 struct bnxt_en_dev *edev = bp->edev; 357 struct bnxt_ulp_ops *ops; 358 struct bnxt_ulp *ulp; 359 360 if (!bnxt_ulp_registered(edev)) 361 return; 362 ulp = edev->ulp_tbl; 363 364 rcu_read_lock(); 365 366 ops = rcu_dereference(ulp->ulp_ops); 367 if (!ops || !ops->ulp_async_notifier) 368 goto exit_unlock_rcu; 369 if (!ulp->async_events_bmap || event_id > ulp->max_async_event_id) 370 goto exit_unlock_rcu; 371 372 /* Read max_async_event_id first before testing the bitmap. */ 373 smp_rmb(); 374 375 if (test_bit(event_id, ulp->async_events_bmap)) 376 ops->ulp_async_notifier(ulp->handle, cmpl); 377 exit_unlock_rcu: 378 rcu_read_unlock(); 379 } 380 381 void bnxt_register_async_events(struct bnxt_en_dev *edev, 382 unsigned long *events_bmap, u16 max_id) 383 { 384 struct net_device *dev = edev->net; 385 struct bnxt *bp = netdev_priv(dev); 386 struct bnxt_ulp *ulp; 387 388 ulp = edev->ulp_tbl; 389 ulp->async_events_bmap = events_bmap; 390 /* Make sure bnxt_ulp_async_events() sees this order */ 391 smp_wmb(); 392 ulp->max_async_event_id = max_id; 393 bnxt_hwrm_func_drv_rgtr(bp, events_bmap, max_id + 1, true); 394 } 395 EXPORT_SYMBOL(bnxt_register_async_events); 396 397 void bnxt_rdma_aux_device_uninit(struct bnxt *bp) 398 { 399 struct bnxt_aux_priv *aux_priv; 400 struct auxiliary_device *adev; 401 402 /* Skip if no auxiliary device init was done. */ 403 if (!bp->aux_priv) 404 return; 405 406 aux_priv = bp->aux_priv; 407 adev = &aux_priv->aux_dev; 408 auxiliary_device_uninit(adev); 409 } 410 411 static void bnxt_aux_dev_release(struct device *dev) 412 { 413 struct bnxt_aux_priv *aux_priv = 414 container_of(dev, struct bnxt_aux_priv, aux_dev.dev); 415 struct bnxt *bp = netdev_priv(aux_priv->edev->net); 416 417 ida_free(&bnxt_aux_dev_ids, aux_priv->id); 418 kfree(aux_priv->edev->ulp_tbl); 419 bp->edev = NULL; 420 kfree(aux_priv->edev); 421 kfree(aux_priv); 422 bp->aux_priv = NULL; 423 } 424 425 void bnxt_rdma_aux_device_del(struct bnxt *bp) 426 { 427 if (!bp->edev) 428 return; 429 430 auxiliary_device_delete(&bp->aux_priv->aux_dev); 431 } 432 433 static void bnxt_set_edev_info(struct bnxt_en_dev *edev, struct bnxt *bp) 434 { 435 edev->net = bp->dev; 436 edev->pdev = bp->pdev; 437 edev->l2_db_size = bp->db_size; 438 edev->l2_db_size_nc = bp->db_size; 439 edev->l2_db_offset = bp->db_offset; 440 mutex_init(&edev->en_dev_lock); 441 442 if (bp->flags & BNXT_FLAG_ROCEV1_CAP) 443 edev->flags |= BNXT_EN_FLAG_ROCEV1_CAP; 444 if (bp->flags & BNXT_FLAG_ROCEV2_CAP) 445 edev->flags |= BNXT_EN_FLAG_ROCEV2_CAP; 446 if (bp->flags & BNXT_FLAG_VF) 447 edev->flags |= BNXT_EN_FLAG_VF; 448 if (BNXT_ROCE_VF_RESC_CAP(bp)) 449 edev->flags |= BNXT_EN_FLAG_ROCE_VF_RES_MGMT; 450 if (BNXT_SW_RES_LMT(bp)) 451 edev->flags |= BNXT_EN_FLAG_SW_RES_LMT; 452 453 edev->chip_num = bp->chip_num; 454 edev->hw_ring_stats_size = bp->hw_ring_stats_size; 455 edev->pf_port_id = bp->pf.port_id; 456 edev->en_state = bp->state; 457 edev->bar0 = bp->bar0; 458 } 459 460 void bnxt_rdma_aux_device_add(struct bnxt *bp) 461 { 462 struct auxiliary_device *aux_dev; 463 int rc; 464 465 if (!bp->edev) 466 return; 467 468 aux_dev = &bp->aux_priv->aux_dev; 469 rc = auxiliary_device_add(aux_dev); 470 if (rc) { 471 netdev_warn(bp->dev, "Failed to add auxiliary device for ROCE\n"); 472 auxiliary_device_uninit(aux_dev); 473 bp->flags &= ~BNXT_FLAG_ROCE_CAP; 474 } 475 } 476 477 void bnxt_rdma_aux_device_init(struct bnxt *bp) 478 { 479 struct auxiliary_device *aux_dev; 480 struct bnxt_aux_priv *aux_priv; 481 struct bnxt_en_dev *edev; 482 struct bnxt_ulp *ulp; 483 int rc; 484 485 if (!(bp->flags & BNXT_FLAG_ROCE_CAP)) 486 return; 487 488 aux_priv = kzalloc(sizeof(*bp->aux_priv), GFP_KERNEL); 489 if (!aux_priv) 490 goto exit; 491 492 aux_priv->id = ida_alloc(&bnxt_aux_dev_ids, GFP_KERNEL); 493 if (aux_priv->id < 0) { 494 netdev_warn(bp->dev, 495 "ida alloc failed for ROCE auxiliary device\n"); 496 kfree(aux_priv); 497 goto exit; 498 } 499 500 aux_dev = &aux_priv->aux_dev; 501 aux_dev->id = aux_priv->id; 502 aux_dev->name = "rdma"; 503 aux_dev->dev.parent = &bp->pdev->dev; 504 aux_dev->dev.release = bnxt_aux_dev_release; 505 506 rc = auxiliary_device_init(aux_dev); 507 if (rc) { 508 ida_free(&bnxt_aux_dev_ids, aux_priv->id); 509 kfree(aux_priv); 510 goto exit; 511 } 512 bp->aux_priv = aux_priv; 513 514 /* From this point, all cleanup will happen via the .release callback & 515 * any error unwinding will need to include a call to 516 * auxiliary_device_uninit. 517 */ 518 edev = kzalloc(sizeof(*edev), GFP_KERNEL); 519 if (!edev) 520 goto aux_dev_uninit; 521 522 aux_priv->edev = edev; 523 524 ulp = kzalloc(sizeof(*ulp), GFP_KERNEL); 525 if (!ulp) 526 goto aux_dev_uninit; 527 528 edev->ulp_tbl = ulp; 529 bp->edev = edev; 530 bnxt_set_edev_info(edev, bp); 531 bp->ulp_num_msix_want = bnxt_set_dflt_ulp_msix(bp); 532 533 return; 534 535 aux_dev_uninit: 536 auxiliary_device_uninit(aux_dev); 537 exit: 538 bp->flags &= ~BNXT_FLAG_ROCE_CAP; 539 } 540