blob: 532818785a7f9cd5a699c393ee120d201b963926 [file] [log] [blame]
Roland Dreier225c7b12007-05-08 18:00:38 -07001/*
2 * Copyright (c) 2007 Cisco Systems, Inc. All rights reserved.
3 *
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
33#include <rdma/ib_mad.h>
34#include <rdma/ib_smi.h>
Jack Morgenstein37bfc7c2012-08-03 08:40:44 +000035#include <rdma/ib_sa.h>
36#include <rdma/ib_cache.h>
Roland Dreier225c7b12007-05-08 18:00:38 -070037
Jack Morgensteinafa8fd12012-08-03 08:40:56 +000038#include <linux/random.h>
Roland Dreier225c7b12007-05-08 18:00:38 -070039#include <linux/mlx4/cmd.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090040#include <linux/gfp.h>
Or Gerlitzc3779132011-06-15 14:51:27 +000041#include <rdma/ib_pma.h>
Roland Dreier225c7b12007-05-08 18:00:38 -070042
43#include "mlx4_ib.h"
44
45enum {
46 MLX4_IB_VENDOR_CLASS1 = 0x9,
47 MLX4_IB_VENDOR_CLASS2 = 0xa
48};
49
Jack Morgensteinfc065732012-08-03 08:40:42 +000050#define MLX4_TUN_SEND_WRID_SHIFT 34
51#define MLX4_TUN_QPN_SHIFT 32
52#define MLX4_TUN_WRID_RECV (((u64) 1) << MLX4_TUN_SEND_WRID_SHIFT)
53#define MLX4_TUN_SET_WRID_QPN(a) (((u64) ((a) & 0x3)) << MLX4_TUN_QPN_SHIFT)
54
55#define MLX4_TUN_IS_RECV(a) (((a) >> MLX4_TUN_SEND_WRID_SHIFT) & 0x1)
56#define MLX4_TUN_WRID_QPN(a) (((a) >> MLX4_TUN_QPN_SHIFT) & 0x3)
57
Jack Morgenstein2a4fae12012-08-03 08:40:50 +000058 /* Port mgmt change event handling */
59
60#define GET_BLK_PTR_FROM_EQE(eqe) be32_to_cpu(eqe->event.port_mgmt_change.params.tbl_change_info.block_ptr)
61#define GET_MASK_FROM_EQE(eqe) be32_to_cpu(eqe->event.port_mgmt_change.params.tbl_change_info.tbl_entries_mask)
62#define NUM_IDX_IN_PKEY_TBL_BLK 32
63#define GUID_TBL_ENTRY_SIZE 8 /* size in bytes */
64#define GUID_TBL_BLK_NUM_ENTRIES 8
65#define GUID_TBL_BLK_SIZE (GUID_TBL_ENTRY_SIZE * GUID_TBL_BLK_NUM_ENTRIES)
66
Majd Dibbiny61a38552015-03-18 16:51:37 +020067/* Counters should be saturate once they reach their maximum value */
68#define ASSIGN_32BIT_COUNTER(counter, value) do {\
69 if ((value) > U32_MAX) \
70 counter = cpu_to_be32(U32_MAX); \
71 else \
72 counter = cpu_to_be32(value); \
73} while (0)
74
Jack Morgensteinfc065732012-08-03 08:40:42 +000075struct mlx4_mad_rcv_buf {
76 struct ib_grh grh;
77 u8 payload[256];
78} __packed;
79
80struct mlx4_mad_snd_buf {
81 u8 payload[256];
82} __packed;
83
84struct mlx4_tunnel_mad {
85 struct ib_grh grh;
86 struct mlx4_ib_tunnel_header hdr;
87 struct ib_mad mad;
88} __packed;
89
90struct mlx4_rcv_tunnel_mad {
91 struct mlx4_rcv_tunnel_hdr hdr;
92 struct ib_grh grh;
93 struct ib_mad mad;
94} __packed;
95
Oren Duerb9c5d6a2012-08-03 08:40:46 +000096static void handle_client_rereg_event(struct mlx4_ib_dev *dev, u8 port_num);
Jack Morgenstein2a4fae12012-08-03 08:40:50 +000097static void handle_lid_change_event(struct mlx4_ib_dev *dev, u8 port_num);
98static void __propagate_pkey_ev(struct mlx4_ib_dev *dev, int port_num,
99 int block, u32 change_bitmap);
Oren Duerb9c5d6a2012-08-03 08:40:46 +0000100
Jack Morgensteinafa8fd12012-08-03 08:40:56 +0000101__be64 mlx4_ib_gen_node_guid(void)
102{
103#define NODE_GUID_HI ((u64) (((u64)IB_OPENIB_OUI) << 40))
Andrew Morton50bea5c2013-05-07 16:18:16 -0700104 return cpu_to_be64(NODE_GUID_HI | prandom_u32());
Jack Morgensteinafa8fd12012-08-03 08:40:56 +0000105}
106
Oren Duerb9c5d6a2012-08-03 08:40:46 +0000107__be64 mlx4_ib_get_new_demux_tid(struct mlx4_ib_demux_ctx *ctx)
108{
109 return cpu_to_be64(atomic_inc_return(&ctx->tid)) |
110 cpu_to_be64(0xff00000000000000LL);
111}
112
Jack Morgenstein0a9a0182012-08-03 08:40:45 +0000113int mlx4_MAD_IFC(struct mlx4_ib_dev *dev, int mad_ifc_flags,
Ira Weinya97e2d82015-05-31 17:15:30 -0400114 int port, const struct ib_wc *in_wc,
115 const struct ib_grh *in_grh,
116 const void *in_mad, void *response_mad)
Roland Dreier225c7b12007-05-08 18:00:38 -0700117{
118 struct mlx4_cmd_mailbox *inmailbox, *outmailbox;
119 void *inbox;
120 int err;
121 u32 in_modifier = port;
122 u8 op_modifier = 0;
123
124 inmailbox = mlx4_alloc_cmd_mailbox(dev->dev);
125 if (IS_ERR(inmailbox))
126 return PTR_ERR(inmailbox);
127 inbox = inmailbox->buf;
128
129 outmailbox = mlx4_alloc_cmd_mailbox(dev->dev);
130 if (IS_ERR(outmailbox)) {
131 mlx4_free_cmd_mailbox(dev->dev, inmailbox);
132 return PTR_ERR(outmailbox);
133 }
134
135 memcpy(inbox, in_mad, 256);
136
137 /*
138 * Key check traps can't be generated unless we have in_wc to
139 * tell us where to send the trap.
140 */
Jack Morgenstein0a9a0182012-08-03 08:40:45 +0000141 if ((mad_ifc_flags & MLX4_MAD_IFC_IGNORE_MKEY) || !in_wc)
Roland Dreier225c7b12007-05-08 18:00:38 -0700142 op_modifier |= 0x1;
Jack Morgenstein0a9a0182012-08-03 08:40:45 +0000143 if ((mad_ifc_flags & MLX4_MAD_IFC_IGNORE_BKEY) || !in_wc)
Roland Dreier225c7b12007-05-08 18:00:38 -0700144 op_modifier |= 0x2;
Jack Morgenstein0a9a0182012-08-03 08:40:45 +0000145 if (mlx4_is_mfunc(dev->dev) &&
146 (mad_ifc_flags & MLX4_MAD_IFC_NET_VIEW || in_wc))
147 op_modifier |= 0x8;
Roland Dreier225c7b12007-05-08 18:00:38 -0700148
149 if (in_wc) {
150 struct {
151 __be32 my_qpn;
152 u32 reserved1;
153 __be32 rqpn;
154 u8 sl;
155 u8 g_path;
156 u16 reserved2[2];
157 __be16 pkey;
158 u32 reserved3[11];
159 u8 grh[40];
160 } *ext_info;
161
162 memset(inbox + 256, 0, 256);
163 ext_info = inbox + 256;
164
165 ext_info->my_qpn = cpu_to_be32(in_wc->qp->qp_num);
166 ext_info->rqpn = cpu_to_be32(in_wc->src_qp);
167 ext_info->sl = in_wc->sl << 4;
168 ext_info->g_path = in_wc->dlid_path_bits |
169 (in_wc->wc_flags & IB_WC_GRH ? 0x80 : 0);
170 ext_info->pkey = cpu_to_be16(in_wc->pkey_index);
171
172 if (in_grh)
173 memcpy(ext_info->grh, in_grh, 40);
174
175 op_modifier |= 0x4;
176
177 in_modifier |= in_wc->slid << 16;
178 }
179
Jack Morgenstein0a9a0182012-08-03 08:40:45 +0000180 err = mlx4_cmd_box(dev->dev, inmailbox->dma, outmailbox->dma, in_modifier,
181 mlx4_is_master(dev->dev) ? (op_modifier & ~0x8) : op_modifier,
Jack Morgensteinf9baff52011-12-13 04:10:51 +0000182 MLX4_CMD_MAD_IFC, MLX4_CMD_TIME_CLASS_C,
Jack Morgenstein0a9a0182012-08-03 08:40:45 +0000183 (op_modifier & 0x8) ? MLX4_CMD_NATIVE : MLX4_CMD_WRAPPED);
Roland Dreier225c7b12007-05-08 18:00:38 -0700184
Ilpo Järvinenfe11cb62007-08-16 01:02:07 +0300185 if (!err)
Roland Dreier225c7b12007-05-08 18:00:38 -0700186 memcpy(response_mad, outmailbox->buf, 256);
187
188 mlx4_free_cmd_mailbox(dev->dev, inmailbox);
189 mlx4_free_cmd_mailbox(dev->dev, outmailbox);
190
191 return err;
192}
193
194static void update_sm_ah(struct mlx4_ib_dev *dev, u8 port_num, u16 lid, u8 sl)
195{
196 struct ib_ah *new_ah;
197 struct ib_ah_attr ah_attr;
Jack Morgensteindf7fba62012-08-03 08:26:45 +0000198 unsigned long flags;
Roland Dreier225c7b12007-05-08 18:00:38 -0700199
200 if (!dev->send_agent[port_num - 1][0])
201 return;
202
203 memset(&ah_attr, 0, sizeof ah_attr);
204 ah_attr.dlid = lid;
205 ah_attr.sl = sl;
206 ah_attr.port_num = port_num;
207
208 new_ah = ib_create_ah(dev->send_agent[port_num - 1][0]->qp->pd,
209 &ah_attr);
210 if (IS_ERR(new_ah))
211 return;
212
Jack Morgensteindf7fba62012-08-03 08:26:45 +0000213 spin_lock_irqsave(&dev->sm_lock, flags);
Roland Dreier225c7b12007-05-08 18:00:38 -0700214 if (dev->sm_ah[port_num - 1])
215 ib_destroy_ah(dev->sm_ah[port_num - 1]);
216 dev->sm_ah[port_num - 1] = new_ah;
Jack Morgensteindf7fba62012-08-03 08:26:45 +0000217 spin_unlock_irqrestore(&dev->sm_lock, flags);
Roland Dreier225c7b12007-05-08 18:00:38 -0700218}
219
220/*
Jack Morgenstein00f5ce92012-06-19 11:21:40 +0300221 * Snoop SM MADs for port info, GUID info, and P_Key table sets, so we can
222 * synthesize LID change, Client-Rereg, GID change, and P_Key change events.
Roland Dreier225c7b12007-05-08 18:00:38 -0700223 */
Ira Weinya97e2d82015-05-31 17:15:30 -0400224static void smp_snoop(struct ib_device *ibdev, u8 port_num, const struct ib_mad *mad,
Jack Morgenstein00f5ce92012-06-19 11:21:40 +0300225 u16 prev_lid)
Roland Dreier225c7b12007-05-08 18:00:38 -0700226{
Jack Morgenstein00f5ce92012-06-19 11:21:40 +0300227 struct ib_port_info *pinfo;
228 u16 lid;
Jack Morgenstein54679e12012-08-03 08:40:43 +0000229 __be16 *base;
230 u32 bn, pkey_change_bitmap;
231 int i;
232
Roland Dreier225c7b12007-05-08 18:00:38 -0700233
Jack Morgenstein00f5ce92012-06-19 11:21:40 +0300234 struct mlx4_ib_dev *dev = to_mdev(ibdev);
Roland Dreier225c7b12007-05-08 18:00:38 -0700235 if ((mad->mad_hdr.mgmt_class == IB_MGMT_CLASS_SUBN_LID_ROUTED ||
236 mad->mad_hdr.mgmt_class == IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE) &&
Jack Morgenstein00f5ce92012-06-19 11:21:40 +0300237 mad->mad_hdr.method == IB_MGMT_METHOD_SET)
238 switch (mad->mad_hdr.attr_id) {
239 case IB_SMP_ATTR_PORT_INFO:
240 pinfo = (struct ib_port_info *) ((struct ib_smp *) mad)->data;
241 lid = be16_to_cpu(pinfo->lid);
Roland Dreier225c7b12007-05-08 18:00:38 -0700242
Jack Morgenstein00f5ce92012-06-19 11:21:40 +0300243 update_sm_ah(dev, port_num,
Roland Dreier225c7b12007-05-08 18:00:38 -0700244 be16_to_cpu(pinfo->sm_lid),
245 pinfo->neighbormtu_mastersmsl & 0xf);
246
Jack Morgenstein00f5ce92012-06-19 11:21:40 +0300247 if (pinfo->clientrereg_resv_subnetto & 0x80)
Oren Duerb9c5d6a2012-08-03 08:40:46 +0000248 handle_client_rereg_event(dev, port_num);
Roland Dreier225c7b12007-05-08 18:00:38 -0700249
Jack Morgenstein00f5ce92012-06-19 11:21:40 +0300250 if (prev_lid != lid)
Jack Morgenstein2a4fae12012-08-03 08:40:50 +0000251 handle_lid_change_event(dev, port_num);
Jack Morgenstein00f5ce92012-06-19 11:21:40 +0300252 break;
Roland Dreier225c7b12007-05-08 18:00:38 -0700253
Jack Morgenstein00f5ce92012-06-19 11:21:40 +0300254 case IB_SMP_ATTR_PKEY_TABLE:
Jack Morgenstein54679e12012-08-03 08:40:43 +0000255 if (!mlx4_is_mfunc(dev->dev)) {
256 mlx4_ib_dispatch_event(dev, port_num,
257 IB_EVENT_PKEY_CHANGE);
258 break;
259 }
260
Jack Morgenstein2a4fae12012-08-03 08:40:50 +0000261 /* at this point, we are running in the master.
262 * Slaves do not receive SMPs.
263 */
Jack Morgenstein54679e12012-08-03 08:40:43 +0000264 bn = be32_to_cpu(((struct ib_smp *)mad)->attr_mod) & 0xFFFF;
265 base = (__be16 *) &(((struct ib_smp *)mad)->data[0]);
266 pkey_change_bitmap = 0;
267 for (i = 0; i < 32; i++) {
268 pr_debug("PKEY[%d] = x%x\n",
269 i + bn*32, be16_to_cpu(base[i]));
270 if (be16_to_cpu(base[i]) !=
271 dev->pkeys.phys_pkey_cache[port_num - 1][i + bn*32]) {
272 pkey_change_bitmap |= (1 << i);
273 dev->pkeys.phys_pkey_cache[port_num - 1][i + bn*32] =
274 be16_to_cpu(base[i]);
275 }
276 }
277 pr_debug("PKEY Change event: port=%d, "
278 "block=0x%x, change_bitmap=0x%x\n",
279 port_num, bn, pkey_change_bitmap);
280
Jack Morgenstein2a4fae12012-08-03 08:40:50 +0000281 if (pkey_change_bitmap) {
Jack Morgenstein54679e12012-08-03 08:40:43 +0000282 mlx4_ib_dispatch_event(dev, port_num,
283 IB_EVENT_PKEY_CHANGE);
Jack Morgenstein2a4fae12012-08-03 08:40:50 +0000284 if (!dev->sriov.is_going_down)
285 __propagate_pkey_ev(dev, port_num, bn,
286 pkey_change_bitmap);
287 }
Jack Morgenstein00f5ce92012-06-19 11:21:40 +0300288 break;
289
290 case IB_SMP_ATTR_GUID_INFO:
Jack Morgenstein66349612012-06-19 11:21:44 +0300291 /* paravirtualized master's guid is guid 0 -- does not change */
292 if (!mlx4_is_master(dev->dev))
293 mlx4_ib_dispatch_event(dev, port_num,
294 IB_EVENT_GID_CHANGE);
Jack Morgenstein2a4fae12012-08-03 08:40:50 +0000295 /*if master, notify relevant slaves*/
296 if (mlx4_is_master(dev->dev) &&
297 !dev->sriov.is_going_down) {
298 bn = be32_to_cpu(((struct ib_smp *)mad)->attr_mod);
299 mlx4_ib_update_cache_on_guid_change(dev, bn, port_num,
300 (u8 *)(&((struct ib_smp *)mad)->data));
301 mlx4_ib_notify_slaves_on_guid_change(dev, bn, port_num,
302 (u8 *)(&((struct ib_smp *)mad)->data));
303 }
Jack Morgenstein00f5ce92012-06-19 11:21:40 +0300304 break;
Jack Morgenstein2a4fae12012-08-03 08:40:50 +0000305
Jack Morgenstein00f5ce92012-06-19 11:21:40 +0300306 default:
307 break;
Roland Dreier225c7b12007-05-08 18:00:38 -0700308 }
Roland Dreier225c7b12007-05-08 18:00:38 -0700309}
310
Jack Morgenstein2a4fae12012-08-03 08:40:50 +0000311static void __propagate_pkey_ev(struct mlx4_ib_dev *dev, int port_num,
312 int block, u32 change_bitmap)
313{
314 int i, ix, slave, err;
315 int have_event = 0;
316
317 for (slave = 0; slave < dev->dev->caps.sqp_demux; slave++) {
318 if (slave == mlx4_master_func_num(dev->dev))
319 continue;
320 if (!mlx4_is_slave_active(dev->dev, slave))
321 continue;
322
323 have_event = 0;
324 for (i = 0; i < 32; i++) {
325 if (!(change_bitmap & (1 << i)))
326 continue;
327 for (ix = 0;
328 ix < dev->dev->caps.pkey_table_len[port_num]; ix++) {
329 if (dev->pkeys.virt2phys_pkey[slave][port_num - 1]
330 [ix] == i + 32 * block) {
331 err = mlx4_gen_pkey_eqe(dev->dev, slave, port_num);
332 pr_debug("propagate_pkey_ev: slave %d,"
333 " port %d, ix %d (%d)\n",
334 slave, port_num, ix, err);
335 have_event = 1;
336 break;
337 }
338 }
339 if (have_event)
340 break;
341 }
342 }
343}
344
Roland Dreier225c7b12007-05-08 18:00:38 -0700345static void node_desc_override(struct ib_device *dev,
346 struct ib_mad *mad)
347{
Jack Morgensteindf7fba62012-08-03 08:26:45 +0000348 unsigned long flags;
349
Roland Dreier225c7b12007-05-08 18:00:38 -0700350 if ((mad->mad_hdr.mgmt_class == IB_MGMT_CLASS_SUBN_LID_ROUTED ||
351 mad->mad_hdr.mgmt_class == IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE) &&
352 mad->mad_hdr.method == IB_MGMT_METHOD_GET_RESP &&
353 mad->mad_hdr.attr_id == IB_SMP_ATTR_NODE_DESC) {
Jack Morgensteindf7fba62012-08-03 08:26:45 +0000354 spin_lock_irqsave(&to_mdev(dev)->sm_lock, flags);
Roland Dreier225c7b12007-05-08 18:00:38 -0700355 memcpy(((struct ib_smp *) mad)->data, dev->node_desc, 64);
Jack Morgensteindf7fba62012-08-03 08:26:45 +0000356 spin_unlock_irqrestore(&to_mdev(dev)->sm_lock, flags);
Roland Dreier225c7b12007-05-08 18:00:38 -0700357 }
358}
359
Ira Weinya97e2d82015-05-31 17:15:30 -0400360static void forward_trap(struct mlx4_ib_dev *dev, u8 port_num, const struct ib_mad *mad)
Roland Dreier225c7b12007-05-08 18:00:38 -0700361{
362 int qpn = mad->mad_hdr.mgmt_class != IB_MGMT_CLASS_SUBN_LID_ROUTED;
363 struct ib_mad_send_buf *send_buf;
364 struct ib_mad_agent *agent = dev->send_agent[port_num - 1][qpn];
365 int ret;
Jack Morgensteindf7fba62012-08-03 08:26:45 +0000366 unsigned long flags;
Roland Dreier225c7b12007-05-08 18:00:38 -0700367
368 if (agent) {
369 send_buf = ib_create_send_mad(agent, qpn, 0, 0, IB_MGMT_MAD_HDR,
Ira Weinyda2dfaa2015-06-06 14:38:28 -0400370 IB_MGMT_MAD_DATA, GFP_ATOMIC,
371 IB_MGMT_BASE_VERSION);
Dan Carpenter13974902011-01-10 17:42:06 -0800372 if (IS_ERR(send_buf))
373 return;
Roland Dreier225c7b12007-05-08 18:00:38 -0700374 /*
375 * We rely here on the fact that MLX QPs don't use the
376 * address handle after the send is posted (this is
377 * wrong following the IB spec strictly, but we know
378 * it's OK for our devices).
379 */
Jack Morgensteindf7fba62012-08-03 08:26:45 +0000380 spin_lock_irqsave(&dev->sm_lock, flags);
Roland Dreier225c7b12007-05-08 18:00:38 -0700381 memcpy(send_buf->mad, mad, sizeof *mad);
382 if ((send_buf->ah = dev->sm_ah[port_num - 1]))
383 ret = ib_post_send_mad(send_buf, NULL);
384 else
385 ret = -EINVAL;
Jack Morgensteindf7fba62012-08-03 08:26:45 +0000386 spin_unlock_irqrestore(&dev->sm_lock, flags);
Roland Dreier225c7b12007-05-08 18:00:38 -0700387
388 if (ret)
389 ib_free_send_mad(send_buf);
390 }
391}
392
Jack Morgenstein37bfc7c2012-08-03 08:40:44 +0000393static int mlx4_ib_demux_sa_handler(struct ib_device *ibdev, int port, int slave,
394 struct ib_sa_mad *sa_mad)
395{
Oren Duerb9c5d6a2012-08-03 08:40:46 +0000396 int ret = 0;
397
398 /* dispatch to different sa handlers */
399 switch (be16_to_cpu(sa_mad->mad_hdr.attr_id)) {
400 case IB_SA_ATTR_MC_MEMBER_REC:
401 ret = mlx4_ib_mcg_demux_handler(ibdev, port, slave, sa_mad);
402 break;
403 default:
404 break;
405 }
406 return ret;
Jack Morgenstein37bfc7c2012-08-03 08:40:44 +0000407}
408
409int mlx4_ib_find_real_gid(struct ib_device *ibdev, u8 port, __be64 guid)
410{
411 struct mlx4_ib_dev *dev = to_mdev(ibdev);
412 int i;
413
414 for (i = 0; i < dev->dev->caps.sqp_demux; i++) {
415 if (dev->sriov.demux[port - 1].guid_cache[i] == guid)
416 return i;
417 }
418 return -1;
419}
420
421
Jack Morgenstein2c75d2c2012-10-17 16:42:58 +0000422static int find_slave_port_pkey_ix(struct mlx4_ib_dev *dev, int slave,
423 u8 port, u16 pkey, u16 *ix)
Jack Morgenstein37bfc7c2012-08-03 08:40:44 +0000424{
Jack Morgenstein2c75d2c2012-10-17 16:42:58 +0000425 int i, ret;
426 u8 unassigned_pkey_ix, pkey_ix, partial_ix = 0xFF;
427 u16 slot_pkey;
Jack Morgenstein37bfc7c2012-08-03 08:40:44 +0000428
Jack Morgenstein2c75d2c2012-10-17 16:42:58 +0000429 if (slave == mlx4_master_func_num(dev->dev))
430 return ib_find_cached_pkey(&dev->ib_dev, port, pkey, ix);
Jack Morgenstein37bfc7c2012-08-03 08:40:44 +0000431
Jack Morgenstein2c75d2c2012-10-17 16:42:58 +0000432 unassigned_pkey_ix = dev->dev->phys_caps.pkey_phys_table_len[port] - 1;
433
434 for (i = 0; i < dev->dev->caps.pkey_table_len[port]; i++) {
435 if (dev->pkeys.virt2phys_pkey[slave][port - 1][i] == unassigned_pkey_ix)
436 continue;
437
438 pkey_ix = dev->pkeys.virt2phys_pkey[slave][port - 1][i];
439
440 ret = ib_get_cached_pkey(&dev->ib_dev, port, pkey_ix, &slot_pkey);
441 if (ret)
442 continue;
443 if ((slot_pkey & 0x7FFF) == (pkey & 0x7FFF)) {
444 if (slot_pkey & 0x8000) {
445 *ix = (u16) pkey_ix;
446 return 0;
447 } else {
448 /* take first partial pkey index found */
449 if (partial_ix == 0xFF)
450 partial_ix = pkey_ix;
451 }
452 }
Jack Morgenstein37bfc7c2012-08-03 08:40:44 +0000453 }
454
Jack Morgenstein2c75d2c2012-10-17 16:42:58 +0000455 if (partial_ix < 0xFF) {
456 *ix = (u16) partial_ix;
457 return 0;
458 }
Jack Morgenstein37bfc7c2012-08-03 08:40:44 +0000459
Jack Morgenstein2c75d2c2012-10-17 16:42:58 +0000460 return -EINVAL;
Jack Morgenstein37bfc7c2012-08-03 08:40:44 +0000461}
462
463int mlx4_ib_send_to_slave(struct mlx4_ib_dev *dev, int slave, u8 port,
464 enum ib_qp_type dest_qpt, struct ib_wc *wc,
465 struct ib_grh *grh, struct ib_mad *mad)
466{
467 struct ib_sge list;
468 struct ib_send_wr wr, *bad_wr;
469 struct mlx4_ib_demux_pv_ctx *tun_ctx;
470 struct mlx4_ib_demux_pv_qp *tun_qp;
471 struct mlx4_rcv_tunnel_mad *tun_mad;
472 struct ib_ah_attr attr;
473 struct ib_ah *ah;
474 struct ib_qp *src_qp = NULL;
475 unsigned tun_tx_ix = 0;
476 int dqpn;
477 int ret = 0;
Jack Morgenstein37bfc7c2012-08-03 08:40:44 +0000478 u16 tun_pkey_ix;
Jack Morgenstein2c75d2c2012-10-17 16:42:58 +0000479 u16 cached_pkey;
Jack Morgenstein6ee51a42014-03-12 12:00:37 +0200480 u8 is_eth = dev->dev->caps.port_type[port] == MLX4_PORT_TYPE_ETH;
Jack Morgenstein37bfc7c2012-08-03 08:40:44 +0000481
482 if (dest_qpt > IB_QPT_GSI)
483 return -EINVAL;
484
485 tun_ctx = dev->sriov.demux[port-1].tun[slave];
486
487 /* check if proxy qp created */
488 if (!tun_ctx || tun_ctx->state != DEMUX_PV_STATE_ACTIVE)
489 return -EAGAIN;
490
Jack Morgenstein37bfc7c2012-08-03 08:40:44 +0000491 if (!dest_qpt)
492 tun_qp = &tun_ctx->qp[0];
493 else
494 tun_qp = &tun_ctx->qp[1];
495
Jack Morgenstein2c75d2c2012-10-17 16:42:58 +0000496 /* compute P_Key index to put in tunnel header for slave */
Jack Morgenstein37bfc7c2012-08-03 08:40:44 +0000497 if (dest_qpt) {
Jack Morgenstein2c75d2c2012-10-17 16:42:58 +0000498 u16 pkey_ix;
499 ret = ib_get_cached_pkey(&dev->ib_dev, port, wc->pkey_index, &cached_pkey);
Jack Morgenstein37bfc7c2012-08-03 08:40:44 +0000500 if (ret)
501 return -EINVAL;
502
Jack Morgenstein2c75d2c2012-10-17 16:42:58 +0000503 ret = find_slave_port_pkey_ix(dev, slave, port, cached_pkey, &pkey_ix);
504 if (ret)
Jack Morgenstein37bfc7c2012-08-03 08:40:44 +0000505 return -EINVAL;
Jack Morgenstein2c75d2c2012-10-17 16:42:58 +0000506 tun_pkey_ix = pkey_ix;
Jack Morgenstein37bfc7c2012-08-03 08:40:44 +0000507 } else
508 tun_pkey_ix = dev->pkeys.virt2phys_pkey[slave][port - 1][0];
509
Jack Morgenstein47605df2012-08-03 08:40:57 +0000510 dqpn = dev->dev->phys_caps.base_proxy_sqpn + 8 * slave + port + (dest_qpt * 2) - 1;
Jack Morgenstein37bfc7c2012-08-03 08:40:44 +0000511
512 /* get tunnel tx data buf for slave */
513 src_qp = tun_qp->qp;
514
515 /* create ah. Just need an empty one with the port num for the post send.
516 * The driver will set the force loopback bit in post_send */
517 memset(&attr, 0, sizeof attr);
518 attr.port_num = port;
Jack Morgenstein6ee51a42014-03-12 12:00:37 +0200519 if (is_eth) {
Jack Morgensteinb6ffaef2014-03-12 12:00:39 +0200520 memcpy(&attr.grh.dgid.raw[0], &grh->dgid.raw[0], 16);
Jack Morgenstein6ee51a42014-03-12 12:00:37 +0200521 attr.ah_flags = IB_AH_GRH;
522 }
Jack Morgenstein37bfc7c2012-08-03 08:40:44 +0000523 ah = ib_create_ah(tun_ctx->pd, &attr);
524 if (IS_ERR(ah))
525 return -ENOMEM;
526
527 /* allocate tunnel tx buf after pass failure returns */
528 spin_lock(&tun_qp->tx_lock);
529 if (tun_qp->tx_ix_head - tun_qp->tx_ix_tail >=
530 (MLX4_NUM_TUNNEL_BUFS - 1))
531 ret = -EAGAIN;
532 else
533 tun_tx_ix = (++tun_qp->tx_ix_head) & (MLX4_NUM_TUNNEL_BUFS - 1);
534 spin_unlock(&tun_qp->tx_lock);
535 if (ret)
536 goto out;
537
538 tun_mad = (struct mlx4_rcv_tunnel_mad *) (tun_qp->tx_ring[tun_tx_ix].buf.addr);
539 if (tun_qp->tx_ring[tun_tx_ix].ah)
540 ib_destroy_ah(tun_qp->tx_ring[tun_tx_ix].ah);
541 tun_qp->tx_ring[tun_tx_ix].ah = ah;
542 ib_dma_sync_single_for_cpu(&dev->ib_dev,
543 tun_qp->tx_ring[tun_tx_ix].buf.map,
544 sizeof (struct mlx4_rcv_tunnel_mad),
545 DMA_TO_DEVICE);
546
547 /* copy over to tunnel buffer */
548 if (grh)
549 memcpy(&tun_mad->grh, grh, sizeof *grh);
550 memcpy(&tun_mad->mad, mad, sizeof *mad);
551
552 /* adjust tunnel data */
553 tun_mad->hdr.pkey_index = cpu_to_be16(tun_pkey_ix);
Jack Morgenstein37bfc7c2012-08-03 08:40:44 +0000554 tun_mad->hdr.flags_src_qp = cpu_to_be32(wc->src_qp & 0xFFFFFF);
555 tun_mad->hdr.g_ml_path = (grh && (wc->wc_flags & IB_WC_GRH)) ? 0x80 : 0;
556
Jack Morgenstein5ea8bbf2014-03-12 12:00:41 +0200557 if (is_eth) {
558 u16 vlan = 0;
559 if (mlx4_get_slave_default_vlan(dev->dev, port, slave, &vlan,
560 NULL)) {
561 /* VST mode */
562 if (vlan != wc->vlan_id)
563 /* Packet vlan is not the VST-assigned vlan.
564 * Drop the packet.
565 */
566 goto out;
567 else
568 /* Remove the vlan tag before forwarding
569 * the packet to the VF.
570 */
571 vlan = 0xffff;
572 } else {
573 vlan = wc->vlan_id;
574 }
575
576 tun_mad->hdr.sl_vid = cpu_to_be16(vlan);
577 memcpy((char *)&tun_mad->hdr.mac_31_0, &(wc->smac[0]), 4);
578 memcpy((char *)&tun_mad->hdr.slid_mac_47_32, &(wc->smac[4]), 2);
579 } else {
580 tun_mad->hdr.sl_vid = cpu_to_be16(((u16)(wc->sl)) << 12);
581 tun_mad->hdr.slid_mac_47_32 = cpu_to_be16(wc->slid);
582 }
583
Jack Morgenstein37bfc7c2012-08-03 08:40:44 +0000584 ib_dma_sync_single_for_device(&dev->ib_dev,
585 tun_qp->tx_ring[tun_tx_ix].buf.map,
586 sizeof (struct mlx4_rcv_tunnel_mad),
587 DMA_TO_DEVICE);
588
589 list.addr = tun_qp->tx_ring[tun_tx_ix].buf.map;
590 list.length = sizeof (struct mlx4_rcv_tunnel_mad);
591 list.lkey = tun_ctx->mr->lkey;
592
593 wr.wr.ud.ah = ah;
594 wr.wr.ud.port_num = port;
595 wr.wr.ud.remote_qkey = IB_QP_SET_QKEY;
596 wr.wr.ud.remote_qpn = dqpn;
597 wr.next = NULL;
598 wr.wr_id = ((u64) tun_tx_ix) | MLX4_TUN_SET_WRID_QPN(dest_qpt);
599 wr.sg_list = &list;
600 wr.num_sge = 1;
601 wr.opcode = IB_WR_SEND;
602 wr.send_flags = IB_SEND_SIGNALED;
603
604 ret = ib_post_send(src_qp, &wr, &bad_wr);
605out:
606 if (ret)
607 ib_destroy_ah(ah);
608 return ret;
609}
610
611static int mlx4_ib_demux_mad(struct ib_device *ibdev, u8 port,
612 struct ib_wc *wc, struct ib_grh *grh,
613 struct ib_mad *mad)
614{
615 struct mlx4_ib_dev *dev = to_mdev(ibdev);
616 int err;
617 int slave;
618 u8 *slave_id;
Jack Morgenstein6ee51a42014-03-12 12:00:37 +0200619 int is_eth = 0;
620
621 if (rdma_port_get_link_layer(ibdev, port) == IB_LINK_LAYER_INFINIBAND)
622 is_eth = 0;
623 else
624 is_eth = 1;
625
626 if (is_eth) {
627 if (!(wc->wc_flags & IB_WC_GRH)) {
628 mlx4_ib_warn(ibdev, "RoCE grh not present.\n");
629 return -EINVAL;
630 }
631 if (mad->mad_hdr.mgmt_class != IB_MGMT_CLASS_CM) {
632 mlx4_ib_warn(ibdev, "RoCE mgmt class is not CM\n");
633 return -EINVAL;
634 }
635 if (mlx4_get_slave_from_roce_gid(dev->dev, port, grh->dgid.raw, &slave)) {
636 mlx4_ib_warn(ibdev, "failed matching grh\n");
637 return -ENOENT;
638 }
639 if (slave >= dev->dev->caps.sqp_demux) {
640 mlx4_ib_warn(ibdev, "slave id: %d is bigger than allowed:%d\n",
641 slave, dev->dev->caps.sqp_demux);
642 return -ENOENT;
643 }
644
645 if (mlx4_ib_demux_cm_handler(ibdev, port, NULL, mad))
646 return 0;
647
648 err = mlx4_ib_send_to_slave(dev, slave, port, wc->qp->qp_type, wc, grh, mad);
649 if (err)
650 pr_debug("failed sending to slave %d via tunnel qp (%d)\n",
651 slave, err);
652 return 0;
653 }
Jack Morgenstein37bfc7c2012-08-03 08:40:44 +0000654
655 /* Initially assume that this mad is for us */
656 slave = mlx4_master_func_num(dev->dev);
657
658 /* See if the slave id is encoded in a response mad */
659 if (mad->mad_hdr.method & 0x80) {
660 slave_id = (u8 *) &mad->mad_hdr.tid;
661 slave = *slave_id;
662 if (slave != 255) /*255 indicates the dom0*/
663 *slave_id = 0; /* remap tid */
664 }
665
666 /* If a grh is present, we demux according to it */
667 if (wc->wc_flags & IB_WC_GRH) {
668 slave = mlx4_ib_find_real_gid(ibdev, port, grh->dgid.global.interface_id);
669 if (slave < 0) {
670 mlx4_ib_warn(ibdev, "failed matching grh\n");
671 return -ENOENT;
672 }
673 }
674 /* Class-specific handling */
675 switch (mad->mad_hdr.mgmt_class) {
Jack Morgenstein97982f52014-05-29 16:31:02 +0300676 case IB_MGMT_CLASS_SUBN_LID_ROUTED:
677 case IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE:
678 /* 255 indicates the dom0 */
679 if (slave != 255 && slave != mlx4_master_func_num(dev->dev)) {
680 if (!mlx4_vf_smi_enabled(dev->dev, slave, port))
681 return -EPERM;
682 /* for a VF. drop unsolicited MADs */
683 if (!(mad->mad_hdr.method & IB_MGMT_METHOD_RESP)) {
684 mlx4_ib_warn(ibdev, "demux QP0. rejecting unsolicited mad for slave %d class 0x%x, method 0x%x\n",
685 slave, mad->mad_hdr.mgmt_class,
686 mad->mad_hdr.method);
687 return -EINVAL;
688 }
689 }
690 break;
Jack Morgenstein37bfc7c2012-08-03 08:40:44 +0000691 case IB_MGMT_CLASS_SUBN_ADM:
692 if (mlx4_ib_demux_sa_handler(ibdev, port, slave,
693 (struct ib_sa_mad *) mad))
694 return 0;
695 break;
Amir Vadai3cf69cc2012-08-03 08:40:47 +0000696 case IB_MGMT_CLASS_CM:
697 if (mlx4_ib_demux_cm_handler(ibdev, port, &slave, mad))
698 return 0;
699 break;
Jack Morgenstein37bfc7c2012-08-03 08:40:44 +0000700 case IB_MGMT_CLASS_DEVICE_MGMT:
701 if (mad->mad_hdr.method != IB_MGMT_METHOD_GET_RESP)
702 return 0;
703 break;
704 default:
705 /* Drop unsupported classes for slaves in tunnel mode */
706 if (slave != mlx4_master_func_num(dev->dev)) {
707 pr_debug("dropping unsupported ingress mad from class:%d "
708 "for slave:%d\n", mad->mad_hdr.mgmt_class, slave);
709 return 0;
710 }
711 }
712 /*make sure that no slave==255 was not handled yet.*/
713 if (slave >= dev->dev->caps.sqp_demux) {
714 mlx4_ib_warn(ibdev, "slave id: %d is bigger than allowed:%d\n",
715 slave, dev->dev->caps.sqp_demux);
716 return -ENOENT;
717 }
718
719 err = mlx4_ib_send_to_slave(dev, slave, port, wc->qp->qp_type, wc, grh, mad);
720 if (err)
721 pr_debug("failed sending to slave %d via tunnel qp (%d)\n",
722 slave, err);
723 return 0;
724}
725
Or Gerlitzc3779132011-06-15 14:51:27 +0000726static int ib_process_mad(struct ib_device *ibdev, int mad_flags, u8 port_num,
Ira Weinya97e2d82015-05-31 17:15:30 -0400727 const struct ib_wc *in_wc, const struct ib_grh *in_grh,
728 const struct ib_mad *in_mad, struct ib_mad *out_mad)
Roland Dreier225c7b12007-05-08 18:00:38 -0700729{
Moni Shouaf0f6f342009-01-28 14:54:35 -0800730 u16 slid, prev_lid = 0;
Roland Dreier225c7b12007-05-08 18:00:38 -0700731 int err;
Moni Shouaf0f6f342009-01-28 14:54:35 -0800732 struct ib_port_attr pattr;
Roland Dreier225c7b12007-05-08 18:00:38 -0700733
Jack Morgensteinb1d8eb52012-06-19 11:21:35 +0300734 if (in_wc && in_wc->qp->qp_num) {
735 pr_debug("received MAD: slid:%d sqpn:%d "
736 "dlid_bits:%d dqpn:%d wc_flags:0x%x, cls %x, mtd %x, atr %x\n",
737 in_wc->slid, in_wc->src_qp,
738 in_wc->dlid_path_bits,
739 in_wc->qp->qp_num,
740 in_wc->wc_flags,
741 in_mad->mad_hdr.mgmt_class, in_mad->mad_hdr.method,
742 be16_to_cpu(in_mad->mad_hdr.attr_id));
743 if (in_wc->wc_flags & IB_WC_GRH) {
744 pr_debug("sgid_hi:0x%016llx sgid_lo:0x%016llx\n",
745 be64_to_cpu(in_grh->sgid.global.subnet_prefix),
746 be64_to_cpu(in_grh->sgid.global.interface_id));
747 pr_debug("dgid_hi:0x%016llx dgid_lo:0x%016llx\n",
748 be64_to_cpu(in_grh->dgid.global.subnet_prefix),
749 be64_to_cpu(in_grh->dgid.global.interface_id));
750 }
751 }
752
Roland Dreier225c7b12007-05-08 18:00:38 -0700753 slid = in_wc ? in_wc->slid : be16_to_cpu(IB_LID_PERMISSIVE);
754
755 if (in_mad->mad_hdr.method == IB_MGMT_METHOD_TRAP && slid == 0) {
756 forward_trap(to_mdev(ibdev), port_num, in_mad);
757 return IB_MAD_RESULT_SUCCESS | IB_MAD_RESULT_CONSUMED;
758 }
759
760 if (in_mad->mad_hdr.mgmt_class == IB_MGMT_CLASS_SUBN_LID_ROUTED ||
761 in_mad->mad_hdr.mgmt_class == IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE) {
762 if (in_mad->mad_hdr.method != IB_MGMT_METHOD_GET &&
763 in_mad->mad_hdr.method != IB_MGMT_METHOD_SET &&
764 in_mad->mad_hdr.method != IB_MGMT_METHOD_TRAP_REPRESS)
765 return IB_MAD_RESULT_SUCCESS;
766
767 /*
Jack Morgensteina6f7fea2012-01-26 16:41:33 +0200768 * Don't process SMInfo queries -- the SMA can't handle them.
Roland Dreier225c7b12007-05-08 18:00:38 -0700769 */
Jack Morgensteina6f7fea2012-01-26 16:41:33 +0200770 if (in_mad->mad_hdr.attr_id == IB_SMP_ATTR_SM_INFO)
Roland Dreier225c7b12007-05-08 18:00:38 -0700771 return IB_MAD_RESULT_SUCCESS;
772 } else if (in_mad->mad_hdr.mgmt_class == IB_MGMT_CLASS_PERF_MGMT ||
773 in_mad->mad_hdr.mgmt_class == MLX4_IB_VENDOR_CLASS1 ||
Eli Cohen6578cf32008-07-14 23:48:45 -0700774 in_mad->mad_hdr.mgmt_class == MLX4_IB_VENDOR_CLASS2 ||
775 in_mad->mad_hdr.mgmt_class == IB_MGMT_CLASS_CONG_MGMT) {
Roland Dreier225c7b12007-05-08 18:00:38 -0700776 if (in_mad->mad_hdr.method != IB_MGMT_METHOD_GET &&
777 in_mad->mad_hdr.method != IB_MGMT_METHOD_SET)
778 return IB_MAD_RESULT_SUCCESS;
779 } else
780 return IB_MAD_RESULT_SUCCESS;
781
Moni Shouaf0f6f342009-01-28 14:54:35 -0800782 if ((in_mad->mad_hdr.mgmt_class == IB_MGMT_CLASS_SUBN_LID_ROUTED ||
783 in_mad->mad_hdr.mgmt_class == IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE) &&
784 in_mad->mad_hdr.method == IB_MGMT_METHOD_SET &&
785 in_mad->mad_hdr.attr_id == IB_SMP_ATTR_PORT_INFO &&
786 !ib_query_port(ibdev, port_num, &pattr))
787 prev_lid = pattr.lid;
788
Roland Dreier225c7b12007-05-08 18:00:38 -0700789 err = mlx4_MAD_IFC(to_mdev(ibdev),
Jack Morgenstein0a9a0182012-08-03 08:40:45 +0000790 (mad_flags & IB_MAD_IGNORE_MKEY ? MLX4_MAD_IFC_IGNORE_MKEY : 0) |
791 (mad_flags & IB_MAD_IGNORE_BKEY ? MLX4_MAD_IFC_IGNORE_BKEY : 0) |
792 MLX4_MAD_IFC_NET_VIEW,
Roland Dreier225c7b12007-05-08 18:00:38 -0700793 port_num, in_wc, in_grh, in_mad, out_mad);
794 if (err)
795 return IB_MAD_RESULT_FAILURE;
796
797 if (!out_mad->mad_hdr.status) {
Jack Morgenstein00f5ce92012-06-19 11:21:40 +0300798 if (!(to_mdev(ibdev)->dev->caps.flags & MLX4_DEV_CAP_FLAG_PORT_MNG_CHG_EV))
799 smp_snoop(ibdev, port_num, in_mad, prev_lid);
Jack Morgenstein992e8e6e2012-08-03 08:40:54 +0000800 /* slaves get node desc from FW */
801 if (!mlx4_is_slave(to_mdev(ibdev)->dev))
802 node_desc_override(ibdev, out_mad);
Roland Dreier225c7b12007-05-08 18:00:38 -0700803 }
804
805 /* set return bit in status of directed route responses */
806 if (in_mad->mad_hdr.mgmt_class == IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE)
807 out_mad->mad_hdr.status |= cpu_to_be16(1 << 15);
808
809 if (in_mad->mad_hdr.method == IB_MGMT_METHOD_TRAP_REPRESS)
810 /* no response for trap repress */
811 return IB_MAD_RESULT_SUCCESS | IB_MAD_RESULT_CONSUMED;
812
813 return IB_MAD_RESULT_SUCCESS | IB_MAD_RESULT_REPLY;
814}
815
Or Gerlitzc3779132011-06-15 14:51:27 +0000816static void edit_counter(struct mlx4_counter *cnt,
817 struct ib_pma_portcounters *pma_cnt)
818{
Majd Dibbiny61a38552015-03-18 16:51:37 +0200819 ASSIGN_32BIT_COUNTER(pma_cnt->port_xmit_data,
820 (be64_to_cpu(cnt->tx_bytes) >> 2));
821 ASSIGN_32BIT_COUNTER(pma_cnt->port_rcv_data,
822 (be64_to_cpu(cnt->rx_bytes) >> 2));
823 ASSIGN_32BIT_COUNTER(pma_cnt->port_xmit_packets,
824 be64_to_cpu(cnt->tx_frames));
825 ASSIGN_32BIT_COUNTER(pma_cnt->port_rcv_packets,
826 be64_to_cpu(cnt->rx_frames));
Or Gerlitzc3779132011-06-15 14:51:27 +0000827}
828
829static int iboe_process_mad(struct ib_device *ibdev, int mad_flags, u8 port_num,
Ira Weinya97e2d82015-05-31 17:15:30 -0400830 const struct ib_wc *in_wc, const struct ib_grh *in_grh,
831 const struct ib_mad *in_mad, struct ib_mad *out_mad)
Or Gerlitzc3779132011-06-15 14:51:27 +0000832{
833 struct mlx4_cmd_mailbox *mailbox;
834 struct mlx4_ib_dev *dev = to_mdev(ibdev);
835 int err;
836 u32 inmod = dev->counters[port_num - 1] & 0xffff;
837 u8 mode;
838
839 if (in_mad->mad_hdr.mgmt_class != IB_MGMT_CLASS_PERF_MGMT)
840 return -EINVAL;
841
842 mailbox = mlx4_alloc_cmd_mailbox(dev->dev);
843 if (IS_ERR(mailbox))
844 return IB_MAD_RESULT_FAILURE;
845
846 err = mlx4_cmd_box(dev->dev, 0, mailbox->dma, inmod, 0,
Jack Morgensteinf9baff52011-12-13 04:10:51 +0000847 MLX4_CMD_QUERY_IF_STAT, MLX4_CMD_TIME_CLASS_C,
848 MLX4_CMD_WRAPPED);
Or Gerlitzc3779132011-06-15 14:51:27 +0000849 if (err)
850 err = IB_MAD_RESULT_FAILURE;
851 else {
852 memset(out_mad->data, 0, sizeof out_mad->data);
853 mode = ((struct mlx4_counter *)mailbox->buf)->counter_mode;
854 switch (mode & 0xf) {
855 case 0:
856 edit_counter(mailbox->buf,
857 (void *)(out_mad->data + 40));
858 err = IB_MAD_RESULT_SUCCESS | IB_MAD_RESULT_REPLY;
859 break;
860 default:
861 err = IB_MAD_RESULT_FAILURE;
862 }
863 }
864
865 mlx4_free_cmd_mailbox(dev->dev, mailbox);
866
867 return err;
868}
869
870int mlx4_ib_process_mad(struct ib_device *ibdev, int mad_flags, u8 port_num,
Ira Weinya97e2d82015-05-31 17:15:30 -0400871 const struct ib_wc *in_wc, const struct ib_grh *in_grh,
872 const struct ib_mad *in_mad, struct ib_mad *out_mad)
Or Gerlitzc3779132011-06-15 14:51:27 +0000873{
874 switch (rdma_port_get_link_layer(ibdev, port_num)) {
875 case IB_LINK_LAYER_INFINIBAND:
876 return ib_process_mad(ibdev, mad_flags, port_num, in_wc,
877 in_grh, in_mad, out_mad);
878 case IB_LINK_LAYER_ETHERNET:
879 return iboe_process_mad(ibdev, mad_flags, port_num, in_wc,
880 in_grh, in_mad, out_mad);
881 default:
882 return -EINVAL;
883 }
884}
885
Roland Dreier225c7b12007-05-08 18:00:38 -0700886static void send_handler(struct ib_mad_agent *agent,
887 struct ib_mad_send_wc *mad_send_wc)
888{
Jack Morgenstein992e8e6e2012-08-03 08:40:54 +0000889 if (mad_send_wc->send_buf->context[0])
890 ib_destroy_ah(mad_send_wc->send_buf->context[0]);
Roland Dreier225c7b12007-05-08 18:00:38 -0700891 ib_free_send_mad(mad_send_wc->send_buf);
892}
893
894int mlx4_ib_mad_init(struct mlx4_ib_dev *dev)
895{
896 struct ib_mad_agent *agent;
897 int p, q;
898 int ret;
Eli Cohenfa417f72010-10-24 21:08:52 -0700899 enum rdma_link_layer ll;
Roland Dreier225c7b12007-05-08 18:00:38 -0700900
Eli Cohenfa417f72010-10-24 21:08:52 -0700901 for (p = 0; p < dev->num_ports; ++p) {
902 ll = rdma_port_get_link_layer(&dev->ib_dev, p + 1);
Roland Dreier225c7b12007-05-08 18:00:38 -0700903 for (q = 0; q <= 1; ++q) {
Eli Cohenfa417f72010-10-24 21:08:52 -0700904 if (ll == IB_LINK_LAYER_INFINIBAND) {
905 agent = ib_register_mad_agent(&dev->ib_dev, p + 1,
906 q ? IB_QPT_GSI : IB_QPT_SMI,
907 NULL, 0, send_handler,
Ira Weiny0f29b462014-08-08 19:00:55 -0400908 NULL, NULL, 0);
Eli Cohenfa417f72010-10-24 21:08:52 -0700909 if (IS_ERR(agent)) {
910 ret = PTR_ERR(agent);
911 goto err;
912 }
913 dev->send_agent[p][q] = agent;
914 } else
915 dev->send_agent[p][q] = NULL;
Roland Dreier225c7b12007-05-08 18:00:38 -0700916 }
Eli Cohenfa417f72010-10-24 21:08:52 -0700917 }
Roland Dreier225c7b12007-05-08 18:00:38 -0700918
919 return 0;
920
921err:
Yevgeny Petrilin7ff93f82008-10-22 15:38:42 -0700922 for (p = 0; p < dev->num_ports; ++p)
Roland Dreier225c7b12007-05-08 18:00:38 -0700923 for (q = 0; q <= 1; ++q)
924 if (dev->send_agent[p][q])
925 ib_unregister_mad_agent(dev->send_agent[p][q]);
926
927 return ret;
928}
929
930void mlx4_ib_mad_cleanup(struct mlx4_ib_dev *dev)
931{
932 struct ib_mad_agent *agent;
933 int p, q;
934
Yevgeny Petrilin7ff93f82008-10-22 15:38:42 -0700935 for (p = 0; p < dev->num_ports; ++p) {
Roland Dreier225c7b12007-05-08 18:00:38 -0700936 for (q = 0; q <= 1; ++q) {
937 agent = dev->send_agent[p][q];
Eli Cohenfa417f72010-10-24 21:08:52 -0700938 if (agent) {
939 dev->send_agent[p][q] = NULL;
940 ib_unregister_mad_agent(agent);
941 }
Roland Dreier225c7b12007-05-08 18:00:38 -0700942 }
943
944 if (dev->sm_ah[p])
945 ib_destroy_ah(dev->sm_ah[p]);
946 }
947}
Jack Morgenstein00f5ce92012-06-19 11:21:40 +0300948
Jack Morgenstein2a4fae12012-08-03 08:40:50 +0000949static void handle_lid_change_event(struct mlx4_ib_dev *dev, u8 port_num)
950{
951 mlx4_ib_dispatch_event(dev, port_num, IB_EVENT_LID_CHANGE);
952
953 if (mlx4_is_master(dev->dev) && !dev->sriov.is_going_down)
954 mlx4_gen_slaves_port_mgt_ev(dev->dev, port_num,
955 MLX4_EQ_PORT_INFO_LID_CHANGE_MASK);
956}
957
Oren Duerb9c5d6a2012-08-03 08:40:46 +0000958static void handle_client_rereg_event(struct mlx4_ib_dev *dev, u8 port_num)
959{
Jack Morgensteina0c64a12012-08-03 08:40:49 +0000960 /* re-configure the alias-guid and mcg's */
Oren Duerb9c5d6a2012-08-03 08:40:46 +0000961 if (mlx4_is_master(dev->dev)) {
Jack Morgensteina0c64a12012-08-03 08:40:49 +0000962 mlx4_ib_invalidate_all_guid_record(dev, port_num);
963
Jack Morgenstein2a4fae12012-08-03 08:40:50 +0000964 if (!dev->sriov.is_going_down) {
Oren Duerb9c5d6a2012-08-03 08:40:46 +0000965 mlx4_ib_mcg_port_cleanup(&dev->sriov.demux[port_num - 1], 0);
Jack Morgenstein2a4fae12012-08-03 08:40:50 +0000966 mlx4_gen_slaves_port_mgt_ev(dev->dev, port_num,
967 MLX4_EQ_PORT_INFO_CLIENT_REREG_MASK);
968 }
Oren Duerb9c5d6a2012-08-03 08:40:46 +0000969 }
970 mlx4_ib_dispatch_event(dev, port_num, IB_EVENT_CLIENT_REREGISTER);
971}
972
Jack Morgenstein2a4fae12012-08-03 08:40:50 +0000973static void propagate_pkey_ev(struct mlx4_ib_dev *dev, int port_num,
974 struct mlx4_eqe *eqe)
975{
976 __propagate_pkey_ev(dev, port_num, GET_BLK_PTR_FROM_EQE(eqe),
977 GET_MASK_FROM_EQE(eqe));
978}
979
980static void handle_slaves_guid_change(struct mlx4_ib_dev *dev, u8 port_num,
981 u32 guid_tbl_blk_num, u32 change_bitmap)
982{
983 struct ib_smp *in_mad = NULL;
984 struct ib_smp *out_mad = NULL;
985 u16 i;
986
987 if (!mlx4_is_mfunc(dev->dev) || !mlx4_is_master(dev->dev))
988 return;
989
990 in_mad = kmalloc(sizeof *in_mad, GFP_KERNEL);
991 out_mad = kmalloc(sizeof *out_mad, GFP_KERNEL);
992 if (!in_mad || !out_mad) {
993 mlx4_ib_warn(&dev->ib_dev, "failed to allocate memory for guid info mads\n");
994 goto out;
995 }
996
997 guid_tbl_blk_num *= 4;
998
999 for (i = 0; i < 4; i++) {
1000 if (change_bitmap && (!((change_bitmap >> (8 * i)) & 0xff)))
1001 continue;
1002 memset(in_mad, 0, sizeof *in_mad);
1003 memset(out_mad, 0, sizeof *out_mad);
1004
1005 in_mad->base_version = 1;
1006 in_mad->mgmt_class = IB_MGMT_CLASS_SUBN_LID_ROUTED;
1007 in_mad->class_version = 1;
1008 in_mad->method = IB_MGMT_METHOD_GET;
1009 in_mad->attr_id = IB_SMP_ATTR_GUID_INFO;
1010 in_mad->attr_mod = cpu_to_be32(guid_tbl_blk_num + i);
1011
1012 if (mlx4_MAD_IFC(dev,
1013 MLX4_MAD_IFC_IGNORE_KEYS | MLX4_MAD_IFC_NET_VIEW,
1014 port_num, NULL, NULL, in_mad, out_mad)) {
1015 mlx4_ib_warn(&dev->ib_dev, "Failed in get GUID INFO MAD_IFC\n");
1016 goto out;
1017 }
1018
1019 mlx4_ib_update_cache_on_guid_change(dev, guid_tbl_blk_num + i,
1020 port_num,
1021 (u8 *)(&((struct ib_smp *)out_mad)->data));
1022 mlx4_ib_notify_slaves_on_guid_change(dev, guid_tbl_blk_num + i,
1023 port_num,
1024 (u8 *)(&((struct ib_smp *)out_mad)->data));
1025 }
1026
1027out:
1028 kfree(in_mad);
1029 kfree(out_mad);
1030 return;
1031}
1032
Jack Morgenstein00f5ce92012-06-19 11:21:40 +03001033void handle_port_mgmt_change_event(struct work_struct *work)
1034{
1035 struct ib_event_work *ew = container_of(work, struct ib_event_work, work);
1036 struct mlx4_ib_dev *dev = ew->ib_dev;
1037 struct mlx4_eqe *eqe = &(ew->ib_eqe);
1038 u8 port = eqe->event.port_mgmt_change.port;
1039 u32 changed_attr;
Jack Morgenstein2a4fae12012-08-03 08:40:50 +00001040 u32 tbl_block;
1041 u32 change_bitmap;
Jack Morgenstein00f5ce92012-06-19 11:21:40 +03001042
1043 switch (eqe->subtype) {
1044 case MLX4_DEV_PMC_SUBTYPE_PORT_INFO:
1045 changed_attr = be32_to_cpu(eqe->event.port_mgmt_change.params.port_info.changed_attr);
1046
1047 /* Update the SM ah - This should be done before handling
1048 the other changed attributes so that MADs can be sent to the SM */
1049 if (changed_attr & MSTR_SM_CHANGE_MASK) {
1050 u16 lid = be16_to_cpu(eqe->event.port_mgmt_change.params.port_info.mstr_sm_lid);
1051 u8 sl = eqe->event.port_mgmt_change.params.port_info.mstr_sm_sl & 0xf;
1052 update_sm_ah(dev, port, lid, sl);
1053 }
1054
1055 /* Check if it is a lid change event */
1056 if (changed_attr & MLX4_EQ_PORT_INFO_LID_CHANGE_MASK)
Jack Morgenstein2a4fae12012-08-03 08:40:50 +00001057 handle_lid_change_event(dev, port);
Jack Morgenstein00f5ce92012-06-19 11:21:40 +03001058
1059 /* Generate GUID changed event */
Jack Morgenstein2a4fae12012-08-03 08:40:50 +00001060 if (changed_attr & MLX4_EQ_PORT_INFO_GID_PFX_CHANGE_MASK) {
Jack Morgenstein00f5ce92012-06-19 11:21:40 +03001061 mlx4_ib_dispatch_event(dev, port, IB_EVENT_GID_CHANGE);
Jack Morgenstein2a4fae12012-08-03 08:40:50 +00001062 /*if master, notify all slaves*/
1063 if (mlx4_is_master(dev->dev))
1064 mlx4_gen_slaves_port_mgt_ev(dev->dev, port,
1065 MLX4_EQ_PORT_INFO_GID_PFX_CHANGE_MASK);
1066 }
Jack Morgenstein00f5ce92012-06-19 11:21:40 +03001067
1068 if (changed_attr & MLX4_EQ_PORT_INFO_CLIENT_REREG_MASK)
Oren Duerb9c5d6a2012-08-03 08:40:46 +00001069 handle_client_rereg_event(dev, port);
Jack Morgenstein00f5ce92012-06-19 11:21:40 +03001070 break;
1071
1072 case MLX4_DEV_PMC_SUBTYPE_PKEY_TABLE:
1073 mlx4_ib_dispatch_event(dev, port, IB_EVENT_PKEY_CHANGE);
Jack Morgenstein2a4fae12012-08-03 08:40:50 +00001074 if (mlx4_is_master(dev->dev) && !dev->sriov.is_going_down)
1075 propagate_pkey_ev(dev, port, eqe);
Jack Morgenstein00f5ce92012-06-19 11:21:40 +03001076 break;
1077 case MLX4_DEV_PMC_SUBTYPE_GUID_INFO:
Jack Morgenstein66349612012-06-19 11:21:44 +03001078 /* paravirtualized master's guid is guid 0 -- does not change */
1079 if (!mlx4_is_master(dev->dev))
1080 mlx4_ib_dispatch_event(dev, port, IB_EVENT_GID_CHANGE);
Jack Morgenstein2a4fae12012-08-03 08:40:50 +00001081 /*if master, notify relevant slaves*/
1082 else if (!dev->sriov.is_going_down) {
1083 tbl_block = GET_BLK_PTR_FROM_EQE(eqe);
1084 change_bitmap = GET_MASK_FROM_EQE(eqe);
1085 handle_slaves_guid_change(dev, port, tbl_block, change_bitmap);
1086 }
Jack Morgenstein00f5ce92012-06-19 11:21:40 +03001087 break;
1088 default:
1089 pr_warn("Unsupported subtype 0x%x for "
1090 "Port Management Change event\n", eqe->subtype);
1091 }
1092
1093 kfree(ew);
1094}
1095
1096void mlx4_ib_dispatch_event(struct mlx4_ib_dev *dev, u8 port_num,
1097 enum ib_event_type type)
1098{
1099 struct ib_event event;
1100
1101 event.device = &dev->ib_dev;
1102 event.element.port_num = port_num;
1103 event.event = type;
1104
1105 ib_dispatch_event(&event);
1106}
Jack Morgensteinfc065732012-08-03 08:40:42 +00001107
1108static void mlx4_ib_tunnel_comp_handler(struct ib_cq *cq, void *arg)
1109{
1110 unsigned long flags;
1111 struct mlx4_ib_demux_pv_ctx *ctx = cq->cq_context;
1112 struct mlx4_ib_dev *dev = to_mdev(ctx->ib_dev);
1113 spin_lock_irqsave(&dev->sriov.going_down_lock, flags);
1114 if (!dev->sriov.is_going_down && ctx->state == DEMUX_PV_STATE_ACTIVE)
1115 queue_work(ctx->wq, &ctx->work);
1116 spin_unlock_irqrestore(&dev->sriov.going_down_lock, flags);
1117}
1118
1119static int mlx4_ib_post_pv_qp_buf(struct mlx4_ib_demux_pv_ctx *ctx,
1120 struct mlx4_ib_demux_pv_qp *tun_qp,
1121 int index)
1122{
1123 struct ib_sge sg_list;
1124 struct ib_recv_wr recv_wr, *bad_recv_wr;
1125 int size;
1126
1127 size = (tun_qp->qp->qp_type == IB_QPT_UD) ?
1128 sizeof (struct mlx4_tunnel_mad) : sizeof (struct mlx4_mad_rcv_buf);
1129
1130 sg_list.addr = tun_qp->ring[index].map;
1131 sg_list.length = size;
1132 sg_list.lkey = ctx->mr->lkey;
1133
1134 recv_wr.next = NULL;
1135 recv_wr.sg_list = &sg_list;
1136 recv_wr.num_sge = 1;
1137 recv_wr.wr_id = (u64) index | MLX4_TUN_WRID_RECV |
1138 MLX4_TUN_SET_WRID_QPN(tun_qp->proxy_qpt);
1139 ib_dma_sync_single_for_device(ctx->ib_dev, tun_qp->ring[index].map,
1140 size, DMA_FROM_DEVICE);
1141 return ib_post_recv(tun_qp->qp, &recv_wr, &bad_recv_wr);
1142}
1143
Jack Morgenstein37bfc7c2012-08-03 08:40:44 +00001144static int mlx4_ib_multiplex_sa_handler(struct ib_device *ibdev, int port,
1145 int slave, struct ib_sa_mad *sa_mad)
1146{
Oren Duerb9c5d6a2012-08-03 08:40:46 +00001147 int ret = 0;
1148
1149 /* dispatch to different sa handlers */
1150 switch (be16_to_cpu(sa_mad->mad_hdr.attr_id)) {
1151 case IB_SA_ATTR_MC_MEMBER_REC:
1152 ret = mlx4_ib_mcg_multiplex_handler(ibdev, port, slave, sa_mad);
1153 break;
1154 default:
1155 break;
1156 }
1157 return ret;
Jack Morgenstein37bfc7c2012-08-03 08:40:44 +00001158}
1159
1160static int is_proxy_qp0(struct mlx4_ib_dev *dev, int qpn, int slave)
1161{
Jack Morgenstein47605df2012-08-03 08:40:57 +00001162 int proxy_start = dev->dev->phys_caps.base_proxy_sqpn + 8 * slave;
Jack Morgenstein37bfc7c2012-08-03 08:40:44 +00001163
Jack Morgenstein47605df2012-08-03 08:40:57 +00001164 return (qpn >= proxy_start && qpn <= proxy_start + 1);
Jack Morgenstein37bfc7c2012-08-03 08:40:44 +00001165}
1166
1167
1168int mlx4_ib_send_to_wire(struct mlx4_ib_dev *dev, int slave, u8 port,
Jack Morgenstein5ea8bbf2014-03-12 12:00:41 +02001169 enum ib_qp_type dest_qpt, u16 pkey_index,
1170 u32 remote_qpn, u32 qkey, struct ib_ah_attr *attr,
1171 u8 *s_mac, struct ib_mad *mad)
Jack Morgenstein37bfc7c2012-08-03 08:40:44 +00001172{
1173 struct ib_sge list;
1174 struct ib_send_wr wr, *bad_wr;
1175 struct mlx4_ib_demux_pv_ctx *sqp_ctx;
1176 struct mlx4_ib_demux_pv_qp *sqp;
1177 struct mlx4_mad_snd_buf *sqp_mad;
1178 struct ib_ah *ah;
1179 struct ib_qp *send_qp = NULL;
1180 unsigned wire_tx_ix = 0;
1181 int ret = 0;
1182 u16 wire_pkey_ix;
1183 int src_qpnum;
1184 u8 sgid_index;
1185
1186
1187 sqp_ctx = dev->sriov.sqps[port-1];
1188
1189 /* check if proxy qp created */
1190 if (!sqp_ctx || sqp_ctx->state != DEMUX_PV_STATE_ACTIVE)
1191 return -EAGAIN;
1192
Jack Morgenstein37bfc7c2012-08-03 08:40:44 +00001193 if (dest_qpt == IB_QPT_SMI) {
1194 src_qpnum = 0;
1195 sqp = &sqp_ctx->qp[0];
1196 wire_pkey_ix = dev->pkeys.virt2phys_pkey[slave][port - 1][0];
1197 } else {
1198 src_qpnum = 1;
1199 sqp = &sqp_ctx->qp[1];
1200 wire_pkey_ix = dev->pkeys.virt2phys_pkey[slave][port - 1][pkey_index];
1201 }
1202
1203 send_qp = sqp->qp;
1204
1205 /* create ah */
1206 sgid_index = attr->grh.sgid_index;
1207 attr->grh.sgid_index = 0;
1208 ah = ib_create_ah(sqp_ctx->pd, attr);
1209 if (IS_ERR(ah))
1210 return -ENOMEM;
1211 attr->grh.sgid_index = sgid_index;
1212 to_mah(ah)->av.ib.gid_index = sgid_index;
1213 /* get rid of force-loopback bit */
1214 to_mah(ah)->av.ib.port_pd &= cpu_to_be32(0x7FFFFFFF);
1215 spin_lock(&sqp->tx_lock);
1216 if (sqp->tx_ix_head - sqp->tx_ix_tail >=
1217 (MLX4_NUM_TUNNEL_BUFS - 1))
1218 ret = -EAGAIN;
1219 else
1220 wire_tx_ix = (++sqp->tx_ix_head) & (MLX4_NUM_TUNNEL_BUFS - 1);
1221 spin_unlock(&sqp->tx_lock);
1222 if (ret)
1223 goto out;
1224
1225 sqp_mad = (struct mlx4_mad_snd_buf *) (sqp->tx_ring[wire_tx_ix].buf.addr);
1226 if (sqp->tx_ring[wire_tx_ix].ah)
1227 ib_destroy_ah(sqp->tx_ring[wire_tx_ix].ah);
1228 sqp->tx_ring[wire_tx_ix].ah = ah;
1229 ib_dma_sync_single_for_cpu(&dev->ib_dev,
1230 sqp->tx_ring[wire_tx_ix].buf.map,
1231 sizeof (struct mlx4_mad_snd_buf),
1232 DMA_TO_DEVICE);
1233
1234 memcpy(&sqp_mad->payload, mad, sizeof *mad);
1235
1236 ib_dma_sync_single_for_device(&dev->ib_dev,
1237 sqp->tx_ring[wire_tx_ix].buf.map,
1238 sizeof (struct mlx4_mad_snd_buf),
1239 DMA_TO_DEVICE);
1240
1241 list.addr = sqp->tx_ring[wire_tx_ix].buf.map;
1242 list.length = sizeof (struct mlx4_mad_snd_buf);
1243 list.lkey = sqp_ctx->mr->lkey;
1244
1245 wr.wr.ud.ah = ah;
1246 wr.wr.ud.port_num = port;
1247 wr.wr.ud.pkey_index = wire_pkey_ix;
1248 wr.wr.ud.remote_qkey = qkey;
1249 wr.wr.ud.remote_qpn = remote_qpn;
1250 wr.next = NULL;
1251 wr.wr_id = ((u64) wire_tx_ix) | MLX4_TUN_SET_WRID_QPN(src_qpnum);
1252 wr.sg_list = &list;
1253 wr.num_sge = 1;
1254 wr.opcode = IB_WR_SEND;
1255 wr.send_flags = IB_SEND_SIGNALED;
Jack Morgenstein5ea8bbf2014-03-12 12:00:41 +02001256 if (s_mac)
1257 memcpy(to_mah(ah)->av.eth.s_mac, s_mac, 6);
1258
Jack Morgenstein37bfc7c2012-08-03 08:40:44 +00001259
1260 ret = ib_post_send(send_qp, &wr, &bad_wr);
1261out:
1262 if (ret)
1263 ib_destroy_ah(ah);
1264 return ret;
1265}
1266
Jack Morgensteinb6ffaef2014-03-12 12:00:39 +02001267static int get_slave_base_gid_ix(struct mlx4_ib_dev *dev, int slave, int port)
1268{
Jack Morgensteinb6ffaef2014-03-12 12:00:39 +02001269 if (rdma_port_get_link_layer(&dev->ib_dev, port) == IB_LINK_LAYER_INFINIBAND)
1270 return slave;
Matan Barak449fc482014-03-19 18:11:52 +02001271 return mlx4_get_base_gid_ix(dev->dev, slave, port);
Jack Morgensteinb6ffaef2014-03-12 12:00:39 +02001272}
1273
1274static void fill_in_real_sgid_index(struct mlx4_ib_dev *dev, int slave, int port,
1275 struct ib_ah_attr *ah_attr)
1276{
1277 if (rdma_port_get_link_layer(&dev->ib_dev, port) == IB_LINK_LAYER_INFINIBAND)
1278 ah_attr->grh.sgid_index = slave;
1279 else
1280 ah_attr->grh.sgid_index += get_slave_base_gid_ix(dev, slave, port);
1281}
1282
Jack Morgenstein37bfc7c2012-08-03 08:40:44 +00001283static void mlx4_ib_multiplex_mad(struct mlx4_ib_demux_pv_ctx *ctx, struct ib_wc *wc)
1284{
1285 struct mlx4_ib_dev *dev = to_mdev(ctx->ib_dev);
1286 struct mlx4_ib_demux_pv_qp *tun_qp = &ctx->qp[MLX4_TUN_WRID_QPN(wc->wr_id)];
1287 int wr_ix = wc->wr_id & (MLX4_NUM_TUNNEL_BUFS - 1);
1288 struct mlx4_tunnel_mad *tunnel = tun_qp->ring[wr_ix].addr;
1289 struct mlx4_ib_ah ah;
1290 struct ib_ah_attr ah_attr;
1291 u8 *slave_id;
1292 int slave;
Matan Barak449fc482014-03-19 18:11:52 +02001293 int port;
Jack Morgenstein37bfc7c2012-08-03 08:40:44 +00001294
1295 /* Get slave that sent this packet */
Jack Morgenstein47605df2012-08-03 08:40:57 +00001296 if (wc->src_qp < dev->dev->phys_caps.base_proxy_sqpn ||
1297 wc->src_qp >= dev->dev->phys_caps.base_proxy_sqpn + 8 * MLX4_MFUNC_MAX ||
Jack Morgenstein37bfc7c2012-08-03 08:40:44 +00001298 (wc->src_qp & 0x1) != ctx->port - 1 ||
1299 wc->src_qp & 0x4) {
1300 mlx4_ib_warn(ctx->ib_dev, "can't multiplex bad sqp:%d\n", wc->src_qp);
1301 return;
1302 }
Jack Morgenstein47605df2012-08-03 08:40:57 +00001303 slave = ((wc->src_qp & ~0x7) - dev->dev->phys_caps.base_proxy_sqpn) / 8;
Jack Morgenstein37bfc7c2012-08-03 08:40:44 +00001304 if (slave != ctx->slave) {
1305 mlx4_ib_warn(ctx->ib_dev, "can't multiplex bad sqp:%d: "
1306 "belongs to another slave\n", wc->src_qp);
1307 return;
1308 }
Jack Morgenstein37bfc7c2012-08-03 08:40:44 +00001309
1310 /* Map transaction ID */
1311 ib_dma_sync_single_for_cpu(ctx->ib_dev, tun_qp->ring[wr_ix].map,
1312 sizeof (struct mlx4_tunnel_mad),
1313 DMA_FROM_DEVICE);
1314 switch (tunnel->mad.mad_hdr.method) {
1315 case IB_MGMT_METHOD_SET:
1316 case IB_MGMT_METHOD_GET:
1317 case IB_MGMT_METHOD_REPORT:
1318 case IB_SA_METHOD_GET_TABLE:
1319 case IB_SA_METHOD_DELETE:
1320 case IB_SA_METHOD_GET_MULTI:
1321 case IB_SA_METHOD_GET_TRACE_TBL:
1322 slave_id = (u8 *) &tunnel->mad.mad_hdr.tid;
1323 if (*slave_id) {
1324 mlx4_ib_warn(ctx->ib_dev, "egress mad has non-null tid msb:%d "
1325 "class:%d slave:%d\n", *slave_id,
1326 tunnel->mad.mad_hdr.mgmt_class, slave);
1327 return;
1328 } else
1329 *slave_id = slave;
1330 default:
1331 /* nothing */;
1332 }
1333
1334 /* Class-specific handling */
1335 switch (tunnel->mad.mad_hdr.mgmt_class) {
Jack Morgenstein97982f52014-05-29 16:31:02 +03001336 case IB_MGMT_CLASS_SUBN_LID_ROUTED:
1337 case IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE:
1338 if (slave != mlx4_master_func_num(dev->dev) &&
1339 !mlx4_vf_smi_enabled(dev->dev, slave, ctx->port))
1340 return;
1341 break;
Jack Morgenstein37bfc7c2012-08-03 08:40:44 +00001342 case IB_MGMT_CLASS_SUBN_ADM:
1343 if (mlx4_ib_multiplex_sa_handler(ctx->ib_dev, ctx->port, slave,
1344 (struct ib_sa_mad *) &tunnel->mad))
1345 return;
1346 break;
Amir Vadai3cf69cc2012-08-03 08:40:47 +00001347 case IB_MGMT_CLASS_CM:
1348 if (mlx4_ib_multiplex_cm_handler(ctx->ib_dev, ctx->port, slave,
1349 (struct ib_mad *) &tunnel->mad))
1350 return;
1351 break;
Jack Morgenstein37bfc7c2012-08-03 08:40:44 +00001352 case IB_MGMT_CLASS_DEVICE_MGMT:
1353 if (tunnel->mad.mad_hdr.method != IB_MGMT_METHOD_GET &&
1354 tunnel->mad.mad_hdr.method != IB_MGMT_METHOD_SET)
1355 return;
1356 break;
1357 default:
1358 /* Drop unsupported classes for slaves in tunnel mode */
1359 if (slave != mlx4_master_func_num(dev->dev)) {
1360 mlx4_ib_warn(ctx->ib_dev, "dropping unsupported egress mad from class:%d "
1361 "for slave:%d\n", tunnel->mad.mad_hdr.mgmt_class, slave);
1362 return;
1363 }
1364 }
1365
1366 /* We are using standard ib_core services to send the mad, so generate a
1367 * stadard address handle by decoding the tunnelled mlx4_ah fields */
1368 memcpy(&ah.av, &tunnel->hdr.av, sizeof (struct mlx4_av));
1369 ah.ibah.device = ctx->ib_dev;
1370 mlx4_ib_query_ah(&ah.ibah, &ah_attr);
Jack Morgenstein6ee51a42014-03-12 12:00:37 +02001371 if (ah_attr.ah_flags & IB_AH_GRH)
Jack Morgensteinb6ffaef2014-03-12 12:00:39 +02001372 fill_in_real_sgid_index(dev, slave, ctx->port, &ah_attr);
Jack Morgenstein37bfc7c2012-08-03 08:40:44 +00001373
Matan Barak449fc482014-03-19 18:11:52 +02001374 port = mlx4_slave_convert_port(dev->dev, slave, ah_attr.port_num);
1375 if (port < 0)
1376 return;
1377 ah_attr.port_num = port;
Jack Morgenstein5ea8bbf2014-03-12 12:00:41 +02001378 memcpy(ah_attr.dmac, tunnel->hdr.mac, 6);
1379 ah_attr.vlan_id = be16_to_cpu(tunnel->hdr.vlan);
1380 /* if slave have default vlan use it */
1381 mlx4_get_slave_default_vlan(dev->dev, ctx->port, slave,
1382 &ah_attr.vlan_id, &ah_attr.sl);
1383
Jack Morgenstein37bfc7c2012-08-03 08:40:44 +00001384 mlx4_ib_send_to_wire(dev, slave, ctx->port,
1385 is_proxy_qp0(dev, wc->src_qp, slave) ?
1386 IB_QPT_SMI : IB_QPT_GSI,
1387 be16_to_cpu(tunnel->hdr.pkey_index),
1388 be32_to_cpu(tunnel->hdr.remote_qpn),
1389 be32_to_cpu(tunnel->hdr.qkey),
Jack Morgenstein5ea8bbf2014-03-12 12:00:41 +02001390 &ah_attr, wc->smac, &tunnel->mad);
Jack Morgenstein37bfc7c2012-08-03 08:40:44 +00001391}
1392
Jack Morgensteinfc065732012-08-03 08:40:42 +00001393static int mlx4_ib_alloc_pv_bufs(struct mlx4_ib_demux_pv_ctx *ctx,
1394 enum ib_qp_type qp_type, int is_tun)
1395{
1396 int i;
1397 struct mlx4_ib_demux_pv_qp *tun_qp;
1398 int rx_buf_size, tx_buf_size;
1399
1400 if (qp_type > IB_QPT_GSI)
1401 return -EINVAL;
1402
1403 tun_qp = &ctx->qp[qp_type];
1404
1405 tun_qp->ring = kzalloc(sizeof (struct mlx4_ib_buf) * MLX4_NUM_TUNNEL_BUFS,
1406 GFP_KERNEL);
1407 if (!tun_qp->ring)
1408 return -ENOMEM;
1409
1410 tun_qp->tx_ring = kcalloc(MLX4_NUM_TUNNEL_BUFS,
1411 sizeof (struct mlx4_ib_tun_tx_buf),
1412 GFP_KERNEL);
1413 if (!tun_qp->tx_ring) {
1414 kfree(tun_qp->ring);
1415 tun_qp->ring = NULL;
1416 return -ENOMEM;
1417 }
1418
1419 if (is_tun) {
1420 rx_buf_size = sizeof (struct mlx4_tunnel_mad);
1421 tx_buf_size = sizeof (struct mlx4_rcv_tunnel_mad);
1422 } else {
1423 rx_buf_size = sizeof (struct mlx4_mad_rcv_buf);
1424 tx_buf_size = sizeof (struct mlx4_mad_snd_buf);
1425 }
1426
1427 for (i = 0; i < MLX4_NUM_TUNNEL_BUFS; i++) {
1428 tun_qp->ring[i].addr = kmalloc(rx_buf_size, GFP_KERNEL);
1429 if (!tun_qp->ring[i].addr)
1430 goto err;
1431 tun_qp->ring[i].map = ib_dma_map_single(ctx->ib_dev,
1432 tun_qp->ring[i].addr,
1433 rx_buf_size,
1434 DMA_FROM_DEVICE);
Sebastian Ottcc47d3692015-03-16 18:49:59 +01001435 if (ib_dma_mapping_error(ctx->ib_dev, tun_qp->ring[i].map)) {
1436 kfree(tun_qp->ring[i].addr);
1437 goto err;
1438 }
Jack Morgensteinfc065732012-08-03 08:40:42 +00001439 }
1440
1441 for (i = 0; i < MLX4_NUM_TUNNEL_BUFS; i++) {
1442 tun_qp->tx_ring[i].buf.addr =
1443 kmalloc(tx_buf_size, GFP_KERNEL);
1444 if (!tun_qp->tx_ring[i].buf.addr)
1445 goto tx_err;
1446 tun_qp->tx_ring[i].buf.map =
1447 ib_dma_map_single(ctx->ib_dev,
1448 tun_qp->tx_ring[i].buf.addr,
1449 tx_buf_size,
1450 DMA_TO_DEVICE);
Sebastian Ottcc47d3692015-03-16 18:49:59 +01001451 if (ib_dma_mapping_error(ctx->ib_dev,
1452 tun_qp->tx_ring[i].buf.map)) {
1453 kfree(tun_qp->tx_ring[i].buf.addr);
1454 goto tx_err;
1455 }
Jack Morgensteinfc065732012-08-03 08:40:42 +00001456 tun_qp->tx_ring[i].ah = NULL;
1457 }
1458 spin_lock_init(&tun_qp->tx_lock);
1459 tun_qp->tx_ix_head = 0;
1460 tun_qp->tx_ix_tail = 0;
1461 tun_qp->proxy_qpt = qp_type;
1462
1463 return 0;
1464
1465tx_err:
1466 while (i > 0) {
1467 --i;
1468 ib_dma_unmap_single(ctx->ib_dev, tun_qp->tx_ring[i].buf.map,
1469 tx_buf_size, DMA_TO_DEVICE);
1470 kfree(tun_qp->tx_ring[i].buf.addr);
1471 }
1472 kfree(tun_qp->tx_ring);
1473 tun_qp->tx_ring = NULL;
1474 i = MLX4_NUM_TUNNEL_BUFS;
1475err:
1476 while (i > 0) {
1477 --i;
1478 ib_dma_unmap_single(ctx->ib_dev, tun_qp->ring[i].map,
1479 rx_buf_size, DMA_FROM_DEVICE);
1480 kfree(tun_qp->ring[i].addr);
1481 }
1482 kfree(tun_qp->ring);
1483 tun_qp->ring = NULL;
1484 return -ENOMEM;
1485}
1486
1487static void mlx4_ib_free_pv_qp_bufs(struct mlx4_ib_demux_pv_ctx *ctx,
1488 enum ib_qp_type qp_type, int is_tun)
1489{
1490 int i;
1491 struct mlx4_ib_demux_pv_qp *tun_qp;
1492 int rx_buf_size, tx_buf_size;
1493
1494 if (qp_type > IB_QPT_GSI)
1495 return;
1496
1497 tun_qp = &ctx->qp[qp_type];
1498 if (is_tun) {
1499 rx_buf_size = sizeof (struct mlx4_tunnel_mad);
1500 tx_buf_size = sizeof (struct mlx4_rcv_tunnel_mad);
1501 } else {
1502 rx_buf_size = sizeof (struct mlx4_mad_rcv_buf);
1503 tx_buf_size = sizeof (struct mlx4_mad_snd_buf);
1504 }
1505
1506
1507 for (i = 0; i < MLX4_NUM_TUNNEL_BUFS; i++) {
1508 ib_dma_unmap_single(ctx->ib_dev, tun_qp->ring[i].map,
1509 rx_buf_size, DMA_FROM_DEVICE);
1510 kfree(tun_qp->ring[i].addr);
1511 }
1512
1513 for (i = 0; i < MLX4_NUM_TUNNEL_BUFS; i++) {
1514 ib_dma_unmap_single(ctx->ib_dev, tun_qp->tx_ring[i].buf.map,
1515 tx_buf_size, DMA_TO_DEVICE);
1516 kfree(tun_qp->tx_ring[i].buf.addr);
1517 if (tun_qp->tx_ring[i].ah)
1518 ib_destroy_ah(tun_qp->tx_ring[i].ah);
1519 }
1520 kfree(tun_qp->tx_ring);
1521 kfree(tun_qp->ring);
1522}
1523
1524static void mlx4_ib_tunnel_comp_worker(struct work_struct *work)
1525{
Jack Morgenstein37bfc7c2012-08-03 08:40:44 +00001526 struct mlx4_ib_demux_pv_ctx *ctx;
1527 struct mlx4_ib_demux_pv_qp *tun_qp;
1528 struct ib_wc wc;
1529 int ret;
1530 ctx = container_of(work, struct mlx4_ib_demux_pv_ctx, work);
1531 ib_req_notify_cq(ctx->cq, IB_CQ_NEXT_COMP);
1532
1533 while (ib_poll_cq(ctx->cq, 1, &wc) == 1) {
1534 tun_qp = &ctx->qp[MLX4_TUN_WRID_QPN(wc.wr_id)];
1535 if (wc.status == IB_WC_SUCCESS) {
1536 switch (wc.opcode) {
1537 case IB_WC_RECV:
1538 mlx4_ib_multiplex_mad(ctx, &wc);
1539 ret = mlx4_ib_post_pv_qp_buf(ctx, tun_qp,
1540 wc.wr_id &
1541 (MLX4_NUM_TUNNEL_BUFS - 1));
1542 if (ret)
1543 pr_err("Failed reposting tunnel "
1544 "buf:%lld\n", wc.wr_id);
1545 break;
1546 case IB_WC_SEND:
1547 pr_debug("received tunnel send completion:"
1548 "wrid=0x%llx, status=0x%x\n",
1549 wc.wr_id, wc.status);
1550 ib_destroy_ah(tun_qp->tx_ring[wc.wr_id &
1551 (MLX4_NUM_TUNNEL_BUFS - 1)].ah);
1552 tun_qp->tx_ring[wc.wr_id & (MLX4_NUM_TUNNEL_BUFS - 1)].ah
1553 = NULL;
1554 spin_lock(&tun_qp->tx_lock);
1555 tun_qp->tx_ix_tail++;
1556 spin_unlock(&tun_qp->tx_lock);
1557
1558 break;
1559 default:
1560 break;
1561 }
1562 } else {
1563 pr_debug("mlx4_ib: completion error in tunnel: %d."
1564 " status = %d, wrid = 0x%llx\n",
1565 ctx->slave, wc.status, wc.wr_id);
1566 if (!MLX4_TUN_IS_RECV(wc.wr_id)) {
1567 ib_destroy_ah(tun_qp->tx_ring[wc.wr_id &
1568 (MLX4_NUM_TUNNEL_BUFS - 1)].ah);
1569 tun_qp->tx_ring[wc.wr_id & (MLX4_NUM_TUNNEL_BUFS - 1)].ah
1570 = NULL;
1571 spin_lock(&tun_qp->tx_lock);
1572 tun_qp->tx_ix_tail++;
1573 spin_unlock(&tun_qp->tx_lock);
1574 }
1575 }
1576 }
Jack Morgensteinfc065732012-08-03 08:40:42 +00001577}
1578
1579static void pv_qp_event_handler(struct ib_event *event, void *qp_context)
1580{
1581 struct mlx4_ib_demux_pv_ctx *sqp = qp_context;
1582
1583 /* It's worse than that! He's dead, Jim! */
1584 pr_err("Fatal error (%d) on a MAD QP on port %d\n",
1585 event->event, sqp->port);
1586}
1587
1588static int create_pv_sqp(struct mlx4_ib_demux_pv_ctx *ctx,
1589 enum ib_qp_type qp_type, int create_tun)
1590{
1591 int i, ret;
1592 struct mlx4_ib_demux_pv_qp *tun_qp;
1593 struct mlx4_ib_qp_tunnel_init_attr qp_init_attr;
1594 struct ib_qp_attr attr;
1595 int qp_attr_mask_INIT;
1596
1597 if (qp_type > IB_QPT_GSI)
1598 return -EINVAL;
1599
1600 tun_qp = &ctx->qp[qp_type];
1601
1602 memset(&qp_init_attr, 0, sizeof qp_init_attr);
1603 qp_init_attr.init_attr.send_cq = ctx->cq;
1604 qp_init_attr.init_attr.recv_cq = ctx->cq;
1605 qp_init_attr.init_attr.sq_sig_type = IB_SIGNAL_ALL_WR;
1606 qp_init_attr.init_attr.cap.max_send_wr = MLX4_NUM_TUNNEL_BUFS;
1607 qp_init_attr.init_attr.cap.max_recv_wr = MLX4_NUM_TUNNEL_BUFS;
1608 qp_init_attr.init_attr.cap.max_send_sge = 1;
1609 qp_init_attr.init_attr.cap.max_recv_sge = 1;
1610 if (create_tun) {
1611 qp_init_attr.init_attr.qp_type = IB_QPT_UD;
1612 qp_init_attr.init_attr.create_flags = MLX4_IB_SRIOV_TUNNEL_QP;
1613 qp_init_attr.port = ctx->port;
1614 qp_init_attr.slave = ctx->slave;
1615 qp_init_attr.proxy_qp_type = qp_type;
1616 qp_attr_mask_INIT = IB_QP_STATE | IB_QP_PKEY_INDEX |
1617 IB_QP_QKEY | IB_QP_PORT;
1618 } else {
1619 qp_init_attr.init_attr.qp_type = qp_type;
1620 qp_init_attr.init_attr.create_flags = MLX4_IB_SRIOV_SQP;
1621 qp_attr_mask_INIT = IB_QP_STATE | IB_QP_PKEY_INDEX | IB_QP_QKEY;
1622 }
1623 qp_init_attr.init_attr.port_num = ctx->port;
1624 qp_init_attr.init_attr.qp_context = ctx;
1625 qp_init_attr.init_attr.event_handler = pv_qp_event_handler;
1626 tun_qp->qp = ib_create_qp(ctx->pd, &qp_init_attr.init_attr);
1627 if (IS_ERR(tun_qp->qp)) {
1628 ret = PTR_ERR(tun_qp->qp);
1629 tun_qp->qp = NULL;
1630 pr_err("Couldn't create %s QP (%d)\n",
1631 create_tun ? "tunnel" : "special", ret);
1632 return ret;
1633 }
1634
1635 memset(&attr, 0, sizeof attr);
1636 attr.qp_state = IB_QPS_INIT;
Jack Morgenstein3eac103f2013-07-18 14:02:30 +03001637 ret = 0;
1638 if (create_tun)
1639 ret = find_slave_port_pkey_ix(to_mdev(ctx->ib_dev), ctx->slave,
1640 ctx->port, IB_DEFAULT_PKEY_FULL,
1641 &attr.pkey_index);
1642 if (ret || !create_tun)
1643 attr.pkey_index =
1644 to_mdev(ctx->ib_dev)->pkeys.virt2phys_pkey[ctx->slave][ctx->port - 1][0];
Jack Morgensteinfc065732012-08-03 08:40:42 +00001645 attr.qkey = IB_QP1_QKEY;
1646 attr.port_num = ctx->port;
1647 ret = ib_modify_qp(tun_qp->qp, &attr, qp_attr_mask_INIT);
1648 if (ret) {
1649 pr_err("Couldn't change %s qp state to INIT (%d)\n",
1650 create_tun ? "tunnel" : "special", ret);
1651 goto err_qp;
1652 }
1653 attr.qp_state = IB_QPS_RTR;
1654 ret = ib_modify_qp(tun_qp->qp, &attr, IB_QP_STATE);
1655 if (ret) {
1656 pr_err("Couldn't change %s qp state to RTR (%d)\n",
1657 create_tun ? "tunnel" : "special", ret);
1658 goto err_qp;
1659 }
1660 attr.qp_state = IB_QPS_RTS;
1661 attr.sq_psn = 0;
1662 ret = ib_modify_qp(tun_qp->qp, &attr, IB_QP_STATE | IB_QP_SQ_PSN);
1663 if (ret) {
1664 pr_err("Couldn't change %s qp state to RTS (%d)\n",
1665 create_tun ? "tunnel" : "special", ret);
1666 goto err_qp;
1667 }
1668
1669 for (i = 0; i < MLX4_NUM_TUNNEL_BUFS; i++) {
1670 ret = mlx4_ib_post_pv_qp_buf(ctx, tun_qp, i);
1671 if (ret) {
1672 pr_err(" mlx4_ib_post_pv_buf error"
1673 " (err = %d, i = %d)\n", ret, i);
1674 goto err_qp;
1675 }
1676 }
1677 return 0;
1678
1679err_qp:
1680 ib_destroy_qp(tun_qp->qp);
1681 tun_qp->qp = NULL;
1682 return ret;
1683}
1684
1685/*
1686 * IB MAD completion callback for real SQPs
1687 */
1688static void mlx4_ib_sqp_comp_worker(struct work_struct *work)
1689{
Jack Morgenstein37bfc7c2012-08-03 08:40:44 +00001690 struct mlx4_ib_demux_pv_ctx *ctx;
1691 struct mlx4_ib_demux_pv_qp *sqp;
1692 struct ib_wc wc;
1693 struct ib_grh *grh;
1694 struct ib_mad *mad;
1695
1696 ctx = container_of(work, struct mlx4_ib_demux_pv_ctx, work);
1697 ib_req_notify_cq(ctx->cq, IB_CQ_NEXT_COMP);
1698
1699 while (mlx4_ib_poll_cq(ctx->cq, 1, &wc) == 1) {
1700 sqp = &ctx->qp[MLX4_TUN_WRID_QPN(wc.wr_id)];
1701 if (wc.status == IB_WC_SUCCESS) {
1702 switch (wc.opcode) {
1703 case IB_WC_SEND:
1704 ib_destroy_ah(sqp->tx_ring[wc.wr_id &
1705 (MLX4_NUM_TUNNEL_BUFS - 1)].ah);
1706 sqp->tx_ring[wc.wr_id & (MLX4_NUM_TUNNEL_BUFS - 1)].ah
1707 = NULL;
1708 spin_lock(&sqp->tx_lock);
1709 sqp->tx_ix_tail++;
1710 spin_unlock(&sqp->tx_lock);
1711 break;
1712 case IB_WC_RECV:
1713 mad = (struct ib_mad *) &(((struct mlx4_mad_rcv_buf *)
1714 (sqp->ring[wc.wr_id &
1715 (MLX4_NUM_TUNNEL_BUFS - 1)].addr))->payload);
1716 grh = &(((struct mlx4_mad_rcv_buf *)
1717 (sqp->ring[wc.wr_id &
1718 (MLX4_NUM_TUNNEL_BUFS - 1)].addr))->grh);
1719 mlx4_ib_demux_mad(ctx->ib_dev, ctx->port, &wc, grh, mad);
1720 if (mlx4_ib_post_pv_qp_buf(ctx, sqp, wc.wr_id &
1721 (MLX4_NUM_TUNNEL_BUFS - 1)))
1722 pr_err("Failed reposting SQP "
1723 "buf:%lld\n", wc.wr_id);
1724 break;
1725 default:
1726 BUG_ON(1);
1727 break;
1728 }
1729 } else {
1730 pr_debug("mlx4_ib: completion error in tunnel: %d."
1731 " status = %d, wrid = 0x%llx\n",
1732 ctx->slave, wc.status, wc.wr_id);
1733 if (!MLX4_TUN_IS_RECV(wc.wr_id)) {
1734 ib_destroy_ah(sqp->tx_ring[wc.wr_id &
1735 (MLX4_NUM_TUNNEL_BUFS - 1)].ah);
1736 sqp->tx_ring[wc.wr_id & (MLX4_NUM_TUNNEL_BUFS - 1)].ah
1737 = NULL;
1738 spin_lock(&sqp->tx_lock);
1739 sqp->tx_ix_tail++;
1740 spin_unlock(&sqp->tx_lock);
1741 }
1742 }
1743 }
Jack Morgensteinfc065732012-08-03 08:40:42 +00001744}
1745
1746static int alloc_pv_object(struct mlx4_ib_dev *dev, int slave, int port,
1747 struct mlx4_ib_demux_pv_ctx **ret_ctx)
1748{
1749 struct mlx4_ib_demux_pv_ctx *ctx;
1750
1751 *ret_ctx = NULL;
1752 ctx = kzalloc(sizeof (struct mlx4_ib_demux_pv_ctx), GFP_KERNEL);
1753 if (!ctx) {
1754 pr_err("failed allocating pv resource context "
1755 "for port %d, slave %d\n", port, slave);
1756 return -ENOMEM;
1757 }
1758
1759 ctx->ib_dev = &dev->ib_dev;
1760 ctx->port = port;
1761 ctx->slave = slave;
1762 *ret_ctx = ctx;
1763 return 0;
1764}
1765
1766static void free_pv_object(struct mlx4_ib_dev *dev, int slave, int port)
1767{
1768 if (dev->sriov.demux[port - 1].tun[slave]) {
1769 kfree(dev->sriov.demux[port - 1].tun[slave]);
1770 dev->sriov.demux[port - 1].tun[slave] = NULL;
1771 }
1772}
1773
1774static int create_pv_resources(struct ib_device *ibdev, int slave, int port,
1775 int create_tun, struct mlx4_ib_demux_pv_ctx *ctx)
1776{
1777 int ret, cq_size;
Matan Barak8e372102015-06-11 16:35:21 +03001778 struct ib_cq_init_attr cq_attr = {};
Jack Morgensteinfc065732012-08-03 08:40:42 +00001779
Jack Morgenstein3806d082012-08-03 08:40:58 +00001780 if (ctx->state != DEMUX_PV_STATE_DOWN)
1781 return -EEXIST;
1782
Jack Morgensteinfc065732012-08-03 08:40:42 +00001783 ctx->state = DEMUX_PV_STATE_STARTING;
Jack Morgenstein97982f52014-05-29 16:31:02 +03001784 /* have QP0 only if link layer is IB */
1785 if (rdma_port_get_link_layer(ibdev, ctx->port) ==
1786 IB_LINK_LAYER_INFINIBAND)
Jack Morgensteinfc065732012-08-03 08:40:42 +00001787 ctx->has_smi = 1;
1788
1789 if (ctx->has_smi) {
1790 ret = mlx4_ib_alloc_pv_bufs(ctx, IB_QPT_SMI, create_tun);
1791 if (ret) {
1792 pr_err("Failed allocating qp0 tunnel bufs (%d)\n", ret);
1793 goto err_out;
1794 }
1795 }
1796
1797 ret = mlx4_ib_alloc_pv_bufs(ctx, IB_QPT_GSI, create_tun);
1798 if (ret) {
1799 pr_err("Failed allocating qp1 tunnel bufs (%d)\n", ret);
1800 goto err_out_qp0;
1801 }
1802
1803 cq_size = 2 * MLX4_NUM_TUNNEL_BUFS;
1804 if (ctx->has_smi)
1805 cq_size *= 2;
1806
Matan Barak8e372102015-06-11 16:35:21 +03001807 cq_attr.cqe = cq_size;
Jack Morgensteinfc065732012-08-03 08:40:42 +00001808 ctx->cq = ib_create_cq(ctx->ib_dev, mlx4_ib_tunnel_comp_handler,
Matan Barak8e372102015-06-11 16:35:21 +03001809 NULL, ctx, &cq_attr);
Jack Morgensteinfc065732012-08-03 08:40:42 +00001810 if (IS_ERR(ctx->cq)) {
1811 ret = PTR_ERR(ctx->cq);
1812 pr_err("Couldn't create tunnel CQ (%d)\n", ret);
1813 goto err_buf;
1814 }
1815
1816 ctx->pd = ib_alloc_pd(ctx->ib_dev);
1817 if (IS_ERR(ctx->pd)) {
1818 ret = PTR_ERR(ctx->pd);
1819 pr_err("Couldn't create tunnel PD (%d)\n", ret);
1820 goto err_cq;
1821 }
1822
1823 ctx->mr = ib_get_dma_mr(ctx->pd, IB_ACCESS_LOCAL_WRITE);
1824 if (IS_ERR(ctx->mr)) {
1825 ret = PTR_ERR(ctx->mr);
1826 pr_err("Couldn't get tunnel DMA MR (%d)\n", ret);
1827 goto err_pd;
1828 }
1829
1830 if (ctx->has_smi) {
1831 ret = create_pv_sqp(ctx, IB_QPT_SMI, create_tun);
1832 if (ret) {
1833 pr_err("Couldn't create %s QP0 (%d)\n",
1834 create_tun ? "tunnel for" : "", ret);
1835 goto err_mr;
1836 }
1837 }
1838
1839 ret = create_pv_sqp(ctx, IB_QPT_GSI, create_tun);
1840 if (ret) {
1841 pr_err("Couldn't create %s QP1 (%d)\n",
1842 create_tun ? "tunnel for" : "", ret);
1843 goto err_qp0;
1844 }
1845
1846 if (create_tun)
1847 INIT_WORK(&ctx->work, mlx4_ib_tunnel_comp_worker);
1848 else
1849 INIT_WORK(&ctx->work, mlx4_ib_sqp_comp_worker);
1850
1851 ctx->wq = to_mdev(ibdev)->sriov.demux[port - 1].wq;
1852
1853 ret = ib_req_notify_cq(ctx->cq, IB_CQ_NEXT_COMP);
1854 if (ret) {
1855 pr_err("Couldn't arm tunnel cq (%d)\n", ret);
1856 goto err_wq;
1857 }
1858 ctx->state = DEMUX_PV_STATE_ACTIVE;
1859 return 0;
1860
1861err_wq:
1862 ctx->wq = NULL;
1863 ib_destroy_qp(ctx->qp[1].qp);
1864 ctx->qp[1].qp = NULL;
1865
1866
1867err_qp0:
1868 if (ctx->has_smi)
1869 ib_destroy_qp(ctx->qp[0].qp);
1870 ctx->qp[0].qp = NULL;
1871
1872err_mr:
1873 ib_dereg_mr(ctx->mr);
1874 ctx->mr = NULL;
1875
1876err_pd:
1877 ib_dealloc_pd(ctx->pd);
1878 ctx->pd = NULL;
1879
1880err_cq:
1881 ib_destroy_cq(ctx->cq);
1882 ctx->cq = NULL;
1883
1884err_buf:
1885 mlx4_ib_free_pv_qp_bufs(ctx, IB_QPT_GSI, create_tun);
1886
1887err_out_qp0:
1888 if (ctx->has_smi)
1889 mlx4_ib_free_pv_qp_bufs(ctx, IB_QPT_SMI, create_tun);
1890err_out:
1891 ctx->state = DEMUX_PV_STATE_DOWN;
1892 return ret;
1893}
1894
1895static void destroy_pv_resources(struct mlx4_ib_dev *dev, int slave, int port,
1896 struct mlx4_ib_demux_pv_ctx *ctx, int flush)
1897{
1898 if (!ctx)
1899 return;
1900 if (ctx->state > DEMUX_PV_STATE_DOWN) {
1901 ctx->state = DEMUX_PV_STATE_DOWNING;
1902 if (flush)
1903 flush_workqueue(ctx->wq);
1904 if (ctx->has_smi) {
1905 ib_destroy_qp(ctx->qp[0].qp);
1906 ctx->qp[0].qp = NULL;
1907 mlx4_ib_free_pv_qp_bufs(ctx, IB_QPT_SMI, 1);
1908 }
1909 ib_destroy_qp(ctx->qp[1].qp);
1910 ctx->qp[1].qp = NULL;
1911 mlx4_ib_free_pv_qp_bufs(ctx, IB_QPT_GSI, 1);
1912 ib_dereg_mr(ctx->mr);
1913 ctx->mr = NULL;
1914 ib_dealloc_pd(ctx->pd);
1915 ctx->pd = NULL;
1916 ib_destroy_cq(ctx->cq);
1917 ctx->cq = NULL;
1918 ctx->state = DEMUX_PV_STATE_DOWN;
1919 }
1920}
1921
1922static int mlx4_ib_tunnels_update(struct mlx4_ib_dev *dev, int slave,
1923 int port, int do_init)
1924{
1925 int ret = 0;
1926
1927 if (!do_init) {
Oren Duerb9c5d6a2012-08-03 08:40:46 +00001928 clean_vf_mcast(&dev->sriov.demux[port - 1], slave);
Jack Morgensteinfc065732012-08-03 08:40:42 +00001929 /* for master, destroy real sqp resources */
1930 if (slave == mlx4_master_func_num(dev->dev))
1931 destroy_pv_resources(dev, slave, port,
1932 dev->sriov.sqps[port - 1], 1);
1933 /* destroy the tunnel qp resources */
1934 destroy_pv_resources(dev, slave, port,
1935 dev->sriov.demux[port - 1].tun[slave], 1);
1936 return 0;
1937 }
1938
1939 /* create the tunnel qp resources */
1940 ret = create_pv_resources(&dev->ib_dev, slave, port, 1,
1941 dev->sriov.demux[port - 1].tun[slave]);
1942
1943 /* for master, create the real sqp resources */
1944 if (!ret && slave == mlx4_master_func_num(dev->dev))
1945 ret = create_pv_resources(&dev->ib_dev, slave, port, 0,
1946 dev->sriov.sqps[port - 1]);
1947 return ret;
1948}
1949
1950void mlx4_ib_tunnels_update_work(struct work_struct *work)
1951{
1952 struct mlx4_ib_demux_work *dmxw;
1953
1954 dmxw = container_of(work, struct mlx4_ib_demux_work, work);
1955 mlx4_ib_tunnels_update(dmxw->dev, dmxw->slave, (int) dmxw->port,
1956 dmxw->do_init);
1957 kfree(dmxw);
1958 return;
1959}
1960
1961static int mlx4_ib_alloc_demux_ctx(struct mlx4_ib_dev *dev,
1962 struct mlx4_ib_demux_ctx *ctx,
1963 int port)
1964{
1965 char name[12];
1966 int ret = 0;
1967 int i;
1968
1969 ctx->tun = kcalloc(dev->dev->caps.sqp_demux,
1970 sizeof (struct mlx4_ib_demux_pv_ctx *), GFP_KERNEL);
1971 if (!ctx->tun)
1972 return -ENOMEM;
1973
1974 ctx->dev = dev;
1975 ctx->port = port;
1976 ctx->ib_dev = &dev->ib_dev;
1977
Matan Barak449fc482014-03-19 18:11:52 +02001978 for (i = 0;
Yishai Hadas872bf2f2015-01-25 16:59:35 +02001979 i < min(dev->dev->caps.sqp_demux,
1980 (u16)(dev->dev->persist->num_vfs + 1));
Matan Barak449fc482014-03-19 18:11:52 +02001981 i++) {
1982 struct mlx4_active_ports actv_ports =
1983 mlx4_get_active_ports(dev->dev, i);
1984
1985 if (!test_bit(port - 1, actv_ports.ports))
1986 continue;
1987
Jack Morgensteinfc065732012-08-03 08:40:42 +00001988 ret = alloc_pv_object(dev, i, port, &ctx->tun[i]);
1989 if (ret) {
1990 ret = -ENOMEM;
Oren Duerb9c5d6a2012-08-03 08:40:46 +00001991 goto err_mcg;
Jack Morgensteinfc065732012-08-03 08:40:42 +00001992 }
1993 }
1994
Oren Duerb9c5d6a2012-08-03 08:40:46 +00001995 ret = mlx4_ib_mcg_port_init(ctx);
1996 if (ret) {
1997 pr_err("Failed initializing mcg para-virt (%d)\n", ret);
1998 goto err_mcg;
1999 }
2000
Jack Morgensteinfc065732012-08-03 08:40:42 +00002001 snprintf(name, sizeof name, "mlx4_ibt%d", port);
2002 ctx->wq = create_singlethread_workqueue(name);
2003 if (!ctx->wq) {
2004 pr_err("Failed to create tunnelling WQ for port %d\n", port);
2005 ret = -ENOMEM;
2006 goto err_wq;
2007 }
2008
2009 snprintf(name, sizeof name, "mlx4_ibud%d", port);
2010 ctx->ud_wq = create_singlethread_workqueue(name);
2011 if (!ctx->ud_wq) {
2012 pr_err("Failed to create up/down WQ for port %d\n", port);
2013 ret = -ENOMEM;
2014 goto err_udwq;
2015 }
2016
2017 return 0;
2018
2019err_udwq:
2020 destroy_workqueue(ctx->wq);
2021 ctx->wq = NULL;
2022
2023err_wq:
Oren Duerb9c5d6a2012-08-03 08:40:46 +00002024 mlx4_ib_mcg_port_cleanup(ctx, 1);
2025err_mcg:
Jack Morgensteinfc065732012-08-03 08:40:42 +00002026 for (i = 0; i < dev->dev->caps.sqp_demux; i++)
2027 free_pv_object(dev, i, port);
2028 kfree(ctx->tun);
2029 ctx->tun = NULL;
2030 return ret;
2031}
2032
2033static void mlx4_ib_free_sqp_ctx(struct mlx4_ib_demux_pv_ctx *sqp_ctx)
2034{
2035 if (sqp_ctx->state > DEMUX_PV_STATE_DOWN) {
2036 sqp_ctx->state = DEMUX_PV_STATE_DOWNING;
2037 flush_workqueue(sqp_ctx->wq);
2038 if (sqp_ctx->has_smi) {
2039 ib_destroy_qp(sqp_ctx->qp[0].qp);
2040 sqp_ctx->qp[0].qp = NULL;
2041 mlx4_ib_free_pv_qp_bufs(sqp_ctx, IB_QPT_SMI, 0);
2042 }
2043 ib_destroy_qp(sqp_ctx->qp[1].qp);
2044 sqp_ctx->qp[1].qp = NULL;
2045 mlx4_ib_free_pv_qp_bufs(sqp_ctx, IB_QPT_GSI, 0);
2046 ib_dereg_mr(sqp_ctx->mr);
2047 sqp_ctx->mr = NULL;
2048 ib_dealloc_pd(sqp_ctx->pd);
2049 sqp_ctx->pd = NULL;
2050 ib_destroy_cq(sqp_ctx->cq);
2051 sqp_ctx->cq = NULL;
2052 sqp_ctx->state = DEMUX_PV_STATE_DOWN;
2053 }
2054}
2055
2056static void mlx4_ib_free_demux_ctx(struct mlx4_ib_demux_ctx *ctx)
2057{
2058 int i;
2059 if (ctx) {
2060 struct mlx4_ib_dev *dev = to_mdev(ctx->ib_dev);
Oren Duerb9c5d6a2012-08-03 08:40:46 +00002061 mlx4_ib_mcg_port_cleanup(ctx, 1);
Jack Morgensteinfc065732012-08-03 08:40:42 +00002062 for (i = 0; i < dev->dev->caps.sqp_demux; i++) {
2063 if (!ctx->tun[i])
2064 continue;
2065 if (ctx->tun[i]->state > DEMUX_PV_STATE_DOWN)
2066 ctx->tun[i]->state = DEMUX_PV_STATE_DOWNING;
2067 }
2068 flush_workqueue(ctx->wq);
2069 for (i = 0; i < dev->dev->caps.sqp_demux; i++) {
2070 destroy_pv_resources(dev, i, ctx->port, ctx->tun[i], 0);
2071 free_pv_object(dev, i, ctx->port);
2072 }
2073 kfree(ctx->tun);
2074 destroy_workqueue(ctx->ud_wq);
2075 destroy_workqueue(ctx->wq);
2076 }
2077}
2078
2079static void mlx4_ib_master_tunnels(struct mlx4_ib_dev *dev, int do_init)
2080{
2081 int i;
2082
2083 if (!mlx4_is_master(dev->dev))
2084 return;
2085 /* initialize or tear down tunnel QPs for the master */
2086 for (i = 0; i < dev->dev->caps.num_ports; i++)
2087 mlx4_ib_tunnels_update(dev, mlx4_master_func_num(dev->dev), i + 1, do_init);
2088 return;
2089}
2090
2091int mlx4_ib_init_sriov(struct mlx4_ib_dev *dev)
2092{
2093 int i = 0;
2094 int err;
2095
2096 if (!mlx4_is_mfunc(dev->dev))
2097 return 0;
2098
2099 dev->sriov.is_going_down = 0;
2100 spin_lock_init(&dev->sriov.going_down_lock);
Amir Vadai3cf69cc2012-08-03 08:40:47 +00002101 mlx4_ib_cm_paravirt_init(dev);
Jack Morgensteinfc065732012-08-03 08:40:42 +00002102
2103 mlx4_ib_warn(&dev->ib_dev, "multi-function enabled\n");
2104
2105 if (mlx4_is_slave(dev->dev)) {
2106 mlx4_ib_warn(&dev->ib_dev, "operating in qp1 tunnel mode\n");
2107 return 0;
2108 }
2109
Jack Morgensteinafa8fd12012-08-03 08:40:56 +00002110 for (i = 0; i < dev->dev->caps.sqp_demux; i++) {
2111 if (i == mlx4_master_func_num(dev->dev))
2112 mlx4_put_slave_node_guid(dev->dev, i, dev->ib_dev.node_guid);
2113 else
2114 mlx4_put_slave_node_guid(dev->dev, i, mlx4_ib_gen_node_guid());
2115 }
2116
Jack Morgensteina0c64a12012-08-03 08:40:49 +00002117 err = mlx4_ib_init_alias_guid_service(dev);
2118 if (err) {
2119 mlx4_ib_warn(&dev->ib_dev, "Failed init alias guid process.\n");
2120 goto paravirt_err;
2121 }
Jack Morgensteinc1e7e462012-08-03 08:40:51 +00002122 err = mlx4_ib_device_register_sysfs(dev);
2123 if (err) {
2124 mlx4_ib_warn(&dev->ib_dev, "Failed to register sysfs\n");
2125 goto sysfs_err;
2126 }
Jack Morgensteina0c64a12012-08-03 08:40:49 +00002127
Jack Morgensteinfc065732012-08-03 08:40:42 +00002128 mlx4_ib_warn(&dev->ib_dev, "initializing demux service for %d qp1 clients\n",
2129 dev->dev->caps.sqp_demux);
2130 for (i = 0; i < dev->num_ports; i++) {
Jack Morgensteina0c64a12012-08-03 08:40:49 +00002131 union ib_gid gid;
2132 err = __mlx4_ib_query_gid(&dev->ib_dev, i + 1, 0, &gid, 1);
2133 if (err)
2134 goto demux_err;
2135 dev->sriov.demux[i].guid_cache[0] = gid.global.interface_id;
Jack Morgensteinfc065732012-08-03 08:40:42 +00002136 err = alloc_pv_object(dev, mlx4_master_func_num(dev->dev), i + 1,
2137 &dev->sriov.sqps[i]);
2138 if (err)
2139 goto demux_err;
2140 err = mlx4_ib_alloc_demux_ctx(dev, &dev->sriov.demux[i], i + 1);
2141 if (err)
Dan Carpentercab66d12013-02-04 11:22:36 +00002142 goto free_pv;
Jack Morgensteinfc065732012-08-03 08:40:42 +00002143 }
2144 mlx4_ib_master_tunnels(dev, 1);
2145 return 0;
2146
Dan Carpentercab66d12013-02-04 11:22:36 +00002147free_pv:
2148 free_pv_object(dev, mlx4_master_func_num(dev->dev), i + 1);
Jack Morgensteinfc065732012-08-03 08:40:42 +00002149demux_err:
Dan Carpentercab66d12013-02-04 11:22:36 +00002150 while (--i >= 0) {
Jack Morgensteinfc065732012-08-03 08:40:42 +00002151 free_pv_object(dev, mlx4_master_func_num(dev->dev), i + 1);
2152 mlx4_ib_free_demux_ctx(&dev->sriov.demux[i]);
Jack Morgensteinfc065732012-08-03 08:40:42 +00002153 }
Jack Morgensteinc1e7e462012-08-03 08:40:51 +00002154 mlx4_ib_device_unregister_sysfs(dev);
2155
2156sysfs_err:
Jack Morgensteina0c64a12012-08-03 08:40:49 +00002157 mlx4_ib_destroy_alias_guid_service(dev);
2158
2159paravirt_err:
Amir Vadai3cf69cc2012-08-03 08:40:47 +00002160 mlx4_ib_cm_paravirt_clean(dev, -1);
Jack Morgensteinfc065732012-08-03 08:40:42 +00002161
2162 return err;
2163}
2164
2165void mlx4_ib_close_sriov(struct mlx4_ib_dev *dev)
2166{
2167 int i;
2168 unsigned long flags;
2169
2170 if (!mlx4_is_mfunc(dev->dev))
2171 return;
2172
2173 spin_lock_irqsave(&dev->sriov.going_down_lock, flags);
2174 dev->sriov.is_going_down = 1;
2175 spin_unlock_irqrestore(&dev->sriov.going_down_lock, flags);
Amir Vadai3cf69cc2012-08-03 08:40:47 +00002176 if (mlx4_is_master(dev->dev)) {
Jack Morgensteinfc065732012-08-03 08:40:42 +00002177 for (i = 0; i < dev->num_ports; i++) {
2178 flush_workqueue(dev->sriov.demux[i].ud_wq);
2179 mlx4_ib_free_sqp_ctx(dev->sriov.sqps[i]);
2180 kfree(dev->sriov.sqps[i]);
2181 dev->sriov.sqps[i] = NULL;
2182 mlx4_ib_free_demux_ctx(&dev->sriov.demux[i]);
2183 }
Amir Vadai3cf69cc2012-08-03 08:40:47 +00002184
2185 mlx4_ib_cm_paravirt_clean(dev, -1);
Jack Morgensteina0c64a12012-08-03 08:40:49 +00002186 mlx4_ib_destroy_alias_guid_service(dev);
Jack Morgensteinc1e7e462012-08-03 08:40:51 +00002187 mlx4_ib_device_unregister_sysfs(dev);
Amir Vadai3cf69cc2012-08-03 08:40:47 +00002188 }
Jack Morgensteinfc065732012-08-03 08:40:42 +00002189}