1
2
3
4
5
6
7
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->cp_nr_rings == max_stat_ctxs)
49 return -ENOMEM;
50 }
51
52 atomic_set(&ulp->ref_count, 0);
53 ulp->handle = handle;
54 rcu_assign_pointer(ulp->ulp_ops, ulp_ops);
55
56 if (ulp_id == BNXT_ROCE_ULP) {
57 if (test_bit(BNXT_STATE_OPEN, &bp->state))
58 bnxt_hwrm_vnic_cfg(bp, 0);
59 }
60
61 return 0;
62}
63
64static int bnxt_unregister_dev(struct bnxt_en_dev *edev, int ulp_id)
65{
66 struct net_device *dev = edev->net;
67 struct bnxt *bp = netdev_priv(dev);
68 struct bnxt_ulp *ulp;
69 int i = 0;
70
71 ASSERT_RTNL();
72 if (ulp_id >= BNXT_MAX_ULP)
73 return -EINVAL;
74
75 ulp = &edev->ulp_tbl[ulp_id];
76 if (!rcu_access_pointer(ulp->ulp_ops)) {
77 netdev_err(bp->dev, "ulp id %d not registered\n", ulp_id);
78 return -EINVAL;
79 }
80 if (ulp_id == BNXT_ROCE_ULP && ulp->msix_requested)
81 edev->en_ops->bnxt_free_msix(edev, ulp_id);
82
83 if (ulp->max_async_event_id)
84 bnxt_hwrm_func_rgtr_async_events(bp, NULL, 0);
85
86 RCU_INIT_POINTER(ulp->ulp_ops, NULL);
87 synchronize_rcu();
88 ulp->max_async_event_id = 0;
89 ulp->async_events_bmap = NULL;
90 while (atomic_read(&ulp->ref_count) != 0 && i < 10) {
91 msleep(100);
92 i++;
93 }
94 return 0;
95}
96
97static void bnxt_fill_msix_vecs(struct bnxt *bp, struct bnxt_msix_entry *ent)
98{
99 struct bnxt_en_dev *edev = bp->edev;
100 int num_msix, idx, i;
101
102 num_msix = edev->ulp_tbl[BNXT_ROCE_ULP].msix_requested;
103 idx = edev->ulp_tbl[BNXT_ROCE_ULP].msix_base;
104 for (i = 0; i < num_msix; i++) {
105 ent[i].vector = bp->irq_tbl[idx + i].vector;
106 ent[i].ring_idx = idx + i;
107 ent[i].db_offset = (idx + i) * 0x80;
108 }
109}
110
111static int bnxt_req_msix_vecs(struct bnxt_en_dev *edev, int ulp_id,
112 struct bnxt_msix_entry *ent, int num_msix)
113{
114 struct net_device *dev = edev->net;
115 struct bnxt *bp = netdev_priv(dev);
116 int max_idx, max_cp_rings;
117 int avail_msix, idx;
118 int rc = 0;
119
120 ASSERT_RTNL();
121 if (ulp_id != BNXT_ROCE_ULP)
122 return -EINVAL;
123
124 if (!(bp->flags & BNXT_FLAG_USING_MSIX))
125 return -ENODEV;
126
127 if (edev->ulp_tbl[ulp_id].msix_requested)
128 return -EAGAIN;
129
130 max_cp_rings = bnxt_get_max_func_cp_rings(bp);
131 avail_msix = bnxt_get_avail_msix(bp, num_msix);
132 if (!avail_msix)
133 return -ENOMEM;
134 if (avail_msix > num_msix)
135 avail_msix = num_msix;
136
137 if (BNXT_NEW_RM(bp)) {
138 idx = bp->cp_nr_rings;
139 } else {
140 max_idx = min_t(int, bp->total_irqs, max_cp_rings);
141 idx = max_idx - avail_msix;
142 }
143 edev->ulp_tbl[ulp_id].msix_base = idx;
144 edev->ulp_tbl[ulp_id].msix_requested = avail_msix;
145 if (bp->total_irqs < (idx + avail_msix)) {
146 if (netif_running(dev)) {
147 bnxt_close_nic(bp, true, false);
148 rc = bnxt_open_nic(bp, true, false);
149 } else {
150 rc = bnxt_reserve_rings(bp);
151 }
152 }
153 if (rc) {
154 edev->ulp_tbl[ulp_id].msix_requested = 0;
155 return -EAGAIN;
156 }
157
158 if (BNXT_NEW_RM(bp)) {
159 struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
160
161 avail_msix = hw_resc->resv_irqs - bp->cp_nr_rings;
162 edev->ulp_tbl[ulp_id].msix_requested = avail_msix;
163 }
164 bnxt_fill_msix_vecs(bp, ent);
165 edev->flags |= BNXT_EN_FLAG_MSIX_REQUESTED;
166 return avail_msix;
167}
168
169static int bnxt_free_msix_vecs(struct bnxt_en_dev *edev, int ulp_id)
170{
171 struct net_device *dev = edev->net;
172 struct bnxt *bp = netdev_priv(dev);
173
174 ASSERT_RTNL();
175 if (ulp_id != BNXT_ROCE_ULP)
176 return -EINVAL;
177
178 if (!(edev->flags & BNXT_EN_FLAG_MSIX_REQUESTED))
179 return 0;
180
181 edev->ulp_tbl[ulp_id].msix_requested = 0;
182 edev->flags &= ~BNXT_EN_FLAG_MSIX_REQUESTED;
183 if (netif_running(dev)) {
184 bnxt_close_nic(bp, true, false);
185 bnxt_open_nic(bp, true, false);
186 }
187 return 0;
188}
189
190int bnxt_get_ulp_msix_num(struct bnxt *bp)
191{
192 if (bnxt_ulp_registered(bp->edev, BNXT_ROCE_ULP)) {
193 struct bnxt_en_dev *edev = bp->edev;
194
195 return edev->ulp_tbl[BNXT_ROCE_ULP].msix_requested;
196 }
197 return 0;
198}
199
200int bnxt_get_ulp_msix_base(struct bnxt *bp)
201{
202 if (bnxt_ulp_registered(bp->edev, BNXT_ROCE_ULP)) {
203 struct bnxt_en_dev *edev = bp->edev;
204
205 if (edev->ulp_tbl[BNXT_ROCE_ULP].msix_requested)
206 return edev->ulp_tbl[BNXT_ROCE_ULP].msix_base;
207 }
208 return 0;
209}
210
211int bnxt_get_ulp_stat_ctxs(struct bnxt *bp)
212{
213 if (bnxt_ulp_registered(bp->edev, BNXT_ROCE_ULP))
214 return BNXT_MIN_ROCE_STAT_CTXS;
215
216 return 0;
217}
218
219static int bnxt_send_msg(struct bnxt_en_dev *edev, int ulp_id,
220 struct bnxt_fw_msg *fw_msg)
221{
222 struct net_device *dev = edev->net;
223 struct bnxt *bp = netdev_priv(dev);
224 struct input *req;
225 int rc;
226
227 mutex_lock(&bp->hwrm_cmd_lock);
228 req = fw_msg->msg;
229 req->resp_addr = cpu_to_le64(bp->hwrm_cmd_resp_dma_addr);
230 rc = _hwrm_send_message(bp, fw_msg->msg, fw_msg->msg_len,
231 fw_msg->timeout);
232 if (!rc) {
233 struct output *resp = bp->hwrm_cmd_resp_addr;
234 u32 len = le16_to_cpu(resp->resp_len);
235
236 if (fw_msg->resp_max_len < len)
237 len = fw_msg->resp_max_len;
238
239 memcpy(fw_msg->resp, resp, len);
240 }
241 mutex_unlock(&bp->hwrm_cmd_lock);
242 return rc;
243}
244
245static void bnxt_ulp_get(struct bnxt_ulp *ulp)
246{
247 atomic_inc(&ulp->ref_count);
248}
249
250static void bnxt_ulp_put(struct bnxt_ulp *ulp)
251{
252 atomic_dec(&ulp->ref_count);
253}
254
255void bnxt_ulp_stop(struct bnxt *bp)
256{
257 struct bnxt_en_dev *edev = bp->edev;
258 struct bnxt_ulp_ops *ops;
259 int i;
260
261 if (!edev)
262 return;
263
264 for (i = 0; i < BNXT_MAX_ULP; i++) {
265 struct bnxt_ulp *ulp = &edev->ulp_tbl[i];
266
267 ops = rtnl_dereference(ulp->ulp_ops);
268 if (!ops || !ops->ulp_stop)
269 continue;
270 ops->ulp_stop(ulp->handle);
271 }
272}
273
274void bnxt_ulp_start(struct bnxt *bp)
275{
276 struct bnxt_en_dev *edev = bp->edev;
277 struct bnxt_ulp_ops *ops;
278 int i;
279
280 if (!edev)
281 return;
282
283 for (i = 0; i < BNXT_MAX_ULP; i++) {
284 struct bnxt_ulp *ulp = &edev->ulp_tbl[i];
285
286 ops = rtnl_dereference(ulp->ulp_ops);
287 if (!ops || !ops->ulp_start)
288 continue;
289 ops->ulp_start(ulp->handle);
290 }
291}
292
293void bnxt_ulp_sriov_cfg(struct bnxt *bp, int num_vfs)
294{
295 struct bnxt_en_dev *edev = bp->edev;
296 struct bnxt_ulp_ops *ops;
297 int i;
298
299 if (!edev)
300 return;
301
302 for (i = 0; i < BNXT_MAX_ULP; i++) {
303 struct bnxt_ulp *ulp = &edev->ulp_tbl[i];
304
305 rcu_read_lock();
306 ops = rcu_dereference(ulp->ulp_ops);
307 if (!ops || !ops->ulp_sriov_config) {
308 rcu_read_unlock();
309 continue;
310 }
311 bnxt_ulp_get(ulp);
312 rcu_read_unlock();
313 ops->ulp_sriov_config(ulp->handle, num_vfs);
314 bnxt_ulp_put(ulp);
315 }
316}
317
318void bnxt_ulp_shutdown(struct bnxt *bp)
319{
320 struct bnxt_en_dev *edev = bp->edev;
321 struct bnxt_ulp_ops *ops;
322 int i;
323
324 if (!edev)
325 return;
326
327 for (i = 0; i < BNXT_MAX_ULP; i++) {
328 struct bnxt_ulp *ulp = &edev->ulp_tbl[i];
329
330 ops = rtnl_dereference(ulp->ulp_ops);
331 if (!ops || !ops->ulp_shutdown)
332 continue;
333 ops->ulp_shutdown(ulp->handle);
334 }
335}
336
337void bnxt_ulp_irq_stop(struct bnxt *bp)
338{
339 struct bnxt_en_dev *edev = bp->edev;
340 struct bnxt_ulp_ops *ops;
341
342 if (!edev || !(edev->flags & BNXT_EN_FLAG_MSIX_REQUESTED))
343 return;
344
345 if (bnxt_ulp_registered(bp->edev, BNXT_ROCE_ULP)) {
346 struct bnxt_ulp *ulp = &edev->ulp_tbl[BNXT_ROCE_ULP];
347
348 if (!ulp->msix_requested)
349 return;
350
351 ops = rtnl_dereference(ulp->ulp_ops);
352 if (!ops || !ops->ulp_irq_stop)
353 return;
354 ops->ulp_irq_stop(ulp->handle);
355 }
356}
357
358void bnxt_ulp_irq_restart(struct bnxt *bp, int err)
359{
360 struct bnxt_en_dev *edev = bp->edev;
361 struct bnxt_ulp_ops *ops;
362
363 if (!edev || !(edev->flags & BNXT_EN_FLAG_MSIX_REQUESTED))
364 return;
365
366 if (bnxt_ulp_registered(bp->edev, BNXT_ROCE_ULP)) {
367 struct bnxt_ulp *ulp = &edev->ulp_tbl[BNXT_ROCE_ULP];
368 struct bnxt_msix_entry *ent = NULL;
369
370 if (!ulp->msix_requested)
371 return;
372
373 ops = rtnl_dereference(ulp->ulp_ops);
374 if (!ops || !ops->ulp_irq_restart)
375 return;
376
377 if (!err) {
378 ent = kcalloc(ulp->msix_requested, sizeof(*ent),
379 GFP_KERNEL);
380 if (!ent)
381 return;
382 bnxt_fill_msix_vecs(bp, ent);
383 }
384 ops->ulp_irq_restart(ulp->handle, ent);
385 kfree(ent);
386 }
387}
388
389void bnxt_ulp_async_events(struct bnxt *bp, struct hwrm_async_event_cmpl *cmpl)
390{
391 u16 event_id = le16_to_cpu(cmpl->event_id);
392 struct bnxt_en_dev *edev = bp->edev;
393 struct bnxt_ulp_ops *ops;
394 int i;
395
396 if (!edev)
397 return;
398
399 rcu_read_lock();
400 for (i = 0; i < BNXT_MAX_ULP; i++) {
401 struct bnxt_ulp *ulp = &edev->ulp_tbl[i];
402
403 ops = rcu_dereference(ulp->ulp_ops);
404 if (!ops || !ops->ulp_async_notifier)
405 continue;
406 if (!ulp->async_events_bmap ||
407 event_id > ulp->max_async_event_id)
408 continue;
409
410
411 smp_rmb();
412 if (test_bit(event_id, ulp->async_events_bmap))
413 ops->ulp_async_notifier(ulp->handle, cmpl);
414 }
415 rcu_read_unlock();
416}
417
418static int bnxt_register_async_events(struct bnxt_en_dev *edev, int ulp_id,
419 unsigned long *events_bmap, u16 max_id)
420{
421 struct net_device *dev = edev->net;
422 struct bnxt *bp = netdev_priv(dev);
423 struct bnxt_ulp *ulp;
424
425 if (ulp_id >= BNXT_MAX_ULP)
426 return -EINVAL;
427
428 ulp = &edev->ulp_tbl[ulp_id];
429 ulp->async_events_bmap = events_bmap;
430
431 smp_wmb();
432 ulp->max_async_event_id = max_id;
433 bnxt_hwrm_func_rgtr_async_events(bp, events_bmap, max_id + 1);
434 return 0;
435}
436
437static const struct bnxt_en_ops bnxt_en_ops_tbl = {
438 .bnxt_register_device = bnxt_register_dev,
439 .bnxt_unregister_device = bnxt_unregister_dev,
440 .bnxt_request_msix = bnxt_req_msix_vecs,
441 .bnxt_free_msix = bnxt_free_msix_vecs,
442 .bnxt_send_fw_msg = bnxt_send_msg,
443 .bnxt_register_fw_async_events = bnxt_register_async_events,
444};
445
446struct bnxt_en_dev *bnxt_ulp_probe(struct net_device *dev)
447{
448 struct bnxt *bp = netdev_priv(dev);
449 struct bnxt_en_dev *edev;
450
451 edev = bp->edev;
452 if (!edev) {
453 edev = kzalloc(sizeof(*edev), GFP_KERNEL);
454 if (!edev)
455 return ERR_PTR(-ENOMEM);
456 edev->en_ops = &bnxt_en_ops_tbl;
457 if (bp->flags & BNXT_FLAG_ROCEV1_CAP)
458 edev->flags |= BNXT_EN_FLAG_ROCEV1_CAP;
459 if (bp->flags & BNXT_FLAG_ROCEV2_CAP)
460 edev->flags |= BNXT_EN_FLAG_ROCEV2_CAP;
461 edev->net = dev;
462 edev->pdev = bp->pdev;
463 bp->edev = edev;
464 }
465 return bp->edev;
466}
467