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