xref: /linux/drivers/net/ethernet/broadcom/bnxt/bnxt_ulp.c (revision fb7399cf2d0b33825b8039f95c45395c7deba25c)
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 #include <linux/auxiliary_bus.h>
23 #include <net/netdev_lock.h>
24 
25 #include "bnxt_hsi.h"
26 #include "bnxt.h"
27 #include "bnxt_hwrm.h"
28 #include "bnxt_ulp.h"
29 
30 static DEFINE_IDA(bnxt_aux_dev_ids);
31 
32 static void bnxt_fill_msix_vecs(struct bnxt *bp, struct bnxt_msix_entry *ent)
33 {
34 	struct bnxt_en_dev *edev = bp->edev;
35 	int num_msix, i;
36 
37 	if (!edev->ulp_tbl->msix_requested) {
38 		netdev_warn(bp->dev, "Requested MSI-X vectors insufficient\n");
39 		return;
40 	}
41 	num_msix = edev->ulp_tbl->msix_requested;
42 	for (i = 0; i < num_msix; i++) {
43 		ent[i].vector = bp->irq_tbl[i].vector;
44 		ent[i].ring_idx = i;
45 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
46 			ent[i].db_offset = bp->db_offset;
47 		else
48 			ent[i].db_offset = i * 0x80;
49 	}
50 }
51 
52 int bnxt_get_ulp_msix_num(struct bnxt *bp)
53 {
54 	if (bp->edev)
55 		return bp->edev->ulp_num_msix_vec;
56 	return 0;
57 }
58 
59 void bnxt_set_ulp_msix_num(struct bnxt *bp, int num)
60 {
61 	if (bp->edev)
62 		bp->edev->ulp_num_msix_vec = num;
63 }
64 
65 int bnxt_get_ulp_msix_num_in_use(struct bnxt *bp)
66 {
67 	if (bnxt_ulp_registered(bp->edev))
68 		return bp->edev->ulp_num_msix_vec;
69 	return 0;
70 }
71 
72 int bnxt_get_ulp_stat_ctxs(struct bnxt *bp)
73 {
74 	if (bp->edev)
75 		return bp->edev->ulp_num_ctxs;
76 	return 0;
77 }
78 
79 void bnxt_set_ulp_stat_ctxs(struct bnxt *bp, int num_ulp_ctx)
80 {
81 	if (bp->edev)
82 		bp->edev->ulp_num_ctxs = num_ulp_ctx;
83 }
84 
85 int bnxt_get_ulp_stat_ctxs_in_use(struct bnxt *bp)
86 {
87 	if (bnxt_ulp_registered(bp->edev))
88 		return bp->edev->ulp_num_ctxs;
89 	return 0;
90 }
91 
92 void bnxt_set_dflt_ulp_stat_ctxs(struct bnxt *bp)
93 {
94 	if (bp->edev) {
95 		bp->edev->ulp_num_ctxs = BNXT_MIN_ROCE_STAT_CTXS;
96 		/* Reserve one additional stat_ctx for PF0 (except
97 		 * on 1-port NICs) as it also creates one stat_ctx
98 		 * for PF1 in case of RoCE bonding.
99 		 */
100 		if (BNXT_PF(bp) && !bp->pf.port_id &&
101 		    bp->port_count > 1)
102 			bp->edev->ulp_num_ctxs++;
103 	}
104 }
105 
106 int bnxt_register_dev(struct bnxt_en_dev *edev,
107 		      struct bnxt_ulp_ops *ulp_ops,
108 		      void *handle)
109 {
110 	struct net_device *dev = edev->net;
111 	struct bnxt *bp = netdev_priv(dev);
112 	unsigned int max_stat_ctxs;
113 	struct bnxt_ulp *ulp;
114 	int rc = 0;
115 
116 	netdev_lock(dev);
117 	mutex_lock(&edev->en_dev_lock);
118 	if (!bp->irq_tbl) {
119 		rc = -ENODEV;
120 		goto exit;
121 	}
122 	max_stat_ctxs = bnxt_get_max_func_stat_ctxs(bp);
123 	if (max_stat_ctxs <= BNXT_MIN_ROCE_STAT_CTXS ||
124 	    bp->cp_nr_rings == max_stat_ctxs) {
125 		rc = -ENOMEM;
126 		goto exit;
127 	}
128 
129 	ulp = edev->ulp_tbl;
130 	ulp->handle = handle;
131 	rcu_assign_pointer(ulp->ulp_ops, ulp_ops);
132 
133 	if (test_bit(BNXT_STATE_OPEN, &bp->state))
134 		bnxt_hwrm_vnic_cfg(bp, &bp->vnic_info[BNXT_VNIC_DEFAULT]);
135 
136 	edev->ulp_tbl->msix_requested = bnxt_get_ulp_msix_num(bp);
137 
138 	bnxt_fill_msix_vecs(bp, bp->edev->msix_entries);
139 	edev->flags |= BNXT_EN_FLAG_MSIX_REQUESTED;
140 exit:
141 	mutex_unlock(&edev->en_dev_lock);
142 	netdev_unlock(dev);
143 	return rc;
144 }
145 EXPORT_SYMBOL(bnxt_register_dev);
146 
147 void bnxt_unregister_dev(struct bnxt_en_dev *edev)
148 {
149 	struct net_device *dev = edev->net;
150 	struct bnxt *bp = netdev_priv(dev);
151 	struct bnxt_ulp *ulp;
152 
153 	ulp = edev->ulp_tbl;
154 	netdev_lock(dev);
155 	mutex_lock(&edev->en_dev_lock);
156 	if (ulp->msix_requested)
157 		edev->flags &= ~BNXT_EN_FLAG_MSIX_REQUESTED;
158 	edev->ulp_tbl->msix_requested = 0;
159 
160 	if (ulp->max_async_event_id)
161 		bnxt_hwrm_func_drv_rgtr(bp, NULL, 0, true);
162 
163 	RCU_INIT_POINTER(ulp->ulp_ops, NULL);
164 	synchronize_rcu();
165 	ulp->max_async_event_id = 0;
166 	ulp->async_events_bmap = NULL;
167 	mutex_unlock(&edev->en_dev_lock);
168 	netdev_unlock(dev);
169 	return;
170 }
171 EXPORT_SYMBOL(bnxt_unregister_dev);
172 
173 static int bnxt_set_dflt_ulp_msix(struct bnxt *bp)
174 {
175 	int roce_msix = BNXT_MAX_ROCE_MSIX;
176 
177 	if (BNXT_VF(bp))
178 		roce_msix = BNXT_MAX_ROCE_MSIX_VF;
179 	else if (bp->port_partition_type)
180 		roce_msix = BNXT_MAX_ROCE_MSIX_NPAR_PF;
181 
182 	/* NQ MSIX vectors should match the number of CPUs plus 1 more for
183 	 * the CREQ MSIX, up to the default.
184 	 */
185 	return min_t(int, roce_msix, num_online_cpus() + 1);
186 }
187 
188 int bnxt_send_msg(struct bnxt_en_dev *edev,
189 			 struct bnxt_fw_msg *fw_msg)
190 {
191 	struct net_device *dev = edev->net;
192 	struct bnxt *bp = netdev_priv(dev);
193 	struct output *resp;
194 	struct input *req;
195 	u32 resp_len;
196 	int rc;
197 
198 	if (bp->fw_reset_state)
199 		return -EBUSY;
200 
201 	rc = hwrm_req_init(bp, req, 0 /* don't care */);
202 	if (rc)
203 		return rc;
204 
205 	rc = hwrm_req_replace(bp, req, fw_msg->msg, fw_msg->msg_len);
206 	if (rc)
207 		goto drop_req;
208 
209 	hwrm_req_timeout(bp, req, fw_msg->timeout);
210 	resp = hwrm_req_hold(bp, req);
211 	rc = hwrm_req_send(bp, req);
212 	resp_len = le16_to_cpu(resp->resp_len);
213 	if (resp_len) {
214 		if (fw_msg->resp_max_len < resp_len)
215 			resp_len = fw_msg->resp_max_len;
216 
217 		memcpy(fw_msg->resp, resp, resp_len);
218 	}
219 drop_req:
220 	hwrm_req_drop(bp, req);
221 	return rc;
222 }
223 EXPORT_SYMBOL(bnxt_send_msg);
224 
225 void bnxt_ulp_stop(struct bnxt *bp)
226 {
227 	struct bnxt_aux_priv *aux_priv = bp->aux_priv;
228 	struct bnxt_en_dev *edev = bp->edev;
229 
230 	if (!edev)
231 		return;
232 
233 	mutex_lock(&edev->en_dev_lock);
234 	if (!bnxt_ulp_registered(edev)) {
235 		mutex_unlock(&edev->en_dev_lock);
236 		return;
237 	}
238 
239 	edev->flags |= BNXT_EN_FLAG_ULP_STOPPED;
240 	if (aux_priv) {
241 		struct auxiliary_device *adev;
242 
243 		adev = &aux_priv->aux_dev;
244 		if (adev->dev.driver) {
245 			const struct auxiliary_driver *adrv;
246 			pm_message_t pm = {};
247 
248 			adrv = to_auxiliary_drv(adev->dev.driver);
249 			edev->en_state = bp->state;
250 			adrv->suspend(adev, pm);
251 		}
252 	}
253 	mutex_unlock(&edev->en_dev_lock);
254 }
255 
256 void bnxt_ulp_start(struct bnxt *bp, int err)
257 {
258 	struct bnxt_aux_priv *aux_priv = bp->aux_priv;
259 	struct bnxt_en_dev *edev = bp->edev;
260 
261 	if (!edev)
262 		return;
263 
264 	edev->flags &= ~BNXT_EN_FLAG_ULP_STOPPED;
265 
266 	if (err)
267 		return;
268 
269 	mutex_lock(&edev->en_dev_lock);
270 	if (!bnxt_ulp_registered(edev)) {
271 		mutex_unlock(&edev->en_dev_lock);
272 		return;
273 	}
274 
275 	if (edev->ulp_tbl->msix_requested)
276 		bnxt_fill_msix_vecs(bp, edev->msix_entries);
277 
278 	if (aux_priv) {
279 		struct auxiliary_device *adev;
280 
281 		adev = &aux_priv->aux_dev;
282 		if (adev->dev.driver) {
283 			const struct auxiliary_driver *adrv;
284 
285 			adrv = to_auxiliary_drv(adev->dev.driver);
286 			edev->en_state = bp->state;
287 			adrv->resume(adev);
288 		}
289 	}
290 	mutex_unlock(&edev->en_dev_lock);
291 }
292 
293 void bnxt_ulp_irq_stop(struct bnxt *bp)
294 {
295 	struct bnxt_en_dev *edev = bp->edev;
296 	struct bnxt_ulp_ops *ops;
297 	bool reset = false;
298 
299 	if (!edev || !(edev->flags & BNXT_EN_FLAG_MSIX_REQUESTED))
300 		return;
301 
302 	if (bnxt_ulp_registered(bp->edev)) {
303 		struct bnxt_ulp *ulp = edev->ulp_tbl;
304 
305 		if (!ulp->msix_requested)
306 			return;
307 
308 		ops = netdev_lock_dereference(ulp->ulp_ops, bp->dev);
309 		if (!ops || !ops->ulp_irq_stop)
310 			return;
311 		if (test_bit(BNXT_STATE_FW_RESET_DET, &bp->state))
312 			reset = true;
313 		ops->ulp_irq_stop(ulp->handle, reset);
314 	}
315 }
316 
317 void bnxt_ulp_irq_restart(struct bnxt *bp, int err)
318 {
319 	struct bnxt_en_dev *edev = bp->edev;
320 	struct bnxt_ulp_ops *ops;
321 
322 	if (!edev || !(edev->flags & BNXT_EN_FLAG_MSIX_REQUESTED))
323 		return;
324 
325 	if (bnxt_ulp_registered(bp->edev)) {
326 		struct bnxt_ulp *ulp = edev->ulp_tbl;
327 		struct bnxt_msix_entry *ent = NULL;
328 
329 		if (!ulp->msix_requested)
330 			return;
331 
332 		ops = netdev_lock_dereference(ulp->ulp_ops, bp->dev);
333 		if (!ops || !ops->ulp_irq_restart)
334 			return;
335 
336 		if (!err) {
337 			ent = kcalloc(ulp->msix_requested, sizeof(*ent),
338 				      GFP_KERNEL);
339 			if (!ent)
340 				return;
341 			bnxt_fill_msix_vecs(bp, ent);
342 		}
343 		ops->ulp_irq_restart(ulp->handle, ent);
344 		kfree(ent);
345 	}
346 }
347 
348 void bnxt_ulp_async_events(struct bnxt *bp, struct hwrm_async_event_cmpl *cmpl)
349 {
350 	u16 event_id = le16_to_cpu(cmpl->event_id);
351 	struct bnxt_en_dev *edev = bp->edev;
352 	struct bnxt_ulp_ops *ops;
353 	struct bnxt_ulp *ulp;
354 
355 	if (!bnxt_ulp_registered(edev))
356 		return;
357 	ulp = edev->ulp_tbl;
358 
359 	rcu_read_lock();
360 
361 	ops = rcu_dereference(ulp->ulp_ops);
362 	if (!ops || !ops->ulp_async_notifier)
363 		goto exit_unlock_rcu;
364 	if (!ulp->async_events_bmap || event_id > ulp->max_async_event_id)
365 		goto exit_unlock_rcu;
366 
367 	/* Read max_async_event_id first before testing the bitmap. */
368 	smp_rmb();
369 
370 	if (test_bit(event_id, ulp->async_events_bmap))
371 		ops->ulp_async_notifier(ulp->handle, cmpl);
372 exit_unlock_rcu:
373 	rcu_read_unlock();
374 }
375 
376 void bnxt_register_async_events(struct bnxt_en_dev *edev,
377 				unsigned long *events_bmap, u16 max_id)
378 {
379 	struct net_device *dev = edev->net;
380 	struct bnxt *bp = netdev_priv(dev);
381 	struct bnxt_ulp *ulp;
382 
383 	ulp = edev->ulp_tbl;
384 	ulp->async_events_bmap = events_bmap;
385 	/* Make sure bnxt_ulp_async_events() sees this order */
386 	smp_wmb();
387 	ulp->max_async_event_id = max_id;
388 	bnxt_hwrm_func_drv_rgtr(bp, events_bmap, max_id + 1, true);
389 }
390 EXPORT_SYMBOL(bnxt_register_async_events);
391 
392 void bnxt_rdma_aux_device_uninit(struct bnxt *bp)
393 {
394 	struct bnxt_aux_priv *aux_priv;
395 	struct auxiliary_device *adev;
396 
397 	/* Skip if no auxiliary device init was done. */
398 	if (!bp->aux_priv)
399 		return;
400 
401 	aux_priv = bp->aux_priv;
402 	adev = &aux_priv->aux_dev;
403 	auxiliary_device_uninit(adev);
404 }
405 
406 static void bnxt_aux_dev_release(struct device *dev)
407 {
408 	struct bnxt_aux_priv *aux_priv =
409 		container_of(dev, struct bnxt_aux_priv, aux_dev.dev);
410 	struct bnxt *bp = netdev_priv(aux_priv->edev->net);
411 
412 	ida_free(&bnxt_aux_dev_ids, aux_priv->id);
413 	kfree(aux_priv->edev->ulp_tbl);
414 	bp->edev = NULL;
415 	kfree(aux_priv->edev);
416 	kfree(aux_priv);
417 	bp->aux_priv = NULL;
418 }
419 
420 void bnxt_rdma_aux_device_del(struct bnxt *bp)
421 {
422 	if (!bp->edev)
423 		return;
424 
425 	auxiliary_device_delete(&bp->aux_priv->aux_dev);
426 }
427 
428 static void bnxt_set_edev_info(struct bnxt_en_dev *edev, struct bnxt *bp)
429 {
430 	edev->net = bp->dev;
431 	edev->pdev = bp->pdev;
432 	edev->l2_db_size = bp->db_size;
433 	edev->l2_db_size_nc = bp->db_size;
434 	edev->l2_db_offset = bp->db_offset;
435 	mutex_init(&edev->en_dev_lock);
436 
437 	if (bp->flags & BNXT_FLAG_ROCEV1_CAP)
438 		edev->flags |= BNXT_EN_FLAG_ROCEV1_CAP;
439 	if (bp->flags & BNXT_FLAG_ROCEV2_CAP)
440 		edev->flags |= BNXT_EN_FLAG_ROCEV2_CAP;
441 	if (bp->flags & BNXT_FLAG_VF)
442 		edev->flags |= BNXT_EN_FLAG_VF;
443 	if (BNXT_ROCE_VF_RESC_CAP(bp))
444 		edev->flags |= BNXT_EN_FLAG_ROCE_VF_RES_MGMT;
445 	if (BNXT_SW_RES_LMT(bp))
446 		edev->flags |= BNXT_EN_FLAG_SW_RES_LMT;
447 
448 	edev->chip_num = bp->chip_num;
449 	edev->hw_ring_stats_size = bp->hw_ring_stats_size;
450 	edev->pf_port_id = bp->pf.port_id;
451 	edev->en_state = bp->state;
452 	edev->bar0 = bp->bar0;
453 }
454 
455 void bnxt_rdma_aux_device_add(struct bnxt *bp)
456 {
457 	struct auxiliary_device *aux_dev;
458 	int rc;
459 
460 	if (!bp->edev)
461 		return;
462 
463 	aux_dev = &bp->aux_priv->aux_dev;
464 	rc = auxiliary_device_add(aux_dev);
465 	if (rc) {
466 		netdev_warn(bp->dev, "Failed to add auxiliary device for ROCE\n");
467 		auxiliary_device_uninit(aux_dev);
468 		bp->flags &= ~BNXT_FLAG_ROCE_CAP;
469 	}
470 }
471 
472 void bnxt_rdma_aux_device_init(struct bnxt *bp)
473 {
474 	struct auxiliary_device *aux_dev;
475 	struct bnxt_aux_priv *aux_priv;
476 	struct bnxt_en_dev *edev;
477 	struct bnxt_ulp *ulp;
478 	int rc;
479 
480 	if (!(bp->flags & BNXT_FLAG_ROCE_CAP))
481 		return;
482 
483 	aux_priv = kzalloc(sizeof(*bp->aux_priv), GFP_KERNEL);
484 	if (!aux_priv)
485 		goto exit;
486 
487 	aux_priv->id = ida_alloc(&bnxt_aux_dev_ids, GFP_KERNEL);
488 	if (aux_priv->id < 0) {
489 		netdev_warn(bp->dev,
490 			    "ida alloc failed for ROCE auxiliary device\n");
491 		kfree(aux_priv);
492 		goto exit;
493 	}
494 
495 	aux_dev = &aux_priv->aux_dev;
496 	aux_dev->id = aux_priv->id;
497 	aux_dev->name = "rdma";
498 	aux_dev->dev.parent = &bp->pdev->dev;
499 	aux_dev->dev.release = bnxt_aux_dev_release;
500 
501 	rc = auxiliary_device_init(aux_dev);
502 	if (rc) {
503 		ida_free(&bnxt_aux_dev_ids, aux_priv->id);
504 		kfree(aux_priv);
505 		goto exit;
506 	}
507 	bp->aux_priv = aux_priv;
508 
509 	/* From this point, all cleanup will happen via the .release callback &
510 	 * any error unwinding will need to include a call to
511 	 * auxiliary_device_uninit.
512 	 */
513 	edev = kzalloc(sizeof(*edev), GFP_KERNEL);
514 	if (!edev)
515 		goto aux_dev_uninit;
516 
517 	aux_priv->edev = edev;
518 
519 	ulp = kzalloc(sizeof(*ulp), GFP_KERNEL);
520 	if (!ulp)
521 		goto aux_dev_uninit;
522 
523 	edev->ulp_tbl = ulp;
524 	bp->edev = edev;
525 	bnxt_set_edev_info(edev, bp);
526 	bp->ulp_num_msix_want = bnxt_set_dflt_ulp_msix(bp);
527 
528 	return;
529 
530 aux_dev_uninit:
531 	auxiliary_device_uninit(aux_dev);
532 exit:
533 	bp->flags &= ~BNXT_FLAG_ROCE_CAP;
534 }
535