blob: fe435edf305b6924ea21219ba39584c4052af2e6 [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 |
683 MLX5_RX_HASH_DST_PORT_UDP;
684 resp.response_length += sizeof(resp.rss_caps);
685 }
686 } else {
687 if (field_avail(typeof(resp), tso_caps, uhw->outlen))
688 resp.response_length += sizeof(resp.tso_caps);
689 if (field_avail(typeof(resp), rss_caps, uhw->outlen))
690 resp.response_length += sizeof(resp.rss_caps);
Bodong Wang402ca532016-06-17 15:02:20 +0300691 }
692
Erez Shitritf0313962016-02-21 16:27:17 +0200693 if (MLX5_CAP_GEN(mdev, ipoib_basic_offloads)) {
694 props->device_cap_flags |= IB_DEVICE_UD_IP_CSUM;
695 props->device_cap_flags |= IB_DEVICE_UD_TSO;
696 }
697
Maor Gottlieb03404e82017-05-30 10:29:13 +0300698 if (MLX5_CAP_GEN(dev->mdev, rq_delay_drop) &&
699 MLX5_CAP_GEN(dev->mdev, general_notification_event))
700 props->raw_packet_caps |= IB_RAW_PACKET_CAP_DELAY_DROP;
701
Yishai Hadas1d54f892017-06-08 16:15:11 +0300702 if (MLX5_CAP_GEN(mdev, ipoib_enhanced_offloads) &&
703 MLX5_CAP_IPOIB_ENHANCED(mdev, csum_cap))
704 props->device_cap_flags |= IB_DEVICE_UD_IP_CSUM;
705
Majd Dibbinycff5a0f2016-04-17 17:19:38 +0300706 if (MLX5_CAP_GEN(dev->mdev, eth_net_offloads) &&
Noa Osheroviche8161332017-01-18 15:40:01 +0200707 MLX5_CAP_ETH(dev->mdev, scatter_fcs)) {
708 /* Legacy bit to support old userspace libraries */
Majd Dibbinycff5a0f2016-04-17 17:19:38 +0300709 props->device_cap_flags |= IB_DEVICE_RAW_SCATTER_FCS;
Noa Osheroviche8161332017-01-18 15:40:01 +0200710 props->raw_packet_caps |= IB_RAW_PACKET_CAP_SCATTER_FCS;
711 }
Majd Dibbinycff5a0f2016-04-17 17:19:38 +0300712
Maor Gottliebda6d6ba32016-06-04 15:15:28 +0300713 if (mlx5_get_flow_namespace(dev->mdev, MLX5_FLOW_NAMESPACE_BYPASS))
714 props->device_cap_flags |= IB_DEVICE_MANAGED_FLOW_STEERING;
715
Noa Osherovichb1383aa2017-10-29 13:59:45 +0200716 if (MLX5_CAP_GEN(mdev, end_pad))
717 props->device_cap_flags |= IB_DEVICE_PCI_WRITE_END_PADDING;
718
Majd Dibbiny1b5daf12015-06-04 19:30:46 +0300719 props->vendor_part_id = mdev->pdev->device;
720 props->hw_ver = mdev->pdev->revision;
Eli Cohene126ba92013-07-07 17:25:49 +0300721
722 props->max_mr_size = ~0ull;
Sagi Grimberge0238a62015-07-21 14:40:12 +0300723 props->page_size_cap = ~(min_page_size - 1);
Saeed Mahameed938fe832015-05-28 22:28:41 +0300724 props->max_qp = 1 << MLX5_CAP_GEN(mdev, log_max_qp);
725 props->max_qp_wr = 1 << MLX5_CAP_GEN(mdev, log_max_qp_sz);
726 max_rq_sg = MLX5_CAP_GEN(mdev, max_wqe_sz_rq) /
727 sizeof(struct mlx5_wqe_data_seg);
Eli Cohen288c01b2016-10-27 16:36:45 +0300728 max_sq_desc = min_t(int, MLX5_CAP_GEN(mdev, max_wqe_sz_sq), 512);
729 max_sq_sg = (max_sq_desc - sizeof(struct mlx5_wqe_ctrl_seg) -
730 sizeof(struct mlx5_wqe_raddr_seg)) /
731 sizeof(struct mlx5_wqe_data_seg);
Eli Cohene126ba92013-07-07 17:25:49 +0300732 props->max_sge = min(max_rq_sg, max_sq_sg);
Sagi Grimberg986ef952016-03-31 19:03:25 +0300733 props->max_sge_rd = MLX5_MAX_SGE_RD;
Saeed Mahameed938fe832015-05-28 22:28:41 +0300734 props->max_cq = 1 << MLX5_CAP_GEN(mdev, log_max_cq);
Leon Romanovsky9f177682016-01-14 08:11:40 +0200735 props->max_cqe = (1 << MLX5_CAP_GEN(mdev, log_max_cq_sz)) - 1;
Saeed Mahameed938fe832015-05-28 22:28:41 +0300736 props->max_mr = 1 << MLX5_CAP_GEN(mdev, log_max_mkey);
737 props->max_pd = 1 << MLX5_CAP_GEN(mdev, log_max_pd);
738 props->max_qp_rd_atom = 1 << MLX5_CAP_GEN(mdev, log_max_ra_req_qp);
739 props->max_qp_init_rd_atom = 1 << MLX5_CAP_GEN(mdev, log_max_ra_res_qp);
740 props->max_srq = 1 << MLX5_CAP_GEN(mdev, log_max_srq);
741 props->max_srq_wr = (1 << MLX5_CAP_GEN(mdev, log_max_srq_sz)) - 1;
742 props->local_ca_ack_delay = MLX5_CAP_GEN(mdev, local_ca_ack_delay);
Eli Cohene126ba92013-07-07 17:25:49 +0300743 props->max_res_rd_atom = props->max_qp_rd_atom * props->max_qp;
Eli Cohene126ba92013-07-07 17:25:49 +0300744 props->max_srq_sge = max_rq_sg - 1;
Sagi Grimberg911f4332016-03-03 13:37:51 +0200745 props->max_fast_reg_page_list_len =
746 1 << MLX5_CAP_GEN(mdev, log_max_klm_list_size);
Eran Ben Elishada7525d2015-12-14 16:34:10 +0200747 get_atomic_caps(dev, props);
Eli Cohen81bea282013-09-11 16:35:30 +0300748 props->masked_atomic_cap = IB_ATOMIC_NONE;
Saeed Mahameed938fe832015-05-28 22:28:41 +0300749 props->max_mcast_grp = 1 << MLX5_CAP_GEN(mdev, log_max_mcg);
750 props->max_mcast_qp_attach = MLX5_CAP_GEN(mdev, max_qp_mcg);
Eli Cohene126ba92013-07-07 17:25:49 +0300751 props->max_total_mcast_qp_attach = props->max_mcast_qp_attach *
752 props->max_mcast_grp;
753 props->max_map_per_fmr = INT_MAX; /* no limit in ConnectIB */
Maor Gottlieb86695a62016-10-27 16:36:38 +0300754 props->max_ah = INT_MAX;
Matan Barak7c60bcb2015-12-15 20:30:11 +0200755 props->hca_core_clock = MLX5_CAP_GEN(mdev, device_frequency_khz);
756 props->timestamp_mask = 0x7FFFFFFFFFFFFFFFULL;
Eli Cohene126ba92013-07-07 17:25:49 +0300757
Haggai Eran8cdd3122014-12-11 17:04:20 +0200758#ifdef CONFIG_INFINIBAND_ON_DEMAND_PAGING
Saeed Mahameed938fe832015-05-28 22:28:41 +0300759 if (MLX5_CAP_GEN(mdev, pg))
Haggai Eran8cdd3122014-12-11 17:04:20 +0200760 props->device_cap_flags |= IB_DEVICE_ON_DEMAND_PAGING;
761 props->odp_caps = dev->odp_caps;
762#endif
763
Leon Romanovsky051f2632015-12-20 12:16:11 +0200764 if (MLX5_CAP_GEN(mdev, cd))
765 props->device_cap_flags |= IB_DEVICE_CROSS_CHANNEL;
766
Eli Coheneff901d2016-03-11 22:58:42 +0200767 if (!mlx5_core_is_pf(mdev))
768 props->device_cap_flags |= IB_DEVICE_VIRTUAL_FUNCTION;
769
Yishai Hadas31f69a82016-08-28 11:28:45 +0300770 if (mlx5_ib_port_link_layer(ibdev, 1) ==
771 IB_LINK_LAYER_ETHERNET) {
772 props->rss_caps.max_rwq_indirection_tables =
773 1 << MLX5_CAP_GEN(dev->mdev, log_max_rqt);
774 props->rss_caps.max_rwq_indirection_table_size =
775 1 << MLX5_CAP_GEN(dev->mdev, log_max_rqt_size);
776 props->rss_caps.supported_qpts = 1 << IB_QPT_RAW_PACKET;
777 props->max_wq_type_rq =
778 1 << MLX5_CAP_GEN(dev->mdev, log_max_rq);
779 }
780
Artemy Kovalyoveb761892017-08-17 15:52:09 +0300781 if (MLX5_CAP_GEN(mdev, tag_matching)) {
Leon Romanovsky78b1beb2017-09-24 21:46:29 +0300782 props->tm_caps.max_rndv_hdr_size = MLX5_TM_MAX_RNDV_MSG_SIZE;
783 props->tm_caps.max_num_tags =
Artemy Kovalyoveb761892017-08-17 15:52:09 +0300784 (1 << MLX5_CAP_GEN(mdev, log_tag_matching_list_sz)) - 1;
Leon Romanovsky78b1beb2017-09-24 21:46:29 +0300785 props->tm_caps.flags = IB_TM_CAP_RC;
786 props->tm_caps.max_ops =
Artemy Kovalyoveb761892017-08-17 15:52:09 +0300787 1 << MLX5_CAP_GEN(mdev, log_max_qp_sz);
Leon Romanovsky78b1beb2017-09-24 21:46:29 +0300788 props->tm_caps.max_sge = MLX5_TM_MAX_SGE;
Artemy Kovalyoveb761892017-08-17 15:52:09 +0300789 }
790
Yonatan Cohen87ab3f52017-11-13 10:51:18 +0200791 if (MLX5_CAP_GEN(dev->mdev, cq_moderation)) {
792 props->cq_caps.max_cq_moderation_count =
793 MLX5_MAX_CQ_COUNT;
794 props->cq_caps.max_cq_moderation_period =
795 MLX5_MAX_CQ_PERIOD;
796 }
797
Bodong Wang7e43a2a2016-10-31 12:16:44 +0200798 if (field_avail(typeof(resp), cqe_comp_caps, uhw->outlen)) {
799 resp.cqe_comp_caps.max_num =
800 MLX5_CAP_GEN(dev->mdev, cqe_compression) ?
801 MLX5_CAP_GEN(dev->mdev, cqe_compression_max_num) : 0;
802 resp.cqe_comp_caps.supported_format =
803 MLX5_IB_CQE_RES_FORMAT_HASH |
804 MLX5_IB_CQE_RES_FORMAT_CSUM;
805 resp.response_length += sizeof(resp.cqe_comp_caps);
806 }
807
Bodong Wangd9491672016-12-01 13:43:13 +0200808 if (field_avail(typeof(resp), packet_pacing_caps, uhw->outlen)) {
809 if (MLX5_CAP_QOS(mdev, packet_pacing) &&
810 MLX5_CAP_GEN(mdev, qos)) {
811 resp.packet_pacing_caps.qp_rate_limit_max =
812 MLX5_CAP_QOS(mdev, packet_pacing_max_rate);
813 resp.packet_pacing_caps.qp_rate_limit_min =
814 MLX5_CAP_QOS(mdev, packet_pacing_min_rate);
815 resp.packet_pacing_caps.supported_qpts |=
816 1 << IB_QPT_RAW_PACKET;
817 }
818 resp.response_length += sizeof(resp.packet_pacing_caps);
819 }
820
Leon Romanovsky9f885202017-01-02 11:37:39 +0200821 if (field_avail(typeof(resp), mlx5_ib_support_multi_pkt_send_wqes,
822 uhw->outlen)) {
Bodong Wang795b6092017-08-17 15:52:34 +0300823 if (MLX5_CAP_ETH(mdev, multi_pkt_send_wqe))
824 resp.mlx5_ib_support_multi_pkt_send_wqes =
825 MLX5_IB_ALLOW_MPW;
Bodong Wang050da902017-08-17 15:52:35 +0300826
827 if (MLX5_CAP_ETH(mdev, enhanced_multi_pkt_send_wqe))
828 resp.mlx5_ib_support_multi_pkt_send_wqes |=
829 MLX5_IB_SUPPORT_EMPW;
830
Leon Romanovsky9f885202017-01-02 11:37:39 +0200831 resp.response_length +=
832 sizeof(resp.mlx5_ib_support_multi_pkt_send_wqes);
833 }
834
Guy Levide57f2a2017-10-19 08:25:52 +0300835 if (field_avail(typeof(resp), flags, uhw->outlen)) {
836 resp.response_length += sizeof(resp.flags);
Guy Levi7a0c8f42017-10-19 08:25:53 +0300837
Guy Levide57f2a2017-10-19 08:25:52 +0300838 if (MLX5_CAP_GEN(mdev, cqe_compression_128))
839 resp.flags |=
840 MLX5_IB_QUERY_DEV_RESP_FLAGS_CQE_128B_COMP;
Guy Levi7a0c8f42017-10-19 08:25:53 +0300841
842 if (MLX5_CAP_GEN(mdev, cqe_128_always))
843 resp.flags |= MLX5_IB_QUERY_DEV_RESP_FLAGS_CQE_128B_PAD;
Guy Levide57f2a2017-10-19 08:25:52 +0300844 }
Leon Romanovsky9f885202017-01-02 11:37:39 +0200845
Noa Osherovich96dc3fc2017-08-17 15:52:28 +0300846 if (field_avail(typeof(resp), sw_parsing_caps,
847 uhw->outlen)) {
848 resp.response_length += sizeof(resp.sw_parsing_caps);
849 if (MLX5_CAP_ETH(mdev, swp)) {
850 resp.sw_parsing_caps.sw_parsing_offloads |=
851 MLX5_IB_SW_PARSING;
852
853 if (MLX5_CAP_ETH(mdev, swp_csum))
854 resp.sw_parsing_caps.sw_parsing_offloads |=
855 MLX5_IB_SW_PARSING_CSUM;
856
857 if (MLX5_CAP_ETH(mdev, swp_lso))
858 resp.sw_parsing_caps.sw_parsing_offloads |=
859 MLX5_IB_SW_PARSING_LSO;
860
861 if (resp.sw_parsing_caps.sw_parsing_offloads)
862 resp.sw_parsing_caps.supported_qpts =
863 BIT(IB_QPT_RAW_PACKET);
864 }
865 }
866
Noa Osherovichb4f34592017-10-17 18:01:12 +0300867 if (field_avail(typeof(resp), striding_rq_caps, uhw->outlen)) {
868 resp.response_length += sizeof(resp.striding_rq_caps);
869 if (MLX5_CAP_GEN(mdev, striding_rq)) {
870 resp.striding_rq_caps.min_single_stride_log_num_of_bytes =
871 MLX5_MIN_SINGLE_STRIDE_LOG_NUM_BYTES;
872 resp.striding_rq_caps.max_single_stride_log_num_of_bytes =
873 MLX5_MAX_SINGLE_STRIDE_LOG_NUM_BYTES;
874 resp.striding_rq_caps.min_single_wqe_log_num_of_strides =
875 MLX5_MIN_SINGLE_WQE_LOG_NUM_STRIDES;
876 resp.striding_rq_caps.max_single_wqe_log_num_of_strides =
877 MLX5_MAX_SINGLE_WQE_LOG_NUM_STRIDES;
878 resp.striding_rq_caps.supported_qpts =
879 BIT(IB_QPT_RAW_PACKET);
880 }
881 }
882
Maor Gottliebf95ef6c2017-10-19 08:25:55 +0300883 if (field_avail(typeof(resp), tunnel_offloads_caps,
884 uhw->outlen)) {
885 resp.response_length += sizeof(resp.tunnel_offloads_caps);
886 if (MLX5_CAP_ETH(mdev, tunnel_stateless_vxlan))
887 resp.tunnel_offloads_caps |=
888 MLX5_IB_TUNNELED_OFFLOADS_VXLAN;
889 if (MLX5_CAP_ETH(mdev, tunnel_stateless_geneve_rx))
890 resp.tunnel_offloads_caps |=
891 MLX5_IB_TUNNELED_OFFLOADS_GENEVE;
892 if (MLX5_CAP_ETH(mdev, tunnel_stateless_gre))
893 resp.tunnel_offloads_caps |=
894 MLX5_IB_TUNNELED_OFFLOADS_GRE;
895 }
896
Bodong Wang402ca532016-06-17 15:02:20 +0300897 if (uhw->outlen) {
898 err = ib_copy_to_udata(uhw, &resp, resp.response_length);
899
900 if (err)
901 return err;
902 }
903
Majd Dibbiny1b5daf12015-06-04 19:30:46 +0300904 return 0;
905}
Eli Cohene126ba92013-07-07 17:25:49 +0300906
Majd Dibbiny1b5daf12015-06-04 19:30:46 +0300907enum mlx5_ib_width {
908 MLX5_IB_WIDTH_1X = 1 << 0,
909 MLX5_IB_WIDTH_2X = 1 << 1,
910 MLX5_IB_WIDTH_4X = 1 << 2,
911 MLX5_IB_WIDTH_8X = 1 << 3,
912 MLX5_IB_WIDTH_12X = 1 << 4
913};
914
915static int translate_active_width(struct ib_device *ibdev, u8 active_width,
916 u8 *ib_width)
917{
918 struct mlx5_ib_dev *dev = to_mdev(ibdev);
919 int err = 0;
920
921 if (active_width & MLX5_IB_WIDTH_1X) {
922 *ib_width = IB_WIDTH_1X;
923 } else if (active_width & MLX5_IB_WIDTH_2X) {
924 mlx5_ib_dbg(dev, "active_width %d is not supported by IB spec\n",
925 (int)active_width);
926 err = -EINVAL;
927 } else if (active_width & MLX5_IB_WIDTH_4X) {
928 *ib_width = IB_WIDTH_4X;
929 } else if (active_width & MLX5_IB_WIDTH_8X) {
930 *ib_width = IB_WIDTH_8X;
931 } else if (active_width & MLX5_IB_WIDTH_12X) {
932 *ib_width = IB_WIDTH_12X;
933 } else {
934 mlx5_ib_dbg(dev, "Invalid active_width %d\n",
935 (int)active_width);
936 err = -EINVAL;
937 }
938
939 return err;
940}
941
942static int mlx5_mtu_to_ib_mtu(int mtu)
943{
944 switch (mtu) {
945 case 256: return 1;
946 case 512: return 2;
947 case 1024: return 3;
948 case 2048: return 4;
949 case 4096: return 5;
950 default:
951 pr_warn("invalid mtu\n");
952 return -1;
953 }
954}
955
956enum ib_max_vl_num {
957 __IB_MAX_VL_0 = 1,
958 __IB_MAX_VL_0_1 = 2,
959 __IB_MAX_VL_0_3 = 3,
960 __IB_MAX_VL_0_7 = 4,
961 __IB_MAX_VL_0_14 = 5,
962};
963
964enum mlx5_vl_hw_cap {
965 MLX5_VL_HW_0 = 1,
966 MLX5_VL_HW_0_1 = 2,
967 MLX5_VL_HW_0_2 = 3,
968 MLX5_VL_HW_0_3 = 4,
969 MLX5_VL_HW_0_4 = 5,
970 MLX5_VL_HW_0_5 = 6,
971 MLX5_VL_HW_0_6 = 7,
972 MLX5_VL_HW_0_7 = 8,
973 MLX5_VL_HW_0_14 = 15
974};
975
976static int translate_max_vl_num(struct ib_device *ibdev, u8 vl_hw_cap,
977 u8 *max_vl_num)
978{
979 switch (vl_hw_cap) {
980 case MLX5_VL_HW_0:
981 *max_vl_num = __IB_MAX_VL_0;
982 break;
983 case MLX5_VL_HW_0_1:
984 *max_vl_num = __IB_MAX_VL_0_1;
985 break;
986 case MLX5_VL_HW_0_3:
987 *max_vl_num = __IB_MAX_VL_0_3;
988 break;
989 case MLX5_VL_HW_0_7:
990 *max_vl_num = __IB_MAX_VL_0_7;
991 break;
992 case MLX5_VL_HW_0_14:
993 *max_vl_num = __IB_MAX_VL_0_14;
994 break;
995
996 default:
997 return -EINVAL;
998 }
999
1000 return 0;
1001}
1002
1003static int mlx5_query_hca_port(struct ib_device *ibdev, u8 port,
1004 struct ib_port_attr *props)
1005{
1006 struct mlx5_ib_dev *dev = to_mdev(ibdev);
1007 struct mlx5_core_dev *mdev = dev->mdev;
1008 struct mlx5_hca_vport_context *rep;
Saeed Mahameed046339e2016-04-22 00:33:03 +03001009 u16 max_mtu;
1010 u16 oper_mtu;
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03001011 int err;
1012 u8 ib_link_width_oper;
1013 u8 vl_hw_cap;
1014
1015 rep = kzalloc(sizeof(*rep), GFP_KERNEL);
1016 if (!rep) {
1017 err = -ENOMEM;
1018 goto out;
1019 }
1020
Or Gerlitzc4550c62017-01-24 13:02:39 +02001021 /* props being zeroed by the caller, avoid zeroing it here */
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03001022
1023 err = mlx5_query_hca_vport_context(mdev, 0, port, 0, rep);
1024 if (err)
1025 goto out;
1026
1027 props->lid = rep->lid;
1028 props->lmc = rep->lmc;
1029 props->sm_lid = rep->sm_lid;
1030 props->sm_sl = rep->sm_sl;
1031 props->state = rep->vport_state;
1032 props->phys_state = rep->port_physical_state;
1033 props->port_cap_flags = rep->cap_mask1;
1034 props->gid_tbl_len = mlx5_get_gid_table_len(MLX5_CAP_GEN(mdev, gid_table_size));
1035 props->max_msg_sz = 1 << MLX5_CAP_GEN(mdev, log_max_msg);
1036 props->pkey_tbl_len = mlx5_to_sw_pkey_sz(MLX5_CAP_GEN(mdev, pkey_table_size));
1037 props->bad_pkey_cntr = rep->pkey_violation_counter;
1038 props->qkey_viol_cntr = rep->qkey_violation_counter;
1039 props->subnet_timeout = rep->subnet_timeout;
1040 props->init_type_reply = rep->init_type_reply;
Eli Coheneff901d2016-03-11 22:58:42 +02001041 props->grh_required = rep->grh_required;
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03001042
1043 err = mlx5_query_port_link_width_oper(mdev, &ib_link_width_oper, port);
1044 if (err)
1045 goto out;
1046
1047 err = translate_active_width(ibdev, ib_link_width_oper,
1048 &props->active_width);
1049 if (err)
1050 goto out;
Noa Osherovichd5beb7f2016-06-02 10:47:53 +03001051 err = mlx5_query_port_ib_proto_oper(mdev, &props->active_speed, port);
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03001052 if (err)
1053 goto out;
1054
Saeed Mahameedfacc9692015-06-11 14:47:27 +03001055 mlx5_query_port_max_mtu(mdev, &max_mtu, port);
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03001056
1057 props->max_mtu = mlx5_mtu_to_ib_mtu(max_mtu);
1058
Saeed Mahameedfacc9692015-06-11 14:47:27 +03001059 mlx5_query_port_oper_mtu(mdev, &oper_mtu, port);
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03001060
1061 props->active_mtu = mlx5_mtu_to_ib_mtu(oper_mtu);
1062
1063 err = mlx5_query_port_vl_hw_cap(mdev, &vl_hw_cap, port);
1064 if (err)
1065 goto out;
1066
1067 err = translate_max_vl_num(ibdev, vl_hw_cap,
1068 &props->max_vl_num);
1069out:
1070 kfree(rep);
Eli Cohene126ba92013-07-07 17:25:49 +03001071 return err;
1072}
1073
1074int mlx5_ib_query_port(struct ib_device *ibdev, u8 port,
1075 struct ib_port_attr *props)
1076{
Ilan Tayari095b0922017-05-14 16:04:30 +03001077 unsigned int count;
1078 int ret;
1079
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03001080 switch (mlx5_get_vport_access_method(ibdev)) {
1081 case MLX5_VPORT_ACCESS_METHOD_MAD:
Ilan Tayari095b0922017-05-14 16:04:30 +03001082 ret = mlx5_query_mad_ifc_port(ibdev, port, props);
1083 break;
Eli Cohene126ba92013-07-07 17:25:49 +03001084
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03001085 case MLX5_VPORT_ACCESS_METHOD_HCA:
Ilan Tayari095b0922017-05-14 16:04:30 +03001086 ret = mlx5_query_hca_port(ibdev, port, props);
1087 break;
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03001088
Achiad Shochat3f89a642015-12-23 18:47:21 +02001089 case MLX5_VPORT_ACCESS_METHOD_NIC:
Ilan Tayari095b0922017-05-14 16:04:30 +03001090 ret = mlx5_query_port_roce(ibdev, port, props);
1091 break;
Achiad Shochat3f89a642015-12-23 18:47:21 +02001092
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03001093 default:
Ilan Tayari095b0922017-05-14 16:04:30 +03001094 ret = -EINVAL;
Eli Cohene126ba92013-07-07 17:25:49 +03001095 }
Ilan Tayari095b0922017-05-14 16:04:30 +03001096
1097 if (!ret && props) {
1098 count = mlx5_core_reserved_gids_count(to_mdev(ibdev)->mdev);
1099 props->gid_tbl_len -= count;
1100 }
1101 return ret;
Eli Cohene126ba92013-07-07 17:25:49 +03001102}
1103
1104static int mlx5_ib_query_gid(struct ib_device *ibdev, u8 port, int index,
1105 union ib_gid *gid)
1106{
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03001107 struct mlx5_ib_dev *dev = to_mdev(ibdev);
1108 struct mlx5_core_dev *mdev = dev->mdev;
Eli Cohene126ba92013-07-07 17:25:49 +03001109
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03001110 switch (mlx5_get_vport_access_method(ibdev)) {
1111 case MLX5_VPORT_ACCESS_METHOD_MAD:
1112 return mlx5_query_mad_ifc_gids(ibdev, port, index, gid);
Eli Cohene126ba92013-07-07 17:25:49 +03001113
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03001114 case MLX5_VPORT_ACCESS_METHOD_HCA:
1115 return mlx5_query_hca_vport_gid(mdev, 0, port, 0, index, gid);
Eli Cohene126ba92013-07-07 17:25:49 +03001116
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03001117 default:
1118 return -EINVAL;
1119 }
Eli Cohene126ba92013-07-07 17:25:49 +03001120
Eli Cohene126ba92013-07-07 17:25:49 +03001121}
1122
1123static int mlx5_ib_query_pkey(struct ib_device *ibdev, u8 port, u16 index,
1124 u16 *pkey)
1125{
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03001126 struct mlx5_ib_dev *dev = to_mdev(ibdev);
1127 struct mlx5_core_dev *mdev = dev->mdev;
Eli Cohene126ba92013-07-07 17:25:49 +03001128
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03001129 switch (mlx5_get_vport_access_method(ibdev)) {
1130 case MLX5_VPORT_ACCESS_METHOD_MAD:
1131 return mlx5_query_mad_ifc_pkey(ibdev, port, index, pkey);
Eli Cohene126ba92013-07-07 17:25:49 +03001132
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03001133 case MLX5_VPORT_ACCESS_METHOD_HCA:
1134 case MLX5_VPORT_ACCESS_METHOD_NIC:
1135 return mlx5_query_hca_vport_pkey(mdev, 0, port, 0, index,
1136 pkey);
1137 default:
1138 return -EINVAL;
1139 }
Eli Cohene126ba92013-07-07 17:25:49 +03001140}
1141
Eli Cohene126ba92013-07-07 17:25:49 +03001142static int mlx5_ib_modify_device(struct ib_device *ibdev, int mask,
1143 struct ib_device_modify *props)
1144{
1145 struct mlx5_ib_dev *dev = to_mdev(ibdev);
1146 struct mlx5_reg_node_desc in;
1147 struct mlx5_reg_node_desc out;
1148 int err;
1149
1150 if (mask & ~IB_DEVICE_MODIFY_NODE_DESC)
1151 return -EOPNOTSUPP;
1152
1153 if (!(mask & IB_DEVICE_MODIFY_NODE_DESC))
1154 return 0;
1155
1156 /*
1157 * If possible, pass node desc to FW, so it can generate
1158 * a 144 trap. If cmd fails, just ignore.
1159 */
Yuval Shaiabd99fde2016-08-25 10:57:07 -07001160 memcpy(&in, props->node_desc, IB_DEVICE_NODE_DESC_MAX);
Jack Morgenstein9603b612014-07-28 23:30:22 +03001161 err = mlx5_core_access_reg(dev->mdev, &in, sizeof(in), &out,
Eli Cohene126ba92013-07-07 17:25:49 +03001162 sizeof(out), MLX5_REG_NODE_DESC, 0, 1);
1163 if (err)
1164 return err;
1165
Yuval Shaiabd99fde2016-08-25 10:57:07 -07001166 memcpy(ibdev->node_desc, props->node_desc, IB_DEVICE_NODE_DESC_MAX);
Eli Cohene126ba92013-07-07 17:25:49 +03001167
1168 return err;
1169}
1170
Eli Cohencdbe33d2017-02-14 07:25:38 +02001171static int set_port_caps_atomic(struct mlx5_ib_dev *dev, u8 port_num, u32 mask,
1172 u32 value)
1173{
1174 struct mlx5_hca_vport_context ctx = {};
1175 int err;
1176
1177 err = mlx5_query_hca_vport_context(dev->mdev, 0,
1178 port_num, 0, &ctx);
1179 if (err)
1180 return err;
1181
1182 if (~ctx.cap_mask1_perm & mask) {
1183 mlx5_ib_warn(dev, "trying to change bitmask 0x%X but change supported 0x%X\n",
1184 mask, ctx.cap_mask1_perm);
1185 return -EINVAL;
1186 }
1187
1188 ctx.cap_mask1 = value;
1189 ctx.cap_mask1_perm = mask;
1190 err = mlx5_core_modify_hca_vport_context(dev->mdev, 0,
1191 port_num, 0, &ctx);
1192
1193 return err;
1194}
1195
Eli Cohene126ba92013-07-07 17:25:49 +03001196static int mlx5_ib_modify_port(struct ib_device *ibdev, u8 port, int mask,
1197 struct ib_port_modify *props)
1198{
1199 struct mlx5_ib_dev *dev = to_mdev(ibdev);
1200 struct ib_port_attr attr;
1201 u32 tmp;
1202 int err;
Eli Cohencdbe33d2017-02-14 07:25:38 +02001203 u32 change_mask;
1204 u32 value;
1205 bool is_ib = (mlx5_ib_port_link_layer(ibdev, port) ==
1206 IB_LINK_LAYER_INFINIBAND);
1207
Majd Dibbinyec255872017-08-23 08:35:42 +03001208 /* CM layer calls ib_modify_port() regardless of the link layer. For
1209 * Ethernet ports, qkey violation and Port capabilities are meaningless.
1210 */
1211 if (!is_ib)
1212 return 0;
1213
Eli Cohencdbe33d2017-02-14 07:25:38 +02001214 if (MLX5_CAP_GEN(dev->mdev, ib_virt) && is_ib) {
1215 change_mask = props->clr_port_cap_mask | props->set_port_cap_mask;
1216 value = ~props->clr_port_cap_mask | props->set_port_cap_mask;
1217 return set_port_caps_atomic(dev, port, change_mask, value);
1218 }
Eli Cohene126ba92013-07-07 17:25:49 +03001219
1220 mutex_lock(&dev->cap_mask_mutex);
1221
Or Gerlitzc4550c62017-01-24 13:02:39 +02001222 err = ib_query_port(ibdev, port, &attr);
Eli Cohene126ba92013-07-07 17:25:49 +03001223 if (err)
1224 goto out;
1225
1226 tmp = (attr.port_cap_flags | props->set_port_cap_mask) &
1227 ~props->clr_port_cap_mask;
1228
Jack Morgenstein9603b612014-07-28 23:30:22 +03001229 err = mlx5_set_port_caps(dev->mdev, port, tmp);
Eli Cohene126ba92013-07-07 17:25:49 +03001230
1231out:
1232 mutex_unlock(&dev->cap_mask_mutex);
1233 return err;
1234}
1235
Eli Cohen30aa60b2017-01-03 23:55:27 +02001236static void print_lib_caps(struct mlx5_ib_dev *dev, u64 caps)
1237{
1238 mlx5_ib_dbg(dev, "MLX5_LIB_CAP_4K_UAR = %s\n",
1239 caps & MLX5_LIB_CAP_4K_UAR ? "y" : "n");
1240}
1241
Eli Cohenb037c292017-01-03 23:55:26 +02001242static int calc_total_bfregs(struct mlx5_ib_dev *dev, bool lib_uar_4k,
1243 struct mlx5_ib_alloc_ucontext_req_v2 *req,
1244 u32 *num_sys_pages)
1245{
1246 int uars_per_sys_page;
1247 int bfregs_per_sys_page;
1248 int ref_bfregs = req->total_num_bfregs;
1249
1250 if (req->total_num_bfregs == 0)
1251 return -EINVAL;
1252
1253 BUILD_BUG_ON(MLX5_MAX_BFREGS % MLX5_NON_FP_BFREGS_IN_PAGE);
1254 BUILD_BUG_ON(MLX5_MAX_BFREGS < MLX5_NON_FP_BFREGS_IN_PAGE);
1255
1256 if (req->total_num_bfregs > MLX5_MAX_BFREGS)
1257 return -ENOMEM;
1258
1259 uars_per_sys_page = get_uars_per_sys_page(dev, lib_uar_4k);
1260 bfregs_per_sys_page = uars_per_sys_page * MLX5_NON_FP_BFREGS_PER_UAR;
1261 req->total_num_bfregs = ALIGN(req->total_num_bfregs, bfregs_per_sys_page);
1262 *num_sys_pages = req->total_num_bfregs / bfregs_per_sys_page;
1263
1264 if (req->num_low_latency_bfregs > req->total_num_bfregs - 1)
1265 return -EINVAL;
1266
Colin Ian King9c2d33d2017-06-27 08:40:59 +01001267 mlx5_ib_dbg(dev, "uar_4k: fw support %s, lib support %s, user requested %d bfregs, allocated %d, using %d sys pages\n",
Eli Cohenb037c292017-01-03 23:55:26 +02001268 MLX5_CAP_GEN(dev->mdev, uar_4k) ? "yes" : "no",
1269 lib_uar_4k ? "yes" : "no", ref_bfregs,
1270 req->total_num_bfregs, *num_sys_pages);
1271
1272 return 0;
1273}
1274
1275static int allocate_uars(struct mlx5_ib_dev *dev, struct mlx5_ib_ucontext *context)
1276{
1277 struct mlx5_bfreg_info *bfregi;
1278 int err;
1279 int i;
1280
1281 bfregi = &context->bfregi;
1282 for (i = 0; i < bfregi->num_sys_pages; i++) {
1283 err = mlx5_cmd_alloc_uar(dev->mdev, &bfregi->sys_pages[i]);
1284 if (err)
1285 goto error;
1286
1287 mlx5_ib_dbg(dev, "allocated uar %d\n", bfregi->sys_pages[i]);
1288 }
1289 return 0;
1290
1291error:
1292 for (--i; i >= 0; i--)
1293 if (mlx5_cmd_free_uar(dev->mdev, bfregi->sys_pages[i]))
1294 mlx5_ib_warn(dev, "failed to free uar %d\n", i);
1295
1296 return err;
1297}
1298
1299static int deallocate_uars(struct mlx5_ib_dev *dev, struct mlx5_ib_ucontext *context)
1300{
1301 struct mlx5_bfreg_info *bfregi;
1302 int err;
1303 int i;
1304
1305 bfregi = &context->bfregi;
1306 for (i = 0; i < bfregi->num_sys_pages; i++) {
1307 err = mlx5_cmd_free_uar(dev->mdev, bfregi->sys_pages[i]);
1308 if (err) {
1309 mlx5_ib_warn(dev, "failed to free uar %d\n", i);
1310 return err;
1311 }
1312 }
1313 return 0;
1314}
1315
Huy Nguyenc85023e2017-05-30 09:42:54 +03001316static int mlx5_ib_alloc_transport_domain(struct mlx5_ib_dev *dev, u32 *tdn)
1317{
1318 int err;
1319
1320 err = mlx5_core_alloc_transport_domain(dev->mdev, tdn);
1321 if (err)
1322 return err;
1323
1324 if ((MLX5_CAP_GEN(dev->mdev, port_type) != MLX5_CAP_PORT_TYPE_ETH) ||
1325 !MLX5_CAP_GEN(dev->mdev, disable_local_lb))
1326 return err;
1327
1328 mutex_lock(&dev->lb_mutex);
1329 dev->user_td++;
1330
1331 if (dev->user_td == 2)
1332 err = mlx5_nic_vport_update_local_lb(dev->mdev, true);
1333
1334 mutex_unlock(&dev->lb_mutex);
1335 return err;
1336}
1337
1338static void mlx5_ib_dealloc_transport_domain(struct mlx5_ib_dev *dev, u32 tdn)
1339{
1340 mlx5_core_dealloc_transport_domain(dev->mdev, tdn);
1341
1342 if ((MLX5_CAP_GEN(dev->mdev, port_type) != MLX5_CAP_PORT_TYPE_ETH) ||
1343 !MLX5_CAP_GEN(dev->mdev, disable_local_lb))
1344 return;
1345
1346 mutex_lock(&dev->lb_mutex);
1347 dev->user_td--;
1348
1349 if (dev->user_td < 2)
1350 mlx5_nic_vport_update_local_lb(dev->mdev, false);
1351
1352 mutex_unlock(&dev->lb_mutex);
1353}
1354
Eli Cohene126ba92013-07-07 17:25:49 +03001355static struct ib_ucontext *mlx5_ib_alloc_ucontext(struct ib_device *ibdev,
1356 struct ib_udata *udata)
1357{
1358 struct mlx5_ib_dev *dev = to_mdev(ibdev);
Matan Barakb368d7c2015-12-15 20:30:12 +02001359 struct mlx5_ib_alloc_ucontext_req_v2 req = {};
1360 struct mlx5_ib_alloc_ucontext_resp resp = {};
Eli Cohene126ba92013-07-07 17:25:49 +03001361 struct mlx5_ib_ucontext *context;
Eli Cohen2f5ff262017-01-03 23:55:21 +02001362 struct mlx5_bfreg_info *bfregi;
Eli Cohen78c0f982014-01-30 13:49:48 +02001363 int ver;
Eli Cohene126ba92013-07-07 17:25:49 +03001364 int err;
Majd Dibbinya168a41c2016-01-28 17:51:47 +02001365 size_t min_req_v2 = offsetof(struct mlx5_ib_alloc_ucontext_req_v2,
1366 max_cqe_version);
Eli Cohenb037c292017-01-03 23:55:26 +02001367 bool lib_uar_4k;
Eli Cohene126ba92013-07-07 17:25:49 +03001368
1369 if (!dev->ib_active)
1370 return ERR_PTR(-EAGAIN);
1371
Amrani, Rame0931112017-06-27 17:04:42 +03001372 if (udata->inlen == sizeof(struct mlx5_ib_alloc_ucontext_req))
Eli Cohen78c0f982014-01-30 13:49:48 +02001373 ver = 0;
Amrani, Rame0931112017-06-27 17:04:42 +03001374 else if (udata->inlen >= min_req_v2)
Eli Cohen78c0f982014-01-30 13:49:48 +02001375 ver = 2;
1376 else
1377 return ERR_PTR(-EINVAL);
1378
Amrani, Rame0931112017-06-27 17:04:42 +03001379 err = ib_copy_from_udata(&req, udata, min(udata->inlen, sizeof(req)));
Eli Cohene126ba92013-07-07 17:25:49 +03001380 if (err)
1381 return ERR_PTR(err);
1382
Matan Barakb368d7c2015-12-15 20:30:12 +02001383 if (req.flags)
Eli Cohen78c0f982014-01-30 13:49:48 +02001384 return ERR_PTR(-EINVAL);
1385
Haggai Abramovskyf72300c2016-01-14 19:12:58 +02001386 if (req.comp_mask || req.reserved0 || req.reserved1 || req.reserved2)
Matan Barakb368d7c2015-12-15 20:30:12 +02001387 return ERR_PTR(-EOPNOTSUPP);
1388
Eli Cohen2f5ff262017-01-03 23:55:21 +02001389 req.total_num_bfregs = ALIGN(req.total_num_bfregs,
1390 MLX5_NON_FP_BFREGS_PER_UAR);
1391 if (req.num_low_latency_bfregs > req.total_num_bfregs - 1)
Eli Cohene126ba92013-07-07 17:25:49 +03001392 return ERR_PTR(-EINVAL);
1393
Saeed Mahameed938fe832015-05-28 22:28:41 +03001394 resp.qp_tab_size = 1 << MLX5_CAP_GEN(dev->mdev, log_max_qp);
Noa Osherovich2cc6ad52016-06-04 15:15:33 +03001395 if (mlx5_core_is_pf(dev->mdev) && MLX5_CAP_GEN(dev->mdev, bf))
1396 resp.bf_reg_size = 1 << MLX5_CAP_GEN(dev->mdev, log_bf_reg_size);
Daniel Jurgensb47bd6e2016-10-25 18:36:24 +03001397 resp.cache_line_size = cache_line_size();
Saeed Mahameed938fe832015-05-28 22:28:41 +03001398 resp.max_sq_desc_sz = MLX5_CAP_GEN(dev->mdev, max_wqe_sz_sq);
1399 resp.max_rq_desc_sz = MLX5_CAP_GEN(dev->mdev, max_wqe_sz_rq);
1400 resp.max_send_wqebb = 1 << MLX5_CAP_GEN(dev->mdev, log_max_qp_sz);
1401 resp.max_recv_wr = 1 << MLX5_CAP_GEN(dev->mdev, log_max_qp_sz);
1402 resp.max_srq_recv_wr = 1 << MLX5_CAP_GEN(dev->mdev, log_max_srq_sz);
Haggai Abramovskyf72300c2016-01-14 19:12:58 +02001403 resp.cqe_version = min_t(__u8,
1404 (__u8)MLX5_CAP_GEN(dev->mdev, cqe_version),
1405 req.max_cqe_version);
Eli Cohen30aa60b2017-01-03 23:55:27 +02001406 resp.log_uar_size = MLX5_CAP_GEN(dev->mdev, uar_4k) ?
1407 MLX5_ADAPTER_PAGE_SHIFT : PAGE_SHIFT;
1408 resp.num_uars_per_page = MLX5_CAP_GEN(dev->mdev, uar_4k) ?
1409 MLX5_CAP_GEN(dev->mdev, num_of_uars_per_page) : 1;
Matan Barakb368d7c2015-12-15 20:30:12 +02001410 resp.response_length = min(offsetof(typeof(resp), response_length) +
1411 sizeof(resp.response_length), udata->outlen);
Eli Cohene126ba92013-07-07 17:25:49 +03001412
1413 context = kzalloc(sizeof(*context), GFP_KERNEL);
1414 if (!context)
1415 return ERR_PTR(-ENOMEM);
1416
Eli Cohen30aa60b2017-01-03 23:55:27 +02001417 lib_uar_4k = req.lib_caps & MLX5_LIB_CAP_4K_UAR;
Eli Cohen2f5ff262017-01-03 23:55:21 +02001418 bfregi = &context->bfregi;
Eli Cohenb037c292017-01-03 23:55:26 +02001419
1420 /* updates req->total_num_bfregs */
1421 err = calc_total_bfregs(dev, lib_uar_4k, &req, &bfregi->num_sys_pages);
1422 if (err)
1423 goto out_ctx;
1424
Eli Cohen2f5ff262017-01-03 23:55:21 +02001425 mutex_init(&bfregi->lock);
Eli Cohenb037c292017-01-03 23:55:26 +02001426 bfregi->lib_uar_4k = lib_uar_4k;
1427 bfregi->count = kcalloc(req.total_num_bfregs, sizeof(*bfregi->count),
1428 GFP_KERNEL);
1429 if (!bfregi->count) {
Eli Cohene126ba92013-07-07 17:25:49 +03001430 err = -ENOMEM;
1431 goto out_ctx;
1432 }
1433
Eli Cohenb037c292017-01-03 23:55:26 +02001434 bfregi->sys_pages = kcalloc(bfregi->num_sys_pages,
1435 sizeof(*bfregi->sys_pages),
1436 GFP_KERNEL);
1437 if (!bfregi->sys_pages) {
Eli Cohene126ba92013-07-07 17:25:49 +03001438 err = -ENOMEM;
Eli Cohenb037c292017-01-03 23:55:26 +02001439 goto out_count;
Eli Cohene126ba92013-07-07 17:25:49 +03001440 }
1441
Eli Cohenb037c292017-01-03 23:55:26 +02001442 err = allocate_uars(dev, context);
1443 if (err)
1444 goto out_sys_pages;
Eli Cohene126ba92013-07-07 17:25:49 +03001445
Haggai Eranb4cfe442014-12-11 17:04:26 +02001446#ifdef CONFIG_INFINIBAND_ON_DEMAND_PAGING
1447 context->ibucontext.invalidate_range = &mlx5_ib_invalidate_range;
1448#endif
1449
Artemy Kovalyov7d0cc6e2017-01-02 11:37:44 +02001450 context->upd_xlt_page = __get_free_page(GFP_KERNEL);
1451 if (!context->upd_xlt_page) {
1452 err = -ENOMEM;
1453 goto out_uars;
1454 }
1455 mutex_init(&context->upd_xlt_page_mutex);
1456
majd@mellanox.com146d2f12016-01-14 19:13:02 +02001457 if (MLX5_CAP_GEN(dev->mdev, log_max_transport_domain)) {
Huy Nguyenc85023e2017-05-30 09:42:54 +03001458 err = mlx5_ib_alloc_transport_domain(dev, &context->tdn);
majd@mellanox.com146d2f12016-01-14 19:13:02 +02001459 if (err)
Artemy Kovalyov7d0cc6e2017-01-02 11:37:44 +02001460 goto out_page;
majd@mellanox.com146d2f12016-01-14 19:13:02 +02001461 }
1462
Maor Gottlieb7c2344c2016-06-17 14:56:44 +03001463 INIT_LIST_HEAD(&context->vma_private_list);
Eli Cohene126ba92013-07-07 17:25:49 +03001464 INIT_LIST_HEAD(&context->db_page_list);
1465 mutex_init(&context->db_page_mutex);
1466
Eli Cohen2f5ff262017-01-03 23:55:21 +02001467 resp.tot_bfregs = req.total_num_bfregs;
Saeed Mahameed938fe832015-05-28 22:28:41 +03001468 resp.num_ports = MLX5_CAP_GEN(dev->mdev, num_ports);
Matan Barakb368d7c2015-12-15 20:30:12 +02001469
Haggai Abramovskyf72300c2016-01-14 19:12:58 +02001470 if (field_avail(typeof(resp), cqe_version, udata->outlen))
1471 resp.response_length += sizeof(resp.cqe_version);
Matan Barakb368d7c2015-12-15 20:30:12 +02001472
Bodong Wang402ca532016-06-17 15:02:20 +03001473 if (field_avail(typeof(resp), cmds_supp_uhw, udata->outlen)) {
Moni Shoua6ad279c52016-11-23 08:23:23 +02001474 resp.cmds_supp_uhw |= MLX5_USER_CMDS_SUPP_UHW_QUERY_DEVICE |
1475 MLX5_USER_CMDS_SUPP_UHW_CREATE_AH;
Bodong Wang402ca532016-06-17 15:02:20 +03001476 resp.response_length += sizeof(resp.cmds_supp_uhw);
1477 }
1478
Or Gerlitz78984892016-11-30 20:33:33 +02001479 if (field_avail(typeof(resp), eth_min_inline, udata->outlen)) {
1480 if (mlx5_ib_port_link_layer(ibdev, 1) == IB_LINK_LAYER_ETHERNET) {
1481 mlx5_query_min_inline(dev->mdev, &resp.eth_min_inline);
1482 resp.eth_min_inline++;
1483 }
1484 resp.response_length += sizeof(resp.eth_min_inline);
1485 }
1486
Noa Osherovichbc5c6ee2016-06-04 15:15:31 +03001487 /*
1488 * We don't want to expose information from the PCI bar that is located
1489 * after 4096 bytes, so if the arch only supports larger pages, let's
1490 * pretend we don't support reading the HCA's core clock. This is also
1491 * forced by mmap function.
1492 */
Eli Cohende8d6e02017-01-03 23:55:19 +02001493 if (field_avail(typeof(resp), hca_core_clock_offset, udata->outlen)) {
1494 if (PAGE_SIZE <= 4096) {
1495 resp.comp_mask |=
1496 MLX5_IB_ALLOC_UCONTEXT_RESP_MASK_CORE_CLOCK_OFFSET;
1497 resp.hca_core_clock_offset =
1498 offsetof(struct mlx5_init_seg, internal_timer_h) % PAGE_SIZE;
1499 }
Haggai Abramovskyf72300c2016-01-14 19:12:58 +02001500 resp.response_length += sizeof(resp.hca_core_clock_offset) +
Bodong Wang402ca532016-06-17 15:02:20 +03001501 sizeof(resp.reserved2);
Matan Barakb368d7c2015-12-15 20:30:12 +02001502 }
1503
Eli Cohen30aa60b2017-01-03 23:55:27 +02001504 if (field_avail(typeof(resp), log_uar_size, udata->outlen))
1505 resp.response_length += sizeof(resp.log_uar_size);
1506
1507 if (field_avail(typeof(resp), num_uars_per_page, udata->outlen))
1508 resp.response_length += sizeof(resp.num_uars_per_page);
1509
Matan Barakb368d7c2015-12-15 20:30:12 +02001510 err = ib_copy_to_udata(udata, &resp, resp.response_length);
Eli Cohene126ba92013-07-07 17:25:49 +03001511 if (err)
majd@mellanox.com146d2f12016-01-14 19:13:02 +02001512 goto out_td;
Eli Cohene126ba92013-07-07 17:25:49 +03001513
Eli Cohen2f5ff262017-01-03 23:55:21 +02001514 bfregi->ver = ver;
1515 bfregi->num_low_latency_bfregs = req.num_low_latency_bfregs;
Haggai Abramovskyf72300c2016-01-14 19:12:58 +02001516 context->cqe_version = resp.cqe_version;
Eli Cohen30aa60b2017-01-03 23:55:27 +02001517 context->lib_caps = req.lib_caps;
1518 print_lib_caps(dev, context->lib_caps);
Haggai Abramovskyf72300c2016-01-14 19:12:58 +02001519
Eli Cohene126ba92013-07-07 17:25:49 +03001520 return &context->ibucontext;
1521
majd@mellanox.com146d2f12016-01-14 19:13:02 +02001522out_td:
1523 if (MLX5_CAP_GEN(dev->mdev, log_max_transport_domain))
Huy Nguyenc85023e2017-05-30 09:42:54 +03001524 mlx5_ib_dealloc_transport_domain(dev, context->tdn);
majd@mellanox.com146d2f12016-01-14 19:13:02 +02001525
Artemy Kovalyov7d0cc6e2017-01-02 11:37:44 +02001526out_page:
1527 free_page(context->upd_xlt_page);
1528
Eli Cohene126ba92013-07-07 17:25:49 +03001529out_uars:
Eli Cohenb037c292017-01-03 23:55:26 +02001530 deallocate_uars(dev, context);
1531
1532out_sys_pages:
1533 kfree(bfregi->sys_pages);
1534
Eli Cohene126ba92013-07-07 17:25:49 +03001535out_count:
Eli Cohen2f5ff262017-01-03 23:55:21 +02001536 kfree(bfregi->count);
Eli Cohene126ba92013-07-07 17:25:49 +03001537
Eli Cohene126ba92013-07-07 17:25:49 +03001538out_ctx:
1539 kfree(context);
Eli Cohenb037c292017-01-03 23:55:26 +02001540
Eli Cohene126ba92013-07-07 17:25:49 +03001541 return ERR_PTR(err);
1542}
1543
1544static int mlx5_ib_dealloc_ucontext(struct ib_ucontext *ibcontext)
1545{
1546 struct mlx5_ib_ucontext *context = to_mucontext(ibcontext);
1547 struct mlx5_ib_dev *dev = to_mdev(ibcontext->device);
Eli Cohenb037c292017-01-03 23:55:26 +02001548 struct mlx5_bfreg_info *bfregi;
Eli Cohene126ba92013-07-07 17:25:49 +03001549
Eli Cohenb037c292017-01-03 23:55:26 +02001550 bfregi = &context->bfregi;
majd@mellanox.com146d2f12016-01-14 19:13:02 +02001551 if (MLX5_CAP_GEN(dev->mdev, log_max_transport_domain))
Huy Nguyenc85023e2017-05-30 09:42:54 +03001552 mlx5_ib_dealloc_transport_domain(dev, context->tdn);
majd@mellanox.com146d2f12016-01-14 19:13:02 +02001553
Artemy Kovalyov7d0cc6e2017-01-02 11:37:44 +02001554 free_page(context->upd_xlt_page);
Eli Cohenb037c292017-01-03 23:55:26 +02001555 deallocate_uars(dev, context);
1556 kfree(bfregi->sys_pages);
Eli Cohen2f5ff262017-01-03 23:55:21 +02001557 kfree(bfregi->count);
Eli Cohene126ba92013-07-07 17:25:49 +03001558 kfree(context);
1559
1560 return 0;
1561}
1562
Eli Cohenb037c292017-01-03 23:55:26 +02001563static phys_addr_t uar_index2pfn(struct mlx5_ib_dev *dev,
1564 struct mlx5_bfreg_info *bfregi,
1565 int idx)
Eli Cohene126ba92013-07-07 17:25:49 +03001566{
Eli Cohenb037c292017-01-03 23:55:26 +02001567 int fw_uars_per_page;
1568
1569 fw_uars_per_page = MLX5_CAP_GEN(dev->mdev, uar_4k) ? MLX5_UARS_IN_PAGE : 1;
1570
1571 return (pci_resource_start(dev->mdev->pdev, 0) >> PAGE_SHIFT) +
1572 bfregi->sys_pages[idx] / fw_uars_per_page;
Eli Cohene126ba92013-07-07 17:25:49 +03001573}
1574
1575static int get_command(unsigned long offset)
1576{
1577 return (offset >> MLX5_IB_MMAP_CMD_SHIFT) & MLX5_IB_MMAP_CMD_MASK;
1578}
1579
1580static int get_arg(unsigned long offset)
1581{
1582 return offset & ((1 << MLX5_IB_MMAP_CMD_SHIFT) - 1);
1583}
1584
1585static int get_index(unsigned long offset)
1586{
1587 return get_arg(offset);
1588}
1589
Maor Gottlieb7c2344c2016-06-17 14:56:44 +03001590static void mlx5_ib_vma_open(struct vm_area_struct *area)
1591{
1592 /* vma_open is called when a new VMA is created on top of our VMA. This
1593 * is done through either mremap flow or split_vma (usually due to
1594 * mlock, madvise, munmap, etc.) We do not support a clone of the VMA,
1595 * as this VMA is strongly hardware related. Therefore we set the
1596 * vm_ops of the newly created/cloned VMA to NULL, to prevent it from
1597 * calling us again and trying to do incorrect actions. We assume that
1598 * the original VMA size is exactly a single page, and therefore all
1599 * "splitting" operation will not happen to it.
1600 */
1601 area->vm_ops = NULL;
1602}
1603
1604static void mlx5_ib_vma_close(struct vm_area_struct *area)
1605{
1606 struct mlx5_ib_vma_private_data *mlx5_ib_vma_priv_data;
1607
1608 /* It's guaranteed that all VMAs opened on a FD are closed before the
1609 * file itself is closed, therefore no sync is needed with the regular
1610 * closing flow. (e.g. mlx5 ib_dealloc_ucontext)
1611 * However need a sync with accessing the vma as part of
1612 * mlx5_ib_disassociate_ucontext.
1613 * The close operation is usually called under mm->mmap_sem except when
1614 * process is exiting.
1615 * The exiting case is handled explicitly as part of
1616 * mlx5_ib_disassociate_ucontext.
1617 */
1618 mlx5_ib_vma_priv_data = (struct mlx5_ib_vma_private_data *)area->vm_private_data;
1619
1620 /* setting the vma context pointer to null in the mlx5_ib driver's
1621 * private data, to protect a race condition in
1622 * mlx5_ib_disassociate_ucontext().
1623 */
1624 mlx5_ib_vma_priv_data->vma = NULL;
1625 list_del(&mlx5_ib_vma_priv_data->list);
1626 kfree(mlx5_ib_vma_priv_data);
1627}
1628
1629static const struct vm_operations_struct mlx5_ib_vm_ops = {
1630 .open = mlx5_ib_vma_open,
1631 .close = mlx5_ib_vma_close
1632};
1633
1634static int mlx5_ib_set_vma_data(struct vm_area_struct *vma,
1635 struct mlx5_ib_ucontext *ctx)
1636{
1637 struct mlx5_ib_vma_private_data *vma_prv;
1638 struct list_head *vma_head = &ctx->vma_private_list;
1639
1640 vma_prv = kzalloc(sizeof(*vma_prv), GFP_KERNEL);
1641 if (!vma_prv)
1642 return -ENOMEM;
1643
1644 vma_prv->vma = vma;
1645 vma->vm_private_data = vma_prv;
1646 vma->vm_ops = &mlx5_ib_vm_ops;
1647
1648 list_add(&vma_prv->list, vma_head);
1649
1650 return 0;
1651}
1652
1653static void mlx5_ib_disassociate_ucontext(struct ib_ucontext *ibcontext)
1654{
1655 int ret;
1656 struct vm_area_struct *vma;
1657 struct mlx5_ib_vma_private_data *vma_private, *n;
1658 struct mlx5_ib_ucontext *context = to_mucontext(ibcontext);
1659 struct task_struct *owning_process = NULL;
1660 struct mm_struct *owning_mm = NULL;
1661
1662 owning_process = get_pid_task(ibcontext->tgid, PIDTYPE_PID);
1663 if (!owning_process)
1664 return;
1665
1666 owning_mm = get_task_mm(owning_process);
1667 if (!owning_mm) {
1668 pr_info("no mm, disassociate ucontext is pending task termination\n");
1669 while (1) {
1670 put_task_struct(owning_process);
1671 usleep_range(1000, 2000);
1672 owning_process = get_pid_task(ibcontext->tgid,
1673 PIDTYPE_PID);
1674 if (!owning_process ||
1675 owning_process->state == TASK_DEAD) {
1676 pr_info("disassociate ucontext done, task was terminated\n");
1677 /* in case task was dead need to release the
1678 * task struct.
1679 */
1680 if (owning_process)
1681 put_task_struct(owning_process);
1682 return;
1683 }
1684 }
1685 }
1686
1687 /* need to protect from a race on closing the vma as part of
1688 * mlx5_ib_vma_close.
1689 */
Maor Gottliebecc7d832017-03-29 06:03:02 +03001690 down_write(&owning_mm->mmap_sem);
Maor Gottlieb7c2344c2016-06-17 14:56:44 +03001691 list_for_each_entry_safe(vma_private, n, &context->vma_private_list,
1692 list) {
1693 vma = vma_private->vma;
1694 ret = zap_vma_ptes(vma, vma->vm_start,
1695 PAGE_SIZE);
1696 WARN_ONCE(ret, "%s: zap_vma_ptes failed", __func__);
1697 /* context going to be destroyed, should
1698 * not access ops any more.
1699 */
Maor Gottlieb13776612017-03-29 06:03:03 +03001700 vma->vm_flags &= ~(VM_SHARED | VM_MAYSHARE);
Maor Gottlieb7c2344c2016-06-17 14:56:44 +03001701 vma->vm_ops = NULL;
1702 list_del(&vma_private->list);
1703 kfree(vma_private);
1704 }
Maor Gottliebecc7d832017-03-29 06:03:02 +03001705 up_write(&owning_mm->mmap_sem);
Maor Gottlieb7c2344c2016-06-17 14:56:44 +03001706 mmput(owning_mm);
1707 put_task_struct(owning_process);
1708}
1709
Guy Levi37aa5c32016-04-27 16:49:50 +03001710static inline char *mmap_cmd2str(enum mlx5_ib_mmap_cmd cmd)
1711{
1712 switch (cmd) {
1713 case MLX5_IB_MMAP_WC_PAGE:
1714 return "WC";
1715 case MLX5_IB_MMAP_REGULAR_PAGE:
1716 return "best effort WC";
1717 case MLX5_IB_MMAP_NC_PAGE:
1718 return "NC";
1719 default:
1720 return NULL;
1721 }
1722}
1723
1724static int uar_mmap(struct mlx5_ib_dev *dev, enum mlx5_ib_mmap_cmd cmd,
Maor Gottlieb7c2344c2016-06-17 14:56:44 +03001725 struct vm_area_struct *vma,
1726 struct mlx5_ib_ucontext *context)
Guy Levi37aa5c32016-04-27 16:49:50 +03001727{
Eli Cohen2f5ff262017-01-03 23:55:21 +02001728 struct mlx5_bfreg_info *bfregi = &context->bfregi;
Guy Levi37aa5c32016-04-27 16:49:50 +03001729 int err;
1730 unsigned long idx;
1731 phys_addr_t pfn, pa;
1732 pgprot_t prot;
Eli Cohenb037c292017-01-03 23:55:26 +02001733 int uars_per_page;
1734
1735 if (vma->vm_end - vma->vm_start != PAGE_SIZE)
1736 return -EINVAL;
1737
1738 uars_per_page = get_uars_per_sys_page(dev, bfregi->lib_uar_4k);
1739 idx = get_index(vma->vm_pgoff);
1740 if (idx % uars_per_page ||
1741 idx * uars_per_page >= bfregi->num_sys_pages) {
1742 mlx5_ib_warn(dev, "invalid uar index %lu\n", idx);
1743 return -EINVAL;
1744 }
Guy Levi37aa5c32016-04-27 16:49:50 +03001745
1746 switch (cmd) {
1747 case MLX5_IB_MMAP_WC_PAGE:
1748/* Some architectures don't support WC memory */
1749#if defined(CONFIG_X86)
1750 if (!pat_enabled())
1751 return -EPERM;
1752#elif !(defined(CONFIG_PPC) || (defined(CONFIG_ARM) && defined(CONFIG_MMU)))
1753 return -EPERM;
1754#endif
1755 /* fall through */
1756 case MLX5_IB_MMAP_REGULAR_PAGE:
1757 /* For MLX5_IB_MMAP_REGULAR_PAGE do the best effort to get WC */
1758 prot = pgprot_writecombine(vma->vm_page_prot);
1759 break;
1760 case MLX5_IB_MMAP_NC_PAGE:
1761 prot = pgprot_noncached(vma->vm_page_prot);
1762 break;
1763 default:
1764 return -EINVAL;
1765 }
1766
Eli Cohenb037c292017-01-03 23:55:26 +02001767 pfn = uar_index2pfn(dev, bfregi, idx);
Guy Levi37aa5c32016-04-27 16:49:50 +03001768 mlx5_ib_dbg(dev, "uar idx 0x%lx, pfn %pa\n", idx, &pfn);
1769
1770 vma->vm_page_prot = prot;
1771 err = io_remap_pfn_range(vma, vma->vm_start, pfn,
1772 PAGE_SIZE, vma->vm_page_prot);
1773 if (err) {
1774 mlx5_ib_err(dev, "io_remap_pfn_range failed with error=%d, vm_start=0x%lx, pfn=%pa, mmap_cmd=%s\n",
1775 err, vma->vm_start, &pfn, mmap_cmd2str(cmd));
1776 return -EAGAIN;
1777 }
1778
1779 pa = pfn << PAGE_SHIFT;
1780 mlx5_ib_dbg(dev, "mapped %s at 0x%lx, PA %pa\n", mmap_cmd2str(cmd),
1781 vma->vm_start, &pa);
1782
Maor Gottlieb7c2344c2016-06-17 14:56:44 +03001783 return mlx5_ib_set_vma_data(vma, context);
Guy Levi37aa5c32016-04-27 16:49:50 +03001784}
1785
Eli Cohene126ba92013-07-07 17:25:49 +03001786static int mlx5_ib_mmap(struct ib_ucontext *ibcontext, struct vm_area_struct *vma)
1787{
1788 struct mlx5_ib_ucontext *context = to_mucontext(ibcontext);
1789 struct mlx5_ib_dev *dev = to_mdev(ibcontext->device);
Eli Cohene126ba92013-07-07 17:25:49 +03001790 unsigned long command;
Eli Cohene126ba92013-07-07 17:25:49 +03001791 phys_addr_t pfn;
1792
1793 command = get_command(vma->vm_pgoff);
1794 switch (command) {
Guy Levi37aa5c32016-04-27 16:49:50 +03001795 case MLX5_IB_MMAP_WC_PAGE:
1796 case MLX5_IB_MMAP_NC_PAGE:
Eli Cohene126ba92013-07-07 17:25:49 +03001797 case MLX5_IB_MMAP_REGULAR_PAGE:
Maor Gottlieb7c2344c2016-06-17 14:56:44 +03001798 return uar_mmap(dev, command, vma, context);
Eli Cohene126ba92013-07-07 17:25:49 +03001799
1800 case MLX5_IB_MMAP_GET_CONTIGUOUS_PAGES:
1801 return -ENOSYS;
1802
Matan Barakd69e3bc2015-12-15 20:30:13 +02001803 case MLX5_IB_MMAP_CORE_CLOCK:
Matan Barakd69e3bc2015-12-15 20:30:13 +02001804 if (vma->vm_end - vma->vm_start != PAGE_SIZE)
1805 return -EINVAL;
1806
Matan Barak6cbac1e2016-04-14 16:52:10 +03001807 if (vma->vm_flags & VM_WRITE)
Matan Barakd69e3bc2015-12-15 20:30:13 +02001808 return -EPERM;
1809
1810 /* Don't expose to user-space information it shouldn't have */
1811 if (PAGE_SIZE > 4096)
1812 return -EOPNOTSUPP;
1813
1814 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
1815 pfn = (dev->mdev->iseg_base +
1816 offsetof(struct mlx5_init_seg, internal_timer_h)) >>
1817 PAGE_SHIFT;
1818 if (io_remap_pfn_range(vma, vma->vm_start, pfn,
1819 PAGE_SIZE, vma->vm_page_prot))
1820 return -EAGAIN;
1821
1822 mlx5_ib_dbg(dev, "mapped internal timer at 0x%lx, PA 0x%llx\n",
1823 vma->vm_start,
1824 (unsigned long long)pfn << PAGE_SHIFT);
1825 break;
Matan Barakd69e3bc2015-12-15 20:30:13 +02001826
Eli Cohene126ba92013-07-07 17:25:49 +03001827 default:
1828 return -EINVAL;
1829 }
1830
1831 return 0;
1832}
1833
Eli Cohene126ba92013-07-07 17:25:49 +03001834static struct ib_pd *mlx5_ib_alloc_pd(struct ib_device *ibdev,
1835 struct ib_ucontext *context,
1836 struct ib_udata *udata)
1837{
1838 struct mlx5_ib_alloc_pd_resp resp;
1839 struct mlx5_ib_pd *pd;
1840 int err;
1841
1842 pd = kmalloc(sizeof(*pd), GFP_KERNEL);
1843 if (!pd)
1844 return ERR_PTR(-ENOMEM);
1845
Jack Morgenstein9603b612014-07-28 23:30:22 +03001846 err = mlx5_core_alloc_pd(to_mdev(ibdev)->mdev, &pd->pdn);
Eli Cohene126ba92013-07-07 17:25:49 +03001847 if (err) {
1848 kfree(pd);
1849 return ERR_PTR(err);
1850 }
1851
1852 if (context) {
1853 resp.pdn = pd->pdn;
1854 if (ib_copy_to_udata(udata, &resp, sizeof(resp))) {
Jack Morgenstein9603b612014-07-28 23:30:22 +03001855 mlx5_core_dealloc_pd(to_mdev(ibdev)->mdev, pd->pdn);
Eli Cohene126ba92013-07-07 17:25:49 +03001856 kfree(pd);
1857 return ERR_PTR(-EFAULT);
1858 }
Eli Cohene126ba92013-07-07 17:25:49 +03001859 }
1860
1861 return &pd->ibpd;
1862}
1863
1864static int mlx5_ib_dealloc_pd(struct ib_pd *pd)
1865{
1866 struct mlx5_ib_dev *mdev = to_mdev(pd->device);
1867 struct mlx5_ib_pd *mpd = to_mpd(pd);
1868
Jack Morgenstein9603b612014-07-28 23:30:22 +03001869 mlx5_core_dealloc_pd(mdev->mdev, mpd->pdn);
Eli Cohene126ba92013-07-07 17:25:49 +03001870 kfree(mpd);
1871
1872 return 0;
1873}
1874
Maor Gottlieb466fa6d2016-08-30 16:58:36 +03001875enum {
1876 MATCH_CRITERIA_ENABLE_OUTER_BIT,
1877 MATCH_CRITERIA_ENABLE_MISC_BIT,
1878 MATCH_CRITERIA_ENABLE_INNER_BIT
1879};
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02001880
Maor Gottlieb466fa6d2016-08-30 16:58:36 +03001881#define HEADER_IS_ZERO(match_criteria, headers) \
1882 !(memchr_inv(MLX5_ADDR_OF(fte_match_param, match_criteria, headers), \
1883 0, MLX5_FLD_SZ_BYTES(fte_match_param, headers))) \
1884
1885static u8 get_match_criteria_enable(u32 *match_criteria)
1886{
1887 u8 match_criteria_enable;
1888
1889 match_criteria_enable =
1890 (!HEADER_IS_ZERO(match_criteria, outer_headers)) <<
1891 MATCH_CRITERIA_ENABLE_OUTER_BIT;
1892 match_criteria_enable |=
1893 (!HEADER_IS_ZERO(match_criteria, misc_parameters)) <<
1894 MATCH_CRITERIA_ENABLE_MISC_BIT;
1895 match_criteria_enable |=
1896 (!HEADER_IS_ZERO(match_criteria, inner_headers)) <<
1897 MATCH_CRITERIA_ENABLE_INNER_BIT;
1898
1899 return match_criteria_enable;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02001900}
1901
Maor Gottliebca0d4752016-08-30 16:58:35 +03001902static void set_proto(void *outer_c, void *outer_v, u8 mask, u8 val)
1903{
1904 MLX5_SET(fte_match_set_lyr_2_4, outer_c, ip_protocol, mask);
1905 MLX5_SET(fte_match_set_lyr_2_4, outer_v, ip_protocol, val);
1906}
1907
Moses Reuben2d1e6972016-11-14 19:04:52 +02001908static void set_flow_label(void *misc_c, void *misc_v, u8 mask, u8 val,
1909 bool inner)
1910{
1911 if (inner) {
1912 MLX5_SET(fte_match_set_misc,
1913 misc_c, inner_ipv6_flow_label, mask);
1914 MLX5_SET(fte_match_set_misc,
1915 misc_v, inner_ipv6_flow_label, val);
1916 } else {
1917 MLX5_SET(fte_match_set_misc,
1918 misc_c, outer_ipv6_flow_label, mask);
1919 MLX5_SET(fte_match_set_misc,
1920 misc_v, outer_ipv6_flow_label, val);
1921 }
1922}
1923
Maor Gottliebca0d4752016-08-30 16:58:35 +03001924static void set_tos(void *outer_c, void *outer_v, u8 mask, u8 val)
1925{
1926 MLX5_SET(fte_match_set_lyr_2_4, outer_c, ip_ecn, mask);
1927 MLX5_SET(fte_match_set_lyr_2_4, outer_v, ip_ecn, val);
1928 MLX5_SET(fte_match_set_lyr_2_4, outer_c, ip_dscp, mask >> 2);
1929 MLX5_SET(fte_match_set_lyr_2_4, outer_v, ip_dscp, val >> 2);
1930}
1931
Maor Gottliebc47ac6a2016-08-30 16:58:31 +03001932#define LAST_ETH_FIELD vlan_tag
1933#define LAST_IB_FIELD sl
Maor Gottliebca0d4752016-08-30 16:58:35 +03001934#define LAST_IPV4_FIELD tos
Maor Gottlieb466fa6d2016-08-30 16:58:36 +03001935#define LAST_IPV6_FIELD traffic_class
Maor Gottliebc47ac6a2016-08-30 16:58:31 +03001936#define LAST_TCP_UDP_FIELD src_port
Moses Reubenffb30d82016-11-14 19:04:50 +02001937#define LAST_TUNNEL_FIELD tunnel_id
Moses Reuben2ac693f2017-01-18 14:59:50 +02001938#define LAST_FLOW_TAG_FIELD tag_id
Slava Shwartsmana22ed862017-04-03 13:13:52 +03001939#define LAST_DROP_FIELD size
Maor Gottliebc47ac6a2016-08-30 16:58:31 +03001940
1941/* Field is the last supported field */
1942#define FIELDS_NOT_SUPPORTED(filter, field)\
1943 memchr_inv((void *)&filter.field +\
1944 sizeof(filter.field), 0,\
1945 sizeof(filter) -\
1946 offsetof(typeof(filter), field) -\
1947 sizeof(filter.field))
1948
Ariel Levkovich19cc7522017-04-03 13:11:03 +03001949#define IPV4_VERSION 4
1950#define IPV6_VERSION 6
1951static int parse_flow_attr(struct mlx5_core_dev *mdev, u32 *match_c,
1952 u32 *match_v, const union ib_flow_spec *ib_spec,
Slava Shwartsmana22ed862017-04-03 13:13:52 +03001953 u32 *tag_id, bool *is_drop)
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02001954{
Maor Gottlieb466fa6d2016-08-30 16:58:36 +03001955 void *misc_params_c = MLX5_ADDR_OF(fte_match_param, match_c,
1956 misc_parameters);
1957 void *misc_params_v = MLX5_ADDR_OF(fte_match_param, match_v,
1958 misc_parameters);
Moses Reuben2d1e6972016-11-14 19:04:52 +02001959 void *headers_c;
1960 void *headers_v;
Ariel Levkovich19cc7522017-04-03 13:11:03 +03001961 int match_ipv;
Maor Gottlieb466fa6d2016-08-30 16:58:36 +03001962
Moses Reuben2d1e6972016-11-14 19:04:52 +02001963 if (ib_spec->type & IB_FLOW_SPEC_INNER) {
1964 headers_c = MLX5_ADDR_OF(fte_match_param, match_c,
1965 inner_headers);
1966 headers_v = MLX5_ADDR_OF(fte_match_param, match_v,
1967 inner_headers);
Ariel Levkovich19cc7522017-04-03 13:11:03 +03001968 match_ipv = MLX5_CAP_FLOWTABLE_NIC_RX(mdev,
1969 ft_field_support.inner_ip_version);
Moses Reuben2d1e6972016-11-14 19:04:52 +02001970 } else {
1971 headers_c = MLX5_ADDR_OF(fte_match_param, match_c,
1972 outer_headers);
1973 headers_v = MLX5_ADDR_OF(fte_match_param, match_v,
1974 outer_headers);
Ariel Levkovich19cc7522017-04-03 13:11:03 +03001975 match_ipv = MLX5_CAP_FLOWTABLE_NIC_RX(mdev,
1976 ft_field_support.outer_ip_version);
Moses Reuben2d1e6972016-11-14 19:04:52 +02001977 }
1978
1979 switch (ib_spec->type & ~IB_FLOW_SPEC_INNER) {
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02001980 case IB_FLOW_SPEC_ETH:
Maor Gottliebc47ac6a2016-08-30 16:58:31 +03001981 if (FIELDS_NOT_SUPPORTED(ib_spec->eth.mask, LAST_ETH_FIELD))
Leon Romanovsky1ffd3a22017-01-18 14:59:51 +02001982 return -EOPNOTSUPP;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02001983
Moses Reuben2d1e6972016-11-14 19:04:52 +02001984 ether_addr_copy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02001985 dmac_47_16),
1986 ib_spec->eth.mask.dst_mac);
Moses Reuben2d1e6972016-11-14 19:04:52 +02001987 ether_addr_copy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02001988 dmac_47_16),
1989 ib_spec->eth.val.dst_mac);
1990
Moses Reuben2d1e6972016-11-14 19:04:52 +02001991 ether_addr_copy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
Maor Gottliebee3da802016-09-12 19:16:24 +03001992 smac_47_16),
1993 ib_spec->eth.mask.src_mac);
Moses Reuben2d1e6972016-11-14 19:04:52 +02001994 ether_addr_copy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
Maor Gottliebee3da802016-09-12 19:16:24 +03001995 smac_47_16),
1996 ib_spec->eth.val.src_mac);
1997
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02001998 if (ib_spec->eth.mask.vlan_tag) {
Moses Reuben2d1e6972016-11-14 19:04:52 +02001999 MLX5_SET(fte_match_set_lyr_2_4, headers_c,
Mohamad Haj Yahia10543362016-10-09 16:25:43 +03002000 cvlan_tag, 1);
Moses Reuben2d1e6972016-11-14 19:04:52 +02002001 MLX5_SET(fte_match_set_lyr_2_4, headers_v,
Mohamad Haj Yahia10543362016-10-09 16:25:43 +03002002 cvlan_tag, 1);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002003
Moses Reuben2d1e6972016-11-14 19:04:52 +02002004 MLX5_SET(fte_match_set_lyr_2_4, headers_c,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002005 first_vid, ntohs(ib_spec->eth.mask.vlan_tag));
Moses Reuben2d1e6972016-11-14 19:04:52 +02002006 MLX5_SET(fte_match_set_lyr_2_4, headers_v,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002007 first_vid, ntohs(ib_spec->eth.val.vlan_tag));
2008
Moses Reuben2d1e6972016-11-14 19:04:52 +02002009 MLX5_SET(fte_match_set_lyr_2_4, headers_c,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002010 first_cfi,
2011 ntohs(ib_spec->eth.mask.vlan_tag) >> 12);
Moses Reuben2d1e6972016-11-14 19:04:52 +02002012 MLX5_SET(fte_match_set_lyr_2_4, headers_v,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002013 first_cfi,
2014 ntohs(ib_spec->eth.val.vlan_tag) >> 12);
2015
Moses Reuben2d1e6972016-11-14 19:04:52 +02002016 MLX5_SET(fte_match_set_lyr_2_4, headers_c,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002017 first_prio,
2018 ntohs(ib_spec->eth.mask.vlan_tag) >> 13);
Moses Reuben2d1e6972016-11-14 19:04:52 +02002019 MLX5_SET(fte_match_set_lyr_2_4, headers_v,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002020 first_prio,
2021 ntohs(ib_spec->eth.val.vlan_tag) >> 13);
2022 }
Moses Reuben2d1e6972016-11-14 19:04:52 +02002023 MLX5_SET(fte_match_set_lyr_2_4, headers_c,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002024 ethertype, ntohs(ib_spec->eth.mask.ether_type));
Moses Reuben2d1e6972016-11-14 19:04:52 +02002025 MLX5_SET(fte_match_set_lyr_2_4, headers_v,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002026 ethertype, ntohs(ib_spec->eth.val.ether_type));
2027 break;
2028 case IB_FLOW_SPEC_IPV4:
Maor Gottliebc47ac6a2016-08-30 16:58:31 +03002029 if (FIELDS_NOT_SUPPORTED(ib_spec->ipv4.mask, LAST_IPV4_FIELD))
Leon Romanovsky1ffd3a22017-01-18 14:59:51 +02002030 return -EOPNOTSUPP;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002031
Ariel Levkovich19cc7522017-04-03 13:11:03 +03002032 if (match_ipv) {
2033 MLX5_SET(fte_match_set_lyr_2_4, headers_c,
2034 ip_version, 0xf);
2035 MLX5_SET(fte_match_set_lyr_2_4, headers_v,
2036 ip_version, IPV4_VERSION);
2037 } else {
2038 MLX5_SET(fte_match_set_lyr_2_4, headers_c,
2039 ethertype, 0xffff);
2040 MLX5_SET(fte_match_set_lyr_2_4, headers_v,
2041 ethertype, ETH_P_IP);
2042 }
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002043
Moses Reuben2d1e6972016-11-14 19:04:52 +02002044 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002045 src_ipv4_src_ipv6.ipv4_layout.ipv4),
2046 &ib_spec->ipv4.mask.src_ip,
2047 sizeof(ib_spec->ipv4.mask.src_ip));
Moses Reuben2d1e6972016-11-14 19:04:52 +02002048 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002049 src_ipv4_src_ipv6.ipv4_layout.ipv4),
2050 &ib_spec->ipv4.val.src_ip,
2051 sizeof(ib_spec->ipv4.val.src_ip));
Moses Reuben2d1e6972016-11-14 19:04:52 +02002052 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002053 dst_ipv4_dst_ipv6.ipv4_layout.ipv4),
2054 &ib_spec->ipv4.mask.dst_ip,
2055 sizeof(ib_spec->ipv4.mask.dst_ip));
Moses Reuben2d1e6972016-11-14 19:04:52 +02002056 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002057 dst_ipv4_dst_ipv6.ipv4_layout.ipv4),
2058 &ib_spec->ipv4.val.dst_ip,
2059 sizeof(ib_spec->ipv4.val.dst_ip));
Maor Gottliebca0d4752016-08-30 16:58:35 +03002060
Moses Reuben2d1e6972016-11-14 19:04:52 +02002061 set_tos(headers_c, headers_v,
Maor Gottliebca0d4752016-08-30 16:58:35 +03002062 ib_spec->ipv4.mask.tos, ib_spec->ipv4.val.tos);
2063
Moses Reuben2d1e6972016-11-14 19:04:52 +02002064 set_proto(headers_c, headers_v,
Maor Gottliebca0d4752016-08-30 16:58:35 +03002065 ib_spec->ipv4.mask.proto, ib_spec->ipv4.val.proto);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002066 break;
Maor Gottlieb026bae02016-06-17 15:14:51 +03002067 case IB_FLOW_SPEC_IPV6:
Maor Gottliebc47ac6a2016-08-30 16:58:31 +03002068 if (FIELDS_NOT_SUPPORTED(ib_spec->ipv6.mask, LAST_IPV6_FIELD))
Leon Romanovsky1ffd3a22017-01-18 14:59:51 +02002069 return -EOPNOTSUPP;
Maor Gottlieb026bae02016-06-17 15:14:51 +03002070
Ariel Levkovich19cc7522017-04-03 13:11:03 +03002071 if (match_ipv) {
2072 MLX5_SET(fte_match_set_lyr_2_4, headers_c,
2073 ip_version, 0xf);
2074 MLX5_SET(fte_match_set_lyr_2_4, headers_v,
2075 ip_version, IPV6_VERSION);
2076 } else {
2077 MLX5_SET(fte_match_set_lyr_2_4, headers_c,
2078 ethertype, 0xffff);
2079 MLX5_SET(fte_match_set_lyr_2_4, headers_v,
2080 ethertype, ETH_P_IPV6);
2081 }
Maor Gottlieb026bae02016-06-17 15:14:51 +03002082
Moses Reuben2d1e6972016-11-14 19:04:52 +02002083 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
Maor Gottlieb026bae02016-06-17 15:14:51 +03002084 src_ipv4_src_ipv6.ipv6_layout.ipv6),
2085 &ib_spec->ipv6.mask.src_ip,
2086 sizeof(ib_spec->ipv6.mask.src_ip));
Moses Reuben2d1e6972016-11-14 19:04:52 +02002087 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
Maor Gottlieb026bae02016-06-17 15:14:51 +03002088 src_ipv4_src_ipv6.ipv6_layout.ipv6),
2089 &ib_spec->ipv6.val.src_ip,
2090 sizeof(ib_spec->ipv6.val.src_ip));
Moses Reuben2d1e6972016-11-14 19:04:52 +02002091 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
Maor Gottlieb026bae02016-06-17 15:14:51 +03002092 dst_ipv4_dst_ipv6.ipv6_layout.ipv6),
2093 &ib_spec->ipv6.mask.dst_ip,
2094 sizeof(ib_spec->ipv6.mask.dst_ip));
Moses Reuben2d1e6972016-11-14 19:04:52 +02002095 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
Maor Gottlieb026bae02016-06-17 15:14:51 +03002096 dst_ipv4_dst_ipv6.ipv6_layout.ipv6),
2097 &ib_spec->ipv6.val.dst_ip,
2098 sizeof(ib_spec->ipv6.val.dst_ip));
Maor Gottlieb466fa6d2016-08-30 16:58:36 +03002099
Moses Reuben2d1e6972016-11-14 19:04:52 +02002100 set_tos(headers_c, headers_v,
Maor Gottlieb466fa6d2016-08-30 16:58:36 +03002101 ib_spec->ipv6.mask.traffic_class,
2102 ib_spec->ipv6.val.traffic_class);
2103
Moses Reuben2d1e6972016-11-14 19:04:52 +02002104 set_proto(headers_c, headers_v,
Maor Gottlieb466fa6d2016-08-30 16:58:36 +03002105 ib_spec->ipv6.mask.next_hdr,
2106 ib_spec->ipv6.val.next_hdr);
2107
Moses Reuben2d1e6972016-11-14 19:04:52 +02002108 set_flow_label(misc_params_c, misc_params_v,
2109 ntohl(ib_spec->ipv6.mask.flow_label),
2110 ntohl(ib_spec->ipv6.val.flow_label),
2111 ib_spec->type & IB_FLOW_SPEC_INNER);
2112
Maor Gottlieb026bae02016-06-17 15:14:51 +03002113 break;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002114 case IB_FLOW_SPEC_TCP:
Maor Gottliebc47ac6a2016-08-30 16:58:31 +03002115 if (FIELDS_NOT_SUPPORTED(ib_spec->tcp_udp.mask,
2116 LAST_TCP_UDP_FIELD))
Leon Romanovsky1ffd3a22017-01-18 14:59:51 +02002117 return -EOPNOTSUPP;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002118
Moses Reuben2d1e6972016-11-14 19:04:52 +02002119 MLX5_SET(fte_match_set_lyr_2_4, headers_c, ip_protocol,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002120 0xff);
Moses Reuben2d1e6972016-11-14 19:04:52 +02002121 MLX5_SET(fte_match_set_lyr_2_4, headers_v, ip_protocol,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002122 IPPROTO_TCP);
2123
Moses Reuben2d1e6972016-11-14 19:04:52 +02002124 MLX5_SET(fte_match_set_lyr_2_4, headers_c, tcp_sport,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002125 ntohs(ib_spec->tcp_udp.mask.src_port));
Moses Reuben2d1e6972016-11-14 19:04:52 +02002126 MLX5_SET(fte_match_set_lyr_2_4, headers_v, tcp_sport,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002127 ntohs(ib_spec->tcp_udp.val.src_port));
2128
Moses Reuben2d1e6972016-11-14 19:04:52 +02002129 MLX5_SET(fte_match_set_lyr_2_4, headers_c, tcp_dport,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002130 ntohs(ib_spec->tcp_udp.mask.dst_port));
Moses Reuben2d1e6972016-11-14 19:04:52 +02002131 MLX5_SET(fte_match_set_lyr_2_4, headers_v, tcp_dport,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002132 ntohs(ib_spec->tcp_udp.val.dst_port));
2133 break;
2134 case IB_FLOW_SPEC_UDP:
Maor Gottliebc47ac6a2016-08-30 16:58:31 +03002135 if (FIELDS_NOT_SUPPORTED(ib_spec->tcp_udp.mask,
2136 LAST_TCP_UDP_FIELD))
Leon Romanovsky1ffd3a22017-01-18 14:59:51 +02002137 return -EOPNOTSUPP;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002138
Moses Reuben2d1e6972016-11-14 19:04:52 +02002139 MLX5_SET(fte_match_set_lyr_2_4, headers_c, ip_protocol,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002140 0xff);
Moses Reuben2d1e6972016-11-14 19:04:52 +02002141 MLX5_SET(fte_match_set_lyr_2_4, headers_v, ip_protocol,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002142 IPPROTO_UDP);
2143
Moses Reuben2d1e6972016-11-14 19:04:52 +02002144 MLX5_SET(fte_match_set_lyr_2_4, headers_c, udp_sport,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002145 ntohs(ib_spec->tcp_udp.mask.src_port));
Moses Reuben2d1e6972016-11-14 19:04:52 +02002146 MLX5_SET(fte_match_set_lyr_2_4, headers_v, udp_sport,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002147 ntohs(ib_spec->tcp_udp.val.src_port));
2148
Moses Reuben2d1e6972016-11-14 19:04:52 +02002149 MLX5_SET(fte_match_set_lyr_2_4, headers_c, udp_dport,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002150 ntohs(ib_spec->tcp_udp.mask.dst_port));
Moses Reuben2d1e6972016-11-14 19:04:52 +02002151 MLX5_SET(fte_match_set_lyr_2_4, headers_v, udp_dport,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002152 ntohs(ib_spec->tcp_udp.val.dst_port));
2153 break;
Moses Reubenffb30d82016-11-14 19:04:50 +02002154 case IB_FLOW_SPEC_VXLAN_TUNNEL:
2155 if (FIELDS_NOT_SUPPORTED(ib_spec->tunnel.mask,
2156 LAST_TUNNEL_FIELD))
Leon Romanovsky1ffd3a22017-01-18 14:59:51 +02002157 return -EOPNOTSUPP;
Moses Reubenffb30d82016-11-14 19:04:50 +02002158
2159 MLX5_SET(fte_match_set_misc, misc_params_c, vxlan_vni,
2160 ntohl(ib_spec->tunnel.mask.tunnel_id));
2161 MLX5_SET(fte_match_set_misc, misc_params_v, vxlan_vni,
2162 ntohl(ib_spec->tunnel.val.tunnel_id));
2163 break;
Moses Reuben2ac693f2017-01-18 14:59:50 +02002164 case IB_FLOW_SPEC_ACTION_TAG:
2165 if (FIELDS_NOT_SUPPORTED(ib_spec->flow_tag,
2166 LAST_FLOW_TAG_FIELD))
2167 return -EOPNOTSUPP;
2168 if (ib_spec->flow_tag.tag_id >= BIT(24))
2169 return -EINVAL;
2170
2171 *tag_id = ib_spec->flow_tag.tag_id;
2172 break;
Slava Shwartsmana22ed862017-04-03 13:13:52 +03002173 case IB_FLOW_SPEC_ACTION_DROP:
2174 if (FIELDS_NOT_SUPPORTED(ib_spec->drop,
2175 LAST_DROP_FIELD))
2176 return -EOPNOTSUPP;
2177 *is_drop = true;
2178 break;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002179 default:
2180 return -EINVAL;
2181 }
2182
2183 return 0;
2184}
2185
2186/* If a flow could catch both multicast and unicast packets,
2187 * it won't fall into the multicast flow steering table and this rule
2188 * could steal other multicast packets.
2189 */
Yishai Hadasa550ddf2017-08-17 15:52:33 +03002190static bool flow_is_multicast_only(const struct ib_flow_attr *ib_attr)
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002191{
Yishai Hadas81e30882017-06-08 16:15:09 +03002192 union ib_flow_spec *flow_spec;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002193
2194 if (ib_attr->type != IB_FLOW_ATTR_NORMAL ||
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002195 ib_attr->num_of_specs < 1)
2196 return false;
2197
Yishai Hadas81e30882017-06-08 16:15:09 +03002198 flow_spec = (union ib_flow_spec *)(ib_attr + 1);
2199 if (flow_spec->type == IB_FLOW_SPEC_IPV4) {
2200 struct ib_flow_spec_ipv4 *ipv4_spec;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002201
Yishai Hadas81e30882017-06-08 16:15:09 +03002202 ipv4_spec = (struct ib_flow_spec_ipv4 *)flow_spec;
2203 if (ipv4_is_multicast(ipv4_spec->val.dst_ip))
2204 return true;
2205
2206 return false;
2207 }
2208
2209 if (flow_spec->type == IB_FLOW_SPEC_ETH) {
2210 struct ib_flow_spec_eth *eth_spec;
2211
2212 eth_spec = (struct ib_flow_spec_eth *)flow_spec;
2213 return is_multicast_ether_addr(eth_spec->mask.dst_mac) &&
2214 is_multicast_ether_addr(eth_spec->val.dst_mac);
2215 }
2216
2217 return false;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002218}
2219
Ariel Levkovich19cc7522017-04-03 13:11:03 +03002220static bool is_valid_ethertype(struct mlx5_core_dev *mdev,
2221 const struct ib_flow_attr *flow_attr,
Ariel Levkovich0f750962017-04-03 13:11:02 +03002222 bool check_inner)
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002223{
2224 union ib_flow_spec *ib_spec = (union ib_flow_spec *)(flow_attr + 1);
Ariel Levkovich19cc7522017-04-03 13:11:03 +03002225 int match_ipv = check_inner ?
2226 MLX5_CAP_FLOWTABLE_NIC_RX(mdev,
2227 ft_field_support.inner_ip_version) :
2228 MLX5_CAP_FLOWTABLE_NIC_RX(mdev,
2229 ft_field_support.outer_ip_version);
Ariel Levkovich0f750962017-04-03 13:11:02 +03002230 int inner_bit = check_inner ? IB_FLOW_SPEC_INNER : 0;
2231 bool ipv4_spec_valid, ipv6_spec_valid;
2232 unsigned int ip_spec_type = 0;
2233 bool has_ethertype = false;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002234 unsigned int spec_index;
Ariel Levkovich0f750962017-04-03 13:11:02 +03002235 bool mask_valid = true;
2236 u16 eth_type = 0;
2237 bool type_valid;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002238
2239 /* Validate that ethertype is correct */
2240 for (spec_index = 0; spec_index < flow_attr->num_of_specs; spec_index++) {
Ariel Levkovich0f750962017-04-03 13:11:02 +03002241 if ((ib_spec->type == (IB_FLOW_SPEC_ETH | inner_bit)) &&
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002242 ib_spec->eth.mask.ether_type) {
Ariel Levkovich0f750962017-04-03 13:11:02 +03002243 mask_valid = (ib_spec->eth.mask.ether_type ==
2244 htons(0xffff));
2245 has_ethertype = true;
2246 eth_type = ntohs(ib_spec->eth.val.ether_type);
2247 } else if ((ib_spec->type == (IB_FLOW_SPEC_IPV4 | inner_bit)) ||
2248 (ib_spec->type == (IB_FLOW_SPEC_IPV6 | inner_bit))) {
2249 ip_spec_type = ib_spec->type;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002250 }
2251 ib_spec = (void *)ib_spec + ib_spec->size;
2252 }
Ariel Levkovich0f750962017-04-03 13:11:02 +03002253
2254 type_valid = (!has_ethertype) || (!ip_spec_type);
2255 if (!type_valid && mask_valid) {
2256 ipv4_spec_valid = (eth_type == ETH_P_IP) &&
2257 (ip_spec_type == (IB_FLOW_SPEC_IPV4 | inner_bit));
2258 ipv6_spec_valid = (eth_type == ETH_P_IPV6) &&
2259 (ip_spec_type == (IB_FLOW_SPEC_IPV6 | inner_bit));
Ariel Levkovich19cc7522017-04-03 13:11:03 +03002260
2261 type_valid = (ipv4_spec_valid) || (ipv6_spec_valid) ||
2262 (((eth_type == ETH_P_MPLS_UC) ||
2263 (eth_type == ETH_P_MPLS_MC)) && match_ipv);
Ariel Levkovich0f750962017-04-03 13:11:02 +03002264 }
2265
2266 return type_valid;
2267}
2268
Ariel Levkovich19cc7522017-04-03 13:11:03 +03002269static bool is_valid_attr(struct mlx5_core_dev *mdev,
2270 const struct ib_flow_attr *flow_attr)
Ariel Levkovich0f750962017-04-03 13:11:02 +03002271{
Ariel Levkovich19cc7522017-04-03 13:11:03 +03002272 return is_valid_ethertype(mdev, flow_attr, false) &&
2273 is_valid_ethertype(mdev, flow_attr, true);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002274}
2275
2276static void put_flow_table(struct mlx5_ib_dev *dev,
2277 struct mlx5_ib_flow_prio *prio, bool ft_added)
2278{
2279 prio->refcount -= !!ft_added;
2280 if (!prio->refcount) {
2281 mlx5_destroy_flow_table(prio->flow_table);
2282 prio->flow_table = NULL;
2283 }
2284}
2285
2286static int mlx5_ib_destroy_flow(struct ib_flow *flow_id)
2287{
2288 struct mlx5_ib_dev *dev = to_mdev(flow_id->qp->device);
2289 struct mlx5_ib_flow_handler *handler = container_of(flow_id,
2290 struct mlx5_ib_flow_handler,
2291 ibflow);
2292 struct mlx5_ib_flow_handler *iter, *tmp;
2293
2294 mutex_lock(&dev->flow_db.lock);
2295
2296 list_for_each_entry_safe(iter, tmp, &handler->list, list) {
Mark Bloch74491de2016-08-31 11:24:25 +00002297 mlx5_del_flow_rules(iter->rule);
Maor Gottliebcc0e5d42016-08-28 14:16:34 +03002298 put_flow_table(dev, iter->prio, true);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002299 list_del(&iter->list);
2300 kfree(iter);
2301 }
2302
Mark Bloch74491de2016-08-31 11:24:25 +00002303 mlx5_del_flow_rules(handler->rule);
Maor Gottlieb5497adc2016-08-28 14:16:31 +03002304 put_flow_table(dev, handler->prio, true);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002305 mutex_unlock(&dev->flow_db.lock);
2306
2307 kfree(handler);
2308
2309 return 0;
2310}
2311
Maor Gottlieb35d190112016-03-07 18:51:47 +02002312static int ib_prio_to_core_prio(unsigned int priority, bool dont_trap)
2313{
2314 priority *= 2;
2315 if (!dont_trap)
2316 priority++;
2317 return priority;
2318}
2319
Maor Gottliebcc0e5d42016-08-28 14:16:34 +03002320enum flow_table_type {
2321 MLX5_IB_FT_RX,
2322 MLX5_IB_FT_TX
2323};
2324
Maor Gottlieb00b7c2a2017-03-29 06:09:01 +03002325#define MLX5_FS_MAX_TYPES 6
2326#define MLX5_FS_MAX_ENTRIES BIT(16)
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002327static struct mlx5_ib_flow_prio *get_flow_table(struct mlx5_ib_dev *dev,
Maor Gottliebcc0e5d42016-08-28 14:16:34 +03002328 struct ib_flow_attr *flow_attr,
2329 enum flow_table_type ft_type)
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002330{
Maor Gottlieb35d190112016-03-07 18:51:47 +02002331 bool dont_trap = flow_attr->flags & IB_FLOW_ATTR_FLAGS_DONT_TRAP;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002332 struct mlx5_flow_namespace *ns = NULL;
2333 struct mlx5_ib_flow_prio *prio;
2334 struct mlx5_flow_table *ft;
Maor Gottliebdac388e2017-03-29 06:09:00 +03002335 int max_table_size;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002336 int num_entries;
2337 int num_groups;
2338 int priority;
2339 int err = 0;
2340
Maor Gottliebdac388e2017-03-29 06:09:00 +03002341 max_table_size = BIT(MLX5_CAP_FLOWTABLE_NIC_RX(dev->mdev,
2342 log_max_ft_size));
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002343 if (flow_attr->type == IB_FLOW_ATTR_NORMAL) {
Maor Gottlieb35d190112016-03-07 18:51:47 +02002344 if (flow_is_multicast_only(flow_attr) &&
2345 !dont_trap)
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002346 priority = MLX5_IB_FLOW_MCAST_PRIO;
2347 else
Maor Gottlieb35d190112016-03-07 18:51:47 +02002348 priority = ib_prio_to_core_prio(flow_attr->priority,
2349 dont_trap);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002350 ns = mlx5_get_flow_namespace(dev->mdev,
2351 MLX5_FLOW_NAMESPACE_BYPASS);
2352 num_entries = MLX5_FS_MAX_ENTRIES;
2353 num_groups = MLX5_FS_MAX_TYPES;
2354 prio = &dev->flow_db.prios[priority];
2355 } else if (flow_attr->type == IB_FLOW_ATTR_ALL_DEFAULT ||
2356 flow_attr->type == IB_FLOW_ATTR_MC_DEFAULT) {
2357 ns = mlx5_get_flow_namespace(dev->mdev,
2358 MLX5_FLOW_NAMESPACE_LEFTOVERS);
2359 build_leftovers_ft_param(&priority,
2360 &num_entries,
2361 &num_groups);
2362 prio = &dev->flow_db.prios[MLX5_IB_FLOW_LEFTOVERS_PRIO];
Maor Gottliebcc0e5d42016-08-28 14:16:34 +03002363 } else if (flow_attr->type == IB_FLOW_ATTR_SNIFFER) {
2364 if (!MLX5_CAP_FLOWTABLE(dev->mdev,
2365 allow_sniffer_and_nic_rx_shared_tir))
2366 return ERR_PTR(-ENOTSUPP);
2367
2368 ns = mlx5_get_flow_namespace(dev->mdev, ft_type == MLX5_IB_FT_RX ?
2369 MLX5_FLOW_NAMESPACE_SNIFFER_RX :
2370 MLX5_FLOW_NAMESPACE_SNIFFER_TX);
2371
2372 prio = &dev->flow_db.sniffer[ft_type];
2373 priority = 0;
2374 num_entries = 1;
2375 num_groups = 1;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002376 }
2377
2378 if (!ns)
2379 return ERR_PTR(-ENOTSUPP);
2380
Maor Gottliebdac388e2017-03-29 06:09:00 +03002381 if (num_entries > max_table_size)
2382 return ERR_PTR(-ENOMEM);
2383
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002384 ft = prio->flow_table;
2385 if (!ft) {
2386 ft = mlx5_create_auto_grouped_flow_table(ns, priority,
2387 num_entries,
Maor Gottliebd63cd282016-04-29 01:36:35 +03002388 num_groups,
Hadar Hen Zionc9f1b072016-11-07 15:14:44 +02002389 0, 0);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002390
2391 if (!IS_ERR(ft)) {
2392 prio->refcount = 0;
2393 prio->flow_table = ft;
2394 } else {
2395 err = PTR_ERR(ft);
2396 }
2397 }
2398
2399 return err ? ERR_PTR(err) : prio;
2400}
2401
Yishai Hadasa550ddf2017-08-17 15:52:33 +03002402static void set_underlay_qp(struct mlx5_ib_dev *dev,
2403 struct mlx5_flow_spec *spec,
2404 u32 underlay_qpn)
2405{
2406 void *misc_params_c = MLX5_ADDR_OF(fte_match_param,
2407 spec->match_criteria,
2408 misc_parameters);
2409 void *misc_params_v = MLX5_ADDR_OF(fte_match_param, spec->match_value,
2410 misc_parameters);
2411
2412 if (underlay_qpn &&
2413 MLX5_CAP_FLOWTABLE_NIC_RX(dev->mdev,
2414 ft_field_support.bth_dst_qp)) {
2415 MLX5_SET(fte_match_set_misc,
2416 misc_params_v, bth_dst_qp, underlay_qpn);
2417 MLX5_SET(fte_match_set_misc,
2418 misc_params_c, bth_dst_qp, 0xffffff);
2419 }
2420}
2421
2422static struct mlx5_ib_flow_handler *_create_flow_rule(struct mlx5_ib_dev *dev,
2423 struct mlx5_ib_flow_prio *ft_prio,
2424 const struct ib_flow_attr *flow_attr,
2425 struct mlx5_flow_destination *dst,
2426 u32 underlay_qpn)
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002427{
2428 struct mlx5_flow_table *ft = ft_prio->flow_table;
2429 struct mlx5_ib_flow_handler *handler;
Hadar Hen Zion66958ed2016-11-07 15:14:45 +02002430 struct mlx5_flow_act flow_act = {0};
Maor Gottliebc5bb1732016-07-04 17:23:05 +03002431 struct mlx5_flow_spec *spec;
Slava Shwartsmana22ed862017-04-03 13:13:52 +03002432 struct mlx5_flow_destination *rule_dst = dst;
Maor Gottliebdd063d02016-08-28 14:16:32 +03002433 const void *ib_flow = (const void *)flow_attr + sizeof(*flow_attr);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002434 unsigned int spec_index;
Moses Reuben2ac693f2017-01-18 14:59:50 +02002435 u32 flow_tag = MLX5_FS_DEFAULT_FLOW_TAG;
Slava Shwartsmana22ed862017-04-03 13:13:52 +03002436 bool is_drop = false;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002437 int err = 0;
Slava Shwartsmana22ed862017-04-03 13:13:52 +03002438 int dest_num = 1;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002439
Ariel Levkovich19cc7522017-04-03 13:11:03 +03002440 if (!is_valid_attr(dev->mdev, flow_attr))
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002441 return ERR_PTR(-EINVAL);
2442
Leon Romanovsky1b9a07e2017-05-10 21:32:18 +03002443 spec = kvzalloc(sizeof(*spec), GFP_KERNEL);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002444 handler = kzalloc(sizeof(*handler), GFP_KERNEL);
Maor Gottliebc5bb1732016-07-04 17:23:05 +03002445 if (!handler || !spec) {
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002446 err = -ENOMEM;
2447 goto free;
2448 }
2449
2450 INIT_LIST_HEAD(&handler->list);
2451
2452 for (spec_index = 0; spec_index < flow_attr->num_of_specs; spec_index++) {
Ariel Levkovich19cc7522017-04-03 13:11:03 +03002453 err = parse_flow_attr(dev->mdev, spec->match_criteria,
Slava Shwartsmana22ed862017-04-03 13:13:52 +03002454 spec->match_value,
2455 ib_flow, &flow_tag, &is_drop);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002456 if (err < 0)
2457 goto free;
2458
2459 ib_flow += ((union ib_flow_spec *)ib_flow)->size;
2460 }
2461
Yishai Hadasa550ddf2017-08-17 15:52:33 +03002462 if (!flow_is_multicast_only(flow_attr))
2463 set_underlay_qp(dev, spec, underlay_qpn);
2464
Maor Gottlieb466fa6d2016-08-30 16:58:36 +03002465 spec->match_criteria_enable = get_match_criteria_enable(spec->match_criteria);
Slava Shwartsmana22ed862017-04-03 13:13:52 +03002466 if (is_drop) {
2467 flow_act.action = MLX5_FLOW_CONTEXT_ACTION_DROP;
2468 rule_dst = NULL;
2469 dest_num = 0;
2470 } else {
2471 flow_act.action = dst ? MLX5_FLOW_CONTEXT_ACTION_FWD_DEST :
2472 MLX5_FLOW_CONTEXT_ACTION_FWD_NEXT_PRIO;
2473 }
Moses Reuben2ac693f2017-01-18 14:59:50 +02002474
2475 if (flow_tag != MLX5_FS_DEFAULT_FLOW_TAG &&
2476 (flow_attr->type == IB_FLOW_ATTR_ALL_DEFAULT ||
2477 flow_attr->type == IB_FLOW_ATTR_MC_DEFAULT)) {
2478 mlx5_ib_warn(dev, "Flow tag %u and attribute type %x isn't allowed in leftovers\n",
2479 flow_tag, flow_attr->type);
2480 err = -EINVAL;
2481 goto free;
2482 }
2483 flow_act.flow_tag = flow_tag;
Mark Bloch74491de2016-08-31 11:24:25 +00002484 handler->rule = mlx5_add_flow_rules(ft, spec,
Hadar Hen Zion66958ed2016-11-07 15:14:45 +02002485 &flow_act,
Slava Shwartsmana22ed862017-04-03 13:13:52 +03002486 rule_dst, dest_num);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002487
2488 if (IS_ERR(handler->rule)) {
2489 err = PTR_ERR(handler->rule);
2490 goto free;
2491 }
2492
Maor Gottliebd9d49802016-08-28 14:16:33 +03002493 ft_prio->refcount++;
Maor Gottlieb5497adc2016-08-28 14:16:31 +03002494 handler->prio = ft_prio;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002495
2496 ft_prio->flow_table = ft;
2497free:
2498 if (err)
2499 kfree(handler);
Maor Gottliebc5bb1732016-07-04 17:23:05 +03002500 kvfree(spec);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002501 return err ? ERR_PTR(err) : handler;
2502}
2503
Yishai Hadasa550ddf2017-08-17 15:52:33 +03002504static struct mlx5_ib_flow_handler *create_flow_rule(struct mlx5_ib_dev *dev,
2505 struct mlx5_ib_flow_prio *ft_prio,
2506 const struct ib_flow_attr *flow_attr,
2507 struct mlx5_flow_destination *dst)
2508{
2509 return _create_flow_rule(dev, ft_prio, flow_attr, dst, 0);
2510}
2511
Maor Gottlieb35d190112016-03-07 18:51:47 +02002512static struct mlx5_ib_flow_handler *create_dont_trap_rule(struct mlx5_ib_dev *dev,
2513 struct mlx5_ib_flow_prio *ft_prio,
2514 struct ib_flow_attr *flow_attr,
2515 struct mlx5_flow_destination *dst)
2516{
2517 struct mlx5_ib_flow_handler *handler_dst = NULL;
2518 struct mlx5_ib_flow_handler *handler = NULL;
2519
2520 handler = create_flow_rule(dev, ft_prio, flow_attr, NULL);
2521 if (!IS_ERR(handler)) {
2522 handler_dst = create_flow_rule(dev, ft_prio,
2523 flow_attr, dst);
2524 if (IS_ERR(handler_dst)) {
Mark Bloch74491de2016-08-31 11:24:25 +00002525 mlx5_del_flow_rules(handler->rule);
Maor Gottliebd9d49802016-08-28 14:16:33 +03002526 ft_prio->refcount--;
Maor Gottlieb35d190112016-03-07 18:51:47 +02002527 kfree(handler);
2528 handler = handler_dst;
2529 } else {
2530 list_add(&handler_dst->list, &handler->list);
2531 }
2532 }
2533
2534 return handler;
2535}
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002536enum {
2537 LEFTOVERS_MC,
2538 LEFTOVERS_UC,
2539};
2540
2541static struct mlx5_ib_flow_handler *create_leftovers_rule(struct mlx5_ib_dev *dev,
2542 struct mlx5_ib_flow_prio *ft_prio,
2543 struct ib_flow_attr *flow_attr,
2544 struct mlx5_flow_destination *dst)
2545{
2546 struct mlx5_ib_flow_handler *handler_ucast = NULL;
2547 struct mlx5_ib_flow_handler *handler = NULL;
2548
2549 static struct {
2550 struct ib_flow_attr flow_attr;
2551 struct ib_flow_spec_eth eth_flow;
2552 } leftovers_specs[] = {
2553 [LEFTOVERS_MC] = {
2554 .flow_attr = {
2555 .num_of_specs = 1,
2556 .size = sizeof(leftovers_specs[0])
2557 },
2558 .eth_flow = {
2559 .type = IB_FLOW_SPEC_ETH,
2560 .size = sizeof(struct ib_flow_spec_eth),
2561 .mask = {.dst_mac = {0x1} },
2562 .val = {.dst_mac = {0x1} }
2563 }
2564 },
2565 [LEFTOVERS_UC] = {
2566 .flow_attr = {
2567 .num_of_specs = 1,
2568 .size = sizeof(leftovers_specs[0])
2569 },
2570 .eth_flow = {
2571 .type = IB_FLOW_SPEC_ETH,
2572 .size = sizeof(struct ib_flow_spec_eth),
2573 .mask = {.dst_mac = {0x1} },
2574 .val = {.dst_mac = {} }
2575 }
2576 }
2577 };
2578
2579 handler = create_flow_rule(dev, ft_prio,
2580 &leftovers_specs[LEFTOVERS_MC].flow_attr,
2581 dst);
2582 if (!IS_ERR(handler) &&
2583 flow_attr->type == IB_FLOW_ATTR_ALL_DEFAULT) {
2584 handler_ucast = create_flow_rule(dev, ft_prio,
2585 &leftovers_specs[LEFTOVERS_UC].flow_attr,
2586 dst);
2587 if (IS_ERR(handler_ucast)) {
Mark Bloch74491de2016-08-31 11:24:25 +00002588 mlx5_del_flow_rules(handler->rule);
Maor Gottliebd9d49802016-08-28 14:16:33 +03002589 ft_prio->refcount--;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002590 kfree(handler);
2591 handler = handler_ucast;
2592 } else {
2593 list_add(&handler_ucast->list, &handler->list);
2594 }
2595 }
2596
2597 return handler;
2598}
2599
Maor Gottliebcc0e5d42016-08-28 14:16:34 +03002600static struct mlx5_ib_flow_handler *create_sniffer_rule(struct mlx5_ib_dev *dev,
2601 struct mlx5_ib_flow_prio *ft_rx,
2602 struct mlx5_ib_flow_prio *ft_tx,
2603 struct mlx5_flow_destination *dst)
2604{
2605 struct mlx5_ib_flow_handler *handler_rx;
2606 struct mlx5_ib_flow_handler *handler_tx;
2607 int err;
2608 static const struct ib_flow_attr flow_attr = {
2609 .num_of_specs = 0,
2610 .size = sizeof(flow_attr)
2611 };
2612
2613 handler_rx = create_flow_rule(dev, ft_rx, &flow_attr, dst);
2614 if (IS_ERR(handler_rx)) {
2615 err = PTR_ERR(handler_rx);
2616 goto err;
2617 }
2618
2619 handler_tx = create_flow_rule(dev, ft_tx, &flow_attr, dst);
2620 if (IS_ERR(handler_tx)) {
2621 err = PTR_ERR(handler_tx);
2622 goto err_tx;
2623 }
2624
2625 list_add(&handler_tx->list, &handler_rx->list);
2626
2627 return handler_rx;
2628
2629err_tx:
Mark Bloch74491de2016-08-31 11:24:25 +00002630 mlx5_del_flow_rules(handler_rx->rule);
Maor Gottliebcc0e5d42016-08-28 14:16:34 +03002631 ft_rx->refcount--;
2632 kfree(handler_rx);
2633err:
2634 return ERR_PTR(err);
2635}
2636
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002637static struct ib_flow *mlx5_ib_create_flow(struct ib_qp *qp,
2638 struct ib_flow_attr *flow_attr,
2639 int domain)
2640{
2641 struct mlx5_ib_dev *dev = to_mdev(qp->device);
Yishai Hadasd9f88e52016-08-28 10:58:37 +03002642 struct mlx5_ib_qp *mqp = to_mqp(qp);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002643 struct mlx5_ib_flow_handler *handler = NULL;
2644 struct mlx5_flow_destination *dst = NULL;
Maor Gottliebcc0e5d42016-08-28 14:16:34 +03002645 struct mlx5_ib_flow_prio *ft_prio_tx = NULL;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002646 struct mlx5_ib_flow_prio *ft_prio;
2647 int err;
Yishai Hadasa550ddf2017-08-17 15:52:33 +03002648 int underlay_qpn;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002649
2650 if (flow_attr->priority > MLX5_IB_FLOW_LAST_PRIO)
Maor Gottliebdac388e2017-03-29 06:09:00 +03002651 return ERR_PTR(-ENOMEM);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002652
2653 if (domain != IB_FLOW_DOMAIN_USER ||
2654 flow_attr->port > MLX5_CAP_GEN(dev->mdev, num_ports) ||
Maor Gottlieb35d190112016-03-07 18:51:47 +02002655 (flow_attr->flags & ~IB_FLOW_ATTR_FLAGS_DONT_TRAP))
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002656 return ERR_PTR(-EINVAL);
2657
2658 dst = kzalloc(sizeof(*dst), GFP_KERNEL);
2659 if (!dst)
2660 return ERR_PTR(-ENOMEM);
2661
2662 mutex_lock(&dev->flow_db.lock);
2663
Maor Gottliebcc0e5d42016-08-28 14:16:34 +03002664 ft_prio = get_flow_table(dev, flow_attr, MLX5_IB_FT_RX);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002665 if (IS_ERR(ft_prio)) {
2666 err = PTR_ERR(ft_prio);
2667 goto unlock;
2668 }
Maor Gottliebcc0e5d42016-08-28 14:16:34 +03002669 if (flow_attr->type == IB_FLOW_ATTR_SNIFFER) {
2670 ft_prio_tx = get_flow_table(dev, flow_attr, MLX5_IB_FT_TX);
2671 if (IS_ERR(ft_prio_tx)) {
2672 err = PTR_ERR(ft_prio_tx);
2673 ft_prio_tx = NULL;
2674 goto destroy_ft;
2675 }
2676 }
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002677
2678 dst->type = MLX5_FLOW_DESTINATION_TYPE_TIR;
Yishai Hadasd9f88e52016-08-28 10:58:37 +03002679 if (mqp->flags & MLX5_IB_QP_RSS)
2680 dst->tir_num = mqp->rss_qp.tirn;
2681 else
2682 dst->tir_num = mqp->raw_packet_qp.rq.tirn;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002683
2684 if (flow_attr->type == IB_FLOW_ATTR_NORMAL) {
Maor Gottlieb35d190112016-03-07 18:51:47 +02002685 if (flow_attr->flags & IB_FLOW_ATTR_FLAGS_DONT_TRAP) {
2686 handler = create_dont_trap_rule(dev, ft_prio,
2687 flow_attr, dst);
2688 } else {
Yishai Hadasa550ddf2017-08-17 15:52:33 +03002689 underlay_qpn = (mqp->flags & MLX5_IB_QP_UNDERLAY) ?
2690 mqp->underlay_qpn : 0;
2691 handler = _create_flow_rule(dev, ft_prio, flow_attr,
2692 dst, underlay_qpn);
Maor Gottlieb35d190112016-03-07 18:51:47 +02002693 }
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002694 } else if (flow_attr->type == IB_FLOW_ATTR_ALL_DEFAULT ||
2695 flow_attr->type == IB_FLOW_ATTR_MC_DEFAULT) {
2696 handler = create_leftovers_rule(dev, ft_prio, flow_attr,
2697 dst);
Maor Gottliebcc0e5d42016-08-28 14:16:34 +03002698 } else if (flow_attr->type == IB_FLOW_ATTR_SNIFFER) {
2699 handler = create_sniffer_rule(dev, ft_prio, ft_prio_tx, dst);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002700 } else {
2701 err = -EINVAL;
2702 goto destroy_ft;
2703 }
2704
2705 if (IS_ERR(handler)) {
2706 err = PTR_ERR(handler);
2707 handler = NULL;
2708 goto destroy_ft;
2709 }
2710
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002711 mutex_unlock(&dev->flow_db.lock);
2712 kfree(dst);
2713
2714 return &handler->ibflow;
2715
2716destroy_ft:
2717 put_flow_table(dev, ft_prio, false);
Maor Gottliebcc0e5d42016-08-28 14:16:34 +03002718 if (ft_prio_tx)
2719 put_flow_table(dev, ft_prio_tx, false);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002720unlock:
2721 mutex_unlock(&dev->flow_db.lock);
2722 kfree(dst);
2723 kfree(handler);
2724 return ERR_PTR(err);
2725}
2726
Eli Cohene126ba92013-07-07 17:25:49 +03002727static int mlx5_ib_mcg_attach(struct ib_qp *ibqp, union ib_gid *gid, u16 lid)
2728{
2729 struct mlx5_ib_dev *dev = to_mdev(ibqp->device);
Yishai Hadas81e30882017-06-08 16:15:09 +03002730 struct mlx5_ib_qp *mqp = to_mqp(ibqp);
Eli Cohene126ba92013-07-07 17:25:49 +03002731 int err;
2732
Yishai Hadas81e30882017-06-08 16:15:09 +03002733 if (mqp->flags & MLX5_IB_QP_UNDERLAY) {
2734 mlx5_ib_dbg(dev, "Attaching a multi cast group to underlay QP is not supported\n");
2735 return -EOPNOTSUPP;
2736 }
2737
Jack Morgenstein9603b612014-07-28 23:30:22 +03002738 err = mlx5_core_attach_mcg(dev->mdev, gid, ibqp->qp_num);
Eli Cohene126ba92013-07-07 17:25:49 +03002739 if (err)
2740 mlx5_ib_warn(dev, "failed attaching QPN 0x%x, MGID %pI6\n",
2741 ibqp->qp_num, gid->raw);
2742
2743 return err;
2744}
2745
2746static int mlx5_ib_mcg_detach(struct ib_qp *ibqp, union ib_gid *gid, u16 lid)
2747{
2748 struct mlx5_ib_dev *dev = to_mdev(ibqp->device);
2749 int err;
2750
Jack Morgenstein9603b612014-07-28 23:30:22 +03002751 err = mlx5_core_detach_mcg(dev->mdev, gid, ibqp->qp_num);
Eli Cohene126ba92013-07-07 17:25:49 +03002752 if (err)
2753 mlx5_ib_warn(dev, "failed detaching QPN 0x%x, MGID %pI6\n",
2754 ibqp->qp_num, gid->raw);
2755
2756 return err;
2757}
2758
2759static int init_node_data(struct mlx5_ib_dev *dev)
2760{
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03002761 int err;
Eli Cohene126ba92013-07-07 17:25:49 +03002762
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03002763 err = mlx5_query_node_desc(dev, dev->ib_dev.node_desc);
Eli Cohene126ba92013-07-07 17:25:49 +03002764 if (err)
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03002765 return err;
Eli Cohene126ba92013-07-07 17:25:49 +03002766
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03002767 dev->mdev->rev_id = dev->mdev->pdev->revision;
Eli Cohene126ba92013-07-07 17:25:49 +03002768
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03002769 return mlx5_query_node_guid(dev, &dev->ib_dev.node_guid);
Eli Cohene126ba92013-07-07 17:25:49 +03002770}
2771
2772static ssize_t show_fw_pages(struct device *device, struct device_attribute *attr,
2773 char *buf)
2774{
2775 struct mlx5_ib_dev *dev =
2776 container_of(device, struct mlx5_ib_dev, ib_dev.dev);
2777
Jack Morgenstein9603b612014-07-28 23:30:22 +03002778 return sprintf(buf, "%d\n", dev->mdev->priv.fw_pages);
Eli Cohene126ba92013-07-07 17:25:49 +03002779}
2780
2781static ssize_t show_reg_pages(struct device *device,
2782 struct device_attribute *attr, char *buf)
2783{
2784 struct mlx5_ib_dev *dev =
2785 container_of(device, struct mlx5_ib_dev, ib_dev.dev);
2786
Haggai Eran6aec21f2014-12-11 17:04:23 +02002787 return sprintf(buf, "%d\n", atomic_read(&dev->mdev->priv.reg_pages));
Eli Cohene126ba92013-07-07 17:25:49 +03002788}
2789
2790static ssize_t show_hca(struct device *device, struct device_attribute *attr,
2791 char *buf)
2792{
2793 struct mlx5_ib_dev *dev =
2794 container_of(device, struct mlx5_ib_dev, ib_dev.dev);
Jack Morgenstein9603b612014-07-28 23:30:22 +03002795 return sprintf(buf, "MT%d\n", dev->mdev->pdev->device);
Eli Cohene126ba92013-07-07 17:25:49 +03002796}
2797
Eli Cohene126ba92013-07-07 17:25:49 +03002798static ssize_t show_rev(struct device *device, struct device_attribute *attr,
2799 char *buf)
2800{
2801 struct mlx5_ib_dev *dev =
2802 container_of(device, struct mlx5_ib_dev, ib_dev.dev);
Jack Morgenstein9603b612014-07-28 23:30:22 +03002803 return sprintf(buf, "%x\n", dev->mdev->rev_id);
Eli Cohene126ba92013-07-07 17:25:49 +03002804}
2805
2806static ssize_t show_board(struct device *device, struct device_attribute *attr,
2807 char *buf)
2808{
2809 struct mlx5_ib_dev *dev =
2810 container_of(device, struct mlx5_ib_dev, ib_dev.dev);
2811 return sprintf(buf, "%.*s\n", MLX5_BOARD_ID_LEN,
Jack Morgenstein9603b612014-07-28 23:30:22 +03002812 dev->mdev->board_id);
Eli Cohene126ba92013-07-07 17:25:49 +03002813}
2814
2815static DEVICE_ATTR(hw_rev, S_IRUGO, show_rev, NULL);
Eli Cohene126ba92013-07-07 17:25:49 +03002816static DEVICE_ATTR(hca_type, S_IRUGO, show_hca, NULL);
2817static DEVICE_ATTR(board_id, S_IRUGO, show_board, NULL);
2818static DEVICE_ATTR(fw_pages, S_IRUGO, show_fw_pages, NULL);
2819static DEVICE_ATTR(reg_pages, S_IRUGO, show_reg_pages, NULL);
2820
2821static struct device_attribute *mlx5_class_attributes[] = {
2822 &dev_attr_hw_rev,
Eli Cohene126ba92013-07-07 17:25:49 +03002823 &dev_attr_hca_type,
2824 &dev_attr_board_id,
2825 &dev_attr_fw_pages,
2826 &dev_attr_reg_pages,
2827};
2828
Haggai Eran7722f472016-02-29 15:45:07 +02002829static void pkey_change_handler(struct work_struct *work)
2830{
2831 struct mlx5_ib_port_resources *ports =
2832 container_of(work, struct mlx5_ib_port_resources,
2833 pkey_change_work);
2834
2835 mutex_lock(&ports->devr->mutex);
2836 mlx5_ib_gsi_pkey_change(ports->gsi);
2837 mutex_unlock(&ports->devr->mutex);
2838}
2839
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03002840static void mlx5_ib_handle_internal_error(struct mlx5_ib_dev *ibdev)
2841{
2842 struct mlx5_ib_qp *mqp;
2843 struct mlx5_ib_cq *send_mcq, *recv_mcq;
2844 struct mlx5_core_cq *mcq;
2845 struct list_head cq_armed_list;
2846 unsigned long flags_qp;
2847 unsigned long flags_cq;
2848 unsigned long flags;
2849
2850 INIT_LIST_HEAD(&cq_armed_list);
2851
2852 /* Go over qp list reside on that ibdev, sync with create/destroy qp.*/
2853 spin_lock_irqsave(&ibdev->reset_flow_resource_lock, flags);
2854 list_for_each_entry(mqp, &ibdev->qp_list, qps_list) {
2855 spin_lock_irqsave(&mqp->sq.lock, flags_qp);
2856 if (mqp->sq.tail != mqp->sq.head) {
2857 send_mcq = to_mcq(mqp->ibqp.send_cq);
2858 spin_lock_irqsave(&send_mcq->lock, flags_cq);
2859 if (send_mcq->mcq.comp &&
2860 mqp->ibqp.send_cq->comp_handler) {
2861 if (!send_mcq->mcq.reset_notify_added) {
2862 send_mcq->mcq.reset_notify_added = 1;
2863 list_add_tail(&send_mcq->mcq.reset_notify,
2864 &cq_armed_list);
2865 }
2866 }
2867 spin_unlock_irqrestore(&send_mcq->lock, flags_cq);
2868 }
2869 spin_unlock_irqrestore(&mqp->sq.lock, flags_qp);
2870 spin_lock_irqsave(&mqp->rq.lock, flags_qp);
2871 /* no handling is needed for SRQ */
2872 if (!mqp->ibqp.srq) {
2873 if (mqp->rq.tail != mqp->rq.head) {
2874 recv_mcq = to_mcq(mqp->ibqp.recv_cq);
2875 spin_lock_irqsave(&recv_mcq->lock, flags_cq);
2876 if (recv_mcq->mcq.comp &&
2877 mqp->ibqp.recv_cq->comp_handler) {
2878 if (!recv_mcq->mcq.reset_notify_added) {
2879 recv_mcq->mcq.reset_notify_added = 1;
2880 list_add_tail(&recv_mcq->mcq.reset_notify,
2881 &cq_armed_list);
2882 }
2883 }
2884 spin_unlock_irqrestore(&recv_mcq->lock,
2885 flags_cq);
2886 }
2887 }
2888 spin_unlock_irqrestore(&mqp->rq.lock, flags_qp);
2889 }
2890 /*At that point all inflight post send were put to be executed as of we
2891 * lock/unlock above locks Now need to arm all involved CQs.
2892 */
2893 list_for_each_entry(mcq, &cq_armed_list, reset_notify) {
2894 mcq->comp(mcq);
2895 }
2896 spin_unlock_irqrestore(&ibdev->reset_flow_resource_lock, flags);
2897}
2898
Maor Gottlieb03404e82017-05-30 10:29:13 +03002899static void delay_drop_handler(struct work_struct *work)
2900{
2901 int err;
2902 struct mlx5_ib_delay_drop *delay_drop =
2903 container_of(work, struct mlx5_ib_delay_drop,
2904 delay_drop_work);
2905
Maor Gottliebfe248c32017-05-30 10:29:14 +03002906 atomic_inc(&delay_drop->events_cnt);
2907
Maor Gottlieb03404e82017-05-30 10:29:13 +03002908 mutex_lock(&delay_drop->lock);
2909 err = mlx5_core_set_delay_drop(delay_drop->dev->mdev,
2910 delay_drop->timeout);
2911 if (err) {
2912 mlx5_ib_warn(delay_drop->dev, "Failed to set delay drop, timeout=%u\n",
2913 delay_drop->timeout);
2914 delay_drop->activate = false;
2915 }
2916 mutex_unlock(&delay_drop->lock);
2917}
2918
Jack Morgenstein9603b612014-07-28 23:30:22 +03002919static void mlx5_ib_event(struct mlx5_core_dev *dev, void *context,
Jack Morgenstein4d2f9bb2014-07-28 23:30:24 +03002920 enum mlx5_dev_event event, unsigned long param)
Eli Cohene126ba92013-07-07 17:25:49 +03002921{
Jack Morgenstein9603b612014-07-28 23:30:22 +03002922 struct mlx5_ib_dev *ibdev = (struct mlx5_ib_dev *)context;
Eli Cohene126ba92013-07-07 17:25:49 +03002923 struct ib_event ibev;
Eli Cohendbaaff22016-10-27 16:36:44 +03002924 bool fatal = false;
Eli Cohene126ba92013-07-07 17:25:49 +03002925 u8 port = 0;
2926
2927 switch (event) {
2928 case MLX5_DEV_EVENT_SYS_ERROR:
Eli Cohene126ba92013-07-07 17:25:49 +03002929 ibev.event = IB_EVENT_DEVICE_FATAL;
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03002930 mlx5_ib_handle_internal_error(ibdev);
Eli Cohendbaaff22016-10-27 16:36:44 +03002931 fatal = true;
Eli Cohene126ba92013-07-07 17:25:49 +03002932 break;
2933
2934 case MLX5_DEV_EVENT_PORT_UP:
Eli Cohene126ba92013-07-07 17:25:49 +03002935 case MLX5_DEV_EVENT_PORT_DOWN:
Noa Osherovich2788cf32016-06-04 15:15:29 +03002936 case MLX5_DEV_EVENT_PORT_INITIALIZED:
Jack Morgenstein4d2f9bb2014-07-28 23:30:24 +03002937 port = (u8)param;
Aviv Heller5ec8c832016-09-18 20:48:00 +03002938
2939 /* In RoCE, port up/down events are handled in
2940 * mlx5_netdev_event().
2941 */
2942 if (mlx5_ib_port_link_layer(&ibdev->ib_dev, port) ==
2943 IB_LINK_LAYER_ETHERNET)
2944 return;
2945
2946 ibev.event = (event == MLX5_DEV_EVENT_PORT_UP) ?
2947 IB_EVENT_PORT_ACTIVE : IB_EVENT_PORT_ERR;
Eli Cohene126ba92013-07-07 17:25:49 +03002948 break;
2949
Eli Cohene126ba92013-07-07 17:25:49 +03002950 case MLX5_DEV_EVENT_LID_CHANGE:
2951 ibev.event = IB_EVENT_LID_CHANGE;
Jack Morgenstein4d2f9bb2014-07-28 23:30:24 +03002952 port = (u8)param;
Eli Cohene126ba92013-07-07 17:25:49 +03002953 break;
2954
2955 case MLX5_DEV_EVENT_PKEY_CHANGE:
2956 ibev.event = IB_EVENT_PKEY_CHANGE;
Jack Morgenstein4d2f9bb2014-07-28 23:30:24 +03002957 port = (u8)param;
Haggai Eran7722f472016-02-29 15:45:07 +02002958
2959 schedule_work(&ibdev->devr.ports[port - 1].pkey_change_work);
Eli Cohene126ba92013-07-07 17:25:49 +03002960 break;
2961
2962 case MLX5_DEV_EVENT_GUID_CHANGE:
2963 ibev.event = IB_EVENT_GID_CHANGE;
Jack Morgenstein4d2f9bb2014-07-28 23:30:24 +03002964 port = (u8)param;
Eli Cohene126ba92013-07-07 17:25:49 +03002965 break;
2966
2967 case MLX5_DEV_EVENT_CLIENT_REREG:
2968 ibev.event = IB_EVENT_CLIENT_REREGISTER;
Jack Morgenstein4d2f9bb2014-07-28 23:30:24 +03002969 port = (u8)param;
Eli Cohene126ba92013-07-07 17:25:49 +03002970 break;
Maor Gottlieb03404e82017-05-30 10:29:13 +03002971 case MLX5_DEV_EVENT_DELAY_DROP_TIMEOUT:
2972 schedule_work(&ibdev->delay_drop.delay_drop_work);
2973 goto out;
Saeed Mahameedbdc37922016-09-29 19:35:38 +03002974 default:
Maor Gottlieb03404e82017-05-30 10:29:13 +03002975 goto out;
Eli Cohene126ba92013-07-07 17:25:49 +03002976 }
2977
2978 ibev.device = &ibdev->ib_dev;
2979 ibev.element.port_num = port;
2980
Eli Cohena0c84c32013-09-11 16:35:27 +03002981 if (port < 1 || port > ibdev->num_ports) {
2982 mlx5_ib_warn(ibdev, "warning: event on port %d\n", port);
Maor Gottlieb03404e82017-05-30 10:29:13 +03002983 goto out;
Eli Cohena0c84c32013-09-11 16:35:27 +03002984 }
2985
Eli Cohene126ba92013-07-07 17:25:49 +03002986 if (ibdev->ib_active)
2987 ib_dispatch_event(&ibev);
Eli Cohendbaaff22016-10-27 16:36:44 +03002988
2989 if (fatal)
2990 ibdev->ib_active = false;
Maor Gottlieb03404e82017-05-30 10:29:13 +03002991
2992out:
2993 return;
Eli Cohene126ba92013-07-07 17:25:49 +03002994}
2995
Maor Gottliebc43f1112017-01-18 14:10:33 +02002996static int set_has_smi_cap(struct mlx5_ib_dev *dev)
2997{
2998 struct mlx5_hca_vport_context vport_ctx;
2999 int err;
3000 int port;
3001
3002 for (port = 1; port <= MLX5_CAP_GEN(dev->mdev, num_ports); port++) {
3003 dev->mdev->port_caps[port - 1].has_smi = false;
3004 if (MLX5_CAP_GEN(dev->mdev, port_type) ==
3005 MLX5_CAP_PORT_TYPE_IB) {
3006 if (MLX5_CAP_GEN(dev->mdev, ib_virt)) {
3007 err = mlx5_query_hca_vport_context(dev->mdev, 0,
3008 port, 0,
3009 &vport_ctx);
3010 if (err) {
3011 mlx5_ib_err(dev, "query_hca_vport_context for port=%d failed %d\n",
3012 port, err);
3013 return err;
3014 }
3015 dev->mdev->port_caps[port - 1].has_smi =
3016 vport_ctx.has_smi;
3017 } else {
3018 dev->mdev->port_caps[port - 1].has_smi = true;
3019 }
3020 }
3021 }
3022 return 0;
3023}
3024
Eli Cohene126ba92013-07-07 17:25:49 +03003025static void get_ext_port_caps(struct mlx5_ib_dev *dev)
3026{
3027 int port;
3028
Saeed Mahameed938fe832015-05-28 22:28:41 +03003029 for (port = 1; port <= MLX5_CAP_GEN(dev->mdev, num_ports); port++)
Eli Cohene126ba92013-07-07 17:25:49 +03003030 mlx5_query_ext_port_caps(dev, port);
3031}
3032
3033static int get_port_caps(struct mlx5_ib_dev *dev)
3034{
3035 struct ib_device_attr *dprops = NULL;
3036 struct ib_port_attr *pprops = NULL;
Dan Carpenterf614fc12015-01-12 11:56:58 +03003037 int err = -ENOMEM;
Eli Cohene126ba92013-07-07 17:25:49 +03003038 int port;
Matan Barak2528e332015-06-11 16:35:25 +03003039 struct ib_udata uhw = {.inlen = 0, .outlen = 0};
Eli Cohene126ba92013-07-07 17:25:49 +03003040
3041 pprops = kmalloc(sizeof(*pprops), GFP_KERNEL);
3042 if (!pprops)
3043 goto out;
3044
3045 dprops = kmalloc(sizeof(*dprops), GFP_KERNEL);
3046 if (!dprops)
3047 goto out;
3048
Maor Gottliebc43f1112017-01-18 14:10:33 +02003049 err = set_has_smi_cap(dev);
3050 if (err)
3051 goto out;
3052
Matan Barak2528e332015-06-11 16:35:25 +03003053 err = mlx5_ib_query_device(&dev->ib_dev, dprops, &uhw);
Eli Cohene126ba92013-07-07 17:25:49 +03003054 if (err) {
3055 mlx5_ib_warn(dev, "query_device failed %d\n", err);
3056 goto out;
3057 }
3058
Saeed Mahameed938fe832015-05-28 22:28:41 +03003059 for (port = 1; port <= MLX5_CAP_GEN(dev->mdev, num_ports); port++) {
Or Gerlitzc4550c62017-01-24 13:02:39 +02003060 memset(pprops, 0, sizeof(*pprops));
Eli Cohene126ba92013-07-07 17:25:49 +03003061 err = mlx5_ib_query_port(&dev->ib_dev, port, pprops);
3062 if (err) {
Saeed Mahameed938fe832015-05-28 22:28:41 +03003063 mlx5_ib_warn(dev, "query_port %d failed %d\n",
3064 port, err);
Eli Cohene126ba92013-07-07 17:25:49 +03003065 break;
3066 }
Saeed Mahameed938fe832015-05-28 22:28:41 +03003067 dev->mdev->port_caps[port - 1].pkey_table_len =
3068 dprops->max_pkeys;
3069 dev->mdev->port_caps[port - 1].gid_table_len =
3070 pprops->gid_tbl_len;
Eli Cohene126ba92013-07-07 17:25:49 +03003071 mlx5_ib_dbg(dev, "pkey_table_len %d, gid_table_len %d\n",
3072 dprops->max_pkeys, pprops->gid_tbl_len);
3073 }
3074
3075out:
3076 kfree(pprops);
3077 kfree(dprops);
3078
3079 return err;
3080}
3081
3082static void destroy_umrc_res(struct mlx5_ib_dev *dev)
3083{
3084 int err;
3085
3086 err = mlx5_mr_cache_cleanup(dev);
3087 if (err)
3088 mlx5_ib_warn(dev, "mr cache cleanup failed\n");
3089
3090 mlx5_ib_destroy_qp(dev->umrc.qp);
Christoph Hellwigadd08d72016-03-03 09:38:22 +01003091 ib_free_cq(dev->umrc.cq);
Eli Cohene126ba92013-07-07 17:25:49 +03003092 ib_dealloc_pd(dev->umrc.pd);
3093}
3094
3095enum {
3096 MAX_UMR_WR = 128,
3097};
3098
3099static int create_umr_res(struct mlx5_ib_dev *dev)
3100{
3101 struct ib_qp_init_attr *init_attr = NULL;
3102 struct ib_qp_attr *attr = NULL;
3103 struct ib_pd *pd;
3104 struct ib_cq *cq;
3105 struct ib_qp *qp;
Eli Cohene126ba92013-07-07 17:25:49 +03003106 int ret;
3107
3108 attr = kzalloc(sizeof(*attr), GFP_KERNEL);
3109 init_attr = kzalloc(sizeof(*init_attr), GFP_KERNEL);
3110 if (!attr || !init_attr) {
3111 ret = -ENOMEM;
3112 goto error_0;
3113 }
3114
Christoph Hellwiged082d32016-09-05 12:56:17 +02003115 pd = ib_alloc_pd(&dev->ib_dev, 0);
Eli Cohene126ba92013-07-07 17:25:49 +03003116 if (IS_ERR(pd)) {
3117 mlx5_ib_dbg(dev, "Couldn't create PD for sync UMR QP\n");
3118 ret = PTR_ERR(pd);
3119 goto error_0;
3120 }
3121
Christoph Hellwigadd08d72016-03-03 09:38:22 +01003122 cq = ib_alloc_cq(&dev->ib_dev, NULL, 128, 0, IB_POLL_SOFTIRQ);
Eli Cohene126ba92013-07-07 17:25:49 +03003123 if (IS_ERR(cq)) {
3124 mlx5_ib_dbg(dev, "Couldn't create CQ for sync UMR QP\n");
3125 ret = PTR_ERR(cq);
3126 goto error_2;
3127 }
Eli Cohene126ba92013-07-07 17:25:49 +03003128
3129 init_attr->send_cq = cq;
3130 init_attr->recv_cq = cq;
3131 init_attr->sq_sig_type = IB_SIGNAL_ALL_WR;
3132 init_attr->cap.max_send_wr = MAX_UMR_WR;
3133 init_attr->cap.max_send_sge = 1;
3134 init_attr->qp_type = MLX5_IB_QPT_REG_UMR;
3135 init_attr->port_num = 1;
3136 qp = mlx5_ib_create_qp(pd, init_attr, NULL);
3137 if (IS_ERR(qp)) {
3138 mlx5_ib_dbg(dev, "Couldn't create sync UMR QP\n");
3139 ret = PTR_ERR(qp);
3140 goto error_3;
3141 }
3142 qp->device = &dev->ib_dev;
3143 qp->real_qp = qp;
3144 qp->uobject = NULL;
3145 qp->qp_type = MLX5_IB_QPT_REG_UMR;
Majd Dibbiny31fde032017-10-30 14:23:13 +02003146 qp->send_cq = init_attr->send_cq;
3147 qp->recv_cq = init_attr->recv_cq;
Eli Cohene126ba92013-07-07 17:25:49 +03003148
3149 attr->qp_state = IB_QPS_INIT;
3150 attr->port_num = 1;
3151 ret = mlx5_ib_modify_qp(qp, attr, IB_QP_STATE | IB_QP_PKEY_INDEX |
3152 IB_QP_PORT, NULL);
3153 if (ret) {
3154 mlx5_ib_dbg(dev, "Couldn't modify UMR QP\n");
3155 goto error_4;
3156 }
3157
3158 memset(attr, 0, sizeof(*attr));
3159 attr->qp_state = IB_QPS_RTR;
3160 attr->path_mtu = IB_MTU_256;
3161
3162 ret = mlx5_ib_modify_qp(qp, attr, IB_QP_STATE, NULL);
3163 if (ret) {
3164 mlx5_ib_dbg(dev, "Couldn't modify umr QP to rtr\n");
3165 goto error_4;
3166 }
3167
3168 memset(attr, 0, sizeof(*attr));
3169 attr->qp_state = IB_QPS_RTS;
3170 ret = mlx5_ib_modify_qp(qp, attr, IB_QP_STATE, NULL);
3171 if (ret) {
3172 mlx5_ib_dbg(dev, "Couldn't modify umr QP to rts\n");
3173 goto error_4;
3174 }
3175
3176 dev->umrc.qp = qp;
3177 dev->umrc.cq = cq;
Eli Cohene126ba92013-07-07 17:25:49 +03003178 dev->umrc.pd = pd;
3179
3180 sema_init(&dev->umrc.sem, MAX_UMR_WR);
3181 ret = mlx5_mr_cache_init(dev);
3182 if (ret) {
3183 mlx5_ib_warn(dev, "mr cache init failed %d\n", ret);
3184 goto error_4;
3185 }
3186
3187 kfree(attr);
3188 kfree(init_attr);
3189
3190 return 0;
3191
3192error_4:
3193 mlx5_ib_destroy_qp(qp);
3194
3195error_3:
Christoph Hellwigadd08d72016-03-03 09:38:22 +01003196 ib_free_cq(cq);
Eli Cohene126ba92013-07-07 17:25:49 +03003197
3198error_2:
Eli Cohene126ba92013-07-07 17:25:49 +03003199 ib_dealloc_pd(pd);
3200
3201error_0:
3202 kfree(attr);
3203 kfree(init_attr);
3204 return ret;
3205}
3206
Max Gurtovoy6e8484c2017-05-28 10:53:11 +03003207static u8 mlx5_get_umr_fence(u8 umr_fence_cap)
3208{
3209 switch (umr_fence_cap) {
3210 case MLX5_CAP_UMR_FENCE_NONE:
3211 return MLX5_FENCE_MODE_NONE;
3212 case MLX5_CAP_UMR_FENCE_SMALL:
3213 return MLX5_FENCE_MODE_INITIATOR_SMALL;
3214 default:
3215 return MLX5_FENCE_MODE_STRONG_ORDERING;
3216 }
3217}
3218
Eli Cohene126ba92013-07-07 17:25:49 +03003219static int create_dev_resources(struct mlx5_ib_resources *devr)
3220{
3221 struct ib_srq_init_attr attr;
3222 struct mlx5_ib_dev *dev;
Matan Barakbcf4c1e2015-06-11 16:35:20 +03003223 struct ib_cq_init_attr cq_attr = {.cqe = 1};
Haggai Eran7722f472016-02-29 15:45:07 +02003224 int port;
Eli Cohene126ba92013-07-07 17:25:49 +03003225 int ret = 0;
3226
3227 dev = container_of(devr, struct mlx5_ib_dev, devr);
3228
Haggai Erand16e91d2016-02-29 15:45:05 +02003229 mutex_init(&devr->mutex);
3230
Eli Cohene126ba92013-07-07 17:25:49 +03003231 devr->p0 = mlx5_ib_alloc_pd(&dev->ib_dev, NULL, NULL);
3232 if (IS_ERR(devr->p0)) {
3233 ret = PTR_ERR(devr->p0);
3234 goto error0;
3235 }
3236 devr->p0->device = &dev->ib_dev;
3237 devr->p0->uobject = NULL;
3238 atomic_set(&devr->p0->usecnt, 0);
3239
Matan Barakbcf4c1e2015-06-11 16:35:20 +03003240 devr->c0 = mlx5_ib_create_cq(&dev->ib_dev, &cq_attr, NULL, NULL);
Eli Cohene126ba92013-07-07 17:25:49 +03003241 if (IS_ERR(devr->c0)) {
3242 ret = PTR_ERR(devr->c0);
3243 goto error1;
3244 }
3245 devr->c0->device = &dev->ib_dev;
3246 devr->c0->uobject = NULL;
3247 devr->c0->comp_handler = NULL;
3248 devr->c0->event_handler = NULL;
3249 devr->c0->cq_context = NULL;
3250 atomic_set(&devr->c0->usecnt, 0);
3251
3252 devr->x0 = mlx5_ib_alloc_xrcd(&dev->ib_dev, NULL, NULL);
3253 if (IS_ERR(devr->x0)) {
3254 ret = PTR_ERR(devr->x0);
3255 goto error2;
3256 }
3257 devr->x0->device = &dev->ib_dev;
3258 devr->x0->inode = NULL;
3259 atomic_set(&devr->x0->usecnt, 0);
3260 mutex_init(&devr->x0->tgt_qp_mutex);
3261 INIT_LIST_HEAD(&devr->x0->tgt_qp_list);
3262
3263 devr->x1 = mlx5_ib_alloc_xrcd(&dev->ib_dev, NULL, NULL);
3264 if (IS_ERR(devr->x1)) {
3265 ret = PTR_ERR(devr->x1);
3266 goto error3;
3267 }
3268 devr->x1->device = &dev->ib_dev;
3269 devr->x1->inode = NULL;
3270 atomic_set(&devr->x1->usecnt, 0);
3271 mutex_init(&devr->x1->tgt_qp_mutex);
3272 INIT_LIST_HEAD(&devr->x1->tgt_qp_list);
3273
3274 memset(&attr, 0, sizeof(attr));
3275 attr.attr.max_sge = 1;
3276 attr.attr.max_wr = 1;
3277 attr.srq_type = IB_SRQT_XRC;
Artemy Kovalyov1a56ff62017-08-17 15:52:04 +03003278 attr.ext.cq = devr->c0;
Eli Cohene126ba92013-07-07 17:25:49 +03003279 attr.ext.xrc.xrcd = devr->x0;
3280
3281 devr->s0 = mlx5_ib_create_srq(devr->p0, &attr, NULL);
3282 if (IS_ERR(devr->s0)) {
3283 ret = PTR_ERR(devr->s0);
3284 goto error4;
3285 }
3286 devr->s0->device = &dev->ib_dev;
3287 devr->s0->pd = devr->p0;
3288 devr->s0->uobject = NULL;
3289 devr->s0->event_handler = NULL;
3290 devr->s0->srq_context = NULL;
3291 devr->s0->srq_type = IB_SRQT_XRC;
3292 devr->s0->ext.xrc.xrcd = devr->x0;
Artemy Kovalyov1a56ff62017-08-17 15:52:04 +03003293 devr->s0->ext.cq = devr->c0;
Eli Cohene126ba92013-07-07 17:25:49 +03003294 atomic_inc(&devr->s0->ext.xrc.xrcd->usecnt);
Artemy Kovalyov1a56ff62017-08-17 15:52:04 +03003295 atomic_inc(&devr->s0->ext.cq->usecnt);
Eli Cohene126ba92013-07-07 17:25:49 +03003296 atomic_inc(&devr->p0->usecnt);
3297 atomic_set(&devr->s0->usecnt, 0);
3298
Haggai Abramonvsky4aa17b22015-06-04 19:30:48 +03003299 memset(&attr, 0, sizeof(attr));
3300 attr.attr.max_sge = 1;
3301 attr.attr.max_wr = 1;
3302 attr.srq_type = IB_SRQT_BASIC;
3303 devr->s1 = mlx5_ib_create_srq(devr->p0, &attr, NULL);
3304 if (IS_ERR(devr->s1)) {
3305 ret = PTR_ERR(devr->s1);
3306 goto error5;
3307 }
3308 devr->s1->device = &dev->ib_dev;
3309 devr->s1->pd = devr->p0;
3310 devr->s1->uobject = NULL;
3311 devr->s1->event_handler = NULL;
3312 devr->s1->srq_context = NULL;
3313 devr->s1->srq_type = IB_SRQT_BASIC;
Artemy Kovalyov1a56ff62017-08-17 15:52:04 +03003314 devr->s1->ext.cq = devr->c0;
Haggai Abramonvsky4aa17b22015-06-04 19:30:48 +03003315 atomic_inc(&devr->p0->usecnt);
Artemy Kovalyov1a56ff62017-08-17 15:52:04 +03003316 atomic_set(&devr->s1->usecnt, 0);
Haggai Abramonvsky4aa17b22015-06-04 19:30:48 +03003317
Haggai Eran7722f472016-02-29 15:45:07 +02003318 for (port = 0; port < ARRAY_SIZE(devr->ports); ++port) {
3319 INIT_WORK(&devr->ports[port].pkey_change_work,
3320 pkey_change_handler);
3321 devr->ports[port].devr = devr;
3322 }
3323
Eli Cohene126ba92013-07-07 17:25:49 +03003324 return 0;
3325
Haggai Abramonvsky4aa17b22015-06-04 19:30:48 +03003326error5:
3327 mlx5_ib_destroy_srq(devr->s0);
Eli Cohene126ba92013-07-07 17:25:49 +03003328error4:
3329 mlx5_ib_dealloc_xrcd(devr->x1);
3330error3:
3331 mlx5_ib_dealloc_xrcd(devr->x0);
3332error2:
3333 mlx5_ib_destroy_cq(devr->c0);
3334error1:
3335 mlx5_ib_dealloc_pd(devr->p0);
3336error0:
3337 return ret;
3338}
3339
3340static void destroy_dev_resources(struct mlx5_ib_resources *devr)
3341{
Haggai Eran7722f472016-02-29 15:45:07 +02003342 struct mlx5_ib_dev *dev =
3343 container_of(devr, struct mlx5_ib_dev, devr);
3344 int port;
3345
Haggai Abramonvsky4aa17b22015-06-04 19:30:48 +03003346 mlx5_ib_destroy_srq(devr->s1);
Eli Cohene126ba92013-07-07 17:25:49 +03003347 mlx5_ib_destroy_srq(devr->s0);
3348 mlx5_ib_dealloc_xrcd(devr->x0);
3349 mlx5_ib_dealloc_xrcd(devr->x1);
3350 mlx5_ib_destroy_cq(devr->c0);
3351 mlx5_ib_dealloc_pd(devr->p0);
Haggai Eran7722f472016-02-29 15:45:07 +02003352
3353 /* Make sure no change P_Key work items are still executing */
3354 for (port = 0; port < dev->num_ports; ++port)
3355 cancel_work_sync(&devr->ports[port].pkey_change_work);
Eli Cohene126ba92013-07-07 17:25:49 +03003356}
3357
Achiad Shochate53505a2015-12-23 18:47:25 +02003358static u32 get_core_cap_flags(struct ib_device *ibdev)
3359{
3360 struct mlx5_ib_dev *dev = to_mdev(ibdev);
3361 enum rdma_link_layer ll = mlx5_ib_port_link_layer(ibdev, 1);
3362 u8 l3_type_cap = MLX5_CAP_ROCE(dev->mdev, l3_type);
3363 u8 roce_version_cap = MLX5_CAP_ROCE(dev->mdev, roce_version);
3364 u32 ret = 0;
3365
3366 if (ll == IB_LINK_LAYER_INFINIBAND)
3367 return RDMA_CORE_PORT_IBA_IB;
3368
Or Gerlitz72cd5712017-01-24 13:02:36 +02003369 ret = RDMA_CORE_PORT_RAW_PACKET;
3370
Achiad Shochate53505a2015-12-23 18:47:25 +02003371 if (!(l3_type_cap & MLX5_ROCE_L3_TYPE_IPV4_CAP))
Or Gerlitz72cd5712017-01-24 13:02:36 +02003372 return ret;
Achiad Shochate53505a2015-12-23 18:47:25 +02003373
3374 if (!(l3_type_cap & MLX5_ROCE_L3_TYPE_IPV6_CAP))
Or Gerlitz72cd5712017-01-24 13:02:36 +02003375 return ret;
Achiad Shochate53505a2015-12-23 18:47:25 +02003376
3377 if (roce_version_cap & MLX5_ROCE_VERSION_1_CAP)
3378 ret |= RDMA_CORE_PORT_IBA_ROCE;
3379
3380 if (roce_version_cap & MLX5_ROCE_VERSION_2_CAP)
3381 ret |= RDMA_CORE_PORT_IBA_ROCE_UDP_ENCAP;
3382
3383 return ret;
3384}
3385
Ira Weiny77386132015-05-13 20:02:58 -04003386static int mlx5_port_immutable(struct ib_device *ibdev, u8 port_num,
3387 struct ib_port_immutable *immutable)
3388{
3389 struct ib_port_attr attr;
Or Gerlitzca5b91d2016-11-27 16:51:36 +02003390 struct mlx5_ib_dev *dev = to_mdev(ibdev);
3391 enum rdma_link_layer ll = mlx5_ib_port_link_layer(ibdev, port_num);
Ira Weiny77386132015-05-13 20:02:58 -04003392 int err;
3393
Or Gerlitzc4550c62017-01-24 13:02:39 +02003394 immutable->core_cap_flags = get_core_cap_flags(ibdev);
3395
3396 err = ib_query_port(ibdev, port_num, &attr);
Ira Weiny77386132015-05-13 20:02:58 -04003397 if (err)
3398 return err;
3399
3400 immutable->pkey_tbl_len = attr.pkey_tbl_len;
3401 immutable->gid_tbl_len = attr.gid_tbl_len;
Achiad Shochate53505a2015-12-23 18:47:25 +02003402 immutable->core_cap_flags = get_core_cap_flags(ibdev);
Or Gerlitzca5b91d2016-11-27 16:51:36 +02003403 if ((ll == IB_LINK_LAYER_INFINIBAND) || MLX5_CAP_GEN(dev->mdev, roce))
3404 immutable->max_mad_size = IB_MGMT_MAD_SIZE;
Ira Weiny77386132015-05-13 20:02:58 -04003405
3406 return 0;
3407}
3408
Leon Romanovsky9abb0d12017-06-27 16:49:53 +03003409static void get_dev_fw_str(struct ib_device *ibdev, char *str)
Ira Weinyc7342822016-06-15 02:22:01 -04003410{
3411 struct mlx5_ib_dev *dev =
3412 container_of(ibdev, struct mlx5_ib_dev, ib_dev);
Leon Romanovsky9abb0d12017-06-27 16:49:53 +03003413 snprintf(str, IB_FW_VERSION_NAME_MAX, "%d.%d.%04d",
3414 fw_rev_maj(dev->mdev), fw_rev_min(dev->mdev),
3415 fw_rev_sub(dev->mdev));
Ira Weinyc7342822016-06-15 02:22:01 -04003416}
3417
Or Gerlitz45f95ac2016-11-27 16:51:35 +02003418static int mlx5_eth_lag_init(struct mlx5_ib_dev *dev)
Aviv Heller9ef9c642016-09-18 20:48:01 +03003419{
3420 struct mlx5_core_dev *mdev = dev->mdev;
3421 struct mlx5_flow_namespace *ns = mlx5_get_flow_namespace(mdev,
3422 MLX5_FLOW_NAMESPACE_LAG);
3423 struct mlx5_flow_table *ft;
3424 int err;
3425
3426 if (!ns || !mlx5_lag_is_active(mdev))
3427 return 0;
3428
3429 err = mlx5_cmd_create_vport_lag(mdev);
3430 if (err)
3431 return err;
3432
3433 ft = mlx5_create_lag_demux_flow_table(ns, 0, 0);
3434 if (IS_ERR(ft)) {
3435 err = PTR_ERR(ft);
3436 goto err_destroy_vport_lag;
3437 }
3438
3439 dev->flow_db.lag_demux_ft = ft;
3440 return 0;
3441
3442err_destroy_vport_lag:
3443 mlx5_cmd_destroy_vport_lag(mdev);
3444 return err;
3445}
3446
Or Gerlitz45f95ac2016-11-27 16:51:35 +02003447static void mlx5_eth_lag_cleanup(struct mlx5_ib_dev *dev)
Aviv Heller9ef9c642016-09-18 20:48:01 +03003448{
3449 struct mlx5_core_dev *mdev = dev->mdev;
3450
3451 if (dev->flow_db.lag_demux_ft) {
3452 mlx5_destroy_flow_table(dev->flow_db.lag_demux_ft);
3453 dev->flow_db.lag_demux_ft = NULL;
3454
3455 mlx5_cmd_destroy_vport_lag(mdev);
3456 }
3457}
3458
Or Gerlitzd012f5d2016-11-27 16:51:34 +02003459static int mlx5_add_netdev_notifier(struct mlx5_ib_dev *dev)
Achiad Shochatfc24fc52015-12-23 18:47:17 +02003460{
Achiad Shochate53505a2015-12-23 18:47:25 +02003461 int err;
3462
Achiad Shochatfc24fc52015-12-23 18:47:17 +02003463 dev->roce.nb.notifier_call = mlx5_netdev_event;
Achiad Shochate53505a2015-12-23 18:47:25 +02003464 err = register_netdevice_notifier(&dev->roce.nb);
Aviv Heller5ec8c832016-09-18 20:48:00 +03003465 if (err) {
3466 dev->roce.nb.notifier_call = NULL;
Achiad Shochate53505a2015-12-23 18:47:25 +02003467 return err;
Aviv Heller5ec8c832016-09-18 20:48:00 +03003468 }
Achiad Shochate53505a2015-12-23 18:47:25 +02003469
Or Gerlitzd012f5d2016-11-27 16:51:34 +02003470 return 0;
3471}
Achiad Shochate53505a2015-12-23 18:47:25 +02003472
Or Gerlitzd012f5d2016-11-27 16:51:34 +02003473static void mlx5_remove_netdev_notifier(struct mlx5_ib_dev *dev)
Eli Cohene126ba92013-07-07 17:25:49 +03003474{
3475 if (dev->roce.nb.notifier_call) {
3476 unregister_netdevice_notifier(&dev->roce.nb);
3477 dev->roce.nb.notifier_call = NULL;
3478 }
3479}
3480
Or Gerlitz45f95ac2016-11-27 16:51:35 +02003481static int mlx5_enable_eth(struct mlx5_ib_dev *dev)
Eli Cohene126ba92013-07-07 17:25:49 +03003482{
Eli Cohene126ba92013-07-07 17:25:49 +03003483 int err;
3484
Or Gerlitzd012f5d2016-11-27 16:51:34 +02003485 err = mlx5_add_netdev_notifier(dev);
3486 if (err)
Achiad Shochate53505a2015-12-23 18:47:25 +02003487 return err;
Achiad Shochate53505a2015-12-23 18:47:25 +02003488
Or Gerlitzca5b91d2016-11-27 16:51:36 +02003489 if (MLX5_CAP_GEN(dev->mdev, roce)) {
3490 err = mlx5_nic_vport_enable_roce(dev->mdev);
3491 if (err)
3492 goto err_unregister_netdevice_notifier;
3493 }
Achiad Shochate53505a2015-12-23 18:47:25 +02003494
Or Gerlitz45f95ac2016-11-27 16:51:35 +02003495 err = mlx5_eth_lag_init(dev);
Aviv Heller9ef9c642016-09-18 20:48:01 +03003496 if (err)
3497 goto err_disable_roce;
3498
Achiad Shochate53505a2015-12-23 18:47:25 +02003499 return 0;
3500
Aviv Heller9ef9c642016-09-18 20:48:01 +03003501err_disable_roce:
Or Gerlitzca5b91d2016-11-27 16:51:36 +02003502 if (MLX5_CAP_GEN(dev->mdev, roce))
3503 mlx5_nic_vport_disable_roce(dev->mdev);
Aviv Heller9ef9c642016-09-18 20:48:01 +03003504
Achiad Shochate53505a2015-12-23 18:47:25 +02003505err_unregister_netdevice_notifier:
Or Gerlitzd012f5d2016-11-27 16:51:34 +02003506 mlx5_remove_netdev_notifier(dev);
Achiad Shochate53505a2015-12-23 18:47:25 +02003507 return err;
Achiad Shochatfc24fc52015-12-23 18:47:17 +02003508}
3509
Or Gerlitz45f95ac2016-11-27 16:51:35 +02003510static void mlx5_disable_eth(struct mlx5_ib_dev *dev)
Achiad Shochatfc24fc52015-12-23 18:47:17 +02003511{
Or Gerlitz45f95ac2016-11-27 16:51:35 +02003512 mlx5_eth_lag_cleanup(dev);
Or Gerlitzca5b91d2016-11-27 16:51:36 +02003513 if (MLX5_CAP_GEN(dev->mdev, roce))
3514 mlx5_nic_vport_disable_roce(dev->mdev);
Achiad Shochatfc24fc52015-12-23 18:47:17 +02003515}
3516
Parav Pandite1f24a72017-04-16 07:29:29 +03003517struct mlx5_ib_counter {
Kamal Heib7c16f472017-01-18 15:25:09 +02003518 const char *name;
3519 size_t offset;
3520};
3521
3522#define INIT_Q_COUNTER(_name) \
3523 { .name = #_name, .offset = MLX5_BYTE_OFF(query_q_counter_out, _name)}
3524
Parav Pandite1f24a72017-04-16 07:29:29 +03003525static const struct mlx5_ib_counter basic_q_cnts[] = {
Kamal Heib7c16f472017-01-18 15:25:09 +02003526 INIT_Q_COUNTER(rx_write_requests),
3527 INIT_Q_COUNTER(rx_read_requests),
3528 INIT_Q_COUNTER(rx_atomic_requests),
3529 INIT_Q_COUNTER(out_of_buffer),
3530};
3531
Parav Pandite1f24a72017-04-16 07:29:29 +03003532static const struct mlx5_ib_counter out_of_seq_q_cnts[] = {
Kamal Heib7c16f472017-01-18 15:25:09 +02003533 INIT_Q_COUNTER(out_of_sequence),
3534};
3535
Parav Pandite1f24a72017-04-16 07:29:29 +03003536static const struct mlx5_ib_counter retrans_q_cnts[] = {
Kamal Heib7c16f472017-01-18 15:25:09 +02003537 INIT_Q_COUNTER(duplicate_request),
3538 INIT_Q_COUNTER(rnr_nak_retry_err),
3539 INIT_Q_COUNTER(packet_seq_err),
3540 INIT_Q_COUNTER(implied_nak_seq_err),
3541 INIT_Q_COUNTER(local_ack_timeout_err),
3542};
3543
Parav Pandite1f24a72017-04-16 07:29:29 +03003544#define INIT_CONG_COUNTER(_name) \
3545 { .name = #_name, .offset = \
3546 MLX5_BYTE_OFF(query_cong_statistics_out, _name ## _high)}
3547
3548static const struct mlx5_ib_counter cong_cnts[] = {
3549 INIT_CONG_COUNTER(rp_cnp_ignored),
3550 INIT_CONG_COUNTER(rp_cnp_handled),
3551 INIT_CONG_COUNTER(np_ecn_marked_roce_packets),
3552 INIT_CONG_COUNTER(np_cnp_sent),
3553};
3554
Parav Pandit58dcb602017-06-19 07:19:37 +03003555static const struct mlx5_ib_counter extended_err_cnts[] = {
3556 INIT_Q_COUNTER(resp_local_length_error),
3557 INIT_Q_COUNTER(resp_cqe_error),
3558 INIT_Q_COUNTER(req_cqe_error),
3559 INIT_Q_COUNTER(req_remote_invalid_request),
3560 INIT_Q_COUNTER(req_remote_access_errors),
3561 INIT_Q_COUNTER(resp_remote_access_errors),
3562 INIT_Q_COUNTER(resp_cqe_flush_error),
3563 INIT_Q_COUNTER(req_cqe_flush_error),
3564};
3565
Parav Pandite1f24a72017-04-16 07:29:29 +03003566static void mlx5_ib_dealloc_counters(struct mlx5_ib_dev *dev)
Mark Bloch0837e862016-06-17 15:10:55 +03003567{
3568 unsigned int i;
3569
Kamal Heib7c16f472017-01-18 15:25:09 +02003570 for (i = 0; i < dev->num_ports; i++) {
Mark Bloch0837e862016-06-17 15:10:55 +03003571 mlx5_core_dealloc_q_counter(dev->mdev,
Parav Pandite1f24a72017-04-16 07:29:29 +03003572 dev->port[i].cnts.set_id);
3573 kfree(dev->port[i].cnts.names);
3574 kfree(dev->port[i].cnts.offsets);
Kamal Heib7c16f472017-01-18 15:25:09 +02003575 }
3576}
3577
Parav Pandite1f24a72017-04-16 07:29:29 +03003578static int __mlx5_ib_alloc_counters(struct mlx5_ib_dev *dev,
3579 struct mlx5_ib_counters *cnts)
Kamal Heib7c16f472017-01-18 15:25:09 +02003580{
3581 u32 num_counters;
3582
3583 num_counters = ARRAY_SIZE(basic_q_cnts);
3584
3585 if (MLX5_CAP_GEN(dev->mdev, out_of_seq_cnt))
3586 num_counters += ARRAY_SIZE(out_of_seq_q_cnts);
3587
3588 if (MLX5_CAP_GEN(dev->mdev, retransmission_q_counters))
3589 num_counters += ARRAY_SIZE(retrans_q_cnts);
Parav Pandit58dcb602017-06-19 07:19:37 +03003590
3591 if (MLX5_CAP_GEN(dev->mdev, enhanced_error_q_counters))
3592 num_counters += ARRAY_SIZE(extended_err_cnts);
3593
Parav Pandite1f24a72017-04-16 07:29:29 +03003594 cnts->num_q_counters = num_counters;
Kamal Heib7c16f472017-01-18 15:25:09 +02003595
Parav Pandite1f24a72017-04-16 07:29:29 +03003596 if (MLX5_CAP_GEN(dev->mdev, cc_query_allowed)) {
3597 cnts->num_cong_counters = ARRAY_SIZE(cong_cnts);
3598 num_counters += ARRAY_SIZE(cong_cnts);
3599 }
3600
3601 cnts->names = kcalloc(num_counters, sizeof(cnts->names), GFP_KERNEL);
3602 if (!cnts->names)
Kamal Heib7c16f472017-01-18 15:25:09 +02003603 return -ENOMEM;
3604
Parav Pandite1f24a72017-04-16 07:29:29 +03003605 cnts->offsets = kcalloc(num_counters,
3606 sizeof(cnts->offsets), GFP_KERNEL);
3607 if (!cnts->offsets)
Kamal Heib7c16f472017-01-18 15:25:09 +02003608 goto err_names;
3609
Kamal Heib7c16f472017-01-18 15:25:09 +02003610 return 0;
3611
3612err_names:
Parav Pandite1f24a72017-04-16 07:29:29 +03003613 kfree(cnts->names);
Kamal Heib7c16f472017-01-18 15:25:09 +02003614 return -ENOMEM;
3615}
3616
Parav Pandite1f24a72017-04-16 07:29:29 +03003617static void mlx5_ib_fill_counters(struct mlx5_ib_dev *dev,
3618 const char **names,
3619 size_t *offsets)
Kamal Heib7c16f472017-01-18 15:25:09 +02003620{
3621 int i;
3622 int j = 0;
3623
3624 for (i = 0; i < ARRAY_SIZE(basic_q_cnts); i++, j++) {
3625 names[j] = basic_q_cnts[i].name;
3626 offsets[j] = basic_q_cnts[i].offset;
3627 }
3628
3629 if (MLX5_CAP_GEN(dev->mdev, out_of_seq_cnt)) {
3630 for (i = 0; i < ARRAY_SIZE(out_of_seq_q_cnts); i++, j++) {
3631 names[j] = out_of_seq_q_cnts[i].name;
3632 offsets[j] = out_of_seq_q_cnts[i].offset;
3633 }
3634 }
3635
3636 if (MLX5_CAP_GEN(dev->mdev, retransmission_q_counters)) {
3637 for (i = 0; i < ARRAY_SIZE(retrans_q_cnts); i++, j++) {
3638 names[j] = retrans_q_cnts[i].name;
3639 offsets[j] = retrans_q_cnts[i].offset;
3640 }
3641 }
Parav Pandite1f24a72017-04-16 07:29:29 +03003642
Parav Pandit58dcb602017-06-19 07:19:37 +03003643 if (MLX5_CAP_GEN(dev->mdev, enhanced_error_q_counters)) {
3644 for (i = 0; i < ARRAY_SIZE(extended_err_cnts); i++, j++) {
3645 names[j] = extended_err_cnts[i].name;
3646 offsets[j] = extended_err_cnts[i].offset;
3647 }
3648 }
3649
Parav Pandite1f24a72017-04-16 07:29:29 +03003650 if (MLX5_CAP_GEN(dev->mdev, cc_query_allowed)) {
3651 for (i = 0; i < ARRAY_SIZE(cong_cnts); i++, j++) {
3652 names[j] = cong_cnts[i].name;
3653 offsets[j] = cong_cnts[i].offset;
3654 }
3655 }
Mark Bloch0837e862016-06-17 15:10:55 +03003656}
3657
Parav Pandite1f24a72017-04-16 07:29:29 +03003658static int mlx5_ib_alloc_counters(struct mlx5_ib_dev *dev)
Mark Bloch0837e862016-06-17 15:10:55 +03003659{
3660 int i;
3661 int ret;
3662
3663 for (i = 0; i < dev->num_ports; i++) {
Kamal Heib7c16f472017-01-18 15:25:09 +02003664 struct mlx5_ib_port *port = &dev->port[i];
3665
Mark Bloch0837e862016-06-17 15:10:55 +03003666 ret = mlx5_core_alloc_q_counter(dev->mdev,
Parav Pandite1f24a72017-04-16 07:29:29 +03003667 &port->cnts.set_id);
Mark Bloch0837e862016-06-17 15:10:55 +03003668 if (ret) {
3669 mlx5_ib_warn(dev,
3670 "couldn't allocate queue counter for port %d, err %d\n",
3671 i + 1, ret);
3672 goto dealloc_counters;
3673 }
Kamal Heib7c16f472017-01-18 15:25:09 +02003674
Parav Pandite1f24a72017-04-16 07:29:29 +03003675 ret = __mlx5_ib_alloc_counters(dev, &port->cnts);
Kamal Heib7c16f472017-01-18 15:25:09 +02003676 if (ret)
3677 goto dealloc_counters;
3678
Parav Pandite1f24a72017-04-16 07:29:29 +03003679 mlx5_ib_fill_counters(dev, port->cnts.names,
3680 port->cnts.offsets);
Mark Bloch0837e862016-06-17 15:10:55 +03003681 }
3682
3683 return 0;
3684
3685dealloc_counters:
3686 while (--i >= 0)
3687 mlx5_core_dealloc_q_counter(dev->mdev,
Parav Pandite1f24a72017-04-16 07:29:29 +03003688 dev->port[i].cnts.set_id);
Mark Bloch0837e862016-06-17 15:10:55 +03003689
3690 return ret;
3691}
3692
Mark Bloch0ad17a82016-06-17 15:10:56 +03003693static struct rdma_hw_stats *mlx5_ib_alloc_hw_stats(struct ib_device *ibdev,
3694 u8 port_num)
3695{
Kamal Heib7c16f472017-01-18 15:25:09 +02003696 struct mlx5_ib_dev *dev = to_mdev(ibdev);
3697 struct mlx5_ib_port *port = &dev->port[port_num - 1];
Mark Bloch0ad17a82016-06-17 15:10:56 +03003698
3699 /* We support only per port stats */
3700 if (port_num == 0)
3701 return NULL;
3702
Parav Pandite1f24a72017-04-16 07:29:29 +03003703 return rdma_alloc_hw_stats_struct(port->cnts.names,
3704 port->cnts.num_q_counters +
3705 port->cnts.num_cong_counters,
Mark Bloch0ad17a82016-06-17 15:10:56 +03003706 RDMA_HW_STATS_DEFAULT_LIFESPAN);
3707}
3708
Parav Pandite1f24a72017-04-16 07:29:29 +03003709static int mlx5_ib_query_q_counters(struct mlx5_ib_dev *dev,
3710 struct mlx5_ib_port *port,
3711 struct rdma_hw_stats *stats)
3712{
3713 int outlen = MLX5_ST_SZ_BYTES(query_q_counter_out);
3714 void *out;
3715 __be32 val;
3716 int ret, i;
3717
Leon Romanovsky1b9a07e2017-05-10 21:32:18 +03003718 out = kvzalloc(outlen, GFP_KERNEL);
Parav Pandite1f24a72017-04-16 07:29:29 +03003719 if (!out)
3720 return -ENOMEM;
3721
3722 ret = mlx5_core_query_q_counter(dev->mdev,
3723 port->cnts.set_id, 0,
3724 out, outlen);
3725 if (ret)
3726 goto free;
3727
3728 for (i = 0; i < port->cnts.num_q_counters; i++) {
3729 val = *(__be32 *)(out + port->cnts.offsets[i]);
3730 stats->value[i] = (u64)be32_to_cpu(val);
3731 }
3732
3733free:
3734 kvfree(out);
3735 return ret;
3736}
3737
3738static int mlx5_ib_query_cong_counters(struct mlx5_ib_dev *dev,
3739 struct mlx5_ib_port *port,
3740 struct rdma_hw_stats *stats)
3741{
3742 int outlen = MLX5_ST_SZ_BYTES(query_cong_statistics_out);
3743 void *out;
3744 int ret, i;
3745 int offset = port->cnts.num_q_counters;
3746
Leon Romanovsky1b9a07e2017-05-10 21:32:18 +03003747 out = kvzalloc(outlen, GFP_KERNEL);
Parav Pandite1f24a72017-04-16 07:29:29 +03003748 if (!out)
3749 return -ENOMEM;
3750
3751 ret = mlx5_cmd_query_cong_counter(dev->mdev, false, out, outlen);
3752 if (ret)
3753 goto free;
3754
3755 for (i = 0; i < port->cnts.num_cong_counters; i++) {
3756 stats->value[i + offset] =
3757 be64_to_cpup((__be64 *)(out +
3758 port->cnts.offsets[i + offset]));
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)) {
3783 ret = mlx5_ib_query_cong_counters(dev, port, stats);
3784 if (ret)
3785 return ret;
3786 num_counters += port->cnts.num_cong_counters;
Mark Bloch0ad17a82016-06-17 15:10:56 +03003787 }
Kamal Heib7c16f472017-01-18 15:25:09 +02003788
Parav Pandite1f24a72017-04-16 07:29:29 +03003789 return num_counters;
Mark Bloch0ad17a82016-06-17 15:10:56 +03003790}
3791
Niranjana Vishwanathapura8e959602017-06-30 13:14:46 -07003792static void mlx5_ib_free_rdma_netdev(struct net_device *netdev)
3793{
3794 return mlx5_rdma_netdev_free(netdev);
3795}
3796
Erez Shitrit693dfd52017-04-27 17:01:34 +03003797static struct net_device*
3798mlx5_ib_alloc_rdma_netdev(struct ib_device *hca,
3799 u8 port_num,
3800 enum rdma_netdev_t type,
3801 const char *name,
3802 unsigned char name_assign_type,
3803 void (*setup)(struct net_device *))
3804{
Niranjana Vishwanathapura8e959602017-06-30 13:14:46 -07003805 struct net_device *netdev;
3806 struct rdma_netdev *rn;
3807
Erez Shitrit693dfd52017-04-27 17:01:34 +03003808 if (type != RDMA_NETDEV_IPOIB)
3809 return ERR_PTR(-EOPNOTSUPP);
3810
Niranjana Vishwanathapura8e959602017-06-30 13:14:46 -07003811 netdev = mlx5_rdma_netdev_alloc(to_mdev(hca)->mdev, hca,
3812 name, setup);
3813 if (likely(!IS_ERR_OR_NULL(netdev))) {
3814 rn = netdev_priv(netdev);
3815 rn->free_rdma_netdev = mlx5_ib_free_rdma_netdev;
3816 }
3817 return netdev;
Erez Shitrit693dfd52017-04-27 17:01:34 +03003818}
3819
Maor Gottliebfe248c32017-05-30 10:29:14 +03003820static void delay_drop_debugfs_cleanup(struct mlx5_ib_dev *dev)
3821{
3822 if (!dev->delay_drop.dbg)
3823 return;
3824 debugfs_remove_recursive(dev->delay_drop.dbg->dir_debugfs);
3825 kfree(dev->delay_drop.dbg);
3826 dev->delay_drop.dbg = NULL;
3827}
3828
Maor Gottlieb03404e82017-05-30 10:29:13 +03003829static void cancel_delay_drop(struct mlx5_ib_dev *dev)
3830{
3831 if (!(dev->ib_dev.attrs.raw_packet_caps & IB_RAW_PACKET_CAP_DELAY_DROP))
3832 return;
3833
3834 cancel_work_sync(&dev->delay_drop.delay_drop_work);
Maor Gottliebfe248c32017-05-30 10:29:14 +03003835 delay_drop_debugfs_cleanup(dev);
3836}
3837
3838static ssize_t delay_drop_timeout_read(struct file *filp, char __user *buf,
3839 size_t count, loff_t *pos)
3840{
3841 struct mlx5_ib_delay_drop *delay_drop = filp->private_data;
3842 char lbuf[20];
3843 int len;
3844
3845 len = snprintf(lbuf, sizeof(lbuf), "%u\n", delay_drop->timeout);
3846 return simple_read_from_buffer(buf, count, pos, lbuf, len);
3847}
3848
3849static ssize_t delay_drop_timeout_write(struct file *filp, const char __user *buf,
3850 size_t count, loff_t *pos)
3851{
3852 struct mlx5_ib_delay_drop *delay_drop = filp->private_data;
3853 u32 timeout;
3854 u32 var;
3855
3856 if (kstrtouint_from_user(buf, count, 0, &var))
3857 return -EFAULT;
3858
3859 timeout = min_t(u32, roundup(var, 100), MLX5_MAX_DELAY_DROP_TIMEOUT_MS *
3860 1000);
3861 if (timeout != var)
3862 mlx5_ib_dbg(delay_drop->dev, "Round delay drop timeout to %u usec\n",
3863 timeout);
3864
3865 delay_drop->timeout = timeout;
3866
3867 return count;
3868}
3869
3870static const struct file_operations fops_delay_drop_timeout = {
3871 .owner = THIS_MODULE,
3872 .open = simple_open,
3873 .write = delay_drop_timeout_write,
3874 .read = delay_drop_timeout_read,
3875};
3876
3877static int delay_drop_debugfs_init(struct mlx5_ib_dev *dev)
3878{
3879 struct mlx5_ib_dbg_delay_drop *dbg;
3880
3881 if (!mlx5_debugfs_root)
3882 return 0;
3883
3884 dbg = kzalloc(sizeof(*dbg), GFP_KERNEL);
3885 if (!dbg)
3886 return -ENOMEM;
3887
Sudip Mukherjeecbafad82017-09-18 12:28:48 +01003888 dev->delay_drop.dbg = dbg;
3889
Maor Gottliebfe248c32017-05-30 10:29:14 +03003890 dbg->dir_debugfs =
3891 debugfs_create_dir("delay_drop",
3892 dev->mdev->priv.dbg_root);
3893 if (!dbg->dir_debugfs)
Sudip Mukherjeecbafad82017-09-18 12:28:48 +01003894 goto out_debugfs;
Maor Gottliebfe248c32017-05-30 10:29:14 +03003895
3896 dbg->events_cnt_debugfs =
3897 debugfs_create_atomic_t("num_timeout_events", 0400,
3898 dbg->dir_debugfs,
3899 &dev->delay_drop.events_cnt);
3900 if (!dbg->events_cnt_debugfs)
3901 goto out_debugfs;
3902
3903 dbg->rqs_cnt_debugfs =
3904 debugfs_create_atomic_t("num_rqs", 0400,
3905 dbg->dir_debugfs,
3906 &dev->delay_drop.rqs_cnt);
3907 if (!dbg->rqs_cnt_debugfs)
3908 goto out_debugfs;
3909
3910 dbg->timeout_debugfs =
3911 debugfs_create_file("timeout", 0600,
3912 dbg->dir_debugfs,
3913 &dev->delay_drop,
3914 &fops_delay_drop_timeout);
3915 if (!dbg->timeout_debugfs)
3916 goto out_debugfs;
3917
3918 return 0;
3919
3920out_debugfs:
3921 delay_drop_debugfs_cleanup(dev);
3922 return -ENOMEM;
Maor Gottlieb03404e82017-05-30 10:29:13 +03003923}
3924
3925static void init_delay_drop(struct mlx5_ib_dev *dev)
3926{
3927 if (!(dev->ib_dev.attrs.raw_packet_caps & IB_RAW_PACKET_CAP_DELAY_DROP))
3928 return;
3929
3930 mutex_init(&dev->delay_drop.lock);
3931 dev->delay_drop.dev = dev;
3932 dev->delay_drop.activate = false;
3933 dev->delay_drop.timeout = MLX5_MAX_DELAY_DROP_TIMEOUT_MS * 1000;
3934 INIT_WORK(&dev->delay_drop.delay_drop_work, delay_drop_handler);
Maor Gottliebfe248c32017-05-30 10:29:14 +03003935 atomic_set(&dev->delay_drop.rqs_cnt, 0);
3936 atomic_set(&dev->delay_drop.events_cnt, 0);
3937
3938 if (delay_drop_debugfs_init(dev))
3939 mlx5_ib_warn(dev, "Failed to init delay drop debugfs\n");
Maor Gottlieb03404e82017-05-30 10:29:13 +03003940}
3941
Leon Romanovsky84305d712017-08-17 15:50:53 +03003942static const struct cpumask *
3943mlx5_ib_get_vector_affinity(struct ib_device *ibdev, int comp_vector)
Sagi Grimberg40b24402017-07-13 11:09:42 +03003944{
3945 struct mlx5_ib_dev *dev = to_mdev(ibdev);
3946
3947 return mlx5_get_vector_affinity(dev->mdev, comp_vector);
3948}
3949
Jack Morgenstein9603b612014-07-28 23:30:22 +03003950static void *mlx5_ib_add(struct mlx5_core_dev *mdev)
Eli Cohene126ba92013-07-07 17:25:49 +03003951{
Eli Cohene126ba92013-07-07 17:25:49 +03003952 struct mlx5_ib_dev *dev;
Achiad Shochatebd61f62015-12-23 18:47:16 +02003953 enum rdma_link_layer ll;
3954 int port_type_cap;
Aviv Heller4babcf92016-09-18 20:48:03 +03003955 const char *name;
Eli Cohene126ba92013-07-07 17:25:49 +03003956 int err;
3957 int i;
3958
Achiad Shochatebd61f62015-12-23 18:47:16 +02003959 port_type_cap = MLX5_CAP_GEN(mdev, port_type);
3960 ll = mlx5_port_type_cap_to_rdma_ll(port_type_cap);
3961
Eli Cohene126ba92013-07-07 17:25:49 +03003962 printk_once(KERN_INFO "%s", mlx5_version);
3963
3964 dev = (struct mlx5_ib_dev *)ib_alloc_device(sizeof(*dev));
3965 if (!dev)
Jack Morgenstein9603b612014-07-28 23:30:22 +03003966 return NULL;
Eli Cohene126ba92013-07-07 17:25:49 +03003967
Jack Morgenstein9603b612014-07-28 23:30:22 +03003968 dev->mdev = mdev;
Eli Cohene126ba92013-07-07 17:25:49 +03003969
Mark Bloch0837e862016-06-17 15:10:55 +03003970 dev->port = kcalloc(MLX5_CAP_GEN(mdev, num_ports), sizeof(*dev->port),
3971 GFP_KERNEL);
3972 if (!dev->port)
3973 goto err_dealloc;
3974
Achiad Shochatfc24fc52015-12-23 18:47:17 +02003975 rwlock_init(&dev->roce.netdev_lock);
Eli Cohene126ba92013-07-07 17:25:49 +03003976 err = get_port_caps(dev);
3977 if (err)
Mark Bloch0837e862016-06-17 15:10:55 +03003978 goto err_free_port;
Eli Cohene126ba92013-07-07 17:25:49 +03003979
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03003980 if (mlx5_use_mad_ifc(dev))
3981 get_ext_port_caps(dev);
Eli Cohene126ba92013-07-07 17:25:49 +03003982
Aviv Heller4babcf92016-09-18 20:48:03 +03003983 if (!mlx5_lag_is_active(mdev))
3984 name = "mlx5_%d";
3985 else
3986 name = "mlx5_bond_%d";
3987
3988 strlcpy(dev->ib_dev.name, name, IB_DEVICE_NAME_MAX);
Eli Cohene126ba92013-07-07 17:25:49 +03003989 dev->ib_dev.owner = THIS_MODULE;
3990 dev->ib_dev.node_type = RDMA_NODE_IB_CA;
Sagi Grimbergc6790aa2015-09-24 10:34:23 +03003991 dev->ib_dev.local_dma_lkey = 0 /* not supported for now */;
Saeed Mahameed938fe832015-05-28 22:28:41 +03003992 dev->num_ports = MLX5_CAP_GEN(mdev, num_ports);
Eli Cohene126ba92013-07-07 17:25:49 +03003993 dev->ib_dev.phys_port_cnt = dev->num_ports;
Saeed Mahameed233d05d2015-04-02 17:07:32 +03003994 dev->ib_dev.num_comp_vectors =
3995 dev->mdev->priv.eq_table.num_comp_vectors;
Bart Van Assche9b0c2892017-01-20 13:04:21 -08003996 dev->ib_dev.dev.parent = &mdev->pdev->dev;
Eli Cohene126ba92013-07-07 17:25:49 +03003997
3998 dev->ib_dev.uverbs_abi_ver = MLX5_IB_UVERBS_ABI_VERSION;
3999 dev->ib_dev.uverbs_cmd_mask =
4000 (1ull << IB_USER_VERBS_CMD_GET_CONTEXT) |
4001 (1ull << IB_USER_VERBS_CMD_QUERY_DEVICE) |
4002 (1ull << IB_USER_VERBS_CMD_QUERY_PORT) |
4003 (1ull << IB_USER_VERBS_CMD_ALLOC_PD) |
4004 (1ull << IB_USER_VERBS_CMD_DEALLOC_PD) |
Moni Shoua41c450f2016-11-23 08:23:26 +02004005 (1ull << IB_USER_VERBS_CMD_CREATE_AH) |
4006 (1ull << IB_USER_VERBS_CMD_DESTROY_AH) |
Eli Cohene126ba92013-07-07 17:25:49 +03004007 (1ull << IB_USER_VERBS_CMD_REG_MR) |
Noa Osherovich56e11d62016-02-29 16:46:51 +02004008 (1ull << IB_USER_VERBS_CMD_REREG_MR) |
Eli Cohene126ba92013-07-07 17:25:49 +03004009 (1ull << IB_USER_VERBS_CMD_DEREG_MR) |
4010 (1ull << IB_USER_VERBS_CMD_CREATE_COMP_CHANNEL) |
4011 (1ull << IB_USER_VERBS_CMD_CREATE_CQ) |
4012 (1ull << IB_USER_VERBS_CMD_RESIZE_CQ) |
4013 (1ull << IB_USER_VERBS_CMD_DESTROY_CQ) |
4014 (1ull << IB_USER_VERBS_CMD_CREATE_QP) |
4015 (1ull << IB_USER_VERBS_CMD_MODIFY_QP) |
4016 (1ull << IB_USER_VERBS_CMD_QUERY_QP) |
4017 (1ull << IB_USER_VERBS_CMD_DESTROY_QP) |
4018 (1ull << IB_USER_VERBS_CMD_ATTACH_MCAST) |
4019 (1ull << IB_USER_VERBS_CMD_DETACH_MCAST) |
4020 (1ull << IB_USER_VERBS_CMD_CREATE_SRQ) |
4021 (1ull << IB_USER_VERBS_CMD_MODIFY_SRQ) |
4022 (1ull << IB_USER_VERBS_CMD_QUERY_SRQ) |
4023 (1ull << IB_USER_VERBS_CMD_DESTROY_SRQ) |
4024 (1ull << IB_USER_VERBS_CMD_CREATE_XSRQ) |
4025 (1ull << IB_USER_VERBS_CMD_OPEN_QP);
Haggai Eran1707cb42015-02-08 13:28:52 +02004026 dev->ib_dev.uverbs_ex_cmd_mask =
Matan Barakd4584dd2016-01-28 17:51:46 +02004027 (1ull << IB_USER_VERBS_EX_CMD_QUERY_DEVICE) |
4028 (1ull << IB_USER_VERBS_EX_CMD_CREATE_CQ) |
Bodong Wang7d29f342016-12-01 13:43:16 +02004029 (1ull << IB_USER_VERBS_EX_CMD_CREATE_QP) |
Yonatan Cohenb0e9df62017-11-13 10:51:15 +02004030 (1ull << IB_USER_VERBS_EX_CMD_MODIFY_QP) |
4031 (1ull << IB_USER_VERBS_EX_CMD_MODIFY_CQ);
Eli Cohene126ba92013-07-07 17:25:49 +03004032
4033 dev->ib_dev.query_device = mlx5_ib_query_device;
4034 dev->ib_dev.query_port = mlx5_ib_query_port;
Achiad Shochatebd61f62015-12-23 18:47:16 +02004035 dev->ib_dev.get_link_layer = mlx5_ib_port_link_layer;
Achiad Shochatfc24fc52015-12-23 18:47:17 +02004036 if (ll == IB_LINK_LAYER_ETHERNET)
4037 dev->ib_dev.get_netdev = mlx5_ib_get_netdev;
Eli Cohene126ba92013-07-07 17:25:49 +03004038 dev->ib_dev.query_gid = mlx5_ib_query_gid;
Achiad Shochat3cca2602015-12-23 18:47:23 +02004039 dev->ib_dev.add_gid = mlx5_ib_add_gid;
4040 dev->ib_dev.del_gid = mlx5_ib_del_gid;
Eli Cohene126ba92013-07-07 17:25:49 +03004041 dev->ib_dev.query_pkey = mlx5_ib_query_pkey;
4042 dev->ib_dev.modify_device = mlx5_ib_modify_device;
4043 dev->ib_dev.modify_port = mlx5_ib_modify_port;
4044 dev->ib_dev.alloc_ucontext = mlx5_ib_alloc_ucontext;
4045 dev->ib_dev.dealloc_ucontext = mlx5_ib_dealloc_ucontext;
4046 dev->ib_dev.mmap = mlx5_ib_mmap;
4047 dev->ib_dev.alloc_pd = mlx5_ib_alloc_pd;
4048 dev->ib_dev.dealloc_pd = mlx5_ib_dealloc_pd;
4049 dev->ib_dev.create_ah = mlx5_ib_create_ah;
4050 dev->ib_dev.query_ah = mlx5_ib_query_ah;
4051 dev->ib_dev.destroy_ah = mlx5_ib_destroy_ah;
4052 dev->ib_dev.create_srq = mlx5_ib_create_srq;
4053 dev->ib_dev.modify_srq = mlx5_ib_modify_srq;
4054 dev->ib_dev.query_srq = mlx5_ib_query_srq;
4055 dev->ib_dev.destroy_srq = mlx5_ib_destroy_srq;
4056 dev->ib_dev.post_srq_recv = mlx5_ib_post_srq_recv;
4057 dev->ib_dev.create_qp = mlx5_ib_create_qp;
4058 dev->ib_dev.modify_qp = mlx5_ib_modify_qp;
4059 dev->ib_dev.query_qp = mlx5_ib_query_qp;
4060 dev->ib_dev.destroy_qp = mlx5_ib_destroy_qp;
4061 dev->ib_dev.post_send = mlx5_ib_post_send;
4062 dev->ib_dev.post_recv = mlx5_ib_post_recv;
4063 dev->ib_dev.create_cq = mlx5_ib_create_cq;
4064 dev->ib_dev.modify_cq = mlx5_ib_modify_cq;
4065 dev->ib_dev.resize_cq = mlx5_ib_resize_cq;
4066 dev->ib_dev.destroy_cq = mlx5_ib_destroy_cq;
4067 dev->ib_dev.poll_cq = mlx5_ib_poll_cq;
4068 dev->ib_dev.req_notify_cq = mlx5_ib_arm_cq;
4069 dev->ib_dev.get_dma_mr = mlx5_ib_get_dma_mr;
4070 dev->ib_dev.reg_user_mr = mlx5_ib_reg_user_mr;
Noa Osherovich56e11d62016-02-29 16:46:51 +02004071 dev->ib_dev.rereg_user_mr = mlx5_ib_rereg_user_mr;
Eli Cohene126ba92013-07-07 17:25:49 +03004072 dev->ib_dev.dereg_mr = mlx5_ib_dereg_mr;
4073 dev->ib_dev.attach_mcast = mlx5_ib_mcg_attach;
4074 dev->ib_dev.detach_mcast = mlx5_ib_mcg_detach;
4075 dev->ib_dev.process_mad = mlx5_ib_process_mad;
Sagi Grimberg9bee1782015-07-30 10:32:35 +03004076 dev->ib_dev.alloc_mr = mlx5_ib_alloc_mr;
Sagi Grimberg8a187ee2015-10-13 19:11:26 +03004077 dev->ib_dev.map_mr_sg = mlx5_ib_map_mr_sg;
Sagi Grimbergd5436ba2014-02-23 14:19:12 +02004078 dev->ib_dev.check_mr_status = mlx5_ib_check_mr_status;
Ira Weiny77386132015-05-13 20:02:58 -04004079 dev->ib_dev.get_port_immutable = mlx5_port_immutable;
Ira Weinyc7342822016-06-15 02:22:01 -04004080 dev->ib_dev.get_dev_fw_str = get_dev_fw_str;
Sagi Grimberg40b24402017-07-13 11:09:42 +03004081 dev->ib_dev.get_vector_affinity = mlx5_ib_get_vector_affinity;
Niranjana Vishwanathapura8e959602017-06-30 13:14:46 -07004082 if (MLX5_CAP_GEN(mdev, ipoib_enhanced_offloads))
Alex Vesker022d0382017-06-14 09:59:06 +03004083 dev->ib_dev.alloc_rdma_netdev = mlx5_ib_alloc_rdma_netdev;
Niranjana Vishwanathapura8e959602017-06-30 13:14:46 -07004084
Eli Coheneff901d2016-03-11 22:58:42 +02004085 if (mlx5_core_is_pf(mdev)) {
4086 dev->ib_dev.get_vf_config = mlx5_ib_get_vf_config;
4087 dev->ib_dev.set_vf_link_state = mlx5_ib_set_vf_link_state;
4088 dev->ib_dev.get_vf_stats = mlx5_ib_get_vf_stats;
4089 dev->ib_dev.set_vf_guid = mlx5_ib_set_vf_guid;
4090 }
Eli Cohene126ba92013-07-07 17:25:49 +03004091
Maor Gottlieb7c2344c2016-06-17 14:56:44 +03004092 dev->ib_dev.disassociate_ucontext = mlx5_ib_disassociate_ucontext;
4093
Saeed Mahameed938fe832015-05-28 22:28:41 +03004094 mlx5_ib_internal_fill_odp_caps(dev);
Haggai Eran8cdd3122014-12-11 17:04:20 +02004095
Max Gurtovoy6e8484c2017-05-28 10:53:11 +03004096 dev->umr_fence = mlx5_get_umr_fence(MLX5_CAP_GEN(mdev, umr_fence));
4097
Matan Barakd2370e02016-02-29 18:05:30 +02004098 if (MLX5_CAP_GEN(mdev, imaicl)) {
4099 dev->ib_dev.alloc_mw = mlx5_ib_alloc_mw;
4100 dev->ib_dev.dealloc_mw = mlx5_ib_dealloc_mw;
4101 dev->ib_dev.uverbs_cmd_mask |=
4102 (1ull << IB_USER_VERBS_CMD_ALLOC_MW) |
4103 (1ull << IB_USER_VERBS_CMD_DEALLOC_MW);
4104 }
4105
Kamal Heib7c16f472017-01-18 15:25:09 +02004106 if (MLX5_CAP_GEN(dev->mdev, max_qp_cnt)) {
Mark Bloch0ad17a82016-06-17 15:10:56 +03004107 dev->ib_dev.get_hw_stats = mlx5_ib_get_hw_stats;
4108 dev->ib_dev.alloc_hw_stats = mlx5_ib_alloc_hw_stats;
4109 }
4110
Saeed Mahameed938fe832015-05-28 22:28:41 +03004111 if (MLX5_CAP_GEN(mdev, xrc)) {
Eli Cohene126ba92013-07-07 17:25:49 +03004112 dev->ib_dev.alloc_xrcd = mlx5_ib_alloc_xrcd;
4113 dev->ib_dev.dealloc_xrcd = mlx5_ib_dealloc_xrcd;
4114 dev->ib_dev.uverbs_cmd_mask |=
4115 (1ull << IB_USER_VERBS_CMD_OPEN_XRCD) |
4116 (1ull << IB_USER_VERBS_CMD_CLOSE_XRCD);
4117 }
4118
Yishai Hadas81e30882017-06-08 16:15:09 +03004119 dev->ib_dev.create_flow = mlx5_ib_create_flow;
4120 dev->ib_dev.destroy_flow = mlx5_ib_destroy_flow;
4121 dev->ib_dev.uverbs_ex_cmd_mask |=
4122 (1ull << IB_USER_VERBS_EX_CMD_CREATE_FLOW) |
4123 (1ull << IB_USER_VERBS_EX_CMD_DESTROY_FLOW);
4124
Linus Torvalds048ccca2016-01-23 18:45:06 -08004125 if (mlx5_ib_port_link_layer(&dev->ib_dev, 1) ==
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02004126 IB_LINK_LAYER_ETHERNET) {
Yishai Hadas79b20a62016-05-23 15:20:50 +03004127 dev->ib_dev.create_wq = mlx5_ib_create_wq;
4128 dev->ib_dev.modify_wq = mlx5_ib_modify_wq;
4129 dev->ib_dev.destroy_wq = mlx5_ib_destroy_wq;
Yishai Hadasc5f90922016-05-23 15:20:53 +03004130 dev->ib_dev.create_rwq_ind_table = mlx5_ib_create_rwq_ind_table;
4131 dev->ib_dev.destroy_rwq_ind_table = mlx5_ib_destroy_rwq_ind_table;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02004132 dev->ib_dev.uverbs_ex_cmd_mask |=
Yishai Hadas79b20a62016-05-23 15:20:50 +03004133 (1ull << IB_USER_VERBS_EX_CMD_CREATE_WQ) |
4134 (1ull << IB_USER_VERBS_EX_CMD_MODIFY_WQ) |
Yishai Hadasc5f90922016-05-23 15:20:53 +03004135 (1ull << IB_USER_VERBS_EX_CMD_DESTROY_WQ) |
4136 (1ull << IB_USER_VERBS_EX_CMD_CREATE_RWQ_IND_TBL) |
4137 (1ull << IB_USER_VERBS_EX_CMD_DESTROY_RWQ_IND_TBL);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02004138 }
Eli Cohene126ba92013-07-07 17:25:49 +03004139 err = init_node_data(dev);
4140 if (err)
Majd Dibbiny90be7c82016-10-27 16:36:39 +03004141 goto err_free_port;
Eli Cohene126ba92013-07-07 17:25:49 +03004142
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02004143 mutex_init(&dev->flow_db.lock);
Eli Cohene126ba92013-07-07 17:25:49 +03004144 mutex_init(&dev->cap_mask_mutex);
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03004145 INIT_LIST_HEAD(&dev->qp_list);
4146 spin_lock_init(&dev->reset_flow_resource_lock);
Eli Cohene126ba92013-07-07 17:25:49 +03004147
Achiad Shochatfc24fc52015-12-23 18:47:17 +02004148 if (ll == IB_LINK_LAYER_ETHERNET) {
Or Gerlitz45f95ac2016-11-27 16:51:35 +02004149 err = mlx5_enable_eth(dev);
Achiad Shochatfc24fc52015-12-23 18:47:17 +02004150 if (err)
Majd Dibbiny90be7c82016-10-27 16:36:39 +03004151 goto err_free_port;
Moni Shouafd65f1b2017-05-30 09:56:05 +03004152 dev->roce.last_port_state = IB_PORT_DOWN;
Achiad Shochatfc24fc52015-12-23 18:47:17 +02004153 }
4154
Eli Cohene126ba92013-07-07 17:25:49 +03004155 err = create_dev_resources(&dev->devr);
4156 if (err)
Or Gerlitz45f95ac2016-11-27 16:51:35 +02004157 goto err_disable_eth;
Eli Cohene126ba92013-07-07 17:25:49 +03004158
Haggai Eran6aec21f2014-12-11 17:04:23 +02004159 err = mlx5_ib_odp_init_one(dev);
Wei Yongjun281d1a92013-07-30 07:54:26 +08004160 if (err)
Eli Cohene126ba92013-07-07 17:25:49 +03004161 goto err_rsrc;
4162
Kamal Heib45bded22017-01-18 14:10:32 +02004163 if (MLX5_CAP_GEN(dev->mdev, max_qp_cnt)) {
Parav Pandite1f24a72017-04-16 07:29:29 +03004164 err = mlx5_ib_alloc_counters(dev);
Kamal Heib45bded22017-01-18 14:10:32 +02004165 if (err)
4166 goto err_odp;
4167 }
Haggai Eran6aec21f2014-12-11 17:04:23 +02004168
Parav Pandit4a2da0b2017-05-30 10:05:15 +03004169 err = mlx5_ib_init_cong_debugfs(dev);
4170 if (err)
4171 goto err_cnt;
4172
Eli Cohen5fe9dec2017-01-03 23:55:25 +02004173 dev->mdev->priv.uar = mlx5_get_uars_page(dev->mdev);
4174 if (!dev->mdev->priv.uar)
Parav Pandit4a2da0b2017-05-30 10:05:15 +03004175 goto err_cong;
Eli Cohen5fe9dec2017-01-03 23:55:25 +02004176
4177 err = mlx5_alloc_bfreg(dev->mdev, &dev->bfreg, false, false);
4178 if (err)
4179 goto err_uar_page;
4180
4181 err = mlx5_alloc_bfreg(dev->mdev, &dev->fp_bfreg, false, true);
4182 if (err)
4183 goto err_bfreg;
4184
Mark Bloch0837e862016-06-17 15:10:55 +03004185 err = ib_register_device(&dev->ib_dev, NULL);
4186 if (err)
Eli Cohen5fe9dec2017-01-03 23:55:25 +02004187 goto err_fp_bfreg;
Mark Bloch0837e862016-06-17 15:10:55 +03004188
Eli Cohene126ba92013-07-07 17:25:49 +03004189 err = create_umr_res(dev);
4190 if (err)
4191 goto err_dev;
4192
Maor Gottlieb03404e82017-05-30 10:29:13 +03004193 init_delay_drop(dev);
4194
Eli Cohene126ba92013-07-07 17:25:49 +03004195 for (i = 0; i < ARRAY_SIZE(mlx5_class_attributes); i++) {
Wei Yongjun281d1a92013-07-30 07:54:26 +08004196 err = device_create_file(&dev->ib_dev.dev,
4197 mlx5_class_attributes[i]);
4198 if (err)
Maor Gottlieb03404e82017-05-30 10:29:13 +03004199 goto err_delay_drop;
Eli Cohene126ba92013-07-07 17:25:49 +03004200 }
4201
Huy Nguyenc85023e2017-05-30 09:42:54 +03004202 if ((MLX5_CAP_GEN(mdev, port_type) == MLX5_CAP_PORT_TYPE_ETH) &&
4203 MLX5_CAP_GEN(mdev, disable_local_lb))
4204 mutex_init(&dev->lb_mutex);
4205
Eli Cohene126ba92013-07-07 17:25:49 +03004206 dev->ib_active = true;
4207
Jack Morgenstein9603b612014-07-28 23:30:22 +03004208 return dev;
Eli Cohene126ba92013-07-07 17:25:49 +03004209
Maor Gottlieb03404e82017-05-30 10:29:13 +03004210err_delay_drop:
4211 cancel_delay_drop(dev);
Eli Cohene126ba92013-07-07 17:25:49 +03004212 destroy_umrc_res(dev);
4213
4214err_dev:
4215 ib_unregister_device(&dev->ib_dev);
4216
Eli Cohen5fe9dec2017-01-03 23:55:25 +02004217err_fp_bfreg:
4218 mlx5_free_bfreg(dev->mdev, &dev->fp_bfreg);
4219
4220err_bfreg:
4221 mlx5_free_bfreg(dev->mdev, &dev->bfreg);
4222
4223err_uar_page:
4224 mlx5_put_uars_page(dev->mdev, dev->mdev->priv.uar);
4225
Parav Pandit4a2da0b2017-05-30 10:05:15 +03004226err_cong:
Parav Pandite19cd282017-10-01 09:54:35 +03004227 mlx5_ib_cleanup_cong_debugfs(dev);
4228err_cnt:
Kamal Heib45bded22017-01-18 14:10:32 +02004229 if (MLX5_CAP_GEN(dev->mdev, max_qp_cnt))
Parav Pandite1f24a72017-04-16 07:29:29 +03004230 mlx5_ib_dealloc_counters(dev);
Mark Bloch0837e862016-06-17 15:10:55 +03004231
Haggai Eran6aec21f2014-12-11 17:04:23 +02004232err_odp:
4233 mlx5_ib_odp_remove_one(dev);
4234
Eli Cohene126ba92013-07-07 17:25:49 +03004235err_rsrc:
4236 destroy_dev_resources(&dev->devr);
4237
Or Gerlitz45f95ac2016-11-27 16:51:35 +02004238err_disable_eth:
Aviv Heller5ec8c832016-09-18 20:48:00 +03004239 if (ll == IB_LINK_LAYER_ETHERNET) {
Or Gerlitz45f95ac2016-11-27 16:51:35 +02004240 mlx5_disable_eth(dev);
Or Gerlitzd012f5d2016-11-27 16:51:34 +02004241 mlx5_remove_netdev_notifier(dev);
Aviv Heller5ec8c832016-09-18 20:48:00 +03004242 }
Achiad Shochatfc24fc52015-12-23 18:47:17 +02004243
Mark Bloch0837e862016-06-17 15:10:55 +03004244err_free_port:
4245 kfree(dev->port);
4246
Jack Morgenstein9603b612014-07-28 23:30:22 +03004247err_dealloc:
Eli Cohene126ba92013-07-07 17:25:49 +03004248 ib_dealloc_device((struct ib_device *)dev);
4249
Jack Morgenstein9603b612014-07-28 23:30:22 +03004250 return NULL;
Eli Cohene126ba92013-07-07 17:25:49 +03004251}
4252
Jack Morgenstein9603b612014-07-28 23:30:22 +03004253static void mlx5_ib_remove(struct mlx5_core_dev *mdev, void *context)
Eli Cohene126ba92013-07-07 17:25:49 +03004254{
Jack Morgenstein9603b612014-07-28 23:30:22 +03004255 struct mlx5_ib_dev *dev = context;
Achiad Shochatfc24fc52015-12-23 18:47:17 +02004256 enum rdma_link_layer ll = mlx5_ib_port_link_layer(&dev->ib_dev, 1);
Haggai Eran6aec21f2014-12-11 17:04:23 +02004257
Maor Gottlieb03404e82017-05-30 10:29:13 +03004258 cancel_delay_drop(dev);
Or Gerlitzd012f5d2016-11-27 16:51:34 +02004259 mlx5_remove_netdev_notifier(dev);
Eli Cohene126ba92013-07-07 17:25:49 +03004260 ib_unregister_device(&dev->ib_dev);
Eli Cohen5fe9dec2017-01-03 23:55:25 +02004261 mlx5_free_bfreg(dev->mdev, &dev->fp_bfreg);
4262 mlx5_free_bfreg(dev->mdev, &dev->bfreg);
4263 mlx5_put_uars_page(dev->mdev, mdev->priv.uar);
Parav Pandit4a2da0b2017-05-30 10:05:15 +03004264 mlx5_ib_cleanup_cong_debugfs(dev);
Kamal Heib45bded22017-01-18 14:10:32 +02004265 if (MLX5_CAP_GEN(dev->mdev, max_qp_cnt))
Parav Pandite1f24a72017-04-16 07:29:29 +03004266 mlx5_ib_dealloc_counters(dev);
Eli Coheneefd56e2014-09-14 16:47:50 +03004267 destroy_umrc_res(dev);
Haggai Eran6aec21f2014-12-11 17:04:23 +02004268 mlx5_ib_odp_remove_one(dev);
Eli Cohene126ba92013-07-07 17:25:49 +03004269 destroy_dev_resources(&dev->devr);
Achiad Shochatfc24fc52015-12-23 18:47:17 +02004270 if (ll == IB_LINK_LAYER_ETHERNET)
Or Gerlitz45f95ac2016-11-27 16:51:35 +02004271 mlx5_disable_eth(dev);
Mark Bloch0837e862016-06-17 15:10:55 +03004272 kfree(dev->port);
Eli Cohene126ba92013-07-07 17:25:49 +03004273 ib_dealloc_device(&dev->ib_dev);
4274}
4275
Jack Morgenstein9603b612014-07-28 23:30:22 +03004276static struct mlx5_interface mlx5_ib_interface = {
4277 .add = mlx5_ib_add,
4278 .remove = mlx5_ib_remove,
4279 .event = mlx5_ib_event,
Artemy Kovalyovd9aaed82017-01-02 11:37:46 +02004280#ifdef CONFIG_INFINIBAND_ON_DEMAND_PAGING
4281 .pfault = mlx5_ib_pfault,
4282#endif
Saeed Mahameed64613d942015-04-02 17:07:34 +03004283 .protocol = MLX5_INTERFACE_PROTOCOL_IB,
Eli Cohene126ba92013-07-07 17:25:49 +03004284};
4285
4286static int __init mlx5_ib_init(void)
4287{
Haggai Eran6aec21f2014-12-11 17:04:23 +02004288 int err;
4289
Artemy Kovalyov81713d32017-01-18 16:58:11 +02004290 mlx5_ib_odp_init();
Jack Morgenstein9603b612014-07-28 23:30:22 +03004291
Haggai Eran6aec21f2014-12-11 17:04:23 +02004292 err = mlx5_register_interface(&mlx5_ib_interface);
Haggai Eran6aec21f2014-12-11 17:04:23 +02004293
4294 return err;
Eli Cohene126ba92013-07-07 17:25:49 +03004295}
4296
4297static void __exit mlx5_ib_cleanup(void)
4298{
Jack Morgenstein9603b612014-07-28 23:30:22 +03004299 mlx5_unregister_interface(&mlx5_ib_interface);
Eli Cohene126ba92013-07-07 17:25:49 +03004300}
4301
4302module_init(mlx5_ib_init);
4303module_exit(mlx5_ib_cleanup);