Linux Audio

Check our new training course

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