blob: 02da3f58f29636f6d2df4457879c672909dd3388 [file] [log] [blame]
Eli Cohene126ba92013-07-07 17:25:49 +03001/*
Saeed Mahameed6cf0a152015-04-02 17:07:30 +03002 * Copyright (c) 2013-2015, Mellanox Technologies. All rights reserved.
Eli Cohene126ba92013-07-07 17:25:49 +03003 *
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:
9 *
10 * Redistribution and use in source and binary forms, with or
11 * without modification, are permitted provided that the following
12 * conditions are met:
13 *
14 * - Redistributions of source code must retain the above
15 * copyright notice, this list of conditions and the following
16 * disclaimer.
17 *
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.
22 *
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
30 * SOFTWARE.
31 */
32
Maor Gottliebfe248c32017-05-30 10:29:14 +030033#include <linux/debugfs.h>
Christoph Hellwigadec6402015-08-28 09:27:19 +020034#include <linux/highmem.h>
Eli Cohene126ba92013-07-07 17:25:49 +030035#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>
Guy Levi37aa5c32016-04-27 16:49:50 +030041#if defined(CONFIG_X86)
42#include <asm/pat.h>
43#endif
Eli Cohene126ba92013-07-07 17:25:49 +030044#include <linux/sched.h>
Ingo Molnar6e84f312017-02-08 18:51:29 +010045#include <linux/sched/mm.h>
Ingo Molnar0881e7b2017-02-05 15:30:50 +010046#include <linux/sched/task.h>
Maor Gottlieb7c2344c2016-06-17 14:56:44 +030047#include <linux/delay.h>
Eli Cohene126ba92013-07-07 17:25:49 +030048#include <rdma/ib_user_verbs.h>
Achiad Shochat3f89a642015-12-23 18:47:21 +020049#include <rdma/ib_addr.h>
Achiad Shochat2811ba52015-12-23 18:47:24 +020050#include <rdma/ib_cache.h>
Achiad Shochatada68c32016-02-22 18:17:23 +020051#include <linux/mlx5/port.h>
Majd Dibbiny1b5daf12015-06-04 19:30:46 +030052#include <linux/mlx5/vport.h>
Maor Gottlieb7c2344c2016-06-17 14:56:44 +030053#include <linux/list.h>
Eli Cohene126ba92013-07-07 17:25:49 +030054#include <rdma/ib_smi.h>
55#include <rdma/ib_umem.h>
Maor Gottlieb038d2ef2016-01-11 10:26:07 +020056#include <linux/in.h>
57#include <linux/etherdevice.h>
58#include <linux/mlx5/fs.h>
Or Gerlitz78984892016-11-30 20:33:33 +020059#include <linux/mlx5/vport.h>
Eli Cohene126ba92013-07-07 17:25:49 +030060#include "mlx5_ib.h"
Parav Pandite1f24a72017-04-16 07:29:29 +030061#include "cmd.h"
Huy Nguyenc85023e2017-05-30 09:42:54 +030062#include <linux/mlx5/vport.h>
Eli Cohene126ba92013-07-07 17:25:49 +030063
64#define DRIVER_NAME "mlx5_ib"
Tariq Toukanb3599112017-02-22 17:45:46 +020065#define DRIVER_VERSION "5.0-0"
Eli Cohene126ba92013-07-07 17:25:49 +030066
67MODULE_AUTHOR("Eli Cohen <eli@mellanox.com>");
68MODULE_DESCRIPTION("Mellanox Connect-IB HCA IB driver");
69MODULE_LICENSE("Dual BSD/GPL");
Eli Cohene126ba92013-07-07 17:25:49 +030070
Eli Cohene126ba92013-07-07 17:25:49 +030071static char mlx5_version[] =
72 DRIVER_NAME ": Mellanox Connect-IB Infiniband driver v"
Tariq Toukanb3599112017-02-22 17:45:46 +020073 DRIVER_VERSION "\n";
Eli Cohene126ba92013-07-07 17:25:49 +030074
Eran Ben Elishada7525d2015-12-14 16:34:10 +020075enum {
76 MLX5_ATOMIC_SIZE_QP_8BYTES = 1 << 3,
77};
Majd Dibbiny1b5daf12015-06-04 19:30:46 +030078
Majd Dibbiny1b5daf12015-06-04 19:30:46 +030079static enum rdma_link_layer
Achiad Shochatebd61f62015-12-23 18:47:16 +020080mlx5_port_type_cap_to_rdma_ll(int port_type_cap)
Majd Dibbiny1b5daf12015-06-04 19:30:46 +030081{
Achiad Shochatebd61f62015-12-23 18:47:16 +020082 switch (port_type_cap) {
Majd Dibbiny1b5daf12015-06-04 19:30:46 +030083 case MLX5_CAP_PORT_TYPE_IB:
84 return IB_LINK_LAYER_INFINIBAND;
85 case MLX5_CAP_PORT_TYPE_ETH:
86 return IB_LINK_LAYER_ETHERNET;
87 default:
88 return IB_LINK_LAYER_UNSPECIFIED;
89 }
90}
91
Achiad Shochatebd61f62015-12-23 18:47:16 +020092static enum rdma_link_layer
93mlx5_ib_port_link_layer(struct ib_device *device, u8 port_num)
94{
95 struct mlx5_ib_dev *dev = to_mdev(device);
96 int port_type_cap = MLX5_CAP_GEN(dev->mdev, port_type);
97
98 return mlx5_port_type_cap_to_rdma_ll(port_type_cap);
99}
100
Moni Shouafd65f1b2017-05-30 09:56:05 +0300101static int get_port_state(struct ib_device *ibdev,
102 u8 port_num,
103 enum ib_port_state *state)
104{
105 struct ib_port_attr attr;
106 int ret;
107
108 memset(&attr, 0, sizeof(attr));
109 ret = mlx5_ib_query_port(ibdev, port_num, &attr);
110 if (!ret)
111 *state = attr.state;
112 return ret;
113}
114
Achiad Shochatfc24fc52015-12-23 18:47:17 +0200115static int mlx5_netdev_event(struct notifier_block *this,
116 unsigned long event, void *ptr)
117{
118 struct net_device *ndev = netdev_notifier_info_to_dev(ptr);
119 struct mlx5_ib_dev *ibdev = container_of(this, struct mlx5_ib_dev,
120 roce.nb);
121
Aviv Heller5ec8c832016-09-18 20:48:00 +0300122 switch (event) {
123 case NETDEV_REGISTER:
124 case NETDEV_UNREGISTER:
125 write_lock(&ibdev->roce.netdev_lock);
126 if (ndev->dev.parent == &ibdev->mdev->pdev->dev)
127 ibdev->roce.netdev = (event == NETDEV_UNREGISTER) ?
128 NULL : ndev;
129 write_unlock(&ibdev->roce.netdev_lock);
130 break;
Achiad Shochatfc24fc52015-12-23 18:47:17 +0200131
Moni Shouafd65f1b2017-05-30 09:56:05 +0300132 case NETDEV_CHANGE:
Aviv Heller5ec8c832016-09-18 20:48:00 +0300133 case NETDEV_UP:
Aviv Heller88621df2016-09-18 20:48:02 +0300134 case NETDEV_DOWN: {
135 struct net_device *lag_ndev = mlx5_lag_get_roce_netdev(ibdev->mdev);
136 struct net_device *upper = NULL;
137
138 if (lag_ndev) {
139 upper = netdev_master_upper_dev_get(lag_ndev);
140 dev_put(lag_ndev);
141 }
142
143 if ((upper == ndev || (!upper && ndev == ibdev->roce.netdev))
144 && ibdev->ib_active) {
Bart Van Assche626bc022016-12-05 17:18:08 -0800145 struct ib_event ibev = { };
Moni Shouafd65f1b2017-05-30 09:56:05 +0300146 enum ib_port_state port_state;
Aviv Heller5ec8c832016-09-18 20:48:00 +0300147
Moni Shouafd65f1b2017-05-30 09:56:05 +0300148 if (get_port_state(&ibdev->ib_dev, 1, &port_state))
149 return NOTIFY_DONE;
150
151 if (ibdev->roce.last_port_state == port_state)
152 return NOTIFY_DONE;
153
154 ibdev->roce.last_port_state = port_state;
Aviv Heller5ec8c832016-09-18 20:48:00 +0300155 ibev.device = &ibdev->ib_dev;
Moni Shouafd65f1b2017-05-30 09:56:05 +0300156 if (port_state == IB_PORT_DOWN)
157 ibev.event = IB_EVENT_PORT_ERR;
158 else if (port_state == IB_PORT_ACTIVE)
159 ibev.event = IB_EVENT_PORT_ACTIVE;
160 else
161 return NOTIFY_DONE;
162
Aviv Heller5ec8c832016-09-18 20:48:00 +0300163 ibev.element.port_num = 1;
164 ib_dispatch_event(&ibev);
165 }
166 break;
Aviv Heller88621df2016-09-18 20:48:02 +0300167 }
Aviv Heller5ec8c832016-09-18 20:48:00 +0300168
169 default:
170 break;
171 }
Achiad Shochatfc24fc52015-12-23 18:47:17 +0200172
173 return NOTIFY_DONE;
174}
175
176static struct net_device *mlx5_ib_get_netdev(struct ib_device *device,
177 u8 port_num)
178{
179 struct mlx5_ib_dev *ibdev = to_mdev(device);
180 struct net_device *ndev;
181
Aviv Heller88621df2016-09-18 20:48:02 +0300182 ndev = mlx5_lag_get_roce_netdev(ibdev->mdev);
183 if (ndev)
184 return ndev;
185
Achiad Shochatfc24fc52015-12-23 18:47:17 +0200186 /* Ensure ndev does not disappear before we invoke dev_hold()
187 */
188 read_lock(&ibdev->roce.netdev_lock);
189 ndev = ibdev->roce.netdev;
190 if (ndev)
191 dev_hold(ndev);
192 read_unlock(&ibdev->roce.netdev_lock);
193
194 return ndev;
195}
196
Noa Osherovichf1b65df2017-04-20 20:53:33 +0300197static int translate_eth_proto_oper(u32 eth_proto_oper, u8 *active_speed,
198 u8 *active_width)
199{
200 switch (eth_proto_oper) {
201 case MLX5E_PROT_MASK(MLX5E_1000BASE_CX_SGMII):
202 case MLX5E_PROT_MASK(MLX5E_1000BASE_KX):
203 case MLX5E_PROT_MASK(MLX5E_100BASE_TX):
204 case MLX5E_PROT_MASK(MLX5E_1000BASE_T):
205 *active_width = IB_WIDTH_1X;
206 *active_speed = IB_SPEED_SDR;
207 break;
208 case MLX5E_PROT_MASK(MLX5E_10GBASE_T):
209 case MLX5E_PROT_MASK(MLX5E_10GBASE_CX4):
210 case MLX5E_PROT_MASK(MLX5E_10GBASE_KX4):
211 case MLX5E_PROT_MASK(MLX5E_10GBASE_KR):
212 case MLX5E_PROT_MASK(MLX5E_10GBASE_CR):
213 case MLX5E_PROT_MASK(MLX5E_10GBASE_SR):
214 case MLX5E_PROT_MASK(MLX5E_10GBASE_ER):
215 *active_width = IB_WIDTH_1X;
216 *active_speed = IB_SPEED_QDR;
217 break;
218 case MLX5E_PROT_MASK(MLX5E_25GBASE_CR):
219 case MLX5E_PROT_MASK(MLX5E_25GBASE_KR):
220 case MLX5E_PROT_MASK(MLX5E_25GBASE_SR):
221 *active_width = IB_WIDTH_1X;
222 *active_speed = IB_SPEED_EDR;
223 break;
224 case MLX5E_PROT_MASK(MLX5E_40GBASE_CR4):
225 case MLX5E_PROT_MASK(MLX5E_40GBASE_KR4):
226 case MLX5E_PROT_MASK(MLX5E_40GBASE_SR4):
227 case MLX5E_PROT_MASK(MLX5E_40GBASE_LR4):
228 *active_width = IB_WIDTH_4X;
229 *active_speed = IB_SPEED_QDR;
230 break;
231 case MLX5E_PROT_MASK(MLX5E_50GBASE_CR2):
232 case MLX5E_PROT_MASK(MLX5E_50GBASE_KR2):
233 case MLX5E_PROT_MASK(MLX5E_50GBASE_SR2):
234 *active_width = IB_WIDTH_1X;
235 *active_speed = IB_SPEED_HDR;
236 break;
237 case MLX5E_PROT_MASK(MLX5E_56GBASE_R4):
238 *active_width = IB_WIDTH_4X;
239 *active_speed = IB_SPEED_FDR;
240 break;
241 case MLX5E_PROT_MASK(MLX5E_100GBASE_CR4):
242 case MLX5E_PROT_MASK(MLX5E_100GBASE_SR4):
243 case MLX5E_PROT_MASK(MLX5E_100GBASE_KR4):
244 case MLX5E_PROT_MASK(MLX5E_100GBASE_LR4):
245 *active_width = IB_WIDTH_4X;
246 *active_speed = IB_SPEED_EDR;
247 break;
248 default:
249 return -EINVAL;
250 }
251
252 return 0;
253}
254
Ilan Tayari095b0922017-05-14 16:04:30 +0300255static int mlx5_query_port_roce(struct ib_device *device, u8 port_num,
256 struct ib_port_attr *props)
Achiad Shochat3f89a642015-12-23 18:47:21 +0200257{
258 struct mlx5_ib_dev *dev = to_mdev(device);
Noa Osherovichf1b65df2017-04-20 20:53:33 +0300259 struct mlx5_core_dev *mdev = dev->mdev;
Aviv Heller88621df2016-09-18 20:48:02 +0300260 struct net_device *ndev, *upper;
Achiad Shochat3f89a642015-12-23 18:47:21 +0200261 enum ib_mtu ndev_ib_mtu;
Leon Romanovskyc876a1b2016-01-09 13:06:25 +0200262 u16 qkey_viol_cntr;
Noa Osherovichf1b65df2017-04-20 20:53:33 +0300263 u32 eth_prot_oper;
Ilan Tayari095b0922017-05-14 16:04:30 +0300264 int err;
Achiad Shochat3f89a642015-12-23 18:47:21 +0200265
Noa Osherovichf1b65df2017-04-20 20:53:33 +0300266 /* Possible bad flows are checked before filling out props so in case
267 * of an error it will still be zeroed out.
Noa Osherovich50f22fd2017-04-20 20:53:32 +0300268 */
Ilan Tayari095b0922017-05-14 16:04:30 +0300269 err = mlx5_query_port_eth_proto_oper(mdev, &eth_prot_oper, port_num);
270 if (err)
271 return err;
Noa Osherovichf1b65df2017-04-20 20:53:33 +0300272
273 translate_eth_proto_oper(eth_prot_oper, &props->active_speed,
274 &props->active_width);
Achiad Shochat3f89a642015-12-23 18:47:21 +0200275
276 props->port_cap_flags |= IB_PORT_CM_SUP;
277 props->port_cap_flags |= IB_PORT_IP_BASED_GIDS;
278
279 props->gid_tbl_len = MLX5_CAP_ROCE(dev->mdev,
280 roce_address_table_size);
281 props->max_mtu = IB_MTU_4096;
282 props->max_msg_sz = 1 << MLX5_CAP_GEN(dev->mdev, log_max_msg);
283 props->pkey_tbl_len = 1;
284 props->state = IB_PORT_DOWN;
285 props->phys_state = 3;
286
Leon Romanovskyc876a1b2016-01-09 13:06:25 +0200287 mlx5_query_nic_vport_qkey_viol_cntr(dev->mdev, &qkey_viol_cntr);
288 props->qkey_viol_cntr = qkey_viol_cntr;
Achiad Shochat3f89a642015-12-23 18:47:21 +0200289
290 ndev = mlx5_ib_get_netdev(device, port_num);
291 if (!ndev)
Ilan Tayari095b0922017-05-14 16:04:30 +0300292 return 0;
Achiad Shochat3f89a642015-12-23 18:47:21 +0200293
Aviv Heller88621df2016-09-18 20:48:02 +0300294 if (mlx5_lag_is_active(dev->mdev)) {
295 rcu_read_lock();
296 upper = netdev_master_upper_dev_get_rcu(ndev);
297 if (upper) {
298 dev_put(ndev);
299 ndev = upper;
300 dev_hold(ndev);
301 }
302 rcu_read_unlock();
303 }
304
Achiad Shochat3f89a642015-12-23 18:47:21 +0200305 if (netif_running(ndev) && netif_carrier_ok(ndev)) {
306 props->state = IB_PORT_ACTIVE;
307 props->phys_state = 5;
308 }
309
310 ndev_ib_mtu = iboe_get_mtu(ndev->mtu);
311
312 dev_put(ndev);
313
314 props->active_mtu = min(props->max_mtu, ndev_ib_mtu);
Ilan Tayari095b0922017-05-14 16:04:30 +0300315 return 0;
Achiad Shochat3f89a642015-12-23 18:47:21 +0200316}
317
Ilan Tayari095b0922017-05-14 16:04:30 +0300318static int set_roce_addr(struct mlx5_ib_dev *dev, u8 port_num,
319 unsigned int index, const union ib_gid *gid,
320 const struct ib_gid_attr *attr)
Achiad Shochat3cca2602015-12-23 18:47:23 +0200321{
Ilan Tayari095b0922017-05-14 16:04:30 +0300322 enum ib_gid_type gid_type = IB_GID_TYPE_IB;
323 u8 roce_version = 0;
324 u8 roce_l3_type = 0;
325 bool vlan = false;
326 u8 mac[ETH_ALEN];
327 u16 vlan_id = 0;
Achiad Shochat3cca2602015-12-23 18:47:23 +0200328
Ilan Tayari095b0922017-05-14 16:04:30 +0300329 if (gid) {
330 gid_type = attr->gid_type;
331 ether_addr_copy(mac, attr->ndev->dev_addr);
Achiad Shochat3cca2602015-12-23 18:47:23 +0200332
Ilan Tayari095b0922017-05-14 16:04:30 +0300333 if (is_vlan_dev(attr->ndev)) {
334 vlan = true;
335 vlan_id = vlan_dev_vlan_id(attr->ndev);
336 }
Achiad Shochat3cca2602015-12-23 18:47:23 +0200337 }
338
Ilan Tayari095b0922017-05-14 16:04:30 +0300339 switch (gid_type) {
Achiad Shochat3cca2602015-12-23 18:47:23 +0200340 case IB_GID_TYPE_IB:
Ilan Tayari095b0922017-05-14 16:04:30 +0300341 roce_version = MLX5_ROCE_VERSION_1;
Achiad Shochat3cca2602015-12-23 18:47:23 +0200342 break;
343 case IB_GID_TYPE_ROCE_UDP_ENCAP:
Ilan Tayari095b0922017-05-14 16:04:30 +0300344 roce_version = MLX5_ROCE_VERSION_2;
345 if (ipv6_addr_v4mapped((void *)gid))
346 roce_l3_type = MLX5_ROCE_L3_TYPE_IPV4;
347 else
348 roce_l3_type = MLX5_ROCE_L3_TYPE_IPV6;
Achiad Shochat3cca2602015-12-23 18:47:23 +0200349 break;
350
351 default:
Ilan Tayari095b0922017-05-14 16:04:30 +0300352 mlx5_ib_warn(dev, "Unexpected GID type %u\n", gid_type);
Achiad Shochat3cca2602015-12-23 18:47:23 +0200353 }
354
Ilan Tayari095b0922017-05-14 16:04:30 +0300355 return mlx5_core_roce_gid_set(dev->mdev, index, roce_version,
356 roce_l3_type, gid->raw, mac, vlan,
357 vlan_id);
Achiad Shochat3cca2602015-12-23 18:47:23 +0200358}
359
360static int mlx5_ib_add_gid(struct ib_device *device, u8 port_num,
361 unsigned int index, const union ib_gid *gid,
362 const struct ib_gid_attr *attr,
363 __always_unused void **context)
364{
Ilan Tayari095b0922017-05-14 16:04:30 +0300365 return set_roce_addr(to_mdev(device), port_num, index, gid, attr);
Achiad Shochat3cca2602015-12-23 18:47:23 +0200366}
367
368static int mlx5_ib_del_gid(struct ib_device *device, u8 port_num,
369 unsigned int index, __always_unused void **context)
370{
Ilan Tayari095b0922017-05-14 16:04:30 +0300371 return set_roce_addr(to_mdev(device), port_num, index, NULL, NULL);
Achiad Shochat3cca2602015-12-23 18:47:23 +0200372}
373
Achiad Shochat2811ba52015-12-23 18:47:24 +0200374__be16 mlx5_get_roce_udp_sport(struct mlx5_ib_dev *dev, u8 port_num,
375 int index)
376{
377 struct ib_gid_attr attr;
378 union ib_gid gid;
379
380 if (ib_get_cached_gid(&dev->ib_dev, port_num, index, &gid, &attr))
381 return 0;
382
383 if (!attr.ndev)
384 return 0;
385
386 dev_put(attr.ndev);
387
388 if (attr.gid_type != IB_GID_TYPE_ROCE_UDP_ENCAP)
389 return 0;
390
391 return cpu_to_be16(MLX5_CAP_ROCE(dev->mdev, r_roce_min_src_udp_port));
392}
393
Majd Dibbinyed884512017-01-18 14:10:35 +0200394int mlx5_get_roce_gid_type(struct mlx5_ib_dev *dev, u8 port_num,
395 int index, enum ib_gid_type *gid_type)
396{
397 struct ib_gid_attr attr;
398 union ib_gid gid;
399 int ret;
400
401 ret = ib_get_cached_gid(&dev->ib_dev, port_num, index, &gid, &attr);
402 if (ret)
403 return ret;
404
405 if (!attr.ndev)
406 return -ENODEV;
407
408 dev_put(attr.ndev);
409
410 *gid_type = attr.gid_type;
411
412 return 0;
413}
414
Majd Dibbiny1b5daf12015-06-04 19:30:46 +0300415static int mlx5_use_mad_ifc(struct mlx5_ib_dev *dev)
416{
Noa Osherovich7fae6652016-09-12 19:16:23 +0300417 if (MLX5_CAP_GEN(dev->mdev, port_type) == MLX5_CAP_PORT_TYPE_IB)
418 return !MLX5_CAP_GEN(dev->mdev, ib_virt);
419 return 0;
Majd Dibbiny1b5daf12015-06-04 19:30:46 +0300420}
421
422enum {
423 MLX5_VPORT_ACCESS_METHOD_MAD,
424 MLX5_VPORT_ACCESS_METHOD_HCA,
425 MLX5_VPORT_ACCESS_METHOD_NIC,
426};
427
428static int mlx5_get_vport_access_method(struct ib_device *ibdev)
429{
430 if (mlx5_use_mad_ifc(to_mdev(ibdev)))
431 return MLX5_VPORT_ACCESS_METHOD_MAD;
432
Achiad Shochatebd61f62015-12-23 18:47:16 +0200433 if (mlx5_ib_port_link_layer(ibdev, 1) ==
Majd Dibbiny1b5daf12015-06-04 19:30:46 +0300434 IB_LINK_LAYER_ETHERNET)
435 return MLX5_VPORT_ACCESS_METHOD_NIC;
436
437 return MLX5_VPORT_ACCESS_METHOD_HCA;
438}
439
Eran Ben Elishada7525d2015-12-14 16:34:10 +0200440static void get_atomic_caps(struct mlx5_ib_dev *dev,
441 struct ib_device_attr *props)
442{
443 u8 tmp;
444 u8 atomic_operations = MLX5_CAP_ATOMIC(dev->mdev, atomic_operations);
445 u8 atomic_size_qp = MLX5_CAP_ATOMIC(dev->mdev, atomic_size_qp);
446 u8 atomic_req_8B_endianness_mode =
Or Gerlitzbd108382017-05-28 15:24:17 +0300447 MLX5_CAP_ATOMIC(dev->mdev, atomic_req_8B_endianness_mode);
Eran Ben Elishada7525d2015-12-14 16:34:10 +0200448
449 /* Check if HW supports 8 bytes standard atomic operations and capable
450 * of host endianness respond
451 */
452 tmp = MLX5_ATOMIC_OPS_CMP_SWAP | MLX5_ATOMIC_OPS_FETCH_ADD;
453 if (((atomic_operations & tmp) == tmp) &&
454 (atomic_size_qp & MLX5_ATOMIC_SIZE_QP_8BYTES) &&
455 (atomic_req_8B_endianness_mode)) {
456 props->atomic_cap = IB_ATOMIC_HCA;
457 } else {
458 props->atomic_cap = IB_ATOMIC_NONE;
459 }
460}
461
Majd Dibbiny1b5daf12015-06-04 19:30:46 +0300462static int mlx5_query_system_image_guid(struct ib_device *ibdev,
463 __be64 *sys_image_guid)
464{
465 struct mlx5_ib_dev *dev = to_mdev(ibdev);
466 struct mlx5_core_dev *mdev = dev->mdev;
467 u64 tmp;
468 int err;
469
470 switch (mlx5_get_vport_access_method(ibdev)) {
471 case MLX5_VPORT_ACCESS_METHOD_MAD:
472 return mlx5_query_mad_ifc_system_image_guid(ibdev,
473 sys_image_guid);
474
475 case MLX5_VPORT_ACCESS_METHOD_HCA:
476 err = mlx5_query_hca_vport_system_image_guid(mdev, &tmp);
Achiad Shochat3f89a642015-12-23 18:47:21 +0200477 break;
478
479 case MLX5_VPORT_ACCESS_METHOD_NIC:
480 err = mlx5_query_nic_vport_system_image_guid(mdev, &tmp);
481 break;
Majd Dibbiny1b5daf12015-06-04 19:30:46 +0300482
483 default:
484 return -EINVAL;
485 }
Achiad Shochat3f89a642015-12-23 18:47:21 +0200486
487 if (!err)
488 *sys_image_guid = cpu_to_be64(tmp);
489
490 return err;
491
Majd Dibbiny1b5daf12015-06-04 19:30:46 +0300492}
493
494static int mlx5_query_max_pkeys(struct ib_device *ibdev,
495 u16 *max_pkeys)
496{
497 struct mlx5_ib_dev *dev = to_mdev(ibdev);
498 struct mlx5_core_dev *mdev = dev->mdev;
499
500 switch (mlx5_get_vport_access_method(ibdev)) {
501 case MLX5_VPORT_ACCESS_METHOD_MAD:
502 return mlx5_query_mad_ifc_max_pkeys(ibdev, max_pkeys);
503
504 case MLX5_VPORT_ACCESS_METHOD_HCA:
505 case MLX5_VPORT_ACCESS_METHOD_NIC:
506 *max_pkeys = mlx5_to_sw_pkey_sz(MLX5_CAP_GEN(mdev,
507 pkey_table_size));
508 return 0;
509
510 default:
511 return -EINVAL;
512 }
513}
514
515static int mlx5_query_vendor_id(struct ib_device *ibdev,
516 u32 *vendor_id)
517{
518 struct mlx5_ib_dev *dev = to_mdev(ibdev);
519
520 switch (mlx5_get_vport_access_method(ibdev)) {
521 case MLX5_VPORT_ACCESS_METHOD_MAD:
522 return mlx5_query_mad_ifc_vendor_id(ibdev, vendor_id);
523
524 case MLX5_VPORT_ACCESS_METHOD_HCA:
525 case MLX5_VPORT_ACCESS_METHOD_NIC:
526 return mlx5_core_query_vendor_id(dev->mdev, vendor_id);
527
528 default:
529 return -EINVAL;
530 }
531}
532
533static int mlx5_query_node_guid(struct mlx5_ib_dev *dev,
534 __be64 *node_guid)
535{
536 u64 tmp;
537 int err;
538
539 switch (mlx5_get_vport_access_method(&dev->ib_dev)) {
540 case MLX5_VPORT_ACCESS_METHOD_MAD:
541 return mlx5_query_mad_ifc_node_guid(dev, node_guid);
542
543 case MLX5_VPORT_ACCESS_METHOD_HCA:
544 err = mlx5_query_hca_vport_node_guid(dev->mdev, &tmp);
Achiad Shochat3f89a642015-12-23 18:47:21 +0200545 break;
546
547 case MLX5_VPORT_ACCESS_METHOD_NIC:
548 err = mlx5_query_nic_vport_node_guid(dev->mdev, &tmp);
549 break;
Majd Dibbiny1b5daf12015-06-04 19:30:46 +0300550
551 default:
552 return -EINVAL;
553 }
Achiad Shochat3f89a642015-12-23 18:47:21 +0200554
555 if (!err)
556 *node_guid = cpu_to_be64(tmp);
557
558 return err;
Majd Dibbiny1b5daf12015-06-04 19:30:46 +0300559}
560
561struct mlx5_reg_node_desc {
Yuval Shaiabd99fde2016-08-25 10:57:07 -0700562 u8 desc[IB_DEVICE_NODE_DESC_MAX];
Majd Dibbiny1b5daf12015-06-04 19:30:46 +0300563};
564
565static int mlx5_query_node_desc(struct mlx5_ib_dev *dev, char *node_desc)
566{
567 struct mlx5_reg_node_desc in;
568
569 if (mlx5_use_mad_ifc(dev))
570 return mlx5_query_mad_ifc_node_desc(dev, node_desc);
571
572 memset(&in, 0, sizeof(in));
573
574 return mlx5_core_access_reg(dev->mdev, &in, sizeof(in), node_desc,
575 sizeof(struct mlx5_reg_node_desc),
576 MLX5_REG_NODE_DESC, 0, 0);
577}
578
Eli Cohene126ba92013-07-07 17:25:49 +0300579static int mlx5_ib_query_device(struct ib_device *ibdev,
Matan Barak2528e332015-06-11 16:35:25 +0300580 struct ib_device_attr *props,
581 struct ib_udata *uhw)
Eli Cohene126ba92013-07-07 17:25:49 +0300582{
583 struct mlx5_ib_dev *dev = to_mdev(ibdev);
Saeed Mahameed938fe832015-05-28 22:28:41 +0300584 struct mlx5_core_dev *mdev = dev->mdev;
Eli Cohene126ba92013-07-07 17:25:49 +0300585 int err = -ENOMEM;
Eli Cohen288c01b2016-10-27 16:36:45 +0300586 int max_sq_desc;
Eli Cohene126ba92013-07-07 17:25:49 +0300587 int max_rq_sg;
588 int max_sq_sg;
Sagi Grimberge0238a62015-07-21 14:40:12 +0300589 u64 min_page_size = 1ull << MLX5_CAP_GEN(mdev, log_pg_sz);
Bodong Wang402ca532016-06-17 15:02:20 +0300590 struct mlx5_ib_query_device_resp resp = {};
591 size_t resp_len;
592 u64 max_tso;
Eli Cohene126ba92013-07-07 17:25:49 +0300593
Bodong Wang402ca532016-06-17 15:02:20 +0300594 resp_len = sizeof(resp.comp_mask) + sizeof(resp.response_length);
595 if (uhw->outlen && uhw->outlen < resp_len)
596 return -EINVAL;
597 else
598 resp.response_length = resp_len;
599
600 if (uhw->inlen && !ib_is_udata_cleared(uhw, 0, uhw->inlen))
Matan Barak2528e332015-06-11 16:35:25 +0300601 return -EINVAL;
602
Eli Cohene126ba92013-07-07 17:25:49 +0300603 memset(props, 0, sizeof(*props));
Majd Dibbiny1b5daf12015-06-04 19:30:46 +0300604 err = mlx5_query_system_image_guid(ibdev,
605 &props->sys_image_guid);
606 if (err)
607 return err;
608
609 err = mlx5_query_max_pkeys(ibdev, &props->max_pkeys);
610 if (err)
611 return err;
612
613 err = mlx5_query_vendor_id(ibdev, &props->vendor_id);
614 if (err)
615 return err;
Eli Cohene126ba92013-07-07 17:25:49 +0300616
Jack Morgenstein9603b612014-07-28 23:30:22 +0300617 props->fw_ver = ((u64)fw_rev_maj(dev->mdev) << 32) |
618 (fw_rev_min(dev->mdev) << 16) |
619 fw_rev_sub(dev->mdev);
Eli Cohene126ba92013-07-07 17:25:49 +0300620 props->device_cap_flags = IB_DEVICE_CHANGE_PHY_PORT |
621 IB_DEVICE_PORT_ACTIVE_EVENT |
622 IB_DEVICE_SYS_IMAGE_GUID |
Eli Cohen1a4c3a32014-02-06 17:41:25 +0200623 IB_DEVICE_RC_RNR_NAK_GEN;
Saeed Mahameed938fe832015-05-28 22:28:41 +0300624
625 if (MLX5_CAP_GEN(mdev, pkv))
Eli Cohene126ba92013-07-07 17:25:49 +0300626 props->device_cap_flags |= IB_DEVICE_BAD_PKEY_CNTR;
Saeed Mahameed938fe832015-05-28 22:28:41 +0300627 if (MLX5_CAP_GEN(mdev, qkv))
Eli Cohene126ba92013-07-07 17:25:49 +0300628 props->device_cap_flags |= IB_DEVICE_BAD_QKEY_CNTR;
Saeed Mahameed938fe832015-05-28 22:28:41 +0300629 if (MLX5_CAP_GEN(mdev, apm))
Eli Cohene126ba92013-07-07 17:25:49 +0300630 props->device_cap_flags |= IB_DEVICE_AUTO_PATH_MIG;
Saeed Mahameed938fe832015-05-28 22:28:41 +0300631 if (MLX5_CAP_GEN(mdev, xrc))
Eli Cohene126ba92013-07-07 17:25:49 +0300632 props->device_cap_flags |= IB_DEVICE_XRC;
Matan Barakd2370e02016-02-29 18:05:30 +0200633 if (MLX5_CAP_GEN(mdev, imaicl)) {
634 props->device_cap_flags |= IB_DEVICE_MEM_WINDOW |
635 IB_DEVICE_MEM_WINDOW_TYPE_2B;
636 props->max_mw = 1 << MLX5_CAP_GEN(mdev, log_max_mkey);
Sagi Grimbergb005d312016-02-29 19:07:33 +0200637 /* We support 'Gappy' memory registration too */
638 props->device_cap_flags |= IB_DEVICE_SG_GAPS_REG;
Matan Barakd2370e02016-02-29 18:05:30 +0200639 }
Eli Cohene126ba92013-07-07 17:25:49 +0300640 props->device_cap_flags |= IB_DEVICE_MEM_MGT_EXTENSIONS;
Saeed Mahameed938fe832015-05-28 22:28:41 +0300641 if (MLX5_CAP_GEN(mdev, sho)) {
Sagi Grimberg2dea9092014-02-23 14:19:13 +0200642 props->device_cap_flags |= IB_DEVICE_SIGNATURE_HANDOVER;
643 /* At this stage no support for signature handover */
644 props->sig_prot_cap = IB_PROT_T10DIF_TYPE_1 |
645 IB_PROT_T10DIF_TYPE_2 |
646 IB_PROT_T10DIF_TYPE_3;
647 props->sig_guard_cap = IB_GUARD_T10DIF_CRC |
648 IB_GUARD_T10DIF_CSUM;
649 }
Saeed Mahameed938fe832015-05-28 22:28:41 +0300650 if (MLX5_CAP_GEN(mdev, block_lb_mc))
Eli Cohenf360d882014-04-02 00:10:16 +0300651 props->device_cap_flags |= IB_DEVICE_BLOCK_MULTICAST_LOOPBACK;
Eli Cohene126ba92013-07-07 17:25:49 +0300652
Bodong Wang402ca532016-06-17 15:02:20 +0300653 if (MLX5_CAP_GEN(dev->mdev, eth_net_offloads)) {
Noa Osheroviche8161332017-01-18 15:40:01 +0200654 if (MLX5_CAP_ETH(mdev, csum_cap)) {
655 /* Legacy bit to support old userspace libraries */
Bodong Wang88115fe2015-12-18 13:53:20 +0200656 props->device_cap_flags |= IB_DEVICE_RAW_IP_CSUM;
Noa Osheroviche8161332017-01-18 15:40:01 +0200657 props->raw_packet_caps |= IB_RAW_PACKET_CAP_IP_CSUM;
658 }
659
660 if (MLX5_CAP_ETH(dev->mdev, vlan_cap))
661 props->raw_packet_caps |=
662 IB_RAW_PACKET_CAP_CVLAN_STRIPPING;
Bodong Wang88115fe2015-12-18 13:53:20 +0200663
Bodong Wang402ca532016-06-17 15:02:20 +0300664 if (field_avail(typeof(resp), tso_caps, uhw->outlen)) {
665 max_tso = MLX5_CAP_ETH(mdev, max_lso_cap);
666 if (max_tso) {
667 resp.tso_caps.max_tso = 1 << max_tso;
668 resp.tso_caps.supported_qpts |=
669 1 << IB_QPT_RAW_PACKET;
670 resp.response_length += sizeof(resp.tso_caps);
671 }
672 }
Yishai Hadas31f69a82016-08-28 11:28:45 +0300673
674 if (field_avail(typeof(resp), rss_caps, uhw->outlen)) {
675 resp.rss_caps.rx_hash_function =
676 MLX5_RX_HASH_FUNC_TOEPLITZ;
677 resp.rss_caps.rx_hash_fields_mask =
678 MLX5_RX_HASH_SRC_IPV4 |
679 MLX5_RX_HASH_DST_IPV4 |
680 MLX5_RX_HASH_SRC_IPV6 |
681 MLX5_RX_HASH_DST_IPV6 |
682 MLX5_RX_HASH_SRC_PORT_TCP |
683 MLX5_RX_HASH_DST_PORT_TCP |
684 MLX5_RX_HASH_SRC_PORT_UDP |
685 MLX5_RX_HASH_DST_PORT_UDP;
686 resp.response_length += sizeof(resp.rss_caps);
687 }
688 } else {
689 if (field_avail(typeof(resp), tso_caps, uhw->outlen))
690 resp.response_length += sizeof(resp.tso_caps);
691 if (field_avail(typeof(resp), rss_caps, uhw->outlen))
692 resp.response_length += sizeof(resp.rss_caps);
Bodong Wang402ca532016-06-17 15:02:20 +0300693 }
694
Erez Shitritf0313962016-02-21 16:27:17 +0200695 if (MLX5_CAP_GEN(mdev, ipoib_basic_offloads)) {
696 props->device_cap_flags |= IB_DEVICE_UD_IP_CSUM;
697 props->device_cap_flags |= IB_DEVICE_UD_TSO;
698 }
699
Maor Gottlieb03404e82017-05-30 10:29:13 +0300700 if (MLX5_CAP_GEN(dev->mdev, rq_delay_drop) &&
701 MLX5_CAP_GEN(dev->mdev, general_notification_event))
702 props->raw_packet_caps |= IB_RAW_PACKET_CAP_DELAY_DROP;
703
Yishai Hadas1d54f892017-06-08 16:15:11 +0300704 if (MLX5_CAP_GEN(mdev, ipoib_enhanced_offloads) &&
705 MLX5_CAP_IPOIB_ENHANCED(mdev, csum_cap))
706 props->device_cap_flags |= IB_DEVICE_UD_IP_CSUM;
707
Majd Dibbinycff5a0f2016-04-17 17:19:38 +0300708 if (MLX5_CAP_GEN(dev->mdev, eth_net_offloads) &&
Noa Osheroviche8161332017-01-18 15:40:01 +0200709 MLX5_CAP_ETH(dev->mdev, scatter_fcs)) {
710 /* Legacy bit to support old userspace libraries */
Majd Dibbinycff5a0f2016-04-17 17:19:38 +0300711 props->device_cap_flags |= IB_DEVICE_RAW_SCATTER_FCS;
Noa Osheroviche8161332017-01-18 15:40:01 +0200712 props->raw_packet_caps |= IB_RAW_PACKET_CAP_SCATTER_FCS;
713 }
Majd Dibbinycff5a0f2016-04-17 17:19:38 +0300714
Maor Gottliebda6d6ba32016-06-04 15:15:28 +0300715 if (mlx5_get_flow_namespace(dev->mdev, MLX5_FLOW_NAMESPACE_BYPASS))
716 props->device_cap_flags |= IB_DEVICE_MANAGED_FLOW_STEERING;
717
Majd Dibbiny1b5daf12015-06-04 19:30:46 +0300718 props->vendor_part_id = mdev->pdev->device;
719 props->hw_ver = mdev->pdev->revision;
Eli Cohene126ba92013-07-07 17:25:49 +0300720
721 props->max_mr_size = ~0ull;
Sagi Grimberge0238a62015-07-21 14:40:12 +0300722 props->page_size_cap = ~(min_page_size - 1);
Saeed Mahameed938fe832015-05-28 22:28:41 +0300723 props->max_qp = 1 << MLX5_CAP_GEN(mdev, log_max_qp);
724 props->max_qp_wr = 1 << MLX5_CAP_GEN(mdev, log_max_qp_sz);
725 max_rq_sg = MLX5_CAP_GEN(mdev, max_wqe_sz_rq) /
726 sizeof(struct mlx5_wqe_data_seg);
Eli Cohen288c01b2016-10-27 16:36:45 +0300727 max_sq_desc = min_t(int, MLX5_CAP_GEN(mdev, max_wqe_sz_sq), 512);
728 max_sq_sg = (max_sq_desc - sizeof(struct mlx5_wqe_ctrl_seg) -
729 sizeof(struct mlx5_wqe_raddr_seg)) /
730 sizeof(struct mlx5_wqe_data_seg);
Eli Cohene126ba92013-07-07 17:25:49 +0300731 props->max_sge = min(max_rq_sg, max_sq_sg);
Sagi Grimberg986ef952016-03-31 19:03:25 +0300732 props->max_sge_rd = MLX5_MAX_SGE_RD;
Saeed Mahameed938fe832015-05-28 22:28:41 +0300733 props->max_cq = 1 << MLX5_CAP_GEN(mdev, log_max_cq);
Leon Romanovsky9f177682016-01-14 08:11:40 +0200734 props->max_cqe = (1 << MLX5_CAP_GEN(mdev, log_max_cq_sz)) - 1;
Saeed Mahameed938fe832015-05-28 22:28:41 +0300735 props->max_mr = 1 << MLX5_CAP_GEN(mdev, log_max_mkey);
736 props->max_pd = 1 << MLX5_CAP_GEN(mdev, log_max_pd);
737 props->max_qp_rd_atom = 1 << MLX5_CAP_GEN(mdev, log_max_ra_req_qp);
738 props->max_qp_init_rd_atom = 1 << MLX5_CAP_GEN(mdev, log_max_ra_res_qp);
739 props->max_srq = 1 << MLX5_CAP_GEN(mdev, log_max_srq);
740 props->max_srq_wr = (1 << MLX5_CAP_GEN(mdev, log_max_srq_sz)) - 1;
741 props->local_ca_ack_delay = MLX5_CAP_GEN(mdev, local_ca_ack_delay);
Eli Cohene126ba92013-07-07 17:25:49 +0300742 props->max_res_rd_atom = props->max_qp_rd_atom * props->max_qp;
Eli Cohene126ba92013-07-07 17:25:49 +0300743 props->max_srq_sge = max_rq_sg - 1;
Sagi Grimberg911f4332016-03-03 13:37:51 +0200744 props->max_fast_reg_page_list_len =
745 1 << MLX5_CAP_GEN(mdev, log_max_klm_list_size);
Eran Ben Elishada7525d2015-12-14 16:34:10 +0200746 get_atomic_caps(dev, props);
Eli Cohen81bea282013-09-11 16:35:30 +0300747 props->masked_atomic_cap = IB_ATOMIC_NONE;
Saeed Mahameed938fe832015-05-28 22:28:41 +0300748 props->max_mcast_grp = 1 << MLX5_CAP_GEN(mdev, log_max_mcg);
749 props->max_mcast_qp_attach = MLX5_CAP_GEN(mdev, max_qp_mcg);
Eli Cohene126ba92013-07-07 17:25:49 +0300750 props->max_total_mcast_qp_attach = props->max_mcast_qp_attach *
751 props->max_mcast_grp;
752 props->max_map_per_fmr = INT_MAX; /* no limit in ConnectIB */
Maor Gottlieb86695a62016-10-27 16:36:38 +0300753 props->max_ah = INT_MAX;
Matan Barak7c60bcb2015-12-15 20:30:11 +0200754 props->hca_core_clock = MLX5_CAP_GEN(mdev, device_frequency_khz);
755 props->timestamp_mask = 0x7FFFFFFFFFFFFFFFULL;
Eli Cohene126ba92013-07-07 17:25:49 +0300756
Haggai Eran8cdd3122014-12-11 17:04:20 +0200757#ifdef CONFIG_INFINIBAND_ON_DEMAND_PAGING
Saeed Mahameed938fe832015-05-28 22:28:41 +0300758 if (MLX5_CAP_GEN(mdev, pg))
Haggai Eran8cdd3122014-12-11 17:04:20 +0200759 props->device_cap_flags |= IB_DEVICE_ON_DEMAND_PAGING;
760 props->odp_caps = dev->odp_caps;
761#endif
762
Leon Romanovsky051f2632015-12-20 12:16:11 +0200763 if (MLX5_CAP_GEN(mdev, cd))
764 props->device_cap_flags |= IB_DEVICE_CROSS_CHANNEL;
765
Eli Coheneff901d2016-03-11 22:58:42 +0200766 if (!mlx5_core_is_pf(mdev))
767 props->device_cap_flags |= IB_DEVICE_VIRTUAL_FUNCTION;
768
Yishai Hadas31f69a82016-08-28 11:28:45 +0300769 if (mlx5_ib_port_link_layer(ibdev, 1) ==
770 IB_LINK_LAYER_ETHERNET) {
771 props->rss_caps.max_rwq_indirection_tables =
772 1 << MLX5_CAP_GEN(dev->mdev, log_max_rqt);
773 props->rss_caps.max_rwq_indirection_table_size =
774 1 << MLX5_CAP_GEN(dev->mdev, log_max_rqt_size);
775 props->rss_caps.supported_qpts = 1 << IB_QPT_RAW_PACKET;
776 props->max_wq_type_rq =
777 1 << MLX5_CAP_GEN(dev->mdev, log_max_rq);
778 }
779
Artemy Kovalyoveb761892017-08-17 15:52:09 +0300780 if (MLX5_CAP_GEN(mdev, tag_matching)) {
Leon Romanovsky78b1beb2017-09-24 21:46:29 +0300781 props->tm_caps.max_rndv_hdr_size = MLX5_TM_MAX_RNDV_MSG_SIZE;
782 props->tm_caps.max_num_tags =
Artemy Kovalyoveb761892017-08-17 15:52:09 +0300783 (1 << MLX5_CAP_GEN(mdev, log_tag_matching_list_sz)) - 1;
Leon Romanovsky78b1beb2017-09-24 21:46:29 +0300784 props->tm_caps.flags = IB_TM_CAP_RC;
785 props->tm_caps.max_ops =
Artemy Kovalyoveb761892017-08-17 15:52:09 +0300786 1 << MLX5_CAP_GEN(mdev, log_max_qp_sz);
Leon Romanovsky78b1beb2017-09-24 21:46:29 +0300787 props->tm_caps.max_sge = MLX5_TM_MAX_SGE;
Artemy Kovalyoveb761892017-08-17 15:52:09 +0300788 }
789
Bodong Wang7e43a2a2016-10-31 12:16:44 +0200790 if (field_avail(typeof(resp), cqe_comp_caps, uhw->outlen)) {
791 resp.cqe_comp_caps.max_num =
792 MLX5_CAP_GEN(dev->mdev, cqe_compression) ?
793 MLX5_CAP_GEN(dev->mdev, cqe_compression_max_num) : 0;
794 resp.cqe_comp_caps.supported_format =
795 MLX5_IB_CQE_RES_FORMAT_HASH |
796 MLX5_IB_CQE_RES_FORMAT_CSUM;
797 resp.response_length += sizeof(resp.cqe_comp_caps);
798 }
799
Bodong Wangd9491672016-12-01 13:43:13 +0200800 if (field_avail(typeof(resp), packet_pacing_caps, uhw->outlen)) {
801 if (MLX5_CAP_QOS(mdev, packet_pacing) &&
802 MLX5_CAP_GEN(mdev, qos)) {
803 resp.packet_pacing_caps.qp_rate_limit_max =
804 MLX5_CAP_QOS(mdev, packet_pacing_max_rate);
805 resp.packet_pacing_caps.qp_rate_limit_min =
806 MLX5_CAP_QOS(mdev, packet_pacing_min_rate);
807 resp.packet_pacing_caps.supported_qpts |=
808 1 << IB_QPT_RAW_PACKET;
809 }
810 resp.response_length += sizeof(resp.packet_pacing_caps);
811 }
812
Leon Romanovsky9f885202017-01-02 11:37:39 +0200813 if (field_avail(typeof(resp), mlx5_ib_support_multi_pkt_send_wqes,
814 uhw->outlen)) {
Bodong Wang795b6092017-08-17 15:52:34 +0300815 if (MLX5_CAP_ETH(mdev, multi_pkt_send_wqe))
816 resp.mlx5_ib_support_multi_pkt_send_wqes =
817 MLX5_IB_ALLOW_MPW;
Bodong Wang050da902017-08-17 15:52:35 +0300818
819 if (MLX5_CAP_ETH(mdev, enhanced_multi_pkt_send_wqe))
820 resp.mlx5_ib_support_multi_pkt_send_wqes |=
821 MLX5_IB_SUPPORT_EMPW;
822
Leon Romanovsky9f885202017-01-02 11:37:39 +0200823 resp.response_length +=
824 sizeof(resp.mlx5_ib_support_multi_pkt_send_wqes);
825 }
826
827 if (field_avail(typeof(resp), reserved, uhw->outlen))
828 resp.response_length += sizeof(resp.reserved);
829
Noa Osherovich96dc3fc2017-08-17 15:52:28 +0300830 if (field_avail(typeof(resp), sw_parsing_caps,
831 uhw->outlen)) {
832 resp.response_length += sizeof(resp.sw_parsing_caps);
833 if (MLX5_CAP_ETH(mdev, swp)) {
834 resp.sw_parsing_caps.sw_parsing_offloads |=
835 MLX5_IB_SW_PARSING;
836
837 if (MLX5_CAP_ETH(mdev, swp_csum))
838 resp.sw_parsing_caps.sw_parsing_offloads |=
839 MLX5_IB_SW_PARSING_CSUM;
840
841 if (MLX5_CAP_ETH(mdev, swp_lso))
842 resp.sw_parsing_caps.sw_parsing_offloads |=
843 MLX5_IB_SW_PARSING_LSO;
844
845 if (resp.sw_parsing_caps.sw_parsing_offloads)
846 resp.sw_parsing_caps.supported_qpts =
847 BIT(IB_QPT_RAW_PACKET);
848 }
849 }
850
Noa Osherovichb4f34592017-10-17 18:01:12 +0300851 if (field_avail(typeof(resp), striding_rq_caps, uhw->outlen)) {
852 resp.response_length += sizeof(resp.striding_rq_caps);
853 if (MLX5_CAP_GEN(mdev, striding_rq)) {
854 resp.striding_rq_caps.min_single_stride_log_num_of_bytes =
855 MLX5_MIN_SINGLE_STRIDE_LOG_NUM_BYTES;
856 resp.striding_rq_caps.max_single_stride_log_num_of_bytes =
857 MLX5_MAX_SINGLE_STRIDE_LOG_NUM_BYTES;
858 resp.striding_rq_caps.min_single_wqe_log_num_of_strides =
859 MLX5_MIN_SINGLE_WQE_LOG_NUM_STRIDES;
860 resp.striding_rq_caps.max_single_wqe_log_num_of_strides =
861 MLX5_MAX_SINGLE_WQE_LOG_NUM_STRIDES;
862 resp.striding_rq_caps.supported_qpts =
863 BIT(IB_QPT_RAW_PACKET);
864 }
865 }
866
Bodong Wang402ca532016-06-17 15:02:20 +0300867 if (uhw->outlen) {
868 err = ib_copy_to_udata(uhw, &resp, resp.response_length);
869
870 if (err)
871 return err;
872 }
873
Majd Dibbiny1b5daf12015-06-04 19:30:46 +0300874 return 0;
875}
Eli Cohene126ba92013-07-07 17:25:49 +0300876
Majd Dibbiny1b5daf12015-06-04 19:30:46 +0300877enum mlx5_ib_width {
878 MLX5_IB_WIDTH_1X = 1 << 0,
879 MLX5_IB_WIDTH_2X = 1 << 1,
880 MLX5_IB_WIDTH_4X = 1 << 2,
881 MLX5_IB_WIDTH_8X = 1 << 3,
882 MLX5_IB_WIDTH_12X = 1 << 4
883};
884
885static int translate_active_width(struct ib_device *ibdev, u8 active_width,
886 u8 *ib_width)
887{
888 struct mlx5_ib_dev *dev = to_mdev(ibdev);
889 int err = 0;
890
891 if (active_width & MLX5_IB_WIDTH_1X) {
892 *ib_width = IB_WIDTH_1X;
893 } else if (active_width & MLX5_IB_WIDTH_2X) {
894 mlx5_ib_dbg(dev, "active_width %d is not supported by IB spec\n",
895 (int)active_width);
896 err = -EINVAL;
897 } else if (active_width & MLX5_IB_WIDTH_4X) {
898 *ib_width = IB_WIDTH_4X;
899 } else if (active_width & MLX5_IB_WIDTH_8X) {
900 *ib_width = IB_WIDTH_8X;
901 } else if (active_width & MLX5_IB_WIDTH_12X) {
902 *ib_width = IB_WIDTH_12X;
903 } else {
904 mlx5_ib_dbg(dev, "Invalid active_width %d\n",
905 (int)active_width);
906 err = -EINVAL;
907 }
908
909 return err;
910}
911
912static int mlx5_mtu_to_ib_mtu(int mtu)
913{
914 switch (mtu) {
915 case 256: return 1;
916 case 512: return 2;
917 case 1024: return 3;
918 case 2048: return 4;
919 case 4096: return 5;
920 default:
921 pr_warn("invalid mtu\n");
922 return -1;
923 }
924}
925
926enum ib_max_vl_num {
927 __IB_MAX_VL_0 = 1,
928 __IB_MAX_VL_0_1 = 2,
929 __IB_MAX_VL_0_3 = 3,
930 __IB_MAX_VL_0_7 = 4,
931 __IB_MAX_VL_0_14 = 5,
932};
933
934enum mlx5_vl_hw_cap {
935 MLX5_VL_HW_0 = 1,
936 MLX5_VL_HW_0_1 = 2,
937 MLX5_VL_HW_0_2 = 3,
938 MLX5_VL_HW_0_3 = 4,
939 MLX5_VL_HW_0_4 = 5,
940 MLX5_VL_HW_0_5 = 6,
941 MLX5_VL_HW_0_6 = 7,
942 MLX5_VL_HW_0_7 = 8,
943 MLX5_VL_HW_0_14 = 15
944};
945
946static int translate_max_vl_num(struct ib_device *ibdev, u8 vl_hw_cap,
947 u8 *max_vl_num)
948{
949 switch (vl_hw_cap) {
950 case MLX5_VL_HW_0:
951 *max_vl_num = __IB_MAX_VL_0;
952 break;
953 case MLX5_VL_HW_0_1:
954 *max_vl_num = __IB_MAX_VL_0_1;
955 break;
956 case MLX5_VL_HW_0_3:
957 *max_vl_num = __IB_MAX_VL_0_3;
958 break;
959 case MLX5_VL_HW_0_7:
960 *max_vl_num = __IB_MAX_VL_0_7;
961 break;
962 case MLX5_VL_HW_0_14:
963 *max_vl_num = __IB_MAX_VL_0_14;
964 break;
965
966 default:
967 return -EINVAL;
968 }
969
970 return 0;
971}
972
973static int mlx5_query_hca_port(struct ib_device *ibdev, u8 port,
974 struct ib_port_attr *props)
975{
976 struct mlx5_ib_dev *dev = to_mdev(ibdev);
977 struct mlx5_core_dev *mdev = dev->mdev;
978 struct mlx5_hca_vport_context *rep;
Saeed Mahameed046339e2016-04-22 00:33:03 +0300979 u16 max_mtu;
980 u16 oper_mtu;
Majd Dibbiny1b5daf12015-06-04 19:30:46 +0300981 int err;
982 u8 ib_link_width_oper;
983 u8 vl_hw_cap;
984
985 rep = kzalloc(sizeof(*rep), GFP_KERNEL);
986 if (!rep) {
987 err = -ENOMEM;
988 goto out;
989 }
990
Or Gerlitzc4550c62017-01-24 13:02:39 +0200991 /* props being zeroed by the caller, avoid zeroing it here */
Majd Dibbiny1b5daf12015-06-04 19:30:46 +0300992
993 err = mlx5_query_hca_vport_context(mdev, 0, port, 0, rep);
994 if (err)
995 goto out;
996
997 props->lid = rep->lid;
998 props->lmc = rep->lmc;
999 props->sm_lid = rep->sm_lid;
1000 props->sm_sl = rep->sm_sl;
1001 props->state = rep->vport_state;
1002 props->phys_state = rep->port_physical_state;
1003 props->port_cap_flags = rep->cap_mask1;
1004 props->gid_tbl_len = mlx5_get_gid_table_len(MLX5_CAP_GEN(mdev, gid_table_size));
1005 props->max_msg_sz = 1 << MLX5_CAP_GEN(mdev, log_max_msg);
1006 props->pkey_tbl_len = mlx5_to_sw_pkey_sz(MLX5_CAP_GEN(mdev, pkey_table_size));
1007 props->bad_pkey_cntr = rep->pkey_violation_counter;
1008 props->qkey_viol_cntr = rep->qkey_violation_counter;
1009 props->subnet_timeout = rep->subnet_timeout;
1010 props->init_type_reply = rep->init_type_reply;
Eli Coheneff901d2016-03-11 22:58:42 +02001011 props->grh_required = rep->grh_required;
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03001012
1013 err = mlx5_query_port_link_width_oper(mdev, &ib_link_width_oper, port);
1014 if (err)
1015 goto out;
1016
1017 err = translate_active_width(ibdev, ib_link_width_oper,
1018 &props->active_width);
1019 if (err)
1020 goto out;
Noa Osherovichd5beb7f2016-06-02 10:47:53 +03001021 err = mlx5_query_port_ib_proto_oper(mdev, &props->active_speed, port);
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03001022 if (err)
1023 goto out;
1024
Saeed Mahameedfacc9692015-06-11 14:47:27 +03001025 mlx5_query_port_max_mtu(mdev, &max_mtu, port);
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03001026
1027 props->max_mtu = mlx5_mtu_to_ib_mtu(max_mtu);
1028
Saeed Mahameedfacc9692015-06-11 14:47:27 +03001029 mlx5_query_port_oper_mtu(mdev, &oper_mtu, port);
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03001030
1031 props->active_mtu = mlx5_mtu_to_ib_mtu(oper_mtu);
1032
1033 err = mlx5_query_port_vl_hw_cap(mdev, &vl_hw_cap, port);
1034 if (err)
1035 goto out;
1036
1037 err = translate_max_vl_num(ibdev, vl_hw_cap,
1038 &props->max_vl_num);
1039out:
1040 kfree(rep);
Eli Cohene126ba92013-07-07 17:25:49 +03001041 return err;
1042}
1043
1044int mlx5_ib_query_port(struct ib_device *ibdev, u8 port,
1045 struct ib_port_attr *props)
1046{
Ilan Tayari095b0922017-05-14 16:04:30 +03001047 unsigned int count;
1048 int ret;
1049
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03001050 switch (mlx5_get_vport_access_method(ibdev)) {
1051 case MLX5_VPORT_ACCESS_METHOD_MAD:
Ilan Tayari095b0922017-05-14 16:04:30 +03001052 ret = mlx5_query_mad_ifc_port(ibdev, port, props);
1053 break;
Eli Cohene126ba92013-07-07 17:25:49 +03001054
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03001055 case MLX5_VPORT_ACCESS_METHOD_HCA:
Ilan Tayari095b0922017-05-14 16:04:30 +03001056 ret = mlx5_query_hca_port(ibdev, port, props);
1057 break;
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03001058
Achiad Shochat3f89a642015-12-23 18:47:21 +02001059 case MLX5_VPORT_ACCESS_METHOD_NIC:
Ilan Tayari095b0922017-05-14 16:04:30 +03001060 ret = mlx5_query_port_roce(ibdev, port, props);
1061 break;
Achiad Shochat3f89a642015-12-23 18:47:21 +02001062
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03001063 default:
Ilan Tayari095b0922017-05-14 16:04:30 +03001064 ret = -EINVAL;
Eli Cohene126ba92013-07-07 17:25:49 +03001065 }
Ilan Tayari095b0922017-05-14 16:04:30 +03001066
1067 if (!ret && props) {
1068 count = mlx5_core_reserved_gids_count(to_mdev(ibdev)->mdev);
1069 props->gid_tbl_len -= count;
1070 }
1071 return ret;
Eli Cohene126ba92013-07-07 17:25:49 +03001072}
1073
1074static int mlx5_ib_query_gid(struct ib_device *ibdev, u8 port, int index,
1075 union ib_gid *gid)
1076{
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03001077 struct mlx5_ib_dev *dev = to_mdev(ibdev);
1078 struct mlx5_core_dev *mdev = dev->mdev;
Eli Cohene126ba92013-07-07 17:25:49 +03001079
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03001080 switch (mlx5_get_vport_access_method(ibdev)) {
1081 case MLX5_VPORT_ACCESS_METHOD_MAD:
1082 return mlx5_query_mad_ifc_gids(ibdev, port, index, gid);
Eli Cohene126ba92013-07-07 17:25:49 +03001083
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03001084 case MLX5_VPORT_ACCESS_METHOD_HCA:
1085 return mlx5_query_hca_vport_gid(mdev, 0, port, 0, index, gid);
Eli Cohene126ba92013-07-07 17:25:49 +03001086
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03001087 default:
1088 return -EINVAL;
1089 }
Eli Cohene126ba92013-07-07 17:25:49 +03001090
Eli Cohene126ba92013-07-07 17:25:49 +03001091}
1092
1093static int mlx5_ib_query_pkey(struct ib_device *ibdev, u8 port, u16 index,
1094 u16 *pkey)
1095{
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03001096 struct mlx5_ib_dev *dev = to_mdev(ibdev);
1097 struct mlx5_core_dev *mdev = dev->mdev;
Eli Cohene126ba92013-07-07 17:25:49 +03001098
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03001099 switch (mlx5_get_vport_access_method(ibdev)) {
1100 case MLX5_VPORT_ACCESS_METHOD_MAD:
1101 return mlx5_query_mad_ifc_pkey(ibdev, port, index, pkey);
Eli Cohene126ba92013-07-07 17:25:49 +03001102
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03001103 case MLX5_VPORT_ACCESS_METHOD_HCA:
1104 case MLX5_VPORT_ACCESS_METHOD_NIC:
1105 return mlx5_query_hca_vport_pkey(mdev, 0, port, 0, index,
1106 pkey);
1107 default:
1108 return -EINVAL;
1109 }
Eli Cohene126ba92013-07-07 17:25:49 +03001110}
1111
Eli Cohene126ba92013-07-07 17:25:49 +03001112static int mlx5_ib_modify_device(struct ib_device *ibdev, int mask,
1113 struct ib_device_modify *props)
1114{
1115 struct mlx5_ib_dev *dev = to_mdev(ibdev);
1116 struct mlx5_reg_node_desc in;
1117 struct mlx5_reg_node_desc out;
1118 int err;
1119
1120 if (mask & ~IB_DEVICE_MODIFY_NODE_DESC)
1121 return -EOPNOTSUPP;
1122
1123 if (!(mask & IB_DEVICE_MODIFY_NODE_DESC))
1124 return 0;
1125
1126 /*
1127 * If possible, pass node desc to FW, so it can generate
1128 * a 144 trap. If cmd fails, just ignore.
1129 */
Yuval Shaiabd99fde2016-08-25 10:57:07 -07001130 memcpy(&in, props->node_desc, IB_DEVICE_NODE_DESC_MAX);
Jack Morgenstein9603b612014-07-28 23:30:22 +03001131 err = mlx5_core_access_reg(dev->mdev, &in, sizeof(in), &out,
Eli Cohene126ba92013-07-07 17:25:49 +03001132 sizeof(out), MLX5_REG_NODE_DESC, 0, 1);
1133 if (err)
1134 return err;
1135
Yuval Shaiabd99fde2016-08-25 10:57:07 -07001136 memcpy(ibdev->node_desc, props->node_desc, IB_DEVICE_NODE_DESC_MAX);
Eli Cohene126ba92013-07-07 17:25:49 +03001137
1138 return err;
1139}
1140
Eli Cohencdbe33d2017-02-14 07:25:38 +02001141static int set_port_caps_atomic(struct mlx5_ib_dev *dev, u8 port_num, u32 mask,
1142 u32 value)
1143{
1144 struct mlx5_hca_vport_context ctx = {};
1145 int err;
1146
1147 err = mlx5_query_hca_vport_context(dev->mdev, 0,
1148 port_num, 0, &ctx);
1149 if (err)
1150 return err;
1151
1152 if (~ctx.cap_mask1_perm & mask) {
1153 mlx5_ib_warn(dev, "trying to change bitmask 0x%X but change supported 0x%X\n",
1154 mask, ctx.cap_mask1_perm);
1155 return -EINVAL;
1156 }
1157
1158 ctx.cap_mask1 = value;
1159 ctx.cap_mask1_perm = mask;
1160 err = mlx5_core_modify_hca_vport_context(dev->mdev, 0,
1161 port_num, 0, &ctx);
1162
1163 return err;
1164}
1165
Eli Cohene126ba92013-07-07 17:25:49 +03001166static int mlx5_ib_modify_port(struct ib_device *ibdev, u8 port, int mask,
1167 struct ib_port_modify *props)
1168{
1169 struct mlx5_ib_dev *dev = to_mdev(ibdev);
1170 struct ib_port_attr attr;
1171 u32 tmp;
1172 int err;
Eli Cohencdbe33d2017-02-14 07:25:38 +02001173 u32 change_mask;
1174 u32 value;
1175 bool is_ib = (mlx5_ib_port_link_layer(ibdev, port) ==
1176 IB_LINK_LAYER_INFINIBAND);
1177
Majd Dibbinyec255872017-08-23 08:35:42 +03001178 /* CM layer calls ib_modify_port() regardless of the link layer. For
1179 * Ethernet ports, qkey violation and Port capabilities are meaningless.
1180 */
1181 if (!is_ib)
1182 return 0;
1183
Eli Cohencdbe33d2017-02-14 07:25:38 +02001184 if (MLX5_CAP_GEN(dev->mdev, ib_virt) && is_ib) {
1185 change_mask = props->clr_port_cap_mask | props->set_port_cap_mask;
1186 value = ~props->clr_port_cap_mask | props->set_port_cap_mask;
1187 return set_port_caps_atomic(dev, port, change_mask, value);
1188 }
Eli Cohene126ba92013-07-07 17:25:49 +03001189
1190 mutex_lock(&dev->cap_mask_mutex);
1191
Or Gerlitzc4550c62017-01-24 13:02:39 +02001192 err = ib_query_port(ibdev, port, &attr);
Eli Cohene126ba92013-07-07 17:25:49 +03001193 if (err)
1194 goto out;
1195
1196 tmp = (attr.port_cap_flags | props->set_port_cap_mask) &
1197 ~props->clr_port_cap_mask;
1198
Jack Morgenstein9603b612014-07-28 23:30:22 +03001199 err = mlx5_set_port_caps(dev->mdev, port, tmp);
Eli Cohene126ba92013-07-07 17:25:49 +03001200
1201out:
1202 mutex_unlock(&dev->cap_mask_mutex);
1203 return err;
1204}
1205
Eli Cohen30aa60b2017-01-03 23:55:27 +02001206static void print_lib_caps(struct mlx5_ib_dev *dev, u64 caps)
1207{
1208 mlx5_ib_dbg(dev, "MLX5_LIB_CAP_4K_UAR = %s\n",
1209 caps & MLX5_LIB_CAP_4K_UAR ? "y" : "n");
1210}
1211
Eli Cohenb037c292017-01-03 23:55:26 +02001212static int calc_total_bfregs(struct mlx5_ib_dev *dev, bool lib_uar_4k,
1213 struct mlx5_ib_alloc_ucontext_req_v2 *req,
1214 u32 *num_sys_pages)
1215{
1216 int uars_per_sys_page;
1217 int bfregs_per_sys_page;
1218 int ref_bfregs = req->total_num_bfregs;
1219
1220 if (req->total_num_bfregs == 0)
1221 return -EINVAL;
1222
1223 BUILD_BUG_ON(MLX5_MAX_BFREGS % MLX5_NON_FP_BFREGS_IN_PAGE);
1224 BUILD_BUG_ON(MLX5_MAX_BFREGS < MLX5_NON_FP_BFREGS_IN_PAGE);
1225
1226 if (req->total_num_bfregs > MLX5_MAX_BFREGS)
1227 return -ENOMEM;
1228
1229 uars_per_sys_page = get_uars_per_sys_page(dev, lib_uar_4k);
1230 bfregs_per_sys_page = uars_per_sys_page * MLX5_NON_FP_BFREGS_PER_UAR;
1231 req->total_num_bfregs = ALIGN(req->total_num_bfregs, bfregs_per_sys_page);
1232 *num_sys_pages = req->total_num_bfregs / bfregs_per_sys_page;
1233
1234 if (req->num_low_latency_bfregs > req->total_num_bfregs - 1)
1235 return -EINVAL;
1236
Colin Ian King9c2d33d2017-06-27 08:40:59 +01001237 mlx5_ib_dbg(dev, "uar_4k: fw support %s, lib support %s, user requested %d bfregs, allocated %d, using %d sys pages\n",
Eli Cohenb037c292017-01-03 23:55:26 +02001238 MLX5_CAP_GEN(dev->mdev, uar_4k) ? "yes" : "no",
1239 lib_uar_4k ? "yes" : "no", ref_bfregs,
1240 req->total_num_bfregs, *num_sys_pages);
1241
1242 return 0;
1243}
1244
1245static int allocate_uars(struct mlx5_ib_dev *dev, struct mlx5_ib_ucontext *context)
1246{
1247 struct mlx5_bfreg_info *bfregi;
1248 int err;
1249 int i;
1250
1251 bfregi = &context->bfregi;
1252 for (i = 0; i < bfregi->num_sys_pages; i++) {
1253 err = mlx5_cmd_alloc_uar(dev->mdev, &bfregi->sys_pages[i]);
1254 if (err)
1255 goto error;
1256
1257 mlx5_ib_dbg(dev, "allocated uar %d\n", bfregi->sys_pages[i]);
1258 }
1259 return 0;
1260
1261error:
1262 for (--i; i >= 0; i--)
1263 if (mlx5_cmd_free_uar(dev->mdev, bfregi->sys_pages[i]))
1264 mlx5_ib_warn(dev, "failed to free uar %d\n", i);
1265
1266 return err;
1267}
1268
1269static int deallocate_uars(struct mlx5_ib_dev *dev, struct mlx5_ib_ucontext *context)
1270{
1271 struct mlx5_bfreg_info *bfregi;
1272 int err;
1273 int i;
1274
1275 bfregi = &context->bfregi;
1276 for (i = 0; i < bfregi->num_sys_pages; i++) {
1277 err = mlx5_cmd_free_uar(dev->mdev, bfregi->sys_pages[i]);
1278 if (err) {
1279 mlx5_ib_warn(dev, "failed to free uar %d\n", i);
1280 return err;
1281 }
1282 }
1283 return 0;
1284}
1285
Huy Nguyenc85023e2017-05-30 09:42:54 +03001286static int mlx5_ib_alloc_transport_domain(struct mlx5_ib_dev *dev, u32 *tdn)
1287{
1288 int err;
1289
1290 err = mlx5_core_alloc_transport_domain(dev->mdev, tdn);
1291 if (err)
1292 return err;
1293
1294 if ((MLX5_CAP_GEN(dev->mdev, port_type) != MLX5_CAP_PORT_TYPE_ETH) ||
1295 !MLX5_CAP_GEN(dev->mdev, disable_local_lb))
1296 return err;
1297
1298 mutex_lock(&dev->lb_mutex);
1299 dev->user_td++;
1300
1301 if (dev->user_td == 2)
1302 err = mlx5_nic_vport_update_local_lb(dev->mdev, true);
1303
1304 mutex_unlock(&dev->lb_mutex);
1305 return err;
1306}
1307
1308static void mlx5_ib_dealloc_transport_domain(struct mlx5_ib_dev *dev, u32 tdn)
1309{
1310 mlx5_core_dealloc_transport_domain(dev->mdev, tdn);
1311
1312 if ((MLX5_CAP_GEN(dev->mdev, port_type) != MLX5_CAP_PORT_TYPE_ETH) ||
1313 !MLX5_CAP_GEN(dev->mdev, disable_local_lb))
1314 return;
1315
1316 mutex_lock(&dev->lb_mutex);
1317 dev->user_td--;
1318
1319 if (dev->user_td < 2)
1320 mlx5_nic_vport_update_local_lb(dev->mdev, false);
1321
1322 mutex_unlock(&dev->lb_mutex);
1323}
1324
Eli Cohene126ba92013-07-07 17:25:49 +03001325static struct ib_ucontext *mlx5_ib_alloc_ucontext(struct ib_device *ibdev,
1326 struct ib_udata *udata)
1327{
1328 struct mlx5_ib_dev *dev = to_mdev(ibdev);
Matan Barakb368d7c2015-12-15 20:30:12 +02001329 struct mlx5_ib_alloc_ucontext_req_v2 req = {};
1330 struct mlx5_ib_alloc_ucontext_resp resp = {};
Eli Cohene126ba92013-07-07 17:25:49 +03001331 struct mlx5_ib_ucontext *context;
Eli Cohen2f5ff262017-01-03 23:55:21 +02001332 struct mlx5_bfreg_info *bfregi;
Eli Cohen78c0f982014-01-30 13:49:48 +02001333 int ver;
Eli Cohene126ba92013-07-07 17:25:49 +03001334 int err;
Majd Dibbinya168a41c2016-01-28 17:51:47 +02001335 size_t min_req_v2 = offsetof(struct mlx5_ib_alloc_ucontext_req_v2,
1336 max_cqe_version);
Eli Cohenb037c292017-01-03 23:55:26 +02001337 bool lib_uar_4k;
Eli Cohene126ba92013-07-07 17:25:49 +03001338
1339 if (!dev->ib_active)
1340 return ERR_PTR(-EAGAIN);
1341
Amrani, Rame0931112017-06-27 17:04:42 +03001342 if (udata->inlen == sizeof(struct mlx5_ib_alloc_ucontext_req))
Eli Cohen78c0f982014-01-30 13:49:48 +02001343 ver = 0;
Amrani, Rame0931112017-06-27 17:04:42 +03001344 else if (udata->inlen >= min_req_v2)
Eli Cohen78c0f982014-01-30 13:49:48 +02001345 ver = 2;
1346 else
1347 return ERR_PTR(-EINVAL);
1348
Amrani, Rame0931112017-06-27 17:04:42 +03001349 err = ib_copy_from_udata(&req, udata, min(udata->inlen, sizeof(req)));
Eli Cohene126ba92013-07-07 17:25:49 +03001350 if (err)
1351 return ERR_PTR(err);
1352
Matan Barakb368d7c2015-12-15 20:30:12 +02001353 if (req.flags)
Eli Cohen78c0f982014-01-30 13:49:48 +02001354 return ERR_PTR(-EINVAL);
1355
Haggai Abramovskyf72300c2016-01-14 19:12:58 +02001356 if (req.comp_mask || req.reserved0 || req.reserved1 || req.reserved2)
Matan Barakb368d7c2015-12-15 20:30:12 +02001357 return ERR_PTR(-EOPNOTSUPP);
1358
Eli Cohen2f5ff262017-01-03 23:55:21 +02001359 req.total_num_bfregs = ALIGN(req.total_num_bfregs,
1360 MLX5_NON_FP_BFREGS_PER_UAR);
1361 if (req.num_low_latency_bfregs > req.total_num_bfregs - 1)
Eli Cohene126ba92013-07-07 17:25:49 +03001362 return ERR_PTR(-EINVAL);
1363
Saeed Mahameed938fe832015-05-28 22:28:41 +03001364 resp.qp_tab_size = 1 << MLX5_CAP_GEN(dev->mdev, log_max_qp);
Noa Osherovich2cc6ad52016-06-04 15:15:33 +03001365 if (mlx5_core_is_pf(dev->mdev) && MLX5_CAP_GEN(dev->mdev, bf))
1366 resp.bf_reg_size = 1 << MLX5_CAP_GEN(dev->mdev, log_bf_reg_size);
Daniel Jurgensb47bd6e2016-10-25 18:36:24 +03001367 resp.cache_line_size = cache_line_size();
Saeed Mahameed938fe832015-05-28 22:28:41 +03001368 resp.max_sq_desc_sz = MLX5_CAP_GEN(dev->mdev, max_wqe_sz_sq);
1369 resp.max_rq_desc_sz = MLX5_CAP_GEN(dev->mdev, max_wqe_sz_rq);
1370 resp.max_send_wqebb = 1 << MLX5_CAP_GEN(dev->mdev, log_max_qp_sz);
1371 resp.max_recv_wr = 1 << MLX5_CAP_GEN(dev->mdev, log_max_qp_sz);
1372 resp.max_srq_recv_wr = 1 << MLX5_CAP_GEN(dev->mdev, log_max_srq_sz);
Haggai Abramovskyf72300c2016-01-14 19:12:58 +02001373 resp.cqe_version = min_t(__u8,
1374 (__u8)MLX5_CAP_GEN(dev->mdev, cqe_version),
1375 req.max_cqe_version);
Eli Cohen30aa60b2017-01-03 23:55:27 +02001376 resp.log_uar_size = MLX5_CAP_GEN(dev->mdev, uar_4k) ?
1377 MLX5_ADAPTER_PAGE_SHIFT : PAGE_SHIFT;
1378 resp.num_uars_per_page = MLX5_CAP_GEN(dev->mdev, uar_4k) ?
1379 MLX5_CAP_GEN(dev->mdev, num_of_uars_per_page) : 1;
Matan Barakb368d7c2015-12-15 20:30:12 +02001380 resp.response_length = min(offsetof(typeof(resp), response_length) +
1381 sizeof(resp.response_length), udata->outlen);
Eli Cohene126ba92013-07-07 17:25:49 +03001382
1383 context = kzalloc(sizeof(*context), GFP_KERNEL);
1384 if (!context)
1385 return ERR_PTR(-ENOMEM);
1386
Eli Cohen30aa60b2017-01-03 23:55:27 +02001387 lib_uar_4k = req.lib_caps & MLX5_LIB_CAP_4K_UAR;
Eli Cohen2f5ff262017-01-03 23:55:21 +02001388 bfregi = &context->bfregi;
Eli Cohenb037c292017-01-03 23:55:26 +02001389
1390 /* updates req->total_num_bfregs */
1391 err = calc_total_bfregs(dev, lib_uar_4k, &req, &bfregi->num_sys_pages);
1392 if (err)
1393 goto out_ctx;
1394
Eli Cohen2f5ff262017-01-03 23:55:21 +02001395 mutex_init(&bfregi->lock);
Eli Cohenb037c292017-01-03 23:55:26 +02001396 bfregi->lib_uar_4k = lib_uar_4k;
1397 bfregi->count = kcalloc(req.total_num_bfregs, sizeof(*bfregi->count),
1398 GFP_KERNEL);
1399 if (!bfregi->count) {
Eli Cohene126ba92013-07-07 17:25:49 +03001400 err = -ENOMEM;
1401 goto out_ctx;
1402 }
1403
Eli Cohenb037c292017-01-03 23:55:26 +02001404 bfregi->sys_pages = kcalloc(bfregi->num_sys_pages,
1405 sizeof(*bfregi->sys_pages),
1406 GFP_KERNEL);
1407 if (!bfregi->sys_pages) {
Eli Cohene126ba92013-07-07 17:25:49 +03001408 err = -ENOMEM;
Eli Cohenb037c292017-01-03 23:55:26 +02001409 goto out_count;
Eli Cohene126ba92013-07-07 17:25:49 +03001410 }
1411
Eli Cohenb037c292017-01-03 23:55:26 +02001412 err = allocate_uars(dev, context);
1413 if (err)
1414 goto out_sys_pages;
Eli Cohene126ba92013-07-07 17:25:49 +03001415
Haggai Eranb4cfe442014-12-11 17:04:26 +02001416#ifdef CONFIG_INFINIBAND_ON_DEMAND_PAGING
1417 context->ibucontext.invalidate_range = &mlx5_ib_invalidate_range;
1418#endif
1419
Artemy Kovalyov7d0cc6e2017-01-02 11:37:44 +02001420 context->upd_xlt_page = __get_free_page(GFP_KERNEL);
1421 if (!context->upd_xlt_page) {
1422 err = -ENOMEM;
1423 goto out_uars;
1424 }
1425 mutex_init(&context->upd_xlt_page_mutex);
1426
majd@mellanox.com146d2f12016-01-14 19:13:02 +02001427 if (MLX5_CAP_GEN(dev->mdev, log_max_transport_domain)) {
Huy Nguyenc85023e2017-05-30 09:42:54 +03001428 err = mlx5_ib_alloc_transport_domain(dev, &context->tdn);
majd@mellanox.com146d2f12016-01-14 19:13:02 +02001429 if (err)
Artemy Kovalyov7d0cc6e2017-01-02 11:37:44 +02001430 goto out_page;
majd@mellanox.com146d2f12016-01-14 19:13:02 +02001431 }
1432
Maor Gottlieb7c2344c2016-06-17 14:56:44 +03001433 INIT_LIST_HEAD(&context->vma_private_list);
Eli Cohene126ba92013-07-07 17:25:49 +03001434 INIT_LIST_HEAD(&context->db_page_list);
1435 mutex_init(&context->db_page_mutex);
1436
Eli Cohen2f5ff262017-01-03 23:55:21 +02001437 resp.tot_bfregs = req.total_num_bfregs;
Saeed Mahameed938fe832015-05-28 22:28:41 +03001438 resp.num_ports = MLX5_CAP_GEN(dev->mdev, num_ports);
Matan Barakb368d7c2015-12-15 20:30:12 +02001439
Haggai Abramovskyf72300c2016-01-14 19:12:58 +02001440 if (field_avail(typeof(resp), cqe_version, udata->outlen))
1441 resp.response_length += sizeof(resp.cqe_version);
Matan Barakb368d7c2015-12-15 20:30:12 +02001442
Bodong Wang402ca532016-06-17 15:02:20 +03001443 if (field_avail(typeof(resp), cmds_supp_uhw, udata->outlen)) {
Moni Shoua6ad279c52016-11-23 08:23:23 +02001444 resp.cmds_supp_uhw |= MLX5_USER_CMDS_SUPP_UHW_QUERY_DEVICE |
1445 MLX5_USER_CMDS_SUPP_UHW_CREATE_AH;
Bodong Wang402ca532016-06-17 15:02:20 +03001446 resp.response_length += sizeof(resp.cmds_supp_uhw);
1447 }
1448
Or Gerlitz78984892016-11-30 20:33:33 +02001449 if (field_avail(typeof(resp), eth_min_inline, udata->outlen)) {
1450 if (mlx5_ib_port_link_layer(ibdev, 1) == IB_LINK_LAYER_ETHERNET) {
1451 mlx5_query_min_inline(dev->mdev, &resp.eth_min_inline);
1452 resp.eth_min_inline++;
1453 }
1454 resp.response_length += sizeof(resp.eth_min_inline);
1455 }
1456
Noa Osherovichbc5c6ee2016-06-04 15:15:31 +03001457 /*
1458 * We don't want to expose information from the PCI bar that is located
1459 * after 4096 bytes, so if the arch only supports larger pages, let's
1460 * pretend we don't support reading the HCA's core clock. This is also
1461 * forced by mmap function.
1462 */
Eli Cohende8d6e02017-01-03 23:55:19 +02001463 if (field_avail(typeof(resp), hca_core_clock_offset, udata->outlen)) {
1464 if (PAGE_SIZE <= 4096) {
1465 resp.comp_mask |=
1466 MLX5_IB_ALLOC_UCONTEXT_RESP_MASK_CORE_CLOCK_OFFSET;
1467 resp.hca_core_clock_offset =
1468 offsetof(struct mlx5_init_seg, internal_timer_h) % PAGE_SIZE;
1469 }
Haggai Abramovskyf72300c2016-01-14 19:12:58 +02001470 resp.response_length += sizeof(resp.hca_core_clock_offset) +
Bodong Wang402ca532016-06-17 15:02:20 +03001471 sizeof(resp.reserved2);
Matan Barakb368d7c2015-12-15 20:30:12 +02001472 }
1473
Eli Cohen30aa60b2017-01-03 23:55:27 +02001474 if (field_avail(typeof(resp), log_uar_size, udata->outlen))
1475 resp.response_length += sizeof(resp.log_uar_size);
1476
1477 if (field_avail(typeof(resp), num_uars_per_page, udata->outlen))
1478 resp.response_length += sizeof(resp.num_uars_per_page);
1479
Matan Barakb368d7c2015-12-15 20:30:12 +02001480 err = ib_copy_to_udata(udata, &resp, resp.response_length);
Eli Cohene126ba92013-07-07 17:25:49 +03001481 if (err)
majd@mellanox.com146d2f12016-01-14 19:13:02 +02001482 goto out_td;
Eli Cohene126ba92013-07-07 17:25:49 +03001483
Eli Cohen2f5ff262017-01-03 23:55:21 +02001484 bfregi->ver = ver;
1485 bfregi->num_low_latency_bfregs = req.num_low_latency_bfregs;
Haggai Abramovskyf72300c2016-01-14 19:12:58 +02001486 context->cqe_version = resp.cqe_version;
Eli Cohen30aa60b2017-01-03 23:55:27 +02001487 context->lib_caps = req.lib_caps;
1488 print_lib_caps(dev, context->lib_caps);
Haggai Abramovskyf72300c2016-01-14 19:12:58 +02001489
Eli Cohene126ba92013-07-07 17:25:49 +03001490 return &context->ibucontext;
1491
majd@mellanox.com146d2f12016-01-14 19:13:02 +02001492out_td:
1493 if (MLX5_CAP_GEN(dev->mdev, log_max_transport_domain))
Huy Nguyenc85023e2017-05-30 09:42:54 +03001494 mlx5_ib_dealloc_transport_domain(dev, context->tdn);
majd@mellanox.com146d2f12016-01-14 19:13:02 +02001495
Artemy Kovalyov7d0cc6e2017-01-02 11:37:44 +02001496out_page:
1497 free_page(context->upd_xlt_page);
1498
Eli Cohene126ba92013-07-07 17:25:49 +03001499out_uars:
Eli Cohenb037c292017-01-03 23:55:26 +02001500 deallocate_uars(dev, context);
1501
1502out_sys_pages:
1503 kfree(bfregi->sys_pages);
1504
Eli Cohene126ba92013-07-07 17:25:49 +03001505out_count:
Eli Cohen2f5ff262017-01-03 23:55:21 +02001506 kfree(bfregi->count);
Eli Cohene126ba92013-07-07 17:25:49 +03001507
Eli Cohene126ba92013-07-07 17:25:49 +03001508out_ctx:
1509 kfree(context);
Eli Cohenb037c292017-01-03 23:55:26 +02001510
Eli Cohene126ba92013-07-07 17:25:49 +03001511 return ERR_PTR(err);
1512}
1513
1514static int mlx5_ib_dealloc_ucontext(struct ib_ucontext *ibcontext)
1515{
1516 struct mlx5_ib_ucontext *context = to_mucontext(ibcontext);
1517 struct mlx5_ib_dev *dev = to_mdev(ibcontext->device);
Eli Cohenb037c292017-01-03 23:55:26 +02001518 struct mlx5_bfreg_info *bfregi;
Eli Cohene126ba92013-07-07 17:25:49 +03001519
Eli Cohenb037c292017-01-03 23:55:26 +02001520 bfregi = &context->bfregi;
majd@mellanox.com146d2f12016-01-14 19:13:02 +02001521 if (MLX5_CAP_GEN(dev->mdev, log_max_transport_domain))
Huy Nguyenc85023e2017-05-30 09:42:54 +03001522 mlx5_ib_dealloc_transport_domain(dev, context->tdn);
majd@mellanox.com146d2f12016-01-14 19:13:02 +02001523
Artemy Kovalyov7d0cc6e2017-01-02 11:37:44 +02001524 free_page(context->upd_xlt_page);
Eli Cohenb037c292017-01-03 23:55:26 +02001525 deallocate_uars(dev, context);
1526 kfree(bfregi->sys_pages);
Eli Cohen2f5ff262017-01-03 23:55:21 +02001527 kfree(bfregi->count);
Eli Cohene126ba92013-07-07 17:25:49 +03001528 kfree(context);
1529
1530 return 0;
1531}
1532
Eli Cohenb037c292017-01-03 23:55:26 +02001533static phys_addr_t uar_index2pfn(struct mlx5_ib_dev *dev,
1534 struct mlx5_bfreg_info *bfregi,
1535 int idx)
Eli Cohene126ba92013-07-07 17:25:49 +03001536{
Eli Cohenb037c292017-01-03 23:55:26 +02001537 int fw_uars_per_page;
1538
1539 fw_uars_per_page = MLX5_CAP_GEN(dev->mdev, uar_4k) ? MLX5_UARS_IN_PAGE : 1;
1540
1541 return (pci_resource_start(dev->mdev->pdev, 0) >> PAGE_SHIFT) +
1542 bfregi->sys_pages[idx] / fw_uars_per_page;
Eli Cohene126ba92013-07-07 17:25:49 +03001543}
1544
1545static int get_command(unsigned long offset)
1546{
1547 return (offset >> MLX5_IB_MMAP_CMD_SHIFT) & MLX5_IB_MMAP_CMD_MASK;
1548}
1549
1550static int get_arg(unsigned long offset)
1551{
1552 return offset & ((1 << MLX5_IB_MMAP_CMD_SHIFT) - 1);
1553}
1554
1555static int get_index(unsigned long offset)
1556{
1557 return get_arg(offset);
1558}
1559
Maor Gottlieb7c2344c2016-06-17 14:56:44 +03001560static void mlx5_ib_vma_open(struct vm_area_struct *area)
1561{
1562 /* vma_open is called when a new VMA is created on top of our VMA. This
1563 * is done through either mremap flow or split_vma (usually due to
1564 * mlock, madvise, munmap, etc.) We do not support a clone of the VMA,
1565 * as this VMA is strongly hardware related. Therefore we set the
1566 * vm_ops of the newly created/cloned VMA to NULL, to prevent it from
1567 * calling us again and trying to do incorrect actions. We assume that
1568 * the original VMA size is exactly a single page, and therefore all
1569 * "splitting" operation will not happen to it.
1570 */
1571 area->vm_ops = NULL;
1572}
1573
1574static void mlx5_ib_vma_close(struct vm_area_struct *area)
1575{
1576 struct mlx5_ib_vma_private_data *mlx5_ib_vma_priv_data;
1577
1578 /* It's guaranteed that all VMAs opened on a FD are closed before the
1579 * file itself is closed, therefore no sync is needed with the regular
1580 * closing flow. (e.g. mlx5 ib_dealloc_ucontext)
1581 * However need a sync with accessing the vma as part of
1582 * mlx5_ib_disassociate_ucontext.
1583 * The close operation is usually called under mm->mmap_sem except when
1584 * process is exiting.
1585 * The exiting case is handled explicitly as part of
1586 * mlx5_ib_disassociate_ucontext.
1587 */
1588 mlx5_ib_vma_priv_data = (struct mlx5_ib_vma_private_data *)area->vm_private_data;
1589
1590 /* setting the vma context pointer to null in the mlx5_ib driver's
1591 * private data, to protect a race condition in
1592 * mlx5_ib_disassociate_ucontext().
1593 */
1594 mlx5_ib_vma_priv_data->vma = NULL;
1595 list_del(&mlx5_ib_vma_priv_data->list);
1596 kfree(mlx5_ib_vma_priv_data);
1597}
1598
1599static const struct vm_operations_struct mlx5_ib_vm_ops = {
1600 .open = mlx5_ib_vma_open,
1601 .close = mlx5_ib_vma_close
1602};
1603
1604static int mlx5_ib_set_vma_data(struct vm_area_struct *vma,
1605 struct mlx5_ib_ucontext *ctx)
1606{
1607 struct mlx5_ib_vma_private_data *vma_prv;
1608 struct list_head *vma_head = &ctx->vma_private_list;
1609
1610 vma_prv = kzalloc(sizeof(*vma_prv), GFP_KERNEL);
1611 if (!vma_prv)
1612 return -ENOMEM;
1613
1614 vma_prv->vma = vma;
1615 vma->vm_private_data = vma_prv;
1616 vma->vm_ops = &mlx5_ib_vm_ops;
1617
1618 list_add(&vma_prv->list, vma_head);
1619
1620 return 0;
1621}
1622
1623static void mlx5_ib_disassociate_ucontext(struct ib_ucontext *ibcontext)
1624{
1625 int ret;
1626 struct vm_area_struct *vma;
1627 struct mlx5_ib_vma_private_data *vma_private, *n;
1628 struct mlx5_ib_ucontext *context = to_mucontext(ibcontext);
1629 struct task_struct *owning_process = NULL;
1630 struct mm_struct *owning_mm = NULL;
1631
1632 owning_process = get_pid_task(ibcontext->tgid, PIDTYPE_PID);
1633 if (!owning_process)
1634 return;
1635
1636 owning_mm = get_task_mm(owning_process);
1637 if (!owning_mm) {
1638 pr_info("no mm, disassociate ucontext is pending task termination\n");
1639 while (1) {
1640 put_task_struct(owning_process);
1641 usleep_range(1000, 2000);
1642 owning_process = get_pid_task(ibcontext->tgid,
1643 PIDTYPE_PID);
1644 if (!owning_process ||
1645 owning_process->state == TASK_DEAD) {
1646 pr_info("disassociate ucontext done, task was terminated\n");
1647 /* in case task was dead need to release the
1648 * task struct.
1649 */
1650 if (owning_process)
1651 put_task_struct(owning_process);
1652 return;
1653 }
1654 }
1655 }
1656
1657 /* need to protect from a race on closing the vma as part of
1658 * mlx5_ib_vma_close.
1659 */
Maor Gottliebecc7d832017-03-29 06:03:02 +03001660 down_write(&owning_mm->mmap_sem);
Maor Gottlieb7c2344c2016-06-17 14:56:44 +03001661 list_for_each_entry_safe(vma_private, n, &context->vma_private_list,
1662 list) {
1663 vma = vma_private->vma;
1664 ret = zap_vma_ptes(vma, vma->vm_start,
1665 PAGE_SIZE);
1666 WARN_ONCE(ret, "%s: zap_vma_ptes failed", __func__);
1667 /* context going to be destroyed, should
1668 * not access ops any more.
1669 */
Maor Gottlieb13776612017-03-29 06:03:03 +03001670 vma->vm_flags &= ~(VM_SHARED | VM_MAYSHARE);
Maor Gottlieb7c2344c2016-06-17 14:56:44 +03001671 vma->vm_ops = NULL;
1672 list_del(&vma_private->list);
1673 kfree(vma_private);
1674 }
Maor Gottliebecc7d832017-03-29 06:03:02 +03001675 up_write(&owning_mm->mmap_sem);
Maor Gottlieb7c2344c2016-06-17 14:56:44 +03001676 mmput(owning_mm);
1677 put_task_struct(owning_process);
1678}
1679
Guy Levi37aa5c32016-04-27 16:49:50 +03001680static inline char *mmap_cmd2str(enum mlx5_ib_mmap_cmd cmd)
1681{
1682 switch (cmd) {
1683 case MLX5_IB_MMAP_WC_PAGE:
1684 return "WC";
1685 case MLX5_IB_MMAP_REGULAR_PAGE:
1686 return "best effort WC";
1687 case MLX5_IB_MMAP_NC_PAGE:
1688 return "NC";
1689 default:
1690 return NULL;
1691 }
1692}
1693
1694static int uar_mmap(struct mlx5_ib_dev *dev, enum mlx5_ib_mmap_cmd cmd,
Maor Gottlieb7c2344c2016-06-17 14:56:44 +03001695 struct vm_area_struct *vma,
1696 struct mlx5_ib_ucontext *context)
Guy Levi37aa5c32016-04-27 16:49:50 +03001697{
Eli Cohen2f5ff262017-01-03 23:55:21 +02001698 struct mlx5_bfreg_info *bfregi = &context->bfregi;
Guy Levi37aa5c32016-04-27 16:49:50 +03001699 int err;
1700 unsigned long idx;
1701 phys_addr_t pfn, pa;
1702 pgprot_t prot;
Eli Cohenb037c292017-01-03 23:55:26 +02001703 int uars_per_page;
1704
1705 if (vma->vm_end - vma->vm_start != PAGE_SIZE)
1706 return -EINVAL;
1707
1708 uars_per_page = get_uars_per_sys_page(dev, bfregi->lib_uar_4k);
1709 idx = get_index(vma->vm_pgoff);
1710 if (idx % uars_per_page ||
1711 idx * uars_per_page >= bfregi->num_sys_pages) {
1712 mlx5_ib_warn(dev, "invalid uar index %lu\n", idx);
1713 return -EINVAL;
1714 }
Guy Levi37aa5c32016-04-27 16:49:50 +03001715
1716 switch (cmd) {
1717 case MLX5_IB_MMAP_WC_PAGE:
1718/* Some architectures don't support WC memory */
1719#if defined(CONFIG_X86)
1720 if (!pat_enabled())
1721 return -EPERM;
1722#elif !(defined(CONFIG_PPC) || (defined(CONFIG_ARM) && defined(CONFIG_MMU)))
1723 return -EPERM;
1724#endif
1725 /* fall through */
1726 case MLX5_IB_MMAP_REGULAR_PAGE:
1727 /* For MLX5_IB_MMAP_REGULAR_PAGE do the best effort to get WC */
1728 prot = pgprot_writecombine(vma->vm_page_prot);
1729 break;
1730 case MLX5_IB_MMAP_NC_PAGE:
1731 prot = pgprot_noncached(vma->vm_page_prot);
1732 break;
1733 default:
1734 return -EINVAL;
1735 }
1736
Eli Cohenb037c292017-01-03 23:55:26 +02001737 pfn = uar_index2pfn(dev, bfregi, idx);
Guy Levi37aa5c32016-04-27 16:49:50 +03001738 mlx5_ib_dbg(dev, "uar idx 0x%lx, pfn %pa\n", idx, &pfn);
1739
1740 vma->vm_page_prot = prot;
1741 err = io_remap_pfn_range(vma, vma->vm_start, pfn,
1742 PAGE_SIZE, vma->vm_page_prot);
1743 if (err) {
1744 mlx5_ib_err(dev, "io_remap_pfn_range failed with error=%d, vm_start=0x%lx, pfn=%pa, mmap_cmd=%s\n",
1745 err, vma->vm_start, &pfn, mmap_cmd2str(cmd));
1746 return -EAGAIN;
1747 }
1748
1749 pa = pfn << PAGE_SHIFT;
1750 mlx5_ib_dbg(dev, "mapped %s at 0x%lx, PA %pa\n", mmap_cmd2str(cmd),
1751 vma->vm_start, &pa);
1752
Maor Gottlieb7c2344c2016-06-17 14:56:44 +03001753 return mlx5_ib_set_vma_data(vma, context);
Guy Levi37aa5c32016-04-27 16:49:50 +03001754}
1755
Eli Cohene126ba92013-07-07 17:25:49 +03001756static int mlx5_ib_mmap(struct ib_ucontext *ibcontext, struct vm_area_struct *vma)
1757{
1758 struct mlx5_ib_ucontext *context = to_mucontext(ibcontext);
1759 struct mlx5_ib_dev *dev = to_mdev(ibcontext->device);
Eli Cohene126ba92013-07-07 17:25:49 +03001760 unsigned long command;
Eli Cohene126ba92013-07-07 17:25:49 +03001761 phys_addr_t pfn;
1762
1763 command = get_command(vma->vm_pgoff);
1764 switch (command) {
Guy Levi37aa5c32016-04-27 16:49:50 +03001765 case MLX5_IB_MMAP_WC_PAGE:
1766 case MLX5_IB_MMAP_NC_PAGE:
Eli Cohene126ba92013-07-07 17:25:49 +03001767 case MLX5_IB_MMAP_REGULAR_PAGE:
Maor Gottlieb7c2344c2016-06-17 14:56:44 +03001768 return uar_mmap(dev, command, vma, context);
Eli Cohene126ba92013-07-07 17:25:49 +03001769
1770 case MLX5_IB_MMAP_GET_CONTIGUOUS_PAGES:
1771 return -ENOSYS;
1772
Matan Barakd69e3bc2015-12-15 20:30:13 +02001773 case MLX5_IB_MMAP_CORE_CLOCK:
Matan Barakd69e3bc2015-12-15 20:30:13 +02001774 if (vma->vm_end - vma->vm_start != PAGE_SIZE)
1775 return -EINVAL;
1776
Matan Barak6cbac1e2016-04-14 16:52:10 +03001777 if (vma->vm_flags & VM_WRITE)
Matan Barakd69e3bc2015-12-15 20:30:13 +02001778 return -EPERM;
1779
1780 /* Don't expose to user-space information it shouldn't have */
1781 if (PAGE_SIZE > 4096)
1782 return -EOPNOTSUPP;
1783
1784 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
1785 pfn = (dev->mdev->iseg_base +
1786 offsetof(struct mlx5_init_seg, internal_timer_h)) >>
1787 PAGE_SHIFT;
1788 if (io_remap_pfn_range(vma, vma->vm_start, pfn,
1789 PAGE_SIZE, vma->vm_page_prot))
1790 return -EAGAIN;
1791
1792 mlx5_ib_dbg(dev, "mapped internal timer at 0x%lx, PA 0x%llx\n",
1793 vma->vm_start,
1794 (unsigned long long)pfn << PAGE_SHIFT);
1795 break;
Matan Barakd69e3bc2015-12-15 20:30:13 +02001796
Eli Cohene126ba92013-07-07 17:25:49 +03001797 default:
1798 return -EINVAL;
1799 }
1800
1801 return 0;
1802}
1803
Eli Cohene126ba92013-07-07 17:25:49 +03001804static struct ib_pd *mlx5_ib_alloc_pd(struct ib_device *ibdev,
1805 struct ib_ucontext *context,
1806 struct ib_udata *udata)
1807{
1808 struct mlx5_ib_alloc_pd_resp resp;
1809 struct mlx5_ib_pd *pd;
1810 int err;
1811
1812 pd = kmalloc(sizeof(*pd), GFP_KERNEL);
1813 if (!pd)
1814 return ERR_PTR(-ENOMEM);
1815
Jack Morgenstein9603b612014-07-28 23:30:22 +03001816 err = mlx5_core_alloc_pd(to_mdev(ibdev)->mdev, &pd->pdn);
Eli Cohene126ba92013-07-07 17:25:49 +03001817 if (err) {
1818 kfree(pd);
1819 return ERR_PTR(err);
1820 }
1821
1822 if (context) {
1823 resp.pdn = pd->pdn;
1824 if (ib_copy_to_udata(udata, &resp, sizeof(resp))) {
Jack Morgenstein9603b612014-07-28 23:30:22 +03001825 mlx5_core_dealloc_pd(to_mdev(ibdev)->mdev, pd->pdn);
Eli Cohene126ba92013-07-07 17:25:49 +03001826 kfree(pd);
1827 return ERR_PTR(-EFAULT);
1828 }
Eli Cohene126ba92013-07-07 17:25:49 +03001829 }
1830
1831 return &pd->ibpd;
1832}
1833
1834static int mlx5_ib_dealloc_pd(struct ib_pd *pd)
1835{
1836 struct mlx5_ib_dev *mdev = to_mdev(pd->device);
1837 struct mlx5_ib_pd *mpd = to_mpd(pd);
1838
Jack Morgenstein9603b612014-07-28 23:30:22 +03001839 mlx5_core_dealloc_pd(mdev->mdev, mpd->pdn);
Eli Cohene126ba92013-07-07 17:25:49 +03001840 kfree(mpd);
1841
1842 return 0;
1843}
1844
Maor Gottlieb466fa6d2016-08-30 16:58:36 +03001845enum {
1846 MATCH_CRITERIA_ENABLE_OUTER_BIT,
1847 MATCH_CRITERIA_ENABLE_MISC_BIT,
1848 MATCH_CRITERIA_ENABLE_INNER_BIT
1849};
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02001850
Maor Gottlieb466fa6d2016-08-30 16:58:36 +03001851#define HEADER_IS_ZERO(match_criteria, headers) \
1852 !(memchr_inv(MLX5_ADDR_OF(fte_match_param, match_criteria, headers), \
1853 0, MLX5_FLD_SZ_BYTES(fte_match_param, headers))) \
1854
1855static u8 get_match_criteria_enable(u32 *match_criteria)
1856{
1857 u8 match_criteria_enable;
1858
1859 match_criteria_enable =
1860 (!HEADER_IS_ZERO(match_criteria, outer_headers)) <<
1861 MATCH_CRITERIA_ENABLE_OUTER_BIT;
1862 match_criteria_enable |=
1863 (!HEADER_IS_ZERO(match_criteria, misc_parameters)) <<
1864 MATCH_CRITERIA_ENABLE_MISC_BIT;
1865 match_criteria_enable |=
1866 (!HEADER_IS_ZERO(match_criteria, inner_headers)) <<
1867 MATCH_CRITERIA_ENABLE_INNER_BIT;
1868
1869 return match_criteria_enable;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02001870}
1871
Maor Gottliebca0d4752016-08-30 16:58:35 +03001872static void set_proto(void *outer_c, void *outer_v, u8 mask, u8 val)
1873{
1874 MLX5_SET(fte_match_set_lyr_2_4, outer_c, ip_protocol, mask);
1875 MLX5_SET(fte_match_set_lyr_2_4, outer_v, ip_protocol, val);
1876}
1877
Moses Reuben2d1e6972016-11-14 19:04:52 +02001878static void set_flow_label(void *misc_c, void *misc_v, u8 mask, u8 val,
1879 bool inner)
1880{
1881 if (inner) {
1882 MLX5_SET(fte_match_set_misc,
1883 misc_c, inner_ipv6_flow_label, mask);
1884 MLX5_SET(fte_match_set_misc,
1885 misc_v, inner_ipv6_flow_label, val);
1886 } else {
1887 MLX5_SET(fte_match_set_misc,
1888 misc_c, outer_ipv6_flow_label, mask);
1889 MLX5_SET(fte_match_set_misc,
1890 misc_v, outer_ipv6_flow_label, val);
1891 }
1892}
1893
Maor Gottliebca0d4752016-08-30 16:58:35 +03001894static void set_tos(void *outer_c, void *outer_v, u8 mask, u8 val)
1895{
1896 MLX5_SET(fte_match_set_lyr_2_4, outer_c, ip_ecn, mask);
1897 MLX5_SET(fte_match_set_lyr_2_4, outer_v, ip_ecn, val);
1898 MLX5_SET(fte_match_set_lyr_2_4, outer_c, ip_dscp, mask >> 2);
1899 MLX5_SET(fte_match_set_lyr_2_4, outer_v, ip_dscp, val >> 2);
1900}
1901
Maor Gottliebc47ac6a2016-08-30 16:58:31 +03001902#define LAST_ETH_FIELD vlan_tag
1903#define LAST_IB_FIELD sl
Maor Gottliebca0d4752016-08-30 16:58:35 +03001904#define LAST_IPV4_FIELD tos
Maor Gottlieb466fa6d2016-08-30 16:58:36 +03001905#define LAST_IPV6_FIELD traffic_class
Maor Gottliebc47ac6a2016-08-30 16:58:31 +03001906#define LAST_TCP_UDP_FIELD src_port
Moses Reubenffb30d82016-11-14 19:04:50 +02001907#define LAST_TUNNEL_FIELD tunnel_id
Moses Reuben2ac693f2017-01-18 14:59:50 +02001908#define LAST_FLOW_TAG_FIELD tag_id
Slava Shwartsmana22ed862017-04-03 13:13:52 +03001909#define LAST_DROP_FIELD size
Maor Gottliebc47ac6a2016-08-30 16:58:31 +03001910
1911/* Field is the last supported field */
1912#define FIELDS_NOT_SUPPORTED(filter, field)\
1913 memchr_inv((void *)&filter.field +\
1914 sizeof(filter.field), 0,\
1915 sizeof(filter) -\
1916 offsetof(typeof(filter), field) -\
1917 sizeof(filter.field))
1918
Ariel Levkovich19cc7522017-04-03 13:11:03 +03001919#define IPV4_VERSION 4
1920#define IPV6_VERSION 6
1921static int parse_flow_attr(struct mlx5_core_dev *mdev, u32 *match_c,
1922 u32 *match_v, const union ib_flow_spec *ib_spec,
Slava Shwartsmana22ed862017-04-03 13:13:52 +03001923 u32 *tag_id, bool *is_drop)
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02001924{
Maor Gottlieb466fa6d2016-08-30 16:58:36 +03001925 void *misc_params_c = MLX5_ADDR_OF(fte_match_param, match_c,
1926 misc_parameters);
1927 void *misc_params_v = MLX5_ADDR_OF(fte_match_param, match_v,
1928 misc_parameters);
Moses Reuben2d1e6972016-11-14 19:04:52 +02001929 void *headers_c;
1930 void *headers_v;
Ariel Levkovich19cc7522017-04-03 13:11:03 +03001931 int match_ipv;
Maor Gottlieb466fa6d2016-08-30 16:58:36 +03001932
Moses Reuben2d1e6972016-11-14 19:04:52 +02001933 if (ib_spec->type & IB_FLOW_SPEC_INNER) {
1934 headers_c = MLX5_ADDR_OF(fte_match_param, match_c,
1935 inner_headers);
1936 headers_v = MLX5_ADDR_OF(fte_match_param, match_v,
1937 inner_headers);
Ariel Levkovich19cc7522017-04-03 13:11:03 +03001938 match_ipv = MLX5_CAP_FLOWTABLE_NIC_RX(mdev,
1939 ft_field_support.inner_ip_version);
Moses Reuben2d1e6972016-11-14 19:04:52 +02001940 } else {
1941 headers_c = MLX5_ADDR_OF(fte_match_param, match_c,
1942 outer_headers);
1943 headers_v = MLX5_ADDR_OF(fte_match_param, match_v,
1944 outer_headers);
Ariel Levkovich19cc7522017-04-03 13:11:03 +03001945 match_ipv = MLX5_CAP_FLOWTABLE_NIC_RX(mdev,
1946 ft_field_support.outer_ip_version);
Moses Reuben2d1e6972016-11-14 19:04:52 +02001947 }
1948
1949 switch (ib_spec->type & ~IB_FLOW_SPEC_INNER) {
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02001950 case IB_FLOW_SPEC_ETH:
Maor Gottliebc47ac6a2016-08-30 16:58:31 +03001951 if (FIELDS_NOT_SUPPORTED(ib_spec->eth.mask, LAST_ETH_FIELD))
Leon Romanovsky1ffd3a22017-01-18 14:59:51 +02001952 return -EOPNOTSUPP;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02001953
Moses Reuben2d1e6972016-11-14 19:04:52 +02001954 ether_addr_copy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02001955 dmac_47_16),
1956 ib_spec->eth.mask.dst_mac);
Moses Reuben2d1e6972016-11-14 19:04:52 +02001957 ether_addr_copy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02001958 dmac_47_16),
1959 ib_spec->eth.val.dst_mac);
1960
Moses Reuben2d1e6972016-11-14 19:04:52 +02001961 ether_addr_copy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
Maor Gottliebee3da802016-09-12 19:16:24 +03001962 smac_47_16),
1963 ib_spec->eth.mask.src_mac);
Moses Reuben2d1e6972016-11-14 19:04:52 +02001964 ether_addr_copy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
Maor Gottliebee3da802016-09-12 19:16:24 +03001965 smac_47_16),
1966 ib_spec->eth.val.src_mac);
1967
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02001968 if (ib_spec->eth.mask.vlan_tag) {
Moses Reuben2d1e6972016-11-14 19:04:52 +02001969 MLX5_SET(fte_match_set_lyr_2_4, headers_c,
Mohamad Haj Yahia10543362016-10-09 16:25:43 +03001970 cvlan_tag, 1);
Moses Reuben2d1e6972016-11-14 19:04:52 +02001971 MLX5_SET(fte_match_set_lyr_2_4, headers_v,
Mohamad Haj Yahia10543362016-10-09 16:25:43 +03001972 cvlan_tag, 1);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02001973
Moses Reuben2d1e6972016-11-14 19:04:52 +02001974 MLX5_SET(fte_match_set_lyr_2_4, headers_c,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02001975 first_vid, ntohs(ib_spec->eth.mask.vlan_tag));
Moses Reuben2d1e6972016-11-14 19:04:52 +02001976 MLX5_SET(fte_match_set_lyr_2_4, headers_v,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02001977 first_vid, ntohs(ib_spec->eth.val.vlan_tag));
1978
Moses Reuben2d1e6972016-11-14 19:04:52 +02001979 MLX5_SET(fte_match_set_lyr_2_4, headers_c,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02001980 first_cfi,
1981 ntohs(ib_spec->eth.mask.vlan_tag) >> 12);
Moses Reuben2d1e6972016-11-14 19:04:52 +02001982 MLX5_SET(fte_match_set_lyr_2_4, headers_v,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02001983 first_cfi,
1984 ntohs(ib_spec->eth.val.vlan_tag) >> 12);
1985
Moses Reuben2d1e6972016-11-14 19:04:52 +02001986 MLX5_SET(fte_match_set_lyr_2_4, headers_c,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02001987 first_prio,
1988 ntohs(ib_spec->eth.mask.vlan_tag) >> 13);
Moses Reuben2d1e6972016-11-14 19:04:52 +02001989 MLX5_SET(fte_match_set_lyr_2_4, headers_v,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02001990 first_prio,
1991 ntohs(ib_spec->eth.val.vlan_tag) >> 13);
1992 }
Moses Reuben2d1e6972016-11-14 19:04:52 +02001993 MLX5_SET(fte_match_set_lyr_2_4, headers_c,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02001994 ethertype, ntohs(ib_spec->eth.mask.ether_type));
Moses Reuben2d1e6972016-11-14 19:04:52 +02001995 MLX5_SET(fte_match_set_lyr_2_4, headers_v,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02001996 ethertype, ntohs(ib_spec->eth.val.ether_type));
1997 break;
1998 case IB_FLOW_SPEC_IPV4:
Maor Gottliebc47ac6a2016-08-30 16:58:31 +03001999 if (FIELDS_NOT_SUPPORTED(ib_spec->ipv4.mask, LAST_IPV4_FIELD))
Leon Romanovsky1ffd3a22017-01-18 14:59:51 +02002000 return -EOPNOTSUPP;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002001
Ariel Levkovich19cc7522017-04-03 13:11:03 +03002002 if (match_ipv) {
2003 MLX5_SET(fte_match_set_lyr_2_4, headers_c,
2004 ip_version, 0xf);
2005 MLX5_SET(fte_match_set_lyr_2_4, headers_v,
2006 ip_version, IPV4_VERSION);
2007 } else {
2008 MLX5_SET(fte_match_set_lyr_2_4, headers_c,
2009 ethertype, 0xffff);
2010 MLX5_SET(fte_match_set_lyr_2_4, headers_v,
2011 ethertype, ETH_P_IP);
2012 }
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002013
Moses Reuben2d1e6972016-11-14 19:04:52 +02002014 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002015 src_ipv4_src_ipv6.ipv4_layout.ipv4),
2016 &ib_spec->ipv4.mask.src_ip,
2017 sizeof(ib_spec->ipv4.mask.src_ip));
Moses Reuben2d1e6972016-11-14 19:04:52 +02002018 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002019 src_ipv4_src_ipv6.ipv4_layout.ipv4),
2020 &ib_spec->ipv4.val.src_ip,
2021 sizeof(ib_spec->ipv4.val.src_ip));
Moses Reuben2d1e6972016-11-14 19:04:52 +02002022 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002023 dst_ipv4_dst_ipv6.ipv4_layout.ipv4),
2024 &ib_spec->ipv4.mask.dst_ip,
2025 sizeof(ib_spec->ipv4.mask.dst_ip));
Moses Reuben2d1e6972016-11-14 19:04:52 +02002026 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002027 dst_ipv4_dst_ipv6.ipv4_layout.ipv4),
2028 &ib_spec->ipv4.val.dst_ip,
2029 sizeof(ib_spec->ipv4.val.dst_ip));
Maor Gottliebca0d4752016-08-30 16:58:35 +03002030
Moses Reuben2d1e6972016-11-14 19:04:52 +02002031 set_tos(headers_c, headers_v,
Maor Gottliebca0d4752016-08-30 16:58:35 +03002032 ib_spec->ipv4.mask.tos, ib_spec->ipv4.val.tos);
2033
Moses Reuben2d1e6972016-11-14 19:04:52 +02002034 set_proto(headers_c, headers_v,
Maor Gottliebca0d4752016-08-30 16:58:35 +03002035 ib_spec->ipv4.mask.proto, ib_spec->ipv4.val.proto);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002036 break;
Maor Gottlieb026bae02016-06-17 15:14:51 +03002037 case IB_FLOW_SPEC_IPV6:
Maor Gottliebc47ac6a2016-08-30 16:58:31 +03002038 if (FIELDS_NOT_SUPPORTED(ib_spec->ipv6.mask, LAST_IPV6_FIELD))
Leon Romanovsky1ffd3a22017-01-18 14:59:51 +02002039 return -EOPNOTSUPP;
Maor Gottlieb026bae02016-06-17 15:14:51 +03002040
Ariel Levkovich19cc7522017-04-03 13:11:03 +03002041 if (match_ipv) {
2042 MLX5_SET(fte_match_set_lyr_2_4, headers_c,
2043 ip_version, 0xf);
2044 MLX5_SET(fte_match_set_lyr_2_4, headers_v,
2045 ip_version, IPV6_VERSION);
2046 } else {
2047 MLX5_SET(fte_match_set_lyr_2_4, headers_c,
2048 ethertype, 0xffff);
2049 MLX5_SET(fte_match_set_lyr_2_4, headers_v,
2050 ethertype, ETH_P_IPV6);
2051 }
Maor Gottlieb026bae02016-06-17 15:14:51 +03002052
Moses Reuben2d1e6972016-11-14 19:04:52 +02002053 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
Maor Gottlieb026bae02016-06-17 15:14:51 +03002054 src_ipv4_src_ipv6.ipv6_layout.ipv6),
2055 &ib_spec->ipv6.mask.src_ip,
2056 sizeof(ib_spec->ipv6.mask.src_ip));
Moses Reuben2d1e6972016-11-14 19:04:52 +02002057 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
Maor Gottlieb026bae02016-06-17 15:14:51 +03002058 src_ipv4_src_ipv6.ipv6_layout.ipv6),
2059 &ib_spec->ipv6.val.src_ip,
2060 sizeof(ib_spec->ipv6.val.src_ip));
Moses Reuben2d1e6972016-11-14 19:04:52 +02002061 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
Maor Gottlieb026bae02016-06-17 15:14:51 +03002062 dst_ipv4_dst_ipv6.ipv6_layout.ipv6),
2063 &ib_spec->ipv6.mask.dst_ip,
2064 sizeof(ib_spec->ipv6.mask.dst_ip));
Moses Reuben2d1e6972016-11-14 19:04:52 +02002065 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
Maor Gottlieb026bae02016-06-17 15:14:51 +03002066 dst_ipv4_dst_ipv6.ipv6_layout.ipv6),
2067 &ib_spec->ipv6.val.dst_ip,
2068 sizeof(ib_spec->ipv6.val.dst_ip));
Maor Gottlieb466fa6d2016-08-30 16:58:36 +03002069
Moses Reuben2d1e6972016-11-14 19:04:52 +02002070 set_tos(headers_c, headers_v,
Maor Gottlieb466fa6d2016-08-30 16:58:36 +03002071 ib_spec->ipv6.mask.traffic_class,
2072 ib_spec->ipv6.val.traffic_class);
2073
Moses Reuben2d1e6972016-11-14 19:04:52 +02002074 set_proto(headers_c, headers_v,
Maor Gottlieb466fa6d2016-08-30 16:58:36 +03002075 ib_spec->ipv6.mask.next_hdr,
2076 ib_spec->ipv6.val.next_hdr);
2077
Moses Reuben2d1e6972016-11-14 19:04:52 +02002078 set_flow_label(misc_params_c, misc_params_v,
2079 ntohl(ib_spec->ipv6.mask.flow_label),
2080 ntohl(ib_spec->ipv6.val.flow_label),
2081 ib_spec->type & IB_FLOW_SPEC_INNER);
2082
Maor Gottlieb026bae02016-06-17 15:14:51 +03002083 break;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002084 case IB_FLOW_SPEC_TCP:
Maor Gottliebc47ac6a2016-08-30 16:58:31 +03002085 if (FIELDS_NOT_SUPPORTED(ib_spec->tcp_udp.mask,
2086 LAST_TCP_UDP_FIELD))
Leon Romanovsky1ffd3a22017-01-18 14:59:51 +02002087 return -EOPNOTSUPP;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002088
Moses Reuben2d1e6972016-11-14 19:04:52 +02002089 MLX5_SET(fte_match_set_lyr_2_4, headers_c, ip_protocol,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002090 0xff);
Moses Reuben2d1e6972016-11-14 19:04:52 +02002091 MLX5_SET(fte_match_set_lyr_2_4, headers_v, ip_protocol,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002092 IPPROTO_TCP);
2093
Moses Reuben2d1e6972016-11-14 19:04:52 +02002094 MLX5_SET(fte_match_set_lyr_2_4, headers_c, tcp_sport,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002095 ntohs(ib_spec->tcp_udp.mask.src_port));
Moses Reuben2d1e6972016-11-14 19:04:52 +02002096 MLX5_SET(fte_match_set_lyr_2_4, headers_v, tcp_sport,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002097 ntohs(ib_spec->tcp_udp.val.src_port));
2098
Moses Reuben2d1e6972016-11-14 19:04:52 +02002099 MLX5_SET(fte_match_set_lyr_2_4, headers_c, tcp_dport,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002100 ntohs(ib_spec->tcp_udp.mask.dst_port));
Moses Reuben2d1e6972016-11-14 19:04:52 +02002101 MLX5_SET(fte_match_set_lyr_2_4, headers_v, tcp_dport,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002102 ntohs(ib_spec->tcp_udp.val.dst_port));
2103 break;
2104 case IB_FLOW_SPEC_UDP:
Maor Gottliebc47ac6a2016-08-30 16:58:31 +03002105 if (FIELDS_NOT_SUPPORTED(ib_spec->tcp_udp.mask,
2106 LAST_TCP_UDP_FIELD))
Leon Romanovsky1ffd3a22017-01-18 14:59:51 +02002107 return -EOPNOTSUPP;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002108
Moses Reuben2d1e6972016-11-14 19:04:52 +02002109 MLX5_SET(fte_match_set_lyr_2_4, headers_c, ip_protocol,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002110 0xff);
Moses Reuben2d1e6972016-11-14 19:04:52 +02002111 MLX5_SET(fte_match_set_lyr_2_4, headers_v, ip_protocol,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002112 IPPROTO_UDP);
2113
Moses Reuben2d1e6972016-11-14 19:04:52 +02002114 MLX5_SET(fte_match_set_lyr_2_4, headers_c, udp_sport,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002115 ntohs(ib_spec->tcp_udp.mask.src_port));
Moses Reuben2d1e6972016-11-14 19:04:52 +02002116 MLX5_SET(fte_match_set_lyr_2_4, headers_v, udp_sport,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002117 ntohs(ib_spec->tcp_udp.val.src_port));
2118
Moses Reuben2d1e6972016-11-14 19:04:52 +02002119 MLX5_SET(fte_match_set_lyr_2_4, headers_c, udp_dport,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002120 ntohs(ib_spec->tcp_udp.mask.dst_port));
Moses Reuben2d1e6972016-11-14 19:04:52 +02002121 MLX5_SET(fte_match_set_lyr_2_4, headers_v, udp_dport,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002122 ntohs(ib_spec->tcp_udp.val.dst_port));
2123 break;
Moses Reubenffb30d82016-11-14 19:04:50 +02002124 case IB_FLOW_SPEC_VXLAN_TUNNEL:
2125 if (FIELDS_NOT_SUPPORTED(ib_spec->tunnel.mask,
2126 LAST_TUNNEL_FIELD))
Leon Romanovsky1ffd3a22017-01-18 14:59:51 +02002127 return -EOPNOTSUPP;
Moses Reubenffb30d82016-11-14 19:04:50 +02002128
2129 MLX5_SET(fte_match_set_misc, misc_params_c, vxlan_vni,
2130 ntohl(ib_spec->tunnel.mask.tunnel_id));
2131 MLX5_SET(fte_match_set_misc, misc_params_v, vxlan_vni,
2132 ntohl(ib_spec->tunnel.val.tunnel_id));
2133 break;
Moses Reuben2ac693f2017-01-18 14:59:50 +02002134 case IB_FLOW_SPEC_ACTION_TAG:
2135 if (FIELDS_NOT_SUPPORTED(ib_spec->flow_tag,
2136 LAST_FLOW_TAG_FIELD))
2137 return -EOPNOTSUPP;
2138 if (ib_spec->flow_tag.tag_id >= BIT(24))
2139 return -EINVAL;
2140
2141 *tag_id = ib_spec->flow_tag.tag_id;
2142 break;
Slava Shwartsmana22ed862017-04-03 13:13:52 +03002143 case IB_FLOW_SPEC_ACTION_DROP:
2144 if (FIELDS_NOT_SUPPORTED(ib_spec->drop,
2145 LAST_DROP_FIELD))
2146 return -EOPNOTSUPP;
2147 *is_drop = true;
2148 break;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002149 default:
2150 return -EINVAL;
2151 }
2152
2153 return 0;
2154}
2155
2156/* If a flow could catch both multicast and unicast packets,
2157 * it won't fall into the multicast flow steering table and this rule
2158 * could steal other multicast packets.
2159 */
Yishai Hadasa550ddf2017-08-17 15:52:33 +03002160static bool flow_is_multicast_only(const struct ib_flow_attr *ib_attr)
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002161{
Yishai Hadas81e30882017-06-08 16:15:09 +03002162 union ib_flow_spec *flow_spec;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002163
2164 if (ib_attr->type != IB_FLOW_ATTR_NORMAL ||
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002165 ib_attr->num_of_specs < 1)
2166 return false;
2167
Yishai Hadas81e30882017-06-08 16:15:09 +03002168 flow_spec = (union ib_flow_spec *)(ib_attr + 1);
2169 if (flow_spec->type == IB_FLOW_SPEC_IPV4) {
2170 struct ib_flow_spec_ipv4 *ipv4_spec;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002171
Yishai Hadas81e30882017-06-08 16:15:09 +03002172 ipv4_spec = (struct ib_flow_spec_ipv4 *)flow_spec;
2173 if (ipv4_is_multicast(ipv4_spec->val.dst_ip))
2174 return true;
2175
2176 return false;
2177 }
2178
2179 if (flow_spec->type == IB_FLOW_SPEC_ETH) {
2180 struct ib_flow_spec_eth *eth_spec;
2181
2182 eth_spec = (struct ib_flow_spec_eth *)flow_spec;
2183 return is_multicast_ether_addr(eth_spec->mask.dst_mac) &&
2184 is_multicast_ether_addr(eth_spec->val.dst_mac);
2185 }
2186
2187 return false;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002188}
2189
Ariel Levkovich19cc7522017-04-03 13:11:03 +03002190static bool is_valid_ethertype(struct mlx5_core_dev *mdev,
2191 const struct ib_flow_attr *flow_attr,
Ariel Levkovich0f750962017-04-03 13:11:02 +03002192 bool check_inner)
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002193{
2194 union ib_flow_spec *ib_spec = (union ib_flow_spec *)(flow_attr + 1);
Ariel Levkovich19cc7522017-04-03 13:11:03 +03002195 int match_ipv = check_inner ?
2196 MLX5_CAP_FLOWTABLE_NIC_RX(mdev,
2197 ft_field_support.inner_ip_version) :
2198 MLX5_CAP_FLOWTABLE_NIC_RX(mdev,
2199 ft_field_support.outer_ip_version);
Ariel Levkovich0f750962017-04-03 13:11:02 +03002200 int inner_bit = check_inner ? IB_FLOW_SPEC_INNER : 0;
2201 bool ipv4_spec_valid, ipv6_spec_valid;
2202 unsigned int ip_spec_type = 0;
2203 bool has_ethertype = false;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002204 unsigned int spec_index;
Ariel Levkovich0f750962017-04-03 13:11:02 +03002205 bool mask_valid = true;
2206 u16 eth_type = 0;
2207 bool type_valid;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002208
2209 /* Validate that ethertype is correct */
2210 for (spec_index = 0; spec_index < flow_attr->num_of_specs; spec_index++) {
Ariel Levkovich0f750962017-04-03 13:11:02 +03002211 if ((ib_spec->type == (IB_FLOW_SPEC_ETH | inner_bit)) &&
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002212 ib_spec->eth.mask.ether_type) {
Ariel Levkovich0f750962017-04-03 13:11:02 +03002213 mask_valid = (ib_spec->eth.mask.ether_type ==
2214 htons(0xffff));
2215 has_ethertype = true;
2216 eth_type = ntohs(ib_spec->eth.val.ether_type);
2217 } else if ((ib_spec->type == (IB_FLOW_SPEC_IPV4 | inner_bit)) ||
2218 (ib_spec->type == (IB_FLOW_SPEC_IPV6 | inner_bit))) {
2219 ip_spec_type = ib_spec->type;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002220 }
2221 ib_spec = (void *)ib_spec + ib_spec->size;
2222 }
Ariel Levkovich0f750962017-04-03 13:11:02 +03002223
2224 type_valid = (!has_ethertype) || (!ip_spec_type);
2225 if (!type_valid && mask_valid) {
2226 ipv4_spec_valid = (eth_type == ETH_P_IP) &&
2227 (ip_spec_type == (IB_FLOW_SPEC_IPV4 | inner_bit));
2228 ipv6_spec_valid = (eth_type == ETH_P_IPV6) &&
2229 (ip_spec_type == (IB_FLOW_SPEC_IPV6 | inner_bit));
Ariel Levkovich19cc7522017-04-03 13:11:03 +03002230
2231 type_valid = (ipv4_spec_valid) || (ipv6_spec_valid) ||
2232 (((eth_type == ETH_P_MPLS_UC) ||
2233 (eth_type == ETH_P_MPLS_MC)) && match_ipv);
Ariel Levkovich0f750962017-04-03 13:11:02 +03002234 }
2235
2236 return type_valid;
2237}
2238
Ariel Levkovich19cc7522017-04-03 13:11:03 +03002239static bool is_valid_attr(struct mlx5_core_dev *mdev,
2240 const struct ib_flow_attr *flow_attr)
Ariel Levkovich0f750962017-04-03 13:11:02 +03002241{
Ariel Levkovich19cc7522017-04-03 13:11:03 +03002242 return is_valid_ethertype(mdev, flow_attr, false) &&
2243 is_valid_ethertype(mdev, flow_attr, true);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002244}
2245
2246static void put_flow_table(struct mlx5_ib_dev *dev,
2247 struct mlx5_ib_flow_prio *prio, bool ft_added)
2248{
2249 prio->refcount -= !!ft_added;
2250 if (!prio->refcount) {
2251 mlx5_destroy_flow_table(prio->flow_table);
2252 prio->flow_table = NULL;
2253 }
2254}
2255
2256static int mlx5_ib_destroy_flow(struct ib_flow *flow_id)
2257{
2258 struct mlx5_ib_dev *dev = to_mdev(flow_id->qp->device);
2259 struct mlx5_ib_flow_handler *handler = container_of(flow_id,
2260 struct mlx5_ib_flow_handler,
2261 ibflow);
2262 struct mlx5_ib_flow_handler *iter, *tmp;
2263
2264 mutex_lock(&dev->flow_db.lock);
2265
2266 list_for_each_entry_safe(iter, tmp, &handler->list, list) {
Mark Bloch74491de2016-08-31 11:24:25 +00002267 mlx5_del_flow_rules(iter->rule);
Maor Gottliebcc0e5d42016-08-28 14:16:34 +03002268 put_flow_table(dev, iter->prio, true);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002269 list_del(&iter->list);
2270 kfree(iter);
2271 }
2272
Mark Bloch74491de2016-08-31 11:24:25 +00002273 mlx5_del_flow_rules(handler->rule);
Maor Gottlieb5497adc2016-08-28 14:16:31 +03002274 put_flow_table(dev, handler->prio, true);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002275 mutex_unlock(&dev->flow_db.lock);
2276
2277 kfree(handler);
2278
2279 return 0;
2280}
2281
Maor Gottlieb35d190112016-03-07 18:51:47 +02002282static int ib_prio_to_core_prio(unsigned int priority, bool dont_trap)
2283{
2284 priority *= 2;
2285 if (!dont_trap)
2286 priority++;
2287 return priority;
2288}
2289
Maor Gottliebcc0e5d42016-08-28 14:16:34 +03002290enum flow_table_type {
2291 MLX5_IB_FT_RX,
2292 MLX5_IB_FT_TX
2293};
2294
Maor Gottlieb00b7c2a2017-03-29 06:09:01 +03002295#define MLX5_FS_MAX_TYPES 6
2296#define MLX5_FS_MAX_ENTRIES BIT(16)
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002297static struct mlx5_ib_flow_prio *get_flow_table(struct mlx5_ib_dev *dev,
Maor Gottliebcc0e5d42016-08-28 14:16:34 +03002298 struct ib_flow_attr *flow_attr,
2299 enum flow_table_type ft_type)
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002300{
Maor Gottlieb35d190112016-03-07 18:51:47 +02002301 bool dont_trap = flow_attr->flags & IB_FLOW_ATTR_FLAGS_DONT_TRAP;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002302 struct mlx5_flow_namespace *ns = NULL;
2303 struct mlx5_ib_flow_prio *prio;
2304 struct mlx5_flow_table *ft;
Maor Gottliebdac388e2017-03-29 06:09:00 +03002305 int max_table_size;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002306 int num_entries;
2307 int num_groups;
2308 int priority;
2309 int err = 0;
2310
Maor Gottliebdac388e2017-03-29 06:09:00 +03002311 max_table_size = BIT(MLX5_CAP_FLOWTABLE_NIC_RX(dev->mdev,
2312 log_max_ft_size));
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002313 if (flow_attr->type == IB_FLOW_ATTR_NORMAL) {
Maor Gottlieb35d190112016-03-07 18:51:47 +02002314 if (flow_is_multicast_only(flow_attr) &&
2315 !dont_trap)
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002316 priority = MLX5_IB_FLOW_MCAST_PRIO;
2317 else
Maor Gottlieb35d190112016-03-07 18:51:47 +02002318 priority = ib_prio_to_core_prio(flow_attr->priority,
2319 dont_trap);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002320 ns = mlx5_get_flow_namespace(dev->mdev,
2321 MLX5_FLOW_NAMESPACE_BYPASS);
2322 num_entries = MLX5_FS_MAX_ENTRIES;
2323 num_groups = MLX5_FS_MAX_TYPES;
2324 prio = &dev->flow_db.prios[priority];
2325 } else if (flow_attr->type == IB_FLOW_ATTR_ALL_DEFAULT ||
2326 flow_attr->type == IB_FLOW_ATTR_MC_DEFAULT) {
2327 ns = mlx5_get_flow_namespace(dev->mdev,
2328 MLX5_FLOW_NAMESPACE_LEFTOVERS);
2329 build_leftovers_ft_param(&priority,
2330 &num_entries,
2331 &num_groups);
2332 prio = &dev->flow_db.prios[MLX5_IB_FLOW_LEFTOVERS_PRIO];
Maor Gottliebcc0e5d42016-08-28 14:16:34 +03002333 } else if (flow_attr->type == IB_FLOW_ATTR_SNIFFER) {
2334 if (!MLX5_CAP_FLOWTABLE(dev->mdev,
2335 allow_sniffer_and_nic_rx_shared_tir))
2336 return ERR_PTR(-ENOTSUPP);
2337
2338 ns = mlx5_get_flow_namespace(dev->mdev, ft_type == MLX5_IB_FT_RX ?
2339 MLX5_FLOW_NAMESPACE_SNIFFER_RX :
2340 MLX5_FLOW_NAMESPACE_SNIFFER_TX);
2341
2342 prio = &dev->flow_db.sniffer[ft_type];
2343 priority = 0;
2344 num_entries = 1;
2345 num_groups = 1;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002346 }
2347
2348 if (!ns)
2349 return ERR_PTR(-ENOTSUPP);
2350
Maor Gottliebdac388e2017-03-29 06:09:00 +03002351 if (num_entries > max_table_size)
2352 return ERR_PTR(-ENOMEM);
2353
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002354 ft = prio->flow_table;
2355 if (!ft) {
2356 ft = mlx5_create_auto_grouped_flow_table(ns, priority,
2357 num_entries,
Maor Gottliebd63cd282016-04-29 01:36:35 +03002358 num_groups,
Hadar Hen Zionc9f1b072016-11-07 15:14:44 +02002359 0, 0);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002360
2361 if (!IS_ERR(ft)) {
2362 prio->refcount = 0;
2363 prio->flow_table = ft;
2364 } else {
2365 err = PTR_ERR(ft);
2366 }
2367 }
2368
2369 return err ? ERR_PTR(err) : prio;
2370}
2371
Yishai Hadasa550ddf2017-08-17 15:52:33 +03002372static void set_underlay_qp(struct mlx5_ib_dev *dev,
2373 struct mlx5_flow_spec *spec,
2374 u32 underlay_qpn)
2375{
2376 void *misc_params_c = MLX5_ADDR_OF(fte_match_param,
2377 spec->match_criteria,
2378 misc_parameters);
2379 void *misc_params_v = MLX5_ADDR_OF(fte_match_param, spec->match_value,
2380 misc_parameters);
2381
2382 if (underlay_qpn &&
2383 MLX5_CAP_FLOWTABLE_NIC_RX(dev->mdev,
2384 ft_field_support.bth_dst_qp)) {
2385 MLX5_SET(fte_match_set_misc,
2386 misc_params_v, bth_dst_qp, underlay_qpn);
2387 MLX5_SET(fte_match_set_misc,
2388 misc_params_c, bth_dst_qp, 0xffffff);
2389 }
2390}
2391
2392static struct mlx5_ib_flow_handler *_create_flow_rule(struct mlx5_ib_dev *dev,
2393 struct mlx5_ib_flow_prio *ft_prio,
2394 const struct ib_flow_attr *flow_attr,
2395 struct mlx5_flow_destination *dst,
2396 u32 underlay_qpn)
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002397{
2398 struct mlx5_flow_table *ft = ft_prio->flow_table;
2399 struct mlx5_ib_flow_handler *handler;
Hadar Hen Zion66958ed2016-11-07 15:14:45 +02002400 struct mlx5_flow_act flow_act = {0};
Maor Gottliebc5bb1732016-07-04 17:23:05 +03002401 struct mlx5_flow_spec *spec;
Slava Shwartsmana22ed862017-04-03 13:13:52 +03002402 struct mlx5_flow_destination *rule_dst = dst;
Maor Gottliebdd063d02016-08-28 14:16:32 +03002403 const void *ib_flow = (const void *)flow_attr + sizeof(*flow_attr);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002404 unsigned int spec_index;
Moses Reuben2ac693f2017-01-18 14:59:50 +02002405 u32 flow_tag = MLX5_FS_DEFAULT_FLOW_TAG;
Slava Shwartsmana22ed862017-04-03 13:13:52 +03002406 bool is_drop = false;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002407 int err = 0;
Slava Shwartsmana22ed862017-04-03 13:13:52 +03002408 int dest_num = 1;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002409
Ariel Levkovich19cc7522017-04-03 13:11:03 +03002410 if (!is_valid_attr(dev->mdev, flow_attr))
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002411 return ERR_PTR(-EINVAL);
2412
Leon Romanovsky1b9a07e2017-05-10 21:32:18 +03002413 spec = kvzalloc(sizeof(*spec), GFP_KERNEL);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002414 handler = kzalloc(sizeof(*handler), GFP_KERNEL);
Maor Gottliebc5bb1732016-07-04 17:23:05 +03002415 if (!handler || !spec) {
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002416 err = -ENOMEM;
2417 goto free;
2418 }
2419
2420 INIT_LIST_HEAD(&handler->list);
2421
2422 for (spec_index = 0; spec_index < flow_attr->num_of_specs; spec_index++) {
Ariel Levkovich19cc7522017-04-03 13:11:03 +03002423 err = parse_flow_attr(dev->mdev, spec->match_criteria,
Slava Shwartsmana22ed862017-04-03 13:13:52 +03002424 spec->match_value,
2425 ib_flow, &flow_tag, &is_drop);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002426 if (err < 0)
2427 goto free;
2428
2429 ib_flow += ((union ib_flow_spec *)ib_flow)->size;
2430 }
2431
Yishai Hadasa550ddf2017-08-17 15:52:33 +03002432 if (!flow_is_multicast_only(flow_attr))
2433 set_underlay_qp(dev, spec, underlay_qpn);
2434
Maor Gottlieb466fa6d2016-08-30 16:58:36 +03002435 spec->match_criteria_enable = get_match_criteria_enable(spec->match_criteria);
Slava Shwartsmana22ed862017-04-03 13:13:52 +03002436 if (is_drop) {
2437 flow_act.action = MLX5_FLOW_CONTEXT_ACTION_DROP;
2438 rule_dst = NULL;
2439 dest_num = 0;
2440 } else {
2441 flow_act.action = dst ? MLX5_FLOW_CONTEXT_ACTION_FWD_DEST :
2442 MLX5_FLOW_CONTEXT_ACTION_FWD_NEXT_PRIO;
2443 }
Moses Reuben2ac693f2017-01-18 14:59:50 +02002444
2445 if (flow_tag != MLX5_FS_DEFAULT_FLOW_TAG &&
2446 (flow_attr->type == IB_FLOW_ATTR_ALL_DEFAULT ||
2447 flow_attr->type == IB_FLOW_ATTR_MC_DEFAULT)) {
2448 mlx5_ib_warn(dev, "Flow tag %u and attribute type %x isn't allowed in leftovers\n",
2449 flow_tag, flow_attr->type);
2450 err = -EINVAL;
2451 goto free;
2452 }
2453 flow_act.flow_tag = flow_tag;
Mark Bloch74491de2016-08-31 11:24:25 +00002454 handler->rule = mlx5_add_flow_rules(ft, spec,
Hadar Hen Zion66958ed2016-11-07 15:14:45 +02002455 &flow_act,
Slava Shwartsmana22ed862017-04-03 13:13:52 +03002456 rule_dst, dest_num);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002457
2458 if (IS_ERR(handler->rule)) {
2459 err = PTR_ERR(handler->rule);
2460 goto free;
2461 }
2462
Maor Gottliebd9d49802016-08-28 14:16:33 +03002463 ft_prio->refcount++;
Maor Gottlieb5497adc2016-08-28 14:16:31 +03002464 handler->prio = ft_prio;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002465
2466 ft_prio->flow_table = ft;
2467free:
2468 if (err)
2469 kfree(handler);
Maor Gottliebc5bb1732016-07-04 17:23:05 +03002470 kvfree(spec);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002471 return err ? ERR_PTR(err) : handler;
2472}
2473
Yishai Hadasa550ddf2017-08-17 15:52:33 +03002474static struct mlx5_ib_flow_handler *create_flow_rule(struct mlx5_ib_dev *dev,
2475 struct mlx5_ib_flow_prio *ft_prio,
2476 const struct ib_flow_attr *flow_attr,
2477 struct mlx5_flow_destination *dst)
2478{
2479 return _create_flow_rule(dev, ft_prio, flow_attr, dst, 0);
2480}
2481
Maor Gottlieb35d190112016-03-07 18:51:47 +02002482static struct mlx5_ib_flow_handler *create_dont_trap_rule(struct mlx5_ib_dev *dev,
2483 struct mlx5_ib_flow_prio *ft_prio,
2484 struct ib_flow_attr *flow_attr,
2485 struct mlx5_flow_destination *dst)
2486{
2487 struct mlx5_ib_flow_handler *handler_dst = NULL;
2488 struct mlx5_ib_flow_handler *handler = NULL;
2489
2490 handler = create_flow_rule(dev, ft_prio, flow_attr, NULL);
2491 if (!IS_ERR(handler)) {
2492 handler_dst = create_flow_rule(dev, ft_prio,
2493 flow_attr, dst);
2494 if (IS_ERR(handler_dst)) {
Mark Bloch74491de2016-08-31 11:24:25 +00002495 mlx5_del_flow_rules(handler->rule);
Maor Gottliebd9d49802016-08-28 14:16:33 +03002496 ft_prio->refcount--;
Maor Gottlieb35d190112016-03-07 18:51:47 +02002497 kfree(handler);
2498 handler = handler_dst;
2499 } else {
2500 list_add(&handler_dst->list, &handler->list);
2501 }
2502 }
2503
2504 return handler;
2505}
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002506enum {
2507 LEFTOVERS_MC,
2508 LEFTOVERS_UC,
2509};
2510
2511static struct mlx5_ib_flow_handler *create_leftovers_rule(struct mlx5_ib_dev *dev,
2512 struct mlx5_ib_flow_prio *ft_prio,
2513 struct ib_flow_attr *flow_attr,
2514 struct mlx5_flow_destination *dst)
2515{
2516 struct mlx5_ib_flow_handler *handler_ucast = NULL;
2517 struct mlx5_ib_flow_handler *handler = NULL;
2518
2519 static struct {
2520 struct ib_flow_attr flow_attr;
2521 struct ib_flow_spec_eth eth_flow;
2522 } leftovers_specs[] = {
2523 [LEFTOVERS_MC] = {
2524 .flow_attr = {
2525 .num_of_specs = 1,
2526 .size = sizeof(leftovers_specs[0])
2527 },
2528 .eth_flow = {
2529 .type = IB_FLOW_SPEC_ETH,
2530 .size = sizeof(struct ib_flow_spec_eth),
2531 .mask = {.dst_mac = {0x1} },
2532 .val = {.dst_mac = {0x1} }
2533 }
2534 },
2535 [LEFTOVERS_UC] = {
2536 .flow_attr = {
2537 .num_of_specs = 1,
2538 .size = sizeof(leftovers_specs[0])
2539 },
2540 .eth_flow = {
2541 .type = IB_FLOW_SPEC_ETH,
2542 .size = sizeof(struct ib_flow_spec_eth),
2543 .mask = {.dst_mac = {0x1} },
2544 .val = {.dst_mac = {} }
2545 }
2546 }
2547 };
2548
2549 handler = create_flow_rule(dev, ft_prio,
2550 &leftovers_specs[LEFTOVERS_MC].flow_attr,
2551 dst);
2552 if (!IS_ERR(handler) &&
2553 flow_attr->type == IB_FLOW_ATTR_ALL_DEFAULT) {
2554 handler_ucast = create_flow_rule(dev, ft_prio,
2555 &leftovers_specs[LEFTOVERS_UC].flow_attr,
2556 dst);
2557 if (IS_ERR(handler_ucast)) {
Mark Bloch74491de2016-08-31 11:24:25 +00002558 mlx5_del_flow_rules(handler->rule);
Maor Gottliebd9d49802016-08-28 14:16:33 +03002559 ft_prio->refcount--;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002560 kfree(handler);
2561 handler = handler_ucast;
2562 } else {
2563 list_add(&handler_ucast->list, &handler->list);
2564 }
2565 }
2566
2567 return handler;
2568}
2569
Maor Gottliebcc0e5d42016-08-28 14:16:34 +03002570static struct mlx5_ib_flow_handler *create_sniffer_rule(struct mlx5_ib_dev *dev,
2571 struct mlx5_ib_flow_prio *ft_rx,
2572 struct mlx5_ib_flow_prio *ft_tx,
2573 struct mlx5_flow_destination *dst)
2574{
2575 struct mlx5_ib_flow_handler *handler_rx;
2576 struct mlx5_ib_flow_handler *handler_tx;
2577 int err;
2578 static const struct ib_flow_attr flow_attr = {
2579 .num_of_specs = 0,
2580 .size = sizeof(flow_attr)
2581 };
2582
2583 handler_rx = create_flow_rule(dev, ft_rx, &flow_attr, dst);
2584 if (IS_ERR(handler_rx)) {
2585 err = PTR_ERR(handler_rx);
2586 goto err;
2587 }
2588
2589 handler_tx = create_flow_rule(dev, ft_tx, &flow_attr, dst);
2590 if (IS_ERR(handler_tx)) {
2591 err = PTR_ERR(handler_tx);
2592 goto err_tx;
2593 }
2594
2595 list_add(&handler_tx->list, &handler_rx->list);
2596
2597 return handler_rx;
2598
2599err_tx:
Mark Bloch74491de2016-08-31 11:24:25 +00002600 mlx5_del_flow_rules(handler_rx->rule);
Maor Gottliebcc0e5d42016-08-28 14:16:34 +03002601 ft_rx->refcount--;
2602 kfree(handler_rx);
2603err:
2604 return ERR_PTR(err);
2605}
2606
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002607static struct ib_flow *mlx5_ib_create_flow(struct ib_qp *qp,
2608 struct ib_flow_attr *flow_attr,
2609 int domain)
2610{
2611 struct mlx5_ib_dev *dev = to_mdev(qp->device);
Yishai Hadasd9f88e52016-08-28 10:58:37 +03002612 struct mlx5_ib_qp *mqp = to_mqp(qp);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002613 struct mlx5_ib_flow_handler *handler = NULL;
2614 struct mlx5_flow_destination *dst = NULL;
Maor Gottliebcc0e5d42016-08-28 14:16:34 +03002615 struct mlx5_ib_flow_prio *ft_prio_tx = NULL;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002616 struct mlx5_ib_flow_prio *ft_prio;
2617 int err;
Yishai Hadasa550ddf2017-08-17 15:52:33 +03002618 int underlay_qpn;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002619
2620 if (flow_attr->priority > MLX5_IB_FLOW_LAST_PRIO)
Maor Gottliebdac388e2017-03-29 06:09:00 +03002621 return ERR_PTR(-ENOMEM);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002622
2623 if (domain != IB_FLOW_DOMAIN_USER ||
2624 flow_attr->port > MLX5_CAP_GEN(dev->mdev, num_ports) ||
Maor Gottlieb35d190112016-03-07 18:51:47 +02002625 (flow_attr->flags & ~IB_FLOW_ATTR_FLAGS_DONT_TRAP))
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002626 return ERR_PTR(-EINVAL);
2627
2628 dst = kzalloc(sizeof(*dst), GFP_KERNEL);
2629 if (!dst)
2630 return ERR_PTR(-ENOMEM);
2631
2632 mutex_lock(&dev->flow_db.lock);
2633
Maor Gottliebcc0e5d42016-08-28 14:16:34 +03002634 ft_prio = get_flow_table(dev, flow_attr, MLX5_IB_FT_RX);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002635 if (IS_ERR(ft_prio)) {
2636 err = PTR_ERR(ft_prio);
2637 goto unlock;
2638 }
Maor Gottliebcc0e5d42016-08-28 14:16:34 +03002639 if (flow_attr->type == IB_FLOW_ATTR_SNIFFER) {
2640 ft_prio_tx = get_flow_table(dev, flow_attr, MLX5_IB_FT_TX);
2641 if (IS_ERR(ft_prio_tx)) {
2642 err = PTR_ERR(ft_prio_tx);
2643 ft_prio_tx = NULL;
2644 goto destroy_ft;
2645 }
2646 }
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002647
2648 dst->type = MLX5_FLOW_DESTINATION_TYPE_TIR;
Yishai Hadasd9f88e52016-08-28 10:58:37 +03002649 if (mqp->flags & MLX5_IB_QP_RSS)
2650 dst->tir_num = mqp->rss_qp.tirn;
2651 else
2652 dst->tir_num = mqp->raw_packet_qp.rq.tirn;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002653
2654 if (flow_attr->type == IB_FLOW_ATTR_NORMAL) {
Maor Gottlieb35d190112016-03-07 18:51:47 +02002655 if (flow_attr->flags & IB_FLOW_ATTR_FLAGS_DONT_TRAP) {
2656 handler = create_dont_trap_rule(dev, ft_prio,
2657 flow_attr, dst);
2658 } else {
Yishai Hadasa550ddf2017-08-17 15:52:33 +03002659 underlay_qpn = (mqp->flags & MLX5_IB_QP_UNDERLAY) ?
2660 mqp->underlay_qpn : 0;
2661 handler = _create_flow_rule(dev, ft_prio, flow_attr,
2662 dst, underlay_qpn);
Maor Gottlieb35d190112016-03-07 18:51:47 +02002663 }
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002664 } else if (flow_attr->type == IB_FLOW_ATTR_ALL_DEFAULT ||
2665 flow_attr->type == IB_FLOW_ATTR_MC_DEFAULT) {
2666 handler = create_leftovers_rule(dev, ft_prio, flow_attr,
2667 dst);
Maor Gottliebcc0e5d42016-08-28 14:16:34 +03002668 } else if (flow_attr->type == IB_FLOW_ATTR_SNIFFER) {
2669 handler = create_sniffer_rule(dev, ft_prio, ft_prio_tx, dst);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002670 } else {
2671 err = -EINVAL;
2672 goto destroy_ft;
2673 }
2674
2675 if (IS_ERR(handler)) {
2676 err = PTR_ERR(handler);
2677 handler = NULL;
2678 goto destroy_ft;
2679 }
2680
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002681 mutex_unlock(&dev->flow_db.lock);
2682 kfree(dst);
2683
2684 return &handler->ibflow;
2685
2686destroy_ft:
2687 put_flow_table(dev, ft_prio, false);
Maor Gottliebcc0e5d42016-08-28 14:16:34 +03002688 if (ft_prio_tx)
2689 put_flow_table(dev, ft_prio_tx, false);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002690unlock:
2691 mutex_unlock(&dev->flow_db.lock);
2692 kfree(dst);
2693 kfree(handler);
2694 return ERR_PTR(err);
2695}
2696
Eli Cohene126ba92013-07-07 17:25:49 +03002697static int mlx5_ib_mcg_attach(struct ib_qp *ibqp, union ib_gid *gid, u16 lid)
2698{
2699 struct mlx5_ib_dev *dev = to_mdev(ibqp->device);
Yishai Hadas81e30882017-06-08 16:15:09 +03002700 struct mlx5_ib_qp *mqp = to_mqp(ibqp);
Eli Cohene126ba92013-07-07 17:25:49 +03002701 int err;
2702
Yishai Hadas81e30882017-06-08 16:15:09 +03002703 if (mqp->flags & MLX5_IB_QP_UNDERLAY) {
2704 mlx5_ib_dbg(dev, "Attaching a multi cast group to underlay QP is not supported\n");
2705 return -EOPNOTSUPP;
2706 }
2707
Jack Morgenstein9603b612014-07-28 23:30:22 +03002708 err = mlx5_core_attach_mcg(dev->mdev, gid, ibqp->qp_num);
Eli Cohene126ba92013-07-07 17:25:49 +03002709 if (err)
2710 mlx5_ib_warn(dev, "failed attaching QPN 0x%x, MGID %pI6\n",
2711 ibqp->qp_num, gid->raw);
2712
2713 return err;
2714}
2715
2716static int mlx5_ib_mcg_detach(struct ib_qp *ibqp, union ib_gid *gid, u16 lid)
2717{
2718 struct mlx5_ib_dev *dev = to_mdev(ibqp->device);
2719 int err;
2720
Jack Morgenstein9603b612014-07-28 23:30:22 +03002721 err = mlx5_core_detach_mcg(dev->mdev, gid, ibqp->qp_num);
Eli Cohene126ba92013-07-07 17:25:49 +03002722 if (err)
2723 mlx5_ib_warn(dev, "failed detaching QPN 0x%x, MGID %pI6\n",
2724 ibqp->qp_num, gid->raw);
2725
2726 return err;
2727}
2728
2729static int init_node_data(struct mlx5_ib_dev *dev)
2730{
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03002731 int err;
Eli Cohene126ba92013-07-07 17:25:49 +03002732
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03002733 err = mlx5_query_node_desc(dev, dev->ib_dev.node_desc);
Eli Cohene126ba92013-07-07 17:25:49 +03002734 if (err)
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03002735 return err;
Eli Cohene126ba92013-07-07 17:25:49 +03002736
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03002737 dev->mdev->rev_id = dev->mdev->pdev->revision;
Eli Cohene126ba92013-07-07 17:25:49 +03002738
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03002739 return mlx5_query_node_guid(dev, &dev->ib_dev.node_guid);
Eli Cohene126ba92013-07-07 17:25:49 +03002740}
2741
2742static ssize_t show_fw_pages(struct device *device, struct device_attribute *attr,
2743 char *buf)
2744{
2745 struct mlx5_ib_dev *dev =
2746 container_of(device, struct mlx5_ib_dev, ib_dev.dev);
2747
Jack Morgenstein9603b612014-07-28 23:30:22 +03002748 return sprintf(buf, "%d\n", dev->mdev->priv.fw_pages);
Eli Cohene126ba92013-07-07 17:25:49 +03002749}
2750
2751static ssize_t show_reg_pages(struct device *device,
2752 struct device_attribute *attr, char *buf)
2753{
2754 struct mlx5_ib_dev *dev =
2755 container_of(device, struct mlx5_ib_dev, ib_dev.dev);
2756
Haggai Eran6aec21f2014-12-11 17:04:23 +02002757 return sprintf(buf, "%d\n", atomic_read(&dev->mdev->priv.reg_pages));
Eli Cohene126ba92013-07-07 17:25:49 +03002758}
2759
2760static ssize_t show_hca(struct device *device, struct device_attribute *attr,
2761 char *buf)
2762{
2763 struct mlx5_ib_dev *dev =
2764 container_of(device, struct mlx5_ib_dev, ib_dev.dev);
Jack Morgenstein9603b612014-07-28 23:30:22 +03002765 return sprintf(buf, "MT%d\n", dev->mdev->pdev->device);
Eli Cohene126ba92013-07-07 17:25:49 +03002766}
2767
Eli Cohene126ba92013-07-07 17:25:49 +03002768static ssize_t show_rev(struct device *device, struct device_attribute *attr,
2769 char *buf)
2770{
2771 struct mlx5_ib_dev *dev =
2772 container_of(device, struct mlx5_ib_dev, ib_dev.dev);
Jack Morgenstein9603b612014-07-28 23:30:22 +03002773 return sprintf(buf, "%x\n", dev->mdev->rev_id);
Eli Cohene126ba92013-07-07 17:25:49 +03002774}
2775
2776static ssize_t show_board(struct device *device, struct device_attribute *attr,
2777 char *buf)
2778{
2779 struct mlx5_ib_dev *dev =
2780 container_of(device, struct mlx5_ib_dev, ib_dev.dev);
2781 return sprintf(buf, "%.*s\n", MLX5_BOARD_ID_LEN,
Jack Morgenstein9603b612014-07-28 23:30:22 +03002782 dev->mdev->board_id);
Eli Cohene126ba92013-07-07 17:25:49 +03002783}
2784
2785static DEVICE_ATTR(hw_rev, S_IRUGO, show_rev, NULL);
Eli Cohene126ba92013-07-07 17:25:49 +03002786static DEVICE_ATTR(hca_type, S_IRUGO, show_hca, NULL);
2787static DEVICE_ATTR(board_id, S_IRUGO, show_board, NULL);
2788static DEVICE_ATTR(fw_pages, S_IRUGO, show_fw_pages, NULL);
2789static DEVICE_ATTR(reg_pages, S_IRUGO, show_reg_pages, NULL);
2790
2791static struct device_attribute *mlx5_class_attributes[] = {
2792 &dev_attr_hw_rev,
Eli Cohene126ba92013-07-07 17:25:49 +03002793 &dev_attr_hca_type,
2794 &dev_attr_board_id,
2795 &dev_attr_fw_pages,
2796 &dev_attr_reg_pages,
2797};
2798
Haggai Eran7722f472016-02-29 15:45:07 +02002799static void pkey_change_handler(struct work_struct *work)
2800{
2801 struct mlx5_ib_port_resources *ports =
2802 container_of(work, struct mlx5_ib_port_resources,
2803 pkey_change_work);
2804
2805 mutex_lock(&ports->devr->mutex);
2806 mlx5_ib_gsi_pkey_change(ports->gsi);
2807 mutex_unlock(&ports->devr->mutex);
2808}
2809
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03002810static void mlx5_ib_handle_internal_error(struct mlx5_ib_dev *ibdev)
2811{
2812 struct mlx5_ib_qp *mqp;
2813 struct mlx5_ib_cq *send_mcq, *recv_mcq;
2814 struct mlx5_core_cq *mcq;
2815 struct list_head cq_armed_list;
2816 unsigned long flags_qp;
2817 unsigned long flags_cq;
2818 unsigned long flags;
2819
2820 INIT_LIST_HEAD(&cq_armed_list);
2821
2822 /* Go over qp list reside on that ibdev, sync with create/destroy qp.*/
2823 spin_lock_irqsave(&ibdev->reset_flow_resource_lock, flags);
2824 list_for_each_entry(mqp, &ibdev->qp_list, qps_list) {
2825 spin_lock_irqsave(&mqp->sq.lock, flags_qp);
2826 if (mqp->sq.tail != mqp->sq.head) {
2827 send_mcq = to_mcq(mqp->ibqp.send_cq);
2828 spin_lock_irqsave(&send_mcq->lock, flags_cq);
2829 if (send_mcq->mcq.comp &&
2830 mqp->ibqp.send_cq->comp_handler) {
2831 if (!send_mcq->mcq.reset_notify_added) {
2832 send_mcq->mcq.reset_notify_added = 1;
2833 list_add_tail(&send_mcq->mcq.reset_notify,
2834 &cq_armed_list);
2835 }
2836 }
2837 spin_unlock_irqrestore(&send_mcq->lock, flags_cq);
2838 }
2839 spin_unlock_irqrestore(&mqp->sq.lock, flags_qp);
2840 spin_lock_irqsave(&mqp->rq.lock, flags_qp);
2841 /* no handling is needed for SRQ */
2842 if (!mqp->ibqp.srq) {
2843 if (mqp->rq.tail != mqp->rq.head) {
2844 recv_mcq = to_mcq(mqp->ibqp.recv_cq);
2845 spin_lock_irqsave(&recv_mcq->lock, flags_cq);
2846 if (recv_mcq->mcq.comp &&
2847 mqp->ibqp.recv_cq->comp_handler) {
2848 if (!recv_mcq->mcq.reset_notify_added) {
2849 recv_mcq->mcq.reset_notify_added = 1;
2850 list_add_tail(&recv_mcq->mcq.reset_notify,
2851 &cq_armed_list);
2852 }
2853 }
2854 spin_unlock_irqrestore(&recv_mcq->lock,
2855 flags_cq);
2856 }
2857 }
2858 spin_unlock_irqrestore(&mqp->rq.lock, flags_qp);
2859 }
2860 /*At that point all inflight post send were put to be executed as of we
2861 * lock/unlock above locks Now need to arm all involved CQs.
2862 */
2863 list_for_each_entry(mcq, &cq_armed_list, reset_notify) {
2864 mcq->comp(mcq);
2865 }
2866 spin_unlock_irqrestore(&ibdev->reset_flow_resource_lock, flags);
2867}
2868
Maor Gottlieb03404e82017-05-30 10:29:13 +03002869static void delay_drop_handler(struct work_struct *work)
2870{
2871 int err;
2872 struct mlx5_ib_delay_drop *delay_drop =
2873 container_of(work, struct mlx5_ib_delay_drop,
2874 delay_drop_work);
2875
Maor Gottliebfe248c32017-05-30 10:29:14 +03002876 atomic_inc(&delay_drop->events_cnt);
2877
Maor Gottlieb03404e82017-05-30 10:29:13 +03002878 mutex_lock(&delay_drop->lock);
2879 err = mlx5_core_set_delay_drop(delay_drop->dev->mdev,
2880 delay_drop->timeout);
2881 if (err) {
2882 mlx5_ib_warn(delay_drop->dev, "Failed to set delay drop, timeout=%u\n",
2883 delay_drop->timeout);
2884 delay_drop->activate = false;
2885 }
2886 mutex_unlock(&delay_drop->lock);
2887}
2888
Jack Morgenstein9603b612014-07-28 23:30:22 +03002889static void mlx5_ib_event(struct mlx5_core_dev *dev, void *context,
Jack Morgenstein4d2f9bb2014-07-28 23:30:24 +03002890 enum mlx5_dev_event event, unsigned long param)
Eli Cohene126ba92013-07-07 17:25:49 +03002891{
Jack Morgenstein9603b612014-07-28 23:30:22 +03002892 struct mlx5_ib_dev *ibdev = (struct mlx5_ib_dev *)context;
Eli Cohene126ba92013-07-07 17:25:49 +03002893 struct ib_event ibev;
Eli Cohendbaaff22016-10-27 16:36:44 +03002894 bool fatal = false;
Eli Cohene126ba92013-07-07 17:25:49 +03002895 u8 port = 0;
2896
2897 switch (event) {
2898 case MLX5_DEV_EVENT_SYS_ERROR:
Eli Cohene126ba92013-07-07 17:25:49 +03002899 ibev.event = IB_EVENT_DEVICE_FATAL;
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03002900 mlx5_ib_handle_internal_error(ibdev);
Eli Cohendbaaff22016-10-27 16:36:44 +03002901 fatal = true;
Eli Cohene126ba92013-07-07 17:25:49 +03002902 break;
2903
2904 case MLX5_DEV_EVENT_PORT_UP:
Eli Cohene126ba92013-07-07 17:25:49 +03002905 case MLX5_DEV_EVENT_PORT_DOWN:
Noa Osherovich2788cf32016-06-04 15:15:29 +03002906 case MLX5_DEV_EVENT_PORT_INITIALIZED:
Jack Morgenstein4d2f9bb2014-07-28 23:30:24 +03002907 port = (u8)param;
Aviv Heller5ec8c832016-09-18 20:48:00 +03002908
2909 /* In RoCE, port up/down events are handled in
2910 * mlx5_netdev_event().
2911 */
2912 if (mlx5_ib_port_link_layer(&ibdev->ib_dev, port) ==
2913 IB_LINK_LAYER_ETHERNET)
2914 return;
2915
2916 ibev.event = (event == MLX5_DEV_EVENT_PORT_UP) ?
2917 IB_EVENT_PORT_ACTIVE : IB_EVENT_PORT_ERR;
Eli Cohene126ba92013-07-07 17:25:49 +03002918 break;
2919
Eli Cohene126ba92013-07-07 17:25:49 +03002920 case MLX5_DEV_EVENT_LID_CHANGE:
2921 ibev.event = IB_EVENT_LID_CHANGE;
Jack Morgenstein4d2f9bb2014-07-28 23:30:24 +03002922 port = (u8)param;
Eli Cohene126ba92013-07-07 17:25:49 +03002923 break;
2924
2925 case MLX5_DEV_EVENT_PKEY_CHANGE:
2926 ibev.event = IB_EVENT_PKEY_CHANGE;
Jack Morgenstein4d2f9bb2014-07-28 23:30:24 +03002927 port = (u8)param;
Haggai Eran7722f472016-02-29 15:45:07 +02002928
2929 schedule_work(&ibdev->devr.ports[port - 1].pkey_change_work);
Eli Cohene126ba92013-07-07 17:25:49 +03002930 break;
2931
2932 case MLX5_DEV_EVENT_GUID_CHANGE:
2933 ibev.event = IB_EVENT_GID_CHANGE;
Jack Morgenstein4d2f9bb2014-07-28 23:30:24 +03002934 port = (u8)param;
Eli Cohene126ba92013-07-07 17:25:49 +03002935 break;
2936
2937 case MLX5_DEV_EVENT_CLIENT_REREG:
2938 ibev.event = IB_EVENT_CLIENT_REREGISTER;
Jack Morgenstein4d2f9bb2014-07-28 23:30:24 +03002939 port = (u8)param;
Eli Cohene126ba92013-07-07 17:25:49 +03002940 break;
Maor Gottlieb03404e82017-05-30 10:29:13 +03002941 case MLX5_DEV_EVENT_DELAY_DROP_TIMEOUT:
2942 schedule_work(&ibdev->delay_drop.delay_drop_work);
2943 goto out;
Saeed Mahameedbdc37922016-09-29 19:35:38 +03002944 default:
Maor Gottlieb03404e82017-05-30 10:29:13 +03002945 goto out;
Eli Cohene126ba92013-07-07 17:25:49 +03002946 }
2947
2948 ibev.device = &ibdev->ib_dev;
2949 ibev.element.port_num = port;
2950
Eli Cohena0c84c32013-09-11 16:35:27 +03002951 if (port < 1 || port > ibdev->num_ports) {
2952 mlx5_ib_warn(ibdev, "warning: event on port %d\n", port);
Maor Gottlieb03404e82017-05-30 10:29:13 +03002953 goto out;
Eli Cohena0c84c32013-09-11 16:35:27 +03002954 }
2955
Eli Cohene126ba92013-07-07 17:25:49 +03002956 if (ibdev->ib_active)
2957 ib_dispatch_event(&ibev);
Eli Cohendbaaff22016-10-27 16:36:44 +03002958
2959 if (fatal)
2960 ibdev->ib_active = false;
Maor Gottlieb03404e82017-05-30 10:29:13 +03002961
2962out:
2963 return;
Eli Cohene126ba92013-07-07 17:25:49 +03002964}
2965
Maor Gottliebc43f1112017-01-18 14:10:33 +02002966static int set_has_smi_cap(struct mlx5_ib_dev *dev)
2967{
2968 struct mlx5_hca_vport_context vport_ctx;
2969 int err;
2970 int port;
2971
2972 for (port = 1; port <= MLX5_CAP_GEN(dev->mdev, num_ports); port++) {
2973 dev->mdev->port_caps[port - 1].has_smi = false;
2974 if (MLX5_CAP_GEN(dev->mdev, port_type) ==
2975 MLX5_CAP_PORT_TYPE_IB) {
2976 if (MLX5_CAP_GEN(dev->mdev, ib_virt)) {
2977 err = mlx5_query_hca_vport_context(dev->mdev, 0,
2978 port, 0,
2979 &vport_ctx);
2980 if (err) {
2981 mlx5_ib_err(dev, "query_hca_vport_context for port=%d failed %d\n",
2982 port, err);
2983 return err;
2984 }
2985 dev->mdev->port_caps[port - 1].has_smi =
2986 vport_ctx.has_smi;
2987 } else {
2988 dev->mdev->port_caps[port - 1].has_smi = true;
2989 }
2990 }
2991 }
2992 return 0;
2993}
2994
Eli Cohene126ba92013-07-07 17:25:49 +03002995static void get_ext_port_caps(struct mlx5_ib_dev *dev)
2996{
2997 int port;
2998
Saeed Mahameed938fe832015-05-28 22:28:41 +03002999 for (port = 1; port <= MLX5_CAP_GEN(dev->mdev, num_ports); port++)
Eli Cohene126ba92013-07-07 17:25:49 +03003000 mlx5_query_ext_port_caps(dev, port);
3001}
3002
3003static int get_port_caps(struct mlx5_ib_dev *dev)
3004{
3005 struct ib_device_attr *dprops = NULL;
3006 struct ib_port_attr *pprops = NULL;
Dan Carpenterf614fc12015-01-12 11:56:58 +03003007 int err = -ENOMEM;
Eli Cohene126ba92013-07-07 17:25:49 +03003008 int port;
Matan Barak2528e332015-06-11 16:35:25 +03003009 struct ib_udata uhw = {.inlen = 0, .outlen = 0};
Eli Cohene126ba92013-07-07 17:25:49 +03003010
3011 pprops = kmalloc(sizeof(*pprops), GFP_KERNEL);
3012 if (!pprops)
3013 goto out;
3014
3015 dprops = kmalloc(sizeof(*dprops), GFP_KERNEL);
3016 if (!dprops)
3017 goto out;
3018
Maor Gottliebc43f1112017-01-18 14:10:33 +02003019 err = set_has_smi_cap(dev);
3020 if (err)
3021 goto out;
3022
Matan Barak2528e332015-06-11 16:35:25 +03003023 err = mlx5_ib_query_device(&dev->ib_dev, dprops, &uhw);
Eli Cohene126ba92013-07-07 17:25:49 +03003024 if (err) {
3025 mlx5_ib_warn(dev, "query_device failed %d\n", err);
3026 goto out;
3027 }
3028
Saeed Mahameed938fe832015-05-28 22:28:41 +03003029 for (port = 1; port <= MLX5_CAP_GEN(dev->mdev, num_ports); port++) {
Or Gerlitzc4550c62017-01-24 13:02:39 +02003030 memset(pprops, 0, sizeof(*pprops));
Eli Cohene126ba92013-07-07 17:25:49 +03003031 err = mlx5_ib_query_port(&dev->ib_dev, port, pprops);
3032 if (err) {
Saeed Mahameed938fe832015-05-28 22:28:41 +03003033 mlx5_ib_warn(dev, "query_port %d failed %d\n",
3034 port, err);
Eli Cohene126ba92013-07-07 17:25:49 +03003035 break;
3036 }
Saeed Mahameed938fe832015-05-28 22:28:41 +03003037 dev->mdev->port_caps[port - 1].pkey_table_len =
3038 dprops->max_pkeys;
3039 dev->mdev->port_caps[port - 1].gid_table_len =
3040 pprops->gid_tbl_len;
Eli Cohene126ba92013-07-07 17:25:49 +03003041 mlx5_ib_dbg(dev, "pkey_table_len %d, gid_table_len %d\n",
3042 dprops->max_pkeys, pprops->gid_tbl_len);
3043 }
3044
3045out:
3046 kfree(pprops);
3047 kfree(dprops);
3048
3049 return err;
3050}
3051
3052static void destroy_umrc_res(struct mlx5_ib_dev *dev)
3053{
3054 int err;
3055
3056 err = mlx5_mr_cache_cleanup(dev);
3057 if (err)
3058 mlx5_ib_warn(dev, "mr cache cleanup failed\n");
3059
3060 mlx5_ib_destroy_qp(dev->umrc.qp);
Christoph Hellwigadd08d72016-03-03 09:38:22 +01003061 ib_free_cq(dev->umrc.cq);
Eli Cohene126ba92013-07-07 17:25:49 +03003062 ib_dealloc_pd(dev->umrc.pd);
3063}
3064
3065enum {
3066 MAX_UMR_WR = 128,
3067};
3068
3069static int create_umr_res(struct mlx5_ib_dev *dev)
3070{
3071 struct ib_qp_init_attr *init_attr = NULL;
3072 struct ib_qp_attr *attr = NULL;
3073 struct ib_pd *pd;
3074 struct ib_cq *cq;
3075 struct ib_qp *qp;
Eli Cohene126ba92013-07-07 17:25:49 +03003076 int ret;
3077
3078 attr = kzalloc(sizeof(*attr), GFP_KERNEL);
3079 init_attr = kzalloc(sizeof(*init_attr), GFP_KERNEL);
3080 if (!attr || !init_attr) {
3081 ret = -ENOMEM;
3082 goto error_0;
3083 }
3084
Christoph Hellwiged082d32016-09-05 12:56:17 +02003085 pd = ib_alloc_pd(&dev->ib_dev, 0);
Eli Cohene126ba92013-07-07 17:25:49 +03003086 if (IS_ERR(pd)) {
3087 mlx5_ib_dbg(dev, "Couldn't create PD for sync UMR QP\n");
3088 ret = PTR_ERR(pd);
3089 goto error_0;
3090 }
3091
Christoph Hellwigadd08d72016-03-03 09:38:22 +01003092 cq = ib_alloc_cq(&dev->ib_dev, NULL, 128, 0, IB_POLL_SOFTIRQ);
Eli Cohene126ba92013-07-07 17:25:49 +03003093 if (IS_ERR(cq)) {
3094 mlx5_ib_dbg(dev, "Couldn't create CQ for sync UMR QP\n");
3095 ret = PTR_ERR(cq);
3096 goto error_2;
3097 }
Eli Cohene126ba92013-07-07 17:25:49 +03003098
3099 init_attr->send_cq = cq;
3100 init_attr->recv_cq = cq;
3101 init_attr->sq_sig_type = IB_SIGNAL_ALL_WR;
3102 init_attr->cap.max_send_wr = MAX_UMR_WR;
3103 init_attr->cap.max_send_sge = 1;
3104 init_attr->qp_type = MLX5_IB_QPT_REG_UMR;
3105 init_attr->port_num = 1;
3106 qp = mlx5_ib_create_qp(pd, init_attr, NULL);
3107 if (IS_ERR(qp)) {
3108 mlx5_ib_dbg(dev, "Couldn't create sync UMR QP\n");
3109 ret = PTR_ERR(qp);
3110 goto error_3;
3111 }
3112 qp->device = &dev->ib_dev;
3113 qp->real_qp = qp;
3114 qp->uobject = NULL;
3115 qp->qp_type = MLX5_IB_QPT_REG_UMR;
3116
3117 attr->qp_state = IB_QPS_INIT;
3118 attr->port_num = 1;
3119 ret = mlx5_ib_modify_qp(qp, attr, IB_QP_STATE | IB_QP_PKEY_INDEX |
3120 IB_QP_PORT, NULL);
3121 if (ret) {
3122 mlx5_ib_dbg(dev, "Couldn't modify UMR QP\n");
3123 goto error_4;
3124 }
3125
3126 memset(attr, 0, sizeof(*attr));
3127 attr->qp_state = IB_QPS_RTR;
3128 attr->path_mtu = IB_MTU_256;
3129
3130 ret = mlx5_ib_modify_qp(qp, attr, IB_QP_STATE, NULL);
3131 if (ret) {
3132 mlx5_ib_dbg(dev, "Couldn't modify umr QP to rtr\n");
3133 goto error_4;
3134 }
3135
3136 memset(attr, 0, sizeof(*attr));
3137 attr->qp_state = IB_QPS_RTS;
3138 ret = mlx5_ib_modify_qp(qp, attr, IB_QP_STATE, NULL);
3139 if (ret) {
3140 mlx5_ib_dbg(dev, "Couldn't modify umr QP to rts\n");
3141 goto error_4;
3142 }
3143
3144 dev->umrc.qp = qp;
3145 dev->umrc.cq = cq;
Eli Cohene126ba92013-07-07 17:25:49 +03003146 dev->umrc.pd = pd;
3147
3148 sema_init(&dev->umrc.sem, MAX_UMR_WR);
3149 ret = mlx5_mr_cache_init(dev);
3150 if (ret) {
3151 mlx5_ib_warn(dev, "mr cache init failed %d\n", ret);
3152 goto error_4;
3153 }
3154
3155 kfree(attr);
3156 kfree(init_attr);
3157
3158 return 0;
3159
3160error_4:
3161 mlx5_ib_destroy_qp(qp);
3162
3163error_3:
Christoph Hellwigadd08d72016-03-03 09:38:22 +01003164 ib_free_cq(cq);
Eli Cohene126ba92013-07-07 17:25:49 +03003165
3166error_2:
Eli Cohene126ba92013-07-07 17:25:49 +03003167 ib_dealloc_pd(pd);
3168
3169error_0:
3170 kfree(attr);
3171 kfree(init_attr);
3172 return ret;
3173}
3174
Max Gurtovoy6e8484c2017-05-28 10:53:11 +03003175static u8 mlx5_get_umr_fence(u8 umr_fence_cap)
3176{
3177 switch (umr_fence_cap) {
3178 case MLX5_CAP_UMR_FENCE_NONE:
3179 return MLX5_FENCE_MODE_NONE;
3180 case MLX5_CAP_UMR_FENCE_SMALL:
3181 return MLX5_FENCE_MODE_INITIATOR_SMALL;
3182 default:
3183 return MLX5_FENCE_MODE_STRONG_ORDERING;
3184 }
3185}
3186
Eli Cohene126ba92013-07-07 17:25:49 +03003187static int create_dev_resources(struct mlx5_ib_resources *devr)
3188{
3189 struct ib_srq_init_attr attr;
3190 struct mlx5_ib_dev *dev;
Matan Barakbcf4c1e2015-06-11 16:35:20 +03003191 struct ib_cq_init_attr cq_attr = {.cqe = 1};
Haggai Eran7722f472016-02-29 15:45:07 +02003192 int port;
Eli Cohene126ba92013-07-07 17:25:49 +03003193 int ret = 0;
3194
3195 dev = container_of(devr, struct mlx5_ib_dev, devr);
3196
Haggai Erand16e91d2016-02-29 15:45:05 +02003197 mutex_init(&devr->mutex);
3198
Eli Cohene126ba92013-07-07 17:25:49 +03003199 devr->p0 = mlx5_ib_alloc_pd(&dev->ib_dev, NULL, NULL);
3200 if (IS_ERR(devr->p0)) {
3201 ret = PTR_ERR(devr->p0);
3202 goto error0;
3203 }
3204 devr->p0->device = &dev->ib_dev;
3205 devr->p0->uobject = NULL;
3206 atomic_set(&devr->p0->usecnt, 0);
3207
Matan Barakbcf4c1e2015-06-11 16:35:20 +03003208 devr->c0 = mlx5_ib_create_cq(&dev->ib_dev, &cq_attr, NULL, NULL);
Eli Cohene126ba92013-07-07 17:25:49 +03003209 if (IS_ERR(devr->c0)) {
3210 ret = PTR_ERR(devr->c0);
3211 goto error1;
3212 }
3213 devr->c0->device = &dev->ib_dev;
3214 devr->c0->uobject = NULL;
3215 devr->c0->comp_handler = NULL;
3216 devr->c0->event_handler = NULL;
3217 devr->c0->cq_context = NULL;
3218 atomic_set(&devr->c0->usecnt, 0);
3219
3220 devr->x0 = mlx5_ib_alloc_xrcd(&dev->ib_dev, NULL, NULL);
3221 if (IS_ERR(devr->x0)) {
3222 ret = PTR_ERR(devr->x0);
3223 goto error2;
3224 }
3225 devr->x0->device = &dev->ib_dev;
3226 devr->x0->inode = NULL;
3227 atomic_set(&devr->x0->usecnt, 0);
3228 mutex_init(&devr->x0->tgt_qp_mutex);
3229 INIT_LIST_HEAD(&devr->x0->tgt_qp_list);
3230
3231 devr->x1 = mlx5_ib_alloc_xrcd(&dev->ib_dev, NULL, NULL);
3232 if (IS_ERR(devr->x1)) {
3233 ret = PTR_ERR(devr->x1);
3234 goto error3;
3235 }
3236 devr->x1->device = &dev->ib_dev;
3237 devr->x1->inode = NULL;
3238 atomic_set(&devr->x1->usecnt, 0);
3239 mutex_init(&devr->x1->tgt_qp_mutex);
3240 INIT_LIST_HEAD(&devr->x1->tgt_qp_list);
3241
3242 memset(&attr, 0, sizeof(attr));
3243 attr.attr.max_sge = 1;
3244 attr.attr.max_wr = 1;
3245 attr.srq_type = IB_SRQT_XRC;
Artemy Kovalyov1a56ff62017-08-17 15:52:04 +03003246 attr.ext.cq = devr->c0;
Eli Cohene126ba92013-07-07 17:25:49 +03003247 attr.ext.xrc.xrcd = devr->x0;
3248
3249 devr->s0 = mlx5_ib_create_srq(devr->p0, &attr, NULL);
3250 if (IS_ERR(devr->s0)) {
3251 ret = PTR_ERR(devr->s0);
3252 goto error4;
3253 }
3254 devr->s0->device = &dev->ib_dev;
3255 devr->s0->pd = devr->p0;
3256 devr->s0->uobject = NULL;
3257 devr->s0->event_handler = NULL;
3258 devr->s0->srq_context = NULL;
3259 devr->s0->srq_type = IB_SRQT_XRC;
3260 devr->s0->ext.xrc.xrcd = devr->x0;
Artemy Kovalyov1a56ff62017-08-17 15:52:04 +03003261 devr->s0->ext.cq = devr->c0;
Eli Cohene126ba92013-07-07 17:25:49 +03003262 atomic_inc(&devr->s0->ext.xrc.xrcd->usecnt);
Artemy Kovalyov1a56ff62017-08-17 15:52:04 +03003263 atomic_inc(&devr->s0->ext.cq->usecnt);
Eli Cohene126ba92013-07-07 17:25:49 +03003264 atomic_inc(&devr->p0->usecnt);
3265 atomic_set(&devr->s0->usecnt, 0);
3266
Haggai Abramonvsky4aa17b22015-06-04 19:30:48 +03003267 memset(&attr, 0, sizeof(attr));
3268 attr.attr.max_sge = 1;
3269 attr.attr.max_wr = 1;
3270 attr.srq_type = IB_SRQT_BASIC;
3271 devr->s1 = mlx5_ib_create_srq(devr->p0, &attr, NULL);
3272 if (IS_ERR(devr->s1)) {
3273 ret = PTR_ERR(devr->s1);
3274 goto error5;
3275 }
3276 devr->s1->device = &dev->ib_dev;
3277 devr->s1->pd = devr->p0;
3278 devr->s1->uobject = NULL;
3279 devr->s1->event_handler = NULL;
3280 devr->s1->srq_context = NULL;
3281 devr->s1->srq_type = IB_SRQT_BASIC;
Artemy Kovalyov1a56ff62017-08-17 15:52:04 +03003282 devr->s1->ext.cq = devr->c0;
Haggai Abramonvsky4aa17b22015-06-04 19:30:48 +03003283 atomic_inc(&devr->p0->usecnt);
Artemy Kovalyov1a56ff62017-08-17 15:52:04 +03003284 atomic_set(&devr->s1->usecnt, 0);
Haggai Abramonvsky4aa17b22015-06-04 19:30:48 +03003285
Haggai Eran7722f472016-02-29 15:45:07 +02003286 for (port = 0; port < ARRAY_SIZE(devr->ports); ++port) {
3287 INIT_WORK(&devr->ports[port].pkey_change_work,
3288 pkey_change_handler);
3289 devr->ports[port].devr = devr;
3290 }
3291
Eli Cohene126ba92013-07-07 17:25:49 +03003292 return 0;
3293
Haggai Abramonvsky4aa17b22015-06-04 19:30:48 +03003294error5:
3295 mlx5_ib_destroy_srq(devr->s0);
Eli Cohene126ba92013-07-07 17:25:49 +03003296error4:
3297 mlx5_ib_dealloc_xrcd(devr->x1);
3298error3:
3299 mlx5_ib_dealloc_xrcd(devr->x0);
3300error2:
3301 mlx5_ib_destroy_cq(devr->c0);
3302error1:
3303 mlx5_ib_dealloc_pd(devr->p0);
3304error0:
3305 return ret;
3306}
3307
3308static void destroy_dev_resources(struct mlx5_ib_resources *devr)
3309{
Haggai Eran7722f472016-02-29 15:45:07 +02003310 struct mlx5_ib_dev *dev =
3311 container_of(devr, struct mlx5_ib_dev, devr);
3312 int port;
3313
Haggai Abramonvsky4aa17b22015-06-04 19:30:48 +03003314 mlx5_ib_destroy_srq(devr->s1);
Eli Cohene126ba92013-07-07 17:25:49 +03003315 mlx5_ib_destroy_srq(devr->s0);
3316 mlx5_ib_dealloc_xrcd(devr->x0);
3317 mlx5_ib_dealloc_xrcd(devr->x1);
3318 mlx5_ib_destroy_cq(devr->c0);
3319 mlx5_ib_dealloc_pd(devr->p0);
Haggai Eran7722f472016-02-29 15:45:07 +02003320
3321 /* Make sure no change P_Key work items are still executing */
3322 for (port = 0; port < dev->num_ports; ++port)
3323 cancel_work_sync(&devr->ports[port].pkey_change_work);
Eli Cohene126ba92013-07-07 17:25:49 +03003324}
3325
Achiad Shochate53505a2015-12-23 18:47:25 +02003326static u32 get_core_cap_flags(struct ib_device *ibdev)
3327{
3328 struct mlx5_ib_dev *dev = to_mdev(ibdev);
3329 enum rdma_link_layer ll = mlx5_ib_port_link_layer(ibdev, 1);
3330 u8 l3_type_cap = MLX5_CAP_ROCE(dev->mdev, l3_type);
3331 u8 roce_version_cap = MLX5_CAP_ROCE(dev->mdev, roce_version);
3332 u32 ret = 0;
3333
3334 if (ll == IB_LINK_LAYER_INFINIBAND)
3335 return RDMA_CORE_PORT_IBA_IB;
3336
Or Gerlitz72cd5712017-01-24 13:02:36 +02003337 ret = RDMA_CORE_PORT_RAW_PACKET;
3338
Achiad Shochate53505a2015-12-23 18:47:25 +02003339 if (!(l3_type_cap & MLX5_ROCE_L3_TYPE_IPV4_CAP))
Or Gerlitz72cd5712017-01-24 13:02:36 +02003340 return ret;
Achiad Shochate53505a2015-12-23 18:47:25 +02003341
3342 if (!(l3_type_cap & MLX5_ROCE_L3_TYPE_IPV6_CAP))
Or Gerlitz72cd5712017-01-24 13:02:36 +02003343 return ret;
Achiad Shochate53505a2015-12-23 18:47:25 +02003344
3345 if (roce_version_cap & MLX5_ROCE_VERSION_1_CAP)
3346 ret |= RDMA_CORE_PORT_IBA_ROCE;
3347
3348 if (roce_version_cap & MLX5_ROCE_VERSION_2_CAP)
3349 ret |= RDMA_CORE_PORT_IBA_ROCE_UDP_ENCAP;
3350
3351 return ret;
3352}
3353
Ira Weiny77386132015-05-13 20:02:58 -04003354static int mlx5_port_immutable(struct ib_device *ibdev, u8 port_num,
3355 struct ib_port_immutable *immutable)
3356{
3357 struct ib_port_attr attr;
Or Gerlitzca5b91d2016-11-27 16:51:36 +02003358 struct mlx5_ib_dev *dev = to_mdev(ibdev);
3359 enum rdma_link_layer ll = mlx5_ib_port_link_layer(ibdev, port_num);
Ira Weiny77386132015-05-13 20:02:58 -04003360 int err;
3361
Or Gerlitzc4550c62017-01-24 13:02:39 +02003362 immutable->core_cap_flags = get_core_cap_flags(ibdev);
3363
3364 err = ib_query_port(ibdev, port_num, &attr);
Ira Weiny77386132015-05-13 20:02:58 -04003365 if (err)
3366 return err;
3367
3368 immutable->pkey_tbl_len = attr.pkey_tbl_len;
3369 immutable->gid_tbl_len = attr.gid_tbl_len;
Achiad Shochate53505a2015-12-23 18:47:25 +02003370 immutable->core_cap_flags = get_core_cap_flags(ibdev);
Or Gerlitzca5b91d2016-11-27 16:51:36 +02003371 if ((ll == IB_LINK_LAYER_INFINIBAND) || MLX5_CAP_GEN(dev->mdev, roce))
3372 immutable->max_mad_size = IB_MGMT_MAD_SIZE;
Ira Weiny77386132015-05-13 20:02:58 -04003373
3374 return 0;
3375}
3376
Leon Romanovsky9abb0d12017-06-27 16:49:53 +03003377static void get_dev_fw_str(struct ib_device *ibdev, char *str)
Ira Weinyc7342822016-06-15 02:22:01 -04003378{
3379 struct mlx5_ib_dev *dev =
3380 container_of(ibdev, struct mlx5_ib_dev, ib_dev);
Leon Romanovsky9abb0d12017-06-27 16:49:53 +03003381 snprintf(str, IB_FW_VERSION_NAME_MAX, "%d.%d.%04d",
3382 fw_rev_maj(dev->mdev), fw_rev_min(dev->mdev),
3383 fw_rev_sub(dev->mdev));
Ira Weinyc7342822016-06-15 02:22:01 -04003384}
3385
Or Gerlitz45f95ac2016-11-27 16:51:35 +02003386static int mlx5_eth_lag_init(struct mlx5_ib_dev *dev)
Aviv Heller9ef9c642016-09-18 20:48:01 +03003387{
3388 struct mlx5_core_dev *mdev = dev->mdev;
3389 struct mlx5_flow_namespace *ns = mlx5_get_flow_namespace(mdev,
3390 MLX5_FLOW_NAMESPACE_LAG);
3391 struct mlx5_flow_table *ft;
3392 int err;
3393
3394 if (!ns || !mlx5_lag_is_active(mdev))
3395 return 0;
3396
3397 err = mlx5_cmd_create_vport_lag(mdev);
3398 if (err)
3399 return err;
3400
3401 ft = mlx5_create_lag_demux_flow_table(ns, 0, 0);
3402 if (IS_ERR(ft)) {
3403 err = PTR_ERR(ft);
3404 goto err_destroy_vport_lag;
3405 }
3406
3407 dev->flow_db.lag_demux_ft = ft;
3408 return 0;
3409
3410err_destroy_vport_lag:
3411 mlx5_cmd_destroy_vport_lag(mdev);
3412 return err;
3413}
3414
Or Gerlitz45f95ac2016-11-27 16:51:35 +02003415static void mlx5_eth_lag_cleanup(struct mlx5_ib_dev *dev)
Aviv Heller9ef9c642016-09-18 20:48:01 +03003416{
3417 struct mlx5_core_dev *mdev = dev->mdev;
3418
3419 if (dev->flow_db.lag_demux_ft) {
3420 mlx5_destroy_flow_table(dev->flow_db.lag_demux_ft);
3421 dev->flow_db.lag_demux_ft = NULL;
3422
3423 mlx5_cmd_destroy_vport_lag(mdev);
3424 }
3425}
3426
Or Gerlitzd012f5d2016-11-27 16:51:34 +02003427static int mlx5_add_netdev_notifier(struct mlx5_ib_dev *dev)
Achiad Shochatfc24fc52015-12-23 18:47:17 +02003428{
Achiad Shochate53505a2015-12-23 18:47:25 +02003429 int err;
3430
Achiad Shochatfc24fc52015-12-23 18:47:17 +02003431 dev->roce.nb.notifier_call = mlx5_netdev_event;
Achiad Shochate53505a2015-12-23 18:47:25 +02003432 err = register_netdevice_notifier(&dev->roce.nb);
Aviv Heller5ec8c832016-09-18 20:48:00 +03003433 if (err) {
3434 dev->roce.nb.notifier_call = NULL;
Achiad Shochate53505a2015-12-23 18:47:25 +02003435 return err;
Aviv Heller5ec8c832016-09-18 20:48:00 +03003436 }
Achiad Shochate53505a2015-12-23 18:47:25 +02003437
Or Gerlitzd012f5d2016-11-27 16:51:34 +02003438 return 0;
3439}
Achiad Shochate53505a2015-12-23 18:47:25 +02003440
Or Gerlitzd012f5d2016-11-27 16:51:34 +02003441static void mlx5_remove_netdev_notifier(struct mlx5_ib_dev *dev)
Eli Cohene126ba92013-07-07 17:25:49 +03003442{
3443 if (dev->roce.nb.notifier_call) {
3444 unregister_netdevice_notifier(&dev->roce.nb);
3445 dev->roce.nb.notifier_call = NULL;
3446 }
3447}
3448
Or Gerlitz45f95ac2016-11-27 16:51:35 +02003449static int mlx5_enable_eth(struct mlx5_ib_dev *dev)
Eli Cohene126ba92013-07-07 17:25:49 +03003450{
Eli Cohene126ba92013-07-07 17:25:49 +03003451 int err;
3452
Or Gerlitzd012f5d2016-11-27 16:51:34 +02003453 err = mlx5_add_netdev_notifier(dev);
3454 if (err)
Achiad Shochate53505a2015-12-23 18:47:25 +02003455 return err;
Achiad Shochate53505a2015-12-23 18:47:25 +02003456
Or Gerlitzca5b91d2016-11-27 16:51:36 +02003457 if (MLX5_CAP_GEN(dev->mdev, roce)) {
3458 err = mlx5_nic_vport_enable_roce(dev->mdev);
3459 if (err)
3460 goto err_unregister_netdevice_notifier;
3461 }
Achiad Shochate53505a2015-12-23 18:47:25 +02003462
Or Gerlitz45f95ac2016-11-27 16:51:35 +02003463 err = mlx5_eth_lag_init(dev);
Aviv Heller9ef9c642016-09-18 20:48:01 +03003464 if (err)
3465 goto err_disable_roce;
3466
Achiad Shochate53505a2015-12-23 18:47:25 +02003467 return 0;
3468
Aviv Heller9ef9c642016-09-18 20:48:01 +03003469err_disable_roce:
Or Gerlitzca5b91d2016-11-27 16:51:36 +02003470 if (MLX5_CAP_GEN(dev->mdev, roce))
3471 mlx5_nic_vport_disable_roce(dev->mdev);
Aviv Heller9ef9c642016-09-18 20:48:01 +03003472
Achiad Shochate53505a2015-12-23 18:47:25 +02003473err_unregister_netdevice_notifier:
Or Gerlitzd012f5d2016-11-27 16:51:34 +02003474 mlx5_remove_netdev_notifier(dev);
Achiad Shochate53505a2015-12-23 18:47:25 +02003475 return err;
Achiad Shochatfc24fc52015-12-23 18:47:17 +02003476}
3477
Or Gerlitz45f95ac2016-11-27 16:51:35 +02003478static void mlx5_disable_eth(struct mlx5_ib_dev *dev)
Achiad Shochatfc24fc52015-12-23 18:47:17 +02003479{
Or Gerlitz45f95ac2016-11-27 16:51:35 +02003480 mlx5_eth_lag_cleanup(dev);
Or Gerlitzca5b91d2016-11-27 16:51:36 +02003481 if (MLX5_CAP_GEN(dev->mdev, roce))
3482 mlx5_nic_vport_disable_roce(dev->mdev);
Achiad Shochatfc24fc52015-12-23 18:47:17 +02003483}
3484
Parav Pandite1f24a72017-04-16 07:29:29 +03003485struct mlx5_ib_counter {
Kamal Heib7c16f472017-01-18 15:25:09 +02003486 const char *name;
3487 size_t offset;
3488};
3489
3490#define INIT_Q_COUNTER(_name) \
3491 { .name = #_name, .offset = MLX5_BYTE_OFF(query_q_counter_out, _name)}
3492
Parav Pandite1f24a72017-04-16 07:29:29 +03003493static const struct mlx5_ib_counter basic_q_cnts[] = {
Kamal Heib7c16f472017-01-18 15:25:09 +02003494 INIT_Q_COUNTER(rx_write_requests),
3495 INIT_Q_COUNTER(rx_read_requests),
3496 INIT_Q_COUNTER(rx_atomic_requests),
3497 INIT_Q_COUNTER(out_of_buffer),
3498};
3499
Parav Pandite1f24a72017-04-16 07:29:29 +03003500static const struct mlx5_ib_counter out_of_seq_q_cnts[] = {
Kamal Heib7c16f472017-01-18 15:25:09 +02003501 INIT_Q_COUNTER(out_of_sequence),
3502};
3503
Parav Pandite1f24a72017-04-16 07:29:29 +03003504static const struct mlx5_ib_counter retrans_q_cnts[] = {
Kamal Heib7c16f472017-01-18 15:25:09 +02003505 INIT_Q_COUNTER(duplicate_request),
3506 INIT_Q_COUNTER(rnr_nak_retry_err),
3507 INIT_Q_COUNTER(packet_seq_err),
3508 INIT_Q_COUNTER(implied_nak_seq_err),
3509 INIT_Q_COUNTER(local_ack_timeout_err),
3510};
3511
Parav Pandite1f24a72017-04-16 07:29:29 +03003512#define INIT_CONG_COUNTER(_name) \
3513 { .name = #_name, .offset = \
3514 MLX5_BYTE_OFF(query_cong_statistics_out, _name ## _high)}
3515
3516static const struct mlx5_ib_counter cong_cnts[] = {
3517 INIT_CONG_COUNTER(rp_cnp_ignored),
3518 INIT_CONG_COUNTER(rp_cnp_handled),
3519 INIT_CONG_COUNTER(np_ecn_marked_roce_packets),
3520 INIT_CONG_COUNTER(np_cnp_sent),
3521};
3522
Parav Pandit58dcb602017-06-19 07:19:37 +03003523static const struct mlx5_ib_counter extended_err_cnts[] = {
3524 INIT_Q_COUNTER(resp_local_length_error),
3525 INIT_Q_COUNTER(resp_cqe_error),
3526 INIT_Q_COUNTER(req_cqe_error),
3527 INIT_Q_COUNTER(req_remote_invalid_request),
3528 INIT_Q_COUNTER(req_remote_access_errors),
3529 INIT_Q_COUNTER(resp_remote_access_errors),
3530 INIT_Q_COUNTER(resp_cqe_flush_error),
3531 INIT_Q_COUNTER(req_cqe_flush_error),
3532};
3533
Parav Pandite1f24a72017-04-16 07:29:29 +03003534static void mlx5_ib_dealloc_counters(struct mlx5_ib_dev *dev)
Mark Bloch0837e862016-06-17 15:10:55 +03003535{
3536 unsigned int i;
3537
Kamal Heib7c16f472017-01-18 15:25:09 +02003538 for (i = 0; i < dev->num_ports; i++) {
Mark Bloch0837e862016-06-17 15:10:55 +03003539 mlx5_core_dealloc_q_counter(dev->mdev,
Parav Pandite1f24a72017-04-16 07:29:29 +03003540 dev->port[i].cnts.set_id);
3541 kfree(dev->port[i].cnts.names);
3542 kfree(dev->port[i].cnts.offsets);
Kamal Heib7c16f472017-01-18 15:25:09 +02003543 }
3544}
3545
Parav Pandite1f24a72017-04-16 07:29:29 +03003546static int __mlx5_ib_alloc_counters(struct mlx5_ib_dev *dev,
3547 struct mlx5_ib_counters *cnts)
Kamal Heib7c16f472017-01-18 15:25:09 +02003548{
3549 u32 num_counters;
3550
3551 num_counters = ARRAY_SIZE(basic_q_cnts);
3552
3553 if (MLX5_CAP_GEN(dev->mdev, out_of_seq_cnt))
3554 num_counters += ARRAY_SIZE(out_of_seq_q_cnts);
3555
3556 if (MLX5_CAP_GEN(dev->mdev, retransmission_q_counters))
3557 num_counters += ARRAY_SIZE(retrans_q_cnts);
Parav Pandit58dcb602017-06-19 07:19:37 +03003558
3559 if (MLX5_CAP_GEN(dev->mdev, enhanced_error_q_counters))
3560 num_counters += ARRAY_SIZE(extended_err_cnts);
3561
Parav Pandite1f24a72017-04-16 07:29:29 +03003562 cnts->num_q_counters = num_counters;
Kamal Heib7c16f472017-01-18 15:25:09 +02003563
Parav Pandite1f24a72017-04-16 07:29:29 +03003564 if (MLX5_CAP_GEN(dev->mdev, cc_query_allowed)) {
3565 cnts->num_cong_counters = ARRAY_SIZE(cong_cnts);
3566 num_counters += ARRAY_SIZE(cong_cnts);
3567 }
3568
3569 cnts->names = kcalloc(num_counters, sizeof(cnts->names), GFP_KERNEL);
3570 if (!cnts->names)
Kamal Heib7c16f472017-01-18 15:25:09 +02003571 return -ENOMEM;
3572
Parav Pandite1f24a72017-04-16 07:29:29 +03003573 cnts->offsets = kcalloc(num_counters,
3574 sizeof(cnts->offsets), GFP_KERNEL);
3575 if (!cnts->offsets)
Kamal Heib7c16f472017-01-18 15:25:09 +02003576 goto err_names;
3577
Kamal Heib7c16f472017-01-18 15:25:09 +02003578 return 0;
3579
3580err_names:
Parav Pandite1f24a72017-04-16 07:29:29 +03003581 kfree(cnts->names);
Kamal Heib7c16f472017-01-18 15:25:09 +02003582 return -ENOMEM;
3583}
3584
Parav Pandite1f24a72017-04-16 07:29:29 +03003585static void mlx5_ib_fill_counters(struct mlx5_ib_dev *dev,
3586 const char **names,
3587 size_t *offsets)
Kamal Heib7c16f472017-01-18 15:25:09 +02003588{
3589 int i;
3590 int j = 0;
3591
3592 for (i = 0; i < ARRAY_SIZE(basic_q_cnts); i++, j++) {
3593 names[j] = basic_q_cnts[i].name;
3594 offsets[j] = basic_q_cnts[i].offset;
3595 }
3596
3597 if (MLX5_CAP_GEN(dev->mdev, out_of_seq_cnt)) {
3598 for (i = 0; i < ARRAY_SIZE(out_of_seq_q_cnts); i++, j++) {
3599 names[j] = out_of_seq_q_cnts[i].name;
3600 offsets[j] = out_of_seq_q_cnts[i].offset;
3601 }
3602 }
3603
3604 if (MLX5_CAP_GEN(dev->mdev, retransmission_q_counters)) {
3605 for (i = 0; i < ARRAY_SIZE(retrans_q_cnts); i++, j++) {
3606 names[j] = retrans_q_cnts[i].name;
3607 offsets[j] = retrans_q_cnts[i].offset;
3608 }
3609 }
Parav Pandite1f24a72017-04-16 07:29:29 +03003610
Parav Pandit58dcb602017-06-19 07:19:37 +03003611 if (MLX5_CAP_GEN(dev->mdev, enhanced_error_q_counters)) {
3612 for (i = 0; i < ARRAY_SIZE(extended_err_cnts); i++, j++) {
3613 names[j] = extended_err_cnts[i].name;
3614 offsets[j] = extended_err_cnts[i].offset;
3615 }
3616 }
3617
Parav Pandite1f24a72017-04-16 07:29:29 +03003618 if (MLX5_CAP_GEN(dev->mdev, cc_query_allowed)) {
3619 for (i = 0; i < ARRAY_SIZE(cong_cnts); i++, j++) {
3620 names[j] = cong_cnts[i].name;
3621 offsets[j] = cong_cnts[i].offset;
3622 }
3623 }
Mark Bloch0837e862016-06-17 15:10:55 +03003624}
3625
Parav Pandite1f24a72017-04-16 07:29:29 +03003626static int mlx5_ib_alloc_counters(struct mlx5_ib_dev *dev)
Mark Bloch0837e862016-06-17 15:10:55 +03003627{
3628 int i;
3629 int ret;
3630
3631 for (i = 0; i < dev->num_ports; i++) {
Kamal Heib7c16f472017-01-18 15:25:09 +02003632 struct mlx5_ib_port *port = &dev->port[i];
3633
Mark Bloch0837e862016-06-17 15:10:55 +03003634 ret = mlx5_core_alloc_q_counter(dev->mdev,
Parav Pandite1f24a72017-04-16 07:29:29 +03003635 &port->cnts.set_id);
Mark Bloch0837e862016-06-17 15:10:55 +03003636 if (ret) {
3637 mlx5_ib_warn(dev,
3638 "couldn't allocate queue counter for port %d, err %d\n",
3639 i + 1, ret);
3640 goto dealloc_counters;
3641 }
Kamal Heib7c16f472017-01-18 15:25:09 +02003642
Parav Pandite1f24a72017-04-16 07:29:29 +03003643 ret = __mlx5_ib_alloc_counters(dev, &port->cnts);
Kamal Heib7c16f472017-01-18 15:25:09 +02003644 if (ret)
3645 goto dealloc_counters;
3646
Parav Pandite1f24a72017-04-16 07:29:29 +03003647 mlx5_ib_fill_counters(dev, port->cnts.names,
3648 port->cnts.offsets);
Mark Bloch0837e862016-06-17 15:10:55 +03003649 }
3650
3651 return 0;
3652
3653dealloc_counters:
3654 while (--i >= 0)
3655 mlx5_core_dealloc_q_counter(dev->mdev,
Parav Pandite1f24a72017-04-16 07:29:29 +03003656 dev->port[i].cnts.set_id);
Mark Bloch0837e862016-06-17 15:10:55 +03003657
3658 return ret;
3659}
3660
Mark Bloch0ad17a82016-06-17 15:10:56 +03003661static struct rdma_hw_stats *mlx5_ib_alloc_hw_stats(struct ib_device *ibdev,
3662 u8 port_num)
3663{
Kamal Heib7c16f472017-01-18 15:25:09 +02003664 struct mlx5_ib_dev *dev = to_mdev(ibdev);
3665 struct mlx5_ib_port *port = &dev->port[port_num - 1];
Mark Bloch0ad17a82016-06-17 15:10:56 +03003666
3667 /* We support only per port stats */
3668 if (port_num == 0)
3669 return NULL;
3670
Parav Pandite1f24a72017-04-16 07:29:29 +03003671 return rdma_alloc_hw_stats_struct(port->cnts.names,
3672 port->cnts.num_q_counters +
3673 port->cnts.num_cong_counters,
Mark Bloch0ad17a82016-06-17 15:10:56 +03003674 RDMA_HW_STATS_DEFAULT_LIFESPAN);
3675}
3676
Parav Pandite1f24a72017-04-16 07:29:29 +03003677static int mlx5_ib_query_q_counters(struct mlx5_ib_dev *dev,
3678 struct mlx5_ib_port *port,
3679 struct rdma_hw_stats *stats)
3680{
3681 int outlen = MLX5_ST_SZ_BYTES(query_q_counter_out);
3682 void *out;
3683 __be32 val;
3684 int ret, i;
3685
Leon Romanovsky1b9a07e2017-05-10 21:32:18 +03003686 out = kvzalloc(outlen, GFP_KERNEL);
Parav Pandite1f24a72017-04-16 07:29:29 +03003687 if (!out)
3688 return -ENOMEM;
3689
3690 ret = mlx5_core_query_q_counter(dev->mdev,
3691 port->cnts.set_id, 0,
3692 out, outlen);
3693 if (ret)
3694 goto free;
3695
3696 for (i = 0; i < port->cnts.num_q_counters; i++) {
3697 val = *(__be32 *)(out + port->cnts.offsets[i]);
3698 stats->value[i] = (u64)be32_to_cpu(val);
3699 }
3700
3701free:
3702 kvfree(out);
3703 return ret;
3704}
3705
3706static int mlx5_ib_query_cong_counters(struct mlx5_ib_dev *dev,
3707 struct mlx5_ib_port *port,
3708 struct rdma_hw_stats *stats)
3709{
3710 int outlen = MLX5_ST_SZ_BYTES(query_cong_statistics_out);
3711 void *out;
3712 int ret, i;
3713 int offset = port->cnts.num_q_counters;
3714
Leon Romanovsky1b9a07e2017-05-10 21:32:18 +03003715 out = kvzalloc(outlen, GFP_KERNEL);
Parav Pandite1f24a72017-04-16 07:29:29 +03003716 if (!out)
3717 return -ENOMEM;
3718
3719 ret = mlx5_cmd_query_cong_counter(dev->mdev, false, out, outlen);
3720 if (ret)
3721 goto free;
3722
3723 for (i = 0; i < port->cnts.num_cong_counters; i++) {
3724 stats->value[i + offset] =
3725 be64_to_cpup((__be64 *)(out +
3726 port->cnts.offsets[i + offset]));
3727 }
3728
3729free:
3730 kvfree(out);
3731 return ret;
3732}
3733
Mark Bloch0ad17a82016-06-17 15:10:56 +03003734static int mlx5_ib_get_hw_stats(struct ib_device *ibdev,
3735 struct rdma_hw_stats *stats,
Kamal Heib7c16f472017-01-18 15:25:09 +02003736 u8 port_num, int index)
Mark Bloch0ad17a82016-06-17 15:10:56 +03003737{
3738 struct mlx5_ib_dev *dev = to_mdev(ibdev);
Kamal Heib7c16f472017-01-18 15:25:09 +02003739 struct mlx5_ib_port *port = &dev->port[port_num - 1];
Parav Pandite1f24a72017-04-16 07:29:29 +03003740 int ret, num_counters;
Mark Bloch0ad17a82016-06-17 15:10:56 +03003741
Kamal Heib7c16f472017-01-18 15:25:09 +02003742 if (!stats)
Parav Pandite1f24a72017-04-16 07:29:29 +03003743 return -EINVAL;
Mark Bloch0ad17a82016-06-17 15:10:56 +03003744
Parav Pandite1f24a72017-04-16 07:29:29 +03003745 ret = mlx5_ib_query_q_counters(dev, port, stats);
Mark Bloch0ad17a82016-06-17 15:10:56 +03003746 if (ret)
Parav Pandite1f24a72017-04-16 07:29:29 +03003747 return ret;
3748 num_counters = port->cnts.num_q_counters;
Mark Bloch0ad17a82016-06-17 15:10:56 +03003749
Parav Pandite1f24a72017-04-16 07:29:29 +03003750 if (MLX5_CAP_GEN(dev->mdev, cc_query_allowed)) {
3751 ret = mlx5_ib_query_cong_counters(dev, port, stats);
3752 if (ret)
3753 return ret;
3754 num_counters += port->cnts.num_cong_counters;
Mark Bloch0ad17a82016-06-17 15:10:56 +03003755 }
Kamal Heib7c16f472017-01-18 15:25:09 +02003756
Parav Pandite1f24a72017-04-16 07:29:29 +03003757 return num_counters;
Mark Bloch0ad17a82016-06-17 15:10:56 +03003758}
3759
Niranjana Vishwanathapura8e959602017-06-30 13:14:46 -07003760static void mlx5_ib_free_rdma_netdev(struct net_device *netdev)
3761{
3762 return mlx5_rdma_netdev_free(netdev);
3763}
3764
Erez Shitrit693dfd52017-04-27 17:01:34 +03003765static struct net_device*
3766mlx5_ib_alloc_rdma_netdev(struct ib_device *hca,
3767 u8 port_num,
3768 enum rdma_netdev_t type,
3769 const char *name,
3770 unsigned char name_assign_type,
3771 void (*setup)(struct net_device *))
3772{
Niranjana Vishwanathapura8e959602017-06-30 13:14:46 -07003773 struct net_device *netdev;
3774 struct rdma_netdev *rn;
3775
Erez Shitrit693dfd52017-04-27 17:01:34 +03003776 if (type != RDMA_NETDEV_IPOIB)
3777 return ERR_PTR(-EOPNOTSUPP);
3778
Niranjana Vishwanathapura8e959602017-06-30 13:14:46 -07003779 netdev = mlx5_rdma_netdev_alloc(to_mdev(hca)->mdev, hca,
3780 name, setup);
3781 if (likely(!IS_ERR_OR_NULL(netdev))) {
3782 rn = netdev_priv(netdev);
3783 rn->free_rdma_netdev = mlx5_ib_free_rdma_netdev;
3784 }
3785 return netdev;
Erez Shitrit693dfd52017-04-27 17:01:34 +03003786}
3787
Maor Gottliebfe248c32017-05-30 10:29:14 +03003788static void delay_drop_debugfs_cleanup(struct mlx5_ib_dev *dev)
3789{
3790 if (!dev->delay_drop.dbg)
3791 return;
3792 debugfs_remove_recursive(dev->delay_drop.dbg->dir_debugfs);
3793 kfree(dev->delay_drop.dbg);
3794 dev->delay_drop.dbg = NULL;
3795}
3796
Maor Gottlieb03404e82017-05-30 10:29:13 +03003797static void cancel_delay_drop(struct mlx5_ib_dev *dev)
3798{
3799 if (!(dev->ib_dev.attrs.raw_packet_caps & IB_RAW_PACKET_CAP_DELAY_DROP))
3800 return;
3801
3802 cancel_work_sync(&dev->delay_drop.delay_drop_work);
Maor Gottliebfe248c32017-05-30 10:29:14 +03003803 delay_drop_debugfs_cleanup(dev);
3804}
3805
3806static ssize_t delay_drop_timeout_read(struct file *filp, char __user *buf,
3807 size_t count, loff_t *pos)
3808{
3809 struct mlx5_ib_delay_drop *delay_drop = filp->private_data;
3810 char lbuf[20];
3811 int len;
3812
3813 len = snprintf(lbuf, sizeof(lbuf), "%u\n", delay_drop->timeout);
3814 return simple_read_from_buffer(buf, count, pos, lbuf, len);
3815}
3816
3817static ssize_t delay_drop_timeout_write(struct file *filp, const char __user *buf,
3818 size_t count, loff_t *pos)
3819{
3820 struct mlx5_ib_delay_drop *delay_drop = filp->private_data;
3821 u32 timeout;
3822 u32 var;
3823
3824 if (kstrtouint_from_user(buf, count, 0, &var))
3825 return -EFAULT;
3826
3827 timeout = min_t(u32, roundup(var, 100), MLX5_MAX_DELAY_DROP_TIMEOUT_MS *
3828 1000);
3829 if (timeout != var)
3830 mlx5_ib_dbg(delay_drop->dev, "Round delay drop timeout to %u usec\n",
3831 timeout);
3832
3833 delay_drop->timeout = timeout;
3834
3835 return count;
3836}
3837
3838static const struct file_operations fops_delay_drop_timeout = {
3839 .owner = THIS_MODULE,
3840 .open = simple_open,
3841 .write = delay_drop_timeout_write,
3842 .read = delay_drop_timeout_read,
3843};
3844
3845static int delay_drop_debugfs_init(struct mlx5_ib_dev *dev)
3846{
3847 struct mlx5_ib_dbg_delay_drop *dbg;
3848
3849 if (!mlx5_debugfs_root)
3850 return 0;
3851
3852 dbg = kzalloc(sizeof(*dbg), GFP_KERNEL);
3853 if (!dbg)
3854 return -ENOMEM;
3855
Sudip Mukherjeecbafad82017-09-18 12:28:48 +01003856 dev->delay_drop.dbg = dbg;
3857
Maor Gottliebfe248c32017-05-30 10:29:14 +03003858 dbg->dir_debugfs =
3859 debugfs_create_dir("delay_drop",
3860 dev->mdev->priv.dbg_root);
3861 if (!dbg->dir_debugfs)
Sudip Mukherjeecbafad82017-09-18 12:28:48 +01003862 goto out_debugfs;
Maor Gottliebfe248c32017-05-30 10:29:14 +03003863
3864 dbg->events_cnt_debugfs =
3865 debugfs_create_atomic_t("num_timeout_events", 0400,
3866 dbg->dir_debugfs,
3867 &dev->delay_drop.events_cnt);
3868 if (!dbg->events_cnt_debugfs)
3869 goto out_debugfs;
3870
3871 dbg->rqs_cnt_debugfs =
3872 debugfs_create_atomic_t("num_rqs", 0400,
3873 dbg->dir_debugfs,
3874 &dev->delay_drop.rqs_cnt);
3875 if (!dbg->rqs_cnt_debugfs)
3876 goto out_debugfs;
3877
3878 dbg->timeout_debugfs =
3879 debugfs_create_file("timeout", 0600,
3880 dbg->dir_debugfs,
3881 &dev->delay_drop,
3882 &fops_delay_drop_timeout);
3883 if (!dbg->timeout_debugfs)
3884 goto out_debugfs;
3885
3886 return 0;
3887
3888out_debugfs:
3889 delay_drop_debugfs_cleanup(dev);
3890 return -ENOMEM;
Maor Gottlieb03404e82017-05-30 10:29:13 +03003891}
3892
3893static void init_delay_drop(struct mlx5_ib_dev *dev)
3894{
3895 if (!(dev->ib_dev.attrs.raw_packet_caps & IB_RAW_PACKET_CAP_DELAY_DROP))
3896 return;
3897
3898 mutex_init(&dev->delay_drop.lock);
3899 dev->delay_drop.dev = dev;
3900 dev->delay_drop.activate = false;
3901 dev->delay_drop.timeout = MLX5_MAX_DELAY_DROP_TIMEOUT_MS * 1000;
3902 INIT_WORK(&dev->delay_drop.delay_drop_work, delay_drop_handler);
Maor Gottliebfe248c32017-05-30 10:29:14 +03003903 atomic_set(&dev->delay_drop.rqs_cnt, 0);
3904 atomic_set(&dev->delay_drop.events_cnt, 0);
3905
3906 if (delay_drop_debugfs_init(dev))
3907 mlx5_ib_warn(dev, "Failed to init delay drop debugfs\n");
Maor Gottlieb03404e82017-05-30 10:29:13 +03003908}
3909
Leon Romanovsky84305d712017-08-17 15:50:53 +03003910static const struct cpumask *
3911mlx5_ib_get_vector_affinity(struct ib_device *ibdev, int comp_vector)
Sagi Grimberg40b24402017-07-13 11:09:42 +03003912{
3913 struct mlx5_ib_dev *dev = to_mdev(ibdev);
3914
3915 return mlx5_get_vector_affinity(dev->mdev, comp_vector);
3916}
3917
Jack Morgenstein9603b612014-07-28 23:30:22 +03003918static void *mlx5_ib_add(struct mlx5_core_dev *mdev)
Eli Cohene126ba92013-07-07 17:25:49 +03003919{
Eli Cohene126ba92013-07-07 17:25:49 +03003920 struct mlx5_ib_dev *dev;
Achiad Shochatebd61f62015-12-23 18:47:16 +02003921 enum rdma_link_layer ll;
3922 int port_type_cap;
Aviv Heller4babcf92016-09-18 20:48:03 +03003923 const char *name;
Eli Cohene126ba92013-07-07 17:25:49 +03003924 int err;
3925 int i;
3926
Achiad Shochatebd61f62015-12-23 18:47:16 +02003927 port_type_cap = MLX5_CAP_GEN(mdev, port_type);
3928 ll = mlx5_port_type_cap_to_rdma_ll(port_type_cap);
3929
Eli Cohene126ba92013-07-07 17:25:49 +03003930 printk_once(KERN_INFO "%s", mlx5_version);
3931
3932 dev = (struct mlx5_ib_dev *)ib_alloc_device(sizeof(*dev));
3933 if (!dev)
Jack Morgenstein9603b612014-07-28 23:30:22 +03003934 return NULL;
Eli Cohene126ba92013-07-07 17:25:49 +03003935
Jack Morgenstein9603b612014-07-28 23:30:22 +03003936 dev->mdev = mdev;
Eli Cohene126ba92013-07-07 17:25:49 +03003937
Mark Bloch0837e862016-06-17 15:10:55 +03003938 dev->port = kcalloc(MLX5_CAP_GEN(mdev, num_ports), sizeof(*dev->port),
3939 GFP_KERNEL);
3940 if (!dev->port)
3941 goto err_dealloc;
3942
Achiad Shochatfc24fc52015-12-23 18:47:17 +02003943 rwlock_init(&dev->roce.netdev_lock);
Eli Cohene126ba92013-07-07 17:25:49 +03003944 err = get_port_caps(dev);
3945 if (err)
Mark Bloch0837e862016-06-17 15:10:55 +03003946 goto err_free_port;
Eli Cohene126ba92013-07-07 17:25:49 +03003947
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03003948 if (mlx5_use_mad_ifc(dev))
3949 get_ext_port_caps(dev);
Eli Cohene126ba92013-07-07 17:25:49 +03003950
Aviv Heller4babcf92016-09-18 20:48:03 +03003951 if (!mlx5_lag_is_active(mdev))
3952 name = "mlx5_%d";
3953 else
3954 name = "mlx5_bond_%d";
3955
3956 strlcpy(dev->ib_dev.name, name, IB_DEVICE_NAME_MAX);
Eli Cohene126ba92013-07-07 17:25:49 +03003957 dev->ib_dev.owner = THIS_MODULE;
3958 dev->ib_dev.node_type = RDMA_NODE_IB_CA;
Sagi Grimbergc6790aa2015-09-24 10:34:23 +03003959 dev->ib_dev.local_dma_lkey = 0 /* not supported for now */;
Saeed Mahameed938fe832015-05-28 22:28:41 +03003960 dev->num_ports = MLX5_CAP_GEN(mdev, num_ports);
Eli Cohene126ba92013-07-07 17:25:49 +03003961 dev->ib_dev.phys_port_cnt = dev->num_ports;
Saeed Mahameed233d05d2015-04-02 17:07:32 +03003962 dev->ib_dev.num_comp_vectors =
3963 dev->mdev->priv.eq_table.num_comp_vectors;
Bart Van Assche9b0c2892017-01-20 13:04:21 -08003964 dev->ib_dev.dev.parent = &mdev->pdev->dev;
Eli Cohene126ba92013-07-07 17:25:49 +03003965
3966 dev->ib_dev.uverbs_abi_ver = MLX5_IB_UVERBS_ABI_VERSION;
3967 dev->ib_dev.uverbs_cmd_mask =
3968 (1ull << IB_USER_VERBS_CMD_GET_CONTEXT) |
3969 (1ull << IB_USER_VERBS_CMD_QUERY_DEVICE) |
3970 (1ull << IB_USER_VERBS_CMD_QUERY_PORT) |
3971 (1ull << IB_USER_VERBS_CMD_ALLOC_PD) |
3972 (1ull << IB_USER_VERBS_CMD_DEALLOC_PD) |
Moni Shoua41c450f2016-11-23 08:23:26 +02003973 (1ull << IB_USER_VERBS_CMD_CREATE_AH) |
3974 (1ull << IB_USER_VERBS_CMD_DESTROY_AH) |
Eli Cohene126ba92013-07-07 17:25:49 +03003975 (1ull << IB_USER_VERBS_CMD_REG_MR) |
Noa Osherovich56e11d62016-02-29 16:46:51 +02003976 (1ull << IB_USER_VERBS_CMD_REREG_MR) |
Eli Cohene126ba92013-07-07 17:25:49 +03003977 (1ull << IB_USER_VERBS_CMD_DEREG_MR) |
3978 (1ull << IB_USER_VERBS_CMD_CREATE_COMP_CHANNEL) |
3979 (1ull << IB_USER_VERBS_CMD_CREATE_CQ) |
3980 (1ull << IB_USER_VERBS_CMD_RESIZE_CQ) |
3981 (1ull << IB_USER_VERBS_CMD_DESTROY_CQ) |
3982 (1ull << IB_USER_VERBS_CMD_CREATE_QP) |
3983 (1ull << IB_USER_VERBS_CMD_MODIFY_QP) |
3984 (1ull << IB_USER_VERBS_CMD_QUERY_QP) |
3985 (1ull << IB_USER_VERBS_CMD_DESTROY_QP) |
3986 (1ull << IB_USER_VERBS_CMD_ATTACH_MCAST) |
3987 (1ull << IB_USER_VERBS_CMD_DETACH_MCAST) |
3988 (1ull << IB_USER_VERBS_CMD_CREATE_SRQ) |
3989 (1ull << IB_USER_VERBS_CMD_MODIFY_SRQ) |
3990 (1ull << IB_USER_VERBS_CMD_QUERY_SRQ) |
3991 (1ull << IB_USER_VERBS_CMD_DESTROY_SRQ) |
3992 (1ull << IB_USER_VERBS_CMD_CREATE_XSRQ) |
3993 (1ull << IB_USER_VERBS_CMD_OPEN_QP);
Haggai Eran1707cb42015-02-08 13:28:52 +02003994 dev->ib_dev.uverbs_ex_cmd_mask =
Matan Barakd4584dd2016-01-28 17:51:46 +02003995 (1ull << IB_USER_VERBS_EX_CMD_QUERY_DEVICE) |
3996 (1ull << IB_USER_VERBS_EX_CMD_CREATE_CQ) |
Bodong Wang7d29f342016-12-01 13:43:16 +02003997 (1ull << IB_USER_VERBS_EX_CMD_CREATE_QP) |
3998 (1ull << IB_USER_VERBS_EX_CMD_MODIFY_QP);
Eli Cohene126ba92013-07-07 17:25:49 +03003999
4000 dev->ib_dev.query_device = mlx5_ib_query_device;
4001 dev->ib_dev.query_port = mlx5_ib_query_port;
Achiad Shochatebd61f62015-12-23 18:47:16 +02004002 dev->ib_dev.get_link_layer = mlx5_ib_port_link_layer;
Achiad Shochatfc24fc52015-12-23 18:47:17 +02004003 if (ll == IB_LINK_LAYER_ETHERNET)
4004 dev->ib_dev.get_netdev = mlx5_ib_get_netdev;
Eli Cohene126ba92013-07-07 17:25:49 +03004005 dev->ib_dev.query_gid = mlx5_ib_query_gid;
Achiad Shochat3cca2602015-12-23 18:47:23 +02004006 dev->ib_dev.add_gid = mlx5_ib_add_gid;
4007 dev->ib_dev.del_gid = mlx5_ib_del_gid;
Eli Cohene126ba92013-07-07 17:25:49 +03004008 dev->ib_dev.query_pkey = mlx5_ib_query_pkey;
4009 dev->ib_dev.modify_device = mlx5_ib_modify_device;
4010 dev->ib_dev.modify_port = mlx5_ib_modify_port;
4011 dev->ib_dev.alloc_ucontext = mlx5_ib_alloc_ucontext;
4012 dev->ib_dev.dealloc_ucontext = mlx5_ib_dealloc_ucontext;
4013 dev->ib_dev.mmap = mlx5_ib_mmap;
4014 dev->ib_dev.alloc_pd = mlx5_ib_alloc_pd;
4015 dev->ib_dev.dealloc_pd = mlx5_ib_dealloc_pd;
4016 dev->ib_dev.create_ah = mlx5_ib_create_ah;
4017 dev->ib_dev.query_ah = mlx5_ib_query_ah;
4018 dev->ib_dev.destroy_ah = mlx5_ib_destroy_ah;
4019 dev->ib_dev.create_srq = mlx5_ib_create_srq;
4020 dev->ib_dev.modify_srq = mlx5_ib_modify_srq;
4021 dev->ib_dev.query_srq = mlx5_ib_query_srq;
4022 dev->ib_dev.destroy_srq = mlx5_ib_destroy_srq;
4023 dev->ib_dev.post_srq_recv = mlx5_ib_post_srq_recv;
4024 dev->ib_dev.create_qp = mlx5_ib_create_qp;
4025 dev->ib_dev.modify_qp = mlx5_ib_modify_qp;
4026 dev->ib_dev.query_qp = mlx5_ib_query_qp;
4027 dev->ib_dev.destroy_qp = mlx5_ib_destroy_qp;
4028 dev->ib_dev.post_send = mlx5_ib_post_send;
4029 dev->ib_dev.post_recv = mlx5_ib_post_recv;
4030 dev->ib_dev.create_cq = mlx5_ib_create_cq;
4031 dev->ib_dev.modify_cq = mlx5_ib_modify_cq;
4032 dev->ib_dev.resize_cq = mlx5_ib_resize_cq;
4033 dev->ib_dev.destroy_cq = mlx5_ib_destroy_cq;
4034 dev->ib_dev.poll_cq = mlx5_ib_poll_cq;
4035 dev->ib_dev.req_notify_cq = mlx5_ib_arm_cq;
4036 dev->ib_dev.get_dma_mr = mlx5_ib_get_dma_mr;
4037 dev->ib_dev.reg_user_mr = mlx5_ib_reg_user_mr;
Noa Osherovich56e11d62016-02-29 16:46:51 +02004038 dev->ib_dev.rereg_user_mr = mlx5_ib_rereg_user_mr;
Eli Cohene126ba92013-07-07 17:25:49 +03004039 dev->ib_dev.dereg_mr = mlx5_ib_dereg_mr;
4040 dev->ib_dev.attach_mcast = mlx5_ib_mcg_attach;
4041 dev->ib_dev.detach_mcast = mlx5_ib_mcg_detach;
4042 dev->ib_dev.process_mad = mlx5_ib_process_mad;
Sagi Grimberg9bee1782015-07-30 10:32:35 +03004043 dev->ib_dev.alloc_mr = mlx5_ib_alloc_mr;
Sagi Grimberg8a187ee2015-10-13 19:11:26 +03004044 dev->ib_dev.map_mr_sg = mlx5_ib_map_mr_sg;
Sagi Grimbergd5436ba2014-02-23 14:19:12 +02004045 dev->ib_dev.check_mr_status = mlx5_ib_check_mr_status;
Ira Weiny77386132015-05-13 20:02:58 -04004046 dev->ib_dev.get_port_immutable = mlx5_port_immutable;
Ira Weinyc7342822016-06-15 02:22:01 -04004047 dev->ib_dev.get_dev_fw_str = get_dev_fw_str;
Sagi Grimberg40b24402017-07-13 11:09:42 +03004048 dev->ib_dev.get_vector_affinity = mlx5_ib_get_vector_affinity;
Niranjana Vishwanathapura8e959602017-06-30 13:14:46 -07004049 if (MLX5_CAP_GEN(mdev, ipoib_enhanced_offloads))
Alex Vesker022d0382017-06-14 09:59:06 +03004050 dev->ib_dev.alloc_rdma_netdev = mlx5_ib_alloc_rdma_netdev;
Niranjana Vishwanathapura8e959602017-06-30 13:14:46 -07004051
Eli Coheneff901d2016-03-11 22:58:42 +02004052 if (mlx5_core_is_pf(mdev)) {
4053 dev->ib_dev.get_vf_config = mlx5_ib_get_vf_config;
4054 dev->ib_dev.set_vf_link_state = mlx5_ib_set_vf_link_state;
4055 dev->ib_dev.get_vf_stats = mlx5_ib_get_vf_stats;
4056 dev->ib_dev.set_vf_guid = mlx5_ib_set_vf_guid;
4057 }
Eli Cohene126ba92013-07-07 17:25:49 +03004058
Maor Gottlieb7c2344c2016-06-17 14:56:44 +03004059 dev->ib_dev.disassociate_ucontext = mlx5_ib_disassociate_ucontext;
4060
Saeed Mahameed938fe832015-05-28 22:28:41 +03004061 mlx5_ib_internal_fill_odp_caps(dev);
Haggai Eran8cdd3122014-12-11 17:04:20 +02004062
Max Gurtovoy6e8484c2017-05-28 10:53:11 +03004063 dev->umr_fence = mlx5_get_umr_fence(MLX5_CAP_GEN(mdev, umr_fence));
4064
Matan Barakd2370e02016-02-29 18:05:30 +02004065 if (MLX5_CAP_GEN(mdev, imaicl)) {
4066 dev->ib_dev.alloc_mw = mlx5_ib_alloc_mw;
4067 dev->ib_dev.dealloc_mw = mlx5_ib_dealloc_mw;
4068 dev->ib_dev.uverbs_cmd_mask |=
4069 (1ull << IB_USER_VERBS_CMD_ALLOC_MW) |
4070 (1ull << IB_USER_VERBS_CMD_DEALLOC_MW);
4071 }
4072
Kamal Heib7c16f472017-01-18 15:25:09 +02004073 if (MLX5_CAP_GEN(dev->mdev, max_qp_cnt)) {
Mark Bloch0ad17a82016-06-17 15:10:56 +03004074 dev->ib_dev.get_hw_stats = mlx5_ib_get_hw_stats;
4075 dev->ib_dev.alloc_hw_stats = mlx5_ib_alloc_hw_stats;
4076 }
4077
Saeed Mahameed938fe832015-05-28 22:28:41 +03004078 if (MLX5_CAP_GEN(mdev, xrc)) {
Eli Cohene126ba92013-07-07 17:25:49 +03004079 dev->ib_dev.alloc_xrcd = mlx5_ib_alloc_xrcd;
4080 dev->ib_dev.dealloc_xrcd = mlx5_ib_dealloc_xrcd;
4081 dev->ib_dev.uverbs_cmd_mask |=
4082 (1ull << IB_USER_VERBS_CMD_OPEN_XRCD) |
4083 (1ull << IB_USER_VERBS_CMD_CLOSE_XRCD);
4084 }
4085
Yishai Hadas81e30882017-06-08 16:15:09 +03004086 dev->ib_dev.create_flow = mlx5_ib_create_flow;
4087 dev->ib_dev.destroy_flow = mlx5_ib_destroy_flow;
4088 dev->ib_dev.uverbs_ex_cmd_mask |=
4089 (1ull << IB_USER_VERBS_EX_CMD_CREATE_FLOW) |
4090 (1ull << IB_USER_VERBS_EX_CMD_DESTROY_FLOW);
4091
Linus Torvalds048ccca2016-01-23 18:45:06 -08004092 if (mlx5_ib_port_link_layer(&dev->ib_dev, 1) ==
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02004093 IB_LINK_LAYER_ETHERNET) {
Yishai Hadas79b20a62016-05-23 15:20:50 +03004094 dev->ib_dev.create_wq = mlx5_ib_create_wq;
4095 dev->ib_dev.modify_wq = mlx5_ib_modify_wq;
4096 dev->ib_dev.destroy_wq = mlx5_ib_destroy_wq;
Yishai Hadasc5f90922016-05-23 15:20:53 +03004097 dev->ib_dev.create_rwq_ind_table = mlx5_ib_create_rwq_ind_table;
4098 dev->ib_dev.destroy_rwq_ind_table = mlx5_ib_destroy_rwq_ind_table;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02004099 dev->ib_dev.uverbs_ex_cmd_mask |=
Yishai Hadas79b20a62016-05-23 15:20:50 +03004100 (1ull << IB_USER_VERBS_EX_CMD_CREATE_WQ) |
4101 (1ull << IB_USER_VERBS_EX_CMD_MODIFY_WQ) |
Yishai Hadasc5f90922016-05-23 15:20:53 +03004102 (1ull << IB_USER_VERBS_EX_CMD_DESTROY_WQ) |
4103 (1ull << IB_USER_VERBS_EX_CMD_CREATE_RWQ_IND_TBL) |
4104 (1ull << IB_USER_VERBS_EX_CMD_DESTROY_RWQ_IND_TBL);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02004105 }
Eli Cohene126ba92013-07-07 17:25:49 +03004106 err = init_node_data(dev);
4107 if (err)
Majd Dibbiny90be7c82016-10-27 16:36:39 +03004108 goto err_free_port;
Eli Cohene126ba92013-07-07 17:25:49 +03004109
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02004110 mutex_init(&dev->flow_db.lock);
Eli Cohene126ba92013-07-07 17:25:49 +03004111 mutex_init(&dev->cap_mask_mutex);
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03004112 INIT_LIST_HEAD(&dev->qp_list);
4113 spin_lock_init(&dev->reset_flow_resource_lock);
Eli Cohene126ba92013-07-07 17:25:49 +03004114
Achiad Shochatfc24fc52015-12-23 18:47:17 +02004115 if (ll == IB_LINK_LAYER_ETHERNET) {
Or Gerlitz45f95ac2016-11-27 16:51:35 +02004116 err = mlx5_enable_eth(dev);
Achiad Shochatfc24fc52015-12-23 18:47:17 +02004117 if (err)
Majd Dibbiny90be7c82016-10-27 16:36:39 +03004118 goto err_free_port;
Moni Shouafd65f1b2017-05-30 09:56:05 +03004119 dev->roce.last_port_state = IB_PORT_DOWN;
Achiad Shochatfc24fc52015-12-23 18:47:17 +02004120 }
4121
Eli Cohene126ba92013-07-07 17:25:49 +03004122 err = create_dev_resources(&dev->devr);
4123 if (err)
Or Gerlitz45f95ac2016-11-27 16:51:35 +02004124 goto err_disable_eth;
Eli Cohene126ba92013-07-07 17:25:49 +03004125
Haggai Eran6aec21f2014-12-11 17:04:23 +02004126 err = mlx5_ib_odp_init_one(dev);
Wei Yongjun281d1a92013-07-30 07:54:26 +08004127 if (err)
Eli Cohene126ba92013-07-07 17:25:49 +03004128 goto err_rsrc;
4129
Kamal Heib45bded22017-01-18 14:10:32 +02004130 if (MLX5_CAP_GEN(dev->mdev, max_qp_cnt)) {
Parav Pandite1f24a72017-04-16 07:29:29 +03004131 err = mlx5_ib_alloc_counters(dev);
Kamal Heib45bded22017-01-18 14:10:32 +02004132 if (err)
4133 goto err_odp;
4134 }
Haggai Eran6aec21f2014-12-11 17:04:23 +02004135
Parav Pandit4a2da0b2017-05-30 10:05:15 +03004136 err = mlx5_ib_init_cong_debugfs(dev);
4137 if (err)
4138 goto err_cnt;
4139
Eli Cohen5fe9dec2017-01-03 23:55:25 +02004140 dev->mdev->priv.uar = mlx5_get_uars_page(dev->mdev);
4141 if (!dev->mdev->priv.uar)
Parav Pandit4a2da0b2017-05-30 10:05:15 +03004142 goto err_cong;
Eli Cohen5fe9dec2017-01-03 23:55:25 +02004143
4144 err = mlx5_alloc_bfreg(dev->mdev, &dev->bfreg, false, false);
4145 if (err)
4146 goto err_uar_page;
4147
4148 err = mlx5_alloc_bfreg(dev->mdev, &dev->fp_bfreg, false, true);
4149 if (err)
4150 goto err_bfreg;
4151
Mark Bloch0837e862016-06-17 15:10:55 +03004152 err = ib_register_device(&dev->ib_dev, NULL);
4153 if (err)
Eli Cohen5fe9dec2017-01-03 23:55:25 +02004154 goto err_fp_bfreg;
Mark Bloch0837e862016-06-17 15:10:55 +03004155
Eli Cohene126ba92013-07-07 17:25:49 +03004156 err = create_umr_res(dev);
4157 if (err)
4158 goto err_dev;
4159
Maor Gottlieb03404e82017-05-30 10:29:13 +03004160 init_delay_drop(dev);
4161
Eli Cohene126ba92013-07-07 17:25:49 +03004162 for (i = 0; i < ARRAY_SIZE(mlx5_class_attributes); i++) {
Wei Yongjun281d1a92013-07-30 07:54:26 +08004163 err = device_create_file(&dev->ib_dev.dev,
4164 mlx5_class_attributes[i]);
4165 if (err)
Maor Gottlieb03404e82017-05-30 10:29:13 +03004166 goto err_delay_drop;
Eli Cohene126ba92013-07-07 17:25:49 +03004167 }
4168
Huy Nguyenc85023e2017-05-30 09:42:54 +03004169 if ((MLX5_CAP_GEN(mdev, port_type) == MLX5_CAP_PORT_TYPE_ETH) &&
4170 MLX5_CAP_GEN(mdev, disable_local_lb))
4171 mutex_init(&dev->lb_mutex);
4172
Eli Cohene126ba92013-07-07 17:25:49 +03004173 dev->ib_active = true;
4174
Jack Morgenstein9603b612014-07-28 23:30:22 +03004175 return dev;
Eli Cohene126ba92013-07-07 17:25:49 +03004176
Maor Gottlieb03404e82017-05-30 10:29:13 +03004177err_delay_drop:
4178 cancel_delay_drop(dev);
Eli Cohene126ba92013-07-07 17:25:49 +03004179 destroy_umrc_res(dev);
4180
4181err_dev:
4182 ib_unregister_device(&dev->ib_dev);
4183
Eli Cohen5fe9dec2017-01-03 23:55:25 +02004184err_fp_bfreg:
4185 mlx5_free_bfreg(dev->mdev, &dev->fp_bfreg);
4186
4187err_bfreg:
4188 mlx5_free_bfreg(dev->mdev, &dev->bfreg);
4189
4190err_uar_page:
4191 mlx5_put_uars_page(dev->mdev, dev->mdev->priv.uar);
4192
Parav Pandit4a2da0b2017-05-30 10:05:15 +03004193err_cong:
Parav Pandite19cd282017-10-01 09:54:35 +03004194 mlx5_ib_cleanup_cong_debugfs(dev);
4195err_cnt:
Kamal Heib45bded22017-01-18 14:10:32 +02004196 if (MLX5_CAP_GEN(dev->mdev, max_qp_cnt))
Parav Pandite1f24a72017-04-16 07:29:29 +03004197 mlx5_ib_dealloc_counters(dev);
Mark Bloch0837e862016-06-17 15:10:55 +03004198
Haggai Eran6aec21f2014-12-11 17:04:23 +02004199err_odp:
4200 mlx5_ib_odp_remove_one(dev);
4201
Eli Cohene126ba92013-07-07 17:25:49 +03004202err_rsrc:
4203 destroy_dev_resources(&dev->devr);
4204
Or Gerlitz45f95ac2016-11-27 16:51:35 +02004205err_disable_eth:
Aviv Heller5ec8c832016-09-18 20:48:00 +03004206 if (ll == IB_LINK_LAYER_ETHERNET) {
Or Gerlitz45f95ac2016-11-27 16:51:35 +02004207 mlx5_disable_eth(dev);
Or Gerlitzd012f5d2016-11-27 16:51:34 +02004208 mlx5_remove_netdev_notifier(dev);
Aviv Heller5ec8c832016-09-18 20:48:00 +03004209 }
Achiad Shochatfc24fc52015-12-23 18:47:17 +02004210
Mark Bloch0837e862016-06-17 15:10:55 +03004211err_free_port:
4212 kfree(dev->port);
4213
Jack Morgenstein9603b612014-07-28 23:30:22 +03004214err_dealloc:
Eli Cohene126ba92013-07-07 17:25:49 +03004215 ib_dealloc_device((struct ib_device *)dev);
4216
Jack Morgenstein9603b612014-07-28 23:30:22 +03004217 return NULL;
Eli Cohene126ba92013-07-07 17:25:49 +03004218}
4219
Jack Morgenstein9603b612014-07-28 23:30:22 +03004220static void mlx5_ib_remove(struct mlx5_core_dev *mdev, void *context)
Eli Cohene126ba92013-07-07 17:25:49 +03004221{
Jack Morgenstein9603b612014-07-28 23:30:22 +03004222 struct mlx5_ib_dev *dev = context;
Achiad Shochatfc24fc52015-12-23 18:47:17 +02004223 enum rdma_link_layer ll = mlx5_ib_port_link_layer(&dev->ib_dev, 1);
Haggai Eran6aec21f2014-12-11 17:04:23 +02004224
Maor Gottlieb03404e82017-05-30 10:29:13 +03004225 cancel_delay_drop(dev);
Or Gerlitzd012f5d2016-11-27 16:51:34 +02004226 mlx5_remove_netdev_notifier(dev);
Eli Cohene126ba92013-07-07 17:25:49 +03004227 ib_unregister_device(&dev->ib_dev);
Eli Cohen5fe9dec2017-01-03 23:55:25 +02004228 mlx5_free_bfreg(dev->mdev, &dev->fp_bfreg);
4229 mlx5_free_bfreg(dev->mdev, &dev->bfreg);
4230 mlx5_put_uars_page(dev->mdev, mdev->priv.uar);
Parav Pandit4a2da0b2017-05-30 10:05:15 +03004231 mlx5_ib_cleanup_cong_debugfs(dev);
Kamal Heib45bded22017-01-18 14:10:32 +02004232 if (MLX5_CAP_GEN(dev->mdev, max_qp_cnt))
Parav Pandite1f24a72017-04-16 07:29:29 +03004233 mlx5_ib_dealloc_counters(dev);
Eli Coheneefd56e2014-09-14 16:47:50 +03004234 destroy_umrc_res(dev);
Haggai Eran6aec21f2014-12-11 17:04:23 +02004235 mlx5_ib_odp_remove_one(dev);
Eli Cohene126ba92013-07-07 17:25:49 +03004236 destroy_dev_resources(&dev->devr);
Achiad Shochatfc24fc52015-12-23 18:47:17 +02004237 if (ll == IB_LINK_LAYER_ETHERNET)
Or Gerlitz45f95ac2016-11-27 16:51:35 +02004238 mlx5_disable_eth(dev);
Mark Bloch0837e862016-06-17 15:10:55 +03004239 kfree(dev->port);
Eli Cohene126ba92013-07-07 17:25:49 +03004240 ib_dealloc_device(&dev->ib_dev);
4241}
4242
Jack Morgenstein9603b612014-07-28 23:30:22 +03004243static struct mlx5_interface mlx5_ib_interface = {
4244 .add = mlx5_ib_add,
4245 .remove = mlx5_ib_remove,
4246 .event = mlx5_ib_event,
Artemy Kovalyovd9aaed82017-01-02 11:37:46 +02004247#ifdef CONFIG_INFINIBAND_ON_DEMAND_PAGING
4248 .pfault = mlx5_ib_pfault,
4249#endif
Saeed Mahameed64613d942015-04-02 17:07:34 +03004250 .protocol = MLX5_INTERFACE_PROTOCOL_IB,
Eli Cohene126ba92013-07-07 17:25:49 +03004251};
4252
4253static int __init mlx5_ib_init(void)
4254{
Haggai Eran6aec21f2014-12-11 17:04:23 +02004255 int err;
4256
Artemy Kovalyov81713d32017-01-18 16:58:11 +02004257 mlx5_ib_odp_init();
Jack Morgenstein9603b612014-07-28 23:30:22 +03004258
Haggai Eran6aec21f2014-12-11 17:04:23 +02004259 err = mlx5_register_interface(&mlx5_ib_interface);
Haggai Eran6aec21f2014-12-11 17:04:23 +02004260
4261 return err;
Eli Cohene126ba92013-07-07 17:25:49 +03004262}
4263
4264static void __exit mlx5_ib_cleanup(void)
4265{
Jack Morgenstein9603b612014-07-28 23:30:22 +03004266 mlx5_unregister_interface(&mlx5_ib_interface);
Eli Cohene126ba92013-07-07 17:25:49 +03004267}
4268
4269module_init(mlx5_ib_init);
4270module_exit(mlx5_ib_cleanup);