xref: /linux/drivers/net/ethernet/mellanox/mlx5/core/dev.c (revision c6fbb759)
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/mlx5/driver.h>
34 #include <linux/mlx5/eswitch.h>
35 #include <linux/mlx5/mlx5_ifc_vdpa.h>
36 #include <linux/mlx5/vport.h>
37 #include "mlx5_core.h"
38 
39 /* intf dev list mutex */
40 static DEFINE_MUTEX(mlx5_intf_mutex);
41 static DEFINE_IDA(mlx5_adev_ida);
42 
43 static bool is_eth_rep_supported(struct mlx5_core_dev *dev)
44 {
45 	if (!IS_ENABLED(CONFIG_MLX5_ESWITCH))
46 		return false;
47 
48 	if (!MLX5_ESWITCH_MANAGER(dev))
49 		return false;
50 
51 	if (!is_mdev_switchdev_mode(dev))
52 		return false;
53 
54 	return true;
55 }
56 
57 bool mlx5_eth_supported(struct mlx5_core_dev *dev)
58 {
59 	if (!IS_ENABLED(CONFIG_MLX5_CORE_EN))
60 		return false;
61 
62 	if (MLX5_CAP_GEN(dev, port_type) != MLX5_CAP_PORT_TYPE_ETH)
63 		return false;
64 
65 	if (!MLX5_CAP_GEN(dev, eth_net_offloads)) {
66 		mlx5_core_warn(dev, "Missing eth_net_offloads capability\n");
67 		return false;
68 	}
69 
70 	if (!MLX5_CAP_GEN(dev, nic_flow_table)) {
71 		mlx5_core_warn(dev, "Missing nic_flow_table capability\n");
72 		return false;
73 	}
74 
75 	if (!MLX5_CAP_ETH(dev, csum_cap)) {
76 		mlx5_core_warn(dev, "Missing csum_cap capability\n");
77 		return false;
78 	}
79 
80 	if (!MLX5_CAP_ETH(dev, max_lso_cap)) {
81 		mlx5_core_warn(dev, "Missing max_lso_cap capability\n");
82 		return false;
83 	}
84 
85 	if (!MLX5_CAP_ETH(dev, vlan_cap)) {
86 		mlx5_core_warn(dev, "Missing vlan_cap capability\n");
87 		return false;
88 	}
89 
90 	if (!MLX5_CAP_ETH(dev, rss_ind_tbl_cap)) {
91 		mlx5_core_warn(dev, "Missing rss_ind_tbl_cap capability\n");
92 		return false;
93 	}
94 
95 	if (MLX5_CAP_FLOWTABLE(dev,
96 			       flow_table_properties_nic_receive.max_ft_level) < 3) {
97 		mlx5_core_warn(dev, "max_ft_level < 3\n");
98 		return false;
99 	}
100 
101 	if (!MLX5_CAP_ETH(dev, self_lb_en_modifiable))
102 		mlx5_core_warn(dev, "Self loop back prevention is not supported\n");
103 	if (!MLX5_CAP_GEN(dev, cq_moderation))
104 		mlx5_core_warn(dev, "CQ moderation is not supported\n");
105 
106 	return true;
107 }
108 
109 static bool is_eth_enabled(struct mlx5_core_dev *dev)
110 {
111 	union devlink_param_value val;
112 	int err;
113 
114 	err = devlink_param_driverinit_value_get(priv_to_devlink(dev),
115 						 DEVLINK_PARAM_GENERIC_ID_ENABLE_ETH,
116 						 &val);
117 	return err ? false : val.vbool;
118 }
119 
120 bool mlx5_vnet_supported(struct mlx5_core_dev *dev)
121 {
122 	if (!IS_ENABLED(CONFIG_MLX5_VDPA_NET))
123 		return false;
124 
125 	if (mlx5_core_is_pf(dev))
126 		return false;
127 
128 	if (!(MLX5_CAP_GEN_64(dev, general_obj_types) &
129 	      MLX5_GENERAL_OBJ_TYPES_CAP_VIRTIO_NET_Q))
130 		return false;
131 
132 	if (!(MLX5_CAP_DEV_VDPA_EMULATION(dev, event_mode) &
133 	      MLX5_VIRTIO_Q_EVENT_MODE_QP_MODE))
134 		return false;
135 
136 	if (!MLX5_CAP_DEV_VDPA_EMULATION(dev, eth_frame_offload_type))
137 		return false;
138 
139 	return true;
140 }
141 
142 static bool is_vnet_enabled(struct mlx5_core_dev *dev)
143 {
144 	union devlink_param_value val;
145 	int err;
146 
147 	err = devlink_param_driverinit_value_get(priv_to_devlink(dev),
148 						 DEVLINK_PARAM_GENERIC_ID_ENABLE_VNET,
149 						 &val);
150 	return err ? false : val.vbool;
151 }
152 
153 static bool is_ib_rep_supported(struct mlx5_core_dev *dev)
154 {
155 	if (!IS_ENABLED(CONFIG_MLX5_INFINIBAND))
156 		return false;
157 
158 	if (dev->priv.flags & MLX5_PRIV_FLAGS_DISABLE_IB_ADEV)
159 		return false;
160 
161 	if (!is_eth_rep_supported(dev))
162 		return false;
163 
164 	if (!MLX5_ESWITCH_MANAGER(dev))
165 		return false;
166 
167 	if (!is_mdev_switchdev_mode(dev))
168 		return false;
169 
170 	if (mlx5_core_mp_enabled(dev))
171 		return false;
172 
173 	return true;
174 }
175 
176 static bool is_mp_supported(struct mlx5_core_dev *dev)
177 {
178 	if (!IS_ENABLED(CONFIG_MLX5_INFINIBAND))
179 		return false;
180 
181 	if (dev->priv.flags & MLX5_PRIV_FLAGS_DISABLE_IB_ADEV)
182 		return false;
183 
184 	if (is_ib_rep_supported(dev))
185 		return false;
186 
187 	if (MLX5_CAP_GEN(dev, port_type) != MLX5_CAP_PORT_TYPE_ETH)
188 		return false;
189 
190 	if (!mlx5_core_is_mp_slave(dev))
191 		return false;
192 
193 	return true;
194 }
195 
196 bool mlx5_rdma_supported(struct mlx5_core_dev *dev)
197 {
198 	if (!IS_ENABLED(CONFIG_MLX5_INFINIBAND))
199 		return false;
200 
201 	if (dev->priv.flags & MLX5_PRIV_FLAGS_DISABLE_IB_ADEV)
202 		return false;
203 
204 	if (is_ib_rep_supported(dev))
205 		return false;
206 
207 	if (is_mp_supported(dev))
208 		return false;
209 
210 	return true;
211 }
212 
213 static bool is_ib_enabled(struct mlx5_core_dev *dev)
214 {
215 	union devlink_param_value val;
216 	int err;
217 
218 	err = devlink_param_driverinit_value_get(priv_to_devlink(dev),
219 						 DEVLINK_PARAM_GENERIC_ID_ENABLE_RDMA,
220 						 &val);
221 	return err ? false : val.vbool;
222 }
223 
224 enum {
225 	MLX5_INTERFACE_PROTOCOL_ETH,
226 	MLX5_INTERFACE_PROTOCOL_ETH_REP,
227 
228 	MLX5_INTERFACE_PROTOCOL_IB,
229 	MLX5_INTERFACE_PROTOCOL_IB_REP,
230 	MLX5_INTERFACE_PROTOCOL_MPIB,
231 
232 	MLX5_INTERFACE_PROTOCOL_VNET,
233 };
234 
235 static const struct mlx5_adev_device {
236 	const char *suffix;
237 	bool (*is_supported)(struct mlx5_core_dev *dev);
238 	bool (*is_enabled)(struct mlx5_core_dev *dev);
239 } mlx5_adev_devices[] = {
240 	[MLX5_INTERFACE_PROTOCOL_VNET] = { .suffix = "vnet",
241 					   .is_supported = &mlx5_vnet_supported,
242 					   .is_enabled = &is_vnet_enabled },
243 	[MLX5_INTERFACE_PROTOCOL_IB] = { .suffix = "rdma",
244 					 .is_supported = &mlx5_rdma_supported,
245 					 .is_enabled = &is_ib_enabled },
246 	[MLX5_INTERFACE_PROTOCOL_ETH] = { .suffix = "eth",
247 					  .is_supported = &mlx5_eth_supported,
248 					  .is_enabled = &is_eth_enabled },
249 	[MLX5_INTERFACE_PROTOCOL_ETH_REP] = { .suffix = "eth-rep",
250 					   .is_supported = &is_eth_rep_supported },
251 	[MLX5_INTERFACE_PROTOCOL_IB_REP] = { .suffix = "rdma-rep",
252 					   .is_supported = &is_ib_rep_supported },
253 	[MLX5_INTERFACE_PROTOCOL_MPIB] = { .suffix = "multiport",
254 					   .is_supported = &is_mp_supported },
255 };
256 
257 int mlx5_adev_idx_alloc(void)
258 {
259 	return ida_alloc(&mlx5_adev_ida, GFP_KERNEL);
260 }
261 
262 void mlx5_adev_idx_free(int idx)
263 {
264 	ida_free(&mlx5_adev_ida, idx);
265 }
266 
267 int mlx5_adev_init(struct mlx5_core_dev *dev)
268 {
269 	struct mlx5_priv *priv = &dev->priv;
270 
271 	priv->adev = kcalloc(ARRAY_SIZE(mlx5_adev_devices),
272 			     sizeof(struct mlx5_adev *), GFP_KERNEL);
273 	if (!priv->adev)
274 		return -ENOMEM;
275 
276 	return 0;
277 }
278 
279 void mlx5_adev_cleanup(struct mlx5_core_dev *dev)
280 {
281 	struct mlx5_priv *priv = &dev->priv;
282 
283 	kfree(priv->adev);
284 }
285 
286 static void adev_release(struct device *dev)
287 {
288 	struct mlx5_adev *mlx5_adev =
289 		container_of(dev, struct mlx5_adev, adev.dev);
290 	struct mlx5_priv *priv = &mlx5_adev->mdev->priv;
291 	int idx = mlx5_adev->idx;
292 
293 	kfree(mlx5_adev);
294 	priv->adev[idx] = NULL;
295 }
296 
297 static struct mlx5_adev *add_adev(struct mlx5_core_dev *dev, int idx)
298 {
299 	const char *suffix = mlx5_adev_devices[idx].suffix;
300 	struct auxiliary_device *adev;
301 	struct mlx5_adev *madev;
302 	int ret;
303 
304 	madev = kzalloc(sizeof(*madev), GFP_KERNEL);
305 	if (!madev)
306 		return ERR_PTR(-ENOMEM);
307 
308 	adev = &madev->adev;
309 	adev->id = dev->priv.adev_idx;
310 	adev->name = suffix;
311 	adev->dev.parent = dev->device;
312 	adev->dev.release = adev_release;
313 	madev->mdev = dev;
314 	madev->idx = idx;
315 
316 	ret = auxiliary_device_init(adev);
317 	if (ret) {
318 		kfree(madev);
319 		return ERR_PTR(ret);
320 	}
321 
322 	ret = auxiliary_device_add(adev);
323 	if (ret) {
324 		auxiliary_device_uninit(adev);
325 		return ERR_PTR(ret);
326 	}
327 	return madev;
328 }
329 
330 static void del_adev(struct auxiliary_device *adev)
331 {
332 	auxiliary_device_delete(adev);
333 	auxiliary_device_uninit(adev);
334 }
335 
336 int mlx5_attach_device(struct mlx5_core_dev *dev)
337 {
338 	struct mlx5_priv *priv = &dev->priv;
339 	struct auxiliary_device *adev;
340 	struct auxiliary_driver *adrv;
341 	int ret = 0, i;
342 
343 	devl_assert_locked(priv_to_devlink(dev));
344 	mutex_lock(&mlx5_intf_mutex);
345 	priv->flags &= ~MLX5_PRIV_FLAGS_DETACH;
346 	priv->flags |= MLX5_PRIV_FLAGS_MLX5E_LOCKED_FLOW;
347 	for (i = 0; i < ARRAY_SIZE(mlx5_adev_devices); i++) {
348 		if (!priv->adev[i]) {
349 			bool is_supported = false;
350 
351 			if (mlx5_adev_devices[i].is_enabled) {
352 				bool enabled;
353 
354 				enabled = mlx5_adev_devices[i].is_enabled(dev);
355 				if (!enabled)
356 					continue;
357 			}
358 
359 			if (mlx5_adev_devices[i].is_supported)
360 				is_supported = mlx5_adev_devices[i].is_supported(dev);
361 
362 			if (!is_supported)
363 				continue;
364 
365 			priv->adev[i] = add_adev(dev, i);
366 			if (IS_ERR(priv->adev[i])) {
367 				ret = PTR_ERR(priv->adev[i]);
368 				priv->adev[i] = NULL;
369 			}
370 		} else {
371 			adev = &priv->adev[i]->adev;
372 
373 			/* Pay attention that this is not PCI driver that
374 			 * mlx5_core_dev is connected, but auxiliary driver.
375 			 *
376 			 * Here we can race of module unload with devlink
377 			 * reload, but we don't need to take extra lock because
378 			 * we are holding global mlx5_intf_mutex.
379 			 */
380 			if (!adev->dev.driver)
381 				continue;
382 			adrv = to_auxiliary_drv(adev->dev.driver);
383 
384 			if (adrv->resume)
385 				ret = adrv->resume(adev);
386 		}
387 		if (ret) {
388 			mlx5_core_warn(dev, "Device[%d] (%s) failed to load\n",
389 				       i, mlx5_adev_devices[i].suffix);
390 
391 			break;
392 		}
393 	}
394 	priv->flags &= ~MLX5_PRIV_FLAGS_MLX5E_LOCKED_FLOW;
395 	mutex_unlock(&mlx5_intf_mutex);
396 	return ret;
397 }
398 
399 void mlx5_detach_device(struct mlx5_core_dev *dev)
400 {
401 	struct mlx5_priv *priv = &dev->priv;
402 	struct auxiliary_device *adev;
403 	struct auxiliary_driver *adrv;
404 	pm_message_t pm = {};
405 	int i;
406 
407 	devl_assert_locked(priv_to_devlink(dev));
408 	mutex_lock(&mlx5_intf_mutex);
409 	priv->flags |= MLX5_PRIV_FLAGS_MLX5E_LOCKED_FLOW;
410 	for (i = ARRAY_SIZE(mlx5_adev_devices) - 1; i >= 0; i--) {
411 		if (!priv->adev[i])
412 			continue;
413 
414 		if (mlx5_adev_devices[i].is_enabled) {
415 			bool enabled;
416 
417 			enabled = mlx5_adev_devices[i].is_enabled(dev);
418 			if (!enabled)
419 				goto skip_suspend;
420 		}
421 
422 		adev = &priv->adev[i]->adev;
423 		/* Auxiliary driver was unbind manually through sysfs */
424 		if (!adev->dev.driver)
425 			goto skip_suspend;
426 
427 		adrv = to_auxiliary_drv(adev->dev.driver);
428 
429 		if (adrv->suspend) {
430 			adrv->suspend(adev, pm);
431 			continue;
432 		}
433 
434 skip_suspend:
435 		del_adev(&priv->adev[i]->adev);
436 		priv->adev[i] = NULL;
437 	}
438 	priv->flags &= ~MLX5_PRIV_FLAGS_MLX5E_LOCKED_FLOW;
439 	priv->flags |= MLX5_PRIV_FLAGS_DETACH;
440 	mutex_unlock(&mlx5_intf_mutex);
441 }
442 
443 int mlx5_register_device(struct mlx5_core_dev *dev)
444 {
445 	int ret;
446 
447 	devl_assert_locked(priv_to_devlink(dev));
448 	mutex_lock(&mlx5_intf_mutex);
449 	dev->priv.flags &= ~MLX5_PRIV_FLAGS_DISABLE_ALL_ADEV;
450 	ret = mlx5_rescan_drivers_locked(dev);
451 	mutex_unlock(&mlx5_intf_mutex);
452 	if (ret)
453 		mlx5_unregister_device(dev);
454 
455 	return ret;
456 }
457 
458 void mlx5_unregister_device(struct mlx5_core_dev *dev)
459 {
460 	devl_assert_locked(priv_to_devlink(dev));
461 	mutex_lock(&mlx5_intf_mutex);
462 	dev->priv.flags = MLX5_PRIV_FLAGS_DISABLE_ALL_ADEV;
463 	mlx5_rescan_drivers_locked(dev);
464 	mutex_unlock(&mlx5_intf_mutex);
465 }
466 
467 static int add_drivers(struct mlx5_core_dev *dev)
468 {
469 	struct mlx5_priv *priv = &dev->priv;
470 	int i, ret = 0;
471 
472 	for (i = 0; i < ARRAY_SIZE(mlx5_adev_devices); i++) {
473 		bool is_supported = false;
474 
475 		if (priv->adev[i])
476 			continue;
477 
478 		if (mlx5_adev_devices[i].is_supported)
479 			is_supported = mlx5_adev_devices[i].is_supported(dev);
480 
481 		if (!is_supported)
482 			continue;
483 
484 		priv->adev[i] = add_adev(dev, i);
485 		if (IS_ERR(priv->adev[i])) {
486 			mlx5_core_warn(dev, "Device[%d] (%s) failed to load\n",
487 				       i, mlx5_adev_devices[i].suffix);
488 			/* We continue to rescan drivers and leave to the caller
489 			 * to make decision if to release everything or continue.
490 			 */
491 			ret = PTR_ERR(priv->adev[i]);
492 			priv->adev[i] = NULL;
493 		}
494 	}
495 	return ret;
496 }
497 
498 static void delete_drivers(struct mlx5_core_dev *dev)
499 {
500 	struct mlx5_priv *priv = &dev->priv;
501 	bool delete_all;
502 	int i;
503 
504 	delete_all = priv->flags & MLX5_PRIV_FLAGS_DISABLE_ALL_ADEV;
505 
506 	for (i = ARRAY_SIZE(mlx5_adev_devices) - 1; i >= 0; i--) {
507 		bool is_supported = false;
508 
509 		if (!priv->adev[i])
510 			continue;
511 
512 		if (mlx5_adev_devices[i].is_enabled) {
513 			bool enabled;
514 
515 			enabled = mlx5_adev_devices[i].is_enabled(dev);
516 			if (!enabled)
517 				goto del_adev;
518 		}
519 
520 		if (mlx5_adev_devices[i].is_supported && !delete_all)
521 			is_supported = mlx5_adev_devices[i].is_supported(dev);
522 
523 		if (is_supported)
524 			continue;
525 
526 del_adev:
527 		del_adev(&priv->adev[i]->adev);
528 		priv->adev[i] = NULL;
529 	}
530 }
531 
532 /* This function is used after mlx5_core_dev is reconfigured.
533  */
534 int mlx5_rescan_drivers_locked(struct mlx5_core_dev *dev)
535 {
536 	struct mlx5_priv *priv = &dev->priv;
537 	int err = 0;
538 
539 	lockdep_assert_held(&mlx5_intf_mutex);
540 	if (priv->flags & MLX5_PRIV_FLAGS_DETACH)
541 		return 0;
542 
543 	priv->flags |= MLX5_PRIV_FLAGS_MLX5E_LOCKED_FLOW;
544 	delete_drivers(dev);
545 	if (priv->flags & MLX5_PRIV_FLAGS_DISABLE_ALL_ADEV)
546 		goto out;
547 
548 	err = add_drivers(dev);
549 
550 out:
551 	priv->flags &= ~MLX5_PRIV_FLAGS_MLX5E_LOCKED_FLOW;
552 	return err;
553 }
554 
555 bool mlx5_same_hw_devs(struct mlx5_core_dev *dev, struct mlx5_core_dev *peer_dev)
556 {
557 	u64 fsystem_guid, psystem_guid;
558 
559 	fsystem_guid = mlx5_query_nic_system_image_guid(dev);
560 	psystem_guid = mlx5_query_nic_system_image_guid(peer_dev);
561 
562 	return (fsystem_guid && psystem_guid && fsystem_guid == psystem_guid);
563 }
564 
565 static u32 mlx5_gen_pci_id(const struct mlx5_core_dev *dev)
566 {
567 	return (u32)((pci_domain_nr(dev->pdev->bus) << 16) |
568 		     (dev->pdev->bus->number << 8) |
569 		     PCI_SLOT(dev->pdev->devfn));
570 }
571 
572 static int _next_phys_dev(struct mlx5_core_dev *mdev,
573 			  const struct mlx5_core_dev *curr)
574 {
575 	if (!mlx5_core_is_pf(mdev))
576 		return 0;
577 
578 	if (mdev == curr)
579 		return 0;
580 
581 	if (!mlx5_same_hw_devs(mdev, (struct mlx5_core_dev *)curr) &&
582 	    mlx5_gen_pci_id(mdev) != mlx5_gen_pci_id(curr))
583 		return 0;
584 
585 	return 1;
586 }
587 
588 static void *pci_get_other_drvdata(struct device *this, struct device *other)
589 {
590 	if (this->driver != other->driver)
591 		return NULL;
592 
593 	return pci_get_drvdata(to_pci_dev(other));
594 }
595 
596 static int next_phys_dev_lag(struct device *dev, const void *data)
597 {
598 	struct mlx5_core_dev *mdev, *this = (struct mlx5_core_dev *)data;
599 
600 	mdev = pci_get_other_drvdata(this->device, dev);
601 	if (!mdev)
602 		return 0;
603 
604 	if (!MLX5_CAP_GEN(mdev, vport_group_manager) ||
605 	    !MLX5_CAP_GEN(mdev, lag_master) ||
606 	    (MLX5_CAP_GEN(mdev, num_lag_ports) > MLX5_MAX_PORTS ||
607 	     MLX5_CAP_GEN(mdev, num_lag_ports) <= 1))
608 		return 0;
609 
610 	return _next_phys_dev(mdev, data);
611 }
612 
613 static struct mlx5_core_dev *mlx5_get_next_dev(struct mlx5_core_dev *dev,
614 					       int (*match)(struct device *dev, const void *data))
615 {
616 	struct device *next;
617 
618 	if (!mlx5_core_is_pf(dev))
619 		return NULL;
620 
621 	next = bus_find_device(&pci_bus_type, NULL, dev, match);
622 	if (!next)
623 		return NULL;
624 
625 	put_device(next);
626 	return pci_get_drvdata(to_pci_dev(next));
627 }
628 
629 /* Must be called with intf_mutex held */
630 struct mlx5_core_dev *mlx5_get_next_phys_dev_lag(struct mlx5_core_dev *dev)
631 {
632 	lockdep_assert_held(&mlx5_intf_mutex);
633 	return mlx5_get_next_dev(dev, &next_phys_dev_lag);
634 }
635 
636 void mlx5_dev_list_lock(void)
637 {
638 	mutex_lock(&mlx5_intf_mutex);
639 }
640 void mlx5_dev_list_unlock(void)
641 {
642 	mutex_unlock(&mlx5_intf_mutex);
643 }
644 
645 int mlx5_dev_list_trylock(void)
646 {
647 	return mutex_trylock(&mlx5_intf_mutex);
648 }
649