blob: 2ab505d1e8e3ee6a671497858319a21e54ca29bf [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
Christoph Hellwigadec6402015-08-28 09:27:19 +020033#include <linux/highmem.h>
Eli Cohene126ba92013-07-07 17:25:49 +030034#include <linux/module.h>
35#include <linux/init.h>
36#include <linux/errno.h>
37#include <linux/pci.h>
38#include <linux/dma-mapping.h>
39#include <linux/slab.h>
Guy Levi37aa5c32016-04-27 16:49:50 +030040#if defined(CONFIG_X86)
41#include <asm/pat.h>
42#endif
Eli Cohene126ba92013-07-07 17:25:49 +030043#include <linux/sched.h>
Ingo Molnar6e84f312017-02-08 18:51:29 +010044#include <linux/sched/mm.h>
Ingo Molnar0881e7b2017-02-05 15:30:50 +010045#include <linux/sched/task.h>
Maor Gottlieb7c2344c2016-06-17 14:56:44 +030046#include <linux/delay.h>
Eli Cohene126ba92013-07-07 17:25:49 +030047#include <rdma/ib_user_verbs.h>
Achiad Shochat3f89a642015-12-23 18:47:21 +020048#include <rdma/ib_addr.h>
Achiad Shochat2811ba52015-12-23 18:47:24 +020049#include <rdma/ib_cache.h>
Achiad Shochatada68c32016-02-22 18:17:23 +020050#include <linux/mlx5/port.h>
Majd Dibbiny1b5daf12015-06-04 19:30:46 +030051#include <linux/mlx5/vport.h>
Maor Gottlieb7c2344c2016-06-17 14:56:44 +030052#include <linux/list.h>
Eli Cohene126ba92013-07-07 17:25:49 +030053#include <rdma/ib_smi.h>
54#include <rdma/ib_umem.h>
Maor Gottlieb038d2ef2016-01-11 10:26:07 +020055#include <linux/in.h>
56#include <linux/etherdevice.h>
57#include <linux/mlx5/fs.h>
Or Gerlitz78984892016-11-30 20:33:33 +020058#include <linux/mlx5/vport.h>
Eli Cohene126ba92013-07-07 17:25:49 +030059#include "mlx5_ib.h"
Parav Pandite1f24a72017-04-16 07:29:29 +030060#include "cmd.h"
Eli Cohene126ba92013-07-07 17:25:49 +030061
62#define DRIVER_NAME "mlx5_ib"
Tariq Toukanb3599112017-02-22 17:45:46 +020063#define DRIVER_VERSION "5.0-0"
Eli Cohene126ba92013-07-07 17:25:49 +030064
65MODULE_AUTHOR("Eli Cohen <eli@mellanox.com>");
66MODULE_DESCRIPTION("Mellanox Connect-IB HCA IB driver");
67MODULE_LICENSE("Dual BSD/GPL");
68MODULE_VERSION(DRIVER_VERSION);
69
Eli Cohene126ba92013-07-07 17:25:49 +030070static char mlx5_version[] =
71 DRIVER_NAME ": Mellanox Connect-IB Infiniband driver v"
Tariq Toukanb3599112017-02-22 17:45:46 +020072 DRIVER_VERSION "\n";
Eli Cohene126ba92013-07-07 17:25:49 +030073
Eran Ben Elishada7525d2015-12-14 16:34:10 +020074enum {
75 MLX5_ATOMIC_SIZE_QP_8BYTES = 1 << 3,
76};
Majd Dibbiny1b5daf12015-06-04 19:30:46 +030077
Majd Dibbiny1b5daf12015-06-04 19:30:46 +030078static enum rdma_link_layer
Achiad Shochatebd61f62015-12-23 18:47:16 +020079mlx5_port_type_cap_to_rdma_ll(int port_type_cap)
Majd Dibbiny1b5daf12015-06-04 19:30:46 +030080{
Achiad Shochatebd61f62015-12-23 18:47:16 +020081 switch (port_type_cap) {
Majd Dibbiny1b5daf12015-06-04 19:30:46 +030082 case MLX5_CAP_PORT_TYPE_IB:
83 return IB_LINK_LAYER_INFINIBAND;
84 case MLX5_CAP_PORT_TYPE_ETH:
85 return IB_LINK_LAYER_ETHERNET;
86 default:
87 return IB_LINK_LAYER_UNSPECIFIED;
88 }
89}
90
Achiad Shochatebd61f62015-12-23 18:47:16 +020091static enum rdma_link_layer
92mlx5_ib_port_link_layer(struct ib_device *device, u8 port_num)
93{
94 struct mlx5_ib_dev *dev = to_mdev(device);
95 int port_type_cap = MLX5_CAP_GEN(dev->mdev, port_type);
96
97 return mlx5_port_type_cap_to_rdma_ll(port_type_cap);
98}
99
Achiad Shochatfc24fc52015-12-23 18:47:17 +0200100static int mlx5_netdev_event(struct notifier_block *this,
101 unsigned long event, void *ptr)
102{
103 struct net_device *ndev = netdev_notifier_info_to_dev(ptr);
104 struct mlx5_ib_dev *ibdev = container_of(this, struct mlx5_ib_dev,
105 roce.nb);
106
Aviv Heller5ec8c832016-09-18 20:48:00 +0300107 switch (event) {
108 case NETDEV_REGISTER:
109 case NETDEV_UNREGISTER:
110 write_lock(&ibdev->roce.netdev_lock);
111 if (ndev->dev.parent == &ibdev->mdev->pdev->dev)
112 ibdev->roce.netdev = (event == NETDEV_UNREGISTER) ?
113 NULL : ndev;
114 write_unlock(&ibdev->roce.netdev_lock);
115 break;
Achiad Shochatfc24fc52015-12-23 18:47:17 +0200116
Aviv Heller5ec8c832016-09-18 20:48:00 +0300117 case NETDEV_UP:
Aviv Heller88621df2016-09-18 20:48:02 +0300118 case NETDEV_DOWN: {
119 struct net_device *lag_ndev = mlx5_lag_get_roce_netdev(ibdev->mdev);
120 struct net_device *upper = NULL;
121
122 if (lag_ndev) {
123 upper = netdev_master_upper_dev_get(lag_ndev);
124 dev_put(lag_ndev);
125 }
126
127 if ((upper == ndev || (!upper && ndev == ibdev->roce.netdev))
128 && ibdev->ib_active) {
Bart Van Assche626bc022016-12-05 17:18:08 -0800129 struct ib_event ibev = { };
Aviv Heller5ec8c832016-09-18 20:48:00 +0300130
131 ibev.device = &ibdev->ib_dev;
132 ibev.event = (event == NETDEV_UP) ?
133 IB_EVENT_PORT_ACTIVE : IB_EVENT_PORT_ERR;
134 ibev.element.port_num = 1;
135 ib_dispatch_event(&ibev);
136 }
137 break;
Aviv Heller88621df2016-09-18 20:48:02 +0300138 }
Aviv Heller5ec8c832016-09-18 20:48:00 +0300139
140 default:
141 break;
142 }
Achiad Shochatfc24fc52015-12-23 18:47:17 +0200143
144 return NOTIFY_DONE;
145}
146
147static struct net_device *mlx5_ib_get_netdev(struct ib_device *device,
148 u8 port_num)
149{
150 struct mlx5_ib_dev *ibdev = to_mdev(device);
151 struct net_device *ndev;
152
Aviv Heller88621df2016-09-18 20:48:02 +0300153 ndev = mlx5_lag_get_roce_netdev(ibdev->mdev);
154 if (ndev)
155 return ndev;
156
Achiad Shochatfc24fc52015-12-23 18:47:17 +0200157 /* Ensure ndev does not disappear before we invoke dev_hold()
158 */
159 read_lock(&ibdev->roce.netdev_lock);
160 ndev = ibdev->roce.netdev;
161 if (ndev)
162 dev_hold(ndev);
163 read_unlock(&ibdev->roce.netdev_lock);
164
165 return ndev;
166}
167
Noa Osherovichf1b65df2017-04-20 20:53:33 +0300168static int translate_eth_proto_oper(u32 eth_proto_oper, u8 *active_speed,
169 u8 *active_width)
170{
171 switch (eth_proto_oper) {
172 case MLX5E_PROT_MASK(MLX5E_1000BASE_CX_SGMII):
173 case MLX5E_PROT_MASK(MLX5E_1000BASE_KX):
174 case MLX5E_PROT_MASK(MLX5E_100BASE_TX):
175 case MLX5E_PROT_MASK(MLX5E_1000BASE_T):
176 *active_width = IB_WIDTH_1X;
177 *active_speed = IB_SPEED_SDR;
178 break;
179 case MLX5E_PROT_MASK(MLX5E_10GBASE_T):
180 case MLX5E_PROT_MASK(MLX5E_10GBASE_CX4):
181 case MLX5E_PROT_MASK(MLX5E_10GBASE_KX4):
182 case MLX5E_PROT_MASK(MLX5E_10GBASE_KR):
183 case MLX5E_PROT_MASK(MLX5E_10GBASE_CR):
184 case MLX5E_PROT_MASK(MLX5E_10GBASE_SR):
185 case MLX5E_PROT_MASK(MLX5E_10GBASE_ER):
186 *active_width = IB_WIDTH_1X;
187 *active_speed = IB_SPEED_QDR;
188 break;
189 case MLX5E_PROT_MASK(MLX5E_25GBASE_CR):
190 case MLX5E_PROT_MASK(MLX5E_25GBASE_KR):
191 case MLX5E_PROT_MASK(MLX5E_25GBASE_SR):
192 *active_width = IB_WIDTH_1X;
193 *active_speed = IB_SPEED_EDR;
194 break;
195 case MLX5E_PROT_MASK(MLX5E_40GBASE_CR4):
196 case MLX5E_PROT_MASK(MLX5E_40GBASE_KR4):
197 case MLX5E_PROT_MASK(MLX5E_40GBASE_SR4):
198 case MLX5E_PROT_MASK(MLX5E_40GBASE_LR4):
199 *active_width = IB_WIDTH_4X;
200 *active_speed = IB_SPEED_QDR;
201 break;
202 case MLX5E_PROT_MASK(MLX5E_50GBASE_CR2):
203 case MLX5E_PROT_MASK(MLX5E_50GBASE_KR2):
204 case MLX5E_PROT_MASK(MLX5E_50GBASE_SR2):
205 *active_width = IB_WIDTH_1X;
206 *active_speed = IB_SPEED_HDR;
207 break;
208 case MLX5E_PROT_MASK(MLX5E_56GBASE_R4):
209 *active_width = IB_WIDTH_4X;
210 *active_speed = IB_SPEED_FDR;
211 break;
212 case MLX5E_PROT_MASK(MLX5E_100GBASE_CR4):
213 case MLX5E_PROT_MASK(MLX5E_100GBASE_SR4):
214 case MLX5E_PROT_MASK(MLX5E_100GBASE_KR4):
215 case MLX5E_PROT_MASK(MLX5E_100GBASE_LR4):
216 *active_width = IB_WIDTH_4X;
217 *active_speed = IB_SPEED_EDR;
218 break;
219 default:
220 return -EINVAL;
221 }
222
223 return 0;
224}
225
Noa Osherovich50f22fd2017-04-20 20:53:32 +0300226static void mlx5_query_port_roce(struct ib_device *device, u8 port_num,
227 struct ib_port_attr *props)
Achiad Shochat3f89a642015-12-23 18:47:21 +0200228{
229 struct mlx5_ib_dev *dev = to_mdev(device);
Noa Osherovichf1b65df2017-04-20 20:53:33 +0300230 struct mlx5_core_dev *mdev = dev->mdev;
Aviv Heller88621df2016-09-18 20:48:02 +0300231 struct net_device *ndev, *upper;
Achiad Shochat3f89a642015-12-23 18:47:21 +0200232 enum ib_mtu ndev_ib_mtu;
Leon Romanovskyc876a1b2016-01-09 13:06:25 +0200233 u16 qkey_viol_cntr;
Noa Osherovichf1b65df2017-04-20 20:53:33 +0300234 u32 eth_prot_oper;
Achiad Shochat3f89a642015-12-23 18:47:21 +0200235
Noa Osherovichf1b65df2017-04-20 20:53:33 +0300236 /* Possible bad flows are checked before filling out props so in case
237 * of an error it will still be zeroed out.
Noa Osherovich50f22fd2017-04-20 20:53:32 +0300238 */
Noa Osherovichf1b65df2017-04-20 20:53:33 +0300239 if (mlx5_query_port_eth_proto_oper(mdev, &eth_prot_oper, port_num))
240 return;
241
242 translate_eth_proto_oper(eth_prot_oper, &props->active_speed,
243 &props->active_width);
Achiad Shochat3f89a642015-12-23 18:47:21 +0200244
245 props->port_cap_flags |= IB_PORT_CM_SUP;
246 props->port_cap_flags |= IB_PORT_IP_BASED_GIDS;
247
248 props->gid_tbl_len = MLX5_CAP_ROCE(dev->mdev,
249 roce_address_table_size);
250 props->max_mtu = IB_MTU_4096;
251 props->max_msg_sz = 1 << MLX5_CAP_GEN(dev->mdev, log_max_msg);
252 props->pkey_tbl_len = 1;
253 props->state = IB_PORT_DOWN;
254 props->phys_state = 3;
255
Leon Romanovskyc876a1b2016-01-09 13:06:25 +0200256 mlx5_query_nic_vport_qkey_viol_cntr(dev->mdev, &qkey_viol_cntr);
257 props->qkey_viol_cntr = qkey_viol_cntr;
Achiad Shochat3f89a642015-12-23 18:47:21 +0200258
259 ndev = mlx5_ib_get_netdev(device, port_num);
260 if (!ndev)
Noa Osherovich50f22fd2017-04-20 20:53:32 +0300261 return;
Achiad Shochat3f89a642015-12-23 18:47:21 +0200262
Aviv Heller88621df2016-09-18 20:48:02 +0300263 if (mlx5_lag_is_active(dev->mdev)) {
264 rcu_read_lock();
265 upper = netdev_master_upper_dev_get_rcu(ndev);
266 if (upper) {
267 dev_put(ndev);
268 ndev = upper;
269 dev_hold(ndev);
270 }
271 rcu_read_unlock();
272 }
273
Achiad Shochat3f89a642015-12-23 18:47:21 +0200274 if (netif_running(ndev) && netif_carrier_ok(ndev)) {
275 props->state = IB_PORT_ACTIVE;
276 props->phys_state = 5;
277 }
278
279 ndev_ib_mtu = iboe_get_mtu(ndev->mtu);
280
281 dev_put(ndev);
282
283 props->active_mtu = min(props->max_mtu, ndev_ib_mtu);
Achiad Shochat3f89a642015-12-23 18:47:21 +0200284}
285
Achiad Shochat3cca2602015-12-23 18:47:23 +0200286static void ib_gid_to_mlx5_roce_addr(const union ib_gid *gid,
287 const struct ib_gid_attr *attr,
288 void *mlx5_addr)
289{
290#define MLX5_SET_RA(p, f, v) MLX5_SET(roce_addr_layout, p, f, v)
291 char *mlx5_addr_l3_addr = MLX5_ADDR_OF(roce_addr_layout, mlx5_addr,
292 source_l3_address);
293 void *mlx5_addr_mac = MLX5_ADDR_OF(roce_addr_layout, mlx5_addr,
294 source_mac_47_32);
295
296 if (!gid)
297 return;
298
299 ether_addr_copy(mlx5_addr_mac, attr->ndev->dev_addr);
300
301 if (is_vlan_dev(attr->ndev)) {
302 MLX5_SET_RA(mlx5_addr, vlan_valid, 1);
303 MLX5_SET_RA(mlx5_addr, vlan_id, vlan_dev_vlan_id(attr->ndev));
304 }
305
306 switch (attr->gid_type) {
307 case IB_GID_TYPE_IB:
308 MLX5_SET_RA(mlx5_addr, roce_version, MLX5_ROCE_VERSION_1);
309 break;
310 case IB_GID_TYPE_ROCE_UDP_ENCAP:
311 MLX5_SET_RA(mlx5_addr, roce_version, MLX5_ROCE_VERSION_2);
312 break;
313
314 default:
315 WARN_ON(true);
316 }
317
318 if (attr->gid_type != IB_GID_TYPE_IB) {
319 if (ipv6_addr_v4mapped((void *)gid))
320 MLX5_SET_RA(mlx5_addr, roce_l3_type,
321 MLX5_ROCE_L3_TYPE_IPV4);
322 else
323 MLX5_SET_RA(mlx5_addr, roce_l3_type,
324 MLX5_ROCE_L3_TYPE_IPV6);
325 }
326
327 if ((attr->gid_type == IB_GID_TYPE_IB) ||
328 !ipv6_addr_v4mapped((void *)gid))
329 memcpy(mlx5_addr_l3_addr, gid, sizeof(*gid));
330 else
331 memcpy(&mlx5_addr_l3_addr[12], &gid->raw[12], 4);
332}
333
334static int set_roce_addr(struct ib_device *device, u8 port_num,
335 unsigned int index,
336 const union ib_gid *gid,
337 const struct ib_gid_attr *attr)
338{
Saeed Mahameedc4f287c2016-07-19 20:17:12 +0300339 struct mlx5_ib_dev *dev = to_mdev(device);
340 u32 in[MLX5_ST_SZ_DW(set_roce_address_in)] = {0};
341 u32 out[MLX5_ST_SZ_DW(set_roce_address_out)] = {0};
Achiad Shochat3cca2602015-12-23 18:47:23 +0200342 void *in_addr = MLX5_ADDR_OF(set_roce_address_in, in, roce_address);
343 enum rdma_link_layer ll = mlx5_ib_port_link_layer(device, port_num);
344
345 if (ll != IB_LINK_LAYER_ETHERNET)
346 return -EINVAL;
347
Achiad Shochat3cca2602015-12-23 18:47:23 +0200348 ib_gid_to_mlx5_roce_addr(gid, attr, in_addr);
349
350 MLX5_SET(set_roce_address_in, in, roce_address_index, index);
351 MLX5_SET(set_roce_address_in, in, opcode, MLX5_CMD_OP_SET_ROCE_ADDRESS);
Achiad Shochat3cca2602015-12-23 18:47:23 +0200352 return mlx5_cmd_exec(dev->mdev, in, sizeof(in), out, sizeof(out));
353}
354
355static int mlx5_ib_add_gid(struct ib_device *device, u8 port_num,
356 unsigned int index, const union ib_gid *gid,
357 const struct ib_gid_attr *attr,
358 __always_unused void **context)
359{
360 return set_roce_addr(device, port_num, index, gid, attr);
361}
362
363static int mlx5_ib_del_gid(struct ib_device *device, u8 port_num,
364 unsigned int index, __always_unused void **context)
365{
366 return set_roce_addr(device, port_num, index, NULL, NULL);
367}
368
Achiad Shochat2811ba52015-12-23 18:47:24 +0200369__be16 mlx5_get_roce_udp_sport(struct mlx5_ib_dev *dev, u8 port_num,
370 int index)
371{
372 struct ib_gid_attr attr;
373 union ib_gid gid;
374
375 if (ib_get_cached_gid(&dev->ib_dev, port_num, index, &gid, &attr))
376 return 0;
377
378 if (!attr.ndev)
379 return 0;
380
381 dev_put(attr.ndev);
382
383 if (attr.gid_type != IB_GID_TYPE_ROCE_UDP_ENCAP)
384 return 0;
385
386 return cpu_to_be16(MLX5_CAP_ROCE(dev->mdev, r_roce_min_src_udp_port));
387}
388
Majd Dibbinyed884512017-01-18 14:10:35 +0200389int mlx5_get_roce_gid_type(struct mlx5_ib_dev *dev, u8 port_num,
390 int index, enum ib_gid_type *gid_type)
391{
392 struct ib_gid_attr attr;
393 union ib_gid gid;
394 int ret;
395
396 ret = ib_get_cached_gid(&dev->ib_dev, port_num, index, &gid, &attr);
397 if (ret)
398 return ret;
399
400 if (!attr.ndev)
401 return -ENODEV;
402
403 dev_put(attr.ndev);
404
405 *gid_type = attr.gid_type;
406
407 return 0;
408}
409
Majd Dibbiny1b5daf12015-06-04 19:30:46 +0300410static int mlx5_use_mad_ifc(struct mlx5_ib_dev *dev)
411{
Noa Osherovich7fae6652016-09-12 19:16:23 +0300412 if (MLX5_CAP_GEN(dev->mdev, port_type) == MLX5_CAP_PORT_TYPE_IB)
413 return !MLX5_CAP_GEN(dev->mdev, ib_virt);
414 return 0;
Majd Dibbiny1b5daf12015-06-04 19:30:46 +0300415}
416
417enum {
418 MLX5_VPORT_ACCESS_METHOD_MAD,
419 MLX5_VPORT_ACCESS_METHOD_HCA,
420 MLX5_VPORT_ACCESS_METHOD_NIC,
421};
422
423static int mlx5_get_vport_access_method(struct ib_device *ibdev)
424{
425 if (mlx5_use_mad_ifc(to_mdev(ibdev)))
426 return MLX5_VPORT_ACCESS_METHOD_MAD;
427
Achiad Shochatebd61f62015-12-23 18:47:16 +0200428 if (mlx5_ib_port_link_layer(ibdev, 1) ==
Majd Dibbiny1b5daf12015-06-04 19:30:46 +0300429 IB_LINK_LAYER_ETHERNET)
430 return MLX5_VPORT_ACCESS_METHOD_NIC;
431
432 return MLX5_VPORT_ACCESS_METHOD_HCA;
433}
434
Eran Ben Elishada7525d2015-12-14 16:34:10 +0200435static void get_atomic_caps(struct mlx5_ib_dev *dev,
436 struct ib_device_attr *props)
437{
438 u8 tmp;
439 u8 atomic_operations = MLX5_CAP_ATOMIC(dev->mdev, atomic_operations);
440 u8 atomic_size_qp = MLX5_CAP_ATOMIC(dev->mdev, atomic_size_qp);
441 u8 atomic_req_8B_endianness_mode =
Or Gerlitzbd108382017-05-28 15:24:17 +0300442 MLX5_CAP_ATOMIC(dev->mdev, atomic_req_8B_endianness_mode);
Eran Ben Elishada7525d2015-12-14 16:34:10 +0200443
444 /* Check if HW supports 8 bytes standard atomic operations and capable
445 * of host endianness respond
446 */
447 tmp = MLX5_ATOMIC_OPS_CMP_SWAP | MLX5_ATOMIC_OPS_FETCH_ADD;
448 if (((atomic_operations & tmp) == tmp) &&
449 (atomic_size_qp & MLX5_ATOMIC_SIZE_QP_8BYTES) &&
450 (atomic_req_8B_endianness_mode)) {
451 props->atomic_cap = IB_ATOMIC_HCA;
452 } else {
453 props->atomic_cap = IB_ATOMIC_NONE;
454 }
455}
456
Majd Dibbiny1b5daf12015-06-04 19:30:46 +0300457static int mlx5_query_system_image_guid(struct ib_device *ibdev,
458 __be64 *sys_image_guid)
459{
460 struct mlx5_ib_dev *dev = to_mdev(ibdev);
461 struct mlx5_core_dev *mdev = dev->mdev;
462 u64 tmp;
463 int err;
464
465 switch (mlx5_get_vport_access_method(ibdev)) {
466 case MLX5_VPORT_ACCESS_METHOD_MAD:
467 return mlx5_query_mad_ifc_system_image_guid(ibdev,
468 sys_image_guid);
469
470 case MLX5_VPORT_ACCESS_METHOD_HCA:
471 err = mlx5_query_hca_vport_system_image_guid(mdev, &tmp);
Achiad Shochat3f89a642015-12-23 18:47:21 +0200472 break;
473
474 case MLX5_VPORT_ACCESS_METHOD_NIC:
475 err = mlx5_query_nic_vport_system_image_guid(mdev, &tmp);
476 break;
Majd Dibbiny1b5daf12015-06-04 19:30:46 +0300477
478 default:
479 return -EINVAL;
480 }
Achiad Shochat3f89a642015-12-23 18:47:21 +0200481
482 if (!err)
483 *sys_image_guid = cpu_to_be64(tmp);
484
485 return err;
486
Majd Dibbiny1b5daf12015-06-04 19:30:46 +0300487}
488
489static int mlx5_query_max_pkeys(struct ib_device *ibdev,
490 u16 *max_pkeys)
491{
492 struct mlx5_ib_dev *dev = to_mdev(ibdev);
493 struct mlx5_core_dev *mdev = dev->mdev;
494
495 switch (mlx5_get_vport_access_method(ibdev)) {
496 case MLX5_VPORT_ACCESS_METHOD_MAD:
497 return mlx5_query_mad_ifc_max_pkeys(ibdev, max_pkeys);
498
499 case MLX5_VPORT_ACCESS_METHOD_HCA:
500 case MLX5_VPORT_ACCESS_METHOD_NIC:
501 *max_pkeys = mlx5_to_sw_pkey_sz(MLX5_CAP_GEN(mdev,
502 pkey_table_size));
503 return 0;
504
505 default:
506 return -EINVAL;
507 }
508}
509
510static int mlx5_query_vendor_id(struct ib_device *ibdev,
511 u32 *vendor_id)
512{
513 struct mlx5_ib_dev *dev = to_mdev(ibdev);
514
515 switch (mlx5_get_vport_access_method(ibdev)) {
516 case MLX5_VPORT_ACCESS_METHOD_MAD:
517 return mlx5_query_mad_ifc_vendor_id(ibdev, vendor_id);
518
519 case MLX5_VPORT_ACCESS_METHOD_HCA:
520 case MLX5_VPORT_ACCESS_METHOD_NIC:
521 return mlx5_core_query_vendor_id(dev->mdev, vendor_id);
522
523 default:
524 return -EINVAL;
525 }
526}
527
528static int mlx5_query_node_guid(struct mlx5_ib_dev *dev,
529 __be64 *node_guid)
530{
531 u64 tmp;
532 int err;
533
534 switch (mlx5_get_vport_access_method(&dev->ib_dev)) {
535 case MLX5_VPORT_ACCESS_METHOD_MAD:
536 return mlx5_query_mad_ifc_node_guid(dev, node_guid);
537
538 case MLX5_VPORT_ACCESS_METHOD_HCA:
539 err = mlx5_query_hca_vport_node_guid(dev->mdev, &tmp);
Achiad Shochat3f89a642015-12-23 18:47:21 +0200540 break;
541
542 case MLX5_VPORT_ACCESS_METHOD_NIC:
543 err = mlx5_query_nic_vport_node_guid(dev->mdev, &tmp);
544 break;
Majd Dibbiny1b5daf12015-06-04 19:30:46 +0300545
546 default:
547 return -EINVAL;
548 }
Achiad Shochat3f89a642015-12-23 18:47:21 +0200549
550 if (!err)
551 *node_guid = cpu_to_be64(tmp);
552
553 return err;
Majd Dibbiny1b5daf12015-06-04 19:30:46 +0300554}
555
556struct mlx5_reg_node_desc {
Yuval Shaiabd99fde2016-08-25 10:57:07 -0700557 u8 desc[IB_DEVICE_NODE_DESC_MAX];
Majd Dibbiny1b5daf12015-06-04 19:30:46 +0300558};
559
560static int mlx5_query_node_desc(struct mlx5_ib_dev *dev, char *node_desc)
561{
562 struct mlx5_reg_node_desc in;
563
564 if (mlx5_use_mad_ifc(dev))
565 return mlx5_query_mad_ifc_node_desc(dev, node_desc);
566
567 memset(&in, 0, sizeof(in));
568
569 return mlx5_core_access_reg(dev->mdev, &in, sizeof(in), node_desc,
570 sizeof(struct mlx5_reg_node_desc),
571 MLX5_REG_NODE_DESC, 0, 0);
572}
573
Eli Cohene126ba92013-07-07 17:25:49 +0300574static int mlx5_ib_query_device(struct ib_device *ibdev,
Matan Barak2528e332015-06-11 16:35:25 +0300575 struct ib_device_attr *props,
576 struct ib_udata *uhw)
Eli Cohene126ba92013-07-07 17:25:49 +0300577{
578 struct mlx5_ib_dev *dev = to_mdev(ibdev);
Saeed Mahameed938fe832015-05-28 22:28:41 +0300579 struct mlx5_core_dev *mdev = dev->mdev;
Eli Cohene126ba92013-07-07 17:25:49 +0300580 int err = -ENOMEM;
Eli Cohen288c01b2016-10-27 16:36:45 +0300581 int max_sq_desc;
Eli Cohene126ba92013-07-07 17:25:49 +0300582 int max_rq_sg;
583 int max_sq_sg;
Sagi Grimberge0238a62015-07-21 14:40:12 +0300584 u64 min_page_size = 1ull << MLX5_CAP_GEN(mdev, log_pg_sz);
Bodong Wang402ca532016-06-17 15:02:20 +0300585 struct mlx5_ib_query_device_resp resp = {};
586 size_t resp_len;
587 u64 max_tso;
Eli Cohene126ba92013-07-07 17:25:49 +0300588
Bodong Wang402ca532016-06-17 15:02:20 +0300589 resp_len = sizeof(resp.comp_mask) + sizeof(resp.response_length);
590 if (uhw->outlen && uhw->outlen < resp_len)
591 return -EINVAL;
592 else
593 resp.response_length = resp_len;
594
595 if (uhw->inlen && !ib_is_udata_cleared(uhw, 0, uhw->inlen))
Matan Barak2528e332015-06-11 16:35:25 +0300596 return -EINVAL;
597
Eli Cohene126ba92013-07-07 17:25:49 +0300598 memset(props, 0, sizeof(*props));
Majd Dibbiny1b5daf12015-06-04 19:30:46 +0300599 err = mlx5_query_system_image_guid(ibdev,
600 &props->sys_image_guid);
601 if (err)
602 return err;
603
604 err = mlx5_query_max_pkeys(ibdev, &props->max_pkeys);
605 if (err)
606 return err;
607
608 err = mlx5_query_vendor_id(ibdev, &props->vendor_id);
609 if (err)
610 return err;
Eli Cohene126ba92013-07-07 17:25:49 +0300611
Jack Morgenstein9603b612014-07-28 23:30:22 +0300612 props->fw_ver = ((u64)fw_rev_maj(dev->mdev) << 32) |
613 (fw_rev_min(dev->mdev) << 16) |
614 fw_rev_sub(dev->mdev);
Eli Cohene126ba92013-07-07 17:25:49 +0300615 props->device_cap_flags = IB_DEVICE_CHANGE_PHY_PORT |
616 IB_DEVICE_PORT_ACTIVE_EVENT |
617 IB_DEVICE_SYS_IMAGE_GUID |
Eli Cohen1a4c3a32014-02-06 17:41:25 +0200618 IB_DEVICE_RC_RNR_NAK_GEN;
Saeed Mahameed938fe832015-05-28 22:28:41 +0300619
620 if (MLX5_CAP_GEN(mdev, pkv))
Eli Cohene126ba92013-07-07 17:25:49 +0300621 props->device_cap_flags |= IB_DEVICE_BAD_PKEY_CNTR;
Saeed Mahameed938fe832015-05-28 22:28:41 +0300622 if (MLX5_CAP_GEN(mdev, qkv))
Eli Cohene126ba92013-07-07 17:25:49 +0300623 props->device_cap_flags |= IB_DEVICE_BAD_QKEY_CNTR;
Saeed Mahameed938fe832015-05-28 22:28:41 +0300624 if (MLX5_CAP_GEN(mdev, apm))
Eli Cohene126ba92013-07-07 17:25:49 +0300625 props->device_cap_flags |= IB_DEVICE_AUTO_PATH_MIG;
Saeed Mahameed938fe832015-05-28 22:28:41 +0300626 if (MLX5_CAP_GEN(mdev, xrc))
Eli Cohene126ba92013-07-07 17:25:49 +0300627 props->device_cap_flags |= IB_DEVICE_XRC;
Matan Barakd2370e02016-02-29 18:05:30 +0200628 if (MLX5_CAP_GEN(mdev, imaicl)) {
629 props->device_cap_flags |= IB_DEVICE_MEM_WINDOW |
630 IB_DEVICE_MEM_WINDOW_TYPE_2B;
631 props->max_mw = 1 << MLX5_CAP_GEN(mdev, log_max_mkey);
Sagi Grimbergb005d312016-02-29 19:07:33 +0200632 /* We support 'Gappy' memory registration too */
633 props->device_cap_flags |= IB_DEVICE_SG_GAPS_REG;
Matan Barakd2370e02016-02-29 18:05:30 +0200634 }
Eli Cohene126ba92013-07-07 17:25:49 +0300635 props->device_cap_flags |= IB_DEVICE_MEM_MGT_EXTENSIONS;
Saeed Mahameed938fe832015-05-28 22:28:41 +0300636 if (MLX5_CAP_GEN(mdev, sho)) {
Sagi Grimberg2dea9092014-02-23 14:19:13 +0200637 props->device_cap_flags |= IB_DEVICE_SIGNATURE_HANDOVER;
638 /* At this stage no support for signature handover */
639 props->sig_prot_cap = IB_PROT_T10DIF_TYPE_1 |
640 IB_PROT_T10DIF_TYPE_2 |
641 IB_PROT_T10DIF_TYPE_3;
642 props->sig_guard_cap = IB_GUARD_T10DIF_CRC |
643 IB_GUARD_T10DIF_CSUM;
644 }
Saeed Mahameed938fe832015-05-28 22:28:41 +0300645 if (MLX5_CAP_GEN(mdev, block_lb_mc))
Eli Cohenf360d882014-04-02 00:10:16 +0300646 props->device_cap_flags |= IB_DEVICE_BLOCK_MULTICAST_LOOPBACK;
Eli Cohene126ba92013-07-07 17:25:49 +0300647
Bodong Wang402ca532016-06-17 15:02:20 +0300648 if (MLX5_CAP_GEN(dev->mdev, eth_net_offloads)) {
Noa Osheroviche8161332017-01-18 15:40:01 +0200649 if (MLX5_CAP_ETH(mdev, csum_cap)) {
650 /* Legacy bit to support old userspace libraries */
Bodong Wang88115fe2015-12-18 13:53:20 +0200651 props->device_cap_flags |= IB_DEVICE_RAW_IP_CSUM;
Noa Osheroviche8161332017-01-18 15:40:01 +0200652 props->raw_packet_caps |= IB_RAW_PACKET_CAP_IP_CSUM;
653 }
654
655 if (MLX5_CAP_ETH(dev->mdev, vlan_cap))
656 props->raw_packet_caps |=
657 IB_RAW_PACKET_CAP_CVLAN_STRIPPING;
Bodong Wang88115fe2015-12-18 13:53:20 +0200658
Bodong Wang402ca532016-06-17 15:02:20 +0300659 if (field_avail(typeof(resp), tso_caps, uhw->outlen)) {
660 max_tso = MLX5_CAP_ETH(mdev, max_lso_cap);
661 if (max_tso) {
662 resp.tso_caps.max_tso = 1 << max_tso;
663 resp.tso_caps.supported_qpts |=
664 1 << IB_QPT_RAW_PACKET;
665 resp.response_length += sizeof(resp.tso_caps);
666 }
667 }
Yishai Hadas31f69a82016-08-28 11:28:45 +0300668
669 if (field_avail(typeof(resp), rss_caps, uhw->outlen)) {
670 resp.rss_caps.rx_hash_function =
671 MLX5_RX_HASH_FUNC_TOEPLITZ;
672 resp.rss_caps.rx_hash_fields_mask =
673 MLX5_RX_HASH_SRC_IPV4 |
674 MLX5_RX_HASH_DST_IPV4 |
675 MLX5_RX_HASH_SRC_IPV6 |
676 MLX5_RX_HASH_DST_IPV6 |
677 MLX5_RX_HASH_SRC_PORT_TCP |
678 MLX5_RX_HASH_DST_PORT_TCP |
679 MLX5_RX_HASH_SRC_PORT_UDP |
680 MLX5_RX_HASH_DST_PORT_UDP;
681 resp.response_length += sizeof(resp.rss_caps);
682 }
683 } else {
684 if (field_avail(typeof(resp), tso_caps, uhw->outlen))
685 resp.response_length += sizeof(resp.tso_caps);
686 if (field_avail(typeof(resp), rss_caps, uhw->outlen))
687 resp.response_length += sizeof(resp.rss_caps);
Bodong Wang402ca532016-06-17 15:02:20 +0300688 }
689
Erez Shitritf0313962016-02-21 16:27:17 +0200690 if (MLX5_CAP_GEN(mdev, ipoib_basic_offloads)) {
691 props->device_cap_flags |= IB_DEVICE_UD_IP_CSUM;
692 props->device_cap_flags |= IB_DEVICE_UD_TSO;
693 }
694
Majd Dibbinycff5a0f2016-04-17 17:19:38 +0300695 if (MLX5_CAP_GEN(dev->mdev, eth_net_offloads) &&
Noa Osheroviche8161332017-01-18 15:40:01 +0200696 MLX5_CAP_ETH(dev->mdev, scatter_fcs)) {
697 /* Legacy bit to support old userspace libraries */
Majd Dibbinycff5a0f2016-04-17 17:19:38 +0300698 props->device_cap_flags |= IB_DEVICE_RAW_SCATTER_FCS;
Noa Osheroviche8161332017-01-18 15:40:01 +0200699 props->raw_packet_caps |= IB_RAW_PACKET_CAP_SCATTER_FCS;
700 }
Majd Dibbinycff5a0f2016-04-17 17:19:38 +0300701
Maor Gottliebda6d6ba32016-06-04 15:15:28 +0300702 if (mlx5_get_flow_namespace(dev->mdev, MLX5_FLOW_NAMESPACE_BYPASS))
703 props->device_cap_flags |= IB_DEVICE_MANAGED_FLOW_STEERING;
704
Majd Dibbiny1b5daf12015-06-04 19:30:46 +0300705 props->vendor_part_id = mdev->pdev->device;
706 props->hw_ver = mdev->pdev->revision;
Eli Cohene126ba92013-07-07 17:25:49 +0300707
708 props->max_mr_size = ~0ull;
Sagi Grimberge0238a62015-07-21 14:40:12 +0300709 props->page_size_cap = ~(min_page_size - 1);
Saeed Mahameed938fe832015-05-28 22:28:41 +0300710 props->max_qp = 1 << MLX5_CAP_GEN(mdev, log_max_qp);
711 props->max_qp_wr = 1 << MLX5_CAP_GEN(mdev, log_max_qp_sz);
712 max_rq_sg = MLX5_CAP_GEN(mdev, max_wqe_sz_rq) /
713 sizeof(struct mlx5_wqe_data_seg);
Eli Cohen288c01b2016-10-27 16:36:45 +0300714 max_sq_desc = min_t(int, MLX5_CAP_GEN(mdev, max_wqe_sz_sq), 512);
715 max_sq_sg = (max_sq_desc - sizeof(struct mlx5_wqe_ctrl_seg) -
716 sizeof(struct mlx5_wqe_raddr_seg)) /
717 sizeof(struct mlx5_wqe_data_seg);
Eli Cohene126ba92013-07-07 17:25:49 +0300718 props->max_sge = min(max_rq_sg, max_sq_sg);
Sagi Grimberg986ef952016-03-31 19:03:25 +0300719 props->max_sge_rd = MLX5_MAX_SGE_RD;
Saeed Mahameed938fe832015-05-28 22:28:41 +0300720 props->max_cq = 1 << MLX5_CAP_GEN(mdev, log_max_cq);
Leon Romanovsky9f177682016-01-14 08:11:40 +0200721 props->max_cqe = (1 << MLX5_CAP_GEN(mdev, log_max_cq_sz)) - 1;
Saeed Mahameed938fe832015-05-28 22:28:41 +0300722 props->max_mr = 1 << MLX5_CAP_GEN(mdev, log_max_mkey);
723 props->max_pd = 1 << MLX5_CAP_GEN(mdev, log_max_pd);
724 props->max_qp_rd_atom = 1 << MLX5_CAP_GEN(mdev, log_max_ra_req_qp);
725 props->max_qp_init_rd_atom = 1 << MLX5_CAP_GEN(mdev, log_max_ra_res_qp);
726 props->max_srq = 1 << MLX5_CAP_GEN(mdev, log_max_srq);
727 props->max_srq_wr = (1 << MLX5_CAP_GEN(mdev, log_max_srq_sz)) - 1;
728 props->local_ca_ack_delay = MLX5_CAP_GEN(mdev, local_ca_ack_delay);
Eli Cohene126ba92013-07-07 17:25:49 +0300729 props->max_res_rd_atom = props->max_qp_rd_atom * props->max_qp;
Eli Cohene126ba92013-07-07 17:25:49 +0300730 props->max_srq_sge = max_rq_sg - 1;
Sagi Grimberg911f4332016-03-03 13:37:51 +0200731 props->max_fast_reg_page_list_len =
732 1 << MLX5_CAP_GEN(mdev, log_max_klm_list_size);
Eran Ben Elishada7525d2015-12-14 16:34:10 +0200733 get_atomic_caps(dev, props);
Eli Cohen81bea282013-09-11 16:35:30 +0300734 props->masked_atomic_cap = IB_ATOMIC_NONE;
Saeed Mahameed938fe832015-05-28 22:28:41 +0300735 props->max_mcast_grp = 1 << MLX5_CAP_GEN(mdev, log_max_mcg);
736 props->max_mcast_qp_attach = MLX5_CAP_GEN(mdev, max_qp_mcg);
Eli Cohene126ba92013-07-07 17:25:49 +0300737 props->max_total_mcast_qp_attach = props->max_mcast_qp_attach *
738 props->max_mcast_grp;
739 props->max_map_per_fmr = INT_MAX; /* no limit in ConnectIB */
Maor Gottlieb86695a62016-10-27 16:36:38 +0300740 props->max_ah = INT_MAX;
Matan Barak7c60bcb2015-12-15 20:30:11 +0200741 props->hca_core_clock = MLX5_CAP_GEN(mdev, device_frequency_khz);
742 props->timestamp_mask = 0x7FFFFFFFFFFFFFFFULL;
Eli Cohene126ba92013-07-07 17:25:49 +0300743
Haggai Eran8cdd3122014-12-11 17:04:20 +0200744#ifdef CONFIG_INFINIBAND_ON_DEMAND_PAGING
Saeed Mahameed938fe832015-05-28 22:28:41 +0300745 if (MLX5_CAP_GEN(mdev, pg))
Haggai Eran8cdd3122014-12-11 17:04:20 +0200746 props->device_cap_flags |= IB_DEVICE_ON_DEMAND_PAGING;
747 props->odp_caps = dev->odp_caps;
748#endif
749
Leon Romanovsky051f2632015-12-20 12:16:11 +0200750 if (MLX5_CAP_GEN(mdev, cd))
751 props->device_cap_flags |= IB_DEVICE_CROSS_CHANNEL;
752
Eli Coheneff901d2016-03-11 22:58:42 +0200753 if (!mlx5_core_is_pf(mdev))
754 props->device_cap_flags |= IB_DEVICE_VIRTUAL_FUNCTION;
755
Yishai Hadas31f69a82016-08-28 11:28:45 +0300756 if (mlx5_ib_port_link_layer(ibdev, 1) ==
757 IB_LINK_LAYER_ETHERNET) {
758 props->rss_caps.max_rwq_indirection_tables =
759 1 << MLX5_CAP_GEN(dev->mdev, log_max_rqt);
760 props->rss_caps.max_rwq_indirection_table_size =
761 1 << MLX5_CAP_GEN(dev->mdev, log_max_rqt_size);
762 props->rss_caps.supported_qpts = 1 << IB_QPT_RAW_PACKET;
763 props->max_wq_type_rq =
764 1 << MLX5_CAP_GEN(dev->mdev, log_max_rq);
765 }
766
Bodong Wang7e43a2a2016-10-31 12:16:44 +0200767 if (field_avail(typeof(resp), cqe_comp_caps, uhw->outlen)) {
768 resp.cqe_comp_caps.max_num =
769 MLX5_CAP_GEN(dev->mdev, cqe_compression) ?
770 MLX5_CAP_GEN(dev->mdev, cqe_compression_max_num) : 0;
771 resp.cqe_comp_caps.supported_format =
772 MLX5_IB_CQE_RES_FORMAT_HASH |
773 MLX5_IB_CQE_RES_FORMAT_CSUM;
774 resp.response_length += sizeof(resp.cqe_comp_caps);
775 }
776
Bodong Wangd9491672016-12-01 13:43:13 +0200777 if (field_avail(typeof(resp), packet_pacing_caps, uhw->outlen)) {
778 if (MLX5_CAP_QOS(mdev, packet_pacing) &&
779 MLX5_CAP_GEN(mdev, qos)) {
780 resp.packet_pacing_caps.qp_rate_limit_max =
781 MLX5_CAP_QOS(mdev, packet_pacing_max_rate);
782 resp.packet_pacing_caps.qp_rate_limit_min =
783 MLX5_CAP_QOS(mdev, packet_pacing_min_rate);
784 resp.packet_pacing_caps.supported_qpts |=
785 1 << IB_QPT_RAW_PACKET;
786 }
787 resp.response_length += sizeof(resp.packet_pacing_caps);
788 }
789
Leon Romanovsky9f885202017-01-02 11:37:39 +0200790 if (field_avail(typeof(resp), mlx5_ib_support_multi_pkt_send_wqes,
791 uhw->outlen)) {
792 resp.mlx5_ib_support_multi_pkt_send_wqes =
793 MLX5_CAP_ETH(mdev, multi_pkt_send_wqe);
794 resp.response_length +=
795 sizeof(resp.mlx5_ib_support_multi_pkt_send_wqes);
796 }
797
798 if (field_avail(typeof(resp), reserved, uhw->outlen))
799 resp.response_length += sizeof(resp.reserved);
800
Bodong Wang402ca532016-06-17 15:02:20 +0300801 if (uhw->outlen) {
802 err = ib_copy_to_udata(uhw, &resp, resp.response_length);
803
804 if (err)
805 return err;
806 }
807
Majd Dibbiny1b5daf12015-06-04 19:30:46 +0300808 return 0;
809}
Eli Cohene126ba92013-07-07 17:25:49 +0300810
Majd Dibbiny1b5daf12015-06-04 19:30:46 +0300811enum mlx5_ib_width {
812 MLX5_IB_WIDTH_1X = 1 << 0,
813 MLX5_IB_WIDTH_2X = 1 << 1,
814 MLX5_IB_WIDTH_4X = 1 << 2,
815 MLX5_IB_WIDTH_8X = 1 << 3,
816 MLX5_IB_WIDTH_12X = 1 << 4
817};
818
819static int translate_active_width(struct ib_device *ibdev, u8 active_width,
820 u8 *ib_width)
821{
822 struct mlx5_ib_dev *dev = to_mdev(ibdev);
823 int err = 0;
824
825 if (active_width & MLX5_IB_WIDTH_1X) {
826 *ib_width = IB_WIDTH_1X;
827 } else if (active_width & MLX5_IB_WIDTH_2X) {
828 mlx5_ib_dbg(dev, "active_width %d is not supported by IB spec\n",
829 (int)active_width);
830 err = -EINVAL;
831 } else if (active_width & MLX5_IB_WIDTH_4X) {
832 *ib_width = IB_WIDTH_4X;
833 } else if (active_width & MLX5_IB_WIDTH_8X) {
834 *ib_width = IB_WIDTH_8X;
835 } else if (active_width & MLX5_IB_WIDTH_12X) {
836 *ib_width = IB_WIDTH_12X;
837 } else {
838 mlx5_ib_dbg(dev, "Invalid active_width %d\n",
839 (int)active_width);
840 err = -EINVAL;
841 }
842
843 return err;
844}
845
846static int mlx5_mtu_to_ib_mtu(int mtu)
847{
848 switch (mtu) {
849 case 256: return 1;
850 case 512: return 2;
851 case 1024: return 3;
852 case 2048: return 4;
853 case 4096: return 5;
854 default:
855 pr_warn("invalid mtu\n");
856 return -1;
857 }
858}
859
860enum ib_max_vl_num {
861 __IB_MAX_VL_0 = 1,
862 __IB_MAX_VL_0_1 = 2,
863 __IB_MAX_VL_0_3 = 3,
864 __IB_MAX_VL_0_7 = 4,
865 __IB_MAX_VL_0_14 = 5,
866};
867
868enum mlx5_vl_hw_cap {
869 MLX5_VL_HW_0 = 1,
870 MLX5_VL_HW_0_1 = 2,
871 MLX5_VL_HW_0_2 = 3,
872 MLX5_VL_HW_0_3 = 4,
873 MLX5_VL_HW_0_4 = 5,
874 MLX5_VL_HW_0_5 = 6,
875 MLX5_VL_HW_0_6 = 7,
876 MLX5_VL_HW_0_7 = 8,
877 MLX5_VL_HW_0_14 = 15
878};
879
880static int translate_max_vl_num(struct ib_device *ibdev, u8 vl_hw_cap,
881 u8 *max_vl_num)
882{
883 switch (vl_hw_cap) {
884 case MLX5_VL_HW_0:
885 *max_vl_num = __IB_MAX_VL_0;
886 break;
887 case MLX5_VL_HW_0_1:
888 *max_vl_num = __IB_MAX_VL_0_1;
889 break;
890 case MLX5_VL_HW_0_3:
891 *max_vl_num = __IB_MAX_VL_0_3;
892 break;
893 case MLX5_VL_HW_0_7:
894 *max_vl_num = __IB_MAX_VL_0_7;
895 break;
896 case MLX5_VL_HW_0_14:
897 *max_vl_num = __IB_MAX_VL_0_14;
898 break;
899
900 default:
901 return -EINVAL;
902 }
903
904 return 0;
905}
906
907static int mlx5_query_hca_port(struct ib_device *ibdev, u8 port,
908 struct ib_port_attr *props)
909{
910 struct mlx5_ib_dev *dev = to_mdev(ibdev);
911 struct mlx5_core_dev *mdev = dev->mdev;
912 struct mlx5_hca_vport_context *rep;
Saeed Mahameed046339e2016-04-22 00:33:03 +0300913 u16 max_mtu;
914 u16 oper_mtu;
Majd Dibbiny1b5daf12015-06-04 19:30:46 +0300915 int err;
916 u8 ib_link_width_oper;
917 u8 vl_hw_cap;
918
919 rep = kzalloc(sizeof(*rep), GFP_KERNEL);
920 if (!rep) {
921 err = -ENOMEM;
922 goto out;
923 }
924
Or Gerlitzc4550c62017-01-24 13:02:39 +0200925 /* props being zeroed by the caller, avoid zeroing it here */
Majd Dibbiny1b5daf12015-06-04 19:30:46 +0300926
927 err = mlx5_query_hca_vport_context(mdev, 0, port, 0, rep);
928 if (err)
929 goto out;
930
931 props->lid = rep->lid;
932 props->lmc = rep->lmc;
933 props->sm_lid = rep->sm_lid;
934 props->sm_sl = rep->sm_sl;
935 props->state = rep->vport_state;
936 props->phys_state = rep->port_physical_state;
937 props->port_cap_flags = rep->cap_mask1;
938 props->gid_tbl_len = mlx5_get_gid_table_len(MLX5_CAP_GEN(mdev, gid_table_size));
939 props->max_msg_sz = 1 << MLX5_CAP_GEN(mdev, log_max_msg);
940 props->pkey_tbl_len = mlx5_to_sw_pkey_sz(MLX5_CAP_GEN(mdev, pkey_table_size));
941 props->bad_pkey_cntr = rep->pkey_violation_counter;
942 props->qkey_viol_cntr = rep->qkey_violation_counter;
943 props->subnet_timeout = rep->subnet_timeout;
944 props->init_type_reply = rep->init_type_reply;
Eli Coheneff901d2016-03-11 22:58:42 +0200945 props->grh_required = rep->grh_required;
Majd Dibbiny1b5daf12015-06-04 19:30:46 +0300946
947 err = mlx5_query_port_link_width_oper(mdev, &ib_link_width_oper, port);
948 if (err)
949 goto out;
950
951 err = translate_active_width(ibdev, ib_link_width_oper,
952 &props->active_width);
953 if (err)
954 goto out;
Noa Osherovichd5beb7f2016-06-02 10:47:53 +0300955 err = mlx5_query_port_ib_proto_oper(mdev, &props->active_speed, port);
Majd Dibbiny1b5daf12015-06-04 19:30:46 +0300956 if (err)
957 goto out;
958
Saeed Mahameedfacc9692015-06-11 14:47:27 +0300959 mlx5_query_port_max_mtu(mdev, &max_mtu, port);
Majd Dibbiny1b5daf12015-06-04 19:30:46 +0300960
961 props->max_mtu = mlx5_mtu_to_ib_mtu(max_mtu);
962
Saeed Mahameedfacc9692015-06-11 14:47:27 +0300963 mlx5_query_port_oper_mtu(mdev, &oper_mtu, port);
Majd Dibbiny1b5daf12015-06-04 19:30:46 +0300964
965 props->active_mtu = mlx5_mtu_to_ib_mtu(oper_mtu);
966
967 err = mlx5_query_port_vl_hw_cap(mdev, &vl_hw_cap, port);
968 if (err)
969 goto out;
970
971 err = translate_max_vl_num(ibdev, vl_hw_cap,
972 &props->max_vl_num);
973out:
974 kfree(rep);
Eli Cohene126ba92013-07-07 17:25:49 +0300975 return err;
976}
977
978int mlx5_ib_query_port(struct ib_device *ibdev, u8 port,
979 struct ib_port_attr *props)
980{
Majd Dibbiny1b5daf12015-06-04 19:30:46 +0300981 switch (mlx5_get_vport_access_method(ibdev)) {
982 case MLX5_VPORT_ACCESS_METHOD_MAD:
983 return mlx5_query_mad_ifc_port(ibdev, port, props);
Eli Cohene126ba92013-07-07 17:25:49 +0300984
Majd Dibbiny1b5daf12015-06-04 19:30:46 +0300985 case MLX5_VPORT_ACCESS_METHOD_HCA:
986 return mlx5_query_hca_port(ibdev, port, props);
987
Achiad Shochat3f89a642015-12-23 18:47:21 +0200988 case MLX5_VPORT_ACCESS_METHOD_NIC:
Noa Osherovich50f22fd2017-04-20 20:53:32 +0300989 mlx5_query_port_roce(ibdev, port, props);
990 return 0;
Achiad Shochat3f89a642015-12-23 18:47:21 +0200991
Majd Dibbiny1b5daf12015-06-04 19:30:46 +0300992 default:
Eli Cohene126ba92013-07-07 17:25:49 +0300993 return -EINVAL;
994 }
Eli Cohene126ba92013-07-07 17:25:49 +0300995}
996
997static int mlx5_ib_query_gid(struct ib_device *ibdev, u8 port, int index,
998 union ib_gid *gid)
999{
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03001000 struct mlx5_ib_dev *dev = to_mdev(ibdev);
1001 struct mlx5_core_dev *mdev = dev->mdev;
Eli Cohene126ba92013-07-07 17:25:49 +03001002
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03001003 switch (mlx5_get_vport_access_method(ibdev)) {
1004 case MLX5_VPORT_ACCESS_METHOD_MAD:
1005 return mlx5_query_mad_ifc_gids(ibdev, port, index, gid);
Eli Cohene126ba92013-07-07 17:25:49 +03001006
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03001007 case MLX5_VPORT_ACCESS_METHOD_HCA:
1008 return mlx5_query_hca_vport_gid(mdev, 0, port, 0, index, gid);
Eli Cohene126ba92013-07-07 17:25:49 +03001009
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03001010 default:
1011 return -EINVAL;
1012 }
Eli Cohene126ba92013-07-07 17:25:49 +03001013
Eli Cohene126ba92013-07-07 17:25:49 +03001014}
1015
1016static int mlx5_ib_query_pkey(struct ib_device *ibdev, u8 port, u16 index,
1017 u16 *pkey)
1018{
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03001019 struct mlx5_ib_dev *dev = to_mdev(ibdev);
1020 struct mlx5_core_dev *mdev = dev->mdev;
Eli Cohene126ba92013-07-07 17:25:49 +03001021
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03001022 switch (mlx5_get_vport_access_method(ibdev)) {
1023 case MLX5_VPORT_ACCESS_METHOD_MAD:
1024 return mlx5_query_mad_ifc_pkey(ibdev, port, index, pkey);
Eli Cohene126ba92013-07-07 17:25:49 +03001025
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03001026 case MLX5_VPORT_ACCESS_METHOD_HCA:
1027 case MLX5_VPORT_ACCESS_METHOD_NIC:
1028 return mlx5_query_hca_vport_pkey(mdev, 0, port, 0, index,
1029 pkey);
1030 default:
1031 return -EINVAL;
1032 }
Eli Cohene126ba92013-07-07 17:25:49 +03001033}
1034
Eli Cohene126ba92013-07-07 17:25:49 +03001035static int mlx5_ib_modify_device(struct ib_device *ibdev, int mask,
1036 struct ib_device_modify *props)
1037{
1038 struct mlx5_ib_dev *dev = to_mdev(ibdev);
1039 struct mlx5_reg_node_desc in;
1040 struct mlx5_reg_node_desc out;
1041 int err;
1042
1043 if (mask & ~IB_DEVICE_MODIFY_NODE_DESC)
1044 return -EOPNOTSUPP;
1045
1046 if (!(mask & IB_DEVICE_MODIFY_NODE_DESC))
1047 return 0;
1048
1049 /*
1050 * If possible, pass node desc to FW, so it can generate
1051 * a 144 trap. If cmd fails, just ignore.
1052 */
Yuval Shaiabd99fde2016-08-25 10:57:07 -07001053 memcpy(&in, props->node_desc, IB_DEVICE_NODE_DESC_MAX);
Jack Morgenstein9603b612014-07-28 23:30:22 +03001054 err = mlx5_core_access_reg(dev->mdev, &in, sizeof(in), &out,
Eli Cohene126ba92013-07-07 17:25:49 +03001055 sizeof(out), MLX5_REG_NODE_DESC, 0, 1);
1056 if (err)
1057 return err;
1058
Yuval Shaiabd99fde2016-08-25 10:57:07 -07001059 memcpy(ibdev->node_desc, props->node_desc, IB_DEVICE_NODE_DESC_MAX);
Eli Cohene126ba92013-07-07 17:25:49 +03001060
1061 return err;
1062}
1063
Eli Cohencdbe33d2017-02-14 07:25:38 +02001064static int set_port_caps_atomic(struct mlx5_ib_dev *dev, u8 port_num, u32 mask,
1065 u32 value)
1066{
1067 struct mlx5_hca_vport_context ctx = {};
1068 int err;
1069
1070 err = mlx5_query_hca_vport_context(dev->mdev, 0,
1071 port_num, 0, &ctx);
1072 if (err)
1073 return err;
1074
1075 if (~ctx.cap_mask1_perm & mask) {
1076 mlx5_ib_warn(dev, "trying to change bitmask 0x%X but change supported 0x%X\n",
1077 mask, ctx.cap_mask1_perm);
1078 return -EINVAL;
1079 }
1080
1081 ctx.cap_mask1 = value;
1082 ctx.cap_mask1_perm = mask;
1083 err = mlx5_core_modify_hca_vport_context(dev->mdev, 0,
1084 port_num, 0, &ctx);
1085
1086 return err;
1087}
1088
Eli Cohene126ba92013-07-07 17:25:49 +03001089static int mlx5_ib_modify_port(struct ib_device *ibdev, u8 port, int mask,
1090 struct ib_port_modify *props)
1091{
1092 struct mlx5_ib_dev *dev = to_mdev(ibdev);
1093 struct ib_port_attr attr;
1094 u32 tmp;
1095 int err;
Eli Cohencdbe33d2017-02-14 07:25:38 +02001096 u32 change_mask;
1097 u32 value;
1098 bool is_ib = (mlx5_ib_port_link_layer(ibdev, port) ==
1099 IB_LINK_LAYER_INFINIBAND);
1100
1101 if (MLX5_CAP_GEN(dev->mdev, ib_virt) && is_ib) {
1102 change_mask = props->clr_port_cap_mask | props->set_port_cap_mask;
1103 value = ~props->clr_port_cap_mask | props->set_port_cap_mask;
1104 return set_port_caps_atomic(dev, port, change_mask, value);
1105 }
Eli Cohene126ba92013-07-07 17:25:49 +03001106
1107 mutex_lock(&dev->cap_mask_mutex);
1108
Or Gerlitzc4550c62017-01-24 13:02:39 +02001109 err = ib_query_port(ibdev, port, &attr);
Eli Cohene126ba92013-07-07 17:25:49 +03001110 if (err)
1111 goto out;
1112
1113 tmp = (attr.port_cap_flags | props->set_port_cap_mask) &
1114 ~props->clr_port_cap_mask;
1115
Jack Morgenstein9603b612014-07-28 23:30:22 +03001116 err = mlx5_set_port_caps(dev->mdev, port, tmp);
Eli Cohene126ba92013-07-07 17:25:49 +03001117
1118out:
1119 mutex_unlock(&dev->cap_mask_mutex);
1120 return err;
1121}
1122
Eli Cohen30aa60b2017-01-03 23:55:27 +02001123static void print_lib_caps(struct mlx5_ib_dev *dev, u64 caps)
1124{
1125 mlx5_ib_dbg(dev, "MLX5_LIB_CAP_4K_UAR = %s\n",
1126 caps & MLX5_LIB_CAP_4K_UAR ? "y" : "n");
1127}
1128
Eli Cohenb037c292017-01-03 23:55:26 +02001129static int calc_total_bfregs(struct mlx5_ib_dev *dev, bool lib_uar_4k,
1130 struct mlx5_ib_alloc_ucontext_req_v2 *req,
1131 u32 *num_sys_pages)
1132{
1133 int uars_per_sys_page;
1134 int bfregs_per_sys_page;
1135 int ref_bfregs = req->total_num_bfregs;
1136
1137 if (req->total_num_bfregs == 0)
1138 return -EINVAL;
1139
1140 BUILD_BUG_ON(MLX5_MAX_BFREGS % MLX5_NON_FP_BFREGS_IN_PAGE);
1141 BUILD_BUG_ON(MLX5_MAX_BFREGS < MLX5_NON_FP_BFREGS_IN_PAGE);
1142
1143 if (req->total_num_bfregs > MLX5_MAX_BFREGS)
1144 return -ENOMEM;
1145
1146 uars_per_sys_page = get_uars_per_sys_page(dev, lib_uar_4k);
1147 bfregs_per_sys_page = uars_per_sys_page * MLX5_NON_FP_BFREGS_PER_UAR;
1148 req->total_num_bfregs = ALIGN(req->total_num_bfregs, bfregs_per_sys_page);
1149 *num_sys_pages = req->total_num_bfregs / bfregs_per_sys_page;
1150
1151 if (req->num_low_latency_bfregs > req->total_num_bfregs - 1)
1152 return -EINVAL;
1153
1154 mlx5_ib_dbg(dev, "uar_4k: fw support %s, lib support %s, user requested %d bfregs, alloated %d, using %d sys pages\n",
1155 MLX5_CAP_GEN(dev->mdev, uar_4k) ? "yes" : "no",
1156 lib_uar_4k ? "yes" : "no", ref_bfregs,
1157 req->total_num_bfregs, *num_sys_pages);
1158
1159 return 0;
1160}
1161
1162static int allocate_uars(struct mlx5_ib_dev *dev, struct mlx5_ib_ucontext *context)
1163{
1164 struct mlx5_bfreg_info *bfregi;
1165 int err;
1166 int i;
1167
1168 bfregi = &context->bfregi;
1169 for (i = 0; i < bfregi->num_sys_pages; i++) {
1170 err = mlx5_cmd_alloc_uar(dev->mdev, &bfregi->sys_pages[i]);
1171 if (err)
1172 goto error;
1173
1174 mlx5_ib_dbg(dev, "allocated uar %d\n", bfregi->sys_pages[i]);
1175 }
1176 return 0;
1177
1178error:
1179 for (--i; i >= 0; i--)
1180 if (mlx5_cmd_free_uar(dev->mdev, bfregi->sys_pages[i]))
1181 mlx5_ib_warn(dev, "failed to free uar %d\n", i);
1182
1183 return err;
1184}
1185
1186static int deallocate_uars(struct mlx5_ib_dev *dev, struct mlx5_ib_ucontext *context)
1187{
1188 struct mlx5_bfreg_info *bfregi;
1189 int err;
1190 int i;
1191
1192 bfregi = &context->bfregi;
1193 for (i = 0; i < bfregi->num_sys_pages; i++) {
1194 err = mlx5_cmd_free_uar(dev->mdev, bfregi->sys_pages[i]);
1195 if (err) {
1196 mlx5_ib_warn(dev, "failed to free uar %d\n", i);
1197 return err;
1198 }
1199 }
1200 return 0;
1201}
1202
Eli Cohene126ba92013-07-07 17:25:49 +03001203static struct ib_ucontext *mlx5_ib_alloc_ucontext(struct ib_device *ibdev,
1204 struct ib_udata *udata)
1205{
1206 struct mlx5_ib_dev *dev = to_mdev(ibdev);
Matan Barakb368d7c2015-12-15 20:30:12 +02001207 struct mlx5_ib_alloc_ucontext_req_v2 req = {};
1208 struct mlx5_ib_alloc_ucontext_resp resp = {};
Eli Cohene126ba92013-07-07 17:25:49 +03001209 struct mlx5_ib_ucontext *context;
Eli Cohen2f5ff262017-01-03 23:55:21 +02001210 struct mlx5_bfreg_info *bfregi;
Eli Cohen78c0f982014-01-30 13:49:48 +02001211 int ver;
Eli Cohene126ba92013-07-07 17:25:49 +03001212 int err;
Jack Morgensteinf241e742014-07-28 23:30:23 +03001213 size_t reqlen;
Majd Dibbinya168a41c2016-01-28 17:51:47 +02001214 size_t min_req_v2 = offsetof(struct mlx5_ib_alloc_ucontext_req_v2,
1215 max_cqe_version);
Eli Cohenb037c292017-01-03 23:55:26 +02001216 bool lib_uar_4k;
Eli Cohene126ba92013-07-07 17:25:49 +03001217
1218 if (!dev->ib_active)
1219 return ERR_PTR(-EAGAIN);
1220
Haggai Abramovskydfbee852016-01-14 19:12:56 +02001221 if (udata->inlen < sizeof(struct ib_uverbs_cmd_hdr))
1222 return ERR_PTR(-EINVAL);
1223
Eli Cohen78c0f982014-01-30 13:49:48 +02001224 reqlen = udata->inlen - sizeof(struct ib_uverbs_cmd_hdr);
1225 if (reqlen == sizeof(struct mlx5_ib_alloc_ucontext_req))
1226 ver = 0;
Majd Dibbinya168a41c2016-01-28 17:51:47 +02001227 else if (reqlen >= min_req_v2)
Eli Cohen78c0f982014-01-30 13:49:48 +02001228 ver = 2;
1229 else
1230 return ERR_PTR(-EINVAL);
1231
Matan Barakb368d7c2015-12-15 20:30:12 +02001232 err = ib_copy_from_udata(&req, udata, min(reqlen, sizeof(req)));
Eli Cohene126ba92013-07-07 17:25:49 +03001233 if (err)
1234 return ERR_PTR(err);
1235
Matan Barakb368d7c2015-12-15 20:30:12 +02001236 if (req.flags)
Eli Cohen78c0f982014-01-30 13:49:48 +02001237 return ERR_PTR(-EINVAL);
1238
Haggai Abramovskyf72300c2016-01-14 19:12:58 +02001239 if (req.comp_mask || req.reserved0 || req.reserved1 || req.reserved2)
Matan Barakb368d7c2015-12-15 20:30:12 +02001240 return ERR_PTR(-EOPNOTSUPP);
1241
Eli Cohen2f5ff262017-01-03 23:55:21 +02001242 req.total_num_bfregs = ALIGN(req.total_num_bfregs,
1243 MLX5_NON_FP_BFREGS_PER_UAR);
1244 if (req.num_low_latency_bfregs > req.total_num_bfregs - 1)
Eli Cohene126ba92013-07-07 17:25:49 +03001245 return ERR_PTR(-EINVAL);
1246
Saeed Mahameed938fe832015-05-28 22:28:41 +03001247 resp.qp_tab_size = 1 << MLX5_CAP_GEN(dev->mdev, log_max_qp);
Noa Osherovich2cc6ad52016-06-04 15:15:33 +03001248 if (mlx5_core_is_pf(dev->mdev) && MLX5_CAP_GEN(dev->mdev, bf))
1249 resp.bf_reg_size = 1 << MLX5_CAP_GEN(dev->mdev, log_bf_reg_size);
Daniel Jurgensb47bd6e2016-10-25 18:36:24 +03001250 resp.cache_line_size = cache_line_size();
Saeed Mahameed938fe832015-05-28 22:28:41 +03001251 resp.max_sq_desc_sz = MLX5_CAP_GEN(dev->mdev, max_wqe_sz_sq);
1252 resp.max_rq_desc_sz = MLX5_CAP_GEN(dev->mdev, max_wqe_sz_rq);
1253 resp.max_send_wqebb = 1 << MLX5_CAP_GEN(dev->mdev, log_max_qp_sz);
1254 resp.max_recv_wr = 1 << MLX5_CAP_GEN(dev->mdev, log_max_qp_sz);
1255 resp.max_srq_recv_wr = 1 << MLX5_CAP_GEN(dev->mdev, log_max_srq_sz);
Haggai Abramovskyf72300c2016-01-14 19:12:58 +02001256 resp.cqe_version = min_t(__u8,
1257 (__u8)MLX5_CAP_GEN(dev->mdev, cqe_version),
1258 req.max_cqe_version);
Eli Cohen30aa60b2017-01-03 23:55:27 +02001259 resp.log_uar_size = MLX5_CAP_GEN(dev->mdev, uar_4k) ?
1260 MLX5_ADAPTER_PAGE_SHIFT : PAGE_SHIFT;
1261 resp.num_uars_per_page = MLX5_CAP_GEN(dev->mdev, uar_4k) ?
1262 MLX5_CAP_GEN(dev->mdev, num_of_uars_per_page) : 1;
Matan Barakb368d7c2015-12-15 20:30:12 +02001263 resp.response_length = min(offsetof(typeof(resp), response_length) +
1264 sizeof(resp.response_length), udata->outlen);
Eli Cohene126ba92013-07-07 17:25:49 +03001265
1266 context = kzalloc(sizeof(*context), GFP_KERNEL);
1267 if (!context)
1268 return ERR_PTR(-ENOMEM);
1269
Eli Cohen30aa60b2017-01-03 23:55:27 +02001270 lib_uar_4k = req.lib_caps & MLX5_LIB_CAP_4K_UAR;
Eli Cohen2f5ff262017-01-03 23:55:21 +02001271 bfregi = &context->bfregi;
Eli Cohenb037c292017-01-03 23:55:26 +02001272
1273 /* updates req->total_num_bfregs */
1274 err = calc_total_bfregs(dev, lib_uar_4k, &req, &bfregi->num_sys_pages);
1275 if (err)
1276 goto out_ctx;
1277
Eli Cohen2f5ff262017-01-03 23:55:21 +02001278 mutex_init(&bfregi->lock);
Eli Cohenb037c292017-01-03 23:55:26 +02001279 bfregi->lib_uar_4k = lib_uar_4k;
1280 bfregi->count = kcalloc(req.total_num_bfregs, sizeof(*bfregi->count),
1281 GFP_KERNEL);
1282 if (!bfregi->count) {
Eli Cohene126ba92013-07-07 17:25:49 +03001283 err = -ENOMEM;
1284 goto out_ctx;
1285 }
1286
Eli Cohenb037c292017-01-03 23:55:26 +02001287 bfregi->sys_pages = kcalloc(bfregi->num_sys_pages,
1288 sizeof(*bfregi->sys_pages),
1289 GFP_KERNEL);
1290 if (!bfregi->sys_pages) {
Eli Cohene126ba92013-07-07 17:25:49 +03001291 err = -ENOMEM;
Eli Cohenb037c292017-01-03 23:55:26 +02001292 goto out_count;
Eli Cohene126ba92013-07-07 17:25:49 +03001293 }
1294
Eli Cohenb037c292017-01-03 23:55:26 +02001295 err = allocate_uars(dev, context);
1296 if (err)
1297 goto out_sys_pages;
Eli Cohene126ba92013-07-07 17:25:49 +03001298
Haggai Eranb4cfe442014-12-11 17:04:26 +02001299#ifdef CONFIG_INFINIBAND_ON_DEMAND_PAGING
1300 context->ibucontext.invalidate_range = &mlx5_ib_invalidate_range;
1301#endif
1302
Artemy Kovalyov7d0cc6e2017-01-02 11:37:44 +02001303 context->upd_xlt_page = __get_free_page(GFP_KERNEL);
1304 if (!context->upd_xlt_page) {
1305 err = -ENOMEM;
1306 goto out_uars;
1307 }
1308 mutex_init(&context->upd_xlt_page_mutex);
1309
majd@mellanox.com146d2f12016-01-14 19:13:02 +02001310 if (MLX5_CAP_GEN(dev->mdev, log_max_transport_domain)) {
1311 err = mlx5_core_alloc_transport_domain(dev->mdev,
1312 &context->tdn);
1313 if (err)
Artemy Kovalyov7d0cc6e2017-01-02 11:37:44 +02001314 goto out_page;
majd@mellanox.com146d2f12016-01-14 19:13:02 +02001315 }
1316
Maor Gottlieb7c2344c2016-06-17 14:56:44 +03001317 INIT_LIST_HEAD(&context->vma_private_list);
Eli Cohene126ba92013-07-07 17:25:49 +03001318 INIT_LIST_HEAD(&context->db_page_list);
1319 mutex_init(&context->db_page_mutex);
1320
Eli Cohen2f5ff262017-01-03 23:55:21 +02001321 resp.tot_bfregs = req.total_num_bfregs;
Saeed Mahameed938fe832015-05-28 22:28:41 +03001322 resp.num_ports = MLX5_CAP_GEN(dev->mdev, num_ports);
Matan Barakb368d7c2015-12-15 20:30:12 +02001323
Haggai Abramovskyf72300c2016-01-14 19:12:58 +02001324 if (field_avail(typeof(resp), cqe_version, udata->outlen))
1325 resp.response_length += sizeof(resp.cqe_version);
Matan Barakb368d7c2015-12-15 20:30:12 +02001326
Bodong Wang402ca532016-06-17 15:02:20 +03001327 if (field_avail(typeof(resp), cmds_supp_uhw, udata->outlen)) {
Moni Shoua6ad279c52016-11-23 08:23:23 +02001328 resp.cmds_supp_uhw |= MLX5_USER_CMDS_SUPP_UHW_QUERY_DEVICE |
1329 MLX5_USER_CMDS_SUPP_UHW_CREATE_AH;
Bodong Wang402ca532016-06-17 15:02:20 +03001330 resp.response_length += sizeof(resp.cmds_supp_uhw);
1331 }
1332
Or Gerlitz78984892016-11-30 20:33:33 +02001333 if (field_avail(typeof(resp), eth_min_inline, udata->outlen)) {
1334 if (mlx5_ib_port_link_layer(ibdev, 1) == IB_LINK_LAYER_ETHERNET) {
1335 mlx5_query_min_inline(dev->mdev, &resp.eth_min_inline);
1336 resp.eth_min_inline++;
1337 }
1338 resp.response_length += sizeof(resp.eth_min_inline);
1339 }
1340
Noa Osherovichbc5c6ee2016-06-04 15:15:31 +03001341 /*
1342 * We don't want to expose information from the PCI bar that is located
1343 * after 4096 bytes, so if the arch only supports larger pages, let's
1344 * pretend we don't support reading the HCA's core clock. This is also
1345 * forced by mmap function.
1346 */
Eli Cohende8d6e02017-01-03 23:55:19 +02001347 if (field_avail(typeof(resp), hca_core_clock_offset, udata->outlen)) {
1348 if (PAGE_SIZE <= 4096) {
1349 resp.comp_mask |=
1350 MLX5_IB_ALLOC_UCONTEXT_RESP_MASK_CORE_CLOCK_OFFSET;
1351 resp.hca_core_clock_offset =
1352 offsetof(struct mlx5_init_seg, internal_timer_h) % PAGE_SIZE;
1353 }
Haggai Abramovskyf72300c2016-01-14 19:12:58 +02001354 resp.response_length += sizeof(resp.hca_core_clock_offset) +
Bodong Wang402ca532016-06-17 15:02:20 +03001355 sizeof(resp.reserved2);
Matan Barakb368d7c2015-12-15 20:30:12 +02001356 }
1357
Eli Cohen30aa60b2017-01-03 23:55:27 +02001358 if (field_avail(typeof(resp), log_uar_size, udata->outlen))
1359 resp.response_length += sizeof(resp.log_uar_size);
1360
1361 if (field_avail(typeof(resp), num_uars_per_page, udata->outlen))
1362 resp.response_length += sizeof(resp.num_uars_per_page);
1363
Matan Barakb368d7c2015-12-15 20:30:12 +02001364 err = ib_copy_to_udata(udata, &resp, resp.response_length);
Eli Cohene126ba92013-07-07 17:25:49 +03001365 if (err)
majd@mellanox.com146d2f12016-01-14 19:13:02 +02001366 goto out_td;
Eli Cohene126ba92013-07-07 17:25:49 +03001367
Eli Cohen2f5ff262017-01-03 23:55:21 +02001368 bfregi->ver = ver;
1369 bfregi->num_low_latency_bfregs = req.num_low_latency_bfregs;
Haggai Abramovskyf72300c2016-01-14 19:12:58 +02001370 context->cqe_version = resp.cqe_version;
Eli Cohen30aa60b2017-01-03 23:55:27 +02001371 context->lib_caps = req.lib_caps;
1372 print_lib_caps(dev, context->lib_caps);
Haggai Abramovskyf72300c2016-01-14 19:12:58 +02001373
Eli Cohene126ba92013-07-07 17:25:49 +03001374 return &context->ibucontext;
1375
majd@mellanox.com146d2f12016-01-14 19:13:02 +02001376out_td:
1377 if (MLX5_CAP_GEN(dev->mdev, log_max_transport_domain))
1378 mlx5_core_dealloc_transport_domain(dev->mdev, context->tdn);
1379
Artemy Kovalyov7d0cc6e2017-01-02 11:37:44 +02001380out_page:
1381 free_page(context->upd_xlt_page);
1382
Eli Cohene126ba92013-07-07 17:25:49 +03001383out_uars:
Eli Cohenb037c292017-01-03 23:55:26 +02001384 deallocate_uars(dev, context);
1385
1386out_sys_pages:
1387 kfree(bfregi->sys_pages);
1388
Eli Cohene126ba92013-07-07 17:25:49 +03001389out_count:
Eli Cohen2f5ff262017-01-03 23:55:21 +02001390 kfree(bfregi->count);
Eli Cohene126ba92013-07-07 17:25:49 +03001391
Eli Cohene126ba92013-07-07 17:25:49 +03001392out_ctx:
1393 kfree(context);
Eli Cohenb037c292017-01-03 23:55:26 +02001394
Eli Cohene126ba92013-07-07 17:25:49 +03001395 return ERR_PTR(err);
1396}
1397
1398static int mlx5_ib_dealloc_ucontext(struct ib_ucontext *ibcontext)
1399{
1400 struct mlx5_ib_ucontext *context = to_mucontext(ibcontext);
1401 struct mlx5_ib_dev *dev = to_mdev(ibcontext->device);
Eli Cohenb037c292017-01-03 23:55:26 +02001402 struct mlx5_bfreg_info *bfregi;
Eli Cohene126ba92013-07-07 17:25:49 +03001403
Eli Cohenb037c292017-01-03 23:55:26 +02001404 bfregi = &context->bfregi;
majd@mellanox.com146d2f12016-01-14 19:13:02 +02001405 if (MLX5_CAP_GEN(dev->mdev, log_max_transport_domain))
1406 mlx5_core_dealloc_transport_domain(dev->mdev, context->tdn);
1407
Artemy Kovalyov7d0cc6e2017-01-02 11:37:44 +02001408 free_page(context->upd_xlt_page);
Eli Cohenb037c292017-01-03 23:55:26 +02001409 deallocate_uars(dev, context);
1410 kfree(bfregi->sys_pages);
Eli Cohen2f5ff262017-01-03 23:55:21 +02001411 kfree(bfregi->count);
Eli Cohene126ba92013-07-07 17:25:49 +03001412 kfree(context);
1413
1414 return 0;
1415}
1416
Eli Cohenb037c292017-01-03 23:55:26 +02001417static phys_addr_t uar_index2pfn(struct mlx5_ib_dev *dev,
1418 struct mlx5_bfreg_info *bfregi,
1419 int idx)
Eli Cohene126ba92013-07-07 17:25:49 +03001420{
Eli Cohenb037c292017-01-03 23:55:26 +02001421 int fw_uars_per_page;
1422
1423 fw_uars_per_page = MLX5_CAP_GEN(dev->mdev, uar_4k) ? MLX5_UARS_IN_PAGE : 1;
1424
1425 return (pci_resource_start(dev->mdev->pdev, 0) >> PAGE_SHIFT) +
1426 bfregi->sys_pages[idx] / fw_uars_per_page;
Eli Cohene126ba92013-07-07 17:25:49 +03001427}
1428
1429static int get_command(unsigned long offset)
1430{
1431 return (offset >> MLX5_IB_MMAP_CMD_SHIFT) & MLX5_IB_MMAP_CMD_MASK;
1432}
1433
1434static int get_arg(unsigned long offset)
1435{
1436 return offset & ((1 << MLX5_IB_MMAP_CMD_SHIFT) - 1);
1437}
1438
1439static int get_index(unsigned long offset)
1440{
1441 return get_arg(offset);
1442}
1443
Maor Gottlieb7c2344c2016-06-17 14:56:44 +03001444static void mlx5_ib_vma_open(struct vm_area_struct *area)
1445{
1446 /* vma_open is called when a new VMA is created on top of our VMA. This
1447 * is done through either mremap flow or split_vma (usually due to
1448 * mlock, madvise, munmap, etc.) We do not support a clone of the VMA,
1449 * as this VMA is strongly hardware related. Therefore we set the
1450 * vm_ops of the newly created/cloned VMA to NULL, to prevent it from
1451 * calling us again and trying to do incorrect actions. We assume that
1452 * the original VMA size is exactly a single page, and therefore all
1453 * "splitting" operation will not happen to it.
1454 */
1455 area->vm_ops = NULL;
1456}
1457
1458static void mlx5_ib_vma_close(struct vm_area_struct *area)
1459{
1460 struct mlx5_ib_vma_private_data *mlx5_ib_vma_priv_data;
1461
1462 /* It's guaranteed that all VMAs opened on a FD are closed before the
1463 * file itself is closed, therefore no sync is needed with the regular
1464 * closing flow. (e.g. mlx5 ib_dealloc_ucontext)
1465 * However need a sync with accessing the vma as part of
1466 * mlx5_ib_disassociate_ucontext.
1467 * The close operation is usually called under mm->mmap_sem except when
1468 * process is exiting.
1469 * The exiting case is handled explicitly as part of
1470 * mlx5_ib_disassociate_ucontext.
1471 */
1472 mlx5_ib_vma_priv_data = (struct mlx5_ib_vma_private_data *)area->vm_private_data;
1473
1474 /* setting the vma context pointer to null in the mlx5_ib driver's
1475 * private data, to protect a race condition in
1476 * mlx5_ib_disassociate_ucontext().
1477 */
1478 mlx5_ib_vma_priv_data->vma = NULL;
1479 list_del(&mlx5_ib_vma_priv_data->list);
1480 kfree(mlx5_ib_vma_priv_data);
1481}
1482
1483static const struct vm_operations_struct mlx5_ib_vm_ops = {
1484 .open = mlx5_ib_vma_open,
1485 .close = mlx5_ib_vma_close
1486};
1487
1488static int mlx5_ib_set_vma_data(struct vm_area_struct *vma,
1489 struct mlx5_ib_ucontext *ctx)
1490{
1491 struct mlx5_ib_vma_private_data *vma_prv;
1492 struct list_head *vma_head = &ctx->vma_private_list;
1493
1494 vma_prv = kzalloc(sizeof(*vma_prv), GFP_KERNEL);
1495 if (!vma_prv)
1496 return -ENOMEM;
1497
1498 vma_prv->vma = vma;
1499 vma->vm_private_data = vma_prv;
1500 vma->vm_ops = &mlx5_ib_vm_ops;
1501
1502 list_add(&vma_prv->list, vma_head);
1503
1504 return 0;
1505}
1506
1507static void mlx5_ib_disassociate_ucontext(struct ib_ucontext *ibcontext)
1508{
1509 int ret;
1510 struct vm_area_struct *vma;
1511 struct mlx5_ib_vma_private_data *vma_private, *n;
1512 struct mlx5_ib_ucontext *context = to_mucontext(ibcontext);
1513 struct task_struct *owning_process = NULL;
1514 struct mm_struct *owning_mm = NULL;
1515
1516 owning_process = get_pid_task(ibcontext->tgid, PIDTYPE_PID);
1517 if (!owning_process)
1518 return;
1519
1520 owning_mm = get_task_mm(owning_process);
1521 if (!owning_mm) {
1522 pr_info("no mm, disassociate ucontext is pending task termination\n");
1523 while (1) {
1524 put_task_struct(owning_process);
1525 usleep_range(1000, 2000);
1526 owning_process = get_pid_task(ibcontext->tgid,
1527 PIDTYPE_PID);
1528 if (!owning_process ||
1529 owning_process->state == TASK_DEAD) {
1530 pr_info("disassociate ucontext done, task was terminated\n");
1531 /* in case task was dead need to release the
1532 * task struct.
1533 */
1534 if (owning_process)
1535 put_task_struct(owning_process);
1536 return;
1537 }
1538 }
1539 }
1540
1541 /* need to protect from a race on closing the vma as part of
1542 * mlx5_ib_vma_close.
1543 */
Maor Gottliebecc7d832017-03-29 06:03:02 +03001544 down_write(&owning_mm->mmap_sem);
Maor Gottlieb7c2344c2016-06-17 14:56:44 +03001545 list_for_each_entry_safe(vma_private, n, &context->vma_private_list,
1546 list) {
1547 vma = vma_private->vma;
1548 ret = zap_vma_ptes(vma, vma->vm_start,
1549 PAGE_SIZE);
1550 WARN_ONCE(ret, "%s: zap_vma_ptes failed", __func__);
1551 /* context going to be destroyed, should
1552 * not access ops any more.
1553 */
Maor Gottlieb13776612017-03-29 06:03:03 +03001554 vma->vm_flags &= ~(VM_SHARED | VM_MAYSHARE);
Maor Gottlieb7c2344c2016-06-17 14:56:44 +03001555 vma->vm_ops = NULL;
1556 list_del(&vma_private->list);
1557 kfree(vma_private);
1558 }
Maor Gottliebecc7d832017-03-29 06:03:02 +03001559 up_write(&owning_mm->mmap_sem);
Maor Gottlieb7c2344c2016-06-17 14:56:44 +03001560 mmput(owning_mm);
1561 put_task_struct(owning_process);
1562}
1563
Guy Levi37aa5c32016-04-27 16:49:50 +03001564static inline char *mmap_cmd2str(enum mlx5_ib_mmap_cmd cmd)
1565{
1566 switch (cmd) {
1567 case MLX5_IB_MMAP_WC_PAGE:
1568 return "WC";
1569 case MLX5_IB_MMAP_REGULAR_PAGE:
1570 return "best effort WC";
1571 case MLX5_IB_MMAP_NC_PAGE:
1572 return "NC";
1573 default:
1574 return NULL;
1575 }
1576}
1577
1578static int uar_mmap(struct mlx5_ib_dev *dev, enum mlx5_ib_mmap_cmd cmd,
Maor Gottlieb7c2344c2016-06-17 14:56:44 +03001579 struct vm_area_struct *vma,
1580 struct mlx5_ib_ucontext *context)
Guy Levi37aa5c32016-04-27 16:49:50 +03001581{
Eli Cohen2f5ff262017-01-03 23:55:21 +02001582 struct mlx5_bfreg_info *bfregi = &context->bfregi;
Guy Levi37aa5c32016-04-27 16:49:50 +03001583 int err;
1584 unsigned long idx;
1585 phys_addr_t pfn, pa;
1586 pgprot_t prot;
Eli Cohenb037c292017-01-03 23:55:26 +02001587 int uars_per_page;
1588
1589 if (vma->vm_end - vma->vm_start != PAGE_SIZE)
1590 return -EINVAL;
1591
1592 uars_per_page = get_uars_per_sys_page(dev, bfregi->lib_uar_4k);
1593 idx = get_index(vma->vm_pgoff);
1594 if (idx % uars_per_page ||
1595 idx * uars_per_page >= bfregi->num_sys_pages) {
1596 mlx5_ib_warn(dev, "invalid uar index %lu\n", idx);
1597 return -EINVAL;
1598 }
Guy Levi37aa5c32016-04-27 16:49:50 +03001599
1600 switch (cmd) {
1601 case MLX5_IB_MMAP_WC_PAGE:
1602/* Some architectures don't support WC memory */
1603#if defined(CONFIG_X86)
1604 if (!pat_enabled())
1605 return -EPERM;
1606#elif !(defined(CONFIG_PPC) || (defined(CONFIG_ARM) && defined(CONFIG_MMU)))
1607 return -EPERM;
1608#endif
1609 /* fall through */
1610 case MLX5_IB_MMAP_REGULAR_PAGE:
1611 /* For MLX5_IB_MMAP_REGULAR_PAGE do the best effort to get WC */
1612 prot = pgprot_writecombine(vma->vm_page_prot);
1613 break;
1614 case MLX5_IB_MMAP_NC_PAGE:
1615 prot = pgprot_noncached(vma->vm_page_prot);
1616 break;
1617 default:
1618 return -EINVAL;
1619 }
1620
Eli Cohenb037c292017-01-03 23:55:26 +02001621 pfn = uar_index2pfn(dev, bfregi, idx);
Guy Levi37aa5c32016-04-27 16:49:50 +03001622 mlx5_ib_dbg(dev, "uar idx 0x%lx, pfn %pa\n", idx, &pfn);
1623
1624 vma->vm_page_prot = prot;
1625 err = io_remap_pfn_range(vma, vma->vm_start, pfn,
1626 PAGE_SIZE, vma->vm_page_prot);
1627 if (err) {
1628 mlx5_ib_err(dev, "io_remap_pfn_range failed with error=%d, vm_start=0x%lx, pfn=%pa, mmap_cmd=%s\n",
1629 err, vma->vm_start, &pfn, mmap_cmd2str(cmd));
1630 return -EAGAIN;
1631 }
1632
1633 pa = pfn << PAGE_SHIFT;
1634 mlx5_ib_dbg(dev, "mapped %s at 0x%lx, PA %pa\n", mmap_cmd2str(cmd),
1635 vma->vm_start, &pa);
1636
Maor Gottlieb7c2344c2016-06-17 14:56:44 +03001637 return mlx5_ib_set_vma_data(vma, context);
Guy Levi37aa5c32016-04-27 16:49:50 +03001638}
1639
Eli Cohene126ba92013-07-07 17:25:49 +03001640static int mlx5_ib_mmap(struct ib_ucontext *ibcontext, struct vm_area_struct *vma)
1641{
1642 struct mlx5_ib_ucontext *context = to_mucontext(ibcontext);
1643 struct mlx5_ib_dev *dev = to_mdev(ibcontext->device);
Eli Cohene126ba92013-07-07 17:25:49 +03001644 unsigned long command;
Eli Cohene126ba92013-07-07 17:25:49 +03001645 phys_addr_t pfn;
1646
1647 command = get_command(vma->vm_pgoff);
1648 switch (command) {
Guy Levi37aa5c32016-04-27 16:49:50 +03001649 case MLX5_IB_MMAP_WC_PAGE:
1650 case MLX5_IB_MMAP_NC_PAGE:
Eli Cohene126ba92013-07-07 17:25:49 +03001651 case MLX5_IB_MMAP_REGULAR_PAGE:
Maor Gottlieb7c2344c2016-06-17 14:56:44 +03001652 return uar_mmap(dev, command, vma, context);
Eli Cohene126ba92013-07-07 17:25:49 +03001653
1654 case MLX5_IB_MMAP_GET_CONTIGUOUS_PAGES:
1655 return -ENOSYS;
1656
Matan Barakd69e3bc2015-12-15 20:30:13 +02001657 case MLX5_IB_MMAP_CORE_CLOCK:
Matan Barakd69e3bc2015-12-15 20:30:13 +02001658 if (vma->vm_end - vma->vm_start != PAGE_SIZE)
1659 return -EINVAL;
1660
Matan Barak6cbac1e2016-04-14 16:52:10 +03001661 if (vma->vm_flags & VM_WRITE)
Matan Barakd69e3bc2015-12-15 20:30:13 +02001662 return -EPERM;
1663
1664 /* Don't expose to user-space information it shouldn't have */
1665 if (PAGE_SIZE > 4096)
1666 return -EOPNOTSUPP;
1667
1668 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
1669 pfn = (dev->mdev->iseg_base +
1670 offsetof(struct mlx5_init_seg, internal_timer_h)) >>
1671 PAGE_SHIFT;
1672 if (io_remap_pfn_range(vma, vma->vm_start, pfn,
1673 PAGE_SIZE, vma->vm_page_prot))
1674 return -EAGAIN;
1675
1676 mlx5_ib_dbg(dev, "mapped internal timer at 0x%lx, PA 0x%llx\n",
1677 vma->vm_start,
1678 (unsigned long long)pfn << PAGE_SHIFT);
1679 break;
Matan Barakd69e3bc2015-12-15 20:30:13 +02001680
Eli Cohene126ba92013-07-07 17:25:49 +03001681 default:
1682 return -EINVAL;
1683 }
1684
1685 return 0;
1686}
1687
Eli Cohene126ba92013-07-07 17:25:49 +03001688static struct ib_pd *mlx5_ib_alloc_pd(struct ib_device *ibdev,
1689 struct ib_ucontext *context,
1690 struct ib_udata *udata)
1691{
1692 struct mlx5_ib_alloc_pd_resp resp;
1693 struct mlx5_ib_pd *pd;
1694 int err;
1695
1696 pd = kmalloc(sizeof(*pd), GFP_KERNEL);
1697 if (!pd)
1698 return ERR_PTR(-ENOMEM);
1699
Jack Morgenstein9603b612014-07-28 23:30:22 +03001700 err = mlx5_core_alloc_pd(to_mdev(ibdev)->mdev, &pd->pdn);
Eli Cohene126ba92013-07-07 17:25:49 +03001701 if (err) {
1702 kfree(pd);
1703 return ERR_PTR(err);
1704 }
1705
1706 if (context) {
1707 resp.pdn = pd->pdn;
1708 if (ib_copy_to_udata(udata, &resp, sizeof(resp))) {
Jack Morgenstein9603b612014-07-28 23:30:22 +03001709 mlx5_core_dealloc_pd(to_mdev(ibdev)->mdev, pd->pdn);
Eli Cohene126ba92013-07-07 17:25:49 +03001710 kfree(pd);
1711 return ERR_PTR(-EFAULT);
1712 }
Eli Cohene126ba92013-07-07 17:25:49 +03001713 }
1714
1715 return &pd->ibpd;
1716}
1717
1718static int mlx5_ib_dealloc_pd(struct ib_pd *pd)
1719{
1720 struct mlx5_ib_dev *mdev = to_mdev(pd->device);
1721 struct mlx5_ib_pd *mpd = to_mpd(pd);
1722
Jack Morgenstein9603b612014-07-28 23:30:22 +03001723 mlx5_core_dealloc_pd(mdev->mdev, mpd->pdn);
Eli Cohene126ba92013-07-07 17:25:49 +03001724 kfree(mpd);
1725
1726 return 0;
1727}
1728
Maor Gottlieb466fa6d2016-08-30 16:58:36 +03001729enum {
1730 MATCH_CRITERIA_ENABLE_OUTER_BIT,
1731 MATCH_CRITERIA_ENABLE_MISC_BIT,
1732 MATCH_CRITERIA_ENABLE_INNER_BIT
1733};
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02001734
Maor Gottlieb466fa6d2016-08-30 16:58:36 +03001735#define HEADER_IS_ZERO(match_criteria, headers) \
1736 !(memchr_inv(MLX5_ADDR_OF(fte_match_param, match_criteria, headers), \
1737 0, MLX5_FLD_SZ_BYTES(fte_match_param, headers))) \
1738
1739static u8 get_match_criteria_enable(u32 *match_criteria)
1740{
1741 u8 match_criteria_enable;
1742
1743 match_criteria_enable =
1744 (!HEADER_IS_ZERO(match_criteria, outer_headers)) <<
1745 MATCH_CRITERIA_ENABLE_OUTER_BIT;
1746 match_criteria_enable |=
1747 (!HEADER_IS_ZERO(match_criteria, misc_parameters)) <<
1748 MATCH_CRITERIA_ENABLE_MISC_BIT;
1749 match_criteria_enable |=
1750 (!HEADER_IS_ZERO(match_criteria, inner_headers)) <<
1751 MATCH_CRITERIA_ENABLE_INNER_BIT;
1752
1753 return match_criteria_enable;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02001754}
1755
Maor Gottliebca0d4752016-08-30 16:58:35 +03001756static void set_proto(void *outer_c, void *outer_v, u8 mask, u8 val)
1757{
1758 MLX5_SET(fte_match_set_lyr_2_4, outer_c, ip_protocol, mask);
1759 MLX5_SET(fte_match_set_lyr_2_4, outer_v, ip_protocol, val);
1760}
1761
Moses Reuben2d1e6972016-11-14 19:04:52 +02001762static void set_flow_label(void *misc_c, void *misc_v, u8 mask, u8 val,
1763 bool inner)
1764{
1765 if (inner) {
1766 MLX5_SET(fte_match_set_misc,
1767 misc_c, inner_ipv6_flow_label, mask);
1768 MLX5_SET(fte_match_set_misc,
1769 misc_v, inner_ipv6_flow_label, val);
1770 } else {
1771 MLX5_SET(fte_match_set_misc,
1772 misc_c, outer_ipv6_flow_label, mask);
1773 MLX5_SET(fte_match_set_misc,
1774 misc_v, outer_ipv6_flow_label, val);
1775 }
1776}
1777
Maor Gottliebca0d4752016-08-30 16:58:35 +03001778static void set_tos(void *outer_c, void *outer_v, u8 mask, u8 val)
1779{
1780 MLX5_SET(fte_match_set_lyr_2_4, outer_c, ip_ecn, mask);
1781 MLX5_SET(fte_match_set_lyr_2_4, outer_v, ip_ecn, val);
1782 MLX5_SET(fte_match_set_lyr_2_4, outer_c, ip_dscp, mask >> 2);
1783 MLX5_SET(fte_match_set_lyr_2_4, outer_v, ip_dscp, val >> 2);
1784}
1785
Maor Gottliebc47ac6a2016-08-30 16:58:31 +03001786#define LAST_ETH_FIELD vlan_tag
1787#define LAST_IB_FIELD sl
Maor Gottliebca0d4752016-08-30 16:58:35 +03001788#define LAST_IPV4_FIELD tos
Maor Gottlieb466fa6d2016-08-30 16:58:36 +03001789#define LAST_IPV6_FIELD traffic_class
Maor Gottliebc47ac6a2016-08-30 16:58:31 +03001790#define LAST_TCP_UDP_FIELD src_port
Moses Reubenffb30d82016-11-14 19:04:50 +02001791#define LAST_TUNNEL_FIELD tunnel_id
Moses Reuben2ac693f2017-01-18 14:59:50 +02001792#define LAST_FLOW_TAG_FIELD tag_id
Slava Shwartsmana22ed862017-04-03 13:13:52 +03001793#define LAST_DROP_FIELD size
Maor Gottliebc47ac6a2016-08-30 16:58:31 +03001794
1795/* Field is the last supported field */
1796#define FIELDS_NOT_SUPPORTED(filter, field)\
1797 memchr_inv((void *)&filter.field +\
1798 sizeof(filter.field), 0,\
1799 sizeof(filter) -\
1800 offsetof(typeof(filter), field) -\
1801 sizeof(filter.field))
1802
Ariel Levkovich19cc7522017-04-03 13:11:03 +03001803#define IPV4_VERSION 4
1804#define IPV6_VERSION 6
1805static int parse_flow_attr(struct mlx5_core_dev *mdev, u32 *match_c,
1806 u32 *match_v, const union ib_flow_spec *ib_spec,
Slava Shwartsmana22ed862017-04-03 13:13:52 +03001807 u32 *tag_id, bool *is_drop)
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02001808{
Maor Gottlieb466fa6d2016-08-30 16:58:36 +03001809 void *misc_params_c = MLX5_ADDR_OF(fte_match_param, match_c,
1810 misc_parameters);
1811 void *misc_params_v = MLX5_ADDR_OF(fte_match_param, match_v,
1812 misc_parameters);
Moses Reuben2d1e6972016-11-14 19:04:52 +02001813 void *headers_c;
1814 void *headers_v;
Ariel Levkovich19cc7522017-04-03 13:11:03 +03001815 int match_ipv;
Maor Gottlieb466fa6d2016-08-30 16:58:36 +03001816
Moses Reuben2d1e6972016-11-14 19:04:52 +02001817 if (ib_spec->type & IB_FLOW_SPEC_INNER) {
1818 headers_c = MLX5_ADDR_OF(fte_match_param, match_c,
1819 inner_headers);
1820 headers_v = MLX5_ADDR_OF(fte_match_param, match_v,
1821 inner_headers);
Ariel Levkovich19cc7522017-04-03 13:11:03 +03001822 match_ipv = MLX5_CAP_FLOWTABLE_NIC_RX(mdev,
1823 ft_field_support.inner_ip_version);
Moses Reuben2d1e6972016-11-14 19:04:52 +02001824 } else {
1825 headers_c = MLX5_ADDR_OF(fte_match_param, match_c,
1826 outer_headers);
1827 headers_v = MLX5_ADDR_OF(fte_match_param, match_v,
1828 outer_headers);
Ariel Levkovich19cc7522017-04-03 13:11:03 +03001829 match_ipv = MLX5_CAP_FLOWTABLE_NIC_RX(mdev,
1830 ft_field_support.outer_ip_version);
Moses Reuben2d1e6972016-11-14 19:04:52 +02001831 }
1832
1833 switch (ib_spec->type & ~IB_FLOW_SPEC_INNER) {
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02001834 case IB_FLOW_SPEC_ETH:
Maor Gottliebc47ac6a2016-08-30 16:58:31 +03001835 if (FIELDS_NOT_SUPPORTED(ib_spec->eth.mask, LAST_ETH_FIELD))
Leon Romanovsky1ffd3a22017-01-18 14:59:51 +02001836 return -EOPNOTSUPP;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02001837
Moses Reuben2d1e6972016-11-14 19:04:52 +02001838 ether_addr_copy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02001839 dmac_47_16),
1840 ib_spec->eth.mask.dst_mac);
Moses Reuben2d1e6972016-11-14 19:04:52 +02001841 ether_addr_copy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02001842 dmac_47_16),
1843 ib_spec->eth.val.dst_mac);
1844
Moses Reuben2d1e6972016-11-14 19:04:52 +02001845 ether_addr_copy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
Maor Gottliebee3da802016-09-12 19:16:24 +03001846 smac_47_16),
1847 ib_spec->eth.mask.src_mac);
Moses Reuben2d1e6972016-11-14 19:04:52 +02001848 ether_addr_copy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
Maor Gottliebee3da802016-09-12 19:16:24 +03001849 smac_47_16),
1850 ib_spec->eth.val.src_mac);
1851
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02001852 if (ib_spec->eth.mask.vlan_tag) {
Moses Reuben2d1e6972016-11-14 19:04:52 +02001853 MLX5_SET(fte_match_set_lyr_2_4, headers_c,
Mohamad Haj Yahia10543362016-10-09 16:25:43 +03001854 cvlan_tag, 1);
Moses Reuben2d1e6972016-11-14 19:04:52 +02001855 MLX5_SET(fte_match_set_lyr_2_4, headers_v,
Mohamad Haj Yahia10543362016-10-09 16:25:43 +03001856 cvlan_tag, 1);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02001857
Moses Reuben2d1e6972016-11-14 19:04:52 +02001858 MLX5_SET(fte_match_set_lyr_2_4, headers_c,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02001859 first_vid, ntohs(ib_spec->eth.mask.vlan_tag));
Moses Reuben2d1e6972016-11-14 19:04:52 +02001860 MLX5_SET(fte_match_set_lyr_2_4, headers_v,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02001861 first_vid, ntohs(ib_spec->eth.val.vlan_tag));
1862
Moses Reuben2d1e6972016-11-14 19:04:52 +02001863 MLX5_SET(fte_match_set_lyr_2_4, headers_c,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02001864 first_cfi,
1865 ntohs(ib_spec->eth.mask.vlan_tag) >> 12);
Moses Reuben2d1e6972016-11-14 19:04:52 +02001866 MLX5_SET(fte_match_set_lyr_2_4, headers_v,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02001867 first_cfi,
1868 ntohs(ib_spec->eth.val.vlan_tag) >> 12);
1869
Moses Reuben2d1e6972016-11-14 19:04:52 +02001870 MLX5_SET(fte_match_set_lyr_2_4, headers_c,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02001871 first_prio,
1872 ntohs(ib_spec->eth.mask.vlan_tag) >> 13);
Moses Reuben2d1e6972016-11-14 19:04:52 +02001873 MLX5_SET(fte_match_set_lyr_2_4, headers_v,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02001874 first_prio,
1875 ntohs(ib_spec->eth.val.vlan_tag) >> 13);
1876 }
Moses Reuben2d1e6972016-11-14 19:04:52 +02001877 MLX5_SET(fte_match_set_lyr_2_4, headers_c,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02001878 ethertype, ntohs(ib_spec->eth.mask.ether_type));
Moses Reuben2d1e6972016-11-14 19:04:52 +02001879 MLX5_SET(fte_match_set_lyr_2_4, headers_v,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02001880 ethertype, ntohs(ib_spec->eth.val.ether_type));
1881 break;
1882 case IB_FLOW_SPEC_IPV4:
Maor Gottliebc47ac6a2016-08-30 16:58:31 +03001883 if (FIELDS_NOT_SUPPORTED(ib_spec->ipv4.mask, LAST_IPV4_FIELD))
Leon Romanovsky1ffd3a22017-01-18 14:59:51 +02001884 return -EOPNOTSUPP;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02001885
Ariel Levkovich19cc7522017-04-03 13:11:03 +03001886 if (match_ipv) {
1887 MLX5_SET(fte_match_set_lyr_2_4, headers_c,
1888 ip_version, 0xf);
1889 MLX5_SET(fte_match_set_lyr_2_4, headers_v,
1890 ip_version, IPV4_VERSION);
1891 } else {
1892 MLX5_SET(fte_match_set_lyr_2_4, headers_c,
1893 ethertype, 0xffff);
1894 MLX5_SET(fte_match_set_lyr_2_4, headers_v,
1895 ethertype, ETH_P_IP);
1896 }
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02001897
Moses Reuben2d1e6972016-11-14 19:04:52 +02001898 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02001899 src_ipv4_src_ipv6.ipv4_layout.ipv4),
1900 &ib_spec->ipv4.mask.src_ip,
1901 sizeof(ib_spec->ipv4.mask.src_ip));
Moses Reuben2d1e6972016-11-14 19:04:52 +02001902 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02001903 src_ipv4_src_ipv6.ipv4_layout.ipv4),
1904 &ib_spec->ipv4.val.src_ip,
1905 sizeof(ib_spec->ipv4.val.src_ip));
Moses Reuben2d1e6972016-11-14 19:04:52 +02001906 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02001907 dst_ipv4_dst_ipv6.ipv4_layout.ipv4),
1908 &ib_spec->ipv4.mask.dst_ip,
1909 sizeof(ib_spec->ipv4.mask.dst_ip));
Moses Reuben2d1e6972016-11-14 19:04:52 +02001910 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02001911 dst_ipv4_dst_ipv6.ipv4_layout.ipv4),
1912 &ib_spec->ipv4.val.dst_ip,
1913 sizeof(ib_spec->ipv4.val.dst_ip));
Maor Gottliebca0d4752016-08-30 16:58:35 +03001914
Moses Reuben2d1e6972016-11-14 19:04:52 +02001915 set_tos(headers_c, headers_v,
Maor Gottliebca0d4752016-08-30 16:58:35 +03001916 ib_spec->ipv4.mask.tos, ib_spec->ipv4.val.tos);
1917
Moses Reuben2d1e6972016-11-14 19:04:52 +02001918 set_proto(headers_c, headers_v,
Maor Gottliebca0d4752016-08-30 16:58:35 +03001919 ib_spec->ipv4.mask.proto, ib_spec->ipv4.val.proto);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02001920 break;
Maor Gottlieb026bae02016-06-17 15:14:51 +03001921 case IB_FLOW_SPEC_IPV6:
Maor Gottliebc47ac6a2016-08-30 16:58:31 +03001922 if (FIELDS_NOT_SUPPORTED(ib_spec->ipv6.mask, LAST_IPV6_FIELD))
Leon Romanovsky1ffd3a22017-01-18 14:59:51 +02001923 return -EOPNOTSUPP;
Maor Gottlieb026bae02016-06-17 15:14:51 +03001924
Ariel Levkovich19cc7522017-04-03 13:11:03 +03001925 if (match_ipv) {
1926 MLX5_SET(fte_match_set_lyr_2_4, headers_c,
1927 ip_version, 0xf);
1928 MLX5_SET(fte_match_set_lyr_2_4, headers_v,
1929 ip_version, IPV6_VERSION);
1930 } else {
1931 MLX5_SET(fte_match_set_lyr_2_4, headers_c,
1932 ethertype, 0xffff);
1933 MLX5_SET(fte_match_set_lyr_2_4, headers_v,
1934 ethertype, ETH_P_IPV6);
1935 }
Maor Gottlieb026bae02016-06-17 15:14:51 +03001936
Moses Reuben2d1e6972016-11-14 19:04:52 +02001937 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
Maor Gottlieb026bae02016-06-17 15:14:51 +03001938 src_ipv4_src_ipv6.ipv6_layout.ipv6),
1939 &ib_spec->ipv6.mask.src_ip,
1940 sizeof(ib_spec->ipv6.mask.src_ip));
Moses Reuben2d1e6972016-11-14 19:04:52 +02001941 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
Maor Gottlieb026bae02016-06-17 15:14:51 +03001942 src_ipv4_src_ipv6.ipv6_layout.ipv6),
1943 &ib_spec->ipv6.val.src_ip,
1944 sizeof(ib_spec->ipv6.val.src_ip));
Moses Reuben2d1e6972016-11-14 19:04:52 +02001945 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
Maor Gottlieb026bae02016-06-17 15:14:51 +03001946 dst_ipv4_dst_ipv6.ipv6_layout.ipv6),
1947 &ib_spec->ipv6.mask.dst_ip,
1948 sizeof(ib_spec->ipv6.mask.dst_ip));
Moses Reuben2d1e6972016-11-14 19:04:52 +02001949 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
Maor Gottlieb026bae02016-06-17 15:14:51 +03001950 dst_ipv4_dst_ipv6.ipv6_layout.ipv6),
1951 &ib_spec->ipv6.val.dst_ip,
1952 sizeof(ib_spec->ipv6.val.dst_ip));
Maor Gottlieb466fa6d2016-08-30 16:58:36 +03001953
Moses Reuben2d1e6972016-11-14 19:04:52 +02001954 set_tos(headers_c, headers_v,
Maor Gottlieb466fa6d2016-08-30 16:58:36 +03001955 ib_spec->ipv6.mask.traffic_class,
1956 ib_spec->ipv6.val.traffic_class);
1957
Moses Reuben2d1e6972016-11-14 19:04:52 +02001958 set_proto(headers_c, headers_v,
Maor Gottlieb466fa6d2016-08-30 16:58:36 +03001959 ib_spec->ipv6.mask.next_hdr,
1960 ib_spec->ipv6.val.next_hdr);
1961
Moses Reuben2d1e6972016-11-14 19:04:52 +02001962 set_flow_label(misc_params_c, misc_params_v,
1963 ntohl(ib_spec->ipv6.mask.flow_label),
1964 ntohl(ib_spec->ipv6.val.flow_label),
1965 ib_spec->type & IB_FLOW_SPEC_INNER);
1966
Maor Gottlieb026bae02016-06-17 15:14:51 +03001967 break;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02001968 case IB_FLOW_SPEC_TCP:
Maor Gottliebc47ac6a2016-08-30 16:58:31 +03001969 if (FIELDS_NOT_SUPPORTED(ib_spec->tcp_udp.mask,
1970 LAST_TCP_UDP_FIELD))
Leon Romanovsky1ffd3a22017-01-18 14:59:51 +02001971 return -EOPNOTSUPP;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02001972
Moses Reuben2d1e6972016-11-14 19:04:52 +02001973 MLX5_SET(fte_match_set_lyr_2_4, headers_c, ip_protocol,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02001974 0xff);
Moses Reuben2d1e6972016-11-14 19:04:52 +02001975 MLX5_SET(fte_match_set_lyr_2_4, headers_v, ip_protocol,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02001976 IPPROTO_TCP);
1977
Moses Reuben2d1e6972016-11-14 19:04:52 +02001978 MLX5_SET(fte_match_set_lyr_2_4, headers_c, tcp_sport,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02001979 ntohs(ib_spec->tcp_udp.mask.src_port));
Moses Reuben2d1e6972016-11-14 19:04:52 +02001980 MLX5_SET(fte_match_set_lyr_2_4, headers_v, tcp_sport,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02001981 ntohs(ib_spec->tcp_udp.val.src_port));
1982
Moses Reuben2d1e6972016-11-14 19:04:52 +02001983 MLX5_SET(fte_match_set_lyr_2_4, headers_c, tcp_dport,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02001984 ntohs(ib_spec->tcp_udp.mask.dst_port));
Moses Reuben2d1e6972016-11-14 19:04:52 +02001985 MLX5_SET(fte_match_set_lyr_2_4, headers_v, tcp_dport,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02001986 ntohs(ib_spec->tcp_udp.val.dst_port));
1987 break;
1988 case IB_FLOW_SPEC_UDP:
Maor Gottliebc47ac6a2016-08-30 16:58:31 +03001989 if (FIELDS_NOT_SUPPORTED(ib_spec->tcp_udp.mask,
1990 LAST_TCP_UDP_FIELD))
Leon Romanovsky1ffd3a22017-01-18 14:59:51 +02001991 return -EOPNOTSUPP;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02001992
Moses Reuben2d1e6972016-11-14 19:04:52 +02001993 MLX5_SET(fte_match_set_lyr_2_4, headers_c, ip_protocol,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02001994 0xff);
Moses Reuben2d1e6972016-11-14 19:04:52 +02001995 MLX5_SET(fte_match_set_lyr_2_4, headers_v, ip_protocol,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02001996 IPPROTO_UDP);
1997
Moses Reuben2d1e6972016-11-14 19:04:52 +02001998 MLX5_SET(fte_match_set_lyr_2_4, headers_c, udp_sport,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02001999 ntohs(ib_spec->tcp_udp.mask.src_port));
Moses Reuben2d1e6972016-11-14 19:04:52 +02002000 MLX5_SET(fte_match_set_lyr_2_4, headers_v, udp_sport,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002001 ntohs(ib_spec->tcp_udp.val.src_port));
2002
Moses Reuben2d1e6972016-11-14 19:04:52 +02002003 MLX5_SET(fte_match_set_lyr_2_4, headers_c, udp_dport,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002004 ntohs(ib_spec->tcp_udp.mask.dst_port));
Moses Reuben2d1e6972016-11-14 19:04:52 +02002005 MLX5_SET(fte_match_set_lyr_2_4, headers_v, udp_dport,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002006 ntohs(ib_spec->tcp_udp.val.dst_port));
2007 break;
Moses Reubenffb30d82016-11-14 19:04:50 +02002008 case IB_FLOW_SPEC_VXLAN_TUNNEL:
2009 if (FIELDS_NOT_SUPPORTED(ib_spec->tunnel.mask,
2010 LAST_TUNNEL_FIELD))
Leon Romanovsky1ffd3a22017-01-18 14:59:51 +02002011 return -EOPNOTSUPP;
Moses Reubenffb30d82016-11-14 19:04:50 +02002012
2013 MLX5_SET(fte_match_set_misc, misc_params_c, vxlan_vni,
2014 ntohl(ib_spec->tunnel.mask.tunnel_id));
2015 MLX5_SET(fte_match_set_misc, misc_params_v, vxlan_vni,
2016 ntohl(ib_spec->tunnel.val.tunnel_id));
2017 break;
Moses Reuben2ac693f2017-01-18 14:59:50 +02002018 case IB_FLOW_SPEC_ACTION_TAG:
2019 if (FIELDS_NOT_SUPPORTED(ib_spec->flow_tag,
2020 LAST_FLOW_TAG_FIELD))
2021 return -EOPNOTSUPP;
2022 if (ib_spec->flow_tag.tag_id >= BIT(24))
2023 return -EINVAL;
2024
2025 *tag_id = ib_spec->flow_tag.tag_id;
2026 break;
Slava Shwartsmana22ed862017-04-03 13:13:52 +03002027 case IB_FLOW_SPEC_ACTION_DROP:
2028 if (FIELDS_NOT_SUPPORTED(ib_spec->drop,
2029 LAST_DROP_FIELD))
2030 return -EOPNOTSUPP;
2031 *is_drop = true;
2032 break;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002033 default:
2034 return -EINVAL;
2035 }
2036
2037 return 0;
2038}
2039
2040/* If a flow could catch both multicast and unicast packets,
2041 * it won't fall into the multicast flow steering table and this rule
2042 * could steal other multicast packets.
2043 */
2044static bool flow_is_multicast_only(struct ib_flow_attr *ib_attr)
2045{
2046 struct ib_flow_spec_eth *eth_spec;
2047
2048 if (ib_attr->type != IB_FLOW_ATTR_NORMAL ||
2049 ib_attr->size < sizeof(struct ib_flow_attr) +
2050 sizeof(struct ib_flow_spec_eth) ||
2051 ib_attr->num_of_specs < 1)
2052 return false;
2053
2054 eth_spec = (struct ib_flow_spec_eth *)(ib_attr + 1);
2055 if (eth_spec->type != IB_FLOW_SPEC_ETH ||
2056 eth_spec->size != sizeof(*eth_spec))
2057 return false;
2058
2059 return is_multicast_ether_addr(eth_spec->mask.dst_mac) &&
2060 is_multicast_ether_addr(eth_spec->val.dst_mac);
2061}
2062
Ariel Levkovich19cc7522017-04-03 13:11:03 +03002063static bool is_valid_ethertype(struct mlx5_core_dev *mdev,
2064 const struct ib_flow_attr *flow_attr,
Ariel Levkovich0f750962017-04-03 13:11:02 +03002065 bool check_inner)
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002066{
2067 union ib_flow_spec *ib_spec = (union ib_flow_spec *)(flow_attr + 1);
Ariel Levkovich19cc7522017-04-03 13:11:03 +03002068 int match_ipv = check_inner ?
2069 MLX5_CAP_FLOWTABLE_NIC_RX(mdev,
2070 ft_field_support.inner_ip_version) :
2071 MLX5_CAP_FLOWTABLE_NIC_RX(mdev,
2072 ft_field_support.outer_ip_version);
Ariel Levkovich0f750962017-04-03 13:11:02 +03002073 int inner_bit = check_inner ? IB_FLOW_SPEC_INNER : 0;
2074 bool ipv4_spec_valid, ipv6_spec_valid;
2075 unsigned int ip_spec_type = 0;
2076 bool has_ethertype = false;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002077 unsigned int spec_index;
Ariel Levkovich0f750962017-04-03 13:11:02 +03002078 bool mask_valid = true;
2079 u16 eth_type = 0;
2080 bool type_valid;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002081
2082 /* Validate that ethertype is correct */
2083 for (spec_index = 0; spec_index < flow_attr->num_of_specs; spec_index++) {
Ariel Levkovich0f750962017-04-03 13:11:02 +03002084 if ((ib_spec->type == (IB_FLOW_SPEC_ETH | inner_bit)) &&
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002085 ib_spec->eth.mask.ether_type) {
Ariel Levkovich0f750962017-04-03 13:11:02 +03002086 mask_valid = (ib_spec->eth.mask.ether_type ==
2087 htons(0xffff));
2088 has_ethertype = true;
2089 eth_type = ntohs(ib_spec->eth.val.ether_type);
2090 } else if ((ib_spec->type == (IB_FLOW_SPEC_IPV4 | inner_bit)) ||
2091 (ib_spec->type == (IB_FLOW_SPEC_IPV6 | inner_bit))) {
2092 ip_spec_type = ib_spec->type;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002093 }
2094 ib_spec = (void *)ib_spec + ib_spec->size;
2095 }
Ariel Levkovich0f750962017-04-03 13:11:02 +03002096
2097 type_valid = (!has_ethertype) || (!ip_spec_type);
2098 if (!type_valid && mask_valid) {
2099 ipv4_spec_valid = (eth_type == ETH_P_IP) &&
2100 (ip_spec_type == (IB_FLOW_SPEC_IPV4 | inner_bit));
2101 ipv6_spec_valid = (eth_type == ETH_P_IPV6) &&
2102 (ip_spec_type == (IB_FLOW_SPEC_IPV6 | inner_bit));
Ariel Levkovich19cc7522017-04-03 13:11:03 +03002103
2104 type_valid = (ipv4_spec_valid) || (ipv6_spec_valid) ||
2105 (((eth_type == ETH_P_MPLS_UC) ||
2106 (eth_type == ETH_P_MPLS_MC)) && match_ipv);
Ariel Levkovich0f750962017-04-03 13:11:02 +03002107 }
2108
2109 return type_valid;
2110}
2111
Ariel Levkovich19cc7522017-04-03 13:11:03 +03002112static bool is_valid_attr(struct mlx5_core_dev *mdev,
2113 const struct ib_flow_attr *flow_attr)
Ariel Levkovich0f750962017-04-03 13:11:02 +03002114{
Ariel Levkovich19cc7522017-04-03 13:11:03 +03002115 return is_valid_ethertype(mdev, flow_attr, false) &&
2116 is_valid_ethertype(mdev, flow_attr, true);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002117}
2118
2119static void put_flow_table(struct mlx5_ib_dev *dev,
2120 struct mlx5_ib_flow_prio *prio, bool ft_added)
2121{
2122 prio->refcount -= !!ft_added;
2123 if (!prio->refcount) {
2124 mlx5_destroy_flow_table(prio->flow_table);
2125 prio->flow_table = NULL;
2126 }
2127}
2128
2129static int mlx5_ib_destroy_flow(struct ib_flow *flow_id)
2130{
2131 struct mlx5_ib_dev *dev = to_mdev(flow_id->qp->device);
2132 struct mlx5_ib_flow_handler *handler = container_of(flow_id,
2133 struct mlx5_ib_flow_handler,
2134 ibflow);
2135 struct mlx5_ib_flow_handler *iter, *tmp;
2136
2137 mutex_lock(&dev->flow_db.lock);
2138
2139 list_for_each_entry_safe(iter, tmp, &handler->list, list) {
Mark Bloch74491de2016-08-31 11:24:25 +00002140 mlx5_del_flow_rules(iter->rule);
Maor Gottliebcc0e5d42016-08-28 14:16:34 +03002141 put_flow_table(dev, iter->prio, true);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002142 list_del(&iter->list);
2143 kfree(iter);
2144 }
2145
Mark Bloch74491de2016-08-31 11:24:25 +00002146 mlx5_del_flow_rules(handler->rule);
Maor Gottlieb5497adc2016-08-28 14:16:31 +03002147 put_flow_table(dev, handler->prio, true);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002148 mutex_unlock(&dev->flow_db.lock);
2149
2150 kfree(handler);
2151
2152 return 0;
2153}
2154
Maor Gottlieb35d190112016-03-07 18:51:47 +02002155static int ib_prio_to_core_prio(unsigned int priority, bool dont_trap)
2156{
2157 priority *= 2;
2158 if (!dont_trap)
2159 priority++;
2160 return priority;
2161}
2162
Maor Gottliebcc0e5d42016-08-28 14:16:34 +03002163enum flow_table_type {
2164 MLX5_IB_FT_RX,
2165 MLX5_IB_FT_TX
2166};
2167
Maor Gottlieb00b7c2a2017-03-29 06:09:01 +03002168#define MLX5_FS_MAX_TYPES 6
2169#define MLX5_FS_MAX_ENTRIES BIT(16)
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002170static struct mlx5_ib_flow_prio *get_flow_table(struct mlx5_ib_dev *dev,
Maor Gottliebcc0e5d42016-08-28 14:16:34 +03002171 struct ib_flow_attr *flow_attr,
2172 enum flow_table_type ft_type)
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002173{
Maor Gottlieb35d190112016-03-07 18:51:47 +02002174 bool dont_trap = flow_attr->flags & IB_FLOW_ATTR_FLAGS_DONT_TRAP;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002175 struct mlx5_flow_namespace *ns = NULL;
2176 struct mlx5_ib_flow_prio *prio;
2177 struct mlx5_flow_table *ft;
Maor Gottliebdac388e2017-03-29 06:09:00 +03002178 int max_table_size;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002179 int num_entries;
2180 int num_groups;
2181 int priority;
2182 int err = 0;
2183
Maor Gottliebdac388e2017-03-29 06:09:00 +03002184 max_table_size = BIT(MLX5_CAP_FLOWTABLE_NIC_RX(dev->mdev,
2185 log_max_ft_size));
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002186 if (flow_attr->type == IB_FLOW_ATTR_NORMAL) {
Maor Gottlieb35d190112016-03-07 18:51:47 +02002187 if (flow_is_multicast_only(flow_attr) &&
2188 !dont_trap)
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002189 priority = MLX5_IB_FLOW_MCAST_PRIO;
2190 else
Maor Gottlieb35d190112016-03-07 18:51:47 +02002191 priority = ib_prio_to_core_prio(flow_attr->priority,
2192 dont_trap);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002193 ns = mlx5_get_flow_namespace(dev->mdev,
2194 MLX5_FLOW_NAMESPACE_BYPASS);
2195 num_entries = MLX5_FS_MAX_ENTRIES;
2196 num_groups = MLX5_FS_MAX_TYPES;
2197 prio = &dev->flow_db.prios[priority];
2198 } else if (flow_attr->type == IB_FLOW_ATTR_ALL_DEFAULT ||
2199 flow_attr->type == IB_FLOW_ATTR_MC_DEFAULT) {
2200 ns = mlx5_get_flow_namespace(dev->mdev,
2201 MLX5_FLOW_NAMESPACE_LEFTOVERS);
2202 build_leftovers_ft_param(&priority,
2203 &num_entries,
2204 &num_groups);
2205 prio = &dev->flow_db.prios[MLX5_IB_FLOW_LEFTOVERS_PRIO];
Maor Gottliebcc0e5d42016-08-28 14:16:34 +03002206 } else if (flow_attr->type == IB_FLOW_ATTR_SNIFFER) {
2207 if (!MLX5_CAP_FLOWTABLE(dev->mdev,
2208 allow_sniffer_and_nic_rx_shared_tir))
2209 return ERR_PTR(-ENOTSUPP);
2210
2211 ns = mlx5_get_flow_namespace(dev->mdev, ft_type == MLX5_IB_FT_RX ?
2212 MLX5_FLOW_NAMESPACE_SNIFFER_RX :
2213 MLX5_FLOW_NAMESPACE_SNIFFER_TX);
2214
2215 prio = &dev->flow_db.sniffer[ft_type];
2216 priority = 0;
2217 num_entries = 1;
2218 num_groups = 1;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002219 }
2220
2221 if (!ns)
2222 return ERR_PTR(-ENOTSUPP);
2223
Maor Gottliebdac388e2017-03-29 06:09:00 +03002224 if (num_entries > max_table_size)
2225 return ERR_PTR(-ENOMEM);
2226
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002227 ft = prio->flow_table;
2228 if (!ft) {
2229 ft = mlx5_create_auto_grouped_flow_table(ns, priority,
2230 num_entries,
Maor Gottliebd63cd282016-04-29 01:36:35 +03002231 num_groups,
Hadar Hen Zionc9f1b072016-11-07 15:14:44 +02002232 0, 0);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002233
2234 if (!IS_ERR(ft)) {
2235 prio->refcount = 0;
2236 prio->flow_table = ft;
2237 } else {
2238 err = PTR_ERR(ft);
2239 }
2240 }
2241
2242 return err ? ERR_PTR(err) : prio;
2243}
2244
2245static struct mlx5_ib_flow_handler *create_flow_rule(struct mlx5_ib_dev *dev,
2246 struct mlx5_ib_flow_prio *ft_prio,
Maor Gottliebdd063d02016-08-28 14:16:32 +03002247 const struct ib_flow_attr *flow_attr,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002248 struct mlx5_flow_destination *dst)
2249{
2250 struct mlx5_flow_table *ft = ft_prio->flow_table;
2251 struct mlx5_ib_flow_handler *handler;
Hadar Hen Zion66958ed2016-11-07 15:14:45 +02002252 struct mlx5_flow_act flow_act = {0};
Maor Gottliebc5bb1732016-07-04 17:23:05 +03002253 struct mlx5_flow_spec *spec;
Slava Shwartsmana22ed862017-04-03 13:13:52 +03002254 struct mlx5_flow_destination *rule_dst = dst;
Maor Gottliebdd063d02016-08-28 14:16:32 +03002255 const void *ib_flow = (const void *)flow_attr + sizeof(*flow_attr);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002256 unsigned int spec_index;
Moses Reuben2ac693f2017-01-18 14:59:50 +02002257 u32 flow_tag = MLX5_FS_DEFAULT_FLOW_TAG;
Slava Shwartsmana22ed862017-04-03 13:13:52 +03002258 bool is_drop = false;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002259 int err = 0;
Slava Shwartsmana22ed862017-04-03 13:13:52 +03002260 int dest_num = 1;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002261
Ariel Levkovich19cc7522017-04-03 13:11:03 +03002262 if (!is_valid_attr(dev->mdev, flow_attr))
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002263 return ERR_PTR(-EINVAL);
2264
Leon Romanovsky1b9a07e2017-05-10 21:32:18 +03002265 spec = kvzalloc(sizeof(*spec), GFP_KERNEL);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002266 handler = kzalloc(sizeof(*handler), GFP_KERNEL);
Maor Gottliebc5bb1732016-07-04 17:23:05 +03002267 if (!handler || !spec) {
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002268 err = -ENOMEM;
2269 goto free;
2270 }
2271
2272 INIT_LIST_HEAD(&handler->list);
2273
2274 for (spec_index = 0; spec_index < flow_attr->num_of_specs; spec_index++) {
Ariel Levkovich19cc7522017-04-03 13:11:03 +03002275 err = parse_flow_attr(dev->mdev, spec->match_criteria,
Slava Shwartsmana22ed862017-04-03 13:13:52 +03002276 spec->match_value,
2277 ib_flow, &flow_tag, &is_drop);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002278 if (err < 0)
2279 goto free;
2280
2281 ib_flow += ((union ib_flow_spec *)ib_flow)->size;
2282 }
2283
Maor Gottlieb466fa6d2016-08-30 16:58:36 +03002284 spec->match_criteria_enable = get_match_criteria_enable(spec->match_criteria);
Slava Shwartsmana22ed862017-04-03 13:13:52 +03002285 if (is_drop) {
2286 flow_act.action = MLX5_FLOW_CONTEXT_ACTION_DROP;
2287 rule_dst = NULL;
2288 dest_num = 0;
2289 } else {
2290 flow_act.action = dst ? MLX5_FLOW_CONTEXT_ACTION_FWD_DEST :
2291 MLX5_FLOW_CONTEXT_ACTION_FWD_NEXT_PRIO;
2292 }
Moses Reuben2ac693f2017-01-18 14:59:50 +02002293
2294 if (flow_tag != MLX5_FS_DEFAULT_FLOW_TAG &&
2295 (flow_attr->type == IB_FLOW_ATTR_ALL_DEFAULT ||
2296 flow_attr->type == IB_FLOW_ATTR_MC_DEFAULT)) {
2297 mlx5_ib_warn(dev, "Flow tag %u and attribute type %x isn't allowed in leftovers\n",
2298 flow_tag, flow_attr->type);
2299 err = -EINVAL;
2300 goto free;
2301 }
2302 flow_act.flow_tag = flow_tag;
Mark Bloch74491de2016-08-31 11:24:25 +00002303 handler->rule = mlx5_add_flow_rules(ft, spec,
Hadar Hen Zion66958ed2016-11-07 15:14:45 +02002304 &flow_act,
Slava Shwartsmana22ed862017-04-03 13:13:52 +03002305 rule_dst, dest_num);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002306
2307 if (IS_ERR(handler->rule)) {
2308 err = PTR_ERR(handler->rule);
2309 goto free;
2310 }
2311
Maor Gottliebd9d49802016-08-28 14:16:33 +03002312 ft_prio->refcount++;
Maor Gottlieb5497adc2016-08-28 14:16:31 +03002313 handler->prio = ft_prio;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002314
2315 ft_prio->flow_table = ft;
2316free:
2317 if (err)
2318 kfree(handler);
Maor Gottliebc5bb1732016-07-04 17:23:05 +03002319 kvfree(spec);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002320 return err ? ERR_PTR(err) : handler;
2321}
2322
Maor Gottlieb35d190112016-03-07 18:51:47 +02002323static struct mlx5_ib_flow_handler *create_dont_trap_rule(struct mlx5_ib_dev *dev,
2324 struct mlx5_ib_flow_prio *ft_prio,
2325 struct ib_flow_attr *flow_attr,
2326 struct mlx5_flow_destination *dst)
2327{
2328 struct mlx5_ib_flow_handler *handler_dst = NULL;
2329 struct mlx5_ib_flow_handler *handler = NULL;
2330
2331 handler = create_flow_rule(dev, ft_prio, flow_attr, NULL);
2332 if (!IS_ERR(handler)) {
2333 handler_dst = create_flow_rule(dev, ft_prio,
2334 flow_attr, dst);
2335 if (IS_ERR(handler_dst)) {
Mark Bloch74491de2016-08-31 11:24:25 +00002336 mlx5_del_flow_rules(handler->rule);
Maor Gottliebd9d49802016-08-28 14:16:33 +03002337 ft_prio->refcount--;
Maor Gottlieb35d190112016-03-07 18:51:47 +02002338 kfree(handler);
2339 handler = handler_dst;
2340 } else {
2341 list_add(&handler_dst->list, &handler->list);
2342 }
2343 }
2344
2345 return handler;
2346}
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002347enum {
2348 LEFTOVERS_MC,
2349 LEFTOVERS_UC,
2350};
2351
2352static struct mlx5_ib_flow_handler *create_leftovers_rule(struct mlx5_ib_dev *dev,
2353 struct mlx5_ib_flow_prio *ft_prio,
2354 struct ib_flow_attr *flow_attr,
2355 struct mlx5_flow_destination *dst)
2356{
2357 struct mlx5_ib_flow_handler *handler_ucast = NULL;
2358 struct mlx5_ib_flow_handler *handler = NULL;
2359
2360 static struct {
2361 struct ib_flow_attr flow_attr;
2362 struct ib_flow_spec_eth eth_flow;
2363 } leftovers_specs[] = {
2364 [LEFTOVERS_MC] = {
2365 .flow_attr = {
2366 .num_of_specs = 1,
2367 .size = sizeof(leftovers_specs[0])
2368 },
2369 .eth_flow = {
2370 .type = IB_FLOW_SPEC_ETH,
2371 .size = sizeof(struct ib_flow_spec_eth),
2372 .mask = {.dst_mac = {0x1} },
2373 .val = {.dst_mac = {0x1} }
2374 }
2375 },
2376 [LEFTOVERS_UC] = {
2377 .flow_attr = {
2378 .num_of_specs = 1,
2379 .size = sizeof(leftovers_specs[0])
2380 },
2381 .eth_flow = {
2382 .type = IB_FLOW_SPEC_ETH,
2383 .size = sizeof(struct ib_flow_spec_eth),
2384 .mask = {.dst_mac = {0x1} },
2385 .val = {.dst_mac = {} }
2386 }
2387 }
2388 };
2389
2390 handler = create_flow_rule(dev, ft_prio,
2391 &leftovers_specs[LEFTOVERS_MC].flow_attr,
2392 dst);
2393 if (!IS_ERR(handler) &&
2394 flow_attr->type == IB_FLOW_ATTR_ALL_DEFAULT) {
2395 handler_ucast = create_flow_rule(dev, ft_prio,
2396 &leftovers_specs[LEFTOVERS_UC].flow_attr,
2397 dst);
2398 if (IS_ERR(handler_ucast)) {
Mark Bloch74491de2016-08-31 11:24:25 +00002399 mlx5_del_flow_rules(handler->rule);
Maor Gottliebd9d49802016-08-28 14:16:33 +03002400 ft_prio->refcount--;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002401 kfree(handler);
2402 handler = handler_ucast;
2403 } else {
2404 list_add(&handler_ucast->list, &handler->list);
2405 }
2406 }
2407
2408 return handler;
2409}
2410
Maor Gottliebcc0e5d42016-08-28 14:16:34 +03002411static struct mlx5_ib_flow_handler *create_sniffer_rule(struct mlx5_ib_dev *dev,
2412 struct mlx5_ib_flow_prio *ft_rx,
2413 struct mlx5_ib_flow_prio *ft_tx,
2414 struct mlx5_flow_destination *dst)
2415{
2416 struct mlx5_ib_flow_handler *handler_rx;
2417 struct mlx5_ib_flow_handler *handler_tx;
2418 int err;
2419 static const struct ib_flow_attr flow_attr = {
2420 .num_of_specs = 0,
2421 .size = sizeof(flow_attr)
2422 };
2423
2424 handler_rx = create_flow_rule(dev, ft_rx, &flow_attr, dst);
2425 if (IS_ERR(handler_rx)) {
2426 err = PTR_ERR(handler_rx);
2427 goto err;
2428 }
2429
2430 handler_tx = create_flow_rule(dev, ft_tx, &flow_attr, dst);
2431 if (IS_ERR(handler_tx)) {
2432 err = PTR_ERR(handler_tx);
2433 goto err_tx;
2434 }
2435
2436 list_add(&handler_tx->list, &handler_rx->list);
2437
2438 return handler_rx;
2439
2440err_tx:
Mark Bloch74491de2016-08-31 11:24:25 +00002441 mlx5_del_flow_rules(handler_rx->rule);
Maor Gottliebcc0e5d42016-08-28 14:16:34 +03002442 ft_rx->refcount--;
2443 kfree(handler_rx);
2444err:
2445 return ERR_PTR(err);
2446}
2447
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002448static struct ib_flow *mlx5_ib_create_flow(struct ib_qp *qp,
2449 struct ib_flow_attr *flow_attr,
2450 int domain)
2451{
2452 struct mlx5_ib_dev *dev = to_mdev(qp->device);
Yishai Hadasd9f88e52016-08-28 10:58:37 +03002453 struct mlx5_ib_qp *mqp = to_mqp(qp);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002454 struct mlx5_ib_flow_handler *handler = NULL;
2455 struct mlx5_flow_destination *dst = NULL;
Maor Gottliebcc0e5d42016-08-28 14:16:34 +03002456 struct mlx5_ib_flow_prio *ft_prio_tx = NULL;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002457 struct mlx5_ib_flow_prio *ft_prio;
2458 int err;
2459
2460 if (flow_attr->priority > MLX5_IB_FLOW_LAST_PRIO)
Maor Gottliebdac388e2017-03-29 06:09:00 +03002461 return ERR_PTR(-ENOMEM);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002462
2463 if (domain != IB_FLOW_DOMAIN_USER ||
2464 flow_attr->port > MLX5_CAP_GEN(dev->mdev, num_ports) ||
Maor Gottlieb35d190112016-03-07 18:51:47 +02002465 (flow_attr->flags & ~IB_FLOW_ATTR_FLAGS_DONT_TRAP))
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002466 return ERR_PTR(-EINVAL);
2467
2468 dst = kzalloc(sizeof(*dst), GFP_KERNEL);
2469 if (!dst)
2470 return ERR_PTR(-ENOMEM);
2471
2472 mutex_lock(&dev->flow_db.lock);
2473
Maor Gottliebcc0e5d42016-08-28 14:16:34 +03002474 ft_prio = get_flow_table(dev, flow_attr, MLX5_IB_FT_RX);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002475 if (IS_ERR(ft_prio)) {
2476 err = PTR_ERR(ft_prio);
2477 goto unlock;
2478 }
Maor Gottliebcc0e5d42016-08-28 14:16:34 +03002479 if (flow_attr->type == IB_FLOW_ATTR_SNIFFER) {
2480 ft_prio_tx = get_flow_table(dev, flow_attr, MLX5_IB_FT_TX);
2481 if (IS_ERR(ft_prio_tx)) {
2482 err = PTR_ERR(ft_prio_tx);
2483 ft_prio_tx = NULL;
2484 goto destroy_ft;
2485 }
2486 }
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002487
2488 dst->type = MLX5_FLOW_DESTINATION_TYPE_TIR;
Yishai Hadasd9f88e52016-08-28 10:58:37 +03002489 if (mqp->flags & MLX5_IB_QP_RSS)
2490 dst->tir_num = mqp->rss_qp.tirn;
2491 else
2492 dst->tir_num = mqp->raw_packet_qp.rq.tirn;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002493
2494 if (flow_attr->type == IB_FLOW_ATTR_NORMAL) {
Maor Gottlieb35d190112016-03-07 18:51:47 +02002495 if (flow_attr->flags & IB_FLOW_ATTR_FLAGS_DONT_TRAP) {
2496 handler = create_dont_trap_rule(dev, ft_prio,
2497 flow_attr, dst);
2498 } else {
2499 handler = create_flow_rule(dev, ft_prio, flow_attr,
2500 dst);
2501 }
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002502 } else if (flow_attr->type == IB_FLOW_ATTR_ALL_DEFAULT ||
2503 flow_attr->type == IB_FLOW_ATTR_MC_DEFAULT) {
2504 handler = create_leftovers_rule(dev, ft_prio, flow_attr,
2505 dst);
Maor Gottliebcc0e5d42016-08-28 14:16:34 +03002506 } else if (flow_attr->type == IB_FLOW_ATTR_SNIFFER) {
2507 handler = create_sniffer_rule(dev, ft_prio, ft_prio_tx, dst);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002508 } else {
2509 err = -EINVAL;
2510 goto destroy_ft;
2511 }
2512
2513 if (IS_ERR(handler)) {
2514 err = PTR_ERR(handler);
2515 handler = NULL;
2516 goto destroy_ft;
2517 }
2518
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002519 mutex_unlock(&dev->flow_db.lock);
2520 kfree(dst);
2521
2522 return &handler->ibflow;
2523
2524destroy_ft:
2525 put_flow_table(dev, ft_prio, false);
Maor Gottliebcc0e5d42016-08-28 14:16:34 +03002526 if (ft_prio_tx)
2527 put_flow_table(dev, ft_prio_tx, false);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002528unlock:
2529 mutex_unlock(&dev->flow_db.lock);
2530 kfree(dst);
2531 kfree(handler);
2532 return ERR_PTR(err);
2533}
2534
Eli Cohene126ba92013-07-07 17:25:49 +03002535static int mlx5_ib_mcg_attach(struct ib_qp *ibqp, union ib_gid *gid, u16 lid)
2536{
2537 struct mlx5_ib_dev *dev = to_mdev(ibqp->device);
2538 int err;
2539
Jack Morgenstein9603b612014-07-28 23:30:22 +03002540 err = mlx5_core_attach_mcg(dev->mdev, gid, ibqp->qp_num);
Eli Cohene126ba92013-07-07 17:25:49 +03002541 if (err)
2542 mlx5_ib_warn(dev, "failed attaching QPN 0x%x, MGID %pI6\n",
2543 ibqp->qp_num, gid->raw);
2544
2545 return err;
2546}
2547
2548static int mlx5_ib_mcg_detach(struct ib_qp *ibqp, union ib_gid *gid, u16 lid)
2549{
2550 struct mlx5_ib_dev *dev = to_mdev(ibqp->device);
2551 int err;
2552
Jack Morgenstein9603b612014-07-28 23:30:22 +03002553 err = mlx5_core_detach_mcg(dev->mdev, gid, ibqp->qp_num);
Eli Cohene126ba92013-07-07 17:25:49 +03002554 if (err)
2555 mlx5_ib_warn(dev, "failed detaching QPN 0x%x, MGID %pI6\n",
2556 ibqp->qp_num, gid->raw);
2557
2558 return err;
2559}
2560
2561static int init_node_data(struct mlx5_ib_dev *dev)
2562{
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03002563 int err;
Eli Cohene126ba92013-07-07 17:25:49 +03002564
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03002565 err = mlx5_query_node_desc(dev, dev->ib_dev.node_desc);
Eli Cohene126ba92013-07-07 17:25:49 +03002566 if (err)
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03002567 return err;
Eli Cohene126ba92013-07-07 17:25:49 +03002568
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03002569 dev->mdev->rev_id = dev->mdev->pdev->revision;
Eli Cohene126ba92013-07-07 17:25:49 +03002570
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03002571 return mlx5_query_node_guid(dev, &dev->ib_dev.node_guid);
Eli Cohene126ba92013-07-07 17:25:49 +03002572}
2573
2574static ssize_t show_fw_pages(struct device *device, struct device_attribute *attr,
2575 char *buf)
2576{
2577 struct mlx5_ib_dev *dev =
2578 container_of(device, struct mlx5_ib_dev, ib_dev.dev);
2579
Jack Morgenstein9603b612014-07-28 23:30:22 +03002580 return sprintf(buf, "%d\n", dev->mdev->priv.fw_pages);
Eli Cohene126ba92013-07-07 17:25:49 +03002581}
2582
2583static ssize_t show_reg_pages(struct device *device,
2584 struct device_attribute *attr, char *buf)
2585{
2586 struct mlx5_ib_dev *dev =
2587 container_of(device, struct mlx5_ib_dev, ib_dev.dev);
2588
Haggai Eran6aec21f2014-12-11 17:04:23 +02002589 return sprintf(buf, "%d\n", atomic_read(&dev->mdev->priv.reg_pages));
Eli Cohene126ba92013-07-07 17:25:49 +03002590}
2591
2592static ssize_t show_hca(struct device *device, struct device_attribute *attr,
2593 char *buf)
2594{
2595 struct mlx5_ib_dev *dev =
2596 container_of(device, struct mlx5_ib_dev, ib_dev.dev);
Jack Morgenstein9603b612014-07-28 23:30:22 +03002597 return sprintf(buf, "MT%d\n", dev->mdev->pdev->device);
Eli Cohene126ba92013-07-07 17:25:49 +03002598}
2599
Eli Cohene126ba92013-07-07 17:25:49 +03002600static ssize_t show_rev(struct device *device, struct device_attribute *attr,
2601 char *buf)
2602{
2603 struct mlx5_ib_dev *dev =
2604 container_of(device, struct mlx5_ib_dev, ib_dev.dev);
Jack Morgenstein9603b612014-07-28 23:30:22 +03002605 return sprintf(buf, "%x\n", dev->mdev->rev_id);
Eli Cohene126ba92013-07-07 17:25:49 +03002606}
2607
2608static ssize_t show_board(struct device *device, struct device_attribute *attr,
2609 char *buf)
2610{
2611 struct mlx5_ib_dev *dev =
2612 container_of(device, struct mlx5_ib_dev, ib_dev.dev);
2613 return sprintf(buf, "%.*s\n", MLX5_BOARD_ID_LEN,
Jack Morgenstein9603b612014-07-28 23:30:22 +03002614 dev->mdev->board_id);
Eli Cohene126ba92013-07-07 17:25:49 +03002615}
2616
2617static DEVICE_ATTR(hw_rev, S_IRUGO, show_rev, NULL);
Eli Cohene126ba92013-07-07 17:25:49 +03002618static DEVICE_ATTR(hca_type, S_IRUGO, show_hca, NULL);
2619static DEVICE_ATTR(board_id, S_IRUGO, show_board, NULL);
2620static DEVICE_ATTR(fw_pages, S_IRUGO, show_fw_pages, NULL);
2621static DEVICE_ATTR(reg_pages, S_IRUGO, show_reg_pages, NULL);
2622
2623static struct device_attribute *mlx5_class_attributes[] = {
2624 &dev_attr_hw_rev,
Eli Cohene126ba92013-07-07 17:25:49 +03002625 &dev_attr_hca_type,
2626 &dev_attr_board_id,
2627 &dev_attr_fw_pages,
2628 &dev_attr_reg_pages,
2629};
2630
Haggai Eran7722f472016-02-29 15:45:07 +02002631static void pkey_change_handler(struct work_struct *work)
2632{
2633 struct mlx5_ib_port_resources *ports =
2634 container_of(work, struct mlx5_ib_port_resources,
2635 pkey_change_work);
2636
2637 mutex_lock(&ports->devr->mutex);
2638 mlx5_ib_gsi_pkey_change(ports->gsi);
2639 mutex_unlock(&ports->devr->mutex);
2640}
2641
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03002642static void mlx5_ib_handle_internal_error(struct mlx5_ib_dev *ibdev)
2643{
2644 struct mlx5_ib_qp *mqp;
2645 struct mlx5_ib_cq *send_mcq, *recv_mcq;
2646 struct mlx5_core_cq *mcq;
2647 struct list_head cq_armed_list;
2648 unsigned long flags_qp;
2649 unsigned long flags_cq;
2650 unsigned long flags;
2651
2652 INIT_LIST_HEAD(&cq_armed_list);
2653
2654 /* Go over qp list reside on that ibdev, sync with create/destroy qp.*/
2655 spin_lock_irqsave(&ibdev->reset_flow_resource_lock, flags);
2656 list_for_each_entry(mqp, &ibdev->qp_list, qps_list) {
2657 spin_lock_irqsave(&mqp->sq.lock, flags_qp);
2658 if (mqp->sq.tail != mqp->sq.head) {
2659 send_mcq = to_mcq(mqp->ibqp.send_cq);
2660 spin_lock_irqsave(&send_mcq->lock, flags_cq);
2661 if (send_mcq->mcq.comp &&
2662 mqp->ibqp.send_cq->comp_handler) {
2663 if (!send_mcq->mcq.reset_notify_added) {
2664 send_mcq->mcq.reset_notify_added = 1;
2665 list_add_tail(&send_mcq->mcq.reset_notify,
2666 &cq_armed_list);
2667 }
2668 }
2669 spin_unlock_irqrestore(&send_mcq->lock, flags_cq);
2670 }
2671 spin_unlock_irqrestore(&mqp->sq.lock, flags_qp);
2672 spin_lock_irqsave(&mqp->rq.lock, flags_qp);
2673 /* no handling is needed for SRQ */
2674 if (!mqp->ibqp.srq) {
2675 if (mqp->rq.tail != mqp->rq.head) {
2676 recv_mcq = to_mcq(mqp->ibqp.recv_cq);
2677 spin_lock_irqsave(&recv_mcq->lock, flags_cq);
2678 if (recv_mcq->mcq.comp &&
2679 mqp->ibqp.recv_cq->comp_handler) {
2680 if (!recv_mcq->mcq.reset_notify_added) {
2681 recv_mcq->mcq.reset_notify_added = 1;
2682 list_add_tail(&recv_mcq->mcq.reset_notify,
2683 &cq_armed_list);
2684 }
2685 }
2686 spin_unlock_irqrestore(&recv_mcq->lock,
2687 flags_cq);
2688 }
2689 }
2690 spin_unlock_irqrestore(&mqp->rq.lock, flags_qp);
2691 }
2692 /*At that point all inflight post send were put to be executed as of we
2693 * lock/unlock above locks Now need to arm all involved CQs.
2694 */
2695 list_for_each_entry(mcq, &cq_armed_list, reset_notify) {
2696 mcq->comp(mcq);
2697 }
2698 spin_unlock_irqrestore(&ibdev->reset_flow_resource_lock, flags);
2699}
2700
Jack Morgenstein9603b612014-07-28 23:30:22 +03002701static void mlx5_ib_event(struct mlx5_core_dev *dev, void *context,
Jack Morgenstein4d2f9bb2014-07-28 23:30:24 +03002702 enum mlx5_dev_event event, unsigned long param)
Eli Cohene126ba92013-07-07 17:25:49 +03002703{
Jack Morgenstein9603b612014-07-28 23:30:22 +03002704 struct mlx5_ib_dev *ibdev = (struct mlx5_ib_dev *)context;
Eli Cohene126ba92013-07-07 17:25:49 +03002705 struct ib_event ibev;
Eli Cohendbaaff22016-10-27 16:36:44 +03002706 bool fatal = false;
Eli Cohene126ba92013-07-07 17:25:49 +03002707 u8 port = 0;
2708
2709 switch (event) {
2710 case MLX5_DEV_EVENT_SYS_ERROR:
Eli Cohene126ba92013-07-07 17:25:49 +03002711 ibev.event = IB_EVENT_DEVICE_FATAL;
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03002712 mlx5_ib_handle_internal_error(ibdev);
Eli Cohendbaaff22016-10-27 16:36:44 +03002713 fatal = true;
Eli Cohene126ba92013-07-07 17:25:49 +03002714 break;
2715
2716 case MLX5_DEV_EVENT_PORT_UP:
Eli Cohene126ba92013-07-07 17:25:49 +03002717 case MLX5_DEV_EVENT_PORT_DOWN:
Noa Osherovich2788cf32016-06-04 15:15:29 +03002718 case MLX5_DEV_EVENT_PORT_INITIALIZED:
Jack Morgenstein4d2f9bb2014-07-28 23:30:24 +03002719 port = (u8)param;
Aviv Heller5ec8c832016-09-18 20:48:00 +03002720
2721 /* In RoCE, port up/down events are handled in
2722 * mlx5_netdev_event().
2723 */
2724 if (mlx5_ib_port_link_layer(&ibdev->ib_dev, port) ==
2725 IB_LINK_LAYER_ETHERNET)
2726 return;
2727
2728 ibev.event = (event == MLX5_DEV_EVENT_PORT_UP) ?
2729 IB_EVENT_PORT_ACTIVE : IB_EVENT_PORT_ERR;
Eli Cohene126ba92013-07-07 17:25:49 +03002730 break;
2731
Eli Cohene126ba92013-07-07 17:25:49 +03002732 case MLX5_DEV_EVENT_LID_CHANGE:
2733 ibev.event = IB_EVENT_LID_CHANGE;
Jack Morgenstein4d2f9bb2014-07-28 23:30:24 +03002734 port = (u8)param;
Eli Cohene126ba92013-07-07 17:25:49 +03002735 break;
2736
2737 case MLX5_DEV_EVENT_PKEY_CHANGE:
2738 ibev.event = IB_EVENT_PKEY_CHANGE;
Jack Morgenstein4d2f9bb2014-07-28 23:30:24 +03002739 port = (u8)param;
Haggai Eran7722f472016-02-29 15:45:07 +02002740
2741 schedule_work(&ibdev->devr.ports[port - 1].pkey_change_work);
Eli Cohene126ba92013-07-07 17:25:49 +03002742 break;
2743
2744 case MLX5_DEV_EVENT_GUID_CHANGE:
2745 ibev.event = IB_EVENT_GID_CHANGE;
Jack Morgenstein4d2f9bb2014-07-28 23:30:24 +03002746 port = (u8)param;
Eli Cohene126ba92013-07-07 17:25:49 +03002747 break;
2748
2749 case MLX5_DEV_EVENT_CLIENT_REREG:
2750 ibev.event = IB_EVENT_CLIENT_REREGISTER;
Jack Morgenstein4d2f9bb2014-07-28 23:30:24 +03002751 port = (u8)param;
Eli Cohene126ba92013-07-07 17:25:49 +03002752 break;
Saeed Mahameedbdc37922016-09-29 19:35:38 +03002753 default:
2754 return;
Eli Cohene126ba92013-07-07 17:25:49 +03002755 }
2756
2757 ibev.device = &ibdev->ib_dev;
2758 ibev.element.port_num = port;
2759
Eli Cohena0c84c32013-09-11 16:35:27 +03002760 if (port < 1 || port > ibdev->num_ports) {
2761 mlx5_ib_warn(ibdev, "warning: event on port %d\n", port);
2762 return;
2763 }
2764
Eli Cohene126ba92013-07-07 17:25:49 +03002765 if (ibdev->ib_active)
2766 ib_dispatch_event(&ibev);
Eli Cohendbaaff22016-10-27 16:36:44 +03002767
2768 if (fatal)
2769 ibdev->ib_active = false;
Eli Cohene126ba92013-07-07 17:25:49 +03002770}
2771
Maor Gottliebc43f1112017-01-18 14:10:33 +02002772static int set_has_smi_cap(struct mlx5_ib_dev *dev)
2773{
2774 struct mlx5_hca_vport_context vport_ctx;
2775 int err;
2776 int port;
2777
2778 for (port = 1; port <= MLX5_CAP_GEN(dev->mdev, num_ports); port++) {
2779 dev->mdev->port_caps[port - 1].has_smi = false;
2780 if (MLX5_CAP_GEN(dev->mdev, port_type) ==
2781 MLX5_CAP_PORT_TYPE_IB) {
2782 if (MLX5_CAP_GEN(dev->mdev, ib_virt)) {
2783 err = mlx5_query_hca_vport_context(dev->mdev, 0,
2784 port, 0,
2785 &vport_ctx);
2786 if (err) {
2787 mlx5_ib_err(dev, "query_hca_vport_context for port=%d failed %d\n",
2788 port, err);
2789 return err;
2790 }
2791 dev->mdev->port_caps[port - 1].has_smi =
2792 vport_ctx.has_smi;
2793 } else {
2794 dev->mdev->port_caps[port - 1].has_smi = true;
2795 }
2796 }
2797 }
2798 return 0;
2799}
2800
Eli Cohene126ba92013-07-07 17:25:49 +03002801static void get_ext_port_caps(struct mlx5_ib_dev *dev)
2802{
2803 int port;
2804
Saeed Mahameed938fe832015-05-28 22:28:41 +03002805 for (port = 1; port <= MLX5_CAP_GEN(dev->mdev, num_ports); port++)
Eli Cohene126ba92013-07-07 17:25:49 +03002806 mlx5_query_ext_port_caps(dev, port);
2807}
2808
2809static int get_port_caps(struct mlx5_ib_dev *dev)
2810{
2811 struct ib_device_attr *dprops = NULL;
2812 struct ib_port_attr *pprops = NULL;
Dan Carpenterf614fc12015-01-12 11:56:58 +03002813 int err = -ENOMEM;
Eli Cohene126ba92013-07-07 17:25:49 +03002814 int port;
Matan Barak2528e332015-06-11 16:35:25 +03002815 struct ib_udata uhw = {.inlen = 0, .outlen = 0};
Eli Cohene126ba92013-07-07 17:25:49 +03002816
2817 pprops = kmalloc(sizeof(*pprops), GFP_KERNEL);
2818 if (!pprops)
2819 goto out;
2820
2821 dprops = kmalloc(sizeof(*dprops), GFP_KERNEL);
2822 if (!dprops)
2823 goto out;
2824
Maor Gottliebc43f1112017-01-18 14:10:33 +02002825 err = set_has_smi_cap(dev);
2826 if (err)
2827 goto out;
2828
Matan Barak2528e332015-06-11 16:35:25 +03002829 err = mlx5_ib_query_device(&dev->ib_dev, dprops, &uhw);
Eli Cohene126ba92013-07-07 17:25:49 +03002830 if (err) {
2831 mlx5_ib_warn(dev, "query_device failed %d\n", err);
2832 goto out;
2833 }
2834
Saeed Mahameed938fe832015-05-28 22:28:41 +03002835 for (port = 1; port <= MLX5_CAP_GEN(dev->mdev, num_ports); port++) {
Or Gerlitzc4550c62017-01-24 13:02:39 +02002836 memset(pprops, 0, sizeof(*pprops));
Eli Cohene126ba92013-07-07 17:25:49 +03002837 err = mlx5_ib_query_port(&dev->ib_dev, port, pprops);
2838 if (err) {
Saeed Mahameed938fe832015-05-28 22:28:41 +03002839 mlx5_ib_warn(dev, "query_port %d failed %d\n",
2840 port, err);
Eli Cohene126ba92013-07-07 17:25:49 +03002841 break;
2842 }
Saeed Mahameed938fe832015-05-28 22:28:41 +03002843 dev->mdev->port_caps[port - 1].pkey_table_len =
2844 dprops->max_pkeys;
2845 dev->mdev->port_caps[port - 1].gid_table_len =
2846 pprops->gid_tbl_len;
Eli Cohene126ba92013-07-07 17:25:49 +03002847 mlx5_ib_dbg(dev, "pkey_table_len %d, gid_table_len %d\n",
2848 dprops->max_pkeys, pprops->gid_tbl_len);
2849 }
2850
2851out:
2852 kfree(pprops);
2853 kfree(dprops);
2854
2855 return err;
2856}
2857
2858static void destroy_umrc_res(struct mlx5_ib_dev *dev)
2859{
2860 int err;
2861
2862 err = mlx5_mr_cache_cleanup(dev);
2863 if (err)
2864 mlx5_ib_warn(dev, "mr cache cleanup failed\n");
2865
2866 mlx5_ib_destroy_qp(dev->umrc.qp);
Christoph Hellwigadd08d72016-03-03 09:38:22 +01002867 ib_free_cq(dev->umrc.cq);
Eli Cohene126ba92013-07-07 17:25:49 +03002868 ib_dealloc_pd(dev->umrc.pd);
2869}
2870
2871enum {
2872 MAX_UMR_WR = 128,
2873};
2874
2875static int create_umr_res(struct mlx5_ib_dev *dev)
2876{
2877 struct ib_qp_init_attr *init_attr = NULL;
2878 struct ib_qp_attr *attr = NULL;
2879 struct ib_pd *pd;
2880 struct ib_cq *cq;
2881 struct ib_qp *qp;
Eli Cohene126ba92013-07-07 17:25:49 +03002882 int ret;
2883
2884 attr = kzalloc(sizeof(*attr), GFP_KERNEL);
2885 init_attr = kzalloc(sizeof(*init_attr), GFP_KERNEL);
2886 if (!attr || !init_attr) {
2887 ret = -ENOMEM;
2888 goto error_0;
2889 }
2890
Christoph Hellwiged082d32016-09-05 12:56:17 +02002891 pd = ib_alloc_pd(&dev->ib_dev, 0);
Eli Cohene126ba92013-07-07 17:25:49 +03002892 if (IS_ERR(pd)) {
2893 mlx5_ib_dbg(dev, "Couldn't create PD for sync UMR QP\n");
2894 ret = PTR_ERR(pd);
2895 goto error_0;
2896 }
2897
Christoph Hellwigadd08d72016-03-03 09:38:22 +01002898 cq = ib_alloc_cq(&dev->ib_dev, NULL, 128, 0, IB_POLL_SOFTIRQ);
Eli Cohene126ba92013-07-07 17:25:49 +03002899 if (IS_ERR(cq)) {
2900 mlx5_ib_dbg(dev, "Couldn't create CQ for sync UMR QP\n");
2901 ret = PTR_ERR(cq);
2902 goto error_2;
2903 }
Eli Cohene126ba92013-07-07 17:25:49 +03002904
2905 init_attr->send_cq = cq;
2906 init_attr->recv_cq = cq;
2907 init_attr->sq_sig_type = IB_SIGNAL_ALL_WR;
2908 init_attr->cap.max_send_wr = MAX_UMR_WR;
2909 init_attr->cap.max_send_sge = 1;
2910 init_attr->qp_type = MLX5_IB_QPT_REG_UMR;
2911 init_attr->port_num = 1;
2912 qp = mlx5_ib_create_qp(pd, init_attr, NULL);
2913 if (IS_ERR(qp)) {
2914 mlx5_ib_dbg(dev, "Couldn't create sync UMR QP\n");
2915 ret = PTR_ERR(qp);
2916 goto error_3;
2917 }
2918 qp->device = &dev->ib_dev;
2919 qp->real_qp = qp;
2920 qp->uobject = NULL;
2921 qp->qp_type = MLX5_IB_QPT_REG_UMR;
2922
2923 attr->qp_state = IB_QPS_INIT;
2924 attr->port_num = 1;
2925 ret = mlx5_ib_modify_qp(qp, attr, IB_QP_STATE | IB_QP_PKEY_INDEX |
2926 IB_QP_PORT, NULL);
2927 if (ret) {
2928 mlx5_ib_dbg(dev, "Couldn't modify UMR QP\n");
2929 goto error_4;
2930 }
2931
2932 memset(attr, 0, sizeof(*attr));
2933 attr->qp_state = IB_QPS_RTR;
2934 attr->path_mtu = IB_MTU_256;
2935
2936 ret = mlx5_ib_modify_qp(qp, attr, IB_QP_STATE, NULL);
2937 if (ret) {
2938 mlx5_ib_dbg(dev, "Couldn't modify umr QP to rtr\n");
2939 goto error_4;
2940 }
2941
2942 memset(attr, 0, sizeof(*attr));
2943 attr->qp_state = IB_QPS_RTS;
2944 ret = mlx5_ib_modify_qp(qp, attr, IB_QP_STATE, NULL);
2945 if (ret) {
2946 mlx5_ib_dbg(dev, "Couldn't modify umr QP to rts\n");
2947 goto error_4;
2948 }
2949
2950 dev->umrc.qp = qp;
2951 dev->umrc.cq = cq;
Eli Cohene126ba92013-07-07 17:25:49 +03002952 dev->umrc.pd = pd;
2953
2954 sema_init(&dev->umrc.sem, MAX_UMR_WR);
2955 ret = mlx5_mr_cache_init(dev);
2956 if (ret) {
2957 mlx5_ib_warn(dev, "mr cache init failed %d\n", ret);
2958 goto error_4;
2959 }
2960
2961 kfree(attr);
2962 kfree(init_attr);
2963
2964 return 0;
2965
2966error_4:
2967 mlx5_ib_destroy_qp(qp);
2968
2969error_3:
Christoph Hellwigadd08d72016-03-03 09:38:22 +01002970 ib_free_cq(cq);
Eli Cohene126ba92013-07-07 17:25:49 +03002971
2972error_2:
Eli Cohene126ba92013-07-07 17:25:49 +03002973 ib_dealloc_pd(pd);
2974
2975error_0:
2976 kfree(attr);
2977 kfree(init_attr);
2978 return ret;
2979}
2980
Max Gurtovoy6e8484c2017-05-28 10:53:11 +03002981static u8 mlx5_get_umr_fence(u8 umr_fence_cap)
2982{
2983 switch (umr_fence_cap) {
2984 case MLX5_CAP_UMR_FENCE_NONE:
2985 return MLX5_FENCE_MODE_NONE;
2986 case MLX5_CAP_UMR_FENCE_SMALL:
2987 return MLX5_FENCE_MODE_INITIATOR_SMALL;
2988 default:
2989 return MLX5_FENCE_MODE_STRONG_ORDERING;
2990 }
2991}
2992
Eli Cohene126ba92013-07-07 17:25:49 +03002993static int create_dev_resources(struct mlx5_ib_resources *devr)
2994{
2995 struct ib_srq_init_attr attr;
2996 struct mlx5_ib_dev *dev;
Matan Barakbcf4c1e2015-06-11 16:35:20 +03002997 struct ib_cq_init_attr cq_attr = {.cqe = 1};
Haggai Eran7722f472016-02-29 15:45:07 +02002998 int port;
Eli Cohene126ba92013-07-07 17:25:49 +03002999 int ret = 0;
3000
3001 dev = container_of(devr, struct mlx5_ib_dev, devr);
3002
Haggai Erand16e91d2016-02-29 15:45:05 +02003003 mutex_init(&devr->mutex);
3004
Eli Cohene126ba92013-07-07 17:25:49 +03003005 devr->p0 = mlx5_ib_alloc_pd(&dev->ib_dev, NULL, NULL);
3006 if (IS_ERR(devr->p0)) {
3007 ret = PTR_ERR(devr->p0);
3008 goto error0;
3009 }
3010 devr->p0->device = &dev->ib_dev;
3011 devr->p0->uobject = NULL;
3012 atomic_set(&devr->p0->usecnt, 0);
3013
Matan Barakbcf4c1e2015-06-11 16:35:20 +03003014 devr->c0 = mlx5_ib_create_cq(&dev->ib_dev, &cq_attr, NULL, NULL);
Eli Cohene126ba92013-07-07 17:25:49 +03003015 if (IS_ERR(devr->c0)) {
3016 ret = PTR_ERR(devr->c0);
3017 goto error1;
3018 }
3019 devr->c0->device = &dev->ib_dev;
3020 devr->c0->uobject = NULL;
3021 devr->c0->comp_handler = NULL;
3022 devr->c0->event_handler = NULL;
3023 devr->c0->cq_context = NULL;
3024 atomic_set(&devr->c0->usecnt, 0);
3025
3026 devr->x0 = mlx5_ib_alloc_xrcd(&dev->ib_dev, NULL, NULL);
3027 if (IS_ERR(devr->x0)) {
3028 ret = PTR_ERR(devr->x0);
3029 goto error2;
3030 }
3031 devr->x0->device = &dev->ib_dev;
3032 devr->x0->inode = NULL;
3033 atomic_set(&devr->x0->usecnt, 0);
3034 mutex_init(&devr->x0->tgt_qp_mutex);
3035 INIT_LIST_HEAD(&devr->x0->tgt_qp_list);
3036
3037 devr->x1 = mlx5_ib_alloc_xrcd(&dev->ib_dev, NULL, NULL);
3038 if (IS_ERR(devr->x1)) {
3039 ret = PTR_ERR(devr->x1);
3040 goto error3;
3041 }
3042 devr->x1->device = &dev->ib_dev;
3043 devr->x1->inode = NULL;
3044 atomic_set(&devr->x1->usecnt, 0);
3045 mutex_init(&devr->x1->tgt_qp_mutex);
3046 INIT_LIST_HEAD(&devr->x1->tgt_qp_list);
3047
3048 memset(&attr, 0, sizeof(attr));
3049 attr.attr.max_sge = 1;
3050 attr.attr.max_wr = 1;
3051 attr.srq_type = IB_SRQT_XRC;
3052 attr.ext.xrc.cq = devr->c0;
3053 attr.ext.xrc.xrcd = devr->x0;
3054
3055 devr->s0 = mlx5_ib_create_srq(devr->p0, &attr, NULL);
3056 if (IS_ERR(devr->s0)) {
3057 ret = PTR_ERR(devr->s0);
3058 goto error4;
3059 }
3060 devr->s0->device = &dev->ib_dev;
3061 devr->s0->pd = devr->p0;
3062 devr->s0->uobject = NULL;
3063 devr->s0->event_handler = NULL;
3064 devr->s0->srq_context = NULL;
3065 devr->s0->srq_type = IB_SRQT_XRC;
3066 devr->s0->ext.xrc.xrcd = devr->x0;
3067 devr->s0->ext.xrc.cq = devr->c0;
3068 atomic_inc(&devr->s0->ext.xrc.xrcd->usecnt);
3069 atomic_inc(&devr->s0->ext.xrc.cq->usecnt);
3070 atomic_inc(&devr->p0->usecnt);
3071 atomic_set(&devr->s0->usecnt, 0);
3072
Haggai Abramonvsky4aa17b22015-06-04 19:30:48 +03003073 memset(&attr, 0, sizeof(attr));
3074 attr.attr.max_sge = 1;
3075 attr.attr.max_wr = 1;
3076 attr.srq_type = IB_SRQT_BASIC;
3077 devr->s1 = mlx5_ib_create_srq(devr->p0, &attr, NULL);
3078 if (IS_ERR(devr->s1)) {
3079 ret = PTR_ERR(devr->s1);
3080 goto error5;
3081 }
3082 devr->s1->device = &dev->ib_dev;
3083 devr->s1->pd = devr->p0;
3084 devr->s1->uobject = NULL;
3085 devr->s1->event_handler = NULL;
3086 devr->s1->srq_context = NULL;
3087 devr->s1->srq_type = IB_SRQT_BASIC;
3088 devr->s1->ext.xrc.cq = devr->c0;
3089 atomic_inc(&devr->p0->usecnt);
3090 atomic_set(&devr->s0->usecnt, 0);
3091
Haggai Eran7722f472016-02-29 15:45:07 +02003092 for (port = 0; port < ARRAY_SIZE(devr->ports); ++port) {
3093 INIT_WORK(&devr->ports[port].pkey_change_work,
3094 pkey_change_handler);
3095 devr->ports[port].devr = devr;
3096 }
3097
Eli Cohene126ba92013-07-07 17:25:49 +03003098 return 0;
3099
Haggai Abramonvsky4aa17b22015-06-04 19:30:48 +03003100error5:
3101 mlx5_ib_destroy_srq(devr->s0);
Eli Cohene126ba92013-07-07 17:25:49 +03003102error4:
3103 mlx5_ib_dealloc_xrcd(devr->x1);
3104error3:
3105 mlx5_ib_dealloc_xrcd(devr->x0);
3106error2:
3107 mlx5_ib_destroy_cq(devr->c0);
3108error1:
3109 mlx5_ib_dealloc_pd(devr->p0);
3110error0:
3111 return ret;
3112}
3113
3114static void destroy_dev_resources(struct mlx5_ib_resources *devr)
3115{
Haggai Eran7722f472016-02-29 15:45:07 +02003116 struct mlx5_ib_dev *dev =
3117 container_of(devr, struct mlx5_ib_dev, devr);
3118 int port;
3119
Haggai Abramonvsky4aa17b22015-06-04 19:30:48 +03003120 mlx5_ib_destroy_srq(devr->s1);
Eli Cohene126ba92013-07-07 17:25:49 +03003121 mlx5_ib_destroy_srq(devr->s0);
3122 mlx5_ib_dealloc_xrcd(devr->x0);
3123 mlx5_ib_dealloc_xrcd(devr->x1);
3124 mlx5_ib_destroy_cq(devr->c0);
3125 mlx5_ib_dealloc_pd(devr->p0);
Haggai Eran7722f472016-02-29 15:45:07 +02003126
3127 /* Make sure no change P_Key work items are still executing */
3128 for (port = 0; port < dev->num_ports; ++port)
3129 cancel_work_sync(&devr->ports[port].pkey_change_work);
Eli Cohene126ba92013-07-07 17:25:49 +03003130}
3131
Achiad Shochate53505a2015-12-23 18:47:25 +02003132static u32 get_core_cap_flags(struct ib_device *ibdev)
3133{
3134 struct mlx5_ib_dev *dev = to_mdev(ibdev);
3135 enum rdma_link_layer ll = mlx5_ib_port_link_layer(ibdev, 1);
3136 u8 l3_type_cap = MLX5_CAP_ROCE(dev->mdev, l3_type);
3137 u8 roce_version_cap = MLX5_CAP_ROCE(dev->mdev, roce_version);
3138 u32 ret = 0;
3139
3140 if (ll == IB_LINK_LAYER_INFINIBAND)
3141 return RDMA_CORE_PORT_IBA_IB;
3142
Or Gerlitz72cd5712017-01-24 13:02:36 +02003143 ret = RDMA_CORE_PORT_RAW_PACKET;
3144
Achiad Shochate53505a2015-12-23 18:47:25 +02003145 if (!(l3_type_cap & MLX5_ROCE_L3_TYPE_IPV4_CAP))
Or Gerlitz72cd5712017-01-24 13:02:36 +02003146 return ret;
Achiad Shochate53505a2015-12-23 18:47:25 +02003147
3148 if (!(l3_type_cap & MLX5_ROCE_L3_TYPE_IPV6_CAP))
Or Gerlitz72cd5712017-01-24 13:02:36 +02003149 return ret;
Achiad Shochate53505a2015-12-23 18:47:25 +02003150
3151 if (roce_version_cap & MLX5_ROCE_VERSION_1_CAP)
3152 ret |= RDMA_CORE_PORT_IBA_ROCE;
3153
3154 if (roce_version_cap & MLX5_ROCE_VERSION_2_CAP)
3155 ret |= RDMA_CORE_PORT_IBA_ROCE_UDP_ENCAP;
3156
3157 return ret;
3158}
3159
Ira Weiny77386132015-05-13 20:02:58 -04003160static int mlx5_port_immutable(struct ib_device *ibdev, u8 port_num,
3161 struct ib_port_immutable *immutable)
3162{
3163 struct ib_port_attr attr;
Or Gerlitzca5b91d2016-11-27 16:51:36 +02003164 struct mlx5_ib_dev *dev = to_mdev(ibdev);
3165 enum rdma_link_layer ll = mlx5_ib_port_link_layer(ibdev, port_num);
Ira Weiny77386132015-05-13 20:02:58 -04003166 int err;
3167
Or Gerlitzc4550c62017-01-24 13:02:39 +02003168 immutable->core_cap_flags = get_core_cap_flags(ibdev);
3169
3170 err = ib_query_port(ibdev, port_num, &attr);
Ira Weiny77386132015-05-13 20:02:58 -04003171 if (err)
3172 return err;
3173
3174 immutable->pkey_tbl_len = attr.pkey_tbl_len;
3175 immutable->gid_tbl_len = attr.gid_tbl_len;
Achiad Shochate53505a2015-12-23 18:47:25 +02003176 immutable->core_cap_flags = get_core_cap_flags(ibdev);
Or Gerlitzca5b91d2016-11-27 16:51:36 +02003177 if ((ll == IB_LINK_LAYER_INFINIBAND) || MLX5_CAP_GEN(dev->mdev, roce))
3178 immutable->max_mad_size = IB_MGMT_MAD_SIZE;
Ira Weiny77386132015-05-13 20:02:58 -04003179
3180 return 0;
3181}
3182
Ira Weinyc7342822016-06-15 02:22:01 -04003183static void get_dev_fw_str(struct ib_device *ibdev, char *str,
3184 size_t str_len)
3185{
3186 struct mlx5_ib_dev *dev =
3187 container_of(ibdev, struct mlx5_ib_dev, ib_dev);
3188 snprintf(str, str_len, "%d.%d.%04d", fw_rev_maj(dev->mdev),
3189 fw_rev_min(dev->mdev), fw_rev_sub(dev->mdev));
3190}
3191
Or Gerlitz45f95ac2016-11-27 16:51:35 +02003192static int mlx5_eth_lag_init(struct mlx5_ib_dev *dev)
Aviv Heller9ef9c642016-09-18 20:48:01 +03003193{
3194 struct mlx5_core_dev *mdev = dev->mdev;
3195 struct mlx5_flow_namespace *ns = mlx5_get_flow_namespace(mdev,
3196 MLX5_FLOW_NAMESPACE_LAG);
3197 struct mlx5_flow_table *ft;
3198 int err;
3199
3200 if (!ns || !mlx5_lag_is_active(mdev))
3201 return 0;
3202
3203 err = mlx5_cmd_create_vport_lag(mdev);
3204 if (err)
3205 return err;
3206
3207 ft = mlx5_create_lag_demux_flow_table(ns, 0, 0);
3208 if (IS_ERR(ft)) {
3209 err = PTR_ERR(ft);
3210 goto err_destroy_vport_lag;
3211 }
3212
3213 dev->flow_db.lag_demux_ft = ft;
3214 return 0;
3215
3216err_destroy_vport_lag:
3217 mlx5_cmd_destroy_vport_lag(mdev);
3218 return err;
3219}
3220
Or Gerlitz45f95ac2016-11-27 16:51:35 +02003221static void mlx5_eth_lag_cleanup(struct mlx5_ib_dev *dev)
Aviv Heller9ef9c642016-09-18 20:48:01 +03003222{
3223 struct mlx5_core_dev *mdev = dev->mdev;
3224
3225 if (dev->flow_db.lag_demux_ft) {
3226 mlx5_destroy_flow_table(dev->flow_db.lag_demux_ft);
3227 dev->flow_db.lag_demux_ft = NULL;
3228
3229 mlx5_cmd_destroy_vport_lag(mdev);
3230 }
3231}
3232
Or Gerlitzd012f5d2016-11-27 16:51:34 +02003233static int mlx5_add_netdev_notifier(struct mlx5_ib_dev *dev)
Achiad Shochatfc24fc52015-12-23 18:47:17 +02003234{
Achiad Shochate53505a2015-12-23 18:47:25 +02003235 int err;
3236
Achiad Shochatfc24fc52015-12-23 18:47:17 +02003237 dev->roce.nb.notifier_call = mlx5_netdev_event;
Achiad Shochate53505a2015-12-23 18:47:25 +02003238 err = register_netdevice_notifier(&dev->roce.nb);
Aviv Heller5ec8c832016-09-18 20:48:00 +03003239 if (err) {
3240 dev->roce.nb.notifier_call = NULL;
Achiad Shochate53505a2015-12-23 18:47:25 +02003241 return err;
Aviv Heller5ec8c832016-09-18 20:48:00 +03003242 }
Achiad Shochate53505a2015-12-23 18:47:25 +02003243
Or Gerlitzd012f5d2016-11-27 16:51:34 +02003244 return 0;
3245}
Achiad Shochate53505a2015-12-23 18:47:25 +02003246
Or Gerlitzd012f5d2016-11-27 16:51:34 +02003247static void mlx5_remove_netdev_notifier(struct mlx5_ib_dev *dev)
Eli Cohene126ba92013-07-07 17:25:49 +03003248{
3249 if (dev->roce.nb.notifier_call) {
3250 unregister_netdevice_notifier(&dev->roce.nb);
3251 dev->roce.nb.notifier_call = NULL;
3252 }
3253}
3254
Or Gerlitz45f95ac2016-11-27 16:51:35 +02003255static int mlx5_enable_eth(struct mlx5_ib_dev *dev)
Eli Cohene126ba92013-07-07 17:25:49 +03003256{
Eli Cohene126ba92013-07-07 17:25:49 +03003257 int err;
3258
Or Gerlitzd012f5d2016-11-27 16:51:34 +02003259 err = mlx5_add_netdev_notifier(dev);
3260 if (err)
Achiad Shochate53505a2015-12-23 18:47:25 +02003261 return err;
Achiad Shochate53505a2015-12-23 18:47:25 +02003262
Or Gerlitzca5b91d2016-11-27 16:51:36 +02003263 if (MLX5_CAP_GEN(dev->mdev, roce)) {
3264 err = mlx5_nic_vport_enable_roce(dev->mdev);
3265 if (err)
3266 goto err_unregister_netdevice_notifier;
3267 }
Achiad Shochate53505a2015-12-23 18:47:25 +02003268
Or Gerlitz45f95ac2016-11-27 16:51:35 +02003269 err = mlx5_eth_lag_init(dev);
Aviv Heller9ef9c642016-09-18 20:48:01 +03003270 if (err)
3271 goto err_disable_roce;
3272
Achiad Shochate53505a2015-12-23 18:47:25 +02003273 return 0;
3274
Aviv Heller9ef9c642016-09-18 20:48:01 +03003275err_disable_roce:
Or Gerlitzca5b91d2016-11-27 16:51:36 +02003276 if (MLX5_CAP_GEN(dev->mdev, roce))
3277 mlx5_nic_vport_disable_roce(dev->mdev);
Aviv Heller9ef9c642016-09-18 20:48:01 +03003278
Achiad Shochate53505a2015-12-23 18:47:25 +02003279err_unregister_netdevice_notifier:
Or Gerlitzd012f5d2016-11-27 16:51:34 +02003280 mlx5_remove_netdev_notifier(dev);
Achiad Shochate53505a2015-12-23 18:47:25 +02003281 return err;
Achiad Shochatfc24fc52015-12-23 18:47:17 +02003282}
3283
Or Gerlitz45f95ac2016-11-27 16:51:35 +02003284static void mlx5_disable_eth(struct mlx5_ib_dev *dev)
Achiad Shochatfc24fc52015-12-23 18:47:17 +02003285{
Or Gerlitz45f95ac2016-11-27 16:51:35 +02003286 mlx5_eth_lag_cleanup(dev);
Or Gerlitzca5b91d2016-11-27 16:51:36 +02003287 if (MLX5_CAP_GEN(dev->mdev, roce))
3288 mlx5_nic_vport_disable_roce(dev->mdev);
Achiad Shochatfc24fc52015-12-23 18:47:17 +02003289}
3290
Parav Pandite1f24a72017-04-16 07:29:29 +03003291struct mlx5_ib_counter {
Kamal Heib7c16f472017-01-18 15:25:09 +02003292 const char *name;
3293 size_t offset;
3294};
3295
3296#define INIT_Q_COUNTER(_name) \
3297 { .name = #_name, .offset = MLX5_BYTE_OFF(query_q_counter_out, _name)}
3298
Parav Pandite1f24a72017-04-16 07:29:29 +03003299static const struct mlx5_ib_counter basic_q_cnts[] = {
Kamal Heib7c16f472017-01-18 15:25:09 +02003300 INIT_Q_COUNTER(rx_write_requests),
3301 INIT_Q_COUNTER(rx_read_requests),
3302 INIT_Q_COUNTER(rx_atomic_requests),
3303 INIT_Q_COUNTER(out_of_buffer),
3304};
3305
Parav Pandite1f24a72017-04-16 07:29:29 +03003306static const struct mlx5_ib_counter out_of_seq_q_cnts[] = {
Kamal Heib7c16f472017-01-18 15:25:09 +02003307 INIT_Q_COUNTER(out_of_sequence),
3308};
3309
Parav Pandite1f24a72017-04-16 07:29:29 +03003310static const struct mlx5_ib_counter retrans_q_cnts[] = {
Kamal Heib7c16f472017-01-18 15:25:09 +02003311 INIT_Q_COUNTER(duplicate_request),
3312 INIT_Q_COUNTER(rnr_nak_retry_err),
3313 INIT_Q_COUNTER(packet_seq_err),
3314 INIT_Q_COUNTER(implied_nak_seq_err),
3315 INIT_Q_COUNTER(local_ack_timeout_err),
3316};
3317
Parav Pandite1f24a72017-04-16 07:29:29 +03003318#define INIT_CONG_COUNTER(_name) \
3319 { .name = #_name, .offset = \
3320 MLX5_BYTE_OFF(query_cong_statistics_out, _name ## _high)}
3321
3322static const struct mlx5_ib_counter cong_cnts[] = {
3323 INIT_CONG_COUNTER(rp_cnp_ignored),
3324 INIT_CONG_COUNTER(rp_cnp_handled),
3325 INIT_CONG_COUNTER(np_ecn_marked_roce_packets),
3326 INIT_CONG_COUNTER(np_cnp_sent),
3327};
3328
3329static void mlx5_ib_dealloc_counters(struct mlx5_ib_dev *dev)
Mark Bloch0837e862016-06-17 15:10:55 +03003330{
3331 unsigned int i;
3332
Kamal Heib7c16f472017-01-18 15:25:09 +02003333 for (i = 0; i < dev->num_ports; i++) {
Mark Bloch0837e862016-06-17 15:10:55 +03003334 mlx5_core_dealloc_q_counter(dev->mdev,
Parav Pandite1f24a72017-04-16 07:29:29 +03003335 dev->port[i].cnts.set_id);
3336 kfree(dev->port[i].cnts.names);
3337 kfree(dev->port[i].cnts.offsets);
Kamal Heib7c16f472017-01-18 15:25:09 +02003338 }
3339}
3340
Parav Pandite1f24a72017-04-16 07:29:29 +03003341static int __mlx5_ib_alloc_counters(struct mlx5_ib_dev *dev,
3342 struct mlx5_ib_counters *cnts)
Kamal Heib7c16f472017-01-18 15:25:09 +02003343{
3344 u32 num_counters;
3345
3346 num_counters = ARRAY_SIZE(basic_q_cnts);
3347
3348 if (MLX5_CAP_GEN(dev->mdev, out_of_seq_cnt))
3349 num_counters += ARRAY_SIZE(out_of_seq_q_cnts);
3350
3351 if (MLX5_CAP_GEN(dev->mdev, retransmission_q_counters))
3352 num_counters += ARRAY_SIZE(retrans_q_cnts);
Parav Pandite1f24a72017-04-16 07:29:29 +03003353 cnts->num_q_counters = num_counters;
Kamal Heib7c16f472017-01-18 15:25:09 +02003354
Parav Pandite1f24a72017-04-16 07:29:29 +03003355 if (MLX5_CAP_GEN(dev->mdev, cc_query_allowed)) {
3356 cnts->num_cong_counters = ARRAY_SIZE(cong_cnts);
3357 num_counters += ARRAY_SIZE(cong_cnts);
3358 }
3359
3360 cnts->names = kcalloc(num_counters, sizeof(cnts->names), GFP_KERNEL);
3361 if (!cnts->names)
Kamal Heib7c16f472017-01-18 15:25:09 +02003362 return -ENOMEM;
3363
Parav Pandite1f24a72017-04-16 07:29:29 +03003364 cnts->offsets = kcalloc(num_counters,
3365 sizeof(cnts->offsets), GFP_KERNEL);
3366 if (!cnts->offsets)
Kamal Heib7c16f472017-01-18 15:25:09 +02003367 goto err_names;
3368
Kamal Heib7c16f472017-01-18 15:25:09 +02003369 return 0;
3370
3371err_names:
Parav Pandite1f24a72017-04-16 07:29:29 +03003372 kfree(cnts->names);
Kamal Heib7c16f472017-01-18 15:25:09 +02003373 return -ENOMEM;
3374}
3375
Parav Pandite1f24a72017-04-16 07:29:29 +03003376static void mlx5_ib_fill_counters(struct mlx5_ib_dev *dev,
3377 const char **names,
3378 size_t *offsets)
Kamal Heib7c16f472017-01-18 15:25:09 +02003379{
3380 int i;
3381 int j = 0;
3382
3383 for (i = 0; i < ARRAY_SIZE(basic_q_cnts); i++, j++) {
3384 names[j] = basic_q_cnts[i].name;
3385 offsets[j] = basic_q_cnts[i].offset;
3386 }
3387
3388 if (MLX5_CAP_GEN(dev->mdev, out_of_seq_cnt)) {
3389 for (i = 0; i < ARRAY_SIZE(out_of_seq_q_cnts); i++, j++) {
3390 names[j] = out_of_seq_q_cnts[i].name;
3391 offsets[j] = out_of_seq_q_cnts[i].offset;
3392 }
3393 }
3394
3395 if (MLX5_CAP_GEN(dev->mdev, retransmission_q_counters)) {
3396 for (i = 0; i < ARRAY_SIZE(retrans_q_cnts); i++, j++) {
3397 names[j] = retrans_q_cnts[i].name;
3398 offsets[j] = retrans_q_cnts[i].offset;
3399 }
3400 }
Parav Pandite1f24a72017-04-16 07:29:29 +03003401
3402 if (MLX5_CAP_GEN(dev->mdev, cc_query_allowed)) {
3403 for (i = 0; i < ARRAY_SIZE(cong_cnts); i++, j++) {
3404 names[j] = cong_cnts[i].name;
3405 offsets[j] = cong_cnts[i].offset;
3406 }
3407 }
Mark Bloch0837e862016-06-17 15:10:55 +03003408}
3409
Parav Pandite1f24a72017-04-16 07:29:29 +03003410static int mlx5_ib_alloc_counters(struct mlx5_ib_dev *dev)
Mark Bloch0837e862016-06-17 15:10:55 +03003411{
3412 int i;
3413 int ret;
3414
3415 for (i = 0; i < dev->num_ports; i++) {
Kamal Heib7c16f472017-01-18 15:25:09 +02003416 struct mlx5_ib_port *port = &dev->port[i];
3417
Mark Bloch0837e862016-06-17 15:10:55 +03003418 ret = mlx5_core_alloc_q_counter(dev->mdev,
Parav Pandite1f24a72017-04-16 07:29:29 +03003419 &port->cnts.set_id);
Mark Bloch0837e862016-06-17 15:10:55 +03003420 if (ret) {
3421 mlx5_ib_warn(dev,
3422 "couldn't allocate queue counter for port %d, err %d\n",
3423 i + 1, ret);
3424 goto dealloc_counters;
3425 }
Kamal Heib7c16f472017-01-18 15:25:09 +02003426
Parav Pandite1f24a72017-04-16 07:29:29 +03003427 ret = __mlx5_ib_alloc_counters(dev, &port->cnts);
Kamal Heib7c16f472017-01-18 15:25:09 +02003428 if (ret)
3429 goto dealloc_counters;
3430
Parav Pandite1f24a72017-04-16 07:29:29 +03003431 mlx5_ib_fill_counters(dev, port->cnts.names,
3432 port->cnts.offsets);
Mark Bloch0837e862016-06-17 15:10:55 +03003433 }
3434
3435 return 0;
3436
3437dealloc_counters:
3438 while (--i >= 0)
3439 mlx5_core_dealloc_q_counter(dev->mdev,
Parav Pandite1f24a72017-04-16 07:29:29 +03003440 dev->port[i].cnts.set_id);
Mark Bloch0837e862016-06-17 15:10:55 +03003441
3442 return ret;
3443}
3444
Mark Bloch0ad17a82016-06-17 15:10:56 +03003445static struct rdma_hw_stats *mlx5_ib_alloc_hw_stats(struct ib_device *ibdev,
3446 u8 port_num)
3447{
Kamal Heib7c16f472017-01-18 15:25:09 +02003448 struct mlx5_ib_dev *dev = to_mdev(ibdev);
3449 struct mlx5_ib_port *port = &dev->port[port_num - 1];
Mark Bloch0ad17a82016-06-17 15:10:56 +03003450
3451 /* We support only per port stats */
3452 if (port_num == 0)
3453 return NULL;
3454
Parav Pandite1f24a72017-04-16 07:29:29 +03003455 return rdma_alloc_hw_stats_struct(port->cnts.names,
3456 port->cnts.num_q_counters +
3457 port->cnts.num_cong_counters,
Mark Bloch0ad17a82016-06-17 15:10:56 +03003458 RDMA_HW_STATS_DEFAULT_LIFESPAN);
3459}
3460
Parav Pandite1f24a72017-04-16 07:29:29 +03003461static int mlx5_ib_query_q_counters(struct mlx5_ib_dev *dev,
3462 struct mlx5_ib_port *port,
3463 struct rdma_hw_stats *stats)
3464{
3465 int outlen = MLX5_ST_SZ_BYTES(query_q_counter_out);
3466 void *out;
3467 __be32 val;
3468 int ret, i;
3469
Leon Romanovsky1b9a07e2017-05-10 21:32:18 +03003470 out = kvzalloc(outlen, GFP_KERNEL);
Parav Pandite1f24a72017-04-16 07:29:29 +03003471 if (!out)
3472 return -ENOMEM;
3473
3474 ret = mlx5_core_query_q_counter(dev->mdev,
3475 port->cnts.set_id, 0,
3476 out, outlen);
3477 if (ret)
3478 goto free;
3479
3480 for (i = 0; i < port->cnts.num_q_counters; i++) {
3481 val = *(__be32 *)(out + port->cnts.offsets[i]);
3482 stats->value[i] = (u64)be32_to_cpu(val);
3483 }
3484
3485free:
3486 kvfree(out);
3487 return ret;
3488}
3489
3490static int mlx5_ib_query_cong_counters(struct mlx5_ib_dev *dev,
3491 struct mlx5_ib_port *port,
3492 struct rdma_hw_stats *stats)
3493{
3494 int outlen = MLX5_ST_SZ_BYTES(query_cong_statistics_out);
3495 void *out;
3496 int ret, i;
3497 int offset = port->cnts.num_q_counters;
3498
Leon Romanovsky1b9a07e2017-05-10 21:32:18 +03003499 out = kvzalloc(outlen, GFP_KERNEL);
Parav Pandite1f24a72017-04-16 07:29:29 +03003500 if (!out)
3501 return -ENOMEM;
3502
3503 ret = mlx5_cmd_query_cong_counter(dev->mdev, false, out, outlen);
3504 if (ret)
3505 goto free;
3506
3507 for (i = 0; i < port->cnts.num_cong_counters; i++) {
3508 stats->value[i + offset] =
3509 be64_to_cpup((__be64 *)(out +
3510 port->cnts.offsets[i + offset]));
3511 }
3512
3513free:
3514 kvfree(out);
3515 return ret;
3516}
3517
Mark Bloch0ad17a82016-06-17 15:10:56 +03003518static int mlx5_ib_get_hw_stats(struct ib_device *ibdev,
3519 struct rdma_hw_stats *stats,
Kamal Heib7c16f472017-01-18 15:25:09 +02003520 u8 port_num, int index)
Mark Bloch0ad17a82016-06-17 15:10:56 +03003521{
3522 struct mlx5_ib_dev *dev = to_mdev(ibdev);
Kamal Heib7c16f472017-01-18 15:25:09 +02003523 struct mlx5_ib_port *port = &dev->port[port_num - 1];
Parav Pandite1f24a72017-04-16 07:29:29 +03003524 int ret, num_counters;
Mark Bloch0ad17a82016-06-17 15:10:56 +03003525
Kamal Heib7c16f472017-01-18 15:25:09 +02003526 if (!stats)
Parav Pandite1f24a72017-04-16 07:29:29 +03003527 return -EINVAL;
Mark Bloch0ad17a82016-06-17 15:10:56 +03003528
Parav Pandite1f24a72017-04-16 07:29:29 +03003529 ret = mlx5_ib_query_q_counters(dev, port, stats);
Mark Bloch0ad17a82016-06-17 15:10:56 +03003530 if (ret)
Parav Pandite1f24a72017-04-16 07:29:29 +03003531 return ret;
3532 num_counters = port->cnts.num_q_counters;
Mark Bloch0ad17a82016-06-17 15:10:56 +03003533
Parav Pandite1f24a72017-04-16 07:29:29 +03003534 if (MLX5_CAP_GEN(dev->mdev, cc_query_allowed)) {
3535 ret = mlx5_ib_query_cong_counters(dev, port, stats);
3536 if (ret)
3537 return ret;
3538 num_counters += port->cnts.num_cong_counters;
Mark Bloch0ad17a82016-06-17 15:10:56 +03003539 }
Kamal Heib7c16f472017-01-18 15:25:09 +02003540
Parav Pandite1f24a72017-04-16 07:29:29 +03003541 return num_counters;
Mark Bloch0ad17a82016-06-17 15:10:56 +03003542}
3543
Erez Shitrit693dfd52017-04-27 17:01:34 +03003544static struct net_device*
3545mlx5_ib_alloc_rdma_netdev(struct ib_device *hca,
3546 u8 port_num,
3547 enum rdma_netdev_t type,
3548 const char *name,
3549 unsigned char name_assign_type,
3550 void (*setup)(struct net_device *))
3551{
3552 if (type != RDMA_NETDEV_IPOIB)
3553 return ERR_PTR(-EOPNOTSUPP);
3554
3555 return mlx5_rdma_netdev_alloc(to_mdev(hca)->mdev, hca,
3556 name, setup);
3557}
3558
3559static void mlx5_ib_free_rdma_netdev(struct net_device *netdev)
3560{
3561 return mlx5_rdma_netdev_free(netdev);
3562}
3563
Jack Morgenstein9603b612014-07-28 23:30:22 +03003564static void *mlx5_ib_add(struct mlx5_core_dev *mdev)
Eli Cohene126ba92013-07-07 17:25:49 +03003565{
Eli Cohene126ba92013-07-07 17:25:49 +03003566 struct mlx5_ib_dev *dev;
Achiad Shochatebd61f62015-12-23 18:47:16 +02003567 enum rdma_link_layer ll;
3568 int port_type_cap;
Aviv Heller4babcf92016-09-18 20:48:03 +03003569 const char *name;
Eli Cohene126ba92013-07-07 17:25:49 +03003570 int err;
3571 int i;
3572
Achiad Shochatebd61f62015-12-23 18:47:16 +02003573 port_type_cap = MLX5_CAP_GEN(mdev, port_type);
3574 ll = mlx5_port_type_cap_to_rdma_ll(port_type_cap);
3575
Eli Cohene126ba92013-07-07 17:25:49 +03003576 printk_once(KERN_INFO "%s", mlx5_version);
3577
3578 dev = (struct mlx5_ib_dev *)ib_alloc_device(sizeof(*dev));
3579 if (!dev)
Jack Morgenstein9603b612014-07-28 23:30:22 +03003580 return NULL;
Eli Cohene126ba92013-07-07 17:25:49 +03003581
Jack Morgenstein9603b612014-07-28 23:30:22 +03003582 dev->mdev = mdev;
Eli Cohene126ba92013-07-07 17:25:49 +03003583
Mark Bloch0837e862016-06-17 15:10:55 +03003584 dev->port = kcalloc(MLX5_CAP_GEN(mdev, num_ports), sizeof(*dev->port),
3585 GFP_KERNEL);
3586 if (!dev->port)
3587 goto err_dealloc;
3588
Achiad Shochatfc24fc52015-12-23 18:47:17 +02003589 rwlock_init(&dev->roce.netdev_lock);
Eli Cohene126ba92013-07-07 17:25:49 +03003590 err = get_port_caps(dev);
3591 if (err)
Mark Bloch0837e862016-06-17 15:10:55 +03003592 goto err_free_port;
Eli Cohene126ba92013-07-07 17:25:49 +03003593
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03003594 if (mlx5_use_mad_ifc(dev))
3595 get_ext_port_caps(dev);
Eli Cohene126ba92013-07-07 17:25:49 +03003596
Aviv Heller4babcf92016-09-18 20:48:03 +03003597 if (!mlx5_lag_is_active(mdev))
3598 name = "mlx5_%d";
3599 else
3600 name = "mlx5_bond_%d";
3601
3602 strlcpy(dev->ib_dev.name, name, IB_DEVICE_NAME_MAX);
Eli Cohene126ba92013-07-07 17:25:49 +03003603 dev->ib_dev.owner = THIS_MODULE;
3604 dev->ib_dev.node_type = RDMA_NODE_IB_CA;
Sagi Grimbergc6790aa2015-09-24 10:34:23 +03003605 dev->ib_dev.local_dma_lkey = 0 /* not supported for now */;
Saeed Mahameed938fe832015-05-28 22:28:41 +03003606 dev->num_ports = MLX5_CAP_GEN(mdev, num_ports);
Eli Cohene126ba92013-07-07 17:25:49 +03003607 dev->ib_dev.phys_port_cnt = dev->num_ports;
Saeed Mahameed233d05d2015-04-02 17:07:32 +03003608 dev->ib_dev.num_comp_vectors =
3609 dev->mdev->priv.eq_table.num_comp_vectors;
Bart Van Assche9b0c2892017-01-20 13:04:21 -08003610 dev->ib_dev.dev.parent = &mdev->pdev->dev;
Eli Cohene126ba92013-07-07 17:25:49 +03003611
3612 dev->ib_dev.uverbs_abi_ver = MLX5_IB_UVERBS_ABI_VERSION;
3613 dev->ib_dev.uverbs_cmd_mask =
3614 (1ull << IB_USER_VERBS_CMD_GET_CONTEXT) |
3615 (1ull << IB_USER_VERBS_CMD_QUERY_DEVICE) |
3616 (1ull << IB_USER_VERBS_CMD_QUERY_PORT) |
3617 (1ull << IB_USER_VERBS_CMD_ALLOC_PD) |
3618 (1ull << IB_USER_VERBS_CMD_DEALLOC_PD) |
Moni Shoua41c450f2016-11-23 08:23:26 +02003619 (1ull << IB_USER_VERBS_CMD_CREATE_AH) |
3620 (1ull << IB_USER_VERBS_CMD_DESTROY_AH) |
Eli Cohene126ba92013-07-07 17:25:49 +03003621 (1ull << IB_USER_VERBS_CMD_REG_MR) |
Noa Osherovich56e11d62016-02-29 16:46:51 +02003622 (1ull << IB_USER_VERBS_CMD_REREG_MR) |
Eli Cohene126ba92013-07-07 17:25:49 +03003623 (1ull << IB_USER_VERBS_CMD_DEREG_MR) |
3624 (1ull << IB_USER_VERBS_CMD_CREATE_COMP_CHANNEL) |
3625 (1ull << IB_USER_VERBS_CMD_CREATE_CQ) |
3626 (1ull << IB_USER_VERBS_CMD_RESIZE_CQ) |
3627 (1ull << IB_USER_VERBS_CMD_DESTROY_CQ) |
3628 (1ull << IB_USER_VERBS_CMD_CREATE_QP) |
3629 (1ull << IB_USER_VERBS_CMD_MODIFY_QP) |
3630 (1ull << IB_USER_VERBS_CMD_QUERY_QP) |
3631 (1ull << IB_USER_VERBS_CMD_DESTROY_QP) |
3632 (1ull << IB_USER_VERBS_CMD_ATTACH_MCAST) |
3633 (1ull << IB_USER_VERBS_CMD_DETACH_MCAST) |
3634 (1ull << IB_USER_VERBS_CMD_CREATE_SRQ) |
3635 (1ull << IB_USER_VERBS_CMD_MODIFY_SRQ) |
3636 (1ull << IB_USER_VERBS_CMD_QUERY_SRQ) |
3637 (1ull << IB_USER_VERBS_CMD_DESTROY_SRQ) |
3638 (1ull << IB_USER_VERBS_CMD_CREATE_XSRQ) |
3639 (1ull << IB_USER_VERBS_CMD_OPEN_QP);
Haggai Eran1707cb42015-02-08 13:28:52 +02003640 dev->ib_dev.uverbs_ex_cmd_mask =
Matan Barakd4584dd2016-01-28 17:51:46 +02003641 (1ull << IB_USER_VERBS_EX_CMD_QUERY_DEVICE) |
3642 (1ull << IB_USER_VERBS_EX_CMD_CREATE_CQ) |
Bodong Wang7d29f342016-12-01 13:43:16 +02003643 (1ull << IB_USER_VERBS_EX_CMD_CREATE_QP) |
3644 (1ull << IB_USER_VERBS_EX_CMD_MODIFY_QP);
Eli Cohene126ba92013-07-07 17:25:49 +03003645
3646 dev->ib_dev.query_device = mlx5_ib_query_device;
3647 dev->ib_dev.query_port = mlx5_ib_query_port;
Achiad Shochatebd61f62015-12-23 18:47:16 +02003648 dev->ib_dev.get_link_layer = mlx5_ib_port_link_layer;
Achiad Shochatfc24fc52015-12-23 18:47:17 +02003649 if (ll == IB_LINK_LAYER_ETHERNET)
3650 dev->ib_dev.get_netdev = mlx5_ib_get_netdev;
Eli Cohene126ba92013-07-07 17:25:49 +03003651 dev->ib_dev.query_gid = mlx5_ib_query_gid;
Achiad Shochat3cca2602015-12-23 18:47:23 +02003652 dev->ib_dev.add_gid = mlx5_ib_add_gid;
3653 dev->ib_dev.del_gid = mlx5_ib_del_gid;
Eli Cohene126ba92013-07-07 17:25:49 +03003654 dev->ib_dev.query_pkey = mlx5_ib_query_pkey;
3655 dev->ib_dev.modify_device = mlx5_ib_modify_device;
3656 dev->ib_dev.modify_port = mlx5_ib_modify_port;
3657 dev->ib_dev.alloc_ucontext = mlx5_ib_alloc_ucontext;
3658 dev->ib_dev.dealloc_ucontext = mlx5_ib_dealloc_ucontext;
3659 dev->ib_dev.mmap = mlx5_ib_mmap;
3660 dev->ib_dev.alloc_pd = mlx5_ib_alloc_pd;
3661 dev->ib_dev.dealloc_pd = mlx5_ib_dealloc_pd;
3662 dev->ib_dev.create_ah = mlx5_ib_create_ah;
3663 dev->ib_dev.query_ah = mlx5_ib_query_ah;
3664 dev->ib_dev.destroy_ah = mlx5_ib_destroy_ah;
3665 dev->ib_dev.create_srq = mlx5_ib_create_srq;
3666 dev->ib_dev.modify_srq = mlx5_ib_modify_srq;
3667 dev->ib_dev.query_srq = mlx5_ib_query_srq;
3668 dev->ib_dev.destroy_srq = mlx5_ib_destroy_srq;
3669 dev->ib_dev.post_srq_recv = mlx5_ib_post_srq_recv;
3670 dev->ib_dev.create_qp = mlx5_ib_create_qp;
3671 dev->ib_dev.modify_qp = mlx5_ib_modify_qp;
3672 dev->ib_dev.query_qp = mlx5_ib_query_qp;
3673 dev->ib_dev.destroy_qp = mlx5_ib_destroy_qp;
3674 dev->ib_dev.post_send = mlx5_ib_post_send;
3675 dev->ib_dev.post_recv = mlx5_ib_post_recv;
3676 dev->ib_dev.create_cq = mlx5_ib_create_cq;
3677 dev->ib_dev.modify_cq = mlx5_ib_modify_cq;
3678 dev->ib_dev.resize_cq = mlx5_ib_resize_cq;
3679 dev->ib_dev.destroy_cq = mlx5_ib_destroy_cq;
3680 dev->ib_dev.poll_cq = mlx5_ib_poll_cq;
3681 dev->ib_dev.req_notify_cq = mlx5_ib_arm_cq;
3682 dev->ib_dev.get_dma_mr = mlx5_ib_get_dma_mr;
3683 dev->ib_dev.reg_user_mr = mlx5_ib_reg_user_mr;
Noa Osherovich56e11d62016-02-29 16:46:51 +02003684 dev->ib_dev.rereg_user_mr = mlx5_ib_rereg_user_mr;
Eli Cohene126ba92013-07-07 17:25:49 +03003685 dev->ib_dev.dereg_mr = mlx5_ib_dereg_mr;
3686 dev->ib_dev.attach_mcast = mlx5_ib_mcg_attach;
3687 dev->ib_dev.detach_mcast = mlx5_ib_mcg_detach;
3688 dev->ib_dev.process_mad = mlx5_ib_process_mad;
Sagi Grimberg9bee1782015-07-30 10:32:35 +03003689 dev->ib_dev.alloc_mr = mlx5_ib_alloc_mr;
Sagi Grimberg8a187ee2015-10-13 19:11:26 +03003690 dev->ib_dev.map_mr_sg = mlx5_ib_map_mr_sg;
Sagi Grimbergd5436ba2014-02-23 14:19:12 +02003691 dev->ib_dev.check_mr_status = mlx5_ib_check_mr_status;
Ira Weiny77386132015-05-13 20:02:58 -04003692 dev->ib_dev.get_port_immutable = mlx5_port_immutable;
Ira Weinyc7342822016-06-15 02:22:01 -04003693 dev->ib_dev.get_dev_fw_str = get_dev_fw_str;
Erez Shitrit693dfd52017-04-27 17:01:34 +03003694 dev->ib_dev.alloc_rdma_netdev = mlx5_ib_alloc_rdma_netdev;
3695 dev->ib_dev.free_rdma_netdev = mlx5_ib_free_rdma_netdev;
Eli Coheneff901d2016-03-11 22:58:42 +02003696 if (mlx5_core_is_pf(mdev)) {
3697 dev->ib_dev.get_vf_config = mlx5_ib_get_vf_config;
3698 dev->ib_dev.set_vf_link_state = mlx5_ib_set_vf_link_state;
3699 dev->ib_dev.get_vf_stats = mlx5_ib_get_vf_stats;
3700 dev->ib_dev.set_vf_guid = mlx5_ib_set_vf_guid;
3701 }
Eli Cohene126ba92013-07-07 17:25:49 +03003702
Maor Gottlieb7c2344c2016-06-17 14:56:44 +03003703 dev->ib_dev.disassociate_ucontext = mlx5_ib_disassociate_ucontext;
3704
Saeed Mahameed938fe832015-05-28 22:28:41 +03003705 mlx5_ib_internal_fill_odp_caps(dev);
Haggai Eran8cdd3122014-12-11 17:04:20 +02003706
Max Gurtovoy6e8484c2017-05-28 10:53:11 +03003707 dev->umr_fence = mlx5_get_umr_fence(MLX5_CAP_GEN(mdev, umr_fence));
3708
Matan Barakd2370e02016-02-29 18:05:30 +02003709 if (MLX5_CAP_GEN(mdev, imaicl)) {
3710 dev->ib_dev.alloc_mw = mlx5_ib_alloc_mw;
3711 dev->ib_dev.dealloc_mw = mlx5_ib_dealloc_mw;
3712 dev->ib_dev.uverbs_cmd_mask |=
3713 (1ull << IB_USER_VERBS_CMD_ALLOC_MW) |
3714 (1ull << IB_USER_VERBS_CMD_DEALLOC_MW);
3715 }
3716
Kamal Heib7c16f472017-01-18 15:25:09 +02003717 if (MLX5_CAP_GEN(dev->mdev, max_qp_cnt)) {
Mark Bloch0ad17a82016-06-17 15:10:56 +03003718 dev->ib_dev.get_hw_stats = mlx5_ib_get_hw_stats;
3719 dev->ib_dev.alloc_hw_stats = mlx5_ib_alloc_hw_stats;
3720 }
3721
Saeed Mahameed938fe832015-05-28 22:28:41 +03003722 if (MLX5_CAP_GEN(mdev, xrc)) {
Eli Cohene126ba92013-07-07 17:25:49 +03003723 dev->ib_dev.alloc_xrcd = mlx5_ib_alloc_xrcd;
3724 dev->ib_dev.dealloc_xrcd = mlx5_ib_dealloc_xrcd;
3725 dev->ib_dev.uverbs_cmd_mask |=
3726 (1ull << IB_USER_VERBS_CMD_OPEN_XRCD) |
3727 (1ull << IB_USER_VERBS_CMD_CLOSE_XRCD);
3728 }
3729
Linus Torvalds048ccca2016-01-23 18:45:06 -08003730 if (mlx5_ib_port_link_layer(&dev->ib_dev, 1) ==
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003731 IB_LINK_LAYER_ETHERNET) {
3732 dev->ib_dev.create_flow = mlx5_ib_create_flow;
3733 dev->ib_dev.destroy_flow = mlx5_ib_destroy_flow;
Yishai Hadas79b20a62016-05-23 15:20:50 +03003734 dev->ib_dev.create_wq = mlx5_ib_create_wq;
3735 dev->ib_dev.modify_wq = mlx5_ib_modify_wq;
3736 dev->ib_dev.destroy_wq = mlx5_ib_destroy_wq;
Yishai Hadasc5f90922016-05-23 15:20:53 +03003737 dev->ib_dev.create_rwq_ind_table = mlx5_ib_create_rwq_ind_table;
3738 dev->ib_dev.destroy_rwq_ind_table = mlx5_ib_destroy_rwq_ind_table;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003739 dev->ib_dev.uverbs_ex_cmd_mask |=
3740 (1ull << IB_USER_VERBS_EX_CMD_CREATE_FLOW) |
Yishai Hadas79b20a62016-05-23 15:20:50 +03003741 (1ull << IB_USER_VERBS_EX_CMD_DESTROY_FLOW) |
3742 (1ull << IB_USER_VERBS_EX_CMD_CREATE_WQ) |
3743 (1ull << IB_USER_VERBS_EX_CMD_MODIFY_WQ) |
Yishai Hadasc5f90922016-05-23 15:20:53 +03003744 (1ull << IB_USER_VERBS_EX_CMD_DESTROY_WQ) |
3745 (1ull << IB_USER_VERBS_EX_CMD_CREATE_RWQ_IND_TBL) |
3746 (1ull << IB_USER_VERBS_EX_CMD_DESTROY_RWQ_IND_TBL);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003747 }
Eli Cohene126ba92013-07-07 17:25:49 +03003748 err = init_node_data(dev);
3749 if (err)
Majd Dibbiny90be7c82016-10-27 16:36:39 +03003750 goto err_free_port;
Eli Cohene126ba92013-07-07 17:25:49 +03003751
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003752 mutex_init(&dev->flow_db.lock);
Eli Cohene126ba92013-07-07 17:25:49 +03003753 mutex_init(&dev->cap_mask_mutex);
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03003754 INIT_LIST_HEAD(&dev->qp_list);
3755 spin_lock_init(&dev->reset_flow_resource_lock);
Eli Cohene126ba92013-07-07 17:25:49 +03003756
Achiad Shochatfc24fc52015-12-23 18:47:17 +02003757 if (ll == IB_LINK_LAYER_ETHERNET) {
Or Gerlitz45f95ac2016-11-27 16:51:35 +02003758 err = mlx5_enable_eth(dev);
Achiad Shochatfc24fc52015-12-23 18:47:17 +02003759 if (err)
Majd Dibbiny90be7c82016-10-27 16:36:39 +03003760 goto err_free_port;
Achiad Shochatfc24fc52015-12-23 18:47:17 +02003761 }
3762
Eli Cohene126ba92013-07-07 17:25:49 +03003763 err = create_dev_resources(&dev->devr);
3764 if (err)
Or Gerlitz45f95ac2016-11-27 16:51:35 +02003765 goto err_disable_eth;
Eli Cohene126ba92013-07-07 17:25:49 +03003766
Haggai Eran6aec21f2014-12-11 17:04:23 +02003767 err = mlx5_ib_odp_init_one(dev);
Wei Yongjun281d1a92013-07-30 07:54:26 +08003768 if (err)
Eli Cohene126ba92013-07-07 17:25:49 +03003769 goto err_rsrc;
3770
Kamal Heib45bded22017-01-18 14:10:32 +02003771 if (MLX5_CAP_GEN(dev->mdev, max_qp_cnt)) {
Parav Pandite1f24a72017-04-16 07:29:29 +03003772 err = mlx5_ib_alloc_counters(dev);
Kamal Heib45bded22017-01-18 14:10:32 +02003773 if (err)
3774 goto err_odp;
3775 }
Haggai Eran6aec21f2014-12-11 17:04:23 +02003776
Eli Cohen5fe9dec2017-01-03 23:55:25 +02003777 dev->mdev->priv.uar = mlx5_get_uars_page(dev->mdev);
3778 if (!dev->mdev->priv.uar)
Parav Pandite1f24a72017-04-16 07:29:29 +03003779 goto err_cnt;
Eli Cohen5fe9dec2017-01-03 23:55:25 +02003780
3781 err = mlx5_alloc_bfreg(dev->mdev, &dev->bfreg, false, false);
3782 if (err)
3783 goto err_uar_page;
3784
3785 err = mlx5_alloc_bfreg(dev->mdev, &dev->fp_bfreg, false, true);
3786 if (err)
3787 goto err_bfreg;
3788
Mark Bloch0837e862016-06-17 15:10:55 +03003789 err = ib_register_device(&dev->ib_dev, NULL);
3790 if (err)
Eli Cohen5fe9dec2017-01-03 23:55:25 +02003791 goto err_fp_bfreg;
Mark Bloch0837e862016-06-17 15:10:55 +03003792
Eli Cohene126ba92013-07-07 17:25:49 +03003793 err = create_umr_res(dev);
3794 if (err)
3795 goto err_dev;
3796
3797 for (i = 0; i < ARRAY_SIZE(mlx5_class_attributes); i++) {
Wei Yongjun281d1a92013-07-30 07:54:26 +08003798 err = device_create_file(&dev->ib_dev.dev,
3799 mlx5_class_attributes[i]);
3800 if (err)
Eli Cohene126ba92013-07-07 17:25:49 +03003801 goto err_umrc;
3802 }
3803
3804 dev->ib_active = true;
3805
Jack Morgenstein9603b612014-07-28 23:30:22 +03003806 return dev;
Eli Cohene126ba92013-07-07 17:25:49 +03003807
3808err_umrc:
3809 destroy_umrc_res(dev);
3810
3811err_dev:
3812 ib_unregister_device(&dev->ib_dev);
3813
Eli Cohen5fe9dec2017-01-03 23:55:25 +02003814err_fp_bfreg:
3815 mlx5_free_bfreg(dev->mdev, &dev->fp_bfreg);
3816
3817err_bfreg:
3818 mlx5_free_bfreg(dev->mdev, &dev->bfreg);
3819
3820err_uar_page:
3821 mlx5_put_uars_page(dev->mdev, dev->mdev->priv.uar);
3822
Parav Pandite1f24a72017-04-16 07:29:29 +03003823err_cnt:
Kamal Heib45bded22017-01-18 14:10:32 +02003824 if (MLX5_CAP_GEN(dev->mdev, max_qp_cnt))
Parav Pandite1f24a72017-04-16 07:29:29 +03003825 mlx5_ib_dealloc_counters(dev);
Mark Bloch0837e862016-06-17 15:10:55 +03003826
Haggai Eran6aec21f2014-12-11 17:04:23 +02003827err_odp:
3828 mlx5_ib_odp_remove_one(dev);
3829
Eli Cohene126ba92013-07-07 17:25:49 +03003830err_rsrc:
3831 destroy_dev_resources(&dev->devr);
3832
Or Gerlitz45f95ac2016-11-27 16:51:35 +02003833err_disable_eth:
Aviv Heller5ec8c832016-09-18 20:48:00 +03003834 if (ll == IB_LINK_LAYER_ETHERNET) {
Or Gerlitz45f95ac2016-11-27 16:51:35 +02003835 mlx5_disable_eth(dev);
Or Gerlitzd012f5d2016-11-27 16:51:34 +02003836 mlx5_remove_netdev_notifier(dev);
Aviv Heller5ec8c832016-09-18 20:48:00 +03003837 }
Achiad Shochatfc24fc52015-12-23 18:47:17 +02003838
Mark Bloch0837e862016-06-17 15:10:55 +03003839err_free_port:
3840 kfree(dev->port);
3841
Jack Morgenstein9603b612014-07-28 23:30:22 +03003842err_dealloc:
Eli Cohene126ba92013-07-07 17:25:49 +03003843 ib_dealloc_device((struct ib_device *)dev);
3844
Jack Morgenstein9603b612014-07-28 23:30:22 +03003845 return NULL;
Eli Cohene126ba92013-07-07 17:25:49 +03003846}
3847
Jack Morgenstein9603b612014-07-28 23:30:22 +03003848static void mlx5_ib_remove(struct mlx5_core_dev *mdev, void *context)
Eli Cohene126ba92013-07-07 17:25:49 +03003849{
Jack Morgenstein9603b612014-07-28 23:30:22 +03003850 struct mlx5_ib_dev *dev = context;
Achiad Shochatfc24fc52015-12-23 18:47:17 +02003851 enum rdma_link_layer ll = mlx5_ib_port_link_layer(&dev->ib_dev, 1);
Haggai Eran6aec21f2014-12-11 17:04:23 +02003852
Or Gerlitzd012f5d2016-11-27 16:51:34 +02003853 mlx5_remove_netdev_notifier(dev);
Eli Cohene126ba92013-07-07 17:25:49 +03003854 ib_unregister_device(&dev->ib_dev);
Eli Cohen5fe9dec2017-01-03 23:55:25 +02003855 mlx5_free_bfreg(dev->mdev, &dev->fp_bfreg);
3856 mlx5_free_bfreg(dev->mdev, &dev->bfreg);
3857 mlx5_put_uars_page(dev->mdev, mdev->priv.uar);
Kamal Heib45bded22017-01-18 14:10:32 +02003858 if (MLX5_CAP_GEN(dev->mdev, max_qp_cnt))
Parav Pandite1f24a72017-04-16 07:29:29 +03003859 mlx5_ib_dealloc_counters(dev);
Eli Coheneefd56e2014-09-14 16:47:50 +03003860 destroy_umrc_res(dev);
Haggai Eran6aec21f2014-12-11 17:04:23 +02003861 mlx5_ib_odp_remove_one(dev);
Eli Cohene126ba92013-07-07 17:25:49 +03003862 destroy_dev_resources(&dev->devr);
Achiad Shochatfc24fc52015-12-23 18:47:17 +02003863 if (ll == IB_LINK_LAYER_ETHERNET)
Or Gerlitz45f95ac2016-11-27 16:51:35 +02003864 mlx5_disable_eth(dev);
Mark Bloch0837e862016-06-17 15:10:55 +03003865 kfree(dev->port);
Eli Cohene126ba92013-07-07 17:25:49 +03003866 ib_dealloc_device(&dev->ib_dev);
3867}
3868
Jack Morgenstein9603b612014-07-28 23:30:22 +03003869static struct mlx5_interface mlx5_ib_interface = {
3870 .add = mlx5_ib_add,
3871 .remove = mlx5_ib_remove,
3872 .event = mlx5_ib_event,
Artemy Kovalyovd9aaed82017-01-02 11:37:46 +02003873#ifdef CONFIG_INFINIBAND_ON_DEMAND_PAGING
3874 .pfault = mlx5_ib_pfault,
3875#endif
Saeed Mahameed64613d942015-04-02 17:07:34 +03003876 .protocol = MLX5_INTERFACE_PROTOCOL_IB,
Eli Cohene126ba92013-07-07 17:25:49 +03003877};
3878
3879static int __init mlx5_ib_init(void)
3880{
Haggai Eran6aec21f2014-12-11 17:04:23 +02003881 int err;
3882
Artemy Kovalyov81713d32017-01-18 16:58:11 +02003883 mlx5_ib_odp_init();
Jack Morgenstein9603b612014-07-28 23:30:22 +03003884
Haggai Eran6aec21f2014-12-11 17:04:23 +02003885 err = mlx5_register_interface(&mlx5_ib_interface);
Haggai Eran6aec21f2014-12-11 17:04:23 +02003886
3887 return err;
Eli Cohene126ba92013-07-07 17:25:49 +03003888}
3889
3890static void __exit mlx5_ib_cleanup(void)
3891{
Jack Morgenstein9603b612014-07-28 23:30:22 +03003892 mlx5_unregister_interface(&mlx5_ib_interface);
Eli Cohene126ba92013-07-07 17:25:49 +03003893}
3894
3895module_init(mlx5_ib_init);
3896module_exit(mlx5_ib_cleanup);