2 * Copyright (c) 2013-2015, Mellanox Technologies. All rights reserved.
4 * This software is available to you under a choice of one of two
5 * licenses. You may choose to be licensed under the terms of the GNU
6 * General Public License (GPL) Version 2, available from the file
7 * COPYING in the main directory of this source tree, or the
8 * OpenIB.org BSD license below:
10 * Redistribution and use in source and binary forms, with or
11 * without modification, are permitted provided that the following
14 * - Redistributions of source code must retain the above
15 * copyright notice, this list of conditions and the following
18 * - Redistributions in binary form must reproduce the above
19 * copyright notice, this list of conditions and the following
20 * disclaimer in the documentation and/or other materials
21 * provided with the distribution.
23 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
27 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
28 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
29 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
33 #include <linux/debugfs.h>
34 #include <linux/highmem.h>
35 #include <linux/module.h>
36 #include <linux/init.h>
37 #include <linux/errno.h>
38 #include <linux/pci.h>
39 #include <linux/dma-mapping.h>
40 #include <linux/slab.h>
41 #include <linux/bitmap.h>
42 #if defined(CONFIG_X86)
45 #include <linux/sched.h>
46 #include <linux/sched/mm.h>
47 #include <linux/sched/task.h>
48 #include <linux/delay.h>
49 #include <rdma/ib_user_verbs.h>
50 #include <rdma/ib_addr.h>
51 #include <rdma/ib_cache.h>
52 #include <linux/mlx5/port.h>
53 #include <linux/mlx5/vport.h>
54 #include <linux/mlx5/fs.h>
55 #include <linux/list.h>
56 #include <rdma/ib_smi.h>
57 #include <rdma/ib_umem.h>
59 #include <linux/etherdevice.h>
64 #include <linux/mlx5/fs_helpers.h>
65 #include <linux/mlx5/accel.h>
66 #include <rdma/uverbs_std_types.h>
67 #include <rdma/mlx5_user_ioctl_verbs.h>
68 #include <rdma/mlx5_user_ioctl_cmds.h>
70 #define UVERBS_MODULE_NAME mlx5_ib
71 #include <rdma/uverbs_named_ioctl.h>
73 #define DRIVER_NAME "mlx5_ib"
74 #define DRIVER_VERSION "5.0-0"
77 MODULE_DESCRIPTION("Mellanox Connect-IB HCA IB driver");
78 MODULE_LICENSE("Dual BSD/GPL");
80 static char mlx5_version[] =
81 DRIVER_NAME ": Mellanox Connect-IB Infiniband driver v"
84 struct mlx5_ib_event_work {
85 struct work_struct work;
87 struct mlx5_ib_dev *dev;
88 struct mlx5_ib_multiport_info *mpi;
96 MLX5_ATOMIC_SIZE_QP_8BYTES = 1 << 3,
99 static struct workqueue_struct *mlx5_ib_event_wq;
100 static LIST_HEAD(mlx5_ib_unaffiliated_port_list);
101 static LIST_HEAD(mlx5_ib_dev_list);
103 * This mutex should be held when accessing either of the above lists
105 static DEFINE_MUTEX(mlx5_ib_multiport_mutex);
107 /* We can't use an array for xlt_emergency_page because dma_map_single
108 * doesn't work on kernel modules memory
110 static unsigned long xlt_emergency_page;
111 static struct mutex xlt_emergency_page_mutex;
113 struct mlx5_ib_dev *mlx5_ib_get_ibdev_from_mpi(struct mlx5_ib_multiport_info *mpi)
115 struct mlx5_ib_dev *dev;
117 mutex_lock(&mlx5_ib_multiport_mutex);
119 mutex_unlock(&mlx5_ib_multiport_mutex);
123 static enum rdma_link_layer
124 mlx5_port_type_cap_to_rdma_ll(int port_type_cap)
126 switch (port_type_cap) {
127 case MLX5_CAP_PORT_TYPE_IB:
128 return IB_LINK_LAYER_INFINIBAND;
129 case MLX5_CAP_PORT_TYPE_ETH:
130 return IB_LINK_LAYER_ETHERNET;
132 return IB_LINK_LAYER_UNSPECIFIED;
136 static enum rdma_link_layer
137 mlx5_ib_port_link_layer(struct ib_device *device, u8 port_num)
139 struct mlx5_ib_dev *dev = to_mdev(device);
140 int port_type_cap = MLX5_CAP_GEN(dev->mdev, port_type);
142 return mlx5_port_type_cap_to_rdma_ll(port_type_cap);
145 static int get_port_state(struct ib_device *ibdev,
147 enum ib_port_state *state)
149 struct ib_port_attr attr;
152 memset(&attr, 0, sizeof(attr));
153 ret = ibdev->ops.query_port(ibdev, port_num, &attr);
159 static int mlx5_netdev_event(struct notifier_block *this,
160 unsigned long event, void *ptr)
162 struct mlx5_roce *roce = container_of(this, struct mlx5_roce, nb);
163 struct net_device *ndev = netdev_notifier_info_to_dev(ptr);
164 u8 port_num = roce->native_port_num;
165 struct mlx5_core_dev *mdev;
166 struct mlx5_ib_dev *ibdev;
169 mdev = mlx5_ib_get_native_port_mdev(ibdev, port_num, NULL);
174 case NETDEV_REGISTER:
175 write_lock(&roce->netdev_lock);
177 struct mlx5_eswitch *esw = ibdev->mdev->priv.eswitch;
178 struct net_device *rep_ndev;
180 rep_ndev = mlx5_ib_get_rep_netdev(esw,
182 if (rep_ndev == ndev)
184 } else if (ndev->dev.parent == mdev->device) {
187 write_unlock(&roce->netdev_lock);
190 case NETDEV_UNREGISTER:
191 write_lock(&roce->netdev_lock);
192 if (roce->netdev == ndev)
194 write_unlock(&roce->netdev_lock);
200 struct net_device *lag_ndev = mlx5_lag_get_roce_netdev(mdev);
201 struct net_device *upper = NULL;
204 upper = netdev_master_upper_dev_get(lag_ndev);
208 if ((upper == ndev || (!upper && ndev == roce->netdev))
209 && ibdev->ib_active) {
210 struct ib_event ibev = { };
211 enum ib_port_state port_state;
213 if (get_port_state(&ibdev->ib_dev, port_num,
217 if (roce->last_port_state == port_state)
220 roce->last_port_state = port_state;
221 ibev.device = &ibdev->ib_dev;
222 if (port_state == IB_PORT_DOWN)
223 ibev.event = IB_EVENT_PORT_ERR;
224 else if (port_state == IB_PORT_ACTIVE)
225 ibev.event = IB_EVENT_PORT_ACTIVE;
229 ibev.element.port_num = port_num;
230 ib_dispatch_event(&ibev);
239 mlx5_ib_put_native_port_mdev(ibdev, port_num);
243 static struct net_device *mlx5_ib_get_netdev(struct ib_device *device,
246 struct mlx5_ib_dev *ibdev = to_mdev(device);
247 struct net_device *ndev;
248 struct mlx5_core_dev *mdev;
250 mdev = mlx5_ib_get_native_port_mdev(ibdev, port_num, NULL);
254 ndev = mlx5_lag_get_roce_netdev(mdev);
258 /* Ensure ndev does not disappear before we invoke dev_hold()
260 read_lock(&ibdev->roce[port_num - 1].netdev_lock);
261 ndev = ibdev->roce[port_num - 1].netdev;
264 read_unlock(&ibdev->roce[port_num - 1].netdev_lock);
267 mlx5_ib_put_native_port_mdev(ibdev, port_num);
271 struct mlx5_core_dev *mlx5_ib_get_native_port_mdev(struct mlx5_ib_dev *ibdev,
275 enum rdma_link_layer ll = mlx5_ib_port_link_layer(&ibdev->ib_dev,
277 struct mlx5_core_dev *mdev = NULL;
278 struct mlx5_ib_multiport_info *mpi;
279 struct mlx5_ib_port *port;
281 if (!mlx5_core_mp_enabled(ibdev->mdev) ||
282 ll != IB_LINK_LAYER_ETHERNET) {
284 *native_port_num = ib_port_num;
289 *native_port_num = 1;
291 port = &ibdev->port[ib_port_num - 1];
295 spin_lock(&port->mp.mpi_lock);
296 mpi = ibdev->port[ib_port_num - 1].mp.mpi;
297 if (mpi && !mpi->unaffiliate) {
299 /* If it's the master no need to refcount, it'll exist
300 * as long as the ib_dev exists.
305 spin_unlock(&port->mp.mpi_lock);
310 void mlx5_ib_put_native_port_mdev(struct mlx5_ib_dev *ibdev, u8 port_num)
312 enum rdma_link_layer ll = mlx5_ib_port_link_layer(&ibdev->ib_dev,
314 struct mlx5_ib_multiport_info *mpi;
315 struct mlx5_ib_port *port;
317 if (!mlx5_core_mp_enabled(ibdev->mdev) || ll != IB_LINK_LAYER_ETHERNET)
320 port = &ibdev->port[port_num - 1];
322 spin_lock(&port->mp.mpi_lock);
323 mpi = ibdev->port[port_num - 1].mp.mpi;
328 if (mpi->unaffiliate)
329 complete(&mpi->unref_comp);
331 spin_unlock(&port->mp.mpi_lock);
334 static int translate_eth_legacy_proto_oper(u32 eth_proto_oper, u8 *active_speed,
337 switch (eth_proto_oper) {
338 case MLX5E_PROT_MASK(MLX5E_1000BASE_CX_SGMII):
339 case MLX5E_PROT_MASK(MLX5E_1000BASE_KX):
340 case MLX5E_PROT_MASK(MLX5E_100BASE_TX):
341 case MLX5E_PROT_MASK(MLX5E_1000BASE_T):
342 *active_width = IB_WIDTH_1X;
343 *active_speed = IB_SPEED_SDR;
345 case MLX5E_PROT_MASK(MLX5E_10GBASE_T):
346 case MLX5E_PROT_MASK(MLX5E_10GBASE_CX4):
347 case MLX5E_PROT_MASK(MLX5E_10GBASE_KX4):
348 case MLX5E_PROT_MASK(MLX5E_10GBASE_KR):
349 case MLX5E_PROT_MASK(MLX5E_10GBASE_CR):
350 case MLX5E_PROT_MASK(MLX5E_10GBASE_SR):
351 case MLX5E_PROT_MASK(MLX5E_10GBASE_ER):
352 *active_width = IB_WIDTH_1X;
353 *active_speed = IB_SPEED_QDR;
355 case MLX5E_PROT_MASK(MLX5E_25GBASE_CR):
356 case MLX5E_PROT_MASK(MLX5E_25GBASE_KR):
357 case MLX5E_PROT_MASK(MLX5E_25GBASE_SR):
358 *active_width = IB_WIDTH_1X;
359 *active_speed = IB_SPEED_EDR;
361 case MLX5E_PROT_MASK(MLX5E_40GBASE_CR4):
362 case MLX5E_PROT_MASK(MLX5E_40GBASE_KR4):
363 case MLX5E_PROT_MASK(MLX5E_40GBASE_SR4):
364 case MLX5E_PROT_MASK(MLX5E_40GBASE_LR4):
365 *active_width = IB_WIDTH_4X;
366 *active_speed = IB_SPEED_QDR;
368 case MLX5E_PROT_MASK(MLX5E_50GBASE_CR2):
369 case MLX5E_PROT_MASK(MLX5E_50GBASE_KR2):
370 case MLX5E_PROT_MASK(MLX5E_50GBASE_SR2):
371 *active_width = IB_WIDTH_1X;
372 *active_speed = IB_SPEED_HDR;
374 case MLX5E_PROT_MASK(MLX5E_56GBASE_R4):
375 *active_width = IB_WIDTH_4X;
376 *active_speed = IB_SPEED_FDR;
378 case MLX5E_PROT_MASK(MLX5E_100GBASE_CR4):
379 case MLX5E_PROT_MASK(MLX5E_100GBASE_SR4):
380 case MLX5E_PROT_MASK(MLX5E_100GBASE_KR4):
381 case MLX5E_PROT_MASK(MLX5E_100GBASE_LR4):
382 *active_width = IB_WIDTH_4X;
383 *active_speed = IB_SPEED_EDR;
392 static int translate_eth_ext_proto_oper(u32 eth_proto_oper, u8 *active_speed,
395 switch (eth_proto_oper) {
396 case MLX5E_PROT_MASK(MLX5E_SGMII_100M):
397 case MLX5E_PROT_MASK(MLX5E_1000BASE_X_SGMII):
398 *active_width = IB_WIDTH_1X;
399 *active_speed = IB_SPEED_SDR;
401 case MLX5E_PROT_MASK(MLX5E_5GBASE_R):
402 *active_width = IB_WIDTH_1X;
403 *active_speed = IB_SPEED_DDR;
405 case MLX5E_PROT_MASK(MLX5E_10GBASE_XFI_XAUI_1):
406 *active_width = IB_WIDTH_1X;
407 *active_speed = IB_SPEED_QDR;
409 case MLX5E_PROT_MASK(MLX5E_40GBASE_XLAUI_4_XLPPI_4):
410 *active_width = IB_WIDTH_4X;
411 *active_speed = IB_SPEED_QDR;
413 case MLX5E_PROT_MASK(MLX5E_25GAUI_1_25GBASE_CR_KR):
414 *active_width = IB_WIDTH_1X;
415 *active_speed = IB_SPEED_EDR;
417 case MLX5E_PROT_MASK(MLX5E_50GAUI_2_LAUI_2_50GBASE_CR2_KR2):
418 *active_width = IB_WIDTH_2X;
419 *active_speed = IB_SPEED_EDR;
421 case MLX5E_PROT_MASK(MLX5E_50GAUI_1_LAUI_1_50GBASE_CR_KR):
422 *active_width = IB_WIDTH_1X;
423 *active_speed = IB_SPEED_HDR;
425 case MLX5E_PROT_MASK(MLX5E_CAUI_4_100GBASE_CR4_KR4):
426 *active_width = IB_WIDTH_4X;
427 *active_speed = IB_SPEED_EDR;
429 case MLX5E_PROT_MASK(MLX5E_100GAUI_2_100GBASE_CR2_KR2):
430 *active_width = IB_WIDTH_2X;
431 *active_speed = IB_SPEED_HDR;
433 case MLX5E_PROT_MASK(MLX5E_200GAUI_4_200GBASE_CR4_KR4):
434 *active_width = IB_WIDTH_4X;
435 *active_speed = IB_SPEED_HDR;
444 static int translate_eth_proto_oper(u32 eth_proto_oper, u8 *active_speed,
445 u8 *active_width, bool ext)
448 translate_eth_ext_proto_oper(eth_proto_oper, active_speed,
450 translate_eth_legacy_proto_oper(eth_proto_oper, active_speed,
454 static int mlx5_query_port_roce(struct ib_device *device, u8 port_num,
455 struct ib_port_attr *props)
457 struct mlx5_ib_dev *dev = to_mdev(device);
458 u32 out[MLX5_ST_SZ_DW(ptys_reg)] = {0};
459 struct mlx5_core_dev *mdev;
460 struct net_device *ndev, *upper;
461 enum ib_mtu ndev_ib_mtu;
462 bool put_mdev = true;
469 mdev = mlx5_ib_get_native_port_mdev(dev, port_num, &mdev_port_num);
471 /* This means the port isn't affiliated yet. Get the
472 * info for the master port instead.
480 /* Possible bad flows are checked before filling out props so in case
481 * of an error it will still be zeroed out.
483 err = mlx5_query_port_ptys(mdev, out, sizeof(out), MLX5_PTYS_EN,
487 ext = MLX5_CAP_PCAM_FEATURE(dev->mdev, ptys_extended_ethernet);
488 eth_prot_oper = MLX5_GET_ETH_PROTO(ptys_reg, out, ext, eth_proto_oper);
490 props->active_width = IB_WIDTH_4X;
491 props->active_speed = IB_SPEED_QDR;
493 translate_eth_proto_oper(eth_prot_oper, &props->active_speed,
494 &props->active_width, ext);
496 props->port_cap_flags |= IB_PORT_CM_SUP;
497 props->ip_gids = true;
499 props->gid_tbl_len = MLX5_CAP_ROCE(dev->mdev,
500 roce_address_table_size);
501 props->max_mtu = IB_MTU_4096;
502 props->max_msg_sz = 1 << MLX5_CAP_GEN(dev->mdev, log_max_msg);
503 props->pkey_tbl_len = 1;
504 props->state = IB_PORT_DOWN;
505 props->phys_state = 3;
507 mlx5_query_nic_vport_qkey_viol_cntr(mdev, &qkey_viol_cntr);
508 props->qkey_viol_cntr = qkey_viol_cntr;
510 /* If this is a stub query for an unaffiliated port stop here */
514 ndev = mlx5_ib_get_netdev(device, port_num);
518 if (dev->lag_active) {
520 upper = netdev_master_upper_dev_get_rcu(ndev);
529 if (netif_running(ndev) && netif_carrier_ok(ndev)) {
530 props->state = IB_PORT_ACTIVE;
531 props->phys_state = 5;
534 ndev_ib_mtu = iboe_get_mtu(ndev->mtu);
538 props->active_mtu = min(props->max_mtu, ndev_ib_mtu);
541 mlx5_ib_put_native_port_mdev(dev, port_num);
545 struct mlx5_ib_vlan_info {
550 static int get_lower_dev_vlan(struct net_device *lower_dev, void *data)
552 struct mlx5_ib_vlan_info *vlan_info = data;
554 if (is_vlan_dev(lower_dev)) {
555 vlan_info->vlan = true;
556 vlan_info->vlan_id = vlan_dev_vlan_id(lower_dev);
558 /* We are interested only in first level vlan device, so
559 * always return 1 to stop iterating over next level devices.
564 static int set_roce_addr(struct mlx5_ib_dev *dev, u8 port_num,
565 unsigned int index, const union ib_gid *gid,
566 const struct ib_gid_attr *attr)
568 enum ib_gid_type gid_type = IB_GID_TYPE_IB;
569 struct mlx5_ib_vlan_info vlan_info = { };
575 gid_type = attr->gid_type;
576 ether_addr_copy(mac, attr->ndev->dev_addr);
578 if (is_vlan_dev(attr->ndev)) {
579 vlan_info.vlan = true;
580 vlan_info.vlan_id = vlan_dev_vlan_id(attr->ndev);
582 /* If the netdev is upper device and if it's lower
583 * lower device is vlan device, consider vlan id of
584 * the lower vlan device for this gid entry.
587 netdev_walk_all_lower_dev_rcu(attr->ndev,
588 get_lower_dev_vlan, &vlan_info);
595 roce_version = MLX5_ROCE_VERSION_1;
597 case IB_GID_TYPE_ROCE_UDP_ENCAP:
598 roce_version = MLX5_ROCE_VERSION_2;
599 if (ipv6_addr_v4mapped((void *)gid))
600 roce_l3_type = MLX5_ROCE_L3_TYPE_IPV4;
602 roce_l3_type = MLX5_ROCE_L3_TYPE_IPV6;
606 mlx5_ib_warn(dev, "Unexpected GID type %u\n", gid_type);
609 return mlx5_core_roce_gid_set(dev->mdev, index, roce_version,
610 roce_l3_type, gid->raw, mac,
611 vlan_info.vlan, vlan_info.vlan_id,
615 static int mlx5_ib_add_gid(const struct ib_gid_attr *attr,
616 __always_unused void **context)
618 return set_roce_addr(to_mdev(attr->device), attr->port_num,
619 attr->index, &attr->gid, attr);
622 static int mlx5_ib_del_gid(const struct ib_gid_attr *attr,
623 __always_unused void **context)
625 return set_roce_addr(to_mdev(attr->device), attr->port_num,
626 attr->index, NULL, NULL);
629 __be16 mlx5_get_roce_udp_sport(struct mlx5_ib_dev *dev,
630 const struct ib_gid_attr *attr)
632 if (attr->gid_type != IB_GID_TYPE_ROCE_UDP_ENCAP)
635 return cpu_to_be16(MLX5_CAP_ROCE(dev->mdev, r_roce_min_src_udp_port));
638 static int mlx5_use_mad_ifc(struct mlx5_ib_dev *dev)
640 if (MLX5_CAP_GEN(dev->mdev, port_type) == MLX5_CAP_PORT_TYPE_IB)
641 return !MLX5_CAP_GEN(dev->mdev, ib_virt);
646 MLX5_VPORT_ACCESS_METHOD_MAD,
647 MLX5_VPORT_ACCESS_METHOD_HCA,
648 MLX5_VPORT_ACCESS_METHOD_NIC,
651 static int mlx5_get_vport_access_method(struct ib_device *ibdev)
653 if (mlx5_use_mad_ifc(to_mdev(ibdev)))
654 return MLX5_VPORT_ACCESS_METHOD_MAD;
656 if (mlx5_ib_port_link_layer(ibdev, 1) ==
657 IB_LINK_LAYER_ETHERNET)
658 return MLX5_VPORT_ACCESS_METHOD_NIC;
660 return MLX5_VPORT_ACCESS_METHOD_HCA;
663 static void get_atomic_caps(struct mlx5_ib_dev *dev,
665 struct ib_device_attr *props)
668 u8 atomic_operations = MLX5_CAP_ATOMIC(dev->mdev, atomic_operations);
669 u8 atomic_req_8B_endianness_mode =
670 MLX5_CAP_ATOMIC(dev->mdev, atomic_req_8B_endianness_mode);
672 /* Check if HW supports 8 bytes standard atomic operations and capable
673 * of host endianness respond
675 tmp = MLX5_ATOMIC_OPS_CMP_SWAP | MLX5_ATOMIC_OPS_FETCH_ADD;
676 if (((atomic_operations & tmp) == tmp) &&
677 (atomic_size_qp & MLX5_ATOMIC_SIZE_QP_8BYTES) &&
678 (atomic_req_8B_endianness_mode)) {
679 props->atomic_cap = IB_ATOMIC_HCA;
681 props->atomic_cap = IB_ATOMIC_NONE;
685 static void get_atomic_caps_qp(struct mlx5_ib_dev *dev,
686 struct ib_device_attr *props)
688 u8 atomic_size_qp = MLX5_CAP_ATOMIC(dev->mdev, atomic_size_qp);
690 get_atomic_caps(dev, atomic_size_qp, props);
693 static void get_atomic_caps_dc(struct mlx5_ib_dev *dev,
694 struct ib_device_attr *props)
696 u8 atomic_size_qp = MLX5_CAP_ATOMIC(dev->mdev, atomic_size_dc);
698 get_atomic_caps(dev, atomic_size_qp, props);
701 bool mlx5_ib_dc_atomic_is_supported(struct mlx5_ib_dev *dev)
703 struct ib_device_attr props = {};
705 get_atomic_caps_dc(dev, &props);
706 return (props.atomic_cap == IB_ATOMIC_HCA) ? true : false;
708 static int mlx5_query_system_image_guid(struct ib_device *ibdev,
709 __be64 *sys_image_guid)
711 struct mlx5_ib_dev *dev = to_mdev(ibdev);
712 struct mlx5_core_dev *mdev = dev->mdev;
716 switch (mlx5_get_vport_access_method(ibdev)) {
717 case MLX5_VPORT_ACCESS_METHOD_MAD:
718 return mlx5_query_mad_ifc_system_image_guid(ibdev,
721 case MLX5_VPORT_ACCESS_METHOD_HCA:
722 err = mlx5_query_hca_vport_system_image_guid(mdev, &tmp);
725 case MLX5_VPORT_ACCESS_METHOD_NIC:
726 err = mlx5_query_nic_vport_system_image_guid(mdev, &tmp);
734 *sys_image_guid = cpu_to_be64(tmp);
740 static int mlx5_query_max_pkeys(struct ib_device *ibdev,
743 struct mlx5_ib_dev *dev = to_mdev(ibdev);
744 struct mlx5_core_dev *mdev = dev->mdev;
746 switch (mlx5_get_vport_access_method(ibdev)) {
747 case MLX5_VPORT_ACCESS_METHOD_MAD:
748 return mlx5_query_mad_ifc_max_pkeys(ibdev, max_pkeys);
750 case MLX5_VPORT_ACCESS_METHOD_HCA:
751 case MLX5_VPORT_ACCESS_METHOD_NIC:
752 *max_pkeys = mlx5_to_sw_pkey_sz(MLX5_CAP_GEN(mdev,
761 static int mlx5_query_vendor_id(struct ib_device *ibdev,
764 struct mlx5_ib_dev *dev = to_mdev(ibdev);
766 switch (mlx5_get_vport_access_method(ibdev)) {
767 case MLX5_VPORT_ACCESS_METHOD_MAD:
768 return mlx5_query_mad_ifc_vendor_id(ibdev, vendor_id);
770 case MLX5_VPORT_ACCESS_METHOD_HCA:
771 case MLX5_VPORT_ACCESS_METHOD_NIC:
772 return mlx5_core_query_vendor_id(dev->mdev, vendor_id);
779 static int mlx5_query_node_guid(struct mlx5_ib_dev *dev,
785 switch (mlx5_get_vport_access_method(&dev->ib_dev)) {
786 case MLX5_VPORT_ACCESS_METHOD_MAD:
787 return mlx5_query_mad_ifc_node_guid(dev, node_guid);
789 case MLX5_VPORT_ACCESS_METHOD_HCA:
790 err = mlx5_query_hca_vport_node_guid(dev->mdev, &tmp);
793 case MLX5_VPORT_ACCESS_METHOD_NIC:
794 err = mlx5_query_nic_vport_node_guid(dev->mdev, &tmp);
802 *node_guid = cpu_to_be64(tmp);
807 struct mlx5_reg_node_desc {
808 u8 desc[IB_DEVICE_NODE_DESC_MAX];
811 static int mlx5_query_node_desc(struct mlx5_ib_dev *dev, char *node_desc)
813 struct mlx5_reg_node_desc in;
815 if (mlx5_use_mad_ifc(dev))
816 return mlx5_query_mad_ifc_node_desc(dev, node_desc);
818 memset(&in, 0, sizeof(in));
820 return mlx5_core_access_reg(dev->mdev, &in, sizeof(in), node_desc,
821 sizeof(struct mlx5_reg_node_desc),
822 MLX5_REG_NODE_DESC, 0, 0);
825 static int mlx5_ib_query_device(struct ib_device *ibdev,
826 struct ib_device_attr *props,
827 struct ib_udata *uhw)
829 struct mlx5_ib_dev *dev = to_mdev(ibdev);
830 struct mlx5_core_dev *mdev = dev->mdev;
835 u64 min_page_size = 1ull << MLX5_CAP_GEN(mdev, log_pg_sz);
836 bool raw_support = !mlx5_core_mp_enabled(mdev);
837 struct mlx5_ib_query_device_resp resp = {};
841 resp_len = sizeof(resp.comp_mask) + sizeof(resp.response_length);
842 if (uhw->outlen && uhw->outlen < resp_len)
845 resp.response_length = resp_len;
847 if (uhw->inlen && !ib_is_udata_cleared(uhw, 0, uhw->inlen))
850 memset(props, 0, sizeof(*props));
851 err = mlx5_query_system_image_guid(ibdev,
852 &props->sys_image_guid);
856 err = mlx5_query_max_pkeys(ibdev, &props->max_pkeys);
860 err = mlx5_query_vendor_id(ibdev, &props->vendor_id);
864 props->fw_ver = ((u64)fw_rev_maj(dev->mdev) << 32) |
865 (fw_rev_min(dev->mdev) << 16) |
866 fw_rev_sub(dev->mdev);
867 props->device_cap_flags = IB_DEVICE_CHANGE_PHY_PORT |
868 IB_DEVICE_PORT_ACTIVE_EVENT |
869 IB_DEVICE_SYS_IMAGE_GUID |
870 IB_DEVICE_RC_RNR_NAK_GEN;
872 if (MLX5_CAP_GEN(mdev, pkv))
873 props->device_cap_flags |= IB_DEVICE_BAD_PKEY_CNTR;
874 if (MLX5_CAP_GEN(mdev, qkv))
875 props->device_cap_flags |= IB_DEVICE_BAD_QKEY_CNTR;
876 if (MLX5_CAP_GEN(mdev, apm))
877 props->device_cap_flags |= IB_DEVICE_AUTO_PATH_MIG;
878 if (MLX5_CAP_GEN(mdev, xrc))
879 props->device_cap_flags |= IB_DEVICE_XRC;
880 if (MLX5_CAP_GEN(mdev, imaicl)) {
881 props->device_cap_flags |= IB_DEVICE_MEM_WINDOW |
882 IB_DEVICE_MEM_WINDOW_TYPE_2B;
883 props->max_mw = 1 << MLX5_CAP_GEN(mdev, log_max_mkey);
884 /* We support 'Gappy' memory registration too */
885 props->device_cap_flags |= IB_DEVICE_SG_GAPS_REG;
887 props->device_cap_flags |= IB_DEVICE_MEM_MGT_EXTENSIONS;
888 if (MLX5_CAP_GEN(mdev, sho)) {
889 props->device_cap_flags |= IB_DEVICE_SIGNATURE_HANDOVER;
890 /* At this stage no support for signature handover */
891 props->sig_prot_cap = IB_PROT_T10DIF_TYPE_1 |
892 IB_PROT_T10DIF_TYPE_2 |
893 IB_PROT_T10DIF_TYPE_3;
894 props->sig_guard_cap = IB_GUARD_T10DIF_CRC |
895 IB_GUARD_T10DIF_CSUM;
897 if (MLX5_CAP_GEN(mdev, block_lb_mc))
898 props->device_cap_flags |= IB_DEVICE_BLOCK_MULTICAST_LOOPBACK;
900 if (MLX5_CAP_GEN(dev->mdev, eth_net_offloads) && raw_support) {
901 if (MLX5_CAP_ETH(mdev, csum_cap)) {
902 /* Legacy bit to support old userspace libraries */
903 props->device_cap_flags |= IB_DEVICE_RAW_IP_CSUM;
904 props->raw_packet_caps |= IB_RAW_PACKET_CAP_IP_CSUM;
907 if (MLX5_CAP_ETH(dev->mdev, vlan_cap))
908 props->raw_packet_caps |=
909 IB_RAW_PACKET_CAP_CVLAN_STRIPPING;
911 if (field_avail(typeof(resp), tso_caps, uhw->outlen)) {
912 max_tso = MLX5_CAP_ETH(mdev, max_lso_cap);
914 resp.tso_caps.max_tso = 1 << max_tso;
915 resp.tso_caps.supported_qpts |=
916 1 << IB_QPT_RAW_PACKET;
917 resp.response_length += sizeof(resp.tso_caps);
921 if (field_avail(typeof(resp), rss_caps, uhw->outlen)) {
922 resp.rss_caps.rx_hash_function =
923 MLX5_RX_HASH_FUNC_TOEPLITZ;
924 resp.rss_caps.rx_hash_fields_mask =
925 MLX5_RX_HASH_SRC_IPV4 |
926 MLX5_RX_HASH_DST_IPV4 |
927 MLX5_RX_HASH_SRC_IPV6 |
928 MLX5_RX_HASH_DST_IPV6 |
929 MLX5_RX_HASH_SRC_PORT_TCP |
930 MLX5_RX_HASH_DST_PORT_TCP |
931 MLX5_RX_HASH_SRC_PORT_UDP |
932 MLX5_RX_HASH_DST_PORT_UDP |
934 if (mlx5_accel_ipsec_device_caps(dev->mdev) &
935 MLX5_ACCEL_IPSEC_CAP_DEVICE)
936 resp.rss_caps.rx_hash_fields_mask |=
937 MLX5_RX_HASH_IPSEC_SPI;
938 resp.response_length += sizeof(resp.rss_caps);
941 if (field_avail(typeof(resp), tso_caps, uhw->outlen))
942 resp.response_length += sizeof(resp.tso_caps);
943 if (field_avail(typeof(resp), rss_caps, uhw->outlen))
944 resp.response_length += sizeof(resp.rss_caps);
947 if (MLX5_CAP_GEN(mdev, ipoib_basic_offloads)) {
948 props->device_cap_flags |= IB_DEVICE_UD_IP_CSUM;
949 props->device_cap_flags |= IB_DEVICE_UD_TSO;
952 if (MLX5_CAP_GEN(dev->mdev, rq_delay_drop) &&
953 MLX5_CAP_GEN(dev->mdev, general_notification_event) &&
955 props->raw_packet_caps |= IB_RAW_PACKET_CAP_DELAY_DROP;
957 if (MLX5_CAP_GEN(mdev, ipoib_enhanced_offloads) &&
958 MLX5_CAP_IPOIB_ENHANCED(mdev, csum_cap))
959 props->device_cap_flags |= IB_DEVICE_UD_IP_CSUM;
961 if (MLX5_CAP_GEN(dev->mdev, eth_net_offloads) &&
962 MLX5_CAP_ETH(dev->mdev, scatter_fcs) &&
964 /* Legacy bit to support old userspace libraries */
965 props->device_cap_flags |= IB_DEVICE_RAW_SCATTER_FCS;
966 props->raw_packet_caps |= IB_RAW_PACKET_CAP_SCATTER_FCS;
969 if (MLX5_CAP_DEV_MEM(mdev, memic)) {
971 MLX5_CAP_DEV_MEM(mdev, max_memic_size);
974 if (mlx5_get_flow_namespace(dev->mdev, MLX5_FLOW_NAMESPACE_BYPASS))
975 props->device_cap_flags |= IB_DEVICE_MANAGED_FLOW_STEERING;
977 if (MLX5_CAP_GEN(mdev, end_pad))
978 props->device_cap_flags |= IB_DEVICE_PCI_WRITE_END_PADDING;
980 props->vendor_part_id = mdev->pdev->device;
981 props->hw_ver = mdev->pdev->revision;
983 props->max_mr_size = ~0ull;
984 props->page_size_cap = ~(min_page_size - 1);
985 props->max_qp = 1 << MLX5_CAP_GEN(mdev, log_max_qp);
986 props->max_qp_wr = 1 << MLX5_CAP_GEN(mdev, log_max_qp_sz);
987 max_rq_sg = MLX5_CAP_GEN(mdev, max_wqe_sz_rq) /
988 sizeof(struct mlx5_wqe_data_seg);
989 max_sq_desc = min_t(int, MLX5_CAP_GEN(mdev, max_wqe_sz_sq), 512);
990 max_sq_sg = (max_sq_desc - sizeof(struct mlx5_wqe_ctrl_seg) -
991 sizeof(struct mlx5_wqe_raddr_seg)) /
992 sizeof(struct mlx5_wqe_data_seg);
993 props->max_send_sge = max_sq_sg;
994 props->max_recv_sge = max_rq_sg;
995 props->max_sge_rd = MLX5_MAX_SGE_RD;
996 props->max_cq = 1 << MLX5_CAP_GEN(mdev, log_max_cq);
997 props->max_cqe = (1 << MLX5_CAP_GEN(mdev, log_max_cq_sz)) - 1;
998 props->max_mr = 1 << MLX5_CAP_GEN(mdev, log_max_mkey);
999 props->max_pd = 1 << MLX5_CAP_GEN(mdev, log_max_pd);
1000 props->max_qp_rd_atom = 1 << MLX5_CAP_GEN(mdev, log_max_ra_req_qp);
1001 props->max_qp_init_rd_atom = 1 << MLX5_CAP_GEN(mdev, log_max_ra_res_qp);
1002 props->max_srq = 1 << MLX5_CAP_GEN(mdev, log_max_srq);
1003 props->max_srq_wr = (1 << MLX5_CAP_GEN(mdev, log_max_srq_sz)) - 1;
1004 props->local_ca_ack_delay = MLX5_CAP_GEN(mdev, local_ca_ack_delay);
1005 props->max_res_rd_atom = props->max_qp_rd_atom * props->max_qp;
1006 props->max_srq_sge = max_rq_sg - 1;
1007 props->max_fast_reg_page_list_len =
1008 1 << MLX5_CAP_GEN(mdev, log_max_klm_list_size);
1009 get_atomic_caps_qp(dev, props);
1010 props->masked_atomic_cap = IB_ATOMIC_NONE;
1011 props->max_mcast_grp = 1 << MLX5_CAP_GEN(mdev, log_max_mcg);
1012 props->max_mcast_qp_attach = MLX5_CAP_GEN(mdev, max_qp_mcg);
1013 props->max_total_mcast_qp_attach = props->max_mcast_qp_attach *
1014 props->max_mcast_grp;
1015 props->max_map_per_fmr = INT_MAX; /* no limit in ConnectIB */
1016 props->max_ah = INT_MAX;
1017 props->hca_core_clock = MLX5_CAP_GEN(mdev, device_frequency_khz);
1018 props->timestamp_mask = 0x7FFFFFFFFFFFFFFFULL;
1020 if (IS_ENABLED(CONFIG_INFINIBAND_ON_DEMAND_PAGING)) {
1021 if (MLX5_CAP_GEN(mdev, pg))
1022 props->device_cap_flags |= IB_DEVICE_ON_DEMAND_PAGING;
1023 props->odp_caps = dev->odp_caps;
1026 if (MLX5_CAP_GEN(mdev, cd))
1027 props->device_cap_flags |= IB_DEVICE_CROSS_CHANNEL;
1029 if (!mlx5_core_is_pf(mdev))
1030 props->device_cap_flags |= IB_DEVICE_VIRTUAL_FUNCTION;
1032 if (mlx5_ib_port_link_layer(ibdev, 1) ==
1033 IB_LINK_LAYER_ETHERNET && raw_support) {
1034 props->rss_caps.max_rwq_indirection_tables =
1035 1 << MLX5_CAP_GEN(dev->mdev, log_max_rqt);
1036 props->rss_caps.max_rwq_indirection_table_size =
1037 1 << MLX5_CAP_GEN(dev->mdev, log_max_rqt_size);
1038 props->rss_caps.supported_qpts = 1 << IB_QPT_RAW_PACKET;
1039 props->max_wq_type_rq =
1040 1 << MLX5_CAP_GEN(dev->mdev, log_max_rq);
1043 if (MLX5_CAP_GEN(mdev, tag_matching)) {
1044 props->tm_caps.max_rndv_hdr_size = MLX5_TM_MAX_RNDV_MSG_SIZE;
1045 props->tm_caps.max_num_tags =
1046 (1 << MLX5_CAP_GEN(mdev, log_tag_matching_list_sz)) - 1;
1047 props->tm_caps.flags = IB_TM_CAP_RC;
1048 props->tm_caps.max_ops =
1049 1 << MLX5_CAP_GEN(mdev, log_max_qp_sz);
1050 props->tm_caps.max_sge = MLX5_TM_MAX_SGE;
1053 if (MLX5_CAP_GEN(dev->mdev, cq_moderation)) {
1054 props->cq_caps.max_cq_moderation_count =
1056 props->cq_caps.max_cq_moderation_period =
1060 if (field_avail(typeof(resp), cqe_comp_caps, uhw->outlen)) {
1061 resp.response_length += sizeof(resp.cqe_comp_caps);
1063 if (MLX5_CAP_GEN(dev->mdev, cqe_compression)) {
1064 resp.cqe_comp_caps.max_num =
1065 MLX5_CAP_GEN(dev->mdev,
1066 cqe_compression_max_num);
1068 resp.cqe_comp_caps.supported_format =
1069 MLX5_IB_CQE_RES_FORMAT_HASH |
1070 MLX5_IB_CQE_RES_FORMAT_CSUM;
1072 if (MLX5_CAP_GEN(dev->mdev, mini_cqe_resp_stride_index))
1073 resp.cqe_comp_caps.supported_format |=
1074 MLX5_IB_CQE_RES_FORMAT_CSUM_STRIDX;
1078 if (field_avail(typeof(resp), packet_pacing_caps, uhw->outlen) &&
1080 if (MLX5_CAP_QOS(mdev, packet_pacing) &&
1081 MLX5_CAP_GEN(mdev, qos)) {
1082 resp.packet_pacing_caps.qp_rate_limit_max =
1083 MLX5_CAP_QOS(mdev, packet_pacing_max_rate);
1084 resp.packet_pacing_caps.qp_rate_limit_min =
1085 MLX5_CAP_QOS(mdev, packet_pacing_min_rate);
1086 resp.packet_pacing_caps.supported_qpts |=
1087 1 << IB_QPT_RAW_PACKET;
1088 if (MLX5_CAP_QOS(mdev, packet_pacing_burst_bound) &&
1089 MLX5_CAP_QOS(mdev, packet_pacing_typical_size))
1090 resp.packet_pacing_caps.cap_flags |=
1091 MLX5_IB_PP_SUPPORT_BURST;
1093 resp.response_length += sizeof(resp.packet_pacing_caps);
1096 if (field_avail(typeof(resp), mlx5_ib_support_multi_pkt_send_wqes,
1098 if (MLX5_CAP_ETH(mdev, multi_pkt_send_wqe))
1099 resp.mlx5_ib_support_multi_pkt_send_wqes =
1102 if (MLX5_CAP_ETH(mdev, enhanced_multi_pkt_send_wqe))
1103 resp.mlx5_ib_support_multi_pkt_send_wqes |=
1104 MLX5_IB_SUPPORT_EMPW;
1106 resp.response_length +=
1107 sizeof(resp.mlx5_ib_support_multi_pkt_send_wqes);
1110 if (field_avail(typeof(resp), flags, uhw->outlen)) {
1111 resp.response_length += sizeof(resp.flags);
1113 if (MLX5_CAP_GEN(mdev, cqe_compression_128))
1115 MLX5_IB_QUERY_DEV_RESP_FLAGS_CQE_128B_COMP;
1117 if (MLX5_CAP_GEN(mdev, cqe_128_always))
1118 resp.flags |= MLX5_IB_QUERY_DEV_RESP_FLAGS_CQE_128B_PAD;
1119 if (MLX5_CAP_GEN(mdev, qp_packet_based))
1121 MLX5_IB_QUERY_DEV_RESP_PACKET_BASED_CREDIT_MODE;
1123 resp.flags |= MLX5_IB_QUERY_DEV_RESP_FLAGS_SCAT2CQE_DCT;
1126 if (field_avail(typeof(resp), sw_parsing_caps,
1128 resp.response_length += sizeof(resp.sw_parsing_caps);
1129 if (MLX5_CAP_ETH(mdev, swp)) {
1130 resp.sw_parsing_caps.sw_parsing_offloads |=
1133 if (MLX5_CAP_ETH(mdev, swp_csum))
1134 resp.sw_parsing_caps.sw_parsing_offloads |=
1135 MLX5_IB_SW_PARSING_CSUM;
1137 if (MLX5_CAP_ETH(mdev, swp_lso))
1138 resp.sw_parsing_caps.sw_parsing_offloads |=
1139 MLX5_IB_SW_PARSING_LSO;
1141 if (resp.sw_parsing_caps.sw_parsing_offloads)
1142 resp.sw_parsing_caps.supported_qpts =
1143 BIT(IB_QPT_RAW_PACKET);
1147 if (field_avail(typeof(resp), striding_rq_caps, uhw->outlen) &&
1149 resp.response_length += sizeof(resp.striding_rq_caps);
1150 if (MLX5_CAP_GEN(mdev, striding_rq)) {
1151 resp.striding_rq_caps.min_single_stride_log_num_of_bytes =
1152 MLX5_MIN_SINGLE_STRIDE_LOG_NUM_BYTES;
1153 resp.striding_rq_caps.max_single_stride_log_num_of_bytes =
1154 MLX5_MAX_SINGLE_STRIDE_LOG_NUM_BYTES;
1155 resp.striding_rq_caps.min_single_wqe_log_num_of_strides =
1156 MLX5_MIN_SINGLE_WQE_LOG_NUM_STRIDES;
1157 resp.striding_rq_caps.max_single_wqe_log_num_of_strides =
1158 MLX5_MAX_SINGLE_WQE_LOG_NUM_STRIDES;
1159 resp.striding_rq_caps.supported_qpts =
1160 BIT(IB_QPT_RAW_PACKET);
1164 if (field_avail(typeof(resp), tunnel_offloads_caps,
1166 resp.response_length += sizeof(resp.tunnel_offloads_caps);
1167 if (MLX5_CAP_ETH(mdev, tunnel_stateless_vxlan))
1168 resp.tunnel_offloads_caps |=
1169 MLX5_IB_TUNNELED_OFFLOADS_VXLAN;
1170 if (MLX5_CAP_ETH(mdev, tunnel_stateless_geneve_rx))
1171 resp.tunnel_offloads_caps |=
1172 MLX5_IB_TUNNELED_OFFLOADS_GENEVE;
1173 if (MLX5_CAP_ETH(mdev, tunnel_stateless_gre))
1174 resp.tunnel_offloads_caps |=
1175 MLX5_IB_TUNNELED_OFFLOADS_GRE;
1176 if (MLX5_CAP_GEN(mdev, flex_parser_protocols) &
1177 MLX5_FLEX_PROTO_CW_MPLS_GRE)
1178 resp.tunnel_offloads_caps |=
1179 MLX5_IB_TUNNELED_OFFLOADS_MPLS_GRE;
1180 if (MLX5_CAP_GEN(mdev, flex_parser_protocols) &
1181 MLX5_FLEX_PROTO_CW_MPLS_UDP)
1182 resp.tunnel_offloads_caps |=
1183 MLX5_IB_TUNNELED_OFFLOADS_MPLS_UDP;
1187 err = ib_copy_to_udata(uhw, &resp, resp.response_length);
1196 enum mlx5_ib_width {
1197 MLX5_IB_WIDTH_1X = 1 << 0,
1198 MLX5_IB_WIDTH_2X = 1 << 1,
1199 MLX5_IB_WIDTH_4X = 1 << 2,
1200 MLX5_IB_WIDTH_8X = 1 << 3,
1201 MLX5_IB_WIDTH_12X = 1 << 4
1204 static void translate_active_width(struct ib_device *ibdev, u8 active_width,
1207 struct mlx5_ib_dev *dev = to_mdev(ibdev);
1209 if (active_width & MLX5_IB_WIDTH_1X)
1210 *ib_width = IB_WIDTH_1X;
1211 else if (active_width & MLX5_IB_WIDTH_2X)
1212 *ib_width = IB_WIDTH_2X;
1213 else if (active_width & MLX5_IB_WIDTH_4X)
1214 *ib_width = IB_WIDTH_4X;
1215 else if (active_width & MLX5_IB_WIDTH_8X)
1216 *ib_width = IB_WIDTH_8X;
1217 else if (active_width & MLX5_IB_WIDTH_12X)
1218 *ib_width = IB_WIDTH_12X;
1220 mlx5_ib_dbg(dev, "Invalid active_width %d, setting width to default value: 4x\n",
1222 *ib_width = IB_WIDTH_4X;
1228 static int mlx5_mtu_to_ib_mtu(int mtu)
1233 case 1024: return 3;
1234 case 2048: return 4;
1235 case 4096: return 5;
1237 pr_warn("invalid mtu\n");
1242 enum ib_max_vl_num {
1244 __IB_MAX_VL_0_1 = 2,
1245 __IB_MAX_VL_0_3 = 3,
1246 __IB_MAX_VL_0_7 = 4,
1247 __IB_MAX_VL_0_14 = 5,
1250 enum mlx5_vl_hw_cap {
1259 MLX5_VL_HW_0_14 = 15
1262 static int translate_max_vl_num(struct ib_device *ibdev, u8 vl_hw_cap,
1265 switch (vl_hw_cap) {
1267 *max_vl_num = __IB_MAX_VL_0;
1269 case MLX5_VL_HW_0_1:
1270 *max_vl_num = __IB_MAX_VL_0_1;
1272 case MLX5_VL_HW_0_3:
1273 *max_vl_num = __IB_MAX_VL_0_3;
1275 case MLX5_VL_HW_0_7:
1276 *max_vl_num = __IB_MAX_VL_0_7;
1278 case MLX5_VL_HW_0_14:
1279 *max_vl_num = __IB_MAX_VL_0_14;
1289 static int mlx5_query_hca_port(struct ib_device *ibdev, u8 port,
1290 struct ib_port_attr *props)
1292 struct mlx5_ib_dev *dev = to_mdev(ibdev);
1293 struct mlx5_core_dev *mdev = dev->mdev;
1294 struct mlx5_hca_vport_context *rep;
1298 u8 ib_link_width_oper;
1301 rep = kzalloc(sizeof(*rep), GFP_KERNEL);
1307 /* props being zeroed by the caller, avoid zeroing it here */
1309 err = mlx5_query_hca_vport_context(mdev, 0, port, 0, rep);
1313 props->lid = rep->lid;
1314 props->lmc = rep->lmc;
1315 props->sm_lid = rep->sm_lid;
1316 props->sm_sl = rep->sm_sl;
1317 props->state = rep->vport_state;
1318 props->phys_state = rep->port_physical_state;
1319 props->port_cap_flags = rep->cap_mask1;
1320 props->gid_tbl_len = mlx5_get_gid_table_len(MLX5_CAP_GEN(mdev, gid_table_size));
1321 props->max_msg_sz = 1 << MLX5_CAP_GEN(mdev, log_max_msg);
1322 props->pkey_tbl_len = mlx5_to_sw_pkey_sz(MLX5_CAP_GEN(mdev, pkey_table_size));
1323 props->bad_pkey_cntr = rep->pkey_violation_counter;
1324 props->qkey_viol_cntr = rep->qkey_violation_counter;
1325 props->subnet_timeout = rep->subnet_timeout;
1326 props->init_type_reply = rep->init_type_reply;
1328 if (props->port_cap_flags & IB_PORT_CAP_MASK2_SUP)
1329 props->port_cap_flags2 = rep->cap_mask2;
1331 err = mlx5_query_port_link_width_oper(mdev, &ib_link_width_oper, port);
1335 translate_active_width(ibdev, ib_link_width_oper, &props->active_width);
1337 err = mlx5_query_port_ib_proto_oper(mdev, &props->active_speed, port);
1341 mlx5_query_port_max_mtu(mdev, &max_mtu, port);
1343 props->max_mtu = mlx5_mtu_to_ib_mtu(max_mtu);
1345 mlx5_query_port_oper_mtu(mdev, &oper_mtu, port);
1347 props->active_mtu = mlx5_mtu_to_ib_mtu(oper_mtu);
1349 err = mlx5_query_port_vl_hw_cap(mdev, &vl_hw_cap, port);
1353 err = translate_max_vl_num(ibdev, vl_hw_cap,
1354 &props->max_vl_num);
1360 int mlx5_ib_query_port(struct ib_device *ibdev, u8 port,
1361 struct ib_port_attr *props)
1366 switch (mlx5_get_vport_access_method(ibdev)) {
1367 case MLX5_VPORT_ACCESS_METHOD_MAD:
1368 ret = mlx5_query_mad_ifc_port(ibdev, port, props);
1371 case MLX5_VPORT_ACCESS_METHOD_HCA:
1372 ret = mlx5_query_hca_port(ibdev, port, props);
1375 case MLX5_VPORT_ACCESS_METHOD_NIC:
1376 ret = mlx5_query_port_roce(ibdev, port, props);
1383 if (!ret && props) {
1384 struct mlx5_ib_dev *dev = to_mdev(ibdev);
1385 struct mlx5_core_dev *mdev;
1386 bool put_mdev = true;
1388 mdev = mlx5_ib_get_native_port_mdev(dev, port, NULL);
1390 /* If the port isn't affiliated yet query the master.
1391 * The master and slave will have the same values.
1397 count = mlx5_core_reserved_gids_count(mdev);
1399 mlx5_ib_put_native_port_mdev(dev, port);
1400 props->gid_tbl_len -= count;
1405 static int mlx5_ib_rep_query_port(struct ib_device *ibdev, u8 port,
1406 struct ib_port_attr *props)
1410 /* Only link layer == ethernet is valid for representors */
1411 ret = mlx5_query_port_roce(ibdev, port, props);
1415 /* We don't support GIDS */
1416 props->gid_tbl_len = 0;
1421 static int mlx5_ib_query_gid(struct ib_device *ibdev, u8 port, int index,
1424 struct mlx5_ib_dev *dev = to_mdev(ibdev);
1425 struct mlx5_core_dev *mdev = dev->mdev;
1427 switch (mlx5_get_vport_access_method(ibdev)) {
1428 case MLX5_VPORT_ACCESS_METHOD_MAD:
1429 return mlx5_query_mad_ifc_gids(ibdev, port, index, gid);
1431 case MLX5_VPORT_ACCESS_METHOD_HCA:
1432 return mlx5_query_hca_vport_gid(mdev, 0, port, 0, index, gid);
1440 static int mlx5_query_hca_nic_pkey(struct ib_device *ibdev, u8 port,
1441 u16 index, u16 *pkey)
1443 struct mlx5_ib_dev *dev = to_mdev(ibdev);
1444 struct mlx5_core_dev *mdev;
1445 bool put_mdev = true;
1449 mdev = mlx5_ib_get_native_port_mdev(dev, port, &mdev_port_num);
1451 /* The port isn't affiliated yet, get the PKey from the master
1452 * port. For RoCE the PKey tables will be the same.
1459 err = mlx5_query_hca_vport_pkey(mdev, 0, mdev_port_num, 0,
1462 mlx5_ib_put_native_port_mdev(dev, port);
1467 static int mlx5_ib_query_pkey(struct ib_device *ibdev, u8 port, u16 index,
1470 switch (mlx5_get_vport_access_method(ibdev)) {
1471 case MLX5_VPORT_ACCESS_METHOD_MAD:
1472 return mlx5_query_mad_ifc_pkey(ibdev, port, index, pkey);
1474 case MLX5_VPORT_ACCESS_METHOD_HCA:
1475 case MLX5_VPORT_ACCESS_METHOD_NIC:
1476 return mlx5_query_hca_nic_pkey(ibdev, port, index, pkey);
1482 static int mlx5_ib_modify_device(struct ib_device *ibdev, int mask,
1483 struct ib_device_modify *props)
1485 struct mlx5_ib_dev *dev = to_mdev(ibdev);
1486 struct mlx5_reg_node_desc in;
1487 struct mlx5_reg_node_desc out;
1490 if (mask & ~IB_DEVICE_MODIFY_NODE_DESC)
1493 if (!(mask & IB_DEVICE_MODIFY_NODE_DESC))
1497 * If possible, pass node desc to FW, so it can generate
1498 * a 144 trap. If cmd fails, just ignore.
1500 memcpy(&in, props->node_desc, IB_DEVICE_NODE_DESC_MAX);
1501 err = mlx5_core_access_reg(dev->mdev, &in, sizeof(in), &out,
1502 sizeof(out), MLX5_REG_NODE_DESC, 0, 1);
1506 memcpy(ibdev->node_desc, props->node_desc, IB_DEVICE_NODE_DESC_MAX);
1511 static int set_port_caps_atomic(struct mlx5_ib_dev *dev, u8 port_num, u32 mask,
1514 struct mlx5_hca_vport_context ctx = {};
1515 struct mlx5_core_dev *mdev;
1519 mdev = mlx5_ib_get_native_port_mdev(dev, port_num, &mdev_port_num);
1523 err = mlx5_query_hca_vport_context(mdev, 0, mdev_port_num, 0, &ctx);
1527 if (~ctx.cap_mask1_perm & mask) {
1528 mlx5_ib_warn(dev, "trying to change bitmask 0x%X but change supported 0x%X\n",
1529 mask, ctx.cap_mask1_perm);
1534 ctx.cap_mask1 = value;
1535 ctx.cap_mask1_perm = mask;
1536 err = mlx5_core_modify_hca_vport_context(mdev, 0, mdev_port_num,
1540 mlx5_ib_put_native_port_mdev(dev, port_num);
1545 static int mlx5_ib_modify_port(struct ib_device *ibdev, u8 port, int mask,
1546 struct ib_port_modify *props)
1548 struct mlx5_ib_dev *dev = to_mdev(ibdev);
1549 struct ib_port_attr attr;
1554 bool is_ib = (mlx5_ib_port_link_layer(ibdev, port) ==
1555 IB_LINK_LAYER_INFINIBAND);
1557 /* CM layer calls ib_modify_port() regardless of the link layer. For
1558 * Ethernet ports, qkey violation and Port capabilities are meaningless.
1563 if (MLX5_CAP_GEN(dev->mdev, ib_virt) && is_ib) {
1564 change_mask = props->clr_port_cap_mask | props->set_port_cap_mask;
1565 value = ~props->clr_port_cap_mask | props->set_port_cap_mask;
1566 return set_port_caps_atomic(dev, port, change_mask, value);
1569 mutex_lock(&dev->cap_mask_mutex);
1571 err = ib_query_port(ibdev, port, &attr);
1575 tmp = (attr.port_cap_flags | props->set_port_cap_mask) &
1576 ~props->clr_port_cap_mask;
1578 err = mlx5_set_port_caps(dev->mdev, port, tmp);
1581 mutex_unlock(&dev->cap_mask_mutex);
1585 static void print_lib_caps(struct mlx5_ib_dev *dev, u64 caps)
1587 mlx5_ib_dbg(dev, "MLX5_LIB_CAP_4K_UAR = %s\n",
1588 caps & MLX5_LIB_CAP_4K_UAR ? "y" : "n");
1591 static u16 calc_dynamic_bfregs(int uars_per_sys_page)
1593 /* Large page with non 4k uar support might limit the dynamic size */
1594 if (uars_per_sys_page == 1 && PAGE_SIZE > 4096)
1595 return MLX5_MIN_DYN_BFREGS;
1597 return MLX5_MAX_DYN_BFREGS;
1600 static int calc_total_bfregs(struct mlx5_ib_dev *dev, bool lib_uar_4k,
1601 struct mlx5_ib_alloc_ucontext_req_v2 *req,
1602 struct mlx5_bfreg_info *bfregi)
1604 int uars_per_sys_page;
1605 int bfregs_per_sys_page;
1606 int ref_bfregs = req->total_num_bfregs;
1608 if (req->total_num_bfregs == 0)
1611 BUILD_BUG_ON(MLX5_MAX_BFREGS % MLX5_NON_FP_BFREGS_IN_PAGE);
1612 BUILD_BUG_ON(MLX5_MAX_BFREGS < MLX5_NON_FP_BFREGS_IN_PAGE);
1614 if (req->total_num_bfregs > MLX5_MAX_BFREGS)
1617 uars_per_sys_page = get_uars_per_sys_page(dev, lib_uar_4k);
1618 bfregs_per_sys_page = uars_per_sys_page * MLX5_NON_FP_BFREGS_PER_UAR;
1619 /* This holds the required static allocation asked by the user */
1620 req->total_num_bfregs = ALIGN(req->total_num_bfregs, bfregs_per_sys_page);
1621 if (req->num_low_latency_bfregs > req->total_num_bfregs - 1)
1624 bfregi->num_static_sys_pages = req->total_num_bfregs / bfregs_per_sys_page;
1625 bfregi->num_dyn_bfregs = ALIGN(calc_dynamic_bfregs(uars_per_sys_page), bfregs_per_sys_page);
1626 bfregi->total_num_bfregs = req->total_num_bfregs + bfregi->num_dyn_bfregs;
1627 bfregi->num_sys_pages = bfregi->total_num_bfregs / bfregs_per_sys_page;
1629 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",
1630 MLX5_CAP_GEN(dev->mdev, uar_4k) ? "yes" : "no",
1631 lib_uar_4k ? "yes" : "no", ref_bfregs,
1632 req->total_num_bfregs, bfregi->total_num_bfregs,
1633 bfregi->num_sys_pages);
1638 static int allocate_uars(struct mlx5_ib_dev *dev, struct mlx5_ib_ucontext *context)
1640 struct mlx5_bfreg_info *bfregi;
1644 bfregi = &context->bfregi;
1645 for (i = 0; i < bfregi->num_static_sys_pages; i++) {
1646 err = mlx5_cmd_alloc_uar(dev->mdev, &bfregi->sys_pages[i]);
1650 mlx5_ib_dbg(dev, "allocated uar %d\n", bfregi->sys_pages[i]);
1653 for (i = bfregi->num_static_sys_pages; i < bfregi->num_sys_pages; i++)
1654 bfregi->sys_pages[i] = MLX5_IB_INVALID_UAR_INDEX;
1659 for (--i; i >= 0; i--)
1660 if (mlx5_cmd_free_uar(dev->mdev, bfregi->sys_pages[i]))
1661 mlx5_ib_warn(dev, "failed to free uar %d\n", i);
1666 static void deallocate_uars(struct mlx5_ib_dev *dev,
1667 struct mlx5_ib_ucontext *context)
1669 struct mlx5_bfreg_info *bfregi;
1672 bfregi = &context->bfregi;
1673 for (i = 0; i < bfregi->num_sys_pages; i++)
1674 if (i < bfregi->num_static_sys_pages ||
1675 bfregi->sys_pages[i] != MLX5_IB_INVALID_UAR_INDEX)
1676 mlx5_cmd_free_uar(dev->mdev, bfregi->sys_pages[i]);
1679 int mlx5_ib_enable_lb(struct mlx5_ib_dev *dev, bool td, bool qp)
1683 mutex_lock(&dev->lb.mutex);
1689 if (dev->lb.user_td == 2 ||
1691 if (!dev->lb.enabled) {
1692 err = mlx5_nic_vport_update_local_lb(dev->mdev, true);
1693 dev->lb.enabled = true;
1697 mutex_unlock(&dev->lb.mutex);
1702 void mlx5_ib_disable_lb(struct mlx5_ib_dev *dev, bool td, bool qp)
1704 mutex_lock(&dev->lb.mutex);
1710 if (dev->lb.user_td == 1 &&
1712 if (dev->lb.enabled) {
1713 mlx5_nic_vport_update_local_lb(dev->mdev, false);
1714 dev->lb.enabled = false;
1718 mutex_unlock(&dev->lb.mutex);
1721 static int mlx5_ib_alloc_transport_domain(struct mlx5_ib_dev *dev, u32 *tdn,
1726 if (!MLX5_CAP_GEN(dev->mdev, log_max_transport_domain))
1729 err = mlx5_cmd_alloc_transport_domain(dev->mdev, tdn, uid);
1733 if ((MLX5_CAP_GEN(dev->mdev, port_type) != MLX5_CAP_PORT_TYPE_ETH) ||
1734 (!MLX5_CAP_GEN(dev->mdev, disable_local_lb_uc) &&
1735 !MLX5_CAP_GEN(dev->mdev, disable_local_lb_mc)))
1738 return mlx5_ib_enable_lb(dev, true, false);
1741 static void mlx5_ib_dealloc_transport_domain(struct mlx5_ib_dev *dev, u32 tdn,
1744 if (!MLX5_CAP_GEN(dev->mdev, log_max_transport_domain))
1747 mlx5_cmd_dealloc_transport_domain(dev->mdev, tdn, uid);
1749 if ((MLX5_CAP_GEN(dev->mdev, port_type) != MLX5_CAP_PORT_TYPE_ETH) ||
1750 (!MLX5_CAP_GEN(dev->mdev, disable_local_lb_uc) &&
1751 !MLX5_CAP_GEN(dev->mdev, disable_local_lb_mc)))
1754 mlx5_ib_disable_lb(dev, true, false);
1757 static int mlx5_ib_alloc_ucontext(struct ib_ucontext *uctx,
1758 struct ib_udata *udata)
1760 struct ib_device *ibdev = uctx->device;
1761 struct mlx5_ib_dev *dev = to_mdev(ibdev);
1762 struct mlx5_ib_alloc_ucontext_req_v2 req = {};
1763 struct mlx5_ib_alloc_ucontext_resp resp = {};
1764 struct mlx5_core_dev *mdev = dev->mdev;
1765 struct mlx5_ib_ucontext *context = to_mucontext(uctx);
1766 struct mlx5_bfreg_info *bfregi;
1769 size_t min_req_v2 = offsetof(struct mlx5_ib_alloc_ucontext_req_v2,
1774 if (!dev->ib_active)
1777 if (udata->inlen == sizeof(struct mlx5_ib_alloc_ucontext_req))
1779 else if (udata->inlen >= min_req_v2)
1784 err = ib_copy_from_udata(&req, udata, min(udata->inlen, sizeof(req)));
1788 if (req.flags & ~MLX5_IB_ALLOC_UCTX_DEVX)
1791 if (req.comp_mask || req.reserved0 || req.reserved1 || req.reserved2)
1794 req.total_num_bfregs = ALIGN(req.total_num_bfregs,
1795 MLX5_NON_FP_BFREGS_PER_UAR);
1796 if (req.num_low_latency_bfregs > req.total_num_bfregs - 1)
1799 resp.qp_tab_size = 1 << MLX5_CAP_GEN(dev->mdev, log_max_qp);
1800 if (mlx5_core_is_pf(dev->mdev) && MLX5_CAP_GEN(dev->mdev, bf))
1801 resp.bf_reg_size = 1 << MLX5_CAP_GEN(dev->mdev, log_bf_reg_size);
1802 resp.cache_line_size = cache_line_size();
1803 resp.max_sq_desc_sz = MLX5_CAP_GEN(dev->mdev, max_wqe_sz_sq);
1804 resp.max_rq_desc_sz = MLX5_CAP_GEN(dev->mdev, max_wqe_sz_rq);
1805 resp.max_send_wqebb = 1 << MLX5_CAP_GEN(dev->mdev, log_max_qp_sz);
1806 resp.max_recv_wr = 1 << MLX5_CAP_GEN(dev->mdev, log_max_qp_sz);
1807 resp.max_srq_recv_wr = 1 << MLX5_CAP_GEN(dev->mdev, log_max_srq_sz);
1808 resp.cqe_version = min_t(__u8,
1809 (__u8)MLX5_CAP_GEN(dev->mdev, cqe_version),
1810 req.max_cqe_version);
1811 resp.log_uar_size = MLX5_CAP_GEN(dev->mdev, uar_4k) ?
1812 MLX5_ADAPTER_PAGE_SHIFT : PAGE_SHIFT;
1813 resp.num_uars_per_page = MLX5_CAP_GEN(dev->mdev, uar_4k) ?
1814 MLX5_CAP_GEN(dev->mdev, num_of_uars_per_page) : 1;
1815 resp.response_length = min(offsetof(typeof(resp), response_length) +
1816 sizeof(resp.response_length), udata->outlen);
1818 if (mlx5_accel_ipsec_device_caps(dev->mdev) & MLX5_ACCEL_IPSEC_CAP_DEVICE) {
1819 if (mlx5_get_flow_namespace(dev->mdev, MLX5_FLOW_NAMESPACE_EGRESS))
1820 resp.flow_action_flags |= MLX5_USER_ALLOC_UCONTEXT_FLOW_ACTION_FLAGS_ESP_AES_GCM;
1821 if (mlx5_accel_ipsec_device_caps(dev->mdev) & MLX5_ACCEL_IPSEC_CAP_REQUIRED_METADATA)
1822 resp.flow_action_flags |= MLX5_USER_ALLOC_UCONTEXT_FLOW_ACTION_FLAGS_ESP_AES_GCM_REQ_METADATA;
1823 if (MLX5_CAP_FLOWTABLE(dev->mdev, flow_table_properties_nic_receive.ft_field_support.outer_esp_spi))
1824 resp.flow_action_flags |= MLX5_USER_ALLOC_UCONTEXT_FLOW_ACTION_FLAGS_ESP_AES_GCM_SPI_STEERING;
1825 if (mlx5_accel_ipsec_device_caps(dev->mdev) & MLX5_ACCEL_IPSEC_CAP_TX_IV_IS_ESN)
1826 resp.flow_action_flags |= MLX5_USER_ALLOC_UCONTEXT_FLOW_ACTION_FLAGS_ESP_AES_GCM_TX_IV_IS_ESN;
1827 /* MLX5_USER_ALLOC_UCONTEXT_FLOW_ACTION_FLAGS_ESP_AES_GCM_FULL_OFFLOAD is currently always 0 */
1830 lib_uar_4k = req.lib_caps & MLX5_LIB_CAP_4K_UAR;
1831 bfregi = &context->bfregi;
1833 /* updates req->total_num_bfregs */
1834 err = calc_total_bfregs(dev, lib_uar_4k, &req, bfregi);
1838 mutex_init(&bfregi->lock);
1839 bfregi->lib_uar_4k = lib_uar_4k;
1840 bfregi->count = kcalloc(bfregi->total_num_bfregs, sizeof(*bfregi->count),
1842 if (!bfregi->count) {
1847 bfregi->sys_pages = kcalloc(bfregi->num_sys_pages,
1848 sizeof(*bfregi->sys_pages),
1850 if (!bfregi->sys_pages) {
1855 err = allocate_uars(dev, context);
1859 if (ibdev->attrs.device_cap_flags & IB_DEVICE_ON_DEMAND_PAGING)
1860 context->ibucontext.invalidate_range =
1861 &mlx5_ib_invalidate_range;
1863 if (req.flags & MLX5_IB_ALLOC_UCTX_DEVX) {
1864 err = mlx5_ib_devx_create(dev, true);
1867 context->devx_uid = err;
1870 err = mlx5_ib_alloc_transport_domain(dev, &context->tdn,
1875 if (MLX5_CAP_GEN(dev->mdev, dump_fill_mkey)) {
1876 err = mlx5_cmd_dump_fill_mkey(dev->mdev, &dump_fill_mkey);
1881 INIT_LIST_HEAD(&context->db_page_list);
1882 mutex_init(&context->db_page_mutex);
1884 resp.tot_bfregs = req.total_num_bfregs;
1885 resp.num_ports = dev->num_ports;
1887 if (field_avail(typeof(resp), cqe_version, udata->outlen))
1888 resp.response_length += sizeof(resp.cqe_version);
1890 if (field_avail(typeof(resp), cmds_supp_uhw, udata->outlen)) {
1891 resp.cmds_supp_uhw |= MLX5_USER_CMDS_SUPP_UHW_QUERY_DEVICE |
1892 MLX5_USER_CMDS_SUPP_UHW_CREATE_AH;
1893 resp.response_length += sizeof(resp.cmds_supp_uhw);
1896 if (field_avail(typeof(resp), eth_min_inline, udata->outlen)) {
1897 if (mlx5_ib_port_link_layer(ibdev, 1) == IB_LINK_LAYER_ETHERNET) {
1898 mlx5_query_min_inline(dev->mdev, &resp.eth_min_inline);
1899 resp.eth_min_inline++;
1901 resp.response_length += sizeof(resp.eth_min_inline);
1904 if (field_avail(typeof(resp), clock_info_versions, udata->outlen)) {
1905 if (mdev->clock_info)
1906 resp.clock_info_versions = BIT(MLX5_IB_CLOCK_INFO_V1);
1907 resp.response_length += sizeof(resp.clock_info_versions);
1911 * We don't want to expose information from the PCI bar that is located
1912 * after 4096 bytes, so if the arch only supports larger pages, let's
1913 * pretend we don't support reading the HCA's core clock. This is also
1914 * forced by mmap function.
1916 if (field_avail(typeof(resp), hca_core_clock_offset, udata->outlen)) {
1917 if (PAGE_SIZE <= 4096) {
1919 MLX5_IB_ALLOC_UCONTEXT_RESP_MASK_CORE_CLOCK_OFFSET;
1920 resp.hca_core_clock_offset =
1921 offsetof(struct mlx5_init_seg, internal_timer_h) % PAGE_SIZE;
1923 resp.response_length += sizeof(resp.hca_core_clock_offset);
1926 if (field_avail(typeof(resp), log_uar_size, udata->outlen))
1927 resp.response_length += sizeof(resp.log_uar_size);
1929 if (field_avail(typeof(resp), num_uars_per_page, udata->outlen))
1930 resp.response_length += sizeof(resp.num_uars_per_page);
1932 if (field_avail(typeof(resp), num_dyn_bfregs, udata->outlen)) {
1933 resp.num_dyn_bfregs = bfregi->num_dyn_bfregs;
1934 resp.response_length += sizeof(resp.num_dyn_bfregs);
1937 if (field_avail(typeof(resp), dump_fill_mkey, udata->outlen)) {
1938 if (MLX5_CAP_GEN(dev->mdev, dump_fill_mkey)) {
1939 resp.dump_fill_mkey = dump_fill_mkey;
1941 MLX5_IB_ALLOC_UCONTEXT_RESP_MASK_DUMP_FILL_MKEY;
1943 resp.response_length += sizeof(resp.dump_fill_mkey);
1946 err = ib_copy_to_udata(udata, &resp, resp.response_length);
1951 bfregi->num_low_latency_bfregs = req.num_low_latency_bfregs;
1952 context->cqe_version = resp.cqe_version;
1953 context->lib_caps = req.lib_caps;
1954 print_lib_caps(dev, context->lib_caps);
1956 if (dev->lag_active) {
1957 u8 port = mlx5_core_native_port_num(dev->mdev);
1959 atomic_set(&context->tx_port_affinity,
1961 1, &dev->roce[port].tx_port_affinity));
1967 mlx5_ib_dealloc_transport_domain(dev, context->tdn, context->devx_uid);
1969 if (req.flags & MLX5_IB_ALLOC_UCTX_DEVX)
1970 mlx5_ib_devx_destroy(dev, context->devx_uid);
1973 deallocate_uars(dev, context);
1976 kfree(bfregi->sys_pages);
1979 kfree(bfregi->count);
1985 static void mlx5_ib_dealloc_ucontext(struct ib_ucontext *ibcontext)
1987 struct mlx5_ib_ucontext *context = to_mucontext(ibcontext);
1988 struct mlx5_ib_dev *dev = to_mdev(ibcontext->device);
1989 struct mlx5_bfreg_info *bfregi;
1991 /* All umem's must be destroyed before destroying the ucontext. */
1992 mutex_lock(&ibcontext->per_mm_list_lock);
1993 WARN_ON(!list_empty(&ibcontext->per_mm_list));
1994 mutex_unlock(&ibcontext->per_mm_list_lock);
1996 bfregi = &context->bfregi;
1997 mlx5_ib_dealloc_transport_domain(dev, context->tdn, context->devx_uid);
1999 if (context->devx_uid)
2000 mlx5_ib_devx_destroy(dev, context->devx_uid);
2002 deallocate_uars(dev, context);
2003 kfree(bfregi->sys_pages);
2004 kfree(bfregi->count);
2007 static phys_addr_t uar_index2pfn(struct mlx5_ib_dev *dev,
2010 int fw_uars_per_page;
2012 fw_uars_per_page = MLX5_CAP_GEN(dev->mdev, uar_4k) ? MLX5_UARS_IN_PAGE : 1;
2014 return (dev->mdev->bar_addr >> PAGE_SHIFT) + uar_idx / fw_uars_per_page;
2017 static int get_command(unsigned long offset)
2019 return (offset >> MLX5_IB_MMAP_CMD_SHIFT) & MLX5_IB_MMAP_CMD_MASK;
2022 static int get_arg(unsigned long offset)
2024 return offset & ((1 << MLX5_IB_MMAP_CMD_SHIFT) - 1);
2027 static int get_index(unsigned long offset)
2029 return get_arg(offset);
2032 /* Index resides in an extra byte to enable larger values than 255 */
2033 static int get_extended_index(unsigned long offset)
2035 return get_arg(offset) | ((offset >> 16) & 0xff) << 8;
2039 static void mlx5_ib_disassociate_ucontext(struct ib_ucontext *ibcontext)
2043 static inline char *mmap_cmd2str(enum mlx5_ib_mmap_cmd cmd)
2046 case MLX5_IB_MMAP_WC_PAGE:
2048 case MLX5_IB_MMAP_REGULAR_PAGE:
2049 return "best effort WC";
2050 case MLX5_IB_MMAP_NC_PAGE:
2052 case MLX5_IB_MMAP_DEVICE_MEM:
2053 return "Device Memory";
2059 static int mlx5_ib_mmap_clock_info_page(struct mlx5_ib_dev *dev,
2060 struct vm_area_struct *vma,
2061 struct mlx5_ib_ucontext *context)
2063 if (vma->vm_end - vma->vm_start != PAGE_SIZE)
2066 if (get_index(vma->vm_pgoff) != MLX5_IB_CLOCK_INFO_V1)
2069 if (vma->vm_flags & VM_WRITE)
2071 vma->vm_flags &= ~VM_MAYWRITE;
2073 if (!dev->mdev->clock_info_page)
2076 return rdma_user_mmap_page(&context->ibucontext, vma,
2077 dev->mdev->clock_info_page, PAGE_SIZE);
2080 static int uar_mmap(struct mlx5_ib_dev *dev, enum mlx5_ib_mmap_cmd cmd,
2081 struct vm_area_struct *vma,
2082 struct mlx5_ib_ucontext *context)
2084 struct mlx5_bfreg_info *bfregi = &context->bfregi;
2089 u32 bfreg_dyn_idx = 0;
2091 int dyn_uar = (cmd == MLX5_IB_MMAP_ALLOC_WC);
2092 int max_valid_idx = dyn_uar ? bfregi->num_sys_pages :
2093 bfregi->num_static_sys_pages;
2095 if (vma->vm_end - vma->vm_start != PAGE_SIZE)
2099 idx = get_extended_index(vma->vm_pgoff) + bfregi->num_static_sys_pages;
2101 idx = get_index(vma->vm_pgoff);
2103 if (idx >= max_valid_idx) {
2104 mlx5_ib_warn(dev, "invalid uar index %lu, max=%d\n",
2105 idx, max_valid_idx);
2110 case MLX5_IB_MMAP_WC_PAGE:
2111 case MLX5_IB_MMAP_ALLOC_WC:
2112 /* Some architectures don't support WC memory */
2113 #if defined(CONFIG_X86)
2116 #elif !(defined(CONFIG_PPC) || (defined(CONFIG_ARM) && defined(CONFIG_MMU)))
2120 case MLX5_IB_MMAP_REGULAR_PAGE:
2121 /* For MLX5_IB_MMAP_REGULAR_PAGE do the best effort to get WC */
2122 prot = pgprot_writecombine(vma->vm_page_prot);
2124 case MLX5_IB_MMAP_NC_PAGE:
2125 prot = pgprot_noncached(vma->vm_page_prot);
2134 uars_per_page = get_uars_per_sys_page(dev, bfregi->lib_uar_4k);
2135 bfreg_dyn_idx = idx * (uars_per_page * MLX5_NON_FP_BFREGS_PER_UAR);
2136 if (bfreg_dyn_idx >= bfregi->total_num_bfregs) {
2137 mlx5_ib_warn(dev, "invalid bfreg_dyn_idx %u, max=%u\n",
2138 bfreg_dyn_idx, bfregi->total_num_bfregs);
2142 mutex_lock(&bfregi->lock);
2143 /* Fail if uar already allocated, first bfreg index of each
2144 * page holds its count.
2146 if (bfregi->count[bfreg_dyn_idx]) {
2147 mlx5_ib_warn(dev, "wrong offset, idx %lu is busy, bfregn=%u\n", idx, bfreg_dyn_idx);
2148 mutex_unlock(&bfregi->lock);
2152 bfregi->count[bfreg_dyn_idx]++;
2153 mutex_unlock(&bfregi->lock);
2155 err = mlx5_cmd_alloc_uar(dev->mdev, &uar_index);
2157 mlx5_ib_warn(dev, "UAR alloc failed\n");
2161 uar_index = bfregi->sys_pages[idx];
2164 pfn = uar_index2pfn(dev, uar_index);
2165 mlx5_ib_dbg(dev, "uar idx 0x%lx, pfn %pa\n", idx, &pfn);
2167 err = rdma_user_mmap_io(&context->ibucontext, vma, pfn, PAGE_SIZE,
2171 "rdma_user_mmap_io failed with error=%d, mmap_cmd=%s\n",
2172 err, mmap_cmd2str(cmd));
2177 bfregi->sys_pages[idx] = uar_index;
2184 mlx5_cmd_free_uar(dev->mdev, idx);
2187 mlx5_ib_free_bfreg(dev, bfregi, bfreg_dyn_idx);
2192 static int dm_mmap(struct ib_ucontext *context, struct vm_area_struct *vma)
2194 struct mlx5_ib_ucontext *mctx = to_mucontext(context);
2195 struct mlx5_ib_dev *dev = to_mdev(context->device);
2196 u16 page_idx = get_extended_index(vma->vm_pgoff);
2197 size_t map_size = vma->vm_end - vma->vm_start;
2198 u32 npages = map_size >> PAGE_SHIFT;
2201 if (find_next_zero_bit(mctx->dm_pages, page_idx + npages, page_idx) !=
2205 pfn = ((dev->mdev->bar_addr +
2206 MLX5_CAP64_DEV_MEM(dev->mdev, memic_bar_start_addr)) >>
2209 return rdma_user_mmap_io(context, vma, pfn, map_size,
2210 pgprot_writecombine(vma->vm_page_prot));
2213 static int mlx5_ib_mmap(struct ib_ucontext *ibcontext, struct vm_area_struct *vma)
2215 struct mlx5_ib_ucontext *context = to_mucontext(ibcontext);
2216 struct mlx5_ib_dev *dev = to_mdev(ibcontext->device);
2217 unsigned long command;
2220 command = get_command(vma->vm_pgoff);
2222 case MLX5_IB_MMAP_WC_PAGE:
2223 case MLX5_IB_MMAP_NC_PAGE:
2224 case MLX5_IB_MMAP_REGULAR_PAGE:
2225 case MLX5_IB_MMAP_ALLOC_WC:
2226 return uar_mmap(dev, command, vma, context);
2228 case MLX5_IB_MMAP_GET_CONTIGUOUS_PAGES:
2231 case MLX5_IB_MMAP_CORE_CLOCK:
2232 if (vma->vm_end - vma->vm_start != PAGE_SIZE)
2235 if (vma->vm_flags & VM_WRITE)
2237 vma->vm_flags &= ~VM_MAYWRITE;
2239 /* Don't expose to user-space information it shouldn't have */
2240 if (PAGE_SIZE > 4096)
2243 pfn = (dev->mdev->iseg_base +
2244 offsetof(struct mlx5_init_seg, internal_timer_h)) >>
2246 return rdma_user_mmap_io(&context->ibucontext, vma, pfn,
2248 pgprot_noncached(vma->vm_page_prot));
2249 case MLX5_IB_MMAP_CLOCK_INFO:
2250 return mlx5_ib_mmap_clock_info_page(dev, vma, context);
2252 case MLX5_IB_MMAP_DEVICE_MEM:
2253 return dm_mmap(ibcontext, vma);
2262 struct ib_dm *mlx5_ib_alloc_dm(struct ib_device *ibdev,
2263 struct ib_ucontext *context,
2264 struct ib_dm_alloc_attr *attr,
2265 struct uverbs_attr_bundle *attrs)
2267 u64 act_size = roundup(attr->length, MLX5_MEMIC_BASE_SIZE);
2268 struct mlx5_memic *memic = &to_mdev(ibdev)->memic;
2269 phys_addr_t memic_addr;
2270 struct mlx5_ib_dm *dm;
2275 dm = kzalloc(sizeof(*dm), GFP_KERNEL);
2277 return ERR_PTR(-ENOMEM);
2279 mlx5_ib_dbg(to_mdev(ibdev), "alloc_memic req: user_length=0x%llx act_length=0x%llx log_alignment=%d\n",
2280 attr->length, act_size, attr->alignment);
2282 err = mlx5_cmd_alloc_memic(memic, &memic_addr,
2283 act_size, attr->alignment);
2287 start_offset = memic_addr & ~PAGE_MASK;
2288 page_idx = (memic_addr - memic->dev->bar_addr -
2289 MLX5_CAP64_DEV_MEM(memic->dev, memic_bar_start_addr)) >>
2292 err = uverbs_copy_to(attrs,
2293 MLX5_IB_ATTR_ALLOC_DM_RESP_START_OFFSET,
2294 &start_offset, sizeof(start_offset));
2298 err = uverbs_copy_to(attrs,
2299 MLX5_IB_ATTR_ALLOC_DM_RESP_PAGE_INDEX,
2300 &page_idx, sizeof(page_idx));
2304 bitmap_set(to_mucontext(context)->dm_pages, page_idx,
2305 DIV_ROUND_UP(act_size, PAGE_SIZE));
2307 dm->dev_addr = memic_addr;
2312 mlx5_cmd_dealloc_memic(memic, memic_addr,
2316 return ERR_PTR(err);
2319 int mlx5_ib_dealloc_dm(struct ib_dm *ibdm)
2321 struct mlx5_memic *memic = &to_mdev(ibdm->device)->memic;
2322 struct mlx5_ib_dm *dm = to_mdm(ibdm);
2323 u64 act_size = roundup(dm->ibdm.length, MLX5_MEMIC_BASE_SIZE);
2327 ret = mlx5_cmd_dealloc_memic(memic, dm->dev_addr, act_size);
2331 page_idx = (dm->dev_addr - memic->dev->bar_addr -
2332 MLX5_CAP64_DEV_MEM(memic->dev, memic_bar_start_addr)) >>
2334 bitmap_clear(to_mucontext(ibdm->uobject->context)->dm_pages,
2336 DIV_ROUND_UP(act_size, PAGE_SIZE));
2343 static int mlx5_ib_alloc_pd(struct ib_pd *ibpd, struct ib_ucontext *context,
2344 struct ib_udata *udata)
2346 struct mlx5_ib_pd *pd = to_mpd(ibpd);
2347 struct ib_device *ibdev = ibpd->device;
2348 struct mlx5_ib_alloc_pd_resp resp;
2350 u32 out[MLX5_ST_SZ_DW(alloc_pd_out)] = {};
2351 u32 in[MLX5_ST_SZ_DW(alloc_pd_in)] = {};
2354 uid = context ? to_mucontext(context)->devx_uid : 0;
2355 MLX5_SET(alloc_pd_in, in, opcode, MLX5_CMD_OP_ALLOC_PD);
2356 MLX5_SET(alloc_pd_in, in, uid, uid);
2357 err = mlx5_cmd_exec(to_mdev(ibdev)->mdev, in, sizeof(in),
2362 pd->pdn = MLX5_GET(alloc_pd_out, out, pd);
2366 if (ib_copy_to_udata(udata, &resp, sizeof(resp))) {
2367 mlx5_cmd_dealloc_pd(to_mdev(ibdev)->mdev, pd->pdn, uid);
2375 static void mlx5_ib_dealloc_pd(struct ib_pd *pd)
2377 struct mlx5_ib_dev *mdev = to_mdev(pd->device);
2378 struct mlx5_ib_pd *mpd = to_mpd(pd);
2380 mlx5_cmd_dealloc_pd(mdev->mdev, mpd->pdn, mpd->uid);
2384 MATCH_CRITERIA_ENABLE_OUTER_BIT,
2385 MATCH_CRITERIA_ENABLE_MISC_BIT,
2386 MATCH_CRITERIA_ENABLE_INNER_BIT,
2387 MATCH_CRITERIA_ENABLE_MISC2_BIT
2390 #define HEADER_IS_ZERO(match_criteria, headers) \
2391 !(memchr_inv(MLX5_ADDR_OF(fte_match_param, match_criteria, headers), \
2392 0, MLX5_FLD_SZ_BYTES(fte_match_param, headers))) \
2394 static u8 get_match_criteria_enable(u32 *match_criteria)
2396 u8 match_criteria_enable;
2398 match_criteria_enable =
2399 (!HEADER_IS_ZERO(match_criteria, outer_headers)) <<
2400 MATCH_CRITERIA_ENABLE_OUTER_BIT;
2401 match_criteria_enable |=
2402 (!HEADER_IS_ZERO(match_criteria, misc_parameters)) <<
2403 MATCH_CRITERIA_ENABLE_MISC_BIT;
2404 match_criteria_enable |=
2405 (!HEADER_IS_ZERO(match_criteria, inner_headers)) <<
2406 MATCH_CRITERIA_ENABLE_INNER_BIT;
2407 match_criteria_enable |=
2408 (!HEADER_IS_ZERO(match_criteria, misc_parameters_2)) <<
2409 MATCH_CRITERIA_ENABLE_MISC2_BIT;
2411 return match_criteria_enable;
2414 static int set_proto(void *outer_c, void *outer_v, u8 mask, u8 val)
2423 entry_mask = MLX5_GET(fte_match_set_lyr_2_4, outer_c,
2425 entry_val = MLX5_GET(fte_match_set_lyr_2_4, outer_v,
2428 MLX5_SET(fte_match_set_lyr_2_4, outer_c, ip_protocol, mask);
2429 MLX5_SET(fte_match_set_lyr_2_4, outer_v, ip_protocol, val);
2432 /* Don't override existing ip protocol */
2433 if (mask != entry_mask || val != entry_val)
2439 static void set_flow_label(void *misc_c, void *misc_v, u32 mask, u32 val,
2443 MLX5_SET(fte_match_set_misc,
2444 misc_c, inner_ipv6_flow_label, mask);
2445 MLX5_SET(fte_match_set_misc,
2446 misc_v, inner_ipv6_flow_label, val);
2448 MLX5_SET(fte_match_set_misc,
2449 misc_c, outer_ipv6_flow_label, mask);
2450 MLX5_SET(fte_match_set_misc,
2451 misc_v, outer_ipv6_flow_label, val);
2455 static void set_tos(void *outer_c, void *outer_v, u8 mask, u8 val)
2457 MLX5_SET(fte_match_set_lyr_2_4, outer_c, ip_ecn, mask);
2458 MLX5_SET(fte_match_set_lyr_2_4, outer_v, ip_ecn, val);
2459 MLX5_SET(fte_match_set_lyr_2_4, outer_c, ip_dscp, mask >> 2);
2460 MLX5_SET(fte_match_set_lyr_2_4, outer_v, ip_dscp, val >> 2);
2463 static int check_mpls_supp_fields(u32 field_support, const __be32 *set_mask)
2465 if (MLX5_GET(fte_match_mpls, set_mask, mpls_label) &&
2466 !(field_support & MLX5_FIELD_SUPPORT_MPLS_LABEL))
2469 if (MLX5_GET(fte_match_mpls, set_mask, mpls_exp) &&
2470 !(field_support & MLX5_FIELD_SUPPORT_MPLS_EXP))
2473 if (MLX5_GET(fte_match_mpls, set_mask, mpls_s_bos) &&
2474 !(field_support & MLX5_FIELD_SUPPORT_MPLS_S_BOS))
2477 if (MLX5_GET(fte_match_mpls, set_mask, mpls_ttl) &&
2478 !(field_support & MLX5_FIELD_SUPPORT_MPLS_TTL))
2484 #define LAST_ETH_FIELD vlan_tag
2485 #define LAST_IB_FIELD sl
2486 #define LAST_IPV4_FIELD tos
2487 #define LAST_IPV6_FIELD traffic_class
2488 #define LAST_TCP_UDP_FIELD src_port
2489 #define LAST_TUNNEL_FIELD tunnel_id
2490 #define LAST_FLOW_TAG_FIELD tag_id
2491 #define LAST_DROP_FIELD size
2492 #define LAST_COUNTERS_FIELD counters
2494 /* Field is the last supported field */
2495 #define FIELDS_NOT_SUPPORTED(filter, field)\
2496 memchr_inv((void *)&filter.field +\
2497 sizeof(filter.field), 0,\
2499 offsetof(typeof(filter), field) -\
2500 sizeof(filter.field))
2502 int parse_flow_flow_action(struct mlx5_ib_flow_action *maction,
2504 struct mlx5_flow_act *action)
2507 switch (maction->ib_action.type) {
2508 case IB_FLOW_ACTION_ESP:
2509 if (action->action & (MLX5_FLOW_CONTEXT_ACTION_ENCRYPT |
2510 MLX5_FLOW_CONTEXT_ACTION_DECRYPT))
2512 /* Currently only AES_GCM keymat is supported by the driver */
2513 action->esp_id = (uintptr_t)maction->esp_aes_gcm.ctx;
2514 action->action |= is_egress ?
2515 MLX5_FLOW_CONTEXT_ACTION_ENCRYPT :
2516 MLX5_FLOW_CONTEXT_ACTION_DECRYPT;
2518 case IB_FLOW_ACTION_UNSPECIFIED:
2519 if (maction->flow_action_raw.sub_type ==
2520 MLX5_IB_FLOW_ACTION_MODIFY_HEADER) {
2521 if (action->action & MLX5_FLOW_CONTEXT_ACTION_MOD_HDR)
2523 action->action |= MLX5_FLOW_CONTEXT_ACTION_MOD_HDR;
2524 action->modify_id = maction->flow_action_raw.action_id;
2527 if (maction->flow_action_raw.sub_type ==
2528 MLX5_IB_FLOW_ACTION_DECAP) {
2529 if (action->action & MLX5_FLOW_CONTEXT_ACTION_DECAP)
2531 action->action |= MLX5_FLOW_CONTEXT_ACTION_DECAP;
2534 if (maction->flow_action_raw.sub_type ==
2535 MLX5_IB_FLOW_ACTION_PACKET_REFORMAT) {
2536 if (action->action &
2537 MLX5_FLOW_CONTEXT_ACTION_PACKET_REFORMAT)
2540 MLX5_FLOW_CONTEXT_ACTION_PACKET_REFORMAT;
2541 action->reformat_id =
2542 maction->flow_action_raw.action_id;
2551 static int parse_flow_attr(struct mlx5_core_dev *mdev, u32 *match_c,
2552 u32 *match_v, const union ib_flow_spec *ib_spec,
2553 const struct ib_flow_attr *flow_attr,
2554 struct mlx5_flow_act *action, u32 prev_type)
2556 void *misc_params_c = MLX5_ADDR_OF(fte_match_param, match_c,
2558 void *misc_params_v = MLX5_ADDR_OF(fte_match_param, match_v,
2560 void *misc_params2_c = MLX5_ADDR_OF(fte_match_param, match_c,
2562 void *misc_params2_v = MLX5_ADDR_OF(fte_match_param, match_v,
2569 if (ib_spec->type & IB_FLOW_SPEC_INNER) {
2570 headers_c = MLX5_ADDR_OF(fte_match_param, match_c,
2572 headers_v = MLX5_ADDR_OF(fte_match_param, match_v,
2574 match_ipv = MLX5_CAP_FLOWTABLE_NIC_RX(mdev,
2575 ft_field_support.inner_ip_version);
2577 headers_c = MLX5_ADDR_OF(fte_match_param, match_c,
2579 headers_v = MLX5_ADDR_OF(fte_match_param, match_v,
2581 match_ipv = MLX5_CAP_FLOWTABLE_NIC_RX(mdev,
2582 ft_field_support.outer_ip_version);
2585 switch (ib_spec->type & ~IB_FLOW_SPEC_INNER) {
2586 case IB_FLOW_SPEC_ETH:
2587 if (FIELDS_NOT_SUPPORTED(ib_spec->eth.mask, LAST_ETH_FIELD))
2590 ether_addr_copy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
2592 ib_spec->eth.mask.dst_mac);
2593 ether_addr_copy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
2595 ib_spec->eth.val.dst_mac);
2597 ether_addr_copy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
2599 ib_spec->eth.mask.src_mac);
2600 ether_addr_copy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
2602 ib_spec->eth.val.src_mac);
2604 if (ib_spec->eth.mask.vlan_tag) {
2605 MLX5_SET(fte_match_set_lyr_2_4, headers_c,
2607 MLX5_SET(fte_match_set_lyr_2_4, headers_v,
2610 MLX5_SET(fte_match_set_lyr_2_4, headers_c,
2611 first_vid, ntohs(ib_spec->eth.mask.vlan_tag));
2612 MLX5_SET(fte_match_set_lyr_2_4, headers_v,
2613 first_vid, ntohs(ib_spec->eth.val.vlan_tag));
2615 MLX5_SET(fte_match_set_lyr_2_4, headers_c,
2617 ntohs(ib_spec->eth.mask.vlan_tag) >> 12);
2618 MLX5_SET(fte_match_set_lyr_2_4, headers_v,
2620 ntohs(ib_spec->eth.val.vlan_tag) >> 12);
2622 MLX5_SET(fte_match_set_lyr_2_4, headers_c,
2624 ntohs(ib_spec->eth.mask.vlan_tag) >> 13);
2625 MLX5_SET(fte_match_set_lyr_2_4, headers_v,
2627 ntohs(ib_spec->eth.val.vlan_tag) >> 13);
2629 MLX5_SET(fte_match_set_lyr_2_4, headers_c,
2630 ethertype, ntohs(ib_spec->eth.mask.ether_type));
2631 MLX5_SET(fte_match_set_lyr_2_4, headers_v,
2632 ethertype, ntohs(ib_spec->eth.val.ether_type));
2634 case IB_FLOW_SPEC_IPV4:
2635 if (FIELDS_NOT_SUPPORTED(ib_spec->ipv4.mask, LAST_IPV4_FIELD))
2639 MLX5_SET(fte_match_set_lyr_2_4, headers_c,
2641 MLX5_SET(fte_match_set_lyr_2_4, headers_v,
2642 ip_version, MLX5_FS_IPV4_VERSION);
2644 MLX5_SET(fte_match_set_lyr_2_4, headers_c,
2646 MLX5_SET(fte_match_set_lyr_2_4, headers_v,
2647 ethertype, ETH_P_IP);
2650 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
2651 src_ipv4_src_ipv6.ipv4_layout.ipv4),
2652 &ib_spec->ipv4.mask.src_ip,
2653 sizeof(ib_spec->ipv4.mask.src_ip));
2654 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
2655 src_ipv4_src_ipv6.ipv4_layout.ipv4),
2656 &ib_spec->ipv4.val.src_ip,
2657 sizeof(ib_spec->ipv4.val.src_ip));
2658 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
2659 dst_ipv4_dst_ipv6.ipv4_layout.ipv4),
2660 &ib_spec->ipv4.mask.dst_ip,
2661 sizeof(ib_spec->ipv4.mask.dst_ip));
2662 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
2663 dst_ipv4_dst_ipv6.ipv4_layout.ipv4),
2664 &ib_spec->ipv4.val.dst_ip,
2665 sizeof(ib_spec->ipv4.val.dst_ip));
2667 set_tos(headers_c, headers_v,
2668 ib_spec->ipv4.mask.tos, ib_spec->ipv4.val.tos);
2670 if (set_proto(headers_c, headers_v,
2671 ib_spec->ipv4.mask.proto,
2672 ib_spec->ipv4.val.proto))
2675 case IB_FLOW_SPEC_IPV6:
2676 if (FIELDS_NOT_SUPPORTED(ib_spec->ipv6.mask, LAST_IPV6_FIELD))
2680 MLX5_SET(fte_match_set_lyr_2_4, headers_c,
2682 MLX5_SET(fte_match_set_lyr_2_4, headers_v,
2683 ip_version, MLX5_FS_IPV6_VERSION);
2685 MLX5_SET(fte_match_set_lyr_2_4, headers_c,
2687 MLX5_SET(fte_match_set_lyr_2_4, headers_v,
2688 ethertype, ETH_P_IPV6);
2691 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
2692 src_ipv4_src_ipv6.ipv6_layout.ipv6),
2693 &ib_spec->ipv6.mask.src_ip,
2694 sizeof(ib_spec->ipv6.mask.src_ip));
2695 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
2696 src_ipv4_src_ipv6.ipv6_layout.ipv6),
2697 &ib_spec->ipv6.val.src_ip,
2698 sizeof(ib_spec->ipv6.val.src_ip));
2699 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
2700 dst_ipv4_dst_ipv6.ipv6_layout.ipv6),
2701 &ib_spec->ipv6.mask.dst_ip,
2702 sizeof(ib_spec->ipv6.mask.dst_ip));
2703 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
2704 dst_ipv4_dst_ipv6.ipv6_layout.ipv6),
2705 &ib_spec->ipv6.val.dst_ip,
2706 sizeof(ib_spec->ipv6.val.dst_ip));
2708 set_tos(headers_c, headers_v,
2709 ib_spec->ipv6.mask.traffic_class,
2710 ib_spec->ipv6.val.traffic_class);
2712 if (set_proto(headers_c, headers_v,
2713 ib_spec->ipv6.mask.next_hdr,
2714 ib_spec->ipv6.val.next_hdr))
2717 set_flow_label(misc_params_c, misc_params_v,
2718 ntohl(ib_spec->ipv6.mask.flow_label),
2719 ntohl(ib_spec->ipv6.val.flow_label),
2720 ib_spec->type & IB_FLOW_SPEC_INNER);
2722 case IB_FLOW_SPEC_ESP:
2723 if (ib_spec->esp.mask.seq)
2726 MLX5_SET(fte_match_set_misc, misc_params_c, outer_esp_spi,
2727 ntohl(ib_spec->esp.mask.spi));
2728 MLX5_SET(fte_match_set_misc, misc_params_v, outer_esp_spi,
2729 ntohl(ib_spec->esp.val.spi));
2731 case IB_FLOW_SPEC_TCP:
2732 if (FIELDS_NOT_SUPPORTED(ib_spec->tcp_udp.mask,
2733 LAST_TCP_UDP_FIELD))
2736 if (set_proto(headers_c, headers_v, 0xff, IPPROTO_TCP))
2739 MLX5_SET(fte_match_set_lyr_2_4, headers_c, tcp_sport,
2740 ntohs(ib_spec->tcp_udp.mask.src_port));
2741 MLX5_SET(fte_match_set_lyr_2_4, headers_v, tcp_sport,
2742 ntohs(ib_spec->tcp_udp.val.src_port));
2744 MLX5_SET(fte_match_set_lyr_2_4, headers_c, tcp_dport,
2745 ntohs(ib_spec->tcp_udp.mask.dst_port));
2746 MLX5_SET(fte_match_set_lyr_2_4, headers_v, tcp_dport,
2747 ntohs(ib_spec->tcp_udp.val.dst_port));
2749 case IB_FLOW_SPEC_UDP:
2750 if (FIELDS_NOT_SUPPORTED(ib_spec->tcp_udp.mask,
2751 LAST_TCP_UDP_FIELD))
2754 if (set_proto(headers_c, headers_v, 0xff, IPPROTO_UDP))
2757 MLX5_SET(fte_match_set_lyr_2_4, headers_c, udp_sport,
2758 ntohs(ib_spec->tcp_udp.mask.src_port));
2759 MLX5_SET(fte_match_set_lyr_2_4, headers_v, udp_sport,
2760 ntohs(ib_spec->tcp_udp.val.src_port));
2762 MLX5_SET(fte_match_set_lyr_2_4, headers_c, udp_dport,
2763 ntohs(ib_spec->tcp_udp.mask.dst_port));
2764 MLX5_SET(fte_match_set_lyr_2_4, headers_v, udp_dport,
2765 ntohs(ib_spec->tcp_udp.val.dst_port));
2767 case IB_FLOW_SPEC_GRE:
2768 if (ib_spec->gre.mask.c_ks_res0_ver)
2771 if (set_proto(headers_c, headers_v, 0xff, IPPROTO_GRE))
2774 MLX5_SET(fte_match_set_lyr_2_4, headers_c, ip_protocol,
2776 MLX5_SET(fte_match_set_lyr_2_4, headers_v, ip_protocol,
2779 MLX5_SET(fte_match_set_misc, misc_params_c, gre_protocol,
2780 ntohs(ib_spec->gre.mask.protocol));
2781 MLX5_SET(fte_match_set_misc, misc_params_v, gre_protocol,
2782 ntohs(ib_spec->gre.val.protocol));
2784 memcpy(MLX5_ADDR_OF(fte_match_set_misc, misc_params_c,
2786 &ib_spec->gre.mask.key,
2787 sizeof(ib_spec->gre.mask.key));
2788 memcpy(MLX5_ADDR_OF(fte_match_set_misc, misc_params_v,
2790 &ib_spec->gre.val.key,
2791 sizeof(ib_spec->gre.val.key));
2793 case IB_FLOW_SPEC_MPLS:
2794 switch (prev_type) {
2795 case IB_FLOW_SPEC_UDP:
2796 if (check_mpls_supp_fields(MLX5_CAP_FLOWTABLE_NIC_RX(mdev,
2797 ft_field_support.outer_first_mpls_over_udp),
2798 &ib_spec->mpls.mask.tag))
2801 memcpy(MLX5_ADDR_OF(fte_match_set_misc2, misc_params2_v,
2802 outer_first_mpls_over_udp),
2803 &ib_spec->mpls.val.tag,
2804 sizeof(ib_spec->mpls.val.tag));
2805 memcpy(MLX5_ADDR_OF(fte_match_set_misc2, misc_params2_c,
2806 outer_first_mpls_over_udp),
2807 &ib_spec->mpls.mask.tag,
2808 sizeof(ib_spec->mpls.mask.tag));
2810 case IB_FLOW_SPEC_GRE:
2811 if (check_mpls_supp_fields(MLX5_CAP_FLOWTABLE_NIC_RX(mdev,
2812 ft_field_support.outer_first_mpls_over_gre),
2813 &ib_spec->mpls.mask.tag))
2816 memcpy(MLX5_ADDR_OF(fte_match_set_misc2, misc_params2_v,
2817 outer_first_mpls_over_gre),
2818 &ib_spec->mpls.val.tag,
2819 sizeof(ib_spec->mpls.val.tag));
2820 memcpy(MLX5_ADDR_OF(fte_match_set_misc2, misc_params2_c,
2821 outer_first_mpls_over_gre),
2822 &ib_spec->mpls.mask.tag,
2823 sizeof(ib_spec->mpls.mask.tag));
2826 if (ib_spec->type & IB_FLOW_SPEC_INNER) {
2827 if (check_mpls_supp_fields(MLX5_CAP_FLOWTABLE_NIC_RX(mdev,
2828 ft_field_support.inner_first_mpls),
2829 &ib_spec->mpls.mask.tag))
2832 memcpy(MLX5_ADDR_OF(fte_match_set_misc2, misc_params2_v,
2834 &ib_spec->mpls.val.tag,
2835 sizeof(ib_spec->mpls.val.tag));
2836 memcpy(MLX5_ADDR_OF(fte_match_set_misc2, misc_params2_c,
2838 &ib_spec->mpls.mask.tag,
2839 sizeof(ib_spec->mpls.mask.tag));
2841 if (check_mpls_supp_fields(MLX5_CAP_FLOWTABLE_NIC_RX(mdev,
2842 ft_field_support.outer_first_mpls),
2843 &ib_spec->mpls.mask.tag))
2846 memcpy(MLX5_ADDR_OF(fte_match_set_misc2, misc_params2_v,
2848 &ib_spec->mpls.val.tag,
2849 sizeof(ib_spec->mpls.val.tag));
2850 memcpy(MLX5_ADDR_OF(fte_match_set_misc2, misc_params2_c,
2852 &ib_spec->mpls.mask.tag,
2853 sizeof(ib_spec->mpls.mask.tag));
2857 case IB_FLOW_SPEC_VXLAN_TUNNEL:
2858 if (FIELDS_NOT_SUPPORTED(ib_spec->tunnel.mask,
2862 MLX5_SET(fte_match_set_misc, misc_params_c, vxlan_vni,
2863 ntohl(ib_spec->tunnel.mask.tunnel_id));
2864 MLX5_SET(fte_match_set_misc, misc_params_v, vxlan_vni,
2865 ntohl(ib_spec->tunnel.val.tunnel_id));
2867 case IB_FLOW_SPEC_ACTION_TAG:
2868 if (FIELDS_NOT_SUPPORTED(ib_spec->flow_tag,
2869 LAST_FLOW_TAG_FIELD))
2871 if (ib_spec->flow_tag.tag_id >= BIT(24))
2874 action->flow_tag = ib_spec->flow_tag.tag_id;
2875 action->flags |= FLOW_ACT_HAS_TAG;
2877 case IB_FLOW_SPEC_ACTION_DROP:
2878 if (FIELDS_NOT_SUPPORTED(ib_spec->drop,
2881 action->action |= MLX5_FLOW_CONTEXT_ACTION_DROP;
2883 case IB_FLOW_SPEC_ACTION_HANDLE:
2884 ret = parse_flow_flow_action(to_mflow_act(ib_spec->action.act),
2885 flow_attr->flags & IB_FLOW_ATTR_FLAGS_EGRESS, action);
2889 case IB_FLOW_SPEC_ACTION_COUNT:
2890 if (FIELDS_NOT_SUPPORTED(ib_spec->flow_count,
2891 LAST_COUNTERS_FIELD))
2894 /* for now support only one counters spec per flow */
2895 if (action->action & MLX5_FLOW_CONTEXT_ACTION_COUNT)
2898 action->counters = ib_spec->flow_count.counters;
2899 action->action |= MLX5_FLOW_CONTEXT_ACTION_COUNT;
2908 /* If a flow could catch both multicast and unicast packets,
2909 * it won't fall into the multicast flow steering table and this rule
2910 * could steal other multicast packets.
2912 static bool flow_is_multicast_only(const struct ib_flow_attr *ib_attr)
2914 union ib_flow_spec *flow_spec;
2916 if (ib_attr->type != IB_FLOW_ATTR_NORMAL ||
2917 ib_attr->num_of_specs < 1)
2920 flow_spec = (union ib_flow_spec *)(ib_attr + 1);
2921 if (flow_spec->type == IB_FLOW_SPEC_IPV4) {
2922 struct ib_flow_spec_ipv4 *ipv4_spec;
2924 ipv4_spec = (struct ib_flow_spec_ipv4 *)flow_spec;
2925 if (ipv4_is_multicast(ipv4_spec->val.dst_ip))
2931 if (flow_spec->type == IB_FLOW_SPEC_ETH) {
2932 struct ib_flow_spec_eth *eth_spec;
2934 eth_spec = (struct ib_flow_spec_eth *)flow_spec;
2935 return is_multicast_ether_addr(eth_spec->mask.dst_mac) &&
2936 is_multicast_ether_addr(eth_spec->val.dst_mac);
2948 static enum valid_spec
2949 is_valid_esp_aes_gcm(struct mlx5_core_dev *mdev,
2950 const struct mlx5_flow_spec *spec,
2951 const struct mlx5_flow_act *flow_act,
2954 const u32 *match_c = spec->match_criteria;
2956 (flow_act->action & (MLX5_FLOW_CONTEXT_ACTION_ENCRYPT |
2957 MLX5_FLOW_CONTEXT_ACTION_DECRYPT));
2958 bool is_ipsec = mlx5_fs_is_ipsec_flow(match_c);
2959 bool is_drop = flow_act->action & MLX5_FLOW_CONTEXT_ACTION_DROP;
2962 * Currently only crypto is supported in egress, when regular egress
2963 * rules would be supported, always return VALID_SPEC_NA.
2966 return VALID_SPEC_NA;
2968 return is_crypto && is_ipsec &&
2969 (!egress || (!is_drop && !(flow_act->flags & FLOW_ACT_HAS_TAG))) ?
2970 VALID_SPEC_VALID : VALID_SPEC_INVALID;
2973 static bool is_valid_spec(struct mlx5_core_dev *mdev,
2974 const struct mlx5_flow_spec *spec,
2975 const struct mlx5_flow_act *flow_act,
2978 /* We curretly only support ipsec egress flow */
2979 return is_valid_esp_aes_gcm(mdev, spec, flow_act, egress) != VALID_SPEC_INVALID;
2982 static bool is_valid_ethertype(struct mlx5_core_dev *mdev,
2983 const struct ib_flow_attr *flow_attr,
2986 union ib_flow_spec *ib_spec = (union ib_flow_spec *)(flow_attr + 1);
2987 int match_ipv = check_inner ?
2988 MLX5_CAP_FLOWTABLE_NIC_RX(mdev,
2989 ft_field_support.inner_ip_version) :
2990 MLX5_CAP_FLOWTABLE_NIC_RX(mdev,
2991 ft_field_support.outer_ip_version);
2992 int inner_bit = check_inner ? IB_FLOW_SPEC_INNER : 0;
2993 bool ipv4_spec_valid, ipv6_spec_valid;
2994 unsigned int ip_spec_type = 0;
2995 bool has_ethertype = false;
2996 unsigned int spec_index;
2997 bool mask_valid = true;
3001 /* Validate that ethertype is correct */
3002 for (spec_index = 0; spec_index < flow_attr->num_of_specs; spec_index++) {
3003 if ((ib_spec->type == (IB_FLOW_SPEC_ETH | inner_bit)) &&
3004 ib_spec->eth.mask.ether_type) {
3005 mask_valid = (ib_spec->eth.mask.ether_type ==
3007 has_ethertype = true;
3008 eth_type = ntohs(ib_spec->eth.val.ether_type);
3009 } else if ((ib_spec->type == (IB_FLOW_SPEC_IPV4 | inner_bit)) ||
3010 (ib_spec->type == (IB_FLOW_SPEC_IPV6 | inner_bit))) {
3011 ip_spec_type = ib_spec->type;
3013 ib_spec = (void *)ib_spec + ib_spec->size;
3016 type_valid = (!has_ethertype) || (!ip_spec_type);
3017 if (!type_valid && mask_valid) {
3018 ipv4_spec_valid = (eth_type == ETH_P_IP) &&
3019 (ip_spec_type == (IB_FLOW_SPEC_IPV4 | inner_bit));
3020 ipv6_spec_valid = (eth_type == ETH_P_IPV6) &&
3021 (ip_spec_type == (IB_FLOW_SPEC_IPV6 | inner_bit));
3023 type_valid = (ipv4_spec_valid) || (ipv6_spec_valid) ||
3024 (((eth_type == ETH_P_MPLS_UC) ||
3025 (eth_type == ETH_P_MPLS_MC)) && match_ipv);
3031 static bool is_valid_attr(struct mlx5_core_dev *mdev,
3032 const struct ib_flow_attr *flow_attr)
3034 return is_valid_ethertype(mdev, flow_attr, false) &&
3035 is_valid_ethertype(mdev, flow_attr, true);
3038 static void put_flow_table(struct mlx5_ib_dev *dev,
3039 struct mlx5_ib_flow_prio *prio, bool ft_added)
3041 prio->refcount -= !!ft_added;
3042 if (!prio->refcount) {
3043 mlx5_destroy_flow_table(prio->flow_table);
3044 prio->flow_table = NULL;
3048 static void counters_clear_description(struct ib_counters *counters)
3050 struct mlx5_ib_mcounters *mcounters = to_mcounters(counters);
3052 mutex_lock(&mcounters->mcntrs_mutex);
3053 kfree(mcounters->counters_data);
3054 mcounters->counters_data = NULL;
3055 mcounters->cntrs_max_index = 0;
3056 mutex_unlock(&mcounters->mcntrs_mutex);
3059 static int mlx5_ib_destroy_flow(struct ib_flow *flow_id)
3061 struct mlx5_ib_flow_handler *handler = container_of(flow_id,
3062 struct mlx5_ib_flow_handler,
3064 struct mlx5_ib_flow_handler *iter, *tmp;
3065 struct mlx5_ib_dev *dev = handler->dev;
3067 mutex_lock(&dev->flow_db->lock);
3069 list_for_each_entry_safe(iter, tmp, &handler->list, list) {
3070 mlx5_del_flow_rules(iter->rule);
3071 put_flow_table(dev, iter->prio, true);
3072 list_del(&iter->list);
3076 mlx5_del_flow_rules(handler->rule);
3077 put_flow_table(dev, handler->prio, true);
3078 if (handler->ibcounters &&
3079 atomic_read(&handler->ibcounters->usecnt) == 1)
3080 counters_clear_description(handler->ibcounters);
3082 mutex_unlock(&dev->flow_db->lock);
3083 if (handler->flow_matcher)
3084 atomic_dec(&handler->flow_matcher->usecnt);
3090 static int ib_prio_to_core_prio(unsigned int priority, bool dont_trap)
3098 enum flow_table_type {
3103 #define MLX5_FS_MAX_TYPES 6
3104 #define MLX5_FS_MAX_ENTRIES BIT(16)
3106 static struct mlx5_ib_flow_prio *_get_prio(struct mlx5_flow_namespace *ns,
3107 struct mlx5_ib_flow_prio *prio,
3109 int num_entries, int num_groups,
3112 struct mlx5_flow_table *ft;
3114 ft = mlx5_create_auto_grouped_flow_table(ns, priority,
3119 return ERR_CAST(ft);
3121 prio->flow_table = ft;
3126 static struct mlx5_ib_flow_prio *get_flow_table(struct mlx5_ib_dev *dev,
3127 struct ib_flow_attr *flow_attr,
3128 enum flow_table_type ft_type)
3130 bool dont_trap = flow_attr->flags & IB_FLOW_ATTR_FLAGS_DONT_TRAP;
3131 struct mlx5_flow_namespace *ns = NULL;
3132 struct mlx5_ib_flow_prio *prio;
3133 struct mlx5_flow_table *ft;
3140 max_table_size = BIT(MLX5_CAP_FLOWTABLE_NIC_RX(dev->mdev,
3142 if (flow_attr->type == IB_FLOW_ATTR_NORMAL) {
3143 enum mlx5_flow_namespace_type fn_type;
3145 if (flow_is_multicast_only(flow_attr) &&
3147 priority = MLX5_IB_FLOW_MCAST_PRIO;
3149 priority = ib_prio_to_core_prio(flow_attr->priority,
3151 if (ft_type == MLX5_IB_FT_RX) {
3152 fn_type = MLX5_FLOW_NAMESPACE_BYPASS;
3153 prio = &dev->flow_db->prios[priority];
3155 MLX5_CAP_FLOWTABLE_NIC_RX(dev->mdev, decap))
3156 flags |= MLX5_FLOW_TABLE_TUNNEL_EN_DECAP;
3158 MLX5_CAP_FLOWTABLE_NIC_RX(dev->mdev,
3159 reformat_l3_tunnel_to_l2))
3160 flags |= MLX5_FLOW_TABLE_TUNNEL_EN_REFORMAT;
3163 BIT(MLX5_CAP_FLOWTABLE_NIC_TX(dev->mdev,
3165 fn_type = MLX5_FLOW_NAMESPACE_EGRESS;
3166 prio = &dev->flow_db->egress_prios[priority];
3168 MLX5_CAP_FLOWTABLE_NIC_TX(dev->mdev, reformat))
3169 flags |= MLX5_FLOW_TABLE_TUNNEL_EN_REFORMAT;
3171 ns = mlx5_get_flow_namespace(dev->mdev, fn_type);
3172 num_entries = MLX5_FS_MAX_ENTRIES;
3173 num_groups = MLX5_FS_MAX_TYPES;
3174 } else if (flow_attr->type == IB_FLOW_ATTR_ALL_DEFAULT ||
3175 flow_attr->type == IB_FLOW_ATTR_MC_DEFAULT) {
3176 ns = mlx5_get_flow_namespace(dev->mdev,
3177 MLX5_FLOW_NAMESPACE_LEFTOVERS);
3178 build_leftovers_ft_param(&priority,
3181 prio = &dev->flow_db->prios[MLX5_IB_FLOW_LEFTOVERS_PRIO];
3182 } else if (flow_attr->type == IB_FLOW_ATTR_SNIFFER) {
3183 if (!MLX5_CAP_FLOWTABLE(dev->mdev,
3184 allow_sniffer_and_nic_rx_shared_tir))
3185 return ERR_PTR(-ENOTSUPP);
3187 ns = mlx5_get_flow_namespace(dev->mdev, ft_type == MLX5_IB_FT_RX ?
3188 MLX5_FLOW_NAMESPACE_SNIFFER_RX :
3189 MLX5_FLOW_NAMESPACE_SNIFFER_TX);
3191 prio = &dev->flow_db->sniffer[ft_type];
3198 return ERR_PTR(-ENOTSUPP);
3200 if (num_entries > max_table_size)
3201 return ERR_PTR(-ENOMEM);
3203 ft = prio->flow_table;
3205 return _get_prio(ns, prio, priority, num_entries, num_groups,
3211 static void set_underlay_qp(struct mlx5_ib_dev *dev,
3212 struct mlx5_flow_spec *spec,
3215 void *misc_params_c = MLX5_ADDR_OF(fte_match_param,
3216 spec->match_criteria,
3218 void *misc_params_v = MLX5_ADDR_OF(fte_match_param, spec->match_value,
3222 MLX5_CAP_FLOWTABLE_NIC_RX(dev->mdev,
3223 ft_field_support.bth_dst_qp)) {
3224 MLX5_SET(fte_match_set_misc,
3225 misc_params_v, bth_dst_qp, underlay_qpn);
3226 MLX5_SET(fte_match_set_misc,
3227 misc_params_c, bth_dst_qp, 0xffffff);
3231 static int read_flow_counters(struct ib_device *ibdev,
3232 struct mlx5_read_counters_attr *read_attr)
3234 struct mlx5_fc *fc = read_attr->hw_cntrs_hndl;
3235 struct mlx5_ib_dev *dev = to_mdev(ibdev);
3237 return mlx5_fc_query(dev->mdev, fc,
3238 &read_attr->out[IB_COUNTER_PACKETS],
3239 &read_attr->out[IB_COUNTER_BYTES]);
3242 /* flow counters currently expose two counters packets and bytes */
3243 #define FLOW_COUNTERS_NUM 2
3244 static int counters_set_description(struct ib_counters *counters,
3245 enum mlx5_ib_counters_type counters_type,
3246 struct mlx5_ib_flow_counters_desc *desc_data,
3249 struct mlx5_ib_mcounters *mcounters = to_mcounters(counters);
3250 u32 cntrs_max_index = 0;
3253 if (counters_type != MLX5_IB_COUNTERS_FLOW)
3256 /* init the fields for the object */
3257 mcounters->type = counters_type;
3258 mcounters->read_counters = read_flow_counters;
3259 mcounters->counters_num = FLOW_COUNTERS_NUM;
3260 mcounters->ncounters = ncounters;
3261 /* each counter entry have both description and index pair */
3262 for (i = 0; i < ncounters; i++) {
3263 if (desc_data[i].description > IB_COUNTER_BYTES)
3266 if (cntrs_max_index <= desc_data[i].index)
3267 cntrs_max_index = desc_data[i].index + 1;
3270 mutex_lock(&mcounters->mcntrs_mutex);
3271 mcounters->counters_data = desc_data;
3272 mcounters->cntrs_max_index = cntrs_max_index;
3273 mutex_unlock(&mcounters->mcntrs_mutex);
3278 #define MAX_COUNTERS_NUM (USHRT_MAX / (sizeof(u32) * 2))
3279 static int flow_counters_set_data(struct ib_counters *ibcounters,
3280 struct mlx5_ib_create_flow *ucmd)
3282 struct mlx5_ib_mcounters *mcounters = to_mcounters(ibcounters);
3283 struct mlx5_ib_flow_counters_data *cntrs_data = NULL;
3284 struct mlx5_ib_flow_counters_desc *desc_data = NULL;
3285 bool hw_hndl = false;
3288 if (ucmd && ucmd->ncounters_data != 0) {
3289 cntrs_data = ucmd->data;
3290 if (cntrs_data->ncounters > MAX_COUNTERS_NUM)
3293 desc_data = kcalloc(cntrs_data->ncounters,
3299 if (copy_from_user(desc_data,
3300 u64_to_user_ptr(cntrs_data->counters_data),
3301 sizeof(*desc_data) * cntrs_data->ncounters)) {
3307 if (!mcounters->hw_cntrs_hndl) {
3308 mcounters->hw_cntrs_hndl = mlx5_fc_create(
3309 to_mdev(ibcounters->device)->mdev, false);
3310 if (IS_ERR(mcounters->hw_cntrs_hndl)) {
3311 ret = PTR_ERR(mcounters->hw_cntrs_hndl);
3318 /* counters already bound to at least one flow */
3319 if (mcounters->cntrs_max_index) {
3324 ret = counters_set_description(ibcounters,
3325 MLX5_IB_COUNTERS_FLOW,
3327 cntrs_data->ncounters);
3331 } else if (!mcounters->cntrs_max_index) {
3332 /* counters not bound yet, must have udata passed */
3341 mlx5_fc_destroy(to_mdev(ibcounters->device)->mdev,
3342 mcounters->hw_cntrs_hndl);
3343 mcounters->hw_cntrs_hndl = NULL;
3350 static struct mlx5_ib_flow_handler *_create_flow_rule(struct mlx5_ib_dev *dev,
3351 struct mlx5_ib_flow_prio *ft_prio,
3352 const struct ib_flow_attr *flow_attr,
3353 struct mlx5_flow_destination *dst,
3355 struct mlx5_ib_create_flow *ucmd)
3357 struct mlx5_flow_table *ft = ft_prio->flow_table;
3358 struct mlx5_ib_flow_handler *handler;
3359 struct mlx5_flow_act flow_act = {.flow_tag = MLX5_FS_DEFAULT_FLOW_TAG};
3360 struct mlx5_flow_spec *spec;
3361 struct mlx5_flow_destination dest_arr[2] = {};
3362 struct mlx5_flow_destination *rule_dst = dest_arr;
3363 const void *ib_flow = (const void *)flow_attr + sizeof(*flow_attr);
3364 unsigned int spec_index;
3368 bool is_egress = flow_attr->flags & IB_FLOW_ATTR_FLAGS_EGRESS;
3370 if (!is_valid_attr(dev->mdev, flow_attr))
3371 return ERR_PTR(-EINVAL);
3373 if (dev->rep && is_egress)
3374 return ERR_PTR(-EINVAL);
3376 spec = kvzalloc(sizeof(*spec), GFP_KERNEL);
3377 handler = kzalloc(sizeof(*handler), GFP_KERNEL);
3378 if (!handler || !spec) {
3383 INIT_LIST_HEAD(&handler->list);
3385 memcpy(&dest_arr[0], dst, sizeof(*dst));
3389 for (spec_index = 0; spec_index < flow_attr->num_of_specs; spec_index++) {
3390 err = parse_flow_attr(dev->mdev, spec->match_criteria,
3392 ib_flow, flow_attr, &flow_act,
3397 prev_type = ((union ib_flow_spec *)ib_flow)->type;
3398 ib_flow += ((union ib_flow_spec *)ib_flow)->size;
3401 if (!flow_is_multicast_only(flow_attr))
3402 set_underlay_qp(dev, spec, underlay_qpn);
3407 misc = MLX5_ADDR_OF(fte_match_param, spec->match_value,
3409 MLX5_SET(fte_match_set_misc, misc, source_port,
3411 misc = MLX5_ADDR_OF(fte_match_param, spec->match_criteria,
3413 MLX5_SET_TO_ONES(fte_match_set_misc, misc, source_port);
3416 spec->match_criteria_enable = get_match_criteria_enable(spec->match_criteria);
3419 !is_valid_spec(dev->mdev, spec, &flow_act, is_egress)) {
3424 if (flow_act.action & MLX5_FLOW_CONTEXT_ACTION_COUNT) {
3425 struct mlx5_ib_mcounters *mcounters;
3427 err = flow_counters_set_data(flow_act.counters, ucmd);
3431 mcounters = to_mcounters(flow_act.counters);
3432 handler->ibcounters = flow_act.counters;
3433 dest_arr[dest_num].type =
3434 MLX5_FLOW_DESTINATION_TYPE_COUNTER;
3435 dest_arr[dest_num].counter_id =
3436 mlx5_fc_id(mcounters->hw_cntrs_hndl);
3440 if (flow_act.action & MLX5_FLOW_CONTEXT_ACTION_DROP) {
3441 if (!(flow_act.action & MLX5_FLOW_CONTEXT_ACTION_COUNT)) {
3447 flow_act.action |= MLX5_FLOW_CONTEXT_ACTION_ALLOW;
3450 dest_num ? MLX5_FLOW_CONTEXT_ACTION_FWD_DEST :
3451 MLX5_FLOW_CONTEXT_ACTION_FWD_NEXT_PRIO;
3454 if ((flow_act.flags & FLOW_ACT_HAS_TAG) &&
3455 (flow_attr->type == IB_FLOW_ATTR_ALL_DEFAULT ||
3456 flow_attr->type == IB_FLOW_ATTR_MC_DEFAULT)) {
3457 mlx5_ib_warn(dev, "Flow tag %u and attribute type %x isn't allowed in leftovers\n",
3458 flow_act.flow_tag, flow_attr->type);
3462 handler->rule = mlx5_add_flow_rules(ft, spec,
3464 rule_dst, dest_num);
3466 if (IS_ERR(handler->rule)) {
3467 err = PTR_ERR(handler->rule);
3471 ft_prio->refcount++;
3472 handler->prio = ft_prio;
3475 ft_prio->flow_table = ft;
3477 if (err && handler) {
3478 if (handler->ibcounters &&
3479 atomic_read(&handler->ibcounters->usecnt) == 1)
3480 counters_clear_description(handler->ibcounters);
3484 return err ? ERR_PTR(err) : handler;
3487 static struct mlx5_ib_flow_handler *create_flow_rule(struct mlx5_ib_dev *dev,
3488 struct mlx5_ib_flow_prio *ft_prio,
3489 const struct ib_flow_attr *flow_attr,
3490 struct mlx5_flow_destination *dst)
3492 return _create_flow_rule(dev, ft_prio, flow_attr, dst, 0, NULL);
3495 static struct mlx5_ib_flow_handler *create_dont_trap_rule(struct mlx5_ib_dev *dev,
3496 struct mlx5_ib_flow_prio *ft_prio,
3497 struct ib_flow_attr *flow_attr,
3498 struct mlx5_flow_destination *dst)
3500 struct mlx5_ib_flow_handler *handler_dst = NULL;
3501 struct mlx5_ib_flow_handler *handler = NULL;
3503 handler = create_flow_rule(dev, ft_prio, flow_attr, NULL);
3504 if (!IS_ERR(handler)) {
3505 handler_dst = create_flow_rule(dev, ft_prio,
3507 if (IS_ERR(handler_dst)) {
3508 mlx5_del_flow_rules(handler->rule);
3509 ft_prio->refcount--;
3511 handler = handler_dst;
3513 list_add(&handler_dst->list, &handler->list);
3524 static struct mlx5_ib_flow_handler *create_leftovers_rule(struct mlx5_ib_dev *dev,
3525 struct mlx5_ib_flow_prio *ft_prio,
3526 struct ib_flow_attr *flow_attr,
3527 struct mlx5_flow_destination *dst)
3529 struct mlx5_ib_flow_handler *handler_ucast = NULL;
3530 struct mlx5_ib_flow_handler *handler = NULL;
3533 struct ib_flow_attr flow_attr;
3534 struct ib_flow_spec_eth eth_flow;
3535 } leftovers_specs[] = {
3539 .size = sizeof(leftovers_specs[0])
3542 .type = IB_FLOW_SPEC_ETH,
3543 .size = sizeof(struct ib_flow_spec_eth),
3544 .mask = {.dst_mac = {0x1} },
3545 .val = {.dst_mac = {0x1} }
3551 .size = sizeof(leftovers_specs[0])
3554 .type = IB_FLOW_SPEC_ETH,
3555 .size = sizeof(struct ib_flow_spec_eth),
3556 .mask = {.dst_mac = {0x1} },
3557 .val = {.dst_mac = {} }
3562 handler = create_flow_rule(dev, ft_prio,
3563 &leftovers_specs[LEFTOVERS_MC].flow_attr,
3565 if (!IS_ERR(handler) &&
3566 flow_attr->type == IB_FLOW_ATTR_ALL_DEFAULT) {
3567 handler_ucast = create_flow_rule(dev, ft_prio,
3568 &leftovers_specs[LEFTOVERS_UC].flow_attr,
3570 if (IS_ERR(handler_ucast)) {
3571 mlx5_del_flow_rules(handler->rule);
3572 ft_prio->refcount--;
3574 handler = handler_ucast;
3576 list_add(&handler_ucast->list, &handler->list);
3583 static struct mlx5_ib_flow_handler *create_sniffer_rule(struct mlx5_ib_dev *dev,
3584 struct mlx5_ib_flow_prio *ft_rx,
3585 struct mlx5_ib_flow_prio *ft_tx,
3586 struct mlx5_flow_destination *dst)
3588 struct mlx5_ib_flow_handler *handler_rx;
3589 struct mlx5_ib_flow_handler *handler_tx;
3591 static const struct ib_flow_attr flow_attr = {
3593 .size = sizeof(flow_attr)
3596 handler_rx = create_flow_rule(dev, ft_rx, &flow_attr, dst);
3597 if (IS_ERR(handler_rx)) {
3598 err = PTR_ERR(handler_rx);
3602 handler_tx = create_flow_rule(dev, ft_tx, &flow_attr, dst);
3603 if (IS_ERR(handler_tx)) {
3604 err = PTR_ERR(handler_tx);
3608 list_add(&handler_tx->list, &handler_rx->list);
3613 mlx5_del_flow_rules(handler_rx->rule);
3617 return ERR_PTR(err);
3620 static struct ib_flow *mlx5_ib_create_flow(struct ib_qp *qp,
3621 struct ib_flow_attr *flow_attr,
3623 struct ib_udata *udata)
3625 struct mlx5_ib_dev *dev = to_mdev(qp->device);
3626 struct mlx5_ib_qp *mqp = to_mqp(qp);
3627 struct mlx5_ib_flow_handler *handler = NULL;
3628 struct mlx5_flow_destination *dst = NULL;
3629 struct mlx5_ib_flow_prio *ft_prio_tx = NULL;
3630 struct mlx5_ib_flow_prio *ft_prio;
3631 bool is_egress = flow_attr->flags & IB_FLOW_ATTR_FLAGS_EGRESS;
3632 struct mlx5_ib_create_flow *ucmd = NULL, ucmd_hdr;
3633 size_t min_ucmd_sz, required_ucmd_sz;
3637 if (udata && udata->inlen) {
3638 min_ucmd_sz = offsetof(typeof(ucmd_hdr), reserved) +
3639 sizeof(ucmd_hdr.reserved);
3640 if (udata->inlen < min_ucmd_sz)
3641 return ERR_PTR(-EOPNOTSUPP);
3643 err = ib_copy_from_udata(&ucmd_hdr, udata, min_ucmd_sz);
3645 return ERR_PTR(err);
3647 /* currently supports only one counters data */
3648 if (ucmd_hdr.ncounters_data > 1)
3649 return ERR_PTR(-EINVAL);
3651 required_ucmd_sz = min_ucmd_sz +
3652 sizeof(struct mlx5_ib_flow_counters_data) *
3653 ucmd_hdr.ncounters_data;
3654 if (udata->inlen > required_ucmd_sz &&
3655 !ib_is_udata_cleared(udata, required_ucmd_sz,
3656 udata->inlen - required_ucmd_sz))
3657 return ERR_PTR(-EOPNOTSUPP);
3659 ucmd = kzalloc(required_ucmd_sz, GFP_KERNEL);
3661 return ERR_PTR(-ENOMEM);
3663 err = ib_copy_from_udata(ucmd, udata, required_ucmd_sz);
3668 if (flow_attr->priority > MLX5_IB_FLOW_LAST_PRIO) {
3673 if (domain != IB_FLOW_DOMAIN_USER ||
3674 flow_attr->port > dev->num_ports ||
3675 (flow_attr->flags & ~(IB_FLOW_ATTR_FLAGS_DONT_TRAP |
3676 IB_FLOW_ATTR_FLAGS_EGRESS))) {
3682 (flow_attr->type == IB_FLOW_ATTR_ALL_DEFAULT ||
3683 flow_attr->type == IB_FLOW_ATTR_MC_DEFAULT)) {
3688 dst = kzalloc(sizeof(*dst), GFP_KERNEL);
3694 mutex_lock(&dev->flow_db->lock);
3696 ft_prio = get_flow_table(dev, flow_attr,
3697 is_egress ? MLX5_IB_FT_TX : MLX5_IB_FT_RX);
3698 if (IS_ERR(ft_prio)) {
3699 err = PTR_ERR(ft_prio);
3702 if (flow_attr->type == IB_FLOW_ATTR_SNIFFER) {
3703 ft_prio_tx = get_flow_table(dev, flow_attr, MLX5_IB_FT_TX);
3704 if (IS_ERR(ft_prio_tx)) {
3705 err = PTR_ERR(ft_prio_tx);
3712 dst->type = MLX5_FLOW_DESTINATION_TYPE_PORT;
3714 dst->type = MLX5_FLOW_DESTINATION_TYPE_TIR;
3715 if (mqp->flags & MLX5_IB_QP_RSS)
3716 dst->tir_num = mqp->rss_qp.tirn;
3718 dst->tir_num = mqp->raw_packet_qp.rq.tirn;
3721 if (flow_attr->type == IB_FLOW_ATTR_NORMAL) {
3722 if (flow_attr->flags & IB_FLOW_ATTR_FLAGS_DONT_TRAP) {
3723 handler = create_dont_trap_rule(dev, ft_prio,
3726 underlay_qpn = (mqp->flags & MLX5_IB_QP_UNDERLAY) ?
3727 mqp->underlay_qpn : 0;
3728 handler = _create_flow_rule(dev, ft_prio, flow_attr,
3729 dst, underlay_qpn, ucmd);
3731 } else if (flow_attr->type == IB_FLOW_ATTR_ALL_DEFAULT ||
3732 flow_attr->type == IB_FLOW_ATTR_MC_DEFAULT) {
3733 handler = create_leftovers_rule(dev, ft_prio, flow_attr,
3735 } else if (flow_attr->type == IB_FLOW_ATTR_SNIFFER) {
3736 handler = create_sniffer_rule(dev, ft_prio, ft_prio_tx, dst);
3742 if (IS_ERR(handler)) {
3743 err = PTR_ERR(handler);
3748 mutex_unlock(&dev->flow_db->lock);
3752 return &handler->ibflow;
3755 put_flow_table(dev, ft_prio, false);
3757 put_flow_table(dev, ft_prio_tx, false);
3759 mutex_unlock(&dev->flow_db->lock);
3763 return ERR_PTR(err);
3766 static struct mlx5_ib_flow_prio *
3767 _get_flow_table(struct mlx5_ib_dev *dev,
3768 struct mlx5_ib_flow_matcher *fs_matcher,
3771 struct mlx5_flow_namespace *ns = NULL;
3772 struct mlx5_ib_flow_prio *prio;
3777 if (fs_matcher->ns_type == MLX5_FLOW_NAMESPACE_BYPASS) {
3778 max_table_size = BIT(MLX5_CAP_FLOWTABLE_NIC_RX(dev->mdev,
3780 if (MLX5_CAP_FLOWTABLE_NIC_RX(dev->mdev, decap))
3781 flags |= MLX5_FLOW_TABLE_TUNNEL_EN_DECAP;
3782 if (MLX5_CAP_FLOWTABLE_NIC_RX(dev->mdev,
3783 reformat_l3_tunnel_to_l2))
3784 flags |= MLX5_FLOW_TABLE_TUNNEL_EN_REFORMAT;
3785 } else { /* Can only be MLX5_FLOW_NAMESPACE_EGRESS */
3786 max_table_size = BIT(MLX5_CAP_FLOWTABLE_NIC_TX(dev->mdev,
3788 if (MLX5_CAP_FLOWTABLE_NIC_TX(dev->mdev, reformat))
3789 flags |= MLX5_FLOW_TABLE_TUNNEL_EN_REFORMAT;
3792 if (max_table_size < MLX5_FS_MAX_ENTRIES)
3793 return ERR_PTR(-ENOMEM);
3796 priority = MLX5_IB_FLOW_MCAST_PRIO;
3798 priority = ib_prio_to_core_prio(fs_matcher->priority, false);
3800 ns = mlx5_get_flow_namespace(dev->mdev, fs_matcher->ns_type);
3802 return ERR_PTR(-ENOTSUPP);
3804 if (fs_matcher->ns_type == MLX5_FLOW_NAMESPACE_BYPASS)
3805 prio = &dev->flow_db->prios[priority];
3807 prio = &dev->flow_db->egress_prios[priority];
3809 if (prio->flow_table)
3812 return _get_prio(ns, prio, priority, MLX5_FS_MAX_ENTRIES,
3813 MLX5_FS_MAX_TYPES, flags);
3816 static struct mlx5_ib_flow_handler *
3817 _create_raw_flow_rule(struct mlx5_ib_dev *dev,
3818 struct mlx5_ib_flow_prio *ft_prio,
3819 struct mlx5_flow_destination *dst,
3820 struct mlx5_ib_flow_matcher *fs_matcher,
3821 struct mlx5_flow_act *flow_act,
3822 void *cmd_in, int inlen,
3825 struct mlx5_ib_flow_handler *handler;
3826 struct mlx5_flow_spec *spec;
3827 struct mlx5_flow_table *ft = ft_prio->flow_table;
3830 spec = kvzalloc(sizeof(*spec), GFP_KERNEL);
3831 handler = kzalloc(sizeof(*handler), GFP_KERNEL);
3832 if (!handler || !spec) {
3837 INIT_LIST_HEAD(&handler->list);
3839 memcpy(spec->match_value, cmd_in, inlen);
3840 memcpy(spec->match_criteria, fs_matcher->matcher_mask.match_params,
3841 fs_matcher->mask_len);
3842 spec->match_criteria_enable = fs_matcher->match_criteria_enable;
3844 handler->rule = mlx5_add_flow_rules(ft, spec,
3845 flow_act, dst, dst_num);
3847 if (IS_ERR(handler->rule)) {
3848 err = PTR_ERR(handler->rule);
3852 ft_prio->refcount++;
3853 handler->prio = ft_prio;
3855 ft_prio->flow_table = ft;
3861 return err ? ERR_PTR(err) : handler;
3864 static bool raw_fs_is_multicast(struct mlx5_ib_flow_matcher *fs_matcher,
3868 void *match_v_set_lyr_2_4, *match_c_set_lyr_2_4;
3869 void *dmac, *dmac_mask;
3870 void *ipv4, *ipv4_mask;
3872 if (!(fs_matcher->match_criteria_enable &
3873 (1 << MATCH_CRITERIA_ENABLE_OUTER_BIT)))
3876 match_c = fs_matcher->matcher_mask.match_params;
3877 match_v_set_lyr_2_4 = MLX5_ADDR_OF(fte_match_param, match_v,
3879 match_c_set_lyr_2_4 = MLX5_ADDR_OF(fte_match_param, match_c,
3882 dmac = MLX5_ADDR_OF(fte_match_set_lyr_2_4, match_v_set_lyr_2_4,
3884 dmac_mask = MLX5_ADDR_OF(fte_match_set_lyr_2_4, match_c_set_lyr_2_4,
3887 if (is_multicast_ether_addr(dmac) &&
3888 is_multicast_ether_addr(dmac_mask))
3891 ipv4 = MLX5_ADDR_OF(fte_match_set_lyr_2_4, match_v_set_lyr_2_4,
3892 dst_ipv4_dst_ipv6.ipv4_layout.ipv4);
3894 ipv4_mask = MLX5_ADDR_OF(fte_match_set_lyr_2_4, match_c_set_lyr_2_4,
3895 dst_ipv4_dst_ipv6.ipv4_layout.ipv4);
3897 if (ipv4_is_multicast(*(__be32 *)(ipv4)) &&
3898 ipv4_is_multicast(*(__be32 *)(ipv4_mask)))
3904 struct mlx5_ib_flow_handler *
3905 mlx5_ib_raw_fs_rule_add(struct mlx5_ib_dev *dev,
3906 struct mlx5_ib_flow_matcher *fs_matcher,
3907 struct mlx5_flow_act *flow_act,
3909 void *cmd_in, int inlen, int dest_id,
3912 struct mlx5_flow_destination *dst;
3913 struct mlx5_ib_flow_prio *ft_prio;
3914 struct mlx5_ib_flow_handler *handler;
3919 if (fs_matcher->flow_type != MLX5_IB_FLOW_TYPE_NORMAL)
3920 return ERR_PTR(-EOPNOTSUPP);
3922 if (fs_matcher->priority > MLX5_IB_FLOW_LAST_PRIO)
3923 return ERR_PTR(-ENOMEM);
3925 dst = kcalloc(2, sizeof(*dst), GFP_KERNEL);
3927 return ERR_PTR(-ENOMEM);
3929 mcast = raw_fs_is_multicast(fs_matcher, cmd_in);
3930 mutex_lock(&dev->flow_db->lock);
3932 ft_prio = _get_flow_table(dev, fs_matcher, mcast);
3933 if (IS_ERR(ft_prio)) {
3934 err = PTR_ERR(ft_prio);
3938 if (dest_type == MLX5_FLOW_DESTINATION_TYPE_TIR) {
3939 dst[dst_num].type = dest_type;
3940 dst[dst_num].tir_num = dest_id;
3941 flow_act->action |= MLX5_FLOW_CONTEXT_ACTION_FWD_DEST;
3942 } else if (dest_type == MLX5_FLOW_DESTINATION_TYPE_FLOW_TABLE) {
3943 dst[dst_num].type = MLX5_FLOW_DESTINATION_TYPE_FLOW_TABLE_NUM;
3944 dst[dst_num].ft_num = dest_id;
3945 flow_act->action |= MLX5_FLOW_CONTEXT_ACTION_FWD_DEST;
3947 dst[dst_num].type = MLX5_FLOW_DESTINATION_TYPE_PORT;
3948 flow_act->action |= MLX5_FLOW_CONTEXT_ACTION_ALLOW;
3953 if (flow_act->action & MLX5_FLOW_CONTEXT_ACTION_COUNT) {
3954 dst[dst_num].type = MLX5_FLOW_DESTINATION_TYPE_COUNTER;
3955 dst[dst_num].counter_id = counter_id;
3959 handler = _create_raw_flow_rule(dev, ft_prio, dst, fs_matcher, flow_act,
3960 cmd_in, inlen, dst_num);
3962 if (IS_ERR(handler)) {
3963 err = PTR_ERR(handler);
3967 mutex_unlock(&dev->flow_db->lock);
3968 atomic_inc(&fs_matcher->usecnt);
3969 handler->flow_matcher = fs_matcher;
3976 put_flow_table(dev, ft_prio, false);
3978 mutex_unlock(&dev->flow_db->lock);
3981 return ERR_PTR(err);
3984 static u32 mlx5_ib_flow_action_flags_to_accel_xfrm_flags(u32 mlx5_flags)
3988 if (mlx5_flags & MLX5_IB_UAPI_FLOW_ACTION_FLAGS_REQUIRE_METADATA)
3989 flags |= MLX5_ACCEL_XFRM_FLAG_REQUIRE_METADATA;
3994 #define MLX5_FLOW_ACTION_ESP_CREATE_LAST_SUPPORTED MLX5_IB_UAPI_FLOW_ACTION_FLAGS_REQUIRE_METADATA
3995 static struct ib_flow_action *
3996 mlx5_ib_create_flow_action_esp(struct ib_device *device,
3997 const struct ib_flow_action_attrs_esp *attr,
3998 struct uverbs_attr_bundle *attrs)
4000 struct mlx5_ib_dev *mdev = to_mdev(device);
4001 struct ib_uverbs_flow_action_esp_keymat_aes_gcm *aes_gcm;
4002 struct mlx5_accel_esp_xfrm_attrs accel_attrs = {};
4003 struct mlx5_ib_flow_action *action;
4008 err = uverbs_get_flags64(
4009 &action_flags, attrs, MLX5_IB_ATTR_CREATE_FLOW_ACTION_FLAGS,
4010 ((MLX5_FLOW_ACTION_ESP_CREATE_LAST_SUPPORTED << 1) - 1));
4012 return ERR_PTR(err);
4014 flags = mlx5_ib_flow_action_flags_to_accel_xfrm_flags(action_flags);
4016 /* We current only support a subset of the standard features. Only a
4017 * keymat of type AES_GCM, with icv_len == 16, iv_algo == SEQ and esn
4018 * (with overlap). Full offload mode isn't supported.
4020 if (!attr->keymat || attr->replay || attr->encap ||
4021 attr->spi || attr->seq || attr->tfc_pad ||
4022 attr->hard_limit_pkts ||
4023 (attr->flags & ~(IB_FLOW_ACTION_ESP_FLAGS_ESN_TRIGGERED |
4024 IB_UVERBS_FLOW_ACTION_ESP_FLAGS_ENCRYPT)))
4025 return ERR_PTR(-EOPNOTSUPP);
4027 if (attr->keymat->protocol !=
4028 IB_UVERBS_FLOW_ACTION_ESP_KEYMAT_AES_GCM)
4029 return ERR_PTR(-EOPNOTSUPP);
4031 aes_gcm = &attr->keymat->keymat.aes_gcm;
4033 if (aes_gcm->icv_len != 16 ||
4034 aes_gcm->iv_algo != IB_UVERBS_FLOW_ACTION_IV_ALGO_SEQ)
4035 return ERR_PTR(-EOPNOTSUPP);
4037 action = kmalloc(sizeof(*action), GFP_KERNEL);
4039 return ERR_PTR(-ENOMEM);
4041 action->esp_aes_gcm.ib_flags = attr->flags;
4042 memcpy(&accel_attrs.keymat.aes_gcm.aes_key, &aes_gcm->aes_key,
4043 sizeof(accel_attrs.keymat.aes_gcm.aes_key));
4044 accel_attrs.keymat.aes_gcm.key_len = aes_gcm->key_len * 8;
4045 memcpy(&accel_attrs.keymat.aes_gcm.salt, &aes_gcm->salt,
4046 sizeof(accel_attrs.keymat.aes_gcm.salt));
4047 memcpy(&accel_attrs.keymat.aes_gcm.seq_iv, &aes_gcm->iv,
4048 sizeof(accel_attrs.keymat.aes_gcm.seq_iv));
4049 accel_attrs.keymat.aes_gcm.icv_len = aes_gcm->icv_len * 8;
4050 accel_attrs.keymat.aes_gcm.iv_algo = MLX5_ACCEL_ESP_AES_GCM_IV_ALGO_SEQ;
4051 accel_attrs.keymat_type = MLX5_ACCEL_ESP_KEYMAT_AES_GCM;
4053 accel_attrs.esn = attr->esn;
4054 if (attr->flags & IB_FLOW_ACTION_ESP_FLAGS_ESN_TRIGGERED)
4055 accel_attrs.flags |= MLX5_ACCEL_ESP_FLAGS_ESN_TRIGGERED;
4056 if (attr->flags & IB_UVERBS_FLOW_ACTION_ESP_FLAGS_ESN_NEW_WINDOW)
4057 accel_attrs.flags |= MLX5_ACCEL_ESP_FLAGS_ESN_STATE_OVERLAP;
4059 if (attr->flags & IB_UVERBS_FLOW_ACTION_ESP_FLAGS_ENCRYPT)
4060 accel_attrs.action |= MLX5_ACCEL_ESP_ACTION_ENCRYPT;
4062 action->esp_aes_gcm.ctx =
4063 mlx5_accel_esp_create_xfrm(mdev->mdev, &accel_attrs, flags);
4064 if (IS_ERR(action->esp_aes_gcm.ctx)) {
4065 err = PTR_ERR(action->esp_aes_gcm.ctx);
4069 action->esp_aes_gcm.ib_flags = attr->flags;
4071 return &action->ib_action;
4075 return ERR_PTR(err);
4079 mlx5_ib_modify_flow_action_esp(struct ib_flow_action *action,
4080 const struct ib_flow_action_attrs_esp *attr,
4081 struct uverbs_attr_bundle *attrs)
4083 struct mlx5_ib_flow_action *maction = to_mflow_act(action);
4084 struct mlx5_accel_esp_xfrm_attrs accel_attrs;
4087 if (attr->keymat || attr->replay || attr->encap ||
4088 attr->spi || attr->seq || attr->tfc_pad ||
4089 attr->hard_limit_pkts ||
4090 (attr->flags & ~(IB_FLOW_ACTION_ESP_FLAGS_ESN_TRIGGERED |
4091 IB_FLOW_ACTION_ESP_FLAGS_MOD_ESP_ATTRS |
4092 IB_UVERBS_FLOW_ACTION_ESP_FLAGS_ESN_NEW_WINDOW)))
4095 /* Only the ESN value or the MLX5_ACCEL_ESP_FLAGS_ESN_STATE_OVERLAP can
4098 if (!(maction->esp_aes_gcm.ib_flags &
4099 IB_FLOW_ACTION_ESP_FLAGS_ESN_TRIGGERED) &&
4100 attr->flags & (IB_FLOW_ACTION_ESP_FLAGS_ESN_TRIGGERED |
4101 IB_UVERBS_FLOW_ACTION_ESP_FLAGS_ESN_NEW_WINDOW))
4104 memcpy(&accel_attrs, &maction->esp_aes_gcm.ctx->attrs,
4105 sizeof(accel_attrs));
4107 accel_attrs.esn = attr->esn;
4108 if (attr->flags & IB_UVERBS_FLOW_ACTION_ESP_FLAGS_ESN_NEW_WINDOW)
4109 accel_attrs.flags |= MLX5_ACCEL_ESP_FLAGS_ESN_STATE_OVERLAP;
4111 accel_attrs.flags &= ~MLX5_ACCEL_ESP_FLAGS_ESN_STATE_OVERLAP;
4113 err = mlx5_accel_esp_modify_xfrm(maction->esp_aes_gcm.ctx,
4118 maction->esp_aes_gcm.ib_flags &=
4119 ~IB_UVERBS_FLOW_ACTION_ESP_FLAGS_ESN_NEW_WINDOW;
4120 maction->esp_aes_gcm.ib_flags |=
4121 attr->flags & IB_UVERBS_FLOW_ACTION_ESP_FLAGS_ESN_NEW_WINDOW;
4126 static int mlx5_ib_destroy_flow_action(struct ib_flow_action *action)
4128 struct mlx5_ib_flow_action *maction = to_mflow_act(action);
4130 switch (action->type) {
4131 case IB_FLOW_ACTION_ESP:
4133 * We only support aes_gcm by now, so we implicitly know this is
4134 * the underline crypto.
4136 mlx5_accel_esp_destroy_xfrm(maction->esp_aes_gcm.ctx);
4138 case IB_FLOW_ACTION_UNSPECIFIED:
4139 mlx5_ib_destroy_flow_action_raw(maction);
4150 static int mlx5_ib_mcg_attach(struct ib_qp *ibqp, union ib_gid *gid, u16 lid)
4152 struct mlx5_ib_dev *dev = to_mdev(ibqp->device);
4153 struct mlx5_ib_qp *mqp = to_mqp(ibqp);
4158 to_mpd(ibqp->pd)->uid : 0;
4160 if (mqp->flags & MLX5_IB_QP_UNDERLAY) {
4161 mlx5_ib_dbg(dev, "Attaching a multi cast group to underlay QP is not supported\n");
4165 err = mlx5_cmd_attach_mcg(dev->mdev, gid, ibqp->qp_num, uid);
4167 mlx5_ib_warn(dev, "failed attaching QPN 0x%x, MGID %pI6\n",
4168 ibqp->qp_num, gid->raw);
4173 static int mlx5_ib_mcg_detach(struct ib_qp *ibqp, union ib_gid *gid, u16 lid)
4175 struct mlx5_ib_dev *dev = to_mdev(ibqp->device);
4180 to_mpd(ibqp->pd)->uid : 0;
4181 err = mlx5_cmd_detach_mcg(dev->mdev, gid, ibqp->qp_num, uid);
4183 mlx5_ib_warn(dev, "failed detaching QPN 0x%x, MGID %pI6\n",
4184 ibqp->qp_num, gid->raw);
4189 static int init_node_data(struct mlx5_ib_dev *dev)
4193 err = mlx5_query_node_desc(dev, dev->ib_dev.node_desc);
4197 dev->mdev->rev_id = dev->mdev->pdev->revision;
4199 return mlx5_query_node_guid(dev, &dev->ib_dev.node_guid);
4202 static ssize_t fw_pages_show(struct device *device,
4203 struct device_attribute *attr, char *buf)
4205 struct mlx5_ib_dev *dev =
4206 rdma_device_to_drv_device(device, struct mlx5_ib_dev, ib_dev);
4208 return sprintf(buf, "%d\n", dev->mdev->priv.fw_pages);
4210 static DEVICE_ATTR_RO(fw_pages);
4212 static ssize_t reg_pages_show(struct device *device,
4213 struct device_attribute *attr, char *buf)
4215 struct mlx5_ib_dev *dev =
4216 rdma_device_to_drv_device(device, struct mlx5_ib_dev, ib_dev);
4218 return sprintf(buf, "%d\n", atomic_read(&dev->mdev->priv.reg_pages));
4220 static DEVICE_ATTR_RO(reg_pages);
4222 static ssize_t hca_type_show(struct device *device,
4223 struct device_attribute *attr, char *buf)
4225 struct mlx5_ib_dev *dev =
4226 rdma_device_to_drv_device(device, struct mlx5_ib_dev, ib_dev);
4228 return sprintf(buf, "MT%d\n", dev->mdev->pdev->device);
4230 static DEVICE_ATTR_RO(hca_type);
4232 static ssize_t hw_rev_show(struct device *device,
4233 struct device_attribute *attr, char *buf)
4235 struct mlx5_ib_dev *dev =
4236 rdma_device_to_drv_device(device, struct mlx5_ib_dev, ib_dev);
4238 return sprintf(buf, "%x\n", dev->mdev->rev_id);
4240 static DEVICE_ATTR_RO(hw_rev);
4242 static ssize_t board_id_show(struct device *device,
4243 struct device_attribute *attr, char *buf)
4245 struct mlx5_ib_dev *dev =
4246 rdma_device_to_drv_device(device, struct mlx5_ib_dev, ib_dev);
4248 return sprintf(buf, "%.*s\n", MLX5_BOARD_ID_LEN,
4249 dev->mdev->board_id);
4251 static DEVICE_ATTR_RO(board_id);
4253 static struct attribute *mlx5_class_attributes[] = {
4254 &dev_attr_hw_rev.attr,
4255 &dev_attr_hca_type.attr,
4256 &dev_attr_board_id.attr,
4257 &dev_attr_fw_pages.attr,
4258 &dev_attr_reg_pages.attr,
4262 static const struct attribute_group mlx5_attr_group = {
4263 .attrs = mlx5_class_attributes,
4266 static void pkey_change_handler(struct work_struct *work)
4268 struct mlx5_ib_port_resources *ports =
4269 container_of(work, struct mlx5_ib_port_resources,
4272 mutex_lock(&ports->devr->mutex);
4273 mlx5_ib_gsi_pkey_change(ports->gsi);
4274 mutex_unlock(&ports->devr->mutex);
4277 static void mlx5_ib_handle_internal_error(struct mlx5_ib_dev *ibdev)
4279 struct mlx5_ib_qp *mqp;
4280 struct mlx5_ib_cq *send_mcq, *recv_mcq;
4281 struct mlx5_core_cq *mcq;
4282 struct list_head cq_armed_list;
4283 unsigned long flags_qp;
4284 unsigned long flags_cq;
4285 unsigned long flags;
4287 INIT_LIST_HEAD(&cq_armed_list);
4289 /* Go over qp list reside on that ibdev, sync with create/destroy qp.*/
4290 spin_lock_irqsave(&ibdev->reset_flow_resource_lock, flags);
4291 list_for_each_entry(mqp, &ibdev->qp_list, qps_list) {
4292 spin_lock_irqsave(&mqp->sq.lock, flags_qp);
4293 if (mqp->sq.tail != mqp->sq.head) {
4294 send_mcq = to_mcq(mqp->ibqp.send_cq);
4295 spin_lock_irqsave(&send_mcq->lock, flags_cq);
4296 if (send_mcq->mcq.comp &&
4297 mqp->ibqp.send_cq->comp_handler) {
4298 if (!send_mcq->mcq.reset_notify_added) {
4299 send_mcq->mcq.reset_notify_added = 1;
4300 list_add_tail(&send_mcq->mcq.reset_notify,
4304 spin_unlock_irqrestore(&send_mcq->lock, flags_cq);
4306 spin_unlock_irqrestore(&mqp->sq.lock, flags_qp);
4307 spin_lock_irqsave(&mqp->rq.lock, flags_qp);
4308 /* no handling is needed for SRQ */
4309 if (!mqp->ibqp.srq) {
4310 if (mqp->rq.tail != mqp->rq.head) {
4311 recv_mcq = to_mcq(mqp->ibqp.recv_cq);
4312 spin_lock_irqsave(&recv_mcq->lock, flags_cq);
4313 if (recv_mcq->mcq.comp &&
4314 mqp->ibqp.recv_cq->comp_handler) {
4315 if (!recv_mcq->mcq.reset_notify_added) {
4316 recv_mcq->mcq.reset_notify_added = 1;
4317 list_add_tail(&recv_mcq->mcq.reset_notify,
4321 spin_unlock_irqrestore(&recv_mcq->lock,
4325 spin_unlock_irqrestore(&mqp->rq.lock, flags_qp);
4327 /*At that point all inflight post send were put to be executed as of we
4328 * lock/unlock above locks Now need to arm all involved CQs.
4330 list_for_each_entry(mcq, &cq_armed_list, reset_notify) {
4333 spin_unlock_irqrestore(&ibdev->reset_flow_resource_lock, flags);
4336 static void delay_drop_handler(struct work_struct *work)
4339 struct mlx5_ib_delay_drop *delay_drop =
4340 container_of(work, struct mlx5_ib_delay_drop,
4343 atomic_inc(&delay_drop->events_cnt);
4345 mutex_lock(&delay_drop->lock);
4346 err = mlx5_core_set_delay_drop(delay_drop->dev->mdev,
4347 delay_drop->timeout);
4349 mlx5_ib_warn(delay_drop->dev, "Failed to set delay drop, timeout=%u\n",
4350 delay_drop->timeout);
4351 delay_drop->activate = false;
4353 mutex_unlock(&delay_drop->lock);
4356 static void handle_general_event(struct mlx5_ib_dev *ibdev, struct mlx5_eqe *eqe,
4357 struct ib_event *ibev)
4359 u8 port = (eqe->data.port.port >> 4) & 0xf;
4361 switch (eqe->sub_type) {
4362 case MLX5_GENERAL_SUBTYPE_DELAY_DROP_TIMEOUT:
4363 if (mlx5_ib_port_link_layer(&ibdev->ib_dev, port) ==
4364 IB_LINK_LAYER_ETHERNET)
4365 schedule_work(&ibdev->delay_drop.delay_drop_work);
4367 default: /* do nothing */
4372 static int handle_port_change(struct mlx5_ib_dev *ibdev, struct mlx5_eqe *eqe,
4373 struct ib_event *ibev)
4375 u8 port = (eqe->data.port.port >> 4) & 0xf;
4377 ibev->element.port_num = port;
4379 switch (eqe->sub_type) {
4380 case MLX5_PORT_CHANGE_SUBTYPE_ACTIVE:
4381 case MLX5_PORT_CHANGE_SUBTYPE_DOWN:
4382 case MLX5_PORT_CHANGE_SUBTYPE_INITIALIZED:
4383 /* In RoCE, port up/down events are handled in
4384 * mlx5_netdev_event().
4386 if (mlx5_ib_port_link_layer(&ibdev->ib_dev, port) ==
4387 IB_LINK_LAYER_ETHERNET)
4390 ibev->event = (eqe->sub_type == MLX5_PORT_CHANGE_SUBTYPE_ACTIVE) ?
4391 IB_EVENT_PORT_ACTIVE : IB_EVENT_PORT_ERR;
4394 case MLX5_PORT_CHANGE_SUBTYPE_LID:
4395 ibev->event = IB_EVENT_LID_CHANGE;
4398 case MLX5_PORT_CHANGE_SUBTYPE_PKEY:
4399 ibev->event = IB_EVENT_PKEY_CHANGE;
4400 schedule_work(&ibdev->devr.ports[port - 1].pkey_change_work);
4403 case MLX5_PORT_CHANGE_SUBTYPE_GUID:
4404 ibev->event = IB_EVENT_GID_CHANGE;
4407 case MLX5_PORT_CHANGE_SUBTYPE_CLIENT_REREG:
4408 ibev->event = IB_EVENT_CLIENT_REREGISTER;
4417 static void mlx5_ib_handle_event(struct work_struct *_work)
4419 struct mlx5_ib_event_work *work =
4420 container_of(_work, struct mlx5_ib_event_work, work);
4421 struct mlx5_ib_dev *ibdev;
4422 struct ib_event ibev;
4425 if (work->is_slave) {
4426 ibdev = mlx5_ib_get_ibdev_from_mpi(work->mpi);
4433 switch (work->event) {
4434 case MLX5_DEV_EVENT_SYS_ERROR:
4435 ibev.event = IB_EVENT_DEVICE_FATAL;
4436 mlx5_ib_handle_internal_error(ibdev);
4437 ibev.element.port_num = (u8)(unsigned long)work->param;
4440 case MLX5_EVENT_TYPE_PORT_CHANGE:
4441 if (handle_port_change(ibdev, work->param, &ibev))
4444 case MLX5_EVENT_TYPE_GENERAL_EVENT:
4445 handle_general_event(ibdev, work->param, &ibev);
4451 ibev.device = &ibdev->ib_dev;
4453 if (!rdma_is_port_valid(&ibdev->ib_dev, ibev.element.port_num)) {
4454 mlx5_ib_warn(ibdev, "warning: event on port %d\n", ibev.element.port_num);
4458 if (ibdev->ib_active)
4459 ib_dispatch_event(&ibev);
4462 ibdev->ib_active = false;
4467 static int mlx5_ib_event(struct notifier_block *nb,
4468 unsigned long event, void *param)
4470 struct mlx5_ib_event_work *work;
4472 work = kmalloc(sizeof(*work), GFP_ATOMIC);
4476 INIT_WORK(&work->work, mlx5_ib_handle_event);
4477 work->dev = container_of(nb, struct mlx5_ib_dev, mdev_events);
4478 work->is_slave = false;
4479 work->param = param;
4480 work->event = event;
4482 queue_work(mlx5_ib_event_wq, &work->work);
4487 static int mlx5_ib_event_slave_port(struct notifier_block *nb,
4488 unsigned long event, void *param)
4490 struct mlx5_ib_event_work *work;
4492 work = kmalloc(sizeof(*work), GFP_ATOMIC);
4496 INIT_WORK(&work->work, mlx5_ib_handle_event);
4497 work->mpi = container_of(nb, struct mlx5_ib_multiport_info, mdev_events);
4498 work->is_slave = true;
4499 work->param = param;
4500 work->event = event;
4501 queue_work(mlx5_ib_event_wq, &work->work);
4506 static int set_has_smi_cap(struct mlx5_ib_dev *dev)
4508 struct mlx5_hca_vport_context vport_ctx;
4512 for (port = 1; port <= dev->num_ports; port++) {
4513 dev->mdev->port_caps[port - 1].has_smi = false;
4514 if (MLX5_CAP_GEN(dev->mdev, port_type) ==
4515 MLX5_CAP_PORT_TYPE_IB) {
4516 if (MLX5_CAP_GEN(dev->mdev, ib_virt)) {
4517 err = mlx5_query_hca_vport_context(dev->mdev, 0,
4521 mlx5_ib_err(dev, "query_hca_vport_context for port=%d failed %d\n",
4525 dev->mdev->port_caps[port - 1].has_smi =
4528 dev->mdev->port_caps[port - 1].has_smi = true;
4535 static void get_ext_port_caps(struct mlx5_ib_dev *dev)
4539 for (port = 1; port <= dev->num_ports; port++)
4540 mlx5_query_ext_port_caps(dev, port);
4543 static int get_port_caps(struct mlx5_ib_dev *dev, u8 port)
4545 struct ib_device_attr *dprops = NULL;
4546 struct ib_port_attr *pprops = NULL;
4548 struct ib_udata uhw = {.inlen = 0, .outlen = 0};
4550 pprops = kmalloc(sizeof(*pprops), GFP_KERNEL);
4554 dprops = kmalloc(sizeof(*dprops), GFP_KERNEL);
4558 err = set_has_smi_cap(dev);
4562 err = mlx5_ib_query_device(&dev->ib_dev, dprops, &uhw);
4564 mlx5_ib_warn(dev, "query_device failed %d\n", err);
4568 memset(pprops, 0, sizeof(*pprops));
4569 err = mlx5_ib_query_port(&dev->ib_dev, port, pprops);
4571 mlx5_ib_warn(dev, "query_port %d failed %d\n",
4576 dev->mdev->port_caps[port - 1].pkey_table_len =
4578 dev->mdev->port_caps[port - 1].gid_table_len =
4579 pprops->gid_tbl_len;
4580 mlx5_ib_dbg(dev, "port %d: pkey_table_len %d, gid_table_len %d\n",
4581 port, dprops->max_pkeys, pprops->gid_tbl_len);
4590 static void destroy_umrc_res(struct mlx5_ib_dev *dev)
4594 err = mlx5_mr_cache_cleanup(dev);
4596 mlx5_ib_warn(dev, "mr cache cleanup failed\n");
4599 mlx5_ib_destroy_qp(dev->umrc.qp);
4601 ib_free_cq(dev->umrc.cq);
4603 ib_dealloc_pd(dev->umrc.pd);
4610 static int create_umr_res(struct mlx5_ib_dev *dev)
4612 struct ib_qp_init_attr *init_attr = NULL;
4613 struct ib_qp_attr *attr = NULL;
4619 attr = kzalloc(sizeof(*attr), GFP_KERNEL);
4620 init_attr = kzalloc(sizeof(*init_attr), GFP_KERNEL);
4621 if (!attr || !init_attr) {
4626 pd = ib_alloc_pd(&dev->ib_dev, 0);
4628 mlx5_ib_dbg(dev, "Couldn't create PD for sync UMR QP\n");
4633 cq = ib_alloc_cq(&dev->ib_dev, NULL, 128, 0, IB_POLL_SOFTIRQ);
4635 mlx5_ib_dbg(dev, "Couldn't create CQ for sync UMR QP\n");
4640 init_attr->send_cq = cq;
4641 init_attr->recv_cq = cq;
4642 init_attr->sq_sig_type = IB_SIGNAL_ALL_WR;
4643 init_attr->cap.max_send_wr = MAX_UMR_WR;
4644 init_attr->cap.max_send_sge = 1;
4645 init_attr->qp_type = MLX5_IB_QPT_REG_UMR;
4646 init_attr->port_num = 1;
4647 qp = mlx5_ib_create_qp(pd, init_attr, NULL);
4649 mlx5_ib_dbg(dev, "Couldn't create sync UMR QP\n");
4653 qp->device = &dev->ib_dev;
4656 qp->qp_type = MLX5_IB_QPT_REG_UMR;
4657 qp->send_cq = init_attr->send_cq;
4658 qp->recv_cq = init_attr->recv_cq;
4660 attr->qp_state = IB_QPS_INIT;
4662 ret = mlx5_ib_modify_qp(qp, attr, IB_QP_STATE | IB_QP_PKEY_INDEX |
4665 mlx5_ib_dbg(dev, "Couldn't modify UMR QP\n");
4669 memset(attr, 0, sizeof(*attr));
4670 attr->qp_state = IB_QPS_RTR;
4671 attr->path_mtu = IB_MTU_256;
4673 ret = mlx5_ib_modify_qp(qp, attr, IB_QP_STATE, NULL);
4675 mlx5_ib_dbg(dev, "Couldn't modify umr QP to rtr\n");
4679 memset(attr, 0, sizeof(*attr));
4680 attr->qp_state = IB_QPS_RTS;
4681 ret = mlx5_ib_modify_qp(qp, attr, IB_QP_STATE, NULL);
4683 mlx5_ib_dbg(dev, "Couldn't modify umr QP to rts\n");
4691 sema_init(&dev->umrc.sem, MAX_UMR_WR);
4692 ret = mlx5_mr_cache_init(dev);
4694 mlx5_ib_warn(dev, "mr cache init failed %d\n", ret);
4704 mlx5_ib_destroy_qp(qp);
4705 dev->umrc.qp = NULL;
4709 dev->umrc.cq = NULL;
4713 dev->umrc.pd = NULL;
4721 static u8 mlx5_get_umr_fence(u8 umr_fence_cap)
4723 switch (umr_fence_cap) {
4724 case MLX5_CAP_UMR_FENCE_NONE:
4725 return MLX5_FENCE_MODE_NONE;
4726 case MLX5_CAP_UMR_FENCE_SMALL:
4727 return MLX5_FENCE_MODE_INITIATOR_SMALL;
4729 return MLX5_FENCE_MODE_STRONG_ORDERING;
4733 static int create_dev_resources(struct mlx5_ib_resources *devr)
4735 struct ib_srq_init_attr attr;
4736 struct mlx5_ib_dev *dev;
4737 struct ib_device *ibdev;
4738 struct ib_cq_init_attr cq_attr = {.cqe = 1};
4742 dev = container_of(devr, struct mlx5_ib_dev, devr);
4743 ibdev = &dev->ib_dev;
4745 mutex_init(&devr->mutex);
4747 devr->p0 = rdma_zalloc_drv_obj(ibdev, ib_pd);
4751 devr->p0->device = ibdev;
4752 devr->p0->uobject = NULL;
4753 atomic_set(&devr->p0->usecnt, 0);
4755 ret = mlx5_ib_alloc_pd(devr->p0, NULL, NULL);
4759 devr->c0 = mlx5_ib_create_cq(&dev->ib_dev, &cq_attr, NULL, NULL);
4760 if (IS_ERR(devr->c0)) {
4761 ret = PTR_ERR(devr->c0);
4764 devr->c0->device = &dev->ib_dev;
4765 devr->c0->uobject = NULL;
4766 devr->c0->comp_handler = NULL;
4767 devr->c0->event_handler = NULL;
4768 devr->c0->cq_context = NULL;
4769 atomic_set(&devr->c0->usecnt, 0);
4771 devr->x0 = mlx5_ib_alloc_xrcd(&dev->ib_dev, NULL, NULL);
4772 if (IS_ERR(devr->x0)) {
4773 ret = PTR_ERR(devr->x0);
4776 devr->x0->device = &dev->ib_dev;
4777 devr->x0->inode = NULL;
4778 atomic_set(&devr->x0->usecnt, 0);
4779 mutex_init(&devr->x0->tgt_qp_mutex);
4780 INIT_LIST_HEAD(&devr->x0->tgt_qp_list);
4782 devr->x1 = mlx5_ib_alloc_xrcd(&dev->ib_dev, NULL, NULL);
4783 if (IS_ERR(devr->x1)) {
4784 ret = PTR_ERR(devr->x1);
4787 devr->x1->device = &dev->ib_dev;
4788 devr->x1->inode = NULL;
4789 atomic_set(&devr->x1->usecnt, 0);
4790 mutex_init(&devr->x1->tgt_qp_mutex);
4791 INIT_LIST_HEAD(&devr->x1->tgt_qp_list);
4793 memset(&attr, 0, sizeof(attr));
4794 attr.attr.max_sge = 1;
4795 attr.attr.max_wr = 1;
4796 attr.srq_type = IB_SRQT_XRC;
4797 attr.ext.cq = devr->c0;
4798 attr.ext.xrc.xrcd = devr->x0;
4800 devr->s0 = mlx5_ib_create_srq(devr->p0, &attr, NULL);
4801 if (IS_ERR(devr->s0)) {
4802 ret = PTR_ERR(devr->s0);
4805 devr->s0->device = &dev->ib_dev;
4806 devr->s0->pd = devr->p0;
4807 devr->s0->uobject = NULL;
4808 devr->s0->event_handler = NULL;
4809 devr->s0->srq_context = NULL;
4810 devr->s0->srq_type = IB_SRQT_XRC;
4811 devr->s0->ext.xrc.xrcd = devr->x0;
4812 devr->s0->ext.cq = devr->c0;
4813 atomic_inc(&devr->s0->ext.xrc.xrcd->usecnt);
4814 atomic_inc(&devr->s0->ext.cq->usecnt);
4815 atomic_inc(&devr->p0->usecnt);
4816 atomic_set(&devr->s0->usecnt, 0);
4818 memset(&attr, 0, sizeof(attr));
4819 attr.attr.max_sge = 1;
4820 attr.attr.max_wr = 1;
4821 attr.srq_type = IB_SRQT_BASIC;
4822 devr->s1 = mlx5_ib_create_srq(devr->p0, &attr, NULL);
4823 if (IS_ERR(devr->s1)) {
4824 ret = PTR_ERR(devr->s1);
4827 devr->s1->device = &dev->ib_dev;
4828 devr->s1->pd = devr->p0;
4829 devr->s1->uobject = NULL;
4830 devr->s1->event_handler = NULL;
4831 devr->s1->srq_context = NULL;
4832 devr->s1->srq_type = IB_SRQT_BASIC;
4833 devr->s1->ext.cq = devr->c0;
4834 atomic_inc(&devr->p0->usecnt);
4835 atomic_set(&devr->s1->usecnt, 0);
4837 for (port = 0; port < ARRAY_SIZE(devr->ports); ++port) {
4838 INIT_WORK(&devr->ports[port].pkey_change_work,
4839 pkey_change_handler);
4840 devr->ports[port].devr = devr;
4846 mlx5_ib_destroy_srq(devr->s0);
4848 mlx5_ib_dealloc_xrcd(devr->x1);
4850 mlx5_ib_dealloc_xrcd(devr->x0);
4852 mlx5_ib_destroy_cq(devr->c0);
4854 mlx5_ib_dealloc_pd(devr->p0);
4860 static void destroy_dev_resources(struct mlx5_ib_resources *devr)
4862 struct mlx5_ib_dev *dev =
4863 container_of(devr, struct mlx5_ib_dev, devr);
4866 mlx5_ib_destroy_srq(devr->s1);
4867 mlx5_ib_destroy_srq(devr->s0);
4868 mlx5_ib_dealloc_xrcd(devr->x0);
4869 mlx5_ib_dealloc_xrcd(devr->x1);
4870 mlx5_ib_destroy_cq(devr->c0);
4871 mlx5_ib_dealloc_pd(devr->p0);
4874 /* Make sure no change P_Key work items are still executing */
4875 for (port = 0; port < dev->num_ports; ++port)
4876 cancel_work_sync(&devr->ports[port].pkey_change_work);
4879 static u32 get_core_cap_flags(struct ib_device *ibdev,
4880 struct mlx5_hca_vport_context *rep)
4882 struct mlx5_ib_dev *dev = to_mdev(ibdev);
4883 enum rdma_link_layer ll = mlx5_ib_port_link_layer(ibdev, 1);
4884 u8 l3_type_cap = MLX5_CAP_ROCE(dev->mdev, l3_type);
4885 u8 roce_version_cap = MLX5_CAP_ROCE(dev->mdev, roce_version);
4886 bool raw_support = !mlx5_core_mp_enabled(dev->mdev);
4889 if (rep->grh_required)
4890 ret |= RDMA_CORE_CAP_IB_GRH_REQUIRED;
4892 if (ll == IB_LINK_LAYER_INFINIBAND)
4893 return ret | RDMA_CORE_PORT_IBA_IB;
4896 ret |= RDMA_CORE_PORT_RAW_PACKET;
4898 if (!(l3_type_cap & MLX5_ROCE_L3_TYPE_IPV4_CAP))
4901 if (!(l3_type_cap & MLX5_ROCE_L3_TYPE_IPV6_CAP))
4904 if (roce_version_cap & MLX5_ROCE_VERSION_1_CAP)
4905 ret |= RDMA_CORE_PORT_IBA_ROCE;
4907 if (roce_version_cap & MLX5_ROCE_VERSION_2_CAP)
4908 ret |= RDMA_CORE_PORT_IBA_ROCE_UDP_ENCAP;
4913 static int mlx5_port_immutable(struct ib_device *ibdev, u8 port_num,
4914 struct ib_port_immutable *immutable)
4916 struct ib_port_attr attr;
4917 struct mlx5_ib_dev *dev = to_mdev(ibdev);
4918 enum rdma_link_layer ll = mlx5_ib_port_link_layer(ibdev, port_num);
4919 struct mlx5_hca_vport_context rep = {0};
4922 err = ib_query_port(ibdev, port_num, &attr);
4926 if (ll == IB_LINK_LAYER_INFINIBAND) {
4927 err = mlx5_query_hca_vport_context(dev->mdev, 0, port_num, 0,
4933 immutable->pkey_tbl_len = attr.pkey_tbl_len;
4934 immutable->gid_tbl_len = attr.gid_tbl_len;
4935 immutable->core_cap_flags = get_core_cap_flags(ibdev, &rep);
4936 if ((ll == IB_LINK_LAYER_INFINIBAND) || MLX5_CAP_GEN(dev->mdev, roce))
4937 immutable->max_mad_size = IB_MGMT_MAD_SIZE;
4942 static int mlx5_port_rep_immutable(struct ib_device *ibdev, u8 port_num,
4943 struct ib_port_immutable *immutable)
4945 struct ib_port_attr attr;
4948 immutable->core_cap_flags = RDMA_CORE_PORT_RAW_PACKET;
4950 err = ib_query_port(ibdev, port_num, &attr);
4954 immutable->pkey_tbl_len = attr.pkey_tbl_len;
4955 immutable->gid_tbl_len = attr.gid_tbl_len;
4956 immutable->core_cap_flags = RDMA_CORE_PORT_RAW_PACKET;
4961 static void get_dev_fw_str(struct ib_device *ibdev, char *str)
4963 struct mlx5_ib_dev *dev =
4964 container_of(ibdev, struct mlx5_ib_dev, ib_dev);
4965 snprintf(str, IB_FW_VERSION_NAME_MAX, "%d.%d.%04d",
4966 fw_rev_maj(dev->mdev), fw_rev_min(dev->mdev),
4967 fw_rev_sub(dev->mdev));
4970 static int mlx5_eth_lag_init(struct mlx5_ib_dev *dev)
4972 struct mlx5_core_dev *mdev = dev->mdev;
4973 struct mlx5_flow_namespace *ns = mlx5_get_flow_namespace(mdev,
4974 MLX5_FLOW_NAMESPACE_LAG);
4975 struct mlx5_flow_table *ft;
4978 if (!ns || !mlx5_lag_is_roce(mdev))
4981 err = mlx5_cmd_create_vport_lag(mdev);
4985 ft = mlx5_create_lag_demux_flow_table(ns, 0, 0);
4988 goto err_destroy_vport_lag;
4991 dev->flow_db->lag_demux_ft = ft;
4992 dev->lag_active = true;
4995 err_destroy_vport_lag:
4996 mlx5_cmd_destroy_vport_lag(mdev);
5000 static void mlx5_eth_lag_cleanup(struct mlx5_ib_dev *dev)
5002 struct mlx5_core_dev *mdev = dev->mdev;
5004 if (dev->lag_active) {
5005 dev->lag_active = false;
5007 mlx5_destroy_flow_table(dev->flow_db->lag_demux_ft);
5008 dev->flow_db->lag_demux_ft = NULL;
5010 mlx5_cmd_destroy_vport_lag(mdev);
5014 static int mlx5_add_netdev_notifier(struct mlx5_ib_dev *dev, u8 port_num)
5018 dev->roce[port_num].nb.notifier_call = mlx5_netdev_event;
5019 err = register_netdevice_notifier(&dev->roce[port_num].nb);
5021 dev->roce[port_num].nb.notifier_call = NULL;
5028 static void mlx5_remove_netdev_notifier(struct mlx5_ib_dev *dev, u8 port_num)
5030 if (dev->roce[port_num].nb.notifier_call) {
5031 unregister_netdevice_notifier(&dev->roce[port_num].nb);
5032 dev->roce[port_num].nb.notifier_call = NULL;
5036 static int mlx5_enable_eth(struct mlx5_ib_dev *dev)
5040 if (MLX5_CAP_GEN(dev->mdev, roce)) {
5041 err = mlx5_nic_vport_enable_roce(dev->mdev);
5046 err = mlx5_eth_lag_init(dev);
5048 goto err_disable_roce;
5053 if (MLX5_CAP_GEN(dev->mdev, roce))
5054 mlx5_nic_vport_disable_roce(dev->mdev);
5059 static void mlx5_disable_eth(struct mlx5_ib_dev *dev)
5061 mlx5_eth_lag_cleanup(dev);
5062 if (MLX5_CAP_GEN(dev->mdev, roce))
5063 mlx5_nic_vport_disable_roce(dev->mdev);
5066 struct mlx5_ib_counter {
5071 #define INIT_Q_COUNTER(_name) \
5072 { .name = #_name, .offset = MLX5_BYTE_OFF(query_q_counter_out, _name)}
5074 static const struct mlx5_ib_counter basic_q_cnts[] = {
5075 INIT_Q_COUNTER(rx_write_requests),
5076 INIT_Q_COUNTER(rx_read_requests),
5077 INIT_Q_COUNTER(rx_atomic_requests),
5078 INIT_Q_COUNTER(out_of_buffer),
5081 static const struct mlx5_ib_counter out_of_seq_q_cnts[] = {
5082 INIT_Q_COUNTER(out_of_sequence),
5085 static const struct mlx5_ib_counter retrans_q_cnts[] = {
5086 INIT_Q_COUNTER(duplicate_request),
5087 INIT_Q_COUNTER(rnr_nak_retry_err),
5088 INIT_Q_COUNTER(packet_seq_err),
5089 INIT_Q_COUNTER(implied_nak_seq_err),
5090 INIT_Q_COUNTER(local_ack_timeout_err),
5093 #define INIT_CONG_COUNTER(_name) \
5094 { .name = #_name, .offset = \
5095 MLX5_BYTE_OFF(query_cong_statistics_out, _name ## _high)}
5097 static const struct mlx5_ib_counter cong_cnts[] = {
5098 INIT_CONG_COUNTER(rp_cnp_ignored),
5099 INIT_CONG_COUNTER(rp_cnp_handled),
5100 INIT_CONG_COUNTER(np_ecn_marked_roce_packets),
5101 INIT_CONG_COUNTER(np_cnp_sent),
5104 static const struct mlx5_ib_counter extended_err_cnts[] = {
5105 INIT_Q_COUNTER(resp_local_length_error),
5106 INIT_Q_COUNTER(resp_cqe_error),
5107 INIT_Q_COUNTER(req_cqe_error),
5108 INIT_Q_COUNTER(req_remote_invalid_request),
5109 INIT_Q_COUNTER(req_remote_access_errors),
5110 INIT_Q_COUNTER(resp_remote_access_errors),
5111 INIT_Q_COUNTER(resp_cqe_flush_error),
5112 INIT_Q_COUNTER(req_cqe_flush_error),
5115 #define INIT_EXT_PPCNT_COUNTER(_name) \
5116 { .name = #_name, .offset = \
5117 MLX5_BYTE_OFF(ppcnt_reg, \
5118 counter_set.eth_extended_cntrs_grp_data_layout._name##_high)}
5120 static const struct mlx5_ib_counter ext_ppcnt_cnts[] = {
5121 INIT_EXT_PPCNT_COUNTER(rx_icrc_encapsulated),
5124 static void mlx5_ib_dealloc_counters(struct mlx5_ib_dev *dev)
5128 for (i = 0; i < dev->num_ports; i++) {
5129 if (dev->port[i].cnts.set_id_valid)
5130 mlx5_core_dealloc_q_counter(dev->mdev,
5131 dev->port[i].cnts.set_id);
5132 kfree(dev->port[i].cnts.names);
5133 kfree(dev->port[i].cnts.offsets);
5137 static int __mlx5_ib_alloc_counters(struct mlx5_ib_dev *dev,
5138 struct mlx5_ib_counters *cnts)
5142 num_counters = ARRAY_SIZE(basic_q_cnts);
5144 if (MLX5_CAP_GEN(dev->mdev, out_of_seq_cnt))
5145 num_counters += ARRAY_SIZE(out_of_seq_q_cnts);
5147 if (MLX5_CAP_GEN(dev->mdev, retransmission_q_counters))
5148 num_counters += ARRAY_SIZE(retrans_q_cnts);
5150 if (MLX5_CAP_GEN(dev->mdev, enhanced_error_q_counters))
5151 num_counters += ARRAY_SIZE(extended_err_cnts);
5153 cnts->num_q_counters = num_counters;
5155 if (MLX5_CAP_GEN(dev->mdev, cc_query_allowed)) {
5156 cnts->num_cong_counters = ARRAY_SIZE(cong_cnts);
5157 num_counters += ARRAY_SIZE(cong_cnts);
5159 if (MLX5_CAP_PCAM_FEATURE(dev->mdev, rx_icrc_encapsulated_counter)) {
5160 cnts->num_ext_ppcnt_counters = ARRAY_SIZE(ext_ppcnt_cnts);
5161 num_counters += ARRAY_SIZE(ext_ppcnt_cnts);
5163 cnts->names = kcalloc(num_counters, sizeof(cnts->names), GFP_KERNEL);
5167 cnts->offsets = kcalloc(num_counters,
5168 sizeof(cnts->offsets), GFP_KERNEL);
5180 static void mlx5_ib_fill_counters(struct mlx5_ib_dev *dev,
5187 for (i = 0; i < ARRAY_SIZE(basic_q_cnts); i++, j++) {
5188 names[j] = basic_q_cnts[i].name;
5189 offsets[j] = basic_q_cnts[i].offset;
5192 if (MLX5_CAP_GEN(dev->mdev, out_of_seq_cnt)) {
5193 for (i = 0; i < ARRAY_SIZE(out_of_seq_q_cnts); i++, j++) {
5194 names[j] = out_of_seq_q_cnts[i].name;
5195 offsets[j] = out_of_seq_q_cnts[i].offset;
5199 if (MLX5_CAP_GEN(dev->mdev, retransmission_q_counters)) {
5200 for (i = 0; i < ARRAY_SIZE(retrans_q_cnts); i++, j++) {
5201 names[j] = retrans_q_cnts[i].name;
5202 offsets[j] = retrans_q_cnts[i].offset;
5206 if (MLX5_CAP_GEN(dev->mdev, enhanced_error_q_counters)) {
5207 for (i = 0; i < ARRAY_SIZE(extended_err_cnts); i++, j++) {
5208 names[j] = extended_err_cnts[i].name;
5209 offsets[j] = extended_err_cnts[i].offset;
5213 if (MLX5_CAP_GEN(dev->mdev, cc_query_allowed)) {
5214 for (i = 0; i < ARRAY_SIZE(cong_cnts); i++, j++) {
5215 names[j] = cong_cnts[i].name;
5216 offsets[j] = cong_cnts[i].offset;
5220 if (MLX5_CAP_PCAM_FEATURE(dev->mdev, rx_icrc_encapsulated_counter)) {
5221 for (i = 0; i < ARRAY_SIZE(ext_ppcnt_cnts); i++, j++) {
5222 names[j] = ext_ppcnt_cnts[i].name;
5223 offsets[j] = ext_ppcnt_cnts[i].offset;
5228 static int mlx5_ib_alloc_counters(struct mlx5_ib_dev *dev)
5234 is_shared = MLX5_CAP_GEN(dev->mdev, log_max_uctx) != 0;
5236 for (i = 0; i < dev->num_ports; i++) {
5237 err = __mlx5_ib_alloc_counters(dev, &dev->port[i].cnts);
5241 mlx5_ib_fill_counters(dev, dev->port[i].cnts.names,
5242 dev->port[i].cnts.offsets);
5244 err = mlx5_cmd_alloc_q_counter(dev->mdev,
5245 &dev->port[i].cnts.set_id,
5247 MLX5_SHARED_RESOURCE_UID : 0);
5250 "couldn't allocate queue counter for port %d, err %d\n",
5254 dev->port[i].cnts.set_id_valid = true;
5260 mlx5_ib_dealloc_counters(dev);
5264 static struct rdma_hw_stats *mlx5_ib_alloc_hw_stats(struct ib_device *ibdev,
5267 struct mlx5_ib_dev *dev = to_mdev(ibdev);
5268 struct mlx5_ib_port *port = &dev->port[port_num - 1];
5270 /* We support only per port stats */
5274 return rdma_alloc_hw_stats_struct(port->cnts.names,
5275 port->cnts.num_q_counters +
5276 port->cnts.num_cong_counters +
5277 port->cnts.num_ext_ppcnt_counters,
5278 RDMA_HW_STATS_DEFAULT_LIFESPAN);
5281 static int mlx5_ib_query_q_counters(struct mlx5_core_dev *mdev,
5282 struct mlx5_ib_port *port,
5283 struct rdma_hw_stats *stats)
5285 int outlen = MLX5_ST_SZ_BYTES(query_q_counter_out);
5290 out = kvzalloc(outlen, GFP_KERNEL);
5294 ret = mlx5_core_query_q_counter(mdev,
5295 port->cnts.set_id, 0,
5300 for (i = 0; i < port->cnts.num_q_counters; i++) {
5301 val = *(__be32 *)(out + port->cnts.offsets[i]);
5302 stats->value[i] = (u64)be32_to_cpu(val);
5310 static int mlx5_ib_query_ext_ppcnt_counters(struct mlx5_ib_dev *dev,
5311 struct mlx5_ib_port *port,
5312 struct rdma_hw_stats *stats)
5314 int offset = port->cnts.num_q_counters + port->cnts.num_cong_counters;
5315 int sz = MLX5_ST_SZ_BYTES(ppcnt_reg);
5319 out = kvzalloc(sz, GFP_KERNEL);
5323 ret = mlx5_cmd_query_ext_ppcnt_counters(dev->mdev, out);
5327 for (i = 0; i < port->cnts.num_ext_ppcnt_counters; i++) {
5328 stats->value[i + offset] =
5329 be64_to_cpup((__be64 *)(out +
5330 port->cnts.offsets[i + offset]));
5338 static int mlx5_ib_get_hw_stats(struct ib_device *ibdev,
5339 struct rdma_hw_stats *stats,
5340 u8 port_num, int index)
5342 struct mlx5_ib_dev *dev = to_mdev(ibdev);
5343 struct mlx5_ib_port *port = &dev->port[port_num - 1];
5344 struct mlx5_core_dev *mdev;
5345 int ret, num_counters;
5351 num_counters = port->cnts.num_q_counters +
5352 port->cnts.num_cong_counters +
5353 port->cnts.num_ext_ppcnt_counters;
5355 /* q_counters are per IB device, query the master mdev */
5356 ret = mlx5_ib_query_q_counters(dev->mdev, port, stats);
5360 if (MLX5_CAP_PCAM_FEATURE(dev->mdev, rx_icrc_encapsulated_counter)) {
5361 ret = mlx5_ib_query_ext_ppcnt_counters(dev, port, stats);
5366 if (MLX5_CAP_GEN(dev->mdev, cc_query_allowed)) {
5367 mdev = mlx5_ib_get_native_port_mdev(dev, port_num,
5370 /* If port is not affiliated yet, its in down state
5371 * which doesn't have any counters yet, so it would be
5372 * zero. So no need to read from the HCA.
5376 ret = mlx5_lag_query_cong_counters(dev->mdev,
5378 port->cnts.num_q_counters,
5379 port->cnts.num_cong_counters,
5380 port->cnts.offsets +
5381 port->cnts.num_q_counters);
5383 mlx5_ib_put_native_port_mdev(dev, port_num);
5389 return num_counters;
5392 static int mlx5_ib_rn_get_params(struct ib_device *device, u8 port_num,
5393 enum rdma_netdev_t type,
5394 struct rdma_netdev_alloc_params *params)
5396 if (type != RDMA_NETDEV_IPOIB)
5399 return mlx5_rdma_rn_get_params(to_mdev(device)->mdev, device, params);
5402 static void delay_drop_debugfs_cleanup(struct mlx5_ib_dev *dev)
5404 if (!dev->delay_drop.dbg)
5406 debugfs_remove_recursive(dev->delay_drop.dbg->dir_debugfs);
5407 kfree(dev->delay_drop.dbg);
5408 dev->delay_drop.dbg = NULL;
5411 static void cancel_delay_drop(struct mlx5_ib_dev *dev)
5413 if (!(dev->ib_dev.attrs.raw_packet_caps & IB_RAW_PACKET_CAP_DELAY_DROP))
5416 cancel_work_sync(&dev->delay_drop.delay_drop_work);
5417 delay_drop_debugfs_cleanup(dev);
5420 static ssize_t delay_drop_timeout_read(struct file *filp, char __user *buf,
5421 size_t count, loff_t *pos)
5423 struct mlx5_ib_delay_drop *delay_drop = filp->private_data;
5427 len = snprintf(lbuf, sizeof(lbuf), "%u\n", delay_drop->timeout);
5428 return simple_read_from_buffer(buf, count, pos, lbuf, len);
5431 static ssize_t delay_drop_timeout_write(struct file *filp, const char __user *buf,
5432 size_t count, loff_t *pos)
5434 struct mlx5_ib_delay_drop *delay_drop = filp->private_data;
5438 if (kstrtouint_from_user(buf, count, 0, &var))
5441 timeout = min_t(u32, roundup(var, 100), MLX5_MAX_DELAY_DROP_TIMEOUT_MS *
5444 mlx5_ib_dbg(delay_drop->dev, "Round delay drop timeout to %u usec\n",
5447 delay_drop->timeout = timeout;
5452 static const struct file_operations fops_delay_drop_timeout = {
5453 .owner = THIS_MODULE,
5454 .open = simple_open,
5455 .write = delay_drop_timeout_write,
5456 .read = delay_drop_timeout_read,
5459 static int delay_drop_debugfs_init(struct mlx5_ib_dev *dev)
5461 struct mlx5_ib_dbg_delay_drop *dbg;
5463 if (!mlx5_debugfs_root)
5466 dbg = kzalloc(sizeof(*dbg), GFP_KERNEL);
5470 dev->delay_drop.dbg = dbg;
5473 debugfs_create_dir("delay_drop",
5474 dev->mdev->priv.dbg_root);
5475 if (!dbg->dir_debugfs)
5478 dbg->events_cnt_debugfs =
5479 debugfs_create_atomic_t("num_timeout_events", 0400,
5481 &dev->delay_drop.events_cnt);
5482 if (!dbg->events_cnt_debugfs)
5485 dbg->rqs_cnt_debugfs =
5486 debugfs_create_atomic_t("num_rqs", 0400,
5488 &dev->delay_drop.rqs_cnt);
5489 if (!dbg->rqs_cnt_debugfs)
5492 dbg->timeout_debugfs =
5493 debugfs_create_file("timeout", 0600,
5496 &fops_delay_drop_timeout);
5497 if (!dbg->timeout_debugfs)
5503 delay_drop_debugfs_cleanup(dev);
5507 static void init_delay_drop(struct mlx5_ib_dev *dev)
5509 if (!(dev->ib_dev.attrs.raw_packet_caps & IB_RAW_PACKET_CAP_DELAY_DROP))
5512 mutex_init(&dev->delay_drop.lock);
5513 dev->delay_drop.dev = dev;
5514 dev->delay_drop.activate = false;
5515 dev->delay_drop.timeout = MLX5_MAX_DELAY_DROP_TIMEOUT_MS * 1000;
5516 INIT_WORK(&dev->delay_drop.delay_drop_work, delay_drop_handler);
5517 atomic_set(&dev->delay_drop.rqs_cnt, 0);
5518 atomic_set(&dev->delay_drop.events_cnt, 0);
5520 if (delay_drop_debugfs_init(dev))
5521 mlx5_ib_warn(dev, "Failed to init delay drop debugfs\n");
5524 /* The mlx5_ib_multiport_mutex should be held when calling this function */
5525 static void mlx5_ib_unbind_slave_port(struct mlx5_ib_dev *ibdev,
5526 struct mlx5_ib_multiport_info *mpi)
5528 u8 port_num = mlx5_core_native_port_num(mpi->mdev) - 1;
5529 struct mlx5_ib_port *port = &ibdev->port[port_num];
5534 mlx5_ib_cleanup_cong_debugfs(ibdev, port_num);
5536 spin_lock(&port->mp.mpi_lock);
5538 spin_unlock(&port->mp.mpi_lock);
5542 if (mpi->mdev_events.notifier_call)
5543 mlx5_notifier_unregister(mpi->mdev, &mpi->mdev_events);
5544 mpi->mdev_events.notifier_call = NULL;
5548 spin_unlock(&port->mp.mpi_lock);
5549 mlx5_remove_netdev_notifier(ibdev, port_num);
5550 spin_lock(&port->mp.mpi_lock);
5552 comps = mpi->mdev_refcnt;
5554 mpi->unaffiliate = true;
5555 init_completion(&mpi->unref_comp);
5556 spin_unlock(&port->mp.mpi_lock);
5558 for (i = 0; i < comps; i++)
5559 wait_for_completion(&mpi->unref_comp);
5561 spin_lock(&port->mp.mpi_lock);
5562 mpi->unaffiliate = false;
5565 port->mp.mpi = NULL;
5567 list_add_tail(&mpi->list, &mlx5_ib_unaffiliated_port_list);
5569 spin_unlock(&port->mp.mpi_lock);
5571 err = mlx5_nic_vport_unaffiliate_multiport(mpi->mdev);
5573 mlx5_ib_dbg(ibdev, "unaffiliated port %d\n", port_num + 1);
5574 /* Log an error, still needed to cleanup the pointers and add
5575 * it back to the list.
5578 mlx5_ib_err(ibdev, "Failed to unaffiliate port %u\n",
5581 ibdev->roce[port_num].last_port_state = IB_PORT_DOWN;
5584 /* The mlx5_ib_multiport_mutex should be held when calling this function */
5585 static bool mlx5_ib_bind_slave_port(struct mlx5_ib_dev *ibdev,
5586 struct mlx5_ib_multiport_info *mpi)
5588 u8 port_num = mlx5_core_native_port_num(mpi->mdev) - 1;
5591 spin_lock(&ibdev->port[port_num].mp.mpi_lock);
5592 if (ibdev->port[port_num].mp.mpi) {
5593 mlx5_ib_dbg(ibdev, "port %d already affiliated.\n",
5595 spin_unlock(&ibdev->port[port_num].mp.mpi_lock);
5599 ibdev->port[port_num].mp.mpi = mpi;
5601 mpi->mdev_events.notifier_call = NULL;
5602 spin_unlock(&ibdev->port[port_num].mp.mpi_lock);
5604 err = mlx5_nic_vport_affiliate_multiport(ibdev->mdev, mpi->mdev);
5608 err = get_port_caps(ibdev, mlx5_core_native_port_num(mpi->mdev));
5612 err = mlx5_add_netdev_notifier(ibdev, port_num);
5614 mlx5_ib_err(ibdev, "failed adding netdev notifier for port %u\n",
5619 mpi->mdev_events.notifier_call = mlx5_ib_event_slave_port;
5620 mlx5_notifier_register(mpi->mdev, &mpi->mdev_events);
5622 mlx5_ib_init_cong_debugfs(ibdev, port_num);
5627 mlx5_ib_unbind_slave_port(ibdev, mpi);
5631 static int mlx5_ib_init_multiport_master(struct mlx5_ib_dev *dev)
5633 int port_num = mlx5_core_native_port_num(dev->mdev) - 1;
5634 enum rdma_link_layer ll = mlx5_ib_port_link_layer(&dev->ib_dev,
5636 struct mlx5_ib_multiport_info *mpi;
5640 if (!mlx5_core_is_mp_master(dev->mdev) || ll != IB_LINK_LAYER_ETHERNET)
5643 err = mlx5_query_nic_vport_system_image_guid(dev->mdev,
5644 &dev->sys_image_guid);
5648 err = mlx5_nic_vport_enable_roce(dev->mdev);
5652 mutex_lock(&mlx5_ib_multiport_mutex);
5653 for (i = 0; i < dev->num_ports; i++) {
5656 /* build a stub multiport info struct for the native port. */
5657 if (i == port_num) {
5658 mpi = kzalloc(sizeof(*mpi), GFP_KERNEL);
5660 mutex_unlock(&mlx5_ib_multiport_mutex);
5661 mlx5_nic_vport_disable_roce(dev->mdev);
5665 mpi->is_master = true;
5666 mpi->mdev = dev->mdev;
5667 mpi->sys_image_guid = dev->sys_image_guid;
5668 dev->port[i].mp.mpi = mpi;
5674 list_for_each_entry(mpi, &mlx5_ib_unaffiliated_port_list,
5676 if (dev->sys_image_guid == mpi->sys_image_guid &&
5677 (mlx5_core_native_port_num(mpi->mdev) - 1) == i) {
5678 bound = mlx5_ib_bind_slave_port(dev, mpi);
5682 dev_dbg(mpi->mdev->device,
5683 "removing port from unaffiliated list.\n");
5684 mlx5_ib_dbg(dev, "port %d bound\n", i + 1);
5685 list_del(&mpi->list);
5690 get_port_caps(dev, i + 1);
5691 mlx5_ib_dbg(dev, "no free port found for port %d\n",
5696 list_add_tail(&dev->ib_dev_list, &mlx5_ib_dev_list);
5697 mutex_unlock(&mlx5_ib_multiport_mutex);
5701 static void mlx5_ib_cleanup_multiport_master(struct mlx5_ib_dev *dev)
5703 int port_num = mlx5_core_native_port_num(dev->mdev) - 1;
5704 enum rdma_link_layer ll = mlx5_ib_port_link_layer(&dev->ib_dev,
5708 if (!mlx5_core_is_mp_master(dev->mdev) || ll != IB_LINK_LAYER_ETHERNET)
5711 mutex_lock(&mlx5_ib_multiport_mutex);
5712 for (i = 0; i < dev->num_ports; i++) {
5713 if (dev->port[i].mp.mpi) {
5714 /* Destroy the native port stub */
5715 if (i == port_num) {
5716 kfree(dev->port[i].mp.mpi);
5717 dev->port[i].mp.mpi = NULL;
5719 mlx5_ib_dbg(dev, "unbinding port_num: %d\n", i + 1);
5720 mlx5_ib_unbind_slave_port(dev, dev->port[i].mp.mpi);
5725 mlx5_ib_dbg(dev, "removing from devlist\n");
5726 list_del(&dev->ib_dev_list);
5727 mutex_unlock(&mlx5_ib_multiport_mutex);
5729 mlx5_nic_vport_disable_roce(dev->mdev);
5732 ADD_UVERBS_ATTRIBUTES_SIMPLE(
5735 UVERBS_METHOD_DM_ALLOC,
5736 UVERBS_ATTR_PTR_OUT(MLX5_IB_ATTR_ALLOC_DM_RESP_START_OFFSET,
5737 UVERBS_ATTR_TYPE(u64),
5739 UVERBS_ATTR_PTR_OUT(MLX5_IB_ATTR_ALLOC_DM_RESP_PAGE_INDEX,
5740 UVERBS_ATTR_TYPE(u16),
5743 ADD_UVERBS_ATTRIBUTES_SIMPLE(
5744 mlx5_ib_flow_action,
5745 UVERBS_OBJECT_FLOW_ACTION,
5746 UVERBS_METHOD_FLOW_ACTION_ESP_CREATE,
5747 UVERBS_ATTR_FLAGS_IN(MLX5_IB_ATTR_CREATE_FLOW_ACTION_FLAGS,
5748 enum mlx5_ib_uapi_flow_action_flags));
5750 static const struct uapi_definition mlx5_ib_defs[] = {
5751 #if IS_ENABLED(CONFIG_INFINIBAND_USER_ACCESS)
5752 UAPI_DEF_CHAIN(mlx5_ib_devx_defs),
5753 UAPI_DEF_CHAIN(mlx5_ib_flow_defs),
5756 UAPI_DEF_CHAIN_OBJ_TREE(UVERBS_OBJECT_FLOW_ACTION,
5757 &mlx5_ib_flow_action),
5758 UAPI_DEF_CHAIN_OBJ_TREE(UVERBS_OBJECT_DM, &mlx5_ib_dm),
5762 static int mlx5_ib_read_counters(struct ib_counters *counters,
5763 struct ib_counters_read_attr *read_attr,
5764 struct uverbs_attr_bundle *attrs)
5766 struct mlx5_ib_mcounters *mcounters = to_mcounters(counters);
5767 struct mlx5_read_counters_attr mread_attr = {};
5768 struct mlx5_ib_flow_counters_desc *desc;
5771 mutex_lock(&mcounters->mcntrs_mutex);
5772 if (mcounters->cntrs_max_index > read_attr->ncounters) {
5777 mread_attr.out = kcalloc(mcounters->counters_num, sizeof(u64),
5779 if (!mread_attr.out) {
5784 mread_attr.hw_cntrs_hndl = mcounters->hw_cntrs_hndl;
5785 mread_attr.flags = read_attr->flags;
5786 ret = mcounters->read_counters(counters->device, &mread_attr);
5790 /* do the pass over the counters data array to assign according to the
5791 * descriptions and indexing pairs
5793 desc = mcounters->counters_data;
5794 for (i = 0; i < mcounters->ncounters; i++)
5795 read_attr->counters_buff[desc[i].index] += mread_attr.out[desc[i].description];
5798 kfree(mread_attr.out);
5800 mutex_unlock(&mcounters->mcntrs_mutex);
5804 static int mlx5_ib_destroy_counters(struct ib_counters *counters)
5806 struct mlx5_ib_mcounters *mcounters = to_mcounters(counters);
5808 counters_clear_description(counters);
5809 if (mcounters->hw_cntrs_hndl)
5810 mlx5_fc_destroy(to_mdev(counters->device)->mdev,
5811 mcounters->hw_cntrs_hndl);
5818 static struct ib_counters *mlx5_ib_create_counters(struct ib_device *device,
5819 struct uverbs_attr_bundle *attrs)
5821 struct mlx5_ib_mcounters *mcounters;
5823 mcounters = kzalloc(sizeof(*mcounters), GFP_KERNEL);
5825 return ERR_PTR(-ENOMEM);
5827 mutex_init(&mcounters->mcntrs_mutex);
5829 return &mcounters->ibcntrs;
5832 void mlx5_ib_stage_init_cleanup(struct mlx5_ib_dev *dev)
5834 mlx5_ib_cleanup_multiport_master(dev);
5835 if (IS_ENABLED(CONFIG_INFINIBAND_ON_DEMAND_PAGING)) {
5836 srcu_barrier(&dev->mr_srcu);
5837 cleanup_srcu_struct(&dev->mr_srcu);
5842 int mlx5_ib_stage_init_init(struct mlx5_ib_dev *dev)
5844 struct mlx5_core_dev *mdev = dev->mdev;
5848 dev->port = kcalloc(dev->num_ports, sizeof(*dev->port),
5853 for (i = 0; i < dev->num_ports; i++) {
5854 spin_lock_init(&dev->port[i].mp.mpi_lock);
5855 rwlock_init(&dev->roce[i].netdev_lock);
5858 err = mlx5_ib_init_multiport_master(dev);
5862 if (!mlx5_core_mp_enabled(mdev)) {
5863 for (i = 1; i <= dev->num_ports; i++) {
5864 err = get_port_caps(dev, i);
5869 err = get_port_caps(dev, mlx5_core_native_port_num(mdev));
5874 if (mlx5_use_mad_ifc(dev))
5875 get_ext_port_caps(dev);
5877 dev->ib_dev.owner = THIS_MODULE;
5878 dev->ib_dev.node_type = RDMA_NODE_IB_CA;
5879 dev->ib_dev.local_dma_lkey = 0 /* not supported for now */;
5880 dev->ib_dev.phys_port_cnt = dev->num_ports;
5881 dev->ib_dev.num_comp_vectors = mlx5_comp_vectors_count(mdev);
5882 dev->ib_dev.dev.parent = mdev->device;
5884 mutex_init(&dev->cap_mask_mutex);
5885 INIT_LIST_HEAD(&dev->qp_list);
5886 spin_lock_init(&dev->reset_flow_resource_lock);
5888 spin_lock_init(&dev->memic.memic_lock);
5889 dev->memic.dev = mdev;
5891 if (IS_ENABLED(CONFIG_INFINIBAND_ON_DEMAND_PAGING)) {
5892 err = init_srcu_struct(&dev->mr_srcu);
5899 mlx5_ib_cleanup_multiport_master(dev);
5907 static int mlx5_ib_stage_flow_db_init(struct mlx5_ib_dev *dev)
5909 dev->flow_db = kzalloc(sizeof(*dev->flow_db), GFP_KERNEL);
5914 mutex_init(&dev->flow_db->lock);
5919 int mlx5_ib_stage_rep_flow_db_init(struct mlx5_ib_dev *dev)
5921 struct mlx5_ib_dev *nic_dev;
5923 nic_dev = mlx5_ib_get_uplink_ibdev(dev->mdev->priv.eswitch);
5928 dev->flow_db = nic_dev->flow_db;
5933 static void mlx5_ib_stage_flow_db_cleanup(struct mlx5_ib_dev *dev)
5935 kfree(dev->flow_db);
5938 static const struct ib_device_ops mlx5_ib_dev_ops = {
5939 .add_gid = mlx5_ib_add_gid,
5940 .alloc_mr = mlx5_ib_alloc_mr,
5941 .alloc_pd = mlx5_ib_alloc_pd,
5942 .alloc_ucontext = mlx5_ib_alloc_ucontext,
5943 .attach_mcast = mlx5_ib_mcg_attach,
5944 .check_mr_status = mlx5_ib_check_mr_status,
5945 .create_ah = mlx5_ib_create_ah,
5946 .create_counters = mlx5_ib_create_counters,
5947 .create_cq = mlx5_ib_create_cq,
5948 .create_flow = mlx5_ib_create_flow,
5949 .create_qp = mlx5_ib_create_qp,
5950 .create_srq = mlx5_ib_create_srq,
5951 .dealloc_pd = mlx5_ib_dealloc_pd,
5952 .dealloc_ucontext = mlx5_ib_dealloc_ucontext,
5953 .del_gid = mlx5_ib_del_gid,
5954 .dereg_mr = mlx5_ib_dereg_mr,
5955 .destroy_ah = mlx5_ib_destroy_ah,
5956 .destroy_counters = mlx5_ib_destroy_counters,
5957 .destroy_cq = mlx5_ib_destroy_cq,
5958 .destroy_flow = mlx5_ib_destroy_flow,
5959 .destroy_flow_action = mlx5_ib_destroy_flow_action,
5960 .destroy_qp = mlx5_ib_destroy_qp,
5961 .destroy_srq = mlx5_ib_destroy_srq,
5962 .detach_mcast = mlx5_ib_mcg_detach,
5963 .disassociate_ucontext = mlx5_ib_disassociate_ucontext,
5964 .drain_rq = mlx5_ib_drain_rq,
5965 .drain_sq = mlx5_ib_drain_sq,
5966 .get_dev_fw_str = get_dev_fw_str,
5967 .get_dma_mr = mlx5_ib_get_dma_mr,
5968 .get_link_layer = mlx5_ib_port_link_layer,
5969 .map_mr_sg = mlx5_ib_map_mr_sg,
5970 .mmap = mlx5_ib_mmap,
5971 .modify_cq = mlx5_ib_modify_cq,
5972 .modify_device = mlx5_ib_modify_device,
5973 .modify_port = mlx5_ib_modify_port,
5974 .modify_qp = mlx5_ib_modify_qp,
5975 .modify_srq = mlx5_ib_modify_srq,
5976 .poll_cq = mlx5_ib_poll_cq,
5977 .post_recv = mlx5_ib_post_recv,
5978 .post_send = mlx5_ib_post_send,
5979 .post_srq_recv = mlx5_ib_post_srq_recv,
5980 .process_mad = mlx5_ib_process_mad,
5981 .query_ah = mlx5_ib_query_ah,
5982 .query_device = mlx5_ib_query_device,
5983 .query_gid = mlx5_ib_query_gid,
5984 .query_pkey = mlx5_ib_query_pkey,
5985 .query_qp = mlx5_ib_query_qp,
5986 .query_srq = mlx5_ib_query_srq,
5987 .read_counters = mlx5_ib_read_counters,
5988 .reg_user_mr = mlx5_ib_reg_user_mr,
5989 .req_notify_cq = mlx5_ib_arm_cq,
5990 .rereg_user_mr = mlx5_ib_rereg_user_mr,
5991 .resize_cq = mlx5_ib_resize_cq,
5992 INIT_RDMA_OBJ_SIZE(ib_pd, mlx5_ib_pd, ibpd),
5993 INIT_RDMA_OBJ_SIZE(ib_ucontext, mlx5_ib_ucontext, ibucontext),
5996 static const struct ib_device_ops mlx5_ib_dev_flow_ipsec_ops = {
5997 .create_flow_action_esp = mlx5_ib_create_flow_action_esp,
5998 .modify_flow_action_esp = mlx5_ib_modify_flow_action_esp,
6001 static const struct ib_device_ops mlx5_ib_dev_ipoib_enhanced_ops = {
6002 .rdma_netdev_get_params = mlx5_ib_rn_get_params,
6005 static const struct ib_device_ops mlx5_ib_dev_sriov_ops = {
6006 .get_vf_config = mlx5_ib_get_vf_config,
6007 .get_vf_stats = mlx5_ib_get_vf_stats,
6008 .set_vf_guid = mlx5_ib_set_vf_guid,
6009 .set_vf_link_state = mlx5_ib_set_vf_link_state,
6012 static const struct ib_device_ops mlx5_ib_dev_mw_ops = {
6013 .alloc_mw = mlx5_ib_alloc_mw,
6014 .dealloc_mw = mlx5_ib_dealloc_mw,
6017 static const struct ib_device_ops mlx5_ib_dev_xrc_ops = {
6018 .alloc_xrcd = mlx5_ib_alloc_xrcd,
6019 .dealloc_xrcd = mlx5_ib_dealloc_xrcd,
6022 static const struct ib_device_ops mlx5_ib_dev_dm_ops = {
6023 .alloc_dm = mlx5_ib_alloc_dm,
6024 .dealloc_dm = mlx5_ib_dealloc_dm,
6025 .reg_dm_mr = mlx5_ib_reg_dm_mr,
6028 int mlx5_ib_stage_caps_init(struct mlx5_ib_dev *dev)
6030 struct mlx5_core_dev *mdev = dev->mdev;
6033 dev->ib_dev.uverbs_abi_ver = MLX5_IB_UVERBS_ABI_VERSION;
6034 dev->ib_dev.uverbs_cmd_mask =
6035 (1ull << IB_USER_VERBS_CMD_GET_CONTEXT) |
6036 (1ull << IB_USER_VERBS_CMD_QUERY_DEVICE) |
6037 (1ull << IB_USER_VERBS_CMD_QUERY_PORT) |
6038 (1ull << IB_USER_VERBS_CMD_ALLOC_PD) |
6039 (1ull << IB_USER_VERBS_CMD_DEALLOC_PD) |
6040 (1ull << IB_USER_VERBS_CMD_CREATE_AH) |
6041 (1ull << IB_USER_VERBS_CMD_DESTROY_AH) |
6042 (1ull << IB_USER_VERBS_CMD_REG_MR) |
6043 (1ull << IB_USER_VERBS_CMD_REREG_MR) |
6044 (1ull << IB_USER_VERBS_CMD_DEREG_MR) |
6045 (1ull << IB_USER_VERBS_CMD_CREATE_COMP_CHANNEL) |
6046 (1ull << IB_USER_VERBS_CMD_CREATE_CQ) |
6047 (1ull << IB_USER_VERBS_CMD_RESIZE_CQ) |
6048 (1ull << IB_USER_VERBS_CMD_DESTROY_CQ) |
6049 (1ull << IB_USER_VERBS_CMD_CREATE_QP) |
6050 (1ull << IB_USER_VERBS_CMD_MODIFY_QP) |
6051 (1ull << IB_USER_VERBS_CMD_QUERY_QP) |
6052 (1ull << IB_USER_VERBS_CMD_DESTROY_QP) |
6053 (1ull << IB_USER_VERBS_CMD_ATTACH_MCAST) |
6054 (1ull << IB_USER_VERBS_CMD_DETACH_MCAST) |
6055 (1ull << IB_USER_VERBS_CMD_CREATE_SRQ) |
6056 (1ull << IB_USER_VERBS_CMD_MODIFY_SRQ) |
6057 (1ull << IB_USER_VERBS_CMD_QUERY_SRQ) |
6058 (1ull << IB_USER_VERBS_CMD_DESTROY_SRQ) |
6059 (1ull << IB_USER_VERBS_CMD_CREATE_XSRQ) |
6060 (1ull << IB_USER_VERBS_CMD_OPEN_QP);
6061 dev->ib_dev.uverbs_ex_cmd_mask =
6062 (1ull << IB_USER_VERBS_EX_CMD_QUERY_DEVICE) |
6063 (1ull << IB_USER_VERBS_EX_CMD_CREATE_CQ) |
6064 (1ull << IB_USER_VERBS_EX_CMD_CREATE_QP) |
6065 (1ull << IB_USER_VERBS_EX_CMD_MODIFY_QP) |
6066 (1ull << IB_USER_VERBS_EX_CMD_MODIFY_CQ) |
6067 (1ull << IB_USER_VERBS_EX_CMD_CREATE_FLOW) |
6068 (1ull << IB_USER_VERBS_EX_CMD_DESTROY_FLOW);
6070 if (MLX5_CAP_GEN(mdev, ipoib_enhanced_offloads) &&
6071 IS_ENABLED(CONFIG_MLX5_CORE_IPOIB))
6072 ib_set_device_ops(&dev->ib_dev,
6073 &mlx5_ib_dev_ipoib_enhanced_ops);
6075 if (mlx5_core_is_pf(mdev))
6076 ib_set_device_ops(&dev->ib_dev, &mlx5_ib_dev_sriov_ops);
6078 dev->umr_fence = mlx5_get_umr_fence(MLX5_CAP_GEN(mdev, umr_fence));
6080 if (MLX5_CAP_GEN(mdev, imaicl)) {
6081 dev->ib_dev.uverbs_cmd_mask |=
6082 (1ull << IB_USER_VERBS_CMD_ALLOC_MW) |
6083 (1ull << IB_USER_VERBS_CMD_DEALLOC_MW);
6084 ib_set_device_ops(&dev->ib_dev, &mlx5_ib_dev_mw_ops);
6087 if (MLX5_CAP_GEN(mdev, xrc)) {
6088 dev->ib_dev.uverbs_cmd_mask |=
6089 (1ull << IB_USER_VERBS_CMD_OPEN_XRCD) |
6090 (1ull << IB_USER_VERBS_CMD_CLOSE_XRCD);
6091 ib_set_device_ops(&dev->ib_dev, &mlx5_ib_dev_xrc_ops);
6094 if (MLX5_CAP_DEV_MEM(mdev, memic))
6095 ib_set_device_ops(&dev->ib_dev, &mlx5_ib_dev_dm_ops);
6097 if (mlx5_accel_ipsec_device_caps(dev->mdev) &
6098 MLX5_ACCEL_IPSEC_CAP_DEVICE)
6099 ib_set_device_ops(&dev->ib_dev, &mlx5_ib_dev_flow_ipsec_ops);
6100 dev->ib_dev.driver_id = RDMA_DRIVER_MLX5;
6101 ib_set_device_ops(&dev->ib_dev, &mlx5_ib_dev_ops);
6103 if (IS_ENABLED(CONFIG_INFINIBAND_USER_ACCESS))
6104 dev->ib_dev.driver_def = mlx5_ib_defs;
6106 err = init_node_data(dev);
6110 if ((MLX5_CAP_GEN(dev->mdev, port_type) == MLX5_CAP_PORT_TYPE_ETH) &&
6111 (MLX5_CAP_GEN(dev->mdev, disable_local_lb_uc) ||
6112 MLX5_CAP_GEN(dev->mdev, disable_local_lb_mc)))
6113 mutex_init(&dev->lb.mutex);
6118 static const struct ib_device_ops mlx5_ib_dev_port_ops = {
6119 .get_port_immutable = mlx5_port_immutable,
6120 .query_port = mlx5_ib_query_port,
6123 static int mlx5_ib_stage_non_default_cb(struct mlx5_ib_dev *dev)
6125 ib_set_device_ops(&dev->ib_dev, &mlx5_ib_dev_port_ops);
6129 static const struct ib_device_ops mlx5_ib_dev_port_rep_ops = {
6130 .get_port_immutable = mlx5_port_rep_immutable,
6131 .query_port = mlx5_ib_rep_query_port,
6134 int mlx5_ib_stage_rep_non_default_cb(struct mlx5_ib_dev *dev)
6136 ib_set_device_ops(&dev->ib_dev, &mlx5_ib_dev_port_rep_ops);
6140 static const struct ib_device_ops mlx5_ib_dev_common_roce_ops = {
6141 .create_rwq_ind_table = mlx5_ib_create_rwq_ind_table,
6142 .create_wq = mlx5_ib_create_wq,
6143 .destroy_rwq_ind_table = mlx5_ib_destroy_rwq_ind_table,
6144 .destroy_wq = mlx5_ib_destroy_wq,
6145 .get_netdev = mlx5_ib_get_netdev,
6146 .modify_wq = mlx5_ib_modify_wq,
6149 static int mlx5_ib_stage_common_roce_init(struct mlx5_ib_dev *dev)
6154 for (i = 0; i < dev->num_ports; i++) {
6155 dev->roce[i].dev = dev;
6156 dev->roce[i].native_port_num = i + 1;
6157 dev->roce[i].last_port_state = IB_PORT_DOWN;
6160 dev->ib_dev.uverbs_ex_cmd_mask |=
6161 (1ull << IB_USER_VERBS_EX_CMD_CREATE_WQ) |
6162 (1ull << IB_USER_VERBS_EX_CMD_MODIFY_WQ) |
6163 (1ull << IB_USER_VERBS_EX_CMD_DESTROY_WQ) |
6164 (1ull << IB_USER_VERBS_EX_CMD_CREATE_RWQ_IND_TBL) |
6165 (1ull << IB_USER_VERBS_EX_CMD_DESTROY_RWQ_IND_TBL);
6166 ib_set_device_ops(&dev->ib_dev, &mlx5_ib_dev_common_roce_ops);
6168 port_num = mlx5_core_native_port_num(dev->mdev) - 1;
6170 return mlx5_add_netdev_notifier(dev, port_num);
6173 static void mlx5_ib_stage_common_roce_cleanup(struct mlx5_ib_dev *dev)
6175 u8 port_num = mlx5_core_native_port_num(dev->mdev) - 1;
6177 mlx5_remove_netdev_notifier(dev, port_num);
6180 int mlx5_ib_stage_rep_roce_init(struct mlx5_ib_dev *dev)
6182 struct mlx5_core_dev *mdev = dev->mdev;
6183 enum rdma_link_layer ll;
6187 port_type_cap = MLX5_CAP_GEN(mdev, port_type);
6188 ll = mlx5_port_type_cap_to_rdma_ll(port_type_cap);
6190 if (ll == IB_LINK_LAYER_ETHERNET)
6191 err = mlx5_ib_stage_common_roce_init(dev);
6196 void mlx5_ib_stage_rep_roce_cleanup(struct mlx5_ib_dev *dev)
6198 mlx5_ib_stage_common_roce_cleanup(dev);
6201 static int mlx5_ib_stage_roce_init(struct mlx5_ib_dev *dev)
6203 struct mlx5_core_dev *mdev = dev->mdev;
6204 enum rdma_link_layer ll;
6208 port_type_cap = MLX5_CAP_GEN(mdev, port_type);
6209 ll = mlx5_port_type_cap_to_rdma_ll(port_type_cap);
6211 if (ll == IB_LINK_LAYER_ETHERNET) {
6212 err = mlx5_ib_stage_common_roce_init(dev);
6216 err = mlx5_enable_eth(dev);
6223 mlx5_ib_stage_common_roce_cleanup(dev);
6228 static void mlx5_ib_stage_roce_cleanup(struct mlx5_ib_dev *dev)
6230 struct mlx5_core_dev *mdev = dev->mdev;
6231 enum rdma_link_layer ll;
6234 port_type_cap = MLX5_CAP_GEN(mdev, port_type);
6235 ll = mlx5_port_type_cap_to_rdma_ll(port_type_cap);
6237 if (ll == IB_LINK_LAYER_ETHERNET) {
6238 mlx5_disable_eth(dev);
6239 mlx5_ib_stage_common_roce_cleanup(dev);
6243 int mlx5_ib_stage_dev_res_init(struct mlx5_ib_dev *dev)
6245 return create_dev_resources(&dev->devr);
6248 void mlx5_ib_stage_dev_res_cleanup(struct mlx5_ib_dev *dev)
6250 destroy_dev_resources(&dev->devr);
6253 static int mlx5_ib_stage_odp_init(struct mlx5_ib_dev *dev)
6255 mlx5_ib_internal_fill_odp_caps(dev);
6257 return mlx5_ib_odp_init_one(dev);
6260 static void mlx5_ib_stage_odp_cleanup(struct mlx5_ib_dev *dev)
6262 mlx5_ib_odp_cleanup_one(dev);
6265 static const struct ib_device_ops mlx5_ib_dev_hw_stats_ops = {
6266 .alloc_hw_stats = mlx5_ib_alloc_hw_stats,
6267 .get_hw_stats = mlx5_ib_get_hw_stats,
6270 int mlx5_ib_stage_counters_init(struct mlx5_ib_dev *dev)
6272 if (MLX5_CAP_GEN(dev->mdev, max_qp_cnt)) {
6273 ib_set_device_ops(&dev->ib_dev, &mlx5_ib_dev_hw_stats_ops);
6275 return mlx5_ib_alloc_counters(dev);
6281 void mlx5_ib_stage_counters_cleanup(struct mlx5_ib_dev *dev)
6283 if (MLX5_CAP_GEN(dev->mdev, max_qp_cnt))
6284 mlx5_ib_dealloc_counters(dev);
6287 static int mlx5_ib_stage_cong_debugfs_init(struct mlx5_ib_dev *dev)
6289 mlx5_ib_init_cong_debugfs(dev,
6290 mlx5_core_native_port_num(dev->mdev) - 1);
6294 static void mlx5_ib_stage_cong_debugfs_cleanup(struct mlx5_ib_dev *dev)
6296 mlx5_ib_cleanup_cong_debugfs(dev,
6297 mlx5_core_native_port_num(dev->mdev) - 1);
6300 static int mlx5_ib_stage_uar_init(struct mlx5_ib_dev *dev)
6302 dev->mdev->priv.uar = mlx5_get_uars_page(dev->mdev);
6303 return PTR_ERR_OR_ZERO(dev->mdev->priv.uar);
6306 static void mlx5_ib_stage_uar_cleanup(struct mlx5_ib_dev *dev)
6308 mlx5_put_uars_page(dev->mdev, dev->mdev->priv.uar);
6311 int mlx5_ib_stage_bfrag_init(struct mlx5_ib_dev *dev)
6315 err = mlx5_alloc_bfreg(dev->mdev, &dev->bfreg, false, false);
6319 err = mlx5_alloc_bfreg(dev->mdev, &dev->fp_bfreg, false, true);
6321 mlx5_free_bfreg(dev->mdev, &dev->fp_bfreg);
6326 void mlx5_ib_stage_bfrag_cleanup(struct mlx5_ib_dev *dev)
6328 mlx5_free_bfreg(dev->mdev, &dev->fp_bfreg);
6329 mlx5_free_bfreg(dev->mdev, &dev->bfreg);
6332 int mlx5_ib_stage_ib_reg_init(struct mlx5_ib_dev *dev)
6336 rdma_set_device_sysfs_group(&dev->ib_dev, &mlx5_attr_group);
6337 if (!mlx5_lag_is_roce(dev->mdev))
6340 name = "mlx5_bond_%d";
6341 return ib_register_device(&dev->ib_dev, name);
6344 void mlx5_ib_stage_pre_ib_reg_umr_cleanup(struct mlx5_ib_dev *dev)
6346 destroy_umrc_res(dev);
6349 void mlx5_ib_stage_ib_reg_cleanup(struct mlx5_ib_dev *dev)
6351 ib_unregister_device(&dev->ib_dev);
6354 int mlx5_ib_stage_post_ib_reg_umr_init(struct mlx5_ib_dev *dev)
6356 return create_umr_res(dev);
6359 static int mlx5_ib_stage_delay_drop_init(struct mlx5_ib_dev *dev)
6361 init_delay_drop(dev);
6366 static void mlx5_ib_stage_delay_drop_cleanup(struct mlx5_ib_dev *dev)
6368 cancel_delay_drop(dev);
6371 static int mlx5_ib_stage_dev_notifier_init(struct mlx5_ib_dev *dev)
6373 dev->mdev_events.notifier_call = mlx5_ib_event;
6374 mlx5_notifier_register(dev->mdev, &dev->mdev_events);
6378 static void mlx5_ib_stage_dev_notifier_cleanup(struct mlx5_ib_dev *dev)
6380 mlx5_notifier_unregister(dev->mdev, &dev->mdev_events);
6383 static int mlx5_ib_stage_devx_init(struct mlx5_ib_dev *dev)
6387 uid = mlx5_ib_devx_create(dev, false);
6389 dev->devx_whitelist_uid = uid;
6393 static void mlx5_ib_stage_devx_cleanup(struct mlx5_ib_dev *dev)
6395 if (dev->devx_whitelist_uid)
6396 mlx5_ib_devx_destroy(dev, dev->devx_whitelist_uid);
6399 void __mlx5_ib_remove(struct mlx5_ib_dev *dev,
6400 const struct mlx5_ib_profile *profile,
6403 /* Number of stages to cleanup */
6406 if (profile->stage[stage].cleanup)
6407 profile->stage[stage].cleanup(dev);
6411 void *__mlx5_ib_add(struct mlx5_ib_dev *dev,
6412 const struct mlx5_ib_profile *profile)
6417 for (i = 0; i < MLX5_IB_STAGE_MAX; i++) {
6418 if (profile->stage[i].init) {
6419 err = profile->stage[i].init(dev);
6425 dev->profile = profile;
6426 dev->ib_active = true;
6431 __mlx5_ib_remove(dev, profile, i);
6436 static const struct mlx5_ib_profile pf_profile = {
6437 STAGE_CREATE(MLX5_IB_STAGE_INIT,
6438 mlx5_ib_stage_init_init,
6439 mlx5_ib_stage_init_cleanup),
6440 STAGE_CREATE(MLX5_IB_STAGE_FLOW_DB,
6441 mlx5_ib_stage_flow_db_init,
6442 mlx5_ib_stage_flow_db_cleanup),
6443 STAGE_CREATE(MLX5_IB_STAGE_CAPS,
6444 mlx5_ib_stage_caps_init,
6446 STAGE_CREATE(MLX5_IB_STAGE_NON_DEFAULT_CB,
6447 mlx5_ib_stage_non_default_cb,
6449 STAGE_CREATE(MLX5_IB_STAGE_ROCE,
6450 mlx5_ib_stage_roce_init,
6451 mlx5_ib_stage_roce_cleanup),
6452 STAGE_CREATE(MLX5_IB_STAGE_SRQ,
6453 mlx5_init_srq_table,
6454 mlx5_cleanup_srq_table),
6455 STAGE_CREATE(MLX5_IB_STAGE_DEVICE_RESOURCES,
6456 mlx5_ib_stage_dev_res_init,
6457 mlx5_ib_stage_dev_res_cleanup),
6458 STAGE_CREATE(MLX5_IB_STAGE_DEVICE_NOTIFIER,
6459 mlx5_ib_stage_dev_notifier_init,
6460 mlx5_ib_stage_dev_notifier_cleanup),
6461 STAGE_CREATE(MLX5_IB_STAGE_ODP,
6462 mlx5_ib_stage_odp_init,
6463 mlx5_ib_stage_odp_cleanup),
6464 STAGE_CREATE(MLX5_IB_STAGE_COUNTERS,
6465 mlx5_ib_stage_counters_init,
6466 mlx5_ib_stage_counters_cleanup),
6467 STAGE_CREATE(MLX5_IB_STAGE_CONG_DEBUGFS,
6468 mlx5_ib_stage_cong_debugfs_init,
6469 mlx5_ib_stage_cong_debugfs_cleanup),
6470 STAGE_CREATE(MLX5_IB_STAGE_UAR,
6471 mlx5_ib_stage_uar_init,
6472 mlx5_ib_stage_uar_cleanup),
6473 STAGE_CREATE(MLX5_IB_STAGE_BFREG,
6474 mlx5_ib_stage_bfrag_init,
6475 mlx5_ib_stage_bfrag_cleanup),
6476 STAGE_CREATE(MLX5_IB_STAGE_PRE_IB_REG_UMR,
6478 mlx5_ib_stage_pre_ib_reg_umr_cleanup),
6479 STAGE_CREATE(MLX5_IB_STAGE_WHITELIST_UID,
6480 mlx5_ib_stage_devx_init,
6481 mlx5_ib_stage_devx_cleanup),
6482 STAGE_CREATE(MLX5_IB_STAGE_IB_REG,
6483 mlx5_ib_stage_ib_reg_init,
6484 mlx5_ib_stage_ib_reg_cleanup),
6485 STAGE_CREATE(MLX5_IB_STAGE_POST_IB_REG_UMR,
6486 mlx5_ib_stage_post_ib_reg_umr_init,
6488 STAGE_CREATE(MLX5_IB_STAGE_DELAY_DROP,
6489 mlx5_ib_stage_delay_drop_init,
6490 mlx5_ib_stage_delay_drop_cleanup),
6493 const struct mlx5_ib_profile uplink_rep_profile = {
6494 STAGE_CREATE(MLX5_IB_STAGE_INIT,
6495 mlx5_ib_stage_init_init,
6496 mlx5_ib_stage_init_cleanup),
6497 STAGE_CREATE(MLX5_IB_STAGE_FLOW_DB,
6498 mlx5_ib_stage_flow_db_init,
6499 mlx5_ib_stage_flow_db_cleanup),
6500 STAGE_CREATE(MLX5_IB_STAGE_CAPS,
6501 mlx5_ib_stage_caps_init,
6503 STAGE_CREATE(MLX5_IB_STAGE_NON_DEFAULT_CB,
6504 mlx5_ib_stage_rep_non_default_cb,
6506 STAGE_CREATE(MLX5_IB_STAGE_ROCE,
6507 mlx5_ib_stage_rep_roce_init,
6508 mlx5_ib_stage_rep_roce_cleanup),
6509 STAGE_CREATE(MLX5_IB_STAGE_SRQ,
6510 mlx5_init_srq_table,
6511 mlx5_cleanup_srq_table),
6512 STAGE_CREATE(MLX5_IB_STAGE_DEVICE_RESOURCES,
6513 mlx5_ib_stage_dev_res_init,
6514 mlx5_ib_stage_dev_res_cleanup),
6515 STAGE_CREATE(MLX5_IB_STAGE_DEVICE_NOTIFIER,
6516 mlx5_ib_stage_dev_notifier_init,
6517 mlx5_ib_stage_dev_notifier_cleanup),
6518 STAGE_CREATE(MLX5_IB_STAGE_COUNTERS,
6519 mlx5_ib_stage_counters_init,
6520 mlx5_ib_stage_counters_cleanup),
6521 STAGE_CREATE(MLX5_IB_STAGE_UAR,
6522 mlx5_ib_stage_uar_init,
6523 mlx5_ib_stage_uar_cleanup),
6524 STAGE_CREATE(MLX5_IB_STAGE_BFREG,
6525 mlx5_ib_stage_bfrag_init,
6526 mlx5_ib_stage_bfrag_cleanup),
6527 STAGE_CREATE(MLX5_IB_STAGE_PRE_IB_REG_UMR,
6529 mlx5_ib_stage_pre_ib_reg_umr_cleanup),
6530 STAGE_CREATE(MLX5_IB_STAGE_IB_REG,
6531 mlx5_ib_stage_ib_reg_init,
6532 mlx5_ib_stage_ib_reg_cleanup),
6533 STAGE_CREATE(MLX5_IB_STAGE_POST_IB_REG_UMR,
6534 mlx5_ib_stage_post_ib_reg_umr_init,
6538 static void *mlx5_ib_add_slave_port(struct mlx5_core_dev *mdev)
6540 struct mlx5_ib_multiport_info *mpi;
6541 struct mlx5_ib_dev *dev;
6545 mpi = kzalloc(sizeof(*mpi), GFP_KERNEL);
6551 err = mlx5_query_nic_vport_system_image_guid(mdev,
6552 &mpi->sys_image_guid);
6558 mutex_lock(&mlx5_ib_multiport_mutex);
6559 list_for_each_entry(dev, &mlx5_ib_dev_list, ib_dev_list) {
6560 if (dev->sys_image_guid == mpi->sys_image_guid)
6561 bound = mlx5_ib_bind_slave_port(dev, mpi);
6564 rdma_roce_rescan_device(&dev->ib_dev);
6570 list_add_tail(&mpi->list, &mlx5_ib_unaffiliated_port_list);
6571 dev_dbg(mdev->device,
6572 "no suitable IB device found to bind to, added to unaffiliated list.\n");
6574 mutex_unlock(&mlx5_ib_multiport_mutex);
6579 static void *mlx5_ib_add(struct mlx5_core_dev *mdev)
6581 enum rdma_link_layer ll;
6582 struct mlx5_ib_dev *dev;
6585 printk_once(KERN_INFO "%s", mlx5_version);
6587 if (MLX5_ESWITCH_MANAGER(mdev) &&
6588 mlx5_ib_eswitch_mode(mdev->priv.eswitch) == SRIOV_OFFLOADS) {
6589 mlx5_ib_register_vport_reps(mdev);
6593 port_type_cap = MLX5_CAP_GEN(mdev, port_type);
6594 ll = mlx5_port_type_cap_to_rdma_ll(port_type_cap);
6596 if (mlx5_core_is_mp_slave(mdev) && ll == IB_LINK_LAYER_ETHERNET)
6597 return mlx5_ib_add_slave_port(mdev);
6599 dev = ib_alloc_device(mlx5_ib_dev, ib_dev);
6604 dev->num_ports = max(MLX5_CAP_GEN(mdev, num_ports),
6605 MLX5_CAP_GEN(mdev, num_vhca_ports));
6607 return __mlx5_ib_add(dev, &pf_profile);
6610 static void mlx5_ib_remove(struct mlx5_core_dev *mdev, void *context)
6612 struct mlx5_ib_multiport_info *mpi;
6613 struct mlx5_ib_dev *dev;
6615 if (MLX5_ESWITCH_MANAGER(mdev) && context == mdev) {
6616 mlx5_ib_unregister_vport_reps(mdev);
6620 if (mlx5_core_is_mp_slave(mdev)) {
6622 mutex_lock(&mlx5_ib_multiport_mutex);
6624 mlx5_ib_unbind_slave_port(mpi->ibdev, mpi);
6625 list_del(&mpi->list);
6626 mutex_unlock(&mlx5_ib_multiport_mutex);
6631 __mlx5_ib_remove(dev, dev->profile, MLX5_IB_STAGE_MAX);
6633 ib_dealloc_device((struct ib_device *)dev);
6636 static struct mlx5_interface mlx5_ib_interface = {
6638 .remove = mlx5_ib_remove,
6639 .protocol = MLX5_INTERFACE_PROTOCOL_IB,
6642 unsigned long mlx5_ib_get_xlt_emergency_page(void)
6644 mutex_lock(&xlt_emergency_page_mutex);
6645 return xlt_emergency_page;
6648 void mlx5_ib_put_xlt_emergency_page(void)
6650 mutex_unlock(&xlt_emergency_page_mutex);
6653 static int __init mlx5_ib_init(void)
6657 xlt_emergency_page = __get_free_page(GFP_KERNEL);
6658 if (!xlt_emergency_page)
6661 mutex_init(&xlt_emergency_page_mutex);
6663 mlx5_ib_event_wq = alloc_ordered_workqueue("mlx5_ib_event_wq", 0);
6664 if (!mlx5_ib_event_wq) {
6665 free_page(xlt_emergency_page);
6671 err = mlx5_register_interface(&mlx5_ib_interface);
6676 static void __exit mlx5_ib_cleanup(void)
6678 mlx5_unregister_interface(&mlx5_ib_interface);
6679 destroy_workqueue(mlx5_ib_event_wq);
6680 mutex_destroy(&xlt_emergency_page_mutex);
6681 free_page(xlt_emergency_page);
6684 module_init(mlx5_ib_init);
6685 module_exit(mlx5_ib_cleanup);