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