1 // SPDX-License-Identifier: GPL-2.0 OR Linux-OpenIB
3 * Copyright (c) 2013-2020, Mellanox Technologies inc. All rights reserved.
4 * Copyright (c) 2020, Intel Corporation. All rights reserved.
7 #include <linux/debugfs.h>
8 #include <linux/highmem.h>
9 #include <linux/module.h>
10 #include <linux/init.h>
11 #include <linux/errno.h>
12 #include <linux/pci.h>
13 #include <linux/dma-mapping.h>
14 #include <linux/slab.h>
15 #include <linux/bitmap.h>
16 #include <linux/sched.h>
17 #include <linux/sched/mm.h>
18 #include <linux/sched/task.h>
19 #include <linux/delay.h>
20 #include <rdma/ib_user_verbs.h>
21 #include <rdma/ib_addr.h>
22 #include <rdma/ib_cache.h>
23 #include <linux/mlx5/port.h>
24 #include <linux/mlx5/vport.h>
25 #include <linux/mlx5/fs.h>
26 #include <linux/mlx5/eswitch.h>
27 #include <linux/list.h>
28 #include <rdma/ib_smi.h>
29 #include <rdma/ib_umem_odp.h>
32 #include <linux/etherdevice.h>
45 #include <rdma/uverbs_std_types.h>
46 #include <rdma/uverbs_ioctl.h>
47 #include <rdma/mlx5_user_ioctl_verbs.h>
48 #include <rdma/mlx5_user_ioctl_cmds.h>
51 #define UVERBS_MODULE_NAME mlx5_ib
52 #include <rdma/uverbs_named_ioctl.h>
54 MODULE_AUTHOR("Eli Cohen <eli@mellanox.com>");
55 MODULE_DESCRIPTION("Mellanox 5th generation network adapters (ConnectX series) IB driver");
56 MODULE_LICENSE("Dual BSD/GPL");
58 struct mlx5_ib_event_work {
59 struct work_struct work;
61 struct mlx5_ib_dev *dev;
62 struct mlx5_ib_multiport_info *mpi;
70 MLX5_ATOMIC_SIZE_QP_8BYTES = 1 << 3,
73 static struct workqueue_struct *mlx5_ib_event_wq;
74 static LIST_HEAD(mlx5_ib_unaffiliated_port_list);
75 static LIST_HEAD(mlx5_ib_dev_list);
77 * This mutex should be held when accessing either of the above lists
79 static DEFINE_MUTEX(mlx5_ib_multiport_mutex);
81 struct mlx5_ib_dev *mlx5_ib_get_ibdev_from_mpi(struct mlx5_ib_multiport_info *mpi)
83 struct mlx5_ib_dev *dev;
85 mutex_lock(&mlx5_ib_multiport_mutex);
87 mutex_unlock(&mlx5_ib_multiport_mutex);
91 static enum rdma_link_layer
92 mlx5_port_type_cap_to_rdma_ll(int port_type_cap)
94 switch (port_type_cap) {
95 case MLX5_CAP_PORT_TYPE_IB:
96 return IB_LINK_LAYER_INFINIBAND;
97 case MLX5_CAP_PORT_TYPE_ETH:
98 return IB_LINK_LAYER_ETHERNET;
100 return IB_LINK_LAYER_UNSPECIFIED;
104 static enum rdma_link_layer
105 mlx5_ib_port_link_layer(struct ib_device *device, u32 port_num)
107 struct mlx5_ib_dev *dev = to_mdev(device);
108 int port_type_cap = MLX5_CAP_GEN(dev->mdev, port_type);
110 return mlx5_port_type_cap_to_rdma_ll(port_type_cap);
113 static int get_port_state(struct ib_device *ibdev,
115 enum ib_port_state *state)
117 struct ib_port_attr attr;
120 memset(&attr, 0, sizeof(attr));
121 ret = ibdev->ops.query_port(ibdev, port_num, &attr);
127 static struct mlx5_roce *mlx5_get_rep_roce(struct mlx5_ib_dev *dev,
128 struct net_device *ndev,
129 struct net_device *upper,
132 struct net_device *rep_ndev;
133 struct mlx5_ib_port *port;
136 for (i = 0; i < dev->num_ports; i++) {
137 port = &dev->port[i];
141 if (upper == ndev && port->rep->vport == MLX5_VPORT_UPLINK) {
146 if (upper && port->rep->vport == MLX5_VPORT_UPLINK)
149 read_lock(&port->roce.netdev_lock);
150 rep_ndev = mlx5_ib_get_rep_netdev(port->rep->esw,
152 if (rep_ndev == ndev) {
153 read_unlock(&port->roce.netdev_lock);
157 read_unlock(&port->roce.netdev_lock);
163 static int mlx5_netdev_event(struct notifier_block *this,
164 unsigned long event, void *ptr)
166 struct mlx5_roce *roce = container_of(this, struct mlx5_roce, nb);
167 struct net_device *ndev = netdev_notifier_info_to_dev(ptr);
168 u32 port_num = roce->native_port_num;
169 struct mlx5_core_dev *mdev;
170 struct mlx5_ib_dev *ibdev;
173 mdev = mlx5_ib_get_native_port_mdev(ibdev, port_num, NULL);
178 case NETDEV_REGISTER:
179 /* Should already be registered during the load */
182 write_lock(&roce->netdev_lock);
183 if (ndev->dev.parent == mdev->device)
185 write_unlock(&roce->netdev_lock);
188 case NETDEV_UNREGISTER:
189 /* In case of reps, ib device goes away before the netdevs */
190 write_lock(&roce->netdev_lock);
191 if (roce->netdev == ndev)
193 write_unlock(&roce->netdev_lock);
199 struct net_device *lag_ndev = mlx5_lag_get_roce_netdev(mdev);
200 struct net_device *upper = NULL;
203 upper = netdev_master_upper_dev_get(lag_ndev);
208 roce = mlx5_get_rep_roce(ibdev, ndev, upper, &port_num);
211 if ((upper == ndev ||
212 ((!upper || ibdev->is_rep) && ndev == roce->netdev)) &&
214 struct ib_event ibev = { };
215 enum ib_port_state port_state;
217 if (get_port_state(&ibdev->ib_dev, port_num,
221 if (roce->last_port_state == port_state)
224 roce->last_port_state = port_state;
225 ibev.device = &ibdev->ib_dev;
226 if (port_state == IB_PORT_DOWN)
227 ibev.event = IB_EVENT_PORT_ERR;
228 else if (port_state == IB_PORT_ACTIVE)
229 ibev.event = IB_EVENT_PORT_ACTIVE;
233 ibev.element.port_num = port_num;
234 ib_dispatch_event(&ibev);
243 mlx5_ib_put_native_port_mdev(ibdev, port_num);
247 static struct net_device *mlx5_ib_get_netdev(struct ib_device *device,
250 struct mlx5_ib_dev *ibdev = to_mdev(device);
251 struct net_device *ndev;
252 struct mlx5_core_dev *mdev;
254 mdev = mlx5_ib_get_native_port_mdev(ibdev, port_num, NULL);
258 ndev = mlx5_lag_get_roce_netdev(mdev);
262 /* Ensure ndev does not disappear before we invoke dev_hold()
264 read_lock(&ibdev->port[port_num - 1].roce.netdev_lock);
265 ndev = ibdev->port[port_num - 1].roce.netdev;
268 read_unlock(&ibdev->port[port_num - 1].roce.netdev_lock);
271 mlx5_ib_put_native_port_mdev(ibdev, port_num);
275 struct mlx5_core_dev *mlx5_ib_get_native_port_mdev(struct mlx5_ib_dev *ibdev,
277 u32 *native_port_num)
279 enum rdma_link_layer ll = mlx5_ib_port_link_layer(&ibdev->ib_dev,
281 struct mlx5_core_dev *mdev = NULL;
282 struct mlx5_ib_multiport_info *mpi;
283 struct mlx5_ib_port *port;
285 if (!mlx5_core_mp_enabled(ibdev->mdev) ||
286 ll != IB_LINK_LAYER_ETHERNET) {
288 *native_port_num = ib_port_num;
293 *native_port_num = 1;
295 port = &ibdev->port[ib_port_num - 1];
296 spin_lock(&port->mp.mpi_lock);
297 mpi = ibdev->port[ib_port_num - 1].mp.mpi;
298 if (mpi && !mpi->unaffiliate) {
300 /* If it's the master no need to refcount, it'll exist
301 * as long as the ib_dev exists.
306 spin_unlock(&port->mp.mpi_lock);
311 void mlx5_ib_put_native_port_mdev(struct mlx5_ib_dev *ibdev, u32 port_num)
313 enum rdma_link_layer ll = mlx5_ib_port_link_layer(&ibdev->ib_dev,
315 struct mlx5_ib_multiport_info *mpi;
316 struct mlx5_ib_port *port;
318 if (!mlx5_core_mp_enabled(ibdev->mdev) || ll != IB_LINK_LAYER_ETHERNET)
321 port = &ibdev->port[port_num - 1];
323 spin_lock(&port->mp.mpi_lock);
324 mpi = ibdev->port[port_num - 1].mp.mpi;
329 if (mpi->unaffiliate)
330 complete(&mpi->unref_comp);
332 spin_unlock(&port->mp.mpi_lock);
335 static int translate_eth_legacy_proto_oper(u32 eth_proto_oper,
336 u16 *active_speed, u8 *active_width)
338 switch (eth_proto_oper) {
339 case MLX5E_PROT_MASK(MLX5E_1000BASE_CX_SGMII):
340 case MLX5E_PROT_MASK(MLX5E_1000BASE_KX):
341 case MLX5E_PROT_MASK(MLX5E_100BASE_TX):
342 case MLX5E_PROT_MASK(MLX5E_1000BASE_T):
343 *active_width = IB_WIDTH_1X;
344 *active_speed = IB_SPEED_SDR;
346 case MLX5E_PROT_MASK(MLX5E_10GBASE_T):
347 case MLX5E_PROT_MASK(MLX5E_10GBASE_CX4):
348 case MLX5E_PROT_MASK(MLX5E_10GBASE_KX4):
349 case MLX5E_PROT_MASK(MLX5E_10GBASE_KR):
350 case MLX5E_PROT_MASK(MLX5E_10GBASE_CR):
351 case MLX5E_PROT_MASK(MLX5E_10GBASE_SR):
352 case MLX5E_PROT_MASK(MLX5E_10GBASE_ER):
353 *active_width = IB_WIDTH_1X;
354 *active_speed = IB_SPEED_QDR;
356 case MLX5E_PROT_MASK(MLX5E_25GBASE_CR):
357 case MLX5E_PROT_MASK(MLX5E_25GBASE_KR):
358 case MLX5E_PROT_MASK(MLX5E_25GBASE_SR):
359 *active_width = IB_WIDTH_1X;
360 *active_speed = IB_SPEED_EDR;
362 case MLX5E_PROT_MASK(MLX5E_40GBASE_CR4):
363 case MLX5E_PROT_MASK(MLX5E_40GBASE_KR4):
364 case MLX5E_PROT_MASK(MLX5E_40GBASE_SR4):
365 case MLX5E_PROT_MASK(MLX5E_40GBASE_LR4):
366 *active_width = IB_WIDTH_4X;
367 *active_speed = IB_SPEED_QDR;
369 case MLX5E_PROT_MASK(MLX5E_50GBASE_CR2):
370 case MLX5E_PROT_MASK(MLX5E_50GBASE_KR2):
371 case MLX5E_PROT_MASK(MLX5E_50GBASE_SR2):
372 *active_width = IB_WIDTH_1X;
373 *active_speed = IB_SPEED_HDR;
375 case MLX5E_PROT_MASK(MLX5E_56GBASE_R4):
376 *active_width = IB_WIDTH_4X;
377 *active_speed = IB_SPEED_FDR;
379 case MLX5E_PROT_MASK(MLX5E_100GBASE_CR4):
380 case MLX5E_PROT_MASK(MLX5E_100GBASE_SR4):
381 case MLX5E_PROT_MASK(MLX5E_100GBASE_KR4):
382 case MLX5E_PROT_MASK(MLX5E_100GBASE_LR4):
383 *active_width = IB_WIDTH_4X;
384 *active_speed = IB_SPEED_EDR;
393 static int translate_eth_ext_proto_oper(u32 eth_proto_oper, u16 *active_speed,
396 switch (eth_proto_oper) {
397 case MLX5E_PROT_MASK(MLX5E_SGMII_100M):
398 case MLX5E_PROT_MASK(MLX5E_1000BASE_X_SGMII):
399 *active_width = IB_WIDTH_1X;
400 *active_speed = IB_SPEED_SDR;
402 case MLX5E_PROT_MASK(MLX5E_5GBASE_R):
403 *active_width = IB_WIDTH_1X;
404 *active_speed = IB_SPEED_DDR;
406 case MLX5E_PROT_MASK(MLX5E_10GBASE_XFI_XAUI_1):
407 *active_width = IB_WIDTH_1X;
408 *active_speed = IB_SPEED_QDR;
410 case MLX5E_PROT_MASK(MLX5E_40GBASE_XLAUI_4_XLPPI_4):
411 *active_width = IB_WIDTH_4X;
412 *active_speed = IB_SPEED_QDR;
414 case MLX5E_PROT_MASK(MLX5E_25GAUI_1_25GBASE_CR_KR):
415 *active_width = IB_WIDTH_1X;
416 *active_speed = IB_SPEED_EDR;
418 case MLX5E_PROT_MASK(MLX5E_50GAUI_2_LAUI_2_50GBASE_CR2_KR2):
419 *active_width = IB_WIDTH_2X;
420 *active_speed = IB_SPEED_EDR;
422 case MLX5E_PROT_MASK(MLX5E_50GAUI_1_LAUI_1_50GBASE_CR_KR):
423 *active_width = IB_WIDTH_1X;
424 *active_speed = IB_SPEED_HDR;
426 case MLX5E_PROT_MASK(MLX5E_CAUI_4_100GBASE_CR4_KR4):
427 *active_width = IB_WIDTH_4X;
428 *active_speed = IB_SPEED_EDR;
430 case MLX5E_PROT_MASK(MLX5E_100GAUI_2_100GBASE_CR2_KR2):
431 *active_width = IB_WIDTH_2X;
432 *active_speed = IB_SPEED_HDR;
434 case MLX5E_PROT_MASK(MLX5E_100GAUI_1_100GBASE_CR_KR):
435 *active_width = IB_WIDTH_1X;
436 *active_speed = IB_SPEED_NDR;
438 case MLX5E_PROT_MASK(MLX5E_200GAUI_4_200GBASE_CR4_KR4):
439 *active_width = IB_WIDTH_4X;
440 *active_speed = IB_SPEED_HDR;
442 case MLX5E_PROT_MASK(MLX5E_200GAUI_2_200GBASE_CR2_KR2):
443 *active_width = IB_WIDTH_2X;
444 *active_speed = IB_SPEED_NDR;
446 case MLX5E_PROT_MASK(MLX5E_400GAUI_8):
447 *active_width = IB_WIDTH_8X;
448 *active_speed = IB_SPEED_HDR;
450 case MLX5E_PROT_MASK(MLX5E_400GAUI_4_400GBASE_CR4_KR4):
451 *active_width = IB_WIDTH_4X;
452 *active_speed = IB_SPEED_NDR;
461 static int translate_eth_proto_oper(u32 eth_proto_oper, u16 *active_speed,
462 u8 *active_width, bool ext)
465 translate_eth_ext_proto_oper(eth_proto_oper, active_speed,
467 translate_eth_legacy_proto_oper(eth_proto_oper, active_speed,
471 static int mlx5_query_port_roce(struct ib_device *device, u32 port_num,
472 struct ib_port_attr *props)
474 struct mlx5_ib_dev *dev = to_mdev(device);
475 u32 out[MLX5_ST_SZ_DW(ptys_reg)] = {0};
476 struct mlx5_core_dev *mdev;
477 struct net_device *ndev, *upper;
478 enum ib_mtu ndev_ib_mtu;
479 bool put_mdev = true;
485 mdev = mlx5_ib_get_native_port_mdev(dev, port_num, &mdev_port_num);
487 /* This means the port isn't affiliated yet. Get the
488 * info for the master port instead.
496 /* Possible bad flows are checked before filling out props so in case
497 * of an error it will still be zeroed out.
498 * Use native port in case of reps
501 err = mlx5_query_port_ptys(mdev, out, sizeof(out), MLX5_PTYS_EN,
504 err = mlx5_query_port_ptys(mdev, out, sizeof(out), MLX5_PTYS_EN,
508 ext = !!MLX5_GET_ETH_PROTO(ptys_reg, out, true, eth_proto_capability);
509 eth_prot_oper = MLX5_GET_ETH_PROTO(ptys_reg, out, ext, eth_proto_oper);
511 props->active_width = IB_WIDTH_4X;
512 props->active_speed = IB_SPEED_QDR;
514 translate_eth_proto_oper(eth_prot_oper, &props->active_speed,
515 &props->active_width, ext);
517 if (!dev->is_rep && dev->mdev->roce.roce_en) {
520 props->port_cap_flags |= IB_PORT_CM_SUP;
521 props->ip_gids = true;
522 props->gid_tbl_len = MLX5_CAP_ROCE(dev->mdev,
523 roce_address_table_size);
524 mlx5_query_nic_vport_qkey_viol_cntr(mdev, &qkey_viol_cntr);
525 props->qkey_viol_cntr = qkey_viol_cntr;
527 props->max_mtu = IB_MTU_4096;
528 props->max_msg_sz = 1 << MLX5_CAP_GEN(dev->mdev, log_max_msg);
529 props->pkey_tbl_len = 1;
530 props->state = IB_PORT_DOWN;
531 props->phys_state = IB_PORT_PHYS_STATE_DISABLED;
533 /* If this is a stub query for an unaffiliated port stop here */
537 ndev = mlx5_ib_get_netdev(device, port_num);
541 if (dev->lag_active) {
543 upper = netdev_master_upper_dev_get_rcu(ndev);
552 if (netif_running(ndev) && netif_carrier_ok(ndev)) {
553 props->state = IB_PORT_ACTIVE;
554 props->phys_state = IB_PORT_PHYS_STATE_LINK_UP;
557 ndev_ib_mtu = iboe_get_mtu(ndev->mtu);
561 props->active_mtu = min(props->max_mtu, ndev_ib_mtu);
564 mlx5_ib_put_native_port_mdev(dev, port_num);
568 int set_roce_addr(struct mlx5_ib_dev *dev, u32 port_num,
569 unsigned int index, const union ib_gid *gid,
570 const struct ib_gid_attr *attr)
572 enum ib_gid_type gid_type;
573 u16 vlan_id = 0xffff;
579 gid_type = attr->gid_type;
581 ret = rdma_read_gid_l2_fields(attr, &vlan_id, &mac[0]);
587 case IB_GID_TYPE_ROCE:
588 roce_version = MLX5_ROCE_VERSION_1;
590 case IB_GID_TYPE_ROCE_UDP_ENCAP:
591 roce_version = MLX5_ROCE_VERSION_2;
592 if (gid && ipv6_addr_v4mapped((void *)gid))
593 roce_l3_type = MLX5_ROCE_L3_TYPE_IPV4;
595 roce_l3_type = MLX5_ROCE_L3_TYPE_IPV6;
599 mlx5_ib_warn(dev, "Unexpected GID type %u\n", gid_type);
602 return mlx5_core_roce_gid_set(dev->mdev, index, roce_version,
603 roce_l3_type, gid->raw, mac,
604 vlan_id < VLAN_CFI_MASK, vlan_id,
608 static int mlx5_ib_add_gid(const struct ib_gid_attr *attr,
609 __always_unused void **context)
613 ret = mlx5r_add_gid_macsec_operations(attr);
617 return set_roce_addr(to_mdev(attr->device), attr->port_num,
618 attr->index, &attr->gid, attr);
621 static int mlx5_ib_del_gid(const struct ib_gid_attr *attr,
622 __always_unused void **context)
626 ret = set_roce_addr(to_mdev(attr->device), attr->port_num,
627 attr->index, NULL, attr);
631 mlx5r_del_gid_macsec_operations(attr);
635 __be16 mlx5_get_roce_udp_sport_min(const struct mlx5_ib_dev *dev,
636 const struct ib_gid_attr *attr)
638 if (attr->gid_type != IB_GID_TYPE_ROCE_UDP_ENCAP)
641 return cpu_to_be16(MLX5_CAP_ROCE(dev->mdev, r_roce_min_src_udp_port));
644 static int mlx5_use_mad_ifc(struct mlx5_ib_dev *dev)
646 if (MLX5_CAP_GEN(dev->mdev, port_type) == MLX5_CAP_PORT_TYPE_IB)
647 return !MLX5_CAP_GEN(dev->mdev, ib_virt);
652 MLX5_VPORT_ACCESS_METHOD_MAD,
653 MLX5_VPORT_ACCESS_METHOD_HCA,
654 MLX5_VPORT_ACCESS_METHOD_NIC,
657 static int mlx5_get_vport_access_method(struct ib_device *ibdev)
659 if (mlx5_use_mad_ifc(to_mdev(ibdev)))
660 return MLX5_VPORT_ACCESS_METHOD_MAD;
662 if (mlx5_ib_port_link_layer(ibdev, 1) ==
663 IB_LINK_LAYER_ETHERNET)
664 return MLX5_VPORT_ACCESS_METHOD_NIC;
666 return MLX5_VPORT_ACCESS_METHOD_HCA;
669 static void get_atomic_caps(struct mlx5_ib_dev *dev,
671 struct ib_device_attr *props)
674 u8 atomic_operations = MLX5_CAP_ATOMIC(dev->mdev, atomic_operations);
675 u8 atomic_req_8B_endianness_mode =
676 MLX5_CAP_ATOMIC(dev->mdev, atomic_req_8B_endianness_mode);
678 /* Check if HW supports 8 bytes standard atomic operations and capable
679 * of host endianness respond
681 tmp = MLX5_ATOMIC_OPS_CMP_SWAP | MLX5_ATOMIC_OPS_FETCH_ADD;
682 if (((atomic_operations & tmp) == tmp) &&
683 (atomic_size_qp & MLX5_ATOMIC_SIZE_QP_8BYTES) &&
684 (atomic_req_8B_endianness_mode)) {
685 props->atomic_cap = IB_ATOMIC_HCA;
687 props->atomic_cap = IB_ATOMIC_NONE;
691 static void get_atomic_caps_qp(struct mlx5_ib_dev *dev,
692 struct ib_device_attr *props)
694 u8 atomic_size_qp = MLX5_CAP_ATOMIC(dev->mdev, atomic_size_qp);
696 get_atomic_caps(dev, atomic_size_qp, props);
699 static int mlx5_query_system_image_guid(struct ib_device *ibdev,
700 __be64 *sys_image_guid)
702 struct mlx5_ib_dev *dev = to_mdev(ibdev);
703 struct mlx5_core_dev *mdev = dev->mdev;
707 switch (mlx5_get_vport_access_method(ibdev)) {
708 case MLX5_VPORT_ACCESS_METHOD_MAD:
709 return mlx5_query_mad_ifc_system_image_guid(ibdev,
712 case MLX5_VPORT_ACCESS_METHOD_HCA:
713 err = mlx5_query_hca_vport_system_image_guid(mdev, &tmp);
716 case MLX5_VPORT_ACCESS_METHOD_NIC:
717 err = mlx5_query_nic_vport_system_image_guid(mdev, &tmp);
725 *sys_image_guid = cpu_to_be64(tmp);
731 static int mlx5_query_max_pkeys(struct ib_device *ibdev,
734 struct mlx5_ib_dev *dev = to_mdev(ibdev);
735 struct mlx5_core_dev *mdev = dev->mdev;
737 switch (mlx5_get_vport_access_method(ibdev)) {
738 case MLX5_VPORT_ACCESS_METHOD_MAD:
739 return mlx5_query_mad_ifc_max_pkeys(ibdev, max_pkeys);
741 case MLX5_VPORT_ACCESS_METHOD_HCA:
742 case MLX5_VPORT_ACCESS_METHOD_NIC:
743 *max_pkeys = mlx5_to_sw_pkey_sz(MLX5_CAP_GEN(mdev,
752 static int mlx5_query_vendor_id(struct ib_device *ibdev,
755 struct mlx5_ib_dev *dev = to_mdev(ibdev);
757 switch (mlx5_get_vport_access_method(ibdev)) {
758 case MLX5_VPORT_ACCESS_METHOD_MAD:
759 return mlx5_query_mad_ifc_vendor_id(ibdev, vendor_id);
761 case MLX5_VPORT_ACCESS_METHOD_HCA:
762 case MLX5_VPORT_ACCESS_METHOD_NIC:
763 return mlx5_core_query_vendor_id(dev->mdev, vendor_id);
770 static int mlx5_query_node_guid(struct mlx5_ib_dev *dev,
776 switch (mlx5_get_vport_access_method(&dev->ib_dev)) {
777 case MLX5_VPORT_ACCESS_METHOD_MAD:
778 return mlx5_query_mad_ifc_node_guid(dev, node_guid);
780 case MLX5_VPORT_ACCESS_METHOD_HCA:
781 err = mlx5_query_hca_vport_node_guid(dev->mdev, &tmp);
784 case MLX5_VPORT_ACCESS_METHOD_NIC:
785 err = mlx5_query_nic_vport_node_guid(dev->mdev, &tmp);
793 *node_guid = cpu_to_be64(tmp);
798 struct mlx5_reg_node_desc {
799 u8 desc[IB_DEVICE_NODE_DESC_MAX];
802 static int mlx5_query_node_desc(struct mlx5_ib_dev *dev, char *node_desc)
804 struct mlx5_reg_node_desc in;
806 if (mlx5_use_mad_ifc(dev))
807 return mlx5_query_mad_ifc_node_desc(dev, node_desc);
809 memset(&in, 0, sizeof(in));
811 return mlx5_core_access_reg(dev->mdev, &in, sizeof(in), node_desc,
812 sizeof(struct mlx5_reg_node_desc),
813 MLX5_REG_NODE_DESC, 0, 0);
816 static int mlx5_ib_query_device(struct ib_device *ibdev,
817 struct ib_device_attr *props,
818 struct ib_udata *uhw)
820 size_t uhw_outlen = (uhw) ? uhw->outlen : 0;
821 struct mlx5_ib_dev *dev = to_mdev(ibdev);
822 struct mlx5_core_dev *mdev = dev->mdev;
827 u64 min_page_size = 1ull << MLX5_CAP_GEN(mdev, log_pg_sz);
828 bool raw_support = !mlx5_core_mp_enabled(mdev);
829 struct mlx5_ib_query_device_resp resp = {};
833 resp_len = sizeof(resp.comp_mask) + sizeof(resp.response_length);
834 if (uhw_outlen && uhw_outlen < resp_len)
837 resp.response_length = resp_len;
839 if (uhw && uhw->inlen && !ib_is_udata_cleared(uhw, 0, uhw->inlen))
842 memset(props, 0, sizeof(*props));
843 err = mlx5_query_system_image_guid(ibdev,
844 &props->sys_image_guid);
848 props->max_pkeys = dev->pkey_table_len;
850 err = mlx5_query_vendor_id(ibdev, &props->vendor_id);
854 props->fw_ver = ((u64)fw_rev_maj(dev->mdev) << 32) |
855 (fw_rev_min(dev->mdev) << 16) |
856 fw_rev_sub(dev->mdev);
857 props->device_cap_flags = IB_DEVICE_CHANGE_PHY_PORT |
858 IB_DEVICE_PORT_ACTIVE_EVENT |
859 IB_DEVICE_SYS_IMAGE_GUID |
860 IB_DEVICE_RC_RNR_NAK_GEN;
862 if (MLX5_CAP_GEN(mdev, pkv))
863 props->device_cap_flags |= IB_DEVICE_BAD_PKEY_CNTR;
864 if (MLX5_CAP_GEN(mdev, qkv))
865 props->device_cap_flags |= IB_DEVICE_BAD_QKEY_CNTR;
866 if (MLX5_CAP_GEN(mdev, apm))
867 props->device_cap_flags |= IB_DEVICE_AUTO_PATH_MIG;
868 if (MLX5_CAP_GEN(mdev, xrc))
869 props->device_cap_flags |= IB_DEVICE_XRC;
870 if (MLX5_CAP_GEN(mdev, imaicl)) {
871 props->device_cap_flags |= IB_DEVICE_MEM_WINDOW |
872 IB_DEVICE_MEM_WINDOW_TYPE_2B;
873 props->max_mw = 1 << MLX5_CAP_GEN(mdev, log_max_mkey);
874 /* We support 'Gappy' memory registration too */
875 props->kernel_cap_flags |= IBK_SG_GAPS_REG;
877 /* IB_WR_REG_MR always requires changing the entity size with UMR */
878 if (!MLX5_CAP_GEN(dev->mdev, umr_modify_entity_size_disabled))
879 props->device_cap_flags |= IB_DEVICE_MEM_MGT_EXTENSIONS;
880 if (MLX5_CAP_GEN(mdev, sho)) {
881 props->kernel_cap_flags |= IBK_INTEGRITY_HANDOVER;
882 /* At this stage no support for signature handover */
883 props->sig_prot_cap = IB_PROT_T10DIF_TYPE_1 |
884 IB_PROT_T10DIF_TYPE_2 |
885 IB_PROT_T10DIF_TYPE_3;
886 props->sig_guard_cap = IB_GUARD_T10DIF_CRC |
887 IB_GUARD_T10DIF_CSUM;
889 if (MLX5_CAP_GEN(mdev, block_lb_mc))
890 props->kernel_cap_flags |= IBK_BLOCK_MULTICAST_LOOPBACK;
892 if (MLX5_CAP_GEN(dev->mdev, eth_net_offloads) && raw_support) {
893 if (MLX5_CAP_ETH(mdev, csum_cap)) {
894 /* Legacy bit to support old userspace libraries */
895 props->device_cap_flags |= IB_DEVICE_RAW_IP_CSUM;
896 props->raw_packet_caps |= IB_RAW_PACKET_CAP_IP_CSUM;
899 if (MLX5_CAP_ETH(dev->mdev, vlan_cap))
900 props->raw_packet_caps |=
901 IB_RAW_PACKET_CAP_CVLAN_STRIPPING;
903 if (offsetofend(typeof(resp), tso_caps) <= uhw_outlen) {
904 max_tso = MLX5_CAP_ETH(mdev, max_lso_cap);
906 resp.tso_caps.max_tso = 1 << max_tso;
907 resp.tso_caps.supported_qpts |=
908 1 << IB_QPT_RAW_PACKET;
909 resp.response_length += sizeof(resp.tso_caps);
913 if (offsetofend(typeof(resp), rss_caps) <= uhw_outlen) {
914 resp.rss_caps.rx_hash_function =
915 MLX5_RX_HASH_FUNC_TOEPLITZ;
916 resp.rss_caps.rx_hash_fields_mask =
917 MLX5_RX_HASH_SRC_IPV4 |
918 MLX5_RX_HASH_DST_IPV4 |
919 MLX5_RX_HASH_SRC_IPV6 |
920 MLX5_RX_HASH_DST_IPV6 |
921 MLX5_RX_HASH_SRC_PORT_TCP |
922 MLX5_RX_HASH_DST_PORT_TCP |
923 MLX5_RX_HASH_SRC_PORT_UDP |
924 MLX5_RX_HASH_DST_PORT_UDP |
926 resp.response_length += sizeof(resp.rss_caps);
929 if (offsetofend(typeof(resp), tso_caps) <= uhw_outlen)
930 resp.response_length += sizeof(resp.tso_caps);
931 if (offsetofend(typeof(resp), rss_caps) <= uhw_outlen)
932 resp.response_length += sizeof(resp.rss_caps);
935 if (MLX5_CAP_GEN(mdev, ipoib_basic_offloads)) {
936 props->device_cap_flags |= IB_DEVICE_UD_IP_CSUM;
937 props->kernel_cap_flags |= IBK_UD_TSO;
940 if (MLX5_CAP_GEN(dev->mdev, rq_delay_drop) &&
941 MLX5_CAP_GEN(dev->mdev, general_notification_event) &&
943 props->raw_packet_caps |= IB_RAW_PACKET_CAP_DELAY_DROP;
945 if (MLX5_CAP_GEN(mdev, ipoib_enhanced_offloads) &&
946 MLX5_CAP_IPOIB_ENHANCED(mdev, csum_cap))
947 props->device_cap_flags |= IB_DEVICE_UD_IP_CSUM;
949 if (MLX5_CAP_GEN(dev->mdev, eth_net_offloads) &&
950 MLX5_CAP_ETH(dev->mdev, scatter_fcs) &&
952 /* Legacy bit to support old userspace libraries */
953 props->device_cap_flags |= IB_DEVICE_RAW_SCATTER_FCS;
954 props->raw_packet_caps |= IB_RAW_PACKET_CAP_SCATTER_FCS;
957 if (MLX5_CAP_DEV_MEM(mdev, memic)) {
959 MLX5_CAP_DEV_MEM(mdev, max_memic_size);
962 if (mlx5_get_flow_namespace(dev->mdev, MLX5_FLOW_NAMESPACE_BYPASS))
963 props->device_cap_flags |= IB_DEVICE_MANAGED_FLOW_STEERING;
965 if (MLX5_CAP_GEN(mdev, end_pad))
966 props->device_cap_flags |= IB_DEVICE_PCI_WRITE_END_PADDING;
968 props->vendor_part_id = mdev->pdev->device;
969 props->hw_ver = mdev->pdev->revision;
971 props->max_mr_size = ~0ull;
972 props->page_size_cap = ~(min_page_size - 1);
973 props->max_qp = 1 << MLX5_CAP_GEN(mdev, log_max_qp);
974 props->max_qp_wr = 1 << MLX5_CAP_GEN(mdev, log_max_qp_sz);
975 max_rq_sg = MLX5_CAP_GEN(mdev, max_wqe_sz_rq) /
976 sizeof(struct mlx5_wqe_data_seg);
977 max_sq_desc = min_t(int, MLX5_CAP_GEN(mdev, max_wqe_sz_sq), 512);
978 max_sq_sg = (max_sq_desc - sizeof(struct mlx5_wqe_ctrl_seg) -
979 sizeof(struct mlx5_wqe_raddr_seg)) /
980 sizeof(struct mlx5_wqe_data_seg);
981 props->max_send_sge = max_sq_sg;
982 props->max_recv_sge = max_rq_sg;
983 props->max_sge_rd = MLX5_MAX_SGE_RD;
984 props->max_cq = 1 << MLX5_CAP_GEN(mdev, log_max_cq);
985 props->max_cqe = (1 << MLX5_CAP_GEN(mdev, log_max_cq_sz)) - 1;
986 props->max_mr = 1 << MLX5_CAP_GEN(mdev, log_max_mkey);
987 props->max_pd = 1 << MLX5_CAP_GEN(mdev, log_max_pd);
988 props->max_qp_rd_atom = 1 << MLX5_CAP_GEN(mdev, log_max_ra_req_qp);
989 props->max_qp_init_rd_atom = 1 << MLX5_CAP_GEN(mdev, log_max_ra_res_qp);
990 props->max_srq = 1 << MLX5_CAP_GEN(mdev, log_max_srq);
991 props->max_srq_wr = (1 << MLX5_CAP_GEN(mdev, log_max_srq_sz)) - 1;
992 props->local_ca_ack_delay = MLX5_CAP_GEN(mdev, local_ca_ack_delay);
993 props->max_res_rd_atom = props->max_qp_rd_atom * props->max_qp;
994 props->max_srq_sge = max_rq_sg - 1;
995 props->max_fast_reg_page_list_len =
996 1 << MLX5_CAP_GEN(mdev, log_max_klm_list_size);
997 props->max_pi_fast_reg_page_list_len =
998 props->max_fast_reg_page_list_len / 2;
1000 MLX5_CAP_GEN(mdev, max_sgl_for_optimized_performance);
1001 get_atomic_caps_qp(dev, props);
1002 props->masked_atomic_cap = IB_ATOMIC_NONE;
1003 props->max_mcast_grp = 1 << MLX5_CAP_GEN(mdev, log_max_mcg);
1004 props->max_mcast_qp_attach = MLX5_CAP_GEN(mdev, max_qp_mcg);
1005 props->max_total_mcast_qp_attach = props->max_mcast_qp_attach *
1006 props->max_mcast_grp;
1007 props->max_ah = INT_MAX;
1008 props->hca_core_clock = MLX5_CAP_GEN(mdev, device_frequency_khz);
1009 props->timestamp_mask = 0x7FFFFFFFFFFFFFFFULL;
1011 if (IS_ENABLED(CONFIG_INFINIBAND_ON_DEMAND_PAGING)) {
1012 if (dev->odp_caps.general_caps & IB_ODP_SUPPORT)
1013 props->kernel_cap_flags |= IBK_ON_DEMAND_PAGING;
1014 props->odp_caps = dev->odp_caps;
1016 /* ODP for kernel QPs is not implemented for receive
1019 props->odp_caps.per_transport_caps.rc_odp_caps &=
1020 ~(IB_ODP_SUPPORT_READ |
1021 IB_ODP_SUPPORT_SRQ_RECV);
1022 props->odp_caps.per_transport_caps.uc_odp_caps &=
1023 ~(IB_ODP_SUPPORT_READ |
1024 IB_ODP_SUPPORT_SRQ_RECV);
1025 props->odp_caps.per_transport_caps.ud_odp_caps &=
1026 ~(IB_ODP_SUPPORT_READ |
1027 IB_ODP_SUPPORT_SRQ_RECV);
1028 props->odp_caps.per_transport_caps.xrc_odp_caps &=
1029 ~(IB_ODP_SUPPORT_READ |
1030 IB_ODP_SUPPORT_SRQ_RECV);
1034 if (mlx5_core_is_vf(mdev))
1035 props->kernel_cap_flags |= IBK_VIRTUAL_FUNCTION;
1037 if (mlx5_ib_port_link_layer(ibdev, 1) ==
1038 IB_LINK_LAYER_ETHERNET && raw_support) {
1039 props->rss_caps.max_rwq_indirection_tables =
1040 1 << MLX5_CAP_GEN(dev->mdev, log_max_rqt);
1041 props->rss_caps.max_rwq_indirection_table_size =
1042 1 << MLX5_CAP_GEN(dev->mdev, log_max_rqt_size);
1043 props->rss_caps.supported_qpts = 1 << IB_QPT_RAW_PACKET;
1044 props->max_wq_type_rq =
1045 1 << MLX5_CAP_GEN(dev->mdev, log_max_rq);
1048 if (MLX5_CAP_GEN(mdev, tag_matching)) {
1049 props->tm_caps.max_num_tags =
1050 (1 << MLX5_CAP_GEN(mdev, log_tag_matching_list_sz)) - 1;
1051 props->tm_caps.max_ops =
1052 1 << MLX5_CAP_GEN(mdev, log_max_qp_sz);
1053 props->tm_caps.max_sge = MLX5_TM_MAX_SGE;
1056 if (MLX5_CAP_GEN(mdev, tag_matching) &&
1057 MLX5_CAP_GEN(mdev, rndv_offload_rc)) {
1058 props->tm_caps.flags = IB_TM_CAP_RNDV_RC;
1059 props->tm_caps.max_rndv_hdr_size = MLX5_TM_MAX_RNDV_MSG_SIZE;
1062 if (MLX5_CAP_GEN(dev->mdev, cq_moderation)) {
1063 props->cq_caps.max_cq_moderation_count =
1065 props->cq_caps.max_cq_moderation_period =
1069 if (offsetofend(typeof(resp), cqe_comp_caps) <= uhw_outlen) {
1070 resp.response_length += sizeof(resp.cqe_comp_caps);
1072 if (MLX5_CAP_GEN(dev->mdev, cqe_compression)) {
1073 resp.cqe_comp_caps.max_num =
1074 MLX5_CAP_GEN(dev->mdev,
1075 cqe_compression_max_num);
1077 resp.cqe_comp_caps.supported_format =
1078 MLX5_IB_CQE_RES_FORMAT_HASH |
1079 MLX5_IB_CQE_RES_FORMAT_CSUM;
1081 if (MLX5_CAP_GEN(dev->mdev, mini_cqe_resp_stride_index))
1082 resp.cqe_comp_caps.supported_format |=
1083 MLX5_IB_CQE_RES_FORMAT_CSUM_STRIDX;
1087 if (offsetofend(typeof(resp), packet_pacing_caps) <= uhw_outlen &&
1089 if (MLX5_CAP_QOS(mdev, packet_pacing) &&
1090 MLX5_CAP_GEN(mdev, qos)) {
1091 resp.packet_pacing_caps.qp_rate_limit_max =
1092 MLX5_CAP_QOS(mdev, packet_pacing_max_rate);
1093 resp.packet_pacing_caps.qp_rate_limit_min =
1094 MLX5_CAP_QOS(mdev, packet_pacing_min_rate);
1095 resp.packet_pacing_caps.supported_qpts |=
1096 1 << IB_QPT_RAW_PACKET;
1097 if (MLX5_CAP_QOS(mdev, packet_pacing_burst_bound) &&
1098 MLX5_CAP_QOS(mdev, packet_pacing_typical_size))
1099 resp.packet_pacing_caps.cap_flags |=
1100 MLX5_IB_PP_SUPPORT_BURST;
1102 resp.response_length += sizeof(resp.packet_pacing_caps);
1105 if (offsetofend(typeof(resp), mlx5_ib_support_multi_pkt_send_wqes) <=
1107 if (MLX5_CAP_ETH(mdev, multi_pkt_send_wqe))
1108 resp.mlx5_ib_support_multi_pkt_send_wqes =
1111 if (MLX5_CAP_ETH(mdev, enhanced_multi_pkt_send_wqe))
1112 resp.mlx5_ib_support_multi_pkt_send_wqes |=
1113 MLX5_IB_SUPPORT_EMPW;
1115 resp.response_length +=
1116 sizeof(resp.mlx5_ib_support_multi_pkt_send_wqes);
1119 if (offsetofend(typeof(resp), flags) <= uhw_outlen) {
1120 resp.response_length += sizeof(resp.flags);
1122 if (MLX5_CAP_GEN(mdev, cqe_compression_128))
1124 MLX5_IB_QUERY_DEV_RESP_FLAGS_CQE_128B_COMP;
1126 if (MLX5_CAP_GEN(mdev, cqe_128_always))
1127 resp.flags |= MLX5_IB_QUERY_DEV_RESP_FLAGS_CQE_128B_PAD;
1128 if (MLX5_CAP_GEN(mdev, qp_packet_based))
1130 MLX5_IB_QUERY_DEV_RESP_PACKET_BASED_CREDIT_MODE;
1132 resp.flags |= MLX5_IB_QUERY_DEV_RESP_FLAGS_SCAT2CQE_DCT;
1135 if (offsetofend(typeof(resp), sw_parsing_caps) <= uhw_outlen) {
1136 resp.response_length += sizeof(resp.sw_parsing_caps);
1137 if (MLX5_CAP_ETH(mdev, swp)) {
1138 resp.sw_parsing_caps.sw_parsing_offloads |=
1141 if (MLX5_CAP_ETH(mdev, swp_csum))
1142 resp.sw_parsing_caps.sw_parsing_offloads |=
1143 MLX5_IB_SW_PARSING_CSUM;
1145 if (MLX5_CAP_ETH(mdev, swp_lso))
1146 resp.sw_parsing_caps.sw_parsing_offloads |=
1147 MLX5_IB_SW_PARSING_LSO;
1149 if (resp.sw_parsing_caps.sw_parsing_offloads)
1150 resp.sw_parsing_caps.supported_qpts =
1151 BIT(IB_QPT_RAW_PACKET);
1155 if (offsetofend(typeof(resp), striding_rq_caps) <= uhw_outlen &&
1157 resp.response_length += sizeof(resp.striding_rq_caps);
1158 if (MLX5_CAP_GEN(mdev, striding_rq)) {
1159 resp.striding_rq_caps.min_single_stride_log_num_of_bytes =
1160 MLX5_MIN_SINGLE_STRIDE_LOG_NUM_BYTES;
1161 resp.striding_rq_caps.max_single_stride_log_num_of_bytes =
1162 MLX5_MAX_SINGLE_STRIDE_LOG_NUM_BYTES;
1163 if (MLX5_CAP_GEN(dev->mdev, ext_stride_num_range))
1164 resp.striding_rq_caps
1165 .min_single_wqe_log_num_of_strides =
1166 MLX5_EXT_MIN_SINGLE_WQE_LOG_NUM_STRIDES;
1168 resp.striding_rq_caps
1169 .min_single_wqe_log_num_of_strides =
1170 MLX5_MIN_SINGLE_WQE_LOG_NUM_STRIDES;
1171 resp.striding_rq_caps.max_single_wqe_log_num_of_strides =
1172 MLX5_MAX_SINGLE_WQE_LOG_NUM_STRIDES;
1173 resp.striding_rq_caps.supported_qpts =
1174 BIT(IB_QPT_RAW_PACKET);
1178 if (offsetofend(typeof(resp), tunnel_offloads_caps) <= uhw_outlen) {
1179 resp.response_length += sizeof(resp.tunnel_offloads_caps);
1180 if (MLX5_CAP_ETH(mdev, tunnel_stateless_vxlan))
1181 resp.tunnel_offloads_caps |=
1182 MLX5_IB_TUNNELED_OFFLOADS_VXLAN;
1183 if (MLX5_CAP_ETH(mdev, tunnel_stateless_geneve_rx))
1184 resp.tunnel_offloads_caps |=
1185 MLX5_IB_TUNNELED_OFFLOADS_GENEVE;
1186 if (MLX5_CAP_ETH(mdev, tunnel_stateless_gre))
1187 resp.tunnel_offloads_caps |=
1188 MLX5_IB_TUNNELED_OFFLOADS_GRE;
1189 if (MLX5_CAP_ETH(mdev, tunnel_stateless_mpls_over_gre))
1190 resp.tunnel_offloads_caps |=
1191 MLX5_IB_TUNNELED_OFFLOADS_MPLS_GRE;
1192 if (MLX5_CAP_ETH(mdev, tunnel_stateless_mpls_over_udp))
1193 resp.tunnel_offloads_caps |=
1194 MLX5_IB_TUNNELED_OFFLOADS_MPLS_UDP;
1197 if (offsetofend(typeof(resp), dci_streams_caps) <= uhw_outlen) {
1198 resp.response_length += sizeof(resp.dci_streams_caps);
1200 resp.dci_streams_caps.max_log_num_concurent =
1201 MLX5_CAP_GEN(mdev, log_max_dci_stream_channels);
1203 resp.dci_streams_caps.max_log_num_errored =
1204 MLX5_CAP_GEN(mdev, log_max_dci_errored_streams);
1208 err = ib_copy_to_udata(uhw, &resp, resp.response_length);
1217 static void translate_active_width(struct ib_device *ibdev, u16 active_width,
1220 struct mlx5_ib_dev *dev = to_mdev(ibdev);
1222 if (active_width & MLX5_PTYS_WIDTH_1X)
1223 *ib_width = IB_WIDTH_1X;
1224 else if (active_width & MLX5_PTYS_WIDTH_2X)
1225 *ib_width = IB_WIDTH_2X;
1226 else if (active_width & MLX5_PTYS_WIDTH_4X)
1227 *ib_width = IB_WIDTH_4X;
1228 else if (active_width & MLX5_PTYS_WIDTH_8X)
1229 *ib_width = IB_WIDTH_8X;
1230 else if (active_width & MLX5_PTYS_WIDTH_12X)
1231 *ib_width = IB_WIDTH_12X;
1233 mlx5_ib_dbg(dev, "Invalid active_width %d, setting width to default value: 4x\n",
1235 *ib_width = IB_WIDTH_4X;
1241 static int mlx5_mtu_to_ib_mtu(int mtu)
1246 case 1024: return 3;
1247 case 2048: return 4;
1248 case 4096: return 5;
1250 pr_warn("invalid mtu\n");
1255 enum ib_max_vl_num {
1257 __IB_MAX_VL_0_1 = 2,
1258 __IB_MAX_VL_0_3 = 3,
1259 __IB_MAX_VL_0_7 = 4,
1260 __IB_MAX_VL_0_14 = 5,
1263 enum mlx5_vl_hw_cap {
1272 MLX5_VL_HW_0_14 = 15
1275 static int translate_max_vl_num(struct ib_device *ibdev, u8 vl_hw_cap,
1278 switch (vl_hw_cap) {
1280 *max_vl_num = __IB_MAX_VL_0;
1282 case MLX5_VL_HW_0_1:
1283 *max_vl_num = __IB_MAX_VL_0_1;
1285 case MLX5_VL_HW_0_3:
1286 *max_vl_num = __IB_MAX_VL_0_3;
1288 case MLX5_VL_HW_0_7:
1289 *max_vl_num = __IB_MAX_VL_0_7;
1291 case MLX5_VL_HW_0_14:
1292 *max_vl_num = __IB_MAX_VL_0_14;
1302 static int mlx5_query_hca_port(struct ib_device *ibdev, u32 port,
1303 struct ib_port_attr *props)
1305 struct mlx5_ib_dev *dev = to_mdev(ibdev);
1306 struct mlx5_core_dev *mdev = dev->mdev;
1307 struct mlx5_hca_vport_context *rep;
1311 u16 ib_link_width_oper;
1314 rep = kzalloc(sizeof(*rep), GFP_KERNEL);
1320 /* props being zeroed by the caller, avoid zeroing it here */
1322 err = mlx5_query_hca_vport_context(mdev, 0, port, 0, rep);
1326 props->lid = rep->lid;
1327 props->lmc = rep->lmc;
1328 props->sm_lid = rep->sm_lid;
1329 props->sm_sl = rep->sm_sl;
1330 props->state = rep->vport_state;
1331 props->phys_state = rep->port_physical_state;
1332 props->port_cap_flags = rep->cap_mask1;
1333 props->gid_tbl_len = mlx5_get_gid_table_len(MLX5_CAP_GEN(mdev, gid_table_size));
1334 props->max_msg_sz = 1 << MLX5_CAP_GEN(mdev, log_max_msg);
1335 props->pkey_tbl_len = mlx5_to_sw_pkey_sz(MLX5_CAP_GEN(mdev, pkey_table_size));
1336 props->bad_pkey_cntr = rep->pkey_violation_counter;
1337 props->qkey_viol_cntr = rep->qkey_violation_counter;
1338 props->subnet_timeout = rep->subnet_timeout;
1339 props->init_type_reply = rep->init_type_reply;
1341 if (props->port_cap_flags & IB_PORT_CAP_MASK2_SUP)
1342 props->port_cap_flags2 = rep->cap_mask2;
1344 err = mlx5_query_ib_port_oper(mdev, &ib_link_width_oper,
1345 &props->active_speed, port);
1349 translate_active_width(ibdev, ib_link_width_oper, &props->active_width);
1351 mlx5_query_port_max_mtu(mdev, &max_mtu, port);
1353 props->max_mtu = mlx5_mtu_to_ib_mtu(max_mtu);
1355 mlx5_query_port_oper_mtu(mdev, &oper_mtu, port);
1357 props->active_mtu = mlx5_mtu_to_ib_mtu(oper_mtu);
1359 err = mlx5_query_port_vl_hw_cap(mdev, &vl_hw_cap, port);
1363 err = translate_max_vl_num(ibdev, vl_hw_cap,
1364 &props->max_vl_num);
1370 int mlx5_ib_query_port(struct ib_device *ibdev, u32 port,
1371 struct ib_port_attr *props)
1376 switch (mlx5_get_vport_access_method(ibdev)) {
1377 case MLX5_VPORT_ACCESS_METHOD_MAD:
1378 ret = mlx5_query_mad_ifc_port(ibdev, port, props);
1381 case MLX5_VPORT_ACCESS_METHOD_HCA:
1382 ret = mlx5_query_hca_port(ibdev, port, props);
1385 case MLX5_VPORT_ACCESS_METHOD_NIC:
1386 ret = mlx5_query_port_roce(ibdev, port, props);
1393 if (!ret && props) {
1394 struct mlx5_ib_dev *dev = to_mdev(ibdev);
1395 struct mlx5_core_dev *mdev;
1396 bool put_mdev = true;
1398 mdev = mlx5_ib_get_native_port_mdev(dev, port, NULL);
1400 /* If the port isn't affiliated yet query the master.
1401 * The master and slave will have the same values.
1407 count = mlx5_core_reserved_gids_count(mdev);
1409 mlx5_ib_put_native_port_mdev(dev, port);
1410 props->gid_tbl_len -= count;
1415 static int mlx5_ib_rep_query_port(struct ib_device *ibdev, u32 port,
1416 struct ib_port_attr *props)
1418 return mlx5_query_port_roce(ibdev, port, props);
1421 static int mlx5_ib_rep_query_pkey(struct ib_device *ibdev, u32 port, u16 index,
1424 /* Default special Pkey for representor device port as per the
1425 * IB specification 1.3 section 10.9.1.2.
1431 static int mlx5_ib_query_gid(struct ib_device *ibdev, u32 port, int index,
1434 struct mlx5_ib_dev *dev = to_mdev(ibdev);
1435 struct mlx5_core_dev *mdev = dev->mdev;
1437 switch (mlx5_get_vport_access_method(ibdev)) {
1438 case MLX5_VPORT_ACCESS_METHOD_MAD:
1439 return mlx5_query_mad_ifc_gids(ibdev, port, index, gid);
1441 case MLX5_VPORT_ACCESS_METHOD_HCA:
1442 return mlx5_query_hca_vport_gid(mdev, 0, port, 0, index, gid);
1450 static int mlx5_query_hca_nic_pkey(struct ib_device *ibdev, u32 port,
1451 u16 index, u16 *pkey)
1453 struct mlx5_ib_dev *dev = to_mdev(ibdev);
1454 struct mlx5_core_dev *mdev;
1455 bool put_mdev = true;
1459 mdev = mlx5_ib_get_native_port_mdev(dev, port, &mdev_port_num);
1461 /* The port isn't affiliated yet, get the PKey from the master
1462 * port. For RoCE the PKey tables will be the same.
1469 err = mlx5_query_hca_vport_pkey(mdev, 0, mdev_port_num, 0,
1472 mlx5_ib_put_native_port_mdev(dev, port);
1477 static int mlx5_ib_query_pkey(struct ib_device *ibdev, u32 port, u16 index,
1480 switch (mlx5_get_vport_access_method(ibdev)) {
1481 case MLX5_VPORT_ACCESS_METHOD_MAD:
1482 return mlx5_query_mad_ifc_pkey(ibdev, port, index, pkey);
1484 case MLX5_VPORT_ACCESS_METHOD_HCA:
1485 case MLX5_VPORT_ACCESS_METHOD_NIC:
1486 return mlx5_query_hca_nic_pkey(ibdev, port, index, pkey);
1492 static int mlx5_ib_modify_device(struct ib_device *ibdev, int mask,
1493 struct ib_device_modify *props)
1495 struct mlx5_ib_dev *dev = to_mdev(ibdev);
1496 struct mlx5_reg_node_desc in;
1497 struct mlx5_reg_node_desc out;
1500 if (mask & ~IB_DEVICE_MODIFY_NODE_DESC)
1503 if (!(mask & IB_DEVICE_MODIFY_NODE_DESC))
1507 * If possible, pass node desc to FW, so it can generate
1508 * a 144 trap. If cmd fails, just ignore.
1510 memcpy(&in, props->node_desc, IB_DEVICE_NODE_DESC_MAX);
1511 err = mlx5_core_access_reg(dev->mdev, &in, sizeof(in), &out,
1512 sizeof(out), MLX5_REG_NODE_DESC, 0, 1);
1516 memcpy(ibdev->node_desc, props->node_desc, IB_DEVICE_NODE_DESC_MAX);
1521 static int set_port_caps_atomic(struct mlx5_ib_dev *dev, u32 port_num, u32 mask,
1524 struct mlx5_hca_vport_context ctx = {};
1525 struct mlx5_core_dev *mdev;
1529 mdev = mlx5_ib_get_native_port_mdev(dev, port_num, &mdev_port_num);
1533 err = mlx5_query_hca_vport_context(mdev, 0, mdev_port_num, 0, &ctx);
1537 if (~ctx.cap_mask1_perm & mask) {
1538 mlx5_ib_warn(dev, "trying to change bitmask 0x%X but change supported 0x%X\n",
1539 mask, ctx.cap_mask1_perm);
1544 ctx.cap_mask1 = value;
1545 ctx.cap_mask1_perm = mask;
1546 err = mlx5_core_modify_hca_vport_context(mdev, 0, mdev_port_num,
1550 mlx5_ib_put_native_port_mdev(dev, port_num);
1555 static int mlx5_ib_modify_port(struct ib_device *ibdev, u32 port, int mask,
1556 struct ib_port_modify *props)
1558 struct mlx5_ib_dev *dev = to_mdev(ibdev);
1559 struct ib_port_attr attr;
1564 bool is_ib = (mlx5_ib_port_link_layer(ibdev, port) ==
1565 IB_LINK_LAYER_INFINIBAND);
1567 /* CM layer calls ib_modify_port() regardless of the link layer. For
1568 * Ethernet ports, qkey violation and Port capabilities are meaningless.
1573 if (MLX5_CAP_GEN(dev->mdev, ib_virt) && is_ib) {
1574 change_mask = props->clr_port_cap_mask | props->set_port_cap_mask;
1575 value = ~props->clr_port_cap_mask | props->set_port_cap_mask;
1576 return set_port_caps_atomic(dev, port, change_mask, value);
1579 mutex_lock(&dev->cap_mask_mutex);
1581 err = ib_query_port(ibdev, port, &attr);
1585 tmp = (attr.port_cap_flags | props->set_port_cap_mask) &
1586 ~props->clr_port_cap_mask;
1588 err = mlx5_set_port_caps(dev->mdev, port, tmp);
1591 mutex_unlock(&dev->cap_mask_mutex);
1595 static void print_lib_caps(struct mlx5_ib_dev *dev, u64 caps)
1597 mlx5_ib_dbg(dev, "MLX5_LIB_CAP_4K_UAR = %s\n",
1598 caps & MLX5_LIB_CAP_4K_UAR ? "y" : "n");
1601 static u16 calc_dynamic_bfregs(int uars_per_sys_page)
1603 /* Large page with non 4k uar support might limit the dynamic size */
1604 if (uars_per_sys_page == 1 && PAGE_SIZE > 4096)
1605 return MLX5_MIN_DYN_BFREGS;
1607 return MLX5_MAX_DYN_BFREGS;
1610 static int calc_total_bfregs(struct mlx5_ib_dev *dev, bool lib_uar_4k,
1611 struct mlx5_ib_alloc_ucontext_req_v2 *req,
1612 struct mlx5_bfreg_info *bfregi)
1614 int uars_per_sys_page;
1615 int bfregs_per_sys_page;
1616 int ref_bfregs = req->total_num_bfregs;
1618 if (req->total_num_bfregs == 0)
1621 BUILD_BUG_ON(MLX5_MAX_BFREGS % MLX5_NON_FP_BFREGS_IN_PAGE);
1622 BUILD_BUG_ON(MLX5_MAX_BFREGS < MLX5_NON_FP_BFREGS_IN_PAGE);
1624 if (req->total_num_bfregs > MLX5_MAX_BFREGS)
1627 uars_per_sys_page = get_uars_per_sys_page(dev, lib_uar_4k);
1628 bfregs_per_sys_page = uars_per_sys_page * MLX5_NON_FP_BFREGS_PER_UAR;
1629 /* This holds the required static allocation asked by the user */
1630 req->total_num_bfregs = ALIGN(req->total_num_bfregs, bfregs_per_sys_page);
1631 if (req->num_low_latency_bfregs > req->total_num_bfregs - 1)
1634 bfregi->num_static_sys_pages = req->total_num_bfregs / bfregs_per_sys_page;
1635 bfregi->num_dyn_bfregs = ALIGN(calc_dynamic_bfregs(uars_per_sys_page), bfregs_per_sys_page);
1636 bfregi->total_num_bfregs = req->total_num_bfregs + bfregi->num_dyn_bfregs;
1637 bfregi->num_sys_pages = bfregi->total_num_bfregs / bfregs_per_sys_page;
1639 mlx5_ib_dbg(dev, "uar_4k: fw support %s, lib support %s, user requested %d bfregs, allocated %d, total bfregs %d, using %d sys pages\n",
1640 MLX5_CAP_GEN(dev->mdev, uar_4k) ? "yes" : "no",
1641 lib_uar_4k ? "yes" : "no", ref_bfregs,
1642 req->total_num_bfregs, bfregi->total_num_bfregs,
1643 bfregi->num_sys_pages);
1648 static int allocate_uars(struct mlx5_ib_dev *dev, struct mlx5_ib_ucontext *context)
1650 struct mlx5_bfreg_info *bfregi;
1654 bfregi = &context->bfregi;
1655 for (i = 0; i < bfregi->num_static_sys_pages; i++) {
1656 err = mlx5_cmd_uar_alloc(dev->mdev, &bfregi->sys_pages[i],
1661 mlx5_ib_dbg(dev, "allocated uar %d\n", bfregi->sys_pages[i]);
1664 for (i = bfregi->num_static_sys_pages; i < bfregi->num_sys_pages; i++)
1665 bfregi->sys_pages[i] = MLX5_IB_INVALID_UAR_INDEX;
1670 for (--i; i >= 0; i--)
1671 if (mlx5_cmd_uar_dealloc(dev->mdev, bfregi->sys_pages[i],
1673 mlx5_ib_warn(dev, "failed to free uar %d\n", i);
1678 static void deallocate_uars(struct mlx5_ib_dev *dev,
1679 struct mlx5_ib_ucontext *context)
1681 struct mlx5_bfreg_info *bfregi;
1684 bfregi = &context->bfregi;
1685 for (i = 0; i < bfregi->num_sys_pages; i++)
1686 if (i < bfregi->num_static_sys_pages ||
1687 bfregi->sys_pages[i] != MLX5_IB_INVALID_UAR_INDEX)
1688 mlx5_cmd_uar_dealloc(dev->mdev, bfregi->sys_pages[i],
1692 int mlx5_ib_enable_lb(struct mlx5_ib_dev *dev, bool td, bool qp)
1696 mutex_lock(&dev->lb.mutex);
1702 if (dev->lb.user_td == 2 ||
1704 if (!dev->lb.enabled) {
1705 err = mlx5_nic_vport_update_local_lb(dev->mdev, true);
1706 dev->lb.enabled = true;
1710 mutex_unlock(&dev->lb.mutex);
1715 void mlx5_ib_disable_lb(struct mlx5_ib_dev *dev, bool td, bool qp)
1717 mutex_lock(&dev->lb.mutex);
1723 if (dev->lb.user_td == 1 &&
1725 if (dev->lb.enabled) {
1726 mlx5_nic_vport_update_local_lb(dev->mdev, false);
1727 dev->lb.enabled = false;
1731 mutex_unlock(&dev->lb.mutex);
1734 static int mlx5_ib_alloc_transport_domain(struct mlx5_ib_dev *dev, u32 *tdn,
1739 if (!MLX5_CAP_GEN(dev->mdev, log_max_transport_domain))
1742 err = mlx5_cmd_alloc_transport_domain(dev->mdev, tdn, uid);
1746 if ((MLX5_CAP_GEN(dev->mdev, port_type) != MLX5_CAP_PORT_TYPE_ETH) ||
1747 (!MLX5_CAP_GEN(dev->mdev, disable_local_lb_uc) &&
1748 !MLX5_CAP_GEN(dev->mdev, disable_local_lb_mc)))
1751 return mlx5_ib_enable_lb(dev, true, false);
1754 static void mlx5_ib_dealloc_transport_domain(struct mlx5_ib_dev *dev, u32 tdn,
1757 if (!MLX5_CAP_GEN(dev->mdev, log_max_transport_domain))
1760 mlx5_cmd_dealloc_transport_domain(dev->mdev, tdn, uid);
1762 if ((MLX5_CAP_GEN(dev->mdev, port_type) != MLX5_CAP_PORT_TYPE_ETH) ||
1763 (!MLX5_CAP_GEN(dev->mdev, disable_local_lb_uc) &&
1764 !MLX5_CAP_GEN(dev->mdev, disable_local_lb_mc)))
1767 mlx5_ib_disable_lb(dev, true, false);
1770 static int set_ucontext_resp(struct ib_ucontext *uctx,
1771 struct mlx5_ib_alloc_ucontext_resp *resp)
1773 struct ib_device *ibdev = uctx->device;
1774 struct mlx5_ib_dev *dev = to_mdev(ibdev);
1775 struct mlx5_ib_ucontext *context = to_mucontext(uctx);
1776 struct mlx5_bfreg_info *bfregi = &context->bfregi;
1778 if (MLX5_CAP_GEN(dev->mdev, dump_fill_mkey)) {
1779 resp->dump_fill_mkey = dev->mkeys.dump_fill_mkey;
1781 MLX5_IB_ALLOC_UCONTEXT_RESP_MASK_DUMP_FILL_MKEY;
1784 resp->qp_tab_size = 1 << MLX5_CAP_GEN(dev->mdev, log_max_qp);
1785 if (dev->wc_support)
1786 resp->bf_reg_size = 1 << MLX5_CAP_GEN(dev->mdev,
1788 resp->cache_line_size = cache_line_size();
1789 resp->max_sq_desc_sz = MLX5_CAP_GEN(dev->mdev, max_wqe_sz_sq);
1790 resp->max_rq_desc_sz = MLX5_CAP_GEN(dev->mdev, max_wqe_sz_rq);
1791 resp->max_send_wqebb = 1 << MLX5_CAP_GEN(dev->mdev, log_max_qp_sz);
1792 resp->max_recv_wr = 1 << MLX5_CAP_GEN(dev->mdev, log_max_qp_sz);
1793 resp->max_srq_recv_wr = 1 << MLX5_CAP_GEN(dev->mdev, log_max_srq_sz);
1794 resp->cqe_version = context->cqe_version;
1795 resp->log_uar_size = MLX5_CAP_GEN(dev->mdev, uar_4k) ?
1796 MLX5_ADAPTER_PAGE_SHIFT : PAGE_SHIFT;
1797 resp->num_uars_per_page = MLX5_CAP_GEN(dev->mdev, uar_4k) ?
1798 MLX5_CAP_GEN(dev->mdev,
1799 num_of_uars_per_page) : 1;
1800 resp->tot_bfregs = bfregi->lib_uar_dyn ? 0 :
1801 bfregi->total_num_bfregs - bfregi->num_dyn_bfregs;
1802 resp->num_ports = dev->num_ports;
1803 resp->cmds_supp_uhw |= MLX5_USER_CMDS_SUPP_UHW_QUERY_DEVICE |
1804 MLX5_USER_CMDS_SUPP_UHW_CREATE_AH;
1806 if (mlx5_ib_port_link_layer(ibdev, 1) == IB_LINK_LAYER_ETHERNET) {
1807 mlx5_query_min_inline(dev->mdev, &resp->eth_min_inline);
1808 resp->eth_min_inline++;
1811 if (dev->mdev->clock_info)
1812 resp->clock_info_versions = BIT(MLX5_IB_CLOCK_INFO_V1);
1815 * We don't want to expose information from the PCI bar that is located
1816 * after 4096 bytes, so if the arch only supports larger pages, let's
1817 * pretend we don't support reading the HCA's core clock. This is also
1818 * forced by mmap function.
1820 if (PAGE_SIZE <= 4096) {
1822 MLX5_IB_ALLOC_UCONTEXT_RESP_MASK_CORE_CLOCK_OFFSET;
1823 resp->hca_core_clock_offset =
1824 offsetof(struct mlx5_init_seg,
1825 internal_timer_h) % PAGE_SIZE;
1828 if (MLX5_CAP_GEN(dev->mdev, ece_support))
1829 resp->comp_mask |= MLX5_IB_ALLOC_UCONTEXT_RESP_MASK_ECE;
1831 if (rt_supported(MLX5_CAP_GEN(dev->mdev, sq_ts_format)) &&
1832 rt_supported(MLX5_CAP_GEN(dev->mdev, rq_ts_format)) &&
1833 rt_supported(MLX5_CAP_ROCE(dev->mdev, qp_ts_format)))
1835 MLX5_IB_ALLOC_UCONTEXT_RESP_MASK_REAL_TIME_TS;
1837 resp->num_dyn_bfregs = bfregi->num_dyn_bfregs;
1839 if (MLX5_CAP_GEN(dev->mdev, drain_sigerr))
1840 resp->comp_mask |= MLX5_IB_ALLOC_UCONTEXT_RESP_MASK_SQD2RTS;
1843 MLX5_IB_ALLOC_UCONTEXT_RESP_MASK_MKEY_UPDATE_TAG;
1848 static int mlx5_ib_alloc_ucontext(struct ib_ucontext *uctx,
1849 struct ib_udata *udata)
1851 struct ib_device *ibdev = uctx->device;
1852 struct mlx5_ib_dev *dev = to_mdev(ibdev);
1853 struct mlx5_ib_alloc_ucontext_req_v2 req = {};
1854 struct mlx5_ib_alloc_ucontext_resp resp = {};
1855 struct mlx5_ib_ucontext *context = to_mucontext(uctx);
1856 struct mlx5_bfreg_info *bfregi;
1859 size_t min_req_v2 = offsetof(struct mlx5_ib_alloc_ucontext_req_v2,
1864 if (!dev->ib_active)
1867 if (udata->inlen == sizeof(struct mlx5_ib_alloc_ucontext_req))
1869 else if (udata->inlen >= min_req_v2)
1874 err = ib_copy_from_udata(&req, udata, min(udata->inlen, sizeof(req)));
1878 if (req.flags & ~MLX5_IB_ALLOC_UCTX_DEVX)
1881 if (req.comp_mask || req.reserved0 || req.reserved1 || req.reserved2)
1884 req.total_num_bfregs = ALIGN(req.total_num_bfregs,
1885 MLX5_NON_FP_BFREGS_PER_UAR);
1886 if (req.num_low_latency_bfregs > req.total_num_bfregs - 1)
1889 if (req.flags & MLX5_IB_ALLOC_UCTX_DEVX) {
1890 err = mlx5_ib_devx_create(dev, true);
1893 context->devx_uid = err;
1896 lib_uar_4k = req.lib_caps & MLX5_LIB_CAP_4K_UAR;
1897 lib_uar_dyn = req.lib_caps & MLX5_LIB_CAP_DYN_UAR;
1898 bfregi = &context->bfregi;
1901 bfregi->lib_uar_dyn = lib_uar_dyn;
1905 /* updates req->total_num_bfregs */
1906 err = calc_total_bfregs(dev, lib_uar_4k, &req, bfregi);
1910 mutex_init(&bfregi->lock);
1911 bfregi->lib_uar_4k = lib_uar_4k;
1912 bfregi->count = kcalloc(bfregi->total_num_bfregs, sizeof(*bfregi->count),
1914 if (!bfregi->count) {
1919 bfregi->sys_pages = kcalloc(bfregi->num_sys_pages,
1920 sizeof(*bfregi->sys_pages),
1922 if (!bfregi->sys_pages) {
1927 err = allocate_uars(dev, context);
1932 err = mlx5_ib_alloc_transport_domain(dev, &context->tdn,
1937 INIT_LIST_HEAD(&context->db_page_list);
1938 mutex_init(&context->db_page_mutex);
1940 context->cqe_version = min_t(__u8,
1941 (__u8)MLX5_CAP_GEN(dev->mdev, cqe_version),
1942 req.max_cqe_version);
1944 err = set_ucontext_resp(uctx, &resp);
1948 resp.response_length = min(udata->outlen, sizeof(resp));
1949 err = ib_copy_to_udata(udata, &resp, resp.response_length);
1954 bfregi->num_low_latency_bfregs = req.num_low_latency_bfregs;
1955 context->lib_caps = req.lib_caps;
1956 print_lib_caps(dev, context->lib_caps);
1958 if (mlx5_ib_lag_should_assign_affinity(dev)) {
1959 u32 port = mlx5_core_native_port_num(dev->mdev) - 1;
1961 atomic_set(&context->tx_port_affinity,
1963 1, &dev->port[port].roce.tx_port_affinity));
1969 mlx5_ib_dealloc_transport_domain(dev, context->tdn, context->devx_uid);
1972 deallocate_uars(dev, context);
1975 kfree(bfregi->sys_pages);
1978 kfree(bfregi->count);
1981 if (req.flags & MLX5_IB_ALLOC_UCTX_DEVX)
1982 mlx5_ib_devx_destroy(dev, context->devx_uid);
1988 static int mlx5_ib_query_ucontext(struct ib_ucontext *ibcontext,
1989 struct uverbs_attr_bundle *attrs)
1991 struct mlx5_ib_alloc_ucontext_resp uctx_resp = {};
1994 ret = set_ucontext_resp(ibcontext, &uctx_resp);
1998 uctx_resp.response_length =
2000 uverbs_attr_get_len(attrs,
2001 MLX5_IB_ATTR_QUERY_CONTEXT_RESP_UCTX),
2004 ret = uverbs_copy_to_struct_or_zero(attrs,
2005 MLX5_IB_ATTR_QUERY_CONTEXT_RESP_UCTX,
2011 static void mlx5_ib_dealloc_ucontext(struct ib_ucontext *ibcontext)
2013 struct mlx5_ib_ucontext *context = to_mucontext(ibcontext);
2014 struct mlx5_ib_dev *dev = to_mdev(ibcontext->device);
2015 struct mlx5_bfreg_info *bfregi;
2017 bfregi = &context->bfregi;
2018 mlx5_ib_dealloc_transport_domain(dev, context->tdn, context->devx_uid);
2020 deallocate_uars(dev, context);
2021 kfree(bfregi->sys_pages);
2022 kfree(bfregi->count);
2024 if (context->devx_uid)
2025 mlx5_ib_devx_destroy(dev, context->devx_uid);
2028 static phys_addr_t uar_index2pfn(struct mlx5_ib_dev *dev,
2031 int fw_uars_per_page;
2033 fw_uars_per_page = MLX5_CAP_GEN(dev->mdev, uar_4k) ? MLX5_UARS_IN_PAGE : 1;
2035 return (dev->mdev->bar_addr >> PAGE_SHIFT) + uar_idx / fw_uars_per_page;
2038 static u64 uar_index2paddress(struct mlx5_ib_dev *dev,
2041 unsigned int fw_uars_per_page;
2043 fw_uars_per_page = MLX5_CAP_GEN(dev->mdev, uar_4k) ?
2044 MLX5_UARS_IN_PAGE : 1;
2046 return (dev->mdev->bar_addr + (uar_idx / fw_uars_per_page) * PAGE_SIZE);
2049 static int get_command(unsigned long offset)
2051 return (offset >> MLX5_IB_MMAP_CMD_SHIFT) & MLX5_IB_MMAP_CMD_MASK;
2054 static int get_arg(unsigned long offset)
2056 return offset & ((1 << MLX5_IB_MMAP_CMD_SHIFT) - 1);
2059 static int get_index(unsigned long offset)
2061 return get_arg(offset);
2064 /* Index resides in an extra byte to enable larger values than 255 */
2065 static int get_extended_index(unsigned long offset)
2067 return get_arg(offset) | ((offset >> 16) & 0xff) << 8;
2071 static void mlx5_ib_disassociate_ucontext(struct ib_ucontext *ibcontext)
2075 static inline char *mmap_cmd2str(enum mlx5_ib_mmap_cmd cmd)
2078 case MLX5_IB_MMAP_WC_PAGE:
2080 case MLX5_IB_MMAP_REGULAR_PAGE:
2081 return "best effort WC";
2082 case MLX5_IB_MMAP_NC_PAGE:
2084 case MLX5_IB_MMAP_DEVICE_MEM:
2085 return "Device Memory";
2091 static int mlx5_ib_mmap_clock_info_page(struct mlx5_ib_dev *dev,
2092 struct vm_area_struct *vma,
2093 struct mlx5_ib_ucontext *context)
2095 if ((vma->vm_end - vma->vm_start != PAGE_SIZE) ||
2096 !(vma->vm_flags & VM_SHARED))
2099 if (get_index(vma->vm_pgoff) != MLX5_IB_CLOCK_INFO_V1)
2102 if (vma->vm_flags & (VM_WRITE | VM_EXEC))
2104 vm_flags_clear(vma, VM_MAYWRITE);
2106 if (!dev->mdev->clock_info)
2109 return vm_insert_page(vma, vma->vm_start,
2110 virt_to_page(dev->mdev->clock_info));
2113 static void mlx5_ib_mmap_free(struct rdma_user_mmap_entry *entry)
2115 struct mlx5_user_mmap_entry *mentry = to_mmmap(entry);
2116 struct mlx5_ib_dev *dev = to_mdev(entry->ucontext->device);
2117 struct mlx5_var_table *var_table = &dev->var_table;
2118 struct mlx5_ib_ucontext *context = to_mucontext(entry->ucontext);
2120 switch (mentry->mmap_flag) {
2121 case MLX5_IB_MMAP_TYPE_MEMIC:
2122 case MLX5_IB_MMAP_TYPE_MEMIC_OP:
2123 mlx5_ib_dm_mmap_free(dev, mentry);
2125 case MLX5_IB_MMAP_TYPE_VAR:
2126 mutex_lock(&var_table->bitmap_lock);
2127 clear_bit(mentry->page_idx, var_table->bitmap);
2128 mutex_unlock(&var_table->bitmap_lock);
2131 case MLX5_IB_MMAP_TYPE_UAR_WC:
2132 case MLX5_IB_MMAP_TYPE_UAR_NC:
2133 mlx5_cmd_uar_dealloc(dev->mdev, mentry->page_idx,
2142 static int uar_mmap(struct mlx5_ib_dev *dev, enum mlx5_ib_mmap_cmd cmd,
2143 struct vm_area_struct *vma,
2144 struct mlx5_ib_ucontext *context)
2146 struct mlx5_bfreg_info *bfregi = &context->bfregi;
2151 u32 bfreg_dyn_idx = 0;
2153 int dyn_uar = (cmd == MLX5_IB_MMAP_ALLOC_WC);
2154 int max_valid_idx = dyn_uar ? bfregi->num_sys_pages :
2155 bfregi->num_static_sys_pages;
2157 if (bfregi->lib_uar_dyn)
2160 if (vma->vm_end - vma->vm_start != PAGE_SIZE)
2164 idx = get_extended_index(vma->vm_pgoff) + bfregi->num_static_sys_pages;
2166 idx = get_index(vma->vm_pgoff);
2168 if (idx >= max_valid_idx) {
2169 mlx5_ib_warn(dev, "invalid uar index %lu, max=%d\n",
2170 idx, max_valid_idx);
2175 case MLX5_IB_MMAP_WC_PAGE:
2176 case MLX5_IB_MMAP_ALLOC_WC:
2177 case MLX5_IB_MMAP_REGULAR_PAGE:
2178 /* For MLX5_IB_MMAP_REGULAR_PAGE do the best effort to get WC */
2179 prot = pgprot_writecombine(vma->vm_page_prot);
2181 case MLX5_IB_MMAP_NC_PAGE:
2182 prot = pgprot_noncached(vma->vm_page_prot);
2191 uars_per_page = get_uars_per_sys_page(dev, bfregi->lib_uar_4k);
2192 bfreg_dyn_idx = idx * (uars_per_page * MLX5_NON_FP_BFREGS_PER_UAR);
2193 if (bfreg_dyn_idx >= bfregi->total_num_bfregs) {
2194 mlx5_ib_warn(dev, "invalid bfreg_dyn_idx %u, max=%u\n",
2195 bfreg_dyn_idx, bfregi->total_num_bfregs);
2199 mutex_lock(&bfregi->lock);
2200 /* Fail if uar already allocated, first bfreg index of each
2201 * page holds its count.
2203 if (bfregi->count[bfreg_dyn_idx]) {
2204 mlx5_ib_warn(dev, "wrong offset, idx %lu is busy, bfregn=%u\n", idx, bfreg_dyn_idx);
2205 mutex_unlock(&bfregi->lock);
2209 bfregi->count[bfreg_dyn_idx]++;
2210 mutex_unlock(&bfregi->lock);
2212 err = mlx5_cmd_uar_alloc(dev->mdev, &uar_index,
2215 mlx5_ib_warn(dev, "UAR alloc failed\n");
2219 uar_index = bfregi->sys_pages[idx];
2222 pfn = uar_index2pfn(dev, uar_index);
2223 mlx5_ib_dbg(dev, "uar idx 0x%lx, pfn %pa\n", idx, &pfn);
2225 err = rdma_user_mmap_io(&context->ibucontext, vma, pfn, PAGE_SIZE,
2229 "rdma_user_mmap_io failed with error=%d, mmap_cmd=%s\n",
2230 err, mmap_cmd2str(cmd));
2235 bfregi->sys_pages[idx] = uar_index;
2242 mlx5_cmd_uar_dealloc(dev->mdev, idx, context->devx_uid);
2245 mlx5_ib_free_bfreg(dev, bfregi, bfreg_dyn_idx);
2250 static unsigned long mlx5_vma_to_pgoff(struct vm_area_struct *vma)
2255 command = get_command(vma->vm_pgoff);
2256 idx = get_extended_index(vma->vm_pgoff);
2258 return (command << 16 | idx);
2261 static int mlx5_ib_mmap_offset(struct mlx5_ib_dev *dev,
2262 struct vm_area_struct *vma,
2263 struct ib_ucontext *ucontext)
2265 struct mlx5_user_mmap_entry *mentry;
2266 struct rdma_user_mmap_entry *entry;
2267 unsigned long pgoff;
2272 pgoff = mlx5_vma_to_pgoff(vma);
2273 entry = rdma_user_mmap_entry_get_pgoff(ucontext, pgoff);
2277 mentry = to_mmmap(entry);
2278 pfn = (mentry->address >> PAGE_SHIFT);
2279 if (mentry->mmap_flag == MLX5_IB_MMAP_TYPE_VAR ||
2280 mentry->mmap_flag == MLX5_IB_MMAP_TYPE_UAR_NC)
2281 prot = pgprot_noncached(vma->vm_page_prot);
2283 prot = pgprot_writecombine(vma->vm_page_prot);
2284 ret = rdma_user_mmap_io(ucontext, vma, pfn,
2285 entry->npages * PAGE_SIZE,
2288 rdma_user_mmap_entry_put(&mentry->rdma_entry);
2292 static u64 mlx5_entry_to_mmap_offset(struct mlx5_user_mmap_entry *entry)
2294 u64 cmd = (entry->rdma_entry.start_pgoff >> 16) & 0xFFFF;
2295 u64 index = entry->rdma_entry.start_pgoff & 0xFFFF;
2297 return (((index >> 8) << 16) | (cmd << MLX5_IB_MMAP_CMD_SHIFT) |
2298 (index & 0xFF)) << PAGE_SHIFT;
2301 static int mlx5_ib_mmap(struct ib_ucontext *ibcontext, struct vm_area_struct *vma)
2303 struct mlx5_ib_ucontext *context = to_mucontext(ibcontext);
2304 struct mlx5_ib_dev *dev = to_mdev(ibcontext->device);
2305 unsigned long command;
2308 command = get_command(vma->vm_pgoff);
2310 case MLX5_IB_MMAP_WC_PAGE:
2311 case MLX5_IB_MMAP_ALLOC_WC:
2312 if (!dev->wc_support)
2315 case MLX5_IB_MMAP_NC_PAGE:
2316 case MLX5_IB_MMAP_REGULAR_PAGE:
2317 return uar_mmap(dev, command, vma, context);
2319 case MLX5_IB_MMAP_GET_CONTIGUOUS_PAGES:
2322 case MLX5_IB_MMAP_CORE_CLOCK:
2323 if (vma->vm_end - vma->vm_start != PAGE_SIZE)
2326 if (vma->vm_flags & VM_WRITE)
2328 vm_flags_clear(vma, VM_MAYWRITE);
2330 /* Don't expose to user-space information it shouldn't have */
2331 if (PAGE_SIZE > 4096)
2334 pfn = (dev->mdev->iseg_base +
2335 offsetof(struct mlx5_init_seg, internal_timer_h)) >>
2337 return rdma_user_mmap_io(&context->ibucontext, vma, pfn,
2339 pgprot_noncached(vma->vm_page_prot),
2341 case MLX5_IB_MMAP_CLOCK_INFO:
2342 return mlx5_ib_mmap_clock_info_page(dev, vma, context);
2345 return mlx5_ib_mmap_offset(dev, vma, ibcontext);
2351 static int mlx5_ib_alloc_pd(struct ib_pd *ibpd, struct ib_udata *udata)
2353 struct mlx5_ib_pd *pd = to_mpd(ibpd);
2354 struct ib_device *ibdev = ibpd->device;
2355 struct mlx5_ib_alloc_pd_resp resp;
2357 u32 out[MLX5_ST_SZ_DW(alloc_pd_out)] = {};
2358 u32 in[MLX5_ST_SZ_DW(alloc_pd_in)] = {};
2360 struct mlx5_ib_ucontext *context = rdma_udata_to_drv_context(
2361 udata, struct mlx5_ib_ucontext, ibucontext);
2363 uid = context ? context->devx_uid : 0;
2364 MLX5_SET(alloc_pd_in, in, opcode, MLX5_CMD_OP_ALLOC_PD);
2365 MLX5_SET(alloc_pd_in, in, uid, uid);
2366 err = mlx5_cmd_exec_inout(to_mdev(ibdev)->mdev, alloc_pd, in, out);
2370 pd->pdn = MLX5_GET(alloc_pd_out, out, pd);
2374 if (ib_copy_to_udata(udata, &resp, sizeof(resp))) {
2375 mlx5_cmd_dealloc_pd(to_mdev(ibdev)->mdev, pd->pdn, uid);
2383 static int mlx5_ib_dealloc_pd(struct ib_pd *pd, struct ib_udata *udata)
2385 struct mlx5_ib_dev *mdev = to_mdev(pd->device);
2386 struct mlx5_ib_pd *mpd = to_mpd(pd);
2388 return mlx5_cmd_dealloc_pd(mdev->mdev, mpd->pdn, mpd->uid);
2391 static int mlx5_ib_mcg_attach(struct ib_qp *ibqp, union ib_gid *gid, u16 lid)
2393 struct mlx5_ib_dev *dev = to_mdev(ibqp->device);
2394 struct mlx5_ib_qp *mqp = to_mqp(ibqp);
2399 to_mpd(ibqp->pd)->uid : 0;
2401 if (mqp->flags & IB_QP_CREATE_SOURCE_QPN) {
2402 mlx5_ib_dbg(dev, "Attaching a multi cast group to underlay QP is not supported\n");
2406 err = mlx5_cmd_attach_mcg(dev->mdev, gid, ibqp->qp_num, uid);
2408 mlx5_ib_warn(dev, "failed attaching QPN 0x%x, MGID %pI6\n",
2409 ibqp->qp_num, gid->raw);
2414 static int mlx5_ib_mcg_detach(struct ib_qp *ibqp, union ib_gid *gid, u16 lid)
2416 struct mlx5_ib_dev *dev = to_mdev(ibqp->device);
2421 to_mpd(ibqp->pd)->uid : 0;
2422 err = mlx5_cmd_detach_mcg(dev->mdev, gid, ibqp->qp_num, uid);
2424 mlx5_ib_warn(dev, "failed detaching QPN 0x%x, MGID %pI6\n",
2425 ibqp->qp_num, gid->raw);
2430 static int init_node_data(struct mlx5_ib_dev *dev)
2434 err = mlx5_query_node_desc(dev, dev->ib_dev.node_desc);
2438 dev->mdev->rev_id = dev->mdev->pdev->revision;
2440 return mlx5_query_node_guid(dev, &dev->ib_dev.node_guid);
2443 static ssize_t fw_pages_show(struct device *device,
2444 struct device_attribute *attr, char *buf)
2446 struct mlx5_ib_dev *dev =
2447 rdma_device_to_drv_device(device, struct mlx5_ib_dev, ib_dev);
2449 return sysfs_emit(buf, "%d\n", dev->mdev->priv.fw_pages);
2451 static DEVICE_ATTR_RO(fw_pages);
2453 static ssize_t reg_pages_show(struct device *device,
2454 struct device_attribute *attr, char *buf)
2456 struct mlx5_ib_dev *dev =
2457 rdma_device_to_drv_device(device, struct mlx5_ib_dev, ib_dev);
2459 return sysfs_emit(buf, "%d\n", atomic_read(&dev->mdev->priv.reg_pages));
2461 static DEVICE_ATTR_RO(reg_pages);
2463 static ssize_t hca_type_show(struct device *device,
2464 struct device_attribute *attr, char *buf)
2466 struct mlx5_ib_dev *dev =
2467 rdma_device_to_drv_device(device, struct mlx5_ib_dev, ib_dev);
2469 return sysfs_emit(buf, "MT%d\n", dev->mdev->pdev->device);
2471 static DEVICE_ATTR_RO(hca_type);
2473 static ssize_t hw_rev_show(struct device *device,
2474 struct device_attribute *attr, char *buf)
2476 struct mlx5_ib_dev *dev =
2477 rdma_device_to_drv_device(device, struct mlx5_ib_dev, ib_dev);
2479 return sysfs_emit(buf, "%x\n", dev->mdev->rev_id);
2481 static DEVICE_ATTR_RO(hw_rev);
2483 static ssize_t board_id_show(struct device *device,
2484 struct device_attribute *attr, char *buf)
2486 struct mlx5_ib_dev *dev =
2487 rdma_device_to_drv_device(device, struct mlx5_ib_dev, ib_dev);
2489 return sysfs_emit(buf, "%.*s\n", MLX5_BOARD_ID_LEN,
2490 dev->mdev->board_id);
2492 static DEVICE_ATTR_RO(board_id);
2494 static struct attribute *mlx5_class_attributes[] = {
2495 &dev_attr_hw_rev.attr,
2496 &dev_attr_hca_type.attr,
2497 &dev_attr_board_id.attr,
2498 &dev_attr_fw_pages.attr,
2499 &dev_attr_reg_pages.attr,
2503 static const struct attribute_group mlx5_attr_group = {
2504 .attrs = mlx5_class_attributes,
2507 static void pkey_change_handler(struct work_struct *work)
2509 struct mlx5_ib_port_resources *ports =
2510 container_of(work, struct mlx5_ib_port_resources,
2515 * We got this event before device was fully configured
2516 * and MAD registration code wasn't called/finished yet.
2520 mlx5_ib_gsi_pkey_change(ports->gsi);
2523 static void mlx5_ib_handle_internal_error(struct mlx5_ib_dev *ibdev)
2525 struct mlx5_ib_qp *mqp;
2526 struct mlx5_ib_cq *send_mcq, *recv_mcq;
2527 struct mlx5_core_cq *mcq;
2528 struct list_head cq_armed_list;
2529 unsigned long flags_qp;
2530 unsigned long flags_cq;
2531 unsigned long flags;
2533 INIT_LIST_HEAD(&cq_armed_list);
2535 /* Go over qp list reside on that ibdev, sync with create/destroy qp.*/
2536 spin_lock_irqsave(&ibdev->reset_flow_resource_lock, flags);
2537 list_for_each_entry(mqp, &ibdev->qp_list, qps_list) {
2538 spin_lock_irqsave(&mqp->sq.lock, flags_qp);
2539 if (mqp->sq.tail != mqp->sq.head) {
2540 send_mcq = to_mcq(mqp->ibqp.send_cq);
2541 spin_lock_irqsave(&send_mcq->lock, flags_cq);
2542 if (send_mcq->mcq.comp &&
2543 mqp->ibqp.send_cq->comp_handler) {
2544 if (!send_mcq->mcq.reset_notify_added) {
2545 send_mcq->mcq.reset_notify_added = 1;
2546 list_add_tail(&send_mcq->mcq.reset_notify,
2550 spin_unlock_irqrestore(&send_mcq->lock, flags_cq);
2552 spin_unlock_irqrestore(&mqp->sq.lock, flags_qp);
2553 spin_lock_irqsave(&mqp->rq.lock, flags_qp);
2554 /* no handling is needed for SRQ */
2555 if (!mqp->ibqp.srq) {
2556 if (mqp->rq.tail != mqp->rq.head) {
2557 recv_mcq = to_mcq(mqp->ibqp.recv_cq);
2558 spin_lock_irqsave(&recv_mcq->lock, flags_cq);
2559 if (recv_mcq->mcq.comp &&
2560 mqp->ibqp.recv_cq->comp_handler) {
2561 if (!recv_mcq->mcq.reset_notify_added) {
2562 recv_mcq->mcq.reset_notify_added = 1;
2563 list_add_tail(&recv_mcq->mcq.reset_notify,
2567 spin_unlock_irqrestore(&recv_mcq->lock,
2571 spin_unlock_irqrestore(&mqp->rq.lock, flags_qp);
2573 /*At that point all inflight post send were put to be executed as of we
2574 * lock/unlock above locks Now need to arm all involved CQs.
2576 list_for_each_entry(mcq, &cq_armed_list, reset_notify) {
2577 mcq->comp(mcq, NULL);
2579 spin_unlock_irqrestore(&ibdev->reset_flow_resource_lock, flags);
2582 static void delay_drop_handler(struct work_struct *work)
2585 struct mlx5_ib_delay_drop *delay_drop =
2586 container_of(work, struct mlx5_ib_delay_drop,
2589 atomic_inc(&delay_drop->events_cnt);
2591 mutex_lock(&delay_drop->lock);
2592 err = mlx5_core_set_delay_drop(delay_drop->dev, delay_drop->timeout);
2594 mlx5_ib_warn(delay_drop->dev, "Failed to set delay drop, timeout=%u\n",
2595 delay_drop->timeout);
2596 delay_drop->activate = false;
2598 mutex_unlock(&delay_drop->lock);
2601 static void handle_general_event(struct mlx5_ib_dev *ibdev, struct mlx5_eqe *eqe,
2602 struct ib_event *ibev)
2604 u32 port = (eqe->data.port.port >> 4) & 0xf;
2606 switch (eqe->sub_type) {
2607 case MLX5_GENERAL_SUBTYPE_DELAY_DROP_TIMEOUT:
2608 if (mlx5_ib_port_link_layer(&ibdev->ib_dev, port) ==
2609 IB_LINK_LAYER_ETHERNET)
2610 schedule_work(&ibdev->delay_drop.delay_drop_work);
2612 default: /* do nothing */
2617 static int handle_port_change(struct mlx5_ib_dev *ibdev, struct mlx5_eqe *eqe,
2618 struct ib_event *ibev)
2620 u32 port = (eqe->data.port.port >> 4) & 0xf;
2622 ibev->element.port_num = port;
2624 switch (eqe->sub_type) {
2625 case MLX5_PORT_CHANGE_SUBTYPE_ACTIVE:
2626 case MLX5_PORT_CHANGE_SUBTYPE_DOWN:
2627 case MLX5_PORT_CHANGE_SUBTYPE_INITIALIZED:
2628 /* In RoCE, port up/down events are handled in
2629 * mlx5_netdev_event().
2631 if (mlx5_ib_port_link_layer(&ibdev->ib_dev, port) ==
2632 IB_LINK_LAYER_ETHERNET)
2635 ibev->event = (eqe->sub_type == MLX5_PORT_CHANGE_SUBTYPE_ACTIVE) ?
2636 IB_EVENT_PORT_ACTIVE : IB_EVENT_PORT_ERR;
2639 case MLX5_PORT_CHANGE_SUBTYPE_LID:
2640 ibev->event = IB_EVENT_LID_CHANGE;
2643 case MLX5_PORT_CHANGE_SUBTYPE_PKEY:
2644 ibev->event = IB_EVENT_PKEY_CHANGE;
2645 schedule_work(&ibdev->devr.ports[port - 1].pkey_change_work);
2648 case MLX5_PORT_CHANGE_SUBTYPE_GUID:
2649 ibev->event = IB_EVENT_GID_CHANGE;
2652 case MLX5_PORT_CHANGE_SUBTYPE_CLIENT_REREG:
2653 ibev->event = IB_EVENT_CLIENT_REREGISTER;
2662 static void mlx5_ib_handle_event(struct work_struct *_work)
2664 struct mlx5_ib_event_work *work =
2665 container_of(_work, struct mlx5_ib_event_work, work);
2666 struct mlx5_ib_dev *ibdev;
2667 struct ib_event ibev;
2670 if (work->is_slave) {
2671 ibdev = mlx5_ib_get_ibdev_from_mpi(work->mpi);
2678 switch (work->event) {
2679 case MLX5_DEV_EVENT_SYS_ERROR:
2680 ibev.event = IB_EVENT_DEVICE_FATAL;
2681 mlx5_ib_handle_internal_error(ibdev);
2682 ibev.element.port_num = (u8)(unsigned long)work->param;
2685 case MLX5_EVENT_TYPE_PORT_CHANGE:
2686 if (handle_port_change(ibdev, work->param, &ibev))
2689 case MLX5_EVENT_TYPE_GENERAL_EVENT:
2690 handle_general_event(ibdev, work->param, &ibev);
2696 ibev.device = &ibdev->ib_dev;
2698 if (!rdma_is_port_valid(&ibdev->ib_dev, ibev.element.port_num)) {
2699 mlx5_ib_warn(ibdev, "warning: event on port %d\n", ibev.element.port_num);
2703 if (ibdev->ib_active)
2704 ib_dispatch_event(&ibev);
2707 ibdev->ib_active = false;
2712 static int mlx5_ib_event(struct notifier_block *nb,
2713 unsigned long event, void *param)
2715 struct mlx5_ib_event_work *work;
2717 work = kmalloc(sizeof(*work), GFP_ATOMIC);
2721 INIT_WORK(&work->work, mlx5_ib_handle_event);
2722 work->dev = container_of(nb, struct mlx5_ib_dev, mdev_events);
2723 work->is_slave = false;
2724 work->param = param;
2725 work->event = event;
2727 queue_work(mlx5_ib_event_wq, &work->work);
2732 static int mlx5_ib_event_slave_port(struct notifier_block *nb,
2733 unsigned long event, void *param)
2735 struct mlx5_ib_event_work *work;
2737 work = kmalloc(sizeof(*work), GFP_ATOMIC);
2741 INIT_WORK(&work->work, mlx5_ib_handle_event);
2742 work->mpi = container_of(nb, struct mlx5_ib_multiport_info, mdev_events);
2743 work->is_slave = true;
2744 work->param = param;
2745 work->event = event;
2746 queue_work(mlx5_ib_event_wq, &work->work);
2751 static int set_has_smi_cap(struct mlx5_ib_dev *dev)
2753 struct mlx5_hca_vport_context vport_ctx;
2757 if (MLX5_CAP_GEN(dev->mdev, port_type) != MLX5_CAP_PORT_TYPE_IB)
2760 for (port = 1; port <= dev->num_ports; port++) {
2761 if (!MLX5_CAP_GEN(dev->mdev, ib_virt)) {
2762 dev->port_caps[port - 1].has_smi = true;
2765 err = mlx5_query_hca_vport_context(dev->mdev, 0, port, 0,
2768 mlx5_ib_err(dev, "query_hca_vport_context for port=%d failed %d\n",
2772 dev->port_caps[port - 1].has_smi = vport_ctx.has_smi;
2778 static void get_ext_port_caps(struct mlx5_ib_dev *dev)
2782 rdma_for_each_port (&dev->ib_dev, port)
2783 mlx5_query_ext_port_caps(dev, port);
2786 static u8 mlx5_get_umr_fence(u8 umr_fence_cap)
2788 switch (umr_fence_cap) {
2789 case MLX5_CAP_UMR_FENCE_NONE:
2790 return MLX5_FENCE_MODE_NONE;
2791 case MLX5_CAP_UMR_FENCE_SMALL:
2792 return MLX5_FENCE_MODE_INITIATOR_SMALL;
2794 return MLX5_FENCE_MODE_STRONG_ORDERING;
2798 static int mlx5_ib_dev_res_init(struct mlx5_ib_dev *dev)
2800 struct mlx5_ib_resources *devr = &dev->devr;
2801 struct ib_srq_init_attr attr;
2802 struct ib_device *ibdev;
2803 struct ib_cq_init_attr cq_attr = {.cqe = 1};
2807 ibdev = &dev->ib_dev;
2809 if (!MLX5_CAP_GEN(dev->mdev, xrc))
2812 devr->p0 = ib_alloc_pd(ibdev, 0);
2813 if (IS_ERR(devr->p0))
2814 return PTR_ERR(devr->p0);
2816 devr->c0 = ib_create_cq(ibdev, NULL, NULL, NULL, &cq_attr);
2817 if (IS_ERR(devr->c0)) {
2818 ret = PTR_ERR(devr->c0);
2822 ret = mlx5_cmd_xrcd_alloc(dev->mdev, &devr->xrcdn0, 0);
2826 ret = mlx5_cmd_xrcd_alloc(dev->mdev, &devr->xrcdn1, 0);
2830 memset(&attr, 0, sizeof(attr));
2831 attr.attr.max_sge = 1;
2832 attr.attr.max_wr = 1;
2833 attr.srq_type = IB_SRQT_XRC;
2834 attr.ext.cq = devr->c0;
2836 devr->s0 = ib_create_srq(devr->p0, &attr);
2837 if (IS_ERR(devr->s0)) {
2838 ret = PTR_ERR(devr->s0);
2842 memset(&attr, 0, sizeof(attr));
2843 attr.attr.max_sge = 1;
2844 attr.attr.max_wr = 1;
2845 attr.srq_type = IB_SRQT_BASIC;
2847 devr->s1 = ib_create_srq(devr->p0, &attr);
2848 if (IS_ERR(devr->s1)) {
2849 ret = PTR_ERR(devr->s1);
2853 for (port = 0; port < ARRAY_SIZE(devr->ports); ++port)
2854 INIT_WORK(&devr->ports[port].pkey_change_work,
2855 pkey_change_handler);
2860 ib_destroy_srq(devr->s0);
2862 mlx5_cmd_xrcd_dealloc(dev->mdev, devr->xrcdn1, 0);
2864 mlx5_cmd_xrcd_dealloc(dev->mdev, devr->xrcdn0, 0);
2866 ib_destroy_cq(devr->c0);
2868 ib_dealloc_pd(devr->p0);
2872 static void mlx5_ib_dev_res_cleanup(struct mlx5_ib_dev *dev)
2874 struct mlx5_ib_resources *devr = &dev->devr;
2878 * Make sure no change P_Key work items are still executing.
2880 * At this stage, the mlx5_ib_event should be unregistered
2881 * and it ensures that no new works are added.
2883 for (port = 0; port < ARRAY_SIZE(devr->ports); ++port)
2884 cancel_work_sync(&devr->ports[port].pkey_change_work);
2886 ib_destroy_srq(devr->s1);
2887 ib_destroy_srq(devr->s0);
2888 mlx5_cmd_xrcd_dealloc(dev->mdev, devr->xrcdn1, 0);
2889 mlx5_cmd_xrcd_dealloc(dev->mdev, devr->xrcdn0, 0);
2890 ib_destroy_cq(devr->c0);
2891 ib_dealloc_pd(devr->p0);
2894 static u32 get_core_cap_flags(struct ib_device *ibdev,
2895 struct mlx5_hca_vport_context *rep)
2897 struct mlx5_ib_dev *dev = to_mdev(ibdev);
2898 enum rdma_link_layer ll = mlx5_ib_port_link_layer(ibdev, 1);
2899 u8 l3_type_cap = MLX5_CAP_ROCE(dev->mdev, l3_type);
2900 u8 roce_version_cap = MLX5_CAP_ROCE(dev->mdev, roce_version);
2901 bool raw_support = !mlx5_core_mp_enabled(dev->mdev);
2904 if (rep->grh_required)
2905 ret |= RDMA_CORE_CAP_IB_GRH_REQUIRED;
2907 if (ll == IB_LINK_LAYER_INFINIBAND)
2908 return ret | RDMA_CORE_PORT_IBA_IB;
2911 ret |= RDMA_CORE_PORT_RAW_PACKET;
2913 if (!(l3_type_cap & MLX5_ROCE_L3_TYPE_IPV4_CAP))
2916 if (!(l3_type_cap & MLX5_ROCE_L3_TYPE_IPV6_CAP))
2919 if (roce_version_cap & MLX5_ROCE_VERSION_1_CAP)
2920 ret |= RDMA_CORE_PORT_IBA_ROCE;
2922 if (roce_version_cap & MLX5_ROCE_VERSION_2_CAP)
2923 ret |= RDMA_CORE_PORT_IBA_ROCE_UDP_ENCAP;
2928 static int mlx5_port_immutable(struct ib_device *ibdev, u32 port_num,
2929 struct ib_port_immutable *immutable)
2931 struct ib_port_attr attr;
2932 struct mlx5_ib_dev *dev = to_mdev(ibdev);
2933 enum rdma_link_layer ll = mlx5_ib_port_link_layer(ibdev, port_num);
2934 struct mlx5_hca_vport_context rep = {0};
2937 err = ib_query_port(ibdev, port_num, &attr);
2941 if (ll == IB_LINK_LAYER_INFINIBAND) {
2942 err = mlx5_query_hca_vport_context(dev->mdev, 0, port_num, 0,
2948 immutable->pkey_tbl_len = attr.pkey_tbl_len;
2949 immutable->gid_tbl_len = attr.gid_tbl_len;
2950 immutable->core_cap_flags = get_core_cap_flags(ibdev, &rep);
2951 immutable->max_mad_size = IB_MGMT_MAD_SIZE;
2956 static int mlx5_port_rep_immutable(struct ib_device *ibdev, u32 port_num,
2957 struct ib_port_immutable *immutable)
2959 struct ib_port_attr attr;
2962 immutable->core_cap_flags = RDMA_CORE_PORT_RAW_PACKET;
2964 err = ib_query_port(ibdev, port_num, &attr);
2968 immutable->pkey_tbl_len = attr.pkey_tbl_len;
2969 immutable->gid_tbl_len = attr.gid_tbl_len;
2970 immutable->core_cap_flags = RDMA_CORE_PORT_RAW_PACKET;
2975 static void get_dev_fw_str(struct ib_device *ibdev, char *str)
2977 struct mlx5_ib_dev *dev =
2978 container_of(ibdev, struct mlx5_ib_dev, ib_dev);
2979 snprintf(str, IB_FW_VERSION_NAME_MAX, "%d.%d.%04d",
2980 fw_rev_maj(dev->mdev), fw_rev_min(dev->mdev),
2981 fw_rev_sub(dev->mdev));
2984 static int mlx5_eth_lag_init(struct mlx5_ib_dev *dev)
2986 struct mlx5_core_dev *mdev = dev->mdev;
2987 struct mlx5_flow_namespace *ns = mlx5_get_flow_namespace(mdev,
2988 MLX5_FLOW_NAMESPACE_LAG);
2989 struct mlx5_flow_table *ft;
2992 if (!ns || !mlx5_lag_is_active(mdev))
2995 err = mlx5_cmd_create_vport_lag(mdev);
2999 ft = mlx5_create_lag_demux_flow_table(ns, 0, 0);
3002 goto err_destroy_vport_lag;
3005 dev->flow_db->lag_demux_ft = ft;
3006 dev->lag_ports = mlx5_lag_get_num_ports(mdev);
3007 dev->lag_active = true;
3010 err_destroy_vport_lag:
3011 mlx5_cmd_destroy_vport_lag(mdev);
3015 static void mlx5_eth_lag_cleanup(struct mlx5_ib_dev *dev)
3017 struct mlx5_core_dev *mdev = dev->mdev;
3019 if (dev->lag_active) {
3020 dev->lag_active = false;
3022 mlx5_destroy_flow_table(dev->flow_db->lag_demux_ft);
3023 dev->flow_db->lag_demux_ft = NULL;
3025 mlx5_cmd_destroy_vport_lag(mdev);
3029 static void mlx5_netdev_notifier_register(struct mlx5_roce *roce,
3030 struct net_device *netdev)
3034 if (roce->tracking_netdev)
3036 roce->tracking_netdev = netdev;
3037 roce->nb.notifier_call = mlx5_netdev_event;
3038 err = register_netdevice_notifier_dev_net(netdev, &roce->nb, &roce->nn);
3042 static void mlx5_netdev_notifier_unregister(struct mlx5_roce *roce)
3044 if (!roce->tracking_netdev)
3046 unregister_netdevice_notifier_dev_net(roce->tracking_netdev, &roce->nb,
3048 roce->tracking_netdev = NULL;
3051 static int mlx5e_mdev_notifier_event(struct notifier_block *nb,
3052 unsigned long event, void *data)
3054 struct mlx5_roce *roce = container_of(nb, struct mlx5_roce, mdev_nb);
3055 struct net_device *netdev = data;
3058 case MLX5_DRIVER_EVENT_UPLINK_NETDEV:
3060 mlx5_netdev_notifier_register(roce, netdev);
3062 mlx5_netdev_notifier_unregister(roce);
3071 static void mlx5_mdev_netdev_track(struct mlx5_ib_dev *dev, u32 port_num)
3073 struct mlx5_roce *roce = &dev->port[port_num].roce;
3075 roce->mdev_nb.notifier_call = mlx5e_mdev_notifier_event;
3076 mlx5_blocking_notifier_register(dev->mdev, &roce->mdev_nb);
3077 mlx5_core_uplink_netdev_event_replay(dev->mdev);
3080 static void mlx5_mdev_netdev_untrack(struct mlx5_ib_dev *dev, u32 port_num)
3082 struct mlx5_roce *roce = &dev->port[port_num].roce;
3084 mlx5_blocking_notifier_unregister(dev->mdev, &roce->mdev_nb);
3085 mlx5_netdev_notifier_unregister(roce);
3088 static int mlx5_enable_eth(struct mlx5_ib_dev *dev)
3092 if (!dev->is_rep && dev->profile != &raw_eth_profile) {
3093 err = mlx5_nic_vport_enable_roce(dev->mdev);
3098 err = mlx5_eth_lag_init(dev);
3100 goto err_disable_roce;
3105 if (!dev->is_rep && dev->profile != &raw_eth_profile)
3106 mlx5_nic_vport_disable_roce(dev->mdev);
3111 static void mlx5_disable_eth(struct mlx5_ib_dev *dev)
3113 mlx5_eth_lag_cleanup(dev);
3114 if (!dev->is_rep && dev->profile != &raw_eth_profile)
3115 mlx5_nic_vport_disable_roce(dev->mdev);
3118 static int mlx5_ib_rn_get_params(struct ib_device *device, u32 port_num,
3119 enum rdma_netdev_t type,
3120 struct rdma_netdev_alloc_params *params)
3122 if (type != RDMA_NETDEV_IPOIB)
3125 return mlx5_rdma_rn_get_params(to_mdev(device)->mdev, device, params);
3128 static ssize_t delay_drop_timeout_read(struct file *filp, char __user *buf,
3129 size_t count, loff_t *pos)
3131 struct mlx5_ib_delay_drop *delay_drop = filp->private_data;
3135 len = snprintf(lbuf, sizeof(lbuf), "%u\n", delay_drop->timeout);
3136 return simple_read_from_buffer(buf, count, pos, lbuf, len);
3139 static ssize_t delay_drop_timeout_write(struct file *filp, const char __user *buf,
3140 size_t count, loff_t *pos)
3142 struct mlx5_ib_delay_drop *delay_drop = filp->private_data;
3146 if (kstrtouint_from_user(buf, count, 0, &var))
3149 timeout = min_t(u32, roundup(var, 100), MLX5_MAX_DELAY_DROP_TIMEOUT_MS *
3152 mlx5_ib_dbg(delay_drop->dev, "Round delay drop timeout to %u usec\n",
3155 delay_drop->timeout = timeout;
3160 static const struct file_operations fops_delay_drop_timeout = {
3161 .owner = THIS_MODULE,
3162 .open = simple_open,
3163 .write = delay_drop_timeout_write,
3164 .read = delay_drop_timeout_read,
3167 static void mlx5_ib_unbind_slave_port(struct mlx5_ib_dev *ibdev,
3168 struct mlx5_ib_multiport_info *mpi)
3170 u32 port_num = mlx5_core_native_port_num(mpi->mdev) - 1;
3171 struct mlx5_ib_port *port = &ibdev->port[port_num];
3176 lockdep_assert_held(&mlx5_ib_multiport_mutex);
3178 mlx5_ib_cleanup_cong_debugfs(ibdev, port_num);
3180 spin_lock(&port->mp.mpi_lock);
3182 spin_unlock(&port->mp.mpi_lock);
3188 spin_unlock(&port->mp.mpi_lock);
3189 if (mpi->mdev_events.notifier_call)
3190 mlx5_notifier_unregister(mpi->mdev, &mpi->mdev_events);
3191 mpi->mdev_events.notifier_call = NULL;
3192 mlx5_mdev_netdev_untrack(ibdev, port_num);
3193 spin_lock(&port->mp.mpi_lock);
3195 comps = mpi->mdev_refcnt;
3197 mpi->unaffiliate = true;
3198 init_completion(&mpi->unref_comp);
3199 spin_unlock(&port->mp.mpi_lock);
3201 for (i = 0; i < comps; i++)
3202 wait_for_completion(&mpi->unref_comp);
3204 spin_lock(&port->mp.mpi_lock);
3205 mpi->unaffiliate = false;
3208 port->mp.mpi = NULL;
3210 spin_unlock(&port->mp.mpi_lock);
3212 err = mlx5_nic_vport_unaffiliate_multiport(mpi->mdev);
3214 mlx5_ib_dbg(ibdev, "unaffiliated port %u\n", port_num + 1);
3215 /* Log an error, still needed to cleanup the pointers and add
3216 * it back to the list.
3219 mlx5_ib_err(ibdev, "Failed to unaffiliate port %u\n",
3222 ibdev->port[port_num].roce.last_port_state = IB_PORT_DOWN;
3225 static bool mlx5_ib_bind_slave_port(struct mlx5_ib_dev *ibdev,
3226 struct mlx5_ib_multiport_info *mpi)
3228 u32 port_num = mlx5_core_native_port_num(mpi->mdev) - 1;
3231 lockdep_assert_held(&mlx5_ib_multiport_mutex);
3233 spin_lock(&ibdev->port[port_num].mp.mpi_lock);
3234 if (ibdev->port[port_num].mp.mpi) {
3235 mlx5_ib_dbg(ibdev, "port %u already affiliated.\n",
3237 spin_unlock(&ibdev->port[port_num].mp.mpi_lock);
3241 ibdev->port[port_num].mp.mpi = mpi;
3243 mpi->mdev_events.notifier_call = NULL;
3244 spin_unlock(&ibdev->port[port_num].mp.mpi_lock);
3246 err = mlx5_nic_vport_affiliate_multiport(ibdev->mdev, mpi->mdev);
3250 mlx5_mdev_netdev_track(ibdev, port_num);
3252 mpi->mdev_events.notifier_call = mlx5_ib_event_slave_port;
3253 mlx5_notifier_register(mpi->mdev, &mpi->mdev_events);
3255 mlx5_ib_init_cong_debugfs(ibdev, port_num);
3260 mlx5_ib_unbind_slave_port(ibdev, mpi);
3264 static int mlx5_ib_init_multiport_master(struct mlx5_ib_dev *dev)
3266 u32 port_num = mlx5_core_native_port_num(dev->mdev) - 1;
3267 enum rdma_link_layer ll = mlx5_ib_port_link_layer(&dev->ib_dev,
3269 struct mlx5_ib_multiport_info *mpi;
3273 if (!mlx5_core_is_mp_master(dev->mdev) || ll != IB_LINK_LAYER_ETHERNET)
3276 err = mlx5_query_nic_vport_system_image_guid(dev->mdev,
3277 &dev->sys_image_guid);
3281 err = mlx5_nic_vport_enable_roce(dev->mdev);
3285 mutex_lock(&mlx5_ib_multiport_mutex);
3286 for (i = 0; i < dev->num_ports; i++) {
3289 /* build a stub multiport info struct for the native port. */
3290 if (i == port_num) {
3291 mpi = kzalloc(sizeof(*mpi), GFP_KERNEL);
3293 mutex_unlock(&mlx5_ib_multiport_mutex);
3294 mlx5_nic_vport_disable_roce(dev->mdev);
3298 mpi->is_master = true;
3299 mpi->mdev = dev->mdev;
3300 mpi->sys_image_guid = dev->sys_image_guid;
3301 dev->port[i].mp.mpi = mpi;
3307 list_for_each_entry(mpi, &mlx5_ib_unaffiliated_port_list,
3309 if (dev->sys_image_guid == mpi->sys_image_guid &&
3310 (mlx5_core_native_port_num(mpi->mdev) - 1) == i) {
3311 bound = mlx5_ib_bind_slave_port(dev, mpi);
3315 dev_dbg(mpi->mdev->device,
3316 "removing port from unaffiliated list.\n");
3317 mlx5_ib_dbg(dev, "port %d bound\n", i + 1);
3318 list_del(&mpi->list);
3323 mlx5_ib_dbg(dev, "no free port found for port %d\n",
3327 list_add_tail(&dev->ib_dev_list, &mlx5_ib_dev_list);
3328 mutex_unlock(&mlx5_ib_multiport_mutex);
3332 static void mlx5_ib_cleanup_multiport_master(struct mlx5_ib_dev *dev)
3334 u32 port_num = mlx5_core_native_port_num(dev->mdev) - 1;
3335 enum rdma_link_layer ll = mlx5_ib_port_link_layer(&dev->ib_dev,
3339 if (!mlx5_core_is_mp_master(dev->mdev) || ll != IB_LINK_LAYER_ETHERNET)
3342 mutex_lock(&mlx5_ib_multiport_mutex);
3343 for (i = 0; i < dev->num_ports; i++) {
3344 if (dev->port[i].mp.mpi) {
3345 /* Destroy the native port stub */
3346 if (i == port_num) {
3347 kfree(dev->port[i].mp.mpi);
3348 dev->port[i].mp.mpi = NULL;
3350 mlx5_ib_dbg(dev, "unbinding port_num: %u\n",
3352 list_add_tail(&dev->port[i].mp.mpi->list,
3353 &mlx5_ib_unaffiliated_port_list);
3354 mlx5_ib_unbind_slave_port(dev,
3355 dev->port[i].mp.mpi);
3360 mlx5_ib_dbg(dev, "removing from devlist\n");
3361 list_del(&dev->ib_dev_list);
3362 mutex_unlock(&mlx5_ib_multiport_mutex);
3364 mlx5_nic_vport_disable_roce(dev->mdev);
3367 static int mmap_obj_cleanup(struct ib_uobject *uobject,
3368 enum rdma_remove_reason why,
3369 struct uverbs_attr_bundle *attrs)
3371 struct mlx5_user_mmap_entry *obj = uobject->object;
3373 rdma_user_mmap_entry_remove(&obj->rdma_entry);
3377 static int mlx5_rdma_user_mmap_entry_insert(struct mlx5_ib_ucontext *c,
3378 struct mlx5_user_mmap_entry *entry,
3381 return rdma_user_mmap_entry_insert_range(
3382 &c->ibucontext, &entry->rdma_entry, length,
3383 (MLX5_IB_MMAP_OFFSET_START << 16),
3384 ((MLX5_IB_MMAP_OFFSET_END << 16) + (1UL << 16) - 1));
3387 static struct mlx5_user_mmap_entry *
3388 alloc_var_entry(struct mlx5_ib_ucontext *c)
3390 struct mlx5_user_mmap_entry *entry;
3391 struct mlx5_var_table *var_table;
3395 var_table = &to_mdev(c->ibucontext.device)->var_table;
3396 entry = kzalloc(sizeof(*entry), GFP_KERNEL);
3398 return ERR_PTR(-ENOMEM);
3400 mutex_lock(&var_table->bitmap_lock);
3401 page_idx = find_first_zero_bit(var_table->bitmap,
3402 var_table->num_var_hw_entries);
3403 if (page_idx >= var_table->num_var_hw_entries) {
3405 mutex_unlock(&var_table->bitmap_lock);
3409 set_bit(page_idx, var_table->bitmap);
3410 mutex_unlock(&var_table->bitmap_lock);
3412 entry->address = var_table->hw_start_addr +
3413 (page_idx * var_table->stride_size);
3414 entry->page_idx = page_idx;
3415 entry->mmap_flag = MLX5_IB_MMAP_TYPE_VAR;
3417 err = mlx5_rdma_user_mmap_entry_insert(c, entry,
3418 var_table->stride_size);
3425 mutex_lock(&var_table->bitmap_lock);
3426 clear_bit(page_idx, var_table->bitmap);
3427 mutex_unlock(&var_table->bitmap_lock);
3430 return ERR_PTR(err);
3433 static int UVERBS_HANDLER(MLX5_IB_METHOD_VAR_OBJ_ALLOC)(
3434 struct uverbs_attr_bundle *attrs)
3436 struct ib_uobject *uobj = uverbs_attr_get_uobject(
3437 attrs, MLX5_IB_ATTR_VAR_OBJ_ALLOC_HANDLE);
3438 struct mlx5_ib_ucontext *c;
3439 struct mlx5_user_mmap_entry *entry;
3444 c = to_mucontext(ib_uverbs_get_ucontext(attrs));
3448 entry = alloc_var_entry(c);
3450 return PTR_ERR(entry);
3452 mmap_offset = mlx5_entry_to_mmap_offset(entry);
3453 length = entry->rdma_entry.npages * PAGE_SIZE;
3454 uobj->object = entry;
3455 uverbs_finalize_uobj_create(attrs, MLX5_IB_ATTR_VAR_OBJ_ALLOC_HANDLE);
3457 err = uverbs_copy_to(attrs, MLX5_IB_ATTR_VAR_OBJ_ALLOC_MMAP_OFFSET,
3458 &mmap_offset, sizeof(mmap_offset));
3462 err = uverbs_copy_to(attrs, MLX5_IB_ATTR_VAR_OBJ_ALLOC_PAGE_ID,
3463 &entry->page_idx, sizeof(entry->page_idx));
3467 err = uverbs_copy_to(attrs, MLX5_IB_ATTR_VAR_OBJ_ALLOC_MMAP_LENGTH,
3468 &length, sizeof(length));
3472 DECLARE_UVERBS_NAMED_METHOD(
3473 MLX5_IB_METHOD_VAR_OBJ_ALLOC,
3474 UVERBS_ATTR_IDR(MLX5_IB_ATTR_VAR_OBJ_ALLOC_HANDLE,
3478 UVERBS_ATTR_PTR_OUT(MLX5_IB_ATTR_VAR_OBJ_ALLOC_PAGE_ID,
3479 UVERBS_ATTR_TYPE(u32),
3481 UVERBS_ATTR_PTR_OUT(MLX5_IB_ATTR_VAR_OBJ_ALLOC_MMAP_LENGTH,
3482 UVERBS_ATTR_TYPE(u32),
3484 UVERBS_ATTR_PTR_OUT(MLX5_IB_ATTR_VAR_OBJ_ALLOC_MMAP_OFFSET,
3485 UVERBS_ATTR_TYPE(u64),
3488 DECLARE_UVERBS_NAMED_METHOD_DESTROY(
3489 MLX5_IB_METHOD_VAR_OBJ_DESTROY,
3490 UVERBS_ATTR_IDR(MLX5_IB_ATTR_VAR_OBJ_DESTROY_HANDLE,
3492 UVERBS_ACCESS_DESTROY,
3495 DECLARE_UVERBS_NAMED_OBJECT(MLX5_IB_OBJECT_VAR,
3496 UVERBS_TYPE_ALLOC_IDR(mmap_obj_cleanup),
3497 &UVERBS_METHOD(MLX5_IB_METHOD_VAR_OBJ_ALLOC),
3498 &UVERBS_METHOD(MLX5_IB_METHOD_VAR_OBJ_DESTROY));
3500 static bool var_is_supported(struct ib_device *device)
3502 struct mlx5_ib_dev *dev = to_mdev(device);
3504 return (MLX5_CAP_GEN_64(dev->mdev, general_obj_types) &
3505 MLX5_GENERAL_OBJ_TYPES_CAP_VIRTIO_NET_Q);
3508 static struct mlx5_user_mmap_entry *
3509 alloc_uar_entry(struct mlx5_ib_ucontext *c,
3510 enum mlx5_ib_uapi_uar_alloc_type alloc_type)
3512 struct mlx5_user_mmap_entry *entry;
3513 struct mlx5_ib_dev *dev;
3517 entry = kzalloc(sizeof(*entry), GFP_KERNEL);
3519 return ERR_PTR(-ENOMEM);
3521 dev = to_mdev(c->ibucontext.device);
3522 err = mlx5_cmd_uar_alloc(dev->mdev, &uar_index, c->devx_uid);
3526 entry->page_idx = uar_index;
3527 entry->address = uar_index2paddress(dev, uar_index);
3528 if (alloc_type == MLX5_IB_UAPI_UAR_ALLOC_TYPE_BF)
3529 entry->mmap_flag = MLX5_IB_MMAP_TYPE_UAR_WC;
3531 entry->mmap_flag = MLX5_IB_MMAP_TYPE_UAR_NC;
3533 err = mlx5_rdma_user_mmap_entry_insert(c, entry, PAGE_SIZE);
3540 mlx5_cmd_uar_dealloc(dev->mdev, uar_index, c->devx_uid);
3543 return ERR_PTR(err);
3546 static int UVERBS_HANDLER(MLX5_IB_METHOD_UAR_OBJ_ALLOC)(
3547 struct uverbs_attr_bundle *attrs)
3549 struct ib_uobject *uobj = uverbs_attr_get_uobject(
3550 attrs, MLX5_IB_ATTR_UAR_OBJ_ALLOC_HANDLE);
3551 enum mlx5_ib_uapi_uar_alloc_type alloc_type;
3552 struct mlx5_ib_ucontext *c;
3553 struct mlx5_user_mmap_entry *entry;
3558 c = to_mucontext(ib_uverbs_get_ucontext(attrs));
3562 err = uverbs_get_const(&alloc_type, attrs,
3563 MLX5_IB_ATTR_UAR_OBJ_ALLOC_TYPE);
3567 if (alloc_type != MLX5_IB_UAPI_UAR_ALLOC_TYPE_BF &&
3568 alloc_type != MLX5_IB_UAPI_UAR_ALLOC_TYPE_NC)
3571 if (!to_mdev(c->ibucontext.device)->wc_support &&
3572 alloc_type == MLX5_IB_UAPI_UAR_ALLOC_TYPE_BF)
3575 entry = alloc_uar_entry(c, alloc_type);
3577 return PTR_ERR(entry);
3579 mmap_offset = mlx5_entry_to_mmap_offset(entry);
3580 length = entry->rdma_entry.npages * PAGE_SIZE;
3581 uobj->object = entry;
3582 uverbs_finalize_uobj_create(attrs, MLX5_IB_ATTR_UAR_OBJ_ALLOC_HANDLE);
3584 err = uverbs_copy_to(attrs, MLX5_IB_ATTR_UAR_OBJ_ALLOC_MMAP_OFFSET,
3585 &mmap_offset, sizeof(mmap_offset));
3589 err = uverbs_copy_to(attrs, MLX5_IB_ATTR_UAR_OBJ_ALLOC_PAGE_ID,
3590 &entry->page_idx, sizeof(entry->page_idx));
3594 err = uverbs_copy_to(attrs, MLX5_IB_ATTR_UAR_OBJ_ALLOC_MMAP_LENGTH,
3595 &length, sizeof(length));
3599 DECLARE_UVERBS_NAMED_METHOD(
3600 MLX5_IB_METHOD_UAR_OBJ_ALLOC,
3601 UVERBS_ATTR_IDR(MLX5_IB_ATTR_UAR_OBJ_ALLOC_HANDLE,
3605 UVERBS_ATTR_CONST_IN(MLX5_IB_ATTR_UAR_OBJ_ALLOC_TYPE,
3606 enum mlx5_ib_uapi_uar_alloc_type,
3608 UVERBS_ATTR_PTR_OUT(MLX5_IB_ATTR_UAR_OBJ_ALLOC_PAGE_ID,
3609 UVERBS_ATTR_TYPE(u32),
3611 UVERBS_ATTR_PTR_OUT(MLX5_IB_ATTR_UAR_OBJ_ALLOC_MMAP_LENGTH,
3612 UVERBS_ATTR_TYPE(u32),
3614 UVERBS_ATTR_PTR_OUT(MLX5_IB_ATTR_UAR_OBJ_ALLOC_MMAP_OFFSET,
3615 UVERBS_ATTR_TYPE(u64),
3618 DECLARE_UVERBS_NAMED_METHOD_DESTROY(
3619 MLX5_IB_METHOD_UAR_OBJ_DESTROY,
3620 UVERBS_ATTR_IDR(MLX5_IB_ATTR_UAR_OBJ_DESTROY_HANDLE,
3622 UVERBS_ACCESS_DESTROY,
3625 DECLARE_UVERBS_NAMED_OBJECT(MLX5_IB_OBJECT_UAR,
3626 UVERBS_TYPE_ALLOC_IDR(mmap_obj_cleanup),
3627 &UVERBS_METHOD(MLX5_IB_METHOD_UAR_OBJ_ALLOC),
3628 &UVERBS_METHOD(MLX5_IB_METHOD_UAR_OBJ_DESTROY));
3630 ADD_UVERBS_ATTRIBUTES_SIMPLE(
3631 mlx5_ib_query_context,
3632 UVERBS_OBJECT_DEVICE,
3633 UVERBS_METHOD_QUERY_CONTEXT,
3634 UVERBS_ATTR_PTR_OUT(
3635 MLX5_IB_ATTR_QUERY_CONTEXT_RESP_UCTX,
3636 UVERBS_ATTR_STRUCT(struct mlx5_ib_alloc_ucontext_resp,
3640 static const struct uapi_definition mlx5_ib_defs[] = {
3641 UAPI_DEF_CHAIN(mlx5_ib_devx_defs),
3642 UAPI_DEF_CHAIN(mlx5_ib_flow_defs),
3643 UAPI_DEF_CHAIN(mlx5_ib_qos_defs),
3644 UAPI_DEF_CHAIN(mlx5_ib_std_types_defs),
3645 UAPI_DEF_CHAIN(mlx5_ib_dm_defs),
3647 UAPI_DEF_CHAIN_OBJ_TREE(UVERBS_OBJECT_DEVICE, &mlx5_ib_query_context),
3648 UAPI_DEF_CHAIN_OBJ_TREE_NAMED(MLX5_IB_OBJECT_VAR,
3649 UAPI_DEF_IS_OBJ_SUPPORTED(var_is_supported)),
3650 UAPI_DEF_CHAIN_OBJ_TREE_NAMED(MLX5_IB_OBJECT_UAR),
3654 static void mlx5_ib_stage_init_cleanup(struct mlx5_ib_dev *dev)
3656 mlx5_ib_cleanup_multiport_master(dev);
3657 WARN_ON(!xa_empty(&dev->odp_mkeys));
3658 mutex_destroy(&dev->cap_mask_mutex);
3659 WARN_ON(!xa_empty(&dev->sig_mrs));
3660 WARN_ON(!bitmap_empty(dev->dm.memic_alloc_pages, MLX5_MAX_MEMIC_PAGES));
3661 mlx5r_macsec_dealloc_gids(dev);
3664 static int mlx5_ib_stage_init_init(struct mlx5_ib_dev *dev)
3666 struct mlx5_core_dev *mdev = dev->mdev;
3669 dev->ib_dev.node_type = RDMA_NODE_IB_CA;
3670 dev->ib_dev.local_dma_lkey = 0 /* not supported for now */;
3671 dev->ib_dev.phys_port_cnt = dev->num_ports;
3672 dev->ib_dev.dev.parent = mdev->device;
3673 dev->ib_dev.lag_flags = RDMA_LAG_FLAGS_HASH_ALL_SLAVES;
3675 for (i = 0; i < dev->num_ports; i++) {
3676 spin_lock_init(&dev->port[i].mp.mpi_lock);
3677 rwlock_init(&dev->port[i].roce.netdev_lock);
3678 dev->port[i].roce.dev = dev;
3679 dev->port[i].roce.native_port_num = i + 1;
3680 dev->port[i].roce.last_port_state = IB_PORT_DOWN;
3683 err = mlx5r_cmd_query_special_mkeys(dev);
3687 err = mlx5r_macsec_init_gids_and_devlist(dev);
3691 err = mlx5_ib_init_multiport_master(dev);
3695 err = set_has_smi_cap(dev);
3699 err = mlx5_query_max_pkeys(&dev->ib_dev, &dev->pkey_table_len);
3703 if (mlx5_use_mad_ifc(dev))
3704 get_ext_port_caps(dev);
3706 dev->ib_dev.num_comp_vectors = mlx5_comp_vectors_max(mdev);
3708 mutex_init(&dev->cap_mask_mutex);
3709 INIT_LIST_HEAD(&dev->qp_list);
3710 spin_lock_init(&dev->reset_flow_resource_lock);
3711 xa_init(&dev->odp_mkeys);
3712 xa_init(&dev->sig_mrs);
3713 atomic_set(&dev->mkey_var, 0);
3715 spin_lock_init(&dev->dm.lock);
3719 mlx5r_macsec_dealloc_gids(dev);
3721 mlx5_ib_cleanup_multiport_master(dev);
3725 static int mlx5_ib_enable_driver(struct ib_device *dev)
3727 struct mlx5_ib_dev *mdev = to_mdev(dev);
3730 ret = mlx5_ib_test_wc(mdev);
3731 mlx5_ib_dbg(mdev, "Write-Combining %s",
3732 mdev->wc_support ? "supported" : "not supported");
3737 static const struct ib_device_ops mlx5_ib_dev_ops = {
3738 .owner = THIS_MODULE,
3739 .driver_id = RDMA_DRIVER_MLX5,
3740 .uverbs_abi_ver = MLX5_IB_UVERBS_ABI_VERSION,
3742 .add_gid = mlx5_ib_add_gid,
3743 .alloc_mr = mlx5_ib_alloc_mr,
3744 .alloc_mr_integrity = mlx5_ib_alloc_mr_integrity,
3745 .alloc_pd = mlx5_ib_alloc_pd,
3746 .alloc_ucontext = mlx5_ib_alloc_ucontext,
3747 .attach_mcast = mlx5_ib_mcg_attach,
3748 .check_mr_status = mlx5_ib_check_mr_status,
3749 .create_ah = mlx5_ib_create_ah,
3750 .create_cq = mlx5_ib_create_cq,
3751 .create_qp = mlx5_ib_create_qp,
3752 .create_srq = mlx5_ib_create_srq,
3753 .create_user_ah = mlx5_ib_create_ah,
3754 .dealloc_pd = mlx5_ib_dealloc_pd,
3755 .dealloc_ucontext = mlx5_ib_dealloc_ucontext,
3756 .del_gid = mlx5_ib_del_gid,
3757 .dereg_mr = mlx5_ib_dereg_mr,
3758 .destroy_ah = mlx5_ib_destroy_ah,
3759 .destroy_cq = mlx5_ib_destroy_cq,
3760 .destroy_qp = mlx5_ib_destroy_qp,
3761 .destroy_srq = mlx5_ib_destroy_srq,
3762 .detach_mcast = mlx5_ib_mcg_detach,
3763 .disassociate_ucontext = mlx5_ib_disassociate_ucontext,
3764 .drain_rq = mlx5_ib_drain_rq,
3765 .drain_sq = mlx5_ib_drain_sq,
3766 .device_group = &mlx5_attr_group,
3767 .enable_driver = mlx5_ib_enable_driver,
3768 .get_dev_fw_str = get_dev_fw_str,
3769 .get_dma_mr = mlx5_ib_get_dma_mr,
3770 .get_link_layer = mlx5_ib_port_link_layer,
3771 .map_mr_sg = mlx5_ib_map_mr_sg,
3772 .map_mr_sg_pi = mlx5_ib_map_mr_sg_pi,
3773 .mmap = mlx5_ib_mmap,
3774 .mmap_free = mlx5_ib_mmap_free,
3775 .modify_cq = mlx5_ib_modify_cq,
3776 .modify_device = mlx5_ib_modify_device,
3777 .modify_port = mlx5_ib_modify_port,
3778 .modify_qp = mlx5_ib_modify_qp,
3779 .modify_srq = mlx5_ib_modify_srq,
3780 .poll_cq = mlx5_ib_poll_cq,
3781 .post_recv = mlx5_ib_post_recv_nodrain,
3782 .post_send = mlx5_ib_post_send_nodrain,
3783 .post_srq_recv = mlx5_ib_post_srq_recv,
3784 .process_mad = mlx5_ib_process_mad,
3785 .query_ah = mlx5_ib_query_ah,
3786 .query_device = mlx5_ib_query_device,
3787 .query_gid = mlx5_ib_query_gid,
3788 .query_pkey = mlx5_ib_query_pkey,
3789 .query_qp = mlx5_ib_query_qp,
3790 .query_srq = mlx5_ib_query_srq,
3791 .query_ucontext = mlx5_ib_query_ucontext,
3792 .reg_user_mr = mlx5_ib_reg_user_mr,
3793 .reg_user_mr_dmabuf = mlx5_ib_reg_user_mr_dmabuf,
3794 .req_notify_cq = mlx5_ib_arm_cq,
3795 .rereg_user_mr = mlx5_ib_rereg_user_mr,
3796 .resize_cq = mlx5_ib_resize_cq,
3798 INIT_RDMA_OBJ_SIZE(ib_ah, mlx5_ib_ah, ibah),
3799 INIT_RDMA_OBJ_SIZE(ib_counters, mlx5_ib_mcounters, ibcntrs),
3800 INIT_RDMA_OBJ_SIZE(ib_cq, mlx5_ib_cq, ibcq),
3801 INIT_RDMA_OBJ_SIZE(ib_pd, mlx5_ib_pd, ibpd),
3802 INIT_RDMA_OBJ_SIZE(ib_qp, mlx5_ib_qp, ibqp),
3803 INIT_RDMA_OBJ_SIZE(ib_srq, mlx5_ib_srq, ibsrq),
3804 INIT_RDMA_OBJ_SIZE(ib_ucontext, mlx5_ib_ucontext, ibucontext),
3807 static const struct ib_device_ops mlx5_ib_dev_ipoib_enhanced_ops = {
3808 .rdma_netdev_get_params = mlx5_ib_rn_get_params,
3811 static const struct ib_device_ops mlx5_ib_dev_sriov_ops = {
3812 .get_vf_config = mlx5_ib_get_vf_config,
3813 .get_vf_guid = mlx5_ib_get_vf_guid,
3814 .get_vf_stats = mlx5_ib_get_vf_stats,
3815 .set_vf_guid = mlx5_ib_set_vf_guid,
3816 .set_vf_link_state = mlx5_ib_set_vf_link_state,
3819 static const struct ib_device_ops mlx5_ib_dev_mw_ops = {
3820 .alloc_mw = mlx5_ib_alloc_mw,
3821 .dealloc_mw = mlx5_ib_dealloc_mw,
3823 INIT_RDMA_OBJ_SIZE(ib_mw, mlx5_ib_mw, ibmw),
3826 static const struct ib_device_ops mlx5_ib_dev_xrc_ops = {
3827 .alloc_xrcd = mlx5_ib_alloc_xrcd,
3828 .dealloc_xrcd = mlx5_ib_dealloc_xrcd,
3830 INIT_RDMA_OBJ_SIZE(ib_xrcd, mlx5_ib_xrcd, ibxrcd),
3833 static int mlx5_ib_init_var_table(struct mlx5_ib_dev *dev)
3835 struct mlx5_core_dev *mdev = dev->mdev;
3836 struct mlx5_var_table *var_table = &dev->var_table;
3837 u8 log_doorbell_bar_size;
3838 u8 log_doorbell_stride;
3841 log_doorbell_bar_size = MLX5_CAP_DEV_VDPA_EMULATION(mdev,
3842 log_doorbell_bar_size);
3843 log_doorbell_stride = MLX5_CAP_DEV_VDPA_EMULATION(mdev,
3844 log_doorbell_stride);
3845 var_table->hw_start_addr = dev->mdev->bar_addr +
3846 MLX5_CAP64_DEV_VDPA_EMULATION(mdev,
3847 doorbell_bar_offset);
3848 bar_size = (1ULL << log_doorbell_bar_size) * 4096;
3849 var_table->stride_size = 1ULL << log_doorbell_stride;
3850 var_table->num_var_hw_entries = div_u64(bar_size,
3851 var_table->stride_size);
3852 mutex_init(&var_table->bitmap_lock);
3853 var_table->bitmap = bitmap_zalloc(var_table->num_var_hw_entries,
3855 return (var_table->bitmap) ? 0 : -ENOMEM;
3858 static void mlx5_ib_stage_caps_cleanup(struct mlx5_ib_dev *dev)
3860 bitmap_free(dev->var_table.bitmap);
3863 static int mlx5_ib_stage_caps_init(struct mlx5_ib_dev *dev)
3865 struct mlx5_core_dev *mdev = dev->mdev;
3868 if (MLX5_CAP_GEN(mdev, ipoib_enhanced_offloads) &&
3869 IS_ENABLED(CONFIG_MLX5_CORE_IPOIB))
3870 ib_set_device_ops(&dev->ib_dev,
3871 &mlx5_ib_dev_ipoib_enhanced_ops);
3873 if (mlx5_core_is_pf(mdev))
3874 ib_set_device_ops(&dev->ib_dev, &mlx5_ib_dev_sriov_ops);
3876 dev->umr_fence = mlx5_get_umr_fence(MLX5_CAP_GEN(mdev, umr_fence));
3878 if (MLX5_CAP_GEN(mdev, imaicl))
3879 ib_set_device_ops(&dev->ib_dev, &mlx5_ib_dev_mw_ops);
3881 if (MLX5_CAP_GEN(mdev, xrc))
3882 ib_set_device_ops(&dev->ib_dev, &mlx5_ib_dev_xrc_ops);
3884 if (MLX5_CAP_DEV_MEM(mdev, memic) ||
3885 MLX5_CAP_GEN_64(dev->mdev, general_obj_types) &
3886 MLX5_GENERAL_OBJ_TYPES_CAP_SW_ICM)
3887 ib_set_device_ops(&dev->ib_dev, &mlx5_ib_dev_dm_ops);
3889 ib_set_device_ops(&dev->ib_dev, &mlx5_ib_dev_ops);
3891 if (IS_ENABLED(CONFIG_INFINIBAND_USER_ACCESS))
3892 dev->ib_dev.driver_def = mlx5_ib_defs;
3894 err = init_node_data(dev);
3898 if ((MLX5_CAP_GEN(dev->mdev, port_type) == MLX5_CAP_PORT_TYPE_ETH) &&
3899 (MLX5_CAP_GEN(dev->mdev, disable_local_lb_uc) ||
3900 MLX5_CAP_GEN(dev->mdev, disable_local_lb_mc)))
3901 mutex_init(&dev->lb.mutex);
3903 if (MLX5_CAP_GEN_64(dev->mdev, general_obj_types) &
3904 MLX5_GENERAL_OBJ_TYPES_CAP_VIRTIO_NET_Q) {
3905 err = mlx5_ib_init_var_table(dev);
3910 dev->ib_dev.use_cq_dim = true;
3915 static const struct ib_device_ops mlx5_ib_dev_port_ops = {
3916 .get_port_immutable = mlx5_port_immutable,
3917 .query_port = mlx5_ib_query_port,
3920 static int mlx5_ib_stage_non_default_cb(struct mlx5_ib_dev *dev)
3922 ib_set_device_ops(&dev->ib_dev, &mlx5_ib_dev_port_ops);
3926 static const struct ib_device_ops mlx5_ib_dev_port_rep_ops = {
3927 .get_port_immutable = mlx5_port_rep_immutable,
3928 .query_port = mlx5_ib_rep_query_port,
3929 .query_pkey = mlx5_ib_rep_query_pkey,
3932 static int mlx5_ib_stage_raw_eth_non_default_cb(struct mlx5_ib_dev *dev)
3934 ib_set_device_ops(&dev->ib_dev, &mlx5_ib_dev_port_rep_ops);
3938 static const struct ib_device_ops mlx5_ib_dev_common_roce_ops = {
3939 .create_rwq_ind_table = mlx5_ib_create_rwq_ind_table,
3940 .create_wq = mlx5_ib_create_wq,
3941 .destroy_rwq_ind_table = mlx5_ib_destroy_rwq_ind_table,
3942 .destroy_wq = mlx5_ib_destroy_wq,
3943 .get_netdev = mlx5_ib_get_netdev,
3944 .modify_wq = mlx5_ib_modify_wq,
3946 INIT_RDMA_OBJ_SIZE(ib_rwq_ind_table, mlx5_ib_rwq_ind_table,
3950 static int mlx5_ib_roce_init(struct mlx5_ib_dev *dev)
3952 struct mlx5_core_dev *mdev = dev->mdev;
3953 enum rdma_link_layer ll;
3958 port_type_cap = MLX5_CAP_GEN(mdev, port_type);
3959 ll = mlx5_port_type_cap_to_rdma_ll(port_type_cap);
3961 if (ll == IB_LINK_LAYER_ETHERNET) {
3962 ib_set_device_ops(&dev->ib_dev, &mlx5_ib_dev_common_roce_ops);
3964 port_num = mlx5_core_native_port_num(dev->mdev) - 1;
3966 /* Register only for native ports */
3967 mlx5_mdev_netdev_track(dev, port_num);
3969 err = mlx5_enable_eth(dev);
3976 mlx5_mdev_netdev_untrack(dev, port_num);
3980 static void mlx5_ib_roce_cleanup(struct mlx5_ib_dev *dev)
3982 struct mlx5_core_dev *mdev = dev->mdev;
3983 enum rdma_link_layer ll;
3987 port_type_cap = MLX5_CAP_GEN(mdev, port_type);
3988 ll = mlx5_port_type_cap_to_rdma_ll(port_type_cap);
3990 if (ll == IB_LINK_LAYER_ETHERNET) {
3991 mlx5_disable_eth(dev);
3993 port_num = mlx5_core_native_port_num(dev->mdev) - 1;
3994 mlx5_mdev_netdev_untrack(dev, port_num);
3998 static int mlx5_ib_stage_cong_debugfs_init(struct mlx5_ib_dev *dev)
4000 mlx5_ib_init_cong_debugfs(dev,
4001 mlx5_core_native_port_num(dev->mdev) - 1);
4005 static void mlx5_ib_stage_cong_debugfs_cleanup(struct mlx5_ib_dev *dev)
4007 mlx5_ib_cleanup_cong_debugfs(dev,
4008 mlx5_core_native_port_num(dev->mdev) - 1);
4011 static int mlx5_ib_stage_uar_init(struct mlx5_ib_dev *dev)
4013 dev->mdev->priv.uar = mlx5_get_uars_page(dev->mdev);
4014 return PTR_ERR_OR_ZERO(dev->mdev->priv.uar);
4017 static void mlx5_ib_stage_uar_cleanup(struct mlx5_ib_dev *dev)
4019 mlx5_put_uars_page(dev->mdev, dev->mdev->priv.uar);
4022 static int mlx5_ib_stage_bfrag_init(struct mlx5_ib_dev *dev)
4026 err = mlx5_alloc_bfreg(dev->mdev, &dev->bfreg, false, false);
4030 err = mlx5_alloc_bfreg(dev->mdev, &dev->fp_bfreg, false, true);
4032 mlx5_free_bfreg(dev->mdev, &dev->bfreg);
4037 static void mlx5_ib_stage_bfrag_cleanup(struct mlx5_ib_dev *dev)
4039 mlx5_free_bfreg(dev->mdev, &dev->fp_bfreg);
4040 mlx5_free_bfreg(dev->mdev, &dev->bfreg);
4043 static int mlx5_ib_stage_ib_reg_init(struct mlx5_ib_dev *dev)
4047 if (!mlx5_lag_is_active(dev->mdev))
4050 name = "mlx5_bond_%d";
4051 return ib_register_device(&dev->ib_dev, name, &dev->mdev->pdev->dev);
4054 static void mlx5_ib_stage_pre_ib_reg_umr_cleanup(struct mlx5_ib_dev *dev)
4056 mlx5_mkey_cache_cleanup(dev);
4057 mlx5r_umr_resource_cleanup(dev);
4060 static void mlx5_ib_stage_ib_reg_cleanup(struct mlx5_ib_dev *dev)
4062 ib_unregister_device(&dev->ib_dev);
4065 static int mlx5_ib_stage_post_ib_reg_umr_init(struct mlx5_ib_dev *dev)
4069 ret = mlx5r_umr_resource_init(dev);
4073 ret = mlx5_mkey_cache_init(dev);
4075 mlx5_ib_warn(dev, "mr cache init failed %d\n", ret);
4076 mlx5r_umr_resource_cleanup(dev);
4081 static int mlx5_ib_stage_delay_drop_init(struct mlx5_ib_dev *dev)
4083 struct dentry *root;
4085 if (!(dev->ib_dev.attrs.raw_packet_caps & IB_RAW_PACKET_CAP_DELAY_DROP))
4088 mutex_init(&dev->delay_drop.lock);
4089 dev->delay_drop.dev = dev;
4090 dev->delay_drop.activate = false;
4091 dev->delay_drop.timeout = MLX5_MAX_DELAY_DROP_TIMEOUT_MS * 1000;
4092 INIT_WORK(&dev->delay_drop.delay_drop_work, delay_drop_handler);
4093 atomic_set(&dev->delay_drop.rqs_cnt, 0);
4094 atomic_set(&dev->delay_drop.events_cnt, 0);
4096 if (!mlx5_debugfs_root)
4099 root = debugfs_create_dir("delay_drop", mlx5_debugfs_get_dev_root(dev->mdev));
4100 dev->delay_drop.dir_debugfs = root;
4102 debugfs_create_atomic_t("num_timeout_events", 0400, root,
4103 &dev->delay_drop.events_cnt);
4104 debugfs_create_atomic_t("num_rqs", 0400, root,
4105 &dev->delay_drop.rqs_cnt);
4106 debugfs_create_file("timeout", 0600, root, &dev->delay_drop,
4107 &fops_delay_drop_timeout);
4111 static void mlx5_ib_stage_delay_drop_cleanup(struct mlx5_ib_dev *dev)
4113 if (!(dev->ib_dev.attrs.raw_packet_caps & IB_RAW_PACKET_CAP_DELAY_DROP))
4116 cancel_work_sync(&dev->delay_drop.delay_drop_work);
4117 if (!dev->delay_drop.dir_debugfs)
4120 debugfs_remove_recursive(dev->delay_drop.dir_debugfs);
4121 dev->delay_drop.dir_debugfs = NULL;
4124 static int mlx5_ib_stage_dev_notifier_init(struct mlx5_ib_dev *dev)
4126 dev->mdev_events.notifier_call = mlx5_ib_event;
4127 mlx5_notifier_register(dev->mdev, &dev->mdev_events);
4129 mlx5r_macsec_event_register(dev);
4134 static void mlx5_ib_stage_dev_notifier_cleanup(struct mlx5_ib_dev *dev)
4136 mlx5r_macsec_event_unregister(dev);
4137 mlx5_notifier_unregister(dev->mdev, &dev->mdev_events);
4140 void __mlx5_ib_remove(struct mlx5_ib_dev *dev,
4141 const struct mlx5_ib_profile *profile,
4144 dev->ib_active = false;
4146 /* Number of stages to cleanup */
4149 if (profile->stage[stage].cleanup)
4150 profile->stage[stage].cleanup(dev);
4154 ib_dealloc_device(&dev->ib_dev);
4157 int __mlx5_ib_add(struct mlx5_ib_dev *dev,
4158 const struct mlx5_ib_profile *profile)
4163 dev->profile = profile;
4165 for (i = 0; i < MLX5_IB_STAGE_MAX; i++) {
4166 if (profile->stage[i].init) {
4167 err = profile->stage[i].init(dev);
4173 dev->ib_active = true;
4177 /* Clean up stages which were initialized */
4180 if (profile->stage[i].cleanup)
4181 profile->stage[i].cleanup(dev);
4186 static const struct mlx5_ib_profile pf_profile = {
4187 STAGE_CREATE(MLX5_IB_STAGE_INIT,
4188 mlx5_ib_stage_init_init,
4189 mlx5_ib_stage_init_cleanup),
4190 STAGE_CREATE(MLX5_IB_STAGE_FS,
4192 mlx5_ib_fs_cleanup),
4193 STAGE_CREATE(MLX5_IB_STAGE_CAPS,
4194 mlx5_ib_stage_caps_init,
4195 mlx5_ib_stage_caps_cleanup),
4196 STAGE_CREATE(MLX5_IB_STAGE_NON_DEFAULT_CB,
4197 mlx5_ib_stage_non_default_cb,
4199 STAGE_CREATE(MLX5_IB_STAGE_ROCE,
4201 mlx5_ib_roce_cleanup),
4202 STAGE_CREATE(MLX5_IB_STAGE_QP,
4204 mlx5_cleanup_qp_table),
4205 STAGE_CREATE(MLX5_IB_STAGE_SRQ,
4206 mlx5_init_srq_table,
4207 mlx5_cleanup_srq_table),
4208 STAGE_CREATE(MLX5_IB_STAGE_DEVICE_RESOURCES,
4209 mlx5_ib_dev_res_init,
4210 mlx5_ib_dev_res_cleanup),
4211 STAGE_CREATE(MLX5_IB_STAGE_DEVICE_NOTIFIER,
4212 mlx5_ib_stage_dev_notifier_init,
4213 mlx5_ib_stage_dev_notifier_cleanup),
4214 STAGE_CREATE(MLX5_IB_STAGE_ODP,
4215 mlx5_ib_odp_init_one,
4216 mlx5_ib_odp_cleanup_one),
4217 STAGE_CREATE(MLX5_IB_STAGE_COUNTERS,
4218 mlx5_ib_counters_init,
4219 mlx5_ib_counters_cleanup),
4220 STAGE_CREATE(MLX5_IB_STAGE_CONG_DEBUGFS,
4221 mlx5_ib_stage_cong_debugfs_init,
4222 mlx5_ib_stage_cong_debugfs_cleanup),
4223 STAGE_CREATE(MLX5_IB_STAGE_UAR,
4224 mlx5_ib_stage_uar_init,
4225 mlx5_ib_stage_uar_cleanup),
4226 STAGE_CREATE(MLX5_IB_STAGE_BFREG,
4227 mlx5_ib_stage_bfrag_init,
4228 mlx5_ib_stage_bfrag_cleanup),
4229 STAGE_CREATE(MLX5_IB_STAGE_PRE_IB_REG_UMR,
4231 mlx5_ib_stage_pre_ib_reg_umr_cleanup),
4232 STAGE_CREATE(MLX5_IB_STAGE_WHITELIST_UID,
4234 mlx5_ib_devx_cleanup),
4235 STAGE_CREATE(MLX5_IB_STAGE_IB_REG,
4236 mlx5_ib_stage_ib_reg_init,
4237 mlx5_ib_stage_ib_reg_cleanup),
4238 STAGE_CREATE(MLX5_IB_STAGE_POST_IB_REG_UMR,
4239 mlx5_ib_stage_post_ib_reg_umr_init,
4241 STAGE_CREATE(MLX5_IB_STAGE_DELAY_DROP,
4242 mlx5_ib_stage_delay_drop_init,
4243 mlx5_ib_stage_delay_drop_cleanup),
4244 STAGE_CREATE(MLX5_IB_STAGE_RESTRACK,
4245 mlx5_ib_restrack_init,
4249 const struct mlx5_ib_profile raw_eth_profile = {
4250 STAGE_CREATE(MLX5_IB_STAGE_INIT,
4251 mlx5_ib_stage_init_init,
4252 mlx5_ib_stage_init_cleanup),
4253 STAGE_CREATE(MLX5_IB_STAGE_FS,
4255 mlx5_ib_fs_cleanup),
4256 STAGE_CREATE(MLX5_IB_STAGE_CAPS,
4257 mlx5_ib_stage_caps_init,
4258 mlx5_ib_stage_caps_cleanup),
4259 STAGE_CREATE(MLX5_IB_STAGE_NON_DEFAULT_CB,
4260 mlx5_ib_stage_raw_eth_non_default_cb,
4262 STAGE_CREATE(MLX5_IB_STAGE_ROCE,
4264 mlx5_ib_roce_cleanup),
4265 STAGE_CREATE(MLX5_IB_STAGE_QP,
4267 mlx5_cleanup_qp_table),
4268 STAGE_CREATE(MLX5_IB_STAGE_SRQ,
4269 mlx5_init_srq_table,
4270 mlx5_cleanup_srq_table),
4271 STAGE_CREATE(MLX5_IB_STAGE_DEVICE_RESOURCES,
4272 mlx5_ib_dev_res_init,
4273 mlx5_ib_dev_res_cleanup),
4274 STAGE_CREATE(MLX5_IB_STAGE_DEVICE_NOTIFIER,
4275 mlx5_ib_stage_dev_notifier_init,
4276 mlx5_ib_stage_dev_notifier_cleanup),
4277 STAGE_CREATE(MLX5_IB_STAGE_COUNTERS,
4278 mlx5_ib_counters_init,
4279 mlx5_ib_counters_cleanup),
4280 STAGE_CREATE(MLX5_IB_STAGE_CONG_DEBUGFS,
4281 mlx5_ib_stage_cong_debugfs_init,
4282 mlx5_ib_stage_cong_debugfs_cleanup),
4283 STAGE_CREATE(MLX5_IB_STAGE_UAR,
4284 mlx5_ib_stage_uar_init,
4285 mlx5_ib_stage_uar_cleanup),
4286 STAGE_CREATE(MLX5_IB_STAGE_BFREG,
4287 mlx5_ib_stage_bfrag_init,
4288 mlx5_ib_stage_bfrag_cleanup),
4289 STAGE_CREATE(MLX5_IB_STAGE_PRE_IB_REG_UMR,
4291 mlx5_ib_stage_pre_ib_reg_umr_cleanup),
4292 STAGE_CREATE(MLX5_IB_STAGE_WHITELIST_UID,
4294 mlx5_ib_devx_cleanup),
4295 STAGE_CREATE(MLX5_IB_STAGE_IB_REG,
4296 mlx5_ib_stage_ib_reg_init,
4297 mlx5_ib_stage_ib_reg_cleanup),
4298 STAGE_CREATE(MLX5_IB_STAGE_POST_IB_REG_UMR,
4299 mlx5_ib_stage_post_ib_reg_umr_init,
4301 STAGE_CREATE(MLX5_IB_STAGE_DELAY_DROP,
4302 mlx5_ib_stage_delay_drop_init,
4303 mlx5_ib_stage_delay_drop_cleanup),
4304 STAGE_CREATE(MLX5_IB_STAGE_RESTRACK,
4305 mlx5_ib_restrack_init,
4309 static int mlx5r_mp_probe(struct auxiliary_device *adev,
4310 const struct auxiliary_device_id *id)
4312 struct mlx5_adev *idev = container_of(adev, struct mlx5_adev, adev);
4313 struct mlx5_core_dev *mdev = idev->mdev;
4314 struct mlx5_ib_multiport_info *mpi;
4315 struct mlx5_ib_dev *dev;
4319 mpi = kzalloc(sizeof(*mpi), GFP_KERNEL);
4324 err = mlx5_query_nic_vport_system_image_guid(mdev,
4325 &mpi->sys_image_guid);
4331 mutex_lock(&mlx5_ib_multiport_mutex);
4332 list_for_each_entry(dev, &mlx5_ib_dev_list, ib_dev_list) {
4333 if (dev->sys_image_guid == mpi->sys_image_guid)
4334 bound = mlx5_ib_bind_slave_port(dev, mpi);
4337 rdma_roce_rescan_device(&dev->ib_dev);
4338 mpi->ibdev->ib_active = true;
4344 list_add_tail(&mpi->list, &mlx5_ib_unaffiliated_port_list);
4345 dev_dbg(mdev->device,
4346 "no suitable IB device found to bind to, added to unaffiliated list.\n");
4348 mutex_unlock(&mlx5_ib_multiport_mutex);
4350 auxiliary_set_drvdata(adev, mpi);
4354 static void mlx5r_mp_remove(struct auxiliary_device *adev)
4356 struct mlx5_ib_multiport_info *mpi;
4358 mpi = auxiliary_get_drvdata(adev);
4359 mutex_lock(&mlx5_ib_multiport_mutex);
4361 mlx5_ib_unbind_slave_port(mpi->ibdev, mpi);
4363 list_del(&mpi->list);
4364 mutex_unlock(&mlx5_ib_multiport_mutex);
4368 static int mlx5r_probe(struct auxiliary_device *adev,
4369 const struct auxiliary_device_id *id)
4371 struct mlx5_adev *idev = container_of(adev, struct mlx5_adev, adev);
4372 struct mlx5_core_dev *mdev = idev->mdev;
4373 const struct mlx5_ib_profile *profile;
4374 int port_type_cap, num_ports, ret;
4375 enum rdma_link_layer ll;
4376 struct mlx5_ib_dev *dev;
4378 port_type_cap = MLX5_CAP_GEN(mdev, port_type);
4379 ll = mlx5_port_type_cap_to_rdma_ll(port_type_cap);
4381 num_ports = max(MLX5_CAP_GEN(mdev, num_ports),
4382 MLX5_CAP_GEN(mdev, num_vhca_ports));
4383 dev = ib_alloc_device(mlx5_ib_dev, ib_dev);
4386 dev->port = kcalloc(num_ports, sizeof(*dev->port),
4389 ib_dealloc_device(&dev->ib_dev);
4394 dev->num_ports = num_ports;
4396 if (ll == IB_LINK_LAYER_ETHERNET && !mlx5_get_roce_state(mdev))
4397 profile = &raw_eth_profile;
4399 profile = &pf_profile;
4401 ret = __mlx5_ib_add(dev, profile);
4404 ib_dealloc_device(&dev->ib_dev);
4408 auxiliary_set_drvdata(adev, dev);
4412 static void mlx5r_remove(struct auxiliary_device *adev)
4414 struct mlx5_ib_dev *dev;
4416 dev = auxiliary_get_drvdata(adev);
4417 __mlx5_ib_remove(dev, dev->profile, MLX5_IB_STAGE_MAX);
4420 static const struct auxiliary_device_id mlx5r_mp_id_table[] = {
4421 { .name = MLX5_ADEV_NAME ".multiport", },
4425 static const struct auxiliary_device_id mlx5r_id_table[] = {
4426 { .name = MLX5_ADEV_NAME ".rdma", },
4430 MODULE_DEVICE_TABLE(auxiliary, mlx5r_mp_id_table);
4431 MODULE_DEVICE_TABLE(auxiliary, mlx5r_id_table);
4433 static struct auxiliary_driver mlx5r_mp_driver = {
4434 .name = "multiport",
4435 .probe = mlx5r_mp_probe,
4436 .remove = mlx5r_mp_remove,
4437 .id_table = mlx5r_mp_id_table,
4440 static struct auxiliary_driver mlx5r_driver = {
4442 .probe = mlx5r_probe,
4443 .remove = mlx5r_remove,
4444 .id_table = mlx5r_id_table,
4447 static int __init mlx5_ib_init(void)
4451 xlt_emergency_page = (void *)__get_free_page(GFP_KERNEL);
4452 if (!xlt_emergency_page)
4455 mlx5_ib_event_wq = alloc_ordered_workqueue("mlx5_ib_event_wq", 0);
4456 if (!mlx5_ib_event_wq) {
4457 free_page((unsigned long)xlt_emergency_page);
4461 ret = mlx5_ib_qp_event_init();
4466 ret = mlx5r_rep_init();
4469 ret = auxiliary_driver_register(&mlx5r_mp_driver);
4472 ret = auxiliary_driver_register(&mlx5r_driver);
4478 auxiliary_driver_unregister(&mlx5r_mp_driver);
4480 mlx5r_rep_cleanup();
4482 mlx5_ib_qp_event_cleanup();
4484 destroy_workqueue(mlx5_ib_event_wq);
4485 free_page((unsigned long)xlt_emergency_page);
4489 static void __exit mlx5_ib_cleanup(void)
4491 auxiliary_driver_unregister(&mlx5r_driver);
4492 auxiliary_driver_unregister(&mlx5r_mp_driver);
4493 mlx5r_rep_cleanup();
4495 mlx5_ib_qp_event_cleanup();
4496 destroy_workqueue(mlx5_ib_event_wq);
4497 free_page((unsigned long)xlt_emergency_page);
4500 module_init(mlx5_ib_init);
4501 module_exit(mlx5_ib_cleanup);