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/highmem.h>
34 #include <linux/module.h>
35 #include <linux/init.h>
36 #include <linux/errno.h>
37 #include <linux/pci.h>
38 #include <linux/dma-mapping.h>
39 #include <linux/slab.h>
40 #if defined(CONFIG_X86)
43 #include <linux/sched.h>
44 #include <linux/sched/mm.h>
45 #include <linux/sched/task.h>
46 #include <linux/delay.h>
47 #include <rdma/ib_user_verbs.h>
48 #include <rdma/ib_addr.h>
49 #include <rdma/ib_cache.h>
50 #include <linux/mlx5/port.h>
51 #include <linux/mlx5/vport.h>
52 #include <linux/list.h>
53 #include <rdma/ib_smi.h>
54 #include <rdma/ib_umem.h>
56 #include <linux/etherdevice.h>
57 #include <linux/mlx5/fs.h>
58 #include <linux/mlx5/vport.h>
62 #define DRIVER_NAME "mlx5_ib"
63 #define DRIVER_VERSION "5.0-0"
65 MODULE_AUTHOR("Eli Cohen <eli@mellanox.com>");
66 MODULE_DESCRIPTION("Mellanox Connect-IB HCA IB driver");
67 MODULE_LICENSE("Dual BSD/GPL");
68 MODULE_VERSION(DRIVER_VERSION);
70 static char mlx5_version[] =
71 DRIVER_NAME ": Mellanox Connect-IB Infiniband driver v"
75 MLX5_ATOMIC_SIZE_QP_8BYTES = 1 << 3,
78 static enum rdma_link_layer
79 mlx5_port_type_cap_to_rdma_ll(int port_type_cap)
81 switch (port_type_cap) {
82 case MLX5_CAP_PORT_TYPE_IB:
83 return IB_LINK_LAYER_INFINIBAND;
84 case MLX5_CAP_PORT_TYPE_ETH:
85 return IB_LINK_LAYER_ETHERNET;
87 return IB_LINK_LAYER_UNSPECIFIED;
91 static enum rdma_link_layer
92 mlx5_ib_port_link_layer(struct ib_device *device, u8 port_num)
94 struct mlx5_ib_dev *dev = to_mdev(device);
95 int port_type_cap = MLX5_CAP_GEN(dev->mdev, port_type);
97 return mlx5_port_type_cap_to_rdma_ll(port_type_cap);
100 static int mlx5_netdev_event(struct notifier_block *this,
101 unsigned long event, void *ptr)
103 struct net_device *ndev = netdev_notifier_info_to_dev(ptr);
104 struct mlx5_ib_dev *ibdev = container_of(this, struct mlx5_ib_dev,
108 case NETDEV_REGISTER:
109 case NETDEV_UNREGISTER:
110 write_lock(&ibdev->roce.netdev_lock);
111 if (ndev->dev.parent == &ibdev->mdev->pdev->dev)
112 ibdev->roce.netdev = (event == NETDEV_UNREGISTER) ?
114 write_unlock(&ibdev->roce.netdev_lock);
119 struct net_device *lag_ndev = mlx5_lag_get_roce_netdev(ibdev->mdev);
120 struct net_device *upper = NULL;
123 upper = netdev_master_upper_dev_get(lag_ndev);
127 if ((upper == ndev || (!upper && ndev == ibdev->roce.netdev))
128 && ibdev->ib_active) {
129 struct ib_event ibev = { };
131 ibev.device = &ibdev->ib_dev;
132 ibev.event = (event == NETDEV_UP) ?
133 IB_EVENT_PORT_ACTIVE : IB_EVENT_PORT_ERR;
134 ibev.element.port_num = 1;
135 ib_dispatch_event(&ibev);
147 static struct net_device *mlx5_ib_get_netdev(struct ib_device *device,
150 struct mlx5_ib_dev *ibdev = to_mdev(device);
151 struct net_device *ndev;
153 ndev = mlx5_lag_get_roce_netdev(ibdev->mdev);
157 /* Ensure ndev does not disappear before we invoke dev_hold()
159 read_lock(&ibdev->roce.netdev_lock);
160 ndev = ibdev->roce.netdev;
163 read_unlock(&ibdev->roce.netdev_lock);
168 static int translate_eth_proto_oper(u32 eth_proto_oper, u8 *active_speed,
171 switch (eth_proto_oper) {
172 case MLX5E_PROT_MASK(MLX5E_1000BASE_CX_SGMII):
173 case MLX5E_PROT_MASK(MLX5E_1000BASE_KX):
174 case MLX5E_PROT_MASK(MLX5E_100BASE_TX):
175 case MLX5E_PROT_MASK(MLX5E_1000BASE_T):
176 *active_width = IB_WIDTH_1X;
177 *active_speed = IB_SPEED_SDR;
179 case MLX5E_PROT_MASK(MLX5E_10GBASE_T):
180 case MLX5E_PROT_MASK(MLX5E_10GBASE_CX4):
181 case MLX5E_PROT_MASK(MLX5E_10GBASE_KX4):
182 case MLX5E_PROT_MASK(MLX5E_10GBASE_KR):
183 case MLX5E_PROT_MASK(MLX5E_10GBASE_CR):
184 case MLX5E_PROT_MASK(MLX5E_10GBASE_SR):
185 case MLX5E_PROT_MASK(MLX5E_10GBASE_ER):
186 *active_width = IB_WIDTH_1X;
187 *active_speed = IB_SPEED_QDR;
189 case MLX5E_PROT_MASK(MLX5E_25GBASE_CR):
190 case MLX5E_PROT_MASK(MLX5E_25GBASE_KR):
191 case MLX5E_PROT_MASK(MLX5E_25GBASE_SR):
192 *active_width = IB_WIDTH_1X;
193 *active_speed = IB_SPEED_EDR;
195 case MLX5E_PROT_MASK(MLX5E_40GBASE_CR4):
196 case MLX5E_PROT_MASK(MLX5E_40GBASE_KR4):
197 case MLX5E_PROT_MASK(MLX5E_40GBASE_SR4):
198 case MLX5E_PROT_MASK(MLX5E_40GBASE_LR4):
199 *active_width = IB_WIDTH_4X;
200 *active_speed = IB_SPEED_QDR;
202 case MLX5E_PROT_MASK(MLX5E_50GBASE_CR2):
203 case MLX5E_PROT_MASK(MLX5E_50GBASE_KR2):
204 case MLX5E_PROT_MASK(MLX5E_50GBASE_SR2):
205 *active_width = IB_WIDTH_1X;
206 *active_speed = IB_SPEED_HDR;
208 case MLX5E_PROT_MASK(MLX5E_56GBASE_R4):
209 *active_width = IB_WIDTH_4X;
210 *active_speed = IB_SPEED_FDR;
212 case MLX5E_PROT_MASK(MLX5E_100GBASE_CR4):
213 case MLX5E_PROT_MASK(MLX5E_100GBASE_SR4):
214 case MLX5E_PROT_MASK(MLX5E_100GBASE_KR4):
215 case MLX5E_PROT_MASK(MLX5E_100GBASE_LR4):
216 *active_width = IB_WIDTH_4X;
217 *active_speed = IB_SPEED_EDR;
226 static int mlx5_query_port_roce(struct ib_device *device, u8 port_num,
227 struct ib_port_attr *props)
229 struct mlx5_ib_dev *dev = to_mdev(device);
230 struct mlx5_core_dev *mdev = dev->mdev;
231 struct net_device *ndev, *upper;
232 enum ib_mtu ndev_ib_mtu;
237 /* Possible bad flows are checked before filling out props so in case
238 * of an error it will still be zeroed out.
240 err = mlx5_query_port_eth_proto_oper(mdev, ð_prot_oper, port_num);
244 translate_eth_proto_oper(eth_prot_oper, &props->active_speed,
245 &props->active_width);
247 props->port_cap_flags |= IB_PORT_CM_SUP;
248 props->port_cap_flags |= IB_PORT_IP_BASED_GIDS;
250 props->gid_tbl_len = MLX5_CAP_ROCE(dev->mdev,
251 roce_address_table_size);
252 props->max_mtu = IB_MTU_4096;
253 props->max_msg_sz = 1 << MLX5_CAP_GEN(dev->mdev, log_max_msg);
254 props->pkey_tbl_len = 1;
255 props->state = IB_PORT_DOWN;
256 props->phys_state = 3;
258 mlx5_query_nic_vport_qkey_viol_cntr(dev->mdev, &qkey_viol_cntr);
259 props->qkey_viol_cntr = qkey_viol_cntr;
261 ndev = mlx5_ib_get_netdev(device, port_num);
265 if (mlx5_lag_is_active(dev->mdev)) {
267 upper = netdev_master_upper_dev_get_rcu(ndev);
276 if (netif_running(ndev) && netif_carrier_ok(ndev)) {
277 props->state = IB_PORT_ACTIVE;
278 props->phys_state = 5;
281 ndev_ib_mtu = iboe_get_mtu(ndev->mtu);
285 props->active_mtu = min(props->max_mtu, ndev_ib_mtu);
289 static int set_roce_addr(struct mlx5_ib_dev *dev, u8 port_num,
290 unsigned int index, const union ib_gid *gid,
291 const struct ib_gid_attr *attr)
293 enum ib_gid_type gid_type = IB_GID_TYPE_IB;
301 gid_type = attr->gid_type;
302 ether_addr_copy(mac, attr->ndev->dev_addr);
304 if (is_vlan_dev(attr->ndev)) {
306 vlan_id = vlan_dev_vlan_id(attr->ndev);
312 roce_version = MLX5_ROCE_VERSION_1;
314 case IB_GID_TYPE_ROCE_UDP_ENCAP:
315 roce_version = MLX5_ROCE_VERSION_2;
316 if (ipv6_addr_v4mapped((void *)gid))
317 roce_l3_type = MLX5_ROCE_L3_TYPE_IPV4;
319 roce_l3_type = MLX5_ROCE_L3_TYPE_IPV6;
323 mlx5_ib_warn(dev, "Unexpected GID type %u\n", gid_type);
326 return mlx5_core_roce_gid_set(dev->mdev, index, roce_version,
327 roce_l3_type, gid->raw, mac, vlan,
331 static int mlx5_ib_add_gid(struct ib_device *device, u8 port_num,
332 unsigned int index, const union ib_gid *gid,
333 const struct ib_gid_attr *attr,
334 __always_unused void **context)
336 return set_roce_addr(to_mdev(device), port_num, index, gid, attr);
339 static int mlx5_ib_del_gid(struct ib_device *device, u8 port_num,
340 unsigned int index, __always_unused void **context)
342 return set_roce_addr(to_mdev(device), port_num, index, NULL, NULL);
345 __be16 mlx5_get_roce_udp_sport(struct mlx5_ib_dev *dev, u8 port_num,
348 struct ib_gid_attr attr;
351 if (ib_get_cached_gid(&dev->ib_dev, port_num, index, &gid, &attr))
359 if (attr.gid_type != IB_GID_TYPE_ROCE_UDP_ENCAP)
362 return cpu_to_be16(MLX5_CAP_ROCE(dev->mdev, r_roce_min_src_udp_port));
365 int mlx5_get_roce_gid_type(struct mlx5_ib_dev *dev, u8 port_num,
366 int index, enum ib_gid_type *gid_type)
368 struct ib_gid_attr attr;
372 ret = ib_get_cached_gid(&dev->ib_dev, port_num, index, &gid, &attr);
381 *gid_type = attr.gid_type;
386 static int mlx5_use_mad_ifc(struct mlx5_ib_dev *dev)
388 if (MLX5_CAP_GEN(dev->mdev, port_type) == MLX5_CAP_PORT_TYPE_IB)
389 return !MLX5_CAP_GEN(dev->mdev, ib_virt);
394 MLX5_VPORT_ACCESS_METHOD_MAD,
395 MLX5_VPORT_ACCESS_METHOD_HCA,
396 MLX5_VPORT_ACCESS_METHOD_NIC,
399 static int mlx5_get_vport_access_method(struct ib_device *ibdev)
401 if (mlx5_use_mad_ifc(to_mdev(ibdev)))
402 return MLX5_VPORT_ACCESS_METHOD_MAD;
404 if (mlx5_ib_port_link_layer(ibdev, 1) ==
405 IB_LINK_LAYER_ETHERNET)
406 return MLX5_VPORT_ACCESS_METHOD_NIC;
408 return MLX5_VPORT_ACCESS_METHOD_HCA;
411 static void get_atomic_caps(struct mlx5_ib_dev *dev,
412 struct ib_device_attr *props)
415 u8 atomic_operations = MLX5_CAP_ATOMIC(dev->mdev, atomic_operations);
416 u8 atomic_size_qp = MLX5_CAP_ATOMIC(dev->mdev, atomic_size_qp);
417 u8 atomic_req_8B_endianness_mode =
418 MLX5_CAP_ATOMIC(dev->mdev, atomic_req_8B_endianness_mode);
420 /* Check if HW supports 8 bytes standard atomic operations and capable
421 * of host endianness respond
423 tmp = MLX5_ATOMIC_OPS_CMP_SWAP | MLX5_ATOMIC_OPS_FETCH_ADD;
424 if (((atomic_operations & tmp) == tmp) &&
425 (atomic_size_qp & MLX5_ATOMIC_SIZE_QP_8BYTES) &&
426 (atomic_req_8B_endianness_mode)) {
427 props->atomic_cap = IB_ATOMIC_HCA;
429 props->atomic_cap = IB_ATOMIC_NONE;
433 static int mlx5_query_system_image_guid(struct ib_device *ibdev,
434 __be64 *sys_image_guid)
436 struct mlx5_ib_dev *dev = to_mdev(ibdev);
437 struct mlx5_core_dev *mdev = dev->mdev;
441 switch (mlx5_get_vport_access_method(ibdev)) {
442 case MLX5_VPORT_ACCESS_METHOD_MAD:
443 return mlx5_query_mad_ifc_system_image_guid(ibdev,
446 case MLX5_VPORT_ACCESS_METHOD_HCA:
447 err = mlx5_query_hca_vport_system_image_guid(mdev, &tmp);
450 case MLX5_VPORT_ACCESS_METHOD_NIC:
451 err = mlx5_query_nic_vport_system_image_guid(mdev, &tmp);
459 *sys_image_guid = cpu_to_be64(tmp);
465 static int mlx5_query_max_pkeys(struct ib_device *ibdev,
468 struct mlx5_ib_dev *dev = to_mdev(ibdev);
469 struct mlx5_core_dev *mdev = dev->mdev;
471 switch (mlx5_get_vport_access_method(ibdev)) {
472 case MLX5_VPORT_ACCESS_METHOD_MAD:
473 return mlx5_query_mad_ifc_max_pkeys(ibdev, max_pkeys);
475 case MLX5_VPORT_ACCESS_METHOD_HCA:
476 case MLX5_VPORT_ACCESS_METHOD_NIC:
477 *max_pkeys = mlx5_to_sw_pkey_sz(MLX5_CAP_GEN(mdev,
486 static int mlx5_query_vendor_id(struct ib_device *ibdev,
489 struct mlx5_ib_dev *dev = to_mdev(ibdev);
491 switch (mlx5_get_vport_access_method(ibdev)) {
492 case MLX5_VPORT_ACCESS_METHOD_MAD:
493 return mlx5_query_mad_ifc_vendor_id(ibdev, vendor_id);
495 case MLX5_VPORT_ACCESS_METHOD_HCA:
496 case MLX5_VPORT_ACCESS_METHOD_NIC:
497 return mlx5_core_query_vendor_id(dev->mdev, vendor_id);
504 static int mlx5_query_node_guid(struct mlx5_ib_dev *dev,
510 switch (mlx5_get_vport_access_method(&dev->ib_dev)) {
511 case MLX5_VPORT_ACCESS_METHOD_MAD:
512 return mlx5_query_mad_ifc_node_guid(dev, node_guid);
514 case MLX5_VPORT_ACCESS_METHOD_HCA:
515 err = mlx5_query_hca_vport_node_guid(dev->mdev, &tmp);
518 case MLX5_VPORT_ACCESS_METHOD_NIC:
519 err = mlx5_query_nic_vport_node_guid(dev->mdev, &tmp);
527 *node_guid = cpu_to_be64(tmp);
532 struct mlx5_reg_node_desc {
533 u8 desc[IB_DEVICE_NODE_DESC_MAX];
536 static int mlx5_query_node_desc(struct mlx5_ib_dev *dev, char *node_desc)
538 struct mlx5_reg_node_desc in;
540 if (mlx5_use_mad_ifc(dev))
541 return mlx5_query_mad_ifc_node_desc(dev, node_desc);
543 memset(&in, 0, sizeof(in));
545 return mlx5_core_access_reg(dev->mdev, &in, sizeof(in), node_desc,
546 sizeof(struct mlx5_reg_node_desc),
547 MLX5_REG_NODE_DESC, 0, 0);
550 static int mlx5_ib_query_device(struct ib_device *ibdev,
551 struct ib_device_attr *props,
552 struct ib_udata *uhw)
554 struct mlx5_ib_dev *dev = to_mdev(ibdev);
555 struct mlx5_core_dev *mdev = dev->mdev;
560 u64 min_page_size = 1ull << MLX5_CAP_GEN(mdev, log_pg_sz);
561 struct mlx5_ib_query_device_resp resp = {};
565 resp_len = sizeof(resp.comp_mask) + sizeof(resp.response_length);
566 if (uhw->outlen && uhw->outlen < resp_len)
569 resp.response_length = resp_len;
571 if (uhw->inlen && !ib_is_udata_cleared(uhw, 0, uhw->inlen))
574 memset(props, 0, sizeof(*props));
575 err = mlx5_query_system_image_guid(ibdev,
576 &props->sys_image_guid);
580 err = mlx5_query_max_pkeys(ibdev, &props->max_pkeys);
584 err = mlx5_query_vendor_id(ibdev, &props->vendor_id);
588 props->fw_ver = ((u64)fw_rev_maj(dev->mdev) << 32) |
589 (fw_rev_min(dev->mdev) << 16) |
590 fw_rev_sub(dev->mdev);
591 props->device_cap_flags = IB_DEVICE_CHANGE_PHY_PORT |
592 IB_DEVICE_PORT_ACTIVE_EVENT |
593 IB_DEVICE_SYS_IMAGE_GUID |
594 IB_DEVICE_RC_RNR_NAK_GEN;
596 if (MLX5_CAP_GEN(mdev, pkv))
597 props->device_cap_flags |= IB_DEVICE_BAD_PKEY_CNTR;
598 if (MLX5_CAP_GEN(mdev, qkv))
599 props->device_cap_flags |= IB_DEVICE_BAD_QKEY_CNTR;
600 if (MLX5_CAP_GEN(mdev, apm))
601 props->device_cap_flags |= IB_DEVICE_AUTO_PATH_MIG;
602 if (MLX5_CAP_GEN(mdev, xrc))
603 props->device_cap_flags |= IB_DEVICE_XRC;
604 if (MLX5_CAP_GEN(mdev, imaicl)) {
605 props->device_cap_flags |= IB_DEVICE_MEM_WINDOW |
606 IB_DEVICE_MEM_WINDOW_TYPE_2B;
607 props->max_mw = 1 << MLX5_CAP_GEN(mdev, log_max_mkey);
608 /* We support 'Gappy' memory registration too */
609 props->device_cap_flags |= IB_DEVICE_SG_GAPS_REG;
611 props->device_cap_flags |= IB_DEVICE_MEM_MGT_EXTENSIONS;
612 if (MLX5_CAP_GEN(mdev, sho)) {
613 props->device_cap_flags |= IB_DEVICE_SIGNATURE_HANDOVER;
614 /* At this stage no support for signature handover */
615 props->sig_prot_cap = IB_PROT_T10DIF_TYPE_1 |
616 IB_PROT_T10DIF_TYPE_2 |
617 IB_PROT_T10DIF_TYPE_3;
618 props->sig_guard_cap = IB_GUARD_T10DIF_CRC |
619 IB_GUARD_T10DIF_CSUM;
621 if (MLX5_CAP_GEN(mdev, block_lb_mc))
622 props->device_cap_flags |= IB_DEVICE_BLOCK_MULTICAST_LOOPBACK;
624 if (MLX5_CAP_GEN(dev->mdev, eth_net_offloads)) {
625 if (MLX5_CAP_ETH(mdev, csum_cap)) {
626 /* Legacy bit to support old userspace libraries */
627 props->device_cap_flags |= IB_DEVICE_RAW_IP_CSUM;
628 props->raw_packet_caps |= IB_RAW_PACKET_CAP_IP_CSUM;
631 if (MLX5_CAP_ETH(dev->mdev, vlan_cap))
632 props->raw_packet_caps |=
633 IB_RAW_PACKET_CAP_CVLAN_STRIPPING;
635 if (field_avail(typeof(resp), tso_caps, uhw->outlen)) {
636 max_tso = MLX5_CAP_ETH(mdev, max_lso_cap);
638 resp.tso_caps.max_tso = 1 << max_tso;
639 resp.tso_caps.supported_qpts |=
640 1 << IB_QPT_RAW_PACKET;
641 resp.response_length += sizeof(resp.tso_caps);
645 if (field_avail(typeof(resp), rss_caps, uhw->outlen)) {
646 resp.rss_caps.rx_hash_function =
647 MLX5_RX_HASH_FUNC_TOEPLITZ;
648 resp.rss_caps.rx_hash_fields_mask =
649 MLX5_RX_HASH_SRC_IPV4 |
650 MLX5_RX_HASH_DST_IPV4 |
651 MLX5_RX_HASH_SRC_IPV6 |
652 MLX5_RX_HASH_DST_IPV6 |
653 MLX5_RX_HASH_SRC_PORT_TCP |
654 MLX5_RX_HASH_DST_PORT_TCP |
655 MLX5_RX_HASH_SRC_PORT_UDP |
656 MLX5_RX_HASH_DST_PORT_UDP;
657 resp.response_length += sizeof(resp.rss_caps);
660 if (field_avail(typeof(resp), tso_caps, uhw->outlen))
661 resp.response_length += sizeof(resp.tso_caps);
662 if (field_avail(typeof(resp), rss_caps, uhw->outlen))
663 resp.response_length += sizeof(resp.rss_caps);
666 if (MLX5_CAP_GEN(mdev, ipoib_basic_offloads)) {
667 props->device_cap_flags |= IB_DEVICE_UD_IP_CSUM;
668 props->device_cap_flags |= IB_DEVICE_UD_TSO;
671 if (MLX5_CAP_GEN(dev->mdev, eth_net_offloads) &&
672 MLX5_CAP_ETH(dev->mdev, scatter_fcs)) {
673 /* Legacy bit to support old userspace libraries */
674 props->device_cap_flags |= IB_DEVICE_RAW_SCATTER_FCS;
675 props->raw_packet_caps |= IB_RAW_PACKET_CAP_SCATTER_FCS;
678 if (mlx5_get_flow_namespace(dev->mdev, MLX5_FLOW_NAMESPACE_BYPASS))
679 props->device_cap_flags |= IB_DEVICE_MANAGED_FLOW_STEERING;
681 props->vendor_part_id = mdev->pdev->device;
682 props->hw_ver = mdev->pdev->revision;
684 props->max_mr_size = ~0ull;
685 props->page_size_cap = ~(min_page_size - 1);
686 props->max_qp = 1 << MLX5_CAP_GEN(mdev, log_max_qp);
687 props->max_qp_wr = 1 << MLX5_CAP_GEN(mdev, log_max_qp_sz);
688 max_rq_sg = MLX5_CAP_GEN(mdev, max_wqe_sz_rq) /
689 sizeof(struct mlx5_wqe_data_seg);
690 max_sq_desc = min_t(int, MLX5_CAP_GEN(mdev, max_wqe_sz_sq), 512);
691 max_sq_sg = (max_sq_desc - sizeof(struct mlx5_wqe_ctrl_seg) -
692 sizeof(struct mlx5_wqe_raddr_seg)) /
693 sizeof(struct mlx5_wqe_data_seg);
694 props->max_sge = min(max_rq_sg, max_sq_sg);
695 props->max_sge_rd = MLX5_MAX_SGE_RD;
696 props->max_cq = 1 << MLX5_CAP_GEN(mdev, log_max_cq);
697 props->max_cqe = (1 << MLX5_CAP_GEN(mdev, log_max_cq_sz)) - 1;
698 props->max_mr = 1 << MLX5_CAP_GEN(mdev, log_max_mkey);
699 props->max_pd = 1 << MLX5_CAP_GEN(mdev, log_max_pd);
700 props->max_qp_rd_atom = 1 << MLX5_CAP_GEN(mdev, log_max_ra_req_qp);
701 props->max_qp_init_rd_atom = 1 << MLX5_CAP_GEN(mdev, log_max_ra_res_qp);
702 props->max_srq = 1 << MLX5_CAP_GEN(mdev, log_max_srq);
703 props->max_srq_wr = (1 << MLX5_CAP_GEN(mdev, log_max_srq_sz)) - 1;
704 props->local_ca_ack_delay = MLX5_CAP_GEN(mdev, local_ca_ack_delay);
705 props->max_res_rd_atom = props->max_qp_rd_atom * props->max_qp;
706 props->max_srq_sge = max_rq_sg - 1;
707 props->max_fast_reg_page_list_len =
708 1 << MLX5_CAP_GEN(mdev, log_max_klm_list_size);
709 get_atomic_caps(dev, props);
710 props->masked_atomic_cap = IB_ATOMIC_NONE;
711 props->max_mcast_grp = 1 << MLX5_CAP_GEN(mdev, log_max_mcg);
712 props->max_mcast_qp_attach = MLX5_CAP_GEN(mdev, max_qp_mcg);
713 props->max_total_mcast_qp_attach = props->max_mcast_qp_attach *
714 props->max_mcast_grp;
715 props->max_map_per_fmr = INT_MAX; /* no limit in ConnectIB */
716 props->max_ah = INT_MAX;
717 props->hca_core_clock = MLX5_CAP_GEN(mdev, device_frequency_khz);
718 props->timestamp_mask = 0x7FFFFFFFFFFFFFFFULL;
720 #ifdef CONFIG_INFINIBAND_ON_DEMAND_PAGING
721 if (MLX5_CAP_GEN(mdev, pg))
722 props->device_cap_flags |= IB_DEVICE_ON_DEMAND_PAGING;
723 props->odp_caps = dev->odp_caps;
726 if (MLX5_CAP_GEN(mdev, cd))
727 props->device_cap_flags |= IB_DEVICE_CROSS_CHANNEL;
729 if (!mlx5_core_is_pf(mdev))
730 props->device_cap_flags |= IB_DEVICE_VIRTUAL_FUNCTION;
732 if (mlx5_ib_port_link_layer(ibdev, 1) ==
733 IB_LINK_LAYER_ETHERNET) {
734 props->rss_caps.max_rwq_indirection_tables =
735 1 << MLX5_CAP_GEN(dev->mdev, log_max_rqt);
736 props->rss_caps.max_rwq_indirection_table_size =
737 1 << MLX5_CAP_GEN(dev->mdev, log_max_rqt_size);
738 props->rss_caps.supported_qpts = 1 << IB_QPT_RAW_PACKET;
739 props->max_wq_type_rq =
740 1 << MLX5_CAP_GEN(dev->mdev, log_max_rq);
743 if (field_avail(typeof(resp), cqe_comp_caps, uhw->outlen)) {
744 resp.cqe_comp_caps.max_num =
745 MLX5_CAP_GEN(dev->mdev, cqe_compression) ?
746 MLX5_CAP_GEN(dev->mdev, cqe_compression_max_num) : 0;
747 resp.cqe_comp_caps.supported_format =
748 MLX5_IB_CQE_RES_FORMAT_HASH |
749 MLX5_IB_CQE_RES_FORMAT_CSUM;
750 resp.response_length += sizeof(resp.cqe_comp_caps);
753 if (field_avail(typeof(resp), packet_pacing_caps, uhw->outlen)) {
754 if (MLX5_CAP_QOS(mdev, packet_pacing) &&
755 MLX5_CAP_GEN(mdev, qos)) {
756 resp.packet_pacing_caps.qp_rate_limit_max =
757 MLX5_CAP_QOS(mdev, packet_pacing_max_rate);
758 resp.packet_pacing_caps.qp_rate_limit_min =
759 MLX5_CAP_QOS(mdev, packet_pacing_min_rate);
760 resp.packet_pacing_caps.supported_qpts |=
761 1 << IB_QPT_RAW_PACKET;
763 resp.response_length += sizeof(resp.packet_pacing_caps);
766 if (field_avail(typeof(resp), mlx5_ib_support_multi_pkt_send_wqes,
768 resp.mlx5_ib_support_multi_pkt_send_wqes =
769 MLX5_CAP_ETH(mdev, multi_pkt_send_wqe);
770 resp.response_length +=
771 sizeof(resp.mlx5_ib_support_multi_pkt_send_wqes);
774 if (field_avail(typeof(resp), reserved, uhw->outlen))
775 resp.response_length += sizeof(resp.reserved);
778 err = ib_copy_to_udata(uhw, &resp, resp.response_length);
788 MLX5_IB_WIDTH_1X = 1 << 0,
789 MLX5_IB_WIDTH_2X = 1 << 1,
790 MLX5_IB_WIDTH_4X = 1 << 2,
791 MLX5_IB_WIDTH_8X = 1 << 3,
792 MLX5_IB_WIDTH_12X = 1 << 4
795 static int translate_active_width(struct ib_device *ibdev, u8 active_width,
798 struct mlx5_ib_dev *dev = to_mdev(ibdev);
801 if (active_width & MLX5_IB_WIDTH_1X) {
802 *ib_width = IB_WIDTH_1X;
803 } else if (active_width & MLX5_IB_WIDTH_2X) {
804 mlx5_ib_dbg(dev, "active_width %d is not supported by IB spec\n",
807 } else if (active_width & MLX5_IB_WIDTH_4X) {
808 *ib_width = IB_WIDTH_4X;
809 } else if (active_width & MLX5_IB_WIDTH_8X) {
810 *ib_width = IB_WIDTH_8X;
811 } else if (active_width & MLX5_IB_WIDTH_12X) {
812 *ib_width = IB_WIDTH_12X;
814 mlx5_ib_dbg(dev, "Invalid active_width %d\n",
822 static int mlx5_mtu_to_ib_mtu(int mtu)
831 pr_warn("invalid mtu\n");
841 __IB_MAX_VL_0_14 = 5,
844 enum mlx5_vl_hw_cap {
856 static int translate_max_vl_num(struct ib_device *ibdev, u8 vl_hw_cap,
861 *max_vl_num = __IB_MAX_VL_0;
864 *max_vl_num = __IB_MAX_VL_0_1;
867 *max_vl_num = __IB_MAX_VL_0_3;
870 *max_vl_num = __IB_MAX_VL_0_7;
872 case MLX5_VL_HW_0_14:
873 *max_vl_num = __IB_MAX_VL_0_14;
883 static int mlx5_query_hca_port(struct ib_device *ibdev, u8 port,
884 struct ib_port_attr *props)
886 struct mlx5_ib_dev *dev = to_mdev(ibdev);
887 struct mlx5_core_dev *mdev = dev->mdev;
888 struct mlx5_hca_vport_context *rep;
892 u8 ib_link_width_oper;
895 rep = kzalloc(sizeof(*rep), GFP_KERNEL);
901 /* props being zeroed by the caller, avoid zeroing it here */
903 err = mlx5_query_hca_vport_context(mdev, 0, port, 0, rep);
907 props->lid = rep->lid;
908 props->lmc = rep->lmc;
909 props->sm_lid = rep->sm_lid;
910 props->sm_sl = rep->sm_sl;
911 props->state = rep->vport_state;
912 props->phys_state = rep->port_physical_state;
913 props->port_cap_flags = rep->cap_mask1;
914 props->gid_tbl_len = mlx5_get_gid_table_len(MLX5_CAP_GEN(mdev, gid_table_size));
915 props->max_msg_sz = 1 << MLX5_CAP_GEN(mdev, log_max_msg);
916 props->pkey_tbl_len = mlx5_to_sw_pkey_sz(MLX5_CAP_GEN(mdev, pkey_table_size));
917 props->bad_pkey_cntr = rep->pkey_violation_counter;
918 props->qkey_viol_cntr = rep->qkey_violation_counter;
919 props->subnet_timeout = rep->subnet_timeout;
920 props->init_type_reply = rep->init_type_reply;
921 props->grh_required = rep->grh_required;
923 err = mlx5_query_port_link_width_oper(mdev, &ib_link_width_oper, port);
927 err = translate_active_width(ibdev, ib_link_width_oper,
928 &props->active_width);
931 err = mlx5_query_port_ib_proto_oper(mdev, &props->active_speed, port);
935 mlx5_query_port_max_mtu(mdev, &max_mtu, port);
937 props->max_mtu = mlx5_mtu_to_ib_mtu(max_mtu);
939 mlx5_query_port_oper_mtu(mdev, &oper_mtu, port);
941 props->active_mtu = mlx5_mtu_to_ib_mtu(oper_mtu);
943 err = mlx5_query_port_vl_hw_cap(mdev, &vl_hw_cap, port);
947 err = translate_max_vl_num(ibdev, vl_hw_cap,
954 int mlx5_ib_query_port(struct ib_device *ibdev, u8 port,
955 struct ib_port_attr *props)
960 switch (mlx5_get_vport_access_method(ibdev)) {
961 case MLX5_VPORT_ACCESS_METHOD_MAD:
962 ret = mlx5_query_mad_ifc_port(ibdev, port, props);
965 case MLX5_VPORT_ACCESS_METHOD_HCA:
966 ret = mlx5_query_hca_port(ibdev, port, props);
969 case MLX5_VPORT_ACCESS_METHOD_NIC:
970 ret = mlx5_query_port_roce(ibdev, port, props);
978 count = mlx5_core_reserved_gids_count(to_mdev(ibdev)->mdev);
979 props->gid_tbl_len -= count;
984 static int mlx5_ib_query_gid(struct ib_device *ibdev, u8 port, int index,
987 struct mlx5_ib_dev *dev = to_mdev(ibdev);
988 struct mlx5_core_dev *mdev = dev->mdev;
990 switch (mlx5_get_vport_access_method(ibdev)) {
991 case MLX5_VPORT_ACCESS_METHOD_MAD:
992 return mlx5_query_mad_ifc_gids(ibdev, port, index, gid);
994 case MLX5_VPORT_ACCESS_METHOD_HCA:
995 return mlx5_query_hca_vport_gid(mdev, 0, port, 0, index, gid);
1003 static int mlx5_ib_query_pkey(struct ib_device *ibdev, u8 port, u16 index,
1006 struct mlx5_ib_dev *dev = to_mdev(ibdev);
1007 struct mlx5_core_dev *mdev = dev->mdev;
1009 switch (mlx5_get_vport_access_method(ibdev)) {
1010 case MLX5_VPORT_ACCESS_METHOD_MAD:
1011 return mlx5_query_mad_ifc_pkey(ibdev, port, index, pkey);
1013 case MLX5_VPORT_ACCESS_METHOD_HCA:
1014 case MLX5_VPORT_ACCESS_METHOD_NIC:
1015 return mlx5_query_hca_vport_pkey(mdev, 0, port, 0, index,
1022 static int mlx5_ib_modify_device(struct ib_device *ibdev, int mask,
1023 struct ib_device_modify *props)
1025 struct mlx5_ib_dev *dev = to_mdev(ibdev);
1026 struct mlx5_reg_node_desc in;
1027 struct mlx5_reg_node_desc out;
1030 if (mask & ~IB_DEVICE_MODIFY_NODE_DESC)
1033 if (!(mask & IB_DEVICE_MODIFY_NODE_DESC))
1037 * If possible, pass node desc to FW, so it can generate
1038 * a 144 trap. If cmd fails, just ignore.
1040 memcpy(&in, props->node_desc, IB_DEVICE_NODE_DESC_MAX);
1041 err = mlx5_core_access_reg(dev->mdev, &in, sizeof(in), &out,
1042 sizeof(out), MLX5_REG_NODE_DESC, 0, 1);
1046 memcpy(ibdev->node_desc, props->node_desc, IB_DEVICE_NODE_DESC_MAX);
1051 static int set_port_caps_atomic(struct mlx5_ib_dev *dev, u8 port_num, u32 mask,
1054 struct mlx5_hca_vport_context ctx = {};
1057 err = mlx5_query_hca_vport_context(dev->mdev, 0,
1062 if (~ctx.cap_mask1_perm & mask) {
1063 mlx5_ib_warn(dev, "trying to change bitmask 0x%X but change supported 0x%X\n",
1064 mask, ctx.cap_mask1_perm);
1068 ctx.cap_mask1 = value;
1069 ctx.cap_mask1_perm = mask;
1070 err = mlx5_core_modify_hca_vport_context(dev->mdev, 0,
1076 static int mlx5_ib_modify_port(struct ib_device *ibdev, u8 port, int mask,
1077 struct ib_port_modify *props)
1079 struct mlx5_ib_dev *dev = to_mdev(ibdev);
1080 struct ib_port_attr attr;
1085 bool is_ib = (mlx5_ib_port_link_layer(ibdev, port) ==
1086 IB_LINK_LAYER_INFINIBAND);
1088 if (MLX5_CAP_GEN(dev->mdev, ib_virt) && is_ib) {
1089 change_mask = props->clr_port_cap_mask | props->set_port_cap_mask;
1090 value = ~props->clr_port_cap_mask | props->set_port_cap_mask;
1091 return set_port_caps_atomic(dev, port, change_mask, value);
1094 mutex_lock(&dev->cap_mask_mutex);
1096 err = ib_query_port(ibdev, port, &attr);
1100 tmp = (attr.port_cap_flags | props->set_port_cap_mask) &
1101 ~props->clr_port_cap_mask;
1103 err = mlx5_set_port_caps(dev->mdev, port, tmp);
1106 mutex_unlock(&dev->cap_mask_mutex);
1110 static void print_lib_caps(struct mlx5_ib_dev *dev, u64 caps)
1112 mlx5_ib_dbg(dev, "MLX5_LIB_CAP_4K_UAR = %s\n",
1113 caps & MLX5_LIB_CAP_4K_UAR ? "y" : "n");
1116 static int calc_total_bfregs(struct mlx5_ib_dev *dev, bool lib_uar_4k,
1117 struct mlx5_ib_alloc_ucontext_req_v2 *req,
1120 int uars_per_sys_page;
1121 int bfregs_per_sys_page;
1122 int ref_bfregs = req->total_num_bfregs;
1124 if (req->total_num_bfregs == 0)
1127 BUILD_BUG_ON(MLX5_MAX_BFREGS % MLX5_NON_FP_BFREGS_IN_PAGE);
1128 BUILD_BUG_ON(MLX5_MAX_BFREGS < MLX5_NON_FP_BFREGS_IN_PAGE);
1130 if (req->total_num_bfregs > MLX5_MAX_BFREGS)
1133 uars_per_sys_page = get_uars_per_sys_page(dev, lib_uar_4k);
1134 bfregs_per_sys_page = uars_per_sys_page * MLX5_NON_FP_BFREGS_PER_UAR;
1135 req->total_num_bfregs = ALIGN(req->total_num_bfregs, bfregs_per_sys_page);
1136 *num_sys_pages = req->total_num_bfregs / bfregs_per_sys_page;
1138 if (req->num_low_latency_bfregs > req->total_num_bfregs - 1)
1141 mlx5_ib_dbg(dev, "uar_4k: fw support %s, lib support %s, user requested %d bfregs, alloated %d, using %d sys pages\n",
1142 MLX5_CAP_GEN(dev->mdev, uar_4k) ? "yes" : "no",
1143 lib_uar_4k ? "yes" : "no", ref_bfregs,
1144 req->total_num_bfregs, *num_sys_pages);
1149 static int allocate_uars(struct mlx5_ib_dev *dev, struct mlx5_ib_ucontext *context)
1151 struct mlx5_bfreg_info *bfregi;
1155 bfregi = &context->bfregi;
1156 for (i = 0; i < bfregi->num_sys_pages; i++) {
1157 err = mlx5_cmd_alloc_uar(dev->mdev, &bfregi->sys_pages[i]);
1161 mlx5_ib_dbg(dev, "allocated uar %d\n", bfregi->sys_pages[i]);
1166 for (--i; i >= 0; i--)
1167 if (mlx5_cmd_free_uar(dev->mdev, bfregi->sys_pages[i]))
1168 mlx5_ib_warn(dev, "failed to free uar %d\n", i);
1173 static int deallocate_uars(struct mlx5_ib_dev *dev, struct mlx5_ib_ucontext *context)
1175 struct mlx5_bfreg_info *bfregi;
1179 bfregi = &context->bfregi;
1180 for (i = 0; i < bfregi->num_sys_pages; i++) {
1181 err = mlx5_cmd_free_uar(dev->mdev, bfregi->sys_pages[i]);
1183 mlx5_ib_warn(dev, "failed to free uar %d\n", i);
1190 static struct ib_ucontext *mlx5_ib_alloc_ucontext(struct ib_device *ibdev,
1191 struct ib_udata *udata)
1193 struct mlx5_ib_dev *dev = to_mdev(ibdev);
1194 struct mlx5_ib_alloc_ucontext_req_v2 req = {};
1195 struct mlx5_ib_alloc_ucontext_resp resp = {};
1196 struct mlx5_ib_ucontext *context;
1197 struct mlx5_bfreg_info *bfregi;
1201 size_t min_req_v2 = offsetof(struct mlx5_ib_alloc_ucontext_req_v2,
1205 if (!dev->ib_active)
1206 return ERR_PTR(-EAGAIN);
1208 if (udata->inlen < sizeof(struct ib_uverbs_cmd_hdr))
1209 return ERR_PTR(-EINVAL);
1211 reqlen = udata->inlen - sizeof(struct ib_uverbs_cmd_hdr);
1212 if (reqlen == sizeof(struct mlx5_ib_alloc_ucontext_req))
1214 else if (reqlen >= min_req_v2)
1217 return ERR_PTR(-EINVAL);
1219 err = ib_copy_from_udata(&req, udata, min(reqlen, sizeof(req)));
1221 return ERR_PTR(err);
1224 return ERR_PTR(-EINVAL);
1226 if (req.comp_mask || req.reserved0 || req.reserved1 || req.reserved2)
1227 return ERR_PTR(-EOPNOTSUPP);
1229 req.total_num_bfregs = ALIGN(req.total_num_bfregs,
1230 MLX5_NON_FP_BFREGS_PER_UAR);
1231 if (req.num_low_latency_bfregs > req.total_num_bfregs - 1)
1232 return ERR_PTR(-EINVAL);
1234 resp.qp_tab_size = 1 << MLX5_CAP_GEN(dev->mdev, log_max_qp);
1235 if (mlx5_core_is_pf(dev->mdev) && MLX5_CAP_GEN(dev->mdev, bf))
1236 resp.bf_reg_size = 1 << MLX5_CAP_GEN(dev->mdev, log_bf_reg_size);
1237 resp.cache_line_size = cache_line_size();
1238 resp.max_sq_desc_sz = MLX5_CAP_GEN(dev->mdev, max_wqe_sz_sq);
1239 resp.max_rq_desc_sz = MLX5_CAP_GEN(dev->mdev, max_wqe_sz_rq);
1240 resp.max_send_wqebb = 1 << MLX5_CAP_GEN(dev->mdev, log_max_qp_sz);
1241 resp.max_recv_wr = 1 << MLX5_CAP_GEN(dev->mdev, log_max_qp_sz);
1242 resp.max_srq_recv_wr = 1 << MLX5_CAP_GEN(dev->mdev, log_max_srq_sz);
1243 resp.cqe_version = min_t(__u8,
1244 (__u8)MLX5_CAP_GEN(dev->mdev, cqe_version),
1245 req.max_cqe_version);
1246 resp.log_uar_size = MLX5_CAP_GEN(dev->mdev, uar_4k) ?
1247 MLX5_ADAPTER_PAGE_SHIFT : PAGE_SHIFT;
1248 resp.num_uars_per_page = MLX5_CAP_GEN(dev->mdev, uar_4k) ?
1249 MLX5_CAP_GEN(dev->mdev, num_of_uars_per_page) : 1;
1250 resp.response_length = min(offsetof(typeof(resp), response_length) +
1251 sizeof(resp.response_length), udata->outlen);
1253 context = kzalloc(sizeof(*context), GFP_KERNEL);
1255 return ERR_PTR(-ENOMEM);
1257 lib_uar_4k = req.lib_caps & MLX5_LIB_CAP_4K_UAR;
1258 bfregi = &context->bfregi;
1260 /* updates req->total_num_bfregs */
1261 err = calc_total_bfregs(dev, lib_uar_4k, &req, &bfregi->num_sys_pages);
1265 mutex_init(&bfregi->lock);
1266 bfregi->lib_uar_4k = lib_uar_4k;
1267 bfregi->count = kcalloc(req.total_num_bfregs, sizeof(*bfregi->count),
1269 if (!bfregi->count) {
1274 bfregi->sys_pages = kcalloc(bfregi->num_sys_pages,
1275 sizeof(*bfregi->sys_pages),
1277 if (!bfregi->sys_pages) {
1282 err = allocate_uars(dev, context);
1286 #ifdef CONFIG_INFINIBAND_ON_DEMAND_PAGING
1287 context->ibucontext.invalidate_range = &mlx5_ib_invalidate_range;
1290 context->upd_xlt_page = __get_free_page(GFP_KERNEL);
1291 if (!context->upd_xlt_page) {
1295 mutex_init(&context->upd_xlt_page_mutex);
1297 if (MLX5_CAP_GEN(dev->mdev, log_max_transport_domain)) {
1298 err = mlx5_core_alloc_transport_domain(dev->mdev,
1304 INIT_LIST_HEAD(&context->vma_private_list);
1305 INIT_LIST_HEAD(&context->db_page_list);
1306 mutex_init(&context->db_page_mutex);
1308 resp.tot_bfregs = req.total_num_bfregs;
1309 resp.num_ports = MLX5_CAP_GEN(dev->mdev, num_ports);
1311 if (field_avail(typeof(resp), cqe_version, udata->outlen))
1312 resp.response_length += sizeof(resp.cqe_version);
1314 if (field_avail(typeof(resp), cmds_supp_uhw, udata->outlen)) {
1315 resp.cmds_supp_uhw |= MLX5_USER_CMDS_SUPP_UHW_QUERY_DEVICE |
1316 MLX5_USER_CMDS_SUPP_UHW_CREATE_AH;
1317 resp.response_length += sizeof(resp.cmds_supp_uhw);
1320 if (field_avail(typeof(resp), eth_min_inline, udata->outlen)) {
1321 if (mlx5_ib_port_link_layer(ibdev, 1) == IB_LINK_LAYER_ETHERNET) {
1322 mlx5_query_min_inline(dev->mdev, &resp.eth_min_inline);
1323 resp.eth_min_inline++;
1325 resp.response_length += sizeof(resp.eth_min_inline);
1329 * We don't want to expose information from the PCI bar that is located
1330 * after 4096 bytes, so if the arch only supports larger pages, let's
1331 * pretend we don't support reading the HCA's core clock. This is also
1332 * forced by mmap function.
1334 if (field_avail(typeof(resp), hca_core_clock_offset, udata->outlen)) {
1335 if (PAGE_SIZE <= 4096) {
1337 MLX5_IB_ALLOC_UCONTEXT_RESP_MASK_CORE_CLOCK_OFFSET;
1338 resp.hca_core_clock_offset =
1339 offsetof(struct mlx5_init_seg, internal_timer_h) % PAGE_SIZE;
1341 resp.response_length += sizeof(resp.hca_core_clock_offset) +
1342 sizeof(resp.reserved2);
1345 if (field_avail(typeof(resp), log_uar_size, udata->outlen))
1346 resp.response_length += sizeof(resp.log_uar_size);
1348 if (field_avail(typeof(resp), num_uars_per_page, udata->outlen))
1349 resp.response_length += sizeof(resp.num_uars_per_page);
1351 err = ib_copy_to_udata(udata, &resp, resp.response_length);
1356 bfregi->num_low_latency_bfregs = req.num_low_latency_bfregs;
1357 context->cqe_version = resp.cqe_version;
1358 context->lib_caps = req.lib_caps;
1359 print_lib_caps(dev, context->lib_caps);
1361 return &context->ibucontext;
1364 if (MLX5_CAP_GEN(dev->mdev, log_max_transport_domain))
1365 mlx5_core_dealloc_transport_domain(dev->mdev, context->tdn);
1368 free_page(context->upd_xlt_page);
1371 deallocate_uars(dev, context);
1374 kfree(bfregi->sys_pages);
1377 kfree(bfregi->count);
1382 return ERR_PTR(err);
1385 static int mlx5_ib_dealloc_ucontext(struct ib_ucontext *ibcontext)
1387 struct mlx5_ib_ucontext *context = to_mucontext(ibcontext);
1388 struct mlx5_ib_dev *dev = to_mdev(ibcontext->device);
1389 struct mlx5_bfreg_info *bfregi;
1391 bfregi = &context->bfregi;
1392 if (MLX5_CAP_GEN(dev->mdev, log_max_transport_domain))
1393 mlx5_core_dealloc_transport_domain(dev->mdev, context->tdn);
1395 free_page(context->upd_xlt_page);
1396 deallocate_uars(dev, context);
1397 kfree(bfregi->sys_pages);
1398 kfree(bfregi->count);
1404 static phys_addr_t uar_index2pfn(struct mlx5_ib_dev *dev,
1405 struct mlx5_bfreg_info *bfregi,
1408 int fw_uars_per_page;
1410 fw_uars_per_page = MLX5_CAP_GEN(dev->mdev, uar_4k) ? MLX5_UARS_IN_PAGE : 1;
1412 return (pci_resource_start(dev->mdev->pdev, 0) >> PAGE_SHIFT) +
1413 bfregi->sys_pages[idx] / fw_uars_per_page;
1416 static int get_command(unsigned long offset)
1418 return (offset >> MLX5_IB_MMAP_CMD_SHIFT) & MLX5_IB_MMAP_CMD_MASK;
1421 static int get_arg(unsigned long offset)
1423 return offset & ((1 << MLX5_IB_MMAP_CMD_SHIFT) - 1);
1426 static int get_index(unsigned long offset)
1428 return get_arg(offset);
1431 static void mlx5_ib_vma_open(struct vm_area_struct *area)
1433 /* vma_open is called when a new VMA is created on top of our VMA. This
1434 * is done through either mremap flow or split_vma (usually due to
1435 * mlock, madvise, munmap, etc.) We do not support a clone of the VMA,
1436 * as this VMA is strongly hardware related. Therefore we set the
1437 * vm_ops of the newly created/cloned VMA to NULL, to prevent it from
1438 * calling us again and trying to do incorrect actions. We assume that
1439 * the original VMA size is exactly a single page, and therefore all
1440 * "splitting" operation will not happen to it.
1442 area->vm_ops = NULL;
1445 static void mlx5_ib_vma_close(struct vm_area_struct *area)
1447 struct mlx5_ib_vma_private_data *mlx5_ib_vma_priv_data;
1449 /* It's guaranteed that all VMAs opened on a FD are closed before the
1450 * file itself is closed, therefore no sync is needed with the regular
1451 * closing flow. (e.g. mlx5 ib_dealloc_ucontext)
1452 * However need a sync with accessing the vma as part of
1453 * mlx5_ib_disassociate_ucontext.
1454 * The close operation is usually called under mm->mmap_sem except when
1455 * process is exiting.
1456 * The exiting case is handled explicitly as part of
1457 * mlx5_ib_disassociate_ucontext.
1459 mlx5_ib_vma_priv_data = (struct mlx5_ib_vma_private_data *)area->vm_private_data;
1461 /* setting the vma context pointer to null in the mlx5_ib driver's
1462 * private data, to protect a race condition in
1463 * mlx5_ib_disassociate_ucontext().
1465 mlx5_ib_vma_priv_data->vma = NULL;
1466 list_del(&mlx5_ib_vma_priv_data->list);
1467 kfree(mlx5_ib_vma_priv_data);
1470 static const struct vm_operations_struct mlx5_ib_vm_ops = {
1471 .open = mlx5_ib_vma_open,
1472 .close = mlx5_ib_vma_close
1475 static int mlx5_ib_set_vma_data(struct vm_area_struct *vma,
1476 struct mlx5_ib_ucontext *ctx)
1478 struct mlx5_ib_vma_private_data *vma_prv;
1479 struct list_head *vma_head = &ctx->vma_private_list;
1481 vma_prv = kzalloc(sizeof(*vma_prv), GFP_KERNEL);
1486 vma->vm_private_data = vma_prv;
1487 vma->vm_ops = &mlx5_ib_vm_ops;
1489 list_add(&vma_prv->list, vma_head);
1494 static void mlx5_ib_disassociate_ucontext(struct ib_ucontext *ibcontext)
1497 struct vm_area_struct *vma;
1498 struct mlx5_ib_vma_private_data *vma_private, *n;
1499 struct mlx5_ib_ucontext *context = to_mucontext(ibcontext);
1500 struct task_struct *owning_process = NULL;
1501 struct mm_struct *owning_mm = NULL;
1503 owning_process = get_pid_task(ibcontext->tgid, PIDTYPE_PID);
1504 if (!owning_process)
1507 owning_mm = get_task_mm(owning_process);
1509 pr_info("no mm, disassociate ucontext is pending task termination\n");
1511 put_task_struct(owning_process);
1512 usleep_range(1000, 2000);
1513 owning_process = get_pid_task(ibcontext->tgid,
1515 if (!owning_process ||
1516 owning_process->state == TASK_DEAD) {
1517 pr_info("disassociate ucontext done, task was terminated\n");
1518 /* in case task was dead need to release the
1522 put_task_struct(owning_process);
1528 /* need to protect from a race on closing the vma as part of
1529 * mlx5_ib_vma_close.
1531 down_write(&owning_mm->mmap_sem);
1532 list_for_each_entry_safe(vma_private, n, &context->vma_private_list,
1534 vma = vma_private->vma;
1535 ret = zap_vma_ptes(vma, vma->vm_start,
1537 WARN_ONCE(ret, "%s: zap_vma_ptes failed", __func__);
1538 /* context going to be destroyed, should
1539 * not access ops any more.
1541 vma->vm_flags &= ~(VM_SHARED | VM_MAYSHARE);
1543 list_del(&vma_private->list);
1546 up_write(&owning_mm->mmap_sem);
1548 put_task_struct(owning_process);
1551 static inline char *mmap_cmd2str(enum mlx5_ib_mmap_cmd cmd)
1554 case MLX5_IB_MMAP_WC_PAGE:
1556 case MLX5_IB_MMAP_REGULAR_PAGE:
1557 return "best effort WC";
1558 case MLX5_IB_MMAP_NC_PAGE:
1565 static int uar_mmap(struct mlx5_ib_dev *dev, enum mlx5_ib_mmap_cmd cmd,
1566 struct vm_area_struct *vma,
1567 struct mlx5_ib_ucontext *context)
1569 struct mlx5_bfreg_info *bfregi = &context->bfregi;
1572 phys_addr_t pfn, pa;
1576 if (vma->vm_end - vma->vm_start != PAGE_SIZE)
1579 uars_per_page = get_uars_per_sys_page(dev, bfregi->lib_uar_4k);
1580 idx = get_index(vma->vm_pgoff);
1581 if (idx % uars_per_page ||
1582 idx * uars_per_page >= bfregi->num_sys_pages) {
1583 mlx5_ib_warn(dev, "invalid uar index %lu\n", idx);
1588 case MLX5_IB_MMAP_WC_PAGE:
1589 /* Some architectures don't support WC memory */
1590 #if defined(CONFIG_X86)
1593 #elif !(defined(CONFIG_PPC) || (defined(CONFIG_ARM) && defined(CONFIG_MMU)))
1597 case MLX5_IB_MMAP_REGULAR_PAGE:
1598 /* For MLX5_IB_MMAP_REGULAR_PAGE do the best effort to get WC */
1599 prot = pgprot_writecombine(vma->vm_page_prot);
1601 case MLX5_IB_MMAP_NC_PAGE:
1602 prot = pgprot_noncached(vma->vm_page_prot);
1608 pfn = uar_index2pfn(dev, bfregi, idx);
1609 mlx5_ib_dbg(dev, "uar idx 0x%lx, pfn %pa\n", idx, &pfn);
1611 vma->vm_page_prot = prot;
1612 err = io_remap_pfn_range(vma, vma->vm_start, pfn,
1613 PAGE_SIZE, vma->vm_page_prot);
1615 mlx5_ib_err(dev, "io_remap_pfn_range failed with error=%d, vm_start=0x%lx, pfn=%pa, mmap_cmd=%s\n",
1616 err, vma->vm_start, &pfn, mmap_cmd2str(cmd));
1620 pa = pfn << PAGE_SHIFT;
1621 mlx5_ib_dbg(dev, "mapped %s at 0x%lx, PA %pa\n", mmap_cmd2str(cmd),
1622 vma->vm_start, &pa);
1624 return mlx5_ib_set_vma_data(vma, context);
1627 static int mlx5_ib_mmap(struct ib_ucontext *ibcontext, struct vm_area_struct *vma)
1629 struct mlx5_ib_ucontext *context = to_mucontext(ibcontext);
1630 struct mlx5_ib_dev *dev = to_mdev(ibcontext->device);
1631 unsigned long command;
1634 command = get_command(vma->vm_pgoff);
1636 case MLX5_IB_MMAP_WC_PAGE:
1637 case MLX5_IB_MMAP_NC_PAGE:
1638 case MLX5_IB_MMAP_REGULAR_PAGE:
1639 return uar_mmap(dev, command, vma, context);
1641 case MLX5_IB_MMAP_GET_CONTIGUOUS_PAGES:
1644 case MLX5_IB_MMAP_CORE_CLOCK:
1645 if (vma->vm_end - vma->vm_start != PAGE_SIZE)
1648 if (vma->vm_flags & VM_WRITE)
1651 /* Don't expose to user-space information it shouldn't have */
1652 if (PAGE_SIZE > 4096)
1655 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
1656 pfn = (dev->mdev->iseg_base +
1657 offsetof(struct mlx5_init_seg, internal_timer_h)) >>
1659 if (io_remap_pfn_range(vma, vma->vm_start, pfn,
1660 PAGE_SIZE, vma->vm_page_prot))
1663 mlx5_ib_dbg(dev, "mapped internal timer at 0x%lx, PA 0x%llx\n",
1665 (unsigned long long)pfn << PAGE_SHIFT);
1675 static struct ib_pd *mlx5_ib_alloc_pd(struct ib_device *ibdev,
1676 struct ib_ucontext *context,
1677 struct ib_udata *udata)
1679 struct mlx5_ib_alloc_pd_resp resp;
1680 struct mlx5_ib_pd *pd;
1683 pd = kmalloc(sizeof(*pd), GFP_KERNEL);
1685 return ERR_PTR(-ENOMEM);
1687 err = mlx5_core_alloc_pd(to_mdev(ibdev)->mdev, &pd->pdn);
1690 return ERR_PTR(err);
1695 if (ib_copy_to_udata(udata, &resp, sizeof(resp))) {
1696 mlx5_core_dealloc_pd(to_mdev(ibdev)->mdev, pd->pdn);
1698 return ERR_PTR(-EFAULT);
1705 static int mlx5_ib_dealloc_pd(struct ib_pd *pd)
1707 struct mlx5_ib_dev *mdev = to_mdev(pd->device);
1708 struct mlx5_ib_pd *mpd = to_mpd(pd);
1710 mlx5_core_dealloc_pd(mdev->mdev, mpd->pdn);
1717 MATCH_CRITERIA_ENABLE_OUTER_BIT,
1718 MATCH_CRITERIA_ENABLE_MISC_BIT,
1719 MATCH_CRITERIA_ENABLE_INNER_BIT
1722 #define HEADER_IS_ZERO(match_criteria, headers) \
1723 !(memchr_inv(MLX5_ADDR_OF(fte_match_param, match_criteria, headers), \
1724 0, MLX5_FLD_SZ_BYTES(fte_match_param, headers))) \
1726 static u8 get_match_criteria_enable(u32 *match_criteria)
1728 u8 match_criteria_enable;
1730 match_criteria_enable =
1731 (!HEADER_IS_ZERO(match_criteria, outer_headers)) <<
1732 MATCH_CRITERIA_ENABLE_OUTER_BIT;
1733 match_criteria_enable |=
1734 (!HEADER_IS_ZERO(match_criteria, misc_parameters)) <<
1735 MATCH_CRITERIA_ENABLE_MISC_BIT;
1736 match_criteria_enable |=
1737 (!HEADER_IS_ZERO(match_criteria, inner_headers)) <<
1738 MATCH_CRITERIA_ENABLE_INNER_BIT;
1740 return match_criteria_enable;
1743 static void set_proto(void *outer_c, void *outer_v, u8 mask, u8 val)
1745 MLX5_SET(fte_match_set_lyr_2_4, outer_c, ip_protocol, mask);
1746 MLX5_SET(fte_match_set_lyr_2_4, outer_v, ip_protocol, val);
1749 static void set_flow_label(void *misc_c, void *misc_v, u8 mask, u8 val,
1753 MLX5_SET(fte_match_set_misc,
1754 misc_c, inner_ipv6_flow_label, mask);
1755 MLX5_SET(fte_match_set_misc,
1756 misc_v, inner_ipv6_flow_label, val);
1758 MLX5_SET(fte_match_set_misc,
1759 misc_c, outer_ipv6_flow_label, mask);
1760 MLX5_SET(fte_match_set_misc,
1761 misc_v, outer_ipv6_flow_label, val);
1765 static void set_tos(void *outer_c, void *outer_v, u8 mask, u8 val)
1767 MLX5_SET(fte_match_set_lyr_2_4, outer_c, ip_ecn, mask);
1768 MLX5_SET(fte_match_set_lyr_2_4, outer_v, ip_ecn, val);
1769 MLX5_SET(fte_match_set_lyr_2_4, outer_c, ip_dscp, mask >> 2);
1770 MLX5_SET(fte_match_set_lyr_2_4, outer_v, ip_dscp, val >> 2);
1773 #define LAST_ETH_FIELD vlan_tag
1774 #define LAST_IB_FIELD sl
1775 #define LAST_IPV4_FIELD tos
1776 #define LAST_IPV6_FIELD traffic_class
1777 #define LAST_TCP_UDP_FIELD src_port
1778 #define LAST_TUNNEL_FIELD tunnel_id
1779 #define LAST_FLOW_TAG_FIELD tag_id
1780 #define LAST_DROP_FIELD size
1782 /* Field is the last supported field */
1783 #define FIELDS_NOT_SUPPORTED(filter, field)\
1784 memchr_inv((void *)&filter.field +\
1785 sizeof(filter.field), 0,\
1787 offsetof(typeof(filter), field) -\
1788 sizeof(filter.field))
1790 #define IPV4_VERSION 4
1791 #define IPV6_VERSION 6
1792 static int parse_flow_attr(struct mlx5_core_dev *mdev, u32 *match_c,
1793 u32 *match_v, const union ib_flow_spec *ib_spec,
1794 u32 *tag_id, bool *is_drop)
1796 void *misc_params_c = MLX5_ADDR_OF(fte_match_param, match_c,
1798 void *misc_params_v = MLX5_ADDR_OF(fte_match_param, match_v,
1804 if (ib_spec->type & IB_FLOW_SPEC_INNER) {
1805 headers_c = MLX5_ADDR_OF(fte_match_param, match_c,
1807 headers_v = MLX5_ADDR_OF(fte_match_param, match_v,
1809 match_ipv = MLX5_CAP_FLOWTABLE_NIC_RX(mdev,
1810 ft_field_support.inner_ip_version);
1812 headers_c = MLX5_ADDR_OF(fte_match_param, match_c,
1814 headers_v = MLX5_ADDR_OF(fte_match_param, match_v,
1816 match_ipv = MLX5_CAP_FLOWTABLE_NIC_RX(mdev,
1817 ft_field_support.outer_ip_version);
1820 switch (ib_spec->type & ~IB_FLOW_SPEC_INNER) {
1821 case IB_FLOW_SPEC_ETH:
1822 if (FIELDS_NOT_SUPPORTED(ib_spec->eth.mask, LAST_ETH_FIELD))
1825 ether_addr_copy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
1827 ib_spec->eth.mask.dst_mac);
1828 ether_addr_copy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
1830 ib_spec->eth.val.dst_mac);
1832 ether_addr_copy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
1834 ib_spec->eth.mask.src_mac);
1835 ether_addr_copy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
1837 ib_spec->eth.val.src_mac);
1839 if (ib_spec->eth.mask.vlan_tag) {
1840 MLX5_SET(fte_match_set_lyr_2_4, headers_c,
1842 MLX5_SET(fte_match_set_lyr_2_4, headers_v,
1845 MLX5_SET(fte_match_set_lyr_2_4, headers_c,
1846 first_vid, ntohs(ib_spec->eth.mask.vlan_tag));
1847 MLX5_SET(fte_match_set_lyr_2_4, headers_v,
1848 first_vid, ntohs(ib_spec->eth.val.vlan_tag));
1850 MLX5_SET(fte_match_set_lyr_2_4, headers_c,
1852 ntohs(ib_spec->eth.mask.vlan_tag) >> 12);
1853 MLX5_SET(fte_match_set_lyr_2_4, headers_v,
1855 ntohs(ib_spec->eth.val.vlan_tag) >> 12);
1857 MLX5_SET(fte_match_set_lyr_2_4, headers_c,
1859 ntohs(ib_spec->eth.mask.vlan_tag) >> 13);
1860 MLX5_SET(fte_match_set_lyr_2_4, headers_v,
1862 ntohs(ib_spec->eth.val.vlan_tag) >> 13);
1864 MLX5_SET(fte_match_set_lyr_2_4, headers_c,
1865 ethertype, ntohs(ib_spec->eth.mask.ether_type));
1866 MLX5_SET(fte_match_set_lyr_2_4, headers_v,
1867 ethertype, ntohs(ib_spec->eth.val.ether_type));
1869 case IB_FLOW_SPEC_IPV4:
1870 if (FIELDS_NOT_SUPPORTED(ib_spec->ipv4.mask, LAST_IPV4_FIELD))
1874 MLX5_SET(fte_match_set_lyr_2_4, headers_c,
1876 MLX5_SET(fte_match_set_lyr_2_4, headers_v,
1877 ip_version, IPV4_VERSION);
1879 MLX5_SET(fte_match_set_lyr_2_4, headers_c,
1881 MLX5_SET(fte_match_set_lyr_2_4, headers_v,
1882 ethertype, ETH_P_IP);
1885 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
1886 src_ipv4_src_ipv6.ipv4_layout.ipv4),
1887 &ib_spec->ipv4.mask.src_ip,
1888 sizeof(ib_spec->ipv4.mask.src_ip));
1889 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
1890 src_ipv4_src_ipv6.ipv4_layout.ipv4),
1891 &ib_spec->ipv4.val.src_ip,
1892 sizeof(ib_spec->ipv4.val.src_ip));
1893 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
1894 dst_ipv4_dst_ipv6.ipv4_layout.ipv4),
1895 &ib_spec->ipv4.mask.dst_ip,
1896 sizeof(ib_spec->ipv4.mask.dst_ip));
1897 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
1898 dst_ipv4_dst_ipv6.ipv4_layout.ipv4),
1899 &ib_spec->ipv4.val.dst_ip,
1900 sizeof(ib_spec->ipv4.val.dst_ip));
1902 set_tos(headers_c, headers_v,
1903 ib_spec->ipv4.mask.tos, ib_spec->ipv4.val.tos);
1905 set_proto(headers_c, headers_v,
1906 ib_spec->ipv4.mask.proto, ib_spec->ipv4.val.proto);
1908 case IB_FLOW_SPEC_IPV6:
1909 if (FIELDS_NOT_SUPPORTED(ib_spec->ipv6.mask, LAST_IPV6_FIELD))
1913 MLX5_SET(fte_match_set_lyr_2_4, headers_c,
1915 MLX5_SET(fte_match_set_lyr_2_4, headers_v,
1916 ip_version, IPV6_VERSION);
1918 MLX5_SET(fte_match_set_lyr_2_4, headers_c,
1920 MLX5_SET(fte_match_set_lyr_2_4, headers_v,
1921 ethertype, ETH_P_IPV6);
1924 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
1925 src_ipv4_src_ipv6.ipv6_layout.ipv6),
1926 &ib_spec->ipv6.mask.src_ip,
1927 sizeof(ib_spec->ipv6.mask.src_ip));
1928 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
1929 src_ipv4_src_ipv6.ipv6_layout.ipv6),
1930 &ib_spec->ipv6.val.src_ip,
1931 sizeof(ib_spec->ipv6.val.src_ip));
1932 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
1933 dst_ipv4_dst_ipv6.ipv6_layout.ipv6),
1934 &ib_spec->ipv6.mask.dst_ip,
1935 sizeof(ib_spec->ipv6.mask.dst_ip));
1936 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
1937 dst_ipv4_dst_ipv6.ipv6_layout.ipv6),
1938 &ib_spec->ipv6.val.dst_ip,
1939 sizeof(ib_spec->ipv6.val.dst_ip));
1941 set_tos(headers_c, headers_v,
1942 ib_spec->ipv6.mask.traffic_class,
1943 ib_spec->ipv6.val.traffic_class);
1945 set_proto(headers_c, headers_v,
1946 ib_spec->ipv6.mask.next_hdr,
1947 ib_spec->ipv6.val.next_hdr);
1949 set_flow_label(misc_params_c, misc_params_v,
1950 ntohl(ib_spec->ipv6.mask.flow_label),
1951 ntohl(ib_spec->ipv6.val.flow_label),
1952 ib_spec->type & IB_FLOW_SPEC_INNER);
1955 case IB_FLOW_SPEC_TCP:
1956 if (FIELDS_NOT_SUPPORTED(ib_spec->tcp_udp.mask,
1957 LAST_TCP_UDP_FIELD))
1960 MLX5_SET(fte_match_set_lyr_2_4, headers_c, ip_protocol,
1962 MLX5_SET(fte_match_set_lyr_2_4, headers_v, ip_protocol,
1965 MLX5_SET(fte_match_set_lyr_2_4, headers_c, tcp_sport,
1966 ntohs(ib_spec->tcp_udp.mask.src_port));
1967 MLX5_SET(fte_match_set_lyr_2_4, headers_v, tcp_sport,
1968 ntohs(ib_spec->tcp_udp.val.src_port));
1970 MLX5_SET(fte_match_set_lyr_2_4, headers_c, tcp_dport,
1971 ntohs(ib_spec->tcp_udp.mask.dst_port));
1972 MLX5_SET(fte_match_set_lyr_2_4, headers_v, tcp_dport,
1973 ntohs(ib_spec->tcp_udp.val.dst_port));
1975 case IB_FLOW_SPEC_UDP:
1976 if (FIELDS_NOT_SUPPORTED(ib_spec->tcp_udp.mask,
1977 LAST_TCP_UDP_FIELD))
1980 MLX5_SET(fte_match_set_lyr_2_4, headers_c, ip_protocol,
1982 MLX5_SET(fte_match_set_lyr_2_4, headers_v, ip_protocol,
1985 MLX5_SET(fte_match_set_lyr_2_4, headers_c, udp_sport,
1986 ntohs(ib_spec->tcp_udp.mask.src_port));
1987 MLX5_SET(fte_match_set_lyr_2_4, headers_v, udp_sport,
1988 ntohs(ib_spec->tcp_udp.val.src_port));
1990 MLX5_SET(fte_match_set_lyr_2_4, headers_c, udp_dport,
1991 ntohs(ib_spec->tcp_udp.mask.dst_port));
1992 MLX5_SET(fte_match_set_lyr_2_4, headers_v, udp_dport,
1993 ntohs(ib_spec->tcp_udp.val.dst_port));
1995 case IB_FLOW_SPEC_VXLAN_TUNNEL:
1996 if (FIELDS_NOT_SUPPORTED(ib_spec->tunnel.mask,
2000 MLX5_SET(fte_match_set_misc, misc_params_c, vxlan_vni,
2001 ntohl(ib_spec->tunnel.mask.tunnel_id));
2002 MLX5_SET(fte_match_set_misc, misc_params_v, vxlan_vni,
2003 ntohl(ib_spec->tunnel.val.tunnel_id));
2005 case IB_FLOW_SPEC_ACTION_TAG:
2006 if (FIELDS_NOT_SUPPORTED(ib_spec->flow_tag,
2007 LAST_FLOW_TAG_FIELD))
2009 if (ib_spec->flow_tag.tag_id >= BIT(24))
2012 *tag_id = ib_spec->flow_tag.tag_id;
2014 case IB_FLOW_SPEC_ACTION_DROP:
2015 if (FIELDS_NOT_SUPPORTED(ib_spec->drop,
2027 /* If a flow could catch both multicast and unicast packets,
2028 * it won't fall into the multicast flow steering table and this rule
2029 * could steal other multicast packets.
2031 static bool flow_is_multicast_only(struct ib_flow_attr *ib_attr)
2033 struct ib_flow_spec_eth *eth_spec;
2035 if (ib_attr->type != IB_FLOW_ATTR_NORMAL ||
2036 ib_attr->size < sizeof(struct ib_flow_attr) +
2037 sizeof(struct ib_flow_spec_eth) ||
2038 ib_attr->num_of_specs < 1)
2041 eth_spec = (struct ib_flow_spec_eth *)(ib_attr + 1);
2042 if (eth_spec->type != IB_FLOW_SPEC_ETH ||
2043 eth_spec->size != sizeof(*eth_spec))
2046 return is_multicast_ether_addr(eth_spec->mask.dst_mac) &&
2047 is_multicast_ether_addr(eth_spec->val.dst_mac);
2050 static bool is_valid_ethertype(struct mlx5_core_dev *mdev,
2051 const struct ib_flow_attr *flow_attr,
2054 union ib_flow_spec *ib_spec = (union ib_flow_spec *)(flow_attr + 1);
2055 int match_ipv = check_inner ?
2056 MLX5_CAP_FLOWTABLE_NIC_RX(mdev,
2057 ft_field_support.inner_ip_version) :
2058 MLX5_CAP_FLOWTABLE_NIC_RX(mdev,
2059 ft_field_support.outer_ip_version);
2060 int inner_bit = check_inner ? IB_FLOW_SPEC_INNER : 0;
2061 bool ipv4_spec_valid, ipv6_spec_valid;
2062 unsigned int ip_spec_type = 0;
2063 bool has_ethertype = false;
2064 unsigned int spec_index;
2065 bool mask_valid = true;
2069 /* Validate that ethertype is correct */
2070 for (spec_index = 0; spec_index < flow_attr->num_of_specs; spec_index++) {
2071 if ((ib_spec->type == (IB_FLOW_SPEC_ETH | inner_bit)) &&
2072 ib_spec->eth.mask.ether_type) {
2073 mask_valid = (ib_spec->eth.mask.ether_type ==
2075 has_ethertype = true;
2076 eth_type = ntohs(ib_spec->eth.val.ether_type);
2077 } else if ((ib_spec->type == (IB_FLOW_SPEC_IPV4 | inner_bit)) ||
2078 (ib_spec->type == (IB_FLOW_SPEC_IPV6 | inner_bit))) {
2079 ip_spec_type = ib_spec->type;
2081 ib_spec = (void *)ib_spec + ib_spec->size;
2084 type_valid = (!has_ethertype) || (!ip_spec_type);
2085 if (!type_valid && mask_valid) {
2086 ipv4_spec_valid = (eth_type == ETH_P_IP) &&
2087 (ip_spec_type == (IB_FLOW_SPEC_IPV4 | inner_bit));
2088 ipv6_spec_valid = (eth_type == ETH_P_IPV6) &&
2089 (ip_spec_type == (IB_FLOW_SPEC_IPV6 | inner_bit));
2091 type_valid = (ipv4_spec_valid) || (ipv6_spec_valid) ||
2092 (((eth_type == ETH_P_MPLS_UC) ||
2093 (eth_type == ETH_P_MPLS_MC)) && match_ipv);
2099 static bool is_valid_attr(struct mlx5_core_dev *mdev,
2100 const struct ib_flow_attr *flow_attr)
2102 return is_valid_ethertype(mdev, flow_attr, false) &&
2103 is_valid_ethertype(mdev, flow_attr, true);
2106 static void put_flow_table(struct mlx5_ib_dev *dev,
2107 struct mlx5_ib_flow_prio *prio, bool ft_added)
2109 prio->refcount -= !!ft_added;
2110 if (!prio->refcount) {
2111 mlx5_destroy_flow_table(prio->flow_table);
2112 prio->flow_table = NULL;
2116 static int mlx5_ib_destroy_flow(struct ib_flow *flow_id)
2118 struct mlx5_ib_dev *dev = to_mdev(flow_id->qp->device);
2119 struct mlx5_ib_flow_handler *handler = container_of(flow_id,
2120 struct mlx5_ib_flow_handler,
2122 struct mlx5_ib_flow_handler *iter, *tmp;
2124 mutex_lock(&dev->flow_db.lock);
2126 list_for_each_entry_safe(iter, tmp, &handler->list, list) {
2127 mlx5_del_flow_rules(iter->rule);
2128 put_flow_table(dev, iter->prio, true);
2129 list_del(&iter->list);
2133 mlx5_del_flow_rules(handler->rule);
2134 put_flow_table(dev, handler->prio, true);
2135 mutex_unlock(&dev->flow_db.lock);
2142 static int ib_prio_to_core_prio(unsigned int priority, bool dont_trap)
2150 enum flow_table_type {
2155 #define MLX5_FS_MAX_TYPES 6
2156 #define MLX5_FS_MAX_ENTRIES BIT(16)
2157 static struct mlx5_ib_flow_prio *get_flow_table(struct mlx5_ib_dev *dev,
2158 struct ib_flow_attr *flow_attr,
2159 enum flow_table_type ft_type)
2161 bool dont_trap = flow_attr->flags & IB_FLOW_ATTR_FLAGS_DONT_TRAP;
2162 struct mlx5_flow_namespace *ns = NULL;
2163 struct mlx5_ib_flow_prio *prio;
2164 struct mlx5_flow_table *ft;
2171 max_table_size = BIT(MLX5_CAP_FLOWTABLE_NIC_RX(dev->mdev,
2173 if (flow_attr->type == IB_FLOW_ATTR_NORMAL) {
2174 if (flow_is_multicast_only(flow_attr) &&
2176 priority = MLX5_IB_FLOW_MCAST_PRIO;
2178 priority = ib_prio_to_core_prio(flow_attr->priority,
2180 ns = mlx5_get_flow_namespace(dev->mdev,
2181 MLX5_FLOW_NAMESPACE_BYPASS);
2182 num_entries = MLX5_FS_MAX_ENTRIES;
2183 num_groups = MLX5_FS_MAX_TYPES;
2184 prio = &dev->flow_db.prios[priority];
2185 } else if (flow_attr->type == IB_FLOW_ATTR_ALL_DEFAULT ||
2186 flow_attr->type == IB_FLOW_ATTR_MC_DEFAULT) {
2187 ns = mlx5_get_flow_namespace(dev->mdev,
2188 MLX5_FLOW_NAMESPACE_LEFTOVERS);
2189 build_leftovers_ft_param(&priority,
2192 prio = &dev->flow_db.prios[MLX5_IB_FLOW_LEFTOVERS_PRIO];
2193 } else if (flow_attr->type == IB_FLOW_ATTR_SNIFFER) {
2194 if (!MLX5_CAP_FLOWTABLE(dev->mdev,
2195 allow_sniffer_and_nic_rx_shared_tir))
2196 return ERR_PTR(-ENOTSUPP);
2198 ns = mlx5_get_flow_namespace(dev->mdev, ft_type == MLX5_IB_FT_RX ?
2199 MLX5_FLOW_NAMESPACE_SNIFFER_RX :
2200 MLX5_FLOW_NAMESPACE_SNIFFER_TX);
2202 prio = &dev->flow_db.sniffer[ft_type];
2209 return ERR_PTR(-ENOTSUPP);
2211 if (num_entries > max_table_size)
2212 return ERR_PTR(-ENOMEM);
2214 ft = prio->flow_table;
2216 ft = mlx5_create_auto_grouped_flow_table(ns, priority,
2223 prio->flow_table = ft;
2229 return err ? ERR_PTR(err) : prio;
2232 static struct mlx5_ib_flow_handler *create_flow_rule(struct mlx5_ib_dev *dev,
2233 struct mlx5_ib_flow_prio *ft_prio,
2234 const struct ib_flow_attr *flow_attr,
2235 struct mlx5_flow_destination *dst)
2237 struct mlx5_flow_table *ft = ft_prio->flow_table;
2238 struct mlx5_ib_flow_handler *handler;
2239 struct mlx5_flow_act flow_act = {0};
2240 struct mlx5_flow_spec *spec;
2241 struct mlx5_flow_destination *rule_dst = dst;
2242 const void *ib_flow = (const void *)flow_attr + sizeof(*flow_attr);
2243 unsigned int spec_index;
2244 u32 flow_tag = MLX5_FS_DEFAULT_FLOW_TAG;
2245 bool is_drop = false;
2249 if (!is_valid_attr(dev->mdev, flow_attr))
2250 return ERR_PTR(-EINVAL);
2252 spec = kvzalloc(sizeof(*spec), GFP_KERNEL);
2253 handler = kzalloc(sizeof(*handler), GFP_KERNEL);
2254 if (!handler || !spec) {
2259 INIT_LIST_HEAD(&handler->list);
2261 for (spec_index = 0; spec_index < flow_attr->num_of_specs; spec_index++) {
2262 err = parse_flow_attr(dev->mdev, spec->match_criteria,
2264 ib_flow, &flow_tag, &is_drop);
2268 ib_flow += ((union ib_flow_spec *)ib_flow)->size;
2271 spec->match_criteria_enable = get_match_criteria_enable(spec->match_criteria);
2273 flow_act.action = MLX5_FLOW_CONTEXT_ACTION_DROP;
2277 flow_act.action = dst ? MLX5_FLOW_CONTEXT_ACTION_FWD_DEST :
2278 MLX5_FLOW_CONTEXT_ACTION_FWD_NEXT_PRIO;
2281 if (flow_tag != MLX5_FS_DEFAULT_FLOW_TAG &&
2282 (flow_attr->type == IB_FLOW_ATTR_ALL_DEFAULT ||
2283 flow_attr->type == IB_FLOW_ATTR_MC_DEFAULT)) {
2284 mlx5_ib_warn(dev, "Flow tag %u and attribute type %x isn't allowed in leftovers\n",
2285 flow_tag, flow_attr->type);
2289 flow_act.flow_tag = flow_tag;
2290 handler->rule = mlx5_add_flow_rules(ft, spec,
2292 rule_dst, dest_num);
2294 if (IS_ERR(handler->rule)) {
2295 err = PTR_ERR(handler->rule);
2299 ft_prio->refcount++;
2300 handler->prio = ft_prio;
2302 ft_prio->flow_table = ft;
2307 return err ? ERR_PTR(err) : handler;
2310 static struct mlx5_ib_flow_handler *create_dont_trap_rule(struct mlx5_ib_dev *dev,
2311 struct mlx5_ib_flow_prio *ft_prio,
2312 struct ib_flow_attr *flow_attr,
2313 struct mlx5_flow_destination *dst)
2315 struct mlx5_ib_flow_handler *handler_dst = NULL;
2316 struct mlx5_ib_flow_handler *handler = NULL;
2318 handler = create_flow_rule(dev, ft_prio, flow_attr, NULL);
2319 if (!IS_ERR(handler)) {
2320 handler_dst = create_flow_rule(dev, ft_prio,
2322 if (IS_ERR(handler_dst)) {
2323 mlx5_del_flow_rules(handler->rule);
2324 ft_prio->refcount--;
2326 handler = handler_dst;
2328 list_add(&handler_dst->list, &handler->list);
2339 static struct mlx5_ib_flow_handler *create_leftovers_rule(struct mlx5_ib_dev *dev,
2340 struct mlx5_ib_flow_prio *ft_prio,
2341 struct ib_flow_attr *flow_attr,
2342 struct mlx5_flow_destination *dst)
2344 struct mlx5_ib_flow_handler *handler_ucast = NULL;
2345 struct mlx5_ib_flow_handler *handler = NULL;
2348 struct ib_flow_attr flow_attr;
2349 struct ib_flow_spec_eth eth_flow;
2350 } leftovers_specs[] = {
2354 .size = sizeof(leftovers_specs[0])
2357 .type = IB_FLOW_SPEC_ETH,
2358 .size = sizeof(struct ib_flow_spec_eth),
2359 .mask = {.dst_mac = {0x1} },
2360 .val = {.dst_mac = {0x1} }
2366 .size = sizeof(leftovers_specs[0])
2369 .type = IB_FLOW_SPEC_ETH,
2370 .size = sizeof(struct ib_flow_spec_eth),
2371 .mask = {.dst_mac = {0x1} },
2372 .val = {.dst_mac = {} }
2377 handler = create_flow_rule(dev, ft_prio,
2378 &leftovers_specs[LEFTOVERS_MC].flow_attr,
2380 if (!IS_ERR(handler) &&
2381 flow_attr->type == IB_FLOW_ATTR_ALL_DEFAULT) {
2382 handler_ucast = create_flow_rule(dev, ft_prio,
2383 &leftovers_specs[LEFTOVERS_UC].flow_attr,
2385 if (IS_ERR(handler_ucast)) {
2386 mlx5_del_flow_rules(handler->rule);
2387 ft_prio->refcount--;
2389 handler = handler_ucast;
2391 list_add(&handler_ucast->list, &handler->list);
2398 static struct mlx5_ib_flow_handler *create_sniffer_rule(struct mlx5_ib_dev *dev,
2399 struct mlx5_ib_flow_prio *ft_rx,
2400 struct mlx5_ib_flow_prio *ft_tx,
2401 struct mlx5_flow_destination *dst)
2403 struct mlx5_ib_flow_handler *handler_rx;
2404 struct mlx5_ib_flow_handler *handler_tx;
2406 static const struct ib_flow_attr flow_attr = {
2408 .size = sizeof(flow_attr)
2411 handler_rx = create_flow_rule(dev, ft_rx, &flow_attr, dst);
2412 if (IS_ERR(handler_rx)) {
2413 err = PTR_ERR(handler_rx);
2417 handler_tx = create_flow_rule(dev, ft_tx, &flow_attr, dst);
2418 if (IS_ERR(handler_tx)) {
2419 err = PTR_ERR(handler_tx);
2423 list_add(&handler_tx->list, &handler_rx->list);
2428 mlx5_del_flow_rules(handler_rx->rule);
2432 return ERR_PTR(err);
2435 static struct ib_flow *mlx5_ib_create_flow(struct ib_qp *qp,
2436 struct ib_flow_attr *flow_attr,
2439 struct mlx5_ib_dev *dev = to_mdev(qp->device);
2440 struct mlx5_ib_qp *mqp = to_mqp(qp);
2441 struct mlx5_ib_flow_handler *handler = NULL;
2442 struct mlx5_flow_destination *dst = NULL;
2443 struct mlx5_ib_flow_prio *ft_prio_tx = NULL;
2444 struct mlx5_ib_flow_prio *ft_prio;
2447 if (flow_attr->priority > MLX5_IB_FLOW_LAST_PRIO)
2448 return ERR_PTR(-ENOMEM);
2450 if (domain != IB_FLOW_DOMAIN_USER ||
2451 flow_attr->port > MLX5_CAP_GEN(dev->mdev, num_ports) ||
2452 (flow_attr->flags & ~IB_FLOW_ATTR_FLAGS_DONT_TRAP))
2453 return ERR_PTR(-EINVAL);
2455 dst = kzalloc(sizeof(*dst), GFP_KERNEL);
2457 return ERR_PTR(-ENOMEM);
2459 mutex_lock(&dev->flow_db.lock);
2461 ft_prio = get_flow_table(dev, flow_attr, MLX5_IB_FT_RX);
2462 if (IS_ERR(ft_prio)) {
2463 err = PTR_ERR(ft_prio);
2466 if (flow_attr->type == IB_FLOW_ATTR_SNIFFER) {
2467 ft_prio_tx = get_flow_table(dev, flow_attr, MLX5_IB_FT_TX);
2468 if (IS_ERR(ft_prio_tx)) {
2469 err = PTR_ERR(ft_prio_tx);
2475 dst->type = MLX5_FLOW_DESTINATION_TYPE_TIR;
2476 if (mqp->flags & MLX5_IB_QP_RSS)
2477 dst->tir_num = mqp->rss_qp.tirn;
2479 dst->tir_num = mqp->raw_packet_qp.rq.tirn;
2481 if (flow_attr->type == IB_FLOW_ATTR_NORMAL) {
2482 if (flow_attr->flags & IB_FLOW_ATTR_FLAGS_DONT_TRAP) {
2483 handler = create_dont_trap_rule(dev, ft_prio,
2486 handler = create_flow_rule(dev, ft_prio, flow_attr,
2489 } else if (flow_attr->type == IB_FLOW_ATTR_ALL_DEFAULT ||
2490 flow_attr->type == IB_FLOW_ATTR_MC_DEFAULT) {
2491 handler = create_leftovers_rule(dev, ft_prio, flow_attr,
2493 } else if (flow_attr->type == IB_FLOW_ATTR_SNIFFER) {
2494 handler = create_sniffer_rule(dev, ft_prio, ft_prio_tx, dst);
2500 if (IS_ERR(handler)) {
2501 err = PTR_ERR(handler);
2506 mutex_unlock(&dev->flow_db.lock);
2509 return &handler->ibflow;
2512 put_flow_table(dev, ft_prio, false);
2514 put_flow_table(dev, ft_prio_tx, false);
2516 mutex_unlock(&dev->flow_db.lock);
2519 return ERR_PTR(err);
2522 static int mlx5_ib_mcg_attach(struct ib_qp *ibqp, union ib_gid *gid, u16 lid)
2524 struct mlx5_ib_dev *dev = to_mdev(ibqp->device);
2527 err = mlx5_core_attach_mcg(dev->mdev, gid, ibqp->qp_num);
2529 mlx5_ib_warn(dev, "failed attaching QPN 0x%x, MGID %pI6\n",
2530 ibqp->qp_num, gid->raw);
2535 static int mlx5_ib_mcg_detach(struct ib_qp *ibqp, union ib_gid *gid, u16 lid)
2537 struct mlx5_ib_dev *dev = to_mdev(ibqp->device);
2540 err = mlx5_core_detach_mcg(dev->mdev, gid, ibqp->qp_num);
2542 mlx5_ib_warn(dev, "failed detaching QPN 0x%x, MGID %pI6\n",
2543 ibqp->qp_num, gid->raw);
2548 static int init_node_data(struct mlx5_ib_dev *dev)
2552 err = mlx5_query_node_desc(dev, dev->ib_dev.node_desc);
2556 dev->mdev->rev_id = dev->mdev->pdev->revision;
2558 return mlx5_query_node_guid(dev, &dev->ib_dev.node_guid);
2561 static ssize_t show_fw_pages(struct device *device, struct device_attribute *attr,
2564 struct mlx5_ib_dev *dev =
2565 container_of(device, struct mlx5_ib_dev, ib_dev.dev);
2567 return sprintf(buf, "%d\n", dev->mdev->priv.fw_pages);
2570 static ssize_t show_reg_pages(struct device *device,
2571 struct device_attribute *attr, char *buf)
2573 struct mlx5_ib_dev *dev =
2574 container_of(device, struct mlx5_ib_dev, ib_dev.dev);
2576 return sprintf(buf, "%d\n", atomic_read(&dev->mdev->priv.reg_pages));
2579 static ssize_t show_hca(struct device *device, struct device_attribute *attr,
2582 struct mlx5_ib_dev *dev =
2583 container_of(device, struct mlx5_ib_dev, ib_dev.dev);
2584 return sprintf(buf, "MT%d\n", dev->mdev->pdev->device);
2587 static ssize_t show_rev(struct device *device, struct device_attribute *attr,
2590 struct mlx5_ib_dev *dev =
2591 container_of(device, struct mlx5_ib_dev, ib_dev.dev);
2592 return sprintf(buf, "%x\n", dev->mdev->rev_id);
2595 static ssize_t show_board(struct device *device, struct device_attribute *attr,
2598 struct mlx5_ib_dev *dev =
2599 container_of(device, struct mlx5_ib_dev, ib_dev.dev);
2600 return sprintf(buf, "%.*s\n", MLX5_BOARD_ID_LEN,
2601 dev->mdev->board_id);
2604 static DEVICE_ATTR(hw_rev, S_IRUGO, show_rev, NULL);
2605 static DEVICE_ATTR(hca_type, S_IRUGO, show_hca, NULL);
2606 static DEVICE_ATTR(board_id, S_IRUGO, show_board, NULL);
2607 static DEVICE_ATTR(fw_pages, S_IRUGO, show_fw_pages, NULL);
2608 static DEVICE_ATTR(reg_pages, S_IRUGO, show_reg_pages, NULL);
2610 static struct device_attribute *mlx5_class_attributes[] = {
2615 &dev_attr_reg_pages,
2618 static void pkey_change_handler(struct work_struct *work)
2620 struct mlx5_ib_port_resources *ports =
2621 container_of(work, struct mlx5_ib_port_resources,
2624 mutex_lock(&ports->devr->mutex);
2625 mlx5_ib_gsi_pkey_change(ports->gsi);
2626 mutex_unlock(&ports->devr->mutex);
2629 static void mlx5_ib_handle_internal_error(struct mlx5_ib_dev *ibdev)
2631 struct mlx5_ib_qp *mqp;
2632 struct mlx5_ib_cq *send_mcq, *recv_mcq;
2633 struct mlx5_core_cq *mcq;
2634 struct list_head cq_armed_list;
2635 unsigned long flags_qp;
2636 unsigned long flags_cq;
2637 unsigned long flags;
2639 INIT_LIST_HEAD(&cq_armed_list);
2641 /* Go over qp list reside on that ibdev, sync with create/destroy qp.*/
2642 spin_lock_irqsave(&ibdev->reset_flow_resource_lock, flags);
2643 list_for_each_entry(mqp, &ibdev->qp_list, qps_list) {
2644 spin_lock_irqsave(&mqp->sq.lock, flags_qp);
2645 if (mqp->sq.tail != mqp->sq.head) {
2646 send_mcq = to_mcq(mqp->ibqp.send_cq);
2647 spin_lock_irqsave(&send_mcq->lock, flags_cq);
2648 if (send_mcq->mcq.comp &&
2649 mqp->ibqp.send_cq->comp_handler) {
2650 if (!send_mcq->mcq.reset_notify_added) {
2651 send_mcq->mcq.reset_notify_added = 1;
2652 list_add_tail(&send_mcq->mcq.reset_notify,
2656 spin_unlock_irqrestore(&send_mcq->lock, flags_cq);
2658 spin_unlock_irqrestore(&mqp->sq.lock, flags_qp);
2659 spin_lock_irqsave(&mqp->rq.lock, flags_qp);
2660 /* no handling is needed for SRQ */
2661 if (!mqp->ibqp.srq) {
2662 if (mqp->rq.tail != mqp->rq.head) {
2663 recv_mcq = to_mcq(mqp->ibqp.recv_cq);
2664 spin_lock_irqsave(&recv_mcq->lock, flags_cq);
2665 if (recv_mcq->mcq.comp &&
2666 mqp->ibqp.recv_cq->comp_handler) {
2667 if (!recv_mcq->mcq.reset_notify_added) {
2668 recv_mcq->mcq.reset_notify_added = 1;
2669 list_add_tail(&recv_mcq->mcq.reset_notify,
2673 spin_unlock_irqrestore(&recv_mcq->lock,
2677 spin_unlock_irqrestore(&mqp->rq.lock, flags_qp);
2679 /*At that point all inflight post send were put to be executed as of we
2680 * lock/unlock above locks Now need to arm all involved CQs.
2682 list_for_each_entry(mcq, &cq_armed_list, reset_notify) {
2685 spin_unlock_irqrestore(&ibdev->reset_flow_resource_lock, flags);
2688 static void mlx5_ib_event(struct mlx5_core_dev *dev, void *context,
2689 enum mlx5_dev_event event, unsigned long param)
2691 struct mlx5_ib_dev *ibdev = (struct mlx5_ib_dev *)context;
2692 struct ib_event ibev;
2697 case MLX5_DEV_EVENT_SYS_ERROR:
2698 ibev.event = IB_EVENT_DEVICE_FATAL;
2699 mlx5_ib_handle_internal_error(ibdev);
2703 case MLX5_DEV_EVENT_PORT_UP:
2704 case MLX5_DEV_EVENT_PORT_DOWN:
2705 case MLX5_DEV_EVENT_PORT_INITIALIZED:
2708 /* In RoCE, port up/down events are handled in
2709 * mlx5_netdev_event().
2711 if (mlx5_ib_port_link_layer(&ibdev->ib_dev, port) ==
2712 IB_LINK_LAYER_ETHERNET)
2715 ibev.event = (event == MLX5_DEV_EVENT_PORT_UP) ?
2716 IB_EVENT_PORT_ACTIVE : IB_EVENT_PORT_ERR;
2719 case MLX5_DEV_EVENT_LID_CHANGE:
2720 ibev.event = IB_EVENT_LID_CHANGE;
2724 case MLX5_DEV_EVENT_PKEY_CHANGE:
2725 ibev.event = IB_EVENT_PKEY_CHANGE;
2728 schedule_work(&ibdev->devr.ports[port - 1].pkey_change_work);
2731 case MLX5_DEV_EVENT_GUID_CHANGE:
2732 ibev.event = IB_EVENT_GID_CHANGE;
2736 case MLX5_DEV_EVENT_CLIENT_REREG:
2737 ibev.event = IB_EVENT_CLIENT_REREGISTER;
2744 ibev.device = &ibdev->ib_dev;
2745 ibev.element.port_num = port;
2747 if (port < 1 || port > ibdev->num_ports) {
2748 mlx5_ib_warn(ibdev, "warning: event on port %d\n", port);
2752 if (ibdev->ib_active)
2753 ib_dispatch_event(&ibev);
2756 ibdev->ib_active = false;
2759 static int set_has_smi_cap(struct mlx5_ib_dev *dev)
2761 struct mlx5_hca_vport_context vport_ctx;
2765 for (port = 1; port <= MLX5_CAP_GEN(dev->mdev, num_ports); port++) {
2766 dev->mdev->port_caps[port - 1].has_smi = false;
2767 if (MLX5_CAP_GEN(dev->mdev, port_type) ==
2768 MLX5_CAP_PORT_TYPE_IB) {
2769 if (MLX5_CAP_GEN(dev->mdev, ib_virt)) {
2770 err = mlx5_query_hca_vport_context(dev->mdev, 0,
2774 mlx5_ib_err(dev, "query_hca_vport_context for port=%d failed %d\n",
2778 dev->mdev->port_caps[port - 1].has_smi =
2781 dev->mdev->port_caps[port - 1].has_smi = true;
2788 static void get_ext_port_caps(struct mlx5_ib_dev *dev)
2792 for (port = 1; port <= MLX5_CAP_GEN(dev->mdev, num_ports); port++)
2793 mlx5_query_ext_port_caps(dev, port);
2796 static int get_port_caps(struct mlx5_ib_dev *dev)
2798 struct ib_device_attr *dprops = NULL;
2799 struct ib_port_attr *pprops = NULL;
2802 struct ib_udata uhw = {.inlen = 0, .outlen = 0};
2804 pprops = kmalloc(sizeof(*pprops), GFP_KERNEL);
2808 dprops = kmalloc(sizeof(*dprops), GFP_KERNEL);
2812 err = set_has_smi_cap(dev);
2816 err = mlx5_ib_query_device(&dev->ib_dev, dprops, &uhw);
2818 mlx5_ib_warn(dev, "query_device failed %d\n", err);
2822 for (port = 1; port <= MLX5_CAP_GEN(dev->mdev, num_ports); port++) {
2823 memset(pprops, 0, sizeof(*pprops));
2824 err = mlx5_ib_query_port(&dev->ib_dev, port, pprops);
2826 mlx5_ib_warn(dev, "query_port %d failed %d\n",
2830 dev->mdev->port_caps[port - 1].pkey_table_len =
2832 dev->mdev->port_caps[port - 1].gid_table_len =
2833 pprops->gid_tbl_len;
2834 mlx5_ib_dbg(dev, "pkey_table_len %d, gid_table_len %d\n",
2835 dprops->max_pkeys, pprops->gid_tbl_len);
2845 static void destroy_umrc_res(struct mlx5_ib_dev *dev)
2849 err = mlx5_mr_cache_cleanup(dev);
2851 mlx5_ib_warn(dev, "mr cache cleanup failed\n");
2853 mlx5_ib_destroy_qp(dev->umrc.qp);
2854 ib_free_cq(dev->umrc.cq);
2855 ib_dealloc_pd(dev->umrc.pd);
2862 static int create_umr_res(struct mlx5_ib_dev *dev)
2864 struct ib_qp_init_attr *init_attr = NULL;
2865 struct ib_qp_attr *attr = NULL;
2871 attr = kzalloc(sizeof(*attr), GFP_KERNEL);
2872 init_attr = kzalloc(sizeof(*init_attr), GFP_KERNEL);
2873 if (!attr || !init_attr) {
2878 pd = ib_alloc_pd(&dev->ib_dev, 0);
2880 mlx5_ib_dbg(dev, "Couldn't create PD for sync UMR QP\n");
2885 cq = ib_alloc_cq(&dev->ib_dev, NULL, 128, 0, IB_POLL_SOFTIRQ);
2887 mlx5_ib_dbg(dev, "Couldn't create CQ for sync UMR QP\n");
2892 init_attr->send_cq = cq;
2893 init_attr->recv_cq = cq;
2894 init_attr->sq_sig_type = IB_SIGNAL_ALL_WR;
2895 init_attr->cap.max_send_wr = MAX_UMR_WR;
2896 init_attr->cap.max_send_sge = 1;
2897 init_attr->qp_type = MLX5_IB_QPT_REG_UMR;
2898 init_attr->port_num = 1;
2899 qp = mlx5_ib_create_qp(pd, init_attr, NULL);
2901 mlx5_ib_dbg(dev, "Couldn't create sync UMR QP\n");
2905 qp->device = &dev->ib_dev;
2908 qp->qp_type = MLX5_IB_QPT_REG_UMR;
2910 attr->qp_state = IB_QPS_INIT;
2912 ret = mlx5_ib_modify_qp(qp, attr, IB_QP_STATE | IB_QP_PKEY_INDEX |
2915 mlx5_ib_dbg(dev, "Couldn't modify UMR QP\n");
2919 memset(attr, 0, sizeof(*attr));
2920 attr->qp_state = IB_QPS_RTR;
2921 attr->path_mtu = IB_MTU_256;
2923 ret = mlx5_ib_modify_qp(qp, attr, IB_QP_STATE, NULL);
2925 mlx5_ib_dbg(dev, "Couldn't modify umr QP to rtr\n");
2929 memset(attr, 0, sizeof(*attr));
2930 attr->qp_state = IB_QPS_RTS;
2931 ret = mlx5_ib_modify_qp(qp, attr, IB_QP_STATE, NULL);
2933 mlx5_ib_dbg(dev, "Couldn't modify umr QP to rts\n");
2941 sema_init(&dev->umrc.sem, MAX_UMR_WR);
2942 ret = mlx5_mr_cache_init(dev);
2944 mlx5_ib_warn(dev, "mr cache init failed %d\n", ret);
2954 mlx5_ib_destroy_qp(qp);
2968 static u8 mlx5_get_umr_fence(u8 umr_fence_cap)
2970 switch (umr_fence_cap) {
2971 case MLX5_CAP_UMR_FENCE_NONE:
2972 return MLX5_FENCE_MODE_NONE;
2973 case MLX5_CAP_UMR_FENCE_SMALL:
2974 return MLX5_FENCE_MODE_INITIATOR_SMALL;
2976 return MLX5_FENCE_MODE_STRONG_ORDERING;
2980 static int create_dev_resources(struct mlx5_ib_resources *devr)
2982 struct ib_srq_init_attr attr;
2983 struct mlx5_ib_dev *dev;
2984 struct ib_cq_init_attr cq_attr = {.cqe = 1};
2988 dev = container_of(devr, struct mlx5_ib_dev, devr);
2990 mutex_init(&devr->mutex);
2992 devr->p0 = mlx5_ib_alloc_pd(&dev->ib_dev, NULL, NULL);
2993 if (IS_ERR(devr->p0)) {
2994 ret = PTR_ERR(devr->p0);
2997 devr->p0->device = &dev->ib_dev;
2998 devr->p0->uobject = NULL;
2999 atomic_set(&devr->p0->usecnt, 0);
3001 devr->c0 = mlx5_ib_create_cq(&dev->ib_dev, &cq_attr, NULL, NULL);
3002 if (IS_ERR(devr->c0)) {
3003 ret = PTR_ERR(devr->c0);
3006 devr->c0->device = &dev->ib_dev;
3007 devr->c0->uobject = NULL;
3008 devr->c0->comp_handler = NULL;
3009 devr->c0->event_handler = NULL;
3010 devr->c0->cq_context = NULL;
3011 atomic_set(&devr->c0->usecnt, 0);
3013 devr->x0 = mlx5_ib_alloc_xrcd(&dev->ib_dev, NULL, NULL);
3014 if (IS_ERR(devr->x0)) {
3015 ret = PTR_ERR(devr->x0);
3018 devr->x0->device = &dev->ib_dev;
3019 devr->x0->inode = NULL;
3020 atomic_set(&devr->x0->usecnt, 0);
3021 mutex_init(&devr->x0->tgt_qp_mutex);
3022 INIT_LIST_HEAD(&devr->x0->tgt_qp_list);
3024 devr->x1 = mlx5_ib_alloc_xrcd(&dev->ib_dev, NULL, NULL);
3025 if (IS_ERR(devr->x1)) {
3026 ret = PTR_ERR(devr->x1);
3029 devr->x1->device = &dev->ib_dev;
3030 devr->x1->inode = NULL;
3031 atomic_set(&devr->x1->usecnt, 0);
3032 mutex_init(&devr->x1->tgt_qp_mutex);
3033 INIT_LIST_HEAD(&devr->x1->tgt_qp_list);
3035 memset(&attr, 0, sizeof(attr));
3036 attr.attr.max_sge = 1;
3037 attr.attr.max_wr = 1;
3038 attr.srq_type = IB_SRQT_XRC;
3039 attr.ext.xrc.cq = devr->c0;
3040 attr.ext.xrc.xrcd = devr->x0;
3042 devr->s0 = mlx5_ib_create_srq(devr->p0, &attr, NULL);
3043 if (IS_ERR(devr->s0)) {
3044 ret = PTR_ERR(devr->s0);
3047 devr->s0->device = &dev->ib_dev;
3048 devr->s0->pd = devr->p0;
3049 devr->s0->uobject = NULL;
3050 devr->s0->event_handler = NULL;
3051 devr->s0->srq_context = NULL;
3052 devr->s0->srq_type = IB_SRQT_XRC;
3053 devr->s0->ext.xrc.xrcd = devr->x0;
3054 devr->s0->ext.xrc.cq = devr->c0;
3055 atomic_inc(&devr->s0->ext.xrc.xrcd->usecnt);
3056 atomic_inc(&devr->s0->ext.xrc.cq->usecnt);
3057 atomic_inc(&devr->p0->usecnt);
3058 atomic_set(&devr->s0->usecnt, 0);
3060 memset(&attr, 0, sizeof(attr));
3061 attr.attr.max_sge = 1;
3062 attr.attr.max_wr = 1;
3063 attr.srq_type = IB_SRQT_BASIC;
3064 devr->s1 = mlx5_ib_create_srq(devr->p0, &attr, NULL);
3065 if (IS_ERR(devr->s1)) {
3066 ret = PTR_ERR(devr->s1);
3069 devr->s1->device = &dev->ib_dev;
3070 devr->s1->pd = devr->p0;
3071 devr->s1->uobject = NULL;
3072 devr->s1->event_handler = NULL;
3073 devr->s1->srq_context = NULL;
3074 devr->s1->srq_type = IB_SRQT_BASIC;
3075 devr->s1->ext.xrc.cq = devr->c0;
3076 atomic_inc(&devr->p0->usecnt);
3077 atomic_set(&devr->s0->usecnt, 0);
3079 for (port = 0; port < ARRAY_SIZE(devr->ports); ++port) {
3080 INIT_WORK(&devr->ports[port].pkey_change_work,
3081 pkey_change_handler);
3082 devr->ports[port].devr = devr;
3088 mlx5_ib_destroy_srq(devr->s0);
3090 mlx5_ib_dealloc_xrcd(devr->x1);
3092 mlx5_ib_dealloc_xrcd(devr->x0);
3094 mlx5_ib_destroy_cq(devr->c0);
3096 mlx5_ib_dealloc_pd(devr->p0);
3101 static void destroy_dev_resources(struct mlx5_ib_resources *devr)
3103 struct mlx5_ib_dev *dev =
3104 container_of(devr, struct mlx5_ib_dev, devr);
3107 mlx5_ib_destroy_srq(devr->s1);
3108 mlx5_ib_destroy_srq(devr->s0);
3109 mlx5_ib_dealloc_xrcd(devr->x0);
3110 mlx5_ib_dealloc_xrcd(devr->x1);
3111 mlx5_ib_destroy_cq(devr->c0);
3112 mlx5_ib_dealloc_pd(devr->p0);
3114 /* Make sure no change P_Key work items are still executing */
3115 for (port = 0; port < dev->num_ports; ++port)
3116 cancel_work_sync(&devr->ports[port].pkey_change_work);
3119 static u32 get_core_cap_flags(struct ib_device *ibdev)
3121 struct mlx5_ib_dev *dev = to_mdev(ibdev);
3122 enum rdma_link_layer ll = mlx5_ib_port_link_layer(ibdev, 1);
3123 u8 l3_type_cap = MLX5_CAP_ROCE(dev->mdev, l3_type);
3124 u8 roce_version_cap = MLX5_CAP_ROCE(dev->mdev, roce_version);
3127 if (ll == IB_LINK_LAYER_INFINIBAND)
3128 return RDMA_CORE_PORT_IBA_IB;
3130 ret = RDMA_CORE_PORT_RAW_PACKET;
3132 if (!(l3_type_cap & MLX5_ROCE_L3_TYPE_IPV4_CAP))
3135 if (!(l3_type_cap & MLX5_ROCE_L3_TYPE_IPV6_CAP))
3138 if (roce_version_cap & MLX5_ROCE_VERSION_1_CAP)
3139 ret |= RDMA_CORE_PORT_IBA_ROCE;
3141 if (roce_version_cap & MLX5_ROCE_VERSION_2_CAP)
3142 ret |= RDMA_CORE_PORT_IBA_ROCE_UDP_ENCAP;
3147 static int mlx5_port_immutable(struct ib_device *ibdev, u8 port_num,
3148 struct ib_port_immutable *immutable)
3150 struct ib_port_attr attr;
3151 struct mlx5_ib_dev *dev = to_mdev(ibdev);
3152 enum rdma_link_layer ll = mlx5_ib_port_link_layer(ibdev, port_num);
3155 immutable->core_cap_flags = get_core_cap_flags(ibdev);
3157 err = ib_query_port(ibdev, port_num, &attr);
3161 immutable->pkey_tbl_len = attr.pkey_tbl_len;
3162 immutable->gid_tbl_len = attr.gid_tbl_len;
3163 immutable->core_cap_flags = get_core_cap_flags(ibdev);
3164 if ((ll == IB_LINK_LAYER_INFINIBAND) || MLX5_CAP_GEN(dev->mdev, roce))
3165 immutable->max_mad_size = IB_MGMT_MAD_SIZE;
3170 static void get_dev_fw_str(struct ib_device *ibdev, char *str,
3173 struct mlx5_ib_dev *dev =
3174 container_of(ibdev, struct mlx5_ib_dev, ib_dev);
3175 snprintf(str, str_len, "%d.%d.%04d", fw_rev_maj(dev->mdev),
3176 fw_rev_min(dev->mdev), fw_rev_sub(dev->mdev));
3179 static int mlx5_eth_lag_init(struct mlx5_ib_dev *dev)
3181 struct mlx5_core_dev *mdev = dev->mdev;
3182 struct mlx5_flow_namespace *ns = mlx5_get_flow_namespace(mdev,
3183 MLX5_FLOW_NAMESPACE_LAG);
3184 struct mlx5_flow_table *ft;
3187 if (!ns || !mlx5_lag_is_active(mdev))
3190 err = mlx5_cmd_create_vport_lag(mdev);
3194 ft = mlx5_create_lag_demux_flow_table(ns, 0, 0);
3197 goto err_destroy_vport_lag;
3200 dev->flow_db.lag_demux_ft = ft;
3203 err_destroy_vport_lag:
3204 mlx5_cmd_destroy_vport_lag(mdev);
3208 static void mlx5_eth_lag_cleanup(struct mlx5_ib_dev *dev)
3210 struct mlx5_core_dev *mdev = dev->mdev;
3212 if (dev->flow_db.lag_demux_ft) {
3213 mlx5_destroy_flow_table(dev->flow_db.lag_demux_ft);
3214 dev->flow_db.lag_demux_ft = NULL;
3216 mlx5_cmd_destroy_vport_lag(mdev);
3220 static int mlx5_add_netdev_notifier(struct mlx5_ib_dev *dev)
3224 dev->roce.nb.notifier_call = mlx5_netdev_event;
3225 err = register_netdevice_notifier(&dev->roce.nb);
3227 dev->roce.nb.notifier_call = NULL;
3234 static void mlx5_remove_netdev_notifier(struct mlx5_ib_dev *dev)
3236 if (dev->roce.nb.notifier_call) {
3237 unregister_netdevice_notifier(&dev->roce.nb);
3238 dev->roce.nb.notifier_call = NULL;
3242 static int mlx5_enable_eth(struct mlx5_ib_dev *dev)
3246 err = mlx5_add_netdev_notifier(dev);
3250 if (MLX5_CAP_GEN(dev->mdev, roce)) {
3251 err = mlx5_nic_vport_enable_roce(dev->mdev);
3253 goto err_unregister_netdevice_notifier;
3256 err = mlx5_eth_lag_init(dev);
3258 goto err_disable_roce;
3263 if (MLX5_CAP_GEN(dev->mdev, roce))
3264 mlx5_nic_vport_disable_roce(dev->mdev);
3266 err_unregister_netdevice_notifier:
3267 mlx5_remove_netdev_notifier(dev);
3271 static void mlx5_disable_eth(struct mlx5_ib_dev *dev)
3273 mlx5_eth_lag_cleanup(dev);
3274 if (MLX5_CAP_GEN(dev->mdev, roce))
3275 mlx5_nic_vport_disable_roce(dev->mdev);
3278 struct mlx5_ib_counter {
3283 #define INIT_Q_COUNTER(_name) \
3284 { .name = #_name, .offset = MLX5_BYTE_OFF(query_q_counter_out, _name)}
3286 static const struct mlx5_ib_counter basic_q_cnts[] = {
3287 INIT_Q_COUNTER(rx_write_requests),
3288 INIT_Q_COUNTER(rx_read_requests),
3289 INIT_Q_COUNTER(rx_atomic_requests),
3290 INIT_Q_COUNTER(out_of_buffer),
3293 static const struct mlx5_ib_counter out_of_seq_q_cnts[] = {
3294 INIT_Q_COUNTER(out_of_sequence),
3297 static const struct mlx5_ib_counter retrans_q_cnts[] = {
3298 INIT_Q_COUNTER(duplicate_request),
3299 INIT_Q_COUNTER(rnr_nak_retry_err),
3300 INIT_Q_COUNTER(packet_seq_err),
3301 INIT_Q_COUNTER(implied_nak_seq_err),
3302 INIT_Q_COUNTER(local_ack_timeout_err),
3305 #define INIT_CONG_COUNTER(_name) \
3306 { .name = #_name, .offset = \
3307 MLX5_BYTE_OFF(query_cong_statistics_out, _name ## _high)}
3309 static const struct mlx5_ib_counter cong_cnts[] = {
3310 INIT_CONG_COUNTER(rp_cnp_ignored),
3311 INIT_CONG_COUNTER(rp_cnp_handled),
3312 INIT_CONG_COUNTER(np_ecn_marked_roce_packets),
3313 INIT_CONG_COUNTER(np_cnp_sent),
3316 static void mlx5_ib_dealloc_counters(struct mlx5_ib_dev *dev)
3320 for (i = 0; i < dev->num_ports; i++) {
3321 mlx5_core_dealloc_q_counter(dev->mdev,
3322 dev->port[i].cnts.set_id);
3323 kfree(dev->port[i].cnts.names);
3324 kfree(dev->port[i].cnts.offsets);
3328 static int __mlx5_ib_alloc_counters(struct mlx5_ib_dev *dev,
3329 struct mlx5_ib_counters *cnts)
3333 num_counters = ARRAY_SIZE(basic_q_cnts);
3335 if (MLX5_CAP_GEN(dev->mdev, out_of_seq_cnt))
3336 num_counters += ARRAY_SIZE(out_of_seq_q_cnts);
3338 if (MLX5_CAP_GEN(dev->mdev, retransmission_q_counters))
3339 num_counters += ARRAY_SIZE(retrans_q_cnts);
3340 cnts->num_q_counters = num_counters;
3342 if (MLX5_CAP_GEN(dev->mdev, cc_query_allowed)) {
3343 cnts->num_cong_counters = ARRAY_SIZE(cong_cnts);
3344 num_counters += ARRAY_SIZE(cong_cnts);
3347 cnts->names = kcalloc(num_counters, sizeof(cnts->names), GFP_KERNEL);
3351 cnts->offsets = kcalloc(num_counters,
3352 sizeof(cnts->offsets), GFP_KERNEL);
3363 static void mlx5_ib_fill_counters(struct mlx5_ib_dev *dev,
3370 for (i = 0; i < ARRAY_SIZE(basic_q_cnts); i++, j++) {
3371 names[j] = basic_q_cnts[i].name;
3372 offsets[j] = basic_q_cnts[i].offset;
3375 if (MLX5_CAP_GEN(dev->mdev, out_of_seq_cnt)) {
3376 for (i = 0; i < ARRAY_SIZE(out_of_seq_q_cnts); i++, j++) {
3377 names[j] = out_of_seq_q_cnts[i].name;
3378 offsets[j] = out_of_seq_q_cnts[i].offset;
3382 if (MLX5_CAP_GEN(dev->mdev, retransmission_q_counters)) {
3383 for (i = 0; i < ARRAY_SIZE(retrans_q_cnts); i++, j++) {
3384 names[j] = retrans_q_cnts[i].name;
3385 offsets[j] = retrans_q_cnts[i].offset;
3389 if (MLX5_CAP_GEN(dev->mdev, cc_query_allowed)) {
3390 for (i = 0; i < ARRAY_SIZE(cong_cnts); i++, j++) {
3391 names[j] = cong_cnts[i].name;
3392 offsets[j] = cong_cnts[i].offset;
3397 static int mlx5_ib_alloc_counters(struct mlx5_ib_dev *dev)
3402 for (i = 0; i < dev->num_ports; i++) {
3403 struct mlx5_ib_port *port = &dev->port[i];
3405 ret = mlx5_core_alloc_q_counter(dev->mdev,
3406 &port->cnts.set_id);
3409 "couldn't allocate queue counter for port %d, err %d\n",
3411 goto dealloc_counters;
3414 ret = __mlx5_ib_alloc_counters(dev, &port->cnts);
3416 goto dealloc_counters;
3418 mlx5_ib_fill_counters(dev, port->cnts.names,
3419 port->cnts.offsets);
3426 mlx5_core_dealloc_q_counter(dev->mdev,
3427 dev->port[i].cnts.set_id);
3432 static struct rdma_hw_stats *mlx5_ib_alloc_hw_stats(struct ib_device *ibdev,
3435 struct mlx5_ib_dev *dev = to_mdev(ibdev);
3436 struct mlx5_ib_port *port = &dev->port[port_num - 1];
3438 /* We support only per port stats */
3442 return rdma_alloc_hw_stats_struct(port->cnts.names,
3443 port->cnts.num_q_counters +
3444 port->cnts.num_cong_counters,
3445 RDMA_HW_STATS_DEFAULT_LIFESPAN);
3448 static int mlx5_ib_query_q_counters(struct mlx5_ib_dev *dev,
3449 struct mlx5_ib_port *port,
3450 struct rdma_hw_stats *stats)
3452 int outlen = MLX5_ST_SZ_BYTES(query_q_counter_out);
3457 out = kvzalloc(outlen, GFP_KERNEL);
3461 ret = mlx5_core_query_q_counter(dev->mdev,
3462 port->cnts.set_id, 0,
3467 for (i = 0; i < port->cnts.num_q_counters; i++) {
3468 val = *(__be32 *)(out + port->cnts.offsets[i]);
3469 stats->value[i] = (u64)be32_to_cpu(val);
3477 static int mlx5_ib_query_cong_counters(struct mlx5_ib_dev *dev,
3478 struct mlx5_ib_port *port,
3479 struct rdma_hw_stats *stats)
3481 int outlen = MLX5_ST_SZ_BYTES(query_cong_statistics_out);
3484 int offset = port->cnts.num_q_counters;
3486 out = kvzalloc(outlen, GFP_KERNEL);
3490 ret = mlx5_cmd_query_cong_counter(dev->mdev, false, out, outlen);
3494 for (i = 0; i < port->cnts.num_cong_counters; i++) {
3495 stats->value[i + offset] =
3496 be64_to_cpup((__be64 *)(out +
3497 port->cnts.offsets[i + offset]));
3505 static int mlx5_ib_get_hw_stats(struct ib_device *ibdev,
3506 struct rdma_hw_stats *stats,
3507 u8 port_num, int index)
3509 struct mlx5_ib_dev *dev = to_mdev(ibdev);
3510 struct mlx5_ib_port *port = &dev->port[port_num - 1];
3511 int ret, num_counters;
3516 ret = mlx5_ib_query_q_counters(dev, port, stats);
3519 num_counters = port->cnts.num_q_counters;
3521 if (MLX5_CAP_GEN(dev->mdev, cc_query_allowed)) {
3522 ret = mlx5_ib_query_cong_counters(dev, port, stats);
3525 num_counters += port->cnts.num_cong_counters;
3528 return num_counters;
3531 static void mlx5_ib_free_rdma_netdev(struct net_device *netdev)
3533 return mlx5_rdma_netdev_free(netdev);
3536 static struct net_device*
3537 mlx5_ib_alloc_rdma_netdev(struct ib_device *hca,
3539 enum rdma_netdev_t type,
3541 unsigned char name_assign_type,
3542 void (*setup)(struct net_device *))
3544 struct net_device *netdev;
3545 struct rdma_netdev *rn;
3547 if (type != RDMA_NETDEV_IPOIB)
3548 return ERR_PTR(-EOPNOTSUPP);
3550 netdev = mlx5_rdma_netdev_alloc(to_mdev(hca)->mdev, hca,
3552 if (likely(!IS_ERR_OR_NULL(netdev))) {
3553 rn = netdev_priv(netdev);
3554 rn->free_rdma_netdev = mlx5_ib_free_rdma_netdev;
3559 static void *mlx5_ib_add(struct mlx5_core_dev *mdev)
3561 struct mlx5_ib_dev *dev;
3562 enum rdma_link_layer ll;
3568 port_type_cap = MLX5_CAP_GEN(mdev, port_type);
3569 ll = mlx5_port_type_cap_to_rdma_ll(port_type_cap);
3571 printk_once(KERN_INFO "%s", mlx5_version);
3573 dev = (struct mlx5_ib_dev *)ib_alloc_device(sizeof(*dev));
3579 dev->port = kcalloc(MLX5_CAP_GEN(mdev, num_ports), sizeof(*dev->port),
3584 rwlock_init(&dev->roce.netdev_lock);
3585 err = get_port_caps(dev);
3589 if (mlx5_use_mad_ifc(dev))
3590 get_ext_port_caps(dev);
3592 if (!mlx5_lag_is_active(mdev))
3595 name = "mlx5_bond_%d";
3597 strlcpy(dev->ib_dev.name, name, IB_DEVICE_NAME_MAX);
3598 dev->ib_dev.owner = THIS_MODULE;
3599 dev->ib_dev.node_type = RDMA_NODE_IB_CA;
3600 dev->ib_dev.local_dma_lkey = 0 /* not supported for now */;
3601 dev->num_ports = MLX5_CAP_GEN(mdev, num_ports);
3602 dev->ib_dev.phys_port_cnt = dev->num_ports;
3603 dev->ib_dev.num_comp_vectors =
3604 dev->mdev->priv.eq_table.num_comp_vectors;
3605 dev->ib_dev.dev.parent = &mdev->pdev->dev;
3607 dev->ib_dev.uverbs_abi_ver = MLX5_IB_UVERBS_ABI_VERSION;
3608 dev->ib_dev.uverbs_cmd_mask =
3609 (1ull << IB_USER_VERBS_CMD_GET_CONTEXT) |
3610 (1ull << IB_USER_VERBS_CMD_QUERY_DEVICE) |
3611 (1ull << IB_USER_VERBS_CMD_QUERY_PORT) |
3612 (1ull << IB_USER_VERBS_CMD_ALLOC_PD) |
3613 (1ull << IB_USER_VERBS_CMD_DEALLOC_PD) |
3614 (1ull << IB_USER_VERBS_CMD_CREATE_AH) |
3615 (1ull << IB_USER_VERBS_CMD_DESTROY_AH) |
3616 (1ull << IB_USER_VERBS_CMD_REG_MR) |
3617 (1ull << IB_USER_VERBS_CMD_REREG_MR) |
3618 (1ull << IB_USER_VERBS_CMD_DEREG_MR) |
3619 (1ull << IB_USER_VERBS_CMD_CREATE_COMP_CHANNEL) |
3620 (1ull << IB_USER_VERBS_CMD_CREATE_CQ) |
3621 (1ull << IB_USER_VERBS_CMD_RESIZE_CQ) |
3622 (1ull << IB_USER_VERBS_CMD_DESTROY_CQ) |
3623 (1ull << IB_USER_VERBS_CMD_CREATE_QP) |
3624 (1ull << IB_USER_VERBS_CMD_MODIFY_QP) |
3625 (1ull << IB_USER_VERBS_CMD_QUERY_QP) |
3626 (1ull << IB_USER_VERBS_CMD_DESTROY_QP) |
3627 (1ull << IB_USER_VERBS_CMD_ATTACH_MCAST) |
3628 (1ull << IB_USER_VERBS_CMD_DETACH_MCAST) |
3629 (1ull << IB_USER_VERBS_CMD_CREATE_SRQ) |
3630 (1ull << IB_USER_VERBS_CMD_MODIFY_SRQ) |
3631 (1ull << IB_USER_VERBS_CMD_QUERY_SRQ) |
3632 (1ull << IB_USER_VERBS_CMD_DESTROY_SRQ) |
3633 (1ull << IB_USER_VERBS_CMD_CREATE_XSRQ) |
3634 (1ull << IB_USER_VERBS_CMD_OPEN_QP);
3635 dev->ib_dev.uverbs_ex_cmd_mask =
3636 (1ull << IB_USER_VERBS_EX_CMD_QUERY_DEVICE) |
3637 (1ull << IB_USER_VERBS_EX_CMD_CREATE_CQ) |
3638 (1ull << IB_USER_VERBS_EX_CMD_CREATE_QP) |
3639 (1ull << IB_USER_VERBS_EX_CMD_MODIFY_QP);
3641 dev->ib_dev.query_device = mlx5_ib_query_device;
3642 dev->ib_dev.query_port = mlx5_ib_query_port;
3643 dev->ib_dev.get_link_layer = mlx5_ib_port_link_layer;
3644 if (ll == IB_LINK_LAYER_ETHERNET)
3645 dev->ib_dev.get_netdev = mlx5_ib_get_netdev;
3646 dev->ib_dev.query_gid = mlx5_ib_query_gid;
3647 dev->ib_dev.add_gid = mlx5_ib_add_gid;
3648 dev->ib_dev.del_gid = mlx5_ib_del_gid;
3649 dev->ib_dev.query_pkey = mlx5_ib_query_pkey;
3650 dev->ib_dev.modify_device = mlx5_ib_modify_device;
3651 dev->ib_dev.modify_port = mlx5_ib_modify_port;
3652 dev->ib_dev.alloc_ucontext = mlx5_ib_alloc_ucontext;
3653 dev->ib_dev.dealloc_ucontext = mlx5_ib_dealloc_ucontext;
3654 dev->ib_dev.mmap = mlx5_ib_mmap;
3655 dev->ib_dev.alloc_pd = mlx5_ib_alloc_pd;
3656 dev->ib_dev.dealloc_pd = mlx5_ib_dealloc_pd;
3657 dev->ib_dev.create_ah = mlx5_ib_create_ah;
3658 dev->ib_dev.query_ah = mlx5_ib_query_ah;
3659 dev->ib_dev.destroy_ah = mlx5_ib_destroy_ah;
3660 dev->ib_dev.create_srq = mlx5_ib_create_srq;
3661 dev->ib_dev.modify_srq = mlx5_ib_modify_srq;
3662 dev->ib_dev.query_srq = mlx5_ib_query_srq;
3663 dev->ib_dev.destroy_srq = mlx5_ib_destroy_srq;
3664 dev->ib_dev.post_srq_recv = mlx5_ib_post_srq_recv;
3665 dev->ib_dev.create_qp = mlx5_ib_create_qp;
3666 dev->ib_dev.modify_qp = mlx5_ib_modify_qp;
3667 dev->ib_dev.query_qp = mlx5_ib_query_qp;
3668 dev->ib_dev.destroy_qp = mlx5_ib_destroy_qp;
3669 dev->ib_dev.post_send = mlx5_ib_post_send;
3670 dev->ib_dev.post_recv = mlx5_ib_post_recv;
3671 dev->ib_dev.create_cq = mlx5_ib_create_cq;
3672 dev->ib_dev.modify_cq = mlx5_ib_modify_cq;
3673 dev->ib_dev.resize_cq = mlx5_ib_resize_cq;
3674 dev->ib_dev.destroy_cq = mlx5_ib_destroy_cq;
3675 dev->ib_dev.poll_cq = mlx5_ib_poll_cq;
3676 dev->ib_dev.req_notify_cq = mlx5_ib_arm_cq;
3677 dev->ib_dev.get_dma_mr = mlx5_ib_get_dma_mr;
3678 dev->ib_dev.reg_user_mr = mlx5_ib_reg_user_mr;
3679 dev->ib_dev.rereg_user_mr = mlx5_ib_rereg_user_mr;
3680 dev->ib_dev.dereg_mr = mlx5_ib_dereg_mr;
3681 dev->ib_dev.attach_mcast = mlx5_ib_mcg_attach;
3682 dev->ib_dev.detach_mcast = mlx5_ib_mcg_detach;
3683 dev->ib_dev.process_mad = mlx5_ib_process_mad;
3684 dev->ib_dev.alloc_mr = mlx5_ib_alloc_mr;
3685 dev->ib_dev.map_mr_sg = mlx5_ib_map_mr_sg;
3686 dev->ib_dev.check_mr_status = mlx5_ib_check_mr_status;
3687 dev->ib_dev.get_port_immutable = mlx5_port_immutable;
3688 dev->ib_dev.get_dev_fw_str = get_dev_fw_str;
3689 if (MLX5_CAP_GEN(mdev, ipoib_enhanced_offloads))
3690 dev->ib_dev.alloc_rdma_netdev = mlx5_ib_alloc_rdma_netdev;
3692 if (mlx5_core_is_pf(mdev)) {
3693 dev->ib_dev.get_vf_config = mlx5_ib_get_vf_config;
3694 dev->ib_dev.set_vf_link_state = mlx5_ib_set_vf_link_state;
3695 dev->ib_dev.get_vf_stats = mlx5_ib_get_vf_stats;
3696 dev->ib_dev.set_vf_guid = mlx5_ib_set_vf_guid;
3699 dev->ib_dev.disassociate_ucontext = mlx5_ib_disassociate_ucontext;
3701 mlx5_ib_internal_fill_odp_caps(dev);
3703 dev->umr_fence = mlx5_get_umr_fence(MLX5_CAP_GEN(mdev, umr_fence));
3705 if (MLX5_CAP_GEN(mdev, imaicl)) {
3706 dev->ib_dev.alloc_mw = mlx5_ib_alloc_mw;
3707 dev->ib_dev.dealloc_mw = mlx5_ib_dealloc_mw;
3708 dev->ib_dev.uverbs_cmd_mask |=
3709 (1ull << IB_USER_VERBS_CMD_ALLOC_MW) |
3710 (1ull << IB_USER_VERBS_CMD_DEALLOC_MW);
3713 if (MLX5_CAP_GEN(dev->mdev, max_qp_cnt)) {
3714 dev->ib_dev.get_hw_stats = mlx5_ib_get_hw_stats;
3715 dev->ib_dev.alloc_hw_stats = mlx5_ib_alloc_hw_stats;
3718 if (MLX5_CAP_GEN(mdev, xrc)) {
3719 dev->ib_dev.alloc_xrcd = mlx5_ib_alloc_xrcd;
3720 dev->ib_dev.dealloc_xrcd = mlx5_ib_dealloc_xrcd;
3721 dev->ib_dev.uverbs_cmd_mask |=
3722 (1ull << IB_USER_VERBS_CMD_OPEN_XRCD) |
3723 (1ull << IB_USER_VERBS_CMD_CLOSE_XRCD);
3726 if (mlx5_ib_port_link_layer(&dev->ib_dev, 1) ==
3727 IB_LINK_LAYER_ETHERNET) {
3728 dev->ib_dev.create_flow = mlx5_ib_create_flow;
3729 dev->ib_dev.destroy_flow = mlx5_ib_destroy_flow;
3730 dev->ib_dev.create_wq = mlx5_ib_create_wq;
3731 dev->ib_dev.modify_wq = mlx5_ib_modify_wq;
3732 dev->ib_dev.destroy_wq = mlx5_ib_destroy_wq;
3733 dev->ib_dev.create_rwq_ind_table = mlx5_ib_create_rwq_ind_table;
3734 dev->ib_dev.destroy_rwq_ind_table = mlx5_ib_destroy_rwq_ind_table;
3735 dev->ib_dev.uverbs_ex_cmd_mask |=
3736 (1ull << IB_USER_VERBS_EX_CMD_CREATE_FLOW) |
3737 (1ull << IB_USER_VERBS_EX_CMD_DESTROY_FLOW) |
3738 (1ull << IB_USER_VERBS_EX_CMD_CREATE_WQ) |
3739 (1ull << IB_USER_VERBS_EX_CMD_MODIFY_WQ) |
3740 (1ull << IB_USER_VERBS_EX_CMD_DESTROY_WQ) |
3741 (1ull << IB_USER_VERBS_EX_CMD_CREATE_RWQ_IND_TBL) |
3742 (1ull << IB_USER_VERBS_EX_CMD_DESTROY_RWQ_IND_TBL);
3744 err = init_node_data(dev);
3748 mutex_init(&dev->flow_db.lock);
3749 mutex_init(&dev->cap_mask_mutex);
3750 INIT_LIST_HEAD(&dev->qp_list);
3751 spin_lock_init(&dev->reset_flow_resource_lock);
3753 if (ll == IB_LINK_LAYER_ETHERNET) {
3754 err = mlx5_enable_eth(dev);
3759 err = create_dev_resources(&dev->devr);
3761 goto err_disable_eth;
3763 err = mlx5_ib_odp_init_one(dev);
3767 if (MLX5_CAP_GEN(dev->mdev, max_qp_cnt)) {
3768 err = mlx5_ib_alloc_counters(dev);
3773 dev->mdev->priv.uar = mlx5_get_uars_page(dev->mdev);
3774 if (!dev->mdev->priv.uar)
3777 err = mlx5_alloc_bfreg(dev->mdev, &dev->bfreg, false, false);
3781 err = mlx5_alloc_bfreg(dev->mdev, &dev->fp_bfreg, false, true);
3785 err = ib_register_device(&dev->ib_dev, NULL);
3789 err = create_umr_res(dev);
3793 for (i = 0; i < ARRAY_SIZE(mlx5_class_attributes); i++) {
3794 err = device_create_file(&dev->ib_dev.dev,
3795 mlx5_class_attributes[i]);
3800 dev->ib_active = true;
3805 destroy_umrc_res(dev);
3808 ib_unregister_device(&dev->ib_dev);
3811 mlx5_free_bfreg(dev->mdev, &dev->fp_bfreg);
3814 mlx5_free_bfreg(dev->mdev, &dev->bfreg);
3817 mlx5_put_uars_page(dev->mdev, dev->mdev->priv.uar);
3820 if (MLX5_CAP_GEN(dev->mdev, max_qp_cnt))
3821 mlx5_ib_dealloc_counters(dev);
3824 mlx5_ib_odp_remove_one(dev);
3827 destroy_dev_resources(&dev->devr);
3830 if (ll == IB_LINK_LAYER_ETHERNET) {
3831 mlx5_disable_eth(dev);
3832 mlx5_remove_netdev_notifier(dev);
3839 ib_dealloc_device((struct ib_device *)dev);
3844 static void mlx5_ib_remove(struct mlx5_core_dev *mdev, void *context)
3846 struct mlx5_ib_dev *dev = context;
3847 enum rdma_link_layer ll = mlx5_ib_port_link_layer(&dev->ib_dev, 1);
3849 mlx5_remove_netdev_notifier(dev);
3850 ib_unregister_device(&dev->ib_dev);
3851 mlx5_free_bfreg(dev->mdev, &dev->fp_bfreg);
3852 mlx5_free_bfreg(dev->mdev, &dev->bfreg);
3853 mlx5_put_uars_page(dev->mdev, mdev->priv.uar);
3854 if (MLX5_CAP_GEN(dev->mdev, max_qp_cnt))
3855 mlx5_ib_dealloc_counters(dev);
3856 destroy_umrc_res(dev);
3857 mlx5_ib_odp_remove_one(dev);
3858 destroy_dev_resources(&dev->devr);
3859 if (ll == IB_LINK_LAYER_ETHERNET)
3860 mlx5_disable_eth(dev);
3862 ib_dealloc_device(&dev->ib_dev);
3865 static struct mlx5_interface mlx5_ib_interface = {
3867 .remove = mlx5_ib_remove,
3868 .event = mlx5_ib_event,
3869 #ifdef CONFIG_INFINIBAND_ON_DEMAND_PAGING
3870 .pfault = mlx5_ib_pfault,
3872 .protocol = MLX5_INTERFACE_PROTOCOL_IB,
3875 static int __init mlx5_ib_init(void)
3881 err = mlx5_register_interface(&mlx5_ib_interface);
3886 static void __exit mlx5_ib_cleanup(void)
3888 mlx5_unregister_interface(&mlx5_ib_interface);
3891 module_init(mlx5_ib_init);
3892 module_exit(mlx5_ib_cleanup);