blob: f7fcde1ff0aae66fef09d6eaccf8f9de6b008ba1 [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
Ilan Tayari095b0922017-05-14 16:04:30 +0300226static int 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;
Ilan Tayari095b0922017-05-14 16:04:30 +0300235 int err;
Achiad Shochat3f89a642015-12-23 18:47:21 +0200236
Noa Osherovichf1b65df2017-04-20 20:53:33 +0300237 /* Possible bad flows are checked before filling out props so in case
238 * of an error it will still be zeroed out.
Noa Osherovich50f22fd2017-04-20 20:53:32 +0300239 */
Ilan Tayari095b0922017-05-14 16:04:30 +0300240 err = mlx5_query_port_eth_proto_oper(mdev, &eth_prot_oper, port_num);
241 if (err)
242 return err;
Noa Osherovichf1b65df2017-04-20 20:53:33 +0300243
244 translate_eth_proto_oper(eth_prot_oper, &props->active_speed,
245 &props->active_width);
Achiad Shochat3f89a642015-12-23 18:47:21 +0200246
247 props->port_cap_flags |= IB_PORT_CM_SUP;
248 props->port_cap_flags |= IB_PORT_IP_BASED_GIDS;
249
250 props->gid_tbl_len = MLX5_CAP_ROCE(dev->mdev,
251 roce_address_table_size);
252 props->max_mtu = IB_MTU_4096;
253 props->max_msg_sz = 1 << MLX5_CAP_GEN(dev->mdev, log_max_msg);
254 props->pkey_tbl_len = 1;
255 props->state = IB_PORT_DOWN;
256 props->phys_state = 3;
257
Leon Romanovskyc876a1b2016-01-09 13:06:25 +0200258 mlx5_query_nic_vport_qkey_viol_cntr(dev->mdev, &qkey_viol_cntr);
259 props->qkey_viol_cntr = qkey_viol_cntr;
Achiad Shochat3f89a642015-12-23 18:47:21 +0200260
261 ndev = mlx5_ib_get_netdev(device, port_num);
262 if (!ndev)
Ilan Tayari095b0922017-05-14 16:04:30 +0300263 return 0;
Achiad Shochat3f89a642015-12-23 18:47:21 +0200264
Aviv Heller88621df2016-09-18 20:48:02 +0300265 if (mlx5_lag_is_active(dev->mdev)) {
266 rcu_read_lock();
267 upper = netdev_master_upper_dev_get_rcu(ndev);
268 if (upper) {
269 dev_put(ndev);
270 ndev = upper;
271 dev_hold(ndev);
272 }
273 rcu_read_unlock();
274 }
275
Achiad Shochat3f89a642015-12-23 18:47:21 +0200276 if (netif_running(ndev) && netif_carrier_ok(ndev)) {
277 props->state = IB_PORT_ACTIVE;
278 props->phys_state = 5;
279 }
280
281 ndev_ib_mtu = iboe_get_mtu(ndev->mtu);
282
283 dev_put(ndev);
284
285 props->active_mtu = min(props->max_mtu, ndev_ib_mtu);
Ilan Tayari095b0922017-05-14 16:04:30 +0300286 return 0;
Achiad Shochat3f89a642015-12-23 18:47:21 +0200287}
288
Ilan Tayari095b0922017-05-14 16:04:30 +0300289static int set_roce_addr(struct mlx5_ib_dev *dev, u8 port_num,
290 unsigned int index, const union ib_gid *gid,
291 const struct ib_gid_attr *attr)
Achiad Shochat3cca2602015-12-23 18:47:23 +0200292{
Ilan Tayari095b0922017-05-14 16:04:30 +0300293 enum ib_gid_type gid_type = IB_GID_TYPE_IB;
294 u8 roce_version = 0;
295 u8 roce_l3_type = 0;
296 bool vlan = false;
297 u8 mac[ETH_ALEN];
298 u16 vlan_id = 0;
Achiad Shochat3cca2602015-12-23 18:47:23 +0200299
Ilan Tayari095b0922017-05-14 16:04:30 +0300300 if (gid) {
301 gid_type = attr->gid_type;
302 ether_addr_copy(mac, attr->ndev->dev_addr);
Achiad Shochat3cca2602015-12-23 18:47:23 +0200303
Ilan Tayari095b0922017-05-14 16:04:30 +0300304 if (is_vlan_dev(attr->ndev)) {
305 vlan = true;
306 vlan_id = vlan_dev_vlan_id(attr->ndev);
307 }
Achiad Shochat3cca2602015-12-23 18:47:23 +0200308 }
309
Ilan Tayari095b0922017-05-14 16:04:30 +0300310 switch (gid_type) {
Achiad Shochat3cca2602015-12-23 18:47:23 +0200311 case IB_GID_TYPE_IB:
Ilan Tayari095b0922017-05-14 16:04:30 +0300312 roce_version = MLX5_ROCE_VERSION_1;
Achiad Shochat3cca2602015-12-23 18:47:23 +0200313 break;
314 case IB_GID_TYPE_ROCE_UDP_ENCAP:
Ilan Tayari095b0922017-05-14 16:04:30 +0300315 roce_version = MLX5_ROCE_VERSION_2;
316 if (ipv6_addr_v4mapped((void *)gid))
317 roce_l3_type = MLX5_ROCE_L3_TYPE_IPV4;
318 else
319 roce_l3_type = MLX5_ROCE_L3_TYPE_IPV6;
Achiad Shochat3cca2602015-12-23 18:47:23 +0200320 break;
321
322 default:
Ilan Tayari095b0922017-05-14 16:04:30 +0300323 mlx5_ib_warn(dev, "Unexpected GID type %u\n", gid_type);
Achiad Shochat3cca2602015-12-23 18:47:23 +0200324 }
325
Ilan Tayari095b0922017-05-14 16:04:30 +0300326 return mlx5_core_roce_gid_set(dev->mdev, index, roce_version,
327 roce_l3_type, gid->raw, mac, vlan,
328 vlan_id);
Achiad Shochat3cca2602015-12-23 18:47:23 +0200329}
330
331static int mlx5_ib_add_gid(struct ib_device *device, u8 port_num,
332 unsigned int index, const union ib_gid *gid,
333 const struct ib_gid_attr *attr,
334 __always_unused void **context)
335{
Ilan Tayari095b0922017-05-14 16:04:30 +0300336 return set_roce_addr(to_mdev(device), port_num, index, gid, attr);
Achiad Shochat3cca2602015-12-23 18:47:23 +0200337}
338
339static int mlx5_ib_del_gid(struct ib_device *device, u8 port_num,
340 unsigned int index, __always_unused void **context)
341{
Ilan Tayari095b0922017-05-14 16:04:30 +0300342 return set_roce_addr(to_mdev(device), port_num, index, NULL, NULL);
Achiad Shochat3cca2602015-12-23 18:47:23 +0200343}
344
Achiad Shochat2811ba52015-12-23 18:47:24 +0200345__be16 mlx5_get_roce_udp_sport(struct mlx5_ib_dev *dev, u8 port_num,
346 int index)
347{
348 struct ib_gid_attr attr;
349 union ib_gid gid;
350
351 if (ib_get_cached_gid(&dev->ib_dev, port_num, index, &gid, &attr))
352 return 0;
353
354 if (!attr.ndev)
355 return 0;
356
357 dev_put(attr.ndev);
358
359 if (attr.gid_type != IB_GID_TYPE_ROCE_UDP_ENCAP)
360 return 0;
361
362 return cpu_to_be16(MLX5_CAP_ROCE(dev->mdev, r_roce_min_src_udp_port));
363}
364
Majd Dibbinyed884512017-01-18 14:10:35 +0200365int mlx5_get_roce_gid_type(struct mlx5_ib_dev *dev, u8 port_num,
366 int index, enum ib_gid_type *gid_type)
367{
368 struct ib_gid_attr attr;
369 union ib_gid gid;
370 int ret;
371
372 ret = ib_get_cached_gid(&dev->ib_dev, port_num, index, &gid, &attr);
373 if (ret)
374 return ret;
375
376 if (!attr.ndev)
377 return -ENODEV;
378
379 dev_put(attr.ndev);
380
381 *gid_type = attr.gid_type;
382
383 return 0;
384}
385
Majd Dibbiny1b5daf12015-06-04 19:30:46 +0300386static int mlx5_use_mad_ifc(struct mlx5_ib_dev *dev)
387{
Noa Osherovich7fae6652016-09-12 19:16:23 +0300388 if (MLX5_CAP_GEN(dev->mdev, port_type) == MLX5_CAP_PORT_TYPE_IB)
389 return !MLX5_CAP_GEN(dev->mdev, ib_virt);
390 return 0;
Majd Dibbiny1b5daf12015-06-04 19:30:46 +0300391}
392
393enum {
394 MLX5_VPORT_ACCESS_METHOD_MAD,
395 MLX5_VPORT_ACCESS_METHOD_HCA,
396 MLX5_VPORT_ACCESS_METHOD_NIC,
397};
398
399static int mlx5_get_vport_access_method(struct ib_device *ibdev)
400{
401 if (mlx5_use_mad_ifc(to_mdev(ibdev)))
402 return MLX5_VPORT_ACCESS_METHOD_MAD;
403
Achiad Shochatebd61f62015-12-23 18:47:16 +0200404 if (mlx5_ib_port_link_layer(ibdev, 1) ==
Majd Dibbiny1b5daf12015-06-04 19:30:46 +0300405 IB_LINK_LAYER_ETHERNET)
406 return MLX5_VPORT_ACCESS_METHOD_NIC;
407
408 return MLX5_VPORT_ACCESS_METHOD_HCA;
409}
410
Eran Ben Elishada7525d2015-12-14 16:34:10 +0200411static void get_atomic_caps(struct mlx5_ib_dev *dev,
412 struct ib_device_attr *props)
413{
414 u8 tmp;
415 u8 atomic_operations = MLX5_CAP_ATOMIC(dev->mdev, atomic_operations);
416 u8 atomic_size_qp = MLX5_CAP_ATOMIC(dev->mdev, atomic_size_qp);
417 u8 atomic_req_8B_endianness_mode =
Or Gerlitzbd108382017-05-28 15:24:17 +0300418 MLX5_CAP_ATOMIC(dev->mdev, atomic_req_8B_endianness_mode);
Eran Ben Elishada7525d2015-12-14 16:34:10 +0200419
420 /* Check if HW supports 8 bytes standard atomic operations and capable
421 * of host endianness respond
422 */
423 tmp = MLX5_ATOMIC_OPS_CMP_SWAP | MLX5_ATOMIC_OPS_FETCH_ADD;
424 if (((atomic_operations & tmp) == tmp) &&
425 (atomic_size_qp & MLX5_ATOMIC_SIZE_QP_8BYTES) &&
426 (atomic_req_8B_endianness_mode)) {
427 props->atomic_cap = IB_ATOMIC_HCA;
428 } else {
429 props->atomic_cap = IB_ATOMIC_NONE;
430 }
431}
432
Majd Dibbiny1b5daf12015-06-04 19:30:46 +0300433static int mlx5_query_system_image_guid(struct ib_device *ibdev,
434 __be64 *sys_image_guid)
435{
436 struct mlx5_ib_dev *dev = to_mdev(ibdev);
437 struct mlx5_core_dev *mdev = dev->mdev;
438 u64 tmp;
439 int err;
440
441 switch (mlx5_get_vport_access_method(ibdev)) {
442 case MLX5_VPORT_ACCESS_METHOD_MAD:
443 return mlx5_query_mad_ifc_system_image_guid(ibdev,
444 sys_image_guid);
445
446 case MLX5_VPORT_ACCESS_METHOD_HCA:
447 err = mlx5_query_hca_vport_system_image_guid(mdev, &tmp);
Achiad Shochat3f89a642015-12-23 18:47:21 +0200448 break;
449
450 case MLX5_VPORT_ACCESS_METHOD_NIC:
451 err = mlx5_query_nic_vport_system_image_guid(mdev, &tmp);
452 break;
Majd Dibbiny1b5daf12015-06-04 19:30:46 +0300453
454 default:
455 return -EINVAL;
456 }
Achiad Shochat3f89a642015-12-23 18:47:21 +0200457
458 if (!err)
459 *sys_image_guid = cpu_to_be64(tmp);
460
461 return err;
462
Majd Dibbiny1b5daf12015-06-04 19:30:46 +0300463}
464
465static int mlx5_query_max_pkeys(struct ib_device *ibdev,
466 u16 *max_pkeys)
467{
468 struct mlx5_ib_dev *dev = to_mdev(ibdev);
469 struct mlx5_core_dev *mdev = dev->mdev;
470
471 switch (mlx5_get_vport_access_method(ibdev)) {
472 case MLX5_VPORT_ACCESS_METHOD_MAD:
473 return mlx5_query_mad_ifc_max_pkeys(ibdev, max_pkeys);
474
475 case MLX5_VPORT_ACCESS_METHOD_HCA:
476 case MLX5_VPORT_ACCESS_METHOD_NIC:
477 *max_pkeys = mlx5_to_sw_pkey_sz(MLX5_CAP_GEN(mdev,
478 pkey_table_size));
479 return 0;
480
481 default:
482 return -EINVAL;
483 }
484}
485
486static int mlx5_query_vendor_id(struct ib_device *ibdev,
487 u32 *vendor_id)
488{
489 struct mlx5_ib_dev *dev = to_mdev(ibdev);
490
491 switch (mlx5_get_vport_access_method(ibdev)) {
492 case MLX5_VPORT_ACCESS_METHOD_MAD:
493 return mlx5_query_mad_ifc_vendor_id(ibdev, vendor_id);
494
495 case MLX5_VPORT_ACCESS_METHOD_HCA:
496 case MLX5_VPORT_ACCESS_METHOD_NIC:
497 return mlx5_core_query_vendor_id(dev->mdev, vendor_id);
498
499 default:
500 return -EINVAL;
501 }
502}
503
504static int mlx5_query_node_guid(struct mlx5_ib_dev *dev,
505 __be64 *node_guid)
506{
507 u64 tmp;
508 int err;
509
510 switch (mlx5_get_vport_access_method(&dev->ib_dev)) {
511 case MLX5_VPORT_ACCESS_METHOD_MAD:
512 return mlx5_query_mad_ifc_node_guid(dev, node_guid);
513
514 case MLX5_VPORT_ACCESS_METHOD_HCA:
515 err = mlx5_query_hca_vport_node_guid(dev->mdev, &tmp);
Achiad Shochat3f89a642015-12-23 18:47:21 +0200516 break;
517
518 case MLX5_VPORT_ACCESS_METHOD_NIC:
519 err = mlx5_query_nic_vport_node_guid(dev->mdev, &tmp);
520 break;
Majd Dibbiny1b5daf12015-06-04 19:30:46 +0300521
522 default:
523 return -EINVAL;
524 }
Achiad Shochat3f89a642015-12-23 18:47:21 +0200525
526 if (!err)
527 *node_guid = cpu_to_be64(tmp);
528
529 return err;
Majd Dibbiny1b5daf12015-06-04 19:30:46 +0300530}
531
532struct mlx5_reg_node_desc {
Yuval Shaiabd99fde2016-08-25 10:57:07 -0700533 u8 desc[IB_DEVICE_NODE_DESC_MAX];
Majd Dibbiny1b5daf12015-06-04 19:30:46 +0300534};
535
536static int mlx5_query_node_desc(struct mlx5_ib_dev *dev, char *node_desc)
537{
538 struct mlx5_reg_node_desc in;
539
540 if (mlx5_use_mad_ifc(dev))
541 return mlx5_query_mad_ifc_node_desc(dev, node_desc);
542
543 memset(&in, 0, sizeof(in));
544
545 return mlx5_core_access_reg(dev->mdev, &in, sizeof(in), node_desc,
546 sizeof(struct mlx5_reg_node_desc),
547 MLX5_REG_NODE_DESC, 0, 0);
548}
549
Eli Cohene126ba92013-07-07 17:25:49 +0300550static int mlx5_ib_query_device(struct ib_device *ibdev,
Matan Barak2528e332015-06-11 16:35:25 +0300551 struct ib_device_attr *props,
552 struct ib_udata *uhw)
Eli Cohene126ba92013-07-07 17:25:49 +0300553{
554 struct mlx5_ib_dev *dev = to_mdev(ibdev);
Saeed Mahameed938fe832015-05-28 22:28:41 +0300555 struct mlx5_core_dev *mdev = dev->mdev;
Eli Cohene126ba92013-07-07 17:25:49 +0300556 int err = -ENOMEM;
Eli Cohen288c01b2016-10-27 16:36:45 +0300557 int max_sq_desc;
Eli Cohene126ba92013-07-07 17:25:49 +0300558 int max_rq_sg;
559 int max_sq_sg;
Sagi Grimberge0238a62015-07-21 14:40:12 +0300560 u64 min_page_size = 1ull << MLX5_CAP_GEN(mdev, log_pg_sz);
Bodong Wang402ca532016-06-17 15:02:20 +0300561 struct mlx5_ib_query_device_resp resp = {};
562 size_t resp_len;
563 u64 max_tso;
Eli Cohene126ba92013-07-07 17:25:49 +0300564
Bodong Wang402ca532016-06-17 15:02:20 +0300565 resp_len = sizeof(resp.comp_mask) + sizeof(resp.response_length);
566 if (uhw->outlen && uhw->outlen < resp_len)
567 return -EINVAL;
568 else
569 resp.response_length = resp_len;
570
571 if (uhw->inlen && !ib_is_udata_cleared(uhw, 0, uhw->inlen))
Matan Barak2528e332015-06-11 16:35:25 +0300572 return -EINVAL;
573
Eli Cohene126ba92013-07-07 17:25:49 +0300574 memset(props, 0, sizeof(*props));
Majd Dibbiny1b5daf12015-06-04 19:30:46 +0300575 err = mlx5_query_system_image_guid(ibdev,
576 &props->sys_image_guid);
577 if (err)
578 return err;
579
580 err = mlx5_query_max_pkeys(ibdev, &props->max_pkeys);
581 if (err)
582 return err;
583
584 err = mlx5_query_vendor_id(ibdev, &props->vendor_id);
585 if (err)
586 return err;
Eli Cohene126ba92013-07-07 17:25:49 +0300587
Jack Morgenstein9603b612014-07-28 23:30:22 +0300588 props->fw_ver = ((u64)fw_rev_maj(dev->mdev) << 32) |
589 (fw_rev_min(dev->mdev) << 16) |
590 fw_rev_sub(dev->mdev);
Eli Cohene126ba92013-07-07 17:25:49 +0300591 props->device_cap_flags = IB_DEVICE_CHANGE_PHY_PORT |
592 IB_DEVICE_PORT_ACTIVE_EVENT |
593 IB_DEVICE_SYS_IMAGE_GUID |
Eli Cohen1a4c3a32014-02-06 17:41:25 +0200594 IB_DEVICE_RC_RNR_NAK_GEN;
Saeed Mahameed938fe832015-05-28 22:28:41 +0300595
596 if (MLX5_CAP_GEN(mdev, pkv))
Eli Cohene126ba92013-07-07 17:25:49 +0300597 props->device_cap_flags |= IB_DEVICE_BAD_PKEY_CNTR;
Saeed Mahameed938fe832015-05-28 22:28:41 +0300598 if (MLX5_CAP_GEN(mdev, qkv))
Eli Cohene126ba92013-07-07 17:25:49 +0300599 props->device_cap_flags |= IB_DEVICE_BAD_QKEY_CNTR;
Saeed Mahameed938fe832015-05-28 22:28:41 +0300600 if (MLX5_CAP_GEN(mdev, apm))
Eli Cohene126ba92013-07-07 17:25:49 +0300601 props->device_cap_flags |= IB_DEVICE_AUTO_PATH_MIG;
Saeed Mahameed938fe832015-05-28 22:28:41 +0300602 if (MLX5_CAP_GEN(mdev, xrc))
Eli Cohene126ba92013-07-07 17:25:49 +0300603 props->device_cap_flags |= IB_DEVICE_XRC;
Matan Barakd2370e02016-02-29 18:05:30 +0200604 if (MLX5_CAP_GEN(mdev, imaicl)) {
605 props->device_cap_flags |= IB_DEVICE_MEM_WINDOW |
606 IB_DEVICE_MEM_WINDOW_TYPE_2B;
607 props->max_mw = 1 << MLX5_CAP_GEN(mdev, log_max_mkey);
Sagi Grimbergb005d312016-02-29 19:07:33 +0200608 /* We support 'Gappy' memory registration too */
609 props->device_cap_flags |= IB_DEVICE_SG_GAPS_REG;
Matan Barakd2370e02016-02-29 18:05:30 +0200610 }
Eli Cohene126ba92013-07-07 17:25:49 +0300611 props->device_cap_flags |= IB_DEVICE_MEM_MGT_EXTENSIONS;
Saeed Mahameed938fe832015-05-28 22:28:41 +0300612 if (MLX5_CAP_GEN(mdev, sho)) {
Sagi Grimberg2dea9092014-02-23 14:19:13 +0200613 props->device_cap_flags |= IB_DEVICE_SIGNATURE_HANDOVER;
614 /* At this stage no support for signature handover */
615 props->sig_prot_cap = IB_PROT_T10DIF_TYPE_1 |
616 IB_PROT_T10DIF_TYPE_2 |
617 IB_PROT_T10DIF_TYPE_3;
618 props->sig_guard_cap = IB_GUARD_T10DIF_CRC |
619 IB_GUARD_T10DIF_CSUM;
620 }
Saeed Mahameed938fe832015-05-28 22:28:41 +0300621 if (MLX5_CAP_GEN(mdev, block_lb_mc))
Eli Cohenf360d882014-04-02 00:10:16 +0300622 props->device_cap_flags |= IB_DEVICE_BLOCK_MULTICAST_LOOPBACK;
Eli Cohene126ba92013-07-07 17:25:49 +0300623
Bodong Wang402ca532016-06-17 15:02:20 +0300624 if (MLX5_CAP_GEN(dev->mdev, eth_net_offloads)) {
Noa Osheroviche8161332017-01-18 15:40:01 +0200625 if (MLX5_CAP_ETH(mdev, csum_cap)) {
626 /* Legacy bit to support old userspace libraries */
Bodong Wang88115fe2015-12-18 13:53:20 +0200627 props->device_cap_flags |= IB_DEVICE_RAW_IP_CSUM;
Noa Osheroviche8161332017-01-18 15:40:01 +0200628 props->raw_packet_caps |= IB_RAW_PACKET_CAP_IP_CSUM;
629 }
630
631 if (MLX5_CAP_ETH(dev->mdev, vlan_cap))
632 props->raw_packet_caps |=
633 IB_RAW_PACKET_CAP_CVLAN_STRIPPING;
Bodong Wang88115fe2015-12-18 13:53:20 +0200634
Bodong Wang402ca532016-06-17 15:02:20 +0300635 if (field_avail(typeof(resp), tso_caps, uhw->outlen)) {
636 max_tso = MLX5_CAP_ETH(mdev, max_lso_cap);
637 if (max_tso) {
638 resp.tso_caps.max_tso = 1 << max_tso;
639 resp.tso_caps.supported_qpts |=
640 1 << IB_QPT_RAW_PACKET;
641 resp.response_length += sizeof(resp.tso_caps);
642 }
643 }
Yishai Hadas31f69a82016-08-28 11:28:45 +0300644
645 if (field_avail(typeof(resp), rss_caps, uhw->outlen)) {
646 resp.rss_caps.rx_hash_function =
647 MLX5_RX_HASH_FUNC_TOEPLITZ;
648 resp.rss_caps.rx_hash_fields_mask =
649 MLX5_RX_HASH_SRC_IPV4 |
650 MLX5_RX_HASH_DST_IPV4 |
651 MLX5_RX_HASH_SRC_IPV6 |
652 MLX5_RX_HASH_DST_IPV6 |
653 MLX5_RX_HASH_SRC_PORT_TCP |
654 MLX5_RX_HASH_DST_PORT_TCP |
655 MLX5_RX_HASH_SRC_PORT_UDP |
656 MLX5_RX_HASH_DST_PORT_UDP;
657 resp.response_length += sizeof(resp.rss_caps);
658 }
659 } else {
660 if (field_avail(typeof(resp), tso_caps, uhw->outlen))
661 resp.response_length += sizeof(resp.tso_caps);
662 if (field_avail(typeof(resp), rss_caps, uhw->outlen))
663 resp.response_length += sizeof(resp.rss_caps);
Bodong Wang402ca532016-06-17 15:02:20 +0300664 }
665
Erez Shitritf0313962016-02-21 16:27:17 +0200666 if (MLX5_CAP_GEN(mdev, ipoib_basic_offloads)) {
667 props->device_cap_flags |= IB_DEVICE_UD_IP_CSUM;
668 props->device_cap_flags |= IB_DEVICE_UD_TSO;
669 }
670
Majd Dibbinycff5a0f2016-04-17 17:19:38 +0300671 if (MLX5_CAP_GEN(dev->mdev, eth_net_offloads) &&
Noa Osheroviche8161332017-01-18 15:40:01 +0200672 MLX5_CAP_ETH(dev->mdev, scatter_fcs)) {
673 /* Legacy bit to support old userspace libraries */
Majd Dibbinycff5a0f2016-04-17 17:19:38 +0300674 props->device_cap_flags |= IB_DEVICE_RAW_SCATTER_FCS;
Noa Osheroviche8161332017-01-18 15:40:01 +0200675 props->raw_packet_caps |= IB_RAW_PACKET_CAP_SCATTER_FCS;
676 }
Majd Dibbinycff5a0f2016-04-17 17:19:38 +0300677
Maor Gottliebda6d6ba32016-06-04 15:15:28 +0300678 if (mlx5_get_flow_namespace(dev->mdev, MLX5_FLOW_NAMESPACE_BYPASS))
679 props->device_cap_flags |= IB_DEVICE_MANAGED_FLOW_STEERING;
680
Majd Dibbiny1b5daf12015-06-04 19:30:46 +0300681 props->vendor_part_id = mdev->pdev->device;
682 props->hw_ver = mdev->pdev->revision;
Eli Cohene126ba92013-07-07 17:25:49 +0300683
684 props->max_mr_size = ~0ull;
Sagi Grimberge0238a62015-07-21 14:40:12 +0300685 props->page_size_cap = ~(min_page_size - 1);
Saeed Mahameed938fe832015-05-28 22:28:41 +0300686 props->max_qp = 1 << MLX5_CAP_GEN(mdev, log_max_qp);
687 props->max_qp_wr = 1 << MLX5_CAP_GEN(mdev, log_max_qp_sz);
688 max_rq_sg = MLX5_CAP_GEN(mdev, max_wqe_sz_rq) /
689 sizeof(struct mlx5_wqe_data_seg);
Eli Cohen288c01b2016-10-27 16:36:45 +0300690 max_sq_desc = min_t(int, MLX5_CAP_GEN(mdev, max_wqe_sz_sq), 512);
691 max_sq_sg = (max_sq_desc - sizeof(struct mlx5_wqe_ctrl_seg) -
692 sizeof(struct mlx5_wqe_raddr_seg)) /
693 sizeof(struct mlx5_wqe_data_seg);
Eli Cohene126ba92013-07-07 17:25:49 +0300694 props->max_sge = min(max_rq_sg, max_sq_sg);
Sagi Grimberg986ef952016-03-31 19:03:25 +0300695 props->max_sge_rd = MLX5_MAX_SGE_RD;
Saeed Mahameed938fe832015-05-28 22:28:41 +0300696 props->max_cq = 1 << MLX5_CAP_GEN(mdev, log_max_cq);
Leon Romanovsky9f177682016-01-14 08:11:40 +0200697 props->max_cqe = (1 << MLX5_CAP_GEN(mdev, log_max_cq_sz)) - 1;
Saeed Mahameed938fe832015-05-28 22:28:41 +0300698 props->max_mr = 1 << MLX5_CAP_GEN(mdev, log_max_mkey);
699 props->max_pd = 1 << MLX5_CAP_GEN(mdev, log_max_pd);
700 props->max_qp_rd_atom = 1 << MLX5_CAP_GEN(mdev, log_max_ra_req_qp);
701 props->max_qp_init_rd_atom = 1 << MLX5_CAP_GEN(mdev, log_max_ra_res_qp);
702 props->max_srq = 1 << MLX5_CAP_GEN(mdev, log_max_srq);
703 props->max_srq_wr = (1 << MLX5_CAP_GEN(mdev, log_max_srq_sz)) - 1;
704 props->local_ca_ack_delay = MLX5_CAP_GEN(mdev, local_ca_ack_delay);
Eli Cohene126ba92013-07-07 17:25:49 +0300705 props->max_res_rd_atom = props->max_qp_rd_atom * props->max_qp;
Eli Cohene126ba92013-07-07 17:25:49 +0300706 props->max_srq_sge = max_rq_sg - 1;
Sagi Grimberg911f4332016-03-03 13:37:51 +0200707 props->max_fast_reg_page_list_len =
708 1 << MLX5_CAP_GEN(mdev, log_max_klm_list_size);
Eran Ben Elishada7525d2015-12-14 16:34:10 +0200709 get_atomic_caps(dev, props);
Eli Cohen81bea282013-09-11 16:35:30 +0300710 props->masked_atomic_cap = IB_ATOMIC_NONE;
Saeed Mahameed938fe832015-05-28 22:28:41 +0300711 props->max_mcast_grp = 1 << MLX5_CAP_GEN(mdev, log_max_mcg);
712 props->max_mcast_qp_attach = MLX5_CAP_GEN(mdev, max_qp_mcg);
Eli Cohene126ba92013-07-07 17:25:49 +0300713 props->max_total_mcast_qp_attach = props->max_mcast_qp_attach *
714 props->max_mcast_grp;
715 props->max_map_per_fmr = INT_MAX; /* no limit in ConnectIB */
Maor Gottlieb86695a62016-10-27 16:36:38 +0300716 props->max_ah = INT_MAX;
Matan Barak7c60bcb2015-12-15 20:30:11 +0200717 props->hca_core_clock = MLX5_CAP_GEN(mdev, device_frequency_khz);
718 props->timestamp_mask = 0x7FFFFFFFFFFFFFFFULL;
Eli Cohene126ba92013-07-07 17:25:49 +0300719
Haggai Eran8cdd3122014-12-11 17:04:20 +0200720#ifdef CONFIG_INFINIBAND_ON_DEMAND_PAGING
Saeed Mahameed938fe832015-05-28 22:28:41 +0300721 if (MLX5_CAP_GEN(mdev, pg))
Haggai Eran8cdd3122014-12-11 17:04:20 +0200722 props->device_cap_flags |= IB_DEVICE_ON_DEMAND_PAGING;
723 props->odp_caps = dev->odp_caps;
724#endif
725
Leon Romanovsky051f2632015-12-20 12:16:11 +0200726 if (MLX5_CAP_GEN(mdev, cd))
727 props->device_cap_flags |= IB_DEVICE_CROSS_CHANNEL;
728
Eli Coheneff901d2016-03-11 22:58:42 +0200729 if (!mlx5_core_is_pf(mdev))
730 props->device_cap_flags |= IB_DEVICE_VIRTUAL_FUNCTION;
731
Yishai Hadas31f69a82016-08-28 11:28:45 +0300732 if (mlx5_ib_port_link_layer(ibdev, 1) ==
733 IB_LINK_LAYER_ETHERNET) {
734 props->rss_caps.max_rwq_indirection_tables =
735 1 << MLX5_CAP_GEN(dev->mdev, log_max_rqt);
736 props->rss_caps.max_rwq_indirection_table_size =
737 1 << MLX5_CAP_GEN(dev->mdev, log_max_rqt_size);
738 props->rss_caps.supported_qpts = 1 << IB_QPT_RAW_PACKET;
739 props->max_wq_type_rq =
740 1 << MLX5_CAP_GEN(dev->mdev, log_max_rq);
741 }
742
Bodong Wang7e43a2a2016-10-31 12:16:44 +0200743 if (field_avail(typeof(resp), cqe_comp_caps, uhw->outlen)) {
744 resp.cqe_comp_caps.max_num =
745 MLX5_CAP_GEN(dev->mdev, cqe_compression) ?
746 MLX5_CAP_GEN(dev->mdev, cqe_compression_max_num) : 0;
747 resp.cqe_comp_caps.supported_format =
748 MLX5_IB_CQE_RES_FORMAT_HASH |
749 MLX5_IB_CQE_RES_FORMAT_CSUM;
750 resp.response_length += sizeof(resp.cqe_comp_caps);
751 }
752
Bodong Wangd9491672016-12-01 13:43:13 +0200753 if (field_avail(typeof(resp), packet_pacing_caps, uhw->outlen)) {
754 if (MLX5_CAP_QOS(mdev, packet_pacing) &&
755 MLX5_CAP_GEN(mdev, qos)) {
756 resp.packet_pacing_caps.qp_rate_limit_max =
757 MLX5_CAP_QOS(mdev, packet_pacing_max_rate);
758 resp.packet_pacing_caps.qp_rate_limit_min =
759 MLX5_CAP_QOS(mdev, packet_pacing_min_rate);
760 resp.packet_pacing_caps.supported_qpts |=
761 1 << IB_QPT_RAW_PACKET;
762 }
763 resp.response_length += sizeof(resp.packet_pacing_caps);
764 }
765
Leon Romanovsky9f885202017-01-02 11:37:39 +0200766 if (field_avail(typeof(resp), mlx5_ib_support_multi_pkt_send_wqes,
767 uhw->outlen)) {
768 resp.mlx5_ib_support_multi_pkt_send_wqes =
769 MLX5_CAP_ETH(mdev, multi_pkt_send_wqe);
770 resp.response_length +=
771 sizeof(resp.mlx5_ib_support_multi_pkt_send_wqes);
772 }
773
774 if (field_avail(typeof(resp), reserved, uhw->outlen))
775 resp.response_length += sizeof(resp.reserved);
776
Bodong Wang402ca532016-06-17 15:02:20 +0300777 if (uhw->outlen) {
778 err = ib_copy_to_udata(uhw, &resp, resp.response_length);
779
780 if (err)
781 return err;
782 }
783
Majd Dibbiny1b5daf12015-06-04 19:30:46 +0300784 return 0;
785}
Eli Cohene126ba92013-07-07 17:25:49 +0300786
Majd Dibbiny1b5daf12015-06-04 19:30:46 +0300787enum mlx5_ib_width {
788 MLX5_IB_WIDTH_1X = 1 << 0,
789 MLX5_IB_WIDTH_2X = 1 << 1,
790 MLX5_IB_WIDTH_4X = 1 << 2,
791 MLX5_IB_WIDTH_8X = 1 << 3,
792 MLX5_IB_WIDTH_12X = 1 << 4
793};
794
795static int translate_active_width(struct ib_device *ibdev, u8 active_width,
796 u8 *ib_width)
797{
798 struct mlx5_ib_dev *dev = to_mdev(ibdev);
799 int err = 0;
800
801 if (active_width & MLX5_IB_WIDTH_1X) {
802 *ib_width = IB_WIDTH_1X;
803 } else if (active_width & MLX5_IB_WIDTH_2X) {
804 mlx5_ib_dbg(dev, "active_width %d is not supported by IB spec\n",
805 (int)active_width);
806 err = -EINVAL;
807 } else if (active_width & MLX5_IB_WIDTH_4X) {
808 *ib_width = IB_WIDTH_4X;
809 } else if (active_width & MLX5_IB_WIDTH_8X) {
810 *ib_width = IB_WIDTH_8X;
811 } else if (active_width & MLX5_IB_WIDTH_12X) {
812 *ib_width = IB_WIDTH_12X;
813 } else {
814 mlx5_ib_dbg(dev, "Invalid active_width %d\n",
815 (int)active_width);
816 err = -EINVAL;
817 }
818
819 return err;
820}
821
822static int mlx5_mtu_to_ib_mtu(int mtu)
823{
824 switch (mtu) {
825 case 256: return 1;
826 case 512: return 2;
827 case 1024: return 3;
828 case 2048: return 4;
829 case 4096: return 5;
830 default:
831 pr_warn("invalid mtu\n");
832 return -1;
833 }
834}
835
836enum ib_max_vl_num {
837 __IB_MAX_VL_0 = 1,
838 __IB_MAX_VL_0_1 = 2,
839 __IB_MAX_VL_0_3 = 3,
840 __IB_MAX_VL_0_7 = 4,
841 __IB_MAX_VL_0_14 = 5,
842};
843
844enum mlx5_vl_hw_cap {
845 MLX5_VL_HW_0 = 1,
846 MLX5_VL_HW_0_1 = 2,
847 MLX5_VL_HW_0_2 = 3,
848 MLX5_VL_HW_0_3 = 4,
849 MLX5_VL_HW_0_4 = 5,
850 MLX5_VL_HW_0_5 = 6,
851 MLX5_VL_HW_0_6 = 7,
852 MLX5_VL_HW_0_7 = 8,
853 MLX5_VL_HW_0_14 = 15
854};
855
856static int translate_max_vl_num(struct ib_device *ibdev, u8 vl_hw_cap,
857 u8 *max_vl_num)
858{
859 switch (vl_hw_cap) {
860 case MLX5_VL_HW_0:
861 *max_vl_num = __IB_MAX_VL_0;
862 break;
863 case MLX5_VL_HW_0_1:
864 *max_vl_num = __IB_MAX_VL_0_1;
865 break;
866 case MLX5_VL_HW_0_3:
867 *max_vl_num = __IB_MAX_VL_0_3;
868 break;
869 case MLX5_VL_HW_0_7:
870 *max_vl_num = __IB_MAX_VL_0_7;
871 break;
872 case MLX5_VL_HW_0_14:
873 *max_vl_num = __IB_MAX_VL_0_14;
874 break;
875
876 default:
877 return -EINVAL;
878 }
879
880 return 0;
881}
882
883static int mlx5_query_hca_port(struct ib_device *ibdev, u8 port,
884 struct ib_port_attr *props)
885{
886 struct mlx5_ib_dev *dev = to_mdev(ibdev);
887 struct mlx5_core_dev *mdev = dev->mdev;
888 struct mlx5_hca_vport_context *rep;
Saeed Mahameed046339e2016-04-22 00:33:03 +0300889 u16 max_mtu;
890 u16 oper_mtu;
Majd Dibbiny1b5daf12015-06-04 19:30:46 +0300891 int err;
892 u8 ib_link_width_oper;
893 u8 vl_hw_cap;
894
895 rep = kzalloc(sizeof(*rep), GFP_KERNEL);
896 if (!rep) {
897 err = -ENOMEM;
898 goto out;
899 }
900
Or Gerlitzc4550c62017-01-24 13:02:39 +0200901 /* props being zeroed by the caller, avoid zeroing it here */
Majd Dibbiny1b5daf12015-06-04 19:30:46 +0300902
903 err = mlx5_query_hca_vport_context(mdev, 0, port, 0, rep);
904 if (err)
905 goto out;
906
907 props->lid = rep->lid;
908 props->lmc = rep->lmc;
909 props->sm_lid = rep->sm_lid;
910 props->sm_sl = rep->sm_sl;
911 props->state = rep->vport_state;
912 props->phys_state = rep->port_physical_state;
913 props->port_cap_flags = rep->cap_mask1;
914 props->gid_tbl_len = mlx5_get_gid_table_len(MLX5_CAP_GEN(mdev, gid_table_size));
915 props->max_msg_sz = 1 << MLX5_CAP_GEN(mdev, log_max_msg);
916 props->pkey_tbl_len = mlx5_to_sw_pkey_sz(MLX5_CAP_GEN(mdev, pkey_table_size));
917 props->bad_pkey_cntr = rep->pkey_violation_counter;
918 props->qkey_viol_cntr = rep->qkey_violation_counter;
919 props->subnet_timeout = rep->subnet_timeout;
920 props->init_type_reply = rep->init_type_reply;
Eli Coheneff901d2016-03-11 22:58:42 +0200921 props->grh_required = rep->grh_required;
Majd Dibbiny1b5daf12015-06-04 19:30:46 +0300922
923 err = mlx5_query_port_link_width_oper(mdev, &ib_link_width_oper, port);
924 if (err)
925 goto out;
926
927 err = translate_active_width(ibdev, ib_link_width_oper,
928 &props->active_width);
929 if (err)
930 goto out;
Noa Osherovichd5beb7f2016-06-02 10:47:53 +0300931 err = mlx5_query_port_ib_proto_oper(mdev, &props->active_speed, port);
Majd Dibbiny1b5daf12015-06-04 19:30:46 +0300932 if (err)
933 goto out;
934
Saeed Mahameedfacc9692015-06-11 14:47:27 +0300935 mlx5_query_port_max_mtu(mdev, &max_mtu, port);
Majd Dibbiny1b5daf12015-06-04 19:30:46 +0300936
937 props->max_mtu = mlx5_mtu_to_ib_mtu(max_mtu);
938
Saeed Mahameedfacc9692015-06-11 14:47:27 +0300939 mlx5_query_port_oper_mtu(mdev, &oper_mtu, port);
Majd Dibbiny1b5daf12015-06-04 19:30:46 +0300940
941 props->active_mtu = mlx5_mtu_to_ib_mtu(oper_mtu);
942
943 err = mlx5_query_port_vl_hw_cap(mdev, &vl_hw_cap, port);
944 if (err)
945 goto out;
946
947 err = translate_max_vl_num(ibdev, vl_hw_cap,
948 &props->max_vl_num);
949out:
950 kfree(rep);
Eli Cohene126ba92013-07-07 17:25:49 +0300951 return err;
952}
953
954int mlx5_ib_query_port(struct ib_device *ibdev, u8 port,
955 struct ib_port_attr *props)
956{
Ilan Tayari095b0922017-05-14 16:04:30 +0300957 unsigned int count;
958 int ret;
959
Majd Dibbiny1b5daf12015-06-04 19:30:46 +0300960 switch (mlx5_get_vport_access_method(ibdev)) {
961 case MLX5_VPORT_ACCESS_METHOD_MAD:
Ilan Tayari095b0922017-05-14 16:04:30 +0300962 ret = mlx5_query_mad_ifc_port(ibdev, port, props);
963 break;
Eli Cohene126ba92013-07-07 17:25:49 +0300964
Majd Dibbiny1b5daf12015-06-04 19:30:46 +0300965 case MLX5_VPORT_ACCESS_METHOD_HCA:
Ilan Tayari095b0922017-05-14 16:04:30 +0300966 ret = mlx5_query_hca_port(ibdev, port, props);
967 break;
Majd Dibbiny1b5daf12015-06-04 19:30:46 +0300968
Achiad Shochat3f89a642015-12-23 18:47:21 +0200969 case MLX5_VPORT_ACCESS_METHOD_NIC:
Ilan Tayari095b0922017-05-14 16:04:30 +0300970 ret = mlx5_query_port_roce(ibdev, port, props);
971 break;
Achiad Shochat3f89a642015-12-23 18:47:21 +0200972
Majd Dibbiny1b5daf12015-06-04 19:30:46 +0300973 default:
Ilan Tayari095b0922017-05-14 16:04:30 +0300974 ret = -EINVAL;
Eli Cohene126ba92013-07-07 17:25:49 +0300975 }
Ilan Tayari095b0922017-05-14 16:04:30 +0300976
977 if (!ret && props) {
978 count = mlx5_core_reserved_gids_count(to_mdev(ibdev)->mdev);
979 props->gid_tbl_len -= count;
980 }
981 return ret;
Eli Cohene126ba92013-07-07 17:25:49 +0300982}
983
984static int mlx5_ib_query_gid(struct ib_device *ibdev, u8 port, int index,
985 union ib_gid *gid)
986{
Majd Dibbiny1b5daf12015-06-04 19:30:46 +0300987 struct mlx5_ib_dev *dev = to_mdev(ibdev);
988 struct mlx5_core_dev *mdev = dev->mdev;
Eli Cohene126ba92013-07-07 17:25:49 +0300989
Majd Dibbiny1b5daf12015-06-04 19:30:46 +0300990 switch (mlx5_get_vport_access_method(ibdev)) {
991 case MLX5_VPORT_ACCESS_METHOD_MAD:
992 return mlx5_query_mad_ifc_gids(ibdev, port, index, gid);
Eli Cohene126ba92013-07-07 17:25:49 +0300993
Majd Dibbiny1b5daf12015-06-04 19:30:46 +0300994 case MLX5_VPORT_ACCESS_METHOD_HCA:
995 return mlx5_query_hca_vport_gid(mdev, 0, port, 0, index, gid);
Eli Cohene126ba92013-07-07 17:25:49 +0300996
Majd Dibbiny1b5daf12015-06-04 19:30:46 +0300997 default:
998 return -EINVAL;
999 }
Eli Cohene126ba92013-07-07 17:25:49 +03001000
Eli Cohene126ba92013-07-07 17:25:49 +03001001}
1002
1003static int mlx5_ib_query_pkey(struct ib_device *ibdev, u8 port, u16 index,
1004 u16 *pkey)
1005{
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03001006 struct mlx5_ib_dev *dev = to_mdev(ibdev);
1007 struct mlx5_core_dev *mdev = dev->mdev;
Eli Cohene126ba92013-07-07 17:25:49 +03001008
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03001009 switch (mlx5_get_vport_access_method(ibdev)) {
1010 case MLX5_VPORT_ACCESS_METHOD_MAD:
1011 return mlx5_query_mad_ifc_pkey(ibdev, port, index, pkey);
Eli Cohene126ba92013-07-07 17:25:49 +03001012
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03001013 case MLX5_VPORT_ACCESS_METHOD_HCA:
1014 case MLX5_VPORT_ACCESS_METHOD_NIC:
1015 return mlx5_query_hca_vport_pkey(mdev, 0, port, 0, index,
1016 pkey);
1017 default:
1018 return -EINVAL;
1019 }
Eli Cohene126ba92013-07-07 17:25:49 +03001020}
1021
Eli Cohene126ba92013-07-07 17:25:49 +03001022static int mlx5_ib_modify_device(struct ib_device *ibdev, int mask,
1023 struct ib_device_modify *props)
1024{
1025 struct mlx5_ib_dev *dev = to_mdev(ibdev);
1026 struct mlx5_reg_node_desc in;
1027 struct mlx5_reg_node_desc out;
1028 int err;
1029
1030 if (mask & ~IB_DEVICE_MODIFY_NODE_DESC)
1031 return -EOPNOTSUPP;
1032
1033 if (!(mask & IB_DEVICE_MODIFY_NODE_DESC))
1034 return 0;
1035
1036 /*
1037 * If possible, pass node desc to FW, so it can generate
1038 * a 144 trap. If cmd fails, just ignore.
1039 */
Yuval Shaiabd99fde2016-08-25 10:57:07 -07001040 memcpy(&in, props->node_desc, IB_DEVICE_NODE_DESC_MAX);
Jack Morgenstein9603b612014-07-28 23:30:22 +03001041 err = mlx5_core_access_reg(dev->mdev, &in, sizeof(in), &out,
Eli Cohene126ba92013-07-07 17:25:49 +03001042 sizeof(out), MLX5_REG_NODE_DESC, 0, 1);
1043 if (err)
1044 return err;
1045
Yuval Shaiabd99fde2016-08-25 10:57:07 -07001046 memcpy(ibdev->node_desc, props->node_desc, IB_DEVICE_NODE_DESC_MAX);
Eli Cohene126ba92013-07-07 17:25:49 +03001047
1048 return err;
1049}
1050
Eli Cohencdbe33d2017-02-14 07:25:38 +02001051static int set_port_caps_atomic(struct mlx5_ib_dev *dev, u8 port_num, u32 mask,
1052 u32 value)
1053{
1054 struct mlx5_hca_vport_context ctx = {};
1055 int err;
1056
1057 err = mlx5_query_hca_vport_context(dev->mdev, 0,
1058 port_num, 0, &ctx);
1059 if (err)
1060 return err;
1061
1062 if (~ctx.cap_mask1_perm & mask) {
1063 mlx5_ib_warn(dev, "trying to change bitmask 0x%X but change supported 0x%X\n",
1064 mask, ctx.cap_mask1_perm);
1065 return -EINVAL;
1066 }
1067
1068 ctx.cap_mask1 = value;
1069 ctx.cap_mask1_perm = mask;
1070 err = mlx5_core_modify_hca_vport_context(dev->mdev, 0,
1071 port_num, 0, &ctx);
1072
1073 return err;
1074}
1075
Eli Cohene126ba92013-07-07 17:25:49 +03001076static int mlx5_ib_modify_port(struct ib_device *ibdev, u8 port, int mask,
1077 struct ib_port_modify *props)
1078{
1079 struct mlx5_ib_dev *dev = to_mdev(ibdev);
1080 struct ib_port_attr attr;
1081 u32 tmp;
1082 int err;
Eli Cohencdbe33d2017-02-14 07:25:38 +02001083 u32 change_mask;
1084 u32 value;
1085 bool is_ib = (mlx5_ib_port_link_layer(ibdev, port) ==
1086 IB_LINK_LAYER_INFINIBAND);
1087
Majd Dibbinyec255872017-08-23 08:35:42 +03001088 /* CM layer calls ib_modify_port() regardless of the link layer. For
1089 * Ethernet ports, qkey violation and Port capabilities are meaningless.
1090 */
1091 if (!is_ib)
1092 return 0;
1093
Eli Cohencdbe33d2017-02-14 07:25:38 +02001094 if (MLX5_CAP_GEN(dev->mdev, ib_virt) && is_ib) {
1095 change_mask = props->clr_port_cap_mask | props->set_port_cap_mask;
1096 value = ~props->clr_port_cap_mask | props->set_port_cap_mask;
1097 return set_port_caps_atomic(dev, port, change_mask, value);
1098 }
Eli Cohene126ba92013-07-07 17:25:49 +03001099
1100 mutex_lock(&dev->cap_mask_mutex);
1101
Or Gerlitzc4550c62017-01-24 13:02:39 +02001102 err = ib_query_port(ibdev, port, &attr);
Eli Cohene126ba92013-07-07 17:25:49 +03001103 if (err)
1104 goto out;
1105
1106 tmp = (attr.port_cap_flags | props->set_port_cap_mask) &
1107 ~props->clr_port_cap_mask;
1108
Jack Morgenstein9603b612014-07-28 23:30:22 +03001109 err = mlx5_set_port_caps(dev->mdev, port, tmp);
Eli Cohene126ba92013-07-07 17:25:49 +03001110
1111out:
1112 mutex_unlock(&dev->cap_mask_mutex);
1113 return err;
1114}
1115
Eli Cohen30aa60b2017-01-03 23:55:27 +02001116static void print_lib_caps(struct mlx5_ib_dev *dev, u64 caps)
1117{
1118 mlx5_ib_dbg(dev, "MLX5_LIB_CAP_4K_UAR = %s\n",
1119 caps & MLX5_LIB_CAP_4K_UAR ? "y" : "n");
1120}
1121
Eli Cohenb037c292017-01-03 23:55:26 +02001122static int calc_total_bfregs(struct mlx5_ib_dev *dev, bool lib_uar_4k,
1123 struct mlx5_ib_alloc_ucontext_req_v2 *req,
1124 u32 *num_sys_pages)
1125{
1126 int uars_per_sys_page;
1127 int bfregs_per_sys_page;
1128 int ref_bfregs = req->total_num_bfregs;
1129
1130 if (req->total_num_bfregs == 0)
1131 return -EINVAL;
1132
1133 BUILD_BUG_ON(MLX5_MAX_BFREGS % MLX5_NON_FP_BFREGS_IN_PAGE);
1134 BUILD_BUG_ON(MLX5_MAX_BFREGS < MLX5_NON_FP_BFREGS_IN_PAGE);
1135
1136 if (req->total_num_bfregs > MLX5_MAX_BFREGS)
1137 return -ENOMEM;
1138
1139 uars_per_sys_page = get_uars_per_sys_page(dev, lib_uar_4k);
1140 bfregs_per_sys_page = uars_per_sys_page * MLX5_NON_FP_BFREGS_PER_UAR;
1141 req->total_num_bfregs = ALIGN(req->total_num_bfregs, bfregs_per_sys_page);
1142 *num_sys_pages = req->total_num_bfregs / bfregs_per_sys_page;
1143
1144 if (req->num_low_latency_bfregs > req->total_num_bfregs - 1)
1145 return -EINVAL;
1146
1147 mlx5_ib_dbg(dev, "uar_4k: fw support %s, lib support %s, user requested %d bfregs, alloated %d, using %d sys pages\n",
1148 MLX5_CAP_GEN(dev->mdev, uar_4k) ? "yes" : "no",
1149 lib_uar_4k ? "yes" : "no", ref_bfregs,
1150 req->total_num_bfregs, *num_sys_pages);
1151
1152 return 0;
1153}
1154
1155static int allocate_uars(struct mlx5_ib_dev *dev, struct mlx5_ib_ucontext *context)
1156{
1157 struct mlx5_bfreg_info *bfregi;
1158 int err;
1159 int i;
1160
1161 bfregi = &context->bfregi;
1162 for (i = 0; i < bfregi->num_sys_pages; i++) {
1163 err = mlx5_cmd_alloc_uar(dev->mdev, &bfregi->sys_pages[i]);
1164 if (err)
1165 goto error;
1166
1167 mlx5_ib_dbg(dev, "allocated uar %d\n", bfregi->sys_pages[i]);
1168 }
1169 return 0;
1170
1171error:
1172 for (--i; i >= 0; i--)
1173 if (mlx5_cmd_free_uar(dev->mdev, bfregi->sys_pages[i]))
1174 mlx5_ib_warn(dev, "failed to free uar %d\n", i);
1175
1176 return err;
1177}
1178
1179static int deallocate_uars(struct mlx5_ib_dev *dev, struct mlx5_ib_ucontext *context)
1180{
1181 struct mlx5_bfreg_info *bfregi;
1182 int err;
1183 int i;
1184
1185 bfregi = &context->bfregi;
1186 for (i = 0; i < bfregi->num_sys_pages; i++) {
1187 err = mlx5_cmd_free_uar(dev->mdev, bfregi->sys_pages[i]);
1188 if (err) {
1189 mlx5_ib_warn(dev, "failed to free uar %d\n", i);
1190 return err;
1191 }
1192 }
1193 return 0;
1194}
1195
Eli Cohene126ba92013-07-07 17:25:49 +03001196static struct ib_ucontext *mlx5_ib_alloc_ucontext(struct ib_device *ibdev,
1197 struct ib_udata *udata)
1198{
1199 struct mlx5_ib_dev *dev = to_mdev(ibdev);
Matan Barakb368d7c2015-12-15 20:30:12 +02001200 struct mlx5_ib_alloc_ucontext_req_v2 req = {};
1201 struct mlx5_ib_alloc_ucontext_resp resp = {};
Eli Cohene126ba92013-07-07 17:25:49 +03001202 struct mlx5_ib_ucontext *context;
Eli Cohen2f5ff262017-01-03 23:55:21 +02001203 struct mlx5_bfreg_info *bfregi;
Eli Cohen78c0f982014-01-30 13:49:48 +02001204 int ver;
Eli Cohene126ba92013-07-07 17:25:49 +03001205 int err;
Jack Morgensteinf241e742014-07-28 23:30:23 +03001206 size_t reqlen;
Majd Dibbinya168a41c2016-01-28 17:51:47 +02001207 size_t min_req_v2 = offsetof(struct mlx5_ib_alloc_ucontext_req_v2,
1208 max_cqe_version);
Eli Cohenb037c292017-01-03 23:55:26 +02001209 bool lib_uar_4k;
Eli Cohene126ba92013-07-07 17:25:49 +03001210
1211 if (!dev->ib_active)
1212 return ERR_PTR(-EAGAIN);
1213
Haggai Abramovskydfbee852016-01-14 19:12:56 +02001214 if (udata->inlen < sizeof(struct ib_uverbs_cmd_hdr))
1215 return ERR_PTR(-EINVAL);
1216
Eli Cohen78c0f982014-01-30 13:49:48 +02001217 reqlen = udata->inlen - sizeof(struct ib_uverbs_cmd_hdr);
1218 if (reqlen == sizeof(struct mlx5_ib_alloc_ucontext_req))
1219 ver = 0;
Majd Dibbinya168a41c2016-01-28 17:51:47 +02001220 else if (reqlen >= min_req_v2)
Eli Cohen78c0f982014-01-30 13:49:48 +02001221 ver = 2;
1222 else
1223 return ERR_PTR(-EINVAL);
1224
Matan Barakb368d7c2015-12-15 20:30:12 +02001225 err = ib_copy_from_udata(&req, udata, min(reqlen, sizeof(req)));
Eli Cohene126ba92013-07-07 17:25:49 +03001226 if (err)
1227 return ERR_PTR(err);
1228
Matan Barakb368d7c2015-12-15 20:30:12 +02001229 if (req.flags)
Eli Cohen78c0f982014-01-30 13:49:48 +02001230 return ERR_PTR(-EINVAL);
1231
Haggai Abramovskyf72300c2016-01-14 19:12:58 +02001232 if (req.comp_mask || req.reserved0 || req.reserved1 || req.reserved2)
Matan Barakb368d7c2015-12-15 20:30:12 +02001233 return ERR_PTR(-EOPNOTSUPP);
1234
Eli Cohen2f5ff262017-01-03 23:55:21 +02001235 req.total_num_bfregs = ALIGN(req.total_num_bfregs,
1236 MLX5_NON_FP_BFREGS_PER_UAR);
1237 if (req.num_low_latency_bfregs > req.total_num_bfregs - 1)
Eli Cohene126ba92013-07-07 17:25:49 +03001238 return ERR_PTR(-EINVAL);
1239
Saeed Mahameed938fe832015-05-28 22:28:41 +03001240 resp.qp_tab_size = 1 << MLX5_CAP_GEN(dev->mdev, log_max_qp);
Noa Osherovich2cc6ad52016-06-04 15:15:33 +03001241 if (mlx5_core_is_pf(dev->mdev) && MLX5_CAP_GEN(dev->mdev, bf))
1242 resp.bf_reg_size = 1 << MLX5_CAP_GEN(dev->mdev, log_bf_reg_size);
Daniel Jurgensb47bd6e2016-10-25 18:36:24 +03001243 resp.cache_line_size = cache_line_size();
Saeed Mahameed938fe832015-05-28 22:28:41 +03001244 resp.max_sq_desc_sz = MLX5_CAP_GEN(dev->mdev, max_wqe_sz_sq);
1245 resp.max_rq_desc_sz = MLX5_CAP_GEN(dev->mdev, max_wqe_sz_rq);
1246 resp.max_send_wqebb = 1 << MLX5_CAP_GEN(dev->mdev, log_max_qp_sz);
1247 resp.max_recv_wr = 1 << MLX5_CAP_GEN(dev->mdev, log_max_qp_sz);
1248 resp.max_srq_recv_wr = 1 << MLX5_CAP_GEN(dev->mdev, log_max_srq_sz);
Haggai Abramovskyf72300c2016-01-14 19:12:58 +02001249 resp.cqe_version = min_t(__u8,
1250 (__u8)MLX5_CAP_GEN(dev->mdev, cqe_version),
1251 req.max_cqe_version);
Eli Cohen30aa60b2017-01-03 23:55:27 +02001252 resp.log_uar_size = MLX5_CAP_GEN(dev->mdev, uar_4k) ?
1253 MLX5_ADAPTER_PAGE_SHIFT : PAGE_SHIFT;
1254 resp.num_uars_per_page = MLX5_CAP_GEN(dev->mdev, uar_4k) ?
1255 MLX5_CAP_GEN(dev->mdev, num_of_uars_per_page) : 1;
Matan Barakb368d7c2015-12-15 20:30:12 +02001256 resp.response_length = min(offsetof(typeof(resp), response_length) +
1257 sizeof(resp.response_length), udata->outlen);
Eli Cohene126ba92013-07-07 17:25:49 +03001258
1259 context = kzalloc(sizeof(*context), GFP_KERNEL);
1260 if (!context)
1261 return ERR_PTR(-ENOMEM);
1262
Eli Cohen30aa60b2017-01-03 23:55:27 +02001263 lib_uar_4k = req.lib_caps & MLX5_LIB_CAP_4K_UAR;
Eli Cohen2f5ff262017-01-03 23:55:21 +02001264 bfregi = &context->bfregi;
Eli Cohenb037c292017-01-03 23:55:26 +02001265
1266 /* updates req->total_num_bfregs */
1267 err = calc_total_bfregs(dev, lib_uar_4k, &req, &bfregi->num_sys_pages);
1268 if (err)
1269 goto out_ctx;
1270
Eli Cohen2f5ff262017-01-03 23:55:21 +02001271 mutex_init(&bfregi->lock);
Eli Cohenb037c292017-01-03 23:55:26 +02001272 bfregi->lib_uar_4k = lib_uar_4k;
1273 bfregi->count = kcalloc(req.total_num_bfregs, sizeof(*bfregi->count),
1274 GFP_KERNEL);
1275 if (!bfregi->count) {
Eli Cohene126ba92013-07-07 17:25:49 +03001276 err = -ENOMEM;
1277 goto out_ctx;
1278 }
1279
Eli Cohenb037c292017-01-03 23:55:26 +02001280 bfregi->sys_pages = kcalloc(bfregi->num_sys_pages,
1281 sizeof(*bfregi->sys_pages),
1282 GFP_KERNEL);
1283 if (!bfregi->sys_pages) {
Eli Cohene126ba92013-07-07 17:25:49 +03001284 err = -ENOMEM;
Eli Cohenb037c292017-01-03 23:55:26 +02001285 goto out_count;
Eli Cohene126ba92013-07-07 17:25:49 +03001286 }
1287
Eli Cohenb037c292017-01-03 23:55:26 +02001288 err = allocate_uars(dev, context);
1289 if (err)
1290 goto out_sys_pages;
Eli Cohene126ba92013-07-07 17:25:49 +03001291
Haggai Eranb4cfe442014-12-11 17:04:26 +02001292#ifdef CONFIG_INFINIBAND_ON_DEMAND_PAGING
1293 context->ibucontext.invalidate_range = &mlx5_ib_invalidate_range;
1294#endif
1295
Artemy Kovalyov7d0cc6e2017-01-02 11:37:44 +02001296 context->upd_xlt_page = __get_free_page(GFP_KERNEL);
1297 if (!context->upd_xlt_page) {
1298 err = -ENOMEM;
1299 goto out_uars;
1300 }
1301 mutex_init(&context->upd_xlt_page_mutex);
1302
majd@mellanox.com146d2f12016-01-14 19:13:02 +02001303 if (MLX5_CAP_GEN(dev->mdev, log_max_transport_domain)) {
1304 err = mlx5_core_alloc_transport_domain(dev->mdev,
1305 &context->tdn);
1306 if (err)
Artemy Kovalyov7d0cc6e2017-01-02 11:37:44 +02001307 goto out_page;
majd@mellanox.com146d2f12016-01-14 19:13:02 +02001308 }
1309
Maor Gottlieb7c2344c2016-06-17 14:56:44 +03001310 INIT_LIST_HEAD(&context->vma_private_list);
Eli Cohene126ba92013-07-07 17:25:49 +03001311 INIT_LIST_HEAD(&context->db_page_list);
1312 mutex_init(&context->db_page_mutex);
1313
Eli Cohen2f5ff262017-01-03 23:55:21 +02001314 resp.tot_bfregs = req.total_num_bfregs;
Saeed Mahameed938fe832015-05-28 22:28:41 +03001315 resp.num_ports = MLX5_CAP_GEN(dev->mdev, num_ports);
Matan Barakb368d7c2015-12-15 20:30:12 +02001316
Haggai Abramovskyf72300c2016-01-14 19:12:58 +02001317 if (field_avail(typeof(resp), cqe_version, udata->outlen))
1318 resp.response_length += sizeof(resp.cqe_version);
Matan Barakb368d7c2015-12-15 20:30:12 +02001319
Bodong Wang402ca532016-06-17 15:02:20 +03001320 if (field_avail(typeof(resp), cmds_supp_uhw, udata->outlen)) {
Moni Shoua6ad279c52016-11-23 08:23:23 +02001321 resp.cmds_supp_uhw |= MLX5_USER_CMDS_SUPP_UHW_QUERY_DEVICE |
1322 MLX5_USER_CMDS_SUPP_UHW_CREATE_AH;
Bodong Wang402ca532016-06-17 15:02:20 +03001323 resp.response_length += sizeof(resp.cmds_supp_uhw);
1324 }
1325
Or Gerlitz78984892016-11-30 20:33:33 +02001326 if (field_avail(typeof(resp), eth_min_inline, udata->outlen)) {
1327 if (mlx5_ib_port_link_layer(ibdev, 1) == IB_LINK_LAYER_ETHERNET) {
1328 mlx5_query_min_inline(dev->mdev, &resp.eth_min_inline);
1329 resp.eth_min_inline++;
1330 }
1331 resp.response_length += sizeof(resp.eth_min_inline);
1332 }
1333
Noa Osherovichbc5c6ee2016-06-04 15:15:31 +03001334 /*
1335 * We don't want to expose information from the PCI bar that is located
1336 * after 4096 bytes, so if the arch only supports larger pages, let's
1337 * pretend we don't support reading the HCA's core clock. This is also
1338 * forced by mmap function.
1339 */
Eli Cohende8d6e02017-01-03 23:55:19 +02001340 if (field_avail(typeof(resp), hca_core_clock_offset, udata->outlen)) {
1341 if (PAGE_SIZE <= 4096) {
1342 resp.comp_mask |=
1343 MLX5_IB_ALLOC_UCONTEXT_RESP_MASK_CORE_CLOCK_OFFSET;
1344 resp.hca_core_clock_offset =
1345 offsetof(struct mlx5_init_seg, internal_timer_h) % PAGE_SIZE;
1346 }
Haggai Abramovskyf72300c2016-01-14 19:12:58 +02001347 resp.response_length += sizeof(resp.hca_core_clock_offset) +
Bodong Wang402ca532016-06-17 15:02:20 +03001348 sizeof(resp.reserved2);
Matan Barakb368d7c2015-12-15 20:30:12 +02001349 }
1350
Eli Cohen30aa60b2017-01-03 23:55:27 +02001351 if (field_avail(typeof(resp), log_uar_size, udata->outlen))
1352 resp.response_length += sizeof(resp.log_uar_size);
1353
1354 if (field_avail(typeof(resp), num_uars_per_page, udata->outlen))
1355 resp.response_length += sizeof(resp.num_uars_per_page);
1356
Matan Barakb368d7c2015-12-15 20:30:12 +02001357 err = ib_copy_to_udata(udata, &resp, resp.response_length);
Eli Cohene126ba92013-07-07 17:25:49 +03001358 if (err)
majd@mellanox.com146d2f12016-01-14 19:13:02 +02001359 goto out_td;
Eli Cohene126ba92013-07-07 17:25:49 +03001360
Eli Cohen2f5ff262017-01-03 23:55:21 +02001361 bfregi->ver = ver;
1362 bfregi->num_low_latency_bfregs = req.num_low_latency_bfregs;
Haggai Abramovskyf72300c2016-01-14 19:12:58 +02001363 context->cqe_version = resp.cqe_version;
Eli Cohen30aa60b2017-01-03 23:55:27 +02001364 context->lib_caps = req.lib_caps;
1365 print_lib_caps(dev, context->lib_caps);
Haggai Abramovskyf72300c2016-01-14 19:12:58 +02001366
Eli Cohene126ba92013-07-07 17:25:49 +03001367 return &context->ibucontext;
1368
majd@mellanox.com146d2f12016-01-14 19:13:02 +02001369out_td:
1370 if (MLX5_CAP_GEN(dev->mdev, log_max_transport_domain))
1371 mlx5_core_dealloc_transport_domain(dev->mdev, context->tdn);
1372
Artemy Kovalyov7d0cc6e2017-01-02 11:37:44 +02001373out_page:
1374 free_page(context->upd_xlt_page);
1375
Eli Cohene126ba92013-07-07 17:25:49 +03001376out_uars:
Eli Cohenb037c292017-01-03 23:55:26 +02001377 deallocate_uars(dev, context);
1378
1379out_sys_pages:
1380 kfree(bfregi->sys_pages);
1381
Eli Cohene126ba92013-07-07 17:25:49 +03001382out_count:
Eli Cohen2f5ff262017-01-03 23:55:21 +02001383 kfree(bfregi->count);
Eli Cohene126ba92013-07-07 17:25:49 +03001384
Eli Cohene126ba92013-07-07 17:25:49 +03001385out_ctx:
1386 kfree(context);
Eli Cohenb037c292017-01-03 23:55:26 +02001387
Eli Cohene126ba92013-07-07 17:25:49 +03001388 return ERR_PTR(err);
1389}
1390
1391static int mlx5_ib_dealloc_ucontext(struct ib_ucontext *ibcontext)
1392{
1393 struct mlx5_ib_ucontext *context = to_mucontext(ibcontext);
1394 struct mlx5_ib_dev *dev = to_mdev(ibcontext->device);
Eli Cohenb037c292017-01-03 23:55:26 +02001395 struct mlx5_bfreg_info *bfregi;
Eli Cohene126ba92013-07-07 17:25:49 +03001396
Eli Cohenb037c292017-01-03 23:55:26 +02001397 bfregi = &context->bfregi;
majd@mellanox.com146d2f12016-01-14 19:13:02 +02001398 if (MLX5_CAP_GEN(dev->mdev, log_max_transport_domain))
1399 mlx5_core_dealloc_transport_domain(dev->mdev, context->tdn);
1400
Artemy Kovalyov7d0cc6e2017-01-02 11:37:44 +02001401 free_page(context->upd_xlt_page);
Eli Cohenb037c292017-01-03 23:55:26 +02001402 deallocate_uars(dev, context);
1403 kfree(bfregi->sys_pages);
Eli Cohen2f5ff262017-01-03 23:55:21 +02001404 kfree(bfregi->count);
Eli Cohene126ba92013-07-07 17:25:49 +03001405 kfree(context);
1406
1407 return 0;
1408}
1409
Eli Cohenb037c292017-01-03 23:55:26 +02001410static phys_addr_t uar_index2pfn(struct mlx5_ib_dev *dev,
1411 struct mlx5_bfreg_info *bfregi,
1412 int idx)
Eli Cohene126ba92013-07-07 17:25:49 +03001413{
Eli Cohenb037c292017-01-03 23:55:26 +02001414 int fw_uars_per_page;
1415
1416 fw_uars_per_page = MLX5_CAP_GEN(dev->mdev, uar_4k) ? MLX5_UARS_IN_PAGE : 1;
1417
1418 return (pci_resource_start(dev->mdev->pdev, 0) >> PAGE_SHIFT) +
1419 bfregi->sys_pages[idx] / fw_uars_per_page;
Eli Cohene126ba92013-07-07 17:25:49 +03001420}
1421
1422static int get_command(unsigned long offset)
1423{
1424 return (offset >> MLX5_IB_MMAP_CMD_SHIFT) & MLX5_IB_MMAP_CMD_MASK;
1425}
1426
1427static int get_arg(unsigned long offset)
1428{
1429 return offset & ((1 << MLX5_IB_MMAP_CMD_SHIFT) - 1);
1430}
1431
1432static int get_index(unsigned long offset)
1433{
1434 return get_arg(offset);
1435}
1436
Maor Gottlieb7c2344c2016-06-17 14:56:44 +03001437static void mlx5_ib_vma_open(struct vm_area_struct *area)
1438{
1439 /* vma_open is called when a new VMA is created on top of our VMA. This
1440 * is done through either mremap flow or split_vma (usually due to
1441 * mlock, madvise, munmap, etc.) We do not support a clone of the VMA,
1442 * as this VMA is strongly hardware related. Therefore we set the
1443 * vm_ops of the newly created/cloned VMA to NULL, to prevent it from
1444 * calling us again and trying to do incorrect actions. We assume that
1445 * the original VMA size is exactly a single page, and therefore all
1446 * "splitting" operation will not happen to it.
1447 */
1448 area->vm_ops = NULL;
1449}
1450
1451static void mlx5_ib_vma_close(struct vm_area_struct *area)
1452{
1453 struct mlx5_ib_vma_private_data *mlx5_ib_vma_priv_data;
1454
1455 /* It's guaranteed that all VMAs opened on a FD are closed before the
1456 * file itself is closed, therefore no sync is needed with the regular
1457 * closing flow. (e.g. mlx5 ib_dealloc_ucontext)
1458 * However need a sync with accessing the vma as part of
1459 * mlx5_ib_disassociate_ucontext.
1460 * The close operation is usually called under mm->mmap_sem except when
1461 * process is exiting.
1462 * The exiting case is handled explicitly as part of
1463 * mlx5_ib_disassociate_ucontext.
1464 */
1465 mlx5_ib_vma_priv_data = (struct mlx5_ib_vma_private_data *)area->vm_private_data;
1466
1467 /* setting the vma context pointer to null in the mlx5_ib driver's
1468 * private data, to protect a race condition in
1469 * mlx5_ib_disassociate_ucontext().
1470 */
1471 mlx5_ib_vma_priv_data->vma = NULL;
1472 list_del(&mlx5_ib_vma_priv_data->list);
1473 kfree(mlx5_ib_vma_priv_data);
1474}
1475
1476static const struct vm_operations_struct mlx5_ib_vm_ops = {
1477 .open = mlx5_ib_vma_open,
1478 .close = mlx5_ib_vma_close
1479};
1480
1481static int mlx5_ib_set_vma_data(struct vm_area_struct *vma,
1482 struct mlx5_ib_ucontext *ctx)
1483{
1484 struct mlx5_ib_vma_private_data *vma_prv;
1485 struct list_head *vma_head = &ctx->vma_private_list;
1486
1487 vma_prv = kzalloc(sizeof(*vma_prv), GFP_KERNEL);
1488 if (!vma_prv)
1489 return -ENOMEM;
1490
1491 vma_prv->vma = vma;
1492 vma->vm_private_data = vma_prv;
1493 vma->vm_ops = &mlx5_ib_vm_ops;
1494
1495 list_add(&vma_prv->list, vma_head);
1496
1497 return 0;
1498}
1499
1500static void mlx5_ib_disassociate_ucontext(struct ib_ucontext *ibcontext)
1501{
1502 int ret;
1503 struct vm_area_struct *vma;
1504 struct mlx5_ib_vma_private_data *vma_private, *n;
1505 struct mlx5_ib_ucontext *context = to_mucontext(ibcontext);
1506 struct task_struct *owning_process = NULL;
1507 struct mm_struct *owning_mm = NULL;
1508
1509 owning_process = get_pid_task(ibcontext->tgid, PIDTYPE_PID);
1510 if (!owning_process)
1511 return;
1512
1513 owning_mm = get_task_mm(owning_process);
1514 if (!owning_mm) {
1515 pr_info("no mm, disassociate ucontext is pending task termination\n");
1516 while (1) {
1517 put_task_struct(owning_process);
1518 usleep_range(1000, 2000);
1519 owning_process = get_pid_task(ibcontext->tgid,
1520 PIDTYPE_PID);
1521 if (!owning_process ||
1522 owning_process->state == TASK_DEAD) {
1523 pr_info("disassociate ucontext done, task was terminated\n");
1524 /* in case task was dead need to release the
1525 * task struct.
1526 */
1527 if (owning_process)
1528 put_task_struct(owning_process);
1529 return;
1530 }
1531 }
1532 }
1533
1534 /* need to protect from a race on closing the vma as part of
1535 * mlx5_ib_vma_close.
1536 */
Maor Gottliebecc7d832017-03-29 06:03:02 +03001537 down_write(&owning_mm->mmap_sem);
Maor Gottlieb7c2344c2016-06-17 14:56:44 +03001538 list_for_each_entry_safe(vma_private, n, &context->vma_private_list,
1539 list) {
1540 vma = vma_private->vma;
1541 ret = zap_vma_ptes(vma, vma->vm_start,
1542 PAGE_SIZE);
1543 WARN_ONCE(ret, "%s: zap_vma_ptes failed", __func__);
1544 /* context going to be destroyed, should
1545 * not access ops any more.
1546 */
Maor Gottlieb13776612017-03-29 06:03:03 +03001547 vma->vm_flags &= ~(VM_SHARED | VM_MAYSHARE);
Maor Gottlieb7c2344c2016-06-17 14:56:44 +03001548 vma->vm_ops = NULL;
1549 list_del(&vma_private->list);
1550 kfree(vma_private);
1551 }
Maor Gottliebecc7d832017-03-29 06:03:02 +03001552 up_write(&owning_mm->mmap_sem);
Maor Gottlieb7c2344c2016-06-17 14:56:44 +03001553 mmput(owning_mm);
1554 put_task_struct(owning_process);
1555}
1556
Guy Levi37aa5c32016-04-27 16:49:50 +03001557static inline char *mmap_cmd2str(enum mlx5_ib_mmap_cmd cmd)
1558{
1559 switch (cmd) {
1560 case MLX5_IB_MMAP_WC_PAGE:
1561 return "WC";
1562 case MLX5_IB_MMAP_REGULAR_PAGE:
1563 return "best effort WC";
1564 case MLX5_IB_MMAP_NC_PAGE:
1565 return "NC";
1566 default:
1567 return NULL;
1568 }
1569}
1570
1571static int uar_mmap(struct mlx5_ib_dev *dev, enum mlx5_ib_mmap_cmd cmd,
Maor Gottlieb7c2344c2016-06-17 14:56:44 +03001572 struct vm_area_struct *vma,
1573 struct mlx5_ib_ucontext *context)
Guy Levi37aa5c32016-04-27 16:49:50 +03001574{
Eli Cohen2f5ff262017-01-03 23:55:21 +02001575 struct mlx5_bfreg_info *bfregi = &context->bfregi;
Guy Levi37aa5c32016-04-27 16:49:50 +03001576 int err;
1577 unsigned long idx;
1578 phys_addr_t pfn, pa;
1579 pgprot_t prot;
Eli Cohenb037c292017-01-03 23:55:26 +02001580 int uars_per_page;
1581
1582 if (vma->vm_end - vma->vm_start != PAGE_SIZE)
1583 return -EINVAL;
1584
1585 uars_per_page = get_uars_per_sys_page(dev, bfregi->lib_uar_4k);
1586 idx = get_index(vma->vm_pgoff);
1587 if (idx % uars_per_page ||
1588 idx * uars_per_page >= bfregi->num_sys_pages) {
1589 mlx5_ib_warn(dev, "invalid uar index %lu\n", idx);
1590 return -EINVAL;
1591 }
Guy Levi37aa5c32016-04-27 16:49:50 +03001592
1593 switch (cmd) {
1594 case MLX5_IB_MMAP_WC_PAGE:
1595/* Some architectures don't support WC memory */
1596#if defined(CONFIG_X86)
1597 if (!pat_enabled())
1598 return -EPERM;
1599#elif !(defined(CONFIG_PPC) || (defined(CONFIG_ARM) && defined(CONFIG_MMU)))
1600 return -EPERM;
1601#endif
1602 /* fall through */
1603 case MLX5_IB_MMAP_REGULAR_PAGE:
1604 /* For MLX5_IB_MMAP_REGULAR_PAGE do the best effort to get WC */
1605 prot = pgprot_writecombine(vma->vm_page_prot);
1606 break;
1607 case MLX5_IB_MMAP_NC_PAGE:
1608 prot = pgprot_noncached(vma->vm_page_prot);
1609 break;
1610 default:
1611 return -EINVAL;
1612 }
1613
Eli Cohenb037c292017-01-03 23:55:26 +02001614 pfn = uar_index2pfn(dev, bfregi, idx);
Guy Levi37aa5c32016-04-27 16:49:50 +03001615 mlx5_ib_dbg(dev, "uar idx 0x%lx, pfn %pa\n", idx, &pfn);
1616
1617 vma->vm_page_prot = prot;
1618 err = io_remap_pfn_range(vma, vma->vm_start, pfn,
1619 PAGE_SIZE, vma->vm_page_prot);
1620 if (err) {
1621 mlx5_ib_err(dev, "io_remap_pfn_range failed with error=%d, vm_start=0x%lx, pfn=%pa, mmap_cmd=%s\n",
1622 err, vma->vm_start, &pfn, mmap_cmd2str(cmd));
1623 return -EAGAIN;
1624 }
1625
1626 pa = pfn << PAGE_SHIFT;
1627 mlx5_ib_dbg(dev, "mapped %s at 0x%lx, PA %pa\n", mmap_cmd2str(cmd),
1628 vma->vm_start, &pa);
1629
Maor Gottlieb7c2344c2016-06-17 14:56:44 +03001630 return mlx5_ib_set_vma_data(vma, context);
Guy Levi37aa5c32016-04-27 16:49:50 +03001631}
1632
Eli Cohene126ba92013-07-07 17:25:49 +03001633static int mlx5_ib_mmap(struct ib_ucontext *ibcontext, struct vm_area_struct *vma)
1634{
1635 struct mlx5_ib_ucontext *context = to_mucontext(ibcontext);
1636 struct mlx5_ib_dev *dev = to_mdev(ibcontext->device);
Eli Cohene126ba92013-07-07 17:25:49 +03001637 unsigned long command;
Eli Cohene126ba92013-07-07 17:25:49 +03001638 phys_addr_t pfn;
1639
1640 command = get_command(vma->vm_pgoff);
1641 switch (command) {
Guy Levi37aa5c32016-04-27 16:49:50 +03001642 case MLX5_IB_MMAP_WC_PAGE:
1643 case MLX5_IB_MMAP_NC_PAGE:
Eli Cohene126ba92013-07-07 17:25:49 +03001644 case MLX5_IB_MMAP_REGULAR_PAGE:
Maor Gottlieb7c2344c2016-06-17 14:56:44 +03001645 return uar_mmap(dev, command, vma, context);
Eli Cohene126ba92013-07-07 17:25:49 +03001646
1647 case MLX5_IB_MMAP_GET_CONTIGUOUS_PAGES:
1648 return -ENOSYS;
1649
Matan Barakd69e3bc2015-12-15 20:30:13 +02001650 case MLX5_IB_MMAP_CORE_CLOCK:
Matan Barakd69e3bc2015-12-15 20:30:13 +02001651 if (vma->vm_end - vma->vm_start != PAGE_SIZE)
1652 return -EINVAL;
1653
Matan Barak6cbac1e2016-04-14 16:52:10 +03001654 if (vma->vm_flags & VM_WRITE)
Matan Barakd69e3bc2015-12-15 20:30:13 +02001655 return -EPERM;
1656
1657 /* Don't expose to user-space information it shouldn't have */
1658 if (PAGE_SIZE > 4096)
1659 return -EOPNOTSUPP;
1660
1661 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
1662 pfn = (dev->mdev->iseg_base +
1663 offsetof(struct mlx5_init_seg, internal_timer_h)) >>
1664 PAGE_SHIFT;
1665 if (io_remap_pfn_range(vma, vma->vm_start, pfn,
1666 PAGE_SIZE, vma->vm_page_prot))
1667 return -EAGAIN;
1668
1669 mlx5_ib_dbg(dev, "mapped internal timer at 0x%lx, PA 0x%llx\n",
1670 vma->vm_start,
1671 (unsigned long long)pfn << PAGE_SHIFT);
1672 break;
Matan Barakd69e3bc2015-12-15 20:30:13 +02001673
Eli Cohene126ba92013-07-07 17:25:49 +03001674 default:
1675 return -EINVAL;
1676 }
1677
1678 return 0;
1679}
1680
Eli Cohene126ba92013-07-07 17:25:49 +03001681static struct ib_pd *mlx5_ib_alloc_pd(struct ib_device *ibdev,
1682 struct ib_ucontext *context,
1683 struct ib_udata *udata)
1684{
1685 struct mlx5_ib_alloc_pd_resp resp;
1686 struct mlx5_ib_pd *pd;
1687 int err;
1688
1689 pd = kmalloc(sizeof(*pd), GFP_KERNEL);
1690 if (!pd)
1691 return ERR_PTR(-ENOMEM);
1692
Jack Morgenstein9603b612014-07-28 23:30:22 +03001693 err = mlx5_core_alloc_pd(to_mdev(ibdev)->mdev, &pd->pdn);
Eli Cohene126ba92013-07-07 17:25:49 +03001694 if (err) {
1695 kfree(pd);
1696 return ERR_PTR(err);
1697 }
1698
1699 if (context) {
1700 resp.pdn = pd->pdn;
1701 if (ib_copy_to_udata(udata, &resp, sizeof(resp))) {
Jack Morgenstein9603b612014-07-28 23:30:22 +03001702 mlx5_core_dealloc_pd(to_mdev(ibdev)->mdev, pd->pdn);
Eli Cohene126ba92013-07-07 17:25:49 +03001703 kfree(pd);
1704 return ERR_PTR(-EFAULT);
1705 }
Eli Cohene126ba92013-07-07 17:25:49 +03001706 }
1707
1708 return &pd->ibpd;
1709}
1710
1711static int mlx5_ib_dealloc_pd(struct ib_pd *pd)
1712{
1713 struct mlx5_ib_dev *mdev = to_mdev(pd->device);
1714 struct mlx5_ib_pd *mpd = to_mpd(pd);
1715
Jack Morgenstein9603b612014-07-28 23:30:22 +03001716 mlx5_core_dealloc_pd(mdev->mdev, mpd->pdn);
Eli Cohene126ba92013-07-07 17:25:49 +03001717 kfree(mpd);
1718
1719 return 0;
1720}
1721
Maor Gottlieb466fa6d2016-08-30 16:58:36 +03001722enum {
1723 MATCH_CRITERIA_ENABLE_OUTER_BIT,
1724 MATCH_CRITERIA_ENABLE_MISC_BIT,
1725 MATCH_CRITERIA_ENABLE_INNER_BIT
1726};
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02001727
Maor Gottlieb466fa6d2016-08-30 16:58:36 +03001728#define HEADER_IS_ZERO(match_criteria, headers) \
1729 !(memchr_inv(MLX5_ADDR_OF(fte_match_param, match_criteria, headers), \
1730 0, MLX5_FLD_SZ_BYTES(fte_match_param, headers))) \
1731
1732static u8 get_match_criteria_enable(u32 *match_criteria)
1733{
1734 u8 match_criteria_enable;
1735
1736 match_criteria_enable =
1737 (!HEADER_IS_ZERO(match_criteria, outer_headers)) <<
1738 MATCH_CRITERIA_ENABLE_OUTER_BIT;
1739 match_criteria_enable |=
1740 (!HEADER_IS_ZERO(match_criteria, misc_parameters)) <<
1741 MATCH_CRITERIA_ENABLE_MISC_BIT;
1742 match_criteria_enable |=
1743 (!HEADER_IS_ZERO(match_criteria, inner_headers)) <<
1744 MATCH_CRITERIA_ENABLE_INNER_BIT;
1745
1746 return match_criteria_enable;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02001747}
1748
Maor Gottliebca0d4752016-08-30 16:58:35 +03001749static void set_proto(void *outer_c, void *outer_v, u8 mask, u8 val)
1750{
1751 MLX5_SET(fte_match_set_lyr_2_4, outer_c, ip_protocol, mask);
1752 MLX5_SET(fte_match_set_lyr_2_4, outer_v, ip_protocol, val);
1753}
1754
Moses Reuben2d1e6972016-11-14 19:04:52 +02001755static void set_flow_label(void *misc_c, void *misc_v, u8 mask, u8 val,
1756 bool inner)
1757{
1758 if (inner) {
1759 MLX5_SET(fte_match_set_misc,
1760 misc_c, inner_ipv6_flow_label, mask);
1761 MLX5_SET(fte_match_set_misc,
1762 misc_v, inner_ipv6_flow_label, val);
1763 } else {
1764 MLX5_SET(fte_match_set_misc,
1765 misc_c, outer_ipv6_flow_label, mask);
1766 MLX5_SET(fte_match_set_misc,
1767 misc_v, outer_ipv6_flow_label, val);
1768 }
1769}
1770
Maor Gottliebca0d4752016-08-30 16:58:35 +03001771static void set_tos(void *outer_c, void *outer_v, u8 mask, u8 val)
1772{
1773 MLX5_SET(fte_match_set_lyr_2_4, outer_c, ip_ecn, mask);
1774 MLX5_SET(fte_match_set_lyr_2_4, outer_v, ip_ecn, val);
1775 MLX5_SET(fte_match_set_lyr_2_4, outer_c, ip_dscp, mask >> 2);
1776 MLX5_SET(fte_match_set_lyr_2_4, outer_v, ip_dscp, val >> 2);
1777}
1778
Maor Gottliebc47ac6a2016-08-30 16:58:31 +03001779#define LAST_ETH_FIELD vlan_tag
1780#define LAST_IB_FIELD sl
Maor Gottliebca0d4752016-08-30 16:58:35 +03001781#define LAST_IPV4_FIELD tos
Maor Gottlieb466fa6d2016-08-30 16:58:36 +03001782#define LAST_IPV6_FIELD traffic_class
Maor Gottliebc47ac6a2016-08-30 16:58:31 +03001783#define LAST_TCP_UDP_FIELD src_port
Moses Reubenffb30d82016-11-14 19:04:50 +02001784#define LAST_TUNNEL_FIELD tunnel_id
Moses Reuben2ac693f2017-01-18 14:59:50 +02001785#define LAST_FLOW_TAG_FIELD tag_id
Slava Shwartsmana22ed862017-04-03 13:13:52 +03001786#define LAST_DROP_FIELD size
Maor Gottliebc47ac6a2016-08-30 16:58:31 +03001787
1788/* Field is the last supported field */
1789#define FIELDS_NOT_SUPPORTED(filter, field)\
1790 memchr_inv((void *)&filter.field +\
1791 sizeof(filter.field), 0,\
1792 sizeof(filter) -\
1793 offsetof(typeof(filter), field) -\
1794 sizeof(filter.field))
1795
Ariel Levkovich19cc7522017-04-03 13:11:03 +03001796#define IPV4_VERSION 4
1797#define IPV6_VERSION 6
1798static int parse_flow_attr(struct mlx5_core_dev *mdev, u32 *match_c,
1799 u32 *match_v, const union ib_flow_spec *ib_spec,
Slava Shwartsmana22ed862017-04-03 13:13:52 +03001800 u32 *tag_id, bool *is_drop)
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02001801{
Maor Gottlieb466fa6d2016-08-30 16:58:36 +03001802 void *misc_params_c = MLX5_ADDR_OF(fte_match_param, match_c,
1803 misc_parameters);
1804 void *misc_params_v = MLX5_ADDR_OF(fte_match_param, match_v,
1805 misc_parameters);
Moses Reuben2d1e6972016-11-14 19:04:52 +02001806 void *headers_c;
1807 void *headers_v;
Ariel Levkovich19cc7522017-04-03 13:11:03 +03001808 int match_ipv;
Maor Gottlieb466fa6d2016-08-30 16:58:36 +03001809
Moses Reuben2d1e6972016-11-14 19:04:52 +02001810 if (ib_spec->type & IB_FLOW_SPEC_INNER) {
1811 headers_c = MLX5_ADDR_OF(fte_match_param, match_c,
1812 inner_headers);
1813 headers_v = MLX5_ADDR_OF(fte_match_param, match_v,
1814 inner_headers);
Ariel Levkovich19cc7522017-04-03 13:11:03 +03001815 match_ipv = MLX5_CAP_FLOWTABLE_NIC_RX(mdev,
1816 ft_field_support.inner_ip_version);
Moses Reuben2d1e6972016-11-14 19:04:52 +02001817 } else {
1818 headers_c = MLX5_ADDR_OF(fte_match_param, match_c,
1819 outer_headers);
1820 headers_v = MLX5_ADDR_OF(fte_match_param, match_v,
1821 outer_headers);
Ariel Levkovich19cc7522017-04-03 13:11:03 +03001822 match_ipv = MLX5_CAP_FLOWTABLE_NIC_RX(mdev,
1823 ft_field_support.outer_ip_version);
Moses Reuben2d1e6972016-11-14 19:04:52 +02001824 }
1825
1826 switch (ib_spec->type & ~IB_FLOW_SPEC_INNER) {
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02001827 case IB_FLOW_SPEC_ETH:
Maor Gottliebc47ac6a2016-08-30 16:58:31 +03001828 if (FIELDS_NOT_SUPPORTED(ib_spec->eth.mask, LAST_ETH_FIELD))
Leon Romanovsky1ffd3a22017-01-18 14:59:51 +02001829 return -EOPNOTSUPP;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02001830
Moses Reuben2d1e6972016-11-14 19:04:52 +02001831 ether_addr_copy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02001832 dmac_47_16),
1833 ib_spec->eth.mask.dst_mac);
Moses Reuben2d1e6972016-11-14 19:04:52 +02001834 ether_addr_copy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02001835 dmac_47_16),
1836 ib_spec->eth.val.dst_mac);
1837
Moses Reuben2d1e6972016-11-14 19:04:52 +02001838 ether_addr_copy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
Maor Gottliebee3da802016-09-12 19:16:24 +03001839 smac_47_16),
1840 ib_spec->eth.mask.src_mac);
Moses Reuben2d1e6972016-11-14 19:04:52 +02001841 ether_addr_copy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
Maor Gottliebee3da802016-09-12 19:16:24 +03001842 smac_47_16),
1843 ib_spec->eth.val.src_mac);
1844
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02001845 if (ib_spec->eth.mask.vlan_tag) {
Moses Reuben2d1e6972016-11-14 19:04:52 +02001846 MLX5_SET(fte_match_set_lyr_2_4, headers_c,
Mohamad Haj Yahia10543362016-10-09 16:25:43 +03001847 cvlan_tag, 1);
Moses Reuben2d1e6972016-11-14 19:04:52 +02001848 MLX5_SET(fte_match_set_lyr_2_4, headers_v,
Mohamad Haj Yahia10543362016-10-09 16:25:43 +03001849 cvlan_tag, 1);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02001850
Moses Reuben2d1e6972016-11-14 19:04:52 +02001851 MLX5_SET(fte_match_set_lyr_2_4, headers_c,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02001852 first_vid, ntohs(ib_spec->eth.mask.vlan_tag));
Moses Reuben2d1e6972016-11-14 19:04:52 +02001853 MLX5_SET(fte_match_set_lyr_2_4, headers_v,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02001854 first_vid, ntohs(ib_spec->eth.val.vlan_tag));
1855
Moses Reuben2d1e6972016-11-14 19:04:52 +02001856 MLX5_SET(fte_match_set_lyr_2_4, headers_c,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02001857 first_cfi,
1858 ntohs(ib_spec->eth.mask.vlan_tag) >> 12);
Moses Reuben2d1e6972016-11-14 19:04:52 +02001859 MLX5_SET(fte_match_set_lyr_2_4, headers_v,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02001860 first_cfi,
1861 ntohs(ib_spec->eth.val.vlan_tag) >> 12);
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_prio,
1865 ntohs(ib_spec->eth.mask.vlan_tag) >> 13);
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_prio,
1868 ntohs(ib_spec->eth.val.vlan_tag) >> 13);
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 ethertype, ntohs(ib_spec->eth.mask.ether_type));
Moses Reuben2d1e6972016-11-14 19:04:52 +02001872 MLX5_SET(fte_match_set_lyr_2_4, headers_v,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02001873 ethertype, ntohs(ib_spec->eth.val.ether_type));
1874 break;
1875 case IB_FLOW_SPEC_IPV4:
Maor Gottliebc47ac6a2016-08-30 16:58:31 +03001876 if (FIELDS_NOT_SUPPORTED(ib_spec->ipv4.mask, LAST_IPV4_FIELD))
Leon Romanovsky1ffd3a22017-01-18 14:59:51 +02001877 return -EOPNOTSUPP;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02001878
Ariel Levkovich19cc7522017-04-03 13:11:03 +03001879 if (match_ipv) {
1880 MLX5_SET(fte_match_set_lyr_2_4, headers_c,
1881 ip_version, 0xf);
1882 MLX5_SET(fte_match_set_lyr_2_4, headers_v,
1883 ip_version, IPV4_VERSION);
1884 } else {
1885 MLX5_SET(fte_match_set_lyr_2_4, headers_c,
1886 ethertype, 0xffff);
1887 MLX5_SET(fte_match_set_lyr_2_4, headers_v,
1888 ethertype, ETH_P_IP);
1889 }
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02001890
Moses Reuben2d1e6972016-11-14 19:04:52 +02001891 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02001892 src_ipv4_src_ipv6.ipv4_layout.ipv4),
1893 &ib_spec->ipv4.mask.src_ip,
1894 sizeof(ib_spec->ipv4.mask.src_ip));
Moses Reuben2d1e6972016-11-14 19:04:52 +02001895 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02001896 src_ipv4_src_ipv6.ipv4_layout.ipv4),
1897 &ib_spec->ipv4.val.src_ip,
1898 sizeof(ib_spec->ipv4.val.src_ip));
Moses Reuben2d1e6972016-11-14 19:04:52 +02001899 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02001900 dst_ipv4_dst_ipv6.ipv4_layout.ipv4),
1901 &ib_spec->ipv4.mask.dst_ip,
1902 sizeof(ib_spec->ipv4.mask.dst_ip));
Moses Reuben2d1e6972016-11-14 19:04:52 +02001903 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02001904 dst_ipv4_dst_ipv6.ipv4_layout.ipv4),
1905 &ib_spec->ipv4.val.dst_ip,
1906 sizeof(ib_spec->ipv4.val.dst_ip));
Maor Gottliebca0d4752016-08-30 16:58:35 +03001907
Moses Reuben2d1e6972016-11-14 19:04:52 +02001908 set_tos(headers_c, headers_v,
Maor Gottliebca0d4752016-08-30 16:58:35 +03001909 ib_spec->ipv4.mask.tos, ib_spec->ipv4.val.tos);
1910
Moses Reuben2d1e6972016-11-14 19:04:52 +02001911 set_proto(headers_c, headers_v,
Maor Gottliebca0d4752016-08-30 16:58:35 +03001912 ib_spec->ipv4.mask.proto, ib_spec->ipv4.val.proto);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02001913 break;
Maor Gottlieb026bae02016-06-17 15:14:51 +03001914 case IB_FLOW_SPEC_IPV6:
Maor Gottliebc47ac6a2016-08-30 16:58:31 +03001915 if (FIELDS_NOT_SUPPORTED(ib_spec->ipv6.mask, LAST_IPV6_FIELD))
Leon Romanovsky1ffd3a22017-01-18 14:59:51 +02001916 return -EOPNOTSUPP;
Maor Gottlieb026bae02016-06-17 15:14:51 +03001917
Ariel Levkovich19cc7522017-04-03 13:11:03 +03001918 if (match_ipv) {
1919 MLX5_SET(fte_match_set_lyr_2_4, headers_c,
1920 ip_version, 0xf);
1921 MLX5_SET(fte_match_set_lyr_2_4, headers_v,
1922 ip_version, IPV6_VERSION);
1923 } else {
1924 MLX5_SET(fte_match_set_lyr_2_4, headers_c,
1925 ethertype, 0xffff);
1926 MLX5_SET(fte_match_set_lyr_2_4, headers_v,
1927 ethertype, ETH_P_IPV6);
1928 }
Maor Gottlieb026bae02016-06-17 15:14:51 +03001929
Moses Reuben2d1e6972016-11-14 19:04:52 +02001930 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
Maor Gottlieb026bae02016-06-17 15:14:51 +03001931 src_ipv4_src_ipv6.ipv6_layout.ipv6),
1932 &ib_spec->ipv6.mask.src_ip,
1933 sizeof(ib_spec->ipv6.mask.src_ip));
Moses Reuben2d1e6972016-11-14 19:04:52 +02001934 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
Maor Gottlieb026bae02016-06-17 15:14:51 +03001935 src_ipv4_src_ipv6.ipv6_layout.ipv6),
1936 &ib_spec->ipv6.val.src_ip,
1937 sizeof(ib_spec->ipv6.val.src_ip));
Moses Reuben2d1e6972016-11-14 19:04:52 +02001938 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
Maor Gottlieb026bae02016-06-17 15:14:51 +03001939 dst_ipv4_dst_ipv6.ipv6_layout.ipv6),
1940 &ib_spec->ipv6.mask.dst_ip,
1941 sizeof(ib_spec->ipv6.mask.dst_ip));
Moses Reuben2d1e6972016-11-14 19:04:52 +02001942 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
Maor Gottlieb026bae02016-06-17 15:14:51 +03001943 dst_ipv4_dst_ipv6.ipv6_layout.ipv6),
1944 &ib_spec->ipv6.val.dst_ip,
1945 sizeof(ib_spec->ipv6.val.dst_ip));
Maor Gottlieb466fa6d2016-08-30 16:58:36 +03001946
Moses Reuben2d1e6972016-11-14 19:04:52 +02001947 set_tos(headers_c, headers_v,
Maor Gottlieb466fa6d2016-08-30 16:58:36 +03001948 ib_spec->ipv6.mask.traffic_class,
1949 ib_spec->ipv6.val.traffic_class);
1950
Moses Reuben2d1e6972016-11-14 19:04:52 +02001951 set_proto(headers_c, headers_v,
Maor Gottlieb466fa6d2016-08-30 16:58:36 +03001952 ib_spec->ipv6.mask.next_hdr,
1953 ib_spec->ipv6.val.next_hdr);
1954
Moses Reuben2d1e6972016-11-14 19:04:52 +02001955 set_flow_label(misc_params_c, misc_params_v,
1956 ntohl(ib_spec->ipv6.mask.flow_label),
1957 ntohl(ib_spec->ipv6.val.flow_label),
1958 ib_spec->type & IB_FLOW_SPEC_INNER);
1959
Maor Gottlieb026bae02016-06-17 15:14:51 +03001960 break;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02001961 case IB_FLOW_SPEC_TCP:
Maor Gottliebc47ac6a2016-08-30 16:58:31 +03001962 if (FIELDS_NOT_SUPPORTED(ib_spec->tcp_udp.mask,
1963 LAST_TCP_UDP_FIELD))
Leon Romanovsky1ffd3a22017-01-18 14:59:51 +02001964 return -EOPNOTSUPP;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02001965
Moses Reuben2d1e6972016-11-14 19:04:52 +02001966 MLX5_SET(fte_match_set_lyr_2_4, headers_c, ip_protocol,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02001967 0xff);
Moses Reuben2d1e6972016-11-14 19:04:52 +02001968 MLX5_SET(fte_match_set_lyr_2_4, headers_v, ip_protocol,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02001969 IPPROTO_TCP);
1970
Moses Reuben2d1e6972016-11-14 19:04:52 +02001971 MLX5_SET(fte_match_set_lyr_2_4, headers_c, tcp_sport,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02001972 ntohs(ib_spec->tcp_udp.mask.src_port));
Moses Reuben2d1e6972016-11-14 19:04:52 +02001973 MLX5_SET(fte_match_set_lyr_2_4, headers_v, tcp_sport,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02001974 ntohs(ib_spec->tcp_udp.val.src_port));
1975
Moses Reuben2d1e6972016-11-14 19:04:52 +02001976 MLX5_SET(fte_match_set_lyr_2_4, headers_c, tcp_dport,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02001977 ntohs(ib_spec->tcp_udp.mask.dst_port));
Moses Reuben2d1e6972016-11-14 19:04:52 +02001978 MLX5_SET(fte_match_set_lyr_2_4, headers_v, tcp_dport,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02001979 ntohs(ib_spec->tcp_udp.val.dst_port));
1980 break;
1981 case IB_FLOW_SPEC_UDP:
Maor Gottliebc47ac6a2016-08-30 16:58:31 +03001982 if (FIELDS_NOT_SUPPORTED(ib_spec->tcp_udp.mask,
1983 LAST_TCP_UDP_FIELD))
Leon Romanovsky1ffd3a22017-01-18 14:59:51 +02001984 return -EOPNOTSUPP;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02001985
Moses Reuben2d1e6972016-11-14 19:04:52 +02001986 MLX5_SET(fte_match_set_lyr_2_4, headers_c, ip_protocol,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02001987 0xff);
Moses Reuben2d1e6972016-11-14 19:04:52 +02001988 MLX5_SET(fte_match_set_lyr_2_4, headers_v, ip_protocol,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02001989 IPPROTO_UDP);
1990
Moses Reuben2d1e6972016-11-14 19:04:52 +02001991 MLX5_SET(fte_match_set_lyr_2_4, headers_c, udp_sport,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02001992 ntohs(ib_spec->tcp_udp.mask.src_port));
Moses Reuben2d1e6972016-11-14 19:04:52 +02001993 MLX5_SET(fte_match_set_lyr_2_4, headers_v, udp_sport,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02001994 ntohs(ib_spec->tcp_udp.val.src_port));
1995
Moses Reuben2d1e6972016-11-14 19:04:52 +02001996 MLX5_SET(fte_match_set_lyr_2_4, headers_c, udp_dport,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02001997 ntohs(ib_spec->tcp_udp.mask.dst_port));
Moses Reuben2d1e6972016-11-14 19:04:52 +02001998 MLX5_SET(fte_match_set_lyr_2_4, headers_v, udp_dport,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02001999 ntohs(ib_spec->tcp_udp.val.dst_port));
2000 break;
Moses Reubenffb30d82016-11-14 19:04:50 +02002001 case IB_FLOW_SPEC_VXLAN_TUNNEL:
2002 if (FIELDS_NOT_SUPPORTED(ib_spec->tunnel.mask,
2003 LAST_TUNNEL_FIELD))
Leon Romanovsky1ffd3a22017-01-18 14:59:51 +02002004 return -EOPNOTSUPP;
Moses Reubenffb30d82016-11-14 19:04:50 +02002005
2006 MLX5_SET(fte_match_set_misc, misc_params_c, vxlan_vni,
2007 ntohl(ib_spec->tunnel.mask.tunnel_id));
2008 MLX5_SET(fte_match_set_misc, misc_params_v, vxlan_vni,
2009 ntohl(ib_spec->tunnel.val.tunnel_id));
2010 break;
Moses Reuben2ac693f2017-01-18 14:59:50 +02002011 case IB_FLOW_SPEC_ACTION_TAG:
2012 if (FIELDS_NOT_SUPPORTED(ib_spec->flow_tag,
2013 LAST_FLOW_TAG_FIELD))
2014 return -EOPNOTSUPP;
2015 if (ib_spec->flow_tag.tag_id >= BIT(24))
2016 return -EINVAL;
2017
2018 *tag_id = ib_spec->flow_tag.tag_id;
2019 break;
Slava Shwartsmana22ed862017-04-03 13:13:52 +03002020 case IB_FLOW_SPEC_ACTION_DROP:
2021 if (FIELDS_NOT_SUPPORTED(ib_spec->drop,
2022 LAST_DROP_FIELD))
2023 return -EOPNOTSUPP;
2024 *is_drop = true;
2025 break;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002026 default:
2027 return -EINVAL;
2028 }
2029
2030 return 0;
2031}
2032
2033/* If a flow could catch both multicast and unicast packets,
2034 * it won't fall into the multicast flow steering table and this rule
2035 * could steal other multicast packets.
2036 */
2037static bool flow_is_multicast_only(struct ib_flow_attr *ib_attr)
2038{
2039 struct ib_flow_spec_eth *eth_spec;
2040
2041 if (ib_attr->type != IB_FLOW_ATTR_NORMAL ||
2042 ib_attr->size < sizeof(struct ib_flow_attr) +
2043 sizeof(struct ib_flow_spec_eth) ||
2044 ib_attr->num_of_specs < 1)
2045 return false;
2046
2047 eth_spec = (struct ib_flow_spec_eth *)(ib_attr + 1);
2048 if (eth_spec->type != IB_FLOW_SPEC_ETH ||
2049 eth_spec->size != sizeof(*eth_spec))
2050 return false;
2051
2052 return is_multicast_ether_addr(eth_spec->mask.dst_mac) &&
2053 is_multicast_ether_addr(eth_spec->val.dst_mac);
2054}
2055
Ariel Levkovich19cc7522017-04-03 13:11:03 +03002056static bool is_valid_ethertype(struct mlx5_core_dev *mdev,
2057 const struct ib_flow_attr *flow_attr,
Ariel Levkovich0f750962017-04-03 13:11:02 +03002058 bool check_inner)
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002059{
2060 union ib_flow_spec *ib_spec = (union ib_flow_spec *)(flow_attr + 1);
Ariel Levkovich19cc7522017-04-03 13:11:03 +03002061 int match_ipv = check_inner ?
2062 MLX5_CAP_FLOWTABLE_NIC_RX(mdev,
2063 ft_field_support.inner_ip_version) :
2064 MLX5_CAP_FLOWTABLE_NIC_RX(mdev,
2065 ft_field_support.outer_ip_version);
Ariel Levkovich0f750962017-04-03 13:11:02 +03002066 int inner_bit = check_inner ? IB_FLOW_SPEC_INNER : 0;
2067 bool ipv4_spec_valid, ipv6_spec_valid;
2068 unsigned int ip_spec_type = 0;
2069 bool has_ethertype = false;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002070 unsigned int spec_index;
Ariel Levkovich0f750962017-04-03 13:11:02 +03002071 bool mask_valid = true;
2072 u16 eth_type = 0;
2073 bool type_valid;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002074
2075 /* Validate that ethertype is correct */
2076 for (spec_index = 0; spec_index < flow_attr->num_of_specs; spec_index++) {
Ariel Levkovich0f750962017-04-03 13:11:02 +03002077 if ((ib_spec->type == (IB_FLOW_SPEC_ETH | inner_bit)) &&
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002078 ib_spec->eth.mask.ether_type) {
Ariel Levkovich0f750962017-04-03 13:11:02 +03002079 mask_valid = (ib_spec->eth.mask.ether_type ==
2080 htons(0xffff));
2081 has_ethertype = true;
2082 eth_type = ntohs(ib_spec->eth.val.ether_type);
2083 } else if ((ib_spec->type == (IB_FLOW_SPEC_IPV4 | inner_bit)) ||
2084 (ib_spec->type == (IB_FLOW_SPEC_IPV6 | inner_bit))) {
2085 ip_spec_type = ib_spec->type;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002086 }
2087 ib_spec = (void *)ib_spec + ib_spec->size;
2088 }
Ariel Levkovich0f750962017-04-03 13:11:02 +03002089
2090 type_valid = (!has_ethertype) || (!ip_spec_type);
2091 if (!type_valid && mask_valid) {
2092 ipv4_spec_valid = (eth_type == ETH_P_IP) &&
2093 (ip_spec_type == (IB_FLOW_SPEC_IPV4 | inner_bit));
2094 ipv6_spec_valid = (eth_type == ETH_P_IPV6) &&
2095 (ip_spec_type == (IB_FLOW_SPEC_IPV6 | inner_bit));
Ariel Levkovich19cc7522017-04-03 13:11:03 +03002096
2097 type_valid = (ipv4_spec_valid) || (ipv6_spec_valid) ||
2098 (((eth_type == ETH_P_MPLS_UC) ||
2099 (eth_type == ETH_P_MPLS_MC)) && match_ipv);
Ariel Levkovich0f750962017-04-03 13:11:02 +03002100 }
2101
2102 return type_valid;
2103}
2104
Ariel Levkovich19cc7522017-04-03 13:11:03 +03002105static bool is_valid_attr(struct mlx5_core_dev *mdev,
2106 const struct ib_flow_attr *flow_attr)
Ariel Levkovich0f750962017-04-03 13:11:02 +03002107{
Ariel Levkovich19cc7522017-04-03 13:11:03 +03002108 return is_valid_ethertype(mdev, flow_attr, false) &&
2109 is_valid_ethertype(mdev, flow_attr, true);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002110}
2111
2112static void put_flow_table(struct mlx5_ib_dev *dev,
2113 struct mlx5_ib_flow_prio *prio, bool ft_added)
2114{
2115 prio->refcount -= !!ft_added;
2116 if (!prio->refcount) {
2117 mlx5_destroy_flow_table(prio->flow_table);
2118 prio->flow_table = NULL;
2119 }
2120}
2121
2122static int mlx5_ib_destroy_flow(struct ib_flow *flow_id)
2123{
2124 struct mlx5_ib_dev *dev = to_mdev(flow_id->qp->device);
2125 struct mlx5_ib_flow_handler *handler = container_of(flow_id,
2126 struct mlx5_ib_flow_handler,
2127 ibflow);
2128 struct mlx5_ib_flow_handler *iter, *tmp;
2129
2130 mutex_lock(&dev->flow_db.lock);
2131
2132 list_for_each_entry_safe(iter, tmp, &handler->list, list) {
Mark Bloch74491de2016-08-31 11:24:25 +00002133 mlx5_del_flow_rules(iter->rule);
Maor Gottliebcc0e5d42016-08-28 14:16:34 +03002134 put_flow_table(dev, iter->prio, true);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002135 list_del(&iter->list);
2136 kfree(iter);
2137 }
2138
Mark Bloch74491de2016-08-31 11:24:25 +00002139 mlx5_del_flow_rules(handler->rule);
Maor Gottlieb5497adc2016-08-28 14:16:31 +03002140 put_flow_table(dev, handler->prio, true);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002141 mutex_unlock(&dev->flow_db.lock);
2142
2143 kfree(handler);
2144
2145 return 0;
2146}
2147
Maor Gottlieb35d190112016-03-07 18:51:47 +02002148static int ib_prio_to_core_prio(unsigned int priority, bool dont_trap)
2149{
2150 priority *= 2;
2151 if (!dont_trap)
2152 priority++;
2153 return priority;
2154}
2155
Maor Gottliebcc0e5d42016-08-28 14:16:34 +03002156enum flow_table_type {
2157 MLX5_IB_FT_RX,
2158 MLX5_IB_FT_TX
2159};
2160
Maor Gottlieb00b7c2a2017-03-29 06:09:01 +03002161#define MLX5_FS_MAX_TYPES 6
2162#define MLX5_FS_MAX_ENTRIES BIT(16)
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002163static struct mlx5_ib_flow_prio *get_flow_table(struct mlx5_ib_dev *dev,
Maor Gottliebcc0e5d42016-08-28 14:16:34 +03002164 struct ib_flow_attr *flow_attr,
2165 enum flow_table_type ft_type)
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002166{
Maor Gottlieb35d190112016-03-07 18:51:47 +02002167 bool dont_trap = flow_attr->flags & IB_FLOW_ATTR_FLAGS_DONT_TRAP;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002168 struct mlx5_flow_namespace *ns = NULL;
2169 struct mlx5_ib_flow_prio *prio;
2170 struct mlx5_flow_table *ft;
Maor Gottliebdac388e2017-03-29 06:09:00 +03002171 int max_table_size;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002172 int num_entries;
2173 int num_groups;
2174 int priority;
2175 int err = 0;
2176
Maor Gottliebdac388e2017-03-29 06:09:00 +03002177 max_table_size = BIT(MLX5_CAP_FLOWTABLE_NIC_RX(dev->mdev,
2178 log_max_ft_size));
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002179 if (flow_attr->type == IB_FLOW_ATTR_NORMAL) {
Maor Gottlieb35d190112016-03-07 18:51:47 +02002180 if (flow_is_multicast_only(flow_attr) &&
2181 !dont_trap)
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002182 priority = MLX5_IB_FLOW_MCAST_PRIO;
2183 else
Maor Gottlieb35d190112016-03-07 18:51:47 +02002184 priority = ib_prio_to_core_prio(flow_attr->priority,
2185 dont_trap);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002186 ns = mlx5_get_flow_namespace(dev->mdev,
2187 MLX5_FLOW_NAMESPACE_BYPASS);
2188 num_entries = MLX5_FS_MAX_ENTRIES;
2189 num_groups = MLX5_FS_MAX_TYPES;
2190 prio = &dev->flow_db.prios[priority];
2191 } else if (flow_attr->type == IB_FLOW_ATTR_ALL_DEFAULT ||
2192 flow_attr->type == IB_FLOW_ATTR_MC_DEFAULT) {
2193 ns = mlx5_get_flow_namespace(dev->mdev,
2194 MLX5_FLOW_NAMESPACE_LEFTOVERS);
2195 build_leftovers_ft_param(&priority,
2196 &num_entries,
2197 &num_groups);
2198 prio = &dev->flow_db.prios[MLX5_IB_FLOW_LEFTOVERS_PRIO];
Maor Gottliebcc0e5d42016-08-28 14:16:34 +03002199 } else if (flow_attr->type == IB_FLOW_ATTR_SNIFFER) {
2200 if (!MLX5_CAP_FLOWTABLE(dev->mdev,
2201 allow_sniffer_and_nic_rx_shared_tir))
2202 return ERR_PTR(-ENOTSUPP);
2203
2204 ns = mlx5_get_flow_namespace(dev->mdev, ft_type == MLX5_IB_FT_RX ?
2205 MLX5_FLOW_NAMESPACE_SNIFFER_RX :
2206 MLX5_FLOW_NAMESPACE_SNIFFER_TX);
2207
2208 prio = &dev->flow_db.sniffer[ft_type];
2209 priority = 0;
2210 num_entries = 1;
2211 num_groups = 1;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002212 }
2213
2214 if (!ns)
2215 return ERR_PTR(-ENOTSUPP);
2216
Maor Gottliebdac388e2017-03-29 06:09:00 +03002217 if (num_entries > max_table_size)
2218 return ERR_PTR(-ENOMEM);
2219
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002220 ft = prio->flow_table;
2221 if (!ft) {
2222 ft = mlx5_create_auto_grouped_flow_table(ns, priority,
2223 num_entries,
Maor Gottliebd63cd282016-04-29 01:36:35 +03002224 num_groups,
Hadar Hen Zionc9f1b072016-11-07 15:14:44 +02002225 0, 0);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002226
2227 if (!IS_ERR(ft)) {
2228 prio->refcount = 0;
2229 prio->flow_table = ft;
2230 } else {
2231 err = PTR_ERR(ft);
2232 }
2233 }
2234
2235 return err ? ERR_PTR(err) : prio;
2236}
2237
2238static struct mlx5_ib_flow_handler *create_flow_rule(struct mlx5_ib_dev *dev,
2239 struct mlx5_ib_flow_prio *ft_prio,
Maor Gottliebdd063d02016-08-28 14:16:32 +03002240 const struct ib_flow_attr *flow_attr,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002241 struct mlx5_flow_destination *dst)
2242{
2243 struct mlx5_flow_table *ft = ft_prio->flow_table;
2244 struct mlx5_ib_flow_handler *handler;
Hadar Hen Zion66958ed2016-11-07 15:14:45 +02002245 struct mlx5_flow_act flow_act = {0};
Maor Gottliebc5bb1732016-07-04 17:23:05 +03002246 struct mlx5_flow_spec *spec;
Slava Shwartsmana22ed862017-04-03 13:13:52 +03002247 struct mlx5_flow_destination *rule_dst = dst;
Maor Gottliebdd063d02016-08-28 14:16:32 +03002248 const void *ib_flow = (const void *)flow_attr + sizeof(*flow_attr);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002249 unsigned int spec_index;
Moses Reuben2ac693f2017-01-18 14:59:50 +02002250 u32 flow_tag = MLX5_FS_DEFAULT_FLOW_TAG;
Slava Shwartsmana22ed862017-04-03 13:13:52 +03002251 bool is_drop = false;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002252 int err = 0;
Slava Shwartsmana22ed862017-04-03 13:13:52 +03002253 int dest_num = 1;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002254
Ariel Levkovich19cc7522017-04-03 13:11:03 +03002255 if (!is_valid_attr(dev->mdev, flow_attr))
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002256 return ERR_PTR(-EINVAL);
2257
Leon Romanovsky1b9a07e2017-05-10 21:32:18 +03002258 spec = kvzalloc(sizeof(*spec), GFP_KERNEL);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002259 handler = kzalloc(sizeof(*handler), GFP_KERNEL);
Maor Gottliebc5bb1732016-07-04 17:23:05 +03002260 if (!handler || !spec) {
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002261 err = -ENOMEM;
2262 goto free;
2263 }
2264
2265 INIT_LIST_HEAD(&handler->list);
2266
2267 for (spec_index = 0; spec_index < flow_attr->num_of_specs; spec_index++) {
Ariel Levkovich19cc7522017-04-03 13:11:03 +03002268 err = parse_flow_attr(dev->mdev, spec->match_criteria,
Slava Shwartsmana22ed862017-04-03 13:13:52 +03002269 spec->match_value,
2270 ib_flow, &flow_tag, &is_drop);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002271 if (err < 0)
2272 goto free;
2273
2274 ib_flow += ((union ib_flow_spec *)ib_flow)->size;
2275 }
2276
Maor Gottlieb466fa6d2016-08-30 16:58:36 +03002277 spec->match_criteria_enable = get_match_criteria_enable(spec->match_criteria);
Slava Shwartsmana22ed862017-04-03 13:13:52 +03002278 if (is_drop) {
2279 flow_act.action = MLX5_FLOW_CONTEXT_ACTION_DROP;
2280 rule_dst = NULL;
2281 dest_num = 0;
2282 } else {
2283 flow_act.action = dst ? MLX5_FLOW_CONTEXT_ACTION_FWD_DEST :
2284 MLX5_FLOW_CONTEXT_ACTION_FWD_NEXT_PRIO;
2285 }
Moses Reuben2ac693f2017-01-18 14:59:50 +02002286
2287 if (flow_tag != MLX5_FS_DEFAULT_FLOW_TAG &&
2288 (flow_attr->type == IB_FLOW_ATTR_ALL_DEFAULT ||
2289 flow_attr->type == IB_FLOW_ATTR_MC_DEFAULT)) {
2290 mlx5_ib_warn(dev, "Flow tag %u and attribute type %x isn't allowed in leftovers\n",
2291 flow_tag, flow_attr->type);
2292 err = -EINVAL;
2293 goto free;
2294 }
2295 flow_act.flow_tag = flow_tag;
Mark Bloch74491de2016-08-31 11:24:25 +00002296 handler->rule = mlx5_add_flow_rules(ft, spec,
Hadar Hen Zion66958ed2016-11-07 15:14:45 +02002297 &flow_act,
Slava Shwartsmana22ed862017-04-03 13:13:52 +03002298 rule_dst, dest_num);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002299
2300 if (IS_ERR(handler->rule)) {
2301 err = PTR_ERR(handler->rule);
2302 goto free;
2303 }
2304
Maor Gottliebd9d49802016-08-28 14:16:33 +03002305 ft_prio->refcount++;
Maor Gottlieb5497adc2016-08-28 14:16:31 +03002306 handler->prio = ft_prio;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002307
2308 ft_prio->flow_table = ft;
2309free:
2310 if (err)
2311 kfree(handler);
Maor Gottliebc5bb1732016-07-04 17:23:05 +03002312 kvfree(spec);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002313 return err ? ERR_PTR(err) : handler;
2314}
2315
Maor Gottlieb35d190112016-03-07 18:51:47 +02002316static struct mlx5_ib_flow_handler *create_dont_trap_rule(struct mlx5_ib_dev *dev,
2317 struct mlx5_ib_flow_prio *ft_prio,
2318 struct ib_flow_attr *flow_attr,
2319 struct mlx5_flow_destination *dst)
2320{
2321 struct mlx5_ib_flow_handler *handler_dst = NULL;
2322 struct mlx5_ib_flow_handler *handler = NULL;
2323
2324 handler = create_flow_rule(dev, ft_prio, flow_attr, NULL);
2325 if (!IS_ERR(handler)) {
2326 handler_dst = create_flow_rule(dev, ft_prio,
2327 flow_attr, dst);
2328 if (IS_ERR(handler_dst)) {
Mark Bloch74491de2016-08-31 11:24:25 +00002329 mlx5_del_flow_rules(handler->rule);
Maor Gottliebd9d49802016-08-28 14:16:33 +03002330 ft_prio->refcount--;
Maor Gottlieb35d190112016-03-07 18:51:47 +02002331 kfree(handler);
2332 handler = handler_dst;
2333 } else {
2334 list_add(&handler_dst->list, &handler->list);
2335 }
2336 }
2337
2338 return handler;
2339}
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002340enum {
2341 LEFTOVERS_MC,
2342 LEFTOVERS_UC,
2343};
2344
2345static struct mlx5_ib_flow_handler *create_leftovers_rule(struct mlx5_ib_dev *dev,
2346 struct mlx5_ib_flow_prio *ft_prio,
2347 struct ib_flow_attr *flow_attr,
2348 struct mlx5_flow_destination *dst)
2349{
2350 struct mlx5_ib_flow_handler *handler_ucast = NULL;
2351 struct mlx5_ib_flow_handler *handler = NULL;
2352
2353 static struct {
2354 struct ib_flow_attr flow_attr;
2355 struct ib_flow_spec_eth eth_flow;
2356 } leftovers_specs[] = {
2357 [LEFTOVERS_MC] = {
2358 .flow_attr = {
2359 .num_of_specs = 1,
2360 .size = sizeof(leftovers_specs[0])
2361 },
2362 .eth_flow = {
2363 .type = IB_FLOW_SPEC_ETH,
2364 .size = sizeof(struct ib_flow_spec_eth),
2365 .mask = {.dst_mac = {0x1} },
2366 .val = {.dst_mac = {0x1} }
2367 }
2368 },
2369 [LEFTOVERS_UC] = {
2370 .flow_attr = {
2371 .num_of_specs = 1,
2372 .size = sizeof(leftovers_specs[0])
2373 },
2374 .eth_flow = {
2375 .type = IB_FLOW_SPEC_ETH,
2376 .size = sizeof(struct ib_flow_spec_eth),
2377 .mask = {.dst_mac = {0x1} },
2378 .val = {.dst_mac = {} }
2379 }
2380 }
2381 };
2382
2383 handler = create_flow_rule(dev, ft_prio,
2384 &leftovers_specs[LEFTOVERS_MC].flow_attr,
2385 dst);
2386 if (!IS_ERR(handler) &&
2387 flow_attr->type == IB_FLOW_ATTR_ALL_DEFAULT) {
2388 handler_ucast = create_flow_rule(dev, ft_prio,
2389 &leftovers_specs[LEFTOVERS_UC].flow_attr,
2390 dst);
2391 if (IS_ERR(handler_ucast)) {
Mark Bloch74491de2016-08-31 11:24:25 +00002392 mlx5_del_flow_rules(handler->rule);
Maor Gottliebd9d49802016-08-28 14:16:33 +03002393 ft_prio->refcount--;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002394 kfree(handler);
2395 handler = handler_ucast;
2396 } else {
2397 list_add(&handler_ucast->list, &handler->list);
2398 }
2399 }
2400
2401 return handler;
2402}
2403
Maor Gottliebcc0e5d42016-08-28 14:16:34 +03002404static struct mlx5_ib_flow_handler *create_sniffer_rule(struct mlx5_ib_dev *dev,
2405 struct mlx5_ib_flow_prio *ft_rx,
2406 struct mlx5_ib_flow_prio *ft_tx,
2407 struct mlx5_flow_destination *dst)
2408{
2409 struct mlx5_ib_flow_handler *handler_rx;
2410 struct mlx5_ib_flow_handler *handler_tx;
2411 int err;
2412 static const struct ib_flow_attr flow_attr = {
2413 .num_of_specs = 0,
2414 .size = sizeof(flow_attr)
2415 };
2416
2417 handler_rx = create_flow_rule(dev, ft_rx, &flow_attr, dst);
2418 if (IS_ERR(handler_rx)) {
2419 err = PTR_ERR(handler_rx);
2420 goto err;
2421 }
2422
2423 handler_tx = create_flow_rule(dev, ft_tx, &flow_attr, dst);
2424 if (IS_ERR(handler_tx)) {
2425 err = PTR_ERR(handler_tx);
2426 goto err_tx;
2427 }
2428
2429 list_add(&handler_tx->list, &handler_rx->list);
2430
2431 return handler_rx;
2432
2433err_tx:
Mark Bloch74491de2016-08-31 11:24:25 +00002434 mlx5_del_flow_rules(handler_rx->rule);
Maor Gottliebcc0e5d42016-08-28 14:16:34 +03002435 ft_rx->refcount--;
2436 kfree(handler_rx);
2437err:
2438 return ERR_PTR(err);
2439}
2440
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002441static struct ib_flow *mlx5_ib_create_flow(struct ib_qp *qp,
2442 struct ib_flow_attr *flow_attr,
2443 int domain)
2444{
2445 struct mlx5_ib_dev *dev = to_mdev(qp->device);
Yishai Hadasd9f88e52016-08-28 10:58:37 +03002446 struct mlx5_ib_qp *mqp = to_mqp(qp);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002447 struct mlx5_ib_flow_handler *handler = NULL;
2448 struct mlx5_flow_destination *dst = NULL;
Maor Gottliebcc0e5d42016-08-28 14:16:34 +03002449 struct mlx5_ib_flow_prio *ft_prio_tx = NULL;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002450 struct mlx5_ib_flow_prio *ft_prio;
2451 int err;
2452
2453 if (flow_attr->priority > MLX5_IB_FLOW_LAST_PRIO)
Maor Gottliebdac388e2017-03-29 06:09:00 +03002454 return ERR_PTR(-ENOMEM);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002455
2456 if (domain != IB_FLOW_DOMAIN_USER ||
2457 flow_attr->port > MLX5_CAP_GEN(dev->mdev, num_ports) ||
Maor Gottlieb35d190112016-03-07 18:51:47 +02002458 (flow_attr->flags & ~IB_FLOW_ATTR_FLAGS_DONT_TRAP))
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002459 return ERR_PTR(-EINVAL);
2460
2461 dst = kzalloc(sizeof(*dst), GFP_KERNEL);
2462 if (!dst)
2463 return ERR_PTR(-ENOMEM);
2464
2465 mutex_lock(&dev->flow_db.lock);
2466
Maor Gottliebcc0e5d42016-08-28 14:16:34 +03002467 ft_prio = get_flow_table(dev, flow_attr, MLX5_IB_FT_RX);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002468 if (IS_ERR(ft_prio)) {
2469 err = PTR_ERR(ft_prio);
2470 goto unlock;
2471 }
Maor Gottliebcc0e5d42016-08-28 14:16:34 +03002472 if (flow_attr->type == IB_FLOW_ATTR_SNIFFER) {
2473 ft_prio_tx = get_flow_table(dev, flow_attr, MLX5_IB_FT_TX);
2474 if (IS_ERR(ft_prio_tx)) {
2475 err = PTR_ERR(ft_prio_tx);
2476 ft_prio_tx = NULL;
2477 goto destroy_ft;
2478 }
2479 }
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002480
2481 dst->type = MLX5_FLOW_DESTINATION_TYPE_TIR;
Yishai Hadasd9f88e52016-08-28 10:58:37 +03002482 if (mqp->flags & MLX5_IB_QP_RSS)
2483 dst->tir_num = mqp->rss_qp.tirn;
2484 else
2485 dst->tir_num = mqp->raw_packet_qp.rq.tirn;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002486
2487 if (flow_attr->type == IB_FLOW_ATTR_NORMAL) {
Maor Gottlieb35d190112016-03-07 18:51:47 +02002488 if (flow_attr->flags & IB_FLOW_ATTR_FLAGS_DONT_TRAP) {
2489 handler = create_dont_trap_rule(dev, ft_prio,
2490 flow_attr, dst);
2491 } else {
2492 handler = create_flow_rule(dev, ft_prio, flow_attr,
2493 dst);
2494 }
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002495 } else if (flow_attr->type == IB_FLOW_ATTR_ALL_DEFAULT ||
2496 flow_attr->type == IB_FLOW_ATTR_MC_DEFAULT) {
2497 handler = create_leftovers_rule(dev, ft_prio, flow_attr,
2498 dst);
Maor Gottliebcc0e5d42016-08-28 14:16:34 +03002499 } else if (flow_attr->type == IB_FLOW_ATTR_SNIFFER) {
2500 handler = create_sniffer_rule(dev, ft_prio, ft_prio_tx, dst);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002501 } else {
2502 err = -EINVAL;
2503 goto destroy_ft;
2504 }
2505
2506 if (IS_ERR(handler)) {
2507 err = PTR_ERR(handler);
2508 handler = NULL;
2509 goto destroy_ft;
2510 }
2511
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002512 mutex_unlock(&dev->flow_db.lock);
2513 kfree(dst);
2514
2515 return &handler->ibflow;
2516
2517destroy_ft:
2518 put_flow_table(dev, ft_prio, false);
Maor Gottliebcc0e5d42016-08-28 14:16:34 +03002519 if (ft_prio_tx)
2520 put_flow_table(dev, ft_prio_tx, false);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002521unlock:
2522 mutex_unlock(&dev->flow_db.lock);
2523 kfree(dst);
2524 kfree(handler);
2525 return ERR_PTR(err);
2526}
2527
Eli Cohene126ba92013-07-07 17:25:49 +03002528static int mlx5_ib_mcg_attach(struct ib_qp *ibqp, union ib_gid *gid, u16 lid)
2529{
2530 struct mlx5_ib_dev *dev = to_mdev(ibqp->device);
2531 int err;
2532
Jack Morgenstein9603b612014-07-28 23:30:22 +03002533 err = mlx5_core_attach_mcg(dev->mdev, gid, ibqp->qp_num);
Eli Cohene126ba92013-07-07 17:25:49 +03002534 if (err)
2535 mlx5_ib_warn(dev, "failed attaching QPN 0x%x, MGID %pI6\n",
2536 ibqp->qp_num, gid->raw);
2537
2538 return err;
2539}
2540
2541static int mlx5_ib_mcg_detach(struct ib_qp *ibqp, union ib_gid *gid, u16 lid)
2542{
2543 struct mlx5_ib_dev *dev = to_mdev(ibqp->device);
2544 int err;
2545
Jack Morgenstein9603b612014-07-28 23:30:22 +03002546 err = mlx5_core_detach_mcg(dev->mdev, gid, ibqp->qp_num);
Eli Cohene126ba92013-07-07 17:25:49 +03002547 if (err)
2548 mlx5_ib_warn(dev, "failed detaching QPN 0x%x, MGID %pI6\n",
2549 ibqp->qp_num, gid->raw);
2550
2551 return err;
2552}
2553
2554static int init_node_data(struct mlx5_ib_dev *dev)
2555{
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03002556 int err;
Eli Cohene126ba92013-07-07 17:25:49 +03002557
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03002558 err = mlx5_query_node_desc(dev, dev->ib_dev.node_desc);
Eli Cohene126ba92013-07-07 17:25:49 +03002559 if (err)
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03002560 return err;
Eli Cohene126ba92013-07-07 17:25:49 +03002561
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03002562 dev->mdev->rev_id = dev->mdev->pdev->revision;
Eli Cohene126ba92013-07-07 17:25:49 +03002563
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03002564 return mlx5_query_node_guid(dev, &dev->ib_dev.node_guid);
Eli Cohene126ba92013-07-07 17:25:49 +03002565}
2566
2567static ssize_t show_fw_pages(struct device *device, struct device_attribute *attr,
2568 char *buf)
2569{
2570 struct mlx5_ib_dev *dev =
2571 container_of(device, struct mlx5_ib_dev, ib_dev.dev);
2572
Jack Morgenstein9603b612014-07-28 23:30:22 +03002573 return sprintf(buf, "%d\n", dev->mdev->priv.fw_pages);
Eli Cohene126ba92013-07-07 17:25:49 +03002574}
2575
2576static ssize_t show_reg_pages(struct device *device,
2577 struct device_attribute *attr, char *buf)
2578{
2579 struct mlx5_ib_dev *dev =
2580 container_of(device, struct mlx5_ib_dev, ib_dev.dev);
2581
Haggai Eran6aec21f2014-12-11 17:04:23 +02002582 return sprintf(buf, "%d\n", atomic_read(&dev->mdev->priv.reg_pages));
Eli Cohene126ba92013-07-07 17:25:49 +03002583}
2584
2585static ssize_t show_hca(struct device *device, struct device_attribute *attr,
2586 char *buf)
2587{
2588 struct mlx5_ib_dev *dev =
2589 container_of(device, struct mlx5_ib_dev, ib_dev.dev);
Jack Morgenstein9603b612014-07-28 23:30:22 +03002590 return sprintf(buf, "MT%d\n", dev->mdev->pdev->device);
Eli Cohene126ba92013-07-07 17:25:49 +03002591}
2592
Eli Cohene126ba92013-07-07 17:25:49 +03002593static ssize_t show_rev(struct device *device, struct device_attribute *attr,
2594 char *buf)
2595{
2596 struct mlx5_ib_dev *dev =
2597 container_of(device, struct mlx5_ib_dev, ib_dev.dev);
Jack Morgenstein9603b612014-07-28 23:30:22 +03002598 return sprintf(buf, "%x\n", dev->mdev->rev_id);
Eli Cohene126ba92013-07-07 17:25:49 +03002599}
2600
2601static ssize_t show_board(struct device *device, struct device_attribute *attr,
2602 char *buf)
2603{
2604 struct mlx5_ib_dev *dev =
2605 container_of(device, struct mlx5_ib_dev, ib_dev.dev);
2606 return sprintf(buf, "%.*s\n", MLX5_BOARD_ID_LEN,
Jack Morgenstein9603b612014-07-28 23:30:22 +03002607 dev->mdev->board_id);
Eli Cohene126ba92013-07-07 17:25:49 +03002608}
2609
2610static DEVICE_ATTR(hw_rev, S_IRUGO, show_rev, NULL);
Eli Cohene126ba92013-07-07 17:25:49 +03002611static DEVICE_ATTR(hca_type, S_IRUGO, show_hca, NULL);
2612static DEVICE_ATTR(board_id, S_IRUGO, show_board, NULL);
2613static DEVICE_ATTR(fw_pages, S_IRUGO, show_fw_pages, NULL);
2614static DEVICE_ATTR(reg_pages, S_IRUGO, show_reg_pages, NULL);
2615
2616static struct device_attribute *mlx5_class_attributes[] = {
2617 &dev_attr_hw_rev,
Eli Cohene126ba92013-07-07 17:25:49 +03002618 &dev_attr_hca_type,
2619 &dev_attr_board_id,
2620 &dev_attr_fw_pages,
2621 &dev_attr_reg_pages,
2622};
2623
Haggai Eran7722f472016-02-29 15:45:07 +02002624static void pkey_change_handler(struct work_struct *work)
2625{
2626 struct mlx5_ib_port_resources *ports =
2627 container_of(work, struct mlx5_ib_port_resources,
2628 pkey_change_work);
2629
2630 mutex_lock(&ports->devr->mutex);
2631 mlx5_ib_gsi_pkey_change(ports->gsi);
2632 mutex_unlock(&ports->devr->mutex);
2633}
2634
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03002635static void mlx5_ib_handle_internal_error(struct mlx5_ib_dev *ibdev)
2636{
2637 struct mlx5_ib_qp *mqp;
2638 struct mlx5_ib_cq *send_mcq, *recv_mcq;
2639 struct mlx5_core_cq *mcq;
2640 struct list_head cq_armed_list;
2641 unsigned long flags_qp;
2642 unsigned long flags_cq;
2643 unsigned long flags;
2644
2645 INIT_LIST_HEAD(&cq_armed_list);
2646
2647 /* Go over qp list reside on that ibdev, sync with create/destroy qp.*/
2648 spin_lock_irqsave(&ibdev->reset_flow_resource_lock, flags);
2649 list_for_each_entry(mqp, &ibdev->qp_list, qps_list) {
2650 spin_lock_irqsave(&mqp->sq.lock, flags_qp);
2651 if (mqp->sq.tail != mqp->sq.head) {
2652 send_mcq = to_mcq(mqp->ibqp.send_cq);
2653 spin_lock_irqsave(&send_mcq->lock, flags_cq);
2654 if (send_mcq->mcq.comp &&
2655 mqp->ibqp.send_cq->comp_handler) {
2656 if (!send_mcq->mcq.reset_notify_added) {
2657 send_mcq->mcq.reset_notify_added = 1;
2658 list_add_tail(&send_mcq->mcq.reset_notify,
2659 &cq_armed_list);
2660 }
2661 }
2662 spin_unlock_irqrestore(&send_mcq->lock, flags_cq);
2663 }
2664 spin_unlock_irqrestore(&mqp->sq.lock, flags_qp);
2665 spin_lock_irqsave(&mqp->rq.lock, flags_qp);
2666 /* no handling is needed for SRQ */
2667 if (!mqp->ibqp.srq) {
2668 if (mqp->rq.tail != mqp->rq.head) {
2669 recv_mcq = to_mcq(mqp->ibqp.recv_cq);
2670 spin_lock_irqsave(&recv_mcq->lock, flags_cq);
2671 if (recv_mcq->mcq.comp &&
2672 mqp->ibqp.recv_cq->comp_handler) {
2673 if (!recv_mcq->mcq.reset_notify_added) {
2674 recv_mcq->mcq.reset_notify_added = 1;
2675 list_add_tail(&recv_mcq->mcq.reset_notify,
2676 &cq_armed_list);
2677 }
2678 }
2679 spin_unlock_irqrestore(&recv_mcq->lock,
2680 flags_cq);
2681 }
2682 }
2683 spin_unlock_irqrestore(&mqp->rq.lock, flags_qp);
2684 }
2685 /*At that point all inflight post send were put to be executed as of we
2686 * lock/unlock above locks Now need to arm all involved CQs.
2687 */
2688 list_for_each_entry(mcq, &cq_armed_list, reset_notify) {
2689 mcq->comp(mcq);
2690 }
2691 spin_unlock_irqrestore(&ibdev->reset_flow_resource_lock, flags);
2692}
2693
Jack Morgenstein9603b612014-07-28 23:30:22 +03002694static void mlx5_ib_event(struct mlx5_core_dev *dev, void *context,
Jack Morgenstein4d2f9bb2014-07-28 23:30:24 +03002695 enum mlx5_dev_event event, unsigned long param)
Eli Cohene126ba92013-07-07 17:25:49 +03002696{
Jack Morgenstein9603b612014-07-28 23:30:22 +03002697 struct mlx5_ib_dev *ibdev = (struct mlx5_ib_dev *)context;
Eli Cohene126ba92013-07-07 17:25:49 +03002698 struct ib_event ibev;
Eli Cohendbaaff22016-10-27 16:36:44 +03002699 bool fatal = false;
Eli Cohene126ba92013-07-07 17:25:49 +03002700 u8 port = 0;
2701
2702 switch (event) {
2703 case MLX5_DEV_EVENT_SYS_ERROR:
Eli Cohene126ba92013-07-07 17:25:49 +03002704 ibev.event = IB_EVENT_DEVICE_FATAL;
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03002705 mlx5_ib_handle_internal_error(ibdev);
Eli Cohendbaaff22016-10-27 16:36:44 +03002706 fatal = true;
Eli Cohene126ba92013-07-07 17:25:49 +03002707 break;
2708
2709 case MLX5_DEV_EVENT_PORT_UP:
Eli Cohene126ba92013-07-07 17:25:49 +03002710 case MLX5_DEV_EVENT_PORT_DOWN:
Noa Osherovich2788cf32016-06-04 15:15:29 +03002711 case MLX5_DEV_EVENT_PORT_INITIALIZED:
Jack Morgenstein4d2f9bb2014-07-28 23:30:24 +03002712 port = (u8)param;
Aviv Heller5ec8c832016-09-18 20:48:00 +03002713
2714 /* In RoCE, port up/down events are handled in
2715 * mlx5_netdev_event().
2716 */
2717 if (mlx5_ib_port_link_layer(&ibdev->ib_dev, port) ==
2718 IB_LINK_LAYER_ETHERNET)
2719 return;
2720
2721 ibev.event = (event == MLX5_DEV_EVENT_PORT_UP) ?
2722 IB_EVENT_PORT_ACTIVE : IB_EVENT_PORT_ERR;
Eli Cohene126ba92013-07-07 17:25:49 +03002723 break;
2724
Eli Cohene126ba92013-07-07 17:25:49 +03002725 case MLX5_DEV_EVENT_LID_CHANGE:
2726 ibev.event = IB_EVENT_LID_CHANGE;
Jack Morgenstein4d2f9bb2014-07-28 23:30:24 +03002727 port = (u8)param;
Eli Cohene126ba92013-07-07 17:25:49 +03002728 break;
2729
2730 case MLX5_DEV_EVENT_PKEY_CHANGE:
2731 ibev.event = IB_EVENT_PKEY_CHANGE;
Jack Morgenstein4d2f9bb2014-07-28 23:30:24 +03002732 port = (u8)param;
Haggai Eran7722f472016-02-29 15:45:07 +02002733
2734 schedule_work(&ibdev->devr.ports[port - 1].pkey_change_work);
Eli Cohene126ba92013-07-07 17:25:49 +03002735 break;
2736
2737 case MLX5_DEV_EVENT_GUID_CHANGE:
2738 ibev.event = IB_EVENT_GID_CHANGE;
Jack Morgenstein4d2f9bb2014-07-28 23:30:24 +03002739 port = (u8)param;
Eli Cohene126ba92013-07-07 17:25:49 +03002740 break;
2741
2742 case MLX5_DEV_EVENT_CLIENT_REREG:
2743 ibev.event = IB_EVENT_CLIENT_REREGISTER;
Jack Morgenstein4d2f9bb2014-07-28 23:30:24 +03002744 port = (u8)param;
Eli Cohene126ba92013-07-07 17:25:49 +03002745 break;
Saeed Mahameedbdc37922016-09-29 19:35:38 +03002746 default:
2747 return;
Eli Cohene126ba92013-07-07 17:25:49 +03002748 }
2749
2750 ibev.device = &ibdev->ib_dev;
2751 ibev.element.port_num = port;
2752
Eli Cohena0c84c32013-09-11 16:35:27 +03002753 if (port < 1 || port > ibdev->num_ports) {
2754 mlx5_ib_warn(ibdev, "warning: event on port %d\n", port);
2755 return;
2756 }
2757
Eli Cohene126ba92013-07-07 17:25:49 +03002758 if (ibdev->ib_active)
2759 ib_dispatch_event(&ibev);
Eli Cohendbaaff22016-10-27 16:36:44 +03002760
2761 if (fatal)
2762 ibdev->ib_active = false;
Eli Cohene126ba92013-07-07 17:25:49 +03002763}
2764
Maor Gottliebc43f1112017-01-18 14:10:33 +02002765static int set_has_smi_cap(struct mlx5_ib_dev *dev)
2766{
2767 struct mlx5_hca_vport_context vport_ctx;
2768 int err;
2769 int port;
2770
2771 for (port = 1; port <= MLX5_CAP_GEN(dev->mdev, num_ports); port++) {
2772 dev->mdev->port_caps[port - 1].has_smi = false;
2773 if (MLX5_CAP_GEN(dev->mdev, port_type) ==
2774 MLX5_CAP_PORT_TYPE_IB) {
2775 if (MLX5_CAP_GEN(dev->mdev, ib_virt)) {
2776 err = mlx5_query_hca_vport_context(dev->mdev, 0,
2777 port, 0,
2778 &vport_ctx);
2779 if (err) {
2780 mlx5_ib_err(dev, "query_hca_vport_context for port=%d failed %d\n",
2781 port, err);
2782 return err;
2783 }
2784 dev->mdev->port_caps[port - 1].has_smi =
2785 vport_ctx.has_smi;
2786 } else {
2787 dev->mdev->port_caps[port - 1].has_smi = true;
2788 }
2789 }
2790 }
2791 return 0;
2792}
2793
Eli Cohene126ba92013-07-07 17:25:49 +03002794static void get_ext_port_caps(struct mlx5_ib_dev *dev)
2795{
2796 int port;
2797
Saeed Mahameed938fe832015-05-28 22:28:41 +03002798 for (port = 1; port <= MLX5_CAP_GEN(dev->mdev, num_ports); port++)
Eli Cohene126ba92013-07-07 17:25:49 +03002799 mlx5_query_ext_port_caps(dev, port);
2800}
2801
2802static int get_port_caps(struct mlx5_ib_dev *dev)
2803{
2804 struct ib_device_attr *dprops = NULL;
2805 struct ib_port_attr *pprops = NULL;
Dan Carpenterf614fc12015-01-12 11:56:58 +03002806 int err = -ENOMEM;
Eli Cohene126ba92013-07-07 17:25:49 +03002807 int port;
Matan Barak2528e332015-06-11 16:35:25 +03002808 struct ib_udata uhw = {.inlen = 0, .outlen = 0};
Eli Cohene126ba92013-07-07 17:25:49 +03002809
2810 pprops = kmalloc(sizeof(*pprops), GFP_KERNEL);
2811 if (!pprops)
2812 goto out;
2813
2814 dprops = kmalloc(sizeof(*dprops), GFP_KERNEL);
2815 if (!dprops)
2816 goto out;
2817
Maor Gottliebc43f1112017-01-18 14:10:33 +02002818 err = set_has_smi_cap(dev);
2819 if (err)
2820 goto out;
2821
Matan Barak2528e332015-06-11 16:35:25 +03002822 err = mlx5_ib_query_device(&dev->ib_dev, dprops, &uhw);
Eli Cohene126ba92013-07-07 17:25:49 +03002823 if (err) {
2824 mlx5_ib_warn(dev, "query_device failed %d\n", err);
2825 goto out;
2826 }
2827
Saeed Mahameed938fe832015-05-28 22:28:41 +03002828 for (port = 1; port <= MLX5_CAP_GEN(dev->mdev, num_ports); port++) {
Or Gerlitzc4550c62017-01-24 13:02:39 +02002829 memset(pprops, 0, sizeof(*pprops));
Eli Cohene126ba92013-07-07 17:25:49 +03002830 err = mlx5_ib_query_port(&dev->ib_dev, port, pprops);
2831 if (err) {
Saeed Mahameed938fe832015-05-28 22:28:41 +03002832 mlx5_ib_warn(dev, "query_port %d failed %d\n",
2833 port, err);
Eli Cohene126ba92013-07-07 17:25:49 +03002834 break;
2835 }
Saeed Mahameed938fe832015-05-28 22:28:41 +03002836 dev->mdev->port_caps[port - 1].pkey_table_len =
2837 dprops->max_pkeys;
2838 dev->mdev->port_caps[port - 1].gid_table_len =
2839 pprops->gid_tbl_len;
Eli Cohene126ba92013-07-07 17:25:49 +03002840 mlx5_ib_dbg(dev, "pkey_table_len %d, gid_table_len %d\n",
2841 dprops->max_pkeys, pprops->gid_tbl_len);
2842 }
2843
2844out:
2845 kfree(pprops);
2846 kfree(dprops);
2847
2848 return err;
2849}
2850
2851static void destroy_umrc_res(struct mlx5_ib_dev *dev)
2852{
2853 int err;
2854
2855 err = mlx5_mr_cache_cleanup(dev);
2856 if (err)
2857 mlx5_ib_warn(dev, "mr cache cleanup failed\n");
2858
2859 mlx5_ib_destroy_qp(dev->umrc.qp);
Christoph Hellwigadd08d72016-03-03 09:38:22 +01002860 ib_free_cq(dev->umrc.cq);
Eli Cohene126ba92013-07-07 17:25:49 +03002861 ib_dealloc_pd(dev->umrc.pd);
2862}
2863
2864enum {
2865 MAX_UMR_WR = 128,
2866};
2867
2868static int create_umr_res(struct mlx5_ib_dev *dev)
2869{
2870 struct ib_qp_init_attr *init_attr = NULL;
2871 struct ib_qp_attr *attr = NULL;
2872 struct ib_pd *pd;
2873 struct ib_cq *cq;
2874 struct ib_qp *qp;
Eli Cohene126ba92013-07-07 17:25:49 +03002875 int ret;
2876
2877 attr = kzalloc(sizeof(*attr), GFP_KERNEL);
2878 init_attr = kzalloc(sizeof(*init_attr), GFP_KERNEL);
2879 if (!attr || !init_attr) {
2880 ret = -ENOMEM;
2881 goto error_0;
2882 }
2883
Christoph Hellwiged082d32016-09-05 12:56:17 +02002884 pd = ib_alloc_pd(&dev->ib_dev, 0);
Eli Cohene126ba92013-07-07 17:25:49 +03002885 if (IS_ERR(pd)) {
2886 mlx5_ib_dbg(dev, "Couldn't create PD for sync UMR QP\n");
2887 ret = PTR_ERR(pd);
2888 goto error_0;
2889 }
2890
Christoph Hellwigadd08d72016-03-03 09:38:22 +01002891 cq = ib_alloc_cq(&dev->ib_dev, NULL, 128, 0, IB_POLL_SOFTIRQ);
Eli Cohene126ba92013-07-07 17:25:49 +03002892 if (IS_ERR(cq)) {
2893 mlx5_ib_dbg(dev, "Couldn't create CQ for sync UMR QP\n");
2894 ret = PTR_ERR(cq);
2895 goto error_2;
2896 }
Eli Cohene126ba92013-07-07 17:25:49 +03002897
2898 init_attr->send_cq = cq;
2899 init_attr->recv_cq = cq;
2900 init_attr->sq_sig_type = IB_SIGNAL_ALL_WR;
2901 init_attr->cap.max_send_wr = MAX_UMR_WR;
2902 init_attr->cap.max_send_sge = 1;
2903 init_attr->qp_type = MLX5_IB_QPT_REG_UMR;
2904 init_attr->port_num = 1;
2905 qp = mlx5_ib_create_qp(pd, init_attr, NULL);
2906 if (IS_ERR(qp)) {
2907 mlx5_ib_dbg(dev, "Couldn't create sync UMR QP\n");
2908 ret = PTR_ERR(qp);
2909 goto error_3;
2910 }
2911 qp->device = &dev->ib_dev;
2912 qp->real_qp = qp;
2913 qp->uobject = NULL;
2914 qp->qp_type = MLX5_IB_QPT_REG_UMR;
2915
2916 attr->qp_state = IB_QPS_INIT;
2917 attr->port_num = 1;
2918 ret = mlx5_ib_modify_qp(qp, attr, IB_QP_STATE | IB_QP_PKEY_INDEX |
2919 IB_QP_PORT, NULL);
2920 if (ret) {
2921 mlx5_ib_dbg(dev, "Couldn't modify UMR QP\n");
2922 goto error_4;
2923 }
2924
2925 memset(attr, 0, sizeof(*attr));
2926 attr->qp_state = IB_QPS_RTR;
2927 attr->path_mtu = IB_MTU_256;
2928
2929 ret = mlx5_ib_modify_qp(qp, attr, IB_QP_STATE, NULL);
2930 if (ret) {
2931 mlx5_ib_dbg(dev, "Couldn't modify umr QP to rtr\n");
2932 goto error_4;
2933 }
2934
2935 memset(attr, 0, sizeof(*attr));
2936 attr->qp_state = IB_QPS_RTS;
2937 ret = mlx5_ib_modify_qp(qp, attr, IB_QP_STATE, NULL);
2938 if (ret) {
2939 mlx5_ib_dbg(dev, "Couldn't modify umr QP to rts\n");
2940 goto error_4;
2941 }
2942
2943 dev->umrc.qp = qp;
2944 dev->umrc.cq = cq;
Eli Cohene126ba92013-07-07 17:25:49 +03002945 dev->umrc.pd = pd;
2946
2947 sema_init(&dev->umrc.sem, MAX_UMR_WR);
2948 ret = mlx5_mr_cache_init(dev);
2949 if (ret) {
2950 mlx5_ib_warn(dev, "mr cache init failed %d\n", ret);
2951 goto error_4;
2952 }
2953
2954 kfree(attr);
2955 kfree(init_attr);
2956
2957 return 0;
2958
2959error_4:
2960 mlx5_ib_destroy_qp(qp);
2961
2962error_3:
Christoph Hellwigadd08d72016-03-03 09:38:22 +01002963 ib_free_cq(cq);
Eli Cohene126ba92013-07-07 17:25:49 +03002964
2965error_2:
Eli Cohene126ba92013-07-07 17:25:49 +03002966 ib_dealloc_pd(pd);
2967
2968error_0:
2969 kfree(attr);
2970 kfree(init_attr);
2971 return ret;
2972}
2973
Max Gurtovoy6e8484c2017-05-28 10:53:11 +03002974static u8 mlx5_get_umr_fence(u8 umr_fence_cap)
2975{
2976 switch (umr_fence_cap) {
2977 case MLX5_CAP_UMR_FENCE_NONE:
2978 return MLX5_FENCE_MODE_NONE;
2979 case MLX5_CAP_UMR_FENCE_SMALL:
2980 return MLX5_FENCE_MODE_INITIATOR_SMALL;
2981 default:
2982 return MLX5_FENCE_MODE_STRONG_ORDERING;
2983 }
2984}
2985
Eli Cohene126ba92013-07-07 17:25:49 +03002986static int create_dev_resources(struct mlx5_ib_resources *devr)
2987{
2988 struct ib_srq_init_attr attr;
2989 struct mlx5_ib_dev *dev;
Matan Barakbcf4c1e2015-06-11 16:35:20 +03002990 struct ib_cq_init_attr cq_attr = {.cqe = 1};
Haggai Eran7722f472016-02-29 15:45:07 +02002991 int port;
Eli Cohene126ba92013-07-07 17:25:49 +03002992 int ret = 0;
2993
2994 dev = container_of(devr, struct mlx5_ib_dev, devr);
2995
Haggai Erand16e91d2016-02-29 15:45:05 +02002996 mutex_init(&devr->mutex);
2997
Eli Cohene126ba92013-07-07 17:25:49 +03002998 devr->p0 = mlx5_ib_alloc_pd(&dev->ib_dev, NULL, NULL);
2999 if (IS_ERR(devr->p0)) {
3000 ret = PTR_ERR(devr->p0);
3001 goto error0;
3002 }
3003 devr->p0->device = &dev->ib_dev;
3004 devr->p0->uobject = NULL;
3005 atomic_set(&devr->p0->usecnt, 0);
3006
Matan Barakbcf4c1e2015-06-11 16:35:20 +03003007 devr->c0 = mlx5_ib_create_cq(&dev->ib_dev, &cq_attr, NULL, NULL);
Eli Cohene126ba92013-07-07 17:25:49 +03003008 if (IS_ERR(devr->c0)) {
3009 ret = PTR_ERR(devr->c0);
3010 goto error1;
3011 }
3012 devr->c0->device = &dev->ib_dev;
3013 devr->c0->uobject = NULL;
3014 devr->c0->comp_handler = NULL;
3015 devr->c0->event_handler = NULL;
3016 devr->c0->cq_context = NULL;
3017 atomic_set(&devr->c0->usecnt, 0);
3018
3019 devr->x0 = mlx5_ib_alloc_xrcd(&dev->ib_dev, NULL, NULL);
3020 if (IS_ERR(devr->x0)) {
3021 ret = PTR_ERR(devr->x0);
3022 goto error2;
3023 }
3024 devr->x0->device = &dev->ib_dev;
3025 devr->x0->inode = NULL;
3026 atomic_set(&devr->x0->usecnt, 0);
3027 mutex_init(&devr->x0->tgt_qp_mutex);
3028 INIT_LIST_HEAD(&devr->x0->tgt_qp_list);
3029
3030 devr->x1 = mlx5_ib_alloc_xrcd(&dev->ib_dev, NULL, NULL);
3031 if (IS_ERR(devr->x1)) {
3032 ret = PTR_ERR(devr->x1);
3033 goto error3;
3034 }
3035 devr->x1->device = &dev->ib_dev;
3036 devr->x1->inode = NULL;
3037 atomic_set(&devr->x1->usecnt, 0);
3038 mutex_init(&devr->x1->tgt_qp_mutex);
3039 INIT_LIST_HEAD(&devr->x1->tgt_qp_list);
3040
3041 memset(&attr, 0, sizeof(attr));
3042 attr.attr.max_sge = 1;
3043 attr.attr.max_wr = 1;
3044 attr.srq_type = IB_SRQT_XRC;
3045 attr.ext.xrc.cq = devr->c0;
3046 attr.ext.xrc.xrcd = devr->x0;
3047
3048 devr->s0 = mlx5_ib_create_srq(devr->p0, &attr, NULL);
3049 if (IS_ERR(devr->s0)) {
3050 ret = PTR_ERR(devr->s0);
3051 goto error4;
3052 }
3053 devr->s0->device = &dev->ib_dev;
3054 devr->s0->pd = devr->p0;
3055 devr->s0->uobject = NULL;
3056 devr->s0->event_handler = NULL;
3057 devr->s0->srq_context = NULL;
3058 devr->s0->srq_type = IB_SRQT_XRC;
3059 devr->s0->ext.xrc.xrcd = devr->x0;
3060 devr->s0->ext.xrc.cq = devr->c0;
3061 atomic_inc(&devr->s0->ext.xrc.xrcd->usecnt);
3062 atomic_inc(&devr->s0->ext.xrc.cq->usecnt);
3063 atomic_inc(&devr->p0->usecnt);
3064 atomic_set(&devr->s0->usecnt, 0);
3065
Haggai Abramonvsky4aa17b22015-06-04 19:30:48 +03003066 memset(&attr, 0, sizeof(attr));
3067 attr.attr.max_sge = 1;
3068 attr.attr.max_wr = 1;
3069 attr.srq_type = IB_SRQT_BASIC;
3070 devr->s1 = mlx5_ib_create_srq(devr->p0, &attr, NULL);
3071 if (IS_ERR(devr->s1)) {
3072 ret = PTR_ERR(devr->s1);
3073 goto error5;
3074 }
3075 devr->s1->device = &dev->ib_dev;
3076 devr->s1->pd = devr->p0;
3077 devr->s1->uobject = NULL;
3078 devr->s1->event_handler = NULL;
3079 devr->s1->srq_context = NULL;
3080 devr->s1->srq_type = IB_SRQT_BASIC;
3081 devr->s1->ext.xrc.cq = devr->c0;
3082 atomic_inc(&devr->p0->usecnt);
3083 atomic_set(&devr->s0->usecnt, 0);
3084
Haggai Eran7722f472016-02-29 15:45:07 +02003085 for (port = 0; port < ARRAY_SIZE(devr->ports); ++port) {
3086 INIT_WORK(&devr->ports[port].pkey_change_work,
3087 pkey_change_handler);
3088 devr->ports[port].devr = devr;
3089 }
3090
Eli Cohene126ba92013-07-07 17:25:49 +03003091 return 0;
3092
Haggai Abramonvsky4aa17b22015-06-04 19:30:48 +03003093error5:
3094 mlx5_ib_destroy_srq(devr->s0);
Eli Cohene126ba92013-07-07 17:25:49 +03003095error4:
3096 mlx5_ib_dealloc_xrcd(devr->x1);
3097error3:
3098 mlx5_ib_dealloc_xrcd(devr->x0);
3099error2:
3100 mlx5_ib_destroy_cq(devr->c0);
3101error1:
3102 mlx5_ib_dealloc_pd(devr->p0);
3103error0:
3104 return ret;
3105}
3106
3107static void destroy_dev_resources(struct mlx5_ib_resources *devr)
3108{
Haggai Eran7722f472016-02-29 15:45:07 +02003109 struct mlx5_ib_dev *dev =
3110 container_of(devr, struct mlx5_ib_dev, devr);
3111 int port;
3112
Haggai Abramonvsky4aa17b22015-06-04 19:30:48 +03003113 mlx5_ib_destroy_srq(devr->s1);
Eli Cohene126ba92013-07-07 17:25:49 +03003114 mlx5_ib_destroy_srq(devr->s0);
3115 mlx5_ib_dealloc_xrcd(devr->x0);
3116 mlx5_ib_dealloc_xrcd(devr->x1);
3117 mlx5_ib_destroy_cq(devr->c0);
3118 mlx5_ib_dealloc_pd(devr->p0);
Haggai Eran7722f472016-02-29 15:45:07 +02003119
3120 /* Make sure no change P_Key work items are still executing */
3121 for (port = 0; port < dev->num_ports; ++port)
3122 cancel_work_sync(&devr->ports[port].pkey_change_work);
Eli Cohene126ba92013-07-07 17:25:49 +03003123}
3124
Achiad Shochate53505a2015-12-23 18:47:25 +02003125static u32 get_core_cap_flags(struct ib_device *ibdev)
3126{
3127 struct mlx5_ib_dev *dev = to_mdev(ibdev);
3128 enum rdma_link_layer ll = mlx5_ib_port_link_layer(ibdev, 1);
3129 u8 l3_type_cap = MLX5_CAP_ROCE(dev->mdev, l3_type);
3130 u8 roce_version_cap = MLX5_CAP_ROCE(dev->mdev, roce_version);
3131 u32 ret = 0;
3132
3133 if (ll == IB_LINK_LAYER_INFINIBAND)
3134 return RDMA_CORE_PORT_IBA_IB;
3135
Or Gerlitz72cd5712017-01-24 13:02:36 +02003136 ret = RDMA_CORE_PORT_RAW_PACKET;
3137
Achiad Shochate53505a2015-12-23 18:47:25 +02003138 if (!(l3_type_cap & MLX5_ROCE_L3_TYPE_IPV4_CAP))
Or Gerlitz72cd5712017-01-24 13:02:36 +02003139 return ret;
Achiad Shochate53505a2015-12-23 18:47:25 +02003140
3141 if (!(l3_type_cap & MLX5_ROCE_L3_TYPE_IPV6_CAP))
Or Gerlitz72cd5712017-01-24 13:02:36 +02003142 return ret;
Achiad Shochate53505a2015-12-23 18:47:25 +02003143
3144 if (roce_version_cap & MLX5_ROCE_VERSION_1_CAP)
3145 ret |= RDMA_CORE_PORT_IBA_ROCE;
3146
3147 if (roce_version_cap & MLX5_ROCE_VERSION_2_CAP)
3148 ret |= RDMA_CORE_PORT_IBA_ROCE_UDP_ENCAP;
3149
3150 return ret;
3151}
3152
Ira Weiny77386132015-05-13 20:02:58 -04003153static int mlx5_port_immutable(struct ib_device *ibdev, u8 port_num,
3154 struct ib_port_immutable *immutable)
3155{
3156 struct ib_port_attr attr;
Or Gerlitzca5b91d2016-11-27 16:51:36 +02003157 struct mlx5_ib_dev *dev = to_mdev(ibdev);
3158 enum rdma_link_layer ll = mlx5_ib_port_link_layer(ibdev, port_num);
Ira Weiny77386132015-05-13 20:02:58 -04003159 int err;
3160
Or Gerlitzc4550c62017-01-24 13:02:39 +02003161 immutable->core_cap_flags = get_core_cap_flags(ibdev);
3162
3163 err = ib_query_port(ibdev, port_num, &attr);
Ira Weiny77386132015-05-13 20:02:58 -04003164 if (err)
3165 return err;
3166
3167 immutable->pkey_tbl_len = attr.pkey_tbl_len;
3168 immutable->gid_tbl_len = attr.gid_tbl_len;
Achiad Shochate53505a2015-12-23 18:47:25 +02003169 immutable->core_cap_flags = get_core_cap_flags(ibdev);
Or Gerlitzca5b91d2016-11-27 16:51:36 +02003170 if ((ll == IB_LINK_LAYER_INFINIBAND) || MLX5_CAP_GEN(dev->mdev, roce))
3171 immutable->max_mad_size = IB_MGMT_MAD_SIZE;
Ira Weiny77386132015-05-13 20:02:58 -04003172
3173 return 0;
3174}
3175
Ira Weinyc7342822016-06-15 02:22:01 -04003176static void get_dev_fw_str(struct ib_device *ibdev, char *str,
3177 size_t str_len)
3178{
3179 struct mlx5_ib_dev *dev =
3180 container_of(ibdev, struct mlx5_ib_dev, ib_dev);
3181 snprintf(str, str_len, "%d.%d.%04d", fw_rev_maj(dev->mdev),
3182 fw_rev_min(dev->mdev), fw_rev_sub(dev->mdev));
3183}
3184
Or Gerlitz45f95ac2016-11-27 16:51:35 +02003185static int mlx5_eth_lag_init(struct mlx5_ib_dev *dev)
Aviv Heller9ef9c642016-09-18 20:48:01 +03003186{
3187 struct mlx5_core_dev *mdev = dev->mdev;
3188 struct mlx5_flow_namespace *ns = mlx5_get_flow_namespace(mdev,
3189 MLX5_FLOW_NAMESPACE_LAG);
3190 struct mlx5_flow_table *ft;
3191 int err;
3192
3193 if (!ns || !mlx5_lag_is_active(mdev))
3194 return 0;
3195
3196 err = mlx5_cmd_create_vport_lag(mdev);
3197 if (err)
3198 return err;
3199
3200 ft = mlx5_create_lag_demux_flow_table(ns, 0, 0);
3201 if (IS_ERR(ft)) {
3202 err = PTR_ERR(ft);
3203 goto err_destroy_vport_lag;
3204 }
3205
3206 dev->flow_db.lag_demux_ft = ft;
3207 return 0;
3208
3209err_destroy_vport_lag:
3210 mlx5_cmd_destroy_vport_lag(mdev);
3211 return err;
3212}
3213
Or Gerlitz45f95ac2016-11-27 16:51:35 +02003214static void mlx5_eth_lag_cleanup(struct mlx5_ib_dev *dev)
Aviv Heller9ef9c642016-09-18 20:48:01 +03003215{
3216 struct mlx5_core_dev *mdev = dev->mdev;
3217
3218 if (dev->flow_db.lag_demux_ft) {
3219 mlx5_destroy_flow_table(dev->flow_db.lag_demux_ft);
3220 dev->flow_db.lag_demux_ft = NULL;
3221
3222 mlx5_cmd_destroy_vport_lag(mdev);
3223 }
3224}
3225
Or Gerlitzd012f5d2016-11-27 16:51:34 +02003226static int mlx5_add_netdev_notifier(struct mlx5_ib_dev *dev)
Achiad Shochatfc24fc52015-12-23 18:47:17 +02003227{
Achiad Shochate53505a2015-12-23 18:47:25 +02003228 int err;
3229
Achiad Shochatfc24fc52015-12-23 18:47:17 +02003230 dev->roce.nb.notifier_call = mlx5_netdev_event;
Achiad Shochate53505a2015-12-23 18:47:25 +02003231 err = register_netdevice_notifier(&dev->roce.nb);
Aviv Heller5ec8c832016-09-18 20:48:00 +03003232 if (err) {
3233 dev->roce.nb.notifier_call = NULL;
Achiad Shochate53505a2015-12-23 18:47:25 +02003234 return err;
Aviv Heller5ec8c832016-09-18 20:48:00 +03003235 }
Achiad Shochate53505a2015-12-23 18:47:25 +02003236
Or Gerlitzd012f5d2016-11-27 16:51:34 +02003237 return 0;
3238}
Achiad Shochate53505a2015-12-23 18:47:25 +02003239
Or Gerlitzd012f5d2016-11-27 16:51:34 +02003240static void mlx5_remove_netdev_notifier(struct mlx5_ib_dev *dev)
Eli Cohene126ba92013-07-07 17:25:49 +03003241{
3242 if (dev->roce.nb.notifier_call) {
3243 unregister_netdevice_notifier(&dev->roce.nb);
3244 dev->roce.nb.notifier_call = NULL;
3245 }
3246}
3247
Or Gerlitz45f95ac2016-11-27 16:51:35 +02003248static int mlx5_enable_eth(struct mlx5_ib_dev *dev)
Eli Cohene126ba92013-07-07 17:25:49 +03003249{
Eli Cohene126ba92013-07-07 17:25:49 +03003250 int err;
3251
Or Gerlitzd012f5d2016-11-27 16:51:34 +02003252 err = mlx5_add_netdev_notifier(dev);
3253 if (err)
Achiad Shochate53505a2015-12-23 18:47:25 +02003254 return err;
Achiad Shochate53505a2015-12-23 18:47:25 +02003255
Or Gerlitzca5b91d2016-11-27 16:51:36 +02003256 if (MLX5_CAP_GEN(dev->mdev, roce)) {
3257 err = mlx5_nic_vport_enable_roce(dev->mdev);
3258 if (err)
3259 goto err_unregister_netdevice_notifier;
3260 }
Achiad Shochate53505a2015-12-23 18:47:25 +02003261
Or Gerlitz45f95ac2016-11-27 16:51:35 +02003262 err = mlx5_eth_lag_init(dev);
Aviv Heller9ef9c642016-09-18 20:48:01 +03003263 if (err)
3264 goto err_disable_roce;
3265
Achiad Shochate53505a2015-12-23 18:47:25 +02003266 return 0;
3267
Aviv Heller9ef9c642016-09-18 20:48:01 +03003268err_disable_roce:
Or Gerlitzca5b91d2016-11-27 16:51:36 +02003269 if (MLX5_CAP_GEN(dev->mdev, roce))
3270 mlx5_nic_vport_disable_roce(dev->mdev);
Aviv Heller9ef9c642016-09-18 20:48:01 +03003271
Achiad Shochate53505a2015-12-23 18:47:25 +02003272err_unregister_netdevice_notifier:
Or Gerlitzd012f5d2016-11-27 16:51:34 +02003273 mlx5_remove_netdev_notifier(dev);
Achiad Shochate53505a2015-12-23 18:47:25 +02003274 return err;
Achiad Shochatfc24fc52015-12-23 18:47:17 +02003275}
3276
Or Gerlitz45f95ac2016-11-27 16:51:35 +02003277static void mlx5_disable_eth(struct mlx5_ib_dev *dev)
Achiad Shochatfc24fc52015-12-23 18:47:17 +02003278{
Or Gerlitz45f95ac2016-11-27 16:51:35 +02003279 mlx5_eth_lag_cleanup(dev);
Or Gerlitzca5b91d2016-11-27 16:51:36 +02003280 if (MLX5_CAP_GEN(dev->mdev, roce))
3281 mlx5_nic_vport_disable_roce(dev->mdev);
Achiad Shochatfc24fc52015-12-23 18:47:17 +02003282}
3283
Parav Pandite1f24a72017-04-16 07:29:29 +03003284struct mlx5_ib_counter {
Kamal Heib7c16f472017-01-18 15:25:09 +02003285 const char *name;
3286 size_t offset;
3287};
3288
3289#define INIT_Q_COUNTER(_name) \
3290 { .name = #_name, .offset = MLX5_BYTE_OFF(query_q_counter_out, _name)}
3291
Parav Pandite1f24a72017-04-16 07:29:29 +03003292static const struct mlx5_ib_counter basic_q_cnts[] = {
Kamal Heib7c16f472017-01-18 15:25:09 +02003293 INIT_Q_COUNTER(rx_write_requests),
3294 INIT_Q_COUNTER(rx_read_requests),
3295 INIT_Q_COUNTER(rx_atomic_requests),
3296 INIT_Q_COUNTER(out_of_buffer),
3297};
3298
Parav Pandite1f24a72017-04-16 07:29:29 +03003299static const struct mlx5_ib_counter out_of_seq_q_cnts[] = {
Kamal Heib7c16f472017-01-18 15:25:09 +02003300 INIT_Q_COUNTER(out_of_sequence),
3301};
3302
Parav Pandite1f24a72017-04-16 07:29:29 +03003303static const struct mlx5_ib_counter retrans_q_cnts[] = {
Kamal Heib7c16f472017-01-18 15:25:09 +02003304 INIT_Q_COUNTER(duplicate_request),
3305 INIT_Q_COUNTER(rnr_nak_retry_err),
3306 INIT_Q_COUNTER(packet_seq_err),
3307 INIT_Q_COUNTER(implied_nak_seq_err),
3308 INIT_Q_COUNTER(local_ack_timeout_err),
3309};
3310
Parav Pandite1f24a72017-04-16 07:29:29 +03003311#define INIT_CONG_COUNTER(_name) \
3312 { .name = #_name, .offset = \
3313 MLX5_BYTE_OFF(query_cong_statistics_out, _name ## _high)}
3314
3315static const struct mlx5_ib_counter cong_cnts[] = {
3316 INIT_CONG_COUNTER(rp_cnp_ignored),
3317 INIT_CONG_COUNTER(rp_cnp_handled),
3318 INIT_CONG_COUNTER(np_ecn_marked_roce_packets),
3319 INIT_CONG_COUNTER(np_cnp_sent),
3320};
3321
3322static void mlx5_ib_dealloc_counters(struct mlx5_ib_dev *dev)
Mark Bloch0837e862016-06-17 15:10:55 +03003323{
3324 unsigned int i;
3325
Kamal Heib7c16f472017-01-18 15:25:09 +02003326 for (i = 0; i < dev->num_ports; i++) {
Mark Bloch0837e862016-06-17 15:10:55 +03003327 mlx5_core_dealloc_q_counter(dev->mdev,
Parav Pandite1f24a72017-04-16 07:29:29 +03003328 dev->port[i].cnts.set_id);
3329 kfree(dev->port[i].cnts.names);
3330 kfree(dev->port[i].cnts.offsets);
Kamal Heib7c16f472017-01-18 15:25:09 +02003331 }
3332}
3333
Parav Pandite1f24a72017-04-16 07:29:29 +03003334static int __mlx5_ib_alloc_counters(struct mlx5_ib_dev *dev,
3335 struct mlx5_ib_counters *cnts)
Kamal Heib7c16f472017-01-18 15:25:09 +02003336{
3337 u32 num_counters;
3338
3339 num_counters = ARRAY_SIZE(basic_q_cnts);
3340
3341 if (MLX5_CAP_GEN(dev->mdev, out_of_seq_cnt))
3342 num_counters += ARRAY_SIZE(out_of_seq_q_cnts);
3343
3344 if (MLX5_CAP_GEN(dev->mdev, retransmission_q_counters))
3345 num_counters += ARRAY_SIZE(retrans_q_cnts);
Parav Pandite1f24a72017-04-16 07:29:29 +03003346 cnts->num_q_counters = num_counters;
Kamal Heib7c16f472017-01-18 15:25:09 +02003347
Parav Pandite1f24a72017-04-16 07:29:29 +03003348 if (MLX5_CAP_GEN(dev->mdev, cc_query_allowed)) {
3349 cnts->num_cong_counters = ARRAY_SIZE(cong_cnts);
3350 num_counters += ARRAY_SIZE(cong_cnts);
3351 }
3352
3353 cnts->names = kcalloc(num_counters, sizeof(cnts->names), GFP_KERNEL);
3354 if (!cnts->names)
Kamal Heib7c16f472017-01-18 15:25:09 +02003355 return -ENOMEM;
3356
Parav Pandite1f24a72017-04-16 07:29:29 +03003357 cnts->offsets = kcalloc(num_counters,
3358 sizeof(cnts->offsets), GFP_KERNEL);
3359 if (!cnts->offsets)
Kamal Heib7c16f472017-01-18 15:25:09 +02003360 goto err_names;
3361
Kamal Heib7c16f472017-01-18 15:25:09 +02003362 return 0;
3363
3364err_names:
Parav Pandite1f24a72017-04-16 07:29:29 +03003365 kfree(cnts->names);
Kamal Heib7c16f472017-01-18 15:25:09 +02003366 return -ENOMEM;
3367}
3368
Parav Pandite1f24a72017-04-16 07:29:29 +03003369static void mlx5_ib_fill_counters(struct mlx5_ib_dev *dev,
3370 const char **names,
3371 size_t *offsets)
Kamal Heib7c16f472017-01-18 15:25:09 +02003372{
3373 int i;
3374 int j = 0;
3375
3376 for (i = 0; i < ARRAY_SIZE(basic_q_cnts); i++, j++) {
3377 names[j] = basic_q_cnts[i].name;
3378 offsets[j] = basic_q_cnts[i].offset;
3379 }
3380
3381 if (MLX5_CAP_GEN(dev->mdev, out_of_seq_cnt)) {
3382 for (i = 0; i < ARRAY_SIZE(out_of_seq_q_cnts); i++, j++) {
3383 names[j] = out_of_seq_q_cnts[i].name;
3384 offsets[j] = out_of_seq_q_cnts[i].offset;
3385 }
3386 }
3387
3388 if (MLX5_CAP_GEN(dev->mdev, retransmission_q_counters)) {
3389 for (i = 0; i < ARRAY_SIZE(retrans_q_cnts); i++, j++) {
3390 names[j] = retrans_q_cnts[i].name;
3391 offsets[j] = retrans_q_cnts[i].offset;
3392 }
3393 }
Parav Pandite1f24a72017-04-16 07:29:29 +03003394
3395 if (MLX5_CAP_GEN(dev->mdev, cc_query_allowed)) {
3396 for (i = 0; i < ARRAY_SIZE(cong_cnts); i++, j++) {
3397 names[j] = cong_cnts[i].name;
3398 offsets[j] = cong_cnts[i].offset;
3399 }
3400 }
Mark Bloch0837e862016-06-17 15:10:55 +03003401}
3402
Parav Pandite1f24a72017-04-16 07:29:29 +03003403static int mlx5_ib_alloc_counters(struct mlx5_ib_dev *dev)
Mark Bloch0837e862016-06-17 15:10:55 +03003404{
3405 int i;
3406 int ret;
3407
3408 for (i = 0; i < dev->num_ports; i++) {
Kamal Heib7c16f472017-01-18 15:25:09 +02003409 struct mlx5_ib_port *port = &dev->port[i];
3410
Mark Bloch0837e862016-06-17 15:10:55 +03003411 ret = mlx5_core_alloc_q_counter(dev->mdev,
Parav Pandite1f24a72017-04-16 07:29:29 +03003412 &port->cnts.set_id);
Mark Bloch0837e862016-06-17 15:10:55 +03003413 if (ret) {
3414 mlx5_ib_warn(dev,
3415 "couldn't allocate queue counter for port %d, err %d\n",
3416 i + 1, ret);
3417 goto dealloc_counters;
3418 }
Kamal Heib7c16f472017-01-18 15:25:09 +02003419
Parav Pandite1f24a72017-04-16 07:29:29 +03003420 ret = __mlx5_ib_alloc_counters(dev, &port->cnts);
Kamal Heib7c16f472017-01-18 15:25:09 +02003421 if (ret)
3422 goto dealloc_counters;
3423
Parav Pandite1f24a72017-04-16 07:29:29 +03003424 mlx5_ib_fill_counters(dev, port->cnts.names,
3425 port->cnts.offsets);
Mark Bloch0837e862016-06-17 15:10:55 +03003426 }
3427
3428 return 0;
3429
3430dealloc_counters:
3431 while (--i >= 0)
3432 mlx5_core_dealloc_q_counter(dev->mdev,
Parav Pandite1f24a72017-04-16 07:29:29 +03003433 dev->port[i].cnts.set_id);
Mark Bloch0837e862016-06-17 15:10:55 +03003434
3435 return ret;
3436}
3437
Mark Bloch0ad17a82016-06-17 15:10:56 +03003438static struct rdma_hw_stats *mlx5_ib_alloc_hw_stats(struct ib_device *ibdev,
3439 u8 port_num)
3440{
Kamal Heib7c16f472017-01-18 15:25:09 +02003441 struct mlx5_ib_dev *dev = to_mdev(ibdev);
3442 struct mlx5_ib_port *port = &dev->port[port_num - 1];
Mark Bloch0ad17a82016-06-17 15:10:56 +03003443
3444 /* We support only per port stats */
3445 if (port_num == 0)
3446 return NULL;
3447
Parav Pandite1f24a72017-04-16 07:29:29 +03003448 return rdma_alloc_hw_stats_struct(port->cnts.names,
3449 port->cnts.num_q_counters +
3450 port->cnts.num_cong_counters,
Mark Bloch0ad17a82016-06-17 15:10:56 +03003451 RDMA_HW_STATS_DEFAULT_LIFESPAN);
3452}
3453
Parav Pandite1f24a72017-04-16 07:29:29 +03003454static int mlx5_ib_query_q_counters(struct mlx5_ib_dev *dev,
3455 struct mlx5_ib_port *port,
3456 struct rdma_hw_stats *stats)
3457{
3458 int outlen = MLX5_ST_SZ_BYTES(query_q_counter_out);
3459 void *out;
3460 __be32 val;
3461 int ret, i;
3462
Leon Romanovsky1b9a07e2017-05-10 21:32:18 +03003463 out = kvzalloc(outlen, GFP_KERNEL);
Parav Pandite1f24a72017-04-16 07:29:29 +03003464 if (!out)
3465 return -ENOMEM;
3466
3467 ret = mlx5_core_query_q_counter(dev->mdev,
3468 port->cnts.set_id, 0,
3469 out, outlen);
3470 if (ret)
3471 goto free;
3472
3473 for (i = 0; i < port->cnts.num_q_counters; i++) {
3474 val = *(__be32 *)(out + port->cnts.offsets[i]);
3475 stats->value[i] = (u64)be32_to_cpu(val);
3476 }
3477
3478free:
3479 kvfree(out);
3480 return ret;
3481}
3482
3483static int mlx5_ib_query_cong_counters(struct mlx5_ib_dev *dev,
3484 struct mlx5_ib_port *port,
3485 struct rdma_hw_stats *stats)
3486{
3487 int outlen = MLX5_ST_SZ_BYTES(query_cong_statistics_out);
3488 void *out;
3489 int ret, i;
3490 int offset = port->cnts.num_q_counters;
3491
Leon Romanovsky1b9a07e2017-05-10 21:32:18 +03003492 out = kvzalloc(outlen, GFP_KERNEL);
Parav Pandite1f24a72017-04-16 07:29:29 +03003493 if (!out)
3494 return -ENOMEM;
3495
3496 ret = mlx5_cmd_query_cong_counter(dev->mdev, false, out, outlen);
3497 if (ret)
3498 goto free;
3499
3500 for (i = 0; i < port->cnts.num_cong_counters; i++) {
3501 stats->value[i + offset] =
3502 be64_to_cpup((__be64 *)(out +
3503 port->cnts.offsets[i + offset]));
3504 }
3505
3506free:
3507 kvfree(out);
3508 return ret;
3509}
3510
Mark Bloch0ad17a82016-06-17 15:10:56 +03003511static int mlx5_ib_get_hw_stats(struct ib_device *ibdev,
3512 struct rdma_hw_stats *stats,
Kamal Heib7c16f472017-01-18 15:25:09 +02003513 u8 port_num, int index)
Mark Bloch0ad17a82016-06-17 15:10:56 +03003514{
3515 struct mlx5_ib_dev *dev = to_mdev(ibdev);
Kamal Heib7c16f472017-01-18 15:25:09 +02003516 struct mlx5_ib_port *port = &dev->port[port_num - 1];
Parav Pandite1f24a72017-04-16 07:29:29 +03003517 int ret, num_counters;
Mark Bloch0ad17a82016-06-17 15:10:56 +03003518
Kamal Heib7c16f472017-01-18 15:25:09 +02003519 if (!stats)
Parav Pandite1f24a72017-04-16 07:29:29 +03003520 return -EINVAL;
Mark Bloch0ad17a82016-06-17 15:10:56 +03003521
Parav Pandite1f24a72017-04-16 07:29:29 +03003522 ret = mlx5_ib_query_q_counters(dev, port, stats);
Mark Bloch0ad17a82016-06-17 15:10:56 +03003523 if (ret)
Parav Pandite1f24a72017-04-16 07:29:29 +03003524 return ret;
3525 num_counters = port->cnts.num_q_counters;
Mark Bloch0ad17a82016-06-17 15:10:56 +03003526
Parav Pandite1f24a72017-04-16 07:29:29 +03003527 if (MLX5_CAP_GEN(dev->mdev, cc_query_allowed)) {
3528 ret = mlx5_ib_query_cong_counters(dev, port, stats);
3529 if (ret)
3530 return ret;
3531 num_counters += port->cnts.num_cong_counters;
Mark Bloch0ad17a82016-06-17 15:10:56 +03003532 }
Kamal Heib7c16f472017-01-18 15:25:09 +02003533
Parav Pandite1f24a72017-04-16 07:29:29 +03003534 return num_counters;
Mark Bloch0ad17a82016-06-17 15:10:56 +03003535}
3536
Niranjana Vishwanathapura8e959602017-06-30 13:14:46 -07003537static void mlx5_ib_free_rdma_netdev(struct net_device *netdev)
3538{
3539 return mlx5_rdma_netdev_free(netdev);
3540}
3541
Erez Shitrit693dfd52017-04-27 17:01:34 +03003542static struct net_device*
3543mlx5_ib_alloc_rdma_netdev(struct ib_device *hca,
3544 u8 port_num,
3545 enum rdma_netdev_t type,
3546 const char *name,
3547 unsigned char name_assign_type,
3548 void (*setup)(struct net_device *))
3549{
Niranjana Vishwanathapura8e959602017-06-30 13:14:46 -07003550 struct net_device *netdev;
3551 struct rdma_netdev *rn;
3552
Erez Shitrit693dfd52017-04-27 17:01:34 +03003553 if (type != RDMA_NETDEV_IPOIB)
3554 return ERR_PTR(-EOPNOTSUPP);
3555
Niranjana Vishwanathapura8e959602017-06-30 13:14:46 -07003556 netdev = mlx5_rdma_netdev_alloc(to_mdev(hca)->mdev, hca,
3557 name, setup);
3558 if (likely(!IS_ERR_OR_NULL(netdev))) {
3559 rn = netdev_priv(netdev);
3560 rn->free_rdma_netdev = mlx5_ib_free_rdma_netdev;
3561 }
3562 return netdev;
Erez Shitrit693dfd52017-04-27 17:01:34 +03003563}
3564
Jack Morgenstein9603b612014-07-28 23:30:22 +03003565static void *mlx5_ib_add(struct mlx5_core_dev *mdev)
Eli Cohene126ba92013-07-07 17:25:49 +03003566{
Eli Cohene126ba92013-07-07 17:25:49 +03003567 struct mlx5_ib_dev *dev;
Achiad Shochatebd61f62015-12-23 18:47:16 +02003568 enum rdma_link_layer ll;
3569 int port_type_cap;
Aviv Heller4babcf92016-09-18 20:48:03 +03003570 const char *name;
Eli Cohene126ba92013-07-07 17:25:49 +03003571 int err;
3572 int i;
3573
Achiad Shochatebd61f62015-12-23 18:47:16 +02003574 port_type_cap = MLX5_CAP_GEN(mdev, port_type);
3575 ll = mlx5_port_type_cap_to_rdma_ll(port_type_cap);
3576
Eli Cohene126ba92013-07-07 17:25:49 +03003577 printk_once(KERN_INFO "%s", mlx5_version);
3578
3579 dev = (struct mlx5_ib_dev *)ib_alloc_device(sizeof(*dev));
3580 if (!dev)
Jack Morgenstein9603b612014-07-28 23:30:22 +03003581 return NULL;
Eli Cohene126ba92013-07-07 17:25:49 +03003582
Jack Morgenstein9603b612014-07-28 23:30:22 +03003583 dev->mdev = mdev;
Eli Cohene126ba92013-07-07 17:25:49 +03003584
Mark Bloch0837e862016-06-17 15:10:55 +03003585 dev->port = kcalloc(MLX5_CAP_GEN(mdev, num_ports), sizeof(*dev->port),
3586 GFP_KERNEL);
3587 if (!dev->port)
3588 goto err_dealloc;
3589
Achiad Shochatfc24fc52015-12-23 18:47:17 +02003590 rwlock_init(&dev->roce.netdev_lock);
Eli Cohene126ba92013-07-07 17:25:49 +03003591 err = get_port_caps(dev);
3592 if (err)
Mark Bloch0837e862016-06-17 15:10:55 +03003593 goto err_free_port;
Eli Cohene126ba92013-07-07 17:25:49 +03003594
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03003595 if (mlx5_use_mad_ifc(dev))
3596 get_ext_port_caps(dev);
Eli Cohene126ba92013-07-07 17:25:49 +03003597
Aviv Heller4babcf92016-09-18 20:48:03 +03003598 if (!mlx5_lag_is_active(mdev))
3599 name = "mlx5_%d";
3600 else
3601 name = "mlx5_bond_%d";
3602
3603 strlcpy(dev->ib_dev.name, name, IB_DEVICE_NAME_MAX);
Eli Cohene126ba92013-07-07 17:25:49 +03003604 dev->ib_dev.owner = THIS_MODULE;
3605 dev->ib_dev.node_type = RDMA_NODE_IB_CA;
Sagi Grimbergc6790aa2015-09-24 10:34:23 +03003606 dev->ib_dev.local_dma_lkey = 0 /* not supported for now */;
Saeed Mahameed938fe832015-05-28 22:28:41 +03003607 dev->num_ports = MLX5_CAP_GEN(mdev, num_ports);
Eli Cohene126ba92013-07-07 17:25:49 +03003608 dev->ib_dev.phys_port_cnt = dev->num_ports;
Saeed Mahameed233d05d2015-04-02 17:07:32 +03003609 dev->ib_dev.num_comp_vectors =
3610 dev->mdev->priv.eq_table.num_comp_vectors;
Bart Van Assche9b0c2892017-01-20 13:04:21 -08003611 dev->ib_dev.dev.parent = &mdev->pdev->dev;
Eli Cohene126ba92013-07-07 17:25:49 +03003612
3613 dev->ib_dev.uverbs_abi_ver = MLX5_IB_UVERBS_ABI_VERSION;
3614 dev->ib_dev.uverbs_cmd_mask =
3615 (1ull << IB_USER_VERBS_CMD_GET_CONTEXT) |
3616 (1ull << IB_USER_VERBS_CMD_QUERY_DEVICE) |
3617 (1ull << IB_USER_VERBS_CMD_QUERY_PORT) |
3618 (1ull << IB_USER_VERBS_CMD_ALLOC_PD) |
3619 (1ull << IB_USER_VERBS_CMD_DEALLOC_PD) |
Moni Shoua41c450f2016-11-23 08:23:26 +02003620 (1ull << IB_USER_VERBS_CMD_CREATE_AH) |
3621 (1ull << IB_USER_VERBS_CMD_DESTROY_AH) |
Eli Cohene126ba92013-07-07 17:25:49 +03003622 (1ull << IB_USER_VERBS_CMD_REG_MR) |
Noa Osherovich56e11d62016-02-29 16:46:51 +02003623 (1ull << IB_USER_VERBS_CMD_REREG_MR) |
Eli Cohene126ba92013-07-07 17:25:49 +03003624 (1ull << IB_USER_VERBS_CMD_DEREG_MR) |
3625 (1ull << IB_USER_VERBS_CMD_CREATE_COMP_CHANNEL) |
3626 (1ull << IB_USER_VERBS_CMD_CREATE_CQ) |
3627 (1ull << IB_USER_VERBS_CMD_RESIZE_CQ) |
3628 (1ull << IB_USER_VERBS_CMD_DESTROY_CQ) |
3629 (1ull << IB_USER_VERBS_CMD_CREATE_QP) |
3630 (1ull << IB_USER_VERBS_CMD_MODIFY_QP) |
3631 (1ull << IB_USER_VERBS_CMD_QUERY_QP) |
3632 (1ull << IB_USER_VERBS_CMD_DESTROY_QP) |
3633 (1ull << IB_USER_VERBS_CMD_ATTACH_MCAST) |
3634 (1ull << IB_USER_VERBS_CMD_DETACH_MCAST) |
3635 (1ull << IB_USER_VERBS_CMD_CREATE_SRQ) |
3636 (1ull << IB_USER_VERBS_CMD_MODIFY_SRQ) |
3637 (1ull << IB_USER_VERBS_CMD_QUERY_SRQ) |
3638 (1ull << IB_USER_VERBS_CMD_DESTROY_SRQ) |
3639 (1ull << IB_USER_VERBS_CMD_CREATE_XSRQ) |
3640 (1ull << IB_USER_VERBS_CMD_OPEN_QP);
Haggai Eran1707cb42015-02-08 13:28:52 +02003641 dev->ib_dev.uverbs_ex_cmd_mask =
Matan Barakd4584dd2016-01-28 17:51:46 +02003642 (1ull << IB_USER_VERBS_EX_CMD_QUERY_DEVICE) |
3643 (1ull << IB_USER_VERBS_EX_CMD_CREATE_CQ) |
Bodong Wang7d29f342016-12-01 13:43:16 +02003644 (1ull << IB_USER_VERBS_EX_CMD_CREATE_QP) |
3645 (1ull << IB_USER_VERBS_EX_CMD_MODIFY_QP);
Eli Cohene126ba92013-07-07 17:25:49 +03003646
3647 dev->ib_dev.query_device = mlx5_ib_query_device;
3648 dev->ib_dev.query_port = mlx5_ib_query_port;
Achiad Shochatebd61f62015-12-23 18:47:16 +02003649 dev->ib_dev.get_link_layer = mlx5_ib_port_link_layer;
Achiad Shochatfc24fc52015-12-23 18:47:17 +02003650 if (ll == IB_LINK_LAYER_ETHERNET)
3651 dev->ib_dev.get_netdev = mlx5_ib_get_netdev;
Eli Cohene126ba92013-07-07 17:25:49 +03003652 dev->ib_dev.query_gid = mlx5_ib_query_gid;
Achiad Shochat3cca2602015-12-23 18:47:23 +02003653 dev->ib_dev.add_gid = mlx5_ib_add_gid;
3654 dev->ib_dev.del_gid = mlx5_ib_del_gid;
Eli Cohene126ba92013-07-07 17:25:49 +03003655 dev->ib_dev.query_pkey = mlx5_ib_query_pkey;
3656 dev->ib_dev.modify_device = mlx5_ib_modify_device;
3657 dev->ib_dev.modify_port = mlx5_ib_modify_port;
3658 dev->ib_dev.alloc_ucontext = mlx5_ib_alloc_ucontext;
3659 dev->ib_dev.dealloc_ucontext = mlx5_ib_dealloc_ucontext;
3660 dev->ib_dev.mmap = mlx5_ib_mmap;
3661 dev->ib_dev.alloc_pd = mlx5_ib_alloc_pd;
3662 dev->ib_dev.dealloc_pd = mlx5_ib_dealloc_pd;
3663 dev->ib_dev.create_ah = mlx5_ib_create_ah;
3664 dev->ib_dev.query_ah = mlx5_ib_query_ah;
3665 dev->ib_dev.destroy_ah = mlx5_ib_destroy_ah;
3666 dev->ib_dev.create_srq = mlx5_ib_create_srq;
3667 dev->ib_dev.modify_srq = mlx5_ib_modify_srq;
3668 dev->ib_dev.query_srq = mlx5_ib_query_srq;
3669 dev->ib_dev.destroy_srq = mlx5_ib_destroy_srq;
3670 dev->ib_dev.post_srq_recv = mlx5_ib_post_srq_recv;
3671 dev->ib_dev.create_qp = mlx5_ib_create_qp;
3672 dev->ib_dev.modify_qp = mlx5_ib_modify_qp;
3673 dev->ib_dev.query_qp = mlx5_ib_query_qp;
3674 dev->ib_dev.destroy_qp = mlx5_ib_destroy_qp;
3675 dev->ib_dev.post_send = mlx5_ib_post_send;
3676 dev->ib_dev.post_recv = mlx5_ib_post_recv;
3677 dev->ib_dev.create_cq = mlx5_ib_create_cq;
3678 dev->ib_dev.modify_cq = mlx5_ib_modify_cq;
3679 dev->ib_dev.resize_cq = mlx5_ib_resize_cq;
3680 dev->ib_dev.destroy_cq = mlx5_ib_destroy_cq;
3681 dev->ib_dev.poll_cq = mlx5_ib_poll_cq;
3682 dev->ib_dev.req_notify_cq = mlx5_ib_arm_cq;
3683 dev->ib_dev.get_dma_mr = mlx5_ib_get_dma_mr;
3684 dev->ib_dev.reg_user_mr = mlx5_ib_reg_user_mr;
Noa Osherovich56e11d62016-02-29 16:46:51 +02003685 dev->ib_dev.rereg_user_mr = mlx5_ib_rereg_user_mr;
Eli Cohene126ba92013-07-07 17:25:49 +03003686 dev->ib_dev.dereg_mr = mlx5_ib_dereg_mr;
3687 dev->ib_dev.attach_mcast = mlx5_ib_mcg_attach;
3688 dev->ib_dev.detach_mcast = mlx5_ib_mcg_detach;
3689 dev->ib_dev.process_mad = mlx5_ib_process_mad;
Sagi Grimberg9bee1782015-07-30 10:32:35 +03003690 dev->ib_dev.alloc_mr = mlx5_ib_alloc_mr;
Sagi Grimberg8a187ee2015-10-13 19:11:26 +03003691 dev->ib_dev.map_mr_sg = mlx5_ib_map_mr_sg;
Sagi Grimbergd5436ba2014-02-23 14:19:12 +02003692 dev->ib_dev.check_mr_status = mlx5_ib_check_mr_status;
Ira Weiny77386132015-05-13 20:02:58 -04003693 dev->ib_dev.get_port_immutable = mlx5_port_immutable;
Ira Weinyc7342822016-06-15 02:22:01 -04003694 dev->ib_dev.get_dev_fw_str = get_dev_fw_str;
Niranjana Vishwanathapura8e959602017-06-30 13:14:46 -07003695 if (MLX5_CAP_GEN(mdev, ipoib_enhanced_offloads))
Alex Vesker022d0382017-06-14 09:59:06 +03003696 dev->ib_dev.alloc_rdma_netdev = mlx5_ib_alloc_rdma_netdev;
Niranjana Vishwanathapura8e959602017-06-30 13:14:46 -07003697
Eli Coheneff901d2016-03-11 22:58:42 +02003698 if (mlx5_core_is_pf(mdev)) {
3699 dev->ib_dev.get_vf_config = mlx5_ib_get_vf_config;
3700 dev->ib_dev.set_vf_link_state = mlx5_ib_set_vf_link_state;
3701 dev->ib_dev.get_vf_stats = mlx5_ib_get_vf_stats;
3702 dev->ib_dev.set_vf_guid = mlx5_ib_set_vf_guid;
3703 }
Eli Cohene126ba92013-07-07 17:25:49 +03003704
Maor Gottlieb7c2344c2016-06-17 14:56:44 +03003705 dev->ib_dev.disassociate_ucontext = mlx5_ib_disassociate_ucontext;
3706
Saeed Mahameed938fe832015-05-28 22:28:41 +03003707 mlx5_ib_internal_fill_odp_caps(dev);
Haggai Eran8cdd3122014-12-11 17:04:20 +02003708
Max Gurtovoy6e8484c2017-05-28 10:53:11 +03003709 dev->umr_fence = mlx5_get_umr_fence(MLX5_CAP_GEN(mdev, umr_fence));
3710
Matan Barakd2370e02016-02-29 18:05:30 +02003711 if (MLX5_CAP_GEN(mdev, imaicl)) {
3712 dev->ib_dev.alloc_mw = mlx5_ib_alloc_mw;
3713 dev->ib_dev.dealloc_mw = mlx5_ib_dealloc_mw;
3714 dev->ib_dev.uverbs_cmd_mask |=
3715 (1ull << IB_USER_VERBS_CMD_ALLOC_MW) |
3716 (1ull << IB_USER_VERBS_CMD_DEALLOC_MW);
3717 }
3718
Kamal Heib7c16f472017-01-18 15:25:09 +02003719 if (MLX5_CAP_GEN(dev->mdev, max_qp_cnt)) {
Mark Bloch0ad17a82016-06-17 15:10:56 +03003720 dev->ib_dev.get_hw_stats = mlx5_ib_get_hw_stats;
3721 dev->ib_dev.alloc_hw_stats = mlx5_ib_alloc_hw_stats;
3722 }
3723
Saeed Mahameed938fe832015-05-28 22:28:41 +03003724 if (MLX5_CAP_GEN(mdev, xrc)) {
Eli Cohene126ba92013-07-07 17:25:49 +03003725 dev->ib_dev.alloc_xrcd = mlx5_ib_alloc_xrcd;
3726 dev->ib_dev.dealloc_xrcd = mlx5_ib_dealloc_xrcd;
3727 dev->ib_dev.uverbs_cmd_mask |=
3728 (1ull << IB_USER_VERBS_CMD_OPEN_XRCD) |
3729 (1ull << IB_USER_VERBS_CMD_CLOSE_XRCD);
3730 }
3731
Linus Torvalds048ccca2016-01-23 18:45:06 -08003732 if (mlx5_ib_port_link_layer(&dev->ib_dev, 1) ==
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003733 IB_LINK_LAYER_ETHERNET) {
3734 dev->ib_dev.create_flow = mlx5_ib_create_flow;
3735 dev->ib_dev.destroy_flow = mlx5_ib_destroy_flow;
Yishai Hadas79b20a62016-05-23 15:20:50 +03003736 dev->ib_dev.create_wq = mlx5_ib_create_wq;
3737 dev->ib_dev.modify_wq = mlx5_ib_modify_wq;
3738 dev->ib_dev.destroy_wq = mlx5_ib_destroy_wq;
Yishai Hadasc5f90922016-05-23 15:20:53 +03003739 dev->ib_dev.create_rwq_ind_table = mlx5_ib_create_rwq_ind_table;
3740 dev->ib_dev.destroy_rwq_ind_table = mlx5_ib_destroy_rwq_ind_table;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003741 dev->ib_dev.uverbs_ex_cmd_mask |=
3742 (1ull << IB_USER_VERBS_EX_CMD_CREATE_FLOW) |
Yishai Hadas79b20a62016-05-23 15:20:50 +03003743 (1ull << IB_USER_VERBS_EX_CMD_DESTROY_FLOW) |
3744 (1ull << IB_USER_VERBS_EX_CMD_CREATE_WQ) |
3745 (1ull << IB_USER_VERBS_EX_CMD_MODIFY_WQ) |
Yishai Hadasc5f90922016-05-23 15:20:53 +03003746 (1ull << IB_USER_VERBS_EX_CMD_DESTROY_WQ) |
3747 (1ull << IB_USER_VERBS_EX_CMD_CREATE_RWQ_IND_TBL) |
3748 (1ull << IB_USER_VERBS_EX_CMD_DESTROY_RWQ_IND_TBL);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003749 }
Eli Cohene126ba92013-07-07 17:25:49 +03003750 err = init_node_data(dev);
3751 if (err)
Majd Dibbiny90be7c82016-10-27 16:36:39 +03003752 goto err_free_port;
Eli Cohene126ba92013-07-07 17:25:49 +03003753
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003754 mutex_init(&dev->flow_db.lock);
Eli Cohene126ba92013-07-07 17:25:49 +03003755 mutex_init(&dev->cap_mask_mutex);
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03003756 INIT_LIST_HEAD(&dev->qp_list);
3757 spin_lock_init(&dev->reset_flow_resource_lock);
Eli Cohene126ba92013-07-07 17:25:49 +03003758
Achiad Shochatfc24fc52015-12-23 18:47:17 +02003759 if (ll == IB_LINK_LAYER_ETHERNET) {
Or Gerlitz45f95ac2016-11-27 16:51:35 +02003760 err = mlx5_enable_eth(dev);
Achiad Shochatfc24fc52015-12-23 18:47:17 +02003761 if (err)
Majd Dibbiny90be7c82016-10-27 16:36:39 +03003762 goto err_free_port;
Achiad Shochatfc24fc52015-12-23 18:47:17 +02003763 }
3764
Eli Cohene126ba92013-07-07 17:25:49 +03003765 err = create_dev_resources(&dev->devr);
3766 if (err)
Or Gerlitz45f95ac2016-11-27 16:51:35 +02003767 goto err_disable_eth;
Eli Cohene126ba92013-07-07 17:25:49 +03003768
Haggai Eran6aec21f2014-12-11 17:04:23 +02003769 err = mlx5_ib_odp_init_one(dev);
Wei Yongjun281d1a92013-07-30 07:54:26 +08003770 if (err)
Eli Cohene126ba92013-07-07 17:25:49 +03003771 goto err_rsrc;
3772
Kamal Heib45bded22017-01-18 14:10:32 +02003773 if (MLX5_CAP_GEN(dev->mdev, max_qp_cnt)) {
Parav Pandite1f24a72017-04-16 07:29:29 +03003774 err = mlx5_ib_alloc_counters(dev);
Kamal Heib45bded22017-01-18 14:10:32 +02003775 if (err)
3776 goto err_odp;
3777 }
Haggai Eran6aec21f2014-12-11 17:04:23 +02003778
Eli Cohen5fe9dec2017-01-03 23:55:25 +02003779 dev->mdev->priv.uar = mlx5_get_uars_page(dev->mdev);
3780 if (!dev->mdev->priv.uar)
Parav Pandite1f24a72017-04-16 07:29:29 +03003781 goto err_cnt;
Eli Cohen5fe9dec2017-01-03 23:55:25 +02003782
3783 err = mlx5_alloc_bfreg(dev->mdev, &dev->bfreg, false, false);
3784 if (err)
3785 goto err_uar_page;
3786
3787 err = mlx5_alloc_bfreg(dev->mdev, &dev->fp_bfreg, false, true);
3788 if (err)
3789 goto err_bfreg;
3790
Mark Bloch0837e862016-06-17 15:10:55 +03003791 err = ib_register_device(&dev->ib_dev, NULL);
3792 if (err)
Eli Cohen5fe9dec2017-01-03 23:55:25 +02003793 goto err_fp_bfreg;
Mark Bloch0837e862016-06-17 15:10:55 +03003794
Eli Cohene126ba92013-07-07 17:25:49 +03003795 err = create_umr_res(dev);
3796 if (err)
3797 goto err_dev;
3798
3799 for (i = 0; i < ARRAY_SIZE(mlx5_class_attributes); i++) {
Wei Yongjun281d1a92013-07-30 07:54:26 +08003800 err = device_create_file(&dev->ib_dev.dev,
3801 mlx5_class_attributes[i]);
3802 if (err)
Eli Cohene126ba92013-07-07 17:25:49 +03003803 goto err_umrc;
3804 }
3805
3806 dev->ib_active = true;
3807
Jack Morgenstein9603b612014-07-28 23:30:22 +03003808 return dev;
Eli Cohene126ba92013-07-07 17:25:49 +03003809
3810err_umrc:
3811 destroy_umrc_res(dev);
3812
3813err_dev:
3814 ib_unregister_device(&dev->ib_dev);
3815
Eli Cohen5fe9dec2017-01-03 23:55:25 +02003816err_fp_bfreg:
3817 mlx5_free_bfreg(dev->mdev, &dev->fp_bfreg);
3818
3819err_bfreg:
3820 mlx5_free_bfreg(dev->mdev, &dev->bfreg);
3821
3822err_uar_page:
3823 mlx5_put_uars_page(dev->mdev, dev->mdev->priv.uar);
3824
Parav Pandite1f24a72017-04-16 07:29:29 +03003825err_cnt:
Kamal Heib45bded22017-01-18 14:10:32 +02003826 if (MLX5_CAP_GEN(dev->mdev, max_qp_cnt))
Parav Pandite1f24a72017-04-16 07:29:29 +03003827 mlx5_ib_dealloc_counters(dev);
Mark Bloch0837e862016-06-17 15:10:55 +03003828
Haggai Eran6aec21f2014-12-11 17:04:23 +02003829err_odp:
3830 mlx5_ib_odp_remove_one(dev);
3831
Eli Cohene126ba92013-07-07 17:25:49 +03003832err_rsrc:
3833 destroy_dev_resources(&dev->devr);
3834
Or Gerlitz45f95ac2016-11-27 16:51:35 +02003835err_disable_eth:
Aviv Heller5ec8c832016-09-18 20:48:00 +03003836 if (ll == IB_LINK_LAYER_ETHERNET) {
Or Gerlitz45f95ac2016-11-27 16:51:35 +02003837 mlx5_disable_eth(dev);
Or Gerlitzd012f5d2016-11-27 16:51:34 +02003838 mlx5_remove_netdev_notifier(dev);
Aviv Heller5ec8c832016-09-18 20:48:00 +03003839 }
Achiad Shochatfc24fc52015-12-23 18:47:17 +02003840
Mark Bloch0837e862016-06-17 15:10:55 +03003841err_free_port:
3842 kfree(dev->port);
3843
Jack Morgenstein9603b612014-07-28 23:30:22 +03003844err_dealloc:
Eli Cohene126ba92013-07-07 17:25:49 +03003845 ib_dealloc_device((struct ib_device *)dev);
3846
Jack Morgenstein9603b612014-07-28 23:30:22 +03003847 return NULL;
Eli Cohene126ba92013-07-07 17:25:49 +03003848}
3849
Jack Morgenstein9603b612014-07-28 23:30:22 +03003850static void mlx5_ib_remove(struct mlx5_core_dev *mdev, void *context)
Eli Cohene126ba92013-07-07 17:25:49 +03003851{
Jack Morgenstein9603b612014-07-28 23:30:22 +03003852 struct mlx5_ib_dev *dev = context;
Achiad Shochatfc24fc52015-12-23 18:47:17 +02003853 enum rdma_link_layer ll = mlx5_ib_port_link_layer(&dev->ib_dev, 1);
Haggai Eran6aec21f2014-12-11 17:04:23 +02003854
Or Gerlitzd012f5d2016-11-27 16:51:34 +02003855 mlx5_remove_netdev_notifier(dev);
Eli Cohene126ba92013-07-07 17:25:49 +03003856 ib_unregister_device(&dev->ib_dev);
Eli Cohen5fe9dec2017-01-03 23:55:25 +02003857 mlx5_free_bfreg(dev->mdev, &dev->fp_bfreg);
3858 mlx5_free_bfreg(dev->mdev, &dev->bfreg);
3859 mlx5_put_uars_page(dev->mdev, mdev->priv.uar);
Kamal Heib45bded22017-01-18 14:10:32 +02003860 if (MLX5_CAP_GEN(dev->mdev, max_qp_cnt))
Parav Pandite1f24a72017-04-16 07:29:29 +03003861 mlx5_ib_dealloc_counters(dev);
Eli Coheneefd56e2014-09-14 16:47:50 +03003862 destroy_umrc_res(dev);
Haggai Eran6aec21f2014-12-11 17:04:23 +02003863 mlx5_ib_odp_remove_one(dev);
Eli Cohene126ba92013-07-07 17:25:49 +03003864 destroy_dev_resources(&dev->devr);
Achiad Shochatfc24fc52015-12-23 18:47:17 +02003865 if (ll == IB_LINK_LAYER_ETHERNET)
Or Gerlitz45f95ac2016-11-27 16:51:35 +02003866 mlx5_disable_eth(dev);
Mark Bloch0837e862016-06-17 15:10:55 +03003867 kfree(dev->port);
Eli Cohene126ba92013-07-07 17:25:49 +03003868 ib_dealloc_device(&dev->ib_dev);
3869}
3870
Jack Morgenstein9603b612014-07-28 23:30:22 +03003871static struct mlx5_interface mlx5_ib_interface = {
3872 .add = mlx5_ib_add,
3873 .remove = mlx5_ib_remove,
3874 .event = mlx5_ib_event,
Artemy Kovalyovd9aaed82017-01-02 11:37:46 +02003875#ifdef CONFIG_INFINIBAND_ON_DEMAND_PAGING
3876 .pfault = mlx5_ib_pfault,
3877#endif
Saeed Mahameed64613d942015-04-02 17:07:34 +03003878 .protocol = MLX5_INTERFACE_PROTOCOL_IB,
Eli Cohene126ba92013-07-07 17:25:49 +03003879};
3880
3881static int __init mlx5_ib_init(void)
3882{
Haggai Eran6aec21f2014-12-11 17:04:23 +02003883 int err;
3884
Artemy Kovalyov81713d32017-01-18 16:58:11 +02003885 mlx5_ib_odp_init();
Jack Morgenstein9603b612014-07-28 23:30:22 +03003886
Haggai Eran6aec21f2014-12-11 17:04:23 +02003887 err = mlx5_register_interface(&mlx5_ib_interface);
Haggai Eran6aec21f2014-12-11 17:04:23 +02003888
3889 return err;
Eli Cohene126ba92013-07-07 17:25:49 +03003890}
3891
3892static void __exit mlx5_ib_cleanup(void)
3893{
Jack Morgenstein9603b612014-07-28 23:30:22 +03003894 mlx5_unregister_interface(&mlx5_ib_interface);
Eli Cohene126ba92013-07-07 17:25:49 +03003895}
3896
3897module_init(mlx5_ib_init);
3898module_exit(mlx5_ib_cleanup);