blob: 77da75a55c0200edf5229d1dd88939473f85194c [file] [log] [blame]
Michael Chana588e452016-12-07 00:26:21 -05001/* Broadcom NetXtreme-C/E network driver.
2 *
3 * Copyright (c) 2016 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
23#include "bnxt_hsi.h"
24#include "bnxt.h"
25#include "bnxt_ulp.h"
26
27static int bnxt_register_dev(struct bnxt_en_dev *edev, int ulp_id,
28 struct bnxt_ulp_ops *ulp_ops, void *handle)
29{
30 struct net_device *dev = edev->net;
31 struct bnxt *bp = netdev_priv(dev);
32 struct bnxt_ulp *ulp;
33
34 ASSERT_RTNL();
35 if (ulp_id >= BNXT_MAX_ULP)
36 return -EINVAL;
37
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);
41 return -EBUSY;
42 }
43 if (ulp_id == BNXT_ROCE_ULP) {
44 unsigned int max_stat_ctxs;
45
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)
49 return -ENOMEM;
50 bnxt_set_max_func_stat_ctxs(bp, max_stat_ctxs -
51 BNXT_MIN_ROCE_STAT_CTXS);
52 }
53
54 atomic_set(&ulp->ref_count, 0);
55 ulp->handle = handle;
56 rcu_assign_pointer(ulp->ulp_ops, ulp_ops);
57
58 if (ulp_id == BNXT_ROCE_ULP) {
59 if (test_bit(BNXT_STATE_OPEN, &bp->state))
60 bnxt_hwrm_vnic_cfg(bp, 0);
61 }
62
63 return 0;
64}
65
66static int bnxt_unregister_dev(struct bnxt_en_dev *edev, int ulp_id)
67{
68 struct net_device *dev = edev->net;
69 struct bnxt *bp = netdev_priv(dev);
70 struct bnxt_ulp *ulp;
71 int i = 0;
72
73 ASSERT_RTNL();
74 if (ulp_id >= BNXT_MAX_ULP)
75 return -EINVAL;
76
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);
80 return -EINVAL;
81 }
82 if (ulp_id == BNXT_ROCE_ULP) {
83 unsigned int max_stat_ctxs;
84
85 max_stat_ctxs = bnxt_get_max_func_stat_ctxs(bp);
86 bnxt_set_max_func_stat_ctxs(bp, max_stat_ctxs + 1);
87 }
88 if (ulp->max_async_event_id)
89 bnxt_hwrm_func_rgtr_async_events(bp, NULL, 0);
90
91 RCU_INIT_POINTER(ulp->ulp_ops, NULL);
92 synchronize_rcu();
93 ulp->max_async_event_id = 0;
94 ulp->async_events_bmap = NULL;
95 while (atomic_read(&ulp->ref_count) != 0 && i < 10) {
96 msleep(100);
97 i++;
98 }
99 return 0;
100}
101
102static int bnxt_req_msix_vecs(struct bnxt_en_dev *edev, int ulp_id,
103 struct bnxt_msix_entry *ent, int num_msix)
104{
105 struct net_device *dev = edev->net;
106 struct bnxt *bp = netdev_priv(dev);
107 int max_idx, max_cp_rings;
108 int avail_msix, i, idx;
109
110 ASSERT_RTNL();
111 if (ulp_id != BNXT_ROCE_ULP)
112 return -EINVAL;
113
114 if (!(bp->flags & BNXT_FLAG_USING_MSIX))
115 return -ENODEV;
116
117 max_cp_rings = bnxt_get_max_func_cp_rings(bp);
118 max_idx = min_t(int, bp->total_irqs, max_cp_rings);
119 avail_msix = max_idx - bp->cp_nr_rings;
120 if (!avail_msix)
121 return -ENOMEM;
122 if (avail_msix > num_msix)
123 avail_msix = num_msix;
124
125 idx = max_idx - avail_msix;
126 for (i = 0; i < avail_msix; i++) {
127 ent[i].vector = bp->irq_tbl[idx + i].vector;
128 ent[i].ring_idx = idx + i;
129 ent[i].db_offset = (idx + i) * 0x80;
130 }
131 bnxt_set_max_func_irqs(bp, max_idx - avail_msix);
132 bnxt_set_max_func_cp_rings(bp, max_cp_rings - avail_msix);
133 edev->ulp_tbl[ulp_id].msix_requested = avail_msix;
134 return avail_msix;
135}
136
137static int bnxt_free_msix_vecs(struct bnxt_en_dev *edev, int ulp_id)
138{
139 struct net_device *dev = edev->net;
140 struct bnxt *bp = netdev_priv(dev);
141 int max_cp_rings, msix_requested;
142
143 ASSERT_RTNL();
144 if (ulp_id != BNXT_ROCE_ULP)
145 return -EINVAL;
146
147 max_cp_rings = bnxt_get_max_func_cp_rings(bp);
148 msix_requested = edev->ulp_tbl[ulp_id].msix_requested;
149 bnxt_set_max_func_cp_rings(bp, max_cp_rings + msix_requested);
150 edev->ulp_tbl[ulp_id].msix_requested = 0;
151 bnxt_set_max_func_irqs(bp, bp->total_irqs);
152 return 0;
153}
154
155void bnxt_subtract_ulp_resources(struct bnxt *bp, int ulp_id)
156{
157 ASSERT_RTNL();
158 if (bnxt_ulp_registered(bp->edev, ulp_id)) {
159 struct bnxt_en_dev *edev = bp->edev;
160 unsigned int msix_req, max;
161
162 msix_req = edev->ulp_tbl[ulp_id].msix_requested;
163 max = bnxt_get_max_func_cp_rings(bp);
164 bnxt_set_max_func_cp_rings(bp, max - msix_req);
165 max = bnxt_get_max_func_stat_ctxs(bp);
166 bnxt_set_max_func_stat_ctxs(bp, max - 1);
167 }
168}
169
170static int bnxt_send_msg(struct bnxt_en_dev *edev, int ulp_id,
171 struct bnxt_fw_msg *fw_msg)
172{
173 struct net_device *dev = edev->net;
174 struct bnxt *bp = netdev_priv(dev);
175 struct input *req;
176 int rc;
177
178 mutex_lock(&bp->hwrm_cmd_lock);
179 req = fw_msg->msg;
180 req->resp_addr = cpu_to_le64(bp->hwrm_cmd_resp_dma_addr);
181 rc = _hwrm_send_message(bp, fw_msg->msg, fw_msg->msg_len,
182 fw_msg->timeout);
183 if (!rc) {
184 struct output *resp = bp->hwrm_cmd_resp_addr;
185 u32 len = le16_to_cpu(resp->resp_len);
186
187 if (fw_msg->resp_max_len < len)
188 len = fw_msg->resp_max_len;
189
190 memcpy(fw_msg->resp, resp, len);
191 }
192 mutex_unlock(&bp->hwrm_cmd_lock);
193 return rc;
194}
195
196static void bnxt_ulp_get(struct bnxt_ulp *ulp)
197{
198 atomic_inc(&ulp->ref_count);
199}
200
201static void bnxt_ulp_put(struct bnxt_ulp *ulp)
202{
203 atomic_dec(&ulp->ref_count);
204}
205
206void bnxt_ulp_stop(struct bnxt *bp)
207{
208 struct bnxt_en_dev *edev = bp->edev;
209 struct bnxt_ulp_ops *ops;
210 int i;
211
212 if (!edev)
213 return;
214
215 for (i = 0; i < BNXT_MAX_ULP; i++) {
216 struct bnxt_ulp *ulp = &edev->ulp_tbl[i];
217
218 ops = rtnl_dereference(ulp->ulp_ops);
219 if (!ops || !ops->ulp_stop)
220 continue;
221 ops->ulp_stop(ulp->handle);
222 }
223}
224
225void bnxt_ulp_start(struct bnxt *bp)
226{
227 struct bnxt_en_dev *edev = bp->edev;
228 struct bnxt_ulp_ops *ops;
229 int i;
230
231 if (!edev)
232 return;
233
234 for (i = 0; i < BNXT_MAX_ULP; i++) {
235 struct bnxt_ulp *ulp = &edev->ulp_tbl[i];
236
237 ops = rtnl_dereference(ulp->ulp_ops);
238 if (!ops || !ops->ulp_start)
239 continue;
240 ops->ulp_start(ulp->handle);
241 }
242}
243
244void bnxt_ulp_sriov_cfg(struct bnxt *bp, int num_vfs)
245{
246 struct bnxt_en_dev *edev = bp->edev;
247 struct bnxt_ulp_ops *ops;
248 int i;
249
250 if (!edev)
251 return;
252
253 for (i = 0; i < BNXT_MAX_ULP; i++) {
254 struct bnxt_ulp *ulp = &edev->ulp_tbl[i];
255
256 rcu_read_lock();
257 ops = rcu_dereference(ulp->ulp_ops);
258 if (!ops || !ops->ulp_sriov_config) {
259 rcu_read_unlock();
260 continue;
261 }
262 bnxt_ulp_get(ulp);
263 rcu_read_unlock();
264 ops->ulp_sriov_config(ulp->handle, num_vfs);
265 bnxt_ulp_put(ulp);
266 }
267}
268
Michael Chan0efd2fc2017-05-29 19:06:06 -0400269void bnxt_ulp_shutdown(struct bnxt *bp)
270{
271 struct bnxt_en_dev *edev = bp->edev;
272 struct bnxt_ulp_ops *ops;
273 int i;
274
275 if (!edev)
276 return;
277
278 for (i = 0; i < BNXT_MAX_ULP; i++) {
279 struct bnxt_ulp *ulp = &edev->ulp_tbl[i];
280
281 ops = rtnl_dereference(ulp->ulp_ops);
282 if (!ops || !ops->ulp_shutdown)
283 continue;
284 ops->ulp_shutdown(ulp->handle);
285 }
286}
287
Michael Chana588e452016-12-07 00:26:21 -0500288void bnxt_ulp_async_events(struct bnxt *bp, struct hwrm_async_event_cmpl *cmpl)
289{
290 u16 event_id = le16_to_cpu(cmpl->event_id);
291 struct bnxt_en_dev *edev = bp->edev;
292 struct bnxt_ulp_ops *ops;
293 int i;
294
295 if (!edev)
296 return;
297
298 rcu_read_lock();
299 for (i = 0; i < BNXT_MAX_ULP; i++) {
300 struct bnxt_ulp *ulp = &edev->ulp_tbl[i];
301
302 ops = rcu_dereference(ulp->ulp_ops);
303 if (!ops || !ops->ulp_async_notifier)
304 continue;
305 if (!ulp->async_events_bmap ||
306 event_id > ulp->max_async_event_id)
307 continue;
308
309 /* Read max_async_event_id first before testing the bitmap. */
310 smp_rmb();
311 if (test_bit(event_id, ulp->async_events_bmap))
312 ops->ulp_async_notifier(ulp->handle, cmpl);
313 }
314 rcu_read_unlock();
315}
316
317static int bnxt_register_async_events(struct bnxt_en_dev *edev, int ulp_id,
318 unsigned long *events_bmap, u16 max_id)
319{
320 struct net_device *dev = edev->net;
321 struct bnxt *bp = netdev_priv(dev);
322 struct bnxt_ulp *ulp;
323
324 if (ulp_id >= BNXT_MAX_ULP)
325 return -EINVAL;
326
327 ulp = &edev->ulp_tbl[ulp_id];
328 ulp->async_events_bmap = events_bmap;
329 /* Make sure bnxt_ulp_async_events() sees this order */
330 smp_wmb();
331 ulp->max_async_event_id = max_id;
332 bnxt_hwrm_func_rgtr_async_events(bp, events_bmap, max_id + 1);
333 return 0;
334}
335
336static const struct bnxt_en_ops bnxt_en_ops_tbl = {
337 .bnxt_register_device = bnxt_register_dev,
338 .bnxt_unregister_device = bnxt_unregister_dev,
339 .bnxt_request_msix = bnxt_req_msix_vecs,
340 .bnxt_free_msix = bnxt_free_msix_vecs,
341 .bnxt_send_fw_msg = bnxt_send_msg,
342 .bnxt_register_fw_async_events = bnxt_register_async_events,
343};
344
345struct bnxt_en_dev *bnxt_ulp_probe(struct net_device *dev)
346{
347 struct bnxt *bp = netdev_priv(dev);
348 struct bnxt_en_dev *edev;
349
350 edev = bp->edev;
351 if (!edev) {
352 edev = kzalloc(sizeof(*edev), GFP_KERNEL);
353 if (!edev)
354 return ERR_PTR(-ENOMEM);
355 edev->en_ops = &bnxt_en_ops_tbl;
356 if (bp->flags & BNXT_FLAG_ROCEV1_CAP)
357 edev->flags |= BNXT_EN_FLAG_ROCEV1_CAP;
358 if (bp->flags & BNXT_FLAG_ROCEV2_CAP)
359 edev->flags |= BNXT_EN_FLAG_ROCEV2_CAP;
360 edev->net = dev;
361 edev->pdev = bp->pdev;
362 bp->edev = edev;
363 }
364 return bp->edev;
365}