blob: ad4b12decc23f4baef07c354c0247eb01e78cd30 [file] [log] [blame]
Eli Cohene126ba92013-07-07 17:25:49 +03001/*
Saeed Mahameed6cf0a152015-04-02 17:07:30 +03002 * Copyright (c) 2013-2015, Mellanox Technologies. All rights reserved.
Eli Cohene126ba92013-07-07 17:25:49 +03003 *
4 * This software is available to you under a choice of one of two
5 * licenses. You may choose to be licensed under the terms of the GNU
6 * General Public License (GPL) Version 2, available from the file
7 * COPYING in the main directory of this source tree, or the
8 * OpenIB.org BSD license below:
9 *
10 * Redistribution and use in source and binary forms, with or
11 * without modification, are permitted provided that the following
12 * conditions are met:
13 *
14 * - Redistributions of source code must retain the above
15 * copyright notice, this list of conditions and the following
16 * disclaimer.
17 *
18 * - Redistributions in binary form must reproduce the above
19 * copyright notice, this list of conditions and the following
20 * disclaimer in the documentation and/or other materials
21 * provided with the distribution.
22 *
23 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
27 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
28 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
29 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
30 * SOFTWARE.
31 */
32
Christoph Hellwigadec6402015-08-28 09:27:19 +020033#include <linux/highmem.h>
Eli Cohene126ba92013-07-07 17:25:49 +030034#include <linux/module.h>
35#include <linux/init.h>
36#include <linux/errno.h>
37#include <linux/pci.h>
38#include <linux/dma-mapping.h>
39#include <linux/slab.h>
Guy Levi37aa5c32016-04-27 16:49:50 +030040#if defined(CONFIG_X86)
41#include <asm/pat.h>
42#endif
Eli Cohene126ba92013-07-07 17:25:49 +030043#include <linux/sched.h>
Ingo Molnar6e84f312017-02-08 18:51:29 +010044#include <linux/sched/mm.h>
Ingo Molnar0881e7b2017-02-05 15:30:50 +010045#include <linux/sched/task.h>
Maor Gottlieb7c2344c2016-06-17 14:56:44 +030046#include <linux/delay.h>
Eli Cohene126ba92013-07-07 17:25:49 +030047#include <rdma/ib_user_verbs.h>
Achiad Shochat3f89a642015-12-23 18:47:21 +020048#include <rdma/ib_addr.h>
Achiad Shochat2811ba52015-12-23 18:47:24 +020049#include <rdma/ib_cache.h>
Achiad Shochatada68c32016-02-22 18:17:23 +020050#include <linux/mlx5/port.h>
Majd Dibbiny1b5daf12015-06-04 19:30:46 +030051#include <linux/mlx5/vport.h>
Maor Gottlieb7c2344c2016-06-17 14:56:44 +030052#include <linux/list.h>
Eli Cohene126ba92013-07-07 17:25:49 +030053#include <rdma/ib_smi.h>
54#include <rdma/ib_umem.h>
Maor Gottlieb038d2ef2016-01-11 10:26:07 +020055#include <linux/in.h>
56#include <linux/etherdevice.h>
57#include <linux/mlx5/fs.h>
Or Gerlitz78984892016-11-30 20:33:33 +020058#include <linux/mlx5/vport.h>
Eli Cohene126ba92013-07-07 17:25:49 +030059#include "mlx5_ib.h"
Parav Pandite1f24a72017-04-16 07:29:29 +030060#include "cmd.h"
Huy Nguyenc85023e2017-05-30 09:42:54 +030061#include <linux/mlx5/vport.h>
Eli Cohene126ba92013-07-07 17:25:49 +030062
63#define DRIVER_NAME "mlx5_ib"
Tariq Toukanb3599112017-02-22 17:45:46 +020064#define DRIVER_VERSION "5.0-0"
Eli Cohene126ba92013-07-07 17:25:49 +030065
66MODULE_AUTHOR("Eli Cohen <eli@mellanox.com>");
67MODULE_DESCRIPTION("Mellanox Connect-IB HCA IB driver");
68MODULE_LICENSE("Dual BSD/GPL");
69MODULE_VERSION(DRIVER_VERSION);
70
Eli Cohene126ba92013-07-07 17:25:49 +030071static char mlx5_version[] =
72 DRIVER_NAME ": Mellanox Connect-IB Infiniband driver v"
Tariq Toukanb3599112017-02-22 17:45:46 +020073 DRIVER_VERSION "\n";
Eli Cohene126ba92013-07-07 17:25:49 +030074
Eran Ben Elishada7525d2015-12-14 16:34:10 +020075enum {
76 MLX5_ATOMIC_SIZE_QP_8BYTES = 1 << 3,
77};
Majd Dibbiny1b5daf12015-06-04 19:30:46 +030078
Majd Dibbiny1b5daf12015-06-04 19:30:46 +030079static enum rdma_link_layer
Achiad Shochatebd61f62015-12-23 18:47:16 +020080mlx5_port_type_cap_to_rdma_ll(int port_type_cap)
Majd Dibbiny1b5daf12015-06-04 19:30:46 +030081{
Achiad Shochatebd61f62015-12-23 18:47:16 +020082 switch (port_type_cap) {
Majd Dibbiny1b5daf12015-06-04 19:30:46 +030083 case MLX5_CAP_PORT_TYPE_IB:
84 return IB_LINK_LAYER_INFINIBAND;
85 case MLX5_CAP_PORT_TYPE_ETH:
86 return IB_LINK_LAYER_ETHERNET;
87 default:
88 return IB_LINK_LAYER_UNSPECIFIED;
89 }
90}
91
Achiad Shochatebd61f62015-12-23 18:47:16 +020092static enum rdma_link_layer
93mlx5_ib_port_link_layer(struct ib_device *device, u8 port_num)
94{
95 struct mlx5_ib_dev *dev = to_mdev(device);
96 int port_type_cap = MLX5_CAP_GEN(dev->mdev, port_type);
97
98 return mlx5_port_type_cap_to_rdma_ll(port_type_cap);
99}
100
Moni Shouafd65f1b2017-05-30 09:56:05 +0300101static int get_port_state(struct ib_device *ibdev,
102 u8 port_num,
103 enum ib_port_state *state)
104{
105 struct ib_port_attr attr;
106 int ret;
107
108 memset(&attr, 0, sizeof(attr));
109 ret = mlx5_ib_query_port(ibdev, port_num, &attr);
110 if (!ret)
111 *state = attr.state;
112 return ret;
113}
114
Achiad Shochatfc24fc52015-12-23 18:47:17 +0200115static int mlx5_netdev_event(struct notifier_block *this,
116 unsigned long event, void *ptr)
117{
118 struct net_device *ndev = netdev_notifier_info_to_dev(ptr);
119 struct mlx5_ib_dev *ibdev = container_of(this, struct mlx5_ib_dev,
120 roce.nb);
121
Aviv Heller5ec8c832016-09-18 20:48:00 +0300122 switch (event) {
123 case NETDEV_REGISTER:
124 case NETDEV_UNREGISTER:
125 write_lock(&ibdev->roce.netdev_lock);
126 if (ndev->dev.parent == &ibdev->mdev->pdev->dev)
127 ibdev->roce.netdev = (event == NETDEV_UNREGISTER) ?
128 NULL : ndev;
129 write_unlock(&ibdev->roce.netdev_lock);
130 break;
Achiad Shochatfc24fc52015-12-23 18:47:17 +0200131
Moni Shouafd65f1b2017-05-30 09:56:05 +0300132 case NETDEV_CHANGE:
Aviv Heller5ec8c832016-09-18 20:48:00 +0300133 case NETDEV_UP:
Aviv Heller88621df2016-09-18 20:48:02 +0300134 case NETDEV_DOWN: {
135 struct net_device *lag_ndev = mlx5_lag_get_roce_netdev(ibdev->mdev);
136 struct net_device *upper = NULL;
137
138 if (lag_ndev) {
139 upper = netdev_master_upper_dev_get(lag_ndev);
140 dev_put(lag_ndev);
141 }
142
143 if ((upper == ndev || (!upper && ndev == ibdev->roce.netdev))
144 && ibdev->ib_active) {
Bart Van Assche626bc022016-12-05 17:18:08 -0800145 struct ib_event ibev = { };
Moni Shouafd65f1b2017-05-30 09:56:05 +0300146 enum ib_port_state port_state;
Aviv Heller5ec8c832016-09-18 20:48:00 +0300147
Moni Shouafd65f1b2017-05-30 09:56:05 +0300148 if (get_port_state(&ibdev->ib_dev, 1, &port_state))
149 return NOTIFY_DONE;
150
151 if (ibdev->roce.last_port_state == port_state)
152 return NOTIFY_DONE;
153
154 ibdev->roce.last_port_state = port_state;
Aviv Heller5ec8c832016-09-18 20:48:00 +0300155 ibev.device = &ibdev->ib_dev;
Moni Shouafd65f1b2017-05-30 09:56:05 +0300156 if (port_state == IB_PORT_DOWN)
157 ibev.event = IB_EVENT_PORT_ERR;
158 else if (port_state == IB_PORT_ACTIVE)
159 ibev.event = IB_EVENT_PORT_ACTIVE;
160 else
161 return NOTIFY_DONE;
162
Aviv Heller5ec8c832016-09-18 20:48:00 +0300163 ibev.element.port_num = 1;
164 ib_dispatch_event(&ibev);
165 }
166 break;
Aviv Heller88621df2016-09-18 20:48:02 +0300167 }
Aviv Heller5ec8c832016-09-18 20:48:00 +0300168
169 default:
170 break;
171 }
Achiad Shochatfc24fc52015-12-23 18:47:17 +0200172
173 return NOTIFY_DONE;
174}
175
176static struct net_device *mlx5_ib_get_netdev(struct ib_device *device,
177 u8 port_num)
178{
179 struct mlx5_ib_dev *ibdev = to_mdev(device);
180 struct net_device *ndev;
181
Aviv Heller88621df2016-09-18 20:48:02 +0300182 ndev = mlx5_lag_get_roce_netdev(ibdev->mdev);
183 if (ndev)
184 return ndev;
185
Achiad Shochatfc24fc52015-12-23 18:47:17 +0200186 /* Ensure ndev does not disappear before we invoke dev_hold()
187 */
188 read_lock(&ibdev->roce.netdev_lock);
189 ndev = ibdev->roce.netdev;
190 if (ndev)
191 dev_hold(ndev);
192 read_unlock(&ibdev->roce.netdev_lock);
193
194 return ndev;
195}
196
Noa Osherovichf1b65df2017-04-20 20:53:33 +0300197static int translate_eth_proto_oper(u32 eth_proto_oper, u8 *active_speed,
198 u8 *active_width)
199{
200 switch (eth_proto_oper) {
201 case MLX5E_PROT_MASK(MLX5E_1000BASE_CX_SGMII):
202 case MLX5E_PROT_MASK(MLX5E_1000BASE_KX):
203 case MLX5E_PROT_MASK(MLX5E_100BASE_TX):
204 case MLX5E_PROT_MASK(MLX5E_1000BASE_T):
205 *active_width = IB_WIDTH_1X;
206 *active_speed = IB_SPEED_SDR;
207 break;
208 case MLX5E_PROT_MASK(MLX5E_10GBASE_T):
209 case MLX5E_PROT_MASK(MLX5E_10GBASE_CX4):
210 case MLX5E_PROT_MASK(MLX5E_10GBASE_KX4):
211 case MLX5E_PROT_MASK(MLX5E_10GBASE_KR):
212 case MLX5E_PROT_MASK(MLX5E_10GBASE_CR):
213 case MLX5E_PROT_MASK(MLX5E_10GBASE_SR):
214 case MLX5E_PROT_MASK(MLX5E_10GBASE_ER):
215 *active_width = IB_WIDTH_1X;
216 *active_speed = IB_SPEED_QDR;
217 break;
218 case MLX5E_PROT_MASK(MLX5E_25GBASE_CR):
219 case MLX5E_PROT_MASK(MLX5E_25GBASE_KR):
220 case MLX5E_PROT_MASK(MLX5E_25GBASE_SR):
221 *active_width = IB_WIDTH_1X;
222 *active_speed = IB_SPEED_EDR;
223 break;
224 case MLX5E_PROT_MASK(MLX5E_40GBASE_CR4):
225 case MLX5E_PROT_MASK(MLX5E_40GBASE_KR4):
226 case MLX5E_PROT_MASK(MLX5E_40GBASE_SR4):
227 case MLX5E_PROT_MASK(MLX5E_40GBASE_LR4):
228 *active_width = IB_WIDTH_4X;
229 *active_speed = IB_SPEED_QDR;
230 break;
231 case MLX5E_PROT_MASK(MLX5E_50GBASE_CR2):
232 case MLX5E_PROT_MASK(MLX5E_50GBASE_KR2):
233 case MLX5E_PROT_MASK(MLX5E_50GBASE_SR2):
234 *active_width = IB_WIDTH_1X;
235 *active_speed = IB_SPEED_HDR;
236 break;
237 case MLX5E_PROT_MASK(MLX5E_56GBASE_R4):
238 *active_width = IB_WIDTH_4X;
239 *active_speed = IB_SPEED_FDR;
240 break;
241 case MLX5E_PROT_MASK(MLX5E_100GBASE_CR4):
242 case MLX5E_PROT_MASK(MLX5E_100GBASE_SR4):
243 case MLX5E_PROT_MASK(MLX5E_100GBASE_KR4):
244 case MLX5E_PROT_MASK(MLX5E_100GBASE_LR4):
245 *active_width = IB_WIDTH_4X;
246 *active_speed = IB_SPEED_EDR;
247 break;
248 default:
249 return -EINVAL;
250 }
251
252 return 0;
253}
254
Ilan Tayari095b0922017-05-14 16:04:30 +0300255static int mlx5_query_port_roce(struct ib_device *device, u8 port_num,
256 struct ib_port_attr *props)
Achiad Shochat3f89a642015-12-23 18:47:21 +0200257{
258 struct mlx5_ib_dev *dev = to_mdev(device);
Noa Osherovichf1b65df2017-04-20 20:53:33 +0300259 struct mlx5_core_dev *mdev = dev->mdev;
Aviv Heller88621df2016-09-18 20:48:02 +0300260 struct net_device *ndev, *upper;
Achiad Shochat3f89a642015-12-23 18:47:21 +0200261 enum ib_mtu ndev_ib_mtu;
Leon Romanovskyc876a1b2016-01-09 13:06:25 +0200262 u16 qkey_viol_cntr;
Noa Osherovichf1b65df2017-04-20 20:53:33 +0300263 u32 eth_prot_oper;
Ilan Tayari095b0922017-05-14 16:04:30 +0300264 int err;
Achiad Shochat3f89a642015-12-23 18:47:21 +0200265
Noa Osherovichf1b65df2017-04-20 20:53:33 +0300266 /* Possible bad flows are checked before filling out props so in case
267 * of an error it will still be zeroed out.
Noa Osherovich50f22fd2017-04-20 20:53:32 +0300268 */
Ilan Tayari095b0922017-05-14 16:04:30 +0300269 err = mlx5_query_port_eth_proto_oper(mdev, &eth_prot_oper, port_num);
270 if (err)
271 return err;
Noa Osherovichf1b65df2017-04-20 20:53:33 +0300272
273 translate_eth_proto_oper(eth_prot_oper, &props->active_speed,
274 &props->active_width);
Achiad Shochat3f89a642015-12-23 18:47:21 +0200275
276 props->port_cap_flags |= IB_PORT_CM_SUP;
277 props->port_cap_flags |= IB_PORT_IP_BASED_GIDS;
278
279 props->gid_tbl_len = MLX5_CAP_ROCE(dev->mdev,
280 roce_address_table_size);
281 props->max_mtu = IB_MTU_4096;
282 props->max_msg_sz = 1 << MLX5_CAP_GEN(dev->mdev, log_max_msg);
283 props->pkey_tbl_len = 1;
284 props->state = IB_PORT_DOWN;
285 props->phys_state = 3;
286
Leon Romanovskyc876a1b2016-01-09 13:06:25 +0200287 mlx5_query_nic_vport_qkey_viol_cntr(dev->mdev, &qkey_viol_cntr);
288 props->qkey_viol_cntr = qkey_viol_cntr;
Achiad Shochat3f89a642015-12-23 18:47:21 +0200289
290 ndev = mlx5_ib_get_netdev(device, port_num);
291 if (!ndev)
Ilan Tayari095b0922017-05-14 16:04:30 +0300292 return 0;
Achiad Shochat3f89a642015-12-23 18:47:21 +0200293
Aviv Heller88621df2016-09-18 20:48:02 +0300294 if (mlx5_lag_is_active(dev->mdev)) {
295 rcu_read_lock();
296 upper = netdev_master_upper_dev_get_rcu(ndev);
297 if (upper) {
298 dev_put(ndev);
299 ndev = upper;
300 dev_hold(ndev);
301 }
302 rcu_read_unlock();
303 }
304
Achiad Shochat3f89a642015-12-23 18:47:21 +0200305 if (netif_running(ndev) && netif_carrier_ok(ndev)) {
306 props->state = IB_PORT_ACTIVE;
307 props->phys_state = 5;
308 }
309
310 ndev_ib_mtu = iboe_get_mtu(ndev->mtu);
311
312 dev_put(ndev);
313
314 props->active_mtu = min(props->max_mtu, ndev_ib_mtu);
Ilan Tayari095b0922017-05-14 16:04:30 +0300315 return 0;
Achiad Shochat3f89a642015-12-23 18:47:21 +0200316}
317
Ilan Tayari095b0922017-05-14 16:04:30 +0300318static int set_roce_addr(struct mlx5_ib_dev *dev, u8 port_num,
319 unsigned int index, const union ib_gid *gid,
320 const struct ib_gid_attr *attr)
Achiad Shochat3cca2602015-12-23 18:47:23 +0200321{
Ilan Tayari095b0922017-05-14 16:04:30 +0300322 enum ib_gid_type gid_type = IB_GID_TYPE_IB;
323 u8 roce_version = 0;
324 u8 roce_l3_type = 0;
325 bool vlan = false;
326 u8 mac[ETH_ALEN];
327 u16 vlan_id = 0;
Achiad Shochat3cca2602015-12-23 18:47:23 +0200328
Ilan Tayari095b0922017-05-14 16:04:30 +0300329 if (gid) {
330 gid_type = attr->gid_type;
331 ether_addr_copy(mac, attr->ndev->dev_addr);
Achiad Shochat3cca2602015-12-23 18:47:23 +0200332
Ilan Tayari095b0922017-05-14 16:04:30 +0300333 if (is_vlan_dev(attr->ndev)) {
334 vlan = true;
335 vlan_id = vlan_dev_vlan_id(attr->ndev);
336 }
Achiad Shochat3cca2602015-12-23 18:47:23 +0200337 }
338
Ilan Tayari095b0922017-05-14 16:04:30 +0300339 switch (gid_type) {
Achiad Shochat3cca2602015-12-23 18:47:23 +0200340 case IB_GID_TYPE_IB:
Ilan Tayari095b0922017-05-14 16:04:30 +0300341 roce_version = MLX5_ROCE_VERSION_1;
Achiad Shochat3cca2602015-12-23 18:47:23 +0200342 break;
343 case IB_GID_TYPE_ROCE_UDP_ENCAP:
Ilan Tayari095b0922017-05-14 16:04:30 +0300344 roce_version = MLX5_ROCE_VERSION_2;
345 if (ipv6_addr_v4mapped((void *)gid))
346 roce_l3_type = MLX5_ROCE_L3_TYPE_IPV4;
347 else
348 roce_l3_type = MLX5_ROCE_L3_TYPE_IPV6;
Achiad Shochat3cca2602015-12-23 18:47:23 +0200349 break;
350
351 default:
Ilan Tayari095b0922017-05-14 16:04:30 +0300352 mlx5_ib_warn(dev, "Unexpected GID type %u\n", gid_type);
Achiad Shochat3cca2602015-12-23 18:47:23 +0200353 }
354
Ilan Tayari095b0922017-05-14 16:04:30 +0300355 return mlx5_core_roce_gid_set(dev->mdev, index, roce_version,
356 roce_l3_type, gid->raw, mac, vlan,
357 vlan_id);
Achiad Shochat3cca2602015-12-23 18:47:23 +0200358}
359
360static int mlx5_ib_add_gid(struct ib_device *device, u8 port_num,
361 unsigned int index, const union ib_gid *gid,
362 const struct ib_gid_attr *attr,
363 __always_unused void **context)
364{
Ilan Tayari095b0922017-05-14 16:04:30 +0300365 return set_roce_addr(to_mdev(device), port_num, index, gid, attr);
Achiad Shochat3cca2602015-12-23 18:47:23 +0200366}
367
368static int mlx5_ib_del_gid(struct ib_device *device, u8 port_num,
369 unsigned int index, __always_unused void **context)
370{
Ilan Tayari095b0922017-05-14 16:04:30 +0300371 return set_roce_addr(to_mdev(device), port_num, index, NULL, NULL);
Achiad Shochat3cca2602015-12-23 18:47:23 +0200372}
373
Achiad Shochat2811ba52015-12-23 18:47:24 +0200374__be16 mlx5_get_roce_udp_sport(struct mlx5_ib_dev *dev, u8 port_num,
375 int index)
376{
377 struct ib_gid_attr attr;
378 union ib_gid gid;
379
380 if (ib_get_cached_gid(&dev->ib_dev, port_num, index, &gid, &attr))
381 return 0;
382
383 if (!attr.ndev)
384 return 0;
385
386 dev_put(attr.ndev);
387
388 if (attr.gid_type != IB_GID_TYPE_ROCE_UDP_ENCAP)
389 return 0;
390
391 return cpu_to_be16(MLX5_CAP_ROCE(dev->mdev, r_roce_min_src_udp_port));
392}
393
Majd Dibbinyed884512017-01-18 14:10:35 +0200394int mlx5_get_roce_gid_type(struct mlx5_ib_dev *dev, u8 port_num,
395 int index, enum ib_gid_type *gid_type)
396{
397 struct ib_gid_attr attr;
398 union ib_gid gid;
399 int ret;
400
401 ret = ib_get_cached_gid(&dev->ib_dev, port_num, index, &gid, &attr);
402 if (ret)
403 return ret;
404
405 if (!attr.ndev)
406 return -ENODEV;
407
408 dev_put(attr.ndev);
409
410 *gid_type = attr.gid_type;
411
412 return 0;
413}
414
Majd Dibbiny1b5daf12015-06-04 19:30:46 +0300415static int mlx5_use_mad_ifc(struct mlx5_ib_dev *dev)
416{
Noa Osherovich7fae6652016-09-12 19:16:23 +0300417 if (MLX5_CAP_GEN(dev->mdev, port_type) == MLX5_CAP_PORT_TYPE_IB)
418 return !MLX5_CAP_GEN(dev->mdev, ib_virt);
419 return 0;
Majd Dibbiny1b5daf12015-06-04 19:30:46 +0300420}
421
422enum {
423 MLX5_VPORT_ACCESS_METHOD_MAD,
424 MLX5_VPORT_ACCESS_METHOD_HCA,
425 MLX5_VPORT_ACCESS_METHOD_NIC,
426};
427
428static int mlx5_get_vport_access_method(struct ib_device *ibdev)
429{
430 if (mlx5_use_mad_ifc(to_mdev(ibdev)))
431 return MLX5_VPORT_ACCESS_METHOD_MAD;
432
Achiad Shochatebd61f62015-12-23 18:47:16 +0200433 if (mlx5_ib_port_link_layer(ibdev, 1) ==
Majd Dibbiny1b5daf12015-06-04 19:30:46 +0300434 IB_LINK_LAYER_ETHERNET)
435 return MLX5_VPORT_ACCESS_METHOD_NIC;
436
437 return MLX5_VPORT_ACCESS_METHOD_HCA;
438}
439
Eran Ben Elishada7525d2015-12-14 16:34:10 +0200440static void get_atomic_caps(struct mlx5_ib_dev *dev,
441 struct ib_device_attr *props)
442{
443 u8 tmp;
444 u8 atomic_operations = MLX5_CAP_ATOMIC(dev->mdev, atomic_operations);
445 u8 atomic_size_qp = MLX5_CAP_ATOMIC(dev->mdev, atomic_size_qp);
446 u8 atomic_req_8B_endianness_mode =
Or Gerlitzbd108382017-05-28 15:24:17 +0300447 MLX5_CAP_ATOMIC(dev->mdev, atomic_req_8B_endianness_mode);
Eran Ben Elishada7525d2015-12-14 16:34:10 +0200448
449 /* Check if HW supports 8 bytes standard atomic operations and capable
450 * of host endianness respond
451 */
452 tmp = MLX5_ATOMIC_OPS_CMP_SWAP | MLX5_ATOMIC_OPS_FETCH_ADD;
453 if (((atomic_operations & tmp) == tmp) &&
454 (atomic_size_qp & MLX5_ATOMIC_SIZE_QP_8BYTES) &&
455 (atomic_req_8B_endianness_mode)) {
456 props->atomic_cap = IB_ATOMIC_HCA;
457 } else {
458 props->atomic_cap = IB_ATOMIC_NONE;
459 }
460}
461
Majd Dibbiny1b5daf12015-06-04 19:30:46 +0300462static int mlx5_query_system_image_guid(struct ib_device *ibdev,
463 __be64 *sys_image_guid)
464{
465 struct mlx5_ib_dev *dev = to_mdev(ibdev);
466 struct mlx5_core_dev *mdev = dev->mdev;
467 u64 tmp;
468 int err;
469
470 switch (mlx5_get_vport_access_method(ibdev)) {
471 case MLX5_VPORT_ACCESS_METHOD_MAD:
472 return mlx5_query_mad_ifc_system_image_guid(ibdev,
473 sys_image_guid);
474
475 case MLX5_VPORT_ACCESS_METHOD_HCA:
476 err = mlx5_query_hca_vport_system_image_guid(mdev, &tmp);
Achiad Shochat3f89a642015-12-23 18:47:21 +0200477 break;
478
479 case MLX5_VPORT_ACCESS_METHOD_NIC:
480 err = mlx5_query_nic_vport_system_image_guid(mdev, &tmp);
481 break;
Majd Dibbiny1b5daf12015-06-04 19:30:46 +0300482
483 default:
484 return -EINVAL;
485 }
Achiad Shochat3f89a642015-12-23 18:47:21 +0200486
487 if (!err)
488 *sys_image_guid = cpu_to_be64(tmp);
489
490 return err;
491
Majd Dibbiny1b5daf12015-06-04 19:30:46 +0300492}
493
494static int mlx5_query_max_pkeys(struct ib_device *ibdev,
495 u16 *max_pkeys)
496{
497 struct mlx5_ib_dev *dev = to_mdev(ibdev);
498 struct mlx5_core_dev *mdev = dev->mdev;
499
500 switch (mlx5_get_vport_access_method(ibdev)) {
501 case MLX5_VPORT_ACCESS_METHOD_MAD:
502 return mlx5_query_mad_ifc_max_pkeys(ibdev, max_pkeys);
503
504 case MLX5_VPORT_ACCESS_METHOD_HCA:
505 case MLX5_VPORT_ACCESS_METHOD_NIC:
506 *max_pkeys = mlx5_to_sw_pkey_sz(MLX5_CAP_GEN(mdev,
507 pkey_table_size));
508 return 0;
509
510 default:
511 return -EINVAL;
512 }
513}
514
515static int mlx5_query_vendor_id(struct ib_device *ibdev,
516 u32 *vendor_id)
517{
518 struct mlx5_ib_dev *dev = to_mdev(ibdev);
519
520 switch (mlx5_get_vport_access_method(ibdev)) {
521 case MLX5_VPORT_ACCESS_METHOD_MAD:
522 return mlx5_query_mad_ifc_vendor_id(ibdev, vendor_id);
523
524 case MLX5_VPORT_ACCESS_METHOD_HCA:
525 case MLX5_VPORT_ACCESS_METHOD_NIC:
526 return mlx5_core_query_vendor_id(dev->mdev, vendor_id);
527
528 default:
529 return -EINVAL;
530 }
531}
532
533static int mlx5_query_node_guid(struct mlx5_ib_dev *dev,
534 __be64 *node_guid)
535{
536 u64 tmp;
537 int err;
538
539 switch (mlx5_get_vport_access_method(&dev->ib_dev)) {
540 case MLX5_VPORT_ACCESS_METHOD_MAD:
541 return mlx5_query_mad_ifc_node_guid(dev, node_guid);
542
543 case MLX5_VPORT_ACCESS_METHOD_HCA:
544 err = mlx5_query_hca_vport_node_guid(dev->mdev, &tmp);
Achiad Shochat3f89a642015-12-23 18:47:21 +0200545 break;
546
547 case MLX5_VPORT_ACCESS_METHOD_NIC:
548 err = mlx5_query_nic_vport_node_guid(dev->mdev, &tmp);
549 break;
Majd Dibbiny1b5daf12015-06-04 19:30:46 +0300550
551 default:
552 return -EINVAL;
553 }
Achiad Shochat3f89a642015-12-23 18:47:21 +0200554
555 if (!err)
556 *node_guid = cpu_to_be64(tmp);
557
558 return err;
Majd Dibbiny1b5daf12015-06-04 19:30:46 +0300559}
560
561struct mlx5_reg_node_desc {
Yuval Shaiabd99fde2016-08-25 10:57:07 -0700562 u8 desc[IB_DEVICE_NODE_DESC_MAX];
Majd Dibbiny1b5daf12015-06-04 19:30:46 +0300563};
564
565static int mlx5_query_node_desc(struct mlx5_ib_dev *dev, char *node_desc)
566{
567 struct mlx5_reg_node_desc in;
568
569 if (mlx5_use_mad_ifc(dev))
570 return mlx5_query_mad_ifc_node_desc(dev, node_desc);
571
572 memset(&in, 0, sizeof(in));
573
574 return mlx5_core_access_reg(dev->mdev, &in, sizeof(in), node_desc,
575 sizeof(struct mlx5_reg_node_desc),
576 MLX5_REG_NODE_DESC, 0, 0);
577}
578
Eli Cohene126ba92013-07-07 17:25:49 +0300579static int mlx5_ib_query_device(struct ib_device *ibdev,
Matan Barak2528e332015-06-11 16:35:25 +0300580 struct ib_device_attr *props,
581 struct ib_udata *uhw)
Eli Cohene126ba92013-07-07 17:25:49 +0300582{
583 struct mlx5_ib_dev *dev = to_mdev(ibdev);
Saeed Mahameed938fe832015-05-28 22:28:41 +0300584 struct mlx5_core_dev *mdev = dev->mdev;
Eli Cohene126ba92013-07-07 17:25:49 +0300585 int err = -ENOMEM;
Eli Cohen288c01b2016-10-27 16:36:45 +0300586 int max_sq_desc;
Eli Cohene126ba92013-07-07 17:25:49 +0300587 int max_rq_sg;
588 int max_sq_sg;
Sagi Grimberge0238a62015-07-21 14:40:12 +0300589 u64 min_page_size = 1ull << MLX5_CAP_GEN(mdev, log_pg_sz);
Bodong Wang402ca532016-06-17 15:02:20 +0300590 struct mlx5_ib_query_device_resp resp = {};
591 size_t resp_len;
592 u64 max_tso;
Eli Cohene126ba92013-07-07 17:25:49 +0300593
Bodong Wang402ca532016-06-17 15:02:20 +0300594 resp_len = sizeof(resp.comp_mask) + sizeof(resp.response_length);
595 if (uhw->outlen && uhw->outlen < resp_len)
596 return -EINVAL;
597 else
598 resp.response_length = resp_len;
599
600 if (uhw->inlen && !ib_is_udata_cleared(uhw, 0, uhw->inlen))
Matan Barak2528e332015-06-11 16:35:25 +0300601 return -EINVAL;
602
Eli Cohene126ba92013-07-07 17:25:49 +0300603 memset(props, 0, sizeof(*props));
Majd Dibbiny1b5daf12015-06-04 19:30:46 +0300604 err = mlx5_query_system_image_guid(ibdev,
605 &props->sys_image_guid);
606 if (err)
607 return err;
608
609 err = mlx5_query_max_pkeys(ibdev, &props->max_pkeys);
610 if (err)
611 return err;
612
613 err = mlx5_query_vendor_id(ibdev, &props->vendor_id);
614 if (err)
615 return err;
Eli Cohene126ba92013-07-07 17:25:49 +0300616
Jack Morgenstein9603b612014-07-28 23:30:22 +0300617 props->fw_ver = ((u64)fw_rev_maj(dev->mdev) << 32) |
618 (fw_rev_min(dev->mdev) << 16) |
619 fw_rev_sub(dev->mdev);
Eli Cohene126ba92013-07-07 17:25:49 +0300620 props->device_cap_flags = IB_DEVICE_CHANGE_PHY_PORT |
621 IB_DEVICE_PORT_ACTIVE_EVENT |
622 IB_DEVICE_SYS_IMAGE_GUID |
Eli Cohen1a4c3a32014-02-06 17:41:25 +0200623 IB_DEVICE_RC_RNR_NAK_GEN;
Saeed Mahameed938fe832015-05-28 22:28:41 +0300624
625 if (MLX5_CAP_GEN(mdev, pkv))
Eli Cohene126ba92013-07-07 17:25:49 +0300626 props->device_cap_flags |= IB_DEVICE_BAD_PKEY_CNTR;
Saeed Mahameed938fe832015-05-28 22:28:41 +0300627 if (MLX5_CAP_GEN(mdev, qkv))
Eli Cohene126ba92013-07-07 17:25:49 +0300628 props->device_cap_flags |= IB_DEVICE_BAD_QKEY_CNTR;
Saeed Mahameed938fe832015-05-28 22:28:41 +0300629 if (MLX5_CAP_GEN(mdev, apm))
Eli Cohene126ba92013-07-07 17:25:49 +0300630 props->device_cap_flags |= IB_DEVICE_AUTO_PATH_MIG;
Saeed Mahameed938fe832015-05-28 22:28:41 +0300631 if (MLX5_CAP_GEN(mdev, xrc))
Eli Cohene126ba92013-07-07 17:25:49 +0300632 props->device_cap_flags |= IB_DEVICE_XRC;
Matan Barakd2370e02016-02-29 18:05:30 +0200633 if (MLX5_CAP_GEN(mdev, imaicl)) {
634 props->device_cap_flags |= IB_DEVICE_MEM_WINDOW |
635 IB_DEVICE_MEM_WINDOW_TYPE_2B;
636 props->max_mw = 1 << MLX5_CAP_GEN(mdev, log_max_mkey);
Sagi Grimbergb005d312016-02-29 19:07:33 +0200637 /* We support 'Gappy' memory registration too */
638 props->device_cap_flags |= IB_DEVICE_SG_GAPS_REG;
Matan Barakd2370e02016-02-29 18:05:30 +0200639 }
Eli Cohene126ba92013-07-07 17:25:49 +0300640 props->device_cap_flags |= IB_DEVICE_MEM_MGT_EXTENSIONS;
Saeed Mahameed938fe832015-05-28 22:28:41 +0300641 if (MLX5_CAP_GEN(mdev, sho)) {
Sagi Grimberg2dea9092014-02-23 14:19:13 +0200642 props->device_cap_flags |= IB_DEVICE_SIGNATURE_HANDOVER;
643 /* At this stage no support for signature handover */
644 props->sig_prot_cap = IB_PROT_T10DIF_TYPE_1 |
645 IB_PROT_T10DIF_TYPE_2 |
646 IB_PROT_T10DIF_TYPE_3;
647 props->sig_guard_cap = IB_GUARD_T10DIF_CRC |
648 IB_GUARD_T10DIF_CSUM;
649 }
Saeed Mahameed938fe832015-05-28 22:28:41 +0300650 if (MLX5_CAP_GEN(mdev, block_lb_mc))
Eli Cohenf360d882014-04-02 00:10:16 +0300651 props->device_cap_flags |= IB_DEVICE_BLOCK_MULTICAST_LOOPBACK;
Eli Cohene126ba92013-07-07 17:25:49 +0300652
Bodong Wang402ca532016-06-17 15:02:20 +0300653 if (MLX5_CAP_GEN(dev->mdev, eth_net_offloads)) {
Noa Osheroviche8161332017-01-18 15:40:01 +0200654 if (MLX5_CAP_ETH(mdev, csum_cap)) {
655 /* Legacy bit to support old userspace libraries */
Bodong Wang88115fe2015-12-18 13:53:20 +0200656 props->device_cap_flags |= IB_DEVICE_RAW_IP_CSUM;
Noa Osheroviche8161332017-01-18 15:40:01 +0200657 props->raw_packet_caps |= IB_RAW_PACKET_CAP_IP_CSUM;
658 }
659
660 if (MLX5_CAP_ETH(dev->mdev, vlan_cap))
661 props->raw_packet_caps |=
662 IB_RAW_PACKET_CAP_CVLAN_STRIPPING;
Bodong Wang88115fe2015-12-18 13:53:20 +0200663
Bodong Wang402ca532016-06-17 15:02:20 +0300664 if (field_avail(typeof(resp), tso_caps, uhw->outlen)) {
665 max_tso = MLX5_CAP_ETH(mdev, max_lso_cap);
666 if (max_tso) {
667 resp.tso_caps.max_tso = 1 << max_tso;
668 resp.tso_caps.supported_qpts |=
669 1 << IB_QPT_RAW_PACKET;
670 resp.response_length += sizeof(resp.tso_caps);
671 }
672 }
Yishai Hadas31f69a82016-08-28 11:28:45 +0300673
674 if (field_avail(typeof(resp), rss_caps, uhw->outlen)) {
675 resp.rss_caps.rx_hash_function =
676 MLX5_RX_HASH_FUNC_TOEPLITZ;
677 resp.rss_caps.rx_hash_fields_mask =
678 MLX5_RX_HASH_SRC_IPV4 |
679 MLX5_RX_HASH_DST_IPV4 |
680 MLX5_RX_HASH_SRC_IPV6 |
681 MLX5_RX_HASH_DST_IPV6 |
682 MLX5_RX_HASH_SRC_PORT_TCP |
683 MLX5_RX_HASH_DST_PORT_TCP |
684 MLX5_RX_HASH_SRC_PORT_UDP |
685 MLX5_RX_HASH_DST_PORT_UDP;
686 resp.response_length += sizeof(resp.rss_caps);
687 }
688 } else {
689 if (field_avail(typeof(resp), tso_caps, uhw->outlen))
690 resp.response_length += sizeof(resp.tso_caps);
691 if (field_avail(typeof(resp), rss_caps, uhw->outlen))
692 resp.response_length += sizeof(resp.rss_caps);
Bodong Wang402ca532016-06-17 15:02:20 +0300693 }
694
Erez Shitritf0313962016-02-21 16:27:17 +0200695 if (MLX5_CAP_GEN(mdev, ipoib_basic_offloads)) {
696 props->device_cap_flags |= IB_DEVICE_UD_IP_CSUM;
697 props->device_cap_flags |= IB_DEVICE_UD_TSO;
698 }
699
Maor Gottlieb03404e82017-05-30 10:29:13 +0300700 if (MLX5_CAP_GEN(dev->mdev, rq_delay_drop) &&
701 MLX5_CAP_GEN(dev->mdev, general_notification_event))
702 props->raw_packet_caps |= IB_RAW_PACKET_CAP_DELAY_DROP;
703
Majd Dibbinycff5a0f2016-04-17 17:19:38 +0300704 if (MLX5_CAP_GEN(dev->mdev, eth_net_offloads) &&
Noa Osheroviche8161332017-01-18 15:40:01 +0200705 MLX5_CAP_ETH(dev->mdev, scatter_fcs)) {
706 /* Legacy bit to support old userspace libraries */
Majd Dibbinycff5a0f2016-04-17 17:19:38 +0300707 props->device_cap_flags |= IB_DEVICE_RAW_SCATTER_FCS;
Noa Osheroviche8161332017-01-18 15:40:01 +0200708 props->raw_packet_caps |= IB_RAW_PACKET_CAP_SCATTER_FCS;
709 }
Majd Dibbinycff5a0f2016-04-17 17:19:38 +0300710
Maor Gottliebda6d6ba32016-06-04 15:15:28 +0300711 if (mlx5_get_flow_namespace(dev->mdev, MLX5_FLOW_NAMESPACE_BYPASS))
712 props->device_cap_flags |= IB_DEVICE_MANAGED_FLOW_STEERING;
713
Majd Dibbiny1b5daf12015-06-04 19:30:46 +0300714 props->vendor_part_id = mdev->pdev->device;
715 props->hw_ver = mdev->pdev->revision;
Eli Cohene126ba92013-07-07 17:25:49 +0300716
717 props->max_mr_size = ~0ull;
Sagi Grimberge0238a62015-07-21 14:40:12 +0300718 props->page_size_cap = ~(min_page_size - 1);
Saeed Mahameed938fe832015-05-28 22:28:41 +0300719 props->max_qp = 1 << MLX5_CAP_GEN(mdev, log_max_qp);
720 props->max_qp_wr = 1 << MLX5_CAP_GEN(mdev, log_max_qp_sz);
721 max_rq_sg = MLX5_CAP_GEN(mdev, max_wqe_sz_rq) /
722 sizeof(struct mlx5_wqe_data_seg);
Eli Cohen288c01b2016-10-27 16:36:45 +0300723 max_sq_desc = min_t(int, MLX5_CAP_GEN(mdev, max_wqe_sz_sq), 512);
724 max_sq_sg = (max_sq_desc - sizeof(struct mlx5_wqe_ctrl_seg) -
725 sizeof(struct mlx5_wqe_raddr_seg)) /
726 sizeof(struct mlx5_wqe_data_seg);
Eli Cohene126ba92013-07-07 17:25:49 +0300727 props->max_sge = min(max_rq_sg, max_sq_sg);
Sagi Grimberg986ef952016-03-31 19:03:25 +0300728 props->max_sge_rd = MLX5_MAX_SGE_RD;
Saeed Mahameed938fe832015-05-28 22:28:41 +0300729 props->max_cq = 1 << MLX5_CAP_GEN(mdev, log_max_cq);
Leon Romanovsky9f177682016-01-14 08:11:40 +0200730 props->max_cqe = (1 << MLX5_CAP_GEN(mdev, log_max_cq_sz)) - 1;
Saeed Mahameed938fe832015-05-28 22:28:41 +0300731 props->max_mr = 1 << MLX5_CAP_GEN(mdev, log_max_mkey);
732 props->max_pd = 1 << MLX5_CAP_GEN(mdev, log_max_pd);
733 props->max_qp_rd_atom = 1 << MLX5_CAP_GEN(mdev, log_max_ra_req_qp);
734 props->max_qp_init_rd_atom = 1 << MLX5_CAP_GEN(mdev, log_max_ra_res_qp);
735 props->max_srq = 1 << MLX5_CAP_GEN(mdev, log_max_srq);
736 props->max_srq_wr = (1 << MLX5_CAP_GEN(mdev, log_max_srq_sz)) - 1;
737 props->local_ca_ack_delay = MLX5_CAP_GEN(mdev, local_ca_ack_delay);
Eli Cohene126ba92013-07-07 17:25:49 +0300738 props->max_res_rd_atom = props->max_qp_rd_atom * props->max_qp;
Eli Cohene126ba92013-07-07 17:25:49 +0300739 props->max_srq_sge = max_rq_sg - 1;
Sagi Grimberg911f4332016-03-03 13:37:51 +0200740 props->max_fast_reg_page_list_len =
741 1 << MLX5_CAP_GEN(mdev, log_max_klm_list_size);
Eran Ben Elishada7525d2015-12-14 16:34:10 +0200742 get_atomic_caps(dev, props);
Eli Cohen81bea282013-09-11 16:35:30 +0300743 props->masked_atomic_cap = IB_ATOMIC_NONE;
Saeed Mahameed938fe832015-05-28 22:28:41 +0300744 props->max_mcast_grp = 1 << MLX5_CAP_GEN(mdev, log_max_mcg);
745 props->max_mcast_qp_attach = MLX5_CAP_GEN(mdev, max_qp_mcg);
Eli Cohene126ba92013-07-07 17:25:49 +0300746 props->max_total_mcast_qp_attach = props->max_mcast_qp_attach *
747 props->max_mcast_grp;
748 props->max_map_per_fmr = INT_MAX; /* no limit in ConnectIB */
Maor Gottlieb86695a62016-10-27 16:36:38 +0300749 props->max_ah = INT_MAX;
Matan Barak7c60bcb2015-12-15 20:30:11 +0200750 props->hca_core_clock = MLX5_CAP_GEN(mdev, device_frequency_khz);
751 props->timestamp_mask = 0x7FFFFFFFFFFFFFFFULL;
Eli Cohene126ba92013-07-07 17:25:49 +0300752
Haggai Eran8cdd3122014-12-11 17:04:20 +0200753#ifdef CONFIG_INFINIBAND_ON_DEMAND_PAGING
Saeed Mahameed938fe832015-05-28 22:28:41 +0300754 if (MLX5_CAP_GEN(mdev, pg))
Haggai Eran8cdd3122014-12-11 17:04:20 +0200755 props->device_cap_flags |= IB_DEVICE_ON_DEMAND_PAGING;
756 props->odp_caps = dev->odp_caps;
757#endif
758
Leon Romanovsky051f2632015-12-20 12:16:11 +0200759 if (MLX5_CAP_GEN(mdev, cd))
760 props->device_cap_flags |= IB_DEVICE_CROSS_CHANNEL;
761
Eli Coheneff901d2016-03-11 22:58:42 +0200762 if (!mlx5_core_is_pf(mdev))
763 props->device_cap_flags |= IB_DEVICE_VIRTUAL_FUNCTION;
764
Yishai Hadas31f69a82016-08-28 11:28:45 +0300765 if (mlx5_ib_port_link_layer(ibdev, 1) ==
766 IB_LINK_LAYER_ETHERNET) {
767 props->rss_caps.max_rwq_indirection_tables =
768 1 << MLX5_CAP_GEN(dev->mdev, log_max_rqt);
769 props->rss_caps.max_rwq_indirection_table_size =
770 1 << MLX5_CAP_GEN(dev->mdev, log_max_rqt_size);
771 props->rss_caps.supported_qpts = 1 << IB_QPT_RAW_PACKET;
772 props->max_wq_type_rq =
773 1 << MLX5_CAP_GEN(dev->mdev, log_max_rq);
774 }
775
Bodong Wang7e43a2a2016-10-31 12:16:44 +0200776 if (field_avail(typeof(resp), cqe_comp_caps, uhw->outlen)) {
777 resp.cqe_comp_caps.max_num =
778 MLX5_CAP_GEN(dev->mdev, cqe_compression) ?
779 MLX5_CAP_GEN(dev->mdev, cqe_compression_max_num) : 0;
780 resp.cqe_comp_caps.supported_format =
781 MLX5_IB_CQE_RES_FORMAT_HASH |
782 MLX5_IB_CQE_RES_FORMAT_CSUM;
783 resp.response_length += sizeof(resp.cqe_comp_caps);
784 }
785
Bodong Wangd9491672016-12-01 13:43:13 +0200786 if (field_avail(typeof(resp), packet_pacing_caps, uhw->outlen)) {
787 if (MLX5_CAP_QOS(mdev, packet_pacing) &&
788 MLX5_CAP_GEN(mdev, qos)) {
789 resp.packet_pacing_caps.qp_rate_limit_max =
790 MLX5_CAP_QOS(mdev, packet_pacing_max_rate);
791 resp.packet_pacing_caps.qp_rate_limit_min =
792 MLX5_CAP_QOS(mdev, packet_pacing_min_rate);
793 resp.packet_pacing_caps.supported_qpts |=
794 1 << IB_QPT_RAW_PACKET;
795 }
796 resp.response_length += sizeof(resp.packet_pacing_caps);
797 }
798
Leon Romanovsky9f885202017-01-02 11:37:39 +0200799 if (field_avail(typeof(resp), mlx5_ib_support_multi_pkt_send_wqes,
800 uhw->outlen)) {
801 resp.mlx5_ib_support_multi_pkt_send_wqes =
802 MLX5_CAP_ETH(mdev, multi_pkt_send_wqe);
803 resp.response_length +=
804 sizeof(resp.mlx5_ib_support_multi_pkt_send_wqes);
805 }
806
807 if (field_avail(typeof(resp), reserved, uhw->outlen))
808 resp.response_length += sizeof(resp.reserved);
809
Bodong Wang402ca532016-06-17 15:02:20 +0300810 if (uhw->outlen) {
811 err = ib_copy_to_udata(uhw, &resp, resp.response_length);
812
813 if (err)
814 return err;
815 }
816
Majd Dibbiny1b5daf12015-06-04 19:30:46 +0300817 return 0;
818}
Eli Cohene126ba92013-07-07 17:25:49 +0300819
Majd Dibbiny1b5daf12015-06-04 19:30:46 +0300820enum mlx5_ib_width {
821 MLX5_IB_WIDTH_1X = 1 << 0,
822 MLX5_IB_WIDTH_2X = 1 << 1,
823 MLX5_IB_WIDTH_4X = 1 << 2,
824 MLX5_IB_WIDTH_8X = 1 << 3,
825 MLX5_IB_WIDTH_12X = 1 << 4
826};
827
828static int translate_active_width(struct ib_device *ibdev, u8 active_width,
829 u8 *ib_width)
830{
831 struct mlx5_ib_dev *dev = to_mdev(ibdev);
832 int err = 0;
833
834 if (active_width & MLX5_IB_WIDTH_1X) {
835 *ib_width = IB_WIDTH_1X;
836 } else if (active_width & MLX5_IB_WIDTH_2X) {
837 mlx5_ib_dbg(dev, "active_width %d is not supported by IB spec\n",
838 (int)active_width);
839 err = -EINVAL;
840 } else if (active_width & MLX5_IB_WIDTH_4X) {
841 *ib_width = IB_WIDTH_4X;
842 } else if (active_width & MLX5_IB_WIDTH_8X) {
843 *ib_width = IB_WIDTH_8X;
844 } else if (active_width & MLX5_IB_WIDTH_12X) {
845 *ib_width = IB_WIDTH_12X;
846 } else {
847 mlx5_ib_dbg(dev, "Invalid active_width %d\n",
848 (int)active_width);
849 err = -EINVAL;
850 }
851
852 return err;
853}
854
855static int mlx5_mtu_to_ib_mtu(int mtu)
856{
857 switch (mtu) {
858 case 256: return 1;
859 case 512: return 2;
860 case 1024: return 3;
861 case 2048: return 4;
862 case 4096: return 5;
863 default:
864 pr_warn("invalid mtu\n");
865 return -1;
866 }
867}
868
869enum ib_max_vl_num {
870 __IB_MAX_VL_0 = 1,
871 __IB_MAX_VL_0_1 = 2,
872 __IB_MAX_VL_0_3 = 3,
873 __IB_MAX_VL_0_7 = 4,
874 __IB_MAX_VL_0_14 = 5,
875};
876
877enum mlx5_vl_hw_cap {
878 MLX5_VL_HW_0 = 1,
879 MLX5_VL_HW_0_1 = 2,
880 MLX5_VL_HW_0_2 = 3,
881 MLX5_VL_HW_0_3 = 4,
882 MLX5_VL_HW_0_4 = 5,
883 MLX5_VL_HW_0_5 = 6,
884 MLX5_VL_HW_0_6 = 7,
885 MLX5_VL_HW_0_7 = 8,
886 MLX5_VL_HW_0_14 = 15
887};
888
889static int translate_max_vl_num(struct ib_device *ibdev, u8 vl_hw_cap,
890 u8 *max_vl_num)
891{
892 switch (vl_hw_cap) {
893 case MLX5_VL_HW_0:
894 *max_vl_num = __IB_MAX_VL_0;
895 break;
896 case MLX5_VL_HW_0_1:
897 *max_vl_num = __IB_MAX_VL_0_1;
898 break;
899 case MLX5_VL_HW_0_3:
900 *max_vl_num = __IB_MAX_VL_0_3;
901 break;
902 case MLX5_VL_HW_0_7:
903 *max_vl_num = __IB_MAX_VL_0_7;
904 break;
905 case MLX5_VL_HW_0_14:
906 *max_vl_num = __IB_MAX_VL_0_14;
907 break;
908
909 default:
910 return -EINVAL;
911 }
912
913 return 0;
914}
915
916static int mlx5_query_hca_port(struct ib_device *ibdev, u8 port,
917 struct ib_port_attr *props)
918{
919 struct mlx5_ib_dev *dev = to_mdev(ibdev);
920 struct mlx5_core_dev *mdev = dev->mdev;
921 struct mlx5_hca_vport_context *rep;
Saeed Mahameed046339e2016-04-22 00:33:03 +0300922 u16 max_mtu;
923 u16 oper_mtu;
Majd Dibbiny1b5daf12015-06-04 19:30:46 +0300924 int err;
925 u8 ib_link_width_oper;
926 u8 vl_hw_cap;
927
928 rep = kzalloc(sizeof(*rep), GFP_KERNEL);
929 if (!rep) {
930 err = -ENOMEM;
931 goto out;
932 }
933
Or Gerlitzc4550c62017-01-24 13:02:39 +0200934 /* props being zeroed by the caller, avoid zeroing it here */
Majd Dibbiny1b5daf12015-06-04 19:30:46 +0300935
936 err = mlx5_query_hca_vport_context(mdev, 0, port, 0, rep);
937 if (err)
938 goto out;
939
940 props->lid = rep->lid;
941 props->lmc = rep->lmc;
942 props->sm_lid = rep->sm_lid;
943 props->sm_sl = rep->sm_sl;
944 props->state = rep->vport_state;
945 props->phys_state = rep->port_physical_state;
946 props->port_cap_flags = rep->cap_mask1;
947 props->gid_tbl_len = mlx5_get_gid_table_len(MLX5_CAP_GEN(mdev, gid_table_size));
948 props->max_msg_sz = 1 << MLX5_CAP_GEN(mdev, log_max_msg);
949 props->pkey_tbl_len = mlx5_to_sw_pkey_sz(MLX5_CAP_GEN(mdev, pkey_table_size));
950 props->bad_pkey_cntr = rep->pkey_violation_counter;
951 props->qkey_viol_cntr = rep->qkey_violation_counter;
952 props->subnet_timeout = rep->subnet_timeout;
953 props->init_type_reply = rep->init_type_reply;
Eli Coheneff901d2016-03-11 22:58:42 +0200954 props->grh_required = rep->grh_required;
Majd Dibbiny1b5daf12015-06-04 19:30:46 +0300955
956 err = mlx5_query_port_link_width_oper(mdev, &ib_link_width_oper, port);
957 if (err)
958 goto out;
959
960 err = translate_active_width(ibdev, ib_link_width_oper,
961 &props->active_width);
962 if (err)
963 goto out;
Noa Osherovichd5beb7f2016-06-02 10:47:53 +0300964 err = mlx5_query_port_ib_proto_oper(mdev, &props->active_speed, port);
Majd Dibbiny1b5daf12015-06-04 19:30:46 +0300965 if (err)
966 goto out;
967
Saeed Mahameedfacc9692015-06-11 14:47:27 +0300968 mlx5_query_port_max_mtu(mdev, &max_mtu, port);
Majd Dibbiny1b5daf12015-06-04 19:30:46 +0300969
970 props->max_mtu = mlx5_mtu_to_ib_mtu(max_mtu);
971
Saeed Mahameedfacc9692015-06-11 14:47:27 +0300972 mlx5_query_port_oper_mtu(mdev, &oper_mtu, port);
Majd Dibbiny1b5daf12015-06-04 19:30:46 +0300973
974 props->active_mtu = mlx5_mtu_to_ib_mtu(oper_mtu);
975
976 err = mlx5_query_port_vl_hw_cap(mdev, &vl_hw_cap, port);
977 if (err)
978 goto out;
979
980 err = translate_max_vl_num(ibdev, vl_hw_cap,
981 &props->max_vl_num);
982out:
983 kfree(rep);
Eli Cohene126ba92013-07-07 17:25:49 +0300984 return err;
985}
986
987int mlx5_ib_query_port(struct ib_device *ibdev, u8 port,
988 struct ib_port_attr *props)
989{
Ilan Tayari095b0922017-05-14 16:04:30 +0300990 unsigned int count;
991 int ret;
992
Majd Dibbiny1b5daf12015-06-04 19:30:46 +0300993 switch (mlx5_get_vport_access_method(ibdev)) {
994 case MLX5_VPORT_ACCESS_METHOD_MAD:
Ilan Tayari095b0922017-05-14 16:04:30 +0300995 ret = mlx5_query_mad_ifc_port(ibdev, port, props);
996 break;
Eli Cohene126ba92013-07-07 17:25:49 +0300997
Majd Dibbiny1b5daf12015-06-04 19:30:46 +0300998 case MLX5_VPORT_ACCESS_METHOD_HCA:
Ilan Tayari095b0922017-05-14 16:04:30 +0300999 ret = mlx5_query_hca_port(ibdev, port, props);
1000 break;
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03001001
Achiad Shochat3f89a642015-12-23 18:47:21 +02001002 case MLX5_VPORT_ACCESS_METHOD_NIC:
Ilan Tayari095b0922017-05-14 16:04:30 +03001003 ret = mlx5_query_port_roce(ibdev, port, props);
1004 break;
Achiad Shochat3f89a642015-12-23 18:47:21 +02001005
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03001006 default:
Ilan Tayari095b0922017-05-14 16:04:30 +03001007 ret = -EINVAL;
Eli Cohene126ba92013-07-07 17:25:49 +03001008 }
Ilan Tayari095b0922017-05-14 16:04:30 +03001009
1010 if (!ret && props) {
1011 count = mlx5_core_reserved_gids_count(to_mdev(ibdev)->mdev);
1012 props->gid_tbl_len -= count;
1013 }
1014 return ret;
Eli Cohene126ba92013-07-07 17:25:49 +03001015}
1016
1017static int mlx5_ib_query_gid(struct ib_device *ibdev, u8 port, int index,
1018 union ib_gid *gid)
1019{
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03001020 struct mlx5_ib_dev *dev = to_mdev(ibdev);
1021 struct mlx5_core_dev *mdev = dev->mdev;
Eli Cohene126ba92013-07-07 17:25:49 +03001022
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03001023 switch (mlx5_get_vport_access_method(ibdev)) {
1024 case MLX5_VPORT_ACCESS_METHOD_MAD:
1025 return mlx5_query_mad_ifc_gids(ibdev, port, index, gid);
Eli Cohene126ba92013-07-07 17:25:49 +03001026
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03001027 case MLX5_VPORT_ACCESS_METHOD_HCA:
1028 return mlx5_query_hca_vport_gid(mdev, 0, port, 0, index, gid);
Eli Cohene126ba92013-07-07 17:25:49 +03001029
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03001030 default:
1031 return -EINVAL;
1032 }
Eli Cohene126ba92013-07-07 17:25:49 +03001033
Eli Cohene126ba92013-07-07 17:25:49 +03001034}
1035
1036static int mlx5_ib_query_pkey(struct ib_device *ibdev, u8 port, u16 index,
1037 u16 *pkey)
1038{
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03001039 struct mlx5_ib_dev *dev = to_mdev(ibdev);
1040 struct mlx5_core_dev *mdev = dev->mdev;
Eli Cohene126ba92013-07-07 17:25:49 +03001041
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03001042 switch (mlx5_get_vport_access_method(ibdev)) {
1043 case MLX5_VPORT_ACCESS_METHOD_MAD:
1044 return mlx5_query_mad_ifc_pkey(ibdev, port, index, pkey);
Eli Cohene126ba92013-07-07 17:25:49 +03001045
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03001046 case MLX5_VPORT_ACCESS_METHOD_HCA:
1047 case MLX5_VPORT_ACCESS_METHOD_NIC:
1048 return mlx5_query_hca_vport_pkey(mdev, 0, port, 0, index,
1049 pkey);
1050 default:
1051 return -EINVAL;
1052 }
Eli Cohene126ba92013-07-07 17:25:49 +03001053}
1054
Eli Cohene126ba92013-07-07 17:25:49 +03001055static int mlx5_ib_modify_device(struct ib_device *ibdev, int mask,
1056 struct ib_device_modify *props)
1057{
1058 struct mlx5_ib_dev *dev = to_mdev(ibdev);
1059 struct mlx5_reg_node_desc in;
1060 struct mlx5_reg_node_desc out;
1061 int err;
1062
1063 if (mask & ~IB_DEVICE_MODIFY_NODE_DESC)
1064 return -EOPNOTSUPP;
1065
1066 if (!(mask & IB_DEVICE_MODIFY_NODE_DESC))
1067 return 0;
1068
1069 /*
1070 * If possible, pass node desc to FW, so it can generate
1071 * a 144 trap. If cmd fails, just ignore.
1072 */
Yuval Shaiabd99fde2016-08-25 10:57:07 -07001073 memcpy(&in, props->node_desc, IB_DEVICE_NODE_DESC_MAX);
Jack Morgenstein9603b612014-07-28 23:30:22 +03001074 err = mlx5_core_access_reg(dev->mdev, &in, sizeof(in), &out,
Eli Cohene126ba92013-07-07 17:25:49 +03001075 sizeof(out), MLX5_REG_NODE_DESC, 0, 1);
1076 if (err)
1077 return err;
1078
Yuval Shaiabd99fde2016-08-25 10:57:07 -07001079 memcpy(ibdev->node_desc, props->node_desc, IB_DEVICE_NODE_DESC_MAX);
Eli Cohene126ba92013-07-07 17:25:49 +03001080
1081 return err;
1082}
1083
Eli Cohencdbe33d2017-02-14 07:25:38 +02001084static int set_port_caps_atomic(struct mlx5_ib_dev *dev, u8 port_num, u32 mask,
1085 u32 value)
1086{
1087 struct mlx5_hca_vport_context ctx = {};
1088 int err;
1089
1090 err = mlx5_query_hca_vport_context(dev->mdev, 0,
1091 port_num, 0, &ctx);
1092 if (err)
1093 return err;
1094
1095 if (~ctx.cap_mask1_perm & mask) {
1096 mlx5_ib_warn(dev, "trying to change bitmask 0x%X but change supported 0x%X\n",
1097 mask, ctx.cap_mask1_perm);
1098 return -EINVAL;
1099 }
1100
1101 ctx.cap_mask1 = value;
1102 ctx.cap_mask1_perm = mask;
1103 err = mlx5_core_modify_hca_vport_context(dev->mdev, 0,
1104 port_num, 0, &ctx);
1105
1106 return err;
1107}
1108
Eli Cohene126ba92013-07-07 17:25:49 +03001109static int mlx5_ib_modify_port(struct ib_device *ibdev, u8 port, int mask,
1110 struct ib_port_modify *props)
1111{
1112 struct mlx5_ib_dev *dev = to_mdev(ibdev);
1113 struct ib_port_attr attr;
1114 u32 tmp;
1115 int err;
Eli Cohencdbe33d2017-02-14 07:25:38 +02001116 u32 change_mask;
1117 u32 value;
1118 bool is_ib = (mlx5_ib_port_link_layer(ibdev, port) ==
1119 IB_LINK_LAYER_INFINIBAND);
1120
1121 if (MLX5_CAP_GEN(dev->mdev, ib_virt) && is_ib) {
1122 change_mask = props->clr_port_cap_mask | props->set_port_cap_mask;
1123 value = ~props->clr_port_cap_mask | props->set_port_cap_mask;
1124 return set_port_caps_atomic(dev, port, change_mask, value);
1125 }
Eli Cohene126ba92013-07-07 17:25:49 +03001126
1127 mutex_lock(&dev->cap_mask_mutex);
1128
Or Gerlitzc4550c62017-01-24 13:02:39 +02001129 err = ib_query_port(ibdev, port, &attr);
Eli Cohene126ba92013-07-07 17:25:49 +03001130 if (err)
1131 goto out;
1132
1133 tmp = (attr.port_cap_flags | props->set_port_cap_mask) &
1134 ~props->clr_port_cap_mask;
1135
Jack Morgenstein9603b612014-07-28 23:30:22 +03001136 err = mlx5_set_port_caps(dev->mdev, port, tmp);
Eli Cohene126ba92013-07-07 17:25:49 +03001137
1138out:
1139 mutex_unlock(&dev->cap_mask_mutex);
1140 return err;
1141}
1142
Eli Cohen30aa60b2017-01-03 23:55:27 +02001143static void print_lib_caps(struct mlx5_ib_dev *dev, u64 caps)
1144{
1145 mlx5_ib_dbg(dev, "MLX5_LIB_CAP_4K_UAR = %s\n",
1146 caps & MLX5_LIB_CAP_4K_UAR ? "y" : "n");
1147}
1148
Eli Cohenb037c292017-01-03 23:55:26 +02001149static int calc_total_bfregs(struct mlx5_ib_dev *dev, bool lib_uar_4k,
1150 struct mlx5_ib_alloc_ucontext_req_v2 *req,
1151 u32 *num_sys_pages)
1152{
1153 int uars_per_sys_page;
1154 int bfregs_per_sys_page;
1155 int ref_bfregs = req->total_num_bfregs;
1156
1157 if (req->total_num_bfregs == 0)
1158 return -EINVAL;
1159
1160 BUILD_BUG_ON(MLX5_MAX_BFREGS % MLX5_NON_FP_BFREGS_IN_PAGE);
1161 BUILD_BUG_ON(MLX5_MAX_BFREGS < MLX5_NON_FP_BFREGS_IN_PAGE);
1162
1163 if (req->total_num_bfregs > MLX5_MAX_BFREGS)
1164 return -ENOMEM;
1165
1166 uars_per_sys_page = get_uars_per_sys_page(dev, lib_uar_4k);
1167 bfregs_per_sys_page = uars_per_sys_page * MLX5_NON_FP_BFREGS_PER_UAR;
1168 req->total_num_bfregs = ALIGN(req->total_num_bfregs, bfregs_per_sys_page);
1169 *num_sys_pages = req->total_num_bfregs / bfregs_per_sys_page;
1170
1171 if (req->num_low_latency_bfregs > req->total_num_bfregs - 1)
1172 return -EINVAL;
1173
1174 mlx5_ib_dbg(dev, "uar_4k: fw support %s, lib support %s, user requested %d bfregs, alloated %d, using %d sys pages\n",
1175 MLX5_CAP_GEN(dev->mdev, uar_4k) ? "yes" : "no",
1176 lib_uar_4k ? "yes" : "no", ref_bfregs,
1177 req->total_num_bfregs, *num_sys_pages);
1178
1179 return 0;
1180}
1181
1182static int allocate_uars(struct mlx5_ib_dev *dev, struct mlx5_ib_ucontext *context)
1183{
1184 struct mlx5_bfreg_info *bfregi;
1185 int err;
1186 int i;
1187
1188 bfregi = &context->bfregi;
1189 for (i = 0; i < bfregi->num_sys_pages; i++) {
1190 err = mlx5_cmd_alloc_uar(dev->mdev, &bfregi->sys_pages[i]);
1191 if (err)
1192 goto error;
1193
1194 mlx5_ib_dbg(dev, "allocated uar %d\n", bfregi->sys_pages[i]);
1195 }
1196 return 0;
1197
1198error:
1199 for (--i; i >= 0; i--)
1200 if (mlx5_cmd_free_uar(dev->mdev, bfregi->sys_pages[i]))
1201 mlx5_ib_warn(dev, "failed to free uar %d\n", i);
1202
1203 return err;
1204}
1205
1206static int deallocate_uars(struct mlx5_ib_dev *dev, struct mlx5_ib_ucontext *context)
1207{
1208 struct mlx5_bfreg_info *bfregi;
1209 int err;
1210 int i;
1211
1212 bfregi = &context->bfregi;
1213 for (i = 0; i < bfregi->num_sys_pages; i++) {
1214 err = mlx5_cmd_free_uar(dev->mdev, bfregi->sys_pages[i]);
1215 if (err) {
1216 mlx5_ib_warn(dev, "failed to free uar %d\n", i);
1217 return err;
1218 }
1219 }
1220 return 0;
1221}
1222
Huy Nguyenc85023e2017-05-30 09:42:54 +03001223static int mlx5_ib_alloc_transport_domain(struct mlx5_ib_dev *dev, u32 *tdn)
1224{
1225 int err;
1226
1227 err = mlx5_core_alloc_transport_domain(dev->mdev, tdn);
1228 if (err)
1229 return err;
1230
1231 if ((MLX5_CAP_GEN(dev->mdev, port_type) != MLX5_CAP_PORT_TYPE_ETH) ||
1232 !MLX5_CAP_GEN(dev->mdev, disable_local_lb))
1233 return err;
1234
1235 mutex_lock(&dev->lb_mutex);
1236 dev->user_td++;
1237
1238 if (dev->user_td == 2)
1239 err = mlx5_nic_vport_update_local_lb(dev->mdev, true);
1240
1241 mutex_unlock(&dev->lb_mutex);
1242 return err;
1243}
1244
1245static void mlx5_ib_dealloc_transport_domain(struct mlx5_ib_dev *dev, u32 tdn)
1246{
1247 mlx5_core_dealloc_transport_domain(dev->mdev, tdn);
1248
1249 if ((MLX5_CAP_GEN(dev->mdev, port_type) != MLX5_CAP_PORT_TYPE_ETH) ||
1250 !MLX5_CAP_GEN(dev->mdev, disable_local_lb))
1251 return;
1252
1253 mutex_lock(&dev->lb_mutex);
1254 dev->user_td--;
1255
1256 if (dev->user_td < 2)
1257 mlx5_nic_vport_update_local_lb(dev->mdev, false);
1258
1259 mutex_unlock(&dev->lb_mutex);
1260}
1261
Eli Cohene126ba92013-07-07 17:25:49 +03001262static struct ib_ucontext *mlx5_ib_alloc_ucontext(struct ib_device *ibdev,
1263 struct ib_udata *udata)
1264{
1265 struct mlx5_ib_dev *dev = to_mdev(ibdev);
Matan Barakb368d7c2015-12-15 20:30:12 +02001266 struct mlx5_ib_alloc_ucontext_req_v2 req = {};
1267 struct mlx5_ib_alloc_ucontext_resp resp = {};
Eli Cohene126ba92013-07-07 17:25:49 +03001268 struct mlx5_ib_ucontext *context;
Eli Cohen2f5ff262017-01-03 23:55:21 +02001269 struct mlx5_bfreg_info *bfregi;
Eli Cohen78c0f982014-01-30 13:49:48 +02001270 int ver;
Eli Cohene126ba92013-07-07 17:25:49 +03001271 int err;
Jack Morgensteinf241e742014-07-28 23:30:23 +03001272 size_t reqlen;
Majd Dibbinya168a41c2016-01-28 17:51:47 +02001273 size_t min_req_v2 = offsetof(struct mlx5_ib_alloc_ucontext_req_v2,
1274 max_cqe_version);
Eli Cohenb037c292017-01-03 23:55:26 +02001275 bool lib_uar_4k;
Eli Cohene126ba92013-07-07 17:25:49 +03001276
1277 if (!dev->ib_active)
1278 return ERR_PTR(-EAGAIN);
1279
Haggai Abramovskydfbee852016-01-14 19:12:56 +02001280 if (udata->inlen < sizeof(struct ib_uverbs_cmd_hdr))
1281 return ERR_PTR(-EINVAL);
1282
Eli Cohen78c0f982014-01-30 13:49:48 +02001283 reqlen = udata->inlen - sizeof(struct ib_uverbs_cmd_hdr);
1284 if (reqlen == sizeof(struct mlx5_ib_alloc_ucontext_req))
1285 ver = 0;
Majd Dibbinya168a41c2016-01-28 17:51:47 +02001286 else if (reqlen >= min_req_v2)
Eli Cohen78c0f982014-01-30 13:49:48 +02001287 ver = 2;
1288 else
1289 return ERR_PTR(-EINVAL);
1290
Matan Barakb368d7c2015-12-15 20:30:12 +02001291 err = ib_copy_from_udata(&req, udata, min(reqlen, sizeof(req)));
Eli Cohene126ba92013-07-07 17:25:49 +03001292 if (err)
1293 return ERR_PTR(err);
1294
Matan Barakb368d7c2015-12-15 20:30:12 +02001295 if (req.flags)
Eli Cohen78c0f982014-01-30 13:49:48 +02001296 return ERR_PTR(-EINVAL);
1297
Haggai Abramovskyf72300c2016-01-14 19:12:58 +02001298 if (req.comp_mask || req.reserved0 || req.reserved1 || req.reserved2)
Matan Barakb368d7c2015-12-15 20:30:12 +02001299 return ERR_PTR(-EOPNOTSUPP);
1300
Eli Cohen2f5ff262017-01-03 23:55:21 +02001301 req.total_num_bfregs = ALIGN(req.total_num_bfregs,
1302 MLX5_NON_FP_BFREGS_PER_UAR);
1303 if (req.num_low_latency_bfregs > req.total_num_bfregs - 1)
Eli Cohene126ba92013-07-07 17:25:49 +03001304 return ERR_PTR(-EINVAL);
1305
Saeed Mahameed938fe832015-05-28 22:28:41 +03001306 resp.qp_tab_size = 1 << MLX5_CAP_GEN(dev->mdev, log_max_qp);
Noa Osherovich2cc6ad52016-06-04 15:15:33 +03001307 if (mlx5_core_is_pf(dev->mdev) && MLX5_CAP_GEN(dev->mdev, bf))
1308 resp.bf_reg_size = 1 << MLX5_CAP_GEN(dev->mdev, log_bf_reg_size);
Daniel Jurgensb47bd6e2016-10-25 18:36:24 +03001309 resp.cache_line_size = cache_line_size();
Saeed Mahameed938fe832015-05-28 22:28:41 +03001310 resp.max_sq_desc_sz = MLX5_CAP_GEN(dev->mdev, max_wqe_sz_sq);
1311 resp.max_rq_desc_sz = MLX5_CAP_GEN(dev->mdev, max_wqe_sz_rq);
1312 resp.max_send_wqebb = 1 << MLX5_CAP_GEN(dev->mdev, log_max_qp_sz);
1313 resp.max_recv_wr = 1 << MLX5_CAP_GEN(dev->mdev, log_max_qp_sz);
1314 resp.max_srq_recv_wr = 1 << MLX5_CAP_GEN(dev->mdev, log_max_srq_sz);
Haggai Abramovskyf72300c2016-01-14 19:12:58 +02001315 resp.cqe_version = min_t(__u8,
1316 (__u8)MLX5_CAP_GEN(dev->mdev, cqe_version),
1317 req.max_cqe_version);
Eli Cohen30aa60b2017-01-03 23:55:27 +02001318 resp.log_uar_size = MLX5_CAP_GEN(dev->mdev, uar_4k) ?
1319 MLX5_ADAPTER_PAGE_SHIFT : PAGE_SHIFT;
1320 resp.num_uars_per_page = MLX5_CAP_GEN(dev->mdev, uar_4k) ?
1321 MLX5_CAP_GEN(dev->mdev, num_of_uars_per_page) : 1;
Matan Barakb368d7c2015-12-15 20:30:12 +02001322 resp.response_length = min(offsetof(typeof(resp), response_length) +
1323 sizeof(resp.response_length), udata->outlen);
Eli Cohene126ba92013-07-07 17:25:49 +03001324
1325 context = kzalloc(sizeof(*context), GFP_KERNEL);
1326 if (!context)
1327 return ERR_PTR(-ENOMEM);
1328
Eli Cohen30aa60b2017-01-03 23:55:27 +02001329 lib_uar_4k = req.lib_caps & MLX5_LIB_CAP_4K_UAR;
Eli Cohen2f5ff262017-01-03 23:55:21 +02001330 bfregi = &context->bfregi;
Eli Cohenb037c292017-01-03 23:55:26 +02001331
1332 /* updates req->total_num_bfregs */
1333 err = calc_total_bfregs(dev, lib_uar_4k, &req, &bfregi->num_sys_pages);
1334 if (err)
1335 goto out_ctx;
1336
Eli Cohen2f5ff262017-01-03 23:55:21 +02001337 mutex_init(&bfregi->lock);
Eli Cohenb037c292017-01-03 23:55:26 +02001338 bfregi->lib_uar_4k = lib_uar_4k;
1339 bfregi->count = kcalloc(req.total_num_bfregs, sizeof(*bfregi->count),
1340 GFP_KERNEL);
1341 if (!bfregi->count) {
Eli Cohene126ba92013-07-07 17:25:49 +03001342 err = -ENOMEM;
1343 goto out_ctx;
1344 }
1345
Eli Cohenb037c292017-01-03 23:55:26 +02001346 bfregi->sys_pages = kcalloc(bfregi->num_sys_pages,
1347 sizeof(*bfregi->sys_pages),
1348 GFP_KERNEL);
1349 if (!bfregi->sys_pages) {
Eli Cohene126ba92013-07-07 17:25:49 +03001350 err = -ENOMEM;
Eli Cohenb037c292017-01-03 23:55:26 +02001351 goto out_count;
Eli Cohene126ba92013-07-07 17:25:49 +03001352 }
1353
Eli Cohenb037c292017-01-03 23:55:26 +02001354 err = allocate_uars(dev, context);
1355 if (err)
1356 goto out_sys_pages;
Eli Cohene126ba92013-07-07 17:25:49 +03001357
Haggai Eranb4cfe442014-12-11 17:04:26 +02001358#ifdef CONFIG_INFINIBAND_ON_DEMAND_PAGING
1359 context->ibucontext.invalidate_range = &mlx5_ib_invalidate_range;
1360#endif
1361
Artemy Kovalyov7d0cc6e2017-01-02 11:37:44 +02001362 context->upd_xlt_page = __get_free_page(GFP_KERNEL);
1363 if (!context->upd_xlt_page) {
1364 err = -ENOMEM;
1365 goto out_uars;
1366 }
1367 mutex_init(&context->upd_xlt_page_mutex);
1368
majd@mellanox.com146d2f12016-01-14 19:13:02 +02001369 if (MLX5_CAP_GEN(dev->mdev, log_max_transport_domain)) {
Huy Nguyenc85023e2017-05-30 09:42:54 +03001370 err = mlx5_ib_alloc_transport_domain(dev, &context->tdn);
majd@mellanox.com146d2f12016-01-14 19:13:02 +02001371 if (err)
Artemy Kovalyov7d0cc6e2017-01-02 11:37:44 +02001372 goto out_page;
majd@mellanox.com146d2f12016-01-14 19:13:02 +02001373 }
1374
Maor Gottlieb7c2344c2016-06-17 14:56:44 +03001375 INIT_LIST_HEAD(&context->vma_private_list);
Eli Cohene126ba92013-07-07 17:25:49 +03001376 INIT_LIST_HEAD(&context->db_page_list);
1377 mutex_init(&context->db_page_mutex);
1378
Eli Cohen2f5ff262017-01-03 23:55:21 +02001379 resp.tot_bfregs = req.total_num_bfregs;
Saeed Mahameed938fe832015-05-28 22:28:41 +03001380 resp.num_ports = MLX5_CAP_GEN(dev->mdev, num_ports);
Matan Barakb368d7c2015-12-15 20:30:12 +02001381
Haggai Abramovskyf72300c2016-01-14 19:12:58 +02001382 if (field_avail(typeof(resp), cqe_version, udata->outlen))
1383 resp.response_length += sizeof(resp.cqe_version);
Matan Barakb368d7c2015-12-15 20:30:12 +02001384
Bodong Wang402ca532016-06-17 15:02:20 +03001385 if (field_avail(typeof(resp), cmds_supp_uhw, udata->outlen)) {
Moni Shoua6ad279c52016-11-23 08:23:23 +02001386 resp.cmds_supp_uhw |= MLX5_USER_CMDS_SUPP_UHW_QUERY_DEVICE |
1387 MLX5_USER_CMDS_SUPP_UHW_CREATE_AH;
Bodong Wang402ca532016-06-17 15:02:20 +03001388 resp.response_length += sizeof(resp.cmds_supp_uhw);
1389 }
1390
Or Gerlitz78984892016-11-30 20:33:33 +02001391 if (field_avail(typeof(resp), eth_min_inline, udata->outlen)) {
1392 if (mlx5_ib_port_link_layer(ibdev, 1) == IB_LINK_LAYER_ETHERNET) {
1393 mlx5_query_min_inline(dev->mdev, &resp.eth_min_inline);
1394 resp.eth_min_inline++;
1395 }
1396 resp.response_length += sizeof(resp.eth_min_inline);
1397 }
1398
Noa Osherovichbc5c6ee2016-06-04 15:15:31 +03001399 /*
1400 * We don't want to expose information from the PCI bar that is located
1401 * after 4096 bytes, so if the arch only supports larger pages, let's
1402 * pretend we don't support reading the HCA's core clock. This is also
1403 * forced by mmap function.
1404 */
Eli Cohende8d6e02017-01-03 23:55:19 +02001405 if (field_avail(typeof(resp), hca_core_clock_offset, udata->outlen)) {
1406 if (PAGE_SIZE <= 4096) {
1407 resp.comp_mask |=
1408 MLX5_IB_ALLOC_UCONTEXT_RESP_MASK_CORE_CLOCK_OFFSET;
1409 resp.hca_core_clock_offset =
1410 offsetof(struct mlx5_init_seg, internal_timer_h) % PAGE_SIZE;
1411 }
Haggai Abramovskyf72300c2016-01-14 19:12:58 +02001412 resp.response_length += sizeof(resp.hca_core_clock_offset) +
Bodong Wang402ca532016-06-17 15:02:20 +03001413 sizeof(resp.reserved2);
Matan Barakb368d7c2015-12-15 20:30:12 +02001414 }
1415
Eli Cohen30aa60b2017-01-03 23:55:27 +02001416 if (field_avail(typeof(resp), log_uar_size, udata->outlen))
1417 resp.response_length += sizeof(resp.log_uar_size);
1418
1419 if (field_avail(typeof(resp), num_uars_per_page, udata->outlen))
1420 resp.response_length += sizeof(resp.num_uars_per_page);
1421
Matan Barakb368d7c2015-12-15 20:30:12 +02001422 err = ib_copy_to_udata(udata, &resp, resp.response_length);
Eli Cohene126ba92013-07-07 17:25:49 +03001423 if (err)
majd@mellanox.com146d2f12016-01-14 19:13:02 +02001424 goto out_td;
Eli Cohene126ba92013-07-07 17:25:49 +03001425
Eli Cohen2f5ff262017-01-03 23:55:21 +02001426 bfregi->ver = ver;
1427 bfregi->num_low_latency_bfregs = req.num_low_latency_bfregs;
Haggai Abramovskyf72300c2016-01-14 19:12:58 +02001428 context->cqe_version = resp.cqe_version;
Eli Cohen30aa60b2017-01-03 23:55:27 +02001429 context->lib_caps = req.lib_caps;
1430 print_lib_caps(dev, context->lib_caps);
Haggai Abramovskyf72300c2016-01-14 19:12:58 +02001431
Eli Cohene126ba92013-07-07 17:25:49 +03001432 return &context->ibucontext;
1433
majd@mellanox.com146d2f12016-01-14 19:13:02 +02001434out_td:
1435 if (MLX5_CAP_GEN(dev->mdev, log_max_transport_domain))
Huy Nguyenc85023e2017-05-30 09:42:54 +03001436 mlx5_ib_dealloc_transport_domain(dev, context->tdn);
majd@mellanox.com146d2f12016-01-14 19:13:02 +02001437
Artemy Kovalyov7d0cc6e2017-01-02 11:37:44 +02001438out_page:
1439 free_page(context->upd_xlt_page);
1440
Eli Cohene126ba92013-07-07 17:25:49 +03001441out_uars:
Eli Cohenb037c292017-01-03 23:55:26 +02001442 deallocate_uars(dev, context);
1443
1444out_sys_pages:
1445 kfree(bfregi->sys_pages);
1446
Eli Cohene126ba92013-07-07 17:25:49 +03001447out_count:
Eli Cohen2f5ff262017-01-03 23:55:21 +02001448 kfree(bfregi->count);
Eli Cohene126ba92013-07-07 17:25:49 +03001449
Eli Cohene126ba92013-07-07 17:25:49 +03001450out_ctx:
1451 kfree(context);
Eli Cohenb037c292017-01-03 23:55:26 +02001452
Eli Cohene126ba92013-07-07 17:25:49 +03001453 return ERR_PTR(err);
1454}
1455
1456static int mlx5_ib_dealloc_ucontext(struct ib_ucontext *ibcontext)
1457{
1458 struct mlx5_ib_ucontext *context = to_mucontext(ibcontext);
1459 struct mlx5_ib_dev *dev = to_mdev(ibcontext->device);
Eli Cohenb037c292017-01-03 23:55:26 +02001460 struct mlx5_bfreg_info *bfregi;
Eli Cohene126ba92013-07-07 17:25:49 +03001461
Eli Cohenb037c292017-01-03 23:55:26 +02001462 bfregi = &context->bfregi;
majd@mellanox.com146d2f12016-01-14 19:13:02 +02001463 if (MLX5_CAP_GEN(dev->mdev, log_max_transport_domain))
Huy Nguyenc85023e2017-05-30 09:42:54 +03001464 mlx5_ib_dealloc_transport_domain(dev, context->tdn);
majd@mellanox.com146d2f12016-01-14 19:13:02 +02001465
Artemy Kovalyov7d0cc6e2017-01-02 11:37:44 +02001466 free_page(context->upd_xlt_page);
Eli Cohenb037c292017-01-03 23:55:26 +02001467 deallocate_uars(dev, context);
1468 kfree(bfregi->sys_pages);
Eli Cohen2f5ff262017-01-03 23:55:21 +02001469 kfree(bfregi->count);
Eli Cohene126ba92013-07-07 17:25:49 +03001470 kfree(context);
1471
1472 return 0;
1473}
1474
Eli Cohenb037c292017-01-03 23:55:26 +02001475static phys_addr_t uar_index2pfn(struct mlx5_ib_dev *dev,
1476 struct mlx5_bfreg_info *bfregi,
1477 int idx)
Eli Cohene126ba92013-07-07 17:25:49 +03001478{
Eli Cohenb037c292017-01-03 23:55:26 +02001479 int fw_uars_per_page;
1480
1481 fw_uars_per_page = MLX5_CAP_GEN(dev->mdev, uar_4k) ? MLX5_UARS_IN_PAGE : 1;
1482
1483 return (pci_resource_start(dev->mdev->pdev, 0) >> PAGE_SHIFT) +
1484 bfregi->sys_pages[idx] / fw_uars_per_page;
Eli Cohene126ba92013-07-07 17:25:49 +03001485}
1486
1487static int get_command(unsigned long offset)
1488{
1489 return (offset >> MLX5_IB_MMAP_CMD_SHIFT) & MLX5_IB_MMAP_CMD_MASK;
1490}
1491
1492static int get_arg(unsigned long offset)
1493{
1494 return offset & ((1 << MLX5_IB_MMAP_CMD_SHIFT) - 1);
1495}
1496
1497static int get_index(unsigned long offset)
1498{
1499 return get_arg(offset);
1500}
1501
Maor Gottlieb7c2344c2016-06-17 14:56:44 +03001502static void mlx5_ib_vma_open(struct vm_area_struct *area)
1503{
1504 /* vma_open is called when a new VMA is created on top of our VMA. This
1505 * is done through either mremap flow or split_vma (usually due to
1506 * mlock, madvise, munmap, etc.) We do not support a clone of the VMA,
1507 * as this VMA is strongly hardware related. Therefore we set the
1508 * vm_ops of the newly created/cloned VMA to NULL, to prevent it from
1509 * calling us again and trying to do incorrect actions. We assume that
1510 * the original VMA size is exactly a single page, and therefore all
1511 * "splitting" operation will not happen to it.
1512 */
1513 area->vm_ops = NULL;
1514}
1515
1516static void mlx5_ib_vma_close(struct vm_area_struct *area)
1517{
1518 struct mlx5_ib_vma_private_data *mlx5_ib_vma_priv_data;
1519
1520 /* It's guaranteed that all VMAs opened on a FD are closed before the
1521 * file itself is closed, therefore no sync is needed with the regular
1522 * closing flow. (e.g. mlx5 ib_dealloc_ucontext)
1523 * However need a sync with accessing the vma as part of
1524 * mlx5_ib_disassociate_ucontext.
1525 * The close operation is usually called under mm->mmap_sem except when
1526 * process is exiting.
1527 * The exiting case is handled explicitly as part of
1528 * mlx5_ib_disassociate_ucontext.
1529 */
1530 mlx5_ib_vma_priv_data = (struct mlx5_ib_vma_private_data *)area->vm_private_data;
1531
1532 /* setting the vma context pointer to null in the mlx5_ib driver's
1533 * private data, to protect a race condition in
1534 * mlx5_ib_disassociate_ucontext().
1535 */
1536 mlx5_ib_vma_priv_data->vma = NULL;
1537 list_del(&mlx5_ib_vma_priv_data->list);
1538 kfree(mlx5_ib_vma_priv_data);
1539}
1540
1541static const struct vm_operations_struct mlx5_ib_vm_ops = {
1542 .open = mlx5_ib_vma_open,
1543 .close = mlx5_ib_vma_close
1544};
1545
1546static int mlx5_ib_set_vma_data(struct vm_area_struct *vma,
1547 struct mlx5_ib_ucontext *ctx)
1548{
1549 struct mlx5_ib_vma_private_data *vma_prv;
1550 struct list_head *vma_head = &ctx->vma_private_list;
1551
1552 vma_prv = kzalloc(sizeof(*vma_prv), GFP_KERNEL);
1553 if (!vma_prv)
1554 return -ENOMEM;
1555
1556 vma_prv->vma = vma;
1557 vma->vm_private_data = vma_prv;
1558 vma->vm_ops = &mlx5_ib_vm_ops;
1559
1560 list_add(&vma_prv->list, vma_head);
1561
1562 return 0;
1563}
1564
1565static void mlx5_ib_disassociate_ucontext(struct ib_ucontext *ibcontext)
1566{
1567 int ret;
1568 struct vm_area_struct *vma;
1569 struct mlx5_ib_vma_private_data *vma_private, *n;
1570 struct mlx5_ib_ucontext *context = to_mucontext(ibcontext);
1571 struct task_struct *owning_process = NULL;
1572 struct mm_struct *owning_mm = NULL;
1573
1574 owning_process = get_pid_task(ibcontext->tgid, PIDTYPE_PID);
1575 if (!owning_process)
1576 return;
1577
1578 owning_mm = get_task_mm(owning_process);
1579 if (!owning_mm) {
1580 pr_info("no mm, disassociate ucontext is pending task termination\n");
1581 while (1) {
1582 put_task_struct(owning_process);
1583 usleep_range(1000, 2000);
1584 owning_process = get_pid_task(ibcontext->tgid,
1585 PIDTYPE_PID);
1586 if (!owning_process ||
1587 owning_process->state == TASK_DEAD) {
1588 pr_info("disassociate ucontext done, task was terminated\n");
1589 /* in case task was dead need to release the
1590 * task struct.
1591 */
1592 if (owning_process)
1593 put_task_struct(owning_process);
1594 return;
1595 }
1596 }
1597 }
1598
1599 /* need to protect from a race on closing the vma as part of
1600 * mlx5_ib_vma_close.
1601 */
Maor Gottliebecc7d832017-03-29 06:03:02 +03001602 down_write(&owning_mm->mmap_sem);
Maor Gottlieb7c2344c2016-06-17 14:56:44 +03001603 list_for_each_entry_safe(vma_private, n, &context->vma_private_list,
1604 list) {
1605 vma = vma_private->vma;
1606 ret = zap_vma_ptes(vma, vma->vm_start,
1607 PAGE_SIZE);
1608 WARN_ONCE(ret, "%s: zap_vma_ptes failed", __func__);
1609 /* context going to be destroyed, should
1610 * not access ops any more.
1611 */
Maor Gottlieb13776612017-03-29 06:03:03 +03001612 vma->vm_flags &= ~(VM_SHARED | VM_MAYSHARE);
Maor Gottlieb7c2344c2016-06-17 14:56:44 +03001613 vma->vm_ops = NULL;
1614 list_del(&vma_private->list);
1615 kfree(vma_private);
1616 }
Maor Gottliebecc7d832017-03-29 06:03:02 +03001617 up_write(&owning_mm->mmap_sem);
Maor Gottlieb7c2344c2016-06-17 14:56:44 +03001618 mmput(owning_mm);
1619 put_task_struct(owning_process);
1620}
1621
Guy Levi37aa5c32016-04-27 16:49:50 +03001622static inline char *mmap_cmd2str(enum mlx5_ib_mmap_cmd cmd)
1623{
1624 switch (cmd) {
1625 case MLX5_IB_MMAP_WC_PAGE:
1626 return "WC";
1627 case MLX5_IB_MMAP_REGULAR_PAGE:
1628 return "best effort WC";
1629 case MLX5_IB_MMAP_NC_PAGE:
1630 return "NC";
1631 default:
1632 return NULL;
1633 }
1634}
1635
1636static int uar_mmap(struct mlx5_ib_dev *dev, enum mlx5_ib_mmap_cmd cmd,
Maor Gottlieb7c2344c2016-06-17 14:56:44 +03001637 struct vm_area_struct *vma,
1638 struct mlx5_ib_ucontext *context)
Guy Levi37aa5c32016-04-27 16:49:50 +03001639{
Eli Cohen2f5ff262017-01-03 23:55:21 +02001640 struct mlx5_bfreg_info *bfregi = &context->bfregi;
Guy Levi37aa5c32016-04-27 16:49:50 +03001641 int err;
1642 unsigned long idx;
1643 phys_addr_t pfn, pa;
1644 pgprot_t prot;
Eli Cohenb037c292017-01-03 23:55:26 +02001645 int uars_per_page;
1646
1647 if (vma->vm_end - vma->vm_start != PAGE_SIZE)
1648 return -EINVAL;
1649
1650 uars_per_page = get_uars_per_sys_page(dev, bfregi->lib_uar_4k);
1651 idx = get_index(vma->vm_pgoff);
1652 if (idx % uars_per_page ||
1653 idx * uars_per_page >= bfregi->num_sys_pages) {
1654 mlx5_ib_warn(dev, "invalid uar index %lu\n", idx);
1655 return -EINVAL;
1656 }
Guy Levi37aa5c32016-04-27 16:49:50 +03001657
1658 switch (cmd) {
1659 case MLX5_IB_MMAP_WC_PAGE:
1660/* Some architectures don't support WC memory */
1661#if defined(CONFIG_X86)
1662 if (!pat_enabled())
1663 return -EPERM;
1664#elif !(defined(CONFIG_PPC) || (defined(CONFIG_ARM) && defined(CONFIG_MMU)))
1665 return -EPERM;
1666#endif
1667 /* fall through */
1668 case MLX5_IB_MMAP_REGULAR_PAGE:
1669 /* For MLX5_IB_MMAP_REGULAR_PAGE do the best effort to get WC */
1670 prot = pgprot_writecombine(vma->vm_page_prot);
1671 break;
1672 case MLX5_IB_MMAP_NC_PAGE:
1673 prot = pgprot_noncached(vma->vm_page_prot);
1674 break;
1675 default:
1676 return -EINVAL;
1677 }
1678
Eli Cohenb037c292017-01-03 23:55:26 +02001679 pfn = uar_index2pfn(dev, bfregi, idx);
Guy Levi37aa5c32016-04-27 16:49:50 +03001680 mlx5_ib_dbg(dev, "uar idx 0x%lx, pfn %pa\n", idx, &pfn);
1681
1682 vma->vm_page_prot = prot;
1683 err = io_remap_pfn_range(vma, vma->vm_start, pfn,
1684 PAGE_SIZE, vma->vm_page_prot);
1685 if (err) {
1686 mlx5_ib_err(dev, "io_remap_pfn_range failed with error=%d, vm_start=0x%lx, pfn=%pa, mmap_cmd=%s\n",
1687 err, vma->vm_start, &pfn, mmap_cmd2str(cmd));
1688 return -EAGAIN;
1689 }
1690
1691 pa = pfn << PAGE_SHIFT;
1692 mlx5_ib_dbg(dev, "mapped %s at 0x%lx, PA %pa\n", mmap_cmd2str(cmd),
1693 vma->vm_start, &pa);
1694
Maor Gottlieb7c2344c2016-06-17 14:56:44 +03001695 return mlx5_ib_set_vma_data(vma, context);
Guy Levi37aa5c32016-04-27 16:49:50 +03001696}
1697
Eli Cohene126ba92013-07-07 17:25:49 +03001698static int mlx5_ib_mmap(struct ib_ucontext *ibcontext, struct vm_area_struct *vma)
1699{
1700 struct mlx5_ib_ucontext *context = to_mucontext(ibcontext);
1701 struct mlx5_ib_dev *dev = to_mdev(ibcontext->device);
Eli Cohene126ba92013-07-07 17:25:49 +03001702 unsigned long command;
Eli Cohene126ba92013-07-07 17:25:49 +03001703 phys_addr_t pfn;
1704
1705 command = get_command(vma->vm_pgoff);
1706 switch (command) {
Guy Levi37aa5c32016-04-27 16:49:50 +03001707 case MLX5_IB_MMAP_WC_PAGE:
1708 case MLX5_IB_MMAP_NC_PAGE:
Eli Cohene126ba92013-07-07 17:25:49 +03001709 case MLX5_IB_MMAP_REGULAR_PAGE:
Maor Gottlieb7c2344c2016-06-17 14:56:44 +03001710 return uar_mmap(dev, command, vma, context);
Eli Cohene126ba92013-07-07 17:25:49 +03001711
1712 case MLX5_IB_MMAP_GET_CONTIGUOUS_PAGES:
1713 return -ENOSYS;
1714
Matan Barakd69e3bc2015-12-15 20:30:13 +02001715 case MLX5_IB_MMAP_CORE_CLOCK:
Matan Barakd69e3bc2015-12-15 20:30:13 +02001716 if (vma->vm_end - vma->vm_start != PAGE_SIZE)
1717 return -EINVAL;
1718
Matan Barak6cbac1e2016-04-14 16:52:10 +03001719 if (vma->vm_flags & VM_WRITE)
Matan Barakd69e3bc2015-12-15 20:30:13 +02001720 return -EPERM;
1721
1722 /* Don't expose to user-space information it shouldn't have */
1723 if (PAGE_SIZE > 4096)
1724 return -EOPNOTSUPP;
1725
1726 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
1727 pfn = (dev->mdev->iseg_base +
1728 offsetof(struct mlx5_init_seg, internal_timer_h)) >>
1729 PAGE_SHIFT;
1730 if (io_remap_pfn_range(vma, vma->vm_start, pfn,
1731 PAGE_SIZE, vma->vm_page_prot))
1732 return -EAGAIN;
1733
1734 mlx5_ib_dbg(dev, "mapped internal timer at 0x%lx, PA 0x%llx\n",
1735 vma->vm_start,
1736 (unsigned long long)pfn << PAGE_SHIFT);
1737 break;
Matan Barakd69e3bc2015-12-15 20:30:13 +02001738
Eli Cohene126ba92013-07-07 17:25:49 +03001739 default:
1740 return -EINVAL;
1741 }
1742
1743 return 0;
1744}
1745
Eli Cohene126ba92013-07-07 17:25:49 +03001746static struct ib_pd *mlx5_ib_alloc_pd(struct ib_device *ibdev,
1747 struct ib_ucontext *context,
1748 struct ib_udata *udata)
1749{
1750 struct mlx5_ib_alloc_pd_resp resp;
1751 struct mlx5_ib_pd *pd;
1752 int err;
1753
1754 pd = kmalloc(sizeof(*pd), GFP_KERNEL);
1755 if (!pd)
1756 return ERR_PTR(-ENOMEM);
1757
Jack Morgenstein9603b612014-07-28 23:30:22 +03001758 err = mlx5_core_alloc_pd(to_mdev(ibdev)->mdev, &pd->pdn);
Eli Cohene126ba92013-07-07 17:25:49 +03001759 if (err) {
1760 kfree(pd);
1761 return ERR_PTR(err);
1762 }
1763
1764 if (context) {
1765 resp.pdn = pd->pdn;
1766 if (ib_copy_to_udata(udata, &resp, sizeof(resp))) {
Jack Morgenstein9603b612014-07-28 23:30:22 +03001767 mlx5_core_dealloc_pd(to_mdev(ibdev)->mdev, pd->pdn);
Eli Cohene126ba92013-07-07 17:25:49 +03001768 kfree(pd);
1769 return ERR_PTR(-EFAULT);
1770 }
Eli Cohene126ba92013-07-07 17:25:49 +03001771 }
1772
1773 return &pd->ibpd;
1774}
1775
1776static int mlx5_ib_dealloc_pd(struct ib_pd *pd)
1777{
1778 struct mlx5_ib_dev *mdev = to_mdev(pd->device);
1779 struct mlx5_ib_pd *mpd = to_mpd(pd);
1780
Jack Morgenstein9603b612014-07-28 23:30:22 +03001781 mlx5_core_dealloc_pd(mdev->mdev, mpd->pdn);
Eli Cohene126ba92013-07-07 17:25:49 +03001782 kfree(mpd);
1783
1784 return 0;
1785}
1786
Maor Gottlieb466fa6d2016-08-30 16:58:36 +03001787enum {
1788 MATCH_CRITERIA_ENABLE_OUTER_BIT,
1789 MATCH_CRITERIA_ENABLE_MISC_BIT,
1790 MATCH_CRITERIA_ENABLE_INNER_BIT
1791};
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02001792
Maor Gottlieb466fa6d2016-08-30 16:58:36 +03001793#define HEADER_IS_ZERO(match_criteria, headers) \
1794 !(memchr_inv(MLX5_ADDR_OF(fte_match_param, match_criteria, headers), \
1795 0, MLX5_FLD_SZ_BYTES(fte_match_param, headers))) \
1796
1797static u8 get_match_criteria_enable(u32 *match_criteria)
1798{
1799 u8 match_criteria_enable;
1800
1801 match_criteria_enable =
1802 (!HEADER_IS_ZERO(match_criteria, outer_headers)) <<
1803 MATCH_CRITERIA_ENABLE_OUTER_BIT;
1804 match_criteria_enable |=
1805 (!HEADER_IS_ZERO(match_criteria, misc_parameters)) <<
1806 MATCH_CRITERIA_ENABLE_MISC_BIT;
1807 match_criteria_enable |=
1808 (!HEADER_IS_ZERO(match_criteria, inner_headers)) <<
1809 MATCH_CRITERIA_ENABLE_INNER_BIT;
1810
1811 return match_criteria_enable;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02001812}
1813
Maor Gottliebca0d4752016-08-30 16:58:35 +03001814static void set_proto(void *outer_c, void *outer_v, u8 mask, u8 val)
1815{
1816 MLX5_SET(fte_match_set_lyr_2_4, outer_c, ip_protocol, mask);
1817 MLX5_SET(fte_match_set_lyr_2_4, outer_v, ip_protocol, val);
1818}
1819
Moses Reuben2d1e6972016-11-14 19:04:52 +02001820static void set_flow_label(void *misc_c, void *misc_v, u8 mask, u8 val,
1821 bool inner)
1822{
1823 if (inner) {
1824 MLX5_SET(fte_match_set_misc,
1825 misc_c, inner_ipv6_flow_label, mask);
1826 MLX5_SET(fte_match_set_misc,
1827 misc_v, inner_ipv6_flow_label, val);
1828 } else {
1829 MLX5_SET(fte_match_set_misc,
1830 misc_c, outer_ipv6_flow_label, mask);
1831 MLX5_SET(fte_match_set_misc,
1832 misc_v, outer_ipv6_flow_label, val);
1833 }
1834}
1835
Maor Gottliebca0d4752016-08-30 16:58:35 +03001836static void set_tos(void *outer_c, void *outer_v, u8 mask, u8 val)
1837{
1838 MLX5_SET(fte_match_set_lyr_2_4, outer_c, ip_ecn, mask);
1839 MLX5_SET(fte_match_set_lyr_2_4, outer_v, ip_ecn, val);
1840 MLX5_SET(fte_match_set_lyr_2_4, outer_c, ip_dscp, mask >> 2);
1841 MLX5_SET(fte_match_set_lyr_2_4, outer_v, ip_dscp, val >> 2);
1842}
1843
Maor Gottliebc47ac6a2016-08-30 16:58:31 +03001844#define LAST_ETH_FIELD vlan_tag
1845#define LAST_IB_FIELD sl
Maor Gottliebca0d4752016-08-30 16:58:35 +03001846#define LAST_IPV4_FIELD tos
Maor Gottlieb466fa6d2016-08-30 16:58:36 +03001847#define LAST_IPV6_FIELD traffic_class
Maor Gottliebc47ac6a2016-08-30 16:58:31 +03001848#define LAST_TCP_UDP_FIELD src_port
Moses Reubenffb30d82016-11-14 19:04:50 +02001849#define LAST_TUNNEL_FIELD tunnel_id
Moses Reuben2ac693f2017-01-18 14:59:50 +02001850#define LAST_FLOW_TAG_FIELD tag_id
Slava Shwartsmana22ed862017-04-03 13:13:52 +03001851#define LAST_DROP_FIELD size
Maor Gottliebc47ac6a2016-08-30 16:58:31 +03001852
1853/* Field is the last supported field */
1854#define FIELDS_NOT_SUPPORTED(filter, field)\
1855 memchr_inv((void *)&filter.field +\
1856 sizeof(filter.field), 0,\
1857 sizeof(filter) -\
1858 offsetof(typeof(filter), field) -\
1859 sizeof(filter.field))
1860
Ariel Levkovich19cc7522017-04-03 13:11:03 +03001861#define IPV4_VERSION 4
1862#define IPV6_VERSION 6
1863static int parse_flow_attr(struct mlx5_core_dev *mdev, u32 *match_c,
1864 u32 *match_v, const union ib_flow_spec *ib_spec,
Slava Shwartsmana22ed862017-04-03 13:13:52 +03001865 u32 *tag_id, bool *is_drop)
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02001866{
Maor Gottlieb466fa6d2016-08-30 16:58:36 +03001867 void *misc_params_c = MLX5_ADDR_OF(fte_match_param, match_c,
1868 misc_parameters);
1869 void *misc_params_v = MLX5_ADDR_OF(fte_match_param, match_v,
1870 misc_parameters);
Moses Reuben2d1e6972016-11-14 19:04:52 +02001871 void *headers_c;
1872 void *headers_v;
Ariel Levkovich19cc7522017-04-03 13:11:03 +03001873 int match_ipv;
Maor Gottlieb466fa6d2016-08-30 16:58:36 +03001874
Moses Reuben2d1e6972016-11-14 19:04:52 +02001875 if (ib_spec->type & IB_FLOW_SPEC_INNER) {
1876 headers_c = MLX5_ADDR_OF(fte_match_param, match_c,
1877 inner_headers);
1878 headers_v = MLX5_ADDR_OF(fte_match_param, match_v,
1879 inner_headers);
Ariel Levkovich19cc7522017-04-03 13:11:03 +03001880 match_ipv = MLX5_CAP_FLOWTABLE_NIC_RX(mdev,
1881 ft_field_support.inner_ip_version);
Moses Reuben2d1e6972016-11-14 19:04:52 +02001882 } else {
1883 headers_c = MLX5_ADDR_OF(fte_match_param, match_c,
1884 outer_headers);
1885 headers_v = MLX5_ADDR_OF(fte_match_param, match_v,
1886 outer_headers);
Ariel Levkovich19cc7522017-04-03 13:11:03 +03001887 match_ipv = MLX5_CAP_FLOWTABLE_NIC_RX(mdev,
1888 ft_field_support.outer_ip_version);
Moses Reuben2d1e6972016-11-14 19:04:52 +02001889 }
1890
1891 switch (ib_spec->type & ~IB_FLOW_SPEC_INNER) {
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02001892 case IB_FLOW_SPEC_ETH:
Maor Gottliebc47ac6a2016-08-30 16:58:31 +03001893 if (FIELDS_NOT_SUPPORTED(ib_spec->eth.mask, LAST_ETH_FIELD))
Leon Romanovsky1ffd3a22017-01-18 14:59:51 +02001894 return -EOPNOTSUPP;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02001895
Moses Reuben2d1e6972016-11-14 19:04:52 +02001896 ether_addr_copy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02001897 dmac_47_16),
1898 ib_spec->eth.mask.dst_mac);
Moses Reuben2d1e6972016-11-14 19:04:52 +02001899 ether_addr_copy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02001900 dmac_47_16),
1901 ib_spec->eth.val.dst_mac);
1902
Moses Reuben2d1e6972016-11-14 19:04:52 +02001903 ether_addr_copy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
Maor Gottliebee3da802016-09-12 19:16:24 +03001904 smac_47_16),
1905 ib_spec->eth.mask.src_mac);
Moses Reuben2d1e6972016-11-14 19:04:52 +02001906 ether_addr_copy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
Maor Gottliebee3da802016-09-12 19:16:24 +03001907 smac_47_16),
1908 ib_spec->eth.val.src_mac);
1909
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02001910 if (ib_spec->eth.mask.vlan_tag) {
Moses Reuben2d1e6972016-11-14 19:04:52 +02001911 MLX5_SET(fte_match_set_lyr_2_4, headers_c,
Mohamad Haj Yahia10543362016-10-09 16:25:43 +03001912 cvlan_tag, 1);
Moses Reuben2d1e6972016-11-14 19:04:52 +02001913 MLX5_SET(fte_match_set_lyr_2_4, headers_v,
Mohamad Haj Yahia10543362016-10-09 16:25:43 +03001914 cvlan_tag, 1);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02001915
Moses Reuben2d1e6972016-11-14 19:04:52 +02001916 MLX5_SET(fte_match_set_lyr_2_4, headers_c,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02001917 first_vid, ntohs(ib_spec->eth.mask.vlan_tag));
Moses Reuben2d1e6972016-11-14 19:04:52 +02001918 MLX5_SET(fte_match_set_lyr_2_4, headers_v,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02001919 first_vid, ntohs(ib_spec->eth.val.vlan_tag));
1920
Moses Reuben2d1e6972016-11-14 19:04:52 +02001921 MLX5_SET(fte_match_set_lyr_2_4, headers_c,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02001922 first_cfi,
1923 ntohs(ib_spec->eth.mask.vlan_tag) >> 12);
Moses Reuben2d1e6972016-11-14 19:04:52 +02001924 MLX5_SET(fte_match_set_lyr_2_4, headers_v,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02001925 first_cfi,
1926 ntohs(ib_spec->eth.val.vlan_tag) >> 12);
1927
Moses Reuben2d1e6972016-11-14 19:04:52 +02001928 MLX5_SET(fte_match_set_lyr_2_4, headers_c,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02001929 first_prio,
1930 ntohs(ib_spec->eth.mask.vlan_tag) >> 13);
Moses Reuben2d1e6972016-11-14 19:04:52 +02001931 MLX5_SET(fte_match_set_lyr_2_4, headers_v,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02001932 first_prio,
1933 ntohs(ib_spec->eth.val.vlan_tag) >> 13);
1934 }
Moses Reuben2d1e6972016-11-14 19:04:52 +02001935 MLX5_SET(fte_match_set_lyr_2_4, headers_c,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02001936 ethertype, ntohs(ib_spec->eth.mask.ether_type));
Moses Reuben2d1e6972016-11-14 19:04:52 +02001937 MLX5_SET(fte_match_set_lyr_2_4, headers_v,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02001938 ethertype, ntohs(ib_spec->eth.val.ether_type));
1939 break;
1940 case IB_FLOW_SPEC_IPV4:
Maor Gottliebc47ac6a2016-08-30 16:58:31 +03001941 if (FIELDS_NOT_SUPPORTED(ib_spec->ipv4.mask, LAST_IPV4_FIELD))
Leon Romanovsky1ffd3a22017-01-18 14:59:51 +02001942 return -EOPNOTSUPP;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02001943
Ariel Levkovich19cc7522017-04-03 13:11:03 +03001944 if (match_ipv) {
1945 MLX5_SET(fte_match_set_lyr_2_4, headers_c,
1946 ip_version, 0xf);
1947 MLX5_SET(fte_match_set_lyr_2_4, headers_v,
1948 ip_version, IPV4_VERSION);
1949 } else {
1950 MLX5_SET(fte_match_set_lyr_2_4, headers_c,
1951 ethertype, 0xffff);
1952 MLX5_SET(fte_match_set_lyr_2_4, headers_v,
1953 ethertype, ETH_P_IP);
1954 }
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02001955
Moses Reuben2d1e6972016-11-14 19:04:52 +02001956 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02001957 src_ipv4_src_ipv6.ipv4_layout.ipv4),
1958 &ib_spec->ipv4.mask.src_ip,
1959 sizeof(ib_spec->ipv4.mask.src_ip));
Moses Reuben2d1e6972016-11-14 19:04:52 +02001960 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02001961 src_ipv4_src_ipv6.ipv4_layout.ipv4),
1962 &ib_spec->ipv4.val.src_ip,
1963 sizeof(ib_spec->ipv4.val.src_ip));
Moses Reuben2d1e6972016-11-14 19:04:52 +02001964 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02001965 dst_ipv4_dst_ipv6.ipv4_layout.ipv4),
1966 &ib_spec->ipv4.mask.dst_ip,
1967 sizeof(ib_spec->ipv4.mask.dst_ip));
Moses Reuben2d1e6972016-11-14 19:04:52 +02001968 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02001969 dst_ipv4_dst_ipv6.ipv4_layout.ipv4),
1970 &ib_spec->ipv4.val.dst_ip,
1971 sizeof(ib_spec->ipv4.val.dst_ip));
Maor Gottliebca0d4752016-08-30 16:58:35 +03001972
Moses Reuben2d1e6972016-11-14 19:04:52 +02001973 set_tos(headers_c, headers_v,
Maor Gottliebca0d4752016-08-30 16:58:35 +03001974 ib_spec->ipv4.mask.tos, ib_spec->ipv4.val.tos);
1975
Moses Reuben2d1e6972016-11-14 19:04:52 +02001976 set_proto(headers_c, headers_v,
Maor Gottliebca0d4752016-08-30 16:58:35 +03001977 ib_spec->ipv4.mask.proto, ib_spec->ipv4.val.proto);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02001978 break;
Maor Gottlieb026bae02016-06-17 15:14:51 +03001979 case IB_FLOW_SPEC_IPV6:
Maor Gottliebc47ac6a2016-08-30 16:58:31 +03001980 if (FIELDS_NOT_SUPPORTED(ib_spec->ipv6.mask, LAST_IPV6_FIELD))
Leon Romanovsky1ffd3a22017-01-18 14:59:51 +02001981 return -EOPNOTSUPP;
Maor Gottlieb026bae02016-06-17 15:14:51 +03001982
Ariel Levkovich19cc7522017-04-03 13:11:03 +03001983 if (match_ipv) {
1984 MLX5_SET(fte_match_set_lyr_2_4, headers_c,
1985 ip_version, 0xf);
1986 MLX5_SET(fte_match_set_lyr_2_4, headers_v,
1987 ip_version, IPV6_VERSION);
1988 } else {
1989 MLX5_SET(fte_match_set_lyr_2_4, headers_c,
1990 ethertype, 0xffff);
1991 MLX5_SET(fte_match_set_lyr_2_4, headers_v,
1992 ethertype, ETH_P_IPV6);
1993 }
Maor Gottlieb026bae02016-06-17 15:14:51 +03001994
Moses Reuben2d1e6972016-11-14 19:04:52 +02001995 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
Maor Gottlieb026bae02016-06-17 15:14:51 +03001996 src_ipv4_src_ipv6.ipv6_layout.ipv6),
1997 &ib_spec->ipv6.mask.src_ip,
1998 sizeof(ib_spec->ipv6.mask.src_ip));
Moses Reuben2d1e6972016-11-14 19:04:52 +02001999 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
Maor Gottlieb026bae02016-06-17 15:14:51 +03002000 src_ipv4_src_ipv6.ipv6_layout.ipv6),
2001 &ib_spec->ipv6.val.src_ip,
2002 sizeof(ib_spec->ipv6.val.src_ip));
Moses Reuben2d1e6972016-11-14 19:04:52 +02002003 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
Maor Gottlieb026bae02016-06-17 15:14:51 +03002004 dst_ipv4_dst_ipv6.ipv6_layout.ipv6),
2005 &ib_spec->ipv6.mask.dst_ip,
2006 sizeof(ib_spec->ipv6.mask.dst_ip));
Moses Reuben2d1e6972016-11-14 19:04:52 +02002007 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
Maor Gottlieb026bae02016-06-17 15:14:51 +03002008 dst_ipv4_dst_ipv6.ipv6_layout.ipv6),
2009 &ib_spec->ipv6.val.dst_ip,
2010 sizeof(ib_spec->ipv6.val.dst_ip));
Maor Gottlieb466fa6d2016-08-30 16:58:36 +03002011
Moses Reuben2d1e6972016-11-14 19:04:52 +02002012 set_tos(headers_c, headers_v,
Maor Gottlieb466fa6d2016-08-30 16:58:36 +03002013 ib_spec->ipv6.mask.traffic_class,
2014 ib_spec->ipv6.val.traffic_class);
2015
Moses Reuben2d1e6972016-11-14 19:04:52 +02002016 set_proto(headers_c, headers_v,
Maor Gottlieb466fa6d2016-08-30 16:58:36 +03002017 ib_spec->ipv6.mask.next_hdr,
2018 ib_spec->ipv6.val.next_hdr);
2019
Moses Reuben2d1e6972016-11-14 19:04:52 +02002020 set_flow_label(misc_params_c, misc_params_v,
2021 ntohl(ib_spec->ipv6.mask.flow_label),
2022 ntohl(ib_spec->ipv6.val.flow_label),
2023 ib_spec->type & IB_FLOW_SPEC_INNER);
2024
Maor Gottlieb026bae02016-06-17 15:14:51 +03002025 break;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002026 case IB_FLOW_SPEC_TCP:
Maor Gottliebc47ac6a2016-08-30 16:58:31 +03002027 if (FIELDS_NOT_SUPPORTED(ib_spec->tcp_udp.mask,
2028 LAST_TCP_UDP_FIELD))
Leon Romanovsky1ffd3a22017-01-18 14:59:51 +02002029 return -EOPNOTSUPP;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002030
Moses Reuben2d1e6972016-11-14 19:04:52 +02002031 MLX5_SET(fte_match_set_lyr_2_4, headers_c, ip_protocol,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002032 0xff);
Moses Reuben2d1e6972016-11-14 19:04:52 +02002033 MLX5_SET(fte_match_set_lyr_2_4, headers_v, ip_protocol,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002034 IPPROTO_TCP);
2035
Moses Reuben2d1e6972016-11-14 19:04:52 +02002036 MLX5_SET(fte_match_set_lyr_2_4, headers_c, tcp_sport,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002037 ntohs(ib_spec->tcp_udp.mask.src_port));
Moses Reuben2d1e6972016-11-14 19:04:52 +02002038 MLX5_SET(fte_match_set_lyr_2_4, headers_v, tcp_sport,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002039 ntohs(ib_spec->tcp_udp.val.src_port));
2040
Moses Reuben2d1e6972016-11-14 19:04:52 +02002041 MLX5_SET(fte_match_set_lyr_2_4, headers_c, tcp_dport,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002042 ntohs(ib_spec->tcp_udp.mask.dst_port));
Moses Reuben2d1e6972016-11-14 19:04:52 +02002043 MLX5_SET(fte_match_set_lyr_2_4, headers_v, tcp_dport,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002044 ntohs(ib_spec->tcp_udp.val.dst_port));
2045 break;
2046 case IB_FLOW_SPEC_UDP:
Maor Gottliebc47ac6a2016-08-30 16:58:31 +03002047 if (FIELDS_NOT_SUPPORTED(ib_spec->tcp_udp.mask,
2048 LAST_TCP_UDP_FIELD))
Leon Romanovsky1ffd3a22017-01-18 14:59:51 +02002049 return -EOPNOTSUPP;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002050
Moses Reuben2d1e6972016-11-14 19:04:52 +02002051 MLX5_SET(fte_match_set_lyr_2_4, headers_c, ip_protocol,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002052 0xff);
Moses Reuben2d1e6972016-11-14 19:04:52 +02002053 MLX5_SET(fte_match_set_lyr_2_4, headers_v, ip_protocol,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002054 IPPROTO_UDP);
2055
Moses Reuben2d1e6972016-11-14 19:04:52 +02002056 MLX5_SET(fte_match_set_lyr_2_4, headers_c, udp_sport,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002057 ntohs(ib_spec->tcp_udp.mask.src_port));
Moses Reuben2d1e6972016-11-14 19:04:52 +02002058 MLX5_SET(fte_match_set_lyr_2_4, headers_v, udp_sport,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002059 ntohs(ib_spec->tcp_udp.val.src_port));
2060
Moses Reuben2d1e6972016-11-14 19:04:52 +02002061 MLX5_SET(fte_match_set_lyr_2_4, headers_c, udp_dport,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002062 ntohs(ib_spec->tcp_udp.mask.dst_port));
Moses Reuben2d1e6972016-11-14 19:04:52 +02002063 MLX5_SET(fte_match_set_lyr_2_4, headers_v, udp_dport,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002064 ntohs(ib_spec->tcp_udp.val.dst_port));
2065 break;
Moses Reubenffb30d82016-11-14 19:04:50 +02002066 case IB_FLOW_SPEC_VXLAN_TUNNEL:
2067 if (FIELDS_NOT_SUPPORTED(ib_spec->tunnel.mask,
2068 LAST_TUNNEL_FIELD))
Leon Romanovsky1ffd3a22017-01-18 14:59:51 +02002069 return -EOPNOTSUPP;
Moses Reubenffb30d82016-11-14 19:04:50 +02002070
2071 MLX5_SET(fte_match_set_misc, misc_params_c, vxlan_vni,
2072 ntohl(ib_spec->tunnel.mask.tunnel_id));
2073 MLX5_SET(fte_match_set_misc, misc_params_v, vxlan_vni,
2074 ntohl(ib_spec->tunnel.val.tunnel_id));
2075 break;
Moses Reuben2ac693f2017-01-18 14:59:50 +02002076 case IB_FLOW_SPEC_ACTION_TAG:
2077 if (FIELDS_NOT_SUPPORTED(ib_spec->flow_tag,
2078 LAST_FLOW_TAG_FIELD))
2079 return -EOPNOTSUPP;
2080 if (ib_spec->flow_tag.tag_id >= BIT(24))
2081 return -EINVAL;
2082
2083 *tag_id = ib_spec->flow_tag.tag_id;
2084 break;
Slava Shwartsmana22ed862017-04-03 13:13:52 +03002085 case IB_FLOW_SPEC_ACTION_DROP:
2086 if (FIELDS_NOT_SUPPORTED(ib_spec->drop,
2087 LAST_DROP_FIELD))
2088 return -EOPNOTSUPP;
2089 *is_drop = true;
2090 break;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002091 default:
2092 return -EINVAL;
2093 }
2094
2095 return 0;
2096}
2097
2098/* If a flow could catch both multicast and unicast packets,
2099 * it won't fall into the multicast flow steering table and this rule
2100 * could steal other multicast packets.
2101 */
2102static bool flow_is_multicast_only(struct ib_flow_attr *ib_attr)
2103{
2104 struct ib_flow_spec_eth *eth_spec;
2105
2106 if (ib_attr->type != IB_FLOW_ATTR_NORMAL ||
2107 ib_attr->size < sizeof(struct ib_flow_attr) +
2108 sizeof(struct ib_flow_spec_eth) ||
2109 ib_attr->num_of_specs < 1)
2110 return false;
2111
2112 eth_spec = (struct ib_flow_spec_eth *)(ib_attr + 1);
2113 if (eth_spec->type != IB_FLOW_SPEC_ETH ||
2114 eth_spec->size != sizeof(*eth_spec))
2115 return false;
2116
2117 return is_multicast_ether_addr(eth_spec->mask.dst_mac) &&
2118 is_multicast_ether_addr(eth_spec->val.dst_mac);
2119}
2120
Ariel Levkovich19cc7522017-04-03 13:11:03 +03002121static bool is_valid_ethertype(struct mlx5_core_dev *mdev,
2122 const struct ib_flow_attr *flow_attr,
Ariel Levkovich0f750962017-04-03 13:11:02 +03002123 bool check_inner)
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002124{
2125 union ib_flow_spec *ib_spec = (union ib_flow_spec *)(flow_attr + 1);
Ariel Levkovich19cc7522017-04-03 13:11:03 +03002126 int match_ipv = check_inner ?
2127 MLX5_CAP_FLOWTABLE_NIC_RX(mdev,
2128 ft_field_support.inner_ip_version) :
2129 MLX5_CAP_FLOWTABLE_NIC_RX(mdev,
2130 ft_field_support.outer_ip_version);
Ariel Levkovich0f750962017-04-03 13:11:02 +03002131 int inner_bit = check_inner ? IB_FLOW_SPEC_INNER : 0;
2132 bool ipv4_spec_valid, ipv6_spec_valid;
2133 unsigned int ip_spec_type = 0;
2134 bool has_ethertype = false;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002135 unsigned int spec_index;
Ariel Levkovich0f750962017-04-03 13:11:02 +03002136 bool mask_valid = true;
2137 u16 eth_type = 0;
2138 bool type_valid;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002139
2140 /* Validate that ethertype is correct */
2141 for (spec_index = 0; spec_index < flow_attr->num_of_specs; spec_index++) {
Ariel Levkovich0f750962017-04-03 13:11:02 +03002142 if ((ib_spec->type == (IB_FLOW_SPEC_ETH | inner_bit)) &&
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002143 ib_spec->eth.mask.ether_type) {
Ariel Levkovich0f750962017-04-03 13:11:02 +03002144 mask_valid = (ib_spec->eth.mask.ether_type ==
2145 htons(0xffff));
2146 has_ethertype = true;
2147 eth_type = ntohs(ib_spec->eth.val.ether_type);
2148 } else if ((ib_spec->type == (IB_FLOW_SPEC_IPV4 | inner_bit)) ||
2149 (ib_spec->type == (IB_FLOW_SPEC_IPV6 | inner_bit))) {
2150 ip_spec_type = ib_spec->type;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002151 }
2152 ib_spec = (void *)ib_spec + ib_spec->size;
2153 }
Ariel Levkovich0f750962017-04-03 13:11:02 +03002154
2155 type_valid = (!has_ethertype) || (!ip_spec_type);
2156 if (!type_valid && mask_valid) {
2157 ipv4_spec_valid = (eth_type == ETH_P_IP) &&
2158 (ip_spec_type == (IB_FLOW_SPEC_IPV4 | inner_bit));
2159 ipv6_spec_valid = (eth_type == ETH_P_IPV6) &&
2160 (ip_spec_type == (IB_FLOW_SPEC_IPV6 | inner_bit));
Ariel Levkovich19cc7522017-04-03 13:11:03 +03002161
2162 type_valid = (ipv4_spec_valid) || (ipv6_spec_valid) ||
2163 (((eth_type == ETH_P_MPLS_UC) ||
2164 (eth_type == ETH_P_MPLS_MC)) && match_ipv);
Ariel Levkovich0f750962017-04-03 13:11:02 +03002165 }
2166
2167 return type_valid;
2168}
2169
Ariel Levkovich19cc7522017-04-03 13:11:03 +03002170static bool is_valid_attr(struct mlx5_core_dev *mdev,
2171 const struct ib_flow_attr *flow_attr)
Ariel Levkovich0f750962017-04-03 13:11:02 +03002172{
Ariel Levkovich19cc7522017-04-03 13:11:03 +03002173 return is_valid_ethertype(mdev, flow_attr, false) &&
2174 is_valid_ethertype(mdev, flow_attr, true);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002175}
2176
2177static void put_flow_table(struct mlx5_ib_dev *dev,
2178 struct mlx5_ib_flow_prio *prio, bool ft_added)
2179{
2180 prio->refcount -= !!ft_added;
2181 if (!prio->refcount) {
2182 mlx5_destroy_flow_table(prio->flow_table);
2183 prio->flow_table = NULL;
2184 }
2185}
2186
2187static int mlx5_ib_destroy_flow(struct ib_flow *flow_id)
2188{
2189 struct mlx5_ib_dev *dev = to_mdev(flow_id->qp->device);
2190 struct mlx5_ib_flow_handler *handler = container_of(flow_id,
2191 struct mlx5_ib_flow_handler,
2192 ibflow);
2193 struct mlx5_ib_flow_handler *iter, *tmp;
2194
2195 mutex_lock(&dev->flow_db.lock);
2196
2197 list_for_each_entry_safe(iter, tmp, &handler->list, list) {
Mark Bloch74491de2016-08-31 11:24:25 +00002198 mlx5_del_flow_rules(iter->rule);
Maor Gottliebcc0e5d42016-08-28 14:16:34 +03002199 put_flow_table(dev, iter->prio, true);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002200 list_del(&iter->list);
2201 kfree(iter);
2202 }
2203
Mark Bloch74491de2016-08-31 11:24:25 +00002204 mlx5_del_flow_rules(handler->rule);
Maor Gottlieb5497adc2016-08-28 14:16:31 +03002205 put_flow_table(dev, handler->prio, true);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002206 mutex_unlock(&dev->flow_db.lock);
2207
2208 kfree(handler);
2209
2210 return 0;
2211}
2212
Maor Gottlieb35d190112016-03-07 18:51:47 +02002213static int ib_prio_to_core_prio(unsigned int priority, bool dont_trap)
2214{
2215 priority *= 2;
2216 if (!dont_trap)
2217 priority++;
2218 return priority;
2219}
2220
Maor Gottliebcc0e5d42016-08-28 14:16:34 +03002221enum flow_table_type {
2222 MLX5_IB_FT_RX,
2223 MLX5_IB_FT_TX
2224};
2225
Maor Gottlieb00b7c2a2017-03-29 06:09:01 +03002226#define MLX5_FS_MAX_TYPES 6
2227#define MLX5_FS_MAX_ENTRIES BIT(16)
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002228static struct mlx5_ib_flow_prio *get_flow_table(struct mlx5_ib_dev *dev,
Maor Gottliebcc0e5d42016-08-28 14:16:34 +03002229 struct ib_flow_attr *flow_attr,
2230 enum flow_table_type ft_type)
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002231{
Maor Gottlieb35d190112016-03-07 18:51:47 +02002232 bool dont_trap = flow_attr->flags & IB_FLOW_ATTR_FLAGS_DONT_TRAP;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002233 struct mlx5_flow_namespace *ns = NULL;
2234 struct mlx5_ib_flow_prio *prio;
2235 struct mlx5_flow_table *ft;
Maor Gottliebdac388e2017-03-29 06:09:00 +03002236 int max_table_size;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002237 int num_entries;
2238 int num_groups;
2239 int priority;
2240 int err = 0;
2241
Maor Gottliebdac388e2017-03-29 06:09:00 +03002242 max_table_size = BIT(MLX5_CAP_FLOWTABLE_NIC_RX(dev->mdev,
2243 log_max_ft_size));
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002244 if (flow_attr->type == IB_FLOW_ATTR_NORMAL) {
Maor Gottlieb35d190112016-03-07 18:51:47 +02002245 if (flow_is_multicast_only(flow_attr) &&
2246 !dont_trap)
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002247 priority = MLX5_IB_FLOW_MCAST_PRIO;
2248 else
Maor Gottlieb35d190112016-03-07 18:51:47 +02002249 priority = ib_prio_to_core_prio(flow_attr->priority,
2250 dont_trap);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002251 ns = mlx5_get_flow_namespace(dev->mdev,
2252 MLX5_FLOW_NAMESPACE_BYPASS);
2253 num_entries = MLX5_FS_MAX_ENTRIES;
2254 num_groups = MLX5_FS_MAX_TYPES;
2255 prio = &dev->flow_db.prios[priority];
2256 } else if (flow_attr->type == IB_FLOW_ATTR_ALL_DEFAULT ||
2257 flow_attr->type == IB_FLOW_ATTR_MC_DEFAULT) {
2258 ns = mlx5_get_flow_namespace(dev->mdev,
2259 MLX5_FLOW_NAMESPACE_LEFTOVERS);
2260 build_leftovers_ft_param(&priority,
2261 &num_entries,
2262 &num_groups);
2263 prio = &dev->flow_db.prios[MLX5_IB_FLOW_LEFTOVERS_PRIO];
Maor Gottliebcc0e5d42016-08-28 14:16:34 +03002264 } else if (flow_attr->type == IB_FLOW_ATTR_SNIFFER) {
2265 if (!MLX5_CAP_FLOWTABLE(dev->mdev,
2266 allow_sniffer_and_nic_rx_shared_tir))
2267 return ERR_PTR(-ENOTSUPP);
2268
2269 ns = mlx5_get_flow_namespace(dev->mdev, ft_type == MLX5_IB_FT_RX ?
2270 MLX5_FLOW_NAMESPACE_SNIFFER_RX :
2271 MLX5_FLOW_NAMESPACE_SNIFFER_TX);
2272
2273 prio = &dev->flow_db.sniffer[ft_type];
2274 priority = 0;
2275 num_entries = 1;
2276 num_groups = 1;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002277 }
2278
2279 if (!ns)
2280 return ERR_PTR(-ENOTSUPP);
2281
Maor Gottliebdac388e2017-03-29 06:09:00 +03002282 if (num_entries > max_table_size)
2283 return ERR_PTR(-ENOMEM);
2284
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002285 ft = prio->flow_table;
2286 if (!ft) {
2287 ft = mlx5_create_auto_grouped_flow_table(ns, priority,
2288 num_entries,
Maor Gottliebd63cd282016-04-29 01:36:35 +03002289 num_groups,
Hadar Hen Zionc9f1b072016-11-07 15:14:44 +02002290 0, 0);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002291
2292 if (!IS_ERR(ft)) {
2293 prio->refcount = 0;
2294 prio->flow_table = ft;
2295 } else {
2296 err = PTR_ERR(ft);
2297 }
2298 }
2299
2300 return err ? ERR_PTR(err) : prio;
2301}
2302
2303static struct mlx5_ib_flow_handler *create_flow_rule(struct mlx5_ib_dev *dev,
2304 struct mlx5_ib_flow_prio *ft_prio,
Maor Gottliebdd063d02016-08-28 14:16:32 +03002305 const struct ib_flow_attr *flow_attr,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002306 struct mlx5_flow_destination *dst)
2307{
2308 struct mlx5_flow_table *ft = ft_prio->flow_table;
2309 struct mlx5_ib_flow_handler *handler;
Hadar Hen Zion66958ed2016-11-07 15:14:45 +02002310 struct mlx5_flow_act flow_act = {0};
Maor Gottliebc5bb1732016-07-04 17:23:05 +03002311 struct mlx5_flow_spec *spec;
Slava Shwartsmana22ed862017-04-03 13:13:52 +03002312 struct mlx5_flow_destination *rule_dst = dst;
Maor Gottliebdd063d02016-08-28 14:16:32 +03002313 const void *ib_flow = (const void *)flow_attr + sizeof(*flow_attr);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002314 unsigned int spec_index;
Moses Reuben2ac693f2017-01-18 14:59:50 +02002315 u32 flow_tag = MLX5_FS_DEFAULT_FLOW_TAG;
Slava Shwartsmana22ed862017-04-03 13:13:52 +03002316 bool is_drop = false;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002317 int err = 0;
Slava Shwartsmana22ed862017-04-03 13:13:52 +03002318 int dest_num = 1;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002319
Ariel Levkovich19cc7522017-04-03 13:11:03 +03002320 if (!is_valid_attr(dev->mdev, flow_attr))
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002321 return ERR_PTR(-EINVAL);
2322
Leon Romanovsky1b9a07e2017-05-10 21:32:18 +03002323 spec = kvzalloc(sizeof(*spec), GFP_KERNEL);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002324 handler = kzalloc(sizeof(*handler), GFP_KERNEL);
Maor Gottliebc5bb1732016-07-04 17:23:05 +03002325 if (!handler || !spec) {
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002326 err = -ENOMEM;
2327 goto free;
2328 }
2329
2330 INIT_LIST_HEAD(&handler->list);
2331
2332 for (spec_index = 0; spec_index < flow_attr->num_of_specs; spec_index++) {
Ariel Levkovich19cc7522017-04-03 13:11:03 +03002333 err = parse_flow_attr(dev->mdev, spec->match_criteria,
Slava Shwartsmana22ed862017-04-03 13:13:52 +03002334 spec->match_value,
2335 ib_flow, &flow_tag, &is_drop);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002336 if (err < 0)
2337 goto free;
2338
2339 ib_flow += ((union ib_flow_spec *)ib_flow)->size;
2340 }
2341
Maor Gottlieb466fa6d2016-08-30 16:58:36 +03002342 spec->match_criteria_enable = get_match_criteria_enable(spec->match_criteria);
Slava Shwartsmana22ed862017-04-03 13:13:52 +03002343 if (is_drop) {
2344 flow_act.action = MLX5_FLOW_CONTEXT_ACTION_DROP;
2345 rule_dst = NULL;
2346 dest_num = 0;
2347 } else {
2348 flow_act.action = dst ? MLX5_FLOW_CONTEXT_ACTION_FWD_DEST :
2349 MLX5_FLOW_CONTEXT_ACTION_FWD_NEXT_PRIO;
2350 }
Moses Reuben2ac693f2017-01-18 14:59:50 +02002351
2352 if (flow_tag != MLX5_FS_DEFAULT_FLOW_TAG &&
2353 (flow_attr->type == IB_FLOW_ATTR_ALL_DEFAULT ||
2354 flow_attr->type == IB_FLOW_ATTR_MC_DEFAULT)) {
2355 mlx5_ib_warn(dev, "Flow tag %u and attribute type %x isn't allowed in leftovers\n",
2356 flow_tag, flow_attr->type);
2357 err = -EINVAL;
2358 goto free;
2359 }
2360 flow_act.flow_tag = flow_tag;
Mark Bloch74491de2016-08-31 11:24:25 +00002361 handler->rule = mlx5_add_flow_rules(ft, spec,
Hadar Hen Zion66958ed2016-11-07 15:14:45 +02002362 &flow_act,
Slava Shwartsmana22ed862017-04-03 13:13:52 +03002363 rule_dst, dest_num);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002364
2365 if (IS_ERR(handler->rule)) {
2366 err = PTR_ERR(handler->rule);
2367 goto free;
2368 }
2369
Maor Gottliebd9d49802016-08-28 14:16:33 +03002370 ft_prio->refcount++;
Maor Gottlieb5497adc2016-08-28 14:16:31 +03002371 handler->prio = ft_prio;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002372
2373 ft_prio->flow_table = ft;
2374free:
2375 if (err)
2376 kfree(handler);
Maor Gottliebc5bb1732016-07-04 17:23:05 +03002377 kvfree(spec);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002378 return err ? ERR_PTR(err) : handler;
2379}
2380
Maor Gottlieb35d190112016-03-07 18:51:47 +02002381static struct mlx5_ib_flow_handler *create_dont_trap_rule(struct mlx5_ib_dev *dev,
2382 struct mlx5_ib_flow_prio *ft_prio,
2383 struct ib_flow_attr *flow_attr,
2384 struct mlx5_flow_destination *dst)
2385{
2386 struct mlx5_ib_flow_handler *handler_dst = NULL;
2387 struct mlx5_ib_flow_handler *handler = NULL;
2388
2389 handler = create_flow_rule(dev, ft_prio, flow_attr, NULL);
2390 if (!IS_ERR(handler)) {
2391 handler_dst = create_flow_rule(dev, ft_prio,
2392 flow_attr, dst);
2393 if (IS_ERR(handler_dst)) {
Mark Bloch74491de2016-08-31 11:24:25 +00002394 mlx5_del_flow_rules(handler->rule);
Maor Gottliebd9d49802016-08-28 14:16:33 +03002395 ft_prio->refcount--;
Maor Gottlieb35d190112016-03-07 18:51:47 +02002396 kfree(handler);
2397 handler = handler_dst;
2398 } else {
2399 list_add(&handler_dst->list, &handler->list);
2400 }
2401 }
2402
2403 return handler;
2404}
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002405enum {
2406 LEFTOVERS_MC,
2407 LEFTOVERS_UC,
2408};
2409
2410static struct mlx5_ib_flow_handler *create_leftovers_rule(struct mlx5_ib_dev *dev,
2411 struct mlx5_ib_flow_prio *ft_prio,
2412 struct ib_flow_attr *flow_attr,
2413 struct mlx5_flow_destination *dst)
2414{
2415 struct mlx5_ib_flow_handler *handler_ucast = NULL;
2416 struct mlx5_ib_flow_handler *handler = NULL;
2417
2418 static struct {
2419 struct ib_flow_attr flow_attr;
2420 struct ib_flow_spec_eth eth_flow;
2421 } leftovers_specs[] = {
2422 [LEFTOVERS_MC] = {
2423 .flow_attr = {
2424 .num_of_specs = 1,
2425 .size = sizeof(leftovers_specs[0])
2426 },
2427 .eth_flow = {
2428 .type = IB_FLOW_SPEC_ETH,
2429 .size = sizeof(struct ib_flow_spec_eth),
2430 .mask = {.dst_mac = {0x1} },
2431 .val = {.dst_mac = {0x1} }
2432 }
2433 },
2434 [LEFTOVERS_UC] = {
2435 .flow_attr = {
2436 .num_of_specs = 1,
2437 .size = sizeof(leftovers_specs[0])
2438 },
2439 .eth_flow = {
2440 .type = IB_FLOW_SPEC_ETH,
2441 .size = sizeof(struct ib_flow_spec_eth),
2442 .mask = {.dst_mac = {0x1} },
2443 .val = {.dst_mac = {} }
2444 }
2445 }
2446 };
2447
2448 handler = create_flow_rule(dev, ft_prio,
2449 &leftovers_specs[LEFTOVERS_MC].flow_attr,
2450 dst);
2451 if (!IS_ERR(handler) &&
2452 flow_attr->type == IB_FLOW_ATTR_ALL_DEFAULT) {
2453 handler_ucast = create_flow_rule(dev, ft_prio,
2454 &leftovers_specs[LEFTOVERS_UC].flow_attr,
2455 dst);
2456 if (IS_ERR(handler_ucast)) {
Mark Bloch74491de2016-08-31 11:24:25 +00002457 mlx5_del_flow_rules(handler->rule);
Maor Gottliebd9d49802016-08-28 14:16:33 +03002458 ft_prio->refcount--;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002459 kfree(handler);
2460 handler = handler_ucast;
2461 } else {
2462 list_add(&handler_ucast->list, &handler->list);
2463 }
2464 }
2465
2466 return handler;
2467}
2468
Maor Gottliebcc0e5d42016-08-28 14:16:34 +03002469static struct mlx5_ib_flow_handler *create_sniffer_rule(struct mlx5_ib_dev *dev,
2470 struct mlx5_ib_flow_prio *ft_rx,
2471 struct mlx5_ib_flow_prio *ft_tx,
2472 struct mlx5_flow_destination *dst)
2473{
2474 struct mlx5_ib_flow_handler *handler_rx;
2475 struct mlx5_ib_flow_handler *handler_tx;
2476 int err;
2477 static const struct ib_flow_attr flow_attr = {
2478 .num_of_specs = 0,
2479 .size = sizeof(flow_attr)
2480 };
2481
2482 handler_rx = create_flow_rule(dev, ft_rx, &flow_attr, dst);
2483 if (IS_ERR(handler_rx)) {
2484 err = PTR_ERR(handler_rx);
2485 goto err;
2486 }
2487
2488 handler_tx = create_flow_rule(dev, ft_tx, &flow_attr, dst);
2489 if (IS_ERR(handler_tx)) {
2490 err = PTR_ERR(handler_tx);
2491 goto err_tx;
2492 }
2493
2494 list_add(&handler_tx->list, &handler_rx->list);
2495
2496 return handler_rx;
2497
2498err_tx:
Mark Bloch74491de2016-08-31 11:24:25 +00002499 mlx5_del_flow_rules(handler_rx->rule);
Maor Gottliebcc0e5d42016-08-28 14:16:34 +03002500 ft_rx->refcount--;
2501 kfree(handler_rx);
2502err:
2503 return ERR_PTR(err);
2504}
2505
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002506static struct ib_flow *mlx5_ib_create_flow(struct ib_qp *qp,
2507 struct ib_flow_attr *flow_attr,
2508 int domain)
2509{
2510 struct mlx5_ib_dev *dev = to_mdev(qp->device);
Yishai Hadasd9f88e52016-08-28 10:58:37 +03002511 struct mlx5_ib_qp *mqp = to_mqp(qp);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002512 struct mlx5_ib_flow_handler *handler = NULL;
2513 struct mlx5_flow_destination *dst = NULL;
Maor Gottliebcc0e5d42016-08-28 14:16:34 +03002514 struct mlx5_ib_flow_prio *ft_prio_tx = NULL;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002515 struct mlx5_ib_flow_prio *ft_prio;
2516 int err;
2517
2518 if (flow_attr->priority > MLX5_IB_FLOW_LAST_PRIO)
Maor Gottliebdac388e2017-03-29 06:09:00 +03002519 return ERR_PTR(-ENOMEM);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002520
2521 if (domain != IB_FLOW_DOMAIN_USER ||
2522 flow_attr->port > MLX5_CAP_GEN(dev->mdev, num_ports) ||
Maor Gottlieb35d190112016-03-07 18:51:47 +02002523 (flow_attr->flags & ~IB_FLOW_ATTR_FLAGS_DONT_TRAP))
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002524 return ERR_PTR(-EINVAL);
2525
2526 dst = kzalloc(sizeof(*dst), GFP_KERNEL);
2527 if (!dst)
2528 return ERR_PTR(-ENOMEM);
2529
2530 mutex_lock(&dev->flow_db.lock);
2531
Maor Gottliebcc0e5d42016-08-28 14:16:34 +03002532 ft_prio = get_flow_table(dev, flow_attr, MLX5_IB_FT_RX);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002533 if (IS_ERR(ft_prio)) {
2534 err = PTR_ERR(ft_prio);
2535 goto unlock;
2536 }
Maor Gottliebcc0e5d42016-08-28 14:16:34 +03002537 if (flow_attr->type == IB_FLOW_ATTR_SNIFFER) {
2538 ft_prio_tx = get_flow_table(dev, flow_attr, MLX5_IB_FT_TX);
2539 if (IS_ERR(ft_prio_tx)) {
2540 err = PTR_ERR(ft_prio_tx);
2541 ft_prio_tx = NULL;
2542 goto destroy_ft;
2543 }
2544 }
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002545
2546 dst->type = MLX5_FLOW_DESTINATION_TYPE_TIR;
Yishai Hadasd9f88e52016-08-28 10:58:37 +03002547 if (mqp->flags & MLX5_IB_QP_RSS)
2548 dst->tir_num = mqp->rss_qp.tirn;
2549 else
2550 dst->tir_num = mqp->raw_packet_qp.rq.tirn;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002551
2552 if (flow_attr->type == IB_FLOW_ATTR_NORMAL) {
Maor Gottlieb35d190112016-03-07 18:51:47 +02002553 if (flow_attr->flags & IB_FLOW_ATTR_FLAGS_DONT_TRAP) {
2554 handler = create_dont_trap_rule(dev, ft_prio,
2555 flow_attr, dst);
2556 } else {
2557 handler = create_flow_rule(dev, ft_prio, flow_attr,
2558 dst);
2559 }
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002560 } else if (flow_attr->type == IB_FLOW_ATTR_ALL_DEFAULT ||
2561 flow_attr->type == IB_FLOW_ATTR_MC_DEFAULT) {
2562 handler = create_leftovers_rule(dev, ft_prio, flow_attr,
2563 dst);
Maor Gottliebcc0e5d42016-08-28 14:16:34 +03002564 } else if (flow_attr->type == IB_FLOW_ATTR_SNIFFER) {
2565 handler = create_sniffer_rule(dev, ft_prio, ft_prio_tx, dst);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002566 } else {
2567 err = -EINVAL;
2568 goto destroy_ft;
2569 }
2570
2571 if (IS_ERR(handler)) {
2572 err = PTR_ERR(handler);
2573 handler = NULL;
2574 goto destroy_ft;
2575 }
2576
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002577 mutex_unlock(&dev->flow_db.lock);
2578 kfree(dst);
2579
2580 return &handler->ibflow;
2581
2582destroy_ft:
2583 put_flow_table(dev, ft_prio, false);
Maor Gottliebcc0e5d42016-08-28 14:16:34 +03002584 if (ft_prio_tx)
2585 put_flow_table(dev, ft_prio_tx, false);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002586unlock:
2587 mutex_unlock(&dev->flow_db.lock);
2588 kfree(dst);
2589 kfree(handler);
2590 return ERR_PTR(err);
2591}
2592
Eli Cohene126ba92013-07-07 17:25:49 +03002593static int mlx5_ib_mcg_attach(struct ib_qp *ibqp, union ib_gid *gid, u16 lid)
2594{
2595 struct mlx5_ib_dev *dev = to_mdev(ibqp->device);
2596 int err;
2597
Jack Morgenstein9603b612014-07-28 23:30:22 +03002598 err = mlx5_core_attach_mcg(dev->mdev, gid, ibqp->qp_num);
Eli Cohene126ba92013-07-07 17:25:49 +03002599 if (err)
2600 mlx5_ib_warn(dev, "failed attaching QPN 0x%x, MGID %pI6\n",
2601 ibqp->qp_num, gid->raw);
2602
2603 return err;
2604}
2605
2606static int mlx5_ib_mcg_detach(struct ib_qp *ibqp, union ib_gid *gid, u16 lid)
2607{
2608 struct mlx5_ib_dev *dev = to_mdev(ibqp->device);
2609 int err;
2610
Jack Morgenstein9603b612014-07-28 23:30:22 +03002611 err = mlx5_core_detach_mcg(dev->mdev, gid, ibqp->qp_num);
Eli Cohene126ba92013-07-07 17:25:49 +03002612 if (err)
2613 mlx5_ib_warn(dev, "failed detaching QPN 0x%x, MGID %pI6\n",
2614 ibqp->qp_num, gid->raw);
2615
2616 return err;
2617}
2618
2619static int init_node_data(struct mlx5_ib_dev *dev)
2620{
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03002621 int err;
Eli Cohene126ba92013-07-07 17:25:49 +03002622
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03002623 err = mlx5_query_node_desc(dev, dev->ib_dev.node_desc);
Eli Cohene126ba92013-07-07 17:25:49 +03002624 if (err)
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03002625 return err;
Eli Cohene126ba92013-07-07 17:25:49 +03002626
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03002627 dev->mdev->rev_id = dev->mdev->pdev->revision;
Eli Cohene126ba92013-07-07 17:25:49 +03002628
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03002629 return mlx5_query_node_guid(dev, &dev->ib_dev.node_guid);
Eli Cohene126ba92013-07-07 17:25:49 +03002630}
2631
2632static ssize_t show_fw_pages(struct device *device, struct device_attribute *attr,
2633 char *buf)
2634{
2635 struct mlx5_ib_dev *dev =
2636 container_of(device, struct mlx5_ib_dev, ib_dev.dev);
2637
Jack Morgenstein9603b612014-07-28 23:30:22 +03002638 return sprintf(buf, "%d\n", dev->mdev->priv.fw_pages);
Eli Cohene126ba92013-07-07 17:25:49 +03002639}
2640
2641static ssize_t show_reg_pages(struct device *device,
2642 struct device_attribute *attr, char *buf)
2643{
2644 struct mlx5_ib_dev *dev =
2645 container_of(device, struct mlx5_ib_dev, ib_dev.dev);
2646
Haggai Eran6aec21f2014-12-11 17:04:23 +02002647 return sprintf(buf, "%d\n", atomic_read(&dev->mdev->priv.reg_pages));
Eli Cohene126ba92013-07-07 17:25:49 +03002648}
2649
2650static ssize_t show_hca(struct device *device, struct device_attribute *attr,
2651 char *buf)
2652{
2653 struct mlx5_ib_dev *dev =
2654 container_of(device, struct mlx5_ib_dev, ib_dev.dev);
Jack Morgenstein9603b612014-07-28 23:30:22 +03002655 return sprintf(buf, "MT%d\n", dev->mdev->pdev->device);
Eli Cohene126ba92013-07-07 17:25:49 +03002656}
2657
Eli Cohene126ba92013-07-07 17:25:49 +03002658static ssize_t show_rev(struct device *device, struct device_attribute *attr,
2659 char *buf)
2660{
2661 struct mlx5_ib_dev *dev =
2662 container_of(device, struct mlx5_ib_dev, ib_dev.dev);
Jack Morgenstein9603b612014-07-28 23:30:22 +03002663 return sprintf(buf, "%x\n", dev->mdev->rev_id);
Eli Cohene126ba92013-07-07 17:25:49 +03002664}
2665
2666static ssize_t show_board(struct device *device, struct device_attribute *attr,
2667 char *buf)
2668{
2669 struct mlx5_ib_dev *dev =
2670 container_of(device, struct mlx5_ib_dev, ib_dev.dev);
2671 return sprintf(buf, "%.*s\n", MLX5_BOARD_ID_LEN,
Jack Morgenstein9603b612014-07-28 23:30:22 +03002672 dev->mdev->board_id);
Eli Cohene126ba92013-07-07 17:25:49 +03002673}
2674
2675static DEVICE_ATTR(hw_rev, S_IRUGO, show_rev, NULL);
Eli Cohene126ba92013-07-07 17:25:49 +03002676static DEVICE_ATTR(hca_type, S_IRUGO, show_hca, NULL);
2677static DEVICE_ATTR(board_id, S_IRUGO, show_board, NULL);
2678static DEVICE_ATTR(fw_pages, S_IRUGO, show_fw_pages, NULL);
2679static DEVICE_ATTR(reg_pages, S_IRUGO, show_reg_pages, NULL);
2680
2681static struct device_attribute *mlx5_class_attributes[] = {
2682 &dev_attr_hw_rev,
Eli Cohene126ba92013-07-07 17:25:49 +03002683 &dev_attr_hca_type,
2684 &dev_attr_board_id,
2685 &dev_attr_fw_pages,
2686 &dev_attr_reg_pages,
2687};
2688
Haggai Eran7722f472016-02-29 15:45:07 +02002689static void pkey_change_handler(struct work_struct *work)
2690{
2691 struct mlx5_ib_port_resources *ports =
2692 container_of(work, struct mlx5_ib_port_resources,
2693 pkey_change_work);
2694
2695 mutex_lock(&ports->devr->mutex);
2696 mlx5_ib_gsi_pkey_change(ports->gsi);
2697 mutex_unlock(&ports->devr->mutex);
2698}
2699
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03002700static void mlx5_ib_handle_internal_error(struct mlx5_ib_dev *ibdev)
2701{
2702 struct mlx5_ib_qp *mqp;
2703 struct mlx5_ib_cq *send_mcq, *recv_mcq;
2704 struct mlx5_core_cq *mcq;
2705 struct list_head cq_armed_list;
2706 unsigned long flags_qp;
2707 unsigned long flags_cq;
2708 unsigned long flags;
2709
2710 INIT_LIST_HEAD(&cq_armed_list);
2711
2712 /* Go over qp list reside on that ibdev, sync with create/destroy qp.*/
2713 spin_lock_irqsave(&ibdev->reset_flow_resource_lock, flags);
2714 list_for_each_entry(mqp, &ibdev->qp_list, qps_list) {
2715 spin_lock_irqsave(&mqp->sq.lock, flags_qp);
2716 if (mqp->sq.tail != mqp->sq.head) {
2717 send_mcq = to_mcq(mqp->ibqp.send_cq);
2718 spin_lock_irqsave(&send_mcq->lock, flags_cq);
2719 if (send_mcq->mcq.comp &&
2720 mqp->ibqp.send_cq->comp_handler) {
2721 if (!send_mcq->mcq.reset_notify_added) {
2722 send_mcq->mcq.reset_notify_added = 1;
2723 list_add_tail(&send_mcq->mcq.reset_notify,
2724 &cq_armed_list);
2725 }
2726 }
2727 spin_unlock_irqrestore(&send_mcq->lock, flags_cq);
2728 }
2729 spin_unlock_irqrestore(&mqp->sq.lock, flags_qp);
2730 spin_lock_irqsave(&mqp->rq.lock, flags_qp);
2731 /* no handling is needed for SRQ */
2732 if (!mqp->ibqp.srq) {
2733 if (mqp->rq.tail != mqp->rq.head) {
2734 recv_mcq = to_mcq(mqp->ibqp.recv_cq);
2735 spin_lock_irqsave(&recv_mcq->lock, flags_cq);
2736 if (recv_mcq->mcq.comp &&
2737 mqp->ibqp.recv_cq->comp_handler) {
2738 if (!recv_mcq->mcq.reset_notify_added) {
2739 recv_mcq->mcq.reset_notify_added = 1;
2740 list_add_tail(&recv_mcq->mcq.reset_notify,
2741 &cq_armed_list);
2742 }
2743 }
2744 spin_unlock_irqrestore(&recv_mcq->lock,
2745 flags_cq);
2746 }
2747 }
2748 spin_unlock_irqrestore(&mqp->rq.lock, flags_qp);
2749 }
2750 /*At that point all inflight post send were put to be executed as of we
2751 * lock/unlock above locks Now need to arm all involved CQs.
2752 */
2753 list_for_each_entry(mcq, &cq_armed_list, reset_notify) {
2754 mcq->comp(mcq);
2755 }
2756 spin_unlock_irqrestore(&ibdev->reset_flow_resource_lock, flags);
2757}
2758
Maor Gottlieb03404e82017-05-30 10:29:13 +03002759static void delay_drop_handler(struct work_struct *work)
2760{
2761 int err;
2762 struct mlx5_ib_delay_drop *delay_drop =
2763 container_of(work, struct mlx5_ib_delay_drop,
2764 delay_drop_work);
2765
2766 mutex_lock(&delay_drop->lock);
2767 err = mlx5_core_set_delay_drop(delay_drop->dev->mdev,
2768 delay_drop->timeout);
2769 if (err) {
2770 mlx5_ib_warn(delay_drop->dev, "Failed to set delay drop, timeout=%u\n",
2771 delay_drop->timeout);
2772 delay_drop->activate = false;
2773 }
2774 mutex_unlock(&delay_drop->lock);
2775}
2776
Jack Morgenstein9603b612014-07-28 23:30:22 +03002777static void mlx5_ib_event(struct mlx5_core_dev *dev, void *context,
Jack Morgenstein4d2f9bb2014-07-28 23:30:24 +03002778 enum mlx5_dev_event event, unsigned long param)
Eli Cohene126ba92013-07-07 17:25:49 +03002779{
Jack Morgenstein9603b612014-07-28 23:30:22 +03002780 struct mlx5_ib_dev *ibdev = (struct mlx5_ib_dev *)context;
Eli Cohene126ba92013-07-07 17:25:49 +03002781 struct ib_event ibev;
Eli Cohendbaaff22016-10-27 16:36:44 +03002782 bool fatal = false;
Eli Cohene126ba92013-07-07 17:25:49 +03002783 u8 port = 0;
2784
2785 switch (event) {
2786 case MLX5_DEV_EVENT_SYS_ERROR:
Eli Cohene126ba92013-07-07 17:25:49 +03002787 ibev.event = IB_EVENT_DEVICE_FATAL;
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03002788 mlx5_ib_handle_internal_error(ibdev);
Eli Cohendbaaff22016-10-27 16:36:44 +03002789 fatal = true;
Eli Cohene126ba92013-07-07 17:25:49 +03002790 break;
2791
2792 case MLX5_DEV_EVENT_PORT_UP:
Eli Cohene126ba92013-07-07 17:25:49 +03002793 case MLX5_DEV_EVENT_PORT_DOWN:
Noa Osherovich2788cf32016-06-04 15:15:29 +03002794 case MLX5_DEV_EVENT_PORT_INITIALIZED:
Jack Morgenstein4d2f9bb2014-07-28 23:30:24 +03002795 port = (u8)param;
Aviv Heller5ec8c832016-09-18 20:48:00 +03002796
2797 /* In RoCE, port up/down events are handled in
2798 * mlx5_netdev_event().
2799 */
2800 if (mlx5_ib_port_link_layer(&ibdev->ib_dev, port) ==
2801 IB_LINK_LAYER_ETHERNET)
2802 return;
2803
2804 ibev.event = (event == MLX5_DEV_EVENT_PORT_UP) ?
2805 IB_EVENT_PORT_ACTIVE : IB_EVENT_PORT_ERR;
Eli Cohene126ba92013-07-07 17:25:49 +03002806 break;
2807
Eli Cohene126ba92013-07-07 17:25:49 +03002808 case MLX5_DEV_EVENT_LID_CHANGE:
2809 ibev.event = IB_EVENT_LID_CHANGE;
Jack Morgenstein4d2f9bb2014-07-28 23:30:24 +03002810 port = (u8)param;
Eli Cohene126ba92013-07-07 17:25:49 +03002811 break;
2812
2813 case MLX5_DEV_EVENT_PKEY_CHANGE:
2814 ibev.event = IB_EVENT_PKEY_CHANGE;
Jack Morgenstein4d2f9bb2014-07-28 23:30:24 +03002815 port = (u8)param;
Haggai Eran7722f472016-02-29 15:45:07 +02002816
2817 schedule_work(&ibdev->devr.ports[port - 1].pkey_change_work);
Eli Cohene126ba92013-07-07 17:25:49 +03002818 break;
2819
2820 case MLX5_DEV_EVENT_GUID_CHANGE:
2821 ibev.event = IB_EVENT_GID_CHANGE;
Jack Morgenstein4d2f9bb2014-07-28 23:30:24 +03002822 port = (u8)param;
Eli Cohene126ba92013-07-07 17:25:49 +03002823 break;
2824
2825 case MLX5_DEV_EVENT_CLIENT_REREG:
2826 ibev.event = IB_EVENT_CLIENT_REREGISTER;
Jack Morgenstein4d2f9bb2014-07-28 23:30:24 +03002827 port = (u8)param;
Eli Cohene126ba92013-07-07 17:25:49 +03002828 break;
Maor Gottlieb03404e82017-05-30 10:29:13 +03002829 case MLX5_DEV_EVENT_DELAY_DROP_TIMEOUT:
2830 schedule_work(&ibdev->delay_drop.delay_drop_work);
2831 goto out;
Saeed Mahameedbdc37922016-09-29 19:35:38 +03002832 default:
Maor Gottlieb03404e82017-05-30 10:29:13 +03002833 goto out;
Eli Cohene126ba92013-07-07 17:25:49 +03002834 }
2835
2836 ibev.device = &ibdev->ib_dev;
2837 ibev.element.port_num = port;
2838
Eli Cohena0c84c32013-09-11 16:35:27 +03002839 if (port < 1 || port > ibdev->num_ports) {
2840 mlx5_ib_warn(ibdev, "warning: event on port %d\n", port);
Maor Gottlieb03404e82017-05-30 10:29:13 +03002841 goto out;
Eli Cohena0c84c32013-09-11 16:35:27 +03002842 }
2843
Eli Cohene126ba92013-07-07 17:25:49 +03002844 if (ibdev->ib_active)
2845 ib_dispatch_event(&ibev);
Eli Cohendbaaff22016-10-27 16:36:44 +03002846
2847 if (fatal)
2848 ibdev->ib_active = false;
Maor Gottlieb03404e82017-05-30 10:29:13 +03002849
2850out:
2851 return;
Eli Cohene126ba92013-07-07 17:25:49 +03002852}
2853
Maor Gottliebc43f1112017-01-18 14:10:33 +02002854static int set_has_smi_cap(struct mlx5_ib_dev *dev)
2855{
2856 struct mlx5_hca_vport_context vport_ctx;
2857 int err;
2858 int port;
2859
2860 for (port = 1; port <= MLX5_CAP_GEN(dev->mdev, num_ports); port++) {
2861 dev->mdev->port_caps[port - 1].has_smi = false;
2862 if (MLX5_CAP_GEN(dev->mdev, port_type) ==
2863 MLX5_CAP_PORT_TYPE_IB) {
2864 if (MLX5_CAP_GEN(dev->mdev, ib_virt)) {
2865 err = mlx5_query_hca_vport_context(dev->mdev, 0,
2866 port, 0,
2867 &vport_ctx);
2868 if (err) {
2869 mlx5_ib_err(dev, "query_hca_vport_context for port=%d failed %d\n",
2870 port, err);
2871 return err;
2872 }
2873 dev->mdev->port_caps[port - 1].has_smi =
2874 vport_ctx.has_smi;
2875 } else {
2876 dev->mdev->port_caps[port - 1].has_smi = true;
2877 }
2878 }
2879 }
2880 return 0;
2881}
2882
Eli Cohene126ba92013-07-07 17:25:49 +03002883static void get_ext_port_caps(struct mlx5_ib_dev *dev)
2884{
2885 int port;
2886
Saeed Mahameed938fe832015-05-28 22:28:41 +03002887 for (port = 1; port <= MLX5_CAP_GEN(dev->mdev, num_ports); port++)
Eli Cohene126ba92013-07-07 17:25:49 +03002888 mlx5_query_ext_port_caps(dev, port);
2889}
2890
2891static int get_port_caps(struct mlx5_ib_dev *dev)
2892{
2893 struct ib_device_attr *dprops = NULL;
2894 struct ib_port_attr *pprops = NULL;
Dan Carpenterf614fc12015-01-12 11:56:58 +03002895 int err = -ENOMEM;
Eli Cohene126ba92013-07-07 17:25:49 +03002896 int port;
Matan Barak2528e332015-06-11 16:35:25 +03002897 struct ib_udata uhw = {.inlen = 0, .outlen = 0};
Eli Cohene126ba92013-07-07 17:25:49 +03002898
2899 pprops = kmalloc(sizeof(*pprops), GFP_KERNEL);
2900 if (!pprops)
2901 goto out;
2902
2903 dprops = kmalloc(sizeof(*dprops), GFP_KERNEL);
2904 if (!dprops)
2905 goto out;
2906
Maor Gottliebc43f1112017-01-18 14:10:33 +02002907 err = set_has_smi_cap(dev);
2908 if (err)
2909 goto out;
2910
Matan Barak2528e332015-06-11 16:35:25 +03002911 err = mlx5_ib_query_device(&dev->ib_dev, dprops, &uhw);
Eli Cohene126ba92013-07-07 17:25:49 +03002912 if (err) {
2913 mlx5_ib_warn(dev, "query_device failed %d\n", err);
2914 goto out;
2915 }
2916
Saeed Mahameed938fe832015-05-28 22:28:41 +03002917 for (port = 1; port <= MLX5_CAP_GEN(dev->mdev, num_ports); port++) {
Or Gerlitzc4550c62017-01-24 13:02:39 +02002918 memset(pprops, 0, sizeof(*pprops));
Eli Cohene126ba92013-07-07 17:25:49 +03002919 err = mlx5_ib_query_port(&dev->ib_dev, port, pprops);
2920 if (err) {
Saeed Mahameed938fe832015-05-28 22:28:41 +03002921 mlx5_ib_warn(dev, "query_port %d failed %d\n",
2922 port, err);
Eli Cohene126ba92013-07-07 17:25:49 +03002923 break;
2924 }
Saeed Mahameed938fe832015-05-28 22:28:41 +03002925 dev->mdev->port_caps[port - 1].pkey_table_len =
2926 dprops->max_pkeys;
2927 dev->mdev->port_caps[port - 1].gid_table_len =
2928 pprops->gid_tbl_len;
Eli Cohene126ba92013-07-07 17:25:49 +03002929 mlx5_ib_dbg(dev, "pkey_table_len %d, gid_table_len %d\n",
2930 dprops->max_pkeys, pprops->gid_tbl_len);
2931 }
2932
2933out:
2934 kfree(pprops);
2935 kfree(dprops);
2936
2937 return err;
2938}
2939
2940static void destroy_umrc_res(struct mlx5_ib_dev *dev)
2941{
2942 int err;
2943
2944 err = mlx5_mr_cache_cleanup(dev);
2945 if (err)
2946 mlx5_ib_warn(dev, "mr cache cleanup failed\n");
2947
2948 mlx5_ib_destroy_qp(dev->umrc.qp);
Christoph Hellwigadd08d72016-03-03 09:38:22 +01002949 ib_free_cq(dev->umrc.cq);
Eli Cohene126ba92013-07-07 17:25:49 +03002950 ib_dealloc_pd(dev->umrc.pd);
2951}
2952
2953enum {
2954 MAX_UMR_WR = 128,
2955};
2956
2957static int create_umr_res(struct mlx5_ib_dev *dev)
2958{
2959 struct ib_qp_init_attr *init_attr = NULL;
2960 struct ib_qp_attr *attr = NULL;
2961 struct ib_pd *pd;
2962 struct ib_cq *cq;
2963 struct ib_qp *qp;
Eli Cohene126ba92013-07-07 17:25:49 +03002964 int ret;
2965
2966 attr = kzalloc(sizeof(*attr), GFP_KERNEL);
2967 init_attr = kzalloc(sizeof(*init_attr), GFP_KERNEL);
2968 if (!attr || !init_attr) {
2969 ret = -ENOMEM;
2970 goto error_0;
2971 }
2972
Christoph Hellwiged082d32016-09-05 12:56:17 +02002973 pd = ib_alloc_pd(&dev->ib_dev, 0);
Eli Cohene126ba92013-07-07 17:25:49 +03002974 if (IS_ERR(pd)) {
2975 mlx5_ib_dbg(dev, "Couldn't create PD for sync UMR QP\n");
2976 ret = PTR_ERR(pd);
2977 goto error_0;
2978 }
2979
Christoph Hellwigadd08d72016-03-03 09:38:22 +01002980 cq = ib_alloc_cq(&dev->ib_dev, NULL, 128, 0, IB_POLL_SOFTIRQ);
Eli Cohene126ba92013-07-07 17:25:49 +03002981 if (IS_ERR(cq)) {
2982 mlx5_ib_dbg(dev, "Couldn't create CQ for sync UMR QP\n");
2983 ret = PTR_ERR(cq);
2984 goto error_2;
2985 }
Eli Cohene126ba92013-07-07 17:25:49 +03002986
2987 init_attr->send_cq = cq;
2988 init_attr->recv_cq = cq;
2989 init_attr->sq_sig_type = IB_SIGNAL_ALL_WR;
2990 init_attr->cap.max_send_wr = MAX_UMR_WR;
2991 init_attr->cap.max_send_sge = 1;
2992 init_attr->qp_type = MLX5_IB_QPT_REG_UMR;
2993 init_attr->port_num = 1;
2994 qp = mlx5_ib_create_qp(pd, init_attr, NULL);
2995 if (IS_ERR(qp)) {
2996 mlx5_ib_dbg(dev, "Couldn't create sync UMR QP\n");
2997 ret = PTR_ERR(qp);
2998 goto error_3;
2999 }
3000 qp->device = &dev->ib_dev;
3001 qp->real_qp = qp;
3002 qp->uobject = NULL;
3003 qp->qp_type = MLX5_IB_QPT_REG_UMR;
3004
3005 attr->qp_state = IB_QPS_INIT;
3006 attr->port_num = 1;
3007 ret = mlx5_ib_modify_qp(qp, attr, IB_QP_STATE | IB_QP_PKEY_INDEX |
3008 IB_QP_PORT, NULL);
3009 if (ret) {
3010 mlx5_ib_dbg(dev, "Couldn't modify UMR QP\n");
3011 goto error_4;
3012 }
3013
3014 memset(attr, 0, sizeof(*attr));
3015 attr->qp_state = IB_QPS_RTR;
3016 attr->path_mtu = IB_MTU_256;
3017
3018 ret = mlx5_ib_modify_qp(qp, attr, IB_QP_STATE, NULL);
3019 if (ret) {
3020 mlx5_ib_dbg(dev, "Couldn't modify umr QP to rtr\n");
3021 goto error_4;
3022 }
3023
3024 memset(attr, 0, sizeof(*attr));
3025 attr->qp_state = IB_QPS_RTS;
3026 ret = mlx5_ib_modify_qp(qp, attr, IB_QP_STATE, NULL);
3027 if (ret) {
3028 mlx5_ib_dbg(dev, "Couldn't modify umr QP to rts\n");
3029 goto error_4;
3030 }
3031
3032 dev->umrc.qp = qp;
3033 dev->umrc.cq = cq;
Eli Cohene126ba92013-07-07 17:25:49 +03003034 dev->umrc.pd = pd;
3035
3036 sema_init(&dev->umrc.sem, MAX_UMR_WR);
3037 ret = mlx5_mr_cache_init(dev);
3038 if (ret) {
3039 mlx5_ib_warn(dev, "mr cache init failed %d\n", ret);
3040 goto error_4;
3041 }
3042
3043 kfree(attr);
3044 kfree(init_attr);
3045
3046 return 0;
3047
3048error_4:
3049 mlx5_ib_destroy_qp(qp);
3050
3051error_3:
Christoph Hellwigadd08d72016-03-03 09:38:22 +01003052 ib_free_cq(cq);
Eli Cohene126ba92013-07-07 17:25:49 +03003053
3054error_2:
Eli Cohene126ba92013-07-07 17:25:49 +03003055 ib_dealloc_pd(pd);
3056
3057error_0:
3058 kfree(attr);
3059 kfree(init_attr);
3060 return ret;
3061}
3062
Max Gurtovoy6e8484c2017-05-28 10:53:11 +03003063static u8 mlx5_get_umr_fence(u8 umr_fence_cap)
3064{
3065 switch (umr_fence_cap) {
3066 case MLX5_CAP_UMR_FENCE_NONE:
3067 return MLX5_FENCE_MODE_NONE;
3068 case MLX5_CAP_UMR_FENCE_SMALL:
3069 return MLX5_FENCE_MODE_INITIATOR_SMALL;
3070 default:
3071 return MLX5_FENCE_MODE_STRONG_ORDERING;
3072 }
3073}
3074
Eli Cohene126ba92013-07-07 17:25:49 +03003075static int create_dev_resources(struct mlx5_ib_resources *devr)
3076{
3077 struct ib_srq_init_attr attr;
3078 struct mlx5_ib_dev *dev;
Matan Barakbcf4c1e2015-06-11 16:35:20 +03003079 struct ib_cq_init_attr cq_attr = {.cqe = 1};
Haggai Eran7722f472016-02-29 15:45:07 +02003080 int port;
Eli Cohene126ba92013-07-07 17:25:49 +03003081 int ret = 0;
3082
3083 dev = container_of(devr, struct mlx5_ib_dev, devr);
3084
Haggai Erand16e91d2016-02-29 15:45:05 +02003085 mutex_init(&devr->mutex);
3086
Eli Cohene126ba92013-07-07 17:25:49 +03003087 devr->p0 = mlx5_ib_alloc_pd(&dev->ib_dev, NULL, NULL);
3088 if (IS_ERR(devr->p0)) {
3089 ret = PTR_ERR(devr->p0);
3090 goto error0;
3091 }
3092 devr->p0->device = &dev->ib_dev;
3093 devr->p0->uobject = NULL;
3094 atomic_set(&devr->p0->usecnt, 0);
3095
Matan Barakbcf4c1e2015-06-11 16:35:20 +03003096 devr->c0 = mlx5_ib_create_cq(&dev->ib_dev, &cq_attr, NULL, NULL);
Eli Cohene126ba92013-07-07 17:25:49 +03003097 if (IS_ERR(devr->c0)) {
3098 ret = PTR_ERR(devr->c0);
3099 goto error1;
3100 }
3101 devr->c0->device = &dev->ib_dev;
3102 devr->c0->uobject = NULL;
3103 devr->c0->comp_handler = NULL;
3104 devr->c0->event_handler = NULL;
3105 devr->c0->cq_context = NULL;
3106 atomic_set(&devr->c0->usecnt, 0);
3107
3108 devr->x0 = mlx5_ib_alloc_xrcd(&dev->ib_dev, NULL, NULL);
3109 if (IS_ERR(devr->x0)) {
3110 ret = PTR_ERR(devr->x0);
3111 goto error2;
3112 }
3113 devr->x0->device = &dev->ib_dev;
3114 devr->x0->inode = NULL;
3115 atomic_set(&devr->x0->usecnt, 0);
3116 mutex_init(&devr->x0->tgt_qp_mutex);
3117 INIT_LIST_HEAD(&devr->x0->tgt_qp_list);
3118
3119 devr->x1 = mlx5_ib_alloc_xrcd(&dev->ib_dev, NULL, NULL);
3120 if (IS_ERR(devr->x1)) {
3121 ret = PTR_ERR(devr->x1);
3122 goto error3;
3123 }
3124 devr->x1->device = &dev->ib_dev;
3125 devr->x1->inode = NULL;
3126 atomic_set(&devr->x1->usecnt, 0);
3127 mutex_init(&devr->x1->tgt_qp_mutex);
3128 INIT_LIST_HEAD(&devr->x1->tgt_qp_list);
3129
3130 memset(&attr, 0, sizeof(attr));
3131 attr.attr.max_sge = 1;
3132 attr.attr.max_wr = 1;
3133 attr.srq_type = IB_SRQT_XRC;
3134 attr.ext.xrc.cq = devr->c0;
3135 attr.ext.xrc.xrcd = devr->x0;
3136
3137 devr->s0 = mlx5_ib_create_srq(devr->p0, &attr, NULL);
3138 if (IS_ERR(devr->s0)) {
3139 ret = PTR_ERR(devr->s0);
3140 goto error4;
3141 }
3142 devr->s0->device = &dev->ib_dev;
3143 devr->s0->pd = devr->p0;
3144 devr->s0->uobject = NULL;
3145 devr->s0->event_handler = NULL;
3146 devr->s0->srq_context = NULL;
3147 devr->s0->srq_type = IB_SRQT_XRC;
3148 devr->s0->ext.xrc.xrcd = devr->x0;
3149 devr->s0->ext.xrc.cq = devr->c0;
3150 atomic_inc(&devr->s0->ext.xrc.xrcd->usecnt);
3151 atomic_inc(&devr->s0->ext.xrc.cq->usecnt);
3152 atomic_inc(&devr->p0->usecnt);
3153 atomic_set(&devr->s0->usecnt, 0);
3154
Haggai Abramonvsky4aa17b22015-06-04 19:30:48 +03003155 memset(&attr, 0, sizeof(attr));
3156 attr.attr.max_sge = 1;
3157 attr.attr.max_wr = 1;
3158 attr.srq_type = IB_SRQT_BASIC;
3159 devr->s1 = mlx5_ib_create_srq(devr->p0, &attr, NULL);
3160 if (IS_ERR(devr->s1)) {
3161 ret = PTR_ERR(devr->s1);
3162 goto error5;
3163 }
3164 devr->s1->device = &dev->ib_dev;
3165 devr->s1->pd = devr->p0;
3166 devr->s1->uobject = NULL;
3167 devr->s1->event_handler = NULL;
3168 devr->s1->srq_context = NULL;
3169 devr->s1->srq_type = IB_SRQT_BASIC;
3170 devr->s1->ext.xrc.cq = devr->c0;
3171 atomic_inc(&devr->p0->usecnt);
3172 atomic_set(&devr->s0->usecnt, 0);
3173
Haggai Eran7722f472016-02-29 15:45:07 +02003174 for (port = 0; port < ARRAY_SIZE(devr->ports); ++port) {
3175 INIT_WORK(&devr->ports[port].pkey_change_work,
3176 pkey_change_handler);
3177 devr->ports[port].devr = devr;
3178 }
3179
Eli Cohene126ba92013-07-07 17:25:49 +03003180 return 0;
3181
Haggai Abramonvsky4aa17b22015-06-04 19:30:48 +03003182error5:
3183 mlx5_ib_destroy_srq(devr->s0);
Eli Cohene126ba92013-07-07 17:25:49 +03003184error4:
3185 mlx5_ib_dealloc_xrcd(devr->x1);
3186error3:
3187 mlx5_ib_dealloc_xrcd(devr->x0);
3188error2:
3189 mlx5_ib_destroy_cq(devr->c0);
3190error1:
3191 mlx5_ib_dealloc_pd(devr->p0);
3192error0:
3193 return ret;
3194}
3195
3196static void destroy_dev_resources(struct mlx5_ib_resources *devr)
3197{
Haggai Eran7722f472016-02-29 15:45:07 +02003198 struct mlx5_ib_dev *dev =
3199 container_of(devr, struct mlx5_ib_dev, devr);
3200 int port;
3201
Haggai Abramonvsky4aa17b22015-06-04 19:30:48 +03003202 mlx5_ib_destroy_srq(devr->s1);
Eli Cohene126ba92013-07-07 17:25:49 +03003203 mlx5_ib_destroy_srq(devr->s0);
3204 mlx5_ib_dealloc_xrcd(devr->x0);
3205 mlx5_ib_dealloc_xrcd(devr->x1);
3206 mlx5_ib_destroy_cq(devr->c0);
3207 mlx5_ib_dealloc_pd(devr->p0);
Haggai Eran7722f472016-02-29 15:45:07 +02003208
3209 /* Make sure no change P_Key work items are still executing */
3210 for (port = 0; port < dev->num_ports; ++port)
3211 cancel_work_sync(&devr->ports[port].pkey_change_work);
Eli Cohene126ba92013-07-07 17:25:49 +03003212}
3213
Achiad Shochate53505a2015-12-23 18:47:25 +02003214static u32 get_core_cap_flags(struct ib_device *ibdev)
3215{
3216 struct mlx5_ib_dev *dev = to_mdev(ibdev);
3217 enum rdma_link_layer ll = mlx5_ib_port_link_layer(ibdev, 1);
3218 u8 l3_type_cap = MLX5_CAP_ROCE(dev->mdev, l3_type);
3219 u8 roce_version_cap = MLX5_CAP_ROCE(dev->mdev, roce_version);
3220 u32 ret = 0;
3221
3222 if (ll == IB_LINK_LAYER_INFINIBAND)
3223 return RDMA_CORE_PORT_IBA_IB;
3224
Or Gerlitz72cd5712017-01-24 13:02:36 +02003225 ret = RDMA_CORE_PORT_RAW_PACKET;
3226
Achiad Shochate53505a2015-12-23 18:47:25 +02003227 if (!(l3_type_cap & MLX5_ROCE_L3_TYPE_IPV4_CAP))
Or Gerlitz72cd5712017-01-24 13:02:36 +02003228 return ret;
Achiad Shochate53505a2015-12-23 18:47:25 +02003229
3230 if (!(l3_type_cap & MLX5_ROCE_L3_TYPE_IPV6_CAP))
Or Gerlitz72cd5712017-01-24 13:02:36 +02003231 return ret;
Achiad Shochate53505a2015-12-23 18:47:25 +02003232
3233 if (roce_version_cap & MLX5_ROCE_VERSION_1_CAP)
3234 ret |= RDMA_CORE_PORT_IBA_ROCE;
3235
3236 if (roce_version_cap & MLX5_ROCE_VERSION_2_CAP)
3237 ret |= RDMA_CORE_PORT_IBA_ROCE_UDP_ENCAP;
3238
3239 return ret;
3240}
3241
Ira Weiny77386132015-05-13 20:02:58 -04003242static int mlx5_port_immutable(struct ib_device *ibdev, u8 port_num,
3243 struct ib_port_immutable *immutable)
3244{
3245 struct ib_port_attr attr;
Or Gerlitzca5b91d2016-11-27 16:51:36 +02003246 struct mlx5_ib_dev *dev = to_mdev(ibdev);
3247 enum rdma_link_layer ll = mlx5_ib_port_link_layer(ibdev, port_num);
Ira Weiny77386132015-05-13 20:02:58 -04003248 int err;
3249
Or Gerlitzc4550c62017-01-24 13:02:39 +02003250 immutable->core_cap_flags = get_core_cap_flags(ibdev);
3251
3252 err = ib_query_port(ibdev, port_num, &attr);
Ira Weiny77386132015-05-13 20:02:58 -04003253 if (err)
3254 return err;
3255
3256 immutable->pkey_tbl_len = attr.pkey_tbl_len;
3257 immutable->gid_tbl_len = attr.gid_tbl_len;
Achiad Shochate53505a2015-12-23 18:47:25 +02003258 immutable->core_cap_flags = get_core_cap_flags(ibdev);
Or Gerlitzca5b91d2016-11-27 16:51:36 +02003259 if ((ll == IB_LINK_LAYER_INFINIBAND) || MLX5_CAP_GEN(dev->mdev, roce))
3260 immutable->max_mad_size = IB_MGMT_MAD_SIZE;
Ira Weiny77386132015-05-13 20:02:58 -04003261
3262 return 0;
3263}
3264
Ira Weinyc7342822016-06-15 02:22:01 -04003265static void get_dev_fw_str(struct ib_device *ibdev, char *str,
3266 size_t str_len)
3267{
3268 struct mlx5_ib_dev *dev =
3269 container_of(ibdev, struct mlx5_ib_dev, ib_dev);
3270 snprintf(str, str_len, "%d.%d.%04d", fw_rev_maj(dev->mdev),
3271 fw_rev_min(dev->mdev), fw_rev_sub(dev->mdev));
3272}
3273
Or Gerlitz45f95ac2016-11-27 16:51:35 +02003274static int mlx5_eth_lag_init(struct mlx5_ib_dev *dev)
Aviv Heller9ef9c642016-09-18 20:48:01 +03003275{
3276 struct mlx5_core_dev *mdev = dev->mdev;
3277 struct mlx5_flow_namespace *ns = mlx5_get_flow_namespace(mdev,
3278 MLX5_FLOW_NAMESPACE_LAG);
3279 struct mlx5_flow_table *ft;
3280 int err;
3281
3282 if (!ns || !mlx5_lag_is_active(mdev))
3283 return 0;
3284
3285 err = mlx5_cmd_create_vport_lag(mdev);
3286 if (err)
3287 return err;
3288
3289 ft = mlx5_create_lag_demux_flow_table(ns, 0, 0);
3290 if (IS_ERR(ft)) {
3291 err = PTR_ERR(ft);
3292 goto err_destroy_vport_lag;
3293 }
3294
3295 dev->flow_db.lag_demux_ft = ft;
3296 return 0;
3297
3298err_destroy_vport_lag:
3299 mlx5_cmd_destroy_vport_lag(mdev);
3300 return err;
3301}
3302
Or Gerlitz45f95ac2016-11-27 16:51:35 +02003303static void mlx5_eth_lag_cleanup(struct mlx5_ib_dev *dev)
Aviv Heller9ef9c642016-09-18 20:48:01 +03003304{
3305 struct mlx5_core_dev *mdev = dev->mdev;
3306
3307 if (dev->flow_db.lag_demux_ft) {
3308 mlx5_destroy_flow_table(dev->flow_db.lag_demux_ft);
3309 dev->flow_db.lag_demux_ft = NULL;
3310
3311 mlx5_cmd_destroy_vport_lag(mdev);
3312 }
3313}
3314
Or Gerlitzd012f5d2016-11-27 16:51:34 +02003315static int mlx5_add_netdev_notifier(struct mlx5_ib_dev *dev)
Achiad Shochatfc24fc52015-12-23 18:47:17 +02003316{
Achiad Shochate53505a2015-12-23 18:47:25 +02003317 int err;
3318
Achiad Shochatfc24fc52015-12-23 18:47:17 +02003319 dev->roce.nb.notifier_call = mlx5_netdev_event;
Achiad Shochate53505a2015-12-23 18:47:25 +02003320 err = register_netdevice_notifier(&dev->roce.nb);
Aviv Heller5ec8c832016-09-18 20:48:00 +03003321 if (err) {
3322 dev->roce.nb.notifier_call = NULL;
Achiad Shochate53505a2015-12-23 18:47:25 +02003323 return err;
Aviv Heller5ec8c832016-09-18 20:48:00 +03003324 }
Achiad Shochate53505a2015-12-23 18:47:25 +02003325
Or Gerlitzd012f5d2016-11-27 16:51:34 +02003326 return 0;
3327}
Achiad Shochate53505a2015-12-23 18:47:25 +02003328
Or Gerlitzd012f5d2016-11-27 16:51:34 +02003329static void mlx5_remove_netdev_notifier(struct mlx5_ib_dev *dev)
Eli Cohene126ba92013-07-07 17:25:49 +03003330{
3331 if (dev->roce.nb.notifier_call) {
3332 unregister_netdevice_notifier(&dev->roce.nb);
3333 dev->roce.nb.notifier_call = NULL;
3334 }
3335}
3336
Or Gerlitz45f95ac2016-11-27 16:51:35 +02003337static int mlx5_enable_eth(struct mlx5_ib_dev *dev)
Eli Cohene126ba92013-07-07 17:25:49 +03003338{
Eli Cohene126ba92013-07-07 17:25:49 +03003339 int err;
3340
Or Gerlitzd012f5d2016-11-27 16:51:34 +02003341 err = mlx5_add_netdev_notifier(dev);
3342 if (err)
Achiad Shochate53505a2015-12-23 18:47:25 +02003343 return err;
Achiad Shochate53505a2015-12-23 18:47:25 +02003344
Or Gerlitzca5b91d2016-11-27 16:51:36 +02003345 if (MLX5_CAP_GEN(dev->mdev, roce)) {
3346 err = mlx5_nic_vport_enable_roce(dev->mdev);
3347 if (err)
3348 goto err_unregister_netdevice_notifier;
3349 }
Achiad Shochate53505a2015-12-23 18:47:25 +02003350
Or Gerlitz45f95ac2016-11-27 16:51:35 +02003351 err = mlx5_eth_lag_init(dev);
Aviv Heller9ef9c642016-09-18 20:48:01 +03003352 if (err)
3353 goto err_disable_roce;
3354
Achiad Shochate53505a2015-12-23 18:47:25 +02003355 return 0;
3356
Aviv Heller9ef9c642016-09-18 20:48:01 +03003357err_disable_roce:
Or Gerlitzca5b91d2016-11-27 16:51:36 +02003358 if (MLX5_CAP_GEN(dev->mdev, roce))
3359 mlx5_nic_vport_disable_roce(dev->mdev);
Aviv Heller9ef9c642016-09-18 20:48:01 +03003360
Achiad Shochate53505a2015-12-23 18:47:25 +02003361err_unregister_netdevice_notifier:
Or Gerlitzd012f5d2016-11-27 16:51:34 +02003362 mlx5_remove_netdev_notifier(dev);
Achiad Shochate53505a2015-12-23 18:47:25 +02003363 return err;
Achiad Shochatfc24fc52015-12-23 18:47:17 +02003364}
3365
Or Gerlitz45f95ac2016-11-27 16:51:35 +02003366static void mlx5_disable_eth(struct mlx5_ib_dev *dev)
Achiad Shochatfc24fc52015-12-23 18:47:17 +02003367{
Or Gerlitz45f95ac2016-11-27 16:51:35 +02003368 mlx5_eth_lag_cleanup(dev);
Or Gerlitzca5b91d2016-11-27 16:51:36 +02003369 if (MLX5_CAP_GEN(dev->mdev, roce))
3370 mlx5_nic_vport_disable_roce(dev->mdev);
Achiad Shochatfc24fc52015-12-23 18:47:17 +02003371}
3372
Parav Pandite1f24a72017-04-16 07:29:29 +03003373struct mlx5_ib_counter {
Kamal Heib7c16f472017-01-18 15:25:09 +02003374 const char *name;
3375 size_t offset;
3376};
3377
3378#define INIT_Q_COUNTER(_name) \
3379 { .name = #_name, .offset = MLX5_BYTE_OFF(query_q_counter_out, _name)}
3380
Parav Pandite1f24a72017-04-16 07:29:29 +03003381static const struct mlx5_ib_counter basic_q_cnts[] = {
Kamal Heib7c16f472017-01-18 15:25:09 +02003382 INIT_Q_COUNTER(rx_write_requests),
3383 INIT_Q_COUNTER(rx_read_requests),
3384 INIT_Q_COUNTER(rx_atomic_requests),
3385 INIT_Q_COUNTER(out_of_buffer),
3386};
3387
Parav Pandite1f24a72017-04-16 07:29:29 +03003388static const struct mlx5_ib_counter out_of_seq_q_cnts[] = {
Kamal Heib7c16f472017-01-18 15:25:09 +02003389 INIT_Q_COUNTER(out_of_sequence),
3390};
3391
Parav Pandite1f24a72017-04-16 07:29:29 +03003392static const struct mlx5_ib_counter retrans_q_cnts[] = {
Kamal Heib7c16f472017-01-18 15:25:09 +02003393 INIT_Q_COUNTER(duplicate_request),
3394 INIT_Q_COUNTER(rnr_nak_retry_err),
3395 INIT_Q_COUNTER(packet_seq_err),
3396 INIT_Q_COUNTER(implied_nak_seq_err),
3397 INIT_Q_COUNTER(local_ack_timeout_err),
3398};
3399
Parav Pandite1f24a72017-04-16 07:29:29 +03003400#define INIT_CONG_COUNTER(_name) \
3401 { .name = #_name, .offset = \
3402 MLX5_BYTE_OFF(query_cong_statistics_out, _name ## _high)}
3403
3404static const struct mlx5_ib_counter cong_cnts[] = {
3405 INIT_CONG_COUNTER(rp_cnp_ignored),
3406 INIT_CONG_COUNTER(rp_cnp_handled),
3407 INIT_CONG_COUNTER(np_ecn_marked_roce_packets),
3408 INIT_CONG_COUNTER(np_cnp_sent),
3409};
3410
3411static void mlx5_ib_dealloc_counters(struct mlx5_ib_dev *dev)
Mark Bloch0837e862016-06-17 15:10:55 +03003412{
3413 unsigned int i;
3414
Kamal Heib7c16f472017-01-18 15:25:09 +02003415 for (i = 0; i < dev->num_ports; i++) {
Mark Bloch0837e862016-06-17 15:10:55 +03003416 mlx5_core_dealloc_q_counter(dev->mdev,
Parav Pandite1f24a72017-04-16 07:29:29 +03003417 dev->port[i].cnts.set_id);
3418 kfree(dev->port[i].cnts.names);
3419 kfree(dev->port[i].cnts.offsets);
Kamal Heib7c16f472017-01-18 15:25:09 +02003420 }
3421}
3422
Parav Pandite1f24a72017-04-16 07:29:29 +03003423static int __mlx5_ib_alloc_counters(struct mlx5_ib_dev *dev,
3424 struct mlx5_ib_counters *cnts)
Kamal Heib7c16f472017-01-18 15:25:09 +02003425{
3426 u32 num_counters;
3427
3428 num_counters = ARRAY_SIZE(basic_q_cnts);
3429
3430 if (MLX5_CAP_GEN(dev->mdev, out_of_seq_cnt))
3431 num_counters += ARRAY_SIZE(out_of_seq_q_cnts);
3432
3433 if (MLX5_CAP_GEN(dev->mdev, retransmission_q_counters))
3434 num_counters += ARRAY_SIZE(retrans_q_cnts);
Parav Pandite1f24a72017-04-16 07:29:29 +03003435 cnts->num_q_counters = num_counters;
Kamal Heib7c16f472017-01-18 15:25:09 +02003436
Parav Pandite1f24a72017-04-16 07:29:29 +03003437 if (MLX5_CAP_GEN(dev->mdev, cc_query_allowed)) {
3438 cnts->num_cong_counters = ARRAY_SIZE(cong_cnts);
3439 num_counters += ARRAY_SIZE(cong_cnts);
3440 }
3441
3442 cnts->names = kcalloc(num_counters, sizeof(cnts->names), GFP_KERNEL);
3443 if (!cnts->names)
Kamal Heib7c16f472017-01-18 15:25:09 +02003444 return -ENOMEM;
3445
Parav Pandite1f24a72017-04-16 07:29:29 +03003446 cnts->offsets = kcalloc(num_counters,
3447 sizeof(cnts->offsets), GFP_KERNEL);
3448 if (!cnts->offsets)
Kamal Heib7c16f472017-01-18 15:25:09 +02003449 goto err_names;
3450
Kamal Heib7c16f472017-01-18 15:25:09 +02003451 return 0;
3452
3453err_names:
Parav Pandite1f24a72017-04-16 07:29:29 +03003454 kfree(cnts->names);
Kamal Heib7c16f472017-01-18 15:25:09 +02003455 return -ENOMEM;
3456}
3457
Parav Pandite1f24a72017-04-16 07:29:29 +03003458static void mlx5_ib_fill_counters(struct mlx5_ib_dev *dev,
3459 const char **names,
3460 size_t *offsets)
Kamal Heib7c16f472017-01-18 15:25:09 +02003461{
3462 int i;
3463 int j = 0;
3464
3465 for (i = 0; i < ARRAY_SIZE(basic_q_cnts); i++, j++) {
3466 names[j] = basic_q_cnts[i].name;
3467 offsets[j] = basic_q_cnts[i].offset;
3468 }
3469
3470 if (MLX5_CAP_GEN(dev->mdev, out_of_seq_cnt)) {
3471 for (i = 0; i < ARRAY_SIZE(out_of_seq_q_cnts); i++, j++) {
3472 names[j] = out_of_seq_q_cnts[i].name;
3473 offsets[j] = out_of_seq_q_cnts[i].offset;
3474 }
3475 }
3476
3477 if (MLX5_CAP_GEN(dev->mdev, retransmission_q_counters)) {
3478 for (i = 0; i < ARRAY_SIZE(retrans_q_cnts); i++, j++) {
3479 names[j] = retrans_q_cnts[i].name;
3480 offsets[j] = retrans_q_cnts[i].offset;
3481 }
3482 }
Parav Pandite1f24a72017-04-16 07:29:29 +03003483
3484 if (MLX5_CAP_GEN(dev->mdev, cc_query_allowed)) {
3485 for (i = 0; i < ARRAY_SIZE(cong_cnts); i++, j++) {
3486 names[j] = cong_cnts[i].name;
3487 offsets[j] = cong_cnts[i].offset;
3488 }
3489 }
Mark Bloch0837e862016-06-17 15:10:55 +03003490}
3491
Parav Pandite1f24a72017-04-16 07:29:29 +03003492static int mlx5_ib_alloc_counters(struct mlx5_ib_dev *dev)
Mark Bloch0837e862016-06-17 15:10:55 +03003493{
3494 int i;
3495 int ret;
3496
3497 for (i = 0; i < dev->num_ports; i++) {
Kamal Heib7c16f472017-01-18 15:25:09 +02003498 struct mlx5_ib_port *port = &dev->port[i];
3499
Mark Bloch0837e862016-06-17 15:10:55 +03003500 ret = mlx5_core_alloc_q_counter(dev->mdev,
Parav Pandite1f24a72017-04-16 07:29:29 +03003501 &port->cnts.set_id);
Mark Bloch0837e862016-06-17 15:10:55 +03003502 if (ret) {
3503 mlx5_ib_warn(dev,
3504 "couldn't allocate queue counter for port %d, err %d\n",
3505 i + 1, ret);
3506 goto dealloc_counters;
3507 }
Kamal Heib7c16f472017-01-18 15:25:09 +02003508
Parav Pandite1f24a72017-04-16 07:29:29 +03003509 ret = __mlx5_ib_alloc_counters(dev, &port->cnts);
Kamal Heib7c16f472017-01-18 15:25:09 +02003510 if (ret)
3511 goto dealloc_counters;
3512
Parav Pandite1f24a72017-04-16 07:29:29 +03003513 mlx5_ib_fill_counters(dev, port->cnts.names,
3514 port->cnts.offsets);
Mark Bloch0837e862016-06-17 15:10:55 +03003515 }
3516
3517 return 0;
3518
3519dealloc_counters:
3520 while (--i >= 0)
3521 mlx5_core_dealloc_q_counter(dev->mdev,
Parav Pandite1f24a72017-04-16 07:29:29 +03003522 dev->port[i].cnts.set_id);
Mark Bloch0837e862016-06-17 15:10:55 +03003523
3524 return ret;
3525}
3526
Mark Bloch0ad17a82016-06-17 15:10:56 +03003527static struct rdma_hw_stats *mlx5_ib_alloc_hw_stats(struct ib_device *ibdev,
3528 u8 port_num)
3529{
Kamal Heib7c16f472017-01-18 15:25:09 +02003530 struct mlx5_ib_dev *dev = to_mdev(ibdev);
3531 struct mlx5_ib_port *port = &dev->port[port_num - 1];
Mark Bloch0ad17a82016-06-17 15:10:56 +03003532
3533 /* We support only per port stats */
3534 if (port_num == 0)
3535 return NULL;
3536
Parav Pandite1f24a72017-04-16 07:29:29 +03003537 return rdma_alloc_hw_stats_struct(port->cnts.names,
3538 port->cnts.num_q_counters +
3539 port->cnts.num_cong_counters,
Mark Bloch0ad17a82016-06-17 15:10:56 +03003540 RDMA_HW_STATS_DEFAULT_LIFESPAN);
3541}
3542
Parav Pandite1f24a72017-04-16 07:29:29 +03003543static int mlx5_ib_query_q_counters(struct mlx5_ib_dev *dev,
3544 struct mlx5_ib_port *port,
3545 struct rdma_hw_stats *stats)
3546{
3547 int outlen = MLX5_ST_SZ_BYTES(query_q_counter_out);
3548 void *out;
3549 __be32 val;
3550 int ret, i;
3551
Leon Romanovsky1b9a07e2017-05-10 21:32:18 +03003552 out = kvzalloc(outlen, GFP_KERNEL);
Parav Pandite1f24a72017-04-16 07:29:29 +03003553 if (!out)
3554 return -ENOMEM;
3555
3556 ret = mlx5_core_query_q_counter(dev->mdev,
3557 port->cnts.set_id, 0,
3558 out, outlen);
3559 if (ret)
3560 goto free;
3561
3562 for (i = 0; i < port->cnts.num_q_counters; i++) {
3563 val = *(__be32 *)(out + port->cnts.offsets[i]);
3564 stats->value[i] = (u64)be32_to_cpu(val);
3565 }
3566
3567free:
3568 kvfree(out);
3569 return ret;
3570}
3571
3572static int mlx5_ib_query_cong_counters(struct mlx5_ib_dev *dev,
3573 struct mlx5_ib_port *port,
3574 struct rdma_hw_stats *stats)
3575{
3576 int outlen = MLX5_ST_SZ_BYTES(query_cong_statistics_out);
3577 void *out;
3578 int ret, i;
3579 int offset = port->cnts.num_q_counters;
3580
Leon Romanovsky1b9a07e2017-05-10 21:32:18 +03003581 out = kvzalloc(outlen, GFP_KERNEL);
Parav Pandite1f24a72017-04-16 07:29:29 +03003582 if (!out)
3583 return -ENOMEM;
3584
3585 ret = mlx5_cmd_query_cong_counter(dev->mdev, false, out, outlen);
3586 if (ret)
3587 goto free;
3588
3589 for (i = 0; i < port->cnts.num_cong_counters; i++) {
3590 stats->value[i + offset] =
3591 be64_to_cpup((__be64 *)(out +
3592 port->cnts.offsets[i + offset]));
3593 }
3594
3595free:
3596 kvfree(out);
3597 return ret;
3598}
3599
Mark Bloch0ad17a82016-06-17 15:10:56 +03003600static int mlx5_ib_get_hw_stats(struct ib_device *ibdev,
3601 struct rdma_hw_stats *stats,
Kamal Heib7c16f472017-01-18 15:25:09 +02003602 u8 port_num, int index)
Mark Bloch0ad17a82016-06-17 15:10:56 +03003603{
3604 struct mlx5_ib_dev *dev = to_mdev(ibdev);
Kamal Heib7c16f472017-01-18 15:25:09 +02003605 struct mlx5_ib_port *port = &dev->port[port_num - 1];
Parav Pandite1f24a72017-04-16 07:29:29 +03003606 int ret, num_counters;
Mark Bloch0ad17a82016-06-17 15:10:56 +03003607
Kamal Heib7c16f472017-01-18 15:25:09 +02003608 if (!stats)
Parav Pandite1f24a72017-04-16 07:29:29 +03003609 return -EINVAL;
Mark Bloch0ad17a82016-06-17 15:10:56 +03003610
Parav Pandite1f24a72017-04-16 07:29:29 +03003611 ret = mlx5_ib_query_q_counters(dev, port, stats);
Mark Bloch0ad17a82016-06-17 15:10:56 +03003612 if (ret)
Parav Pandite1f24a72017-04-16 07:29:29 +03003613 return ret;
3614 num_counters = port->cnts.num_q_counters;
Mark Bloch0ad17a82016-06-17 15:10:56 +03003615
Parav Pandite1f24a72017-04-16 07:29:29 +03003616 if (MLX5_CAP_GEN(dev->mdev, cc_query_allowed)) {
3617 ret = mlx5_ib_query_cong_counters(dev, port, stats);
3618 if (ret)
3619 return ret;
3620 num_counters += port->cnts.num_cong_counters;
Mark Bloch0ad17a82016-06-17 15:10:56 +03003621 }
Kamal Heib7c16f472017-01-18 15:25:09 +02003622
Parav Pandite1f24a72017-04-16 07:29:29 +03003623 return num_counters;
Mark Bloch0ad17a82016-06-17 15:10:56 +03003624}
3625
Niranjana Vishwanathapura8e959602017-06-30 13:14:46 -07003626static void mlx5_ib_free_rdma_netdev(struct net_device *netdev)
3627{
3628 return mlx5_rdma_netdev_free(netdev);
3629}
3630
Erez Shitrit693dfd52017-04-27 17:01:34 +03003631static struct net_device*
3632mlx5_ib_alloc_rdma_netdev(struct ib_device *hca,
3633 u8 port_num,
3634 enum rdma_netdev_t type,
3635 const char *name,
3636 unsigned char name_assign_type,
3637 void (*setup)(struct net_device *))
3638{
Niranjana Vishwanathapura8e959602017-06-30 13:14:46 -07003639 struct net_device *netdev;
3640 struct rdma_netdev *rn;
3641
Erez Shitrit693dfd52017-04-27 17:01:34 +03003642 if (type != RDMA_NETDEV_IPOIB)
3643 return ERR_PTR(-EOPNOTSUPP);
3644
Niranjana Vishwanathapura8e959602017-06-30 13:14:46 -07003645 netdev = mlx5_rdma_netdev_alloc(to_mdev(hca)->mdev, hca,
3646 name, setup);
3647 if (likely(!IS_ERR_OR_NULL(netdev))) {
3648 rn = netdev_priv(netdev);
3649 rn->free_rdma_netdev = mlx5_ib_free_rdma_netdev;
3650 }
3651 return netdev;
Erez Shitrit693dfd52017-04-27 17:01:34 +03003652}
3653
Maor Gottlieb03404e82017-05-30 10:29:13 +03003654static void cancel_delay_drop(struct mlx5_ib_dev *dev)
3655{
3656 if (!(dev->ib_dev.attrs.raw_packet_caps & IB_RAW_PACKET_CAP_DELAY_DROP))
3657 return;
3658
3659 cancel_work_sync(&dev->delay_drop.delay_drop_work);
3660}
3661
3662static void init_delay_drop(struct mlx5_ib_dev *dev)
3663{
3664 if (!(dev->ib_dev.attrs.raw_packet_caps & IB_RAW_PACKET_CAP_DELAY_DROP))
3665 return;
3666
3667 mutex_init(&dev->delay_drop.lock);
3668 dev->delay_drop.dev = dev;
3669 dev->delay_drop.activate = false;
3670 dev->delay_drop.timeout = MLX5_MAX_DELAY_DROP_TIMEOUT_MS * 1000;
3671 INIT_WORK(&dev->delay_drop.delay_drop_work, delay_drop_handler);
3672}
3673
Jack Morgenstein9603b612014-07-28 23:30:22 +03003674static void *mlx5_ib_add(struct mlx5_core_dev *mdev)
Eli Cohene126ba92013-07-07 17:25:49 +03003675{
Eli Cohene126ba92013-07-07 17:25:49 +03003676 struct mlx5_ib_dev *dev;
Achiad Shochatebd61f62015-12-23 18:47:16 +02003677 enum rdma_link_layer ll;
3678 int port_type_cap;
Aviv Heller4babcf92016-09-18 20:48:03 +03003679 const char *name;
Eli Cohene126ba92013-07-07 17:25:49 +03003680 int err;
3681 int i;
3682
Achiad Shochatebd61f62015-12-23 18:47:16 +02003683 port_type_cap = MLX5_CAP_GEN(mdev, port_type);
3684 ll = mlx5_port_type_cap_to_rdma_ll(port_type_cap);
3685
Eli Cohene126ba92013-07-07 17:25:49 +03003686 printk_once(KERN_INFO "%s", mlx5_version);
3687
3688 dev = (struct mlx5_ib_dev *)ib_alloc_device(sizeof(*dev));
3689 if (!dev)
Jack Morgenstein9603b612014-07-28 23:30:22 +03003690 return NULL;
Eli Cohene126ba92013-07-07 17:25:49 +03003691
Jack Morgenstein9603b612014-07-28 23:30:22 +03003692 dev->mdev = mdev;
Eli Cohene126ba92013-07-07 17:25:49 +03003693
Mark Bloch0837e862016-06-17 15:10:55 +03003694 dev->port = kcalloc(MLX5_CAP_GEN(mdev, num_ports), sizeof(*dev->port),
3695 GFP_KERNEL);
3696 if (!dev->port)
3697 goto err_dealloc;
3698
Achiad Shochatfc24fc52015-12-23 18:47:17 +02003699 rwlock_init(&dev->roce.netdev_lock);
Eli Cohene126ba92013-07-07 17:25:49 +03003700 err = get_port_caps(dev);
3701 if (err)
Mark Bloch0837e862016-06-17 15:10:55 +03003702 goto err_free_port;
Eli Cohene126ba92013-07-07 17:25:49 +03003703
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03003704 if (mlx5_use_mad_ifc(dev))
3705 get_ext_port_caps(dev);
Eli Cohene126ba92013-07-07 17:25:49 +03003706
Aviv Heller4babcf92016-09-18 20:48:03 +03003707 if (!mlx5_lag_is_active(mdev))
3708 name = "mlx5_%d";
3709 else
3710 name = "mlx5_bond_%d";
3711
3712 strlcpy(dev->ib_dev.name, name, IB_DEVICE_NAME_MAX);
Eli Cohene126ba92013-07-07 17:25:49 +03003713 dev->ib_dev.owner = THIS_MODULE;
3714 dev->ib_dev.node_type = RDMA_NODE_IB_CA;
Sagi Grimbergc6790aa2015-09-24 10:34:23 +03003715 dev->ib_dev.local_dma_lkey = 0 /* not supported for now */;
Saeed Mahameed938fe832015-05-28 22:28:41 +03003716 dev->num_ports = MLX5_CAP_GEN(mdev, num_ports);
Eli Cohene126ba92013-07-07 17:25:49 +03003717 dev->ib_dev.phys_port_cnt = dev->num_ports;
Saeed Mahameed233d05d2015-04-02 17:07:32 +03003718 dev->ib_dev.num_comp_vectors =
3719 dev->mdev->priv.eq_table.num_comp_vectors;
Bart Van Assche9b0c2892017-01-20 13:04:21 -08003720 dev->ib_dev.dev.parent = &mdev->pdev->dev;
Eli Cohene126ba92013-07-07 17:25:49 +03003721
3722 dev->ib_dev.uverbs_abi_ver = MLX5_IB_UVERBS_ABI_VERSION;
3723 dev->ib_dev.uverbs_cmd_mask =
3724 (1ull << IB_USER_VERBS_CMD_GET_CONTEXT) |
3725 (1ull << IB_USER_VERBS_CMD_QUERY_DEVICE) |
3726 (1ull << IB_USER_VERBS_CMD_QUERY_PORT) |
3727 (1ull << IB_USER_VERBS_CMD_ALLOC_PD) |
3728 (1ull << IB_USER_VERBS_CMD_DEALLOC_PD) |
Moni Shoua41c450f2016-11-23 08:23:26 +02003729 (1ull << IB_USER_VERBS_CMD_CREATE_AH) |
3730 (1ull << IB_USER_VERBS_CMD_DESTROY_AH) |
Eli Cohene126ba92013-07-07 17:25:49 +03003731 (1ull << IB_USER_VERBS_CMD_REG_MR) |
Noa Osherovich56e11d62016-02-29 16:46:51 +02003732 (1ull << IB_USER_VERBS_CMD_REREG_MR) |
Eli Cohene126ba92013-07-07 17:25:49 +03003733 (1ull << IB_USER_VERBS_CMD_DEREG_MR) |
3734 (1ull << IB_USER_VERBS_CMD_CREATE_COMP_CHANNEL) |
3735 (1ull << IB_USER_VERBS_CMD_CREATE_CQ) |
3736 (1ull << IB_USER_VERBS_CMD_RESIZE_CQ) |
3737 (1ull << IB_USER_VERBS_CMD_DESTROY_CQ) |
3738 (1ull << IB_USER_VERBS_CMD_CREATE_QP) |
3739 (1ull << IB_USER_VERBS_CMD_MODIFY_QP) |
3740 (1ull << IB_USER_VERBS_CMD_QUERY_QP) |
3741 (1ull << IB_USER_VERBS_CMD_DESTROY_QP) |
3742 (1ull << IB_USER_VERBS_CMD_ATTACH_MCAST) |
3743 (1ull << IB_USER_VERBS_CMD_DETACH_MCAST) |
3744 (1ull << IB_USER_VERBS_CMD_CREATE_SRQ) |
3745 (1ull << IB_USER_VERBS_CMD_MODIFY_SRQ) |
3746 (1ull << IB_USER_VERBS_CMD_QUERY_SRQ) |
3747 (1ull << IB_USER_VERBS_CMD_DESTROY_SRQ) |
3748 (1ull << IB_USER_VERBS_CMD_CREATE_XSRQ) |
3749 (1ull << IB_USER_VERBS_CMD_OPEN_QP);
Haggai Eran1707cb42015-02-08 13:28:52 +02003750 dev->ib_dev.uverbs_ex_cmd_mask =
Matan Barakd4584dd2016-01-28 17:51:46 +02003751 (1ull << IB_USER_VERBS_EX_CMD_QUERY_DEVICE) |
3752 (1ull << IB_USER_VERBS_EX_CMD_CREATE_CQ) |
Bodong Wang7d29f342016-12-01 13:43:16 +02003753 (1ull << IB_USER_VERBS_EX_CMD_CREATE_QP) |
3754 (1ull << IB_USER_VERBS_EX_CMD_MODIFY_QP);
Eli Cohene126ba92013-07-07 17:25:49 +03003755
3756 dev->ib_dev.query_device = mlx5_ib_query_device;
3757 dev->ib_dev.query_port = mlx5_ib_query_port;
Achiad Shochatebd61f62015-12-23 18:47:16 +02003758 dev->ib_dev.get_link_layer = mlx5_ib_port_link_layer;
Achiad Shochatfc24fc52015-12-23 18:47:17 +02003759 if (ll == IB_LINK_LAYER_ETHERNET)
3760 dev->ib_dev.get_netdev = mlx5_ib_get_netdev;
Eli Cohene126ba92013-07-07 17:25:49 +03003761 dev->ib_dev.query_gid = mlx5_ib_query_gid;
Achiad Shochat3cca2602015-12-23 18:47:23 +02003762 dev->ib_dev.add_gid = mlx5_ib_add_gid;
3763 dev->ib_dev.del_gid = mlx5_ib_del_gid;
Eli Cohene126ba92013-07-07 17:25:49 +03003764 dev->ib_dev.query_pkey = mlx5_ib_query_pkey;
3765 dev->ib_dev.modify_device = mlx5_ib_modify_device;
3766 dev->ib_dev.modify_port = mlx5_ib_modify_port;
3767 dev->ib_dev.alloc_ucontext = mlx5_ib_alloc_ucontext;
3768 dev->ib_dev.dealloc_ucontext = mlx5_ib_dealloc_ucontext;
3769 dev->ib_dev.mmap = mlx5_ib_mmap;
3770 dev->ib_dev.alloc_pd = mlx5_ib_alloc_pd;
3771 dev->ib_dev.dealloc_pd = mlx5_ib_dealloc_pd;
3772 dev->ib_dev.create_ah = mlx5_ib_create_ah;
3773 dev->ib_dev.query_ah = mlx5_ib_query_ah;
3774 dev->ib_dev.destroy_ah = mlx5_ib_destroy_ah;
3775 dev->ib_dev.create_srq = mlx5_ib_create_srq;
3776 dev->ib_dev.modify_srq = mlx5_ib_modify_srq;
3777 dev->ib_dev.query_srq = mlx5_ib_query_srq;
3778 dev->ib_dev.destroy_srq = mlx5_ib_destroy_srq;
3779 dev->ib_dev.post_srq_recv = mlx5_ib_post_srq_recv;
3780 dev->ib_dev.create_qp = mlx5_ib_create_qp;
3781 dev->ib_dev.modify_qp = mlx5_ib_modify_qp;
3782 dev->ib_dev.query_qp = mlx5_ib_query_qp;
3783 dev->ib_dev.destroy_qp = mlx5_ib_destroy_qp;
3784 dev->ib_dev.post_send = mlx5_ib_post_send;
3785 dev->ib_dev.post_recv = mlx5_ib_post_recv;
3786 dev->ib_dev.create_cq = mlx5_ib_create_cq;
3787 dev->ib_dev.modify_cq = mlx5_ib_modify_cq;
3788 dev->ib_dev.resize_cq = mlx5_ib_resize_cq;
3789 dev->ib_dev.destroy_cq = mlx5_ib_destroy_cq;
3790 dev->ib_dev.poll_cq = mlx5_ib_poll_cq;
3791 dev->ib_dev.req_notify_cq = mlx5_ib_arm_cq;
3792 dev->ib_dev.get_dma_mr = mlx5_ib_get_dma_mr;
3793 dev->ib_dev.reg_user_mr = mlx5_ib_reg_user_mr;
Noa Osherovich56e11d62016-02-29 16:46:51 +02003794 dev->ib_dev.rereg_user_mr = mlx5_ib_rereg_user_mr;
Eli Cohene126ba92013-07-07 17:25:49 +03003795 dev->ib_dev.dereg_mr = mlx5_ib_dereg_mr;
3796 dev->ib_dev.attach_mcast = mlx5_ib_mcg_attach;
3797 dev->ib_dev.detach_mcast = mlx5_ib_mcg_detach;
3798 dev->ib_dev.process_mad = mlx5_ib_process_mad;
Sagi Grimberg9bee1782015-07-30 10:32:35 +03003799 dev->ib_dev.alloc_mr = mlx5_ib_alloc_mr;
Sagi Grimberg8a187ee2015-10-13 19:11:26 +03003800 dev->ib_dev.map_mr_sg = mlx5_ib_map_mr_sg;
Sagi Grimbergd5436ba2014-02-23 14:19:12 +02003801 dev->ib_dev.check_mr_status = mlx5_ib_check_mr_status;
Ira Weiny77386132015-05-13 20:02:58 -04003802 dev->ib_dev.get_port_immutable = mlx5_port_immutable;
Ira Weinyc7342822016-06-15 02:22:01 -04003803 dev->ib_dev.get_dev_fw_str = get_dev_fw_str;
Niranjana Vishwanathapura8e959602017-06-30 13:14:46 -07003804 if (MLX5_CAP_GEN(mdev, ipoib_enhanced_offloads))
Alex Vesker022d0382017-06-14 09:59:06 +03003805 dev->ib_dev.alloc_rdma_netdev = mlx5_ib_alloc_rdma_netdev;
Niranjana Vishwanathapura8e959602017-06-30 13:14:46 -07003806
Eli Coheneff901d2016-03-11 22:58:42 +02003807 if (mlx5_core_is_pf(mdev)) {
3808 dev->ib_dev.get_vf_config = mlx5_ib_get_vf_config;
3809 dev->ib_dev.set_vf_link_state = mlx5_ib_set_vf_link_state;
3810 dev->ib_dev.get_vf_stats = mlx5_ib_get_vf_stats;
3811 dev->ib_dev.set_vf_guid = mlx5_ib_set_vf_guid;
3812 }
Eli Cohene126ba92013-07-07 17:25:49 +03003813
Maor Gottlieb7c2344c2016-06-17 14:56:44 +03003814 dev->ib_dev.disassociate_ucontext = mlx5_ib_disassociate_ucontext;
3815
Saeed Mahameed938fe832015-05-28 22:28:41 +03003816 mlx5_ib_internal_fill_odp_caps(dev);
Haggai Eran8cdd3122014-12-11 17:04:20 +02003817
Max Gurtovoy6e8484c2017-05-28 10:53:11 +03003818 dev->umr_fence = mlx5_get_umr_fence(MLX5_CAP_GEN(mdev, umr_fence));
3819
Matan Barakd2370e02016-02-29 18:05:30 +02003820 if (MLX5_CAP_GEN(mdev, imaicl)) {
3821 dev->ib_dev.alloc_mw = mlx5_ib_alloc_mw;
3822 dev->ib_dev.dealloc_mw = mlx5_ib_dealloc_mw;
3823 dev->ib_dev.uverbs_cmd_mask |=
3824 (1ull << IB_USER_VERBS_CMD_ALLOC_MW) |
3825 (1ull << IB_USER_VERBS_CMD_DEALLOC_MW);
3826 }
3827
Kamal Heib7c16f472017-01-18 15:25:09 +02003828 if (MLX5_CAP_GEN(dev->mdev, max_qp_cnt)) {
Mark Bloch0ad17a82016-06-17 15:10:56 +03003829 dev->ib_dev.get_hw_stats = mlx5_ib_get_hw_stats;
3830 dev->ib_dev.alloc_hw_stats = mlx5_ib_alloc_hw_stats;
3831 }
3832
Saeed Mahameed938fe832015-05-28 22:28:41 +03003833 if (MLX5_CAP_GEN(mdev, xrc)) {
Eli Cohene126ba92013-07-07 17:25:49 +03003834 dev->ib_dev.alloc_xrcd = mlx5_ib_alloc_xrcd;
3835 dev->ib_dev.dealloc_xrcd = mlx5_ib_dealloc_xrcd;
3836 dev->ib_dev.uverbs_cmd_mask |=
3837 (1ull << IB_USER_VERBS_CMD_OPEN_XRCD) |
3838 (1ull << IB_USER_VERBS_CMD_CLOSE_XRCD);
3839 }
3840
Linus Torvalds048ccca2016-01-23 18:45:06 -08003841 if (mlx5_ib_port_link_layer(&dev->ib_dev, 1) ==
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003842 IB_LINK_LAYER_ETHERNET) {
3843 dev->ib_dev.create_flow = mlx5_ib_create_flow;
3844 dev->ib_dev.destroy_flow = mlx5_ib_destroy_flow;
Yishai Hadas79b20a62016-05-23 15:20:50 +03003845 dev->ib_dev.create_wq = mlx5_ib_create_wq;
3846 dev->ib_dev.modify_wq = mlx5_ib_modify_wq;
3847 dev->ib_dev.destroy_wq = mlx5_ib_destroy_wq;
Yishai Hadasc5f90922016-05-23 15:20:53 +03003848 dev->ib_dev.create_rwq_ind_table = mlx5_ib_create_rwq_ind_table;
3849 dev->ib_dev.destroy_rwq_ind_table = mlx5_ib_destroy_rwq_ind_table;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003850 dev->ib_dev.uverbs_ex_cmd_mask |=
3851 (1ull << IB_USER_VERBS_EX_CMD_CREATE_FLOW) |
Yishai Hadas79b20a62016-05-23 15:20:50 +03003852 (1ull << IB_USER_VERBS_EX_CMD_DESTROY_FLOW) |
3853 (1ull << IB_USER_VERBS_EX_CMD_CREATE_WQ) |
3854 (1ull << IB_USER_VERBS_EX_CMD_MODIFY_WQ) |
Yishai Hadasc5f90922016-05-23 15:20:53 +03003855 (1ull << IB_USER_VERBS_EX_CMD_DESTROY_WQ) |
3856 (1ull << IB_USER_VERBS_EX_CMD_CREATE_RWQ_IND_TBL) |
3857 (1ull << IB_USER_VERBS_EX_CMD_DESTROY_RWQ_IND_TBL);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003858 }
Eli Cohene126ba92013-07-07 17:25:49 +03003859 err = init_node_data(dev);
3860 if (err)
Majd Dibbiny90be7c82016-10-27 16:36:39 +03003861 goto err_free_port;
Eli Cohene126ba92013-07-07 17:25:49 +03003862
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003863 mutex_init(&dev->flow_db.lock);
Eli Cohene126ba92013-07-07 17:25:49 +03003864 mutex_init(&dev->cap_mask_mutex);
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03003865 INIT_LIST_HEAD(&dev->qp_list);
3866 spin_lock_init(&dev->reset_flow_resource_lock);
Eli Cohene126ba92013-07-07 17:25:49 +03003867
Achiad Shochatfc24fc52015-12-23 18:47:17 +02003868 if (ll == IB_LINK_LAYER_ETHERNET) {
Or Gerlitz45f95ac2016-11-27 16:51:35 +02003869 err = mlx5_enable_eth(dev);
Achiad Shochatfc24fc52015-12-23 18:47:17 +02003870 if (err)
Majd Dibbiny90be7c82016-10-27 16:36:39 +03003871 goto err_free_port;
Moni Shouafd65f1b2017-05-30 09:56:05 +03003872 dev->roce.last_port_state = IB_PORT_DOWN;
Achiad Shochatfc24fc52015-12-23 18:47:17 +02003873 }
3874
Eli Cohene126ba92013-07-07 17:25:49 +03003875 err = create_dev_resources(&dev->devr);
3876 if (err)
Or Gerlitz45f95ac2016-11-27 16:51:35 +02003877 goto err_disable_eth;
Eli Cohene126ba92013-07-07 17:25:49 +03003878
Haggai Eran6aec21f2014-12-11 17:04:23 +02003879 err = mlx5_ib_odp_init_one(dev);
Wei Yongjun281d1a92013-07-30 07:54:26 +08003880 if (err)
Eli Cohene126ba92013-07-07 17:25:49 +03003881 goto err_rsrc;
3882
Kamal Heib45bded22017-01-18 14:10:32 +02003883 if (MLX5_CAP_GEN(dev->mdev, max_qp_cnt)) {
Parav Pandite1f24a72017-04-16 07:29:29 +03003884 err = mlx5_ib_alloc_counters(dev);
Kamal Heib45bded22017-01-18 14:10:32 +02003885 if (err)
3886 goto err_odp;
3887 }
Haggai Eran6aec21f2014-12-11 17:04:23 +02003888
Parav Pandit4a2da0b2017-05-30 10:05:15 +03003889 err = mlx5_ib_init_cong_debugfs(dev);
3890 if (err)
3891 goto err_cnt;
3892
Eli Cohen5fe9dec2017-01-03 23:55:25 +02003893 dev->mdev->priv.uar = mlx5_get_uars_page(dev->mdev);
3894 if (!dev->mdev->priv.uar)
Parav Pandit4a2da0b2017-05-30 10:05:15 +03003895 goto err_cong;
Eli Cohen5fe9dec2017-01-03 23:55:25 +02003896
3897 err = mlx5_alloc_bfreg(dev->mdev, &dev->bfreg, false, false);
3898 if (err)
3899 goto err_uar_page;
3900
3901 err = mlx5_alloc_bfreg(dev->mdev, &dev->fp_bfreg, false, true);
3902 if (err)
3903 goto err_bfreg;
3904
Mark Bloch0837e862016-06-17 15:10:55 +03003905 err = ib_register_device(&dev->ib_dev, NULL);
3906 if (err)
Eli Cohen5fe9dec2017-01-03 23:55:25 +02003907 goto err_fp_bfreg;
Mark Bloch0837e862016-06-17 15:10:55 +03003908
Eli Cohene126ba92013-07-07 17:25:49 +03003909 err = create_umr_res(dev);
3910 if (err)
3911 goto err_dev;
3912
Maor Gottlieb03404e82017-05-30 10:29:13 +03003913 init_delay_drop(dev);
3914
Eli Cohene126ba92013-07-07 17:25:49 +03003915 for (i = 0; i < ARRAY_SIZE(mlx5_class_attributes); i++) {
Wei Yongjun281d1a92013-07-30 07:54:26 +08003916 err = device_create_file(&dev->ib_dev.dev,
3917 mlx5_class_attributes[i]);
3918 if (err)
Maor Gottlieb03404e82017-05-30 10:29:13 +03003919 goto err_delay_drop;
Eli Cohene126ba92013-07-07 17:25:49 +03003920 }
3921
Huy Nguyenc85023e2017-05-30 09:42:54 +03003922 if ((MLX5_CAP_GEN(mdev, port_type) == MLX5_CAP_PORT_TYPE_ETH) &&
3923 MLX5_CAP_GEN(mdev, disable_local_lb))
3924 mutex_init(&dev->lb_mutex);
3925
Eli Cohene126ba92013-07-07 17:25:49 +03003926 dev->ib_active = true;
3927
Jack Morgenstein9603b612014-07-28 23:30:22 +03003928 return dev;
Eli Cohene126ba92013-07-07 17:25:49 +03003929
Maor Gottlieb03404e82017-05-30 10:29:13 +03003930err_delay_drop:
3931 cancel_delay_drop(dev);
Eli Cohene126ba92013-07-07 17:25:49 +03003932 destroy_umrc_res(dev);
3933
3934err_dev:
3935 ib_unregister_device(&dev->ib_dev);
3936
Eli Cohen5fe9dec2017-01-03 23:55:25 +02003937err_fp_bfreg:
3938 mlx5_free_bfreg(dev->mdev, &dev->fp_bfreg);
3939
3940err_bfreg:
3941 mlx5_free_bfreg(dev->mdev, &dev->bfreg);
3942
3943err_uar_page:
3944 mlx5_put_uars_page(dev->mdev, dev->mdev->priv.uar);
3945
Parav Pandite1f24a72017-04-16 07:29:29 +03003946err_cnt:
Parav Pandit4a2da0b2017-05-30 10:05:15 +03003947 mlx5_ib_cleanup_cong_debugfs(dev);
3948err_cong:
Kamal Heib45bded22017-01-18 14:10:32 +02003949 if (MLX5_CAP_GEN(dev->mdev, max_qp_cnt))
Parav Pandite1f24a72017-04-16 07:29:29 +03003950 mlx5_ib_dealloc_counters(dev);
Mark Bloch0837e862016-06-17 15:10:55 +03003951
Haggai Eran6aec21f2014-12-11 17:04:23 +02003952err_odp:
3953 mlx5_ib_odp_remove_one(dev);
3954
Eli Cohene126ba92013-07-07 17:25:49 +03003955err_rsrc:
3956 destroy_dev_resources(&dev->devr);
3957
Or Gerlitz45f95ac2016-11-27 16:51:35 +02003958err_disable_eth:
Aviv Heller5ec8c832016-09-18 20:48:00 +03003959 if (ll == IB_LINK_LAYER_ETHERNET) {
Or Gerlitz45f95ac2016-11-27 16:51:35 +02003960 mlx5_disable_eth(dev);
Or Gerlitzd012f5d2016-11-27 16:51:34 +02003961 mlx5_remove_netdev_notifier(dev);
Aviv Heller5ec8c832016-09-18 20:48:00 +03003962 }
Achiad Shochatfc24fc52015-12-23 18:47:17 +02003963
Mark Bloch0837e862016-06-17 15:10:55 +03003964err_free_port:
3965 kfree(dev->port);
3966
Jack Morgenstein9603b612014-07-28 23:30:22 +03003967err_dealloc:
Eli Cohene126ba92013-07-07 17:25:49 +03003968 ib_dealloc_device((struct ib_device *)dev);
3969
Jack Morgenstein9603b612014-07-28 23:30:22 +03003970 return NULL;
Eli Cohene126ba92013-07-07 17:25:49 +03003971}
3972
Jack Morgenstein9603b612014-07-28 23:30:22 +03003973static void mlx5_ib_remove(struct mlx5_core_dev *mdev, void *context)
Eli Cohene126ba92013-07-07 17:25:49 +03003974{
Jack Morgenstein9603b612014-07-28 23:30:22 +03003975 struct mlx5_ib_dev *dev = context;
Achiad Shochatfc24fc52015-12-23 18:47:17 +02003976 enum rdma_link_layer ll = mlx5_ib_port_link_layer(&dev->ib_dev, 1);
Haggai Eran6aec21f2014-12-11 17:04:23 +02003977
Maor Gottlieb03404e82017-05-30 10:29:13 +03003978 cancel_delay_drop(dev);
Or Gerlitzd012f5d2016-11-27 16:51:34 +02003979 mlx5_remove_netdev_notifier(dev);
Eli Cohene126ba92013-07-07 17:25:49 +03003980 ib_unregister_device(&dev->ib_dev);
Eli Cohen5fe9dec2017-01-03 23:55:25 +02003981 mlx5_free_bfreg(dev->mdev, &dev->fp_bfreg);
3982 mlx5_free_bfreg(dev->mdev, &dev->bfreg);
3983 mlx5_put_uars_page(dev->mdev, mdev->priv.uar);
Parav Pandit4a2da0b2017-05-30 10:05:15 +03003984 mlx5_ib_cleanup_cong_debugfs(dev);
Kamal Heib45bded22017-01-18 14:10:32 +02003985 if (MLX5_CAP_GEN(dev->mdev, max_qp_cnt))
Parav Pandite1f24a72017-04-16 07:29:29 +03003986 mlx5_ib_dealloc_counters(dev);
Eli Coheneefd56e2014-09-14 16:47:50 +03003987 destroy_umrc_res(dev);
Haggai Eran6aec21f2014-12-11 17:04:23 +02003988 mlx5_ib_odp_remove_one(dev);
Eli Cohene126ba92013-07-07 17:25:49 +03003989 destroy_dev_resources(&dev->devr);
Achiad Shochatfc24fc52015-12-23 18:47:17 +02003990 if (ll == IB_LINK_LAYER_ETHERNET)
Or Gerlitz45f95ac2016-11-27 16:51:35 +02003991 mlx5_disable_eth(dev);
Mark Bloch0837e862016-06-17 15:10:55 +03003992 kfree(dev->port);
Eli Cohene126ba92013-07-07 17:25:49 +03003993 ib_dealloc_device(&dev->ib_dev);
3994}
3995
Jack Morgenstein9603b612014-07-28 23:30:22 +03003996static struct mlx5_interface mlx5_ib_interface = {
3997 .add = mlx5_ib_add,
3998 .remove = mlx5_ib_remove,
3999 .event = mlx5_ib_event,
Artemy Kovalyovd9aaed82017-01-02 11:37:46 +02004000#ifdef CONFIG_INFINIBAND_ON_DEMAND_PAGING
4001 .pfault = mlx5_ib_pfault,
4002#endif
Saeed Mahameed64613d942015-04-02 17:07:34 +03004003 .protocol = MLX5_INTERFACE_PROTOCOL_IB,
Eli Cohene126ba92013-07-07 17:25:49 +03004004};
4005
4006static int __init mlx5_ib_init(void)
4007{
Haggai Eran6aec21f2014-12-11 17:04:23 +02004008 int err;
4009
Artemy Kovalyov81713d32017-01-18 16:58:11 +02004010 mlx5_ib_odp_init();
Jack Morgenstein9603b612014-07-28 23:30:22 +03004011
Haggai Eran6aec21f2014-12-11 17:04:23 +02004012 err = mlx5_register_interface(&mlx5_ib_interface);
Haggai Eran6aec21f2014-12-11 17:04:23 +02004013
4014 return err;
Eli Cohene126ba92013-07-07 17:25:49 +03004015}
4016
4017static void __exit mlx5_ib_cleanup(void)
4018{
Jack Morgenstein9603b612014-07-28 23:30:22 +03004019 mlx5_unregister_interface(&mlx5_ib_interface);
Eli Cohene126ba92013-07-07 17:25:49 +03004020}
4021
4022module_init(mlx5_ib_init);
4023module_exit(mlx5_ib_cleanup);