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