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