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");
69 static char mlx5_version
[] =
70 DRIVER_NAME
": Mellanox Connect-IB Infiniband driver v"
74 MLX5_ATOMIC_SIZE_QP_8BYTES
= 1 << 3,
77 static enum rdma_link_layer
78 mlx5_port_type_cap_to_rdma_ll(int port_type_cap
)
80 switch (port_type_cap
) {
81 case MLX5_CAP_PORT_TYPE_IB
:
82 return IB_LINK_LAYER_INFINIBAND
;
83 case MLX5_CAP_PORT_TYPE_ETH
:
84 return IB_LINK_LAYER_ETHERNET
;
86 return IB_LINK_LAYER_UNSPECIFIED
;
90 static enum rdma_link_layer
91 mlx5_ib_port_link_layer(struct ib_device
*device
, u8 port_num
)
93 struct mlx5_ib_dev
*dev
= to_mdev(device
);
94 int port_type_cap
= MLX5_CAP_GEN(dev
->mdev
, port_type
);
96 return mlx5_port_type_cap_to_rdma_ll(port_type_cap
);
99 static int mlx5_netdev_event(struct notifier_block
*this,
100 unsigned long event
, void *ptr
)
102 struct net_device
*ndev
= netdev_notifier_info_to_dev(ptr
);
103 struct mlx5_ib_dev
*ibdev
= container_of(this, struct mlx5_ib_dev
,
107 case NETDEV_REGISTER
:
108 case NETDEV_UNREGISTER
:
109 write_lock(&ibdev
->roce
.netdev_lock
);
110 if (ndev
->dev
.parent
== &ibdev
->mdev
->pdev
->dev
)
111 ibdev
->roce
.netdev
= (event
== NETDEV_UNREGISTER
) ?
113 write_unlock(&ibdev
->roce
.netdev_lock
);
118 struct net_device
*lag_ndev
= mlx5_lag_get_roce_netdev(ibdev
->mdev
);
119 struct net_device
*upper
= NULL
;
122 upper
= netdev_master_upper_dev_get(lag_ndev
);
126 if ((upper
== ndev
|| (!upper
&& ndev
== ibdev
->roce
.netdev
))
127 && ibdev
->ib_active
) {
128 struct ib_event ibev
= { };
130 ibev
.device
= &ibdev
->ib_dev
;
131 ibev
.event
= (event
== NETDEV_UP
) ?
132 IB_EVENT_PORT_ACTIVE
: IB_EVENT_PORT_ERR
;
133 ibev
.element
.port_num
= 1;
134 ib_dispatch_event(&ibev
);
146 static struct net_device
*mlx5_ib_get_netdev(struct ib_device
*device
,
149 struct mlx5_ib_dev
*ibdev
= to_mdev(device
);
150 struct net_device
*ndev
;
152 ndev
= mlx5_lag_get_roce_netdev(ibdev
->mdev
);
156 /* Ensure ndev does not disappear before we invoke dev_hold()
158 read_lock(&ibdev
->roce
.netdev_lock
);
159 ndev
= ibdev
->roce
.netdev
;
162 read_unlock(&ibdev
->roce
.netdev_lock
);
167 static int translate_eth_proto_oper(u32 eth_proto_oper
, u8
*active_speed
,
170 switch (eth_proto_oper
) {
171 case MLX5E_PROT_MASK(MLX5E_1000BASE_CX_SGMII
):
172 case MLX5E_PROT_MASK(MLX5E_1000BASE_KX
):
173 case MLX5E_PROT_MASK(MLX5E_100BASE_TX
):
174 case MLX5E_PROT_MASK(MLX5E_1000BASE_T
):
175 *active_width
= IB_WIDTH_1X
;
176 *active_speed
= IB_SPEED_SDR
;
178 case MLX5E_PROT_MASK(MLX5E_10GBASE_T
):
179 case MLX5E_PROT_MASK(MLX5E_10GBASE_CX4
):
180 case MLX5E_PROT_MASK(MLX5E_10GBASE_KX4
):
181 case MLX5E_PROT_MASK(MLX5E_10GBASE_KR
):
182 case MLX5E_PROT_MASK(MLX5E_10GBASE_CR
):
183 case MLX5E_PROT_MASK(MLX5E_10GBASE_SR
):
184 case MLX5E_PROT_MASK(MLX5E_10GBASE_ER
):
185 *active_width
= IB_WIDTH_1X
;
186 *active_speed
= IB_SPEED_QDR
;
188 case MLX5E_PROT_MASK(MLX5E_25GBASE_CR
):
189 case MLX5E_PROT_MASK(MLX5E_25GBASE_KR
):
190 case MLX5E_PROT_MASK(MLX5E_25GBASE_SR
):
191 *active_width
= IB_WIDTH_1X
;
192 *active_speed
= IB_SPEED_EDR
;
194 case MLX5E_PROT_MASK(MLX5E_40GBASE_CR4
):
195 case MLX5E_PROT_MASK(MLX5E_40GBASE_KR4
):
196 case MLX5E_PROT_MASK(MLX5E_40GBASE_SR4
):
197 case MLX5E_PROT_MASK(MLX5E_40GBASE_LR4
):
198 *active_width
= IB_WIDTH_4X
;
199 *active_speed
= IB_SPEED_QDR
;
201 case MLX5E_PROT_MASK(MLX5E_50GBASE_CR2
):
202 case MLX5E_PROT_MASK(MLX5E_50GBASE_KR2
):
203 case MLX5E_PROT_MASK(MLX5E_50GBASE_SR2
):
204 *active_width
= IB_WIDTH_1X
;
205 *active_speed
= IB_SPEED_HDR
;
207 case MLX5E_PROT_MASK(MLX5E_56GBASE_R4
):
208 *active_width
= IB_WIDTH_4X
;
209 *active_speed
= IB_SPEED_FDR
;
211 case MLX5E_PROT_MASK(MLX5E_100GBASE_CR4
):
212 case MLX5E_PROT_MASK(MLX5E_100GBASE_SR4
):
213 case MLX5E_PROT_MASK(MLX5E_100GBASE_KR4
):
214 case MLX5E_PROT_MASK(MLX5E_100GBASE_LR4
):
215 *active_width
= IB_WIDTH_4X
;
216 *active_speed
= IB_SPEED_EDR
;
225 static int mlx5_query_port_roce(struct ib_device
*device
, u8 port_num
,
226 struct ib_port_attr
*props
)
228 struct mlx5_ib_dev
*dev
= to_mdev(device
);
229 struct mlx5_core_dev
*mdev
= dev
->mdev
;
230 struct net_device
*ndev
, *upper
;
231 enum ib_mtu ndev_ib_mtu
;
236 /* Possible bad flows are checked before filling out props so in case
237 * of an error it will still be zeroed out.
239 err
= mlx5_query_port_eth_proto_oper(mdev
, ð_prot_oper
, port_num
);
243 translate_eth_proto_oper(eth_prot_oper
, &props
->active_speed
,
244 &props
->active_width
);
246 props
->port_cap_flags
|= IB_PORT_CM_SUP
;
247 props
->port_cap_flags
|= IB_PORT_IP_BASED_GIDS
;
249 props
->gid_tbl_len
= MLX5_CAP_ROCE(dev
->mdev
,
250 roce_address_table_size
);
251 props
->max_mtu
= IB_MTU_4096
;
252 props
->max_msg_sz
= 1 << MLX5_CAP_GEN(dev
->mdev
, log_max_msg
);
253 props
->pkey_tbl_len
= 1;
254 props
->state
= IB_PORT_DOWN
;
255 props
->phys_state
= 3;
257 mlx5_query_nic_vport_qkey_viol_cntr(dev
->mdev
, &qkey_viol_cntr
);
258 props
->qkey_viol_cntr
= qkey_viol_cntr
;
260 ndev
= mlx5_ib_get_netdev(device
, port_num
);
264 if (mlx5_lag_is_active(dev
->mdev
)) {
266 upper
= netdev_master_upper_dev_get_rcu(ndev
);
275 if (netif_running(ndev
) && netif_carrier_ok(ndev
)) {
276 props
->state
= IB_PORT_ACTIVE
;
277 props
->phys_state
= 5;
280 ndev_ib_mtu
= iboe_get_mtu(ndev
->mtu
);
284 props
->active_mtu
= min(props
->max_mtu
, ndev_ib_mtu
);
288 static int set_roce_addr(struct mlx5_ib_dev
*dev
, u8 port_num
,
289 unsigned int index
, const union ib_gid
*gid
,
290 const struct ib_gid_attr
*attr
)
292 enum ib_gid_type gid_type
= IB_GID_TYPE_IB
;
300 gid_type
= attr
->gid_type
;
301 ether_addr_copy(mac
, attr
->ndev
->dev_addr
);
303 if (is_vlan_dev(attr
->ndev
)) {
305 vlan_id
= vlan_dev_vlan_id(attr
->ndev
);
311 roce_version
= MLX5_ROCE_VERSION_1
;
313 case IB_GID_TYPE_ROCE_UDP_ENCAP
:
314 roce_version
= MLX5_ROCE_VERSION_2
;
315 if (ipv6_addr_v4mapped((void *)gid
))
316 roce_l3_type
= MLX5_ROCE_L3_TYPE_IPV4
;
318 roce_l3_type
= MLX5_ROCE_L3_TYPE_IPV6
;
322 mlx5_ib_warn(dev
, "Unexpected GID type %u\n", gid_type
);
325 return mlx5_core_roce_gid_set(dev
->mdev
, index
, roce_version
,
326 roce_l3_type
, gid
->raw
, mac
, vlan
,
330 static int mlx5_ib_add_gid(struct ib_device
*device
, u8 port_num
,
331 unsigned int index
, const union ib_gid
*gid
,
332 const struct ib_gid_attr
*attr
,
333 __always_unused
void **context
)
335 return set_roce_addr(to_mdev(device
), port_num
, index
, gid
, attr
);
338 static int mlx5_ib_del_gid(struct ib_device
*device
, u8 port_num
,
339 unsigned int index
, __always_unused
void **context
)
341 return set_roce_addr(to_mdev(device
), port_num
, index
, NULL
, NULL
);
344 __be16
mlx5_get_roce_udp_sport(struct mlx5_ib_dev
*dev
, u8 port_num
,
347 struct ib_gid_attr attr
;
350 if (ib_get_cached_gid(&dev
->ib_dev
, port_num
, index
, &gid
, &attr
))
358 if (attr
.gid_type
!= IB_GID_TYPE_ROCE_UDP_ENCAP
)
361 return cpu_to_be16(MLX5_CAP_ROCE(dev
->mdev
, r_roce_min_src_udp_port
));
364 int mlx5_get_roce_gid_type(struct mlx5_ib_dev
*dev
, u8 port_num
,
365 int index
, enum ib_gid_type
*gid_type
)
367 struct ib_gid_attr attr
;
371 ret
= ib_get_cached_gid(&dev
->ib_dev
, port_num
, index
, &gid
, &attr
);
380 *gid_type
= attr
.gid_type
;
385 static int mlx5_use_mad_ifc(struct mlx5_ib_dev
*dev
)
387 if (MLX5_CAP_GEN(dev
->mdev
, port_type
) == MLX5_CAP_PORT_TYPE_IB
)
388 return !MLX5_CAP_GEN(dev
->mdev
, ib_virt
);
393 MLX5_VPORT_ACCESS_METHOD_MAD
,
394 MLX5_VPORT_ACCESS_METHOD_HCA
,
395 MLX5_VPORT_ACCESS_METHOD_NIC
,
398 static int mlx5_get_vport_access_method(struct ib_device
*ibdev
)
400 if (mlx5_use_mad_ifc(to_mdev(ibdev
)))
401 return MLX5_VPORT_ACCESS_METHOD_MAD
;
403 if (mlx5_ib_port_link_layer(ibdev
, 1) ==
404 IB_LINK_LAYER_ETHERNET
)
405 return MLX5_VPORT_ACCESS_METHOD_NIC
;
407 return MLX5_VPORT_ACCESS_METHOD_HCA
;
410 static void get_atomic_caps(struct mlx5_ib_dev
*dev
,
411 struct ib_device_attr
*props
)
414 u8 atomic_operations
= MLX5_CAP_ATOMIC(dev
->mdev
, atomic_operations
);
415 u8 atomic_size_qp
= MLX5_CAP_ATOMIC(dev
->mdev
, atomic_size_qp
);
416 u8 atomic_req_8B_endianness_mode
=
417 MLX5_CAP_ATOMIC(dev
->mdev
, atomic_req_8B_endianness_mode
);
419 /* Check if HW supports 8 bytes standard atomic operations and capable
420 * of host endianness respond
422 tmp
= MLX5_ATOMIC_OPS_CMP_SWAP
| MLX5_ATOMIC_OPS_FETCH_ADD
;
423 if (((atomic_operations
& tmp
) == tmp
) &&
424 (atomic_size_qp
& MLX5_ATOMIC_SIZE_QP_8BYTES
) &&
425 (atomic_req_8B_endianness_mode
)) {
426 props
->atomic_cap
= IB_ATOMIC_HCA
;
428 props
->atomic_cap
= IB_ATOMIC_NONE
;
432 static int mlx5_query_system_image_guid(struct ib_device
*ibdev
,
433 __be64
*sys_image_guid
)
435 struct mlx5_ib_dev
*dev
= to_mdev(ibdev
);
436 struct mlx5_core_dev
*mdev
= dev
->mdev
;
440 switch (mlx5_get_vport_access_method(ibdev
)) {
441 case MLX5_VPORT_ACCESS_METHOD_MAD
:
442 return mlx5_query_mad_ifc_system_image_guid(ibdev
,
445 case MLX5_VPORT_ACCESS_METHOD_HCA
:
446 err
= mlx5_query_hca_vport_system_image_guid(mdev
, &tmp
);
449 case MLX5_VPORT_ACCESS_METHOD_NIC
:
450 err
= mlx5_query_nic_vport_system_image_guid(mdev
, &tmp
);
458 *sys_image_guid
= cpu_to_be64(tmp
);
464 static int mlx5_query_max_pkeys(struct ib_device
*ibdev
,
467 struct mlx5_ib_dev
*dev
= to_mdev(ibdev
);
468 struct mlx5_core_dev
*mdev
= dev
->mdev
;
470 switch (mlx5_get_vport_access_method(ibdev
)) {
471 case MLX5_VPORT_ACCESS_METHOD_MAD
:
472 return mlx5_query_mad_ifc_max_pkeys(ibdev
, max_pkeys
);
474 case MLX5_VPORT_ACCESS_METHOD_HCA
:
475 case MLX5_VPORT_ACCESS_METHOD_NIC
:
476 *max_pkeys
= mlx5_to_sw_pkey_sz(MLX5_CAP_GEN(mdev
,
485 static int mlx5_query_vendor_id(struct ib_device
*ibdev
,
488 struct mlx5_ib_dev
*dev
= to_mdev(ibdev
);
490 switch (mlx5_get_vport_access_method(ibdev
)) {
491 case MLX5_VPORT_ACCESS_METHOD_MAD
:
492 return mlx5_query_mad_ifc_vendor_id(ibdev
, vendor_id
);
494 case MLX5_VPORT_ACCESS_METHOD_HCA
:
495 case MLX5_VPORT_ACCESS_METHOD_NIC
:
496 return mlx5_core_query_vendor_id(dev
->mdev
, vendor_id
);
503 static int mlx5_query_node_guid(struct mlx5_ib_dev
*dev
,
509 switch (mlx5_get_vport_access_method(&dev
->ib_dev
)) {
510 case MLX5_VPORT_ACCESS_METHOD_MAD
:
511 return mlx5_query_mad_ifc_node_guid(dev
, node_guid
);
513 case MLX5_VPORT_ACCESS_METHOD_HCA
:
514 err
= mlx5_query_hca_vport_node_guid(dev
->mdev
, &tmp
);
517 case MLX5_VPORT_ACCESS_METHOD_NIC
:
518 err
= mlx5_query_nic_vport_node_guid(dev
->mdev
, &tmp
);
526 *node_guid
= cpu_to_be64(tmp
);
531 struct mlx5_reg_node_desc
{
532 u8 desc
[IB_DEVICE_NODE_DESC_MAX
];
535 static int mlx5_query_node_desc(struct mlx5_ib_dev
*dev
, char *node_desc
)
537 struct mlx5_reg_node_desc in
;
539 if (mlx5_use_mad_ifc(dev
))
540 return mlx5_query_mad_ifc_node_desc(dev
, node_desc
);
542 memset(&in
, 0, sizeof(in
));
544 return mlx5_core_access_reg(dev
->mdev
, &in
, sizeof(in
), node_desc
,
545 sizeof(struct mlx5_reg_node_desc
),
546 MLX5_REG_NODE_DESC
, 0, 0);
549 static int mlx5_ib_query_device(struct ib_device
*ibdev
,
550 struct ib_device_attr
*props
,
551 struct ib_udata
*uhw
)
553 struct mlx5_ib_dev
*dev
= to_mdev(ibdev
);
554 struct mlx5_core_dev
*mdev
= dev
->mdev
;
559 u64 min_page_size
= 1ull << MLX5_CAP_GEN(mdev
, log_pg_sz
);
560 struct mlx5_ib_query_device_resp resp
= {};
564 resp_len
= sizeof(resp
.comp_mask
) + sizeof(resp
.response_length
);
565 if (uhw
->outlen
&& uhw
->outlen
< resp_len
)
568 resp
.response_length
= resp_len
;
570 if (uhw
->inlen
&& !ib_is_udata_cleared(uhw
, 0, uhw
->inlen
))
573 memset(props
, 0, sizeof(*props
));
574 err
= mlx5_query_system_image_guid(ibdev
,
575 &props
->sys_image_guid
);
579 err
= mlx5_query_max_pkeys(ibdev
, &props
->max_pkeys
);
583 err
= mlx5_query_vendor_id(ibdev
, &props
->vendor_id
);
587 props
->fw_ver
= ((u64
)fw_rev_maj(dev
->mdev
) << 32) |
588 (fw_rev_min(dev
->mdev
) << 16) |
589 fw_rev_sub(dev
->mdev
);
590 props
->device_cap_flags
= IB_DEVICE_CHANGE_PHY_PORT
|
591 IB_DEVICE_PORT_ACTIVE_EVENT
|
592 IB_DEVICE_SYS_IMAGE_GUID
|
593 IB_DEVICE_RC_RNR_NAK_GEN
;
595 if (MLX5_CAP_GEN(mdev
, pkv
))
596 props
->device_cap_flags
|= IB_DEVICE_BAD_PKEY_CNTR
;
597 if (MLX5_CAP_GEN(mdev
, qkv
))
598 props
->device_cap_flags
|= IB_DEVICE_BAD_QKEY_CNTR
;
599 if (MLX5_CAP_GEN(mdev
, apm
))
600 props
->device_cap_flags
|= IB_DEVICE_AUTO_PATH_MIG
;
601 if (MLX5_CAP_GEN(mdev
, xrc
))
602 props
->device_cap_flags
|= IB_DEVICE_XRC
;
603 if (MLX5_CAP_GEN(mdev
, imaicl
)) {
604 props
->device_cap_flags
|= IB_DEVICE_MEM_WINDOW
|
605 IB_DEVICE_MEM_WINDOW_TYPE_2B
;
606 props
->max_mw
= 1 << MLX5_CAP_GEN(mdev
, log_max_mkey
);
607 /* We support 'Gappy' memory registration too */
608 props
->device_cap_flags
|= IB_DEVICE_SG_GAPS_REG
;
610 props
->device_cap_flags
|= IB_DEVICE_MEM_MGT_EXTENSIONS
;
611 if (MLX5_CAP_GEN(mdev
, sho
)) {
612 props
->device_cap_flags
|= IB_DEVICE_SIGNATURE_HANDOVER
;
613 /* At this stage no support for signature handover */
614 props
->sig_prot_cap
= IB_PROT_T10DIF_TYPE_1
|
615 IB_PROT_T10DIF_TYPE_2
|
616 IB_PROT_T10DIF_TYPE_3
;
617 props
->sig_guard_cap
= IB_GUARD_T10DIF_CRC
|
618 IB_GUARD_T10DIF_CSUM
;
620 if (MLX5_CAP_GEN(mdev
, block_lb_mc
))
621 props
->device_cap_flags
|= IB_DEVICE_BLOCK_MULTICAST_LOOPBACK
;
623 if (MLX5_CAP_GEN(dev
->mdev
, eth_net_offloads
)) {
624 if (MLX5_CAP_ETH(mdev
, csum_cap
)) {
625 /* Legacy bit to support old userspace libraries */
626 props
->device_cap_flags
|= IB_DEVICE_RAW_IP_CSUM
;
627 props
->raw_packet_caps
|= IB_RAW_PACKET_CAP_IP_CSUM
;
630 if (MLX5_CAP_ETH(dev
->mdev
, vlan_cap
))
631 props
->raw_packet_caps
|=
632 IB_RAW_PACKET_CAP_CVLAN_STRIPPING
;
634 if (field_avail(typeof(resp
), tso_caps
, uhw
->outlen
)) {
635 max_tso
= MLX5_CAP_ETH(mdev
, max_lso_cap
);
637 resp
.tso_caps
.max_tso
= 1 << max_tso
;
638 resp
.tso_caps
.supported_qpts
|=
639 1 << IB_QPT_RAW_PACKET
;
640 resp
.response_length
+= sizeof(resp
.tso_caps
);
644 if (field_avail(typeof(resp
), rss_caps
, uhw
->outlen
)) {
645 resp
.rss_caps
.rx_hash_function
=
646 MLX5_RX_HASH_FUNC_TOEPLITZ
;
647 resp
.rss_caps
.rx_hash_fields_mask
=
648 MLX5_RX_HASH_SRC_IPV4
|
649 MLX5_RX_HASH_DST_IPV4
|
650 MLX5_RX_HASH_SRC_IPV6
|
651 MLX5_RX_HASH_DST_IPV6
|
652 MLX5_RX_HASH_SRC_PORT_TCP
|
653 MLX5_RX_HASH_DST_PORT_TCP
|
654 MLX5_RX_HASH_SRC_PORT_UDP
|
655 MLX5_RX_HASH_DST_PORT_UDP
;
656 resp
.response_length
+= sizeof(resp
.rss_caps
);
659 if (field_avail(typeof(resp
), tso_caps
, uhw
->outlen
))
660 resp
.response_length
+= sizeof(resp
.tso_caps
);
661 if (field_avail(typeof(resp
), rss_caps
, uhw
->outlen
))
662 resp
.response_length
+= sizeof(resp
.rss_caps
);
665 if (MLX5_CAP_GEN(mdev
, ipoib_basic_offloads
)) {
666 props
->device_cap_flags
|= IB_DEVICE_UD_IP_CSUM
;
667 props
->device_cap_flags
|= IB_DEVICE_UD_TSO
;
670 if (MLX5_CAP_GEN(dev
->mdev
, eth_net_offloads
) &&
671 MLX5_CAP_ETH(dev
->mdev
, scatter_fcs
)) {
672 /* Legacy bit to support old userspace libraries */
673 props
->device_cap_flags
|= IB_DEVICE_RAW_SCATTER_FCS
;
674 props
->raw_packet_caps
|= IB_RAW_PACKET_CAP_SCATTER_FCS
;
677 if (mlx5_get_flow_namespace(dev
->mdev
, MLX5_FLOW_NAMESPACE_BYPASS
))
678 props
->device_cap_flags
|= IB_DEVICE_MANAGED_FLOW_STEERING
;
680 props
->vendor_part_id
= mdev
->pdev
->device
;
681 props
->hw_ver
= mdev
->pdev
->revision
;
683 props
->max_mr_size
= ~0ull;
684 props
->page_size_cap
= ~(min_page_size
- 1);
685 props
->max_qp
= 1 << MLX5_CAP_GEN(mdev
, log_max_qp
);
686 props
->max_qp_wr
= 1 << MLX5_CAP_GEN(mdev
, log_max_qp_sz
);
687 max_rq_sg
= MLX5_CAP_GEN(mdev
, max_wqe_sz_rq
) /
688 sizeof(struct mlx5_wqe_data_seg
);
689 max_sq_desc
= min_t(int, MLX5_CAP_GEN(mdev
, max_wqe_sz_sq
), 512);
690 max_sq_sg
= (max_sq_desc
- sizeof(struct mlx5_wqe_ctrl_seg
) -
691 sizeof(struct mlx5_wqe_raddr_seg
)) /
692 sizeof(struct mlx5_wqe_data_seg
);
693 props
->max_sge
= min(max_rq_sg
, max_sq_sg
);
694 props
->max_sge_rd
= MLX5_MAX_SGE_RD
;
695 props
->max_cq
= 1 << MLX5_CAP_GEN(mdev
, log_max_cq
);
696 props
->max_cqe
= (1 << MLX5_CAP_GEN(mdev
, log_max_cq_sz
)) - 1;
697 props
->max_mr
= 1 << MLX5_CAP_GEN(mdev
, log_max_mkey
);
698 props
->max_pd
= 1 << MLX5_CAP_GEN(mdev
, log_max_pd
);
699 props
->max_qp_rd_atom
= 1 << MLX5_CAP_GEN(mdev
, log_max_ra_req_qp
);
700 props
->max_qp_init_rd_atom
= 1 << MLX5_CAP_GEN(mdev
, log_max_ra_res_qp
);
701 props
->max_srq
= 1 << MLX5_CAP_GEN(mdev
, log_max_srq
);
702 props
->max_srq_wr
= (1 << MLX5_CAP_GEN(mdev
, log_max_srq_sz
)) - 1;
703 props
->local_ca_ack_delay
= MLX5_CAP_GEN(mdev
, local_ca_ack_delay
);
704 props
->max_res_rd_atom
= props
->max_qp_rd_atom
* props
->max_qp
;
705 props
->max_srq_sge
= max_rq_sg
- 1;
706 props
->max_fast_reg_page_list_len
=
707 1 << MLX5_CAP_GEN(mdev
, log_max_klm_list_size
);
708 get_atomic_caps(dev
, props
);
709 props
->masked_atomic_cap
= IB_ATOMIC_NONE
;
710 props
->max_mcast_grp
= 1 << MLX5_CAP_GEN(mdev
, log_max_mcg
);
711 props
->max_mcast_qp_attach
= MLX5_CAP_GEN(mdev
, max_qp_mcg
);
712 props
->max_total_mcast_qp_attach
= props
->max_mcast_qp_attach
*
713 props
->max_mcast_grp
;
714 props
->max_map_per_fmr
= INT_MAX
; /* no limit in ConnectIB */
715 props
->max_ah
= INT_MAX
;
716 props
->hca_core_clock
= MLX5_CAP_GEN(mdev
, device_frequency_khz
);
717 props
->timestamp_mask
= 0x7FFFFFFFFFFFFFFFULL
;
719 #ifdef CONFIG_INFINIBAND_ON_DEMAND_PAGING
720 if (MLX5_CAP_GEN(mdev
, pg
))
721 props
->device_cap_flags
|= IB_DEVICE_ON_DEMAND_PAGING
;
722 props
->odp_caps
= dev
->odp_caps
;
725 if (MLX5_CAP_GEN(mdev
, cd
))
726 props
->device_cap_flags
|= IB_DEVICE_CROSS_CHANNEL
;
728 if (!mlx5_core_is_pf(mdev
))
729 props
->device_cap_flags
|= IB_DEVICE_VIRTUAL_FUNCTION
;
731 if (mlx5_ib_port_link_layer(ibdev
, 1) ==
732 IB_LINK_LAYER_ETHERNET
) {
733 props
->rss_caps
.max_rwq_indirection_tables
=
734 1 << MLX5_CAP_GEN(dev
->mdev
, log_max_rqt
);
735 props
->rss_caps
.max_rwq_indirection_table_size
=
736 1 << MLX5_CAP_GEN(dev
->mdev
, log_max_rqt_size
);
737 props
->rss_caps
.supported_qpts
= 1 << IB_QPT_RAW_PACKET
;
738 props
->max_wq_type_rq
=
739 1 << MLX5_CAP_GEN(dev
->mdev
, log_max_rq
);
742 if (field_avail(typeof(resp
), cqe_comp_caps
, uhw
->outlen
)) {
743 resp
.cqe_comp_caps
.max_num
=
744 MLX5_CAP_GEN(dev
->mdev
, cqe_compression
) ?
745 MLX5_CAP_GEN(dev
->mdev
, cqe_compression_max_num
) : 0;
746 resp
.cqe_comp_caps
.supported_format
=
747 MLX5_IB_CQE_RES_FORMAT_HASH
|
748 MLX5_IB_CQE_RES_FORMAT_CSUM
;
749 resp
.response_length
+= sizeof(resp
.cqe_comp_caps
);
752 if (field_avail(typeof(resp
), packet_pacing_caps
, uhw
->outlen
)) {
753 if (MLX5_CAP_QOS(mdev
, packet_pacing
) &&
754 MLX5_CAP_GEN(mdev
, qos
)) {
755 resp
.packet_pacing_caps
.qp_rate_limit_max
=
756 MLX5_CAP_QOS(mdev
, packet_pacing_max_rate
);
757 resp
.packet_pacing_caps
.qp_rate_limit_min
=
758 MLX5_CAP_QOS(mdev
, packet_pacing_min_rate
);
759 resp
.packet_pacing_caps
.supported_qpts
|=
760 1 << IB_QPT_RAW_PACKET
;
762 resp
.response_length
+= sizeof(resp
.packet_pacing_caps
);
765 if (field_avail(typeof(resp
), mlx5_ib_support_multi_pkt_send_wqes
,
767 resp
.mlx5_ib_support_multi_pkt_send_wqes
=
768 MLX5_CAP_ETH(mdev
, multi_pkt_send_wqe
);
769 resp
.response_length
+=
770 sizeof(resp
.mlx5_ib_support_multi_pkt_send_wqes
);
773 if (field_avail(typeof(resp
), reserved
, uhw
->outlen
))
774 resp
.response_length
+= sizeof(resp
.reserved
);
777 err
= ib_copy_to_udata(uhw
, &resp
, resp
.response_length
);
787 MLX5_IB_WIDTH_1X
= 1 << 0,
788 MLX5_IB_WIDTH_2X
= 1 << 1,
789 MLX5_IB_WIDTH_4X
= 1 << 2,
790 MLX5_IB_WIDTH_8X
= 1 << 3,
791 MLX5_IB_WIDTH_12X
= 1 << 4
794 static int translate_active_width(struct ib_device
*ibdev
, u8 active_width
,
797 struct mlx5_ib_dev
*dev
= to_mdev(ibdev
);
800 if (active_width
& MLX5_IB_WIDTH_1X
) {
801 *ib_width
= IB_WIDTH_1X
;
802 } else if (active_width
& MLX5_IB_WIDTH_2X
) {
803 mlx5_ib_dbg(dev
, "active_width %d is not supported by IB spec\n",
806 } else if (active_width
& MLX5_IB_WIDTH_4X
) {
807 *ib_width
= IB_WIDTH_4X
;
808 } else if (active_width
& MLX5_IB_WIDTH_8X
) {
809 *ib_width
= IB_WIDTH_8X
;
810 } else if (active_width
& MLX5_IB_WIDTH_12X
) {
811 *ib_width
= IB_WIDTH_12X
;
813 mlx5_ib_dbg(dev
, "Invalid active_width %d\n",
821 static int mlx5_mtu_to_ib_mtu(int mtu
)
830 pr_warn("invalid mtu\n");
840 __IB_MAX_VL_0_14
= 5,
843 enum mlx5_vl_hw_cap
{
855 static int translate_max_vl_num(struct ib_device
*ibdev
, u8 vl_hw_cap
,
860 *max_vl_num
= __IB_MAX_VL_0
;
863 *max_vl_num
= __IB_MAX_VL_0_1
;
866 *max_vl_num
= __IB_MAX_VL_0_3
;
869 *max_vl_num
= __IB_MAX_VL_0_7
;
871 case MLX5_VL_HW_0_14
:
872 *max_vl_num
= __IB_MAX_VL_0_14
;
882 static int mlx5_query_hca_port(struct ib_device
*ibdev
, u8 port
,
883 struct ib_port_attr
*props
)
885 struct mlx5_ib_dev
*dev
= to_mdev(ibdev
);
886 struct mlx5_core_dev
*mdev
= dev
->mdev
;
887 struct mlx5_hca_vport_context
*rep
;
891 u8 ib_link_width_oper
;
894 rep
= kzalloc(sizeof(*rep
), GFP_KERNEL
);
900 /* props being zeroed by the caller, avoid zeroing it here */
902 err
= mlx5_query_hca_vport_context(mdev
, 0, port
, 0, rep
);
906 props
->lid
= rep
->lid
;
907 props
->lmc
= rep
->lmc
;
908 props
->sm_lid
= rep
->sm_lid
;
909 props
->sm_sl
= rep
->sm_sl
;
910 props
->state
= rep
->vport_state
;
911 props
->phys_state
= rep
->port_physical_state
;
912 props
->port_cap_flags
= rep
->cap_mask1
;
913 props
->gid_tbl_len
= mlx5_get_gid_table_len(MLX5_CAP_GEN(mdev
, gid_table_size
));
914 props
->max_msg_sz
= 1 << MLX5_CAP_GEN(mdev
, log_max_msg
);
915 props
->pkey_tbl_len
= mlx5_to_sw_pkey_sz(MLX5_CAP_GEN(mdev
, pkey_table_size
));
916 props
->bad_pkey_cntr
= rep
->pkey_violation_counter
;
917 props
->qkey_viol_cntr
= rep
->qkey_violation_counter
;
918 props
->subnet_timeout
= rep
->subnet_timeout
;
919 props
->init_type_reply
= rep
->init_type_reply
;
920 props
->grh_required
= rep
->grh_required
;
922 err
= mlx5_query_port_link_width_oper(mdev
, &ib_link_width_oper
, port
);
926 err
= translate_active_width(ibdev
, ib_link_width_oper
,
927 &props
->active_width
);
930 err
= mlx5_query_port_ib_proto_oper(mdev
, &props
->active_speed
, port
);
934 mlx5_query_port_max_mtu(mdev
, &max_mtu
, port
);
936 props
->max_mtu
= mlx5_mtu_to_ib_mtu(max_mtu
);
938 mlx5_query_port_oper_mtu(mdev
, &oper_mtu
, port
);
940 props
->active_mtu
= mlx5_mtu_to_ib_mtu(oper_mtu
);
942 err
= mlx5_query_port_vl_hw_cap(mdev
, &vl_hw_cap
, port
);
946 err
= translate_max_vl_num(ibdev
, vl_hw_cap
,
953 int mlx5_ib_query_port(struct ib_device
*ibdev
, u8 port
,
954 struct ib_port_attr
*props
)
959 switch (mlx5_get_vport_access_method(ibdev
)) {
960 case MLX5_VPORT_ACCESS_METHOD_MAD
:
961 ret
= mlx5_query_mad_ifc_port(ibdev
, port
, props
);
964 case MLX5_VPORT_ACCESS_METHOD_HCA
:
965 ret
= mlx5_query_hca_port(ibdev
, port
, props
);
968 case MLX5_VPORT_ACCESS_METHOD_NIC
:
969 ret
= mlx5_query_port_roce(ibdev
, port
, props
);
977 count
= mlx5_core_reserved_gids_count(to_mdev(ibdev
)->mdev
);
978 props
->gid_tbl_len
-= count
;
983 static int mlx5_ib_query_gid(struct ib_device
*ibdev
, u8 port
, int index
,
986 struct mlx5_ib_dev
*dev
= to_mdev(ibdev
);
987 struct mlx5_core_dev
*mdev
= dev
->mdev
;
989 switch (mlx5_get_vport_access_method(ibdev
)) {
990 case MLX5_VPORT_ACCESS_METHOD_MAD
:
991 return mlx5_query_mad_ifc_gids(ibdev
, port
, index
, gid
);
993 case MLX5_VPORT_ACCESS_METHOD_HCA
:
994 return mlx5_query_hca_vport_gid(mdev
, 0, port
, 0, index
, gid
);
1002 static int mlx5_ib_query_pkey(struct ib_device
*ibdev
, u8 port
, u16 index
,
1005 struct mlx5_ib_dev
*dev
= to_mdev(ibdev
);
1006 struct mlx5_core_dev
*mdev
= dev
->mdev
;
1008 switch (mlx5_get_vport_access_method(ibdev
)) {
1009 case MLX5_VPORT_ACCESS_METHOD_MAD
:
1010 return mlx5_query_mad_ifc_pkey(ibdev
, port
, index
, pkey
);
1012 case MLX5_VPORT_ACCESS_METHOD_HCA
:
1013 case MLX5_VPORT_ACCESS_METHOD_NIC
:
1014 return mlx5_query_hca_vport_pkey(mdev
, 0, port
, 0, index
,
1021 static int mlx5_ib_modify_device(struct ib_device
*ibdev
, int mask
,
1022 struct ib_device_modify
*props
)
1024 struct mlx5_ib_dev
*dev
= to_mdev(ibdev
);
1025 struct mlx5_reg_node_desc in
;
1026 struct mlx5_reg_node_desc out
;
1029 if (mask
& ~IB_DEVICE_MODIFY_NODE_DESC
)
1032 if (!(mask
& IB_DEVICE_MODIFY_NODE_DESC
))
1036 * If possible, pass node desc to FW, so it can generate
1037 * a 144 trap. If cmd fails, just ignore.
1039 memcpy(&in
, props
->node_desc
, IB_DEVICE_NODE_DESC_MAX
);
1040 err
= mlx5_core_access_reg(dev
->mdev
, &in
, sizeof(in
), &out
,
1041 sizeof(out
), MLX5_REG_NODE_DESC
, 0, 1);
1045 memcpy(ibdev
->node_desc
, props
->node_desc
, IB_DEVICE_NODE_DESC_MAX
);
1050 static int set_port_caps_atomic(struct mlx5_ib_dev
*dev
, u8 port_num
, u32 mask
,
1053 struct mlx5_hca_vport_context ctx
= {};
1056 err
= mlx5_query_hca_vport_context(dev
->mdev
, 0,
1061 if (~ctx
.cap_mask1_perm
& mask
) {
1062 mlx5_ib_warn(dev
, "trying to change bitmask 0x%X but change supported 0x%X\n",
1063 mask
, ctx
.cap_mask1_perm
);
1067 ctx
.cap_mask1
= value
;
1068 ctx
.cap_mask1_perm
= mask
;
1069 err
= mlx5_core_modify_hca_vport_context(dev
->mdev
, 0,
1075 static int mlx5_ib_modify_port(struct ib_device
*ibdev
, u8 port
, int mask
,
1076 struct ib_port_modify
*props
)
1078 struct mlx5_ib_dev
*dev
= to_mdev(ibdev
);
1079 struct ib_port_attr attr
;
1084 bool is_ib
= (mlx5_ib_port_link_layer(ibdev
, port
) ==
1085 IB_LINK_LAYER_INFINIBAND
);
1087 if (MLX5_CAP_GEN(dev
->mdev
, ib_virt
) && is_ib
) {
1088 change_mask
= props
->clr_port_cap_mask
| props
->set_port_cap_mask
;
1089 value
= ~props
->clr_port_cap_mask
| props
->set_port_cap_mask
;
1090 return set_port_caps_atomic(dev
, port
, change_mask
, value
);
1093 mutex_lock(&dev
->cap_mask_mutex
);
1095 err
= ib_query_port(ibdev
, port
, &attr
);
1099 tmp
= (attr
.port_cap_flags
| props
->set_port_cap_mask
) &
1100 ~props
->clr_port_cap_mask
;
1102 err
= mlx5_set_port_caps(dev
->mdev
, port
, tmp
);
1105 mutex_unlock(&dev
->cap_mask_mutex
);
1109 static void print_lib_caps(struct mlx5_ib_dev
*dev
, u64 caps
)
1111 mlx5_ib_dbg(dev
, "MLX5_LIB_CAP_4K_UAR = %s\n",
1112 caps
& MLX5_LIB_CAP_4K_UAR
? "y" : "n");
1115 static int calc_total_bfregs(struct mlx5_ib_dev
*dev
, bool lib_uar_4k
,
1116 struct mlx5_ib_alloc_ucontext_req_v2
*req
,
1119 int uars_per_sys_page
;
1120 int bfregs_per_sys_page
;
1121 int ref_bfregs
= req
->total_num_bfregs
;
1123 if (req
->total_num_bfregs
== 0)
1126 BUILD_BUG_ON(MLX5_MAX_BFREGS
% MLX5_NON_FP_BFREGS_IN_PAGE
);
1127 BUILD_BUG_ON(MLX5_MAX_BFREGS
< MLX5_NON_FP_BFREGS_IN_PAGE
);
1129 if (req
->total_num_bfregs
> MLX5_MAX_BFREGS
)
1132 uars_per_sys_page
= get_uars_per_sys_page(dev
, lib_uar_4k
);
1133 bfregs_per_sys_page
= uars_per_sys_page
* MLX5_NON_FP_BFREGS_PER_UAR
;
1134 req
->total_num_bfregs
= ALIGN(req
->total_num_bfregs
, bfregs_per_sys_page
);
1135 *num_sys_pages
= req
->total_num_bfregs
/ bfregs_per_sys_page
;
1137 if (req
->num_low_latency_bfregs
> req
->total_num_bfregs
- 1)
1140 mlx5_ib_dbg(dev
, "uar_4k: fw support %s, lib support %s, user requested %d bfregs, alloated %d, using %d sys pages\n",
1141 MLX5_CAP_GEN(dev
->mdev
, uar_4k
) ? "yes" : "no",
1142 lib_uar_4k
? "yes" : "no", ref_bfregs
,
1143 req
->total_num_bfregs
, *num_sys_pages
);
1148 static int allocate_uars(struct mlx5_ib_dev
*dev
, struct mlx5_ib_ucontext
*context
)
1150 struct mlx5_bfreg_info
*bfregi
;
1154 bfregi
= &context
->bfregi
;
1155 for (i
= 0; i
< bfregi
->num_sys_pages
; i
++) {
1156 err
= mlx5_cmd_alloc_uar(dev
->mdev
, &bfregi
->sys_pages
[i
]);
1160 mlx5_ib_dbg(dev
, "allocated uar %d\n", bfregi
->sys_pages
[i
]);
1165 for (--i
; i
>= 0; i
--)
1166 if (mlx5_cmd_free_uar(dev
->mdev
, bfregi
->sys_pages
[i
]))
1167 mlx5_ib_warn(dev
, "failed to free uar %d\n", i
);
1172 static int deallocate_uars(struct mlx5_ib_dev
*dev
, struct mlx5_ib_ucontext
*context
)
1174 struct mlx5_bfreg_info
*bfregi
;
1178 bfregi
= &context
->bfregi
;
1179 for (i
= 0; i
< bfregi
->num_sys_pages
; i
++) {
1180 err
= mlx5_cmd_free_uar(dev
->mdev
, bfregi
->sys_pages
[i
]);
1182 mlx5_ib_warn(dev
, "failed to free uar %d\n", i
);
1189 static struct ib_ucontext
*mlx5_ib_alloc_ucontext(struct ib_device
*ibdev
,
1190 struct ib_udata
*udata
)
1192 struct mlx5_ib_dev
*dev
= to_mdev(ibdev
);
1193 struct mlx5_ib_alloc_ucontext_req_v2 req
= {};
1194 struct mlx5_ib_alloc_ucontext_resp resp
= {};
1195 struct mlx5_ib_ucontext
*context
;
1196 struct mlx5_bfreg_info
*bfregi
;
1200 size_t min_req_v2
= offsetof(struct mlx5_ib_alloc_ucontext_req_v2
,
1204 if (!dev
->ib_active
)
1205 return ERR_PTR(-EAGAIN
);
1207 if (udata
->inlen
< sizeof(struct ib_uverbs_cmd_hdr
))
1208 return ERR_PTR(-EINVAL
);
1210 reqlen
= udata
->inlen
- sizeof(struct ib_uverbs_cmd_hdr
);
1211 if (reqlen
== sizeof(struct mlx5_ib_alloc_ucontext_req
))
1213 else if (reqlen
>= min_req_v2
)
1216 return ERR_PTR(-EINVAL
);
1218 err
= ib_copy_from_udata(&req
, udata
, min(reqlen
, sizeof(req
)));
1220 return ERR_PTR(err
);
1223 return ERR_PTR(-EINVAL
);
1225 if (req
.comp_mask
|| req
.reserved0
|| req
.reserved1
|| req
.reserved2
)
1226 return ERR_PTR(-EOPNOTSUPP
);
1228 req
.total_num_bfregs
= ALIGN(req
.total_num_bfregs
,
1229 MLX5_NON_FP_BFREGS_PER_UAR
);
1230 if (req
.num_low_latency_bfregs
> req
.total_num_bfregs
- 1)
1231 return ERR_PTR(-EINVAL
);
1233 resp
.qp_tab_size
= 1 << MLX5_CAP_GEN(dev
->mdev
, log_max_qp
);
1234 if (mlx5_core_is_pf(dev
->mdev
) && MLX5_CAP_GEN(dev
->mdev
, bf
))
1235 resp
.bf_reg_size
= 1 << MLX5_CAP_GEN(dev
->mdev
, log_bf_reg_size
);
1236 resp
.cache_line_size
= cache_line_size();
1237 resp
.max_sq_desc_sz
= MLX5_CAP_GEN(dev
->mdev
, max_wqe_sz_sq
);
1238 resp
.max_rq_desc_sz
= MLX5_CAP_GEN(dev
->mdev
, max_wqe_sz_rq
);
1239 resp
.max_send_wqebb
= 1 << MLX5_CAP_GEN(dev
->mdev
, log_max_qp_sz
);
1240 resp
.max_recv_wr
= 1 << MLX5_CAP_GEN(dev
->mdev
, log_max_qp_sz
);
1241 resp
.max_srq_recv_wr
= 1 << MLX5_CAP_GEN(dev
->mdev
, log_max_srq_sz
);
1242 resp
.cqe_version
= min_t(__u8
,
1243 (__u8
)MLX5_CAP_GEN(dev
->mdev
, cqe_version
),
1244 req
.max_cqe_version
);
1245 resp
.log_uar_size
= MLX5_CAP_GEN(dev
->mdev
, uar_4k
) ?
1246 MLX5_ADAPTER_PAGE_SHIFT
: PAGE_SHIFT
;
1247 resp
.num_uars_per_page
= MLX5_CAP_GEN(dev
->mdev
, uar_4k
) ?
1248 MLX5_CAP_GEN(dev
->mdev
, num_of_uars_per_page
) : 1;
1249 resp
.response_length
= min(offsetof(typeof(resp
), response_length
) +
1250 sizeof(resp
.response_length
), udata
->outlen
);
1252 context
= kzalloc(sizeof(*context
), GFP_KERNEL
);
1254 return ERR_PTR(-ENOMEM
);
1256 lib_uar_4k
= req
.lib_caps
& MLX5_LIB_CAP_4K_UAR
;
1257 bfregi
= &context
->bfregi
;
1259 /* updates req->total_num_bfregs */
1260 err
= calc_total_bfregs(dev
, lib_uar_4k
, &req
, &bfregi
->num_sys_pages
);
1264 mutex_init(&bfregi
->lock
);
1265 bfregi
->lib_uar_4k
= lib_uar_4k
;
1266 bfregi
->count
= kcalloc(req
.total_num_bfregs
, sizeof(*bfregi
->count
),
1268 if (!bfregi
->count
) {
1273 bfregi
->sys_pages
= kcalloc(bfregi
->num_sys_pages
,
1274 sizeof(*bfregi
->sys_pages
),
1276 if (!bfregi
->sys_pages
) {
1281 err
= allocate_uars(dev
, context
);
1285 #ifdef CONFIG_INFINIBAND_ON_DEMAND_PAGING
1286 context
->ibucontext
.invalidate_range
= &mlx5_ib_invalidate_range
;
1289 context
->upd_xlt_page
= __get_free_page(GFP_KERNEL
);
1290 if (!context
->upd_xlt_page
) {
1294 mutex_init(&context
->upd_xlt_page_mutex
);
1296 if (MLX5_CAP_GEN(dev
->mdev
, log_max_transport_domain
)) {
1297 err
= mlx5_core_alloc_transport_domain(dev
->mdev
,
1303 INIT_LIST_HEAD(&context
->vma_private_list
);
1304 INIT_LIST_HEAD(&context
->db_page_list
);
1305 mutex_init(&context
->db_page_mutex
);
1307 resp
.tot_bfregs
= req
.total_num_bfregs
;
1308 resp
.num_ports
= MLX5_CAP_GEN(dev
->mdev
, num_ports
);
1310 if (field_avail(typeof(resp
), cqe_version
, udata
->outlen
))
1311 resp
.response_length
+= sizeof(resp
.cqe_version
);
1313 if (field_avail(typeof(resp
), cmds_supp_uhw
, udata
->outlen
)) {
1314 resp
.cmds_supp_uhw
|= MLX5_USER_CMDS_SUPP_UHW_QUERY_DEVICE
|
1315 MLX5_USER_CMDS_SUPP_UHW_CREATE_AH
;
1316 resp
.response_length
+= sizeof(resp
.cmds_supp_uhw
);
1319 if (field_avail(typeof(resp
), eth_min_inline
, udata
->outlen
)) {
1320 if (mlx5_ib_port_link_layer(ibdev
, 1) == IB_LINK_LAYER_ETHERNET
) {
1321 mlx5_query_min_inline(dev
->mdev
, &resp
.eth_min_inline
);
1322 resp
.eth_min_inline
++;
1324 resp
.response_length
+= sizeof(resp
.eth_min_inline
);
1328 * We don't want to expose information from the PCI bar that is located
1329 * after 4096 bytes, so if the arch only supports larger pages, let's
1330 * pretend we don't support reading the HCA's core clock. This is also
1331 * forced by mmap function.
1333 if (field_avail(typeof(resp
), hca_core_clock_offset
, udata
->outlen
)) {
1334 if (PAGE_SIZE
<= 4096) {
1336 MLX5_IB_ALLOC_UCONTEXT_RESP_MASK_CORE_CLOCK_OFFSET
;
1337 resp
.hca_core_clock_offset
=
1338 offsetof(struct mlx5_init_seg
, internal_timer_h
) % PAGE_SIZE
;
1340 resp
.response_length
+= sizeof(resp
.hca_core_clock_offset
) +
1341 sizeof(resp
.reserved2
);
1344 if (field_avail(typeof(resp
), log_uar_size
, udata
->outlen
))
1345 resp
.response_length
+= sizeof(resp
.log_uar_size
);
1347 if (field_avail(typeof(resp
), num_uars_per_page
, udata
->outlen
))
1348 resp
.response_length
+= sizeof(resp
.num_uars_per_page
);
1350 err
= ib_copy_to_udata(udata
, &resp
, resp
.response_length
);
1355 bfregi
->num_low_latency_bfregs
= req
.num_low_latency_bfregs
;
1356 context
->cqe_version
= resp
.cqe_version
;
1357 context
->lib_caps
= req
.lib_caps
;
1358 print_lib_caps(dev
, context
->lib_caps
);
1360 return &context
->ibucontext
;
1363 if (MLX5_CAP_GEN(dev
->mdev
, log_max_transport_domain
))
1364 mlx5_core_dealloc_transport_domain(dev
->mdev
, context
->tdn
);
1367 free_page(context
->upd_xlt_page
);
1370 deallocate_uars(dev
, context
);
1373 kfree(bfregi
->sys_pages
);
1376 kfree(bfregi
->count
);
1381 return ERR_PTR(err
);
1384 static int mlx5_ib_dealloc_ucontext(struct ib_ucontext
*ibcontext
)
1386 struct mlx5_ib_ucontext
*context
= to_mucontext(ibcontext
);
1387 struct mlx5_ib_dev
*dev
= to_mdev(ibcontext
->device
);
1388 struct mlx5_bfreg_info
*bfregi
;
1390 bfregi
= &context
->bfregi
;
1391 if (MLX5_CAP_GEN(dev
->mdev
, log_max_transport_domain
))
1392 mlx5_core_dealloc_transport_domain(dev
->mdev
, context
->tdn
);
1394 free_page(context
->upd_xlt_page
);
1395 deallocate_uars(dev
, context
);
1396 kfree(bfregi
->sys_pages
);
1397 kfree(bfregi
->count
);
1403 static phys_addr_t
uar_index2pfn(struct mlx5_ib_dev
*dev
,
1404 struct mlx5_bfreg_info
*bfregi
,
1407 int fw_uars_per_page
;
1409 fw_uars_per_page
= MLX5_CAP_GEN(dev
->mdev
, uar_4k
) ? MLX5_UARS_IN_PAGE
: 1;
1411 return (pci_resource_start(dev
->mdev
->pdev
, 0) >> PAGE_SHIFT
) +
1412 bfregi
->sys_pages
[idx
] / fw_uars_per_page
;
1415 static int get_command(unsigned long offset
)
1417 return (offset
>> MLX5_IB_MMAP_CMD_SHIFT
) & MLX5_IB_MMAP_CMD_MASK
;
1420 static int get_arg(unsigned long offset
)
1422 return offset
& ((1 << MLX5_IB_MMAP_CMD_SHIFT
) - 1);
1425 static int get_index(unsigned long offset
)
1427 return get_arg(offset
);
1430 static void mlx5_ib_vma_open(struct vm_area_struct
*area
)
1432 /* vma_open is called when a new VMA is created on top of our VMA. This
1433 * is done through either mremap flow or split_vma (usually due to
1434 * mlock, madvise, munmap, etc.) We do not support a clone of the VMA,
1435 * as this VMA is strongly hardware related. Therefore we set the
1436 * vm_ops of the newly created/cloned VMA to NULL, to prevent it from
1437 * calling us again and trying to do incorrect actions. We assume that
1438 * the original VMA size is exactly a single page, and therefore all
1439 * "splitting" operation will not happen to it.
1441 area
->vm_ops
= NULL
;
1444 static void mlx5_ib_vma_close(struct vm_area_struct
*area
)
1446 struct mlx5_ib_vma_private_data
*mlx5_ib_vma_priv_data
;
1448 /* It's guaranteed that all VMAs opened on a FD are closed before the
1449 * file itself is closed, therefore no sync is needed with the regular
1450 * closing flow. (e.g. mlx5 ib_dealloc_ucontext)
1451 * However need a sync with accessing the vma as part of
1452 * mlx5_ib_disassociate_ucontext.
1453 * The close operation is usually called under mm->mmap_sem except when
1454 * process is exiting.
1455 * The exiting case is handled explicitly as part of
1456 * mlx5_ib_disassociate_ucontext.
1458 mlx5_ib_vma_priv_data
= (struct mlx5_ib_vma_private_data
*)area
->vm_private_data
;
1460 /* setting the vma context pointer to null in the mlx5_ib driver's
1461 * private data, to protect a race condition in
1462 * mlx5_ib_disassociate_ucontext().
1464 mlx5_ib_vma_priv_data
->vma
= NULL
;
1465 list_del(&mlx5_ib_vma_priv_data
->list
);
1466 kfree(mlx5_ib_vma_priv_data
);
1469 static const struct vm_operations_struct mlx5_ib_vm_ops
= {
1470 .open
= mlx5_ib_vma_open
,
1471 .close
= mlx5_ib_vma_close
1474 static int mlx5_ib_set_vma_data(struct vm_area_struct
*vma
,
1475 struct mlx5_ib_ucontext
*ctx
)
1477 struct mlx5_ib_vma_private_data
*vma_prv
;
1478 struct list_head
*vma_head
= &ctx
->vma_private_list
;
1480 vma_prv
= kzalloc(sizeof(*vma_prv
), GFP_KERNEL
);
1485 vma
->vm_private_data
= vma_prv
;
1486 vma
->vm_ops
= &mlx5_ib_vm_ops
;
1488 list_add(&vma_prv
->list
, vma_head
);
1493 static void mlx5_ib_disassociate_ucontext(struct ib_ucontext
*ibcontext
)
1496 struct vm_area_struct
*vma
;
1497 struct mlx5_ib_vma_private_data
*vma_private
, *n
;
1498 struct mlx5_ib_ucontext
*context
= to_mucontext(ibcontext
);
1499 struct task_struct
*owning_process
= NULL
;
1500 struct mm_struct
*owning_mm
= NULL
;
1502 owning_process
= get_pid_task(ibcontext
->tgid
, PIDTYPE_PID
);
1503 if (!owning_process
)
1506 owning_mm
= get_task_mm(owning_process
);
1508 pr_info("no mm, disassociate ucontext is pending task termination\n");
1510 put_task_struct(owning_process
);
1511 usleep_range(1000, 2000);
1512 owning_process
= get_pid_task(ibcontext
->tgid
,
1514 if (!owning_process
||
1515 owning_process
->state
== TASK_DEAD
) {
1516 pr_info("disassociate ucontext done, task was terminated\n");
1517 /* in case task was dead need to release the
1521 put_task_struct(owning_process
);
1527 /* need to protect from a race on closing the vma as part of
1528 * mlx5_ib_vma_close.
1530 down_write(&owning_mm
->mmap_sem
);
1531 list_for_each_entry_safe(vma_private
, n
, &context
->vma_private_list
,
1533 vma
= vma_private
->vma
;
1534 ret
= zap_vma_ptes(vma
, vma
->vm_start
,
1536 WARN_ONCE(ret
, "%s: zap_vma_ptes failed", __func__
);
1537 /* context going to be destroyed, should
1538 * not access ops any more.
1540 vma
->vm_flags
&= ~(VM_SHARED
| VM_MAYSHARE
);
1542 list_del(&vma_private
->list
);
1545 up_write(&owning_mm
->mmap_sem
);
1547 put_task_struct(owning_process
);
1550 static inline char *mmap_cmd2str(enum mlx5_ib_mmap_cmd cmd
)
1553 case MLX5_IB_MMAP_WC_PAGE
:
1555 case MLX5_IB_MMAP_REGULAR_PAGE
:
1556 return "best effort WC";
1557 case MLX5_IB_MMAP_NC_PAGE
:
1564 static int uar_mmap(struct mlx5_ib_dev
*dev
, enum mlx5_ib_mmap_cmd cmd
,
1565 struct vm_area_struct
*vma
,
1566 struct mlx5_ib_ucontext
*context
)
1568 struct mlx5_bfreg_info
*bfregi
= &context
->bfregi
;
1571 phys_addr_t pfn
, pa
;
1575 if (vma
->vm_end
- vma
->vm_start
!= PAGE_SIZE
)
1578 uars_per_page
= get_uars_per_sys_page(dev
, bfregi
->lib_uar_4k
);
1579 idx
= get_index(vma
->vm_pgoff
);
1580 if (idx
% uars_per_page
||
1581 idx
* uars_per_page
>= bfregi
->num_sys_pages
) {
1582 mlx5_ib_warn(dev
, "invalid uar index %lu\n", idx
);
1587 case MLX5_IB_MMAP_WC_PAGE
:
1588 /* Some architectures don't support WC memory */
1589 #if defined(CONFIG_X86)
1592 #elif !(defined(CONFIG_PPC) || (defined(CONFIG_ARM) && defined(CONFIG_MMU)))
1596 case MLX5_IB_MMAP_REGULAR_PAGE
:
1597 /* For MLX5_IB_MMAP_REGULAR_PAGE do the best effort to get WC */
1598 prot
= pgprot_writecombine(vma
->vm_page_prot
);
1600 case MLX5_IB_MMAP_NC_PAGE
:
1601 prot
= pgprot_noncached(vma
->vm_page_prot
);
1607 pfn
= uar_index2pfn(dev
, bfregi
, idx
);
1608 mlx5_ib_dbg(dev
, "uar idx 0x%lx, pfn %pa\n", idx
, &pfn
);
1610 vma
->vm_page_prot
= prot
;
1611 err
= io_remap_pfn_range(vma
, vma
->vm_start
, pfn
,
1612 PAGE_SIZE
, vma
->vm_page_prot
);
1614 mlx5_ib_err(dev
, "io_remap_pfn_range failed with error=%d, vm_start=0x%lx, pfn=%pa, mmap_cmd=%s\n",
1615 err
, vma
->vm_start
, &pfn
, mmap_cmd2str(cmd
));
1619 pa
= pfn
<< PAGE_SHIFT
;
1620 mlx5_ib_dbg(dev
, "mapped %s at 0x%lx, PA %pa\n", mmap_cmd2str(cmd
),
1621 vma
->vm_start
, &pa
);
1623 return mlx5_ib_set_vma_data(vma
, context
);
1626 static int mlx5_ib_mmap(struct ib_ucontext
*ibcontext
, struct vm_area_struct
*vma
)
1628 struct mlx5_ib_ucontext
*context
= to_mucontext(ibcontext
);
1629 struct mlx5_ib_dev
*dev
= to_mdev(ibcontext
->device
);
1630 unsigned long command
;
1633 command
= get_command(vma
->vm_pgoff
);
1635 case MLX5_IB_MMAP_WC_PAGE
:
1636 case MLX5_IB_MMAP_NC_PAGE
:
1637 case MLX5_IB_MMAP_REGULAR_PAGE
:
1638 return uar_mmap(dev
, command
, vma
, context
);
1640 case MLX5_IB_MMAP_GET_CONTIGUOUS_PAGES
:
1643 case MLX5_IB_MMAP_CORE_CLOCK
:
1644 if (vma
->vm_end
- vma
->vm_start
!= PAGE_SIZE
)
1647 if (vma
->vm_flags
& VM_WRITE
)
1650 /* Don't expose to user-space information it shouldn't have */
1651 if (PAGE_SIZE
> 4096)
1654 vma
->vm_page_prot
= pgprot_noncached(vma
->vm_page_prot
);
1655 pfn
= (dev
->mdev
->iseg_base
+
1656 offsetof(struct mlx5_init_seg
, internal_timer_h
)) >>
1658 if (io_remap_pfn_range(vma
, vma
->vm_start
, pfn
,
1659 PAGE_SIZE
, vma
->vm_page_prot
))
1662 mlx5_ib_dbg(dev
, "mapped internal timer at 0x%lx, PA 0x%llx\n",
1664 (unsigned long long)pfn
<< PAGE_SHIFT
);
1674 static struct ib_pd
*mlx5_ib_alloc_pd(struct ib_device
*ibdev
,
1675 struct ib_ucontext
*context
,
1676 struct ib_udata
*udata
)
1678 struct mlx5_ib_alloc_pd_resp resp
;
1679 struct mlx5_ib_pd
*pd
;
1682 pd
= kmalloc(sizeof(*pd
), GFP_KERNEL
);
1684 return ERR_PTR(-ENOMEM
);
1686 err
= mlx5_core_alloc_pd(to_mdev(ibdev
)->mdev
, &pd
->pdn
);
1689 return ERR_PTR(err
);
1694 if (ib_copy_to_udata(udata
, &resp
, sizeof(resp
))) {
1695 mlx5_core_dealloc_pd(to_mdev(ibdev
)->mdev
, pd
->pdn
);
1697 return ERR_PTR(-EFAULT
);
1704 static int mlx5_ib_dealloc_pd(struct ib_pd
*pd
)
1706 struct mlx5_ib_dev
*mdev
= to_mdev(pd
->device
);
1707 struct mlx5_ib_pd
*mpd
= to_mpd(pd
);
1709 mlx5_core_dealloc_pd(mdev
->mdev
, mpd
->pdn
);
1716 MATCH_CRITERIA_ENABLE_OUTER_BIT
,
1717 MATCH_CRITERIA_ENABLE_MISC_BIT
,
1718 MATCH_CRITERIA_ENABLE_INNER_BIT
1721 #define HEADER_IS_ZERO(match_criteria, headers) \
1722 !(memchr_inv(MLX5_ADDR_OF(fte_match_param, match_criteria, headers), \
1723 0, MLX5_FLD_SZ_BYTES(fte_match_param, headers))) \
1725 static u8 get_match_criteria_enable(u32 *match_criteria)
1727 u8 match_criteria_enable
;
1729 match_criteria_enable
=
1730 (!HEADER_IS_ZERO(match_criteria
, outer_headers
)) <<
1731 MATCH_CRITERIA_ENABLE_OUTER_BIT
;
1732 match_criteria_enable
|=
1733 (!HEADER_IS_ZERO(match_criteria
, misc_parameters
)) <<
1734 MATCH_CRITERIA_ENABLE_MISC_BIT
;
1735 match_criteria_enable
|=
1736 (!HEADER_IS_ZERO(match_criteria
, inner_headers
)) <<
1737 MATCH_CRITERIA_ENABLE_INNER_BIT
;
1739 return match_criteria_enable
;
1742 static void set_proto(void *outer_c
, void *outer_v
, u8 mask
, u8 val
)
1744 MLX5_SET(fte_match_set_lyr_2_4
, outer_c
, ip_protocol
, mask
);
1745 MLX5_SET(fte_match_set_lyr_2_4
, outer_v
, ip_protocol
, val
);
1748 static void set_flow_label(void *misc_c
, void *misc_v
, u8 mask
, u8 val
,
1752 MLX5_SET(fte_match_set_misc
,
1753 misc_c
, inner_ipv6_flow_label
, mask
);
1754 MLX5_SET(fte_match_set_misc
,
1755 misc_v
, inner_ipv6_flow_label
, val
);
1757 MLX5_SET(fte_match_set_misc
,
1758 misc_c
, outer_ipv6_flow_label
, mask
);
1759 MLX5_SET(fte_match_set_misc
,
1760 misc_v
, outer_ipv6_flow_label
, val
);
1764 static void set_tos(void *outer_c
, void *outer_v
, u8 mask
, u8 val
)
1766 MLX5_SET(fte_match_set_lyr_2_4
, outer_c
, ip_ecn
, mask
);
1767 MLX5_SET(fte_match_set_lyr_2_4
, outer_v
, ip_ecn
, val
);
1768 MLX5_SET(fte_match_set_lyr_2_4
, outer_c
, ip_dscp
, mask
>> 2);
1769 MLX5_SET(fte_match_set_lyr_2_4
, outer_v
, ip_dscp
, val
>> 2);
1772 #define LAST_ETH_FIELD vlan_tag
1773 #define LAST_IB_FIELD sl
1774 #define LAST_IPV4_FIELD tos
1775 #define LAST_IPV6_FIELD traffic_class
1776 #define LAST_TCP_UDP_FIELD src_port
1777 #define LAST_TUNNEL_FIELD tunnel_id
1778 #define LAST_FLOW_TAG_FIELD tag_id
1779 #define LAST_DROP_FIELD size
1781 /* Field is the last supported field */
1782 #define FIELDS_NOT_SUPPORTED(filter, field)\
1783 memchr_inv((void *)&filter.field +\
1784 sizeof(filter.field), 0,\
1786 offsetof(typeof(filter), field) -\
1787 sizeof(filter.field))
1789 #define IPV4_VERSION 4
1790 #define IPV6_VERSION 6
1791 static int parse_flow_attr(struct mlx5_core_dev
*mdev
, u32
*match_c
,
1792 u32
*match_v
, const union ib_flow_spec
*ib_spec
,
1793 u32
*tag_id
, bool *is_drop
)
1795 void *misc_params_c
= MLX5_ADDR_OF(fte_match_param
, match_c
,
1797 void *misc_params_v
= MLX5_ADDR_OF(fte_match_param
, match_v
,
1803 if (ib_spec
->type
& IB_FLOW_SPEC_INNER
) {
1804 headers_c
= MLX5_ADDR_OF(fte_match_param
, match_c
,
1806 headers_v
= MLX5_ADDR_OF(fte_match_param
, match_v
,
1808 match_ipv
= MLX5_CAP_FLOWTABLE_NIC_RX(mdev
,
1809 ft_field_support
.inner_ip_version
);
1811 headers_c
= MLX5_ADDR_OF(fte_match_param
, match_c
,
1813 headers_v
= MLX5_ADDR_OF(fte_match_param
, match_v
,
1815 match_ipv
= MLX5_CAP_FLOWTABLE_NIC_RX(mdev
,
1816 ft_field_support
.outer_ip_version
);
1819 switch (ib_spec
->type
& ~IB_FLOW_SPEC_INNER
) {
1820 case IB_FLOW_SPEC_ETH
:
1821 if (FIELDS_NOT_SUPPORTED(ib_spec
->eth
.mask
, LAST_ETH_FIELD
))
1824 ether_addr_copy(MLX5_ADDR_OF(fte_match_set_lyr_2_4
, headers_c
,
1826 ib_spec
->eth
.mask
.dst_mac
);
1827 ether_addr_copy(MLX5_ADDR_OF(fte_match_set_lyr_2_4
, headers_v
,
1829 ib_spec
->eth
.val
.dst_mac
);
1831 ether_addr_copy(MLX5_ADDR_OF(fte_match_set_lyr_2_4
, headers_c
,
1833 ib_spec
->eth
.mask
.src_mac
);
1834 ether_addr_copy(MLX5_ADDR_OF(fte_match_set_lyr_2_4
, headers_v
,
1836 ib_spec
->eth
.val
.src_mac
);
1838 if (ib_spec
->eth
.mask
.vlan_tag
) {
1839 MLX5_SET(fte_match_set_lyr_2_4
, headers_c
,
1841 MLX5_SET(fte_match_set_lyr_2_4
, headers_v
,
1844 MLX5_SET(fte_match_set_lyr_2_4
, headers_c
,
1845 first_vid
, ntohs(ib_spec
->eth
.mask
.vlan_tag
));
1846 MLX5_SET(fte_match_set_lyr_2_4
, headers_v
,
1847 first_vid
, ntohs(ib_spec
->eth
.val
.vlan_tag
));
1849 MLX5_SET(fte_match_set_lyr_2_4
, headers_c
,
1851 ntohs(ib_spec
->eth
.mask
.vlan_tag
) >> 12);
1852 MLX5_SET(fte_match_set_lyr_2_4
, headers_v
,
1854 ntohs(ib_spec
->eth
.val
.vlan_tag
) >> 12);
1856 MLX5_SET(fte_match_set_lyr_2_4
, headers_c
,
1858 ntohs(ib_spec
->eth
.mask
.vlan_tag
) >> 13);
1859 MLX5_SET(fte_match_set_lyr_2_4
, headers_v
,
1861 ntohs(ib_spec
->eth
.val
.vlan_tag
) >> 13);
1863 MLX5_SET(fte_match_set_lyr_2_4
, headers_c
,
1864 ethertype
, ntohs(ib_spec
->eth
.mask
.ether_type
));
1865 MLX5_SET(fte_match_set_lyr_2_4
, headers_v
,
1866 ethertype
, ntohs(ib_spec
->eth
.val
.ether_type
));
1868 case IB_FLOW_SPEC_IPV4
:
1869 if (FIELDS_NOT_SUPPORTED(ib_spec
->ipv4
.mask
, LAST_IPV4_FIELD
))
1873 MLX5_SET(fte_match_set_lyr_2_4
, headers_c
,
1875 MLX5_SET(fte_match_set_lyr_2_4
, headers_v
,
1876 ip_version
, IPV4_VERSION
);
1878 MLX5_SET(fte_match_set_lyr_2_4
, headers_c
,
1880 MLX5_SET(fte_match_set_lyr_2_4
, headers_v
,
1881 ethertype
, ETH_P_IP
);
1884 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4
, headers_c
,
1885 src_ipv4_src_ipv6
.ipv4_layout
.ipv4
),
1886 &ib_spec
->ipv4
.mask
.src_ip
,
1887 sizeof(ib_spec
->ipv4
.mask
.src_ip
));
1888 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4
, headers_v
,
1889 src_ipv4_src_ipv6
.ipv4_layout
.ipv4
),
1890 &ib_spec
->ipv4
.val
.src_ip
,
1891 sizeof(ib_spec
->ipv4
.val
.src_ip
));
1892 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4
, headers_c
,
1893 dst_ipv4_dst_ipv6
.ipv4_layout
.ipv4
),
1894 &ib_spec
->ipv4
.mask
.dst_ip
,
1895 sizeof(ib_spec
->ipv4
.mask
.dst_ip
));
1896 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4
, headers_v
,
1897 dst_ipv4_dst_ipv6
.ipv4_layout
.ipv4
),
1898 &ib_spec
->ipv4
.val
.dst_ip
,
1899 sizeof(ib_spec
->ipv4
.val
.dst_ip
));
1901 set_tos(headers_c
, headers_v
,
1902 ib_spec
->ipv4
.mask
.tos
, ib_spec
->ipv4
.val
.tos
);
1904 set_proto(headers_c
, headers_v
,
1905 ib_spec
->ipv4
.mask
.proto
, ib_spec
->ipv4
.val
.proto
);
1907 case IB_FLOW_SPEC_IPV6
:
1908 if (FIELDS_NOT_SUPPORTED(ib_spec
->ipv6
.mask
, LAST_IPV6_FIELD
))
1912 MLX5_SET(fte_match_set_lyr_2_4
, headers_c
,
1914 MLX5_SET(fte_match_set_lyr_2_4
, headers_v
,
1915 ip_version
, IPV6_VERSION
);
1917 MLX5_SET(fte_match_set_lyr_2_4
, headers_c
,
1919 MLX5_SET(fte_match_set_lyr_2_4
, headers_v
,
1920 ethertype
, ETH_P_IPV6
);
1923 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4
, headers_c
,
1924 src_ipv4_src_ipv6
.ipv6_layout
.ipv6
),
1925 &ib_spec
->ipv6
.mask
.src_ip
,
1926 sizeof(ib_spec
->ipv6
.mask
.src_ip
));
1927 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4
, headers_v
,
1928 src_ipv4_src_ipv6
.ipv6_layout
.ipv6
),
1929 &ib_spec
->ipv6
.val
.src_ip
,
1930 sizeof(ib_spec
->ipv6
.val
.src_ip
));
1931 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4
, headers_c
,
1932 dst_ipv4_dst_ipv6
.ipv6_layout
.ipv6
),
1933 &ib_spec
->ipv6
.mask
.dst_ip
,
1934 sizeof(ib_spec
->ipv6
.mask
.dst_ip
));
1935 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4
, headers_v
,
1936 dst_ipv4_dst_ipv6
.ipv6_layout
.ipv6
),
1937 &ib_spec
->ipv6
.val
.dst_ip
,
1938 sizeof(ib_spec
->ipv6
.val
.dst_ip
));
1940 set_tos(headers_c
, headers_v
,
1941 ib_spec
->ipv6
.mask
.traffic_class
,
1942 ib_spec
->ipv6
.val
.traffic_class
);
1944 set_proto(headers_c
, headers_v
,
1945 ib_spec
->ipv6
.mask
.next_hdr
,
1946 ib_spec
->ipv6
.val
.next_hdr
);
1948 set_flow_label(misc_params_c
, misc_params_v
,
1949 ntohl(ib_spec
->ipv6
.mask
.flow_label
),
1950 ntohl(ib_spec
->ipv6
.val
.flow_label
),
1951 ib_spec
->type
& IB_FLOW_SPEC_INNER
);
1954 case IB_FLOW_SPEC_TCP
:
1955 if (FIELDS_NOT_SUPPORTED(ib_spec
->tcp_udp
.mask
,
1956 LAST_TCP_UDP_FIELD
))
1959 MLX5_SET(fte_match_set_lyr_2_4
, headers_c
, ip_protocol
,
1961 MLX5_SET(fte_match_set_lyr_2_4
, headers_v
, ip_protocol
,
1964 MLX5_SET(fte_match_set_lyr_2_4
, headers_c
, tcp_sport
,
1965 ntohs(ib_spec
->tcp_udp
.mask
.src_port
));
1966 MLX5_SET(fte_match_set_lyr_2_4
, headers_v
, tcp_sport
,
1967 ntohs(ib_spec
->tcp_udp
.val
.src_port
));
1969 MLX5_SET(fte_match_set_lyr_2_4
, headers_c
, tcp_dport
,
1970 ntohs(ib_spec
->tcp_udp
.mask
.dst_port
));
1971 MLX5_SET(fte_match_set_lyr_2_4
, headers_v
, tcp_dport
,
1972 ntohs(ib_spec
->tcp_udp
.val
.dst_port
));
1974 case IB_FLOW_SPEC_UDP
:
1975 if (FIELDS_NOT_SUPPORTED(ib_spec
->tcp_udp
.mask
,
1976 LAST_TCP_UDP_FIELD
))
1979 MLX5_SET(fte_match_set_lyr_2_4
, headers_c
, ip_protocol
,
1981 MLX5_SET(fte_match_set_lyr_2_4
, headers_v
, ip_protocol
,
1984 MLX5_SET(fte_match_set_lyr_2_4
, headers_c
, udp_sport
,
1985 ntohs(ib_spec
->tcp_udp
.mask
.src_port
));
1986 MLX5_SET(fte_match_set_lyr_2_4
, headers_v
, udp_sport
,
1987 ntohs(ib_spec
->tcp_udp
.val
.src_port
));
1989 MLX5_SET(fte_match_set_lyr_2_4
, headers_c
, udp_dport
,
1990 ntohs(ib_spec
->tcp_udp
.mask
.dst_port
));
1991 MLX5_SET(fte_match_set_lyr_2_4
, headers_v
, udp_dport
,
1992 ntohs(ib_spec
->tcp_udp
.val
.dst_port
));
1994 case IB_FLOW_SPEC_VXLAN_TUNNEL
:
1995 if (FIELDS_NOT_SUPPORTED(ib_spec
->tunnel
.mask
,
1999 MLX5_SET(fte_match_set_misc
, misc_params_c
, vxlan_vni
,
2000 ntohl(ib_spec
->tunnel
.mask
.tunnel_id
));
2001 MLX5_SET(fte_match_set_misc
, misc_params_v
, vxlan_vni
,
2002 ntohl(ib_spec
->tunnel
.val
.tunnel_id
));
2004 case IB_FLOW_SPEC_ACTION_TAG
:
2005 if (FIELDS_NOT_SUPPORTED(ib_spec
->flow_tag
,
2006 LAST_FLOW_TAG_FIELD
))
2008 if (ib_spec
->flow_tag
.tag_id
>= BIT(24))
2011 *tag_id
= ib_spec
->flow_tag
.tag_id
;
2013 case IB_FLOW_SPEC_ACTION_DROP
:
2014 if (FIELDS_NOT_SUPPORTED(ib_spec
->drop
,
2026 /* If a flow could catch both multicast and unicast packets,
2027 * it won't fall into the multicast flow steering table and this rule
2028 * could steal other multicast packets.
2030 static bool flow_is_multicast_only(struct ib_flow_attr
*ib_attr
)
2032 struct ib_flow_spec_eth
*eth_spec
;
2034 if (ib_attr
->type
!= IB_FLOW_ATTR_NORMAL
||
2035 ib_attr
->size
< sizeof(struct ib_flow_attr
) +
2036 sizeof(struct ib_flow_spec_eth
) ||
2037 ib_attr
->num_of_specs
< 1)
2040 eth_spec
= (struct ib_flow_spec_eth
*)(ib_attr
+ 1);
2041 if (eth_spec
->type
!= IB_FLOW_SPEC_ETH
||
2042 eth_spec
->size
!= sizeof(*eth_spec
))
2045 return is_multicast_ether_addr(eth_spec
->mask
.dst_mac
) &&
2046 is_multicast_ether_addr(eth_spec
->val
.dst_mac
);
2049 static bool is_valid_ethertype(struct mlx5_core_dev
*mdev
,
2050 const struct ib_flow_attr
*flow_attr
,
2053 union ib_flow_spec
*ib_spec
= (union ib_flow_spec
*)(flow_attr
+ 1);
2054 int match_ipv
= check_inner
?
2055 MLX5_CAP_FLOWTABLE_NIC_RX(mdev
,
2056 ft_field_support
.inner_ip_version
) :
2057 MLX5_CAP_FLOWTABLE_NIC_RX(mdev
,
2058 ft_field_support
.outer_ip_version
);
2059 int inner_bit
= check_inner
? IB_FLOW_SPEC_INNER
: 0;
2060 bool ipv4_spec_valid
, ipv6_spec_valid
;
2061 unsigned int ip_spec_type
= 0;
2062 bool has_ethertype
= false;
2063 unsigned int spec_index
;
2064 bool mask_valid
= true;
2068 /* Validate that ethertype is correct */
2069 for (spec_index
= 0; spec_index
< flow_attr
->num_of_specs
; spec_index
++) {
2070 if ((ib_spec
->type
== (IB_FLOW_SPEC_ETH
| inner_bit
)) &&
2071 ib_spec
->eth
.mask
.ether_type
) {
2072 mask_valid
= (ib_spec
->eth
.mask
.ether_type
==
2074 has_ethertype
= true;
2075 eth_type
= ntohs(ib_spec
->eth
.val
.ether_type
);
2076 } else if ((ib_spec
->type
== (IB_FLOW_SPEC_IPV4
| inner_bit
)) ||
2077 (ib_spec
->type
== (IB_FLOW_SPEC_IPV6
| inner_bit
))) {
2078 ip_spec_type
= ib_spec
->type
;
2080 ib_spec
= (void *)ib_spec
+ ib_spec
->size
;
2083 type_valid
= (!has_ethertype
) || (!ip_spec_type
);
2084 if (!type_valid
&& mask_valid
) {
2085 ipv4_spec_valid
= (eth_type
== ETH_P_IP
) &&
2086 (ip_spec_type
== (IB_FLOW_SPEC_IPV4
| inner_bit
));
2087 ipv6_spec_valid
= (eth_type
== ETH_P_IPV6
) &&
2088 (ip_spec_type
== (IB_FLOW_SPEC_IPV6
| inner_bit
));
2090 type_valid
= (ipv4_spec_valid
) || (ipv6_spec_valid
) ||
2091 (((eth_type
== ETH_P_MPLS_UC
) ||
2092 (eth_type
== ETH_P_MPLS_MC
)) && match_ipv
);
2098 static bool is_valid_attr(struct mlx5_core_dev
*mdev
,
2099 const struct ib_flow_attr
*flow_attr
)
2101 return is_valid_ethertype(mdev
, flow_attr
, false) &&
2102 is_valid_ethertype(mdev
, flow_attr
, true);
2105 static void put_flow_table(struct mlx5_ib_dev
*dev
,
2106 struct mlx5_ib_flow_prio
*prio
, bool ft_added
)
2108 prio
->refcount
-= !!ft_added
;
2109 if (!prio
->refcount
) {
2110 mlx5_destroy_flow_table(prio
->flow_table
);
2111 prio
->flow_table
= NULL
;
2115 static int mlx5_ib_destroy_flow(struct ib_flow
*flow_id
)
2117 struct mlx5_ib_dev
*dev
= to_mdev(flow_id
->qp
->device
);
2118 struct mlx5_ib_flow_handler
*handler
= container_of(flow_id
,
2119 struct mlx5_ib_flow_handler
,
2121 struct mlx5_ib_flow_handler
*iter
, *tmp
;
2123 mutex_lock(&dev
->flow_db
.lock
);
2125 list_for_each_entry_safe(iter
, tmp
, &handler
->list
, list
) {
2126 mlx5_del_flow_rules(iter
->rule
);
2127 put_flow_table(dev
, iter
->prio
, true);
2128 list_del(&iter
->list
);
2132 mlx5_del_flow_rules(handler
->rule
);
2133 put_flow_table(dev
, handler
->prio
, true);
2134 mutex_unlock(&dev
->flow_db
.lock
);
2141 static int ib_prio_to_core_prio(unsigned int priority
, bool dont_trap
)
2149 enum flow_table_type
{
2154 #define MLX5_FS_MAX_TYPES 6
2155 #define MLX5_FS_MAX_ENTRIES BIT(16)
2156 static struct mlx5_ib_flow_prio
*get_flow_table(struct mlx5_ib_dev
*dev
,
2157 struct ib_flow_attr
*flow_attr
,
2158 enum flow_table_type ft_type
)
2160 bool dont_trap
= flow_attr
->flags
& IB_FLOW_ATTR_FLAGS_DONT_TRAP
;
2161 struct mlx5_flow_namespace
*ns
= NULL
;
2162 struct mlx5_ib_flow_prio
*prio
;
2163 struct mlx5_flow_table
*ft
;
2170 max_table_size
= BIT(MLX5_CAP_FLOWTABLE_NIC_RX(dev
->mdev
,
2172 if (flow_attr
->type
== IB_FLOW_ATTR_NORMAL
) {
2173 if (flow_is_multicast_only(flow_attr
) &&
2175 priority
= MLX5_IB_FLOW_MCAST_PRIO
;
2177 priority
= ib_prio_to_core_prio(flow_attr
->priority
,
2179 ns
= mlx5_get_flow_namespace(dev
->mdev
,
2180 MLX5_FLOW_NAMESPACE_BYPASS
);
2181 num_entries
= MLX5_FS_MAX_ENTRIES
;
2182 num_groups
= MLX5_FS_MAX_TYPES
;
2183 prio
= &dev
->flow_db
.prios
[priority
];
2184 } else if (flow_attr
->type
== IB_FLOW_ATTR_ALL_DEFAULT
||
2185 flow_attr
->type
== IB_FLOW_ATTR_MC_DEFAULT
) {
2186 ns
= mlx5_get_flow_namespace(dev
->mdev
,
2187 MLX5_FLOW_NAMESPACE_LEFTOVERS
);
2188 build_leftovers_ft_param(&priority
,
2191 prio
= &dev
->flow_db
.prios
[MLX5_IB_FLOW_LEFTOVERS_PRIO
];
2192 } else if (flow_attr
->type
== IB_FLOW_ATTR_SNIFFER
) {
2193 if (!MLX5_CAP_FLOWTABLE(dev
->mdev
,
2194 allow_sniffer_and_nic_rx_shared_tir
))
2195 return ERR_PTR(-ENOTSUPP
);
2197 ns
= mlx5_get_flow_namespace(dev
->mdev
, ft_type
== MLX5_IB_FT_RX
?
2198 MLX5_FLOW_NAMESPACE_SNIFFER_RX
:
2199 MLX5_FLOW_NAMESPACE_SNIFFER_TX
);
2201 prio
= &dev
->flow_db
.sniffer
[ft_type
];
2208 return ERR_PTR(-ENOTSUPP
);
2210 if (num_entries
> max_table_size
)
2211 return ERR_PTR(-ENOMEM
);
2213 ft
= prio
->flow_table
;
2215 ft
= mlx5_create_auto_grouped_flow_table(ns
, priority
,
2222 prio
->flow_table
= ft
;
2228 return err
? ERR_PTR(err
) : prio
;
2231 static struct mlx5_ib_flow_handler
*create_flow_rule(struct mlx5_ib_dev
*dev
,
2232 struct mlx5_ib_flow_prio
*ft_prio
,
2233 const struct ib_flow_attr
*flow_attr
,
2234 struct mlx5_flow_destination
*dst
)
2236 struct mlx5_flow_table
*ft
= ft_prio
->flow_table
;
2237 struct mlx5_ib_flow_handler
*handler
;
2238 struct mlx5_flow_act flow_act
= {0};
2239 struct mlx5_flow_spec
*spec
;
2240 struct mlx5_flow_destination
*rule_dst
= dst
;
2241 const void *ib_flow
= (const void *)flow_attr
+ sizeof(*flow_attr
);
2242 unsigned int spec_index
;
2243 u32 flow_tag
= MLX5_FS_DEFAULT_FLOW_TAG
;
2244 bool is_drop
= false;
2248 if (!is_valid_attr(dev
->mdev
, flow_attr
))
2249 return ERR_PTR(-EINVAL
);
2251 spec
= kvzalloc(sizeof(*spec
), GFP_KERNEL
);
2252 handler
= kzalloc(sizeof(*handler
), GFP_KERNEL
);
2253 if (!handler
|| !spec
) {
2258 INIT_LIST_HEAD(&handler
->list
);
2260 for (spec_index
= 0; spec_index
< flow_attr
->num_of_specs
; spec_index
++) {
2261 err
= parse_flow_attr(dev
->mdev
, spec
->match_criteria
,
2263 ib_flow
, &flow_tag
, &is_drop
);
2267 ib_flow
+= ((union ib_flow_spec
*)ib_flow
)->size
;
2270 spec
->match_criteria_enable
= get_match_criteria_enable(spec
->match_criteria
);
2272 flow_act
.action
= MLX5_FLOW_CONTEXT_ACTION_DROP
;
2276 flow_act
.action
= dst
? MLX5_FLOW_CONTEXT_ACTION_FWD_DEST
:
2277 MLX5_FLOW_CONTEXT_ACTION_FWD_NEXT_PRIO
;
2280 if (flow_tag
!= MLX5_FS_DEFAULT_FLOW_TAG
&&
2281 (flow_attr
->type
== IB_FLOW_ATTR_ALL_DEFAULT
||
2282 flow_attr
->type
== IB_FLOW_ATTR_MC_DEFAULT
)) {
2283 mlx5_ib_warn(dev
, "Flow tag %u and attribute type %x isn't allowed in leftovers\n",
2284 flow_tag
, flow_attr
->type
);
2288 flow_act
.flow_tag
= flow_tag
;
2289 handler
->rule
= mlx5_add_flow_rules(ft
, spec
,
2291 rule_dst
, dest_num
);
2293 if (IS_ERR(handler
->rule
)) {
2294 err
= PTR_ERR(handler
->rule
);
2298 ft_prio
->refcount
++;
2299 handler
->prio
= ft_prio
;
2301 ft_prio
->flow_table
= ft
;
2306 return err
? ERR_PTR(err
) : handler
;
2309 static struct mlx5_ib_flow_handler
*create_dont_trap_rule(struct mlx5_ib_dev
*dev
,
2310 struct mlx5_ib_flow_prio
*ft_prio
,
2311 struct ib_flow_attr
*flow_attr
,
2312 struct mlx5_flow_destination
*dst
)
2314 struct mlx5_ib_flow_handler
*handler_dst
= NULL
;
2315 struct mlx5_ib_flow_handler
*handler
= NULL
;
2317 handler
= create_flow_rule(dev
, ft_prio
, flow_attr
, NULL
);
2318 if (!IS_ERR(handler
)) {
2319 handler_dst
= create_flow_rule(dev
, ft_prio
,
2321 if (IS_ERR(handler_dst
)) {
2322 mlx5_del_flow_rules(handler
->rule
);
2323 ft_prio
->refcount
--;
2325 handler
= handler_dst
;
2327 list_add(&handler_dst
->list
, &handler
->list
);
2338 static struct mlx5_ib_flow_handler
*create_leftovers_rule(struct mlx5_ib_dev
*dev
,
2339 struct mlx5_ib_flow_prio
*ft_prio
,
2340 struct ib_flow_attr
*flow_attr
,
2341 struct mlx5_flow_destination
*dst
)
2343 struct mlx5_ib_flow_handler
*handler_ucast
= NULL
;
2344 struct mlx5_ib_flow_handler
*handler
= NULL
;
2347 struct ib_flow_attr flow_attr
;
2348 struct ib_flow_spec_eth eth_flow
;
2349 } leftovers_specs
[] = {
2353 .size
= sizeof(leftovers_specs
[0])
2356 .type
= IB_FLOW_SPEC_ETH
,
2357 .size
= sizeof(struct ib_flow_spec_eth
),
2358 .mask
= {.dst_mac
= {0x1} },
2359 .val
= {.dst_mac
= {0x1} }
2365 .size
= sizeof(leftovers_specs
[0])
2368 .type
= IB_FLOW_SPEC_ETH
,
2369 .size
= sizeof(struct ib_flow_spec_eth
),
2370 .mask
= {.dst_mac
= {0x1} },
2371 .val
= {.dst_mac
= {} }
2376 handler
= create_flow_rule(dev
, ft_prio
,
2377 &leftovers_specs
[LEFTOVERS_MC
].flow_attr
,
2379 if (!IS_ERR(handler
) &&
2380 flow_attr
->type
== IB_FLOW_ATTR_ALL_DEFAULT
) {
2381 handler_ucast
= create_flow_rule(dev
, ft_prio
,
2382 &leftovers_specs
[LEFTOVERS_UC
].flow_attr
,
2384 if (IS_ERR(handler_ucast
)) {
2385 mlx5_del_flow_rules(handler
->rule
);
2386 ft_prio
->refcount
--;
2388 handler
= handler_ucast
;
2390 list_add(&handler_ucast
->list
, &handler
->list
);
2397 static struct mlx5_ib_flow_handler
*create_sniffer_rule(struct mlx5_ib_dev
*dev
,
2398 struct mlx5_ib_flow_prio
*ft_rx
,
2399 struct mlx5_ib_flow_prio
*ft_tx
,
2400 struct mlx5_flow_destination
*dst
)
2402 struct mlx5_ib_flow_handler
*handler_rx
;
2403 struct mlx5_ib_flow_handler
*handler_tx
;
2405 static const struct ib_flow_attr flow_attr
= {
2407 .size
= sizeof(flow_attr
)
2410 handler_rx
= create_flow_rule(dev
, ft_rx
, &flow_attr
, dst
);
2411 if (IS_ERR(handler_rx
)) {
2412 err
= PTR_ERR(handler_rx
);
2416 handler_tx
= create_flow_rule(dev
, ft_tx
, &flow_attr
, dst
);
2417 if (IS_ERR(handler_tx
)) {
2418 err
= PTR_ERR(handler_tx
);
2422 list_add(&handler_tx
->list
, &handler_rx
->list
);
2427 mlx5_del_flow_rules(handler_rx
->rule
);
2431 return ERR_PTR(err
);
2434 static struct ib_flow
*mlx5_ib_create_flow(struct ib_qp
*qp
,
2435 struct ib_flow_attr
*flow_attr
,
2438 struct mlx5_ib_dev
*dev
= to_mdev(qp
->device
);
2439 struct mlx5_ib_qp
*mqp
= to_mqp(qp
);
2440 struct mlx5_ib_flow_handler
*handler
= NULL
;
2441 struct mlx5_flow_destination
*dst
= NULL
;
2442 struct mlx5_ib_flow_prio
*ft_prio_tx
= NULL
;
2443 struct mlx5_ib_flow_prio
*ft_prio
;
2446 if (flow_attr
->priority
> MLX5_IB_FLOW_LAST_PRIO
)
2447 return ERR_PTR(-ENOMEM
);
2449 if (domain
!= IB_FLOW_DOMAIN_USER
||
2450 flow_attr
->port
> MLX5_CAP_GEN(dev
->mdev
, num_ports
) ||
2451 (flow_attr
->flags
& ~IB_FLOW_ATTR_FLAGS_DONT_TRAP
))
2452 return ERR_PTR(-EINVAL
);
2454 dst
= kzalloc(sizeof(*dst
), GFP_KERNEL
);
2456 return ERR_PTR(-ENOMEM
);
2458 mutex_lock(&dev
->flow_db
.lock
);
2460 ft_prio
= get_flow_table(dev
, flow_attr
, MLX5_IB_FT_RX
);
2461 if (IS_ERR(ft_prio
)) {
2462 err
= PTR_ERR(ft_prio
);
2465 if (flow_attr
->type
== IB_FLOW_ATTR_SNIFFER
) {
2466 ft_prio_tx
= get_flow_table(dev
, flow_attr
, MLX5_IB_FT_TX
);
2467 if (IS_ERR(ft_prio_tx
)) {
2468 err
= PTR_ERR(ft_prio_tx
);
2474 dst
->type
= MLX5_FLOW_DESTINATION_TYPE_TIR
;
2475 if (mqp
->flags
& MLX5_IB_QP_RSS
)
2476 dst
->tir_num
= mqp
->rss_qp
.tirn
;
2478 dst
->tir_num
= mqp
->raw_packet_qp
.rq
.tirn
;
2480 if (flow_attr
->type
== IB_FLOW_ATTR_NORMAL
) {
2481 if (flow_attr
->flags
& IB_FLOW_ATTR_FLAGS_DONT_TRAP
) {
2482 handler
= create_dont_trap_rule(dev
, ft_prio
,
2485 handler
= create_flow_rule(dev
, ft_prio
, flow_attr
,
2488 } else if (flow_attr
->type
== IB_FLOW_ATTR_ALL_DEFAULT
||
2489 flow_attr
->type
== IB_FLOW_ATTR_MC_DEFAULT
) {
2490 handler
= create_leftovers_rule(dev
, ft_prio
, flow_attr
,
2492 } else if (flow_attr
->type
== IB_FLOW_ATTR_SNIFFER
) {
2493 handler
= create_sniffer_rule(dev
, ft_prio
, ft_prio_tx
, dst
);
2499 if (IS_ERR(handler
)) {
2500 err
= PTR_ERR(handler
);
2505 mutex_unlock(&dev
->flow_db
.lock
);
2508 return &handler
->ibflow
;
2511 put_flow_table(dev
, ft_prio
, false);
2513 put_flow_table(dev
, ft_prio_tx
, false);
2515 mutex_unlock(&dev
->flow_db
.lock
);
2518 return ERR_PTR(err
);
2521 static int mlx5_ib_mcg_attach(struct ib_qp
*ibqp
, union ib_gid
*gid
, u16 lid
)
2523 struct mlx5_ib_dev
*dev
= to_mdev(ibqp
->device
);
2526 err
= mlx5_core_attach_mcg(dev
->mdev
, gid
, ibqp
->qp_num
);
2528 mlx5_ib_warn(dev
, "failed attaching QPN 0x%x, MGID %pI6\n",
2529 ibqp
->qp_num
, gid
->raw
);
2534 static int mlx5_ib_mcg_detach(struct ib_qp
*ibqp
, union ib_gid
*gid
, u16 lid
)
2536 struct mlx5_ib_dev
*dev
= to_mdev(ibqp
->device
);
2539 err
= mlx5_core_detach_mcg(dev
->mdev
, gid
, ibqp
->qp_num
);
2541 mlx5_ib_warn(dev
, "failed detaching QPN 0x%x, MGID %pI6\n",
2542 ibqp
->qp_num
, gid
->raw
);
2547 static int init_node_data(struct mlx5_ib_dev
*dev
)
2551 err
= mlx5_query_node_desc(dev
, dev
->ib_dev
.node_desc
);
2555 dev
->mdev
->rev_id
= dev
->mdev
->pdev
->revision
;
2557 return mlx5_query_node_guid(dev
, &dev
->ib_dev
.node_guid
);
2560 static ssize_t
show_fw_pages(struct device
*device
, struct device_attribute
*attr
,
2563 struct mlx5_ib_dev
*dev
=
2564 container_of(device
, struct mlx5_ib_dev
, ib_dev
.dev
);
2566 return sprintf(buf
, "%d\n", dev
->mdev
->priv
.fw_pages
);
2569 static ssize_t
show_reg_pages(struct device
*device
,
2570 struct device_attribute
*attr
, char *buf
)
2572 struct mlx5_ib_dev
*dev
=
2573 container_of(device
, struct mlx5_ib_dev
, ib_dev
.dev
);
2575 return sprintf(buf
, "%d\n", atomic_read(&dev
->mdev
->priv
.reg_pages
));
2578 static ssize_t
show_hca(struct device
*device
, struct device_attribute
*attr
,
2581 struct mlx5_ib_dev
*dev
=
2582 container_of(device
, struct mlx5_ib_dev
, ib_dev
.dev
);
2583 return sprintf(buf
, "MT%d\n", dev
->mdev
->pdev
->device
);
2586 static ssize_t
show_rev(struct device
*device
, struct device_attribute
*attr
,
2589 struct mlx5_ib_dev
*dev
=
2590 container_of(device
, struct mlx5_ib_dev
, ib_dev
.dev
);
2591 return sprintf(buf
, "%x\n", dev
->mdev
->rev_id
);
2594 static ssize_t
show_board(struct device
*device
, struct device_attribute
*attr
,
2597 struct mlx5_ib_dev
*dev
=
2598 container_of(device
, struct mlx5_ib_dev
, ib_dev
.dev
);
2599 return sprintf(buf
, "%.*s\n", MLX5_BOARD_ID_LEN
,
2600 dev
->mdev
->board_id
);
2603 static DEVICE_ATTR(hw_rev
, S_IRUGO
, show_rev
, NULL
);
2604 static DEVICE_ATTR(hca_type
, S_IRUGO
, show_hca
, NULL
);
2605 static DEVICE_ATTR(board_id
, S_IRUGO
, show_board
, NULL
);
2606 static DEVICE_ATTR(fw_pages
, S_IRUGO
, show_fw_pages
, NULL
);
2607 static DEVICE_ATTR(reg_pages
, S_IRUGO
, show_reg_pages
, NULL
);
2609 static struct device_attribute
*mlx5_class_attributes
[] = {
2614 &dev_attr_reg_pages
,
2617 static void pkey_change_handler(struct work_struct
*work
)
2619 struct mlx5_ib_port_resources
*ports
=
2620 container_of(work
, struct mlx5_ib_port_resources
,
2623 mutex_lock(&ports
->devr
->mutex
);
2624 mlx5_ib_gsi_pkey_change(ports
->gsi
);
2625 mutex_unlock(&ports
->devr
->mutex
);
2628 static void mlx5_ib_handle_internal_error(struct mlx5_ib_dev
*ibdev
)
2630 struct mlx5_ib_qp
*mqp
;
2631 struct mlx5_ib_cq
*send_mcq
, *recv_mcq
;
2632 struct mlx5_core_cq
*mcq
;
2633 struct list_head cq_armed_list
;
2634 unsigned long flags_qp
;
2635 unsigned long flags_cq
;
2636 unsigned long flags
;
2638 INIT_LIST_HEAD(&cq_armed_list
);
2640 /* Go over qp list reside on that ibdev, sync with create/destroy qp.*/
2641 spin_lock_irqsave(&ibdev
->reset_flow_resource_lock
, flags
);
2642 list_for_each_entry(mqp
, &ibdev
->qp_list
, qps_list
) {
2643 spin_lock_irqsave(&mqp
->sq
.lock
, flags_qp
);
2644 if (mqp
->sq
.tail
!= mqp
->sq
.head
) {
2645 send_mcq
= to_mcq(mqp
->ibqp
.send_cq
);
2646 spin_lock_irqsave(&send_mcq
->lock
, flags_cq
);
2647 if (send_mcq
->mcq
.comp
&&
2648 mqp
->ibqp
.send_cq
->comp_handler
) {
2649 if (!send_mcq
->mcq
.reset_notify_added
) {
2650 send_mcq
->mcq
.reset_notify_added
= 1;
2651 list_add_tail(&send_mcq
->mcq
.reset_notify
,
2655 spin_unlock_irqrestore(&send_mcq
->lock
, flags_cq
);
2657 spin_unlock_irqrestore(&mqp
->sq
.lock
, flags_qp
);
2658 spin_lock_irqsave(&mqp
->rq
.lock
, flags_qp
);
2659 /* no handling is needed for SRQ */
2660 if (!mqp
->ibqp
.srq
) {
2661 if (mqp
->rq
.tail
!= mqp
->rq
.head
) {
2662 recv_mcq
= to_mcq(mqp
->ibqp
.recv_cq
);
2663 spin_lock_irqsave(&recv_mcq
->lock
, flags_cq
);
2664 if (recv_mcq
->mcq
.comp
&&
2665 mqp
->ibqp
.recv_cq
->comp_handler
) {
2666 if (!recv_mcq
->mcq
.reset_notify_added
) {
2667 recv_mcq
->mcq
.reset_notify_added
= 1;
2668 list_add_tail(&recv_mcq
->mcq
.reset_notify
,
2672 spin_unlock_irqrestore(&recv_mcq
->lock
,
2676 spin_unlock_irqrestore(&mqp
->rq
.lock
, flags_qp
);
2678 /*At that point all inflight post send were put to be executed as of we
2679 * lock/unlock above locks Now need to arm all involved CQs.
2681 list_for_each_entry(mcq
, &cq_armed_list
, reset_notify
) {
2684 spin_unlock_irqrestore(&ibdev
->reset_flow_resource_lock
, flags
);
2687 static void mlx5_ib_event(struct mlx5_core_dev
*dev
, void *context
,
2688 enum mlx5_dev_event event
, unsigned long param
)
2690 struct mlx5_ib_dev
*ibdev
= (struct mlx5_ib_dev
*)context
;
2691 struct ib_event ibev
;
2696 case MLX5_DEV_EVENT_SYS_ERROR
:
2697 ibev
.event
= IB_EVENT_DEVICE_FATAL
;
2698 mlx5_ib_handle_internal_error(ibdev
);
2702 case MLX5_DEV_EVENT_PORT_UP
:
2703 case MLX5_DEV_EVENT_PORT_DOWN
:
2704 case MLX5_DEV_EVENT_PORT_INITIALIZED
:
2707 /* In RoCE, port up/down events are handled in
2708 * mlx5_netdev_event().
2710 if (mlx5_ib_port_link_layer(&ibdev
->ib_dev
, port
) ==
2711 IB_LINK_LAYER_ETHERNET
)
2714 ibev
.event
= (event
== MLX5_DEV_EVENT_PORT_UP
) ?
2715 IB_EVENT_PORT_ACTIVE
: IB_EVENT_PORT_ERR
;
2718 case MLX5_DEV_EVENT_LID_CHANGE
:
2719 ibev
.event
= IB_EVENT_LID_CHANGE
;
2723 case MLX5_DEV_EVENT_PKEY_CHANGE
:
2724 ibev
.event
= IB_EVENT_PKEY_CHANGE
;
2727 schedule_work(&ibdev
->devr
.ports
[port
- 1].pkey_change_work
);
2730 case MLX5_DEV_EVENT_GUID_CHANGE
:
2731 ibev
.event
= IB_EVENT_GID_CHANGE
;
2735 case MLX5_DEV_EVENT_CLIENT_REREG
:
2736 ibev
.event
= IB_EVENT_CLIENT_REREGISTER
;
2743 ibev
.device
= &ibdev
->ib_dev
;
2744 ibev
.element
.port_num
= port
;
2746 if (port
< 1 || port
> ibdev
->num_ports
) {
2747 mlx5_ib_warn(ibdev
, "warning: event on port %d\n", port
);
2751 if (ibdev
->ib_active
)
2752 ib_dispatch_event(&ibev
);
2755 ibdev
->ib_active
= false;
2758 static int set_has_smi_cap(struct mlx5_ib_dev
*dev
)
2760 struct mlx5_hca_vport_context vport_ctx
;
2764 for (port
= 1; port
<= MLX5_CAP_GEN(dev
->mdev
, num_ports
); port
++) {
2765 dev
->mdev
->port_caps
[port
- 1].has_smi
= false;
2766 if (MLX5_CAP_GEN(dev
->mdev
, port_type
) ==
2767 MLX5_CAP_PORT_TYPE_IB
) {
2768 if (MLX5_CAP_GEN(dev
->mdev
, ib_virt
)) {
2769 err
= mlx5_query_hca_vport_context(dev
->mdev
, 0,
2773 mlx5_ib_err(dev
, "query_hca_vport_context for port=%d failed %d\n",
2777 dev
->mdev
->port_caps
[port
- 1].has_smi
=
2780 dev
->mdev
->port_caps
[port
- 1].has_smi
= true;
2787 static void get_ext_port_caps(struct mlx5_ib_dev
*dev
)
2791 for (port
= 1; port
<= MLX5_CAP_GEN(dev
->mdev
, num_ports
); port
++)
2792 mlx5_query_ext_port_caps(dev
, port
);
2795 static int get_port_caps(struct mlx5_ib_dev
*dev
)
2797 struct ib_device_attr
*dprops
= NULL
;
2798 struct ib_port_attr
*pprops
= NULL
;
2801 struct ib_udata uhw
= {.inlen
= 0, .outlen
= 0};
2803 pprops
= kmalloc(sizeof(*pprops
), GFP_KERNEL
);
2807 dprops
= kmalloc(sizeof(*dprops
), GFP_KERNEL
);
2811 err
= set_has_smi_cap(dev
);
2815 err
= mlx5_ib_query_device(&dev
->ib_dev
, dprops
, &uhw
);
2817 mlx5_ib_warn(dev
, "query_device failed %d\n", err
);
2821 for (port
= 1; port
<= MLX5_CAP_GEN(dev
->mdev
, num_ports
); port
++) {
2822 memset(pprops
, 0, sizeof(*pprops
));
2823 err
= mlx5_ib_query_port(&dev
->ib_dev
, port
, pprops
);
2825 mlx5_ib_warn(dev
, "query_port %d failed %d\n",
2829 dev
->mdev
->port_caps
[port
- 1].pkey_table_len
=
2831 dev
->mdev
->port_caps
[port
- 1].gid_table_len
=
2832 pprops
->gid_tbl_len
;
2833 mlx5_ib_dbg(dev
, "pkey_table_len %d, gid_table_len %d\n",
2834 dprops
->max_pkeys
, pprops
->gid_tbl_len
);
2844 static void destroy_umrc_res(struct mlx5_ib_dev
*dev
)
2848 err
= mlx5_mr_cache_cleanup(dev
);
2850 mlx5_ib_warn(dev
, "mr cache cleanup failed\n");
2852 mlx5_ib_destroy_qp(dev
->umrc
.qp
);
2853 ib_free_cq(dev
->umrc
.cq
);
2854 ib_dealloc_pd(dev
->umrc
.pd
);
2861 static int create_umr_res(struct mlx5_ib_dev
*dev
)
2863 struct ib_qp_init_attr
*init_attr
= NULL
;
2864 struct ib_qp_attr
*attr
= NULL
;
2870 attr
= kzalloc(sizeof(*attr
), GFP_KERNEL
);
2871 init_attr
= kzalloc(sizeof(*init_attr
), GFP_KERNEL
);
2872 if (!attr
|| !init_attr
) {
2877 pd
= ib_alloc_pd(&dev
->ib_dev
, 0);
2879 mlx5_ib_dbg(dev
, "Couldn't create PD for sync UMR QP\n");
2884 cq
= ib_alloc_cq(&dev
->ib_dev
, NULL
, 128, 0, IB_POLL_SOFTIRQ
);
2886 mlx5_ib_dbg(dev
, "Couldn't create CQ for sync UMR QP\n");
2891 init_attr
->send_cq
= cq
;
2892 init_attr
->recv_cq
= cq
;
2893 init_attr
->sq_sig_type
= IB_SIGNAL_ALL_WR
;
2894 init_attr
->cap
.max_send_wr
= MAX_UMR_WR
;
2895 init_attr
->cap
.max_send_sge
= 1;
2896 init_attr
->qp_type
= MLX5_IB_QPT_REG_UMR
;
2897 init_attr
->port_num
= 1;
2898 qp
= mlx5_ib_create_qp(pd
, init_attr
, NULL
);
2900 mlx5_ib_dbg(dev
, "Couldn't create sync UMR QP\n");
2904 qp
->device
= &dev
->ib_dev
;
2907 qp
->qp_type
= MLX5_IB_QPT_REG_UMR
;
2909 attr
->qp_state
= IB_QPS_INIT
;
2911 ret
= mlx5_ib_modify_qp(qp
, attr
, IB_QP_STATE
| IB_QP_PKEY_INDEX
|
2914 mlx5_ib_dbg(dev
, "Couldn't modify UMR QP\n");
2918 memset(attr
, 0, sizeof(*attr
));
2919 attr
->qp_state
= IB_QPS_RTR
;
2920 attr
->path_mtu
= IB_MTU_256
;
2922 ret
= mlx5_ib_modify_qp(qp
, attr
, IB_QP_STATE
, NULL
);
2924 mlx5_ib_dbg(dev
, "Couldn't modify umr QP to rtr\n");
2928 memset(attr
, 0, sizeof(*attr
));
2929 attr
->qp_state
= IB_QPS_RTS
;
2930 ret
= mlx5_ib_modify_qp(qp
, attr
, IB_QP_STATE
, NULL
);
2932 mlx5_ib_dbg(dev
, "Couldn't modify umr QP to rts\n");
2940 sema_init(&dev
->umrc
.sem
, MAX_UMR_WR
);
2941 ret
= mlx5_mr_cache_init(dev
);
2943 mlx5_ib_warn(dev
, "mr cache init failed %d\n", ret
);
2953 mlx5_ib_destroy_qp(qp
);
2967 static u8
mlx5_get_umr_fence(u8 umr_fence_cap
)
2969 switch (umr_fence_cap
) {
2970 case MLX5_CAP_UMR_FENCE_NONE
:
2971 return MLX5_FENCE_MODE_NONE
;
2972 case MLX5_CAP_UMR_FENCE_SMALL
:
2973 return MLX5_FENCE_MODE_INITIATOR_SMALL
;
2975 return MLX5_FENCE_MODE_STRONG_ORDERING
;
2979 static int create_dev_resources(struct mlx5_ib_resources
*devr
)
2981 struct ib_srq_init_attr attr
;
2982 struct mlx5_ib_dev
*dev
;
2983 struct ib_cq_init_attr cq_attr
= {.cqe
= 1};
2987 dev
= container_of(devr
, struct mlx5_ib_dev
, devr
);
2989 mutex_init(&devr
->mutex
);
2991 devr
->p0
= mlx5_ib_alloc_pd(&dev
->ib_dev
, NULL
, NULL
);
2992 if (IS_ERR(devr
->p0
)) {
2993 ret
= PTR_ERR(devr
->p0
);
2996 devr
->p0
->device
= &dev
->ib_dev
;
2997 devr
->p0
->uobject
= NULL
;
2998 atomic_set(&devr
->p0
->usecnt
, 0);
3000 devr
->c0
= mlx5_ib_create_cq(&dev
->ib_dev
, &cq_attr
, NULL
, NULL
);
3001 if (IS_ERR(devr
->c0
)) {
3002 ret
= PTR_ERR(devr
->c0
);
3005 devr
->c0
->device
= &dev
->ib_dev
;
3006 devr
->c0
->uobject
= NULL
;
3007 devr
->c0
->comp_handler
= NULL
;
3008 devr
->c0
->event_handler
= NULL
;
3009 devr
->c0
->cq_context
= NULL
;
3010 atomic_set(&devr
->c0
->usecnt
, 0);
3012 devr
->x0
= mlx5_ib_alloc_xrcd(&dev
->ib_dev
, NULL
, NULL
);
3013 if (IS_ERR(devr
->x0
)) {
3014 ret
= PTR_ERR(devr
->x0
);
3017 devr
->x0
->device
= &dev
->ib_dev
;
3018 devr
->x0
->inode
= NULL
;
3019 atomic_set(&devr
->x0
->usecnt
, 0);
3020 mutex_init(&devr
->x0
->tgt_qp_mutex
);
3021 INIT_LIST_HEAD(&devr
->x0
->tgt_qp_list
);
3023 devr
->x1
= mlx5_ib_alloc_xrcd(&dev
->ib_dev
, NULL
, NULL
);
3024 if (IS_ERR(devr
->x1
)) {
3025 ret
= PTR_ERR(devr
->x1
);
3028 devr
->x1
->device
= &dev
->ib_dev
;
3029 devr
->x1
->inode
= NULL
;
3030 atomic_set(&devr
->x1
->usecnt
, 0);
3031 mutex_init(&devr
->x1
->tgt_qp_mutex
);
3032 INIT_LIST_HEAD(&devr
->x1
->tgt_qp_list
);
3034 memset(&attr
, 0, sizeof(attr
));
3035 attr
.attr
.max_sge
= 1;
3036 attr
.attr
.max_wr
= 1;
3037 attr
.srq_type
= IB_SRQT_XRC
;
3038 attr
.ext
.xrc
.cq
= devr
->c0
;
3039 attr
.ext
.xrc
.xrcd
= devr
->x0
;
3041 devr
->s0
= mlx5_ib_create_srq(devr
->p0
, &attr
, NULL
);
3042 if (IS_ERR(devr
->s0
)) {
3043 ret
= PTR_ERR(devr
->s0
);
3046 devr
->s0
->device
= &dev
->ib_dev
;
3047 devr
->s0
->pd
= devr
->p0
;
3048 devr
->s0
->uobject
= NULL
;
3049 devr
->s0
->event_handler
= NULL
;
3050 devr
->s0
->srq_context
= NULL
;
3051 devr
->s0
->srq_type
= IB_SRQT_XRC
;
3052 devr
->s0
->ext
.xrc
.xrcd
= devr
->x0
;
3053 devr
->s0
->ext
.xrc
.cq
= devr
->c0
;
3054 atomic_inc(&devr
->s0
->ext
.xrc
.xrcd
->usecnt
);
3055 atomic_inc(&devr
->s0
->ext
.xrc
.cq
->usecnt
);
3056 atomic_inc(&devr
->p0
->usecnt
);
3057 atomic_set(&devr
->s0
->usecnt
, 0);
3059 memset(&attr
, 0, sizeof(attr
));
3060 attr
.attr
.max_sge
= 1;
3061 attr
.attr
.max_wr
= 1;
3062 attr
.srq_type
= IB_SRQT_BASIC
;
3063 devr
->s1
= mlx5_ib_create_srq(devr
->p0
, &attr
, NULL
);
3064 if (IS_ERR(devr
->s1
)) {
3065 ret
= PTR_ERR(devr
->s1
);
3068 devr
->s1
->device
= &dev
->ib_dev
;
3069 devr
->s1
->pd
= devr
->p0
;
3070 devr
->s1
->uobject
= NULL
;
3071 devr
->s1
->event_handler
= NULL
;
3072 devr
->s1
->srq_context
= NULL
;
3073 devr
->s1
->srq_type
= IB_SRQT_BASIC
;
3074 devr
->s1
->ext
.xrc
.cq
= devr
->c0
;
3075 atomic_inc(&devr
->p0
->usecnt
);
3076 atomic_set(&devr
->s0
->usecnt
, 0);
3078 for (port
= 0; port
< ARRAY_SIZE(devr
->ports
); ++port
) {
3079 INIT_WORK(&devr
->ports
[port
].pkey_change_work
,
3080 pkey_change_handler
);
3081 devr
->ports
[port
].devr
= devr
;
3087 mlx5_ib_destroy_srq(devr
->s0
);
3089 mlx5_ib_dealloc_xrcd(devr
->x1
);
3091 mlx5_ib_dealloc_xrcd(devr
->x0
);
3093 mlx5_ib_destroy_cq(devr
->c0
);
3095 mlx5_ib_dealloc_pd(devr
->p0
);
3100 static void destroy_dev_resources(struct mlx5_ib_resources
*devr
)
3102 struct mlx5_ib_dev
*dev
=
3103 container_of(devr
, struct mlx5_ib_dev
, devr
);
3106 mlx5_ib_destroy_srq(devr
->s1
);
3107 mlx5_ib_destroy_srq(devr
->s0
);
3108 mlx5_ib_dealloc_xrcd(devr
->x0
);
3109 mlx5_ib_dealloc_xrcd(devr
->x1
);
3110 mlx5_ib_destroy_cq(devr
->c0
);
3111 mlx5_ib_dealloc_pd(devr
->p0
);
3113 /* Make sure no change P_Key work items are still executing */
3114 for (port
= 0; port
< dev
->num_ports
; ++port
)
3115 cancel_work_sync(&devr
->ports
[port
].pkey_change_work
);
3118 static u32
get_core_cap_flags(struct ib_device
*ibdev
)
3120 struct mlx5_ib_dev
*dev
= to_mdev(ibdev
);
3121 enum rdma_link_layer ll
= mlx5_ib_port_link_layer(ibdev
, 1);
3122 u8 l3_type_cap
= MLX5_CAP_ROCE(dev
->mdev
, l3_type
);
3123 u8 roce_version_cap
= MLX5_CAP_ROCE(dev
->mdev
, roce_version
);
3126 if (ll
== IB_LINK_LAYER_INFINIBAND
)
3127 return RDMA_CORE_PORT_IBA_IB
;
3129 ret
= RDMA_CORE_PORT_RAW_PACKET
;
3131 if (!(l3_type_cap
& MLX5_ROCE_L3_TYPE_IPV4_CAP
))
3134 if (!(l3_type_cap
& MLX5_ROCE_L3_TYPE_IPV6_CAP
))
3137 if (roce_version_cap
& MLX5_ROCE_VERSION_1_CAP
)
3138 ret
|= RDMA_CORE_PORT_IBA_ROCE
;
3140 if (roce_version_cap
& MLX5_ROCE_VERSION_2_CAP
)
3141 ret
|= RDMA_CORE_PORT_IBA_ROCE_UDP_ENCAP
;
3146 static int mlx5_port_immutable(struct ib_device
*ibdev
, u8 port_num
,
3147 struct ib_port_immutable
*immutable
)
3149 struct ib_port_attr attr
;
3150 struct mlx5_ib_dev
*dev
= to_mdev(ibdev
);
3151 enum rdma_link_layer ll
= mlx5_ib_port_link_layer(ibdev
, port_num
);
3154 immutable
->core_cap_flags
= get_core_cap_flags(ibdev
);
3156 err
= ib_query_port(ibdev
, port_num
, &attr
);
3160 immutable
->pkey_tbl_len
= attr
.pkey_tbl_len
;
3161 immutable
->gid_tbl_len
= attr
.gid_tbl_len
;
3162 immutable
->core_cap_flags
= get_core_cap_flags(ibdev
);
3163 if ((ll
== IB_LINK_LAYER_INFINIBAND
) || MLX5_CAP_GEN(dev
->mdev
, roce
))
3164 immutable
->max_mad_size
= IB_MGMT_MAD_SIZE
;
3169 static void get_dev_fw_str(struct ib_device
*ibdev
, char *str
,
3172 struct mlx5_ib_dev
*dev
=
3173 container_of(ibdev
, struct mlx5_ib_dev
, ib_dev
);
3174 snprintf(str
, str_len
, "%d.%d.%04d", fw_rev_maj(dev
->mdev
),
3175 fw_rev_min(dev
->mdev
), fw_rev_sub(dev
->mdev
));
3178 static int mlx5_eth_lag_init(struct mlx5_ib_dev
*dev
)
3180 struct mlx5_core_dev
*mdev
= dev
->mdev
;
3181 struct mlx5_flow_namespace
*ns
= mlx5_get_flow_namespace(mdev
,
3182 MLX5_FLOW_NAMESPACE_LAG
);
3183 struct mlx5_flow_table
*ft
;
3186 if (!ns
|| !mlx5_lag_is_active(mdev
))
3189 err
= mlx5_cmd_create_vport_lag(mdev
);
3193 ft
= mlx5_create_lag_demux_flow_table(ns
, 0, 0);
3196 goto err_destroy_vport_lag
;
3199 dev
->flow_db
.lag_demux_ft
= ft
;
3202 err_destroy_vport_lag
:
3203 mlx5_cmd_destroy_vport_lag(mdev
);
3207 static void mlx5_eth_lag_cleanup(struct mlx5_ib_dev
*dev
)
3209 struct mlx5_core_dev
*mdev
= dev
->mdev
;
3211 if (dev
->flow_db
.lag_demux_ft
) {
3212 mlx5_destroy_flow_table(dev
->flow_db
.lag_demux_ft
);
3213 dev
->flow_db
.lag_demux_ft
= NULL
;
3215 mlx5_cmd_destroy_vport_lag(mdev
);
3219 static int mlx5_add_netdev_notifier(struct mlx5_ib_dev
*dev
)
3223 dev
->roce
.nb
.notifier_call
= mlx5_netdev_event
;
3224 err
= register_netdevice_notifier(&dev
->roce
.nb
);
3226 dev
->roce
.nb
.notifier_call
= NULL
;
3233 static void mlx5_remove_netdev_notifier(struct mlx5_ib_dev
*dev
)
3235 if (dev
->roce
.nb
.notifier_call
) {
3236 unregister_netdevice_notifier(&dev
->roce
.nb
);
3237 dev
->roce
.nb
.notifier_call
= NULL
;
3241 static int mlx5_enable_eth(struct mlx5_ib_dev
*dev
)
3245 err
= mlx5_add_netdev_notifier(dev
);
3249 if (MLX5_CAP_GEN(dev
->mdev
, roce
)) {
3250 err
= mlx5_nic_vport_enable_roce(dev
->mdev
);
3252 goto err_unregister_netdevice_notifier
;
3255 err
= mlx5_eth_lag_init(dev
);
3257 goto err_disable_roce
;
3262 if (MLX5_CAP_GEN(dev
->mdev
, roce
))
3263 mlx5_nic_vport_disable_roce(dev
->mdev
);
3265 err_unregister_netdevice_notifier
:
3266 mlx5_remove_netdev_notifier(dev
);
3270 static void mlx5_disable_eth(struct mlx5_ib_dev
*dev
)
3272 mlx5_eth_lag_cleanup(dev
);
3273 if (MLX5_CAP_GEN(dev
->mdev
, roce
))
3274 mlx5_nic_vport_disable_roce(dev
->mdev
);
3277 struct mlx5_ib_counter
{
3282 #define INIT_Q_COUNTER(_name) \
3283 { .name = #_name, .offset = MLX5_BYTE_OFF(query_q_counter_out, _name)}
3285 static const struct mlx5_ib_counter basic_q_cnts
[] = {
3286 INIT_Q_COUNTER(rx_write_requests
),
3287 INIT_Q_COUNTER(rx_read_requests
),
3288 INIT_Q_COUNTER(rx_atomic_requests
),
3289 INIT_Q_COUNTER(out_of_buffer
),
3292 static const struct mlx5_ib_counter out_of_seq_q_cnts
[] = {
3293 INIT_Q_COUNTER(out_of_sequence
),
3296 static const struct mlx5_ib_counter retrans_q_cnts
[] = {
3297 INIT_Q_COUNTER(duplicate_request
),
3298 INIT_Q_COUNTER(rnr_nak_retry_err
),
3299 INIT_Q_COUNTER(packet_seq_err
),
3300 INIT_Q_COUNTER(implied_nak_seq_err
),
3301 INIT_Q_COUNTER(local_ack_timeout_err
),
3304 #define INIT_CONG_COUNTER(_name) \
3305 { .name = #_name, .offset = \
3306 MLX5_BYTE_OFF(query_cong_statistics_out, _name ## _high)}
3308 static const struct mlx5_ib_counter cong_cnts
[] = {
3309 INIT_CONG_COUNTER(rp_cnp_ignored
),
3310 INIT_CONG_COUNTER(rp_cnp_handled
),
3311 INIT_CONG_COUNTER(np_ecn_marked_roce_packets
),
3312 INIT_CONG_COUNTER(np_cnp_sent
),
3315 static void mlx5_ib_dealloc_counters(struct mlx5_ib_dev
*dev
)
3319 for (i
= 0; i
< dev
->num_ports
; i
++) {
3320 mlx5_core_dealloc_q_counter(dev
->mdev
,
3321 dev
->port
[i
].cnts
.set_id
);
3322 kfree(dev
->port
[i
].cnts
.names
);
3323 kfree(dev
->port
[i
].cnts
.offsets
);
3327 static int __mlx5_ib_alloc_counters(struct mlx5_ib_dev
*dev
,
3328 struct mlx5_ib_counters
*cnts
)
3332 num_counters
= ARRAY_SIZE(basic_q_cnts
);
3334 if (MLX5_CAP_GEN(dev
->mdev
, out_of_seq_cnt
))
3335 num_counters
+= ARRAY_SIZE(out_of_seq_q_cnts
);
3337 if (MLX5_CAP_GEN(dev
->mdev
, retransmission_q_counters
))
3338 num_counters
+= ARRAY_SIZE(retrans_q_cnts
);
3339 cnts
->num_q_counters
= num_counters
;
3341 if (MLX5_CAP_GEN(dev
->mdev
, cc_query_allowed
)) {
3342 cnts
->num_cong_counters
= ARRAY_SIZE(cong_cnts
);
3343 num_counters
+= ARRAY_SIZE(cong_cnts
);
3346 cnts
->names
= kcalloc(num_counters
, sizeof(cnts
->names
), GFP_KERNEL
);
3350 cnts
->offsets
= kcalloc(num_counters
,
3351 sizeof(cnts
->offsets
), GFP_KERNEL
);
3362 static void mlx5_ib_fill_counters(struct mlx5_ib_dev
*dev
,
3369 for (i
= 0; i
< ARRAY_SIZE(basic_q_cnts
); i
++, j
++) {
3370 names
[j
] = basic_q_cnts
[i
].name
;
3371 offsets
[j
] = basic_q_cnts
[i
].offset
;
3374 if (MLX5_CAP_GEN(dev
->mdev
, out_of_seq_cnt
)) {
3375 for (i
= 0; i
< ARRAY_SIZE(out_of_seq_q_cnts
); i
++, j
++) {
3376 names
[j
] = out_of_seq_q_cnts
[i
].name
;
3377 offsets
[j
] = out_of_seq_q_cnts
[i
].offset
;
3381 if (MLX5_CAP_GEN(dev
->mdev
, retransmission_q_counters
)) {
3382 for (i
= 0; i
< ARRAY_SIZE(retrans_q_cnts
); i
++, j
++) {
3383 names
[j
] = retrans_q_cnts
[i
].name
;
3384 offsets
[j
] = retrans_q_cnts
[i
].offset
;
3388 if (MLX5_CAP_GEN(dev
->mdev
, cc_query_allowed
)) {
3389 for (i
= 0; i
< ARRAY_SIZE(cong_cnts
); i
++, j
++) {
3390 names
[j
] = cong_cnts
[i
].name
;
3391 offsets
[j
] = cong_cnts
[i
].offset
;
3396 static int mlx5_ib_alloc_counters(struct mlx5_ib_dev
*dev
)
3401 for (i
= 0; i
< dev
->num_ports
; i
++) {
3402 struct mlx5_ib_port
*port
= &dev
->port
[i
];
3404 ret
= mlx5_core_alloc_q_counter(dev
->mdev
,
3405 &port
->cnts
.set_id
);
3408 "couldn't allocate queue counter for port %d, err %d\n",
3410 goto dealloc_counters
;
3413 ret
= __mlx5_ib_alloc_counters(dev
, &port
->cnts
);
3415 goto dealloc_counters
;
3417 mlx5_ib_fill_counters(dev
, port
->cnts
.names
,
3418 port
->cnts
.offsets
);
3425 mlx5_core_dealloc_q_counter(dev
->mdev
,
3426 dev
->port
[i
].cnts
.set_id
);
3431 static struct rdma_hw_stats
*mlx5_ib_alloc_hw_stats(struct ib_device
*ibdev
,
3434 struct mlx5_ib_dev
*dev
= to_mdev(ibdev
);
3435 struct mlx5_ib_port
*port
= &dev
->port
[port_num
- 1];
3437 /* We support only per port stats */
3441 return rdma_alloc_hw_stats_struct(port
->cnts
.names
,
3442 port
->cnts
.num_q_counters
+
3443 port
->cnts
.num_cong_counters
,
3444 RDMA_HW_STATS_DEFAULT_LIFESPAN
);
3447 static int mlx5_ib_query_q_counters(struct mlx5_ib_dev
*dev
,
3448 struct mlx5_ib_port
*port
,
3449 struct rdma_hw_stats
*stats
)
3451 int outlen
= MLX5_ST_SZ_BYTES(query_q_counter_out
);
3456 out
= kvzalloc(outlen
, GFP_KERNEL
);
3460 ret
= mlx5_core_query_q_counter(dev
->mdev
,
3461 port
->cnts
.set_id
, 0,
3466 for (i
= 0; i
< port
->cnts
.num_q_counters
; i
++) {
3467 val
= *(__be32
*)(out
+ port
->cnts
.offsets
[i
]);
3468 stats
->value
[i
] = (u64
)be32_to_cpu(val
);
3476 static int mlx5_ib_query_cong_counters(struct mlx5_ib_dev
*dev
,
3477 struct mlx5_ib_port
*port
,
3478 struct rdma_hw_stats
*stats
)
3480 int outlen
= MLX5_ST_SZ_BYTES(query_cong_statistics_out
);
3483 int offset
= port
->cnts
.num_q_counters
;
3485 out
= kvzalloc(outlen
, GFP_KERNEL
);
3489 ret
= mlx5_cmd_query_cong_counter(dev
->mdev
, false, out
, outlen
);
3493 for (i
= 0; i
< port
->cnts
.num_cong_counters
; i
++) {
3494 stats
->value
[i
+ offset
] =
3495 be64_to_cpup((__be64
*)(out
+
3496 port
->cnts
.offsets
[i
+ offset
]));
3504 static int mlx5_ib_get_hw_stats(struct ib_device
*ibdev
,
3505 struct rdma_hw_stats
*stats
,
3506 u8 port_num
, int index
)
3508 struct mlx5_ib_dev
*dev
= to_mdev(ibdev
);
3509 struct mlx5_ib_port
*port
= &dev
->port
[port_num
- 1];
3510 int ret
, num_counters
;
3515 ret
= mlx5_ib_query_q_counters(dev
, port
, stats
);
3518 num_counters
= port
->cnts
.num_q_counters
;
3520 if (MLX5_CAP_GEN(dev
->mdev
, cc_query_allowed
)) {
3521 ret
= mlx5_ib_query_cong_counters(dev
, port
, stats
);
3524 num_counters
+= port
->cnts
.num_cong_counters
;
3527 return num_counters
;
3530 static void mlx5_ib_free_rdma_netdev(struct net_device
*netdev
)
3532 return mlx5_rdma_netdev_free(netdev
);
3535 static struct net_device
*
3536 mlx5_ib_alloc_rdma_netdev(struct ib_device
*hca
,
3538 enum rdma_netdev_t type
,
3540 unsigned char name_assign_type
,
3541 void (*setup
)(struct net_device
*))
3543 struct net_device
*netdev
;
3544 struct rdma_netdev
*rn
;
3546 if (type
!= RDMA_NETDEV_IPOIB
)
3547 return ERR_PTR(-EOPNOTSUPP
);
3549 netdev
= mlx5_rdma_netdev_alloc(to_mdev(hca
)->mdev
, hca
,
3551 if (likely(!IS_ERR_OR_NULL(netdev
))) {
3552 rn
= netdev_priv(netdev
);
3553 rn
->free_rdma_netdev
= mlx5_ib_free_rdma_netdev
;
3558 static void *mlx5_ib_add(struct mlx5_core_dev
*mdev
)
3560 struct mlx5_ib_dev
*dev
;
3561 enum rdma_link_layer ll
;
3567 port_type_cap
= MLX5_CAP_GEN(mdev
, port_type
);
3568 ll
= mlx5_port_type_cap_to_rdma_ll(port_type_cap
);
3570 printk_once(KERN_INFO
"%s", mlx5_version
);
3572 dev
= (struct mlx5_ib_dev
*)ib_alloc_device(sizeof(*dev
));
3578 dev
->port
= kcalloc(MLX5_CAP_GEN(mdev
, num_ports
), sizeof(*dev
->port
),
3583 rwlock_init(&dev
->roce
.netdev_lock
);
3584 err
= get_port_caps(dev
);
3588 if (mlx5_use_mad_ifc(dev
))
3589 get_ext_port_caps(dev
);
3591 if (!mlx5_lag_is_active(mdev
))
3594 name
= "mlx5_bond_%d";
3596 strlcpy(dev
->ib_dev
.name
, name
, IB_DEVICE_NAME_MAX
);
3597 dev
->ib_dev
.owner
= THIS_MODULE
;
3598 dev
->ib_dev
.node_type
= RDMA_NODE_IB_CA
;
3599 dev
->ib_dev
.local_dma_lkey
= 0 /* not supported for now */;
3600 dev
->num_ports
= MLX5_CAP_GEN(mdev
, num_ports
);
3601 dev
->ib_dev
.phys_port_cnt
= dev
->num_ports
;
3602 dev
->ib_dev
.num_comp_vectors
=
3603 dev
->mdev
->priv
.eq_table
.num_comp_vectors
;
3604 dev
->ib_dev
.dev
.parent
= &mdev
->pdev
->dev
;
3606 dev
->ib_dev
.uverbs_abi_ver
= MLX5_IB_UVERBS_ABI_VERSION
;
3607 dev
->ib_dev
.uverbs_cmd_mask
=
3608 (1ull << IB_USER_VERBS_CMD_GET_CONTEXT
) |
3609 (1ull << IB_USER_VERBS_CMD_QUERY_DEVICE
) |
3610 (1ull << IB_USER_VERBS_CMD_QUERY_PORT
) |
3611 (1ull << IB_USER_VERBS_CMD_ALLOC_PD
) |
3612 (1ull << IB_USER_VERBS_CMD_DEALLOC_PD
) |
3613 (1ull << IB_USER_VERBS_CMD_CREATE_AH
) |
3614 (1ull << IB_USER_VERBS_CMD_DESTROY_AH
) |
3615 (1ull << IB_USER_VERBS_CMD_REG_MR
) |
3616 (1ull << IB_USER_VERBS_CMD_REREG_MR
) |
3617 (1ull << IB_USER_VERBS_CMD_DEREG_MR
) |
3618 (1ull << IB_USER_VERBS_CMD_CREATE_COMP_CHANNEL
) |
3619 (1ull << IB_USER_VERBS_CMD_CREATE_CQ
) |
3620 (1ull << IB_USER_VERBS_CMD_RESIZE_CQ
) |
3621 (1ull << IB_USER_VERBS_CMD_DESTROY_CQ
) |
3622 (1ull << IB_USER_VERBS_CMD_CREATE_QP
) |
3623 (1ull << IB_USER_VERBS_CMD_MODIFY_QP
) |
3624 (1ull << IB_USER_VERBS_CMD_QUERY_QP
) |
3625 (1ull << IB_USER_VERBS_CMD_DESTROY_QP
) |
3626 (1ull << IB_USER_VERBS_CMD_ATTACH_MCAST
) |
3627 (1ull << IB_USER_VERBS_CMD_DETACH_MCAST
) |
3628 (1ull << IB_USER_VERBS_CMD_CREATE_SRQ
) |
3629 (1ull << IB_USER_VERBS_CMD_MODIFY_SRQ
) |
3630 (1ull << IB_USER_VERBS_CMD_QUERY_SRQ
) |
3631 (1ull << IB_USER_VERBS_CMD_DESTROY_SRQ
) |
3632 (1ull << IB_USER_VERBS_CMD_CREATE_XSRQ
) |
3633 (1ull << IB_USER_VERBS_CMD_OPEN_QP
);
3634 dev
->ib_dev
.uverbs_ex_cmd_mask
=
3635 (1ull << IB_USER_VERBS_EX_CMD_QUERY_DEVICE
) |
3636 (1ull << IB_USER_VERBS_EX_CMD_CREATE_CQ
) |
3637 (1ull << IB_USER_VERBS_EX_CMD_CREATE_QP
) |
3638 (1ull << IB_USER_VERBS_EX_CMD_MODIFY_QP
);
3640 dev
->ib_dev
.query_device
= mlx5_ib_query_device
;
3641 dev
->ib_dev
.query_port
= mlx5_ib_query_port
;
3642 dev
->ib_dev
.get_link_layer
= mlx5_ib_port_link_layer
;
3643 if (ll
== IB_LINK_LAYER_ETHERNET
)
3644 dev
->ib_dev
.get_netdev
= mlx5_ib_get_netdev
;
3645 dev
->ib_dev
.query_gid
= mlx5_ib_query_gid
;
3646 dev
->ib_dev
.add_gid
= mlx5_ib_add_gid
;
3647 dev
->ib_dev
.del_gid
= mlx5_ib_del_gid
;
3648 dev
->ib_dev
.query_pkey
= mlx5_ib_query_pkey
;
3649 dev
->ib_dev
.modify_device
= mlx5_ib_modify_device
;
3650 dev
->ib_dev
.modify_port
= mlx5_ib_modify_port
;
3651 dev
->ib_dev
.alloc_ucontext
= mlx5_ib_alloc_ucontext
;
3652 dev
->ib_dev
.dealloc_ucontext
= mlx5_ib_dealloc_ucontext
;
3653 dev
->ib_dev
.mmap
= mlx5_ib_mmap
;
3654 dev
->ib_dev
.alloc_pd
= mlx5_ib_alloc_pd
;
3655 dev
->ib_dev
.dealloc_pd
= mlx5_ib_dealloc_pd
;
3656 dev
->ib_dev
.create_ah
= mlx5_ib_create_ah
;
3657 dev
->ib_dev
.query_ah
= mlx5_ib_query_ah
;
3658 dev
->ib_dev
.destroy_ah
= mlx5_ib_destroy_ah
;
3659 dev
->ib_dev
.create_srq
= mlx5_ib_create_srq
;
3660 dev
->ib_dev
.modify_srq
= mlx5_ib_modify_srq
;
3661 dev
->ib_dev
.query_srq
= mlx5_ib_query_srq
;
3662 dev
->ib_dev
.destroy_srq
= mlx5_ib_destroy_srq
;
3663 dev
->ib_dev
.post_srq_recv
= mlx5_ib_post_srq_recv
;
3664 dev
->ib_dev
.create_qp
= mlx5_ib_create_qp
;
3665 dev
->ib_dev
.modify_qp
= mlx5_ib_modify_qp
;
3666 dev
->ib_dev
.query_qp
= mlx5_ib_query_qp
;
3667 dev
->ib_dev
.destroy_qp
= mlx5_ib_destroy_qp
;
3668 dev
->ib_dev
.post_send
= mlx5_ib_post_send
;
3669 dev
->ib_dev
.post_recv
= mlx5_ib_post_recv
;
3670 dev
->ib_dev
.create_cq
= mlx5_ib_create_cq
;
3671 dev
->ib_dev
.modify_cq
= mlx5_ib_modify_cq
;
3672 dev
->ib_dev
.resize_cq
= mlx5_ib_resize_cq
;
3673 dev
->ib_dev
.destroy_cq
= mlx5_ib_destroy_cq
;
3674 dev
->ib_dev
.poll_cq
= mlx5_ib_poll_cq
;
3675 dev
->ib_dev
.req_notify_cq
= mlx5_ib_arm_cq
;
3676 dev
->ib_dev
.get_dma_mr
= mlx5_ib_get_dma_mr
;
3677 dev
->ib_dev
.reg_user_mr
= mlx5_ib_reg_user_mr
;
3678 dev
->ib_dev
.rereg_user_mr
= mlx5_ib_rereg_user_mr
;
3679 dev
->ib_dev
.dereg_mr
= mlx5_ib_dereg_mr
;
3680 dev
->ib_dev
.attach_mcast
= mlx5_ib_mcg_attach
;
3681 dev
->ib_dev
.detach_mcast
= mlx5_ib_mcg_detach
;
3682 dev
->ib_dev
.process_mad
= mlx5_ib_process_mad
;
3683 dev
->ib_dev
.alloc_mr
= mlx5_ib_alloc_mr
;
3684 dev
->ib_dev
.map_mr_sg
= mlx5_ib_map_mr_sg
;
3685 dev
->ib_dev
.check_mr_status
= mlx5_ib_check_mr_status
;
3686 dev
->ib_dev
.get_port_immutable
= mlx5_port_immutable
;
3687 dev
->ib_dev
.get_dev_fw_str
= get_dev_fw_str
;
3688 if (MLX5_CAP_GEN(mdev
, ipoib_enhanced_offloads
))
3689 dev
->ib_dev
.alloc_rdma_netdev
= mlx5_ib_alloc_rdma_netdev
;
3691 if (mlx5_core_is_pf(mdev
)) {
3692 dev
->ib_dev
.get_vf_config
= mlx5_ib_get_vf_config
;
3693 dev
->ib_dev
.set_vf_link_state
= mlx5_ib_set_vf_link_state
;
3694 dev
->ib_dev
.get_vf_stats
= mlx5_ib_get_vf_stats
;
3695 dev
->ib_dev
.set_vf_guid
= mlx5_ib_set_vf_guid
;
3698 dev
->ib_dev
.disassociate_ucontext
= mlx5_ib_disassociate_ucontext
;
3700 mlx5_ib_internal_fill_odp_caps(dev
);
3702 dev
->umr_fence
= mlx5_get_umr_fence(MLX5_CAP_GEN(mdev
, umr_fence
));
3704 if (MLX5_CAP_GEN(mdev
, imaicl
)) {
3705 dev
->ib_dev
.alloc_mw
= mlx5_ib_alloc_mw
;
3706 dev
->ib_dev
.dealloc_mw
= mlx5_ib_dealloc_mw
;
3707 dev
->ib_dev
.uverbs_cmd_mask
|=
3708 (1ull << IB_USER_VERBS_CMD_ALLOC_MW
) |
3709 (1ull << IB_USER_VERBS_CMD_DEALLOC_MW
);
3712 if (MLX5_CAP_GEN(dev
->mdev
, max_qp_cnt
)) {
3713 dev
->ib_dev
.get_hw_stats
= mlx5_ib_get_hw_stats
;
3714 dev
->ib_dev
.alloc_hw_stats
= mlx5_ib_alloc_hw_stats
;
3717 if (MLX5_CAP_GEN(mdev
, xrc
)) {
3718 dev
->ib_dev
.alloc_xrcd
= mlx5_ib_alloc_xrcd
;
3719 dev
->ib_dev
.dealloc_xrcd
= mlx5_ib_dealloc_xrcd
;
3720 dev
->ib_dev
.uverbs_cmd_mask
|=
3721 (1ull << IB_USER_VERBS_CMD_OPEN_XRCD
) |
3722 (1ull << IB_USER_VERBS_CMD_CLOSE_XRCD
);
3725 if (mlx5_ib_port_link_layer(&dev
->ib_dev
, 1) ==
3726 IB_LINK_LAYER_ETHERNET
) {
3727 dev
->ib_dev
.create_flow
= mlx5_ib_create_flow
;
3728 dev
->ib_dev
.destroy_flow
= mlx5_ib_destroy_flow
;
3729 dev
->ib_dev
.create_wq
= mlx5_ib_create_wq
;
3730 dev
->ib_dev
.modify_wq
= mlx5_ib_modify_wq
;
3731 dev
->ib_dev
.destroy_wq
= mlx5_ib_destroy_wq
;
3732 dev
->ib_dev
.create_rwq_ind_table
= mlx5_ib_create_rwq_ind_table
;
3733 dev
->ib_dev
.destroy_rwq_ind_table
= mlx5_ib_destroy_rwq_ind_table
;
3734 dev
->ib_dev
.uverbs_ex_cmd_mask
|=
3735 (1ull << IB_USER_VERBS_EX_CMD_CREATE_FLOW
) |
3736 (1ull << IB_USER_VERBS_EX_CMD_DESTROY_FLOW
) |
3737 (1ull << IB_USER_VERBS_EX_CMD_CREATE_WQ
) |
3738 (1ull << IB_USER_VERBS_EX_CMD_MODIFY_WQ
) |
3739 (1ull << IB_USER_VERBS_EX_CMD_DESTROY_WQ
) |
3740 (1ull << IB_USER_VERBS_EX_CMD_CREATE_RWQ_IND_TBL
) |
3741 (1ull << IB_USER_VERBS_EX_CMD_DESTROY_RWQ_IND_TBL
);
3743 err
= init_node_data(dev
);
3747 mutex_init(&dev
->flow_db
.lock
);
3748 mutex_init(&dev
->cap_mask_mutex
);
3749 INIT_LIST_HEAD(&dev
->qp_list
);
3750 spin_lock_init(&dev
->reset_flow_resource_lock
);
3752 if (ll
== IB_LINK_LAYER_ETHERNET
) {
3753 err
= mlx5_enable_eth(dev
);
3758 err
= create_dev_resources(&dev
->devr
);
3760 goto err_disable_eth
;
3762 err
= mlx5_ib_odp_init_one(dev
);
3766 if (MLX5_CAP_GEN(dev
->mdev
, max_qp_cnt
)) {
3767 err
= mlx5_ib_alloc_counters(dev
);
3772 dev
->mdev
->priv
.uar
= mlx5_get_uars_page(dev
->mdev
);
3773 if (!dev
->mdev
->priv
.uar
)
3776 err
= mlx5_alloc_bfreg(dev
->mdev
, &dev
->bfreg
, false, false);
3780 err
= mlx5_alloc_bfreg(dev
->mdev
, &dev
->fp_bfreg
, false, true);
3784 err
= ib_register_device(&dev
->ib_dev
, NULL
);
3788 err
= create_umr_res(dev
);
3792 for (i
= 0; i
< ARRAY_SIZE(mlx5_class_attributes
); i
++) {
3793 err
= device_create_file(&dev
->ib_dev
.dev
,
3794 mlx5_class_attributes
[i
]);
3799 dev
->ib_active
= true;
3804 destroy_umrc_res(dev
);
3807 ib_unregister_device(&dev
->ib_dev
);
3810 mlx5_free_bfreg(dev
->mdev
, &dev
->fp_bfreg
);
3813 mlx5_free_bfreg(dev
->mdev
, &dev
->bfreg
);
3816 mlx5_put_uars_page(dev
->mdev
, dev
->mdev
->priv
.uar
);
3819 if (MLX5_CAP_GEN(dev
->mdev
, max_qp_cnt
))
3820 mlx5_ib_dealloc_counters(dev
);
3823 mlx5_ib_odp_remove_one(dev
);
3826 destroy_dev_resources(&dev
->devr
);
3829 if (ll
== IB_LINK_LAYER_ETHERNET
) {
3830 mlx5_disable_eth(dev
);
3831 mlx5_remove_netdev_notifier(dev
);
3838 ib_dealloc_device((struct ib_device
*)dev
);
3843 static void mlx5_ib_remove(struct mlx5_core_dev
*mdev
, void *context
)
3845 struct mlx5_ib_dev
*dev
= context
;
3846 enum rdma_link_layer ll
= mlx5_ib_port_link_layer(&dev
->ib_dev
, 1);
3848 mlx5_remove_netdev_notifier(dev
);
3849 ib_unregister_device(&dev
->ib_dev
);
3850 mlx5_free_bfreg(dev
->mdev
, &dev
->fp_bfreg
);
3851 mlx5_free_bfreg(dev
->mdev
, &dev
->bfreg
);
3852 mlx5_put_uars_page(dev
->mdev
, mdev
->priv
.uar
);
3853 if (MLX5_CAP_GEN(dev
->mdev
, max_qp_cnt
))
3854 mlx5_ib_dealloc_counters(dev
);
3855 destroy_umrc_res(dev
);
3856 mlx5_ib_odp_remove_one(dev
);
3857 destroy_dev_resources(&dev
->devr
);
3858 if (ll
== IB_LINK_LAYER_ETHERNET
)
3859 mlx5_disable_eth(dev
);
3861 ib_dealloc_device(&dev
->ib_dev
);
3864 static struct mlx5_interface mlx5_ib_interface
= {
3866 .remove
= mlx5_ib_remove
,
3867 .event
= mlx5_ib_event
,
3868 #ifdef CONFIG_INFINIBAND_ON_DEMAND_PAGING
3869 .pfault
= mlx5_ib_pfault
,
3871 .protocol
= MLX5_INTERFACE_PROTOCOL_IB
,
3874 static int __init
mlx5_ib_init(void)
3880 err
= mlx5_register_interface(&mlx5_ib_interface
);
3885 static void __exit
mlx5_ib_cleanup(void)
3887 mlx5_unregister_interface(&mlx5_ib_interface
);
3890 module_init(mlx5_ib_init
);
3891 module_exit(mlx5_ib_cleanup
);