1 /*
2  * Copyright (c) 2013-2015, Mellanox Technologies. All rights reserved.
3  *
4  * This software is available to you under a choice of one of two
5  * licenses.  You may choose to be licensed under the terms of the GNU
6  * General Public License (GPL) Version 2, available from the file
7  * COPYING in the main directory of this source tree, or the
8  * OpenIB.org BSD license below:
9  *
10  *     Redistribution and use in source and binary forms, with or
11  *     without modification, are permitted provided that the following
12  *     conditions are met:
13  *
14  *      - Redistributions of source code must retain the above
15  *        copyright notice, this list of conditions and the following
16  *        disclaimer.
17  *
18  *      - Redistributions in binary form must reproduce the above
19  *        copyright notice, this list of conditions and the following
20  *        disclaimer in the documentation and/or other materials
21  *        provided with the distribution.
22  *
23  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
27  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
28  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
29  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
30  * SOFTWARE.
31  */
32 
33 #include <linux/highmem.h>
34 #include <linux/module.h>
35 #include <linux/init.h>
36 #include <linux/errno.h>
37 #include <linux/pci.h>
38 #include <linux/dma-mapping.h>
39 #include <linux/slab.h>
40 #include <linux/io-mapping.h>
41 #include <linux/interrupt.h>
42 #include <linux/delay.h>
43 #include <linux/mlx5/driver.h>
44 #include <linux/mlx5/cq.h>
45 #include <linux/mlx5/qp.h>
46 #include <linux/debugfs.h>
47 #include <linux/kmod.h>
48 #include <linux/mlx5/mlx5_ifc.h>
49 #include <linux/mlx5/vport.h>
50 #ifdef CONFIG_RFS_ACCEL
51 #include <linux/cpu_rmap.h>
52 #endif
53 #include <linux/version.h>
54 #include <net/devlink.h>
55 #include "mlx5_core.h"
56 #include "lib/eq.h"
57 #include "fs_core.h"
58 #include "lib/mpfs.h"
59 #include "eswitch.h"
60 #include "devlink.h"
61 #include "fw_reset.h"
62 #include "lib/mlx5.h"
63 #include "lib/tout.h"
64 #include "fpga/core.h"
65 #include "en_accel/ipsec.h"
66 #include "lib/clock.h"
67 #include "lib/vxlan.h"
68 #include "lib/geneve.h"
69 #include "lib/devcom.h"
70 #include "lib/pci_vsc.h"
71 #include "diag/fw_tracer.h"
72 #include "ecpf.h"
73 #include "lib/hv_vhca.h"
74 #include "diag/rsc_dump.h"
75 #include "sf/vhca_event.h"
76 #include "sf/dev/dev.h"
77 #include "sf/sf.h"
78 #include "mlx5_irq.h"
79 
80 MODULE_AUTHOR("Eli Cohen <eli@mellanox.com>");
81 MODULE_DESCRIPTION("Mellanox 5th generation network adapters (ConnectX series) core driver");
82 MODULE_LICENSE("Dual BSD/GPL");
83 
84 unsigned int mlx5_core_debug_mask;
85 module_param_named(debug_mask, mlx5_core_debug_mask, uint, 0644);
86 MODULE_PARM_DESC(debug_mask, "debug mask: 1 = dump cmd data, 2 = dump cmd exec time, 3 = both. Default=0");
87 
88 static unsigned int prof_sel = MLX5_DEFAULT_PROF;
89 module_param_named(prof_sel, prof_sel, uint, 0444);
90 MODULE_PARM_DESC(prof_sel, "profile selector. Valid range 0 - 2");
91 
92 static u32 sw_owner_id[4];
93 #define MAX_SW_VHCA_ID (BIT(__mlx5_bit_sz(cmd_hca_cap_2, sw_vhca_id)) - 1)
94 static DEFINE_IDA(sw_vhca_ida);
95 
96 enum {
97 	MLX5_ATOMIC_REQ_MODE_BE = 0x0,
98 	MLX5_ATOMIC_REQ_MODE_HOST_ENDIANNESS = 0x1,
99 };
100 
101 #define LOG_MAX_SUPPORTED_QPS 0xff
102 
103 static struct mlx5_profile profile[] = {
104 	[0] = {
105 		.mask           = 0,
106 	},
107 	[1] = {
108 		.mask		= MLX5_PROF_MASK_QP_SIZE,
109 		.log_max_qp	= 12,
110 	},
111 	[2] = {
112 		.mask		= MLX5_PROF_MASK_QP_SIZE |
113 				  MLX5_PROF_MASK_MR_CACHE,
114 		.log_max_qp	= LOG_MAX_SUPPORTED_QPS,
115 		.mr_cache[0]	= {
116 			.size	= 500,
117 			.limit	= 250
118 		},
119 		.mr_cache[1]	= {
120 			.size	= 500,
121 			.limit	= 250
122 		},
123 		.mr_cache[2]	= {
124 			.size	= 500,
125 			.limit	= 250
126 		},
127 		.mr_cache[3]	= {
128 			.size	= 500,
129 			.limit	= 250
130 		},
131 		.mr_cache[4]	= {
132 			.size	= 500,
133 			.limit	= 250
134 		},
135 		.mr_cache[5]	= {
136 			.size	= 500,
137 			.limit	= 250
138 		},
139 		.mr_cache[6]	= {
140 			.size	= 500,
141 			.limit	= 250
142 		},
143 		.mr_cache[7]	= {
144 			.size	= 500,
145 			.limit	= 250
146 		},
147 		.mr_cache[8]	= {
148 			.size	= 500,
149 			.limit	= 250
150 		},
151 		.mr_cache[9]	= {
152 			.size	= 500,
153 			.limit	= 250
154 		},
155 		.mr_cache[10]	= {
156 			.size	= 500,
157 			.limit	= 250
158 		},
159 		.mr_cache[11]	= {
160 			.size	= 500,
161 			.limit	= 250
162 		},
163 		.mr_cache[12]	= {
164 			.size	= 64,
165 			.limit	= 32
166 		},
167 		.mr_cache[13]	= {
168 			.size	= 32,
169 			.limit	= 16
170 		},
171 		.mr_cache[14]	= {
172 			.size	= 16,
173 			.limit	= 8
174 		},
175 		.mr_cache[15]	= {
176 			.size	= 8,
177 			.limit	= 4
178 		},
179 	},
180 };
181 
wait_fw_init(struct mlx5_core_dev * dev,u32 max_wait_mili,u32 warn_time_mili)182 static int wait_fw_init(struct mlx5_core_dev *dev, u32 max_wait_mili,
183 			u32 warn_time_mili)
184 {
185 	unsigned long warn = jiffies + msecs_to_jiffies(warn_time_mili);
186 	unsigned long end = jiffies + msecs_to_jiffies(max_wait_mili);
187 	u32 fw_initializing;
188 	int err = 0;
189 
190 	do {
191 		fw_initializing = ioread32be(&dev->iseg->initializing);
192 		if (!(fw_initializing >> 31))
193 			break;
194 		if (time_after(jiffies, end) ||
195 		    test_and_clear_bit(MLX5_BREAK_FW_WAIT, &dev->intf_state)) {
196 			err = -EBUSY;
197 			break;
198 		}
199 		if (warn_time_mili && time_after(jiffies, warn)) {
200 			mlx5_core_warn(dev, "Waiting for FW initialization, timeout abort in %ds (0x%x)\n",
201 				       jiffies_to_msecs(end - warn) / 1000, fw_initializing);
202 			warn = jiffies + msecs_to_jiffies(warn_time_mili);
203 		}
204 		msleep(mlx5_tout_ms(dev, FW_PRE_INIT_WAIT));
205 	} while (true);
206 
207 	return err;
208 }
209 
mlx5_set_driver_version(struct mlx5_core_dev * dev)210 static void mlx5_set_driver_version(struct mlx5_core_dev *dev)
211 {
212 	int driver_ver_sz = MLX5_FLD_SZ_BYTES(set_driver_version_in,
213 					      driver_version);
214 	u8 in[MLX5_ST_SZ_BYTES(set_driver_version_in)] = {};
215 	int remaining_size = driver_ver_sz;
216 	char *string;
217 
218 	if (!MLX5_CAP_GEN(dev, driver_version))
219 		return;
220 
221 	string = MLX5_ADDR_OF(set_driver_version_in, in, driver_version);
222 
223 	strncpy(string, "Linux", remaining_size);
224 
225 	remaining_size = max_t(int, 0, driver_ver_sz - strlen(string));
226 	strncat(string, ",", remaining_size);
227 
228 	remaining_size = max_t(int, 0, driver_ver_sz - strlen(string));
229 	strncat(string, KBUILD_MODNAME, remaining_size);
230 
231 	remaining_size = max_t(int, 0, driver_ver_sz - strlen(string));
232 	strncat(string, ",", remaining_size);
233 
234 	remaining_size = max_t(int, 0, driver_ver_sz - strlen(string));
235 
236 	snprintf(string + strlen(string), remaining_size, "%u.%u.%u",
237 		LINUX_VERSION_MAJOR, LINUX_VERSION_PATCHLEVEL,
238 		LINUX_VERSION_SUBLEVEL);
239 
240 	/*Send the command*/
241 	MLX5_SET(set_driver_version_in, in, opcode,
242 		 MLX5_CMD_OP_SET_DRIVER_VERSION);
243 
244 	mlx5_cmd_exec_in(dev, set_driver_version, in);
245 }
246 
set_dma_caps(struct pci_dev * pdev)247 static int set_dma_caps(struct pci_dev *pdev)
248 {
249 	int err;
250 
251 	err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
252 	if (err) {
253 		dev_warn(&pdev->dev, "Warning: couldn't set 64-bit PCI DMA mask\n");
254 		err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
255 		if (err) {
256 			dev_err(&pdev->dev, "Can't set PCI DMA mask, aborting\n");
257 			return err;
258 		}
259 	}
260 
261 	dma_set_max_seg_size(&pdev->dev, 2u * 1024 * 1024 * 1024);
262 	return err;
263 }
264 
mlx5_pci_enable_device(struct mlx5_core_dev * dev)265 static int mlx5_pci_enable_device(struct mlx5_core_dev *dev)
266 {
267 	struct pci_dev *pdev = dev->pdev;
268 	int err = 0;
269 
270 	mutex_lock(&dev->pci_status_mutex);
271 	if (dev->pci_status == MLX5_PCI_STATUS_DISABLED) {
272 		err = pci_enable_device(pdev);
273 		if (!err)
274 			dev->pci_status = MLX5_PCI_STATUS_ENABLED;
275 	}
276 	mutex_unlock(&dev->pci_status_mutex);
277 
278 	return err;
279 }
280 
mlx5_pci_disable_device(struct mlx5_core_dev * dev)281 static void mlx5_pci_disable_device(struct mlx5_core_dev *dev)
282 {
283 	struct pci_dev *pdev = dev->pdev;
284 
285 	mutex_lock(&dev->pci_status_mutex);
286 	if (dev->pci_status == MLX5_PCI_STATUS_ENABLED) {
287 		pci_disable_device(pdev);
288 		dev->pci_status = MLX5_PCI_STATUS_DISABLED;
289 	}
290 	mutex_unlock(&dev->pci_status_mutex);
291 }
292 
request_bar(struct pci_dev * pdev)293 static int request_bar(struct pci_dev *pdev)
294 {
295 	int err = 0;
296 
297 	if (!(pci_resource_flags(pdev, 0) & IORESOURCE_MEM)) {
298 		dev_err(&pdev->dev, "Missing registers BAR, aborting\n");
299 		return -ENODEV;
300 	}
301 
302 	err = pci_request_regions(pdev, KBUILD_MODNAME);
303 	if (err)
304 		dev_err(&pdev->dev, "Couldn't get PCI resources, aborting\n");
305 
306 	return err;
307 }
308 
release_bar(struct pci_dev * pdev)309 static void release_bar(struct pci_dev *pdev)
310 {
311 	pci_release_regions(pdev);
312 }
313 
314 struct mlx5_reg_host_endianness {
315 	u8	he;
316 	u8      rsvd[15];
317 };
318 
to_fw_pkey_sz(struct mlx5_core_dev * dev,u32 size)319 static u16 to_fw_pkey_sz(struct mlx5_core_dev *dev, u32 size)
320 {
321 	switch (size) {
322 	case 128:
323 		return 0;
324 	case 256:
325 		return 1;
326 	case 512:
327 		return 2;
328 	case 1024:
329 		return 3;
330 	case 2048:
331 		return 4;
332 	case 4096:
333 		return 5;
334 	default:
335 		mlx5_core_warn(dev, "invalid pkey table size %d\n", size);
336 		return 0;
337 	}
338 }
339 
mlx5_core_get_caps_mode(struct mlx5_core_dev * dev,enum mlx5_cap_type cap_type,enum mlx5_cap_mode cap_mode)340 static int mlx5_core_get_caps_mode(struct mlx5_core_dev *dev,
341 				   enum mlx5_cap_type cap_type,
342 				   enum mlx5_cap_mode cap_mode)
343 {
344 	u8 in[MLX5_ST_SZ_BYTES(query_hca_cap_in)];
345 	int out_sz = MLX5_ST_SZ_BYTES(query_hca_cap_out);
346 	void *out, *hca_caps;
347 	u16 opmod = (cap_type << 1) | (cap_mode & 0x01);
348 	int err;
349 
350 	memset(in, 0, sizeof(in));
351 	out = kzalloc(out_sz, GFP_KERNEL);
352 	if (!out)
353 		return -ENOMEM;
354 
355 	MLX5_SET(query_hca_cap_in, in, opcode, MLX5_CMD_OP_QUERY_HCA_CAP);
356 	MLX5_SET(query_hca_cap_in, in, op_mod, opmod);
357 	err = mlx5_cmd_exec_inout(dev, query_hca_cap, in, out);
358 	if (err) {
359 		mlx5_core_warn(dev,
360 			       "QUERY_HCA_CAP : type(%x) opmode(%x) Failed(%d)\n",
361 			       cap_type, cap_mode, err);
362 		goto query_ex;
363 	}
364 
365 	hca_caps =  MLX5_ADDR_OF(query_hca_cap_out, out, capability);
366 
367 	switch (cap_mode) {
368 	case HCA_CAP_OPMOD_GET_MAX:
369 		memcpy(dev->caps.hca[cap_type]->max, hca_caps,
370 		       MLX5_UN_SZ_BYTES(hca_cap_union));
371 		break;
372 	case HCA_CAP_OPMOD_GET_CUR:
373 		memcpy(dev->caps.hca[cap_type]->cur, hca_caps,
374 		       MLX5_UN_SZ_BYTES(hca_cap_union));
375 		break;
376 	default:
377 		mlx5_core_warn(dev,
378 			       "Tried to query dev cap type(%x) with wrong opmode(%x)\n",
379 			       cap_type, cap_mode);
380 		err = -EINVAL;
381 		break;
382 	}
383 query_ex:
384 	kfree(out);
385 	return err;
386 }
387 
mlx5_core_get_caps(struct mlx5_core_dev * dev,enum mlx5_cap_type cap_type)388 int mlx5_core_get_caps(struct mlx5_core_dev *dev, enum mlx5_cap_type cap_type)
389 {
390 	int ret;
391 
392 	ret = mlx5_core_get_caps_mode(dev, cap_type, HCA_CAP_OPMOD_GET_CUR);
393 	if (ret)
394 		return ret;
395 	return mlx5_core_get_caps_mode(dev, cap_type, HCA_CAP_OPMOD_GET_MAX);
396 }
397 
set_caps(struct mlx5_core_dev * dev,void * in,int opmod)398 static int set_caps(struct mlx5_core_dev *dev, void *in, int opmod)
399 {
400 	MLX5_SET(set_hca_cap_in, in, opcode, MLX5_CMD_OP_SET_HCA_CAP);
401 	MLX5_SET(set_hca_cap_in, in, op_mod, opmod << 1);
402 	return mlx5_cmd_exec_in(dev, set_hca_cap, in);
403 }
404 
handle_hca_cap_atomic(struct mlx5_core_dev * dev,void * set_ctx)405 static int handle_hca_cap_atomic(struct mlx5_core_dev *dev, void *set_ctx)
406 {
407 	void *set_hca_cap;
408 	int req_endianness;
409 	int err;
410 
411 	if (!MLX5_CAP_GEN(dev, atomic))
412 		return 0;
413 
414 	err = mlx5_core_get_caps(dev, MLX5_CAP_ATOMIC);
415 	if (err)
416 		return err;
417 
418 	req_endianness =
419 		MLX5_CAP_ATOMIC(dev,
420 				supported_atomic_req_8B_endianness_mode_1);
421 
422 	if (req_endianness != MLX5_ATOMIC_REQ_MODE_HOST_ENDIANNESS)
423 		return 0;
424 
425 	set_hca_cap = MLX5_ADDR_OF(set_hca_cap_in, set_ctx, capability);
426 
427 	/* Set requestor to host endianness */
428 	MLX5_SET(atomic_caps, set_hca_cap, atomic_req_8B_endianness_mode,
429 		 MLX5_ATOMIC_REQ_MODE_HOST_ENDIANNESS);
430 
431 	return set_caps(dev, set_ctx, MLX5_SET_HCA_CAP_OP_MOD_ATOMIC);
432 }
433 
handle_hca_cap_odp(struct mlx5_core_dev * dev,void * set_ctx)434 static int handle_hca_cap_odp(struct mlx5_core_dev *dev, void *set_ctx)
435 {
436 	void *set_hca_cap;
437 	bool do_set = false;
438 	int err;
439 
440 	if (!IS_ENABLED(CONFIG_INFINIBAND_ON_DEMAND_PAGING) ||
441 	    !MLX5_CAP_GEN(dev, pg))
442 		return 0;
443 
444 	err = mlx5_core_get_caps(dev, MLX5_CAP_ODP);
445 	if (err)
446 		return err;
447 
448 	set_hca_cap = MLX5_ADDR_OF(set_hca_cap_in, set_ctx, capability);
449 	memcpy(set_hca_cap, dev->caps.hca[MLX5_CAP_ODP]->cur,
450 	       MLX5_ST_SZ_BYTES(odp_cap));
451 
452 #define ODP_CAP_SET_MAX(dev, field)                                            \
453 	do {                                                                   \
454 		u32 _res = MLX5_CAP_ODP_MAX(dev, field);                       \
455 		if (_res) {                                                    \
456 			do_set = true;                                         \
457 			MLX5_SET(odp_cap, set_hca_cap, field, _res);           \
458 		}                                                              \
459 	} while (0)
460 
461 	ODP_CAP_SET_MAX(dev, ud_odp_caps.srq_receive);
462 	ODP_CAP_SET_MAX(dev, rc_odp_caps.srq_receive);
463 	ODP_CAP_SET_MAX(dev, xrc_odp_caps.srq_receive);
464 	ODP_CAP_SET_MAX(dev, xrc_odp_caps.send);
465 	ODP_CAP_SET_MAX(dev, xrc_odp_caps.receive);
466 	ODP_CAP_SET_MAX(dev, xrc_odp_caps.write);
467 	ODP_CAP_SET_MAX(dev, xrc_odp_caps.read);
468 	ODP_CAP_SET_MAX(dev, xrc_odp_caps.atomic);
469 	ODP_CAP_SET_MAX(dev, dc_odp_caps.srq_receive);
470 	ODP_CAP_SET_MAX(dev, dc_odp_caps.send);
471 	ODP_CAP_SET_MAX(dev, dc_odp_caps.receive);
472 	ODP_CAP_SET_MAX(dev, dc_odp_caps.write);
473 	ODP_CAP_SET_MAX(dev, dc_odp_caps.read);
474 	ODP_CAP_SET_MAX(dev, dc_odp_caps.atomic);
475 
476 	if (!do_set)
477 		return 0;
478 
479 	return set_caps(dev, set_ctx, MLX5_SET_HCA_CAP_OP_MOD_ODP);
480 }
481 
max_uc_list_get_devlink_param(struct mlx5_core_dev * dev)482 static int max_uc_list_get_devlink_param(struct mlx5_core_dev *dev)
483 {
484 	struct devlink *devlink = priv_to_devlink(dev);
485 	union devlink_param_value val;
486 	int err;
487 
488 	err = devlink_param_driverinit_value_get(devlink,
489 						 DEVLINK_PARAM_GENERIC_ID_MAX_MACS,
490 						 &val);
491 	if (!err)
492 		return val.vu32;
493 	mlx5_core_dbg(dev, "Failed to get param. err = %d\n", err);
494 	return err;
495 }
496 
mlx5_is_roce_on(struct mlx5_core_dev * dev)497 bool mlx5_is_roce_on(struct mlx5_core_dev *dev)
498 {
499 	struct devlink *devlink = priv_to_devlink(dev);
500 	union devlink_param_value val;
501 	int err;
502 
503 	err = devlink_param_driverinit_value_get(devlink,
504 						 DEVLINK_PARAM_GENERIC_ID_ENABLE_ROCE,
505 						 &val);
506 
507 	if (!err)
508 		return val.vbool;
509 
510 	mlx5_core_dbg(dev, "Failed to get param. err = %d\n", err);
511 	return MLX5_CAP_GEN(dev, roce);
512 }
513 EXPORT_SYMBOL(mlx5_is_roce_on);
514 
handle_hca_cap_2(struct mlx5_core_dev * dev,void * set_ctx)515 static int handle_hca_cap_2(struct mlx5_core_dev *dev, void *set_ctx)
516 {
517 	void *set_hca_cap;
518 	int err;
519 
520 	if (!MLX5_CAP_GEN_MAX(dev, hca_cap_2))
521 		return 0;
522 
523 	err = mlx5_core_get_caps(dev, MLX5_CAP_GENERAL_2);
524 	if (err)
525 		return err;
526 
527 	if (!MLX5_CAP_GEN_2_MAX(dev, sw_vhca_id_valid) ||
528 	    !(dev->priv.sw_vhca_id > 0))
529 		return 0;
530 
531 	set_hca_cap = MLX5_ADDR_OF(set_hca_cap_in, set_ctx,
532 				   capability);
533 	memcpy(set_hca_cap, dev->caps.hca[MLX5_CAP_GENERAL_2]->cur,
534 	       MLX5_ST_SZ_BYTES(cmd_hca_cap_2));
535 	MLX5_SET(cmd_hca_cap_2, set_hca_cap, sw_vhca_id_valid, 1);
536 
537 	return set_caps(dev, set_ctx, MLX5_CAP_GENERAL_2);
538 }
539 
handle_hca_cap(struct mlx5_core_dev * dev,void * set_ctx)540 static int handle_hca_cap(struct mlx5_core_dev *dev, void *set_ctx)
541 {
542 	struct mlx5_profile *prof = &dev->profile;
543 	void *set_hca_cap;
544 	int max_uc_list;
545 	int err;
546 
547 	err = mlx5_core_get_caps(dev, MLX5_CAP_GENERAL);
548 	if (err)
549 		return err;
550 
551 	set_hca_cap = MLX5_ADDR_OF(set_hca_cap_in, set_ctx,
552 				   capability);
553 	memcpy(set_hca_cap, dev->caps.hca[MLX5_CAP_GENERAL]->cur,
554 	       MLX5_ST_SZ_BYTES(cmd_hca_cap));
555 
556 	mlx5_core_dbg(dev, "Current Pkey table size %d Setting new size %d\n",
557 		      mlx5_to_sw_pkey_sz(MLX5_CAP_GEN(dev, pkey_table_size)),
558 		      128);
559 	/* we limit the size of the pkey table to 128 entries for now */
560 	MLX5_SET(cmd_hca_cap, set_hca_cap, pkey_table_size,
561 		 to_fw_pkey_sz(dev, 128));
562 
563 	/* Check log_max_qp from HCA caps to set in current profile */
564 	if (prof->log_max_qp == LOG_MAX_SUPPORTED_QPS) {
565 		prof->log_max_qp = min_t(u8, 18, MLX5_CAP_GEN_MAX(dev, log_max_qp));
566 	} else if (MLX5_CAP_GEN_MAX(dev, log_max_qp) < prof->log_max_qp) {
567 		mlx5_core_warn(dev, "log_max_qp value in current profile is %d, changing it to HCA capability limit (%d)\n",
568 			       prof->log_max_qp,
569 			       MLX5_CAP_GEN_MAX(dev, log_max_qp));
570 		prof->log_max_qp = MLX5_CAP_GEN_MAX(dev, log_max_qp);
571 	}
572 	if (prof->mask & MLX5_PROF_MASK_QP_SIZE)
573 		MLX5_SET(cmd_hca_cap, set_hca_cap, log_max_qp,
574 			 prof->log_max_qp);
575 
576 	/* disable cmdif checksum */
577 	MLX5_SET(cmd_hca_cap, set_hca_cap, cmdif_checksum, 0);
578 
579 	/* Enable 4K UAR only when HCA supports it and page size is bigger
580 	 * than 4K.
581 	 */
582 	if (MLX5_CAP_GEN_MAX(dev, uar_4k) && PAGE_SIZE > 4096)
583 		MLX5_SET(cmd_hca_cap, set_hca_cap, uar_4k, 1);
584 
585 	MLX5_SET(cmd_hca_cap, set_hca_cap, log_uar_page_sz, PAGE_SHIFT - 12);
586 
587 	if (MLX5_CAP_GEN_MAX(dev, cache_line_128byte))
588 		MLX5_SET(cmd_hca_cap,
589 			 set_hca_cap,
590 			 cache_line_128byte,
591 			 cache_line_size() >= 128 ? 1 : 0);
592 
593 	if (MLX5_CAP_GEN_MAX(dev, dct))
594 		MLX5_SET(cmd_hca_cap, set_hca_cap, dct, 1);
595 
596 	if (MLX5_CAP_GEN_MAX(dev, pci_sync_for_fw_update_event))
597 		MLX5_SET(cmd_hca_cap, set_hca_cap, pci_sync_for_fw_update_event, 1);
598 
599 	if (MLX5_CAP_GEN_MAX(dev, num_vhca_ports))
600 		MLX5_SET(cmd_hca_cap,
601 			 set_hca_cap,
602 			 num_vhca_ports,
603 			 MLX5_CAP_GEN_MAX(dev, num_vhca_ports));
604 
605 	if (MLX5_CAP_GEN_MAX(dev, release_all_pages))
606 		MLX5_SET(cmd_hca_cap, set_hca_cap, release_all_pages, 1);
607 
608 	if (MLX5_CAP_GEN_MAX(dev, mkey_by_name))
609 		MLX5_SET(cmd_hca_cap, set_hca_cap, mkey_by_name, 1);
610 
611 	mlx5_vhca_state_cap_handle(dev, set_hca_cap);
612 
613 	if (MLX5_CAP_GEN_MAX(dev, num_total_dynamic_vf_msix))
614 		MLX5_SET(cmd_hca_cap, set_hca_cap, num_total_dynamic_vf_msix,
615 			 MLX5_CAP_GEN_MAX(dev, num_total_dynamic_vf_msix));
616 
617 	if (MLX5_CAP_GEN(dev, roce_rw_supported) && MLX5_CAP_GEN_MAX(dev, roce))
618 		MLX5_SET(cmd_hca_cap, set_hca_cap, roce,
619 			 mlx5_is_roce_on(dev));
620 
621 	max_uc_list = max_uc_list_get_devlink_param(dev);
622 	if (max_uc_list > 0)
623 		MLX5_SET(cmd_hca_cap, set_hca_cap, log_max_current_uc_list,
624 			 ilog2(max_uc_list));
625 
626 	return set_caps(dev, set_ctx, MLX5_SET_HCA_CAP_OP_MOD_GENERAL_DEVICE);
627 }
628 
629 /* Cached MLX5_CAP_GEN(dev, roce) can be out of sync this early in the
630  * boot process.
631  * In case RoCE cap is writable in FW and user/devlink requested to change the
632  * cap, we are yet to query the final state of the above cap.
633  * Hence, the need for this function.
634  *
635  * Returns
636  * True:
637  * 1) RoCE cap is read only in FW and already disabled
638  * OR:
639  * 2) RoCE cap is writable in FW and user/devlink requested it off.
640  *
641  * In any other case, return False.
642  */
is_roce_fw_disabled(struct mlx5_core_dev * dev)643 static bool is_roce_fw_disabled(struct mlx5_core_dev *dev)
644 {
645 	return (MLX5_CAP_GEN(dev, roce_rw_supported) && !mlx5_is_roce_on(dev)) ||
646 		(!MLX5_CAP_GEN(dev, roce_rw_supported) && !MLX5_CAP_GEN(dev, roce));
647 }
648 
handle_hca_cap_roce(struct mlx5_core_dev * dev,void * set_ctx)649 static int handle_hca_cap_roce(struct mlx5_core_dev *dev, void *set_ctx)
650 {
651 	void *set_hca_cap;
652 	int err;
653 
654 	if (is_roce_fw_disabled(dev))
655 		return 0;
656 
657 	err = mlx5_core_get_caps(dev, MLX5_CAP_ROCE);
658 	if (err)
659 		return err;
660 
661 	if (MLX5_CAP_ROCE(dev, sw_r_roce_src_udp_port) ||
662 	    !MLX5_CAP_ROCE_MAX(dev, sw_r_roce_src_udp_port))
663 		return 0;
664 
665 	set_hca_cap = MLX5_ADDR_OF(set_hca_cap_in, set_ctx, capability);
666 	memcpy(set_hca_cap, dev->caps.hca[MLX5_CAP_ROCE]->cur,
667 	       MLX5_ST_SZ_BYTES(roce_cap));
668 	MLX5_SET(roce_cap, set_hca_cap, sw_r_roce_src_udp_port, 1);
669 
670 	err = set_caps(dev, set_ctx, MLX5_SET_HCA_CAP_OP_MOD_ROCE);
671 	return err;
672 }
673 
handle_hca_cap_port_selection(struct mlx5_core_dev * dev,void * set_ctx)674 static int handle_hca_cap_port_selection(struct mlx5_core_dev *dev,
675 					 void *set_ctx)
676 {
677 	void *set_hca_cap;
678 	int err;
679 
680 	if (!MLX5_CAP_GEN(dev, port_selection_cap))
681 		return 0;
682 
683 	err = mlx5_core_get_caps(dev, MLX5_CAP_PORT_SELECTION);
684 	if (err)
685 		return err;
686 
687 	if (MLX5_CAP_PORT_SELECTION(dev, port_select_flow_table_bypass) ||
688 	    !MLX5_CAP_PORT_SELECTION_MAX(dev, port_select_flow_table_bypass))
689 		return 0;
690 
691 	set_hca_cap = MLX5_ADDR_OF(set_hca_cap_in, set_ctx, capability);
692 	memcpy(set_hca_cap, dev->caps.hca[MLX5_CAP_PORT_SELECTION]->cur,
693 	       MLX5_ST_SZ_BYTES(port_selection_cap));
694 	MLX5_SET(port_selection_cap, set_hca_cap, port_select_flow_table_bypass, 1);
695 
696 	err = set_caps(dev, set_ctx, MLX5_SET_HCA_CAP_OP_MODE_PORT_SELECTION);
697 
698 	return err;
699 }
700 
set_hca_cap(struct mlx5_core_dev * dev)701 static int set_hca_cap(struct mlx5_core_dev *dev)
702 {
703 	int set_sz = MLX5_ST_SZ_BYTES(set_hca_cap_in);
704 	void *set_ctx;
705 	int err;
706 
707 	set_ctx = kzalloc(set_sz, GFP_KERNEL);
708 	if (!set_ctx)
709 		return -ENOMEM;
710 
711 	err = handle_hca_cap(dev, set_ctx);
712 	if (err) {
713 		mlx5_core_err(dev, "handle_hca_cap failed\n");
714 		goto out;
715 	}
716 
717 	memset(set_ctx, 0, set_sz);
718 	err = handle_hca_cap_atomic(dev, set_ctx);
719 	if (err) {
720 		mlx5_core_err(dev, "handle_hca_cap_atomic failed\n");
721 		goto out;
722 	}
723 
724 	memset(set_ctx, 0, set_sz);
725 	err = handle_hca_cap_odp(dev, set_ctx);
726 	if (err) {
727 		mlx5_core_err(dev, "handle_hca_cap_odp failed\n");
728 		goto out;
729 	}
730 
731 	memset(set_ctx, 0, set_sz);
732 	err = handle_hca_cap_roce(dev, set_ctx);
733 	if (err) {
734 		mlx5_core_err(dev, "handle_hca_cap_roce failed\n");
735 		goto out;
736 	}
737 
738 	memset(set_ctx, 0, set_sz);
739 	err = handle_hca_cap_2(dev, set_ctx);
740 	if (err) {
741 		mlx5_core_err(dev, "handle_hca_cap_2 failed\n");
742 		goto out;
743 	}
744 
745 	memset(set_ctx, 0, set_sz);
746 	err = handle_hca_cap_port_selection(dev, set_ctx);
747 	if (err) {
748 		mlx5_core_err(dev, "handle_hca_cap_port_selection failed\n");
749 		goto out;
750 	}
751 
752 out:
753 	kfree(set_ctx);
754 	return err;
755 }
756 
set_hca_ctrl(struct mlx5_core_dev * dev)757 static int set_hca_ctrl(struct mlx5_core_dev *dev)
758 {
759 	struct mlx5_reg_host_endianness he_in;
760 	struct mlx5_reg_host_endianness he_out;
761 	int err;
762 
763 	if (!mlx5_core_is_pf(dev))
764 		return 0;
765 
766 	memset(&he_in, 0, sizeof(he_in));
767 	he_in.he = MLX5_SET_HOST_ENDIANNESS;
768 	err = mlx5_core_access_reg(dev, &he_in,  sizeof(he_in),
769 					&he_out, sizeof(he_out),
770 					MLX5_REG_HOST_ENDIANNESS, 0, 1);
771 	return err;
772 }
773 
mlx5_core_set_hca_defaults(struct mlx5_core_dev * dev)774 static int mlx5_core_set_hca_defaults(struct mlx5_core_dev *dev)
775 {
776 	int ret = 0;
777 
778 	/* Disable local_lb by default */
779 	if (MLX5_CAP_GEN(dev, port_type) == MLX5_CAP_PORT_TYPE_ETH)
780 		ret = mlx5_nic_vport_update_local_lb(dev, false);
781 
782 	return ret;
783 }
784 
mlx5_core_enable_hca(struct mlx5_core_dev * dev,u16 func_id)785 int mlx5_core_enable_hca(struct mlx5_core_dev *dev, u16 func_id)
786 {
787 	u32 in[MLX5_ST_SZ_DW(enable_hca_in)] = {};
788 
789 	MLX5_SET(enable_hca_in, in, opcode, MLX5_CMD_OP_ENABLE_HCA);
790 	MLX5_SET(enable_hca_in, in, function_id, func_id);
791 	MLX5_SET(enable_hca_in, in, embedded_cpu_function,
792 		 dev->caps.embedded_cpu);
793 	return mlx5_cmd_exec_in(dev, enable_hca, in);
794 }
795 
mlx5_core_disable_hca(struct mlx5_core_dev * dev,u16 func_id)796 int mlx5_core_disable_hca(struct mlx5_core_dev *dev, u16 func_id)
797 {
798 	u32 in[MLX5_ST_SZ_DW(disable_hca_in)] = {};
799 
800 	MLX5_SET(disable_hca_in, in, opcode, MLX5_CMD_OP_DISABLE_HCA);
801 	MLX5_SET(disable_hca_in, in, function_id, func_id);
802 	MLX5_SET(enable_hca_in, in, embedded_cpu_function,
803 		 dev->caps.embedded_cpu);
804 	return mlx5_cmd_exec_in(dev, disable_hca, in);
805 }
806 
mlx5_core_set_issi(struct mlx5_core_dev * dev)807 static int mlx5_core_set_issi(struct mlx5_core_dev *dev)
808 {
809 	u32 query_out[MLX5_ST_SZ_DW(query_issi_out)] = {};
810 	u32 query_in[MLX5_ST_SZ_DW(query_issi_in)] = {};
811 	u32 sup_issi;
812 	int err;
813 
814 	MLX5_SET(query_issi_in, query_in, opcode, MLX5_CMD_OP_QUERY_ISSI);
815 	err = mlx5_cmd_exec_inout(dev, query_issi, query_in, query_out);
816 	if (err) {
817 		u32 syndrome = MLX5_GET(query_issi_out, query_out, syndrome);
818 		u8 status = MLX5_GET(query_issi_out, query_out, status);
819 
820 		if (!status || syndrome == MLX5_DRIVER_SYND) {
821 			mlx5_core_err(dev, "Failed to query ISSI err(%d) status(%d) synd(%d)\n",
822 				      err, status, syndrome);
823 			return err;
824 		}
825 
826 		mlx5_core_warn(dev, "Query ISSI is not supported by FW, ISSI is 0\n");
827 		dev->issi = 0;
828 		return 0;
829 	}
830 
831 	sup_issi = MLX5_GET(query_issi_out, query_out, supported_issi_dw0);
832 
833 	if (sup_issi & (1 << 1)) {
834 		u32 set_in[MLX5_ST_SZ_DW(set_issi_in)] = {};
835 
836 		MLX5_SET(set_issi_in, set_in, opcode, MLX5_CMD_OP_SET_ISSI);
837 		MLX5_SET(set_issi_in, set_in, current_issi, 1);
838 		err = mlx5_cmd_exec_in(dev, set_issi, set_in);
839 		if (err) {
840 			mlx5_core_err(dev, "Failed to set ISSI to 1 err(%d)\n",
841 				      err);
842 			return err;
843 		}
844 
845 		dev->issi = 1;
846 
847 		return 0;
848 	} else if (sup_issi & (1 << 0) || !sup_issi) {
849 		return 0;
850 	}
851 
852 	return -EOPNOTSUPP;
853 }
854 
mlx5_pci_init(struct mlx5_core_dev * dev,struct pci_dev * pdev,const struct pci_device_id * id)855 static int mlx5_pci_init(struct mlx5_core_dev *dev, struct pci_dev *pdev,
856 			 const struct pci_device_id *id)
857 {
858 	int err = 0;
859 
860 	mutex_init(&dev->pci_status_mutex);
861 	pci_set_drvdata(dev->pdev, dev);
862 
863 	dev->bar_addr = pci_resource_start(pdev, 0);
864 
865 	err = mlx5_pci_enable_device(dev);
866 	if (err) {
867 		mlx5_core_err(dev, "Cannot enable PCI device, aborting\n");
868 		return err;
869 	}
870 
871 	err = request_bar(pdev);
872 	if (err) {
873 		mlx5_core_err(dev, "error requesting BARs, aborting\n");
874 		goto err_disable;
875 	}
876 
877 	pci_set_master(pdev);
878 
879 	err = set_dma_caps(pdev);
880 	if (err) {
881 		mlx5_core_err(dev, "Failed setting DMA capabilities mask, aborting\n");
882 		goto err_clr_master;
883 	}
884 
885 	if (pci_enable_atomic_ops_to_root(pdev, PCI_EXP_DEVCAP2_ATOMIC_COMP32) &&
886 	    pci_enable_atomic_ops_to_root(pdev, PCI_EXP_DEVCAP2_ATOMIC_COMP64) &&
887 	    pci_enable_atomic_ops_to_root(pdev, PCI_EXP_DEVCAP2_ATOMIC_COMP128))
888 		mlx5_core_dbg(dev, "Enabling pci atomics failed\n");
889 
890 	dev->iseg_base = dev->bar_addr;
891 	dev->iseg = ioremap(dev->iseg_base, sizeof(*dev->iseg));
892 	if (!dev->iseg) {
893 		err = -ENOMEM;
894 		mlx5_core_err(dev, "Failed mapping initialization segment, aborting\n");
895 		goto err_clr_master;
896 	}
897 
898 	mlx5_pci_vsc_init(dev);
899 	dev->caps.embedded_cpu = mlx5_read_embedded_cpu(dev);
900 	return 0;
901 
902 err_clr_master:
903 	pci_clear_master(dev->pdev);
904 	release_bar(dev->pdev);
905 err_disable:
906 	mlx5_pci_disable_device(dev);
907 	return err;
908 }
909 
mlx5_pci_close(struct mlx5_core_dev * dev)910 static void mlx5_pci_close(struct mlx5_core_dev *dev)
911 {
912 	/* health work might still be active, and it needs pci bar in
913 	 * order to know the NIC state. Therefore, drain the health WQ
914 	 * before removing the pci bars
915 	 */
916 	mlx5_drain_health_wq(dev);
917 	iounmap(dev->iseg);
918 	pci_clear_master(dev->pdev);
919 	release_bar(dev->pdev);
920 	mlx5_pci_disable_device(dev);
921 }
922 
mlx5_init_once(struct mlx5_core_dev * dev)923 static int mlx5_init_once(struct mlx5_core_dev *dev)
924 {
925 	int err;
926 
927 	dev->priv.devcom = mlx5_devcom_register_device(dev);
928 	if (IS_ERR(dev->priv.devcom))
929 		mlx5_core_err(dev, "failed to register with devcom (0x%p)\n",
930 			      dev->priv.devcom);
931 
932 	err = mlx5_query_board_id(dev);
933 	if (err) {
934 		mlx5_core_err(dev, "query board id failed\n");
935 		goto err_devcom;
936 	}
937 
938 	err = mlx5_irq_table_init(dev);
939 	if (err) {
940 		mlx5_core_err(dev, "failed to initialize irq table\n");
941 		goto err_devcom;
942 	}
943 
944 	err = mlx5_eq_table_init(dev);
945 	if (err) {
946 		mlx5_core_err(dev, "failed to initialize eq\n");
947 		goto err_irq_cleanup;
948 	}
949 
950 	err = mlx5_events_init(dev);
951 	if (err) {
952 		mlx5_core_err(dev, "failed to initialize events\n");
953 		goto err_eq_cleanup;
954 	}
955 
956 	err = mlx5_fw_reset_init(dev);
957 	if (err) {
958 		mlx5_core_err(dev, "failed to initialize fw reset events\n");
959 		goto err_events_cleanup;
960 	}
961 
962 	mlx5_cq_debugfs_init(dev);
963 
964 	mlx5_init_reserved_gids(dev);
965 
966 	mlx5_init_clock(dev);
967 
968 	dev->vxlan = mlx5_vxlan_create(dev);
969 	dev->geneve = mlx5_geneve_create(dev);
970 
971 	err = mlx5_init_rl_table(dev);
972 	if (err) {
973 		mlx5_core_err(dev, "Failed to init rate limiting\n");
974 		goto err_tables_cleanup;
975 	}
976 
977 	err = mlx5_mpfs_init(dev);
978 	if (err) {
979 		mlx5_core_err(dev, "Failed to init l2 table %d\n", err);
980 		goto err_rl_cleanup;
981 	}
982 
983 	err = mlx5_sriov_init(dev);
984 	if (err) {
985 		mlx5_core_err(dev, "Failed to init sriov %d\n", err);
986 		goto err_mpfs_cleanup;
987 	}
988 
989 	err = mlx5_eswitch_init(dev);
990 	if (err) {
991 		mlx5_core_err(dev, "Failed to init eswitch %d\n", err);
992 		goto err_sriov_cleanup;
993 	}
994 
995 	err = mlx5_fpga_init(dev);
996 	if (err) {
997 		mlx5_core_err(dev, "Failed to init fpga device %d\n", err);
998 		goto err_eswitch_cleanup;
999 	}
1000 
1001 	err = mlx5_vhca_event_init(dev);
1002 	if (err) {
1003 		mlx5_core_err(dev, "Failed to init vhca event notifier %d\n", err);
1004 		goto err_fpga_cleanup;
1005 	}
1006 
1007 	err = mlx5_sf_hw_table_init(dev);
1008 	if (err) {
1009 		mlx5_core_err(dev, "Failed to init SF HW table %d\n", err);
1010 		goto err_sf_hw_table_cleanup;
1011 	}
1012 
1013 	err = mlx5_sf_table_init(dev);
1014 	if (err) {
1015 		mlx5_core_err(dev, "Failed to init SF table %d\n", err);
1016 		goto err_sf_table_cleanup;
1017 	}
1018 
1019 	err = mlx5_fs_core_alloc(dev);
1020 	if (err) {
1021 		mlx5_core_err(dev, "Failed to alloc flow steering\n");
1022 		goto err_fs;
1023 	}
1024 
1025 	dev->dm = mlx5_dm_create(dev);
1026 	if (IS_ERR(dev->dm))
1027 		mlx5_core_warn(dev, "Failed to init device memory%d\n", err);
1028 
1029 	dev->tracer = mlx5_fw_tracer_create(dev);
1030 	dev->hv_vhca = mlx5_hv_vhca_create(dev);
1031 	dev->rsc_dump = mlx5_rsc_dump_create(dev);
1032 
1033 	return 0;
1034 
1035 err_fs:
1036 	mlx5_sf_table_cleanup(dev);
1037 err_sf_table_cleanup:
1038 	mlx5_sf_hw_table_cleanup(dev);
1039 err_sf_hw_table_cleanup:
1040 	mlx5_vhca_event_cleanup(dev);
1041 err_fpga_cleanup:
1042 	mlx5_fpga_cleanup(dev);
1043 err_eswitch_cleanup:
1044 	mlx5_eswitch_cleanup(dev->priv.eswitch);
1045 err_sriov_cleanup:
1046 	mlx5_sriov_cleanup(dev);
1047 err_mpfs_cleanup:
1048 	mlx5_mpfs_cleanup(dev);
1049 err_rl_cleanup:
1050 	mlx5_cleanup_rl_table(dev);
1051 err_tables_cleanup:
1052 	mlx5_geneve_destroy(dev->geneve);
1053 	mlx5_vxlan_destroy(dev->vxlan);
1054 	mlx5_cleanup_clock(dev);
1055 	mlx5_cleanup_reserved_gids(dev);
1056 	mlx5_cq_debugfs_cleanup(dev);
1057 	mlx5_fw_reset_cleanup(dev);
1058 err_events_cleanup:
1059 	mlx5_events_cleanup(dev);
1060 err_eq_cleanup:
1061 	mlx5_eq_table_cleanup(dev);
1062 err_irq_cleanup:
1063 	mlx5_irq_table_cleanup(dev);
1064 err_devcom:
1065 	mlx5_devcom_unregister_device(dev->priv.devcom);
1066 
1067 	return err;
1068 }
1069 
mlx5_cleanup_once(struct mlx5_core_dev * dev)1070 static void mlx5_cleanup_once(struct mlx5_core_dev *dev)
1071 {
1072 	mlx5_rsc_dump_destroy(dev);
1073 	mlx5_hv_vhca_destroy(dev->hv_vhca);
1074 	mlx5_fw_tracer_destroy(dev->tracer);
1075 	mlx5_dm_cleanup(dev);
1076 	mlx5_fs_core_free(dev);
1077 	mlx5_sf_table_cleanup(dev);
1078 	mlx5_sf_hw_table_cleanup(dev);
1079 	mlx5_vhca_event_cleanup(dev);
1080 	mlx5_fpga_cleanup(dev);
1081 	mlx5_eswitch_cleanup(dev->priv.eswitch);
1082 	mlx5_sriov_cleanup(dev);
1083 	mlx5_mpfs_cleanup(dev);
1084 	mlx5_cleanup_rl_table(dev);
1085 	mlx5_geneve_destroy(dev->geneve);
1086 	mlx5_vxlan_destroy(dev->vxlan);
1087 	mlx5_cleanup_clock(dev);
1088 	mlx5_cleanup_reserved_gids(dev);
1089 	mlx5_cq_debugfs_cleanup(dev);
1090 	mlx5_fw_reset_cleanup(dev);
1091 	mlx5_events_cleanup(dev);
1092 	mlx5_eq_table_cleanup(dev);
1093 	mlx5_irq_table_cleanup(dev);
1094 	mlx5_devcom_unregister_device(dev->priv.devcom);
1095 }
1096 
mlx5_function_setup(struct mlx5_core_dev * dev,bool boot,u64 timeout)1097 static int mlx5_function_setup(struct mlx5_core_dev *dev, bool boot, u64 timeout)
1098 {
1099 	int err;
1100 
1101 	mlx5_core_info(dev, "firmware version: %d.%d.%d\n", fw_rev_maj(dev),
1102 		       fw_rev_min(dev), fw_rev_sub(dev));
1103 
1104 	/* Only PFs hold the relevant PCIe information for this query */
1105 	if (mlx5_core_is_pf(dev))
1106 		pcie_print_link_status(dev->pdev);
1107 
1108 	/* wait for firmware to accept initialization segments configurations
1109 	 */
1110 	err = wait_fw_init(dev, timeout,
1111 			   mlx5_tout_ms(dev, FW_PRE_INIT_WARN_MESSAGE_INTERVAL));
1112 	if (err) {
1113 		mlx5_core_err(dev, "Firmware over %llu MS in pre-initializing state, aborting\n",
1114 			      timeout);
1115 		return err;
1116 	}
1117 
1118 	err = mlx5_cmd_init(dev);
1119 	if (err) {
1120 		mlx5_core_err(dev, "Failed initializing command interface, aborting\n");
1121 		return err;
1122 	}
1123 
1124 	mlx5_tout_query_iseg(dev);
1125 
1126 	err = wait_fw_init(dev, mlx5_tout_ms(dev, FW_INIT), 0);
1127 	if (err) {
1128 		mlx5_core_err(dev, "Firmware over %llu MS in initializing state, aborting\n",
1129 			      mlx5_tout_ms(dev, FW_INIT));
1130 		goto err_cmd_cleanup;
1131 	}
1132 
1133 	mlx5_cmd_set_state(dev, MLX5_CMDIF_STATE_UP);
1134 
1135 	mlx5_start_health_poll(dev);
1136 
1137 	err = mlx5_core_enable_hca(dev, 0);
1138 	if (err) {
1139 		mlx5_core_err(dev, "enable hca failed\n");
1140 		goto stop_health_poll;
1141 	}
1142 
1143 	err = mlx5_core_set_issi(dev);
1144 	if (err) {
1145 		mlx5_core_err(dev, "failed to set issi\n");
1146 		goto err_disable_hca;
1147 	}
1148 
1149 	err = mlx5_satisfy_startup_pages(dev, 1);
1150 	if (err) {
1151 		mlx5_core_err(dev, "failed to allocate boot pages\n");
1152 		goto err_disable_hca;
1153 	}
1154 
1155 	err = mlx5_tout_query_dtor(dev);
1156 	if (err) {
1157 		mlx5_core_err(dev, "failed to read dtor\n");
1158 		goto reclaim_boot_pages;
1159 	}
1160 
1161 	err = set_hca_ctrl(dev);
1162 	if (err) {
1163 		mlx5_core_err(dev, "set_hca_ctrl failed\n");
1164 		goto reclaim_boot_pages;
1165 	}
1166 
1167 	err = set_hca_cap(dev);
1168 	if (err) {
1169 		mlx5_core_err(dev, "set_hca_cap failed\n");
1170 		goto reclaim_boot_pages;
1171 	}
1172 
1173 	err = mlx5_satisfy_startup_pages(dev, 0);
1174 	if (err) {
1175 		mlx5_core_err(dev, "failed to allocate init pages\n");
1176 		goto reclaim_boot_pages;
1177 	}
1178 
1179 	err = mlx5_cmd_init_hca(dev, sw_owner_id);
1180 	if (err) {
1181 		mlx5_core_err(dev, "init hca failed\n");
1182 		goto reclaim_boot_pages;
1183 	}
1184 
1185 	mlx5_set_driver_version(dev);
1186 
1187 	err = mlx5_query_hca_caps(dev);
1188 	if (err) {
1189 		mlx5_core_err(dev, "query hca failed\n");
1190 		goto reclaim_boot_pages;
1191 	}
1192 	mlx5_start_health_fw_log_up(dev);
1193 
1194 	return 0;
1195 
1196 reclaim_boot_pages:
1197 	mlx5_reclaim_startup_pages(dev);
1198 err_disable_hca:
1199 	mlx5_core_disable_hca(dev, 0);
1200 stop_health_poll:
1201 	mlx5_stop_health_poll(dev, boot);
1202 err_cmd_cleanup:
1203 	mlx5_cmd_set_state(dev, MLX5_CMDIF_STATE_DOWN);
1204 	mlx5_cmd_cleanup(dev);
1205 
1206 	return err;
1207 }
1208 
mlx5_function_teardown(struct mlx5_core_dev * dev,bool boot)1209 static int mlx5_function_teardown(struct mlx5_core_dev *dev, bool boot)
1210 {
1211 	int err;
1212 
1213 	err = mlx5_cmd_teardown_hca(dev);
1214 	if (err) {
1215 		mlx5_core_err(dev, "tear_down_hca failed, skip cleanup\n");
1216 		return err;
1217 	}
1218 	mlx5_reclaim_startup_pages(dev);
1219 	mlx5_core_disable_hca(dev, 0);
1220 	mlx5_stop_health_poll(dev, boot);
1221 	mlx5_cmd_set_state(dev, MLX5_CMDIF_STATE_DOWN);
1222 	mlx5_cmd_cleanup(dev);
1223 
1224 	return 0;
1225 }
1226 
mlx5_load(struct mlx5_core_dev * dev)1227 static int mlx5_load(struct mlx5_core_dev *dev)
1228 {
1229 	int err;
1230 
1231 	dev->priv.uar = mlx5_get_uars_page(dev);
1232 	if (IS_ERR(dev->priv.uar)) {
1233 		mlx5_core_err(dev, "Failed allocating uar, aborting\n");
1234 		err = PTR_ERR(dev->priv.uar);
1235 		return err;
1236 	}
1237 
1238 	mlx5_events_start(dev);
1239 	mlx5_pagealloc_start(dev);
1240 
1241 	err = mlx5_irq_table_create(dev);
1242 	if (err) {
1243 		mlx5_core_err(dev, "Failed to alloc IRQs\n");
1244 		goto err_irq_table;
1245 	}
1246 
1247 	err = mlx5_eq_table_create(dev);
1248 	if (err) {
1249 		mlx5_core_err(dev, "Failed to create EQs\n");
1250 		goto err_eq_table;
1251 	}
1252 
1253 	err = mlx5_fw_tracer_init(dev->tracer);
1254 	if (err) {
1255 		mlx5_core_err(dev, "Failed to init FW tracer %d\n", err);
1256 		mlx5_fw_tracer_destroy(dev->tracer);
1257 		dev->tracer = NULL;
1258 	}
1259 
1260 	mlx5_fw_reset_events_start(dev);
1261 	mlx5_hv_vhca_init(dev->hv_vhca);
1262 
1263 	err = mlx5_rsc_dump_init(dev);
1264 	if (err) {
1265 		mlx5_core_err(dev, "Failed to init Resource dump %d\n", err);
1266 		mlx5_rsc_dump_destroy(dev);
1267 		dev->rsc_dump = NULL;
1268 	}
1269 
1270 	err = mlx5_fpga_device_start(dev);
1271 	if (err) {
1272 		mlx5_core_err(dev, "fpga device start failed %d\n", err);
1273 		goto err_fpga_start;
1274 	}
1275 
1276 	err = mlx5_fs_core_init(dev);
1277 	if (err) {
1278 		mlx5_core_err(dev, "Failed to init flow steering\n");
1279 		goto err_fs;
1280 	}
1281 
1282 	err = mlx5_core_set_hca_defaults(dev);
1283 	if (err) {
1284 		mlx5_core_err(dev, "Failed to set hca defaults\n");
1285 		goto err_set_hca;
1286 	}
1287 
1288 	mlx5_vhca_event_start(dev);
1289 
1290 	err = mlx5_sf_hw_table_create(dev);
1291 	if (err) {
1292 		mlx5_core_err(dev, "sf table create failed %d\n", err);
1293 		goto err_vhca;
1294 	}
1295 
1296 	err = mlx5_ec_init(dev);
1297 	if (err) {
1298 		mlx5_core_err(dev, "Failed to init embedded CPU\n");
1299 		goto err_ec;
1300 	}
1301 
1302 	mlx5_lag_add_mdev(dev);
1303 	err = mlx5_sriov_attach(dev);
1304 	if (err) {
1305 		mlx5_core_err(dev, "sriov init failed %d\n", err);
1306 		goto err_sriov;
1307 	}
1308 
1309 	mlx5_sf_dev_table_create(dev);
1310 
1311 	return 0;
1312 
1313 err_sriov:
1314 	mlx5_lag_remove_mdev(dev);
1315 	mlx5_ec_cleanup(dev);
1316 err_ec:
1317 	mlx5_sf_hw_table_destroy(dev);
1318 err_vhca:
1319 	mlx5_vhca_event_stop(dev);
1320 err_set_hca:
1321 	mlx5_fs_core_cleanup(dev);
1322 err_fs:
1323 	mlx5_fpga_device_stop(dev);
1324 err_fpga_start:
1325 	mlx5_rsc_dump_cleanup(dev);
1326 	mlx5_hv_vhca_cleanup(dev->hv_vhca);
1327 	mlx5_fw_reset_events_stop(dev);
1328 	mlx5_fw_tracer_cleanup(dev->tracer);
1329 	mlx5_eq_table_destroy(dev);
1330 err_eq_table:
1331 	mlx5_irq_table_destroy(dev);
1332 err_irq_table:
1333 	mlx5_pagealloc_stop(dev);
1334 	mlx5_events_stop(dev);
1335 	mlx5_put_uars_page(dev, dev->priv.uar);
1336 	return err;
1337 }
1338 
mlx5_unload(struct mlx5_core_dev * dev)1339 static void mlx5_unload(struct mlx5_core_dev *dev)
1340 {
1341 	mlx5_sf_dev_table_destroy(dev);
1342 	mlx5_sriov_detach(dev);
1343 	mlx5_eswitch_disable(dev->priv.eswitch);
1344 	mlx5_lag_remove_mdev(dev);
1345 	mlx5_ec_cleanup(dev);
1346 	mlx5_sf_hw_table_destroy(dev);
1347 	mlx5_vhca_event_stop(dev);
1348 	mlx5_fs_core_cleanup(dev);
1349 	mlx5_fpga_device_stop(dev);
1350 	mlx5_rsc_dump_cleanup(dev);
1351 	mlx5_hv_vhca_cleanup(dev->hv_vhca);
1352 	mlx5_fw_reset_events_stop(dev);
1353 	mlx5_fw_tracer_cleanup(dev->tracer);
1354 	mlx5_eq_table_destroy(dev);
1355 	mlx5_irq_table_destroy(dev);
1356 	mlx5_pagealloc_stop(dev);
1357 	mlx5_events_stop(dev);
1358 	mlx5_put_uars_page(dev, dev->priv.uar);
1359 }
1360 
mlx5_init_one(struct mlx5_core_dev * dev)1361 int mlx5_init_one(struct mlx5_core_dev *dev)
1362 {
1363 	struct devlink *devlink = priv_to_devlink(dev);
1364 	int err = 0;
1365 
1366 	devl_lock(devlink);
1367 	mutex_lock(&dev->intf_state_mutex);
1368 	dev->state = MLX5_DEVICE_STATE_UP;
1369 
1370 	err = mlx5_function_setup(dev, true, mlx5_tout_ms(dev, FW_PRE_INIT_TIMEOUT));
1371 	if (err)
1372 		goto err_function;
1373 
1374 	err = mlx5_init_once(dev);
1375 	if (err) {
1376 		mlx5_core_err(dev, "sw objs init failed\n");
1377 		goto function_teardown;
1378 	}
1379 
1380 	err = mlx5_load(dev);
1381 	if (err)
1382 		goto err_load;
1383 
1384 	set_bit(MLX5_INTERFACE_STATE_UP, &dev->intf_state);
1385 
1386 	err = mlx5_devlink_register(priv_to_devlink(dev));
1387 	if (err)
1388 		goto err_devlink_reg;
1389 
1390 	err = mlx5_register_device(dev);
1391 	if (err)
1392 		goto err_register;
1393 
1394 	mutex_unlock(&dev->intf_state_mutex);
1395 	devl_unlock(devlink);
1396 	return 0;
1397 
1398 err_register:
1399 	mlx5_devlink_unregister(priv_to_devlink(dev));
1400 err_devlink_reg:
1401 	clear_bit(MLX5_INTERFACE_STATE_UP, &dev->intf_state);
1402 	mlx5_unload(dev);
1403 err_load:
1404 	mlx5_cleanup_once(dev);
1405 function_teardown:
1406 	mlx5_function_teardown(dev, true);
1407 err_function:
1408 	dev->state = MLX5_DEVICE_STATE_INTERNAL_ERROR;
1409 	mutex_unlock(&dev->intf_state_mutex);
1410 	devl_unlock(devlink);
1411 	return err;
1412 }
1413 
mlx5_uninit_one(struct mlx5_core_dev * dev)1414 void mlx5_uninit_one(struct mlx5_core_dev *dev)
1415 {
1416 	struct devlink *devlink = priv_to_devlink(dev);
1417 
1418 	devl_lock(devlink);
1419 	mutex_lock(&dev->intf_state_mutex);
1420 
1421 	mlx5_unregister_device(dev);
1422 	mlx5_devlink_unregister(priv_to_devlink(dev));
1423 
1424 	if (!test_bit(MLX5_INTERFACE_STATE_UP, &dev->intf_state)) {
1425 		mlx5_core_warn(dev, "%s: interface is down, NOP\n",
1426 			       __func__);
1427 		mlx5_cleanup_once(dev);
1428 		goto out;
1429 	}
1430 
1431 	clear_bit(MLX5_INTERFACE_STATE_UP, &dev->intf_state);
1432 	mlx5_unload(dev);
1433 	mlx5_cleanup_once(dev);
1434 	mlx5_function_teardown(dev, true);
1435 out:
1436 	mutex_unlock(&dev->intf_state_mutex);
1437 	devl_unlock(devlink);
1438 }
1439 
mlx5_load_one_devl_locked(struct mlx5_core_dev * dev,bool recovery)1440 int mlx5_load_one_devl_locked(struct mlx5_core_dev *dev, bool recovery)
1441 {
1442 	int err = 0;
1443 	u64 timeout;
1444 
1445 	devl_assert_locked(priv_to_devlink(dev));
1446 	mutex_lock(&dev->intf_state_mutex);
1447 	if (test_bit(MLX5_INTERFACE_STATE_UP, &dev->intf_state)) {
1448 		mlx5_core_warn(dev, "interface is up, NOP\n");
1449 		goto out;
1450 	}
1451 	/* remove any previous indication of internal error */
1452 	dev->state = MLX5_DEVICE_STATE_UP;
1453 
1454 	if (recovery)
1455 		timeout = mlx5_tout_ms(dev, FW_PRE_INIT_ON_RECOVERY_TIMEOUT);
1456 	else
1457 		timeout = mlx5_tout_ms(dev, FW_PRE_INIT_TIMEOUT);
1458 	err = mlx5_function_setup(dev, false, timeout);
1459 	if (err)
1460 		goto err_function;
1461 
1462 	err = mlx5_load(dev);
1463 	if (err)
1464 		goto err_load;
1465 
1466 	set_bit(MLX5_INTERFACE_STATE_UP, &dev->intf_state);
1467 
1468 	err = mlx5_attach_device(dev);
1469 	if (err)
1470 		goto err_attach;
1471 
1472 	mutex_unlock(&dev->intf_state_mutex);
1473 	return 0;
1474 
1475 err_attach:
1476 	clear_bit(MLX5_INTERFACE_STATE_UP, &dev->intf_state);
1477 	mlx5_unload(dev);
1478 err_load:
1479 	mlx5_function_teardown(dev, false);
1480 err_function:
1481 	dev->state = MLX5_DEVICE_STATE_INTERNAL_ERROR;
1482 out:
1483 	mutex_unlock(&dev->intf_state_mutex);
1484 	return err;
1485 }
1486 
mlx5_load_one(struct mlx5_core_dev * dev,bool recovery)1487 int mlx5_load_one(struct mlx5_core_dev *dev, bool recovery)
1488 {
1489 	struct devlink *devlink = priv_to_devlink(dev);
1490 	int ret;
1491 
1492 	devl_lock(devlink);
1493 	ret = mlx5_load_one_devl_locked(dev, recovery);
1494 	devl_unlock(devlink);
1495 	return ret;
1496 }
1497 
mlx5_unload_one_devl_locked(struct mlx5_core_dev * dev)1498 void mlx5_unload_one_devl_locked(struct mlx5_core_dev *dev)
1499 {
1500 	devl_assert_locked(priv_to_devlink(dev));
1501 	mutex_lock(&dev->intf_state_mutex);
1502 
1503 	mlx5_detach_device(dev);
1504 
1505 	if (!test_bit(MLX5_INTERFACE_STATE_UP, &dev->intf_state)) {
1506 		mlx5_core_warn(dev, "%s: interface is down, NOP\n",
1507 			       __func__);
1508 		goto out;
1509 	}
1510 
1511 	clear_bit(MLX5_INTERFACE_STATE_UP, &dev->intf_state);
1512 	mlx5_unload(dev);
1513 	mlx5_function_teardown(dev, false);
1514 out:
1515 	mutex_unlock(&dev->intf_state_mutex);
1516 }
1517 
mlx5_unload_one(struct mlx5_core_dev * dev)1518 void mlx5_unload_one(struct mlx5_core_dev *dev)
1519 {
1520 	struct devlink *devlink = priv_to_devlink(dev);
1521 
1522 	devl_lock(devlink);
1523 	mlx5_unload_one_devl_locked(dev);
1524 	devl_unlock(devlink);
1525 }
1526 
1527 static const int types[] = {
1528 	MLX5_CAP_GENERAL,
1529 	MLX5_CAP_GENERAL_2,
1530 	MLX5_CAP_ETHERNET_OFFLOADS,
1531 	MLX5_CAP_IPOIB_ENHANCED_OFFLOADS,
1532 	MLX5_CAP_ODP,
1533 	MLX5_CAP_ATOMIC,
1534 	MLX5_CAP_ROCE,
1535 	MLX5_CAP_IPOIB_OFFLOADS,
1536 	MLX5_CAP_FLOW_TABLE,
1537 	MLX5_CAP_ESWITCH_FLOW_TABLE,
1538 	MLX5_CAP_ESWITCH,
1539 	MLX5_CAP_VECTOR_CALC,
1540 	MLX5_CAP_QOS,
1541 	MLX5_CAP_DEBUG,
1542 	MLX5_CAP_DEV_MEM,
1543 	MLX5_CAP_DEV_EVENT,
1544 	MLX5_CAP_TLS,
1545 	MLX5_CAP_VDPA_EMULATION,
1546 	MLX5_CAP_IPSEC,
1547 	MLX5_CAP_PORT_SELECTION,
1548 	MLX5_CAP_DEV_SHAMPO,
1549 	MLX5_CAP_MACSEC,
1550 	MLX5_CAP_ADV_VIRTUALIZATION,
1551 };
1552 
mlx5_hca_caps_free(struct mlx5_core_dev * dev)1553 static void mlx5_hca_caps_free(struct mlx5_core_dev *dev)
1554 {
1555 	int type;
1556 	int i;
1557 
1558 	for (i = 0; i < ARRAY_SIZE(types); i++) {
1559 		type = types[i];
1560 		kfree(dev->caps.hca[type]);
1561 	}
1562 }
1563 
mlx5_hca_caps_alloc(struct mlx5_core_dev * dev)1564 static int mlx5_hca_caps_alloc(struct mlx5_core_dev *dev)
1565 {
1566 	struct mlx5_hca_cap *cap;
1567 	int type;
1568 	int i;
1569 
1570 	for (i = 0; i < ARRAY_SIZE(types); i++) {
1571 		cap = kzalloc(sizeof(*cap), GFP_KERNEL);
1572 		if (!cap)
1573 			goto err;
1574 		type = types[i];
1575 		dev->caps.hca[type] = cap;
1576 	}
1577 
1578 	return 0;
1579 
1580 err:
1581 	mlx5_hca_caps_free(dev);
1582 	return -ENOMEM;
1583 }
1584 
mlx5_mdev_init(struct mlx5_core_dev * dev,int profile_idx)1585 int mlx5_mdev_init(struct mlx5_core_dev *dev, int profile_idx)
1586 {
1587 	struct mlx5_priv *priv = &dev->priv;
1588 	int err;
1589 
1590 	memcpy(&dev->profile, &profile[profile_idx], sizeof(dev->profile));
1591 	INIT_LIST_HEAD(&priv->ctx_list);
1592 	spin_lock_init(&priv->ctx_lock);
1593 	lockdep_register_key(&dev->lock_key);
1594 	mutex_init(&dev->intf_state_mutex);
1595 	lockdep_set_class(&dev->intf_state_mutex, &dev->lock_key);
1596 
1597 	mutex_init(&priv->bfregs.reg_head.lock);
1598 	mutex_init(&priv->bfregs.wc_head.lock);
1599 	INIT_LIST_HEAD(&priv->bfregs.reg_head.list);
1600 	INIT_LIST_HEAD(&priv->bfregs.wc_head.list);
1601 
1602 	mutex_init(&priv->alloc_mutex);
1603 	mutex_init(&priv->pgdir_mutex);
1604 	INIT_LIST_HEAD(&priv->pgdir_list);
1605 
1606 	priv->numa_node = dev_to_node(mlx5_core_dma_dev(dev));
1607 	priv->dbg.dbg_root = debugfs_create_dir(dev_name(dev->device),
1608 						mlx5_debugfs_root);
1609 	INIT_LIST_HEAD(&priv->traps);
1610 
1611 	err = mlx5_tout_init(dev);
1612 	if (err) {
1613 		mlx5_core_err(dev, "Failed initializing timeouts, aborting\n");
1614 		goto err_timeout_init;
1615 	}
1616 
1617 	err = mlx5_health_init(dev);
1618 	if (err)
1619 		goto err_health_init;
1620 
1621 	err = mlx5_pagealloc_init(dev);
1622 	if (err)
1623 		goto err_pagealloc_init;
1624 
1625 	err = mlx5_adev_init(dev);
1626 	if (err)
1627 		goto err_adev_init;
1628 
1629 	err = mlx5_hca_caps_alloc(dev);
1630 	if (err)
1631 		goto err_hca_caps;
1632 
1633 	/* The conjunction of sw_vhca_id with sw_owner_id will be a global
1634 	 * unique id per function which uses mlx5_core.
1635 	 * Those values are supplied to FW as part of the init HCA command to
1636 	 * be used by both driver and FW when it's applicable.
1637 	 */
1638 	dev->priv.sw_vhca_id = ida_alloc_range(&sw_vhca_ida, 1,
1639 					       MAX_SW_VHCA_ID,
1640 					       GFP_KERNEL);
1641 	if (dev->priv.sw_vhca_id < 0)
1642 		mlx5_core_err(dev, "failed to allocate sw_vhca_id, err=%d\n",
1643 			      dev->priv.sw_vhca_id);
1644 
1645 	return 0;
1646 
1647 err_hca_caps:
1648 	mlx5_adev_cleanup(dev);
1649 err_adev_init:
1650 	mlx5_pagealloc_cleanup(dev);
1651 err_pagealloc_init:
1652 	mlx5_health_cleanup(dev);
1653 err_health_init:
1654 	mlx5_tout_cleanup(dev);
1655 err_timeout_init:
1656 	debugfs_remove(dev->priv.dbg.dbg_root);
1657 	mutex_destroy(&priv->pgdir_mutex);
1658 	mutex_destroy(&priv->alloc_mutex);
1659 	mutex_destroy(&priv->bfregs.wc_head.lock);
1660 	mutex_destroy(&priv->bfregs.reg_head.lock);
1661 	mutex_destroy(&dev->intf_state_mutex);
1662 	lockdep_unregister_key(&dev->lock_key);
1663 	return err;
1664 }
1665 
mlx5_mdev_uninit(struct mlx5_core_dev * dev)1666 void mlx5_mdev_uninit(struct mlx5_core_dev *dev)
1667 {
1668 	struct mlx5_priv *priv = &dev->priv;
1669 
1670 	if (priv->sw_vhca_id > 0)
1671 		ida_free(&sw_vhca_ida, dev->priv.sw_vhca_id);
1672 
1673 	mlx5_hca_caps_free(dev);
1674 	mlx5_adev_cleanup(dev);
1675 	mlx5_pagealloc_cleanup(dev);
1676 	mlx5_health_cleanup(dev);
1677 	mlx5_tout_cleanup(dev);
1678 	debugfs_remove_recursive(dev->priv.dbg.dbg_root);
1679 	mutex_destroy(&priv->pgdir_mutex);
1680 	mutex_destroy(&priv->alloc_mutex);
1681 	mutex_destroy(&priv->bfregs.wc_head.lock);
1682 	mutex_destroy(&priv->bfregs.reg_head.lock);
1683 	mutex_destroy(&dev->intf_state_mutex);
1684 	lockdep_unregister_key(&dev->lock_key);
1685 }
1686 
probe_one(struct pci_dev * pdev,const struct pci_device_id * id)1687 static int probe_one(struct pci_dev *pdev, const struct pci_device_id *id)
1688 {
1689 	struct mlx5_core_dev *dev;
1690 	struct devlink *devlink;
1691 	int err;
1692 
1693 	devlink = mlx5_devlink_alloc(&pdev->dev);
1694 	if (!devlink) {
1695 		dev_err(&pdev->dev, "devlink alloc failed\n");
1696 		return -ENOMEM;
1697 	}
1698 
1699 	dev = devlink_priv(devlink);
1700 	dev->device = &pdev->dev;
1701 	dev->pdev = pdev;
1702 
1703 	dev->coredev_type = id->driver_data & MLX5_PCI_DEV_IS_VF ?
1704 			 MLX5_COREDEV_VF : MLX5_COREDEV_PF;
1705 
1706 	dev->priv.adev_idx = mlx5_adev_idx_alloc();
1707 	if (dev->priv.adev_idx < 0) {
1708 		err = dev->priv.adev_idx;
1709 		goto adev_init_err;
1710 	}
1711 
1712 	err = mlx5_mdev_init(dev, prof_sel);
1713 	if (err)
1714 		goto mdev_init_err;
1715 
1716 	err = mlx5_pci_init(dev, pdev, id);
1717 	if (err) {
1718 		mlx5_core_err(dev, "mlx5_pci_init failed with error code %d\n",
1719 			      err);
1720 		goto pci_init_err;
1721 	}
1722 
1723 	err = mlx5_init_one(dev);
1724 	if (err) {
1725 		mlx5_core_err(dev, "mlx5_init_one failed with error code %d\n",
1726 			      err);
1727 		goto err_init_one;
1728 	}
1729 
1730 	err = mlx5_crdump_enable(dev);
1731 	if (err)
1732 		dev_err(&pdev->dev, "mlx5_crdump_enable failed with error code %d\n", err);
1733 
1734 	pci_save_state(pdev);
1735 	devlink_register(devlink);
1736 	return 0;
1737 
1738 err_init_one:
1739 	mlx5_pci_close(dev);
1740 pci_init_err:
1741 	mlx5_mdev_uninit(dev);
1742 mdev_init_err:
1743 	mlx5_adev_idx_free(dev->priv.adev_idx);
1744 adev_init_err:
1745 	mlx5_devlink_free(devlink);
1746 
1747 	return err;
1748 }
1749 
remove_one(struct pci_dev * pdev)1750 static void remove_one(struct pci_dev *pdev)
1751 {
1752 	struct mlx5_core_dev *dev  = pci_get_drvdata(pdev);
1753 	struct devlink *devlink = priv_to_devlink(dev);
1754 
1755 	/* mlx5_drain_fw_reset() is using devlink APIs. Hence, we must drain
1756 	 * fw_reset before unregistering the devlink.
1757 	 */
1758 	mlx5_drain_fw_reset(dev);
1759 	set_bit(MLX5_BREAK_FW_WAIT, &dev->intf_state);
1760 	devlink_unregister(devlink);
1761 	mlx5_sriov_disable(pdev);
1762 	mlx5_crdump_disable(dev);
1763 	mlx5_drain_health_wq(dev);
1764 	mlx5_uninit_one(dev);
1765 	mlx5_pci_close(dev);
1766 	mlx5_mdev_uninit(dev);
1767 	mlx5_adev_idx_free(dev->priv.adev_idx);
1768 	mlx5_devlink_free(devlink);
1769 }
1770 
1771 #define mlx5_pci_trace(dev, fmt, ...) ({ \
1772 	struct mlx5_core_dev *__dev = (dev); \
1773 	mlx5_core_info(__dev, "%s Device state = %d health sensors: %d pci_status: %d. " fmt, \
1774 		       __func__, __dev->state, mlx5_health_check_fatal_sensors(__dev), \
1775 		       __dev->pci_status, ##__VA_ARGS__); \
1776 })
1777 
result2str(enum pci_ers_result result)1778 static const char *result2str(enum pci_ers_result result)
1779 {
1780 	return  result == PCI_ERS_RESULT_NEED_RESET ? "need reset" :
1781 		result == PCI_ERS_RESULT_DISCONNECT ? "disconnect" :
1782 		result == PCI_ERS_RESULT_RECOVERED  ? "recovered" :
1783 		"unknown";
1784 }
1785 
mlx5_pci_err_detected(struct pci_dev * pdev,pci_channel_state_t state)1786 static pci_ers_result_t mlx5_pci_err_detected(struct pci_dev *pdev,
1787 					      pci_channel_state_t state)
1788 {
1789 	struct mlx5_core_dev *dev = pci_get_drvdata(pdev);
1790 	enum pci_ers_result res;
1791 
1792 	mlx5_pci_trace(dev, "Enter, pci channel state = %d\n", state);
1793 
1794 	mlx5_enter_error_state(dev, false);
1795 	mlx5_error_sw_reset(dev);
1796 	mlx5_unload_one(dev);
1797 	mlx5_drain_health_wq(dev);
1798 	mlx5_pci_disable_device(dev);
1799 
1800 	res = state == pci_channel_io_perm_failure ?
1801 		PCI_ERS_RESULT_DISCONNECT : PCI_ERS_RESULT_NEED_RESET;
1802 
1803 	mlx5_core_info(dev, "%s Device state = %d pci_status: %d. Exit, result = %d, %s\n",
1804 		       __func__, dev->state, dev->pci_status, res, result2str(res));
1805 	return res;
1806 }
1807 
1808 /* wait for the device to show vital signs by waiting
1809  * for the health counter to start counting.
1810  */
wait_vital(struct pci_dev * pdev)1811 static int wait_vital(struct pci_dev *pdev)
1812 {
1813 	struct mlx5_core_dev *dev = pci_get_drvdata(pdev);
1814 	struct mlx5_core_health *health = &dev->priv.health;
1815 	const int niter = 100;
1816 	u32 last_count = 0;
1817 	u32 count;
1818 	int i;
1819 
1820 	for (i = 0; i < niter; i++) {
1821 		count = ioread32be(health->health_counter);
1822 		if (count && count != 0xffffffff) {
1823 			if (last_count && last_count != count) {
1824 				mlx5_core_info(dev,
1825 					       "wait vital counter value 0x%x after %d iterations\n",
1826 					       count, i);
1827 				return 0;
1828 			}
1829 			last_count = count;
1830 		}
1831 		msleep(50);
1832 	}
1833 
1834 	return -ETIMEDOUT;
1835 }
1836 
mlx5_pci_slot_reset(struct pci_dev * pdev)1837 static pci_ers_result_t mlx5_pci_slot_reset(struct pci_dev *pdev)
1838 {
1839 	enum pci_ers_result res = PCI_ERS_RESULT_DISCONNECT;
1840 	struct mlx5_core_dev *dev = pci_get_drvdata(pdev);
1841 	int err;
1842 
1843 	mlx5_core_info(dev, "%s Device state = %d pci_status: %d. Enter\n",
1844 		       __func__, dev->state, dev->pci_status);
1845 
1846 	err = mlx5_pci_enable_device(dev);
1847 	if (err) {
1848 		mlx5_core_err(dev, "%s: mlx5_pci_enable_device failed with error code: %d\n",
1849 			      __func__, err);
1850 		goto out;
1851 	}
1852 
1853 	pci_set_master(pdev);
1854 	pci_restore_state(pdev);
1855 	pci_save_state(pdev);
1856 
1857 	err = wait_vital(pdev);
1858 	if (err) {
1859 		mlx5_core_err(dev, "%s: wait vital failed with error code: %d\n",
1860 			      __func__, err);
1861 		goto out;
1862 	}
1863 
1864 	res = PCI_ERS_RESULT_RECOVERED;
1865 out:
1866 	mlx5_core_info(dev, "%s Device state = %d pci_status: %d. Exit, err = %d, result = %d, %s\n",
1867 		       __func__, dev->state, dev->pci_status, err, res, result2str(res));
1868 	return res;
1869 }
1870 
mlx5_pci_resume(struct pci_dev * pdev)1871 static void mlx5_pci_resume(struct pci_dev *pdev)
1872 {
1873 	struct mlx5_core_dev *dev = pci_get_drvdata(pdev);
1874 	int err;
1875 
1876 	mlx5_pci_trace(dev, "Enter, loading driver..\n");
1877 
1878 	err = mlx5_load_one(dev, false);
1879 
1880 	if (!err)
1881 		devlink_health_reporter_state_update(dev->priv.health.fw_fatal_reporter,
1882 						     DEVLINK_HEALTH_REPORTER_STATE_HEALTHY);
1883 
1884 	mlx5_pci_trace(dev, "Done, err = %d, device %s\n", err,
1885 		       !err ? "recovered" : "Failed");
1886 }
1887 
1888 static const struct pci_error_handlers mlx5_err_handler = {
1889 	.error_detected = mlx5_pci_err_detected,
1890 	.slot_reset	= mlx5_pci_slot_reset,
1891 	.resume		= mlx5_pci_resume
1892 };
1893 
mlx5_try_fast_unload(struct mlx5_core_dev * dev)1894 static int mlx5_try_fast_unload(struct mlx5_core_dev *dev)
1895 {
1896 	bool fast_teardown = false, force_teardown = false;
1897 	int ret = 1;
1898 
1899 	fast_teardown = MLX5_CAP_GEN(dev, fast_teardown);
1900 	force_teardown = MLX5_CAP_GEN(dev, force_teardown);
1901 
1902 	mlx5_core_dbg(dev, "force teardown firmware support=%d\n", force_teardown);
1903 	mlx5_core_dbg(dev, "fast teardown firmware support=%d\n", fast_teardown);
1904 
1905 	if (!fast_teardown && !force_teardown)
1906 		return -EOPNOTSUPP;
1907 
1908 	if (dev->state == MLX5_DEVICE_STATE_INTERNAL_ERROR) {
1909 		mlx5_core_dbg(dev, "Device in internal error state, giving up\n");
1910 		return -EAGAIN;
1911 	}
1912 
1913 	/* Panic tear down fw command will stop the PCI bus communication
1914 	 * with the HCA, so the health poll is no longer needed.
1915 	 */
1916 	mlx5_drain_health_wq(dev);
1917 	mlx5_stop_health_poll(dev, false);
1918 
1919 	ret = mlx5_cmd_fast_teardown_hca(dev);
1920 	if (!ret)
1921 		goto succeed;
1922 
1923 	ret = mlx5_cmd_force_teardown_hca(dev);
1924 	if (!ret)
1925 		goto succeed;
1926 
1927 	mlx5_core_dbg(dev, "Firmware couldn't do fast unload error: %d\n", ret);
1928 	mlx5_start_health_poll(dev);
1929 	return ret;
1930 
1931 succeed:
1932 	mlx5_enter_error_state(dev, true);
1933 
1934 	/* Some platforms requiring freeing the IRQ's in the shutdown
1935 	 * flow. If they aren't freed they can't be allocated after
1936 	 * kexec. There is no need to cleanup the mlx5_core software
1937 	 * contexts.
1938 	 */
1939 	mlx5_core_eq_free_irqs(dev);
1940 
1941 	return 0;
1942 }
1943 
shutdown(struct pci_dev * pdev)1944 static void shutdown(struct pci_dev *pdev)
1945 {
1946 	struct mlx5_core_dev *dev  = pci_get_drvdata(pdev);
1947 	int err;
1948 
1949 	mlx5_core_info(dev, "Shutdown was called\n");
1950 	set_bit(MLX5_BREAK_FW_WAIT, &dev->intf_state);
1951 	err = mlx5_try_fast_unload(dev);
1952 	if (err)
1953 		mlx5_unload_one(dev);
1954 	mlx5_pci_disable_device(dev);
1955 }
1956 
mlx5_suspend(struct pci_dev * pdev,pm_message_t state)1957 static int mlx5_suspend(struct pci_dev *pdev, pm_message_t state)
1958 {
1959 	struct mlx5_core_dev *dev = pci_get_drvdata(pdev);
1960 
1961 	mlx5_unload_one(dev);
1962 
1963 	return 0;
1964 }
1965 
mlx5_resume(struct pci_dev * pdev)1966 static int mlx5_resume(struct pci_dev *pdev)
1967 {
1968 	struct mlx5_core_dev *dev = pci_get_drvdata(pdev);
1969 
1970 	return mlx5_load_one(dev, false);
1971 }
1972 
1973 static const struct pci_device_id mlx5_core_pci_table[] = {
1974 	{ PCI_VDEVICE(MELLANOX, PCI_DEVICE_ID_MELLANOX_CONNECTIB) },
1975 	{ PCI_VDEVICE(MELLANOX, 0x1012), MLX5_PCI_DEV_IS_VF},	/* Connect-IB VF */
1976 	{ PCI_VDEVICE(MELLANOX, PCI_DEVICE_ID_MELLANOX_CONNECTX4) },
1977 	{ PCI_VDEVICE(MELLANOX, 0x1014), MLX5_PCI_DEV_IS_VF},	/* ConnectX-4 VF */
1978 	{ PCI_VDEVICE(MELLANOX, PCI_DEVICE_ID_MELLANOX_CONNECTX4_LX) },
1979 	{ PCI_VDEVICE(MELLANOX, 0x1016), MLX5_PCI_DEV_IS_VF},	/* ConnectX-4LX VF */
1980 	{ PCI_VDEVICE(MELLANOX, 0x1017) },			/* ConnectX-5, PCIe 3.0 */
1981 	{ PCI_VDEVICE(MELLANOX, 0x1018), MLX5_PCI_DEV_IS_VF},	/* ConnectX-5 VF */
1982 	{ PCI_VDEVICE(MELLANOX, 0x1019) },			/* ConnectX-5 Ex */
1983 	{ PCI_VDEVICE(MELLANOX, 0x101a), MLX5_PCI_DEV_IS_VF},	/* ConnectX-5 Ex VF */
1984 	{ PCI_VDEVICE(MELLANOX, 0x101b) },			/* ConnectX-6 */
1985 	{ PCI_VDEVICE(MELLANOX, 0x101c), MLX5_PCI_DEV_IS_VF},	/* ConnectX-6 VF */
1986 	{ PCI_VDEVICE(MELLANOX, 0x101d) },			/* ConnectX-6 Dx */
1987 	{ PCI_VDEVICE(MELLANOX, 0x101e), MLX5_PCI_DEV_IS_VF},	/* ConnectX Family mlx5Gen Virtual Function */
1988 	{ PCI_VDEVICE(MELLANOX, 0x101f) },			/* ConnectX-6 LX */
1989 	{ PCI_VDEVICE(MELLANOX, 0x1021) },			/* ConnectX-7 */
1990 	{ PCI_VDEVICE(MELLANOX, 0x1023) },			/* ConnectX-8 */
1991 	{ PCI_VDEVICE(MELLANOX, 0xa2d2) },			/* BlueField integrated ConnectX-5 network controller */
1992 	{ PCI_VDEVICE(MELLANOX, 0xa2d3), MLX5_PCI_DEV_IS_VF},	/* BlueField integrated ConnectX-5 network controller VF */
1993 	{ PCI_VDEVICE(MELLANOX, 0xa2d6) },			/* BlueField-2 integrated ConnectX-6 Dx network controller */
1994 	{ PCI_VDEVICE(MELLANOX, 0xa2dc) },			/* BlueField-3 integrated ConnectX-7 network controller */
1995 	{ PCI_VDEVICE(MELLANOX, 0xa2df) },			/* BlueField-4 integrated ConnectX-8 network controller */
1996 	{ 0, }
1997 };
1998 
1999 MODULE_DEVICE_TABLE(pci, mlx5_core_pci_table);
2000 
mlx5_disable_device(struct mlx5_core_dev * dev)2001 void mlx5_disable_device(struct mlx5_core_dev *dev)
2002 {
2003 	mlx5_error_sw_reset(dev);
2004 	mlx5_unload_one_devl_locked(dev);
2005 }
2006 
mlx5_recover_device(struct mlx5_core_dev * dev)2007 int mlx5_recover_device(struct mlx5_core_dev *dev)
2008 {
2009 	if (!mlx5_core_is_sf(dev)) {
2010 		mlx5_pci_disable_device(dev);
2011 		if (mlx5_pci_slot_reset(dev->pdev) != PCI_ERS_RESULT_RECOVERED)
2012 			return -EIO;
2013 	}
2014 
2015 	return mlx5_load_one_devl_locked(dev, true);
2016 }
2017 
2018 static struct pci_driver mlx5_core_driver = {
2019 	.name           = KBUILD_MODNAME,
2020 	.id_table       = mlx5_core_pci_table,
2021 	.probe          = probe_one,
2022 	.remove         = remove_one,
2023 	.suspend        = mlx5_suspend,
2024 	.resume         = mlx5_resume,
2025 	.shutdown	= shutdown,
2026 	.err_handler	= &mlx5_err_handler,
2027 	.sriov_configure   = mlx5_core_sriov_configure,
2028 	.sriov_get_vf_total_msix = mlx5_sriov_get_vf_total_msix,
2029 	.sriov_set_msix_vec_count = mlx5_core_sriov_set_msix_vec_count,
2030 };
2031 
2032 /**
2033  * mlx5_vf_get_core_dev - Get the mlx5 core device from a given VF PCI device if
2034  *                     mlx5_core is its driver.
2035  * @pdev: The associated PCI device.
2036  *
2037  * Upon return the interface state lock stay held to let caller uses it safely.
2038  * Caller must ensure to use the returned mlx5 device for a narrow window
2039  * and put it back with mlx5_vf_put_core_dev() immediately once usage was over.
2040  *
2041  * Return: Pointer to the associated mlx5_core_dev or NULL.
2042  */
mlx5_vf_get_core_dev(struct pci_dev * pdev)2043 struct mlx5_core_dev *mlx5_vf_get_core_dev(struct pci_dev *pdev)
2044 {
2045 	struct mlx5_core_dev *mdev;
2046 
2047 	mdev = pci_iov_get_pf_drvdata(pdev, &mlx5_core_driver);
2048 	if (IS_ERR(mdev))
2049 		return NULL;
2050 
2051 	mutex_lock(&mdev->intf_state_mutex);
2052 	if (!test_bit(MLX5_INTERFACE_STATE_UP, &mdev->intf_state)) {
2053 		mutex_unlock(&mdev->intf_state_mutex);
2054 		return NULL;
2055 	}
2056 
2057 	return mdev;
2058 }
2059 EXPORT_SYMBOL(mlx5_vf_get_core_dev);
2060 
2061 /**
2062  * mlx5_vf_put_core_dev - Put the mlx5 core device back.
2063  * @mdev: The mlx5 core device.
2064  *
2065  * Upon return the interface state lock is unlocked and caller should not
2066  * access the mdev any more.
2067  */
mlx5_vf_put_core_dev(struct mlx5_core_dev * mdev)2068 void mlx5_vf_put_core_dev(struct mlx5_core_dev *mdev)
2069 {
2070 	mutex_unlock(&mdev->intf_state_mutex);
2071 }
2072 EXPORT_SYMBOL(mlx5_vf_put_core_dev);
2073 
mlx5_core_verify_params(void)2074 static void mlx5_core_verify_params(void)
2075 {
2076 	if (prof_sel >= ARRAY_SIZE(profile)) {
2077 		pr_warn("mlx5_core: WARNING: Invalid module parameter prof_sel %d, valid range 0-%zu, changing back to default(%d)\n",
2078 			prof_sel,
2079 			ARRAY_SIZE(profile) - 1,
2080 			MLX5_DEFAULT_PROF);
2081 		prof_sel = MLX5_DEFAULT_PROF;
2082 	}
2083 }
2084 
mlx5_init(void)2085 static int __init mlx5_init(void)
2086 {
2087 	int err;
2088 
2089 	WARN_ONCE(strcmp(MLX5_ADEV_NAME, KBUILD_MODNAME),
2090 		  "mlx5_core name not in sync with kernel module name");
2091 
2092 	get_random_bytes(&sw_owner_id, sizeof(sw_owner_id));
2093 
2094 	mlx5_core_verify_params();
2095 	mlx5_register_debugfs();
2096 
2097 	err = pci_register_driver(&mlx5_core_driver);
2098 	if (err)
2099 		goto err_debug;
2100 
2101 	err = mlx5_sf_driver_register();
2102 	if (err)
2103 		goto err_sf;
2104 
2105 	err = mlx5e_init();
2106 	if (err)
2107 		goto err_en;
2108 
2109 	return 0;
2110 
2111 err_en:
2112 	mlx5_sf_driver_unregister();
2113 err_sf:
2114 	pci_unregister_driver(&mlx5_core_driver);
2115 err_debug:
2116 	mlx5_unregister_debugfs();
2117 	return err;
2118 }
2119 
mlx5_cleanup(void)2120 static void __exit mlx5_cleanup(void)
2121 {
2122 	mlx5e_cleanup();
2123 	mlx5_sf_driver_unregister();
2124 	pci_unregister_driver(&mlx5_core_driver);
2125 	mlx5_unregister_debugfs();
2126 }
2127 
2128 module_init(mlx5_init);
2129 module_exit(mlx5_cleanup);
2130