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/delay.h>
45 #include <rdma/ib_user_verbs.h>
46 #include <rdma/ib_addr.h>
47 #include <rdma/ib_cache.h>
48 #include <linux/mlx5/port.h>
49 #include <linux/mlx5/vport.h>
50 #include <linux/list.h>
51 #include <rdma/ib_smi.h>
52 #include <rdma/ib_umem.h>
54 #include <linux/etherdevice.h>
55 #include <linux/mlx5/fs.h>
58 #define DRIVER_NAME "mlx5_ib"
59 #define DRIVER_VERSION "2.2-1"
60 #define DRIVER_RELDATE "Feb 2014"
62 MODULE_AUTHOR("Eli Cohen <eli@mellanox.com>");
63 MODULE_DESCRIPTION("Mellanox Connect-IB HCA IB driver");
64 MODULE_LICENSE("Dual BSD/GPL");
65 MODULE_VERSION(DRIVER_VERSION
);
67 static int deprecated_prof_sel
= 2;
68 module_param_named(prof_sel
, deprecated_prof_sel
, int, 0444);
69 MODULE_PARM_DESC(prof_sel
, "profile selector. Deprecated here. Moved to module mlx5_core");
71 static char mlx5_version
[] =
72 DRIVER_NAME
": Mellanox Connect-IB Infiniband driver v"
73 DRIVER_VERSION
" (" DRIVER_RELDATE
")\n";
76 MLX5_ATOMIC_SIZE_QP_8BYTES
= 1 << 3,
79 static enum rdma_link_layer
80 mlx5_port_type_cap_to_rdma_ll(int port_type_cap
)
82 switch (port_type_cap
) {
83 case MLX5_CAP_PORT_TYPE_IB
:
84 return IB_LINK_LAYER_INFINIBAND
;
85 case MLX5_CAP_PORT_TYPE_ETH
:
86 return IB_LINK_LAYER_ETHERNET
;
88 return IB_LINK_LAYER_UNSPECIFIED
;
92 static enum rdma_link_layer
93 mlx5_ib_port_link_layer(struct ib_device
*device
, u8 port_num
)
95 struct mlx5_ib_dev
*dev
= to_mdev(device
);
96 int port_type_cap
= MLX5_CAP_GEN(dev
->mdev
, port_type
);
98 return mlx5_port_type_cap_to_rdma_ll(port_type_cap
);
101 static int mlx5_netdev_event(struct notifier_block
*this,
102 unsigned long event
, void *ptr
)
104 struct net_device
*ndev
= netdev_notifier_info_to_dev(ptr
);
105 struct mlx5_ib_dev
*ibdev
= container_of(this, struct mlx5_ib_dev
,
109 case NETDEV_REGISTER
:
110 case NETDEV_UNREGISTER
:
111 write_lock(&ibdev
->roce
.netdev_lock
);
112 if (ndev
->dev
.parent
== &ibdev
->mdev
->pdev
->dev
)
113 ibdev
->roce
.netdev
= (event
== NETDEV_UNREGISTER
) ?
115 write_unlock(&ibdev
->roce
.netdev_lock
);
120 struct net_device
*lag_ndev
= mlx5_lag_get_roce_netdev(ibdev
->mdev
);
121 struct net_device
*upper
= NULL
;
124 upper
= netdev_master_upper_dev_get(lag_ndev
);
128 if ((upper
== ndev
|| (!upper
&& ndev
== ibdev
->roce
.netdev
))
129 && ibdev
->ib_active
) {
130 struct ib_event ibev
= {0};
132 ibev
.device
= &ibdev
->ib_dev
;
133 ibev
.event
= (event
== NETDEV_UP
) ?
134 IB_EVENT_PORT_ACTIVE
: IB_EVENT_PORT_ERR
;
135 ibev
.element
.port_num
= 1;
136 ib_dispatch_event(&ibev
);
148 static struct net_device
*mlx5_ib_get_netdev(struct ib_device
*device
,
151 struct mlx5_ib_dev
*ibdev
= to_mdev(device
);
152 struct net_device
*ndev
;
154 ndev
= mlx5_lag_get_roce_netdev(ibdev
->mdev
);
158 /* Ensure ndev does not disappear before we invoke dev_hold()
160 read_lock(&ibdev
->roce
.netdev_lock
);
161 ndev
= ibdev
->roce
.netdev
;
164 read_unlock(&ibdev
->roce
.netdev_lock
);
169 static int mlx5_query_port_roce(struct ib_device
*device
, u8 port_num
,
170 struct ib_port_attr
*props
)
172 struct mlx5_ib_dev
*dev
= to_mdev(device
);
173 struct net_device
*ndev
, *upper
;
174 enum ib_mtu ndev_ib_mtu
;
177 memset(props
, 0, sizeof(*props
));
179 props
->port_cap_flags
|= IB_PORT_CM_SUP
;
180 props
->port_cap_flags
|= IB_PORT_IP_BASED_GIDS
;
182 props
->gid_tbl_len
= MLX5_CAP_ROCE(dev
->mdev
,
183 roce_address_table_size
);
184 props
->max_mtu
= IB_MTU_4096
;
185 props
->max_msg_sz
= 1 << MLX5_CAP_GEN(dev
->mdev
, log_max_msg
);
186 props
->pkey_tbl_len
= 1;
187 props
->state
= IB_PORT_DOWN
;
188 props
->phys_state
= 3;
190 mlx5_query_nic_vport_qkey_viol_cntr(dev
->mdev
, &qkey_viol_cntr
);
191 props
->qkey_viol_cntr
= qkey_viol_cntr
;
193 ndev
= mlx5_ib_get_netdev(device
, port_num
);
197 if (mlx5_lag_is_active(dev
->mdev
)) {
199 upper
= netdev_master_upper_dev_get_rcu(ndev
);
208 if (netif_running(ndev
) && netif_carrier_ok(ndev
)) {
209 props
->state
= IB_PORT_ACTIVE
;
210 props
->phys_state
= 5;
213 ndev_ib_mtu
= iboe_get_mtu(ndev
->mtu
);
217 props
->active_mtu
= min(props
->max_mtu
, ndev_ib_mtu
);
219 props
->active_width
= IB_WIDTH_4X
; /* TODO */
220 props
->active_speed
= IB_SPEED_QDR
; /* TODO */
225 static void ib_gid_to_mlx5_roce_addr(const union ib_gid
*gid
,
226 const struct ib_gid_attr
*attr
,
229 #define MLX5_SET_RA(p, f, v) MLX5_SET(roce_addr_layout, p, f, v)
230 char *mlx5_addr_l3_addr
= MLX5_ADDR_OF(roce_addr_layout
, mlx5_addr
,
232 void *mlx5_addr_mac
= MLX5_ADDR_OF(roce_addr_layout
, mlx5_addr
,
238 ether_addr_copy(mlx5_addr_mac
, attr
->ndev
->dev_addr
);
240 if (is_vlan_dev(attr
->ndev
)) {
241 MLX5_SET_RA(mlx5_addr
, vlan_valid
, 1);
242 MLX5_SET_RA(mlx5_addr
, vlan_id
, vlan_dev_vlan_id(attr
->ndev
));
245 switch (attr
->gid_type
) {
247 MLX5_SET_RA(mlx5_addr
, roce_version
, MLX5_ROCE_VERSION_1
);
249 case IB_GID_TYPE_ROCE_UDP_ENCAP
:
250 MLX5_SET_RA(mlx5_addr
, roce_version
, MLX5_ROCE_VERSION_2
);
257 if (attr
->gid_type
!= IB_GID_TYPE_IB
) {
258 if (ipv6_addr_v4mapped((void *)gid
))
259 MLX5_SET_RA(mlx5_addr
, roce_l3_type
,
260 MLX5_ROCE_L3_TYPE_IPV4
);
262 MLX5_SET_RA(mlx5_addr
, roce_l3_type
,
263 MLX5_ROCE_L3_TYPE_IPV6
);
266 if ((attr
->gid_type
== IB_GID_TYPE_IB
) ||
267 !ipv6_addr_v4mapped((void *)gid
))
268 memcpy(mlx5_addr_l3_addr
, gid
, sizeof(*gid
));
270 memcpy(&mlx5_addr_l3_addr
[12], &gid
->raw
[12], 4);
273 static int set_roce_addr(struct ib_device
*device
, u8 port_num
,
275 const union ib_gid
*gid
,
276 const struct ib_gid_attr
*attr
)
278 struct mlx5_ib_dev
*dev
= to_mdev(device
);
279 u32 in
[MLX5_ST_SZ_DW(set_roce_address_in
)] = {0};
280 u32 out
[MLX5_ST_SZ_DW(set_roce_address_out
)] = {0};
281 void *in_addr
= MLX5_ADDR_OF(set_roce_address_in
, in
, roce_address
);
282 enum rdma_link_layer ll
= mlx5_ib_port_link_layer(device
, port_num
);
284 if (ll
!= IB_LINK_LAYER_ETHERNET
)
287 ib_gid_to_mlx5_roce_addr(gid
, attr
, in_addr
);
289 MLX5_SET(set_roce_address_in
, in
, roce_address_index
, index
);
290 MLX5_SET(set_roce_address_in
, in
, opcode
, MLX5_CMD_OP_SET_ROCE_ADDRESS
);
291 return mlx5_cmd_exec(dev
->mdev
, in
, sizeof(in
), out
, sizeof(out
));
294 static int mlx5_ib_add_gid(struct ib_device
*device
, u8 port_num
,
295 unsigned int index
, const union ib_gid
*gid
,
296 const struct ib_gid_attr
*attr
,
297 __always_unused
void **context
)
299 return set_roce_addr(device
, port_num
, index
, gid
, attr
);
302 static int mlx5_ib_del_gid(struct ib_device
*device
, u8 port_num
,
303 unsigned int index
, __always_unused
void **context
)
305 return set_roce_addr(device
, port_num
, index
, NULL
, NULL
);
308 __be16
mlx5_get_roce_udp_sport(struct mlx5_ib_dev
*dev
, u8 port_num
,
311 struct ib_gid_attr attr
;
314 if (ib_get_cached_gid(&dev
->ib_dev
, port_num
, index
, &gid
, &attr
))
322 if (attr
.gid_type
!= IB_GID_TYPE_ROCE_UDP_ENCAP
)
325 return cpu_to_be16(MLX5_CAP_ROCE(dev
->mdev
, r_roce_min_src_udp_port
));
328 int mlx5_get_roce_gid_type(struct mlx5_ib_dev
*dev
, u8 port_num
,
329 int index
, enum ib_gid_type
*gid_type
)
331 struct ib_gid_attr attr
;
335 ret
= ib_get_cached_gid(&dev
->ib_dev
, port_num
, index
, &gid
, &attr
);
344 *gid_type
= attr
.gid_type
;
349 static int mlx5_use_mad_ifc(struct mlx5_ib_dev
*dev
)
351 if (MLX5_CAP_GEN(dev
->mdev
, port_type
) == MLX5_CAP_PORT_TYPE_IB
)
352 return !MLX5_CAP_GEN(dev
->mdev
, ib_virt
);
357 MLX5_VPORT_ACCESS_METHOD_MAD
,
358 MLX5_VPORT_ACCESS_METHOD_HCA
,
359 MLX5_VPORT_ACCESS_METHOD_NIC
,
362 static int mlx5_get_vport_access_method(struct ib_device
*ibdev
)
364 if (mlx5_use_mad_ifc(to_mdev(ibdev
)))
365 return MLX5_VPORT_ACCESS_METHOD_MAD
;
367 if (mlx5_ib_port_link_layer(ibdev
, 1) ==
368 IB_LINK_LAYER_ETHERNET
)
369 return MLX5_VPORT_ACCESS_METHOD_NIC
;
371 return MLX5_VPORT_ACCESS_METHOD_HCA
;
374 static void get_atomic_caps(struct mlx5_ib_dev
*dev
,
375 struct ib_device_attr
*props
)
378 u8 atomic_operations
= MLX5_CAP_ATOMIC(dev
->mdev
, atomic_operations
);
379 u8 atomic_size_qp
= MLX5_CAP_ATOMIC(dev
->mdev
, atomic_size_qp
);
380 u8 atomic_req_8B_endianness_mode
=
381 MLX5_CAP_ATOMIC(dev
->mdev
, atomic_req_8B_endianess_mode
);
383 /* Check if HW supports 8 bytes standard atomic operations and capable
384 * of host endianness respond
386 tmp
= MLX5_ATOMIC_OPS_CMP_SWAP
| MLX5_ATOMIC_OPS_FETCH_ADD
;
387 if (((atomic_operations
& tmp
) == tmp
) &&
388 (atomic_size_qp
& MLX5_ATOMIC_SIZE_QP_8BYTES
) &&
389 (atomic_req_8B_endianness_mode
)) {
390 props
->atomic_cap
= IB_ATOMIC_HCA
;
392 props
->atomic_cap
= IB_ATOMIC_NONE
;
396 static int mlx5_query_system_image_guid(struct ib_device
*ibdev
,
397 __be64
*sys_image_guid
)
399 struct mlx5_ib_dev
*dev
= to_mdev(ibdev
);
400 struct mlx5_core_dev
*mdev
= dev
->mdev
;
404 switch (mlx5_get_vport_access_method(ibdev
)) {
405 case MLX5_VPORT_ACCESS_METHOD_MAD
:
406 return mlx5_query_mad_ifc_system_image_guid(ibdev
,
409 case MLX5_VPORT_ACCESS_METHOD_HCA
:
410 err
= mlx5_query_hca_vport_system_image_guid(mdev
, &tmp
);
413 case MLX5_VPORT_ACCESS_METHOD_NIC
:
414 err
= mlx5_query_nic_vport_system_image_guid(mdev
, &tmp
);
422 *sys_image_guid
= cpu_to_be64(tmp
);
428 static int mlx5_query_max_pkeys(struct ib_device
*ibdev
,
431 struct mlx5_ib_dev
*dev
= to_mdev(ibdev
);
432 struct mlx5_core_dev
*mdev
= dev
->mdev
;
434 switch (mlx5_get_vport_access_method(ibdev
)) {
435 case MLX5_VPORT_ACCESS_METHOD_MAD
:
436 return mlx5_query_mad_ifc_max_pkeys(ibdev
, max_pkeys
);
438 case MLX5_VPORT_ACCESS_METHOD_HCA
:
439 case MLX5_VPORT_ACCESS_METHOD_NIC
:
440 *max_pkeys
= mlx5_to_sw_pkey_sz(MLX5_CAP_GEN(mdev
,
449 static int mlx5_query_vendor_id(struct ib_device
*ibdev
,
452 struct mlx5_ib_dev
*dev
= to_mdev(ibdev
);
454 switch (mlx5_get_vport_access_method(ibdev
)) {
455 case MLX5_VPORT_ACCESS_METHOD_MAD
:
456 return mlx5_query_mad_ifc_vendor_id(ibdev
, vendor_id
);
458 case MLX5_VPORT_ACCESS_METHOD_HCA
:
459 case MLX5_VPORT_ACCESS_METHOD_NIC
:
460 return mlx5_core_query_vendor_id(dev
->mdev
, vendor_id
);
467 static int mlx5_query_node_guid(struct mlx5_ib_dev
*dev
,
473 switch (mlx5_get_vport_access_method(&dev
->ib_dev
)) {
474 case MLX5_VPORT_ACCESS_METHOD_MAD
:
475 return mlx5_query_mad_ifc_node_guid(dev
, node_guid
);
477 case MLX5_VPORT_ACCESS_METHOD_HCA
:
478 err
= mlx5_query_hca_vport_node_guid(dev
->mdev
, &tmp
);
481 case MLX5_VPORT_ACCESS_METHOD_NIC
:
482 err
= mlx5_query_nic_vport_node_guid(dev
->mdev
, &tmp
);
490 *node_guid
= cpu_to_be64(tmp
);
495 struct mlx5_reg_node_desc
{
496 u8 desc
[IB_DEVICE_NODE_DESC_MAX
];
499 static int mlx5_query_node_desc(struct mlx5_ib_dev
*dev
, char *node_desc
)
501 struct mlx5_reg_node_desc in
;
503 if (mlx5_use_mad_ifc(dev
))
504 return mlx5_query_mad_ifc_node_desc(dev
, node_desc
);
506 memset(&in
, 0, sizeof(in
));
508 return mlx5_core_access_reg(dev
->mdev
, &in
, sizeof(in
), node_desc
,
509 sizeof(struct mlx5_reg_node_desc
),
510 MLX5_REG_NODE_DESC
, 0, 0);
513 static int mlx5_ib_query_device(struct ib_device
*ibdev
,
514 struct ib_device_attr
*props
,
515 struct ib_udata
*uhw
)
517 struct mlx5_ib_dev
*dev
= to_mdev(ibdev
);
518 struct mlx5_core_dev
*mdev
= dev
->mdev
;
523 u64 min_page_size
= 1ull << MLX5_CAP_GEN(mdev
, log_pg_sz
);
524 struct mlx5_ib_query_device_resp resp
= {};
528 resp_len
= sizeof(resp
.comp_mask
) + sizeof(resp
.response_length
);
529 if (uhw
->outlen
&& uhw
->outlen
< resp_len
)
532 resp
.response_length
= resp_len
;
534 if (uhw
->inlen
&& !ib_is_udata_cleared(uhw
, 0, uhw
->inlen
))
537 memset(props
, 0, sizeof(*props
));
538 err
= mlx5_query_system_image_guid(ibdev
,
539 &props
->sys_image_guid
);
543 err
= mlx5_query_max_pkeys(ibdev
, &props
->max_pkeys
);
547 err
= mlx5_query_vendor_id(ibdev
, &props
->vendor_id
);
551 props
->fw_ver
= ((u64
)fw_rev_maj(dev
->mdev
) << 32) |
552 (fw_rev_min(dev
->mdev
) << 16) |
553 fw_rev_sub(dev
->mdev
);
554 props
->device_cap_flags
= IB_DEVICE_CHANGE_PHY_PORT
|
555 IB_DEVICE_PORT_ACTIVE_EVENT
|
556 IB_DEVICE_SYS_IMAGE_GUID
|
557 IB_DEVICE_RC_RNR_NAK_GEN
;
559 if (MLX5_CAP_GEN(mdev
, pkv
))
560 props
->device_cap_flags
|= IB_DEVICE_BAD_PKEY_CNTR
;
561 if (MLX5_CAP_GEN(mdev
, qkv
))
562 props
->device_cap_flags
|= IB_DEVICE_BAD_QKEY_CNTR
;
563 if (MLX5_CAP_GEN(mdev
, apm
))
564 props
->device_cap_flags
|= IB_DEVICE_AUTO_PATH_MIG
;
565 if (MLX5_CAP_GEN(mdev
, xrc
))
566 props
->device_cap_flags
|= IB_DEVICE_XRC
;
567 if (MLX5_CAP_GEN(mdev
, imaicl
)) {
568 props
->device_cap_flags
|= IB_DEVICE_MEM_WINDOW
|
569 IB_DEVICE_MEM_WINDOW_TYPE_2B
;
570 props
->max_mw
= 1 << MLX5_CAP_GEN(mdev
, log_max_mkey
);
571 /* We support 'Gappy' memory registration too */
572 props
->device_cap_flags
|= IB_DEVICE_SG_GAPS_REG
;
574 props
->device_cap_flags
|= IB_DEVICE_MEM_MGT_EXTENSIONS
;
575 if (MLX5_CAP_GEN(mdev
, sho
)) {
576 props
->device_cap_flags
|= IB_DEVICE_SIGNATURE_HANDOVER
;
577 /* At this stage no support for signature handover */
578 props
->sig_prot_cap
= IB_PROT_T10DIF_TYPE_1
|
579 IB_PROT_T10DIF_TYPE_2
|
580 IB_PROT_T10DIF_TYPE_3
;
581 props
->sig_guard_cap
= IB_GUARD_T10DIF_CRC
|
582 IB_GUARD_T10DIF_CSUM
;
584 if (MLX5_CAP_GEN(mdev
, block_lb_mc
))
585 props
->device_cap_flags
|= IB_DEVICE_BLOCK_MULTICAST_LOOPBACK
;
587 if (MLX5_CAP_GEN(dev
->mdev
, eth_net_offloads
)) {
588 if (MLX5_CAP_ETH(mdev
, csum_cap
))
589 props
->device_cap_flags
|= IB_DEVICE_RAW_IP_CSUM
;
591 if (field_avail(typeof(resp
), tso_caps
, uhw
->outlen
)) {
592 max_tso
= MLX5_CAP_ETH(mdev
, max_lso_cap
);
594 resp
.tso_caps
.max_tso
= 1 << max_tso
;
595 resp
.tso_caps
.supported_qpts
|=
596 1 << IB_QPT_RAW_PACKET
;
597 resp
.response_length
+= sizeof(resp
.tso_caps
);
601 if (field_avail(typeof(resp
), rss_caps
, uhw
->outlen
)) {
602 resp
.rss_caps
.rx_hash_function
=
603 MLX5_RX_HASH_FUNC_TOEPLITZ
;
604 resp
.rss_caps
.rx_hash_fields_mask
=
605 MLX5_RX_HASH_SRC_IPV4
|
606 MLX5_RX_HASH_DST_IPV4
|
607 MLX5_RX_HASH_SRC_IPV6
|
608 MLX5_RX_HASH_DST_IPV6
|
609 MLX5_RX_HASH_SRC_PORT_TCP
|
610 MLX5_RX_HASH_DST_PORT_TCP
|
611 MLX5_RX_HASH_SRC_PORT_UDP
|
612 MLX5_RX_HASH_DST_PORT_UDP
;
613 resp
.response_length
+= sizeof(resp
.rss_caps
);
616 if (field_avail(typeof(resp
), tso_caps
, uhw
->outlen
))
617 resp
.response_length
+= sizeof(resp
.tso_caps
);
618 if (field_avail(typeof(resp
), rss_caps
, uhw
->outlen
))
619 resp
.response_length
+= sizeof(resp
.rss_caps
);
622 if (MLX5_CAP_GEN(mdev
, ipoib_basic_offloads
)) {
623 props
->device_cap_flags
|= IB_DEVICE_UD_IP_CSUM
;
624 props
->device_cap_flags
|= IB_DEVICE_UD_TSO
;
627 if (MLX5_CAP_GEN(dev
->mdev
, eth_net_offloads
) &&
628 MLX5_CAP_ETH(dev
->mdev
, scatter_fcs
))
629 props
->device_cap_flags
|= IB_DEVICE_RAW_SCATTER_FCS
;
631 if (mlx5_get_flow_namespace(dev
->mdev
, MLX5_FLOW_NAMESPACE_BYPASS
))
632 props
->device_cap_flags
|= IB_DEVICE_MANAGED_FLOW_STEERING
;
634 props
->vendor_part_id
= mdev
->pdev
->device
;
635 props
->hw_ver
= mdev
->pdev
->revision
;
637 props
->max_mr_size
= ~0ull;
638 props
->page_size_cap
= ~(min_page_size
- 1);
639 props
->max_qp
= 1 << MLX5_CAP_GEN(mdev
, log_max_qp
);
640 props
->max_qp_wr
= 1 << MLX5_CAP_GEN(mdev
, log_max_qp_sz
);
641 max_rq_sg
= MLX5_CAP_GEN(mdev
, max_wqe_sz_rq
) /
642 sizeof(struct mlx5_wqe_data_seg
);
643 max_sq_desc
= min_t(int, MLX5_CAP_GEN(mdev
, max_wqe_sz_sq
), 512);
644 max_sq_sg
= (max_sq_desc
- sizeof(struct mlx5_wqe_ctrl_seg
) -
645 sizeof(struct mlx5_wqe_raddr_seg
)) /
646 sizeof(struct mlx5_wqe_data_seg
);
647 props
->max_sge
= min(max_rq_sg
, max_sq_sg
);
648 props
->max_sge_rd
= MLX5_MAX_SGE_RD
;
649 props
->max_cq
= 1 << MLX5_CAP_GEN(mdev
, log_max_cq
);
650 props
->max_cqe
= (1 << MLX5_CAP_GEN(mdev
, log_max_cq_sz
)) - 1;
651 props
->max_mr
= 1 << MLX5_CAP_GEN(mdev
, log_max_mkey
);
652 props
->max_pd
= 1 << MLX5_CAP_GEN(mdev
, log_max_pd
);
653 props
->max_qp_rd_atom
= 1 << MLX5_CAP_GEN(mdev
, log_max_ra_req_qp
);
654 props
->max_qp_init_rd_atom
= 1 << MLX5_CAP_GEN(mdev
, log_max_ra_res_qp
);
655 props
->max_srq
= 1 << MLX5_CAP_GEN(mdev
, log_max_srq
);
656 props
->max_srq_wr
= (1 << MLX5_CAP_GEN(mdev
, log_max_srq_sz
)) - 1;
657 props
->local_ca_ack_delay
= MLX5_CAP_GEN(mdev
, local_ca_ack_delay
);
658 props
->max_res_rd_atom
= props
->max_qp_rd_atom
* props
->max_qp
;
659 props
->max_srq_sge
= max_rq_sg
- 1;
660 props
->max_fast_reg_page_list_len
=
661 1 << MLX5_CAP_GEN(mdev
, log_max_klm_list_size
);
662 get_atomic_caps(dev
, props
);
663 props
->masked_atomic_cap
= IB_ATOMIC_NONE
;
664 props
->max_mcast_grp
= 1 << MLX5_CAP_GEN(mdev
, log_max_mcg
);
665 props
->max_mcast_qp_attach
= MLX5_CAP_GEN(mdev
, max_qp_mcg
);
666 props
->max_total_mcast_qp_attach
= props
->max_mcast_qp_attach
*
667 props
->max_mcast_grp
;
668 props
->max_map_per_fmr
= INT_MAX
; /* no limit in ConnectIB */
669 props
->hca_core_clock
= MLX5_CAP_GEN(mdev
, device_frequency_khz
);
670 props
->timestamp_mask
= 0x7FFFFFFFFFFFFFFFULL
;
672 #ifdef CONFIG_INFINIBAND_ON_DEMAND_PAGING
673 if (MLX5_CAP_GEN(mdev
, pg
))
674 props
->device_cap_flags
|= IB_DEVICE_ON_DEMAND_PAGING
;
675 props
->odp_caps
= dev
->odp_caps
;
678 if (MLX5_CAP_GEN(mdev
, cd
))
679 props
->device_cap_flags
|= IB_DEVICE_CROSS_CHANNEL
;
681 if (!mlx5_core_is_pf(mdev
))
682 props
->device_cap_flags
|= IB_DEVICE_VIRTUAL_FUNCTION
;
684 if (mlx5_ib_port_link_layer(ibdev
, 1) ==
685 IB_LINK_LAYER_ETHERNET
) {
686 props
->rss_caps
.max_rwq_indirection_tables
=
687 1 << MLX5_CAP_GEN(dev
->mdev
, log_max_rqt
);
688 props
->rss_caps
.max_rwq_indirection_table_size
=
689 1 << MLX5_CAP_GEN(dev
->mdev
, log_max_rqt_size
);
690 props
->rss_caps
.supported_qpts
= 1 << IB_QPT_RAW_PACKET
;
691 props
->max_wq_type_rq
=
692 1 << MLX5_CAP_GEN(dev
->mdev
, log_max_rq
);
696 err
= ib_copy_to_udata(uhw
, &resp
, resp
.response_length
);
706 MLX5_IB_WIDTH_1X
= 1 << 0,
707 MLX5_IB_WIDTH_2X
= 1 << 1,
708 MLX5_IB_WIDTH_4X
= 1 << 2,
709 MLX5_IB_WIDTH_8X
= 1 << 3,
710 MLX5_IB_WIDTH_12X
= 1 << 4
713 static void translate_active_width(struct ib_device
*ibdev
, u8 active_width
,
716 struct mlx5_ib_dev
*dev
= to_mdev(ibdev
);
718 if (active_width
& MLX5_IB_WIDTH_1X
)
719 *ib_width
= IB_WIDTH_1X
;
720 else if (active_width
& MLX5_IB_WIDTH_4X
)
721 *ib_width
= IB_WIDTH_4X
;
722 else if (active_width
& MLX5_IB_WIDTH_8X
)
723 *ib_width
= IB_WIDTH_8X
;
724 else if (active_width
& MLX5_IB_WIDTH_12X
)
725 *ib_width
= IB_WIDTH_12X
;
727 mlx5_ib_dbg(dev
, "Invalid active_width %d, setting width to default value: 4x\n",
729 *ib_width
= IB_WIDTH_4X
;
735 static int mlx5_mtu_to_ib_mtu(int mtu
)
744 pr_warn("invalid mtu\n");
754 __IB_MAX_VL_0_14
= 5,
757 enum mlx5_vl_hw_cap
{
769 static int translate_max_vl_num(struct ib_device
*ibdev
, u8 vl_hw_cap
,
774 *max_vl_num
= __IB_MAX_VL_0
;
777 *max_vl_num
= __IB_MAX_VL_0_1
;
780 *max_vl_num
= __IB_MAX_VL_0_3
;
783 *max_vl_num
= __IB_MAX_VL_0_7
;
785 case MLX5_VL_HW_0_14
:
786 *max_vl_num
= __IB_MAX_VL_0_14
;
796 static int mlx5_query_hca_port(struct ib_device
*ibdev
, u8 port
,
797 struct ib_port_attr
*props
)
799 struct mlx5_ib_dev
*dev
= to_mdev(ibdev
);
800 struct mlx5_core_dev
*mdev
= dev
->mdev
;
801 struct mlx5_hca_vport_context
*rep
;
805 u8 ib_link_width_oper
;
808 rep
= kzalloc(sizeof(*rep
), GFP_KERNEL
);
814 memset(props
, 0, sizeof(*props
));
816 err
= mlx5_query_hca_vport_context(mdev
, 0, port
, 0, rep
);
820 props
->lid
= rep
->lid
;
821 props
->lmc
= rep
->lmc
;
822 props
->sm_lid
= rep
->sm_lid
;
823 props
->sm_sl
= rep
->sm_sl
;
824 props
->state
= rep
->vport_state
;
825 props
->phys_state
= rep
->port_physical_state
;
826 props
->port_cap_flags
= rep
->cap_mask1
;
827 props
->gid_tbl_len
= mlx5_get_gid_table_len(MLX5_CAP_GEN(mdev
, gid_table_size
));
828 props
->max_msg_sz
= 1 << MLX5_CAP_GEN(mdev
, log_max_msg
);
829 props
->pkey_tbl_len
= mlx5_to_sw_pkey_sz(MLX5_CAP_GEN(mdev
, pkey_table_size
));
830 props
->bad_pkey_cntr
= rep
->pkey_violation_counter
;
831 props
->qkey_viol_cntr
= rep
->qkey_violation_counter
;
832 props
->subnet_timeout
= rep
->subnet_timeout
;
833 props
->init_type_reply
= rep
->init_type_reply
;
834 props
->grh_required
= rep
->grh_required
;
836 err
= mlx5_query_port_link_width_oper(mdev
, &ib_link_width_oper
, port
);
840 translate_active_width(ibdev
, ib_link_width_oper
, &props
->active_width
);
842 err
= mlx5_query_port_ib_proto_oper(mdev
, &props
->active_speed
, port
);
846 mlx5_query_port_max_mtu(mdev
, &max_mtu
, port
);
848 props
->max_mtu
= mlx5_mtu_to_ib_mtu(max_mtu
);
850 mlx5_query_port_oper_mtu(mdev
, &oper_mtu
, port
);
852 props
->active_mtu
= mlx5_mtu_to_ib_mtu(oper_mtu
);
854 err
= mlx5_query_port_vl_hw_cap(mdev
, &vl_hw_cap
, port
);
858 err
= translate_max_vl_num(ibdev
, vl_hw_cap
,
865 int mlx5_ib_query_port(struct ib_device
*ibdev
, u8 port
,
866 struct ib_port_attr
*props
)
868 switch (mlx5_get_vport_access_method(ibdev
)) {
869 case MLX5_VPORT_ACCESS_METHOD_MAD
:
870 return mlx5_query_mad_ifc_port(ibdev
, port
, props
);
872 case MLX5_VPORT_ACCESS_METHOD_HCA
:
873 return mlx5_query_hca_port(ibdev
, port
, props
);
875 case MLX5_VPORT_ACCESS_METHOD_NIC
:
876 return mlx5_query_port_roce(ibdev
, port
, props
);
883 static int mlx5_ib_query_gid(struct ib_device
*ibdev
, u8 port
, int index
,
886 struct mlx5_ib_dev
*dev
= to_mdev(ibdev
);
887 struct mlx5_core_dev
*mdev
= dev
->mdev
;
889 switch (mlx5_get_vport_access_method(ibdev
)) {
890 case MLX5_VPORT_ACCESS_METHOD_MAD
:
891 return mlx5_query_mad_ifc_gids(ibdev
, port
, index
, gid
);
893 case MLX5_VPORT_ACCESS_METHOD_HCA
:
894 return mlx5_query_hca_vport_gid(mdev
, 0, port
, 0, index
, gid
);
902 static int mlx5_ib_query_pkey(struct ib_device
*ibdev
, u8 port
, u16 index
,
905 struct mlx5_ib_dev
*dev
= to_mdev(ibdev
);
906 struct mlx5_core_dev
*mdev
= dev
->mdev
;
908 switch (mlx5_get_vport_access_method(ibdev
)) {
909 case MLX5_VPORT_ACCESS_METHOD_MAD
:
910 return mlx5_query_mad_ifc_pkey(ibdev
, port
, index
, pkey
);
912 case MLX5_VPORT_ACCESS_METHOD_HCA
:
913 case MLX5_VPORT_ACCESS_METHOD_NIC
:
914 return mlx5_query_hca_vport_pkey(mdev
, 0, port
, 0, index
,
921 static int mlx5_ib_modify_device(struct ib_device
*ibdev
, int mask
,
922 struct ib_device_modify
*props
)
924 struct mlx5_ib_dev
*dev
= to_mdev(ibdev
);
925 struct mlx5_reg_node_desc in
;
926 struct mlx5_reg_node_desc out
;
929 if (mask
& ~IB_DEVICE_MODIFY_NODE_DESC
)
932 if (!(mask
& IB_DEVICE_MODIFY_NODE_DESC
))
936 * If possible, pass node desc to FW, so it can generate
937 * a 144 trap. If cmd fails, just ignore.
939 memcpy(&in
, props
->node_desc
, IB_DEVICE_NODE_DESC_MAX
);
940 err
= mlx5_core_access_reg(dev
->mdev
, &in
, sizeof(in
), &out
,
941 sizeof(out
), MLX5_REG_NODE_DESC
, 0, 1);
945 memcpy(ibdev
->node_desc
, props
->node_desc
, IB_DEVICE_NODE_DESC_MAX
);
950 static int mlx5_ib_modify_port(struct ib_device
*ibdev
, u8 port
, int mask
,
951 struct ib_port_modify
*props
)
953 struct mlx5_ib_dev
*dev
= to_mdev(ibdev
);
954 struct ib_port_attr attr
;
958 mutex_lock(&dev
->cap_mask_mutex
);
960 err
= mlx5_ib_query_port(ibdev
, port
, &attr
);
964 tmp
= (attr
.port_cap_flags
| props
->set_port_cap_mask
) &
965 ~props
->clr_port_cap_mask
;
967 err
= mlx5_set_port_caps(dev
->mdev
, port
, tmp
);
970 mutex_unlock(&dev
->cap_mask_mutex
);
974 static struct ib_ucontext
*mlx5_ib_alloc_ucontext(struct ib_device
*ibdev
,
975 struct ib_udata
*udata
)
977 struct mlx5_ib_dev
*dev
= to_mdev(ibdev
);
978 struct mlx5_ib_alloc_ucontext_req_v2 req
= {};
979 struct mlx5_ib_alloc_ucontext_resp resp
= {};
980 struct mlx5_ib_ucontext
*context
;
981 struct mlx5_uuar_info
*uuari
;
982 struct mlx5_uar
*uars
;
990 size_t min_req_v2
= offsetof(struct mlx5_ib_alloc_ucontext_req_v2
,
994 return ERR_PTR(-EAGAIN
);
996 if (udata
->inlen
< sizeof(struct ib_uverbs_cmd_hdr
))
997 return ERR_PTR(-EINVAL
);
999 reqlen
= udata
->inlen
- sizeof(struct ib_uverbs_cmd_hdr
);
1000 if (reqlen
== sizeof(struct mlx5_ib_alloc_ucontext_req
))
1002 else if (reqlen
>= min_req_v2
)
1005 return ERR_PTR(-EINVAL
);
1007 err
= ib_copy_from_udata(&req
, udata
, min(reqlen
, sizeof(req
)));
1009 return ERR_PTR(err
);
1012 return ERR_PTR(-EINVAL
);
1014 if (req
.total_num_uuars
> MLX5_MAX_UUARS
)
1015 return ERR_PTR(-ENOMEM
);
1017 if (req
.total_num_uuars
== 0)
1018 return ERR_PTR(-EINVAL
);
1020 if (req
.comp_mask
|| req
.reserved0
|| req
.reserved1
|| req
.reserved2
)
1021 return ERR_PTR(-EOPNOTSUPP
);
1023 if (reqlen
> sizeof(req
) &&
1024 !ib_is_udata_cleared(udata
, sizeof(req
),
1025 reqlen
- sizeof(req
)))
1026 return ERR_PTR(-EOPNOTSUPP
);
1028 req
.total_num_uuars
= ALIGN(req
.total_num_uuars
,
1029 MLX5_NON_FP_BF_REGS_PER_PAGE
);
1030 if (req
.num_low_latency_uuars
> req
.total_num_uuars
- 1)
1031 return ERR_PTR(-EINVAL
);
1033 num_uars
= req
.total_num_uuars
/ MLX5_NON_FP_BF_REGS_PER_PAGE
;
1034 gross_uuars
= num_uars
* MLX5_BF_REGS_PER_PAGE
;
1035 resp
.qp_tab_size
= 1 << MLX5_CAP_GEN(dev
->mdev
, log_max_qp
);
1036 if (mlx5_core_is_pf(dev
->mdev
) && MLX5_CAP_GEN(dev
->mdev
, bf
))
1037 resp
.bf_reg_size
= 1 << MLX5_CAP_GEN(dev
->mdev
, log_bf_reg_size
);
1038 resp
.cache_line_size
= cache_line_size();
1039 resp
.max_sq_desc_sz
= MLX5_CAP_GEN(dev
->mdev
, max_wqe_sz_sq
);
1040 resp
.max_rq_desc_sz
= MLX5_CAP_GEN(dev
->mdev
, max_wqe_sz_rq
);
1041 resp
.max_send_wqebb
= 1 << MLX5_CAP_GEN(dev
->mdev
, log_max_qp_sz
);
1042 resp
.max_recv_wr
= 1 << MLX5_CAP_GEN(dev
->mdev
, log_max_qp_sz
);
1043 resp
.max_srq_recv_wr
= 1 << MLX5_CAP_GEN(dev
->mdev
, log_max_srq_sz
);
1044 resp
.cqe_version
= min_t(__u8
,
1045 (__u8
)MLX5_CAP_GEN(dev
->mdev
, cqe_version
),
1046 req
.max_cqe_version
);
1047 resp
.response_length
= min(offsetof(typeof(resp
), response_length
) +
1048 sizeof(resp
.response_length
), udata
->outlen
);
1050 context
= kzalloc(sizeof(*context
), GFP_KERNEL
);
1052 return ERR_PTR(-ENOMEM
);
1054 uuari
= &context
->uuari
;
1055 mutex_init(&uuari
->lock
);
1056 uars
= kcalloc(num_uars
, sizeof(*uars
), GFP_KERNEL
);
1062 uuari
->bitmap
= kcalloc(BITS_TO_LONGS(gross_uuars
),
1063 sizeof(*uuari
->bitmap
),
1065 if (!uuari
->bitmap
) {
1070 * clear all fast path uuars
1072 for (i
= 0; i
< gross_uuars
; i
++) {
1074 if (uuarn
== 2 || uuarn
== 3)
1075 set_bit(i
, uuari
->bitmap
);
1078 uuari
->count
= kcalloc(gross_uuars
, sizeof(*uuari
->count
), GFP_KERNEL
);
1079 if (!uuari
->count
) {
1084 for (i
= 0; i
< num_uars
; i
++) {
1085 err
= mlx5_cmd_alloc_uar(dev
->mdev
, &uars
[i
].index
);
1090 #ifdef CONFIG_INFINIBAND_ON_DEMAND_PAGING
1091 context
->ibucontext
.invalidate_range
= &mlx5_ib_invalidate_range
;
1094 if (MLX5_CAP_GEN(dev
->mdev
, log_max_transport_domain
)) {
1095 err
= mlx5_core_alloc_transport_domain(dev
->mdev
,
1101 INIT_LIST_HEAD(&context
->vma_private_list
);
1102 INIT_LIST_HEAD(&context
->db_page_list
);
1103 mutex_init(&context
->db_page_mutex
);
1105 resp
.tot_uuars
= req
.total_num_uuars
;
1106 resp
.num_ports
= MLX5_CAP_GEN(dev
->mdev
, num_ports
);
1108 if (field_avail(typeof(resp
), cqe_version
, udata
->outlen
))
1109 resp
.response_length
+= sizeof(resp
.cqe_version
);
1111 if (field_avail(typeof(resp
), cmds_supp_uhw
, udata
->outlen
)) {
1112 resp
.cmds_supp_uhw
|= MLX5_USER_CMDS_SUPP_UHW_QUERY_DEVICE
;
1113 resp
.response_length
+= sizeof(resp
.cmds_supp_uhw
);
1117 * We don't want to expose information from the PCI bar that is located
1118 * after 4096 bytes, so if the arch only supports larger pages, let's
1119 * pretend we don't support reading the HCA's core clock. This is also
1120 * forced by mmap function.
1122 if (field_avail(typeof(resp
), hca_core_clock_offset
, udata
->outlen
)) {
1123 if (PAGE_SIZE
<= 4096) {
1125 MLX5_IB_ALLOC_UCONTEXT_RESP_MASK_CORE_CLOCK_OFFSET
;
1126 resp
.hca_core_clock_offset
=
1127 offsetof(struct mlx5_init_seg
, internal_timer_h
) % PAGE_SIZE
;
1129 resp
.response_length
+= sizeof(resp
.hca_core_clock_offset
) +
1130 sizeof(resp
.reserved2
);
1133 err
= ib_copy_to_udata(udata
, &resp
, resp
.response_length
);
1138 uuari
->num_low_latency_uuars
= req
.num_low_latency_uuars
;
1140 uuari
->num_uars
= num_uars
;
1141 context
->cqe_version
= resp
.cqe_version
;
1143 return &context
->ibucontext
;
1146 if (MLX5_CAP_GEN(dev
->mdev
, log_max_transport_domain
))
1147 mlx5_core_dealloc_transport_domain(dev
->mdev
, context
->tdn
);
1150 for (i
--; i
>= 0; i
--)
1151 mlx5_cmd_free_uar(dev
->mdev
, uars
[i
].index
);
1153 kfree(uuari
->count
);
1156 kfree(uuari
->bitmap
);
1163 return ERR_PTR(err
);
1166 static int mlx5_ib_dealloc_ucontext(struct ib_ucontext
*ibcontext
)
1168 struct mlx5_ib_ucontext
*context
= to_mucontext(ibcontext
);
1169 struct mlx5_ib_dev
*dev
= to_mdev(ibcontext
->device
);
1170 struct mlx5_uuar_info
*uuari
= &context
->uuari
;
1173 if (MLX5_CAP_GEN(dev
->mdev
, log_max_transport_domain
))
1174 mlx5_core_dealloc_transport_domain(dev
->mdev
, context
->tdn
);
1176 for (i
= 0; i
< uuari
->num_uars
; i
++) {
1177 if (mlx5_cmd_free_uar(dev
->mdev
, uuari
->uars
[i
].index
))
1178 mlx5_ib_warn(dev
, "failed to free UAR 0x%x\n", uuari
->uars
[i
].index
);
1181 kfree(uuari
->count
);
1182 kfree(uuari
->bitmap
);
1189 static phys_addr_t
uar_index2pfn(struct mlx5_ib_dev
*dev
, int index
)
1191 return (pci_resource_start(dev
->mdev
->pdev
, 0) >> PAGE_SHIFT
) + index
;
1194 static int get_command(unsigned long offset
)
1196 return (offset
>> MLX5_IB_MMAP_CMD_SHIFT
) & MLX5_IB_MMAP_CMD_MASK
;
1199 static int get_arg(unsigned long offset
)
1201 return offset
& ((1 << MLX5_IB_MMAP_CMD_SHIFT
) - 1);
1204 static int get_index(unsigned long offset
)
1206 return get_arg(offset
);
1209 static void mlx5_ib_vma_open(struct vm_area_struct
*area
)
1211 /* vma_open is called when a new VMA is created on top of our VMA. This
1212 * is done through either mremap flow or split_vma (usually due to
1213 * mlock, madvise, munmap, etc.) We do not support a clone of the VMA,
1214 * as this VMA is strongly hardware related. Therefore we set the
1215 * vm_ops of the newly created/cloned VMA to NULL, to prevent it from
1216 * calling us again and trying to do incorrect actions. We assume that
1217 * the original VMA size is exactly a single page, and therefore all
1218 * "splitting" operation will not happen to it.
1220 area
->vm_ops
= NULL
;
1223 static void mlx5_ib_vma_close(struct vm_area_struct
*area
)
1225 struct mlx5_ib_vma_private_data
*mlx5_ib_vma_priv_data
;
1227 /* It's guaranteed that all VMAs opened on a FD are closed before the
1228 * file itself is closed, therefore no sync is needed with the regular
1229 * closing flow. (e.g. mlx5 ib_dealloc_ucontext)
1230 * However need a sync with accessing the vma as part of
1231 * mlx5_ib_disassociate_ucontext.
1232 * The close operation is usually called under mm->mmap_sem except when
1233 * process is exiting.
1234 * The exiting case is handled explicitly as part of
1235 * mlx5_ib_disassociate_ucontext.
1237 mlx5_ib_vma_priv_data
= (struct mlx5_ib_vma_private_data
*)area
->vm_private_data
;
1239 /* setting the vma context pointer to null in the mlx5_ib driver's
1240 * private data, to protect a race condition in
1241 * mlx5_ib_disassociate_ucontext().
1243 mlx5_ib_vma_priv_data
->vma
= NULL
;
1244 list_del(&mlx5_ib_vma_priv_data
->list
);
1245 kfree(mlx5_ib_vma_priv_data
);
1248 static const struct vm_operations_struct mlx5_ib_vm_ops
= {
1249 .open
= mlx5_ib_vma_open
,
1250 .close
= mlx5_ib_vma_close
1253 static int mlx5_ib_set_vma_data(struct vm_area_struct
*vma
,
1254 struct mlx5_ib_ucontext
*ctx
)
1256 struct mlx5_ib_vma_private_data
*vma_prv
;
1257 struct list_head
*vma_head
= &ctx
->vma_private_list
;
1259 vma_prv
= kzalloc(sizeof(*vma_prv
), GFP_KERNEL
);
1264 vma
->vm_private_data
= vma_prv
;
1265 vma
->vm_ops
= &mlx5_ib_vm_ops
;
1267 list_add(&vma_prv
->list
, vma_head
);
1272 static void mlx5_ib_disassociate_ucontext(struct ib_ucontext
*ibcontext
)
1275 struct vm_area_struct
*vma
;
1276 struct mlx5_ib_vma_private_data
*vma_private
, *n
;
1277 struct mlx5_ib_ucontext
*context
= to_mucontext(ibcontext
);
1278 struct task_struct
*owning_process
= NULL
;
1279 struct mm_struct
*owning_mm
= NULL
;
1281 owning_process
= get_pid_task(ibcontext
->tgid
, PIDTYPE_PID
);
1282 if (!owning_process
)
1285 owning_mm
= get_task_mm(owning_process
);
1287 pr_info("no mm, disassociate ucontext is pending task termination\n");
1289 put_task_struct(owning_process
);
1290 usleep_range(1000, 2000);
1291 owning_process
= get_pid_task(ibcontext
->tgid
,
1293 if (!owning_process
||
1294 owning_process
->state
== TASK_DEAD
) {
1295 pr_info("disassociate ucontext done, task was terminated\n");
1296 /* in case task was dead need to release the
1300 put_task_struct(owning_process
);
1306 /* need to protect from a race on closing the vma as part of
1307 * mlx5_ib_vma_close.
1309 down_write(&owning_mm
->mmap_sem
);
1310 if (!mmget_still_valid(owning_mm
))
1312 list_for_each_entry_safe(vma_private
, n
, &context
->vma_private_list
,
1314 vma
= vma_private
->vma
;
1315 ret
= zap_vma_ptes(vma
, vma
->vm_start
,
1317 WARN_ONCE(ret
, "%s: zap_vma_ptes failed", __func__
);
1318 /* context going to be destroyed, should
1319 * not access ops any more.
1321 vma
->vm_flags
&= ~(VM_SHARED
| VM_MAYSHARE
);
1323 list_del(&vma_private
->list
);
1327 up_write(&owning_mm
->mmap_sem
);
1329 put_task_struct(owning_process
);
1332 static inline char *mmap_cmd2str(enum mlx5_ib_mmap_cmd cmd
)
1335 case MLX5_IB_MMAP_WC_PAGE
:
1337 case MLX5_IB_MMAP_REGULAR_PAGE
:
1338 return "best effort WC";
1339 case MLX5_IB_MMAP_NC_PAGE
:
1346 static int uar_mmap(struct mlx5_ib_dev
*dev
, enum mlx5_ib_mmap_cmd cmd
,
1347 struct vm_area_struct
*vma
,
1348 struct mlx5_ib_ucontext
*context
)
1350 struct mlx5_uuar_info
*uuari
= &context
->uuari
;
1353 phys_addr_t pfn
, pa
;
1357 case MLX5_IB_MMAP_WC_PAGE
:
1358 /* Some architectures don't support WC memory */
1359 #if defined(CONFIG_X86)
1362 #elif !(defined(CONFIG_PPC) || (defined(CONFIG_ARM) && defined(CONFIG_MMU)))
1366 case MLX5_IB_MMAP_REGULAR_PAGE
:
1367 /* For MLX5_IB_MMAP_REGULAR_PAGE do the best effort to get WC */
1368 prot
= pgprot_writecombine(vma
->vm_page_prot
);
1370 case MLX5_IB_MMAP_NC_PAGE
:
1371 prot
= pgprot_noncached(vma
->vm_page_prot
);
1377 if (vma
->vm_end
- vma
->vm_start
!= PAGE_SIZE
)
1380 idx
= get_index(vma
->vm_pgoff
);
1381 if (idx
>= uuari
->num_uars
)
1384 pfn
= uar_index2pfn(dev
, uuari
->uars
[idx
].index
);
1385 mlx5_ib_dbg(dev
, "uar idx 0x%lx, pfn %pa\n", idx
, &pfn
);
1387 vma
->vm_page_prot
= prot
;
1388 err
= io_remap_pfn_range(vma
, vma
->vm_start
, pfn
,
1389 PAGE_SIZE
, vma
->vm_page_prot
);
1391 mlx5_ib_err(dev
, "io_remap_pfn_range failed with error=%d, vm_start=0x%lx, pfn=%pa, mmap_cmd=%s\n",
1392 err
, vma
->vm_start
, &pfn
, mmap_cmd2str(cmd
));
1396 pa
= pfn
<< PAGE_SHIFT
;
1397 mlx5_ib_dbg(dev
, "mapped %s at 0x%lx, PA %pa\n", mmap_cmd2str(cmd
),
1398 vma
->vm_start
, &pa
);
1400 return mlx5_ib_set_vma_data(vma
, context
);
1403 static int mlx5_ib_mmap(struct ib_ucontext
*ibcontext
, struct vm_area_struct
*vma
)
1405 struct mlx5_ib_ucontext
*context
= to_mucontext(ibcontext
);
1406 struct mlx5_ib_dev
*dev
= to_mdev(ibcontext
->device
);
1407 unsigned long command
;
1410 command
= get_command(vma
->vm_pgoff
);
1412 case MLX5_IB_MMAP_WC_PAGE
:
1413 case MLX5_IB_MMAP_NC_PAGE
:
1414 case MLX5_IB_MMAP_REGULAR_PAGE
:
1415 return uar_mmap(dev
, command
, vma
, context
);
1417 case MLX5_IB_MMAP_GET_CONTIGUOUS_PAGES
:
1420 case MLX5_IB_MMAP_CORE_CLOCK
:
1421 if (vma
->vm_end
- vma
->vm_start
!= PAGE_SIZE
)
1424 if (vma
->vm_flags
& VM_WRITE
)
1427 /* Don't expose to user-space information it shouldn't have */
1428 if (PAGE_SIZE
> 4096)
1431 vma
->vm_page_prot
= pgprot_noncached(vma
->vm_page_prot
);
1432 pfn
= (dev
->mdev
->iseg_base
+
1433 offsetof(struct mlx5_init_seg
, internal_timer_h
)) >>
1435 if (io_remap_pfn_range(vma
, vma
->vm_start
, pfn
,
1436 PAGE_SIZE
, vma
->vm_page_prot
))
1439 mlx5_ib_dbg(dev
, "mapped internal timer at 0x%lx, PA 0x%llx\n",
1441 (unsigned long long)pfn
<< PAGE_SHIFT
);
1451 static struct ib_pd
*mlx5_ib_alloc_pd(struct ib_device
*ibdev
,
1452 struct ib_ucontext
*context
,
1453 struct ib_udata
*udata
)
1455 struct mlx5_ib_alloc_pd_resp resp
;
1456 struct mlx5_ib_pd
*pd
;
1459 pd
= kmalloc(sizeof(*pd
), GFP_KERNEL
);
1461 return ERR_PTR(-ENOMEM
);
1463 err
= mlx5_core_alloc_pd(to_mdev(ibdev
)->mdev
, &pd
->pdn
);
1466 return ERR_PTR(err
);
1471 if (ib_copy_to_udata(udata
, &resp
, sizeof(resp
))) {
1472 mlx5_core_dealloc_pd(to_mdev(ibdev
)->mdev
, pd
->pdn
);
1474 return ERR_PTR(-EFAULT
);
1481 static int mlx5_ib_dealloc_pd(struct ib_pd
*pd
)
1483 struct mlx5_ib_dev
*mdev
= to_mdev(pd
->device
);
1484 struct mlx5_ib_pd
*mpd
= to_mpd(pd
);
1486 mlx5_core_dealloc_pd(mdev
->mdev
, mpd
->pdn
);
1493 MATCH_CRITERIA_ENABLE_OUTER_BIT
,
1494 MATCH_CRITERIA_ENABLE_MISC_BIT
,
1495 MATCH_CRITERIA_ENABLE_INNER_BIT
1498 #define HEADER_IS_ZERO(match_criteria, headers) \
1499 !(memchr_inv(MLX5_ADDR_OF(fte_match_param, match_criteria, headers), \
1500 0, MLX5_FLD_SZ_BYTES(fte_match_param, headers))) \
1502 static u8 get_match_criteria_enable(u32 *match_criteria)
1504 u8 match_criteria_enable
;
1506 match_criteria_enable
=
1507 (!HEADER_IS_ZERO(match_criteria
, outer_headers
)) <<
1508 MATCH_CRITERIA_ENABLE_OUTER_BIT
;
1509 match_criteria_enable
|=
1510 (!HEADER_IS_ZERO(match_criteria
, misc_parameters
)) <<
1511 MATCH_CRITERIA_ENABLE_MISC_BIT
;
1512 match_criteria_enable
|=
1513 (!HEADER_IS_ZERO(match_criteria
, inner_headers
)) <<
1514 MATCH_CRITERIA_ENABLE_INNER_BIT
;
1516 return match_criteria_enable
;
1519 static void set_proto(void *outer_c
, void *outer_v
, u8 mask
, u8 val
)
1521 MLX5_SET(fte_match_set_lyr_2_4
, outer_c
, ip_protocol
, mask
);
1522 MLX5_SET(fte_match_set_lyr_2_4
, outer_v
, ip_protocol
, val
);
1525 static void set_tos(void *outer_c
, void *outer_v
, u8 mask
, u8 val
)
1527 MLX5_SET(fte_match_set_lyr_2_4
, outer_c
, ip_ecn
, mask
);
1528 MLX5_SET(fte_match_set_lyr_2_4
, outer_v
, ip_ecn
, val
);
1529 MLX5_SET(fte_match_set_lyr_2_4
, outer_c
, ip_dscp
, mask
>> 2);
1530 MLX5_SET(fte_match_set_lyr_2_4
, outer_v
, ip_dscp
, val
>> 2);
1533 #define LAST_ETH_FIELD vlan_tag
1534 #define LAST_IB_FIELD sl
1535 #define LAST_IPV4_FIELD tos
1536 #define LAST_IPV6_FIELD traffic_class
1537 #define LAST_TCP_UDP_FIELD src_port
1539 /* Field is the last supported field */
1540 #define FIELDS_NOT_SUPPORTED(filter, field)\
1541 memchr_inv((void *)&filter.field +\
1542 sizeof(filter.field), 0,\
1544 offsetof(typeof(filter), field) -\
1545 sizeof(filter.field))
1547 static int parse_flow_attr(u32
*match_c
, u32
*match_v
,
1548 const union ib_flow_spec
*ib_spec
)
1550 void *outer_headers_c
= MLX5_ADDR_OF(fte_match_param
, match_c
,
1552 void *outer_headers_v
= MLX5_ADDR_OF(fte_match_param
, match_v
,
1554 void *misc_params_c
= MLX5_ADDR_OF(fte_match_param
, match_c
,
1556 void *misc_params_v
= MLX5_ADDR_OF(fte_match_param
, match_v
,
1559 switch (ib_spec
->type
) {
1560 case IB_FLOW_SPEC_ETH
:
1561 if (FIELDS_NOT_SUPPORTED(ib_spec
->eth
.mask
, LAST_ETH_FIELD
))
1564 ether_addr_copy(MLX5_ADDR_OF(fte_match_set_lyr_2_4
, outer_headers_c
,
1566 ib_spec
->eth
.mask
.dst_mac
);
1567 ether_addr_copy(MLX5_ADDR_OF(fte_match_set_lyr_2_4
, outer_headers_v
,
1569 ib_spec
->eth
.val
.dst_mac
);
1571 ether_addr_copy(MLX5_ADDR_OF(fte_match_set_lyr_2_4
, outer_headers_c
,
1573 ib_spec
->eth
.mask
.src_mac
);
1574 ether_addr_copy(MLX5_ADDR_OF(fte_match_set_lyr_2_4
, outer_headers_v
,
1576 ib_spec
->eth
.val
.src_mac
);
1578 if (ib_spec
->eth
.mask
.vlan_tag
) {
1579 MLX5_SET(fte_match_set_lyr_2_4
, outer_headers_c
,
1581 MLX5_SET(fte_match_set_lyr_2_4
, outer_headers_v
,
1584 MLX5_SET(fte_match_set_lyr_2_4
, outer_headers_c
,
1585 first_vid
, ntohs(ib_spec
->eth
.mask
.vlan_tag
));
1586 MLX5_SET(fte_match_set_lyr_2_4
, outer_headers_v
,
1587 first_vid
, ntohs(ib_spec
->eth
.val
.vlan_tag
));
1589 MLX5_SET(fte_match_set_lyr_2_4
, outer_headers_c
,
1591 ntohs(ib_spec
->eth
.mask
.vlan_tag
) >> 12);
1592 MLX5_SET(fte_match_set_lyr_2_4
, outer_headers_v
,
1594 ntohs(ib_spec
->eth
.val
.vlan_tag
) >> 12);
1596 MLX5_SET(fte_match_set_lyr_2_4
, outer_headers_c
,
1598 ntohs(ib_spec
->eth
.mask
.vlan_tag
) >> 13);
1599 MLX5_SET(fte_match_set_lyr_2_4
, outer_headers_v
,
1601 ntohs(ib_spec
->eth
.val
.vlan_tag
) >> 13);
1603 MLX5_SET(fte_match_set_lyr_2_4
, outer_headers_c
,
1604 ethertype
, ntohs(ib_spec
->eth
.mask
.ether_type
));
1605 MLX5_SET(fte_match_set_lyr_2_4
, outer_headers_v
,
1606 ethertype
, ntohs(ib_spec
->eth
.val
.ether_type
));
1608 case IB_FLOW_SPEC_IPV4
:
1609 if (FIELDS_NOT_SUPPORTED(ib_spec
->ipv4
.mask
, LAST_IPV4_FIELD
))
1612 MLX5_SET(fte_match_set_lyr_2_4
, outer_headers_c
,
1614 MLX5_SET(fte_match_set_lyr_2_4
, outer_headers_v
,
1615 ethertype
, ETH_P_IP
);
1617 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4
, outer_headers_c
,
1618 src_ipv4_src_ipv6
.ipv4_layout
.ipv4
),
1619 &ib_spec
->ipv4
.mask
.src_ip
,
1620 sizeof(ib_spec
->ipv4
.mask
.src_ip
));
1621 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4
, outer_headers_v
,
1622 src_ipv4_src_ipv6
.ipv4_layout
.ipv4
),
1623 &ib_spec
->ipv4
.val
.src_ip
,
1624 sizeof(ib_spec
->ipv4
.val
.src_ip
));
1625 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4
, outer_headers_c
,
1626 dst_ipv4_dst_ipv6
.ipv4_layout
.ipv4
),
1627 &ib_spec
->ipv4
.mask
.dst_ip
,
1628 sizeof(ib_spec
->ipv4
.mask
.dst_ip
));
1629 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4
, outer_headers_v
,
1630 dst_ipv4_dst_ipv6
.ipv4_layout
.ipv4
),
1631 &ib_spec
->ipv4
.val
.dst_ip
,
1632 sizeof(ib_spec
->ipv4
.val
.dst_ip
));
1634 set_tos(outer_headers_c
, outer_headers_v
,
1635 ib_spec
->ipv4
.mask
.tos
, ib_spec
->ipv4
.val
.tos
);
1637 set_proto(outer_headers_c
, outer_headers_v
,
1638 ib_spec
->ipv4
.mask
.proto
, ib_spec
->ipv4
.val
.proto
);
1640 case IB_FLOW_SPEC_IPV6
:
1641 if (FIELDS_NOT_SUPPORTED(ib_spec
->ipv6
.mask
, LAST_IPV6_FIELD
))
1644 MLX5_SET(fte_match_set_lyr_2_4
, outer_headers_c
,
1646 MLX5_SET(fte_match_set_lyr_2_4
, outer_headers_v
,
1647 ethertype
, ETH_P_IPV6
);
1649 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4
, outer_headers_c
,
1650 src_ipv4_src_ipv6
.ipv6_layout
.ipv6
),
1651 &ib_spec
->ipv6
.mask
.src_ip
,
1652 sizeof(ib_spec
->ipv6
.mask
.src_ip
));
1653 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4
, outer_headers_v
,
1654 src_ipv4_src_ipv6
.ipv6_layout
.ipv6
),
1655 &ib_spec
->ipv6
.val
.src_ip
,
1656 sizeof(ib_spec
->ipv6
.val
.src_ip
));
1657 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4
, outer_headers_c
,
1658 dst_ipv4_dst_ipv6
.ipv6_layout
.ipv6
),
1659 &ib_spec
->ipv6
.mask
.dst_ip
,
1660 sizeof(ib_spec
->ipv6
.mask
.dst_ip
));
1661 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4
, outer_headers_v
,
1662 dst_ipv4_dst_ipv6
.ipv6_layout
.ipv6
),
1663 &ib_spec
->ipv6
.val
.dst_ip
,
1664 sizeof(ib_spec
->ipv6
.val
.dst_ip
));
1666 set_tos(outer_headers_c
, outer_headers_v
,
1667 ib_spec
->ipv6
.mask
.traffic_class
,
1668 ib_spec
->ipv6
.val
.traffic_class
);
1670 set_proto(outer_headers_c
, outer_headers_v
,
1671 ib_spec
->ipv6
.mask
.next_hdr
,
1672 ib_spec
->ipv6
.val
.next_hdr
);
1674 MLX5_SET(fte_match_set_misc
, misc_params_c
,
1675 outer_ipv6_flow_label
,
1676 ntohl(ib_spec
->ipv6
.mask
.flow_label
));
1677 MLX5_SET(fte_match_set_misc
, misc_params_v
,
1678 outer_ipv6_flow_label
,
1679 ntohl(ib_spec
->ipv6
.val
.flow_label
));
1681 case IB_FLOW_SPEC_TCP
:
1682 if (FIELDS_NOT_SUPPORTED(ib_spec
->tcp_udp
.mask
,
1683 LAST_TCP_UDP_FIELD
))
1686 MLX5_SET(fte_match_set_lyr_2_4
, outer_headers_c
, ip_protocol
,
1688 MLX5_SET(fte_match_set_lyr_2_4
, outer_headers_v
, ip_protocol
,
1691 MLX5_SET(fte_match_set_lyr_2_4
, outer_headers_c
, tcp_sport
,
1692 ntohs(ib_spec
->tcp_udp
.mask
.src_port
));
1693 MLX5_SET(fte_match_set_lyr_2_4
, outer_headers_v
, tcp_sport
,
1694 ntohs(ib_spec
->tcp_udp
.val
.src_port
));
1696 MLX5_SET(fte_match_set_lyr_2_4
, outer_headers_c
, tcp_dport
,
1697 ntohs(ib_spec
->tcp_udp
.mask
.dst_port
));
1698 MLX5_SET(fte_match_set_lyr_2_4
, outer_headers_v
, tcp_dport
,
1699 ntohs(ib_spec
->tcp_udp
.val
.dst_port
));
1701 case IB_FLOW_SPEC_UDP
:
1702 if (FIELDS_NOT_SUPPORTED(ib_spec
->tcp_udp
.mask
,
1703 LAST_TCP_UDP_FIELD
))
1706 MLX5_SET(fte_match_set_lyr_2_4
, outer_headers_c
, ip_protocol
,
1708 MLX5_SET(fte_match_set_lyr_2_4
, outer_headers_v
, ip_protocol
,
1711 MLX5_SET(fte_match_set_lyr_2_4
, outer_headers_c
, udp_sport
,
1712 ntohs(ib_spec
->tcp_udp
.mask
.src_port
));
1713 MLX5_SET(fte_match_set_lyr_2_4
, outer_headers_v
, udp_sport
,
1714 ntohs(ib_spec
->tcp_udp
.val
.src_port
));
1716 MLX5_SET(fte_match_set_lyr_2_4
, outer_headers_c
, udp_dport
,
1717 ntohs(ib_spec
->tcp_udp
.mask
.dst_port
));
1718 MLX5_SET(fte_match_set_lyr_2_4
, outer_headers_v
, udp_dport
,
1719 ntohs(ib_spec
->tcp_udp
.val
.dst_port
));
1728 /* If a flow could catch both multicast and unicast packets,
1729 * it won't fall into the multicast flow steering table and this rule
1730 * could steal other multicast packets.
1732 static bool flow_is_multicast_only(struct ib_flow_attr
*ib_attr
)
1734 struct ib_flow_spec_eth
*eth_spec
;
1736 if (ib_attr
->type
!= IB_FLOW_ATTR_NORMAL
||
1737 ib_attr
->size
< sizeof(struct ib_flow_attr
) +
1738 sizeof(struct ib_flow_spec_eth
) ||
1739 ib_attr
->num_of_specs
< 1)
1742 eth_spec
= (struct ib_flow_spec_eth
*)(ib_attr
+ 1);
1743 if (eth_spec
->type
!= IB_FLOW_SPEC_ETH
||
1744 eth_spec
->size
!= sizeof(*eth_spec
))
1747 return is_multicast_ether_addr(eth_spec
->mask
.dst_mac
) &&
1748 is_multicast_ether_addr(eth_spec
->val
.dst_mac
);
1751 static bool is_valid_attr(const struct ib_flow_attr
*flow_attr
)
1753 union ib_flow_spec
*ib_spec
= (union ib_flow_spec
*)(flow_attr
+ 1);
1754 bool has_ipv4_spec
= false;
1755 bool eth_type_ipv4
= true;
1756 unsigned int spec_index
;
1758 /* Validate that ethertype is correct */
1759 for (spec_index
= 0; spec_index
< flow_attr
->num_of_specs
; spec_index
++) {
1760 if (ib_spec
->type
== IB_FLOW_SPEC_ETH
&&
1761 ib_spec
->eth
.mask
.ether_type
) {
1762 if (!((ib_spec
->eth
.mask
.ether_type
== htons(0xffff)) &&
1763 ib_spec
->eth
.val
.ether_type
== htons(ETH_P_IP
)))
1764 eth_type_ipv4
= false;
1765 } else if (ib_spec
->type
== IB_FLOW_SPEC_IPV4
) {
1766 has_ipv4_spec
= true;
1768 ib_spec
= (void *)ib_spec
+ ib_spec
->size
;
1770 return !has_ipv4_spec
|| eth_type_ipv4
;
1773 static void put_flow_table(struct mlx5_ib_dev
*dev
,
1774 struct mlx5_ib_flow_prio
*prio
, bool ft_added
)
1776 prio
->refcount
-= !!ft_added
;
1777 if (!prio
->refcount
) {
1778 mlx5_destroy_flow_table(prio
->flow_table
);
1779 prio
->flow_table
= NULL
;
1783 static int mlx5_ib_destroy_flow(struct ib_flow
*flow_id
)
1785 struct mlx5_ib_dev
*dev
= to_mdev(flow_id
->qp
->device
);
1786 struct mlx5_ib_flow_handler
*handler
= container_of(flow_id
,
1787 struct mlx5_ib_flow_handler
,
1789 struct mlx5_ib_flow_handler
*iter
, *tmp
;
1791 mutex_lock(&dev
->flow_db
.lock
);
1793 list_for_each_entry_safe(iter
, tmp
, &handler
->list
, list
) {
1794 mlx5_del_flow_rule(iter
->rule
);
1795 put_flow_table(dev
, iter
->prio
, true);
1796 list_del(&iter
->list
);
1800 mlx5_del_flow_rule(handler
->rule
);
1801 put_flow_table(dev
, handler
->prio
, true);
1802 mutex_unlock(&dev
->flow_db
.lock
);
1809 static int ib_prio_to_core_prio(unsigned int priority
, bool dont_trap
)
1817 enum flow_table_type
{
1822 #define MLX5_FS_MAX_TYPES 10
1823 #define MLX5_FS_MAX_ENTRIES 32000UL
1824 static struct mlx5_ib_flow_prio
*get_flow_table(struct mlx5_ib_dev
*dev
,
1825 struct ib_flow_attr
*flow_attr
,
1826 enum flow_table_type ft_type
)
1828 bool dont_trap
= flow_attr
->flags
& IB_FLOW_ATTR_FLAGS_DONT_TRAP
;
1829 struct mlx5_flow_namespace
*ns
= NULL
;
1830 struct mlx5_ib_flow_prio
*prio
;
1831 struct mlx5_flow_table
*ft
;
1837 if (flow_attr
->type
== IB_FLOW_ATTR_NORMAL
) {
1838 if (flow_is_multicast_only(flow_attr
) &&
1840 priority
= MLX5_IB_FLOW_MCAST_PRIO
;
1842 priority
= ib_prio_to_core_prio(flow_attr
->priority
,
1844 ns
= mlx5_get_flow_namespace(dev
->mdev
,
1845 MLX5_FLOW_NAMESPACE_BYPASS
);
1846 num_entries
= MLX5_FS_MAX_ENTRIES
;
1847 num_groups
= MLX5_FS_MAX_TYPES
;
1848 prio
= &dev
->flow_db
.prios
[priority
];
1849 } else if (flow_attr
->type
== IB_FLOW_ATTR_ALL_DEFAULT
||
1850 flow_attr
->type
== IB_FLOW_ATTR_MC_DEFAULT
) {
1851 ns
= mlx5_get_flow_namespace(dev
->mdev
,
1852 MLX5_FLOW_NAMESPACE_LEFTOVERS
);
1853 build_leftovers_ft_param(&priority
,
1856 prio
= &dev
->flow_db
.prios
[MLX5_IB_FLOW_LEFTOVERS_PRIO
];
1857 } else if (flow_attr
->type
== IB_FLOW_ATTR_SNIFFER
) {
1858 if (!MLX5_CAP_FLOWTABLE(dev
->mdev
,
1859 allow_sniffer_and_nic_rx_shared_tir
))
1860 return ERR_PTR(-ENOTSUPP
);
1862 ns
= mlx5_get_flow_namespace(dev
->mdev
, ft_type
== MLX5_IB_FT_RX
?
1863 MLX5_FLOW_NAMESPACE_SNIFFER_RX
:
1864 MLX5_FLOW_NAMESPACE_SNIFFER_TX
);
1866 prio
= &dev
->flow_db
.sniffer
[ft_type
];
1873 return ERR_PTR(-ENOTSUPP
);
1875 ft
= prio
->flow_table
;
1877 ft
= mlx5_create_auto_grouped_flow_table(ns
, priority
,
1884 prio
->flow_table
= ft
;
1890 return err
? ERR_PTR(err
) : prio
;
1893 static struct mlx5_ib_flow_handler
*create_flow_rule(struct mlx5_ib_dev
*dev
,
1894 struct mlx5_ib_flow_prio
*ft_prio
,
1895 const struct ib_flow_attr
*flow_attr
,
1896 struct mlx5_flow_destination
*dst
)
1898 struct mlx5_flow_table
*ft
= ft_prio
->flow_table
;
1899 struct mlx5_ib_flow_handler
*handler
;
1900 struct mlx5_flow_spec
*spec
;
1901 const void *ib_flow
= (const void *)flow_attr
+ sizeof(*flow_attr
);
1902 unsigned int spec_index
;
1906 if (!is_valid_attr(flow_attr
))
1907 return ERR_PTR(-EINVAL
);
1909 spec
= mlx5_vzalloc(sizeof(*spec
));
1910 handler
= kzalloc(sizeof(*handler
), GFP_KERNEL
);
1911 if (!handler
|| !spec
) {
1916 INIT_LIST_HEAD(&handler
->list
);
1918 for (spec_index
= 0; spec_index
< flow_attr
->num_of_specs
; spec_index
++) {
1919 err
= parse_flow_attr(spec
->match_criteria
,
1920 spec
->match_value
, ib_flow
);
1924 ib_flow
+= ((union ib_flow_spec
*)ib_flow
)->size
;
1927 spec
->match_criteria_enable
= get_match_criteria_enable(spec
->match_criteria
);
1928 action
= dst
? MLX5_FLOW_CONTEXT_ACTION_FWD_DEST
:
1929 MLX5_FLOW_CONTEXT_ACTION_FWD_NEXT_PRIO
;
1930 handler
->rule
= mlx5_add_flow_rule(ft
, spec
,
1932 MLX5_FS_DEFAULT_FLOW_TAG
,
1935 if (IS_ERR(handler
->rule
)) {
1936 err
= PTR_ERR(handler
->rule
);
1940 ft_prio
->refcount
++;
1941 handler
->prio
= ft_prio
;
1943 ft_prio
->flow_table
= ft
;
1948 return err
? ERR_PTR(err
) : handler
;
1951 static struct mlx5_ib_flow_handler
*create_dont_trap_rule(struct mlx5_ib_dev
*dev
,
1952 struct mlx5_ib_flow_prio
*ft_prio
,
1953 struct ib_flow_attr
*flow_attr
,
1954 struct mlx5_flow_destination
*dst
)
1956 struct mlx5_ib_flow_handler
*handler_dst
= NULL
;
1957 struct mlx5_ib_flow_handler
*handler
= NULL
;
1959 handler
= create_flow_rule(dev
, ft_prio
, flow_attr
, NULL
);
1960 if (!IS_ERR(handler
)) {
1961 handler_dst
= create_flow_rule(dev
, ft_prio
,
1963 if (IS_ERR(handler_dst
)) {
1964 mlx5_del_flow_rule(handler
->rule
);
1965 ft_prio
->refcount
--;
1967 handler
= handler_dst
;
1969 list_add(&handler_dst
->list
, &handler
->list
);
1980 static struct mlx5_ib_flow_handler
*create_leftovers_rule(struct mlx5_ib_dev
*dev
,
1981 struct mlx5_ib_flow_prio
*ft_prio
,
1982 struct ib_flow_attr
*flow_attr
,
1983 struct mlx5_flow_destination
*dst
)
1985 struct mlx5_ib_flow_handler
*handler_ucast
= NULL
;
1986 struct mlx5_ib_flow_handler
*handler
= NULL
;
1989 struct ib_flow_attr flow_attr
;
1990 struct ib_flow_spec_eth eth_flow
;
1991 } leftovers_specs
[] = {
1995 .size
= sizeof(leftovers_specs
[0])
1998 .type
= IB_FLOW_SPEC_ETH
,
1999 .size
= sizeof(struct ib_flow_spec_eth
),
2000 .mask
= {.dst_mac
= {0x1} },
2001 .val
= {.dst_mac
= {0x1} }
2007 .size
= sizeof(leftovers_specs
[0])
2010 .type
= IB_FLOW_SPEC_ETH
,
2011 .size
= sizeof(struct ib_flow_spec_eth
),
2012 .mask
= {.dst_mac
= {0x1} },
2013 .val
= {.dst_mac
= {} }
2018 handler
= create_flow_rule(dev
, ft_prio
,
2019 &leftovers_specs
[LEFTOVERS_MC
].flow_attr
,
2021 if (!IS_ERR(handler
) &&
2022 flow_attr
->type
== IB_FLOW_ATTR_ALL_DEFAULT
) {
2023 handler_ucast
= create_flow_rule(dev
, ft_prio
,
2024 &leftovers_specs
[LEFTOVERS_UC
].flow_attr
,
2026 if (IS_ERR(handler_ucast
)) {
2027 mlx5_del_flow_rule(handler
->rule
);
2028 ft_prio
->refcount
--;
2030 handler
= handler_ucast
;
2032 list_add(&handler_ucast
->list
, &handler
->list
);
2039 static struct mlx5_ib_flow_handler
*create_sniffer_rule(struct mlx5_ib_dev
*dev
,
2040 struct mlx5_ib_flow_prio
*ft_rx
,
2041 struct mlx5_ib_flow_prio
*ft_tx
,
2042 struct mlx5_flow_destination
*dst
)
2044 struct mlx5_ib_flow_handler
*handler_rx
;
2045 struct mlx5_ib_flow_handler
*handler_tx
;
2047 static const struct ib_flow_attr flow_attr
= {
2049 .size
= sizeof(flow_attr
)
2052 handler_rx
= create_flow_rule(dev
, ft_rx
, &flow_attr
, dst
);
2053 if (IS_ERR(handler_rx
)) {
2054 err
= PTR_ERR(handler_rx
);
2058 handler_tx
= create_flow_rule(dev
, ft_tx
, &flow_attr
, dst
);
2059 if (IS_ERR(handler_tx
)) {
2060 err
= PTR_ERR(handler_tx
);
2064 list_add(&handler_tx
->list
, &handler_rx
->list
);
2069 mlx5_del_flow_rule(handler_rx
->rule
);
2073 return ERR_PTR(err
);
2076 static struct ib_flow
*mlx5_ib_create_flow(struct ib_qp
*qp
,
2077 struct ib_flow_attr
*flow_attr
,
2080 struct mlx5_ib_dev
*dev
= to_mdev(qp
->device
);
2081 struct mlx5_ib_qp
*mqp
= to_mqp(qp
);
2082 struct mlx5_ib_flow_handler
*handler
= NULL
;
2083 struct mlx5_flow_destination
*dst
= NULL
;
2084 struct mlx5_ib_flow_prio
*ft_prio_tx
= NULL
;
2085 struct mlx5_ib_flow_prio
*ft_prio
;
2088 if (flow_attr
->priority
> MLX5_IB_FLOW_LAST_PRIO
)
2089 return ERR_PTR(-ENOSPC
);
2091 if (domain
!= IB_FLOW_DOMAIN_USER
||
2092 flow_attr
->port
> MLX5_CAP_GEN(dev
->mdev
, num_ports
) ||
2093 (flow_attr
->flags
& ~IB_FLOW_ATTR_FLAGS_DONT_TRAP
))
2094 return ERR_PTR(-EINVAL
);
2096 dst
= kzalloc(sizeof(*dst
), GFP_KERNEL
);
2098 return ERR_PTR(-ENOMEM
);
2100 mutex_lock(&dev
->flow_db
.lock
);
2102 ft_prio
= get_flow_table(dev
, flow_attr
, MLX5_IB_FT_RX
);
2103 if (IS_ERR(ft_prio
)) {
2104 err
= PTR_ERR(ft_prio
);
2107 if (flow_attr
->type
== IB_FLOW_ATTR_SNIFFER
) {
2108 ft_prio_tx
= get_flow_table(dev
, flow_attr
, MLX5_IB_FT_TX
);
2109 if (IS_ERR(ft_prio_tx
)) {
2110 err
= PTR_ERR(ft_prio_tx
);
2116 dst
->type
= MLX5_FLOW_DESTINATION_TYPE_TIR
;
2117 if (mqp
->flags
& MLX5_IB_QP_RSS
)
2118 dst
->tir_num
= mqp
->rss_qp
.tirn
;
2120 dst
->tir_num
= mqp
->raw_packet_qp
.rq
.tirn
;
2122 if (flow_attr
->type
== IB_FLOW_ATTR_NORMAL
) {
2123 if (flow_attr
->flags
& IB_FLOW_ATTR_FLAGS_DONT_TRAP
) {
2124 handler
= create_dont_trap_rule(dev
, ft_prio
,
2127 handler
= create_flow_rule(dev
, ft_prio
, flow_attr
,
2130 } else if (flow_attr
->type
== IB_FLOW_ATTR_ALL_DEFAULT
||
2131 flow_attr
->type
== IB_FLOW_ATTR_MC_DEFAULT
) {
2132 handler
= create_leftovers_rule(dev
, ft_prio
, flow_attr
,
2134 } else if (flow_attr
->type
== IB_FLOW_ATTR_SNIFFER
) {
2135 handler
= create_sniffer_rule(dev
, ft_prio
, ft_prio_tx
, dst
);
2141 if (IS_ERR(handler
)) {
2142 err
= PTR_ERR(handler
);
2147 mutex_unlock(&dev
->flow_db
.lock
);
2150 return &handler
->ibflow
;
2153 put_flow_table(dev
, ft_prio
, false);
2155 put_flow_table(dev
, ft_prio_tx
, false);
2157 mutex_unlock(&dev
->flow_db
.lock
);
2160 return ERR_PTR(err
);
2163 static int mlx5_ib_mcg_attach(struct ib_qp
*ibqp
, union ib_gid
*gid
, u16 lid
)
2165 struct mlx5_ib_dev
*dev
= to_mdev(ibqp
->device
);
2168 err
= mlx5_core_attach_mcg(dev
->mdev
, gid
, ibqp
->qp_num
);
2170 mlx5_ib_warn(dev
, "failed attaching QPN 0x%x, MGID %pI6\n",
2171 ibqp
->qp_num
, gid
->raw
);
2176 static int mlx5_ib_mcg_detach(struct ib_qp
*ibqp
, union ib_gid
*gid
, u16 lid
)
2178 struct mlx5_ib_dev
*dev
= to_mdev(ibqp
->device
);
2181 err
= mlx5_core_detach_mcg(dev
->mdev
, gid
, ibqp
->qp_num
);
2183 mlx5_ib_warn(dev
, "failed detaching QPN 0x%x, MGID %pI6\n",
2184 ibqp
->qp_num
, gid
->raw
);
2189 static int init_node_data(struct mlx5_ib_dev
*dev
)
2193 err
= mlx5_query_node_desc(dev
, dev
->ib_dev
.node_desc
);
2197 dev
->mdev
->rev_id
= dev
->mdev
->pdev
->revision
;
2199 return mlx5_query_node_guid(dev
, &dev
->ib_dev
.node_guid
);
2202 static ssize_t
show_fw_pages(struct device
*device
, struct device_attribute
*attr
,
2205 struct mlx5_ib_dev
*dev
=
2206 container_of(device
, struct mlx5_ib_dev
, ib_dev
.dev
);
2208 return sprintf(buf
, "%d\n", dev
->mdev
->priv
.fw_pages
);
2211 static ssize_t
show_reg_pages(struct device
*device
,
2212 struct device_attribute
*attr
, char *buf
)
2214 struct mlx5_ib_dev
*dev
=
2215 container_of(device
, struct mlx5_ib_dev
, ib_dev
.dev
);
2217 return sprintf(buf
, "%d\n", atomic_read(&dev
->mdev
->priv
.reg_pages
));
2220 static ssize_t
show_hca(struct device
*device
, struct device_attribute
*attr
,
2223 struct mlx5_ib_dev
*dev
=
2224 container_of(device
, struct mlx5_ib_dev
, ib_dev
.dev
);
2225 return sprintf(buf
, "MT%d\n", dev
->mdev
->pdev
->device
);
2228 static ssize_t
show_rev(struct device
*device
, struct device_attribute
*attr
,
2231 struct mlx5_ib_dev
*dev
=
2232 container_of(device
, struct mlx5_ib_dev
, ib_dev
.dev
);
2233 return sprintf(buf
, "%x\n", dev
->mdev
->rev_id
);
2236 static ssize_t
show_board(struct device
*device
, struct device_attribute
*attr
,
2239 struct mlx5_ib_dev
*dev
=
2240 container_of(device
, struct mlx5_ib_dev
, ib_dev
.dev
);
2241 return sprintf(buf
, "%.*s\n", MLX5_BOARD_ID_LEN
,
2242 dev
->mdev
->board_id
);
2245 static DEVICE_ATTR(hw_rev
, S_IRUGO
, show_rev
, NULL
);
2246 static DEVICE_ATTR(hca_type
, S_IRUGO
, show_hca
, NULL
);
2247 static DEVICE_ATTR(board_id
, S_IRUGO
, show_board
, NULL
);
2248 static DEVICE_ATTR(fw_pages
, S_IRUGO
, show_fw_pages
, NULL
);
2249 static DEVICE_ATTR(reg_pages
, S_IRUGO
, show_reg_pages
, NULL
);
2251 static struct device_attribute
*mlx5_class_attributes
[] = {
2256 &dev_attr_reg_pages
,
2259 static void pkey_change_handler(struct work_struct
*work
)
2261 struct mlx5_ib_port_resources
*ports
=
2262 container_of(work
, struct mlx5_ib_port_resources
,
2265 mutex_lock(&ports
->devr
->mutex
);
2266 mlx5_ib_gsi_pkey_change(ports
->gsi
);
2267 mutex_unlock(&ports
->devr
->mutex
);
2270 static void mlx5_ib_handle_internal_error(struct mlx5_ib_dev
*ibdev
)
2272 struct mlx5_ib_qp
*mqp
;
2273 struct mlx5_ib_cq
*send_mcq
, *recv_mcq
;
2274 struct mlx5_core_cq
*mcq
;
2275 struct list_head cq_armed_list
;
2276 unsigned long flags_qp
;
2277 unsigned long flags_cq
;
2278 unsigned long flags
;
2280 INIT_LIST_HEAD(&cq_armed_list
);
2282 /* Go over qp list reside on that ibdev, sync with create/destroy qp.*/
2283 spin_lock_irqsave(&ibdev
->reset_flow_resource_lock
, flags
);
2284 list_for_each_entry(mqp
, &ibdev
->qp_list
, qps_list
) {
2285 spin_lock_irqsave(&mqp
->sq
.lock
, flags_qp
);
2286 if (mqp
->sq
.tail
!= mqp
->sq
.head
) {
2287 send_mcq
= to_mcq(mqp
->ibqp
.send_cq
);
2288 spin_lock_irqsave(&send_mcq
->lock
, flags_cq
);
2289 if (send_mcq
->mcq
.comp
&&
2290 mqp
->ibqp
.send_cq
->comp_handler
) {
2291 if (!send_mcq
->mcq
.reset_notify_added
) {
2292 send_mcq
->mcq
.reset_notify_added
= 1;
2293 list_add_tail(&send_mcq
->mcq
.reset_notify
,
2297 spin_unlock_irqrestore(&send_mcq
->lock
, flags_cq
);
2299 spin_unlock_irqrestore(&mqp
->sq
.lock
, flags_qp
);
2300 spin_lock_irqsave(&mqp
->rq
.lock
, flags_qp
);
2301 /* no handling is needed for SRQ */
2302 if (!mqp
->ibqp
.srq
) {
2303 if (mqp
->rq
.tail
!= mqp
->rq
.head
) {
2304 recv_mcq
= to_mcq(mqp
->ibqp
.recv_cq
);
2305 spin_lock_irqsave(&recv_mcq
->lock
, flags_cq
);
2306 if (recv_mcq
->mcq
.comp
&&
2307 mqp
->ibqp
.recv_cq
->comp_handler
) {
2308 if (!recv_mcq
->mcq
.reset_notify_added
) {
2309 recv_mcq
->mcq
.reset_notify_added
= 1;
2310 list_add_tail(&recv_mcq
->mcq
.reset_notify
,
2314 spin_unlock_irqrestore(&recv_mcq
->lock
,
2318 spin_unlock_irqrestore(&mqp
->rq
.lock
, flags_qp
);
2320 /*At that point all inflight post send were put to be executed as of we
2321 * lock/unlock above locks Now need to arm all involved CQs.
2323 list_for_each_entry(mcq
, &cq_armed_list
, reset_notify
) {
2326 spin_unlock_irqrestore(&ibdev
->reset_flow_resource_lock
, flags
);
2329 static void mlx5_ib_event(struct mlx5_core_dev
*dev
, void *context
,
2330 enum mlx5_dev_event event
, unsigned long param
)
2332 struct mlx5_ib_dev
*ibdev
= (struct mlx5_ib_dev
*)context
;
2333 struct ib_event ibev
;
2338 case MLX5_DEV_EVENT_SYS_ERROR
:
2339 ibev
.event
= IB_EVENT_DEVICE_FATAL
;
2340 mlx5_ib_handle_internal_error(ibdev
);
2344 case MLX5_DEV_EVENT_PORT_UP
:
2345 case MLX5_DEV_EVENT_PORT_DOWN
:
2346 case MLX5_DEV_EVENT_PORT_INITIALIZED
:
2349 /* In RoCE, port up/down events are handled in
2350 * mlx5_netdev_event().
2352 if (mlx5_ib_port_link_layer(&ibdev
->ib_dev
, port
) ==
2353 IB_LINK_LAYER_ETHERNET
)
2356 ibev
.event
= (event
== MLX5_DEV_EVENT_PORT_UP
) ?
2357 IB_EVENT_PORT_ACTIVE
: IB_EVENT_PORT_ERR
;
2360 case MLX5_DEV_EVENT_LID_CHANGE
:
2361 ibev
.event
= IB_EVENT_LID_CHANGE
;
2365 case MLX5_DEV_EVENT_PKEY_CHANGE
:
2366 ibev
.event
= IB_EVENT_PKEY_CHANGE
;
2369 schedule_work(&ibdev
->devr
.ports
[port
- 1].pkey_change_work
);
2372 case MLX5_DEV_EVENT_GUID_CHANGE
:
2373 ibev
.event
= IB_EVENT_GID_CHANGE
;
2377 case MLX5_DEV_EVENT_CLIENT_REREG
:
2378 ibev
.event
= IB_EVENT_CLIENT_REREGISTER
;
2383 ibev
.device
= &ibdev
->ib_dev
;
2384 ibev
.element
.port_num
= port
;
2386 if (port
< 1 || port
> ibdev
->num_ports
) {
2387 mlx5_ib_warn(ibdev
, "warning: event on port %d\n", port
);
2391 if (ibdev
->ib_active
)
2392 ib_dispatch_event(&ibev
);
2395 ibdev
->ib_active
= false;
2398 static void get_ext_port_caps(struct mlx5_ib_dev
*dev
)
2402 for (port
= 1; port
<= MLX5_CAP_GEN(dev
->mdev
, num_ports
); port
++)
2403 mlx5_query_ext_port_caps(dev
, port
);
2406 static int get_port_caps(struct mlx5_ib_dev
*dev
)
2408 struct ib_device_attr
*dprops
= NULL
;
2409 struct ib_port_attr
*pprops
= NULL
;
2412 struct ib_udata uhw
= {.inlen
= 0, .outlen
= 0};
2414 pprops
= kmalloc(sizeof(*pprops
), GFP_KERNEL
);
2418 dprops
= kmalloc(sizeof(*dprops
), GFP_KERNEL
);
2422 err
= mlx5_ib_query_device(&dev
->ib_dev
, dprops
, &uhw
);
2424 mlx5_ib_warn(dev
, "query_device failed %d\n", err
);
2428 for (port
= 1; port
<= MLX5_CAP_GEN(dev
->mdev
, num_ports
); port
++) {
2429 err
= mlx5_ib_query_port(&dev
->ib_dev
, port
, pprops
);
2431 mlx5_ib_warn(dev
, "query_port %d failed %d\n",
2435 dev
->mdev
->port_caps
[port
- 1].pkey_table_len
=
2437 dev
->mdev
->port_caps
[port
- 1].gid_table_len
=
2438 pprops
->gid_tbl_len
;
2439 mlx5_ib_dbg(dev
, "pkey_table_len %d, gid_table_len %d\n",
2440 dprops
->max_pkeys
, pprops
->gid_tbl_len
);
2450 static void destroy_umrc_res(struct mlx5_ib_dev
*dev
)
2454 err
= mlx5_mr_cache_cleanup(dev
);
2456 mlx5_ib_warn(dev
, "mr cache cleanup failed\n");
2458 mlx5_ib_destroy_qp(dev
->umrc
.qp
);
2459 ib_free_cq(dev
->umrc
.cq
);
2460 ib_dealloc_pd(dev
->umrc
.pd
);
2467 static int create_umr_res(struct mlx5_ib_dev
*dev
)
2469 struct ib_qp_init_attr
*init_attr
= NULL
;
2470 struct ib_qp_attr
*attr
= NULL
;
2476 attr
= kzalloc(sizeof(*attr
), GFP_KERNEL
);
2477 init_attr
= kzalloc(sizeof(*init_attr
), GFP_KERNEL
);
2478 if (!attr
|| !init_attr
) {
2483 pd
= ib_alloc_pd(&dev
->ib_dev
, 0);
2485 mlx5_ib_dbg(dev
, "Couldn't create PD for sync UMR QP\n");
2490 cq
= ib_alloc_cq(&dev
->ib_dev
, NULL
, 128, 0, IB_POLL_SOFTIRQ
);
2492 mlx5_ib_dbg(dev
, "Couldn't create CQ for sync UMR QP\n");
2497 init_attr
->send_cq
= cq
;
2498 init_attr
->recv_cq
= cq
;
2499 init_attr
->sq_sig_type
= IB_SIGNAL_ALL_WR
;
2500 init_attr
->cap
.max_send_wr
= MAX_UMR_WR
;
2501 init_attr
->cap
.max_send_sge
= 1;
2502 init_attr
->qp_type
= MLX5_IB_QPT_REG_UMR
;
2503 init_attr
->port_num
= 1;
2504 qp
= mlx5_ib_create_qp(pd
, init_attr
, NULL
);
2506 mlx5_ib_dbg(dev
, "Couldn't create sync UMR QP\n");
2510 qp
->device
= &dev
->ib_dev
;
2513 qp
->qp_type
= MLX5_IB_QPT_REG_UMR
;
2514 qp
->send_cq
= init_attr
->send_cq
;
2515 qp
->recv_cq
= init_attr
->recv_cq
;
2517 attr
->qp_state
= IB_QPS_INIT
;
2519 ret
= mlx5_ib_modify_qp(qp
, attr
, IB_QP_STATE
| IB_QP_PKEY_INDEX
|
2522 mlx5_ib_dbg(dev
, "Couldn't modify UMR QP\n");
2526 memset(attr
, 0, sizeof(*attr
));
2527 attr
->qp_state
= IB_QPS_RTR
;
2528 attr
->path_mtu
= IB_MTU_256
;
2530 ret
= mlx5_ib_modify_qp(qp
, attr
, IB_QP_STATE
, NULL
);
2532 mlx5_ib_dbg(dev
, "Couldn't modify umr QP to rtr\n");
2536 memset(attr
, 0, sizeof(*attr
));
2537 attr
->qp_state
= IB_QPS_RTS
;
2538 ret
= mlx5_ib_modify_qp(qp
, attr
, IB_QP_STATE
, NULL
);
2540 mlx5_ib_dbg(dev
, "Couldn't modify umr QP to rts\n");
2548 sema_init(&dev
->umrc
.sem
, MAX_UMR_WR
);
2549 ret
= mlx5_mr_cache_init(dev
);
2551 mlx5_ib_warn(dev
, "mr cache init failed %d\n", ret
);
2561 mlx5_ib_destroy_qp(qp
);
2575 static u8
mlx5_get_umr_fence(u8 umr_fence_cap
)
2577 switch (umr_fence_cap
) {
2578 case MLX5_CAP_UMR_FENCE_NONE
:
2579 return MLX5_FENCE_MODE_NONE
;
2580 case MLX5_CAP_UMR_FENCE_SMALL
:
2581 return MLX5_FENCE_MODE_INITIATOR_SMALL
;
2583 return MLX5_FENCE_MODE_STRONG_ORDERING
;
2587 static int create_dev_resources(struct mlx5_ib_resources
*devr
)
2589 struct ib_srq_init_attr attr
;
2590 struct mlx5_ib_dev
*dev
;
2591 struct ib_cq_init_attr cq_attr
= {.cqe
= 1};
2595 dev
= container_of(devr
, struct mlx5_ib_dev
, devr
);
2597 mutex_init(&devr
->mutex
);
2599 devr
->p0
= mlx5_ib_alloc_pd(&dev
->ib_dev
, NULL
, NULL
);
2600 if (IS_ERR(devr
->p0
)) {
2601 ret
= PTR_ERR(devr
->p0
);
2604 devr
->p0
->device
= &dev
->ib_dev
;
2605 devr
->p0
->uobject
= NULL
;
2606 atomic_set(&devr
->p0
->usecnt
, 0);
2608 devr
->c0
= mlx5_ib_create_cq(&dev
->ib_dev
, &cq_attr
, NULL
, NULL
);
2609 if (IS_ERR(devr
->c0
)) {
2610 ret
= PTR_ERR(devr
->c0
);
2613 devr
->c0
->device
= &dev
->ib_dev
;
2614 devr
->c0
->uobject
= NULL
;
2615 devr
->c0
->comp_handler
= NULL
;
2616 devr
->c0
->event_handler
= NULL
;
2617 devr
->c0
->cq_context
= NULL
;
2618 atomic_set(&devr
->c0
->usecnt
, 0);
2620 devr
->x0
= mlx5_ib_alloc_xrcd(&dev
->ib_dev
, NULL
, NULL
);
2621 if (IS_ERR(devr
->x0
)) {
2622 ret
= PTR_ERR(devr
->x0
);
2625 devr
->x0
->device
= &dev
->ib_dev
;
2626 devr
->x0
->inode
= NULL
;
2627 atomic_set(&devr
->x0
->usecnt
, 0);
2628 mutex_init(&devr
->x0
->tgt_qp_mutex
);
2629 INIT_LIST_HEAD(&devr
->x0
->tgt_qp_list
);
2631 devr
->x1
= mlx5_ib_alloc_xrcd(&dev
->ib_dev
, NULL
, NULL
);
2632 if (IS_ERR(devr
->x1
)) {
2633 ret
= PTR_ERR(devr
->x1
);
2636 devr
->x1
->device
= &dev
->ib_dev
;
2637 devr
->x1
->inode
= NULL
;
2638 atomic_set(&devr
->x1
->usecnt
, 0);
2639 mutex_init(&devr
->x1
->tgt_qp_mutex
);
2640 INIT_LIST_HEAD(&devr
->x1
->tgt_qp_list
);
2642 memset(&attr
, 0, sizeof(attr
));
2643 attr
.attr
.max_sge
= 1;
2644 attr
.attr
.max_wr
= 1;
2645 attr
.srq_type
= IB_SRQT_XRC
;
2646 attr
.ext
.xrc
.cq
= devr
->c0
;
2647 attr
.ext
.xrc
.xrcd
= devr
->x0
;
2649 devr
->s0
= mlx5_ib_create_srq(devr
->p0
, &attr
, NULL
);
2650 if (IS_ERR(devr
->s0
)) {
2651 ret
= PTR_ERR(devr
->s0
);
2654 devr
->s0
->device
= &dev
->ib_dev
;
2655 devr
->s0
->pd
= devr
->p0
;
2656 devr
->s0
->uobject
= NULL
;
2657 devr
->s0
->event_handler
= NULL
;
2658 devr
->s0
->srq_context
= NULL
;
2659 devr
->s0
->srq_type
= IB_SRQT_XRC
;
2660 devr
->s0
->ext
.xrc
.xrcd
= devr
->x0
;
2661 devr
->s0
->ext
.xrc
.cq
= devr
->c0
;
2662 atomic_inc(&devr
->s0
->ext
.xrc
.xrcd
->usecnt
);
2663 atomic_inc(&devr
->s0
->ext
.xrc
.cq
->usecnt
);
2664 atomic_inc(&devr
->p0
->usecnt
);
2665 atomic_set(&devr
->s0
->usecnt
, 0);
2667 memset(&attr
, 0, sizeof(attr
));
2668 attr
.attr
.max_sge
= 1;
2669 attr
.attr
.max_wr
= 1;
2670 attr
.srq_type
= IB_SRQT_BASIC
;
2671 devr
->s1
= mlx5_ib_create_srq(devr
->p0
, &attr
, NULL
);
2672 if (IS_ERR(devr
->s1
)) {
2673 ret
= PTR_ERR(devr
->s1
);
2676 devr
->s1
->device
= &dev
->ib_dev
;
2677 devr
->s1
->pd
= devr
->p0
;
2678 devr
->s1
->uobject
= NULL
;
2679 devr
->s1
->event_handler
= NULL
;
2680 devr
->s1
->srq_context
= NULL
;
2681 devr
->s1
->srq_type
= IB_SRQT_BASIC
;
2682 devr
->s1
->ext
.xrc
.cq
= devr
->c0
;
2683 atomic_inc(&devr
->p0
->usecnt
);
2684 atomic_set(&devr
->s0
->usecnt
, 0);
2686 for (port
= 0; port
< ARRAY_SIZE(devr
->ports
); ++port
) {
2687 INIT_WORK(&devr
->ports
[port
].pkey_change_work
,
2688 pkey_change_handler
);
2689 devr
->ports
[port
].devr
= devr
;
2695 mlx5_ib_destroy_srq(devr
->s0
);
2697 mlx5_ib_dealloc_xrcd(devr
->x1
);
2699 mlx5_ib_dealloc_xrcd(devr
->x0
);
2701 mlx5_ib_destroy_cq(devr
->c0
);
2703 mlx5_ib_dealloc_pd(devr
->p0
);
2708 static void destroy_dev_resources(struct mlx5_ib_resources
*devr
)
2710 struct mlx5_ib_dev
*dev
=
2711 container_of(devr
, struct mlx5_ib_dev
, devr
);
2714 mlx5_ib_destroy_srq(devr
->s1
);
2715 mlx5_ib_destroy_srq(devr
->s0
);
2716 mlx5_ib_dealloc_xrcd(devr
->x0
);
2717 mlx5_ib_dealloc_xrcd(devr
->x1
);
2718 mlx5_ib_destroy_cq(devr
->c0
);
2719 mlx5_ib_dealloc_pd(devr
->p0
);
2721 /* Make sure no change P_Key work items are still executing */
2722 for (port
= 0; port
< dev
->num_ports
; ++port
)
2723 cancel_work_sync(&devr
->ports
[port
].pkey_change_work
);
2726 static u32
get_core_cap_flags(struct ib_device
*ibdev
)
2728 struct mlx5_ib_dev
*dev
= to_mdev(ibdev
);
2729 enum rdma_link_layer ll
= mlx5_ib_port_link_layer(ibdev
, 1);
2730 u8 l3_type_cap
= MLX5_CAP_ROCE(dev
->mdev
, l3_type
);
2731 u8 roce_version_cap
= MLX5_CAP_ROCE(dev
->mdev
, roce_version
);
2734 if (ll
== IB_LINK_LAYER_INFINIBAND
)
2735 return RDMA_CORE_PORT_IBA_IB
;
2737 if (!(l3_type_cap
& MLX5_ROCE_L3_TYPE_IPV4_CAP
))
2740 if (!(l3_type_cap
& MLX5_ROCE_L3_TYPE_IPV6_CAP
))
2743 if (roce_version_cap
& MLX5_ROCE_VERSION_1_CAP
)
2744 ret
|= RDMA_CORE_PORT_IBA_ROCE
;
2746 if (roce_version_cap
& MLX5_ROCE_VERSION_2_CAP
)
2747 ret
|= RDMA_CORE_PORT_IBA_ROCE_UDP_ENCAP
;
2752 static int mlx5_port_immutable(struct ib_device
*ibdev
, u8 port_num
,
2753 struct ib_port_immutable
*immutable
)
2755 struct ib_port_attr attr
;
2758 err
= mlx5_ib_query_port(ibdev
, port_num
, &attr
);
2762 immutable
->pkey_tbl_len
= attr
.pkey_tbl_len
;
2763 immutable
->gid_tbl_len
= attr
.gid_tbl_len
;
2764 immutable
->core_cap_flags
= get_core_cap_flags(ibdev
);
2765 immutable
->max_mad_size
= IB_MGMT_MAD_SIZE
;
2770 static void get_dev_fw_str(struct ib_device
*ibdev
, char *str
,
2773 struct mlx5_ib_dev
*dev
=
2774 container_of(ibdev
, struct mlx5_ib_dev
, ib_dev
);
2775 snprintf(str
, str_len
, "%d.%d.%04d", fw_rev_maj(dev
->mdev
),
2776 fw_rev_min(dev
->mdev
), fw_rev_sub(dev
->mdev
));
2779 static int mlx5_roce_lag_init(struct mlx5_ib_dev
*dev
)
2781 struct mlx5_core_dev
*mdev
= dev
->mdev
;
2782 struct mlx5_flow_namespace
*ns
= mlx5_get_flow_namespace(mdev
,
2783 MLX5_FLOW_NAMESPACE_LAG
);
2784 struct mlx5_flow_table
*ft
;
2787 if (!ns
|| !mlx5_lag_is_active(mdev
))
2790 err
= mlx5_cmd_create_vport_lag(mdev
);
2794 ft
= mlx5_create_lag_demux_flow_table(ns
, 0, 0);
2797 goto err_destroy_vport_lag
;
2800 dev
->flow_db
.lag_demux_ft
= ft
;
2803 err_destroy_vport_lag
:
2804 mlx5_cmd_destroy_vport_lag(mdev
);
2808 static void mlx5_roce_lag_cleanup(struct mlx5_ib_dev
*dev
)
2810 struct mlx5_core_dev
*mdev
= dev
->mdev
;
2812 if (dev
->flow_db
.lag_demux_ft
) {
2813 mlx5_destroy_flow_table(dev
->flow_db
.lag_demux_ft
);
2814 dev
->flow_db
.lag_demux_ft
= NULL
;
2816 mlx5_cmd_destroy_vport_lag(mdev
);
2820 static void mlx5_remove_roce_notifier(struct mlx5_ib_dev
*dev
)
2822 if (dev
->roce
.nb
.notifier_call
) {
2823 unregister_netdevice_notifier(&dev
->roce
.nb
);
2824 dev
->roce
.nb
.notifier_call
= NULL
;
2828 static int mlx5_enable_roce(struct mlx5_ib_dev
*dev
)
2832 dev
->roce
.nb
.notifier_call
= mlx5_netdev_event
;
2833 err
= register_netdevice_notifier(&dev
->roce
.nb
);
2835 dev
->roce
.nb
.notifier_call
= NULL
;
2839 err
= mlx5_nic_vport_enable_roce(dev
->mdev
);
2841 goto err_unregister_netdevice_notifier
;
2843 err
= mlx5_roce_lag_init(dev
);
2845 goto err_disable_roce
;
2850 mlx5_nic_vport_disable_roce(dev
->mdev
);
2852 err_unregister_netdevice_notifier
:
2853 mlx5_remove_roce_notifier(dev
);
2857 static void mlx5_disable_roce(struct mlx5_ib_dev
*dev
)
2859 mlx5_roce_lag_cleanup(dev
);
2860 mlx5_nic_vport_disable_roce(dev
->mdev
);
2863 static void mlx5_ib_dealloc_q_counters(struct mlx5_ib_dev
*dev
)
2867 for (i
= 0; i
< dev
->num_ports
; i
++)
2868 mlx5_core_dealloc_q_counter(dev
->mdev
,
2869 dev
->port
[i
].q_cnt_id
);
2872 static int mlx5_ib_alloc_q_counters(struct mlx5_ib_dev
*dev
)
2877 for (i
= 0; i
< dev
->num_ports
; i
++) {
2878 ret
= mlx5_core_alloc_q_counter(dev
->mdev
,
2879 &dev
->port
[i
].q_cnt_id
);
2882 "couldn't allocate queue counter for port %d, err %d\n",
2884 goto dealloc_counters
;
2892 mlx5_core_dealloc_q_counter(dev
->mdev
,
2893 dev
->port
[i
].q_cnt_id
);
2898 static const char * const names
[] = {
2899 "rx_write_requests",
2901 "rx_atomic_requests",
2904 "duplicate_request",
2905 "rnr_nak_retry_err",
2907 "implied_nak_seq_err",
2908 "local_ack_timeout_err",
2911 static const size_t stats_offsets
[] = {
2912 MLX5_BYTE_OFF(query_q_counter_out
, rx_write_requests
),
2913 MLX5_BYTE_OFF(query_q_counter_out
, rx_read_requests
),
2914 MLX5_BYTE_OFF(query_q_counter_out
, rx_atomic_requests
),
2915 MLX5_BYTE_OFF(query_q_counter_out
, out_of_buffer
),
2916 MLX5_BYTE_OFF(query_q_counter_out
, out_of_sequence
),
2917 MLX5_BYTE_OFF(query_q_counter_out
, duplicate_request
),
2918 MLX5_BYTE_OFF(query_q_counter_out
, rnr_nak_retry_err
),
2919 MLX5_BYTE_OFF(query_q_counter_out
, packet_seq_err
),
2920 MLX5_BYTE_OFF(query_q_counter_out
, implied_nak_seq_err
),
2921 MLX5_BYTE_OFF(query_q_counter_out
, local_ack_timeout_err
),
2924 static struct rdma_hw_stats
*mlx5_ib_alloc_hw_stats(struct ib_device
*ibdev
,
2927 BUILD_BUG_ON(ARRAY_SIZE(names
) != ARRAY_SIZE(stats_offsets
));
2929 /* We support only per port stats */
2933 return rdma_alloc_hw_stats_struct(names
, ARRAY_SIZE(names
),
2934 RDMA_HW_STATS_DEFAULT_LIFESPAN
);
2937 static int mlx5_ib_get_hw_stats(struct ib_device
*ibdev
,
2938 struct rdma_hw_stats
*stats
,
2941 struct mlx5_ib_dev
*dev
= to_mdev(ibdev
);
2942 int outlen
= MLX5_ST_SZ_BYTES(query_q_counter_out
);
2948 if (!port
|| !stats
)
2951 out
= mlx5_vzalloc(outlen
);
2955 ret
= mlx5_core_query_q_counter(dev
->mdev
,
2956 dev
->port
[port
- 1].q_cnt_id
, 0,
2961 for (i
= 0; i
< ARRAY_SIZE(names
); i
++) {
2962 val
= *(__be32
*)(out
+ stats_offsets
[i
]);
2963 stats
->value
[i
] = (u64
)be32_to_cpu(val
);
2967 return ARRAY_SIZE(names
);
2970 static void *mlx5_ib_add(struct mlx5_core_dev
*mdev
)
2972 struct mlx5_ib_dev
*dev
;
2973 enum rdma_link_layer ll
;
2979 port_type_cap
= MLX5_CAP_GEN(mdev
, port_type
);
2980 ll
= mlx5_port_type_cap_to_rdma_ll(port_type_cap
);
2982 if ((ll
== IB_LINK_LAYER_ETHERNET
) && !MLX5_CAP_GEN(mdev
, roce
))
2985 printk_once(KERN_INFO
"%s", mlx5_version
);
2987 dev
= (struct mlx5_ib_dev
*)ib_alloc_device(sizeof(*dev
));
2993 dev
->port
= kcalloc(MLX5_CAP_GEN(mdev
, num_ports
), sizeof(*dev
->port
),
2998 rwlock_init(&dev
->roce
.netdev_lock
);
2999 err
= get_port_caps(dev
);
3003 if (mlx5_use_mad_ifc(dev
))
3004 get_ext_port_caps(dev
);
3006 MLX5_INIT_DOORBELL_LOCK(&dev
->uar_lock
);
3008 if (!mlx5_lag_is_active(mdev
))
3011 name
= "mlx5_bond_%d";
3013 strlcpy(dev
->ib_dev
.name
, name
, IB_DEVICE_NAME_MAX
);
3014 dev
->ib_dev
.owner
= THIS_MODULE
;
3015 dev
->ib_dev
.node_type
= RDMA_NODE_IB_CA
;
3016 dev
->ib_dev
.local_dma_lkey
= 0 /* not supported for now */;
3017 dev
->num_ports
= MLX5_CAP_GEN(mdev
, num_ports
);
3018 dev
->ib_dev
.phys_port_cnt
= dev
->num_ports
;
3019 dev
->ib_dev
.num_comp_vectors
=
3020 dev
->mdev
->priv
.eq_table
.num_comp_vectors
;
3021 dev
->ib_dev
.dma_device
= &mdev
->pdev
->dev
;
3023 dev
->ib_dev
.uverbs_abi_ver
= MLX5_IB_UVERBS_ABI_VERSION
;
3024 dev
->ib_dev
.uverbs_cmd_mask
=
3025 (1ull << IB_USER_VERBS_CMD_GET_CONTEXT
) |
3026 (1ull << IB_USER_VERBS_CMD_QUERY_DEVICE
) |
3027 (1ull << IB_USER_VERBS_CMD_QUERY_PORT
) |
3028 (1ull << IB_USER_VERBS_CMD_ALLOC_PD
) |
3029 (1ull << IB_USER_VERBS_CMD_DEALLOC_PD
) |
3030 (1ull << IB_USER_VERBS_CMD_REG_MR
) |
3031 (1ull << IB_USER_VERBS_CMD_REREG_MR
) |
3032 (1ull << IB_USER_VERBS_CMD_DEREG_MR
) |
3033 (1ull << IB_USER_VERBS_CMD_CREATE_COMP_CHANNEL
) |
3034 (1ull << IB_USER_VERBS_CMD_CREATE_CQ
) |
3035 (1ull << IB_USER_VERBS_CMD_RESIZE_CQ
) |
3036 (1ull << IB_USER_VERBS_CMD_DESTROY_CQ
) |
3037 (1ull << IB_USER_VERBS_CMD_CREATE_QP
) |
3038 (1ull << IB_USER_VERBS_CMD_MODIFY_QP
) |
3039 (1ull << IB_USER_VERBS_CMD_QUERY_QP
) |
3040 (1ull << IB_USER_VERBS_CMD_DESTROY_QP
) |
3041 (1ull << IB_USER_VERBS_CMD_ATTACH_MCAST
) |
3042 (1ull << IB_USER_VERBS_CMD_DETACH_MCAST
) |
3043 (1ull << IB_USER_VERBS_CMD_CREATE_SRQ
) |
3044 (1ull << IB_USER_VERBS_CMD_MODIFY_SRQ
) |
3045 (1ull << IB_USER_VERBS_CMD_QUERY_SRQ
) |
3046 (1ull << IB_USER_VERBS_CMD_DESTROY_SRQ
) |
3047 (1ull << IB_USER_VERBS_CMD_CREATE_XSRQ
) |
3048 (1ull << IB_USER_VERBS_CMD_OPEN_QP
);
3049 dev
->ib_dev
.uverbs_ex_cmd_mask
=
3050 (1ull << IB_USER_VERBS_EX_CMD_QUERY_DEVICE
) |
3051 (1ull << IB_USER_VERBS_EX_CMD_CREATE_CQ
) |
3052 (1ull << IB_USER_VERBS_EX_CMD_CREATE_QP
);
3054 dev
->ib_dev
.query_device
= mlx5_ib_query_device
;
3055 dev
->ib_dev
.query_port
= mlx5_ib_query_port
;
3056 dev
->ib_dev
.get_link_layer
= mlx5_ib_port_link_layer
;
3057 if (ll
== IB_LINK_LAYER_ETHERNET
)
3058 dev
->ib_dev
.get_netdev
= mlx5_ib_get_netdev
;
3059 dev
->ib_dev
.query_gid
= mlx5_ib_query_gid
;
3060 dev
->ib_dev
.add_gid
= mlx5_ib_add_gid
;
3061 dev
->ib_dev
.del_gid
= mlx5_ib_del_gid
;
3062 dev
->ib_dev
.query_pkey
= mlx5_ib_query_pkey
;
3063 dev
->ib_dev
.modify_device
= mlx5_ib_modify_device
;
3064 dev
->ib_dev
.modify_port
= mlx5_ib_modify_port
;
3065 dev
->ib_dev
.alloc_ucontext
= mlx5_ib_alloc_ucontext
;
3066 dev
->ib_dev
.dealloc_ucontext
= mlx5_ib_dealloc_ucontext
;
3067 dev
->ib_dev
.mmap
= mlx5_ib_mmap
;
3068 dev
->ib_dev
.alloc_pd
= mlx5_ib_alloc_pd
;
3069 dev
->ib_dev
.dealloc_pd
= mlx5_ib_dealloc_pd
;
3070 dev
->ib_dev
.create_ah
= mlx5_ib_create_ah
;
3071 dev
->ib_dev
.query_ah
= mlx5_ib_query_ah
;
3072 dev
->ib_dev
.destroy_ah
= mlx5_ib_destroy_ah
;
3073 dev
->ib_dev
.create_srq
= mlx5_ib_create_srq
;
3074 dev
->ib_dev
.modify_srq
= mlx5_ib_modify_srq
;
3075 dev
->ib_dev
.query_srq
= mlx5_ib_query_srq
;
3076 dev
->ib_dev
.destroy_srq
= mlx5_ib_destroy_srq
;
3077 dev
->ib_dev
.post_srq_recv
= mlx5_ib_post_srq_recv
;
3078 dev
->ib_dev
.create_qp
= mlx5_ib_create_qp
;
3079 dev
->ib_dev
.modify_qp
= mlx5_ib_modify_qp
;
3080 dev
->ib_dev
.query_qp
= mlx5_ib_query_qp
;
3081 dev
->ib_dev
.destroy_qp
= mlx5_ib_destroy_qp
;
3082 dev
->ib_dev
.post_send
= mlx5_ib_post_send
;
3083 dev
->ib_dev
.post_recv
= mlx5_ib_post_recv
;
3084 dev
->ib_dev
.create_cq
= mlx5_ib_create_cq
;
3085 dev
->ib_dev
.modify_cq
= mlx5_ib_modify_cq
;
3086 dev
->ib_dev
.resize_cq
= mlx5_ib_resize_cq
;
3087 dev
->ib_dev
.destroy_cq
= mlx5_ib_destroy_cq
;
3088 dev
->ib_dev
.poll_cq
= mlx5_ib_poll_cq
;
3089 dev
->ib_dev
.req_notify_cq
= mlx5_ib_arm_cq
;
3090 dev
->ib_dev
.get_dma_mr
= mlx5_ib_get_dma_mr
;
3091 dev
->ib_dev
.reg_user_mr
= mlx5_ib_reg_user_mr
;
3092 dev
->ib_dev
.rereg_user_mr
= mlx5_ib_rereg_user_mr
;
3093 dev
->ib_dev
.dereg_mr
= mlx5_ib_dereg_mr
;
3094 dev
->ib_dev
.attach_mcast
= mlx5_ib_mcg_attach
;
3095 dev
->ib_dev
.detach_mcast
= mlx5_ib_mcg_detach
;
3096 dev
->ib_dev
.process_mad
= mlx5_ib_process_mad
;
3097 dev
->ib_dev
.alloc_mr
= mlx5_ib_alloc_mr
;
3098 dev
->ib_dev
.map_mr_sg
= mlx5_ib_map_mr_sg
;
3099 dev
->ib_dev
.check_mr_status
= mlx5_ib_check_mr_status
;
3100 dev
->ib_dev
.get_port_immutable
= mlx5_port_immutable
;
3101 dev
->ib_dev
.get_dev_fw_str
= get_dev_fw_str
;
3102 if (mlx5_core_is_pf(mdev
)) {
3103 dev
->ib_dev
.get_vf_config
= mlx5_ib_get_vf_config
;
3104 dev
->ib_dev
.set_vf_link_state
= mlx5_ib_set_vf_link_state
;
3105 dev
->ib_dev
.get_vf_stats
= mlx5_ib_get_vf_stats
;
3106 dev
->ib_dev
.set_vf_guid
= mlx5_ib_set_vf_guid
;
3109 dev
->ib_dev
.disassociate_ucontext
= mlx5_ib_disassociate_ucontext
;
3111 mlx5_ib_internal_fill_odp_caps(dev
);
3113 dev
->umr_fence
= mlx5_get_umr_fence(MLX5_CAP_GEN(mdev
, umr_fence
));
3115 if (MLX5_CAP_GEN(mdev
, imaicl
)) {
3116 dev
->ib_dev
.alloc_mw
= mlx5_ib_alloc_mw
;
3117 dev
->ib_dev
.dealloc_mw
= mlx5_ib_dealloc_mw
;
3118 dev
->ib_dev
.uverbs_cmd_mask
|=
3119 (1ull << IB_USER_VERBS_CMD_ALLOC_MW
) |
3120 (1ull << IB_USER_VERBS_CMD_DEALLOC_MW
);
3123 if (MLX5_CAP_GEN(dev
->mdev
, out_of_seq_cnt
) &&
3124 MLX5_CAP_GEN(dev
->mdev
, retransmission_q_counters
)) {
3125 dev
->ib_dev
.get_hw_stats
= mlx5_ib_get_hw_stats
;
3126 dev
->ib_dev
.alloc_hw_stats
= mlx5_ib_alloc_hw_stats
;
3129 if (MLX5_CAP_GEN(mdev
, xrc
)) {
3130 dev
->ib_dev
.alloc_xrcd
= mlx5_ib_alloc_xrcd
;
3131 dev
->ib_dev
.dealloc_xrcd
= mlx5_ib_dealloc_xrcd
;
3132 dev
->ib_dev
.uverbs_cmd_mask
|=
3133 (1ull << IB_USER_VERBS_CMD_OPEN_XRCD
) |
3134 (1ull << IB_USER_VERBS_CMD_CLOSE_XRCD
);
3137 if (mlx5_ib_port_link_layer(&dev
->ib_dev
, 1) ==
3138 IB_LINK_LAYER_ETHERNET
) {
3139 dev
->ib_dev
.create_flow
= mlx5_ib_create_flow
;
3140 dev
->ib_dev
.destroy_flow
= mlx5_ib_destroy_flow
;
3141 dev
->ib_dev
.create_wq
= mlx5_ib_create_wq
;
3142 dev
->ib_dev
.modify_wq
= mlx5_ib_modify_wq
;
3143 dev
->ib_dev
.destroy_wq
= mlx5_ib_destroy_wq
;
3144 dev
->ib_dev
.create_rwq_ind_table
= mlx5_ib_create_rwq_ind_table
;
3145 dev
->ib_dev
.destroy_rwq_ind_table
= mlx5_ib_destroy_rwq_ind_table
;
3146 dev
->ib_dev
.uverbs_ex_cmd_mask
|=
3147 (1ull << IB_USER_VERBS_EX_CMD_CREATE_FLOW
) |
3148 (1ull << IB_USER_VERBS_EX_CMD_DESTROY_FLOW
) |
3149 (1ull << IB_USER_VERBS_EX_CMD_CREATE_WQ
) |
3150 (1ull << IB_USER_VERBS_EX_CMD_MODIFY_WQ
) |
3151 (1ull << IB_USER_VERBS_EX_CMD_DESTROY_WQ
) |
3152 (1ull << IB_USER_VERBS_EX_CMD_CREATE_RWQ_IND_TBL
) |
3153 (1ull << IB_USER_VERBS_EX_CMD_DESTROY_RWQ_IND_TBL
);
3155 err
= init_node_data(dev
);
3159 mutex_init(&dev
->flow_db
.lock
);
3160 mutex_init(&dev
->cap_mask_mutex
);
3161 INIT_LIST_HEAD(&dev
->qp_list
);
3162 spin_lock_init(&dev
->reset_flow_resource_lock
);
3164 if (ll
== IB_LINK_LAYER_ETHERNET
) {
3165 err
= mlx5_enable_roce(dev
);
3170 err
= create_dev_resources(&dev
->devr
);
3172 goto err_disable_roce
;
3174 err
= mlx5_ib_odp_init_one(dev
);
3178 if (MLX5_CAP_GEN(dev
->mdev
, max_qp_cnt
)) {
3179 err
= mlx5_ib_alloc_q_counters(dev
);
3184 err
= ib_register_device(&dev
->ib_dev
, NULL
);
3188 err
= create_umr_res(dev
);
3192 for (i
= 0; i
< ARRAY_SIZE(mlx5_class_attributes
); i
++) {
3193 err
= device_create_file(&dev
->ib_dev
.dev
,
3194 mlx5_class_attributes
[i
]);
3199 dev
->ib_active
= true;
3204 destroy_umrc_res(dev
);
3207 ib_unregister_device(&dev
->ib_dev
);
3210 if (MLX5_CAP_GEN(dev
->mdev
, max_qp_cnt
))
3211 mlx5_ib_dealloc_q_counters(dev
);
3214 mlx5_ib_odp_remove_one(dev
);
3217 destroy_dev_resources(&dev
->devr
);
3220 if (ll
== IB_LINK_LAYER_ETHERNET
) {
3221 mlx5_disable_roce(dev
);
3222 mlx5_remove_roce_notifier(dev
);
3229 ib_dealloc_device((struct ib_device
*)dev
);
3234 static void mlx5_ib_remove(struct mlx5_core_dev
*mdev
, void *context
)
3236 struct mlx5_ib_dev
*dev
= context
;
3237 enum rdma_link_layer ll
= mlx5_ib_port_link_layer(&dev
->ib_dev
, 1);
3239 mlx5_remove_roce_notifier(dev
);
3240 ib_unregister_device(&dev
->ib_dev
);
3241 if (MLX5_CAP_GEN(dev
->mdev
, max_qp_cnt
))
3242 mlx5_ib_dealloc_q_counters(dev
);
3243 destroy_umrc_res(dev
);
3244 mlx5_ib_odp_remove_one(dev
);
3245 destroy_dev_resources(&dev
->devr
);
3246 if (ll
== IB_LINK_LAYER_ETHERNET
)
3247 mlx5_disable_roce(dev
);
3249 ib_dealloc_device(&dev
->ib_dev
);
3252 static struct mlx5_interface mlx5_ib_interface
= {
3254 .remove
= mlx5_ib_remove
,
3255 .event
= mlx5_ib_event
,
3256 .protocol
= MLX5_INTERFACE_PROTOCOL_IB
,
3259 static int __init
mlx5_ib_init(void)
3263 if (deprecated_prof_sel
!= 2)
3264 pr_warn("prof_sel is deprecated for mlx5_ib, set it for mlx5_core\n");
3266 err
= mlx5_ib_odp_init();
3270 err
= mlx5_register_interface(&mlx5_ib_interface
);
3277 mlx5_ib_odp_cleanup();
3281 static void __exit
mlx5_ib_cleanup(void)
3283 mlx5_unregister_interface(&mlx5_ib_interface
);
3284 mlx5_ib_odp_cleanup();
3287 module_init(mlx5_ib_init
);
3288 module_exit(mlx5_ib_cleanup
);