blob: ba2580693f793e542d8fbddf81b58e5515d826da [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
38#include <linux/mlx4/cmd.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090039#include <linux/gfp.h>
Or Gerlitzc3779132011-06-15 14:51:27 +000040#include <rdma/ib_pma.h>
Roland Dreier225c7b12007-05-08 18:00:38 -070041
42#include "mlx4_ib.h"
43
44enum {
45 MLX4_IB_VENDOR_CLASS1 = 0x9,
46 MLX4_IB_VENDOR_CLASS2 = 0xa
47};
48
Jack Morgensteinfc065732012-08-03 08:40:42 +000049#define MLX4_TUN_SEND_WRID_SHIFT 34
50#define MLX4_TUN_QPN_SHIFT 32
51#define MLX4_TUN_WRID_RECV (((u64) 1) << MLX4_TUN_SEND_WRID_SHIFT)
52#define MLX4_TUN_SET_WRID_QPN(a) (((u64) ((a) & 0x3)) << MLX4_TUN_QPN_SHIFT)
53
54#define MLX4_TUN_IS_RECV(a) (((a) >> MLX4_TUN_SEND_WRID_SHIFT) & 0x1)
55#define MLX4_TUN_WRID_QPN(a) (((a) >> MLX4_TUN_QPN_SHIFT) & 0x3)
56
57struct mlx4_mad_rcv_buf {
58 struct ib_grh grh;
59 u8 payload[256];
60} __packed;
61
62struct mlx4_mad_snd_buf {
63 u8 payload[256];
64} __packed;
65
66struct mlx4_tunnel_mad {
67 struct ib_grh grh;
68 struct mlx4_ib_tunnel_header hdr;
69 struct ib_mad mad;
70} __packed;
71
72struct mlx4_rcv_tunnel_mad {
73 struct mlx4_rcv_tunnel_hdr hdr;
74 struct ib_grh grh;
75 struct ib_mad mad;
76} __packed;
77
Jack Morgenstein0a9a0182012-08-03 08:40:45 +000078int mlx4_MAD_IFC(struct mlx4_ib_dev *dev, int mad_ifc_flags,
Roland Dreier225c7b12007-05-08 18:00:38 -070079 int port, struct ib_wc *in_wc, struct ib_grh *in_grh,
80 void *in_mad, void *response_mad)
81{
82 struct mlx4_cmd_mailbox *inmailbox, *outmailbox;
83 void *inbox;
84 int err;
85 u32 in_modifier = port;
86 u8 op_modifier = 0;
87
88 inmailbox = mlx4_alloc_cmd_mailbox(dev->dev);
89 if (IS_ERR(inmailbox))
90 return PTR_ERR(inmailbox);
91 inbox = inmailbox->buf;
92
93 outmailbox = mlx4_alloc_cmd_mailbox(dev->dev);
94 if (IS_ERR(outmailbox)) {
95 mlx4_free_cmd_mailbox(dev->dev, inmailbox);
96 return PTR_ERR(outmailbox);
97 }
98
99 memcpy(inbox, in_mad, 256);
100
101 /*
102 * Key check traps can't be generated unless we have in_wc to
103 * tell us where to send the trap.
104 */
Jack Morgenstein0a9a0182012-08-03 08:40:45 +0000105 if ((mad_ifc_flags & MLX4_MAD_IFC_IGNORE_MKEY) || !in_wc)
Roland Dreier225c7b12007-05-08 18:00:38 -0700106 op_modifier |= 0x1;
Jack Morgenstein0a9a0182012-08-03 08:40:45 +0000107 if ((mad_ifc_flags & MLX4_MAD_IFC_IGNORE_BKEY) || !in_wc)
Roland Dreier225c7b12007-05-08 18:00:38 -0700108 op_modifier |= 0x2;
Jack Morgenstein0a9a0182012-08-03 08:40:45 +0000109 if (mlx4_is_mfunc(dev->dev) &&
110 (mad_ifc_flags & MLX4_MAD_IFC_NET_VIEW || in_wc))
111 op_modifier |= 0x8;
Roland Dreier225c7b12007-05-08 18:00:38 -0700112
113 if (in_wc) {
114 struct {
115 __be32 my_qpn;
116 u32 reserved1;
117 __be32 rqpn;
118 u8 sl;
119 u8 g_path;
120 u16 reserved2[2];
121 __be16 pkey;
122 u32 reserved3[11];
123 u8 grh[40];
124 } *ext_info;
125
126 memset(inbox + 256, 0, 256);
127 ext_info = inbox + 256;
128
129 ext_info->my_qpn = cpu_to_be32(in_wc->qp->qp_num);
130 ext_info->rqpn = cpu_to_be32(in_wc->src_qp);
131 ext_info->sl = in_wc->sl << 4;
132 ext_info->g_path = in_wc->dlid_path_bits |
133 (in_wc->wc_flags & IB_WC_GRH ? 0x80 : 0);
134 ext_info->pkey = cpu_to_be16(in_wc->pkey_index);
135
136 if (in_grh)
137 memcpy(ext_info->grh, in_grh, 40);
138
139 op_modifier |= 0x4;
140
141 in_modifier |= in_wc->slid << 16;
142 }
143
Jack Morgenstein0a9a0182012-08-03 08:40:45 +0000144 err = mlx4_cmd_box(dev->dev, inmailbox->dma, outmailbox->dma, in_modifier,
145 mlx4_is_master(dev->dev) ? (op_modifier & ~0x8) : op_modifier,
Jack Morgensteinf9baff52011-12-13 04:10:51 +0000146 MLX4_CMD_MAD_IFC, MLX4_CMD_TIME_CLASS_C,
Jack Morgenstein0a9a0182012-08-03 08:40:45 +0000147 (op_modifier & 0x8) ? MLX4_CMD_NATIVE : MLX4_CMD_WRAPPED);
Roland Dreier225c7b12007-05-08 18:00:38 -0700148
Ilpo Järvinenfe11cb62007-08-16 01:02:07 +0300149 if (!err)
Roland Dreier225c7b12007-05-08 18:00:38 -0700150 memcpy(response_mad, outmailbox->buf, 256);
151
152 mlx4_free_cmd_mailbox(dev->dev, inmailbox);
153 mlx4_free_cmd_mailbox(dev->dev, outmailbox);
154
155 return err;
156}
157
158static void update_sm_ah(struct mlx4_ib_dev *dev, u8 port_num, u16 lid, u8 sl)
159{
160 struct ib_ah *new_ah;
161 struct ib_ah_attr ah_attr;
Jack Morgensteindf7fba62012-08-03 08:26:45 +0000162 unsigned long flags;
Roland Dreier225c7b12007-05-08 18:00:38 -0700163
164 if (!dev->send_agent[port_num - 1][0])
165 return;
166
167 memset(&ah_attr, 0, sizeof ah_attr);
168 ah_attr.dlid = lid;
169 ah_attr.sl = sl;
170 ah_attr.port_num = port_num;
171
172 new_ah = ib_create_ah(dev->send_agent[port_num - 1][0]->qp->pd,
173 &ah_attr);
174 if (IS_ERR(new_ah))
175 return;
176
Jack Morgensteindf7fba62012-08-03 08:26:45 +0000177 spin_lock_irqsave(&dev->sm_lock, flags);
Roland Dreier225c7b12007-05-08 18:00:38 -0700178 if (dev->sm_ah[port_num - 1])
179 ib_destroy_ah(dev->sm_ah[port_num - 1]);
180 dev->sm_ah[port_num - 1] = new_ah;
Jack Morgensteindf7fba62012-08-03 08:26:45 +0000181 spin_unlock_irqrestore(&dev->sm_lock, flags);
Roland Dreier225c7b12007-05-08 18:00:38 -0700182}
183
184/*
Jack Morgenstein00f5ce92012-06-19 11:21:40 +0300185 * Snoop SM MADs for port info, GUID info, and P_Key table sets, so we can
186 * synthesize LID change, Client-Rereg, GID change, and P_Key change events.
Roland Dreier225c7b12007-05-08 18:00:38 -0700187 */
Moni Shouaf0f6f342009-01-28 14:54:35 -0800188static void smp_snoop(struct ib_device *ibdev, u8 port_num, struct ib_mad *mad,
Jack Morgenstein00f5ce92012-06-19 11:21:40 +0300189 u16 prev_lid)
Roland Dreier225c7b12007-05-08 18:00:38 -0700190{
Jack Morgenstein00f5ce92012-06-19 11:21:40 +0300191 struct ib_port_info *pinfo;
192 u16 lid;
Jack Morgenstein54679e12012-08-03 08:40:43 +0000193 __be16 *base;
194 u32 bn, pkey_change_bitmap;
195 int i;
196
Roland Dreier225c7b12007-05-08 18:00:38 -0700197
Jack Morgenstein00f5ce92012-06-19 11:21:40 +0300198 struct mlx4_ib_dev *dev = to_mdev(ibdev);
Roland Dreier225c7b12007-05-08 18:00:38 -0700199 if ((mad->mad_hdr.mgmt_class == IB_MGMT_CLASS_SUBN_LID_ROUTED ||
200 mad->mad_hdr.mgmt_class == IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE) &&
Jack Morgenstein00f5ce92012-06-19 11:21:40 +0300201 mad->mad_hdr.method == IB_MGMT_METHOD_SET)
202 switch (mad->mad_hdr.attr_id) {
203 case IB_SMP_ATTR_PORT_INFO:
204 pinfo = (struct ib_port_info *) ((struct ib_smp *) mad)->data;
205 lid = be16_to_cpu(pinfo->lid);
Roland Dreier225c7b12007-05-08 18:00:38 -0700206
Jack Morgenstein00f5ce92012-06-19 11:21:40 +0300207 update_sm_ah(dev, port_num,
Roland Dreier225c7b12007-05-08 18:00:38 -0700208 be16_to_cpu(pinfo->sm_lid),
209 pinfo->neighbormtu_mastersmsl & 0xf);
210
Jack Morgenstein00f5ce92012-06-19 11:21:40 +0300211 if (pinfo->clientrereg_resv_subnetto & 0x80)
212 mlx4_ib_dispatch_event(dev, port_num,
213 IB_EVENT_CLIENT_REREGISTER);
Roland Dreier225c7b12007-05-08 18:00:38 -0700214
Jack Morgenstein00f5ce92012-06-19 11:21:40 +0300215 if (prev_lid != lid)
216 mlx4_ib_dispatch_event(dev, port_num,
217 IB_EVENT_LID_CHANGE);
218 break;
Roland Dreier225c7b12007-05-08 18:00:38 -0700219
Jack Morgenstein00f5ce92012-06-19 11:21:40 +0300220 case IB_SMP_ATTR_PKEY_TABLE:
Jack Morgenstein54679e12012-08-03 08:40:43 +0000221 if (!mlx4_is_mfunc(dev->dev)) {
222 mlx4_ib_dispatch_event(dev, port_num,
223 IB_EVENT_PKEY_CHANGE);
224 break;
225 }
226
227 bn = be32_to_cpu(((struct ib_smp *)mad)->attr_mod) & 0xFFFF;
228 base = (__be16 *) &(((struct ib_smp *)mad)->data[0]);
229 pkey_change_bitmap = 0;
230 for (i = 0; i < 32; i++) {
231 pr_debug("PKEY[%d] = x%x\n",
232 i + bn*32, be16_to_cpu(base[i]));
233 if (be16_to_cpu(base[i]) !=
234 dev->pkeys.phys_pkey_cache[port_num - 1][i + bn*32]) {
235 pkey_change_bitmap |= (1 << i);
236 dev->pkeys.phys_pkey_cache[port_num - 1][i + bn*32] =
237 be16_to_cpu(base[i]);
238 }
239 }
240 pr_debug("PKEY Change event: port=%d, "
241 "block=0x%x, change_bitmap=0x%x\n",
242 port_num, bn, pkey_change_bitmap);
243
244 if (pkey_change_bitmap)
245 mlx4_ib_dispatch_event(dev, port_num,
246 IB_EVENT_PKEY_CHANGE);
247
Jack Morgenstein00f5ce92012-06-19 11:21:40 +0300248 break;
249
250 case IB_SMP_ATTR_GUID_INFO:
Jack Morgenstein66349612012-06-19 11:21:44 +0300251 /* paravirtualized master's guid is guid 0 -- does not change */
252 if (!mlx4_is_master(dev->dev))
253 mlx4_ib_dispatch_event(dev, port_num,
254 IB_EVENT_GID_CHANGE);
Jack Morgenstein00f5ce92012-06-19 11:21:40 +0300255 break;
256 default:
257 break;
Roland Dreier225c7b12007-05-08 18:00:38 -0700258 }
Roland Dreier225c7b12007-05-08 18:00:38 -0700259}
260
261static void node_desc_override(struct ib_device *dev,
262 struct ib_mad *mad)
263{
Jack Morgensteindf7fba62012-08-03 08:26:45 +0000264 unsigned long flags;
265
Roland Dreier225c7b12007-05-08 18:00:38 -0700266 if ((mad->mad_hdr.mgmt_class == IB_MGMT_CLASS_SUBN_LID_ROUTED ||
267 mad->mad_hdr.mgmt_class == IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE) &&
268 mad->mad_hdr.method == IB_MGMT_METHOD_GET_RESP &&
269 mad->mad_hdr.attr_id == IB_SMP_ATTR_NODE_DESC) {
Jack Morgensteindf7fba62012-08-03 08:26:45 +0000270 spin_lock_irqsave(&to_mdev(dev)->sm_lock, flags);
Roland Dreier225c7b12007-05-08 18:00:38 -0700271 memcpy(((struct ib_smp *) mad)->data, dev->node_desc, 64);
Jack Morgensteindf7fba62012-08-03 08:26:45 +0000272 spin_unlock_irqrestore(&to_mdev(dev)->sm_lock, flags);
Roland Dreier225c7b12007-05-08 18:00:38 -0700273 }
274}
275
276static void forward_trap(struct mlx4_ib_dev *dev, u8 port_num, struct ib_mad *mad)
277{
278 int qpn = mad->mad_hdr.mgmt_class != IB_MGMT_CLASS_SUBN_LID_ROUTED;
279 struct ib_mad_send_buf *send_buf;
280 struct ib_mad_agent *agent = dev->send_agent[port_num - 1][qpn];
281 int ret;
Jack Morgensteindf7fba62012-08-03 08:26:45 +0000282 unsigned long flags;
Roland Dreier225c7b12007-05-08 18:00:38 -0700283
284 if (agent) {
285 send_buf = ib_create_send_mad(agent, qpn, 0, 0, IB_MGMT_MAD_HDR,
286 IB_MGMT_MAD_DATA, GFP_ATOMIC);
Dan Carpenter13974902011-01-10 17:42:06 -0800287 if (IS_ERR(send_buf))
288 return;
Roland Dreier225c7b12007-05-08 18:00:38 -0700289 /*
290 * We rely here on the fact that MLX QPs don't use the
291 * address handle after the send is posted (this is
292 * wrong following the IB spec strictly, but we know
293 * it's OK for our devices).
294 */
Jack Morgensteindf7fba62012-08-03 08:26:45 +0000295 spin_lock_irqsave(&dev->sm_lock, flags);
Roland Dreier225c7b12007-05-08 18:00:38 -0700296 memcpy(send_buf->mad, mad, sizeof *mad);
297 if ((send_buf->ah = dev->sm_ah[port_num - 1]))
298 ret = ib_post_send_mad(send_buf, NULL);
299 else
300 ret = -EINVAL;
Jack Morgensteindf7fba62012-08-03 08:26:45 +0000301 spin_unlock_irqrestore(&dev->sm_lock, flags);
Roland Dreier225c7b12007-05-08 18:00:38 -0700302
303 if (ret)
304 ib_free_send_mad(send_buf);
305 }
306}
307
Jack Morgenstein37bfc7c2012-08-03 08:40:44 +0000308static int mlx4_ib_demux_sa_handler(struct ib_device *ibdev, int port, int slave,
309 struct ib_sa_mad *sa_mad)
310{
311 return 0;
312}
313
314int mlx4_ib_find_real_gid(struct ib_device *ibdev, u8 port, __be64 guid)
315{
316 struct mlx4_ib_dev *dev = to_mdev(ibdev);
317 int i;
318
319 for (i = 0; i < dev->dev->caps.sqp_demux; i++) {
320 if (dev->sriov.demux[port - 1].guid_cache[i] == guid)
321 return i;
322 }
323 return -1;
324}
325
326
327static int get_pkey_phys_indices(struct mlx4_ib_dev *ibdev, u8 port, u8 ph_pkey_ix,
328 u8 *full_pk_ix, u8 *partial_pk_ix,
329 int *is_full_member)
330{
331 u16 search_pkey;
332 int fm;
333 int err = 0;
334 u16 pk;
335
336 err = ib_get_cached_pkey(&ibdev->ib_dev, port, ph_pkey_ix, &search_pkey);
337 if (err)
338 return err;
339
340 fm = (search_pkey & 0x8000) ? 1 : 0;
341 if (fm) {
342 *full_pk_ix = ph_pkey_ix;
343 search_pkey &= 0x7FFF;
344 } else {
345 *partial_pk_ix = ph_pkey_ix;
346 search_pkey |= 0x8000;
347 }
348
349 if (ib_find_exact_cached_pkey(&ibdev->ib_dev, port, search_pkey, &pk))
350 pk = 0xFFFF;
351
352 if (fm)
353 *partial_pk_ix = (pk & 0xFF);
354 else
355 *full_pk_ix = (pk & 0xFF);
356
357 *is_full_member = fm;
358 return err;
359}
360
361int mlx4_ib_send_to_slave(struct mlx4_ib_dev *dev, int slave, u8 port,
362 enum ib_qp_type dest_qpt, struct ib_wc *wc,
363 struct ib_grh *grh, struct ib_mad *mad)
364{
365 struct ib_sge list;
366 struct ib_send_wr wr, *bad_wr;
367 struct mlx4_ib_demux_pv_ctx *tun_ctx;
368 struct mlx4_ib_demux_pv_qp *tun_qp;
369 struct mlx4_rcv_tunnel_mad *tun_mad;
370 struct ib_ah_attr attr;
371 struct ib_ah *ah;
372 struct ib_qp *src_qp = NULL;
373 unsigned tun_tx_ix = 0;
374 int dqpn;
375 int ret = 0;
376 int i;
377 int is_full_member = 0;
378 u16 tun_pkey_ix;
379 u8 ph_pkey_ix, full_pk_ix = 0, partial_pk_ix = 0;
380
381 if (dest_qpt > IB_QPT_GSI)
382 return -EINVAL;
383
384 tun_ctx = dev->sriov.demux[port-1].tun[slave];
385
386 /* check if proxy qp created */
387 if (!tun_ctx || tun_ctx->state != DEMUX_PV_STATE_ACTIVE)
388 return -EAGAIN;
389
390 /* QP0 forwarding only for Dom0 */
391 if (!dest_qpt && (mlx4_master_func_num(dev->dev) != slave))
392 return -EINVAL;
393
394 if (!dest_qpt)
395 tun_qp = &tun_ctx->qp[0];
396 else
397 tun_qp = &tun_ctx->qp[1];
398
399 /* compute pkey index for slave */
400 /* get physical pkey -- virtualized Dom0 pkey to phys*/
401 if (dest_qpt) {
402 ph_pkey_ix =
403 dev->pkeys.virt2phys_pkey[mlx4_master_func_num(dev->dev)][port - 1][wc->pkey_index];
404
405 /* now, translate this to the slave pkey index */
406 ret = get_pkey_phys_indices(dev, port, ph_pkey_ix, &full_pk_ix,
407 &partial_pk_ix, &is_full_member);
408 if (ret)
409 return -EINVAL;
410
411 for (i = 0; i < dev->dev->caps.pkey_table_len[port]; i++) {
412 if ((dev->pkeys.virt2phys_pkey[slave][port - 1][i] == full_pk_ix) ||
413 (is_full_member &&
414 (dev->pkeys.virt2phys_pkey[slave][port - 1][i] == partial_pk_ix)))
415 break;
416 }
417 if (i == dev->dev->caps.pkey_table_len[port])
418 return -EINVAL;
419 tun_pkey_ix = i;
420 } else
421 tun_pkey_ix = dev->pkeys.virt2phys_pkey[slave][port - 1][0];
422
423 dqpn = dev->dev->caps.sqp_start + 8 * slave + port + (dest_qpt * 2) - 1;
424
425 /* get tunnel tx data buf for slave */
426 src_qp = tun_qp->qp;
427
428 /* create ah. Just need an empty one with the port num for the post send.
429 * The driver will set the force loopback bit in post_send */
430 memset(&attr, 0, sizeof attr);
431 attr.port_num = port;
432 ah = ib_create_ah(tun_ctx->pd, &attr);
433 if (IS_ERR(ah))
434 return -ENOMEM;
435
436 /* allocate tunnel tx buf after pass failure returns */
437 spin_lock(&tun_qp->tx_lock);
438 if (tun_qp->tx_ix_head - tun_qp->tx_ix_tail >=
439 (MLX4_NUM_TUNNEL_BUFS - 1))
440 ret = -EAGAIN;
441 else
442 tun_tx_ix = (++tun_qp->tx_ix_head) & (MLX4_NUM_TUNNEL_BUFS - 1);
443 spin_unlock(&tun_qp->tx_lock);
444 if (ret)
445 goto out;
446
447 tun_mad = (struct mlx4_rcv_tunnel_mad *) (tun_qp->tx_ring[tun_tx_ix].buf.addr);
448 if (tun_qp->tx_ring[tun_tx_ix].ah)
449 ib_destroy_ah(tun_qp->tx_ring[tun_tx_ix].ah);
450 tun_qp->tx_ring[tun_tx_ix].ah = ah;
451 ib_dma_sync_single_for_cpu(&dev->ib_dev,
452 tun_qp->tx_ring[tun_tx_ix].buf.map,
453 sizeof (struct mlx4_rcv_tunnel_mad),
454 DMA_TO_DEVICE);
455
456 /* copy over to tunnel buffer */
457 if (grh)
458 memcpy(&tun_mad->grh, grh, sizeof *grh);
459 memcpy(&tun_mad->mad, mad, sizeof *mad);
460
461 /* adjust tunnel data */
462 tun_mad->hdr.pkey_index = cpu_to_be16(tun_pkey_ix);
463 tun_mad->hdr.sl_vid = cpu_to_be16(((u16)(wc->sl)) << 12);
464 tun_mad->hdr.slid_mac_47_32 = cpu_to_be16(wc->slid);
465 tun_mad->hdr.flags_src_qp = cpu_to_be32(wc->src_qp & 0xFFFFFF);
466 tun_mad->hdr.g_ml_path = (grh && (wc->wc_flags & IB_WC_GRH)) ? 0x80 : 0;
467
468 ib_dma_sync_single_for_device(&dev->ib_dev,
469 tun_qp->tx_ring[tun_tx_ix].buf.map,
470 sizeof (struct mlx4_rcv_tunnel_mad),
471 DMA_TO_DEVICE);
472
473 list.addr = tun_qp->tx_ring[tun_tx_ix].buf.map;
474 list.length = sizeof (struct mlx4_rcv_tunnel_mad);
475 list.lkey = tun_ctx->mr->lkey;
476
477 wr.wr.ud.ah = ah;
478 wr.wr.ud.port_num = port;
479 wr.wr.ud.remote_qkey = IB_QP_SET_QKEY;
480 wr.wr.ud.remote_qpn = dqpn;
481 wr.next = NULL;
482 wr.wr_id = ((u64) tun_tx_ix) | MLX4_TUN_SET_WRID_QPN(dest_qpt);
483 wr.sg_list = &list;
484 wr.num_sge = 1;
485 wr.opcode = IB_WR_SEND;
486 wr.send_flags = IB_SEND_SIGNALED;
487
488 ret = ib_post_send(src_qp, &wr, &bad_wr);
489out:
490 if (ret)
491 ib_destroy_ah(ah);
492 return ret;
493}
494
495static int mlx4_ib_demux_mad(struct ib_device *ibdev, u8 port,
496 struct ib_wc *wc, struct ib_grh *grh,
497 struct ib_mad *mad)
498{
499 struct mlx4_ib_dev *dev = to_mdev(ibdev);
500 int err;
501 int slave;
502 u8 *slave_id;
503
504 /* Initially assume that this mad is for us */
505 slave = mlx4_master_func_num(dev->dev);
506
507 /* See if the slave id is encoded in a response mad */
508 if (mad->mad_hdr.method & 0x80) {
509 slave_id = (u8 *) &mad->mad_hdr.tid;
510 slave = *slave_id;
511 if (slave != 255) /*255 indicates the dom0*/
512 *slave_id = 0; /* remap tid */
513 }
514
515 /* If a grh is present, we demux according to it */
516 if (wc->wc_flags & IB_WC_GRH) {
517 slave = mlx4_ib_find_real_gid(ibdev, port, grh->dgid.global.interface_id);
518 if (slave < 0) {
519 mlx4_ib_warn(ibdev, "failed matching grh\n");
520 return -ENOENT;
521 }
522 }
523 /* Class-specific handling */
524 switch (mad->mad_hdr.mgmt_class) {
525 case IB_MGMT_CLASS_SUBN_ADM:
526 if (mlx4_ib_demux_sa_handler(ibdev, port, slave,
527 (struct ib_sa_mad *) mad))
528 return 0;
529 break;
530 case IB_MGMT_CLASS_DEVICE_MGMT:
531 if (mad->mad_hdr.method != IB_MGMT_METHOD_GET_RESP)
532 return 0;
533 break;
534 default:
535 /* Drop unsupported classes for slaves in tunnel mode */
536 if (slave != mlx4_master_func_num(dev->dev)) {
537 pr_debug("dropping unsupported ingress mad from class:%d "
538 "for slave:%d\n", mad->mad_hdr.mgmt_class, slave);
539 return 0;
540 }
541 }
542 /*make sure that no slave==255 was not handled yet.*/
543 if (slave >= dev->dev->caps.sqp_demux) {
544 mlx4_ib_warn(ibdev, "slave id: %d is bigger than allowed:%d\n",
545 slave, dev->dev->caps.sqp_demux);
546 return -ENOENT;
547 }
548
549 err = mlx4_ib_send_to_slave(dev, slave, port, wc->qp->qp_type, wc, grh, mad);
550 if (err)
551 pr_debug("failed sending to slave %d via tunnel qp (%d)\n",
552 slave, err);
553 return 0;
554}
555
Or Gerlitzc3779132011-06-15 14:51:27 +0000556static int ib_process_mad(struct ib_device *ibdev, int mad_flags, u8 port_num,
Roland Dreier225c7b12007-05-08 18:00:38 -0700557 struct ib_wc *in_wc, struct ib_grh *in_grh,
558 struct ib_mad *in_mad, struct ib_mad *out_mad)
559{
Moni Shouaf0f6f342009-01-28 14:54:35 -0800560 u16 slid, prev_lid = 0;
Roland Dreier225c7b12007-05-08 18:00:38 -0700561 int err;
Moni Shouaf0f6f342009-01-28 14:54:35 -0800562 struct ib_port_attr pattr;
Roland Dreier225c7b12007-05-08 18:00:38 -0700563
Jack Morgensteinb1d8eb52012-06-19 11:21:35 +0300564 if (in_wc && in_wc->qp->qp_num) {
565 pr_debug("received MAD: slid:%d sqpn:%d "
566 "dlid_bits:%d dqpn:%d wc_flags:0x%x, cls %x, mtd %x, atr %x\n",
567 in_wc->slid, in_wc->src_qp,
568 in_wc->dlid_path_bits,
569 in_wc->qp->qp_num,
570 in_wc->wc_flags,
571 in_mad->mad_hdr.mgmt_class, in_mad->mad_hdr.method,
572 be16_to_cpu(in_mad->mad_hdr.attr_id));
573 if (in_wc->wc_flags & IB_WC_GRH) {
574 pr_debug("sgid_hi:0x%016llx sgid_lo:0x%016llx\n",
575 be64_to_cpu(in_grh->sgid.global.subnet_prefix),
576 be64_to_cpu(in_grh->sgid.global.interface_id));
577 pr_debug("dgid_hi:0x%016llx dgid_lo:0x%016llx\n",
578 be64_to_cpu(in_grh->dgid.global.subnet_prefix),
579 be64_to_cpu(in_grh->dgid.global.interface_id));
580 }
581 }
582
Roland Dreier225c7b12007-05-08 18:00:38 -0700583 slid = in_wc ? in_wc->slid : be16_to_cpu(IB_LID_PERMISSIVE);
584
585 if (in_mad->mad_hdr.method == IB_MGMT_METHOD_TRAP && slid == 0) {
586 forward_trap(to_mdev(ibdev), port_num, in_mad);
587 return IB_MAD_RESULT_SUCCESS | IB_MAD_RESULT_CONSUMED;
588 }
589
590 if (in_mad->mad_hdr.mgmt_class == IB_MGMT_CLASS_SUBN_LID_ROUTED ||
591 in_mad->mad_hdr.mgmt_class == IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE) {
592 if (in_mad->mad_hdr.method != IB_MGMT_METHOD_GET &&
593 in_mad->mad_hdr.method != IB_MGMT_METHOD_SET &&
594 in_mad->mad_hdr.method != IB_MGMT_METHOD_TRAP_REPRESS)
595 return IB_MAD_RESULT_SUCCESS;
596
597 /*
Jack Morgensteina6f7fea2012-01-26 16:41:33 +0200598 * Don't process SMInfo queries -- the SMA can't handle them.
Roland Dreier225c7b12007-05-08 18:00:38 -0700599 */
Jack Morgensteina6f7fea2012-01-26 16:41:33 +0200600 if (in_mad->mad_hdr.attr_id == IB_SMP_ATTR_SM_INFO)
Roland Dreier225c7b12007-05-08 18:00:38 -0700601 return IB_MAD_RESULT_SUCCESS;
602 } else if (in_mad->mad_hdr.mgmt_class == IB_MGMT_CLASS_PERF_MGMT ||
603 in_mad->mad_hdr.mgmt_class == MLX4_IB_VENDOR_CLASS1 ||
Eli Cohen6578cf32008-07-14 23:48:45 -0700604 in_mad->mad_hdr.mgmt_class == MLX4_IB_VENDOR_CLASS2 ||
605 in_mad->mad_hdr.mgmt_class == IB_MGMT_CLASS_CONG_MGMT) {
Roland Dreier225c7b12007-05-08 18:00:38 -0700606 if (in_mad->mad_hdr.method != IB_MGMT_METHOD_GET &&
607 in_mad->mad_hdr.method != IB_MGMT_METHOD_SET)
608 return IB_MAD_RESULT_SUCCESS;
609 } else
610 return IB_MAD_RESULT_SUCCESS;
611
Moni Shouaf0f6f342009-01-28 14:54:35 -0800612 if ((in_mad->mad_hdr.mgmt_class == IB_MGMT_CLASS_SUBN_LID_ROUTED ||
613 in_mad->mad_hdr.mgmt_class == IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE) &&
614 in_mad->mad_hdr.method == IB_MGMT_METHOD_SET &&
615 in_mad->mad_hdr.attr_id == IB_SMP_ATTR_PORT_INFO &&
616 !ib_query_port(ibdev, port_num, &pattr))
617 prev_lid = pattr.lid;
618
Roland Dreier225c7b12007-05-08 18:00:38 -0700619 err = mlx4_MAD_IFC(to_mdev(ibdev),
Jack Morgenstein0a9a0182012-08-03 08:40:45 +0000620 (mad_flags & IB_MAD_IGNORE_MKEY ? MLX4_MAD_IFC_IGNORE_MKEY : 0) |
621 (mad_flags & IB_MAD_IGNORE_BKEY ? MLX4_MAD_IFC_IGNORE_BKEY : 0) |
622 MLX4_MAD_IFC_NET_VIEW,
Roland Dreier225c7b12007-05-08 18:00:38 -0700623 port_num, in_wc, in_grh, in_mad, out_mad);
624 if (err)
625 return IB_MAD_RESULT_FAILURE;
626
627 if (!out_mad->mad_hdr.status) {
Jack Morgenstein00f5ce92012-06-19 11:21:40 +0300628 if (!(to_mdev(ibdev)->dev->caps.flags & MLX4_DEV_CAP_FLAG_PORT_MNG_CHG_EV))
629 smp_snoop(ibdev, port_num, in_mad, prev_lid);
Roland Dreier225c7b12007-05-08 18:00:38 -0700630 node_desc_override(ibdev, out_mad);
631 }
632
633 /* set return bit in status of directed route responses */
634 if (in_mad->mad_hdr.mgmt_class == IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE)
635 out_mad->mad_hdr.status |= cpu_to_be16(1 << 15);
636
637 if (in_mad->mad_hdr.method == IB_MGMT_METHOD_TRAP_REPRESS)
638 /* no response for trap repress */
639 return IB_MAD_RESULT_SUCCESS | IB_MAD_RESULT_CONSUMED;
640
641 return IB_MAD_RESULT_SUCCESS | IB_MAD_RESULT_REPLY;
642}
643
Or Gerlitzc3779132011-06-15 14:51:27 +0000644static void edit_counter(struct mlx4_counter *cnt,
645 struct ib_pma_portcounters *pma_cnt)
646{
647 pma_cnt->port_xmit_data = cpu_to_be32((be64_to_cpu(cnt->tx_bytes)>>2));
648 pma_cnt->port_rcv_data = cpu_to_be32((be64_to_cpu(cnt->rx_bytes)>>2));
649 pma_cnt->port_xmit_packets = cpu_to_be32(be64_to_cpu(cnt->tx_frames));
650 pma_cnt->port_rcv_packets = cpu_to_be32(be64_to_cpu(cnt->rx_frames));
651}
652
653static int iboe_process_mad(struct ib_device *ibdev, int mad_flags, u8 port_num,
654 struct ib_wc *in_wc, struct ib_grh *in_grh,
655 struct ib_mad *in_mad, struct ib_mad *out_mad)
656{
657 struct mlx4_cmd_mailbox *mailbox;
658 struct mlx4_ib_dev *dev = to_mdev(ibdev);
659 int err;
660 u32 inmod = dev->counters[port_num - 1] & 0xffff;
661 u8 mode;
662
663 if (in_mad->mad_hdr.mgmt_class != IB_MGMT_CLASS_PERF_MGMT)
664 return -EINVAL;
665
666 mailbox = mlx4_alloc_cmd_mailbox(dev->dev);
667 if (IS_ERR(mailbox))
668 return IB_MAD_RESULT_FAILURE;
669
670 err = mlx4_cmd_box(dev->dev, 0, mailbox->dma, inmod, 0,
Jack Morgensteinf9baff52011-12-13 04:10:51 +0000671 MLX4_CMD_QUERY_IF_STAT, MLX4_CMD_TIME_CLASS_C,
672 MLX4_CMD_WRAPPED);
Or Gerlitzc3779132011-06-15 14:51:27 +0000673 if (err)
674 err = IB_MAD_RESULT_FAILURE;
675 else {
676 memset(out_mad->data, 0, sizeof out_mad->data);
677 mode = ((struct mlx4_counter *)mailbox->buf)->counter_mode;
678 switch (mode & 0xf) {
679 case 0:
680 edit_counter(mailbox->buf,
681 (void *)(out_mad->data + 40));
682 err = IB_MAD_RESULT_SUCCESS | IB_MAD_RESULT_REPLY;
683 break;
684 default:
685 err = IB_MAD_RESULT_FAILURE;
686 }
687 }
688
689 mlx4_free_cmd_mailbox(dev->dev, mailbox);
690
691 return err;
692}
693
694int mlx4_ib_process_mad(struct ib_device *ibdev, int mad_flags, u8 port_num,
695 struct ib_wc *in_wc, struct ib_grh *in_grh,
696 struct ib_mad *in_mad, struct ib_mad *out_mad)
697{
698 switch (rdma_port_get_link_layer(ibdev, port_num)) {
699 case IB_LINK_LAYER_INFINIBAND:
700 return ib_process_mad(ibdev, mad_flags, port_num, in_wc,
701 in_grh, in_mad, out_mad);
702 case IB_LINK_LAYER_ETHERNET:
703 return iboe_process_mad(ibdev, mad_flags, port_num, in_wc,
704 in_grh, in_mad, out_mad);
705 default:
706 return -EINVAL;
707 }
708}
709
Roland Dreier225c7b12007-05-08 18:00:38 -0700710static void send_handler(struct ib_mad_agent *agent,
711 struct ib_mad_send_wc *mad_send_wc)
712{
713 ib_free_send_mad(mad_send_wc->send_buf);
714}
715
716int mlx4_ib_mad_init(struct mlx4_ib_dev *dev)
717{
718 struct ib_mad_agent *agent;
719 int p, q;
720 int ret;
Eli Cohenfa417f72010-10-24 21:08:52 -0700721 enum rdma_link_layer ll;
Roland Dreier225c7b12007-05-08 18:00:38 -0700722
Eli Cohenfa417f72010-10-24 21:08:52 -0700723 for (p = 0; p < dev->num_ports; ++p) {
724 ll = rdma_port_get_link_layer(&dev->ib_dev, p + 1);
Roland Dreier225c7b12007-05-08 18:00:38 -0700725 for (q = 0; q <= 1; ++q) {
Eli Cohenfa417f72010-10-24 21:08:52 -0700726 if (ll == IB_LINK_LAYER_INFINIBAND) {
727 agent = ib_register_mad_agent(&dev->ib_dev, p + 1,
728 q ? IB_QPT_GSI : IB_QPT_SMI,
729 NULL, 0, send_handler,
730 NULL, NULL);
731 if (IS_ERR(agent)) {
732 ret = PTR_ERR(agent);
733 goto err;
734 }
735 dev->send_agent[p][q] = agent;
736 } else
737 dev->send_agent[p][q] = NULL;
Roland Dreier225c7b12007-05-08 18:00:38 -0700738 }
Eli Cohenfa417f72010-10-24 21:08:52 -0700739 }
Roland Dreier225c7b12007-05-08 18:00:38 -0700740
741 return 0;
742
743err:
Yevgeny Petrilin7ff93f82008-10-22 15:38:42 -0700744 for (p = 0; p < dev->num_ports; ++p)
Roland Dreier225c7b12007-05-08 18:00:38 -0700745 for (q = 0; q <= 1; ++q)
746 if (dev->send_agent[p][q])
747 ib_unregister_mad_agent(dev->send_agent[p][q]);
748
749 return ret;
750}
751
752void mlx4_ib_mad_cleanup(struct mlx4_ib_dev *dev)
753{
754 struct ib_mad_agent *agent;
755 int p, q;
756
Yevgeny Petrilin7ff93f82008-10-22 15:38:42 -0700757 for (p = 0; p < dev->num_ports; ++p) {
Roland Dreier225c7b12007-05-08 18:00:38 -0700758 for (q = 0; q <= 1; ++q) {
759 agent = dev->send_agent[p][q];
Eli Cohenfa417f72010-10-24 21:08:52 -0700760 if (agent) {
761 dev->send_agent[p][q] = NULL;
762 ib_unregister_mad_agent(agent);
763 }
Roland Dreier225c7b12007-05-08 18:00:38 -0700764 }
765
766 if (dev->sm_ah[p])
767 ib_destroy_ah(dev->sm_ah[p]);
768 }
769}
Jack Morgenstein00f5ce92012-06-19 11:21:40 +0300770
771void handle_port_mgmt_change_event(struct work_struct *work)
772{
773 struct ib_event_work *ew = container_of(work, struct ib_event_work, work);
774 struct mlx4_ib_dev *dev = ew->ib_dev;
775 struct mlx4_eqe *eqe = &(ew->ib_eqe);
776 u8 port = eqe->event.port_mgmt_change.port;
777 u32 changed_attr;
778
779 switch (eqe->subtype) {
780 case MLX4_DEV_PMC_SUBTYPE_PORT_INFO:
781 changed_attr = be32_to_cpu(eqe->event.port_mgmt_change.params.port_info.changed_attr);
782
783 /* Update the SM ah - This should be done before handling
784 the other changed attributes so that MADs can be sent to the SM */
785 if (changed_attr & MSTR_SM_CHANGE_MASK) {
786 u16 lid = be16_to_cpu(eqe->event.port_mgmt_change.params.port_info.mstr_sm_lid);
787 u8 sl = eqe->event.port_mgmt_change.params.port_info.mstr_sm_sl & 0xf;
788 update_sm_ah(dev, port, lid, sl);
789 }
790
791 /* Check if it is a lid change event */
792 if (changed_attr & MLX4_EQ_PORT_INFO_LID_CHANGE_MASK)
793 mlx4_ib_dispatch_event(dev, port, IB_EVENT_LID_CHANGE);
794
795 /* Generate GUID changed event */
796 if (changed_attr & MLX4_EQ_PORT_INFO_GID_PFX_CHANGE_MASK)
797 mlx4_ib_dispatch_event(dev, port, IB_EVENT_GID_CHANGE);
798
799 if (changed_attr & MLX4_EQ_PORT_INFO_CLIENT_REREG_MASK)
800 mlx4_ib_dispatch_event(dev, port,
801 IB_EVENT_CLIENT_REREGISTER);
802 break;
803
804 case MLX4_DEV_PMC_SUBTYPE_PKEY_TABLE:
805 mlx4_ib_dispatch_event(dev, port, IB_EVENT_PKEY_CHANGE);
806 break;
807 case MLX4_DEV_PMC_SUBTYPE_GUID_INFO:
Jack Morgenstein66349612012-06-19 11:21:44 +0300808 /* paravirtualized master's guid is guid 0 -- does not change */
809 if (!mlx4_is_master(dev->dev))
810 mlx4_ib_dispatch_event(dev, port, IB_EVENT_GID_CHANGE);
Jack Morgenstein00f5ce92012-06-19 11:21:40 +0300811 break;
812 default:
813 pr_warn("Unsupported subtype 0x%x for "
814 "Port Management Change event\n", eqe->subtype);
815 }
816
817 kfree(ew);
818}
819
820void mlx4_ib_dispatch_event(struct mlx4_ib_dev *dev, u8 port_num,
821 enum ib_event_type type)
822{
823 struct ib_event event;
824
825 event.device = &dev->ib_dev;
826 event.element.port_num = port_num;
827 event.event = type;
828
829 ib_dispatch_event(&event);
830}
Jack Morgensteinfc065732012-08-03 08:40:42 +0000831
832static void mlx4_ib_tunnel_comp_handler(struct ib_cq *cq, void *arg)
833{
834 unsigned long flags;
835 struct mlx4_ib_demux_pv_ctx *ctx = cq->cq_context;
836 struct mlx4_ib_dev *dev = to_mdev(ctx->ib_dev);
837 spin_lock_irqsave(&dev->sriov.going_down_lock, flags);
838 if (!dev->sriov.is_going_down && ctx->state == DEMUX_PV_STATE_ACTIVE)
839 queue_work(ctx->wq, &ctx->work);
840 spin_unlock_irqrestore(&dev->sriov.going_down_lock, flags);
841}
842
843static int mlx4_ib_post_pv_qp_buf(struct mlx4_ib_demux_pv_ctx *ctx,
844 struct mlx4_ib_demux_pv_qp *tun_qp,
845 int index)
846{
847 struct ib_sge sg_list;
848 struct ib_recv_wr recv_wr, *bad_recv_wr;
849 int size;
850
851 size = (tun_qp->qp->qp_type == IB_QPT_UD) ?
852 sizeof (struct mlx4_tunnel_mad) : sizeof (struct mlx4_mad_rcv_buf);
853
854 sg_list.addr = tun_qp->ring[index].map;
855 sg_list.length = size;
856 sg_list.lkey = ctx->mr->lkey;
857
858 recv_wr.next = NULL;
859 recv_wr.sg_list = &sg_list;
860 recv_wr.num_sge = 1;
861 recv_wr.wr_id = (u64) index | MLX4_TUN_WRID_RECV |
862 MLX4_TUN_SET_WRID_QPN(tun_qp->proxy_qpt);
863 ib_dma_sync_single_for_device(ctx->ib_dev, tun_qp->ring[index].map,
864 size, DMA_FROM_DEVICE);
865 return ib_post_recv(tun_qp->qp, &recv_wr, &bad_recv_wr);
866}
867
Jack Morgenstein37bfc7c2012-08-03 08:40:44 +0000868static int mlx4_ib_multiplex_sa_handler(struct ib_device *ibdev, int port,
869 int slave, struct ib_sa_mad *sa_mad)
870{
871 return 0;
872}
873
874static int is_proxy_qp0(struct mlx4_ib_dev *dev, int qpn, int slave)
875{
876 int slave_start = dev->dev->caps.sqp_start + 8 * slave;
877
878 return (qpn >= slave_start && qpn <= slave_start + 1);
879}
880
881
882int mlx4_ib_send_to_wire(struct mlx4_ib_dev *dev, int slave, u8 port,
883 enum ib_qp_type dest_qpt, u16 pkey_index, u32 remote_qpn,
884 u32 qkey, struct ib_ah_attr *attr, struct ib_mad *mad)
885{
886 struct ib_sge list;
887 struct ib_send_wr wr, *bad_wr;
888 struct mlx4_ib_demux_pv_ctx *sqp_ctx;
889 struct mlx4_ib_demux_pv_qp *sqp;
890 struct mlx4_mad_snd_buf *sqp_mad;
891 struct ib_ah *ah;
892 struct ib_qp *send_qp = NULL;
893 unsigned wire_tx_ix = 0;
894 int ret = 0;
895 u16 wire_pkey_ix;
896 int src_qpnum;
897 u8 sgid_index;
898
899
900 sqp_ctx = dev->sriov.sqps[port-1];
901
902 /* check if proxy qp created */
903 if (!sqp_ctx || sqp_ctx->state != DEMUX_PV_STATE_ACTIVE)
904 return -EAGAIN;
905
906 /* QP0 forwarding only for Dom0 */
907 if (dest_qpt == IB_QPT_SMI && (mlx4_master_func_num(dev->dev) != slave))
908 return -EINVAL;
909
910 if (dest_qpt == IB_QPT_SMI) {
911 src_qpnum = 0;
912 sqp = &sqp_ctx->qp[0];
913 wire_pkey_ix = dev->pkeys.virt2phys_pkey[slave][port - 1][0];
914 } else {
915 src_qpnum = 1;
916 sqp = &sqp_ctx->qp[1];
917 wire_pkey_ix = dev->pkeys.virt2phys_pkey[slave][port - 1][pkey_index];
918 }
919
920 send_qp = sqp->qp;
921
922 /* create ah */
923 sgid_index = attr->grh.sgid_index;
924 attr->grh.sgid_index = 0;
925 ah = ib_create_ah(sqp_ctx->pd, attr);
926 if (IS_ERR(ah))
927 return -ENOMEM;
928 attr->grh.sgid_index = sgid_index;
929 to_mah(ah)->av.ib.gid_index = sgid_index;
930 /* get rid of force-loopback bit */
931 to_mah(ah)->av.ib.port_pd &= cpu_to_be32(0x7FFFFFFF);
932 spin_lock(&sqp->tx_lock);
933 if (sqp->tx_ix_head - sqp->tx_ix_tail >=
934 (MLX4_NUM_TUNNEL_BUFS - 1))
935 ret = -EAGAIN;
936 else
937 wire_tx_ix = (++sqp->tx_ix_head) & (MLX4_NUM_TUNNEL_BUFS - 1);
938 spin_unlock(&sqp->tx_lock);
939 if (ret)
940 goto out;
941
942 sqp_mad = (struct mlx4_mad_snd_buf *) (sqp->tx_ring[wire_tx_ix].buf.addr);
943 if (sqp->tx_ring[wire_tx_ix].ah)
944 ib_destroy_ah(sqp->tx_ring[wire_tx_ix].ah);
945 sqp->tx_ring[wire_tx_ix].ah = ah;
946 ib_dma_sync_single_for_cpu(&dev->ib_dev,
947 sqp->tx_ring[wire_tx_ix].buf.map,
948 sizeof (struct mlx4_mad_snd_buf),
949 DMA_TO_DEVICE);
950
951 memcpy(&sqp_mad->payload, mad, sizeof *mad);
952
953 ib_dma_sync_single_for_device(&dev->ib_dev,
954 sqp->tx_ring[wire_tx_ix].buf.map,
955 sizeof (struct mlx4_mad_snd_buf),
956 DMA_TO_DEVICE);
957
958 list.addr = sqp->tx_ring[wire_tx_ix].buf.map;
959 list.length = sizeof (struct mlx4_mad_snd_buf);
960 list.lkey = sqp_ctx->mr->lkey;
961
962 wr.wr.ud.ah = ah;
963 wr.wr.ud.port_num = port;
964 wr.wr.ud.pkey_index = wire_pkey_ix;
965 wr.wr.ud.remote_qkey = qkey;
966 wr.wr.ud.remote_qpn = remote_qpn;
967 wr.next = NULL;
968 wr.wr_id = ((u64) wire_tx_ix) | MLX4_TUN_SET_WRID_QPN(src_qpnum);
969 wr.sg_list = &list;
970 wr.num_sge = 1;
971 wr.opcode = IB_WR_SEND;
972 wr.send_flags = IB_SEND_SIGNALED;
973
974 ret = ib_post_send(send_qp, &wr, &bad_wr);
975out:
976 if (ret)
977 ib_destroy_ah(ah);
978 return ret;
979}
980
981static void mlx4_ib_multiplex_mad(struct mlx4_ib_demux_pv_ctx *ctx, struct ib_wc *wc)
982{
983 struct mlx4_ib_dev *dev = to_mdev(ctx->ib_dev);
984 struct mlx4_ib_demux_pv_qp *tun_qp = &ctx->qp[MLX4_TUN_WRID_QPN(wc->wr_id)];
985 int wr_ix = wc->wr_id & (MLX4_NUM_TUNNEL_BUFS - 1);
986 struct mlx4_tunnel_mad *tunnel = tun_qp->ring[wr_ix].addr;
987 struct mlx4_ib_ah ah;
988 struct ib_ah_attr ah_attr;
989 u8 *slave_id;
990 int slave;
991
992 /* Get slave that sent this packet */
993 if (wc->src_qp < dev->dev->caps.sqp_start ||
994 wc->src_qp >= dev->dev->caps.base_tunnel_sqpn ||
995 (wc->src_qp & 0x1) != ctx->port - 1 ||
996 wc->src_qp & 0x4) {
997 mlx4_ib_warn(ctx->ib_dev, "can't multiplex bad sqp:%d\n", wc->src_qp);
998 return;
999 }
1000 slave = ((wc->src_qp & ~0x7) - dev->dev->caps.sqp_start) / 8;
1001 if (slave != ctx->slave) {
1002 mlx4_ib_warn(ctx->ib_dev, "can't multiplex bad sqp:%d: "
1003 "belongs to another slave\n", wc->src_qp);
1004 return;
1005 }
1006 if (slave != mlx4_master_func_num(dev->dev) && !(wc->src_qp & 0x2)) {
1007 mlx4_ib_warn(ctx->ib_dev, "can't multiplex bad sqp:%d: "
1008 "non-master trying to send QP0 packets\n", wc->src_qp);
1009 return;
1010 }
1011
1012 /* Map transaction ID */
1013 ib_dma_sync_single_for_cpu(ctx->ib_dev, tun_qp->ring[wr_ix].map,
1014 sizeof (struct mlx4_tunnel_mad),
1015 DMA_FROM_DEVICE);
1016 switch (tunnel->mad.mad_hdr.method) {
1017 case IB_MGMT_METHOD_SET:
1018 case IB_MGMT_METHOD_GET:
1019 case IB_MGMT_METHOD_REPORT:
1020 case IB_SA_METHOD_GET_TABLE:
1021 case IB_SA_METHOD_DELETE:
1022 case IB_SA_METHOD_GET_MULTI:
1023 case IB_SA_METHOD_GET_TRACE_TBL:
1024 slave_id = (u8 *) &tunnel->mad.mad_hdr.tid;
1025 if (*slave_id) {
1026 mlx4_ib_warn(ctx->ib_dev, "egress mad has non-null tid msb:%d "
1027 "class:%d slave:%d\n", *slave_id,
1028 tunnel->mad.mad_hdr.mgmt_class, slave);
1029 return;
1030 } else
1031 *slave_id = slave;
1032 default:
1033 /* nothing */;
1034 }
1035
1036 /* Class-specific handling */
1037 switch (tunnel->mad.mad_hdr.mgmt_class) {
1038 case IB_MGMT_CLASS_SUBN_ADM:
1039 if (mlx4_ib_multiplex_sa_handler(ctx->ib_dev, ctx->port, slave,
1040 (struct ib_sa_mad *) &tunnel->mad))
1041 return;
1042 break;
1043 case IB_MGMT_CLASS_DEVICE_MGMT:
1044 if (tunnel->mad.mad_hdr.method != IB_MGMT_METHOD_GET &&
1045 tunnel->mad.mad_hdr.method != IB_MGMT_METHOD_SET)
1046 return;
1047 break;
1048 default:
1049 /* Drop unsupported classes for slaves in tunnel mode */
1050 if (slave != mlx4_master_func_num(dev->dev)) {
1051 mlx4_ib_warn(ctx->ib_dev, "dropping unsupported egress mad from class:%d "
1052 "for slave:%d\n", tunnel->mad.mad_hdr.mgmt_class, slave);
1053 return;
1054 }
1055 }
1056
1057 /* We are using standard ib_core services to send the mad, so generate a
1058 * stadard address handle by decoding the tunnelled mlx4_ah fields */
1059 memcpy(&ah.av, &tunnel->hdr.av, sizeof (struct mlx4_av));
1060 ah.ibah.device = ctx->ib_dev;
1061 mlx4_ib_query_ah(&ah.ibah, &ah_attr);
1062 if ((ah_attr.ah_flags & IB_AH_GRH) &&
1063 (ah_attr.grh.sgid_index != slave)) {
1064 mlx4_ib_warn(ctx->ib_dev, "slave:%d accessed invalid sgid_index:%d\n",
1065 slave, ah_attr.grh.sgid_index);
1066 return;
1067 }
1068
1069 mlx4_ib_send_to_wire(dev, slave, ctx->port,
1070 is_proxy_qp0(dev, wc->src_qp, slave) ?
1071 IB_QPT_SMI : IB_QPT_GSI,
1072 be16_to_cpu(tunnel->hdr.pkey_index),
1073 be32_to_cpu(tunnel->hdr.remote_qpn),
1074 be32_to_cpu(tunnel->hdr.qkey),
1075 &ah_attr, &tunnel->mad);
1076}
1077
Jack Morgensteinfc065732012-08-03 08:40:42 +00001078static int mlx4_ib_alloc_pv_bufs(struct mlx4_ib_demux_pv_ctx *ctx,
1079 enum ib_qp_type qp_type, int is_tun)
1080{
1081 int i;
1082 struct mlx4_ib_demux_pv_qp *tun_qp;
1083 int rx_buf_size, tx_buf_size;
1084
1085 if (qp_type > IB_QPT_GSI)
1086 return -EINVAL;
1087
1088 tun_qp = &ctx->qp[qp_type];
1089
1090 tun_qp->ring = kzalloc(sizeof (struct mlx4_ib_buf) * MLX4_NUM_TUNNEL_BUFS,
1091 GFP_KERNEL);
1092 if (!tun_qp->ring)
1093 return -ENOMEM;
1094
1095 tun_qp->tx_ring = kcalloc(MLX4_NUM_TUNNEL_BUFS,
1096 sizeof (struct mlx4_ib_tun_tx_buf),
1097 GFP_KERNEL);
1098 if (!tun_qp->tx_ring) {
1099 kfree(tun_qp->ring);
1100 tun_qp->ring = NULL;
1101 return -ENOMEM;
1102 }
1103
1104 if (is_tun) {
1105 rx_buf_size = sizeof (struct mlx4_tunnel_mad);
1106 tx_buf_size = sizeof (struct mlx4_rcv_tunnel_mad);
1107 } else {
1108 rx_buf_size = sizeof (struct mlx4_mad_rcv_buf);
1109 tx_buf_size = sizeof (struct mlx4_mad_snd_buf);
1110 }
1111
1112 for (i = 0; i < MLX4_NUM_TUNNEL_BUFS; i++) {
1113 tun_qp->ring[i].addr = kmalloc(rx_buf_size, GFP_KERNEL);
1114 if (!tun_qp->ring[i].addr)
1115 goto err;
1116 tun_qp->ring[i].map = ib_dma_map_single(ctx->ib_dev,
1117 tun_qp->ring[i].addr,
1118 rx_buf_size,
1119 DMA_FROM_DEVICE);
1120 }
1121
1122 for (i = 0; i < MLX4_NUM_TUNNEL_BUFS; i++) {
1123 tun_qp->tx_ring[i].buf.addr =
1124 kmalloc(tx_buf_size, GFP_KERNEL);
1125 if (!tun_qp->tx_ring[i].buf.addr)
1126 goto tx_err;
1127 tun_qp->tx_ring[i].buf.map =
1128 ib_dma_map_single(ctx->ib_dev,
1129 tun_qp->tx_ring[i].buf.addr,
1130 tx_buf_size,
1131 DMA_TO_DEVICE);
1132 tun_qp->tx_ring[i].ah = NULL;
1133 }
1134 spin_lock_init(&tun_qp->tx_lock);
1135 tun_qp->tx_ix_head = 0;
1136 tun_qp->tx_ix_tail = 0;
1137 tun_qp->proxy_qpt = qp_type;
1138
1139 return 0;
1140
1141tx_err:
1142 while (i > 0) {
1143 --i;
1144 ib_dma_unmap_single(ctx->ib_dev, tun_qp->tx_ring[i].buf.map,
1145 tx_buf_size, DMA_TO_DEVICE);
1146 kfree(tun_qp->tx_ring[i].buf.addr);
1147 }
1148 kfree(tun_qp->tx_ring);
1149 tun_qp->tx_ring = NULL;
1150 i = MLX4_NUM_TUNNEL_BUFS;
1151err:
1152 while (i > 0) {
1153 --i;
1154 ib_dma_unmap_single(ctx->ib_dev, tun_qp->ring[i].map,
1155 rx_buf_size, DMA_FROM_DEVICE);
1156 kfree(tun_qp->ring[i].addr);
1157 }
1158 kfree(tun_qp->ring);
1159 tun_qp->ring = NULL;
1160 return -ENOMEM;
1161}
1162
1163static void mlx4_ib_free_pv_qp_bufs(struct mlx4_ib_demux_pv_ctx *ctx,
1164 enum ib_qp_type qp_type, int is_tun)
1165{
1166 int i;
1167 struct mlx4_ib_demux_pv_qp *tun_qp;
1168 int rx_buf_size, tx_buf_size;
1169
1170 if (qp_type > IB_QPT_GSI)
1171 return;
1172
1173 tun_qp = &ctx->qp[qp_type];
1174 if (is_tun) {
1175 rx_buf_size = sizeof (struct mlx4_tunnel_mad);
1176 tx_buf_size = sizeof (struct mlx4_rcv_tunnel_mad);
1177 } else {
1178 rx_buf_size = sizeof (struct mlx4_mad_rcv_buf);
1179 tx_buf_size = sizeof (struct mlx4_mad_snd_buf);
1180 }
1181
1182
1183 for (i = 0; i < MLX4_NUM_TUNNEL_BUFS; i++) {
1184 ib_dma_unmap_single(ctx->ib_dev, tun_qp->ring[i].map,
1185 rx_buf_size, DMA_FROM_DEVICE);
1186 kfree(tun_qp->ring[i].addr);
1187 }
1188
1189 for (i = 0; i < MLX4_NUM_TUNNEL_BUFS; i++) {
1190 ib_dma_unmap_single(ctx->ib_dev, tun_qp->tx_ring[i].buf.map,
1191 tx_buf_size, DMA_TO_DEVICE);
1192 kfree(tun_qp->tx_ring[i].buf.addr);
1193 if (tun_qp->tx_ring[i].ah)
1194 ib_destroy_ah(tun_qp->tx_ring[i].ah);
1195 }
1196 kfree(tun_qp->tx_ring);
1197 kfree(tun_qp->ring);
1198}
1199
1200static void mlx4_ib_tunnel_comp_worker(struct work_struct *work)
1201{
Jack Morgenstein37bfc7c2012-08-03 08:40:44 +00001202 struct mlx4_ib_demux_pv_ctx *ctx;
1203 struct mlx4_ib_demux_pv_qp *tun_qp;
1204 struct ib_wc wc;
1205 int ret;
1206 ctx = container_of(work, struct mlx4_ib_demux_pv_ctx, work);
1207 ib_req_notify_cq(ctx->cq, IB_CQ_NEXT_COMP);
1208
1209 while (ib_poll_cq(ctx->cq, 1, &wc) == 1) {
1210 tun_qp = &ctx->qp[MLX4_TUN_WRID_QPN(wc.wr_id)];
1211 if (wc.status == IB_WC_SUCCESS) {
1212 switch (wc.opcode) {
1213 case IB_WC_RECV:
1214 mlx4_ib_multiplex_mad(ctx, &wc);
1215 ret = mlx4_ib_post_pv_qp_buf(ctx, tun_qp,
1216 wc.wr_id &
1217 (MLX4_NUM_TUNNEL_BUFS - 1));
1218 if (ret)
1219 pr_err("Failed reposting tunnel "
1220 "buf:%lld\n", wc.wr_id);
1221 break;
1222 case IB_WC_SEND:
1223 pr_debug("received tunnel send completion:"
1224 "wrid=0x%llx, status=0x%x\n",
1225 wc.wr_id, wc.status);
1226 ib_destroy_ah(tun_qp->tx_ring[wc.wr_id &
1227 (MLX4_NUM_TUNNEL_BUFS - 1)].ah);
1228 tun_qp->tx_ring[wc.wr_id & (MLX4_NUM_TUNNEL_BUFS - 1)].ah
1229 = NULL;
1230 spin_lock(&tun_qp->tx_lock);
1231 tun_qp->tx_ix_tail++;
1232 spin_unlock(&tun_qp->tx_lock);
1233
1234 break;
1235 default:
1236 break;
1237 }
1238 } else {
1239 pr_debug("mlx4_ib: completion error in tunnel: %d."
1240 " status = %d, wrid = 0x%llx\n",
1241 ctx->slave, wc.status, wc.wr_id);
1242 if (!MLX4_TUN_IS_RECV(wc.wr_id)) {
1243 ib_destroy_ah(tun_qp->tx_ring[wc.wr_id &
1244 (MLX4_NUM_TUNNEL_BUFS - 1)].ah);
1245 tun_qp->tx_ring[wc.wr_id & (MLX4_NUM_TUNNEL_BUFS - 1)].ah
1246 = NULL;
1247 spin_lock(&tun_qp->tx_lock);
1248 tun_qp->tx_ix_tail++;
1249 spin_unlock(&tun_qp->tx_lock);
1250 }
1251 }
1252 }
Jack Morgensteinfc065732012-08-03 08:40:42 +00001253}
1254
1255static void pv_qp_event_handler(struct ib_event *event, void *qp_context)
1256{
1257 struct mlx4_ib_demux_pv_ctx *sqp = qp_context;
1258
1259 /* It's worse than that! He's dead, Jim! */
1260 pr_err("Fatal error (%d) on a MAD QP on port %d\n",
1261 event->event, sqp->port);
1262}
1263
1264static int create_pv_sqp(struct mlx4_ib_demux_pv_ctx *ctx,
1265 enum ib_qp_type qp_type, int create_tun)
1266{
1267 int i, ret;
1268 struct mlx4_ib_demux_pv_qp *tun_qp;
1269 struct mlx4_ib_qp_tunnel_init_attr qp_init_attr;
1270 struct ib_qp_attr attr;
1271 int qp_attr_mask_INIT;
1272
1273 if (qp_type > IB_QPT_GSI)
1274 return -EINVAL;
1275
1276 tun_qp = &ctx->qp[qp_type];
1277
1278 memset(&qp_init_attr, 0, sizeof qp_init_attr);
1279 qp_init_attr.init_attr.send_cq = ctx->cq;
1280 qp_init_attr.init_attr.recv_cq = ctx->cq;
1281 qp_init_attr.init_attr.sq_sig_type = IB_SIGNAL_ALL_WR;
1282 qp_init_attr.init_attr.cap.max_send_wr = MLX4_NUM_TUNNEL_BUFS;
1283 qp_init_attr.init_attr.cap.max_recv_wr = MLX4_NUM_TUNNEL_BUFS;
1284 qp_init_attr.init_attr.cap.max_send_sge = 1;
1285 qp_init_attr.init_attr.cap.max_recv_sge = 1;
1286 if (create_tun) {
1287 qp_init_attr.init_attr.qp_type = IB_QPT_UD;
1288 qp_init_attr.init_attr.create_flags = MLX4_IB_SRIOV_TUNNEL_QP;
1289 qp_init_attr.port = ctx->port;
1290 qp_init_attr.slave = ctx->slave;
1291 qp_init_attr.proxy_qp_type = qp_type;
1292 qp_attr_mask_INIT = IB_QP_STATE | IB_QP_PKEY_INDEX |
1293 IB_QP_QKEY | IB_QP_PORT;
1294 } else {
1295 qp_init_attr.init_attr.qp_type = qp_type;
1296 qp_init_attr.init_attr.create_flags = MLX4_IB_SRIOV_SQP;
1297 qp_attr_mask_INIT = IB_QP_STATE | IB_QP_PKEY_INDEX | IB_QP_QKEY;
1298 }
1299 qp_init_attr.init_attr.port_num = ctx->port;
1300 qp_init_attr.init_attr.qp_context = ctx;
1301 qp_init_attr.init_attr.event_handler = pv_qp_event_handler;
1302 tun_qp->qp = ib_create_qp(ctx->pd, &qp_init_attr.init_attr);
1303 if (IS_ERR(tun_qp->qp)) {
1304 ret = PTR_ERR(tun_qp->qp);
1305 tun_qp->qp = NULL;
1306 pr_err("Couldn't create %s QP (%d)\n",
1307 create_tun ? "tunnel" : "special", ret);
1308 return ret;
1309 }
1310
1311 memset(&attr, 0, sizeof attr);
1312 attr.qp_state = IB_QPS_INIT;
1313 attr.pkey_index =
1314 to_mdev(ctx->ib_dev)->pkeys.virt2phys_pkey[ctx->slave][ctx->port - 1][0];
1315 attr.qkey = IB_QP1_QKEY;
1316 attr.port_num = ctx->port;
1317 ret = ib_modify_qp(tun_qp->qp, &attr, qp_attr_mask_INIT);
1318 if (ret) {
1319 pr_err("Couldn't change %s qp state to INIT (%d)\n",
1320 create_tun ? "tunnel" : "special", ret);
1321 goto err_qp;
1322 }
1323 attr.qp_state = IB_QPS_RTR;
1324 ret = ib_modify_qp(tun_qp->qp, &attr, IB_QP_STATE);
1325 if (ret) {
1326 pr_err("Couldn't change %s qp state to RTR (%d)\n",
1327 create_tun ? "tunnel" : "special", ret);
1328 goto err_qp;
1329 }
1330 attr.qp_state = IB_QPS_RTS;
1331 attr.sq_psn = 0;
1332 ret = ib_modify_qp(tun_qp->qp, &attr, IB_QP_STATE | IB_QP_SQ_PSN);
1333 if (ret) {
1334 pr_err("Couldn't change %s qp state to RTS (%d)\n",
1335 create_tun ? "tunnel" : "special", ret);
1336 goto err_qp;
1337 }
1338
1339 for (i = 0; i < MLX4_NUM_TUNNEL_BUFS; i++) {
1340 ret = mlx4_ib_post_pv_qp_buf(ctx, tun_qp, i);
1341 if (ret) {
1342 pr_err(" mlx4_ib_post_pv_buf error"
1343 " (err = %d, i = %d)\n", ret, i);
1344 goto err_qp;
1345 }
1346 }
1347 return 0;
1348
1349err_qp:
1350 ib_destroy_qp(tun_qp->qp);
1351 tun_qp->qp = NULL;
1352 return ret;
1353}
1354
1355/*
1356 * IB MAD completion callback for real SQPs
1357 */
1358static void mlx4_ib_sqp_comp_worker(struct work_struct *work)
1359{
Jack Morgenstein37bfc7c2012-08-03 08:40:44 +00001360 struct mlx4_ib_demux_pv_ctx *ctx;
1361 struct mlx4_ib_demux_pv_qp *sqp;
1362 struct ib_wc wc;
1363 struct ib_grh *grh;
1364 struct ib_mad *mad;
1365
1366 ctx = container_of(work, struct mlx4_ib_demux_pv_ctx, work);
1367 ib_req_notify_cq(ctx->cq, IB_CQ_NEXT_COMP);
1368
1369 while (mlx4_ib_poll_cq(ctx->cq, 1, &wc) == 1) {
1370 sqp = &ctx->qp[MLX4_TUN_WRID_QPN(wc.wr_id)];
1371 if (wc.status == IB_WC_SUCCESS) {
1372 switch (wc.opcode) {
1373 case IB_WC_SEND:
1374 ib_destroy_ah(sqp->tx_ring[wc.wr_id &
1375 (MLX4_NUM_TUNNEL_BUFS - 1)].ah);
1376 sqp->tx_ring[wc.wr_id & (MLX4_NUM_TUNNEL_BUFS - 1)].ah
1377 = NULL;
1378 spin_lock(&sqp->tx_lock);
1379 sqp->tx_ix_tail++;
1380 spin_unlock(&sqp->tx_lock);
1381 break;
1382 case IB_WC_RECV:
1383 mad = (struct ib_mad *) &(((struct mlx4_mad_rcv_buf *)
1384 (sqp->ring[wc.wr_id &
1385 (MLX4_NUM_TUNNEL_BUFS - 1)].addr))->payload);
1386 grh = &(((struct mlx4_mad_rcv_buf *)
1387 (sqp->ring[wc.wr_id &
1388 (MLX4_NUM_TUNNEL_BUFS - 1)].addr))->grh);
1389 mlx4_ib_demux_mad(ctx->ib_dev, ctx->port, &wc, grh, mad);
1390 if (mlx4_ib_post_pv_qp_buf(ctx, sqp, wc.wr_id &
1391 (MLX4_NUM_TUNNEL_BUFS - 1)))
1392 pr_err("Failed reposting SQP "
1393 "buf:%lld\n", wc.wr_id);
1394 break;
1395 default:
1396 BUG_ON(1);
1397 break;
1398 }
1399 } else {
1400 pr_debug("mlx4_ib: completion error in tunnel: %d."
1401 " status = %d, wrid = 0x%llx\n",
1402 ctx->slave, wc.status, wc.wr_id);
1403 if (!MLX4_TUN_IS_RECV(wc.wr_id)) {
1404 ib_destroy_ah(sqp->tx_ring[wc.wr_id &
1405 (MLX4_NUM_TUNNEL_BUFS - 1)].ah);
1406 sqp->tx_ring[wc.wr_id & (MLX4_NUM_TUNNEL_BUFS - 1)].ah
1407 = NULL;
1408 spin_lock(&sqp->tx_lock);
1409 sqp->tx_ix_tail++;
1410 spin_unlock(&sqp->tx_lock);
1411 }
1412 }
1413 }
Jack Morgensteinfc065732012-08-03 08:40:42 +00001414}
1415
1416static int alloc_pv_object(struct mlx4_ib_dev *dev, int slave, int port,
1417 struct mlx4_ib_demux_pv_ctx **ret_ctx)
1418{
1419 struct mlx4_ib_demux_pv_ctx *ctx;
1420
1421 *ret_ctx = NULL;
1422 ctx = kzalloc(sizeof (struct mlx4_ib_demux_pv_ctx), GFP_KERNEL);
1423 if (!ctx) {
1424 pr_err("failed allocating pv resource context "
1425 "for port %d, slave %d\n", port, slave);
1426 return -ENOMEM;
1427 }
1428
1429 ctx->ib_dev = &dev->ib_dev;
1430 ctx->port = port;
1431 ctx->slave = slave;
1432 *ret_ctx = ctx;
1433 return 0;
1434}
1435
1436static void free_pv_object(struct mlx4_ib_dev *dev, int slave, int port)
1437{
1438 if (dev->sriov.demux[port - 1].tun[slave]) {
1439 kfree(dev->sriov.demux[port - 1].tun[slave]);
1440 dev->sriov.demux[port - 1].tun[slave] = NULL;
1441 }
1442}
1443
1444static int create_pv_resources(struct ib_device *ibdev, int slave, int port,
1445 int create_tun, struct mlx4_ib_demux_pv_ctx *ctx)
1446{
1447 int ret, cq_size;
1448
1449 ctx->state = DEMUX_PV_STATE_STARTING;
1450 /* have QP0 only on port owner, and only if link layer is IB */
1451 if (ctx->slave == mlx4_master_func_num(to_mdev(ctx->ib_dev)->dev) &&
1452 rdma_port_get_link_layer(ibdev, ctx->port) == IB_LINK_LAYER_INFINIBAND)
1453 ctx->has_smi = 1;
1454
1455 if (ctx->has_smi) {
1456 ret = mlx4_ib_alloc_pv_bufs(ctx, IB_QPT_SMI, create_tun);
1457 if (ret) {
1458 pr_err("Failed allocating qp0 tunnel bufs (%d)\n", ret);
1459 goto err_out;
1460 }
1461 }
1462
1463 ret = mlx4_ib_alloc_pv_bufs(ctx, IB_QPT_GSI, create_tun);
1464 if (ret) {
1465 pr_err("Failed allocating qp1 tunnel bufs (%d)\n", ret);
1466 goto err_out_qp0;
1467 }
1468
1469 cq_size = 2 * MLX4_NUM_TUNNEL_BUFS;
1470 if (ctx->has_smi)
1471 cq_size *= 2;
1472
1473 ctx->cq = ib_create_cq(ctx->ib_dev, mlx4_ib_tunnel_comp_handler,
1474 NULL, ctx, cq_size, 0);
1475 if (IS_ERR(ctx->cq)) {
1476 ret = PTR_ERR(ctx->cq);
1477 pr_err("Couldn't create tunnel CQ (%d)\n", ret);
1478 goto err_buf;
1479 }
1480
1481 ctx->pd = ib_alloc_pd(ctx->ib_dev);
1482 if (IS_ERR(ctx->pd)) {
1483 ret = PTR_ERR(ctx->pd);
1484 pr_err("Couldn't create tunnel PD (%d)\n", ret);
1485 goto err_cq;
1486 }
1487
1488 ctx->mr = ib_get_dma_mr(ctx->pd, IB_ACCESS_LOCAL_WRITE);
1489 if (IS_ERR(ctx->mr)) {
1490 ret = PTR_ERR(ctx->mr);
1491 pr_err("Couldn't get tunnel DMA MR (%d)\n", ret);
1492 goto err_pd;
1493 }
1494
1495 if (ctx->has_smi) {
1496 ret = create_pv_sqp(ctx, IB_QPT_SMI, create_tun);
1497 if (ret) {
1498 pr_err("Couldn't create %s QP0 (%d)\n",
1499 create_tun ? "tunnel for" : "", ret);
1500 goto err_mr;
1501 }
1502 }
1503
1504 ret = create_pv_sqp(ctx, IB_QPT_GSI, create_tun);
1505 if (ret) {
1506 pr_err("Couldn't create %s QP1 (%d)\n",
1507 create_tun ? "tunnel for" : "", ret);
1508 goto err_qp0;
1509 }
1510
1511 if (create_tun)
1512 INIT_WORK(&ctx->work, mlx4_ib_tunnel_comp_worker);
1513 else
1514 INIT_WORK(&ctx->work, mlx4_ib_sqp_comp_worker);
1515
1516 ctx->wq = to_mdev(ibdev)->sriov.demux[port - 1].wq;
1517
1518 ret = ib_req_notify_cq(ctx->cq, IB_CQ_NEXT_COMP);
1519 if (ret) {
1520 pr_err("Couldn't arm tunnel cq (%d)\n", ret);
1521 goto err_wq;
1522 }
1523 ctx->state = DEMUX_PV_STATE_ACTIVE;
1524 return 0;
1525
1526err_wq:
1527 ctx->wq = NULL;
1528 ib_destroy_qp(ctx->qp[1].qp);
1529 ctx->qp[1].qp = NULL;
1530
1531
1532err_qp0:
1533 if (ctx->has_smi)
1534 ib_destroy_qp(ctx->qp[0].qp);
1535 ctx->qp[0].qp = NULL;
1536
1537err_mr:
1538 ib_dereg_mr(ctx->mr);
1539 ctx->mr = NULL;
1540
1541err_pd:
1542 ib_dealloc_pd(ctx->pd);
1543 ctx->pd = NULL;
1544
1545err_cq:
1546 ib_destroy_cq(ctx->cq);
1547 ctx->cq = NULL;
1548
1549err_buf:
1550 mlx4_ib_free_pv_qp_bufs(ctx, IB_QPT_GSI, create_tun);
1551
1552err_out_qp0:
1553 if (ctx->has_smi)
1554 mlx4_ib_free_pv_qp_bufs(ctx, IB_QPT_SMI, create_tun);
1555err_out:
1556 ctx->state = DEMUX_PV_STATE_DOWN;
1557 return ret;
1558}
1559
1560static void destroy_pv_resources(struct mlx4_ib_dev *dev, int slave, int port,
1561 struct mlx4_ib_demux_pv_ctx *ctx, int flush)
1562{
1563 if (!ctx)
1564 return;
1565 if (ctx->state > DEMUX_PV_STATE_DOWN) {
1566 ctx->state = DEMUX_PV_STATE_DOWNING;
1567 if (flush)
1568 flush_workqueue(ctx->wq);
1569 if (ctx->has_smi) {
1570 ib_destroy_qp(ctx->qp[0].qp);
1571 ctx->qp[0].qp = NULL;
1572 mlx4_ib_free_pv_qp_bufs(ctx, IB_QPT_SMI, 1);
1573 }
1574 ib_destroy_qp(ctx->qp[1].qp);
1575 ctx->qp[1].qp = NULL;
1576 mlx4_ib_free_pv_qp_bufs(ctx, IB_QPT_GSI, 1);
1577 ib_dereg_mr(ctx->mr);
1578 ctx->mr = NULL;
1579 ib_dealloc_pd(ctx->pd);
1580 ctx->pd = NULL;
1581 ib_destroy_cq(ctx->cq);
1582 ctx->cq = NULL;
1583 ctx->state = DEMUX_PV_STATE_DOWN;
1584 }
1585}
1586
1587static int mlx4_ib_tunnels_update(struct mlx4_ib_dev *dev, int slave,
1588 int port, int do_init)
1589{
1590 int ret = 0;
1591
1592 if (!do_init) {
1593 /* for master, destroy real sqp resources */
1594 if (slave == mlx4_master_func_num(dev->dev))
1595 destroy_pv_resources(dev, slave, port,
1596 dev->sriov.sqps[port - 1], 1);
1597 /* destroy the tunnel qp resources */
1598 destroy_pv_resources(dev, slave, port,
1599 dev->sriov.demux[port - 1].tun[slave], 1);
1600 return 0;
1601 }
1602
1603 /* create the tunnel qp resources */
1604 ret = create_pv_resources(&dev->ib_dev, slave, port, 1,
1605 dev->sriov.demux[port - 1].tun[slave]);
1606
1607 /* for master, create the real sqp resources */
1608 if (!ret && slave == mlx4_master_func_num(dev->dev))
1609 ret = create_pv_resources(&dev->ib_dev, slave, port, 0,
1610 dev->sriov.sqps[port - 1]);
1611 return ret;
1612}
1613
1614void mlx4_ib_tunnels_update_work(struct work_struct *work)
1615{
1616 struct mlx4_ib_demux_work *dmxw;
1617
1618 dmxw = container_of(work, struct mlx4_ib_demux_work, work);
1619 mlx4_ib_tunnels_update(dmxw->dev, dmxw->slave, (int) dmxw->port,
1620 dmxw->do_init);
1621 kfree(dmxw);
1622 return;
1623}
1624
1625static int mlx4_ib_alloc_demux_ctx(struct mlx4_ib_dev *dev,
1626 struct mlx4_ib_demux_ctx *ctx,
1627 int port)
1628{
1629 char name[12];
1630 int ret = 0;
1631 int i;
1632
1633 ctx->tun = kcalloc(dev->dev->caps.sqp_demux,
1634 sizeof (struct mlx4_ib_demux_pv_ctx *), GFP_KERNEL);
1635 if (!ctx->tun)
1636 return -ENOMEM;
1637
1638 ctx->dev = dev;
1639 ctx->port = port;
1640 ctx->ib_dev = &dev->ib_dev;
1641
1642 for (i = 0; i < dev->dev->caps.sqp_demux; i++) {
1643 ret = alloc_pv_object(dev, i, port, &ctx->tun[i]);
1644 if (ret) {
1645 ret = -ENOMEM;
1646 goto err_wq;
1647 }
1648 }
1649
1650 snprintf(name, sizeof name, "mlx4_ibt%d", port);
1651 ctx->wq = create_singlethread_workqueue(name);
1652 if (!ctx->wq) {
1653 pr_err("Failed to create tunnelling WQ for port %d\n", port);
1654 ret = -ENOMEM;
1655 goto err_wq;
1656 }
1657
1658 snprintf(name, sizeof name, "mlx4_ibud%d", port);
1659 ctx->ud_wq = create_singlethread_workqueue(name);
1660 if (!ctx->ud_wq) {
1661 pr_err("Failed to create up/down WQ for port %d\n", port);
1662 ret = -ENOMEM;
1663 goto err_udwq;
1664 }
1665
1666 return 0;
1667
1668err_udwq:
1669 destroy_workqueue(ctx->wq);
1670 ctx->wq = NULL;
1671
1672err_wq:
1673 for (i = 0; i < dev->dev->caps.sqp_demux; i++)
1674 free_pv_object(dev, i, port);
1675 kfree(ctx->tun);
1676 ctx->tun = NULL;
1677 return ret;
1678}
1679
1680static void mlx4_ib_free_sqp_ctx(struct mlx4_ib_demux_pv_ctx *sqp_ctx)
1681{
1682 if (sqp_ctx->state > DEMUX_PV_STATE_DOWN) {
1683 sqp_ctx->state = DEMUX_PV_STATE_DOWNING;
1684 flush_workqueue(sqp_ctx->wq);
1685 if (sqp_ctx->has_smi) {
1686 ib_destroy_qp(sqp_ctx->qp[0].qp);
1687 sqp_ctx->qp[0].qp = NULL;
1688 mlx4_ib_free_pv_qp_bufs(sqp_ctx, IB_QPT_SMI, 0);
1689 }
1690 ib_destroy_qp(sqp_ctx->qp[1].qp);
1691 sqp_ctx->qp[1].qp = NULL;
1692 mlx4_ib_free_pv_qp_bufs(sqp_ctx, IB_QPT_GSI, 0);
1693 ib_dereg_mr(sqp_ctx->mr);
1694 sqp_ctx->mr = NULL;
1695 ib_dealloc_pd(sqp_ctx->pd);
1696 sqp_ctx->pd = NULL;
1697 ib_destroy_cq(sqp_ctx->cq);
1698 sqp_ctx->cq = NULL;
1699 sqp_ctx->state = DEMUX_PV_STATE_DOWN;
1700 }
1701}
1702
1703static void mlx4_ib_free_demux_ctx(struct mlx4_ib_demux_ctx *ctx)
1704{
1705 int i;
1706 if (ctx) {
1707 struct mlx4_ib_dev *dev = to_mdev(ctx->ib_dev);
1708 for (i = 0; i < dev->dev->caps.sqp_demux; i++) {
1709 if (!ctx->tun[i])
1710 continue;
1711 if (ctx->tun[i]->state > DEMUX_PV_STATE_DOWN)
1712 ctx->tun[i]->state = DEMUX_PV_STATE_DOWNING;
1713 }
1714 flush_workqueue(ctx->wq);
1715 for (i = 0; i < dev->dev->caps.sqp_demux; i++) {
1716 destroy_pv_resources(dev, i, ctx->port, ctx->tun[i], 0);
1717 free_pv_object(dev, i, ctx->port);
1718 }
1719 kfree(ctx->tun);
1720 destroy_workqueue(ctx->ud_wq);
1721 destroy_workqueue(ctx->wq);
1722 }
1723}
1724
1725static void mlx4_ib_master_tunnels(struct mlx4_ib_dev *dev, int do_init)
1726{
1727 int i;
1728
1729 if (!mlx4_is_master(dev->dev))
1730 return;
1731 /* initialize or tear down tunnel QPs for the master */
1732 for (i = 0; i < dev->dev->caps.num_ports; i++)
1733 mlx4_ib_tunnels_update(dev, mlx4_master_func_num(dev->dev), i + 1, do_init);
1734 return;
1735}
1736
1737int mlx4_ib_init_sriov(struct mlx4_ib_dev *dev)
1738{
1739 int i = 0;
1740 int err;
1741
1742 if (!mlx4_is_mfunc(dev->dev))
1743 return 0;
1744
1745 dev->sriov.is_going_down = 0;
1746 spin_lock_init(&dev->sriov.going_down_lock);
1747
1748 mlx4_ib_warn(&dev->ib_dev, "multi-function enabled\n");
1749
1750 if (mlx4_is_slave(dev->dev)) {
1751 mlx4_ib_warn(&dev->ib_dev, "operating in qp1 tunnel mode\n");
1752 return 0;
1753 }
1754
1755 mlx4_ib_warn(&dev->ib_dev, "initializing demux service for %d qp1 clients\n",
1756 dev->dev->caps.sqp_demux);
1757 for (i = 0; i < dev->num_ports; i++) {
1758 err = alloc_pv_object(dev, mlx4_master_func_num(dev->dev), i + 1,
1759 &dev->sriov.sqps[i]);
1760 if (err)
1761 goto demux_err;
1762 err = mlx4_ib_alloc_demux_ctx(dev, &dev->sriov.demux[i], i + 1);
1763 if (err)
1764 goto demux_err;
1765 }
1766 mlx4_ib_master_tunnels(dev, 1);
1767 return 0;
1768
1769demux_err:
1770 while (i > 0) {
1771 free_pv_object(dev, mlx4_master_func_num(dev->dev), i + 1);
1772 mlx4_ib_free_demux_ctx(&dev->sriov.demux[i]);
1773 --i;
1774 }
1775
1776 return err;
1777}
1778
1779void mlx4_ib_close_sriov(struct mlx4_ib_dev *dev)
1780{
1781 int i;
1782 unsigned long flags;
1783
1784 if (!mlx4_is_mfunc(dev->dev))
1785 return;
1786
1787 spin_lock_irqsave(&dev->sriov.going_down_lock, flags);
1788 dev->sriov.is_going_down = 1;
1789 spin_unlock_irqrestore(&dev->sriov.going_down_lock, flags);
1790 if (mlx4_is_master(dev->dev))
1791 for (i = 0; i < dev->num_ports; i++) {
1792 flush_workqueue(dev->sriov.demux[i].ud_wq);
1793 mlx4_ib_free_sqp_ctx(dev->sriov.sqps[i]);
1794 kfree(dev->sriov.sqps[i]);
1795 dev->sriov.sqps[i] = NULL;
1796 mlx4_ib_free_demux_ctx(&dev->sriov.demux[i]);
1797 }
1798}