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