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