2 * Copyright (c) 2013-2015, Mellanox Technologies. All rights reserved.
4 * This software is available to you under a choice of one of two
5 * licenses. You may choose to be licensed under the terms of the GNU
6 * General Public License (GPL) Version 2, available from the file
7 * COPYING in the main directory of this source tree, or the
8 * OpenIB.org BSD license below:
10 * Redistribution and use in source and binary forms, with or
11 * without modification, are permitted provided that the following
14 * - Redistributions of source code must retain the above
15 * copyright notice, this list of conditions and the following
18 * - Redistributions in binary form must reproduce the above
19 * copyright notice, this list of conditions and the following
20 * disclaimer in the documentation and/or other materials
21 * provided with the distribution.
23 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
27 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
28 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
29 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
33 #include <linux/debugfs.h>
34 #include <linux/highmem.h>
35 #include <linux/module.h>
36 #include <linux/init.h>
37 #include <linux/errno.h>
38 #include <linux/pci.h>
39 #include <linux/dma-mapping.h>
40 #include <linux/slab.h>
41 #include <linux/bitmap.h>
42 #if defined(CONFIG_X86)
45 #include <linux/sched.h>
46 #include <linux/sched/mm.h>
47 #include <linux/sched/task.h>
48 #include <linux/delay.h>
49 #include <rdma/ib_user_verbs.h>
50 #include <rdma/ib_addr.h>
51 #include <rdma/ib_cache.h>
52 #include <linux/mlx5/port.h>
53 #include <linux/mlx5/vport.h>
54 #include <linux/mlx5/fs.h>
55 #include <linux/list.h>
56 #include <rdma/ib_smi.h>
57 #include <rdma/ib_umem.h>
59 #include <linux/etherdevice.h>
63 #include <linux/mlx5/fs_helpers.h>
64 #include <linux/mlx5/accel.h>
65 #include <rdma/uverbs_std_types.h>
66 #include <rdma/mlx5_user_ioctl_verbs.h>
67 #include <rdma/mlx5_user_ioctl_cmds.h>
69 #define UVERBS_MODULE_NAME mlx5_ib
70 #include <rdma/uverbs_named_ioctl.h>
72 #define DRIVER_NAME "mlx5_ib"
73 #define DRIVER_VERSION "5.0-0"
75 MODULE_AUTHOR("Eli Cohen <eli@mellanox.com>");
76 MODULE_DESCRIPTION("Mellanox Connect-IB HCA IB driver");
77 MODULE_LICENSE("Dual BSD/GPL");
79 static char mlx5_version
[] =
80 DRIVER_NAME
": Mellanox Connect-IB Infiniband driver v"
83 struct mlx5_ib_event_work
{
84 struct work_struct work
;
85 struct mlx5_core_dev
*dev
;
87 enum mlx5_dev_event event
;
92 MLX5_ATOMIC_SIZE_QP_8BYTES
= 1 << 3,
95 static struct workqueue_struct
*mlx5_ib_event_wq
;
96 static LIST_HEAD(mlx5_ib_unaffiliated_port_list
);
97 static LIST_HEAD(mlx5_ib_dev_list
);
99 * This mutex should be held when accessing either of the above lists
101 static DEFINE_MUTEX(mlx5_ib_multiport_mutex
);
103 /* We can't use an array for xlt_emergency_page because dma_map_single
104 * doesn't work on kernel modules memory
106 static unsigned long xlt_emergency_page
;
107 static struct mutex xlt_emergency_page_mutex
;
109 struct mlx5_ib_dev
*mlx5_ib_get_ibdev_from_mpi(struct mlx5_ib_multiport_info
*mpi
)
111 struct mlx5_ib_dev
*dev
;
113 mutex_lock(&mlx5_ib_multiport_mutex
);
115 mutex_unlock(&mlx5_ib_multiport_mutex
);
119 static enum rdma_link_layer
120 mlx5_port_type_cap_to_rdma_ll(int port_type_cap
)
122 switch (port_type_cap
) {
123 case MLX5_CAP_PORT_TYPE_IB
:
124 return IB_LINK_LAYER_INFINIBAND
;
125 case MLX5_CAP_PORT_TYPE_ETH
:
126 return IB_LINK_LAYER_ETHERNET
;
128 return IB_LINK_LAYER_UNSPECIFIED
;
132 static enum rdma_link_layer
133 mlx5_ib_port_link_layer(struct ib_device
*device
, u8 port_num
)
135 struct mlx5_ib_dev
*dev
= to_mdev(device
);
136 int port_type_cap
= MLX5_CAP_GEN(dev
->mdev
, port_type
);
138 return mlx5_port_type_cap_to_rdma_ll(port_type_cap
);
141 static int get_port_state(struct ib_device
*ibdev
,
143 enum ib_port_state
*state
)
145 struct ib_port_attr attr
;
148 memset(&attr
, 0, sizeof(attr
));
149 ret
= ibdev
->query_port(ibdev
, port_num
, &attr
);
155 static int mlx5_netdev_event(struct notifier_block
*this,
156 unsigned long event
, void *ptr
)
158 struct mlx5_roce
*roce
= container_of(this, struct mlx5_roce
, nb
);
159 struct net_device
*ndev
= netdev_notifier_info_to_dev(ptr
);
160 u8 port_num
= roce
->native_port_num
;
161 struct mlx5_core_dev
*mdev
;
162 struct mlx5_ib_dev
*ibdev
;
165 mdev
= mlx5_ib_get_native_port_mdev(ibdev
, port_num
, NULL
);
170 case NETDEV_REGISTER
:
171 case NETDEV_UNREGISTER
:
172 write_lock(&roce
->netdev_lock
);
174 struct mlx5_eswitch
*esw
= ibdev
->mdev
->priv
.eswitch
;
175 struct net_device
*rep_ndev
;
177 rep_ndev
= mlx5_ib_get_rep_netdev(esw
,
179 if (rep_ndev
== ndev
)
180 roce
->netdev
= (event
== NETDEV_UNREGISTER
) ?
182 } else if (ndev
->dev
.parent
== &mdev
->pdev
->dev
) {
183 roce
->netdev
= (event
== NETDEV_UNREGISTER
) ?
186 write_unlock(&roce
->netdev_lock
);
192 struct net_device
*lag_ndev
= mlx5_lag_get_roce_netdev(mdev
);
193 struct net_device
*upper
= NULL
;
196 upper
= netdev_master_upper_dev_get(lag_ndev
);
200 if ((upper
== ndev
|| (!upper
&& ndev
== roce
->netdev
))
201 && ibdev
->ib_active
) {
202 struct ib_event ibev
= { };
203 enum ib_port_state port_state
;
205 if (get_port_state(&ibdev
->ib_dev
, port_num
,
209 if (roce
->last_port_state
== port_state
)
212 roce
->last_port_state
= port_state
;
213 ibev
.device
= &ibdev
->ib_dev
;
214 if (port_state
== IB_PORT_DOWN
)
215 ibev
.event
= IB_EVENT_PORT_ERR
;
216 else if (port_state
== IB_PORT_ACTIVE
)
217 ibev
.event
= IB_EVENT_PORT_ACTIVE
;
221 ibev
.element
.port_num
= port_num
;
222 ib_dispatch_event(&ibev
);
231 mlx5_ib_put_native_port_mdev(ibdev
, port_num
);
235 static struct net_device
*mlx5_ib_get_netdev(struct ib_device
*device
,
238 struct mlx5_ib_dev
*ibdev
= to_mdev(device
);
239 struct net_device
*ndev
;
240 struct mlx5_core_dev
*mdev
;
242 mdev
= mlx5_ib_get_native_port_mdev(ibdev
, port_num
, NULL
);
246 ndev
= mlx5_lag_get_roce_netdev(mdev
);
250 /* Ensure ndev does not disappear before we invoke dev_hold()
252 read_lock(&ibdev
->roce
[port_num
- 1].netdev_lock
);
253 ndev
= ibdev
->roce
[port_num
- 1].netdev
;
256 read_unlock(&ibdev
->roce
[port_num
- 1].netdev_lock
);
259 mlx5_ib_put_native_port_mdev(ibdev
, port_num
);
263 struct mlx5_core_dev
*mlx5_ib_get_native_port_mdev(struct mlx5_ib_dev
*ibdev
,
267 enum rdma_link_layer ll
= mlx5_ib_port_link_layer(&ibdev
->ib_dev
,
269 struct mlx5_core_dev
*mdev
= NULL
;
270 struct mlx5_ib_multiport_info
*mpi
;
271 struct mlx5_ib_port
*port
;
273 if (!mlx5_core_mp_enabled(ibdev
->mdev
) ||
274 ll
!= IB_LINK_LAYER_ETHERNET
) {
276 *native_port_num
= ib_port_num
;
281 *native_port_num
= 1;
283 port
= &ibdev
->port
[ib_port_num
- 1];
287 spin_lock(&port
->mp
.mpi_lock
);
288 mpi
= ibdev
->port
[ib_port_num
- 1].mp
.mpi
;
289 if (mpi
&& !mpi
->unaffiliate
) {
291 /* If it's the master no need to refcount, it'll exist
292 * as long as the ib_dev exists.
297 spin_unlock(&port
->mp
.mpi_lock
);
302 void mlx5_ib_put_native_port_mdev(struct mlx5_ib_dev
*ibdev
, u8 port_num
)
304 enum rdma_link_layer ll
= mlx5_ib_port_link_layer(&ibdev
->ib_dev
,
306 struct mlx5_ib_multiport_info
*mpi
;
307 struct mlx5_ib_port
*port
;
309 if (!mlx5_core_mp_enabled(ibdev
->mdev
) || ll
!= IB_LINK_LAYER_ETHERNET
)
312 port
= &ibdev
->port
[port_num
- 1];
314 spin_lock(&port
->mp
.mpi_lock
);
315 mpi
= ibdev
->port
[port_num
- 1].mp
.mpi
;
320 if (mpi
->unaffiliate
)
321 complete(&mpi
->unref_comp
);
323 spin_unlock(&port
->mp
.mpi_lock
);
326 static int translate_eth_proto_oper(u32 eth_proto_oper
, u8
*active_speed
,
329 switch (eth_proto_oper
) {
330 case MLX5E_PROT_MASK(MLX5E_1000BASE_CX_SGMII
):
331 case MLX5E_PROT_MASK(MLX5E_1000BASE_KX
):
332 case MLX5E_PROT_MASK(MLX5E_100BASE_TX
):
333 case MLX5E_PROT_MASK(MLX5E_1000BASE_T
):
334 *active_width
= IB_WIDTH_1X
;
335 *active_speed
= IB_SPEED_SDR
;
337 case MLX5E_PROT_MASK(MLX5E_10GBASE_T
):
338 case MLX5E_PROT_MASK(MLX5E_10GBASE_CX4
):
339 case MLX5E_PROT_MASK(MLX5E_10GBASE_KX4
):
340 case MLX5E_PROT_MASK(MLX5E_10GBASE_KR
):
341 case MLX5E_PROT_MASK(MLX5E_10GBASE_CR
):
342 case MLX5E_PROT_MASK(MLX5E_10GBASE_SR
):
343 case MLX5E_PROT_MASK(MLX5E_10GBASE_ER
):
344 *active_width
= IB_WIDTH_1X
;
345 *active_speed
= IB_SPEED_QDR
;
347 case MLX5E_PROT_MASK(MLX5E_25GBASE_CR
):
348 case MLX5E_PROT_MASK(MLX5E_25GBASE_KR
):
349 case MLX5E_PROT_MASK(MLX5E_25GBASE_SR
):
350 *active_width
= IB_WIDTH_1X
;
351 *active_speed
= IB_SPEED_EDR
;
353 case MLX5E_PROT_MASK(MLX5E_40GBASE_CR4
):
354 case MLX5E_PROT_MASK(MLX5E_40GBASE_KR4
):
355 case MLX5E_PROT_MASK(MLX5E_40GBASE_SR4
):
356 case MLX5E_PROT_MASK(MLX5E_40GBASE_LR4
):
357 *active_width
= IB_WIDTH_4X
;
358 *active_speed
= IB_SPEED_QDR
;
360 case MLX5E_PROT_MASK(MLX5E_50GBASE_CR2
):
361 case MLX5E_PROT_MASK(MLX5E_50GBASE_KR2
):
362 case MLX5E_PROT_MASK(MLX5E_50GBASE_SR2
):
363 *active_width
= IB_WIDTH_1X
;
364 *active_speed
= IB_SPEED_HDR
;
366 case MLX5E_PROT_MASK(MLX5E_56GBASE_R4
):
367 *active_width
= IB_WIDTH_4X
;
368 *active_speed
= IB_SPEED_FDR
;
370 case MLX5E_PROT_MASK(MLX5E_100GBASE_CR4
):
371 case MLX5E_PROT_MASK(MLX5E_100GBASE_SR4
):
372 case MLX5E_PROT_MASK(MLX5E_100GBASE_KR4
):
373 case MLX5E_PROT_MASK(MLX5E_100GBASE_LR4
):
374 *active_width
= IB_WIDTH_4X
;
375 *active_speed
= IB_SPEED_EDR
;
384 static int mlx5_query_port_roce(struct ib_device
*device
, u8 port_num
,
385 struct ib_port_attr
*props
)
387 struct mlx5_ib_dev
*dev
= to_mdev(device
);
388 struct mlx5_core_dev
*mdev
;
389 struct net_device
*ndev
, *upper
;
390 enum ib_mtu ndev_ib_mtu
;
391 bool put_mdev
= true;
397 mdev
= mlx5_ib_get_native_port_mdev(dev
, port_num
, &mdev_port_num
);
399 /* This means the port isn't affiliated yet. Get the
400 * info for the master port instead.
408 /* Possible bad flows are checked before filling out props so in case
409 * of an error it will still be zeroed out.
411 err
= mlx5_query_port_eth_proto_oper(mdev
, ð_prot_oper
,
416 props
->active_width
= IB_WIDTH_4X
;
417 props
->active_speed
= IB_SPEED_QDR
;
419 translate_eth_proto_oper(eth_prot_oper
, &props
->active_speed
,
420 &props
->active_width
);
422 props
->port_cap_flags
|= IB_PORT_CM_SUP
;
423 props
->ip_gids
= true;
425 props
->gid_tbl_len
= MLX5_CAP_ROCE(dev
->mdev
,
426 roce_address_table_size
);
427 props
->max_mtu
= IB_MTU_4096
;
428 props
->max_msg_sz
= 1 << MLX5_CAP_GEN(dev
->mdev
, log_max_msg
);
429 props
->pkey_tbl_len
= 1;
430 props
->state
= IB_PORT_DOWN
;
431 props
->phys_state
= 3;
433 mlx5_query_nic_vport_qkey_viol_cntr(mdev
, &qkey_viol_cntr
);
434 props
->qkey_viol_cntr
= qkey_viol_cntr
;
436 /* If this is a stub query for an unaffiliated port stop here */
440 ndev
= mlx5_ib_get_netdev(device
, port_num
);
444 if (mlx5_lag_is_active(dev
->mdev
)) {
446 upper
= netdev_master_upper_dev_get_rcu(ndev
);
455 if (netif_running(ndev
) && netif_carrier_ok(ndev
)) {
456 props
->state
= IB_PORT_ACTIVE
;
457 props
->phys_state
= 5;
460 ndev_ib_mtu
= iboe_get_mtu(ndev
->mtu
);
464 props
->active_mtu
= min(props
->max_mtu
, ndev_ib_mtu
);
467 mlx5_ib_put_native_port_mdev(dev
, port_num
);
471 static int set_roce_addr(struct mlx5_ib_dev
*dev
, u8 port_num
,
472 unsigned int index
, const union ib_gid
*gid
,
473 const struct ib_gid_attr
*attr
)
475 enum ib_gid_type gid_type
= IB_GID_TYPE_IB
;
483 gid_type
= attr
->gid_type
;
484 ether_addr_copy(mac
, attr
->ndev
->dev_addr
);
486 if (is_vlan_dev(attr
->ndev
)) {
488 vlan_id
= vlan_dev_vlan_id(attr
->ndev
);
494 roce_version
= MLX5_ROCE_VERSION_1
;
496 case IB_GID_TYPE_ROCE_UDP_ENCAP
:
497 roce_version
= MLX5_ROCE_VERSION_2
;
498 if (ipv6_addr_v4mapped((void *)gid
))
499 roce_l3_type
= MLX5_ROCE_L3_TYPE_IPV4
;
501 roce_l3_type
= MLX5_ROCE_L3_TYPE_IPV6
;
505 mlx5_ib_warn(dev
, "Unexpected GID type %u\n", gid_type
);
508 return mlx5_core_roce_gid_set(dev
->mdev
, index
, roce_version
,
509 roce_l3_type
, gid
->raw
, mac
, vlan
,
513 static int mlx5_ib_add_gid(const struct ib_gid_attr
*attr
,
514 __always_unused
void **context
)
516 return set_roce_addr(to_mdev(attr
->device
), attr
->port_num
,
517 attr
->index
, &attr
->gid
, attr
);
520 static int mlx5_ib_del_gid(const struct ib_gid_attr
*attr
,
521 __always_unused
void **context
)
523 return set_roce_addr(to_mdev(attr
->device
), attr
->port_num
,
524 attr
->index
, NULL
, NULL
);
527 __be16
mlx5_get_roce_udp_sport(struct mlx5_ib_dev
*dev
,
528 const struct ib_gid_attr
*attr
)
530 if (attr
->gid_type
!= IB_GID_TYPE_ROCE_UDP_ENCAP
)
533 return cpu_to_be16(MLX5_CAP_ROCE(dev
->mdev
, r_roce_min_src_udp_port
));
536 static int mlx5_use_mad_ifc(struct mlx5_ib_dev
*dev
)
538 if (MLX5_CAP_GEN(dev
->mdev
, port_type
) == MLX5_CAP_PORT_TYPE_IB
)
539 return !MLX5_CAP_GEN(dev
->mdev
, ib_virt
);
544 MLX5_VPORT_ACCESS_METHOD_MAD
,
545 MLX5_VPORT_ACCESS_METHOD_HCA
,
546 MLX5_VPORT_ACCESS_METHOD_NIC
,
549 static int mlx5_get_vport_access_method(struct ib_device
*ibdev
)
551 if (mlx5_use_mad_ifc(to_mdev(ibdev
)))
552 return MLX5_VPORT_ACCESS_METHOD_MAD
;
554 if (mlx5_ib_port_link_layer(ibdev
, 1) ==
555 IB_LINK_LAYER_ETHERNET
)
556 return MLX5_VPORT_ACCESS_METHOD_NIC
;
558 return MLX5_VPORT_ACCESS_METHOD_HCA
;
561 static void get_atomic_caps(struct mlx5_ib_dev
*dev
,
563 struct ib_device_attr
*props
)
566 u8 atomic_operations
= MLX5_CAP_ATOMIC(dev
->mdev
, atomic_operations
);
567 u8 atomic_req_8B_endianness_mode
=
568 MLX5_CAP_ATOMIC(dev
->mdev
, atomic_req_8B_endianness_mode
);
570 /* Check if HW supports 8 bytes standard atomic operations and capable
571 * of host endianness respond
573 tmp
= MLX5_ATOMIC_OPS_CMP_SWAP
| MLX5_ATOMIC_OPS_FETCH_ADD
;
574 if (((atomic_operations
& tmp
) == tmp
) &&
575 (atomic_size_qp
& MLX5_ATOMIC_SIZE_QP_8BYTES
) &&
576 (atomic_req_8B_endianness_mode
)) {
577 props
->atomic_cap
= IB_ATOMIC_HCA
;
579 props
->atomic_cap
= IB_ATOMIC_NONE
;
583 static void get_atomic_caps_qp(struct mlx5_ib_dev
*dev
,
584 struct ib_device_attr
*props
)
586 u8 atomic_size_qp
= MLX5_CAP_ATOMIC(dev
->mdev
, atomic_size_qp
);
588 get_atomic_caps(dev
, atomic_size_qp
, props
);
591 static void get_atomic_caps_dc(struct mlx5_ib_dev
*dev
,
592 struct ib_device_attr
*props
)
594 u8 atomic_size_qp
= MLX5_CAP_ATOMIC(dev
->mdev
, atomic_size_dc
);
596 get_atomic_caps(dev
, atomic_size_qp
, props
);
599 bool mlx5_ib_dc_atomic_is_supported(struct mlx5_ib_dev
*dev
)
601 struct ib_device_attr props
= {};
603 get_atomic_caps_dc(dev
, &props
);
604 return (props
.atomic_cap
== IB_ATOMIC_HCA
) ? true : false;
606 static int mlx5_query_system_image_guid(struct ib_device
*ibdev
,
607 __be64
*sys_image_guid
)
609 struct mlx5_ib_dev
*dev
= to_mdev(ibdev
);
610 struct mlx5_core_dev
*mdev
= dev
->mdev
;
614 switch (mlx5_get_vport_access_method(ibdev
)) {
615 case MLX5_VPORT_ACCESS_METHOD_MAD
:
616 return mlx5_query_mad_ifc_system_image_guid(ibdev
,
619 case MLX5_VPORT_ACCESS_METHOD_HCA
:
620 err
= mlx5_query_hca_vport_system_image_guid(mdev
, &tmp
);
623 case MLX5_VPORT_ACCESS_METHOD_NIC
:
624 err
= mlx5_query_nic_vport_system_image_guid(mdev
, &tmp
);
632 *sys_image_guid
= cpu_to_be64(tmp
);
638 static int mlx5_query_max_pkeys(struct ib_device
*ibdev
,
641 struct mlx5_ib_dev
*dev
= to_mdev(ibdev
);
642 struct mlx5_core_dev
*mdev
= dev
->mdev
;
644 switch (mlx5_get_vport_access_method(ibdev
)) {
645 case MLX5_VPORT_ACCESS_METHOD_MAD
:
646 return mlx5_query_mad_ifc_max_pkeys(ibdev
, max_pkeys
);
648 case MLX5_VPORT_ACCESS_METHOD_HCA
:
649 case MLX5_VPORT_ACCESS_METHOD_NIC
:
650 *max_pkeys
= mlx5_to_sw_pkey_sz(MLX5_CAP_GEN(mdev
,
659 static int mlx5_query_vendor_id(struct ib_device
*ibdev
,
662 struct mlx5_ib_dev
*dev
= to_mdev(ibdev
);
664 switch (mlx5_get_vport_access_method(ibdev
)) {
665 case MLX5_VPORT_ACCESS_METHOD_MAD
:
666 return mlx5_query_mad_ifc_vendor_id(ibdev
, vendor_id
);
668 case MLX5_VPORT_ACCESS_METHOD_HCA
:
669 case MLX5_VPORT_ACCESS_METHOD_NIC
:
670 return mlx5_core_query_vendor_id(dev
->mdev
, vendor_id
);
677 static int mlx5_query_node_guid(struct mlx5_ib_dev
*dev
,
683 switch (mlx5_get_vport_access_method(&dev
->ib_dev
)) {
684 case MLX5_VPORT_ACCESS_METHOD_MAD
:
685 return mlx5_query_mad_ifc_node_guid(dev
, node_guid
);
687 case MLX5_VPORT_ACCESS_METHOD_HCA
:
688 err
= mlx5_query_hca_vport_node_guid(dev
->mdev
, &tmp
);
691 case MLX5_VPORT_ACCESS_METHOD_NIC
:
692 err
= mlx5_query_nic_vport_node_guid(dev
->mdev
, &tmp
);
700 *node_guid
= cpu_to_be64(tmp
);
705 struct mlx5_reg_node_desc
{
706 u8 desc
[IB_DEVICE_NODE_DESC_MAX
];
709 static int mlx5_query_node_desc(struct mlx5_ib_dev
*dev
, char *node_desc
)
711 struct mlx5_reg_node_desc in
;
713 if (mlx5_use_mad_ifc(dev
))
714 return mlx5_query_mad_ifc_node_desc(dev
, node_desc
);
716 memset(&in
, 0, sizeof(in
));
718 return mlx5_core_access_reg(dev
->mdev
, &in
, sizeof(in
), node_desc
,
719 sizeof(struct mlx5_reg_node_desc
),
720 MLX5_REG_NODE_DESC
, 0, 0);
723 static int mlx5_ib_query_device(struct ib_device
*ibdev
,
724 struct ib_device_attr
*props
,
725 struct ib_udata
*uhw
)
727 struct mlx5_ib_dev
*dev
= to_mdev(ibdev
);
728 struct mlx5_core_dev
*mdev
= dev
->mdev
;
733 u64 min_page_size
= 1ull << MLX5_CAP_GEN(mdev
, log_pg_sz
);
734 bool raw_support
= !mlx5_core_mp_enabled(mdev
);
735 struct mlx5_ib_query_device_resp resp
= {};
739 resp_len
= sizeof(resp
.comp_mask
) + sizeof(resp
.response_length
);
740 if (uhw
->outlen
&& uhw
->outlen
< resp_len
)
743 resp
.response_length
= resp_len
;
745 if (uhw
->inlen
&& !ib_is_udata_cleared(uhw
, 0, uhw
->inlen
))
748 memset(props
, 0, sizeof(*props
));
749 err
= mlx5_query_system_image_guid(ibdev
,
750 &props
->sys_image_guid
);
754 err
= mlx5_query_max_pkeys(ibdev
, &props
->max_pkeys
);
758 err
= mlx5_query_vendor_id(ibdev
, &props
->vendor_id
);
762 props
->fw_ver
= ((u64
)fw_rev_maj(dev
->mdev
) << 32) |
763 (fw_rev_min(dev
->mdev
) << 16) |
764 fw_rev_sub(dev
->mdev
);
765 props
->device_cap_flags
= IB_DEVICE_CHANGE_PHY_PORT
|
766 IB_DEVICE_PORT_ACTIVE_EVENT
|
767 IB_DEVICE_SYS_IMAGE_GUID
|
768 IB_DEVICE_RC_RNR_NAK_GEN
;
770 if (MLX5_CAP_GEN(mdev
, pkv
))
771 props
->device_cap_flags
|= IB_DEVICE_BAD_PKEY_CNTR
;
772 if (MLX5_CAP_GEN(mdev
, qkv
))
773 props
->device_cap_flags
|= IB_DEVICE_BAD_QKEY_CNTR
;
774 if (MLX5_CAP_GEN(mdev
, apm
))
775 props
->device_cap_flags
|= IB_DEVICE_AUTO_PATH_MIG
;
776 if (MLX5_CAP_GEN(mdev
, xrc
))
777 props
->device_cap_flags
|= IB_DEVICE_XRC
;
778 if (MLX5_CAP_GEN(mdev
, imaicl
)) {
779 props
->device_cap_flags
|= IB_DEVICE_MEM_WINDOW
|
780 IB_DEVICE_MEM_WINDOW_TYPE_2B
;
781 props
->max_mw
= 1 << MLX5_CAP_GEN(mdev
, log_max_mkey
);
782 /* We support 'Gappy' memory registration too */
783 props
->device_cap_flags
|= IB_DEVICE_SG_GAPS_REG
;
785 props
->device_cap_flags
|= IB_DEVICE_MEM_MGT_EXTENSIONS
;
786 if (MLX5_CAP_GEN(mdev
, sho
)) {
787 props
->device_cap_flags
|= IB_DEVICE_SIGNATURE_HANDOVER
;
788 /* At this stage no support for signature handover */
789 props
->sig_prot_cap
= IB_PROT_T10DIF_TYPE_1
|
790 IB_PROT_T10DIF_TYPE_2
|
791 IB_PROT_T10DIF_TYPE_3
;
792 props
->sig_guard_cap
= IB_GUARD_T10DIF_CRC
|
793 IB_GUARD_T10DIF_CSUM
;
795 if (MLX5_CAP_GEN(mdev
, block_lb_mc
))
796 props
->device_cap_flags
|= IB_DEVICE_BLOCK_MULTICAST_LOOPBACK
;
798 if (MLX5_CAP_GEN(dev
->mdev
, eth_net_offloads
) && raw_support
) {
799 if (MLX5_CAP_ETH(mdev
, csum_cap
)) {
800 /* Legacy bit to support old userspace libraries */
801 props
->device_cap_flags
|= IB_DEVICE_RAW_IP_CSUM
;
802 props
->raw_packet_caps
|= IB_RAW_PACKET_CAP_IP_CSUM
;
805 if (MLX5_CAP_ETH(dev
->mdev
, vlan_cap
))
806 props
->raw_packet_caps
|=
807 IB_RAW_PACKET_CAP_CVLAN_STRIPPING
;
809 if (field_avail(typeof(resp
), tso_caps
, uhw
->outlen
)) {
810 max_tso
= MLX5_CAP_ETH(mdev
, max_lso_cap
);
812 resp
.tso_caps
.max_tso
= 1 << max_tso
;
813 resp
.tso_caps
.supported_qpts
|=
814 1 << IB_QPT_RAW_PACKET
;
815 resp
.response_length
+= sizeof(resp
.tso_caps
);
819 if (field_avail(typeof(resp
), rss_caps
, uhw
->outlen
)) {
820 resp
.rss_caps
.rx_hash_function
=
821 MLX5_RX_HASH_FUNC_TOEPLITZ
;
822 resp
.rss_caps
.rx_hash_fields_mask
=
823 MLX5_RX_HASH_SRC_IPV4
|
824 MLX5_RX_HASH_DST_IPV4
|
825 MLX5_RX_HASH_SRC_IPV6
|
826 MLX5_RX_HASH_DST_IPV6
|
827 MLX5_RX_HASH_SRC_PORT_TCP
|
828 MLX5_RX_HASH_DST_PORT_TCP
|
829 MLX5_RX_HASH_SRC_PORT_UDP
|
830 MLX5_RX_HASH_DST_PORT_UDP
|
832 if (mlx5_accel_ipsec_device_caps(dev
->mdev
) &
833 MLX5_ACCEL_IPSEC_CAP_DEVICE
)
834 resp
.rss_caps
.rx_hash_fields_mask
|=
835 MLX5_RX_HASH_IPSEC_SPI
;
836 resp
.response_length
+= sizeof(resp
.rss_caps
);
839 if (field_avail(typeof(resp
), tso_caps
, uhw
->outlen
))
840 resp
.response_length
+= sizeof(resp
.tso_caps
);
841 if (field_avail(typeof(resp
), rss_caps
, uhw
->outlen
))
842 resp
.response_length
+= sizeof(resp
.rss_caps
);
845 if (MLX5_CAP_GEN(mdev
, ipoib_basic_offloads
)) {
846 props
->device_cap_flags
|= IB_DEVICE_UD_IP_CSUM
;
847 props
->device_cap_flags
|= IB_DEVICE_UD_TSO
;
850 if (MLX5_CAP_GEN(dev
->mdev
, rq_delay_drop
) &&
851 MLX5_CAP_GEN(dev
->mdev
, general_notification_event
) &&
853 props
->raw_packet_caps
|= IB_RAW_PACKET_CAP_DELAY_DROP
;
855 if (MLX5_CAP_GEN(mdev
, ipoib_enhanced_offloads
) &&
856 MLX5_CAP_IPOIB_ENHANCED(mdev
, csum_cap
))
857 props
->device_cap_flags
|= IB_DEVICE_UD_IP_CSUM
;
859 if (MLX5_CAP_GEN(dev
->mdev
, eth_net_offloads
) &&
860 MLX5_CAP_ETH(dev
->mdev
, scatter_fcs
) &&
862 /* Legacy bit to support old userspace libraries */
863 props
->device_cap_flags
|= IB_DEVICE_RAW_SCATTER_FCS
;
864 props
->raw_packet_caps
|= IB_RAW_PACKET_CAP_SCATTER_FCS
;
867 if (MLX5_CAP_DEV_MEM(mdev
, memic
)) {
869 MLX5_CAP_DEV_MEM(mdev
, max_memic_size
);
872 if (mlx5_get_flow_namespace(dev
->mdev
, MLX5_FLOW_NAMESPACE_BYPASS
))
873 props
->device_cap_flags
|= IB_DEVICE_MANAGED_FLOW_STEERING
;
875 if (MLX5_CAP_GEN(mdev
, end_pad
))
876 props
->device_cap_flags
|= IB_DEVICE_PCI_WRITE_END_PADDING
;
878 props
->vendor_part_id
= mdev
->pdev
->device
;
879 props
->hw_ver
= mdev
->pdev
->revision
;
881 props
->max_mr_size
= ~0ull;
882 props
->page_size_cap
= ~(min_page_size
- 1);
883 props
->max_qp
= 1 << MLX5_CAP_GEN(mdev
, log_max_qp
);
884 props
->max_qp_wr
= 1 << MLX5_CAP_GEN(mdev
, log_max_qp_sz
);
885 max_rq_sg
= MLX5_CAP_GEN(mdev
, max_wqe_sz_rq
) /
886 sizeof(struct mlx5_wqe_data_seg
);
887 max_sq_desc
= min_t(int, MLX5_CAP_GEN(mdev
, max_wqe_sz_sq
), 512);
888 max_sq_sg
= (max_sq_desc
- sizeof(struct mlx5_wqe_ctrl_seg
) -
889 sizeof(struct mlx5_wqe_raddr_seg
)) /
890 sizeof(struct mlx5_wqe_data_seg
);
891 props
->max_send_sge
= max_sq_sg
;
892 props
->max_recv_sge
= max_rq_sg
;
893 props
->max_sge_rd
= MLX5_MAX_SGE_RD
;
894 props
->max_cq
= 1 << MLX5_CAP_GEN(mdev
, log_max_cq
);
895 props
->max_cqe
= (1 << MLX5_CAP_GEN(mdev
, log_max_cq_sz
)) - 1;
896 props
->max_mr
= 1 << MLX5_CAP_GEN(mdev
, log_max_mkey
);
897 props
->max_pd
= 1 << MLX5_CAP_GEN(mdev
, log_max_pd
);
898 props
->max_qp_rd_atom
= 1 << MLX5_CAP_GEN(mdev
, log_max_ra_req_qp
);
899 props
->max_qp_init_rd_atom
= 1 << MLX5_CAP_GEN(mdev
, log_max_ra_res_qp
);
900 props
->max_srq
= 1 << MLX5_CAP_GEN(mdev
, log_max_srq
);
901 props
->max_srq_wr
= (1 << MLX5_CAP_GEN(mdev
, log_max_srq_sz
)) - 1;
902 props
->local_ca_ack_delay
= MLX5_CAP_GEN(mdev
, local_ca_ack_delay
);
903 props
->max_res_rd_atom
= props
->max_qp_rd_atom
* props
->max_qp
;
904 props
->max_srq_sge
= max_rq_sg
- 1;
905 props
->max_fast_reg_page_list_len
=
906 1 << MLX5_CAP_GEN(mdev
, log_max_klm_list_size
);
907 get_atomic_caps_qp(dev
, props
);
908 props
->masked_atomic_cap
= IB_ATOMIC_NONE
;
909 props
->max_mcast_grp
= 1 << MLX5_CAP_GEN(mdev
, log_max_mcg
);
910 props
->max_mcast_qp_attach
= MLX5_CAP_GEN(mdev
, max_qp_mcg
);
911 props
->max_total_mcast_qp_attach
= props
->max_mcast_qp_attach
*
912 props
->max_mcast_grp
;
913 props
->max_map_per_fmr
= INT_MAX
; /* no limit in ConnectIB */
914 props
->max_ah
= INT_MAX
;
915 props
->hca_core_clock
= MLX5_CAP_GEN(mdev
, device_frequency_khz
);
916 props
->timestamp_mask
= 0x7FFFFFFFFFFFFFFFULL
;
918 #ifdef CONFIG_INFINIBAND_ON_DEMAND_PAGING
919 if (MLX5_CAP_GEN(mdev
, pg
))
920 props
->device_cap_flags
|= IB_DEVICE_ON_DEMAND_PAGING
;
921 props
->odp_caps
= dev
->odp_caps
;
924 if (MLX5_CAP_GEN(mdev
, cd
))
925 props
->device_cap_flags
|= IB_DEVICE_CROSS_CHANNEL
;
927 if (!mlx5_core_is_pf(mdev
))
928 props
->device_cap_flags
|= IB_DEVICE_VIRTUAL_FUNCTION
;
930 if (mlx5_ib_port_link_layer(ibdev
, 1) ==
931 IB_LINK_LAYER_ETHERNET
&& raw_support
) {
932 props
->rss_caps
.max_rwq_indirection_tables
=
933 1 << MLX5_CAP_GEN(dev
->mdev
, log_max_rqt
);
934 props
->rss_caps
.max_rwq_indirection_table_size
=
935 1 << MLX5_CAP_GEN(dev
->mdev
, log_max_rqt_size
);
936 props
->rss_caps
.supported_qpts
= 1 << IB_QPT_RAW_PACKET
;
937 props
->max_wq_type_rq
=
938 1 << MLX5_CAP_GEN(dev
->mdev
, log_max_rq
);
941 if (MLX5_CAP_GEN(mdev
, tag_matching
)) {
942 props
->tm_caps
.max_rndv_hdr_size
= MLX5_TM_MAX_RNDV_MSG_SIZE
;
943 props
->tm_caps
.max_num_tags
=
944 (1 << MLX5_CAP_GEN(mdev
, log_tag_matching_list_sz
)) - 1;
945 props
->tm_caps
.flags
= IB_TM_CAP_RC
;
946 props
->tm_caps
.max_ops
=
947 1 << MLX5_CAP_GEN(mdev
, log_max_qp_sz
);
948 props
->tm_caps
.max_sge
= MLX5_TM_MAX_SGE
;
951 if (MLX5_CAP_GEN(dev
->mdev
, cq_moderation
)) {
952 props
->cq_caps
.max_cq_moderation_count
=
954 props
->cq_caps
.max_cq_moderation_period
=
958 if (field_avail(typeof(resp
), cqe_comp_caps
, uhw
->outlen
)) {
959 resp
.response_length
+= sizeof(resp
.cqe_comp_caps
);
961 if (MLX5_CAP_GEN(dev
->mdev
, cqe_compression
)) {
962 resp
.cqe_comp_caps
.max_num
=
963 MLX5_CAP_GEN(dev
->mdev
,
964 cqe_compression_max_num
);
966 resp
.cqe_comp_caps
.supported_format
=
967 MLX5_IB_CQE_RES_FORMAT_HASH
|
968 MLX5_IB_CQE_RES_FORMAT_CSUM
;
970 if (MLX5_CAP_GEN(dev
->mdev
, mini_cqe_resp_stride_index
))
971 resp
.cqe_comp_caps
.supported_format
|=
972 MLX5_IB_CQE_RES_FORMAT_CSUM_STRIDX
;
976 if (field_avail(typeof(resp
), packet_pacing_caps
, uhw
->outlen
) &&
978 if (MLX5_CAP_QOS(mdev
, packet_pacing
) &&
979 MLX5_CAP_GEN(mdev
, qos
)) {
980 resp
.packet_pacing_caps
.qp_rate_limit_max
=
981 MLX5_CAP_QOS(mdev
, packet_pacing_max_rate
);
982 resp
.packet_pacing_caps
.qp_rate_limit_min
=
983 MLX5_CAP_QOS(mdev
, packet_pacing_min_rate
);
984 resp
.packet_pacing_caps
.supported_qpts
|=
985 1 << IB_QPT_RAW_PACKET
;
986 if (MLX5_CAP_QOS(mdev
, packet_pacing_burst_bound
) &&
987 MLX5_CAP_QOS(mdev
, packet_pacing_typical_size
))
988 resp
.packet_pacing_caps
.cap_flags
|=
989 MLX5_IB_PP_SUPPORT_BURST
;
991 resp
.response_length
+= sizeof(resp
.packet_pacing_caps
);
994 if (field_avail(typeof(resp
), mlx5_ib_support_multi_pkt_send_wqes
,
996 if (MLX5_CAP_ETH(mdev
, multi_pkt_send_wqe
))
997 resp
.mlx5_ib_support_multi_pkt_send_wqes
=
1000 if (MLX5_CAP_ETH(mdev
, enhanced_multi_pkt_send_wqe
))
1001 resp
.mlx5_ib_support_multi_pkt_send_wqes
|=
1002 MLX5_IB_SUPPORT_EMPW
;
1004 resp
.response_length
+=
1005 sizeof(resp
.mlx5_ib_support_multi_pkt_send_wqes
);
1008 if (field_avail(typeof(resp
), flags
, uhw
->outlen
)) {
1009 resp
.response_length
+= sizeof(resp
.flags
);
1011 if (MLX5_CAP_GEN(mdev
, cqe_compression_128
))
1013 MLX5_IB_QUERY_DEV_RESP_FLAGS_CQE_128B_COMP
;
1015 if (MLX5_CAP_GEN(mdev
, cqe_128_always
))
1016 resp
.flags
|= MLX5_IB_QUERY_DEV_RESP_FLAGS_CQE_128B_PAD
;
1019 if (field_avail(typeof(resp
), sw_parsing_caps
,
1021 resp
.response_length
+= sizeof(resp
.sw_parsing_caps
);
1022 if (MLX5_CAP_ETH(mdev
, swp
)) {
1023 resp
.sw_parsing_caps
.sw_parsing_offloads
|=
1026 if (MLX5_CAP_ETH(mdev
, swp_csum
))
1027 resp
.sw_parsing_caps
.sw_parsing_offloads
|=
1028 MLX5_IB_SW_PARSING_CSUM
;
1030 if (MLX5_CAP_ETH(mdev
, swp_lso
))
1031 resp
.sw_parsing_caps
.sw_parsing_offloads
|=
1032 MLX5_IB_SW_PARSING_LSO
;
1034 if (resp
.sw_parsing_caps
.sw_parsing_offloads
)
1035 resp
.sw_parsing_caps
.supported_qpts
=
1036 BIT(IB_QPT_RAW_PACKET
);
1040 if (field_avail(typeof(resp
), striding_rq_caps
, uhw
->outlen
) &&
1042 resp
.response_length
+= sizeof(resp
.striding_rq_caps
);
1043 if (MLX5_CAP_GEN(mdev
, striding_rq
)) {
1044 resp
.striding_rq_caps
.min_single_stride_log_num_of_bytes
=
1045 MLX5_MIN_SINGLE_STRIDE_LOG_NUM_BYTES
;
1046 resp
.striding_rq_caps
.max_single_stride_log_num_of_bytes
=
1047 MLX5_MAX_SINGLE_STRIDE_LOG_NUM_BYTES
;
1048 resp
.striding_rq_caps
.min_single_wqe_log_num_of_strides
=
1049 MLX5_MIN_SINGLE_WQE_LOG_NUM_STRIDES
;
1050 resp
.striding_rq_caps
.max_single_wqe_log_num_of_strides
=
1051 MLX5_MAX_SINGLE_WQE_LOG_NUM_STRIDES
;
1052 resp
.striding_rq_caps
.supported_qpts
=
1053 BIT(IB_QPT_RAW_PACKET
);
1057 if (field_avail(typeof(resp
), tunnel_offloads_caps
,
1059 resp
.response_length
+= sizeof(resp
.tunnel_offloads_caps
);
1060 if (MLX5_CAP_ETH(mdev
, tunnel_stateless_vxlan
))
1061 resp
.tunnel_offloads_caps
|=
1062 MLX5_IB_TUNNELED_OFFLOADS_VXLAN
;
1063 if (MLX5_CAP_ETH(mdev
, tunnel_stateless_geneve_rx
))
1064 resp
.tunnel_offloads_caps
|=
1065 MLX5_IB_TUNNELED_OFFLOADS_GENEVE
;
1066 if (MLX5_CAP_ETH(mdev
, tunnel_stateless_gre
))
1067 resp
.tunnel_offloads_caps
|=
1068 MLX5_IB_TUNNELED_OFFLOADS_GRE
;
1069 if (MLX5_CAP_GEN(mdev
, flex_parser_protocols
) &
1070 MLX5_FLEX_PROTO_CW_MPLS_GRE
)
1071 resp
.tunnel_offloads_caps
|=
1072 MLX5_IB_TUNNELED_OFFLOADS_MPLS_GRE
;
1073 if (MLX5_CAP_GEN(mdev
, flex_parser_protocols
) &
1074 MLX5_FLEX_PROTO_CW_MPLS_UDP
)
1075 resp
.tunnel_offloads_caps
|=
1076 MLX5_IB_TUNNELED_OFFLOADS_MPLS_UDP
;
1080 err
= ib_copy_to_udata(uhw
, &resp
, resp
.response_length
);
1089 enum mlx5_ib_width
{
1090 MLX5_IB_WIDTH_1X
= 1 << 0,
1091 MLX5_IB_WIDTH_2X
= 1 << 1,
1092 MLX5_IB_WIDTH_4X
= 1 << 2,
1093 MLX5_IB_WIDTH_8X
= 1 << 3,
1094 MLX5_IB_WIDTH_12X
= 1 << 4
1097 static int translate_active_width(struct ib_device
*ibdev
, u8 active_width
,
1100 struct mlx5_ib_dev
*dev
= to_mdev(ibdev
);
1103 if (active_width
& MLX5_IB_WIDTH_1X
) {
1104 *ib_width
= IB_WIDTH_1X
;
1105 } else if (active_width
& MLX5_IB_WIDTH_2X
) {
1106 mlx5_ib_dbg(dev
, "active_width %d is not supported by IB spec\n",
1109 } else if (active_width
& MLX5_IB_WIDTH_4X
) {
1110 *ib_width
= IB_WIDTH_4X
;
1111 } else if (active_width
& MLX5_IB_WIDTH_8X
) {
1112 *ib_width
= IB_WIDTH_8X
;
1113 } else if (active_width
& MLX5_IB_WIDTH_12X
) {
1114 *ib_width
= IB_WIDTH_12X
;
1116 mlx5_ib_dbg(dev
, "Invalid active_width %d\n",
1124 static int mlx5_mtu_to_ib_mtu(int mtu
)
1129 case 1024: return 3;
1130 case 2048: return 4;
1131 case 4096: return 5;
1133 pr_warn("invalid mtu\n");
1138 enum ib_max_vl_num
{
1140 __IB_MAX_VL_0_1
= 2,
1141 __IB_MAX_VL_0_3
= 3,
1142 __IB_MAX_VL_0_7
= 4,
1143 __IB_MAX_VL_0_14
= 5,
1146 enum mlx5_vl_hw_cap
{
1155 MLX5_VL_HW_0_14
= 15
1158 static int translate_max_vl_num(struct ib_device
*ibdev
, u8 vl_hw_cap
,
1161 switch (vl_hw_cap
) {
1163 *max_vl_num
= __IB_MAX_VL_0
;
1165 case MLX5_VL_HW_0_1
:
1166 *max_vl_num
= __IB_MAX_VL_0_1
;
1168 case MLX5_VL_HW_0_3
:
1169 *max_vl_num
= __IB_MAX_VL_0_3
;
1171 case MLX5_VL_HW_0_7
:
1172 *max_vl_num
= __IB_MAX_VL_0_7
;
1174 case MLX5_VL_HW_0_14
:
1175 *max_vl_num
= __IB_MAX_VL_0_14
;
1185 static int mlx5_query_hca_port(struct ib_device
*ibdev
, u8 port
,
1186 struct ib_port_attr
*props
)
1188 struct mlx5_ib_dev
*dev
= to_mdev(ibdev
);
1189 struct mlx5_core_dev
*mdev
= dev
->mdev
;
1190 struct mlx5_hca_vport_context
*rep
;
1194 u8 ib_link_width_oper
;
1197 rep
= kzalloc(sizeof(*rep
), GFP_KERNEL
);
1203 /* props being zeroed by the caller, avoid zeroing it here */
1205 err
= mlx5_query_hca_vport_context(mdev
, 0, port
, 0, rep
);
1209 props
->lid
= rep
->lid
;
1210 props
->lmc
= rep
->lmc
;
1211 props
->sm_lid
= rep
->sm_lid
;
1212 props
->sm_sl
= rep
->sm_sl
;
1213 props
->state
= rep
->vport_state
;
1214 props
->phys_state
= rep
->port_physical_state
;
1215 props
->port_cap_flags
= rep
->cap_mask1
;
1216 props
->gid_tbl_len
= mlx5_get_gid_table_len(MLX5_CAP_GEN(mdev
, gid_table_size
));
1217 props
->max_msg_sz
= 1 << MLX5_CAP_GEN(mdev
, log_max_msg
);
1218 props
->pkey_tbl_len
= mlx5_to_sw_pkey_sz(MLX5_CAP_GEN(mdev
, pkey_table_size
));
1219 props
->bad_pkey_cntr
= rep
->pkey_violation_counter
;
1220 props
->qkey_viol_cntr
= rep
->qkey_violation_counter
;
1221 props
->subnet_timeout
= rep
->subnet_timeout
;
1222 props
->init_type_reply
= rep
->init_type_reply
;
1224 err
= mlx5_query_port_link_width_oper(mdev
, &ib_link_width_oper
, port
);
1228 err
= translate_active_width(ibdev
, ib_link_width_oper
,
1229 &props
->active_width
);
1232 err
= mlx5_query_port_ib_proto_oper(mdev
, &props
->active_speed
, port
);
1236 mlx5_query_port_max_mtu(mdev
, &max_mtu
, port
);
1238 props
->max_mtu
= mlx5_mtu_to_ib_mtu(max_mtu
);
1240 mlx5_query_port_oper_mtu(mdev
, &oper_mtu
, port
);
1242 props
->active_mtu
= mlx5_mtu_to_ib_mtu(oper_mtu
);
1244 err
= mlx5_query_port_vl_hw_cap(mdev
, &vl_hw_cap
, port
);
1248 err
= translate_max_vl_num(ibdev
, vl_hw_cap
,
1249 &props
->max_vl_num
);
1255 int mlx5_ib_query_port(struct ib_device
*ibdev
, u8 port
,
1256 struct ib_port_attr
*props
)
1261 switch (mlx5_get_vport_access_method(ibdev
)) {
1262 case MLX5_VPORT_ACCESS_METHOD_MAD
:
1263 ret
= mlx5_query_mad_ifc_port(ibdev
, port
, props
);
1266 case MLX5_VPORT_ACCESS_METHOD_HCA
:
1267 ret
= mlx5_query_hca_port(ibdev
, port
, props
);
1270 case MLX5_VPORT_ACCESS_METHOD_NIC
:
1271 ret
= mlx5_query_port_roce(ibdev
, port
, props
);
1278 if (!ret
&& props
) {
1279 struct mlx5_ib_dev
*dev
= to_mdev(ibdev
);
1280 struct mlx5_core_dev
*mdev
;
1281 bool put_mdev
= true;
1283 mdev
= mlx5_ib_get_native_port_mdev(dev
, port
, NULL
);
1285 /* If the port isn't affiliated yet query the master.
1286 * The master and slave will have the same values.
1292 count
= mlx5_core_reserved_gids_count(mdev
);
1294 mlx5_ib_put_native_port_mdev(dev
, port
);
1295 props
->gid_tbl_len
-= count
;
1300 static int mlx5_ib_rep_query_port(struct ib_device
*ibdev
, u8 port
,
1301 struct ib_port_attr
*props
)
1305 /* Only link layer == ethernet is valid for representors */
1306 ret
= mlx5_query_port_roce(ibdev
, port
, props
);
1310 /* We don't support GIDS */
1311 props
->gid_tbl_len
= 0;
1316 static int mlx5_ib_query_gid(struct ib_device
*ibdev
, u8 port
, int index
,
1319 struct mlx5_ib_dev
*dev
= to_mdev(ibdev
);
1320 struct mlx5_core_dev
*mdev
= dev
->mdev
;
1322 switch (mlx5_get_vport_access_method(ibdev
)) {
1323 case MLX5_VPORT_ACCESS_METHOD_MAD
:
1324 return mlx5_query_mad_ifc_gids(ibdev
, port
, index
, gid
);
1326 case MLX5_VPORT_ACCESS_METHOD_HCA
:
1327 return mlx5_query_hca_vport_gid(mdev
, 0, port
, 0, index
, gid
);
1335 static int mlx5_query_hca_nic_pkey(struct ib_device
*ibdev
, u8 port
,
1336 u16 index
, u16
*pkey
)
1338 struct mlx5_ib_dev
*dev
= to_mdev(ibdev
);
1339 struct mlx5_core_dev
*mdev
;
1340 bool put_mdev
= true;
1344 mdev
= mlx5_ib_get_native_port_mdev(dev
, port
, &mdev_port_num
);
1346 /* The port isn't affiliated yet, get the PKey from the master
1347 * port. For RoCE the PKey tables will be the same.
1354 err
= mlx5_query_hca_vport_pkey(mdev
, 0, mdev_port_num
, 0,
1357 mlx5_ib_put_native_port_mdev(dev
, port
);
1362 static int mlx5_ib_query_pkey(struct ib_device
*ibdev
, u8 port
, u16 index
,
1365 switch (mlx5_get_vport_access_method(ibdev
)) {
1366 case MLX5_VPORT_ACCESS_METHOD_MAD
:
1367 return mlx5_query_mad_ifc_pkey(ibdev
, port
, index
, pkey
);
1369 case MLX5_VPORT_ACCESS_METHOD_HCA
:
1370 case MLX5_VPORT_ACCESS_METHOD_NIC
:
1371 return mlx5_query_hca_nic_pkey(ibdev
, port
, index
, pkey
);
1377 static int mlx5_ib_modify_device(struct ib_device
*ibdev
, int mask
,
1378 struct ib_device_modify
*props
)
1380 struct mlx5_ib_dev
*dev
= to_mdev(ibdev
);
1381 struct mlx5_reg_node_desc in
;
1382 struct mlx5_reg_node_desc out
;
1385 if (mask
& ~IB_DEVICE_MODIFY_NODE_DESC
)
1388 if (!(mask
& IB_DEVICE_MODIFY_NODE_DESC
))
1392 * If possible, pass node desc to FW, so it can generate
1393 * a 144 trap. If cmd fails, just ignore.
1395 memcpy(&in
, props
->node_desc
, IB_DEVICE_NODE_DESC_MAX
);
1396 err
= mlx5_core_access_reg(dev
->mdev
, &in
, sizeof(in
), &out
,
1397 sizeof(out
), MLX5_REG_NODE_DESC
, 0, 1);
1401 memcpy(ibdev
->node_desc
, props
->node_desc
, IB_DEVICE_NODE_DESC_MAX
);
1406 static int set_port_caps_atomic(struct mlx5_ib_dev
*dev
, u8 port_num
, u32 mask
,
1409 struct mlx5_hca_vport_context ctx
= {};
1410 struct mlx5_core_dev
*mdev
;
1414 mdev
= mlx5_ib_get_native_port_mdev(dev
, port_num
, &mdev_port_num
);
1418 err
= mlx5_query_hca_vport_context(mdev
, 0, mdev_port_num
, 0, &ctx
);
1422 if (~ctx
.cap_mask1_perm
& mask
) {
1423 mlx5_ib_warn(dev
, "trying to change bitmask 0x%X but change supported 0x%X\n",
1424 mask
, ctx
.cap_mask1_perm
);
1429 ctx
.cap_mask1
= value
;
1430 ctx
.cap_mask1_perm
= mask
;
1431 err
= mlx5_core_modify_hca_vport_context(mdev
, 0, mdev_port_num
,
1435 mlx5_ib_put_native_port_mdev(dev
, port_num
);
1440 static int mlx5_ib_modify_port(struct ib_device
*ibdev
, u8 port
, int mask
,
1441 struct ib_port_modify
*props
)
1443 struct mlx5_ib_dev
*dev
= to_mdev(ibdev
);
1444 struct ib_port_attr attr
;
1449 bool is_ib
= (mlx5_ib_port_link_layer(ibdev
, port
) ==
1450 IB_LINK_LAYER_INFINIBAND
);
1452 /* CM layer calls ib_modify_port() regardless of the link layer. For
1453 * Ethernet ports, qkey violation and Port capabilities are meaningless.
1458 if (MLX5_CAP_GEN(dev
->mdev
, ib_virt
) && is_ib
) {
1459 change_mask
= props
->clr_port_cap_mask
| props
->set_port_cap_mask
;
1460 value
= ~props
->clr_port_cap_mask
| props
->set_port_cap_mask
;
1461 return set_port_caps_atomic(dev
, port
, change_mask
, value
);
1464 mutex_lock(&dev
->cap_mask_mutex
);
1466 err
= ib_query_port(ibdev
, port
, &attr
);
1470 tmp
= (attr
.port_cap_flags
| props
->set_port_cap_mask
) &
1471 ~props
->clr_port_cap_mask
;
1473 err
= mlx5_set_port_caps(dev
->mdev
, port
, tmp
);
1476 mutex_unlock(&dev
->cap_mask_mutex
);
1480 static void print_lib_caps(struct mlx5_ib_dev
*dev
, u64 caps
)
1482 mlx5_ib_dbg(dev
, "MLX5_LIB_CAP_4K_UAR = %s\n",
1483 caps
& MLX5_LIB_CAP_4K_UAR
? "y" : "n");
1486 static u16
calc_dynamic_bfregs(int uars_per_sys_page
)
1488 /* Large page with non 4k uar support might limit the dynamic size */
1489 if (uars_per_sys_page
== 1 && PAGE_SIZE
> 4096)
1490 return MLX5_MIN_DYN_BFREGS
;
1492 return MLX5_MAX_DYN_BFREGS
;
1495 static int calc_total_bfregs(struct mlx5_ib_dev
*dev
, bool lib_uar_4k
,
1496 struct mlx5_ib_alloc_ucontext_req_v2
*req
,
1497 struct mlx5_bfreg_info
*bfregi
)
1499 int uars_per_sys_page
;
1500 int bfregs_per_sys_page
;
1501 int ref_bfregs
= req
->total_num_bfregs
;
1503 if (req
->total_num_bfregs
== 0)
1506 BUILD_BUG_ON(MLX5_MAX_BFREGS
% MLX5_NON_FP_BFREGS_IN_PAGE
);
1507 BUILD_BUG_ON(MLX5_MAX_BFREGS
< MLX5_NON_FP_BFREGS_IN_PAGE
);
1509 if (req
->total_num_bfregs
> MLX5_MAX_BFREGS
)
1512 uars_per_sys_page
= get_uars_per_sys_page(dev
, lib_uar_4k
);
1513 bfregs_per_sys_page
= uars_per_sys_page
* MLX5_NON_FP_BFREGS_PER_UAR
;
1514 /* This holds the required static allocation asked by the user */
1515 req
->total_num_bfregs
= ALIGN(req
->total_num_bfregs
, bfregs_per_sys_page
);
1516 if (req
->num_low_latency_bfregs
> req
->total_num_bfregs
- 1)
1519 bfregi
->num_static_sys_pages
= req
->total_num_bfregs
/ bfregs_per_sys_page
;
1520 bfregi
->num_dyn_bfregs
= ALIGN(calc_dynamic_bfregs(uars_per_sys_page
), bfregs_per_sys_page
);
1521 bfregi
->total_num_bfregs
= req
->total_num_bfregs
+ bfregi
->num_dyn_bfregs
;
1522 bfregi
->num_sys_pages
= bfregi
->total_num_bfregs
/ bfregs_per_sys_page
;
1524 mlx5_ib_dbg(dev
, "uar_4k: fw support %s, lib support %s, user requested %d bfregs, allocated %d, total bfregs %d, using %d sys pages\n",
1525 MLX5_CAP_GEN(dev
->mdev
, uar_4k
) ? "yes" : "no",
1526 lib_uar_4k
? "yes" : "no", ref_bfregs
,
1527 req
->total_num_bfregs
, bfregi
->total_num_bfregs
,
1528 bfregi
->num_sys_pages
);
1533 static int allocate_uars(struct mlx5_ib_dev
*dev
, struct mlx5_ib_ucontext
*context
)
1535 struct mlx5_bfreg_info
*bfregi
;
1539 bfregi
= &context
->bfregi
;
1540 for (i
= 0; i
< bfregi
->num_static_sys_pages
; i
++) {
1541 err
= mlx5_cmd_alloc_uar(dev
->mdev
, &bfregi
->sys_pages
[i
]);
1545 mlx5_ib_dbg(dev
, "allocated uar %d\n", bfregi
->sys_pages
[i
]);
1548 for (i
= bfregi
->num_static_sys_pages
; i
< bfregi
->num_sys_pages
; i
++)
1549 bfregi
->sys_pages
[i
] = MLX5_IB_INVALID_UAR_INDEX
;
1554 for (--i
; i
>= 0; i
--)
1555 if (mlx5_cmd_free_uar(dev
->mdev
, bfregi
->sys_pages
[i
]))
1556 mlx5_ib_warn(dev
, "failed to free uar %d\n", i
);
1561 static void deallocate_uars(struct mlx5_ib_dev
*dev
,
1562 struct mlx5_ib_ucontext
*context
)
1564 struct mlx5_bfreg_info
*bfregi
;
1567 bfregi
= &context
->bfregi
;
1568 for (i
= 0; i
< bfregi
->num_sys_pages
; i
++)
1569 if (i
< bfregi
->num_static_sys_pages
||
1570 bfregi
->sys_pages
[i
] != MLX5_IB_INVALID_UAR_INDEX
)
1571 mlx5_cmd_free_uar(dev
->mdev
, bfregi
->sys_pages
[i
]);
1574 static int mlx5_ib_alloc_transport_domain(struct mlx5_ib_dev
*dev
, u32
*tdn
)
1578 if (!MLX5_CAP_GEN(dev
->mdev
, log_max_transport_domain
))
1581 err
= mlx5_core_alloc_transport_domain(dev
->mdev
, tdn
);
1585 if ((MLX5_CAP_GEN(dev
->mdev
, port_type
) != MLX5_CAP_PORT_TYPE_ETH
) ||
1586 (!MLX5_CAP_GEN(dev
->mdev
, disable_local_lb_uc
) &&
1587 !MLX5_CAP_GEN(dev
->mdev
, disable_local_lb_mc
)))
1590 mutex_lock(&dev
->lb_mutex
);
1593 if (dev
->user_td
== 2)
1594 err
= mlx5_nic_vport_update_local_lb(dev
->mdev
, true);
1596 mutex_unlock(&dev
->lb_mutex
);
1600 static void mlx5_ib_dealloc_transport_domain(struct mlx5_ib_dev
*dev
, u32 tdn
)
1602 if (!MLX5_CAP_GEN(dev
->mdev
, log_max_transport_domain
))
1605 mlx5_core_dealloc_transport_domain(dev
->mdev
, tdn
);
1607 if ((MLX5_CAP_GEN(dev
->mdev
, port_type
) != MLX5_CAP_PORT_TYPE_ETH
) ||
1608 (!MLX5_CAP_GEN(dev
->mdev
, disable_local_lb_uc
) &&
1609 !MLX5_CAP_GEN(dev
->mdev
, disable_local_lb_mc
)))
1612 mutex_lock(&dev
->lb_mutex
);
1615 if (dev
->user_td
< 2)
1616 mlx5_nic_vport_update_local_lb(dev
->mdev
, false);
1618 mutex_unlock(&dev
->lb_mutex
);
1621 static struct ib_ucontext
*mlx5_ib_alloc_ucontext(struct ib_device
*ibdev
,
1622 struct ib_udata
*udata
)
1624 struct mlx5_ib_dev
*dev
= to_mdev(ibdev
);
1625 struct mlx5_ib_alloc_ucontext_req_v2 req
= {};
1626 struct mlx5_ib_alloc_ucontext_resp resp
= {};
1627 struct mlx5_core_dev
*mdev
= dev
->mdev
;
1628 struct mlx5_ib_ucontext
*context
;
1629 struct mlx5_bfreg_info
*bfregi
;
1632 size_t min_req_v2
= offsetof(struct mlx5_ib_alloc_ucontext_req_v2
,
1637 if (!dev
->ib_active
)
1638 return ERR_PTR(-EAGAIN
);
1640 if (udata
->inlen
== sizeof(struct mlx5_ib_alloc_ucontext_req
))
1642 else if (udata
->inlen
>= min_req_v2
)
1645 return ERR_PTR(-EINVAL
);
1647 err
= ib_copy_from_udata(&req
, udata
, min(udata
->inlen
, sizeof(req
)));
1649 return ERR_PTR(err
);
1651 if (req
.flags
& ~MLX5_IB_ALLOC_UCTX_DEVX
)
1652 return ERR_PTR(-EOPNOTSUPP
);
1654 if (req
.comp_mask
|| req
.reserved0
|| req
.reserved1
|| req
.reserved2
)
1655 return ERR_PTR(-EOPNOTSUPP
);
1657 req
.total_num_bfregs
= ALIGN(req
.total_num_bfregs
,
1658 MLX5_NON_FP_BFREGS_PER_UAR
);
1659 if (req
.num_low_latency_bfregs
> req
.total_num_bfregs
- 1)
1660 return ERR_PTR(-EINVAL
);
1662 resp
.qp_tab_size
= 1 << MLX5_CAP_GEN(dev
->mdev
, log_max_qp
);
1663 if (mlx5_core_is_pf(dev
->mdev
) && MLX5_CAP_GEN(dev
->mdev
, bf
))
1664 resp
.bf_reg_size
= 1 << MLX5_CAP_GEN(dev
->mdev
, log_bf_reg_size
);
1665 resp
.cache_line_size
= cache_line_size();
1666 resp
.max_sq_desc_sz
= MLX5_CAP_GEN(dev
->mdev
, max_wqe_sz_sq
);
1667 resp
.max_rq_desc_sz
= MLX5_CAP_GEN(dev
->mdev
, max_wqe_sz_rq
);
1668 resp
.max_send_wqebb
= 1 << MLX5_CAP_GEN(dev
->mdev
, log_max_qp_sz
);
1669 resp
.max_recv_wr
= 1 << MLX5_CAP_GEN(dev
->mdev
, log_max_qp_sz
);
1670 resp
.max_srq_recv_wr
= 1 << MLX5_CAP_GEN(dev
->mdev
, log_max_srq_sz
);
1671 resp
.cqe_version
= min_t(__u8
,
1672 (__u8
)MLX5_CAP_GEN(dev
->mdev
, cqe_version
),
1673 req
.max_cqe_version
);
1674 resp
.log_uar_size
= MLX5_CAP_GEN(dev
->mdev
, uar_4k
) ?
1675 MLX5_ADAPTER_PAGE_SHIFT
: PAGE_SHIFT
;
1676 resp
.num_uars_per_page
= MLX5_CAP_GEN(dev
->mdev
, uar_4k
) ?
1677 MLX5_CAP_GEN(dev
->mdev
, num_of_uars_per_page
) : 1;
1678 resp
.response_length
= min(offsetof(typeof(resp
), response_length
) +
1679 sizeof(resp
.response_length
), udata
->outlen
);
1681 if (mlx5_accel_ipsec_device_caps(dev
->mdev
) & MLX5_ACCEL_IPSEC_CAP_DEVICE
) {
1682 if (mlx5_get_flow_namespace(dev
->mdev
, MLX5_FLOW_NAMESPACE_EGRESS
))
1683 resp
.flow_action_flags
|= MLX5_USER_ALLOC_UCONTEXT_FLOW_ACTION_FLAGS_ESP_AES_GCM
;
1684 if (mlx5_accel_ipsec_device_caps(dev
->mdev
) & MLX5_ACCEL_IPSEC_CAP_REQUIRED_METADATA
)
1685 resp
.flow_action_flags
|= MLX5_USER_ALLOC_UCONTEXT_FLOW_ACTION_FLAGS_ESP_AES_GCM_REQ_METADATA
;
1686 if (MLX5_CAP_FLOWTABLE(dev
->mdev
, flow_table_properties_nic_receive
.ft_field_support
.outer_esp_spi
))
1687 resp
.flow_action_flags
|= MLX5_USER_ALLOC_UCONTEXT_FLOW_ACTION_FLAGS_ESP_AES_GCM_SPI_STEERING
;
1688 if (mlx5_accel_ipsec_device_caps(dev
->mdev
) & MLX5_ACCEL_IPSEC_CAP_TX_IV_IS_ESN
)
1689 resp
.flow_action_flags
|= MLX5_USER_ALLOC_UCONTEXT_FLOW_ACTION_FLAGS_ESP_AES_GCM_TX_IV_IS_ESN
;
1690 /* MLX5_USER_ALLOC_UCONTEXT_FLOW_ACTION_FLAGS_ESP_AES_GCM_FULL_OFFLOAD is currently always 0 */
1693 context
= kzalloc(sizeof(*context
), GFP_KERNEL
);
1695 return ERR_PTR(-ENOMEM
);
1697 lib_uar_4k
= req
.lib_caps
& MLX5_LIB_CAP_4K_UAR
;
1698 bfregi
= &context
->bfregi
;
1700 /* updates req->total_num_bfregs */
1701 err
= calc_total_bfregs(dev
, lib_uar_4k
, &req
, bfregi
);
1705 mutex_init(&bfregi
->lock
);
1706 bfregi
->lib_uar_4k
= lib_uar_4k
;
1707 bfregi
->count
= kcalloc(bfregi
->total_num_bfregs
, sizeof(*bfregi
->count
),
1709 if (!bfregi
->count
) {
1714 bfregi
->sys_pages
= kcalloc(bfregi
->num_sys_pages
,
1715 sizeof(*bfregi
->sys_pages
),
1717 if (!bfregi
->sys_pages
) {
1722 err
= allocate_uars(dev
, context
);
1726 #ifdef CONFIG_INFINIBAND_ON_DEMAND_PAGING
1727 context
->ibucontext
.invalidate_range
= &mlx5_ib_invalidate_range
;
1730 err
= mlx5_ib_alloc_transport_domain(dev
, &context
->tdn
);
1734 if (req
.flags
& MLX5_IB_ALLOC_UCTX_DEVX
) {
1735 /* Block DEVX on Infiniband as of SELinux */
1736 if (mlx5_ib_port_link_layer(ibdev
, 1) != IB_LINK_LAYER_ETHERNET
) {
1741 err
= mlx5_ib_devx_create(dev
, context
);
1746 if (MLX5_CAP_GEN(dev
->mdev
, dump_fill_mkey
)) {
1747 err
= mlx5_cmd_dump_fill_mkey(dev
->mdev
, &dump_fill_mkey
);
1752 INIT_LIST_HEAD(&context
->vma_private_list
);
1753 mutex_init(&context
->vma_private_list_mutex
);
1754 INIT_LIST_HEAD(&context
->db_page_list
);
1755 mutex_init(&context
->db_page_mutex
);
1757 resp
.tot_bfregs
= req
.total_num_bfregs
;
1758 resp
.num_ports
= dev
->num_ports
;
1760 if (field_avail(typeof(resp
), cqe_version
, udata
->outlen
))
1761 resp
.response_length
+= sizeof(resp
.cqe_version
);
1763 if (field_avail(typeof(resp
), cmds_supp_uhw
, udata
->outlen
)) {
1764 resp
.cmds_supp_uhw
|= MLX5_USER_CMDS_SUPP_UHW_QUERY_DEVICE
|
1765 MLX5_USER_CMDS_SUPP_UHW_CREATE_AH
;
1766 resp
.response_length
+= sizeof(resp
.cmds_supp_uhw
);
1769 if (field_avail(typeof(resp
), eth_min_inline
, udata
->outlen
)) {
1770 if (mlx5_ib_port_link_layer(ibdev
, 1) == IB_LINK_LAYER_ETHERNET
) {
1771 mlx5_query_min_inline(dev
->mdev
, &resp
.eth_min_inline
);
1772 resp
.eth_min_inline
++;
1774 resp
.response_length
+= sizeof(resp
.eth_min_inline
);
1777 if (field_avail(typeof(resp
), clock_info_versions
, udata
->outlen
)) {
1778 if (mdev
->clock_info
)
1779 resp
.clock_info_versions
= BIT(MLX5_IB_CLOCK_INFO_V1
);
1780 resp
.response_length
+= sizeof(resp
.clock_info_versions
);
1784 * We don't want to expose information from the PCI bar that is located
1785 * after 4096 bytes, so if the arch only supports larger pages, let's
1786 * pretend we don't support reading the HCA's core clock. This is also
1787 * forced by mmap function.
1789 if (field_avail(typeof(resp
), hca_core_clock_offset
, udata
->outlen
)) {
1790 if (PAGE_SIZE
<= 4096) {
1792 MLX5_IB_ALLOC_UCONTEXT_RESP_MASK_CORE_CLOCK_OFFSET
;
1793 resp
.hca_core_clock_offset
=
1794 offsetof(struct mlx5_init_seg
, internal_timer_h
) % PAGE_SIZE
;
1796 resp
.response_length
+= sizeof(resp
.hca_core_clock_offset
);
1799 if (field_avail(typeof(resp
), log_uar_size
, udata
->outlen
))
1800 resp
.response_length
+= sizeof(resp
.log_uar_size
);
1802 if (field_avail(typeof(resp
), num_uars_per_page
, udata
->outlen
))
1803 resp
.response_length
+= sizeof(resp
.num_uars_per_page
);
1805 if (field_avail(typeof(resp
), num_dyn_bfregs
, udata
->outlen
)) {
1806 resp
.num_dyn_bfregs
= bfregi
->num_dyn_bfregs
;
1807 resp
.response_length
+= sizeof(resp
.num_dyn_bfregs
);
1810 if (field_avail(typeof(resp
), dump_fill_mkey
, udata
->outlen
)) {
1811 if (MLX5_CAP_GEN(dev
->mdev
, dump_fill_mkey
)) {
1812 resp
.dump_fill_mkey
= dump_fill_mkey
;
1814 MLX5_IB_ALLOC_UCONTEXT_RESP_MASK_DUMP_FILL_MKEY
;
1816 resp
.response_length
+= sizeof(resp
.dump_fill_mkey
);
1819 err
= ib_copy_to_udata(udata
, &resp
, resp
.response_length
);
1824 bfregi
->num_low_latency_bfregs
= req
.num_low_latency_bfregs
;
1825 context
->cqe_version
= resp
.cqe_version
;
1826 context
->lib_caps
= req
.lib_caps
;
1827 print_lib_caps(dev
, context
->lib_caps
);
1829 if (mlx5_lag_is_active(dev
->mdev
)) {
1830 u8 port
= mlx5_core_native_port_num(dev
->mdev
);
1832 atomic_set(&context
->tx_port_affinity
,
1834 1, &dev
->roce
[port
].tx_port_affinity
));
1837 return &context
->ibucontext
;
1840 if (req
.flags
& MLX5_IB_ALLOC_UCTX_DEVX
)
1841 mlx5_ib_devx_destroy(dev
, context
);
1843 mlx5_ib_dealloc_transport_domain(dev
, context
->tdn
);
1846 deallocate_uars(dev
, context
);
1849 kfree(bfregi
->sys_pages
);
1852 kfree(bfregi
->count
);
1857 return ERR_PTR(err
);
1860 static int mlx5_ib_dealloc_ucontext(struct ib_ucontext
*ibcontext
)
1862 struct mlx5_ib_ucontext
*context
= to_mucontext(ibcontext
);
1863 struct mlx5_ib_dev
*dev
= to_mdev(ibcontext
->device
);
1864 struct mlx5_bfreg_info
*bfregi
;
1866 if (context
->devx_uid
)
1867 mlx5_ib_devx_destroy(dev
, context
);
1869 bfregi
= &context
->bfregi
;
1870 mlx5_ib_dealloc_transport_domain(dev
, context
->tdn
);
1872 deallocate_uars(dev
, context
);
1873 kfree(bfregi
->sys_pages
);
1874 kfree(bfregi
->count
);
1880 static phys_addr_t
uar_index2pfn(struct mlx5_ib_dev
*dev
,
1883 int fw_uars_per_page
;
1885 fw_uars_per_page
= MLX5_CAP_GEN(dev
->mdev
, uar_4k
) ? MLX5_UARS_IN_PAGE
: 1;
1887 return (pci_resource_start(dev
->mdev
->pdev
, 0) >> PAGE_SHIFT
) + uar_idx
/ fw_uars_per_page
;
1890 static int get_command(unsigned long offset
)
1892 return (offset
>> MLX5_IB_MMAP_CMD_SHIFT
) & MLX5_IB_MMAP_CMD_MASK
;
1895 static int get_arg(unsigned long offset
)
1897 return offset
& ((1 << MLX5_IB_MMAP_CMD_SHIFT
) - 1);
1900 static int get_index(unsigned long offset
)
1902 return get_arg(offset
);
1905 /* Index resides in an extra byte to enable larger values than 255 */
1906 static int get_extended_index(unsigned long offset
)
1908 return get_arg(offset
) | ((offset
>> 16) & 0xff) << 8;
1911 static void mlx5_ib_vma_open(struct vm_area_struct
*area
)
1913 /* vma_open is called when a new VMA is created on top of our VMA. This
1914 * is done through either mremap flow or split_vma (usually due to
1915 * mlock, madvise, munmap, etc.) We do not support a clone of the VMA,
1916 * as this VMA is strongly hardware related. Therefore we set the
1917 * vm_ops of the newly created/cloned VMA to NULL, to prevent it from
1918 * calling us again and trying to do incorrect actions. We assume that
1919 * the original VMA size is exactly a single page, and therefore all
1920 * "splitting" operation will not happen to it.
1922 area
->vm_ops
= NULL
;
1925 static void mlx5_ib_vma_close(struct vm_area_struct
*area
)
1927 struct mlx5_ib_vma_private_data
*mlx5_ib_vma_priv_data
;
1929 /* It's guaranteed that all VMAs opened on a FD are closed before the
1930 * file itself is closed, therefore no sync is needed with the regular
1931 * closing flow. (e.g. mlx5 ib_dealloc_ucontext)
1932 * However need a sync with accessing the vma as part of
1933 * mlx5_ib_disassociate_ucontext.
1934 * The close operation is usually called under mm->mmap_sem except when
1935 * process is exiting.
1936 * The exiting case is handled explicitly as part of
1937 * mlx5_ib_disassociate_ucontext.
1939 mlx5_ib_vma_priv_data
= (struct mlx5_ib_vma_private_data
*)area
->vm_private_data
;
1941 /* setting the vma context pointer to null in the mlx5_ib driver's
1942 * private data, to protect a race condition in
1943 * mlx5_ib_disassociate_ucontext().
1945 mlx5_ib_vma_priv_data
->vma
= NULL
;
1946 mutex_lock(mlx5_ib_vma_priv_data
->vma_private_list_mutex
);
1947 list_del(&mlx5_ib_vma_priv_data
->list
);
1948 mutex_unlock(mlx5_ib_vma_priv_data
->vma_private_list_mutex
);
1949 kfree(mlx5_ib_vma_priv_data
);
1952 static const struct vm_operations_struct mlx5_ib_vm_ops
= {
1953 .open
= mlx5_ib_vma_open
,
1954 .close
= mlx5_ib_vma_close
1957 static int mlx5_ib_set_vma_data(struct vm_area_struct
*vma
,
1958 struct mlx5_ib_ucontext
*ctx
)
1960 struct mlx5_ib_vma_private_data
*vma_prv
;
1961 struct list_head
*vma_head
= &ctx
->vma_private_list
;
1963 vma_prv
= kzalloc(sizeof(*vma_prv
), GFP_KERNEL
);
1968 vma_prv
->vma_private_list_mutex
= &ctx
->vma_private_list_mutex
;
1969 vma
->vm_private_data
= vma_prv
;
1970 vma
->vm_ops
= &mlx5_ib_vm_ops
;
1972 mutex_lock(&ctx
->vma_private_list_mutex
);
1973 list_add(&vma_prv
->list
, vma_head
);
1974 mutex_unlock(&ctx
->vma_private_list_mutex
);
1979 static void mlx5_ib_disassociate_ucontext(struct ib_ucontext
*ibcontext
)
1981 struct vm_area_struct
*vma
;
1982 struct mlx5_ib_vma_private_data
*vma_private
, *n
;
1983 struct mlx5_ib_ucontext
*context
= to_mucontext(ibcontext
);
1985 mutex_lock(&context
->vma_private_list_mutex
);
1986 list_for_each_entry_safe(vma_private
, n
, &context
->vma_private_list
,
1988 vma
= vma_private
->vma
;
1989 zap_vma_ptes(vma
, vma
->vm_start
, PAGE_SIZE
);
1990 /* context going to be destroyed, should
1991 * not access ops any more.
1993 vma
->vm_flags
&= ~(VM_SHARED
| VM_MAYSHARE
);
1995 list_del(&vma_private
->list
);
1998 mutex_unlock(&context
->vma_private_list_mutex
);
2001 static inline char *mmap_cmd2str(enum mlx5_ib_mmap_cmd cmd
)
2004 case MLX5_IB_MMAP_WC_PAGE
:
2006 case MLX5_IB_MMAP_REGULAR_PAGE
:
2007 return "best effort WC";
2008 case MLX5_IB_MMAP_NC_PAGE
:
2010 case MLX5_IB_MMAP_DEVICE_MEM
:
2011 return "Device Memory";
2017 static int mlx5_ib_mmap_clock_info_page(struct mlx5_ib_dev
*dev
,
2018 struct vm_area_struct
*vma
,
2019 struct mlx5_ib_ucontext
*context
)
2024 if (vma
->vm_end
- vma
->vm_start
!= PAGE_SIZE
)
2027 if (get_index(vma
->vm_pgoff
) != MLX5_IB_CLOCK_INFO_V1
)
2030 if (vma
->vm_flags
& VM_WRITE
)
2033 if (!dev
->mdev
->clock_info_page
)
2036 pfn
= page_to_pfn(dev
->mdev
->clock_info_page
);
2037 err
= remap_pfn_range(vma
, vma
->vm_start
, pfn
, PAGE_SIZE
,
2042 return mlx5_ib_set_vma_data(vma
, context
);
2045 static int uar_mmap(struct mlx5_ib_dev
*dev
, enum mlx5_ib_mmap_cmd cmd
,
2046 struct vm_area_struct
*vma
,
2047 struct mlx5_ib_ucontext
*context
)
2049 struct mlx5_bfreg_info
*bfregi
= &context
->bfregi
;
2054 u32 bfreg_dyn_idx
= 0;
2056 int dyn_uar
= (cmd
== MLX5_IB_MMAP_ALLOC_WC
);
2057 int max_valid_idx
= dyn_uar
? bfregi
->num_sys_pages
:
2058 bfregi
->num_static_sys_pages
;
2060 if (vma
->vm_end
- vma
->vm_start
!= PAGE_SIZE
)
2064 idx
= get_extended_index(vma
->vm_pgoff
) + bfregi
->num_static_sys_pages
;
2066 idx
= get_index(vma
->vm_pgoff
);
2068 if (idx
>= max_valid_idx
) {
2069 mlx5_ib_warn(dev
, "invalid uar index %lu, max=%d\n",
2070 idx
, max_valid_idx
);
2075 case MLX5_IB_MMAP_WC_PAGE
:
2076 case MLX5_IB_MMAP_ALLOC_WC
:
2077 /* Some architectures don't support WC memory */
2078 #if defined(CONFIG_X86)
2081 #elif !(defined(CONFIG_PPC) || (defined(CONFIG_ARM) && defined(CONFIG_MMU)))
2085 case MLX5_IB_MMAP_REGULAR_PAGE
:
2086 /* For MLX5_IB_MMAP_REGULAR_PAGE do the best effort to get WC */
2087 prot
= pgprot_writecombine(vma
->vm_page_prot
);
2089 case MLX5_IB_MMAP_NC_PAGE
:
2090 prot
= pgprot_noncached(vma
->vm_page_prot
);
2099 uars_per_page
= get_uars_per_sys_page(dev
, bfregi
->lib_uar_4k
);
2100 bfreg_dyn_idx
= idx
* (uars_per_page
* MLX5_NON_FP_BFREGS_PER_UAR
);
2101 if (bfreg_dyn_idx
>= bfregi
->total_num_bfregs
) {
2102 mlx5_ib_warn(dev
, "invalid bfreg_dyn_idx %u, max=%u\n",
2103 bfreg_dyn_idx
, bfregi
->total_num_bfregs
);
2107 mutex_lock(&bfregi
->lock
);
2108 /* Fail if uar already allocated, first bfreg index of each
2109 * page holds its count.
2111 if (bfregi
->count
[bfreg_dyn_idx
]) {
2112 mlx5_ib_warn(dev
, "wrong offset, idx %lu is busy, bfregn=%u\n", idx
, bfreg_dyn_idx
);
2113 mutex_unlock(&bfregi
->lock
);
2117 bfregi
->count
[bfreg_dyn_idx
]++;
2118 mutex_unlock(&bfregi
->lock
);
2120 err
= mlx5_cmd_alloc_uar(dev
->mdev
, &uar_index
);
2122 mlx5_ib_warn(dev
, "UAR alloc failed\n");
2126 uar_index
= bfregi
->sys_pages
[idx
];
2129 pfn
= uar_index2pfn(dev
, uar_index
);
2130 mlx5_ib_dbg(dev
, "uar idx 0x%lx, pfn %pa\n", idx
, &pfn
);
2132 vma
->vm_page_prot
= prot
;
2133 err
= io_remap_pfn_range(vma
, vma
->vm_start
, pfn
,
2134 PAGE_SIZE
, vma
->vm_page_prot
);
2137 "io_remap_pfn_range failed with error=%d, mmap_cmd=%s\n",
2138 err
, mmap_cmd2str(cmd
));
2143 err
= mlx5_ib_set_vma_data(vma
, context
);
2148 bfregi
->sys_pages
[idx
] = uar_index
;
2155 mlx5_cmd_free_uar(dev
->mdev
, idx
);
2158 mlx5_ib_free_bfreg(dev
, bfregi
, bfreg_dyn_idx
);
2163 static int dm_mmap(struct ib_ucontext
*context
, struct vm_area_struct
*vma
)
2165 struct mlx5_ib_ucontext
*mctx
= to_mucontext(context
);
2166 struct mlx5_ib_dev
*dev
= to_mdev(context
->device
);
2167 u16 page_idx
= get_extended_index(vma
->vm_pgoff
);
2168 size_t map_size
= vma
->vm_end
- vma
->vm_start
;
2169 u32 npages
= map_size
>> PAGE_SHIFT
;
2173 if (find_next_zero_bit(mctx
->dm_pages
, page_idx
+ npages
, page_idx
) !=
2177 pfn
= ((pci_resource_start(dev
->mdev
->pdev
, 0) +
2178 MLX5_CAP64_DEV_MEM(dev
->mdev
, memic_bar_start_addr
)) >>
2181 prot
= pgprot_writecombine(vma
->vm_page_prot
);
2182 vma
->vm_page_prot
= prot
;
2184 if (io_remap_pfn_range(vma
, vma
->vm_start
, pfn
, map_size
,
2188 return mlx5_ib_set_vma_data(vma
, mctx
);
2191 static int mlx5_ib_mmap(struct ib_ucontext
*ibcontext
, struct vm_area_struct
*vma
)
2193 struct mlx5_ib_ucontext
*context
= to_mucontext(ibcontext
);
2194 struct mlx5_ib_dev
*dev
= to_mdev(ibcontext
->device
);
2195 unsigned long command
;
2198 command
= get_command(vma
->vm_pgoff
);
2200 case MLX5_IB_MMAP_WC_PAGE
:
2201 case MLX5_IB_MMAP_NC_PAGE
:
2202 case MLX5_IB_MMAP_REGULAR_PAGE
:
2203 case MLX5_IB_MMAP_ALLOC_WC
:
2204 return uar_mmap(dev
, command
, vma
, context
);
2206 case MLX5_IB_MMAP_GET_CONTIGUOUS_PAGES
:
2209 case MLX5_IB_MMAP_CORE_CLOCK
:
2210 if (vma
->vm_end
- vma
->vm_start
!= PAGE_SIZE
)
2213 if (vma
->vm_flags
& VM_WRITE
)
2216 /* Don't expose to user-space information it shouldn't have */
2217 if (PAGE_SIZE
> 4096)
2220 vma
->vm_page_prot
= pgprot_noncached(vma
->vm_page_prot
);
2221 pfn
= (dev
->mdev
->iseg_base
+
2222 offsetof(struct mlx5_init_seg
, internal_timer_h
)) >>
2224 if (io_remap_pfn_range(vma
, vma
->vm_start
, pfn
,
2225 PAGE_SIZE
, vma
->vm_page_prot
))
2228 case MLX5_IB_MMAP_CLOCK_INFO
:
2229 return mlx5_ib_mmap_clock_info_page(dev
, vma
, context
);
2231 case MLX5_IB_MMAP_DEVICE_MEM
:
2232 return dm_mmap(ibcontext
, vma
);
2241 struct ib_dm
*mlx5_ib_alloc_dm(struct ib_device
*ibdev
,
2242 struct ib_ucontext
*context
,
2243 struct ib_dm_alloc_attr
*attr
,
2244 struct uverbs_attr_bundle
*attrs
)
2246 u64 act_size
= roundup(attr
->length
, MLX5_MEMIC_BASE_SIZE
);
2247 struct mlx5_memic
*memic
= &to_mdev(ibdev
)->memic
;
2248 phys_addr_t memic_addr
;
2249 struct mlx5_ib_dm
*dm
;
2254 dm
= kzalloc(sizeof(*dm
), GFP_KERNEL
);
2256 return ERR_PTR(-ENOMEM
);
2258 mlx5_ib_dbg(to_mdev(ibdev
), "alloc_memic req: user_length=0x%llx act_length=0x%llx log_alignment=%d\n",
2259 attr
->length
, act_size
, attr
->alignment
);
2261 err
= mlx5_cmd_alloc_memic(memic
, &memic_addr
,
2262 act_size
, attr
->alignment
);
2266 start_offset
= memic_addr
& ~PAGE_MASK
;
2267 page_idx
= (memic_addr
- pci_resource_start(memic
->dev
->pdev
, 0) -
2268 MLX5_CAP64_DEV_MEM(memic
->dev
, memic_bar_start_addr
)) >>
2271 err
= uverbs_copy_to(attrs
,
2272 MLX5_IB_ATTR_ALLOC_DM_RESP_START_OFFSET
,
2273 &start_offset
, sizeof(start_offset
));
2277 err
= uverbs_copy_to(attrs
,
2278 MLX5_IB_ATTR_ALLOC_DM_RESP_PAGE_INDEX
,
2279 &page_idx
, sizeof(page_idx
));
2283 bitmap_set(to_mucontext(context
)->dm_pages
, page_idx
,
2284 DIV_ROUND_UP(act_size
, PAGE_SIZE
));
2286 dm
->dev_addr
= memic_addr
;
2291 mlx5_cmd_dealloc_memic(memic
, memic_addr
,
2295 return ERR_PTR(err
);
2298 int mlx5_ib_dealloc_dm(struct ib_dm
*ibdm
)
2300 struct mlx5_memic
*memic
= &to_mdev(ibdm
->device
)->memic
;
2301 struct mlx5_ib_dm
*dm
= to_mdm(ibdm
);
2302 u64 act_size
= roundup(dm
->ibdm
.length
, MLX5_MEMIC_BASE_SIZE
);
2306 ret
= mlx5_cmd_dealloc_memic(memic
, dm
->dev_addr
, act_size
);
2310 page_idx
= (dm
->dev_addr
- pci_resource_start(memic
->dev
->pdev
, 0) -
2311 MLX5_CAP64_DEV_MEM(memic
->dev
, memic_bar_start_addr
)) >>
2313 bitmap_clear(to_mucontext(ibdm
->uobject
->context
)->dm_pages
,
2315 DIV_ROUND_UP(act_size
, PAGE_SIZE
));
2322 static struct ib_pd
*mlx5_ib_alloc_pd(struct ib_device
*ibdev
,
2323 struct ib_ucontext
*context
,
2324 struct ib_udata
*udata
)
2326 struct mlx5_ib_alloc_pd_resp resp
;
2327 struct mlx5_ib_pd
*pd
;
2330 pd
= kmalloc(sizeof(*pd
), GFP_KERNEL
);
2332 return ERR_PTR(-ENOMEM
);
2334 err
= mlx5_core_alloc_pd(to_mdev(ibdev
)->mdev
, &pd
->pdn
);
2337 return ERR_PTR(err
);
2342 if (ib_copy_to_udata(udata
, &resp
, sizeof(resp
))) {
2343 mlx5_core_dealloc_pd(to_mdev(ibdev
)->mdev
, pd
->pdn
);
2345 return ERR_PTR(-EFAULT
);
2352 static int mlx5_ib_dealloc_pd(struct ib_pd
*pd
)
2354 struct mlx5_ib_dev
*mdev
= to_mdev(pd
->device
);
2355 struct mlx5_ib_pd
*mpd
= to_mpd(pd
);
2357 mlx5_core_dealloc_pd(mdev
->mdev
, mpd
->pdn
);
2364 MATCH_CRITERIA_ENABLE_OUTER_BIT
,
2365 MATCH_CRITERIA_ENABLE_MISC_BIT
,
2366 MATCH_CRITERIA_ENABLE_INNER_BIT
,
2367 MATCH_CRITERIA_ENABLE_MISC2_BIT
2370 #define HEADER_IS_ZERO(match_criteria, headers) \
2371 !(memchr_inv(MLX5_ADDR_OF(fte_match_param, match_criteria, headers), \
2372 0, MLX5_FLD_SZ_BYTES(fte_match_param, headers))) \
2374 static u8 get_match_criteria_enable(u32 *match_criteria)
2376 u8 match_criteria_enable
;
2378 match_criteria_enable
=
2379 (!HEADER_IS_ZERO(match_criteria
, outer_headers
)) <<
2380 MATCH_CRITERIA_ENABLE_OUTER_BIT
;
2381 match_criteria_enable
|=
2382 (!HEADER_IS_ZERO(match_criteria
, misc_parameters
)) <<
2383 MATCH_CRITERIA_ENABLE_MISC_BIT
;
2384 match_criteria_enable
|=
2385 (!HEADER_IS_ZERO(match_criteria
, inner_headers
)) <<
2386 MATCH_CRITERIA_ENABLE_INNER_BIT
;
2387 match_criteria_enable
|=
2388 (!HEADER_IS_ZERO(match_criteria
, misc_parameters_2
)) <<
2389 MATCH_CRITERIA_ENABLE_MISC2_BIT
;
2391 return match_criteria_enable
;
2394 static void set_proto(void *outer_c
, void *outer_v
, u8 mask
, u8 val
)
2396 MLX5_SET(fte_match_set_lyr_2_4
, outer_c
, ip_protocol
, mask
);
2397 MLX5_SET(fte_match_set_lyr_2_4
, outer_v
, ip_protocol
, val
);
2400 static void set_flow_label(void *misc_c
, void *misc_v
, u32 mask
, u32 val
,
2404 MLX5_SET(fte_match_set_misc
,
2405 misc_c
, inner_ipv6_flow_label
, mask
);
2406 MLX5_SET(fte_match_set_misc
,
2407 misc_v
, inner_ipv6_flow_label
, val
);
2409 MLX5_SET(fte_match_set_misc
,
2410 misc_c
, outer_ipv6_flow_label
, mask
);
2411 MLX5_SET(fte_match_set_misc
,
2412 misc_v
, outer_ipv6_flow_label
, val
);
2416 static void set_tos(void *outer_c
, void *outer_v
, u8 mask
, u8 val
)
2418 MLX5_SET(fte_match_set_lyr_2_4
, outer_c
, ip_ecn
, mask
);
2419 MLX5_SET(fte_match_set_lyr_2_4
, outer_v
, ip_ecn
, val
);
2420 MLX5_SET(fte_match_set_lyr_2_4
, outer_c
, ip_dscp
, mask
>> 2);
2421 MLX5_SET(fte_match_set_lyr_2_4
, outer_v
, ip_dscp
, val
>> 2);
2424 static int check_mpls_supp_fields(u32 field_support
, const __be32
*set_mask
)
2426 if (MLX5_GET(fte_match_mpls
, set_mask
, mpls_label
) &&
2427 !(field_support
& MLX5_FIELD_SUPPORT_MPLS_LABEL
))
2430 if (MLX5_GET(fte_match_mpls
, set_mask
, mpls_exp
) &&
2431 !(field_support
& MLX5_FIELD_SUPPORT_MPLS_EXP
))
2434 if (MLX5_GET(fte_match_mpls
, set_mask
, mpls_s_bos
) &&
2435 !(field_support
& MLX5_FIELD_SUPPORT_MPLS_S_BOS
))
2438 if (MLX5_GET(fte_match_mpls
, set_mask
, mpls_ttl
) &&
2439 !(field_support
& MLX5_FIELD_SUPPORT_MPLS_TTL
))
2445 #define LAST_ETH_FIELD vlan_tag
2446 #define LAST_IB_FIELD sl
2447 #define LAST_IPV4_FIELD tos
2448 #define LAST_IPV6_FIELD traffic_class
2449 #define LAST_TCP_UDP_FIELD src_port
2450 #define LAST_TUNNEL_FIELD tunnel_id
2451 #define LAST_FLOW_TAG_FIELD tag_id
2452 #define LAST_DROP_FIELD size
2453 #define LAST_COUNTERS_FIELD counters
2455 /* Field is the last supported field */
2456 #define FIELDS_NOT_SUPPORTED(filter, field)\
2457 memchr_inv((void *)&filter.field +\
2458 sizeof(filter.field), 0,\
2460 offsetof(typeof(filter), field) -\
2461 sizeof(filter.field))
2463 static int parse_flow_flow_action(const union ib_flow_spec
*ib_spec
,
2464 const struct ib_flow_attr
*flow_attr
,
2465 struct mlx5_flow_act
*action
)
2467 struct mlx5_ib_flow_action
*maction
= to_mflow_act(ib_spec
->action
.act
);
2469 switch (maction
->ib_action
.type
) {
2470 case IB_FLOW_ACTION_ESP
:
2471 /* Currently only AES_GCM keymat is supported by the driver */
2472 action
->esp_id
= (uintptr_t)maction
->esp_aes_gcm
.ctx
;
2473 action
->action
|= flow_attr
->flags
& IB_FLOW_ATTR_FLAGS_EGRESS
?
2474 MLX5_FLOW_CONTEXT_ACTION_ENCRYPT
:
2475 MLX5_FLOW_CONTEXT_ACTION_DECRYPT
;
2477 case IB_FLOW_ACTION_UNSPECIFIED
:
2478 if (maction
->flow_action_raw
.sub_type
==
2479 MLX5_IB_FLOW_ACTION_MODIFY_HEADER
) {
2480 action
->action
|= MLX5_FLOW_CONTEXT_ACTION_MOD_HDR
;
2481 action
->modify_id
= maction
->flow_action_raw
.action_id
;
2490 static int parse_flow_attr(struct mlx5_core_dev
*mdev
, u32
*match_c
,
2491 u32
*match_v
, const union ib_flow_spec
*ib_spec
,
2492 const struct ib_flow_attr
*flow_attr
,
2493 struct mlx5_flow_act
*action
, u32 prev_type
)
2495 void *misc_params_c
= MLX5_ADDR_OF(fte_match_param
, match_c
,
2497 void *misc_params_v
= MLX5_ADDR_OF(fte_match_param
, match_v
,
2499 void *misc_params2_c
= MLX5_ADDR_OF(fte_match_param
, match_c
,
2501 void *misc_params2_v
= MLX5_ADDR_OF(fte_match_param
, match_v
,
2508 if (ib_spec
->type
& IB_FLOW_SPEC_INNER
) {
2509 headers_c
= MLX5_ADDR_OF(fte_match_param
, match_c
,
2511 headers_v
= MLX5_ADDR_OF(fte_match_param
, match_v
,
2513 match_ipv
= MLX5_CAP_FLOWTABLE_NIC_RX(mdev
,
2514 ft_field_support
.inner_ip_version
);
2516 headers_c
= MLX5_ADDR_OF(fte_match_param
, match_c
,
2518 headers_v
= MLX5_ADDR_OF(fte_match_param
, match_v
,
2520 match_ipv
= MLX5_CAP_FLOWTABLE_NIC_RX(mdev
,
2521 ft_field_support
.outer_ip_version
);
2524 switch (ib_spec
->type
& ~IB_FLOW_SPEC_INNER
) {
2525 case IB_FLOW_SPEC_ETH
:
2526 if (FIELDS_NOT_SUPPORTED(ib_spec
->eth
.mask
, LAST_ETH_FIELD
))
2529 ether_addr_copy(MLX5_ADDR_OF(fte_match_set_lyr_2_4
, headers_c
,
2531 ib_spec
->eth
.mask
.dst_mac
);
2532 ether_addr_copy(MLX5_ADDR_OF(fte_match_set_lyr_2_4
, headers_v
,
2534 ib_spec
->eth
.val
.dst_mac
);
2536 ether_addr_copy(MLX5_ADDR_OF(fte_match_set_lyr_2_4
, headers_c
,
2538 ib_spec
->eth
.mask
.src_mac
);
2539 ether_addr_copy(MLX5_ADDR_OF(fte_match_set_lyr_2_4
, headers_v
,
2541 ib_spec
->eth
.val
.src_mac
);
2543 if (ib_spec
->eth
.mask
.vlan_tag
) {
2544 MLX5_SET(fte_match_set_lyr_2_4
, headers_c
,
2546 MLX5_SET(fte_match_set_lyr_2_4
, headers_v
,
2549 MLX5_SET(fte_match_set_lyr_2_4
, headers_c
,
2550 first_vid
, ntohs(ib_spec
->eth
.mask
.vlan_tag
));
2551 MLX5_SET(fte_match_set_lyr_2_4
, headers_v
,
2552 first_vid
, ntohs(ib_spec
->eth
.val
.vlan_tag
));
2554 MLX5_SET(fte_match_set_lyr_2_4
, headers_c
,
2556 ntohs(ib_spec
->eth
.mask
.vlan_tag
) >> 12);
2557 MLX5_SET(fte_match_set_lyr_2_4
, headers_v
,
2559 ntohs(ib_spec
->eth
.val
.vlan_tag
) >> 12);
2561 MLX5_SET(fte_match_set_lyr_2_4
, headers_c
,
2563 ntohs(ib_spec
->eth
.mask
.vlan_tag
) >> 13);
2564 MLX5_SET(fte_match_set_lyr_2_4
, headers_v
,
2566 ntohs(ib_spec
->eth
.val
.vlan_tag
) >> 13);
2568 MLX5_SET(fte_match_set_lyr_2_4
, headers_c
,
2569 ethertype
, ntohs(ib_spec
->eth
.mask
.ether_type
));
2570 MLX5_SET(fte_match_set_lyr_2_4
, headers_v
,
2571 ethertype
, ntohs(ib_spec
->eth
.val
.ether_type
));
2573 case IB_FLOW_SPEC_IPV4
:
2574 if (FIELDS_NOT_SUPPORTED(ib_spec
->ipv4
.mask
, LAST_IPV4_FIELD
))
2578 MLX5_SET(fte_match_set_lyr_2_4
, headers_c
,
2580 MLX5_SET(fte_match_set_lyr_2_4
, headers_v
,
2581 ip_version
, MLX5_FS_IPV4_VERSION
);
2583 MLX5_SET(fte_match_set_lyr_2_4
, headers_c
,
2585 MLX5_SET(fte_match_set_lyr_2_4
, headers_v
,
2586 ethertype
, ETH_P_IP
);
2589 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4
, headers_c
,
2590 src_ipv4_src_ipv6
.ipv4_layout
.ipv4
),
2591 &ib_spec
->ipv4
.mask
.src_ip
,
2592 sizeof(ib_spec
->ipv4
.mask
.src_ip
));
2593 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4
, headers_v
,
2594 src_ipv4_src_ipv6
.ipv4_layout
.ipv4
),
2595 &ib_spec
->ipv4
.val
.src_ip
,
2596 sizeof(ib_spec
->ipv4
.val
.src_ip
));
2597 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4
, headers_c
,
2598 dst_ipv4_dst_ipv6
.ipv4_layout
.ipv4
),
2599 &ib_spec
->ipv4
.mask
.dst_ip
,
2600 sizeof(ib_spec
->ipv4
.mask
.dst_ip
));
2601 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4
, headers_v
,
2602 dst_ipv4_dst_ipv6
.ipv4_layout
.ipv4
),
2603 &ib_spec
->ipv4
.val
.dst_ip
,
2604 sizeof(ib_spec
->ipv4
.val
.dst_ip
));
2606 set_tos(headers_c
, headers_v
,
2607 ib_spec
->ipv4
.mask
.tos
, ib_spec
->ipv4
.val
.tos
);
2609 set_proto(headers_c
, headers_v
,
2610 ib_spec
->ipv4
.mask
.proto
, ib_spec
->ipv4
.val
.proto
);
2612 case IB_FLOW_SPEC_IPV6
:
2613 if (FIELDS_NOT_SUPPORTED(ib_spec
->ipv6
.mask
, LAST_IPV6_FIELD
))
2617 MLX5_SET(fte_match_set_lyr_2_4
, headers_c
,
2619 MLX5_SET(fte_match_set_lyr_2_4
, headers_v
,
2620 ip_version
, MLX5_FS_IPV6_VERSION
);
2622 MLX5_SET(fte_match_set_lyr_2_4
, headers_c
,
2624 MLX5_SET(fte_match_set_lyr_2_4
, headers_v
,
2625 ethertype
, ETH_P_IPV6
);
2628 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4
, headers_c
,
2629 src_ipv4_src_ipv6
.ipv6_layout
.ipv6
),
2630 &ib_spec
->ipv6
.mask
.src_ip
,
2631 sizeof(ib_spec
->ipv6
.mask
.src_ip
));
2632 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4
, headers_v
,
2633 src_ipv4_src_ipv6
.ipv6_layout
.ipv6
),
2634 &ib_spec
->ipv6
.val
.src_ip
,
2635 sizeof(ib_spec
->ipv6
.val
.src_ip
));
2636 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4
, headers_c
,
2637 dst_ipv4_dst_ipv6
.ipv6_layout
.ipv6
),
2638 &ib_spec
->ipv6
.mask
.dst_ip
,
2639 sizeof(ib_spec
->ipv6
.mask
.dst_ip
));
2640 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4
, headers_v
,
2641 dst_ipv4_dst_ipv6
.ipv6_layout
.ipv6
),
2642 &ib_spec
->ipv6
.val
.dst_ip
,
2643 sizeof(ib_spec
->ipv6
.val
.dst_ip
));
2645 set_tos(headers_c
, headers_v
,
2646 ib_spec
->ipv6
.mask
.traffic_class
,
2647 ib_spec
->ipv6
.val
.traffic_class
);
2649 set_proto(headers_c
, headers_v
,
2650 ib_spec
->ipv6
.mask
.next_hdr
,
2651 ib_spec
->ipv6
.val
.next_hdr
);
2653 set_flow_label(misc_params_c
, misc_params_v
,
2654 ntohl(ib_spec
->ipv6
.mask
.flow_label
),
2655 ntohl(ib_spec
->ipv6
.val
.flow_label
),
2656 ib_spec
->type
& IB_FLOW_SPEC_INNER
);
2658 case IB_FLOW_SPEC_ESP
:
2659 if (ib_spec
->esp
.mask
.seq
)
2662 MLX5_SET(fte_match_set_misc
, misc_params_c
, outer_esp_spi
,
2663 ntohl(ib_spec
->esp
.mask
.spi
));
2664 MLX5_SET(fte_match_set_misc
, misc_params_v
, outer_esp_spi
,
2665 ntohl(ib_spec
->esp
.val
.spi
));
2667 case IB_FLOW_SPEC_TCP
:
2668 if (FIELDS_NOT_SUPPORTED(ib_spec
->tcp_udp
.mask
,
2669 LAST_TCP_UDP_FIELD
))
2672 MLX5_SET(fte_match_set_lyr_2_4
, headers_c
, ip_protocol
,
2674 MLX5_SET(fte_match_set_lyr_2_4
, headers_v
, ip_protocol
,
2677 MLX5_SET(fte_match_set_lyr_2_4
, headers_c
, tcp_sport
,
2678 ntohs(ib_spec
->tcp_udp
.mask
.src_port
));
2679 MLX5_SET(fte_match_set_lyr_2_4
, headers_v
, tcp_sport
,
2680 ntohs(ib_spec
->tcp_udp
.val
.src_port
));
2682 MLX5_SET(fte_match_set_lyr_2_4
, headers_c
, tcp_dport
,
2683 ntohs(ib_spec
->tcp_udp
.mask
.dst_port
));
2684 MLX5_SET(fte_match_set_lyr_2_4
, headers_v
, tcp_dport
,
2685 ntohs(ib_spec
->tcp_udp
.val
.dst_port
));
2687 case IB_FLOW_SPEC_UDP
:
2688 if (FIELDS_NOT_SUPPORTED(ib_spec
->tcp_udp
.mask
,
2689 LAST_TCP_UDP_FIELD
))
2692 MLX5_SET(fte_match_set_lyr_2_4
, headers_c
, ip_protocol
,
2694 MLX5_SET(fte_match_set_lyr_2_4
, headers_v
, ip_protocol
,
2697 MLX5_SET(fte_match_set_lyr_2_4
, headers_c
, udp_sport
,
2698 ntohs(ib_spec
->tcp_udp
.mask
.src_port
));
2699 MLX5_SET(fte_match_set_lyr_2_4
, headers_v
, udp_sport
,
2700 ntohs(ib_spec
->tcp_udp
.val
.src_port
));
2702 MLX5_SET(fte_match_set_lyr_2_4
, headers_c
, udp_dport
,
2703 ntohs(ib_spec
->tcp_udp
.mask
.dst_port
));
2704 MLX5_SET(fte_match_set_lyr_2_4
, headers_v
, udp_dport
,
2705 ntohs(ib_spec
->tcp_udp
.val
.dst_port
));
2707 case IB_FLOW_SPEC_GRE
:
2708 if (ib_spec
->gre
.mask
.c_ks_res0_ver
)
2711 MLX5_SET(fte_match_set_lyr_2_4
, headers_c
, ip_protocol
,
2713 MLX5_SET(fte_match_set_lyr_2_4
, headers_v
, ip_protocol
,
2716 MLX5_SET(fte_match_set_misc
, misc_params_c
, gre_protocol
,
2717 ntohs(ib_spec
->gre
.mask
.protocol
));
2718 MLX5_SET(fte_match_set_misc
, misc_params_v
, gre_protocol
,
2719 ntohs(ib_spec
->gre
.val
.protocol
));
2721 memcpy(MLX5_ADDR_OF(fte_match_set_misc
, misc_params_c
,
2723 &ib_spec
->gre
.mask
.key
,
2724 sizeof(ib_spec
->gre
.mask
.key
));
2725 memcpy(MLX5_ADDR_OF(fte_match_set_misc
, misc_params_v
,
2727 &ib_spec
->gre
.val
.key
,
2728 sizeof(ib_spec
->gre
.val
.key
));
2730 case IB_FLOW_SPEC_MPLS
:
2731 switch (prev_type
) {
2732 case IB_FLOW_SPEC_UDP
:
2733 if (check_mpls_supp_fields(MLX5_CAP_FLOWTABLE_NIC_RX(mdev
,
2734 ft_field_support
.outer_first_mpls_over_udp
),
2735 &ib_spec
->mpls
.mask
.tag
))
2738 memcpy(MLX5_ADDR_OF(fte_match_set_misc2
, misc_params2_v
,
2739 outer_first_mpls_over_udp
),
2740 &ib_spec
->mpls
.val
.tag
,
2741 sizeof(ib_spec
->mpls
.val
.tag
));
2742 memcpy(MLX5_ADDR_OF(fte_match_set_misc2
, misc_params2_c
,
2743 outer_first_mpls_over_udp
),
2744 &ib_spec
->mpls
.mask
.tag
,
2745 sizeof(ib_spec
->mpls
.mask
.tag
));
2747 case IB_FLOW_SPEC_GRE
:
2748 if (check_mpls_supp_fields(MLX5_CAP_FLOWTABLE_NIC_RX(mdev
,
2749 ft_field_support
.outer_first_mpls_over_gre
),
2750 &ib_spec
->mpls
.mask
.tag
))
2753 memcpy(MLX5_ADDR_OF(fte_match_set_misc2
, misc_params2_v
,
2754 outer_first_mpls_over_gre
),
2755 &ib_spec
->mpls
.val
.tag
,
2756 sizeof(ib_spec
->mpls
.val
.tag
));
2757 memcpy(MLX5_ADDR_OF(fte_match_set_misc2
, misc_params2_c
,
2758 outer_first_mpls_over_gre
),
2759 &ib_spec
->mpls
.mask
.tag
,
2760 sizeof(ib_spec
->mpls
.mask
.tag
));
2763 if (ib_spec
->type
& IB_FLOW_SPEC_INNER
) {
2764 if (check_mpls_supp_fields(MLX5_CAP_FLOWTABLE_NIC_RX(mdev
,
2765 ft_field_support
.inner_first_mpls
),
2766 &ib_spec
->mpls
.mask
.tag
))
2769 memcpy(MLX5_ADDR_OF(fte_match_set_misc2
, misc_params2_v
,
2771 &ib_spec
->mpls
.val
.tag
,
2772 sizeof(ib_spec
->mpls
.val
.tag
));
2773 memcpy(MLX5_ADDR_OF(fte_match_set_misc2
, misc_params2_c
,
2775 &ib_spec
->mpls
.mask
.tag
,
2776 sizeof(ib_spec
->mpls
.mask
.tag
));
2778 if (check_mpls_supp_fields(MLX5_CAP_FLOWTABLE_NIC_RX(mdev
,
2779 ft_field_support
.outer_first_mpls
),
2780 &ib_spec
->mpls
.mask
.tag
))
2783 memcpy(MLX5_ADDR_OF(fte_match_set_misc2
, misc_params2_v
,
2785 &ib_spec
->mpls
.val
.tag
,
2786 sizeof(ib_spec
->mpls
.val
.tag
));
2787 memcpy(MLX5_ADDR_OF(fte_match_set_misc2
, misc_params2_c
,
2789 &ib_spec
->mpls
.mask
.tag
,
2790 sizeof(ib_spec
->mpls
.mask
.tag
));
2794 case IB_FLOW_SPEC_VXLAN_TUNNEL
:
2795 if (FIELDS_NOT_SUPPORTED(ib_spec
->tunnel
.mask
,
2799 MLX5_SET(fte_match_set_misc
, misc_params_c
, vxlan_vni
,
2800 ntohl(ib_spec
->tunnel
.mask
.tunnel_id
));
2801 MLX5_SET(fte_match_set_misc
, misc_params_v
, vxlan_vni
,
2802 ntohl(ib_spec
->tunnel
.val
.tunnel_id
));
2804 case IB_FLOW_SPEC_ACTION_TAG
:
2805 if (FIELDS_NOT_SUPPORTED(ib_spec
->flow_tag
,
2806 LAST_FLOW_TAG_FIELD
))
2808 if (ib_spec
->flow_tag
.tag_id
>= BIT(24))
2811 action
->flow_tag
= ib_spec
->flow_tag
.tag_id
;
2812 action
->has_flow_tag
= true;
2814 case IB_FLOW_SPEC_ACTION_DROP
:
2815 if (FIELDS_NOT_SUPPORTED(ib_spec
->drop
,
2818 action
->action
|= MLX5_FLOW_CONTEXT_ACTION_DROP
;
2820 case IB_FLOW_SPEC_ACTION_HANDLE
:
2821 ret
= parse_flow_flow_action(ib_spec
, flow_attr
, action
);
2825 case IB_FLOW_SPEC_ACTION_COUNT
:
2826 if (FIELDS_NOT_SUPPORTED(ib_spec
->flow_count
,
2827 LAST_COUNTERS_FIELD
))
2830 /* for now support only one counters spec per flow */
2831 if (action
->action
& MLX5_FLOW_CONTEXT_ACTION_COUNT
)
2834 action
->counters
= ib_spec
->flow_count
.counters
;
2835 action
->action
|= MLX5_FLOW_CONTEXT_ACTION_COUNT
;
2844 /* If a flow could catch both multicast and unicast packets,
2845 * it won't fall into the multicast flow steering table and this rule
2846 * could steal other multicast packets.
2848 static bool flow_is_multicast_only(const struct ib_flow_attr
*ib_attr
)
2850 union ib_flow_spec
*flow_spec
;
2852 if (ib_attr
->type
!= IB_FLOW_ATTR_NORMAL
||
2853 ib_attr
->num_of_specs
< 1)
2856 flow_spec
= (union ib_flow_spec
*)(ib_attr
+ 1);
2857 if (flow_spec
->type
== IB_FLOW_SPEC_IPV4
) {
2858 struct ib_flow_spec_ipv4
*ipv4_spec
;
2860 ipv4_spec
= (struct ib_flow_spec_ipv4
*)flow_spec
;
2861 if (ipv4_is_multicast(ipv4_spec
->val
.dst_ip
))
2867 if (flow_spec
->type
== IB_FLOW_SPEC_ETH
) {
2868 struct ib_flow_spec_eth
*eth_spec
;
2870 eth_spec
= (struct ib_flow_spec_eth
*)flow_spec
;
2871 return is_multicast_ether_addr(eth_spec
->mask
.dst_mac
) &&
2872 is_multicast_ether_addr(eth_spec
->val
.dst_mac
);
2884 static enum valid_spec
2885 is_valid_esp_aes_gcm(struct mlx5_core_dev
*mdev
,
2886 const struct mlx5_flow_spec
*spec
,
2887 const struct mlx5_flow_act
*flow_act
,
2890 const u32
*match_c
= spec
->match_criteria
;
2892 (flow_act
->action
& (MLX5_FLOW_CONTEXT_ACTION_ENCRYPT
|
2893 MLX5_FLOW_CONTEXT_ACTION_DECRYPT
));
2894 bool is_ipsec
= mlx5_fs_is_ipsec_flow(match_c
);
2895 bool is_drop
= flow_act
->action
& MLX5_FLOW_CONTEXT_ACTION_DROP
;
2898 * Currently only crypto is supported in egress, when regular egress
2899 * rules would be supported, always return VALID_SPEC_NA.
2902 return VALID_SPEC_NA
;
2904 return is_crypto
&& is_ipsec
&&
2905 (!egress
|| (!is_drop
&& !flow_act
->has_flow_tag
)) ?
2906 VALID_SPEC_VALID
: VALID_SPEC_INVALID
;
2909 static bool is_valid_spec(struct mlx5_core_dev
*mdev
,
2910 const struct mlx5_flow_spec
*spec
,
2911 const struct mlx5_flow_act
*flow_act
,
2914 /* We curretly only support ipsec egress flow */
2915 return is_valid_esp_aes_gcm(mdev
, spec
, flow_act
, egress
) != VALID_SPEC_INVALID
;
2918 static bool is_valid_ethertype(struct mlx5_core_dev
*mdev
,
2919 const struct ib_flow_attr
*flow_attr
,
2922 union ib_flow_spec
*ib_spec
= (union ib_flow_spec
*)(flow_attr
+ 1);
2923 int match_ipv
= check_inner
?
2924 MLX5_CAP_FLOWTABLE_NIC_RX(mdev
,
2925 ft_field_support
.inner_ip_version
) :
2926 MLX5_CAP_FLOWTABLE_NIC_RX(mdev
,
2927 ft_field_support
.outer_ip_version
);
2928 int inner_bit
= check_inner
? IB_FLOW_SPEC_INNER
: 0;
2929 bool ipv4_spec_valid
, ipv6_spec_valid
;
2930 unsigned int ip_spec_type
= 0;
2931 bool has_ethertype
= false;
2932 unsigned int spec_index
;
2933 bool mask_valid
= true;
2937 /* Validate that ethertype is correct */
2938 for (spec_index
= 0; spec_index
< flow_attr
->num_of_specs
; spec_index
++) {
2939 if ((ib_spec
->type
== (IB_FLOW_SPEC_ETH
| inner_bit
)) &&
2940 ib_spec
->eth
.mask
.ether_type
) {
2941 mask_valid
= (ib_spec
->eth
.mask
.ether_type
==
2943 has_ethertype
= true;
2944 eth_type
= ntohs(ib_spec
->eth
.val
.ether_type
);
2945 } else if ((ib_spec
->type
== (IB_FLOW_SPEC_IPV4
| inner_bit
)) ||
2946 (ib_spec
->type
== (IB_FLOW_SPEC_IPV6
| inner_bit
))) {
2947 ip_spec_type
= ib_spec
->type
;
2949 ib_spec
= (void *)ib_spec
+ ib_spec
->size
;
2952 type_valid
= (!has_ethertype
) || (!ip_spec_type
);
2953 if (!type_valid
&& mask_valid
) {
2954 ipv4_spec_valid
= (eth_type
== ETH_P_IP
) &&
2955 (ip_spec_type
== (IB_FLOW_SPEC_IPV4
| inner_bit
));
2956 ipv6_spec_valid
= (eth_type
== ETH_P_IPV6
) &&
2957 (ip_spec_type
== (IB_FLOW_SPEC_IPV6
| inner_bit
));
2959 type_valid
= (ipv4_spec_valid
) || (ipv6_spec_valid
) ||
2960 (((eth_type
== ETH_P_MPLS_UC
) ||
2961 (eth_type
== ETH_P_MPLS_MC
)) && match_ipv
);
2967 static bool is_valid_attr(struct mlx5_core_dev
*mdev
,
2968 const struct ib_flow_attr
*flow_attr
)
2970 return is_valid_ethertype(mdev
, flow_attr
, false) &&
2971 is_valid_ethertype(mdev
, flow_attr
, true);
2974 static void put_flow_table(struct mlx5_ib_dev
*dev
,
2975 struct mlx5_ib_flow_prio
*prio
, bool ft_added
)
2977 prio
->refcount
-= !!ft_added
;
2978 if (!prio
->refcount
) {
2979 mlx5_destroy_flow_table(prio
->flow_table
);
2980 prio
->flow_table
= NULL
;
2984 static void counters_clear_description(struct ib_counters
*counters
)
2986 struct mlx5_ib_mcounters
*mcounters
= to_mcounters(counters
);
2988 mutex_lock(&mcounters
->mcntrs_mutex
);
2989 kfree(mcounters
->counters_data
);
2990 mcounters
->counters_data
= NULL
;
2991 mcounters
->cntrs_max_index
= 0;
2992 mutex_unlock(&mcounters
->mcntrs_mutex
);
2995 static int mlx5_ib_destroy_flow(struct ib_flow
*flow_id
)
2997 struct mlx5_ib_flow_handler
*handler
= container_of(flow_id
,
2998 struct mlx5_ib_flow_handler
,
3000 struct mlx5_ib_flow_handler
*iter
, *tmp
;
3001 struct mlx5_ib_dev
*dev
= handler
->dev
;
3003 mutex_lock(&dev
->flow_db
->lock
);
3005 list_for_each_entry_safe(iter
, tmp
, &handler
->list
, list
) {
3006 mlx5_del_flow_rules(iter
->rule
);
3007 put_flow_table(dev
, iter
->prio
, true);
3008 list_del(&iter
->list
);
3012 mlx5_del_flow_rules(handler
->rule
);
3013 put_flow_table(dev
, handler
->prio
, true);
3014 if (handler
->ibcounters
&&
3015 atomic_read(&handler
->ibcounters
->usecnt
) == 1)
3016 counters_clear_description(handler
->ibcounters
);
3018 mutex_unlock(&dev
->flow_db
->lock
);
3019 if (handler
->flow_matcher
)
3020 atomic_dec(&handler
->flow_matcher
->usecnt
);
3026 static int ib_prio_to_core_prio(unsigned int priority
, bool dont_trap
)
3034 enum flow_table_type
{
3039 #define MLX5_FS_MAX_TYPES 6
3040 #define MLX5_FS_MAX_ENTRIES BIT(16)
3042 static struct mlx5_ib_flow_prio
*_get_prio(struct mlx5_flow_namespace
*ns
,
3043 struct mlx5_ib_flow_prio
*prio
,
3045 int num_entries
, int num_groups
,
3048 struct mlx5_flow_table
*ft
;
3050 ft
= mlx5_create_auto_grouped_flow_table(ns
, priority
,
3055 return ERR_CAST(ft
);
3057 prio
->flow_table
= ft
;
3062 static struct mlx5_ib_flow_prio
*get_flow_table(struct mlx5_ib_dev
*dev
,
3063 struct ib_flow_attr
*flow_attr
,
3064 enum flow_table_type ft_type
)
3066 bool dont_trap
= flow_attr
->flags
& IB_FLOW_ATTR_FLAGS_DONT_TRAP
;
3067 struct mlx5_flow_namespace
*ns
= NULL
;
3068 struct mlx5_ib_flow_prio
*prio
;
3069 struct mlx5_flow_table
*ft
;
3076 max_table_size
= BIT(MLX5_CAP_FLOWTABLE_NIC_RX(dev
->mdev
,
3078 if (flow_attr
->type
== IB_FLOW_ATTR_NORMAL
) {
3079 enum mlx5_flow_namespace_type fn_type
;
3081 if (flow_is_multicast_only(flow_attr
) &&
3083 priority
= MLX5_IB_FLOW_MCAST_PRIO
;
3085 priority
= ib_prio_to_core_prio(flow_attr
->priority
,
3087 if (ft_type
== MLX5_IB_FT_RX
) {
3088 fn_type
= MLX5_FLOW_NAMESPACE_BYPASS
;
3089 prio
= &dev
->flow_db
->prios
[priority
];
3091 MLX5_CAP_FLOWTABLE_NIC_RX(dev
->mdev
, decap
))
3092 flags
|= MLX5_FLOW_TABLE_TUNNEL_EN_DECAP
;
3095 BIT(MLX5_CAP_FLOWTABLE_NIC_TX(dev
->mdev
,
3097 fn_type
= MLX5_FLOW_NAMESPACE_EGRESS
;
3098 prio
= &dev
->flow_db
->egress_prios
[priority
];
3100 MLX5_CAP_FLOWTABLE_NIC_TX(dev
->mdev
, reformat
))
3101 flags
|= MLX5_FLOW_TABLE_TUNNEL_EN_REFORMAT
;
3103 ns
= mlx5_get_flow_namespace(dev
->mdev
, fn_type
);
3104 num_entries
= MLX5_FS_MAX_ENTRIES
;
3105 num_groups
= MLX5_FS_MAX_TYPES
;
3106 } else if (flow_attr
->type
== IB_FLOW_ATTR_ALL_DEFAULT
||
3107 flow_attr
->type
== IB_FLOW_ATTR_MC_DEFAULT
) {
3108 ns
= mlx5_get_flow_namespace(dev
->mdev
,
3109 MLX5_FLOW_NAMESPACE_LEFTOVERS
);
3110 build_leftovers_ft_param(&priority
,
3113 prio
= &dev
->flow_db
->prios
[MLX5_IB_FLOW_LEFTOVERS_PRIO
];
3114 } else if (flow_attr
->type
== IB_FLOW_ATTR_SNIFFER
) {
3115 if (!MLX5_CAP_FLOWTABLE(dev
->mdev
,
3116 allow_sniffer_and_nic_rx_shared_tir
))
3117 return ERR_PTR(-ENOTSUPP
);
3119 ns
= mlx5_get_flow_namespace(dev
->mdev
, ft_type
== MLX5_IB_FT_RX
?
3120 MLX5_FLOW_NAMESPACE_SNIFFER_RX
:
3121 MLX5_FLOW_NAMESPACE_SNIFFER_TX
);
3123 prio
= &dev
->flow_db
->sniffer
[ft_type
];
3130 return ERR_PTR(-ENOTSUPP
);
3132 if (num_entries
> max_table_size
)
3133 return ERR_PTR(-ENOMEM
);
3135 ft
= prio
->flow_table
;
3137 return _get_prio(ns
, prio
, priority
, num_entries
, num_groups
,
3143 static void set_underlay_qp(struct mlx5_ib_dev
*dev
,
3144 struct mlx5_flow_spec
*spec
,
3147 void *misc_params_c
= MLX5_ADDR_OF(fte_match_param
,
3148 spec
->match_criteria
,
3150 void *misc_params_v
= MLX5_ADDR_OF(fte_match_param
, spec
->match_value
,
3154 MLX5_CAP_FLOWTABLE_NIC_RX(dev
->mdev
,
3155 ft_field_support
.bth_dst_qp
)) {
3156 MLX5_SET(fte_match_set_misc
,
3157 misc_params_v
, bth_dst_qp
, underlay_qpn
);
3158 MLX5_SET(fte_match_set_misc
,
3159 misc_params_c
, bth_dst_qp
, 0xffffff);
3163 static int read_flow_counters(struct ib_device
*ibdev
,
3164 struct mlx5_read_counters_attr
*read_attr
)
3166 struct mlx5_fc
*fc
= read_attr
->hw_cntrs_hndl
;
3167 struct mlx5_ib_dev
*dev
= to_mdev(ibdev
);
3169 return mlx5_fc_query(dev
->mdev
, fc
,
3170 &read_attr
->out
[IB_COUNTER_PACKETS
],
3171 &read_attr
->out
[IB_COUNTER_BYTES
]);
3174 /* flow counters currently expose two counters packets and bytes */
3175 #define FLOW_COUNTERS_NUM 2
3176 static int counters_set_description(struct ib_counters
*counters
,
3177 enum mlx5_ib_counters_type counters_type
,
3178 struct mlx5_ib_flow_counters_desc
*desc_data
,
3181 struct mlx5_ib_mcounters
*mcounters
= to_mcounters(counters
);
3182 u32 cntrs_max_index
= 0;
3185 if (counters_type
!= MLX5_IB_COUNTERS_FLOW
)
3188 /* init the fields for the object */
3189 mcounters
->type
= counters_type
;
3190 mcounters
->read_counters
= read_flow_counters
;
3191 mcounters
->counters_num
= FLOW_COUNTERS_NUM
;
3192 mcounters
->ncounters
= ncounters
;
3193 /* each counter entry have both description and index pair */
3194 for (i
= 0; i
< ncounters
; i
++) {
3195 if (desc_data
[i
].description
> IB_COUNTER_BYTES
)
3198 if (cntrs_max_index
<= desc_data
[i
].index
)
3199 cntrs_max_index
= desc_data
[i
].index
+ 1;
3202 mutex_lock(&mcounters
->mcntrs_mutex
);
3203 mcounters
->counters_data
= desc_data
;
3204 mcounters
->cntrs_max_index
= cntrs_max_index
;
3205 mutex_unlock(&mcounters
->mcntrs_mutex
);
3210 #define MAX_COUNTERS_NUM (USHRT_MAX / (sizeof(u32) * 2))
3211 static int flow_counters_set_data(struct ib_counters
*ibcounters
,
3212 struct mlx5_ib_create_flow
*ucmd
)
3214 struct mlx5_ib_mcounters
*mcounters
= to_mcounters(ibcounters
);
3215 struct mlx5_ib_flow_counters_data
*cntrs_data
= NULL
;
3216 struct mlx5_ib_flow_counters_desc
*desc_data
= NULL
;
3217 bool hw_hndl
= false;
3220 if (ucmd
&& ucmd
->ncounters_data
!= 0) {
3221 cntrs_data
= ucmd
->data
;
3222 if (cntrs_data
->ncounters
> MAX_COUNTERS_NUM
)
3225 desc_data
= kcalloc(cntrs_data
->ncounters
,
3231 if (copy_from_user(desc_data
,
3232 u64_to_user_ptr(cntrs_data
->counters_data
),
3233 sizeof(*desc_data
) * cntrs_data
->ncounters
)) {
3239 if (!mcounters
->hw_cntrs_hndl
) {
3240 mcounters
->hw_cntrs_hndl
= mlx5_fc_create(
3241 to_mdev(ibcounters
->device
)->mdev
, false);
3242 if (IS_ERR(mcounters
->hw_cntrs_hndl
)) {
3243 ret
= PTR_ERR(mcounters
->hw_cntrs_hndl
);
3250 /* counters already bound to at least one flow */
3251 if (mcounters
->cntrs_max_index
) {
3256 ret
= counters_set_description(ibcounters
,
3257 MLX5_IB_COUNTERS_FLOW
,
3259 cntrs_data
->ncounters
);
3263 } else if (!mcounters
->cntrs_max_index
) {
3264 /* counters not bound yet, must have udata passed */
3273 mlx5_fc_destroy(to_mdev(ibcounters
->device
)->mdev
,
3274 mcounters
->hw_cntrs_hndl
);
3275 mcounters
->hw_cntrs_hndl
= NULL
;
3282 static struct mlx5_ib_flow_handler
*_create_flow_rule(struct mlx5_ib_dev
*dev
,
3283 struct mlx5_ib_flow_prio
*ft_prio
,
3284 const struct ib_flow_attr
*flow_attr
,
3285 struct mlx5_flow_destination
*dst
,
3287 struct mlx5_ib_create_flow
*ucmd
)
3289 struct mlx5_flow_table
*ft
= ft_prio
->flow_table
;
3290 struct mlx5_ib_flow_handler
*handler
;
3291 struct mlx5_flow_act flow_act
= {.flow_tag
= MLX5_FS_DEFAULT_FLOW_TAG
};
3292 struct mlx5_flow_spec
*spec
;
3293 struct mlx5_flow_destination dest_arr
[2] = {};
3294 struct mlx5_flow_destination
*rule_dst
= dest_arr
;
3295 const void *ib_flow
= (const void *)flow_attr
+ sizeof(*flow_attr
);
3296 unsigned int spec_index
;
3300 bool is_egress
= flow_attr
->flags
& IB_FLOW_ATTR_FLAGS_EGRESS
;
3302 if (!is_valid_attr(dev
->mdev
, flow_attr
))
3303 return ERR_PTR(-EINVAL
);
3305 if (dev
->rep
&& is_egress
)
3306 return ERR_PTR(-EINVAL
);
3308 spec
= kvzalloc(sizeof(*spec
), GFP_KERNEL
);
3309 handler
= kzalloc(sizeof(*handler
), GFP_KERNEL
);
3310 if (!handler
|| !spec
) {
3315 INIT_LIST_HEAD(&handler
->list
);
3317 memcpy(&dest_arr
[0], dst
, sizeof(*dst
));
3321 for (spec_index
= 0; spec_index
< flow_attr
->num_of_specs
; spec_index
++) {
3322 err
= parse_flow_attr(dev
->mdev
, spec
->match_criteria
,
3324 ib_flow
, flow_attr
, &flow_act
,
3329 prev_type
= ((union ib_flow_spec
*)ib_flow
)->type
;
3330 ib_flow
+= ((union ib_flow_spec
*)ib_flow
)->size
;
3333 if (!flow_is_multicast_only(flow_attr
))
3334 set_underlay_qp(dev
, spec
, underlay_qpn
);
3339 misc
= MLX5_ADDR_OF(fte_match_param
, spec
->match_value
,
3341 MLX5_SET(fte_match_set_misc
, misc
, source_port
,
3343 misc
= MLX5_ADDR_OF(fte_match_param
, spec
->match_criteria
,
3345 MLX5_SET_TO_ONES(fte_match_set_misc
, misc
, source_port
);
3348 spec
->match_criteria_enable
= get_match_criteria_enable(spec
->match_criteria
);
3351 !is_valid_spec(dev
->mdev
, spec
, &flow_act
, is_egress
)) {
3356 if (flow_act
.action
& MLX5_FLOW_CONTEXT_ACTION_COUNT
) {
3357 err
= flow_counters_set_data(flow_act
.counters
, ucmd
);
3361 handler
->ibcounters
= flow_act
.counters
;
3362 dest_arr
[dest_num
].type
=
3363 MLX5_FLOW_DESTINATION_TYPE_COUNTER
;
3364 dest_arr
[dest_num
].counter
=
3365 to_mcounters(flow_act
.counters
)->hw_cntrs_hndl
;
3369 if (flow_act
.action
& MLX5_FLOW_CONTEXT_ACTION_DROP
) {
3370 if (!(flow_act
.action
& MLX5_FLOW_CONTEXT_ACTION_COUNT
)) {
3376 flow_act
.action
|= MLX5_FLOW_CONTEXT_ACTION_ALLOW
;
3379 dest_num
? MLX5_FLOW_CONTEXT_ACTION_FWD_DEST
:
3380 MLX5_FLOW_CONTEXT_ACTION_FWD_NEXT_PRIO
;
3383 if (flow_act
.has_flow_tag
&&
3384 (flow_attr
->type
== IB_FLOW_ATTR_ALL_DEFAULT
||
3385 flow_attr
->type
== IB_FLOW_ATTR_MC_DEFAULT
)) {
3386 mlx5_ib_warn(dev
, "Flow tag %u and attribute type %x isn't allowed in leftovers\n",
3387 flow_act
.flow_tag
, flow_attr
->type
);
3391 handler
->rule
= mlx5_add_flow_rules(ft
, spec
,
3393 rule_dst
, dest_num
);
3395 if (IS_ERR(handler
->rule
)) {
3396 err
= PTR_ERR(handler
->rule
);
3400 ft_prio
->refcount
++;
3401 handler
->prio
= ft_prio
;
3404 ft_prio
->flow_table
= ft
;
3406 if (err
&& handler
) {
3407 if (handler
->ibcounters
&&
3408 atomic_read(&handler
->ibcounters
->usecnt
) == 1)
3409 counters_clear_description(handler
->ibcounters
);
3413 return err
? ERR_PTR(err
) : handler
;
3416 static struct mlx5_ib_flow_handler
*create_flow_rule(struct mlx5_ib_dev
*dev
,
3417 struct mlx5_ib_flow_prio
*ft_prio
,
3418 const struct ib_flow_attr
*flow_attr
,
3419 struct mlx5_flow_destination
*dst
)
3421 return _create_flow_rule(dev
, ft_prio
, flow_attr
, dst
, 0, NULL
);
3424 static struct mlx5_ib_flow_handler
*create_dont_trap_rule(struct mlx5_ib_dev
*dev
,
3425 struct mlx5_ib_flow_prio
*ft_prio
,
3426 struct ib_flow_attr
*flow_attr
,
3427 struct mlx5_flow_destination
*dst
)
3429 struct mlx5_ib_flow_handler
*handler_dst
= NULL
;
3430 struct mlx5_ib_flow_handler
*handler
= NULL
;
3432 handler
= create_flow_rule(dev
, ft_prio
, flow_attr
, NULL
);
3433 if (!IS_ERR(handler
)) {
3434 handler_dst
= create_flow_rule(dev
, ft_prio
,
3436 if (IS_ERR(handler_dst
)) {
3437 mlx5_del_flow_rules(handler
->rule
);
3438 ft_prio
->refcount
--;
3440 handler
= handler_dst
;
3442 list_add(&handler_dst
->list
, &handler
->list
);
3453 static struct mlx5_ib_flow_handler
*create_leftovers_rule(struct mlx5_ib_dev
*dev
,
3454 struct mlx5_ib_flow_prio
*ft_prio
,
3455 struct ib_flow_attr
*flow_attr
,
3456 struct mlx5_flow_destination
*dst
)
3458 struct mlx5_ib_flow_handler
*handler_ucast
= NULL
;
3459 struct mlx5_ib_flow_handler
*handler
= NULL
;
3462 struct ib_flow_attr flow_attr
;
3463 struct ib_flow_spec_eth eth_flow
;
3464 } leftovers_specs
[] = {
3468 .size
= sizeof(leftovers_specs
[0])
3471 .type
= IB_FLOW_SPEC_ETH
,
3472 .size
= sizeof(struct ib_flow_spec_eth
),
3473 .mask
= {.dst_mac
= {0x1} },
3474 .val
= {.dst_mac
= {0x1} }
3480 .size
= sizeof(leftovers_specs
[0])
3483 .type
= IB_FLOW_SPEC_ETH
,
3484 .size
= sizeof(struct ib_flow_spec_eth
),
3485 .mask
= {.dst_mac
= {0x1} },
3486 .val
= {.dst_mac
= {} }
3491 handler
= create_flow_rule(dev
, ft_prio
,
3492 &leftovers_specs
[LEFTOVERS_MC
].flow_attr
,
3494 if (!IS_ERR(handler
) &&
3495 flow_attr
->type
== IB_FLOW_ATTR_ALL_DEFAULT
) {
3496 handler_ucast
= create_flow_rule(dev
, ft_prio
,
3497 &leftovers_specs
[LEFTOVERS_UC
].flow_attr
,
3499 if (IS_ERR(handler_ucast
)) {
3500 mlx5_del_flow_rules(handler
->rule
);
3501 ft_prio
->refcount
--;
3503 handler
= handler_ucast
;
3505 list_add(&handler_ucast
->list
, &handler
->list
);
3512 static struct mlx5_ib_flow_handler
*create_sniffer_rule(struct mlx5_ib_dev
*dev
,
3513 struct mlx5_ib_flow_prio
*ft_rx
,
3514 struct mlx5_ib_flow_prio
*ft_tx
,
3515 struct mlx5_flow_destination
*dst
)
3517 struct mlx5_ib_flow_handler
*handler_rx
;
3518 struct mlx5_ib_flow_handler
*handler_tx
;
3520 static const struct ib_flow_attr flow_attr
= {
3522 .size
= sizeof(flow_attr
)
3525 handler_rx
= create_flow_rule(dev
, ft_rx
, &flow_attr
, dst
);
3526 if (IS_ERR(handler_rx
)) {
3527 err
= PTR_ERR(handler_rx
);
3531 handler_tx
= create_flow_rule(dev
, ft_tx
, &flow_attr
, dst
);
3532 if (IS_ERR(handler_tx
)) {
3533 err
= PTR_ERR(handler_tx
);
3537 list_add(&handler_tx
->list
, &handler_rx
->list
);
3542 mlx5_del_flow_rules(handler_rx
->rule
);
3546 return ERR_PTR(err
);
3549 static struct ib_flow
*mlx5_ib_create_flow(struct ib_qp
*qp
,
3550 struct ib_flow_attr
*flow_attr
,
3552 struct ib_udata
*udata
)
3554 struct mlx5_ib_dev
*dev
= to_mdev(qp
->device
);
3555 struct mlx5_ib_qp
*mqp
= to_mqp(qp
);
3556 struct mlx5_ib_flow_handler
*handler
= NULL
;
3557 struct mlx5_flow_destination
*dst
= NULL
;
3558 struct mlx5_ib_flow_prio
*ft_prio_tx
= NULL
;
3559 struct mlx5_ib_flow_prio
*ft_prio
;
3560 bool is_egress
= flow_attr
->flags
& IB_FLOW_ATTR_FLAGS_EGRESS
;
3561 struct mlx5_ib_create_flow
*ucmd
= NULL
, ucmd_hdr
;
3562 size_t min_ucmd_sz
, required_ucmd_sz
;
3566 if (udata
&& udata
->inlen
) {
3567 min_ucmd_sz
= offsetof(typeof(ucmd_hdr
), reserved
) +
3568 sizeof(ucmd_hdr
.reserved
);
3569 if (udata
->inlen
< min_ucmd_sz
)
3570 return ERR_PTR(-EOPNOTSUPP
);
3572 err
= ib_copy_from_udata(&ucmd_hdr
, udata
, min_ucmd_sz
);
3574 return ERR_PTR(err
);
3576 /* currently supports only one counters data */
3577 if (ucmd_hdr
.ncounters_data
> 1)
3578 return ERR_PTR(-EINVAL
);
3580 required_ucmd_sz
= min_ucmd_sz
+
3581 sizeof(struct mlx5_ib_flow_counters_data
) *
3582 ucmd_hdr
.ncounters_data
;
3583 if (udata
->inlen
> required_ucmd_sz
&&
3584 !ib_is_udata_cleared(udata
, required_ucmd_sz
,
3585 udata
->inlen
- required_ucmd_sz
))
3586 return ERR_PTR(-EOPNOTSUPP
);
3588 ucmd
= kzalloc(required_ucmd_sz
, GFP_KERNEL
);
3590 return ERR_PTR(-ENOMEM
);
3592 err
= ib_copy_from_udata(ucmd
, udata
, required_ucmd_sz
);
3597 if (flow_attr
->priority
> MLX5_IB_FLOW_LAST_PRIO
) {
3602 if (domain
!= IB_FLOW_DOMAIN_USER
||
3603 flow_attr
->port
> dev
->num_ports
||
3604 (flow_attr
->flags
& ~(IB_FLOW_ATTR_FLAGS_DONT_TRAP
|
3605 IB_FLOW_ATTR_FLAGS_EGRESS
))) {
3611 (flow_attr
->type
== IB_FLOW_ATTR_ALL_DEFAULT
||
3612 flow_attr
->type
== IB_FLOW_ATTR_MC_DEFAULT
)) {
3617 dst
= kzalloc(sizeof(*dst
), GFP_KERNEL
);
3623 mutex_lock(&dev
->flow_db
->lock
);
3625 ft_prio
= get_flow_table(dev
, flow_attr
,
3626 is_egress
? MLX5_IB_FT_TX
: MLX5_IB_FT_RX
);
3627 if (IS_ERR(ft_prio
)) {
3628 err
= PTR_ERR(ft_prio
);
3631 if (flow_attr
->type
== IB_FLOW_ATTR_SNIFFER
) {
3632 ft_prio_tx
= get_flow_table(dev
, flow_attr
, MLX5_IB_FT_TX
);
3633 if (IS_ERR(ft_prio_tx
)) {
3634 err
= PTR_ERR(ft_prio_tx
);
3641 dst
->type
= MLX5_FLOW_DESTINATION_TYPE_PORT
;
3643 dst
->type
= MLX5_FLOW_DESTINATION_TYPE_TIR
;
3644 if (mqp
->flags
& MLX5_IB_QP_RSS
)
3645 dst
->tir_num
= mqp
->rss_qp
.tirn
;
3647 dst
->tir_num
= mqp
->raw_packet_qp
.rq
.tirn
;
3650 if (flow_attr
->type
== IB_FLOW_ATTR_NORMAL
) {
3651 if (flow_attr
->flags
& IB_FLOW_ATTR_FLAGS_DONT_TRAP
) {
3652 handler
= create_dont_trap_rule(dev
, ft_prio
,
3655 underlay_qpn
= (mqp
->flags
& MLX5_IB_QP_UNDERLAY
) ?
3656 mqp
->underlay_qpn
: 0;
3657 handler
= _create_flow_rule(dev
, ft_prio
, flow_attr
,
3658 dst
, underlay_qpn
, ucmd
);
3660 } else if (flow_attr
->type
== IB_FLOW_ATTR_ALL_DEFAULT
||
3661 flow_attr
->type
== IB_FLOW_ATTR_MC_DEFAULT
) {
3662 handler
= create_leftovers_rule(dev
, ft_prio
, flow_attr
,
3664 } else if (flow_attr
->type
== IB_FLOW_ATTR_SNIFFER
) {
3665 handler
= create_sniffer_rule(dev
, ft_prio
, ft_prio_tx
, dst
);
3671 if (IS_ERR(handler
)) {
3672 err
= PTR_ERR(handler
);
3677 mutex_unlock(&dev
->flow_db
->lock
);
3681 return &handler
->ibflow
;
3684 put_flow_table(dev
, ft_prio
, false);
3686 put_flow_table(dev
, ft_prio_tx
, false);
3688 mutex_unlock(&dev
->flow_db
->lock
);
3692 return ERR_PTR(err
);
3695 static struct mlx5_ib_flow_prio
*_get_flow_table(struct mlx5_ib_dev
*dev
,
3696 int priority
, bool mcast
)
3699 struct mlx5_flow_namespace
*ns
= NULL
;
3700 struct mlx5_ib_flow_prio
*prio
;
3702 max_table_size
= BIT(MLX5_CAP_FLOWTABLE_NIC_RX(dev
->mdev
,
3704 if (max_table_size
< MLX5_FS_MAX_ENTRIES
)
3705 return ERR_PTR(-ENOMEM
);
3708 priority
= MLX5_IB_FLOW_MCAST_PRIO
;
3710 priority
= ib_prio_to_core_prio(priority
, false);
3712 ns
= mlx5_get_flow_namespace(dev
->mdev
, MLX5_FLOW_NAMESPACE_BYPASS
);
3714 return ERR_PTR(-ENOTSUPP
);
3716 prio
= &dev
->flow_db
->prios
[priority
];
3718 if (prio
->flow_table
)
3721 return _get_prio(ns
, prio
, priority
, MLX5_FS_MAX_ENTRIES
,
3722 MLX5_FS_MAX_TYPES
, 0);
3725 static struct mlx5_ib_flow_handler
*
3726 _create_raw_flow_rule(struct mlx5_ib_dev
*dev
,
3727 struct mlx5_ib_flow_prio
*ft_prio
,
3728 struct mlx5_flow_destination
*dst
,
3729 struct mlx5_ib_flow_matcher
*fs_matcher
,
3730 void *cmd_in
, int inlen
)
3732 struct mlx5_ib_flow_handler
*handler
;
3733 struct mlx5_flow_act flow_act
= {.flow_tag
= MLX5_FS_DEFAULT_FLOW_TAG
};
3734 struct mlx5_flow_spec
*spec
;
3735 struct mlx5_flow_table
*ft
= ft_prio
->flow_table
;
3738 spec
= kvzalloc(sizeof(*spec
), GFP_KERNEL
);
3739 handler
= kzalloc(sizeof(*handler
), GFP_KERNEL
);
3740 if (!handler
|| !spec
) {
3745 INIT_LIST_HEAD(&handler
->list
);
3747 memcpy(spec
->match_value
, cmd_in
, inlen
);
3748 memcpy(spec
->match_criteria
, fs_matcher
->matcher_mask
.match_params
,
3749 fs_matcher
->mask_len
);
3750 spec
->match_criteria_enable
= fs_matcher
->match_criteria_enable
;
3752 flow_act
.action
|= MLX5_FLOW_CONTEXT_ACTION_FWD_DEST
;
3753 handler
->rule
= mlx5_add_flow_rules(ft
, spec
,
3756 if (IS_ERR(handler
->rule
)) {
3757 err
= PTR_ERR(handler
->rule
);
3761 ft_prio
->refcount
++;
3762 handler
->prio
= ft_prio
;
3764 ft_prio
->flow_table
= ft
;
3770 return err
? ERR_PTR(err
) : handler
;
3773 static bool raw_fs_is_multicast(struct mlx5_ib_flow_matcher
*fs_matcher
,
3777 void *match_v_set_lyr_2_4
, *match_c_set_lyr_2_4
;
3778 void *dmac
, *dmac_mask
;
3779 void *ipv4
, *ipv4_mask
;
3781 if (!(fs_matcher
->match_criteria_enable
&
3782 (1 << MATCH_CRITERIA_ENABLE_OUTER_BIT
)))
3785 match_c
= fs_matcher
->matcher_mask
.match_params
;
3786 match_v_set_lyr_2_4
= MLX5_ADDR_OF(fte_match_param
, match_v
,
3788 match_c_set_lyr_2_4
= MLX5_ADDR_OF(fte_match_param
, match_c
,
3791 dmac
= MLX5_ADDR_OF(fte_match_set_lyr_2_4
, match_v_set_lyr_2_4
,
3793 dmac_mask
= MLX5_ADDR_OF(fte_match_set_lyr_2_4
, match_c_set_lyr_2_4
,
3796 if (is_multicast_ether_addr(dmac
) &&
3797 is_multicast_ether_addr(dmac_mask
))
3800 ipv4
= MLX5_ADDR_OF(fte_match_set_lyr_2_4
, match_v_set_lyr_2_4
,
3801 dst_ipv4_dst_ipv6
.ipv4_layout
.ipv4
);
3803 ipv4_mask
= MLX5_ADDR_OF(fte_match_set_lyr_2_4
, match_c_set_lyr_2_4
,
3804 dst_ipv4_dst_ipv6
.ipv4_layout
.ipv4
);
3806 if (ipv4_is_multicast(*(__be32
*)(ipv4
)) &&
3807 ipv4_is_multicast(*(__be32
*)(ipv4_mask
)))
3813 struct mlx5_ib_flow_handler
*
3814 mlx5_ib_raw_fs_rule_add(struct mlx5_ib_dev
*dev
,
3815 struct mlx5_ib_flow_matcher
*fs_matcher
,
3816 void *cmd_in
, int inlen
, int dest_id
,
3819 struct mlx5_flow_destination
*dst
;
3820 struct mlx5_ib_flow_prio
*ft_prio
;
3821 int priority
= fs_matcher
->priority
;
3822 struct mlx5_ib_flow_handler
*handler
;
3826 if (fs_matcher
->flow_type
!= MLX5_IB_FLOW_TYPE_NORMAL
)
3827 return ERR_PTR(-EOPNOTSUPP
);
3829 if (fs_matcher
->priority
> MLX5_IB_FLOW_LAST_PRIO
)
3830 return ERR_PTR(-ENOMEM
);
3832 dst
= kzalloc(sizeof(*dst
), GFP_KERNEL
);
3834 return ERR_PTR(-ENOMEM
);
3836 mcast
= raw_fs_is_multicast(fs_matcher
, cmd_in
);
3837 mutex_lock(&dev
->flow_db
->lock
);
3839 ft_prio
= _get_flow_table(dev
, priority
, mcast
);
3840 if (IS_ERR(ft_prio
)) {
3841 err
= PTR_ERR(ft_prio
);
3845 if (dest_type
== MLX5_FLOW_DESTINATION_TYPE_TIR
) {
3846 dst
->type
= dest_type
;
3847 dst
->tir_num
= dest_id
;
3849 dst
->type
= MLX5_FLOW_DESTINATION_TYPE_FLOW_TABLE_NUM
;
3850 dst
->ft_num
= dest_id
;
3853 handler
= _create_raw_flow_rule(dev
, ft_prio
, dst
, fs_matcher
, cmd_in
,
3856 if (IS_ERR(handler
)) {
3857 err
= PTR_ERR(handler
);
3861 mutex_unlock(&dev
->flow_db
->lock
);
3862 atomic_inc(&fs_matcher
->usecnt
);
3863 handler
->flow_matcher
= fs_matcher
;
3870 put_flow_table(dev
, ft_prio
, false);
3872 mutex_unlock(&dev
->flow_db
->lock
);
3875 return ERR_PTR(err
);
3878 static u32
mlx5_ib_flow_action_flags_to_accel_xfrm_flags(u32 mlx5_flags
)
3882 if (mlx5_flags
& MLX5_IB_UAPI_FLOW_ACTION_FLAGS_REQUIRE_METADATA
)
3883 flags
|= MLX5_ACCEL_XFRM_FLAG_REQUIRE_METADATA
;
3888 #define MLX5_FLOW_ACTION_ESP_CREATE_LAST_SUPPORTED MLX5_IB_UAPI_FLOW_ACTION_FLAGS_REQUIRE_METADATA
3889 static struct ib_flow_action
*
3890 mlx5_ib_create_flow_action_esp(struct ib_device
*device
,
3891 const struct ib_flow_action_attrs_esp
*attr
,
3892 struct uverbs_attr_bundle
*attrs
)
3894 struct mlx5_ib_dev
*mdev
= to_mdev(device
);
3895 struct ib_uverbs_flow_action_esp_keymat_aes_gcm
*aes_gcm
;
3896 struct mlx5_accel_esp_xfrm_attrs accel_attrs
= {};
3897 struct mlx5_ib_flow_action
*action
;
3902 err
= uverbs_get_flags64(
3903 &action_flags
, attrs
, MLX5_IB_ATTR_CREATE_FLOW_ACTION_FLAGS
,
3904 ((MLX5_FLOW_ACTION_ESP_CREATE_LAST_SUPPORTED
<< 1) - 1));
3906 return ERR_PTR(err
);
3908 flags
= mlx5_ib_flow_action_flags_to_accel_xfrm_flags(action_flags
);
3910 /* We current only support a subset of the standard features. Only a
3911 * keymat of type AES_GCM, with icv_len == 16, iv_algo == SEQ and esn
3912 * (with overlap). Full offload mode isn't supported.
3914 if (!attr
->keymat
|| attr
->replay
|| attr
->encap
||
3915 attr
->spi
|| attr
->seq
|| attr
->tfc_pad
||
3916 attr
->hard_limit_pkts
||
3917 (attr
->flags
& ~(IB_FLOW_ACTION_ESP_FLAGS_ESN_TRIGGERED
|
3918 IB_UVERBS_FLOW_ACTION_ESP_FLAGS_ENCRYPT
)))
3919 return ERR_PTR(-EOPNOTSUPP
);
3921 if (attr
->keymat
->protocol
!=
3922 IB_UVERBS_FLOW_ACTION_ESP_KEYMAT_AES_GCM
)
3923 return ERR_PTR(-EOPNOTSUPP
);
3925 aes_gcm
= &attr
->keymat
->keymat
.aes_gcm
;
3927 if (aes_gcm
->icv_len
!= 16 ||
3928 aes_gcm
->iv_algo
!= IB_UVERBS_FLOW_ACTION_IV_ALGO_SEQ
)
3929 return ERR_PTR(-EOPNOTSUPP
);
3931 action
= kmalloc(sizeof(*action
), GFP_KERNEL
);
3933 return ERR_PTR(-ENOMEM
);
3935 action
->esp_aes_gcm
.ib_flags
= attr
->flags
;
3936 memcpy(&accel_attrs
.keymat
.aes_gcm
.aes_key
, &aes_gcm
->aes_key
,
3937 sizeof(accel_attrs
.keymat
.aes_gcm
.aes_key
));
3938 accel_attrs
.keymat
.aes_gcm
.key_len
= aes_gcm
->key_len
* 8;
3939 memcpy(&accel_attrs
.keymat
.aes_gcm
.salt
, &aes_gcm
->salt
,
3940 sizeof(accel_attrs
.keymat
.aes_gcm
.salt
));
3941 memcpy(&accel_attrs
.keymat
.aes_gcm
.seq_iv
, &aes_gcm
->iv
,
3942 sizeof(accel_attrs
.keymat
.aes_gcm
.seq_iv
));
3943 accel_attrs
.keymat
.aes_gcm
.icv_len
= aes_gcm
->icv_len
* 8;
3944 accel_attrs
.keymat
.aes_gcm
.iv_algo
= MLX5_ACCEL_ESP_AES_GCM_IV_ALGO_SEQ
;
3945 accel_attrs
.keymat_type
= MLX5_ACCEL_ESP_KEYMAT_AES_GCM
;
3947 accel_attrs
.esn
= attr
->esn
;
3948 if (attr
->flags
& IB_FLOW_ACTION_ESP_FLAGS_ESN_TRIGGERED
)
3949 accel_attrs
.flags
|= MLX5_ACCEL_ESP_FLAGS_ESN_TRIGGERED
;
3950 if (attr
->flags
& IB_UVERBS_FLOW_ACTION_ESP_FLAGS_ESN_NEW_WINDOW
)
3951 accel_attrs
.flags
|= MLX5_ACCEL_ESP_FLAGS_ESN_STATE_OVERLAP
;
3953 if (attr
->flags
& IB_UVERBS_FLOW_ACTION_ESP_FLAGS_ENCRYPT
)
3954 accel_attrs
.action
|= MLX5_ACCEL_ESP_ACTION_ENCRYPT
;
3956 action
->esp_aes_gcm
.ctx
=
3957 mlx5_accel_esp_create_xfrm(mdev
->mdev
, &accel_attrs
, flags
);
3958 if (IS_ERR(action
->esp_aes_gcm
.ctx
)) {
3959 err
= PTR_ERR(action
->esp_aes_gcm
.ctx
);
3963 action
->esp_aes_gcm
.ib_flags
= attr
->flags
;
3965 return &action
->ib_action
;
3969 return ERR_PTR(err
);
3973 mlx5_ib_modify_flow_action_esp(struct ib_flow_action
*action
,
3974 const struct ib_flow_action_attrs_esp
*attr
,
3975 struct uverbs_attr_bundle
*attrs
)
3977 struct mlx5_ib_flow_action
*maction
= to_mflow_act(action
);
3978 struct mlx5_accel_esp_xfrm_attrs accel_attrs
;
3981 if (attr
->keymat
|| attr
->replay
|| attr
->encap
||
3982 attr
->spi
|| attr
->seq
|| attr
->tfc_pad
||
3983 attr
->hard_limit_pkts
||
3984 (attr
->flags
& ~(IB_FLOW_ACTION_ESP_FLAGS_ESN_TRIGGERED
|
3985 IB_FLOW_ACTION_ESP_FLAGS_MOD_ESP_ATTRS
|
3986 IB_UVERBS_FLOW_ACTION_ESP_FLAGS_ESN_NEW_WINDOW
)))
3989 /* Only the ESN value or the MLX5_ACCEL_ESP_FLAGS_ESN_STATE_OVERLAP can
3992 if (!(maction
->esp_aes_gcm
.ib_flags
&
3993 IB_FLOW_ACTION_ESP_FLAGS_ESN_TRIGGERED
) &&
3994 attr
->flags
& (IB_FLOW_ACTION_ESP_FLAGS_ESN_TRIGGERED
|
3995 IB_UVERBS_FLOW_ACTION_ESP_FLAGS_ESN_NEW_WINDOW
))
3998 memcpy(&accel_attrs
, &maction
->esp_aes_gcm
.ctx
->attrs
,
3999 sizeof(accel_attrs
));
4001 accel_attrs
.esn
= attr
->esn
;
4002 if (attr
->flags
& IB_UVERBS_FLOW_ACTION_ESP_FLAGS_ESN_NEW_WINDOW
)
4003 accel_attrs
.flags
|= MLX5_ACCEL_ESP_FLAGS_ESN_STATE_OVERLAP
;
4005 accel_attrs
.flags
&= ~MLX5_ACCEL_ESP_FLAGS_ESN_STATE_OVERLAP
;
4007 err
= mlx5_accel_esp_modify_xfrm(maction
->esp_aes_gcm
.ctx
,
4012 maction
->esp_aes_gcm
.ib_flags
&=
4013 ~IB_UVERBS_FLOW_ACTION_ESP_FLAGS_ESN_NEW_WINDOW
;
4014 maction
->esp_aes_gcm
.ib_flags
|=
4015 attr
->flags
& IB_UVERBS_FLOW_ACTION_ESP_FLAGS_ESN_NEW_WINDOW
;
4020 static int mlx5_ib_destroy_flow_action(struct ib_flow_action
*action
)
4022 struct mlx5_ib_flow_action
*maction
= to_mflow_act(action
);
4024 switch (action
->type
) {
4025 case IB_FLOW_ACTION_ESP
:
4027 * We only support aes_gcm by now, so we implicitly know this is
4028 * the underline crypto.
4030 mlx5_accel_esp_destroy_xfrm(maction
->esp_aes_gcm
.ctx
);
4032 case IB_FLOW_ACTION_UNSPECIFIED
:
4033 mlx5_ib_destroy_flow_action_raw(maction
);
4044 static int mlx5_ib_mcg_attach(struct ib_qp
*ibqp
, union ib_gid
*gid
, u16 lid
)
4046 struct mlx5_ib_dev
*dev
= to_mdev(ibqp
->device
);
4047 struct mlx5_ib_qp
*mqp
= to_mqp(ibqp
);
4050 if (mqp
->flags
& MLX5_IB_QP_UNDERLAY
) {
4051 mlx5_ib_dbg(dev
, "Attaching a multi cast group to underlay QP is not supported\n");
4055 err
= mlx5_core_attach_mcg(dev
->mdev
, gid
, ibqp
->qp_num
);
4057 mlx5_ib_warn(dev
, "failed attaching QPN 0x%x, MGID %pI6\n",
4058 ibqp
->qp_num
, gid
->raw
);
4063 static int mlx5_ib_mcg_detach(struct ib_qp
*ibqp
, union ib_gid
*gid
, u16 lid
)
4065 struct mlx5_ib_dev
*dev
= to_mdev(ibqp
->device
);
4068 err
= mlx5_core_detach_mcg(dev
->mdev
, gid
, ibqp
->qp_num
);
4070 mlx5_ib_warn(dev
, "failed detaching QPN 0x%x, MGID %pI6\n",
4071 ibqp
->qp_num
, gid
->raw
);
4076 static int init_node_data(struct mlx5_ib_dev
*dev
)
4080 err
= mlx5_query_node_desc(dev
, dev
->ib_dev
.node_desc
);
4084 dev
->mdev
->rev_id
= dev
->mdev
->pdev
->revision
;
4086 return mlx5_query_node_guid(dev
, &dev
->ib_dev
.node_guid
);
4089 static ssize_t
show_fw_pages(struct device
*device
, struct device_attribute
*attr
,
4092 struct mlx5_ib_dev
*dev
=
4093 container_of(device
, struct mlx5_ib_dev
, ib_dev
.dev
);
4095 return sprintf(buf
, "%d\n", dev
->mdev
->priv
.fw_pages
);
4098 static ssize_t
show_reg_pages(struct device
*device
,
4099 struct device_attribute
*attr
, char *buf
)
4101 struct mlx5_ib_dev
*dev
=
4102 container_of(device
, struct mlx5_ib_dev
, ib_dev
.dev
);
4104 return sprintf(buf
, "%d\n", atomic_read(&dev
->mdev
->priv
.reg_pages
));
4107 static ssize_t
show_hca(struct device
*device
, struct device_attribute
*attr
,
4110 struct mlx5_ib_dev
*dev
=
4111 container_of(device
, struct mlx5_ib_dev
, ib_dev
.dev
);
4112 return sprintf(buf
, "MT%d\n", dev
->mdev
->pdev
->device
);
4115 static ssize_t
show_rev(struct device
*device
, struct device_attribute
*attr
,
4118 struct mlx5_ib_dev
*dev
=
4119 container_of(device
, struct mlx5_ib_dev
, ib_dev
.dev
);
4120 return sprintf(buf
, "%x\n", dev
->mdev
->rev_id
);
4123 static ssize_t
show_board(struct device
*device
, struct device_attribute
*attr
,
4126 struct mlx5_ib_dev
*dev
=
4127 container_of(device
, struct mlx5_ib_dev
, ib_dev
.dev
);
4128 return sprintf(buf
, "%.*s\n", MLX5_BOARD_ID_LEN
,
4129 dev
->mdev
->board_id
);
4132 static DEVICE_ATTR(hw_rev
, S_IRUGO
, show_rev
, NULL
);
4133 static DEVICE_ATTR(hca_type
, S_IRUGO
, show_hca
, NULL
);
4134 static DEVICE_ATTR(board_id
, S_IRUGO
, show_board
, NULL
);
4135 static DEVICE_ATTR(fw_pages
, S_IRUGO
, show_fw_pages
, NULL
);
4136 static DEVICE_ATTR(reg_pages
, S_IRUGO
, show_reg_pages
, NULL
);
4138 static struct device_attribute
*mlx5_class_attributes
[] = {
4143 &dev_attr_reg_pages
,
4146 static void pkey_change_handler(struct work_struct
*work
)
4148 struct mlx5_ib_port_resources
*ports
=
4149 container_of(work
, struct mlx5_ib_port_resources
,
4152 mutex_lock(&ports
->devr
->mutex
);
4153 mlx5_ib_gsi_pkey_change(ports
->gsi
);
4154 mutex_unlock(&ports
->devr
->mutex
);
4157 static void mlx5_ib_handle_internal_error(struct mlx5_ib_dev
*ibdev
)
4159 struct mlx5_ib_qp
*mqp
;
4160 struct mlx5_ib_cq
*send_mcq
, *recv_mcq
;
4161 struct mlx5_core_cq
*mcq
;
4162 struct list_head cq_armed_list
;
4163 unsigned long flags_qp
;
4164 unsigned long flags_cq
;
4165 unsigned long flags
;
4167 INIT_LIST_HEAD(&cq_armed_list
);
4169 /* Go over qp list reside on that ibdev, sync with create/destroy qp.*/
4170 spin_lock_irqsave(&ibdev
->reset_flow_resource_lock
, flags
);
4171 list_for_each_entry(mqp
, &ibdev
->qp_list
, qps_list
) {
4172 spin_lock_irqsave(&mqp
->sq
.lock
, flags_qp
);
4173 if (mqp
->sq
.tail
!= mqp
->sq
.head
) {
4174 send_mcq
= to_mcq(mqp
->ibqp
.send_cq
);
4175 spin_lock_irqsave(&send_mcq
->lock
, flags_cq
);
4176 if (send_mcq
->mcq
.comp
&&
4177 mqp
->ibqp
.send_cq
->comp_handler
) {
4178 if (!send_mcq
->mcq
.reset_notify_added
) {
4179 send_mcq
->mcq
.reset_notify_added
= 1;
4180 list_add_tail(&send_mcq
->mcq
.reset_notify
,
4184 spin_unlock_irqrestore(&send_mcq
->lock
, flags_cq
);
4186 spin_unlock_irqrestore(&mqp
->sq
.lock
, flags_qp
);
4187 spin_lock_irqsave(&mqp
->rq
.lock
, flags_qp
);
4188 /* no handling is needed for SRQ */
4189 if (!mqp
->ibqp
.srq
) {
4190 if (mqp
->rq
.tail
!= mqp
->rq
.head
) {
4191 recv_mcq
= to_mcq(mqp
->ibqp
.recv_cq
);
4192 spin_lock_irqsave(&recv_mcq
->lock
, flags_cq
);
4193 if (recv_mcq
->mcq
.comp
&&
4194 mqp
->ibqp
.recv_cq
->comp_handler
) {
4195 if (!recv_mcq
->mcq
.reset_notify_added
) {
4196 recv_mcq
->mcq
.reset_notify_added
= 1;
4197 list_add_tail(&recv_mcq
->mcq
.reset_notify
,
4201 spin_unlock_irqrestore(&recv_mcq
->lock
,
4205 spin_unlock_irqrestore(&mqp
->rq
.lock
, flags_qp
);
4207 /*At that point all inflight post send were put to be executed as of we
4208 * lock/unlock above locks Now need to arm all involved CQs.
4210 list_for_each_entry(mcq
, &cq_armed_list
, reset_notify
) {
4213 spin_unlock_irqrestore(&ibdev
->reset_flow_resource_lock
, flags
);
4216 static void delay_drop_handler(struct work_struct
*work
)
4219 struct mlx5_ib_delay_drop
*delay_drop
=
4220 container_of(work
, struct mlx5_ib_delay_drop
,
4223 atomic_inc(&delay_drop
->events_cnt
);
4225 mutex_lock(&delay_drop
->lock
);
4226 err
= mlx5_core_set_delay_drop(delay_drop
->dev
->mdev
,
4227 delay_drop
->timeout
);
4229 mlx5_ib_warn(delay_drop
->dev
, "Failed to set delay drop, timeout=%u\n",
4230 delay_drop
->timeout
);
4231 delay_drop
->activate
= false;
4233 mutex_unlock(&delay_drop
->lock
);
4236 static void mlx5_ib_handle_event(struct work_struct
*_work
)
4238 struct mlx5_ib_event_work
*work
=
4239 container_of(_work
, struct mlx5_ib_event_work
, work
);
4240 struct mlx5_ib_dev
*ibdev
;
4241 struct ib_event ibev
;
4243 u8 port
= (u8
)work
->param
;
4245 if (mlx5_core_is_mp_slave(work
->dev
)) {
4246 ibdev
= mlx5_ib_get_ibdev_from_mpi(work
->context
);
4250 ibdev
= work
->context
;
4253 switch (work
->event
) {
4254 case MLX5_DEV_EVENT_SYS_ERROR
:
4255 ibev
.event
= IB_EVENT_DEVICE_FATAL
;
4256 mlx5_ib_handle_internal_error(ibdev
);
4260 case MLX5_DEV_EVENT_PORT_UP
:
4261 case MLX5_DEV_EVENT_PORT_DOWN
:
4262 case MLX5_DEV_EVENT_PORT_INITIALIZED
:
4263 /* In RoCE, port up/down events are handled in
4264 * mlx5_netdev_event().
4266 if (mlx5_ib_port_link_layer(&ibdev
->ib_dev
, port
) ==
4267 IB_LINK_LAYER_ETHERNET
)
4270 ibev
.event
= (work
->event
== MLX5_DEV_EVENT_PORT_UP
) ?
4271 IB_EVENT_PORT_ACTIVE
: IB_EVENT_PORT_ERR
;
4274 case MLX5_DEV_EVENT_LID_CHANGE
:
4275 ibev
.event
= IB_EVENT_LID_CHANGE
;
4278 case MLX5_DEV_EVENT_PKEY_CHANGE
:
4279 ibev
.event
= IB_EVENT_PKEY_CHANGE
;
4280 schedule_work(&ibdev
->devr
.ports
[port
- 1].pkey_change_work
);
4283 case MLX5_DEV_EVENT_GUID_CHANGE
:
4284 ibev
.event
= IB_EVENT_GID_CHANGE
;
4287 case MLX5_DEV_EVENT_CLIENT_REREG
:
4288 ibev
.event
= IB_EVENT_CLIENT_REREGISTER
;
4290 case MLX5_DEV_EVENT_DELAY_DROP_TIMEOUT
:
4291 schedule_work(&ibdev
->delay_drop
.delay_drop_work
);
4297 ibev
.device
= &ibdev
->ib_dev
;
4298 ibev
.element
.port_num
= port
;
4300 if (!rdma_is_port_valid(&ibdev
->ib_dev
, port
)) {
4301 mlx5_ib_warn(ibdev
, "warning: event on port %d\n", port
);
4305 if (ibdev
->ib_active
)
4306 ib_dispatch_event(&ibev
);
4309 ibdev
->ib_active
= false;
4314 static void mlx5_ib_event(struct mlx5_core_dev
*dev
, void *context
,
4315 enum mlx5_dev_event event
, unsigned long param
)
4317 struct mlx5_ib_event_work
*work
;
4319 work
= kmalloc(sizeof(*work
), GFP_ATOMIC
);
4323 INIT_WORK(&work
->work
, mlx5_ib_handle_event
);
4325 work
->param
= param
;
4326 work
->context
= context
;
4327 work
->event
= event
;
4329 queue_work(mlx5_ib_event_wq
, &work
->work
);
4332 static int set_has_smi_cap(struct mlx5_ib_dev
*dev
)
4334 struct mlx5_hca_vport_context vport_ctx
;
4338 for (port
= 1; port
<= dev
->num_ports
; port
++) {
4339 dev
->mdev
->port_caps
[port
- 1].has_smi
= false;
4340 if (MLX5_CAP_GEN(dev
->mdev
, port_type
) ==
4341 MLX5_CAP_PORT_TYPE_IB
) {
4342 if (MLX5_CAP_GEN(dev
->mdev
, ib_virt
)) {
4343 err
= mlx5_query_hca_vport_context(dev
->mdev
, 0,
4347 mlx5_ib_err(dev
, "query_hca_vport_context for port=%d failed %d\n",
4351 dev
->mdev
->port_caps
[port
- 1].has_smi
=
4354 dev
->mdev
->port_caps
[port
- 1].has_smi
= true;
4361 static void get_ext_port_caps(struct mlx5_ib_dev
*dev
)
4365 for (port
= 1; port
<= dev
->num_ports
; port
++)
4366 mlx5_query_ext_port_caps(dev
, port
);
4369 static int get_port_caps(struct mlx5_ib_dev
*dev
, u8 port
)
4371 struct ib_device_attr
*dprops
= NULL
;
4372 struct ib_port_attr
*pprops
= NULL
;
4374 struct ib_udata uhw
= {.inlen
= 0, .outlen
= 0};
4376 pprops
= kmalloc(sizeof(*pprops
), GFP_KERNEL
);
4380 dprops
= kmalloc(sizeof(*dprops
), GFP_KERNEL
);
4384 err
= set_has_smi_cap(dev
);
4388 err
= mlx5_ib_query_device(&dev
->ib_dev
, dprops
, &uhw
);
4390 mlx5_ib_warn(dev
, "query_device failed %d\n", err
);
4394 memset(pprops
, 0, sizeof(*pprops
));
4395 err
= mlx5_ib_query_port(&dev
->ib_dev
, port
, pprops
);
4397 mlx5_ib_warn(dev
, "query_port %d failed %d\n",
4402 dev
->mdev
->port_caps
[port
- 1].pkey_table_len
=
4404 dev
->mdev
->port_caps
[port
- 1].gid_table_len
=
4405 pprops
->gid_tbl_len
;
4406 mlx5_ib_dbg(dev
, "port %d: pkey_table_len %d, gid_table_len %d\n",
4407 port
, dprops
->max_pkeys
, pprops
->gid_tbl_len
);
4416 static void destroy_umrc_res(struct mlx5_ib_dev
*dev
)
4420 err
= mlx5_mr_cache_cleanup(dev
);
4422 mlx5_ib_warn(dev
, "mr cache cleanup failed\n");
4425 mlx5_ib_destroy_qp(dev
->umrc
.qp
);
4427 ib_free_cq(dev
->umrc
.cq
);
4429 ib_dealloc_pd(dev
->umrc
.pd
);
4436 static int create_umr_res(struct mlx5_ib_dev
*dev
)
4438 struct ib_qp_init_attr
*init_attr
= NULL
;
4439 struct ib_qp_attr
*attr
= NULL
;
4445 attr
= kzalloc(sizeof(*attr
), GFP_KERNEL
);
4446 init_attr
= kzalloc(sizeof(*init_attr
), GFP_KERNEL
);
4447 if (!attr
|| !init_attr
) {
4452 pd
= ib_alloc_pd(&dev
->ib_dev
, 0);
4454 mlx5_ib_dbg(dev
, "Couldn't create PD for sync UMR QP\n");
4459 cq
= ib_alloc_cq(&dev
->ib_dev
, NULL
, 128, 0, IB_POLL_SOFTIRQ
);
4461 mlx5_ib_dbg(dev
, "Couldn't create CQ for sync UMR QP\n");
4466 init_attr
->send_cq
= cq
;
4467 init_attr
->recv_cq
= cq
;
4468 init_attr
->sq_sig_type
= IB_SIGNAL_ALL_WR
;
4469 init_attr
->cap
.max_send_wr
= MAX_UMR_WR
;
4470 init_attr
->cap
.max_send_sge
= 1;
4471 init_attr
->qp_type
= MLX5_IB_QPT_REG_UMR
;
4472 init_attr
->port_num
= 1;
4473 qp
= mlx5_ib_create_qp(pd
, init_attr
, NULL
);
4475 mlx5_ib_dbg(dev
, "Couldn't create sync UMR QP\n");
4479 qp
->device
= &dev
->ib_dev
;
4482 qp
->qp_type
= MLX5_IB_QPT_REG_UMR
;
4483 qp
->send_cq
= init_attr
->send_cq
;
4484 qp
->recv_cq
= init_attr
->recv_cq
;
4486 attr
->qp_state
= IB_QPS_INIT
;
4488 ret
= mlx5_ib_modify_qp(qp
, attr
, IB_QP_STATE
| IB_QP_PKEY_INDEX
|
4491 mlx5_ib_dbg(dev
, "Couldn't modify UMR QP\n");
4495 memset(attr
, 0, sizeof(*attr
));
4496 attr
->qp_state
= IB_QPS_RTR
;
4497 attr
->path_mtu
= IB_MTU_256
;
4499 ret
= mlx5_ib_modify_qp(qp
, attr
, IB_QP_STATE
, NULL
);
4501 mlx5_ib_dbg(dev
, "Couldn't modify umr QP to rtr\n");
4505 memset(attr
, 0, sizeof(*attr
));
4506 attr
->qp_state
= IB_QPS_RTS
;
4507 ret
= mlx5_ib_modify_qp(qp
, attr
, IB_QP_STATE
, NULL
);
4509 mlx5_ib_dbg(dev
, "Couldn't modify umr QP to rts\n");
4517 sema_init(&dev
->umrc
.sem
, MAX_UMR_WR
);
4518 ret
= mlx5_mr_cache_init(dev
);
4520 mlx5_ib_warn(dev
, "mr cache init failed %d\n", ret
);
4530 mlx5_ib_destroy_qp(qp
);
4531 dev
->umrc
.qp
= NULL
;
4535 dev
->umrc
.cq
= NULL
;
4539 dev
->umrc
.pd
= NULL
;
4547 static u8
mlx5_get_umr_fence(u8 umr_fence_cap
)
4549 switch (umr_fence_cap
) {
4550 case MLX5_CAP_UMR_FENCE_NONE
:
4551 return MLX5_FENCE_MODE_NONE
;
4552 case MLX5_CAP_UMR_FENCE_SMALL
:
4553 return MLX5_FENCE_MODE_INITIATOR_SMALL
;
4555 return MLX5_FENCE_MODE_STRONG_ORDERING
;
4559 static int create_dev_resources(struct mlx5_ib_resources
*devr
)
4561 struct ib_srq_init_attr attr
;
4562 struct mlx5_ib_dev
*dev
;
4563 struct ib_cq_init_attr cq_attr
= {.cqe
= 1};
4567 dev
= container_of(devr
, struct mlx5_ib_dev
, devr
);
4569 mutex_init(&devr
->mutex
);
4571 devr
->p0
= mlx5_ib_alloc_pd(&dev
->ib_dev
, NULL
, NULL
);
4572 if (IS_ERR(devr
->p0
)) {
4573 ret
= PTR_ERR(devr
->p0
);
4576 devr
->p0
->device
= &dev
->ib_dev
;
4577 devr
->p0
->uobject
= NULL
;
4578 atomic_set(&devr
->p0
->usecnt
, 0);
4580 devr
->c0
= mlx5_ib_create_cq(&dev
->ib_dev
, &cq_attr
, NULL
, NULL
);
4581 if (IS_ERR(devr
->c0
)) {
4582 ret
= PTR_ERR(devr
->c0
);
4585 devr
->c0
->device
= &dev
->ib_dev
;
4586 devr
->c0
->uobject
= NULL
;
4587 devr
->c0
->comp_handler
= NULL
;
4588 devr
->c0
->event_handler
= NULL
;
4589 devr
->c0
->cq_context
= NULL
;
4590 atomic_set(&devr
->c0
->usecnt
, 0);
4592 devr
->x0
= mlx5_ib_alloc_xrcd(&dev
->ib_dev
, NULL
, NULL
);
4593 if (IS_ERR(devr
->x0
)) {
4594 ret
= PTR_ERR(devr
->x0
);
4597 devr
->x0
->device
= &dev
->ib_dev
;
4598 devr
->x0
->inode
= NULL
;
4599 atomic_set(&devr
->x0
->usecnt
, 0);
4600 mutex_init(&devr
->x0
->tgt_qp_mutex
);
4601 INIT_LIST_HEAD(&devr
->x0
->tgt_qp_list
);
4603 devr
->x1
= mlx5_ib_alloc_xrcd(&dev
->ib_dev
, NULL
, NULL
);
4604 if (IS_ERR(devr
->x1
)) {
4605 ret
= PTR_ERR(devr
->x1
);
4608 devr
->x1
->device
= &dev
->ib_dev
;
4609 devr
->x1
->inode
= NULL
;
4610 atomic_set(&devr
->x1
->usecnt
, 0);
4611 mutex_init(&devr
->x1
->tgt_qp_mutex
);
4612 INIT_LIST_HEAD(&devr
->x1
->tgt_qp_list
);
4614 memset(&attr
, 0, sizeof(attr
));
4615 attr
.attr
.max_sge
= 1;
4616 attr
.attr
.max_wr
= 1;
4617 attr
.srq_type
= IB_SRQT_XRC
;
4618 attr
.ext
.cq
= devr
->c0
;
4619 attr
.ext
.xrc
.xrcd
= devr
->x0
;
4621 devr
->s0
= mlx5_ib_create_srq(devr
->p0
, &attr
, NULL
);
4622 if (IS_ERR(devr
->s0
)) {
4623 ret
= PTR_ERR(devr
->s0
);
4626 devr
->s0
->device
= &dev
->ib_dev
;
4627 devr
->s0
->pd
= devr
->p0
;
4628 devr
->s0
->uobject
= NULL
;
4629 devr
->s0
->event_handler
= NULL
;
4630 devr
->s0
->srq_context
= NULL
;
4631 devr
->s0
->srq_type
= IB_SRQT_XRC
;
4632 devr
->s0
->ext
.xrc
.xrcd
= devr
->x0
;
4633 devr
->s0
->ext
.cq
= devr
->c0
;
4634 atomic_inc(&devr
->s0
->ext
.xrc
.xrcd
->usecnt
);
4635 atomic_inc(&devr
->s0
->ext
.cq
->usecnt
);
4636 atomic_inc(&devr
->p0
->usecnt
);
4637 atomic_set(&devr
->s0
->usecnt
, 0);
4639 memset(&attr
, 0, sizeof(attr
));
4640 attr
.attr
.max_sge
= 1;
4641 attr
.attr
.max_wr
= 1;
4642 attr
.srq_type
= IB_SRQT_BASIC
;
4643 devr
->s1
= mlx5_ib_create_srq(devr
->p0
, &attr
, NULL
);
4644 if (IS_ERR(devr
->s1
)) {
4645 ret
= PTR_ERR(devr
->s1
);
4648 devr
->s1
->device
= &dev
->ib_dev
;
4649 devr
->s1
->pd
= devr
->p0
;
4650 devr
->s1
->uobject
= NULL
;
4651 devr
->s1
->event_handler
= NULL
;
4652 devr
->s1
->srq_context
= NULL
;
4653 devr
->s1
->srq_type
= IB_SRQT_BASIC
;
4654 devr
->s1
->ext
.cq
= devr
->c0
;
4655 atomic_inc(&devr
->p0
->usecnt
);
4656 atomic_set(&devr
->s1
->usecnt
, 0);
4658 for (port
= 0; port
< ARRAY_SIZE(devr
->ports
); ++port
) {
4659 INIT_WORK(&devr
->ports
[port
].pkey_change_work
,
4660 pkey_change_handler
);
4661 devr
->ports
[port
].devr
= devr
;
4667 mlx5_ib_destroy_srq(devr
->s0
);
4669 mlx5_ib_dealloc_xrcd(devr
->x1
);
4671 mlx5_ib_dealloc_xrcd(devr
->x0
);
4673 mlx5_ib_destroy_cq(devr
->c0
);
4675 mlx5_ib_dealloc_pd(devr
->p0
);
4680 static void destroy_dev_resources(struct mlx5_ib_resources
*devr
)
4682 struct mlx5_ib_dev
*dev
=
4683 container_of(devr
, struct mlx5_ib_dev
, devr
);
4686 mlx5_ib_destroy_srq(devr
->s1
);
4687 mlx5_ib_destroy_srq(devr
->s0
);
4688 mlx5_ib_dealloc_xrcd(devr
->x0
);
4689 mlx5_ib_dealloc_xrcd(devr
->x1
);
4690 mlx5_ib_destroy_cq(devr
->c0
);
4691 mlx5_ib_dealloc_pd(devr
->p0
);
4693 /* Make sure no change P_Key work items are still executing */
4694 for (port
= 0; port
< dev
->num_ports
; ++port
)
4695 cancel_work_sync(&devr
->ports
[port
].pkey_change_work
);
4698 static u32
get_core_cap_flags(struct ib_device
*ibdev
,
4699 struct mlx5_hca_vport_context
*rep
)
4701 struct mlx5_ib_dev
*dev
= to_mdev(ibdev
);
4702 enum rdma_link_layer ll
= mlx5_ib_port_link_layer(ibdev
, 1);
4703 u8 l3_type_cap
= MLX5_CAP_ROCE(dev
->mdev
, l3_type
);
4704 u8 roce_version_cap
= MLX5_CAP_ROCE(dev
->mdev
, roce_version
);
4705 bool raw_support
= !mlx5_core_mp_enabled(dev
->mdev
);
4708 if (rep
->grh_required
)
4709 ret
|= RDMA_CORE_CAP_IB_GRH_REQUIRED
;
4711 if (ll
== IB_LINK_LAYER_INFINIBAND
)
4712 return ret
| RDMA_CORE_PORT_IBA_IB
;
4715 ret
|= RDMA_CORE_PORT_RAW_PACKET
;
4717 if (!(l3_type_cap
& MLX5_ROCE_L3_TYPE_IPV4_CAP
))
4720 if (!(l3_type_cap
& MLX5_ROCE_L3_TYPE_IPV6_CAP
))
4723 if (roce_version_cap
& MLX5_ROCE_VERSION_1_CAP
)
4724 ret
|= RDMA_CORE_PORT_IBA_ROCE
;
4726 if (roce_version_cap
& MLX5_ROCE_VERSION_2_CAP
)
4727 ret
|= RDMA_CORE_PORT_IBA_ROCE_UDP_ENCAP
;
4732 static int mlx5_port_immutable(struct ib_device
*ibdev
, u8 port_num
,
4733 struct ib_port_immutable
*immutable
)
4735 struct ib_port_attr attr
;
4736 struct mlx5_ib_dev
*dev
= to_mdev(ibdev
);
4737 enum rdma_link_layer ll
= mlx5_ib_port_link_layer(ibdev
, port_num
);
4738 struct mlx5_hca_vport_context rep
= {0};
4741 err
= ib_query_port(ibdev
, port_num
, &attr
);
4745 if (ll
== IB_LINK_LAYER_INFINIBAND
) {
4746 err
= mlx5_query_hca_vport_context(dev
->mdev
, 0, port_num
, 0,
4752 immutable
->pkey_tbl_len
= attr
.pkey_tbl_len
;
4753 immutable
->gid_tbl_len
= attr
.gid_tbl_len
;
4754 immutable
->core_cap_flags
= get_core_cap_flags(ibdev
, &rep
);
4755 if ((ll
== IB_LINK_LAYER_INFINIBAND
) || MLX5_CAP_GEN(dev
->mdev
, roce
))
4756 immutable
->max_mad_size
= IB_MGMT_MAD_SIZE
;
4761 static int mlx5_port_rep_immutable(struct ib_device
*ibdev
, u8 port_num
,
4762 struct ib_port_immutable
*immutable
)
4764 struct ib_port_attr attr
;
4767 immutable
->core_cap_flags
= RDMA_CORE_PORT_RAW_PACKET
;
4769 err
= ib_query_port(ibdev
, port_num
, &attr
);
4773 immutable
->pkey_tbl_len
= attr
.pkey_tbl_len
;
4774 immutable
->gid_tbl_len
= attr
.gid_tbl_len
;
4775 immutable
->core_cap_flags
= RDMA_CORE_PORT_RAW_PACKET
;
4780 static void get_dev_fw_str(struct ib_device
*ibdev
, char *str
)
4782 struct mlx5_ib_dev
*dev
=
4783 container_of(ibdev
, struct mlx5_ib_dev
, ib_dev
);
4784 snprintf(str
, IB_FW_VERSION_NAME_MAX
, "%d.%d.%04d",
4785 fw_rev_maj(dev
->mdev
), fw_rev_min(dev
->mdev
),
4786 fw_rev_sub(dev
->mdev
));
4789 static int mlx5_eth_lag_init(struct mlx5_ib_dev
*dev
)
4791 struct mlx5_core_dev
*mdev
= dev
->mdev
;
4792 struct mlx5_flow_namespace
*ns
= mlx5_get_flow_namespace(mdev
,
4793 MLX5_FLOW_NAMESPACE_LAG
);
4794 struct mlx5_flow_table
*ft
;
4797 if (!ns
|| !mlx5_lag_is_active(mdev
))
4800 err
= mlx5_cmd_create_vport_lag(mdev
);
4804 ft
= mlx5_create_lag_demux_flow_table(ns
, 0, 0);
4807 goto err_destroy_vport_lag
;
4810 dev
->flow_db
->lag_demux_ft
= ft
;
4813 err_destroy_vport_lag
:
4814 mlx5_cmd_destroy_vport_lag(mdev
);
4818 static void mlx5_eth_lag_cleanup(struct mlx5_ib_dev
*dev
)
4820 struct mlx5_core_dev
*mdev
= dev
->mdev
;
4822 if (dev
->flow_db
->lag_demux_ft
) {
4823 mlx5_destroy_flow_table(dev
->flow_db
->lag_demux_ft
);
4824 dev
->flow_db
->lag_demux_ft
= NULL
;
4826 mlx5_cmd_destroy_vport_lag(mdev
);
4830 static int mlx5_add_netdev_notifier(struct mlx5_ib_dev
*dev
, u8 port_num
)
4834 dev
->roce
[port_num
].nb
.notifier_call
= mlx5_netdev_event
;
4835 err
= register_netdevice_notifier(&dev
->roce
[port_num
].nb
);
4837 dev
->roce
[port_num
].nb
.notifier_call
= NULL
;
4844 static void mlx5_remove_netdev_notifier(struct mlx5_ib_dev
*dev
, u8 port_num
)
4846 if (dev
->roce
[port_num
].nb
.notifier_call
) {
4847 unregister_netdevice_notifier(&dev
->roce
[port_num
].nb
);
4848 dev
->roce
[port_num
].nb
.notifier_call
= NULL
;
4852 static int mlx5_enable_eth(struct mlx5_ib_dev
*dev
)
4856 if (MLX5_CAP_GEN(dev
->mdev
, roce
)) {
4857 err
= mlx5_nic_vport_enable_roce(dev
->mdev
);
4862 err
= mlx5_eth_lag_init(dev
);
4864 goto err_disable_roce
;
4869 if (MLX5_CAP_GEN(dev
->mdev
, roce
))
4870 mlx5_nic_vport_disable_roce(dev
->mdev
);
4875 static void mlx5_disable_eth(struct mlx5_ib_dev
*dev
)
4877 mlx5_eth_lag_cleanup(dev
);
4878 if (MLX5_CAP_GEN(dev
->mdev
, roce
))
4879 mlx5_nic_vport_disable_roce(dev
->mdev
);
4882 struct mlx5_ib_counter
{
4887 #define INIT_Q_COUNTER(_name) \
4888 { .name = #_name, .offset = MLX5_BYTE_OFF(query_q_counter_out, _name)}
4890 static const struct mlx5_ib_counter basic_q_cnts
[] = {
4891 INIT_Q_COUNTER(rx_write_requests
),
4892 INIT_Q_COUNTER(rx_read_requests
),
4893 INIT_Q_COUNTER(rx_atomic_requests
),
4894 INIT_Q_COUNTER(out_of_buffer
),
4897 static const struct mlx5_ib_counter out_of_seq_q_cnts
[] = {
4898 INIT_Q_COUNTER(out_of_sequence
),
4901 static const struct mlx5_ib_counter retrans_q_cnts
[] = {
4902 INIT_Q_COUNTER(duplicate_request
),
4903 INIT_Q_COUNTER(rnr_nak_retry_err
),
4904 INIT_Q_COUNTER(packet_seq_err
),
4905 INIT_Q_COUNTER(implied_nak_seq_err
),
4906 INIT_Q_COUNTER(local_ack_timeout_err
),
4909 #define INIT_CONG_COUNTER(_name) \
4910 { .name = #_name, .offset = \
4911 MLX5_BYTE_OFF(query_cong_statistics_out, _name ## _high)}
4913 static const struct mlx5_ib_counter cong_cnts
[] = {
4914 INIT_CONG_COUNTER(rp_cnp_ignored
),
4915 INIT_CONG_COUNTER(rp_cnp_handled
),
4916 INIT_CONG_COUNTER(np_ecn_marked_roce_packets
),
4917 INIT_CONG_COUNTER(np_cnp_sent
),
4920 static const struct mlx5_ib_counter extended_err_cnts
[] = {
4921 INIT_Q_COUNTER(resp_local_length_error
),
4922 INIT_Q_COUNTER(resp_cqe_error
),
4923 INIT_Q_COUNTER(req_cqe_error
),
4924 INIT_Q_COUNTER(req_remote_invalid_request
),
4925 INIT_Q_COUNTER(req_remote_access_errors
),
4926 INIT_Q_COUNTER(resp_remote_access_errors
),
4927 INIT_Q_COUNTER(resp_cqe_flush_error
),
4928 INIT_Q_COUNTER(req_cqe_flush_error
),
4931 #define INIT_EXT_PPCNT_COUNTER(_name) \
4932 { .name = #_name, .offset = \
4933 MLX5_BYTE_OFF(ppcnt_reg, \
4934 counter_set.eth_extended_cntrs_grp_data_layout._name##_high)}
4936 static const struct mlx5_ib_counter ext_ppcnt_cnts
[] = {
4937 INIT_EXT_PPCNT_COUNTER(rx_icrc_encapsulated
),
4940 static void mlx5_ib_dealloc_counters(struct mlx5_ib_dev
*dev
)
4944 for (i
= 0; i
< dev
->num_ports
; i
++) {
4945 if (dev
->port
[i
].cnts
.set_id_valid
)
4946 mlx5_core_dealloc_q_counter(dev
->mdev
,
4947 dev
->port
[i
].cnts
.set_id
);
4948 kfree(dev
->port
[i
].cnts
.names
);
4949 kfree(dev
->port
[i
].cnts
.offsets
);
4953 static int __mlx5_ib_alloc_counters(struct mlx5_ib_dev
*dev
,
4954 struct mlx5_ib_counters
*cnts
)
4958 num_counters
= ARRAY_SIZE(basic_q_cnts
);
4960 if (MLX5_CAP_GEN(dev
->mdev
, out_of_seq_cnt
))
4961 num_counters
+= ARRAY_SIZE(out_of_seq_q_cnts
);
4963 if (MLX5_CAP_GEN(dev
->mdev
, retransmission_q_counters
))
4964 num_counters
+= ARRAY_SIZE(retrans_q_cnts
);
4966 if (MLX5_CAP_GEN(dev
->mdev
, enhanced_error_q_counters
))
4967 num_counters
+= ARRAY_SIZE(extended_err_cnts
);
4969 cnts
->num_q_counters
= num_counters
;
4971 if (MLX5_CAP_GEN(dev
->mdev
, cc_query_allowed
)) {
4972 cnts
->num_cong_counters
= ARRAY_SIZE(cong_cnts
);
4973 num_counters
+= ARRAY_SIZE(cong_cnts
);
4975 if (MLX5_CAP_PCAM_FEATURE(dev
->mdev
, rx_icrc_encapsulated_counter
)) {
4976 cnts
->num_ext_ppcnt_counters
= ARRAY_SIZE(ext_ppcnt_cnts
);
4977 num_counters
+= ARRAY_SIZE(ext_ppcnt_cnts
);
4979 cnts
->names
= kcalloc(num_counters
, sizeof(cnts
->names
), GFP_KERNEL
);
4983 cnts
->offsets
= kcalloc(num_counters
,
4984 sizeof(cnts
->offsets
), GFP_KERNEL
);
4996 static void mlx5_ib_fill_counters(struct mlx5_ib_dev
*dev
,
5003 for (i
= 0; i
< ARRAY_SIZE(basic_q_cnts
); i
++, j
++) {
5004 names
[j
] = basic_q_cnts
[i
].name
;
5005 offsets
[j
] = basic_q_cnts
[i
].offset
;
5008 if (MLX5_CAP_GEN(dev
->mdev
, out_of_seq_cnt
)) {
5009 for (i
= 0; i
< ARRAY_SIZE(out_of_seq_q_cnts
); i
++, j
++) {
5010 names
[j
] = out_of_seq_q_cnts
[i
].name
;
5011 offsets
[j
] = out_of_seq_q_cnts
[i
].offset
;
5015 if (MLX5_CAP_GEN(dev
->mdev
, retransmission_q_counters
)) {
5016 for (i
= 0; i
< ARRAY_SIZE(retrans_q_cnts
); i
++, j
++) {
5017 names
[j
] = retrans_q_cnts
[i
].name
;
5018 offsets
[j
] = retrans_q_cnts
[i
].offset
;
5022 if (MLX5_CAP_GEN(dev
->mdev
, enhanced_error_q_counters
)) {
5023 for (i
= 0; i
< ARRAY_SIZE(extended_err_cnts
); i
++, j
++) {
5024 names
[j
] = extended_err_cnts
[i
].name
;
5025 offsets
[j
] = extended_err_cnts
[i
].offset
;
5029 if (MLX5_CAP_GEN(dev
->mdev
, cc_query_allowed
)) {
5030 for (i
= 0; i
< ARRAY_SIZE(cong_cnts
); i
++, j
++) {
5031 names
[j
] = cong_cnts
[i
].name
;
5032 offsets
[j
] = cong_cnts
[i
].offset
;
5036 if (MLX5_CAP_PCAM_FEATURE(dev
->mdev
, rx_icrc_encapsulated_counter
)) {
5037 for (i
= 0; i
< ARRAY_SIZE(ext_ppcnt_cnts
); i
++, j
++) {
5038 names
[j
] = ext_ppcnt_cnts
[i
].name
;
5039 offsets
[j
] = ext_ppcnt_cnts
[i
].offset
;
5044 static int mlx5_ib_alloc_counters(struct mlx5_ib_dev
*dev
)
5049 for (i
= 0; i
< dev
->num_ports
; i
++) {
5050 err
= __mlx5_ib_alloc_counters(dev
, &dev
->port
[i
].cnts
);
5054 mlx5_ib_fill_counters(dev
, dev
->port
[i
].cnts
.names
,
5055 dev
->port
[i
].cnts
.offsets
);
5057 err
= mlx5_core_alloc_q_counter(dev
->mdev
,
5058 &dev
->port
[i
].cnts
.set_id
);
5061 "couldn't allocate queue counter for port %d, err %d\n",
5065 dev
->port
[i
].cnts
.set_id_valid
= true;
5071 mlx5_ib_dealloc_counters(dev
);
5075 static struct rdma_hw_stats
*mlx5_ib_alloc_hw_stats(struct ib_device
*ibdev
,
5078 struct mlx5_ib_dev
*dev
= to_mdev(ibdev
);
5079 struct mlx5_ib_port
*port
= &dev
->port
[port_num
- 1];
5081 /* We support only per port stats */
5085 return rdma_alloc_hw_stats_struct(port
->cnts
.names
,
5086 port
->cnts
.num_q_counters
+
5087 port
->cnts
.num_cong_counters
+
5088 port
->cnts
.num_ext_ppcnt_counters
,
5089 RDMA_HW_STATS_DEFAULT_LIFESPAN
);
5092 static int mlx5_ib_query_q_counters(struct mlx5_core_dev
*mdev
,
5093 struct mlx5_ib_port
*port
,
5094 struct rdma_hw_stats
*stats
)
5096 int outlen
= MLX5_ST_SZ_BYTES(query_q_counter_out
);
5101 out
= kvzalloc(outlen
, GFP_KERNEL
);
5105 ret
= mlx5_core_query_q_counter(mdev
,
5106 port
->cnts
.set_id
, 0,
5111 for (i
= 0; i
< port
->cnts
.num_q_counters
; i
++) {
5112 val
= *(__be32
*)(out
+ port
->cnts
.offsets
[i
]);
5113 stats
->value
[i
] = (u64
)be32_to_cpu(val
);
5121 static int mlx5_ib_query_ext_ppcnt_counters(struct mlx5_ib_dev
*dev
,
5122 struct mlx5_ib_port
*port
,
5123 struct rdma_hw_stats
*stats
)
5125 int offset
= port
->cnts
.num_q_counters
+ port
->cnts
.num_cong_counters
;
5126 int sz
= MLX5_ST_SZ_BYTES(ppcnt_reg
);
5130 out
= kvzalloc(sz
, GFP_KERNEL
);
5134 ret
= mlx5_cmd_query_ext_ppcnt_counters(dev
->mdev
, out
);
5138 for (i
= 0; i
< port
->cnts
.num_ext_ppcnt_counters
; i
++) {
5139 stats
->value
[i
+ offset
] =
5140 be64_to_cpup((__be64
*)(out
+
5141 port
->cnts
.offsets
[i
+ offset
]));
5149 static int mlx5_ib_get_hw_stats(struct ib_device
*ibdev
,
5150 struct rdma_hw_stats
*stats
,
5151 u8 port_num
, int index
)
5153 struct mlx5_ib_dev
*dev
= to_mdev(ibdev
);
5154 struct mlx5_ib_port
*port
= &dev
->port
[port_num
- 1];
5155 struct mlx5_core_dev
*mdev
;
5156 int ret
, num_counters
;
5162 num_counters
= port
->cnts
.num_q_counters
+
5163 port
->cnts
.num_cong_counters
+
5164 port
->cnts
.num_ext_ppcnt_counters
;
5166 /* q_counters are per IB device, query the master mdev */
5167 ret
= mlx5_ib_query_q_counters(dev
->mdev
, port
, stats
);
5171 if (MLX5_CAP_PCAM_FEATURE(dev
->mdev
, rx_icrc_encapsulated_counter
)) {
5172 ret
= mlx5_ib_query_ext_ppcnt_counters(dev
, port
, stats
);
5177 if (MLX5_CAP_GEN(dev
->mdev
, cc_query_allowed
)) {
5178 mdev
= mlx5_ib_get_native_port_mdev(dev
, port_num
,
5181 /* If port is not affiliated yet, its in down state
5182 * which doesn't have any counters yet, so it would be
5183 * zero. So no need to read from the HCA.
5187 ret
= mlx5_lag_query_cong_counters(dev
->mdev
,
5189 port
->cnts
.num_q_counters
,
5190 port
->cnts
.num_cong_counters
,
5191 port
->cnts
.offsets
+
5192 port
->cnts
.num_q_counters
);
5194 mlx5_ib_put_native_port_mdev(dev
, port_num
);
5200 return num_counters
;
5203 static struct net_device
*
5204 mlx5_ib_alloc_rdma_netdev(struct ib_device
*hca
,
5206 enum rdma_netdev_t type
,
5208 unsigned char name_assign_type
,
5209 void (*setup
)(struct net_device
*))
5211 struct net_device
*netdev
;
5213 if (type
!= RDMA_NETDEV_IPOIB
)
5214 return ERR_PTR(-EOPNOTSUPP
);
5216 netdev
= mlx5_rdma_netdev_alloc(to_mdev(hca
)->mdev
, hca
,
5221 static void delay_drop_debugfs_cleanup(struct mlx5_ib_dev
*dev
)
5223 if (!dev
->delay_drop
.dbg
)
5225 debugfs_remove_recursive(dev
->delay_drop
.dbg
->dir_debugfs
);
5226 kfree(dev
->delay_drop
.dbg
);
5227 dev
->delay_drop
.dbg
= NULL
;
5230 static void cancel_delay_drop(struct mlx5_ib_dev
*dev
)
5232 if (!(dev
->ib_dev
.attrs
.raw_packet_caps
& IB_RAW_PACKET_CAP_DELAY_DROP
))
5235 cancel_work_sync(&dev
->delay_drop
.delay_drop_work
);
5236 delay_drop_debugfs_cleanup(dev
);
5239 static ssize_t
delay_drop_timeout_read(struct file
*filp
, char __user
*buf
,
5240 size_t count
, loff_t
*pos
)
5242 struct mlx5_ib_delay_drop
*delay_drop
= filp
->private_data
;
5246 len
= snprintf(lbuf
, sizeof(lbuf
), "%u\n", delay_drop
->timeout
);
5247 return simple_read_from_buffer(buf
, count
, pos
, lbuf
, len
);
5250 static ssize_t
delay_drop_timeout_write(struct file
*filp
, const char __user
*buf
,
5251 size_t count
, loff_t
*pos
)
5253 struct mlx5_ib_delay_drop
*delay_drop
= filp
->private_data
;
5257 if (kstrtouint_from_user(buf
, count
, 0, &var
))
5260 timeout
= min_t(u32
, roundup(var
, 100), MLX5_MAX_DELAY_DROP_TIMEOUT_MS
*
5263 mlx5_ib_dbg(delay_drop
->dev
, "Round delay drop timeout to %u usec\n",
5266 delay_drop
->timeout
= timeout
;
5271 static const struct file_operations fops_delay_drop_timeout
= {
5272 .owner
= THIS_MODULE
,
5273 .open
= simple_open
,
5274 .write
= delay_drop_timeout_write
,
5275 .read
= delay_drop_timeout_read
,
5278 static int delay_drop_debugfs_init(struct mlx5_ib_dev
*dev
)
5280 struct mlx5_ib_dbg_delay_drop
*dbg
;
5282 if (!mlx5_debugfs_root
)
5285 dbg
= kzalloc(sizeof(*dbg
), GFP_KERNEL
);
5289 dev
->delay_drop
.dbg
= dbg
;
5292 debugfs_create_dir("delay_drop",
5293 dev
->mdev
->priv
.dbg_root
);
5294 if (!dbg
->dir_debugfs
)
5297 dbg
->events_cnt_debugfs
=
5298 debugfs_create_atomic_t("num_timeout_events", 0400,
5300 &dev
->delay_drop
.events_cnt
);
5301 if (!dbg
->events_cnt_debugfs
)
5304 dbg
->rqs_cnt_debugfs
=
5305 debugfs_create_atomic_t("num_rqs", 0400,
5307 &dev
->delay_drop
.rqs_cnt
);
5308 if (!dbg
->rqs_cnt_debugfs
)
5311 dbg
->timeout_debugfs
=
5312 debugfs_create_file("timeout", 0600,
5315 &fops_delay_drop_timeout
);
5316 if (!dbg
->timeout_debugfs
)
5322 delay_drop_debugfs_cleanup(dev
);
5326 static void init_delay_drop(struct mlx5_ib_dev
*dev
)
5328 if (!(dev
->ib_dev
.attrs
.raw_packet_caps
& IB_RAW_PACKET_CAP_DELAY_DROP
))
5331 mutex_init(&dev
->delay_drop
.lock
);
5332 dev
->delay_drop
.dev
= dev
;
5333 dev
->delay_drop
.activate
= false;
5334 dev
->delay_drop
.timeout
= MLX5_MAX_DELAY_DROP_TIMEOUT_MS
* 1000;
5335 INIT_WORK(&dev
->delay_drop
.delay_drop_work
, delay_drop_handler
);
5336 atomic_set(&dev
->delay_drop
.rqs_cnt
, 0);
5337 atomic_set(&dev
->delay_drop
.events_cnt
, 0);
5339 if (delay_drop_debugfs_init(dev
))
5340 mlx5_ib_warn(dev
, "Failed to init delay drop debugfs\n");
5343 static const struct cpumask
*
5344 mlx5_ib_get_vector_affinity(struct ib_device
*ibdev
, int comp_vector
)
5346 struct mlx5_ib_dev
*dev
= to_mdev(ibdev
);
5348 return mlx5_get_vector_affinity_hint(dev
->mdev
, comp_vector
);
5351 /* The mlx5_ib_multiport_mutex should be held when calling this function */
5352 static void mlx5_ib_unbind_slave_port(struct mlx5_ib_dev
*ibdev
,
5353 struct mlx5_ib_multiport_info
*mpi
)
5355 u8 port_num
= mlx5_core_native_port_num(mpi
->mdev
) - 1;
5356 struct mlx5_ib_port
*port
= &ibdev
->port
[port_num
];
5361 mlx5_ib_cleanup_cong_debugfs(ibdev
, port_num
);
5363 spin_lock(&port
->mp
.mpi_lock
);
5365 spin_unlock(&port
->mp
.mpi_lock
);
5370 spin_unlock(&port
->mp
.mpi_lock
);
5371 mlx5_remove_netdev_notifier(ibdev
, port_num
);
5372 spin_lock(&port
->mp
.mpi_lock
);
5374 comps
= mpi
->mdev_refcnt
;
5376 mpi
->unaffiliate
= true;
5377 init_completion(&mpi
->unref_comp
);
5378 spin_unlock(&port
->mp
.mpi_lock
);
5380 for (i
= 0; i
< comps
; i
++)
5381 wait_for_completion(&mpi
->unref_comp
);
5383 spin_lock(&port
->mp
.mpi_lock
);
5384 mpi
->unaffiliate
= false;
5387 port
->mp
.mpi
= NULL
;
5389 list_add_tail(&mpi
->list
, &mlx5_ib_unaffiliated_port_list
);
5391 spin_unlock(&port
->mp
.mpi_lock
);
5393 err
= mlx5_nic_vport_unaffiliate_multiport(mpi
->mdev
);
5395 mlx5_ib_dbg(ibdev
, "unaffiliated port %d\n", port_num
+ 1);
5396 /* Log an error, still needed to cleanup the pointers and add
5397 * it back to the list.
5400 mlx5_ib_err(ibdev
, "Failed to unaffiliate port %u\n",
5403 ibdev
->roce
[port_num
].last_port_state
= IB_PORT_DOWN
;
5406 /* The mlx5_ib_multiport_mutex should be held when calling this function */
5407 static bool mlx5_ib_bind_slave_port(struct mlx5_ib_dev
*ibdev
,
5408 struct mlx5_ib_multiport_info
*mpi
)
5410 u8 port_num
= mlx5_core_native_port_num(mpi
->mdev
) - 1;
5413 spin_lock(&ibdev
->port
[port_num
].mp
.mpi_lock
);
5414 if (ibdev
->port
[port_num
].mp
.mpi
) {
5415 mlx5_ib_dbg(ibdev
, "port %d already affiliated.\n",
5417 spin_unlock(&ibdev
->port
[port_num
].mp
.mpi_lock
);
5421 ibdev
->port
[port_num
].mp
.mpi
= mpi
;
5423 spin_unlock(&ibdev
->port
[port_num
].mp
.mpi_lock
);
5425 err
= mlx5_nic_vport_affiliate_multiport(ibdev
->mdev
, mpi
->mdev
);
5429 err
= get_port_caps(ibdev
, mlx5_core_native_port_num(mpi
->mdev
));
5433 err
= mlx5_add_netdev_notifier(ibdev
, port_num
);
5435 mlx5_ib_err(ibdev
, "failed adding netdev notifier for port %u\n",
5440 err
= mlx5_ib_init_cong_debugfs(ibdev
, port_num
);
5447 mlx5_ib_unbind_slave_port(ibdev
, mpi
);
5451 static int mlx5_ib_init_multiport_master(struct mlx5_ib_dev
*dev
)
5453 int port_num
= mlx5_core_native_port_num(dev
->mdev
) - 1;
5454 enum rdma_link_layer ll
= mlx5_ib_port_link_layer(&dev
->ib_dev
,
5456 struct mlx5_ib_multiport_info
*mpi
;
5460 if (!mlx5_core_is_mp_master(dev
->mdev
) || ll
!= IB_LINK_LAYER_ETHERNET
)
5463 err
= mlx5_query_nic_vport_system_image_guid(dev
->mdev
,
5464 &dev
->sys_image_guid
);
5468 err
= mlx5_nic_vport_enable_roce(dev
->mdev
);
5472 mutex_lock(&mlx5_ib_multiport_mutex
);
5473 for (i
= 0; i
< dev
->num_ports
; i
++) {
5476 /* build a stub multiport info struct for the native port. */
5477 if (i
== port_num
) {
5478 mpi
= kzalloc(sizeof(*mpi
), GFP_KERNEL
);
5480 mutex_unlock(&mlx5_ib_multiport_mutex
);
5481 mlx5_nic_vport_disable_roce(dev
->mdev
);
5485 mpi
->is_master
= true;
5486 mpi
->mdev
= dev
->mdev
;
5487 mpi
->sys_image_guid
= dev
->sys_image_guid
;
5488 dev
->port
[i
].mp
.mpi
= mpi
;
5494 list_for_each_entry(mpi
, &mlx5_ib_unaffiliated_port_list
,
5496 if (dev
->sys_image_guid
== mpi
->sys_image_guid
&&
5497 (mlx5_core_native_port_num(mpi
->mdev
) - 1) == i
) {
5498 bound
= mlx5_ib_bind_slave_port(dev
, mpi
);
5502 dev_dbg(&mpi
->mdev
->pdev
->dev
, "removing port from unaffiliated list.\n");
5503 mlx5_ib_dbg(dev
, "port %d bound\n", i
+ 1);
5504 list_del(&mpi
->list
);
5509 get_port_caps(dev
, i
+ 1);
5510 mlx5_ib_dbg(dev
, "no free port found for port %d\n",
5515 list_add_tail(&dev
->ib_dev_list
, &mlx5_ib_dev_list
);
5516 mutex_unlock(&mlx5_ib_multiport_mutex
);
5520 static void mlx5_ib_cleanup_multiport_master(struct mlx5_ib_dev
*dev
)
5522 int port_num
= mlx5_core_native_port_num(dev
->mdev
) - 1;
5523 enum rdma_link_layer ll
= mlx5_ib_port_link_layer(&dev
->ib_dev
,
5527 if (!mlx5_core_is_mp_master(dev
->mdev
) || ll
!= IB_LINK_LAYER_ETHERNET
)
5530 mutex_lock(&mlx5_ib_multiport_mutex
);
5531 for (i
= 0; i
< dev
->num_ports
; i
++) {
5532 if (dev
->port
[i
].mp
.mpi
) {
5533 /* Destroy the native port stub */
5534 if (i
== port_num
) {
5535 kfree(dev
->port
[i
].mp
.mpi
);
5536 dev
->port
[i
].mp
.mpi
= NULL
;
5538 mlx5_ib_dbg(dev
, "unbinding port_num: %d\n", i
+ 1);
5539 mlx5_ib_unbind_slave_port(dev
, dev
->port
[i
].mp
.mpi
);
5544 mlx5_ib_dbg(dev
, "removing from devlist\n");
5545 list_del(&dev
->ib_dev_list
);
5546 mutex_unlock(&mlx5_ib_multiport_mutex
);
5548 mlx5_nic_vport_disable_roce(dev
->mdev
);
5551 ADD_UVERBS_ATTRIBUTES_SIMPLE(
5554 UVERBS_METHOD_DM_ALLOC
,
5555 UVERBS_ATTR_PTR_OUT(MLX5_IB_ATTR_ALLOC_DM_RESP_START_OFFSET
,
5556 UVERBS_ATTR_TYPE(u64
),
5558 UVERBS_ATTR_PTR_OUT(MLX5_IB_ATTR_ALLOC_DM_RESP_PAGE_INDEX
,
5559 UVERBS_ATTR_TYPE(u16
),
5562 ADD_UVERBS_ATTRIBUTES_SIMPLE(
5563 mlx5_ib_flow_action
,
5564 UVERBS_OBJECT_FLOW_ACTION
,
5565 UVERBS_METHOD_FLOW_ACTION_ESP_CREATE
,
5566 UVERBS_ATTR_FLAGS_IN(MLX5_IB_ATTR_CREATE_FLOW_ACTION_FLAGS
,
5567 enum mlx5_ib_uapi_flow_action_flags
));
5569 static int populate_specs_root(struct mlx5_ib_dev
*dev
)
5571 const struct uverbs_object_tree_def
**trees
= dev
->driver_trees
;
5572 size_t num_trees
= 0;
5574 if (mlx5_accel_ipsec_device_caps(dev
->mdev
) &
5575 MLX5_ACCEL_IPSEC_CAP_DEVICE
)
5576 trees
[num_trees
++] = &mlx5_ib_flow_action
;
5578 if (MLX5_CAP_DEV_MEM(dev
->mdev
, memic
))
5579 trees
[num_trees
++] = &mlx5_ib_dm
;
5581 if (MLX5_CAP_GEN_64(dev
->mdev
, general_obj_types
) &
5582 MLX5_GENERAL_OBJ_TYPES_CAP_UCTX
)
5583 trees
[num_trees
++] = mlx5_ib_get_devx_tree();
5585 num_trees
+= mlx5_ib_get_flow_trees(trees
+ num_trees
);
5587 WARN_ON(num_trees
>= ARRAY_SIZE(dev
->driver_trees
));
5588 trees
[num_trees
] = NULL
;
5589 dev
->ib_dev
.driver_specs
= trees
;
5594 static int mlx5_ib_read_counters(struct ib_counters
*counters
,
5595 struct ib_counters_read_attr
*read_attr
,
5596 struct uverbs_attr_bundle
*attrs
)
5598 struct mlx5_ib_mcounters
*mcounters
= to_mcounters(counters
);
5599 struct mlx5_read_counters_attr mread_attr
= {};
5600 struct mlx5_ib_flow_counters_desc
*desc
;
5603 mutex_lock(&mcounters
->mcntrs_mutex
);
5604 if (mcounters
->cntrs_max_index
> read_attr
->ncounters
) {
5609 mread_attr
.out
= kcalloc(mcounters
->counters_num
, sizeof(u64
),
5611 if (!mread_attr
.out
) {
5616 mread_attr
.hw_cntrs_hndl
= mcounters
->hw_cntrs_hndl
;
5617 mread_attr
.flags
= read_attr
->flags
;
5618 ret
= mcounters
->read_counters(counters
->device
, &mread_attr
);
5622 /* do the pass over the counters data array to assign according to the
5623 * descriptions and indexing pairs
5625 desc
= mcounters
->counters_data
;
5626 for (i
= 0; i
< mcounters
->ncounters
; i
++)
5627 read_attr
->counters_buff
[desc
[i
].index
] += mread_attr
.out
[desc
[i
].description
];
5630 kfree(mread_attr
.out
);
5632 mutex_unlock(&mcounters
->mcntrs_mutex
);
5636 static int mlx5_ib_destroy_counters(struct ib_counters
*counters
)
5638 struct mlx5_ib_mcounters
*mcounters
= to_mcounters(counters
);
5640 counters_clear_description(counters
);
5641 if (mcounters
->hw_cntrs_hndl
)
5642 mlx5_fc_destroy(to_mdev(counters
->device
)->mdev
,
5643 mcounters
->hw_cntrs_hndl
);
5650 static struct ib_counters
*mlx5_ib_create_counters(struct ib_device
*device
,
5651 struct uverbs_attr_bundle
*attrs
)
5653 struct mlx5_ib_mcounters
*mcounters
;
5655 mcounters
= kzalloc(sizeof(*mcounters
), GFP_KERNEL
);
5657 return ERR_PTR(-ENOMEM
);
5659 mutex_init(&mcounters
->mcntrs_mutex
);
5661 return &mcounters
->ibcntrs
;
5664 void mlx5_ib_stage_init_cleanup(struct mlx5_ib_dev
*dev
)
5666 mlx5_ib_cleanup_multiport_master(dev
);
5667 #ifdef CONFIG_INFINIBAND_ON_DEMAND_PAGING
5668 cleanup_srcu_struct(&dev
->mr_srcu
);
5673 int mlx5_ib_stage_init_init(struct mlx5_ib_dev
*dev
)
5675 struct mlx5_core_dev
*mdev
= dev
->mdev
;
5680 dev
->port
= kcalloc(dev
->num_ports
, sizeof(*dev
->port
),
5685 for (i
= 0; i
< dev
->num_ports
; i
++) {
5686 spin_lock_init(&dev
->port
[i
].mp
.mpi_lock
);
5687 rwlock_init(&dev
->roce
[i
].netdev_lock
);
5690 err
= mlx5_ib_init_multiport_master(dev
);
5694 if (!mlx5_core_mp_enabled(mdev
)) {
5695 for (i
= 1; i
<= dev
->num_ports
; i
++) {
5696 err
= get_port_caps(dev
, i
);
5701 err
= get_port_caps(dev
, mlx5_core_native_port_num(mdev
));
5706 if (mlx5_use_mad_ifc(dev
))
5707 get_ext_port_caps(dev
);
5709 if (!mlx5_lag_is_active(mdev
))
5712 name
= "mlx5_bond_%d";
5714 strlcpy(dev
->ib_dev
.name
, name
, IB_DEVICE_NAME_MAX
);
5715 dev
->ib_dev
.owner
= THIS_MODULE
;
5716 dev
->ib_dev
.node_type
= RDMA_NODE_IB_CA
;
5717 dev
->ib_dev
.local_dma_lkey
= 0 /* not supported for now */;
5718 dev
->ib_dev
.phys_port_cnt
= dev
->num_ports
;
5719 dev
->ib_dev
.num_comp_vectors
=
5720 dev
->mdev
->priv
.eq_table
.num_comp_vectors
;
5721 dev
->ib_dev
.dev
.parent
= &mdev
->pdev
->dev
;
5723 mutex_init(&dev
->cap_mask_mutex
);
5724 INIT_LIST_HEAD(&dev
->qp_list
);
5725 spin_lock_init(&dev
->reset_flow_resource_lock
);
5727 spin_lock_init(&dev
->memic
.memic_lock
);
5728 dev
->memic
.dev
= mdev
;
5730 #ifdef CONFIG_INFINIBAND_ON_DEMAND_PAGING
5731 err
= init_srcu_struct(&dev
->mr_srcu
);
5738 mlx5_ib_cleanup_multiport_master(dev
);
5746 static int mlx5_ib_stage_flow_db_init(struct mlx5_ib_dev
*dev
)
5748 dev
->flow_db
= kzalloc(sizeof(*dev
->flow_db
), GFP_KERNEL
);
5753 mutex_init(&dev
->flow_db
->lock
);
5758 int mlx5_ib_stage_rep_flow_db_init(struct mlx5_ib_dev
*dev
)
5760 struct mlx5_ib_dev
*nic_dev
;
5762 nic_dev
= mlx5_ib_get_uplink_ibdev(dev
->mdev
->priv
.eswitch
);
5767 dev
->flow_db
= nic_dev
->flow_db
;
5772 static void mlx5_ib_stage_flow_db_cleanup(struct mlx5_ib_dev
*dev
)
5774 kfree(dev
->flow_db
);
5777 int mlx5_ib_stage_caps_init(struct mlx5_ib_dev
*dev
)
5779 struct mlx5_core_dev
*mdev
= dev
->mdev
;
5782 dev
->ib_dev
.uverbs_abi_ver
= MLX5_IB_UVERBS_ABI_VERSION
;
5783 dev
->ib_dev
.uverbs_cmd_mask
=
5784 (1ull << IB_USER_VERBS_CMD_GET_CONTEXT
) |
5785 (1ull << IB_USER_VERBS_CMD_QUERY_DEVICE
) |
5786 (1ull << IB_USER_VERBS_CMD_QUERY_PORT
) |
5787 (1ull << IB_USER_VERBS_CMD_ALLOC_PD
) |
5788 (1ull << IB_USER_VERBS_CMD_DEALLOC_PD
) |
5789 (1ull << IB_USER_VERBS_CMD_CREATE_AH
) |
5790 (1ull << IB_USER_VERBS_CMD_DESTROY_AH
) |
5791 (1ull << IB_USER_VERBS_CMD_REG_MR
) |
5792 (1ull << IB_USER_VERBS_CMD_REREG_MR
) |
5793 (1ull << IB_USER_VERBS_CMD_DEREG_MR
) |
5794 (1ull << IB_USER_VERBS_CMD_CREATE_COMP_CHANNEL
) |
5795 (1ull << IB_USER_VERBS_CMD_CREATE_CQ
) |
5796 (1ull << IB_USER_VERBS_CMD_RESIZE_CQ
) |
5797 (1ull << IB_USER_VERBS_CMD_DESTROY_CQ
) |
5798 (1ull << IB_USER_VERBS_CMD_CREATE_QP
) |
5799 (1ull << IB_USER_VERBS_CMD_MODIFY_QP
) |
5800 (1ull << IB_USER_VERBS_CMD_QUERY_QP
) |
5801 (1ull << IB_USER_VERBS_CMD_DESTROY_QP
) |
5802 (1ull << IB_USER_VERBS_CMD_ATTACH_MCAST
) |
5803 (1ull << IB_USER_VERBS_CMD_DETACH_MCAST
) |
5804 (1ull << IB_USER_VERBS_CMD_CREATE_SRQ
) |
5805 (1ull << IB_USER_VERBS_CMD_MODIFY_SRQ
) |
5806 (1ull << IB_USER_VERBS_CMD_QUERY_SRQ
) |
5807 (1ull << IB_USER_VERBS_CMD_DESTROY_SRQ
) |
5808 (1ull << IB_USER_VERBS_CMD_CREATE_XSRQ
) |
5809 (1ull << IB_USER_VERBS_CMD_OPEN_QP
);
5810 dev
->ib_dev
.uverbs_ex_cmd_mask
=
5811 (1ull << IB_USER_VERBS_EX_CMD_QUERY_DEVICE
) |
5812 (1ull << IB_USER_VERBS_EX_CMD_CREATE_CQ
) |
5813 (1ull << IB_USER_VERBS_EX_CMD_CREATE_QP
) |
5814 (1ull << IB_USER_VERBS_EX_CMD_MODIFY_QP
) |
5815 (1ull << IB_USER_VERBS_EX_CMD_MODIFY_CQ
);
5817 dev
->ib_dev
.query_device
= mlx5_ib_query_device
;
5818 dev
->ib_dev
.get_link_layer
= mlx5_ib_port_link_layer
;
5819 dev
->ib_dev
.query_gid
= mlx5_ib_query_gid
;
5820 dev
->ib_dev
.add_gid
= mlx5_ib_add_gid
;
5821 dev
->ib_dev
.del_gid
= mlx5_ib_del_gid
;
5822 dev
->ib_dev
.query_pkey
= mlx5_ib_query_pkey
;
5823 dev
->ib_dev
.modify_device
= mlx5_ib_modify_device
;
5824 dev
->ib_dev
.modify_port
= mlx5_ib_modify_port
;
5825 dev
->ib_dev
.alloc_ucontext
= mlx5_ib_alloc_ucontext
;
5826 dev
->ib_dev
.dealloc_ucontext
= mlx5_ib_dealloc_ucontext
;
5827 dev
->ib_dev
.mmap
= mlx5_ib_mmap
;
5828 dev
->ib_dev
.alloc_pd
= mlx5_ib_alloc_pd
;
5829 dev
->ib_dev
.dealloc_pd
= mlx5_ib_dealloc_pd
;
5830 dev
->ib_dev
.create_ah
= mlx5_ib_create_ah
;
5831 dev
->ib_dev
.query_ah
= mlx5_ib_query_ah
;
5832 dev
->ib_dev
.destroy_ah
= mlx5_ib_destroy_ah
;
5833 dev
->ib_dev
.create_srq
= mlx5_ib_create_srq
;
5834 dev
->ib_dev
.modify_srq
= mlx5_ib_modify_srq
;
5835 dev
->ib_dev
.query_srq
= mlx5_ib_query_srq
;
5836 dev
->ib_dev
.destroy_srq
= mlx5_ib_destroy_srq
;
5837 dev
->ib_dev
.post_srq_recv
= mlx5_ib_post_srq_recv
;
5838 dev
->ib_dev
.create_qp
= mlx5_ib_create_qp
;
5839 dev
->ib_dev
.modify_qp
= mlx5_ib_modify_qp
;
5840 dev
->ib_dev
.query_qp
= mlx5_ib_query_qp
;
5841 dev
->ib_dev
.destroy_qp
= mlx5_ib_destroy_qp
;
5842 dev
->ib_dev
.drain_sq
= mlx5_ib_drain_sq
;
5843 dev
->ib_dev
.drain_rq
= mlx5_ib_drain_rq
;
5844 dev
->ib_dev
.post_send
= mlx5_ib_post_send
;
5845 dev
->ib_dev
.post_recv
= mlx5_ib_post_recv
;
5846 dev
->ib_dev
.create_cq
= mlx5_ib_create_cq
;
5847 dev
->ib_dev
.modify_cq
= mlx5_ib_modify_cq
;
5848 dev
->ib_dev
.resize_cq
= mlx5_ib_resize_cq
;
5849 dev
->ib_dev
.destroy_cq
= mlx5_ib_destroy_cq
;
5850 dev
->ib_dev
.poll_cq
= mlx5_ib_poll_cq
;
5851 dev
->ib_dev
.req_notify_cq
= mlx5_ib_arm_cq
;
5852 dev
->ib_dev
.get_dma_mr
= mlx5_ib_get_dma_mr
;
5853 dev
->ib_dev
.reg_user_mr
= mlx5_ib_reg_user_mr
;
5854 dev
->ib_dev
.rereg_user_mr
= mlx5_ib_rereg_user_mr
;
5855 dev
->ib_dev
.dereg_mr
= mlx5_ib_dereg_mr
;
5856 dev
->ib_dev
.attach_mcast
= mlx5_ib_mcg_attach
;
5857 dev
->ib_dev
.detach_mcast
= mlx5_ib_mcg_detach
;
5858 dev
->ib_dev
.process_mad
= mlx5_ib_process_mad
;
5859 dev
->ib_dev
.alloc_mr
= mlx5_ib_alloc_mr
;
5860 dev
->ib_dev
.map_mr_sg
= mlx5_ib_map_mr_sg
;
5861 dev
->ib_dev
.check_mr_status
= mlx5_ib_check_mr_status
;
5862 dev
->ib_dev
.get_dev_fw_str
= get_dev_fw_str
;
5863 dev
->ib_dev
.get_vector_affinity
= mlx5_ib_get_vector_affinity
;
5864 if (MLX5_CAP_GEN(mdev
, ipoib_enhanced_offloads
))
5865 dev
->ib_dev
.alloc_rdma_netdev
= mlx5_ib_alloc_rdma_netdev
;
5867 if (mlx5_core_is_pf(mdev
)) {
5868 dev
->ib_dev
.get_vf_config
= mlx5_ib_get_vf_config
;
5869 dev
->ib_dev
.set_vf_link_state
= mlx5_ib_set_vf_link_state
;
5870 dev
->ib_dev
.get_vf_stats
= mlx5_ib_get_vf_stats
;
5871 dev
->ib_dev
.set_vf_guid
= mlx5_ib_set_vf_guid
;
5874 dev
->ib_dev
.disassociate_ucontext
= mlx5_ib_disassociate_ucontext
;
5876 dev
->umr_fence
= mlx5_get_umr_fence(MLX5_CAP_GEN(mdev
, umr_fence
));
5878 if (MLX5_CAP_GEN(mdev
, imaicl
)) {
5879 dev
->ib_dev
.alloc_mw
= mlx5_ib_alloc_mw
;
5880 dev
->ib_dev
.dealloc_mw
= mlx5_ib_dealloc_mw
;
5881 dev
->ib_dev
.uverbs_cmd_mask
|=
5882 (1ull << IB_USER_VERBS_CMD_ALLOC_MW
) |
5883 (1ull << IB_USER_VERBS_CMD_DEALLOC_MW
);
5886 if (MLX5_CAP_GEN(mdev
, xrc
)) {
5887 dev
->ib_dev
.alloc_xrcd
= mlx5_ib_alloc_xrcd
;
5888 dev
->ib_dev
.dealloc_xrcd
= mlx5_ib_dealloc_xrcd
;
5889 dev
->ib_dev
.uverbs_cmd_mask
|=
5890 (1ull << IB_USER_VERBS_CMD_OPEN_XRCD
) |
5891 (1ull << IB_USER_VERBS_CMD_CLOSE_XRCD
);
5894 if (MLX5_CAP_DEV_MEM(mdev
, memic
)) {
5895 dev
->ib_dev
.alloc_dm
= mlx5_ib_alloc_dm
;
5896 dev
->ib_dev
.dealloc_dm
= mlx5_ib_dealloc_dm
;
5897 dev
->ib_dev
.reg_dm_mr
= mlx5_ib_reg_dm_mr
;
5900 dev
->ib_dev
.create_flow
= mlx5_ib_create_flow
;
5901 dev
->ib_dev
.destroy_flow
= mlx5_ib_destroy_flow
;
5902 dev
->ib_dev
.uverbs_ex_cmd_mask
|=
5903 (1ull << IB_USER_VERBS_EX_CMD_CREATE_FLOW
) |
5904 (1ull << IB_USER_VERBS_EX_CMD_DESTROY_FLOW
);
5905 dev
->ib_dev
.create_flow_action_esp
= mlx5_ib_create_flow_action_esp
;
5906 dev
->ib_dev
.destroy_flow_action
= mlx5_ib_destroy_flow_action
;
5907 dev
->ib_dev
.modify_flow_action_esp
= mlx5_ib_modify_flow_action_esp
;
5908 dev
->ib_dev
.driver_id
= RDMA_DRIVER_MLX5
;
5909 dev
->ib_dev
.create_counters
= mlx5_ib_create_counters
;
5910 dev
->ib_dev
.destroy_counters
= mlx5_ib_destroy_counters
;
5911 dev
->ib_dev
.read_counters
= mlx5_ib_read_counters
;
5913 err
= init_node_data(dev
);
5917 if ((MLX5_CAP_GEN(dev
->mdev
, port_type
) == MLX5_CAP_PORT_TYPE_ETH
) &&
5918 (MLX5_CAP_GEN(dev
->mdev
, disable_local_lb_uc
) ||
5919 MLX5_CAP_GEN(dev
->mdev
, disable_local_lb_mc
)))
5920 mutex_init(&dev
->lb_mutex
);
5925 static int mlx5_ib_stage_non_default_cb(struct mlx5_ib_dev
*dev
)
5927 dev
->ib_dev
.get_port_immutable
= mlx5_port_immutable
;
5928 dev
->ib_dev
.query_port
= mlx5_ib_query_port
;
5933 int mlx5_ib_stage_rep_non_default_cb(struct mlx5_ib_dev
*dev
)
5935 dev
->ib_dev
.get_port_immutable
= mlx5_port_rep_immutable
;
5936 dev
->ib_dev
.query_port
= mlx5_ib_rep_query_port
;
5941 static int mlx5_ib_stage_common_roce_init(struct mlx5_ib_dev
*dev
)
5946 for (i
= 0; i
< dev
->num_ports
; i
++) {
5947 dev
->roce
[i
].dev
= dev
;
5948 dev
->roce
[i
].native_port_num
= i
+ 1;
5949 dev
->roce
[i
].last_port_state
= IB_PORT_DOWN
;
5952 dev
->ib_dev
.get_netdev
= mlx5_ib_get_netdev
;
5953 dev
->ib_dev
.create_wq
= mlx5_ib_create_wq
;
5954 dev
->ib_dev
.modify_wq
= mlx5_ib_modify_wq
;
5955 dev
->ib_dev
.destroy_wq
= mlx5_ib_destroy_wq
;
5956 dev
->ib_dev
.create_rwq_ind_table
= mlx5_ib_create_rwq_ind_table
;
5957 dev
->ib_dev
.destroy_rwq_ind_table
= mlx5_ib_destroy_rwq_ind_table
;
5959 dev
->ib_dev
.uverbs_ex_cmd_mask
|=
5960 (1ull << IB_USER_VERBS_EX_CMD_CREATE_WQ
) |
5961 (1ull << IB_USER_VERBS_EX_CMD_MODIFY_WQ
) |
5962 (1ull << IB_USER_VERBS_EX_CMD_DESTROY_WQ
) |
5963 (1ull << IB_USER_VERBS_EX_CMD_CREATE_RWQ_IND_TBL
) |
5964 (1ull << IB_USER_VERBS_EX_CMD_DESTROY_RWQ_IND_TBL
);
5966 port_num
= mlx5_core_native_port_num(dev
->mdev
) - 1;
5968 return mlx5_add_netdev_notifier(dev
, port_num
);
5971 static void mlx5_ib_stage_common_roce_cleanup(struct mlx5_ib_dev
*dev
)
5973 u8 port_num
= mlx5_core_native_port_num(dev
->mdev
) - 1;
5975 mlx5_remove_netdev_notifier(dev
, port_num
);
5978 int mlx5_ib_stage_rep_roce_init(struct mlx5_ib_dev
*dev
)
5980 struct mlx5_core_dev
*mdev
= dev
->mdev
;
5981 enum rdma_link_layer ll
;
5985 port_type_cap
= MLX5_CAP_GEN(mdev
, port_type
);
5986 ll
= mlx5_port_type_cap_to_rdma_ll(port_type_cap
);
5988 if (ll
== IB_LINK_LAYER_ETHERNET
)
5989 err
= mlx5_ib_stage_common_roce_init(dev
);
5994 void mlx5_ib_stage_rep_roce_cleanup(struct mlx5_ib_dev
*dev
)
5996 mlx5_ib_stage_common_roce_cleanup(dev
);
5999 static int mlx5_ib_stage_roce_init(struct mlx5_ib_dev
*dev
)
6001 struct mlx5_core_dev
*mdev
= dev
->mdev
;
6002 enum rdma_link_layer ll
;
6006 port_type_cap
= MLX5_CAP_GEN(mdev
, port_type
);
6007 ll
= mlx5_port_type_cap_to_rdma_ll(port_type_cap
);
6009 if (ll
== IB_LINK_LAYER_ETHERNET
) {
6010 err
= mlx5_ib_stage_common_roce_init(dev
);
6014 err
= mlx5_enable_eth(dev
);
6021 mlx5_ib_stage_common_roce_cleanup(dev
);
6026 static void mlx5_ib_stage_roce_cleanup(struct mlx5_ib_dev
*dev
)
6028 struct mlx5_core_dev
*mdev
= dev
->mdev
;
6029 enum rdma_link_layer ll
;
6032 port_type_cap
= MLX5_CAP_GEN(mdev
, port_type
);
6033 ll
= mlx5_port_type_cap_to_rdma_ll(port_type_cap
);
6035 if (ll
== IB_LINK_LAYER_ETHERNET
) {
6036 mlx5_disable_eth(dev
);
6037 mlx5_ib_stage_common_roce_cleanup(dev
);
6041 int mlx5_ib_stage_dev_res_init(struct mlx5_ib_dev
*dev
)
6043 return create_dev_resources(&dev
->devr
);
6046 void mlx5_ib_stage_dev_res_cleanup(struct mlx5_ib_dev
*dev
)
6048 destroy_dev_resources(&dev
->devr
);
6051 static int mlx5_ib_stage_odp_init(struct mlx5_ib_dev
*dev
)
6053 mlx5_ib_internal_fill_odp_caps(dev
);
6055 return mlx5_ib_odp_init_one(dev
);
6058 int mlx5_ib_stage_counters_init(struct mlx5_ib_dev
*dev
)
6060 if (MLX5_CAP_GEN(dev
->mdev
, max_qp_cnt
)) {
6061 dev
->ib_dev
.get_hw_stats
= mlx5_ib_get_hw_stats
;
6062 dev
->ib_dev
.alloc_hw_stats
= mlx5_ib_alloc_hw_stats
;
6064 return mlx5_ib_alloc_counters(dev
);
6070 void mlx5_ib_stage_counters_cleanup(struct mlx5_ib_dev
*dev
)
6072 if (MLX5_CAP_GEN(dev
->mdev
, max_qp_cnt
))
6073 mlx5_ib_dealloc_counters(dev
);
6076 static int mlx5_ib_stage_cong_debugfs_init(struct mlx5_ib_dev
*dev
)
6078 return mlx5_ib_init_cong_debugfs(dev
,
6079 mlx5_core_native_port_num(dev
->mdev
) - 1);
6082 static void mlx5_ib_stage_cong_debugfs_cleanup(struct mlx5_ib_dev
*dev
)
6084 mlx5_ib_cleanup_cong_debugfs(dev
,
6085 mlx5_core_native_port_num(dev
->mdev
) - 1);
6088 static int mlx5_ib_stage_uar_init(struct mlx5_ib_dev
*dev
)
6090 dev
->mdev
->priv
.uar
= mlx5_get_uars_page(dev
->mdev
);
6091 return PTR_ERR_OR_ZERO(dev
->mdev
->priv
.uar
);
6094 static void mlx5_ib_stage_uar_cleanup(struct mlx5_ib_dev
*dev
)
6096 mlx5_put_uars_page(dev
->mdev
, dev
->mdev
->priv
.uar
);
6099 int mlx5_ib_stage_bfrag_init(struct mlx5_ib_dev
*dev
)
6103 err
= mlx5_alloc_bfreg(dev
->mdev
, &dev
->bfreg
, false, false);
6107 err
= mlx5_alloc_bfreg(dev
->mdev
, &dev
->fp_bfreg
, false, true);
6109 mlx5_free_bfreg(dev
->mdev
, &dev
->fp_bfreg
);
6114 void mlx5_ib_stage_bfrag_cleanup(struct mlx5_ib_dev
*dev
)
6116 mlx5_free_bfreg(dev
->mdev
, &dev
->fp_bfreg
);
6117 mlx5_free_bfreg(dev
->mdev
, &dev
->bfreg
);
6120 static int mlx5_ib_stage_populate_specs(struct mlx5_ib_dev
*dev
)
6122 return populate_specs_root(dev
);
6125 int mlx5_ib_stage_ib_reg_init(struct mlx5_ib_dev
*dev
)
6127 return ib_register_device(&dev
->ib_dev
, NULL
);
6130 void mlx5_ib_stage_pre_ib_reg_umr_cleanup(struct mlx5_ib_dev
*dev
)
6132 destroy_umrc_res(dev
);
6135 void mlx5_ib_stage_ib_reg_cleanup(struct mlx5_ib_dev
*dev
)
6137 ib_unregister_device(&dev
->ib_dev
);
6140 int mlx5_ib_stage_post_ib_reg_umr_init(struct mlx5_ib_dev
*dev
)
6142 return create_umr_res(dev
);
6145 static int mlx5_ib_stage_delay_drop_init(struct mlx5_ib_dev
*dev
)
6147 init_delay_drop(dev
);
6152 static void mlx5_ib_stage_delay_drop_cleanup(struct mlx5_ib_dev
*dev
)
6154 cancel_delay_drop(dev
);
6157 int mlx5_ib_stage_class_attr_init(struct mlx5_ib_dev
*dev
)
6162 for (i
= 0; i
< ARRAY_SIZE(mlx5_class_attributes
); i
++) {
6163 err
= device_create_file(&dev
->ib_dev
.dev
,
6164 mlx5_class_attributes
[i
]);
6172 static int mlx5_ib_stage_rep_reg_init(struct mlx5_ib_dev
*dev
)
6174 mlx5_ib_register_vport_reps(dev
);
6179 static void mlx5_ib_stage_rep_reg_cleanup(struct mlx5_ib_dev
*dev
)
6181 mlx5_ib_unregister_vport_reps(dev
);
6184 void __mlx5_ib_remove(struct mlx5_ib_dev
*dev
,
6185 const struct mlx5_ib_profile
*profile
,
6188 /* Number of stages to cleanup */
6191 if (profile
->stage
[stage
].cleanup
)
6192 profile
->stage
[stage
].cleanup(dev
);
6195 ib_dealloc_device((struct ib_device
*)dev
);
6198 void *__mlx5_ib_add(struct mlx5_ib_dev
*dev
,
6199 const struct mlx5_ib_profile
*profile
)
6204 printk_once(KERN_INFO
"%s", mlx5_version
);
6206 for (i
= 0; i
< MLX5_IB_STAGE_MAX
; i
++) {
6207 if (profile
->stage
[i
].init
) {
6208 err
= profile
->stage
[i
].init(dev
);
6214 dev
->profile
= profile
;
6215 dev
->ib_active
= true;
6220 __mlx5_ib_remove(dev
, profile
, i
);
6225 static const struct mlx5_ib_profile pf_profile
= {
6226 STAGE_CREATE(MLX5_IB_STAGE_INIT
,
6227 mlx5_ib_stage_init_init
,
6228 mlx5_ib_stage_init_cleanup
),
6229 STAGE_CREATE(MLX5_IB_STAGE_FLOW_DB
,
6230 mlx5_ib_stage_flow_db_init
,
6231 mlx5_ib_stage_flow_db_cleanup
),
6232 STAGE_CREATE(MLX5_IB_STAGE_CAPS
,
6233 mlx5_ib_stage_caps_init
,
6235 STAGE_CREATE(MLX5_IB_STAGE_NON_DEFAULT_CB
,
6236 mlx5_ib_stage_non_default_cb
,
6238 STAGE_CREATE(MLX5_IB_STAGE_ROCE
,
6239 mlx5_ib_stage_roce_init
,
6240 mlx5_ib_stage_roce_cleanup
),
6241 STAGE_CREATE(MLX5_IB_STAGE_DEVICE_RESOURCES
,
6242 mlx5_ib_stage_dev_res_init
,
6243 mlx5_ib_stage_dev_res_cleanup
),
6244 STAGE_CREATE(MLX5_IB_STAGE_ODP
,
6245 mlx5_ib_stage_odp_init
,
6247 STAGE_CREATE(MLX5_IB_STAGE_COUNTERS
,
6248 mlx5_ib_stage_counters_init
,
6249 mlx5_ib_stage_counters_cleanup
),
6250 STAGE_CREATE(MLX5_IB_STAGE_CONG_DEBUGFS
,
6251 mlx5_ib_stage_cong_debugfs_init
,
6252 mlx5_ib_stage_cong_debugfs_cleanup
),
6253 STAGE_CREATE(MLX5_IB_STAGE_UAR
,
6254 mlx5_ib_stage_uar_init
,
6255 mlx5_ib_stage_uar_cleanup
),
6256 STAGE_CREATE(MLX5_IB_STAGE_BFREG
,
6257 mlx5_ib_stage_bfrag_init
,
6258 mlx5_ib_stage_bfrag_cleanup
),
6259 STAGE_CREATE(MLX5_IB_STAGE_PRE_IB_REG_UMR
,
6261 mlx5_ib_stage_pre_ib_reg_umr_cleanup
),
6262 STAGE_CREATE(MLX5_IB_STAGE_SPECS
,
6263 mlx5_ib_stage_populate_specs
,
6265 STAGE_CREATE(MLX5_IB_STAGE_IB_REG
,
6266 mlx5_ib_stage_ib_reg_init
,
6267 mlx5_ib_stage_ib_reg_cleanup
),
6268 STAGE_CREATE(MLX5_IB_STAGE_POST_IB_REG_UMR
,
6269 mlx5_ib_stage_post_ib_reg_umr_init
,
6271 STAGE_CREATE(MLX5_IB_STAGE_DELAY_DROP
,
6272 mlx5_ib_stage_delay_drop_init
,
6273 mlx5_ib_stage_delay_drop_cleanup
),
6274 STAGE_CREATE(MLX5_IB_STAGE_CLASS_ATTR
,
6275 mlx5_ib_stage_class_attr_init
,
6279 static const struct mlx5_ib_profile nic_rep_profile
= {
6280 STAGE_CREATE(MLX5_IB_STAGE_INIT
,
6281 mlx5_ib_stage_init_init
,
6282 mlx5_ib_stage_init_cleanup
),
6283 STAGE_CREATE(MLX5_IB_STAGE_FLOW_DB
,
6284 mlx5_ib_stage_flow_db_init
,
6285 mlx5_ib_stage_flow_db_cleanup
),
6286 STAGE_CREATE(MLX5_IB_STAGE_CAPS
,
6287 mlx5_ib_stage_caps_init
,
6289 STAGE_CREATE(MLX5_IB_STAGE_NON_DEFAULT_CB
,
6290 mlx5_ib_stage_rep_non_default_cb
,
6292 STAGE_CREATE(MLX5_IB_STAGE_ROCE
,
6293 mlx5_ib_stage_rep_roce_init
,
6294 mlx5_ib_stage_rep_roce_cleanup
),
6295 STAGE_CREATE(MLX5_IB_STAGE_DEVICE_RESOURCES
,
6296 mlx5_ib_stage_dev_res_init
,
6297 mlx5_ib_stage_dev_res_cleanup
),
6298 STAGE_CREATE(MLX5_IB_STAGE_COUNTERS
,
6299 mlx5_ib_stage_counters_init
,
6300 mlx5_ib_stage_counters_cleanup
),
6301 STAGE_CREATE(MLX5_IB_STAGE_UAR
,
6302 mlx5_ib_stage_uar_init
,
6303 mlx5_ib_stage_uar_cleanup
),
6304 STAGE_CREATE(MLX5_IB_STAGE_BFREG
,
6305 mlx5_ib_stage_bfrag_init
,
6306 mlx5_ib_stage_bfrag_cleanup
),
6307 STAGE_CREATE(MLX5_IB_STAGE_PRE_IB_REG_UMR
,
6309 mlx5_ib_stage_pre_ib_reg_umr_cleanup
),
6310 STAGE_CREATE(MLX5_IB_STAGE_SPECS
,
6311 mlx5_ib_stage_populate_specs
,
6313 STAGE_CREATE(MLX5_IB_STAGE_IB_REG
,
6314 mlx5_ib_stage_ib_reg_init
,
6315 mlx5_ib_stage_ib_reg_cleanup
),
6316 STAGE_CREATE(MLX5_IB_STAGE_POST_IB_REG_UMR
,
6317 mlx5_ib_stage_post_ib_reg_umr_init
,
6319 STAGE_CREATE(MLX5_IB_STAGE_CLASS_ATTR
,
6320 mlx5_ib_stage_class_attr_init
,
6322 STAGE_CREATE(MLX5_IB_STAGE_REP_REG
,
6323 mlx5_ib_stage_rep_reg_init
,
6324 mlx5_ib_stage_rep_reg_cleanup
),
6327 static void *mlx5_ib_add_slave_port(struct mlx5_core_dev
*mdev
)
6329 struct mlx5_ib_multiport_info
*mpi
;
6330 struct mlx5_ib_dev
*dev
;
6334 mpi
= kzalloc(sizeof(*mpi
), GFP_KERNEL
);
6340 err
= mlx5_query_nic_vport_system_image_guid(mdev
,
6341 &mpi
->sys_image_guid
);
6347 mutex_lock(&mlx5_ib_multiport_mutex
);
6348 list_for_each_entry(dev
, &mlx5_ib_dev_list
, ib_dev_list
) {
6349 if (dev
->sys_image_guid
== mpi
->sys_image_guid
)
6350 bound
= mlx5_ib_bind_slave_port(dev
, mpi
);
6353 rdma_roce_rescan_device(&dev
->ib_dev
);
6359 list_add_tail(&mpi
->list
, &mlx5_ib_unaffiliated_port_list
);
6360 dev_dbg(&mdev
->pdev
->dev
, "no suitable IB device found to bind to, added to unaffiliated list.\n");
6362 mutex_unlock(&mlx5_ib_multiport_mutex
);
6367 static void *mlx5_ib_add(struct mlx5_core_dev
*mdev
)
6369 enum rdma_link_layer ll
;
6370 struct mlx5_ib_dev
*dev
;
6373 printk_once(KERN_INFO
"%s", mlx5_version
);
6375 port_type_cap
= MLX5_CAP_GEN(mdev
, port_type
);
6376 ll
= mlx5_port_type_cap_to_rdma_ll(port_type_cap
);
6378 if (mlx5_core_is_mp_slave(mdev
) && ll
== IB_LINK_LAYER_ETHERNET
)
6379 return mlx5_ib_add_slave_port(mdev
);
6381 dev
= (struct mlx5_ib_dev
*)ib_alloc_device(sizeof(*dev
));
6386 dev
->num_ports
= max(MLX5_CAP_GEN(mdev
, num_ports
),
6387 MLX5_CAP_GEN(mdev
, num_vhca_ports
));
6389 if (MLX5_ESWITCH_MANAGER(mdev
) &&
6390 mlx5_ib_eswitch_mode(mdev
->priv
.eswitch
) == SRIOV_OFFLOADS
) {
6391 dev
->rep
= mlx5_ib_vport_rep(mdev
->priv
.eswitch
, 0);
6393 return __mlx5_ib_add(dev
, &nic_rep_profile
);
6396 return __mlx5_ib_add(dev
, &pf_profile
);
6399 static void mlx5_ib_remove(struct mlx5_core_dev
*mdev
, void *context
)
6401 struct mlx5_ib_multiport_info
*mpi
;
6402 struct mlx5_ib_dev
*dev
;
6404 if (mlx5_core_is_mp_slave(mdev
)) {
6406 mutex_lock(&mlx5_ib_multiport_mutex
);
6408 mlx5_ib_unbind_slave_port(mpi
->ibdev
, mpi
);
6409 list_del(&mpi
->list
);
6410 mutex_unlock(&mlx5_ib_multiport_mutex
);
6415 __mlx5_ib_remove(dev
, dev
->profile
, MLX5_IB_STAGE_MAX
);
6418 static struct mlx5_interface mlx5_ib_interface
= {
6420 .remove
= mlx5_ib_remove
,
6421 .event
= mlx5_ib_event
,
6422 #ifdef CONFIG_INFINIBAND_ON_DEMAND_PAGING
6423 .pfault
= mlx5_ib_pfault
,
6425 .protocol
= MLX5_INTERFACE_PROTOCOL_IB
,
6428 unsigned long mlx5_ib_get_xlt_emergency_page(void)
6430 mutex_lock(&xlt_emergency_page_mutex
);
6431 return xlt_emergency_page
;
6434 void mlx5_ib_put_xlt_emergency_page(void)
6436 mutex_unlock(&xlt_emergency_page_mutex
);
6439 static int __init
mlx5_ib_init(void)
6443 xlt_emergency_page
= __get_free_page(GFP_KERNEL
);
6444 if (!xlt_emergency_page
)
6447 mutex_init(&xlt_emergency_page_mutex
);
6449 mlx5_ib_event_wq
= alloc_ordered_workqueue("mlx5_ib_event_wq", 0);
6450 if (!mlx5_ib_event_wq
) {
6451 free_page(xlt_emergency_page
);
6457 err
= mlx5_register_interface(&mlx5_ib_interface
);
6462 static void __exit
mlx5_ib_cleanup(void)
6464 mlx5_unregister_interface(&mlx5_ib_interface
);
6465 destroy_workqueue(mlx5_ib_event_wq
);
6466 mutex_destroy(&xlt_emergency_page_mutex
);
6467 free_page(xlt_emergency_page
);
6470 module_init(mlx5_ib_init
);
6471 module_exit(mlx5_ib_cleanup
);