GNU Linux-libre 4.19.286-gnu1
[releases.git] / drivers / net / ethernet / broadcom / bnxt / bnxt_ulp.c
1 /* Broadcom NetXtreme-C/E network driver.
2  *
3  * Copyright (c) 2016-2018 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
27 static 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
66 static 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                 if (ulp->msix_requested)
88                         edev->en_ops->bnxt_free_msix(edev, ulp_id);
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
104 static 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
118 static 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;
124         int avail_msix, idx;
125         int rc = 0;
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
134         if (edev->ulp_tbl[ulp_id].msix_requested)
135                 return -EAGAIN;
136
137         max_cp_rings = bnxt_get_max_func_cp_rings(bp);
138         avail_msix = bnxt_get_avail_msix(bp, num_msix);
139         if (!avail_msix)
140                 return -ENOMEM;
141         if (avail_msix > num_msix)
142                 avail_msix = num_msix;
143
144         if (BNXT_NEW_RM(bp)) {
145                 idx = bp->cp_nr_rings;
146         } else {
147                 max_idx = min_t(int, bp->total_irqs, max_cp_rings);
148                 idx = max_idx - avail_msix;
149         }
150         edev->ulp_tbl[ulp_id].msix_base = idx;
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 (BNXT_NEW_RM(bp)) {
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         }
171         bnxt_fill_msix_vecs(bp, ent);
172         edev->flags |= BNXT_EN_FLAG_MSIX_REQUESTED;
173         return avail_msix;
174 }
175
176 static int bnxt_free_msix_vecs(struct bnxt_en_dev *edev, int ulp_id)
177 {
178         struct net_device *dev = edev->net;
179         struct bnxt *bp = netdev_priv(dev);
180
181         ASSERT_RTNL();
182         if (ulp_id != BNXT_ROCE_ULP)
183                 return -EINVAL;
184
185         if (!(edev->flags & BNXT_EN_FLAG_MSIX_REQUESTED))
186                 return 0;
187
188         edev->ulp_tbl[ulp_id].msix_requested = 0;
189         edev->flags &= ~BNXT_EN_FLAG_MSIX_REQUESTED;
190         if (netif_running(dev)) {
191                 bnxt_close_nic(bp, true, false);
192                 bnxt_open_nic(bp, true, false);
193         }
194         return 0;
195 }
196
197 int bnxt_get_ulp_msix_num(struct bnxt *bp)
198 {
199         if (bnxt_ulp_registered(bp->edev, BNXT_ROCE_ULP)) {
200                 struct bnxt_en_dev *edev = bp->edev;
201
202                 return edev->ulp_tbl[BNXT_ROCE_ULP].msix_requested;
203         }
204         return 0;
205 }
206
207 int bnxt_get_ulp_msix_base(struct bnxt *bp)
208 {
209         if (bnxt_ulp_registered(bp->edev, BNXT_ROCE_ULP)) {
210                 struct bnxt_en_dev *edev = bp->edev;
211
212                 if (edev->ulp_tbl[BNXT_ROCE_ULP].msix_requested)
213                         return edev->ulp_tbl[BNXT_ROCE_ULP].msix_base;
214         }
215         return 0;
216 }
217
218 static int bnxt_send_msg(struct bnxt_en_dev *edev, int ulp_id,
219                          struct bnxt_fw_msg *fw_msg)
220 {
221         struct net_device *dev = edev->net;
222         struct bnxt *bp = netdev_priv(dev);
223         struct input *req;
224         int rc;
225
226         mutex_lock(&bp->hwrm_cmd_lock);
227         req = fw_msg->msg;
228         req->resp_addr = cpu_to_le64(bp->hwrm_cmd_resp_dma_addr);
229         rc = _hwrm_send_message(bp, fw_msg->msg, fw_msg->msg_len,
230                                 fw_msg->timeout);
231         if (!rc) {
232                 struct output *resp = bp->hwrm_cmd_resp_addr;
233                 u32 len = le16_to_cpu(resp->resp_len);
234
235                 if (fw_msg->resp_max_len < len)
236                         len = fw_msg->resp_max_len;
237
238                 memcpy(fw_msg->resp, resp, len);
239         }
240         mutex_unlock(&bp->hwrm_cmd_lock);
241         return rc;
242 }
243
244 static void bnxt_ulp_get(struct bnxt_ulp *ulp)
245 {
246         atomic_inc(&ulp->ref_count);
247 }
248
249 static void bnxt_ulp_put(struct bnxt_ulp *ulp)
250 {
251         atomic_dec(&ulp->ref_count);
252 }
253
254 void bnxt_ulp_stop(struct bnxt *bp)
255 {
256         struct bnxt_en_dev *edev = bp->edev;
257         struct bnxt_ulp_ops *ops;
258         int i;
259
260         if (!edev)
261                 return;
262
263         for (i = 0; i < BNXT_MAX_ULP; i++) {
264                 struct bnxt_ulp *ulp = &edev->ulp_tbl[i];
265
266                 ops = rtnl_dereference(ulp->ulp_ops);
267                 if (!ops || !ops->ulp_stop)
268                         continue;
269                 ops->ulp_stop(ulp->handle);
270         }
271 }
272
273 void bnxt_ulp_start(struct bnxt *bp)
274 {
275         struct bnxt_en_dev *edev = bp->edev;
276         struct bnxt_ulp_ops *ops;
277         int i;
278
279         if (!edev)
280                 return;
281
282         for (i = 0; i < BNXT_MAX_ULP; i++) {
283                 struct bnxt_ulp *ulp = &edev->ulp_tbl[i];
284
285                 ops = rtnl_dereference(ulp->ulp_ops);
286                 if (!ops || !ops->ulp_start)
287                         continue;
288                 ops->ulp_start(ulp->handle);
289         }
290 }
291
292 void bnxt_ulp_sriov_cfg(struct bnxt *bp, int num_vfs)
293 {
294         struct bnxt_en_dev *edev = bp->edev;
295         struct bnxt_ulp_ops *ops;
296         int i;
297
298         if (!edev)
299                 return;
300
301         for (i = 0; i < BNXT_MAX_ULP; i++) {
302                 struct bnxt_ulp *ulp = &edev->ulp_tbl[i];
303
304                 rcu_read_lock();
305                 ops = rcu_dereference(ulp->ulp_ops);
306                 if (!ops || !ops->ulp_sriov_config) {
307                         rcu_read_unlock();
308                         continue;
309                 }
310                 bnxt_ulp_get(ulp);
311                 rcu_read_unlock();
312                 ops->ulp_sriov_config(ulp->handle, num_vfs);
313                 bnxt_ulp_put(ulp);
314         }
315 }
316
317 void bnxt_ulp_shutdown(struct bnxt *bp)
318 {
319         struct bnxt_en_dev *edev = bp->edev;
320         struct bnxt_ulp_ops *ops;
321         int i;
322
323         if (!edev)
324                 return;
325
326         for (i = 0; i < BNXT_MAX_ULP; i++) {
327                 struct bnxt_ulp *ulp = &edev->ulp_tbl[i];
328
329                 ops = rtnl_dereference(ulp->ulp_ops);
330                 if (!ops || !ops->ulp_shutdown)
331                         continue;
332                 ops->ulp_shutdown(ulp->handle);
333         }
334 }
335
336 void bnxt_ulp_irq_stop(struct bnxt *bp)
337 {
338         struct bnxt_en_dev *edev = bp->edev;
339         struct bnxt_ulp_ops *ops;
340
341         if (!edev || !(edev->flags & BNXT_EN_FLAG_MSIX_REQUESTED))
342                 return;
343
344         if (bnxt_ulp_registered(bp->edev, BNXT_ROCE_ULP)) {
345                 struct bnxt_ulp *ulp = &edev->ulp_tbl[BNXT_ROCE_ULP];
346
347                 if (!ulp->msix_requested)
348                         return;
349
350                 ops = rtnl_dereference(ulp->ulp_ops);
351                 if (!ops || !ops->ulp_irq_stop)
352                         return;
353                 ops->ulp_irq_stop(ulp->handle);
354         }
355 }
356
357 void bnxt_ulp_irq_restart(struct bnxt *bp, int err)
358 {
359         struct bnxt_en_dev *edev = bp->edev;
360         struct bnxt_ulp_ops *ops;
361
362         if (!edev || !(edev->flags & BNXT_EN_FLAG_MSIX_REQUESTED))
363                 return;
364
365         if (bnxt_ulp_registered(bp->edev, BNXT_ROCE_ULP)) {
366                 struct bnxt_ulp *ulp = &edev->ulp_tbl[BNXT_ROCE_ULP];
367                 struct bnxt_msix_entry *ent = NULL;
368
369                 if (!ulp->msix_requested)
370                         return;
371
372                 ops = rtnl_dereference(ulp->ulp_ops);
373                 if (!ops || !ops->ulp_irq_restart)
374                         return;
375
376                 if (!err) {
377                         ent = kcalloc(ulp->msix_requested, sizeof(*ent),
378                                       GFP_KERNEL);
379                         if (!ent)
380                                 return;
381                         bnxt_fill_msix_vecs(bp, ent);
382                 }
383                 ops->ulp_irq_restart(ulp->handle, ent);
384                 kfree(ent);
385         }
386 }
387
388 void bnxt_ulp_async_events(struct bnxt *bp, struct hwrm_async_event_cmpl *cmpl)
389 {
390         u16 event_id = le16_to_cpu(cmpl->event_id);
391         struct bnxt_en_dev *edev = bp->edev;
392         struct bnxt_ulp_ops *ops;
393         int i;
394
395         if (!edev)
396                 return;
397
398         rcu_read_lock();
399         for (i = 0; i < BNXT_MAX_ULP; i++) {
400                 struct bnxt_ulp *ulp = &edev->ulp_tbl[i];
401
402                 ops = rcu_dereference(ulp->ulp_ops);
403                 if (!ops || !ops->ulp_async_notifier)
404                         continue;
405                 if (!ulp->async_events_bmap ||
406                     event_id > ulp->max_async_event_id)
407                         continue;
408
409                 /* Read max_async_event_id first before testing the bitmap. */
410                 smp_rmb();
411                 if (test_bit(event_id, ulp->async_events_bmap))
412                         ops->ulp_async_notifier(ulp->handle, cmpl);
413         }
414         rcu_read_unlock();
415 }
416
417 static int bnxt_register_async_events(struct bnxt_en_dev *edev, int ulp_id,
418                                       unsigned long *events_bmap, u16 max_id)
419 {
420         struct net_device *dev = edev->net;
421         struct bnxt *bp = netdev_priv(dev);
422         struct bnxt_ulp *ulp;
423
424         if (ulp_id >= BNXT_MAX_ULP)
425                 return -EINVAL;
426
427         ulp = &edev->ulp_tbl[ulp_id];
428         ulp->async_events_bmap = events_bmap;
429         /* Make sure bnxt_ulp_async_events() sees this order */
430         smp_wmb();
431         ulp->max_async_event_id = max_id;
432         bnxt_hwrm_func_rgtr_async_events(bp, events_bmap, max_id + 1);
433         return 0;
434 }
435
436 static const struct bnxt_en_ops bnxt_en_ops_tbl = {
437         .bnxt_register_device   = bnxt_register_dev,
438         .bnxt_unregister_device = bnxt_unregister_dev,
439         .bnxt_request_msix      = bnxt_req_msix_vecs,
440         .bnxt_free_msix         = bnxt_free_msix_vecs,
441         .bnxt_send_fw_msg       = bnxt_send_msg,
442         .bnxt_register_fw_async_events  = bnxt_register_async_events,
443 };
444
445 struct bnxt_en_dev *bnxt_ulp_probe(struct net_device *dev)
446 {
447         struct bnxt *bp = netdev_priv(dev);
448         struct bnxt_en_dev *edev;
449
450         edev = bp->edev;
451         if (!edev) {
452                 edev = kzalloc(sizeof(*edev), GFP_KERNEL);
453                 if (!edev)
454                         return ERR_PTR(-ENOMEM);
455                 edev->en_ops = &bnxt_en_ops_tbl;
456                 if (bp->flags & BNXT_FLAG_ROCEV1_CAP)
457                         edev->flags |= BNXT_EN_FLAG_ROCEV1_CAP;
458                 if (bp->flags & BNXT_FLAG_ROCEV2_CAP)
459                         edev->flags |= BNXT_EN_FLAG_ROCEV2_CAP;
460                 edev->net = dev;
461                 edev->pdev = bp->pdev;
462                 bp->edev = edev;
463         }
464         return bp->edev;
465 }