mlx5_main.c revision 306233
1290650Shselasky/*-
2290650Shselasky * Copyright (c) 2013-2015, Mellanox Technologies, Ltd.  All rights reserved.
3290650Shselasky *
4290650Shselasky * Redistribution and use in source and binary forms, with or without
5290650Shselasky * modification, are permitted provided that the following conditions
6290650Shselasky * are met:
7290650Shselasky * 1. Redistributions of source code must retain the above copyright
8290650Shselasky *    notice, this list of conditions and the following disclaimer.
9290650Shselasky * 2. Redistributions in binary form must reproduce the above copyright
10290650Shselasky *    notice, this list of conditions and the following disclaimer in the
11290650Shselasky *    documentation and/or other materials provided with the distribution.
12290650Shselasky *
13290650Shselasky * THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS `AS IS' AND
14290650Shselasky * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15290650Shselasky * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16290650Shselasky * ARE DISCLAIMED.  IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
17290650Shselasky * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18290650Shselasky * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19290650Shselasky * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20290650Shselasky * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21290650Shselasky * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22290650Shselasky * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
23290650Shselasky * SUCH DAMAGE.
24290650Shselasky *
25290650Shselasky * $FreeBSD: stable/11/sys/dev/mlx5/mlx5_core/mlx5_main.c 306233 2016-09-23 08:17:51Z hselasky $
26290650Shselasky */
27290650Shselasky
28300676Shselasky#define	LINUXKPI_PARAM_PREFIX mlx5_
29300676Shselasky
30290650Shselasky#include <linux/kmod.h>
31290650Shselasky#include <linux/module.h>
32290650Shselasky#include <linux/errno.h>
33290650Shselasky#include <linux/pci.h>
34290650Shselasky#include <linux/dma-mapping.h>
35290650Shselasky#include <linux/slab.h>
36290650Shselasky#include <linux/io-mapping.h>
37290650Shselasky#include <linux/interrupt.h>
38290650Shselasky#include <dev/mlx5/driver.h>
39290650Shselasky#include <dev/mlx5/cq.h>
40290650Shselasky#include <dev/mlx5/qp.h>
41290650Shselasky#include <dev/mlx5/srq.h>
42290650Shselasky#include <linux/delay.h>
43290650Shselasky#include <dev/mlx5/mlx5_ifc.h>
44290650Shselasky#include "mlx5_core.h"
45290650Shselasky
46290650ShselaskyMODULE_AUTHOR("Eli Cohen <eli@mellanox.com>");
47290650ShselaskyMODULE_DESCRIPTION("Mellanox Connect-IB, ConnectX-4 core driver");
48290650ShselaskyMODULE_LICENSE("Dual BSD/GPL");
49290650Shselasky#if (__FreeBSD_version >= 1100000)
50290650ShselaskyMODULE_DEPEND(mlx5, linuxkpi, 1, 1, 1);
51290650Shselasky#endif
52290650ShselaskyMODULE_VERSION(mlx5, 1);
53290650Shselasky
54290650Shselaskyint mlx5_core_debug_mask;
55290650Shselaskymodule_param_named(debug_mask, mlx5_core_debug_mask, int, 0644);
56290650ShselaskyMODULE_PARM_DESC(debug_mask, "debug mask: 1 = dump cmd data, 2 = dump cmd exec time, 3 = both. Default=0");
57290650Shselasky
58290650Shselasky#define MLX5_DEFAULT_PROF	2
59290650Shselaskystatic int prof_sel = MLX5_DEFAULT_PROF;
60290650Shselaskymodule_param_named(prof_sel, prof_sel, int, 0444);
61290650ShselaskyMODULE_PARM_DESC(prof_sel, "profile selector. Valid range 0 - 2");
62290650Shselasky
63290650Shselasky#define NUMA_NO_NODE       -1
64290650Shselasky
65290650Shselaskystruct workqueue_struct *mlx5_core_wq;
66290650Shselaskystatic LIST_HEAD(intf_list);
67290650Shselaskystatic LIST_HEAD(dev_list);
68290650Shselaskystatic DEFINE_MUTEX(intf_mutex);
69290650Shselasky
70290650Shselaskystruct mlx5_device_context {
71290650Shselasky	struct list_head	list;
72290650Shselasky	struct mlx5_interface  *intf;
73290650Shselasky	void		       *context;
74290650Shselasky};
75290650Shselasky
76290650Shselaskystatic struct mlx5_profile profiles[] = {
77290650Shselasky	[0] = {
78290650Shselasky		.mask           = 0,
79290650Shselasky	},
80290650Shselasky	[1] = {
81290650Shselasky		.mask		= MLX5_PROF_MASK_QP_SIZE,
82290650Shselasky		.log_max_qp	= 12,
83290650Shselasky	},
84290650Shselasky	[2] = {
85290650Shselasky		.mask		= MLX5_PROF_MASK_QP_SIZE |
86290650Shselasky				  MLX5_PROF_MASK_MR_CACHE,
87290650Shselasky		.log_max_qp	= 17,
88290650Shselasky		.mr_cache[0]	= {
89290650Shselasky			.size	= 500,
90290650Shselasky			.limit	= 250
91290650Shselasky		},
92290650Shselasky		.mr_cache[1]	= {
93290650Shselasky			.size	= 500,
94290650Shselasky			.limit	= 250
95290650Shselasky		},
96290650Shselasky		.mr_cache[2]	= {
97290650Shselasky			.size	= 500,
98290650Shselasky			.limit	= 250
99290650Shselasky		},
100290650Shselasky		.mr_cache[3]	= {
101290650Shselasky			.size	= 500,
102290650Shselasky			.limit	= 250
103290650Shselasky		},
104290650Shselasky		.mr_cache[4]	= {
105290650Shselasky			.size	= 500,
106290650Shselasky			.limit	= 250
107290650Shselasky		},
108290650Shselasky		.mr_cache[5]	= {
109290650Shselasky			.size	= 500,
110290650Shselasky			.limit	= 250
111290650Shselasky		},
112290650Shselasky		.mr_cache[6]	= {
113290650Shselasky			.size	= 500,
114290650Shselasky			.limit	= 250
115290650Shselasky		},
116290650Shselasky		.mr_cache[7]	= {
117290650Shselasky			.size	= 500,
118290650Shselasky			.limit	= 250
119290650Shselasky		},
120290650Shselasky		.mr_cache[8]	= {
121290650Shselasky			.size	= 500,
122290650Shselasky			.limit	= 250
123290650Shselasky		},
124290650Shselasky		.mr_cache[9]	= {
125290650Shselasky			.size	= 500,
126290650Shselasky			.limit	= 250
127290650Shselasky		},
128290650Shselasky		.mr_cache[10]	= {
129290650Shselasky			.size	= 500,
130290650Shselasky			.limit	= 250
131290650Shselasky		},
132290650Shselasky		.mr_cache[11]	= {
133290650Shselasky			.size	= 500,
134290650Shselasky			.limit	= 250
135290650Shselasky		},
136290650Shselasky		.mr_cache[12]	= {
137290650Shselasky			.size	= 64,
138290650Shselasky			.limit	= 32
139290650Shselasky		},
140290650Shselasky		.mr_cache[13]	= {
141290650Shselasky			.size	= 32,
142290650Shselasky			.limit	= 16
143290650Shselasky		},
144290650Shselasky		.mr_cache[14]	= {
145290650Shselasky			.size	= 16,
146290650Shselasky			.limit	= 8
147290650Shselasky		},
148290650Shselasky	},
149290650Shselasky	[3] = {
150290650Shselasky		.mask		= MLX5_PROF_MASK_QP_SIZE,
151290650Shselasky		.log_max_qp	= 17,
152290650Shselasky	},
153290650Shselasky};
154290650Shselasky
155290650Shselaskystatic int set_dma_caps(struct pci_dev *pdev)
156290650Shselasky{
157290650Shselasky	int err;
158290650Shselasky
159290650Shselasky	err = pci_set_dma_mask(pdev, DMA_BIT_MASK(64));
160290650Shselasky	if (err) {
161290650Shselasky		device_printf((&pdev->dev)->bsddev, "WARN: ""Warning: couldn't set 64-bit PCI DMA mask\n");
162290650Shselasky		err = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
163290650Shselasky		if (err) {
164290650Shselasky			device_printf((&pdev->dev)->bsddev, "ERR: ""Can't set PCI DMA mask, aborting\n");
165290650Shselasky			return err;
166290650Shselasky		}
167290650Shselasky	}
168290650Shselasky
169290650Shselasky	err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64));
170290650Shselasky	if (err) {
171290650Shselasky		device_printf((&pdev->dev)->bsddev, "WARN: ""Warning: couldn't set 64-bit consistent PCI DMA mask\n");
172290650Shselasky		err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32));
173290650Shselasky		if (err) {
174290650Shselasky			device_printf((&pdev->dev)->bsddev, "ERR: ""Can't set consistent PCI DMA mask, aborting\n");
175290650Shselasky			return err;
176290650Shselasky		}
177290650Shselasky	}
178290650Shselasky
179290650Shselasky	dma_set_max_seg_size(&pdev->dev, 2u * 1024 * 1024 * 1024);
180290650Shselasky	return err;
181290650Shselasky}
182290650Shselasky
183290650Shselaskystatic int request_bar(struct pci_dev *pdev)
184290650Shselasky{
185290650Shselasky	int err = 0;
186290650Shselasky
187290650Shselasky	if (!(pci_resource_flags(pdev, 0) & IORESOURCE_MEM)) {
188290650Shselasky		device_printf((&pdev->dev)->bsddev, "ERR: ""Missing registers BAR, aborting\n");
189290650Shselasky		return -ENODEV;
190290650Shselasky	}
191290650Shselasky
192290650Shselasky	err = pci_request_regions(pdev, DRIVER_NAME);
193290650Shselasky	if (err)
194290650Shselasky		device_printf((&pdev->dev)->bsddev, "ERR: ""Couldn't get PCI resources, aborting\n");
195290650Shselasky
196290650Shselasky	return err;
197290650Shselasky}
198290650Shselasky
199290650Shselaskystatic void release_bar(struct pci_dev *pdev)
200290650Shselasky{
201290650Shselasky	pci_release_regions(pdev);
202290650Shselasky}
203290650Shselasky
204290650Shselaskystatic int mlx5_enable_msix(struct mlx5_core_dev *dev)
205290650Shselasky{
206290650Shselasky	struct mlx5_priv *priv = &dev->priv;
207290650Shselasky	struct mlx5_eq_table *table = &priv->eq_table;
208290650Shselasky	int num_eqs = 1 << MLX5_CAP_GEN(dev, log_max_eq);
209290650Shselasky	int nvec;
210290650Shselasky	int i;
211290650Shselasky
212290650Shselasky	nvec = MLX5_CAP_GEN(dev, num_ports) * num_online_cpus() +
213290650Shselasky	       MLX5_EQ_VEC_COMP_BASE;
214290650Shselasky	nvec = min_t(int, nvec, num_eqs);
215290650Shselasky	if (nvec <= MLX5_EQ_VEC_COMP_BASE)
216290650Shselasky		return -ENOMEM;
217290650Shselasky
218290650Shselasky	priv->msix_arr = kzalloc(nvec * sizeof(*priv->msix_arr), GFP_KERNEL);
219290650Shselasky
220290650Shselasky	priv->irq_info = kzalloc(nvec * sizeof(*priv->irq_info), GFP_KERNEL);
221290650Shselasky
222290650Shselasky	for (i = 0; i < nvec; i++)
223290650Shselasky		priv->msix_arr[i].entry = i;
224290650Shselasky
225290650Shselasky	nvec = pci_enable_msix_range(dev->pdev, priv->msix_arr,
226290650Shselasky				     MLX5_EQ_VEC_COMP_BASE + 1, nvec);
227290650Shselasky	if (nvec < 0)
228290650Shselasky		return nvec;
229290650Shselasky
230290650Shselasky	table->num_comp_vectors = nvec - MLX5_EQ_VEC_COMP_BASE;
231290650Shselasky
232290650Shselasky	return 0;
233290650Shselasky
234290650Shselasky}
235290650Shselasky
236290650Shselaskystatic void mlx5_disable_msix(struct mlx5_core_dev *dev)
237290650Shselasky{
238290650Shselasky	struct mlx5_priv *priv = &dev->priv;
239290650Shselasky
240290650Shselasky	pci_disable_msix(dev->pdev);
241290650Shselasky	kfree(priv->irq_info);
242290650Shselasky	kfree(priv->msix_arr);
243290650Shselasky}
244290650Shselasky
245290650Shselaskystruct mlx5_reg_host_endianess {
246290650Shselasky	u8	he;
247290650Shselasky	u8      rsvd[15];
248290650Shselasky};
249290650Shselasky
250290650Shselasky
251290650Shselasky#define CAP_MASK(pos, size) ((u64)((1 << (size)) - 1) << (pos))
252290650Shselasky
253290650Shselaskyenum {
254290650Shselasky	MLX5_CAP_BITS_RW_MASK = CAP_MASK(MLX5_CAP_OFF_CMDIF_CSUM, 2) |
255306233Shselasky				MLX5_DEV_CAP_FLAG_DCT |
256306233Shselasky				MLX5_DEV_CAP_FLAG_DRAIN_SIGERR,
257290650Shselasky};
258290650Shselasky
259290650Shselaskystatic u16 to_fw_pkey_sz(u32 size)
260290650Shselasky{
261290650Shselasky	switch (size) {
262290650Shselasky	case 128:
263290650Shselasky		return 0;
264290650Shselasky	case 256:
265290650Shselasky		return 1;
266290650Shselasky	case 512:
267290650Shselasky		return 2;
268290650Shselasky	case 1024:
269290650Shselasky		return 3;
270290650Shselasky	case 2048:
271290650Shselasky		return 4;
272290650Shselasky	case 4096:
273290650Shselasky		return 5;
274290650Shselasky	default:
275290650Shselasky		printf("mlx5_core: WARN: ""invalid pkey table size %d\n", size);
276290650Shselasky		return 0;
277290650Shselasky	}
278290650Shselasky}
279290650Shselasky
280290650Shselaskyint mlx5_core_get_caps(struct mlx5_core_dev *dev, enum mlx5_cap_type cap_type,
281290650Shselasky		       enum mlx5_cap_mode cap_mode)
282290650Shselasky{
283290650Shselasky	u8 in[MLX5_ST_SZ_BYTES(query_hca_cap_in)];
284290650Shselasky	int out_sz = MLX5_ST_SZ_BYTES(query_hca_cap_out);
285290650Shselasky	void *out, *hca_caps;
286290650Shselasky	u16 opmod = (cap_type << 1) | (cap_mode & 0x01);
287290650Shselasky	int err;
288290650Shselasky
289290650Shselasky	memset(in, 0, sizeof(in));
290290650Shselasky	out = kzalloc(out_sz, GFP_KERNEL);
291290650Shselasky
292290650Shselasky	MLX5_SET(query_hca_cap_in, in, opcode, MLX5_CMD_OP_QUERY_HCA_CAP);
293290650Shselasky	MLX5_SET(query_hca_cap_in, in, op_mod, opmod);
294290650Shselasky	err = mlx5_cmd_exec(dev, in, sizeof(in), out, out_sz);
295290650Shselasky	if (err)
296290650Shselasky		goto query_ex;
297290650Shselasky
298290650Shselasky	err = mlx5_cmd_status_to_err_v2(out);
299290650Shselasky	if (err) {
300290650Shselasky		mlx5_core_warn(dev,
301290650Shselasky			       "QUERY_HCA_CAP : type(%x) opmode(%x) Failed(%d)\n",
302290650Shselasky			       cap_type, cap_mode, err);
303290650Shselasky		goto query_ex;
304290650Shselasky	}
305290650Shselasky
306290650Shselasky	hca_caps =  MLX5_ADDR_OF(query_hca_cap_out, out, capability);
307290650Shselasky
308290650Shselasky	switch (cap_mode) {
309290650Shselasky	case HCA_CAP_OPMOD_GET_MAX:
310290650Shselasky		memcpy(dev->hca_caps_max[cap_type], hca_caps,
311290650Shselasky		       MLX5_UN_SZ_BYTES(hca_cap_union));
312290650Shselasky		break;
313290650Shselasky	case HCA_CAP_OPMOD_GET_CUR:
314290650Shselasky		memcpy(dev->hca_caps_cur[cap_type], hca_caps,
315290650Shselasky		       MLX5_UN_SZ_BYTES(hca_cap_union));
316290650Shselasky		break;
317290650Shselasky	default:
318290650Shselasky		mlx5_core_warn(dev,
319290650Shselasky			       "Tried to query dev cap type(%x) with wrong opmode(%x)\n",
320290650Shselasky			       cap_type, cap_mode);
321290650Shselasky		err = -EINVAL;
322290650Shselasky		break;
323290650Shselasky	}
324290650Shselaskyquery_ex:
325290650Shselasky	kfree(out);
326290650Shselasky	return err;
327290650Shselasky}
328290650Shselasky
329290650Shselaskystatic int set_caps(struct mlx5_core_dev *dev, void *in, int in_sz)
330290650Shselasky{
331290650Shselasky	u32 out[MLX5_ST_SZ_DW(set_hca_cap_out)];
332290650Shselasky	int err;
333290650Shselasky
334290650Shselasky	memset(out, 0, sizeof(out));
335290650Shselasky
336290650Shselasky	MLX5_SET(set_hca_cap_in, in, opcode, MLX5_CMD_OP_SET_HCA_CAP);
337290650Shselasky	err = mlx5_cmd_exec(dev, in, in_sz, out, sizeof(out));
338290650Shselasky	if (err)
339290650Shselasky		return err;
340290650Shselasky
341290650Shselasky	err = mlx5_cmd_status_to_err_v2(out);
342290650Shselasky
343290650Shselasky	return err;
344290650Shselasky}
345290650Shselasky
346290650Shselaskystatic int handle_hca_cap(struct mlx5_core_dev *dev)
347290650Shselasky{
348290650Shselasky	void *set_ctx = NULL;
349290650Shselasky	struct mlx5_profile *prof = dev->profile;
350290650Shselasky	int err = -ENOMEM;
351290650Shselasky	int set_sz = MLX5_ST_SZ_BYTES(set_hca_cap_in);
352290650Shselasky	void *set_hca_cap;
353290650Shselasky
354290650Shselasky	set_ctx = kzalloc(set_sz, GFP_KERNEL);
355290650Shselasky
356290650Shselasky	err = mlx5_core_get_caps(dev, MLX5_CAP_GENERAL, HCA_CAP_OPMOD_GET_MAX);
357290650Shselasky	if (err)
358290650Shselasky		goto query_ex;
359290650Shselasky
360290650Shselasky	err = mlx5_core_get_caps(dev, MLX5_CAP_GENERAL, HCA_CAP_OPMOD_GET_CUR);
361290650Shselasky	if (err)
362290650Shselasky		goto query_ex;
363290650Shselasky
364290650Shselasky	set_hca_cap = MLX5_ADDR_OF(set_hca_cap_in, set_ctx,
365290650Shselasky				   capability);
366290650Shselasky	memcpy(set_hca_cap, dev->hca_caps_cur[MLX5_CAP_GENERAL],
367290650Shselasky	       MLX5_ST_SZ_BYTES(cmd_hca_cap));
368290650Shselasky
369290650Shselasky	mlx5_core_dbg(dev, "Current Pkey table size %d Setting new size %d\n",
370290650Shselasky		      mlx5_to_sw_pkey_sz(MLX5_CAP_GEN(dev, pkey_table_size)),
371290650Shselasky		      128);
372290650Shselasky	/* we limit the size of the pkey table to 128 entries for now */
373290650Shselasky	MLX5_SET(cmd_hca_cap, set_hca_cap, pkey_table_size,
374290650Shselasky		 to_fw_pkey_sz(128));
375290650Shselasky
376290650Shselasky	if (prof->mask & MLX5_PROF_MASK_QP_SIZE)
377290650Shselasky		MLX5_SET(cmd_hca_cap, set_hca_cap, log_max_qp,
378290650Shselasky			 prof->log_max_qp);
379290650Shselasky
380290650Shselasky	/* disable cmdif checksum */
381290650Shselasky	MLX5_SET(cmd_hca_cap, set_hca_cap, cmdif_checksum, 0);
382290650Shselasky
383306233Shselasky	/* enable drain sigerr */
384306233Shselasky	MLX5_SET(cmd_hca_cap, set_hca_cap, drain_sigerr, 1);
385306233Shselasky
386290650Shselasky	MLX5_SET(cmd_hca_cap, set_hca_cap, log_uar_page_sz, PAGE_SHIFT - 12);
387290650Shselasky
388290650Shselasky	err = set_caps(dev, set_ctx, set_sz);
389290650Shselasky
390290650Shselaskyquery_ex:
391290650Shselasky	kfree(set_ctx);
392290650Shselasky	return err;
393290650Shselasky}
394290650Shselasky
395290650Shselaskystatic int set_hca_ctrl(struct mlx5_core_dev *dev)
396290650Shselasky{
397290650Shselasky	struct mlx5_reg_host_endianess he_in;
398290650Shselasky	struct mlx5_reg_host_endianess he_out;
399290650Shselasky	int err;
400290650Shselasky
401306233Shselasky	if (MLX5_CAP_GEN(dev, port_type) == MLX5_CAP_PORT_TYPE_ETH &&
402306233Shselasky	    !MLX5_CAP_GEN(dev, roce))
403306233Shselasky		return 0;
404306233Shselasky
405290650Shselasky	memset(&he_in, 0, sizeof(he_in));
406290650Shselasky	he_in.he = MLX5_SET_HOST_ENDIANNESS;
407290650Shselasky	err = mlx5_core_access_reg(dev, &he_in,  sizeof(he_in),
408290650Shselasky					&he_out, sizeof(he_out),
409290650Shselasky					MLX5_REG_HOST_ENDIANNESS, 0, 1);
410290650Shselasky	return err;
411290650Shselasky}
412290650Shselasky
413290650Shselaskystatic int mlx5_core_enable_hca(struct mlx5_core_dev *dev)
414290650Shselasky{
415290650Shselasky	u32 in[MLX5_ST_SZ_DW(enable_hca_in)];
416290650Shselasky	u32 out[MLX5_ST_SZ_DW(enable_hca_out)];
417290650Shselasky
418290650Shselasky	memset(in, 0, sizeof(in));
419290650Shselasky	MLX5_SET(enable_hca_in, in, opcode, MLX5_CMD_OP_ENABLE_HCA);
420290650Shselasky	memset(out, 0, sizeof(out));
421290650Shselasky	return mlx5_cmd_exec_check_status(dev, in,  sizeof(in),
422290650Shselasky					       out, sizeof(out));
423290650Shselasky}
424290650Shselasky
425290650Shselaskystatic int mlx5_core_disable_hca(struct mlx5_core_dev *dev)
426290650Shselasky{
427290650Shselasky	u32 in[MLX5_ST_SZ_DW(disable_hca_in)];
428290650Shselasky	u32 out[MLX5_ST_SZ_DW(disable_hca_out)];
429290650Shselasky
430290650Shselasky	memset(in, 0, sizeof(in));
431290650Shselasky
432290650Shselasky	MLX5_SET(disable_hca_in, in, opcode, MLX5_CMD_OP_DISABLE_HCA);
433290650Shselasky	memset(out, 0, sizeof(out));
434290650Shselasky	return mlx5_cmd_exec_check_status(dev, in,  sizeof(in),
435290650Shselasky					       out, sizeof(out));
436290650Shselasky}
437290650Shselasky
438290650Shselaskystatic int mlx5_core_set_issi(struct mlx5_core_dev *dev)
439290650Shselasky{
440290650Shselasky	u32 query_in[MLX5_ST_SZ_DW(query_issi_in)];
441290650Shselasky	u32 query_out[MLX5_ST_SZ_DW(query_issi_out)];
442290650Shselasky	u32 set_in[MLX5_ST_SZ_DW(set_issi_in)];
443290650Shselasky	u32 set_out[MLX5_ST_SZ_DW(set_issi_out)];
444290650Shselasky	int err;
445290650Shselasky	u32 sup_issi;
446290650Shselasky
447290650Shselasky	memset(query_in, 0, sizeof(query_in));
448290650Shselasky	memset(query_out, 0, sizeof(query_out));
449290650Shselasky
450290650Shselasky	MLX5_SET(query_issi_in, query_in, opcode, MLX5_CMD_OP_QUERY_ISSI);
451290650Shselasky
452290650Shselasky	err = mlx5_cmd_exec_check_status(dev, query_in, sizeof(query_in),
453290650Shselasky					 query_out, sizeof(query_out));
454290650Shselasky	if (err) {
455290650Shselasky		if (((struct mlx5_outbox_hdr *)query_out)->status ==
456290650Shselasky		    MLX5_CMD_STAT_BAD_OP_ERR) {
457290650Shselasky			pr_debug("Only ISSI 0 is supported\n");
458290650Shselasky			return 0;
459290650Shselasky		}
460290650Shselasky
461290650Shselasky		printf("mlx5_core: ERR: ""failed to query ISSI\n");
462290650Shselasky		return err;
463290650Shselasky	}
464290650Shselasky
465290650Shselasky	sup_issi = MLX5_GET(query_issi_out, query_out, supported_issi_dw0);
466290650Shselasky
467290650Shselasky	if (sup_issi & (1 << 1)) {
468290650Shselasky		memset(set_in, 0, sizeof(set_in));
469290650Shselasky		memset(set_out, 0, sizeof(set_out));
470290650Shselasky
471290650Shselasky		MLX5_SET(set_issi_in, set_in, opcode, MLX5_CMD_OP_SET_ISSI);
472290650Shselasky		MLX5_SET(set_issi_in, set_in, current_issi, 1);
473290650Shselasky
474290650Shselasky		err = mlx5_cmd_exec_check_status(dev, set_in, sizeof(set_in),
475290650Shselasky						 set_out, sizeof(set_out));
476290650Shselasky		if (err) {
477290650Shselasky			printf("mlx5_core: ERR: ""failed to set ISSI=1\n");
478290650Shselasky			return err;
479290650Shselasky		}
480290650Shselasky
481290650Shselasky		dev->issi = 1;
482290650Shselasky
483290650Shselasky		return 0;
484290650Shselasky	} else if (sup_issi & (1 << 0)) {
485290650Shselasky		return 0;
486290650Shselasky	}
487290650Shselasky
488290650Shselasky	return -ENOTSUPP;
489290650Shselasky}
490290650Shselasky
491290650Shselasky
492290650Shselaskyint mlx5_vector2eqn(struct mlx5_core_dev *dev, int vector, int *eqn, int *irqn)
493290650Shselasky{
494290650Shselasky	struct mlx5_eq_table *table = &dev->priv.eq_table;
495290650Shselasky	struct mlx5_eq *eq;
496290650Shselasky	int err = -ENOENT;
497290650Shselasky
498290650Shselasky	spin_lock(&table->lock);
499290650Shselasky	list_for_each_entry(eq, &table->comp_eqs_list, list) {
500290650Shselasky		if (eq->index == vector) {
501290650Shselasky			*eqn = eq->eqn;
502290650Shselasky			*irqn = eq->irqn;
503290650Shselasky			err = 0;
504290650Shselasky			break;
505290650Shselasky		}
506290650Shselasky	}
507290650Shselasky	spin_unlock(&table->lock);
508290650Shselasky
509290650Shselasky	return err;
510290650Shselasky}
511290650ShselaskyEXPORT_SYMBOL(mlx5_vector2eqn);
512290650Shselasky
513290650Shselaskyint mlx5_rename_eq(struct mlx5_core_dev *dev, int eq_ix, char *name)
514290650Shselasky{
515290650Shselasky	struct mlx5_priv *priv = &dev->priv;
516290650Shselasky	struct mlx5_eq_table *table = &priv->eq_table;
517290650Shselasky	struct mlx5_eq *eq;
518290650Shselasky	int err = -ENOENT;
519290650Shselasky
520290650Shselasky	spin_lock(&table->lock);
521290650Shselasky	list_for_each_entry(eq, &table->comp_eqs_list, list) {
522290650Shselasky		if (eq->index == eq_ix) {
523290650Shselasky			int irq_ix = eq_ix + MLX5_EQ_VEC_COMP_BASE;
524290650Shselasky
525290650Shselasky			snprintf(priv->irq_info[irq_ix].name, MLX5_MAX_IRQ_NAME,
526290650Shselasky				 "%s-%d", name, eq_ix);
527290650Shselasky
528290650Shselasky			err = 0;
529290650Shselasky			break;
530290650Shselasky		}
531290650Shselasky	}
532290650Shselasky	spin_unlock(&table->lock);
533290650Shselasky
534290650Shselasky	return err;
535290650Shselasky}
536290650Shselasky
537290650Shselaskystatic void free_comp_eqs(struct mlx5_core_dev *dev)
538290650Shselasky{
539290650Shselasky	struct mlx5_eq_table *table = &dev->priv.eq_table;
540290650Shselasky	struct mlx5_eq *eq, *n;
541290650Shselasky
542290650Shselasky	spin_lock(&table->lock);
543290650Shselasky	list_for_each_entry_safe(eq, n, &table->comp_eqs_list, list) {
544290650Shselasky		list_del(&eq->list);
545290650Shselasky		spin_unlock(&table->lock);
546290650Shselasky		if (mlx5_destroy_unmap_eq(dev, eq))
547290650Shselasky			mlx5_core_warn(dev, "failed to destroy EQ 0x%x\n",
548290650Shselasky				       eq->eqn);
549290650Shselasky		kfree(eq);
550290650Shselasky		spin_lock(&table->lock);
551290650Shselasky	}
552290650Shselasky	spin_unlock(&table->lock);
553290650Shselasky}
554290650Shselasky
555290650Shselaskystatic int alloc_comp_eqs(struct mlx5_core_dev *dev)
556290650Shselasky{
557290650Shselasky	struct mlx5_eq_table *table = &dev->priv.eq_table;
558290650Shselasky	char name[MLX5_MAX_IRQ_NAME];
559290650Shselasky	struct mlx5_eq *eq;
560290650Shselasky	int ncomp_vec;
561290650Shselasky	int nent;
562290650Shselasky	int err;
563290650Shselasky	int i;
564290650Shselasky
565290650Shselasky	INIT_LIST_HEAD(&table->comp_eqs_list);
566290650Shselasky	ncomp_vec = table->num_comp_vectors;
567290650Shselasky	nent = MLX5_COMP_EQ_SIZE;
568290650Shselasky	for (i = 0; i < ncomp_vec; i++) {
569290650Shselasky		eq = kzalloc(sizeof(*eq), GFP_KERNEL);
570290650Shselasky
571290650Shselasky		snprintf(name, MLX5_MAX_IRQ_NAME, "mlx5_comp%d", i);
572290650Shselasky		err = mlx5_create_map_eq(dev, eq,
573290650Shselasky					 i + MLX5_EQ_VEC_COMP_BASE, nent, 0,
574290650Shselasky					 name, &dev->priv.uuari.uars[0]);
575290650Shselasky		if (err) {
576290650Shselasky			kfree(eq);
577290650Shselasky			goto clean;
578290650Shselasky		}
579290650Shselasky		mlx5_core_dbg(dev, "allocated completion EQN %d\n", eq->eqn);
580290650Shselasky		eq->index = i;
581290650Shselasky		spin_lock(&table->lock);
582290650Shselasky		list_add_tail(&eq->list, &table->comp_eqs_list);
583290650Shselasky		spin_unlock(&table->lock);
584290650Shselasky	}
585290650Shselasky
586290650Shselasky	return 0;
587290650Shselasky
588290650Shselaskyclean:
589290650Shselasky	free_comp_eqs(dev);
590290650Shselasky	return err;
591290650Shselasky}
592290650Shselasky
593290650Shselaskystatic int map_bf_area(struct mlx5_core_dev *dev)
594290650Shselasky{
595290650Shselasky	resource_size_t bf_start = pci_resource_start(dev->pdev, 0);
596290650Shselasky	resource_size_t bf_len = pci_resource_len(dev->pdev, 0);
597290650Shselasky
598290650Shselasky	dev->priv.bf_mapping = io_mapping_create_wc(bf_start, bf_len);
599290650Shselasky
600290650Shselasky	return dev->priv.bf_mapping ? 0 : -ENOMEM;
601290650Shselasky}
602290650Shselasky
603290650Shselaskystatic void unmap_bf_area(struct mlx5_core_dev *dev)
604290650Shselasky{
605290650Shselasky	if (dev->priv.bf_mapping)
606290650Shselasky		io_mapping_free(dev->priv.bf_mapping);
607290650Shselasky}
608290650Shselasky
609290650Shselaskystatic inline int fw_initializing(struct mlx5_core_dev *dev)
610290650Shselasky{
611290650Shselasky	return ioread32be(&dev->iseg->initializing) >> 31;
612290650Shselasky}
613290650Shselasky
614290650Shselaskystatic int wait_fw_init(struct mlx5_core_dev *dev, u32 max_wait_mili)
615290650Shselasky{
616290650Shselasky	u64 end = jiffies + msecs_to_jiffies(max_wait_mili);
617290650Shselasky	int err = 0;
618290650Shselasky
619290650Shselasky	while (fw_initializing(dev)) {
620290650Shselasky		if (time_after(jiffies, end)) {
621290650Shselasky			err = -EBUSY;
622290650Shselasky			break;
623290650Shselasky		}
624290650Shselasky		msleep(FW_INIT_WAIT_MS);
625290650Shselasky	}
626290650Shselasky
627290650Shselasky	return err;
628290650Shselasky}
629290650Shselasky
630290650Shselaskystatic int mlx5_dev_init(struct mlx5_core_dev *dev, struct pci_dev *pdev)
631290650Shselasky{
632290650Shselasky	struct mlx5_priv *priv = &dev->priv;
633290650Shselasky	int err;
634290650Shselasky
635290650Shselasky	dev->pdev = pdev;
636290650Shselasky	pci_set_drvdata(dev->pdev, dev);
637290650Shselasky	strncpy(priv->name, dev_name(&pdev->dev), MLX5_MAX_NAME_LEN);
638290650Shselasky	priv->name[MLX5_MAX_NAME_LEN - 1] = 0;
639290650Shselasky
640290650Shselasky	mutex_init(&priv->pgdir_mutex);
641290650Shselasky	INIT_LIST_HEAD(&priv->pgdir_list);
642290650Shselasky	spin_lock_init(&priv->mkey_lock);
643290650Shselasky
644290650Shselasky	priv->numa_node = NUMA_NO_NODE;
645290650Shselasky
646290650Shselasky	err = pci_enable_device(pdev);
647290650Shselasky	if (err) {
648290650Shselasky		device_printf((&pdev->dev)->bsddev, "ERR: ""Cannot enable PCI device, aborting\n");
649290650Shselasky		goto err_dbg;
650290650Shselasky	}
651290650Shselasky
652290650Shselasky	err = request_bar(pdev);
653290650Shselasky	if (err) {
654290650Shselasky		device_printf((&pdev->dev)->bsddev, "ERR: ""error requesting BARs, aborting\n");
655290650Shselasky		goto err_disable;
656290650Shselasky	}
657290650Shselasky
658290650Shselasky	pci_set_master(pdev);
659290650Shselasky
660290650Shselasky	err = set_dma_caps(pdev);
661290650Shselasky	if (err) {
662290650Shselasky		device_printf((&pdev->dev)->bsddev, "ERR: ""Failed setting DMA capabilities mask, aborting\n");
663290650Shselasky		goto err_clr_master;
664290650Shselasky	}
665290650Shselasky
666290650Shselasky	dev->iseg = ioremap(pci_resource_start(dev->pdev, 0),
667290650Shselasky			    sizeof(*dev->iseg));
668290650Shselasky	if (!dev->iseg) {
669290650Shselasky		err = -ENOMEM;
670290650Shselasky		device_printf((&pdev->dev)->bsddev, "ERR: ""Failed mapping initialization segment, aborting\n");
671290650Shselasky		goto err_clr_master;
672290650Shselasky	}
673290650Shselasky	device_printf((&pdev->dev)->bsddev, "INFO: ""firmware version: %d.%d.%d\n", fw_rev_maj(dev), fw_rev_min(dev), fw_rev_sub(dev));
674290650Shselasky
675306233Shselasky	/*
676306233Shselasky	 * On load removing any previous indication of internal error,
677306233Shselasky	 * device is up
678306233Shselasky	 */
679306233Shselasky	dev->state = MLX5_DEVICE_STATE_UP;
680306233Shselasky
681290650Shselasky	err = mlx5_cmd_init(dev);
682290650Shselasky	if (err) {
683290650Shselasky		device_printf((&pdev->dev)->bsddev, "ERR: ""Failed initializing command interface, aborting\n");
684290650Shselasky		goto err_unmap;
685290650Shselasky	}
686290650Shselasky
687290650Shselasky	err = wait_fw_init(dev, FW_INIT_TIMEOUT_MILI);
688290650Shselasky	if (err) {
689290650Shselasky		device_printf((&dev->pdev->dev)->bsddev, "ERR: ""Firmware over %d MS in initializing state, aborting\n", FW_INIT_TIMEOUT_MILI);
690290650Shselasky		goto err_cmd_cleanup;
691290650Shselasky	}
692290650Shselasky
693290650Shselasky	mlx5_pagealloc_init(dev);
694290650Shselasky
695290650Shselasky	err = mlx5_core_enable_hca(dev);
696290650Shselasky	if (err) {
697290650Shselasky		device_printf((&pdev->dev)->bsddev, "ERR: ""enable hca failed\n");
698290650Shselasky		goto err_pagealloc_cleanup;
699290650Shselasky	}
700290650Shselasky
701290650Shselasky	err = mlx5_core_set_issi(dev);
702290650Shselasky	if (err) {
703290650Shselasky		device_printf((&pdev->dev)->bsddev, "ERR: ""failed to set issi\n");
704290650Shselasky		goto err_disable_hca;
705290650Shselasky	}
706290650Shselasky
707290650Shselasky	err = mlx5_pagealloc_start(dev);
708290650Shselasky	if (err) {
709290650Shselasky		device_printf((&pdev->dev)->bsddev, "ERR: ""mlx5_pagealloc_start failed\n");
710290650Shselasky		goto err_disable_hca;
711290650Shselasky	}
712290650Shselasky
713290650Shselasky	err = mlx5_satisfy_startup_pages(dev, 1);
714290650Shselasky	if (err) {
715290650Shselasky		device_printf((&pdev->dev)->bsddev, "ERR: ""failed to allocate boot pages\n");
716290650Shselasky		goto err_pagealloc_stop;
717290650Shselasky	}
718290650Shselasky
719306233Shselasky	err = handle_hca_cap(dev);
720290650Shselasky	if (err) {
721306233Shselasky		device_printf((&pdev->dev)->bsddev, "ERR: ""handle_hca_cap failed\n");
722290650Shselasky		goto reclaim_boot_pages;
723290650Shselasky	}
724290650Shselasky
725306233Shselasky	err = set_hca_ctrl(dev);
726290650Shselasky	if (err) {
727306233Shselasky		device_printf((&pdev->dev)->bsddev, "ERR: ""set_hca_ctrl failed\n");
728290650Shselasky		goto reclaim_boot_pages;
729290650Shselasky	}
730290650Shselasky
731290650Shselasky	err = mlx5_satisfy_startup_pages(dev, 0);
732290650Shselasky	if (err) {
733290650Shselasky		device_printf((&pdev->dev)->bsddev, "ERR: ""failed to allocate init pages\n");
734290650Shselasky		goto reclaim_boot_pages;
735290650Shselasky	}
736290650Shselasky
737290650Shselasky	err = mlx5_cmd_init_hca(dev);
738290650Shselasky	if (err) {
739290650Shselasky		device_printf((&pdev->dev)->bsddev, "ERR: ""init hca failed\n");
740290650Shselasky		goto reclaim_boot_pages;
741290650Shselasky	}
742290650Shselasky
743290650Shselasky	mlx5_start_health_poll(dev);
744290650Shselasky
745290650Shselasky	err = mlx5_query_hca_caps(dev);
746290650Shselasky	if (err) {
747290650Shselasky		device_printf((&pdev->dev)->bsddev, "ERR: ""query hca failed\n");
748290650Shselasky		goto err_stop_poll;
749290650Shselasky	}
750290650Shselasky
751290650Shselasky	err = mlx5_query_board_id(dev);
752290650Shselasky	if (err) {
753290650Shselasky		device_printf((&pdev->dev)->bsddev, "ERR: ""query board id failed\n");
754290650Shselasky		goto err_stop_poll;
755290650Shselasky	}
756290650Shselasky
757290650Shselasky	err = mlx5_enable_msix(dev);
758290650Shselasky	if (err) {
759290650Shselasky		device_printf((&pdev->dev)->bsddev, "ERR: ""enable msix failed\n");
760290650Shselasky		goto err_stop_poll;
761290650Shselasky	}
762290650Shselasky
763290650Shselasky	err = mlx5_eq_init(dev);
764290650Shselasky	if (err) {
765290650Shselasky		device_printf((&pdev->dev)->bsddev, "ERR: ""failed to initialize eq\n");
766290650Shselasky		goto disable_msix;
767290650Shselasky	}
768290650Shselasky
769290650Shselasky	err = mlx5_alloc_uuars(dev, &priv->uuari);
770290650Shselasky	if (err) {
771290650Shselasky		device_printf((&pdev->dev)->bsddev, "ERR: ""Failed allocating uar, aborting\n");
772290650Shselasky		goto err_eq_cleanup;
773290650Shselasky	}
774290650Shselasky
775290650Shselasky	err = mlx5_start_eqs(dev);
776290650Shselasky	if (err) {
777290650Shselasky		device_printf((&pdev->dev)->bsddev, "ERR: ""Failed to start pages and async EQs\n");
778290650Shselasky		goto err_free_uar;
779290650Shselasky	}
780290650Shselasky
781290650Shselasky	err = alloc_comp_eqs(dev);
782290650Shselasky	if (err) {
783290650Shselasky		device_printf((&pdev->dev)->bsddev, "ERR: ""Failed to alloc completion EQs\n");
784290650Shselasky		goto err_stop_eqs;
785290650Shselasky	}
786290650Shselasky
787290650Shselasky	if (map_bf_area(dev))
788290650Shselasky		device_printf((&pdev->dev)->bsddev, "ERR: ""Failed to map blue flame area\n");
789290650Shselasky
790290650Shselasky	MLX5_INIT_DOORBELL_LOCK(&priv->cq_uar_lock);
791290650Shselasky
792290650Shselasky	mlx5_init_cq_table(dev);
793290650Shselasky	mlx5_init_qp_table(dev);
794290650Shselasky	mlx5_init_srq_table(dev);
795290650Shselasky	mlx5_init_mr_table(dev);
796290650Shselasky
797290650Shselasky	return 0;
798290650Shselasky
799290650Shselaskyerr_stop_eqs:
800290650Shselasky	mlx5_stop_eqs(dev);
801290650Shselasky
802290650Shselaskyerr_free_uar:
803290650Shselasky	mlx5_free_uuars(dev, &priv->uuari);
804290650Shselasky
805290650Shselaskyerr_eq_cleanup:
806290650Shselasky	mlx5_eq_cleanup(dev);
807290650Shselasky
808290650Shselaskydisable_msix:
809290650Shselasky	mlx5_disable_msix(dev);
810290650Shselasky
811290650Shselaskyerr_stop_poll:
812290650Shselasky	mlx5_stop_health_poll(dev);
813290650Shselasky	if (mlx5_cmd_teardown_hca(dev)) {
814290650Shselasky		device_printf((&dev->pdev->dev)->bsddev, "ERR: ""tear_down_hca failed, skip cleanup\n");
815290650Shselasky		return err;
816290650Shselasky	}
817290650Shselasky
818290650Shselaskyreclaim_boot_pages:
819290650Shselasky	mlx5_reclaim_startup_pages(dev);
820290650Shselasky
821290650Shselaskyerr_pagealloc_stop:
822290650Shselasky	mlx5_pagealloc_stop(dev);
823290650Shselasky
824290650Shselaskyerr_disable_hca:
825290650Shselasky	mlx5_core_disable_hca(dev);
826290650Shselasky
827290650Shselaskyerr_pagealloc_cleanup:
828290650Shselasky	mlx5_pagealloc_cleanup(dev);
829290650Shselaskyerr_cmd_cleanup:
830290650Shselasky	mlx5_cmd_cleanup(dev);
831290650Shselasky
832290650Shselaskyerr_unmap:
833290650Shselasky	iounmap(dev->iseg);
834290650Shselasky
835290650Shselaskyerr_clr_master:
836290650Shselasky	pci_clear_master(dev->pdev);
837290650Shselasky	release_bar(dev->pdev);
838290650Shselasky
839290650Shselaskyerr_disable:
840290650Shselasky	pci_disable_device(dev->pdev);
841290650Shselasky
842290650Shselaskyerr_dbg:
843306233Shselasky	dev->state = MLX5_DEVICE_STATE_INTERNAL_ERROR;
844290650Shselasky	return err;
845290650Shselasky}
846290650Shselasky
847290650Shselaskystatic void mlx5_dev_cleanup(struct mlx5_core_dev *dev)
848290650Shselasky{
849290650Shselasky	struct mlx5_priv *priv = &dev->priv;
850290650Shselasky
851290650Shselasky	mlx5_cleanup_mr_table(dev);
852290650Shselasky	mlx5_cleanup_srq_table(dev);
853290650Shselasky	mlx5_cleanup_qp_table(dev);
854290650Shselasky	mlx5_cleanup_cq_table(dev);
855290650Shselasky	unmap_bf_area(dev);
856290650Shselasky	free_comp_eqs(dev);
857290650Shselasky	mlx5_stop_eqs(dev);
858290650Shselasky	mlx5_free_uuars(dev, &priv->uuari);
859290650Shselasky	mlx5_eq_cleanup(dev);
860290650Shselasky	mlx5_disable_msix(dev);
861290650Shselasky	mlx5_stop_health_poll(dev);
862290650Shselasky	if (mlx5_cmd_teardown_hca(dev)) {
863290650Shselasky		device_printf((&dev->pdev->dev)->bsddev, "ERR: ""tear_down_hca failed, skip cleanup\n");
864290650Shselasky		return;
865290650Shselasky	}
866290650Shselasky	mlx5_pagealloc_stop(dev);
867290650Shselasky	mlx5_reclaim_startup_pages(dev);
868290650Shselasky	mlx5_core_disable_hca(dev);
869290650Shselasky	mlx5_pagealloc_cleanup(dev);
870290650Shselasky	mlx5_cmd_cleanup(dev);
871290650Shselasky	iounmap(dev->iseg);
872290650Shselasky	pci_clear_master(dev->pdev);
873290650Shselasky	release_bar(dev->pdev);
874290650Shselasky	pci_disable_device(dev->pdev);
875290650Shselasky}
876290650Shselasky
877290650Shselaskystatic void mlx5_add_device(struct mlx5_interface *intf, struct mlx5_priv *priv)
878290650Shselasky{
879290650Shselasky	struct mlx5_device_context *dev_ctx;
880290650Shselasky	struct mlx5_core_dev *dev = container_of(priv, struct mlx5_core_dev, priv);
881290650Shselasky
882290650Shselasky	dev_ctx = kmalloc(sizeof(*dev_ctx), GFP_KERNEL);
883290650Shselasky
884290650Shselasky	dev_ctx->intf    = intf;
885290650Shselasky	dev_ctx->context = intf->add(dev);
886290650Shselasky
887290650Shselasky	if (dev_ctx->context) {
888290650Shselasky		spin_lock_irq(&priv->ctx_lock);
889290650Shselasky		list_add_tail(&dev_ctx->list, &priv->ctx_list);
890290650Shselasky		spin_unlock_irq(&priv->ctx_lock);
891290650Shselasky	} else {
892290650Shselasky		kfree(dev_ctx);
893290650Shselasky	}
894290650Shselasky}
895290650Shselasky
896290650Shselaskystatic void mlx5_remove_device(struct mlx5_interface *intf, struct mlx5_priv *priv)
897290650Shselasky{
898290650Shselasky	struct mlx5_device_context *dev_ctx;
899290650Shselasky	struct mlx5_core_dev *dev = container_of(priv, struct mlx5_core_dev, priv);
900290650Shselasky
901290650Shselasky	list_for_each_entry(dev_ctx, &priv->ctx_list, list)
902290650Shselasky		if (dev_ctx->intf == intf) {
903290650Shselasky			spin_lock_irq(&priv->ctx_lock);
904290650Shselasky			list_del(&dev_ctx->list);
905290650Shselasky			spin_unlock_irq(&priv->ctx_lock);
906290650Shselasky
907290650Shselasky			intf->remove(dev, dev_ctx->context);
908290650Shselasky			kfree(dev_ctx);
909290650Shselasky			return;
910290650Shselasky		}
911290650Shselasky}
912290650Shselaskystatic int mlx5_register_device(struct mlx5_core_dev *dev)
913290650Shselasky{
914290650Shselasky	struct mlx5_priv *priv = &dev->priv;
915290650Shselasky	struct mlx5_interface *intf;
916290650Shselasky
917290650Shselasky	mutex_lock(&intf_mutex);
918290650Shselasky	list_add_tail(&priv->dev_list, &dev_list);
919290650Shselasky	list_for_each_entry(intf, &intf_list, list)
920290650Shselasky		mlx5_add_device(intf, priv);
921290650Shselasky	mutex_unlock(&intf_mutex);
922290650Shselasky
923290650Shselasky	return 0;
924290650Shselasky}
925290650Shselaskystatic void mlx5_unregister_device(struct mlx5_core_dev *dev)
926290650Shselasky{
927290650Shselasky	struct mlx5_priv *priv = &dev->priv;
928290650Shselasky	struct mlx5_interface *intf;
929290650Shselasky
930290650Shselasky	mutex_lock(&intf_mutex);
931290650Shselasky	list_for_each_entry(intf, &intf_list, list)
932290650Shselasky		mlx5_remove_device(intf, priv);
933290650Shselasky	list_del(&priv->dev_list);
934290650Shselasky	mutex_unlock(&intf_mutex);
935290650Shselasky}
936290650Shselasky
937290650Shselaskyint mlx5_register_interface(struct mlx5_interface *intf)
938290650Shselasky{
939290650Shselasky	struct mlx5_priv *priv;
940290650Shselasky
941290650Shselasky	if (!intf->add || !intf->remove)
942290650Shselasky		return -EINVAL;
943290650Shselasky
944290650Shselasky	mutex_lock(&intf_mutex);
945290650Shselasky	list_add_tail(&intf->list, &intf_list);
946290650Shselasky	list_for_each_entry(priv, &dev_list, dev_list)
947290650Shselasky		mlx5_add_device(intf, priv);
948290650Shselasky	mutex_unlock(&intf_mutex);
949290650Shselasky
950290650Shselasky	return 0;
951290650Shselasky}
952290650ShselaskyEXPORT_SYMBOL(mlx5_register_interface);
953290650Shselasky
954290650Shselaskyvoid mlx5_unregister_interface(struct mlx5_interface *intf)
955290650Shselasky{
956290650Shselasky	struct mlx5_priv *priv;
957290650Shselasky
958290650Shselasky	mutex_lock(&intf_mutex);
959290650Shselasky	list_for_each_entry(priv, &dev_list, dev_list)
960290650Shselasky	       mlx5_remove_device(intf, priv);
961290650Shselasky	list_del(&intf->list);
962290650Shselasky	mutex_unlock(&intf_mutex);
963290650Shselasky}
964290650ShselaskyEXPORT_SYMBOL(mlx5_unregister_interface);
965290650Shselasky
966290650Shselaskyvoid *mlx5_get_protocol_dev(struct mlx5_core_dev *mdev, int protocol)
967290650Shselasky{
968290650Shselasky	struct mlx5_priv *priv = &mdev->priv;
969290650Shselasky	struct mlx5_device_context *dev_ctx;
970290650Shselasky	unsigned long flags;
971290650Shselasky	void *result = NULL;
972290650Shselasky
973290650Shselasky	spin_lock_irqsave(&priv->ctx_lock, flags);
974290650Shselasky
975290650Shselasky	list_for_each_entry(dev_ctx, &mdev->priv.ctx_list, list)
976290650Shselasky		if ((dev_ctx->intf->protocol == protocol) &&
977290650Shselasky		    dev_ctx->intf->get_dev) {
978290650Shselasky			result = dev_ctx->intf->get_dev(dev_ctx->context);
979290650Shselasky			break;
980290650Shselasky		}
981290650Shselasky
982290650Shselasky	spin_unlock_irqrestore(&priv->ctx_lock, flags);
983290650Shselasky
984290650Shselasky	return result;
985290650Shselasky}
986290650ShselaskyEXPORT_SYMBOL(mlx5_get_protocol_dev);
987290650Shselasky
988290650Shselaskystatic void mlx5_core_event(struct mlx5_core_dev *dev, enum mlx5_dev_event event,
989290650Shselasky			    unsigned long param)
990290650Shselasky{
991290650Shselasky	struct mlx5_priv *priv = &dev->priv;
992290650Shselasky	struct mlx5_device_context *dev_ctx;
993290650Shselasky	unsigned long flags;
994290650Shselasky
995290650Shselasky	spin_lock_irqsave(&priv->ctx_lock, flags);
996290650Shselasky
997290650Shselasky	list_for_each_entry(dev_ctx, &priv->ctx_list, list)
998290650Shselasky		if (dev_ctx->intf->event)
999290650Shselasky			dev_ctx->intf->event(dev, dev_ctx->context, event, param);
1000290650Shselasky
1001290650Shselasky	spin_unlock_irqrestore(&priv->ctx_lock, flags);
1002290650Shselasky}
1003290650Shselasky
1004290650Shselaskystruct mlx5_core_event_handler {
1005290650Shselasky	void (*event)(struct mlx5_core_dev *dev,
1006290650Shselasky		      enum mlx5_dev_event event,
1007290650Shselasky		      void *data);
1008290650Shselasky};
1009290650Shselasky
1010290650Shselasky
1011290650Shselaskystatic int init_one(struct pci_dev *pdev,
1012290650Shselasky		    const struct pci_device_id *id)
1013290650Shselasky{
1014290650Shselasky	struct mlx5_core_dev *dev;
1015290650Shselasky	struct mlx5_priv *priv;
1016290650Shselasky	int err;
1017290650Shselasky
1018290650Shselasky	dev = kzalloc(sizeof(*dev), GFP_KERNEL);
1019290650Shselasky	priv = &dev->priv;
1020306233Shselasky	if (id)
1021306233Shselasky		priv->pci_dev_data = id->driver_data;
1022290650Shselasky
1023290650Shselasky	if (prof_sel < 0 || prof_sel >= ARRAY_SIZE(profiles)) {
1024290650Shselasky		printf("mlx5_core: WARN: ""selected profile out of range, selecting default (%d)\n", MLX5_DEFAULT_PROF);
1025290650Shselasky		prof_sel = MLX5_DEFAULT_PROF;
1026290650Shselasky	}
1027290650Shselasky	dev->profile = &profiles[prof_sel];
1028290650Shselasky	dev->event = mlx5_core_event;
1029290650Shselasky
1030290650Shselasky	INIT_LIST_HEAD(&priv->ctx_list);
1031290650Shselasky	spin_lock_init(&priv->ctx_lock);
1032290650Shselasky	err = mlx5_dev_init(dev, pdev);
1033290650Shselasky	if (err) {
1034290650Shselasky		device_printf((&pdev->dev)->bsddev, "ERR: ""mlx5_dev_init failed %d\n", err);
1035290650Shselasky		goto out;
1036290650Shselasky	}
1037290650Shselasky
1038290650Shselasky	err = mlx5_register_device(dev);
1039290650Shselasky	if (err) {
1040290650Shselasky		device_printf((&pdev->dev)->bsddev, "ERR: ""mlx5_register_device failed %d\n", err);
1041290650Shselasky		goto out_init;
1042290650Shselasky	}
1043290650Shselasky
1044290650Shselasky
1045290650Shselasky	return 0;
1046290650Shselasky
1047290650Shselaskyout_init:
1048290650Shselasky	mlx5_dev_cleanup(dev);
1049290650Shselaskyout:
1050290650Shselasky	kfree(dev);
1051290650Shselasky	return err;
1052290650Shselasky}
1053290650Shselasky
1054290650Shselaskystatic void remove_one(struct pci_dev *pdev)
1055290650Shselasky{
1056290650Shselasky	struct mlx5_core_dev *dev  = pci_get_drvdata(pdev);
1057290650Shselasky
1058290650Shselasky	mlx5_unregister_device(dev);
1059290650Shselasky	mlx5_dev_cleanup(dev);
1060290650Shselasky	kfree(dev);
1061290650Shselasky}
1062290650Shselasky
1063290650Shselaskystatic const struct pci_device_id mlx5_core_pci_table[] = {
1064290650Shselasky	{ PCI_VDEVICE(MELLANOX, 4113) }, /* Connect-IB */
1065290650Shselasky	{ PCI_VDEVICE(MELLANOX, 4114) }, /* Connect-IB VF */
1066290650Shselasky	{ PCI_VDEVICE(MELLANOX, 4115) }, /* ConnectX-4 */
1067290650Shselasky	{ PCI_VDEVICE(MELLANOX, 4116) }, /* ConnectX-4 VF */
1068290650Shselasky	{ PCI_VDEVICE(MELLANOX, 4117) }, /* ConnectX-4LX */
1069290650Shselasky	{ PCI_VDEVICE(MELLANOX, 4118) }, /* ConnectX-4LX VF */
1070306233Shselasky	{ PCI_VDEVICE(MELLANOX, 4119) }, /* ConnectX-5 */
1071306233Shselasky	{ PCI_VDEVICE(MELLANOX, 4120) }, /* ConnectX-5 VF */
1072290650Shselasky	{ PCI_VDEVICE(MELLANOX, 4121) },
1073290650Shselasky	{ PCI_VDEVICE(MELLANOX, 4122) },
1074290650Shselasky	{ PCI_VDEVICE(MELLANOX, 4123) },
1075290650Shselasky	{ PCI_VDEVICE(MELLANOX, 4124) },
1076290650Shselasky	{ PCI_VDEVICE(MELLANOX, 4125) },
1077290650Shselasky	{ PCI_VDEVICE(MELLANOX, 4126) },
1078290650Shselasky	{ PCI_VDEVICE(MELLANOX, 4127) },
1079290650Shselasky	{ PCI_VDEVICE(MELLANOX, 4128) },
1080290650Shselasky	{ PCI_VDEVICE(MELLANOX, 4129) },
1081290650Shselasky	{ PCI_VDEVICE(MELLANOX, 4130) },
1082290650Shselasky	{ PCI_VDEVICE(MELLANOX, 4131) },
1083290650Shselasky	{ PCI_VDEVICE(MELLANOX, 4132) },
1084290650Shselasky	{ PCI_VDEVICE(MELLANOX, 4133) },
1085290650Shselasky	{ PCI_VDEVICE(MELLANOX, 4134) },
1086290650Shselasky	{ PCI_VDEVICE(MELLANOX, 4135) },
1087290650Shselasky	{ PCI_VDEVICE(MELLANOX, 4136) },
1088290650Shselasky	{ PCI_VDEVICE(MELLANOX, 4137) },
1089290650Shselasky	{ PCI_VDEVICE(MELLANOX, 4138) },
1090290650Shselasky	{ PCI_VDEVICE(MELLANOX, 4139) },
1091290650Shselasky	{ PCI_VDEVICE(MELLANOX, 4140) },
1092290650Shselasky	{ PCI_VDEVICE(MELLANOX, 4141) },
1093290650Shselasky	{ PCI_VDEVICE(MELLANOX, 4142) },
1094290650Shselasky	{ PCI_VDEVICE(MELLANOX, 4143) },
1095290650Shselasky	{ PCI_VDEVICE(MELLANOX, 4144) },
1096290650Shselasky	{ 0, }
1097290650Shselasky};
1098290650Shselasky
1099290650ShselaskyMODULE_DEVICE_TABLE(pci, mlx5_core_pci_table);
1100290650Shselasky
1101290650Shselaskystatic struct pci_driver mlx5_core_driver = {
1102290650Shselasky	.name           = DRIVER_NAME,
1103290650Shselasky	.id_table       = mlx5_core_pci_table,
1104290650Shselasky	.probe          = init_one,
1105290650Shselasky	.remove         = remove_one
1106290650Shselasky};
1107290650Shselasky
1108290650Shselaskystatic int __init init(void)
1109290650Shselasky{
1110290650Shselasky	int err;
1111290650Shselasky
1112290650Shselasky	mlx5_core_wq = create_singlethread_workqueue("mlx5_core_wq");
1113290650Shselasky	if (!mlx5_core_wq) {
1114290650Shselasky		err = -ENOMEM;
1115290650Shselasky		goto err_debug;
1116290650Shselasky	}
1117290650Shselasky	mlx5_health_init();
1118290650Shselasky
1119290650Shselasky	err = pci_register_driver(&mlx5_core_driver);
1120290650Shselasky	if (err)
1121290650Shselasky		goto err_health;
1122290650Shselasky
1123290650Shselasky
1124290650Shselasky	return 0;
1125290650Shselasky
1126290650Shselaskyerr_health:
1127290650Shselasky	mlx5_health_cleanup();
1128290650Shselasky	destroy_workqueue(mlx5_core_wq);
1129290650Shselaskyerr_debug:
1130290650Shselasky	return err;
1131290650Shselasky}
1132290650Shselasky
1133290650Shselaskystatic void __exit cleanup(void)
1134290650Shselasky{
1135290650Shselasky	pci_unregister_driver(&mlx5_core_driver);
1136290650Shselasky	mlx5_health_cleanup();
1137290650Shselasky	destroy_workqueue(mlx5_core_wq);
1138290650Shselasky}
1139290650Shselasky
1140290650Shselaskymodule_init(init);
1141290650Shselaskymodule_exit(cleanup);
1142