1 /* Broadcom NetXtreme-C/E network driver.
3 * Copyright (c) 2016-2018 Broadcom Limited
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.
10 #include <linux/module.h>
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>
26 #include "bnxt_hwrm.h"
29 static DEFINE_IDA(bnxt_aux_dev_ids);
31 static void bnxt_fill_msix_vecs(struct bnxt *bp, struct bnxt_msix_entry *ent)
33 struct bnxt_en_dev *edev = bp->edev;
36 if (!edev->ulp_tbl->msix_requested) {
37 netdev_warn(bp->dev, "Requested MSI-X vectors insufficient\n");
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;
44 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
45 ent[i].db_offset = bp->db_offset;
47 ent[i].db_offset = i * 0x80;
51 int bnxt_get_ulp_msix_num(struct bnxt *bp)
54 return bp->edev->ulp_num_msix_vec;
58 void bnxt_set_ulp_msix_num(struct bnxt *bp, int num)
61 bp->edev->ulp_num_msix_vec = num;
64 int bnxt_get_ulp_msix_num_in_use(struct bnxt *bp)
66 if (bnxt_ulp_registered(bp->edev))
67 return bp->edev->ulp_num_msix_vec;
71 int bnxt_get_ulp_stat_ctxs(struct bnxt *bp)
74 return bp->edev->ulp_num_ctxs;
78 void bnxt_set_ulp_stat_ctxs(struct bnxt *bp, int num_ulp_ctx)
81 bp->edev->ulp_num_ctxs = num_ulp_ctx;
84 int bnxt_get_ulp_stat_ctxs_in_use(struct bnxt *bp)
86 if (bnxt_ulp_registered(bp->edev))
87 return bp->edev->ulp_num_ctxs;
91 void bnxt_set_dflt_ulp_stat_ctxs(struct bnxt *bp)
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.
99 if (BNXT_PF(bp) && !bp->pf.port_id &&
101 bp->edev->ulp_num_ctxs++;
105 int bnxt_register_dev(struct bnxt_en_dev *edev,
106 struct bnxt_ulp_ops *ulp_ops,
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;
116 mutex_lock(&edev->en_dev_lock);
121 max_stat_ctxs = bnxt_get_max_func_stat_ctxs(bp);
122 if (max_stat_ctxs <= BNXT_MIN_ROCE_STAT_CTXS ||
123 bp->cp_nr_rings == max_stat_ctxs) {
129 ulp->handle = handle;
130 rcu_assign_pointer(ulp->ulp_ops, ulp_ops);
132 if (test_bit(BNXT_STATE_OPEN, &bp->state))
133 bnxt_hwrm_vnic_cfg(bp, &bp->vnic_info[BNXT_VNIC_DEFAULT]);
135 edev->ulp_tbl->msix_requested = bnxt_get_ulp_msix_num(bp);
137 bnxt_fill_msix_vecs(bp, bp->edev->msix_entries);
138 edev->flags |= BNXT_EN_FLAG_MSIX_REQUESTED;
140 mutex_unlock(&edev->en_dev_lock);
144 EXPORT_SYMBOL(bnxt_register_dev);
146 void bnxt_unregister_dev(struct bnxt_en_dev *edev)
148 struct net_device *dev = edev->net;
149 struct bnxt *bp = netdev_priv(dev);
150 struct bnxt_ulp *ulp;
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;
160 if (ulp->max_async_event_id)
161 bnxt_hwrm_func_drv_rgtr(bp, NULL, 0, true);
163 RCU_INIT_POINTER(ulp->ulp_ops, NULL);
165 ulp->max_async_event_id = 0;
166 ulp->async_events_bmap = NULL;
167 while (atomic_read(&ulp->ref_count) != 0 && i < 10) {
171 mutex_unlock(&edev->en_dev_lock);
175 EXPORT_SYMBOL(bnxt_unregister_dev);
177 static int bnxt_set_dflt_ulp_msix(struct bnxt *bp)
179 u32 roce_msix = BNXT_VF(bp) ?
180 BNXT_MAX_VF_ROCE_MSIX : BNXT_MAX_ROCE_MSIX;
182 return ((bp->flags & BNXT_FLAG_ROCE_CAP) ?
183 min_t(u32, roce_msix, num_online_cpus()) : 0);
186 int bnxt_send_msg(struct bnxt_en_dev *edev,
187 struct bnxt_fw_msg *fw_msg)
189 struct net_device *dev = edev->net;
190 struct bnxt *bp = netdev_priv(dev);
196 if (bp->fw_reset_state)
199 rc = hwrm_req_init(bp, req, 0 /* don't care */);
203 rc = hwrm_req_replace(bp, req, fw_msg->msg, fw_msg->msg_len);
207 hwrm_req_timeout(bp, req, fw_msg->timeout);
208 resp = hwrm_req_hold(bp, req);
209 rc = hwrm_req_send(bp, req);
210 resp_len = le16_to_cpu(resp->resp_len);
212 if (fw_msg->resp_max_len < resp_len)
213 resp_len = fw_msg->resp_max_len;
215 memcpy(fw_msg->resp, resp, resp_len);
217 hwrm_req_drop(bp, req);
220 EXPORT_SYMBOL(bnxt_send_msg);
222 void bnxt_ulp_stop(struct bnxt *bp)
224 struct bnxt_aux_priv *aux_priv = bp->aux_priv;
225 struct bnxt_en_dev *edev = bp->edev;
230 mutex_lock(&edev->en_dev_lock);
231 if (!bnxt_ulp_registered(edev)) {
232 mutex_unlock(&edev->en_dev_lock);
236 edev->flags |= BNXT_EN_FLAG_ULP_STOPPED;
238 struct auxiliary_device *adev;
240 adev = &aux_priv->aux_dev;
241 if (adev->dev.driver) {
242 const struct auxiliary_driver *adrv;
243 pm_message_t pm = {};
245 adrv = to_auxiliary_drv(adev->dev.driver);
246 edev->en_state = bp->state;
247 adrv->suspend(adev, pm);
250 mutex_unlock(&edev->en_dev_lock);
253 void bnxt_ulp_start(struct bnxt *bp, int err)
255 struct bnxt_aux_priv *aux_priv = bp->aux_priv;
256 struct bnxt_en_dev *edev = bp->edev;
261 edev->flags &= ~BNXT_EN_FLAG_ULP_STOPPED;
266 mutex_lock(&edev->en_dev_lock);
267 if (!bnxt_ulp_registered(edev)) {
268 mutex_unlock(&edev->en_dev_lock);
272 if (edev->ulp_tbl->msix_requested)
273 bnxt_fill_msix_vecs(bp, edev->msix_entries);
276 struct auxiliary_device *adev;
278 adev = &aux_priv->aux_dev;
279 if (adev->dev.driver) {
280 const struct auxiliary_driver *adrv;
282 adrv = to_auxiliary_drv(adev->dev.driver);
283 edev->en_state = bp->state;
287 mutex_unlock(&edev->en_dev_lock);
290 void bnxt_ulp_irq_stop(struct bnxt *bp)
292 struct bnxt_en_dev *edev = bp->edev;
293 struct bnxt_ulp_ops *ops;
295 if (!edev || !(edev->flags & BNXT_EN_FLAG_MSIX_REQUESTED))
298 if (bnxt_ulp_registered(bp->edev)) {
299 struct bnxt_ulp *ulp = edev->ulp_tbl;
301 if (!ulp->msix_requested)
304 ops = rtnl_dereference(ulp->ulp_ops);
305 if (!ops || !ops->ulp_irq_stop)
307 ops->ulp_irq_stop(ulp->handle);
311 void bnxt_ulp_irq_restart(struct bnxt *bp, int err)
313 struct bnxt_en_dev *edev = bp->edev;
314 struct bnxt_ulp_ops *ops;
316 if (!edev || !(edev->flags & BNXT_EN_FLAG_MSIX_REQUESTED))
319 if (bnxt_ulp_registered(bp->edev)) {
320 struct bnxt_ulp *ulp = edev->ulp_tbl;
321 struct bnxt_msix_entry *ent = NULL;
323 if (!ulp->msix_requested)
326 ops = rtnl_dereference(ulp->ulp_ops);
327 if (!ops || !ops->ulp_irq_restart)
331 ent = kcalloc(ulp->msix_requested, sizeof(*ent),
335 bnxt_fill_msix_vecs(bp, ent);
337 ops->ulp_irq_restart(ulp->handle, ent);
342 int bnxt_register_async_events(struct bnxt_en_dev *edev,
343 unsigned long *events_bmap,
346 struct net_device *dev = edev->net;
347 struct bnxt *bp = netdev_priv(dev);
348 struct bnxt_ulp *ulp;
351 ulp->async_events_bmap = events_bmap;
352 /* Make sure bnxt_ulp_async_events() sees this order */
354 ulp->max_async_event_id = max_id;
355 bnxt_hwrm_func_drv_rgtr(bp, events_bmap, max_id + 1, true);
358 EXPORT_SYMBOL(bnxt_register_async_events);
360 void bnxt_rdma_aux_device_uninit(struct bnxt *bp)
362 struct bnxt_aux_priv *aux_priv;
363 struct auxiliary_device *adev;
365 /* Skip if no auxiliary device init was done. */
369 aux_priv = bp->aux_priv;
370 adev = &aux_priv->aux_dev;
371 auxiliary_device_uninit(adev);
374 static void bnxt_aux_dev_release(struct device *dev)
376 struct bnxt_aux_priv *aux_priv =
377 container_of(dev, struct bnxt_aux_priv, aux_dev.dev);
378 struct bnxt *bp = netdev_priv(aux_priv->edev->net);
380 ida_free(&bnxt_aux_dev_ids, aux_priv->id);
381 kfree(aux_priv->edev->ulp_tbl);
383 kfree(aux_priv->edev);
388 void bnxt_rdma_aux_device_del(struct bnxt *bp)
393 auxiliary_device_delete(&bp->aux_priv->aux_dev);
396 static void bnxt_set_edev_info(struct bnxt_en_dev *edev, struct bnxt *bp)
399 edev->pdev = bp->pdev;
400 edev->l2_db_size = bp->db_size;
401 edev->l2_db_size_nc = bp->db_size;
402 edev->l2_db_offset = bp->db_offset;
403 mutex_init(&edev->en_dev_lock);
405 if (bp->flags & BNXT_FLAG_ROCEV1_CAP)
406 edev->flags |= BNXT_EN_FLAG_ROCEV1_CAP;
407 if (bp->flags & BNXT_FLAG_ROCEV2_CAP)
408 edev->flags |= BNXT_EN_FLAG_ROCEV2_CAP;
409 if (bp->flags & BNXT_FLAG_VF)
410 edev->flags |= BNXT_EN_FLAG_VF;
412 edev->chip_num = bp->chip_num;
413 edev->hw_ring_stats_size = bp->hw_ring_stats_size;
414 edev->pf_port_id = bp->pf.port_id;
415 edev->en_state = bp->state;
416 edev->bar0 = bp->bar0;
419 void bnxt_rdma_aux_device_add(struct bnxt *bp)
421 struct auxiliary_device *aux_dev;
427 aux_dev = &bp->aux_priv->aux_dev;
428 rc = auxiliary_device_add(aux_dev);
430 netdev_warn(bp->dev, "Failed to add auxiliary device for ROCE\n");
431 auxiliary_device_uninit(aux_dev);
432 bp->flags &= ~BNXT_FLAG_ROCE_CAP;
436 void bnxt_rdma_aux_device_init(struct bnxt *bp)
438 struct auxiliary_device *aux_dev;
439 struct bnxt_aux_priv *aux_priv;
440 struct bnxt_en_dev *edev;
441 struct bnxt_ulp *ulp;
444 if (!(bp->flags & BNXT_FLAG_ROCE_CAP))
447 aux_priv = kzalloc(sizeof(*bp->aux_priv), GFP_KERNEL);
451 aux_priv->id = ida_alloc(&bnxt_aux_dev_ids, GFP_KERNEL);
452 if (aux_priv->id < 0) {
454 "ida alloc failed for ROCE auxiliary device\n");
459 aux_dev = &aux_priv->aux_dev;
460 aux_dev->id = aux_priv->id;
461 aux_dev->name = "rdma";
462 aux_dev->dev.parent = &bp->pdev->dev;
463 aux_dev->dev.release = bnxt_aux_dev_release;
465 rc = auxiliary_device_init(aux_dev);
467 ida_free(&bnxt_aux_dev_ids, aux_priv->id);
471 bp->aux_priv = aux_priv;
473 /* From this point, all cleanup will happen via the .release callback &
474 * any error unwinding will need to include a call to
475 * auxiliary_device_uninit.
477 edev = kzalloc(sizeof(*edev), GFP_KERNEL);
481 aux_priv->edev = edev;
483 ulp = kzalloc(sizeof(*ulp), GFP_KERNEL);
489 bnxt_set_edev_info(edev, bp);
490 bp->ulp_num_msix_want = bnxt_set_dflt_ulp_msix(bp);
495 auxiliary_device_uninit(aux_dev);
497 bp->flags &= ~BNXT_FLAG_ROCE_CAP;