1 /* Broadcom NetXtreme-C/E network driver.
3 * Copyright (c) 2016 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>
27 static int bnxt_register_dev(struct bnxt_en_dev *edev, int ulp_id,
28 struct bnxt_ulp_ops *ulp_ops, void *handle)
30 struct net_device *dev = edev->net;
31 struct bnxt *bp = netdev_priv(dev);
35 if (ulp_id >= BNXT_MAX_ULP)
38 ulp = &edev->ulp_tbl[ulp_id];
39 if (rcu_access_pointer(ulp->ulp_ops)) {
40 netdev_err(bp->dev, "ulp id %d already registered\n", ulp_id);
43 if (ulp_id == BNXT_ROCE_ULP) {
44 unsigned int max_stat_ctxs;
46 max_stat_ctxs = bnxt_get_max_func_stat_ctxs(bp);
47 if (max_stat_ctxs <= BNXT_MIN_ROCE_STAT_CTXS ||
48 bp->num_stat_ctxs == max_stat_ctxs)
50 bnxt_set_max_func_stat_ctxs(bp, max_stat_ctxs -
51 BNXT_MIN_ROCE_STAT_CTXS);
54 atomic_set(&ulp->ref_count, 0);
56 rcu_assign_pointer(ulp->ulp_ops, ulp_ops);
58 if (ulp_id == BNXT_ROCE_ULP) {
59 if (test_bit(BNXT_STATE_OPEN, &bp->state))
60 bnxt_hwrm_vnic_cfg(bp, 0);
66 static int bnxt_unregister_dev(struct bnxt_en_dev *edev, int ulp_id)
68 struct net_device *dev = edev->net;
69 struct bnxt *bp = netdev_priv(dev);
74 if (ulp_id >= BNXT_MAX_ULP)
77 ulp = &edev->ulp_tbl[ulp_id];
78 if (!rcu_access_pointer(ulp->ulp_ops)) {
79 netdev_err(bp->dev, "ulp id %d not registered\n", ulp_id);
82 if (ulp_id == BNXT_ROCE_ULP) {
83 unsigned int max_stat_ctxs;
85 max_stat_ctxs = bnxt_get_max_func_stat_ctxs(bp);
86 bnxt_set_max_func_stat_ctxs(bp, max_stat_ctxs + 1);
87 if (ulp->msix_requested)
88 edev->en_ops->bnxt_free_msix(edev, ulp_id);
90 if (ulp->max_async_event_id)
91 bnxt_hwrm_func_rgtr_async_events(bp, NULL, 0);
93 RCU_INIT_POINTER(ulp->ulp_ops, NULL);
95 ulp->max_async_event_id = 0;
96 ulp->async_events_bmap = NULL;
97 while (atomic_read(&ulp->ref_count) != 0 && i < 10) {
104 static int bnxt_req_msix_vecs(struct bnxt_en_dev *edev, int ulp_id,
105 struct bnxt_msix_entry *ent, int num_msix)
107 struct net_device *dev = edev->net;
108 struct bnxt *bp = netdev_priv(dev);
109 int max_idx, max_cp_rings;
110 int avail_msix, i, idx;
114 if (ulp_id != BNXT_ROCE_ULP)
117 if (!(bp->flags & BNXT_FLAG_USING_MSIX))
120 if (edev->ulp_tbl[ulp_id].msix_requested)
123 max_cp_rings = bnxt_get_max_func_cp_rings(bp);
124 avail_msix = bnxt_get_avail_msix(bp, num_msix);
127 if (avail_msix > num_msix)
128 avail_msix = num_msix;
130 if (bp->flags & BNXT_FLAG_NEW_RM) {
131 idx = bp->cp_nr_rings;
133 max_idx = min_t(int, bp->total_irqs, max_cp_rings);
134 idx = max_idx - avail_msix;
136 edev->ulp_tbl[ulp_id].msix_base = idx;
137 edev->ulp_tbl[ulp_id].msix_requested = avail_msix;
138 if (bp->total_irqs < (idx + avail_msix)) {
139 if (netif_running(dev)) {
140 bnxt_close_nic(bp, true, false);
141 rc = bnxt_open_nic(bp, true, false);
143 rc = bnxt_reserve_rings(bp);
147 edev->ulp_tbl[ulp_id].msix_requested = 0;
151 if (bp->flags & BNXT_FLAG_NEW_RM) {
152 struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
154 avail_msix = hw_resc->resv_cp_rings - bp->cp_nr_rings;
155 edev->ulp_tbl[ulp_id].msix_requested = avail_msix;
157 for (i = 0; i < avail_msix; i++) {
158 ent[i].vector = bp->irq_tbl[idx + i].vector;
159 ent[i].ring_idx = idx + i;
160 ent[i].db_offset = (idx + i) * 0x80;
162 bnxt_set_max_func_irqs(bp, bnxt_get_max_func_irqs(bp) - avail_msix);
163 bnxt_set_max_func_cp_rings(bp, max_cp_rings - avail_msix);
167 static int bnxt_free_msix_vecs(struct bnxt_en_dev *edev, int ulp_id)
169 struct net_device *dev = edev->net;
170 struct bnxt *bp = netdev_priv(dev);
171 int max_cp_rings, msix_requested;
174 if (ulp_id != BNXT_ROCE_ULP)
177 max_cp_rings = bnxt_get_max_func_cp_rings(bp);
178 msix_requested = edev->ulp_tbl[ulp_id].msix_requested;
179 bnxt_set_max_func_cp_rings(bp, max_cp_rings + msix_requested);
180 edev->ulp_tbl[ulp_id].msix_requested = 0;
181 bnxt_set_max_func_irqs(bp, bnxt_get_max_func_irqs(bp) + msix_requested);
182 if (netif_running(dev)) {
183 bnxt_close_nic(bp, true, false);
184 bnxt_open_nic(bp, true, false);
189 int bnxt_get_ulp_msix_num(struct bnxt *bp)
191 if (bnxt_ulp_registered(bp->edev, BNXT_ROCE_ULP)) {
192 struct bnxt_en_dev *edev = bp->edev;
194 return edev->ulp_tbl[BNXT_ROCE_ULP].msix_requested;
199 int bnxt_get_ulp_msix_base(struct bnxt *bp)
201 if (bnxt_ulp_registered(bp->edev, BNXT_ROCE_ULP)) {
202 struct bnxt_en_dev *edev = bp->edev;
204 if (edev->ulp_tbl[BNXT_ROCE_ULP].msix_requested)
205 return edev->ulp_tbl[BNXT_ROCE_ULP].msix_base;
210 void bnxt_subtract_ulp_resources(struct bnxt *bp, int ulp_id)
213 if (bnxt_ulp_registered(bp->edev, ulp_id)) {
214 struct bnxt_en_dev *edev = bp->edev;
215 unsigned int msix_req, max;
217 msix_req = edev->ulp_tbl[ulp_id].msix_requested;
218 max = bnxt_get_max_func_cp_rings(bp);
219 bnxt_set_max_func_cp_rings(bp, max - msix_req);
220 max = bnxt_get_max_func_stat_ctxs(bp);
221 bnxt_set_max_func_stat_ctxs(bp, max - 1);
225 static int bnxt_send_msg(struct bnxt_en_dev *edev, int ulp_id,
226 struct bnxt_fw_msg *fw_msg)
228 struct net_device *dev = edev->net;
229 struct bnxt *bp = netdev_priv(dev);
233 mutex_lock(&bp->hwrm_cmd_lock);
235 req->resp_addr = cpu_to_le64(bp->hwrm_cmd_resp_dma_addr);
236 rc = _hwrm_send_message(bp, fw_msg->msg, fw_msg->msg_len,
239 struct output *resp = bp->hwrm_cmd_resp_addr;
240 u32 len = le16_to_cpu(resp->resp_len);
242 if (fw_msg->resp_max_len < len)
243 len = fw_msg->resp_max_len;
245 memcpy(fw_msg->resp, resp, len);
247 mutex_unlock(&bp->hwrm_cmd_lock);
251 static void bnxt_ulp_get(struct bnxt_ulp *ulp)
253 atomic_inc(&ulp->ref_count);
256 static void bnxt_ulp_put(struct bnxt_ulp *ulp)
258 atomic_dec(&ulp->ref_count);
261 void bnxt_ulp_stop(struct bnxt *bp)
263 struct bnxt_en_dev *edev = bp->edev;
264 struct bnxt_ulp_ops *ops;
270 for (i = 0; i < BNXT_MAX_ULP; i++) {
271 struct bnxt_ulp *ulp = &edev->ulp_tbl[i];
273 ops = rtnl_dereference(ulp->ulp_ops);
274 if (!ops || !ops->ulp_stop)
276 ops->ulp_stop(ulp->handle);
280 void bnxt_ulp_start(struct bnxt *bp)
282 struct bnxt_en_dev *edev = bp->edev;
283 struct bnxt_ulp_ops *ops;
289 for (i = 0; i < BNXT_MAX_ULP; i++) {
290 struct bnxt_ulp *ulp = &edev->ulp_tbl[i];
292 ops = rtnl_dereference(ulp->ulp_ops);
293 if (!ops || !ops->ulp_start)
295 ops->ulp_start(ulp->handle);
299 void bnxt_ulp_sriov_cfg(struct bnxt *bp, int num_vfs)
301 struct bnxt_en_dev *edev = bp->edev;
302 struct bnxt_ulp_ops *ops;
308 for (i = 0; i < BNXT_MAX_ULP; i++) {
309 struct bnxt_ulp *ulp = &edev->ulp_tbl[i];
312 ops = rcu_dereference(ulp->ulp_ops);
313 if (!ops || !ops->ulp_sriov_config) {
319 ops->ulp_sriov_config(ulp->handle, num_vfs);
324 void bnxt_ulp_shutdown(struct bnxt *bp)
326 struct bnxt_en_dev *edev = bp->edev;
327 struct bnxt_ulp_ops *ops;
333 for (i = 0; i < BNXT_MAX_ULP; i++) {
334 struct bnxt_ulp *ulp = &edev->ulp_tbl[i];
336 ops = rtnl_dereference(ulp->ulp_ops);
337 if (!ops || !ops->ulp_shutdown)
339 ops->ulp_shutdown(ulp->handle);
343 void bnxt_ulp_async_events(struct bnxt *bp, struct hwrm_async_event_cmpl *cmpl)
345 u16 event_id = le16_to_cpu(cmpl->event_id);
346 struct bnxt_en_dev *edev = bp->edev;
347 struct bnxt_ulp_ops *ops;
354 for (i = 0; i < BNXT_MAX_ULP; i++) {
355 struct bnxt_ulp *ulp = &edev->ulp_tbl[i];
357 ops = rcu_dereference(ulp->ulp_ops);
358 if (!ops || !ops->ulp_async_notifier)
360 if (!ulp->async_events_bmap ||
361 event_id > ulp->max_async_event_id)
364 /* Read max_async_event_id first before testing the bitmap. */
366 if (test_bit(event_id, ulp->async_events_bmap))
367 ops->ulp_async_notifier(ulp->handle, cmpl);
372 static int bnxt_register_async_events(struct bnxt_en_dev *edev, int ulp_id,
373 unsigned long *events_bmap, u16 max_id)
375 struct net_device *dev = edev->net;
376 struct bnxt *bp = netdev_priv(dev);
377 struct bnxt_ulp *ulp;
379 if (ulp_id >= BNXT_MAX_ULP)
382 ulp = &edev->ulp_tbl[ulp_id];
383 ulp->async_events_bmap = events_bmap;
384 /* Make sure bnxt_ulp_async_events() sees this order */
386 ulp->max_async_event_id = max_id;
387 bnxt_hwrm_func_rgtr_async_events(bp, events_bmap, max_id + 1);
391 static const struct bnxt_en_ops bnxt_en_ops_tbl = {
392 .bnxt_register_device = bnxt_register_dev,
393 .bnxt_unregister_device = bnxt_unregister_dev,
394 .bnxt_request_msix = bnxt_req_msix_vecs,
395 .bnxt_free_msix = bnxt_free_msix_vecs,
396 .bnxt_send_fw_msg = bnxt_send_msg,
397 .bnxt_register_fw_async_events = bnxt_register_async_events,
400 struct bnxt_en_dev *bnxt_ulp_probe(struct net_device *dev)
402 struct bnxt *bp = netdev_priv(dev);
403 struct bnxt_en_dev *edev;
407 edev = kzalloc(sizeof(*edev), GFP_KERNEL);
409 return ERR_PTR(-ENOMEM);
410 edev->en_ops = &bnxt_en_ops_tbl;
411 if (bp->flags & BNXT_FLAG_ROCEV1_CAP)
412 edev->flags |= BNXT_EN_FLAG_ROCEV1_CAP;
413 if (bp->flags & BNXT_FLAG_ROCEV2_CAP)
414 edev->flags |= BNXT_EN_FLAG_ROCEV2_CAP;
416 edev->pdev = bp->pdev;