2 * Copyright (c) 2006, 2007 Cisco Systems, Inc. All rights reserved.
3 * Copyright (c) 2007, 2008 Mellanox Technologies. All rights reserved.
5 * This software is available to you under a choice of one of two
6 * licenses. You may choose to be licensed under the terms of the GNU
7 * General Public License (GPL) Version 2, available from the file
8 * COPYING in the main directory of this source tree, or the
9 * OpenIB.org BSD license below:
11 * Redistribution and use in source and binary forms, with or
12 * without modification, are permitted provided that the following
15 * - Redistributions of source code must retain the above
16 * copyright notice, this list of conditions and the following
19 * - Redistributions in binary form must reproduce the above
20 * copyright notice, this list of conditions and the following
21 * disclaimer in the documentation and/or other materials
22 * provided with the distribution.
24 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
28 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
29 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
30 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
34 #include <linux/module.h>
35 #include <linux/init.h>
36 #include <linux/slab.h>
37 #include <linux/errno.h>
38 #include <linux/netdevice.h>
39 #include <linux/inetdevice.h>
40 #include <linux/rtnetlink.h>
41 #include <linux/if_vlan.h>
43 #include <net/addrconf.h>
45 #include <rdma/ib_smi.h>
46 #include <rdma/ib_user_verbs.h>
47 #include <rdma/ib_addr.h>
49 #include <linux/mlx4/driver.h>
50 #include <linux/mlx4/cmd.h>
55 #define DRV_NAME MLX4_IB_DRV_NAME
56 #define DRV_VERSION "1.0"
57 #define DRV_RELDATE "April 4, 2008"
59 #define MLX4_IB_FLOW_MAX_PRIO 0xFFF
60 #define MLX4_IB_FLOW_QPN_MASK 0xFFFFFF
62 MODULE_AUTHOR("Roland Dreier");
63 MODULE_DESCRIPTION("Mellanox ConnectX HCA InfiniBand driver");
64 MODULE_LICENSE("Dual BSD/GPL");
65 MODULE_VERSION(DRV_VERSION
);
67 int mlx4_ib_sm_guid_assign
= 1;
68 module_param_named(sm_guid_assign
, mlx4_ib_sm_guid_assign
, int, 0444);
69 MODULE_PARM_DESC(sm_guid_assign
, "Enable SM alias_GUID assignment if sm_guid_assign > 0 (Default: 1)");
71 static const char mlx4_ib_version
[] =
72 DRV_NAME
": Mellanox ConnectX InfiniBand driver v"
73 DRV_VERSION
" (" DRV_RELDATE
")\n";
75 struct update_gid_work
{
76 struct work_struct work
;
77 union ib_gid gids
[128];
78 struct mlx4_ib_dev
*dev
;
82 static void do_slave_init(struct mlx4_ib_dev
*ibdev
, int slave
, int do_init
);
84 static struct workqueue_struct
*wq
;
86 static void init_query_mad(struct ib_smp
*mad
)
88 mad
->base_version
= 1;
89 mad
->mgmt_class
= IB_MGMT_CLASS_SUBN_LID_ROUTED
;
90 mad
->class_version
= 1;
91 mad
->method
= IB_MGMT_METHOD_GET
;
94 static union ib_gid zgid
;
96 static int check_flow_steering_support(struct mlx4_dev
*dev
)
98 int eth_num_ports
= 0;
101 int dmfs
= dev
->caps
.steering_mode
== MLX4_STEERING_MODE_DEVICE_MANAGED
;
105 mlx4_foreach_port(i
, dev
, MLX4_PORT_TYPE_ETH
)
107 mlx4_foreach_port(i
, dev
, MLX4_PORT_TYPE_IB
)
109 dmfs
&= (!ib_num_ports
||
110 (dev
->caps
.flags2
& MLX4_DEV_CAP_FLAG2_DMFS_IPOIB
)) &&
112 (dev
->caps
.flags2
& MLX4_DEV_CAP_FLAG2_FS_EN
));
113 if (ib_num_ports
&& mlx4_is_mfunc(dev
)) {
114 pr_warn("Device managed flow steering is unavailable for IB port in multifunction env.\n");
121 static int mlx4_ib_query_device(struct ib_device
*ibdev
,
122 struct ib_device_attr
*props
)
124 struct mlx4_ib_dev
*dev
= to_mdev(ibdev
);
125 struct ib_smp
*in_mad
= NULL
;
126 struct ib_smp
*out_mad
= NULL
;
129 in_mad
= kzalloc(sizeof *in_mad
, GFP_KERNEL
);
130 out_mad
= kmalloc(sizeof *out_mad
, GFP_KERNEL
);
131 if (!in_mad
|| !out_mad
)
134 init_query_mad(in_mad
);
135 in_mad
->attr_id
= IB_SMP_ATTR_NODE_INFO
;
137 err
= mlx4_MAD_IFC(to_mdev(ibdev
), MLX4_MAD_IFC_IGNORE_KEYS
,
138 1, NULL
, NULL
, in_mad
, out_mad
);
142 memset(props
, 0, sizeof *props
);
144 props
->fw_ver
= dev
->dev
->caps
.fw_ver
;
145 props
->device_cap_flags
= IB_DEVICE_CHANGE_PHY_PORT
|
146 IB_DEVICE_PORT_ACTIVE_EVENT
|
147 IB_DEVICE_SYS_IMAGE_GUID
|
148 IB_DEVICE_RC_RNR_NAK_GEN
|
149 IB_DEVICE_BLOCK_MULTICAST_LOOPBACK
;
150 if (dev
->dev
->caps
.flags
& MLX4_DEV_CAP_FLAG_BAD_PKEY_CNTR
)
151 props
->device_cap_flags
|= IB_DEVICE_BAD_PKEY_CNTR
;
152 if (dev
->dev
->caps
.flags
& MLX4_DEV_CAP_FLAG_BAD_QKEY_CNTR
)
153 props
->device_cap_flags
|= IB_DEVICE_BAD_QKEY_CNTR
;
154 if (dev
->dev
->caps
.flags
& MLX4_DEV_CAP_FLAG_APM
)
155 props
->device_cap_flags
|= IB_DEVICE_AUTO_PATH_MIG
;
156 if (dev
->dev
->caps
.flags
& MLX4_DEV_CAP_FLAG_UD_AV_PORT
)
157 props
->device_cap_flags
|= IB_DEVICE_UD_AV_PORT_ENFORCE
;
158 if (dev
->dev
->caps
.flags
& MLX4_DEV_CAP_FLAG_IPOIB_CSUM
)
159 props
->device_cap_flags
|= IB_DEVICE_UD_IP_CSUM
;
160 if (dev
->dev
->caps
.max_gso_sz
&& dev
->dev
->caps
.flags
& MLX4_DEV_CAP_FLAG_BLH
)
161 props
->device_cap_flags
|= IB_DEVICE_UD_TSO
;
162 if (dev
->dev
->caps
.bmme_flags
& MLX4_BMME_FLAG_RESERVED_LKEY
)
163 props
->device_cap_flags
|= IB_DEVICE_LOCAL_DMA_LKEY
;
164 if ((dev
->dev
->caps
.bmme_flags
& MLX4_BMME_FLAG_LOCAL_INV
) &&
165 (dev
->dev
->caps
.bmme_flags
& MLX4_BMME_FLAG_REMOTE_INV
) &&
166 (dev
->dev
->caps
.bmme_flags
& MLX4_BMME_FLAG_FAST_REG_WR
))
167 props
->device_cap_flags
|= IB_DEVICE_MEM_MGT_EXTENSIONS
;
168 if (dev
->dev
->caps
.flags
& MLX4_DEV_CAP_FLAG_XRC
)
169 props
->device_cap_flags
|= IB_DEVICE_XRC
;
170 if (dev
->dev
->caps
.flags
& MLX4_DEV_CAP_FLAG_MEM_WINDOW
)
171 props
->device_cap_flags
|= IB_DEVICE_MEM_WINDOW
;
172 if (dev
->dev
->caps
.bmme_flags
& MLX4_BMME_FLAG_TYPE_2_WIN
) {
173 if (dev
->dev
->caps
.bmme_flags
& MLX4_BMME_FLAG_WIN_TYPE_2B
)
174 props
->device_cap_flags
|= IB_DEVICE_MEM_WINDOW_TYPE_2B
;
176 props
->device_cap_flags
|= IB_DEVICE_MEM_WINDOW_TYPE_2A
;
177 if (dev
->steering_support
== MLX4_STEERING_MODE_DEVICE_MANAGED
)
178 props
->device_cap_flags
|= IB_DEVICE_MANAGED_FLOW_STEERING
;
181 props
->vendor_id
= be32_to_cpup((__be32
*) (out_mad
->data
+ 36)) &
183 props
->vendor_part_id
= dev
->dev
->pdev
->device
;
184 props
->hw_ver
= be32_to_cpup((__be32
*) (out_mad
->data
+ 32));
185 memcpy(&props
->sys_image_guid
, out_mad
->data
+ 4, 8);
187 props
->max_mr_size
= ~0ull;
188 props
->page_size_cap
= dev
->dev
->caps
.page_size_cap
;
189 props
->max_qp
= dev
->dev
->quotas
.qp
;
190 props
->max_qp_wr
= dev
->dev
->caps
.max_wqes
- MLX4_IB_SQ_MAX_SPARE
;
191 props
->max_sge
= min(dev
->dev
->caps
.max_sq_sg
,
192 dev
->dev
->caps
.max_rq_sg
);
193 props
->max_cq
= dev
->dev
->quotas
.cq
;
194 props
->max_cqe
= dev
->dev
->caps
.max_cqes
;
195 props
->max_mr
= dev
->dev
->quotas
.mpt
;
196 props
->max_pd
= dev
->dev
->caps
.num_pds
- dev
->dev
->caps
.reserved_pds
;
197 props
->max_qp_rd_atom
= dev
->dev
->caps
.max_qp_dest_rdma
;
198 props
->max_qp_init_rd_atom
= dev
->dev
->caps
.max_qp_init_rdma
;
199 props
->max_res_rd_atom
= props
->max_qp_rd_atom
* props
->max_qp
;
200 props
->max_srq
= dev
->dev
->quotas
.srq
;
201 props
->max_srq_wr
= dev
->dev
->caps
.max_srq_wqes
- 1;
202 props
->max_srq_sge
= dev
->dev
->caps
.max_srq_sge
;
203 props
->max_fast_reg_page_list_len
= MLX4_MAX_FAST_REG_PAGES
;
204 props
->local_ca_ack_delay
= dev
->dev
->caps
.local_ca_ack_delay
;
205 props
->atomic_cap
= dev
->dev
->caps
.flags
& MLX4_DEV_CAP_FLAG_ATOMIC
?
206 IB_ATOMIC_HCA
: IB_ATOMIC_NONE
;
207 props
->masked_atomic_cap
= props
->atomic_cap
;
208 props
->max_pkeys
= dev
->dev
->caps
.pkey_table_len
[1];
209 props
->max_mcast_grp
= dev
->dev
->caps
.num_mgms
+ dev
->dev
->caps
.num_amgms
;
210 props
->max_mcast_qp_attach
= dev
->dev
->caps
.num_qp_per_mgm
;
211 props
->max_total_mcast_qp_attach
= props
->max_mcast_qp_attach
*
212 props
->max_mcast_grp
;
213 props
->max_map_per_fmr
= dev
->dev
->caps
.max_fmr_maps
;
222 static enum rdma_link_layer
223 mlx4_ib_port_link_layer(struct ib_device
*device
, u8 port_num
)
225 struct mlx4_dev
*dev
= to_mdev(device
)->dev
;
227 return dev
->caps
.port_mask
[port_num
] == MLX4_PORT_TYPE_IB
?
228 IB_LINK_LAYER_INFINIBAND
: IB_LINK_LAYER_ETHERNET
;
231 static int ib_link_query_port(struct ib_device
*ibdev
, u8 port
,
232 struct ib_port_attr
*props
, int netw_view
)
234 struct ib_smp
*in_mad
= NULL
;
235 struct ib_smp
*out_mad
= NULL
;
236 int ext_active_speed
;
237 int mad_ifc_flags
= MLX4_MAD_IFC_IGNORE_KEYS
;
240 in_mad
= kzalloc(sizeof *in_mad
, GFP_KERNEL
);
241 out_mad
= kmalloc(sizeof *out_mad
, GFP_KERNEL
);
242 if (!in_mad
|| !out_mad
)
245 init_query_mad(in_mad
);
246 in_mad
->attr_id
= IB_SMP_ATTR_PORT_INFO
;
247 in_mad
->attr_mod
= cpu_to_be32(port
);
249 if (mlx4_is_mfunc(to_mdev(ibdev
)->dev
) && netw_view
)
250 mad_ifc_flags
|= MLX4_MAD_IFC_NET_VIEW
;
252 err
= mlx4_MAD_IFC(to_mdev(ibdev
), mad_ifc_flags
, port
, NULL
, NULL
,
258 props
->lid
= be16_to_cpup((__be16
*) (out_mad
->data
+ 16));
259 props
->lmc
= out_mad
->data
[34] & 0x7;
260 props
->sm_lid
= be16_to_cpup((__be16
*) (out_mad
->data
+ 18));
261 props
->sm_sl
= out_mad
->data
[36] & 0xf;
262 props
->state
= out_mad
->data
[32] & 0xf;
263 props
->phys_state
= out_mad
->data
[33] >> 4;
264 props
->port_cap_flags
= be32_to_cpup((__be32
*) (out_mad
->data
+ 20));
266 props
->gid_tbl_len
= out_mad
->data
[50];
268 props
->gid_tbl_len
= to_mdev(ibdev
)->dev
->caps
.gid_table_len
[port
];
269 props
->max_msg_sz
= to_mdev(ibdev
)->dev
->caps
.max_msg_sz
;
270 props
->pkey_tbl_len
= to_mdev(ibdev
)->dev
->caps
.pkey_table_len
[port
];
271 props
->bad_pkey_cntr
= be16_to_cpup((__be16
*) (out_mad
->data
+ 46));
272 props
->qkey_viol_cntr
= be16_to_cpup((__be16
*) (out_mad
->data
+ 48));
273 props
->active_width
= out_mad
->data
[31] & 0xf;
274 props
->active_speed
= out_mad
->data
[35] >> 4;
275 props
->max_mtu
= out_mad
->data
[41] & 0xf;
276 props
->active_mtu
= out_mad
->data
[36] >> 4;
277 props
->subnet_timeout
= out_mad
->data
[51] & 0x1f;
278 props
->max_vl_num
= out_mad
->data
[37] >> 4;
279 props
->init_type_reply
= out_mad
->data
[41] >> 4;
281 /* Check if extended speeds (EDR/FDR/...) are supported */
282 if (props
->port_cap_flags
& IB_PORT_EXTENDED_SPEEDS_SUP
) {
283 ext_active_speed
= out_mad
->data
[62] >> 4;
285 switch (ext_active_speed
) {
287 props
->active_speed
= IB_SPEED_FDR
;
290 props
->active_speed
= IB_SPEED_EDR
;
295 /* If reported active speed is QDR, check if is FDR-10 */
296 if (props
->active_speed
== IB_SPEED_QDR
) {
297 init_query_mad(in_mad
);
298 in_mad
->attr_id
= MLX4_ATTR_EXTENDED_PORT_INFO
;
299 in_mad
->attr_mod
= cpu_to_be32(port
);
301 err
= mlx4_MAD_IFC(to_mdev(ibdev
), mad_ifc_flags
, port
,
302 NULL
, NULL
, in_mad
, out_mad
);
306 /* Checking LinkSpeedActive for FDR-10 */
307 if (out_mad
->data
[15] & 0x1)
308 props
->active_speed
= IB_SPEED_FDR10
;
311 /* Avoid wrong speed value returned by FW if the IB link is down. */
312 if (props
->state
== IB_PORT_DOWN
)
313 props
->active_speed
= IB_SPEED_SDR
;
321 static u8
state_to_phys_state(enum ib_port_state state
)
323 return state
== IB_PORT_ACTIVE
? 5 : 3;
326 static int eth_link_query_port(struct ib_device
*ibdev
, u8 port
,
327 struct ib_port_attr
*props
, int netw_view
)
330 struct mlx4_ib_dev
*mdev
= to_mdev(ibdev
);
331 struct mlx4_ib_iboe
*iboe
= &mdev
->iboe
;
332 struct net_device
*ndev
;
334 struct mlx4_cmd_mailbox
*mailbox
;
337 mailbox
= mlx4_alloc_cmd_mailbox(mdev
->dev
);
339 return PTR_ERR(mailbox
);
341 err
= mlx4_cmd_box(mdev
->dev
, 0, mailbox
->dma
, port
, 0,
342 MLX4_CMD_QUERY_PORT
, MLX4_CMD_TIME_CLASS_B
,
347 props
->active_width
= (((u8
*)mailbox
->buf
)[5] == 0x40) ?
348 IB_WIDTH_4X
: IB_WIDTH_1X
;
349 props
->active_speed
= IB_SPEED_QDR
;
350 props
->port_cap_flags
= IB_PORT_CM_SUP
;
351 props
->gid_tbl_len
= mdev
->dev
->caps
.gid_table_len
[port
];
352 props
->max_msg_sz
= mdev
->dev
->caps
.max_msg_sz
;
353 props
->pkey_tbl_len
= 1;
354 props
->max_mtu
= IB_MTU_4096
;
355 props
->max_vl_num
= 2;
356 props
->state
= IB_PORT_DOWN
;
357 props
->phys_state
= state_to_phys_state(props
->state
);
358 props
->active_mtu
= IB_MTU_256
;
359 spin_lock(&iboe
->lock
);
360 ndev
= iboe
->netdevs
[port
- 1];
364 tmp
= iboe_get_mtu(ndev
->mtu
);
365 props
->active_mtu
= tmp
? min(props
->max_mtu
, tmp
) : IB_MTU_256
;
367 props
->state
= (netif_running(ndev
) && netif_carrier_ok(ndev
)) ?
368 IB_PORT_ACTIVE
: IB_PORT_DOWN
;
369 props
->phys_state
= state_to_phys_state(props
->state
);
371 spin_unlock(&iboe
->lock
);
373 mlx4_free_cmd_mailbox(mdev
->dev
, mailbox
);
377 int __mlx4_ib_query_port(struct ib_device
*ibdev
, u8 port
,
378 struct ib_port_attr
*props
, int netw_view
)
382 memset(props
, 0, sizeof *props
);
384 err
= mlx4_ib_port_link_layer(ibdev
, port
) == IB_LINK_LAYER_INFINIBAND
?
385 ib_link_query_port(ibdev
, port
, props
, netw_view
) :
386 eth_link_query_port(ibdev
, port
, props
, netw_view
);
391 static int mlx4_ib_query_port(struct ib_device
*ibdev
, u8 port
,
392 struct ib_port_attr
*props
)
394 /* returns host view */
395 return __mlx4_ib_query_port(ibdev
, port
, props
, 0);
398 int __mlx4_ib_query_gid(struct ib_device
*ibdev
, u8 port
, int index
,
399 union ib_gid
*gid
, int netw_view
)
401 struct ib_smp
*in_mad
= NULL
;
402 struct ib_smp
*out_mad
= NULL
;
404 struct mlx4_ib_dev
*dev
= to_mdev(ibdev
);
406 int mad_ifc_flags
= MLX4_MAD_IFC_IGNORE_KEYS
;
408 in_mad
= kzalloc(sizeof *in_mad
, GFP_KERNEL
);
409 out_mad
= kmalloc(sizeof *out_mad
, GFP_KERNEL
);
410 if (!in_mad
|| !out_mad
)
413 init_query_mad(in_mad
);
414 in_mad
->attr_id
= IB_SMP_ATTR_PORT_INFO
;
415 in_mad
->attr_mod
= cpu_to_be32(port
);
417 if (mlx4_is_mfunc(dev
->dev
) && netw_view
)
418 mad_ifc_flags
|= MLX4_MAD_IFC_NET_VIEW
;
420 err
= mlx4_MAD_IFC(dev
, mad_ifc_flags
, port
, NULL
, NULL
, in_mad
, out_mad
);
424 memcpy(gid
->raw
, out_mad
->data
+ 8, 8);
426 if (mlx4_is_mfunc(dev
->dev
) && !netw_view
) {
428 /* For any index > 0, return the null guid */
435 init_query_mad(in_mad
);
436 in_mad
->attr_id
= IB_SMP_ATTR_GUID_INFO
;
437 in_mad
->attr_mod
= cpu_to_be32(index
/ 8);
439 err
= mlx4_MAD_IFC(dev
, mad_ifc_flags
, port
,
440 NULL
, NULL
, in_mad
, out_mad
);
444 memcpy(gid
->raw
+ 8, out_mad
->data
+ (index
% 8) * 8, 8);
448 memset(gid
->raw
+ 8, 0, 8);
454 static int iboe_query_gid(struct ib_device
*ibdev
, u8 port
, int index
,
457 struct mlx4_ib_dev
*dev
= to_mdev(ibdev
);
459 *gid
= dev
->iboe
.gid_table
[port
- 1][index
];
464 static int mlx4_ib_query_gid(struct ib_device
*ibdev
, u8 port
, int index
,
467 if (rdma_port_get_link_layer(ibdev
, port
) == IB_LINK_LAYER_INFINIBAND
)
468 return __mlx4_ib_query_gid(ibdev
, port
, index
, gid
, 0);
470 return iboe_query_gid(ibdev
, port
, index
, gid
);
473 int __mlx4_ib_query_pkey(struct ib_device
*ibdev
, u8 port
, u16 index
,
474 u16
*pkey
, int netw_view
)
476 struct ib_smp
*in_mad
= NULL
;
477 struct ib_smp
*out_mad
= NULL
;
478 int mad_ifc_flags
= MLX4_MAD_IFC_IGNORE_KEYS
;
481 in_mad
= kzalloc(sizeof *in_mad
, GFP_KERNEL
);
482 out_mad
= kmalloc(sizeof *out_mad
, GFP_KERNEL
);
483 if (!in_mad
|| !out_mad
)
486 init_query_mad(in_mad
);
487 in_mad
->attr_id
= IB_SMP_ATTR_PKEY_TABLE
;
488 in_mad
->attr_mod
= cpu_to_be32(index
/ 32);
490 if (mlx4_is_mfunc(to_mdev(ibdev
)->dev
) && netw_view
)
491 mad_ifc_flags
|= MLX4_MAD_IFC_NET_VIEW
;
493 err
= mlx4_MAD_IFC(to_mdev(ibdev
), mad_ifc_flags
, port
, NULL
, NULL
,
498 *pkey
= be16_to_cpu(((__be16
*) out_mad
->data
)[index
% 32]);
506 static int mlx4_ib_query_pkey(struct ib_device
*ibdev
, u8 port
, u16 index
, u16
*pkey
)
508 return __mlx4_ib_query_pkey(ibdev
, port
, index
, pkey
, 0);
511 static int mlx4_ib_modify_device(struct ib_device
*ibdev
, int mask
,
512 struct ib_device_modify
*props
)
514 struct mlx4_cmd_mailbox
*mailbox
;
517 if (mask
& ~IB_DEVICE_MODIFY_NODE_DESC
)
520 if (!(mask
& IB_DEVICE_MODIFY_NODE_DESC
))
523 if (mlx4_is_slave(to_mdev(ibdev
)->dev
))
526 spin_lock_irqsave(&to_mdev(ibdev
)->sm_lock
, flags
);
527 memcpy(ibdev
->node_desc
, props
->node_desc
, 64);
528 spin_unlock_irqrestore(&to_mdev(ibdev
)->sm_lock
, flags
);
531 * If possible, pass node desc to FW, so it can generate
532 * a 144 trap. If cmd fails, just ignore.
534 mailbox
= mlx4_alloc_cmd_mailbox(to_mdev(ibdev
)->dev
);
538 memcpy(mailbox
->buf
, props
->node_desc
, 64);
539 mlx4_cmd(to_mdev(ibdev
)->dev
, mailbox
->dma
, 1, 0,
540 MLX4_CMD_SET_NODE
, MLX4_CMD_TIME_CLASS_A
, MLX4_CMD_NATIVE
);
542 mlx4_free_cmd_mailbox(to_mdev(ibdev
)->dev
, mailbox
);
547 static int mlx4_SET_PORT(struct mlx4_ib_dev
*dev
, u8 port
, int reset_qkey_viols
,
550 struct mlx4_cmd_mailbox
*mailbox
;
552 u8 is_eth
= dev
->dev
->caps
.port_type
[port
] == MLX4_PORT_TYPE_ETH
;
554 mailbox
= mlx4_alloc_cmd_mailbox(dev
->dev
);
556 return PTR_ERR(mailbox
);
558 if (dev
->dev
->flags
& MLX4_FLAG_OLD_PORT_CMDS
) {
559 *(u8
*) mailbox
->buf
= !!reset_qkey_viols
<< 6;
560 ((__be32
*) mailbox
->buf
)[2] = cpu_to_be32(cap_mask
);
562 ((u8
*) mailbox
->buf
)[3] = !!reset_qkey_viols
;
563 ((__be32
*) mailbox
->buf
)[1] = cpu_to_be32(cap_mask
);
566 err
= mlx4_cmd(dev
->dev
, mailbox
->dma
, port
, is_eth
, MLX4_CMD_SET_PORT
,
567 MLX4_CMD_TIME_CLASS_B
, MLX4_CMD_NATIVE
);
569 mlx4_free_cmd_mailbox(dev
->dev
, mailbox
);
573 static int mlx4_ib_modify_port(struct ib_device
*ibdev
, u8 port
, int mask
,
574 struct ib_port_modify
*props
)
576 struct ib_port_attr attr
;
580 mutex_lock(&to_mdev(ibdev
)->cap_mask_mutex
);
582 err
= mlx4_ib_query_port(ibdev
, port
, &attr
);
586 cap_mask
= (attr
.port_cap_flags
| props
->set_port_cap_mask
) &
587 ~props
->clr_port_cap_mask
;
589 err
= mlx4_SET_PORT(to_mdev(ibdev
), port
,
590 !!(mask
& IB_PORT_RESET_QKEY_CNTR
),
594 mutex_unlock(&to_mdev(ibdev
)->cap_mask_mutex
);
598 static struct ib_ucontext
*mlx4_ib_alloc_ucontext(struct ib_device
*ibdev
,
599 struct ib_udata
*udata
)
601 struct mlx4_ib_dev
*dev
= to_mdev(ibdev
);
602 struct mlx4_ib_ucontext
*context
;
603 struct mlx4_ib_alloc_ucontext_resp_v3 resp_v3
;
604 struct mlx4_ib_alloc_ucontext_resp resp
;
608 return ERR_PTR(-EAGAIN
);
610 if (ibdev
->uverbs_abi_ver
== MLX4_IB_UVERBS_NO_DEV_CAPS_ABI_VERSION
) {
611 resp_v3
.qp_tab_size
= dev
->dev
->caps
.num_qps
;
612 resp_v3
.bf_reg_size
= dev
->dev
->caps
.bf_reg_size
;
613 resp_v3
.bf_regs_per_page
= dev
->dev
->caps
.bf_regs_per_page
;
615 resp
.dev_caps
= dev
->dev
->caps
.userspace_caps
;
616 resp
.qp_tab_size
= dev
->dev
->caps
.num_qps
;
617 resp
.bf_reg_size
= dev
->dev
->caps
.bf_reg_size
;
618 resp
.bf_regs_per_page
= dev
->dev
->caps
.bf_regs_per_page
;
619 resp
.cqe_size
= dev
->dev
->caps
.cqe_size
;
622 context
= kmalloc(sizeof *context
, GFP_KERNEL
);
624 return ERR_PTR(-ENOMEM
);
626 err
= mlx4_uar_alloc(to_mdev(ibdev
)->dev
, &context
->uar
);
632 INIT_LIST_HEAD(&context
->db_page_list
);
633 mutex_init(&context
->db_page_mutex
);
635 if (ibdev
->uverbs_abi_ver
== MLX4_IB_UVERBS_NO_DEV_CAPS_ABI_VERSION
)
636 err
= ib_copy_to_udata(udata
, &resp_v3
, sizeof(resp_v3
));
638 err
= ib_copy_to_udata(udata
, &resp
, sizeof(resp
));
641 mlx4_uar_free(to_mdev(ibdev
)->dev
, &context
->uar
);
643 return ERR_PTR(-EFAULT
);
646 return &context
->ibucontext
;
649 static int mlx4_ib_dealloc_ucontext(struct ib_ucontext
*ibcontext
)
651 struct mlx4_ib_ucontext
*context
= to_mucontext(ibcontext
);
653 mlx4_uar_free(to_mdev(ibcontext
->device
)->dev
, &context
->uar
);
659 static int mlx4_ib_mmap(struct ib_ucontext
*context
, struct vm_area_struct
*vma
)
661 struct mlx4_ib_dev
*dev
= to_mdev(context
->device
);
663 if (vma
->vm_end
- vma
->vm_start
!= PAGE_SIZE
)
666 if (vma
->vm_pgoff
== 0) {
667 vma
->vm_page_prot
= pgprot_noncached(vma
->vm_page_prot
);
669 if (io_remap_pfn_range(vma
, vma
->vm_start
,
670 to_mucontext(context
)->uar
.pfn
,
671 PAGE_SIZE
, vma
->vm_page_prot
))
673 } else if (vma
->vm_pgoff
== 1 && dev
->dev
->caps
.bf_reg_size
!= 0) {
674 vma
->vm_page_prot
= pgprot_writecombine(vma
->vm_page_prot
);
676 if (io_remap_pfn_range(vma
, vma
->vm_start
,
677 to_mucontext(context
)->uar
.pfn
+
678 dev
->dev
->caps
.num_uars
,
679 PAGE_SIZE
, vma
->vm_page_prot
))
687 static struct ib_pd
*mlx4_ib_alloc_pd(struct ib_device
*ibdev
,
688 struct ib_ucontext
*context
,
689 struct ib_udata
*udata
)
691 struct mlx4_ib_pd
*pd
;
694 pd
= kmalloc(sizeof *pd
, GFP_KERNEL
);
696 return ERR_PTR(-ENOMEM
);
698 err
= mlx4_pd_alloc(to_mdev(ibdev
)->dev
, &pd
->pdn
);
705 if (ib_copy_to_udata(udata
, &pd
->pdn
, sizeof (__u32
))) {
706 mlx4_pd_free(to_mdev(ibdev
)->dev
, pd
->pdn
);
708 return ERR_PTR(-EFAULT
);
714 static int mlx4_ib_dealloc_pd(struct ib_pd
*pd
)
716 mlx4_pd_free(to_mdev(pd
->device
)->dev
, to_mpd(pd
)->pdn
);
722 static struct ib_xrcd
*mlx4_ib_alloc_xrcd(struct ib_device
*ibdev
,
723 struct ib_ucontext
*context
,
724 struct ib_udata
*udata
)
726 struct mlx4_ib_xrcd
*xrcd
;
729 if (!(to_mdev(ibdev
)->dev
->caps
.flags
& MLX4_DEV_CAP_FLAG_XRC
))
730 return ERR_PTR(-ENOSYS
);
732 xrcd
= kmalloc(sizeof *xrcd
, GFP_KERNEL
);
734 return ERR_PTR(-ENOMEM
);
736 err
= mlx4_xrcd_alloc(to_mdev(ibdev
)->dev
, &xrcd
->xrcdn
);
740 xrcd
->pd
= ib_alloc_pd(ibdev
);
741 if (IS_ERR(xrcd
->pd
)) {
742 err
= PTR_ERR(xrcd
->pd
);
746 xrcd
->cq
= ib_create_cq(ibdev
, NULL
, NULL
, xrcd
, 1, 0);
747 if (IS_ERR(xrcd
->cq
)) {
748 err
= PTR_ERR(xrcd
->cq
);
752 return &xrcd
->ibxrcd
;
755 ib_dealloc_pd(xrcd
->pd
);
757 mlx4_xrcd_free(to_mdev(ibdev
)->dev
, xrcd
->xrcdn
);
763 static int mlx4_ib_dealloc_xrcd(struct ib_xrcd
*xrcd
)
765 ib_destroy_cq(to_mxrcd(xrcd
)->cq
);
766 ib_dealloc_pd(to_mxrcd(xrcd
)->pd
);
767 mlx4_xrcd_free(to_mdev(xrcd
->device
)->dev
, to_mxrcd(xrcd
)->xrcdn
);
773 static int add_gid_entry(struct ib_qp
*ibqp
, union ib_gid
*gid
)
775 struct mlx4_ib_qp
*mqp
= to_mqp(ibqp
);
776 struct mlx4_ib_dev
*mdev
= to_mdev(ibqp
->device
);
777 struct mlx4_ib_gid_entry
*ge
;
779 ge
= kzalloc(sizeof *ge
, GFP_KERNEL
);
784 if (mlx4_ib_add_mc(mdev
, mqp
, gid
)) {
785 ge
->port
= mqp
->port
;
789 mutex_lock(&mqp
->mutex
);
790 list_add_tail(&ge
->list
, &mqp
->gid_list
);
791 mutex_unlock(&mqp
->mutex
);
796 int mlx4_ib_add_mc(struct mlx4_ib_dev
*mdev
, struct mlx4_ib_qp
*mqp
,
799 struct net_device
*ndev
;
805 spin_lock(&mdev
->iboe
.lock
);
806 ndev
= mdev
->iboe
.netdevs
[mqp
->port
- 1];
809 spin_unlock(&mdev
->iboe
.lock
);
819 struct mlx4_ib_steering
{
820 struct list_head list
;
825 static int parse_flow_attr(struct mlx4_dev
*dev
,
827 union ib_flow_spec
*ib_spec
,
828 struct _rule_hw
*mlx4_spec
)
830 enum mlx4_net_trans_rule_id type
;
832 switch (ib_spec
->type
) {
833 case IB_FLOW_SPEC_ETH
:
834 type
= MLX4_NET_TRANS_RULE_ID_ETH
;
835 memcpy(mlx4_spec
->eth
.dst_mac
, ib_spec
->eth
.val
.dst_mac
,
837 memcpy(mlx4_spec
->eth
.dst_mac_msk
, ib_spec
->eth
.mask
.dst_mac
,
839 mlx4_spec
->eth
.vlan_tag
= ib_spec
->eth
.val
.vlan_tag
;
840 mlx4_spec
->eth
.vlan_tag_msk
= ib_spec
->eth
.mask
.vlan_tag
;
842 case IB_FLOW_SPEC_IB
:
843 type
= MLX4_NET_TRANS_RULE_ID_IB
;
844 mlx4_spec
->ib
.l3_qpn
=
846 mlx4_spec
->ib
.qpn_mask
=
847 cpu_to_be32(MLX4_IB_FLOW_QPN_MASK
);
851 case IB_FLOW_SPEC_IPV4
:
852 type
= MLX4_NET_TRANS_RULE_ID_IPV4
;
853 mlx4_spec
->ipv4
.src_ip
= ib_spec
->ipv4
.val
.src_ip
;
854 mlx4_spec
->ipv4
.src_ip_msk
= ib_spec
->ipv4
.mask
.src_ip
;
855 mlx4_spec
->ipv4
.dst_ip
= ib_spec
->ipv4
.val
.dst_ip
;
856 mlx4_spec
->ipv4
.dst_ip_msk
= ib_spec
->ipv4
.mask
.dst_ip
;
859 case IB_FLOW_SPEC_TCP
:
860 case IB_FLOW_SPEC_UDP
:
861 type
= ib_spec
->type
== IB_FLOW_SPEC_TCP
?
862 MLX4_NET_TRANS_RULE_ID_TCP
:
863 MLX4_NET_TRANS_RULE_ID_UDP
;
864 mlx4_spec
->tcp_udp
.dst_port
= ib_spec
->tcp_udp
.val
.dst_port
;
865 mlx4_spec
->tcp_udp
.dst_port_msk
= ib_spec
->tcp_udp
.mask
.dst_port
;
866 mlx4_spec
->tcp_udp
.src_port
= ib_spec
->tcp_udp
.val
.src_port
;
867 mlx4_spec
->tcp_udp
.src_port_msk
= ib_spec
->tcp_udp
.mask
.src_port
;
873 if (mlx4_map_sw_to_hw_steering_id(dev
, type
) < 0 ||
874 mlx4_hw_rule_sz(dev
, type
) < 0)
876 mlx4_spec
->id
= cpu_to_be16(mlx4_map_sw_to_hw_steering_id(dev
, type
));
877 mlx4_spec
->size
= mlx4_hw_rule_sz(dev
, type
) >> 2;
878 return mlx4_hw_rule_sz(dev
, type
);
881 struct default_rules
{
882 __u32 mandatory_fields
[IB_FLOW_SPEC_SUPPORT_LAYERS
];
883 __u32 mandatory_not_fields
[IB_FLOW_SPEC_SUPPORT_LAYERS
];
884 __u32 rules_create_list
[IB_FLOW_SPEC_SUPPORT_LAYERS
];
887 static const struct default_rules default_table
[] = {
889 .mandatory_fields
= {IB_FLOW_SPEC_IPV4
},
890 .mandatory_not_fields
= {IB_FLOW_SPEC_ETH
},
891 .rules_create_list
= {IB_FLOW_SPEC_IB
},
892 .link_layer
= IB_LINK_LAYER_INFINIBAND
896 static int __mlx4_ib_default_rules_match(struct ib_qp
*qp
,
897 struct ib_flow_attr
*flow_attr
)
901 const struct default_rules
*pdefault_rules
= default_table
;
902 u8 link_layer
= rdma_port_get_link_layer(qp
->device
, flow_attr
->port
);
904 for (i
= 0; i
< sizeof(default_table
)/sizeof(default_table
[0]); i
++,
906 __u32 field_types
[IB_FLOW_SPEC_SUPPORT_LAYERS
];
907 memset(&field_types
, 0, sizeof(field_types
));
909 if (link_layer
!= pdefault_rules
->link_layer
)
912 ib_flow
= flow_attr
+ 1;
913 /* we assume the specs are sorted */
914 for (j
= 0, k
= 0; k
< IB_FLOW_SPEC_SUPPORT_LAYERS
&&
915 j
< flow_attr
->num_of_specs
; k
++) {
916 union ib_flow_spec
*current_flow
=
917 (union ib_flow_spec
*)ib_flow
;
919 /* same layer but different type */
920 if (((current_flow
->type
& IB_FLOW_SPEC_LAYER_MASK
) ==
921 (pdefault_rules
->mandatory_fields
[k
] &
922 IB_FLOW_SPEC_LAYER_MASK
)) &&
923 (current_flow
->type
!=
924 pdefault_rules
->mandatory_fields
[k
]))
927 /* same layer, try match next one */
928 if (current_flow
->type
==
929 pdefault_rules
->mandatory_fields
[k
]) {
932 ((union ib_flow_spec
*)ib_flow
)->size
;
936 ib_flow
= flow_attr
+ 1;
937 for (j
= 0; j
< flow_attr
->num_of_specs
;
938 j
++, ib_flow
+= ((union ib_flow_spec
*)ib_flow
)->size
)
939 for (k
= 0; k
< IB_FLOW_SPEC_SUPPORT_LAYERS
; k
++)
940 /* same layer and same type */
941 if (((union ib_flow_spec
*)ib_flow
)->type
==
942 pdefault_rules
->mandatory_not_fields
[k
])
951 static int __mlx4_ib_create_default_rules(
952 struct mlx4_ib_dev
*mdev
,
954 const struct default_rules
*pdefault_rules
,
955 struct _rule_hw
*mlx4_spec
) {
959 for (i
= 0; i
< sizeof(pdefault_rules
->rules_create_list
)/
960 sizeof(pdefault_rules
->rules_create_list
[0]); i
++) {
962 union ib_flow_spec ib_spec
;
963 switch (pdefault_rules
->rules_create_list
[i
]) {
967 case IB_FLOW_SPEC_IB
:
968 ib_spec
.type
= IB_FLOW_SPEC_IB
;
969 ib_spec
.size
= sizeof(struct ib_flow_spec_ib
);
976 /* We must put empty rule, qpn is being ignored */
977 ret
= parse_flow_attr(mdev
->dev
, 0, &ib_spec
,
980 pr_info("invalid parsing\n");
984 mlx4_spec
= (void *)mlx4_spec
+ ret
;
990 static int __mlx4_ib_create_flow(struct ib_qp
*qp
, struct ib_flow_attr
*flow_attr
,
992 enum mlx4_net_trans_promisc_mode flow_type
,
998 struct mlx4_ib_dev
*mdev
= to_mdev(qp
->device
);
999 struct mlx4_cmd_mailbox
*mailbox
;
1000 struct mlx4_net_trans_rule_hw_ctrl
*ctrl
;
1003 static const u16 __mlx4_domain
[] = {
1004 [IB_FLOW_DOMAIN_USER
] = MLX4_DOMAIN_UVERBS
,
1005 [IB_FLOW_DOMAIN_ETHTOOL
] = MLX4_DOMAIN_ETHTOOL
,
1006 [IB_FLOW_DOMAIN_RFS
] = MLX4_DOMAIN_RFS
,
1007 [IB_FLOW_DOMAIN_NIC
] = MLX4_DOMAIN_NIC
,
1010 if (flow_attr
->priority
> MLX4_IB_FLOW_MAX_PRIO
) {
1011 pr_err("Invalid priority value %d\n", flow_attr
->priority
);
1015 if (domain
>= IB_FLOW_DOMAIN_NUM
) {
1016 pr_err("Invalid domain value %d\n", domain
);
1020 if (mlx4_map_sw_to_hw_steering_mode(mdev
->dev
, flow_type
) < 0)
1023 mailbox
= mlx4_alloc_cmd_mailbox(mdev
->dev
);
1024 if (IS_ERR(mailbox
))
1025 return PTR_ERR(mailbox
);
1026 ctrl
= mailbox
->buf
;
1028 ctrl
->prio
= cpu_to_be16(__mlx4_domain
[domain
] |
1029 flow_attr
->priority
);
1030 ctrl
->type
= mlx4_map_sw_to_hw_steering_mode(mdev
->dev
, flow_type
);
1031 ctrl
->port
= flow_attr
->port
;
1032 ctrl
->qpn
= cpu_to_be32(qp
->qp_num
);
1034 ib_flow
= flow_attr
+ 1;
1035 size
+= sizeof(struct mlx4_net_trans_rule_hw_ctrl
);
1036 /* Add default flows */
1037 default_flow
= __mlx4_ib_default_rules_match(qp
, flow_attr
);
1038 if (default_flow
>= 0) {
1039 ret
= __mlx4_ib_create_default_rules(
1040 mdev
, qp
, default_table
+ default_flow
,
1041 mailbox
->buf
+ size
);
1043 mlx4_free_cmd_mailbox(mdev
->dev
, mailbox
);
1048 for (i
= 0; i
< flow_attr
->num_of_specs
; i
++) {
1049 ret
= parse_flow_attr(mdev
->dev
, qp
->qp_num
, ib_flow
,
1050 mailbox
->buf
+ size
);
1052 mlx4_free_cmd_mailbox(mdev
->dev
, mailbox
);
1055 ib_flow
+= ((union ib_flow_spec
*) ib_flow
)->size
;
1059 ret
= mlx4_cmd_imm(mdev
->dev
, mailbox
->dma
, reg_id
, size
>> 2, 0,
1060 MLX4_QP_FLOW_STEERING_ATTACH
, MLX4_CMD_TIME_CLASS_A
,
1063 pr_err("mcg table is full. Fail to register network rule.\n");
1064 else if (ret
== -ENXIO
)
1065 pr_err("Device managed flow steering is disabled. Fail to register network rule.\n");
1067 pr_err("Invalid argumant. Fail to register network rule.\n");
1069 mlx4_free_cmd_mailbox(mdev
->dev
, mailbox
);
1073 static int __mlx4_ib_destroy_flow(struct mlx4_dev
*dev
, u64 reg_id
)
1076 err
= mlx4_cmd(dev
, reg_id
, 0, 0,
1077 MLX4_QP_FLOW_STEERING_DETACH
, MLX4_CMD_TIME_CLASS_A
,
1080 pr_err("Fail to detach network rule. registration id = 0x%llx\n",
1085 static struct ib_flow
*mlx4_ib_create_flow(struct ib_qp
*qp
,
1086 struct ib_flow_attr
*flow_attr
,
1090 struct mlx4_ib_flow
*mflow
;
1091 enum mlx4_net_trans_promisc_mode type
[2];
1093 memset(type
, 0, sizeof(type
));
1095 mflow
= kzalloc(sizeof(*mflow
), GFP_KERNEL
);
1101 switch (flow_attr
->type
) {
1102 case IB_FLOW_ATTR_NORMAL
:
1103 type
[0] = MLX4_FS_REGULAR
;
1106 case IB_FLOW_ATTR_ALL_DEFAULT
:
1107 type
[0] = MLX4_FS_ALL_DEFAULT
;
1110 case IB_FLOW_ATTR_MC_DEFAULT
:
1111 type
[0] = MLX4_FS_MC_DEFAULT
;
1114 case IB_FLOW_ATTR_SNIFFER
:
1115 type
[0] = MLX4_FS_UC_SNIFFER
;
1116 type
[1] = MLX4_FS_MC_SNIFFER
;
1124 while (i
< ARRAY_SIZE(type
) && type
[i
]) {
1125 err
= __mlx4_ib_create_flow(qp
, flow_attr
, domain
, type
[i
],
1132 return &mflow
->ibflow
;
1136 return ERR_PTR(err
);
1139 static int mlx4_ib_destroy_flow(struct ib_flow
*flow_id
)
1143 struct mlx4_ib_dev
*mdev
= to_mdev(flow_id
->qp
->device
);
1144 struct mlx4_ib_flow
*mflow
= to_mflow(flow_id
);
1146 while (i
< ARRAY_SIZE(mflow
->reg_id
) && mflow
->reg_id
[i
]) {
1147 err
= __mlx4_ib_destroy_flow(mdev
->dev
, mflow
->reg_id
[i
]);
1157 static int mlx4_ib_mcg_attach(struct ib_qp
*ibqp
, union ib_gid
*gid
, u16 lid
)
1160 struct mlx4_ib_dev
*mdev
= to_mdev(ibqp
->device
);
1161 struct mlx4_ib_qp
*mqp
= to_mqp(ibqp
);
1163 struct mlx4_ib_steering
*ib_steering
= NULL
;
1164 enum mlx4_protocol prot
= (gid
->raw
[1] == 0x0e) ?
1165 MLX4_PROT_IB_IPV4
: MLX4_PROT_IB_IPV6
;
1167 if (mdev
->dev
->caps
.steering_mode
==
1168 MLX4_STEERING_MODE_DEVICE_MANAGED
) {
1169 ib_steering
= kmalloc(sizeof(*ib_steering
), GFP_KERNEL
);
1174 err
= mlx4_multicast_attach(mdev
->dev
, &mqp
->mqp
, gid
->raw
, mqp
->port
,
1176 MLX4_IB_QP_BLOCK_MULTICAST_LOOPBACK
),
1181 err
= add_gid_entry(ibqp
, gid
);
1186 memcpy(ib_steering
->gid
.raw
, gid
->raw
, 16);
1187 ib_steering
->reg_id
= reg_id
;
1188 mutex_lock(&mqp
->mutex
);
1189 list_add(&ib_steering
->list
, &mqp
->steering_rules
);
1190 mutex_unlock(&mqp
->mutex
);
1195 mlx4_multicast_detach(mdev
->dev
, &mqp
->mqp
, gid
->raw
,
1203 static struct mlx4_ib_gid_entry
*find_gid_entry(struct mlx4_ib_qp
*qp
, u8
*raw
)
1205 struct mlx4_ib_gid_entry
*ge
;
1206 struct mlx4_ib_gid_entry
*tmp
;
1207 struct mlx4_ib_gid_entry
*ret
= NULL
;
1209 list_for_each_entry_safe(ge
, tmp
, &qp
->gid_list
, list
) {
1210 if (!memcmp(raw
, ge
->gid
.raw
, 16)) {
1219 static int mlx4_ib_mcg_detach(struct ib_qp
*ibqp
, union ib_gid
*gid
, u16 lid
)
1222 struct mlx4_ib_dev
*mdev
= to_mdev(ibqp
->device
);
1223 struct mlx4_ib_qp
*mqp
= to_mqp(ibqp
);
1224 struct net_device
*ndev
;
1225 struct mlx4_ib_gid_entry
*ge
;
1227 enum mlx4_protocol prot
= (gid
->raw
[1] == 0x0e) ?
1228 MLX4_PROT_IB_IPV4
: MLX4_PROT_IB_IPV6
;
1230 if (mdev
->dev
->caps
.steering_mode
==
1231 MLX4_STEERING_MODE_DEVICE_MANAGED
) {
1232 struct mlx4_ib_steering
*ib_steering
;
1234 mutex_lock(&mqp
->mutex
);
1235 list_for_each_entry(ib_steering
, &mqp
->steering_rules
, list
) {
1236 if (!memcmp(ib_steering
->gid
.raw
, gid
->raw
, 16)) {
1237 list_del(&ib_steering
->list
);
1241 mutex_unlock(&mqp
->mutex
);
1242 if (&ib_steering
->list
== &mqp
->steering_rules
) {
1243 pr_err("Couldn't find reg_id for mgid. Steering rule is left attached\n");
1246 reg_id
= ib_steering
->reg_id
;
1250 err
= mlx4_multicast_detach(mdev
->dev
, &mqp
->mqp
, gid
->raw
,
1255 mutex_lock(&mqp
->mutex
);
1256 ge
= find_gid_entry(mqp
, gid
->raw
);
1258 spin_lock(&mdev
->iboe
.lock
);
1259 ndev
= ge
->added
? mdev
->iboe
.netdevs
[ge
->port
- 1] : NULL
;
1262 spin_unlock(&mdev
->iboe
.lock
);
1265 list_del(&ge
->list
);
1268 pr_warn("could not find mgid entry\n");
1270 mutex_unlock(&mqp
->mutex
);
1275 static int init_node_data(struct mlx4_ib_dev
*dev
)
1277 struct ib_smp
*in_mad
= NULL
;
1278 struct ib_smp
*out_mad
= NULL
;
1279 int mad_ifc_flags
= MLX4_MAD_IFC_IGNORE_KEYS
;
1282 in_mad
= kzalloc(sizeof *in_mad
, GFP_KERNEL
);
1283 out_mad
= kmalloc(sizeof *out_mad
, GFP_KERNEL
);
1284 if (!in_mad
|| !out_mad
)
1287 init_query_mad(in_mad
);
1288 in_mad
->attr_id
= IB_SMP_ATTR_NODE_DESC
;
1289 if (mlx4_is_master(dev
->dev
))
1290 mad_ifc_flags
|= MLX4_MAD_IFC_NET_VIEW
;
1292 err
= mlx4_MAD_IFC(dev
, mad_ifc_flags
, 1, NULL
, NULL
, in_mad
, out_mad
);
1296 memcpy(dev
->ib_dev
.node_desc
, out_mad
->data
, 64);
1298 in_mad
->attr_id
= IB_SMP_ATTR_NODE_INFO
;
1300 err
= mlx4_MAD_IFC(dev
, mad_ifc_flags
, 1, NULL
, NULL
, in_mad
, out_mad
);
1304 dev
->dev
->rev_id
= be32_to_cpup((__be32
*) (out_mad
->data
+ 32));
1305 memcpy(&dev
->ib_dev
.node_guid
, out_mad
->data
+ 12, 8);
1313 static ssize_t
show_hca(struct device
*device
, struct device_attribute
*attr
,
1316 struct mlx4_ib_dev
*dev
=
1317 container_of(device
, struct mlx4_ib_dev
, ib_dev
.dev
);
1318 return sprintf(buf
, "MT%d\n", dev
->dev
->pdev
->device
);
1321 static ssize_t
show_fw_ver(struct device
*device
, struct device_attribute
*attr
,
1324 struct mlx4_ib_dev
*dev
=
1325 container_of(device
, struct mlx4_ib_dev
, ib_dev
.dev
);
1326 return sprintf(buf
, "%d.%d.%d\n", (int) (dev
->dev
->caps
.fw_ver
>> 32),
1327 (int) (dev
->dev
->caps
.fw_ver
>> 16) & 0xffff,
1328 (int) dev
->dev
->caps
.fw_ver
& 0xffff);
1331 static ssize_t
show_rev(struct device
*device
, struct device_attribute
*attr
,
1334 struct mlx4_ib_dev
*dev
=
1335 container_of(device
, struct mlx4_ib_dev
, ib_dev
.dev
);
1336 return sprintf(buf
, "%x\n", dev
->dev
->rev_id
);
1339 static ssize_t
show_board(struct device
*device
, struct device_attribute
*attr
,
1342 struct mlx4_ib_dev
*dev
=
1343 container_of(device
, struct mlx4_ib_dev
, ib_dev
.dev
);
1344 return sprintf(buf
, "%.*s\n", MLX4_BOARD_ID_LEN
,
1345 dev
->dev
->board_id
);
1348 static DEVICE_ATTR(hw_rev
, S_IRUGO
, show_rev
, NULL
);
1349 static DEVICE_ATTR(fw_ver
, S_IRUGO
, show_fw_ver
, NULL
);
1350 static DEVICE_ATTR(hca_type
, S_IRUGO
, show_hca
, NULL
);
1351 static DEVICE_ATTR(board_id
, S_IRUGO
, show_board
, NULL
);
1353 static struct device_attribute
*mlx4_class_attributes
[] = {
1360 static void update_gids_task(struct work_struct
*work
)
1362 struct update_gid_work
*gw
= container_of(work
, struct update_gid_work
, work
);
1363 struct mlx4_cmd_mailbox
*mailbox
;
1366 struct mlx4_dev
*dev
= gw
->dev
->dev
;
1368 mailbox
= mlx4_alloc_cmd_mailbox(dev
);
1369 if (IS_ERR(mailbox
)) {
1370 pr_warn("update gid table failed %ld\n", PTR_ERR(mailbox
));
1374 gids
= mailbox
->buf
;
1375 memcpy(gids
, gw
->gids
, sizeof gw
->gids
);
1377 err
= mlx4_cmd(dev
, mailbox
->dma
, MLX4_SET_PORT_GID_TABLE
<< 8 | gw
->port
,
1378 1, MLX4_CMD_SET_PORT
, MLX4_CMD_TIME_CLASS_B
,
1381 pr_warn("set port command failed\n");
1383 mlx4_ib_dispatch_event(gw
->dev
, gw
->port
, IB_EVENT_GID_CHANGE
);
1385 mlx4_free_cmd_mailbox(dev
, mailbox
);
1389 static void reset_gids_task(struct work_struct
*work
)
1391 struct update_gid_work
*gw
=
1392 container_of(work
, struct update_gid_work
, work
);
1393 struct mlx4_cmd_mailbox
*mailbox
;
1397 struct mlx4_dev
*dev
= gw
->dev
->dev
;
1399 mailbox
= mlx4_alloc_cmd_mailbox(dev
);
1400 if (IS_ERR(mailbox
)) {
1401 pr_warn("reset gid table failed\n");
1405 gids
= mailbox
->buf
;
1406 memcpy(gids
, gw
->gids
, sizeof(gw
->gids
));
1408 for (i
= 1; i
< gw
->dev
->num_ports
+ 1; i
++) {
1409 if (mlx4_ib_port_link_layer(&gw
->dev
->ib_dev
, i
) ==
1410 IB_LINK_LAYER_ETHERNET
) {
1411 err
= mlx4_cmd(dev
, mailbox
->dma
,
1412 MLX4_SET_PORT_GID_TABLE
<< 8 | i
,
1413 1, MLX4_CMD_SET_PORT
,
1414 MLX4_CMD_TIME_CLASS_B
,
1417 pr_warn(KERN_WARNING
1418 "set port %d command failed\n", i
);
1422 mlx4_free_cmd_mailbox(dev
, mailbox
);
1427 static int update_gid_table(struct mlx4_ib_dev
*dev
, int port
,
1428 union ib_gid
*gid
, int clear
)
1430 struct update_gid_work
*work
;
1432 int need_update
= 0;
1437 max_gids
= dev
->dev
->caps
.gid_table_len
[port
];
1438 for (i
= 0; i
< max_gids
; ++i
) {
1439 if (!memcmp(&dev
->iboe
.gid_table
[port
- 1][i
], gid
,
1446 dev
->iboe
.gid_table
[port
- 1][found
] = zgid
;
1454 !memcmp(&dev
->iboe
.gid_table
[port
- 1][i
], &zgid
,
1460 if (found
== -1 && !clear
&& free
>= 0) {
1461 dev
->iboe
.gid_table
[port
- 1][free
] = *gid
;
1468 work
= kzalloc(sizeof(*work
), GFP_ATOMIC
);
1472 memcpy(work
->gids
, dev
->iboe
.gid_table
[port
- 1], sizeof(work
->gids
));
1473 INIT_WORK(&work
->work
, update_gids_task
);
1476 queue_work(wq
, &work
->work
);
1481 static int reset_gid_table(struct mlx4_ib_dev
*dev
)
1483 struct update_gid_work
*work
;
1486 work
= kzalloc(sizeof(*work
), GFP_ATOMIC
);
1489 memset(dev
->iboe
.gid_table
, 0, sizeof(dev
->iboe
.gid_table
));
1490 memset(work
->gids
, 0, sizeof(work
->gids
));
1491 INIT_WORK(&work
->work
, reset_gids_task
);
1493 queue_work(wq
, &work
->work
);
1497 static int mlx4_ib_addr_event(int event
, struct net_device
*event_netdev
,
1498 struct mlx4_ib_dev
*ibdev
, union ib_gid
*gid
)
1500 struct mlx4_ib_iboe
*iboe
;
1502 struct net_device
*real_dev
= rdma_vlan_dev_real_dev(event_netdev
) ?
1503 rdma_vlan_dev_real_dev(event_netdev
) :
1506 if (event
!= NETDEV_DOWN
&& event
!= NETDEV_UP
)
1509 if ((real_dev
!= event_netdev
) &&
1510 (event
== NETDEV_DOWN
) &&
1511 rdma_link_local_addr((struct in6_addr
*)gid
))
1514 iboe
= &ibdev
->iboe
;
1515 spin_lock(&iboe
->lock
);
1517 for (port
= 1; port
<= MLX4_MAX_PORTS
; ++port
)
1518 if ((netif_is_bond_master(real_dev
) &&
1519 (real_dev
== iboe
->masters
[port
- 1])) ||
1520 (!netif_is_bond_master(real_dev
) &&
1521 (real_dev
== iboe
->netdevs
[port
- 1])))
1522 update_gid_table(ibdev
, port
, gid
,
1523 event
== NETDEV_DOWN
);
1525 spin_unlock(&iboe
->lock
);
1530 static u8
mlx4_ib_get_dev_port(struct net_device
*dev
,
1531 struct mlx4_ib_dev
*ibdev
)
1534 struct mlx4_ib_iboe
*iboe
;
1535 struct net_device
*real_dev
= rdma_vlan_dev_real_dev(dev
) ?
1536 rdma_vlan_dev_real_dev(dev
) : dev
;
1538 iboe
= &ibdev
->iboe
;
1539 spin_lock(&iboe
->lock
);
1541 for (port
= 1; port
<= MLX4_MAX_PORTS
; ++port
)
1542 if ((netif_is_bond_master(real_dev
) &&
1543 (real_dev
== iboe
->masters
[port
- 1])) ||
1544 (!netif_is_bond_master(real_dev
) &&
1545 (real_dev
== iboe
->netdevs
[port
- 1])))
1548 spin_unlock(&iboe
->lock
);
1550 if ((port
== 0) || (port
> MLX4_MAX_PORTS
))
1556 static int mlx4_ib_inet_event(struct notifier_block
*this, unsigned long event
,
1559 struct mlx4_ib_dev
*ibdev
;
1560 struct in_ifaddr
*ifa
= ptr
;
1562 struct net_device
*event_netdev
= ifa
->ifa_dev
->dev
;
1564 ipv6_addr_set_v4mapped(ifa
->ifa_address
, (struct in6_addr
*)&gid
);
1566 ibdev
= container_of(this, struct mlx4_ib_dev
, iboe
.nb_inet
);
1568 mlx4_ib_addr_event(event
, event_netdev
, ibdev
, &gid
);
1572 #if IS_ENABLED(CONFIG_IPV6)
1573 static int mlx4_ib_inet6_event(struct notifier_block
*this, unsigned long event
,
1576 struct mlx4_ib_dev
*ibdev
;
1577 struct inet6_ifaddr
*ifa
= ptr
;
1578 union ib_gid
*gid
= (union ib_gid
*)&ifa
->addr
;
1579 struct net_device
*event_netdev
= ifa
->idev
->dev
;
1581 ibdev
= container_of(this, struct mlx4_ib_dev
, iboe
.nb_inet6
);
1583 mlx4_ib_addr_event(event
, event_netdev
, ibdev
, gid
);
1588 static void mlx4_ib_get_dev_addr(struct net_device
*dev
,
1589 struct mlx4_ib_dev
*ibdev
, u8 port
)
1591 struct in_device
*in_dev
;
1592 #if IS_ENABLED(CONFIG_IPV6)
1593 struct inet6_dev
*in6_dev
;
1595 struct inet6_ifaddr
*ifp
;
1600 if ((port
== 0) || (port
> MLX4_MAX_PORTS
))
1604 in_dev
= in_dev_get(dev
);
1607 /*ifa->ifa_address;*/
1608 ipv6_addr_set_v4mapped(ifa
->ifa_address
,
1609 (struct in6_addr
*)&gid
);
1610 update_gid_table(ibdev
, port
, &gid
, 0);
1615 #if IS_ENABLED(CONFIG_IPV6)
1617 in6_dev
= in6_dev_get(dev
);
1619 read_lock_bh(&in6_dev
->lock
);
1620 list_for_each_entry(ifp
, &in6_dev
->addr_list
, if_list
) {
1621 pgid
= (union ib_gid
*)&ifp
->addr
;
1622 update_gid_table(ibdev
, port
, pgid
, 0);
1624 read_unlock_bh(&in6_dev
->lock
);
1625 in6_dev_put(in6_dev
);
1630 static int mlx4_ib_init_gid_table(struct mlx4_ib_dev
*ibdev
)
1632 struct net_device
*dev
;
1634 if (reset_gid_table(ibdev
))
1637 read_lock(&dev_base_lock
);
1639 for_each_netdev(&init_net
, dev
) {
1640 u8 port
= mlx4_ib_get_dev_port(dev
, ibdev
);
1642 mlx4_ib_get_dev_addr(dev
, ibdev
, port
);
1645 read_unlock(&dev_base_lock
);
1650 static void mlx4_ib_scan_netdevs(struct mlx4_ib_dev
*ibdev
)
1652 struct mlx4_ib_iboe
*iboe
;
1655 iboe
= &ibdev
->iboe
;
1657 spin_lock(&iboe
->lock
);
1658 mlx4_foreach_ib_transport_port(port
, ibdev
->dev
) {
1659 struct net_device
*old_master
= iboe
->masters
[port
- 1];
1660 struct net_device
*curr_master
;
1661 iboe
->netdevs
[port
- 1] =
1662 mlx4_get_protocol_dev(ibdev
->dev
, MLX4_PROT_ETH
, port
);
1664 if (iboe
->netdevs
[port
- 1] &&
1665 netif_is_bond_slave(iboe
->netdevs
[port
- 1])) {
1667 iboe
->masters
[port
- 1] = netdev_master_upper_dev_get(
1668 iboe
->netdevs
[port
- 1]);
1671 curr_master
= iboe
->masters
[port
- 1];
1673 /* if bonding is used it is possible that we add it to masters
1674 only after IP address is assigned to the net bonding
1676 if (curr_master
&& (old_master
!= curr_master
))
1677 mlx4_ib_get_dev_addr(curr_master
, ibdev
, port
);
1680 spin_unlock(&iboe
->lock
);
1683 static int mlx4_ib_netdev_event(struct notifier_block
*this,
1684 unsigned long event
, void *ptr
)
1686 struct net_device
*dev
= netdev_notifier_info_to_dev(ptr
);
1687 struct mlx4_ib_dev
*ibdev
;
1689 if (!net_eq(dev_net(dev
), &init_net
))
1692 ibdev
= container_of(this, struct mlx4_ib_dev
, iboe
.nb
);
1693 mlx4_ib_scan_netdevs(ibdev
);
1698 static void init_pkeys(struct mlx4_ib_dev
*ibdev
)
1704 if (mlx4_is_master(ibdev
->dev
)) {
1705 for (slave
= 0; slave
<= ibdev
->dev
->num_vfs
; ++slave
) {
1706 for (port
= 1; port
<= ibdev
->dev
->caps
.num_ports
; ++port
) {
1708 i
< ibdev
->dev
->phys_caps
.pkey_phys_table_len
[port
];
1710 ibdev
->pkeys
.virt2phys_pkey
[slave
][port
- 1][i
] =
1711 /* master has the identity virt2phys pkey mapping */
1712 (slave
== mlx4_master_func_num(ibdev
->dev
) || !i
) ? i
:
1713 ibdev
->dev
->phys_caps
.pkey_phys_table_len
[port
] - 1;
1714 mlx4_sync_pkey_table(ibdev
->dev
, slave
, port
, i
,
1715 ibdev
->pkeys
.virt2phys_pkey
[slave
][port
- 1][i
]);
1719 /* initialize pkey cache */
1720 for (port
= 1; port
<= ibdev
->dev
->caps
.num_ports
; ++port
) {
1722 i
< ibdev
->dev
->phys_caps
.pkey_phys_table_len
[port
];
1724 ibdev
->pkeys
.phys_pkey_cache
[port
-1][i
] =
1730 static void mlx4_ib_alloc_eqs(struct mlx4_dev
*dev
, struct mlx4_ib_dev
*ibdev
)
1733 int eq_per_port
= 0;
1738 /* Legacy mode or comp_pool is not large enough */
1739 if (dev
->caps
.comp_pool
== 0 ||
1740 dev
->caps
.num_ports
> dev
->caps
.comp_pool
)
1743 eq_per_port
= rounddown_pow_of_two(dev
->caps
.comp_pool
/
1744 dev
->caps
.num_ports
);
1748 mlx4_foreach_port(i
, dev
, MLX4_PORT_TYPE_IB
)
1749 added_eqs
+= eq_per_port
;
1751 total_eqs
= dev
->caps
.num_comp_vectors
+ added_eqs
;
1753 ibdev
->eq_table
= kzalloc(total_eqs
* sizeof(int), GFP_KERNEL
);
1754 if (!ibdev
->eq_table
)
1757 ibdev
->eq_added
= added_eqs
;
1760 mlx4_foreach_port(i
, dev
, MLX4_PORT_TYPE_IB
) {
1761 for (j
= 0; j
< eq_per_port
; j
++) {
1762 sprintf(name
, "mlx4-ib-%d-%d@%s",
1763 i
, j
, dev
->pdev
->bus
->name
);
1764 /* Set IRQ for specific name (per ring) */
1765 if (mlx4_assign_eq(dev
, name
, NULL
,
1766 &ibdev
->eq_table
[eq
])) {
1767 /* Use legacy (same as mlx4_en driver) */
1768 pr_warn("Can't allocate EQ %d; reverting to legacy\n", eq
);
1769 ibdev
->eq_table
[eq
] =
1770 (eq
% dev
->caps
.num_comp_vectors
);
1776 /* Fill the reset of the vector with legacy EQ */
1777 for (i
= 0, eq
= added_eqs
; i
< dev
->caps
.num_comp_vectors
; i
++)
1778 ibdev
->eq_table
[eq
++] = i
;
1780 /* Advertise the new number of EQs to clients */
1781 ibdev
->ib_dev
.num_comp_vectors
= total_eqs
;
1784 static void mlx4_ib_free_eqs(struct mlx4_dev
*dev
, struct mlx4_ib_dev
*ibdev
)
1788 /* no additional eqs were added */
1789 if (!ibdev
->eq_table
)
1792 /* Reset the advertised EQ number */
1793 ibdev
->ib_dev
.num_comp_vectors
= dev
->caps
.num_comp_vectors
;
1795 /* Free only the added eqs */
1796 for (i
= 0; i
< ibdev
->eq_added
; i
++) {
1797 /* Don't free legacy eqs if used */
1798 if (ibdev
->eq_table
[i
] <= dev
->caps
.num_comp_vectors
)
1800 mlx4_release_eq(dev
, ibdev
->eq_table
[i
]);
1803 kfree(ibdev
->eq_table
);
1806 static void *mlx4_ib_add(struct mlx4_dev
*dev
)
1808 struct mlx4_ib_dev
*ibdev
;
1812 struct mlx4_ib_iboe
*iboe
;
1814 pr_info_once("%s", mlx4_ib_version
);
1816 mlx4_foreach_non_ib_transport_port(i
, dev
)
1819 if (mlx4_is_mfunc(dev
) && num_ports
) {
1820 dev_err(&dev
->pdev
->dev
, "RoCE is not supported over SRIOV as yet\n");
1825 mlx4_foreach_ib_transport_port(i
, dev
)
1828 /* No point in registering a device with no ports... */
1832 ibdev
= (struct mlx4_ib_dev
*) ib_alloc_device(sizeof *ibdev
);
1834 dev_err(&dev
->pdev
->dev
, "Device struct alloc failed\n");
1838 iboe
= &ibdev
->iboe
;
1840 if (mlx4_pd_alloc(dev
, &ibdev
->priv_pdn
))
1843 if (mlx4_uar_alloc(dev
, &ibdev
->priv_uar
))
1846 ibdev
->uar_map
= ioremap((phys_addr_t
) ibdev
->priv_uar
.pfn
<< PAGE_SHIFT
,
1848 if (!ibdev
->uar_map
)
1850 MLX4_INIT_DOORBELL_LOCK(&ibdev
->uar_lock
);
1854 strlcpy(ibdev
->ib_dev
.name
, "mlx4_%d", IB_DEVICE_NAME_MAX
);
1855 ibdev
->ib_dev
.owner
= THIS_MODULE
;
1856 ibdev
->ib_dev
.node_type
= RDMA_NODE_IB_CA
;
1857 ibdev
->ib_dev
.local_dma_lkey
= dev
->caps
.reserved_lkey
;
1858 ibdev
->num_ports
= num_ports
;
1859 ibdev
->ib_dev
.phys_port_cnt
= ibdev
->num_ports
;
1860 ibdev
->ib_dev
.num_comp_vectors
= dev
->caps
.num_comp_vectors
;
1861 ibdev
->ib_dev
.dma_device
= &dev
->pdev
->dev
;
1863 if (dev
->caps
.userspace_caps
)
1864 ibdev
->ib_dev
.uverbs_abi_ver
= MLX4_IB_UVERBS_ABI_VERSION
;
1866 ibdev
->ib_dev
.uverbs_abi_ver
= MLX4_IB_UVERBS_NO_DEV_CAPS_ABI_VERSION
;
1868 ibdev
->ib_dev
.uverbs_cmd_mask
=
1869 (1ull << IB_USER_VERBS_CMD_GET_CONTEXT
) |
1870 (1ull << IB_USER_VERBS_CMD_QUERY_DEVICE
) |
1871 (1ull << IB_USER_VERBS_CMD_QUERY_PORT
) |
1872 (1ull << IB_USER_VERBS_CMD_ALLOC_PD
) |
1873 (1ull << IB_USER_VERBS_CMD_DEALLOC_PD
) |
1874 (1ull << IB_USER_VERBS_CMD_REG_MR
) |
1875 (1ull << IB_USER_VERBS_CMD_DEREG_MR
) |
1876 (1ull << IB_USER_VERBS_CMD_CREATE_COMP_CHANNEL
) |
1877 (1ull << IB_USER_VERBS_CMD_CREATE_CQ
) |
1878 (1ull << IB_USER_VERBS_CMD_RESIZE_CQ
) |
1879 (1ull << IB_USER_VERBS_CMD_DESTROY_CQ
) |
1880 (1ull << IB_USER_VERBS_CMD_CREATE_QP
) |
1881 (1ull << IB_USER_VERBS_CMD_MODIFY_QP
) |
1882 (1ull << IB_USER_VERBS_CMD_QUERY_QP
) |
1883 (1ull << IB_USER_VERBS_CMD_DESTROY_QP
) |
1884 (1ull << IB_USER_VERBS_CMD_ATTACH_MCAST
) |
1885 (1ull << IB_USER_VERBS_CMD_DETACH_MCAST
) |
1886 (1ull << IB_USER_VERBS_CMD_CREATE_SRQ
) |
1887 (1ull << IB_USER_VERBS_CMD_MODIFY_SRQ
) |
1888 (1ull << IB_USER_VERBS_CMD_QUERY_SRQ
) |
1889 (1ull << IB_USER_VERBS_CMD_DESTROY_SRQ
) |
1890 (1ull << IB_USER_VERBS_CMD_CREATE_XSRQ
) |
1891 (1ull << IB_USER_VERBS_CMD_OPEN_QP
);
1893 ibdev
->ib_dev
.query_device
= mlx4_ib_query_device
;
1894 ibdev
->ib_dev
.query_port
= mlx4_ib_query_port
;
1895 ibdev
->ib_dev
.get_link_layer
= mlx4_ib_port_link_layer
;
1896 ibdev
->ib_dev
.query_gid
= mlx4_ib_query_gid
;
1897 ibdev
->ib_dev
.query_pkey
= mlx4_ib_query_pkey
;
1898 ibdev
->ib_dev
.modify_device
= mlx4_ib_modify_device
;
1899 ibdev
->ib_dev
.modify_port
= mlx4_ib_modify_port
;
1900 ibdev
->ib_dev
.alloc_ucontext
= mlx4_ib_alloc_ucontext
;
1901 ibdev
->ib_dev
.dealloc_ucontext
= mlx4_ib_dealloc_ucontext
;
1902 ibdev
->ib_dev
.mmap
= mlx4_ib_mmap
;
1903 ibdev
->ib_dev
.alloc_pd
= mlx4_ib_alloc_pd
;
1904 ibdev
->ib_dev
.dealloc_pd
= mlx4_ib_dealloc_pd
;
1905 ibdev
->ib_dev
.create_ah
= mlx4_ib_create_ah
;
1906 ibdev
->ib_dev
.query_ah
= mlx4_ib_query_ah
;
1907 ibdev
->ib_dev
.destroy_ah
= mlx4_ib_destroy_ah
;
1908 ibdev
->ib_dev
.create_srq
= mlx4_ib_create_srq
;
1909 ibdev
->ib_dev
.modify_srq
= mlx4_ib_modify_srq
;
1910 ibdev
->ib_dev
.query_srq
= mlx4_ib_query_srq
;
1911 ibdev
->ib_dev
.destroy_srq
= mlx4_ib_destroy_srq
;
1912 ibdev
->ib_dev
.post_srq_recv
= mlx4_ib_post_srq_recv
;
1913 ibdev
->ib_dev
.create_qp
= mlx4_ib_create_qp
;
1914 ibdev
->ib_dev
.modify_qp
= mlx4_ib_modify_qp
;
1915 ibdev
->ib_dev
.query_qp
= mlx4_ib_query_qp
;
1916 ibdev
->ib_dev
.destroy_qp
= mlx4_ib_destroy_qp
;
1917 ibdev
->ib_dev
.post_send
= mlx4_ib_post_send
;
1918 ibdev
->ib_dev
.post_recv
= mlx4_ib_post_recv
;
1919 ibdev
->ib_dev
.create_cq
= mlx4_ib_create_cq
;
1920 ibdev
->ib_dev
.modify_cq
= mlx4_ib_modify_cq
;
1921 ibdev
->ib_dev
.resize_cq
= mlx4_ib_resize_cq
;
1922 ibdev
->ib_dev
.destroy_cq
= mlx4_ib_destroy_cq
;
1923 ibdev
->ib_dev
.poll_cq
= mlx4_ib_poll_cq
;
1924 ibdev
->ib_dev
.req_notify_cq
= mlx4_ib_arm_cq
;
1925 ibdev
->ib_dev
.get_dma_mr
= mlx4_ib_get_dma_mr
;
1926 ibdev
->ib_dev
.reg_user_mr
= mlx4_ib_reg_user_mr
;
1927 ibdev
->ib_dev
.dereg_mr
= mlx4_ib_dereg_mr
;
1928 ibdev
->ib_dev
.alloc_fast_reg_mr
= mlx4_ib_alloc_fast_reg_mr
;
1929 ibdev
->ib_dev
.alloc_fast_reg_page_list
= mlx4_ib_alloc_fast_reg_page_list
;
1930 ibdev
->ib_dev
.free_fast_reg_page_list
= mlx4_ib_free_fast_reg_page_list
;
1931 ibdev
->ib_dev
.attach_mcast
= mlx4_ib_mcg_attach
;
1932 ibdev
->ib_dev
.detach_mcast
= mlx4_ib_mcg_detach
;
1933 ibdev
->ib_dev
.process_mad
= mlx4_ib_process_mad
;
1935 if (!mlx4_is_slave(ibdev
->dev
)) {
1936 ibdev
->ib_dev
.alloc_fmr
= mlx4_ib_fmr_alloc
;
1937 ibdev
->ib_dev
.map_phys_fmr
= mlx4_ib_map_phys_fmr
;
1938 ibdev
->ib_dev
.unmap_fmr
= mlx4_ib_unmap_fmr
;
1939 ibdev
->ib_dev
.dealloc_fmr
= mlx4_ib_fmr_dealloc
;
1942 if (dev
->caps
.flags
& MLX4_DEV_CAP_FLAG_MEM_WINDOW
||
1943 dev
->caps
.bmme_flags
& MLX4_BMME_FLAG_TYPE_2_WIN
) {
1944 ibdev
->ib_dev
.alloc_mw
= mlx4_ib_alloc_mw
;
1945 ibdev
->ib_dev
.bind_mw
= mlx4_ib_bind_mw
;
1946 ibdev
->ib_dev
.dealloc_mw
= mlx4_ib_dealloc_mw
;
1948 ibdev
->ib_dev
.uverbs_cmd_mask
|=
1949 (1ull << IB_USER_VERBS_CMD_ALLOC_MW
) |
1950 (1ull << IB_USER_VERBS_CMD_DEALLOC_MW
);
1953 if (dev
->caps
.flags
& MLX4_DEV_CAP_FLAG_XRC
) {
1954 ibdev
->ib_dev
.alloc_xrcd
= mlx4_ib_alloc_xrcd
;
1955 ibdev
->ib_dev
.dealloc_xrcd
= mlx4_ib_dealloc_xrcd
;
1956 ibdev
->ib_dev
.uverbs_cmd_mask
|=
1957 (1ull << IB_USER_VERBS_CMD_OPEN_XRCD
) |
1958 (1ull << IB_USER_VERBS_CMD_CLOSE_XRCD
);
1961 if (check_flow_steering_support(dev
)) {
1962 ibdev
->steering_support
= MLX4_STEERING_MODE_DEVICE_MANAGED
;
1963 ibdev
->ib_dev
.create_flow
= mlx4_ib_create_flow
;
1964 ibdev
->ib_dev
.destroy_flow
= mlx4_ib_destroy_flow
;
1966 ibdev
->ib_dev
.uverbs_ex_cmd_mask
|=
1967 (1ull << IB_USER_VERBS_EX_CMD_CREATE_FLOW
) |
1968 (1ull << IB_USER_VERBS_EX_CMD_DESTROY_FLOW
);
1971 mlx4_ib_alloc_eqs(dev
, ibdev
);
1973 spin_lock_init(&iboe
->lock
);
1975 if (init_node_data(ibdev
))
1978 for (i
= 0; i
< ibdev
->num_ports
; ++i
) {
1979 if (mlx4_ib_port_link_layer(&ibdev
->ib_dev
, i
+ 1) ==
1980 IB_LINK_LAYER_ETHERNET
) {
1981 err
= mlx4_counter_alloc(ibdev
->dev
, &ibdev
->counters
[i
]);
1983 ibdev
->counters
[i
] = -1;
1985 ibdev
->counters
[i
] = -1;
1988 spin_lock_init(&ibdev
->sm_lock
);
1989 mutex_init(&ibdev
->cap_mask_mutex
);
1991 if (ibdev
->steering_support
== MLX4_STEERING_MODE_DEVICE_MANAGED
) {
1992 ibdev
->steer_qpn_count
= MLX4_IB_UC_MAX_NUM_QPS
;
1993 err
= mlx4_qp_reserve_range(dev
, ibdev
->steer_qpn_count
,
1994 MLX4_IB_UC_STEER_QPN_ALIGN
,
1995 &ibdev
->steer_qpn_base
);
1999 ibdev
->ib_uc_qpns_bitmap
=
2000 kmalloc(BITS_TO_LONGS(ibdev
->steer_qpn_count
) *
2003 if (!ibdev
->ib_uc_qpns_bitmap
) {
2004 dev_err(&dev
->pdev
->dev
, "bit map alloc failed\n");
2005 goto err_steer_qp_release
;
2008 bitmap_zero(ibdev
->ib_uc_qpns_bitmap
, ibdev
->steer_qpn_count
);
2010 err
= mlx4_FLOW_STEERING_IB_UC_QP_RANGE(
2011 dev
, ibdev
->steer_qpn_base
,
2012 ibdev
->steer_qpn_base
+
2013 ibdev
->steer_qpn_count
- 1);
2015 goto err_steer_free_bitmap
;
2018 if (ib_register_device(&ibdev
->ib_dev
, NULL
))
2019 goto err_steer_free_bitmap
;
2021 if (mlx4_ib_mad_init(ibdev
))
2024 if (mlx4_ib_init_sriov(ibdev
))
2027 if (dev
->caps
.flags
& MLX4_DEV_CAP_FLAG_IBOE
) {
2028 if (!iboe
->nb
.notifier_call
) {
2029 iboe
->nb
.notifier_call
= mlx4_ib_netdev_event
;
2030 err
= register_netdevice_notifier(&iboe
->nb
);
2032 iboe
->nb
.notifier_call
= NULL
;
2036 if (!iboe
->nb_inet
.notifier_call
) {
2037 iboe
->nb_inet
.notifier_call
= mlx4_ib_inet_event
;
2038 err
= register_inetaddr_notifier(&iboe
->nb_inet
);
2040 iboe
->nb_inet
.notifier_call
= NULL
;
2044 #if IS_ENABLED(CONFIG_IPV6)
2045 if (!iboe
->nb_inet6
.notifier_call
) {
2046 iboe
->nb_inet6
.notifier_call
= mlx4_ib_inet6_event
;
2047 err
= register_inet6addr_notifier(&iboe
->nb_inet6
);
2049 iboe
->nb_inet6
.notifier_call
= NULL
;
2054 mlx4_ib_scan_netdevs(ibdev
);
2055 mlx4_ib_init_gid_table(ibdev
);
2058 for (j
= 0; j
< ARRAY_SIZE(mlx4_class_attributes
); ++j
) {
2059 if (device_create_file(&ibdev
->ib_dev
.dev
,
2060 mlx4_class_attributes
[j
]))
2064 ibdev
->ib_active
= true;
2066 if (mlx4_is_mfunc(ibdev
->dev
))
2069 /* create paravirt contexts for any VFs which are active */
2070 if (mlx4_is_master(ibdev
->dev
)) {
2071 for (j
= 0; j
< MLX4_MFUNC_MAX
; j
++) {
2072 if (j
== mlx4_master_func_num(ibdev
->dev
))
2074 if (mlx4_is_slave_active(ibdev
->dev
, j
))
2075 do_slave_init(ibdev
, j
, 1);
2081 if (ibdev
->iboe
.nb
.notifier_call
) {
2082 if (unregister_netdevice_notifier(&ibdev
->iboe
.nb
))
2083 pr_warn("failure unregistering notifier\n");
2084 ibdev
->iboe
.nb
.notifier_call
= NULL
;
2086 if (ibdev
->iboe
.nb_inet
.notifier_call
) {
2087 if (unregister_inetaddr_notifier(&ibdev
->iboe
.nb_inet
))
2088 pr_warn("failure unregistering notifier\n");
2089 ibdev
->iboe
.nb_inet
.notifier_call
= NULL
;
2091 #if IS_ENABLED(CONFIG_IPV6)
2092 if (ibdev
->iboe
.nb_inet6
.notifier_call
) {
2093 if (unregister_inet6addr_notifier(&ibdev
->iboe
.nb_inet6
))
2094 pr_warn("failure unregistering notifier\n");
2095 ibdev
->iboe
.nb_inet6
.notifier_call
= NULL
;
2098 flush_workqueue(wq
);
2100 mlx4_ib_close_sriov(ibdev
);
2103 mlx4_ib_mad_cleanup(ibdev
);
2106 ib_unregister_device(&ibdev
->ib_dev
);
2108 err_steer_free_bitmap
:
2109 kfree(ibdev
->ib_uc_qpns_bitmap
);
2111 err_steer_qp_release
:
2112 if (ibdev
->steering_support
== MLX4_STEERING_MODE_DEVICE_MANAGED
)
2113 mlx4_qp_release_range(dev
, ibdev
->steer_qpn_base
,
2114 ibdev
->steer_qpn_count
);
2117 if (ibdev
->counters
[i
- 1] != -1)
2118 mlx4_counter_free(ibdev
->dev
, ibdev
->counters
[i
- 1]);
2121 iounmap(ibdev
->uar_map
);
2124 mlx4_uar_free(dev
, &ibdev
->priv_uar
);
2127 mlx4_pd_free(dev
, ibdev
->priv_pdn
);
2130 ib_dealloc_device(&ibdev
->ib_dev
);
2135 int mlx4_ib_steer_qp_alloc(struct mlx4_ib_dev
*dev
, int count
, int *qpn
)
2139 WARN_ON(!dev
->ib_uc_qpns_bitmap
);
2141 offset
= bitmap_find_free_region(dev
->ib_uc_qpns_bitmap
,
2142 dev
->steer_qpn_count
,
2143 get_count_order(count
));
2147 *qpn
= dev
->steer_qpn_base
+ offset
;
2151 void mlx4_ib_steer_qp_free(struct mlx4_ib_dev
*dev
, u32 qpn
, int count
)
2154 dev
->steering_support
!= MLX4_STEERING_MODE_DEVICE_MANAGED
)
2157 BUG_ON(qpn
< dev
->steer_qpn_base
);
2159 bitmap_release_region(dev
->ib_uc_qpns_bitmap
,
2160 qpn
- dev
->steer_qpn_base
,
2161 get_count_order(count
));
2164 int mlx4_ib_steer_qp_reg(struct mlx4_ib_dev
*mdev
, struct mlx4_ib_qp
*mqp
,
2169 struct ib_flow_attr
*flow
= NULL
;
2170 struct ib_flow_spec_ib
*ib_spec
;
2173 flow_size
= sizeof(struct ib_flow_attr
) +
2174 sizeof(struct ib_flow_spec_ib
);
2175 flow
= kzalloc(flow_size
, GFP_KERNEL
);
2178 flow
->port
= mqp
->port
;
2179 flow
->num_of_specs
= 1;
2180 flow
->size
= flow_size
;
2181 ib_spec
= (struct ib_flow_spec_ib
*)(flow
+ 1);
2182 ib_spec
->type
= IB_FLOW_SPEC_IB
;
2183 ib_spec
->size
= sizeof(struct ib_flow_spec_ib
);
2184 /* Add an empty rule for IB L2 */
2185 memset(&ib_spec
->mask
, 0, sizeof(ib_spec
->mask
));
2187 err
= __mlx4_ib_create_flow(&mqp
->ibqp
, flow
,
2192 err
= __mlx4_ib_destroy_flow(mdev
->dev
, mqp
->reg_id
);
2198 static void mlx4_ib_remove(struct mlx4_dev
*dev
, void *ibdev_ptr
)
2200 struct mlx4_ib_dev
*ibdev
= ibdev_ptr
;
2203 mlx4_ib_close_sriov(ibdev
);
2204 mlx4_ib_mad_cleanup(ibdev
);
2205 ib_unregister_device(&ibdev
->ib_dev
);
2206 if (ibdev
->iboe
.nb
.notifier_call
) {
2207 if (unregister_netdevice_notifier(&ibdev
->iboe
.nb
))
2208 pr_warn("failure unregistering notifier\n");
2209 ibdev
->iboe
.nb
.notifier_call
= NULL
;
2212 if (ibdev
->steering_support
== MLX4_STEERING_MODE_DEVICE_MANAGED
) {
2213 mlx4_qp_release_range(dev
, ibdev
->steer_qpn_base
,
2214 ibdev
->steer_qpn_count
);
2215 kfree(ibdev
->ib_uc_qpns_bitmap
);
2218 if (ibdev
->iboe
.nb_inet
.notifier_call
) {
2219 if (unregister_inetaddr_notifier(&ibdev
->iboe
.nb_inet
))
2220 pr_warn("failure unregistering notifier\n");
2221 ibdev
->iboe
.nb_inet
.notifier_call
= NULL
;
2223 #if IS_ENABLED(CONFIG_IPV6)
2224 if (ibdev
->iboe
.nb_inet6
.notifier_call
) {
2225 if (unregister_inet6addr_notifier(&ibdev
->iboe
.nb_inet6
))
2226 pr_warn("failure unregistering notifier\n");
2227 ibdev
->iboe
.nb_inet6
.notifier_call
= NULL
;
2231 iounmap(ibdev
->uar_map
);
2232 for (p
= 0; p
< ibdev
->num_ports
; ++p
)
2233 if (ibdev
->counters
[p
] != -1)
2234 mlx4_counter_free(ibdev
->dev
, ibdev
->counters
[p
]);
2235 mlx4_foreach_port(p
, dev
, MLX4_PORT_TYPE_IB
)
2236 mlx4_CLOSE_PORT(dev
, p
);
2238 mlx4_ib_free_eqs(dev
, ibdev
);
2240 mlx4_uar_free(dev
, &ibdev
->priv_uar
);
2241 mlx4_pd_free(dev
, ibdev
->priv_pdn
);
2242 ib_dealloc_device(&ibdev
->ib_dev
);
2245 static void do_slave_init(struct mlx4_ib_dev
*ibdev
, int slave
, int do_init
)
2247 struct mlx4_ib_demux_work
**dm
= NULL
;
2248 struct mlx4_dev
*dev
= ibdev
->dev
;
2250 unsigned long flags
;
2252 if (!mlx4_is_master(dev
))
2255 dm
= kcalloc(dev
->caps
.num_ports
, sizeof *dm
, GFP_ATOMIC
);
2257 pr_err("failed to allocate memory for tunneling qp update\n");
2261 for (i
= 0; i
< dev
->caps
.num_ports
; i
++) {
2262 dm
[i
] = kmalloc(sizeof (struct mlx4_ib_demux_work
), GFP_ATOMIC
);
2264 pr_err("failed to allocate memory for tunneling qp update work struct\n");
2265 for (i
= 0; i
< dev
->caps
.num_ports
; i
++) {
2272 /* initialize or tear down tunnel QPs for the slave */
2273 for (i
= 0; i
< dev
->caps
.num_ports
; i
++) {
2274 INIT_WORK(&dm
[i
]->work
, mlx4_ib_tunnels_update_work
);
2275 dm
[i
]->port
= i
+ 1;
2276 dm
[i
]->slave
= slave
;
2277 dm
[i
]->do_init
= do_init
;
2279 spin_lock_irqsave(&ibdev
->sriov
.going_down_lock
, flags
);
2280 if (!ibdev
->sriov
.is_going_down
)
2281 queue_work(ibdev
->sriov
.demux
[i
].ud_wq
, &dm
[i
]->work
);
2282 spin_unlock_irqrestore(&ibdev
->sriov
.going_down_lock
, flags
);
2289 static void mlx4_ib_event(struct mlx4_dev
*dev
, void *ibdev_ptr
,
2290 enum mlx4_dev_event event
, unsigned long param
)
2292 struct ib_event ibev
;
2293 struct mlx4_ib_dev
*ibdev
= to_mdev((struct ib_device
*) ibdev_ptr
);
2294 struct mlx4_eqe
*eqe
= NULL
;
2295 struct ib_event_work
*ew
;
2298 if (event
== MLX4_DEV_EVENT_PORT_MGMT_CHANGE
)
2299 eqe
= (struct mlx4_eqe
*)param
;
2304 case MLX4_DEV_EVENT_PORT_UP
:
2305 if (p
> ibdev
->num_ports
)
2307 if (mlx4_is_master(dev
) &&
2308 rdma_port_get_link_layer(&ibdev
->ib_dev
, p
) ==
2309 IB_LINK_LAYER_INFINIBAND
) {
2310 mlx4_ib_invalidate_all_guid_record(ibdev
, p
);
2312 ibev
.event
= IB_EVENT_PORT_ACTIVE
;
2315 case MLX4_DEV_EVENT_PORT_DOWN
:
2316 if (p
> ibdev
->num_ports
)
2318 ibev
.event
= IB_EVENT_PORT_ERR
;
2321 case MLX4_DEV_EVENT_CATASTROPHIC_ERROR
:
2322 ibdev
->ib_active
= false;
2323 ibev
.event
= IB_EVENT_DEVICE_FATAL
;
2326 case MLX4_DEV_EVENT_PORT_MGMT_CHANGE
:
2327 ew
= kmalloc(sizeof *ew
, GFP_ATOMIC
);
2329 pr_err("failed to allocate memory for events work\n");
2333 INIT_WORK(&ew
->work
, handle_port_mgmt_change_event
);
2334 memcpy(&ew
->ib_eqe
, eqe
, sizeof *eqe
);
2336 /* need to queue only for port owner, which uses GEN_EQE */
2337 if (mlx4_is_master(dev
))
2338 queue_work(wq
, &ew
->work
);
2340 handle_port_mgmt_change_event(&ew
->work
);
2343 case MLX4_DEV_EVENT_SLAVE_INIT
:
2344 /* here, p is the slave id */
2345 do_slave_init(ibdev
, p
, 1);
2348 case MLX4_DEV_EVENT_SLAVE_SHUTDOWN
:
2349 /* here, p is the slave id */
2350 do_slave_init(ibdev
, p
, 0);
2357 ibev
.device
= ibdev_ptr
;
2358 ibev
.element
.port_num
= (u8
) p
;
2360 ib_dispatch_event(&ibev
);
2363 static struct mlx4_interface mlx4_ib_interface
= {
2365 .remove
= mlx4_ib_remove
,
2366 .event
= mlx4_ib_event
,
2367 .protocol
= MLX4_PROT_IB_IPV6
2370 static int __init
mlx4_ib_init(void)
2374 wq
= create_singlethread_workqueue("mlx4_ib");
2378 err
= mlx4_ib_mcg_init();
2382 err
= mlx4_register_interface(&mlx4_ib_interface
);
2389 mlx4_ib_mcg_destroy();
2392 destroy_workqueue(wq
);
2396 static void __exit
mlx4_ib_cleanup(void)
2398 mlx4_unregister_interface(&mlx4_ib_interface
);
2399 mlx4_ib_mcg_destroy();
2400 destroy_workqueue(wq
);
2403 module_init(mlx4_ib_init
);
2404 module_exit(mlx4_ib_cleanup
);