xref: /linux/drivers/net/ethernet/broadcom/bnxt/bnxt_ulp.c (revision 1a9239bb4253f9076b5b4b2a1a4e8d7defd77a95)
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 
bnxt_fill_msix_vecs(struct bnxt * bp,struct bnxt_msix_entry * ent)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 
bnxt_get_ulp_msix_num(struct bnxt * bp)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 
bnxt_set_ulp_msix_num(struct bnxt * bp,int num)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 
bnxt_get_ulp_msix_num_in_use(struct bnxt * bp)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 
bnxt_get_ulp_stat_ctxs(struct bnxt * bp)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 
bnxt_set_ulp_stat_ctxs(struct bnxt * bp,int num_ulp_ctx)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 
bnxt_get_ulp_stat_ctxs_in_use(struct bnxt * bp)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 
bnxt_set_dflt_ulp_stat_ctxs(struct bnxt * bp)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 
bnxt_register_dev(struct bnxt_en_dev * edev,struct bnxt_ulp_ops * ulp_ops,void * handle)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 
bnxt_unregister_dev(struct bnxt_en_dev * edev)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 	int i = 0;
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 	while (atomic_read(&ulp->ref_count) != 0 && i < 10) {
168 		msleep(100);
169 		i++;
170 	}
171 	mutex_unlock(&edev->en_dev_lock);
172 	netdev_unlock(dev);
173 	return;
174 }
175 EXPORT_SYMBOL(bnxt_unregister_dev);
176 
bnxt_set_dflt_ulp_msix(struct bnxt * bp)177 static int bnxt_set_dflt_ulp_msix(struct bnxt *bp)
178 {
179 	int roce_msix = BNXT_MAX_ROCE_MSIX;
180 
181 	if (BNXT_VF(bp))
182 		roce_msix = BNXT_MAX_ROCE_MSIX_VF;
183 	else if (bp->port_partition_type)
184 		roce_msix = BNXT_MAX_ROCE_MSIX_NPAR_PF;
185 
186 	/* NQ MSIX vectors should match the number of CPUs plus 1 more for
187 	 * the CREQ MSIX, up to the default.
188 	 */
189 	return min_t(int, roce_msix, num_online_cpus() + 1);
190 }
191 
bnxt_send_msg(struct bnxt_en_dev * edev,struct bnxt_fw_msg * fw_msg)192 int bnxt_send_msg(struct bnxt_en_dev *edev,
193 			 struct bnxt_fw_msg *fw_msg)
194 {
195 	struct net_device *dev = edev->net;
196 	struct bnxt *bp = netdev_priv(dev);
197 	struct output *resp;
198 	struct input *req;
199 	u32 resp_len;
200 	int rc;
201 
202 	if (bp->fw_reset_state)
203 		return -EBUSY;
204 
205 	rc = hwrm_req_init(bp, req, 0 /* don't care */);
206 	if (rc)
207 		return rc;
208 
209 	rc = hwrm_req_replace(bp, req, fw_msg->msg, fw_msg->msg_len);
210 	if (rc)
211 		goto drop_req;
212 
213 	hwrm_req_timeout(bp, req, fw_msg->timeout);
214 	resp = hwrm_req_hold(bp, req);
215 	rc = hwrm_req_send(bp, req);
216 	resp_len = le16_to_cpu(resp->resp_len);
217 	if (resp_len) {
218 		if (fw_msg->resp_max_len < resp_len)
219 			resp_len = fw_msg->resp_max_len;
220 
221 		memcpy(fw_msg->resp, resp, resp_len);
222 	}
223 drop_req:
224 	hwrm_req_drop(bp, req);
225 	return rc;
226 }
227 EXPORT_SYMBOL(bnxt_send_msg);
228 
bnxt_ulp_stop(struct bnxt * bp)229 void bnxt_ulp_stop(struct bnxt *bp)
230 {
231 	struct bnxt_aux_priv *aux_priv = bp->aux_priv;
232 	struct bnxt_en_dev *edev = bp->edev;
233 
234 	if (!edev)
235 		return;
236 
237 	mutex_lock(&edev->en_dev_lock);
238 	if (!bnxt_ulp_registered(edev)) {
239 		mutex_unlock(&edev->en_dev_lock);
240 		return;
241 	}
242 
243 	edev->flags |= BNXT_EN_FLAG_ULP_STOPPED;
244 	if (aux_priv) {
245 		struct auxiliary_device *adev;
246 
247 		adev = &aux_priv->aux_dev;
248 		if (adev->dev.driver) {
249 			const struct auxiliary_driver *adrv;
250 			pm_message_t pm = {};
251 
252 			adrv = to_auxiliary_drv(adev->dev.driver);
253 			edev->en_state = bp->state;
254 			adrv->suspend(adev, pm);
255 		}
256 	}
257 	mutex_unlock(&edev->en_dev_lock);
258 }
259 
bnxt_ulp_start(struct bnxt * bp,int err)260 void bnxt_ulp_start(struct bnxt *bp, int err)
261 {
262 	struct bnxt_aux_priv *aux_priv = bp->aux_priv;
263 	struct bnxt_en_dev *edev = bp->edev;
264 
265 	if (!edev)
266 		return;
267 
268 	edev->flags &= ~BNXT_EN_FLAG_ULP_STOPPED;
269 
270 	if (err)
271 		return;
272 
273 	mutex_lock(&edev->en_dev_lock);
274 	if (!bnxt_ulp_registered(edev)) {
275 		mutex_unlock(&edev->en_dev_lock);
276 		return;
277 	}
278 
279 	if (edev->ulp_tbl->msix_requested)
280 		bnxt_fill_msix_vecs(bp, edev->msix_entries);
281 
282 	if (aux_priv) {
283 		struct auxiliary_device *adev;
284 
285 		adev = &aux_priv->aux_dev;
286 		if (adev->dev.driver) {
287 			const struct auxiliary_driver *adrv;
288 
289 			adrv = to_auxiliary_drv(adev->dev.driver);
290 			edev->en_state = bp->state;
291 			adrv->resume(adev);
292 		}
293 	}
294 	mutex_unlock(&edev->en_dev_lock);
295 }
296 
bnxt_ulp_irq_stop(struct bnxt * bp)297 void bnxt_ulp_irq_stop(struct bnxt *bp)
298 {
299 	struct bnxt_en_dev *edev = bp->edev;
300 	struct bnxt_ulp_ops *ops;
301 	bool reset = false;
302 
303 	if (!edev || !(edev->flags & BNXT_EN_FLAG_MSIX_REQUESTED))
304 		return;
305 
306 	if (bnxt_ulp_registered(bp->edev)) {
307 		struct bnxt_ulp *ulp = edev->ulp_tbl;
308 
309 		if (!ulp->msix_requested)
310 			return;
311 
312 		netdev_lock(bp->dev);
313 		ops = rcu_dereference(ulp->ulp_ops);
314 		if (!ops || !ops->ulp_irq_stop)
315 			return;
316 		if (test_bit(BNXT_STATE_FW_RESET_DET, &bp->state))
317 			reset = true;
318 		ops->ulp_irq_stop(ulp->handle, reset);
319 		netdev_unlock(bp->dev);
320 	}
321 }
322 
bnxt_ulp_irq_restart(struct bnxt * bp,int err)323 void bnxt_ulp_irq_restart(struct bnxt *bp, int err)
324 {
325 	struct bnxt_en_dev *edev = bp->edev;
326 	struct bnxt_ulp_ops *ops;
327 
328 	if (!edev || !(edev->flags & BNXT_EN_FLAG_MSIX_REQUESTED))
329 		return;
330 
331 	if (bnxt_ulp_registered(bp->edev)) {
332 		struct bnxt_ulp *ulp = edev->ulp_tbl;
333 		struct bnxt_msix_entry *ent = NULL;
334 
335 		if (!ulp->msix_requested)
336 			return;
337 
338 		netdev_lock(bp->dev);
339 		ops = rcu_dereference(ulp->ulp_ops);
340 		if (!ops || !ops->ulp_irq_restart)
341 			return;
342 
343 		if (!err) {
344 			ent = kcalloc(ulp->msix_requested, sizeof(*ent),
345 				      GFP_KERNEL);
346 			if (!ent)
347 				return;
348 			bnxt_fill_msix_vecs(bp, ent);
349 		}
350 		ops->ulp_irq_restart(ulp->handle, ent);
351 		netdev_unlock(bp->dev);
352 		kfree(ent);
353 	}
354 }
355 
bnxt_ulp_async_events(struct bnxt * bp,struct hwrm_async_event_cmpl * cmpl)356 void bnxt_ulp_async_events(struct bnxt *bp, struct hwrm_async_event_cmpl *cmpl)
357 {
358 	u16 event_id = le16_to_cpu(cmpl->event_id);
359 	struct bnxt_en_dev *edev = bp->edev;
360 	struct bnxt_ulp_ops *ops;
361 	struct bnxt_ulp *ulp;
362 
363 	if (!bnxt_ulp_registered(edev))
364 		return;
365 	ulp = edev->ulp_tbl;
366 
367 	rcu_read_lock();
368 
369 	ops = rcu_dereference(ulp->ulp_ops);
370 	if (!ops || !ops->ulp_async_notifier)
371 		goto exit_unlock_rcu;
372 	if (!ulp->async_events_bmap || event_id > ulp->max_async_event_id)
373 		goto exit_unlock_rcu;
374 
375 	/* Read max_async_event_id first before testing the bitmap. */
376 	smp_rmb();
377 
378 	if (test_bit(event_id, ulp->async_events_bmap))
379 		ops->ulp_async_notifier(ulp->handle, cmpl);
380 exit_unlock_rcu:
381 	rcu_read_unlock();
382 }
383 
bnxt_register_async_events(struct bnxt_en_dev * edev,unsigned long * events_bmap,u16 max_id)384 void bnxt_register_async_events(struct bnxt_en_dev *edev,
385 				unsigned long *events_bmap, u16 max_id)
386 {
387 	struct net_device *dev = edev->net;
388 	struct bnxt *bp = netdev_priv(dev);
389 	struct bnxt_ulp *ulp;
390 
391 	ulp = edev->ulp_tbl;
392 	ulp->async_events_bmap = events_bmap;
393 	/* Make sure bnxt_ulp_async_events() sees this order */
394 	smp_wmb();
395 	ulp->max_async_event_id = max_id;
396 	bnxt_hwrm_func_drv_rgtr(bp, events_bmap, max_id + 1, true);
397 }
398 EXPORT_SYMBOL(bnxt_register_async_events);
399 
bnxt_rdma_aux_device_uninit(struct bnxt * bp)400 void bnxt_rdma_aux_device_uninit(struct bnxt *bp)
401 {
402 	struct bnxt_aux_priv *aux_priv;
403 	struct auxiliary_device *adev;
404 
405 	/* Skip if no auxiliary device init was done. */
406 	if (!bp->aux_priv)
407 		return;
408 
409 	aux_priv = bp->aux_priv;
410 	adev = &aux_priv->aux_dev;
411 	auxiliary_device_uninit(adev);
412 }
413 
bnxt_aux_dev_release(struct device * dev)414 static void bnxt_aux_dev_release(struct device *dev)
415 {
416 	struct bnxt_aux_priv *aux_priv =
417 		container_of(dev, struct bnxt_aux_priv, aux_dev.dev);
418 	struct bnxt *bp = netdev_priv(aux_priv->edev->net);
419 
420 	ida_free(&bnxt_aux_dev_ids, aux_priv->id);
421 	kfree(aux_priv->edev->ulp_tbl);
422 	bp->edev = NULL;
423 	kfree(aux_priv->edev);
424 	kfree(aux_priv);
425 	bp->aux_priv = NULL;
426 }
427 
bnxt_rdma_aux_device_del(struct bnxt * bp)428 void bnxt_rdma_aux_device_del(struct bnxt *bp)
429 {
430 	if (!bp->edev)
431 		return;
432 
433 	auxiliary_device_delete(&bp->aux_priv->aux_dev);
434 }
435 
bnxt_set_edev_info(struct bnxt_en_dev * edev,struct bnxt * bp)436 static void bnxt_set_edev_info(struct bnxt_en_dev *edev, struct bnxt *bp)
437 {
438 	edev->net = bp->dev;
439 	edev->pdev = bp->pdev;
440 	edev->l2_db_size = bp->db_size;
441 	edev->l2_db_size_nc = bp->db_size;
442 	edev->l2_db_offset = bp->db_offset;
443 	mutex_init(&edev->en_dev_lock);
444 
445 	if (bp->flags & BNXT_FLAG_ROCEV1_CAP)
446 		edev->flags |= BNXT_EN_FLAG_ROCEV1_CAP;
447 	if (bp->flags & BNXT_FLAG_ROCEV2_CAP)
448 		edev->flags |= BNXT_EN_FLAG_ROCEV2_CAP;
449 	if (bp->flags & BNXT_FLAG_VF)
450 		edev->flags |= BNXT_EN_FLAG_VF;
451 	if (BNXT_ROCE_VF_RESC_CAP(bp))
452 		edev->flags |= BNXT_EN_FLAG_ROCE_VF_RES_MGMT;
453 	if (BNXT_SW_RES_LMT(bp))
454 		edev->flags |= BNXT_EN_FLAG_SW_RES_LMT;
455 
456 	edev->chip_num = bp->chip_num;
457 	edev->hw_ring_stats_size = bp->hw_ring_stats_size;
458 	edev->pf_port_id = bp->pf.port_id;
459 	edev->en_state = bp->state;
460 	edev->bar0 = bp->bar0;
461 }
462 
bnxt_rdma_aux_device_add(struct bnxt * bp)463 void bnxt_rdma_aux_device_add(struct bnxt *bp)
464 {
465 	struct auxiliary_device *aux_dev;
466 	int rc;
467 
468 	if (!bp->edev)
469 		return;
470 
471 	aux_dev = &bp->aux_priv->aux_dev;
472 	rc = auxiliary_device_add(aux_dev);
473 	if (rc) {
474 		netdev_warn(bp->dev, "Failed to add auxiliary device for ROCE\n");
475 		auxiliary_device_uninit(aux_dev);
476 		bp->flags &= ~BNXT_FLAG_ROCE_CAP;
477 	}
478 }
479 
bnxt_rdma_aux_device_init(struct bnxt * bp)480 void bnxt_rdma_aux_device_init(struct bnxt *bp)
481 {
482 	struct auxiliary_device *aux_dev;
483 	struct bnxt_aux_priv *aux_priv;
484 	struct bnxt_en_dev *edev;
485 	struct bnxt_ulp *ulp;
486 	int rc;
487 
488 	if (!(bp->flags & BNXT_FLAG_ROCE_CAP))
489 		return;
490 
491 	aux_priv = kzalloc(sizeof(*bp->aux_priv), GFP_KERNEL);
492 	if (!aux_priv)
493 		goto exit;
494 
495 	aux_priv->id = ida_alloc(&bnxt_aux_dev_ids, GFP_KERNEL);
496 	if (aux_priv->id < 0) {
497 		netdev_warn(bp->dev,
498 			    "ida alloc failed for ROCE auxiliary device\n");
499 		kfree(aux_priv);
500 		goto exit;
501 	}
502 
503 	aux_dev = &aux_priv->aux_dev;
504 	aux_dev->id = aux_priv->id;
505 	aux_dev->name = "rdma";
506 	aux_dev->dev.parent = &bp->pdev->dev;
507 	aux_dev->dev.release = bnxt_aux_dev_release;
508 
509 	rc = auxiliary_device_init(aux_dev);
510 	if (rc) {
511 		ida_free(&bnxt_aux_dev_ids, aux_priv->id);
512 		kfree(aux_priv);
513 		goto exit;
514 	}
515 	bp->aux_priv = aux_priv;
516 
517 	/* From this point, all cleanup will happen via the .release callback &
518 	 * any error unwinding will need to include a call to
519 	 * auxiliary_device_uninit.
520 	 */
521 	edev = kzalloc(sizeof(*edev), GFP_KERNEL);
522 	if (!edev)
523 		goto aux_dev_uninit;
524 
525 	aux_priv->edev = edev;
526 
527 	ulp = kzalloc(sizeof(*ulp), GFP_KERNEL);
528 	if (!ulp)
529 		goto aux_dev_uninit;
530 
531 	edev->ulp_tbl = ulp;
532 	bp->edev = edev;
533 	bnxt_set_edev_info(edev, bp);
534 	bp->ulp_num_msix_want = bnxt_set_dflt_ulp_msix(bp);
535 
536 	return;
537 
538 aux_dev_uninit:
539 	auxiliary_device_uninit(aux_dev);
540 exit:
541 	bp->flags &= ~BNXT_FLAG_ROCE_CAP;
542 }
543