blob: b894bc5be384a6777eda8207cd6fa2640aa6e66c [file] [log] [blame]
Eli Cohene126ba92013-07-07 17:25:49 +03001/*
Saeed Mahameed6cf0a152015-04-02 17:07:30 +03002 * Copyright (c) 2013-2015, Mellanox Technologies. All rights reserved.
Eli Cohene126ba92013-07-07 17:25:49 +03003 *
4 * This software is available to you under a choice of one of two
5 * licenses. You may choose to be licensed under the terms of the GNU
6 * General Public License (GPL) Version 2, available from the file
7 * COPYING in the main directory of this source tree, or the
8 * OpenIB.org BSD license below:
9 *
10 * Redistribution and use in source and binary forms, with or
11 * without modification, are permitted provided that the following
12 * conditions are met:
13 *
14 * - Redistributions of source code must retain the above
15 * copyright notice, this list of conditions and the following
16 * disclaimer.
17 *
18 * - Redistributions in binary form must reproduce the above
19 * copyright notice, this list of conditions and the following
20 * disclaimer in the documentation and/or other materials
21 * provided with the distribution.
22 *
23 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
27 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
28 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
29 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
30 * SOFTWARE.
31 */
32
Maor Gottliebfe248c32017-05-30 10:29:14 +030033#include <linux/debugfs.h>
Christoph Hellwigadec6402015-08-28 09:27:19 +020034#include <linux/highmem.h>
Eli Cohene126ba92013-07-07 17:25:49 +030035#include <linux/module.h>
36#include <linux/init.h>
37#include <linux/errno.h>
38#include <linux/pci.h>
39#include <linux/dma-mapping.h>
40#include <linux/slab.h>
Guy Levi37aa5c32016-04-27 16:49:50 +030041#if defined(CONFIG_X86)
42#include <asm/pat.h>
43#endif
Eli Cohene126ba92013-07-07 17:25:49 +030044#include <linux/sched.h>
Ingo Molnar6e84f312017-02-08 18:51:29 +010045#include <linux/sched/mm.h>
Ingo Molnar0881e7b2017-02-05 15:30:50 +010046#include <linux/sched/task.h>
Maor Gottlieb7c2344c2016-06-17 14:56:44 +030047#include <linux/delay.h>
Eli Cohene126ba92013-07-07 17:25:49 +030048#include <rdma/ib_user_verbs.h>
Achiad Shochat3f89a642015-12-23 18:47:21 +020049#include <rdma/ib_addr.h>
Achiad Shochat2811ba52015-12-23 18:47:24 +020050#include <rdma/ib_cache.h>
Achiad Shochatada68c32016-02-22 18:17:23 +020051#include <linux/mlx5/port.h>
Majd Dibbiny1b5daf12015-06-04 19:30:46 +030052#include <linux/mlx5/vport.h>
Pravin Shedge72c7fe92017-12-06 22:19:39 +053053#include <linux/mlx5/fs.h>
Maor Gottlieb7c2344c2016-06-17 14:56:44 +030054#include <linux/list.h>
Eli Cohene126ba92013-07-07 17:25:49 +030055#include <rdma/ib_smi.h>
56#include <rdma/ib_umem.h>
Maor Gottlieb038d2ef2016-01-11 10:26:07 +020057#include <linux/in.h>
58#include <linux/etherdevice.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");
Eli Cohene126ba92013-07-07 17:25:49 +030068
Eli Cohene126ba92013-07-07 17:25:49 +030069static char mlx5_version[] =
70 DRIVER_NAME ": Mellanox Connect-IB Infiniband driver v"
Tariq Toukanb3599112017-02-22 17:45:46 +020071 DRIVER_VERSION "\n";
Eli Cohene126ba92013-07-07 17:25:49 +030072
Eran Ben Elishada7525d2015-12-14 16:34:10 +020073enum {
74 MLX5_ATOMIC_SIZE_QP_8BYTES = 1 << 3,
75};
Majd Dibbiny1b5daf12015-06-04 19:30:46 +030076
Majd Dibbiny1b5daf12015-06-04 19:30:46 +030077static enum rdma_link_layer
Achiad Shochatebd61f62015-12-23 18:47:16 +020078mlx5_port_type_cap_to_rdma_ll(int port_type_cap)
Majd Dibbiny1b5daf12015-06-04 19:30:46 +030079{
Achiad Shochatebd61f62015-12-23 18:47:16 +020080 switch (port_type_cap) {
Majd Dibbiny1b5daf12015-06-04 19:30:46 +030081 case MLX5_CAP_PORT_TYPE_IB:
82 return IB_LINK_LAYER_INFINIBAND;
83 case MLX5_CAP_PORT_TYPE_ETH:
84 return IB_LINK_LAYER_ETHERNET;
85 default:
86 return IB_LINK_LAYER_UNSPECIFIED;
87 }
88}
89
Achiad Shochatebd61f62015-12-23 18:47:16 +020090static enum rdma_link_layer
91mlx5_ib_port_link_layer(struct ib_device *device, u8 port_num)
92{
93 struct mlx5_ib_dev *dev = to_mdev(device);
94 int port_type_cap = MLX5_CAP_GEN(dev->mdev, port_type);
95
96 return mlx5_port_type_cap_to_rdma_ll(port_type_cap);
97}
98
Moni Shouafd65f1b2017-05-30 09:56:05 +030099static int get_port_state(struct ib_device *ibdev,
100 u8 port_num,
101 enum ib_port_state *state)
102{
103 struct ib_port_attr attr;
104 int ret;
105
106 memset(&attr, 0, sizeof(attr));
107 ret = mlx5_ib_query_port(ibdev, port_num, &attr);
108 if (!ret)
109 *state = attr.state;
110 return ret;
111}
112
Achiad Shochatfc24fc52015-12-23 18:47:17 +0200113static int mlx5_netdev_event(struct notifier_block *this,
114 unsigned long event, void *ptr)
115{
116 struct net_device *ndev = netdev_notifier_info_to_dev(ptr);
117 struct mlx5_ib_dev *ibdev = container_of(this, struct mlx5_ib_dev,
118 roce.nb);
119
Aviv Heller5ec8c832016-09-18 20:48:00 +0300120 switch (event) {
121 case NETDEV_REGISTER:
122 case NETDEV_UNREGISTER:
123 write_lock(&ibdev->roce.netdev_lock);
124 if (ndev->dev.parent == &ibdev->mdev->pdev->dev)
125 ibdev->roce.netdev = (event == NETDEV_UNREGISTER) ?
126 NULL : ndev;
127 write_unlock(&ibdev->roce.netdev_lock);
128 break;
Achiad Shochatfc24fc52015-12-23 18:47:17 +0200129
Moni Shouafd65f1b2017-05-30 09:56:05 +0300130 case NETDEV_CHANGE:
Aviv Heller5ec8c832016-09-18 20:48:00 +0300131 case NETDEV_UP:
Aviv Heller88621df2016-09-18 20:48:02 +0300132 case NETDEV_DOWN: {
133 struct net_device *lag_ndev = mlx5_lag_get_roce_netdev(ibdev->mdev);
134 struct net_device *upper = NULL;
135
136 if (lag_ndev) {
137 upper = netdev_master_upper_dev_get(lag_ndev);
138 dev_put(lag_ndev);
139 }
140
141 if ((upper == ndev || (!upper && ndev == ibdev->roce.netdev))
142 && ibdev->ib_active) {
Bart Van Assche626bc022016-12-05 17:18:08 -0800143 struct ib_event ibev = { };
Moni Shouafd65f1b2017-05-30 09:56:05 +0300144 enum ib_port_state port_state;
Aviv Heller5ec8c832016-09-18 20:48:00 +0300145
Moni Shouafd65f1b2017-05-30 09:56:05 +0300146 if (get_port_state(&ibdev->ib_dev, 1, &port_state))
147 return NOTIFY_DONE;
148
149 if (ibdev->roce.last_port_state == port_state)
150 return NOTIFY_DONE;
151
152 ibdev->roce.last_port_state = port_state;
Aviv Heller5ec8c832016-09-18 20:48:00 +0300153 ibev.device = &ibdev->ib_dev;
Moni Shouafd65f1b2017-05-30 09:56:05 +0300154 if (port_state == IB_PORT_DOWN)
155 ibev.event = IB_EVENT_PORT_ERR;
156 else if (port_state == IB_PORT_ACTIVE)
157 ibev.event = IB_EVENT_PORT_ACTIVE;
158 else
159 return NOTIFY_DONE;
160
Aviv Heller5ec8c832016-09-18 20:48:00 +0300161 ibev.element.port_num = 1;
162 ib_dispatch_event(&ibev);
163 }
164 break;
Aviv Heller88621df2016-09-18 20:48:02 +0300165 }
Aviv Heller5ec8c832016-09-18 20:48:00 +0300166
167 default:
168 break;
169 }
Achiad Shochatfc24fc52015-12-23 18:47:17 +0200170
171 return NOTIFY_DONE;
172}
173
174static struct net_device *mlx5_ib_get_netdev(struct ib_device *device,
175 u8 port_num)
176{
177 struct mlx5_ib_dev *ibdev = to_mdev(device);
178 struct net_device *ndev;
179
Aviv Heller88621df2016-09-18 20:48:02 +0300180 ndev = mlx5_lag_get_roce_netdev(ibdev->mdev);
181 if (ndev)
182 return ndev;
183
Achiad Shochatfc24fc52015-12-23 18:47:17 +0200184 /* Ensure ndev does not disappear before we invoke dev_hold()
185 */
186 read_lock(&ibdev->roce.netdev_lock);
187 ndev = ibdev->roce.netdev;
188 if (ndev)
189 dev_hold(ndev);
190 read_unlock(&ibdev->roce.netdev_lock);
191
192 return ndev;
193}
194
Noa Osherovichf1b65df2017-04-20 20:53:33 +0300195static int translate_eth_proto_oper(u32 eth_proto_oper, u8 *active_speed,
196 u8 *active_width)
197{
198 switch (eth_proto_oper) {
199 case MLX5E_PROT_MASK(MLX5E_1000BASE_CX_SGMII):
200 case MLX5E_PROT_MASK(MLX5E_1000BASE_KX):
201 case MLX5E_PROT_MASK(MLX5E_100BASE_TX):
202 case MLX5E_PROT_MASK(MLX5E_1000BASE_T):
203 *active_width = IB_WIDTH_1X;
204 *active_speed = IB_SPEED_SDR;
205 break;
206 case MLX5E_PROT_MASK(MLX5E_10GBASE_T):
207 case MLX5E_PROT_MASK(MLX5E_10GBASE_CX4):
208 case MLX5E_PROT_MASK(MLX5E_10GBASE_KX4):
209 case MLX5E_PROT_MASK(MLX5E_10GBASE_KR):
210 case MLX5E_PROT_MASK(MLX5E_10GBASE_CR):
211 case MLX5E_PROT_MASK(MLX5E_10GBASE_SR):
212 case MLX5E_PROT_MASK(MLX5E_10GBASE_ER):
213 *active_width = IB_WIDTH_1X;
214 *active_speed = IB_SPEED_QDR;
215 break;
216 case MLX5E_PROT_MASK(MLX5E_25GBASE_CR):
217 case MLX5E_PROT_MASK(MLX5E_25GBASE_KR):
218 case MLX5E_PROT_MASK(MLX5E_25GBASE_SR):
219 *active_width = IB_WIDTH_1X;
220 *active_speed = IB_SPEED_EDR;
221 break;
222 case MLX5E_PROT_MASK(MLX5E_40GBASE_CR4):
223 case MLX5E_PROT_MASK(MLX5E_40GBASE_KR4):
224 case MLX5E_PROT_MASK(MLX5E_40GBASE_SR4):
225 case MLX5E_PROT_MASK(MLX5E_40GBASE_LR4):
226 *active_width = IB_WIDTH_4X;
227 *active_speed = IB_SPEED_QDR;
228 break;
229 case MLX5E_PROT_MASK(MLX5E_50GBASE_CR2):
230 case MLX5E_PROT_MASK(MLX5E_50GBASE_KR2):
231 case MLX5E_PROT_MASK(MLX5E_50GBASE_SR2):
232 *active_width = IB_WIDTH_1X;
233 *active_speed = IB_SPEED_HDR;
234 break;
235 case MLX5E_PROT_MASK(MLX5E_56GBASE_R4):
236 *active_width = IB_WIDTH_4X;
237 *active_speed = IB_SPEED_FDR;
238 break;
239 case MLX5E_PROT_MASK(MLX5E_100GBASE_CR4):
240 case MLX5E_PROT_MASK(MLX5E_100GBASE_SR4):
241 case MLX5E_PROT_MASK(MLX5E_100GBASE_KR4):
242 case MLX5E_PROT_MASK(MLX5E_100GBASE_LR4):
243 *active_width = IB_WIDTH_4X;
244 *active_speed = IB_SPEED_EDR;
245 break;
246 default:
247 return -EINVAL;
248 }
249
250 return 0;
251}
252
Ilan Tayari095b0922017-05-14 16:04:30 +0300253static int mlx5_query_port_roce(struct ib_device *device, u8 port_num,
254 struct ib_port_attr *props)
Achiad Shochat3f89a642015-12-23 18:47:21 +0200255{
256 struct mlx5_ib_dev *dev = to_mdev(device);
Noa Osherovichf1b65df2017-04-20 20:53:33 +0300257 struct mlx5_core_dev *mdev = dev->mdev;
Aviv Heller88621df2016-09-18 20:48:02 +0300258 struct net_device *ndev, *upper;
Achiad Shochat3f89a642015-12-23 18:47:21 +0200259 enum ib_mtu ndev_ib_mtu;
Leon Romanovskyc876a1b2016-01-09 13:06:25 +0200260 u16 qkey_viol_cntr;
Noa Osherovichf1b65df2017-04-20 20:53:33 +0300261 u32 eth_prot_oper;
Ilan Tayari095b0922017-05-14 16:04:30 +0300262 int err;
Achiad Shochat3f89a642015-12-23 18:47:21 +0200263
Noa Osherovichf1b65df2017-04-20 20:53:33 +0300264 /* Possible bad flows are checked before filling out props so in case
265 * of an error it will still be zeroed out.
Noa Osherovich50f22fd2017-04-20 20:53:32 +0300266 */
Ilan Tayari095b0922017-05-14 16:04:30 +0300267 err = mlx5_query_port_eth_proto_oper(mdev, &eth_prot_oper, port_num);
268 if (err)
269 return err;
Noa Osherovichf1b65df2017-04-20 20:53:33 +0300270
271 translate_eth_proto_oper(eth_prot_oper, &props->active_speed,
272 &props->active_width);
Achiad Shochat3f89a642015-12-23 18:47:21 +0200273
274 props->port_cap_flags |= IB_PORT_CM_SUP;
275 props->port_cap_flags |= IB_PORT_IP_BASED_GIDS;
276
277 props->gid_tbl_len = MLX5_CAP_ROCE(dev->mdev,
278 roce_address_table_size);
279 props->max_mtu = IB_MTU_4096;
280 props->max_msg_sz = 1 << MLX5_CAP_GEN(dev->mdev, log_max_msg);
281 props->pkey_tbl_len = 1;
282 props->state = IB_PORT_DOWN;
283 props->phys_state = 3;
284
Leon Romanovskyc876a1b2016-01-09 13:06:25 +0200285 mlx5_query_nic_vport_qkey_viol_cntr(dev->mdev, &qkey_viol_cntr);
286 props->qkey_viol_cntr = qkey_viol_cntr;
Achiad Shochat3f89a642015-12-23 18:47:21 +0200287
288 ndev = mlx5_ib_get_netdev(device, port_num);
289 if (!ndev)
Ilan Tayari095b0922017-05-14 16:04:30 +0300290 return 0;
Achiad Shochat3f89a642015-12-23 18:47:21 +0200291
Aviv Heller88621df2016-09-18 20:48:02 +0300292 if (mlx5_lag_is_active(dev->mdev)) {
293 rcu_read_lock();
294 upper = netdev_master_upper_dev_get_rcu(ndev);
295 if (upper) {
296 dev_put(ndev);
297 ndev = upper;
298 dev_hold(ndev);
299 }
300 rcu_read_unlock();
301 }
302
Achiad Shochat3f89a642015-12-23 18:47:21 +0200303 if (netif_running(ndev) && netif_carrier_ok(ndev)) {
304 props->state = IB_PORT_ACTIVE;
305 props->phys_state = 5;
306 }
307
308 ndev_ib_mtu = iboe_get_mtu(ndev->mtu);
309
310 dev_put(ndev);
311
312 props->active_mtu = min(props->max_mtu, ndev_ib_mtu);
Ilan Tayari095b0922017-05-14 16:04:30 +0300313 return 0;
Achiad Shochat3f89a642015-12-23 18:47:21 +0200314}
315
Ilan Tayari095b0922017-05-14 16:04:30 +0300316static int set_roce_addr(struct mlx5_ib_dev *dev, u8 port_num,
317 unsigned int index, const union ib_gid *gid,
318 const struct ib_gid_attr *attr)
Achiad Shochat3cca2602015-12-23 18:47:23 +0200319{
Ilan Tayari095b0922017-05-14 16:04:30 +0300320 enum ib_gid_type gid_type = IB_GID_TYPE_IB;
321 u8 roce_version = 0;
322 u8 roce_l3_type = 0;
323 bool vlan = false;
324 u8 mac[ETH_ALEN];
325 u16 vlan_id = 0;
Achiad Shochat3cca2602015-12-23 18:47:23 +0200326
Ilan Tayari095b0922017-05-14 16:04:30 +0300327 if (gid) {
328 gid_type = attr->gid_type;
329 ether_addr_copy(mac, attr->ndev->dev_addr);
Achiad Shochat3cca2602015-12-23 18:47:23 +0200330
Ilan Tayari095b0922017-05-14 16:04:30 +0300331 if (is_vlan_dev(attr->ndev)) {
332 vlan = true;
333 vlan_id = vlan_dev_vlan_id(attr->ndev);
334 }
Achiad Shochat3cca2602015-12-23 18:47:23 +0200335 }
336
Ilan Tayari095b0922017-05-14 16:04:30 +0300337 switch (gid_type) {
Achiad Shochat3cca2602015-12-23 18:47:23 +0200338 case IB_GID_TYPE_IB:
Ilan Tayari095b0922017-05-14 16:04:30 +0300339 roce_version = MLX5_ROCE_VERSION_1;
Achiad Shochat3cca2602015-12-23 18:47:23 +0200340 break;
341 case IB_GID_TYPE_ROCE_UDP_ENCAP:
Ilan Tayari095b0922017-05-14 16:04:30 +0300342 roce_version = MLX5_ROCE_VERSION_2;
343 if (ipv6_addr_v4mapped((void *)gid))
344 roce_l3_type = MLX5_ROCE_L3_TYPE_IPV4;
345 else
346 roce_l3_type = MLX5_ROCE_L3_TYPE_IPV6;
Achiad Shochat3cca2602015-12-23 18:47:23 +0200347 break;
348
349 default:
Ilan Tayari095b0922017-05-14 16:04:30 +0300350 mlx5_ib_warn(dev, "Unexpected GID type %u\n", gid_type);
Achiad Shochat3cca2602015-12-23 18:47:23 +0200351 }
352
Ilan Tayari095b0922017-05-14 16:04:30 +0300353 return mlx5_core_roce_gid_set(dev->mdev, index, roce_version,
354 roce_l3_type, gid->raw, mac, vlan,
355 vlan_id);
Achiad Shochat3cca2602015-12-23 18:47:23 +0200356}
357
358static int mlx5_ib_add_gid(struct ib_device *device, u8 port_num,
359 unsigned int index, const union ib_gid *gid,
360 const struct ib_gid_attr *attr,
361 __always_unused void **context)
362{
Ilan Tayari095b0922017-05-14 16:04:30 +0300363 return set_roce_addr(to_mdev(device), port_num, index, gid, attr);
Achiad Shochat3cca2602015-12-23 18:47:23 +0200364}
365
366static int mlx5_ib_del_gid(struct ib_device *device, u8 port_num,
367 unsigned int index, __always_unused void **context)
368{
Ilan Tayari095b0922017-05-14 16:04:30 +0300369 return set_roce_addr(to_mdev(device), port_num, index, NULL, NULL);
Achiad Shochat3cca2602015-12-23 18:47:23 +0200370}
371
Achiad Shochat2811ba52015-12-23 18:47:24 +0200372__be16 mlx5_get_roce_udp_sport(struct mlx5_ib_dev *dev, u8 port_num,
373 int index)
374{
375 struct ib_gid_attr attr;
376 union ib_gid gid;
377
378 if (ib_get_cached_gid(&dev->ib_dev, port_num, index, &gid, &attr))
379 return 0;
380
381 if (!attr.ndev)
382 return 0;
383
384 dev_put(attr.ndev);
385
386 if (attr.gid_type != IB_GID_TYPE_ROCE_UDP_ENCAP)
387 return 0;
388
389 return cpu_to_be16(MLX5_CAP_ROCE(dev->mdev, r_roce_min_src_udp_port));
390}
391
Majd Dibbinyed884512017-01-18 14:10:35 +0200392int mlx5_get_roce_gid_type(struct mlx5_ib_dev *dev, u8 port_num,
393 int index, enum ib_gid_type *gid_type)
394{
395 struct ib_gid_attr attr;
396 union ib_gid gid;
397 int ret;
398
399 ret = ib_get_cached_gid(&dev->ib_dev, port_num, index, &gid, &attr);
400 if (ret)
401 return ret;
402
403 if (!attr.ndev)
404 return -ENODEV;
405
406 dev_put(attr.ndev);
407
408 *gid_type = attr.gid_type;
409
410 return 0;
411}
412
Majd Dibbiny1b5daf12015-06-04 19:30:46 +0300413static int mlx5_use_mad_ifc(struct mlx5_ib_dev *dev)
414{
Noa Osherovich7fae6652016-09-12 19:16:23 +0300415 if (MLX5_CAP_GEN(dev->mdev, port_type) == MLX5_CAP_PORT_TYPE_IB)
416 return !MLX5_CAP_GEN(dev->mdev, ib_virt);
417 return 0;
Majd Dibbiny1b5daf12015-06-04 19:30:46 +0300418}
419
420enum {
421 MLX5_VPORT_ACCESS_METHOD_MAD,
422 MLX5_VPORT_ACCESS_METHOD_HCA,
423 MLX5_VPORT_ACCESS_METHOD_NIC,
424};
425
426static int mlx5_get_vport_access_method(struct ib_device *ibdev)
427{
428 if (mlx5_use_mad_ifc(to_mdev(ibdev)))
429 return MLX5_VPORT_ACCESS_METHOD_MAD;
430
Achiad Shochatebd61f62015-12-23 18:47:16 +0200431 if (mlx5_ib_port_link_layer(ibdev, 1) ==
Majd Dibbiny1b5daf12015-06-04 19:30:46 +0300432 IB_LINK_LAYER_ETHERNET)
433 return MLX5_VPORT_ACCESS_METHOD_NIC;
434
435 return MLX5_VPORT_ACCESS_METHOD_HCA;
436}
437
Eran Ben Elishada7525d2015-12-14 16:34:10 +0200438static void get_atomic_caps(struct mlx5_ib_dev *dev,
439 struct ib_device_attr *props)
440{
441 u8 tmp;
442 u8 atomic_operations = MLX5_CAP_ATOMIC(dev->mdev, atomic_operations);
443 u8 atomic_size_qp = MLX5_CAP_ATOMIC(dev->mdev, atomic_size_qp);
444 u8 atomic_req_8B_endianness_mode =
Or Gerlitzbd108382017-05-28 15:24:17 +0300445 MLX5_CAP_ATOMIC(dev->mdev, atomic_req_8B_endianness_mode);
Eran Ben Elishada7525d2015-12-14 16:34:10 +0200446
447 /* Check if HW supports 8 bytes standard atomic operations and capable
448 * of host endianness respond
449 */
450 tmp = MLX5_ATOMIC_OPS_CMP_SWAP | MLX5_ATOMIC_OPS_FETCH_ADD;
451 if (((atomic_operations & tmp) == tmp) &&
452 (atomic_size_qp & MLX5_ATOMIC_SIZE_QP_8BYTES) &&
453 (atomic_req_8B_endianness_mode)) {
454 props->atomic_cap = IB_ATOMIC_HCA;
455 } else {
456 props->atomic_cap = IB_ATOMIC_NONE;
457 }
458}
459
Majd Dibbiny1b5daf12015-06-04 19:30:46 +0300460static int mlx5_query_system_image_guid(struct ib_device *ibdev,
461 __be64 *sys_image_guid)
462{
463 struct mlx5_ib_dev *dev = to_mdev(ibdev);
464 struct mlx5_core_dev *mdev = dev->mdev;
465 u64 tmp;
466 int err;
467
468 switch (mlx5_get_vport_access_method(ibdev)) {
469 case MLX5_VPORT_ACCESS_METHOD_MAD:
470 return mlx5_query_mad_ifc_system_image_guid(ibdev,
471 sys_image_guid);
472
473 case MLX5_VPORT_ACCESS_METHOD_HCA:
474 err = mlx5_query_hca_vport_system_image_guid(mdev, &tmp);
Achiad Shochat3f89a642015-12-23 18:47:21 +0200475 break;
476
477 case MLX5_VPORT_ACCESS_METHOD_NIC:
478 err = mlx5_query_nic_vport_system_image_guid(mdev, &tmp);
479 break;
Majd Dibbiny1b5daf12015-06-04 19:30:46 +0300480
481 default:
482 return -EINVAL;
483 }
Achiad Shochat3f89a642015-12-23 18:47:21 +0200484
485 if (!err)
486 *sys_image_guid = cpu_to_be64(tmp);
487
488 return err;
489
Majd Dibbiny1b5daf12015-06-04 19:30:46 +0300490}
491
492static int mlx5_query_max_pkeys(struct ib_device *ibdev,
493 u16 *max_pkeys)
494{
495 struct mlx5_ib_dev *dev = to_mdev(ibdev);
496 struct mlx5_core_dev *mdev = dev->mdev;
497
498 switch (mlx5_get_vport_access_method(ibdev)) {
499 case MLX5_VPORT_ACCESS_METHOD_MAD:
500 return mlx5_query_mad_ifc_max_pkeys(ibdev, max_pkeys);
501
502 case MLX5_VPORT_ACCESS_METHOD_HCA:
503 case MLX5_VPORT_ACCESS_METHOD_NIC:
504 *max_pkeys = mlx5_to_sw_pkey_sz(MLX5_CAP_GEN(mdev,
505 pkey_table_size));
506 return 0;
507
508 default:
509 return -EINVAL;
510 }
511}
512
513static int mlx5_query_vendor_id(struct ib_device *ibdev,
514 u32 *vendor_id)
515{
516 struct mlx5_ib_dev *dev = to_mdev(ibdev);
517
518 switch (mlx5_get_vport_access_method(ibdev)) {
519 case MLX5_VPORT_ACCESS_METHOD_MAD:
520 return mlx5_query_mad_ifc_vendor_id(ibdev, vendor_id);
521
522 case MLX5_VPORT_ACCESS_METHOD_HCA:
523 case MLX5_VPORT_ACCESS_METHOD_NIC:
524 return mlx5_core_query_vendor_id(dev->mdev, vendor_id);
525
526 default:
527 return -EINVAL;
528 }
529}
530
531static int mlx5_query_node_guid(struct mlx5_ib_dev *dev,
532 __be64 *node_guid)
533{
534 u64 tmp;
535 int err;
536
537 switch (mlx5_get_vport_access_method(&dev->ib_dev)) {
538 case MLX5_VPORT_ACCESS_METHOD_MAD:
539 return mlx5_query_mad_ifc_node_guid(dev, node_guid);
540
541 case MLX5_VPORT_ACCESS_METHOD_HCA:
542 err = mlx5_query_hca_vport_node_guid(dev->mdev, &tmp);
Achiad Shochat3f89a642015-12-23 18:47:21 +0200543 break;
544
545 case MLX5_VPORT_ACCESS_METHOD_NIC:
546 err = mlx5_query_nic_vport_node_guid(dev->mdev, &tmp);
547 break;
Majd Dibbiny1b5daf12015-06-04 19:30:46 +0300548
549 default:
550 return -EINVAL;
551 }
Achiad Shochat3f89a642015-12-23 18:47:21 +0200552
553 if (!err)
554 *node_guid = cpu_to_be64(tmp);
555
556 return err;
Majd Dibbiny1b5daf12015-06-04 19:30:46 +0300557}
558
559struct mlx5_reg_node_desc {
Yuval Shaiabd99fde2016-08-25 10:57:07 -0700560 u8 desc[IB_DEVICE_NODE_DESC_MAX];
Majd Dibbiny1b5daf12015-06-04 19:30:46 +0300561};
562
563static int mlx5_query_node_desc(struct mlx5_ib_dev *dev, char *node_desc)
564{
565 struct mlx5_reg_node_desc in;
566
567 if (mlx5_use_mad_ifc(dev))
568 return mlx5_query_mad_ifc_node_desc(dev, node_desc);
569
570 memset(&in, 0, sizeof(in));
571
572 return mlx5_core_access_reg(dev->mdev, &in, sizeof(in), node_desc,
573 sizeof(struct mlx5_reg_node_desc),
574 MLX5_REG_NODE_DESC, 0, 0);
575}
576
Eli Cohene126ba92013-07-07 17:25:49 +0300577static int mlx5_ib_query_device(struct ib_device *ibdev,
Matan Barak2528e332015-06-11 16:35:25 +0300578 struct ib_device_attr *props,
579 struct ib_udata *uhw)
Eli Cohene126ba92013-07-07 17:25:49 +0300580{
581 struct mlx5_ib_dev *dev = to_mdev(ibdev);
Saeed Mahameed938fe832015-05-28 22:28:41 +0300582 struct mlx5_core_dev *mdev = dev->mdev;
Eli Cohene126ba92013-07-07 17:25:49 +0300583 int err = -ENOMEM;
Eli Cohen288c01b2016-10-27 16:36:45 +0300584 int max_sq_desc;
Eli Cohene126ba92013-07-07 17:25:49 +0300585 int max_rq_sg;
586 int max_sq_sg;
Sagi Grimberge0238a62015-07-21 14:40:12 +0300587 u64 min_page_size = 1ull << MLX5_CAP_GEN(mdev, log_pg_sz);
Bodong Wang402ca532016-06-17 15:02:20 +0300588 struct mlx5_ib_query_device_resp resp = {};
589 size_t resp_len;
590 u64 max_tso;
Eli Cohene126ba92013-07-07 17:25:49 +0300591
Bodong Wang402ca532016-06-17 15:02:20 +0300592 resp_len = sizeof(resp.comp_mask) + sizeof(resp.response_length);
593 if (uhw->outlen && uhw->outlen < resp_len)
594 return -EINVAL;
595 else
596 resp.response_length = resp_len;
597
598 if (uhw->inlen && !ib_is_udata_cleared(uhw, 0, uhw->inlen))
Matan Barak2528e332015-06-11 16:35:25 +0300599 return -EINVAL;
600
Eli Cohene126ba92013-07-07 17:25:49 +0300601 memset(props, 0, sizeof(*props));
Majd Dibbiny1b5daf12015-06-04 19:30:46 +0300602 err = mlx5_query_system_image_guid(ibdev,
603 &props->sys_image_guid);
604 if (err)
605 return err;
606
607 err = mlx5_query_max_pkeys(ibdev, &props->max_pkeys);
608 if (err)
609 return err;
610
611 err = mlx5_query_vendor_id(ibdev, &props->vendor_id);
612 if (err)
613 return err;
Eli Cohene126ba92013-07-07 17:25:49 +0300614
Jack Morgenstein9603b612014-07-28 23:30:22 +0300615 props->fw_ver = ((u64)fw_rev_maj(dev->mdev) << 32) |
616 (fw_rev_min(dev->mdev) << 16) |
617 fw_rev_sub(dev->mdev);
Eli Cohene126ba92013-07-07 17:25:49 +0300618 props->device_cap_flags = IB_DEVICE_CHANGE_PHY_PORT |
619 IB_DEVICE_PORT_ACTIVE_EVENT |
620 IB_DEVICE_SYS_IMAGE_GUID |
Eli Cohen1a4c3a32014-02-06 17:41:25 +0200621 IB_DEVICE_RC_RNR_NAK_GEN;
Saeed Mahameed938fe832015-05-28 22:28:41 +0300622
623 if (MLX5_CAP_GEN(mdev, pkv))
Eli Cohene126ba92013-07-07 17:25:49 +0300624 props->device_cap_flags |= IB_DEVICE_BAD_PKEY_CNTR;
Saeed Mahameed938fe832015-05-28 22:28:41 +0300625 if (MLX5_CAP_GEN(mdev, qkv))
Eli Cohene126ba92013-07-07 17:25:49 +0300626 props->device_cap_flags |= IB_DEVICE_BAD_QKEY_CNTR;
Saeed Mahameed938fe832015-05-28 22:28:41 +0300627 if (MLX5_CAP_GEN(mdev, apm))
Eli Cohene126ba92013-07-07 17:25:49 +0300628 props->device_cap_flags |= IB_DEVICE_AUTO_PATH_MIG;
Saeed Mahameed938fe832015-05-28 22:28:41 +0300629 if (MLX5_CAP_GEN(mdev, xrc))
Eli Cohene126ba92013-07-07 17:25:49 +0300630 props->device_cap_flags |= IB_DEVICE_XRC;
Matan Barakd2370e02016-02-29 18:05:30 +0200631 if (MLX5_CAP_GEN(mdev, imaicl)) {
632 props->device_cap_flags |= IB_DEVICE_MEM_WINDOW |
633 IB_DEVICE_MEM_WINDOW_TYPE_2B;
634 props->max_mw = 1 << MLX5_CAP_GEN(mdev, log_max_mkey);
Sagi Grimbergb005d312016-02-29 19:07:33 +0200635 /* We support 'Gappy' memory registration too */
636 props->device_cap_flags |= IB_DEVICE_SG_GAPS_REG;
Matan Barakd2370e02016-02-29 18:05:30 +0200637 }
Eli Cohene126ba92013-07-07 17:25:49 +0300638 props->device_cap_flags |= IB_DEVICE_MEM_MGT_EXTENSIONS;
Saeed Mahameed938fe832015-05-28 22:28:41 +0300639 if (MLX5_CAP_GEN(mdev, sho)) {
Sagi Grimberg2dea9092014-02-23 14:19:13 +0200640 props->device_cap_flags |= IB_DEVICE_SIGNATURE_HANDOVER;
641 /* At this stage no support for signature handover */
642 props->sig_prot_cap = IB_PROT_T10DIF_TYPE_1 |
643 IB_PROT_T10DIF_TYPE_2 |
644 IB_PROT_T10DIF_TYPE_3;
645 props->sig_guard_cap = IB_GUARD_T10DIF_CRC |
646 IB_GUARD_T10DIF_CSUM;
647 }
Saeed Mahameed938fe832015-05-28 22:28:41 +0300648 if (MLX5_CAP_GEN(mdev, block_lb_mc))
Eli Cohenf360d882014-04-02 00:10:16 +0300649 props->device_cap_flags |= IB_DEVICE_BLOCK_MULTICAST_LOOPBACK;
Eli Cohene126ba92013-07-07 17:25:49 +0300650
Bodong Wang402ca532016-06-17 15:02:20 +0300651 if (MLX5_CAP_GEN(dev->mdev, eth_net_offloads)) {
Noa Osheroviche8161332017-01-18 15:40:01 +0200652 if (MLX5_CAP_ETH(mdev, csum_cap)) {
653 /* Legacy bit to support old userspace libraries */
Bodong Wang88115fe2015-12-18 13:53:20 +0200654 props->device_cap_flags |= IB_DEVICE_RAW_IP_CSUM;
Noa Osheroviche8161332017-01-18 15:40:01 +0200655 props->raw_packet_caps |= IB_RAW_PACKET_CAP_IP_CSUM;
656 }
657
658 if (MLX5_CAP_ETH(dev->mdev, vlan_cap))
659 props->raw_packet_caps |=
660 IB_RAW_PACKET_CAP_CVLAN_STRIPPING;
Bodong Wang88115fe2015-12-18 13:53:20 +0200661
Bodong Wang402ca532016-06-17 15:02:20 +0300662 if (field_avail(typeof(resp), tso_caps, uhw->outlen)) {
663 max_tso = MLX5_CAP_ETH(mdev, max_lso_cap);
664 if (max_tso) {
665 resp.tso_caps.max_tso = 1 << max_tso;
666 resp.tso_caps.supported_qpts |=
667 1 << IB_QPT_RAW_PACKET;
668 resp.response_length += sizeof(resp.tso_caps);
669 }
670 }
Yishai Hadas31f69a82016-08-28 11:28:45 +0300671
672 if (field_avail(typeof(resp), rss_caps, uhw->outlen)) {
673 resp.rss_caps.rx_hash_function =
674 MLX5_RX_HASH_FUNC_TOEPLITZ;
675 resp.rss_caps.rx_hash_fields_mask =
676 MLX5_RX_HASH_SRC_IPV4 |
677 MLX5_RX_HASH_DST_IPV4 |
678 MLX5_RX_HASH_SRC_IPV6 |
679 MLX5_RX_HASH_DST_IPV6 |
680 MLX5_RX_HASH_SRC_PORT_TCP |
681 MLX5_RX_HASH_DST_PORT_TCP |
682 MLX5_RX_HASH_SRC_PORT_UDP |
Maor Gottlieb4e2b53a2017-12-24 14:51:25 +0200683 MLX5_RX_HASH_DST_PORT_UDP |
684 MLX5_RX_HASH_INNER;
Yishai Hadas31f69a82016-08-28 11:28:45 +0300685 resp.response_length += sizeof(resp.rss_caps);
686 }
687 } else {
688 if (field_avail(typeof(resp), tso_caps, uhw->outlen))
689 resp.response_length += sizeof(resp.tso_caps);
690 if (field_avail(typeof(resp), rss_caps, uhw->outlen))
691 resp.response_length += sizeof(resp.rss_caps);
Bodong Wang402ca532016-06-17 15:02:20 +0300692 }
693
Erez Shitritf0313962016-02-21 16:27:17 +0200694 if (MLX5_CAP_GEN(mdev, ipoib_basic_offloads)) {
695 props->device_cap_flags |= IB_DEVICE_UD_IP_CSUM;
696 props->device_cap_flags |= IB_DEVICE_UD_TSO;
697 }
698
Maor Gottlieb03404e82017-05-30 10:29:13 +0300699 if (MLX5_CAP_GEN(dev->mdev, rq_delay_drop) &&
700 MLX5_CAP_GEN(dev->mdev, general_notification_event))
701 props->raw_packet_caps |= IB_RAW_PACKET_CAP_DELAY_DROP;
702
Yishai Hadas1d54f892017-06-08 16:15:11 +0300703 if (MLX5_CAP_GEN(mdev, ipoib_enhanced_offloads) &&
704 MLX5_CAP_IPOIB_ENHANCED(mdev, csum_cap))
705 props->device_cap_flags |= IB_DEVICE_UD_IP_CSUM;
706
Majd Dibbinycff5a0f2016-04-17 17:19:38 +0300707 if (MLX5_CAP_GEN(dev->mdev, eth_net_offloads) &&
Noa Osheroviche8161332017-01-18 15:40:01 +0200708 MLX5_CAP_ETH(dev->mdev, scatter_fcs)) {
709 /* Legacy bit to support old userspace libraries */
Majd Dibbinycff5a0f2016-04-17 17:19:38 +0300710 props->device_cap_flags |= IB_DEVICE_RAW_SCATTER_FCS;
Noa Osheroviche8161332017-01-18 15:40:01 +0200711 props->raw_packet_caps |= IB_RAW_PACKET_CAP_SCATTER_FCS;
712 }
Majd Dibbinycff5a0f2016-04-17 17:19:38 +0300713
Maor Gottliebda6d6ba32016-06-04 15:15:28 +0300714 if (mlx5_get_flow_namespace(dev->mdev, MLX5_FLOW_NAMESPACE_BYPASS))
715 props->device_cap_flags |= IB_DEVICE_MANAGED_FLOW_STEERING;
716
Noa Osherovichb1383aa2017-10-29 13:59:45 +0200717 if (MLX5_CAP_GEN(mdev, end_pad))
718 props->device_cap_flags |= IB_DEVICE_PCI_WRITE_END_PADDING;
719
Majd Dibbiny1b5daf12015-06-04 19:30:46 +0300720 props->vendor_part_id = mdev->pdev->device;
721 props->hw_ver = mdev->pdev->revision;
Eli Cohene126ba92013-07-07 17:25:49 +0300722
723 props->max_mr_size = ~0ull;
Sagi Grimberge0238a62015-07-21 14:40:12 +0300724 props->page_size_cap = ~(min_page_size - 1);
Saeed Mahameed938fe832015-05-28 22:28:41 +0300725 props->max_qp = 1 << MLX5_CAP_GEN(mdev, log_max_qp);
726 props->max_qp_wr = 1 << MLX5_CAP_GEN(mdev, log_max_qp_sz);
727 max_rq_sg = MLX5_CAP_GEN(mdev, max_wqe_sz_rq) /
728 sizeof(struct mlx5_wqe_data_seg);
Eli Cohen288c01b2016-10-27 16:36:45 +0300729 max_sq_desc = min_t(int, MLX5_CAP_GEN(mdev, max_wqe_sz_sq), 512);
730 max_sq_sg = (max_sq_desc - sizeof(struct mlx5_wqe_ctrl_seg) -
731 sizeof(struct mlx5_wqe_raddr_seg)) /
732 sizeof(struct mlx5_wqe_data_seg);
Eli Cohene126ba92013-07-07 17:25:49 +0300733 props->max_sge = min(max_rq_sg, max_sq_sg);
Sagi Grimberg986ef952016-03-31 19:03:25 +0300734 props->max_sge_rd = MLX5_MAX_SGE_RD;
Saeed Mahameed938fe832015-05-28 22:28:41 +0300735 props->max_cq = 1 << MLX5_CAP_GEN(mdev, log_max_cq);
Leon Romanovsky9f177682016-01-14 08:11:40 +0200736 props->max_cqe = (1 << MLX5_CAP_GEN(mdev, log_max_cq_sz)) - 1;
Saeed Mahameed938fe832015-05-28 22:28:41 +0300737 props->max_mr = 1 << MLX5_CAP_GEN(mdev, log_max_mkey);
738 props->max_pd = 1 << MLX5_CAP_GEN(mdev, log_max_pd);
739 props->max_qp_rd_atom = 1 << MLX5_CAP_GEN(mdev, log_max_ra_req_qp);
740 props->max_qp_init_rd_atom = 1 << MLX5_CAP_GEN(mdev, log_max_ra_res_qp);
741 props->max_srq = 1 << MLX5_CAP_GEN(mdev, log_max_srq);
742 props->max_srq_wr = (1 << MLX5_CAP_GEN(mdev, log_max_srq_sz)) - 1;
743 props->local_ca_ack_delay = MLX5_CAP_GEN(mdev, local_ca_ack_delay);
Eli Cohene126ba92013-07-07 17:25:49 +0300744 props->max_res_rd_atom = props->max_qp_rd_atom * props->max_qp;
Eli Cohene126ba92013-07-07 17:25:49 +0300745 props->max_srq_sge = max_rq_sg - 1;
Sagi Grimberg911f4332016-03-03 13:37:51 +0200746 props->max_fast_reg_page_list_len =
747 1 << MLX5_CAP_GEN(mdev, log_max_klm_list_size);
Eran Ben Elishada7525d2015-12-14 16:34:10 +0200748 get_atomic_caps(dev, props);
Eli Cohen81bea282013-09-11 16:35:30 +0300749 props->masked_atomic_cap = IB_ATOMIC_NONE;
Saeed Mahameed938fe832015-05-28 22:28:41 +0300750 props->max_mcast_grp = 1 << MLX5_CAP_GEN(mdev, log_max_mcg);
751 props->max_mcast_qp_attach = MLX5_CAP_GEN(mdev, max_qp_mcg);
Eli Cohene126ba92013-07-07 17:25:49 +0300752 props->max_total_mcast_qp_attach = props->max_mcast_qp_attach *
753 props->max_mcast_grp;
754 props->max_map_per_fmr = INT_MAX; /* no limit in ConnectIB */
Maor Gottlieb86695a62016-10-27 16:36:38 +0300755 props->max_ah = INT_MAX;
Matan Barak7c60bcb2015-12-15 20:30:11 +0200756 props->hca_core_clock = MLX5_CAP_GEN(mdev, device_frequency_khz);
757 props->timestamp_mask = 0x7FFFFFFFFFFFFFFFULL;
Eli Cohene126ba92013-07-07 17:25:49 +0300758
Haggai Eran8cdd3122014-12-11 17:04:20 +0200759#ifdef CONFIG_INFINIBAND_ON_DEMAND_PAGING
Saeed Mahameed938fe832015-05-28 22:28:41 +0300760 if (MLX5_CAP_GEN(mdev, pg))
Haggai Eran8cdd3122014-12-11 17:04:20 +0200761 props->device_cap_flags |= IB_DEVICE_ON_DEMAND_PAGING;
762 props->odp_caps = dev->odp_caps;
763#endif
764
Leon Romanovsky051f2632015-12-20 12:16:11 +0200765 if (MLX5_CAP_GEN(mdev, cd))
766 props->device_cap_flags |= IB_DEVICE_CROSS_CHANNEL;
767
Eli Coheneff901d2016-03-11 22:58:42 +0200768 if (!mlx5_core_is_pf(mdev))
769 props->device_cap_flags |= IB_DEVICE_VIRTUAL_FUNCTION;
770
Yishai Hadas31f69a82016-08-28 11:28:45 +0300771 if (mlx5_ib_port_link_layer(ibdev, 1) ==
772 IB_LINK_LAYER_ETHERNET) {
773 props->rss_caps.max_rwq_indirection_tables =
774 1 << MLX5_CAP_GEN(dev->mdev, log_max_rqt);
775 props->rss_caps.max_rwq_indirection_table_size =
776 1 << MLX5_CAP_GEN(dev->mdev, log_max_rqt_size);
777 props->rss_caps.supported_qpts = 1 << IB_QPT_RAW_PACKET;
778 props->max_wq_type_rq =
779 1 << MLX5_CAP_GEN(dev->mdev, log_max_rq);
780 }
781
Artemy Kovalyoveb761892017-08-17 15:52:09 +0300782 if (MLX5_CAP_GEN(mdev, tag_matching)) {
Leon Romanovsky78b1beb2017-09-24 21:46:29 +0300783 props->tm_caps.max_rndv_hdr_size = MLX5_TM_MAX_RNDV_MSG_SIZE;
784 props->tm_caps.max_num_tags =
Artemy Kovalyoveb761892017-08-17 15:52:09 +0300785 (1 << MLX5_CAP_GEN(mdev, log_tag_matching_list_sz)) - 1;
Leon Romanovsky78b1beb2017-09-24 21:46:29 +0300786 props->tm_caps.flags = IB_TM_CAP_RC;
787 props->tm_caps.max_ops =
Artemy Kovalyoveb761892017-08-17 15:52:09 +0300788 1 << MLX5_CAP_GEN(mdev, log_max_qp_sz);
Leon Romanovsky78b1beb2017-09-24 21:46:29 +0300789 props->tm_caps.max_sge = MLX5_TM_MAX_SGE;
Artemy Kovalyoveb761892017-08-17 15:52:09 +0300790 }
791
Yonatan Cohen87ab3f52017-11-13 10:51:18 +0200792 if (MLX5_CAP_GEN(dev->mdev, cq_moderation)) {
793 props->cq_caps.max_cq_moderation_count =
794 MLX5_MAX_CQ_COUNT;
795 props->cq_caps.max_cq_moderation_period =
796 MLX5_MAX_CQ_PERIOD;
797 }
798
Bodong Wang7e43a2a2016-10-31 12:16:44 +0200799 if (field_avail(typeof(resp), cqe_comp_caps, uhw->outlen)) {
800 resp.cqe_comp_caps.max_num =
801 MLX5_CAP_GEN(dev->mdev, cqe_compression) ?
802 MLX5_CAP_GEN(dev->mdev, cqe_compression_max_num) : 0;
803 resp.cqe_comp_caps.supported_format =
804 MLX5_IB_CQE_RES_FORMAT_HASH |
805 MLX5_IB_CQE_RES_FORMAT_CSUM;
806 resp.response_length += sizeof(resp.cqe_comp_caps);
807 }
808
Bodong Wangd9491672016-12-01 13:43:13 +0200809 if (field_avail(typeof(resp), packet_pacing_caps, uhw->outlen)) {
810 if (MLX5_CAP_QOS(mdev, packet_pacing) &&
811 MLX5_CAP_GEN(mdev, qos)) {
812 resp.packet_pacing_caps.qp_rate_limit_max =
813 MLX5_CAP_QOS(mdev, packet_pacing_max_rate);
814 resp.packet_pacing_caps.qp_rate_limit_min =
815 MLX5_CAP_QOS(mdev, packet_pacing_min_rate);
816 resp.packet_pacing_caps.supported_qpts |=
817 1 << IB_QPT_RAW_PACKET;
818 }
819 resp.response_length += sizeof(resp.packet_pacing_caps);
820 }
821
Leon Romanovsky9f885202017-01-02 11:37:39 +0200822 if (field_avail(typeof(resp), mlx5_ib_support_multi_pkt_send_wqes,
823 uhw->outlen)) {
Bodong Wang795b6092017-08-17 15:52:34 +0300824 if (MLX5_CAP_ETH(mdev, multi_pkt_send_wqe))
825 resp.mlx5_ib_support_multi_pkt_send_wqes =
826 MLX5_IB_ALLOW_MPW;
Bodong Wang050da902017-08-17 15:52:35 +0300827
828 if (MLX5_CAP_ETH(mdev, enhanced_multi_pkt_send_wqe))
829 resp.mlx5_ib_support_multi_pkt_send_wqes |=
830 MLX5_IB_SUPPORT_EMPW;
831
Leon Romanovsky9f885202017-01-02 11:37:39 +0200832 resp.response_length +=
833 sizeof(resp.mlx5_ib_support_multi_pkt_send_wqes);
834 }
835
Guy Levide57f2a2017-10-19 08:25:52 +0300836 if (field_avail(typeof(resp), flags, uhw->outlen)) {
837 resp.response_length += sizeof(resp.flags);
Guy Levi7a0c8f42017-10-19 08:25:53 +0300838
Guy Levide57f2a2017-10-19 08:25:52 +0300839 if (MLX5_CAP_GEN(mdev, cqe_compression_128))
840 resp.flags |=
841 MLX5_IB_QUERY_DEV_RESP_FLAGS_CQE_128B_COMP;
Guy Levi7a0c8f42017-10-19 08:25:53 +0300842
843 if (MLX5_CAP_GEN(mdev, cqe_128_always))
844 resp.flags |= MLX5_IB_QUERY_DEV_RESP_FLAGS_CQE_128B_PAD;
Guy Levide57f2a2017-10-19 08:25:52 +0300845 }
Leon Romanovsky9f885202017-01-02 11:37:39 +0200846
Noa Osherovich96dc3fc2017-08-17 15:52:28 +0300847 if (field_avail(typeof(resp), sw_parsing_caps,
848 uhw->outlen)) {
849 resp.response_length += sizeof(resp.sw_parsing_caps);
850 if (MLX5_CAP_ETH(mdev, swp)) {
851 resp.sw_parsing_caps.sw_parsing_offloads |=
852 MLX5_IB_SW_PARSING;
853
854 if (MLX5_CAP_ETH(mdev, swp_csum))
855 resp.sw_parsing_caps.sw_parsing_offloads |=
856 MLX5_IB_SW_PARSING_CSUM;
857
858 if (MLX5_CAP_ETH(mdev, swp_lso))
859 resp.sw_parsing_caps.sw_parsing_offloads |=
860 MLX5_IB_SW_PARSING_LSO;
861
862 if (resp.sw_parsing_caps.sw_parsing_offloads)
863 resp.sw_parsing_caps.supported_qpts =
864 BIT(IB_QPT_RAW_PACKET);
865 }
866 }
867
Noa Osherovichb4f34592017-10-17 18:01:12 +0300868 if (field_avail(typeof(resp), striding_rq_caps, uhw->outlen)) {
869 resp.response_length += sizeof(resp.striding_rq_caps);
870 if (MLX5_CAP_GEN(mdev, striding_rq)) {
871 resp.striding_rq_caps.min_single_stride_log_num_of_bytes =
872 MLX5_MIN_SINGLE_STRIDE_LOG_NUM_BYTES;
873 resp.striding_rq_caps.max_single_stride_log_num_of_bytes =
874 MLX5_MAX_SINGLE_STRIDE_LOG_NUM_BYTES;
875 resp.striding_rq_caps.min_single_wqe_log_num_of_strides =
876 MLX5_MIN_SINGLE_WQE_LOG_NUM_STRIDES;
877 resp.striding_rq_caps.max_single_wqe_log_num_of_strides =
878 MLX5_MAX_SINGLE_WQE_LOG_NUM_STRIDES;
879 resp.striding_rq_caps.supported_qpts =
880 BIT(IB_QPT_RAW_PACKET);
881 }
882 }
883
Maor Gottliebf95ef6c2017-10-19 08:25:55 +0300884 if (field_avail(typeof(resp), tunnel_offloads_caps,
885 uhw->outlen)) {
886 resp.response_length += sizeof(resp.tunnel_offloads_caps);
887 if (MLX5_CAP_ETH(mdev, tunnel_stateless_vxlan))
888 resp.tunnel_offloads_caps |=
889 MLX5_IB_TUNNELED_OFFLOADS_VXLAN;
890 if (MLX5_CAP_ETH(mdev, tunnel_stateless_geneve_rx))
891 resp.tunnel_offloads_caps |=
892 MLX5_IB_TUNNELED_OFFLOADS_GENEVE;
893 if (MLX5_CAP_ETH(mdev, tunnel_stateless_gre))
894 resp.tunnel_offloads_caps |=
895 MLX5_IB_TUNNELED_OFFLOADS_GRE;
896 }
897
Bodong Wang402ca532016-06-17 15:02:20 +0300898 if (uhw->outlen) {
899 err = ib_copy_to_udata(uhw, &resp, resp.response_length);
900
901 if (err)
902 return err;
903 }
904
Majd Dibbiny1b5daf12015-06-04 19:30:46 +0300905 return 0;
906}
Eli Cohene126ba92013-07-07 17:25:49 +0300907
Majd Dibbiny1b5daf12015-06-04 19:30:46 +0300908enum mlx5_ib_width {
909 MLX5_IB_WIDTH_1X = 1 << 0,
910 MLX5_IB_WIDTH_2X = 1 << 1,
911 MLX5_IB_WIDTH_4X = 1 << 2,
912 MLX5_IB_WIDTH_8X = 1 << 3,
913 MLX5_IB_WIDTH_12X = 1 << 4
914};
915
916static int translate_active_width(struct ib_device *ibdev, u8 active_width,
917 u8 *ib_width)
918{
919 struct mlx5_ib_dev *dev = to_mdev(ibdev);
920 int err = 0;
921
922 if (active_width & MLX5_IB_WIDTH_1X) {
923 *ib_width = IB_WIDTH_1X;
924 } else if (active_width & MLX5_IB_WIDTH_2X) {
925 mlx5_ib_dbg(dev, "active_width %d is not supported by IB spec\n",
926 (int)active_width);
927 err = -EINVAL;
928 } else if (active_width & MLX5_IB_WIDTH_4X) {
929 *ib_width = IB_WIDTH_4X;
930 } else if (active_width & MLX5_IB_WIDTH_8X) {
931 *ib_width = IB_WIDTH_8X;
932 } else if (active_width & MLX5_IB_WIDTH_12X) {
933 *ib_width = IB_WIDTH_12X;
934 } else {
935 mlx5_ib_dbg(dev, "Invalid active_width %d\n",
936 (int)active_width);
937 err = -EINVAL;
938 }
939
940 return err;
941}
942
943static int mlx5_mtu_to_ib_mtu(int mtu)
944{
945 switch (mtu) {
946 case 256: return 1;
947 case 512: return 2;
948 case 1024: return 3;
949 case 2048: return 4;
950 case 4096: return 5;
951 default:
952 pr_warn("invalid mtu\n");
953 return -1;
954 }
955}
956
957enum ib_max_vl_num {
958 __IB_MAX_VL_0 = 1,
959 __IB_MAX_VL_0_1 = 2,
960 __IB_MAX_VL_0_3 = 3,
961 __IB_MAX_VL_0_7 = 4,
962 __IB_MAX_VL_0_14 = 5,
963};
964
965enum mlx5_vl_hw_cap {
966 MLX5_VL_HW_0 = 1,
967 MLX5_VL_HW_0_1 = 2,
968 MLX5_VL_HW_0_2 = 3,
969 MLX5_VL_HW_0_3 = 4,
970 MLX5_VL_HW_0_4 = 5,
971 MLX5_VL_HW_0_5 = 6,
972 MLX5_VL_HW_0_6 = 7,
973 MLX5_VL_HW_0_7 = 8,
974 MLX5_VL_HW_0_14 = 15
975};
976
977static int translate_max_vl_num(struct ib_device *ibdev, u8 vl_hw_cap,
978 u8 *max_vl_num)
979{
980 switch (vl_hw_cap) {
981 case MLX5_VL_HW_0:
982 *max_vl_num = __IB_MAX_VL_0;
983 break;
984 case MLX5_VL_HW_0_1:
985 *max_vl_num = __IB_MAX_VL_0_1;
986 break;
987 case MLX5_VL_HW_0_3:
988 *max_vl_num = __IB_MAX_VL_0_3;
989 break;
990 case MLX5_VL_HW_0_7:
991 *max_vl_num = __IB_MAX_VL_0_7;
992 break;
993 case MLX5_VL_HW_0_14:
994 *max_vl_num = __IB_MAX_VL_0_14;
995 break;
996
997 default:
998 return -EINVAL;
999 }
1000
1001 return 0;
1002}
1003
1004static int mlx5_query_hca_port(struct ib_device *ibdev, u8 port,
1005 struct ib_port_attr *props)
1006{
1007 struct mlx5_ib_dev *dev = to_mdev(ibdev);
1008 struct mlx5_core_dev *mdev = dev->mdev;
1009 struct mlx5_hca_vport_context *rep;
Saeed Mahameed046339e2016-04-22 00:33:03 +03001010 u16 max_mtu;
1011 u16 oper_mtu;
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03001012 int err;
1013 u8 ib_link_width_oper;
1014 u8 vl_hw_cap;
1015
1016 rep = kzalloc(sizeof(*rep), GFP_KERNEL);
1017 if (!rep) {
1018 err = -ENOMEM;
1019 goto out;
1020 }
1021
Or Gerlitzc4550c62017-01-24 13:02:39 +02001022 /* props being zeroed by the caller, avoid zeroing it here */
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03001023
1024 err = mlx5_query_hca_vport_context(mdev, 0, port, 0, rep);
1025 if (err)
1026 goto out;
1027
1028 props->lid = rep->lid;
1029 props->lmc = rep->lmc;
1030 props->sm_lid = rep->sm_lid;
1031 props->sm_sl = rep->sm_sl;
1032 props->state = rep->vport_state;
1033 props->phys_state = rep->port_physical_state;
1034 props->port_cap_flags = rep->cap_mask1;
1035 props->gid_tbl_len = mlx5_get_gid_table_len(MLX5_CAP_GEN(mdev, gid_table_size));
1036 props->max_msg_sz = 1 << MLX5_CAP_GEN(mdev, log_max_msg);
1037 props->pkey_tbl_len = mlx5_to_sw_pkey_sz(MLX5_CAP_GEN(mdev, pkey_table_size));
1038 props->bad_pkey_cntr = rep->pkey_violation_counter;
1039 props->qkey_viol_cntr = rep->qkey_violation_counter;
1040 props->subnet_timeout = rep->subnet_timeout;
1041 props->init_type_reply = rep->init_type_reply;
Eli Coheneff901d2016-03-11 22:58:42 +02001042 props->grh_required = rep->grh_required;
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03001043
1044 err = mlx5_query_port_link_width_oper(mdev, &ib_link_width_oper, port);
1045 if (err)
1046 goto out;
1047
1048 err = translate_active_width(ibdev, ib_link_width_oper,
1049 &props->active_width);
1050 if (err)
1051 goto out;
Noa Osherovichd5beb7f2016-06-02 10:47:53 +03001052 err = mlx5_query_port_ib_proto_oper(mdev, &props->active_speed, port);
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03001053 if (err)
1054 goto out;
1055
Saeed Mahameedfacc9692015-06-11 14:47:27 +03001056 mlx5_query_port_max_mtu(mdev, &max_mtu, port);
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03001057
1058 props->max_mtu = mlx5_mtu_to_ib_mtu(max_mtu);
1059
Saeed Mahameedfacc9692015-06-11 14:47:27 +03001060 mlx5_query_port_oper_mtu(mdev, &oper_mtu, port);
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03001061
1062 props->active_mtu = mlx5_mtu_to_ib_mtu(oper_mtu);
1063
1064 err = mlx5_query_port_vl_hw_cap(mdev, &vl_hw_cap, port);
1065 if (err)
1066 goto out;
1067
1068 err = translate_max_vl_num(ibdev, vl_hw_cap,
1069 &props->max_vl_num);
1070out:
1071 kfree(rep);
Eli Cohene126ba92013-07-07 17:25:49 +03001072 return err;
1073}
1074
1075int mlx5_ib_query_port(struct ib_device *ibdev, u8 port,
1076 struct ib_port_attr *props)
1077{
Ilan Tayari095b0922017-05-14 16:04:30 +03001078 unsigned int count;
1079 int ret;
1080
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03001081 switch (mlx5_get_vport_access_method(ibdev)) {
1082 case MLX5_VPORT_ACCESS_METHOD_MAD:
Ilan Tayari095b0922017-05-14 16:04:30 +03001083 ret = mlx5_query_mad_ifc_port(ibdev, port, props);
1084 break;
Eli Cohene126ba92013-07-07 17:25:49 +03001085
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03001086 case MLX5_VPORT_ACCESS_METHOD_HCA:
Ilan Tayari095b0922017-05-14 16:04:30 +03001087 ret = mlx5_query_hca_port(ibdev, port, props);
1088 break;
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03001089
Achiad Shochat3f89a642015-12-23 18:47:21 +02001090 case MLX5_VPORT_ACCESS_METHOD_NIC:
Ilan Tayari095b0922017-05-14 16:04:30 +03001091 ret = mlx5_query_port_roce(ibdev, port, props);
1092 break;
Achiad Shochat3f89a642015-12-23 18:47:21 +02001093
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03001094 default:
Ilan Tayari095b0922017-05-14 16:04:30 +03001095 ret = -EINVAL;
Eli Cohene126ba92013-07-07 17:25:49 +03001096 }
Ilan Tayari095b0922017-05-14 16:04:30 +03001097
1098 if (!ret && props) {
1099 count = mlx5_core_reserved_gids_count(to_mdev(ibdev)->mdev);
1100 props->gid_tbl_len -= count;
1101 }
1102 return ret;
Eli Cohene126ba92013-07-07 17:25:49 +03001103}
1104
1105static int mlx5_ib_query_gid(struct ib_device *ibdev, u8 port, int index,
1106 union ib_gid *gid)
1107{
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03001108 struct mlx5_ib_dev *dev = to_mdev(ibdev);
1109 struct mlx5_core_dev *mdev = dev->mdev;
Eli Cohene126ba92013-07-07 17:25:49 +03001110
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03001111 switch (mlx5_get_vport_access_method(ibdev)) {
1112 case MLX5_VPORT_ACCESS_METHOD_MAD:
1113 return mlx5_query_mad_ifc_gids(ibdev, port, index, gid);
Eli Cohene126ba92013-07-07 17:25:49 +03001114
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03001115 case MLX5_VPORT_ACCESS_METHOD_HCA:
1116 return mlx5_query_hca_vport_gid(mdev, 0, port, 0, index, gid);
Eli Cohene126ba92013-07-07 17:25:49 +03001117
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03001118 default:
1119 return -EINVAL;
1120 }
Eli Cohene126ba92013-07-07 17:25:49 +03001121
Eli Cohene126ba92013-07-07 17:25:49 +03001122}
1123
1124static int mlx5_ib_query_pkey(struct ib_device *ibdev, u8 port, u16 index,
1125 u16 *pkey)
1126{
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03001127 struct mlx5_ib_dev *dev = to_mdev(ibdev);
1128 struct mlx5_core_dev *mdev = dev->mdev;
Eli Cohene126ba92013-07-07 17:25:49 +03001129
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03001130 switch (mlx5_get_vport_access_method(ibdev)) {
1131 case MLX5_VPORT_ACCESS_METHOD_MAD:
1132 return mlx5_query_mad_ifc_pkey(ibdev, port, index, pkey);
Eli Cohene126ba92013-07-07 17:25:49 +03001133
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03001134 case MLX5_VPORT_ACCESS_METHOD_HCA:
1135 case MLX5_VPORT_ACCESS_METHOD_NIC:
1136 return mlx5_query_hca_vport_pkey(mdev, 0, port, 0, index,
1137 pkey);
1138 default:
1139 return -EINVAL;
1140 }
Eli Cohene126ba92013-07-07 17:25:49 +03001141}
1142
Eli Cohene126ba92013-07-07 17:25:49 +03001143static int mlx5_ib_modify_device(struct ib_device *ibdev, int mask,
1144 struct ib_device_modify *props)
1145{
1146 struct mlx5_ib_dev *dev = to_mdev(ibdev);
1147 struct mlx5_reg_node_desc in;
1148 struct mlx5_reg_node_desc out;
1149 int err;
1150
1151 if (mask & ~IB_DEVICE_MODIFY_NODE_DESC)
1152 return -EOPNOTSUPP;
1153
1154 if (!(mask & IB_DEVICE_MODIFY_NODE_DESC))
1155 return 0;
1156
1157 /*
1158 * If possible, pass node desc to FW, so it can generate
1159 * a 144 trap. If cmd fails, just ignore.
1160 */
Yuval Shaiabd99fde2016-08-25 10:57:07 -07001161 memcpy(&in, props->node_desc, IB_DEVICE_NODE_DESC_MAX);
Jack Morgenstein9603b612014-07-28 23:30:22 +03001162 err = mlx5_core_access_reg(dev->mdev, &in, sizeof(in), &out,
Eli Cohene126ba92013-07-07 17:25:49 +03001163 sizeof(out), MLX5_REG_NODE_DESC, 0, 1);
1164 if (err)
1165 return err;
1166
Yuval Shaiabd99fde2016-08-25 10:57:07 -07001167 memcpy(ibdev->node_desc, props->node_desc, IB_DEVICE_NODE_DESC_MAX);
Eli Cohene126ba92013-07-07 17:25:49 +03001168
1169 return err;
1170}
1171
Eli Cohencdbe33d2017-02-14 07:25:38 +02001172static int set_port_caps_atomic(struct mlx5_ib_dev *dev, u8 port_num, u32 mask,
1173 u32 value)
1174{
1175 struct mlx5_hca_vport_context ctx = {};
1176 int err;
1177
1178 err = mlx5_query_hca_vport_context(dev->mdev, 0,
1179 port_num, 0, &ctx);
1180 if (err)
1181 return err;
1182
1183 if (~ctx.cap_mask1_perm & mask) {
1184 mlx5_ib_warn(dev, "trying to change bitmask 0x%X but change supported 0x%X\n",
1185 mask, ctx.cap_mask1_perm);
1186 return -EINVAL;
1187 }
1188
1189 ctx.cap_mask1 = value;
1190 ctx.cap_mask1_perm = mask;
1191 err = mlx5_core_modify_hca_vport_context(dev->mdev, 0,
1192 port_num, 0, &ctx);
1193
1194 return err;
1195}
1196
Eli Cohene126ba92013-07-07 17:25:49 +03001197static int mlx5_ib_modify_port(struct ib_device *ibdev, u8 port, int mask,
1198 struct ib_port_modify *props)
1199{
1200 struct mlx5_ib_dev *dev = to_mdev(ibdev);
1201 struct ib_port_attr attr;
1202 u32 tmp;
1203 int err;
Eli Cohencdbe33d2017-02-14 07:25:38 +02001204 u32 change_mask;
1205 u32 value;
1206 bool is_ib = (mlx5_ib_port_link_layer(ibdev, port) ==
1207 IB_LINK_LAYER_INFINIBAND);
1208
Majd Dibbinyec255872017-08-23 08:35:42 +03001209 /* CM layer calls ib_modify_port() regardless of the link layer. For
1210 * Ethernet ports, qkey violation and Port capabilities are meaningless.
1211 */
1212 if (!is_ib)
1213 return 0;
1214
Eli Cohencdbe33d2017-02-14 07:25:38 +02001215 if (MLX5_CAP_GEN(dev->mdev, ib_virt) && is_ib) {
1216 change_mask = props->clr_port_cap_mask | props->set_port_cap_mask;
1217 value = ~props->clr_port_cap_mask | props->set_port_cap_mask;
1218 return set_port_caps_atomic(dev, port, change_mask, value);
1219 }
Eli Cohene126ba92013-07-07 17:25:49 +03001220
1221 mutex_lock(&dev->cap_mask_mutex);
1222
Or Gerlitzc4550c62017-01-24 13:02:39 +02001223 err = ib_query_port(ibdev, port, &attr);
Eli Cohene126ba92013-07-07 17:25:49 +03001224 if (err)
1225 goto out;
1226
1227 tmp = (attr.port_cap_flags | props->set_port_cap_mask) &
1228 ~props->clr_port_cap_mask;
1229
Jack Morgenstein9603b612014-07-28 23:30:22 +03001230 err = mlx5_set_port_caps(dev->mdev, port, tmp);
Eli Cohene126ba92013-07-07 17:25:49 +03001231
1232out:
1233 mutex_unlock(&dev->cap_mask_mutex);
1234 return err;
1235}
1236
Eli Cohen30aa60b2017-01-03 23:55:27 +02001237static void print_lib_caps(struct mlx5_ib_dev *dev, u64 caps)
1238{
1239 mlx5_ib_dbg(dev, "MLX5_LIB_CAP_4K_UAR = %s\n",
1240 caps & MLX5_LIB_CAP_4K_UAR ? "y" : "n");
1241}
1242
Yishai Hadas31a78a52017-12-24 16:31:34 +02001243static u16 calc_dynamic_bfregs(int uars_per_sys_page)
1244{
1245 /* Large page with non 4k uar support might limit the dynamic size */
1246 if (uars_per_sys_page == 1 && PAGE_SIZE > 4096)
1247 return MLX5_MIN_DYN_BFREGS;
1248
1249 return MLX5_MAX_DYN_BFREGS;
1250}
1251
Eli Cohenb037c292017-01-03 23:55:26 +02001252static int calc_total_bfregs(struct mlx5_ib_dev *dev, bool lib_uar_4k,
1253 struct mlx5_ib_alloc_ucontext_req_v2 *req,
Yishai Hadas31a78a52017-12-24 16:31:34 +02001254 struct mlx5_bfreg_info *bfregi)
Eli Cohenb037c292017-01-03 23:55:26 +02001255{
1256 int uars_per_sys_page;
1257 int bfregs_per_sys_page;
1258 int ref_bfregs = req->total_num_bfregs;
1259
1260 if (req->total_num_bfregs == 0)
1261 return -EINVAL;
1262
1263 BUILD_BUG_ON(MLX5_MAX_BFREGS % MLX5_NON_FP_BFREGS_IN_PAGE);
1264 BUILD_BUG_ON(MLX5_MAX_BFREGS < MLX5_NON_FP_BFREGS_IN_PAGE);
1265
1266 if (req->total_num_bfregs > MLX5_MAX_BFREGS)
1267 return -ENOMEM;
1268
1269 uars_per_sys_page = get_uars_per_sys_page(dev, lib_uar_4k);
1270 bfregs_per_sys_page = uars_per_sys_page * MLX5_NON_FP_BFREGS_PER_UAR;
Yishai Hadas31a78a52017-12-24 16:31:34 +02001271 /* This holds the required static allocation asked by the user */
Eli Cohenb037c292017-01-03 23:55:26 +02001272 req->total_num_bfregs = ALIGN(req->total_num_bfregs, bfregs_per_sys_page);
Eli Cohenb037c292017-01-03 23:55:26 +02001273 if (req->num_low_latency_bfregs > req->total_num_bfregs - 1)
1274 return -EINVAL;
1275
Yishai Hadas31a78a52017-12-24 16:31:34 +02001276 bfregi->num_static_sys_pages = req->total_num_bfregs / bfregs_per_sys_page;
1277 bfregi->num_dyn_bfregs = ALIGN(calc_dynamic_bfregs(uars_per_sys_page), bfregs_per_sys_page);
1278 bfregi->total_num_bfregs = req->total_num_bfregs + bfregi->num_dyn_bfregs;
1279 bfregi->num_sys_pages = bfregi->total_num_bfregs / bfregs_per_sys_page;
1280
1281 mlx5_ib_dbg(dev, "uar_4k: fw support %s, lib support %s, user requested %d bfregs, allocated %d, total bfregs %d, using %d sys pages\n",
Eli Cohenb037c292017-01-03 23:55:26 +02001282 MLX5_CAP_GEN(dev->mdev, uar_4k) ? "yes" : "no",
1283 lib_uar_4k ? "yes" : "no", ref_bfregs,
Yishai Hadas31a78a52017-12-24 16:31:34 +02001284 req->total_num_bfregs, bfregi->total_num_bfregs,
1285 bfregi->num_sys_pages);
Eli Cohenb037c292017-01-03 23:55:26 +02001286
1287 return 0;
1288}
1289
1290static int allocate_uars(struct mlx5_ib_dev *dev, struct mlx5_ib_ucontext *context)
1291{
1292 struct mlx5_bfreg_info *bfregi;
1293 int err;
1294 int i;
1295
1296 bfregi = &context->bfregi;
Yishai Hadas31a78a52017-12-24 16:31:34 +02001297 for (i = 0; i < bfregi->num_static_sys_pages; i++) {
Eli Cohenb037c292017-01-03 23:55:26 +02001298 err = mlx5_cmd_alloc_uar(dev->mdev, &bfregi->sys_pages[i]);
1299 if (err)
1300 goto error;
1301
1302 mlx5_ib_dbg(dev, "allocated uar %d\n", bfregi->sys_pages[i]);
1303 }
1304 return 0;
1305
1306error:
1307 for (--i; i >= 0; i--)
1308 if (mlx5_cmd_free_uar(dev->mdev, bfregi->sys_pages[i]))
1309 mlx5_ib_warn(dev, "failed to free uar %d\n", i);
1310
1311 return err;
1312}
1313
1314static int deallocate_uars(struct mlx5_ib_dev *dev, struct mlx5_ib_ucontext *context)
1315{
1316 struct mlx5_bfreg_info *bfregi;
1317 int err;
1318 int i;
1319
1320 bfregi = &context->bfregi;
Yishai Hadas31a78a52017-12-24 16:31:34 +02001321 for (i = 0; i < bfregi->num_static_sys_pages; i++) {
Eli Cohenb037c292017-01-03 23:55:26 +02001322 err = mlx5_cmd_free_uar(dev->mdev, bfregi->sys_pages[i]);
1323 if (err) {
1324 mlx5_ib_warn(dev, "failed to free uar %d\n", i);
1325 return err;
1326 }
1327 }
1328 return 0;
1329}
1330
Huy Nguyenc85023e2017-05-30 09:42:54 +03001331static int mlx5_ib_alloc_transport_domain(struct mlx5_ib_dev *dev, u32 *tdn)
1332{
1333 int err;
1334
1335 err = mlx5_core_alloc_transport_domain(dev->mdev, tdn);
1336 if (err)
1337 return err;
1338
1339 if ((MLX5_CAP_GEN(dev->mdev, port_type) != MLX5_CAP_PORT_TYPE_ETH) ||
1340 !MLX5_CAP_GEN(dev->mdev, disable_local_lb))
1341 return err;
1342
1343 mutex_lock(&dev->lb_mutex);
1344 dev->user_td++;
1345
1346 if (dev->user_td == 2)
1347 err = mlx5_nic_vport_update_local_lb(dev->mdev, true);
1348
1349 mutex_unlock(&dev->lb_mutex);
1350 return err;
1351}
1352
1353static void mlx5_ib_dealloc_transport_domain(struct mlx5_ib_dev *dev, u32 tdn)
1354{
1355 mlx5_core_dealloc_transport_domain(dev->mdev, tdn);
1356
1357 if ((MLX5_CAP_GEN(dev->mdev, port_type) != MLX5_CAP_PORT_TYPE_ETH) ||
1358 !MLX5_CAP_GEN(dev->mdev, disable_local_lb))
1359 return;
1360
1361 mutex_lock(&dev->lb_mutex);
1362 dev->user_td--;
1363
1364 if (dev->user_td < 2)
1365 mlx5_nic_vport_update_local_lb(dev->mdev, false);
1366
1367 mutex_unlock(&dev->lb_mutex);
1368}
1369
Eli Cohene126ba92013-07-07 17:25:49 +03001370static struct ib_ucontext *mlx5_ib_alloc_ucontext(struct ib_device *ibdev,
1371 struct ib_udata *udata)
1372{
1373 struct mlx5_ib_dev *dev = to_mdev(ibdev);
Matan Barakb368d7c2015-12-15 20:30:12 +02001374 struct mlx5_ib_alloc_ucontext_req_v2 req = {};
1375 struct mlx5_ib_alloc_ucontext_resp resp = {};
Eli Cohene126ba92013-07-07 17:25:49 +03001376 struct mlx5_ib_ucontext *context;
Eli Cohen2f5ff262017-01-03 23:55:21 +02001377 struct mlx5_bfreg_info *bfregi;
Eli Cohen78c0f982014-01-30 13:49:48 +02001378 int ver;
Eli Cohene126ba92013-07-07 17:25:49 +03001379 int err;
Majd Dibbinya168a41c2016-01-28 17:51:47 +02001380 size_t min_req_v2 = offsetof(struct mlx5_ib_alloc_ucontext_req_v2,
1381 max_cqe_version);
Eli Cohenb037c292017-01-03 23:55:26 +02001382 bool lib_uar_4k;
Eli Cohene126ba92013-07-07 17:25:49 +03001383
1384 if (!dev->ib_active)
1385 return ERR_PTR(-EAGAIN);
1386
Amrani, Rame0931112017-06-27 17:04:42 +03001387 if (udata->inlen == sizeof(struct mlx5_ib_alloc_ucontext_req))
Eli Cohen78c0f982014-01-30 13:49:48 +02001388 ver = 0;
Amrani, Rame0931112017-06-27 17:04:42 +03001389 else if (udata->inlen >= min_req_v2)
Eli Cohen78c0f982014-01-30 13:49:48 +02001390 ver = 2;
1391 else
1392 return ERR_PTR(-EINVAL);
1393
Amrani, Rame0931112017-06-27 17:04:42 +03001394 err = ib_copy_from_udata(&req, udata, min(udata->inlen, sizeof(req)));
Eli Cohene126ba92013-07-07 17:25:49 +03001395 if (err)
1396 return ERR_PTR(err);
1397
Matan Barakb368d7c2015-12-15 20:30:12 +02001398 if (req.flags)
Eli Cohen78c0f982014-01-30 13:49:48 +02001399 return ERR_PTR(-EINVAL);
1400
Haggai Abramovskyf72300c2016-01-14 19:12:58 +02001401 if (req.comp_mask || req.reserved0 || req.reserved1 || req.reserved2)
Matan Barakb368d7c2015-12-15 20:30:12 +02001402 return ERR_PTR(-EOPNOTSUPP);
1403
Eli Cohen2f5ff262017-01-03 23:55:21 +02001404 req.total_num_bfregs = ALIGN(req.total_num_bfregs,
1405 MLX5_NON_FP_BFREGS_PER_UAR);
1406 if (req.num_low_latency_bfregs > req.total_num_bfregs - 1)
Eli Cohene126ba92013-07-07 17:25:49 +03001407 return ERR_PTR(-EINVAL);
1408
Saeed Mahameed938fe832015-05-28 22:28:41 +03001409 resp.qp_tab_size = 1 << MLX5_CAP_GEN(dev->mdev, log_max_qp);
Noa Osherovich2cc6ad52016-06-04 15:15:33 +03001410 if (mlx5_core_is_pf(dev->mdev) && MLX5_CAP_GEN(dev->mdev, bf))
1411 resp.bf_reg_size = 1 << MLX5_CAP_GEN(dev->mdev, log_bf_reg_size);
Daniel Jurgensb47bd6e2016-10-25 18:36:24 +03001412 resp.cache_line_size = cache_line_size();
Saeed Mahameed938fe832015-05-28 22:28:41 +03001413 resp.max_sq_desc_sz = MLX5_CAP_GEN(dev->mdev, max_wqe_sz_sq);
1414 resp.max_rq_desc_sz = MLX5_CAP_GEN(dev->mdev, max_wqe_sz_rq);
1415 resp.max_send_wqebb = 1 << MLX5_CAP_GEN(dev->mdev, log_max_qp_sz);
1416 resp.max_recv_wr = 1 << MLX5_CAP_GEN(dev->mdev, log_max_qp_sz);
1417 resp.max_srq_recv_wr = 1 << MLX5_CAP_GEN(dev->mdev, log_max_srq_sz);
Haggai Abramovskyf72300c2016-01-14 19:12:58 +02001418 resp.cqe_version = min_t(__u8,
1419 (__u8)MLX5_CAP_GEN(dev->mdev, cqe_version),
1420 req.max_cqe_version);
Eli Cohen30aa60b2017-01-03 23:55:27 +02001421 resp.log_uar_size = MLX5_CAP_GEN(dev->mdev, uar_4k) ?
1422 MLX5_ADAPTER_PAGE_SHIFT : PAGE_SHIFT;
1423 resp.num_uars_per_page = MLX5_CAP_GEN(dev->mdev, uar_4k) ?
1424 MLX5_CAP_GEN(dev->mdev, num_of_uars_per_page) : 1;
Matan Barakb368d7c2015-12-15 20:30:12 +02001425 resp.response_length = min(offsetof(typeof(resp), response_length) +
1426 sizeof(resp.response_length), udata->outlen);
Eli Cohene126ba92013-07-07 17:25:49 +03001427
1428 context = kzalloc(sizeof(*context), GFP_KERNEL);
1429 if (!context)
1430 return ERR_PTR(-ENOMEM);
1431
Eli Cohen30aa60b2017-01-03 23:55:27 +02001432 lib_uar_4k = req.lib_caps & MLX5_LIB_CAP_4K_UAR;
Eli Cohen2f5ff262017-01-03 23:55:21 +02001433 bfregi = &context->bfregi;
Eli Cohenb037c292017-01-03 23:55:26 +02001434
1435 /* updates req->total_num_bfregs */
Yishai Hadas31a78a52017-12-24 16:31:34 +02001436 err = calc_total_bfregs(dev, lib_uar_4k, &req, bfregi);
Eli Cohenb037c292017-01-03 23:55:26 +02001437 if (err)
1438 goto out_ctx;
1439
Eli Cohen2f5ff262017-01-03 23:55:21 +02001440 mutex_init(&bfregi->lock);
Eli Cohenb037c292017-01-03 23:55:26 +02001441 bfregi->lib_uar_4k = lib_uar_4k;
Yishai Hadas31a78a52017-12-24 16:31:34 +02001442 bfregi->count = kcalloc(bfregi->total_num_bfregs, sizeof(*bfregi->count),
Eli Cohenb037c292017-01-03 23:55:26 +02001443 GFP_KERNEL);
1444 if (!bfregi->count) {
Eli Cohene126ba92013-07-07 17:25:49 +03001445 err = -ENOMEM;
1446 goto out_ctx;
1447 }
1448
Eli Cohenb037c292017-01-03 23:55:26 +02001449 bfregi->sys_pages = kcalloc(bfregi->num_sys_pages,
1450 sizeof(*bfregi->sys_pages),
1451 GFP_KERNEL);
1452 if (!bfregi->sys_pages) {
Eli Cohene126ba92013-07-07 17:25:49 +03001453 err = -ENOMEM;
Eli Cohenb037c292017-01-03 23:55:26 +02001454 goto out_count;
Eli Cohene126ba92013-07-07 17:25:49 +03001455 }
1456
Eli Cohenb037c292017-01-03 23:55:26 +02001457 err = allocate_uars(dev, context);
1458 if (err)
1459 goto out_sys_pages;
Eli Cohene126ba92013-07-07 17:25:49 +03001460
Haggai Eranb4cfe442014-12-11 17:04:26 +02001461#ifdef CONFIG_INFINIBAND_ON_DEMAND_PAGING
1462 context->ibucontext.invalidate_range = &mlx5_ib_invalidate_range;
1463#endif
1464
Artemy Kovalyov7d0cc6e2017-01-02 11:37:44 +02001465 context->upd_xlt_page = __get_free_page(GFP_KERNEL);
1466 if (!context->upd_xlt_page) {
1467 err = -ENOMEM;
1468 goto out_uars;
1469 }
1470 mutex_init(&context->upd_xlt_page_mutex);
1471
majd@mellanox.com146d2f12016-01-14 19:13:02 +02001472 if (MLX5_CAP_GEN(dev->mdev, log_max_transport_domain)) {
Huy Nguyenc85023e2017-05-30 09:42:54 +03001473 err = mlx5_ib_alloc_transport_domain(dev, &context->tdn);
majd@mellanox.com146d2f12016-01-14 19:13:02 +02001474 if (err)
Artemy Kovalyov7d0cc6e2017-01-02 11:37:44 +02001475 goto out_page;
majd@mellanox.com146d2f12016-01-14 19:13:02 +02001476 }
1477
Maor Gottlieb7c2344c2016-06-17 14:56:44 +03001478 INIT_LIST_HEAD(&context->vma_private_list);
Majd Dibbinyad9a3662017-12-24 13:54:56 +02001479 mutex_init(&context->vma_private_list_mutex);
Eli Cohene126ba92013-07-07 17:25:49 +03001480 INIT_LIST_HEAD(&context->db_page_list);
1481 mutex_init(&context->db_page_mutex);
1482
Eli Cohen2f5ff262017-01-03 23:55:21 +02001483 resp.tot_bfregs = req.total_num_bfregs;
Saeed Mahameed938fe832015-05-28 22:28:41 +03001484 resp.num_ports = MLX5_CAP_GEN(dev->mdev, num_ports);
Matan Barakb368d7c2015-12-15 20:30:12 +02001485
Haggai Abramovskyf72300c2016-01-14 19:12:58 +02001486 if (field_avail(typeof(resp), cqe_version, udata->outlen))
1487 resp.response_length += sizeof(resp.cqe_version);
Matan Barakb368d7c2015-12-15 20:30:12 +02001488
Bodong Wang402ca532016-06-17 15:02:20 +03001489 if (field_avail(typeof(resp), cmds_supp_uhw, udata->outlen)) {
Moni Shoua6ad279c52016-11-23 08:23:23 +02001490 resp.cmds_supp_uhw |= MLX5_USER_CMDS_SUPP_UHW_QUERY_DEVICE |
1491 MLX5_USER_CMDS_SUPP_UHW_CREATE_AH;
Bodong Wang402ca532016-06-17 15:02:20 +03001492 resp.response_length += sizeof(resp.cmds_supp_uhw);
1493 }
1494
Or Gerlitz78984892016-11-30 20:33:33 +02001495 if (field_avail(typeof(resp), eth_min_inline, udata->outlen)) {
1496 if (mlx5_ib_port_link_layer(ibdev, 1) == IB_LINK_LAYER_ETHERNET) {
1497 mlx5_query_min_inline(dev->mdev, &resp.eth_min_inline);
1498 resp.eth_min_inline++;
1499 }
1500 resp.response_length += sizeof(resp.eth_min_inline);
1501 }
1502
Noa Osherovichbc5c6ee2016-06-04 15:15:31 +03001503 /*
1504 * We don't want to expose information from the PCI bar that is located
1505 * after 4096 bytes, so if the arch only supports larger pages, let's
1506 * pretend we don't support reading the HCA's core clock. This is also
1507 * forced by mmap function.
1508 */
Eli Cohende8d6e02017-01-03 23:55:19 +02001509 if (field_avail(typeof(resp), hca_core_clock_offset, udata->outlen)) {
1510 if (PAGE_SIZE <= 4096) {
1511 resp.comp_mask |=
1512 MLX5_IB_ALLOC_UCONTEXT_RESP_MASK_CORE_CLOCK_OFFSET;
1513 resp.hca_core_clock_offset =
1514 offsetof(struct mlx5_init_seg, internal_timer_h) % PAGE_SIZE;
1515 }
Haggai Abramovskyf72300c2016-01-14 19:12:58 +02001516 resp.response_length += sizeof(resp.hca_core_clock_offset) +
Bodong Wang402ca532016-06-17 15:02:20 +03001517 sizeof(resp.reserved2);
Matan Barakb368d7c2015-12-15 20:30:12 +02001518 }
1519
Eli Cohen30aa60b2017-01-03 23:55:27 +02001520 if (field_avail(typeof(resp), log_uar_size, udata->outlen))
1521 resp.response_length += sizeof(resp.log_uar_size);
1522
1523 if (field_avail(typeof(resp), num_uars_per_page, udata->outlen))
1524 resp.response_length += sizeof(resp.num_uars_per_page);
1525
Yishai Hadas31a78a52017-12-24 16:31:34 +02001526 if (field_avail(typeof(resp), num_dyn_bfregs, udata->outlen)) {
1527 resp.num_dyn_bfregs = bfregi->num_dyn_bfregs;
1528 resp.response_length += sizeof(resp.num_dyn_bfregs);
1529 }
1530
Matan Barakb368d7c2015-12-15 20:30:12 +02001531 err = ib_copy_to_udata(udata, &resp, resp.response_length);
Eli Cohene126ba92013-07-07 17:25:49 +03001532 if (err)
majd@mellanox.com146d2f12016-01-14 19:13:02 +02001533 goto out_td;
Eli Cohene126ba92013-07-07 17:25:49 +03001534
Eli Cohen2f5ff262017-01-03 23:55:21 +02001535 bfregi->ver = ver;
1536 bfregi->num_low_latency_bfregs = req.num_low_latency_bfregs;
Haggai Abramovskyf72300c2016-01-14 19:12:58 +02001537 context->cqe_version = resp.cqe_version;
Eli Cohen30aa60b2017-01-03 23:55:27 +02001538 context->lib_caps = req.lib_caps;
1539 print_lib_caps(dev, context->lib_caps);
Haggai Abramovskyf72300c2016-01-14 19:12:58 +02001540
Eli Cohene126ba92013-07-07 17:25:49 +03001541 return &context->ibucontext;
1542
majd@mellanox.com146d2f12016-01-14 19:13:02 +02001543out_td:
1544 if (MLX5_CAP_GEN(dev->mdev, log_max_transport_domain))
Huy Nguyenc85023e2017-05-30 09:42:54 +03001545 mlx5_ib_dealloc_transport_domain(dev, context->tdn);
majd@mellanox.com146d2f12016-01-14 19:13:02 +02001546
Artemy Kovalyov7d0cc6e2017-01-02 11:37:44 +02001547out_page:
1548 free_page(context->upd_xlt_page);
1549
Eli Cohene126ba92013-07-07 17:25:49 +03001550out_uars:
Eli Cohenb037c292017-01-03 23:55:26 +02001551 deallocate_uars(dev, context);
1552
1553out_sys_pages:
1554 kfree(bfregi->sys_pages);
1555
Eli Cohene126ba92013-07-07 17:25:49 +03001556out_count:
Eli Cohen2f5ff262017-01-03 23:55:21 +02001557 kfree(bfregi->count);
Eli Cohene126ba92013-07-07 17:25:49 +03001558
Eli Cohene126ba92013-07-07 17:25:49 +03001559out_ctx:
1560 kfree(context);
Eli Cohenb037c292017-01-03 23:55:26 +02001561
Eli Cohene126ba92013-07-07 17:25:49 +03001562 return ERR_PTR(err);
1563}
1564
1565static int mlx5_ib_dealloc_ucontext(struct ib_ucontext *ibcontext)
1566{
1567 struct mlx5_ib_ucontext *context = to_mucontext(ibcontext);
1568 struct mlx5_ib_dev *dev = to_mdev(ibcontext->device);
Eli Cohenb037c292017-01-03 23:55:26 +02001569 struct mlx5_bfreg_info *bfregi;
Eli Cohene126ba92013-07-07 17:25:49 +03001570
Eli Cohenb037c292017-01-03 23:55:26 +02001571 bfregi = &context->bfregi;
majd@mellanox.com146d2f12016-01-14 19:13:02 +02001572 if (MLX5_CAP_GEN(dev->mdev, log_max_transport_domain))
Huy Nguyenc85023e2017-05-30 09:42:54 +03001573 mlx5_ib_dealloc_transport_domain(dev, context->tdn);
majd@mellanox.com146d2f12016-01-14 19:13:02 +02001574
Artemy Kovalyov7d0cc6e2017-01-02 11:37:44 +02001575 free_page(context->upd_xlt_page);
Eli Cohenb037c292017-01-03 23:55:26 +02001576 deallocate_uars(dev, context);
1577 kfree(bfregi->sys_pages);
Eli Cohen2f5ff262017-01-03 23:55:21 +02001578 kfree(bfregi->count);
Eli Cohene126ba92013-07-07 17:25:49 +03001579 kfree(context);
1580
1581 return 0;
1582}
1583
Eli Cohenb037c292017-01-03 23:55:26 +02001584static phys_addr_t uar_index2pfn(struct mlx5_ib_dev *dev,
1585 struct mlx5_bfreg_info *bfregi,
1586 int idx)
Eli Cohene126ba92013-07-07 17:25:49 +03001587{
Eli Cohenb037c292017-01-03 23:55:26 +02001588 int fw_uars_per_page;
1589
1590 fw_uars_per_page = MLX5_CAP_GEN(dev->mdev, uar_4k) ? MLX5_UARS_IN_PAGE : 1;
1591
1592 return (pci_resource_start(dev->mdev->pdev, 0) >> PAGE_SHIFT) +
1593 bfregi->sys_pages[idx] / fw_uars_per_page;
Eli Cohene126ba92013-07-07 17:25:49 +03001594}
1595
1596static int get_command(unsigned long offset)
1597{
1598 return (offset >> MLX5_IB_MMAP_CMD_SHIFT) & MLX5_IB_MMAP_CMD_MASK;
1599}
1600
1601static int get_arg(unsigned long offset)
1602{
1603 return offset & ((1 << MLX5_IB_MMAP_CMD_SHIFT) - 1);
1604}
1605
1606static int get_index(unsigned long offset)
1607{
1608 return get_arg(offset);
1609}
1610
Maor Gottlieb7c2344c2016-06-17 14:56:44 +03001611static void mlx5_ib_vma_open(struct vm_area_struct *area)
1612{
1613 /* vma_open is called when a new VMA is created on top of our VMA. This
1614 * is done through either mremap flow or split_vma (usually due to
1615 * mlock, madvise, munmap, etc.) We do not support a clone of the VMA,
1616 * as this VMA is strongly hardware related. Therefore we set the
1617 * vm_ops of the newly created/cloned VMA to NULL, to prevent it from
1618 * calling us again and trying to do incorrect actions. We assume that
1619 * the original VMA size is exactly a single page, and therefore all
1620 * "splitting" operation will not happen to it.
1621 */
1622 area->vm_ops = NULL;
1623}
1624
1625static void mlx5_ib_vma_close(struct vm_area_struct *area)
1626{
1627 struct mlx5_ib_vma_private_data *mlx5_ib_vma_priv_data;
1628
1629 /* It's guaranteed that all VMAs opened on a FD are closed before the
1630 * file itself is closed, therefore no sync is needed with the regular
1631 * closing flow. (e.g. mlx5 ib_dealloc_ucontext)
1632 * However need a sync with accessing the vma as part of
1633 * mlx5_ib_disassociate_ucontext.
1634 * The close operation is usually called under mm->mmap_sem except when
1635 * process is exiting.
1636 * The exiting case is handled explicitly as part of
1637 * mlx5_ib_disassociate_ucontext.
1638 */
1639 mlx5_ib_vma_priv_data = (struct mlx5_ib_vma_private_data *)area->vm_private_data;
1640
1641 /* setting the vma context pointer to null in the mlx5_ib driver's
1642 * private data, to protect a race condition in
1643 * mlx5_ib_disassociate_ucontext().
1644 */
1645 mlx5_ib_vma_priv_data->vma = NULL;
Majd Dibbinyad9a3662017-12-24 13:54:56 +02001646 mutex_lock(mlx5_ib_vma_priv_data->vma_private_list_mutex);
Maor Gottlieb7c2344c2016-06-17 14:56:44 +03001647 list_del(&mlx5_ib_vma_priv_data->list);
Majd Dibbinyad9a3662017-12-24 13:54:56 +02001648 mutex_unlock(mlx5_ib_vma_priv_data->vma_private_list_mutex);
Maor Gottlieb7c2344c2016-06-17 14:56:44 +03001649 kfree(mlx5_ib_vma_priv_data);
1650}
1651
1652static const struct vm_operations_struct mlx5_ib_vm_ops = {
1653 .open = mlx5_ib_vma_open,
1654 .close = mlx5_ib_vma_close
1655};
1656
1657static int mlx5_ib_set_vma_data(struct vm_area_struct *vma,
1658 struct mlx5_ib_ucontext *ctx)
1659{
1660 struct mlx5_ib_vma_private_data *vma_prv;
1661 struct list_head *vma_head = &ctx->vma_private_list;
1662
1663 vma_prv = kzalloc(sizeof(*vma_prv), GFP_KERNEL);
1664 if (!vma_prv)
1665 return -ENOMEM;
1666
1667 vma_prv->vma = vma;
Majd Dibbinyad9a3662017-12-24 13:54:56 +02001668 vma_prv->vma_private_list_mutex = &ctx->vma_private_list_mutex;
Maor Gottlieb7c2344c2016-06-17 14:56:44 +03001669 vma->vm_private_data = vma_prv;
1670 vma->vm_ops = &mlx5_ib_vm_ops;
1671
Majd Dibbinyad9a3662017-12-24 13:54:56 +02001672 mutex_lock(&ctx->vma_private_list_mutex);
Maor Gottlieb7c2344c2016-06-17 14:56:44 +03001673 list_add(&vma_prv->list, vma_head);
Majd Dibbinyad9a3662017-12-24 13:54:56 +02001674 mutex_unlock(&ctx->vma_private_list_mutex);
Maor Gottlieb7c2344c2016-06-17 14:56:44 +03001675
1676 return 0;
1677}
1678
1679static void mlx5_ib_disassociate_ucontext(struct ib_ucontext *ibcontext)
1680{
1681 int ret;
1682 struct vm_area_struct *vma;
1683 struct mlx5_ib_vma_private_data *vma_private, *n;
1684 struct mlx5_ib_ucontext *context = to_mucontext(ibcontext);
1685 struct task_struct *owning_process = NULL;
1686 struct mm_struct *owning_mm = NULL;
1687
1688 owning_process = get_pid_task(ibcontext->tgid, PIDTYPE_PID);
1689 if (!owning_process)
1690 return;
1691
1692 owning_mm = get_task_mm(owning_process);
1693 if (!owning_mm) {
1694 pr_info("no mm, disassociate ucontext is pending task termination\n");
1695 while (1) {
1696 put_task_struct(owning_process);
1697 usleep_range(1000, 2000);
1698 owning_process = get_pid_task(ibcontext->tgid,
1699 PIDTYPE_PID);
1700 if (!owning_process ||
1701 owning_process->state == TASK_DEAD) {
1702 pr_info("disassociate ucontext done, task was terminated\n");
1703 /* in case task was dead need to release the
1704 * task struct.
1705 */
1706 if (owning_process)
1707 put_task_struct(owning_process);
1708 return;
1709 }
1710 }
1711 }
1712
1713 /* need to protect from a race on closing the vma as part of
1714 * mlx5_ib_vma_close.
1715 */
Maor Gottliebecc7d832017-03-29 06:03:02 +03001716 down_write(&owning_mm->mmap_sem);
Majd Dibbinyad9a3662017-12-24 13:54:56 +02001717 mutex_lock(&context->vma_private_list_mutex);
Maor Gottlieb7c2344c2016-06-17 14:56:44 +03001718 list_for_each_entry_safe(vma_private, n, &context->vma_private_list,
1719 list) {
1720 vma = vma_private->vma;
1721 ret = zap_vma_ptes(vma, vma->vm_start,
1722 PAGE_SIZE);
1723 WARN_ONCE(ret, "%s: zap_vma_ptes failed", __func__);
1724 /* context going to be destroyed, should
1725 * not access ops any more.
1726 */
Maor Gottlieb13776612017-03-29 06:03:03 +03001727 vma->vm_flags &= ~(VM_SHARED | VM_MAYSHARE);
Maor Gottlieb7c2344c2016-06-17 14:56:44 +03001728 vma->vm_ops = NULL;
1729 list_del(&vma_private->list);
1730 kfree(vma_private);
1731 }
Majd Dibbinyad9a3662017-12-24 13:54:56 +02001732 mutex_unlock(&context->vma_private_list_mutex);
Maor Gottliebecc7d832017-03-29 06:03:02 +03001733 up_write(&owning_mm->mmap_sem);
Maor Gottlieb7c2344c2016-06-17 14:56:44 +03001734 mmput(owning_mm);
1735 put_task_struct(owning_process);
1736}
1737
Guy Levi37aa5c32016-04-27 16:49:50 +03001738static inline char *mmap_cmd2str(enum mlx5_ib_mmap_cmd cmd)
1739{
1740 switch (cmd) {
1741 case MLX5_IB_MMAP_WC_PAGE:
1742 return "WC";
1743 case MLX5_IB_MMAP_REGULAR_PAGE:
1744 return "best effort WC";
1745 case MLX5_IB_MMAP_NC_PAGE:
1746 return "NC";
1747 default:
1748 return NULL;
1749 }
1750}
1751
1752static int uar_mmap(struct mlx5_ib_dev *dev, enum mlx5_ib_mmap_cmd cmd,
Maor Gottlieb7c2344c2016-06-17 14:56:44 +03001753 struct vm_area_struct *vma,
1754 struct mlx5_ib_ucontext *context)
Guy Levi37aa5c32016-04-27 16:49:50 +03001755{
Eli Cohen2f5ff262017-01-03 23:55:21 +02001756 struct mlx5_bfreg_info *bfregi = &context->bfregi;
Guy Levi37aa5c32016-04-27 16:49:50 +03001757 int err;
1758 unsigned long idx;
1759 phys_addr_t pfn, pa;
1760 pgprot_t prot;
Eli Cohenb037c292017-01-03 23:55:26 +02001761 int uars_per_page;
1762
1763 if (vma->vm_end - vma->vm_start != PAGE_SIZE)
1764 return -EINVAL;
1765
1766 uars_per_page = get_uars_per_sys_page(dev, bfregi->lib_uar_4k);
1767 idx = get_index(vma->vm_pgoff);
1768 if (idx % uars_per_page ||
1769 idx * uars_per_page >= bfregi->num_sys_pages) {
1770 mlx5_ib_warn(dev, "invalid uar index %lu\n", idx);
1771 return -EINVAL;
1772 }
Guy Levi37aa5c32016-04-27 16:49:50 +03001773
1774 switch (cmd) {
1775 case MLX5_IB_MMAP_WC_PAGE:
1776/* Some architectures don't support WC memory */
1777#if defined(CONFIG_X86)
1778 if (!pat_enabled())
1779 return -EPERM;
1780#elif !(defined(CONFIG_PPC) || (defined(CONFIG_ARM) && defined(CONFIG_MMU)))
1781 return -EPERM;
1782#endif
1783 /* fall through */
1784 case MLX5_IB_MMAP_REGULAR_PAGE:
1785 /* For MLX5_IB_MMAP_REGULAR_PAGE do the best effort to get WC */
1786 prot = pgprot_writecombine(vma->vm_page_prot);
1787 break;
1788 case MLX5_IB_MMAP_NC_PAGE:
1789 prot = pgprot_noncached(vma->vm_page_prot);
1790 break;
1791 default:
1792 return -EINVAL;
1793 }
1794
Eli Cohenb037c292017-01-03 23:55:26 +02001795 pfn = uar_index2pfn(dev, bfregi, idx);
Guy Levi37aa5c32016-04-27 16:49:50 +03001796 mlx5_ib_dbg(dev, "uar idx 0x%lx, pfn %pa\n", idx, &pfn);
1797
1798 vma->vm_page_prot = prot;
1799 err = io_remap_pfn_range(vma, vma->vm_start, pfn,
1800 PAGE_SIZE, vma->vm_page_prot);
1801 if (err) {
1802 mlx5_ib_err(dev, "io_remap_pfn_range failed with error=%d, vm_start=0x%lx, pfn=%pa, mmap_cmd=%s\n",
1803 err, vma->vm_start, &pfn, mmap_cmd2str(cmd));
1804 return -EAGAIN;
1805 }
1806
1807 pa = pfn << PAGE_SHIFT;
1808 mlx5_ib_dbg(dev, "mapped %s at 0x%lx, PA %pa\n", mmap_cmd2str(cmd),
1809 vma->vm_start, &pa);
1810
Maor Gottlieb7c2344c2016-06-17 14:56:44 +03001811 return mlx5_ib_set_vma_data(vma, context);
Guy Levi37aa5c32016-04-27 16:49:50 +03001812}
1813
Eli Cohene126ba92013-07-07 17:25:49 +03001814static int mlx5_ib_mmap(struct ib_ucontext *ibcontext, struct vm_area_struct *vma)
1815{
1816 struct mlx5_ib_ucontext *context = to_mucontext(ibcontext);
1817 struct mlx5_ib_dev *dev = to_mdev(ibcontext->device);
Eli Cohene126ba92013-07-07 17:25:49 +03001818 unsigned long command;
Eli Cohene126ba92013-07-07 17:25:49 +03001819 phys_addr_t pfn;
1820
1821 command = get_command(vma->vm_pgoff);
1822 switch (command) {
Guy Levi37aa5c32016-04-27 16:49:50 +03001823 case MLX5_IB_MMAP_WC_PAGE:
1824 case MLX5_IB_MMAP_NC_PAGE:
Eli Cohene126ba92013-07-07 17:25:49 +03001825 case MLX5_IB_MMAP_REGULAR_PAGE:
Maor Gottlieb7c2344c2016-06-17 14:56:44 +03001826 return uar_mmap(dev, command, vma, context);
Eli Cohene126ba92013-07-07 17:25:49 +03001827
1828 case MLX5_IB_MMAP_GET_CONTIGUOUS_PAGES:
1829 return -ENOSYS;
1830
Matan Barakd69e3bc2015-12-15 20:30:13 +02001831 case MLX5_IB_MMAP_CORE_CLOCK:
Matan Barakd69e3bc2015-12-15 20:30:13 +02001832 if (vma->vm_end - vma->vm_start != PAGE_SIZE)
1833 return -EINVAL;
1834
Matan Barak6cbac1e2016-04-14 16:52:10 +03001835 if (vma->vm_flags & VM_WRITE)
Matan Barakd69e3bc2015-12-15 20:30:13 +02001836 return -EPERM;
1837
1838 /* Don't expose to user-space information it shouldn't have */
1839 if (PAGE_SIZE > 4096)
1840 return -EOPNOTSUPP;
1841
1842 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
1843 pfn = (dev->mdev->iseg_base +
1844 offsetof(struct mlx5_init_seg, internal_timer_h)) >>
1845 PAGE_SHIFT;
1846 if (io_remap_pfn_range(vma, vma->vm_start, pfn,
1847 PAGE_SIZE, vma->vm_page_prot))
1848 return -EAGAIN;
1849
1850 mlx5_ib_dbg(dev, "mapped internal timer at 0x%lx, PA 0x%llx\n",
1851 vma->vm_start,
1852 (unsigned long long)pfn << PAGE_SHIFT);
1853 break;
Matan Barakd69e3bc2015-12-15 20:30:13 +02001854
Eli Cohene126ba92013-07-07 17:25:49 +03001855 default:
1856 return -EINVAL;
1857 }
1858
1859 return 0;
1860}
1861
Eli Cohene126ba92013-07-07 17:25:49 +03001862static struct ib_pd *mlx5_ib_alloc_pd(struct ib_device *ibdev,
1863 struct ib_ucontext *context,
1864 struct ib_udata *udata)
1865{
1866 struct mlx5_ib_alloc_pd_resp resp;
1867 struct mlx5_ib_pd *pd;
1868 int err;
1869
1870 pd = kmalloc(sizeof(*pd), GFP_KERNEL);
1871 if (!pd)
1872 return ERR_PTR(-ENOMEM);
1873
Jack Morgenstein9603b612014-07-28 23:30:22 +03001874 err = mlx5_core_alloc_pd(to_mdev(ibdev)->mdev, &pd->pdn);
Eli Cohene126ba92013-07-07 17:25:49 +03001875 if (err) {
1876 kfree(pd);
1877 return ERR_PTR(err);
1878 }
1879
1880 if (context) {
1881 resp.pdn = pd->pdn;
1882 if (ib_copy_to_udata(udata, &resp, sizeof(resp))) {
Jack Morgenstein9603b612014-07-28 23:30:22 +03001883 mlx5_core_dealloc_pd(to_mdev(ibdev)->mdev, pd->pdn);
Eli Cohene126ba92013-07-07 17:25:49 +03001884 kfree(pd);
1885 return ERR_PTR(-EFAULT);
1886 }
Eli Cohene126ba92013-07-07 17:25:49 +03001887 }
1888
1889 return &pd->ibpd;
1890}
1891
1892static int mlx5_ib_dealloc_pd(struct ib_pd *pd)
1893{
1894 struct mlx5_ib_dev *mdev = to_mdev(pd->device);
1895 struct mlx5_ib_pd *mpd = to_mpd(pd);
1896
Jack Morgenstein9603b612014-07-28 23:30:22 +03001897 mlx5_core_dealloc_pd(mdev->mdev, mpd->pdn);
Eli Cohene126ba92013-07-07 17:25:49 +03001898 kfree(mpd);
1899
1900 return 0;
1901}
1902
Maor Gottlieb466fa6d2016-08-30 16:58:36 +03001903enum {
1904 MATCH_CRITERIA_ENABLE_OUTER_BIT,
1905 MATCH_CRITERIA_ENABLE_MISC_BIT,
1906 MATCH_CRITERIA_ENABLE_INNER_BIT
1907};
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02001908
Maor Gottlieb466fa6d2016-08-30 16:58:36 +03001909#define HEADER_IS_ZERO(match_criteria, headers) \
1910 !(memchr_inv(MLX5_ADDR_OF(fte_match_param, match_criteria, headers), \
1911 0, MLX5_FLD_SZ_BYTES(fte_match_param, headers))) \
1912
1913static u8 get_match_criteria_enable(u32 *match_criteria)
1914{
1915 u8 match_criteria_enable;
1916
1917 match_criteria_enable =
1918 (!HEADER_IS_ZERO(match_criteria, outer_headers)) <<
1919 MATCH_CRITERIA_ENABLE_OUTER_BIT;
1920 match_criteria_enable |=
1921 (!HEADER_IS_ZERO(match_criteria, misc_parameters)) <<
1922 MATCH_CRITERIA_ENABLE_MISC_BIT;
1923 match_criteria_enable |=
1924 (!HEADER_IS_ZERO(match_criteria, inner_headers)) <<
1925 MATCH_CRITERIA_ENABLE_INNER_BIT;
1926
1927 return match_criteria_enable;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02001928}
1929
Maor Gottliebca0d4752016-08-30 16:58:35 +03001930static void set_proto(void *outer_c, void *outer_v, u8 mask, u8 val)
1931{
1932 MLX5_SET(fte_match_set_lyr_2_4, outer_c, ip_protocol, mask);
1933 MLX5_SET(fte_match_set_lyr_2_4, outer_v, ip_protocol, val);
1934}
1935
Moses Reuben2d1e6972016-11-14 19:04:52 +02001936static void set_flow_label(void *misc_c, void *misc_v, u8 mask, u8 val,
1937 bool inner)
1938{
1939 if (inner) {
1940 MLX5_SET(fte_match_set_misc,
1941 misc_c, inner_ipv6_flow_label, mask);
1942 MLX5_SET(fte_match_set_misc,
1943 misc_v, inner_ipv6_flow_label, val);
1944 } else {
1945 MLX5_SET(fte_match_set_misc,
1946 misc_c, outer_ipv6_flow_label, mask);
1947 MLX5_SET(fte_match_set_misc,
1948 misc_v, outer_ipv6_flow_label, val);
1949 }
1950}
1951
Maor Gottliebca0d4752016-08-30 16:58:35 +03001952static void set_tos(void *outer_c, void *outer_v, u8 mask, u8 val)
1953{
1954 MLX5_SET(fte_match_set_lyr_2_4, outer_c, ip_ecn, mask);
1955 MLX5_SET(fte_match_set_lyr_2_4, outer_v, ip_ecn, val);
1956 MLX5_SET(fte_match_set_lyr_2_4, outer_c, ip_dscp, mask >> 2);
1957 MLX5_SET(fte_match_set_lyr_2_4, outer_v, ip_dscp, val >> 2);
1958}
1959
Maor Gottliebc47ac6a2016-08-30 16:58:31 +03001960#define LAST_ETH_FIELD vlan_tag
1961#define LAST_IB_FIELD sl
Maor Gottliebca0d4752016-08-30 16:58:35 +03001962#define LAST_IPV4_FIELD tos
Maor Gottlieb466fa6d2016-08-30 16:58:36 +03001963#define LAST_IPV6_FIELD traffic_class
Maor Gottliebc47ac6a2016-08-30 16:58:31 +03001964#define LAST_TCP_UDP_FIELD src_port
Moses Reubenffb30d82016-11-14 19:04:50 +02001965#define LAST_TUNNEL_FIELD tunnel_id
Moses Reuben2ac693f2017-01-18 14:59:50 +02001966#define LAST_FLOW_TAG_FIELD tag_id
Slava Shwartsmana22ed862017-04-03 13:13:52 +03001967#define LAST_DROP_FIELD size
Maor Gottliebc47ac6a2016-08-30 16:58:31 +03001968
1969/* Field is the last supported field */
1970#define FIELDS_NOT_SUPPORTED(filter, field)\
1971 memchr_inv((void *)&filter.field +\
1972 sizeof(filter.field), 0,\
1973 sizeof(filter) -\
1974 offsetof(typeof(filter), field) -\
1975 sizeof(filter.field))
1976
Ariel Levkovich19cc7522017-04-03 13:11:03 +03001977#define IPV4_VERSION 4
1978#define IPV6_VERSION 6
1979static int parse_flow_attr(struct mlx5_core_dev *mdev, u32 *match_c,
1980 u32 *match_v, const union ib_flow_spec *ib_spec,
Slava Shwartsmana22ed862017-04-03 13:13:52 +03001981 u32 *tag_id, bool *is_drop)
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02001982{
Maor Gottlieb466fa6d2016-08-30 16:58:36 +03001983 void *misc_params_c = MLX5_ADDR_OF(fte_match_param, match_c,
1984 misc_parameters);
1985 void *misc_params_v = MLX5_ADDR_OF(fte_match_param, match_v,
1986 misc_parameters);
Moses Reuben2d1e6972016-11-14 19:04:52 +02001987 void *headers_c;
1988 void *headers_v;
Ariel Levkovich19cc7522017-04-03 13:11:03 +03001989 int match_ipv;
Maor Gottlieb466fa6d2016-08-30 16:58:36 +03001990
Moses Reuben2d1e6972016-11-14 19:04:52 +02001991 if (ib_spec->type & IB_FLOW_SPEC_INNER) {
1992 headers_c = MLX5_ADDR_OF(fte_match_param, match_c,
1993 inner_headers);
1994 headers_v = MLX5_ADDR_OF(fte_match_param, match_v,
1995 inner_headers);
Ariel Levkovich19cc7522017-04-03 13:11:03 +03001996 match_ipv = MLX5_CAP_FLOWTABLE_NIC_RX(mdev,
1997 ft_field_support.inner_ip_version);
Moses Reuben2d1e6972016-11-14 19:04:52 +02001998 } else {
1999 headers_c = MLX5_ADDR_OF(fte_match_param, match_c,
2000 outer_headers);
2001 headers_v = MLX5_ADDR_OF(fte_match_param, match_v,
2002 outer_headers);
Ariel Levkovich19cc7522017-04-03 13:11:03 +03002003 match_ipv = MLX5_CAP_FLOWTABLE_NIC_RX(mdev,
2004 ft_field_support.outer_ip_version);
Moses Reuben2d1e6972016-11-14 19:04:52 +02002005 }
2006
2007 switch (ib_spec->type & ~IB_FLOW_SPEC_INNER) {
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002008 case IB_FLOW_SPEC_ETH:
Maor Gottliebc47ac6a2016-08-30 16:58:31 +03002009 if (FIELDS_NOT_SUPPORTED(ib_spec->eth.mask, LAST_ETH_FIELD))
Leon Romanovsky1ffd3a22017-01-18 14:59:51 +02002010 return -EOPNOTSUPP;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002011
Moses Reuben2d1e6972016-11-14 19:04:52 +02002012 ether_addr_copy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002013 dmac_47_16),
2014 ib_spec->eth.mask.dst_mac);
Moses Reuben2d1e6972016-11-14 19:04:52 +02002015 ether_addr_copy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002016 dmac_47_16),
2017 ib_spec->eth.val.dst_mac);
2018
Moses Reuben2d1e6972016-11-14 19:04:52 +02002019 ether_addr_copy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
Maor Gottliebee3da802016-09-12 19:16:24 +03002020 smac_47_16),
2021 ib_spec->eth.mask.src_mac);
Moses Reuben2d1e6972016-11-14 19:04:52 +02002022 ether_addr_copy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
Maor Gottliebee3da802016-09-12 19:16:24 +03002023 smac_47_16),
2024 ib_spec->eth.val.src_mac);
2025
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002026 if (ib_spec->eth.mask.vlan_tag) {
Moses Reuben2d1e6972016-11-14 19:04:52 +02002027 MLX5_SET(fte_match_set_lyr_2_4, headers_c,
Mohamad Haj Yahia10543362016-10-09 16:25:43 +03002028 cvlan_tag, 1);
Moses Reuben2d1e6972016-11-14 19:04:52 +02002029 MLX5_SET(fte_match_set_lyr_2_4, headers_v,
Mohamad Haj Yahia10543362016-10-09 16:25:43 +03002030 cvlan_tag, 1);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002031
Moses Reuben2d1e6972016-11-14 19:04:52 +02002032 MLX5_SET(fte_match_set_lyr_2_4, headers_c,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002033 first_vid, ntohs(ib_spec->eth.mask.vlan_tag));
Moses Reuben2d1e6972016-11-14 19:04:52 +02002034 MLX5_SET(fte_match_set_lyr_2_4, headers_v,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002035 first_vid, ntohs(ib_spec->eth.val.vlan_tag));
2036
Moses Reuben2d1e6972016-11-14 19:04:52 +02002037 MLX5_SET(fte_match_set_lyr_2_4, headers_c,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002038 first_cfi,
2039 ntohs(ib_spec->eth.mask.vlan_tag) >> 12);
Moses Reuben2d1e6972016-11-14 19:04:52 +02002040 MLX5_SET(fte_match_set_lyr_2_4, headers_v,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002041 first_cfi,
2042 ntohs(ib_spec->eth.val.vlan_tag) >> 12);
2043
Moses Reuben2d1e6972016-11-14 19:04:52 +02002044 MLX5_SET(fte_match_set_lyr_2_4, headers_c,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002045 first_prio,
2046 ntohs(ib_spec->eth.mask.vlan_tag) >> 13);
Moses Reuben2d1e6972016-11-14 19:04:52 +02002047 MLX5_SET(fte_match_set_lyr_2_4, headers_v,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002048 first_prio,
2049 ntohs(ib_spec->eth.val.vlan_tag) >> 13);
2050 }
Moses Reuben2d1e6972016-11-14 19:04:52 +02002051 MLX5_SET(fte_match_set_lyr_2_4, headers_c,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002052 ethertype, ntohs(ib_spec->eth.mask.ether_type));
Moses Reuben2d1e6972016-11-14 19:04:52 +02002053 MLX5_SET(fte_match_set_lyr_2_4, headers_v,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002054 ethertype, ntohs(ib_spec->eth.val.ether_type));
2055 break;
2056 case IB_FLOW_SPEC_IPV4:
Maor Gottliebc47ac6a2016-08-30 16:58:31 +03002057 if (FIELDS_NOT_SUPPORTED(ib_spec->ipv4.mask, LAST_IPV4_FIELD))
Leon Romanovsky1ffd3a22017-01-18 14:59:51 +02002058 return -EOPNOTSUPP;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002059
Ariel Levkovich19cc7522017-04-03 13:11:03 +03002060 if (match_ipv) {
2061 MLX5_SET(fte_match_set_lyr_2_4, headers_c,
2062 ip_version, 0xf);
2063 MLX5_SET(fte_match_set_lyr_2_4, headers_v,
2064 ip_version, IPV4_VERSION);
2065 } else {
2066 MLX5_SET(fte_match_set_lyr_2_4, headers_c,
2067 ethertype, 0xffff);
2068 MLX5_SET(fte_match_set_lyr_2_4, headers_v,
2069 ethertype, ETH_P_IP);
2070 }
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002071
Moses Reuben2d1e6972016-11-14 19:04:52 +02002072 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002073 src_ipv4_src_ipv6.ipv4_layout.ipv4),
2074 &ib_spec->ipv4.mask.src_ip,
2075 sizeof(ib_spec->ipv4.mask.src_ip));
Moses Reuben2d1e6972016-11-14 19:04:52 +02002076 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002077 src_ipv4_src_ipv6.ipv4_layout.ipv4),
2078 &ib_spec->ipv4.val.src_ip,
2079 sizeof(ib_spec->ipv4.val.src_ip));
Moses Reuben2d1e6972016-11-14 19:04:52 +02002080 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002081 dst_ipv4_dst_ipv6.ipv4_layout.ipv4),
2082 &ib_spec->ipv4.mask.dst_ip,
2083 sizeof(ib_spec->ipv4.mask.dst_ip));
Moses Reuben2d1e6972016-11-14 19:04:52 +02002084 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002085 dst_ipv4_dst_ipv6.ipv4_layout.ipv4),
2086 &ib_spec->ipv4.val.dst_ip,
2087 sizeof(ib_spec->ipv4.val.dst_ip));
Maor Gottliebca0d4752016-08-30 16:58:35 +03002088
Moses Reuben2d1e6972016-11-14 19:04:52 +02002089 set_tos(headers_c, headers_v,
Maor Gottliebca0d4752016-08-30 16:58:35 +03002090 ib_spec->ipv4.mask.tos, ib_spec->ipv4.val.tos);
2091
Moses Reuben2d1e6972016-11-14 19:04:52 +02002092 set_proto(headers_c, headers_v,
Maor Gottliebca0d4752016-08-30 16:58:35 +03002093 ib_spec->ipv4.mask.proto, ib_spec->ipv4.val.proto);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002094 break;
Maor Gottlieb026bae02016-06-17 15:14:51 +03002095 case IB_FLOW_SPEC_IPV6:
Maor Gottliebc47ac6a2016-08-30 16:58:31 +03002096 if (FIELDS_NOT_SUPPORTED(ib_spec->ipv6.mask, LAST_IPV6_FIELD))
Leon Romanovsky1ffd3a22017-01-18 14:59:51 +02002097 return -EOPNOTSUPP;
Maor Gottlieb026bae02016-06-17 15:14:51 +03002098
Ariel Levkovich19cc7522017-04-03 13:11:03 +03002099 if (match_ipv) {
2100 MLX5_SET(fte_match_set_lyr_2_4, headers_c,
2101 ip_version, 0xf);
2102 MLX5_SET(fte_match_set_lyr_2_4, headers_v,
2103 ip_version, IPV6_VERSION);
2104 } else {
2105 MLX5_SET(fte_match_set_lyr_2_4, headers_c,
2106 ethertype, 0xffff);
2107 MLX5_SET(fte_match_set_lyr_2_4, headers_v,
2108 ethertype, ETH_P_IPV6);
2109 }
Maor Gottlieb026bae02016-06-17 15:14:51 +03002110
Moses Reuben2d1e6972016-11-14 19:04:52 +02002111 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
Maor Gottlieb026bae02016-06-17 15:14:51 +03002112 src_ipv4_src_ipv6.ipv6_layout.ipv6),
2113 &ib_spec->ipv6.mask.src_ip,
2114 sizeof(ib_spec->ipv6.mask.src_ip));
Moses Reuben2d1e6972016-11-14 19:04:52 +02002115 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
Maor Gottlieb026bae02016-06-17 15:14:51 +03002116 src_ipv4_src_ipv6.ipv6_layout.ipv6),
2117 &ib_spec->ipv6.val.src_ip,
2118 sizeof(ib_spec->ipv6.val.src_ip));
Moses Reuben2d1e6972016-11-14 19:04:52 +02002119 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
Maor Gottlieb026bae02016-06-17 15:14:51 +03002120 dst_ipv4_dst_ipv6.ipv6_layout.ipv6),
2121 &ib_spec->ipv6.mask.dst_ip,
2122 sizeof(ib_spec->ipv6.mask.dst_ip));
Moses Reuben2d1e6972016-11-14 19:04:52 +02002123 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
Maor Gottlieb026bae02016-06-17 15:14:51 +03002124 dst_ipv4_dst_ipv6.ipv6_layout.ipv6),
2125 &ib_spec->ipv6.val.dst_ip,
2126 sizeof(ib_spec->ipv6.val.dst_ip));
Maor Gottlieb466fa6d2016-08-30 16:58:36 +03002127
Moses Reuben2d1e6972016-11-14 19:04:52 +02002128 set_tos(headers_c, headers_v,
Maor Gottlieb466fa6d2016-08-30 16:58:36 +03002129 ib_spec->ipv6.mask.traffic_class,
2130 ib_spec->ipv6.val.traffic_class);
2131
Moses Reuben2d1e6972016-11-14 19:04:52 +02002132 set_proto(headers_c, headers_v,
Maor Gottlieb466fa6d2016-08-30 16:58:36 +03002133 ib_spec->ipv6.mask.next_hdr,
2134 ib_spec->ipv6.val.next_hdr);
2135
Moses Reuben2d1e6972016-11-14 19:04:52 +02002136 set_flow_label(misc_params_c, misc_params_v,
2137 ntohl(ib_spec->ipv6.mask.flow_label),
2138 ntohl(ib_spec->ipv6.val.flow_label),
2139 ib_spec->type & IB_FLOW_SPEC_INNER);
2140
Maor Gottlieb026bae02016-06-17 15:14:51 +03002141 break;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002142 case IB_FLOW_SPEC_TCP:
Maor Gottliebc47ac6a2016-08-30 16:58:31 +03002143 if (FIELDS_NOT_SUPPORTED(ib_spec->tcp_udp.mask,
2144 LAST_TCP_UDP_FIELD))
Leon Romanovsky1ffd3a22017-01-18 14:59:51 +02002145 return -EOPNOTSUPP;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002146
Moses Reuben2d1e6972016-11-14 19:04:52 +02002147 MLX5_SET(fte_match_set_lyr_2_4, headers_c, ip_protocol,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002148 0xff);
Moses Reuben2d1e6972016-11-14 19:04:52 +02002149 MLX5_SET(fte_match_set_lyr_2_4, headers_v, ip_protocol,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002150 IPPROTO_TCP);
2151
Moses Reuben2d1e6972016-11-14 19:04:52 +02002152 MLX5_SET(fte_match_set_lyr_2_4, headers_c, tcp_sport,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002153 ntohs(ib_spec->tcp_udp.mask.src_port));
Moses Reuben2d1e6972016-11-14 19:04:52 +02002154 MLX5_SET(fte_match_set_lyr_2_4, headers_v, tcp_sport,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002155 ntohs(ib_spec->tcp_udp.val.src_port));
2156
Moses Reuben2d1e6972016-11-14 19:04:52 +02002157 MLX5_SET(fte_match_set_lyr_2_4, headers_c, tcp_dport,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002158 ntohs(ib_spec->tcp_udp.mask.dst_port));
Moses Reuben2d1e6972016-11-14 19:04:52 +02002159 MLX5_SET(fte_match_set_lyr_2_4, headers_v, tcp_dport,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002160 ntohs(ib_spec->tcp_udp.val.dst_port));
2161 break;
2162 case IB_FLOW_SPEC_UDP:
Maor Gottliebc47ac6a2016-08-30 16:58:31 +03002163 if (FIELDS_NOT_SUPPORTED(ib_spec->tcp_udp.mask,
2164 LAST_TCP_UDP_FIELD))
Leon Romanovsky1ffd3a22017-01-18 14:59:51 +02002165 return -EOPNOTSUPP;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002166
Moses Reuben2d1e6972016-11-14 19:04:52 +02002167 MLX5_SET(fte_match_set_lyr_2_4, headers_c, ip_protocol,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002168 0xff);
Moses Reuben2d1e6972016-11-14 19:04:52 +02002169 MLX5_SET(fte_match_set_lyr_2_4, headers_v, ip_protocol,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002170 IPPROTO_UDP);
2171
Moses Reuben2d1e6972016-11-14 19:04:52 +02002172 MLX5_SET(fte_match_set_lyr_2_4, headers_c, udp_sport,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002173 ntohs(ib_spec->tcp_udp.mask.src_port));
Moses Reuben2d1e6972016-11-14 19:04:52 +02002174 MLX5_SET(fte_match_set_lyr_2_4, headers_v, udp_sport,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002175 ntohs(ib_spec->tcp_udp.val.src_port));
2176
Moses Reuben2d1e6972016-11-14 19:04:52 +02002177 MLX5_SET(fte_match_set_lyr_2_4, headers_c, udp_dport,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002178 ntohs(ib_spec->tcp_udp.mask.dst_port));
Moses Reuben2d1e6972016-11-14 19:04:52 +02002179 MLX5_SET(fte_match_set_lyr_2_4, headers_v, udp_dport,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002180 ntohs(ib_spec->tcp_udp.val.dst_port));
2181 break;
Moses Reubenffb30d82016-11-14 19:04:50 +02002182 case IB_FLOW_SPEC_VXLAN_TUNNEL:
2183 if (FIELDS_NOT_SUPPORTED(ib_spec->tunnel.mask,
2184 LAST_TUNNEL_FIELD))
Leon Romanovsky1ffd3a22017-01-18 14:59:51 +02002185 return -EOPNOTSUPP;
Moses Reubenffb30d82016-11-14 19:04:50 +02002186
2187 MLX5_SET(fte_match_set_misc, misc_params_c, vxlan_vni,
2188 ntohl(ib_spec->tunnel.mask.tunnel_id));
2189 MLX5_SET(fte_match_set_misc, misc_params_v, vxlan_vni,
2190 ntohl(ib_spec->tunnel.val.tunnel_id));
2191 break;
Moses Reuben2ac693f2017-01-18 14:59:50 +02002192 case IB_FLOW_SPEC_ACTION_TAG:
2193 if (FIELDS_NOT_SUPPORTED(ib_spec->flow_tag,
2194 LAST_FLOW_TAG_FIELD))
2195 return -EOPNOTSUPP;
2196 if (ib_spec->flow_tag.tag_id >= BIT(24))
2197 return -EINVAL;
2198
2199 *tag_id = ib_spec->flow_tag.tag_id;
2200 break;
Slava Shwartsmana22ed862017-04-03 13:13:52 +03002201 case IB_FLOW_SPEC_ACTION_DROP:
2202 if (FIELDS_NOT_SUPPORTED(ib_spec->drop,
2203 LAST_DROP_FIELD))
2204 return -EOPNOTSUPP;
2205 *is_drop = true;
2206 break;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002207 default:
2208 return -EINVAL;
2209 }
2210
2211 return 0;
2212}
2213
2214/* If a flow could catch both multicast and unicast packets,
2215 * it won't fall into the multicast flow steering table and this rule
2216 * could steal other multicast packets.
2217 */
Yishai Hadasa550ddf2017-08-17 15:52:33 +03002218static bool flow_is_multicast_only(const struct ib_flow_attr *ib_attr)
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002219{
Yishai Hadas81e30882017-06-08 16:15:09 +03002220 union ib_flow_spec *flow_spec;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002221
2222 if (ib_attr->type != IB_FLOW_ATTR_NORMAL ||
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002223 ib_attr->num_of_specs < 1)
2224 return false;
2225
Yishai Hadas81e30882017-06-08 16:15:09 +03002226 flow_spec = (union ib_flow_spec *)(ib_attr + 1);
2227 if (flow_spec->type == IB_FLOW_SPEC_IPV4) {
2228 struct ib_flow_spec_ipv4 *ipv4_spec;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002229
Yishai Hadas81e30882017-06-08 16:15:09 +03002230 ipv4_spec = (struct ib_flow_spec_ipv4 *)flow_spec;
2231 if (ipv4_is_multicast(ipv4_spec->val.dst_ip))
2232 return true;
2233
2234 return false;
2235 }
2236
2237 if (flow_spec->type == IB_FLOW_SPEC_ETH) {
2238 struct ib_flow_spec_eth *eth_spec;
2239
2240 eth_spec = (struct ib_flow_spec_eth *)flow_spec;
2241 return is_multicast_ether_addr(eth_spec->mask.dst_mac) &&
2242 is_multicast_ether_addr(eth_spec->val.dst_mac);
2243 }
2244
2245 return false;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002246}
2247
Ariel Levkovich19cc7522017-04-03 13:11:03 +03002248static bool is_valid_ethertype(struct mlx5_core_dev *mdev,
2249 const struct ib_flow_attr *flow_attr,
Ariel Levkovich0f750962017-04-03 13:11:02 +03002250 bool check_inner)
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002251{
2252 union ib_flow_spec *ib_spec = (union ib_flow_spec *)(flow_attr + 1);
Ariel Levkovich19cc7522017-04-03 13:11:03 +03002253 int match_ipv = check_inner ?
2254 MLX5_CAP_FLOWTABLE_NIC_RX(mdev,
2255 ft_field_support.inner_ip_version) :
2256 MLX5_CAP_FLOWTABLE_NIC_RX(mdev,
2257 ft_field_support.outer_ip_version);
Ariel Levkovich0f750962017-04-03 13:11:02 +03002258 int inner_bit = check_inner ? IB_FLOW_SPEC_INNER : 0;
2259 bool ipv4_spec_valid, ipv6_spec_valid;
2260 unsigned int ip_spec_type = 0;
2261 bool has_ethertype = false;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002262 unsigned int spec_index;
Ariel Levkovich0f750962017-04-03 13:11:02 +03002263 bool mask_valid = true;
2264 u16 eth_type = 0;
2265 bool type_valid;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002266
2267 /* Validate that ethertype is correct */
2268 for (spec_index = 0; spec_index < flow_attr->num_of_specs; spec_index++) {
Ariel Levkovich0f750962017-04-03 13:11:02 +03002269 if ((ib_spec->type == (IB_FLOW_SPEC_ETH | inner_bit)) &&
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002270 ib_spec->eth.mask.ether_type) {
Ariel Levkovich0f750962017-04-03 13:11:02 +03002271 mask_valid = (ib_spec->eth.mask.ether_type ==
2272 htons(0xffff));
2273 has_ethertype = true;
2274 eth_type = ntohs(ib_spec->eth.val.ether_type);
2275 } else if ((ib_spec->type == (IB_FLOW_SPEC_IPV4 | inner_bit)) ||
2276 (ib_spec->type == (IB_FLOW_SPEC_IPV6 | inner_bit))) {
2277 ip_spec_type = ib_spec->type;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002278 }
2279 ib_spec = (void *)ib_spec + ib_spec->size;
2280 }
Ariel Levkovich0f750962017-04-03 13:11:02 +03002281
2282 type_valid = (!has_ethertype) || (!ip_spec_type);
2283 if (!type_valid && mask_valid) {
2284 ipv4_spec_valid = (eth_type == ETH_P_IP) &&
2285 (ip_spec_type == (IB_FLOW_SPEC_IPV4 | inner_bit));
2286 ipv6_spec_valid = (eth_type == ETH_P_IPV6) &&
2287 (ip_spec_type == (IB_FLOW_SPEC_IPV6 | inner_bit));
Ariel Levkovich19cc7522017-04-03 13:11:03 +03002288
2289 type_valid = (ipv4_spec_valid) || (ipv6_spec_valid) ||
2290 (((eth_type == ETH_P_MPLS_UC) ||
2291 (eth_type == ETH_P_MPLS_MC)) && match_ipv);
Ariel Levkovich0f750962017-04-03 13:11:02 +03002292 }
2293
2294 return type_valid;
2295}
2296
Ariel Levkovich19cc7522017-04-03 13:11:03 +03002297static bool is_valid_attr(struct mlx5_core_dev *mdev,
2298 const struct ib_flow_attr *flow_attr)
Ariel Levkovich0f750962017-04-03 13:11:02 +03002299{
Ariel Levkovich19cc7522017-04-03 13:11:03 +03002300 return is_valid_ethertype(mdev, flow_attr, false) &&
2301 is_valid_ethertype(mdev, flow_attr, true);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002302}
2303
2304static void put_flow_table(struct mlx5_ib_dev *dev,
2305 struct mlx5_ib_flow_prio *prio, bool ft_added)
2306{
2307 prio->refcount -= !!ft_added;
2308 if (!prio->refcount) {
2309 mlx5_destroy_flow_table(prio->flow_table);
2310 prio->flow_table = NULL;
2311 }
2312}
2313
2314static int mlx5_ib_destroy_flow(struct ib_flow *flow_id)
2315{
2316 struct mlx5_ib_dev *dev = to_mdev(flow_id->qp->device);
2317 struct mlx5_ib_flow_handler *handler = container_of(flow_id,
2318 struct mlx5_ib_flow_handler,
2319 ibflow);
2320 struct mlx5_ib_flow_handler *iter, *tmp;
2321
2322 mutex_lock(&dev->flow_db.lock);
2323
2324 list_for_each_entry_safe(iter, tmp, &handler->list, list) {
Mark Bloch74491de2016-08-31 11:24:25 +00002325 mlx5_del_flow_rules(iter->rule);
Maor Gottliebcc0e5d42016-08-28 14:16:34 +03002326 put_flow_table(dev, iter->prio, true);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002327 list_del(&iter->list);
2328 kfree(iter);
2329 }
2330
Mark Bloch74491de2016-08-31 11:24:25 +00002331 mlx5_del_flow_rules(handler->rule);
Maor Gottlieb5497adc2016-08-28 14:16:31 +03002332 put_flow_table(dev, handler->prio, true);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002333 mutex_unlock(&dev->flow_db.lock);
2334
2335 kfree(handler);
2336
2337 return 0;
2338}
2339
Maor Gottlieb35d190112016-03-07 18:51:47 +02002340static int ib_prio_to_core_prio(unsigned int priority, bool dont_trap)
2341{
2342 priority *= 2;
2343 if (!dont_trap)
2344 priority++;
2345 return priority;
2346}
2347
Maor Gottliebcc0e5d42016-08-28 14:16:34 +03002348enum flow_table_type {
2349 MLX5_IB_FT_RX,
2350 MLX5_IB_FT_TX
2351};
2352
Maor Gottlieb00b7c2a2017-03-29 06:09:01 +03002353#define MLX5_FS_MAX_TYPES 6
2354#define MLX5_FS_MAX_ENTRIES BIT(16)
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002355static struct mlx5_ib_flow_prio *get_flow_table(struct mlx5_ib_dev *dev,
Maor Gottliebcc0e5d42016-08-28 14:16:34 +03002356 struct ib_flow_attr *flow_attr,
2357 enum flow_table_type ft_type)
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002358{
Maor Gottlieb35d190112016-03-07 18:51:47 +02002359 bool dont_trap = flow_attr->flags & IB_FLOW_ATTR_FLAGS_DONT_TRAP;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002360 struct mlx5_flow_namespace *ns = NULL;
2361 struct mlx5_ib_flow_prio *prio;
2362 struct mlx5_flow_table *ft;
Maor Gottliebdac388e2017-03-29 06:09:00 +03002363 int max_table_size;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002364 int num_entries;
2365 int num_groups;
2366 int priority;
2367 int err = 0;
2368
Maor Gottliebdac388e2017-03-29 06:09:00 +03002369 max_table_size = BIT(MLX5_CAP_FLOWTABLE_NIC_RX(dev->mdev,
2370 log_max_ft_size));
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002371 if (flow_attr->type == IB_FLOW_ATTR_NORMAL) {
Maor Gottlieb35d190112016-03-07 18:51:47 +02002372 if (flow_is_multicast_only(flow_attr) &&
2373 !dont_trap)
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002374 priority = MLX5_IB_FLOW_MCAST_PRIO;
2375 else
Maor Gottlieb35d190112016-03-07 18:51:47 +02002376 priority = ib_prio_to_core_prio(flow_attr->priority,
2377 dont_trap);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002378 ns = mlx5_get_flow_namespace(dev->mdev,
2379 MLX5_FLOW_NAMESPACE_BYPASS);
2380 num_entries = MLX5_FS_MAX_ENTRIES;
2381 num_groups = MLX5_FS_MAX_TYPES;
2382 prio = &dev->flow_db.prios[priority];
2383 } else if (flow_attr->type == IB_FLOW_ATTR_ALL_DEFAULT ||
2384 flow_attr->type == IB_FLOW_ATTR_MC_DEFAULT) {
2385 ns = mlx5_get_flow_namespace(dev->mdev,
2386 MLX5_FLOW_NAMESPACE_LEFTOVERS);
2387 build_leftovers_ft_param(&priority,
2388 &num_entries,
2389 &num_groups);
2390 prio = &dev->flow_db.prios[MLX5_IB_FLOW_LEFTOVERS_PRIO];
Maor Gottliebcc0e5d42016-08-28 14:16:34 +03002391 } else if (flow_attr->type == IB_FLOW_ATTR_SNIFFER) {
2392 if (!MLX5_CAP_FLOWTABLE(dev->mdev,
2393 allow_sniffer_and_nic_rx_shared_tir))
2394 return ERR_PTR(-ENOTSUPP);
2395
2396 ns = mlx5_get_flow_namespace(dev->mdev, ft_type == MLX5_IB_FT_RX ?
2397 MLX5_FLOW_NAMESPACE_SNIFFER_RX :
2398 MLX5_FLOW_NAMESPACE_SNIFFER_TX);
2399
2400 prio = &dev->flow_db.sniffer[ft_type];
2401 priority = 0;
2402 num_entries = 1;
2403 num_groups = 1;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002404 }
2405
2406 if (!ns)
2407 return ERR_PTR(-ENOTSUPP);
2408
Maor Gottliebdac388e2017-03-29 06:09:00 +03002409 if (num_entries > max_table_size)
2410 return ERR_PTR(-ENOMEM);
2411
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002412 ft = prio->flow_table;
2413 if (!ft) {
2414 ft = mlx5_create_auto_grouped_flow_table(ns, priority,
2415 num_entries,
Maor Gottliebd63cd282016-04-29 01:36:35 +03002416 num_groups,
Hadar Hen Zionc9f1b072016-11-07 15:14:44 +02002417 0, 0);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002418
2419 if (!IS_ERR(ft)) {
2420 prio->refcount = 0;
2421 prio->flow_table = ft;
2422 } else {
2423 err = PTR_ERR(ft);
2424 }
2425 }
2426
2427 return err ? ERR_PTR(err) : prio;
2428}
2429
Yishai Hadasa550ddf2017-08-17 15:52:33 +03002430static void set_underlay_qp(struct mlx5_ib_dev *dev,
2431 struct mlx5_flow_spec *spec,
2432 u32 underlay_qpn)
2433{
2434 void *misc_params_c = MLX5_ADDR_OF(fte_match_param,
2435 spec->match_criteria,
2436 misc_parameters);
2437 void *misc_params_v = MLX5_ADDR_OF(fte_match_param, spec->match_value,
2438 misc_parameters);
2439
2440 if (underlay_qpn &&
2441 MLX5_CAP_FLOWTABLE_NIC_RX(dev->mdev,
2442 ft_field_support.bth_dst_qp)) {
2443 MLX5_SET(fte_match_set_misc,
2444 misc_params_v, bth_dst_qp, underlay_qpn);
2445 MLX5_SET(fte_match_set_misc,
2446 misc_params_c, bth_dst_qp, 0xffffff);
2447 }
2448}
2449
2450static struct mlx5_ib_flow_handler *_create_flow_rule(struct mlx5_ib_dev *dev,
2451 struct mlx5_ib_flow_prio *ft_prio,
2452 const struct ib_flow_attr *flow_attr,
2453 struct mlx5_flow_destination *dst,
2454 u32 underlay_qpn)
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002455{
2456 struct mlx5_flow_table *ft = ft_prio->flow_table;
2457 struct mlx5_ib_flow_handler *handler;
Hadar Hen Zion66958ed2016-11-07 15:14:45 +02002458 struct mlx5_flow_act flow_act = {0};
Maor Gottliebc5bb1732016-07-04 17:23:05 +03002459 struct mlx5_flow_spec *spec;
Slava Shwartsmana22ed862017-04-03 13:13:52 +03002460 struct mlx5_flow_destination *rule_dst = dst;
Maor Gottliebdd063d02016-08-28 14:16:32 +03002461 const void *ib_flow = (const void *)flow_attr + sizeof(*flow_attr);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002462 unsigned int spec_index;
Moses Reuben2ac693f2017-01-18 14:59:50 +02002463 u32 flow_tag = MLX5_FS_DEFAULT_FLOW_TAG;
Slava Shwartsmana22ed862017-04-03 13:13:52 +03002464 bool is_drop = false;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002465 int err = 0;
Slava Shwartsmana22ed862017-04-03 13:13:52 +03002466 int dest_num = 1;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002467
Ariel Levkovich19cc7522017-04-03 13:11:03 +03002468 if (!is_valid_attr(dev->mdev, flow_attr))
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002469 return ERR_PTR(-EINVAL);
2470
Leon Romanovsky1b9a07e2017-05-10 21:32:18 +03002471 spec = kvzalloc(sizeof(*spec), GFP_KERNEL);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002472 handler = kzalloc(sizeof(*handler), GFP_KERNEL);
Maor Gottliebc5bb1732016-07-04 17:23:05 +03002473 if (!handler || !spec) {
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002474 err = -ENOMEM;
2475 goto free;
2476 }
2477
2478 INIT_LIST_HEAD(&handler->list);
2479
2480 for (spec_index = 0; spec_index < flow_attr->num_of_specs; spec_index++) {
Ariel Levkovich19cc7522017-04-03 13:11:03 +03002481 err = parse_flow_attr(dev->mdev, spec->match_criteria,
Slava Shwartsmana22ed862017-04-03 13:13:52 +03002482 spec->match_value,
2483 ib_flow, &flow_tag, &is_drop);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002484 if (err < 0)
2485 goto free;
2486
2487 ib_flow += ((union ib_flow_spec *)ib_flow)->size;
2488 }
2489
Yishai Hadasa550ddf2017-08-17 15:52:33 +03002490 if (!flow_is_multicast_only(flow_attr))
2491 set_underlay_qp(dev, spec, underlay_qpn);
2492
Maor Gottlieb466fa6d2016-08-30 16:58:36 +03002493 spec->match_criteria_enable = get_match_criteria_enable(spec->match_criteria);
Slava Shwartsmana22ed862017-04-03 13:13:52 +03002494 if (is_drop) {
2495 flow_act.action = MLX5_FLOW_CONTEXT_ACTION_DROP;
2496 rule_dst = NULL;
2497 dest_num = 0;
2498 } else {
2499 flow_act.action = dst ? MLX5_FLOW_CONTEXT_ACTION_FWD_DEST :
2500 MLX5_FLOW_CONTEXT_ACTION_FWD_NEXT_PRIO;
2501 }
Moses Reuben2ac693f2017-01-18 14:59:50 +02002502
2503 if (flow_tag != MLX5_FS_DEFAULT_FLOW_TAG &&
2504 (flow_attr->type == IB_FLOW_ATTR_ALL_DEFAULT ||
2505 flow_attr->type == IB_FLOW_ATTR_MC_DEFAULT)) {
2506 mlx5_ib_warn(dev, "Flow tag %u and attribute type %x isn't allowed in leftovers\n",
2507 flow_tag, flow_attr->type);
2508 err = -EINVAL;
2509 goto free;
2510 }
2511 flow_act.flow_tag = flow_tag;
Mark Bloch74491de2016-08-31 11:24:25 +00002512 handler->rule = mlx5_add_flow_rules(ft, spec,
Hadar Hen Zion66958ed2016-11-07 15:14:45 +02002513 &flow_act,
Slava Shwartsmana22ed862017-04-03 13:13:52 +03002514 rule_dst, dest_num);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002515
2516 if (IS_ERR(handler->rule)) {
2517 err = PTR_ERR(handler->rule);
2518 goto free;
2519 }
2520
Maor Gottliebd9d49802016-08-28 14:16:33 +03002521 ft_prio->refcount++;
Maor Gottlieb5497adc2016-08-28 14:16:31 +03002522 handler->prio = ft_prio;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002523
2524 ft_prio->flow_table = ft;
2525free:
2526 if (err)
2527 kfree(handler);
Maor Gottliebc5bb1732016-07-04 17:23:05 +03002528 kvfree(spec);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002529 return err ? ERR_PTR(err) : handler;
2530}
2531
Yishai Hadasa550ddf2017-08-17 15:52:33 +03002532static struct mlx5_ib_flow_handler *create_flow_rule(struct mlx5_ib_dev *dev,
2533 struct mlx5_ib_flow_prio *ft_prio,
2534 const struct ib_flow_attr *flow_attr,
2535 struct mlx5_flow_destination *dst)
2536{
2537 return _create_flow_rule(dev, ft_prio, flow_attr, dst, 0);
2538}
2539
Maor Gottlieb35d190112016-03-07 18:51:47 +02002540static struct mlx5_ib_flow_handler *create_dont_trap_rule(struct mlx5_ib_dev *dev,
2541 struct mlx5_ib_flow_prio *ft_prio,
2542 struct ib_flow_attr *flow_attr,
2543 struct mlx5_flow_destination *dst)
2544{
2545 struct mlx5_ib_flow_handler *handler_dst = NULL;
2546 struct mlx5_ib_flow_handler *handler = NULL;
2547
2548 handler = create_flow_rule(dev, ft_prio, flow_attr, NULL);
2549 if (!IS_ERR(handler)) {
2550 handler_dst = create_flow_rule(dev, ft_prio,
2551 flow_attr, dst);
2552 if (IS_ERR(handler_dst)) {
Mark Bloch74491de2016-08-31 11:24:25 +00002553 mlx5_del_flow_rules(handler->rule);
Maor Gottliebd9d49802016-08-28 14:16:33 +03002554 ft_prio->refcount--;
Maor Gottlieb35d190112016-03-07 18:51:47 +02002555 kfree(handler);
2556 handler = handler_dst;
2557 } else {
2558 list_add(&handler_dst->list, &handler->list);
2559 }
2560 }
2561
2562 return handler;
2563}
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002564enum {
2565 LEFTOVERS_MC,
2566 LEFTOVERS_UC,
2567};
2568
2569static struct mlx5_ib_flow_handler *create_leftovers_rule(struct mlx5_ib_dev *dev,
2570 struct mlx5_ib_flow_prio *ft_prio,
2571 struct ib_flow_attr *flow_attr,
2572 struct mlx5_flow_destination *dst)
2573{
2574 struct mlx5_ib_flow_handler *handler_ucast = NULL;
2575 struct mlx5_ib_flow_handler *handler = NULL;
2576
2577 static struct {
2578 struct ib_flow_attr flow_attr;
2579 struct ib_flow_spec_eth eth_flow;
2580 } leftovers_specs[] = {
2581 [LEFTOVERS_MC] = {
2582 .flow_attr = {
2583 .num_of_specs = 1,
2584 .size = sizeof(leftovers_specs[0])
2585 },
2586 .eth_flow = {
2587 .type = IB_FLOW_SPEC_ETH,
2588 .size = sizeof(struct ib_flow_spec_eth),
2589 .mask = {.dst_mac = {0x1} },
2590 .val = {.dst_mac = {0x1} }
2591 }
2592 },
2593 [LEFTOVERS_UC] = {
2594 .flow_attr = {
2595 .num_of_specs = 1,
2596 .size = sizeof(leftovers_specs[0])
2597 },
2598 .eth_flow = {
2599 .type = IB_FLOW_SPEC_ETH,
2600 .size = sizeof(struct ib_flow_spec_eth),
2601 .mask = {.dst_mac = {0x1} },
2602 .val = {.dst_mac = {} }
2603 }
2604 }
2605 };
2606
2607 handler = create_flow_rule(dev, ft_prio,
2608 &leftovers_specs[LEFTOVERS_MC].flow_attr,
2609 dst);
2610 if (!IS_ERR(handler) &&
2611 flow_attr->type == IB_FLOW_ATTR_ALL_DEFAULT) {
2612 handler_ucast = create_flow_rule(dev, ft_prio,
2613 &leftovers_specs[LEFTOVERS_UC].flow_attr,
2614 dst);
2615 if (IS_ERR(handler_ucast)) {
Mark Bloch74491de2016-08-31 11:24:25 +00002616 mlx5_del_flow_rules(handler->rule);
Maor Gottliebd9d49802016-08-28 14:16:33 +03002617 ft_prio->refcount--;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002618 kfree(handler);
2619 handler = handler_ucast;
2620 } else {
2621 list_add(&handler_ucast->list, &handler->list);
2622 }
2623 }
2624
2625 return handler;
2626}
2627
Maor Gottliebcc0e5d42016-08-28 14:16:34 +03002628static struct mlx5_ib_flow_handler *create_sniffer_rule(struct mlx5_ib_dev *dev,
2629 struct mlx5_ib_flow_prio *ft_rx,
2630 struct mlx5_ib_flow_prio *ft_tx,
2631 struct mlx5_flow_destination *dst)
2632{
2633 struct mlx5_ib_flow_handler *handler_rx;
2634 struct mlx5_ib_flow_handler *handler_tx;
2635 int err;
2636 static const struct ib_flow_attr flow_attr = {
2637 .num_of_specs = 0,
2638 .size = sizeof(flow_attr)
2639 };
2640
2641 handler_rx = create_flow_rule(dev, ft_rx, &flow_attr, dst);
2642 if (IS_ERR(handler_rx)) {
2643 err = PTR_ERR(handler_rx);
2644 goto err;
2645 }
2646
2647 handler_tx = create_flow_rule(dev, ft_tx, &flow_attr, dst);
2648 if (IS_ERR(handler_tx)) {
2649 err = PTR_ERR(handler_tx);
2650 goto err_tx;
2651 }
2652
2653 list_add(&handler_tx->list, &handler_rx->list);
2654
2655 return handler_rx;
2656
2657err_tx:
Mark Bloch74491de2016-08-31 11:24:25 +00002658 mlx5_del_flow_rules(handler_rx->rule);
Maor Gottliebcc0e5d42016-08-28 14:16:34 +03002659 ft_rx->refcount--;
2660 kfree(handler_rx);
2661err:
2662 return ERR_PTR(err);
2663}
2664
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002665static struct ib_flow *mlx5_ib_create_flow(struct ib_qp *qp,
2666 struct ib_flow_attr *flow_attr,
2667 int domain)
2668{
2669 struct mlx5_ib_dev *dev = to_mdev(qp->device);
Yishai Hadasd9f88e52016-08-28 10:58:37 +03002670 struct mlx5_ib_qp *mqp = to_mqp(qp);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002671 struct mlx5_ib_flow_handler *handler = NULL;
2672 struct mlx5_flow_destination *dst = NULL;
Maor Gottliebcc0e5d42016-08-28 14:16:34 +03002673 struct mlx5_ib_flow_prio *ft_prio_tx = NULL;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002674 struct mlx5_ib_flow_prio *ft_prio;
2675 int err;
Yishai Hadasa550ddf2017-08-17 15:52:33 +03002676 int underlay_qpn;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002677
2678 if (flow_attr->priority > MLX5_IB_FLOW_LAST_PRIO)
Maor Gottliebdac388e2017-03-29 06:09:00 +03002679 return ERR_PTR(-ENOMEM);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002680
2681 if (domain != IB_FLOW_DOMAIN_USER ||
2682 flow_attr->port > MLX5_CAP_GEN(dev->mdev, num_ports) ||
Maor Gottlieb35d190112016-03-07 18:51:47 +02002683 (flow_attr->flags & ~IB_FLOW_ATTR_FLAGS_DONT_TRAP))
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002684 return ERR_PTR(-EINVAL);
2685
2686 dst = kzalloc(sizeof(*dst), GFP_KERNEL);
2687 if (!dst)
2688 return ERR_PTR(-ENOMEM);
2689
2690 mutex_lock(&dev->flow_db.lock);
2691
Maor Gottliebcc0e5d42016-08-28 14:16:34 +03002692 ft_prio = get_flow_table(dev, flow_attr, MLX5_IB_FT_RX);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002693 if (IS_ERR(ft_prio)) {
2694 err = PTR_ERR(ft_prio);
2695 goto unlock;
2696 }
Maor Gottliebcc0e5d42016-08-28 14:16:34 +03002697 if (flow_attr->type == IB_FLOW_ATTR_SNIFFER) {
2698 ft_prio_tx = get_flow_table(dev, flow_attr, MLX5_IB_FT_TX);
2699 if (IS_ERR(ft_prio_tx)) {
2700 err = PTR_ERR(ft_prio_tx);
2701 ft_prio_tx = NULL;
2702 goto destroy_ft;
2703 }
2704 }
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002705
2706 dst->type = MLX5_FLOW_DESTINATION_TYPE_TIR;
Yishai Hadasd9f88e52016-08-28 10:58:37 +03002707 if (mqp->flags & MLX5_IB_QP_RSS)
2708 dst->tir_num = mqp->rss_qp.tirn;
2709 else
2710 dst->tir_num = mqp->raw_packet_qp.rq.tirn;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002711
2712 if (flow_attr->type == IB_FLOW_ATTR_NORMAL) {
Maor Gottlieb35d190112016-03-07 18:51:47 +02002713 if (flow_attr->flags & IB_FLOW_ATTR_FLAGS_DONT_TRAP) {
2714 handler = create_dont_trap_rule(dev, ft_prio,
2715 flow_attr, dst);
2716 } else {
Yishai Hadasa550ddf2017-08-17 15:52:33 +03002717 underlay_qpn = (mqp->flags & MLX5_IB_QP_UNDERLAY) ?
2718 mqp->underlay_qpn : 0;
2719 handler = _create_flow_rule(dev, ft_prio, flow_attr,
2720 dst, underlay_qpn);
Maor Gottlieb35d190112016-03-07 18:51:47 +02002721 }
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002722 } else if (flow_attr->type == IB_FLOW_ATTR_ALL_DEFAULT ||
2723 flow_attr->type == IB_FLOW_ATTR_MC_DEFAULT) {
2724 handler = create_leftovers_rule(dev, ft_prio, flow_attr,
2725 dst);
Maor Gottliebcc0e5d42016-08-28 14:16:34 +03002726 } else if (flow_attr->type == IB_FLOW_ATTR_SNIFFER) {
2727 handler = create_sniffer_rule(dev, ft_prio, ft_prio_tx, dst);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002728 } else {
2729 err = -EINVAL;
2730 goto destroy_ft;
2731 }
2732
2733 if (IS_ERR(handler)) {
2734 err = PTR_ERR(handler);
2735 handler = NULL;
2736 goto destroy_ft;
2737 }
2738
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002739 mutex_unlock(&dev->flow_db.lock);
2740 kfree(dst);
2741
2742 return &handler->ibflow;
2743
2744destroy_ft:
2745 put_flow_table(dev, ft_prio, false);
Maor Gottliebcc0e5d42016-08-28 14:16:34 +03002746 if (ft_prio_tx)
2747 put_flow_table(dev, ft_prio_tx, false);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002748unlock:
2749 mutex_unlock(&dev->flow_db.lock);
2750 kfree(dst);
2751 kfree(handler);
2752 return ERR_PTR(err);
2753}
2754
Eli Cohene126ba92013-07-07 17:25:49 +03002755static int mlx5_ib_mcg_attach(struct ib_qp *ibqp, union ib_gid *gid, u16 lid)
2756{
2757 struct mlx5_ib_dev *dev = to_mdev(ibqp->device);
Yishai Hadas81e30882017-06-08 16:15:09 +03002758 struct mlx5_ib_qp *mqp = to_mqp(ibqp);
Eli Cohene126ba92013-07-07 17:25:49 +03002759 int err;
2760
Yishai Hadas81e30882017-06-08 16:15:09 +03002761 if (mqp->flags & MLX5_IB_QP_UNDERLAY) {
2762 mlx5_ib_dbg(dev, "Attaching a multi cast group to underlay QP is not supported\n");
2763 return -EOPNOTSUPP;
2764 }
2765
Jack Morgenstein9603b612014-07-28 23:30:22 +03002766 err = mlx5_core_attach_mcg(dev->mdev, gid, ibqp->qp_num);
Eli Cohene126ba92013-07-07 17:25:49 +03002767 if (err)
2768 mlx5_ib_warn(dev, "failed attaching QPN 0x%x, MGID %pI6\n",
2769 ibqp->qp_num, gid->raw);
2770
2771 return err;
2772}
2773
2774static int mlx5_ib_mcg_detach(struct ib_qp *ibqp, union ib_gid *gid, u16 lid)
2775{
2776 struct mlx5_ib_dev *dev = to_mdev(ibqp->device);
2777 int err;
2778
Jack Morgenstein9603b612014-07-28 23:30:22 +03002779 err = mlx5_core_detach_mcg(dev->mdev, gid, ibqp->qp_num);
Eli Cohene126ba92013-07-07 17:25:49 +03002780 if (err)
2781 mlx5_ib_warn(dev, "failed detaching QPN 0x%x, MGID %pI6\n",
2782 ibqp->qp_num, gid->raw);
2783
2784 return err;
2785}
2786
2787static int init_node_data(struct mlx5_ib_dev *dev)
2788{
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03002789 int err;
Eli Cohene126ba92013-07-07 17:25:49 +03002790
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03002791 err = mlx5_query_node_desc(dev, dev->ib_dev.node_desc);
Eli Cohene126ba92013-07-07 17:25:49 +03002792 if (err)
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03002793 return err;
Eli Cohene126ba92013-07-07 17:25:49 +03002794
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03002795 dev->mdev->rev_id = dev->mdev->pdev->revision;
Eli Cohene126ba92013-07-07 17:25:49 +03002796
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03002797 return mlx5_query_node_guid(dev, &dev->ib_dev.node_guid);
Eli Cohene126ba92013-07-07 17:25:49 +03002798}
2799
2800static ssize_t show_fw_pages(struct device *device, struct device_attribute *attr,
2801 char *buf)
2802{
2803 struct mlx5_ib_dev *dev =
2804 container_of(device, struct mlx5_ib_dev, ib_dev.dev);
2805
Jack Morgenstein9603b612014-07-28 23:30:22 +03002806 return sprintf(buf, "%d\n", dev->mdev->priv.fw_pages);
Eli Cohene126ba92013-07-07 17:25:49 +03002807}
2808
2809static ssize_t show_reg_pages(struct device *device,
2810 struct device_attribute *attr, char *buf)
2811{
2812 struct mlx5_ib_dev *dev =
2813 container_of(device, struct mlx5_ib_dev, ib_dev.dev);
2814
Haggai Eran6aec21f2014-12-11 17:04:23 +02002815 return sprintf(buf, "%d\n", atomic_read(&dev->mdev->priv.reg_pages));
Eli Cohene126ba92013-07-07 17:25:49 +03002816}
2817
2818static ssize_t show_hca(struct device *device, struct device_attribute *attr,
2819 char *buf)
2820{
2821 struct mlx5_ib_dev *dev =
2822 container_of(device, struct mlx5_ib_dev, ib_dev.dev);
Jack Morgenstein9603b612014-07-28 23:30:22 +03002823 return sprintf(buf, "MT%d\n", dev->mdev->pdev->device);
Eli Cohene126ba92013-07-07 17:25:49 +03002824}
2825
Eli Cohene126ba92013-07-07 17:25:49 +03002826static ssize_t show_rev(struct device *device, struct device_attribute *attr,
2827 char *buf)
2828{
2829 struct mlx5_ib_dev *dev =
2830 container_of(device, struct mlx5_ib_dev, ib_dev.dev);
Jack Morgenstein9603b612014-07-28 23:30:22 +03002831 return sprintf(buf, "%x\n", dev->mdev->rev_id);
Eli Cohene126ba92013-07-07 17:25:49 +03002832}
2833
2834static ssize_t show_board(struct device *device, struct device_attribute *attr,
2835 char *buf)
2836{
2837 struct mlx5_ib_dev *dev =
2838 container_of(device, struct mlx5_ib_dev, ib_dev.dev);
2839 return sprintf(buf, "%.*s\n", MLX5_BOARD_ID_LEN,
Jack Morgenstein9603b612014-07-28 23:30:22 +03002840 dev->mdev->board_id);
Eli Cohene126ba92013-07-07 17:25:49 +03002841}
2842
2843static DEVICE_ATTR(hw_rev, S_IRUGO, show_rev, NULL);
Eli Cohene126ba92013-07-07 17:25:49 +03002844static DEVICE_ATTR(hca_type, S_IRUGO, show_hca, NULL);
2845static DEVICE_ATTR(board_id, S_IRUGO, show_board, NULL);
2846static DEVICE_ATTR(fw_pages, S_IRUGO, show_fw_pages, NULL);
2847static DEVICE_ATTR(reg_pages, S_IRUGO, show_reg_pages, NULL);
2848
2849static struct device_attribute *mlx5_class_attributes[] = {
2850 &dev_attr_hw_rev,
Eli Cohene126ba92013-07-07 17:25:49 +03002851 &dev_attr_hca_type,
2852 &dev_attr_board_id,
2853 &dev_attr_fw_pages,
2854 &dev_attr_reg_pages,
2855};
2856
Haggai Eran7722f472016-02-29 15:45:07 +02002857static void pkey_change_handler(struct work_struct *work)
2858{
2859 struct mlx5_ib_port_resources *ports =
2860 container_of(work, struct mlx5_ib_port_resources,
2861 pkey_change_work);
2862
2863 mutex_lock(&ports->devr->mutex);
2864 mlx5_ib_gsi_pkey_change(ports->gsi);
2865 mutex_unlock(&ports->devr->mutex);
2866}
2867
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03002868static void mlx5_ib_handle_internal_error(struct mlx5_ib_dev *ibdev)
2869{
2870 struct mlx5_ib_qp *mqp;
2871 struct mlx5_ib_cq *send_mcq, *recv_mcq;
2872 struct mlx5_core_cq *mcq;
2873 struct list_head cq_armed_list;
2874 unsigned long flags_qp;
2875 unsigned long flags_cq;
2876 unsigned long flags;
2877
2878 INIT_LIST_HEAD(&cq_armed_list);
2879
2880 /* Go over qp list reside on that ibdev, sync with create/destroy qp.*/
2881 spin_lock_irqsave(&ibdev->reset_flow_resource_lock, flags);
2882 list_for_each_entry(mqp, &ibdev->qp_list, qps_list) {
2883 spin_lock_irqsave(&mqp->sq.lock, flags_qp);
2884 if (mqp->sq.tail != mqp->sq.head) {
2885 send_mcq = to_mcq(mqp->ibqp.send_cq);
2886 spin_lock_irqsave(&send_mcq->lock, flags_cq);
2887 if (send_mcq->mcq.comp &&
2888 mqp->ibqp.send_cq->comp_handler) {
2889 if (!send_mcq->mcq.reset_notify_added) {
2890 send_mcq->mcq.reset_notify_added = 1;
2891 list_add_tail(&send_mcq->mcq.reset_notify,
2892 &cq_armed_list);
2893 }
2894 }
2895 spin_unlock_irqrestore(&send_mcq->lock, flags_cq);
2896 }
2897 spin_unlock_irqrestore(&mqp->sq.lock, flags_qp);
2898 spin_lock_irqsave(&mqp->rq.lock, flags_qp);
2899 /* no handling is needed for SRQ */
2900 if (!mqp->ibqp.srq) {
2901 if (mqp->rq.tail != mqp->rq.head) {
2902 recv_mcq = to_mcq(mqp->ibqp.recv_cq);
2903 spin_lock_irqsave(&recv_mcq->lock, flags_cq);
2904 if (recv_mcq->mcq.comp &&
2905 mqp->ibqp.recv_cq->comp_handler) {
2906 if (!recv_mcq->mcq.reset_notify_added) {
2907 recv_mcq->mcq.reset_notify_added = 1;
2908 list_add_tail(&recv_mcq->mcq.reset_notify,
2909 &cq_armed_list);
2910 }
2911 }
2912 spin_unlock_irqrestore(&recv_mcq->lock,
2913 flags_cq);
2914 }
2915 }
2916 spin_unlock_irqrestore(&mqp->rq.lock, flags_qp);
2917 }
2918 /*At that point all inflight post send were put to be executed as of we
2919 * lock/unlock above locks Now need to arm all involved CQs.
2920 */
2921 list_for_each_entry(mcq, &cq_armed_list, reset_notify) {
2922 mcq->comp(mcq);
2923 }
2924 spin_unlock_irqrestore(&ibdev->reset_flow_resource_lock, flags);
2925}
2926
Maor Gottlieb03404e82017-05-30 10:29:13 +03002927static void delay_drop_handler(struct work_struct *work)
2928{
2929 int err;
2930 struct mlx5_ib_delay_drop *delay_drop =
2931 container_of(work, struct mlx5_ib_delay_drop,
2932 delay_drop_work);
2933
Maor Gottliebfe248c32017-05-30 10:29:14 +03002934 atomic_inc(&delay_drop->events_cnt);
2935
Maor Gottlieb03404e82017-05-30 10:29:13 +03002936 mutex_lock(&delay_drop->lock);
2937 err = mlx5_core_set_delay_drop(delay_drop->dev->mdev,
2938 delay_drop->timeout);
2939 if (err) {
2940 mlx5_ib_warn(delay_drop->dev, "Failed to set delay drop, timeout=%u\n",
2941 delay_drop->timeout);
2942 delay_drop->activate = false;
2943 }
2944 mutex_unlock(&delay_drop->lock);
2945}
2946
Jack Morgenstein9603b612014-07-28 23:30:22 +03002947static void mlx5_ib_event(struct mlx5_core_dev *dev, void *context,
Jack Morgenstein4d2f9bb2014-07-28 23:30:24 +03002948 enum mlx5_dev_event event, unsigned long param)
Eli Cohene126ba92013-07-07 17:25:49 +03002949{
Jack Morgenstein9603b612014-07-28 23:30:22 +03002950 struct mlx5_ib_dev *ibdev = (struct mlx5_ib_dev *)context;
Eli Cohene126ba92013-07-07 17:25:49 +03002951 struct ib_event ibev;
Eli Cohendbaaff22016-10-27 16:36:44 +03002952 bool fatal = false;
Eli Cohene126ba92013-07-07 17:25:49 +03002953 u8 port = 0;
2954
2955 switch (event) {
2956 case MLX5_DEV_EVENT_SYS_ERROR:
Eli Cohene126ba92013-07-07 17:25:49 +03002957 ibev.event = IB_EVENT_DEVICE_FATAL;
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03002958 mlx5_ib_handle_internal_error(ibdev);
Eli Cohendbaaff22016-10-27 16:36:44 +03002959 fatal = true;
Eli Cohene126ba92013-07-07 17:25:49 +03002960 break;
2961
2962 case MLX5_DEV_EVENT_PORT_UP:
Eli Cohene126ba92013-07-07 17:25:49 +03002963 case MLX5_DEV_EVENT_PORT_DOWN:
Noa Osherovich2788cf32016-06-04 15:15:29 +03002964 case MLX5_DEV_EVENT_PORT_INITIALIZED:
Jack Morgenstein4d2f9bb2014-07-28 23:30:24 +03002965 port = (u8)param;
Aviv Heller5ec8c832016-09-18 20:48:00 +03002966
2967 /* In RoCE, port up/down events are handled in
2968 * mlx5_netdev_event().
2969 */
2970 if (mlx5_ib_port_link_layer(&ibdev->ib_dev, port) ==
2971 IB_LINK_LAYER_ETHERNET)
2972 return;
2973
2974 ibev.event = (event == MLX5_DEV_EVENT_PORT_UP) ?
2975 IB_EVENT_PORT_ACTIVE : IB_EVENT_PORT_ERR;
Eli Cohene126ba92013-07-07 17:25:49 +03002976 break;
2977
Eli Cohene126ba92013-07-07 17:25:49 +03002978 case MLX5_DEV_EVENT_LID_CHANGE:
2979 ibev.event = IB_EVENT_LID_CHANGE;
Jack Morgenstein4d2f9bb2014-07-28 23:30:24 +03002980 port = (u8)param;
Eli Cohene126ba92013-07-07 17:25:49 +03002981 break;
2982
2983 case MLX5_DEV_EVENT_PKEY_CHANGE:
2984 ibev.event = IB_EVENT_PKEY_CHANGE;
Jack Morgenstein4d2f9bb2014-07-28 23:30:24 +03002985 port = (u8)param;
Haggai Eran7722f472016-02-29 15:45:07 +02002986
2987 schedule_work(&ibdev->devr.ports[port - 1].pkey_change_work);
Eli Cohene126ba92013-07-07 17:25:49 +03002988 break;
2989
2990 case MLX5_DEV_EVENT_GUID_CHANGE:
2991 ibev.event = IB_EVENT_GID_CHANGE;
Jack Morgenstein4d2f9bb2014-07-28 23:30:24 +03002992 port = (u8)param;
Eli Cohene126ba92013-07-07 17:25:49 +03002993 break;
2994
2995 case MLX5_DEV_EVENT_CLIENT_REREG:
2996 ibev.event = IB_EVENT_CLIENT_REREGISTER;
Jack Morgenstein4d2f9bb2014-07-28 23:30:24 +03002997 port = (u8)param;
Eli Cohene126ba92013-07-07 17:25:49 +03002998 break;
Maor Gottlieb03404e82017-05-30 10:29:13 +03002999 case MLX5_DEV_EVENT_DELAY_DROP_TIMEOUT:
3000 schedule_work(&ibdev->delay_drop.delay_drop_work);
3001 goto out;
Saeed Mahameedbdc37922016-09-29 19:35:38 +03003002 default:
Maor Gottlieb03404e82017-05-30 10:29:13 +03003003 goto out;
Eli Cohene126ba92013-07-07 17:25:49 +03003004 }
3005
3006 ibev.device = &ibdev->ib_dev;
3007 ibev.element.port_num = port;
3008
Eli Cohena0c84c32013-09-11 16:35:27 +03003009 if (port < 1 || port > ibdev->num_ports) {
3010 mlx5_ib_warn(ibdev, "warning: event on port %d\n", port);
Maor Gottlieb03404e82017-05-30 10:29:13 +03003011 goto out;
Eli Cohena0c84c32013-09-11 16:35:27 +03003012 }
3013
Eli Cohene126ba92013-07-07 17:25:49 +03003014 if (ibdev->ib_active)
3015 ib_dispatch_event(&ibev);
Eli Cohendbaaff22016-10-27 16:36:44 +03003016
3017 if (fatal)
3018 ibdev->ib_active = false;
Maor Gottlieb03404e82017-05-30 10:29:13 +03003019
3020out:
3021 return;
Eli Cohene126ba92013-07-07 17:25:49 +03003022}
3023
Maor Gottliebc43f1112017-01-18 14:10:33 +02003024static int set_has_smi_cap(struct mlx5_ib_dev *dev)
3025{
3026 struct mlx5_hca_vport_context vport_ctx;
3027 int err;
3028 int port;
3029
3030 for (port = 1; port <= MLX5_CAP_GEN(dev->mdev, num_ports); port++) {
3031 dev->mdev->port_caps[port - 1].has_smi = false;
3032 if (MLX5_CAP_GEN(dev->mdev, port_type) ==
3033 MLX5_CAP_PORT_TYPE_IB) {
3034 if (MLX5_CAP_GEN(dev->mdev, ib_virt)) {
3035 err = mlx5_query_hca_vport_context(dev->mdev, 0,
3036 port, 0,
3037 &vport_ctx);
3038 if (err) {
3039 mlx5_ib_err(dev, "query_hca_vport_context for port=%d failed %d\n",
3040 port, err);
3041 return err;
3042 }
3043 dev->mdev->port_caps[port - 1].has_smi =
3044 vport_ctx.has_smi;
3045 } else {
3046 dev->mdev->port_caps[port - 1].has_smi = true;
3047 }
3048 }
3049 }
3050 return 0;
3051}
3052
Eli Cohene126ba92013-07-07 17:25:49 +03003053static void get_ext_port_caps(struct mlx5_ib_dev *dev)
3054{
3055 int port;
3056
Saeed Mahameed938fe832015-05-28 22:28:41 +03003057 for (port = 1; port <= MLX5_CAP_GEN(dev->mdev, num_ports); port++)
Eli Cohene126ba92013-07-07 17:25:49 +03003058 mlx5_query_ext_port_caps(dev, port);
3059}
3060
3061static int get_port_caps(struct mlx5_ib_dev *dev)
3062{
3063 struct ib_device_attr *dprops = NULL;
3064 struct ib_port_attr *pprops = NULL;
Dan Carpenterf614fc12015-01-12 11:56:58 +03003065 int err = -ENOMEM;
Eli Cohene126ba92013-07-07 17:25:49 +03003066 int port;
Matan Barak2528e332015-06-11 16:35:25 +03003067 struct ib_udata uhw = {.inlen = 0, .outlen = 0};
Eli Cohene126ba92013-07-07 17:25:49 +03003068
3069 pprops = kmalloc(sizeof(*pprops), GFP_KERNEL);
3070 if (!pprops)
3071 goto out;
3072
3073 dprops = kmalloc(sizeof(*dprops), GFP_KERNEL);
3074 if (!dprops)
3075 goto out;
3076
Maor Gottliebc43f1112017-01-18 14:10:33 +02003077 err = set_has_smi_cap(dev);
3078 if (err)
3079 goto out;
3080
Matan Barak2528e332015-06-11 16:35:25 +03003081 err = mlx5_ib_query_device(&dev->ib_dev, dprops, &uhw);
Eli Cohene126ba92013-07-07 17:25:49 +03003082 if (err) {
3083 mlx5_ib_warn(dev, "query_device failed %d\n", err);
3084 goto out;
3085 }
3086
Saeed Mahameed938fe832015-05-28 22:28:41 +03003087 for (port = 1; port <= MLX5_CAP_GEN(dev->mdev, num_ports); port++) {
Or Gerlitzc4550c62017-01-24 13:02:39 +02003088 memset(pprops, 0, sizeof(*pprops));
Eli Cohene126ba92013-07-07 17:25:49 +03003089 err = mlx5_ib_query_port(&dev->ib_dev, port, pprops);
3090 if (err) {
Saeed Mahameed938fe832015-05-28 22:28:41 +03003091 mlx5_ib_warn(dev, "query_port %d failed %d\n",
3092 port, err);
Eli Cohene126ba92013-07-07 17:25:49 +03003093 break;
3094 }
Saeed Mahameed938fe832015-05-28 22:28:41 +03003095 dev->mdev->port_caps[port - 1].pkey_table_len =
3096 dprops->max_pkeys;
3097 dev->mdev->port_caps[port - 1].gid_table_len =
3098 pprops->gid_tbl_len;
Eli Cohene126ba92013-07-07 17:25:49 +03003099 mlx5_ib_dbg(dev, "pkey_table_len %d, gid_table_len %d\n",
3100 dprops->max_pkeys, pprops->gid_tbl_len);
3101 }
3102
3103out:
3104 kfree(pprops);
3105 kfree(dprops);
3106
3107 return err;
3108}
3109
3110static void destroy_umrc_res(struct mlx5_ib_dev *dev)
3111{
3112 int err;
3113
3114 err = mlx5_mr_cache_cleanup(dev);
3115 if (err)
3116 mlx5_ib_warn(dev, "mr cache cleanup failed\n");
3117
3118 mlx5_ib_destroy_qp(dev->umrc.qp);
Christoph Hellwigadd08d72016-03-03 09:38:22 +01003119 ib_free_cq(dev->umrc.cq);
Eli Cohene126ba92013-07-07 17:25:49 +03003120 ib_dealloc_pd(dev->umrc.pd);
3121}
3122
3123enum {
3124 MAX_UMR_WR = 128,
3125};
3126
3127static int create_umr_res(struct mlx5_ib_dev *dev)
3128{
3129 struct ib_qp_init_attr *init_attr = NULL;
3130 struct ib_qp_attr *attr = NULL;
3131 struct ib_pd *pd;
3132 struct ib_cq *cq;
3133 struct ib_qp *qp;
Eli Cohene126ba92013-07-07 17:25:49 +03003134 int ret;
3135
3136 attr = kzalloc(sizeof(*attr), GFP_KERNEL);
3137 init_attr = kzalloc(sizeof(*init_attr), GFP_KERNEL);
3138 if (!attr || !init_attr) {
3139 ret = -ENOMEM;
3140 goto error_0;
3141 }
3142
Christoph Hellwiged082d32016-09-05 12:56:17 +02003143 pd = ib_alloc_pd(&dev->ib_dev, 0);
Eli Cohene126ba92013-07-07 17:25:49 +03003144 if (IS_ERR(pd)) {
3145 mlx5_ib_dbg(dev, "Couldn't create PD for sync UMR QP\n");
3146 ret = PTR_ERR(pd);
3147 goto error_0;
3148 }
3149
Christoph Hellwigadd08d72016-03-03 09:38:22 +01003150 cq = ib_alloc_cq(&dev->ib_dev, NULL, 128, 0, IB_POLL_SOFTIRQ);
Eli Cohene126ba92013-07-07 17:25:49 +03003151 if (IS_ERR(cq)) {
3152 mlx5_ib_dbg(dev, "Couldn't create CQ for sync UMR QP\n");
3153 ret = PTR_ERR(cq);
3154 goto error_2;
3155 }
Eli Cohene126ba92013-07-07 17:25:49 +03003156
3157 init_attr->send_cq = cq;
3158 init_attr->recv_cq = cq;
3159 init_attr->sq_sig_type = IB_SIGNAL_ALL_WR;
3160 init_attr->cap.max_send_wr = MAX_UMR_WR;
3161 init_attr->cap.max_send_sge = 1;
3162 init_attr->qp_type = MLX5_IB_QPT_REG_UMR;
3163 init_attr->port_num = 1;
3164 qp = mlx5_ib_create_qp(pd, init_attr, NULL);
3165 if (IS_ERR(qp)) {
3166 mlx5_ib_dbg(dev, "Couldn't create sync UMR QP\n");
3167 ret = PTR_ERR(qp);
3168 goto error_3;
3169 }
3170 qp->device = &dev->ib_dev;
3171 qp->real_qp = qp;
3172 qp->uobject = NULL;
3173 qp->qp_type = MLX5_IB_QPT_REG_UMR;
Majd Dibbiny31fde032017-10-30 14:23:13 +02003174 qp->send_cq = init_attr->send_cq;
3175 qp->recv_cq = init_attr->recv_cq;
Eli Cohene126ba92013-07-07 17:25:49 +03003176
3177 attr->qp_state = IB_QPS_INIT;
3178 attr->port_num = 1;
3179 ret = mlx5_ib_modify_qp(qp, attr, IB_QP_STATE | IB_QP_PKEY_INDEX |
3180 IB_QP_PORT, NULL);
3181 if (ret) {
3182 mlx5_ib_dbg(dev, "Couldn't modify UMR QP\n");
3183 goto error_4;
3184 }
3185
3186 memset(attr, 0, sizeof(*attr));
3187 attr->qp_state = IB_QPS_RTR;
3188 attr->path_mtu = IB_MTU_256;
3189
3190 ret = mlx5_ib_modify_qp(qp, attr, IB_QP_STATE, NULL);
3191 if (ret) {
3192 mlx5_ib_dbg(dev, "Couldn't modify umr QP to rtr\n");
3193 goto error_4;
3194 }
3195
3196 memset(attr, 0, sizeof(*attr));
3197 attr->qp_state = IB_QPS_RTS;
3198 ret = mlx5_ib_modify_qp(qp, attr, IB_QP_STATE, NULL);
3199 if (ret) {
3200 mlx5_ib_dbg(dev, "Couldn't modify umr QP to rts\n");
3201 goto error_4;
3202 }
3203
3204 dev->umrc.qp = qp;
3205 dev->umrc.cq = cq;
Eli Cohene126ba92013-07-07 17:25:49 +03003206 dev->umrc.pd = pd;
3207
3208 sema_init(&dev->umrc.sem, MAX_UMR_WR);
3209 ret = mlx5_mr_cache_init(dev);
3210 if (ret) {
3211 mlx5_ib_warn(dev, "mr cache init failed %d\n", ret);
3212 goto error_4;
3213 }
3214
3215 kfree(attr);
3216 kfree(init_attr);
3217
3218 return 0;
3219
3220error_4:
3221 mlx5_ib_destroy_qp(qp);
3222
3223error_3:
Christoph Hellwigadd08d72016-03-03 09:38:22 +01003224 ib_free_cq(cq);
Eli Cohene126ba92013-07-07 17:25:49 +03003225
3226error_2:
Eli Cohene126ba92013-07-07 17:25:49 +03003227 ib_dealloc_pd(pd);
3228
3229error_0:
3230 kfree(attr);
3231 kfree(init_attr);
3232 return ret;
3233}
3234
Max Gurtovoy6e8484c2017-05-28 10:53:11 +03003235static u8 mlx5_get_umr_fence(u8 umr_fence_cap)
3236{
3237 switch (umr_fence_cap) {
3238 case MLX5_CAP_UMR_FENCE_NONE:
3239 return MLX5_FENCE_MODE_NONE;
3240 case MLX5_CAP_UMR_FENCE_SMALL:
3241 return MLX5_FENCE_MODE_INITIATOR_SMALL;
3242 default:
3243 return MLX5_FENCE_MODE_STRONG_ORDERING;
3244 }
3245}
3246
Eli Cohene126ba92013-07-07 17:25:49 +03003247static int create_dev_resources(struct mlx5_ib_resources *devr)
3248{
3249 struct ib_srq_init_attr attr;
3250 struct mlx5_ib_dev *dev;
Matan Barakbcf4c1e2015-06-11 16:35:20 +03003251 struct ib_cq_init_attr cq_attr = {.cqe = 1};
Haggai Eran7722f472016-02-29 15:45:07 +02003252 int port;
Eli Cohene126ba92013-07-07 17:25:49 +03003253 int ret = 0;
3254
3255 dev = container_of(devr, struct mlx5_ib_dev, devr);
3256
Haggai Erand16e91d2016-02-29 15:45:05 +02003257 mutex_init(&devr->mutex);
3258
Eli Cohene126ba92013-07-07 17:25:49 +03003259 devr->p0 = mlx5_ib_alloc_pd(&dev->ib_dev, NULL, NULL);
3260 if (IS_ERR(devr->p0)) {
3261 ret = PTR_ERR(devr->p0);
3262 goto error0;
3263 }
3264 devr->p0->device = &dev->ib_dev;
3265 devr->p0->uobject = NULL;
3266 atomic_set(&devr->p0->usecnt, 0);
3267
Matan Barakbcf4c1e2015-06-11 16:35:20 +03003268 devr->c0 = mlx5_ib_create_cq(&dev->ib_dev, &cq_attr, NULL, NULL);
Eli Cohene126ba92013-07-07 17:25:49 +03003269 if (IS_ERR(devr->c0)) {
3270 ret = PTR_ERR(devr->c0);
3271 goto error1;
3272 }
3273 devr->c0->device = &dev->ib_dev;
3274 devr->c0->uobject = NULL;
3275 devr->c0->comp_handler = NULL;
3276 devr->c0->event_handler = NULL;
3277 devr->c0->cq_context = NULL;
3278 atomic_set(&devr->c0->usecnt, 0);
3279
3280 devr->x0 = mlx5_ib_alloc_xrcd(&dev->ib_dev, NULL, NULL);
3281 if (IS_ERR(devr->x0)) {
3282 ret = PTR_ERR(devr->x0);
3283 goto error2;
3284 }
3285 devr->x0->device = &dev->ib_dev;
3286 devr->x0->inode = NULL;
3287 atomic_set(&devr->x0->usecnt, 0);
3288 mutex_init(&devr->x0->tgt_qp_mutex);
3289 INIT_LIST_HEAD(&devr->x0->tgt_qp_list);
3290
3291 devr->x1 = mlx5_ib_alloc_xrcd(&dev->ib_dev, NULL, NULL);
3292 if (IS_ERR(devr->x1)) {
3293 ret = PTR_ERR(devr->x1);
3294 goto error3;
3295 }
3296 devr->x1->device = &dev->ib_dev;
3297 devr->x1->inode = NULL;
3298 atomic_set(&devr->x1->usecnt, 0);
3299 mutex_init(&devr->x1->tgt_qp_mutex);
3300 INIT_LIST_HEAD(&devr->x1->tgt_qp_list);
3301
3302 memset(&attr, 0, sizeof(attr));
3303 attr.attr.max_sge = 1;
3304 attr.attr.max_wr = 1;
3305 attr.srq_type = IB_SRQT_XRC;
Artemy Kovalyov1a56ff62017-08-17 15:52:04 +03003306 attr.ext.cq = devr->c0;
Eli Cohene126ba92013-07-07 17:25:49 +03003307 attr.ext.xrc.xrcd = devr->x0;
3308
3309 devr->s0 = mlx5_ib_create_srq(devr->p0, &attr, NULL);
3310 if (IS_ERR(devr->s0)) {
3311 ret = PTR_ERR(devr->s0);
3312 goto error4;
3313 }
3314 devr->s0->device = &dev->ib_dev;
3315 devr->s0->pd = devr->p0;
3316 devr->s0->uobject = NULL;
3317 devr->s0->event_handler = NULL;
3318 devr->s0->srq_context = NULL;
3319 devr->s0->srq_type = IB_SRQT_XRC;
3320 devr->s0->ext.xrc.xrcd = devr->x0;
Artemy Kovalyov1a56ff62017-08-17 15:52:04 +03003321 devr->s0->ext.cq = devr->c0;
Eli Cohene126ba92013-07-07 17:25:49 +03003322 atomic_inc(&devr->s0->ext.xrc.xrcd->usecnt);
Artemy Kovalyov1a56ff62017-08-17 15:52:04 +03003323 atomic_inc(&devr->s0->ext.cq->usecnt);
Eli Cohene126ba92013-07-07 17:25:49 +03003324 atomic_inc(&devr->p0->usecnt);
3325 atomic_set(&devr->s0->usecnt, 0);
3326
Haggai Abramonvsky4aa17b22015-06-04 19:30:48 +03003327 memset(&attr, 0, sizeof(attr));
3328 attr.attr.max_sge = 1;
3329 attr.attr.max_wr = 1;
3330 attr.srq_type = IB_SRQT_BASIC;
3331 devr->s1 = mlx5_ib_create_srq(devr->p0, &attr, NULL);
3332 if (IS_ERR(devr->s1)) {
3333 ret = PTR_ERR(devr->s1);
3334 goto error5;
3335 }
3336 devr->s1->device = &dev->ib_dev;
3337 devr->s1->pd = devr->p0;
3338 devr->s1->uobject = NULL;
3339 devr->s1->event_handler = NULL;
3340 devr->s1->srq_context = NULL;
3341 devr->s1->srq_type = IB_SRQT_BASIC;
Artemy Kovalyov1a56ff62017-08-17 15:52:04 +03003342 devr->s1->ext.cq = devr->c0;
Haggai Abramonvsky4aa17b22015-06-04 19:30:48 +03003343 atomic_inc(&devr->p0->usecnt);
Artemy Kovalyov1a56ff62017-08-17 15:52:04 +03003344 atomic_set(&devr->s1->usecnt, 0);
Haggai Abramonvsky4aa17b22015-06-04 19:30:48 +03003345
Haggai Eran7722f472016-02-29 15:45:07 +02003346 for (port = 0; port < ARRAY_SIZE(devr->ports); ++port) {
3347 INIT_WORK(&devr->ports[port].pkey_change_work,
3348 pkey_change_handler);
3349 devr->ports[port].devr = devr;
3350 }
3351
Eli Cohene126ba92013-07-07 17:25:49 +03003352 return 0;
3353
Haggai Abramonvsky4aa17b22015-06-04 19:30:48 +03003354error5:
3355 mlx5_ib_destroy_srq(devr->s0);
Eli Cohene126ba92013-07-07 17:25:49 +03003356error4:
3357 mlx5_ib_dealloc_xrcd(devr->x1);
3358error3:
3359 mlx5_ib_dealloc_xrcd(devr->x0);
3360error2:
3361 mlx5_ib_destroy_cq(devr->c0);
3362error1:
3363 mlx5_ib_dealloc_pd(devr->p0);
3364error0:
3365 return ret;
3366}
3367
3368static void destroy_dev_resources(struct mlx5_ib_resources *devr)
3369{
Haggai Eran7722f472016-02-29 15:45:07 +02003370 struct mlx5_ib_dev *dev =
3371 container_of(devr, struct mlx5_ib_dev, devr);
3372 int port;
3373
Haggai Abramonvsky4aa17b22015-06-04 19:30:48 +03003374 mlx5_ib_destroy_srq(devr->s1);
Eli Cohene126ba92013-07-07 17:25:49 +03003375 mlx5_ib_destroy_srq(devr->s0);
3376 mlx5_ib_dealloc_xrcd(devr->x0);
3377 mlx5_ib_dealloc_xrcd(devr->x1);
3378 mlx5_ib_destroy_cq(devr->c0);
3379 mlx5_ib_dealloc_pd(devr->p0);
Haggai Eran7722f472016-02-29 15:45:07 +02003380
3381 /* Make sure no change P_Key work items are still executing */
3382 for (port = 0; port < dev->num_ports; ++port)
3383 cancel_work_sync(&devr->ports[port].pkey_change_work);
Eli Cohene126ba92013-07-07 17:25:49 +03003384}
3385
Achiad Shochate53505a2015-12-23 18:47:25 +02003386static u32 get_core_cap_flags(struct ib_device *ibdev)
3387{
3388 struct mlx5_ib_dev *dev = to_mdev(ibdev);
3389 enum rdma_link_layer ll = mlx5_ib_port_link_layer(ibdev, 1);
3390 u8 l3_type_cap = MLX5_CAP_ROCE(dev->mdev, l3_type);
3391 u8 roce_version_cap = MLX5_CAP_ROCE(dev->mdev, roce_version);
3392 u32 ret = 0;
3393
3394 if (ll == IB_LINK_LAYER_INFINIBAND)
3395 return RDMA_CORE_PORT_IBA_IB;
3396
Or Gerlitz72cd5712017-01-24 13:02:36 +02003397 ret = RDMA_CORE_PORT_RAW_PACKET;
3398
Achiad Shochate53505a2015-12-23 18:47:25 +02003399 if (!(l3_type_cap & MLX5_ROCE_L3_TYPE_IPV4_CAP))
Or Gerlitz72cd5712017-01-24 13:02:36 +02003400 return ret;
Achiad Shochate53505a2015-12-23 18:47:25 +02003401
3402 if (!(l3_type_cap & MLX5_ROCE_L3_TYPE_IPV6_CAP))
Or Gerlitz72cd5712017-01-24 13:02:36 +02003403 return ret;
Achiad Shochate53505a2015-12-23 18:47:25 +02003404
3405 if (roce_version_cap & MLX5_ROCE_VERSION_1_CAP)
3406 ret |= RDMA_CORE_PORT_IBA_ROCE;
3407
3408 if (roce_version_cap & MLX5_ROCE_VERSION_2_CAP)
3409 ret |= RDMA_CORE_PORT_IBA_ROCE_UDP_ENCAP;
3410
3411 return ret;
3412}
3413
Ira Weiny77386132015-05-13 20:02:58 -04003414static int mlx5_port_immutable(struct ib_device *ibdev, u8 port_num,
3415 struct ib_port_immutable *immutable)
3416{
3417 struct ib_port_attr attr;
Or Gerlitzca5b91d2016-11-27 16:51:36 +02003418 struct mlx5_ib_dev *dev = to_mdev(ibdev);
3419 enum rdma_link_layer ll = mlx5_ib_port_link_layer(ibdev, port_num);
Ira Weiny77386132015-05-13 20:02:58 -04003420 int err;
3421
Or Gerlitzc4550c62017-01-24 13:02:39 +02003422 immutable->core_cap_flags = get_core_cap_flags(ibdev);
3423
3424 err = ib_query_port(ibdev, port_num, &attr);
Ira Weiny77386132015-05-13 20:02:58 -04003425 if (err)
3426 return err;
3427
3428 immutable->pkey_tbl_len = attr.pkey_tbl_len;
3429 immutable->gid_tbl_len = attr.gid_tbl_len;
Achiad Shochate53505a2015-12-23 18:47:25 +02003430 immutable->core_cap_flags = get_core_cap_flags(ibdev);
Or Gerlitzca5b91d2016-11-27 16:51:36 +02003431 if ((ll == IB_LINK_LAYER_INFINIBAND) || MLX5_CAP_GEN(dev->mdev, roce))
3432 immutable->max_mad_size = IB_MGMT_MAD_SIZE;
Ira Weiny77386132015-05-13 20:02:58 -04003433
3434 return 0;
3435}
3436
Leon Romanovsky9abb0d12017-06-27 16:49:53 +03003437static void get_dev_fw_str(struct ib_device *ibdev, char *str)
Ira Weinyc7342822016-06-15 02:22:01 -04003438{
3439 struct mlx5_ib_dev *dev =
3440 container_of(ibdev, struct mlx5_ib_dev, ib_dev);
Leon Romanovsky9abb0d12017-06-27 16:49:53 +03003441 snprintf(str, IB_FW_VERSION_NAME_MAX, "%d.%d.%04d",
3442 fw_rev_maj(dev->mdev), fw_rev_min(dev->mdev),
3443 fw_rev_sub(dev->mdev));
Ira Weinyc7342822016-06-15 02:22:01 -04003444}
3445
Or Gerlitz45f95ac2016-11-27 16:51:35 +02003446static int mlx5_eth_lag_init(struct mlx5_ib_dev *dev)
Aviv Heller9ef9c642016-09-18 20:48:01 +03003447{
3448 struct mlx5_core_dev *mdev = dev->mdev;
3449 struct mlx5_flow_namespace *ns = mlx5_get_flow_namespace(mdev,
3450 MLX5_FLOW_NAMESPACE_LAG);
3451 struct mlx5_flow_table *ft;
3452 int err;
3453
3454 if (!ns || !mlx5_lag_is_active(mdev))
3455 return 0;
3456
3457 err = mlx5_cmd_create_vport_lag(mdev);
3458 if (err)
3459 return err;
3460
3461 ft = mlx5_create_lag_demux_flow_table(ns, 0, 0);
3462 if (IS_ERR(ft)) {
3463 err = PTR_ERR(ft);
3464 goto err_destroy_vport_lag;
3465 }
3466
3467 dev->flow_db.lag_demux_ft = ft;
3468 return 0;
3469
3470err_destroy_vport_lag:
3471 mlx5_cmd_destroy_vport_lag(mdev);
3472 return err;
3473}
3474
Or Gerlitz45f95ac2016-11-27 16:51:35 +02003475static void mlx5_eth_lag_cleanup(struct mlx5_ib_dev *dev)
Aviv Heller9ef9c642016-09-18 20:48:01 +03003476{
3477 struct mlx5_core_dev *mdev = dev->mdev;
3478
3479 if (dev->flow_db.lag_demux_ft) {
3480 mlx5_destroy_flow_table(dev->flow_db.lag_demux_ft);
3481 dev->flow_db.lag_demux_ft = NULL;
3482
3483 mlx5_cmd_destroy_vport_lag(mdev);
3484 }
3485}
3486
Or Gerlitzd012f5d2016-11-27 16:51:34 +02003487static int mlx5_add_netdev_notifier(struct mlx5_ib_dev *dev)
Achiad Shochatfc24fc52015-12-23 18:47:17 +02003488{
Achiad Shochate53505a2015-12-23 18:47:25 +02003489 int err;
3490
Achiad Shochatfc24fc52015-12-23 18:47:17 +02003491 dev->roce.nb.notifier_call = mlx5_netdev_event;
Achiad Shochate53505a2015-12-23 18:47:25 +02003492 err = register_netdevice_notifier(&dev->roce.nb);
Aviv Heller5ec8c832016-09-18 20:48:00 +03003493 if (err) {
3494 dev->roce.nb.notifier_call = NULL;
Achiad Shochate53505a2015-12-23 18:47:25 +02003495 return err;
Aviv Heller5ec8c832016-09-18 20:48:00 +03003496 }
Achiad Shochate53505a2015-12-23 18:47:25 +02003497
Or Gerlitzd012f5d2016-11-27 16:51:34 +02003498 return 0;
3499}
Achiad Shochate53505a2015-12-23 18:47:25 +02003500
Or Gerlitzd012f5d2016-11-27 16:51:34 +02003501static void mlx5_remove_netdev_notifier(struct mlx5_ib_dev *dev)
Eli Cohene126ba92013-07-07 17:25:49 +03003502{
3503 if (dev->roce.nb.notifier_call) {
3504 unregister_netdevice_notifier(&dev->roce.nb);
3505 dev->roce.nb.notifier_call = NULL;
3506 }
3507}
3508
Or Gerlitz45f95ac2016-11-27 16:51:35 +02003509static int mlx5_enable_eth(struct mlx5_ib_dev *dev)
Eli Cohene126ba92013-07-07 17:25:49 +03003510{
Eli Cohene126ba92013-07-07 17:25:49 +03003511 int err;
3512
Or Gerlitzd012f5d2016-11-27 16:51:34 +02003513 err = mlx5_add_netdev_notifier(dev);
3514 if (err)
Achiad Shochate53505a2015-12-23 18:47:25 +02003515 return err;
Achiad Shochate53505a2015-12-23 18:47:25 +02003516
Or Gerlitzca5b91d2016-11-27 16:51:36 +02003517 if (MLX5_CAP_GEN(dev->mdev, roce)) {
3518 err = mlx5_nic_vport_enable_roce(dev->mdev);
3519 if (err)
3520 goto err_unregister_netdevice_notifier;
3521 }
Achiad Shochate53505a2015-12-23 18:47:25 +02003522
Or Gerlitz45f95ac2016-11-27 16:51:35 +02003523 err = mlx5_eth_lag_init(dev);
Aviv Heller9ef9c642016-09-18 20:48:01 +03003524 if (err)
3525 goto err_disable_roce;
3526
Achiad Shochate53505a2015-12-23 18:47:25 +02003527 return 0;
3528
Aviv Heller9ef9c642016-09-18 20:48:01 +03003529err_disable_roce:
Or Gerlitzca5b91d2016-11-27 16:51:36 +02003530 if (MLX5_CAP_GEN(dev->mdev, roce))
3531 mlx5_nic_vport_disable_roce(dev->mdev);
Aviv Heller9ef9c642016-09-18 20:48:01 +03003532
Achiad Shochate53505a2015-12-23 18:47:25 +02003533err_unregister_netdevice_notifier:
Or Gerlitzd012f5d2016-11-27 16:51:34 +02003534 mlx5_remove_netdev_notifier(dev);
Achiad Shochate53505a2015-12-23 18:47:25 +02003535 return err;
Achiad Shochatfc24fc52015-12-23 18:47:17 +02003536}
3537
Or Gerlitz45f95ac2016-11-27 16:51:35 +02003538static void mlx5_disable_eth(struct mlx5_ib_dev *dev)
Achiad Shochatfc24fc52015-12-23 18:47:17 +02003539{
Or Gerlitz45f95ac2016-11-27 16:51:35 +02003540 mlx5_eth_lag_cleanup(dev);
Or Gerlitzca5b91d2016-11-27 16:51:36 +02003541 if (MLX5_CAP_GEN(dev->mdev, roce))
3542 mlx5_nic_vport_disable_roce(dev->mdev);
Achiad Shochatfc24fc52015-12-23 18:47:17 +02003543}
3544
Parav Pandite1f24a72017-04-16 07:29:29 +03003545struct mlx5_ib_counter {
Kamal Heib7c16f472017-01-18 15:25:09 +02003546 const char *name;
3547 size_t offset;
3548};
3549
3550#define INIT_Q_COUNTER(_name) \
3551 { .name = #_name, .offset = MLX5_BYTE_OFF(query_q_counter_out, _name)}
3552
Parav Pandite1f24a72017-04-16 07:29:29 +03003553static const struct mlx5_ib_counter basic_q_cnts[] = {
Kamal Heib7c16f472017-01-18 15:25:09 +02003554 INIT_Q_COUNTER(rx_write_requests),
3555 INIT_Q_COUNTER(rx_read_requests),
3556 INIT_Q_COUNTER(rx_atomic_requests),
3557 INIT_Q_COUNTER(out_of_buffer),
3558};
3559
Parav Pandite1f24a72017-04-16 07:29:29 +03003560static const struct mlx5_ib_counter out_of_seq_q_cnts[] = {
Kamal Heib7c16f472017-01-18 15:25:09 +02003561 INIT_Q_COUNTER(out_of_sequence),
3562};
3563
Parav Pandite1f24a72017-04-16 07:29:29 +03003564static const struct mlx5_ib_counter retrans_q_cnts[] = {
Kamal Heib7c16f472017-01-18 15:25:09 +02003565 INIT_Q_COUNTER(duplicate_request),
3566 INIT_Q_COUNTER(rnr_nak_retry_err),
3567 INIT_Q_COUNTER(packet_seq_err),
3568 INIT_Q_COUNTER(implied_nak_seq_err),
3569 INIT_Q_COUNTER(local_ack_timeout_err),
3570};
3571
Parav Pandite1f24a72017-04-16 07:29:29 +03003572#define INIT_CONG_COUNTER(_name) \
3573 { .name = #_name, .offset = \
3574 MLX5_BYTE_OFF(query_cong_statistics_out, _name ## _high)}
3575
3576static const struct mlx5_ib_counter cong_cnts[] = {
3577 INIT_CONG_COUNTER(rp_cnp_ignored),
3578 INIT_CONG_COUNTER(rp_cnp_handled),
3579 INIT_CONG_COUNTER(np_ecn_marked_roce_packets),
3580 INIT_CONG_COUNTER(np_cnp_sent),
3581};
3582
Parav Pandit58dcb602017-06-19 07:19:37 +03003583static const struct mlx5_ib_counter extended_err_cnts[] = {
3584 INIT_Q_COUNTER(resp_local_length_error),
3585 INIT_Q_COUNTER(resp_cqe_error),
3586 INIT_Q_COUNTER(req_cqe_error),
3587 INIT_Q_COUNTER(req_remote_invalid_request),
3588 INIT_Q_COUNTER(req_remote_access_errors),
3589 INIT_Q_COUNTER(resp_remote_access_errors),
3590 INIT_Q_COUNTER(resp_cqe_flush_error),
3591 INIT_Q_COUNTER(req_cqe_flush_error),
3592};
3593
Parav Pandite1f24a72017-04-16 07:29:29 +03003594static void mlx5_ib_dealloc_counters(struct mlx5_ib_dev *dev)
Mark Bloch0837e862016-06-17 15:10:55 +03003595{
3596 unsigned int i;
3597
Kamal Heib7c16f472017-01-18 15:25:09 +02003598 for (i = 0; i < dev->num_ports; i++) {
Mark Bloch0837e862016-06-17 15:10:55 +03003599 mlx5_core_dealloc_q_counter(dev->mdev,
Parav Pandite1f24a72017-04-16 07:29:29 +03003600 dev->port[i].cnts.set_id);
3601 kfree(dev->port[i].cnts.names);
3602 kfree(dev->port[i].cnts.offsets);
Kamal Heib7c16f472017-01-18 15:25:09 +02003603 }
3604}
3605
Parav Pandite1f24a72017-04-16 07:29:29 +03003606static int __mlx5_ib_alloc_counters(struct mlx5_ib_dev *dev,
3607 struct mlx5_ib_counters *cnts)
Kamal Heib7c16f472017-01-18 15:25:09 +02003608{
3609 u32 num_counters;
3610
3611 num_counters = ARRAY_SIZE(basic_q_cnts);
3612
3613 if (MLX5_CAP_GEN(dev->mdev, out_of_seq_cnt))
3614 num_counters += ARRAY_SIZE(out_of_seq_q_cnts);
3615
3616 if (MLX5_CAP_GEN(dev->mdev, retransmission_q_counters))
3617 num_counters += ARRAY_SIZE(retrans_q_cnts);
Parav Pandit58dcb602017-06-19 07:19:37 +03003618
3619 if (MLX5_CAP_GEN(dev->mdev, enhanced_error_q_counters))
3620 num_counters += ARRAY_SIZE(extended_err_cnts);
3621
Parav Pandite1f24a72017-04-16 07:29:29 +03003622 cnts->num_q_counters = num_counters;
Kamal Heib7c16f472017-01-18 15:25:09 +02003623
Parav Pandite1f24a72017-04-16 07:29:29 +03003624 if (MLX5_CAP_GEN(dev->mdev, cc_query_allowed)) {
3625 cnts->num_cong_counters = ARRAY_SIZE(cong_cnts);
3626 num_counters += ARRAY_SIZE(cong_cnts);
3627 }
3628
3629 cnts->names = kcalloc(num_counters, sizeof(cnts->names), GFP_KERNEL);
3630 if (!cnts->names)
Kamal Heib7c16f472017-01-18 15:25:09 +02003631 return -ENOMEM;
3632
Parav Pandite1f24a72017-04-16 07:29:29 +03003633 cnts->offsets = kcalloc(num_counters,
3634 sizeof(cnts->offsets), GFP_KERNEL);
3635 if (!cnts->offsets)
Kamal Heib7c16f472017-01-18 15:25:09 +02003636 goto err_names;
3637
Kamal Heib7c16f472017-01-18 15:25:09 +02003638 return 0;
3639
3640err_names:
Parav Pandite1f24a72017-04-16 07:29:29 +03003641 kfree(cnts->names);
Kamal Heib7c16f472017-01-18 15:25:09 +02003642 return -ENOMEM;
3643}
3644
Parav Pandite1f24a72017-04-16 07:29:29 +03003645static void mlx5_ib_fill_counters(struct mlx5_ib_dev *dev,
3646 const char **names,
3647 size_t *offsets)
Kamal Heib7c16f472017-01-18 15:25:09 +02003648{
3649 int i;
3650 int j = 0;
3651
3652 for (i = 0; i < ARRAY_SIZE(basic_q_cnts); i++, j++) {
3653 names[j] = basic_q_cnts[i].name;
3654 offsets[j] = basic_q_cnts[i].offset;
3655 }
3656
3657 if (MLX5_CAP_GEN(dev->mdev, out_of_seq_cnt)) {
3658 for (i = 0; i < ARRAY_SIZE(out_of_seq_q_cnts); i++, j++) {
3659 names[j] = out_of_seq_q_cnts[i].name;
3660 offsets[j] = out_of_seq_q_cnts[i].offset;
3661 }
3662 }
3663
3664 if (MLX5_CAP_GEN(dev->mdev, retransmission_q_counters)) {
3665 for (i = 0; i < ARRAY_SIZE(retrans_q_cnts); i++, j++) {
3666 names[j] = retrans_q_cnts[i].name;
3667 offsets[j] = retrans_q_cnts[i].offset;
3668 }
3669 }
Parav Pandite1f24a72017-04-16 07:29:29 +03003670
Parav Pandit58dcb602017-06-19 07:19:37 +03003671 if (MLX5_CAP_GEN(dev->mdev, enhanced_error_q_counters)) {
3672 for (i = 0; i < ARRAY_SIZE(extended_err_cnts); i++, j++) {
3673 names[j] = extended_err_cnts[i].name;
3674 offsets[j] = extended_err_cnts[i].offset;
3675 }
3676 }
3677
Parav Pandite1f24a72017-04-16 07:29:29 +03003678 if (MLX5_CAP_GEN(dev->mdev, cc_query_allowed)) {
3679 for (i = 0; i < ARRAY_SIZE(cong_cnts); i++, j++) {
3680 names[j] = cong_cnts[i].name;
3681 offsets[j] = cong_cnts[i].offset;
3682 }
3683 }
Mark Bloch0837e862016-06-17 15:10:55 +03003684}
3685
Parav Pandite1f24a72017-04-16 07:29:29 +03003686static int mlx5_ib_alloc_counters(struct mlx5_ib_dev *dev)
Mark Bloch0837e862016-06-17 15:10:55 +03003687{
3688 int i;
3689 int ret;
3690
3691 for (i = 0; i < dev->num_ports; i++) {
Kamal Heib7c16f472017-01-18 15:25:09 +02003692 struct mlx5_ib_port *port = &dev->port[i];
3693
Mark Bloch0837e862016-06-17 15:10:55 +03003694 ret = mlx5_core_alloc_q_counter(dev->mdev,
Parav Pandite1f24a72017-04-16 07:29:29 +03003695 &port->cnts.set_id);
Mark Bloch0837e862016-06-17 15:10:55 +03003696 if (ret) {
3697 mlx5_ib_warn(dev,
3698 "couldn't allocate queue counter for port %d, err %d\n",
3699 i + 1, ret);
3700 goto dealloc_counters;
3701 }
Kamal Heib7c16f472017-01-18 15:25:09 +02003702
Parav Pandite1f24a72017-04-16 07:29:29 +03003703 ret = __mlx5_ib_alloc_counters(dev, &port->cnts);
Kamal Heib7c16f472017-01-18 15:25:09 +02003704 if (ret)
3705 goto dealloc_counters;
3706
Parav Pandite1f24a72017-04-16 07:29:29 +03003707 mlx5_ib_fill_counters(dev, port->cnts.names,
3708 port->cnts.offsets);
Mark Bloch0837e862016-06-17 15:10:55 +03003709 }
3710
3711 return 0;
3712
3713dealloc_counters:
3714 while (--i >= 0)
3715 mlx5_core_dealloc_q_counter(dev->mdev,
Parav Pandite1f24a72017-04-16 07:29:29 +03003716 dev->port[i].cnts.set_id);
Mark Bloch0837e862016-06-17 15:10:55 +03003717
3718 return ret;
3719}
3720
Mark Bloch0ad17a82016-06-17 15:10:56 +03003721static struct rdma_hw_stats *mlx5_ib_alloc_hw_stats(struct ib_device *ibdev,
3722 u8 port_num)
3723{
Kamal Heib7c16f472017-01-18 15:25:09 +02003724 struct mlx5_ib_dev *dev = to_mdev(ibdev);
3725 struct mlx5_ib_port *port = &dev->port[port_num - 1];
Mark Bloch0ad17a82016-06-17 15:10:56 +03003726
3727 /* We support only per port stats */
3728 if (port_num == 0)
3729 return NULL;
3730
Parav Pandite1f24a72017-04-16 07:29:29 +03003731 return rdma_alloc_hw_stats_struct(port->cnts.names,
3732 port->cnts.num_q_counters +
3733 port->cnts.num_cong_counters,
Mark Bloch0ad17a82016-06-17 15:10:56 +03003734 RDMA_HW_STATS_DEFAULT_LIFESPAN);
3735}
3736
Parav Pandite1f24a72017-04-16 07:29:29 +03003737static int mlx5_ib_query_q_counters(struct mlx5_ib_dev *dev,
3738 struct mlx5_ib_port *port,
3739 struct rdma_hw_stats *stats)
3740{
3741 int outlen = MLX5_ST_SZ_BYTES(query_q_counter_out);
3742 void *out;
3743 __be32 val;
3744 int ret, i;
3745
Leon Romanovsky1b9a07e2017-05-10 21:32:18 +03003746 out = kvzalloc(outlen, GFP_KERNEL);
Parav Pandite1f24a72017-04-16 07:29:29 +03003747 if (!out)
3748 return -ENOMEM;
3749
3750 ret = mlx5_core_query_q_counter(dev->mdev,
3751 port->cnts.set_id, 0,
3752 out, outlen);
3753 if (ret)
3754 goto free;
3755
3756 for (i = 0; i < port->cnts.num_q_counters; i++) {
3757 val = *(__be32 *)(out + port->cnts.offsets[i]);
3758 stats->value[i] = (u64)be32_to_cpu(val);
3759 }
3760
3761free:
3762 kvfree(out);
3763 return ret;
3764}
3765
Mark Bloch0ad17a82016-06-17 15:10:56 +03003766static int mlx5_ib_get_hw_stats(struct ib_device *ibdev,
3767 struct rdma_hw_stats *stats,
Kamal Heib7c16f472017-01-18 15:25:09 +02003768 u8 port_num, int index)
Mark Bloch0ad17a82016-06-17 15:10:56 +03003769{
3770 struct mlx5_ib_dev *dev = to_mdev(ibdev);
Kamal Heib7c16f472017-01-18 15:25:09 +02003771 struct mlx5_ib_port *port = &dev->port[port_num - 1];
Parav Pandite1f24a72017-04-16 07:29:29 +03003772 int ret, num_counters;
Mark Bloch0ad17a82016-06-17 15:10:56 +03003773
Kamal Heib7c16f472017-01-18 15:25:09 +02003774 if (!stats)
Parav Pandite1f24a72017-04-16 07:29:29 +03003775 return -EINVAL;
Mark Bloch0ad17a82016-06-17 15:10:56 +03003776
Parav Pandite1f24a72017-04-16 07:29:29 +03003777 ret = mlx5_ib_query_q_counters(dev, port, stats);
Mark Bloch0ad17a82016-06-17 15:10:56 +03003778 if (ret)
Parav Pandite1f24a72017-04-16 07:29:29 +03003779 return ret;
3780 num_counters = port->cnts.num_q_counters;
Mark Bloch0ad17a82016-06-17 15:10:56 +03003781
Parav Pandite1f24a72017-04-16 07:29:29 +03003782 if (MLX5_CAP_GEN(dev->mdev, cc_query_allowed)) {
Majd Dibbiny71a0ff62017-12-21 17:38:26 +02003783 ret = mlx5_lag_query_cong_counters(dev->mdev,
3784 stats->value +
3785 port->cnts.num_q_counters,
3786 port->cnts.num_cong_counters,
3787 port->cnts.offsets +
3788 port->cnts.num_q_counters);
Parav Pandite1f24a72017-04-16 07:29:29 +03003789 if (ret)
3790 return ret;
3791 num_counters += port->cnts.num_cong_counters;
Mark Bloch0ad17a82016-06-17 15:10:56 +03003792 }
Kamal Heib7c16f472017-01-18 15:25:09 +02003793
Parav Pandite1f24a72017-04-16 07:29:29 +03003794 return num_counters;
Mark Bloch0ad17a82016-06-17 15:10:56 +03003795}
3796
Niranjana Vishwanathapura8e959602017-06-30 13:14:46 -07003797static void mlx5_ib_free_rdma_netdev(struct net_device *netdev)
3798{
3799 return mlx5_rdma_netdev_free(netdev);
3800}
3801
Erez Shitrit693dfd52017-04-27 17:01:34 +03003802static struct net_device*
3803mlx5_ib_alloc_rdma_netdev(struct ib_device *hca,
3804 u8 port_num,
3805 enum rdma_netdev_t type,
3806 const char *name,
3807 unsigned char name_assign_type,
3808 void (*setup)(struct net_device *))
3809{
Niranjana Vishwanathapura8e959602017-06-30 13:14:46 -07003810 struct net_device *netdev;
3811 struct rdma_netdev *rn;
3812
Erez Shitrit693dfd52017-04-27 17:01:34 +03003813 if (type != RDMA_NETDEV_IPOIB)
3814 return ERR_PTR(-EOPNOTSUPP);
3815
Niranjana Vishwanathapura8e959602017-06-30 13:14:46 -07003816 netdev = mlx5_rdma_netdev_alloc(to_mdev(hca)->mdev, hca,
3817 name, setup);
3818 if (likely(!IS_ERR_OR_NULL(netdev))) {
3819 rn = netdev_priv(netdev);
3820 rn->free_rdma_netdev = mlx5_ib_free_rdma_netdev;
3821 }
3822 return netdev;
Erez Shitrit693dfd52017-04-27 17:01:34 +03003823}
3824
Maor Gottliebfe248c32017-05-30 10:29:14 +03003825static void delay_drop_debugfs_cleanup(struct mlx5_ib_dev *dev)
3826{
3827 if (!dev->delay_drop.dbg)
3828 return;
3829 debugfs_remove_recursive(dev->delay_drop.dbg->dir_debugfs);
3830 kfree(dev->delay_drop.dbg);
3831 dev->delay_drop.dbg = NULL;
3832}
3833
Maor Gottlieb03404e82017-05-30 10:29:13 +03003834static void cancel_delay_drop(struct mlx5_ib_dev *dev)
3835{
3836 if (!(dev->ib_dev.attrs.raw_packet_caps & IB_RAW_PACKET_CAP_DELAY_DROP))
3837 return;
3838
3839 cancel_work_sync(&dev->delay_drop.delay_drop_work);
Maor Gottliebfe248c32017-05-30 10:29:14 +03003840 delay_drop_debugfs_cleanup(dev);
3841}
3842
3843static ssize_t delay_drop_timeout_read(struct file *filp, char __user *buf,
3844 size_t count, loff_t *pos)
3845{
3846 struct mlx5_ib_delay_drop *delay_drop = filp->private_data;
3847 char lbuf[20];
3848 int len;
3849
3850 len = snprintf(lbuf, sizeof(lbuf), "%u\n", delay_drop->timeout);
3851 return simple_read_from_buffer(buf, count, pos, lbuf, len);
3852}
3853
3854static ssize_t delay_drop_timeout_write(struct file *filp, const char __user *buf,
3855 size_t count, loff_t *pos)
3856{
3857 struct mlx5_ib_delay_drop *delay_drop = filp->private_data;
3858 u32 timeout;
3859 u32 var;
3860
3861 if (kstrtouint_from_user(buf, count, 0, &var))
3862 return -EFAULT;
3863
3864 timeout = min_t(u32, roundup(var, 100), MLX5_MAX_DELAY_DROP_TIMEOUT_MS *
3865 1000);
3866 if (timeout != var)
3867 mlx5_ib_dbg(delay_drop->dev, "Round delay drop timeout to %u usec\n",
3868 timeout);
3869
3870 delay_drop->timeout = timeout;
3871
3872 return count;
3873}
3874
3875static const struct file_operations fops_delay_drop_timeout = {
3876 .owner = THIS_MODULE,
3877 .open = simple_open,
3878 .write = delay_drop_timeout_write,
3879 .read = delay_drop_timeout_read,
3880};
3881
3882static int delay_drop_debugfs_init(struct mlx5_ib_dev *dev)
3883{
3884 struct mlx5_ib_dbg_delay_drop *dbg;
3885
3886 if (!mlx5_debugfs_root)
3887 return 0;
3888
3889 dbg = kzalloc(sizeof(*dbg), GFP_KERNEL);
3890 if (!dbg)
3891 return -ENOMEM;
3892
Sudip Mukherjeecbafad82017-09-18 12:28:48 +01003893 dev->delay_drop.dbg = dbg;
3894
Maor Gottliebfe248c32017-05-30 10:29:14 +03003895 dbg->dir_debugfs =
3896 debugfs_create_dir("delay_drop",
3897 dev->mdev->priv.dbg_root);
3898 if (!dbg->dir_debugfs)
Sudip Mukherjeecbafad82017-09-18 12:28:48 +01003899 goto out_debugfs;
Maor Gottliebfe248c32017-05-30 10:29:14 +03003900
3901 dbg->events_cnt_debugfs =
3902 debugfs_create_atomic_t("num_timeout_events", 0400,
3903 dbg->dir_debugfs,
3904 &dev->delay_drop.events_cnt);
3905 if (!dbg->events_cnt_debugfs)
3906 goto out_debugfs;
3907
3908 dbg->rqs_cnt_debugfs =
3909 debugfs_create_atomic_t("num_rqs", 0400,
3910 dbg->dir_debugfs,
3911 &dev->delay_drop.rqs_cnt);
3912 if (!dbg->rqs_cnt_debugfs)
3913 goto out_debugfs;
3914
3915 dbg->timeout_debugfs =
3916 debugfs_create_file("timeout", 0600,
3917 dbg->dir_debugfs,
3918 &dev->delay_drop,
3919 &fops_delay_drop_timeout);
3920 if (!dbg->timeout_debugfs)
3921 goto out_debugfs;
3922
3923 return 0;
3924
3925out_debugfs:
3926 delay_drop_debugfs_cleanup(dev);
3927 return -ENOMEM;
Maor Gottlieb03404e82017-05-30 10:29:13 +03003928}
3929
3930static void init_delay_drop(struct mlx5_ib_dev *dev)
3931{
3932 if (!(dev->ib_dev.attrs.raw_packet_caps & IB_RAW_PACKET_CAP_DELAY_DROP))
3933 return;
3934
3935 mutex_init(&dev->delay_drop.lock);
3936 dev->delay_drop.dev = dev;
3937 dev->delay_drop.activate = false;
3938 dev->delay_drop.timeout = MLX5_MAX_DELAY_DROP_TIMEOUT_MS * 1000;
3939 INIT_WORK(&dev->delay_drop.delay_drop_work, delay_drop_handler);
Maor Gottliebfe248c32017-05-30 10:29:14 +03003940 atomic_set(&dev->delay_drop.rqs_cnt, 0);
3941 atomic_set(&dev->delay_drop.events_cnt, 0);
3942
3943 if (delay_drop_debugfs_init(dev))
3944 mlx5_ib_warn(dev, "Failed to init delay drop debugfs\n");
Maor Gottlieb03404e82017-05-30 10:29:13 +03003945}
3946
Leon Romanovsky84305d712017-08-17 15:50:53 +03003947static const struct cpumask *
3948mlx5_ib_get_vector_affinity(struct ib_device *ibdev, int comp_vector)
Sagi Grimberg40b24402017-07-13 11:09:42 +03003949{
3950 struct mlx5_ib_dev *dev = to_mdev(ibdev);
3951
3952 return mlx5_get_vector_affinity(dev->mdev, comp_vector);
3953}
3954
Jack Morgenstein9603b612014-07-28 23:30:22 +03003955static void *mlx5_ib_add(struct mlx5_core_dev *mdev)
Eli Cohene126ba92013-07-07 17:25:49 +03003956{
Eli Cohene126ba92013-07-07 17:25:49 +03003957 struct mlx5_ib_dev *dev;
Achiad Shochatebd61f62015-12-23 18:47:16 +02003958 enum rdma_link_layer ll;
3959 int port_type_cap;
Aviv Heller4babcf92016-09-18 20:48:03 +03003960 const char *name;
Eli Cohene126ba92013-07-07 17:25:49 +03003961 int err;
3962 int i;
3963
Achiad Shochatebd61f62015-12-23 18:47:16 +02003964 port_type_cap = MLX5_CAP_GEN(mdev, port_type);
3965 ll = mlx5_port_type_cap_to_rdma_ll(port_type_cap);
3966
Eli Cohene126ba92013-07-07 17:25:49 +03003967 printk_once(KERN_INFO "%s", mlx5_version);
3968
3969 dev = (struct mlx5_ib_dev *)ib_alloc_device(sizeof(*dev));
3970 if (!dev)
Jack Morgenstein9603b612014-07-28 23:30:22 +03003971 return NULL;
Eli Cohene126ba92013-07-07 17:25:49 +03003972
Jack Morgenstein9603b612014-07-28 23:30:22 +03003973 dev->mdev = mdev;
Eli Cohene126ba92013-07-07 17:25:49 +03003974
Mark Bloch0837e862016-06-17 15:10:55 +03003975 dev->port = kcalloc(MLX5_CAP_GEN(mdev, num_ports), sizeof(*dev->port),
3976 GFP_KERNEL);
3977 if (!dev->port)
3978 goto err_dealloc;
3979
Achiad Shochatfc24fc52015-12-23 18:47:17 +02003980 rwlock_init(&dev->roce.netdev_lock);
Eli Cohene126ba92013-07-07 17:25:49 +03003981 err = get_port_caps(dev);
3982 if (err)
Mark Bloch0837e862016-06-17 15:10:55 +03003983 goto err_free_port;
Eli Cohene126ba92013-07-07 17:25:49 +03003984
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03003985 if (mlx5_use_mad_ifc(dev))
3986 get_ext_port_caps(dev);
Eli Cohene126ba92013-07-07 17:25:49 +03003987
Aviv Heller4babcf92016-09-18 20:48:03 +03003988 if (!mlx5_lag_is_active(mdev))
3989 name = "mlx5_%d";
3990 else
3991 name = "mlx5_bond_%d";
3992
3993 strlcpy(dev->ib_dev.name, name, IB_DEVICE_NAME_MAX);
Eli Cohene126ba92013-07-07 17:25:49 +03003994 dev->ib_dev.owner = THIS_MODULE;
3995 dev->ib_dev.node_type = RDMA_NODE_IB_CA;
Sagi Grimbergc6790aa2015-09-24 10:34:23 +03003996 dev->ib_dev.local_dma_lkey = 0 /* not supported for now */;
Saeed Mahameed938fe832015-05-28 22:28:41 +03003997 dev->num_ports = MLX5_CAP_GEN(mdev, num_ports);
Eli Cohene126ba92013-07-07 17:25:49 +03003998 dev->ib_dev.phys_port_cnt = dev->num_ports;
Saeed Mahameed233d05d2015-04-02 17:07:32 +03003999 dev->ib_dev.num_comp_vectors =
4000 dev->mdev->priv.eq_table.num_comp_vectors;
Bart Van Assche9b0c2892017-01-20 13:04:21 -08004001 dev->ib_dev.dev.parent = &mdev->pdev->dev;
Eli Cohene126ba92013-07-07 17:25:49 +03004002
4003 dev->ib_dev.uverbs_abi_ver = MLX5_IB_UVERBS_ABI_VERSION;
4004 dev->ib_dev.uverbs_cmd_mask =
4005 (1ull << IB_USER_VERBS_CMD_GET_CONTEXT) |
4006 (1ull << IB_USER_VERBS_CMD_QUERY_DEVICE) |
4007 (1ull << IB_USER_VERBS_CMD_QUERY_PORT) |
4008 (1ull << IB_USER_VERBS_CMD_ALLOC_PD) |
4009 (1ull << IB_USER_VERBS_CMD_DEALLOC_PD) |
Moni Shoua41c450f2016-11-23 08:23:26 +02004010 (1ull << IB_USER_VERBS_CMD_CREATE_AH) |
4011 (1ull << IB_USER_VERBS_CMD_DESTROY_AH) |
Eli Cohene126ba92013-07-07 17:25:49 +03004012 (1ull << IB_USER_VERBS_CMD_REG_MR) |
Noa Osherovich56e11d62016-02-29 16:46:51 +02004013 (1ull << IB_USER_VERBS_CMD_REREG_MR) |
Eli Cohene126ba92013-07-07 17:25:49 +03004014 (1ull << IB_USER_VERBS_CMD_DEREG_MR) |
4015 (1ull << IB_USER_VERBS_CMD_CREATE_COMP_CHANNEL) |
4016 (1ull << IB_USER_VERBS_CMD_CREATE_CQ) |
4017 (1ull << IB_USER_VERBS_CMD_RESIZE_CQ) |
4018 (1ull << IB_USER_VERBS_CMD_DESTROY_CQ) |
4019 (1ull << IB_USER_VERBS_CMD_CREATE_QP) |
4020 (1ull << IB_USER_VERBS_CMD_MODIFY_QP) |
4021 (1ull << IB_USER_VERBS_CMD_QUERY_QP) |
4022 (1ull << IB_USER_VERBS_CMD_DESTROY_QP) |
4023 (1ull << IB_USER_VERBS_CMD_ATTACH_MCAST) |
4024 (1ull << IB_USER_VERBS_CMD_DETACH_MCAST) |
4025 (1ull << IB_USER_VERBS_CMD_CREATE_SRQ) |
4026 (1ull << IB_USER_VERBS_CMD_MODIFY_SRQ) |
4027 (1ull << IB_USER_VERBS_CMD_QUERY_SRQ) |
4028 (1ull << IB_USER_VERBS_CMD_DESTROY_SRQ) |
4029 (1ull << IB_USER_VERBS_CMD_CREATE_XSRQ) |
4030 (1ull << IB_USER_VERBS_CMD_OPEN_QP);
Haggai Eran1707cb42015-02-08 13:28:52 +02004031 dev->ib_dev.uverbs_ex_cmd_mask =
Matan Barakd4584dd2016-01-28 17:51:46 +02004032 (1ull << IB_USER_VERBS_EX_CMD_QUERY_DEVICE) |
4033 (1ull << IB_USER_VERBS_EX_CMD_CREATE_CQ) |
Bodong Wang7d29f342016-12-01 13:43:16 +02004034 (1ull << IB_USER_VERBS_EX_CMD_CREATE_QP) |
Yonatan Cohenb0e9df62017-11-13 10:51:15 +02004035 (1ull << IB_USER_VERBS_EX_CMD_MODIFY_QP) |
4036 (1ull << IB_USER_VERBS_EX_CMD_MODIFY_CQ);
Eli Cohene126ba92013-07-07 17:25:49 +03004037
4038 dev->ib_dev.query_device = mlx5_ib_query_device;
4039 dev->ib_dev.query_port = mlx5_ib_query_port;
Achiad Shochatebd61f62015-12-23 18:47:16 +02004040 dev->ib_dev.get_link_layer = mlx5_ib_port_link_layer;
Achiad Shochatfc24fc52015-12-23 18:47:17 +02004041 if (ll == IB_LINK_LAYER_ETHERNET)
4042 dev->ib_dev.get_netdev = mlx5_ib_get_netdev;
Eli Cohene126ba92013-07-07 17:25:49 +03004043 dev->ib_dev.query_gid = mlx5_ib_query_gid;
Achiad Shochat3cca2602015-12-23 18:47:23 +02004044 dev->ib_dev.add_gid = mlx5_ib_add_gid;
4045 dev->ib_dev.del_gid = mlx5_ib_del_gid;
Eli Cohene126ba92013-07-07 17:25:49 +03004046 dev->ib_dev.query_pkey = mlx5_ib_query_pkey;
4047 dev->ib_dev.modify_device = mlx5_ib_modify_device;
4048 dev->ib_dev.modify_port = mlx5_ib_modify_port;
4049 dev->ib_dev.alloc_ucontext = mlx5_ib_alloc_ucontext;
4050 dev->ib_dev.dealloc_ucontext = mlx5_ib_dealloc_ucontext;
4051 dev->ib_dev.mmap = mlx5_ib_mmap;
4052 dev->ib_dev.alloc_pd = mlx5_ib_alloc_pd;
4053 dev->ib_dev.dealloc_pd = mlx5_ib_dealloc_pd;
4054 dev->ib_dev.create_ah = mlx5_ib_create_ah;
4055 dev->ib_dev.query_ah = mlx5_ib_query_ah;
4056 dev->ib_dev.destroy_ah = mlx5_ib_destroy_ah;
4057 dev->ib_dev.create_srq = mlx5_ib_create_srq;
4058 dev->ib_dev.modify_srq = mlx5_ib_modify_srq;
4059 dev->ib_dev.query_srq = mlx5_ib_query_srq;
4060 dev->ib_dev.destroy_srq = mlx5_ib_destroy_srq;
4061 dev->ib_dev.post_srq_recv = mlx5_ib_post_srq_recv;
4062 dev->ib_dev.create_qp = mlx5_ib_create_qp;
4063 dev->ib_dev.modify_qp = mlx5_ib_modify_qp;
4064 dev->ib_dev.query_qp = mlx5_ib_query_qp;
4065 dev->ib_dev.destroy_qp = mlx5_ib_destroy_qp;
4066 dev->ib_dev.post_send = mlx5_ib_post_send;
4067 dev->ib_dev.post_recv = mlx5_ib_post_recv;
4068 dev->ib_dev.create_cq = mlx5_ib_create_cq;
4069 dev->ib_dev.modify_cq = mlx5_ib_modify_cq;
4070 dev->ib_dev.resize_cq = mlx5_ib_resize_cq;
4071 dev->ib_dev.destroy_cq = mlx5_ib_destroy_cq;
4072 dev->ib_dev.poll_cq = mlx5_ib_poll_cq;
4073 dev->ib_dev.req_notify_cq = mlx5_ib_arm_cq;
4074 dev->ib_dev.get_dma_mr = mlx5_ib_get_dma_mr;
4075 dev->ib_dev.reg_user_mr = mlx5_ib_reg_user_mr;
Noa Osherovich56e11d62016-02-29 16:46:51 +02004076 dev->ib_dev.rereg_user_mr = mlx5_ib_rereg_user_mr;
Eli Cohene126ba92013-07-07 17:25:49 +03004077 dev->ib_dev.dereg_mr = mlx5_ib_dereg_mr;
4078 dev->ib_dev.attach_mcast = mlx5_ib_mcg_attach;
4079 dev->ib_dev.detach_mcast = mlx5_ib_mcg_detach;
4080 dev->ib_dev.process_mad = mlx5_ib_process_mad;
Sagi Grimberg9bee1782015-07-30 10:32:35 +03004081 dev->ib_dev.alloc_mr = mlx5_ib_alloc_mr;
Sagi Grimberg8a187ee2015-10-13 19:11:26 +03004082 dev->ib_dev.map_mr_sg = mlx5_ib_map_mr_sg;
Sagi Grimbergd5436ba2014-02-23 14:19:12 +02004083 dev->ib_dev.check_mr_status = mlx5_ib_check_mr_status;
Ira Weiny77386132015-05-13 20:02:58 -04004084 dev->ib_dev.get_port_immutable = mlx5_port_immutable;
Ira Weinyc7342822016-06-15 02:22:01 -04004085 dev->ib_dev.get_dev_fw_str = get_dev_fw_str;
Sagi Grimberg40b24402017-07-13 11:09:42 +03004086 dev->ib_dev.get_vector_affinity = mlx5_ib_get_vector_affinity;
Niranjana Vishwanathapura8e959602017-06-30 13:14:46 -07004087 if (MLX5_CAP_GEN(mdev, ipoib_enhanced_offloads))
Alex Vesker022d0382017-06-14 09:59:06 +03004088 dev->ib_dev.alloc_rdma_netdev = mlx5_ib_alloc_rdma_netdev;
Niranjana Vishwanathapura8e959602017-06-30 13:14:46 -07004089
Eli Coheneff901d2016-03-11 22:58:42 +02004090 if (mlx5_core_is_pf(mdev)) {
4091 dev->ib_dev.get_vf_config = mlx5_ib_get_vf_config;
4092 dev->ib_dev.set_vf_link_state = mlx5_ib_set_vf_link_state;
4093 dev->ib_dev.get_vf_stats = mlx5_ib_get_vf_stats;
4094 dev->ib_dev.set_vf_guid = mlx5_ib_set_vf_guid;
4095 }
Eli Cohene126ba92013-07-07 17:25:49 +03004096
Maor Gottlieb7c2344c2016-06-17 14:56:44 +03004097 dev->ib_dev.disassociate_ucontext = mlx5_ib_disassociate_ucontext;
4098
Saeed Mahameed938fe832015-05-28 22:28:41 +03004099 mlx5_ib_internal_fill_odp_caps(dev);
Haggai Eran8cdd3122014-12-11 17:04:20 +02004100
Max Gurtovoy6e8484c2017-05-28 10:53:11 +03004101 dev->umr_fence = mlx5_get_umr_fence(MLX5_CAP_GEN(mdev, umr_fence));
4102
Matan Barakd2370e02016-02-29 18:05:30 +02004103 if (MLX5_CAP_GEN(mdev, imaicl)) {
4104 dev->ib_dev.alloc_mw = mlx5_ib_alloc_mw;
4105 dev->ib_dev.dealloc_mw = mlx5_ib_dealloc_mw;
4106 dev->ib_dev.uverbs_cmd_mask |=
4107 (1ull << IB_USER_VERBS_CMD_ALLOC_MW) |
4108 (1ull << IB_USER_VERBS_CMD_DEALLOC_MW);
4109 }
4110
Kamal Heib7c16f472017-01-18 15:25:09 +02004111 if (MLX5_CAP_GEN(dev->mdev, max_qp_cnt)) {
Mark Bloch0ad17a82016-06-17 15:10:56 +03004112 dev->ib_dev.get_hw_stats = mlx5_ib_get_hw_stats;
4113 dev->ib_dev.alloc_hw_stats = mlx5_ib_alloc_hw_stats;
4114 }
4115
Saeed Mahameed938fe832015-05-28 22:28:41 +03004116 if (MLX5_CAP_GEN(mdev, xrc)) {
Eli Cohene126ba92013-07-07 17:25:49 +03004117 dev->ib_dev.alloc_xrcd = mlx5_ib_alloc_xrcd;
4118 dev->ib_dev.dealloc_xrcd = mlx5_ib_dealloc_xrcd;
4119 dev->ib_dev.uverbs_cmd_mask |=
4120 (1ull << IB_USER_VERBS_CMD_OPEN_XRCD) |
4121 (1ull << IB_USER_VERBS_CMD_CLOSE_XRCD);
4122 }
4123
Yishai Hadas81e30882017-06-08 16:15:09 +03004124 dev->ib_dev.create_flow = mlx5_ib_create_flow;
4125 dev->ib_dev.destroy_flow = mlx5_ib_destroy_flow;
4126 dev->ib_dev.uverbs_ex_cmd_mask |=
4127 (1ull << IB_USER_VERBS_EX_CMD_CREATE_FLOW) |
4128 (1ull << IB_USER_VERBS_EX_CMD_DESTROY_FLOW);
4129
Linus Torvalds048ccca2016-01-23 18:45:06 -08004130 if (mlx5_ib_port_link_layer(&dev->ib_dev, 1) ==
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02004131 IB_LINK_LAYER_ETHERNET) {
Yishai Hadas79b20a62016-05-23 15:20:50 +03004132 dev->ib_dev.create_wq = mlx5_ib_create_wq;
4133 dev->ib_dev.modify_wq = mlx5_ib_modify_wq;
4134 dev->ib_dev.destroy_wq = mlx5_ib_destroy_wq;
Yishai Hadasc5f90922016-05-23 15:20:53 +03004135 dev->ib_dev.create_rwq_ind_table = mlx5_ib_create_rwq_ind_table;
4136 dev->ib_dev.destroy_rwq_ind_table = mlx5_ib_destroy_rwq_ind_table;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02004137 dev->ib_dev.uverbs_ex_cmd_mask |=
Yishai Hadas79b20a62016-05-23 15:20:50 +03004138 (1ull << IB_USER_VERBS_EX_CMD_CREATE_WQ) |
4139 (1ull << IB_USER_VERBS_EX_CMD_MODIFY_WQ) |
Yishai Hadasc5f90922016-05-23 15:20:53 +03004140 (1ull << IB_USER_VERBS_EX_CMD_DESTROY_WQ) |
4141 (1ull << IB_USER_VERBS_EX_CMD_CREATE_RWQ_IND_TBL) |
4142 (1ull << IB_USER_VERBS_EX_CMD_DESTROY_RWQ_IND_TBL);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02004143 }
Eli Cohene126ba92013-07-07 17:25:49 +03004144 err = init_node_data(dev);
4145 if (err)
Majd Dibbiny90be7c82016-10-27 16:36:39 +03004146 goto err_free_port;
Eli Cohene126ba92013-07-07 17:25:49 +03004147
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02004148 mutex_init(&dev->flow_db.lock);
Eli Cohene126ba92013-07-07 17:25:49 +03004149 mutex_init(&dev->cap_mask_mutex);
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03004150 INIT_LIST_HEAD(&dev->qp_list);
4151 spin_lock_init(&dev->reset_flow_resource_lock);
Eli Cohene126ba92013-07-07 17:25:49 +03004152
Achiad Shochatfc24fc52015-12-23 18:47:17 +02004153 if (ll == IB_LINK_LAYER_ETHERNET) {
Or Gerlitz45f95ac2016-11-27 16:51:35 +02004154 err = mlx5_enable_eth(dev);
Achiad Shochatfc24fc52015-12-23 18:47:17 +02004155 if (err)
Majd Dibbiny90be7c82016-10-27 16:36:39 +03004156 goto err_free_port;
Moni Shouafd65f1b2017-05-30 09:56:05 +03004157 dev->roce.last_port_state = IB_PORT_DOWN;
Achiad Shochatfc24fc52015-12-23 18:47:17 +02004158 }
4159
Eli Cohene126ba92013-07-07 17:25:49 +03004160 err = create_dev_resources(&dev->devr);
4161 if (err)
Or Gerlitz45f95ac2016-11-27 16:51:35 +02004162 goto err_disable_eth;
Eli Cohene126ba92013-07-07 17:25:49 +03004163
Haggai Eran6aec21f2014-12-11 17:04:23 +02004164 err = mlx5_ib_odp_init_one(dev);
Wei Yongjun281d1a92013-07-30 07:54:26 +08004165 if (err)
Eli Cohene126ba92013-07-07 17:25:49 +03004166 goto err_rsrc;
4167
Kamal Heib45bded22017-01-18 14:10:32 +02004168 if (MLX5_CAP_GEN(dev->mdev, max_qp_cnt)) {
Parav Pandite1f24a72017-04-16 07:29:29 +03004169 err = mlx5_ib_alloc_counters(dev);
Kamal Heib45bded22017-01-18 14:10:32 +02004170 if (err)
4171 goto err_odp;
4172 }
Haggai Eran6aec21f2014-12-11 17:04:23 +02004173
Parav Pandit4a2da0b2017-05-30 10:05:15 +03004174 err = mlx5_ib_init_cong_debugfs(dev);
4175 if (err)
4176 goto err_cnt;
4177
Eli Cohen5fe9dec2017-01-03 23:55:25 +02004178 dev->mdev->priv.uar = mlx5_get_uars_page(dev->mdev);
4179 if (!dev->mdev->priv.uar)
Parav Pandit4a2da0b2017-05-30 10:05:15 +03004180 goto err_cong;
Eli Cohen5fe9dec2017-01-03 23:55:25 +02004181
4182 err = mlx5_alloc_bfreg(dev->mdev, &dev->bfreg, false, false);
4183 if (err)
4184 goto err_uar_page;
4185
4186 err = mlx5_alloc_bfreg(dev->mdev, &dev->fp_bfreg, false, true);
4187 if (err)
4188 goto err_bfreg;
4189
Mark Bloch0837e862016-06-17 15:10:55 +03004190 err = ib_register_device(&dev->ib_dev, NULL);
4191 if (err)
Eli Cohen5fe9dec2017-01-03 23:55:25 +02004192 goto err_fp_bfreg;
Mark Bloch0837e862016-06-17 15:10:55 +03004193
Eli Cohene126ba92013-07-07 17:25:49 +03004194 err = create_umr_res(dev);
4195 if (err)
4196 goto err_dev;
4197
Maor Gottlieb03404e82017-05-30 10:29:13 +03004198 init_delay_drop(dev);
4199
Eli Cohene126ba92013-07-07 17:25:49 +03004200 for (i = 0; i < ARRAY_SIZE(mlx5_class_attributes); i++) {
Wei Yongjun281d1a92013-07-30 07:54:26 +08004201 err = device_create_file(&dev->ib_dev.dev,
4202 mlx5_class_attributes[i]);
4203 if (err)
Maor Gottlieb03404e82017-05-30 10:29:13 +03004204 goto err_delay_drop;
Eli Cohene126ba92013-07-07 17:25:49 +03004205 }
4206
Huy Nguyenc85023e2017-05-30 09:42:54 +03004207 if ((MLX5_CAP_GEN(mdev, port_type) == MLX5_CAP_PORT_TYPE_ETH) &&
4208 MLX5_CAP_GEN(mdev, disable_local_lb))
4209 mutex_init(&dev->lb_mutex);
4210
Eli Cohene126ba92013-07-07 17:25:49 +03004211 dev->ib_active = true;
4212
Jack Morgenstein9603b612014-07-28 23:30:22 +03004213 return dev;
Eli Cohene126ba92013-07-07 17:25:49 +03004214
Maor Gottlieb03404e82017-05-30 10:29:13 +03004215err_delay_drop:
4216 cancel_delay_drop(dev);
Eli Cohene126ba92013-07-07 17:25:49 +03004217 destroy_umrc_res(dev);
4218
4219err_dev:
4220 ib_unregister_device(&dev->ib_dev);
4221
Eli Cohen5fe9dec2017-01-03 23:55:25 +02004222err_fp_bfreg:
4223 mlx5_free_bfreg(dev->mdev, &dev->fp_bfreg);
4224
4225err_bfreg:
4226 mlx5_free_bfreg(dev->mdev, &dev->bfreg);
4227
4228err_uar_page:
4229 mlx5_put_uars_page(dev->mdev, dev->mdev->priv.uar);
4230
Parav Pandit4a2da0b2017-05-30 10:05:15 +03004231err_cong:
Parav Pandite19cd282017-10-01 09:54:35 +03004232 mlx5_ib_cleanup_cong_debugfs(dev);
4233err_cnt:
Kamal Heib45bded22017-01-18 14:10:32 +02004234 if (MLX5_CAP_GEN(dev->mdev, max_qp_cnt))
Parav Pandite1f24a72017-04-16 07:29:29 +03004235 mlx5_ib_dealloc_counters(dev);
Mark Bloch0837e862016-06-17 15:10:55 +03004236
Haggai Eran6aec21f2014-12-11 17:04:23 +02004237err_odp:
4238 mlx5_ib_odp_remove_one(dev);
4239
Eli Cohene126ba92013-07-07 17:25:49 +03004240err_rsrc:
4241 destroy_dev_resources(&dev->devr);
4242
Or Gerlitz45f95ac2016-11-27 16:51:35 +02004243err_disable_eth:
Aviv Heller5ec8c832016-09-18 20:48:00 +03004244 if (ll == IB_LINK_LAYER_ETHERNET) {
Or Gerlitz45f95ac2016-11-27 16:51:35 +02004245 mlx5_disable_eth(dev);
Or Gerlitzd012f5d2016-11-27 16:51:34 +02004246 mlx5_remove_netdev_notifier(dev);
Aviv Heller5ec8c832016-09-18 20:48:00 +03004247 }
Achiad Shochatfc24fc52015-12-23 18:47:17 +02004248
Mark Bloch0837e862016-06-17 15:10:55 +03004249err_free_port:
4250 kfree(dev->port);
4251
Jack Morgenstein9603b612014-07-28 23:30:22 +03004252err_dealloc:
Eli Cohene126ba92013-07-07 17:25:49 +03004253 ib_dealloc_device((struct ib_device *)dev);
4254
Jack Morgenstein9603b612014-07-28 23:30:22 +03004255 return NULL;
Eli Cohene126ba92013-07-07 17:25:49 +03004256}
4257
Jack Morgenstein9603b612014-07-28 23:30:22 +03004258static void mlx5_ib_remove(struct mlx5_core_dev *mdev, void *context)
Eli Cohene126ba92013-07-07 17:25:49 +03004259{
Jack Morgenstein9603b612014-07-28 23:30:22 +03004260 struct mlx5_ib_dev *dev = context;
Achiad Shochatfc24fc52015-12-23 18:47:17 +02004261 enum rdma_link_layer ll = mlx5_ib_port_link_layer(&dev->ib_dev, 1);
Haggai Eran6aec21f2014-12-11 17:04:23 +02004262
Maor Gottlieb03404e82017-05-30 10:29:13 +03004263 cancel_delay_drop(dev);
Or Gerlitzd012f5d2016-11-27 16:51:34 +02004264 mlx5_remove_netdev_notifier(dev);
Eli Cohene126ba92013-07-07 17:25:49 +03004265 ib_unregister_device(&dev->ib_dev);
Eli Cohen5fe9dec2017-01-03 23:55:25 +02004266 mlx5_free_bfreg(dev->mdev, &dev->fp_bfreg);
4267 mlx5_free_bfreg(dev->mdev, &dev->bfreg);
4268 mlx5_put_uars_page(dev->mdev, mdev->priv.uar);
Parav Pandit4a2da0b2017-05-30 10:05:15 +03004269 mlx5_ib_cleanup_cong_debugfs(dev);
Kamal Heib45bded22017-01-18 14:10:32 +02004270 if (MLX5_CAP_GEN(dev->mdev, max_qp_cnt))
Parav Pandite1f24a72017-04-16 07:29:29 +03004271 mlx5_ib_dealloc_counters(dev);
Eli Coheneefd56e2014-09-14 16:47:50 +03004272 destroy_umrc_res(dev);
Haggai Eran6aec21f2014-12-11 17:04:23 +02004273 mlx5_ib_odp_remove_one(dev);
Eli Cohene126ba92013-07-07 17:25:49 +03004274 destroy_dev_resources(&dev->devr);
Achiad Shochatfc24fc52015-12-23 18:47:17 +02004275 if (ll == IB_LINK_LAYER_ETHERNET)
Or Gerlitz45f95ac2016-11-27 16:51:35 +02004276 mlx5_disable_eth(dev);
Mark Bloch0837e862016-06-17 15:10:55 +03004277 kfree(dev->port);
Eli Cohene126ba92013-07-07 17:25:49 +03004278 ib_dealloc_device(&dev->ib_dev);
4279}
4280
Jack Morgenstein9603b612014-07-28 23:30:22 +03004281static struct mlx5_interface mlx5_ib_interface = {
4282 .add = mlx5_ib_add,
4283 .remove = mlx5_ib_remove,
4284 .event = mlx5_ib_event,
Artemy Kovalyovd9aaed82017-01-02 11:37:46 +02004285#ifdef CONFIG_INFINIBAND_ON_DEMAND_PAGING
4286 .pfault = mlx5_ib_pfault,
4287#endif
Saeed Mahameed64613d942015-04-02 17:07:34 +03004288 .protocol = MLX5_INTERFACE_PROTOCOL_IB,
Eli Cohene126ba92013-07-07 17:25:49 +03004289};
4290
4291static int __init mlx5_ib_init(void)
4292{
Haggai Eran6aec21f2014-12-11 17:04:23 +02004293 int err;
4294
Artemy Kovalyov81713d32017-01-18 16:58:11 +02004295 mlx5_ib_odp_init();
Jack Morgenstein9603b612014-07-28 23:30:22 +03004296
Haggai Eran6aec21f2014-12-11 17:04:23 +02004297 err = mlx5_register_interface(&mlx5_ib_interface);
Haggai Eran6aec21f2014-12-11 17:04:23 +02004298
4299 return err;
Eli Cohene126ba92013-07-07 17:25:49 +03004300}
4301
4302static void __exit mlx5_ib_cleanup(void)
4303{
Jack Morgenstein9603b612014-07-28 23:30:22 +03004304 mlx5_unregister_interface(&mlx5_ib_interface);
Eli Cohene126ba92013-07-07 17:25:49 +03004305}
4306
4307module_init(mlx5_ib_init);
4308module_exit(mlx5_ib_cleanup);