xref: /openbmc/linux/drivers/infiniband/hw/mlx4/mad.c (revision 3cf69cc8dbebf15b99deb342ea422105ae9c2774)
1225c7b1fSRoland Dreier /*
2225c7b1fSRoland Dreier  * Copyright (c) 2007 Cisco Systems, Inc. All rights reserved.
3225c7b1fSRoland Dreier  *
4225c7b1fSRoland Dreier  * This software is available to you under a choice of one of two
5225c7b1fSRoland Dreier  * licenses.  You may choose to be licensed under the terms of the GNU
6225c7b1fSRoland Dreier  * General Public License (GPL) Version 2, available from the file
7225c7b1fSRoland Dreier  * COPYING in the main directory of this source tree, or the
8225c7b1fSRoland Dreier  * OpenIB.org BSD license below:
9225c7b1fSRoland Dreier  *
10225c7b1fSRoland Dreier  *     Redistribution and use in source and binary forms, with or
11225c7b1fSRoland Dreier  *     without modification, are permitted provided that the following
12225c7b1fSRoland Dreier  *     conditions are met:
13225c7b1fSRoland Dreier  *
14225c7b1fSRoland Dreier  *      - Redistributions of source code must retain the above
15225c7b1fSRoland Dreier  *        copyright notice, this list of conditions and the following
16225c7b1fSRoland Dreier  *        disclaimer.
17225c7b1fSRoland Dreier  *
18225c7b1fSRoland Dreier  *      - Redistributions in binary form must reproduce the above
19225c7b1fSRoland Dreier  *        copyright notice, this list of conditions and the following
20225c7b1fSRoland Dreier  *        disclaimer in the documentation and/or other materials
21225c7b1fSRoland Dreier  *        provided with the distribution.
22225c7b1fSRoland Dreier  *
23225c7b1fSRoland Dreier  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24225c7b1fSRoland Dreier  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25225c7b1fSRoland Dreier  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26225c7b1fSRoland Dreier  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
27225c7b1fSRoland Dreier  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
28225c7b1fSRoland Dreier  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
29225c7b1fSRoland Dreier  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
30225c7b1fSRoland Dreier  * SOFTWARE.
31225c7b1fSRoland Dreier  */
32225c7b1fSRoland Dreier 
33225c7b1fSRoland Dreier #include <rdma/ib_mad.h>
34225c7b1fSRoland Dreier #include <rdma/ib_smi.h>
3537bfc7c1SJack Morgenstein #include <rdma/ib_sa.h>
3637bfc7c1SJack Morgenstein #include <rdma/ib_cache.h>
37225c7b1fSRoland Dreier 
38225c7b1fSRoland Dreier #include <linux/mlx4/cmd.h>
395a0e3ad6STejun Heo #include <linux/gfp.h>
40c3779134SOr Gerlitz #include <rdma/ib_pma.h>
41225c7b1fSRoland Dreier 
42225c7b1fSRoland Dreier #include "mlx4_ib.h"
43225c7b1fSRoland Dreier 
44225c7b1fSRoland Dreier enum {
45225c7b1fSRoland Dreier 	MLX4_IB_VENDOR_CLASS1 = 0x9,
46225c7b1fSRoland Dreier 	MLX4_IB_VENDOR_CLASS2 = 0xa
47225c7b1fSRoland Dreier };
48225c7b1fSRoland Dreier 
49fc06573dSJack Morgenstein #define MLX4_TUN_SEND_WRID_SHIFT 34
50fc06573dSJack Morgenstein #define MLX4_TUN_QPN_SHIFT 32
51fc06573dSJack Morgenstein #define MLX4_TUN_WRID_RECV (((u64) 1) << MLX4_TUN_SEND_WRID_SHIFT)
52fc06573dSJack Morgenstein #define MLX4_TUN_SET_WRID_QPN(a) (((u64) ((a) & 0x3)) << MLX4_TUN_QPN_SHIFT)
53fc06573dSJack Morgenstein 
54fc06573dSJack Morgenstein #define MLX4_TUN_IS_RECV(a)  (((a) >>  MLX4_TUN_SEND_WRID_SHIFT) & 0x1)
55fc06573dSJack Morgenstein #define MLX4_TUN_WRID_QPN(a) (((a) >> MLX4_TUN_QPN_SHIFT) & 0x3)
56fc06573dSJack Morgenstein 
57fc06573dSJack Morgenstein struct mlx4_mad_rcv_buf {
58fc06573dSJack Morgenstein 	struct ib_grh grh;
59fc06573dSJack Morgenstein 	u8 payload[256];
60fc06573dSJack Morgenstein } __packed;
61fc06573dSJack Morgenstein 
62fc06573dSJack Morgenstein struct mlx4_mad_snd_buf {
63fc06573dSJack Morgenstein 	u8 payload[256];
64fc06573dSJack Morgenstein } __packed;
65fc06573dSJack Morgenstein 
66fc06573dSJack Morgenstein struct mlx4_tunnel_mad {
67fc06573dSJack Morgenstein 	struct ib_grh grh;
68fc06573dSJack Morgenstein 	struct mlx4_ib_tunnel_header hdr;
69fc06573dSJack Morgenstein 	struct ib_mad mad;
70fc06573dSJack Morgenstein } __packed;
71fc06573dSJack Morgenstein 
72fc06573dSJack Morgenstein struct mlx4_rcv_tunnel_mad {
73fc06573dSJack Morgenstein 	struct mlx4_rcv_tunnel_hdr hdr;
74fc06573dSJack Morgenstein 	struct ib_grh grh;
75fc06573dSJack Morgenstein 	struct ib_mad mad;
76fc06573dSJack Morgenstein } __packed;
77fc06573dSJack Morgenstein 
78b9c5d6a6SOren Duer static void handle_client_rereg_event(struct mlx4_ib_dev *dev, u8 port_num);
79b9c5d6a6SOren Duer 
80b9c5d6a6SOren Duer __be64 mlx4_ib_get_new_demux_tid(struct mlx4_ib_demux_ctx *ctx)
81b9c5d6a6SOren Duer {
82b9c5d6a6SOren Duer 	return cpu_to_be64(atomic_inc_return(&ctx->tid)) |
83b9c5d6a6SOren Duer 		cpu_to_be64(0xff00000000000000LL);
84b9c5d6a6SOren Duer }
85b9c5d6a6SOren Duer 
860a9a0188SJack Morgenstein int mlx4_MAD_IFC(struct mlx4_ib_dev *dev, int mad_ifc_flags,
87225c7b1fSRoland Dreier 		 int port, struct ib_wc *in_wc, struct ib_grh *in_grh,
88225c7b1fSRoland Dreier 		 void *in_mad, void *response_mad)
89225c7b1fSRoland Dreier {
90225c7b1fSRoland Dreier 	struct mlx4_cmd_mailbox *inmailbox, *outmailbox;
91225c7b1fSRoland Dreier 	void *inbox;
92225c7b1fSRoland Dreier 	int err;
93225c7b1fSRoland Dreier 	u32 in_modifier = port;
94225c7b1fSRoland Dreier 	u8 op_modifier = 0;
95225c7b1fSRoland Dreier 
96225c7b1fSRoland Dreier 	inmailbox = mlx4_alloc_cmd_mailbox(dev->dev);
97225c7b1fSRoland Dreier 	if (IS_ERR(inmailbox))
98225c7b1fSRoland Dreier 		return PTR_ERR(inmailbox);
99225c7b1fSRoland Dreier 	inbox = inmailbox->buf;
100225c7b1fSRoland Dreier 
101225c7b1fSRoland Dreier 	outmailbox = mlx4_alloc_cmd_mailbox(dev->dev);
102225c7b1fSRoland Dreier 	if (IS_ERR(outmailbox)) {
103225c7b1fSRoland Dreier 		mlx4_free_cmd_mailbox(dev->dev, inmailbox);
104225c7b1fSRoland Dreier 		return PTR_ERR(outmailbox);
105225c7b1fSRoland Dreier 	}
106225c7b1fSRoland Dreier 
107225c7b1fSRoland Dreier 	memcpy(inbox, in_mad, 256);
108225c7b1fSRoland Dreier 
109225c7b1fSRoland Dreier 	/*
110225c7b1fSRoland Dreier 	 * Key check traps can't be generated unless we have in_wc to
111225c7b1fSRoland Dreier 	 * tell us where to send the trap.
112225c7b1fSRoland Dreier 	 */
1130a9a0188SJack Morgenstein 	if ((mad_ifc_flags & MLX4_MAD_IFC_IGNORE_MKEY) || !in_wc)
114225c7b1fSRoland Dreier 		op_modifier |= 0x1;
1150a9a0188SJack Morgenstein 	if ((mad_ifc_flags & MLX4_MAD_IFC_IGNORE_BKEY) || !in_wc)
116225c7b1fSRoland Dreier 		op_modifier |= 0x2;
1170a9a0188SJack Morgenstein 	if (mlx4_is_mfunc(dev->dev) &&
1180a9a0188SJack Morgenstein 	    (mad_ifc_flags & MLX4_MAD_IFC_NET_VIEW || in_wc))
1190a9a0188SJack Morgenstein 		op_modifier |= 0x8;
120225c7b1fSRoland Dreier 
121225c7b1fSRoland Dreier 	if (in_wc) {
122225c7b1fSRoland Dreier 		struct {
123225c7b1fSRoland Dreier 			__be32		my_qpn;
124225c7b1fSRoland Dreier 			u32		reserved1;
125225c7b1fSRoland Dreier 			__be32		rqpn;
126225c7b1fSRoland Dreier 			u8		sl;
127225c7b1fSRoland Dreier 			u8		g_path;
128225c7b1fSRoland Dreier 			u16		reserved2[2];
129225c7b1fSRoland Dreier 			__be16		pkey;
130225c7b1fSRoland Dreier 			u32		reserved3[11];
131225c7b1fSRoland Dreier 			u8		grh[40];
132225c7b1fSRoland Dreier 		} *ext_info;
133225c7b1fSRoland Dreier 
134225c7b1fSRoland Dreier 		memset(inbox + 256, 0, 256);
135225c7b1fSRoland Dreier 		ext_info = inbox + 256;
136225c7b1fSRoland Dreier 
137225c7b1fSRoland Dreier 		ext_info->my_qpn = cpu_to_be32(in_wc->qp->qp_num);
138225c7b1fSRoland Dreier 		ext_info->rqpn   = cpu_to_be32(in_wc->src_qp);
139225c7b1fSRoland Dreier 		ext_info->sl     = in_wc->sl << 4;
140225c7b1fSRoland Dreier 		ext_info->g_path = in_wc->dlid_path_bits |
141225c7b1fSRoland Dreier 			(in_wc->wc_flags & IB_WC_GRH ? 0x80 : 0);
142225c7b1fSRoland Dreier 		ext_info->pkey   = cpu_to_be16(in_wc->pkey_index);
143225c7b1fSRoland Dreier 
144225c7b1fSRoland Dreier 		if (in_grh)
145225c7b1fSRoland Dreier 			memcpy(ext_info->grh, in_grh, 40);
146225c7b1fSRoland Dreier 
147225c7b1fSRoland Dreier 		op_modifier |= 0x4;
148225c7b1fSRoland Dreier 
149225c7b1fSRoland Dreier 		in_modifier |= in_wc->slid << 16;
150225c7b1fSRoland Dreier 	}
151225c7b1fSRoland Dreier 
1520a9a0188SJack Morgenstein 	err = mlx4_cmd_box(dev->dev, inmailbox->dma, outmailbox->dma, in_modifier,
1530a9a0188SJack Morgenstein 			   mlx4_is_master(dev->dev) ? (op_modifier & ~0x8) : op_modifier,
154f9baff50SJack Morgenstein 			   MLX4_CMD_MAD_IFC, MLX4_CMD_TIME_CLASS_C,
1550a9a0188SJack Morgenstein 			   (op_modifier & 0x8) ? MLX4_CMD_NATIVE : MLX4_CMD_WRAPPED);
156225c7b1fSRoland Dreier 
157fe11cb6bSIlpo Järvinen 	if (!err)
158225c7b1fSRoland Dreier 		memcpy(response_mad, outmailbox->buf, 256);
159225c7b1fSRoland Dreier 
160225c7b1fSRoland Dreier 	mlx4_free_cmd_mailbox(dev->dev, inmailbox);
161225c7b1fSRoland Dreier 	mlx4_free_cmd_mailbox(dev->dev, outmailbox);
162225c7b1fSRoland Dreier 
163225c7b1fSRoland Dreier 	return err;
164225c7b1fSRoland Dreier }
165225c7b1fSRoland Dreier 
166225c7b1fSRoland Dreier static void update_sm_ah(struct mlx4_ib_dev *dev, u8 port_num, u16 lid, u8 sl)
167225c7b1fSRoland Dreier {
168225c7b1fSRoland Dreier 	struct ib_ah *new_ah;
169225c7b1fSRoland Dreier 	struct ib_ah_attr ah_attr;
170df7fba66SJack Morgenstein 	unsigned long flags;
171225c7b1fSRoland Dreier 
172225c7b1fSRoland Dreier 	if (!dev->send_agent[port_num - 1][0])
173225c7b1fSRoland Dreier 		return;
174225c7b1fSRoland Dreier 
175225c7b1fSRoland Dreier 	memset(&ah_attr, 0, sizeof ah_attr);
176225c7b1fSRoland Dreier 	ah_attr.dlid     = lid;
177225c7b1fSRoland Dreier 	ah_attr.sl       = sl;
178225c7b1fSRoland Dreier 	ah_attr.port_num = port_num;
179225c7b1fSRoland Dreier 
180225c7b1fSRoland Dreier 	new_ah = ib_create_ah(dev->send_agent[port_num - 1][0]->qp->pd,
181225c7b1fSRoland Dreier 			      &ah_attr);
182225c7b1fSRoland Dreier 	if (IS_ERR(new_ah))
183225c7b1fSRoland Dreier 		return;
184225c7b1fSRoland Dreier 
185df7fba66SJack Morgenstein 	spin_lock_irqsave(&dev->sm_lock, flags);
186225c7b1fSRoland Dreier 	if (dev->sm_ah[port_num - 1])
187225c7b1fSRoland Dreier 		ib_destroy_ah(dev->sm_ah[port_num - 1]);
188225c7b1fSRoland Dreier 	dev->sm_ah[port_num - 1] = new_ah;
189df7fba66SJack Morgenstein 	spin_unlock_irqrestore(&dev->sm_lock, flags);
190225c7b1fSRoland Dreier }
191225c7b1fSRoland Dreier 
192225c7b1fSRoland Dreier /*
19300f5ce99SJack Morgenstein  * Snoop SM MADs for port info, GUID info, and  P_Key table sets, so we can
19400f5ce99SJack Morgenstein  * synthesize LID change, Client-Rereg, GID change, and P_Key change events.
195225c7b1fSRoland Dreier  */
196f0f6f346SMoni Shoua static void smp_snoop(struct ib_device *ibdev, u8 port_num, struct ib_mad *mad,
197f0f6f346SMoni Shoua 		      u16 prev_lid)
198225c7b1fSRoland Dreier {
19900f5ce99SJack Morgenstein 	struct ib_port_info *pinfo;
20000f5ce99SJack Morgenstein 	u16 lid;
20154679e14SJack Morgenstein 	__be16 *base;
20254679e14SJack Morgenstein 	u32 bn, pkey_change_bitmap;
20354679e14SJack Morgenstein 	int i;
20454679e14SJack Morgenstein 
205225c7b1fSRoland Dreier 
20600f5ce99SJack Morgenstein 	struct mlx4_ib_dev *dev = to_mdev(ibdev);
207225c7b1fSRoland Dreier 	if ((mad->mad_hdr.mgmt_class == IB_MGMT_CLASS_SUBN_LID_ROUTED ||
208225c7b1fSRoland Dreier 	     mad->mad_hdr.mgmt_class == IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE) &&
20900f5ce99SJack Morgenstein 	    mad->mad_hdr.method == IB_MGMT_METHOD_SET)
21000f5ce99SJack Morgenstein 		switch (mad->mad_hdr.attr_id) {
21100f5ce99SJack Morgenstein 		case IB_SMP_ATTR_PORT_INFO:
21200f5ce99SJack Morgenstein 			pinfo = (struct ib_port_info *) ((struct ib_smp *) mad)->data;
21300f5ce99SJack Morgenstein 			lid = be16_to_cpu(pinfo->lid);
214225c7b1fSRoland Dreier 
21500f5ce99SJack Morgenstein 			update_sm_ah(dev, port_num,
216225c7b1fSRoland Dreier 				     be16_to_cpu(pinfo->sm_lid),
217225c7b1fSRoland Dreier 				     pinfo->neighbormtu_mastersmsl & 0xf);
218225c7b1fSRoland Dreier 
21900f5ce99SJack Morgenstein 			if (pinfo->clientrereg_resv_subnetto & 0x80)
220b9c5d6a6SOren Duer 				handle_client_rereg_event(dev, port_num);
221225c7b1fSRoland Dreier 
22200f5ce99SJack Morgenstein 			if (prev_lid != lid)
22300f5ce99SJack Morgenstein 				mlx4_ib_dispatch_event(dev, port_num,
22400f5ce99SJack Morgenstein 						       IB_EVENT_LID_CHANGE);
22500f5ce99SJack Morgenstein 			break;
226225c7b1fSRoland Dreier 
22700f5ce99SJack Morgenstein 		case IB_SMP_ATTR_PKEY_TABLE:
22854679e14SJack Morgenstein 			if (!mlx4_is_mfunc(dev->dev)) {
22900f5ce99SJack Morgenstein 				mlx4_ib_dispatch_event(dev, port_num,
23000f5ce99SJack Morgenstein 						       IB_EVENT_PKEY_CHANGE);
23100f5ce99SJack Morgenstein 				break;
23254679e14SJack Morgenstein 			}
23354679e14SJack Morgenstein 
23454679e14SJack Morgenstein 			bn  = be32_to_cpu(((struct ib_smp *)mad)->attr_mod) & 0xFFFF;
23554679e14SJack Morgenstein 			base = (__be16 *) &(((struct ib_smp *)mad)->data[0]);
23654679e14SJack Morgenstein 			pkey_change_bitmap = 0;
23754679e14SJack Morgenstein 			for (i = 0; i < 32; i++) {
23854679e14SJack Morgenstein 				pr_debug("PKEY[%d] = x%x\n",
23954679e14SJack Morgenstein 					 i + bn*32, be16_to_cpu(base[i]));
24054679e14SJack Morgenstein 				if (be16_to_cpu(base[i]) !=
24154679e14SJack Morgenstein 				    dev->pkeys.phys_pkey_cache[port_num - 1][i + bn*32]) {
24254679e14SJack Morgenstein 					pkey_change_bitmap |= (1 << i);
24354679e14SJack Morgenstein 					dev->pkeys.phys_pkey_cache[port_num - 1][i + bn*32] =
24454679e14SJack Morgenstein 						be16_to_cpu(base[i]);
24554679e14SJack Morgenstein 				}
24654679e14SJack Morgenstein 			}
24754679e14SJack Morgenstein 			pr_debug("PKEY Change event: port=%d, "
24854679e14SJack Morgenstein 				 "block=0x%x, change_bitmap=0x%x\n",
24954679e14SJack Morgenstein 				 port_num, bn, pkey_change_bitmap);
25054679e14SJack Morgenstein 
25154679e14SJack Morgenstein 			if (pkey_change_bitmap)
25254679e14SJack Morgenstein 				mlx4_ib_dispatch_event(dev, port_num,
25354679e14SJack Morgenstein 						       IB_EVENT_PKEY_CHANGE);
25454679e14SJack Morgenstein 
25554679e14SJack Morgenstein 			break;
256f0f6f346SMoni Shoua 
25700f5ce99SJack Morgenstein 		case IB_SMP_ATTR_GUID_INFO:
2586634961cSJack Morgenstein 			/* paravirtualized master's guid is guid 0 -- does not change */
2596634961cSJack Morgenstein 			if (!mlx4_is_master(dev->dev))
26000f5ce99SJack Morgenstein 				mlx4_ib_dispatch_event(dev, port_num,
26100f5ce99SJack Morgenstein 						       IB_EVENT_GID_CHANGE);
26200f5ce99SJack Morgenstein 			break;
26300f5ce99SJack Morgenstein 		default:
26400f5ce99SJack Morgenstein 			break;
265225c7b1fSRoland Dreier 		}
266225c7b1fSRoland Dreier }
267225c7b1fSRoland Dreier 
268225c7b1fSRoland Dreier static void node_desc_override(struct ib_device *dev,
269225c7b1fSRoland Dreier 			       struct ib_mad *mad)
270225c7b1fSRoland Dreier {
271df7fba66SJack Morgenstein 	unsigned long flags;
272df7fba66SJack Morgenstein 
273225c7b1fSRoland Dreier 	if ((mad->mad_hdr.mgmt_class == IB_MGMT_CLASS_SUBN_LID_ROUTED ||
274225c7b1fSRoland Dreier 	     mad->mad_hdr.mgmt_class == IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE) &&
275225c7b1fSRoland Dreier 	    mad->mad_hdr.method == IB_MGMT_METHOD_GET_RESP &&
276225c7b1fSRoland Dreier 	    mad->mad_hdr.attr_id == IB_SMP_ATTR_NODE_DESC) {
277df7fba66SJack Morgenstein 		spin_lock_irqsave(&to_mdev(dev)->sm_lock, flags);
278225c7b1fSRoland Dreier 		memcpy(((struct ib_smp *) mad)->data, dev->node_desc, 64);
279df7fba66SJack Morgenstein 		spin_unlock_irqrestore(&to_mdev(dev)->sm_lock, flags);
280225c7b1fSRoland Dreier 	}
281225c7b1fSRoland Dreier }
282225c7b1fSRoland Dreier 
283225c7b1fSRoland Dreier static void forward_trap(struct mlx4_ib_dev *dev, u8 port_num, struct ib_mad *mad)
284225c7b1fSRoland Dreier {
285225c7b1fSRoland Dreier 	int qpn = mad->mad_hdr.mgmt_class != IB_MGMT_CLASS_SUBN_LID_ROUTED;
286225c7b1fSRoland Dreier 	struct ib_mad_send_buf *send_buf;
287225c7b1fSRoland Dreier 	struct ib_mad_agent *agent = dev->send_agent[port_num - 1][qpn];
288225c7b1fSRoland Dreier 	int ret;
289df7fba66SJack Morgenstein 	unsigned long flags;
290225c7b1fSRoland Dreier 
291225c7b1fSRoland Dreier 	if (agent) {
292225c7b1fSRoland Dreier 		send_buf = ib_create_send_mad(agent, qpn, 0, 0, IB_MGMT_MAD_HDR,
293225c7b1fSRoland Dreier 					      IB_MGMT_MAD_DATA, GFP_ATOMIC);
29413974909SDan Carpenter 		if (IS_ERR(send_buf))
29513974909SDan Carpenter 			return;
296225c7b1fSRoland Dreier 		/*
297225c7b1fSRoland Dreier 		 * We rely here on the fact that MLX QPs don't use the
298225c7b1fSRoland Dreier 		 * address handle after the send is posted (this is
299225c7b1fSRoland Dreier 		 * wrong following the IB spec strictly, but we know
300225c7b1fSRoland Dreier 		 * it's OK for our devices).
301225c7b1fSRoland Dreier 		 */
302df7fba66SJack Morgenstein 		spin_lock_irqsave(&dev->sm_lock, flags);
303225c7b1fSRoland Dreier 		memcpy(send_buf->mad, mad, sizeof *mad);
304225c7b1fSRoland Dreier 		if ((send_buf->ah = dev->sm_ah[port_num - 1]))
305225c7b1fSRoland Dreier 			ret = ib_post_send_mad(send_buf, NULL);
306225c7b1fSRoland Dreier 		else
307225c7b1fSRoland Dreier 			ret = -EINVAL;
308df7fba66SJack Morgenstein 		spin_unlock_irqrestore(&dev->sm_lock, flags);
309225c7b1fSRoland Dreier 
310225c7b1fSRoland Dreier 		if (ret)
311225c7b1fSRoland Dreier 			ib_free_send_mad(send_buf);
312225c7b1fSRoland Dreier 	}
313225c7b1fSRoland Dreier }
314225c7b1fSRoland Dreier 
31537bfc7c1SJack Morgenstein static int mlx4_ib_demux_sa_handler(struct ib_device *ibdev, int port, int slave,
31637bfc7c1SJack Morgenstein 							     struct ib_sa_mad *sa_mad)
31737bfc7c1SJack Morgenstein {
318b9c5d6a6SOren Duer 	int ret = 0;
319b9c5d6a6SOren Duer 
320b9c5d6a6SOren Duer 	/* dispatch to different sa handlers */
321b9c5d6a6SOren Duer 	switch (be16_to_cpu(sa_mad->mad_hdr.attr_id)) {
322b9c5d6a6SOren Duer 	case IB_SA_ATTR_MC_MEMBER_REC:
323b9c5d6a6SOren Duer 		ret = mlx4_ib_mcg_demux_handler(ibdev, port, slave, sa_mad);
324b9c5d6a6SOren Duer 		break;
325b9c5d6a6SOren Duer 	default:
326b9c5d6a6SOren Duer 		break;
327b9c5d6a6SOren Duer 	}
328b9c5d6a6SOren Duer 	return ret;
32937bfc7c1SJack Morgenstein }
33037bfc7c1SJack Morgenstein 
33137bfc7c1SJack Morgenstein int mlx4_ib_find_real_gid(struct ib_device *ibdev, u8 port, __be64 guid)
33237bfc7c1SJack Morgenstein {
33337bfc7c1SJack Morgenstein 	struct mlx4_ib_dev *dev = to_mdev(ibdev);
33437bfc7c1SJack Morgenstein 	int i;
33537bfc7c1SJack Morgenstein 
33637bfc7c1SJack Morgenstein 	for (i = 0; i < dev->dev->caps.sqp_demux; i++) {
33737bfc7c1SJack Morgenstein 		if (dev->sriov.demux[port - 1].guid_cache[i] == guid)
33837bfc7c1SJack Morgenstein 			return i;
33937bfc7c1SJack Morgenstein 	}
34037bfc7c1SJack Morgenstein 	return -1;
34137bfc7c1SJack Morgenstein }
34237bfc7c1SJack Morgenstein 
34337bfc7c1SJack Morgenstein 
34437bfc7c1SJack Morgenstein static int get_pkey_phys_indices(struct mlx4_ib_dev *ibdev, u8 port, u8 ph_pkey_ix,
34537bfc7c1SJack Morgenstein 				 u8 *full_pk_ix, u8 *partial_pk_ix,
34637bfc7c1SJack Morgenstein 				 int *is_full_member)
34737bfc7c1SJack Morgenstein {
34837bfc7c1SJack Morgenstein 	u16 search_pkey;
34937bfc7c1SJack Morgenstein 	int fm;
35037bfc7c1SJack Morgenstein 	int err = 0;
35137bfc7c1SJack Morgenstein 	u16 pk;
35237bfc7c1SJack Morgenstein 
35337bfc7c1SJack Morgenstein 	err = ib_get_cached_pkey(&ibdev->ib_dev, port, ph_pkey_ix, &search_pkey);
35437bfc7c1SJack Morgenstein 	if (err)
35537bfc7c1SJack Morgenstein 		return err;
35637bfc7c1SJack Morgenstein 
35737bfc7c1SJack Morgenstein 	fm = (search_pkey & 0x8000) ? 1 : 0;
35837bfc7c1SJack Morgenstein 	if (fm) {
35937bfc7c1SJack Morgenstein 		*full_pk_ix = ph_pkey_ix;
36037bfc7c1SJack Morgenstein 		search_pkey &= 0x7FFF;
36137bfc7c1SJack Morgenstein 	} else {
36237bfc7c1SJack Morgenstein 		*partial_pk_ix = ph_pkey_ix;
36337bfc7c1SJack Morgenstein 		search_pkey |= 0x8000;
36437bfc7c1SJack Morgenstein 	}
36537bfc7c1SJack Morgenstein 
36637bfc7c1SJack Morgenstein 	if (ib_find_exact_cached_pkey(&ibdev->ib_dev, port, search_pkey, &pk))
36737bfc7c1SJack Morgenstein 		pk = 0xFFFF;
36837bfc7c1SJack Morgenstein 
36937bfc7c1SJack Morgenstein 	if (fm)
37037bfc7c1SJack Morgenstein 		*partial_pk_ix = (pk & 0xFF);
37137bfc7c1SJack Morgenstein 	else
37237bfc7c1SJack Morgenstein 		*full_pk_ix = (pk & 0xFF);
37337bfc7c1SJack Morgenstein 
37437bfc7c1SJack Morgenstein 	*is_full_member = fm;
37537bfc7c1SJack Morgenstein 	return err;
37637bfc7c1SJack Morgenstein }
37737bfc7c1SJack Morgenstein 
37837bfc7c1SJack Morgenstein int mlx4_ib_send_to_slave(struct mlx4_ib_dev *dev, int slave, u8 port,
37937bfc7c1SJack Morgenstein 			  enum ib_qp_type dest_qpt, struct ib_wc *wc,
38037bfc7c1SJack Morgenstein 			  struct ib_grh *grh, struct ib_mad *mad)
38137bfc7c1SJack Morgenstein {
38237bfc7c1SJack Morgenstein 	struct ib_sge list;
38337bfc7c1SJack Morgenstein 	struct ib_send_wr wr, *bad_wr;
38437bfc7c1SJack Morgenstein 	struct mlx4_ib_demux_pv_ctx *tun_ctx;
38537bfc7c1SJack Morgenstein 	struct mlx4_ib_demux_pv_qp *tun_qp;
38637bfc7c1SJack Morgenstein 	struct mlx4_rcv_tunnel_mad *tun_mad;
38737bfc7c1SJack Morgenstein 	struct ib_ah_attr attr;
38837bfc7c1SJack Morgenstein 	struct ib_ah *ah;
38937bfc7c1SJack Morgenstein 	struct ib_qp *src_qp = NULL;
39037bfc7c1SJack Morgenstein 	unsigned tun_tx_ix = 0;
39137bfc7c1SJack Morgenstein 	int dqpn;
39237bfc7c1SJack Morgenstein 	int ret = 0;
39337bfc7c1SJack Morgenstein 	int i;
39437bfc7c1SJack Morgenstein 	int is_full_member = 0;
39537bfc7c1SJack Morgenstein 	u16 tun_pkey_ix;
39637bfc7c1SJack Morgenstein 	u8 ph_pkey_ix, full_pk_ix = 0, partial_pk_ix = 0;
39737bfc7c1SJack Morgenstein 
39837bfc7c1SJack Morgenstein 	if (dest_qpt > IB_QPT_GSI)
39937bfc7c1SJack Morgenstein 		return -EINVAL;
40037bfc7c1SJack Morgenstein 
40137bfc7c1SJack Morgenstein 	tun_ctx = dev->sriov.demux[port-1].tun[slave];
40237bfc7c1SJack Morgenstein 
40337bfc7c1SJack Morgenstein 	/* check if proxy qp created */
40437bfc7c1SJack Morgenstein 	if (!tun_ctx || tun_ctx->state != DEMUX_PV_STATE_ACTIVE)
40537bfc7c1SJack Morgenstein 		return -EAGAIN;
40637bfc7c1SJack Morgenstein 
40737bfc7c1SJack Morgenstein 	/* QP0 forwarding only for Dom0 */
40837bfc7c1SJack Morgenstein 	if (!dest_qpt && (mlx4_master_func_num(dev->dev) != slave))
40937bfc7c1SJack Morgenstein 		return -EINVAL;
41037bfc7c1SJack Morgenstein 
41137bfc7c1SJack Morgenstein 	if (!dest_qpt)
41237bfc7c1SJack Morgenstein 		tun_qp = &tun_ctx->qp[0];
41337bfc7c1SJack Morgenstein 	else
41437bfc7c1SJack Morgenstein 		tun_qp = &tun_ctx->qp[1];
41537bfc7c1SJack Morgenstein 
41637bfc7c1SJack Morgenstein 	/* compute pkey index for slave */
41737bfc7c1SJack Morgenstein 	/* get physical pkey -- virtualized Dom0 pkey to phys*/
41837bfc7c1SJack Morgenstein 	if (dest_qpt) {
41937bfc7c1SJack Morgenstein 		ph_pkey_ix =
42037bfc7c1SJack Morgenstein 			dev->pkeys.virt2phys_pkey[mlx4_master_func_num(dev->dev)][port - 1][wc->pkey_index];
42137bfc7c1SJack Morgenstein 
42237bfc7c1SJack Morgenstein 		/* now, translate this to the slave pkey index */
42337bfc7c1SJack Morgenstein 		ret = get_pkey_phys_indices(dev, port, ph_pkey_ix, &full_pk_ix,
42437bfc7c1SJack Morgenstein 					    &partial_pk_ix, &is_full_member);
42537bfc7c1SJack Morgenstein 		if (ret)
42637bfc7c1SJack Morgenstein 			return -EINVAL;
42737bfc7c1SJack Morgenstein 
42837bfc7c1SJack Morgenstein 		for (i = 0; i < dev->dev->caps.pkey_table_len[port]; i++) {
42937bfc7c1SJack Morgenstein 			if ((dev->pkeys.virt2phys_pkey[slave][port - 1][i] == full_pk_ix) ||
43037bfc7c1SJack Morgenstein 			    (is_full_member &&
43137bfc7c1SJack Morgenstein 			     (dev->pkeys.virt2phys_pkey[slave][port - 1][i] == partial_pk_ix)))
43237bfc7c1SJack Morgenstein 				break;
43337bfc7c1SJack Morgenstein 		}
43437bfc7c1SJack Morgenstein 		if (i == dev->dev->caps.pkey_table_len[port])
43537bfc7c1SJack Morgenstein 			return -EINVAL;
43637bfc7c1SJack Morgenstein 		tun_pkey_ix = i;
43737bfc7c1SJack Morgenstein 	} else
43837bfc7c1SJack Morgenstein 		tun_pkey_ix = dev->pkeys.virt2phys_pkey[slave][port - 1][0];
43937bfc7c1SJack Morgenstein 
44037bfc7c1SJack Morgenstein 	dqpn = dev->dev->caps.sqp_start + 8 * slave + port + (dest_qpt * 2) - 1;
44137bfc7c1SJack Morgenstein 
44237bfc7c1SJack Morgenstein 	/* get tunnel tx data buf for slave */
44337bfc7c1SJack Morgenstein 	src_qp = tun_qp->qp;
44437bfc7c1SJack Morgenstein 
44537bfc7c1SJack Morgenstein 	/* create ah. Just need an empty one with the port num for the post send.
44637bfc7c1SJack Morgenstein 	 * The driver will set the force loopback bit in post_send */
44737bfc7c1SJack Morgenstein 	memset(&attr, 0, sizeof attr);
44837bfc7c1SJack Morgenstein 	attr.port_num = port;
44937bfc7c1SJack Morgenstein 	ah = ib_create_ah(tun_ctx->pd, &attr);
45037bfc7c1SJack Morgenstein 	if (IS_ERR(ah))
45137bfc7c1SJack Morgenstein 		return -ENOMEM;
45237bfc7c1SJack Morgenstein 
45337bfc7c1SJack Morgenstein 	/* allocate tunnel tx buf after pass failure returns */
45437bfc7c1SJack Morgenstein 	spin_lock(&tun_qp->tx_lock);
45537bfc7c1SJack Morgenstein 	if (tun_qp->tx_ix_head - tun_qp->tx_ix_tail >=
45637bfc7c1SJack Morgenstein 	    (MLX4_NUM_TUNNEL_BUFS - 1))
45737bfc7c1SJack Morgenstein 		ret = -EAGAIN;
45837bfc7c1SJack Morgenstein 	else
45937bfc7c1SJack Morgenstein 		tun_tx_ix = (++tun_qp->tx_ix_head) & (MLX4_NUM_TUNNEL_BUFS - 1);
46037bfc7c1SJack Morgenstein 	spin_unlock(&tun_qp->tx_lock);
46137bfc7c1SJack Morgenstein 	if (ret)
46237bfc7c1SJack Morgenstein 		goto out;
46337bfc7c1SJack Morgenstein 
46437bfc7c1SJack Morgenstein 	tun_mad = (struct mlx4_rcv_tunnel_mad *) (tun_qp->tx_ring[tun_tx_ix].buf.addr);
46537bfc7c1SJack Morgenstein 	if (tun_qp->tx_ring[tun_tx_ix].ah)
46637bfc7c1SJack Morgenstein 		ib_destroy_ah(tun_qp->tx_ring[tun_tx_ix].ah);
46737bfc7c1SJack Morgenstein 	tun_qp->tx_ring[tun_tx_ix].ah = ah;
46837bfc7c1SJack Morgenstein 	ib_dma_sync_single_for_cpu(&dev->ib_dev,
46937bfc7c1SJack Morgenstein 				   tun_qp->tx_ring[tun_tx_ix].buf.map,
47037bfc7c1SJack Morgenstein 				   sizeof (struct mlx4_rcv_tunnel_mad),
47137bfc7c1SJack Morgenstein 				   DMA_TO_DEVICE);
47237bfc7c1SJack Morgenstein 
47337bfc7c1SJack Morgenstein 	/* copy over to tunnel buffer */
47437bfc7c1SJack Morgenstein 	if (grh)
47537bfc7c1SJack Morgenstein 		memcpy(&tun_mad->grh, grh, sizeof *grh);
47637bfc7c1SJack Morgenstein 	memcpy(&tun_mad->mad, mad, sizeof *mad);
47737bfc7c1SJack Morgenstein 
47837bfc7c1SJack Morgenstein 	/* adjust tunnel data */
47937bfc7c1SJack Morgenstein 	tun_mad->hdr.pkey_index = cpu_to_be16(tun_pkey_ix);
48037bfc7c1SJack Morgenstein 	tun_mad->hdr.sl_vid = cpu_to_be16(((u16)(wc->sl)) << 12);
48137bfc7c1SJack Morgenstein 	tun_mad->hdr.slid_mac_47_32 = cpu_to_be16(wc->slid);
48237bfc7c1SJack Morgenstein 	tun_mad->hdr.flags_src_qp = cpu_to_be32(wc->src_qp & 0xFFFFFF);
48337bfc7c1SJack Morgenstein 	tun_mad->hdr.g_ml_path = (grh && (wc->wc_flags & IB_WC_GRH)) ? 0x80 : 0;
48437bfc7c1SJack Morgenstein 
48537bfc7c1SJack Morgenstein 	ib_dma_sync_single_for_device(&dev->ib_dev,
48637bfc7c1SJack Morgenstein 				      tun_qp->tx_ring[tun_tx_ix].buf.map,
48737bfc7c1SJack Morgenstein 				      sizeof (struct mlx4_rcv_tunnel_mad),
48837bfc7c1SJack Morgenstein 				      DMA_TO_DEVICE);
48937bfc7c1SJack Morgenstein 
49037bfc7c1SJack Morgenstein 	list.addr = tun_qp->tx_ring[tun_tx_ix].buf.map;
49137bfc7c1SJack Morgenstein 	list.length = sizeof (struct mlx4_rcv_tunnel_mad);
49237bfc7c1SJack Morgenstein 	list.lkey = tun_ctx->mr->lkey;
49337bfc7c1SJack Morgenstein 
49437bfc7c1SJack Morgenstein 	wr.wr.ud.ah = ah;
49537bfc7c1SJack Morgenstein 	wr.wr.ud.port_num = port;
49637bfc7c1SJack Morgenstein 	wr.wr.ud.remote_qkey = IB_QP_SET_QKEY;
49737bfc7c1SJack Morgenstein 	wr.wr.ud.remote_qpn = dqpn;
49837bfc7c1SJack Morgenstein 	wr.next = NULL;
49937bfc7c1SJack Morgenstein 	wr.wr_id = ((u64) tun_tx_ix) | MLX4_TUN_SET_WRID_QPN(dest_qpt);
50037bfc7c1SJack Morgenstein 	wr.sg_list = &list;
50137bfc7c1SJack Morgenstein 	wr.num_sge = 1;
50237bfc7c1SJack Morgenstein 	wr.opcode = IB_WR_SEND;
50337bfc7c1SJack Morgenstein 	wr.send_flags = IB_SEND_SIGNALED;
50437bfc7c1SJack Morgenstein 
50537bfc7c1SJack Morgenstein 	ret = ib_post_send(src_qp, &wr, &bad_wr);
50637bfc7c1SJack Morgenstein out:
50737bfc7c1SJack Morgenstein 	if (ret)
50837bfc7c1SJack Morgenstein 		ib_destroy_ah(ah);
50937bfc7c1SJack Morgenstein 	return ret;
51037bfc7c1SJack Morgenstein }
51137bfc7c1SJack Morgenstein 
51237bfc7c1SJack Morgenstein static int mlx4_ib_demux_mad(struct ib_device *ibdev, u8 port,
51337bfc7c1SJack Morgenstein 			struct ib_wc *wc, struct ib_grh *grh,
51437bfc7c1SJack Morgenstein 			struct ib_mad *mad)
51537bfc7c1SJack Morgenstein {
51637bfc7c1SJack Morgenstein 	struct mlx4_ib_dev *dev = to_mdev(ibdev);
51737bfc7c1SJack Morgenstein 	int err;
51837bfc7c1SJack Morgenstein 	int slave;
51937bfc7c1SJack Morgenstein 	u8 *slave_id;
52037bfc7c1SJack Morgenstein 
52137bfc7c1SJack Morgenstein 	/* Initially assume that this mad is for us */
52237bfc7c1SJack Morgenstein 	slave = mlx4_master_func_num(dev->dev);
52337bfc7c1SJack Morgenstein 
52437bfc7c1SJack Morgenstein 	/* See if the slave id is encoded in a response mad */
52537bfc7c1SJack Morgenstein 	if (mad->mad_hdr.method & 0x80) {
52637bfc7c1SJack Morgenstein 		slave_id = (u8 *) &mad->mad_hdr.tid;
52737bfc7c1SJack Morgenstein 		slave = *slave_id;
52837bfc7c1SJack Morgenstein 		if (slave != 255) /*255 indicates the dom0*/
52937bfc7c1SJack Morgenstein 			*slave_id = 0; /* remap tid */
53037bfc7c1SJack Morgenstein 	}
53137bfc7c1SJack Morgenstein 
53237bfc7c1SJack Morgenstein 	/* If a grh is present, we demux according to it */
53337bfc7c1SJack Morgenstein 	if (wc->wc_flags & IB_WC_GRH) {
53437bfc7c1SJack Morgenstein 		slave = mlx4_ib_find_real_gid(ibdev, port, grh->dgid.global.interface_id);
53537bfc7c1SJack Morgenstein 		if (slave < 0) {
53637bfc7c1SJack Morgenstein 			mlx4_ib_warn(ibdev, "failed matching grh\n");
53737bfc7c1SJack Morgenstein 			return -ENOENT;
53837bfc7c1SJack Morgenstein 		}
53937bfc7c1SJack Morgenstein 	}
54037bfc7c1SJack Morgenstein 	/* Class-specific handling */
54137bfc7c1SJack Morgenstein 	switch (mad->mad_hdr.mgmt_class) {
54237bfc7c1SJack Morgenstein 	case IB_MGMT_CLASS_SUBN_ADM:
54337bfc7c1SJack Morgenstein 		if (mlx4_ib_demux_sa_handler(ibdev, port, slave,
54437bfc7c1SJack Morgenstein 					     (struct ib_sa_mad *) mad))
54537bfc7c1SJack Morgenstein 			return 0;
54637bfc7c1SJack Morgenstein 		break;
547*3cf69cc8SAmir Vadai 	case IB_MGMT_CLASS_CM:
548*3cf69cc8SAmir Vadai 		if (mlx4_ib_demux_cm_handler(ibdev, port, &slave, mad))
549*3cf69cc8SAmir Vadai 			return 0;
550*3cf69cc8SAmir Vadai 		break;
55137bfc7c1SJack Morgenstein 	case IB_MGMT_CLASS_DEVICE_MGMT:
55237bfc7c1SJack Morgenstein 		if (mad->mad_hdr.method != IB_MGMT_METHOD_GET_RESP)
55337bfc7c1SJack Morgenstein 			return 0;
55437bfc7c1SJack Morgenstein 		break;
55537bfc7c1SJack Morgenstein 	default:
55637bfc7c1SJack Morgenstein 		/* Drop unsupported classes for slaves in tunnel mode */
55737bfc7c1SJack Morgenstein 		if (slave != mlx4_master_func_num(dev->dev)) {
55837bfc7c1SJack Morgenstein 			pr_debug("dropping unsupported ingress mad from class:%d "
55937bfc7c1SJack Morgenstein 				 "for slave:%d\n", mad->mad_hdr.mgmt_class, slave);
56037bfc7c1SJack Morgenstein 			return 0;
56137bfc7c1SJack Morgenstein 		}
56237bfc7c1SJack Morgenstein 	}
56337bfc7c1SJack Morgenstein 	/*make sure that no slave==255 was not handled yet.*/
56437bfc7c1SJack Morgenstein 	if (slave >= dev->dev->caps.sqp_demux) {
56537bfc7c1SJack Morgenstein 		mlx4_ib_warn(ibdev, "slave id: %d is bigger than allowed:%d\n",
56637bfc7c1SJack Morgenstein 			     slave, dev->dev->caps.sqp_demux);
56737bfc7c1SJack Morgenstein 		return -ENOENT;
56837bfc7c1SJack Morgenstein 	}
56937bfc7c1SJack Morgenstein 
57037bfc7c1SJack Morgenstein 	err = mlx4_ib_send_to_slave(dev, slave, port, wc->qp->qp_type, wc, grh, mad);
57137bfc7c1SJack Morgenstein 	if (err)
57237bfc7c1SJack Morgenstein 		pr_debug("failed sending to slave %d via tunnel qp (%d)\n",
57337bfc7c1SJack Morgenstein 			 slave, err);
57437bfc7c1SJack Morgenstein 	return 0;
57537bfc7c1SJack Morgenstein }
57637bfc7c1SJack Morgenstein 
577c3779134SOr Gerlitz static int ib_process_mad(struct ib_device *ibdev, int mad_flags, u8 port_num,
578225c7b1fSRoland Dreier 			struct ib_wc *in_wc, struct ib_grh *in_grh,
579225c7b1fSRoland Dreier 			struct ib_mad *in_mad, struct ib_mad *out_mad)
580225c7b1fSRoland Dreier {
581f0f6f346SMoni Shoua 	u16 slid, prev_lid = 0;
582225c7b1fSRoland Dreier 	int err;
583f0f6f346SMoni Shoua 	struct ib_port_attr pattr;
584225c7b1fSRoland Dreier 
585b1d8eb5aSJack Morgenstein 	if (in_wc && in_wc->qp->qp_num) {
586b1d8eb5aSJack Morgenstein 		pr_debug("received MAD: slid:%d sqpn:%d "
587b1d8eb5aSJack Morgenstein 			"dlid_bits:%d dqpn:%d wc_flags:0x%x, cls %x, mtd %x, atr %x\n",
588b1d8eb5aSJack Morgenstein 			in_wc->slid, in_wc->src_qp,
589b1d8eb5aSJack Morgenstein 			in_wc->dlid_path_bits,
590b1d8eb5aSJack Morgenstein 			in_wc->qp->qp_num,
591b1d8eb5aSJack Morgenstein 			in_wc->wc_flags,
592b1d8eb5aSJack Morgenstein 			in_mad->mad_hdr.mgmt_class, in_mad->mad_hdr.method,
593b1d8eb5aSJack Morgenstein 			be16_to_cpu(in_mad->mad_hdr.attr_id));
594b1d8eb5aSJack Morgenstein 		if (in_wc->wc_flags & IB_WC_GRH) {
595b1d8eb5aSJack Morgenstein 			pr_debug("sgid_hi:0x%016llx sgid_lo:0x%016llx\n",
596b1d8eb5aSJack Morgenstein 				 be64_to_cpu(in_grh->sgid.global.subnet_prefix),
597b1d8eb5aSJack Morgenstein 				 be64_to_cpu(in_grh->sgid.global.interface_id));
598b1d8eb5aSJack Morgenstein 			pr_debug("dgid_hi:0x%016llx dgid_lo:0x%016llx\n",
599b1d8eb5aSJack Morgenstein 				 be64_to_cpu(in_grh->dgid.global.subnet_prefix),
600b1d8eb5aSJack Morgenstein 				 be64_to_cpu(in_grh->dgid.global.interface_id));
601b1d8eb5aSJack Morgenstein 		}
602b1d8eb5aSJack Morgenstein 	}
603b1d8eb5aSJack Morgenstein 
604225c7b1fSRoland Dreier 	slid = in_wc ? in_wc->slid : be16_to_cpu(IB_LID_PERMISSIVE);
605225c7b1fSRoland Dreier 
606225c7b1fSRoland Dreier 	if (in_mad->mad_hdr.method == IB_MGMT_METHOD_TRAP && slid == 0) {
607225c7b1fSRoland Dreier 		forward_trap(to_mdev(ibdev), port_num, in_mad);
608225c7b1fSRoland Dreier 		return IB_MAD_RESULT_SUCCESS | IB_MAD_RESULT_CONSUMED;
609225c7b1fSRoland Dreier 	}
610225c7b1fSRoland Dreier 
611225c7b1fSRoland Dreier 	if (in_mad->mad_hdr.mgmt_class == IB_MGMT_CLASS_SUBN_LID_ROUTED ||
612225c7b1fSRoland Dreier 	    in_mad->mad_hdr.mgmt_class == IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE) {
613225c7b1fSRoland Dreier 		if (in_mad->mad_hdr.method   != IB_MGMT_METHOD_GET &&
614225c7b1fSRoland Dreier 		    in_mad->mad_hdr.method   != IB_MGMT_METHOD_SET &&
615225c7b1fSRoland Dreier 		    in_mad->mad_hdr.method   != IB_MGMT_METHOD_TRAP_REPRESS)
616225c7b1fSRoland Dreier 			return IB_MAD_RESULT_SUCCESS;
617225c7b1fSRoland Dreier 
618225c7b1fSRoland Dreier 		/*
619a6f7feaeSJack Morgenstein 		 * Don't process SMInfo queries -- the SMA can't handle them.
620225c7b1fSRoland Dreier 		 */
621a6f7feaeSJack Morgenstein 		if (in_mad->mad_hdr.attr_id == IB_SMP_ATTR_SM_INFO)
622225c7b1fSRoland Dreier 			return IB_MAD_RESULT_SUCCESS;
623225c7b1fSRoland Dreier 	} else if (in_mad->mad_hdr.mgmt_class == IB_MGMT_CLASS_PERF_MGMT ||
624225c7b1fSRoland Dreier 		   in_mad->mad_hdr.mgmt_class == MLX4_IB_VENDOR_CLASS1   ||
6256578cf33SEli Cohen 		   in_mad->mad_hdr.mgmt_class == MLX4_IB_VENDOR_CLASS2   ||
6266578cf33SEli Cohen 		   in_mad->mad_hdr.mgmt_class == IB_MGMT_CLASS_CONG_MGMT) {
627225c7b1fSRoland Dreier 		if (in_mad->mad_hdr.method  != IB_MGMT_METHOD_GET &&
628225c7b1fSRoland Dreier 		    in_mad->mad_hdr.method  != IB_MGMT_METHOD_SET)
629225c7b1fSRoland Dreier 			return IB_MAD_RESULT_SUCCESS;
630225c7b1fSRoland Dreier 	} else
631225c7b1fSRoland Dreier 		return IB_MAD_RESULT_SUCCESS;
632225c7b1fSRoland Dreier 
633f0f6f346SMoni Shoua 	if ((in_mad->mad_hdr.mgmt_class == IB_MGMT_CLASS_SUBN_LID_ROUTED ||
634f0f6f346SMoni Shoua 	     in_mad->mad_hdr.mgmt_class == IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE) &&
635f0f6f346SMoni Shoua 	    in_mad->mad_hdr.method == IB_MGMT_METHOD_SET &&
636f0f6f346SMoni Shoua 	    in_mad->mad_hdr.attr_id == IB_SMP_ATTR_PORT_INFO &&
637f0f6f346SMoni Shoua 	    !ib_query_port(ibdev, port_num, &pattr))
638f0f6f346SMoni Shoua 		prev_lid = pattr.lid;
639f0f6f346SMoni Shoua 
640225c7b1fSRoland Dreier 	err = mlx4_MAD_IFC(to_mdev(ibdev),
6410a9a0188SJack Morgenstein 			   (mad_flags & IB_MAD_IGNORE_MKEY ? MLX4_MAD_IFC_IGNORE_MKEY : 0) |
6420a9a0188SJack Morgenstein 			   (mad_flags & IB_MAD_IGNORE_BKEY ? MLX4_MAD_IFC_IGNORE_BKEY : 0) |
6430a9a0188SJack Morgenstein 			   MLX4_MAD_IFC_NET_VIEW,
644225c7b1fSRoland Dreier 			   port_num, in_wc, in_grh, in_mad, out_mad);
645225c7b1fSRoland Dreier 	if (err)
646225c7b1fSRoland Dreier 		return IB_MAD_RESULT_FAILURE;
647225c7b1fSRoland Dreier 
648225c7b1fSRoland Dreier 	if (!out_mad->mad_hdr.status) {
64900f5ce99SJack Morgenstein 		if (!(to_mdev(ibdev)->dev->caps.flags & MLX4_DEV_CAP_FLAG_PORT_MNG_CHG_EV))
650f0f6f346SMoni Shoua 			smp_snoop(ibdev, port_num, in_mad, prev_lid);
651225c7b1fSRoland Dreier 		node_desc_override(ibdev, out_mad);
652225c7b1fSRoland Dreier 	}
653225c7b1fSRoland Dreier 
654225c7b1fSRoland Dreier 	/* set return bit in status of directed route responses */
655225c7b1fSRoland Dreier 	if (in_mad->mad_hdr.mgmt_class == IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE)
656225c7b1fSRoland Dreier 		out_mad->mad_hdr.status |= cpu_to_be16(1 << 15);
657225c7b1fSRoland Dreier 
658225c7b1fSRoland Dreier 	if (in_mad->mad_hdr.method == IB_MGMT_METHOD_TRAP_REPRESS)
659225c7b1fSRoland Dreier 		/* no response for trap repress */
660225c7b1fSRoland Dreier 		return IB_MAD_RESULT_SUCCESS | IB_MAD_RESULT_CONSUMED;
661225c7b1fSRoland Dreier 
662225c7b1fSRoland Dreier 	return IB_MAD_RESULT_SUCCESS | IB_MAD_RESULT_REPLY;
663225c7b1fSRoland Dreier }
664225c7b1fSRoland Dreier 
665c3779134SOr Gerlitz static void edit_counter(struct mlx4_counter *cnt,
666c3779134SOr Gerlitz 					struct ib_pma_portcounters *pma_cnt)
667c3779134SOr Gerlitz {
668c3779134SOr Gerlitz 	pma_cnt->port_xmit_data = cpu_to_be32((be64_to_cpu(cnt->tx_bytes)>>2));
669c3779134SOr Gerlitz 	pma_cnt->port_rcv_data  = cpu_to_be32((be64_to_cpu(cnt->rx_bytes)>>2));
670c3779134SOr Gerlitz 	pma_cnt->port_xmit_packets = cpu_to_be32(be64_to_cpu(cnt->tx_frames));
671c3779134SOr Gerlitz 	pma_cnt->port_rcv_packets  = cpu_to_be32(be64_to_cpu(cnt->rx_frames));
672c3779134SOr Gerlitz }
673c3779134SOr Gerlitz 
674c3779134SOr Gerlitz static int iboe_process_mad(struct ib_device *ibdev, int mad_flags, u8 port_num,
675c3779134SOr Gerlitz 			struct ib_wc *in_wc, struct ib_grh *in_grh,
676c3779134SOr Gerlitz 			struct ib_mad *in_mad, struct ib_mad *out_mad)
677c3779134SOr Gerlitz {
678c3779134SOr Gerlitz 	struct mlx4_cmd_mailbox *mailbox;
679c3779134SOr Gerlitz 	struct mlx4_ib_dev *dev = to_mdev(ibdev);
680c3779134SOr Gerlitz 	int err;
681c3779134SOr Gerlitz 	u32 inmod = dev->counters[port_num - 1] & 0xffff;
682c3779134SOr Gerlitz 	u8 mode;
683c3779134SOr Gerlitz 
684c3779134SOr Gerlitz 	if (in_mad->mad_hdr.mgmt_class != IB_MGMT_CLASS_PERF_MGMT)
685c3779134SOr Gerlitz 		return -EINVAL;
686c3779134SOr Gerlitz 
687c3779134SOr Gerlitz 	mailbox = mlx4_alloc_cmd_mailbox(dev->dev);
688c3779134SOr Gerlitz 	if (IS_ERR(mailbox))
689c3779134SOr Gerlitz 		return IB_MAD_RESULT_FAILURE;
690c3779134SOr Gerlitz 
691c3779134SOr Gerlitz 	err = mlx4_cmd_box(dev->dev, 0, mailbox->dma, inmod, 0,
692f9baff50SJack Morgenstein 			   MLX4_CMD_QUERY_IF_STAT, MLX4_CMD_TIME_CLASS_C,
693f9baff50SJack Morgenstein 			   MLX4_CMD_WRAPPED);
694c3779134SOr Gerlitz 	if (err)
695c3779134SOr Gerlitz 		err = IB_MAD_RESULT_FAILURE;
696c3779134SOr Gerlitz 	else {
697c3779134SOr Gerlitz 		memset(out_mad->data, 0, sizeof out_mad->data);
698c3779134SOr Gerlitz 		mode = ((struct mlx4_counter *)mailbox->buf)->counter_mode;
699c3779134SOr Gerlitz 		switch (mode & 0xf) {
700c3779134SOr Gerlitz 		case 0:
701c3779134SOr Gerlitz 			edit_counter(mailbox->buf,
702c3779134SOr Gerlitz 						(void *)(out_mad->data + 40));
703c3779134SOr Gerlitz 			err = IB_MAD_RESULT_SUCCESS | IB_MAD_RESULT_REPLY;
704c3779134SOr Gerlitz 			break;
705c3779134SOr Gerlitz 		default:
706c3779134SOr Gerlitz 			err = IB_MAD_RESULT_FAILURE;
707c3779134SOr Gerlitz 		}
708c3779134SOr Gerlitz 	}
709c3779134SOr Gerlitz 
710c3779134SOr Gerlitz 	mlx4_free_cmd_mailbox(dev->dev, mailbox);
711c3779134SOr Gerlitz 
712c3779134SOr Gerlitz 	return err;
713c3779134SOr Gerlitz }
714c3779134SOr Gerlitz 
715c3779134SOr Gerlitz int mlx4_ib_process_mad(struct ib_device *ibdev, int mad_flags, u8 port_num,
716c3779134SOr Gerlitz 			struct ib_wc *in_wc, struct ib_grh *in_grh,
717c3779134SOr Gerlitz 			struct ib_mad *in_mad, struct ib_mad *out_mad)
718c3779134SOr Gerlitz {
719c3779134SOr Gerlitz 	switch (rdma_port_get_link_layer(ibdev, port_num)) {
720c3779134SOr Gerlitz 	case IB_LINK_LAYER_INFINIBAND:
721c3779134SOr Gerlitz 		return ib_process_mad(ibdev, mad_flags, port_num, in_wc,
722c3779134SOr Gerlitz 				      in_grh, in_mad, out_mad);
723c3779134SOr Gerlitz 	case IB_LINK_LAYER_ETHERNET:
724c3779134SOr Gerlitz 		return iboe_process_mad(ibdev, mad_flags, port_num, in_wc,
725c3779134SOr Gerlitz 					  in_grh, in_mad, out_mad);
726c3779134SOr Gerlitz 	default:
727c3779134SOr Gerlitz 		return -EINVAL;
728c3779134SOr Gerlitz 	}
729c3779134SOr Gerlitz }
730c3779134SOr Gerlitz 
731225c7b1fSRoland Dreier static void send_handler(struct ib_mad_agent *agent,
732225c7b1fSRoland Dreier 			 struct ib_mad_send_wc *mad_send_wc)
733225c7b1fSRoland Dreier {
734225c7b1fSRoland Dreier 	ib_free_send_mad(mad_send_wc->send_buf);
735225c7b1fSRoland Dreier }
736225c7b1fSRoland Dreier 
737225c7b1fSRoland Dreier int mlx4_ib_mad_init(struct mlx4_ib_dev *dev)
738225c7b1fSRoland Dreier {
739225c7b1fSRoland Dreier 	struct ib_mad_agent *agent;
740225c7b1fSRoland Dreier 	int p, q;
741225c7b1fSRoland Dreier 	int ret;
742fa417f7bSEli Cohen 	enum rdma_link_layer ll;
743225c7b1fSRoland Dreier 
744fa417f7bSEli Cohen 	for (p = 0; p < dev->num_ports; ++p) {
745fa417f7bSEli Cohen 		ll = rdma_port_get_link_layer(&dev->ib_dev, p + 1);
746225c7b1fSRoland Dreier 		for (q = 0; q <= 1; ++q) {
747fa417f7bSEli Cohen 			if (ll == IB_LINK_LAYER_INFINIBAND) {
748225c7b1fSRoland Dreier 				agent = ib_register_mad_agent(&dev->ib_dev, p + 1,
749225c7b1fSRoland Dreier 							      q ? IB_QPT_GSI : IB_QPT_SMI,
750225c7b1fSRoland Dreier 							      NULL, 0, send_handler,
751225c7b1fSRoland Dreier 							      NULL, NULL);
752225c7b1fSRoland Dreier 				if (IS_ERR(agent)) {
753225c7b1fSRoland Dreier 					ret = PTR_ERR(agent);
754225c7b1fSRoland Dreier 					goto err;
755225c7b1fSRoland Dreier 				}
756225c7b1fSRoland Dreier 				dev->send_agent[p][q] = agent;
757fa417f7bSEli Cohen 			} else
758fa417f7bSEli Cohen 				dev->send_agent[p][q] = NULL;
759fa417f7bSEli Cohen 		}
760225c7b1fSRoland Dreier 	}
761225c7b1fSRoland Dreier 
762225c7b1fSRoland Dreier 	return 0;
763225c7b1fSRoland Dreier 
764225c7b1fSRoland Dreier err:
7657ff93f8bSYevgeny Petrilin 	for (p = 0; p < dev->num_ports; ++p)
766225c7b1fSRoland Dreier 		for (q = 0; q <= 1; ++q)
767225c7b1fSRoland Dreier 			if (dev->send_agent[p][q])
768225c7b1fSRoland Dreier 				ib_unregister_mad_agent(dev->send_agent[p][q]);
769225c7b1fSRoland Dreier 
770225c7b1fSRoland Dreier 	return ret;
771225c7b1fSRoland Dreier }
772225c7b1fSRoland Dreier 
773225c7b1fSRoland Dreier void mlx4_ib_mad_cleanup(struct mlx4_ib_dev *dev)
774225c7b1fSRoland Dreier {
775225c7b1fSRoland Dreier 	struct ib_mad_agent *agent;
776225c7b1fSRoland Dreier 	int p, q;
777225c7b1fSRoland Dreier 
7787ff93f8bSYevgeny Petrilin 	for (p = 0; p < dev->num_ports; ++p) {
779225c7b1fSRoland Dreier 		for (q = 0; q <= 1; ++q) {
780225c7b1fSRoland Dreier 			agent = dev->send_agent[p][q];
781fa417f7bSEli Cohen 			if (agent) {
782225c7b1fSRoland Dreier 				dev->send_agent[p][q] = NULL;
783225c7b1fSRoland Dreier 				ib_unregister_mad_agent(agent);
784225c7b1fSRoland Dreier 			}
785fa417f7bSEli Cohen 		}
786225c7b1fSRoland Dreier 
787225c7b1fSRoland Dreier 		if (dev->sm_ah[p])
788225c7b1fSRoland Dreier 			ib_destroy_ah(dev->sm_ah[p]);
789225c7b1fSRoland Dreier 	}
790225c7b1fSRoland Dreier }
79100f5ce99SJack Morgenstein 
792b9c5d6a6SOren Duer static void handle_client_rereg_event(struct mlx4_ib_dev *dev, u8 port_num)
793b9c5d6a6SOren Duer {
794b9c5d6a6SOren Duer 	/* re-configure the mcg's */
795b9c5d6a6SOren Duer 	if (mlx4_is_master(dev->dev)) {
796b9c5d6a6SOren Duer 		if (!dev->sriov.is_going_down)
797b9c5d6a6SOren Duer 			mlx4_ib_mcg_port_cleanup(&dev->sriov.demux[port_num - 1], 0);
798b9c5d6a6SOren Duer 	}
799b9c5d6a6SOren Duer 	mlx4_ib_dispatch_event(dev, port_num, IB_EVENT_CLIENT_REREGISTER);
800b9c5d6a6SOren Duer }
801b9c5d6a6SOren Duer 
80200f5ce99SJack Morgenstein void handle_port_mgmt_change_event(struct work_struct *work)
80300f5ce99SJack Morgenstein {
80400f5ce99SJack Morgenstein 	struct ib_event_work *ew = container_of(work, struct ib_event_work, work);
80500f5ce99SJack Morgenstein 	struct mlx4_ib_dev *dev = ew->ib_dev;
80600f5ce99SJack Morgenstein 	struct mlx4_eqe *eqe = &(ew->ib_eqe);
80700f5ce99SJack Morgenstein 	u8 port = eqe->event.port_mgmt_change.port;
80800f5ce99SJack Morgenstein 	u32 changed_attr;
80900f5ce99SJack Morgenstein 
81000f5ce99SJack Morgenstein 	switch (eqe->subtype) {
81100f5ce99SJack Morgenstein 	case MLX4_DEV_PMC_SUBTYPE_PORT_INFO:
81200f5ce99SJack Morgenstein 		changed_attr = be32_to_cpu(eqe->event.port_mgmt_change.params.port_info.changed_attr);
81300f5ce99SJack Morgenstein 
81400f5ce99SJack Morgenstein 		/* Update the SM ah - This should be done before handling
81500f5ce99SJack Morgenstein 		   the other changed attributes so that MADs can be sent to the SM */
81600f5ce99SJack Morgenstein 		if (changed_attr & MSTR_SM_CHANGE_MASK) {
81700f5ce99SJack Morgenstein 			u16 lid = be16_to_cpu(eqe->event.port_mgmt_change.params.port_info.mstr_sm_lid);
81800f5ce99SJack Morgenstein 			u8 sl = eqe->event.port_mgmt_change.params.port_info.mstr_sm_sl & 0xf;
81900f5ce99SJack Morgenstein 			update_sm_ah(dev, port, lid, sl);
82000f5ce99SJack Morgenstein 		}
82100f5ce99SJack Morgenstein 
82200f5ce99SJack Morgenstein 		/* Check if it is a lid change event */
82300f5ce99SJack Morgenstein 		if (changed_attr & MLX4_EQ_PORT_INFO_LID_CHANGE_MASK)
82400f5ce99SJack Morgenstein 			mlx4_ib_dispatch_event(dev, port, IB_EVENT_LID_CHANGE);
82500f5ce99SJack Morgenstein 
82600f5ce99SJack Morgenstein 		/* Generate GUID changed event */
82700f5ce99SJack Morgenstein 		if (changed_attr & MLX4_EQ_PORT_INFO_GID_PFX_CHANGE_MASK)
82800f5ce99SJack Morgenstein 			mlx4_ib_dispatch_event(dev, port, IB_EVENT_GID_CHANGE);
82900f5ce99SJack Morgenstein 
83000f5ce99SJack Morgenstein 		if (changed_attr & MLX4_EQ_PORT_INFO_CLIENT_REREG_MASK)
831b9c5d6a6SOren Duer 			handle_client_rereg_event(dev, port);
83200f5ce99SJack Morgenstein 		break;
83300f5ce99SJack Morgenstein 
83400f5ce99SJack Morgenstein 	case MLX4_DEV_PMC_SUBTYPE_PKEY_TABLE:
83500f5ce99SJack Morgenstein 		mlx4_ib_dispatch_event(dev, port, IB_EVENT_PKEY_CHANGE);
83600f5ce99SJack Morgenstein 		break;
83700f5ce99SJack Morgenstein 	case MLX4_DEV_PMC_SUBTYPE_GUID_INFO:
8386634961cSJack Morgenstein 		/* paravirtualized master's guid is guid 0 -- does not change */
8396634961cSJack Morgenstein 		if (!mlx4_is_master(dev->dev))
84000f5ce99SJack Morgenstein 			mlx4_ib_dispatch_event(dev, port, IB_EVENT_GID_CHANGE);
84100f5ce99SJack Morgenstein 		break;
84200f5ce99SJack Morgenstein 	default:
84300f5ce99SJack Morgenstein 		pr_warn("Unsupported subtype 0x%x for "
84400f5ce99SJack Morgenstein 			"Port Management Change event\n", eqe->subtype);
84500f5ce99SJack Morgenstein 	}
84600f5ce99SJack Morgenstein 
84700f5ce99SJack Morgenstein 	kfree(ew);
84800f5ce99SJack Morgenstein }
84900f5ce99SJack Morgenstein 
85000f5ce99SJack Morgenstein void mlx4_ib_dispatch_event(struct mlx4_ib_dev *dev, u8 port_num,
85100f5ce99SJack Morgenstein 			    enum ib_event_type type)
85200f5ce99SJack Morgenstein {
85300f5ce99SJack Morgenstein 	struct ib_event event;
85400f5ce99SJack Morgenstein 
85500f5ce99SJack Morgenstein 	event.device		= &dev->ib_dev;
85600f5ce99SJack Morgenstein 	event.element.port_num	= port_num;
85700f5ce99SJack Morgenstein 	event.event		= type;
85800f5ce99SJack Morgenstein 
85900f5ce99SJack Morgenstein 	ib_dispatch_event(&event);
86000f5ce99SJack Morgenstein }
861fc06573dSJack Morgenstein 
862fc06573dSJack Morgenstein static void mlx4_ib_tunnel_comp_handler(struct ib_cq *cq, void *arg)
863fc06573dSJack Morgenstein {
864fc06573dSJack Morgenstein 	unsigned long flags;
865fc06573dSJack Morgenstein 	struct mlx4_ib_demux_pv_ctx *ctx = cq->cq_context;
866fc06573dSJack Morgenstein 	struct mlx4_ib_dev *dev = to_mdev(ctx->ib_dev);
867fc06573dSJack Morgenstein 	spin_lock_irqsave(&dev->sriov.going_down_lock, flags);
868fc06573dSJack Morgenstein 	if (!dev->sriov.is_going_down && ctx->state == DEMUX_PV_STATE_ACTIVE)
869fc06573dSJack Morgenstein 		queue_work(ctx->wq, &ctx->work);
870fc06573dSJack Morgenstein 	spin_unlock_irqrestore(&dev->sriov.going_down_lock, flags);
871fc06573dSJack Morgenstein }
872fc06573dSJack Morgenstein 
873fc06573dSJack Morgenstein static int mlx4_ib_post_pv_qp_buf(struct mlx4_ib_demux_pv_ctx *ctx,
874fc06573dSJack Morgenstein 				  struct mlx4_ib_demux_pv_qp *tun_qp,
875fc06573dSJack Morgenstein 				  int index)
876fc06573dSJack Morgenstein {
877fc06573dSJack Morgenstein 	struct ib_sge sg_list;
878fc06573dSJack Morgenstein 	struct ib_recv_wr recv_wr, *bad_recv_wr;
879fc06573dSJack Morgenstein 	int size;
880fc06573dSJack Morgenstein 
881fc06573dSJack Morgenstein 	size = (tun_qp->qp->qp_type == IB_QPT_UD) ?
882fc06573dSJack Morgenstein 		sizeof (struct mlx4_tunnel_mad) : sizeof (struct mlx4_mad_rcv_buf);
883fc06573dSJack Morgenstein 
884fc06573dSJack Morgenstein 	sg_list.addr = tun_qp->ring[index].map;
885fc06573dSJack Morgenstein 	sg_list.length = size;
886fc06573dSJack Morgenstein 	sg_list.lkey = ctx->mr->lkey;
887fc06573dSJack Morgenstein 
888fc06573dSJack Morgenstein 	recv_wr.next = NULL;
889fc06573dSJack Morgenstein 	recv_wr.sg_list = &sg_list;
890fc06573dSJack Morgenstein 	recv_wr.num_sge = 1;
891fc06573dSJack Morgenstein 	recv_wr.wr_id = (u64) index | MLX4_TUN_WRID_RECV |
892fc06573dSJack Morgenstein 		MLX4_TUN_SET_WRID_QPN(tun_qp->proxy_qpt);
893fc06573dSJack Morgenstein 	ib_dma_sync_single_for_device(ctx->ib_dev, tun_qp->ring[index].map,
894fc06573dSJack Morgenstein 				      size, DMA_FROM_DEVICE);
895fc06573dSJack Morgenstein 	return ib_post_recv(tun_qp->qp, &recv_wr, &bad_recv_wr);
896fc06573dSJack Morgenstein }
897fc06573dSJack Morgenstein 
89837bfc7c1SJack Morgenstein static int mlx4_ib_multiplex_sa_handler(struct ib_device *ibdev, int port,
89937bfc7c1SJack Morgenstein 		int slave, struct ib_sa_mad *sa_mad)
90037bfc7c1SJack Morgenstein {
901b9c5d6a6SOren Duer 	int ret = 0;
902b9c5d6a6SOren Duer 
903b9c5d6a6SOren Duer 	/* dispatch to different sa handlers */
904b9c5d6a6SOren Duer 	switch (be16_to_cpu(sa_mad->mad_hdr.attr_id)) {
905b9c5d6a6SOren Duer 	case IB_SA_ATTR_MC_MEMBER_REC:
906b9c5d6a6SOren Duer 		ret = mlx4_ib_mcg_multiplex_handler(ibdev, port, slave, sa_mad);
907b9c5d6a6SOren Duer 		break;
908b9c5d6a6SOren Duer 	default:
909b9c5d6a6SOren Duer 		break;
910b9c5d6a6SOren Duer 	}
911b9c5d6a6SOren Duer 	return ret;
91237bfc7c1SJack Morgenstein }
91337bfc7c1SJack Morgenstein 
91437bfc7c1SJack Morgenstein static int is_proxy_qp0(struct mlx4_ib_dev *dev, int qpn, int slave)
91537bfc7c1SJack Morgenstein {
91637bfc7c1SJack Morgenstein 	int slave_start = dev->dev->caps.sqp_start + 8 * slave;
91737bfc7c1SJack Morgenstein 
91837bfc7c1SJack Morgenstein 	return (qpn >= slave_start && qpn <= slave_start + 1);
91937bfc7c1SJack Morgenstein }
92037bfc7c1SJack Morgenstein 
92137bfc7c1SJack Morgenstein 
92237bfc7c1SJack Morgenstein int mlx4_ib_send_to_wire(struct mlx4_ib_dev *dev, int slave, u8 port,
92337bfc7c1SJack Morgenstein 			 enum ib_qp_type dest_qpt, u16 pkey_index, u32 remote_qpn,
92437bfc7c1SJack Morgenstein 			 u32 qkey, struct ib_ah_attr *attr, struct ib_mad *mad)
92537bfc7c1SJack Morgenstein {
92637bfc7c1SJack Morgenstein 	struct ib_sge list;
92737bfc7c1SJack Morgenstein 	struct ib_send_wr wr, *bad_wr;
92837bfc7c1SJack Morgenstein 	struct mlx4_ib_demux_pv_ctx *sqp_ctx;
92937bfc7c1SJack Morgenstein 	struct mlx4_ib_demux_pv_qp *sqp;
93037bfc7c1SJack Morgenstein 	struct mlx4_mad_snd_buf *sqp_mad;
93137bfc7c1SJack Morgenstein 	struct ib_ah *ah;
93237bfc7c1SJack Morgenstein 	struct ib_qp *send_qp = NULL;
93337bfc7c1SJack Morgenstein 	unsigned wire_tx_ix = 0;
93437bfc7c1SJack Morgenstein 	int ret = 0;
93537bfc7c1SJack Morgenstein 	u16 wire_pkey_ix;
93637bfc7c1SJack Morgenstein 	int src_qpnum;
93737bfc7c1SJack Morgenstein 	u8 sgid_index;
93837bfc7c1SJack Morgenstein 
93937bfc7c1SJack Morgenstein 
94037bfc7c1SJack Morgenstein 	sqp_ctx = dev->sriov.sqps[port-1];
94137bfc7c1SJack Morgenstein 
94237bfc7c1SJack Morgenstein 	/* check if proxy qp created */
94337bfc7c1SJack Morgenstein 	if (!sqp_ctx || sqp_ctx->state != DEMUX_PV_STATE_ACTIVE)
94437bfc7c1SJack Morgenstein 		return -EAGAIN;
94537bfc7c1SJack Morgenstein 
94637bfc7c1SJack Morgenstein 	/* QP0 forwarding only for Dom0 */
94737bfc7c1SJack Morgenstein 	if (dest_qpt == IB_QPT_SMI && (mlx4_master_func_num(dev->dev) != slave))
94837bfc7c1SJack Morgenstein 		return -EINVAL;
94937bfc7c1SJack Morgenstein 
95037bfc7c1SJack Morgenstein 	if (dest_qpt == IB_QPT_SMI) {
95137bfc7c1SJack Morgenstein 		src_qpnum = 0;
95237bfc7c1SJack Morgenstein 		sqp = &sqp_ctx->qp[0];
95337bfc7c1SJack Morgenstein 		wire_pkey_ix = dev->pkeys.virt2phys_pkey[slave][port - 1][0];
95437bfc7c1SJack Morgenstein 	} else {
95537bfc7c1SJack Morgenstein 		src_qpnum = 1;
95637bfc7c1SJack Morgenstein 		sqp = &sqp_ctx->qp[1];
95737bfc7c1SJack Morgenstein 		wire_pkey_ix = dev->pkeys.virt2phys_pkey[slave][port - 1][pkey_index];
95837bfc7c1SJack Morgenstein 	}
95937bfc7c1SJack Morgenstein 
96037bfc7c1SJack Morgenstein 	send_qp = sqp->qp;
96137bfc7c1SJack Morgenstein 
96237bfc7c1SJack Morgenstein 	/* create ah */
96337bfc7c1SJack Morgenstein 	sgid_index = attr->grh.sgid_index;
96437bfc7c1SJack Morgenstein 	attr->grh.sgid_index = 0;
96537bfc7c1SJack Morgenstein 	ah = ib_create_ah(sqp_ctx->pd, attr);
96637bfc7c1SJack Morgenstein 	if (IS_ERR(ah))
96737bfc7c1SJack Morgenstein 		return -ENOMEM;
96837bfc7c1SJack Morgenstein 	attr->grh.sgid_index = sgid_index;
96937bfc7c1SJack Morgenstein 	to_mah(ah)->av.ib.gid_index = sgid_index;
97037bfc7c1SJack Morgenstein 	/* get rid of force-loopback bit */
97137bfc7c1SJack Morgenstein 	to_mah(ah)->av.ib.port_pd &= cpu_to_be32(0x7FFFFFFF);
97237bfc7c1SJack Morgenstein 	spin_lock(&sqp->tx_lock);
97337bfc7c1SJack Morgenstein 	if (sqp->tx_ix_head - sqp->tx_ix_tail >=
97437bfc7c1SJack Morgenstein 	    (MLX4_NUM_TUNNEL_BUFS - 1))
97537bfc7c1SJack Morgenstein 		ret = -EAGAIN;
97637bfc7c1SJack Morgenstein 	else
97737bfc7c1SJack Morgenstein 		wire_tx_ix = (++sqp->tx_ix_head) & (MLX4_NUM_TUNNEL_BUFS - 1);
97837bfc7c1SJack Morgenstein 	spin_unlock(&sqp->tx_lock);
97937bfc7c1SJack Morgenstein 	if (ret)
98037bfc7c1SJack Morgenstein 		goto out;
98137bfc7c1SJack Morgenstein 
98237bfc7c1SJack Morgenstein 	sqp_mad = (struct mlx4_mad_snd_buf *) (sqp->tx_ring[wire_tx_ix].buf.addr);
98337bfc7c1SJack Morgenstein 	if (sqp->tx_ring[wire_tx_ix].ah)
98437bfc7c1SJack Morgenstein 		ib_destroy_ah(sqp->tx_ring[wire_tx_ix].ah);
98537bfc7c1SJack Morgenstein 	sqp->tx_ring[wire_tx_ix].ah = ah;
98637bfc7c1SJack Morgenstein 	ib_dma_sync_single_for_cpu(&dev->ib_dev,
98737bfc7c1SJack Morgenstein 				   sqp->tx_ring[wire_tx_ix].buf.map,
98837bfc7c1SJack Morgenstein 				   sizeof (struct mlx4_mad_snd_buf),
98937bfc7c1SJack Morgenstein 				   DMA_TO_DEVICE);
99037bfc7c1SJack Morgenstein 
99137bfc7c1SJack Morgenstein 	memcpy(&sqp_mad->payload, mad, sizeof *mad);
99237bfc7c1SJack Morgenstein 
99337bfc7c1SJack Morgenstein 	ib_dma_sync_single_for_device(&dev->ib_dev,
99437bfc7c1SJack Morgenstein 				      sqp->tx_ring[wire_tx_ix].buf.map,
99537bfc7c1SJack Morgenstein 				      sizeof (struct mlx4_mad_snd_buf),
99637bfc7c1SJack Morgenstein 				      DMA_TO_DEVICE);
99737bfc7c1SJack Morgenstein 
99837bfc7c1SJack Morgenstein 	list.addr = sqp->tx_ring[wire_tx_ix].buf.map;
99937bfc7c1SJack Morgenstein 	list.length = sizeof (struct mlx4_mad_snd_buf);
100037bfc7c1SJack Morgenstein 	list.lkey = sqp_ctx->mr->lkey;
100137bfc7c1SJack Morgenstein 
100237bfc7c1SJack Morgenstein 	wr.wr.ud.ah = ah;
100337bfc7c1SJack Morgenstein 	wr.wr.ud.port_num = port;
100437bfc7c1SJack Morgenstein 	wr.wr.ud.pkey_index = wire_pkey_ix;
100537bfc7c1SJack Morgenstein 	wr.wr.ud.remote_qkey = qkey;
100637bfc7c1SJack Morgenstein 	wr.wr.ud.remote_qpn = remote_qpn;
100737bfc7c1SJack Morgenstein 	wr.next = NULL;
100837bfc7c1SJack Morgenstein 	wr.wr_id = ((u64) wire_tx_ix) | MLX4_TUN_SET_WRID_QPN(src_qpnum);
100937bfc7c1SJack Morgenstein 	wr.sg_list = &list;
101037bfc7c1SJack Morgenstein 	wr.num_sge = 1;
101137bfc7c1SJack Morgenstein 	wr.opcode = IB_WR_SEND;
101237bfc7c1SJack Morgenstein 	wr.send_flags = IB_SEND_SIGNALED;
101337bfc7c1SJack Morgenstein 
101437bfc7c1SJack Morgenstein 	ret = ib_post_send(send_qp, &wr, &bad_wr);
101537bfc7c1SJack Morgenstein out:
101637bfc7c1SJack Morgenstein 	if (ret)
101737bfc7c1SJack Morgenstein 		ib_destroy_ah(ah);
101837bfc7c1SJack Morgenstein 	return ret;
101937bfc7c1SJack Morgenstein }
102037bfc7c1SJack Morgenstein 
102137bfc7c1SJack Morgenstein static void mlx4_ib_multiplex_mad(struct mlx4_ib_demux_pv_ctx *ctx, struct ib_wc *wc)
102237bfc7c1SJack Morgenstein {
102337bfc7c1SJack Morgenstein 	struct mlx4_ib_dev *dev = to_mdev(ctx->ib_dev);
102437bfc7c1SJack Morgenstein 	struct mlx4_ib_demux_pv_qp *tun_qp = &ctx->qp[MLX4_TUN_WRID_QPN(wc->wr_id)];
102537bfc7c1SJack Morgenstein 	int wr_ix = wc->wr_id & (MLX4_NUM_TUNNEL_BUFS - 1);
102637bfc7c1SJack Morgenstein 	struct mlx4_tunnel_mad *tunnel = tun_qp->ring[wr_ix].addr;
102737bfc7c1SJack Morgenstein 	struct mlx4_ib_ah ah;
102837bfc7c1SJack Morgenstein 	struct ib_ah_attr ah_attr;
102937bfc7c1SJack Morgenstein 	u8 *slave_id;
103037bfc7c1SJack Morgenstein 	int slave;
103137bfc7c1SJack Morgenstein 
103237bfc7c1SJack Morgenstein 	/* Get slave that sent this packet */
103337bfc7c1SJack Morgenstein 	if (wc->src_qp < dev->dev->caps.sqp_start ||
103437bfc7c1SJack Morgenstein 	    wc->src_qp >= dev->dev->caps.base_tunnel_sqpn ||
103537bfc7c1SJack Morgenstein 	    (wc->src_qp & 0x1) != ctx->port - 1 ||
103637bfc7c1SJack Morgenstein 	    wc->src_qp & 0x4) {
103737bfc7c1SJack Morgenstein 		mlx4_ib_warn(ctx->ib_dev, "can't multiplex bad sqp:%d\n", wc->src_qp);
103837bfc7c1SJack Morgenstein 		return;
103937bfc7c1SJack Morgenstein 	}
104037bfc7c1SJack Morgenstein 	slave = ((wc->src_qp & ~0x7) - dev->dev->caps.sqp_start) / 8;
104137bfc7c1SJack Morgenstein 	if (slave != ctx->slave) {
104237bfc7c1SJack Morgenstein 		mlx4_ib_warn(ctx->ib_dev, "can't multiplex bad sqp:%d: "
104337bfc7c1SJack Morgenstein 			     "belongs to another slave\n", wc->src_qp);
104437bfc7c1SJack Morgenstein 		return;
104537bfc7c1SJack Morgenstein 	}
104637bfc7c1SJack Morgenstein 	if (slave != mlx4_master_func_num(dev->dev) && !(wc->src_qp & 0x2)) {
104737bfc7c1SJack Morgenstein 		mlx4_ib_warn(ctx->ib_dev, "can't multiplex bad sqp:%d: "
104837bfc7c1SJack Morgenstein 			     "non-master trying to send QP0 packets\n", wc->src_qp);
104937bfc7c1SJack Morgenstein 		return;
105037bfc7c1SJack Morgenstein 	}
105137bfc7c1SJack Morgenstein 
105237bfc7c1SJack Morgenstein 	/* Map transaction ID */
105337bfc7c1SJack Morgenstein 	ib_dma_sync_single_for_cpu(ctx->ib_dev, tun_qp->ring[wr_ix].map,
105437bfc7c1SJack Morgenstein 				   sizeof (struct mlx4_tunnel_mad),
105537bfc7c1SJack Morgenstein 				   DMA_FROM_DEVICE);
105637bfc7c1SJack Morgenstein 	switch (tunnel->mad.mad_hdr.method) {
105737bfc7c1SJack Morgenstein 	case IB_MGMT_METHOD_SET:
105837bfc7c1SJack Morgenstein 	case IB_MGMT_METHOD_GET:
105937bfc7c1SJack Morgenstein 	case IB_MGMT_METHOD_REPORT:
106037bfc7c1SJack Morgenstein 	case IB_SA_METHOD_GET_TABLE:
106137bfc7c1SJack Morgenstein 	case IB_SA_METHOD_DELETE:
106237bfc7c1SJack Morgenstein 	case IB_SA_METHOD_GET_MULTI:
106337bfc7c1SJack Morgenstein 	case IB_SA_METHOD_GET_TRACE_TBL:
106437bfc7c1SJack Morgenstein 		slave_id = (u8 *) &tunnel->mad.mad_hdr.tid;
106537bfc7c1SJack Morgenstein 		if (*slave_id) {
106637bfc7c1SJack Morgenstein 			mlx4_ib_warn(ctx->ib_dev, "egress mad has non-null tid msb:%d "
106737bfc7c1SJack Morgenstein 				     "class:%d slave:%d\n", *slave_id,
106837bfc7c1SJack Morgenstein 				     tunnel->mad.mad_hdr.mgmt_class, slave);
106937bfc7c1SJack Morgenstein 			return;
107037bfc7c1SJack Morgenstein 		} else
107137bfc7c1SJack Morgenstein 			*slave_id = slave;
107237bfc7c1SJack Morgenstein 	default:
107337bfc7c1SJack Morgenstein 		/* nothing */;
107437bfc7c1SJack Morgenstein 	}
107537bfc7c1SJack Morgenstein 
107637bfc7c1SJack Morgenstein 	/* Class-specific handling */
107737bfc7c1SJack Morgenstein 	switch (tunnel->mad.mad_hdr.mgmt_class) {
107837bfc7c1SJack Morgenstein 	case IB_MGMT_CLASS_SUBN_ADM:
107937bfc7c1SJack Morgenstein 		if (mlx4_ib_multiplex_sa_handler(ctx->ib_dev, ctx->port, slave,
108037bfc7c1SJack Morgenstein 			      (struct ib_sa_mad *) &tunnel->mad))
108137bfc7c1SJack Morgenstein 			return;
108237bfc7c1SJack Morgenstein 		break;
1083*3cf69cc8SAmir Vadai 	case IB_MGMT_CLASS_CM:
1084*3cf69cc8SAmir Vadai 		if (mlx4_ib_multiplex_cm_handler(ctx->ib_dev, ctx->port, slave,
1085*3cf69cc8SAmir Vadai 			      (struct ib_mad *) &tunnel->mad))
1086*3cf69cc8SAmir Vadai 			return;
1087*3cf69cc8SAmir Vadai 		break;
108837bfc7c1SJack Morgenstein 	case IB_MGMT_CLASS_DEVICE_MGMT:
108937bfc7c1SJack Morgenstein 		if (tunnel->mad.mad_hdr.method != IB_MGMT_METHOD_GET &&
109037bfc7c1SJack Morgenstein 		    tunnel->mad.mad_hdr.method != IB_MGMT_METHOD_SET)
109137bfc7c1SJack Morgenstein 			return;
109237bfc7c1SJack Morgenstein 		break;
109337bfc7c1SJack Morgenstein 	default:
109437bfc7c1SJack Morgenstein 		/* Drop unsupported classes for slaves in tunnel mode */
109537bfc7c1SJack Morgenstein 		if (slave != mlx4_master_func_num(dev->dev)) {
109637bfc7c1SJack Morgenstein 			mlx4_ib_warn(ctx->ib_dev, "dropping unsupported egress mad from class:%d "
109737bfc7c1SJack Morgenstein 				     "for slave:%d\n", tunnel->mad.mad_hdr.mgmt_class, slave);
109837bfc7c1SJack Morgenstein 			return;
109937bfc7c1SJack Morgenstein 		}
110037bfc7c1SJack Morgenstein 	}
110137bfc7c1SJack Morgenstein 
110237bfc7c1SJack Morgenstein 	/* We are using standard ib_core services to send the mad, so generate a
110337bfc7c1SJack Morgenstein 	 * stadard address handle by decoding the tunnelled mlx4_ah fields */
110437bfc7c1SJack Morgenstein 	memcpy(&ah.av, &tunnel->hdr.av, sizeof (struct mlx4_av));
110537bfc7c1SJack Morgenstein 	ah.ibah.device = ctx->ib_dev;
110637bfc7c1SJack Morgenstein 	mlx4_ib_query_ah(&ah.ibah, &ah_attr);
110737bfc7c1SJack Morgenstein 	if ((ah_attr.ah_flags & IB_AH_GRH) &&
110837bfc7c1SJack Morgenstein 	    (ah_attr.grh.sgid_index != slave)) {
110937bfc7c1SJack Morgenstein 		mlx4_ib_warn(ctx->ib_dev, "slave:%d accessed invalid sgid_index:%d\n",
111037bfc7c1SJack Morgenstein 			     slave, ah_attr.grh.sgid_index);
111137bfc7c1SJack Morgenstein 		return;
111237bfc7c1SJack Morgenstein 	}
111337bfc7c1SJack Morgenstein 
111437bfc7c1SJack Morgenstein 	mlx4_ib_send_to_wire(dev, slave, ctx->port,
111537bfc7c1SJack Morgenstein 			     is_proxy_qp0(dev, wc->src_qp, slave) ?
111637bfc7c1SJack Morgenstein 			     IB_QPT_SMI : IB_QPT_GSI,
111737bfc7c1SJack Morgenstein 			     be16_to_cpu(tunnel->hdr.pkey_index),
111837bfc7c1SJack Morgenstein 			     be32_to_cpu(tunnel->hdr.remote_qpn),
111937bfc7c1SJack Morgenstein 			     be32_to_cpu(tunnel->hdr.qkey),
112037bfc7c1SJack Morgenstein 			     &ah_attr, &tunnel->mad);
112137bfc7c1SJack Morgenstein }
112237bfc7c1SJack Morgenstein 
1123fc06573dSJack Morgenstein static int mlx4_ib_alloc_pv_bufs(struct mlx4_ib_demux_pv_ctx *ctx,
1124fc06573dSJack Morgenstein 				 enum ib_qp_type qp_type, int is_tun)
1125fc06573dSJack Morgenstein {
1126fc06573dSJack Morgenstein 	int i;
1127fc06573dSJack Morgenstein 	struct mlx4_ib_demux_pv_qp *tun_qp;
1128fc06573dSJack Morgenstein 	int rx_buf_size, tx_buf_size;
1129fc06573dSJack Morgenstein 
1130fc06573dSJack Morgenstein 	if (qp_type > IB_QPT_GSI)
1131fc06573dSJack Morgenstein 		return -EINVAL;
1132fc06573dSJack Morgenstein 
1133fc06573dSJack Morgenstein 	tun_qp = &ctx->qp[qp_type];
1134fc06573dSJack Morgenstein 
1135fc06573dSJack Morgenstein 	tun_qp->ring = kzalloc(sizeof (struct mlx4_ib_buf) * MLX4_NUM_TUNNEL_BUFS,
1136fc06573dSJack Morgenstein 			       GFP_KERNEL);
1137fc06573dSJack Morgenstein 	if (!tun_qp->ring)
1138fc06573dSJack Morgenstein 		return -ENOMEM;
1139fc06573dSJack Morgenstein 
1140fc06573dSJack Morgenstein 	tun_qp->tx_ring = kcalloc(MLX4_NUM_TUNNEL_BUFS,
1141fc06573dSJack Morgenstein 				  sizeof (struct mlx4_ib_tun_tx_buf),
1142fc06573dSJack Morgenstein 				  GFP_KERNEL);
1143fc06573dSJack Morgenstein 	if (!tun_qp->tx_ring) {
1144fc06573dSJack Morgenstein 		kfree(tun_qp->ring);
1145fc06573dSJack Morgenstein 		tun_qp->ring = NULL;
1146fc06573dSJack Morgenstein 		return -ENOMEM;
1147fc06573dSJack Morgenstein 	}
1148fc06573dSJack Morgenstein 
1149fc06573dSJack Morgenstein 	if (is_tun) {
1150fc06573dSJack Morgenstein 		rx_buf_size = sizeof (struct mlx4_tunnel_mad);
1151fc06573dSJack Morgenstein 		tx_buf_size = sizeof (struct mlx4_rcv_tunnel_mad);
1152fc06573dSJack Morgenstein 	} else {
1153fc06573dSJack Morgenstein 		rx_buf_size = sizeof (struct mlx4_mad_rcv_buf);
1154fc06573dSJack Morgenstein 		tx_buf_size = sizeof (struct mlx4_mad_snd_buf);
1155fc06573dSJack Morgenstein 	}
1156fc06573dSJack Morgenstein 
1157fc06573dSJack Morgenstein 	for (i = 0; i < MLX4_NUM_TUNNEL_BUFS; i++) {
1158fc06573dSJack Morgenstein 		tun_qp->ring[i].addr = kmalloc(rx_buf_size, GFP_KERNEL);
1159fc06573dSJack Morgenstein 		if (!tun_qp->ring[i].addr)
1160fc06573dSJack Morgenstein 			goto err;
1161fc06573dSJack Morgenstein 		tun_qp->ring[i].map = ib_dma_map_single(ctx->ib_dev,
1162fc06573dSJack Morgenstein 							tun_qp->ring[i].addr,
1163fc06573dSJack Morgenstein 							rx_buf_size,
1164fc06573dSJack Morgenstein 							DMA_FROM_DEVICE);
1165fc06573dSJack Morgenstein 	}
1166fc06573dSJack Morgenstein 
1167fc06573dSJack Morgenstein 	for (i = 0; i < MLX4_NUM_TUNNEL_BUFS; i++) {
1168fc06573dSJack Morgenstein 		tun_qp->tx_ring[i].buf.addr =
1169fc06573dSJack Morgenstein 			kmalloc(tx_buf_size, GFP_KERNEL);
1170fc06573dSJack Morgenstein 		if (!tun_qp->tx_ring[i].buf.addr)
1171fc06573dSJack Morgenstein 			goto tx_err;
1172fc06573dSJack Morgenstein 		tun_qp->tx_ring[i].buf.map =
1173fc06573dSJack Morgenstein 			ib_dma_map_single(ctx->ib_dev,
1174fc06573dSJack Morgenstein 					  tun_qp->tx_ring[i].buf.addr,
1175fc06573dSJack Morgenstein 					  tx_buf_size,
1176fc06573dSJack Morgenstein 					  DMA_TO_DEVICE);
1177fc06573dSJack Morgenstein 		tun_qp->tx_ring[i].ah = NULL;
1178fc06573dSJack Morgenstein 	}
1179fc06573dSJack Morgenstein 	spin_lock_init(&tun_qp->tx_lock);
1180fc06573dSJack Morgenstein 	tun_qp->tx_ix_head = 0;
1181fc06573dSJack Morgenstein 	tun_qp->tx_ix_tail = 0;
1182fc06573dSJack Morgenstein 	tun_qp->proxy_qpt = qp_type;
1183fc06573dSJack Morgenstein 
1184fc06573dSJack Morgenstein 	return 0;
1185fc06573dSJack Morgenstein 
1186fc06573dSJack Morgenstein tx_err:
1187fc06573dSJack Morgenstein 	while (i > 0) {
1188fc06573dSJack Morgenstein 		--i;
1189fc06573dSJack Morgenstein 		ib_dma_unmap_single(ctx->ib_dev, tun_qp->tx_ring[i].buf.map,
1190fc06573dSJack Morgenstein 				    tx_buf_size, DMA_TO_DEVICE);
1191fc06573dSJack Morgenstein 		kfree(tun_qp->tx_ring[i].buf.addr);
1192fc06573dSJack Morgenstein 	}
1193fc06573dSJack Morgenstein 	kfree(tun_qp->tx_ring);
1194fc06573dSJack Morgenstein 	tun_qp->tx_ring = NULL;
1195fc06573dSJack Morgenstein 	i = MLX4_NUM_TUNNEL_BUFS;
1196fc06573dSJack Morgenstein err:
1197fc06573dSJack Morgenstein 	while (i > 0) {
1198fc06573dSJack Morgenstein 		--i;
1199fc06573dSJack Morgenstein 		ib_dma_unmap_single(ctx->ib_dev, tun_qp->ring[i].map,
1200fc06573dSJack Morgenstein 				    rx_buf_size, DMA_FROM_DEVICE);
1201fc06573dSJack Morgenstein 		kfree(tun_qp->ring[i].addr);
1202fc06573dSJack Morgenstein 	}
1203fc06573dSJack Morgenstein 	kfree(tun_qp->ring);
1204fc06573dSJack Morgenstein 	tun_qp->ring = NULL;
1205fc06573dSJack Morgenstein 	return -ENOMEM;
1206fc06573dSJack Morgenstein }
1207fc06573dSJack Morgenstein 
1208fc06573dSJack Morgenstein static void mlx4_ib_free_pv_qp_bufs(struct mlx4_ib_demux_pv_ctx *ctx,
1209fc06573dSJack Morgenstein 				     enum ib_qp_type qp_type, int is_tun)
1210fc06573dSJack Morgenstein {
1211fc06573dSJack Morgenstein 	int i;
1212fc06573dSJack Morgenstein 	struct mlx4_ib_demux_pv_qp *tun_qp;
1213fc06573dSJack Morgenstein 	int rx_buf_size, tx_buf_size;
1214fc06573dSJack Morgenstein 
1215fc06573dSJack Morgenstein 	if (qp_type > IB_QPT_GSI)
1216fc06573dSJack Morgenstein 		return;
1217fc06573dSJack Morgenstein 
1218fc06573dSJack Morgenstein 	tun_qp = &ctx->qp[qp_type];
1219fc06573dSJack Morgenstein 	if (is_tun) {
1220fc06573dSJack Morgenstein 		rx_buf_size = sizeof (struct mlx4_tunnel_mad);
1221fc06573dSJack Morgenstein 		tx_buf_size = sizeof (struct mlx4_rcv_tunnel_mad);
1222fc06573dSJack Morgenstein 	} else {
1223fc06573dSJack Morgenstein 		rx_buf_size = sizeof (struct mlx4_mad_rcv_buf);
1224fc06573dSJack Morgenstein 		tx_buf_size = sizeof (struct mlx4_mad_snd_buf);
1225fc06573dSJack Morgenstein 	}
1226fc06573dSJack Morgenstein 
1227fc06573dSJack Morgenstein 
1228fc06573dSJack Morgenstein 	for (i = 0; i < MLX4_NUM_TUNNEL_BUFS; i++) {
1229fc06573dSJack Morgenstein 		ib_dma_unmap_single(ctx->ib_dev, tun_qp->ring[i].map,
1230fc06573dSJack Morgenstein 				    rx_buf_size, DMA_FROM_DEVICE);
1231fc06573dSJack Morgenstein 		kfree(tun_qp->ring[i].addr);
1232fc06573dSJack Morgenstein 	}
1233fc06573dSJack Morgenstein 
1234fc06573dSJack Morgenstein 	for (i = 0; i < MLX4_NUM_TUNNEL_BUFS; i++) {
1235fc06573dSJack Morgenstein 		ib_dma_unmap_single(ctx->ib_dev, tun_qp->tx_ring[i].buf.map,
1236fc06573dSJack Morgenstein 				    tx_buf_size, DMA_TO_DEVICE);
1237fc06573dSJack Morgenstein 		kfree(tun_qp->tx_ring[i].buf.addr);
1238fc06573dSJack Morgenstein 		if (tun_qp->tx_ring[i].ah)
1239fc06573dSJack Morgenstein 			ib_destroy_ah(tun_qp->tx_ring[i].ah);
1240fc06573dSJack Morgenstein 	}
1241fc06573dSJack Morgenstein 	kfree(tun_qp->tx_ring);
1242fc06573dSJack Morgenstein 	kfree(tun_qp->ring);
1243fc06573dSJack Morgenstein }
1244fc06573dSJack Morgenstein 
1245fc06573dSJack Morgenstein static void mlx4_ib_tunnel_comp_worker(struct work_struct *work)
1246fc06573dSJack Morgenstein {
124737bfc7c1SJack Morgenstein 	struct mlx4_ib_demux_pv_ctx *ctx;
124837bfc7c1SJack Morgenstein 	struct mlx4_ib_demux_pv_qp *tun_qp;
124937bfc7c1SJack Morgenstein 	struct ib_wc wc;
125037bfc7c1SJack Morgenstein 	int ret;
125137bfc7c1SJack Morgenstein 	ctx = container_of(work, struct mlx4_ib_demux_pv_ctx, work);
125237bfc7c1SJack Morgenstein 	ib_req_notify_cq(ctx->cq, IB_CQ_NEXT_COMP);
125337bfc7c1SJack Morgenstein 
125437bfc7c1SJack Morgenstein 	while (ib_poll_cq(ctx->cq, 1, &wc) == 1) {
125537bfc7c1SJack Morgenstein 		tun_qp = &ctx->qp[MLX4_TUN_WRID_QPN(wc.wr_id)];
125637bfc7c1SJack Morgenstein 		if (wc.status == IB_WC_SUCCESS) {
125737bfc7c1SJack Morgenstein 			switch (wc.opcode) {
125837bfc7c1SJack Morgenstein 			case IB_WC_RECV:
125937bfc7c1SJack Morgenstein 				mlx4_ib_multiplex_mad(ctx, &wc);
126037bfc7c1SJack Morgenstein 				ret = mlx4_ib_post_pv_qp_buf(ctx, tun_qp,
126137bfc7c1SJack Morgenstein 							     wc.wr_id &
126237bfc7c1SJack Morgenstein 							     (MLX4_NUM_TUNNEL_BUFS - 1));
126337bfc7c1SJack Morgenstein 				if (ret)
126437bfc7c1SJack Morgenstein 					pr_err("Failed reposting tunnel "
126537bfc7c1SJack Morgenstein 					       "buf:%lld\n", wc.wr_id);
126637bfc7c1SJack Morgenstein 				break;
126737bfc7c1SJack Morgenstein 			case IB_WC_SEND:
126837bfc7c1SJack Morgenstein 				pr_debug("received tunnel send completion:"
126937bfc7c1SJack Morgenstein 					 "wrid=0x%llx, status=0x%x\n",
127037bfc7c1SJack Morgenstein 					 wc.wr_id, wc.status);
127137bfc7c1SJack Morgenstein 				ib_destroy_ah(tun_qp->tx_ring[wc.wr_id &
127237bfc7c1SJack Morgenstein 					      (MLX4_NUM_TUNNEL_BUFS - 1)].ah);
127337bfc7c1SJack Morgenstein 				tun_qp->tx_ring[wc.wr_id & (MLX4_NUM_TUNNEL_BUFS - 1)].ah
127437bfc7c1SJack Morgenstein 					= NULL;
127537bfc7c1SJack Morgenstein 				spin_lock(&tun_qp->tx_lock);
127637bfc7c1SJack Morgenstein 				tun_qp->tx_ix_tail++;
127737bfc7c1SJack Morgenstein 				spin_unlock(&tun_qp->tx_lock);
127837bfc7c1SJack Morgenstein 
127937bfc7c1SJack Morgenstein 				break;
128037bfc7c1SJack Morgenstein 			default:
128137bfc7c1SJack Morgenstein 				break;
128237bfc7c1SJack Morgenstein 			}
128337bfc7c1SJack Morgenstein 		} else  {
128437bfc7c1SJack Morgenstein 			pr_debug("mlx4_ib: completion error in tunnel: %d."
128537bfc7c1SJack Morgenstein 				 " status = %d, wrid = 0x%llx\n",
128637bfc7c1SJack Morgenstein 				 ctx->slave, wc.status, wc.wr_id);
128737bfc7c1SJack Morgenstein 			if (!MLX4_TUN_IS_RECV(wc.wr_id)) {
128837bfc7c1SJack Morgenstein 				ib_destroy_ah(tun_qp->tx_ring[wc.wr_id &
128937bfc7c1SJack Morgenstein 					      (MLX4_NUM_TUNNEL_BUFS - 1)].ah);
129037bfc7c1SJack Morgenstein 				tun_qp->tx_ring[wc.wr_id & (MLX4_NUM_TUNNEL_BUFS - 1)].ah
129137bfc7c1SJack Morgenstein 					= NULL;
129237bfc7c1SJack Morgenstein 				spin_lock(&tun_qp->tx_lock);
129337bfc7c1SJack Morgenstein 				tun_qp->tx_ix_tail++;
129437bfc7c1SJack Morgenstein 				spin_unlock(&tun_qp->tx_lock);
129537bfc7c1SJack Morgenstein 			}
129637bfc7c1SJack Morgenstein 		}
129737bfc7c1SJack Morgenstein 	}
1298fc06573dSJack Morgenstein }
1299fc06573dSJack Morgenstein 
1300fc06573dSJack Morgenstein static void pv_qp_event_handler(struct ib_event *event, void *qp_context)
1301fc06573dSJack Morgenstein {
1302fc06573dSJack Morgenstein 	struct mlx4_ib_demux_pv_ctx *sqp = qp_context;
1303fc06573dSJack Morgenstein 
1304fc06573dSJack Morgenstein 	/* It's worse than that! He's dead, Jim! */
1305fc06573dSJack Morgenstein 	pr_err("Fatal error (%d) on a MAD QP on port %d\n",
1306fc06573dSJack Morgenstein 	       event->event, sqp->port);
1307fc06573dSJack Morgenstein }
1308fc06573dSJack Morgenstein 
1309fc06573dSJack Morgenstein static int create_pv_sqp(struct mlx4_ib_demux_pv_ctx *ctx,
1310fc06573dSJack Morgenstein 			    enum ib_qp_type qp_type, int create_tun)
1311fc06573dSJack Morgenstein {
1312fc06573dSJack Morgenstein 	int i, ret;
1313fc06573dSJack Morgenstein 	struct mlx4_ib_demux_pv_qp *tun_qp;
1314fc06573dSJack Morgenstein 	struct mlx4_ib_qp_tunnel_init_attr qp_init_attr;
1315fc06573dSJack Morgenstein 	struct ib_qp_attr attr;
1316fc06573dSJack Morgenstein 	int qp_attr_mask_INIT;
1317fc06573dSJack Morgenstein 
1318fc06573dSJack Morgenstein 	if (qp_type > IB_QPT_GSI)
1319fc06573dSJack Morgenstein 		return -EINVAL;
1320fc06573dSJack Morgenstein 
1321fc06573dSJack Morgenstein 	tun_qp = &ctx->qp[qp_type];
1322fc06573dSJack Morgenstein 
1323fc06573dSJack Morgenstein 	memset(&qp_init_attr, 0, sizeof qp_init_attr);
1324fc06573dSJack Morgenstein 	qp_init_attr.init_attr.send_cq = ctx->cq;
1325fc06573dSJack Morgenstein 	qp_init_attr.init_attr.recv_cq = ctx->cq;
1326fc06573dSJack Morgenstein 	qp_init_attr.init_attr.sq_sig_type = IB_SIGNAL_ALL_WR;
1327fc06573dSJack Morgenstein 	qp_init_attr.init_attr.cap.max_send_wr = MLX4_NUM_TUNNEL_BUFS;
1328fc06573dSJack Morgenstein 	qp_init_attr.init_attr.cap.max_recv_wr = MLX4_NUM_TUNNEL_BUFS;
1329fc06573dSJack Morgenstein 	qp_init_attr.init_attr.cap.max_send_sge = 1;
1330fc06573dSJack Morgenstein 	qp_init_attr.init_attr.cap.max_recv_sge = 1;
1331fc06573dSJack Morgenstein 	if (create_tun) {
1332fc06573dSJack Morgenstein 		qp_init_attr.init_attr.qp_type = IB_QPT_UD;
1333fc06573dSJack Morgenstein 		qp_init_attr.init_attr.create_flags = MLX4_IB_SRIOV_TUNNEL_QP;
1334fc06573dSJack Morgenstein 		qp_init_attr.port = ctx->port;
1335fc06573dSJack Morgenstein 		qp_init_attr.slave = ctx->slave;
1336fc06573dSJack Morgenstein 		qp_init_attr.proxy_qp_type = qp_type;
1337fc06573dSJack Morgenstein 		qp_attr_mask_INIT = IB_QP_STATE | IB_QP_PKEY_INDEX |
1338fc06573dSJack Morgenstein 			   IB_QP_QKEY | IB_QP_PORT;
1339fc06573dSJack Morgenstein 	} else {
1340fc06573dSJack Morgenstein 		qp_init_attr.init_attr.qp_type = qp_type;
1341fc06573dSJack Morgenstein 		qp_init_attr.init_attr.create_flags = MLX4_IB_SRIOV_SQP;
1342fc06573dSJack Morgenstein 		qp_attr_mask_INIT = IB_QP_STATE | IB_QP_PKEY_INDEX | IB_QP_QKEY;
1343fc06573dSJack Morgenstein 	}
1344fc06573dSJack Morgenstein 	qp_init_attr.init_attr.port_num = ctx->port;
1345fc06573dSJack Morgenstein 	qp_init_attr.init_attr.qp_context = ctx;
1346fc06573dSJack Morgenstein 	qp_init_attr.init_attr.event_handler = pv_qp_event_handler;
1347fc06573dSJack Morgenstein 	tun_qp->qp = ib_create_qp(ctx->pd, &qp_init_attr.init_attr);
1348fc06573dSJack Morgenstein 	if (IS_ERR(tun_qp->qp)) {
1349fc06573dSJack Morgenstein 		ret = PTR_ERR(tun_qp->qp);
1350fc06573dSJack Morgenstein 		tun_qp->qp = NULL;
1351fc06573dSJack Morgenstein 		pr_err("Couldn't create %s QP (%d)\n",
1352fc06573dSJack Morgenstein 		       create_tun ? "tunnel" : "special", ret);
1353fc06573dSJack Morgenstein 		return ret;
1354fc06573dSJack Morgenstein 	}
1355fc06573dSJack Morgenstein 
1356fc06573dSJack Morgenstein 	memset(&attr, 0, sizeof attr);
1357fc06573dSJack Morgenstein 	attr.qp_state = IB_QPS_INIT;
1358fc06573dSJack Morgenstein 	attr.pkey_index =
1359fc06573dSJack Morgenstein 		to_mdev(ctx->ib_dev)->pkeys.virt2phys_pkey[ctx->slave][ctx->port - 1][0];
1360fc06573dSJack Morgenstein 	attr.qkey = IB_QP1_QKEY;
1361fc06573dSJack Morgenstein 	attr.port_num = ctx->port;
1362fc06573dSJack Morgenstein 	ret = ib_modify_qp(tun_qp->qp, &attr, qp_attr_mask_INIT);
1363fc06573dSJack Morgenstein 	if (ret) {
1364fc06573dSJack Morgenstein 		pr_err("Couldn't change %s qp state to INIT (%d)\n",
1365fc06573dSJack Morgenstein 		       create_tun ? "tunnel" : "special", ret);
1366fc06573dSJack Morgenstein 		goto err_qp;
1367fc06573dSJack Morgenstein 	}
1368fc06573dSJack Morgenstein 	attr.qp_state = IB_QPS_RTR;
1369fc06573dSJack Morgenstein 	ret = ib_modify_qp(tun_qp->qp, &attr, IB_QP_STATE);
1370fc06573dSJack Morgenstein 	if (ret) {
1371fc06573dSJack Morgenstein 		pr_err("Couldn't change %s qp state to RTR (%d)\n",
1372fc06573dSJack Morgenstein 		       create_tun ? "tunnel" : "special", ret);
1373fc06573dSJack Morgenstein 		goto err_qp;
1374fc06573dSJack Morgenstein 	}
1375fc06573dSJack Morgenstein 	attr.qp_state = IB_QPS_RTS;
1376fc06573dSJack Morgenstein 	attr.sq_psn = 0;
1377fc06573dSJack Morgenstein 	ret = ib_modify_qp(tun_qp->qp, &attr, IB_QP_STATE | IB_QP_SQ_PSN);
1378fc06573dSJack Morgenstein 	if (ret) {
1379fc06573dSJack Morgenstein 		pr_err("Couldn't change %s qp state to RTS (%d)\n",
1380fc06573dSJack Morgenstein 		       create_tun ? "tunnel" : "special", ret);
1381fc06573dSJack Morgenstein 		goto err_qp;
1382fc06573dSJack Morgenstein 	}
1383fc06573dSJack Morgenstein 
1384fc06573dSJack Morgenstein 	for (i = 0; i < MLX4_NUM_TUNNEL_BUFS; i++) {
1385fc06573dSJack Morgenstein 		ret = mlx4_ib_post_pv_qp_buf(ctx, tun_qp, i);
1386fc06573dSJack Morgenstein 		if (ret) {
1387fc06573dSJack Morgenstein 			pr_err(" mlx4_ib_post_pv_buf error"
1388fc06573dSJack Morgenstein 			       " (err = %d, i = %d)\n", ret, i);
1389fc06573dSJack Morgenstein 			goto err_qp;
1390fc06573dSJack Morgenstein 		}
1391fc06573dSJack Morgenstein 	}
1392fc06573dSJack Morgenstein 	return 0;
1393fc06573dSJack Morgenstein 
1394fc06573dSJack Morgenstein err_qp:
1395fc06573dSJack Morgenstein 	ib_destroy_qp(tun_qp->qp);
1396fc06573dSJack Morgenstein 	tun_qp->qp = NULL;
1397fc06573dSJack Morgenstein 	return ret;
1398fc06573dSJack Morgenstein }
1399fc06573dSJack Morgenstein 
1400fc06573dSJack Morgenstein /*
1401fc06573dSJack Morgenstein  * IB MAD completion callback for real SQPs
1402fc06573dSJack Morgenstein  */
1403fc06573dSJack Morgenstein static void mlx4_ib_sqp_comp_worker(struct work_struct *work)
1404fc06573dSJack Morgenstein {
140537bfc7c1SJack Morgenstein 	struct mlx4_ib_demux_pv_ctx *ctx;
140637bfc7c1SJack Morgenstein 	struct mlx4_ib_demux_pv_qp *sqp;
140737bfc7c1SJack Morgenstein 	struct ib_wc wc;
140837bfc7c1SJack Morgenstein 	struct ib_grh *grh;
140937bfc7c1SJack Morgenstein 	struct ib_mad *mad;
141037bfc7c1SJack Morgenstein 
141137bfc7c1SJack Morgenstein 	ctx = container_of(work, struct mlx4_ib_demux_pv_ctx, work);
141237bfc7c1SJack Morgenstein 	ib_req_notify_cq(ctx->cq, IB_CQ_NEXT_COMP);
141337bfc7c1SJack Morgenstein 
141437bfc7c1SJack Morgenstein 	while (mlx4_ib_poll_cq(ctx->cq, 1, &wc) == 1) {
141537bfc7c1SJack Morgenstein 		sqp = &ctx->qp[MLX4_TUN_WRID_QPN(wc.wr_id)];
141637bfc7c1SJack Morgenstein 		if (wc.status == IB_WC_SUCCESS) {
141737bfc7c1SJack Morgenstein 			switch (wc.opcode) {
141837bfc7c1SJack Morgenstein 			case IB_WC_SEND:
141937bfc7c1SJack Morgenstein 				ib_destroy_ah(sqp->tx_ring[wc.wr_id &
142037bfc7c1SJack Morgenstein 					      (MLX4_NUM_TUNNEL_BUFS - 1)].ah);
142137bfc7c1SJack Morgenstein 				sqp->tx_ring[wc.wr_id & (MLX4_NUM_TUNNEL_BUFS - 1)].ah
142237bfc7c1SJack Morgenstein 					= NULL;
142337bfc7c1SJack Morgenstein 				spin_lock(&sqp->tx_lock);
142437bfc7c1SJack Morgenstein 				sqp->tx_ix_tail++;
142537bfc7c1SJack Morgenstein 				spin_unlock(&sqp->tx_lock);
142637bfc7c1SJack Morgenstein 				break;
142737bfc7c1SJack Morgenstein 			case IB_WC_RECV:
142837bfc7c1SJack Morgenstein 				mad = (struct ib_mad *) &(((struct mlx4_mad_rcv_buf *)
142937bfc7c1SJack Morgenstein 						(sqp->ring[wc.wr_id &
143037bfc7c1SJack Morgenstein 						(MLX4_NUM_TUNNEL_BUFS - 1)].addr))->payload);
143137bfc7c1SJack Morgenstein 				grh = &(((struct mlx4_mad_rcv_buf *)
143237bfc7c1SJack Morgenstein 						(sqp->ring[wc.wr_id &
143337bfc7c1SJack Morgenstein 						(MLX4_NUM_TUNNEL_BUFS - 1)].addr))->grh);
143437bfc7c1SJack Morgenstein 				mlx4_ib_demux_mad(ctx->ib_dev, ctx->port, &wc, grh, mad);
143537bfc7c1SJack Morgenstein 				if (mlx4_ib_post_pv_qp_buf(ctx, sqp, wc.wr_id &
143637bfc7c1SJack Morgenstein 							   (MLX4_NUM_TUNNEL_BUFS - 1)))
143737bfc7c1SJack Morgenstein 					pr_err("Failed reposting SQP "
143837bfc7c1SJack Morgenstein 					       "buf:%lld\n", wc.wr_id);
143937bfc7c1SJack Morgenstein 				break;
144037bfc7c1SJack Morgenstein 			default:
144137bfc7c1SJack Morgenstein 				BUG_ON(1);
144237bfc7c1SJack Morgenstein 				break;
144337bfc7c1SJack Morgenstein 			}
144437bfc7c1SJack Morgenstein 		} else  {
144537bfc7c1SJack Morgenstein 			pr_debug("mlx4_ib: completion error in tunnel: %d."
144637bfc7c1SJack Morgenstein 				 " status = %d, wrid = 0x%llx\n",
144737bfc7c1SJack Morgenstein 				 ctx->slave, wc.status, wc.wr_id);
144837bfc7c1SJack Morgenstein 			if (!MLX4_TUN_IS_RECV(wc.wr_id)) {
144937bfc7c1SJack Morgenstein 				ib_destroy_ah(sqp->tx_ring[wc.wr_id &
145037bfc7c1SJack Morgenstein 					      (MLX4_NUM_TUNNEL_BUFS - 1)].ah);
145137bfc7c1SJack Morgenstein 				sqp->tx_ring[wc.wr_id & (MLX4_NUM_TUNNEL_BUFS - 1)].ah
145237bfc7c1SJack Morgenstein 					= NULL;
145337bfc7c1SJack Morgenstein 				spin_lock(&sqp->tx_lock);
145437bfc7c1SJack Morgenstein 				sqp->tx_ix_tail++;
145537bfc7c1SJack Morgenstein 				spin_unlock(&sqp->tx_lock);
145637bfc7c1SJack Morgenstein 			}
145737bfc7c1SJack Morgenstein 		}
145837bfc7c1SJack Morgenstein 	}
1459fc06573dSJack Morgenstein }
1460fc06573dSJack Morgenstein 
1461fc06573dSJack Morgenstein static int alloc_pv_object(struct mlx4_ib_dev *dev, int slave, int port,
1462fc06573dSJack Morgenstein 			       struct mlx4_ib_demux_pv_ctx **ret_ctx)
1463fc06573dSJack Morgenstein {
1464fc06573dSJack Morgenstein 	struct mlx4_ib_demux_pv_ctx *ctx;
1465fc06573dSJack Morgenstein 
1466fc06573dSJack Morgenstein 	*ret_ctx = NULL;
1467fc06573dSJack Morgenstein 	ctx = kzalloc(sizeof (struct mlx4_ib_demux_pv_ctx), GFP_KERNEL);
1468fc06573dSJack Morgenstein 	if (!ctx) {
1469fc06573dSJack Morgenstein 		pr_err("failed allocating pv resource context "
1470fc06573dSJack Morgenstein 		       "for port %d, slave %d\n", port, slave);
1471fc06573dSJack Morgenstein 		return -ENOMEM;
1472fc06573dSJack Morgenstein 	}
1473fc06573dSJack Morgenstein 
1474fc06573dSJack Morgenstein 	ctx->ib_dev = &dev->ib_dev;
1475fc06573dSJack Morgenstein 	ctx->port = port;
1476fc06573dSJack Morgenstein 	ctx->slave = slave;
1477fc06573dSJack Morgenstein 	*ret_ctx = ctx;
1478fc06573dSJack Morgenstein 	return 0;
1479fc06573dSJack Morgenstein }
1480fc06573dSJack Morgenstein 
1481fc06573dSJack Morgenstein static void free_pv_object(struct mlx4_ib_dev *dev, int slave, int port)
1482fc06573dSJack Morgenstein {
1483fc06573dSJack Morgenstein 	if (dev->sriov.demux[port - 1].tun[slave]) {
1484fc06573dSJack Morgenstein 		kfree(dev->sriov.demux[port - 1].tun[slave]);
1485fc06573dSJack Morgenstein 		dev->sriov.demux[port - 1].tun[slave] = NULL;
1486fc06573dSJack Morgenstein 	}
1487fc06573dSJack Morgenstein }
1488fc06573dSJack Morgenstein 
1489fc06573dSJack Morgenstein static int create_pv_resources(struct ib_device *ibdev, int slave, int port,
1490fc06573dSJack Morgenstein 			       int create_tun, struct mlx4_ib_demux_pv_ctx *ctx)
1491fc06573dSJack Morgenstein {
1492fc06573dSJack Morgenstein 	int ret, cq_size;
1493fc06573dSJack Morgenstein 
1494fc06573dSJack Morgenstein 	ctx->state = DEMUX_PV_STATE_STARTING;
1495fc06573dSJack Morgenstein 	/* have QP0 only on port owner, and only if link layer is IB */
1496fc06573dSJack Morgenstein 	if (ctx->slave == mlx4_master_func_num(to_mdev(ctx->ib_dev)->dev) &&
1497fc06573dSJack Morgenstein 	    rdma_port_get_link_layer(ibdev, ctx->port) == IB_LINK_LAYER_INFINIBAND)
1498fc06573dSJack Morgenstein 		ctx->has_smi = 1;
1499fc06573dSJack Morgenstein 
1500fc06573dSJack Morgenstein 	if (ctx->has_smi) {
1501fc06573dSJack Morgenstein 		ret = mlx4_ib_alloc_pv_bufs(ctx, IB_QPT_SMI, create_tun);
1502fc06573dSJack Morgenstein 		if (ret) {
1503fc06573dSJack Morgenstein 			pr_err("Failed allocating qp0 tunnel bufs (%d)\n", ret);
1504fc06573dSJack Morgenstein 			goto err_out;
1505fc06573dSJack Morgenstein 		}
1506fc06573dSJack Morgenstein 	}
1507fc06573dSJack Morgenstein 
1508fc06573dSJack Morgenstein 	ret = mlx4_ib_alloc_pv_bufs(ctx, IB_QPT_GSI, create_tun);
1509fc06573dSJack Morgenstein 	if (ret) {
1510fc06573dSJack Morgenstein 		pr_err("Failed allocating qp1 tunnel bufs (%d)\n", ret);
1511fc06573dSJack Morgenstein 		goto err_out_qp0;
1512fc06573dSJack Morgenstein 	}
1513fc06573dSJack Morgenstein 
1514fc06573dSJack Morgenstein 	cq_size = 2 * MLX4_NUM_TUNNEL_BUFS;
1515fc06573dSJack Morgenstein 	if (ctx->has_smi)
1516fc06573dSJack Morgenstein 		cq_size *= 2;
1517fc06573dSJack Morgenstein 
1518fc06573dSJack Morgenstein 	ctx->cq = ib_create_cq(ctx->ib_dev, mlx4_ib_tunnel_comp_handler,
1519fc06573dSJack Morgenstein 			       NULL, ctx, cq_size, 0);
1520fc06573dSJack Morgenstein 	if (IS_ERR(ctx->cq)) {
1521fc06573dSJack Morgenstein 		ret = PTR_ERR(ctx->cq);
1522fc06573dSJack Morgenstein 		pr_err("Couldn't create tunnel CQ (%d)\n", ret);
1523fc06573dSJack Morgenstein 		goto err_buf;
1524fc06573dSJack Morgenstein 	}
1525fc06573dSJack Morgenstein 
1526fc06573dSJack Morgenstein 	ctx->pd = ib_alloc_pd(ctx->ib_dev);
1527fc06573dSJack Morgenstein 	if (IS_ERR(ctx->pd)) {
1528fc06573dSJack Morgenstein 		ret = PTR_ERR(ctx->pd);
1529fc06573dSJack Morgenstein 		pr_err("Couldn't create tunnel PD (%d)\n", ret);
1530fc06573dSJack Morgenstein 		goto err_cq;
1531fc06573dSJack Morgenstein 	}
1532fc06573dSJack Morgenstein 
1533fc06573dSJack Morgenstein 	ctx->mr = ib_get_dma_mr(ctx->pd, IB_ACCESS_LOCAL_WRITE);
1534fc06573dSJack Morgenstein 	if (IS_ERR(ctx->mr)) {
1535fc06573dSJack Morgenstein 		ret = PTR_ERR(ctx->mr);
1536fc06573dSJack Morgenstein 		pr_err("Couldn't get tunnel DMA MR (%d)\n", ret);
1537fc06573dSJack Morgenstein 		goto err_pd;
1538fc06573dSJack Morgenstein 	}
1539fc06573dSJack Morgenstein 
1540fc06573dSJack Morgenstein 	if (ctx->has_smi) {
1541fc06573dSJack Morgenstein 		ret = create_pv_sqp(ctx, IB_QPT_SMI, create_tun);
1542fc06573dSJack Morgenstein 		if (ret) {
1543fc06573dSJack Morgenstein 			pr_err("Couldn't create %s QP0 (%d)\n",
1544fc06573dSJack Morgenstein 			       create_tun ? "tunnel for" : "",  ret);
1545fc06573dSJack Morgenstein 			goto err_mr;
1546fc06573dSJack Morgenstein 		}
1547fc06573dSJack Morgenstein 	}
1548fc06573dSJack Morgenstein 
1549fc06573dSJack Morgenstein 	ret = create_pv_sqp(ctx, IB_QPT_GSI, create_tun);
1550fc06573dSJack Morgenstein 	if (ret) {
1551fc06573dSJack Morgenstein 		pr_err("Couldn't create %s QP1 (%d)\n",
1552fc06573dSJack Morgenstein 		       create_tun ? "tunnel for" : "",  ret);
1553fc06573dSJack Morgenstein 		goto err_qp0;
1554fc06573dSJack Morgenstein 	}
1555fc06573dSJack Morgenstein 
1556fc06573dSJack Morgenstein 	if (create_tun)
1557fc06573dSJack Morgenstein 		INIT_WORK(&ctx->work, mlx4_ib_tunnel_comp_worker);
1558fc06573dSJack Morgenstein 	else
1559fc06573dSJack Morgenstein 		INIT_WORK(&ctx->work, mlx4_ib_sqp_comp_worker);
1560fc06573dSJack Morgenstein 
1561fc06573dSJack Morgenstein 	ctx->wq = to_mdev(ibdev)->sriov.demux[port - 1].wq;
1562fc06573dSJack Morgenstein 
1563fc06573dSJack Morgenstein 	ret = ib_req_notify_cq(ctx->cq, IB_CQ_NEXT_COMP);
1564fc06573dSJack Morgenstein 	if (ret) {
1565fc06573dSJack Morgenstein 		pr_err("Couldn't arm tunnel cq (%d)\n", ret);
1566fc06573dSJack Morgenstein 		goto err_wq;
1567fc06573dSJack Morgenstein 	}
1568fc06573dSJack Morgenstein 	ctx->state = DEMUX_PV_STATE_ACTIVE;
1569fc06573dSJack Morgenstein 	return 0;
1570fc06573dSJack Morgenstein 
1571fc06573dSJack Morgenstein err_wq:
1572fc06573dSJack Morgenstein 	ctx->wq = NULL;
1573fc06573dSJack Morgenstein 	ib_destroy_qp(ctx->qp[1].qp);
1574fc06573dSJack Morgenstein 	ctx->qp[1].qp = NULL;
1575fc06573dSJack Morgenstein 
1576fc06573dSJack Morgenstein 
1577fc06573dSJack Morgenstein err_qp0:
1578fc06573dSJack Morgenstein 	if (ctx->has_smi)
1579fc06573dSJack Morgenstein 		ib_destroy_qp(ctx->qp[0].qp);
1580fc06573dSJack Morgenstein 	ctx->qp[0].qp = NULL;
1581fc06573dSJack Morgenstein 
1582fc06573dSJack Morgenstein err_mr:
1583fc06573dSJack Morgenstein 	ib_dereg_mr(ctx->mr);
1584fc06573dSJack Morgenstein 	ctx->mr = NULL;
1585fc06573dSJack Morgenstein 
1586fc06573dSJack Morgenstein err_pd:
1587fc06573dSJack Morgenstein 	ib_dealloc_pd(ctx->pd);
1588fc06573dSJack Morgenstein 	ctx->pd = NULL;
1589fc06573dSJack Morgenstein 
1590fc06573dSJack Morgenstein err_cq:
1591fc06573dSJack Morgenstein 	ib_destroy_cq(ctx->cq);
1592fc06573dSJack Morgenstein 	ctx->cq = NULL;
1593fc06573dSJack Morgenstein 
1594fc06573dSJack Morgenstein err_buf:
1595fc06573dSJack Morgenstein 	mlx4_ib_free_pv_qp_bufs(ctx, IB_QPT_GSI, create_tun);
1596fc06573dSJack Morgenstein 
1597fc06573dSJack Morgenstein err_out_qp0:
1598fc06573dSJack Morgenstein 	if (ctx->has_smi)
1599fc06573dSJack Morgenstein 		mlx4_ib_free_pv_qp_bufs(ctx, IB_QPT_SMI, create_tun);
1600fc06573dSJack Morgenstein err_out:
1601fc06573dSJack Morgenstein 	ctx->state = DEMUX_PV_STATE_DOWN;
1602fc06573dSJack Morgenstein 	return ret;
1603fc06573dSJack Morgenstein }
1604fc06573dSJack Morgenstein 
1605fc06573dSJack Morgenstein static void destroy_pv_resources(struct mlx4_ib_dev *dev, int slave, int port,
1606fc06573dSJack Morgenstein 				 struct mlx4_ib_demux_pv_ctx *ctx, int flush)
1607fc06573dSJack Morgenstein {
1608fc06573dSJack Morgenstein 	if (!ctx)
1609fc06573dSJack Morgenstein 		return;
1610fc06573dSJack Morgenstein 	if (ctx->state > DEMUX_PV_STATE_DOWN) {
1611fc06573dSJack Morgenstein 		ctx->state = DEMUX_PV_STATE_DOWNING;
1612fc06573dSJack Morgenstein 		if (flush)
1613fc06573dSJack Morgenstein 			flush_workqueue(ctx->wq);
1614fc06573dSJack Morgenstein 		if (ctx->has_smi) {
1615fc06573dSJack Morgenstein 			ib_destroy_qp(ctx->qp[0].qp);
1616fc06573dSJack Morgenstein 			ctx->qp[0].qp = NULL;
1617fc06573dSJack Morgenstein 			mlx4_ib_free_pv_qp_bufs(ctx, IB_QPT_SMI, 1);
1618fc06573dSJack Morgenstein 		}
1619fc06573dSJack Morgenstein 		ib_destroy_qp(ctx->qp[1].qp);
1620fc06573dSJack Morgenstein 		ctx->qp[1].qp = NULL;
1621fc06573dSJack Morgenstein 		mlx4_ib_free_pv_qp_bufs(ctx, IB_QPT_GSI, 1);
1622fc06573dSJack Morgenstein 		ib_dereg_mr(ctx->mr);
1623fc06573dSJack Morgenstein 		ctx->mr = NULL;
1624fc06573dSJack Morgenstein 		ib_dealloc_pd(ctx->pd);
1625fc06573dSJack Morgenstein 		ctx->pd = NULL;
1626fc06573dSJack Morgenstein 		ib_destroy_cq(ctx->cq);
1627fc06573dSJack Morgenstein 		ctx->cq = NULL;
1628fc06573dSJack Morgenstein 		ctx->state = DEMUX_PV_STATE_DOWN;
1629fc06573dSJack Morgenstein 	}
1630fc06573dSJack Morgenstein }
1631fc06573dSJack Morgenstein 
1632fc06573dSJack Morgenstein static int mlx4_ib_tunnels_update(struct mlx4_ib_dev *dev, int slave,
1633fc06573dSJack Morgenstein 				  int port, int do_init)
1634fc06573dSJack Morgenstein {
1635fc06573dSJack Morgenstein 	int ret = 0;
1636fc06573dSJack Morgenstein 
1637fc06573dSJack Morgenstein 	if (!do_init) {
1638b9c5d6a6SOren Duer 		clean_vf_mcast(&dev->sriov.demux[port - 1], slave);
1639fc06573dSJack Morgenstein 		/* for master, destroy real sqp resources */
1640fc06573dSJack Morgenstein 		if (slave == mlx4_master_func_num(dev->dev))
1641fc06573dSJack Morgenstein 			destroy_pv_resources(dev, slave, port,
1642fc06573dSJack Morgenstein 					     dev->sriov.sqps[port - 1], 1);
1643fc06573dSJack Morgenstein 		/* destroy the tunnel qp resources */
1644fc06573dSJack Morgenstein 		destroy_pv_resources(dev, slave, port,
1645fc06573dSJack Morgenstein 				     dev->sriov.demux[port - 1].tun[slave], 1);
1646fc06573dSJack Morgenstein 		return 0;
1647fc06573dSJack Morgenstein 	}
1648fc06573dSJack Morgenstein 
1649fc06573dSJack Morgenstein 	/* create the tunnel qp resources */
1650fc06573dSJack Morgenstein 	ret = create_pv_resources(&dev->ib_dev, slave, port, 1,
1651fc06573dSJack Morgenstein 				  dev->sriov.demux[port - 1].tun[slave]);
1652fc06573dSJack Morgenstein 
1653fc06573dSJack Morgenstein 	/* for master, create the real sqp resources */
1654fc06573dSJack Morgenstein 	if (!ret && slave == mlx4_master_func_num(dev->dev))
1655fc06573dSJack Morgenstein 		ret = create_pv_resources(&dev->ib_dev, slave, port, 0,
1656fc06573dSJack Morgenstein 					  dev->sriov.sqps[port - 1]);
1657fc06573dSJack Morgenstein 	return ret;
1658fc06573dSJack Morgenstein }
1659fc06573dSJack Morgenstein 
1660fc06573dSJack Morgenstein void mlx4_ib_tunnels_update_work(struct work_struct *work)
1661fc06573dSJack Morgenstein {
1662fc06573dSJack Morgenstein 	struct mlx4_ib_demux_work *dmxw;
1663fc06573dSJack Morgenstein 
1664fc06573dSJack Morgenstein 	dmxw = container_of(work, struct mlx4_ib_demux_work, work);
1665fc06573dSJack Morgenstein 	mlx4_ib_tunnels_update(dmxw->dev, dmxw->slave, (int) dmxw->port,
1666fc06573dSJack Morgenstein 			       dmxw->do_init);
1667fc06573dSJack Morgenstein 	kfree(dmxw);
1668fc06573dSJack Morgenstein 	return;
1669fc06573dSJack Morgenstein }
1670fc06573dSJack Morgenstein 
1671fc06573dSJack Morgenstein static int mlx4_ib_alloc_demux_ctx(struct mlx4_ib_dev *dev,
1672fc06573dSJack Morgenstein 				       struct mlx4_ib_demux_ctx *ctx,
1673fc06573dSJack Morgenstein 				       int port)
1674fc06573dSJack Morgenstein {
1675fc06573dSJack Morgenstein 	char name[12];
1676fc06573dSJack Morgenstein 	int ret = 0;
1677fc06573dSJack Morgenstein 	int i;
1678fc06573dSJack Morgenstein 
1679fc06573dSJack Morgenstein 	ctx->tun = kcalloc(dev->dev->caps.sqp_demux,
1680fc06573dSJack Morgenstein 			   sizeof (struct mlx4_ib_demux_pv_ctx *), GFP_KERNEL);
1681fc06573dSJack Morgenstein 	if (!ctx->tun)
1682fc06573dSJack Morgenstein 		return -ENOMEM;
1683fc06573dSJack Morgenstein 
1684fc06573dSJack Morgenstein 	ctx->dev = dev;
1685fc06573dSJack Morgenstein 	ctx->port = port;
1686fc06573dSJack Morgenstein 	ctx->ib_dev = &dev->ib_dev;
1687fc06573dSJack Morgenstein 
1688fc06573dSJack Morgenstein 	for (i = 0; i < dev->dev->caps.sqp_demux; i++) {
1689fc06573dSJack Morgenstein 		ret = alloc_pv_object(dev, i, port, &ctx->tun[i]);
1690fc06573dSJack Morgenstein 		if (ret) {
1691fc06573dSJack Morgenstein 			ret = -ENOMEM;
1692b9c5d6a6SOren Duer 			goto err_mcg;
1693fc06573dSJack Morgenstein 		}
1694fc06573dSJack Morgenstein 	}
1695fc06573dSJack Morgenstein 
1696b9c5d6a6SOren Duer 	ret = mlx4_ib_mcg_port_init(ctx);
1697b9c5d6a6SOren Duer 	if (ret) {
1698b9c5d6a6SOren Duer 		pr_err("Failed initializing mcg para-virt (%d)\n", ret);
1699b9c5d6a6SOren Duer 		goto err_mcg;
1700b9c5d6a6SOren Duer 	}
1701b9c5d6a6SOren Duer 
1702fc06573dSJack Morgenstein 	snprintf(name, sizeof name, "mlx4_ibt%d", port);
1703fc06573dSJack Morgenstein 	ctx->wq = create_singlethread_workqueue(name);
1704fc06573dSJack Morgenstein 	if (!ctx->wq) {
1705fc06573dSJack Morgenstein 		pr_err("Failed to create tunnelling WQ for port %d\n", port);
1706fc06573dSJack Morgenstein 		ret = -ENOMEM;
1707fc06573dSJack Morgenstein 		goto err_wq;
1708fc06573dSJack Morgenstein 	}
1709fc06573dSJack Morgenstein 
1710fc06573dSJack Morgenstein 	snprintf(name, sizeof name, "mlx4_ibud%d", port);
1711fc06573dSJack Morgenstein 	ctx->ud_wq = create_singlethread_workqueue(name);
1712fc06573dSJack Morgenstein 	if (!ctx->ud_wq) {
1713fc06573dSJack Morgenstein 		pr_err("Failed to create up/down WQ for port %d\n", port);
1714fc06573dSJack Morgenstein 		ret = -ENOMEM;
1715fc06573dSJack Morgenstein 		goto err_udwq;
1716fc06573dSJack Morgenstein 	}
1717fc06573dSJack Morgenstein 
1718fc06573dSJack Morgenstein 	return 0;
1719fc06573dSJack Morgenstein 
1720fc06573dSJack Morgenstein err_udwq:
1721fc06573dSJack Morgenstein 	destroy_workqueue(ctx->wq);
1722fc06573dSJack Morgenstein 	ctx->wq = NULL;
1723fc06573dSJack Morgenstein 
1724fc06573dSJack Morgenstein err_wq:
1725b9c5d6a6SOren Duer 	mlx4_ib_mcg_port_cleanup(ctx, 1);
1726b9c5d6a6SOren Duer err_mcg:
1727fc06573dSJack Morgenstein 	for (i = 0; i < dev->dev->caps.sqp_demux; i++)
1728fc06573dSJack Morgenstein 		free_pv_object(dev, i, port);
1729fc06573dSJack Morgenstein 	kfree(ctx->tun);
1730fc06573dSJack Morgenstein 	ctx->tun = NULL;
1731fc06573dSJack Morgenstein 	return ret;
1732fc06573dSJack Morgenstein }
1733fc06573dSJack Morgenstein 
1734fc06573dSJack Morgenstein static void mlx4_ib_free_sqp_ctx(struct mlx4_ib_demux_pv_ctx *sqp_ctx)
1735fc06573dSJack Morgenstein {
1736fc06573dSJack Morgenstein 	if (sqp_ctx->state > DEMUX_PV_STATE_DOWN) {
1737fc06573dSJack Morgenstein 		sqp_ctx->state = DEMUX_PV_STATE_DOWNING;
1738fc06573dSJack Morgenstein 		flush_workqueue(sqp_ctx->wq);
1739fc06573dSJack Morgenstein 		if (sqp_ctx->has_smi) {
1740fc06573dSJack Morgenstein 			ib_destroy_qp(sqp_ctx->qp[0].qp);
1741fc06573dSJack Morgenstein 			sqp_ctx->qp[0].qp = NULL;
1742fc06573dSJack Morgenstein 			mlx4_ib_free_pv_qp_bufs(sqp_ctx, IB_QPT_SMI, 0);
1743fc06573dSJack Morgenstein 		}
1744fc06573dSJack Morgenstein 		ib_destroy_qp(sqp_ctx->qp[1].qp);
1745fc06573dSJack Morgenstein 		sqp_ctx->qp[1].qp = NULL;
1746fc06573dSJack Morgenstein 		mlx4_ib_free_pv_qp_bufs(sqp_ctx, IB_QPT_GSI, 0);
1747fc06573dSJack Morgenstein 		ib_dereg_mr(sqp_ctx->mr);
1748fc06573dSJack Morgenstein 		sqp_ctx->mr = NULL;
1749fc06573dSJack Morgenstein 		ib_dealloc_pd(sqp_ctx->pd);
1750fc06573dSJack Morgenstein 		sqp_ctx->pd = NULL;
1751fc06573dSJack Morgenstein 		ib_destroy_cq(sqp_ctx->cq);
1752fc06573dSJack Morgenstein 		sqp_ctx->cq = NULL;
1753fc06573dSJack Morgenstein 		sqp_ctx->state = DEMUX_PV_STATE_DOWN;
1754fc06573dSJack Morgenstein 	}
1755fc06573dSJack Morgenstein }
1756fc06573dSJack Morgenstein 
1757fc06573dSJack Morgenstein static void mlx4_ib_free_demux_ctx(struct mlx4_ib_demux_ctx *ctx)
1758fc06573dSJack Morgenstein {
1759fc06573dSJack Morgenstein 	int i;
1760fc06573dSJack Morgenstein 	if (ctx) {
1761fc06573dSJack Morgenstein 		struct mlx4_ib_dev *dev = to_mdev(ctx->ib_dev);
1762b9c5d6a6SOren Duer 		mlx4_ib_mcg_port_cleanup(ctx, 1);
1763fc06573dSJack Morgenstein 		for (i = 0; i < dev->dev->caps.sqp_demux; i++) {
1764fc06573dSJack Morgenstein 			if (!ctx->tun[i])
1765fc06573dSJack Morgenstein 				continue;
1766fc06573dSJack Morgenstein 			if (ctx->tun[i]->state > DEMUX_PV_STATE_DOWN)
1767fc06573dSJack Morgenstein 				ctx->tun[i]->state = DEMUX_PV_STATE_DOWNING;
1768fc06573dSJack Morgenstein 		}
1769fc06573dSJack Morgenstein 		flush_workqueue(ctx->wq);
1770fc06573dSJack Morgenstein 		for (i = 0; i < dev->dev->caps.sqp_demux; i++) {
1771fc06573dSJack Morgenstein 			destroy_pv_resources(dev, i, ctx->port, ctx->tun[i], 0);
1772fc06573dSJack Morgenstein 			free_pv_object(dev, i, ctx->port);
1773fc06573dSJack Morgenstein 		}
1774fc06573dSJack Morgenstein 		kfree(ctx->tun);
1775fc06573dSJack Morgenstein 		destroy_workqueue(ctx->ud_wq);
1776fc06573dSJack Morgenstein 		destroy_workqueue(ctx->wq);
1777fc06573dSJack Morgenstein 	}
1778fc06573dSJack Morgenstein }
1779fc06573dSJack Morgenstein 
1780fc06573dSJack Morgenstein static void mlx4_ib_master_tunnels(struct mlx4_ib_dev *dev, int do_init)
1781fc06573dSJack Morgenstein {
1782fc06573dSJack Morgenstein 	int i;
1783fc06573dSJack Morgenstein 
1784fc06573dSJack Morgenstein 	if (!mlx4_is_master(dev->dev))
1785fc06573dSJack Morgenstein 		return;
1786fc06573dSJack Morgenstein 	/* initialize or tear down tunnel QPs for the master */
1787fc06573dSJack Morgenstein 	for (i = 0; i < dev->dev->caps.num_ports; i++)
1788fc06573dSJack Morgenstein 		mlx4_ib_tunnels_update(dev, mlx4_master_func_num(dev->dev), i + 1, do_init);
1789fc06573dSJack Morgenstein 	return;
1790fc06573dSJack Morgenstein }
1791fc06573dSJack Morgenstein 
1792fc06573dSJack Morgenstein int mlx4_ib_init_sriov(struct mlx4_ib_dev *dev)
1793fc06573dSJack Morgenstein {
1794fc06573dSJack Morgenstein 	int i = 0;
1795fc06573dSJack Morgenstein 	int err;
1796fc06573dSJack Morgenstein 
1797fc06573dSJack Morgenstein 	if (!mlx4_is_mfunc(dev->dev))
1798fc06573dSJack Morgenstein 		return 0;
1799fc06573dSJack Morgenstein 
1800fc06573dSJack Morgenstein 	dev->sriov.is_going_down = 0;
1801fc06573dSJack Morgenstein 	spin_lock_init(&dev->sriov.going_down_lock);
1802*3cf69cc8SAmir Vadai 	mlx4_ib_cm_paravirt_init(dev);
1803fc06573dSJack Morgenstein 
1804fc06573dSJack Morgenstein 	mlx4_ib_warn(&dev->ib_dev, "multi-function enabled\n");
1805fc06573dSJack Morgenstein 
1806fc06573dSJack Morgenstein 	if (mlx4_is_slave(dev->dev)) {
1807fc06573dSJack Morgenstein 		mlx4_ib_warn(&dev->ib_dev, "operating in qp1 tunnel mode\n");
1808fc06573dSJack Morgenstein 		return 0;
1809fc06573dSJack Morgenstein 	}
1810fc06573dSJack Morgenstein 
1811fc06573dSJack Morgenstein 	mlx4_ib_warn(&dev->ib_dev, "initializing demux service for %d qp1 clients\n",
1812fc06573dSJack Morgenstein 		     dev->dev->caps.sqp_demux);
1813fc06573dSJack Morgenstein 	for (i = 0; i < dev->num_ports; i++) {
1814fc06573dSJack Morgenstein 		err = alloc_pv_object(dev, mlx4_master_func_num(dev->dev), i + 1,
1815fc06573dSJack Morgenstein 				      &dev->sriov.sqps[i]);
1816fc06573dSJack Morgenstein 		if (err)
1817fc06573dSJack Morgenstein 			goto demux_err;
1818fc06573dSJack Morgenstein 		err = mlx4_ib_alloc_demux_ctx(dev, &dev->sriov.demux[i], i + 1);
1819fc06573dSJack Morgenstein 		if (err)
1820fc06573dSJack Morgenstein 			goto demux_err;
1821fc06573dSJack Morgenstein 	}
1822fc06573dSJack Morgenstein 	mlx4_ib_master_tunnels(dev, 1);
1823fc06573dSJack Morgenstein 	return 0;
1824fc06573dSJack Morgenstein 
1825fc06573dSJack Morgenstein demux_err:
1826fc06573dSJack Morgenstein 	while (i > 0) {
1827fc06573dSJack Morgenstein 		free_pv_object(dev, mlx4_master_func_num(dev->dev), i + 1);
1828fc06573dSJack Morgenstein 		mlx4_ib_free_demux_ctx(&dev->sriov.demux[i]);
1829fc06573dSJack Morgenstein 		--i;
1830fc06573dSJack Morgenstein 	}
1831*3cf69cc8SAmir Vadai 	mlx4_ib_cm_paravirt_clean(dev, -1);
1832fc06573dSJack Morgenstein 
1833fc06573dSJack Morgenstein 	return err;
1834fc06573dSJack Morgenstein }
1835fc06573dSJack Morgenstein 
1836fc06573dSJack Morgenstein void mlx4_ib_close_sriov(struct mlx4_ib_dev *dev)
1837fc06573dSJack Morgenstein {
1838fc06573dSJack Morgenstein 	int i;
1839fc06573dSJack Morgenstein 	unsigned long flags;
1840fc06573dSJack Morgenstein 
1841fc06573dSJack Morgenstein 	if (!mlx4_is_mfunc(dev->dev))
1842fc06573dSJack Morgenstein 		return;
1843fc06573dSJack Morgenstein 
1844fc06573dSJack Morgenstein 	spin_lock_irqsave(&dev->sriov.going_down_lock, flags);
1845fc06573dSJack Morgenstein 	dev->sriov.is_going_down = 1;
1846fc06573dSJack Morgenstein 	spin_unlock_irqrestore(&dev->sriov.going_down_lock, flags);
1847*3cf69cc8SAmir Vadai 	if (mlx4_is_master(dev->dev)) {
1848fc06573dSJack Morgenstein 		for (i = 0; i < dev->num_ports; i++) {
1849fc06573dSJack Morgenstein 			flush_workqueue(dev->sriov.demux[i].ud_wq);
1850fc06573dSJack Morgenstein 			mlx4_ib_free_sqp_ctx(dev->sriov.sqps[i]);
1851fc06573dSJack Morgenstein 			kfree(dev->sriov.sqps[i]);
1852fc06573dSJack Morgenstein 			dev->sriov.sqps[i] = NULL;
1853fc06573dSJack Morgenstein 			mlx4_ib_free_demux_ctx(&dev->sriov.demux[i]);
1854fc06573dSJack Morgenstein 		}
1855*3cf69cc8SAmir Vadai 
1856*3cf69cc8SAmir Vadai 		mlx4_ib_cm_paravirt_clean(dev, -1);
1857*3cf69cc8SAmir Vadai 	}
1858fc06573dSJack Morgenstein }
1859