xref: /openbmc/linux/drivers/net/ethernet/broadcom/bnxt/bnxt_ulp.c (revision d32fd6bb9f2bc8178cdd65ebec1ad670a8bfa241)
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, idx, 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 	idx = edev->ulp_tbl->msix_base;
42 	for (i = 0; i < num_msix; i++) {
43 		ent[i].vector = bp->irq_tbl[idx + i].vector;
44 		ent[i].ring_idx = idx + i;
45 		if (bp->flags & BNXT_FLAG_CHIP_P5) {
46 			ent[i].db_offset = DB_PF_OFFSET_P5;
47 			if (BNXT_VF(bp))
48 				ent[i].db_offset = DB_VF_OFFSET_P5;
49 		} else {
50 			ent[i].db_offset = (idx + i) * 0x80;
51 		}
52 	}
53 }
54 
bnxt_register_dev(struct bnxt_en_dev * edev,struct bnxt_ulp_ops * ulp_ops,void * handle)55 int bnxt_register_dev(struct bnxt_en_dev *edev,
56 		      struct bnxt_ulp_ops *ulp_ops,
57 		      void *handle)
58 {
59 	struct net_device *dev = edev->net;
60 	struct bnxt *bp = netdev_priv(dev);
61 	unsigned int max_stat_ctxs;
62 	struct bnxt_ulp *ulp;
63 
64 	max_stat_ctxs = bnxt_get_max_func_stat_ctxs(bp);
65 	if (max_stat_ctxs <= BNXT_MIN_ROCE_STAT_CTXS ||
66 	    bp->cp_nr_rings == max_stat_ctxs)
67 		return -ENOMEM;
68 
69 	ulp = edev->ulp_tbl;
70 	if (!ulp)
71 		return -ENOMEM;
72 
73 	ulp->handle = handle;
74 	rcu_assign_pointer(ulp->ulp_ops, ulp_ops);
75 
76 	if (test_bit(BNXT_STATE_OPEN, &bp->state))
77 		bnxt_hwrm_vnic_cfg(bp, 0);
78 
79 	bnxt_fill_msix_vecs(bp, bp->edev->msix_entries);
80 	edev->flags |= BNXT_EN_FLAG_MSIX_REQUESTED;
81 	return 0;
82 }
83 EXPORT_SYMBOL(bnxt_register_dev);
84 
bnxt_unregister_dev(struct bnxt_en_dev * edev)85 void bnxt_unregister_dev(struct bnxt_en_dev *edev)
86 {
87 	struct net_device *dev = edev->net;
88 	struct bnxt *bp = netdev_priv(dev);
89 	struct bnxt_ulp *ulp;
90 	int i = 0;
91 
92 	ulp = edev->ulp_tbl;
93 	if (ulp->msix_requested)
94 		edev->flags &= ~BNXT_EN_FLAG_MSIX_REQUESTED;
95 
96 	if (ulp->max_async_event_id)
97 		bnxt_hwrm_func_drv_rgtr(bp, NULL, 0, true);
98 
99 	RCU_INIT_POINTER(ulp->ulp_ops, NULL);
100 	synchronize_rcu();
101 	ulp->max_async_event_id = 0;
102 	ulp->async_events_bmap = NULL;
103 	while (atomic_read(&ulp->ref_count) != 0 && i < 10) {
104 		msleep(100);
105 		i++;
106 	}
107 	return;
108 }
109 EXPORT_SYMBOL(bnxt_unregister_dev);
110 
bnxt_get_ulp_msix_num(struct bnxt * bp)111 int bnxt_get_ulp_msix_num(struct bnxt *bp)
112 {
113 	u32 roce_msix = BNXT_VF(bp) ?
114 			BNXT_MAX_VF_ROCE_MSIX : BNXT_MAX_ROCE_MSIX;
115 
116 	return ((bp->flags & BNXT_FLAG_ROCE_CAP) ?
117 		min_t(u32, roce_msix, num_online_cpus()) : 0);
118 }
119 
bnxt_get_ulp_msix_base(struct bnxt * bp)120 int bnxt_get_ulp_msix_base(struct bnxt *bp)
121 {
122 	if (bnxt_ulp_registered(bp->edev)) {
123 		struct bnxt_en_dev *edev = bp->edev;
124 
125 		if (edev->ulp_tbl->msix_requested)
126 			return edev->ulp_tbl->msix_base;
127 	}
128 	return 0;
129 }
130 
bnxt_get_ulp_stat_ctxs(struct bnxt * bp)131 int bnxt_get_ulp_stat_ctxs(struct bnxt *bp)
132 {
133 	if (bnxt_ulp_registered(bp->edev)) {
134 		struct bnxt_en_dev *edev = bp->edev;
135 
136 		if (edev->ulp_tbl->msix_requested)
137 			return BNXT_MIN_ROCE_STAT_CTXS;
138 	}
139 
140 	return 0;
141 }
142 
bnxt_send_msg(struct bnxt_en_dev * edev,struct bnxt_fw_msg * fw_msg)143 int bnxt_send_msg(struct bnxt_en_dev *edev,
144 			 struct bnxt_fw_msg *fw_msg)
145 {
146 	struct net_device *dev = edev->net;
147 	struct bnxt *bp = netdev_priv(dev);
148 	struct output *resp;
149 	struct input *req;
150 	u32 resp_len;
151 	int rc;
152 
153 	if (bp->fw_reset_state)
154 		return -EBUSY;
155 
156 	rc = hwrm_req_init(bp, req, 0 /* don't care */);
157 	if (rc)
158 		return rc;
159 
160 	rc = hwrm_req_replace(bp, req, fw_msg->msg, fw_msg->msg_len);
161 	if (rc)
162 		goto drop_req;
163 
164 	hwrm_req_timeout(bp, req, fw_msg->timeout);
165 	resp = hwrm_req_hold(bp, req);
166 	rc = hwrm_req_send(bp, req);
167 	resp_len = le16_to_cpu(resp->resp_len);
168 	if (resp_len) {
169 		if (fw_msg->resp_max_len < resp_len)
170 			resp_len = fw_msg->resp_max_len;
171 
172 		memcpy(fw_msg->resp, resp, resp_len);
173 	}
174 drop_req:
175 	hwrm_req_drop(bp, req);
176 	return rc;
177 }
178 EXPORT_SYMBOL(bnxt_send_msg);
179 
bnxt_ulp_stop(struct bnxt * bp)180 void bnxt_ulp_stop(struct bnxt *bp)
181 {
182 	struct bnxt_aux_priv *aux_priv = bp->aux_priv;
183 	struct bnxt_en_dev *edev = bp->edev;
184 
185 	if (!edev)
186 		return;
187 
188 	edev->flags |= BNXT_EN_FLAG_ULP_STOPPED;
189 	if (aux_priv) {
190 		struct auxiliary_device *adev;
191 
192 		adev = &aux_priv->aux_dev;
193 		if (adev->dev.driver) {
194 			struct auxiliary_driver *adrv;
195 			pm_message_t pm = {};
196 
197 			adrv = to_auxiliary_drv(adev->dev.driver);
198 			edev->en_state = bp->state;
199 			adrv->suspend(adev, pm);
200 		}
201 	}
202 }
203 
bnxt_ulp_start(struct bnxt * bp,int err)204 void bnxt_ulp_start(struct bnxt *bp, int err)
205 {
206 	struct bnxt_aux_priv *aux_priv = bp->aux_priv;
207 	struct bnxt_en_dev *edev = bp->edev;
208 
209 	if (!edev)
210 		return;
211 
212 	edev->flags &= ~BNXT_EN_FLAG_ULP_STOPPED;
213 
214 	if (err)
215 		return;
216 
217 	if (edev->ulp_tbl->msix_requested)
218 		bnxt_fill_msix_vecs(bp, edev->msix_entries);
219 
220 	if (aux_priv) {
221 		struct auxiliary_device *adev;
222 
223 		adev = &aux_priv->aux_dev;
224 		if (adev->dev.driver) {
225 			struct auxiliary_driver *adrv;
226 
227 			adrv = to_auxiliary_drv(adev->dev.driver);
228 			edev->en_state = bp->state;
229 			adrv->resume(adev);
230 		}
231 	}
232 
233 }
234 
bnxt_ulp_irq_stop(struct bnxt * bp)235 void bnxt_ulp_irq_stop(struct bnxt *bp)
236 {
237 	struct bnxt_en_dev *edev = bp->edev;
238 	struct bnxt_ulp_ops *ops;
239 
240 	if (!edev || !(edev->flags & BNXT_EN_FLAG_MSIX_REQUESTED))
241 		return;
242 
243 	if (bnxt_ulp_registered(bp->edev)) {
244 		struct bnxt_ulp *ulp = edev->ulp_tbl;
245 
246 		if (!ulp->msix_requested)
247 			return;
248 
249 		ops = rtnl_dereference(ulp->ulp_ops);
250 		if (!ops || !ops->ulp_irq_stop)
251 			return;
252 		ops->ulp_irq_stop(ulp->handle);
253 	}
254 }
255 
bnxt_ulp_irq_restart(struct bnxt * bp,int err)256 void bnxt_ulp_irq_restart(struct bnxt *bp, int err)
257 {
258 	struct bnxt_en_dev *edev = bp->edev;
259 	struct bnxt_ulp_ops *ops;
260 
261 	if (!edev || !(edev->flags & BNXT_EN_FLAG_MSIX_REQUESTED))
262 		return;
263 
264 	if (bnxt_ulp_registered(bp->edev)) {
265 		struct bnxt_ulp *ulp = edev->ulp_tbl;
266 		struct bnxt_msix_entry *ent = NULL;
267 
268 		if (!ulp->msix_requested)
269 			return;
270 
271 		ops = rtnl_dereference(ulp->ulp_ops);
272 		if (!ops || !ops->ulp_irq_restart)
273 			return;
274 
275 		if (!err) {
276 			ent = kcalloc(ulp->msix_requested, sizeof(*ent),
277 				      GFP_KERNEL);
278 			if (!ent)
279 				return;
280 			bnxt_fill_msix_vecs(bp, ent);
281 		}
282 		ops->ulp_irq_restart(ulp->handle, ent);
283 		kfree(ent);
284 	}
285 }
286 
bnxt_register_async_events(struct bnxt_en_dev * edev,unsigned long * events_bmap,u16 max_id)287 int bnxt_register_async_events(struct bnxt_en_dev *edev,
288 			       unsigned long *events_bmap,
289 			       u16 max_id)
290 {
291 	struct net_device *dev = edev->net;
292 	struct bnxt *bp = netdev_priv(dev);
293 	struct bnxt_ulp *ulp;
294 
295 	ulp = edev->ulp_tbl;
296 	ulp->async_events_bmap = events_bmap;
297 	/* Make sure bnxt_ulp_async_events() sees this order */
298 	smp_wmb();
299 	ulp->max_async_event_id = max_id;
300 	bnxt_hwrm_func_drv_rgtr(bp, events_bmap, max_id + 1, true);
301 	return 0;
302 }
303 EXPORT_SYMBOL(bnxt_register_async_events);
304 
bnxt_rdma_aux_device_uninit(struct bnxt * bp)305 void bnxt_rdma_aux_device_uninit(struct bnxt *bp)
306 {
307 	struct bnxt_aux_priv *aux_priv;
308 	struct auxiliary_device *adev;
309 
310 	/* Skip if no auxiliary device init was done. */
311 	if (!bp->aux_priv)
312 		return;
313 
314 	aux_priv = bp->aux_priv;
315 	adev = &aux_priv->aux_dev;
316 	auxiliary_device_delete(adev);
317 	auxiliary_device_uninit(adev);
318 }
319 
bnxt_aux_dev_release(struct device * dev)320 static void bnxt_aux_dev_release(struct device *dev)
321 {
322 	struct bnxt_aux_priv *aux_priv =
323 		container_of(dev, struct bnxt_aux_priv, aux_dev.dev);
324 	struct bnxt *bp = netdev_priv(aux_priv->edev->net);
325 
326 	ida_free(&bnxt_aux_dev_ids, aux_priv->id);
327 	kfree(aux_priv->edev->ulp_tbl);
328 	bp->edev = NULL;
329 	kfree(aux_priv->edev);
330 	kfree(aux_priv);
331 	bp->aux_priv = NULL;
332 }
333 
bnxt_set_edev_info(struct bnxt_en_dev * edev,struct bnxt * bp)334 static void bnxt_set_edev_info(struct bnxt_en_dev *edev, struct bnxt *bp)
335 {
336 	edev->net = bp->dev;
337 	edev->pdev = bp->pdev;
338 	edev->l2_db_size = bp->db_size;
339 	edev->l2_db_size_nc = bp->db_size;
340 
341 	if (bp->flags & BNXT_FLAG_ROCEV1_CAP)
342 		edev->flags |= BNXT_EN_FLAG_ROCEV1_CAP;
343 	if (bp->flags & BNXT_FLAG_ROCEV2_CAP)
344 		edev->flags |= BNXT_EN_FLAG_ROCEV2_CAP;
345 	if (bp->flags & BNXT_FLAG_VF)
346 		edev->flags |= BNXT_EN_FLAG_VF;
347 
348 	edev->chip_num = bp->chip_num;
349 	edev->hw_ring_stats_size = bp->hw_ring_stats_size;
350 	edev->pf_port_id = bp->pf.port_id;
351 	edev->en_state = bp->state;
352 	edev->bar0 = bp->bar0;
353 	edev->ulp_tbl->msix_requested = bnxt_get_ulp_msix_num(bp);
354 }
355 
bnxt_rdma_aux_device_init(struct bnxt * bp)356 void bnxt_rdma_aux_device_init(struct bnxt *bp)
357 {
358 	struct auxiliary_device *aux_dev;
359 	struct bnxt_aux_priv *aux_priv;
360 	struct bnxt_en_dev *edev;
361 	struct bnxt_ulp *ulp;
362 	int rc;
363 
364 	if (!(bp->flags & BNXT_FLAG_ROCE_CAP))
365 		return;
366 
367 	aux_priv = kzalloc(sizeof(*bp->aux_priv), GFP_KERNEL);
368 	if (!aux_priv)
369 		goto exit;
370 
371 	aux_priv->id = ida_alloc(&bnxt_aux_dev_ids, GFP_KERNEL);
372 	if (aux_priv->id < 0) {
373 		netdev_warn(bp->dev,
374 			    "ida alloc failed for ROCE auxiliary device\n");
375 		kfree(aux_priv);
376 		goto exit;
377 	}
378 
379 	aux_dev = &aux_priv->aux_dev;
380 	aux_dev->id = aux_priv->id;
381 	aux_dev->name = "rdma";
382 	aux_dev->dev.parent = &bp->pdev->dev;
383 	aux_dev->dev.release = bnxt_aux_dev_release;
384 
385 	rc = auxiliary_device_init(aux_dev);
386 	if (rc) {
387 		ida_free(&bnxt_aux_dev_ids, aux_priv->id);
388 		kfree(aux_priv);
389 		goto exit;
390 	}
391 	bp->aux_priv = aux_priv;
392 
393 	/* From this point, all cleanup will happen via the .release callback &
394 	 * any error unwinding will need to include a call to
395 	 * auxiliary_device_uninit.
396 	 */
397 	edev = kzalloc(sizeof(*edev), GFP_KERNEL);
398 	if (!edev)
399 		goto aux_dev_uninit;
400 
401 	aux_priv->edev = edev;
402 
403 	ulp = kzalloc(sizeof(*ulp), GFP_KERNEL);
404 	if (!ulp)
405 		goto aux_dev_uninit;
406 
407 	edev->ulp_tbl = ulp;
408 	bp->edev = edev;
409 	bnxt_set_edev_info(edev, bp);
410 
411 	rc = auxiliary_device_add(aux_dev);
412 	if (rc) {
413 		netdev_warn(bp->dev,
414 			    "Failed to add auxiliary device for ROCE\n");
415 		goto aux_dev_uninit;
416 	}
417 
418 	return;
419 
420 aux_dev_uninit:
421 	auxiliary_device_uninit(aux_dev);
422 exit:
423 	bp->flags &= ~BNXT_FLAG_ROCE_CAP;
424 }
425