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"
76 MODULE_AUTHOR("Eli Cohen <eli@mellanox.com>");
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->pdev->dev) {
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_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 mlx5_query_port_roce(struct ib_device *device, u8 port_num,
393 struct ib_port_attr *props)
395 struct mlx5_ib_dev *dev = to_mdev(device);
396 struct mlx5_core_dev *mdev;
397 struct net_device *ndev, *upper;
398 enum ib_mtu ndev_ib_mtu;
399 bool put_mdev = true;
405 mdev = mlx5_ib_get_native_port_mdev(dev, port_num, &mdev_port_num);
407 /* This means the port isn't affiliated yet. Get the
408 * info for the master port instead.
416 /* Possible bad flows are checked before filling out props so in case
417 * of an error it will still be zeroed out.
419 err = mlx5_query_port_eth_proto_oper(mdev, ð_prot_oper,
424 props->active_width = IB_WIDTH_4X;
425 props->active_speed = IB_SPEED_QDR;
427 translate_eth_proto_oper(eth_prot_oper, &props->active_speed,
428 &props->active_width);
430 props->port_cap_flags |= IB_PORT_CM_SUP;
431 props->ip_gids = true;
433 props->gid_tbl_len = MLX5_CAP_ROCE(dev->mdev,
434 roce_address_table_size);
435 props->max_mtu = IB_MTU_4096;
436 props->max_msg_sz = 1 << MLX5_CAP_GEN(dev->mdev, log_max_msg);
437 props->pkey_tbl_len = 1;
438 props->state = IB_PORT_DOWN;
439 props->phys_state = 3;
441 mlx5_query_nic_vport_qkey_viol_cntr(mdev, &qkey_viol_cntr);
442 props->qkey_viol_cntr = qkey_viol_cntr;
444 /* If this is a stub query for an unaffiliated port stop here */
448 ndev = mlx5_ib_get_netdev(device, port_num);
452 if (mlx5_lag_is_active(dev->mdev)) {
454 upper = netdev_master_upper_dev_get_rcu(ndev);
463 if (netif_running(ndev) && netif_carrier_ok(ndev)) {
464 props->state = IB_PORT_ACTIVE;
465 props->phys_state = 5;
468 ndev_ib_mtu = iboe_get_mtu(ndev->mtu);
472 props->active_mtu = min(props->max_mtu, ndev_ib_mtu);
475 mlx5_ib_put_native_port_mdev(dev, port_num);
479 static int set_roce_addr(struct mlx5_ib_dev *dev, u8 port_num,
480 unsigned int index, const union ib_gid *gid,
481 const struct ib_gid_attr *attr)
483 enum ib_gid_type gid_type = IB_GID_TYPE_IB;
491 gid_type = attr->gid_type;
492 ether_addr_copy(mac, attr->ndev->dev_addr);
494 if (is_vlan_dev(attr->ndev)) {
496 vlan_id = vlan_dev_vlan_id(attr->ndev);
502 roce_version = MLX5_ROCE_VERSION_1;
504 case IB_GID_TYPE_ROCE_UDP_ENCAP:
505 roce_version = MLX5_ROCE_VERSION_2;
506 if (ipv6_addr_v4mapped((void *)gid))
507 roce_l3_type = MLX5_ROCE_L3_TYPE_IPV4;
509 roce_l3_type = MLX5_ROCE_L3_TYPE_IPV6;
513 mlx5_ib_warn(dev, "Unexpected GID type %u\n", gid_type);
516 return mlx5_core_roce_gid_set(dev->mdev, index, roce_version,
517 roce_l3_type, gid->raw, mac, vlan,
521 static int mlx5_ib_add_gid(const struct ib_gid_attr *attr,
522 __always_unused void **context)
524 return set_roce_addr(to_mdev(attr->device), attr->port_num,
525 attr->index, &attr->gid, attr);
528 static int mlx5_ib_del_gid(const struct ib_gid_attr *attr,
529 __always_unused void **context)
531 return set_roce_addr(to_mdev(attr->device), attr->port_num,
532 attr->index, NULL, NULL);
535 __be16 mlx5_get_roce_udp_sport(struct mlx5_ib_dev *dev,
536 const struct ib_gid_attr *attr)
538 if (attr->gid_type != IB_GID_TYPE_ROCE_UDP_ENCAP)
541 return cpu_to_be16(MLX5_CAP_ROCE(dev->mdev, r_roce_min_src_udp_port));
544 static int mlx5_use_mad_ifc(struct mlx5_ib_dev *dev)
546 if (MLX5_CAP_GEN(dev->mdev, port_type) == MLX5_CAP_PORT_TYPE_IB)
547 return !MLX5_CAP_GEN(dev->mdev, ib_virt);
552 MLX5_VPORT_ACCESS_METHOD_MAD,
553 MLX5_VPORT_ACCESS_METHOD_HCA,
554 MLX5_VPORT_ACCESS_METHOD_NIC,
557 static int mlx5_get_vport_access_method(struct ib_device *ibdev)
559 if (mlx5_use_mad_ifc(to_mdev(ibdev)))
560 return MLX5_VPORT_ACCESS_METHOD_MAD;
562 if (mlx5_ib_port_link_layer(ibdev, 1) ==
563 IB_LINK_LAYER_ETHERNET)
564 return MLX5_VPORT_ACCESS_METHOD_NIC;
566 return MLX5_VPORT_ACCESS_METHOD_HCA;
569 static void get_atomic_caps(struct mlx5_ib_dev *dev,
571 struct ib_device_attr *props)
574 u8 atomic_operations = MLX5_CAP_ATOMIC(dev->mdev, atomic_operations);
575 u8 atomic_req_8B_endianness_mode =
576 MLX5_CAP_ATOMIC(dev->mdev, atomic_req_8B_endianness_mode);
578 /* Check if HW supports 8 bytes standard atomic operations and capable
579 * of host endianness respond
581 tmp = MLX5_ATOMIC_OPS_CMP_SWAP | MLX5_ATOMIC_OPS_FETCH_ADD;
582 if (((atomic_operations & tmp) == tmp) &&
583 (atomic_size_qp & MLX5_ATOMIC_SIZE_QP_8BYTES) &&
584 (atomic_req_8B_endianness_mode)) {
585 props->atomic_cap = IB_ATOMIC_HCA;
587 props->atomic_cap = IB_ATOMIC_NONE;
591 static void get_atomic_caps_qp(struct mlx5_ib_dev *dev,
592 struct ib_device_attr *props)
594 u8 atomic_size_qp = MLX5_CAP_ATOMIC(dev->mdev, atomic_size_qp);
596 get_atomic_caps(dev, atomic_size_qp, props);
599 static void get_atomic_caps_dc(struct mlx5_ib_dev *dev,
600 struct ib_device_attr *props)
602 u8 atomic_size_qp = MLX5_CAP_ATOMIC(dev->mdev, atomic_size_dc);
604 get_atomic_caps(dev, atomic_size_qp, props);
607 bool mlx5_ib_dc_atomic_is_supported(struct mlx5_ib_dev *dev)
609 struct ib_device_attr props = {};
611 get_atomic_caps_dc(dev, &props);
612 return (props.atomic_cap == IB_ATOMIC_HCA) ? true : false;
614 static int mlx5_query_system_image_guid(struct ib_device *ibdev,
615 __be64 *sys_image_guid)
617 struct mlx5_ib_dev *dev = to_mdev(ibdev);
618 struct mlx5_core_dev *mdev = dev->mdev;
622 switch (mlx5_get_vport_access_method(ibdev)) {
623 case MLX5_VPORT_ACCESS_METHOD_MAD:
624 return mlx5_query_mad_ifc_system_image_guid(ibdev,
627 case MLX5_VPORT_ACCESS_METHOD_HCA:
628 err = mlx5_query_hca_vport_system_image_guid(mdev, &tmp);
631 case MLX5_VPORT_ACCESS_METHOD_NIC:
632 err = mlx5_query_nic_vport_system_image_guid(mdev, &tmp);
640 *sys_image_guid = cpu_to_be64(tmp);
646 static int mlx5_query_max_pkeys(struct ib_device *ibdev,
649 struct mlx5_ib_dev *dev = to_mdev(ibdev);
650 struct mlx5_core_dev *mdev = dev->mdev;
652 switch (mlx5_get_vport_access_method(ibdev)) {
653 case MLX5_VPORT_ACCESS_METHOD_MAD:
654 return mlx5_query_mad_ifc_max_pkeys(ibdev, max_pkeys);
656 case MLX5_VPORT_ACCESS_METHOD_HCA:
657 case MLX5_VPORT_ACCESS_METHOD_NIC:
658 *max_pkeys = mlx5_to_sw_pkey_sz(MLX5_CAP_GEN(mdev,
667 static int mlx5_query_vendor_id(struct ib_device *ibdev,
670 struct mlx5_ib_dev *dev = to_mdev(ibdev);
672 switch (mlx5_get_vport_access_method(ibdev)) {
673 case MLX5_VPORT_ACCESS_METHOD_MAD:
674 return mlx5_query_mad_ifc_vendor_id(ibdev, vendor_id);
676 case MLX5_VPORT_ACCESS_METHOD_HCA:
677 case MLX5_VPORT_ACCESS_METHOD_NIC:
678 return mlx5_core_query_vendor_id(dev->mdev, vendor_id);
685 static int mlx5_query_node_guid(struct mlx5_ib_dev *dev,
691 switch (mlx5_get_vport_access_method(&dev->ib_dev)) {
692 case MLX5_VPORT_ACCESS_METHOD_MAD:
693 return mlx5_query_mad_ifc_node_guid(dev, node_guid);
695 case MLX5_VPORT_ACCESS_METHOD_HCA:
696 err = mlx5_query_hca_vport_node_guid(dev->mdev, &tmp);
699 case MLX5_VPORT_ACCESS_METHOD_NIC:
700 err = mlx5_query_nic_vport_node_guid(dev->mdev, &tmp);
708 *node_guid = cpu_to_be64(tmp);
713 struct mlx5_reg_node_desc {
714 u8 desc[IB_DEVICE_NODE_DESC_MAX];
717 static int mlx5_query_node_desc(struct mlx5_ib_dev *dev, char *node_desc)
719 struct mlx5_reg_node_desc in;
721 if (mlx5_use_mad_ifc(dev))
722 return mlx5_query_mad_ifc_node_desc(dev, node_desc);
724 memset(&in, 0, sizeof(in));
726 return mlx5_core_access_reg(dev->mdev, &in, sizeof(in), node_desc,
727 sizeof(struct mlx5_reg_node_desc),
728 MLX5_REG_NODE_DESC, 0, 0);
731 static int mlx5_ib_query_device(struct ib_device *ibdev,
732 struct ib_device_attr *props,
733 struct ib_udata *uhw)
735 struct mlx5_ib_dev *dev = to_mdev(ibdev);
736 struct mlx5_core_dev *mdev = dev->mdev;
741 u64 min_page_size = 1ull << MLX5_CAP_GEN(mdev, log_pg_sz);
742 bool raw_support = !mlx5_core_mp_enabled(mdev);
743 struct mlx5_ib_query_device_resp resp = {};
747 resp_len = sizeof(resp.comp_mask) + sizeof(resp.response_length);
748 if (uhw->outlen && uhw->outlen < resp_len)
751 resp.response_length = resp_len;
753 if (uhw->inlen && !ib_is_udata_cleared(uhw, 0, uhw->inlen))
756 memset(props, 0, sizeof(*props));
757 err = mlx5_query_system_image_guid(ibdev,
758 &props->sys_image_guid);
762 err = mlx5_query_max_pkeys(ibdev, &props->max_pkeys);
766 err = mlx5_query_vendor_id(ibdev, &props->vendor_id);
770 props->fw_ver = ((u64)fw_rev_maj(dev->mdev) << 32) |
771 (fw_rev_min(dev->mdev) << 16) |
772 fw_rev_sub(dev->mdev);
773 props->device_cap_flags = IB_DEVICE_CHANGE_PHY_PORT |
774 IB_DEVICE_PORT_ACTIVE_EVENT |
775 IB_DEVICE_SYS_IMAGE_GUID |
776 IB_DEVICE_RC_RNR_NAK_GEN;
778 if (MLX5_CAP_GEN(mdev, pkv))
779 props->device_cap_flags |= IB_DEVICE_BAD_PKEY_CNTR;
780 if (MLX5_CAP_GEN(mdev, qkv))
781 props->device_cap_flags |= IB_DEVICE_BAD_QKEY_CNTR;
782 if (MLX5_CAP_GEN(mdev, apm))
783 props->device_cap_flags |= IB_DEVICE_AUTO_PATH_MIG;
784 if (MLX5_CAP_GEN(mdev, xrc))
785 props->device_cap_flags |= IB_DEVICE_XRC;
786 if (MLX5_CAP_GEN(mdev, imaicl)) {
787 props->device_cap_flags |= IB_DEVICE_MEM_WINDOW |
788 IB_DEVICE_MEM_WINDOW_TYPE_2B;
789 props->max_mw = 1 << MLX5_CAP_GEN(mdev, log_max_mkey);
790 /* We support 'Gappy' memory registration too */
791 props->device_cap_flags |= IB_DEVICE_SG_GAPS_REG;
793 props->device_cap_flags |= IB_DEVICE_MEM_MGT_EXTENSIONS;
794 if (MLX5_CAP_GEN(mdev, sho)) {
795 props->device_cap_flags |= IB_DEVICE_SIGNATURE_HANDOVER;
796 /* At this stage no support for signature handover */
797 props->sig_prot_cap = IB_PROT_T10DIF_TYPE_1 |
798 IB_PROT_T10DIF_TYPE_2 |
799 IB_PROT_T10DIF_TYPE_3;
800 props->sig_guard_cap = IB_GUARD_T10DIF_CRC |
801 IB_GUARD_T10DIF_CSUM;
803 if (MLX5_CAP_GEN(mdev, block_lb_mc))
804 props->device_cap_flags |= IB_DEVICE_BLOCK_MULTICAST_LOOPBACK;
806 if (MLX5_CAP_GEN(dev->mdev, eth_net_offloads) && raw_support) {
807 if (MLX5_CAP_ETH(mdev, csum_cap)) {
808 /* Legacy bit to support old userspace libraries */
809 props->device_cap_flags |= IB_DEVICE_RAW_IP_CSUM;
810 props->raw_packet_caps |= IB_RAW_PACKET_CAP_IP_CSUM;
813 if (MLX5_CAP_ETH(dev->mdev, vlan_cap))
814 props->raw_packet_caps |=
815 IB_RAW_PACKET_CAP_CVLAN_STRIPPING;
817 if (field_avail(typeof(resp), tso_caps, uhw->outlen)) {
818 max_tso = MLX5_CAP_ETH(mdev, max_lso_cap);
820 resp.tso_caps.max_tso = 1 << max_tso;
821 resp.tso_caps.supported_qpts |=
822 1 << IB_QPT_RAW_PACKET;
823 resp.response_length += sizeof(resp.tso_caps);
827 if (field_avail(typeof(resp), rss_caps, uhw->outlen)) {
828 resp.rss_caps.rx_hash_function =
829 MLX5_RX_HASH_FUNC_TOEPLITZ;
830 resp.rss_caps.rx_hash_fields_mask =
831 MLX5_RX_HASH_SRC_IPV4 |
832 MLX5_RX_HASH_DST_IPV4 |
833 MLX5_RX_HASH_SRC_IPV6 |
834 MLX5_RX_HASH_DST_IPV6 |
835 MLX5_RX_HASH_SRC_PORT_TCP |
836 MLX5_RX_HASH_DST_PORT_TCP |
837 MLX5_RX_HASH_SRC_PORT_UDP |
838 MLX5_RX_HASH_DST_PORT_UDP |
840 if (mlx5_accel_ipsec_device_caps(dev->mdev) &
841 MLX5_ACCEL_IPSEC_CAP_DEVICE)
842 resp.rss_caps.rx_hash_fields_mask |=
843 MLX5_RX_HASH_IPSEC_SPI;
844 resp.response_length += sizeof(resp.rss_caps);
847 if (field_avail(typeof(resp), tso_caps, uhw->outlen))
848 resp.response_length += sizeof(resp.tso_caps);
849 if (field_avail(typeof(resp), rss_caps, uhw->outlen))
850 resp.response_length += sizeof(resp.rss_caps);
853 if (MLX5_CAP_GEN(mdev, ipoib_basic_offloads)) {
854 props->device_cap_flags |= IB_DEVICE_UD_IP_CSUM;
855 props->device_cap_flags |= IB_DEVICE_UD_TSO;
858 if (MLX5_CAP_GEN(dev->mdev, rq_delay_drop) &&
859 MLX5_CAP_GEN(dev->mdev, general_notification_event) &&
861 props->raw_packet_caps |= IB_RAW_PACKET_CAP_DELAY_DROP;
863 if (MLX5_CAP_GEN(mdev, ipoib_enhanced_offloads) &&
864 MLX5_CAP_IPOIB_ENHANCED(mdev, csum_cap))
865 props->device_cap_flags |= IB_DEVICE_UD_IP_CSUM;
867 if (MLX5_CAP_GEN(dev->mdev, eth_net_offloads) &&
868 MLX5_CAP_ETH(dev->mdev, scatter_fcs) &&
870 /* Legacy bit to support old userspace libraries */
871 props->device_cap_flags |= IB_DEVICE_RAW_SCATTER_FCS;
872 props->raw_packet_caps |= IB_RAW_PACKET_CAP_SCATTER_FCS;
875 if (MLX5_CAP_DEV_MEM(mdev, memic)) {
877 MLX5_CAP_DEV_MEM(mdev, max_memic_size);
880 if (mlx5_get_flow_namespace(dev->mdev, MLX5_FLOW_NAMESPACE_BYPASS))
881 props->device_cap_flags |= IB_DEVICE_MANAGED_FLOW_STEERING;
883 if (MLX5_CAP_GEN(mdev, end_pad))
884 props->device_cap_flags |= IB_DEVICE_PCI_WRITE_END_PADDING;
886 props->vendor_part_id = mdev->pdev->device;
887 props->hw_ver = mdev->pdev->revision;
889 props->max_mr_size = ~0ull;
890 props->page_size_cap = ~(min_page_size - 1);
891 props->max_qp = 1 << MLX5_CAP_GEN(mdev, log_max_qp);
892 props->max_qp_wr = 1 << MLX5_CAP_GEN(mdev, log_max_qp_sz);
893 max_rq_sg = MLX5_CAP_GEN(mdev, max_wqe_sz_rq) /
894 sizeof(struct mlx5_wqe_data_seg);
895 max_sq_desc = min_t(int, MLX5_CAP_GEN(mdev, max_wqe_sz_sq), 512);
896 max_sq_sg = (max_sq_desc - sizeof(struct mlx5_wqe_ctrl_seg) -
897 sizeof(struct mlx5_wqe_raddr_seg)) /
898 sizeof(struct mlx5_wqe_data_seg);
899 props->max_send_sge = max_sq_sg;
900 props->max_recv_sge = max_rq_sg;
901 props->max_sge_rd = MLX5_MAX_SGE_RD;
902 props->max_cq = 1 << MLX5_CAP_GEN(mdev, log_max_cq);
903 props->max_cqe = (1 << MLX5_CAP_GEN(mdev, log_max_cq_sz)) - 1;
904 props->max_mr = 1 << MLX5_CAP_GEN(mdev, log_max_mkey);
905 props->max_pd = 1 << MLX5_CAP_GEN(mdev, log_max_pd);
906 props->max_qp_rd_atom = 1 << MLX5_CAP_GEN(mdev, log_max_ra_req_qp);
907 props->max_qp_init_rd_atom = 1 << MLX5_CAP_GEN(mdev, log_max_ra_res_qp);
908 props->max_srq = 1 << MLX5_CAP_GEN(mdev, log_max_srq);
909 props->max_srq_wr = (1 << MLX5_CAP_GEN(mdev, log_max_srq_sz)) - 1;
910 props->local_ca_ack_delay = MLX5_CAP_GEN(mdev, local_ca_ack_delay);
911 props->max_res_rd_atom = props->max_qp_rd_atom * props->max_qp;
912 props->max_srq_sge = max_rq_sg - 1;
913 props->max_fast_reg_page_list_len =
914 1 << MLX5_CAP_GEN(mdev, log_max_klm_list_size);
915 get_atomic_caps_qp(dev, props);
916 props->masked_atomic_cap = IB_ATOMIC_NONE;
917 props->max_mcast_grp = 1 << MLX5_CAP_GEN(mdev, log_max_mcg);
918 props->max_mcast_qp_attach = MLX5_CAP_GEN(mdev, max_qp_mcg);
919 props->max_total_mcast_qp_attach = props->max_mcast_qp_attach *
920 props->max_mcast_grp;
921 props->max_map_per_fmr = INT_MAX; /* no limit in ConnectIB */
922 props->max_ah = INT_MAX;
923 props->hca_core_clock = MLX5_CAP_GEN(mdev, device_frequency_khz);
924 props->timestamp_mask = 0x7FFFFFFFFFFFFFFFULL;
926 #ifdef CONFIG_INFINIBAND_ON_DEMAND_PAGING
927 if (MLX5_CAP_GEN(mdev, pg))
928 props->device_cap_flags |= IB_DEVICE_ON_DEMAND_PAGING;
929 props->odp_caps = dev->odp_caps;
932 if (MLX5_CAP_GEN(mdev, cd))
933 props->device_cap_flags |= IB_DEVICE_CROSS_CHANNEL;
935 if (!mlx5_core_is_pf(mdev))
936 props->device_cap_flags |= IB_DEVICE_VIRTUAL_FUNCTION;
938 if (mlx5_ib_port_link_layer(ibdev, 1) ==
939 IB_LINK_LAYER_ETHERNET && raw_support) {
940 props->rss_caps.max_rwq_indirection_tables =
941 1 << MLX5_CAP_GEN(dev->mdev, log_max_rqt);
942 props->rss_caps.max_rwq_indirection_table_size =
943 1 << MLX5_CAP_GEN(dev->mdev, log_max_rqt_size);
944 props->rss_caps.supported_qpts = 1 << IB_QPT_RAW_PACKET;
945 props->max_wq_type_rq =
946 1 << MLX5_CAP_GEN(dev->mdev, log_max_rq);
949 if (MLX5_CAP_GEN(mdev, tag_matching)) {
950 props->tm_caps.max_rndv_hdr_size = MLX5_TM_MAX_RNDV_MSG_SIZE;
951 props->tm_caps.max_num_tags =
952 (1 << MLX5_CAP_GEN(mdev, log_tag_matching_list_sz)) - 1;
953 props->tm_caps.flags = IB_TM_CAP_RC;
954 props->tm_caps.max_ops =
955 1 << MLX5_CAP_GEN(mdev, log_max_qp_sz);
956 props->tm_caps.max_sge = MLX5_TM_MAX_SGE;
959 if (MLX5_CAP_GEN(dev->mdev, cq_moderation)) {
960 props->cq_caps.max_cq_moderation_count =
962 props->cq_caps.max_cq_moderation_period =
966 if (field_avail(typeof(resp), cqe_comp_caps, uhw->outlen)) {
967 resp.response_length += sizeof(resp.cqe_comp_caps);
969 if (MLX5_CAP_GEN(dev->mdev, cqe_compression)) {
970 resp.cqe_comp_caps.max_num =
971 MLX5_CAP_GEN(dev->mdev,
972 cqe_compression_max_num);
974 resp.cqe_comp_caps.supported_format =
975 MLX5_IB_CQE_RES_FORMAT_HASH |
976 MLX5_IB_CQE_RES_FORMAT_CSUM;
978 if (MLX5_CAP_GEN(dev->mdev, mini_cqe_resp_stride_index))
979 resp.cqe_comp_caps.supported_format |=
980 MLX5_IB_CQE_RES_FORMAT_CSUM_STRIDX;
984 if (field_avail(typeof(resp), packet_pacing_caps, uhw->outlen) &&
986 if (MLX5_CAP_QOS(mdev, packet_pacing) &&
987 MLX5_CAP_GEN(mdev, qos)) {
988 resp.packet_pacing_caps.qp_rate_limit_max =
989 MLX5_CAP_QOS(mdev, packet_pacing_max_rate);
990 resp.packet_pacing_caps.qp_rate_limit_min =
991 MLX5_CAP_QOS(mdev, packet_pacing_min_rate);
992 resp.packet_pacing_caps.supported_qpts |=
993 1 << IB_QPT_RAW_PACKET;
994 if (MLX5_CAP_QOS(mdev, packet_pacing_burst_bound) &&
995 MLX5_CAP_QOS(mdev, packet_pacing_typical_size))
996 resp.packet_pacing_caps.cap_flags |=
997 MLX5_IB_PP_SUPPORT_BURST;
999 resp.response_length += sizeof(resp.packet_pacing_caps);
1002 if (field_avail(typeof(resp), mlx5_ib_support_multi_pkt_send_wqes,
1004 if (MLX5_CAP_ETH(mdev, multi_pkt_send_wqe))
1005 resp.mlx5_ib_support_multi_pkt_send_wqes =
1008 if (MLX5_CAP_ETH(mdev, enhanced_multi_pkt_send_wqe))
1009 resp.mlx5_ib_support_multi_pkt_send_wqes |=
1010 MLX5_IB_SUPPORT_EMPW;
1012 resp.response_length +=
1013 sizeof(resp.mlx5_ib_support_multi_pkt_send_wqes);
1016 if (field_avail(typeof(resp), flags, uhw->outlen)) {
1017 resp.response_length += sizeof(resp.flags);
1019 if (MLX5_CAP_GEN(mdev, cqe_compression_128))
1021 MLX5_IB_QUERY_DEV_RESP_FLAGS_CQE_128B_COMP;
1023 if (MLX5_CAP_GEN(mdev, cqe_128_always))
1024 resp.flags |= MLX5_IB_QUERY_DEV_RESP_FLAGS_CQE_128B_PAD;
1025 if (MLX5_CAP_GEN(mdev, qp_packet_based))
1027 MLX5_IB_QUERY_DEV_RESP_PACKET_BASED_CREDIT_MODE;
1030 if (field_avail(typeof(resp), sw_parsing_caps,
1032 resp.response_length += sizeof(resp.sw_parsing_caps);
1033 if (MLX5_CAP_ETH(mdev, swp)) {
1034 resp.sw_parsing_caps.sw_parsing_offloads |=
1037 if (MLX5_CAP_ETH(mdev, swp_csum))
1038 resp.sw_parsing_caps.sw_parsing_offloads |=
1039 MLX5_IB_SW_PARSING_CSUM;
1041 if (MLX5_CAP_ETH(mdev, swp_lso))
1042 resp.sw_parsing_caps.sw_parsing_offloads |=
1043 MLX5_IB_SW_PARSING_LSO;
1045 if (resp.sw_parsing_caps.sw_parsing_offloads)
1046 resp.sw_parsing_caps.supported_qpts =
1047 BIT(IB_QPT_RAW_PACKET);
1051 if (field_avail(typeof(resp), striding_rq_caps, uhw->outlen) &&
1053 resp.response_length += sizeof(resp.striding_rq_caps);
1054 if (MLX5_CAP_GEN(mdev, striding_rq)) {
1055 resp.striding_rq_caps.min_single_stride_log_num_of_bytes =
1056 MLX5_MIN_SINGLE_STRIDE_LOG_NUM_BYTES;
1057 resp.striding_rq_caps.max_single_stride_log_num_of_bytes =
1058 MLX5_MAX_SINGLE_STRIDE_LOG_NUM_BYTES;
1059 resp.striding_rq_caps.min_single_wqe_log_num_of_strides =
1060 MLX5_MIN_SINGLE_WQE_LOG_NUM_STRIDES;
1061 resp.striding_rq_caps.max_single_wqe_log_num_of_strides =
1062 MLX5_MAX_SINGLE_WQE_LOG_NUM_STRIDES;
1063 resp.striding_rq_caps.supported_qpts =
1064 BIT(IB_QPT_RAW_PACKET);
1068 if (field_avail(typeof(resp), tunnel_offloads_caps,
1070 resp.response_length += sizeof(resp.tunnel_offloads_caps);
1071 if (MLX5_CAP_ETH(mdev, tunnel_stateless_vxlan))
1072 resp.tunnel_offloads_caps |=
1073 MLX5_IB_TUNNELED_OFFLOADS_VXLAN;
1074 if (MLX5_CAP_ETH(mdev, tunnel_stateless_geneve_rx))
1075 resp.tunnel_offloads_caps |=
1076 MLX5_IB_TUNNELED_OFFLOADS_GENEVE;
1077 if (MLX5_CAP_ETH(mdev, tunnel_stateless_gre))
1078 resp.tunnel_offloads_caps |=
1079 MLX5_IB_TUNNELED_OFFLOADS_GRE;
1080 if (MLX5_CAP_GEN(mdev, flex_parser_protocols) &
1081 MLX5_FLEX_PROTO_CW_MPLS_GRE)
1082 resp.tunnel_offloads_caps |=
1083 MLX5_IB_TUNNELED_OFFLOADS_MPLS_GRE;
1084 if (MLX5_CAP_GEN(mdev, flex_parser_protocols) &
1085 MLX5_FLEX_PROTO_CW_MPLS_UDP)
1086 resp.tunnel_offloads_caps |=
1087 MLX5_IB_TUNNELED_OFFLOADS_MPLS_UDP;
1091 err = ib_copy_to_udata(uhw, &resp, resp.response_length);
1100 enum mlx5_ib_width {
1101 MLX5_IB_WIDTH_1X = 1 << 0,
1102 MLX5_IB_WIDTH_2X = 1 << 1,
1103 MLX5_IB_WIDTH_4X = 1 << 2,
1104 MLX5_IB_WIDTH_8X = 1 << 3,
1105 MLX5_IB_WIDTH_12X = 1 << 4
1108 static void translate_active_width(struct ib_device *ibdev, u8 active_width,
1111 struct mlx5_ib_dev *dev = to_mdev(ibdev);
1113 if (active_width & MLX5_IB_WIDTH_1X)
1114 *ib_width = IB_WIDTH_1X;
1115 else if (active_width & MLX5_IB_WIDTH_2X)
1116 *ib_width = IB_WIDTH_2X;
1117 else if (active_width & MLX5_IB_WIDTH_4X)
1118 *ib_width = IB_WIDTH_4X;
1119 else if (active_width & MLX5_IB_WIDTH_8X)
1120 *ib_width = IB_WIDTH_8X;
1121 else if (active_width & MLX5_IB_WIDTH_12X)
1122 *ib_width = IB_WIDTH_12X;
1124 mlx5_ib_dbg(dev, "Invalid active_width %d, setting width to default value: 4x\n",
1126 *ib_width = IB_WIDTH_4X;
1132 static int mlx5_mtu_to_ib_mtu(int mtu)
1137 case 1024: return 3;
1138 case 2048: return 4;
1139 case 4096: return 5;
1141 pr_warn("invalid mtu\n");
1146 enum ib_max_vl_num {
1148 __IB_MAX_VL_0_1 = 2,
1149 __IB_MAX_VL_0_3 = 3,
1150 __IB_MAX_VL_0_7 = 4,
1151 __IB_MAX_VL_0_14 = 5,
1154 enum mlx5_vl_hw_cap {
1163 MLX5_VL_HW_0_14 = 15
1166 static int translate_max_vl_num(struct ib_device *ibdev, u8 vl_hw_cap,
1169 switch (vl_hw_cap) {
1171 *max_vl_num = __IB_MAX_VL_0;
1173 case MLX5_VL_HW_0_1:
1174 *max_vl_num = __IB_MAX_VL_0_1;
1176 case MLX5_VL_HW_0_3:
1177 *max_vl_num = __IB_MAX_VL_0_3;
1179 case MLX5_VL_HW_0_7:
1180 *max_vl_num = __IB_MAX_VL_0_7;
1182 case MLX5_VL_HW_0_14:
1183 *max_vl_num = __IB_MAX_VL_0_14;
1193 static int mlx5_query_hca_port(struct ib_device *ibdev, u8 port,
1194 struct ib_port_attr *props)
1196 struct mlx5_ib_dev *dev = to_mdev(ibdev);
1197 struct mlx5_core_dev *mdev = dev->mdev;
1198 struct mlx5_hca_vport_context *rep;
1202 u8 ib_link_width_oper;
1205 rep = kzalloc(sizeof(*rep), GFP_KERNEL);
1211 /* props being zeroed by the caller, avoid zeroing it here */
1213 err = mlx5_query_hca_vport_context(mdev, 0, port, 0, rep);
1217 props->lid = rep->lid;
1218 props->lmc = rep->lmc;
1219 props->sm_lid = rep->sm_lid;
1220 props->sm_sl = rep->sm_sl;
1221 props->state = rep->vport_state;
1222 props->phys_state = rep->port_physical_state;
1223 props->port_cap_flags = rep->cap_mask1;
1224 props->gid_tbl_len = mlx5_get_gid_table_len(MLX5_CAP_GEN(mdev, gid_table_size));
1225 props->max_msg_sz = 1 << MLX5_CAP_GEN(mdev, log_max_msg);
1226 props->pkey_tbl_len = mlx5_to_sw_pkey_sz(MLX5_CAP_GEN(mdev, pkey_table_size));
1227 props->bad_pkey_cntr = rep->pkey_violation_counter;
1228 props->qkey_viol_cntr = rep->qkey_violation_counter;
1229 props->subnet_timeout = rep->subnet_timeout;
1230 props->init_type_reply = rep->init_type_reply;
1232 if (props->port_cap_flags & IB_PORT_CAP_MASK2_SUP)
1233 props->port_cap_flags2 = rep->cap_mask2;
1235 err = mlx5_query_port_link_width_oper(mdev, &ib_link_width_oper, port);
1239 translate_active_width(ibdev, ib_link_width_oper, &props->active_width);
1241 err = mlx5_query_port_ib_proto_oper(mdev, &props->active_speed, port);
1245 mlx5_query_port_max_mtu(mdev, &max_mtu, port);
1247 props->max_mtu = mlx5_mtu_to_ib_mtu(max_mtu);
1249 mlx5_query_port_oper_mtu(mdev, &oper_mtu, port);
1251 props->active_mtu = mlx5_mtu_to_ib_mtu(oper_mtu);
1253 err = mlx5_query_port_vl_hw_cap(mdev, &vl_hw_cap, port);
1257 err = translate_max_vl_num(ibdev, vl_hw_cap,
1258 &props->max_vl_num);
1264 int mlx5_ib_query_port(struct ib_device *ibdev, u8 port,
1265 struct ib_port_attr *props)
1270 switch (mlx5_get_vport_access_method(ibdev)) {
1271 case MLX5_VPORT_ACCESS_METHOD_MAD:
1272 ret = mlx5_query_mad_ifc_port(ibdev, port, props);
1275 case MLX5_VPORT_ACCESS_METHOD_HCA:
1276 ret = mlx5_query_hca_port(ibdev, port, props);
1279 case MLX5_VPORT_ACCESS_METHOD_NIC:
1280 ret = mlx5_query_port_roce(ibdev, port, props);
1287 if (!ret && props) {
1288 struct mlx5_ib_dev *dev = to_mdev(ibdev);
1289 struct mlx5_core_dev *mdev;
1290 bool put_mdev = true;
1292 mdev = mlx5_ib_get_native_port_mdev(dev, port, NULL);
1294 /* If the port isn't affiliated yet query the master.
1295 * The master and slave will have the same values.
1301 count = mlx5_core_reserved_gids_count(mdev);
1303 mlx5_ib_put_native_port_mdev(dev, port);
1304 props->gid_tbl_len -= count;
1309 static int mlx5_ib_rep_query_port(struct ib_device *ibdev, u8 port,
1310 struct ib_port_attr *props)
1314 /* Only link layer == ethernet is valid for representors */
1315 ret = mlx5_query_port_roce(ibdev, port, props);
1319 /* We don't support GIDS */
1320 props->gid_tbl_len = 0;
1325 static int mlx5_ib_query_gid(struct ib_device *ibdev, u8 port, int index,
1328 struct mlx5_ib_dev *dev = to_mdev(ibdev);
1329 struct mlx5_core_dev *mdev = dev->mdev;
1331 switch (mlx5_get_vport_access_method(ibdev)) {
1332 case MLX5_VPORT_ACCESS_METHOD_MAD:
1333 return mlx5_query_mad_ifc_gids(ibdev, port, index, gid);
1335 case MLX5_VPORT_ACCESS_METHOD_HCA:
1336 return mlx5_query_hca_vport_gid(mdev, 0, port, 0, index, gid);
1344 static int mlx5_query_hca_nic_pkey(struct ib_device *ibdev, u8 port,
1345 u16 index, u16 *pkey)
1347 struct mlx5_ib_dev *dev = to_mdev(ibdev);
1348 struct mlx5_core_dev *mdev;
1349 bool put_mdev = true;
1353 mdev = mlx5_ib_get_native_port_mdev(dev, port, &mdev_port_num);
1355 /* The port isn't affiliated yet, get the PKey from the master
1356 * port. For RoCE the PKey tables will be the same.
1363 err = mlx5_query_hca_vport_pkey(mdev, 0, mdev_port_num, 0,
1366 mlx5_ib_put_native_port_mdev(dev, port);
1371 static int mlx5_ib_query_pkey(struct ib_device *ibdev, u8 port, u16 index,
1374 switch (mlx5_get_vport_access_method(ibdev)) {
1375 case MLX5_VPORT_ACCESS_METHOD_MAD:
1376 return mlx5_query_mad_ifc_pkey(ibdev, port, index, pkey);
1378 case MLX5_VPORT_ACCESS_METHOD_HCA:
1379 case MLX5_VPORT_ACCESS_METHOD_NIC:
1380 return mlx5_query_hca_nic_pkey(ibdev, port, index, pkey);
1386 static int mlx5_ib_modify_device(struct ib_device *ibdev, int mask,
1387 struct ib_device_modify *props)
1389 struct mlx5_ib_dev *dev = to_mdev(ibdev);
1390 struct mlx5_reg_node_desc in;
1391 struct mlx5_reg_node_desc out;
1394 if (mask & ~IB_DEVICE_MODIFY_NODE_DESC)
1397 if (!(mask & IB_DEVICE_MODIFY_NODE_DESC))
1401 * If possible, pass node desc to FW, so it can generate
1402 * a 144 trap. If cmd fails, just ignore.
1404 memcpy(&in, props->node_desc, IB_DEVICE_NODE_DESC_MAX);
1405 err = mlx5_core_access_reg(dev->mdev, &in, sizeof(in), &out,
1406 sizeof(out), MLX5_REG_NODE_DESC, 0, 1);
1410 memcpy(ibdev->node_desc, props->node_desc, IB_DEVICE_NODE_DESC_MAX);
1415 static int set_port_caps_atomic(struct mlx5_ib_dev *dev, u8 port_num, u32 mask,
1418 struct mlx5_hca_vport_context ctx = {};
1419 struct mlx5_core_dev *mdev;
1423 mdev = mlx5_ib_get_native_port_mdev(dev, port_num, &mdev_port_num);
1427 err = mlx5_query_hca_vport_context(mdev, 0, mdev_port_num, 0, &ctx);
1431 if (~ctx.cap_mask1_perm & mask) {
1432 mlx5_ib_warn(dev, "trying to change bitmask 0x%X but change supported 0x%X\n",
1433 mask, ctx.cap_mask1_perm);
1438 ctx.cap_mask1 = value;
1439 ctx.cap_mask1_perm = mask;
1440 err = mlx5_core_modify_hca_vport_context(mdev, 0, mdev_port_num,
1444 mlx5_ib_put_native_port_mdev(dev, port_num);
1449 static int mlx5_ib_modify_port(struct ib_device *ibdev, u8 port, int mask,
1450 struct ib_port_modify *props)
1452 struct mlx5_ib_dev *dev = to_mdev(ibdev);
1453 struct ib_port_attr attr;
1458 bool is_ib = (mlx5_ib_port_link_layer(ibdev, port) ==
1459 IB_LINK_LAYER_INFINIBAND);
1461 /* CM layer calls ib_modify_port() regardless of the link layer. For
1462 * Ethernet ports, qkey violation and Port capabilities are meaningless.
1467 if (MLX5_CAP_GEN(dev->mdev, ib_virt) && is_ib) {
1468 change_mask = props->clr_port_cap_mask | props->set_port_cap_mask;
1469 value = ~props->clr_port_cap_mask | props->set_port_cap_mask;
1470 return set_port_caps_atomic(dev, port, change_mask, value);
1473 mutex_lock(&dev->cap_mask_mutex);
1475 err = ib_query_port(ibdev, port, &attr);
1479 tmp = (attr.port_cap_flags | props->set_port_cap_mask) &
1480 ~props->clr_port_cap_mask;
1482 err = mlx5_set_port_caps(dev->mdev, port, tmp);
1485 mutex_unlock(&dev->cap_mask_mutex);
1489 static void print_lib_caps(struct mlx5_ib_dev *dev, u64 caps)
1491 mlx5_ib_dbg(dev, "MLX5_LIB_CAP_4K_UAR = %s\n",
1492 caps & MLX5_LIB_CAP_4K_UAR ? "y" : "n");
1495 static u16 calc_dynamic_bfregs(int uars_per_sys_page)
1497 /* Large page with non 4k uar support might limit the dynamic size */
1498 if (uars_per_sys_page == 1 && PAGE_SIZE > 4096)
1499 return MLX5_MIN_DYN_BFREGS;
1501 return MLX5_MAX_DYN_BFREGS;
1504 static int calc_total_bfregs(struct mlx5_ib_dev *dev, bool lib_uar_4k,
1505 struct mlx5_ib_alloc_ucontext_req_v2 *req,
1506 struct mlx5_bfreg_info *bfregi)
1508 int uars_per_sys_page;
1509 int bfregs_per_sys_page;
1510 int ref_bfregs = req->total_num_bfregs;
1512 if (req->total_num_bfregs == 0)
1515 BUILD_BUG_ON(MLX5_MAX_BFREGS % MLX5_NON_FP_BFREGS_IN_PAGE);
1516 BUILD_BUG_ON(MLX5_MAX_BFREGS < MLX5_NON_FP_BFREGS_IN_PAGE);
1518 if (req->total_num_bfregs > MLX5_MAX_BFREGS)
1521 uars_per_sys_page = get_uars_per_sys_page(dev, lib_uar_4k);
1522 bfregs_per_sys_page = uars_per_sys_page * MLX5_NON_FP_BFREGS_PER_UAR;
1523 /* This holds the required static allocation asked by the user */
1524 req->total_num_bfregs = ALIGN(req->total_num_bfregs, bfregs_per_sys_page);
1525 if (req->num_low_latency_bfregs > req->total_num_bfregs - 1)
1528 bfregi->num_static_sys_pages = req->total_num_bfregs / bfregs_per_sys_page;
1529 bfregi->num_dyn_bfregs = ALIGN(calc_dynamic_bfregs(uars_per_sys_page), bfregs_per_sys_page);
1530 bfregi->total_num_bfregs = req->total_num_bfregs + bfregi->num_dyn_bfregs;
1531 bfregi->num_sys_pages = bfregi->total_num_bfregs / bfregs_per_sys_page;
1533 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",
1534 MLX5_CAP_GEN(dev->mdev, uar_4k) ? "yes" : "no",
1535 lib_uar_4k ? "yes" : "no", ref_bfregs,
1536 req->total_num_bfregs, bfregi->total_num_bfregs,
1537 bfregi->num_sys_pages);
1542 static int allocate_uars(struct mlx5_ib_dev *dev, struct mlx5_ib_ucontext *context)
1544 struct mlx5_bfreg_info *bfregi;
1548 bfregi = &context->bfregi;
1549 for (i = 0; i < bfregi->num_static_sys_pages; i++) {
1550 err = mlx5_cmd_alloc_uar(dev->mdev, &bfregi->sys_pages[i]);
1554 mlx5_ib_dbg(dev, "allocated uar %d\n", bfregi->sys_pages[i]);
1557 for (i = bfregi->num_static_sys_pages; i < bfregi->num_sys_pages; i++)
1558 bfregi->sys_pages[i] = MLX5_IB_INVALID_UAR_INDEX;
1563 for (--i; i >= 0; i--)
1564 if (mlx5_cmd_free_uar(dev->mdev, bfregi->sys_pages[i]))
1565 mlx5_ib_warn(dev, "failed to free uar %d\n", i);
1570 static void deallocate_uars(struct mlx5_ib_dev *dev,
1571 struct mlx5_ib_ucontext *context)
1573 struct mlx5_bfreg_info *bfregi;
1576 bfregi = &context->bfregi;
1577 for (i = 0; i < bfregi->num_sys_pages; i++)
1578 if (i < bfregi->num_static_sys_pages ||
1579 bfregi->sys_pages[i] != MLX5_IB_INVALID_UAR_INDEX)
1580 mlx5_cmd_free_uar(dev->mdev, bfregi->sys_pages[i]);
1583 int mlx5_ib_enable_lb(struct mlx5_ib_dev *dev, bool td, bool qp)
1587 mutex_lock(&dev->lb.mutex);
1593 if (dev->lb.user_td == 2 ||
1595 if (!dev->lb.enabled) {
1596 err = mlx5_nic_vport_update_local_lb(dev->mdev, true);
1597 dev->lb.enabled = true;
1601 mutex_unlock(&dev->lb.mutex);
1606 void mlx5_ib_disable_lb(struct mlx5_ib_dev *dev, bool td, bool qp)
1608 mutex_lock(&dev->lb.mutex);
1614 if (dev->lb.user_td == 1 &&
1616 if (dev->lb.enabled) {
1617 mlx5_nic_vport_update_local_lb(dev->mdev, false);
1618 dev->lb.enabled = false;
1622 mutex_unlock(&dev->lb.mutex);
1625 static int mlx5_ib_alloc_transport_domain(struct mlx5_ib_dev *dev, u32 *tdn,
1630 if (!MLX5_CAP_GEN(dev->mdev, log_max_transport_domain))
1633 err = mlx5_cmd_alloc_transport_domain(dev->mdev, tdn, uid);
1637 if ((MLX5_CAP_GEN(dev->mdev, port_type) != MLX5_CAP_PORT_TYPE_ETH) ||
1638 (!MLX5_CAP_GEN(dev->mdev, disable_local_lb_uc) &&
1639 !MLX5_CAP_GEN(dev->mdev, disable_local_lb_mc)))
1642 return mlx5_ib_enable_lb(dev, true, false);
1645 static void mlx5_ib_dealloc_transport_domain(struct mlx5_ib_dev *dev, u32 tdn,
1648 if (!MLX5_CAP_GEN(dev->mdev, log_max_transport_domain))
1651 mlx5_cmd_dealloc_transport_domain(dev->mdev, tdn, uid);
1653 if ((MLX5_CAP_GEN(dev->mdev, port_type) != MLX5_CAP_PORT_TYPE_ETH) ||
1654 (!MLX5_CAP_GEN(dev->mdev, disable_local_lb_uc) &&
1655 !MLX5_CAP_GEN(dev->mdev, disable_local_lb_mc)))
1658 mlx5_ib_disable_lb(dev, true, false);
1661 static struct ib_ucontext *mlx5_ib_alloc_ucontext(struct ib_device *ibdev,
1662 struct ib_udata *udata)
1664 struct mlx5_ib_dev *dev = to_mdev(ibdev);
1665 struct mlx5_ib_alloc_ucontext_req_v2 req = {};
1666 struct mlx5_ib_alloc_ucontext_resp resp = {};
1667 struct mlx5_core_dev *mdev = dev->mdev;
1668 struct mlx5_ib_ucontext *context;
1669 struct mlx5_bfreg_info *bfregi;
1672 size_t min_req_v2 = offsetof(struct mlx5_ib_alloc_ucontext_req_v2,
1677 if (!dev->ib_active)
1678 return ERR_PTR(-EAGAIN);
1680 if (udata->inlen == sizeof(struct mlx5_ib_alloc_ucontext_req))
1682 else if (udata->inlen >= min_req_v2)
1685 return ERR_PTR(-EINVAL);
1687 err = ib_copy_from_udata(&req, udata, min(udata->inlen, sizeof(req)));
1689 return ERR_PTR(err);
1691 if (req.flags & ~MLX5_IB_ALLOC_UCTX_DEVX)
1692 return ERR_PTR(-EOPNOTSUPP);
1694 if (req.comp_mask || req.reserved0 || req.reserved1 || req.reserved2)
1695 return ERR_PTR(-EOPNOTSUPP);
1697 req.total_num_bfregs = ALIGN(req.total_num_bfregs,
1698 MLX5_NON_FP_BFREGS_PER_UAR);
1699 if (req.num_low_latency_bfregs > req.total_num_bfregs - 1)
1700 return ERR_PTR(-EINVAL);
1702 resp.qp_tab_size = 1 << MLX5_CAP_GEN(dev->mdev, log_max_qp);
1703 if (mlx5_core_is_pf(dev->mdev) && MLX5_CAP_GEN(dev->mdev, bf))
1704 resp.bf_reg_size = 1 << MLX5_CAP_GEN(dev->mdev, log_bf_reg_size);
1705 resp.cache_line_size = cache_line_size();
1706 resp.max_sq_desc_sz = MLX5_CAP_GEN(dev->mdev, max_wqe_sz_sq);
1707 resp.max_rq_desc_sz = MLX5_CAP_GEN(dev->mdev, max_wqe_sz_rq);
1708 resp.max_send_wqebb = 1 << MLX5_CAP_GEN(dev->mdev, log_max_qp_sz);
1709 resp.max_recv_wr = 1 << MLX5_CAP_GEN(dev->mdev, log_max_qp_sz);
1710 resp.max_srq_recv_wr = 1 << MLX5_CAP_GEN(dev->mdev, log_max_srq_sz);
1711 resp.cqe_version = min_t(__u8,
1712 (__u8)MLX5_CAP_GEN(dev->mdev, cqe_version),
1713 req.max_cqe_version);
1714 resp.log_uar_size = MLX5_CAP_GEN(dev->mdev, uar_4k) ?
1715 MLX5_ADAPTER_PAGE_SHIFT : PAGE_SHIFT;
1716 resp.num_uars_per_page = MLX5_CAP_GEN(dev->mdev, uar_4k) ?
1717 MLX5_CAP_GEN(dev->mdev, num_of_uars_per_page) : 1;
1718 resp.response_length = min(offsetof(typeof(resp), response_length) +
1719 sizeof(resp.response_length), udata->outlen);
1721 if (mlx5_accel_ipsec_device_caps(dev->mdev) & MLX5_ACCEL_IPSEC_CAP_DEVICE) {
1722 if (mlx5_get_flow_namespace(dev->mdev, MLX5_FLOW_NAMESPACE_EGRESS))
1723 resp.flow_action_flags |= MLX5_USER_ALLOC_UCONTEXT_FLOW_ACTION_FLAGS_ESP_AES_GCM;
1724 if (mlx5_accel_ipsec_device_caps(dev->mdev) & MLX5_ACCEL_IPSEC_CAP_REQUIRED_METADATA)
1725 resp.flow_action_flags |= MLX5_USER_ALLOC_UCONTEXT_FLOW_ACTION_FLAGS_ESP_AES_GCM_REQ_METADATA;
1726 if (MLX5_CAP_FLOWTABLE(dev->mdev, flow_table_properties_nic_receive.ft_field_support.outer_esp_spi))
1727 resp.flow_action_flags |= MLX5_USER_ALLOC_UCONTEXT_FLOW_ACTION_FLAGS_ESP_AES_GCM_SPI_STEERING;
1728 if (mlx5_accel_ipsec_device_caps(dev->mdev) & MLX5_ACCEL_IPSEC_CAP_TX_IV_IS_ESN)
1729 resp.flow_action_flags |= MLX5_USER_ALLOC_UCONTEXT_FLOW_ACTION_FLAGS_ESP_AES_GCM_TX_IV_IS_ESN;
1730 /* MLX5_USER_ALLOC_UCONTEXT_FLOW_ACTION_FLAGS_ESP_AES_GCM_FULL_OFFLOAD is currently always 0 */
1733 context = kzalloc(sizeof(*context), GFP_KERNEL);
1735 return ERR_PTR(-ENOMEM);
1737 lib_uar_4k = req.lib_caps & MLX5_LIB_CAP_4K_UAR;
1738 bfregi = &context->bfregi;
1740 /* updates req->total_num_bfregs */
1741 err = calc_total_bfregs(dev, lib_uar_4k, &req, bfregi);
1745 mutex_init(&bfregi->lock);
1746 bfregi->lib_uar_4k = lib_uar_4k;
1747 bfregi->count = kcalloc(bfregi->total_num_bfregs, sizeof(*bfregi->count),
1749 if (!bfregi->count) {
1754 bfregi->sys_pages = kcalloc(bfregi->num_sys_pages,
1755 sizeof(*bfregi->sys_pages),
1757 if (!bfregi->sys_pages) {
1762 err = allocate_uars(dev, context);
1766 #ifdef CONFIG_INFINIBAND_ON_DEMAND_PAGING
1767 context->ibucontext.invalidate_range = &mlx5_ib_invalidate_range;
1770 if (req.flags & MLX5_IB_ALLOC_UCTX_DEVX) {
1771 err = mlx5_ib_devx_create(dev, true);
1774 context->devx_uid = err;
1777 err = mlx5_ib_alloc_transport_domain(dev, &context->tdn,
1782 if (MLX5_CAP_GEN(dev->mdev, dump_fill_mkey)) {
1783 err = mlx5_cmd_dump_fill_mkey(dev->mdev, &dump_fill_mkey);
1788 INIT_LIST_HEAD(&context->db_page_list);
1789 mutex_init(&context->db_page_mutex);
1791 resp.tot_bfregs = req.total_num_bfregs;
1792 resp.num_ports = dev->num_ports;
1794 if (field_avail(typeof(resp), cqe_version, udata->outlen))
1795 resp.response_length += sizeof(resp.cqe_version);
1797 if (field_avail(typeof(resp), cmds_supp_uhw, udata->outlen)) {
1798 resp.cmds_supp_uhw |= MLX5_USER_CMDS_SUPP_UHW_QUERY_DEVICE |
1799 MLX5_USER_CMDS_SUPP_UHW_CREATE_AH;
1800 resp.response_length += sizeof(resp.cmds_supp_uhw);
1803 if (field_avail(typeof(resp), eth_min_inline, udata->outlen)) {
1804 if (mlx5_ib_port_link_layer(ibdev, 1) == IB_LINK_LAYER_ETHERNET) {
1805 mlx5_query_min_inline(dev->mdev, &resp.eth_min_inline);
1806 resp.eth_min_inline++;
1808 resp.response_length += sizeof(resp.eth_min_inline);
1811 if (field_avail(typeof(resp), clock_info_versions, udata->outlen)) {
1812 if (mdev->clock_info)
1813 resp.clock_info_versions = BIT(MLX5_IB_CLOCK_INFO_V1);
1814 resp.response_length += sizeof(resp.clock_info_versions);
1818 * We don't want to expose information from the PCI bar that is located
1819 * after 4096 bytes, so if the arch only supports larger pages, let's
1820 * pretend we don't support reading the HCA's core clock. This is also
1821 * forced by mmap function.
1823 if (field_avail(typeof(resp), hca_core_clock_offset, udata->outlen)) {
1824 if (PAGE_SIZE <= 4096) {
1826 MLX5_IB_ALLOC_UCONTEXT_RESP_MASK_CORE_CLOCK_OFFSET;
1827 resp.hca_core_clock_offset =
1828 offsetof(struct mlx5_init_seg, internal_timer_h) % PAGE_SIZE;
1830 resp.response_length += sizeof(resp.hca_core_clock_offset);
1833 if (field_avail(typeof(resp), log_uar_size, udata->outlen))
1834 resp.response_length += sizeof(resp.log_uar_size);
1836 if (field_avail(typeof(resp), num_uars_per_page, udata->outlen))
1837 resp.response_length += sizeof(resp.num_uars_per_page);
1839 if (field_avail(typeof(resp), num_dyn_bfregs, udata->outlen)) {
1840 resp.num_dyn_bfregs = bfregi->num_dyn_bfregs;
1841 resp.response_length += sizeof(resp.num_dyn_bfregs);
1844 if (field_avail(typeof(resp), dump_fill_mkey, udata->outlen)) {
1845 if (MLX5_CAP_GEN(dev->mdev, dump_fill_mkey)) {
1846 resp.dump_fill_mkey = dump_fill_mkey;
1848 MLX5_IB_ALLOC_UCONTEXT_RESP_MASK_DUMP_FILL_MKEY;
1850 resp.response_length += sizeof(resp.dump_fill_mkey);
1853 err = ib_copy_to_udata(udata, &resp, resp.response_length);
1858 bfregi->num_low_latency_bfregs = req.num_low_latency_bfregs;
1859 context->cqe_version = resp.cqe_version;
1860 context->lib_caps = req.lib_caps;
1861 print_lib_caps(dev, context->lib_caps);
1863 if (mlx5_lag_is_active(dev->mdev)) {
1864 u8 port = mlx5_core_native_port_num(dev->mdev);
1866 atomic_set(&context->tx_port_affinity,
1868 1, &dev->roce[port].tx_port_affinity));
1871 return &context->ibucontext;
1874 mlx5_ib_dealloc_transport_domain(dev, context->tdn, context->devx_uid);
1876 if (req.flags & MLX5_IB_ALLOC_UCTX_DEVX)
1877 mlx5_ib_devx_destroy(dev, context->devx_uid);
1880 deallocate_uars(dev, context);
1883 kfree(bfregi->sys_pages);
1886 kfree(bfregi->count);
1891 return ERR_PTR(err);
1894 static int mlx5_ib_dealloc_ucontext(struct ib_ucontext *ibcontext)
1896 struct mlx5_ib_ucontext *context = to_mucontext(ibcontext);
1897 struct mlx5_ib_dev *dev = to_mdev(ibcontext->device);
1898 struct mlx5_bfreg_info *bfregi;
1900 #ifdef CONFIG_INFINIBAND_ON_DEMAND_PAGING
1901 /* All umem's must be destroyed before destroying the ucontext. */
1902 mutex_lock(&ibcontext->per_mm_list_lock);
1903 WARN_ON(!list_empty(&ibcontext->per_mm_list));
1904 mutex_unlock(&ibcontext->per_mm_list_lock);
1907 bfregi = &context->bfregi;
1908 mlx5_ib_dealloc_transport_domain(dev, context->tdn, context->devx_uid);
1910 if (context->devx_uid)
1911 mlx5_ib_devx_destroy(dev, context->devx_uid);
1913 deallocate_uars(dev, context);
1914 kfree(bfregi->sys_pages);
1915 kfree(bfregi->count);
1921 static phys_addr_t uar_index2pfn(struct mlx5_ib_dev *dev,
1924 int fw_uars_per_page;
1926 fw_uars_per_page = MLX5_CAP_GEN(dev->mdev, uar_4k) ? MLX5_UARS_IN_PAGE : 1;
1928 return (pci_resource_start(dev->mdev->pdev, 0) >> PAGE_SHIFT) + uar_idx / fw_uars_per_page;
1931 static int get_command(unsigned long offset)
1933 return (offset >> MLX5_IB_MMAP_CMD_SHIFT) & MLX5_IB_MMAP_CMD_MASK;
1936 static int get_arg(unsigned long offset)
1938 return offset & ((1 << MLX5_IB_MMAP_CMD_SHIFT) - 1);
1941 static int get_index(unsigned long offset)
1943 return get_arg(offset);
1946 /* Index resides in an extra byte to enable larger values than 255 */
1947 static int get_extended_index(unsigned long offset)
1949 return get_arg(offset) | ((offset >> 16) & 0xff) << 8;
1953 static void mlx5_ib_disassociate_ucontext(struct ib_ucontext *ibcontext)
1957 static inline char *mmap_cmd2str(enum mlx5_ib_mmap_cmd cmd)
1960 case MLX5_IB_MMAP_WC_PAGE:
1962 case MLX5_IB_MMAP_REGULAR_PAGE:
1963 return "best effort WC";
1964 case MLX5_IB_MMAP_NC_PAGE:
1966 case MLX5_IB_MMAP_DEVICE_MEM:
1967 return "Device Memory";
1973 static int mlx5_ib_mmap_clock_info_page(struct mlx5_ib_dev *dev,
1974 struct vm_area_struct *vma,
1975 struct mlx5_ib_ucontext *context)
1977 if (vma->vm_end - vma->vm_start != PAGE_SIZE)
1980 if (get_index(vma->vm_pgoff) != MLX5_IB_CLOCK_INFO_V1)
1983 if (vma->vm_flags & VM_WRITE)
1986 if (!dev->mdev->clock_info_page)
1989 return rdma_user_mmap_page(&context->ibucontext, vma,
1990 dev->mdev->clock_info_page, PAGE_SIZE);
1993 static int uar_mmap(struct mlx5_ib_dev *dev, enum mlx5_ib_mmap_cmd cmd,
1994 struct vm_area_struct *vma,
1995 struct mlx5_ib_ucontext *context)
1997 struct mlx5_bfreg_info *bfregi = &context->bfregi;
2002 u32 bfreg_dyn_idx = 0;
2004 int dyn_uar = (cmd == MLX5_IB_MMAP_ALLOC_WC);
2005 int max_valid_idx = dyn_uar ? bfregi->num_sys_pages :
2006 bfregi->num_static_sys_pages;
2008 if (vma->vm_end - vma->vm_start != PAGE_SIZE)
2012 idx = get_extended_index(vma->vm_pgoff) + bfregi->num_static_sys_pages;
2014 idx = get_index(vma->vm_pgoff);
2016 if (idx >= max_valid_idx) {
2017 mlx5_ib_warn(dev, "invalid uar index %lu, max=%d\n",
2018 idx, max_valid_idx);
2023 case MLX5_IB_MMAP_WC_PAGE:
2024 case MLX5_IB_MMAP_ALLOC_WC:
2025 /* Some architectures don't support WC memory */
2026 #if defined(CONFIG_X86)
2029 #elif !(defined(CONFIG_PPC) || (defined(CONFIG_ARM) && defined(CONFIG_MMU)))
2033 case MLX5_IB_MMAP_REGULAR_PAGE:
2034 /* For MLX5_IB_MMAP_REGULAR_PAGE do the best effort to get WC */
2035 prot = pgprot_writecombine(vma->vm_page_prot);
2037 case MLX5_IB_MMAP_NC_PAGE:
2038 prot = pgprot_noncached(vma->vm_page_prot);
2047 uars_per_page = get_uars_per_sys_page(dev, bfregi->lib_uar_4k);
2048 bfreg_dyn_idx = idx * (uars_per_page * MLX5_NON_FP_BFREGS_PER_UAR);
2049 if (bfreg_dyn_idx >= bfregi->total_num_bfregs) {
2050 mlx5_ib_warn(dev, "invalid bfreg_dyn_idx %u, max=%u\n",
2051 bfreg_dyn_idx, bfregi->total_num_bfregs);
2055 mutex_lock(&bfregi->lock);
2056 /* Fail if uar already allocated, first bfreg index of each
2057 * page holds its count.
2059 if (bfregi->count[bfreg_dyn_idx]) {
2060 mlx5_ib_warn(dev, "wrong offset, idx %lu is busy, bfregn=%u\n", idx, bfreg_dyn_idx);
2061 mutex_unlock(&bfregi->lock);
2065 bfregi->count[bfreg_dyn_idx]++;
2066 mutex_unlock(&bfregi->lock);
2068 err = mlx5_cmd_alloc_uar(dev->mdev, &uar_index);
2070 mlx5_ib_warn(dev, "UAR alloc failed\n");
2074 uar_index = bfregi->sys_pages[idx];
2077 pfn = uar_index2pfn(dev, uar_index);
2078 mlx5_ib_dbg(dev, "uar idx 0x%lx, pfn %pa\n", idx, &pfn);
2080 err = rdma_user_mmap_io(&context->ibucontext, vma, pfn, PAGE_SIZE,
2084 "rdma_user_mmap_io failed with error=%d, mmap_cmd=%s\n",
2085 err, mmap_cmd2str(cmd));
2090 bfregi->sys_pages[idx] = uar_index;
2097 mlx5_cmd_free_uar(dev->mdev, idx);
2100 mlx5_ib_free_bfreg(dev, bfregi, bfreg_dyn_idx);
2105 static int dm_mmap(struct ib_ucontext *context, struct vm_area_struct *vma)
2107 struct mlx5_ib_ucontext *mctx = to_mucontext(context);
2108 struct mlx5_ib_dev *dev = to_mdev(context->device);
2109 u16 page_idx = get_extended_index(vma->vm_pgoff);
2110 size_t map_size = vma->vm_end - vma->vm_start;
2111 u32 npages = map_size >> PAGE_SHIFT;
2114 if (find_next_zero_bit(mctx->dm_pages, page_idx + npages, page_idx) !=
2118 pfn = ((pci_resource_start(dev->mdev->pdev, 0) +
2119 MLX5_CAP64_DEV_MEM(dev->mdev, memic_bar_start_addr)) >>
2122 return rdma_user_mmap_io(context, vma, pfn, map_size,
2123 pgprot_writecombine(vma->vm_page_prot));
2126 static int mlx5_ib_mmap(struct ib_ucontext *ibcontext, struct vm_area_struct *vma)
2128 struct mlx5_ib_ucontext *context = to_mucontext(ibcontext);
2129 struct mlx5_ib_dev *dev = to_mdev(ibcontext->device);
2130 unsigned long command;
2133 command = get_command(vma->vm_pgoff);
2135 case MLX5_IB_MMAP_WC_PAGE:
2136 case MLX5_IB_MMAP_NC_PAGE:
2137 case MLX5_IB_MMAP_REGULAR_PAGE:
2138 case MLX5_IB_MMAP_ALLOC_WC:
2139 return uar_mmap(dev, command, vma, context);
2141 case MLX5_IB_MMAP_GET_CONTIGUOUS_PAGES:
2144 case MLX5_IB_MMAP_CORE_CLOCK:
2145 if (vma->vm_end - vma->vm_start != PAGE_SIZE)
2148 if (vma->vm_flags & VM_WRITE)
2151 /* Don't expose to user-space information it shouldn't have */
2152 if (PAGE_SIZE > 4096)
2155 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
2156 pfn = (dev->mdev->iseg_base +
2157 offsetof(struct mlx5_init_seg, internal_timer_h)) >>
2159 if (io_remap_pfn_range(vma, vma->vm_start, pfn,
2160 PAGE_SIZE, vma->vm_page_prot))
2163 case MLX5_IB_MMAP_CLOCK_INFO:
2164 return mlx5_ib_mmap_clock_info_page(dev, vma, context);
2166 case MLX5_IB_MMAP_DEVICE_MEM:
2167 return dm_mmap(ibcontext, vma);
2176 struct ib_dm *mlx5_ib_alloc_dm(struct ib_device *ibdev,
2177 struct ib_ucontext *context,
2178 struct ib_dm_alloc_attr *attr,
2179 struct uverbs_attr_bundle *attrs)
2181 u64 act_size = roundup(attr->length, MLX5_MEMIC_BASE_SIZE);
2182 struct mlx5_memic *memic = &to_mdev(ibdev)->memic;
2183 phys_addr_t memic_addr;
2184 struct mlx5_ib_dm *dm;
2189 dm = kzalloc(sizeof(*dm), GFP_KERNEL);
2191 return ERR_PTR(-ENOMEM);
2193 mlx5_ib_dbg(to_mdev(ibdev), "alloc_memic req: user_length=0x%llx act_length=0x%llx log_alignment=%d\n",
2194 attr->length, act_size, attr->alignment);
2196 err = mlx5_cmd_alloc_memic(memic, &memic_addr,
2197 act_size, attr->alignment);
2201 start_offset = memic_addr & ~PAGE_MASK;
2202 page_idx = (memic_addr - pci_resource_start(memic->dev->pdev, 0) -
2203 MLX5_CAP64_DEV_MEM(memic->dev, memic_bar_start_addr)) >>
2206 err = uverbs_copy_to(attrs,
2207 MLX5_IB_ATTR_ALLOC_DM_RESP_START_OFFSET,
2208 &start_offset, sizeof(start_offset));
2212 err = uverbs_copy_to(attrs,
2213 MLX5_IB_ATTR_ALLOC_DM_RESP_PAGE_INDEX,
2214 &page_idx, sizeof(page_idx));
2218 bitmap_set(to_mucontext(context)->dm_pages, page_idx,
2219 DIV_ROUND_UP(act_size, PAGE_SIZE));
2221 dm->dev_addr = memic_addr;
2226 mlx5_cmd_dealloc_memic(memic, memic_addr,
2230 return ERR_PTR(err);
2233 int mlx5_ib_dealloc_dm(struct ib_dm *ibdm)
2235 struct mlx5_memic *memic = &to_mdev(ibdm->device)->memic;
2236 struct mlx5_ib_dm *dm = to_mdm(ibdm);
2237 u64 act_size = roundup(dm->ibdm.length, MLX5_MEMIC_BASE_SIZE);
2241 ret = mlx5_cmd_dealloc_memic(memic, dm->dev_addr, act_size);
2245 page_idx = (dm->dev_addr - pci_resource_start(memic->dev->pdev, 0) -
2246 MLX5_CAP64_DEV_MEM(memic->dev, memic_bar_start_addr)) >>
2248 bitmap_clear(to_mucontext(ibdm->uobject->context)->dm_pages,
2250 DIV_ROUND_UP(act_size, PAGE_SIZE));
2257 static struct ib_pd *mlx5_ib_alloc_pd(struct ib_device *ibdev,
2258 struct ib_ucontext *context,
2259 struct ib_udata *udata)
2261 struct mlx5_ib_alloc_pd_resp resp;
2262 struct mlx5_ib_pd *pd;
2264 u32 out[MLX5_ST_SZ_DW(alloc_pd_out)] = {};
2265 u32 in[MLX5_ST_SZ_DW(alloc_pd_in)] = {};
2268 pd = kmalloc(sizeof(*pd), GFP_KERNEL);
2270 return ERR_PTR(-ENOMEM);
2272 uid = context ? to_mucontext(context)->devx_uid : 0;
2273 MLX5_SET(alloc_pd_in, in, opcode, MLX5_CMD_OP_ALLOC_PD);
2274 MLX5_SET(alloc_pd_in, in, uid, uid);
2275 err = mlx5_cmd_exec(to_mdev(ibdev)->mdev, in, sizeof(in),
2279 return ERR_PTR(err);
2282 pd->pdn = MLX5_GET(alloc_pd_out, out, pd);
2286 if (ib_copy_to_udata(udata, &resp, sizeof(resp))) {
2287 mlx5_cmd_dealloc_pd(to_mdev(ibdev)->mdev, pd->pdn, uid);
2289 return ERR_PTR(-EFAULT);
2296 static int mlx5_ib_dealloc_pd(struct ib_pd *pd)
2298 struct mlx5_ib_dev *mdev = to_mdev(pd->device);
2299 struct mlx5_ib_pd *mpd = to_mpd(pd);
2301 mlx5_cmd_dealloc_pd(mdev->mdev, mpd->pdn, mpd->uid);
2308 MATCH_CRITERIA_ENABLE_OUTER_BIT,
2309 MATCH_CRITERIA_ENABLE_MISC_BIT,
2310 MATCH_CRITERIA_ENABLE_INNER_BIT,
2311 MATCH_CRITERIA_ENABLE_MISC2_BIT
2314 #define HEADER_IS_ZERO(match_criteria, headers) \
2315 !(memchr_inv(MLX5_ADDR_OF(fte_match_param, match_criteria, headers), \
2316 0, MLX5_FLD_SZ_BYTES(fte_match_param, headers))) \
2318 static u8 get_match_criteria_enable(u32 *match_criteria)
2320 u8 match_criteria_enable;
2322 match_criteria_enable =
2323 (!HEADER_IS_ZERO(match_criteria, outer_headers)) <<
2324 MATCH_CRITERIA_ENABLE_OUTER_BIT;
2325 match_criteria_enable |=
2326 (!HEADER_IS_ZERO(match_criteria, misc_parameters)) <<
2327 MATCH_CRITERIA_ENABLE_MISC_BIT;
2328 match_criteria_enable |=
2329 (!HEADER_IS_ZERO(match_criteria, inner_headers)) <<
2330 MATCH_CRITERIA_ENABLE_INNER_BIT;
2331 match_criteria_enable |=
2332 (!HEADER_IS_ZERO(match_criteria, misc_parameters_2)) <<
2333 MATCH_CRITERIA_ENABLE_MISC2_BIT;
2335 return match_criteria_enable;
2338 static void set_proto(void *outer_c, void *outer_v, u8 mask, u8 val)
2340 MLX5_SET(fte_match_set_lyr_2_4, outer_c, ip_protocol, mask);
2341 MLX5_SET(fte_match_set_lyr_2_4, outer_v, ip_protocol, val);
2344 static void set_flow_label(void *misc_c, void *misc_v, u32 mask, u32 val,
2348 MLX5_SET(fte_match_set_misc,
2349 misc_c, inner_ipv6_flow_label, mask);
2350 MLX5_SET(fte_match_set_misc,
2351 misc_v, inner_ipv6_flow_label, val);
2353 MLX5_SET(fte_match_set_misc,
2354 misc_c, outer_ipv6_flow_label, mask);
2355 MLX5_SET(fte_match_set_misc,
2356 misc_v, outer_ipv6_flow_label, val);
2360 static void set_tos(void *outer_c, void *outer_v, u8 mask, u8 val)
2362 MLX5_SET(fte_match_set_lyr_2_4, outer_c, ip_ecn, mask);
2363 MLX5_SET(fte_match_set_lyr_2_4, outer_v, ip_ecn, val);
2364 MLX5_SET(fte_match_set_lyr_2_4, outer_c, ip_dscp, mask >> 2);
2365 MLX5_SET(fte_match_set_lyr_2_4, outer_v, ip_dscp, val >> 2);
2368 static int check_mpls_supp_fields(u32 field_support, const __be32 *set_mask)
2370 if (MLX5_GET(fte_match_mpls, set_mask, mpls_label) &&
2371 !(field_support & MLX5_FIELD_SUPPORT_MPLS_LABEL))
2374 if (MLX5_GET(fte_match_mpls, set_mask, mpls_exp) &&
2375 !(field_support & MLX5_FIELD_SUPPORT_MPLS_EXP))
2378 if (MLX5_GET(fte_match_mpls, set_mask, mpls_s_bos) &&
2379 !(field_support & MLX5_FIELD_SUPPORT_MPLS_S_BOS))
2382 if (MLX5_GET(fte_match_mpls, set_mask, mpls_ttl) &&
2383 !(field_support & MLX5_FIELD_SUPPORT_MPLS_TTL))
2389 #define LAST_ETH_FIELD vlan_tag
2390 #define LAST_IB_FIELD sl
2391 #define LAST_IPV4_FIELD tos
2392 #define LAST_IPV6_FIELD traffic_class
2393 #define LAST_TCP_UDP_FIELD src_port
2394 #define LAST_TUNNEL_FIELD tunnel_id
2395 #define LAST_FLOW_TAG_FIELD tag_id
2396 #define LAST_DROP_FIELD size
2397 #define LAST_COUNTERS_FIELD counters
2399 /* Field is the last supported field */
2400 #define FIELDS_NOT_SUPPORTED(filter, field)\
2401 memchr_inv((void *)&filter.field +\
2402 sizeof(filter.field), 0,\
2404 offsetof(typeof(filter), field) -\
2405 sizeof(filter.field))
2407 int parse_flow_flow_action(struct mlx5_ib_flow_action *maction,
2409 struct mlx5_flow_act *action)
2412 switch (maction->ib_action.type) {
2413 case IB_FLOW_ACTION_ESP:
2414 if (action->action & (MLX5_FLOW_CONTEXT_ACTION_ENCRYPT |
2415 MLX5_FLOW_CONTEXT_ACTION_DECRYPT))
2417 /* Currently only AES_GCM keymat is supported by the driver */
2418 action->esp_id = (uintptr_t)maction->esp_aes_gcm.ctx;
2419 action->action |= is_egress ?
2420 MLX5_FLOW_CONTEXT_ACTION_ENCRYPT :
2421 MLX5_FLOW_CONTEXT_ACTION_DECRYPT;
2423 case IB_FLOW_ACTION_UNSPECIFIED:
2424 if (maction->flow_action_raw.sub_type ==
2425 MLX5_IB_FLOW_ACTION_MODIFY_HEADER) {
2426 if (action->action & MLX5_FLOW_CONTEXT_ACTION_MOD_HDR)
2428 action->action |= MLX5_FLOW_CONTEXT_ACTION_MOD_HDR;
2429 action->modify_id = maction->flow_action_raw.action_id;
2432 if (maction->flow_action_raw.sub_type ==
2433 MLX5_IB_FLOW_ACTION_DECAP) {
2434 if (action->action & MLX5_FLOW_CONTEXT_ACTION_DECAP)
2436 action->action |= MLX5_FLOW_CONTEXT_ACTION_DECAP;
2439 if (maction->flow_action_raw.sub_type ==
2440 MLX5_IB_FLOW_ACTION_PACKET_REFORMAT) {
2441 if (action->action &
2442 MLX5_FLOW_CONTEXT_ACTION_PACKET_REFORMAT)
2445 MLX5_FLOW_CONTEXT_ACTION_PACKET_REFORMAT;
2446 action->reformat_id =
2447 maction->flow_action_raw.action_id;
2456 static int parse_flow_attr(struct mlx5_core_dev *mdev, u32 *match_c,
2457 u32 *match_v, const union ib_flow_spec *ib_spec,
2458 const struct ib_flow_attr *flow_attr,
2459 struct mlx5_flow_act *action, u32 prev_type)
2461 void *misc_params_c = MLX5_ADDR_OF(fte_match_param, match_c,
2463 void *misc_params_v = MLX5_ADDR_OF(fte_match_param, match_v,
2465 void *misc_params2_c = MLX5_ADDR_OF(fte_match_param, match_c,
2467 void *misc_params2_v = MLX5_ADDR_OF(fte_match_param, match_v,
2474 if (ib_spec->type & IB_FLOW_SPEC_INNER) {
2475 headers_c = MLX5_ADDR_OF(fte_match_param, match_c,
2477 headers_v = MLX5_ADDR_OF(fte_match_param, match_v,
2479 match_ipv = MLX5_CAP_FLOWTABLE_NIC_RX(mdev,
2480 ft_field_support.inner_ip_version);
2482 headers_c = MLX5_ADDR_OF(fte_match_param, match_c,
2484 headers_v = MLX5_ADDR_OF(fte_match_param, match_v,
2486 match_ipv = MLX5_CAP_FLOWTABLE_NIC_RX(mdev,
2487 ft_field_support.outer_ip_version);
2490 switch (ib_spec->type & ~IB_FLOW_SPEC_INNER) {
2491 case IB_FLOW_SPEC_ETH:
2492 if (FIELDS_NOT_SUPPORTED(ib_spec->eth.mask, LAST_ETH_FIELD))
2495 ether_addr_copy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
2497 ib_spec->eth.mask.dst_mac);
2498 ether_addr_copy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
2500 ib_spec->eth.val.dst_mac);
2502 ether_addr_copy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
2504 ib_spec->eth.mask.src_mac);
2505 ether_addr_copy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
2507 ib_spec->eth.val.src_mac);
2509 if (ib_spec->eth.mask.vlan_tag) {
2510 MLX5_SET(fte_match_set_lyr_2_4, headers_c,
2512 MLX5_SET(fte_match_set_lyr_2_4, headers_v,
2515 MLX5_SET(fte_match_set_lyr_2_4, headers_c,
2516 first_vid, ntohs(ib_spec->eth.mask.vlan_tag));
2517 MLX5_SET(fte_match_set_lyr_2_4, headers_v,
2518 first_vid, ntohs(ib_spec->eth.val.vlan_tag));
2520 MLX5_SET(fte_match_set_lyr_2_4, headers_c,
2522 ntohs(ib_spec->eth.mask.vlan_tag) >> 12);
2523 MLX5_SET(fte_match_set_lyr_2_4, headers_v,
2525 ntohs(ib_spec->eth.val.vlan_tag) >> 12);
2527 MLX5_SET(fte_match_set_lyr_2_4, headers_c,
2529 ntohs(ib_spec->eth.mask.vlan_tag) >> 13);
2530 MLX5_SET(fte_match_set_lyr_2_4, headers_v,
2532 ntohs(ib_spec->eth.val.vlan_tag) >> 13);
2534 MLX5_SET(fte_match_set_lyr_2_4, headers_c,
2535 ethertype, ntohs(ib_spec->eth.mask.ether_type));
2536 MLX5_SET(fte_match_set_lyr_2_4, headers_v,
2537 ethertype, ntohs(ib_spec->eth.val.ether_type));
2539 case IB_FLOW_SPEC_IPV4:
2540 if (FIELDS_NOT_SUPPORTED(ib_spec->ipv4.mask, LAST_IPV4_FIELD))
2544 MLX5_SET(fte_match_set_lyr_2_4, headers_c,
2546 MLX5_SET(fte_match_set_lyr_2_4, headers_v,
2547 ip_version, MLX5_FS_IPV4_VERSION);
2549 MLX5_SET(fte_match_set_lyr_2_4, headers_c,
2551 MLX5_SET(fte_match_set_lyr_2_4, headers_v,
2552 ethertype, ETH_P_IP);
2555 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
2556 src_ipv4_src_ipv6.ipv4_layout.ipv4),
2557 &ib_spec->ipv4.mask.src_ip,
2558 sizeof(ib_spec->ipv4.mask.src_ip));
2559 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
2560 src_ipv4_src_ipv6.ipv4_layout.ipv4),
2561 &ib_spec->ipv4.val.src_ip,
2562 sizeof(ib_spec->ipv4.val.src_ip));
2563 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
2564 dst_ipv4_dst_ipv6.ipv4_layout.ipv4),
2565 &ib_spec->ipv4.mask.dst_ip,
2566 sizeof(ib_spec->ipv4.mask.dst_ip));
2567 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
2568 dst_ipv4_dst_ipv6.ipv4_layout.ipv4),
2569 &ib_spec->ipv4.val.dst_ip,
2570 sizeof(ib_spec->ipv4.val.dst_ip));
2572 set_tos(headers_c, headers_v,
2573 ib_spec->ipv4.mask.tos, ib_spec->ipv4.val.tos);
2575 set_proto(headers_c, headers_v,
2576 ib_spec->ipv4.mask.proto, ib_spec->ipv4.val.proto);
2578 case IB_FLOW_SPEC_IPV6:
2579 if (FIELDS_NOT_SUPPORTED(ib_spec->ipv6.mask, LAST_IPV6_FIELD))
2583 MLX5_SET(fte_match_set_lyr_2_4, headers_c,
2585 MLX5_SET(fte_match_set_lyr_2_4, headers_v,
2586 ip_version, MLX5_FS_IPV6_VERSION);
2588 MLX5_SET(fte_match_set_lyr_2_4, headers_c,
2590 MLX5_SET(fte_match_set_lyr_2_4, headers_v,
2591 ethertype, ETH_P_IPV6);
2594 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
2595 src_ipv4_src_ipv6.ipv6_layout.ipv6),
2596 &ib_spec->ipv6.mask.src_ip,
2597 sizeof(ib_spec->ipv6.mask.src_ip));
2598 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
2599 src_ipv4_src_ipv6.ipv6_layout.ipv6),
2600 &ib_spec->ipv6.val.src_ip,
2601 sizeof(ib_spec->ipv6.val.src_ip));
2602 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
2603 dst_ipv4_dst_ipv6.ipv6_layout.ipv6),
2604 &ib_spec->ipv6.mask.dst_ip,
2605 sizeof(ib_spec->ipv6.mask.dst_ip));
2606 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
2607 dst_ipv4_dst_ipv6.ipv6_layout.ipv6),
2608 &ib_spec->ipv6.val.dst_ip,
2609 sizeof(ib_spec->ipv6.val.dst_ip));
2611 set_tos(headers_c, headers_v,
2612 ib_spec->ipv6.mask.traffic_class,
2613 ib_spec->ipv6.val.traffic_class);
2615 set_proto(headers_c, headers_v,
2616 ib_spec->ipv6.mask.next_hdr,
2617 ib_spec->ipv6.val.next_hdr);
2619 set_flow_label(misc_params_c, misc_params_v,
2620 ntohl(ib_spec->ipv6.mask.flow_label),
2621 ntohl(ib_spec->ipv6.val.flow_label),
2622 ib_spec->type & IB_FLOW_SPEC_INNER);
2624 case IB_FLOW_SPEC_ESP:
2625 if (ib_spec->esp.mask.seq)
2628 MLX5_SET(fte_match_set_misc, misc_params_c, outer_esp_spi,
2629 ntohl(ib_spec->esp.mask.spi));
2630 MLX5_SET(fte_match_set_misc, misc_params_v, outer_esp_spi,
2631 ntohl(ib_spec->esp.val.spi));
2633 case IB_FLOW_SPEC_TCP:
2634 if (FIELDS_NOT_SUPPORTED(ib_spec->tcp_udp.mask,
2635 LAST_TCP_UDP_FIELD))
2638 MLX5_SET(fte_match_set_lyr_2_4, headers_c, ip_protocol,
2640 MLX5_SET(fte_match_set_lyr_2_4, headers_v, ip_protocol,
2643 MLX5_SET(fte_match_set_lyr_2_4, headers_c, tcp_sport,
2644 ntohs(ib_spec->tcp_udp.mask.src_port));
2645 MLX5_SET(fte_match_set_lyr_2_4, headers_v, tcp_sport,
2646 ntohs(ib_spec->tcp_udp.val.src_port));
2648 MLX5_SET(fte_match_set_lyr_2_4, headers_c, tcp_dport,
2649 ntohs(ib_spec->tcp_udp.mask.dst_port));
2650 MLX5_SET(fte_match_set_lyr_2_4, headers_v, tcp_dport,
2651 ntohs(ib_spec->tcp_udp.val.dst_port));
2653 case IB_FLOW_SPEC_UDP:
2654 if (FIELDS_NOT_SUPPORTED(ib_spec->tcp_udp.mask,
2655 LAST_TCP_UDP_FIELD))
2658 MLX5_SET(fte_match_set_lyr_2_4, headers_c, ip_protocol,
2660 MLX5_SET(fte_match_set_lyr_2_4, headers_v, ip_protocol,
2663 MLX5_SET(fte_match_set_lyr_2_4, headers_c, udp_sport,
2664 ntohs(ib_spec->tcp_udp.mask.src_port));
2665 MLX5_SET(fte_match_set_lyr_2_4, headers_v, udp_sport,
2666 ntohs(ib_spec->tcp_udp.val.src_port));
2668 MLX5_SET(fte_match_set_lyr_2_4, headers_c, udp_dport,
2669 ntohs(ib_spec->tcp_udp.mask.dst_port));
2670 MLX5_SET(fte_match_set_lyr_2_4, headers_v, udp_dport,
2671 ntohs(ib_spec->tcp_udp.val.dst_port));
2673 case IB_FLOW_SPEC_GRE:
2674 if (ib_spec->gre.mask.c_ks_res0_ver)
2677 MLX5_SET(fte_match_set_lyr_2_4, headers_c, ip_protocol,
2679 MLX5_SET(fte_match_set_lyr_2_4, headers_v, ip_protocol,
2682 MLX5_SET(fte_match_set_misc, misc_params_c, gre_protocol,
2683 ntohs(ib_spec->gre.mask.protocol));
2684 MLX5_SET(fte_match_set_misc, misc_params_v, gre_protocol,
2685 ntohs(ib_spec->gre.val.protocol));
2687 memcpy(MLX5_ADDR_OF(fte_match_set_misc, misc_params_c,
2689 &ib_spec->gre.mask.key,
2690 sizeof(ib_spec->gre.mask.key));
2691 memcpy(MLX5_ADDR_OF(fte_match_set_misc, misc_params_v,
2693 &ib_spec->gre.val.key,
2694 sizeof(ib_spec->gre.val.key));
2696 case IB_FLOW_SPEC_MPLS:
2697 switch (prev_type) {
2698 case IB_FLOW_SPEC_UDP:
2699 if (check_mpls_supp_fields(MLX5_CAP_FLOWTABLE_NIC_RX(mdev,
2700 ft_field_support.outer_first_mpls_over_udp),
2701 &ib_spec->mpls.mask.tag))
2704 memcpy(MLX5_ADDR_OF(fte_match_set_misc2, misc_params2_v,
2705 outer_first_mpls_over_udp),
2706 &ib_spec->mpls.val.tag,
2707 sizeof(ib_spec->mpls.val.tag));
2708 memcpy(MLX5_ADDR_OF(fte_match_set_misc2, misc_params2_c,
2709 outer_first_mpls_over_udp),
2710 &ib_spec->mpls.mask.tag,
2711 sizeof(ib_spec->mpls.mask.tag));
2713 case IB_FLOW_SPEC_GRE:
2714 if (check_mpls_supp_fields(MLX5_CAP_FLOWTABLE_NIC_RX(mdev,
2715 ft_field_support.outer_first_mpls_over_gre),
2716 &ib_spec->mpls.mask.tag))
2719 memcpy(MLX5_ADDR_OF(fte_match_set_misc2, misc_params2_v,
2720 outer_first_mpls_over_gre),
2721 &ib_spec->mpls.val.tag,
2722 sizeof(ib_spec->mpls.val.tag));
2723 memcpy(MLX5_ADDR_OF(fte_match_set_misc2, misc_params2_c,
2724 outer_first_mpls_over_gre),
2725 &ib_spec->mpls.mask.tag,
2726 sizeof(ib_spec->mpls.mask.tag));
2729 if (ib_spec->type & IB_FLOW_SPEC_INNER) {
2730 if (check_mpls_supp_fields(MLX5_CAP_FLOWTABLE_NIC_RX(mdev,
2731 ft_field_support.inner_first_mpls),
2732 &ib_spec->mpls.mask.tag))
2735 memcpy(MLX5_ADDR_OF(fte_match_set_misc2, misc_params2_v,
2737 &ib_spec->mpls.val.tag,
2738 sizeof(ib_spec->mpls.val.tag));
2739 memcpy(MLX5_ADDR_OF(fte_match_set_misc2, misc_params2_c,
2741 &ib_spec->mpls.mask.tag,
2742 sizeof(ib_spec->mpls.mask.tag));
2744 if (check_mpls_supp_fields(MLX5_CAP_FLOWTABLE_NIC_RX(mdev,
2745 ft_field_support.outer_first_mpls),
2746 &ib_spec->mpls.mask.tag))
2749 memcpy(MLX5_ADDR_OF(fte_match_set_misc2, misc_params2_v,
2751 &ib_spec->mpls.val.tag,
2752 sizeof(ib_spec->mpls.val.tag));
2753 memcpy(MLX5_ADDR_OF(fte_match_set_misc2, misc_params2_c,
2755 &ib_spec->mpls.mask.tag,
2756 sizeof(ib_spec->mpls.mask.tag));
2760 case IB_FLOW_SPEC_VXLAN_TUNNEL:
2761 if (FIELDS_NOT_SUPPORTED(ib_spec->tunnel.mask,
2765 MLX5_SET(fte_match_set_misc, misc_params_c, vxlan_vni,
2766 ntohl(ib_spec->tunnel.mask.tunnel_id));
2767 MLX5_SET(fte_match_set_misc, misc_params_v, vxlan_vni,
2768 ntohl(ib_spec->tunnel.val.tunnel_id));
2770 case IB_FLOW_SPEC_ACTION_TAG:
2771 if (FIELDS_NOT_SUPPORTED(ib_spec->flow_tag,
2772 LAST_FLOW_TAG_FIELD))
2774 if (ib_spec->flow_tag.tag_id >= BIT(24))
2777 action->flow_tag = ib_spec->flow_tag.tag_id;
2778 action->flags |= FLOW_ACT_HAS_TAG;
2780 case IB_FLOW_SPEC_ACTION_DROP:
2781 if (FIELDS_NOT_SUPPORTED(ib_spec->drop,
2784 action->action |= MLX5_FLOW_CONTEXT_ACTION_DROP;
2786 case IB_FLOW_SPEC_ACTION_HANDLE:
2787 ret = parse_flow_flow_action(to_mflow_act(ib_spec->action.act),
2788 flow_attr->flags & IB_FLOW_ATTR_FLAGS_EGRESS, action);
2792 case IB_FLOW_SPEC_ACTION_COUNT:
2793 if (FIELDS_NOT_SUPPORTED(ib_spec->flow_count,
2794 LAST_COUNTERS_FIELD))
2797 /* for now support only one counters spec per flow */
2798 if (action->action & MLX5_FLOW_CONTEXT_ACTION_COUNT)
2801 action->counters = ib_spec->flow_count.counters;
2802 action->action |= MLX5_FLOW_CONTEXT_ACTION_COUNT;
2811 /* If a flow could catch both multicast and unicast packets,
2812 * it won't fall into the multicast flow steering table and this rule
2813 * could steal other multicast packets.
2815 static bool flow_is_multicast_only(const struct ib_flow_attr *ib_attr)
2817 union ib_flow_spec *flow_spec;
2819 if (ib_attr->type != IB_FLOW_ATTR_NORMAL ||
2820 ib_attr->num_of_specs < 1)
2823 flow_spec = (union ib_flow_spec *)(ib_attr + 1);
2824 if (flow_spec->type == IB_FLOW_SPEC_IPV4) {
2825 struct ib_flow_spec_ipv4 *ipv4_spec;
2827 ipv4_spec = (struct ib_flow_spec_ipv4 *)flow_spec;
2828 if (ipv4_is_multicast(ipv4_spec->val.dst_ip))
2834 if (flow_spec->type == IB_FLOW_SPEC_ETH) {
2835 struct ib_flow_spec_eth *eth_spec;
2837 eth_spec = (struct ib_flow_spec_eth *)flow_spec;
2838 return is_multicast_ether_addr(eth_spec->mask.dst_mac) &&
2839 is_multicast_ether_addr(eth_spec->val.dst_mac);
2851 static enum valid_spec
2852 is_valid_esp_aes_gcm(struct mlx5_core_dev *mdev,
2853 const struct mlx5_flow_spec *spec,
2854 const struct mlx5_flow_act *flow_act,
2857 const u32 *match_c = spec->match_criteria;
2859 (flow_act->action & (MLX5_FLOW_CONTEXT_ACTION_ENCRYPT |
2860 MLX5_FLOW_CONTEXT_ACTION_DECRYPT));
2861 bool is_ipsec = mlx5_fs_is_ipsec_flow(match_c);
2862 bool is_drop = flow_act->action & MLX5_FLOW_CONTEXT_ACTION_DROP;
2865 * Currently only crypto is supported in egress, when regular egress
2866 * rules would be supported, always return VALID_SPEC_NA.
2869 return VALID_SPEC_NA;
2871 return is_crypto && is_ipsec &&
2872 (!egress || (!is_drop && !(flow_act->flags & FLOW_ACT_HAS_TAG))) ?
2873 VALID_SPEC_VALID : VALID_SPEC_INVALID;
2876 static bool is_valid_spec(struct mlx5_core_dev *mdev,
2877 const struct mlx5_flow_spec *spec,
2878 const struct mlx5_flow_act *flow_act,
2881 /* We curretly only support ipsec egress flow */
2882 return is_valid_esp_aes_gcm(mdev, spec, flow_act, egress) != VALID_SPEC_INVALID;
2885 static bool is_valid_ethertype(struct mlx5_core_dev *mdev,
2886 const struct ib_flow_attr *flow_attr,
2889 union ib_flow_spec *ib_spec = (union ib_flow_spec *)(flow_attr + 1);
2890 int match_ipv = check_inner ?
2891 MLX5_CAP_FLOWTABLE_NIC_RX(mdev,
2892 ft_field_support.inner_ip_version) :
2893 MLX5_CAP_FLOWTABLE_NIC_RX(mdev,
2894 ft_field_support.outer_ip_version);
2895 int inner_bit = check_inner ? IB_FLOW_SPEC_INNER : 0;
2896 bool ipv4_spec_valid, ipv6_spec_valid;
2897 unsigned int ip_spec_type = 0;
2898 bool has_ethertype = false;
2899 unsigned int spec_index;
2900 bool mask_valid = true;
2904 /* Validate that ethertype is correct */
2905 for (spec_index = 0; spec_index < flow_attr->num_of_specs; spec_index++) {
2906 if ((ib_spec->type == (IB_FLOW_SPEC_ETH | inner_bit)) &&
2907 ib_spec->eth.mask.ether_type) {
2908 mask_valid = (ib_spec->eth.mask.ether_type ==
2910 has_ethertype = true;
2911 eth_type = ntohs(ib_spec->eth.val.ether_type);
2912 } else if ((ib_spec->type == (IB_FLOW_SPEC_IPV4 | inner_bit)) ||
2913 (ib_spec->type == (IB_FLOW_SPEC_IPV6 | inner_bit))) {
2914 ip_spec_type = ib_spec->type;
2916 ib_spec = (void *)ib_spec + ib_spec->size;
2919 type_valid = (!has_ethertype) || (!ip_spec_type);
2920 if (!type_valid && mask_valid) {
2921 ipv4_spec_valid = (eth_type == ETH_P_IP) &&
2922 (ip_spec_type == (IB_FLOW_SPEC_IPV4 | inner_bit));
2923 ipv6_spec_valid = (eth_type == ETH_P_IPV6) &&
2924 (ip_spec_type == (IB_FLOW_SPEC_IPV6 | inner_bit));
2926 type_valid = (ipv4_spec_valid) || (ipv6_spec_valid) ||
2927 (((eth_type == ETH_P_MPLS_UC) ||
2928 (eth_type == ETH_P_MPLS_MC)) && match_ipv);
2934 static bool is_valid_attr(struct mlx5_core_dev *mdev,
2935 const struct ib_flow_attr *flow_attr)
2937 return is_valid_ethertype(mdev, flow_attr, false) &&
2938 is_valid_ethertype(mdev, flow_attr, true);
2941 static void put_flow_table(struct mlx5_ib_dev *dev,
2942 struct mlx5_ib_flow_prio *prio, bool ft_added)
2944 prio->refcount -= !!ft_added;
2945 if (!prio->refcount) {
2946 mlx5_destroy_flow_table(prio->flow_table);
2947 prio->flow_table = NULL;
2951 static void counters_clear_description(struct ib_counters *counters)
2953 struct mlx5_ib_mcounters *mcounters = to_mcounters(counters);
2955 mutex_lock(&mcounters->mcntrs_mutex);
2956 kfree(mcounters->counters_data);
2957 mcounters->counters_data = NULL;
2958 mcounters->cntrs_max_index = 0;
2959 mutex_unlock(&mcounters->mcntrs_mutex);
2962 static int mlx5_ib_destroy_flow(struct ib_flow *flow_id)
2964 struct mlx5_ib_flow_handler *handler = container_of(flow_id,
2965 struct mlx5_ib_flow_handler,
2967 struct mlx5_ib_flow_handler *iter, *tmp;
2968 struct mlx5_ib_dev *dev = handler->dev;
2970 mutex_lock(&dev->flow_db->lock);
2972 list_for_each_entry_safe(iter, tmp, &handler->list, list) {
2973 mlx5_del_flow_rules(iter->rule);
2974 put_flow_table(dev, iter->prio, true);
2975 list_del(&iter->list);
2979 mlx5_del_flow_rules(handler->rule);
2980 put_flow_table(dev, handler->prio, true);
2981 if (handler->ibcounters &&
2982 atomic_read(&handler->ibcounters->usecnt) == 1)
2983 counters_clear_description(handler->ibcounters);
2985 mutex_unlock(&dev->flow_db->lock);
2986 if (handler->flow_matcher)
2987 atomic_dec(&handler->flow_matcher->usecnt);
2993 static int ib_prio_to_core_prio(unsigned int priority, bool dont_trap)
3001 enum flow_table_type {
3006 #define MLX5_FS_MAX_TYPES 6
3007 #define MLX5_FS_MAX_ENTRIES BIT(16)
3009 static struct mlx5_ib_flow_prio *_get_prio(struct mlx5_flow_namespace *ns,
3010 struct mlx5_ib_flow_prio *prio,
3012 int num_entries, int num_groups,
3015 struct mlx5_flow_table *ft;
3017 ft = mlx5_create_auto_grouped_flow_table(ns, priority,
3022 return ERR_CAST(ft);
3024 prio->flow_table = ft;
3029 static struct mlx5_ib_flow_prio *get_flow_table(struct mlx5_ib_dev *dev,
3030 struct ib_flow_attr *flow_attr,
3031 enum flow_table_type ft_type)
3033 bool dont_trap = flow_attr->flags & IB_FLOW_ATTR_FLAGS_DONT_TRAP;
3034 struct mlx5_flow_namespace *ns = NULL;
3035 struct mlx5_ib_flow_prio *prio;
3036 struct mlx5_flow_table *ft;
3043 max_table_size = BIT(MLX5_CAP_FLOWTABLE_NIC_RX(dev->mdev,
3045 if (flow_attr->type == IB_FLOW_ATTR_NORMAL) {
3046 enum mlx5_flow_namespace_type fn_type;
3048 if (flow_is_multicast_only(flow_attr) &&
3050 priority = MLX5_IB_FLOW_MCAST_PRIO;
3052 priority = ib_prio_to_core_prio(flow_attr->priority,
3054 if (ft_type == MLX5_IB_FT_RX) {
3055 fn_type = MLX5_FLOW_NAMESPACE_BYPASS;
3056 prio = &dev->flow_db->prios[priority];
3058 MLX5_CAP_FLOWTABLE_NIC_RX(dev->mdev, decap))
3059 flags |= MLX5_FLOW_TABLE_TUNNEL_EN_DECAP;
3061 MLX5_CAP_FLOWTABLE_NIC_RX(dev->mdev,
3062 reformat_l3_tunnel_to_l2))
3063 flags |= MLX5_FLOW_TABLE_TUNNEL_EN_REFORMAT;
3066 BIT(MLX5_CAP_FLOWTABLE_NIC_TX(dev->mdev,
3068 fn_type = MLX5_FLOW_NAMESPACE_EGRESS;
3069 prio = &dev->flow_db->egress_prios[priority];
3071 MLX5_CAP_FLOWTABLE_NIC_TX(dev->mdev, reformat))
3072 flags |= MLX5_FLOW_TABLE_TUNNEL_EN_REFORMAT;
3074 ns = mlx5_get_flow_namespace(dev->mdev, fn_type);
3075 num_entries = MLX5_FS_MAX_ENTRIES;
3076 num_groups = MLX5_FS_MAX_TYPES;
3077 } else if (flow_attr->type == IB_FLOW_ATTR_ALL_DEFAULT ||
3078 flow_attr->type == IB_FLOW_ATTR_MC_DEFAULT) {
3079 ns = mlx5_get_flow_namespace(dev->mdev,
3080 MLX5_FLOW_NAMESPACE_LEFTOVERS);
3081 build_leftovers_ft_param(&priority,
3084 prio = &dev->flow_db->prios[MLX5_IB_FLOW_LEFTOVERS_PRIO];
3085 } else if (flow_attr->type == IB_FLOW_ATTR_SNIFFER) {
3086 if (!MLX5_CAP_FLOWTABLE(dev->mdev,
3087 allow_sniffer_and_nic_rx_shared_tir))
3088 return ERR_PTR(-ENOTSUPP);
3090 ns = mlx5_get_flow_namespace(dev->mdev, ft_type == MLX5_IB_FT_RX ?
3091 MLX5_FLOW_NAMESPACE_SNIFFER_RX :
3092 MLX5_FLOW_NAMESPACE_SNIFFER_TX);
3094 prio = &dev->flow_db->sniffer[ft_type];
3101 return ERR_PTR(-ENOTSUPP);
3103 if (num_entries > max_table_size)
3104 return ERR_PTR(-ENOMEM);
3106 ft = prio->flow_table;
3108 return _get_prio(ns, prio, priority, num_entries, num_groups,
3114 static void set_underlay_qp(struct mlx5_ib_dev *dev,
3115 struct mlx5_flow_spec *spec,
3118 void *misc_params_c = MLX5_ADDR_OF(fte_match_param,
3119 spec->match_criteria,
3121 void *misc_params_v = MLX5_ADDR_OF(fte_match_param, spec->match_value,
3125 MLX5_CAP_FLOWTABLE_NIC_RX(dev->mdev,
3126 ft_field_support.bth_dst_qp)) {
3127 MLX5_SET(fte_match_set_misc,
3128 misc_params_v, bth_dst_qp, underlay_qpn);
3129 MLX5_SET(fte_match_set_misc,
3130 misc_params_c, bth_dst_qp, 0xffffff);
3134 static int read_flow_counters(struct ib_device *ibdev,
3135 struct mlx5_read_counters_attr *read_attr)
3137 struct mlx5_fc *fc = read_attr->hw_cntrs_hndl;
3138 struct mlx5_ib_dev *dev = to_mdev(ibdev);
3140 return mlx5_fc_query(dev->mdev, fc,
3141 &read_attr->out[IB_COUNTER_PACKETS],
3142 &read_attr->out[IB_COUNTER_BYTES]);
3145 /* flow counters currently expose two counters packets and bytes */
3146 #define FLOW_COUNTERS_NUM 2
3147 static int counters_set_description(struct ib_counters *counters,
3148 enum mlx5_ib_counters_type counters_type,
3149 struct mlx5_ib_flow_counters_desc *desc_data,
3152 struct mlx5_ib_mcounters *mcounters = to_mcounters(counters);
3153 u32 cntrs_max_index = 0;
3156 if (counters_type != MLX5_IB_COUNTERS_FLOW)
3159 /* init the fields for the object */
3160 mcounters->type = counters_type;
3161 mcounters->read_counters = read_flow_counters;
3162 mcounters->counters_num = FLOW_COUNTERS_NUM;
3163 mcounters->ncounters = ncounters;
3164 /* each counter entry have both description and index pair */
3165 for (i = 0; i < ncounters; i++) {
3166 if (desc_data[i].description > IB_COUNTER_BYTES)
3169 if (cntrs_max_index <= desc_data[i].index)
3170 cntrs_max_index = desc_data[i].index + 1;
3173 mutex_lock(&mcounters->mcntrs_mutex);
3174 mcounters->counters_data = desc_data;
3175 mcounters->cntrs_max_index = cntrs_max_index;
3176 mutex_unlock(&mcounters->mcntrs_mutex);
3181 #define MAX_COUNTERS_NUM (USHRT_MAX / (sizeof(u32) * 2))
3182 static int flow_counters_set_data(struct ib_counters *ibcounters,
3183 struct mlx5_ib_create_flow *ucmd)
3185 struct mlx5_ib_mcounters *mcounters = to_mcounters(ibcounters);
3186 struct mlx5_ib_flow_counters_data *cntrs_data = NULL;
3187 struct mlx5_ib_flow_counters_desc *desc_data = NULL;
3188 bool hw_hndl = false;
3191 if (ucmd && ucmd->ncounters_data != 0) {
3192 cntrs_data = ucmd->data;
3193 if (cntrs_data->ncounters > MAX_COUNTERS_NUM)
3196 desc_data = kcalloc(cntrs_data->ncounters,
3202 if (copy_from_user(desc_data,
3203 u64_to_user_ptr(cntrs_data->counters_data),
3204 sizeof(*desc_data) * cntrs_data->ncounters)) {
3210 if (!mcounters->hw_cntrs_hndl) {
3211 mcounters->hw_cntrs_hndl = mlx5_fc_create(
3212 to_mdev(ibcounters->device)->mdev, false);
3213 if (IS_ERR(mcounters->hw_cntrs_hndl)) {
3214 ret = PTR_ERR(mcounters->hw_cntrs_hndl);
3221 /* counters already bound to at least one flow */
3222 if (mcounters->cntrs_max_index) {
3227 ret = counters_set_description(ibcounters,
3228 MLX5_IB_COUNTERS_FLOW,
3230 cntrs_data->ncounters);
3234 } else if (!mcounters->cntrs_max_index) {
3235 /* counters not bound yet, must have udata passed */
3244 mlx5_fc_destroy(to_mdev(ibcounters->device)->mdev,
3245 mcounters->hw_cntrs_hndl);
3246 mcounters->hw_cntrs_hndl = NULL;
3253 static struct mlx5_ib_flow_handler *_create_flow_rule(struct mlx5_ib_dev *dev,
3254 struct mlx5_ib_flow_prio *ft_prio,
3255 const struct ib_flow_attr *flow_attr,
3256 struct mlx5_flow_destination *dst,
3258 struct mlx5_ib_create_flow *ucmd)
3260 struct mlx5_flow_table *ft = ft_prio->flow_table;
3261 struct mlx5_ib_flow_handler *handler;
3262 struct mlx5_flow_act flow_act = {.flow_tag = MLX5_FS_DEFAULT_FLOW_TAG};
3263 struct mlx5_flow_spec *spec;
3264 struct mlx5_flow_destination dest_arr[2] = {};
3265 struct mlx5_flow_destination *rule_dst = dest_arr;
3266 const void *ib_flow = (const void *)flow_attr + sizeof(*flow_attr);
3267 unsigned int spec_index;
3271 bool is_egress = flow_attr->flags & IB_FLOW_ATTR_FLAGS_EGRESS;
3273 if (!is_valid_attr(dev->mdev, flow_attr))
3274 return ERR_PTR(-EINVAL);
3276 if (dev->rep && is_egress)
3277 return ERR_PTR(-EINVAL);
3279 spec = kvzalloc(sizeof(*spec), GFP_KERNEL);
3280 handler = kzalloc(sizeof(*handler), GFP_KERNEL);
3281 if (!handler || !spec) {
3286 INIT_LIST_HEAD(&handler->list);
3288 memcpy(&dest_arr[0], dst, sizeof(*dst));
3292 for (spec_index = 0; spec_index < flow_attr->num_of_specs; spec_index++) {
3293 err = parse_flow_attr(dev->mdev, spec->match_criteria,
3295 ib_flow, flow_attr, &flow_act,
3300 prev_type = ((union ib_flow_spec *)ib_flow)->type;
3301 ib_flow += ((union ib_flow_spec *)ib_flow)->size;
3304 if (!flow_is_multicast_only(flow_attr))
3305 set_underlay_qp(dev, spec, underlay_qpn);
3310 misc = MLX5_ADDR_OF(fte_match_param, spec->match_value,
3312 MLX5_SET(fte_match_set_misc, misc, source_port,
3314 misc = MLX5_ADDR_OF(fte_match_param, spec->match_criteria,
3316 MLX5_SET_TO_ONES(fte_match_set_misc, misc, source_port);
3319 spec->match_criteria_enable = get_match_criteria_enable(spec->match_criteria);
3322 !is_valid_spec(dev->mdev, spec, &flow_act, is_egress)) {
3327 if (flow_act.action & MLX5_FLOW_CONTEXT_ACTION_COUNT) {
3328 struct mlx5_ib_mcounters *mcounters;
3330 err = flow_counters_set_data(flow_act.counters, ucmd);
3334 mcounters = to_mcounters(flow_act.counters);
3335 handler->ibcounters = flow_act.counters;
3336 dest_arr[dest_num].type =
3337 MLX5_FLOW_DESTINATION_TYPE_COUNTER;
3338 dest_arr[dest_num].counter_id =
3339 mlx5_fc_id(mcounters->hw_cntrs_hndl);
3343 if (flow_act.action & MLX5_FLOW_CONTEXT_ACTION_DROP) {
3344 if (!(flow_act.action & MLX5_FLOW_CONTEXT_ACTION_COUNT)) {
3350 flow_act.action |= MLX5_FLOW_CONTEXT_ACTION_ALLOW;
3353 dest_num ? MLX5_FLOW_CONTEXT_ACTION_FWD_DEST :
3354 MLX5_FLOW_CONTEXT_ACTION_FWD_NEXT_PRIO;
3357 if ((flow_act.flags & FLOW_ACT_HAS_TAG) &&
3358 (flow_attr->type == IB_FLOW_ATTR_ALL_DEFAULT ||
3359 flow_attr->type == IB_FLOW_ATTR_MC_DEFAULT)) {
3360 mlx5_ib_warn(dev, "Flow tag %u and attribute type %x isn't allowed in leftovers\n",
3361 flow_act.flow_tag, flow_attr->type);
3365 handler->rule = mlx5_add_flow_rules(ft, spec,
3367 rule_dst, dest_num);
3369 if (IS_ERR(handler->rule)) {
3370 err = PTR_ERR(handler->rule);
3374 ft_prio->refcount++;
3375 handler->prio = ft_prio;
3378 ft_prio->flow_table = ft;
3380 if (err && handler) {
3381 if (handler->ibcounters &&
3382 atomic_read(&handler->ibcounters->usecnt) == 1)
3383 counters_clear_description(handler->ibcounters);
3387 return err ? ERR_PTR(err) : handler;
3390 static struct mlx5_ib_flow_handler *create_flow_rule(struct mlx5_ib_dev *dev,
3391 struct mlx5_ib_flow_prio *ft_prio,
3392 const struct ib_flow_attr *flow_attr,
3393 struct mlx5_flow_destination *dst)
3395 return _create_flow_rule(dev, ft_prio, flow_attr, dst, 0, NULL);
3398 static struct mlx5_ib_flow_handler *create_dont_trap_rule(struct mlx5_ib_dev *dev,
3399 struct mlx5_ib_flow_prio *ft_prio,
3400 struct ib_flow_attr *flow_attr,
3401 struct mlx5_flow_destination *dst)
3403 struct mlx5_ib_flow_handler *handler_dst = NULL;
3404 struct mlx5_ib_flow_handler *handler = NULL;
3406 handler = create_flow_rule(dev, ft_prio, flow_attr, NULL);
3407 if (!IS_ERR(handler)) {
3408 handler_dst = create_flow_rule(dev, ft_prio,
3410 if (IS_ERR(handler_dst)) {
3411 mlx5_del_flow_rules(handler->rule);
3412 ft_prio->refcount--;
3414 handler = handler_dst;
3416 list_add(&handler_dst->list, &handler->list);
3427 static struct mlx5_ib_flow_handler *create_leftovers_rule(struct mlx5_ib_dev *dev,
3428 struct mlx5_ib_flow_prio *ft_prio,
3429 struct ib_flow_attr *flow_attr,
3430 struct mlx5_flow_destination *dst)
3432 struct mlx5_ib_flow_handler *handler_ucast = NULL;
3433 struct mlx5_ib_flow_handler *handler = NULL;
3436 struct ib_flow_attr flow_attr;
3437 struct ib_flow_spec_eth eth_flow;
3438 } leftovers_specs[] = {
3442 .size = sizeof(leftovers_specs[0])
3445 .type = IB_FLOW_SPEC_ETH,
3446 .size = sizeof(struct ib_flow_spec_eth),
3447 .mask = {.dst_mac = {0x1} },
3448 .val = {.dst_mac = {0x1} }
3454 .size = sizeof(leftovers_specs[0])
3457 .type = IB_FLOW_SPEC_ETH,
3458 .size = sizeof(struct ib_flow_spec_eth),
3459 .mask = {.dst_mac = {0x1} },
3460 .val = {.dst_mac = {} }
3465 handler = create_flow_rule(dev, ft_prio,
3466 &leftovers_specs[LEFTOVERS_MC].flow_attr,
3468 if (!IS_ERR(handler) &&
3469 flow_attr->type == IB_FLOW_ATTR_ALL_DEFAULT) {
3470 handler_ucast = create_flow_rule(dev, ft_prio,
3471 &leftovers_specs[LEFTOVERS_UC].flow_attr,
3473 if (IS_ERR(handler_ucast)) {
3474 mlx5_del_flow_rules(handler->rule);
3475 ft_prio->refcount--;
3477 handler = handler_ucast;
3479 list_add(&handler_ucast->list, &handler->list);
3486 static struct mlx5_ib_flow_handler *create_sniffer_rule(struct mlx5_ib_dev *dev,
3487 struct mlx5_ib_flow_prio *ft_rx,
3488 struct mlx5_ib_flow_prio *ft_tx,
3489 struct mlx5_flow_destination *dst)
3491 struct mlx5_ib_flow_handler *handler_rx;
3492 struct mlx5_ib_flow_handler *handler_tx;
3494 static const struct ib_flow_attr flow_attr = {
3496 .size = sizeof(flow_attr)
3499 handler_rx = create_flow_rule(dev, ft_rx, &flow_attr, dst);
3500 if (IS_ERR(handler_rx)) {
3501 err = PTR_ERR(handler_rx);
3505 handler_tx = create_flow_rule(dev, ft_tx, &flow_attr, dst);
3506 if (IS_ERR(handler_tx)) {
3507 err = PTR_ERR(handler_tx);
3511 list_add(&handler_tx->list, &handler_rx->list);
3516 mlx5_del_flow_rules(handler_rx->rule);
3520 return ERR_PTR(err);
3523 static struct ib_flow *mlx5_ib_create_flow(struct ib_qp *qp,
3524 struct ib_flow_attr *flow_attr,
3526 struct ib_udata *udata)
3528 struct mlx5_ib_dev *dev = to_mdev(qp->device);
3529 struct mlx5_ib_qp *mqp = to_mqp(qp);
3530 struct mlx5_ib_flow_handler *handler = NULL;
3531 struct mlx5_flow_destination *dst = NULL;
3532 struct mlx5_ib_flow_prio *ft_prio_tx = NULL;
3533 struct mlx5_ib_flow_prio *ft_prio;
3534 bool is_egress = flow_attr->flags & IB_FLOW_ATTR_FLAGS_EGRESS;
3535 struct mlx5_ib_create_flow *ucmd = NULL, ucmd_hdr;
3536 size_t min_ucmd_sz, required_ucmd_sz;
3540 if (udata && udata->inlen) {
3541 min_ucmd_sz = offsetof(typeof(ucmd_hdr), reserved) +
3542 sizeof(ucmd_hdr.reserved);
3543 if (udata->inlen < min_ucmd_sz)
3544 return ERR_PTR(-EOPNOTSUPP);
3546 err = ib_copy_from_udata(&ucmd_hdr, udata, min_ucmd_sz);
3548 return ERR_PTR(err);
3550 /* currently supports only one counters data */
3551 if (ucmd_hdr.ncounters_data > 1)
3552 return ERR_PTR(-EINVAL);
3554 required_ucmd_sz = min_ucmd_sz +
3555 sizeof(struct mlx5_ib_flow_counters_data) *
3556 ucmd_hdr.ncounters_data;
3557 if (udata->inlen > required_ucmd_sz &&
3558 !ib_is_udata_cleared(udata, required_ucmd_sz,
3559 udata->inlen - required_ucmd_sz))
3560 return ERR_PTR(-EOPNOTSUPP);
3562 ucmd = kzalloc(required_ucmd_sz, GFP_KERNEL);
3564 return ERR_PTR(-ENOMEM);
3566 err = ib_copy_from_udata(ucmd, udata, required_ucmd_sz);
3571 if (flow_attr->priority > MLX5_IB_FLOW_LAST_PRIO) {
3576 if (domain != IB_FLOW_DOMAIN_USER ||
3577 flow_attr->port > dev->num_ports ||
3578 (flow_attr->flags & ~(IB_FLOW_ATTR_FLAGS_DONT_TRAP |
3579 IB_FLOW_ATTR_FLAGS_EGRESS))) {
3585 (flow_attr->type == IB_FLOW_ATTR_ALL_DEFAULT ||
3586 flow_attr->type == IB_FLOW_ATTR_MC_DEFAULT)) {
3591 dst = kzalloc(sizeof(*dst), GFP_KERNEL);
3597 mutex_lock(&dev->flow_db->lock);
3599 ft_prio = get_flow_table(dev, flow_attr,
3600 is_egress ? MLX5_IB_FT_TX : MLX5_IB_FT_RX);
3601 if (IS_ERR(ft_prio)) {
3602 err = PTR_ERR(ft_prio);
3605 if (flow_attr->type == IB_FLOW_ATTR_SNIFFER) {
3606 ft_prio_tx = get_flow_table(dev, flow_attr, MLX5_IB_FT_TX);
3607 if (IS_ERR(ft_prio_tx)) {
3608 err = PTR_ERR(ft_prio_tx);
3615 dst->type = MLX5_FLOW_DESTINATION_TYPE_PORT;
3617 dst->type = MLX5_FLOW_DESTINATION_TYPE_TIR;
3618 if (mqp->flags & MLX5_IB_QP_RSS)
3619 dst->tir_num = mqp->rss_qp.tirn;
3621 dst->tir_num = mqp->raw_packet_qp.rq.tirn;
3624 if (flow_attr->type == IB_FLOW_ATTR_NORMAL) {
3625 if (flow_attr->flags & IB_FLOW_ATTR_FLAGS_DONT_TRAP) {
3626 handler = create_dont_trap_rule(dev, ft_prio,
3629 underlay_qpn = (mqp->flags & MLX5_IB_QP_UNDERLAY) ?
3630 mqp->underlay_qpn : 0;
3631 handler = _create_flow_rule(dev, ft_prio, flow_attr,
3632 dst, underlay_qpn, ucmd);
3634 } else if (flow_attr->type == IB_FLOW_ATTR_ALL_DEFAULT ||
3635 flow_attr->type == IB_FLOW_ATTR_MC_DEFAULT) {
3636 handler = create_leftovers_rule(dev, ft_prio, flow_attr,
3638 } else if (flow_attr->type == IB_FLOW_ATTR_SNIFFER) {
3639 handler = create_sniffer_rule(dev, ft_prio, ft_prio_tx, dst);
3645 if (IS_ERR(handler)) {
3646 err = PTR_ERR(handler);
3651 mutex_unlock(&dev->flow_db->lock);
3655 return &handler->ibflow;
3658 put_flow_table(dev, ft_prio, false);
3660 put_flow_table(dev, ft_prio_tx, false);
3662 mutex_unlock(&dev->flow_db->lock);
3666 return ERR_PTR(err);
3669 static struct mlx5_ib_flow_prio *
3670 _get_flow_table(struct mlx5_ib_dev *dev,
3671 struct mlx5_ib_flow_matcher *fs_matcher,
3674 struct mlx5_flow_namespace *ns = NULL;
3675 struct mlx5_ib_flow_prio *prio;
3680 if (fs_matcher->ns_type == MLX5_FLOW_NAMESPACE_BYPASS) {
3681 max_table_size = BIT(MLX5_CAP_FLOWTABLE_NIC_RX(dev->mdev,
3683 if (MLX5_CAP_FLOWTABLE_NIC_RX(dev->mdev, decap))
3684 flags |= MLX5_FLOW_TABLE_TUNNEL_EN_DECAP;
3685 if (MLX5_CAP_FLOWTABLE_NIC_RX(dev->mdev,
3686 reformat_l3_tunnel_to_l2))
3687 flags |= MLX5_FLOW_TABLE_TUNNEL_EN_REFORMAT;
3688 } else { /* Can only be MLX5_FLOW_NAMESPACE_EGRESS */
3689 max_table_size = BIT(MLX5_CAP_FLOWTABLE_NIC_TX(dev->mdev,
3691 if (MLX5_CAP_FLOWTABLE_NIC_TX(dev->mdev, reformat))
3692 flags |= MLX5_FLOW_TABLE_TUNNEL_EN_REFORMAT;
3695 if (max_table_size < MLX5_FS_MAX_ENTRIES)
3696 return ERR_PTR(-ENOMEM);
3699 priority = MLX5_IB_FLOW_MCAST_PRIO;
3701 priority = ib_prio_to_core_prio(fs_matcher->priority, false);
3703 ns = mlx5_get_flow_namespace(dev->mdev, fs_matcher->ns_type);
3705 return ERR_PTR(-ENOTSUPP);
3707 if (fs_matcher->ns_type == MLX5_FLOW_NAMESPACE_BYPASS)
3708 prio = &dev->flow_db->prios[priority];
3710 prio = &dev->flow_db->egress_prios[priority];
3712 if (prio->flow_table)
3715 return _get_prio(ns, prio, priority, MLX5_FS_MAX_ENTRIES,
3716 MLX5_FS_MAX_TYPES, flags);
3719 static struct mlx5_ib_flow_handler *
3720 _create_raw_flow_rule(struct mlx5_ib_dev *dev,
3721 struct mlx5_ib_flow_prio *ft_prio,
3722 struct mlx5_flow_destination *dst,
3723 struct mlx5_ib_flow_matcher *fs_matcher,
3724 struct mlx5_flow_act *flow_act,
3725 void *cmd_in, int inlen,
3728 struct mlx5_ib_flow_handler *handler;
3729 struct mlx5_flow_spec *spec;
3730 struct mlx5_flow_table *ft = ft_prio->flow_table;
3733 spec = kvzalloc(sizeof(*spec), GFP_KERNEL);
3734 handler = kzalloc(sizeof(*handler), GFP_KERNEL);
3735 if (!handler || !spec) {
3740 INIT_LIST_HEAD(&handler->list);
3742 memcpy(spec->match_value, cmd_in, inlen);
3743 memcpy(spec->match_criteria, fs_matcher->matcher_mask.match_params,
3744 fs_matcher->mask_len);
3745 spec->match_criteria_enable = fs_matcher->match_criteria_enable;
3747 handler->rule = mlx5_add_flow_rules(ft, spec,
3748 flow_act, dst, dst_num);
3750 if (IS_ERR(handler->rule)) {
3751 err = PTR_ERR(handler->rule);
3755 ft_prio->refcount++;
3756 handler->prio = ft_prio;
3758 ft_prio->flow_table = ft;
3764 return err ? ERR_PTR(err) : handler;
3767 static bool raw_fs_is_multicast(struct mlx5_ib_flow_matcher *fs_matcher,
3771 void *match_v_set_lyr_2_4, *match_c_set_lyr_2_4;
3772 void *dmac, *dmac_mask;
3773 void *ipv4, *ipv4_mask;
3775 if (!(fs_matcher->match_criteria_enable &
3776 (1 << MATCH_CRITERIA_ENABLE_OUTER_BIT)))
3779 match_c = fs_matcher->matcher_mask.match_params;
3780 match_v_set_lyr_2_4 = MLX5_ADDR_OF(fte_match_param, match_v,
3782 match_c_set_lyr_2_4 = MLX5_ADDR_OF(fte_match_param, match_c,
3785 dmac = MLX5_ADDR_OF(fte_match_set_lyr_2_4, match_v_set_lyr_2_4,
3787 dmac_mask = MLX5_ADDR_OF(fte_match_set_lyr_2_4, match_c_set_lyr_2_4,
3790 if (is_multicast_ether_addr(dmac) &&
3791 is_multicast_ether_addr(dmac_mask))
3794 ipv4 = MLX5_ADDR_OF(fte_match_set_lyr_2_4, match_v_set_lyr_2_4,
3795 dst_ipv4_dst_ipv6.ipv4_layout.ipv4);
3797 ipv4_mask = MLX5_ADDR_OF(fte_match_set_lyr_2_4, match_c_set_lyr_2_4,
3798 dst_ipv4_dst_ipv6.ipv4_layout.ipv4);
3800 if (ipv4_is_multicast(*(__be32 *)(ipv4)) &&
3801 ipv4_is_multicast(*(__be32 *)(ipv4_mask)))
3807 struct mlx5_ib_flow_handler *
3808 mlx5_ib_raw_fs_rule_add(struct mlx5_ib_dev *dev,
3809 struct mlx5_ib_flow_matcher *fs_matcher,
3810 struct mlx5_flow_act *flow_act,
3812 void *cmd_in, int inlen, int dest_id,
3815 struct mlx5_flow_destination *dst;
3816 struct mlx5_ib_flow_prio *ft_prio;
3817 struct mlx5_ib_flow_handler *handler;
3822 if (fs_matcher->flow_type != MLX5_IB_FLOW_TYPE_NORMAL)
3823 return ERR_PTR(-EOPNOTSUPP);
3825 if (fs_matcher->priority > MLX5_IB_FLOW_LAST_PRIO)
3826 return ERR_PTR(-ENOMEM);
3828 dst = kzalloc(sizeof(*dst) * 2, GFP_KERNEL);
3830 return ERR_PTR(-ENOMEM);
3832 mcast = raw_fs_is_multicast(fs_matcher, cmd_in);
3833 mutex_lock(&dev->flow_db->lock);
3835 ft_prio = _get_flow_table(dev, fs_matcher, mcast);
3836 if (IS_ERR(ft_prio)) {
3837 err = PTR_ERR(ft_prio);
3841 if (dest_type == MLX5_FLOW_DESTINATION_TYPE_TIR) {
3842 dst[dst_num].type = dest_type;
3843 dst[dst_num].tir_num = dest_id;
3844 flow_act->action |= MLX5_FLOW_CONTEXT_ACTION_FWD_DEST;
3845 } else if (dest_type == MLX5_FLOW_DESTINATION_TYPE_FLOW_TABLE) {
3846 dst[dst_num].type = MLX5_FLOW_DESTINATION_TYPE_FLOW_TABLE_NUM;
3847 dst[dst_num].ft_num = dest_id;
3848 flow_act->action |= MLX5_FLOW_CONTEXT_ACTION_FWD_DEST;
3850 dst[dst_num].type = MLX5_FLOW_DESTINATION_TYPE_PORT;
3851 flow_act->action |= MLX5_FLOW_CONTEXT_ACTION_ALLOW;
3856 if (flow_act->action & MLX5_FLOW_CONTEXT_ACTION_COUNT) {
3857 dst[dst_num].type = MLX5_FLOW_DESTINATION_TYPE_COUNTER;
3858 dst[dst_num].counter_id = counter_id;
3862 handler = _create_raw_flow_rule(dev, ft_prio, dst, fs_matcher, flow_act,
3863 cmd_in, inlen, dst_num);
3865 if (IS_ERR(handler)) {
3866 err = PTR_ERR(handler);
3870 mutex_unlock(&dev->flow_db->lock);
3871 atomic_inc(&fs_matcher->usecnt);
3872 handler->flow_matcher = fs_matcher;
3879 put_flow_table(dev, ft_prio, false);
3881 mutex_unlock(&dev->flow_db->lock);
3884 return ERR_PTR(err);
3887 static u32 mlx5_ib_flow_action_flags_to_accel_xfrm_flags(u32 mlx5_flags)
3891 if (mlx5_flags & MLX5_IB_UAPI_FLOW_ACTION_FLAGS_REQUIRE_METADATA)
3892 flags |= MLX5_ACCEL_XFRM_FLAG_REQUIRE_METADATA;
3897 #define MLX5_FLOW_ACTION_ESP_CREATE_LAST_SUPPORTED MLX5_IB_UAPI_FLOW_ACTION_FLAGS_REQUIRE_METADATA
3898 static struct ib_flow_action *
3899 mlx5_ib_create_flow_action_esp(struct ib_device *device,
3900 const struct ib_flow_action_attrs_esp *attr,
3901 struct uverbs_attr_bundle *attrs)
3903 struct mlx5_ib_dev *mdev = to_mdev(device);
3904 struct ib_uverbs_flow_action_esp_keymat_aes_gcm *aes_gcm;
3905 struct mlx5_accel_esp_xfrm_attrs accel_attrs = {};
3906 struct mlx5_ib_flow_action *action;
3911 err = uverbs_get_flags64(
3912 &action_flags, attrs, MLX5_IB_ATTR_CREATE_FLOW_ACTION_FLAGS,
3913 ((MLX5_FLOW_ACTION_ESP_CREATE_LAST_SUPPORTED << 1) - 1));
3915 return ERR_PTR(err);
3917 flags = mlx5_ib_flow_action_flags_to_accel_xfrm_flags(action_flags);
3919 /* We current only support a subset of the standard features. Only a
3920 * keymat of type AES_GCM, with icv_len == 16, iv_algo == SEQ and esn
3921 * (with overlap). Full offload mode isn't supported.
3923 if (!attr->keymat || attr->replay || attr->encap ||
3924 attr->spi || attr->seq || attr->tfc_pad ||
3925 attr->hard_limit_pkts ||
3926 (attr->flags & ~(IB_FLOW_ACTION_ESP_FLAGS_ESN_TRIGGERED |
3927 IB_UVERBS_FLOW_ACTION_ESP_FLAGS_ENCRYPT)))
3928 return ERR_PTR(-EOPNOTSUPP);
3930 if (attr->keymat->protocol !=
3931 IB_UVERBS_FLOW_ACTION_ESP_KEYMAT_AES_GCM)
3932 return ERR_PTR(-EOPNOTSUPP);
3934 aes_gcm = &attr->keymat->keymat.aes_gcm;
3936 if (aes_gcm->icv_len != 16 ||
3937 aes_gcm->iv_algo != IB_UVERBS_FLOW_ACTION_IV_ALGO_SEQ)
3938 return ERR_PTR(-EOPNOTSUPP);
3940 action = kmalloc(sizeof(*action), GFP_KERNEL);
3942 return ERR_PTR(-ENOMEM);
3944 action->esp_aes_gcm.ib_flags = attr->flags;
3945 memcpy(&accel_attrs.keymat.aes_gcm.aes_key, &aes_gcm->aes_key,
3946 sizeof(accel_attrs.keymat.aes_gcm.aes_key));
3947 accel_attrs.keymat.aes_gcm.key_len = aes_gcm->key_len * 8;
3948 memcpy(&accel_attrs.keymat.aes_gcm.salt, &aes_gcm->salt,
3949 sizeof(accel_attrs.keymat.aes_gcm.salt));
3950 memcpy(&accel_attrs.keymat.aes_gcm.seq_iv, &aes_gcm->iv,
3951 sizeof(accel_attrs.keymat.aes_gcm.seq_iv));
3952 accel_attrs.keymat.aes_gcm.icv_len = aes_gcm->icv_len * 8;
3953 accel_attrs.keymat.aes_gcm.iv_algo = MLX5_ACCEL_ESP_AES_GCM_IV_ALGO_SEQ;
3954 accel_attrs.keymat_type = MLX5_ACCEL_ESP_KEYMAT_AES_GCM;
3956 accel_attrs.esn = attr->esn;
3957 if (attr->flags & IB_FLOW_ACTION_ESP_FLAGS_ESN_TRIGGERED)
3958 accel_attrs.flags |= MLX5_ACCEL_ESP_FLAGS_ESN_TRIGGERED;
3959 if (attr->flags & IB_UVERBS_FLOW_ACTION_ESP_FLAGS_ESN_NEW_WINDOW)
3960 accel_attrs.flags |= MLX5_ACCEL_ESP_FLAGS_ESN_STATE_OVERLAP;
3962 if (attr->flags & IB_UVERBS_FLOW_ACTION_ESP_FLAGS_ENCRYPT)
3963 accel_attrs.action |= MLX5_ACCEL_ESP_ACTION_ENCRYPT;
3965 action->esp_aes_gcm.ctx =
3966 mlx5_accel_esp_create_xfrm(mdev->mdev, &accel_attrs, flags);
3967 if (IS_ERR(action->esp_aes_gcm.ctx)) {
3968 err = PTR_ERR(action->esp_aes_gcm.ctx);
3972 action->esp_aes_gcm.ib_flags = attr->flags;
3974 return &action->ib_action;
3978 return ERR_PTR(err);
3982 mlx5_ib_modify_flow_action_esp(struct ib_flow_action *action,
3983 const struct ib_flow_action_attrs_esp *attr,
3984 struct uverbs_attr_bundle *attrs)
3986 struct mlx5_ib_flow_action *maction = to_mflow_act(action);
3987 struct mlx5_accel_esp_xfrm_attrs accel_attrs;
3990 if (attr->keymat || attr->replay || attr->encap ||
3991 attr->spi || attr->seq || attr->tfc_pad ||
3992 attr->hard_limit_pkts ||
3993 (attr->flags & ~(IB_FLOW_ACTION_ESP_FLAGS_ESN_TRIGGERED |
3994 IB_FLOW_ACTION_ESP_FLAGS_MOD_ESP_ATTRS |
3995 IB_UVERBS_FLOW_ACTION_ESP_FLAGS_ESN_NEW_WINDOW)))
3998 /* Only the ESN value or the MLX5_ACCEL_ESP_FLAGS_ESN_STATE_OVERLAP can
4001 if (!(maction->esp_aes_gcm.ib_flags &
4002 IB_FLOW_ACTION_ESP_FLAGS_ESN_TRIGGERED) &&
4003 attr->flags & (IB_FLOW_ACTION_ESP_FLAGS_ESN_TRIGGERED |
4004 IB_UVERBS_FLOW_ACTION_ESP_FLAGS_ESN_NEW_WINDOW))
4007 memcpy(&accel_attrs, &maction->esp_aes_gcm.ctx->attrs,
4008 sizeof(accel_attrs));
4010 accel_attrs.esn = attr->esn;
4011 if (attr->flags & IB_UVERBS_FLOW_ACTION_ESP_FLAGS_ESN_NEW_WINDOW)
4012 accel_attrs.flags |= MLX5_ACCEL_ESP_FLAGS_ESN_STATE_OVERLAP;
4014 accel_attrs.flags &= ~MLX5_ACCEL_ESP_FLAGS_ESN_STATE_OVERLAP;
4016 err = mlx5_accel_esp_modify_xfrm(maction->esp_aes_gcm.ctx,
4021 maction->esp_aes_gcm.ib_flags &=
4022 ~IB_UVERBS_FLOW_ACTION_ESP_FLAGS_ESN_NEW_WINDOW;
4023 maction->esp_aes_gcm.ib_flags |=
4024 attr->flags & IB_UVERBS_FLOW_ACTION_ESP_FLAGS_ESN_NEW_WINDOW;
4029 static int mlx5_ib_destroy_flow_action(struct ib_flow_action *action)
4031 struct mlx5_ib_flow_action *maction = to_mflow_act(action);
4033 switch (action->type) {
4034 case IB_FLOW_ACTION_ESP:
4036 * We only support aes_gcm by now, so we implicitly know this is
4037 * the underline crypto.
4039 mlx5_accel_esp_destroy_xfrm(maction->esp_aes_gcm.ctx);
4041 case IB_FLOW_ACTION_UNSPECIFIED:
4042 mlx5_ib_destroy_flow_action_raw(maction);
4053 static int mlx5_ib_mcg_attach(struct ib_qp *ibqp, union ib_gid *gid, u16 lid)
4055 struct mlx5_ib_dev *dev = to_mdev(ibqp->device);
4056 struct mlx5_ib_qp *mqp = to_mqp(ibqp);
4061 to_mpd(ibqp->pd)->uid : 0;
4063 if (mqp->flags & MLX5_IB_QP_UNDERLAY) {
4064 mlx5_ib_dbg(dev, "Attaching a multi cast group to underlay QP is not supported\n");
4068 err = mlx5_cmd_attach_mcg(dev->mdev, gid, ibqp->qp_num, uid);
4070 mlx5_ib_warn(dev, "failed attaching QPN 0x%x, MGID %pI6\n",
4071 ibqp->qp_num, gid->raw);
4076 static int mlx5_ib_mcg_detach(struct ib_qp *ibqp, union ib_gid *gid, u16 lid)
4078 struct mlx5_ib_dev *dev = to_mdev(ibqp->device);
4083 to_mpd(ibqp->pd)->uid : 0;
4084 err = mlx5_cmd_detach_mcg(dev->mdev, gid, ibqp->qp_num, uid);
4086 mlx5_ib_warn(dev, "failed detaching QPN 0x%x, MGID %pI6\n",
4087 ibqp->qp_num, gid->raw);
4092 static int init_node_data(struct mlx5_ib_dev *dev)
4096 err = mlx5_query_node_desc(dev, dev->ib_dev.node_desc);
4100 dev->mdev->rev_id = dev->mdev->pdev->revision;
4102 return mlx5_query_node_guid(dev, &dev->ib_dev.node_guid);
4105 static ssize_t fw_pages_show(struct device *device,
4106 struct device_attribute *attr, char *buf)
4108 struct mlx5_ib_dev *dev =
4109 container_of(device, struct mlx5_ib_dev, ib_dev.dev);
4111 return sprintf(buf, "%d\n", dev->mdev->priv.fw_pages);
4113 static DEVICE_ATTR_RO(fw_pages);
4115 static ssize_t reg_pages_show(struct device *device,
4116 struct device_attribute *attr, char *buf)
4118 struct mlx5_ib_dev *dev =
4119 container_of(device, struct mlx5_ib_dev, ib_dev.dev);
4121 return sprintf(buf, "%d\n", atomic_read(&dev->mdev->priv.reg_pages));
4123 static DEVICE_ATTR_RO(reg_pages);
4125 static ssize_t hca_type_show(struct device *device,
4126 struct device_attribute *attr, char *buf)
4128 struct mlx5_ib_dev *dev =
4129 container_of(device, struct mlx5_ib_dev, ib_dev.dev);
4130 return sprintf(buf, "MT%d\n", dev->mdev->pdev->device);
4132 static DEVICE_ATTR_RO(hca_type);
4134 static ssize_t hw_rev_show(struct device *device,
4135 struct device_attribute *attr, char *buf)
4137 struct mlx5_ib_dev *dev =
4138 container_of(device, struct mlx5_ib_dev, ib_dev.dev);
4139 return sprintf(buf, "%x\n", dev->mdev->rev_id);
4141 static DEVICE_ATTR_RO(hw_rev);
4143 static ssize_t board_id_show(struct device *device,
4144 struct device_attribute *attr, char *buf)
4146 struct mlx5_ib_dev *dev =
4147 container_of(device, struct mlx5_ib_dev, ib_dev.dev);
4148 return sprintf(buf, "%.*s\n", MLX5_BOARD_ID_LEN,
4149 dev->mdev->board_id);
4151 static DEVICE_ATTR_RO(board_id);
4153 static struct attribute *mlx5_class_attributes[] = {
4154 &dev_attr_hw_rev.attr,
4155 &dev_attr_hca_type.attr,
4156 &dev_attr_board_id.attr,
4157 &dev_attr_fw_pages.attr,
4158 &dev_attr_reg_pages.attr,
4162 static const struct attribute_group mlx5_attr_group = {
4163 .attrs = mlx5_class_attributes,
4166 static void pkey_change_handler(struct work_struct *work)
4168 struct mlx5_ib_port_resources *ports =
4169 container_of(work, struct mlx5_ib_port_resources,
4172 mutex_lock(&ports->devr->mutex);
4173 mlx5_ib_gsi_pkey_change(ports->gsi);
4174 mutex_unlock(&ports->devr->mutex);
4177 static void mlx5_ib_handle_internal_error(struct mlx5_ib_dev *ibdev)
4179 struct mlx5_ib_qp *mqp;
4180 struct mlx5_ib_cq *send_mcq, *recv_mcq;
4181 struct mlx5_core_cq *mcq;
4182 struct list_head cq_armed_list;
4183 unsigned long flags_qp;
4184 unsigned long flags_cq;
4185 unsigned long flags;
4187 INIT_LIST_HEAD(&cq_armed_list);
4189 /* Go over qp list reside on that ibdev, sync with create/destroy qp.*/
4190 spin_lock_irqsave(&ibdev->reset_flow_resource_lock, flags);
4191 list_for_each_entry(mqp, &ibdev->qp_list, qps_list) {
4192 spin_lock_irqsave(&mqp->sq.lock, flags_qp);
4193 if (mqp->sq.tail != mqp->sq.head) {
4194 send_mcq = to_mcq(mqp->ibqp.send_cq);
4195 spin_lock_irqsave(&send_mcq->lock, flags_cq);
4196 if (send_mcq->mcq.comp &&
4197 mqp->ibqp.send_cq->comp_handler) {
4198 if (!send_mcq->mcq.reset_notify_added) {
4199 send_mcq->mcq.reset_notify_added = 1;
4200 list_add_tail(&send_mcq->mcq.reset_notify,
4204 spin_unlock_irqrestore(&send_mcq->lock, flags_cq);
4206 spin_unlock_irqrestore(&mqp->sq.lock, flags_qp);
4207 spin_lock_irqsave(&mqp->rq.lock, flags_qp);
4208 /* no handling is needed for SRQ */
4209 if (!mqp->ibqp.srq) {
4210 if (mqp->rq.tail != mqp->rq.head) {
4211 recv_mcq = to_mcq(mqp->ibqp.recv_cq);
4212 spin_lock_irqsave(&recv_mcq->lock, flags_cq);
4213 if (recv_mcq->mcq.comp &&
4214 mqp->ibqp.recv_cq->comp_handler) {
4215 if (!recv_mcq->mcq.reset_notify_added) {
4216 recv_mcq->mcq.reset_notify_added = 1;
4217 list_add_tail(&recv_mcq->mcq.reset_notify,
4221 spin_unlock_irqrestore(&recv_mcq->lock,
4225 spin_unlock_irqrestore(&mqp->rq.lock, flags_qp);
4227 /*At that point all inflight post send were put to be executed as of we
4228 * lock/unlock above locks Now need to arm all involved CQs.
4230 list_for_each_entry(mcq, &cq_armed_list, reset_notify) {
4233 spin_unlock_irqrestore(&ibdev->reset_flow_resource_lock, flags);
4236 static void delay_drop_handler(struct work_struct *work)
4239 struct mlx5_ib_delay_drop *delay_drop =
4240 container_of(work, struct mlx5_ib_delay_drop,
4243 atomic_inc(&delay_drop->events_cnt);
4245 mutex_lock(&delay_drop->lock);
4246 err = mlx5_core_set_delay_drop(delay_drop->dev->mdev,
4247 delay_drop->timeout);
4249 mlx5_ib_warn(delay_drop->dev, "Failed to set delay drop, timeout=%u\n",
4250 delay_drop->timeout);
4251 delay_drop->activate = false;
4253 mutex_unlock(&delay_drop->lock);
4256 static void handle_general_event(struct mlx5_ib_dev *ibdev, struct mlx5_eqe *eqe,
4257 struct ib_event *ibev)
4259 switch (eqe->sub_type) {
4260 case MLX5_GENERAL_SUBTYPE_DELAY_DROP_TIMEOUT:
4261 schedule_work(&ibdev->delay_drop.delay_drop_work);
4263 default: /* do nothing */
4268 static int handle_port_change(struct mlx5_ib_dev *ibdev, struct mlx5_eqe *eqe,
4269 struct ib_event *ibev)
4271 u8 port = (eqe->data.port.port >> 4) & 0xf;
4273 ibev->element.port_num = port;
4275 switch (eqe->sub_type) {
4276 case MLX5_PORT_CHANGE_SUBTYPE_ACTIVE:
4277 case MLX5_PORT_CHANGE_SUBTYPE_DOWN:
4278 case MLX5_PORT_CHANGE_SUBTYPE_INITIALIZED:
4279 /* In RoCE, port up/down events are handled in
4280 * mlx5_netdev_event().
4282 if (mlx5_ib_port_link_layer(&ibdev->ib_dev, port) ==
4283 IB_LINK_LAYER_ETHERNET)
4286 ibev->event = (eqe->sub_type == MLX5_PORT_CHANGE_SUBTYPE_ACTIVE) ?
4287 IB_EVENT_PORT_ACTIVE : IB_EVENT_PORT_ERR;
4290 case MLX5_PORT_CHANGE_SUBTYPE_LID:
4291 ibev->event = IB_EVENT_LID_CHANGE;
4294 case MLX5_PORT_CHANGE_SUBTYPE_PKEY:
4295 ibev->event = IB_EVENT_PKEY_CHANGE;
4296 schedule_work(&ibdev->devr.ports[port - 1].pkey_change_work);
4299 case MLX5_PORT_CHANGE_SUBTYPE_GUID:
4300 ibev->event = IB_EVENT_GID_CHANGE;
4303 case MLX5_PORT_CHANGE_SUBTYPE_CLIENT_REREG:
4304 ibev->event = IB_EVENT_CLIENT_REREGISTER;
4313 static void mlx5_ib_handle_event(struct work_struct *_work)
4315 struct mlx5_ib_event_work *work =
4316 container_of(_work, struct mlx5_ib_event_work, work);
4317 struct mlx5_ib_dev *ibdev;
4318 struct ib_event ibev;
4321 if (work->is_slave) {
4322 ibdev = mlx5_ib_get_ibdev_from_mpi(work->mpi);
4329 switch (work->event) {
4330 case MLX5_DEV_EVENT_SYS_ERROR:
4331 ibev.event = IB_EVENT_DEVICE_FATAL;
4332 mlx5_ib_handle_internal_error(ibdev);
4333 ibev.element.port_num = (u8)(unsigned long)work->param;
4336 case MLX5_EVENT_TYPE_PORT_CHANGE:
4337 if (handle_port_change(ibdev, work->param, &ibev))
4340 case MLX5_EVENT_TYPE_GENERAL_EVENT:
4341 handle_general_event(ibdev, work->param, &ibev);
4347 ibev.device = &ibdev->ib_dev;
4349 if (!rdma_is_port_valid(&ibdev->ib_dev, ibev.element.port_num)) {
4350 mlx5_ib_warn(ibdev, "warning: event on port %d\n", ibev.element.port_num);
4354 if (ibdev->ib_active)
4355 ib_dispatch_event(&ibev);
4358 ibdev->ib_active = false;
4363 static int mlx5_ib_event(struct notifier_block *nb,
4364 unsigned long event, void *param)
4366 struct mlx5_ib_event_work *work;
4368 work = kmalloc(sizeof(*work), GFP_ATOMIC);
4372 INIT_WORK(&work->work, mlx5_ib_handle_event);
4373 work->dev = container_of(nb, struct mlx5_ib_dev, mdev_events);
4374 work->is_slave = false;
4375 work->param = param;
4376 work->event = event;
4378 queue_work(mlx5_ib_event_wq, &work->work);
4383 static int mlx5_ib_event_slave_port(struct notifier_block *nb,
4384 unsigned long event, void *param)
4386 struct mlx5_ib_event_work *work;
4388 work = kmalloc(sizeof(*work), GFP_ATOMIC);
4392 INIT_WORK(&work->work, mlx5_ib_handle_event);
4393 work->mpi = container_of(nb, struct mlx5_ib_multiport_info, mdev_events);
4394 work->is_slave = true;
4395 work->param = param;
4396 work->event = event;
4397 queue_work(mlx5_ib_event_wq, &work->work);
4402 static int set_has_smi_cap(struct mlx5_ib_dev *dev)
4404 struct mlx5_hca_vport_context vport_ctx;
4408 for (port = 1; port <= dev->num_ports; port++) {
4409 dev->mdev->port_caps[port - 1].has_smi = false;
4410 if (MLX5_CAP_GEN(dev->mdev, port_type) ==
4411 MLX5_CAP_PORT_TYPE_IB) {
4412 if (MLX5_CAP_GEN(dev->mdev, ib_virt)) {
4413 err = mlx5_query_hca_vport_context(dev->mdev, 0,
4417 mlx5_ib_err(dev, "query_hca_vport_context for port=%d failed %d\n",
4421 dev->mdev->port_caps[port - 1].has_smi =
4424 dev->mdev->port_caps[port - 1].has_smi = true;
4431 static void get_ext_port_caps(struct mlx5_ib_dev *dev)
4435 for (port = 1; port <= dev->num_ports; port++)
4436 mlx5_query_ext_port_caps(dev, port);
4439 static int get_port_caps(struct mlx5_ib_dev *dev, u8 port)
4441 struct ib_device_attr *dprops = NULL;
4442 struct ib_port_attr *pprops = NULL;
4444 struct ib_udata uhw = {.inlen = 0, .outlen = 0};
4446 pprops = kmalloc(sizeof(*pprops), GFP_KERNEL);
4450 dprops = kmalloc(sizeof(*dprops), GFP_KERNEL);
4454 err = set_has_smi_cap(dev);
4458 err = mlx5_ib_query_device(&dev->ib_dev, dprops, &uhw);
4460 mlx5_ib_warn(dev, "query_device failed %d\n", err);
4464 memset(pprops, 0, sizeof(*pprops));
4465 err = mlx5_ib_query_port(&dev->ib_dev, port, pprops);
4467 mlx5_ib_warn(dev, "query_port %d failed %d\n",
4472 dev->mdev->port_caps[port - 1].pkey_table_len =
4474 dev->mdev->port_caps[port - 1].gid_table_len =
4475 pprops->gid_tbl_len;
4476 mlx5_ib_dbg(dev, "port %d: pkey_table_len %d, gid_table_len %d\n",
4477 port, dprops->max_pkeys, pprops->gid_tbl_len);
4486 static void destroy_umrc_res(struct mlx5_ib_dev *dev)
4490 err = mlx5_mr_cache_cleanup(dev);
4492 mlx5_ib_warn(dev, "mr cache cleanup failed\n");
4495 mlx5_ib_destroy_qp(dev->umrc.qp);
4497 ib_free_cq(dev->umrc.cq);
4499 ib_dealloc_pd(dev->umrc.pd);
4506 static int create_umr_res(struct mlx5_ib_dev *dev)
4508 struct ib_qp_init_attr *init_attr = NULL;
4509 struct ib_qp_attr *attr = NULL;
4515 attr = kzalloc(sizeof(*attr), GFP_KERNEL);
4516 init_attr = kzalloc(sizeof(*init_attr), GFP_KERNEL);
4517 if (!attr || !init_attr) {
4522 pd = ib_alloc_pd(&dev->ib_dev, 0);
4524 mlx5_ib_dbg(dev, "Couldn't create PD for sync UMR QP\n");
4529 cq = ib_alloc_cq(&dev->ib_dev, NULL, 128, 0, IB_POLL_SOFTIRQ);
4531 mlx5_ib_dbg(dev, "Couldn't create CQ for sync UMR QP\n");
4536 init_attr->send_cq = cq;
4537 init_attr->recv_cq = cq;
4538 init_attr->sq_sig_type = IB_SIGNAL_ALL_WR;
4539 init_attr->cap.max_send_wr = MAX_UMR_WR;
4540 init_attr->cap.max_send_sge = 1;
4541 init_attr->qp_type = MLX5_IB_QPT_REG_UMR;
4542 init_attr->port_num = 1;
4543 qp = mlx5_ib_create_qp(pd, init_attr, NULL);
4545 mlx5_ib_dbg(dev, "Couldn't create sync UMR QP\n");
4549 qp->device = &dev->ib_dev;
4552 qp->qp_type = MLX5_IB_QPT_REG_UMR;
4553 qp->send_cq = init_attr->send_cq;
4554 qp->recv_cq = init_attr->recv_cq;
4556 attr->qp_state = IB_QPS_INIT;
4558 ret = mlx5_ib_modify_qp(qp, attr, IB_QP_STATE | IB_QP_PKEY_INDEX |
4561 mlx5_ib_dbg(dev, "Couldn't modify UMR QP\n");
4565 memset(attr, 0, sizeof(*attr));
4566 attr->qp_state = IB_QPS_RTR;
4567 attr->path_mtu = IB_MTU_256;
4569 ret = mlx5_ib_modify_qp(qp, attr, IB_QP_STATE, NULL);
4571 mlx5_ib_dbg(dev, "Couldn't modify umr QP to rtr\n");
4575 memset(attr, 0, sizeof(*attr));
4576 attr->qp_state = IB_QPS_RTS;
4577 ret = mlx5_ib_modify_qp(qp, attr, IB_QP_STATE, NULL);
4579 mlx5_ib_dbg(dev, "Couldn't modify umr QP to rts\n");
4587 sema_init(&dev->umrc.sem, MAX_UMR_WR);
4588 ret = mlx5_mr_cache_init(dev);
4590 mlx5_ib_warn(dev, "mr cache init failed %d\n", ret);
4600 mlx5_ib_destroy_qp(qp);
4601 dev->umrc.qp = NULL;
4605 dev->umrc.cq = NULL;
4609 dev->umrc.pd = NULL;
4617 static u8 mlx5_get_umr_fence(u8 umr_fence_cap)
4619 switch (umr_fence_cap) {
4620 case MLX5_CAP_UMR_FENCE_NONE:
4621 return MLX5_FENCE_MODE_NONE;
4622 case MLX5_CAP_UMR_FENCE_SMALL:
4623 return MLX5_FENCE_MODE_INITIATOR_SMALL;
4625 return MLX5_FENCE_MODE_STRONG_ORDERING;
4629 static int create_dev_resources(struct mlx5_ib_resources *devr)
4631 struct ib_srq_init_attr attr;
4632 struct mlx5_ib_dev *dev;
4633 struct ib_cq_init_attr cq_attr = {.cqe = 1};
4637 dev = container_of(devr, struct mlx5_ib_dev, devr);
4639 mutex_init(&devr->mutex);
4641 devr->p0 = mlx5_ib_alloc_pd(&dev->ib_dev, NULL, NULL);
4642 if (IS_ERR(devr->p0)) {
4643 ret = PTR_ERR(devr->p0);
4646 devr->p0->device = &dev->ib_dev;
4647 devr->p0->uobject = NULL;
4648 atomic_set(&devr->p0->usecnt, 0);
4650 devr->c0 = mlx5_ib_create_cq(&dev->ib_dev, &cq_attr, NULL, NULL);
4651 if (IS_ERR(devr->c0)) {
4652 ret = PTR_ERR(devr->c0);
4655 devr->c0->device = &dev->ib_dev;
4656 devr->c0->uobject = NULL;
4657 devr->c0->comp_handler = NULL;
4658 devr->c0->event_handler = NULL;
4659 devr->c0->cq_context = NULL;
4660 atomic_set(&devr->c0->usecnt, 0);
4662 devr->x0 = mlx5_ib_alloc_xrcd(&dev->ib_dev, NULL, NULL);
4663 if (IS_ERR(devr->x0)) {
4664 ret = PTR_ERR(devr->x0);
4667 devr->x0->device = &dev->ib_dev;
4668 devr->x0->inode = NULL;
4669 atomic_set(&devr->x0->usecnt, 0);
4670 mutex_init(&devr->x0->tgt_qp_mutex);
4671 INIT_LIST_HEAD(&devr->x0->tgt_qp_list);
4673 devr->x1 = mlx5_ib_alloc_xrcd(&dev->ib_dev, NULL, NULL);
4674 if (IS_ERR(devr->x1)) {
4675 ret = PTR_ERR(devr->x1);
4678 devr->x1->device = &dev->ib_dev;
4679 devr->x1->inode = NULL;
4680 atomic_set(&devr->x1->usecnt, 0);
4681 mutex_init(&devr->x1->tgt_qp_mutex);
4682 INIT_LIST_HEAD(&devr->x1->tgt_qp_list);
4684 memset(&attr, 0, sizeof(attr));
4685 attr.attr.max_sge = 1;
4686 attr.attr.max_wr = 1;
4687 attr.srq_type = IB_SRQT_XRC;
4688 attr.ext.cq = devr->c0;
4689 attr.ext.xrc.xrcd = devr->x0;
4691 devr->s0 = mlx5_ib_create_srq(devr->p0, &attr, NULL);
4692 if (IS_ERR(devr->s0)) {
4693 ret = PTR_ERR(devr->s0);
4696 devr->s0->device = &dev->ib_dev;
4697 devr->s0->pd = devr->p0;
4698 devr->s0->uobject = NULL;
4699 devr->s0->event_handler = NULL;
4700 devr->s0->srq_context = NULL;
4701 devr->s0->srq_type = IB_SRQT_XRC;
4702 devr->s0->ext.xrc.xrcd = devr->x0;
4703 devr->s0->ext.cq = devr->c0;
4704 atomic_inc(&devr->s0->ext.xrc.xrcd->usecnt);
4705 atomic_inc(&devr->s0->ext.cq->usecnt);
4706 atomic_inc(&devr->p0->usecnt);
4707 atomic_set(&devr->s0->usecnt, 0);
4709 memset(&attr, 0, sizeof(attr));
4710 attr.attr.max_sge = 1;
4711 attr.attr.max_wr = 1;
4712 attr.srq_type = IB_SRQT_BASIC;
4713 devr->s1 = mlx5_ib_create_srq(devr->p0, &attr, NULL);
4714 if (IS_ERR(devr->s1)) {
4715 ret = PTR_ERR(devr->s1);
4718 devr->s1->device = &dev->ib_dev;
4719 devr->s1->pd = devr->p0;
4720 devr->s1->uobject = NULL;
4721 devr->s1->event_handler = NULL;
4722 devr->s1->srq_context = NULL;
4723 devr->s1->srq_type = IB_SRQT_BASIC;
4724 devr->s1->ext.cq = devr->c0;
4725 atomic_inc(&devr->p0->usecnt);
4726 atomic_set(&devr->s1->usecnt, 0);
4728 for (port = 0; port < ARRAY_SIZE(devr->ports); ++port) {
4729 INIT_WORK(&devr->ports[port].pkey_change_work,
4730 pkey_change_handler);
4731 devr->ports[port].devr = devr;
4737 mlx5_ib_destroy_srq(devr->s0);
4739 mlx5_ib_dealloc_xrcd(devr->x1);
4741 mlx5_ib_dealloc_xrcd(devr->x0);
4743 mlx5_ib_destroy_cq(devr->c0);
4745 mlx5_ib_dealloc_pd(devr->p0);
4750 static void destroy_dev_resources(struct mlx5_ib_resources *devr)
4752 struct mlx5_ib_dev *dev =
4753 container_of(devr, struct mlx5_ib_dev, devr);
4756 mlx5_ib_destroy_srq(devr->s1);
4757 mlx5_ib_destroy_srq(devr->s0);
4758 mlx5_ib_dealloc_xrcd(devr->x0);
4759 mlx5_ib_dealloc_xrcd(devr->x1);
4760 mlx5_ib_destroy_cq(devr->c0);
4761 mlx5_ib_dealloc_pd(devr->p0);
4763 /* Make sure no change P_Key work items are still executing */
4764 for (port = 0; port < dev->num_ports; ++port)
4765 cancel_work_sync(&devr->ports[port].pkey_change_work);
4768 static u32 get_core_cap_flags(struct ib_device *ibdev,
4769 struct mlx5_hca_vport_context *rep)
4771 struct mlx5_ib_dev *dev = to_mdev(ibdev);
4772 enum rdma_link_layer ll = mlx5_ib_port_link_layer(ibdev, 1);
4773 u8 l3_type_cap = MLX5_CAP_ROCE(dev->mdev, l3_type);
4774 u8 roce_version_cap = MLX5_CAP_ROCE(dev->mdev, roce_version);
4775 bool raw_support = !mlx5_core_mp_enabled(dev->mdev);
4778 if (rep->grh_required)
4779 ret |= RDMA_CORE_CAP_IB_GRH_REQUIRED;
4781 if (ll == IB_LINK_LAYER_INFINIBAND)
4782 return ret | RDMA_CORE_PORT_IBA_IB;
4785 ret |= RDMA_CORE_PORT_RAW_PACKET;
4787 if (!(l3_type_cap & MLX5_ROCE_L3_TYPE_IPV4_CAP))
4790 if (!(l3_type_cap & MLX5_ROCE_L3_TYPE_IPV6_CAP))
4793 if (roce_version_cap & MLX5_ROCE_VERSION_1_CAP)
4794 ret |= RDMA_CORE_PORT_IBA_ROCE;
4796 if (roce_version_cap & MLX5_ROCE_VERSION_2_CAP)
4797 ret |= RDMA_CORE_PORT_IBA_ROCE_UDP_ENCAP;
4802 static int mlx5_port_immutable(struct ib_device *ibdev, u8 port_num,
4803 struct ib_port_immutable *immutable)
4805 struct ib_port_attr attr;
4806 struct mlx5_ib_dev *dev = to_mdev(ibdev);
4807 enum rdma_link_layer ll = mlx5_ib_port_link_layer(ibdev, port_num);
4808 struct mlx5_hca_vport_context rep = {0};
4811 err = ib_query_port(ibdev, port_num, &attr);
4815 if (ll == IB_LINK_LAYER_INFINIBAND) {
4816 err = mlx5_query_hca_vport_context(dev->mdev, 0, port_num, 0,
4822 immutable->pkey_tbl_len = attr.pkey_tbl_len;
4823 immutable->gid_tbl_len = attr.gid_tbl_len;
4824 immutable->core_cap_flags = get_core_cap_flags(ibdev, &rep);
4825 if ((ll == IB_LINK_LAYER_INFINIBAND) || MLX5_CAP_GEN(dev->mdev, roce))
4826 immutable->max_mad_size = IB_MGMT_MAD_SIZE;
4831 static int mlx5_port_rep_immutable(struct ib_device *ibdev, u8 port_num,
4832 struct ib_port_immutable *immutable)
4834 struct ib_port_attr attr;
4837 immutable->core_cap_flags = RDMA_CORE_PORT_RAW_PACKET;
4839 err = ib_query_port(ibdev, port_num, &attr);
4843 immutable->pkey_tbl_len = attr.pkey_tbl_len;
4844 immutable->gid_tbl_len = attr.gid_tbl_len;
4845 immutable->core_cap_flags = RDMA_CORE_PORT_RAW_PACKET;
4850 static void get_dev_fw_str(struct ib_device *ibdev, char *str)
4852 struct mlx5_ib_dev *dev =
4853 container_of(ibdev, struct mlx5_ib_dev, ib_dev);
4854 snprintf(str, IB_FW_VERSION_NAME_MAX, "%d.%d.%04d",
4855 fw_rev_maj(dev->mdev), fw_rev_min(dev->mdev),
4856 fw_rev_sub(dev->mdev));
4859 static int mlx5_eth_lag_init(struct mlx5_ib_dev *dev)
4861 struct mlx5_core_dev *mdev = dev->mdev;
4862 struct mlx5_flow_namespace *ns = mlx5_get_flow_namespace(mdev,
4863 MLX5_FLOW_NAMESPACE_LAG);
4864 struct mlx5_flow_table *ft;
4867 if (!ns || !mlx5_lag_is_active(mdev))
4870 err = mlx5_cmd_create_vport_lag(mdev);
4874 ft = mlx5_create_lag_demux_flow_table(ns, 0, 0);
4877 goto err_destroy_vport_lag;
4880 dev->flow_db->lag_demux_ft = ft;
4883 err_destroy_vport_lag:
4884 mlx5_cmd_destroy_vport_lag(mdev);
4888 static void mlx5_eth_lag_cleanup(struct mlx5_ib_dev *dev)
4890 struct mlx5_core_dev *mdev = dev->mdev;
4892 if (dev->flow_db->lag_demux_ft) {
4893 mlx5_destroy_flow_table(dev->flow_db->lag_demux_ft);
4894 dev->flow_db->lag_demux_ft = NULL;
4896 mlx5_cmd_destroy_vport_lag(mdev);
4900 static int mlx5_add_netdev_notifier(struct mlx5_ib_dev *dev, u8 port_num)
4904 dev->roce[port_num].nb.notifier_call = mlx5_netdev_event;
4905 err = register_netdevice_notifier(&dev->roce[port_num].nb);
4907 dev->roce[port_num].nb.notifier_call = NULL;
4914 static void mlx5_remove_netdev_notifier(struct mlx5_ib_dev *dev, u8 port_num)
4916 if (dev->roce[port_num].nb.notifier_call) {
4917 unregister_netdevice_notifier(&dev->roce[port_num].nb);
4918 dev->roce[port_num].nb.notifier_call = NULL;
4922 static int mlx5_enable_eth(struct mlx5_ib_dev *dev)
4926 if (MLX5_CAP_GEN(dev->mdev, roce)) {
4927 err = mlx5_nic_vport_enable_roce(dev->mdev);
4932 err = mlx5_eth_lag_init(dev);
4934 goto err_disable_roce;
4939 if (MLX5_CAP_GEN(dev->mdev, roce))
4940 mlx5_nic_vport_disable_roce(dev->mdev);
4945 static void mlx5_disable_eth(struct mlx5_ib_dev *dev)
4947 mlx5_eth_lag_cleanup(dev);
4948 if (MLX5_CAP_GEN(dev->mdev, roce))
4949 mlx5_nic_vport_disable_roce(dev->mdev);
4952 struct mlx5_ib_counter {
4957 #define INIT_Q_COUNTER(_name) \
4958 { .name = #_name, .offset = MLX5_BYTE_OFF(query_q_counter_out, _name)}
4960 static const struct mlx5_ib_counter basic_q_cnts[] = {
4961 INIT_Q_COUNTER(rx_write_requests),
4962 INIT_Q_COUNTER(rx_read_requests),
4963 INIT_Q_COUNTER(rx_atomic_requests),
4964 INIT_Q_COUNTER(out_of_buffer),
4967 static const struct mlx5_ib_counter out_of_seq_q_cnts[] = {
4968 INIT_Q_COUNTER(out_of_sequence),
4971 static const struct mlx5_ib_counter retrans_q_cnts[] = {
4972 INIT_Q_COUNTER(duplicate_request),
4973 INIT_Q_COUNTER(rnr_nak_retry_err),
4974 INIT_Q_COUNTER(packet_seq_err),
4975 INIT_Q_COUNTER(implied_nak_seq_err),
4976 INIT_Q_COUNTER(local_ack_timeout_err),
4979 #define INIT_CONG_COUNTER(_name) \
4980 { .name = #_name, .offset = \
4981 MLX5_BYTE_OFF(query_cong_statistics_out, _name ## _high)}
4983 static const struct mlx5_ib_counter cong_cnts[] = {
4984 INIT_CONG_COUNTER(rp_cnp_ignored),
4985 INIT_CONG_COUNTER(rp_cnp_handled),
4986 INIT_CONG_COUNTER(np_ecn_marked_roce_packets),
4987 INIT_CONG_COUNTER(np_cnp_sent),
4990 static const struct mlx5_ib_counter extended_err_cnts[] = {
4991 INIT_Q_COUNTER(resp_local_length_error),
4992 INIT_Q_COUNTER(resp_cqe_error),
4993 INIT_Q_COUNTER(req_cqe_error),
4994 INIT_Q_COUNTER(req_remote_invalid_request),
4995 INIT_Q_COUNTER(req_remote_access_errors),
4996 INIT_Q_COUNTER(resp_remote_access_errors),
4997 INIT_Q_COUNTER(resp_cqe_flush_error),
4998 INIT_Q_COUNTER(req_cqe_flush_error),
5001 #define INIT_EXT_PPCNT_COUNTER(_name) \
5002 { .name = #_name, .offset = \
5003 MLX5_BYTE_OFF(ppcnt_reg, \
5004 counter_set.eth_extended_cntrs_grp_data_layout._name##_high)}
5006 static const struct mlx5_ib_counter ext_ppcnt_cnts[] = {
5007 INIT_EXT_PPCNT_COUNTER(rx_icrc_encapsulated),
5010 static void mlx5_ib_dealloc_counters(struct mlx5_ib_dev *dev)
5014 for (i = 0; i < dev->num_ports; i++) {
5015 if (dev->port[i].cnts.set_id_valid)
5016 mlx5_core_dealloc_q_counter(dev->mdev,
5017 dev->port[i].cnts.set_id);
5018 kfree(dev->port[i].cnts.names);
5019 kfree(dev->port[i].cnts.offsets);
5023 static int __mlx5_ib_alloc_counters(struct mlx5_ib_dev *dev,
5024 struct mlx5_ib_counters *cnts)
5028 num_counters = ARRAY_SIZE(basic_q_cnts);
5030 if (MLX5_CAP_GEN(dev->mdev, out_of_seq_cnt))
5031 num_counters += ARRAY_SIZE(out_of_seq_q_cnts);
5033 if (MLX5_CAP_GEN(dev->mdev, retransmission_q_counters))
5034 num_counters += ARRAY_SIZE(retrans_q_cnts);
5036 if (MLX5_CAP_GEN(dev->mdev, enhanced_error_q_counters))
5037 num_counters += ARRAY_SIZE(extended_err_cnts);
5039 cnts->num_q_counters = num_counters;
5041 if (MLX5_CAP_GEN(dev->mdev, cc_query_allowed)) {
5042 cnts->num_cong_counters = ARRAY_SIZE(cong_cnts);
5043 num_counters += ARRAY_SIZE(cong_cnts);
5045 if (MLX5_CAP_PCAM_FEATURE(dev->mdev, rx_icrc_encapsulated_counter)) {
5046 cnts->num_ext_ppcnt_counters = ARRAY_SIZE(ext_ppcnt_cnts);
5047 num_counters += ARRAY_SIZE(ext_ppcnt_cnts);
5049 cnts->names = kcalloc(num_counters, sizeof(cnts->names), GFP_KERNEL);
5053 cnts->offsets = kcalloc(num_counters,
5054 sizeof(cnts->offsets), GFP_KERNEL);
5066 static void mlx5_ib_fill_counters(struct mlx5_ib_dev *dev,
5073 for (i = 0; i < ARRAY_SIZE(basic_q_cnts); i++, j++) {
5074 names[j] = basic_q_cnts[i].name;
5075 offsets[j] = basic_q_cnts[i].offset;
5078 if (MLX5_CAP_GEN(dev->mdev, out_of_seq_cnt)) {
5079 for (i = 0; i < ARRAY_SIZE(out_of_seq_q_cnts); i++, j++) {
5080 names[j] = out_of_seq_q_cnts[i].name;
5081 offsets[j] = out_of_seq_q_cnts[i].offset;
5085 if (MLX5_CAP_GEN(dev->mdev, retransmission_q_counters)) {
5086 for (i = 0; i < ARRAY_SIZE(retrans_q_cnts); i++, j++) {
5087 names[j] = retrans_q_cnts[i].name;
5088 offsets[j] = retrans_q_cnts[i].offset;
5092 if (MLX5_CAP_GEN(dev->mdev, enhanced_error_q_counters)) {
5093 for (i = 0; i < ARRAY_SIZE(extended_err_cnts); i++, j++) {
5094 names[j] = extended_err_cnts[i].name;
5095 offsets[j] = extended_err_cnts[i].offset;
5099 if (MLX5_CAP_GEN(dev->mdev, cc_query_allowed)) {
5100 for (i = 0; i < ARRAY_SIZE(cong_cnts); i++, j++) {
5101 names[j] = cong_cnts[i].name;
5102 offsets[j] = cong_cnts[i].offset;
5106 if (MLX5_CAP_PCAM_FEATURE(dev->mdev, rx_icrc_encapsulated_counter)) {
5107 for (i = 0; i < ARRAY_SIZE(ext_ppcnt_cnts); i++, j++) {
5108 names[j] = ext_ppcnt_cnts[i].name;
5109 offsets[j] = ext_ppcnt_cnts[i].offset;
5114 static int mlx5_ib_alloc_counters(struct mlx5_ib_dev *dev)
5119 for (i = 0; i < dev->num_ports; i++) {
5120 err = __mlx5_ib_alloc_counters(dev, &dev->port[i].cnts);
5124 mlx5_ib_fill_counters(dev, dev->port[i].cnts.names,
5125 dev->port[i].cnts.offsets);
5127 err = mlx5_core_alloc_q_counter(dev->mdev,
5128 &dev->port[i].cnts.set_id);
5131 "couldn't allocate queue counter for port %d, err %d\n",
5135 dev->port[i].cnts.set_id_valid = true;
5141 mlx5_ib_dealloc_counters(dev);
5145 static struct rdma_hw_stats *mlx5_ib_alloc_hw_stats(struct ib_device *ibdev,
5148 struct mlx5_ib_dev *dev = to_mdev(ibdev);
5149 struct mlx5_ib_port *port = &dev->port[port_num - 1];
5151 /* We support only per port stats */
5155 return rdma_alloc_hw_stats_struct(port->cnts.names,
5156 port->cnts.num_q_counters +
5157 port->cnts.num_cong_counters +
5158 port->cnts.num_ext_ppcnt_counters,
5159 RDMA_HW_STATS_DEFAULT_LIFESPAN);
5162 static int mlx5_ib_query_q_counters(struct mlx5_core_dev *mdev,
5163 struct mlx5_ib_port *port,
5164 struct rdma_hw_stats *stats)
5166 int outlen = MLX5_ST_SZ_BYTES(query_q_counter_out);
5171 out = kvzalloc(outlen, GFP_KERNEL);
5175 ret = mlx5_core_query_q_counter(mdev,
5176 port->cnts.set_id, 0,
5181 for (i = 0; i < port->cnts.num_q_counters; i++) {
5182 val = *(__be32 *)(out + port->cnts.offsets[i]);
5183 stats->value[i] = (u64)be32_to_cpu(val);
5191 static int mlx5_ib_query_ext_ppcnt_counters(struct mlx5_ib_dev *dev,
5192 struct mlx5_ib_port *port,
5193 struct rdma_hw_stats *stats)
5195 int offset = port->cnts.num_q_counters + port->cnts.num_cong_counters;
5196 int sz = MLX5_ST_SZ_BYTES(ppcnt_reg);
5200 out = kvzalloc(sz, GFP_KERNEL);
5204 ret = mlx5_cmd_query_ext_ppcnt_counters(dev->mdev, out);
5208 for (i = 0; i < port->cnts.num_ext_ppcnt_counters; i++) {
5209 stats->value[i + offset] =
5210 be64_to_cpup((__be64 *)(out +
5211 port->cnts.offsets[i + offset]));
5219 static int mlx5_ib_get_hw_stats(struct ib_device *ibdev,
5220 struct rdma_hw_stats *stats,
5221 u8 port_num, int index)
5223 struct mlx5_ib_dev *dev = to_mdev(ibdev);
5224 struct mlx5_ib_port *port = &dev->port[port_num - 1];
5225 struct mlx5_core_dev *mdev;
5226 int ret, num_counters;
5232 num_counters = port->cnts.num_q_counters +
5233 port->cnts.num_cong_counters +
5234 port->cnts.num_ext_ppcnt_counters;
5236 /* q_counters are per IB device, query the master mdev */
5237 ret = mlx5_ib_query_q_counters(dev->mdev, port, stats);
5241 if (MLX5_CAP_PCAM_FEATURE(dev->mdev, rx_icrc_encapsulated_counter)) {
5242 ret = mlx5_ib_query_ext_ppcnt_counters(dev, port, stats);
5247 if (MLX5_CAP_GEN(dev->mdev, cc_query_allowed)) {
5248 mdev = mlx5_ib_get_native_port_mdev(dev, port_num,
5251 /* If port is not affiliated yet, its in down state
5252 * which doesn't have any counters yet, so it would be
5253 * zero. So no need to read from the HCA.
5257 ret = mlx5_lag_query_cong_counters(dev->mdev,
5259 port->cnts.num_q_counters,
5260 port->cnts.num_cong_counters,
5261 port->cnts.offsets +
5262 port->cnts.num_q_counters);
5264 mlx5_ib_put_native_port_mdev(dev, port_num);
5270 return num_counters;
5273 static int mlx5_ib_rn_get_params(struct ib_device *device, u8 port_num,
5274 enum rdma_netdev_t type,
5275 struct rdma_netdev_alloc_params *params)
5277 if (type != RDMA_NETDEV_IPOIB)
5280 return mlx5_rdma_rn_get_params(to_mdev(device)->mdev, device, params);
5283 static void delay_drop_debugfs_cleanup(struct mlx5_ib_dev *dev)
5285 if (!dev->delay_drop.dbg)
5287 debugfs_remove_recursive(dev->delay_drop.dbg->dir_debugfs);
5288 kfree(dev->delay_drop.dbg);
5289 dev->delay_drop.dbg = NULL;
5292 static void cancel_delay_drop(struct mlx5_ib_dev *dev)
5294 if (!(dev->ib_dev.attrs.raw_packet_caps & IB_RAW_PACKET_CAP_DELAY_DROP))
5297 cancel_work_sync(&dev->delay_drop.delay_drop_work);
5298 delay_drop_debugfs_cleanup(dev);
5301 static ssize_t delay_drop_timeout_read(struct file *filp, char __user *buf,
5302 size_t count, loff_t *pos)
5304 struct mlx5_ib_delay_drop *delay_drop = filp->private_data;
5308 len = snprintf(lbuf, sizeof(lbuf), "%u\n", delay_drop->timeout);
5309 return simple_read_from_buffer(buf, count, pos, lbuf, len);
5312 static ssize_t delay_drop_timeout_write(struct file *filp, const char __user *buf,
5313 size_t count, loff_t *pos)
5315 struct mlx5_ib_delay_drop *delay_drop = filp->private_data;
5319 if (kstrtouint_from_user(buf, count, 0, &var))
5322 timeout = min_t(u32, roundup(var, 100), MLX5_MAX_DELAY_DROP_TIMEOUT_MS *
5325 mlx5_ib_dbg(delay_drop->dev, "Round delay drop timeout to %u usec\n",
5328 delay_drop->timeout = timeout;
5333 static const struct file_operations fops_delay_drop_timeout = {
5334 .owner = THIS_MODULE,
5335 .open = simple_open,
5336 .write = delay_drop_timeout_write,
5337 .read = delay_drop_timeout_read,
5340 static int delay_drop_debugfs_init(struct mlx5_ib_dev *dev)
5342 struct mlx5_ib_dbg_delay_drop *dbg;
5344 if (!mlx5_debugfs_root)
5347 dbg = kzalloc(sizeof(*dbg), GFP_KERNEL);
5351 dev->delay_drop.dbg = dbg;
5354 debugfs_create_dir("delay_drop",
5355 dev->mdev->priv.dbg_root);
5356 if (!dbg->dir_debugfs)
5359 dbg->events_cnt_debugfs =
5360 debugfs_create_atomic_t("num_timeout_events", 0400,
5362 &dev->delay_drop.events_cnt);
5363 if (!dbg->events_cnt_debugfs)
5366 dbg->rqs_cnt_debugfs =
5367 debugfs_create_atomic_t("num_rqs", 0400,
5369 &dev->delay_drop.rqs_cnt);
5370 if (!dbg->rqs_cnt_debugfs)
5373 dbg->timeout_debugfs =
5374 debugfs_create_file("timeout", 0600,
5377 &fops_delay_drop_timeout);
5378 if (!dbg->timeout_debugfs)
5384 delay_drop_debugfs_cleanup(dev);
5388 static void init_delay_drop(struct mlx5_ib_dev *dev)
5390 if (!(dev->ib_dev.attrs.raw_packet_caps & IB_RAW_PACKET_CAP_DELAY_DROP))
5393 mutex_init(&dev->delay_drop.lock);
5394 dev->delay_drop.dev = dev;
5395 dev->delay_drop.activate = false;
5396 dev->delay_drop.timeout = MLX5_MAX_DELAY_DROP_TIMEOUT_MS * 1000;
5397 INIT_WORK(&dev->delay_drop.delay_drop_work, delay_drop_handler);
5398 atomic_set(&dev->delay_drop.rqs_cnt, 0);
5399 atomic_set(&dev->delay_drop.events_cnt, 0);
5401 if (delay_drop_debugfs_init(dev))
5402 mlx5_ib_warn(dev, "Failed to init delay drop debugfs\n");
5405 /* The mlx5_ib_multiport_mutex should be held when calling this function */
5406 static void mlx5_ib_unbind_slave_port(struct mlx5_ib_dev *ibdev,
5407 struct mlx5_ib_multiport_info *mpi)
5409 u8 port_num = mlx5_core_native_port_num(mpi->mdev) - 1;
5410 struct mlx5_ib_port *port = &ibdev->port[port_num];
5415 mlx5_ib_cleanup_cong_debugfs(ibdev, port_num);
5417 spin_lock(&port->mp.mpi_lock);
5419 spin_unlock(&port->mp.mpi_lock);
5423 if (mpi->mdev_events.notifier_call)
5424 mlx5_notifier_unregister(mpi->mdev, &mpi->mdev_events);
5425 mpi->mdev_events.notifier_call = NULL;
5429 spin_unlock(&port->mp.mpi_lock);
5430 mlx5_remove_netdev_notifier(ibdev, port_num);
5431 spin_lock(&port->mp.mpi_lock);
5433 comps = mpi->mdev_refcnt;
5435 mpi->unaffiliate = true;
5436 init_completion(&mpi->unref_comp);
5437 spin_unlock(&port->mp.mpi_lock);
5439 for (i = 0; i < comps; i++)
5440 wait_for_completion(&mpi->unref_comp);
5442 spin_lock(&port->mp.mpi_lock);
5443 mpi->unaffiliate = false;
5446 port->mp.mpi = NULL;
5448 list_add_tail(&mpi->list, &mlx5_ib_unaffiliated_port_list);
5450 spin_unlock(&port->mp.mpi_lock);
5452 err = mlx5_nic_vport_unaffiliate_multiport(mpi->mdev);
5454 mlx5_ib_dbg(ibdev, "unaffiliated port %d\n", port_num + 1);
5455 /* Log an error, still needed to cleanup the pointers and add
5456 * it back to the list.
5459 mlx5_ib_err(ibdev, "Failed to unaffiliate port %u\n",
5462 ibdev->roce[port_num].last_port_state = IB_PORT_DOWN;
5465 /* The mlx5_ib_multiport_mutex should be held when calling this function */
5466 static bool mlx5_ib_bind_slave_port(struct mlx5_ib_dev *ibdev,
5467 struct mlx5_ib_multiport_info *mpi)
5469 u8 port_num = mlx5_core_native_port_num(mpi->mdev) - 1;
5472 spin_lock(&ibdev->port[port_num].mp.mpi_lock);
5473 if (ibdev->port[port_num].mp.mpi) {
5474 mlx5_ib_dbg(ibdev, "port %d already affiliated.\n",
5476 spin_unlock(&ibdev->port[port_num].mp.mpi_lock);
5480 ibdev->port[port_num].mp.mpi = mpi;
5482 mpi->mdev_events.notifier_call = NULL;
5483 spin_unlock(&ibdev->port[port_num].mp.mpi_lock);
5485 err = mlx5_nic_vport_affiliate_multiport(ibdev->mdev, mpi->mdev);
5489 err = get_port_caps(ibdev, mlx5_core_native_port_num(mpi->mdev));
5493 err = mlx5_add_netdev_notifier(ibdev, port_num);
5495 mlx5_ib_err(ibdev, "failed adding netdev notifier for port %u\n",
5500 mpi->mdev_events.notifier_call = mlx5_ib_event_slave_port;
5501 mlx5_notifier_register(mpi->mdev, &mpi->mdev_events);
5503 err = mlx5_ib_init_cong_debugfs(ibdev, port_num);
5510 mlx5_ib_unbind_slave_port(ibdev, mpi);
5514 static int mlx5_ib_init_multiport_master(struct mlx5_ib_dev *dev)
5516 int port_num = mlx5_core_native_port_num(dev->mdev) - 1;
5517 enum rdma_link_layer ll = mlx5_ib_port_link_layer(&dev->ib_dev,
5519 struct mlx5_ib_multiport_info *mpi;
5523 if (!mlx5_core_is_mp_master(dev->mdev) || ll != IB_LINK_LAYER_ETHERNET)
5526 err = mlx5_query_nic_vport_system_image_guid(dev->mdev,
5527 &dev->sys_image_guid);
5531 err = mlx5_nic_vport_enable_roce(dev->mdev);
5535 mutex_lock(&mlx5_ib_multiport_mutex);
5536 for (i = 0; i < dev->num_ports; i++) {
5539 /* build a stub multiport info struct for the native port. */
5540 if (i == port_num) {
5541 mpi = kzalloc(sizeof(*mpi), GFP_KERNEL);
5543 mutex_unlock(&mlx5_ib_multiport_mutex);
5544 mlx5_nic_vport_disable_roce(dev->mdev);
5548 mpi->is_master = true;
5549 mpi->mdev = dev->mdev;
5550 mpi->sys_image_guid = dev->sys_image_guid;
5551 dev->port[i].mp.mpi = mpi;
5557 list_for_each_entry(mpi, &mlx5_ib_unaffiliated_port_list,
5559 if (dev->sys_image_guid == mpi->sys_image_guid &&
5560 (mlx5_core_native_port_num(mpi->mdev) - 1) == i) {
5561 bound = mlx5_ib_bind_slave_port(dev, mpi);
5565 dev_dbg(&mpi->mdev->pdev->dev, "removing port from unaffiliated list.\n");
5566 mlx5_ib_dbg(dev, "port %d bound\n", i + 1);
5567 list_del(&mpi->list);
5572 get_port_caps(dev, i + 1);
5573 mlx5_ib_dbg(dev, "no free port found for port %d\n",
5578 list_add_tail(&dev->ib_dev_list, &mlx5_ib_dev_list);
5579 mutex_unlock(&mlx5_ib_multiport_mutex);
5583 static void mlx5_ib_cleanup_multiport_master(struct mlx5_ib_dev *dev)
5585 int port_num = mlx5_core_native_port_num(dev->mdev) - 1;
5586 enum rdma_link_layer ll = mlx5_ib_port_link_layer(&dev->ib_dev,
5590 if (!mlx5_core_is_mp_master(dev->mdev) || ll != IB_LINK_LAYER_ETHERNET)
5593 mutex_lock(&mlx5_ib_multiport_mutex);
5594 for (i = 0; i < dev->num_ports; i++) {
5595 if (dev->port[i].mp.mpi) {
5596 /* Destroy the native port stub */
5597 if (i == port_num) {
5598 kfree(dev->port[i].mp.mpi);
5599 dev->port[i].mp.mpi = NULL;
5601 mlx5_ib_dbg(dev, "unbinding port_num: %d\n", i + 1);
5602 mlx5_ib_unbind_slave_port(dev, dev->port[i].mp.mpi);
5607 mlx5_ib_dbg(dev, "removing from devlist\n");
5608 list_del(&dev->ib_dev_list);
5609 mutex_unlock(&mlx5_ib_multiport_mutex);
5611 mlx5_nic_vport_disable_roce(dev->mdev);
5614 ADD_UVERBS_ATTRIBUTES_SIMPLE(
5617 UVERBS_METHOD_DM_ALLOC,
5618 UVERBS_ATTR_PTR_OUT(MLX5_IB_ATTR_ALLOC_DM_RESP_START_OFFSET,
5619 UVERBS_ATTR_TYPE(u64),
5621 UVERBS_ATTR_PTR_OUT(MLX5_IB_ATTR_ALLOC_DM_RESP_PAGE_INDEX,
5622 UVERBS_ATTR_TYPE(u16),
5625 ADD_UVERBS_ATTRIBUTES_SIMPLE(
5626 mlx5_ib_flow_action,
5627 UVERBS_OBJECT_FLOW_ACTION,
5628 UVERBS_METHOD_FLOW_ACTION_ESP_CREATE,
5629 UVERBS_ATTR_FLAGS_IN(MLX5_IB_ATTR_CREATE_FLOW_ACTION_FLAGS,
5630 enum mlx5_ib_uapi_flow_action_flags));
5632 static const struct uapi_definition mlx5_ib_defs[] = {
5633 #if IS_ENABLED(CONFIG_INFINIBAND_USER_ACCESS)
5634 UAPI_DEF_CHAIN(mlx5_ib_devx_defs),
5635 UAPI_DEF_CHAIN(mlx5_ib_flow_defs),
5638 UAPI_DEF_CHAIN_OBJ_TREE(UVERBS_OBJECT_FLOW_ACTION,
5639 &mlx5_ib_flow_action),
5640 UAPI_DEF_CHAIN_OBJ_TREE(UVERBS_OBJECT_DM, &mlx5_ib_dm),
5644 static int mlx5_ib_read_counters(struct ib_counters *counters,
5645 struct ib_counters_read_attr *read_attr,
5646 struct uverbs_attr_bundle *attrs)
5648 struct mlx5_ib_mcounters *mcounters = to_mcounters(counters);
5649 struct mlx5_read_counters_attr mread_attr = {};
5650 struct mlx5_ib_flow_counters_desc *desc;
5653 mutex_lock(&mcounters->mcntrs_mutex);
5654 if (mcounters->cntrs_max_index > read_attr->ncounters) {
5659 mread_attr.out = kcalloc(mcounters->counters_num, sizeof(u64),
5661 if (!mread_attr.out) {
5666 mread_attr.hw_cntrs_hndl = mcounters->hw_cntrs_hndl;
5667 mread_attr.flags = read_attr->flags;
5668 ret = mcounters->read_counters(counters->device, &mread_attr);
5672 /* do the pass over the counters data array to assign according to the
5673 * descriptions and indexing pairs
5675 desc = mcounters->counters_data;
5676 for (i = 0; i < mcounters->ncounters; i++)
5677 read_attr->counters_buff[desc[i].index] += mread_attr.out[desc[i].description];
5680 kfree(mread_attr.out);
5682 mutex_unlock(&mcounters->mcntrs_mutex);
5686 static int mlx5_ib_destroy_counters(struct ib_counters *counters)
5688 struct mlx5_ib_mcounters *mcounters = to_mcounters(counters);
5690 counters_clear_description(counters);
5691 if (mcounters->hw_cntrs_hndl)
5692 mlx5_fc_destroy(to_mdev(counters->device)->mdev,
5693 mcounters->hw_cntrs_hndl);
5700 static struct ib_counters *mlx5_ib_create_counters(struct ib_device *device,
5701 struct uverbs_attr_bundle *attrs)
5703 struct mlx5_ib_mcounters *mcounters;
5705 mcounters = kzalloc(sizeof(*mcounters), GFP_KERNEL);
5707 return ERR_PTR(-ENOMEM);
5709 mutex_init(&mcounters->mcntrs_mutex);
5711 return &mcounters->ibcntrs;
5714 void mlx5_ib_stage_init_cleanup(struct mlx5_ib_dev *dev)
5716 mlx5_ib_cleanup_multiport_master(dev);
5717 #ifdef CONFIG_INFINIBAND_ON_DEMAND_PAGING
5718 cleanup_srcu_struct(&dev->mr_srcu);
5719 drain_workqueue(dev->advise_mr_wq);
5720 destroy_workqueue(dev->advise_mr_wq);
5725 int mlx5_ib_stage_init_init(struct mlx5_ib_dev *dev)
5727 struct mlx5_core_dev *mdev = dev->mdev;
5731 dev->port = kcalloc(dev->num_ports, sizeof(*dev->port),
5736 for (i = 0; i < dev->num_ports; i++) {
5737 spin_lock_init(&dev->port[i].mp.mpi_lock);
5738 rwlock_init(&dev->roce[i].netdev_lock);
5741 err = mlx5_ib_init_multiport_master(dev);
5745 if (!mlx5_core_mp_enabled(mdev)) {
5746 for (i = 1; i <= dev->num_ports; i++) {
5747 err = get_port_caps(dev, i);
5752 err = get_port_caps(dev, mlx5_core_native_port_num(mdev));
5757 if (mlx5_use_mad_ifc(dev))
5758 get_ext_port_caps(dev);
5760 dev->ib_dev.owner = THIS_MODULE;
5761 dev->ib_dev.node_type = RDMA_NODE_IB_CA;
5762 dev->ib_dev.local_dma_lkey = 0 /* not supported for now */;
5763 dev->ib_dev.phys_port_cnt = dev->num_ports;
5764 dev->ib_dev.num_comp_vectors = mlx5_comp_vectors_count(mdev);
5765 dev->ib_dev.dev.parent = &mdev->pdev->dev;
5767 mutex_init(&dev->cap_mask_mutex);
5768 INIT_LIST_HEAD(&dev->qp_list);
5769 spin_lock_init(&dev->reset_flow_resource_lock);
5771 spin_lock_init(&dev->memic.memic_lock);
5772 dev->memic.dev = mdev;
5774 #ifdef CONFIG_INFINIBAND_ON_DEMAND_PAGING
5775 dev->advise_mr_wq = alloc_ordered_workqueue("mlx5_ib_advise_mr_wq", 0);
5776 if (!dev->advise_mr_wq) {
5781 err = init_srcu_struct(&dev->mr_srcu);
5783 destroy_workqueue(dev->advise_mr_wq);
5790 mlx5_ib_cleanup_multiport_master(dev);
5798 static int mlx5_ib_stage_flow_db_init(struct mlx5_ib_dev *dev)
5800 dev->flow_db = kzalloc(sizeof(*dev->flow_db), GFP_KERNEL);
5805 mutex_init(&dev->flow_db->lock);
5810 int mlx5_ib_stage_rep_flow_db_init(struct mlx5_ib_dev *dev)
5812 struct mlx5_ib_dev *nic_dev;
5814 nic_dev = mlx5_ib_get_uplink_ibdev(dev->mdev->priv.eswitch);
5819 dev->flow_db = nic_dev->flow_db;
5824 static void mlx5_ib_stage_flow_db_cleanup(struct mlx5_ib_dev *dev)
5826 kfree(dev->flow_db);
5829 static const struct ib_device_ops mlx5_ib_dev_ops = {
5830 .add_gid = mlx5_ib_add_gid,
5831 .alloc_mr = mlx5_ib_alloc_mr,
5832 .alloc_pd = mlx5_ib_alloc_pd,
5833 .alloc_ucontext = mlx5_ib_alloc_ucontext,
5834 .attach_mcast = mlx5_ib_mcg_attach,
5835 .check_mr_status = mlx5_ib_check_mr_status,
5836 .create_ah = mlx5_ib_create_ah,
5837 .create_counters = mlx5_ib_create_counters,
5838 .create_cq = mlx5_ib_create_cq,
5839 .create_flow = mlx5_ib_create_flow,
5840 .create_qp = mlx5_ib_create_qp,
5841 .create_srq = mlx5_ib_create_srq,
5842 .dealloc_pd = mlx5_ib_dealloc_pd,
5843 .dealloc_ucontext = mlx5_ib_dealloc_ucontext,
5844 .del_gid = mlx5_ib_del_gid,
5845 .dereg_mr = mlx5_ib_dereg_mr,
5846 .destroy_ah = mlx5_ib_destroy_ah,
5847 .destroy_counters = mlx5_ib_destroy_counters,
5848 .destroy_cq = mlx5_ib_destroy_cq,
5849 .destroy_flow = mlx5_ib_destroy_flow,
5850 .destroy_flow_action = mlx5_ib_destroy_flow_action,
5851 .destroy_qp = mlx5_ib_destroy_qp,
5852 .destroy_srq = mlx5_ib_destroy_srq,
5853 .detach_mcast = mlx5_ib_mcg_detach,
5854 .disassociate_ucontext = mlx5_ib_disassociate_ucontext,
5855 .drain_rq = mlx5_ib_drain_rq,
5856 .drain_sq = mlx5_ib_drain_sq,
5857 .get_dev_fw_str = get_dev_fw_str,
5858 .get_dma_mr = mlx5_ib_get_dma_mr,
5859 .get_link_layer = mlx5_ib_port_link_layer,
5860 .map_mr_sg = mlx5_ib_map_mr_sg,
5861 .mmap = mlx5_ib_mmap,
5862 .modify_cq = mlx5_ib_modify_cq,
5863 .modify_device = mlx5_ib_modify_device,
5864 .modify_port = mlx5_ib_modify_port,
5865 .modify_qp = mlx5_ib_modify_qp,
5866 .modify_srq = mlx5_ib_modify_srq,
5867 .poll_cq = mlx5_ib_poll_cq,
5868 .post_recv = mlx5_ib_post_recv,
5869 .post_send = mlx5_ib_post_send,
5870 .post_srq_recv = mlx5_ib_post_srq_recv,
5871 .process_mad = mlx5_ib_process_mad,
5872 .query_ah = mlx5_ib_query_ah,
5873 .query_device = mlx5_ib_query_device,
5874 .query_gid = mlx5_ib_query_gid,
5875 .query_pkey = mlx5_ib_query_pkey,
5876 .query_qp = mlx5_ib_query_qp,
5877 .query_srq = mlx5_ib_query_srq,
5878 .read_counters = mlx5_ib_read_counters,
5879 .reg_user_mr = mlx5_ib_reg_user_mr,
5880 .req_notify_cq = mlx5_ib_arm_cq,
5881 .rereg_user_mr = mlx5_ib_rereg_user_mr,
5882 .resize_cq = mlx5_ib_resize_cq,
5885 static const struct ib_device_ops mlx5_ib_dev_flow_ipsec_ops = {
5886 .create_flow_action_esp = mlx5_ib_create_flow_action_esp,
5887 .modify_flow_action_esp = mlx5_ib_modify_flow_action_esp,
5890 static const struct ib_device_ops mlx5_ib_dev_ipoib_enhanced_ops = {
5891 .rdma_netdev_get_params = mlx5_ib_rn_get_params,
5894 static const struct ib_device_ops mlx5_ib_dev_sriov_ops = {
5895 .get_vf_config = mlx5_ib_get_vf_config,
5896 .get_vf_stats = mlx5_ib_get_vf_stats,
5897 .set_vf_guid = mlx5_ib_set_vf_guid,
5898 .set_vf_link_state = mlx5_ib_set_vf_link_state,
5901 static const struct ib_device_ops mlx5_ib_dev_mw_ops = {
5902 .alloc_mw = mlx5_ib_alloc_mw,
5903 .dealloc_mw = mlx5_ib_dealloc_mw,
5906 static const struct ib_device_ops mlx5_ib_dev_xrc_ops = {
5907 .alloc_xrcd = mlx5_ib_alloc_xrcd,
5908 .dealloc_xrcd = mlx5_ib_dealloc_xrcd,
5911 static const struct ib_device_ops mlx5_ib_dev_dm_ops = {
5912 .alloc_dm = mlx5_ib_alloc_dm,
5913 .dealloc_dm = mlx5_ib_dealloc_dm,
5914 .reg_dm_mr = mlx5_ib_reg_dm_mr,
5917 int mlx5_ib_stage_caps_init(struct mlx5_ib_dev *dev)
5919 struct mlx5_core_dev *mdev = dev->mdev;
5922 dev->ib_dev.uverbs_abi_ver = MLX5_IB_UVERBS_ABI_VERSION;
5923 dev->ib_dev.uverbs_cmd_mask =
5924 (1ull << IB_USER_VERBS_CMD_GET_CONTEXT) |
5925 (1ull << IB_USER_VERBS_CMD_QUERY_DEVICE) |
5926 (1ull << IB_USER_VERBS_CMD_QUERY_PORT) |
5927 (1ull << IB_USER_VERBS_CMD_ALLOC_PD) |
5928 (1ull << IB_USER_VERBS_CMD_DEALLOC_PD) |
5929 (1ull << IB_USER_VERBS_CMD_CREATE_AH) |
5930 (1ull << IB_USER_VERBS_CMD_DESTROY_AH) |
5931 (1ull << IB_USER_VERBS_CMD_REG_MR) |
5932 (1ull << IB_USER_VERBS_CMD_REREG_MR) |
5933 (1ull << IB_USER_VERBS_CMD_DEREG_MR) |
5934 (1ull << IB_USER_VERBS_CMD_CREATE_COMP_CHANNEL) |
5935 (1ull << IB_USER_VERBS_CMD_CREATE_CQ) |
5936 (1ull << IB_USER_VERBS_CMD_RESIZE_CQ) |
5937 (1ull << IB_USER_VERBS_CMD_DESTROY_CQ) |
5938 (1ull << IB_USER_VERBS_CMD_CREATE_QP) |
5939 (1ull << IB_USER_VERBS_CMD_MODIFY_QP) |
5940 (1ull << IB_USER_VERBS_CMD_QUERY_QP) |
5941 (1ull << IB_USER_VERBS_CMD_DESTROY_QP) |
5942 (1ull << IB_USER_VERBS_CMD_ATTACH_MCAST) |
5943 (1ull << IB_USER_VERBS_CMD_DETACH_MCAST) |
5944 (1ull << IB_USER_VERBS_CMD_CREATE_SRQ) |
5945 (1ull << IB_USER_VERBS_CMD_MODIFY_SRQ) |
5946 (1ull << IB_USER_VERBS_CMD_QUERY_SRQ) |
5947 (1ull << IB_USER_VERBS_CMD_DESTROY_SRQ) |
5948 (1ull << IB_USER_VERBS_CMD_CREATE_XSRQ) |
5949 (1ull << IB_USER_VERBS_CMD_OPEN_QP);
5950 dev->ib_dev.uverbs_ex_cmd_mask =
5951 (1ull << IB_USER_VERBS_EX_CMD_QUERY_DEVICE) |
5952 (1ull << IB_USER_VERBS_EX_CMD_CREATE_CQ) |
5953 (1ull << IB_USER_VERBS_EX_CMD_CREATE_QP) |
5954 (1ull << IB_USER_VERBS_EX_CMD_MODIFY_QP) |
5955 (1ull << IB_USER_VERBS_EX_CMD_MODIFY_CQ) |
5956 (1ull << IB_USER_VERBS_EX_CMD_CREATE_FLOW) |
5957 (1ull << IB_USER_VERBS_EX_CMD_DESTROY_FLOW);
5959 if (MLX5_CAP_GEN(mdev, ipoib_enhanced_offloads) &&
5960 IS_ENABLED(CONFIG_MLX5_CORE_IPOIB))
5961 ib_set_device_ops(&dev->ib_dev,
5962 &mlx5_ib_dev_ipoib_enhanced_ops);
5964 if (mlx5_core_is_pf(mdev))
5965 ib_set_device_ops(&dev->ib_dev, &mlx5_ib_dev_sriov_ops);
5967 dev->umr_fence = mlx5_get_umr_fence(MLX5_CAP_GEN(mdev, umr_fence));
5969 if (MLX5_CAP_GEN(mdev, imaicl)) {
5970 dev->ib_dev.uverbs_cmd_mask |=
5971 (1ull << IB_USER_VERBS_CMD_ALLOC_MW) |
5972 (1ull << IB_USER_VERBS_CMD_DEALLOC_MW);
5973 ib_set_device_ops(&dev->ib_dev, &mlx5_ib_dev_mw_ops);
5976 if (MLX5_CAP_GEN(mdev, xrc)) {
5977 dev->ib_dev.uverbs_cmd_mask |=
5978 (1ull << IB_USER_VERBS_CMD_OPEN_XRCD) |
5979 (1ull << IB_USER_VERBS_CMD_CLOSE_XRCD);
5980 ib_set_device_ops(&dev->ib_dev, &mlx5_ib_dev_xrc_ops);
5983 if (MLX5_CAP_DEV_MEM(mdev, memic))
5984 ib_set_device_ops(&dev->ib_dev, &mlx5_ib_dev_dm_ops);
5986 if (mlx5_accel_ipsec_device_caps(dev->mdev) &
5987 MLX5_ACCEL_IPSEC_CAP_DEVICE)
5988 ib_set_device_ops(&dev->ib_dev, &mlx5_ib_dev_flow_ipsec_ops);
5989 dev->ib_dev.driver_id = RDMA_DRIVER_MLX5;
5990 ib_set_device_ops(&dev->ib_dev, &mlx5_ib_dev_ops);
5992 if (IS_ENABLED(CONFIG_INFINIBAND_USER_ACCESS))
5993 dev->ib_dev.driver_def = mlx5_ib_defs;
5995 err = init_node_data(dev);
5999 if ((MLX5_CAP_GEN(dev->mdev, port_type) == MLX5_CAP_PORT_TYPE_ETH) &&
6000 (MLX5_CAP_GEN(dev->mdev, disable_local_lb_uc) ||
6001 MLX5_CAP_GEN(dev->mdev, disable_local_lb_mc)))
6002 mutex_init(&dev->lb.mutex);
6007 static const struct ib_device_ops mlx5_ib_dev_port_ops = {
6008 .get_port_immutable = mlx5_port_immutable,
6009 .query_port = mlx5_ib_query_port,
6012 static int mlx5_ib_stage_non_default_cb(struct mlx5_ib_dev *dev)
6014 ib_set_device_ops(&dev->ib_dev, &mlx5_ib_dev_port_ops);
6018 static const struct ib_device_ops mlx5_ib_dev_port_rep_ops = {
6019 .get_port_immutable = mlx5_port_rep_immutable,
6020 .query_port = mlx5_ib_rep_query_port,
6023 int mlx5_ib_stage_rep_non_default_cb(struct mlx5_ib_dev *dev)
6025 ib_set_device_ops(&dev->ib_dev, &mlx5_ib_dev_port_rep_ops);
6029 static const struct ib_device_ops mlx5_ib_dev_common_roce_ops = {
6030 .create_rwq_ind_table = mlx5_ib_create_rwq_ind_table,
6031 .create_wq = mlx5_ib_create_wq,
6032 .destroy_rwq_ind_table = mlx5_ib_destroy_rwq_ind_table,
6033 .destroy_wq = mlx5_ib_destroy_wq,
6034 .get_netdev = mlx5_ib_get_netdev,
6035 .modify_wq = mlx5_ib_modify_wq,
6038 static int mlx5_ib_stage_common_roce_init(struct mlx5_ib_dev *dev)
6043 for (i = 0; i < dev->num_ports; i++) {
6044 dev->roce[i].dev = dev;
6045 dev->roce[i].native_port_num = i + 1;
6046 dev->roce[i].last_port_state = IB_PORT_DOWN;
6049 dev->ib_dev.uverbs_ex_cmd_mask |=
6050 (1ull << IB_USER_VERBS_EX_CMD_CREATE_WQ) |
6051 (1ull << IB_USER_VERBS_EX_CMD_MODIFY_WQ) |
6052 (1ull << IB_USER_VERBS_EX_CMD_DESTROY_WQ) |
6053 (1ull << IB_USER_VERBS_EX_CMD_CREATE_RWQ_IND_TBL) |
6054 (1ull << IB_USER_VERBS_EX_CMD_DESTROY_RWQ_IND_TBL);
6055 ib_set_device_ops(&dev->ib_dev, &mlx5_ib_dev_common_roce_ops);
6057 port_num = mlx5_core_native_port_num(dev->mdev) - 1;
6059 return mlx5_add_netdev_notifier(dev, port_num);
6062 static void mlx5_ib_stage_common_roce_cleanup(struct mlx5_ib_dev *dev)
6064 u8 port_num = mlx5_core_native_port_num(dev->mdev) - 1;
6066 mlx5_remove_netdev_notifier(dev, port_num);
6069 int mlx5_ib_stage_rep_roce_init(struct mlx5_ib_dev *dev)
6071 struct mlx5_core_dev *mdev = dev->mdev;
6072 enum rdma_link_layer ll;
6076 port_type_cap = MLX5_CAP_GEN(mdev, port_type);
6077 ll = mlx5_port_type_cap_to_rdma_ll(port_type_cap);
6079 if (ll == IB_LINK_LAYER_ETHERNET)
6080 err = mlx5_ib_stage_common_roce_init(dev);
6085 void mlx5_ib_stage_rep_roce_cleanup(struct mlx5_ib_dev *dev)
6087 mlx5_ib_stage_common_roce_cleanup(dev);
6090 static int mlx5_ib_stage_roce_init(struct mlx5_ib_dev *dev)
6092 struct mlx5_core_dev *mdev = dev->mdev;
6093 enum rdma_link_layer ll;
6097 port_type_cap = MLX5_CAP_GEN(mdev, port_type);
6098 ll = mlx5_port_type_cap_to_rdma_ll(port_type_cap);
6100 if (ll == IB_LINK_LAYER_ETHERNET) {
6101 err = mlx5_ib_stage_common_roce_init(dev);
6105 err = mlx5_enable_eth(dev);
6112 mlx5_ib_stage_common_roce_cleanup(dev);
6117 static void mlx5_ib_stage_roce_cleanup(struct mlx5_ib_dev *dev)
6119 struct mlx5_core_dev *mdev = dev->mdev;
6120 enum rdma_link_layer ll;
6123 port_type_cap = MLX5_CAP_GEN(mdev, port_type);
6124 ll = mlx5_port_type_cap_to_rdma_ll(port_type_cap);
6126 if (ll == IB_LINK_LAYER_ETHERNET) {
6127 mlx5_disable_eth(dev);
6128 mlx5_ib_stage_common_roce_cleanup(dev);
6132 int mlx5_ib_stage_dev_res_init(struct mlx5_ib_dev *dev)
6134 return create_dev_resources(&dev->devr);
6137 void mlx5_ib_stage_dev_res_cleanup(struct mlx5_ib_dev *dev)
6139 destroy_dev_resources(&dev->devr);
6142 static int mlx5_ib_stage_odp_init(struct mlx5_ib_dev *dev)
6144 mlx5_ib_internal_fill_odp_caps(dev);
6146 return mlx5_ib_odp_init_one(dev);
6149 void mlx5_ib_stage_odp_cleanup(struct mlx5_ib_dev *dev)
6151 mlx5_ib_odp_cleanup_one(dev);
6154 static const struct ib_device_ops mlx5_ib_dev_hw_stats_ops = {
6155 .alloc_hw_stats = mlx5_ib_alloc_hw_stats,
6156 .get_hw_stats = mlx5_ib_get_hw_stats,
6159 int mlx5_ib_stage_counters_init(struct mlx5_ib_dev *dev)
6161 if (MLX5_CAP_GEN(dev->mdev, max_qp_cnt)) {
6162 ib_set_device_ops(&dev->ib_dev, &mlx5_ib_dev_hw_stats_ops);
6164 return mlx5_ib_alloc_counters(dev);
6170 void mlx5_ib_stage_counters_cleanup(struct mlx5_ib_dev *dev)
6172 if (MLX5_CAP_GEN(dev->mdev, max_qp_cnt))
6173 mlx5_ib_dealloc_counters(dev);
6176 static int mlx5_ib_stage_cong_debugfs_init(struct mlx5_ib_dev *dev)
6178 return mlx5_ib_init_cong_debugfs(dev,
6179 mlx5_core_native_port_num(dev->mdev) - 1);
6182 static void mlx5_ib_stage_cong_debugfs_cleanup(struct mlx5_ib_dev *dev)
6184 mlx5_ib_cleanup_cong_debugfs(dev,
6185 mlx5_core_native_port_num(dev->mdev) - 1);
6188 static int mlx5_ib_stage_uar_init(struct mlx5_ib_dev *dev)
6190 dev->mdev->priv.uar = mlx5_get_uars_page(dev->mdev);
6191 return PTR_ERR_OR_ZERO(dev->mdev->priv.uar);
6194 static void mlx5_ib_stage_uar_cleanup(struct mlx5_ib_dev *dev)
6196 mlx5_put_uars_page(dev->mdev, dev->mdev->priv.uar);
6199 int mlx5_ib_stage_bfrag_init(struct mlx5_ib_dev *dev)
6203 err = mlx5_alloc_bfreg(dev->mdev, &dev->bfreg, false, false);
6207 err = mlx5_alloc_bfreg(dev->mdev, &dev->fp_bfreg, false, true);
6209 mlx5_free_bfreg(dev->mdev, &dev->fp_bfreg);
6214 void mlx5_ib_stage_bfrag_cleanup(struct mlx5_ib_dev *dev)
6216 mlx5_free_bfreg(dev->mdev, &dev->fp_bfreg);
6217 mlx5_free_bfreg(dev->mdev, &dev->bfreg);
6220 int mlx5_ib_stage_ib_reg_init(struct mlx5_ib_dev *dev)
6224 rdma_set_device_sysfs_group(&dev->ib_dev, &mlx5_attr_group);
6225 if (!mlx5_lag_is_active(dev->mdev))
6228 name = "mlx5_bond_%d";
6229 return ib_register_device(&dev->ib_dev, name, NULL);
6232 void mlx5_ib_stage_pre_ib_reg_umr_cleanup(struct mlx5_ib_dev *dev)
6234 destroy_umrc_res(dev);
6237 void mlx5_ib_stage_ib_reg_cleanup(struct mlx5_ib_dev *dev)
6239 ib_unregister_device(&dev->ib_dev);
6242 int mlx5_ib_stage_post_ib_reg_umr_init(struct mlx5_ib_dev *dev)
6244 return create_umr_res(dev);
6247 static int mlx5_ib_stage_delay_drop_init(struct mlx5_ib_dev *dev)
6249 init_delay_drop(dev);
6254 static void mlx5_ib_stage_delay_drop_cleanup(struct mlx5_ib_dev *dev)
6256 cancel_delay_drop(dev);
6259 static int mlx5_ib_stage_dev_notifier_init(struct mlx5_ib_dev *dev)
6261 dev->mdev_events.notifier_call = mlx5_ib_event;
6262 mlx5_notifier_register(dev->mdev, &dev->mdev_events);
6266 static void mlx5_ib_stage_dev_notifier_cleanup(struct mlx5_ib_dev *dev)
6268 mlx5_notifier_unregister(dev->mdev, &dev->mdev_events);
6271 static int mlx5_ib_stage_devx_init(struct mlx5_ib_dev *dev)
6275 uid = mlx5_ib_devx_create(dev, false);
6277 dev->devx_whitelist_uid = uid;
6281 static void mlx5_ib_stage_devx_cleanup(struct mlx5_ib_dev *dev)
6283 if (dev->devx_whitelist_uid)
6284 mlx5_ib_devx_destroy(dev, dev->devx_whitelist_uid);
6287 void __mlx5_ib_remove(struct mlx5_ib_dev *dev,
6288 const struct mlx5_ib_profile *profile,
6291 /* Number of stages to cleanup */
6294 if (profile->stage[stage].cleanup)
6295 profile->stage[stage].cleanup(dev);
6299 void *__mlx5_ib_add(struct mlx5_ib_dev *dev,
6300 const struct mlx5_ib_profile *profile)
6305 for (i = 0; i < MLX5_IB_STAGE_MAX; i++) {
6306 if (profile->stage[i].init) {
6307 err = profile->stage[i].init(dev);
6313 dev->profile = profile;
6314 dev->ib_active = true;
6319 __mlx5_ib_remove(dev, profile, i);
6324 static const struct mlx5_ib_profile pf_profile = {
6325 STAGE_CREATE(MLX5_IB_STAGE_INIT,
6326 mlx5_ib_stage_init_init,
6327 mlx5_ib_stage_init_cleanup),
6328 STAGE_CREATE(MLX5_IB_STAGE_FLOW_DB,
6329 mlx5_ib_stage_flow_db_init,
6330 mlx5_ib_stage_flow_db_cleanup),
6331 STAGE_CREATE(MLX5_IB_STAGE_CAPS,
6332 mlx5_ib_stage_caps_init,
6334 STAGE_CREATE(MLX5_IB_STAGE_NON_DEFAULT_CB,
6335 mlx5_ib_stage_non_default_cb,
6337 STAGE_CREATE(MLX5_IB_STAGE_ROCE,
6338 mlx5_ib_stage_roce_init,
6339 mlx5_ib_stage_roce_cleanup),
6340 STAGE_CREATE(MLX5_IB_STAGE_SRQ,
6341 mlx5_init_srq_table,
6342 mlx5_cleanup_srq_table),
6343 STAGE_CREATE(MLX5_IB_STAGE_DEVICE_RESOURCES,
6344 mlx5_ib_stage_dev_res_init,
6345 mlx5_ib_stage_dev_res_cleanup),
6346 STAGE_CREATE(MLX5_IB_STAGE_DEVICE_NOTIFIER,
6347 mlx5_ib_stage_dev_notifier_init,
6348 mlx5_ib_stage_dev_notifier_cleanup),
6349 STAGE_CREATE(MLX5_IB_STAGE_ODP,
6350 mlx5_ib_stage_odp_init,
6351 mlx5_ib_stage_odp_cleanup),
6352 STAGE_CREATE(MLX5_IB_STAGE_COUNTERS,
6353 mlx5_ib_stage_counters_init,
6354 mlx5_ib_stage_counters_cleanup),
6355 STAGE_CREATE(MLX5_IB_STAGE_CONG_DEBUGFS,
6356 mlx5_ib_stage_cong_debugfs_init,
6357 mlx5_ib_stage_cong_debugfs_cleanup),
6358 STAGE_CREATE(MLX5_IB_STAGE_UAR,
6359 mlx5_ib_stage_uar_init,
6360 mlx5_ib_stage_uar_cleanup),
6361 STAGE_CREATE(MLX5_IB_STAGE_BFREG,
6362 mlx5_ib_stage_bfrag_init,
6363 mlx5_ib_stage_bfrag_cleanup),
6364 STAGE_CREATE(MLX5_IB_STAGE_PRE_IB_REG_UMR,
6366 mlx5_ib_stage_pre_ib_reg_umr_cleanup),
6367 STAGE_CREATE(MLX5_IB_STAGE_WHITELIST_UID,
6368 mlx5_ib_stage_devx_init,
6369 mlx5_ib_stage_devx_cleanup),
6370 STAGE_CREATE(MLX5_IB_STAGE_IB_REG,
6371 mlx5_ib_stage_ib_reg_init,
6372 mlx5_ib_stage_ib_reg_cleanup),
6373 STAGE_CREATE(MLX5_IB_STAGE_POST_IB_REG_UMR,
6374 mlx5_ib_stage_post_ib_reg_umr_init,
6376 STAGE_CREATE(MLX5_IB_STAGE_DELAY_DROP,
6377 mlx5_ib_stage_delay_drop_init,
6378 mlx5_ib_stage_delay_drop_cleanup),
6381 static const struct mlx5_ib_profile nic_rep_profile = {
6382 STAGE_CREATE(MLX5_IB_STAGE_INIT,
6383 mlx5_ib_stage_init_init,
6384 mlx5_ib_stage_init_cleanup),
6385 STAGE_CREATE(MLX5_IB_STAGE_FLOW_DB,
6386 mlx5_ib_stage_flow_db_init,
6387 mlx5_ib_stage_flow_db_cleanup),
6388 STAGE_CREATE(MLX5_IB_STAGE_CAPS,
6389 mlx5_ib_stage_caps_init,
6391 STAGE_CREATE(MLX5_IB_STAGE_NON_DEFAULT_CB,
6392 mlx5_ib_stage_rep_non_default_cb,
6394 STAGE_CREATE(MLX5_IB_STAGE_ROCE,
6395 mlx5_ib_stage_rep_roce_init,
6396 mlx5_ib_stage_rep_roce_cleanup),
6397 STAGE_CREATE(MLX5_IB_STAGE_SRQ,
6398 mlx5_init_srq_table,
6399 mlx5_cleanup_srq_table),
6400 STAGE_CREATE(MLX5_IB_STAGE_DEVICE_RESOURCES,
6401 mlx5_ib_stage_dev_res_init,
6402 mlx5_ib_stage_dev_res_cleanup),
6403 STAGE_CREATE(MLX5_IB_STAGE_DEVICE_NOTIFIER,
6404 mlx5_ib_stage_dev_notifier_init,
6405 mlx5_ib_stage_dev_notifier_cleanup),
6406 STAGE_CREATE(MLX5_IB_STAGE_COUNTERS,
6407 mlx5_ib_stage_counters_init,
6408 mlx5_ib_stage_counters_cleanup),
6409 STAGE_CREATE(MLX5_IB_STAGE_UAR,
6410 mlx5_ib_stage_uar_init,
6411 mlx5_ib_stage_uar_cleanup),
6412 STAGE_CREATE(MLX5_IB_STAGE_BFREG,
6413 mlx5_ib_stage_bfrag_init,
6414 mlx5_ib_stage_bfrag_cleanup),
6415 STAGE_CREATE(MLX5_IB_STAGE_PRE_IB_REG_UMR,
6417 mlx5_ib_stage_pre_ib_reg_umr_cleanup),
6418 STAGE_CREATE(MLX5_IB_STAGE_IB_REG,
6419 mlx5_ib_stage_ib_reg_init,
6420 mlx5_ib_stage_ib_reg_cleanup),
6421 STAGE_CREATE(MLX5_IB_STAGE_POST_IB_REG_UMR,
6422 mlx5_ib_stage_post_ib_reg_umr_init,
6426 static void *mlx5_ib_add_slave_port(struct mlx5_core_dev *mdev)
6428 struct mlx5_ib_multiport_info *mpi;
6429 struct mlx5_ib_dev *dev;
6433 mpi = kzalloc(sizeof(*mpi), GFP_KERNEL);
6439 err = mlx5_query_nic_vport_system_image_guid(mdev,
6440 &mpi->sys_image_guid);
6446 mutex_lock(&mlx5_ib_multiport_mutex);
6447 list_for_each_entry(dev, &mlx5_ib_dev_list, ib_dev_list) {
6448 if (dev->sys_image_guid == mpi->sys_image_guid)
6449 bound = mlx5_ib_bind_slave_port(dev, mpi);
6452 rdma_roce_rescan_device(&dev->ib_dev);
6458 list_add_tail(&mpi->list, &mlx5_ib_unaffiliated_port_list);
6459 dev_dbg(&mdev->pdev->dev, "no suitable IB device found to bind to, added to unaffiliated list.\n");
6461 mutex_unlock(&mlx5_ib_multiport_mutex);
6466 static void *mlx5_ib_add(struct mlx5_core_dev *mdev)
6468 enum rdma_link_layer ll;
6469 struct mlx5_ib_dev *dev;
6472 printk_once(KERN_INFO "%s", mlx5_version);
6474 port_type_cap = MLX5_CAP_GEN(mdev, port_type);
6475 ll = mlx5_port_type_cap_to_rdma_ll(port_type_cap);
6477 if (mlx5_core_is_mp_slave(mdev) && ll == IB_LINK_LAYER_ETHERNET)
6478 return mlx5_ib_add_slave_port(mdev);
6480 dev = (struct mlx5_ib_dev *)ib_alloc_device(sizeof(*dev));
6485 dev->num_ports = max(MLX5_CAP_GEN(mdev, num_ports),
6486 MLX5_CAP_GEN(mdev, num_vhca_ports));
6488 if (MLX5_ESWITCH_MANAGER(mdev) &&
6489 mlx5_ib_eswitch_mode(mdev->priv.eswitch) == SRIOV_OFFLOADS) {
6490 dev->rep = mlx5_ib_vport_rep(mdev->priv.eswitch, 0);
6491 dev->profile = &nic_rep_profile;
6492 mlx5_ib_register_vport_reps(dev);
6496 return __mlx5_ib_add(dev, &pf_profile);
6499 static void mlx5_ib_remove(struct mlx5_core_dev *mdev, void *context)
6501 struct mlx5_ib_multiport_info *mpi;
6502 struct mlx5_ib_dev *dev;
6504 if (mlx5_core_is_mp_slave(mdev)) {
6506 mutex_lock(&mlx5_ib_multiport_mutex);
6508 mlx5_ib_unbind_slave_port(mpi->ibdev, mpi);
6509 list_del(&mpi->list);
6510 mutex_unlock(&mlx5_ib_multiport_mutex);
6515 if (dev->profile == &nic_rep_profile)
6516 mlx5_ib_unregister_vport_reps(dev);
6518 __mlx5_ib_remove(dev, dev->profile, MLX5_IB_STAGE_MAX);
6520 ib_dealloc_device((struct ib_device *)dev);
6523 static struct mlx5_interface mlx5_ib_interface = {
6525 .remove = mlx5_ib_remove,
6526 .protocol = MLX5_INTERFACE_PROTOCOL_IB,
6529 unsigned long mlx5_ib_get_xlt_emergency_page(void)
6531 mutex_lock(&xlt_emergency_page_mutex);
6532 return xlt_emergency_page;
6535 void mlx5_ib_put_xlt_emergency_page(void)
6537 mutex_unlock(&xlt_emergency_page_mutex);
6540 static int __init mlx5_ib_init(void)
6544 xlt_emergency_page = __get_free_page(GFP_KERNEL);
6545 if (!xlt_emergency_page)
6548 mutex_init(&xlt_emergency_page_mutex);
6550 mlx5_ib_event_wq = alloc_ordered_workqueue("mlx5_ib_event_wq", 0);
6551 if (!mlx5_ib_event_wq) {
6552 free_page(xlt_emergency_page);
6558 err = mlx5_register_interface(&mlx5_ib_interface);
6563 static void __exit mlx5_ib_cleanup(void)
6565 mlx5_unregister_interface(&mlx5_ib_interface);
6566 destroy_workqueue(mlx5_ib_event_wq);
6567 mutex_destroy(&xlt_emergency_page_mutex);
6568 free_page(xlt_emergency_page);
6571 module_init(mlx5_ib_init);
6572 module_exit(mlx5_ib_cleanup);