bnxt_en: Add support for ethtool get dump.
[linux-block.git] / drivers / net / ethernet / broadcom / bnxt / bnxt_ulp.c
CommitLineData
a588e458
MC
1/* Broadcom NetXtreme-C/E network driver.
2 *
ec86f14e 3 * Copyright (c) 2016-2018 Broadcom Limited
a588e458
MC
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);
146ed3c5
MC
87 if (ulp->msix_requested)
88 edev->en_ops->bnxt_free_msix(edev, ulp_id);
a588e458
MC
89 }
90 if (ulp->max_async_event_id)
91 bnxt_hwrm_func_rgtr_async_events(bp, NULL, 0);
92
93 RCU_INIT_POINTER(ulp->ulp_ops, NULL);
94 synchronize_rcu();
95 ulp->max_async_event_id = 0;
96 ulp->async_events_bmap = NULL;
97 while (atomic_read(&ulp->ref_count) != 0 && i < 10) {
98 msleep(100);
99 i++;
100 }
101 return 0;
102}
103
ec86f14e
MC
104static void bnxt_fill_msix_vecs(struct bnxt *bp, struct bnxt_msix_entry *ent)
105{
106 struct bnxt_en_dev *edev = bp->edev;
107 int num_msix, idx, i;
108
109 num_msix = edev->ulp_tbl[BNXT_ROCE_ULP].msix_requested;
110 idx = edev->ulp_tbl[BNXT_ROCE_ULP].msix_base;
111 for (i = 0; i < num_msix; i++) {
112 ent[i].vector = bp->irq_tbl[idx + i].vector;
113 ent[i].ring_idx = idx + i;
114 ent[i].db_offset = (idx + i) * 0x80;
115 }
116}
117
a588e458
MC
118static int bnxt_req_msix_vecs(struct bnxt_en_dev *edev, int ulp_id,
119 struct bnxt_msix_entry *ent, int num_msix)
120{
121 struct net_device *dev = edev->net;
122 struct bnxt *bp = netdev_priv(dev);
123 int max_idx, max_cp_rings;
ec86f14e 124 int avail_msix, idx;
fbcfc8e4 125 int rc = 0;
a588e458
MC
126
127 ASSERT_RTNL();
128 if (ulp_id != BNXT_ROCE_ULP)
129 return -EINVAL;
130
131 if (!(bp->flags & BNXT_FLAG_USING_MSIX))
132 return -ENODEV;
133
08654eb2
MC
134 if (edev->ulp_tbl[ulp_id].msix_requested)
135 return -EAGAIN;
136
a588e458 137 max_cp_rings = bnxt_get_max_func_cp_rings(bp);
fbcfc8e4 138 avail_msix = bnxt_get_avail_msix(bp, num_msix);
a588e458
MC
139 if (!avail_msix)
140 return -ENOMEM;
141 if (avail_msix > num_msix)
142 avail_msix = num_msix;
143
fbcfc8e4 144 if (bp->flags & BNXT_FLAG_NEW_RM) {
08654eb2 145 idx = bp->cp_nr_rings;
fbcfc8e4
MC
146 } else {
147 max_idx = min_t(int, bp->total_irqs, max_cp_rings);
08654eb2 148 idx = max_idx - avail_msix;
fbcfc8e4 149 }
08654eb2 150 edev->ulp_tbl[ulp_id].msix_base = idx;
fbcfc8e4
MC
151 edev->ulp_tbl[ulp_id].msix_requested = avail_msix;
152 if (bp->total_irqs < (idx + avail_msix)) {
153 if (netif_running(dev)) {
154 bnxt_close_nic(bp, true, false);
155 rc = bnxt_open_nic(bp, true, false);
156 } else {
157 rc = bnxt_reserve_rings(bp);
158 }
159 }
160 if (rc) {
161 edev->ulp_tbl[ulp_id].msix_requested = 0;
162 return -EAGAIN;
163 }
164
165 if (bp->flags & BNXT_FLAG_NEW_RM) {
166 struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
167
168 avail_msix = hw_resc->resv_cp_rings - bp->cp_nr_rings;
169 edev->ulp_tbl[ulp_id].msix_requested = avail_msix;
170 }
ec86f14e 171 bnxt_fill_msix_vecs(bp, ent);
a588e458 172 bnxt_set_max_func_cp_rings(bp, max_cp_rings - avail_msix);
ec86f14e 173 edev->flags |= BNXT_EN_FLAG_MSIX_REQUESTED;
a588e458
MC
174 return avail_msix;
175}
176
177static int bnxt_free_msix_vecs(struct bnxt_en_dev *edev, int ulp_id)
178{
179 struct net_device *dev = edev->net;
180 struct bnxt *bp = netdev_priv(dev);
181 int max_cp_rings, msix_requested;
182
183 ASSERT_RTNL();
184 if (ulp_id != BNXT_ROCE_ULP)
185 return -EINVAL;
186
ec86f14e
MC
187 if (!(edev->flags & BNXT_EN_FLAG_MSIX_REQUESTED))
188 return 0;
189
a588e458
MC
190 max_cp_rings = bnxt_get_max_func_cp_rings(bp);
191 msix_requested = edev->ulp_tbl[ulp_id].msix_requested;
192 bnxt_set_max_func_cp_rings(bp, max_cp_rings + msix_requested);
193 edev->ulp_tbl[ulp_id].msix_requested = 0;
ec86f14e 194 edev->flags &= ~BNXT_EN_FLAG_MSIX_REQUESTED;
fbcfc8e4
MC
195 if (netif_running(dev)) {
196 bnxt_close_nic(bp, true, false);
197 bnxt_open_nic(bp, true, false);
198 }
a588e458
MC
199 return 0;
200}
201
08654eb2
MC
202int bnxt_get_ulp_msix_num(struct bnxt *bp)
203{
204 if (bnxt_ulp_registered(bp->edev, BNXT_ROCE_ULP)) {
205 struct bnxt_en_dev *edev = bp->edev;
206
207 return edev->ulp_tbl[BNXT_ROCE_ULP].msix_requested;
208 }
209 return 0;
210}
211
212int bnxt_get_ulp_msix_base(struct bnxt *bp)
213{
214 if (bnxt_ulp_registered(bp->edev, BNXT_ROCE_ULP)) {
215 struct bnxt_en_dev *edev = bp->edev;
216
217 if (edev->ulp_tbl[BNXT_ROCE_ULP].msix_requested)
218 return edev->ulp_tbl[BNXT_ROCE_ULP].msix_base;
219 }
220 return 0;
221}
222
a588e458
MC
223void bnxt_subtract_ulp_resources(struct bnxt *bp, int ulp_id)
224{
225 ASSERT_RTNL();
226 if (bnxt_ulp_registered(bp->edev, ulp_id)) {
227 struct bnxt_en_dev *edev = bp->edev;
228 unsigned int msix_req, max;
229
230 msix_req = edev->ulp_tbl[ulp_id].msix_requested;
231 max = bnxt_get_max_func_cp_rings(bp);
232 bnxt_set_max_func_cp_rings(bp, max - msix_req);
233 max = bnxt_get_max_func_stat_ctxs(bp);
234 bnxt_set_max_func_stat_ctxs(bp, max - 1);
235 }
236}
237
238static int bnxt_send_msg(struct bnxt_en_dev *edev, int ulp_id,
239 struct bnxt_fw_msg *fw_msg)
240{
241 struct net_device *dev = edev->net;
242 struct bnxt *bp = netdev_priv(dev);
243 struct input *req;
244 int rc;
245
246 mutex_lock(&bp->hwrm_cmd_lock);
247 req = fw_msg->msg;
248 req->resp_addr = cpu_to_le64(bp->hwrm_cmd_resp_dma_addr);
249 rc = _hwrm_send_message(bp, fw_msg->msg, fw_msg->msg_len,
250 fw_msg->timeout);
251 if (!rc) {
252 struct output *resp = bp->hwrm_cmd_resp_addr;
253 u32 len = le16_to_cpu(resp->resp_len);
254
255 if (fw_msg->resp_max_len < len)
256 len = fw_msg->resp_max_len;
257
258 memcpy(fw_msg->resp, resp, len);
259 }
260 mutex_unlock(&bp->hwrm_cmd_lock);
261 return rc;
262}
263
264static void bnxt_ulp_get(struct bnxt_ulp *ulp)
265{
266 atomic_inc(&ulp->ref_count);
267}
268
269static void bnxt_ulp_put(struct bnxt_ulp *ulp)
270{
271 atomic_dec(&ulp->ref_count);
272}
273
274void bnxt_ulp_stop(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_stop)
288 continue;
289 ops->ulp_stop(ulp->handle);
290 }
291}
292
293void bnxt_ulp_start(struct bnxt *bp)
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 ops = rtnl_dereference(ulp->ulp_ops);
306 if (!ops || !ops->ulp_start)
307 continue;
308 ops->ulp_start(ulp->handle);
309 }
310}
311
312void bnxt_ulp_sriov_cfg(struct bnxt *bp, int num_vfs)
313{
314 struct bnxt_en_dev *edev = bp->edev;
315 struct bnxt_ulp_ops *ops;
316 int i;
317
318 if (!edev)
319 return;
320
321 for (i = 0; i < BNXT_MAX_ULP; i++) {
322 struct bnxt_ulp *ulp = &edev->ulp_tbl[i];
323
324 rcu_read_lock();
325 ops = rcu_dereference(ulp->ulp_ops);
326 if (!ops || !ops->ulp_sriov_config) {
327 rcu_read_unlock();
328 continue;
329 }
330 bnxt_ulp_get(ulp);
331 rcu_read_unlock();
332 ops->ulp_sriov_config(ulp->handle, num_vfs);
333 bnxt_ulp_put(ulp);
334 }
335}
336
0efd2fc6
MC
337void bnxt_ulp_shutdown(struct bnxt *bp)
338{
339 struct bnxt_en_dev *edev = bp->edev;
340 struct bnxt_ulp_ops *ops;
341 int i;
342
343 if (!edev)
344 return;
345
346 for (i = 0; i < BNXT_MAX_ULP; i++) {
347 struct bnxt_ulp *ulp = &edev->ulp_tbl[i];
348
349 ops = rtnl_dereference(ulp->ulp_ops);
350 if (!ops || !ops->ulp_shutdown)
351 continue;
352 ops->ulp_shutdown(ulp->handle);
353 }
354}
355
ec86f14e
MC
356void bnxt_ulp_irq_stop(struct bnxt *bp)
357{
358 struct bnxt_en_dev *edev = bp->edev;
359 struct bnxt_ulp_ops *ops;
360
361 if (!edev || !(edev->flags & BNXT_EN_FLAG_MSIX_REQUESTED))
362 return;
363
364 if (bnxt_ulp_registered(bp->edev, BNXT_ROCE_ULP)) {
365 struct bnxt_ulp *ulp = &edev->ulp_tbl[BNXT_ROCE_ULP];
366
367 if (!ulp->msix_requested)
368 return;
369
370 ops = rtnl_dereference(ulp->ulp_ops);
371 if (!ops || !ops->ulp_irq_stop)
372 return;
373 ops->ulp_irq_stop(ulp->handle);
374 }
375}
376
377void bnxt_ulp_irq_restart(struct bnxt *bp, int err)
378{
379 struct bnxt_en_dev *edev = bp->edev;
380 struct bnxt_ulp_ops *ops;
381
382 if (!edev || !(edev->flags & BNXT_EN_FLAG_MSIX_REQUESTED))
383 return;
384
385 if (bnxt_ulp_registered(bp->edev, BNXT_ROCE_ULP)) {
386 struct bnxt_ulp *ulp = &edev->ulp_tbl[BNXT_ROCE_ULP];
387 struct bnxt_msix_entry *ent = NULL;
388
389 if (!ulp->msix_requested)
390 return;
391
392 ops = rtnl_dereference(ulp->ulp_ops);
393 if (!ops || !ops->ulp_irq_restart)
394 return;
395
396 if (!err) {
397 ent = kcalloc(ulp->msix_requested, sizeof(*ent),
398 GFP_KERNEL);
399 if (!ent)
400 return;
401 bnxt_fill_msix_vecs(bp, ent);
402 }
403 ops->ulp_irq_restart(ulp->handle, ent);
404 kfree(ent);
405 }
406}
407
a588e458
MC
408void bnxt_ulp_async_events(struct bnxt *bp, struct hwrm_async_event_cmpl *cmpl)
409{
410 u16 event_id = le16_to_cpu(cmpl->event_id);
411 struct bnxt_en_dev *edev = bp->edev;
412 struct bnxt_ulp_ops *ops;
413 int i;
414
415 if (!edev)
416 return;
417
418 rcu_read_lock();
419 for (i = 0; i < BNXT_MAX_ULP; i++) {
420 struct bnxt_ulp *ulp = &edev->ulp_tbl[i];
421
422 ops = rcu_dereference(ulp->ulp_ops);
423 if (!ops || !ops->ulp_async_notifier)
424 continue;
425 if (!ulp->async_events_bmap ||
426 event_id > ulp->max_async_event_id)
427 continue;
428
429 /* Read max_async_event_id first before testing the bitmap. */
430 smp_rmb();
431 if (test_bit(event_id, ulp->async_events_bmap))
432 ops->ulp_async_notifier(ulp->handle, cmpl);
433 }
434 rcu_read_unlock();
435}
436
437static int bnxt_register_async_events(struct bnxt_en_dev *edev, int ulp_id,
438 unsigned long *events_bmap, u16 max_id)
439{
440 struct net_device *dev = edev->net;
441 struct bnxt *bp = netdev_priv(dev);
442 struct bnxt_ulp *ulp;
443
444 if (ulp_id >= BNXT_MAX_ULP)
445 return -EINVAL;
446
447 ulp = &edev->ulp_tbl[ulp_id];
448 ulp->async_events_bmap = events_bmap;
449 /* Make sure bnxt_ulp_async_events() sees this order */
450 smp_wmb();
451 ulp->max_async_event_id = max_id;
452 bnxt_hwrm_func_rgtr_async_events(bp, events_bmap, max_id + 1);
453 return 0;
454}
455
456static const struct bnxt_en_ops bnxt_en_ops_tbl = {
457 .bnxt_register_device = bnxt_register_dev,
458 .bnxt_unregister_device = bnxt_unregister_dev,
459 .bnxt_request_msix = bnxt_req_msix_vecs,
460 .bnxt_free_msix = bnxt_free_msix_vecs,
461 .bnxt_send_fw_msg = bnxt_send_msg,
462 .bnxt_register_fw_async_events = bnxt_register_async_events,
463};
464
465struct bnxt_en_dev *bnxt_ulp_probe(struct net_device *dev)
466{
467 struct bnxt *bp = netdev_priv(dev);
468 struct bnxt_en_dev *edev;
469
470 edev = bp->edev;
471 if (!edev) {
472 edev = kzalloc(sizeof(*edev), GFP_KERNEL);
473 if (!edev)
474 return ERR_PTR(-ENOMEM);
475 edev->en_ops = &bnxt_en_ops_tbl;
476 if (bp->flags & BNXT_FLAG_ROCEV1_CAP)
477 edev->flags |= BNXT_EN_FLAG_ROCEV1_CAP;
478 if (bp->flags & BNXT_FLAG_ROCEV2_CAP)
479 edev->flags |= BNXT_EN_FLAG_ROCEV2_CAP;
480 edev->net = dev;
481 edev->pdev = bp->pdev;
482 bp->edev = edev;
483 }
484 return bp->edev;
485}