blob: d2aadb13637b7383c6d6782cef95c8f4b402a924 [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
Noa Osherovichb1383aa2017-10-29 13:59:45 +0200718 if (MLX5_CAP_GEN(mdev, end_pad))
719 props->device_cap_flags |= IB_DEVICE_PCI_WRITE_END_PADDING;
720
Majd Dibbiny1b5daf12015-06-04 19:30:46 +0300721 props->vendor_part_id = mdev->pdev->device;
722 props->hw_ver = mdev->pdev->revision;
Eli Cohene126ba92013-07-07 17:25:49 +0300723
724 props->max_mr_size = ~0ull;
Sagi Grimberge0238a62015-07-21 14:40:12 +0300725 props->page_size_cap = ~(min_page_size - 1);
Saeed Mahameed938fe832015-05-28 22:28:41 +0300726 props->max_qp = 1 << MLX5_CAP_GEN(mdev, log_max_qp);
727 props->max_qp_wr = 1 << MLX5_CAP_GEN(mdev, log_max_qp_sz);
728 max_rq_sg = MLX5_CAP_GEN(mdev, max_wqe_sz_rq) /
729 sizeof(struct mlx5_wqe_data_seg);
Eli Cohen288c01b2016-10-27 16:36:45 +0300730 max_sq_desc = min_t(int, MLX5_CAP_GEN(mdev, max_wqe_sz_sq), 512);
731 max_sq_sg = (max_sq_desc - sizeof(struct mlx5_wqe_ctrl_seg) -
732 sizeof(struct mlx5_wqe_raddr_seg)) /
733 sizeof(struct mlx5_wqe_data_seg);
Eli Cohene126ba92013-07-07 17:25:49 +0300734 props->max_sge = min(max_rq_sg, max_sq_sg);
Sagi Grimberg986ef952016-03-31 19:03:25 +0300735 props->max_sge_rd = MLX5_MAX_SGE_RD;
Saeed Mahameed938fe832015-05-28 22:28:41 +0300736 props->max_cq = 1 << MLX5_CAP_GEN(mdev, log_max_cq);
Leon Romanovsky9f177682016-01-14 08:11:40 +0200737 props->max_cqe = (1 << MLX5_CAP_GEN(mdev, log_max_cq_sz)) - 1;
Saeed Mahameed938fe832015-05-28 22:28:41 +0300738 props->max_mr = 1 << MLX5_CAP_GEN(mdev, log_max_mkey);
739 props->max_pd = 1 << MLX5_CAP_GEN(mdev, log_max_pd);
740 props->max_qp_rd_atom = 1 << MLX5_CAP_GEN(mdev, log_max_ra_req_qp);
741 props->max_qp_init_rd_atom = 1 << MLX5_CAP_GEN(mdev, log_max_ra_res_qp);
742 props->max_srq = 1 << MLX5_CAP_GEN(mdev, log_max_srq);
743 props->max_srq_wr = (1 << MLX5_CAP_GEN(mdev, log_max_srq_sz)) - 1;
744 props->local_ca_ack_delay = MLX5_CAP_GEN(mdev, local_ca_ack_delay);
Eli Cohene126ba92013-07-07 17:25:49 +0300745 props->max_res_rd_atom = props->max_qp_rd_atom * props->max_qp;
Eli Cohene126ba92013-07-07 17:25:49 +0300746 props->max_srq_sge = max_rq_sg - 1;
Sagi Grimberg911f4332016-03-03 13:37:51 +0200747 props->max_fast_reg_page_list_len =
748 1 << MLX5_CAP_GEN(mdev, log_max_klm_list_size);
Eran Ben Elishada7525d2015-12-14 16:34:10 +0200749 get_atomic_caps(dev, props);
Eli Cohen81bea282013-09-11 16:35:30 +0300750 props->masked_atomic_cap = IB_ATOMIC_NONE;
Saeed Mahameed938fe832015-05-28 22:28:41 +0300751 props->max_mcast_grp = 1 << MLX5_CAP_GEN(mdev, log_max_mcg);
752 props->max_mcast_qp_attach = MLX5_CAP_GEN(mdev, max_qp_mcg);
Eli Cohene126ba92013-07-07 17:25:49 +0300753 props->max_total_mcast_qp_attach = props->max_mcast_qp_attach *
754 props->max_mcast_grp;
755 props->max_map_per_fmr = INT_MAX; /* no limit in ConnectIB */
Maor Gottlieb86695a62016-10-27 16:36:38 +0300756 props->max_ah = INT_MAX;
Matan Barak7c60bcb2015-12-15 20:30:11 +0200757 props->hca_core_clock = MLX5_CAP_GEN(mdev, device_frequency_khz);
758 props->timestamp_mask = 0x7FFFFFFFFFFFFFFFULL;
Eli Cohene126ba92013-07-07 17:25:49 +0300759
Haggai Eran8cdd3122014-12-11 17:04:20 +0200760#ifdef CONFIG_INFINIBAND_ON_DEMAND_PAGING
Saeed Mahameed938fe832015-05-28 22:28:41 +0300761 if (MLX5_CAP_GEN(mdev, pg))
Haggai Eran8cdd3122014-12-11 17:04:20 +0200762 props->device_cap_flags |= IB_DEVICE_ON_DEMAND_PAGING;
763 props->odp_caps = dev->odp_caps;
764#endif
765
Leon Romanovsky051f2632015-12-20 12:16:11 +0200766 if (MLX5_CAP_GEN(mdev, cd))
767 props->device_cap_flags |= IB_DEVICE_CROSS_CHANNEL;
768
Eli Coheneff901d2016-03-11 22:58:42 +0200769 if (!mlx5_core_is_pf(mdev))
770 props->device_cap_flags |= IB_DEVICE_VIRTUAL_FUNCTION;
771
Yishai Hadas31f69a82016-08-28 11:28:45 +0300772 if (mlx5_ib_port_link_layer(ibdev, 1) ==
773 IB_LINK_LAYER_ETHERNET) {
774 props->rss_caps.max_rwq_indirection_tables =
775 1 << MLX5_CAP_GEN(dev->mdev, log_max_rqt);
776 props->rss_caps.max_rwq_indirection_table_size =
777 1 << MLX5_CAP_GEN(dev->mdev, log_max_rqt_size);
778 props->rss_caps.supported_qpts = 1 << IB_QPT_RAW_PACKET;
779 props->max_wq_type_rq =
780 1 << MLX5_CAP_GEN(dev->mdev, log_max_rq);
781 }
782
Artemy Kovalyoveb761892017-08-17 15:52:09 +0300783 if (MLX5_CAP_GEN(mdev, tag_matching)) {
Leon Romanovsky78b1beb2017-09-24 21:46:29 +0300784 props->tm_caps.max_rndv_hdr_size = MLX5_TM_MAX_RNDV_MSG_SIZE;
785 props->tm_caps.max_num_tags =
Artemy Kovalyoveb761892017-08-17 15:52:09 +0300786 (1 << MLX5_CAP_GEN(mdev, log_tag_matching_list_sz)) - 1;
Leon Romanovsky78b1beb2017-09-24 21:46:29 +0300787 props->tm_caps.flags = IB_TM_CAP_RC;
788 props->tm_caps.max_ops =
Artemy Kovalyoveb761892017-08-17 15:52:09 +0300789 1 << MLX5_CAP_GEN(mdev, log_max_qp_sz);
Leon Romanovsky78b1beb2017-09-24 21:46:29 +0300790 props->tm_caps.max_sge = MLX5_TM_MAX_SGE;
Artemy Kovalyoveb761892017-08-17 15:52:09 +0300791 }
792
Bodong Wang7e43a2a2016-10-31 12:16:44 +0200793 if (field_avail(typeof(resp), cqe_comp_caps, uhw->outlen)) {
794 resp.cqe_comp_caps.max_num =
795 MLX5_CAP_GEN(dev->mdev, cqe_compression) ?
796 MLX5_CAP_GEN(dev->mdev, cqe_compression_max_num) : 0;
797 resp.cqe_comp_caps.supported_format =
798 MLX5_IB_CQE_RES_FORMAT_HASH |
799 MLX5_IB_CQE_RES_FORMAT_CSUM;
800 resp.response_length += sizeof(resp.cqe_comp_caps);
801 }
802
Bodong Wangd9491672016-12-01 13:43:13 +0200803 if (field_avail(typeof(resp), packet_pacing_caps, uhw->outlen)) {
804 if (MLX5_CAP_QOS(mdev, packet_pacing) &&
805 MLX5_CAP_GEN(mdev, qos)) {
806 resp.packet_pacing_caps.qp_rate_limit_max =
807 MLX5_CAP_QOS(mdev, packet_pacing_max_rate);
808 resp.packet_pacing_caps.qp_rate_limit_min =
809 MLX5_CAP_QOS(mdev, packet_pacing_min_rate);
810 resp.packet_pacing_caps.supported_qpts |=
811 1 << IB_QPT_RAW_PACKET;
812 }
813 resp.response_length += sizeof(resp.packet_pacing_caps);
814 }
815
Leon Romanovsky9f885202017-01-02 11:37:39 +0200816 if (field_avail(typeof(resp), mlx5_ib_support_multi_pkt_send_wqes,
817 uhw->outlen)) {
Bodong Wang795b6092017-08-17 15:52:34 +0300818 if (MLX5_CAP_ETH(mdev, multi_pkt_send_wqe))
819 resp.mlx5_ib_support_multi_pkt_send_wqes =
820 MLX5_IB_ALLOW_MPW;
Bodong Wang050da902017-08-17 15:52:35 +0300821
822 if (MLX5_CAP_ETH(mdev, enhanced_multi_pkt_send_wqe))
823 resp.mlx5_ib_support_multi_pkt_send_wqes |=
824 MLX5_IB_SUPPORT_EMPW;
825
Leon Romanovsky9f885202017-01-02 11:37:39 +0200826 resp.response_length +=
827 sizeof(resp.mlx5_ib_support_multi_pkt_send_wqes);
828 }
829
Guy Levide57f2a2017-10-19 08:25:52 +0300830 if (field_avail(typeof(resp), flags, uhw->outlen)) {
831 resp.response_length += sizeof(resp.flags);
Guy Levi7a0c8f42017-10-19 08:25:53 +0300832
Guy Levide57f2a2017-10-19 08:25:52 +0300833 if (MLX5_CAP_GEN(mdev, cqe_compression_128))
834 resp.flags |=
835 MLX5_IB_QUERY_DEV_RESP_FLAGS_CQE_128B_COMP;
Guy Levi7a0c8f42017-10-19 08:25:53 +0300836
837 if (MLX5_CAP_GEN(mdev, cqe_128_always))
838 resp.flags |= MLX5_IB_QUERY_DEV_RESP_FLAGS_CQE_128B_PAD;
Guy Levide57f2a2017-10-19 08:25:52 +0300839 }
Leon Romanovsky9f885202017-01-02 11:37:39 +0200840
Noa Osherovich96dc3fc2017-08-17 15:52:28 +0300841 if (field_avail(typeof(resp), sw_parsing_caps,
842 uhw->outlen)) {
843 resp.response_length += sizeof(resp.sw_parsing_caps);
844 if (MLX5_CAP_ETH(mdev, swp)) {
845 resp.sw_parsing_caps.sw_parsing_offloads |=
846 MLX5_IB_SW_PARSING;
847
848 if (MLX5_CAP_ETH(mdev, swp_csum))
849 resp.sw_parsing_caps.sw_parsing_offloads |=
850 MLX5_IB_SW_PARSING_CSUM;
851
852 if (MLX5_CAP_ETH(mdev, swp_lso))
853 resp.sw_parsing_caps.sw_parsing_offloads |=
854 MLX5_IB_SW_PARSING_LSO;
855
856 if (resp.sw_parsing_caps.sw_parsing_offloads)
857 resp.sw_parsing_caps.supported_qpts =
858 BIT(IB_QPT_RAW_PACKET);
859 }
860 }
861
Noa Osherovichb4f34592017-10-17 18:01:12 +0300862 if (field_avail(typeof(resp), striding_rq_caps, uhw->outlen)) {
863 resp.response_length += sizeof(resp.striding_rq_caps);
864 if (MLX5_CAP_GEN(mdev, striding_rq)) {
865 resp.striding_rq_caps.min_single_stride_log_num_of_bytes =
866 MLX5_MIN_SINGLE_STRIDE_LOG_NUM_BYTES;
867 resp.striding_rq_caps.max_single_stride_log_num_of_bytes =
868 MLX5_MAX_SINGLE_STRIDE_LOG_NUM_BYTES;
869 resp.striding_rq_caps.min_single_wqe_log_num_of_strides =
870 MLX5_MIN_SINGLE_WQE_LOG_NUM_STRIDES;
871 resp.striding_rq_caps.max_single_wqe_log_num_of_strides =
872 MLX5_MAX_SINGLE_WQE_LOG_NUM_STRIDES;
873 resp.striding_rq_caps.supported_qpts =
874 BIT(IB_QPT_RAW_PACKET);
875 }
876 }
877
Maor Gottliebf95ef6c2017-10-19 08:25:55 +0300878 if (field_avail(typeof(resp), tunnel_offloads_caps,
879 uhw->outlen)) {
880 resp.response_length += sizeof(resp.tunnel_offloads_caps);
881 if (MLX5_CAP_ETH(mdev, tunnel_stateless_vxlan))
882 resp.tunnel_offloads_caps |=
883 MLX5_IB_TUNNELED_OFFLOADS_VXLAN;
884 if (MLX5_CAP_ETH(mdev, tunnel_stateless_geneve_rx))
885 resp.tunnel_offloads_caps |=
886 MLX5_IB_TUNNELED_OFFLOADS_GENEVE;
887 if (MLX5_CAP_ETH(mdev, tunnel_stateless_gre))
888 resp.tunnel_offloads_caps |=
889 MLX5_IB_TUNNELED_OFFLOADS_GRE;
890 }
891
Bodong Wang402ca532016-06-17 15:02:20 +0300892 if (uhw->outlen) {
893 err = ib_copy_to_udata(uhw, &resp, resp.response_length);
894
895 if (err)
896 return err;
897 }
898
Majd Dibbiny1b5daf12015-06-04 19:30:46 +0300899 return 0;
900}
Eli Cohene126ba92013-07-07 17:25:49 +0300901
Majd Dibbiny1b5daf12015-06-04 19:30:46 +0300902enum mlx5_ib_width {
903 MLX5_IB_WIDTH_1X = 1 << 0,
904 MLX5_IB_WIDTH_2X = 1 << 1,
905 MLX5_IB_WIDTH_4X = 1 << 2,
906 MLX5_IB_WIDTH_8X = 1 << 3,
907 MLX5_IB_WIDTH_12X = 1 << 4
908};
909
910static int translate_active_width(struct ib_device *ibdev, u8 active_width,
911 u8 *ib_width)
912{
913 struct mlx5_ib_dev *dev = to_mdev(ibdev);
914 int err = 0;
915
916 if (active_width & MLX5_IB_WIDTH_1X) {
917 *ib_width = IB_WIDTH_1X;
918 } else if (active_width & MLX5_IB_WIDTH_2X) {
919 mlx5_ib_dbg(dev, "active_width %d is not supported by IB spec\n",
920 (int)active_width);
921 err = -EINVAL;
922 } else if (active_width & MLX5_IB_WIDTH_4X) {
923 *ib_width = IB_WIDTH_4X;
924 } else if (active_width & MLX5_IB_WIDTH_8X) {
925 *ib_width = IB_WIDTH_8X;
926 } else if (active_width & MLX5_IB_WIDTH_12X) {
927 *ib_width = IB_WIDTH_12X;
928 } else {
929 mlx5_ib_dbg(dev, "Invalid active_width %d\n",
930 (int)active_width);
931 err = -EINVAL;
932 }
933
934 return err;
935}
936
937static int mlx5_mtu_to_ib_mtu(int mtu)
938{
939 switch (mtu) {
940 case 256: return 1;
941 case 512: return 2;
942 case 1024: return 3;
943 case 2048: return 4;
944 case 4096: return 5;
945 default:
946 pr_warn("invalid mtu\n");
947 return -1;
948 }
949}
950
951enum ib_max_vl_num {
952 __IB_MAX_VL_0 = 1,
953 __IB_MAX_VL_0_1 = 2,
954 __IB_MAX_VL_0_3 = 3,
955 __IB_MAX_VL_0_7 = 4,
956 __IB_MAX_VL_0_14 = 5,
957};
958
959enum mlx5_vl_hw_cap {
960 MLX5_VL_HW_0 = 1,
961 MLX5_VL_HW_0_1 = 2,
962 MLX5_VL_HW_0_2 = 3,
963 MLX5_VL_HW_0_3 = 4,
964 MLX5_VL_HW_0_4 = 5,
965 MLX5_VL_HW_0_5 = 6,
966 MLX5_VL_HW_0_6 = 7,
967 MLX5_VL_HW_0_7 = 8,
968 MLX5_VL_HW_0_14 = 15
969};
970
971static int translate_max_vl_num(struct ib_device *ibdev, u8 vl_hw_cap,
972 u8 *max_vl_num)
973{
974 switch (vl_hw_cap) {
975 case MLX5_VL_HW_0:
976 *max_vl_num = __IB_MAX_VL_0;
977 break;
978 case MLX5_VL_HW_0_1:
979 *max_vl_num = __IB_MAX_VL_0_1;
980 break;
981 case MLX5_VL_HW_0_3:
982 *max_vl_num = __IB_MAX_VL_0_3;
983 break;
984 case MLX5_VL_HW_0_7:
985 *max_vl_num = __IB_MAX_VL_0_7;
986 break;
987 case MLX5_VL_HW_0_14:
988 *max_vl_num = __IB_MAX_VL_0_14;
989 break;
990
991 default:
992 return -EINVAL;
993 }
994
995 return 0;
996}
997
998static int mlx5_query_hca_port(struct ib_device *ibdev, u8 port,
999 struct ib_port_attr *props)
1000{
1001 struct mlx5_ib_dev *dev = to_mdev(ibdev);
1002 struct mlx5_core_dev *mdev = dev->mdev;
1003 struct mlx5_hca_vport_context *rep;
Saeed Mahameed046339e2016-04-22 00:33:03 +03001004 u16 max_mtu;
1005 u16 oper_mtu;
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03001006 int err;
1007 u8 ib_link_width_oper;
1008 u8 vl_hw_cap;
1009
1010 rep = kzalloc(sizeof(*rep), GFP_KERNEL);
1011 if (!rep) {
1012 err = -ENOMEM;
1013 goto out;
1014 }
1015
Or Gerlitzc4550c62017-01-24 13:02:39 +02001016 /* props being zeroed by the caller, avoid zeroing it here */
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03001017
1018 err = mlx5_query_hca_vport_context(mdev, 0, port, 0, rep);
1019 if (err)
1020 goto out;
1021
1022 props->lid = rep->lid;
1023 props->lmc = rep->lmc;
1024 props->sm_lid = rep->sm_lid;
1025 props->sm_sl = rep->sm_sl;
1026 props->state = rep->vport_state;
1027 props->phys_state = rep->port_physical_state;
1028 props->port_cap_flags = rep->cap_mask1;
1029 props->gid_tbl_len = mlx5_get_gid_table_len(MLX5_CAP_GEN(mdev, gid_table_size));
1030 props->max_msg_sz = 1 << MLX5_CAP_GEN(mdev, log_max_msg);
1031 props->pkey_tbl_len = mlx5_to_sw_pkey_sz(MLX5_CAP_GEN(mdev, pkey_table_size));
1032 props->bad_pkey_cntr = rep->pkey_violation_counter;
1033 props->qkey_viol_cntr = rep->qkey_violation_counter;
1034 props->subnet_timeout = rep->subnet_timeout;
1035 props->init_type_reply = rep->init_type_reply;
Eli Coheneff901d2016-03-11 22:58:42 +02001036 props->grh_required = rep->grh_required;
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03001037
1038 err = mlx5_query_port_link_width_oper(mdev, &ib_link_width_oper, port);
1039 if (err)
1040 goto out;
1041
1042 err = translate_active_width(ibdev, ib_link_width_oper,
1043 &props->active_width);
1044 if (err)
1045 goto out;
Noa Osherovichd5beb7f2016-06-02 10:47:53 +03001046 err = mlx5_query_port_ib_proto_oper(mdev, &props->active_speed, port);
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03001047 if (err)
1048 goto out;
1049
Saeed Mahameedfacc9692015-06-11 14:47:27 +03001050 mlx5_query_port_max_mtu(mdev, &max_mtu, port);
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03001051
1052 props->max_mtu = mlx5_mtu_to_ib_mtu(max_mtu);
1053
Saeed Mahameedfacc9692015-06-11 14:47:27 +03001054 mlx5_query_port_oper_mtu(mdev, &oper_mtu, port);
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03001055
1056 props->active_mtu = mlx5_mtu_to_ib_mtu(oper_mtu);
1057
1058 err = mlx5_query_port_vl_hw_cap(mdev, &vl_hw_cap, port);
1059 if (err)
1060 goto out;
1061
1062 err = translate_max_vl_num(ibdev, vl_hw_cap,
1063 &props->max_vl_num);
1064out:
1065 kfree(rep);
Eli Cohene126ba92013-07-07 17:25:49 +03001066 return err;
1067}
1068
1069int mlx5_ib_query_port(struct ib_device *ibdev, u8 port,
1070 struct ib_port_attr *props)
1071{
Ilan Tayari095b0922017-05-14 16:04:30 +03001072 unsigned int count;
1073 int ret;
1074
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03001075 switch (mlx5_get_vport_access_method(ibdev)) {
1076 case MLX5_VPORT_ACCESS_METHOD_MAD:
Ilan Tayari095b0922017-05-14 16:04:30 +03001077 ret = mlx5_query_mad_ifc_port(ibdev, port, props);
1078 break;
Eli Cohene126ba92013-07-07 17:25:49 +03001079
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03001080 case MLX5_VPORT_ACCESS_METHOD_HCA:
Ilan Tayari095b0922017-05-14 16:04:30 +03001081 ret = mlx5_query_hca_port(ibdev, port, props);
1082 break;
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03001083
Achiad Shochat3f89a642015-12-23 18:47:21 +02001084 case MLX5_VPORT_ACCESS_METHOD_NIC:
Ilan Tayari095b0922017-05-14 16:04:30 +03001085 ret = mlx5_query_port_roce(ibdev, port, props);
1086 break;
Achiad Shochat3f89a642015-12-23 18:47:21 +02001087
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03001088 default:
Ilan Tayari095b0922017-05-14 16:04:30 +03001089 ret = -EINVAL;
Eli Cohene126ba92013-07-07 17:25:49 +03001090 }
Ilan Tayari095b0922017-05-14 16:04:30 +03001091
1092 if (!ret && props) {
1093 count = mlx5_core_reserved_gids_count(to_mdev(ibdev)->mdev);
1094 props->gid_tbl_len -= count;
1095 }
1096 return ret;
Eli Cohene126ba92013-07-07 17:25:49 +03001097}
1098
1099static int mlx5_ib_query_gid(struct ib_device *ibdev, u8 port, int index,
1100 union ib_gid *gid)
1101{
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03001102 struct mlx5_ib_dev *dev = to_mdev(ibdev);
1103 struct mlx5_core_dev *mdev = dev->mdev;
Eli Cohene126ba92013-07-07 17:25:49 +03001104
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03001105 switch (mlx5_get_vport_access_method(ibdev)) {
1106 case MLX5_VPORT_ACCESS_METHOD_MAD:
1107 return mlx5_query_mad_ifc_gids(ibdev, port, index, gid);
Eli Cohene126ba92013-07-07 17:25:49 +03001108
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03001109 case MLX5_VPORT_ACCESS_METHOD_HCA:
1110 return mlx5_query_hca_vport_gid(mdev, 0, port, 0, index, gid);
Eli Cohene126ba92013-07-07 17:25:49 +03001111
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03001112 default:
1113 return -EINVAL;
1114 }
Eli Cohene126ba92013-07-07 17:25:49 +03001115
Eli Cohene126ba92013-07-07 17:25:49 +03001116}
1117
1118static int mlx5_ib_query_pkey(struct ib_device *ibdev, u8 port, u16 index,
1119 u16 *pkey)
1120{
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03001121 struct mlx5_ib_dev *dev = to_mdev(ibdev);
1122 struct mlx5_core_dev *mdev = dev->mdev;
Eli Cohene126ba92013-07-07 17:25:49 +03001123
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03001124 switch (mlx5_get_vport_access_method(ibdev)) {
1125 case MLX5_VPORT_ACCESS_METHOD_MAD:
1126 return mlx5_query_mad_ifc_pkey(ibdev, port, index, pkey);
Eli Cohene126ba92013-07-07 17:25:49 +03001127
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03001128 case MLX5_VPORT_ACCESS_METHOD_HCA:
1129 case MLX5_VPORT_ACCESS_METHOD_NIC:
1130 return mlx5_query_hca_vport_pkey(mdev, 0, port, 0, index,
1131 pkey);
1132 default:
1133 return -EINVAL;
1134 }
Eli Cohene126ba92013-07-07 17:25:49 +03001135}
1136
Eli Cohene126ba92013-07-07 17:25:49 +03001137static int mlx5_ib_modify_device(struct ib_device *ibdev, int mask,
1138 struct ib_device_modify *props)
1139{
1140 struct mlx5_ib_dev *dev = to_mdev(ibdev);
1141 struct mlx5_reg_node_desc in;
1142 struct mlx5_reg_node_desc out;
1143 int err;
1144
1145 if (mask & ~IB_DEVICE_MODIFY_NODE_DESC)
1146 return -EOPNOTSUPP;
1147
1148 if (!(mask & IB_DEVICE_MODIFY_NODE_DESC))
1149 return 0;
1150
1151 /*
1152 * If possible, pass node desc to FW, so it can generate
1153 * a 144 trap. If cmd fails, just ignore.
1154 */
Yuval Shaiabd99fde2016-08-25 10:57:07 -07001155 memcpy(&in, props->node_desc, IB_DEVICE_NODE_DESC_MAX);
Jack Morgenstein9603b612014-07-28 23:30:22 +03001156 err = mlx5_core_access_reg(dev->mdev, &in, sizeof(in), &out,
Eli Cohene126ba92013-07-07 17:25:49 +03001157 sizeof(out), MLX5_REG_NODE_DESC, 0, 1);
1158 if (err)
1159 return err;
1160
Yuval Shaiabd99fde2016-08-25 10:57:07 -07001161 memcpy(ibdev->node_desc, props->node_desc, IB_DEVICE_NODE_DESC_MAX);
Eli Cohene126ba92013-07-07 17:25:49 +03001162
1163 return err;
1164}
1165
Eli Cohencdbe33d2017-02-14 07:25:38 +02001166static int set_port_caps_atomic(struct mlx5_ib_dev *dev, u8 port_num, u32 mask,
1167 u32 value)
1168{
1169 struct mlx5_hca_vport_context ctx = {};
1170 int err;
1171
1172 err = mlx5_query_hca_vport_context(dev->mdev, 0,
1173 port_num, 0, &ctx);
1174 if (err)
1175 return err;
1176
1177 if (~ctx.cap_mask1_perm & mask) {
1178 mlx5_ib_warn(dev, "trying to change bitmask 0x%X but change supported 0x%X\n",
1179 mask, ctx.cap_mask1_perm);
1180 return -EINVAL;
1181 }
1182
1183 ctx.cap_mask1 = value;
1184 ctx.cap_mask1_perm = mask;
1185 err = mlx5_core_modify_hca_vport_context(dev->mdev, 0,
1186 port_num, 0, &ctx);
1187
1188 return err;
1189}
1190
Eli Cohene126ba92013-07-07 17:25:49 +03001191static int mlx5_ib_modify_port(struct ib_device *ibdev, u8 port, int mask,
1192 struct ib_port_modify *props)
1193{
1194 struct mlx5_ib_dev *dev = to_mdev(ibdev);
1195 struct ib_port_attr attr;
1196 u32 tmp;
1197 int err;
Eli Cohencdbe33d2017-02-14 07:25:38 +02001198 u32 change_mask;
1199 u32 value;
1200 bool is_ib = (mlx5_ib_port_link_layer(ibdev, port) ==
1201 IB_LINK_LAYER_INFINIBAND);
1202
Majd Dibbinyec255872017-08-23 08:35:42 +03001203 /* CM layer calls ib_modify_port() regardless of the link layer. For
1204 * Ethernet ports, qkey violation and Port capabilities are meaningless.
1205 */
1206 if (!is_ib)
1207 return 0;
1208
Eli Cohencdbe33d2017-02-14 07:25:38 +02001209 if (MLX5_CAP_GEN(dev->mdev, ib_virt) && is_ib) {
1210 change_mask = props->clr_port_cap_mask | props->set_port_cap_mask;
1211 value = ~props->clr_port_cap_mask | props->set_port_cap_mask;
1212 return set_port_caps_atomic(dev, port, change_mask, value);
1213 }
Eli Cohene126ba92013-07-07 17:25:49 +03001214
1215 mutex_lock(&dev->cap_mask_mutex);
1216
Or Gerlitzc4550c62017-01-24 13:02:39 +02001217 err = ib_query_port(ibdev, port, &attr);
Eli Cohene126ba92013-07-07 17:25:49 +03001218 if (err)
1219 goto out;
1220
1221 tmp = (attr.port_cap_flags | props->set_port_cap_mask) &
1222 ~props->clr_port_cap_mask;
1223
Jack Morgenstein9603b612014-07-28 23:30:22 +03001224 err = mlx5_set_port_caps(dev->mdev, port, tmp);
Eli Cohene126ba92013-07-07 17:25:49 +03001225
1226out:
1227 mutex_unlock(&dev->cap_mask_mutex);
1228 return err;
1229}
1230
Eli Cohen30aa60b2017-01-03 23:55:27 +02001231static void print_lib_caps(struct mlx5_ib_dev *dev, u64 caps)
1232{
1233 mlx5_ib_dbg(dev, "MLX5_LIB_CAP_4K_UAR = %s\n",
1234 caps & MLX5_LIB_CAP_4K_UAR ? "y" : "n");
1235}
1236
Eli Cohenb037c292017-01-03 23:55:26 +02001237static int calc_total_bfregs(struct mlx5_ib_dev *dev, bool lib_uar_4k,
1238 struct mlx5_ib_alloc_ucontext_req_v2 *req,
1239 u32 *num_sys_pages)
1240{
1241 int uars_per_sys_page;
1242 int bfregs_per_sys_page;
1243 int ref_bfregs = req->total_num_bfregs;
1244
1245 if (req->total_num_bfregs == 0)
1246 return -EINVAL;
1247
1248 BUILD_BUG_ON(MLX5_MAX_BFREGS % MLX5_NON_FP_BFREGS_IN_PAGE);
1249 BUILD_BUG_ON(MLX5_MAX_BFREGS < MLX5_NON_FP_BFREGS_IN_PAGE);
1250
1251 if (req->total_num_bfregs > MLX5_MAX_BFREGS)
1252 return -ENOMEM;
1253
1254 uars_per_sys_page = get_uars_per_sys_page(dev, lib_uar_4k);
1255 bfregs_per_sys_page = uars_per_sys_page * MLX5_NON_FP_BFREGS_PER_UAR;
1256 req->total_num_bfregs = ALIGN(req->total_num_bfregs, bfregs_per_sys_page);
1257 *num_sys_pages = req->total_num_bfregs / bfregs_per_sys_page;
1258
1259 if (req->num_low_latency_bfregs > req->total_num_bfregs - 1)
1260 return -EINVAL;
1261
Colin Ian King9c2d33d2017-06-27 08:40:59 +01001262 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 +02001263 MLX5_CAP_GEN(dev->mdev, uar_4k) ? "yes" : "no",
1264 lib_uar_4k ? "yes" : "no", ref_bfregs,
1265 req->total_num_bfregs, *num_sys_pages);
1266
1267 return 0;
1268}
1269
1270static int allocate_uars(struct mlx5_ib_dev *dev, struct mlx5_ib_ucontext *context)
1271{
1272 struct mlx5_bfreg_info *bfregi;
1273 int err;
1274 int i;
1275
1276 bfregi = &context->bfregi;
1277 for (i = 0; i < bfregi->num_sys_pages; i++) {
1278 err = mlx5_cmd_alloc_uar(dev->mdev, &bfregi->sys_pages[i]);
1279 if (err)
1280 goto error;
1281
1282 mlx5_ib_dbg(dev, "allocated uar %d\n", bfregi->sys_pages[i]);
1283 }
1284 return 0;
1285
1286error:
1287 for (--i; i >= 0; i--)
1288 if (mlx5_cmd_free_uar(dev->mdev, bfregi->sys_pages[i]))
1289 mlx5_ib_warn(dev, "failed to free uar %d\n", i);
1290
1291 return err;
1292}
1293
1294static int deallocate_uars(struct mlx5_ib_dev *dev, struct mlx5_ib_ucontext *context)
1295{
1296 struct mlx5_bfreg_info *bfregi;
1297 int err;
1298 int i;
1299
1300 bfregi = &context->bfregi;
1301 for (i = 0; i < bfregi->num_sys_pages; i++) {
1302 err = mlx5_cmd_free_uar(dev->mdev, bfregi->sys_pages[i]);
1303 if (err) {
1304 mlx5_ib_warn(dev, "failed to free uar %d\n", i);
1305 return err;
1306 }
1307 }
1308 return 0;
1309}
1310
Huy Nguyenc85023e2017-05-30 09:42:54 +03001311static int mlx5_ib_alloc_transport_domain(struct mlx5_ib_dev *dev, u32 *tdn)
1312{
1313 int err;
1314
1315 err = mlx5_core_alloc_transport_domain(dev->mdev, tdn);
1316 if (err)
1317 return err;
1318
1319 if ((MLX5_CAP_GEN(dev->mdev, port_type) != MLX5_CAP_PORT_TYPE_ETH) ||
1320 !MLX5_CAP_GEN(dev->mdev, disable_local_lb))
1321 return err;
1322
1323 mutex_lock(&dev->lb_mutex);
1324 dev->user_td++;
1325
1326 if (dev->user_td == 2)
1327 err = mlx5_nic_vport_update_local_lb(dev->mdev, true);
1328
1329 mutex_unlock(&dev->lb_mutex);
1330 return err;
1331}
1332
1333static void mlx5_ib_dealloc_transport_domain(struct mlx5_ib_dev *dev, u32 tdn)
1334{
1335 mlx5_core_dealloc_transport_domain(dev->mdev, tdn);
1336
1337 if ((MLX5_CAP_GEN(dev->mdev, port_type) != MLX5_CAP_PORT_TYPE_ETH) ||
1338 !MLX5_CAP_GEN(dev->mdev, disable_local_lb))
1339 return;
1340
1341 mutex_lock(&dev->lb_mutex);
1342 dev->user_td--;
1343
1344 if (dev->user_td < 2)
1345 mlx5_nic_vport_update_local_lb(dev->mdev, false);
1346
1347 mutex_unlock(&dev->lb_mutex);
1348}
1349
Eli Cohene126ba92013-07-07 17:25:49 +03001350static struct ib_ucontext *mlx5_ib_alloc_ucontext(struct ib_device *ibdev,
1351 struct ib_udata *udata)
1352{
1353 struct mlx5_ib_dev *dev = to_mdev(ibdev);
Matan Barakb368d7c2015-12-15 20:30:12 +02001354 struct mlx5_ib_alloc_ucontext_req_v2 req = {};
1355 struct mlx5_ib_alloc_ucontext_resp resp = {};
Eli Cohene126ba92013-07-07 17:25:49 +03001356 struct mlx5_ib_ucontext *context;
Eli Cohen2f5ff262017-01-03 23:55:21 +02001357 struct mlx5_bfreg_info *bfregi;
Eli Cohen78c0f982014-01-30 13:49:48 +02001358 int ver;
Eli Cohene126ba92013-07-07 17:25:49 +03001359 int err;
Majd Dibbinya168a41c2016-01-28 17:51:47 +02001360 size_t min_req_v2 = offsetof(struct mlx5_ib_alloc_ucontext_req_v2,
1361 max_cqe_version);
Eli Cohenb037c292017-01-03 23:55:26 +02001362 bool lib_uar_4k;
Eli Cohene126ba92013-07-07 17:25:49 +03001363
1364 if (!dev->ib_active)
1365 return ERR_PTR(-EAGAIN);
1366
Amrani, Rame0931112017-06-27 17:04:42 +03001367 if (udata->inlen == sizeof(struct mlx5_ib_alloc_ucontext_req))
Eli Cohen78c0f982014-01-30 13:49:48 +02001368 ver = 0;
Amrani, Rame0931112017-06-27 17:04:42 +03001369 else if (udata->inlen >= min_req_v2)
Eli Cohen78c0f982014-01-30 13:49:48 +02001370 ver = 2;
1371 else
1372 return ERR_PTR(-EINVAL);
1373
Amrani, Rame0931112017-06-27 17:04:42 +03001374 err = ib_copy_from_udata(&req, udata, min(udata->inlen, sizeof(req)));
Eli Cohene126ba92013-07-07 17:25:49 +03001375 if (err)
1376 return ERR_PTR(err);
1377
Matan Barakb368d7c2015-12-15 20:30:12 +02001378 if (req.flags)
Eli Cohen78c0f982014-01-30 13:49:48 +02001379 return ERR_PTR(-EINVAL);
1380
Haggai Abramovskyf72300c2016-01-14 19:12:58 +02001381 if (req.comp_mask || req.reserved0 || req.reserved1 || req.reserved2)
Matan Barakb368d7c2015-12-15 20:30:12 +02001382 return ERR_PTR(-EOPNOTSUPP);
1383
Eli Cohen2f5ff262017-01-03 23:55:21 +02001384 req.total_num_bfregs = ALIGN(req.total_num_bfregs,
1385 MLX5_NON_FP_BFREGS_PER_UAR);
1386 if (req.num_low_latency_bfregs > req.total_num_bfregs - 1)
Eli Cohene126ba92013-07-07 17:25:49 +03001387 return ERR_PTR(-EINVAL);
1388
Saeed Mahameed938fe832015-05-28 22:28:41 +03001389 resp.qp_tab_size = 1 << MLX5_CAP_GEN(dev->mdev, log_max_qp);
Noa Osherovich2cc6ad52016-06-04 15:15:33 +03001390 if (mlx5_core_is_pf(dev->mdev) && MLX5_CAP_GEN(dev->mdev, bf))
1391 resp.bf_reg_size = 1 << MLX5_CAP_GEN(dev->mdev, log_bf_reg_size);
Daniel Jurgensb47bd6e2016-10-25 18:36:24 +03001392 resp.cache_line_size = cache_line_size();
Saeed Mahameed938fe832015-05-28 22:28:41 +03001393 resp.max_sq_desc_sz = MLX5_CAP_GEN(dev->mdev, max_wqe_sz_sq);
1394 resp.max_rq_desc_sz = MLX5_CAP_GEN(dev->mdev, max_wqe_sz_rq);
1395 resp.max_send_wqebb = 1 << MLX5_CAP_GEN(dev->mdev, log_max_qp_sz);
1396 resp.max_recv_wr = 1 << MLX5_CAP_GEN(dev->mdev, log_max_qp_sz);
1397 resp.max_srq_recv_wr = 1 << MLX5_CAP_GEN(dev->mdev, log_max_srq_sz);
Haggai Abramovskyf72300c2016-01-14 19:12:58 +02001398 resp.cqe_version = min_t(__u8,
1399 (__u8)MLX5_CAP_GEN(dev->mdev, cqe_version),
1400 req.max_cqe_version);
Eli Cohen30aa60b2017-01-03 23:55:27 +02001401 resp.log_uar_size = MLX5_CAP_GEN(dev->mdev, uar_4k) ?
1402 MLX5_ADAPTER_PAGE_SHIFT : PAGE_SHIFT;
1403 resp.num_uars_per_page = MLX5_CAP_GEN(dev->mdev, uar_4k) ?
1404 MLX5_CAP_GEN(dev->mdev, num_of_uars_per_page) : 1;
Matan Barakb368d7c2015-12-15 20:30:12 +02001405 resp.response_length = min(offsetof(typeof(resp), response_length) +
1406 sizeof(resp.response_length), udata->outlen);
Eli Cohene126ba92013-07-07 17:25:49 +03001407
1408 context = kzalloc(sizeof(*context), GFP_KERNEL);
1409 if (!context)
1410 return ERR_PTR(-ENOMEM);
1411
Eli Cohen30aa60b2017-01-03 23:55:27 +02001412 lib_uar_4k = req.lib_caps & MLX5_LIB_CAP_4K_UAR;
Eli Cohen2f5ff262017-01-03 23:55:21 +02001413 bfregi = &context->bfregi;
Eli Cohenb037c292017-01-03 23:55:26 +02001414
1415 /* updates req->total_num_bfregs */
1416 err = calc_total_bfregs(dev, lib_uar_4k, &req, &bfregi->num_sys_pages);
1417 if (err)
1418 goto out_ctx;
1419
Eli Cohen2f5ff262017-01-03 23:55:21 +02001420 mutex_init(&bfregi->lock);
Eli Cohenb037c292017-01-03 23:55:26 +02001421 bfregi->lib_uar_4k = lib_uar_4k;
1422 bfregi->count = kcalloc(req.total_num_bfregs, sizeof(*bfregi->count),
1423 GFP_KERNEL);
1424 if (!bfregi->count) {
Eli Cohene126ba92013-07-07 17:25:49 +03001425 err = -ENOMEM;
1426 goto out_ctx;
1427 }
1428
Eli Cohenb037c292017-01-03 23:55:26 +02001429 bfregi->sys_pages = kcalloc(bfregi->num_sys_pages,
1430 sizeof(*bfregi->sys_pages),
1431 GFP_KERNEL);
1432 if (!bfregi->sys_pages) {
Eli Cohene126ba92013-07-07 17:25:49 +03001433 err = -ENOMEM;
Eli Cohenb037c292017-01-03 23:55:26 +02001434 goto out_count;
Eli Cohene126ba92013-07-07 17:25:49 +03001435 }
1436
Eli Cohenb037c292017-01-03 23:55:26 +02001437 err = allocate_uars(dev, context);
1438 if (err)
1439 goto out_sys_pages;
Eli Cohene126ba92013-07-07 17:25:49 +03001440
Haggai Eranb4cfe442014-12-11 17:04:26 +02001441#ifdef CONFIG_INFINIBAND_ON_DEMAND_PAGING
1442 context->ibucontext.invalidate_range = &mlx5_ib_invalidate_range;
1443#endif
1444
Artemy Kovalyov7d0cc6e2017-01-02 11:37:44 +02001445 context->upd_xlt_page = __get_free_page(GFP_KERNEL);
1446 if (!context->upd_xlt_page) {
1447 err = -ENOMEM;
1448 goto out_uars;
1449 }
1450 mutex_init(&context->upd_xlt_page_mutex);
1451
majd@mellanox.com146d2f12016-01-14 19:13:02 +02001452 if (MLX5_CAP_GEN(dev->mdev, log_max_transport_domain)) {
Huy Nguyenc85023e2017-05-30 09:42:54 +03001453 err = mlx5_ib_alloc_transport_domain(dev, &context->tdn);
majd@mellanox.com146d2f12016-01-14 19:13:02 +02001454 if (err)
Artemy Kovalyov7d0cc6e2017-01-02 11:37:44 +02001455 goto out_page;
majd@mellanox.com146d2f12016-01-14 19:13:02 +02001456 }
1457
Maor Gottlieb7c2344c2016-06-17 14:56:44 +03001458 INIT_LIST_HEAD(&context->vma_private_list);
Eli Cohene126ba92013-07-07 17:25:49 +03001459 INIT_LIST_HEAD(&context->db_page_list);
1460 mutex_init(&context->db_page_mutex);
1461
Eli Cohen2f5ff262017-01-03 23:55:21 +02001462 resp.tot_bfregs = req.total_num_bfregs;
Saeed Mahameed938fe832015-05-28 22:28:41 +03001463 resp.num_ports = MLX5_CAP_GEN(dev->mdev, num_ports);
Matan Barakb368d7c2015-12-15 20:30:12 +02001464
Haggai Abramovskyf72300c2016-01-14 19:12:58 +02001465 if (field_avail(typeof(resp), cqe_version, udata->outlen))
1466 resp.response_length += sizeof(resp.cqe_version);
Matan Barakb368d7c2015-12-15 20:30:12 +02001467
Bodong Wang402ca532016-06-17 15:02:20 +03001468 if (field_avail(typeof(resp), cmds_supp_uhw, udata->outlen)) {
Moni Shoua6ad279c52016-11-23 08:23:23 +02001469 resp.cmds_supp_uhw |= MLX5_USER_CMDS_SUPP_UHW_QUERY_DEVICE |
1470 MLX5_USER_CMDS_SUPP_UHW_CREATE_AH;
Bodong Wang402ca532016-06-17 15:02:20 +03001471 resp.response_length += sizeof(resp.cmds_supp_uhw);
1472 }
1473
Or Gerlitz78984892016-11-30 20:33:33 +02001474 if (field_avail(typeof(resp), eth_min_inline, udata->outlen)) {
1475 if (mlx5_ib_port_link_layer(ibdev, 1) == IB_LINK_LAYER_ETHERNET) {
1476 mlx5_query_min_inline(dev->mdev, &resp.eth_min_inline);
1477 resp.eth_min_inline++;
1478 }
1479 resp.response_length += sizeof(resp.eth_min_inline);
1480 }
1481
Noa Osherovichbc5c6ee2016-06-04 15:15:31 +03001482 /*
1483 * We don't want to expose information from the PCI bar that is located
1484 * after 4096 bytes, so if the arch only supports larger pages, let's
1485 * pretend we don't support reading the HCA's core clock. This is also
1486 * forced by mmap function.
1487 */
Eli Cohende8d6e02017-01-03 23:55:19 +02001488 if (field_avail(typeof(resp), hca_core_clock_offset, udata->outlen)) {
1489 if (PAGE_SIZE <= 4096) {
1490 resp.comp_mask |=
1491 MLX5_IB_ALLOC_UCONTEXT_RESP_MASK_CORE_CLOCK_OFFSET;
1492 resp.hca_core_clock_offset =
1493 offsetof(struct mlx5_init_seg, internal_timer_h) % PAGE_SIZE;
1494 }
Haggai Abramovskyf72300c2016-01-14 19:12:58 +02001495 resp.response_length += sizeof(resp.hca_core_clock_offset) +
Bodong Wang402ca532016-06-17 15:02:20 +03001496 sizeof(resp.reserved2);
Matan Barakb368d7c2015-12-15 20:30:12 +02001497 }
1498
Eli Cohen30aa60b2017-01-03 23:55:27 +02001499 if (field_avail(typeof(resp), log_uar_size, udata->outlen))
1500 resp.response_length += sizeof(resp.log_uar_size);
1501
1502 if (field_avail(typeof(resp), num_uars_per_page, udata->outlen))
1503 resp.response_length += sizeof(resp.num_uars_per_page);
1504
Matan Barakb368d7c2015-12-15 20:30:12 +02001505 err = ib_copy_to_udata(udata, &resp, resp.response_length);
Eli Cohene126ba92013-07-07 17:25:49 +03001506 if (err)
majd@mellanox.com146d2f12016-01-14 19:13:02 +02001507 goto out_td;
Eli Cohene126ba92013-07-07 17:25:49 +03001508
Eli Cohen2f5ff262017-01-03 23:55:21 +02001509 bfregi->ver = ver;
1510 bfregi->num_low_latency_bfregs = req.num_low_latency_bfregs;
Haggai Abramovskyf72300c2016-01-14 19:12:58 +02001511 context->cqe_version = resp.cqe_version;
Eli Cohen30aa60b2017-01-03 23:55:27 +02001512 context->lib_caps = req.lib_caps;
1513 print_lib_caps(dev, context->lib_caps);
Haggai Abramovskyf72300c2016-01-14 19:12:58 +02001514
Eli Cohene126ba92013-07-07 17:25:49 +03001515 return &context->ibucontext;
1516
majd@mellanox.com146d2f12016-01-14 19:13:02 +02001517out_td:
1518 if (MLX5_CAP_GEN(dev->mdev, log_max_transport_domain))
Huy Nguyenc85023e2017-05-30 09:42:54 +03001519 mlx5_ib_dealloc_transport_domain(dev, context->tdn);
majd@mellanox.com146d2f12016-01-14 19:13:02 +02001520
Artemy Kovalyov7d0cc6e2017-01-02 11:37:44 +02001521out_page:
1522 free_page(context->upd_xlt_page);
1523
Eli Cohene126ba92013-07-07 17:25:49 +03001524out_uars:
Eli Cohenb037c292017-01-03 23:55:26 +02001525 deallocate_uars(dev, context);
1526
1527out_sys_pages:
1528 kfree(bfregi->sys_pages);
1529
Eli Cohene126ba92013-07-07 17:25:49 +03001530out_count:
Eli Cohen2f5ff262017-01-03 23:55:21 +02001531 kfree(bfregi->count);
Eli Cohene126ba92013-07-07 17:25:49 +03001532
Eli Cohene126ba92013-07-07 17:25:49 +03001533out_ctx:
1534 kfree(context);
Eli Cohenb037c292017-01-03 23:55:26 +02001535
Eli Cohene126ba92013-07-07 17:25:49 +03001536 return ERR_PTR(err);
1537}
1538
1539static int mlx5_ib_dealloc_ucontext(struct ib_ucontext *ibcontext)
1540{
1541 struct mlx5_ib_ucontext *context = to_mucontext(ibcontext);
1542 struct mlx5_ib_dev *dev = to_mdev(ibcontext->device);
Eli Cohenb037c292017-01-03 23:55:26 +02001543 struct mlx5_bfreg_info *bfregi;
Eli Cohene126ba92013-07-07 17:25:49 +03001544
Eli Cohenb037c292017-01-03 23:55:26 +02001545 bfregi = &context->bfregi;
majd@mellanox.com146d2f12016-01-14 19:13:02 +02001546 if (MLX5_CAP_GEN(dev->mdev, log_max_transport_domain))
Huy Nguyenc85023e2017-05-30 09:42:54 +03001547 mlx5_ib_dealloc_transport_domain(dev, context->tdn);
majd@mellanox.com146d2f12016-01-14 19:13:02 +02001548
Artemy Kovalyov7d0cc6e2017-01-02 11:37:44 +02001549 free_page(context->upd_xlt_page);
Eli Cohenb037c292017-01-03 23:55:26 +02001550 deallocate_uars(dev, context);
1551 kfree(bfregi->sys_pages);
Eli Cohen2f5ff262017-01-03 23:55:21 +02001552 kfree(bfregi->count);
Eli Cohene126ba92013-07-07 17:25:49 +03001553 kfree(context);
1554
1555 return 0;
1556}
1557
Eli Cohenb037c292017-01-03 23:55:26 +02001558static phys_addr_t uar_index2pfn(struct mlx5_ib_dev *dev,
1559 struct mlx5_bfreg_info *bfregi,
1560 int idx)
Eli Cohene126ba92013-07-07 17:25:49 +03001561{
Eli Cohenb037c292017-01-03 23:55:26 +02001562 int fw_uars_per_page;
1563
1564 fw_uars_per_page = MLX5_CAP_GEN(dev->mdev, uar_4k) ? MLX5_UARS_IN_PAGE : 1;
1565
1566 return (pci_resource_start(dev->mdev->pdev, 0) >> PAGE_SHIFT) +
1567 bfregi->sys_pages[idx] / fw_uars_per_page;
Eli Cohene126ba92013-07-07 17:25:49 +03001568}
1569
1570static int get_command(unsigned long offset)
1571{
1572 return (offset >> MLX5_IB_MMAP_CMD_SHIFT) & MLX5_IB_MMAP_CMD_MASK;
1573}
1574
1575static int get_arg(unsigned long offset)
1576{
1577 return offset & ((1 << MLX5_IB_MMAP_CMD_SHIFT) - 1);
1578}
1579
1580static int get_index(unsigned long offset)
1581{
1582 return get_arg(offset);
1583}
1584
Maor Gottlieb7c2344c2016-06-17 14:56:44 +03001585static void mlx5_ib_vma_open(struct vm_area_struct *area)
1586{
1587 /* vma_open is called when a new VMA is created on top of our VMA. This
1588 * is done through either mremap flow or split_vma (usually due to
1589 * mlock, madvise, munmap, etc.) We do not support a clone of the VMA,
1590 * as this VMA is strongly hardware related. Therefore we set the
1591 * vm_ops of the newly created/cloned VMA to NULL, to prevent it from
1592 * calling us again and trying to do incorrect actions. We assume that
1593 * the original VMA size is exactly a single page, and therefore all
1594 * "splitting" operation will not happen to it.
1595 */
1596 area->vm_ops = NULL;
1597}
1598
1599static void mlx5_ib_vma_close(struct vm_area_struct *area)
1600{
1601 struct mlx5_ib_vma_private_data *mlx5_ib_vma_priv_data;
1602
1603 /* It's guaranteed that all VMAs opened on a FD are closed before the
1604 * file itself is closed, therefore no sync is needed with the regular
1605 * closing flow. (e.g. mlx5 ib_dealloc_ucontext)
1606 * However need a sync with accessing the vma as part of
1607 * mlx5_ib_disassociate_ucontext.
1608 * The close operation is usually called under mm->mmap_sem except when
1609 * process is exiting.
1610 * The exiting case is handled explicitly as part of
1611 * mlx5_ib_disassociate_ucontext.
1612 */
1613 mlx5_ib_vma_priv_data = (struct mlx5_ib_vma_private_data *)area->vm_private_data;
1614
1615 /* setting the vma context pointer to null in the mlx5_ib driver's
1616 * private data, to protect a race condition in
1617 * mlx5_ib_disassociate_ucontext().
1618 */
1619 mlx5_ib_vma_priv_data->vma = NULL;
1620 list_del(&mlx5_ib_vma_priv_data->list);
1621 kfree(mlx5_ib_vma_priv_data);
1622}
1623
1624static const struct vm_operations_struct mlx5_ib_vm_ops = {
1625 .open = mlx5_ib_vma_open,
1626 .close = mlx5_ib_vma_close
1627};
1628
1629static int mlx5_ib_set_vma_data(struct vm_area_struct *vma,
1630 struct mlx5_ib_ucontext *ctx)
1631{
1632 struct mlx5_ib_vma_private_data *vma_prv;
1633 struct list_head *vma_head = &ctx->vma_private_list;
1634
1635 vma_prv = kzalloc(sizeof(*vma_prv), GFP_KERNEL);
1636 if (!vma_prv)
1637 return -ENOMEM;
1638
1639 vma_prv->vma = vma;
1640 vma->vm_private_data = vma_prv;
1641 vma->vm_ops = &mlx5_ib_vm_ops;
1642
1643 list_add(&vma_prv->list, vma_head);
1644
1645 return 0;
1646}
1647
1648static void mlx5_ib_disassociate_ucontext(struct ib_ucontext *ibcontext)
1649{
1650 int ret;
1651 struct vm_area_struct *vma;
1652 struct mlx5_ib_vma_private_data *vma_private, *n;
1653 struct mlx5_ib_ucontext *context = to_mucontext(ibcontext);
1654 struct task_struct *owning_process = NULL;
1655 struct mm_struct *owning_mm = NULL;
1656
1657 owning_process = get_pid_task(ibcontext->tgid, PIDTYPE_PID);
1658 if (!owning_process)
1659 return;
1660
1661 owning_mm = get_task_mm(owning_process);
1662 if (!owning_mm) {
1663 pr_info("no mm, disassociate ucontext is pending task termination\n");
1664 while (1) {
1665 put_task_struct(owning_process);
1666 usleep_range(1000, 2000);
1667 owning_process = get_pid_task(ibcontext->tgid,
1668 PIDTYPE_PID);
1669 if (!owning_process ||
1670 owning_process->state == TASK_DEAD) {
1671 pr_info("disassociate ucontext done, task was terminated\n");
1672 /* in case task was dead need to release the
1673 * task struct.
1674 */
1675 if (owning_process)
1676 put_task_struct(owning_process);
1677 return;
1678 }
1679 }
1680 }
1681
1682 /* need to protect from a race on closing the vma as part of
1683 * mlx5_ib_vma_close.
1684 */
Maor Gottliebecc7d832017-03-29 06:03:02 +03001685 down_write(&owning_mm->mmap_sem);
Maor Gottlieb7c2344c2016-06-17 14:56:44 +03001686 list_for_each_entry_safe(vma_private, n, &context->vma_private_list,
1687 list) {
1688 vma = vma_private->vma;
1689 ret = zap_vma_ptes(vma, vma->vm_start,
1690 PAGE_SIZE);
1691 WARN_ONCE(ret, "%s: zap_vma_ptes failed", __func__);
1692 /* context going to be destroyed, should
1693 * not access ops any more.
1694 */
Maor Gottlieb13776612017-03-29 06:03:03 +03001695 vma->vm_flags &= ~(VM_SHARED | VM_MAYSHARE);
Maor Gottlieb7c2344c2016-06-17 14:56:44 +03001696 vma->vm_ops = NULL;
1697 list_del(&vma_private->list);
1698 kfree(vma_private);
1699 }
Maor Gottliebecc7d832017-03-29 06:03:02 +03001700 up_write(&owning_mm->mmap_sem);
Maor Gottlieb7c2344c2016-06-17 14:56:44 +03001701 mmput(owning_mm);
1702 put_task_struct(owning_process);
1703}
1704
Guy Levi37aa5c32016-04-27 16:49:50 +03001705static inline char *mmap_cmd2str(enum mlx5_ib_mmap_cmd cmd)
1706{
1707 switch (cmd) {
1708 case MLX5_IB_MMAP_WC_PAGE:
1709 return "WC";
1710 case MLX5_IB_MMAP_REGULAR_PAGE:
1711 return "best effort WC";
1712 case MLX5_IB_MMAP_NC_PAGE:
1713 return "NC";
1714 default:
1715 return NULL;
1716 }
1717}
1718
1719static int uar_mmap(struct mlx5_ib_dev *dev, enum mlx5_ib_mmap_cmd cmd,
Maor Gottlieb7c2344c2016-06-17 14:56:44 +03001720 struct vm_area_struct *vma,
1721 struct mlx5_ib_ucontext *context)
Guy Levi37aa5c32016-04-27 16:49:50 +03001722{
Eli Cohen2f5ff262017-01-03 23:55:21 +02001723 struct mlx5_bfreg_info *bfregi = &context->bfregi;
Guy Levi37aa5c32016-04-27 16:49:50 +03001724 int err;
1725 unsigned long idx;
1726 phys_addr_t pfn, pa;
1727 pgprot_t prot;
Eli Cohenb037c292017-01-03 23:55:26 +02001728 int uars_per_page;
1729
1730 if (vma->vm_end - vma->vm_start != PAGE_SIZE)
1731 return -EINVAL;
1732
1733 uars_per_page = get_uars_per_sys_page(dev, bfregi->lib_uar_4k);
1734 idx = get_index(vma->vm_pgoff);
1735 if (idx % uars_per_page ||
1736 idx * uars_per_page >= bfregi->num_sys_pages) {
1737 mlx5_ib_warn(dev, "invalid uar index %lu\n", idx);
1738 return -EINVAL;
1739 }
Guy Levi37aa5c32016-04-27 16:49:50 +03001740
1741 switch (cmd) {
1742 case MLX5_IB_MMAP_WC_PAGE:
1743/* Some architectures don't support WC memory */
1744#if defined(CONFIG_X86)
1745 if (!pat_enabled())
1746 return -EPERM;
1747#elif !(defined(CONFIG_PPC) || (defined(CONFIG_ARM) && defined(CONFIG_MMU)))
1748 return -EPERM;
1749#endif
1750 /* fall through */
1751 case MLX5_IB_MMAP_REGULAR_PAGE:
1752 /* For MLX5_IB_MMAP_REGULAR_PAGE do the best effort to get WC */
1753 prot = pgprot_writecombine(vma->vm_page_prot);
1754 break;
1755 case MLX5_IB_MMAP_NC_PAGE:
1756 prot = pgprot_noncached(vma->vm_page_prot);
1757 break;
1758 default:
1759 return -EINVAL;
1760 }
1761
Eli Cohenb037c292017-01-03 23:55:26 +02001762 pfn = uar_index2pfn(dev, bfregi, idx);
Guy Levi37aa5c32016-04-27 16:49:50 +03001763 mlx5_ib_dbg(dev, "uar idx 0x%lx, pfn %pa\n", idx, &pfn);
1764
1765 vma->vm_page_prot = prot;
1766 err = io_remap_pfn_range(vma, vma->vm_start, pfn,
1767 PAGE_SIZE, vma->vm_page_prot);
1768 if (err) {
1769 mlx5_ib_err(dev, "io_remap_pfn_range failed with error=%d, vm_start=0x%lx, pfn=%pa, mmap_cmd=%s\n",
1770 err, vma->vm_start, &pfn, mmap_cmd2str(cmd));
1771 return -EAGAIN;
1772 }
1773
1774 pa = pfn << PAGE_SHIFT;
1775 mlx5_ib_dbg(dev, "mapped %s at 0x%lx, PA %pa\n", mmap_cmd2str(cmd),
1776 vma->vm_start, &pa);
1777
Maor Gottlieb7c2344c2016-06-17 14:56:44 +03001778 return mlx5_ib_set_vma_data(vma, context);
Guy Levi37aa5c32016-04-27 16:49:50 +03001779}
1780
Eli Cohene126ba92013-07-07 17:25:49 +03001781static int mlx5_ib_mmap(struct ib_ucontext *ibcontext, struct vm_area_struct *vma)
1782{
1783 struct mlx5_ib_ucontext *context = to_mucontext(ibcontext);
1784 struct mlx5_ib_dev *dev = to_mdev(ibcontext->device);
Eli Cohene126ba92013-07-07 17:25:49 +03001785 unsigned long command;
Eli Cohene126ba92013-07-07 17:25:49 +03001786 phys_addr_t pfn;
1787
1788 command = get_command(vma->vm_pgoff);
1789 switch (command) {
Guy Levi37aa5c32016-04-27 16:49:50 +03001790 case MLX5_IB_MMAP_WC_PAGE:
1791 case MLX5_IB_MMAP_NC_PAGE:
Eli Cohene126ba92013-07-07 17:25:49 +03001792 case MLX5_IB_MMAP_REGULAR_PAGE:
Maor Gottlieb7c2344c2016-06-17 14:56:44 +03001793 return uar_mmap(dev, command, vma, context);
Eli Cohene126ba92013-07-07 17:25:49 +03001794
1795 case MLX5_IB_MMAP_GET_CONTIGUOUS_PAGES:
1796 return -ENOSYS;
1797
Matan Barakd69e3bc2015-12-15 20:30:13 +02001798 case MLX5_IB_MMAP_CORE_CLOCK:
Matan Barakd69e3bc2015-12-15 20:30:13 +02001799 if (vma->vm_end - vma->vm_start != PAGE_SIZE)
1800 return -EINVAL;
1801
Matan Barak6cbac1e2016-04-14 16:52:10 +03001802 if (vma->vm_flags & VM_WRITE)
Matan Barakd69e3bc2015-12-15 20:30:13 +02001803 return -EPERM;
1804
1805 /* Don't expose to user-space information it shouldn't have */
1806 if (PAGE_SIZE > 4096)
1807 return -EOPNOTSUPP;
1808
1809 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
1810 pfn = (dev->mdev->iseg_base +
1811 offsetof(struct mlx5_init_seg, internal_timer_h)) >>
1812 PAGE_SHIFT;
1813 if (io_remap_pfn_range(vma, vma->vm_start, pfn,
1814 PAGE_SIZE, vma->vm_page_prot))
1815 return -EAGAIN;
1816
1817 mlx5_ib_dbg(dev, "mapped internal timer at 0x%lx, PA 0x%llx\n",
1818 vma->vm_start,
1819 (unsigned long long)pfn << PAGE_SHIFT);
1820 break;
Matan Barakd69e3bc2015-12-15 20:30:13 +02001821
Eli Cohene126ba92013-07-07 17:25:49 +03001822 default:
1823 return -EINVAL;
1824 }
1825
1826 return 0;
1827}
1828
Eli Cohene126ba92013-07-07 17:25:49 +03001829static struct ib_pd *mlx5_ib_alloc_pd(struct ib_device *ibdev,
1830 struct ib_ucontext *context,
1831 struct ib_udata *udata)
1832{
1833 struct mlx5_ib_alloc_pd_resp resp;
1834 struct mlx5_ib_pd *pd;
1835 int err;
1836
1837 pd = kmalloc(sizeof(*pd), GFP_KERNEL);
1838 if (!pd)
1839 return ERR_PTR(-ENOMEM);
1840
Jack Morgenstein9603b612014-07-28 23:30:22 +03001841 err = mlx5_core_alloc_pd(to_mdev(ibdev)->mdev, &pd->pdn);
Eli Cohene126ba92013-07-07 17:25:49 +03001842 if (err) {
1843 kfree(pd);
1844 return ERR_PTR(err);
1845 }
1846
1847 if (context) {
1848 resp.pdn = pd->pdn;
1849 if (ib_copy_to_udata(udata, &resp, sizeof(resp))) {
Jack Morgenstein9603b612014-07-28 23:30:22 +03001850 mlx5_core_dealloc_pd(to_mdev(ibdev)->mdev, pd->pdn);
Eli Cohene126ba92013-07-07 17:25:49 +03001851 kfree(pd);
1852 return ERR_PTR(-EFAULT);
1853 }
Eli Cohene126ba92013-07-07 17:25:49 +03001854 }
1855
1856 return &pd->ibpd;
1857}
1858
1859static int mlx5_ib_dealloc_pd(struct ib_pd *pd)
1860{
1861 struct mlx5_ib_dev *mdev = to_mdev(pd->device);
1862 struct mlx5_ib_pd *mpd = to_mpd(pd);
1863
Jack Morgenstein9603b612014-07-28 23:30:22 +03001864 mlx5_core_dealloc_pd(mdev->mdev, mpd->pdn);
Eli Cohene126ba92013-07-07 17:25:49 +03001865 kfree(mpd);
1866
1867 return 0;
1868}
1869
Maor Gottlieb466fa6d2016-08-30 16:58:36 +03001870enum {
1871 MATCH_CRITERIA_ENABLE_OUTER_BIT,
1872 MATCH_CRITERIA_ENABLE_MISC_BIT,
1873 MATCH_CRITERIA_ENABLE_INNER_BIT
1874};
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02001875
Maor Gottlieb466fa6d2016-08-30 16:58:36 +03001876#define HEADER_IS_ZERO(match_criteria, headers) \
1877 !(memchr_inv(MLX5_ADDR_OF(fte_match_param, match_criteria, headers), \
1878 0, MLX5_FLD_SZ_BYTES(fte_match_param, headers))) \
1879
1880static u8 get_match_criteria_enable(u32 *match_criteria)
1881{
1882 u8 match_criteria_enable;
1883
1884 match_criteria_enable =
1885 (!HEADER_IS_ZERO(match_criteria, outer_headers)) <<
1886 MATCH_CRITERIA_ENABLE_OUTER_BIT;
1887 match_criteria_enable |=
1888 (!HEADER_IS_ZERO(match_criteria, misc_parameters)) <<
1889 MATCH_CRITERIA_ENABLE_MISC_BIT;
1890 match_criteria_enable |=
1891 (!HEADER_IS_ZERO(match_criteria, inner_headers)) <<
1892 MATCH_CRITERIA_ENABLE_INNER_BIT;
1893
1894 return match_criteria_enable;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02001895}
1896
Maor Gottliebca0d4752016-08-30 16:58:35 +03001897static void set_proto(void *outer_c, void *outer_v, u8 mask, u8 val)
1898{
1899 MLX5_SET(fte_match_set_lyr_2_4, outer_c, ip_protocol, mask);
1900 MLX5_SET(fte_match_set_lyr_2_4, outer_v, ip_protocol, val);
1901}
1902
Moses Reuben2d1e6972016-11-14 19:04:52 +02001903static void set_flow_label(void *misc_c, void *misc_v, u8 mask, u8 val,
1904 bool inner)
1905{
1906 if (inner) {
1907 MLX5_SET(fte_match_set_misc,
1908 misc_c, inner_ipv6_flow_label, mask);
1909 MLX5_SET(fte_match_set_misc,
1910 misc_v, inner_ipv6_flow_label, val);
1911 } else {
1912 MLX5_SET(fte_match_set_misc,
1913 misc_c, outer_ipv6_flow_label, mask);
1914 MLX5_SET(fte_match_set_misc,
1915 misc_v, outer_ipv6_flow_label, val);
1916 }
1917}
1918
Maor Gottliebca0d4752016-08-30 16:58:35 +03001919static void set_tos(void *outer_c, void *outer_v, u8 mask, u8 val)
1920{
1921 MLX5_SET(fte_match_set_lyr_2_4, outer_c, ip_ecn, mask);
1922 MLX5_SET(fte_match_set_lyr_2_4, outer_v, ip_ecn, val);
1923 MLX5_SET(fte_match_set_lyr_2_4, outer_c, ip_dscp, mask >> 2);
1924 MLX5_SET(fte_match_set_lyr_2_4, outer_v, ip_dscp, val >> 2);
1925}
1926
Maor Gottliebc47ac6a2016-08-30 16:58:31 +03001927#define LAST_ETH_FIELD vlan_tag
1928#define LAST_IB_FIELD sl
Maor Gottliebca0d4752016-08-30 16:58:35 +03001929#define LAST_IPV4_FIELD tos
Maor Gottlieb466fa6d2016-08-30 16:58:36 +03001930#define LAST_IPV6_FIELD traffic_class
Maor Gottliebc47ac6a2016-08-30 16:58:31 +03001931#define LAST_TCP_UDP_FIELD src_port
Moses Reubenffb30d82016-11-14 19:04:50 +02001932#define LAST_TUNNEL_FIELD tunnel_id
Moses Reuben2ac693f2017-01-18 14:59:50 +02001933#define LAST_FLOW_TAG_FIELD tag_id
Slava Shwartsmana22ed862017-04-03 13:13:52 +03001934#define LAST_DROP_FIELD size
Maor Gottliebc47ac6a2016-08-30 16:58:31 +03001935
1936/* Field is the last supported field */
1937#define FIELDS_NOT_SUPPORTED(filter, field)\
1938 memchr_inv((void *)&filter.field +\
1939 sizeof(filter.field), 0,\
1940 sizeof(filter) -\
1941 offsetof(typeof(filter), field) -\
1942 sizeof(filter.field))
1943
Ariel Levkovich19cc7522017-04-03 13:11:03 +03001944#define IPV4_VERSION 4
1945#define IPV6_VERSION 6
1946static int parse_flow_attr(struct mlx5_core_dev *mdev, u32 *match_c,
1947 u32 *match_v, const union ib_flow_spec *ib_spec,
Slava Shwartsmana22ed862017-04-03 13:13:52 +03001948 u32 *tag_id, bool *is_drop)
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02001949{
Maor Gottlieb466fa6d2016-08-30 16:58:36 +03001950 void *misc_params_c = MLX5_ADDR_OF(fte_match_param, match_c,
1951 misc_parameters);
1952 void *misc_params_v = MLX5_ADDR_OF(fte_match_param, match_v,
1953 misc_parameters);
Moses Reuben2d1e6972016-11-14 19:04:52 +02001954 void *headers_c;
1955 void *headers_v;
Ariel Levkovich19cc7522017-04-03 13:11:03 +03001956 int match_ipv;
Maor Gottlieb466fa6d2016-08-30 16:58:36 +03001957
Moses Reuben2d1e6972016-11-14 19:04:52 +02001958 if (ib_spec->type & IB_FLOW_SPEC_INNER) {
1959 headers_c = MLX5_ADDR_OF(fte_match_param, match_c,
1960 inner_headers);
1961 headers_v = MLX5_ADDR_OF(fte_match_param, match_v,
1962 inner_headers);
Ariel Levkovich19cc7522017-04-03 13:11:03 +03001963 match_ipv = MLX5_CAP_FLOWTABLE_NIC_RX(mdev,
1964 ft_field_support.inner_ip_version);
Moses Reuben2d1e6972016-11-14 19:04:52 +02001965 } else {
1966 headers_c = MLX5_ADDR_OF(fte_match_param, match_c,
1967 outer_headers);
1968 headers_v = MLX5_ADDR_OF(fte_match_param, match_v,
1969 outer_headers);
Ariel Levkovich19cc7522017-04-03 13:11:03 +03001970 match_ipv = MLX5_CAP_FLOWTABLE_NIC_RX(mdev,
1971 ft_field_support.outer_ip_version);
Moses Reuben2d1e6972016-11-14 19:04:52 +02001972 }
1973
1974 switch (ib_spec->type & ~IB_FLOW_SPEC_INNER) {
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02001975 case IB_FLOW_SPEC_ETH:
Maor Gottliebc47ac6a2016-08-30 16:58:31 +03001976 if (FIELDS_NOT_SUPPORTED(ib_spec->eth.mask, LAST_ETH_FIELD))
Leon Romanovsky1ffd3a22017-01-18 14:59:51 +02001977 return -EOPNOTSUPP;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02001978
Moses Reuben2d1e6972016-11-14 19:04:52 +02001979 ether_addr_copy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02001980 dmac_47_16),
1981 ib_spec->eth.mask.dst_mac);
Moses Reuben2d1e6972016-11-14 19:04:52 +02001982 ether_addr_copy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02001983 dmac_47_16),
1984 ib_spec->eth.val.dst_mac);
1985
Moses Reuben2d1e6972016-11-14 19:04:52 +02001986 ether_addr_copy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
Maor Gottliebee3da802016-09-12 19:16:24 +03001987 smac_47_16),
1988 ib_spec->eth.mask.src_mac);
Moses Reuben2d1e6972016-11-14 19:04:52 +02001989 ether_addr_copy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
Maor Gottliebee3da802016-09-12 19:16:24 +03001990 smac_47_16),
1991 ib_spec->eth.val.src_mac);
1992
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02001993 if (ib_spec->eth.mask.vlan_tag) {
Moses Reuben2d1e6972016-11-14 19:04:52 +02001994 MLX5_SET(fte_match_set_lyr_2_4, headers_c,
Mohamad Haj Yahia10543362016-10-09 16:25:43 +03001995 cvlan_tag, 1);
Moses Reuben2d1e6972016-11-14 19:04:52 +02001996 MLX5_SET(fte_match_set_lyr_2_4, headers_v,
Mohamad Haj Yahia10543362016-10-09 16:25:43 +03001997 cvlan_tag, 1);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02001998
Moses Reuben2d1e6972016-11-14 19:04:52 +02001999 MLX5_SET(fte_match_set_lyr_2_4, headers_c,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002000 first_vid, ntohs(ib_spec->eth.mask.vlan_tag));
Moses Reuben2d1e6972016-11-14 19:04:52 +02002001 MLX5_SET(fte_match_set_lyr_2_4, headers_v,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002002 first_vid, ntohs(ib_spec->eth.val.vlan_tag));
2003
Moses Reuben2d1e6972016-11-14 19:04:52 +02002004 MLX5_SET(fte_match_set_lyr_2_4, headers_c,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002005 first_cfi,
2006 ntohs(ib_spec->eth.mask.vlan_tag) >> 12);
Moses Reuben2d1e6972016-11-14 19:04:52 +02002007 MLX5_SET(fte_match_set_lyr_2_4, headers_v,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002008 first_cfi,
2009 ntohs(ib_spec->eth.val.vlan_tag) >> 12);
2010
Moses Reuben2d1e6972016-11-14 19:04:52 +02002011 MLX5_SET(fte_match_set_lyr_2_4, headers_c,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002012 first_prio,
2013 ntohs(ib_spec->eth.mask.vlan_tag) >> 13);
Moses Reuben2d1e6972016-11-14 19:04:52 +02002014 MLX5_SET(fte_match_set_lyr_2_4, headers_v,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002015 first_prio,
2016 ntohs(ib_spec->eth.val.vlan_tag) >> 13);
2017 }
Moses Reuben2d1e6972016-11-14 19:04:52 +02002018 MLX5_SET(fte_match_set_lyr_2_4, headers_c,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002019 ethertype, ntohs(ib_spec->eth.mask.ether_type));
Moses Reuben2d1e6972016-11-14 19:04:52 +02002020 MLX5_SET(fte_match_set_lyr_2_4, headers_v,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002021 ethertype, ntohs(ib_spec->eth.val.ether_type));
2022 break;
2023 case IB_FLOW_SPEC_IPV4:
Maor Gottliebc47ac6a2016-08-30 16:58:31 +03002024 if (FIELDS_NOT_SUPPORTED(ib_spec->ipv4.mask, LAST_IPV4_FIELD))
Leon Romanovsky1ffd3a22017-01-18 14:59:51 +02002025 return -EOPNOTSUPP;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002026
Ariel Levkovich19cc7522017-04-03 13:11:03 +03002027 if (match_ipv) {
2028 MLX5_SET(fte_match_set_lyr_2_4, headers_c,
2029 ip_version, 0xf);
2030 MLX5_SET(fte_match_set_lyr_2_4, headers_v,
2031 ip_version, IPV4_VERSION);
2032 } else {
2033 MLX5_SET(fte_match_set_lyr_2_4, headers_c,
2034 ethertype, 0xffff);
2035 MLX5_SET(fte_match_set_lyr_2_4, headers_v,
2036 ethertype, ETH_P_IP);
2037 }
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002038
Moses Reuben2d1e6972016-11-14 19:04:52 +02002039 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002040 src_ipv4_src_ipv6.ipv4_layout.ipv4),
2041 &ib_spec->ipv4.mask.src_ip,
2042 sizeof(ib_spec->ipv4.mask.src_ip));
Moses Reuben2d1e6972016-11-14 19:04:52 +02002043 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002044 src_ipv4_src_ipv6.ipv4_layout.ipv4),
2045 &ib_spec->ipv4.val.src_ip,
2046 sizeof(ib_spec->ipv4.val.src_ip));
Moses Reuben2d1e6972016-11-14 19:04:52 +02002047 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002048 dst_ipv4_dst_ipv6.ipv4_layout.ipv4),
2049 &ib_spec->ipv4.mask.dst_ip,
2050 sizeof(ib_spec->ipv4.mask.dst_ip));
Moses Reuben2d1e6972016-11-14 19:04:52 +02002051 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002052 dst_ipv4_dst_ipv6.ipv4_layout.ipv4),
2053 &ib_spec->ipv4.val.dst_ip,
2054 sizeof(ib_spec->ipv4.val.dst_ip));
Maor Gottliebca0d4752016-08-30 16:58:35 +03002055
Moses Reuben2d1e6972016-11-14 19:04:52 +02002056 set_tos(headers_c, headers_v,
Maor Gottliebca0d4752016-08-30 16:58:35 +03002057 ib_spec->ipv4.mask.tos, ib_spec->ipv4.val.tos);
2058
Moses Reuben2d1e6972016-11-14 19:04:52 +02002059 set_proto(headers_c, headers_v,
Maor Gottliebca0d4752016-08-30 16:58:35 +03002060 ib_spec->ipv4.mask.proto, ib_spec->ipv4.val.proto);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002061 break;
Maor Gottlieb026bae02016-06-17 15:14:51 +03002062 case IB_FLOW_SPEC_IPV6:
Maor Gottliebc47ac6a2016-08-30 16:58:31 +03002063 if (FIELDS_NOT_SUPPORTED(ib_spec->ipv6.mask, LAST_IPV6_FIELD))
Leon Romanovsky1ffd3a22017-01-18 14:59:51 +02002064 return -EOPNOTSUPP;
Maor Gottlieb026bae02016-06-17 15:14:51 +03002065
Ariel Levkovich19cc7522017-04-03 13:11:03 +03002066 if (match_ipv) {
2067 MLX5_SET(fte_match_set_lyr_2_4, headers_c,
2068 ip_version, 0xf);
2069 MLX5_SET(fte_match_set_lyr_2_4, headers_v,
2070 ip_version, IPV6_VERSION);
2071 } else {
2072 MLX5_SET(fte_match_set_lyr_2_4, headers_c,
2073 ethertype, 0xffff);
2074 MLX5_SET(fte_match_set_lyr_2_4, headers_v,
2075 ethertype, ETH_P_IPV6);
2076 }
Maor Gottlieb026bae02016-06-17 15:14:51 +03002077
Moses Reuben2d1e6972016-11-14 19:04:52 +02002078 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
Maor Gottlieb026bae02016-06-17 15:14:51 +03002079 src_ipv4_src_ipv6.ipv6_layout.ipv6),
2080 &ib_spec->ipv6.mask.src_ip,
2081 sizeof(ib_spec->ipv6.mask.src_ip));
Moses Reuben2d1e6972016-11-14 19:04:52 +02002082 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
Maor Gottlieb026bae02016-06-17 15:14:51 +03002083 src_ipv4_src_ipv6.ipv6_layout.ipv6),
2084 &ib_spec->ipv6.val.src_ip,
2085 sizeof(ib_spec->ipv6.val.src_ip));
Moses Reuben2d1e6972016-11-14 19:04:52 +02002086 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
Maor Gottlieb026bae02016-06-17 15:14:51 +03002087 dst_ipv4_dst_ipv6.ipv6_layout.ipv6),
2088 &ib_spec->ipv6.mask.dst_ip,
2089 sizeof(ib_spec->ipv6.mask.dst_ip));
Moses Reuben2d1e6972016-11-14 19:04:52 +02002090 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
Maor Gottlieb026bae02016-06-17 15:14:51 +03002091 dst_ipv4_dst_ipv6.ipv6_layout.ipv6),
2092 &ib_spec->ipv6.val.dst_ip,
2093 sizeof(ib_spec->ipv6.val.dst_ip));
Maor Gottlieb466fa6d2016-08-30 16:58:36 +03002094
Moses Reuben2d1e6972016-11-14 19:04:52 +02002095 set_tos(headers_c, headers_v,
Maor Gottlieb466fa6d2016-08-30 16:58:36 +03002096 ib_spec->ipv6.mask.traffic_class,
2097 ib_spec->ipv6.val.traffic_class);
2098
Moses Reuben2d1e6972016-11-14 19:04:52 +02002099 set_proto(headers_c, headers_v,
Maor Gottlieb466fa6d2016-08-30 16:58:36 +03002100 ib_spec->ipv6.mask.next_hdr,
2101 ib_spec->ipv6.val.next_hdr);
2102
Moses Reuben2d1e6972016-11-14 19:04:52 +02002103 set_flow_label(misc_params_c, misc_params_v,
2104 ntohl(ib_spec->ipv6.mask.flow_label),
2105 ntohl(ib_spec->ipv6.val.flow_label),
2106 ib_spec->type & IB_FLOW_SPEC_INNER);
2107
Maor Gottlieb026bae02016-06-17 15:14:51 +03002108 break;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002109 case IB_FLOW_SPEC_TCP:
Maor Gottliebc47ac6a2016-08-30 16:58:31 +03002110 if (FIELDS_NOT_SUPPORTED(ib_spec->tcp_udp.mask,
2111 LAST_TCP_UDP_FIELD))
Leon Romanovsky1ffd3a22017-01-18 14:59:51 +02002112 return -EOPNOTSUPP;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002113
Moses Reuben2d1e6972016-11-14 19:04:52 +02002114 MLX5_SET(fte_match_set_lyr_2_4, headers_c, ip_protocol,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002115 0xff);
Moses Reuben2d1e6972016-11-14 19:04:52 +02002116 MLX5_SET(fte_match_set_lyr_2_4, headers_v, ip_protocol,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002117 IPPROTO_TCP);
2118
Moses Reuben2d1e6972016-11-14 19:04:52 +02002119 MLX5_SET(fte_match_set_lyr_2_4, headers_c, tcp_sport,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002120 ntohs(ib_spec->tcp_udp.mask.src_port));
Moses Reuben2d1e6972016-11-14 19:04:52 +02002121 MLX5_SET(fte_match_set_lyr_2_4, headers_v, tcp_sport,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002122 ntohs(ib_spec->tcp_udp.val.src_port));
2123
Moses Reuben2d1e6972016-11-14 19:04:52 +02002124 MLX5_SET(fte_match_set_lyr_2_4, headers_c, tcp_dport,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002125 ntohs(ib_spec->tcp_udp.mask.dst_port));
Moses Reuben2d1e6972016-11-14 19:04:52 +02002126 MLX5_SET(fte_match_set_lyr_2_4, headers_v, tcp_dport,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002127 ntohs(ib_spec->tcp_udp.val.dst_port));
2128 break;
2129 case IB_FLOW_SPEC_UDP:
Maor Gottliebc47ac6a2016-08-30 16:58:31 +03002130 if (FIELDS_NOT_SUPPORTED(ib_spec->tcp_udp.mask,
2131 LAST_TCP_UDP_FIELD))
Leon Romanovsky1ffd3a22017-01-18 14:59:51 +02002132 return -EOPNOTSUPP;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002133
Moses Reuben2d1e6972016-11-14 19:04:52 +02002134 MLX5_SET(fte_match_set_lyr_2_4, headers_c, ip_protocol,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002135 0xff);
Moses Reuben2d1e6972016-11-14 19:04:52 +02002136 MLX5_SET(fte_match_set_lyr_2_4, headers_v, ip_protocol,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002137 IPPROTO_UDP);
2138
Moses Reuben2d1e6972016-11-14 19:04:52 +02002139 MLX5_SET(fte_match_set_lyr_2_4, headers_c, udp_sport,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002140 ntohs(ib_spec->tcp_udp.mask.src_port));
Moses Reuben2d1e6972016-11-14 19:04:52 +02002141 MLX5_SET(fte_match_set_lyr_2_4, headers_v, udp_sport,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002142 ntohs(ib_spec->tcp_udp.val.src_port));
2143
Moses Reuben2d1e6972016-11-14 19:04:52 +02002144 MLX5_SET(fte_match_set_lyr_2_4, headers_c, udp_dport,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002145 ntohs(ib_spec->tcp_udp.mask.dst_port));
Moses Reuben2d1e6972016-11-14 19:04:52 +02002146 MLX5_SET(fte_match_set_lyr_2_4, headers_v, udp_dport,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002147 ntohs(ib_spec->tcp_udp.val.dst_port));
2148 break;
Moses Reubenffb30d82016-11-14 19:04:50 +02002149 case IB_FLOW_SPEC_VXLAN_TUNNEL:
2150 if (FIELDS_NOT_SUPPORTED(ib_spec->tunnel.mask,
2151 LAST_TUNNEL_FIELD))
Leon Romanovsky1ffd3a22017-01-18 14:59:51 +02002152 return -EOPNOTSUPP;
Moses Reubenffb30d82016-11-14 19:04:50 +02002153
2154 MLX5_SET(fte_match_set_misc, misc_params_c, vxlan_vni,
2155 ntohl(ib_spec->tunnel.mask.tunnel_id));
2156 MLX5_SET(fte_match_set_misc, misc_params_v, vxlan_vni,
2157 ntohl(ib_spec->tunnel.val.tunnel_id));
2158 break;
Moses Reuben2ac693f2017-01-18 14:59:50 +02002159 case IB_FLOW_SPEC_ACTION_TAG:
2160 if (FIELDS_NOT_SUPPORTED(ib_spec->flow_tag,
2161 LAST_FLOW_TAG_FIELD))
2162 return -EOPNOTSUPP;
2163 if (ib_spec->flow_tag.tag_id >= BIT(24))
2164 return -EINVAL;
2165
2166 *tag_id = ib_spec->flow_tag.tag_id;
2167 break;
Slava Shwartsmana22ed862017-04-03 13:13:52 +03002168 case IB_FLOW_SPEC_ACTION_DROP:
2169 if (FIELDS_NOT_SUPPORTED(ib_spec->drop,
2170 LAST_DROP_FIELD))
2171 return -EOPNOTSUPP;
2172 *is_drop = true;
2173 break;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002174 default:
2175 return -EINVAL;
2176 }
2177
2178 return 0;
2179}
2180
2181/* If a flow could catch both multicast and unicast packets,
2182 * it won't fall into the multicast flow steering table and this rule
2183 * could steal other multicast packets.
2184 */
Yishai Hadasa550ddf2017-08-17 15:52:33 +03002185static bool flow_is_multicast_only(const struct ib_flow_attr *ib_attr)
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002186{
Yishai Hadas81e30882017-06-08 16:15:09 +03002187 union ib_flow_spec *flow_spec;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002188
2189 if (ib_attr->type != IB_FLOW_ATTR_NORMAL ||
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002190 ib_attr->num_of_specs < 1)
2191 return false;
2192
Yishai Hadas81e30882017-06-08 16:15:09 +03002193 flow_spec = (union ib_flow_spec *)(ib_attr + 1);
2194 if (flow_spec->type == IB_FLOW_SPEC_IPV4) {
2195 struct ib_flow_spec_ipv4 *ipv4_spec;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002196
Yishai Hadas81e30882017-06-08 16:15:09 +03002197 ipv4_spec = (struct ib_flow_spec_ipv4 *)flow_spec;
2198 if (ipv4_is_multicast(ipv4_spec->val.dst_ip))
2199 return true;
2200
2201 return false;
2202 }
2203
2204 if (flow_spec->type == IB_FLOW_SPEC_ETH) {
2205 struct ib_flow_spec_eth *eth_spec;
2206
2207 eth_spec = (struct ib_flow_spec_eth *)flow_spec;
2208 return is_multicast_ether_addr(eth_spec->mask.dst_mac) &&
2209 is_multicast_ether_addr(eth_spec->val.dst_mac);
2210 }
2211
2212 return false;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002213}
2214
Ariel Levkovich19cc7522017-04-03 13:11:03 +03002215static bool is_valid_ethertype(struct mlx5_core_dev *mdev,
2216 const struct ib_flow_attr *flow_attr,
Ariel Levkovich0f750962017-04-03 13:11:02 +03002217 bool check_inner)
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002218{
2219 union ib_flow_spec *ib_spec = (union ib_flow_spec *)(flow_attr + 1);
Ariel Levkovich19cc7522017-04-03 13:11:03 +03002220 int match_ipv = check_inner ?
2221 MLX5_CAP_FLOWTABLE_NIC_RX(mdev,
2222 ft_field_support.inner_ip_version) :
2223 MLX5_CAP_FLOWTABLE_NIC_RX(mdev,
2224 ft_field_support.outer_ip_version);
Ariel Levkovich0f750962017-04-03 13:11:02 +03002225 int inner_bit = check_inner ? IB_FLOW_SPEC_INNER : 0;
2226 bool ipv4_spec_valid, ipv6_spec_valid;
2227 unsigned int ip_spec_type = 0;
2228 bool has_ethertype = false;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002229 unsigned int spec_index;
Ariel Levkovich0f750962017-04-03 13:11:02 +03002230 bool mask_valid = true;
2231 u16 eth_type = 0;
2232 bool type_valid;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002233
2234 /* Validate that ethertype is correct */
2235 for (spec_index = 0; spec_index < flow_attr->num_of_specs; spec_index++) {
Ariel Levkovich0f750962017-04-03 13:11:02 +03002236 if ((ib_spec->type == (IB_FLOW_SPEC_ETH | inner_bit)) &&
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002237 ib_spec->eth.mask.ether_type) {
Ariel Levkovich0f750962017-04-03 13:11:02 +03002238 mask_valid = (ib_spec->eth.mask.ether_type ==
2239 htons(0xffff));
2240 has_ethertype = true;
2241 eth_type = ntohs(ib_spec->eth.val.ether_type);
2242 } else if ((ib_spec->type == (IB_FLOW_SPEC_IPV4 | inner_bit)) ||
2243 (ib_spec->type == (IB_FLOW_SPEC_IPV6 | inner_bit))) {
2244 ip_spec_type = ib_spec->type;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002245 }
2246 ib_spec = (void *)ib_spec + ib_spec->size;
2247 }
Ariel Levkovich0f750962017-04-03 13:11:02 +03002248
2249 type_valid = (!has_ethertype) || (!ip_spec_type);
2250 if (!type_valid && mask_valid) {
2251 ipv4_spec_valid = (eth_type == ETH_P_IP) &&
2252 (ip_spec_type == (IB_FLOW_SPEC_IPV4 | inner_bit));
2253 ipv6_spec_valid = (eth_type == ETH_P_IPV6) &&
2254 (ip_spec_type == (IB_FLOW_SPEC_IPV6 | inner_bit));
Ariel Levkovich19cc7522017-04-03 13:11:03 +03002255
2256 type_valid = (ipv4_spec_valid) || (ipv6_spec_valid) ||
2257 (((eth_type == ETH_P_MPLS_UC) ||
2258 (eth_type == ETH_P_MPLS_MC)) && match_ipv);
Ariel Levkovich0f750962017-04-03 13:11:02 +03002259 }
2260
2261 return type_valid;
2262}
2263
Ariel Levkovich19cc7522017-04-03 13:11:03 +03002264static bool is_valid_attr(struct mlx5_core_dev *mdev,
2265 const struct ib_flow_attr *flow_attr)
Ariel Levkovich0f750962017-04-03 13:11:02 +03002266{
Ariel Levkovich19cc7522017-04-03 13:11:03 +03002267 return is_valid_ethertype(mdev, flow_attr, false) &&
2268 is_valid_ethertype(mdev, flow_attr, true);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002269}
2270
2271static void put_flow_table(struct mlx5_ib_dev *dev,
2272 struct mlx5_ib_flow_prio *prio, bool ft_added)
2273{
2274 prio->refcount -= !!ft_added;
2275 if (!prio->refcount) {
2276 mlx5_destroy_flow_table(prio->flow_table);
2277 prio->flow_table = NULL;
2278 }
2279}
2280
2281static int mlx5_ib_destroy_flow(struct ib_flow *flow_id)
2282{
2283 struct mlx5_ib_dev *dev = to_mdev(flow_id->qp->device);
2284 struct mlx5_ib_flow_handler *handler = container_of(flow_id,
2285 struct mlx5_ib_flow_handler,
2286 ibflow);
2287 struct mlx5_ib_flow_handler *iter, *tmp;
2288
2289 mutex_lock(&dev->flow_db.lock);
2290
2291 list_for_each_entry_safe(iter, tmp, &handler->list, list) {
Mark Bloch74491de2016-08-31 11:24:25 +00002292 mlx5_del_flow_rules(iter->rule);
Maor Gottliebcc0e5d42016-08-28 14:16:34 +03002293 put_flow_table(dev, iter->prio, true);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002294 list_del(&iter->list);
2295 kfree(iter);
2296 }
2297
Mark Bloch74491de2016-08-31 11:24:25 +00002298 mlx5_del_flow_rules(handler->rule);
Maor Gottlieb5497adc2016-08-28 14:16:31 +03002299 put_flow_table(dev, handler->prio, true);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002300 mutex_unlock(&dev->flow_db.lock);
2301
2302 kfree(handler);
2303
2304 return 0;
2305}
2306
Maor Gottlieb35d190112016-03-07 18:51:47 +02002307static int ib_prio_to_core_prio(unsigned int priority, bool dont_trap)
2308{
2309 priority *= 2;
2310 if (!dont_trap)
2311 priority++;
2312 return priority;
2313}
2314
Maor Gottliebcc0e5d42016-08-28 14:16:34 +03002315enum flow_table_type {
2316 MLX5_IB_FT_RX,
2317 MLX5_IB_FT_TX
2318};
2319
Maor Gottlieb00b7c2a2017-03-29 06:09:01 +03002320#define MLX5_FS_MAX_TYPES 6
2321#define MLX5_FS_MAX_ENTRIES BIT(16)
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002322static struct mlx5_ib_flow_prio *get_flow_table(struct mlx5_ib_dev *dev,
Maor Gottliebcc0e5d42016-08-28 14:16:34 +03002323 struct ib_flow_attr *flow_attr,
2324 enum flow_table_type ft_type)
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002325{
Maor Gottlieb35d190112016-03-07 18:51:47 +02002326 bool dont_trap = flow_attr->flags & IB_FLOW_ATTR_FLAGS_DONT_TRAP;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002327 struct mlx5_flow_namespace *ns = NULL;
2328 struct mlx5_ib_flow_prio *prio;
2329 struct mlx5_flow_table *ft;
Maor Gottliebdac388e2017-03-29 06:09:00 +03002330 int max_table_size;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002331 int num_entries;
2332 int num_groups;
2333 int priority;
2334 int err = 0;
2335
Maor Gottliebdac388e2017-03-29 06:09:00 +03002336 max_table_size = BIT(MLX5_CAP_FLOWTABLE_NIC_RX(dev->mdev,
2337 log_max_ft_size));
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002338 if (flow_attr->type == IB_FLOW_ATTR_NORMAL) {
Maor Gottlieb35d190112016-03-07 18:51:47 +02002339 if (flow_is_multicast_only(flow_attr) &&
2340 !dont_trap)
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002341 priority = MLX5_IB_FLOW_MCAST_PRIO;
2342 else
Maor Gottlieb35d190112016-03-07 18:51:47 +02002343 priority = ib_prio_to_core_prio(flow_attr->priority,
2344 dont_trap);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002345 ns = mlx5_get_flow_namespace(dev->mdev,
2346 MLX5_FLOW_NAMESPACE_BYPASS);
2347 num_entries = MLX5_FS_MAX_ENTRIES;
2348 num_groups = MLX5_FS_MAX_TYPES;
2349 prio = &dev->flow_db.prios[priority];
2350 } else if (flow_attr->type == IB_FLOW_ATTR_ALL_DEFAULT ||
2351 flow_attr->type == IB_FLOW_ATTR_MC_DEFAULT) {
2352 ns = mlx5_get_flow_namespace(dev->mdev,
2353 MLX5_FLOW_NAMESPACE_LEFTOVERS);
2354 build_leftovers_ft_param(&priority,
2355 &num_entries,
2356 &num_groups);
2357 prio = &dev->flow_db.prios[MLX5_IB_FLOW_LEFTOVERS_PRIO];
Maor Gottliebcc0e5d42016-08-28 14:16:34 +03002358 } else if (flow_attr->type == IB_FLOW_ATTR_SNIFFER) {
2359 if (!MLX5_CAP_FLOWTABLE(dev->mdev,
2360 allow_sniffer_and_nic_rx_shared_tir))
2361 return ERR_PTR(-ENOTSUPP);
2362
2363 ns = mlx5_get_flow_namespace(dev->mdev, ft_type == MLX5_IB_FT_RX ?
2364 MLX5_FLOW_NAMESPACE_SNIFFER_RX :
2365 MLX5_FLOW_NAMESPACE_SNIFFER_TX);
2366
2367 prio = &dev->flow_db.sniffer[ft_type];
2368 priority = 0;
2369 num_entries = 1;
2370 num_groups = 1;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002371 }
2372
2373 if (!ns)
2374 return ERR_PTR(-ENOTSUPP);
2375
Maor Gottliebdac388e2017-03-29 06:09:00 +03002376 if (num_entries > max_table_size)
2377 return ERR_PTR(-ENOMEM);
2378
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002379 ft = prio->flow_table;
2380 if (!ft) {
2381 ft = mlx5_create_auto_grouped_flow_table(ns, priority,
2382 num_entries,
Maor Gottliebd63cd282016-04-29 01:36:35 +03002383 num_groups,
Hadar Hen Zionc9f1b072016-11-07 15:14:44 +02002384 0, 0);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002385
2386 if (!IS_ERR(ft)) {
2387 prio->refcount = 0;
2388 prio->flow_table = ft;
2389 } else {
2390 err = PTR_ERR(ft);
2391 }
2392 }
2393
2394 return err ? ERR_PTR(err) : prio;
2395}
2396
Yishai Hadasa550ddf2017-08-17 15:52:33 +03002397static void set_underlay_qp(struct mlx5_ib_dev *dev,
2398 struct mlx5_flow_spec *spec,
2399 u32 underlay_qpn)
2400{
2401 void *misc_params_c = MLX5_ADDR_OF(fte_match_param,
2402 spec->match_criteria,
2403 misc_parameters);
2404 void *misc_params_v = MLX5_ADDR_OF(fte_match_param, spec->match_value,
2405 misc_parameters);
2406
2407 if (underlay_qpn &&
2408 MLX5_CAP_FLOWTABLE_NIC_RX(dev->mdev,
2409 ft_field_support.bth_dst_qp)) {
2410 MLX5_SET(fte_match_set_misc,
2411 misc_params_v, bth_dst_qp, underlay_qpn);
2412 MLX5_SET(fte_match_set_misc,
2413 misc_params_c, bth_dst_qp, 0xffffff);
2414 }
2415}
2416
2417static struct mlx5_ib_flow_handler *_create_flow_rule(struct mlx5_ib_dev *dev,
2418 struct mlx5_ib_flow_prio *ft_prio,
2419 const struct ib_flow_attr *flow_attr,
2420 struct mlx5_flow_destination *dst,
2421 u32 underlay_qpn)
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002422{
2423 struct mlx5_flow_table *ft = ft_prio->flow_table;
2424 struct mlx5_ib_flow_handler *handler;
Hadar Hen Zion66958ed2016-11-07 15:14:45 +02002425 struct mlx5_flow_act flow_act = {0};
Maor Gottliebc5bb1732016-07-04 17:23:05 +03002426 struct mlx5_flow_spec *spec;
Slava Shwartsmana22ed862017-04-03 13:13:52 +03002427 struct mlx5_flow_destination *rule_dst = dst;
Maor Gottliebdd063d02016-08-28 14:16:32 +03002428 const void *ib_flow = (const void *)flow_attr + sizeof(*flow_attr);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002429 unsigned int spec_index;
Moses Reuben2ac693f2017-01-18 14:59:50 +02002430 u32 flow_tag = MLX5_FS_DEFAULT_FLOW_TAG;
Slava Shwartsmana22ed862017-04-03 13:13:52 +03002431 bool is_drop = false;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002432 int err = 0;
Slava Shwartsmana22ed862017-04-03 13:13:52 +03002433 int dest_num = 1;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002434
Ariel Levkovich19cc7522017-04-03 13:11:03 +03002435 if (!is_valid_attr(dev->mdev, flow_attr))
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002436 return ERR_PTR(-EINVAL);
2437
Leon Romanovsky1b9a07e2017-05-10 21:32:18 +03002438 spec = kvzalloc(sizeof(*spec), GFP_KERNEL);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002439 handler = kzalloc(sizeof(*handler), GFP_KERNEL);
Maor Gottliebc5bb1732016-07-04 17:23:05 +03002440 if (!handler || !spec) {
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002441 err = -ENOMEM;
2442 goto free;
2443 }
2444
2445 INIT_LIST_HEAD(&handler->list);
2446
2447 for (spec_index = 0; spec_index < flow_attr->num_of_specs; spec_index++) {
Ariel Levkovich19cc7522017-04-03 13:11:03 +03002448 err = parse_flow_attr(dev->mdev, spec->match_criteria,
Slava Shwartsmana22ed862017-04-03 13:13:52 +03002449 spec->match_value,
2450 ib_flow, &flow_tag, &is_drop);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002451 if (err < 0)
2452 goto free;
2453
2454 ib_flow += ((union ib_flow_spec *)ib_flow)->size;
2455 }
2456
Yishai Hadasa550ddf2017-08-17 15:52:33 +03002457 if (!flow_is_multicast_only(flow_attr))
2458 set_underlay_qp(dev, spec, underlay_qpn);
2459
Maor Gottlieb466fa6d2016-08-30 16:58:36 +03002460 spec->match_criteria_enable = get_match_criteria_enable(spec->match_criteria);
Slava Shwartsmana22ed862017-04-03 13:13:52 +03002461 if (is_drop) {
2462 flow_act.action = MLX5_FLOW_CONTEXT_ACTION_DROP;
2463 rule_dst = NULL;
2464 dest_num = 0;
2465 } else {
2466 flow_act.action = dst ? MLX5_FLOW_CONTEXT_ACTION_FWD_DEST :
2467 MLX5_FLOW_CONTEXT_ACTION_FWD_NEXT_PRIO;
2468 }
Moses Reuben2ac693f2017-01-18 14:59:50 +02002469
2470 if (flow_tag != MLX5_FS_DEFAULT_FLOW_TAG &&
2471 (flow_attr->type == IB_FLOW_ATTR_ALL_DEFAULT ||
2472 flow_attr->type == IB_FLOW_ATTR_MC_DEFAULT)) {
2473 mlx5_ib_warn(dev, "Flow tag %u and attribute type %x isn't allowed in leftovers\n",
2474 flow_tag, flow_attr->type);
2475 err = -EINVAL;
2476 goto free;
2477 }
2478 flow_act.flow_tag = flow_tag;
Mark Bloch74491de2016-08-31 11:24:25 +00002479 handler->rule = mlx5_add_flow_rules(ft, spec,
Hadar Hen Zion66958ed2016-11-07 15:14:45 +02002480 &flow_act,
Slava Shwartsmana22ed862017-04-03 13:13:52 +03002481 rule_dst, dest_num);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002482
2483 if (IS_ERR(handler->rule)) {
2484 err = PTR_ERR(handler->rule);
2485 goto free;
2486 }
2487
Maor Gottliebd9d49802016-08-28 14:16:33 +03002488 ft_prio->refcount++;
Maor Gottlieb5497adc2016-08-28 14:16:31 +03002489 handler->prio = ft_prio;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002490
2491 ft_prio->flow_table = ft;
2492free:
2493 if (err)
2494 kfree(handler);
Maor Gottliebc5bb1732016-07-04 17:23:05 +03002495 kvfree(spec);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002496 return err ? ERR_PTR(err) : handler;
2497}
2498
Yishai Hadasa550ddf2017-08-17 15:52:33 +03002499static struct mlx5_ib_flow_handler *create_flow_rule(struct mlx5_ib_dev *dev,
2500 struct mlx5_ib_flow_prio *ft_prio,
2501 const struct ib_flow_attr *flow_attr,
2502 struct mlx5_flow_destination *dst)
2503{
2504 return _create_flow_rule(dev, ft_prio, flow_attr, dst, 0);
2505}
2506
Maor Gottlieb35d190112016-03-07 18:51:47 +02002507static struct mlx5_ib_flow_handler *create_dont_trap_rule(struct mlx5_ib_dev *dev,
2508 struct mlx5_ib_flow_prio *ft_prio,
2509 struct ib_flow_attr *flow_attr,
2510 struct mlx5_flow_destination *dst)
2511{
2512 struct mlx5_ib_flow_handler *handler_dst = NULL;
2513 struct mlx5_ib_flow_handler *handler = NULL;
2514
2515 handler = create_flow_rule(dev, ft_prio, flow_attr, NULL);
2516 if (!IS_ERR(handler)) {
2517 handler_dst = create_flow_rule(dev, ft_prio,
2518 flow_attr, dst);
2519 if (IS_ERR(handler_dst)) {
Mark Bloch74491de2016-08-31 11:24:25 +00002520 mlx5_del_flow_rules(handler->rule);
Maor Gottliebd9d49802016-08-28 14:16:33 +03002521 ft_prio->refcount--;
Maor Gottlieb35d190112016-03-07 18:51:47 +02002522 kfree(handler);
2523 handler = handler_dst;
2524 } else {
2525 list_add(&handler_dst->list, &handler->list);
2526 }
2527 }
2528
2529 return handler;
2530}
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002531enum {
2532 LEFTOVERS_MC,
2533 LEFTOVERS_UC,
2534};
2535
2536static struct mlx5_ib_flow_handler *create_leftovers_rule(struct mlx5_ib_dev *dev,
2537 struct mlx5_ib_flow_prio *ft_prio,
2538 struct ib_flow_attr *flow_attr,
2539 struct mlx5_flow_destination *dst)
2540{
2541 struct mlx5_ib_flow_handler *handler_ucast = NULL;
2542 struct mlx5_ib_flow_handler *handler = NULL;
2543
2544 static struct {
2545 struct ib_flow_attr flow_attr;
2546 struct ib_flow_spec_eth eth_flow;
2547 } leftovers_specs[] = {
2548 [LEFTOVERS_MC] = {
2549 .flow_attr = {
2550 .num_of_specs = 1,
2551 .size = sizeof(leftovers_specs[0])
2552 },
2553 .eth_flow = {
2554 .type = IB_FLOW_SPEC_ETH,
2555 .size = sizeof(struct ib_flow_spec_eth),
2556 .mask = {.dst_mac = {0x1} },
2557 .val = {.dst_mac = {0x1} }
2558 }
2559 },
2560 [LEFTOVERS_UC] = {
2561 .flow_attr = {
2562 .num_of_specs = 1,
2563 .size = sizeof(leftovers_specs[0])
2564 },
2565 .eth_flow = {
2566 .type = IB_FLOW_SPEC_ETH,
2567 .size = sizeof(struct ib_flow_spec_eth),
2568 .mask = {.dst_mac = {0x1} },
2569 .val = {.dst_mac = {} }
2570 }
2571 }
2572 };
2573
2574 handler = create_flow_rule(dev, ft_prio,
2575 &leftovers_specs[LEFTOVERS_MC].flow_attr,
2576 dst);
2577 if (!IS_ERR(handler) &&
2578 flow_attr->type == IB_FLOW_ATTR_ALL_DEFAULT) {
2579 handler_ucast = create_flow_rule(dev, ft_prio,
2580 &leftovers_specs[LEFTOVERS_UC].flow_attr,
2581 dst);
2582 if (IS_ERR(handler_ucast)) {
Mark Bloch74491de2016-08-31 11:24:25 +00002583 mlx5_del_flow_rules(handler->rule);
Maor Gottliebd9d49802016-08-28 14:16:33 +03002584 ft_prio->refcount--;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002585 kfree(handler);
2586 handler = handler_ucast;
2587 } else {
2588 list_add(&handler_ucast->list, &handler->list);
2589 }
2590 }
2591
2592 return handler;
2593}
2594
Maor Gottliebcc0e5d42016-08-28 14:16:34 +03002595static struct mlx5_ib_flow_handler *create_sniffer_rule(struct mlx5_ib_dev *dev,
2596 struct mlx5_ib_flow_prio *ft_rx,
2597 struct mlx5_ib_flow_prio *ft_tx,
2598 struct mlx5_flow_destination *dst)
2599{
2600 struct mlx5_ib_flow_handler *handler_rx;
2601 struct mlx5_ib_flow_handler *handler_tx;
2602 int err;
2603 static const struct ib_flow_attr flow_attr = {
2604 .num_of_specs = 0,
2605 .size = sizeof(flow_attr)
2606 };
2607
2608 handler_rx = create_flow_rule(dev, ft_rx, &flow_attr, dst);
2609 if (IS_ERR(handler_rx)) {
2610 err = PTR_ERR(handler_rx);
2611 goto err;
2612 }
2613
2614 handler_tx = create_flow_rule(dev, ft_tx, &flow_attr, dst);
2615 if (IS_ERR(handler_tx)) {
2616 err = PTR_ERR(handler_tx);
2617 goto err_tx;
2618 }
2619
2620 list_add(&handler_tx->list, &handler_rx->list);
2621
2622 return handler_rx;
2623
2624err_tx:
Mark Bloch74491de2016-08-31 11:24:25 +00002625 mlx5_del_flow_rules(handler_rx->rule);
Maor Gottliebcc0e5d42016-08-28 14:16:34 +03002626 ft_rx->refcount--;
2627 kfree(handler_rx);
2628err:
2629 return ERR_PTR(err);
2630}
2631
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002632static struct ib_flow *mlx5_ib_create_flow(struct ib_qp *qp,
2633 struct ib_flow_attr *flow_attr,
2634 int domain)
2635{
2636 struct mlx5_ib_dev *dev = to_mdev(qp->device);
Yishai Hadasd9f88e52016-08-28 10:58:37 +03002637 struct mlx5_ib_qp *mqp = to_mqp(qp);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002638 struct mlx5_ib_flow_handler *handler = NULL;
2639 struct mlx5_flow_destination *dst = NULL;
Maor Gottliebcc0e5d42016-08-28 14:16:34 +03002640 struct mlx5_ib_flow_prio *ft_prio_tx = NULL;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002641 struct mlx5_ib_flow_prio *ft_prio;
2642 int err;
Yishai Hadasa550ddf2017-08-17 15:52:33 +03002643 int underlay_qpn;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002644
2645 if (flow_attr->priority > MLX5_IB_FLOW_LAST_PRIO)
Maor Gottliebdac388e2017-03-29 06:09:00 +03002646 return ERR_PTR(-ENOMEM);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002647
2648 if (domain != IB_FLOW_DOMAIN_USER ||
2649 flow_attr->port > MLX5_CAP_GEN(dev->mdev, num_ports) ||
Maor Gottlieb35d190112016-03-07 18:51:47 +02002650 (flow_attr->flags & ~IB_FLOW_ATTR_FLAGS_DONT_TRAP))
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002651 return ERR_PTR(-EINVAL);
2652
2653 dst = kzalloc(sizeof(*dst), GFP_KERNEL);
2654 if (!dst)
2655 return ERR_PTR(-ENOMEM);
2656
2657 mutex_lock(&dev->flow_db.lock);
2658
Maor Gottliebcc0e5d42016-08-28 14:16:34 +03002659 ft_prio = get_flow_table(dev, flow_attr, MLX5_IB_FT_RX);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002660 if (IS_ERR(ft_prio)) {
2661 err = PTR_ERR(ft_prio);
2662 goto unlock;
2663 }
Maor Gottliebcc0e5d42016-08-28 14:16:34 +03002664 if (flow_attr->type == IB_FLOW_ATTR_SNIFFER) {
2665 ft_prio_tx = get_flow_table(dev, flow_attr, MLX5_IB_FT_TX);
2666 if (IS_ERR(ft_prio_tx)) {
2667 err = PTR_ERR(ft_prio_tx);
2668 ft_prio_tx = NULL;
2669 goto destroy_ft;
2670 }
2671 }
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002672
2673 dst->type = MLX5_FLOW_DESTINATION_TYPE_TIR;
Yishai Hadasd9f88e52016-08-28 10:58:37 +03002674 if (mqp->flags & MLX5_IB_QP_RSS)
2675 dst->tir_num = mqp->rss_qp.tirn;
2676 else
2677 dst->tir_num = mqp->raw_packet_qp.rq.tirn;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002678
2679 if (flow_attr->type == IB_FLOW_ATTR_NORMAL) {
Maor Gottlieb35d190112016-03-07 18:51:47 +02002680 if (flow_attr->flags & IB_FLOW_ATTR_FLAGS_DONT_TRAP) {
2681 handler = create_dont_trap_rule(dev, ft_prio,
2682 flow_attr, dst);
2683 } else {
Yishai Hadasa550ddf2017-08-17 15:52:33 +03002684 underlay_qpn = (mqp->flags & MLX5_IB_QP_UNDERLAY) ?
2685 mqp->underlay_qpn : 0;
2686 handler = _create_flow_rule(dev, ft_prio, flow_attr,
2687 dst, underlay_qpn);
Maor Gottlieb35d190112016-03-07 18:51:47 +02002688 }
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002689 } else if (flow_attr->type == IB_FLOW_ATTR_ALL_DEFAULT ||
2690 flow_attr->type == IB_FLOW_ATTR_MC_DEFAULT) {
2691 handler = create_leftovers_rule(dev, ft_prio, flow_attr,
2692 dst);
Maor Gottliebcc0e5d42016-08-28 14:16:34 +03002693 } else if (flow_attr->type == IB_FLOW_ATTR_SNIFFER) {
2694 handler = create_sniffer_rule(dev, ft_prio, ft_prio_tx, dst);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002695 } else {
2696 err = -EINVAL;
2697 goto destroy_ft;
2698 }
2699
2700 if (IS_ERR(handler)) {
2701 err = PTR_ERR(handler);
2702 handler = NULL;
2703 goto destroy_ft;
2704 }
2705
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002706 mutex_unlock(&dev->flow_db.lock);
2707 kfree(dst);
2708
2709 return &handler->ibflow;
2710
2711destroy_ft:
2712 put_flow_table(dev, ft_prio, false);
Maor Gottliebcc0e5d42016-08-28 14:16:34 +03002713 if (ft_prio_tx)
2714 put_flow_table(dev, ft_prio_tx, false);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002715unlock:
2716 mutex_unlock(&dev->flow_db.lock);
2717 kfree(dst);
2718 kfree(handler);
2719 return ERR_PTR(err);
2720}
2721
Eli Cohene126ba92013-07-07 17:25:49 +03002722static int mlx5_ib_mcg_attach(struct ib_qp *ibqp, union ib_gid *gid, u16 lid)
2723{
2724 struct mlx5_ib_dev *dev = to_mdev(ibqp->device);
Yishai Hadas81e30882017-06-08 16:15:09 +03002725 struct mlx5_ib_qp *mqp = to_mqp(ibqp);
Eli Cohene126ba92013-07-07 17:25:49 +03002726 int err;
2727
Yishai Hadas81e30882017-06-08 16:15:09 +03002728 if (mqp->flags & MLX5_IB_QP_UNDERLAY) {
2729 mlx5_ib_dbg(dev, "Attaching a multi cast group to underlay QP is not supported\n");
2730 return -EOPNOTSUPP;
2731 }
2732
Jack Morgenstein9603b612014-07-28 23:30:22 +03002733 err = mlx5_core_attach_mcg(dev->mdev, gid, ibqp->qp_num);
Eli Cohene126ba92013-07-07 17:25:49 +03002734 if (err)
2735 mlx5_ib_warn(dev, "failed attaching QPN 0x%x, MGID %pI6\n",
2736 ibqp->qp_num, gid->raw);
2737
2738 return err;
2739}
2740
2741static int mlx5_ib_mcg_detach(struct ib_qp *ibqp, union ib_gid *gid, u16 lid)
2742{
2743 struct mlx5_ib_dev *dev = to_mdev(ibqp->device);
2744 int err;
2745
Jack Morgenstein9603b612014-07-28 23:30:22 +03002746 err = mlx5_core_detach_mcg(dev->mdev, gid, ibqp->qp_num);
Eli Cohene126ba92013-07-07 17:25:49 +03002747 if (err)
2748 mlx5_ib_warn(dev, "failed detaching QPN 0x%x, MGID %pI6\n",
2749 ibqp->qp_num, gid->raw);
2750
2751 return err;
2752}
2753
2754static int init_node_data(struct mlx5_ib_dev *dev)
2755{
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03002756 int err;
Eli Cohene126ba92013-07-07 17:25:49 +03002757
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03002758 err = mlx5_query_node_desc(dev, dev->ib_dev.node_desc);
Eli Cohene126ba92013-07-07 17:25:49 +03002759 if (err)
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03002760 return err;
Eli Cohene126ba92013-07-07 17:25:49 +03002761
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03002762 dev->mdev->rev_id = dev->mdev->pdev->revision;
Eli Cohene126ba92013-07-07 17:25:49 +03002763
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03002764 return mlx5_query_node_guid(dev, &dev->ib_dev.node_guid);
Eli Cohene126ba92013-07-07 17:25:49 +03002765}
2766
2767static ssize_t show_fw_pages(struct device *device, struct device_attribute *attr,
2768 char *buf)
2769{
2770 struct mlx5_ib_dev *dev =
2771 container_of(device, struct mlx5_ib_dev, ib_dev.dev);
2772
Jack Morgenstein9603b612014-07-28 23:30:22 +03002773 return sprintf(buf, "%d\n", dev->mdev->priv.fw_pages);
Eli Cohene126ba92013-07-07 17:25:49 +03002774}
2775
2776static ssize_t show_reg_pages(struct device *device,
2777 struct device_attribute *attr, char *buf)
2778{
2779 struct mlx5_ib_dev *dev =
2780 container_of(device, struct mlx5_ib_dev, ib_dev.dev);
2781
Haggai Eran6aec21f2014-12-11 17:04:23 +02002782 return sprintf(buf, "%d\n", atomic_read(&dev->mdev->priv.reg_pages));
Eli Cohene126ba92013-07-07 17:25:49 +03002783}
2784
2785static ssize_t show_hca(struct device *device, struct device_attribute *attr,
2786 char *buf)
2787{
2788 struct mlx5_ib_dev *dev =
2789 container_of(device, struct mlx5_ib_dev, ib_dev.dev);
Jack Morgenstein9603b612014-07-28 23:30:22 +03002790 return sprintf(buf, "MT%d\n", dev->mdev->pdev->device);
Eli Cohene126ba92013-07-07 17:25:49 +03002791}
2792
Eli Cohene126ba92013-07-07 17:25:49 +03002793static ssize_t show_rev(struct device *device, struct device_attribute *attr,
2794 char *buf)
2795{
2796 struct mlx5_ib_dev *dev =
2797 container_of(device, struct mlx5_ib_dev, ib_dev.dev);
Jack Morgenstein9603b612014-07-28 23:30:22 +03002798 return sprintf(buf, "%x\n", dev->mdev->rev_id);
Eli Cohene126ba92013-07-07 17:25:49 +03002799}
2800
2801static ssize_t show_board(struct device *device, struct device_attribute *attr,
2802 char *buf)
2803{
2804 struct mlx5_ib_dev *dev =
2805 container_of(device, struct mlx5_ib_dev, ib_dev.dev);
2806 return sprintf(buf, "%.*s\n", MLX5_BOARD_ID_LEN,
Jack Morgenstein9603b612014-07-28 23:30:22 +03002807 dev->mdev->board_id);
Eli Cohene126ba92013-07-07 17:25:49 +03002808}
2809
2810static DEVICE_ATTR(hw_rev, S_IRUGO, show_rev, NULL);
Eli Cohene126ba92013-07-07 17:25:49 +03002811static DEVICE_ATTR(hca_type, S_IRUGO, show_hca, NULL);
2812static DEVICE_ATTR(board_id, S_IRUGO, show_board, NULL);
2813static DEVICE_ATTR(fw_pages, S_IRUGO, show_fw_pages, NULL);
2814static DEVICE_ATTR(reg_pages, S_IRUGO, show_reg_pages, NULL);
2815
2816static struct device_attribute *mlx5_class_attributes[] = {
2817 &dev_attr_hw_rev,
Eli Cohene126ba92013-07-07 17:25:49 +03002818 &dev_attr_hca_type,
2819 &dev_attr_board_id,
2820 &dev_attr_fw_pages,
2821 &dev_attr_reg_pages,
2822};
2823
Haggai Eran7722f472016-02-29 15:45:07 +02002824static void pkey_change_handler(struct work_struct *work)
2825{
2826 struct mlx5_ib_port_resources *ports =
2827 container_of(work, struct mlx5_ib_port_resources,
2828 pkey_change_work);
2829
2830 mutex_lock(&ports->devr->mutex);
2831 mlx5_ib_gsi_pkey_change(ports->gsi);
2832 mutex_unlock(&ports->devr->mutex);
2833}
2834
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03002835static void mlx5_ib_handle_internal_error(struct mlx5_ib_dev *ibdev)
2836{
2837 struct mlx5_ib_qp *mqp;
2838 struct mlx5_ib_cq *send_mcq, *recv_mcq;
2839 struct mlx5_core_cq *mcq;
2840 struct list_head cq_armed_list;
2841 unsigned long flags_qp;
2842 unsigned long flags_cq;
2843 unsigned long flags;
2844
2845 INIT_LIST_HEAD(&cq_armed_list);
2846
2847 /* Go over qp list reside on that ibdev, sync with create/destroy qp.*/
2848 spin_lock_irqsave(&ibdev->reset_flow_resource_lock, flags);
2849 list_for_each_entry(mqp, &ibdev->qp_list, qps_list) {
2850 spin_lock_irqsave(&mqp->sq.lock, flags_qp);
2851 if (mqp->sq.tail != mqp->sq.head) {
2852 send_mcq = to_mcq(mqp->ibqp.send_cq);
2853 spin_lock_irqsave(&send_mcq->lock, flags_cq);
2854 if (send_mcq->mcq.comp &&
2855 mqp->ibqp.send_cq->comp_handler) {
2856 if (!send_mcq->mcq.reset_notify_added) {
2857 send_mcq->mcq.reset_notify_added = 1;
2858 list_add_tail(&send_mcq->mcq.reset_notify,
2859 &cq_armed_list);
2860 }
2861 }
2862 spin_unlock_irqrestore(&send_mcq->lock, flags_cq);
2863 }
2864 spin_unlock_irqrestore(&mqp->sq.lock, flags_qp);
2865 spin_lock_irqsave(&mqp->rq.lock, flags_qp);
2866 /* no handling is needed for SRQ */
2867 if (!mqp->ibqp.srq) {
2868 if (mqp->rq.tail != mqp->rq.head) {
2869 recv_mcq = to_mcq(mqp->ibqp.recv_cq);
2870 spin_lock_irqsave(&recv_mcq->lock, flags_cq);
2871 if (recv_mcq->mcq.comp &&
2872 mqp->ibqp.recv_cq->comp_handler) {
2873 if (!recv_mcq->mcq.reset_notify_added) {
2874 recv_mcq->mcq.reset_notify_added = 1;
2875 list_add_tail(&recv_mcq->mcq.reset_notify,
2876 &cq_armed_list);
2877 }
2878 }
2879 spin_unlock_irqrestore(&recv_mcq->lock,
2880 flags_cq);
2881 }
2882 }
2883 spin_unlock_irqrestore(&mqp->rq.lock, flags_qp);
2884 }
2885 /*At that point all inflight post send were put to be executed as of we
2886 * lock/unlock above locks Now need to arm all involved CQs.
2887 */
2888 list_for_each_entry(mcq, &cq_armed_list, reset_notify) {
2889 mcq->comp(mcq);
2890 }
2891 spin_unlock_irqrestore(&ibdev->reset_flow_resource_lock, flags);
2892}
2893
Maor Gottlieb03404e82017-05-30 10:29:13 +03002894static void delay_drop_handler(struct work_struct *work)
2895{
2896 int err;
2897 struct mlx5_ib_delay_drop *delay_drop =
2898 container_of(work, struct mlx5_ib_delay_drop,
2899 delay_drop_work);
2900
Maor Gottliebfe248c32017-05-30 10:29:14 +03002901 atomic_inc(&delay_drop->events_cnt);
2902
Maor Gottlieb03404e82017-05-30 10:29:13 +03002903 mutex_lock(&delay_drop->lock);
2904 err = mlx5_core_set_delay_drop(delay_drop->dev->mdev,
2905 delay_drop->timeout);
2906 if (err) {
2907 mlx5_ib_warn(delay_drop->dev, "Failed to set delay drop, timeout=%u\n",
2908 delay_drop->timeout);
2909 delay_drop->activate = false;
2910 }
2911 mutex_unlock(&delay_drop->lock);
2912}
2913
Jack Morgenstein9603b612014-07-28 23:30:22 +03002914static void mlx5_ib_event(struct mlx5_core_dev *dev, void *context,
Jack Morgenstein4d2f9bb2014-07-28 23:30:24 +03002915 enum mlx5_dev_event event, unsigned long param)
Eli Cohene126ba92013-07-07 17:25:49 +03002916{
Jack Morgenstein9603b612014-07-28 23:30:22 +03002917 struct mlx5_ib_dev *ibdev = (struct mlx5_ib_dev *)context;
Eli Cohene126ba92013-07-07 17:25:49 +03002918 struct ib_event ibev;
Eli Cohendbaaff22016-10-27 16:36:44 +03002919 bool fatal = false;
Eli Cohene126ba92013-07-07 17:25:49 +03002920 u8 port = 0;
2921
2922 switch (event) {
2923 case MLX5_DEV_EVENT_SYS_ERROR:
Eli Cohene126ba92013-07-07 17:25:49 +03002924 ibev.event = IB_EVENT_DEVICE_FATAL;
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03002925 mlx5_ib_handle_internal_error(ibdev);
Eli Cohendbaaff22016-10-27 16:36:44 +03002926 fatal = true;
Eli Cohene126ba92013-07-07 17:25:49 +03002927 break;
2928
2929 case MLX5_DEV_EVENT_PORT_UP:
Eli Cohene126ba92013-07-07 17:25:49 +03002930 case MLX5_DEV_EVENT_PORT_DOWN:
Noa Osherovich2788cf32016-06-04 15:15:29 +03002931 case MLX5_DEV_EVENT_PORT_INITIALIZED:
Jack Morgenstein4d2f9bb2014-07-28 23:30:24 +03002932 port = (u8)param;
Aviv Heller5ec8c832016-09-18 20:48:00 +03002933
2934 /* In RoCE, port up/down events are handled in
2935 * mlx5_netdev_event().
2936 */
2937 if (mlx5_ib_port_link_layer(&ibdev->ib_dev, port) ==
2938 IB_LINK_LAYER_ETHERNET)
2939 return;
2940
2941 ibev.event = (event == MLX5_DEV_EVENT_PORT_UP) ?
2942 IB_EVENT_PORT_ACTIVE : IB_EVENT_PORT_ERR;
Eli Cohene126ba92013-07-07 17:25:49 +03002943 break;
2944
Eli Cohene126ba92013-07-07 17:25:49 +03002945 case MLX5_DEV_EVENT_LID_CHANGE:
2946 ibev.event = IB_EVENT_LID_CHANGE;
Jack Morgenstein4d2f9bb2014-07-28 23:30:24 +03002947 port = (u8)param;
Eli Cohene126ba92013-07-07 17:25:49 +03002948 break;
2949
2950 case MLX5_DEV_EVENT_PKEY_CHANGE:
2951 ibev.event = IB_EVENT_PKEY_CHANGE;
Jack Morgenstein4d2f9bb2014-07-28 23:30:24 +03002952 port = (u8)param;
Haggai Eran7722f472016-02-29 15:45:07 +02002953
2954 schedule_work(&ibdev->devr.ports[port - 1].pkey_change_work);
Eli Cohene126ba92013-07-07 17:25:49 +03002955 break;
2956
2957 case MLX5_DEV_EVENT_GUID_CHANGE:
2958 ibev.event = IB_EVENT_GID_CHANGE;
Jack Morgenstein4d2f9bb2014-07-28 23:30:24 +03002959 port = (u8)param;
Eli Cohene126ba92013-07-07 17:25:49 +03002960 break;
2961
2962 case MLX5_DEV_EVENT_CLIENT_REREG:
2963 ibev.event = IB_EVENT_CLIENT_REREGISTER;
Jack Morgenstein4d2f9bb2014-07-28 23:30:24 +03002964 port = (u8)param;
Eli Cohene126ba92013-07-07 17:25:49 +03002965 break;
Maor Gottlieb03404e82017-05-30 10:29:13 +03002966 case MLX5_DEV_EVENT_DELAY_DROP_TIMEOUT:
2967 schedule_work(&ibdev->delay_drop.delay_drop_work);
2968 goto out;
Saeed Mahameedbdc37922016-09-29 19:35:38 +03002969 default:
Maor Gottlieb03404e82017-05-30 10:29:13 +03002970 goto out;
Eli Cohene126ba92013-07-07 17:25:49 +03002971 }
2972
2973 ibev.device = &ibdev->ib_dev;
2974 ibev.element.port_num = port;
2975
Eli Cohena0c84c32013-09-11 16:35:27 +03002976 if (port < 1 || port > ibdev->num_ports) {
2977 mlx5_ib_warn(ibdev, "warning: event on port %d\n", port);
Maor Gottlieb03404e82017-05-30 10:29:13 +03002978 goto out;
Eli Cohena0c84c32013-09-11 16:35:27 +03002979 }
2980
Eli Cohene126ba92013-07-07 17:25:49 +03002981 if (ibdev->ib_active)
2982 ib_dispatch_event(&ibev);
Eli Cohendbaaff22016-10-27 16:36:44 +03002983
2984 if (fatal)
2985 ibdev->ib_active = false;
Maor Gottlieb03404e82017-05-30 10:29:13 +03002986
2987out:
2988 return;
Eli Cohene126ba92013-07-07 17:25:49 +03002989}
2990
Maor Gottliebc43f1112017-01-18 14:10:33 +02002991static int set_has_smi_cap(struct mlx5_ib_dev *dev)
2992{
2993 struct mlx5_hca_vport_context vport_ctx;
2994 int err;
2995 int port;
2996
2997 for (port = 1; port <= MLX5_CAP_GEN(dev->mdev, num_ports); port++) {
2998 dev->mdev->port_caps[port - 1].has_smi = false;
2999 if (MLX5_CAP_GEN(dev->mdev, port_type) ==
3000 MLX5_CAP_PORT_TYPE_IB) {
3001 if (MLX5_CAP_GEN(dev->mdev, ib_virt)) {
3002 err = mlx5_query_hca_vport_context(dev->mdev, 0,
3003 port, 0,
3004 &vport_ctx);
3005 if (err) {
3006 mlx5_ib_err(dev, "query_hca_vport_context for port=%d failed %d\n",
3007 port, err);
3008 return err;
3009 }
3010 dev->mdev->port_caps[port - 1].has_smi =
3011 vport_ctx.has_smi;
3012 } else {
3013 dev->mdev->port_caps[port - 1].has_smi = true;
3014 }
3015 }
3016 }
3017 return 0;
3018}
3019
Eli Cohene126ba92013-07-07 17:25:49 +03003020static void get_ext_port_caps(struct mlx5_ib_dev *dev)
3021{
3022 int port;
3023
Saeed Mahameed938fe832015-05-28 22:28:41 +03003024 for (port = 1; port <= MLX5_CAP_GEN(dev->mdev, num_ports); port++)
Eli Cohene126ba92013-07-07 17:25:49 +03003025 mlx5_query_ext_port_caps(dev, port);
3026}
3027
3028static int get_port_caps(struct mlx5_ib_dev *dev)
3029{
3030 struct ib_device_attr *dprops = NULL;
3031 struct ib_port_attr *pprops = NULL;
Dan Carpenterf614fc12015-01-12 11:56:58 +03003032 int err = -ENOMEM;
Eli Cohene126ba92013-07-07 17:25:49 +03003033 int port;
Matan Barak2528e332015-06-11 16:35:25 +03003034 struct ib_udata uhw = {.inlen = 0, .outlen = 0};
Eli Cohene126ba92013-07-07 17:25:49 +03003035
3036 pprops = kmalloc(sizeof(*pprops), GFP_KERNEL);
3037 if (!pprops)
3038 goto out;
3039
3040 dprops = kmalloc(sizeof(*dprops), GFP_KERNEL);
3041 if (!dprops)
3042 goto out;
3043
Maor Gottliebc43f1112017-01-18 14:10:33 +02003044 err = set_has_smi_cap(dev);
3045 if (err)
3046 goto out;
3047
Matan Barak2528e332015-06-11 16:35:25 +03003048 err = mlx5_ib_query_device(&dev->ib_dev, dprops, &uhw);
Eli Cohene126ba92013-07-07 17:25:49 +03003049 if (err) {
3050 mlx5_ib_warn(dev, "query_device failed %d\n", err);
3051 goto out;
3052 }
3053
Saeed Mahameed938fe832015-05-28 22:28:41 +03003054 for (port = 1; port <= MLX5_CAP_GEN(dev->mdev, num_ports); port++) {
Or Gerlitzc4550c62017-01-24 13:02:39 +02003055 memset(pprops, 0, sizeof(*pprops));
Eli Cohene126ba92013-07-07 17:25:49 +03003056 err = mlx5_ib_query_port(&dev->ib_dev, port, pprops);
3057 if (err) {
Saeed Mahameed938fe832015-05-28 22:28:41 +03003058 mlx5_ib_warn(dev, "query_port %d failed %d\n",
3059 port, err);
Eli Cohene126ba92013-07-07 17:25:49 +03003060 break;
3061 }
Saeed Mahameed938fe832015-05-28 22:28:41 +03003062 dev->mdev->port_caps[port - 1].pkey_table_len =
3063 dprops->max_pkeys;
3064 dev->mdev->port_caps[port - 1].gid_table_len =
3065 pprops->gid_tbl_len;
Eli Cohene126ba92013-07-07 17:25:49 +03003066 mlx5_ib_dbg(dev, "pkey_table_len %d, gid_table_len %d\n",
3067 dprops->max_pkeys, pprops->gid_tbl_len);
3068 }
3069
3070out:
3071 kfree(pprops);
3072 kfree(dprops);
3073
3074 return err;
3075}
3076
3077static void destroy_umrc_res(struct mlx5_ib_dev *dev)
3078{
3079 int err;
3080
3081 err = mlx5_mr_cache_cleanup(dev);
3082 if (err)
3083 mlx5_ib_warn(dev, "mr cache cleanup failed\n");
3084
3085 mlx5_ib_destroy_qp(dev->umrc.qp);
Christoph Hellwigadd08d72016-03-03 09:38:22 +01003086 ib_free_cq(dev->umrc.cq);
Eli Cohene126ba92013-07-07 17:25:49 +03003087 ib_dealloc_pd(dev->umrc.pd);
3088}
3089
3090enum {
3091 MAX_UMR_WR = 128,
3092};
3093
3094static int create_umr_res(struct mlx5_ib_dev *dev)
3095{
3096 struct ib_qp_init_attr *init_attr = NULL;
3097 struct ib_qp_attr *attr = NULL;
3098 struct ib_pd *pd;
3099 struct ib_cq *cq;
3100 struct ib_qp *qp;
Eli Cohene126ba92013-07-07 17:25:49 +03003101 int ret;
3102
3103 attr = kzalloc(sizeof(*attr), GFP_KERNEL);
3104 init_attr = kzalloc(sizeof(*init_attr), GFP_KERNEL);
3105 if (!attr || !init_attr) {
3106 ret = -ENOMEM;
3107 goto error_0;
3108 }
3109
Christoph Hellwiged082d32016-09-05 12:56:17 +02003110 pd = ib_alloc_pd(&dev->ib_dev, 0);
Eli Cohene126ba92013-07-07 17:25:49 +03003111 if (IS_ERR(pd)) {
3112 mlx5_ib_dbg(dev, "Couldn't create PD for sync UMR QP\n");
3113 ret = PTR_ERR(pd);
3114 goto error_0;
3115 }
3116
Christoph Hellwigadd08d72016-03-03 09:38:22 +01003117 cq = ib_alloc_cq(&dev->ib_dev, NULL, 128, 0, IB_POLL_SOFTIRQ);
Eli Cohene126ba92013-07-07 17:25:49 +03003118 if (IS_ERR(cq)) {
3119 mlx5_ib_dbg(dev, "Couldn't create CQ for sync UMR QP\n");
3120 ret = PTR_ERR(cq);
3121 goto error_2;
3122 }
Eli Cohene126ba92013-07-07 17:25:49 +03003123
3124 init_attr->send_cq = cq;
3125 init_attr->recv_cq = cq;
3126 init_attr->sq_sig_type = IB_SIGNAL_ALL_WR;
3127 init_attr->cap.max_send_wr = MAX_UMR_WR;
3128 init_attr->cap.max_send_sge = 1;
3129 init_attr->qp_type = MLX5_IB_QPT_REG_UMR;
3130 init_attr->port_num = 1;
3131 qp = mlx5_ib_create_qp(pd, init_attr, NULL);
3132 if (IS_ERR(qp)) {
3133 mlx5_ib_dbg(dev, "Couldn't create sync UMR QP\n");
3134 ret = PTR_ERR(qp);
3135 goto error_3;
3136 }
3137 qp->device = &dev->ib_dev;
3138 qp->real_qp = qp;
3139 qp->uobject = NULL;
3140 qp->qp_type = MLX5_IB_QPT_REG_UMR;
Majd Dibbiny31fde032017-10-30 14:23:13 +02003141 qp->send_cq = init_attr->send_cq;
3142 qp->recv_cq = init_attr->recv_cq;
Eli Cohene126ba92013-07-07 17:25:49 +03003143
3144 attr->qp_state = IB_QPS_INIT;
3145 attr->port_num = 1;
3146 ret = mlx5_ib_modify_qp(qp, attr, IB_QP_STATE | IB_QP_PKEY_INDEX |
3147 IB_QP_PORT, NULL);
3148 if (ret) {
3149 mlx5_ib_dbg(dev, "Couldn't modify UMR QP\n");
3150 goto error_4;
3151 }
3152
3153 memset(attr, 0, sizeof(*attr));
3154 attr->qp_state = IB_QPS_RTR;
3155 attr->path_mtu = IB_MTU_256;
3156
3157 ret = mlx5_ib_modify_qp(qp, attr, IB_QP_STATE, NULL);
3158 if (ret) {
3159 mlx5_ib_dbg(dev, "Couldn't modify umr QP to rtr\n");
3160 goto error_4;
3161 }
3162
3163 memset(attr, 0, sizeof(*attr));
3164 attr->qp_state = IB_QPS_RTS;
3165 ret = mlx5_ib_modify_qp(qp, attr, IB_QP_STATE, NULL);
3166 if (ret) {
3167 mlx5_ib_dbg(dev, "Couldn't modify umr QP to rts\n");
3168 goto error_4;
3169 }
3170
3171 dev->umrc.qp = qp;
3172 dev->umrc.cq = cq;
Eli Cohene126ba92013-07-07 17:25:49 +03003173 dev->umrc.pd = pd;
3174
3175 sema_init(&dev->umrc.sem, MAX_UMR_WR);
3176 ret = mlx5_mr_cache_init(dev);
3177 if (ret) {
3178 mlx5_ib_warn(dev, "mr cache init failed %d\n", ret);
3179 goto error_4;
3180 }
3181
3182 kfree(attr);
3183 kfree(init_attr);
3184
3185 return 0;
3186
3187error_4:
3188 mlx5_ib_destroy_qp(qp);
3189
3190error_3:
Christoph Hellwigadd08d72016-03-03 09:38:22 +01003191 ib_free_cq(cq);
Eli Cohene126ba92013-07-07 17:25:49 +03003192
3193error_2:
Eli Cohene126ba92013-07-07 17:25:49 +03003194 ib_dealloc_pd(pd);
3195
3196error_0:
3197 kfree(attr);
3198 kfree(init_attr);
3199 return ret;
3200}
3201
Max Gurtovoy6e8484c2017-05-28 10:53:11 +03003202static u8 mlx5_get_umr_fence(u8 umr_fence_cap)
3203{
3204 switch (umr_fence_cap) {
3205 case MLX5_CAP_UMR_FENCE_NONE:
3206 return MLX5_FENCE_MODE_NONE;
3207 case MLX5_CAP_UMR_FENCE_SMALL:
3208 return MLX5_FENCE_MODE_INITIATOR_SMALL;
3209 default:
3210 return MLX5_FENCE_MODE_STRONG_ORDERING;
3211 }
3212}
3213
Eli Cohene126ba92013-07-07 17:25:49 +03003214static int create_dev_resources(struct mlx5_ib_resources *devr)
3215{
3216 struct ib_srq_init_attr attr;
3217 struct mlx5_ib_dev *dev;
Matan Barakbcf4c1e2015-06-11 16:35:20 +03003218 struct ib_cq_init_attr cq_attr = {.cqe = 1};
Haggai Eran7722f472016-02-29 15:45:07 +02003219 int port;
Eli Cohene126ba92013-07-07 17:25:49 +03003220 int ret = 0;
3221
3222 dev = container_of(devr, struct mlx5_ib_dev, devr);
3223
Haggai Erand16e91d2016-02-29 15:45:05 +02003224 mutex_init(&devr->mutex);
3225
Eli Cohene126ba92013-07-07 17:25:49 +03003226 devr->p0 = mlx5_ib_alloc_pd(&dev->ib_dev, NULL, NULL);
3227 if (IS_ERR(devr->p0)) {
3228 ret = PTR_ERR(devr->p0);
3229 goto error0;
3230 }
3231 devr->p0->device = &dev->ib_dev;
3232 devr->p0->uobject = NULL;
3233 atomic_set(&devr->p0->usecnt, 0);
3234
Matan Barakbcf4c1e2015-06-11 16:35:20 +03003235 devr->c0 = mlx5_ib_create_cq(&dev->ib_dev, &cq_attr, NULL, NULL);
Eli Cohene126ba92013-07-07 17:25:49 +03003236 if (IS_ERR(devr->c0)) {
3237 ret = PTR_ERR(devr->c0);
3238 goto error1;
3239 }
3240 devr->c0->device = &dev->ib_dev;
3241 devr->c0->uobject = NULL;
3242 devr->c0->comp_handler = NULL;
3243 devr->c0->event_handler = NULL;
3244 devr->c0->cq_context = NULL;
3245 atomic_set(&devr->c0->usecnt, 0);
3246
3247 devr->x0 = mlx5_ib_alloc_xrcd(&dev->ib_dev, NULL, NULL);
3248 if (IS_ERR(devr->x0)) {
3249 ret = PTR_ERR(devr->x0);
3250 goto error2;
3251 }
3252 devr->x0->device = &dev->ib_dev;
3253 devr->x0->inode = NULL;
3254 atomic_set(&devr->x0->usecnt, 0);
3255 mutex_init(&devr->x0->tgt_qp_mutex);
3256 INIT_LIST_HEAD(&devr->x0->tgt_qp_list);
3257
3258 devr->x1 = mlx5_ib_alloc_xrcd(&dev->ib_dev, NULL, NULL);
3259 if (IS_ERR(devr->x1)) {
3260 ret = PTR_ERR(devr->x1);
3261 goto error3;
3262 }
3263 devr->x1->device = &dev->ib_dev;
3264 devr->x1->inode = NULL;
3265 atomic_set(&devr->x1->usecnt, 0);
3266 mutex_init(&devr->x1->tgt_qp_mutex);
3267 INIT_LIST_HEAD(&devr->x1->tgt_qp_list);
3268
3269 memset(&attr, 0, sizeof(attr));
3270 attr.attr.max_sge = 1;
3271 attr.attr.max_wr = 1;
3272 attr.srq_type = IB_SRQT_XRC;
Artemy Kovalyov1a56ff62017-08-17 15:52:04 +03003273 attr.ext.cq = devr->c0;
Eli Cohene126ba92013-07-07 17:25:49 +03003274 attr.ext.xrc.xrcd = devr->x0;
3275
3276 devr->s0 = mlx5_ib_create_srq(devr->p0, &attr, NULL);
3277 if (IS_ERR(devr->s0)) {
3278 ret = PTR_ERR(devr->s0);
3279 goto error4;
3280 }
3281 devr->s0->device = &dev->ib_dev;
3282 devr->s0->pd = devr->p0;
3283 devr->s0->uobject = NULL;
3284 devr->s0->event_handler = NULL;
3285 devr->s0->srq_context = NULL;
3286 devr->s0->srq_type = IB_SRQT_XRC;
3287 devr->s0->ext.xrc.xrcd = devr->x0;
Artemy Kovalyov1a56ff62017-08-17 15:52:04 +03003288 devr->s0->ext.cq = devr->c0;
Eli Cohene126ba92013-07-07 17:25:49 +03003289 atomic_inc(&devr->s0->ext.xrc.xrcd->usecnt);
Artemy Kovalyov1a56ff62017-08-17 15:52:04 +03003290 atomic_inc(&devr->s0->ext.cq->usecnt);
Eli Cohene126ba92013-07-07 17:25:49 +03003291 atomic_inc(&devr->p0->usecnt);
3292 atomic_set(&devr->s0->usecnt, 0);
3293
Haggai Abramonvsky4aa17b22015-06-04 19:30:48 +03003294 memset(&attr, 0, sizeof(attr));
3295 attr.attr.max_sge = 1;
3296 attr.attr.max_wr = 1;
3297 attr.srq_type = IB_SRQT_BASIC;
3298 devr->s1 = mlx5_ib_create_srq(devr->p0, &attr, NULL);
3299 if (IS_ERR(devr->s1)) {
3300 ret = PTR_ERR(devr->s1);
3301 goto error5;
3302 }
3303 devr->s1->device = &dev->ib_dev;
3304 devr->s1->pd = devr->p0;
3305 devr->s1->uobject = NULL;
3306 devr->s1->event_handler = NULL;
3307 devr->s1->srq_context = NULL;
3308 devr->s1->srq_type = IB_SRQT_BASIC;
Artemy Kovalyov1a56ff62017-08-17 15:52:04 +03003309 devr->s1->ext.cq = devr->c0;
Haggai Abramonvsky4aa17b22015-06-04 19:30:48 +03003310 atomic_inc(&devr->p0->usecnt);
Artemy Kovalyov1a56ff62017-08-17 15:52:04 +03003311 atomic_set(&devr->s1->usecnt, 0);
Haggai Abramonvsky4aa17b22015-06-04 19:30:48 +03003312
Haggai Eran7722f472016-02-29 15:45:07 +02003313 for (port = 0; port < ARRAY_SIZE(devr->ports); ++port) {
3314 INIT_WORK(&devr->ports[port].pkey_change_work,
3315 pkey_change_handler);
3316 devr->ports[port].devr = devr;
3317 }
3318
Eli Cohene126ba92013-07-07 17:25:49 +03003319 return 0;
3320
Haggai Abramonvsky4aa17b22015-06-04 19:30:48 +03003321error5:
3322 mlx5_ib_destroy_srq(devr->s0);
Eli Cohene126ba92013-07-07 17:25:49 +03003323error4:
3324 mlx5_ib_dealloc_xrcd(devr->x1);
3325error3:
3326 mlx5_ib_dealloc_xrcd(devr->x0);
3327error2:
3328 mlx5_ib_destroy_cq(devr->c0);
3329error1:
3330 mlx5_ib_dealloc_pd(devr->p0);
3331error0:
3332 return ret;
3333}
3334
3335static void destroy_dev_resources(struct mlx5_ib_resources *devr)
3336{
Haggai Eran7722f472016-02-29 15:45:07 +02003337 struct mlx5_ib_dev *dev =
3338 container_of(devr, struct mlx5_ib_dev, devr);
3339 int port;
3340
Haggai Abramonvsky4aa17b22015-06-04 19:30:48 +03003341 mlx5_ib_destroy_srq(devr->s1);
Eli Cohene126ba92013-07-07 17:25:49 +03003342 mlx5_ib_destroy_srq(devr->s0);
3343 mlx5_ib_dealloc_xrcd(devr->x0);
3344 mlx5_ib_dealloc_xrcd(devr->x1);
3345 mlx5_ib_destroy_cq(devr->c0);
3346 mlx5_ib_dealloc_pd(devr->p0);
Haggai Eran7722f472016-02-29 15:45:07 +02003347
3348 /* Make sure no change P_Key work items are still executing */
3349 for (port = 0; port < dev->num_ports; ++port)
3350 cancel_work_sync(&devr->ports[port].pkey_change_work);
Eli Cohene126ba92013-07-07 17:25:49 +03003351}
3352
Achiad Shochate53505a2015-12-23 18:47:25 +02003353static u32 get_core_cap_flags(struct ib_device *ibdev)
3354{
3355 struct mlx5_ib_dev *dev = to_mdev(ibdev);
3356 enum rdma_link_layer ll = mlx5_ib_port_link_layer(ibdev, 1);
3357 u8 l3_type_cap = MLX5_CAP_ROCE(dev->mdev, l3_type);
3358 u8 roce_version_cap = MLX5_CAP_ROCE(dev->mdev, roce_version);
3359 u32 ret = 0;
3360
3361 if (ll == IB_LINK_LAYER_INFINIBAND)
3362 return RDMA_CORE_PORT_IBA_IB;
3363
Or Gerlitz72cd5712017-01-24 13:02:36 +02003364 ret = RDMA_CORE_PORT_RAW_PACKET;
3365
Achiad Shochate53505a2015-12-23 18:47:25 +02003366 if (!(l3_type_cap & MLX5_ROCE_L3_TYPE_IPV4_CAP))
Or Gerlitz72cd5712017-01-24 13:02:36 +02003367 return ret;
Achiad Shochate53505a2015-12-23 18:47:25 +02003368
3369 if (!(l3_type_cap & MLX5_ROCE_L3_TYPE_IPV6_CAP))
Or Gerlitz72cd5712017-01-24 13:02:36 +02003370 return ret;
Achiad Shochate53505a2015-12-23 18:47:25 +02003371
3372 if (roce_version_cap & MLX5_ROCE_VERSION_1_CAP)
3373 ret |= RDMA_CORE_PORT_IBA_ROCE;
3374
3375 if (roce_version_cap & MLX5_ROCE_VERSION_2_CAP)
3376 ret |= RDMA_CORE_PORT_IBA_ROCE_UDP_ENCAP;
3377
3378 return ret;
3379}
3380
Ira Weiny77386132015-05-13 20:02:58 -04003381static int mlx5_port_immutable(struct ib_device *ibdev, u8 port_num,
3382 struct ib_port_immutable *immutable)
3383{
3384 struct ib_port_attr attr;
Or Gerlitzca5b91d2016-11-27 16:51:36 +02003385 struct mlx5_ib_dev *dev = to_mdev(ibdev);
3386 enum rdma_link_layer ll = mlx5_ib_port_link_layer(ibdev, port_num);
Ira Weiny77386132015-05-13 20:02:58 -04003387 int err;
3388
Or Gerlitzc4550c62017-01-24 13:02:39 +02003389 immutable->core_cap_flags = get_core_cap_flags(ibdev);
3390
3391 err = ib_query_port(ibdev, port_num, &attr);
Ira Weiny77386132015-05-13 20:02:58 -04003392 if (err)
3393 return err;
3394
3395 immutable->pkey_tbl_len = attr.pkey_tbl_len;
3396 immutable->gid_tbl_len = attr.gid_tbl_len;
Achiad Shochate53505a2015-12-23 18:47:25 +02003397 immutable->core_cap_flags = get_core_cap_flags(ibdev);
Or Gerlitzca5b91d2016-11-27 16:51:36 +02003398 if ((ll == IB_LINK_LAYER_INFINIBAND) || MLX5_CAP_GEN(dev->mdev, roce))
3399 immutable->max_mad_size = IB_MGMT_MAD_SIZE;
Ira Weiny77386132015-05-13 20:02:58 -04003400
3401 return 0;
3402}
3403
Leon Romanovsky9abb0d12017-06-27 16:49:53 +03003404static void get_dev_fw_str(struct ib_device *ibdev, char *str)
Ira Weinyc7342822016-06-15 02:22:01 -04003405{
3406 struct mlx5_ib_dev *dev =
3407 container_of(ibdev, struct mlx5_ib_dev, ib_dev);
Leon Romanovsky9abb0d12017-06-27 16:49:53 +03003408 snprintf(str, IB_FW_VERSION_NAME_MAX, "%d.%d.%04d",
3409 fw_rev_maj(dev->mdev), fw_rev_min(dev->mdev),
3410 fw_rev_sub(dev->mdev));
Ira Weinyc7342822016-06-15 02:22:01 -04003411}
3412
Or Gerlitz45f95ac2016-11-27 16:51:35 +02003413static int mlx5_eth_lag_init(struct mlx5_ib_dev *dev)
Aviv Heller9ef9c642016-09-18 20:48:01 +03003414{
3415 struct mlx5_core_dev *mdev = dev->mdev;
3416 struct mlx5_flow_namespace *ns = mlx5_get_flow_namespace(mdev,
3417 MLX5_FLOW_NAMESPACE_LAG);
3418 struct mlx5_flow_table *ft;
3419 int err;
3420
3421 if (!ns || !mlx5_lag_is_active(mdev))
3422 return 0;
3423
3424 err = mlx5_cmd_create_vport_lag(mdev);
3425 if (err)
3426 return err;
3427
3428 ft = mlx5_create_lag_demux_flow_table(ns, 0, 0);
3429 if (IS_ERR(ft)) {
3430 err = PTR_ERR(ft);
3431 goto err_destroy_vport_lag;
3432 }
3433
3434 dev->flow_db.lag_demux_ft = ft;
3435 return 0;
3436
3437err_destroy_vport_lag:
3438 mlx5_cmd_destroy_vport_lag(mdev);
3439 return err;
3440}
3441
Or Gerlitz45f95ac2016-11-27 16:51:35 +02003442static void mlx5_eth_lag_cleanup(struct mlx5_ib_dev *dev)
Aviv Heller9ef9c642016-09-18 20:48:01 +03003443{
3444 struct mlx5_core_dev *mdev = dev->mdev;
3445
3446 if (dev->flow_db.lag_demux_ft) {
3447 mlx5_destroy_flow_table(dev->flow_db.lag_demux_ft);
3448 dev->flow_db.lag_demux_ft = NULL;
3449
3450 mlx5_cmd_destroy_vport_lag(mdev);
3451 }
3452}
3453
Or Gerlitzd012f5d2016-11-27 16:51:34 +02003454static int mlx5_add_netdev_notifier(struct mlx5_ib_dev *dev)
Achiad Shochatfc24fc52015-12-23 18:47:17 +02003455{
Achiad Shochate53505a2015-12-23 18:47:25 +02003456 int err;
3457
Achiad Shochatfc24fc52015-12-23 18:47:17 +02003458 dev->roce.nb.notifier_call = mlx5_netdev_event;
Achiad Shochate53505a2015-12-23 18:47:25 +02003459 err = register_netdevice_notifier(&dev->roce.nb);
Aviv Heller5ec8c832016-09-18 20:48:00 +03003460 if (err) {
3461 dev->roce.nb.notifier_call = NULL;
Achiad Shochate53505a2015-12-23 18:47:25 +02003462 return err;
Aviv Heller5ec8c832016-09-18 20:48:00 +03003463 }
Achiad Shochate53505a2015-12-23 18:47:25 +02003464
Or Gerlitzd012f5d2016-11-27 16:51:34 +02003465 return 0;
3466}
Achiad Shochate53505a2015-12-23 18:47:25 +02003467
Or Gerlitzd012f5d2016-11-27 16:51:34 +02003468static void mlx5_remove_netdev_notifier(struct mlx5_ib_dev *dev)
Eli Cohene126ba92013-07-07 17:25:49 +03003469{
3470 if (dev->roce.nb.notifier_call) {
3471 unregister_netdevice_notifier(&dev->roce.nb);
3472 dev->roce.nb.notifier_call = NULL;
3473 }
3474}
3475
Or Gerlitz45f95ac2016-11-27 16:51:35 +02003476static int mlx5_enable_eth(struct mlx5_ib_dev *dev)
Eli Cohene126ba92013-07-07 17:25:49 +03003477{
Eli Cohene126ba92013-07-07 17:25:49 +03003478 int err;
3479
Or Gerlitzd012f5d2016-11-27 16:51:34 +02003480 err = mlx5_add_netdev_notifier(dev);
3481 if (err)
Achiad Shochate53505a2015-12-23 18:47:25 +02003482 return err;
Achiad Shochate53505a2015-12-23 18:47:25 +02003483
Or Gerlitzca5b91d2016-11-27 16:51:36 +02003484 if (MLX5_CAP_GEN(dev->mdev, roce)) {
3485 err = mlx5_nic_vport_enable_roce(dev->mdev);
3486 if (err)
3487 goto err_unregister_netdevice_notifier;
3488 }
Achiad Shochate53505a2015-12-23 18:47:25 +02003489
Or Gerlitz45f95ac2016-11-27 16:51:35 +02003490 err = mlx5_eth_lag_init(dev);
Aviv Heller9ef9c642016-09-18 20:48:01 +03003491 if (err)
3492 goto err_disable_roce;
3493
Achiad Shochate53505a2015-12-23 18:47:25 +02003494 return 0;
3495
Aviv Heller9ef9c642016-09-18 20:48:01 +03003496err_disable_roce:
Or Gerlitzca5b91d2016-11-27 16:51:36 +02003497 if (MLX5_CAP_GEN(dev->mdev, roce))
3498 mlx5_nic_vport_disable_roce(dev->mdev);
Aviv Heller9ef9c642016-09-18 20:48:01 +03003499
Achiad Shochate53505a2015-12-23 18:47:25 +02003500err_unregister_netdevice_notifier:
Or Gerlitzd012f5d2016-11-27 16:51:34 +02003501 mlx5_remove_netdev_notifier(dev);
Achiad Shochate53505a2015-12-23 18:47:25 +02003502 return err;
Achiad Shochatfc24fc52015-12-23 18:47:17 +02003503}
3504
Or Gerlitz45f95ac2016-11-27 16:51:35 +02003505static void mlx5_disable_eth(struct mlx5_ib_dev *dev)
Achiad Shochatfc24fc52015-12-23 18:47:17 +02003506{
Or Gerlitz45f95ac2016-11-27 16:51:35 +02003507 mlx5_eth_lag_cleanup(dev);
Or Gerlitzca5b91d2016-11-27 16:51:36 +02003508 if (MLX5_CAP_GEN(dev->mdev, roce))
3509 mlx5_nic_vport_disable_roce(dev->mdev);
Achiad Shochatfc24fc52015-12-23 18:47:17 +02003510}
3511
Parav Pandite1f24a72017-04-16 07:29:29 +03003512struct mlx5_ib_counter {
Kamal Heib7c16f472017-01-18 15:25:09 +02003513 const char *name;
3514 size_t offset;
3515};
3516
3517#define INIT_Q_COUNTER(_name) \
3518 { .name = #_name, .offset = MLX5_BYTE_OFF(query_q_counter_out, _name)}
3519
Parav Pandite1f24a72017-04-16 07:29:29 +03003520static const struct mlx5_ib_counter basic_q_cnts[] = {
Kamal Heib7c16f472017-01-18 15:25:09 +02003521 INIT_Q_COUNTER(rx_write_requests),
3522 INIT_Q_COUNTER(rx_read_requests),
3523 INIT_Q_COUNTER(rx_atomic_requests),
3524 INIT_Q_COUNTER(out_of_buffer),
3525};
3526
Parav Pandite1f24a72017-04-16 07:29:29 +03003527static const struct mlx5_ib_counter out_of_seq_q_cnts[] = {
Kamal Heib7c16f472017-01-18 15:25:09 +02003528 INIT_Q_COUNTER(out_of_sequence),
3529};
3530
Parav Pandite1f24a72017-04-16 07:29:29 +03003531static const struct mlx5_ib_counter retrans_q_cnts[] = {
Kamal Heib7c16f472017-01-18 15:25:09 +02003532 INIT_Q_COUNTER(duplicate_request),
3533 INIT_Q_COUNTER(rnr_nak_retry_err),
3534 INIT_Q_COUNTER(packet_seq_err),
3535 INIT_Q_COUNTER(implied_nak_seq_err),
3536 INIT_Q_COUNTER(local_ack_timeout_err),
3537};
3538
Parav Pandite1f24a72017-04-16 07:29:29 +03003539#define INIT_CONG_COUNTER(_name) \
3540 { .name = #_name, .offset = \
3541 MLX5_BYTE_OFF(query_cong_statistics_out, _name ## _high)}
3542
3543static const struct mlx5_ib_counter cong_cnts[] = {
3544 INIT_CONG_COUNTER(rp_cnp_ignored),
3545 INIT_CONG_COUNTER(rp_cnp_handled),
3546 INIT_CONG_COUNTER(np_ecn_marked_roce_packets),
3547 INIT_CONG_COUNTER(np_cnp_sent),
3548};
3549
Parav Pandit58dcb602017-06-19 07:19:37 +03003550static const struct mlx5_ib_counter extended_err_cnts[] = {
3551 INIT_Q_COUNTER(resp_local_length_error),
3552 INIT_Q_COUNTER(resp_cqe_error),
3553 INIT_Q_COUNTER(req_cqe_error),
3554 INIT_Q_COUNTER(req_remote_invalid_request),
3555 INIT_Q_COUNTER(req_remote_access_errors),
3556 INIT_Q_COUNTER(resp_remote_access_errors),
3557 INIT_Q_COUNTER(resp_cqe_flush_error),
3558 INIT_Q_COUNTER(req_cqe_flush_error),
3559};
3560
Parav Pandite1f24a72017-04-16 07:29:29 +03003561static void mlx5_ib_dealloc_counters(struct mlx5_ib_dev *dev)
Mark Bloch0837e862016-06-17 15:10:55 +03003562{
3563 unsigned int i;
3564
Kamal Heib7c16f472017-01-18 15:25:09 +02003565 for (i = 0; i < dev->num_ports; i++) {
Mark Bloch0837e862016-06-17 15:10:55 +03003566 mlx5_core_dealloc_q_counter(dev->mdev,
Parav Pandite1f24a72017-04-16 07:29:29 +03003567 dev->port[i].cnts.set_id);
3568 kfree(dev->port[i].cnts.names);
3569 kfree(dev->port[i].cnts.offsets);
Kamal Heib7c16f472017-01-18 15:25:09 +02003570 }
3571}
3572
Parav Pandite1f24a72017-04-16 07:29:29 +03003573static int __mlx5_ib_alloc_counters(struct mlx5_ib_dev *dev,
3574 struct mlx5_ib_counters *cnts)
Kamal Heib7c16f472017-01-18 15:25:09 +02003575{
3576 u32 num_counters;
3577
3578 num_counters = ARRAY_SIZE(basic_q_cnts);
3579
3580 if (MLX5_CAP_GEN(dev->mdev, out_of_seq_cnt))
3581 num_counters += ARRAY_SIZE(out_of_seq_q_cnts);
3582
3583 if (MLX5_CAP_GEN(dev->mdev, retransmission_q_counters))
3584 num_counters += ARRAY_SIZE(retrans_q_cnts);
Parav Pandit58dcb602017-06-19 07:19:37 +03003585
3586 if (MLX5_CAP_GEN(dev->mdev, enhanced_error_q_counters))
3587 num_counters += ARRAY_SIZE(extended_err_cnts);
3588
Parav Pandite1f24a72017-04-16 07:29:29 +03003589 cnts->num_q_counters = num_counters;
Kamal Heib7c16f472017-01-18 15:25:09 +02003590
Parav Pandite1f24a72017-04-16 07:29:29 +03003591 if (MLX5_CAP_GEN(dev->mdev, cc_query_allowed)) {
3592 cnts->num_cong_counters = ARRAY_SIZE(cong_cnts);
3593 num_counters += ARRAY_SIZE(cong_cnts);
3594 }
3595
3596 cnts->names = kcalloc(num_counters, sizeof(cnts->names), GFP_KERNEL);
3597 if (!cnts->names)
Kamal Heib7c16f472017-01-18 15:25:09 +02003598 return -ENOMEM;
3599
Parav Pandite1f24a72017-04-16 07:29:29 +03003600 cnts->offsets = kcalloc(num_counters,
3601 sizeof(cnts->offsets), GFP_KERNEL);
3602 if (!cnts->offsets)
Kamal Heib7c16f472017-01-18 15:25:09 +02003603 goto err_names;
3604
Kamal Heib7c16f472017-01-18 15:25:09 +02003605 return 0;
3606
3607err_names:
Parav Pandite1f24a72017-04-16 07:29:29 +03003608 kfree(cnts->names);
Kamal Heib7c16f472017-01-18 15:25:09 +02003609 return -ENOMEM;
3610}
3611
Parav Pandite1f24a72017-04-16 07:29:29 +03003612static void mlx5_ib_fill_counters(struct mlx5_ib_dev *dev,
3613 const char **names,
3614 size_t *offsets)
Kamal Heib7c16f472017-01-18 15:25:09 +02003615{
3616 int i;
3617 int j = 0;
3618
3619 for (i = 0; i < ARRAY_SIZE(basic_q_cnts); i++, j++) {
3620 names[j] = basic_q_cnts[i].name;
3621 offsets[j] = basic_q_cnts[i].offset;
3622 }
3623
3624 if (MLX5_CAP_GEN(dev->mdev, out_of_seq_cnt)) {
3625 for (i = 0; i < ARRAY_SIZE(out_of_seq_q_cnts); i++, j++) {
3626 names[j] = out_of_seq_q_cnts[i].name;
3627 offsets[j] = out_of_seq_q_cnts[i].offset;
3628 }
3629 }
3630
3631 if (MLX5_CAP_GEN(dev->mdev, retransmission_q_counters)) {
3632 for (i = 0; i < ARRAY_SIZE(retrans_q_cnts); i++, j++) {
3633 names[j] = retrans_q_cnts[i].name;
3634 offsets[j] = retrans_q_cnts[i].offset;
3635 }
3636 }
Parav Pandite1f24a72017-04-16 07:29:29 +03003637
Parav Pandit58dcb602017-06-19 07:19:37 +03003638 if (MLX5_CAP_GEN(dev->mdev, enhanced_error_q_counters)) {
3639 for (i = 0; i < ARRAY_SIZE(extended_err_cnts); i++, j++) {
3640 names[j] = extended_err_cnts[i].name;
3641 offsets[j] = extended_err_cnts[i].offset;
3642 }
3643 }
3644
Parav Pandite1f24a72017-04-16 07:29:29 +03003645 if (MLX5_CAP_GEN(dev->mdev, cc_query_allowed)) {
3646 for (i = 0; i < ARRAY_SIZE(cong_cnts); i++, j++) {
3647 names[j] = cong_cnts[i].name;
3648 offsets[j] = cong_cnts[i].offset;
3649 }
3650 }
Mark Bloch0837e862016-06-17 15:10:55 +03003651}
3652
Parav Pandite1f24a72017-04-16 07:29:29 +03003653static int mlx5_ib_alloc_counters(struct mlx5_ib_dev *dev)
Mark Bloch0837e862016-06-17 15:10:55 +03003654{
3655 int i;
3656 int ret;
3657
3658 for (i = 0; i < dev->num_ports; i++) {
Kamal Heib7c16f472017-01-18 15:25:09 +02003659 struct mlx5_ib_port *port = &dev->port[i];
3660
Mark Bloch0837e862016-06-17 15:10:55 +03003661 ret = mlx5_core_alloc_q_counter(dev->mdev,
Parav Pandite1f24a72017-04-16 07:29:29 +03003662 &port->cnts.set_id);
Mark Bloch0837e862016-06-17 15:10:55 +03003663 if (ret) {
3664 mlx5_ib_warn(dev,
3665 "couldn't allocate queue counter for port %d, err %d\n",
3666 i + 1, ret);
3667 goto dealloc_counters;
3668 }
Kamal Heib7c16f472017-01-18 15:25:09 +02003669
Parav Pandite1f24a72017-04-16 07:29:29 +03003670 ret = __mlx5_ib_alloc_counters(dev, &port->cnts);
Kamal Heib7c16f472017-01-18 15:25:09 +02003671 if (ret)
3672 goto dealloc_counters;
3673
Parav Pandite1f24a72017-04-16 07:29:29 +03003674 mlx5_ib_fill_counters(dev, port->cnts.names,
3675 port->cnts.offsets);
Mark Bloch0837e862016-06-17 15:10:55 +03003676 }
3677
3678 return 0;
3679
3680dealloc_counters:
3681 while (--i >= 0)
3682 mlx5_core_dealloc_q_counter(dev->mdev,
Parav Pandite1f24a72017-04-16 07:29:29 +03003683 dev->port[i].cnts.set_id);
Mark Bloch0837e862016-06-17 15:10:55 +03003684
3685 return ret;
3686}
3687
Mark Bloch0ad17a82016-06-17 15:10:56 +03003688static struct rdma_hw_stats *mlx5_ib_alloc_hw_stats(struct ib_device *ibdev,
3689 u8 port_num)
3690{
Kamal Heib7c16f472017-01-18 15:25:09 +02003691 struct mlx5_ib_dev *dev = to_mdev(ibdev);
3692 struct mlx5_ib_port *port = &dev->port[port_num - 1];
Mark Bloch0ad17a82016-06-17 15:10:56 +03003693
3694 /* We support only per port stats */
3695 if (port_num == 0)
3696 return NULL;
3697
Parav Pandite1f24a72017-04-16 07:29:29 +03003698 return rdma_alloc_hw_stats_struct(port->cnts.names,
3699 port->cnts.num_q_counters +
3700 port->cnts.num_cong_counters,
Mark Bloch0ad17a82016-06-17 15:10:56 +03003701 RDMA_HW_STATS_DEFAULT_LIFESPAN);
3702}
3703
Parav Pandite1f24a72017-04-16 07:29:29 +03003704static int mlx5_ib_query_q_counters(struct mlx5_ib_dev *dev,
3705 struct mlx5_ib_port *port,
3706 struct rdma_hw_stats *stats)
3707{
3708 int outlen = MLX5_ST_SZ_BYTES(query_q_counter_out);
3709 void *out;
3710 __be32 val;
3711 int ret, i;
3712
Leon Romanovsky1b9a07e2017-05-10 21:32:18 +03003713 out = kvzalloc(outlen, GFP_KERNEL);
Parav Pandite1f24a72017-04-16 07:29:29 +03003714 if (!out)
3715 return -ENOMEM;
3716
3717 ret = mlx5_core_query_q_counter(dev->mdev,
3718 port->cnts.set_id, 0,
3719 out, outlen);
3720 if (ret)
3721 goto free;
3722
3723 for (i = 0; i < port->cnts.num_q_counters; i++) {
3724 val = *(__be32 *)(out + port->cnts.offsets[i]);
3725 stats->value[i] = (u64)be32_to_cpu(val);
3726 }
3727
3728free:
3729 kvfree(out);
3730 return ret;
3731}
3732
3733static int mlx5_ib_query_cong_counters(struct mlx5_ib_dev *dev,
3734 struct mlx5_ib_port *port,
3735 struct rdma_hw_stats *stats)
3736{
3737 int outlen = MLX5_ST_SZ_BYTES(query_cong_statistics_out);
3738 void *out;
3739 int ret, i;
3740 int offset = port->cnts.num_q_counters;
3741
Leon Romanovsky1b9a07e2017-05-10 21:32:18 +03003742 out = kvzalloc(outlen, GFP_KERNEL);
Parav Pandite1f24a72017-04-16 07:29:29 +03003743 if (!out)
3744 return -ENOMEM;
3745
3746 ret = mlx5_cmd_query_cong_counter(dev->mdev, false, out, outlen);
3747 if (ret)
3748 goto free;
3749
3750 for (i = 0; i < port->cnts.num_cong_counters; i++) {
3751 stats->value[i + offset] =
3752 be64_to_cpup((__be64 *)(out +
3753 port->cnts.offsets[i + offset]));
3754 }
3755
3756free:
3757 kvfree(out);
3758 return ret;
3759}
3760
Mark Bloch0ad17a82016-06-17 15:10:56 +03003761static int mlx5_ib_get_hw_stats(struct ib_device *ibdev,
3762 struct rdma_hw_stats *stats,
Kamal Heib7c16f472017-01-18 15:25:09 +02003763 u8 port_num, int index)
Mark Bloch0ad17a82016-06-17 15:10:56 +03003764{
3765 struct mlx5_ib_dev *dev = to_mdev(ibdev);
Kamal Heib7c16f472017-01-18 15:25:09 +02003766 struct mlx5_ib_port *port = &dev->port[port_num - 1];
Parav Pandite1f24a72017-04-16 07:29:29 +03003767 int ret, num_counters;
Mark Bloch0ad17a82016-06-17 15:10:56 +03003768
Kamal Heib7c16f472017-01-18 15:25:09 +02003769 if (!stats)
Parav Pandite1f24a72017-04-16 07:29:29 +03003770 return -EINVAL;
Mark Bloch0ad17a82016-06-17 15:10:56 +03003771
Parav Pandite1f24a72017-04-16 07:29:29 +03003772 ret = mlx5_ib_query_q_counters(dev, port, stats);
Mark Bloch0ad17a82016-06-17 15:10:56 +03003773 if (ret)
Parav Pandite1f24a72017-04-16 07:29:29 +03003774 return ret;
3775 num_counters = port->cnts.num_q_counters;
Mark Bloch0ad17a82016-06-17 15:10:56 +03003776
Parav Pandite1f24a72017-04-16 07:29:29 +03003777 if (MLX5_CAP_GEN(dev->mdev, cc_query_allowed)) {
3778 ret = mlx5_ib_query_cong_counters(dev, port, stats);
3779 if (ret)
3780 return ret;
3781 num_counters += port->cnts.num_cong_counters;
Mark Bloch0ad17a82016-06-17 15:10:56 +03003782 }
Kamal Heib7c16f472017-01-18 15:25:09 +02003783
Parav Pandite1f24a72017-04-16 07:29:29 +03003784 return num_counters;
Mark Bloch0ad17a82016-06-17 15:10:56 +03003785}
3786
Niranjana Vishwanathapura8e959602017-06-30 13:14:46 -07003787static void mlx5_ib_free_rdma_netdev(struct net_device *netdev)
3788{
3789 return mlx5_rdma_netdev_free(netdev);
3790}
3791
Erez Shitrit693dfd52017-04-27 17:01:34 +03003792static struct net_device*
3793mlx5_ib_alloc_rdma_netdev(struct ib_device *hca,
3794 u8 port_num,
3795 enum rdma_netdev_t type,
3796 const char *name,
3797 unsigned char name_assign_type,
3798 void (*setup)(struct net_device *))
3799{
Niranjana Vishwanathapura8e959602017-06-30 13:14:46 -07003800 struct net_device *netdev;
3801 struct rdma_netdev *rn;
3802
Erez Shitrit693dfd52017-04-27 17:01:34 +03003803 if (type != RDMA_NETDEV_IPOIB)
3804 return ERR_PTR(-EOPNOTSUPP);
3805
Niranjana Vishwanathapura8e959602017-06-30 13:14:46 -07003806 netdev = mlx5_rdma_netdev_alloc(to_mdev(hca)->mdev, hca,
3807 name, setup);
3808 if (likely(!IS_ERR_OR_NULL(netdev))) {
3809 rn = netdev_priv(netdev);
3810 rn->free_rdma_netdev = mlx5_ib_free_rdma_netdev;
3811 }
3812 return netdev;
Erez Shitrit693dfd52017-04-27 17:01:34 +03003813}
3814
Maor Gottliebfe248c32017-05-30 10:29:14 +03003815static void delay_drop_debugfs_cleanup(struct mlx5_ib_dev *dev)
3816{
3817 if (!dev->delay_drop.dbg)
3818 return;
3819 debugfs_remove_recursive(dev->delay_drop.dbg->dir_debugfs);
3820 kfree(dev->delay_drop.dbg);
3821 dev->delay_drop.dbg = NULL;
3822}
3823
Maor Gottlieb03404e82017-05-30 10:29:13 +03003824static void cancel_delay_drop(struct mlx5_ib_dev *dev)
3825{
3826 if (!(dev->ib_dev.attrs.raw_packet_caps & IB_RAW_PACKET_CAP_DELAY_DROP))
3827 return;
3828
3829 cancel_work_sync(&dev->delay_drop.delay_drop_work);
Maor Gottliebfe248c32017-05-30 10:29:14 +03003830 delay_drop_debugfs_cleanup(dev);
3831}
3832
3833static ssize_t delay_drop_timeout_read(struct file *filp, char __user *buf,
3834 size_t count, loff_t *pos)
3835{
3836 struct mlx5_ib_delay_drop *delay_drop = filp->private_data;
3837 char lbuf[20];
3838 int len;
3839
3840 len = snprintf(lbuf, sizeof(lbuf), "%u\n", delay_drop->timeout);
3841 return simple_read_from_buffer(buf, count, pos, lbuf, len);
3842}
3843
3844static ssize_t delay_drop_timeout_write(struct file *filp, const char __user *buf,
3845 size_t count, loff_t *pos)
3846{
3847 struct mlx5_ib_delay_drop *delay_drop = filp->private_data;
3848 u32 timeout;
3849 u32 var;
3850
3851 if (kstrtouint_from_user(buf, count, 0, &var))
3852 return -EFAULT;
3853
3854 timeout = min_t(u32, roundup(var, 100), MLX5_MAX_DELAY_DROP_TIMEOUT_MS *
3855 1000);
3856 if (timeout != var)
3857 mlx5_ib_dbg(delay_drop->dev, "Round delay drop timeout to %u usec\n",
3858 timeout);
3859
3860 delay_drop->timeout = timeout;
3861
3862 return count;
3863}
3864
3865static const struct file_operations fops_delay_drop_timeout = {
3866 .owner = THIS_MODULE,
3867 .open = simple_open,
3868 .write = delay_drop_timeout_write,
3869 .read = delay_drop_timeout_read,
3870};
3871
3872static int delay_drop_debugfs_init(struct mlx5_ib_dev *dev)
3873{
3874 struct mlx5_ib_dbg_delay_drop *dbg;
3875
3876 if (!mlx5_debugfs_root)
3877 return 0;
3878
3879 dbg = kzalloc(sizeof(*dbg), GFP_KERNEL);
3880 if (!dbg)
3881 return -ENOMEM;
3882
Sudip Mukherjeecbafad82017-09-18 12:28:48 +01003883 dev->delay_drop.dbg = dbg;
3884
Maor Gottliebfe248c32017-05-30 10:29:14 +03003885 dbg->dir_debugfs =
3886 debugfs_create_dir("delay_drop",
3887 dev->mdev->priv.dbg_root);
3888 if (!dbg->dir_debugfs)
Sudip Mukherjeecbafad82017-09-18 12:28:48 +01003889 goto out_debugfs;
Maor Gottliebfe248c32017-05-30 10:29:14 +03003890
3891 dbg->events_cnt_debugfs =
3892 debugfs_create_atomic_t("num_timeout_events", 0400,
3893 dbg->dir_debugfs,
3894 &dev->delay_drop.events_cnt);
3895 if (!dbg->events_cnt_debugfs)
3896 goto out_debugfs;
3897
3898 dbg->rqs_cnt_debugfs =
3899 debugfs_create_atomic_t("num_rqs", 0400,
3900 dbg->dir_debugfs,
3901 &dev->delay_drop.rqs_cnt);
3902 if (!dbg->rqs_cnt_debugfs)
3903 goto out_debugfs;
3904
3905 dbg->timeout_debugfs =
3906 debugfs_create_file("timeout", 0600,
3907 dbg->dir_debugfs,
3908 &dev->delay_drop,
3909 &fops_delay_drop_timeout);
3910 if (!dbg->timeout_debugfs)
3911 goto out_debugfs;
3912
3913 return 0;
3914
3915out_debugfs:
3916 delay_drop_debugfs_cleanup(dev);
3917 return -ENOMEM;
Maor Gottlieb03404e82017-05-30 10:29:13 +03003918}
3919
3920static void init_delay_drop(struct mlx5_ib_dev *dev)
3921{
3922 if (!(dev->ib_dev.attrs.raw_packet_caps & IB_RAW_PACKET_CAP_DELAY_DROP))
3923 return;
3924
3925 mutex_init(&dev->delay_drop.lock);
3926 dev->delay_drop.dev = dev;
3927 dev->delay_drop.activate = false;
3928 dev->delay_drop.timeout = MLX5_MAX_DELAY_DROP_TIMEOUT_MS * 1000;
3929 INIT_WORK(&dev->delay_drop.delay_drop_work, delay_drop_handler);
Maor Gottliebfe248c32017-05-30 10:29:14 +03003930 atomic_set(&dev->delay_drop.rqs_cnt, 0);
3931 atomic_set(&dev->delay_drop.events_cnt, 0);
3932
3933 if (delay_drop_debugfs_init(dev))
3934 mlx5_ib_warn(dev, "Failed to init delay drop debugfs\n");
Maor Gottlieb03404e82017-05-30 10:29:13 +03003935}
3936
Leon Romanovsky84305d712017-08-17 15:50:53 +03003937static const struct cpumask *
3938mlx5_ib_get_vector_affinity(struct ib_device *ibdev, int comp_vector)
Sagi Grimberg40b24402017-07-13 11:09:42 +03003939{
3940 struct mlx5_ib_dev *dev = to_mdev(ibdev);
3941
3942 return mlx5_get_vector_affinity(dev->mdev, comp_vector);
3943}
3944
Jack Morgenstein9603b612014-07-28 23:30:22 +03003945static void *mlx5_ib_add(struct mlx5_core_dev *mdev)
Eli Cohene126ba92013-07-07 17:25:49 +03003946{
Eli Cohene126ba92013-07-07 17:25:49 +03003947 struct mlx5_ib_dev *dev;
Achiad Shochatebd61f62015-12-23 18:47:16 +02003948 enum rdma_link_layer ll;
3949 int port_type_cap;
Aviv Heller4babcf92016-09-18 20:48:03 +03003950 const char *name;
Eli Cohene126ba92013-07-07 17:25:49 +03003951 int err;
3952 int i;
3953
Achiad Shochatebd61f62015-12-23 18:47:16 +02003954 port_type_cap = MLX5_CAP_GEN(mdev, port_type);
3955 ll = mlx5_port_type_cap_to_rdma_ll(port_type_cap);
3956
Eli Cohene126ba92013-07-07 17:25:49 +03003957 printk_once(KERN_INFO "%s", mlx5_version);
3958
3959 dev = (struct mlx5_ib_dev *)ib_alloc_device(sizeof(*dev));
3960 if (!dev)
Jack Morgenstein9603b612014-07-28 23:30:22 +03003961 return NULL;
Eli Cohene126ba92013-07-07 17:25:49 +03003962
Jack Morgenstein9603b612014-07-28 23:30:22 +03003963 dev->mdev = mdev;
Eli Cohene126ba92013-07-07 17:25:49 +03003964
Mark Bloch0837e862016-06-17 15:10:55 +03003965 dev->port = kcalloc(MLX5_CAP_GEN(mdev, num_ports), sizeof(*dev->port),
3966 GFP_KERNEL);
3967 if (!dev->port)
3968 goto err_dealloc;
3969
Achiad Shochatfc24fc52015-12-23 18:47:17 +02003970 rwlock_init(&dev->roce.netdev_lock);
Eli Cohene126ba92013-07-07 17:25:49 +03003971 err = get_port_caps(dev);
3972 if (err)
Mark Bloch0837e862016-06-17 15:10:55 +03003973 goto err_free_port;
Eli Cohene126ba92013-07-07 17:25:49 +03003974
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03003975 if (mlx5_use_mad_ifc(dev))
3976 get_ext_port_caps(dev);
Eli Cohene126ba92013-07-07 17:25:49 +03003977
Aviv Heller4babcf92016-09-18 20:48:03 +03003978 if (!mlx5_lag_is_active(mdev))
3979 name = "mlx5_%d";
3980 else
3981 name = "mlx5_bond_%d";
3982
3983 strlcpy(dev->ib_dev.name, name, IB_DEVICE_NAME_MAX);
Eli Cohene126ba92013-07-07 17:25:49 +03003984 dev->ib_dev.owner = THIS_MODULE;
3985 dev->ib_dev.node_type = RDMA_NODE_IB_CA;
Sagi Grimbergc6790aa2015-09-24 10:34:23 +03003986 dev->ib_dev.local_dma_lkey = 0 /* not supported for now */;
Saeed Mahameed938fe832015-05-28 22:28:41 +03003987 dev->num_ports = MLX5_CAP_GEN(mdev, num_ports);
Eli Cohene126ba92013-07-07 17:25:49 +03003988 dev->ib_dev.phys_port_cnt = dev->num_ports;
Saeed Mahameed233d05d2015-04-02 17:07:32 +03003989 dev->ib_dev.num_comp_vectors =
3990 dev->mdev->priv.eq_table.num_comp_vectors;
Bart Van Assche9b0c2892017-01-20 13:04:21 -08003991 dev->ib_dev.dev.parent = &mdev->pdev->dev;
Eli Cohene126ba92013-07-07 17:25:49 +03003992
3993 dev->ib_dev.uverbs_abi_ver = MLX5_IB_UVERBS_ABI_VERSION;
3994 dev->ib_dev.uverbs_cmd_mask =
3995 (1ull << IB_USER_VERBS_CMD_GET_CONTEXT) |
3996 (1ull << IB_USER_VERBS_CMD_QUERY_DEVICE) |
3997 (1ull << IB_USER_VERBS_CMD_QUERY_PORT) |
3998 (1ull << IB_USER_VERBS_CMD_ALLOC_PD) |
3999 (1ull << IB_USER_VERBS_CMD_DEALLOC_PD) |
Moni Shoua41c450f2016-11-23 08:23:26 +02004000 (1ull << IB_USER_VERBS_CMD_CREATE_AH) |
4001 (1ull << IB_USER_VERBS_CMD_DESTROY_AH) |
Eli Cohene126ba92013-07-07 17:25:49 +03004002 (1ull << IB_USER_VERBS_CMD_REG_MR) |
Noa Osherovich56e11d62016-02-29 16:46:51 +02004003 (1ull << IB_USER_VERBS_CMD_REREG_MR) |
Eli Cohene126ba92013-07-07 17:25:49 +03004004 (1ull << IB_USER_VERBS_CMD_DEREG_MR) |
4005 (1ull << IB_USER_VERBS_CMD_CREATE_COMP_CHANNEL) |
4006 (1ull << IB_USER_VERBS_CMD_CREATE_CQ) |
4007 (1ull << IB_USER_VERBS_CMD_RESIZE_CQ) |
4008 (1ull << IB_USER_VERBS_CMD_DESTROY_CQ) |
4009 (1ull << IB_USER_VERBS_CMD_CREATE_QP) |
4010 (1ull << IB_USER_VERBS_CMD_MODIFY_QP) |
4011 (1ull << IB_USER_VERBS_CMD_QUERY_QP) |
4012 (1ull << IB_USER_VERBS_CMD_DESTROY_QP) |
4013 (1ull << IB_USER_VERBS_CMD_ATTACH_MCAST) |
4014 (1ull << IB_USER_VERBS_CMD_DETACH_MCAST) |
4015 (1ull << IB_USER_VERBS_CMD_CREATE_SRQ) |
4016 (1ull << IB_USER_VERBS_CMD_MODIFY_SRQ) |
4017 (1ull << IB_USER_VERBS_CMD_QUERY_SRQ) |
4018 (1ull << IB_USER_VERBS_CMD_DESTROY_SRQ) |
4019 (1ull << IB_USER_VERBS_CMD_CREATE_XSRQ) |
4020 (1ull << IB_USER_VERBS_CMD_OPEN_QP);
Haggai Eran1707cb42015-02-08 13:28:52 +02004021 dev->ib_dev.uverbs_ex_cmd_mask =
Matan Barakd4584dd2016-01-28 17:51:46 +02004022 (1ull << IB_USER_VERBS_EX_CMD_QUERY_DEVICE) |
4023 (1ull << IB_USER_VERBS_EX_CMD_CREATE_CQ) |
Bodong Wang7d29f342016-12-01 13:43:16 +02004024 (1ull << IB_USER_VERBS_EX_CMD_CREATE_QP) |
4025 (1ull << IB_USER_VERBS_EX_CMD_MODIFY_QP);
Eli Cohene126ba92013-07-07 17:25:49 +03004026
4027 dev->ib_dev.query_device = mlx5_ib_query_device;
4028 dev->ib_dev.query_port = mlx5_ib_query_port;
Achiad Shochatebd61f62015-12-23 18:47:16 +02004029 dev->ib_dev.get_link_layer = mlx5_ib_port_link_layer;
Achiad Shochatfc24fc52015-12-23 18:47:17 +02004030 if (ll == IB_LINK_LAYER_ETHERNET)
4031 dev->ib_dev.get_netdev = mlx5_ib_get_netdev;
Eli Cohene126ba92013-07-07 17:25:49 +03004032 dev->ib_dev.query_gid = mlx5_ib_query_gid;
Achiad Shochat3cca2602015-12-23 18:47:23 +02004033 dev->ib_dev.add_gid = mlx5_ib_add_gid;
4034 dev->ib_dev.del_gid = mlx5_ib_del_gid;
Eli Cohene126ba92013-07-07 17:25:49 +03004035 dev->ib_dev.query_pkey = mlx5_ib_query_pkey;
4036 dev->ib_dev.modify_device = mlx5_ib_modify_device;
4037 dev->ib_dev.modify_port = mlx5_ib_modify_port;
4038 dev->ib_dev.alloc_ucontext = mlx5_ib_alloc_ucontext;
4039 dev->ib_dev.dealloc_ucontext = mlx5_ib_dealloc_ucontext;
4040 dev->ib_dev.mmap = mlx5_ib_mmap;
4041 dev->ib_dev.alloc_pd = mlx5_ib_alloc_pd;
4042 dev->ib_dev.dealloc_pd = mlx5_ib_dealloc_pd;
4043 dev->ib_dev.create_ah = mlx5_ib_create_ah;
4044 dev->ib_dev.query_ah = mlx5_ib_query_ah;
4045 dev->ib_dev.destroy_ah = mlx5_ib_destroy_ah;
4046 dev->ib_dev.create_srq = mlx5_ib_create_srq;
4047 dev->ib_dev.modify_srq = mlx5_ib_modify_srq;
4048 dev->ib_dev.query_srq = mlx5_ib_query_srq;
4049 dev->ib_dev.destroy_srq = mlx5_ib_destroy_srq;
4050 dev->ib_dev.post_srq_recv = mlx5_ib_post_srq_recv;
4051 dev->ib_dev.create_qp = mlx5_ib_create_qp;
4052 dev->ib_dev.modify_qp = mlx5_ib_modify_qp;
4053 dev->ib_dev.query_qp = mlx5_ib_query_qp;
4054 dev->ib_dev.destroy_qp = mlx5_ib_destroy_qp;
4055 dev->ib_dev.post_send = mlx5_ib_post_send;
4056 dev->ib_dev.post_recv = mlx5_ib_post_recv;
4057 dev->ib_dev.create_cq = mlx5_ib_create_cq;
4058 dev->ib_dev.modify_cq = mlx5_ib_modify_cq;
4059 dev->ib_dev.resize_cq = mlx5_ib_resize_cq;
4060 dev->ib_dev.destroy_cq = mlx5_ib_destroy_cq;
4061 dev->ib_dev.poll_cq = mlx5_ib_poll_cq;
4062 dev->ib_dev.req_notify_cq = mlx5_ib_arm_cq;
4063 dev->ib_dev.get_dma_mr = mlx5_ib_get_dma_mr;
4064 dev->ib_dev.reg_user_mr = mlx5_ib_reg_user_mr;
Noa Osherovich56e11d62016-02-29 16:46:51 +02004065 dev->ib_dev.rereg_user_mr = mlx5_ib_rereg_user_mr;
Eli Cohene126ba92013-07-07 17:25:49 +03004066 dev->ib_dev.dereg_mr = mlx5_ib_dereg_mr;
4067 dev->ib_dev.attach_mcast = mlx5_ib_mcg_attach;
4068 dev->ib_dev.detach_mcast = mlx5_ib_mcg_detach;
4069 dev->ib_dev.process_mad = mlx5_ib_process_mad;
Sagi Grimberg9bee1782015-07-30 10:32:35 +03004070 dev->ib_dev.alloc_mr = mlx5_ib_alloc_mr;
Sagi Grimberg8a187ee2015-10-13 19:11:26 +03004071 dev->ib_dev.map_mr_sg = mlx5_ib_map_mr_sg;
Sagi Grimbergd5436ba2014-02-23 14:19:12 +02004072 dev->ib_dev.check_mr_status = mlx5_ib_check_mr_status;
Ira Weiny77386132015-05-13 20:02:58 -04004073 dev->ib_dev.get_port_immutable = mlx5_port_immutable;
Ira Weinyc7342822016-06-15 02:22:01 -04004074 dev->ib_dev.get_dev_fw_str = get_dev_fw_str;
Sagi Grimberg40b24402017-07-13 11:09:42 +03004075 dev->ib_dev.get_vector_affinity = mlx5_ib_get_vector_affinity;
Niranjana Vishwanathapura8e959602017-06-30 13:14:46 -07004076 if (MLX5_CAP_GEN(mdev, ipoib_enhanced_offloads))
Alex Vesker022d0382017-06-14 09:59:06 +03004077 dev->ib_dev.alloc_rdma_netdev = mlx5_ib_alloc_rdma_netdev;
Niranjana Vishwanathapura8e959602017-06-30 13:14:46 -07004078
Eli Coheneff901d2016-03-11 22:58:42 +02004079 if (mlx5_core_is_pf(mdev)) {
4080 dev->ib_dev.get_vf_config = mlx5_ib_get_vf_config;
4081 dev->ib_dev.set_vf_link_state = mlx5_ib_set_vf_link_state;
4082 dev->ib_dev.get_vf_stats = mlx5_ib_get_vf_stats;
4083 dev->ib_dev.set_vf_guid = mlx5_ib_set_vf_guid;
4084 }
Eli Cohene126ba92013-07-07 17:25:49 +03004085
Maor Gottlieb7c2344c2016-06-17 14:56:44 +03004086 dev->ib_dev.disassociate_ucontext = mlx5_ib_disassociate_ucontext;
4087
Saeed Mahameed938fe832015-05-28 22:28:41 +03004088 mlx5_ib_internal_fill_odp_caps(dev);
Haggai Eran8cdd3122014-12-11 17:04:20 +02004089
Max Gurtovoy6e8484c2017-05-28 10:53:11 +03004090 dev->umr_fence = mlx5_get_umr_fence(MLX5_CAP_GEN(mdev, umr_fence));
4091
Matan Barakd2370e02016-02-29 18:05:30 +02004092 if (MLX5_CAP_GEN(mdev, imaicl)) {
4093 dev->ib_dev.alloc_mw = mlx5_ib_alloc_mw;
4094 dev->ib_dev.dealloc_mw = mlx5_ib_dealloc_mw;
4095 dev->ib_dev.uverbs_cmd_mask |=
4096 (1ull << IB_USER_VERBS_CMD_ALLOC_MW) |
4097 (1ull << IB_USER_VERBS_CMD_DEALLOC_MW);
4098 }
4099
Kamal Heib7c16f472017-01-18 15:25:09 +02004100 if (MLX5_CAP_GEN(dev->mdev, max_qp_cnt)) {
Mark Bloch0ad17a82016-06-17 15:10:56 +03004101 dev->ib_dev.get_hw_stats = mlx5_ib_get_hw_stats;
4102 dev->ib_dev.alloc_hw_stats = mlx5_ib_alloc_hw_stats;
4103 }
4104
Saeed Mahameed938fe832015-05-28 22:28:41 +03004105 if (MLX5_CAP_GEN(mdev, xrc)) {
Eli Cohene126ba92013-07-07 17:25:49 +03004106 dev->ib_dev.alloc_xrcd = mlx5_ib_alloc_xrcd;
4107 dev->ib_dev.dealloc_xrcd = mlx5_ib_dealloc_xrcd;
4108 dev->ib_dev.uverbs_cmd_mask |=
4109 (1ull << IB_USER_VERBS_CMD_OPEN_XRCD) |
4110 (1ull << IB_USER_VERBS_CMD_CLOSE_XRCD);
4111 }
4112
Yishai Hadas81e30882017-06-08 16:15:09 +03004113 dev->ib_dev.create_flow = mlx5_ib_create_flow;
4114 dev->ib_dev.destroy_flow = mlx5_ib_destroy_flow;
4115 dev->ib_dev.uverbs_ex_cmd_mask |=
4116 (1ull << IB_USER_VERBS_EX_CMD_CREATE_FLOW) |
4117 (1ull << IB_USER_VERBS_EX_CMD_DESTROY_FLOW);
4118
Linus Torvalds048ccca2016-01-23 18:45:06 -08004119 if (mlx5_ib_port_link_layer(&dev->ib_dev, 1) ==
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02004120 IB_LINK_LAYER_ETHERNET) {
Yishai Hadas79b20a62016-05-23 15:20:50 +03004121 dev->ib_dev.create_wq = mlx5_ib_create_wq;
4122 dev->ib_dev.modify_wq = mlx5_ib_modify_wq;
4123 dev->ib_dev.destroy_wq = mlx5_ib_destroy_wq;
Yishai Hadasc5f90922016-05-23 15:20:53 +03004124 dev->ib_dev.create_rwq_ind_table = mlx5_ib_create_rwq_ind_table;
4125 dev->ib_dev.destroy_rwq_ind_table = mlx5_ib_destroy_rwq_ind_table;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02004126 dev->ib_dev.uverbs_ex_cmd_mask |=
Yishai Hadas79b20a62016-05-23 15:20:50 +03004127 (1ull << IB_USER_VERBS_EX_CMD_CREATE_WQ) |
4128 (1ull << IB_USER_VERBS_EX_CMD_MODIFY_WQ) |
Yishai Hadasc5f90922016-05-23 15:20:53 +03004129 (1ull << IB_USER_VERBS_EX_CMD_DESTROY_WQ) |
4130 (1ull << IB_USER_VERBS_EX_CMD_CREATE_RWQ_IND_TBL) |
4131 (1ull << IB_USER_VERBS_EX_CMD_DESTROY_RWQ_IND_TBL);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02004132 }
Eli Cohene126ba92013-07-07 17:25:49 +03004133 err = init_node_data(dev);
4134 if (err)
Majd Dibbiny90be7c82016-10-27 16:36:39 +03004135 goto err_free_port;
Eli Cohene126ba92013-07-07 17:25:49 +03004136
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02004137 mutex_init(&dev->flow_db.lock);
Eli Cohene126ba92013-07-07 17:25:49 +03004138 mutex_init(&dev->cap_mask_mutex);
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03004139 INIT_LIST_HEAD(&dev->qp_list);
4140 spin_lock_init(&dev->reset_flow_resource_lock);
Eli Cohene126ba92013-07-07 17:25:49 +03004141
Achiad Shochatfc24fc52015-12-23 18:47:17 +02004142 if (ll == IB_LINK_LAYER_ETHERNET) {
Or Gerlitz45f95ac2016-11-27 16:51:35 +02004143 err = mlx5_enable_eth(dev);
Achiad Shochatfc24fc52015-12-23 18:47:17 +02004144 if (err)
Majd Dibbiny90be7c82016-10-27 16:36:39 +03004145 goto err_free_port;
Moni Shouafd65f1b2017-05-30 09:56:05 +03004146 dev->roce.last_port_state = IB_PORT_DOWN;
Achiad Shochatfc24fc52015-12-23 18:47:17 +02004147 }
4148
Eli Cohene126ba92013-07-07 17:25:49 +03004149 err = create_dev_resources(&dev->devr);
4150 if (err)
Or Gerlitz45f95ac2016-11-27 16:51:35 +02004151 goto err_disable_eth;
Eli Cohene126ba92013-07-07 17:25:49 +03004152
Haggai Eran6aec21f2014-12-11 17:04:23 +02004153 err = mlx5_ib_odp_init_one(dev);
Wei Yongjun281d1a92013-07-30 07:54:26 +08004154 if (err)
Eli Cohene126ba92013-07-07 17:25:49 +03004155 goto err_rsrc;
4156
Kamal Heib45bded22017-01-18 14:10:32 +02004157 if (MLX5_CAP_GEN(dev->mdev, max_qp_cnt)) {
Parav Pandite1f24a72017-04-16 07:29:29 +03004158 err = mlx5_ib_alloc_counters(dev);
Kamal Heib45bded22017-01-18 14:10:32 +02004159 if (err)
4160 goto err_odp;
4161 }
Haggai Eran6aec21f2014-12-11 17:04:23 +02004162
Parav Pandit4a2da0b2017-05-30 10:05:15 +03004163 err = mlx5_ib_init_cong_debugfs(dev);
4164 if (err)
4165 goto err_cnt;
4166
Eli Cohen5fe9dec2017-01-03 23:55:25 +02004167 dev->mdev->priv.uar = mlx5_get_uars_page(dev->mdev);
4168 if (!dev->mdev->priv.uar)
Parav Pandit4a2da0b2017-05-30 10:05:15 +03004169 goto err_cong;
Eli Cohen5fe9dec2017-01-03 23:55:25 +02004170
4171 err = mlx5_alloc_bfreg(dev->mdev, &dev->bfreg, false, false);
4172 if (err)
4173 goto err_uar_page;
4174
4175 err = mlx5_alloc_bfreg(dev->mdev, &dev->fp_bfreg, false, true);
4176 if (err)
4177 goto err_bfreg;
4178
Mark Bloch0837e862016-06-17 15:10:55 +03004179 err = ib_register_device(&dev->ib_dev, NULL);
4180 if (err)
Eli Cohen5fe9dec2017-01-03 23:55:25 +02004181 goto err_fp_bfreg;
Mark Bloch0837e862016-06-17 15:10:55 +03004182
Eli Cohene126ba92013-07-07 17:25:49 +03004183 err = create_umr_res(dev);
4184 if (err)
4185 goto err_dev;
4186
Maor Gottlieb03404e82017-05-30 10:29:13 +03004187 init_delay_drop(dev);
4188
Eli Cohene126ba92013-07-07 17:25:49 +03004189 for (i = 0; i < ARRAY_SIZE(mlx5_class_attributes); i++) {
Wei Yongjun281d1a92013-07-30 07:54:26 +08004190 err = device_create_file(&dev->ib_dev.dev,
4191 mlx5_class_attributes[i]);
4192 if (err)
Maor Gottlieb03404e82017-05-30 10:29:13 +03004193 goto err_delay_drop;
Eli Cohene126ba92013-07-07 17:25:49 +03004194 }
4195
Huy Nguyenc85023e2017-05-30 09:42:54 +03004196 if ((MLX5_CAP_GEN(mdev, port_type) == MLX5_CAP_PORT_TYPE_ETH) &&
4197 MLX5_CAP_GEN(mdev, disable_local_lb))
4198 mutex_init(&dev->lb_mutex);
4199
Eli Cohene126ba92013-07-07 17:25:49 +03004200 dev->ib_active = true;
4201
Jack Morgenstein9603b612014-07-28 23:30:22 +03004202 return dev;
Eli Cohene126ba92013-07-07 17:25:49 +03004203
Maor Gottlieb03404e82017-05-30 10:29:13 +03004204err_delay_drop:
4205 cancel_delay_drop(dev);
Eli Cohene126ba92013-07-07 17:25:49 +03004206 destroy_umrc_res(dev);
4207
4208err_dev:
4209 ib_unregister_device(&dev->ib_dev);
4210
Eli Cohen5fe9dec2017-01-03 23:55:25 +02004211err_fp_bfreg:
4212 mlx5_free_bfreg(dev->mdev, &dev->fp_bfreg);
4213
4214err_bfreg:
4215 mlx5_free_bfreg(dev->mdev, &dev->bfreg);
4216
4217err_uar_page:
4218 mlx5_put_uars_page(dev->mdev, dev->mdev->priv.uar);
4219
Parav Pandit4a2da0b2017-05-30 10:05:15 +03004220err_cong:
Parav Pandite19cd282017-10-01 09:54:35 +03004221 mlx5_ib_cleanup_cong_debugfs(dev);
4222err_cnt:
Kamal Heib45bded22017-01-18 14:10:32 +02004223 if (MLX5_CAP_GEN(dev->mdev, max_qp_cnt))
Parav Pandite1f24a72017-04-16 07:29:29 +03004224 mlx5_ib_dealloc_counters(dev);
Mark Bloch0837e862016-06-17 15:10:55 +03004225
Haggai Eran6aec21f2014-12-11 17:04:23 +02004226err_odp:
4227 mlx5_ib_odp_remove_one(dev);
4228
Eli Cohene126ba92013-07-07 17:25:49 +03004229err_rsrc:
4230 destroy_dev_resources(&dev->devr);
4231
Or Gerlitz45f95ac2016-11-27 16:51:35 +02004232err_disable_eth:
Aviv Heller5ec8c832016-09-18 20:48:00 +03004233 if (ll == IB_LINK_LAYER_ETHERNET) {
Or Gerlitz45f95ac2016-11-27 16:51:35 +02004234 mlx5_disable_eth(dev);
Or Gerlitzd012f5d2016-11-27 16:51:34 +02004235 mlx5_remove_netdev_notifier(dev);
Aviv Heller5ec8c832016-09-18 20:48:00 +03004236 }
Achiad Shochatfc24fc52015-12-23 18:47:17 +02004237
Mark Bloch0837e862016-06-17 15:10:55 +03004238err_free_port:
4239 kfree(dev->port);
4240
Jack Morgenstein9603b612014-07-28 23:30:22 +03004241err_dealloc:
Eli Cohene126ba92013-07-07 17:25:49 +03004242 ib_dealloc_device((struct ib_device *)dev);
4243
Jack Morgenstein9603b612014-07-28 23:30:22 +03004244 return NULL;
Eli Cohene126ba92013-07-07 17:25:49 +03004245}
4246
Jack Morgenstein9603b612014-07-28 23:30:22 +03004247static void mlx5_ib_remove(struct mlx5_core_dev *mdev, void *context)
Eli Cohene126ba92013-07-07 17:25:49 +03004248{
Jack Morgenstein9603b612014-07-28 23:30:22 +03004249 struct mlx5_ib_dev *dev = context;
Achiad Shochatfc24fc52015-12-23 18:47:17 +02004250 enum rdma_link_layer ll = mlx5_ib_port_link_layer(&dev->ib_dev, 1);
Haggai Eran6aec21f2014-12-11 17:04:23 +02004251
Maor Gottlieb03404e82017-05-30 10:29:13 +03004252 cancel_delay_drop(dev);
Or Gerlitzd012f5d2016-11-27 16:51:34 +02004253 mlx5_remove_netdev_notifier(dev);
Eli Cohene126ba92013-07-07 17:25:49 +03004254 ib_unregister_device(&dev->ib_dev);
Eli Cohen5fe9dec2017-01-03 23:55:25 +02004255 mlx5_free_bfreg(dev->mdev, &dev->fp_bfreg);
4256 mlx5_free_bfreg(dev->mdev, &dev->bfreg);
4257 mlx5_put_uars_page(dev->mdev, mdev->priv.uar);
Parav Pandit4a2da0b2017-05-30 10:05:15 +03004258 mlx5_ib_cleanup_cong_debugfs(dev);
Kamal Heib45bded22017-01-18 14:10:32 +02004259 if (MLX5_CAP_GEN(dev->mdev, max_qp_cnt))
Parav Pandite1f24a72017-04-16 07:29:29 +03004260 mlx5_ib_dealloc_counters(dev);
Eli Coheneefd56e2014-09-14 16:47:50 +03004261 destroy_umrc_res(dev);
Haggai Eran6aec21f2014-12-11 17:04:23 +02004262 mlx5_ib_odp_remove_one(dev);
Eli Cohene126ba92013-07-07 17:25:49 +03004263 destroy_dev_resources(&dev->devr);
Achiad Shochatfc24fc52015-12-23 18:47:17 +02004264 if (ll == IB_LINK_LAYER_ETHERNET)
Or Gerlitz45f95ac2016-11-27 16:51:35 +02004265 mlx5_disable_eth(dev);
Mark Bloch0837e862016-06-17 15:10:55 +03004266 kfree(dev->port);
Eli Cohene126ba92013-07-07 17:25:49 +03004267 ib_dealloc_device(&dev->ib_dev);
4268}
4269
Jack Morgenstein9603b612014-07-28 23:30:22 +03004270static struct mlx5_interface mlx5_ib_interface = {
4271 .add = mlx5_ib_add,
4272 .remove = mlx5_ib_remove,
4273 .event = mlx5_ib_event,
Artemy Kovalyovd9aaed82017-01-02 11:37:46 +02004274#ifdef CONFIG_INFINIBAND_ON_DEMAND_PAGING
4275 .pfault = mlx5_ib_pfault,
4276#endif
Saeed Mahameed64613d942015-04-02 17:07:34 +03004277 .protocol = MLX5_INTERFACE_PROTOCOL_IB,
Eli Cohene126ba92013-07-07 17:25:49 +03004278};
4279
4280static int __init mlx5_ib_init(void)
4281{
Haggai Eran6aec21f2014-12-11 17:04:23 +02004282 int err;
4283
Artemy Kovalyov81713d32017-01-18 16:58:11 +02004284 mlx5_ib_odp_init();
Jack Morgenstein9603b612014-07-28 23:30:22 +03004285
Haggai Eran6aec21f2014-12-11 17:04:23 +02004286 err = mlx5_register_interface(&mlx5_ib_interface);
Haggai Eran6aec21f2014-12-11 17:04:23 +02004287
4288 return err;
Eli Cohene126ba92013-07-07 17:25:49 +03004289}
4290
4291static void __exit mlx5_ib_cleanup(void)
4292{
Jack Morgenstein9603b612014-07-28 23:30:22 +03004293 mlx5_unregister_interface(&mlx5_ib_interface);
Eli Cohene126ba92013-07-07 17:25:49 +03004294}
4295
4296module_init(mlx5_ib_init);
4297module_exit(mlx5_ib_cleanup);