OSDN Git Service

2583923df5a17272b4b0e03fbca34d9a3f5b4bed
[android-x86/kernel.git] / drivers / net / ethernet / broadcom / bnxt / bnxt_ulp.c
1 /* 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
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 int bnxt_req_msix_vecs(struct bnxt_en_dev *edev, int ulp_id,
105                               struct bnxt_msix_entry *ent, int num_msix)
106 {
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;
111         int rc = 0;
112
113         ASSERT_RTNL();
114         if (ulp_id != BNXT_ROCE_ULP)
115                 return -EINVAL;
116
117         if (!(bp->flags & BNXT_FLAG_USING_MSIX))
118                 return -ENODEV;
119
120         if (edev->ulp_tbl[ulp_id].msix_requested)
121                 return -EAGAIN;
122
123         max_cp_rings = bnxt_get_max_func_cp_rings(bp);
124         avail_msix = bnxt_get_avail_msix(bp, num_msix);
125         if (!avail_msix)
126                 return -ENOMEM;
127         if (avail_msix > num_msix)
128                 avail_msix = num_msix;
129
130         if (bp->flags & BNXT_FLAG_NEW_RM) {
131                 idx = bp->cp_nr_rings;
132         } else {
133                 max_idx = min_t(int, bp->total_irqs, max_cp_rings);
134                 idx = max_idx - avail_msix;
135         }
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);
142                 } else {
143                         rc = bnxt_reserve_rings(bp);
144                 }
145         }
146         if (rc) {
147                 edev->ulp_tbl[ulp_id].msix_requested = 0;
148                 return -EAGAIN;
149         }
150
151         if (bp->flags & BNXT_FLAG_NEW_RM) {
152                 struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
153
154                 avail_msix = hw_resc->resv_cp_rings - bp->cp_nr_rings;
155                 edev->ulp_tbl[ulp_id].msix_requested = avail_msix;
156         }
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;
161         }
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);
164         return avail_msix;
165 }
166
167 static int bnxt_free_msix_vecs(struct bnxt_en_dev *edev, int ulp_id)
168 {
169         struct net_device *dev = edev->net;
170         struct bnxt *bp = netdev_priv(dev);
171         int max_cp_rings, msix_requested;
172
173         ASSERT_RTNL();
174         if (ulp_id != BNXT_ROCE_ULP)
175                 return -EINVAL;
176
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);
185         }
186         return 0;
187 }
188
189 int bnxt_get_ulp_msix_num(struct bnxt *bp)
190 {
191         if (bnxt_ulp_registered(bp->edev, BNXT_ROCE_ULP)) {
192                 struct bnxt_en_dev *edev = bp->edev;
193
194                 return edev->ulp_tbl[BNXT_ROCE_ULP].msix_requested;
195         }
196         return 0;
197 }
198
199 int bnxt_get_ulp_msix_base(struct bnxt *bp)
200 {
201         if (bnxt_ulp_registered(bp->edev, BNXT_ROCE_ULP)) {
202                 struct bnxt_en_dev *edev = bp->edev;
203
204                 if (edev->ulp_tbl[BNXT_ROCE_ULP].msix_requested)
205                         return edev->ulp_tbl[BNXT_ROCE_ULP].msix_base;
206         }
207         return 0;
208 }
209
210 void bnxt_subtract_ulp_resources(struct bnxt *bp, int ulp_id)
211 {
212         ASSERT_RTNL();
213         if (bnxt_ulp_registered(bp->edev, ulp_id)) {
214                 struct bnxt_en_dev *edev = bp->edev;
215                 unsigned int msix_req, max;
216
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);
222         }
223 }
224
225 static int bnxt_send_msg(struct bnxt_en_dev *edev, int ulp_id,
226                          struct bnxt_fw_msg *fw_msg)
227 {
228         struct net_device *dev = edev->net;
229         struct bnxt *bp = netdev_priv(dev);
230         struct input *req;
231         int rc;
232
233         mutex_lock(&bp->hwrm_cmd_lock);
234         req = fw_msg->msg;
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,
237                                 fw_msg->timeout);
238         if (!rc) {
239                 struct output *resp = bp->hwrm_cmd_resp_addr;
240                 u32 len = le16_to_cpu(resp->resp_len);
241
242                 if (fw_msg->resp_max_len < len)
243                         len = fw_msg->resp_max_len;
244
245                 memcpy(fw_msg->resp, resp, len);
246         }
247         mutex_unlock(&bp->hwrm_cmd_lock);
248         return rc;
249 }
250
251 static void bnxt_ulp_get(struct bnxt_ulp *ulp)
252 {
253         atomic_inc(&ulp->ref_count);
254 }
255
256 static void bnxt_ulp_put(struct bnxt_ulp *ulp)
257 {
258         atomic_dec(&ulp->ref_count);
259 }
260
261 void bnxt_ulp_stop(struct bnxt *bp)
262 {
263         struct bnxt_en_dev *edev = bp->edev;
264         struct bnxt_ulp_ops *ops;
265         int i;
266
267         if (!edev)
268                 return;
269
270         for (i = 0; i < BNXT_MAX_ULP; i++) {
271                 struct bnxt_ulp *ulp = &edev->ulp_tbl[i];
272
273                 ops = rtnl_dereference(ulp->ulp_ops);
274                 if (!ops || !ops->ulp_stop)
275                         continue;
276                 ops->ulp_stop(ulp->handle);
277         }
278 }
279
280 void bnxt_ulp_start(struct bnxt *bp)
281 {
282         struct bnxt_en_dev *edev = bp->edev;
283         struct bnxt_ulp_ops *ops;
284         int i;
285
286         if (!edev)
287                 return;
288
289         for (i = 0; i < BNXT_MAX_ULP; i++) {
290                 struct bnxt_ulp *ulp = &edev->ulp_tbl[i];
291
292                 ops = rtnl_dereference(ulp->ulp_ops);
293                 if (!ops || !ops->ulp_start)
294                         continue;
295                 ops->ulp_start(ulp->handle);
296         }
297 }
298
299 void bnxt_ulp_sriov_cfg(struct bnxt *bp, int num_vfs)
300 {
301         struct bnxt_en_dev *edev = bp->edev;
302         struct bnxt_ulp_ops *ops;
303         int i;
304
305         if (!edev)
306                 return;
307
308         for (i = 0; i < BNXT_MAX_ULP; i++) {
309                 struct bnxt_ulp *ulp = &edev->ulp_tbl[i];
310
311                 rcu_read_lock();
312                 ops = rcu_dereference(ulp->ulp_ops);
313                 if (!ops || !ops->ulp_sriov_config) {
314                         rcu_read_unlock();
315                         continue;
316                 }
317                 bnxt_ulp_get(ulp);
318                 rcu_read_unlock();
319                 ops->ulp_sriov_config(ulp->handle, num_vfs);
320                 bnxt_ulp_put(ulp);
321         }
322 }
323
324 void bnxt_ulp_shutdown(struct bnxt *bp)
325 {
326         struct bnxt_en_dev *edev = bp->edev;
327         struct bnxt_ulp_ops *ops;
328         int i;
329
330         if (!edev)
331                 return;
332
333         for (i = 0; i < BNXT_MAX_ULP; i++) {
334                 struct bnxt_ulp *ulp = &edev->ulp_tbl[i];
335
336                 ops = rtnl_dereference(ulp->ulp_ops);
337                 if (!ops || !ops->ulp_shutdown)
338                         continue;
339                 ops->ulp_shutdown(ulp->handle);
340         }
341 }
342
343 void bnxt_ulp_async_events(struct bnxt *bp, struct hwrm_async_event_cmpl *cmpl)
344 {
345         u16 event_id = le16_to_cpu(cmpl->event_id);
346         struct bnxt_en_dev *edev = bp->edev;
347         struct bnxt_ulp_ops *ops;
348         int i;
349
350         if (!edev)
351                 return;
352
353         rcu_read_lock();
354         for (i = 0; i < BNXT_MAX_ULP; i++) {
355                 struct bnxt_ulp *ulp = &edev->ulp_tbl[i];
356
357                 ops = rcu_dereference(ulp->ulp_ops);
358                 if (!ops || !ops->ulp_async_notifier)
359                         continue;
360                 if (!ulp->async_events_bmap ||
361                     event_id > ulp->max_async_event_id)
362                         continue;
363
364                 /* Read max_async_event_id first before testing the bitmap. */
365                 smp_rmb();
366                 if (test_bit(event_id, ulp->async_events_bmap))
367                         ops->ulp_async_notifier(ulp->handle, cmpl);
368         }
369         rcu_read_unlock();
370 }
371
372 static int bnxt_register_async_events(struct bnxt_en_dev *edev, int ulp_id,
373                                       unsigned long *events_bmap, u16 max_id)
374 {
375         struct net_device *dev = edev->net;
376         struct bnxt *bp = netdev_priv(dev);
377         struct bnxt_ulp *ulp;
378
379         if (ulp_id >= BNXT_MAX_ULP)
380                 return -EINVAL;
381
382         ulp = &edev->ulp_tbl[ulp_id];
383         ulp->async_events_bmap = events_bmap;
384         /* Make sure bnxt_ulp_async_events() sees this order */
385         smp_wmb();
386         ulp->max_async_event_id = max_id;
387         bnxt_hwrm_func_rgtr_async_events(bp, events_bmap, max_id + 1);
388         return 0;
389 }
390
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,
398 };
399
400 struct bnxt_en_dev *bnxt_ulp_probe(struct net_device *dev)
401 {
402         struct bnxt *bp = netdev_priv(dev);
403         struct bnxt_en_dev *edev;
404
405         edev = bp->edev;
406         if (!edev) {
407                 edev = kzalloc(sizeof(*edev), GFP_KERNEL);
408                 if (!edev)
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;
415                 edev->net = dev;
416                 edev->pdev = bp->pdev;
417                 bp->edev = edev;
418         }
419         return bp->edev;
420 }