xref: /openbmc/linux/drivers/infiniband/hw/mlx4/mad.c (revision 3ba8e31d5a4343fa042c976a9ce9c3c16946c92d)
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 
38afa8fd1dSJack Morgenstein #include <linux/random.h>
39225c7b1fSRoland Dreier #include <linux/mlx4/cmd.h>
405a0e3ad6STejun Heo #include <linux/gfp.h>
41c3779134SOr Gerlitz #include <rdma/ib_pma.h>
42225c7b1fSRoland Dreier 
43225c7b1fSRoland Dreier #include "mlx4_ib.h"
44225c7b1fSRoland Dreier 
45225c7b1fSRoland Dreier enum {
46225c7b1fSRoland Dreier 	MLX4_IB_VENDOR_CLASS1 = 0x9,
47225c7b1fSRoland Dreier 	MLX4_IB_VENDOR_CLASS2 = 0xa
48225c7b1fSRoland Dreier };
49225c7b1fSRoland Dreier 
50fc06573dSJack Morgenstein #define MLX4_TUN_SEND_WRID_SHIFT 34
51fc06573dSJack Morgenstein #define MLX4_TUN_QPN_SHIFT 32
52fc06573dSJack Morgenstein #define MLX4_TUN_WRID_RECV (((u64) 1) << MLX4_TUN_SEND_WRID_SHIFT)
53fc06573dSJack Morgenstein #define MLX4_TUN_SET_WRID_QPN(a) (((u64) ((a) & 0x3)) << MLX4_TUN_QPN_SHIFT)
54fc06573dSJack Morgenstein 
55fc06573dSJack Morgenstein #define MLX4_TUN_IS_RECV(a)  (((a) >>  MLX4_TUN_SEND_WRID_SHIFT) & 0x1)
56fc06573dSJack Morgenstein #define MLX4_TUN_WRID_QPN(a) (((a) >> MLX4_TUN_QPN_SHIFT) & 0x3)
57fc06573dSJack Morgenstein 
582a4fae14SJack Morgenstein  /* Port mgmt change event handling */
592a4fae14SJack Morgenstein 
602a4fae14SJack Morgenstein #define GET_BLK_PTR_FROM_EQE(eqe) be32_to_cpu(eqe->event.port_mgmt_change.params.tbl_change_info.block_ptr)
612a4fae14SJack Morgenstein #define GET_MASK_FROM_EQE(eqe) be32_to_cpu(eqe->event.port_mgmt_change.params.tbl_change_info.tbl_entries_mask)
622a4fae14SJack Morgenstein #define NUM_IDX_IN_PKEY_TBL_BLK 32
632a4fae14SJack Morgenstein #define GUID_TBL_ENTRY_SIZE 8	   /* size in bytes */
642a4fae14SJack Morgenstein #define GUID_TBL_BLK_NUM_ENTRIES 8
652a4fae14SJack Morgenstein #define GUID_TBL_BLK_SIZE (GUID_TBL_ENTRY_SIZE * GUID_TBL_BLK_NUM_ENTRIES)
662a4fae14SJack Morgenstein 
67fc06573dSJack Morgenstein struct mlx4_mad_rcv_buf {
68fc06573dSJack Morgenstein 	struct ib_grh grh;
69fc06573dSJack Morgenstein 	u8 payload[256];
70fc06573dSJack Morgenstein } __packed;
71fc06573dSJack Morgenstein 
72fc06573dSJack Morgenstein struct mlx4_mad_snd_buf {
73fc06573dSJack Morgenstein 	u8 payload[256];
74fc06573dSJack Morgenstein } __packed;
75fc06573dSJack Morgenstein 
76fc06573dSJack Morgenstein struct mlx4_tunnel_mad {
77fc06573dSJack Morgenstein 	struct ib_grh grh;
78fc06573dSJack Morgenstein 	struct mlx4_ib_tunnel_header hdr;
79fc06573dSJack Morgenstein 	struct ib_mad mad;
80fc06573dSJack Morgenstein } __packed;
81fc06573dSJack Morgenstein 
82fc06573dSJack Morgenstein struct mlx4_rcv_tunnel_mad {
83fc06573dSJack Morgenstein 	struct mlx4_rcv_tunnel_hdr hdr;
84fc06573dSJack Morgenstein 	struct ib_grh grh;
85fc06573dSJack Morgenstein 	struct ib_mad mad;
86fc06573dSJack Morgenstein } __packed;
87fc06573dSJack Morgenstein 
88b9c5d6a6SOren Duer static void handle_client_rereg_event(struct mlx4_ib_dev *dev, u8 port_num);
892a4fae14SJack Morgenstein static void handle_lid_change_event(struct mlx4_ib_dev *dev, u8 port_num);
902a4fae14SJack Morgenstein static void __propagate_pkey_ev(struct mlx4_ib_dev *dev, int port_num,
912a4fae14SJack Morgenstein 				int block, u32 change_bitmap);
92b9c5d6a6SOren Duer 
93afa8fd1dSJack Morgenstein __be64 mlx4_ib_gen_node_guid(void)
94afa8fd1dSJack Morgenstein {
95afa8fd1dSJack Morgenstein #define NODE_GUID_HI	((u64) (((u64)IB_OPENIB_OUI) << 40))
9650bea5c0SAndrew Morton 	return cpu_to_be64(NODE_GUID_HI | prandom_u32());
97afa8fd1dSJack Morgenstein }
98afa8fd1dSJack Morgenstein 
99b9c5d6a6SOren Duer __be64 mlx4_ib_get_new_demux_tid(struct mlx4_ib_demux_ctx *ctx)
100b9c5d6a6SOren Duer {
101b9c5d6a6SOren Duer 	return cpu_to_be64(atomic_inc_return(&ctx->tid)) |
102b9c5d6a6SOren Duer 		cpu_to_be64(0xff00000000000000LL);
103b9c5d6a6SOren Duer }
104b9c5d6a6SOren Duer 
1050a9a0188SJack Morgenstein int mlx4_MAD_IFC(struct mlx4_ib_dev *dev, int mad_ifc_flags,
106a97e2d86SIra Weiny 		 int port, const struct ib_wc *in_wc,
107a97e2d86SIra Weiny 		 const struct ib_grh *in_grh,
108a97e2d86SIra Weiny 		 const void *in_mad, void *response_mad)
109225c7b1fSRoland Dreier {
110225c7b1fSRoland Dreier 	struct mlx4_cmd_mailbox *inmailbox, *outmailbox;
111225c7b1fSRoland Dreier 	void *inbox;
112225c7b1fSRoland Dreier 	int err;
113225c7b1fSRoland Dreier 	u32 in_modifier = port;
114225c7b1fSRoland Dreier 	u8 op_modifier = 0;
115225c7b1fSRoland Dreier 
116225c7b1fSRoland Dreier 	inmailbox = mlx4_alloc_cmd_mailbox(dev->dev);
117225c7b1fSRoland Dreier 	if (IS_ERR(inmailbox))
118225c7b1fSRoland Dreier 		return PTR_ERR(inmailbox);
119225c7b1fSRoland Dreier 	inbox = inmailbox->buf;
120225c7b1fSRoland Dreier 
121225c7b1fSRoland Dreier 	outmailbox = mlx4_alloc_cmd_mailbox(dev->dev);
122225c7b1fSRoland Dreier 	if (IS_ERR(outmailbox)) {
123225c7b1fSRoland Dreier 		mlx4_free_cmd_mailbox(dev->dev, inmailbox);
124225c7b1fSRoland Dreier 		return PTR_ERR(outmailbox);
125225c7b1fSRoland Dreier 	}
126225c7b1fSRoland Dreier 
127225c7b1fSRoland Dreier 	memcpy(inbox, in_mad, 256);
128225c7b1fSRoland Dreier 
129225c7b1fSRoland Dreier 	/*
130225c7b1fSRoland Dreier 	 * Key check traps can't be generated unless we have in_wc to
131225c7b1fSRoland Dreier 	 * tell us where to send the trap.
132225c7b1fSRoland Dreier 	 */
1330a9a0188SJack Morgenstein 	if ((mad_ifc_flags & MLX4_MAD_IFC_IGNORE_MKEY) || !in_wc)
134225c7b1fSRoland Dreier 		op_modifier |= 0x1;
1350a9a0188SJack Morgenstein 	if ((mad_ifc_flags & MLX4_MAD_IFC_IGNORE_BKEY) || !in_wc)
136225c7b1fSRoland Dreier 		op_modifier |= 0x2;
1370a9a0188SJack Morgenstein 	if (mlx4_is_mfunc(dev->dev) &&
1380a9a0188SJack Morgenstein 	    (mad_ifc_flags & MLX4_MAD_IFC_NET_VIEW || in_wc))
1390a9a0188SJack Morgenstein 		op_modifier |= 0x8;
140225c7b1fSRoland Dreier 
141225c7b1fSRoland Dreier 	if (in_wc) {
142225c7b1fSRoland Dreier 		struct {
143225c7b1fSRoland Dreier 			__be32		my_qpn;
144225c7b1fSRoland Dreier 			u32		reserved1;
145225c7b1fSRoland Dreier 			__be32		rqpn;
146225c7b1fSRoland Dreier 			u8		sl;
147225c7b1fSRoland Dreier 			u8		g_path;
148225c7b1fSRoland Dreier 			u16		reserved2[2];
149225c7b1fSRoland Dreier 			__be16		pkey;
150225c7b1fSRoland Dreier 			u32		reserved3[11];
151225c7b1fSRoland Dreier 			u8		grh[40];
152225c7b1fSRoland Dreier 		} *ext_info;
153225c7b1fSRoland Dreier 
154225c7b1fSRoland Dreier 		memset(inbox + 256, 0, 256);
155225c7b1fSRoland Dreier 		ext_info = inbox + 256;
156225c7b1fSRoland Dreier 
157225c7b1fSRoland Dreier 		ext_info->my_qpn = cpu_to_be32(in_wc->qp->qp_num);
158225c7b1fSRoland Dreier 		ext_info->rqpn   = cpu_to_be32(in_wc->src_qp);
159225c7b1fSRoland Dreier 		ext_info->sl     = in_wc->sl << 4;
160225c7b1fSRoland Dreier 		ext_info->g_path = in_wc->dlid_path_bits |
161225c7b1fSRoland Dreier 			(in_wc->wc_flags & IB_WC_GRH ? 0x80 : 0);
162225c7b1fSRoland Dreier 		ext_info->pkey   = cpu_to_be16(in_wc->pkey_index);
163225c7b1fSRoland Dreier 
164225c7b1fSRoland Dreier 		if (in_grh)
165225c7b1fSRoland Dreier 			memcpy(ext_info->grh, in_grh, 40);
166225c7b1fSRoland Dreier 
167225c7b1fSRoland Dreier 		op_modifier |= 0x4;
168225c7b1fSRoland Dreier 
169225c7b1fSRoland Dreier 		in_modifier |= in_wc->slid << 16;
170225c7b1fSRoland Dreier 	}
171225c7b1fSRoland Dreier 
1720a9a0188SJack Morgenstein 	err = mlx4_cmd_box(dev->dev, inmailbox->dma, outmailbox->dma, in_modifier,
1730a9a0188SJack Morgenstein 			   mlx4_is_master(dev->dev) ? (op_modifier & ~0x8) : op_modifier,
174f9baff50SJack Morgenstein 			   MLX4_CMD_MAD_IFC, MLX4_CMD_TIME_CLASS_C,
1750a9a0188SJack Morgenstein 			   (op_modifier & 0x8) ? MLX4_CMD_NATIVE : MLX4_CMD_WRAPPED);
176225c7b1fSRoland Dreier 
177fe11cb6bSIlpo Järvinen 	if (!err)
178225c7b1fSRoland Dreier 		memcpy(response_mad, outmailbox->buf, 256);
179225c7b1fSRoland Dreier 
180225c7b1fSRoland Dreier 	mlx4_free_cmd_mailbox(dev->dev, inmailbox);
181225c7b1fSRoland Dreier 	mlx4_free_cmd_mailbox(dev->dev, outmailbox);
182225c7b1fSRoland Dreier 
183225c7b1fSRoland Dreier 	return err;
184225c7b1fSRoland Dreier }
185225c7b1fSRoland Dreier 
186225c7b1fSRoland Dreier static void update_sm_ah(struct mlx4_ib_dev *dev, u8 port_num, u16 lid, u8 sl)
187225c7b1fSRoland Dreier {
188225c7b1fSRoland Dreier 	struct ib_ah *new_ah;
189225c7b1fSRoland Dreier 	struct ib_ah_attr ah_attr;
190df7fba66SJack Morgenstein 	unsigned long flags;
191225c7b1fSRoland Dreier 
192225c7b1fSRoland Dreier 	if (!dev->send_agent[port_num - 1][0])
193225c7b1fSRoland Dreier 		return;
194225c7b1fSRoland Dreier 
195225c7b1fSRoland Dreier 	memset(&ah_attr, 0, sizeof ah_attr);
196225c7b1fSRoland Dreier 	ah_attr.dlid     = lid;
197225c7b1fSRoland Dreier 	ah_attr.sl       = sl;
198225c7b1fSRoland Dreier 	ah_attr.port_num = port_num;
199225c7b1fSRoland Dreier 
200225c7b1fSRoland Dreier 	new_ah = ib_create_ah(dev->send_agent[port_num - 1][0]->qp->pd,
201225c7b1fSRoland Dreier 			      &ah_attr);
202225c7b1fSRoland Dreier 	if (IS_ERR(new_ah))
203225c7b1fSRoland Dreier 		return;
204225c7b1fSRoland Dreier 
205df7fba66SJack Morgenstein 	spin_lock_irqsave(&dev->sm_lock, flags);
206225c7b1fSRoland Dreier 	if (dev->sm_ah[port_num - 1])
207225c7b1fSRoland Dreier 		ib_destroy_ah(dev->sm_ah[port_num - 1]);
208225c7b1fSRoland Dreier 	dev->sm_ah[port_num - 1] = new_ah;
209df7fba66SJack Morgenstein 	spin_unlock_irqrestore(&dev->sm_lock, flags);
210225c7b1fSRoland Dreier }
211225c7b1fSRoland Dreier 
212225c7b1fSRoland Dreier /*
21300f5ce99SJack Morgenstein  * Snoop SM MADs for port info, GUID info, and  P_Key table sets, so we can
21400f5ce99SJack Morgenstein  * synthesize LID change, Client-Rereg, GID change, and P_Key change events.
215225c7b1fSRoland Dreier  */
216a97e2d86SIra Weiny static void smp_snoop(struct ib_device *ibdev, u8 port_num, const struct ib_mad *mad,
217f0f6f346SMoni Shoua 		      u16 prev_lid)
218225c7b1fSRoland Dreier {
21900f5ce99SJack Morgenstein 	struct ib_port_info *pinfo;
22000f5ce99SJack Morgenstein 	u16 lid;
22154679e14SJack Morgenstein 	__be16 *base;
22254679e14SJack Morgenstein 	u32 bn, pkey_change_bitmap;
22354679e14SJack Morgenstein 	int i;
22454679e14SJack Morgenstein 
225225c7b1fSRoland Dreier 
22600f5ce99SJack Morgenstein 	struct mlx4_ib_dev *dev = to_mdev(ibdev);
227225c7b1fSRoland Dreier 	if ((mad->mad_hdr.mgmt_class == IB_MGMT_CLASS_SUBN_LID_ROUTED ||
228225c7b1fSRoland Dreier 	     mad->mad_hdr.mgmt_class == IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE) &&
22900f5ce99SJack Morgenstein 	    mad->mad_hdr.method == IB_MGMT_METHOD_SET)
23000f5ce99SJack Morgenstein 		switch (mad->mad_hdr.attr_id) {
23100f5ce99SJack Morgenstein 		case IB_SMP_ATTR_PORT_INFO:
23200f5ce99SJack Morgenstein 			pinfo = (struct ib_port_info *) ((struct ib_smp *) mad)->data;
23300f5ce99SJack Morgenstein 			lid = be16_to_cpu(pinfo->lid);
234225c7b1fSRoland Dreier 
23500f5ce99SJack Morgenstein 			update_sm_ah(dev, port_num,
236225c7b1fSRoland Dreier 				     be16_to_cpu(pinfo->sm_lid),
237225c7b1fSRoland Dreier 				     pinfo->neighbormtu_mastersmsl & 0xf);
238225c7b1fSRoland Dreier 
23900f5ce99SJack Morgenstein 			if (pinfo->clientrereg_resv_subnetto & 0x80)
240b9c5d6a6SOren Duer 				handle_client_rereg_event(dev, port_num);
241225c7b1fSRoland Dreier 
24200f5ce99SJack Morgenstein 			if (prev_lid != lid)
2432a4fae14SJack Morgenstein 				handle_lid_change_event(dev, port_num);
24400f5ce99SJack Morgenstein 			break;
245225c7b1fSRoland Dreier 
24600f5ce99SJack Morgenstein 		case IB_SMP_ATTR_PKEY_TABLE:
24754679e14SJack Morgenstein 			if (!mlx4_is_mfunc(dev->dev)) {
24800f5ce99SJack Morgenstein 				mlx4_ib_dispatch_event(dev, port_num,
24900f5ce99SJack Morgenstein 						       IB_EVENT_PKEY_CHANGE);
25000f5ce99SJack Morgenstein 				break;
25154679e14SJack Morgenstein 			}
25254679e14SJack Morgenstein 
2532a4fae14SJack Morgenstein 			/* at this point, we are running in the master.
2542a4fae14SJack Morgenstein 			 * Slaves do not receive SMPs.
2552a4fae14SJack Morgenstein 			 */
25654679e14SJack Morgenstein 			bn  = be32_to_cpu(((struct ib_smp *)mad)->attr_mod) & 0xFFFF;
25754679e14SJack Morgenstein 			base = (__be16 *) &(((struct ib_smp *)mad)->data[0]);
25854679e14SJack Morgenstein 			pkey_change_bitmap = 0;
25954679e14SJack Morgenstein 			for (i = 0; i < 32; i++) {
26054679e14SJack Morgenstein 				pr_debug("PKEY[%d] = x%x\n",
26154679e14SJack Morgenstein 					 i + bn*32, be16_to_cpu(base[i]));
26254679e14SJack Morgenstein 				if (be16_to_cpu(base[i]) !=
26354679e14SJack Morgenstein 				    dev->pkeys.phys_pkey_cache[port_num - 1][i + bn*32]) {
26454679e14SJack Morgenstein 					pkey_change_bitmap |= (1 << i);
26554679e14SJack Morgenstein 					dev->pkeys.phys_pkey_cache[port_num - 1][i + bn*32] =
26654679e14SJack Morgenstein 						be16_to_cpu(base[i]);
26754679e14SJack Morgenstein 				}
26854679e14SJack Morgenstein 			}
26954679e14SJack Morgenstein 			pr_debug("PKEY Change event: port=%d, "
27054679e14SJack Morgenstein 				 "block=0x%x, change_bitmap=0x%x\n",
27154679e14SJack Morgenstein 				 port_num, bn, pkey_change_bitmap);
27254679e14SJack Morgenstein 
2732a4fae14SJack Morgenstein 			if (pkey_change_bitmap) {
27454679e14SJack Morgenstein 				mlx4_ib_dispatch_event(dev, port_num,
27554679e14SJack Morgenstein 						       IB_EVENT_PKEY_CHANGE);
2762a4fae14SJack Morgenstein 				if (!dev->sriov.is_going_down)
2772a4fae14SJack Morgenstein 					__propagate_pkey_ev(dev, port_num, bn,
2782a4fae14SJack Morgenstein 							    pkey_change_bitmap);
2792a4fae14SJack Morgenstein 			}
28054679e14SJack Morgenstein 			break;
281f0f6f346SMoni Shoua 
28200f5ce99SJack Morgenstein 		case IB_SMP_ATTR_GUID_INFO:
2836634961cSJack Morgenstein 			/* paravirtualized master's guid is guid 0 -- does not change */
2846634961cSJack Morgenstein 			if (!mlx4_is_master(dev->dev))
28500f5ce99SJack Morgenstein 				mlx4_ib_dispatch_event(dev, port_num,
28600f5ce99SJack Morgenstein 						       IB_EVENT_GID_CHANGE);
2872a4fae14SJack Morgenstein 			/*if master, notify relevant slaves*/
2882a4fae14SJack Morgenstein 			if (mlx4_is_master(dev->dev) &&
2892a4fae14SJack Morgenstein 			    !dev->sriov.is_going_down) {
2902a4fae14SJack Morgenstein 				bn = be32_to_cpu(((struct ib_smp *)mad)->attr_mod);
2912a4fae14SJack Morgenstein 				mlx4_ib_update_cache_on_guid_change(dev, bn, port_num,
2922a4fae14SJack Morgenstein 								    (u8 *)(&((struct ib_smp *)mad)->data));
2932a4fae14SJack Morgenstein 				mlx4_ib_notify_slaves_on_guid_change(dev, bn, port_num,
2942a4fae14SJack Morgenstein 								     (u8 *)(&((struct ib_smp *)mad)->data));
2952a4fae14SJack Morgenstein 			}
29600f5ce99SJack Morgenstein 			break;
2972a4fae14SJack Morgenstein 
29800f5ce99SJack Morgenstein 		default:
29900f5ce99SJack Morgenstein 			break;
300225c7b1fSRoland Dreier 		}
301225c7b1fSRoland Dreier }
302225c7b1fSRoland Dreier 
3032a4fae14SJack Morgenstein static void __propagate_pkey_ev(struct mlx4_ib_dev *dev, int port_num,
3042a4fae14SJack Morgenstein 				int block, u32 change_bitmap)
3052a4fae14SJack Morgenstein {
3062a4fae14SJack Morgenstein 	int i, ix, slave, err;
3072a4fae14SJack Morgenstein 	int have_event = 0;
3082a4fae14SJack Morgenstein 
3092a4fae14SJack Morgenstein 	for (slave = 0; slave < dev->dev->caps.sqp_demux; slave++) {
3102a4fae14SJack Morgenstein 		if (slave == mlx4_master_func_num(dev->dev))
3112a4fae14SJack Morgenstein 			continue;
3122a4fae14SJack Morgenstein 		if (!mlx4_is_slave_active(dev->dev, slave))
3132a4fae14SJack Morgenstein 			continue;
3142a4fae14SJack Morgenstein 
3152a4fae14SJack Morgenstein 		have_event = 0;
3162a4fae14SJack Morgenstein 		for (i = 0; i < 32; i++) {
3172a4fae14SJack Morgenstein 			if (!(change_bitmap & (1 << i)))
3182a4fae14SJack Morgenstein 				continue;
3192a4fae14SJack Morgenstein 			for (ix = 0;
3202a4fae14SJack Morgenstein 			     ix < dev->dev->caps.pkey_table_len[port_num]; ix++) {
3212a4fae14SJack Morgenstein 				if (dev->pkeys.virt2phys_pkey[slave][port_num - 1]
3222a4fae14SJack Morgenstein 				    [ix] == i + 32 * block) {
3232a4fae14SJack Morgenstein 					err = mlx4_gen_pkey_eqe(dev->dev, slave, port_num);
3242a4fae14SJack Morgenstein 					pr_debug("propagate_pkey_ev: slave %d,"
3252a4fae14SJack Morgenstein 						 " port %d, ix %d (%d)\n",
3262a4fae14SJack Morgenstein 						 slave, port_num, ix, err);
3272a4fae14SJack Morgenstein 					have_event = 1;
3282a4fae14SJack Morgenstein 					break;
3292a4fae14SJack Morgenstein 				}
3302a4fae14SJack Morgenstein 			}
3312a4fae14SJack Morgenstein 			if (have_event)
3322a4fae14SJack Morgenstein 				break;
3332a4fae14SJack Morgenstein 		}
3342a4fae14SJack Morgenstein 	}
3352a4fae14SJack Morgenstein }
3362a4fae14SJack Morgenstein 
337225c7b1fSRoland Dreier static void node_desc_override(struct ib_device *dev,
338225c7b1fSRoland Dreier 			       struct ib_mad *mad)
339225c7b1fSRoland Dreier {
340df7fba66SJack Morgenstein 	unsigned long flags;
341df7fba66SJack Morgenstein 
342225c7b1fSRoland Dreier 	if ((mad->mad_hdr.mgmt_class == IB_MGMT_CLASS_SUBN_LID_ROUTED ||
343225c7b1fSRoland Dreier 	     mad->mad_hdr.mgmt_class == IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE) &&
344225c7b1fSRoland Dreier 	    mad->mad_hdr.method == IB_MGMT_METHOD_GET_RESP &&
345225c7b1fSRoland Dreier 	    mad->mad_hdr.attr_id == IB_SMP_ATTR_NODE_DESC) {
346df7fba66SJack Morgenstein 		spin_lock_irqsave(&to_mdev(dev)->sm_lock, flags);
347225c7b1fSRoland Dreier 		memcpy(((struct ib_smp *) mad)->data, dev->node_desc, 64);
348df7fba66SJack Morgenstein 		spin_unlock_irqrestore(&to_mdev(dev)->sm_lock, flags);
349225c7b1fSRoland Dreier 	}
350225c7b1fSRoland Dreier }
351225c7b1fSRoland Dreier 
352a97e2d86SIra Weiny static void forward_trap(struct mlx4_ib_dev *dev, u8 port_num, const struct ib_mad *mad)
353225c7b1fSRoland Dreier {
354225c7b1fSRoland Dreier 	int qpn = mad->mad_hdr.mgmt_class != IB_MGMT_CLASS_SUBN_LID_ROUTED;
355225c7b1fSRoland Dreier 	struct ib_mad_send_buf *send_buf;
356225c7b1fSRoland Dreier 	struct ib_mad_agent *agent = dev->send_agent[port_num - 1][qpn];
357225c7b1fSRoland Dreier 	int ret;
358df7fba66SJack Morgenstein 	unsigned long flags;
359225c7b1fSRoland Dreier 
360225c7b1fSRoland Dreier 	if (agent) {
361225c7b1fSRoland Dreier 		send_buf = ib_create_send_mad(agent, qpn, 0, 0, IB_MGMT_MAD_HDR,
362da2dfaa3SIra Weiny 					      IB_MGMT_MAD_DATA, GFP_ATOMIC,
363da2dfaa3SIra Weiny 					      IB_MGMT_BASE_VERSION);
36413974909SDan Carpenter 		if (IS_ERR(send_buf))
36513974909SDan Carpenter 			return;
366225c7b1fSRoland Dreier 		/*
367225c7b1fSRoland Dreier 		 * We rely here on the fact that MLX QPs don't use the
368225c7b1fSRoland Dreier 		 * address handle after the send is posted (this is
369225c7b1fSRoland Dreier 		 * wrong following the IB spec strictly, but we know
370225c7b1fSRoland Dreier 		 * it's OK for our devices).
371225c7b1fSRoland Dreier 		 */
372df7fba66SJack Morgenstein 		spin_lock_irqsave(&dev->sm_lock, flags);
373225c7b1fSRoland Dreier 		memcpy(send_buf->mad, mad, sizeof *mad);
374225c7b1fSRoland Dreier 		if ((send_buf->ah = dev->sm_ah[port_num - 1]))
375225c7b1fSRoland Dreier 			ret = ib_post_send_mad(send_buf, NULL);
376225c7b1fSRoland Dreier 		else
377225c7b1fSRoland Dreier 			ret = -EINVAL;
378df7fba66SJack Morgenstein 		spin_unlock_irqrestore(&dev->sm_lock, flags);
379225c7b1fSRoland Dreier 
380225c7b1fSRoland Dreier 		if (ret)
381225c7b1fSRoland Dreier 			ib_free_send_mad(send_buf);
382225c7b1fSRoland Dreier 	}
383225c7b1fSRoland Dreier }
384225c7b1fSRoland Dreier 
38537bfc7c1SJack Morgenstein static int mlx4_ib_demux_sa_handler(struct ib_device *ibdev, int port, int slave,
38637bfc7c1SJack Morgenstein 							     struct ib_sa_mad *sa_mad)
38737bfc7c1SJack Morgenstein {
388b9c5d6a6SOren Duer 	int ret = 0;
389b9c5d6a6SOren Duer 
390b9c5d6a6SOren Duer 	/* dispatch to different sa handlers */
391b9c5d6a6SOren Duer 	switch (be16_to_cpu(sa_mad->mad_hdr.attr_id)) {
392b9c5d6a6SOren Duer 	case IB_SA_ATTR_MC_MEMBER_REC:
393b9c5d6a6SOren Duer 		ret = mlx4_ib_mcg_demux_handler(ibdev, port, slave, sa_mad);
394b9c5d6a6SOren Duer 		break;
395b9c5d6a6SOren Duer 	default:
396b9c5d6a6SOren Duer 		break;
397b9c5d6a6SOren Duer 	}
398b9c5d6a6SOren Duer 	return ret;
39937bfc7c1SJack Morgenstein }
40037bfc7c1SJack Morgenstein 
40137bfc7c1SJack Morgenstein int mlx4_ib_find_real_gid(struct ib_device *ibdev, u8 port, __be64 guid)
40237bfc7c1SJack Morgenstein {
40337bfc7c1SJack Morgenstein 	struct mlx4_ib_dev *dev = to_mdev(ibdev);
40437bfc7c1SJack Morgenstein 	int i;
40537bfc7c1SJack Morgenstein 
40637bfc7c1SJack Morgenstein 	for (i = 0; i < dev->dev->caps.sqp_demux; i++) {
40737bfc7c1SJack Morgenstein 		if (dev->sriov.demux[port - 1].guid_cache[i] == guid)
40837bfc7c1SJack Morgenstein 			return i;
40937bfc7c1SJack Morgenstein 	}
41037bfc7c1SJack Morgenstein 	return -1;
41137bfc7c1SJack Morgenstein }
41237bfc7c1SJack Morgenstein 
41337bfc7c1SJack Morgenstein 
4142c75d2ccSJack Morgenstein static int find_slave_port_pkey_ix(struct mlx4_ib_dev *dev, int slave,
4152c75d2ccSJack Morgenstein 				   u8 port, u16 pkey, u16 *ix)
41637bfc7c1SJack Morgenstein {
4172c75d2ccSJack Morgenstein 	int i, ret;
4182c75d2ccSJack Morgenstein 	u8 unassigned_pkey_ix, pkey_ix, partial_ix = 0xFF;
4192c75d2ccSJack Morgenstein 	u16 slot_pkey;
42037bfc7c1SJack Morgenstein 
4212c75d2ccSJack Morgenstein 	if (slave == mlx4_master_func_num(dev->dev))
4222c75d2ccSJack Morgenstein 		return ib_find_cached_pkey(&dev->ib_dev, port, pkey, ix);
42337bfc7c1SJack Morgenstein 
4242c75d2ccSJack Morgenstein 	unassigned_pkey_ix = dev->dev->phys_caps.pkey_phys_table_len[port] - 1;
4252c75d2ccSJack Morgenstein 
4262c75d2ccSJack Morgenstein 	for (i = 0; i < dev->dev->caps.pkey_table_len[port]; i++) {
4272c75d2ccSJack Morgenstein 		if (dev->pkeys.virt2phys_pkey[slave][port - 1][i] == unassigned_pkey_ix)
4282c75d2ccSJack Morgenstein 			continue;
4292c75d2ccSJack Morgenstein 
4302c75d2ccSJack Morgenstein 		pkey_ix = dev->pkeys.virt2phys_pkey[slave][port - 1][i];
4312c75d2ccSJack Morgenstein 
4322c75d2ccSJack Morgenstein 		ret = ib_get_cached_pkey(&dev->ib_dev, port, pkey_ix, &slot_pkey);
4332c75d2ccSJack Morgenstein 		if (ret)
4342c75d2ccSJack Morgenstein 			continue;
4352c75d2ccSJack Morgenstein 		if ((slot_pkey & 0x7FFF) == (pkey & 0x7FFF)) {
4362c75d2ccSJack Morgenstein 			if (slot_pkey & 0x8000) {
4372c75d2ccSJack Morgenstein 				*ix = (u16) pkey_ix;
4382c75d2ccSJack Morgenstein 				return 0;
43937bfc7c1SJack Morgenstein 			} else {
4402c75d2ccSJack Morgenstein 				/* take first partial pkey index found */
4412c75d2ccSJack Morgenstein 				if (partial_ix == 0xFF)
4422c75d2ccSJack Morgenstein 					partial_ix = pkey_ix;
4432c75d2ccSJack Morgenstein 			}
4442c75d2ccSJack Morgenstein 		}
44537bfc7c1SJack Morgenstein 	}
44637bfc7c1SJack Morgenstein 
4472c75d2ccSJack Morgenstein 	if (partial_ix < 0xFF) {
4482c75d2ccSJack Morgenstein 		*ix = (u16) partial_ix;
4492c75d2ccSJack Morgenstein 		return 0;
4502c75d2ccSJack Morgenstein 	}
45137bfc7c1SJack Morgenstein 
4522c75d2ccSJack Morgenstein 	return -EINVAL;
45337bfc7c1SJack Morgenstein }
45437bfc7c1SJack Morgenstein 
45537bfc7c1SJack Morgenstein int mlx4_ib_send_to_slave(struct mlx4_ib_dev *dev, int slave, u8 port,
45637bfc7c1SJack Morgenstein 			  enum ib_qp_type dest_qpt, struct ib_wc *wc,
45737bfc7c1SJack Morgenstein 			  struct ib_grh *grh, struct ib_mad *mad)
45837bfc7c1SJack Morgenstein {
45937bfc7c1SJack Morgenstein 	struct ib_sge list;
46037bfc7c1SJack Morgenstein 	struct ib_send_wr wr, *bad_wr;
46137bfc7c1SJack Morgenstein 	struct mlx4_ib_demux_pv_ctx *tun_ctx;
46237bfc7c1SJack Morgenstein 	struct mlx4_ib_demux_pv_qp *tun_qp;
46337bfc7c1SJack Morgenstein 	struct mlx4_rcv_tunnel_mad *tun_mad;
46437bfc7c1SJack Morgenstein 	struct ib_ah_attr attr;
46537bfc7c1SJack Morgenstein 	struct ib_ah *ah;
46637bfc7c1SJack Morgenstein 	struct ib_qp *src_qp = NULL;
46737bfc7c1SJack Morgenstein 	unsigned tun_tx_ix = 0;
46837bfc7c1SJack Morgenstein 	int dqpn;
46937bfc7c1SJack Morgenstein 	int ret = 0;
47037bfc7c1SJack Morgenstein 	u16 tun_pkey_ix;
4712c75d2ccSJack Morgenstein 	u16 cached_pkey;
4726ee51a4eSJack Morgenstein 	u8 is_eth = dev->dev->caps.port_type[port] == MLX4_PORT_TYPE_ETH;
47337bfc7c1SJack Morgenstein 
47437bfc7c1SJack Morgenstein 	if (dest_qpt > IB_QPT_GSI)
47537bfc7c1SJack Morgenstein 		return -EINVAL;
47637bfc7c1SJack Morgenstein 
47737bfc7c1SJack Morgenstein 	tun_ctx = dev->sriov.demux[port-1].tun[slave];
47837bfc7c1SJack Morgenstein 
47937bfc7c1SJack Morgenstein 	/* check if proxy qp created */
48037bfc7c1SJack Morgenstein 	if (!tun_ctx || tun_ctx->state != DEMUX_PV_STATE_ACTIVE)
48137bfc7c1SJack Morgenstein 		return -EAGAIN;
48237bfc7c1SJack Morgenstein 
48337bfc7c1SJack Morgenstein 	if (!dest_qpt)
48437bfc7c1SJack Morgenstein 		tun_qp = &tun_ctx->qp[0];
48537bfc7c1SJack Morgenstein 	else
48637bfc7c1SJack Morgenstein 		tun_qp = &tun_ctx->qp[1];
48737bfc7c1SJack Morgenstein 
4882c75d2ccSJack Morgenstein 	/* compute P_Key index to put in tunnel header for slave */
48937bfc7c1SJack Morgenstein 	if (dest_qpt) {
4902c75d2ccSJack Morgenstein 		u16 pkey_ix;
4912c75d2ccSJack Morgenstein 		ret = ib_get_cached_pkey(&dev->ib_dev, port, wc->pkey_index, &cached_pkey);
49237bfc7c1SJack Morgenstein 		if (ret)
49337bfc7c1SJack Morgenstein 			return -EINVAL;
49437bfc7c1SJack Morgenstein 
4952c75d2ccSJack Morgenstein 		ret = find_slave_port_pkey_ix(dev, slave, port, cached_pkey, &pkey_ix);
4962c75d2ccSJack Morgenstein 		if (ret)
49737bfc7c1SJack Morgenstein 			return -EINVAL;
4982c75d2ccSJack Morgenstein 		tun_pkey_ix = pkey_ix;
49937bfc7c1SJack Morgenstein 	} else
50037bfc7c1SJack Morgenstein 		tun_pkey_ix = dev->pkeys.virt2phys_pkey[slave][port - 1][0];
50137bfc7c1SJack Morgenstein 
50247605df9SJack Morgenstein 	dqpn = dev->dev->phys_caps.base_proxy_sqpn + 8 * slave + port + (dest_qpt * 2) - 1;
50337bfc7c1SJack Morgenstein 
50437bfc7c1SJack Morgenstein 	/* get tunnel tx data buf for slave */
50537bfc7c1SJack Morgenstein 	src_qp = tun_qp->qp;
50637bfc7c1SJack Morgenstein 
50737bfc7c1SJack Morgenstein 	/* create ah. Just need an empty one with the port num for the post send.
50837bfc7c1SJack Morgenstein 	 * The driver will set the force loopback bit in post_send */
50937bfc7c1SJack Morgenstein 	memset(&attr, 0, sizeof attr);
51037bfc7c1SJack Morgenstein 	attr.port_num = port;
5116ee51a4eSJack Morgenstein 	if (is_eth) {
512b6ffaeffSJack Morgenstein 		memcpy(&attr.grh.dgid.raw[0], &grh->dgid.raw[0], 16);
5136ee51a4eSJack Morgenstein 		attr.ah_flags = IB_AH_GRH;
5146ee51a4eSJack Morgenstein 	}
51537bfc7c1SJack Morgenstein 	ah = ib_create_ah(tun_ctx->pd, &attr);
51637bfc7c1SJack Morgenstein 	if (IS_ERR(ah))
51737bfc7c1SJack Morgenstein 		return -ENOMEM;
51837bfc7c1SJack Morgenstein 
51937bfc7c1SJack Morgenstein 	/* allocate tunnel tx buf after pass failure returns */
52037bfc7c1SJack Morgenstein 	spin_lock(&tun_qp->tx_lock);
52137bfc7c1SJack Morgenstein 	if (tun_qp->tx_ix_head - tun_qp->tx_ix_tail >=
52237bfc7c1SJack Morgenstein 	    (MLX4_NUM_TUNNEL_BUFS - 1))
52337bfc7c1SJack Morgenstein 		ret = -EAGAIN;
52437bfc7c1SJack Morgenstein 	else
52537bfc7c1SJack Morgenstein 		tun_tx_ix = (++tun_qp->tx_ix_head) & (MLX4_NUM_TUNNEL_BUFS - 1);
52637bfc7c1SJack Morgenstein 	spin_unlock(&tun_qp->tx_lock);
52737bfc7c1SJack Morgenstein 	if (ret)
52837bfc7c1SJack Morgenstein 		goto out;
52937bfc7c1SJack Morgenstein 
53037bfc7c1SJack Morgenstein 	tun_mad = (struct mlx4_rcv_tunnel_mad *) (tun_qp->tx_ring[tun_tx_ix].buf.addr);
53137bfc7c1SJack Morgenstein 	if (tun_qp->tx_ring[tun_tx_ix].ah)
53237bfc7c1SJack Morgenstein 		ib_destroy_ah(tun_qp->tx_ring[tun_tx_ix].ah);
53337bfc7c1SJack Morgenstein 	tun_qp->tx_ring[tun_tx_ix].ah = ah;
53437bfc7c1SJack Morgenstein 	ib_dma_sync_single_for_cpu(&dev->ib_dev,
53537bfc7c1SJack Morgenstein 				   tun_qp->tx_ring[tun_tx_ix].buf.map,
53637bfc7c1SJack Morgenstein 				   sizeof (struct mlx4_rcv_tunnel_mad),
53737bfc7c1SJack Morgenstein 				   DMA_TO_DEVICE);
53837bfc7c1SJack Morgenstein 
53937bfc7c1SJack Morgenstein 	/* copy over to tunnel buffer */
54037bfc7c1SJack Morgenstein 	if (grh)
54137bfc7c1SJack Morgenstein 		memcpy(&tun_mad->grh, grh, sizeof *grh);
54237bfc7c1SJack Morgenstein 	memcpy(&tun_mad->mad, mad, sizeof *mad);
54337bfc7c1SJack Morgenstein 
54437bfc7c1SJack Morgenstein 	/* adjust tunnel data */
54537bfc7c1SJack Morgenstein 	tun_mad->hdr.pkey_index = cpu_to_be16(tun_pkey_ix);
54637bfc7c1SJack Morgenstein 	tun_mad->hdr.flags_src_qp = cpu_to_be32(wc->src_qp & 0xFFFFFF);
54737bfc7c1SJack Morgenstein 	tun_mad->hdr.g_ml_path = (grh && (wc->wc_flags & IB_WC_GRH)) ? 0x80 : 0;
54837bfc7c1SJack Morgenstein 
5495ea8bbfcSJack Morgenstein 	if (is_eth) {
5505ea8bbfcSJack Morgenstein 		u16 vlan = 0;
5515ea8bbfcSJack Morgenstein 		if (mlx4_get_slave_default_vlan(dev->dev, port, slave, &vlan,
5525ea8bbfcSJack Morgenstein 						NULL)) {
5535ea8bbfcSJack Morgenstein 			/* VST mode */
5545ea8bbfcSJack Morgenstein 			if (vlan != wc->vlan_id)
5555ea8bbfcSJack Morgenstein 				/* Packet vlan is not the VST-assigned vlan.
5565ea8bbfcSJack Morgenstein 				 * Drop the packet.
5575ea8bbfcSJack Morgenstein 				 */
5585ea8bbfcSJack Morgenstein 				goto out;
5595ea8bbfcSJack Morgenstein 			 else
5605ea8bbfcSJack Morgenstein 				/* Remove the vlan tag before forwarding
5615ea8bbfcSJack Morgenstein 				 * the packet to the VF.
5625ea8bbfcSJack Morgenstein 				 */
5635ea8bbfcSJack Morgenstein 				vlan = 0xffff;
5645ea8bbfcSJack Morgenstein 		} else {
5655ea8bbfcSJack Morgenstein 			vlan = wc->vlan_id;
5665ea8bbfcSJack Morgenstein 		}
5675ea8bbfcSJack Morgenstein 
5685ea8bbfcSJack Morgenstein 		tun_mad->hdr.sl_vid = cpu_to_be16(vlan);
5695ea8bbfcSJack Morgenstein 		memcpy((char *)&tun_mad->hdr.mac_31_0, &(wc->smac[0]), 4);
5705ea8bbfcSJack Morgenstein 		memcpy((char *)&tun_mad->hdr.slid_mac_47_32, &(wc->smac[4]), 2);
5715ea8bbfcSJack Morgenstein 	} else {
5725ea8bbfcSJack Morgenstein 		tun_mad->hdr.sl_vid = cpu_to_be16(((u16)(wc->sl)) << 12);
5735ea8bbfcSJack Morgenstein 		tun_mad->hdr.slid_mac_47_32 = cpu_to_be16(wc->slid);
5745ea8bbfcSJack Morgenstein 	}
5755ea8bbfcSJack Morgenstein 
57637bfc7c1SJack Morgenstein 	ib_dma_sync_single_for_device(&dev->ib_dev,
57737bfc7c1SJack Morgenstein 				      tun_qp->tx_ring[tun_tx_ix].buf.map,
57837bfc7c1SJack Morgenstein 				      sizeof (struct mlx4_rcv_tunnel_mad),
57937bfc7c1SJack Morgenstein 				      DMA_TO_DEVICE);
58037bfc7c1SJack Morgenstein 
58137bfc7c1SJack Morgenstein 	list.addr = tun_qp->tx_ring[tun_tx_ix].buf.map;
58237bfc7c1SJack Morgenstein 	list.length = sizeof (struct mlx4_rcv_tunnel_mad);
5837dd97576SJason Gunthorpe 	list.lkey = tun_ctx->pd->local_dma_lkey;
58437bfc7c1SJack Morgenstein 
58537bfc7c1SJack Morgenstein 	wr.wr.ud.ah = ah;
58637bfc7c1SJack Morgenstein 	wr.wr.ud.port_num = port;
58737bfc7c1SJack Morgenstein 	wr.wr.ud.remote_qkey = IB_QP_SET_QKEY;
58837bfc7c1SJack Morgenstein 	wr.wr.ud.remote_qpn = dqpn;
58937bfc7c1SJack Morgenstein 	wr.next = NULL;
59037bfc7c1SJack Morgenstein 	wr.wr_id = ((u64) tun_tx_ix) | MLX4_TUN_SET_WRID_QPN(dest_qpt);
59137bfc7c1SJack Morgenstein 	wr.sg_list = &list;
59237bfc7c1SJack Morgenstein 	wr.num_sge = 1;
59337bfc7c1SJack Morgenstein 	wr.opcode = IB_WR_SEND;
59437bfc7c1SJack Morgenstein 	wr.send_flags = IB_SEND_SIGNALED;
59537bfc7c1SJack Morgenstein 
59637bfc7c1SJack Morgenstein 	ret = ib_post_send(src_qp, &wr, &bad_wr);
59737bfc7c1SJack Morgenstein out:
59837bfc7c1SJack Morgenstein 	if (ret)
59937bfc7c1SJack Morgenstein 		ib_destroy_ah(ah);
60037bfc7c1SJack Morgenstein 	return ret;
60137bfc7c1SJack Morgenstein }
60237bfc7c1SJack Morgenstein 
60337bfc7c1SJack Morgenstein static int mlx4_ib_demux_mad(struct ib_device *ibdev, u8 port,
60437bfc7c1SJack Morgenstein 			struct ib_wc *wc, struct ib_grh *grh,
60537bfc7c1SJack Morgenstein 			struct ib_mad *mad)
60637bfc7c1SJack Morgenstein {
60737bfc7c1SJack Morgenstein 	struct mlx4_ib_dev *dev = to_mdev(ibdev);
60837bfc7c1SJack Morgenstein 	int err;
60937bfc7c1SJack Morgenstein 	int slave;
61037bfc7c1SJack Morgenstein 	u8 *slave_id;
6116ee51a4eSJack Morgenstein 	int is_eth = 0;
6126ee51a4eSJack Morgenstein 
6136ee51a4eSJack Morgenstein 	if (rdma_port_get_link_layer(ibdev, port) == IB_LINK_LAYER_INFINIBAND)
6146ee51a4eSJack Morgenstein 		is_eth = 0;
6156ee51a4eSJack Morgenstein 	else
6166ee51a4eSJack Morgenstein 		is_eth = 1;
6176ee51a4eSJack Morgenstein 
6186ee51a4eSJack Morgenstein 	if (is_eth) {
6196ee51a4eSJack Morgenstein 		if (!(wc->wc_flags & IB_WC_GRH)) {
6206ee51a4eSJack Morgenstein 			mlx4_ib_warn(ibdev, "RoCE grh not present.\n");
6216ee51a4eSJack Morgenstein 			return -EINVAL;
6226ee51a4eSJack Morgenstein 		}
6236ee51a4eSJack Morgenstein 		if (mad->mad_hdr.mgmt_class != IB_MGMT_CLASS_CM) {
6246ee51a4eSJack Morgenstein 			mlx4_ib_warn(ibdev, "RoCE mgmt class is not CM\n");
6256ee51a4eSJack Morgenstein 			return -EINVAL;
6266ee51a4eSJack Morgenstein 		}
6276ee51a4eSJack Morgenstein 		if (mlx4_get_slave_from_roce_gid(dev->dev, port, grh->dgid.raw, &slave)) {
6286ee51a4eSJack Morgenstein 			mlx4_ib_warn(ibdev, "failed matching grh\n");
6296ee51a4eSJack Morgenstein 			return -ENOENT;
6306ee51a4eSJack Morgenstein 		}
6316ee51a4eSJack Morgenstein 		if (slave >= dev->dev->caps.sqp_demux) {
6326ee51a4eSJack Morgenstein 			mlx4_ib_warn(ibdev, "slave id: %d is bigger than allowed:%d\n",
6336ee51a4eSJack Morgenstein 				     slave, dev->dev->caps.sqp_demux);
6346ee51a4eSJack Morgenstein 			return -ENOENT;
6356ee51a4eSJack Morgenstein 		}
6366ee51a4eSJack Morgenstein 
6376ee51a4eSJack Morgenstein 		if (mlx4_ib_demux_cm_handler(ibdev, port, NULL, mad))
6386ee51a4eSJack Morgenstein 			return 0;
6396ee51a4eSJack Morgenstein 
6406ee51a4eSJack Morgenstein 		err = mlx4_ib_send_to_slave(dev, slave, port, wc->qp->qp_type, wc, grh, mad);
6416ee51a4eSJack Morgenstein 		if (err)
6426ee51a4eSJack Morgenstein 			pr_debug("failed sending to slave %d via tunnel qp (%d)\n",
6436ee51a4eSJack Morgenstein 				 slave, err);
6446ee51a4eSJack Morgenstein 		return 0;
6456ee51a4eSJack Morgenstein 	}
64637bfc7c1SJack Morgenstein 
64737bfc7c1SJack Morgenstein 	/* Initially assume that this mad is for us */
64837bfc7c1SJack Morgenstein 	slave = mlx4_master_func_num(dev->dev);
64937bfc7c1SJack Morgenstein 
65037bfc7c1SJack Morgenstein 	/* See if the slave id is encoded in a response mad */
65137bfc7c1SJack Morgenstein 	if (mad->mad_hdr.method & 0x80) {
65237bfc7c1SJack Morgenstein 		slave_id = (u8 *) &mad->mad_hdr.tid;
65337bfc7c1SJack Morgenstein 		slave = *slave_id;
65437bfc7c1SJack Morgenstein 		if (slave != 255) /*255 indicates the dom0*/
65537bfc7c1SJack Morgenstein 			*slave_id = 0; /* remap tid */
65637bfc7c1SJack Morgenstein 	}
65737bfc7c1SJack Morgenstein 
65837bfc7c1SJack Morgenstein 	/* If a grh is present, we demux according to it */
65937bfc7c1SJack Morgenstein 	if (wc->wc_flags & IB_WC_GRH) {
66037bfc7c1SJack Morgenstein 		slave = mlx4_ib_find_real_gid(ibdev, port, grh->dgid.global.interface_id);
66137bfc7c1SJack Morgenstein 		if (slave < 0) {
66237bfc7c1SJack Morgenstein 			mlx4_ib_warn(ibdev, "failed matching grh\n");
66337bfc7c1SJack Morgenstein 			return -ENOENT;
66437bfc7c1SJack Morgenstein 		}
66537bfc7c1SJack Morgenstein 	}
66637bfc7c1SJack Morgenstein 	/* Class-specific handling */
66737bfc7c1SJack Morgenstein 	switch (mad->mad_hdr.mgmt_class) {
66897982f5aSJack Morgenstein 	case IB_MGMT_CLASS_SUBN_LID_ROUTED:
66997982f5aSJack Morgenstein 	case IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE:
67097982f5aSJack Morgenstein 		/* 255 indicates the dom0 */
67197982f5aSJack Morgenstein 		if (slave != 255 && slave != mlx4_master_func_num(dev->dev)) {
67297982f5aSJack Morgenstein 			if (!mlx4_vf_smi_enabled(dev->dev, slave, port))
67397982f5aSJack Morgenstein 				return -EPERM;
67497982f5aSJack Morgenstein 			/* for a VF. drop unsolicited MADs */
67597982f5aSJack Morgenstein 			if (!(mad->mad_hdr.method & IB_MGMT_METHOD_RESP)) {
67697982f5aSJack Morgenstein 				mlx4_ib_warn(ibdev, "demux QP0. rejecting unsolicited mad for slave %d class 0x%x, method 0x%x\n",
67797982f5aSJack Morgenstein 					     slave, mad->mad_hdr.mgmt_class,
67897982f5aSJack Morgenstein 					     mad->mad_hdr.method);
67997982f5aSJack Morgenstein 				return -EINVAL;
68097982f5aSJack Morgenstein 			}
68197982f5aSJack Morgenstein 		}
68297982f5aSJack Morgenstein 		break;
68337bfc7c1SJack Morgenstein 	case IB_MGMT_CLASS_SUBN_ADM:
68437bfc7c1SJack Morgenstein 		if (mlx4_ib_demux_sa_handler(ibdev, port, slave,
68537bfc7c1SJack Morgenstein 					     (struct ib_sa_mad *) mad))
68637bfc7c1SJack Morgenstein 			return 0;
68737bfc7c1SJack Morgenstein 		break;
6883cf69cc8SAmir Vadai 	case IB_MGMT_CLASS_CM:
6893cf69cc8SAmir Vadai 		if (mlx4_ib_demux_cm_handler(ibdev, port, &slave, mad))
6903cf69cc8SAmir Vadai 			return 0;
6913cf69cc8SAmir Vadai 		break;
69237bfc7c1SJack Morgenstein 	case IB_MGMT_CLASS_DEVICE_MGMT:
69337bfc7c1SJack Morgenstein 		if (mad->mad_hdr.method != IB_MGMT_METHOD_GET_RESP)
69437bfc7c1SJack Morgenstein 			return 0;
69537bfc7c1SJack Morgenstein 		break;
69637bfc7c1SJack Morgenstein 	default:
69737bfc7c1SJack Morgenstein 		/* Drop unsupported classes for slaves in tunnel mode */
69837bfc7c1SJack Morgenstein 		if (slave != mlx4_master_func_num(dev->dev)) {
69937bfc7c1SJack Morgenstein 			pr_debug("dropping unsupported ingress mad from class:%d "
70037bfc7c1SJack Morgenstein 				 "for slave:%d\n", mad->mad_hdr.mgmt_class, slave);
70137bfc7c1SJack Morgenstein 			return 0;
70237bfc7c1SJack Morgenstein 		}
70337bfc7c1SJack Morgenstein 	}
70437bfc7c1SJack Morgenstein 	/*make sure that no slave==255 was not handled yet.*/
70537bfc7c1SJack Morgenstein 	if (slave >= dev->dev->caps.sqp_demux) {
70637bfc7c1SJack Morgenstein 		mlx4_ib_warn(ibdev, "slave id: %d is bigger than allowed:%d\n",
70737bfc7c1SJack Morgenstein 			     slave, dev->dev->caps.sqp_demux);
70837bfc7c1SJack Morgenstein 		return -ENOENT;
70937bfc7c1SJack Morgenstein 	}
71037bfc7c1SJack Morgenstein 
71137bfc7c1SJack Morgenstein 	err = mlx4_ib_send_to_slave(dev, slave, port, wc->qp->qp_type, wc, grh, mad);
71237bfc7c1SJack Morgenstein 	if (err)
71337bfc7c1SJack Morgenstein 		pr_debug("failed sending to slave %d via tunnel qp (%d)\n",
71437bfc7c1SJack Morgenstein 			 slave, err);
71537bfc7c1SJack Morgenstein 	return 0;
71637bfc7c1SJack Morgenstein }
71737bfc7c1SJack Morgenstein 
718c3779134SOr Gerlitz static int ib_process_mad(struct ib_device *ibdev, int mad_flags, u8 port_num,
719a97e2d86SIra Weiny 			const struct ib_wc *in_wc, const struct ib_grh *in_grh,
720a97e2d86SIra Weiny 			const struct ib_mad *in_mad, struct ib_mad *out_mad)
721225c7b1fSRoland Dreier {
722f0f6f346SMoni Shoua 	u16 slid, prev_lid = 0;
723225c7b1fSRoland Dreier 	int err;
724f0f6f346SMoni Shoua 	struct ib_port_attr pattr;
725225c7b1fSRoland Dreier 
726b1d8eb5aSJack Morgenstein 	if (in_wc && in_wc->qp->qp_num) {
727b1d8eb5aSJack Morgenstein 		pr_debug("received MAD: slid:%d sqpn:%d "
728b1d8eb5aSJack Morgenstein 			"dlid_bits:%d dqpn:%d wc_flags:0x%x, cls %x, mtd %x, atr %x\n",
729b1d8eb5aSJack Morgenstein 			in_wc->slid, in_wc->src_qp,
730b1d8eb5aSJack Morgenstein 			in_wc->dlid_path_bits,
731b1d8eb5aSJack Morgenstein 			in_wc->qp->qp_num,
732b1d8eb5aSJack Morgenstein 			in_wc->wc_flags,
733b1d8eb5aSJack Morgenstein 			in_mad->mad_hdr.mgmt_class, in_mad->mad_hdr.method,
734b1d8eb5aSJack Morgenstein 			be16_to_cpu(in_mad->mad_hdr.attr_id));
735b1d8eb5aSJack Morgenstein 		if (in_wc->wc_flags & IB_WC_GRH) {
736b1d8eb5aSJack Morgenstein 			pr_debug("sgid_hi:0x%016llx sgid_lo:0x%016llx\n",
737b1d8eb5aSJack Morgenstein 				 be64_to_cpu(in_grh->sgid.global.subnet_prefix),
738b1d8eb5aSJack Morgenstein 				 be64_to_cpu(in_grh->sgid.global.interface_id));
739b1d8eb5aSJack Morgenstein 			pr_debug("dgid_hi:0x%016llx dgid_lo:0x%016llx\n",
740b1d8eb5aSJack Morgenstein 				 be64_to_cpu(in_grh->dgid.global.subnet_prefix),
741b1d8eb5aSJack Morgenstein 				 be64_to_cpu(in_grh->dgid.global.interface_id));
742b1d8eb5aSJack Morgenstein 		}
743b1d8eb5aSJack Morgenstein 	}
744b1d8eb5aSJack Morgenstein 
745225c7b1fSRoland Dreier 	slid = in_wc ? in_wc->slid : be16_to_cpu(IB_LID_PERMISSIVE);
746225c7b1fSRoland Dreier 
747225c7b1fSRoland Dreier 	if (in_mad->mad_hdr.method == IB_MGMT_METHOD_TRAP && slid == 0) {
748225c7b1fSRoland Dreier 		forward_trap(to_mdev(ibdev), port_num, in_mad);
749225c7b1fSRoland Dreier 		return IB_MAD_RESULT_SUCCESS | IB_MAD_RESULT_CONSUMED;
750225c7b1fSRoland Dreier 	}
751225c7b1fSRoland Dreier 
752225c7b1fSRoland Dreier 	if (in_mad->mad_hdr.mgmt_class == IB_MGMT_CLASS_SUBN_LID_ROUTED ||
753225c7b1fSRoland Dreier 	    in_mad->mad_hdr.mgmt_class == IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE) {
754225c7b1fSRoland Dreier 		if (in_mad->mad_hdr.method   != IB_MGMT_METHOD_GET &&
755225c7b1fSRoland Dreier 		    in_mad->mad_hdr.method   != IB_MGMT_METHOD_SET &&
756225c7b1fSRoland Dreier 		    in_mad->mad_hdr.method   != IB_MGMT_METHOD_TRAP_REPRESS)
757225c7b1fSRoland Dreier 			return IB_MAD_RESULT_SUCCESS;
758225c7b1fSRoland Dreier 
759225c7b1fSRoland Dreier 		/*
760a6f7feaeSJack Morgenstein 		 * Don't process SMInfo queries -- the SMA can't handle them.
761225c7b1fSRoland Dreier 		 */
762a6f7feaeSJack Morgenstein 		if (in_mad->mad_hdr.attr_id == IB_SMP_ATTR_SM_INFO)
763225c7b1fSRoland Dreier 			return IB_MAD_RESULT_SUCCESS;
764225c7b1fSRoland Dreier 	} else if (in_mad->mad_hdr.mgmt_class == IB_MGMT_CLASS_PERF_MGMT ||
765225c7b1fSRoland Dreier 		   in_mad->mad_hdr.mgmt_class == MLX4_IB_VENDOR_CLASS1   ||
7666578cf33SEli Cohen 		   in_mad->mad_hdr.mgmt_class == MLX4_IB_VENDOR_CLASS2   ||
7676578cf33SEli Cohen 		   in_mad->mad_hdr.mgmt_class == IB_MGMT_CLASS_CONG_MGMT) {
768225c7b1fSRoland Dreier 		if (in_mad->mad_hdr.method  != IB_MGMT_METHOD_GET &&
769225c7b1fSRoland Dreier 		    in_mad->mad_hdr.method  != IB_MGMT_METHOD_SET)
770225c7b1fSRoland Dreier 			return IB_MAD_RESULT_SUCCESS;
771225c7b1fSRoland Dreier 	} else
772225c7b1fSRoland Dreier 		return IB_MAD_RESULT_SUCCESS;
773225c7b1fSRoland Dreier 
774f0f6f346SMoni Shoua 	if ((in_mad->mad_hdr.mgmt_class == IB_MGMT_CLASS_SUBN_LID_ROUTED ||
775f0f6f346SMoni Shoua 	     in_mad->mad_hdr.mgmt_class == IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE) &&
776f0f6f346SMoni Shoua 	    in_mad->mad_hdr.method == IB_MGMT_METHOD_SET &&
777f0f6f346SMoni Shoua 	    in_mad->mad_hdr.attr_id == IB_SMP_ATTR_PORT_INFO &&
778f0f6f346SMoni Shoua 	    !ib_query_port(ibdev, port_num, &pattr))
779f0f6f346SMoni Shoua 		prev_lid = pattr.lid;
780f0f6f346SMoni Shoua 
781225c7b1fSRoland Dreier 	err = mlx4_MAD_IFC(to_mdev(ibdev),
7820a9a0188SJack Morgenstein 			   (mad_flags & IB_MAD_IGNORE_MKEY ? MLX4_MAD_IFC_IGNORE_MKEY : 0) |
7830a9a0188SJack Morgenstein 			   (mad_flags & IB_MAD_IGNORE_BKEY ? MLX4_MAD_IFC_IGNORE_BKEY : 0) |
7840a9a0188SJack Morgenstein 			   MLX4_MAD_IFC_NET_VIEW,
785225c7b1fSRoland Dreier 			   port_num, in_wc, in_grh, in_mad, out_mad);
786225c7b1fSRoland Dreier 	if (err)
787225c7b1fSRoland Dreier 		return IB_MAD_RESULT_FAILURE;
788225c7b1fSRoland Dreier 
789225c7b1fSRoland Dreier 	if (!out_mad->mad_hdr.status) {
79000f5ce99SJack Morgenstein 		if (!(to_mdev(ibdev)->dev->caps.flags & MLX4_DEV_CAP_FLAG_PORT_MNG_CHG_EV))
791f0f6f346SMoni Shoua 			smp_snoop(ibdev, port_num, in_mad, prev_lid);
792992e8e6eSJack Morgenstein 		/* slaves get node desc from FW */
793992e8e6eSJack Morgenstein 		if (!mlx4_is_slave(to_mdev(ibdev)->dev))
794225c7b1fSRoland Dreier 			node_desc_override(ibdev, out_mad);
795225c7b1fSRoland Dreier 	}
796225c7b1fSRoland Dreier 
797225c7b1fSRoland Dreier 	/* set return bit in status of directed route responses */
798225c7b1fSRoland Dreier 	if (in_mad->mad_hdr.mgmt_class == IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE)
799225c7b1fSRoland Dreier 		out_mad->mad_hdr.status |= cpu_to_be16(1 << 15);
800225c7b1fSRoland Dreier 
801225c7b1fSRoland Dreier 	if (in_mad->mad_hdr.method == IB_MGMT_METHOD_TRAP_REPRESS)
802225c7b1fSRoland Dreier 		/* no response for trap repress */
803225c7b1fSRoland Dreier 		return IB_MAD_RESULT_SUCCESS | IB_MAD_RESULT_CONSUMED;
804225c7b1fSRoland Dreier 
805225c7b1fSRoland Dreier 	return IB_MAD_RESULT_SUCCESS | IB_MAD_RESULT_REPLY;
806225c7b1fSRoland Dreier }
807225c7b1fSRoland Dreier 
808c3779134SOr Gerlitz static void edit_counter(struct mlx4_counter *cnt,
809c3779134SOr Gerlitz 					struct ib_pma_portcounters *pma_cnt)
810c3779134SOr Gerlitz {
81161a3855bSMajd Dibbiny 	ASSIGN_32BIT_COUNTER(pma_cnt->port_xmit_data,
81261a3855bSMajd Dibbiny 			     (be64_to_cpu(cnt->tx_bytes) >> 2));
81361a3855bSMajd Dibbiny 	ASSIGN_32BIT_COUNTER(pma_cnt->port_rcv_data,
81461a3855bSMajd Dibbiny 			     (be64_to_cpu(cnt->rx_bytes) >> 2));
81561a3855bSMajd Dibbiny 	ASSIGN_32BIT_COUNTER(pma_cnt->port_xmit_packets,
81661a3855bSMajd Dibbiny 			     be64_to_cpu(cnt->tx_frames));
81761a3855bSMajd Dibbiny 	ASSIGN_32BIT_COUNTER(pma_cnt->port_rcv_packets,
81861a3855bSMajd Dibbiny 			     be64_to_cpu(cnt->rx_frames));
819c3779134SOr Gerlitz }
820c3779134SOr Gerlitz 
821c3779134SOr Gerlitz static int iboe_process_mad(struct ib_device *ibdev, int mad_flags, u8 port_num,
822a97e2d86SIra Weiny 			const struct ib_wc *in_wc, const struct ib_grh *in_grh,
823a97e2d86SIra Weiny 			const struct ib_mad *in_mad, struct ib_mad *out_mad)
824c3779134SOr Gerlitz {
8259616982fSEran Ben Elisha 	struct mlx4_counter counter_stats;
826c3779134SOr Gerlitz 	struct mlx4_ib_dev *dev = to_mdev(ibdev);
827*3ba8e31dSEran Ben Elisha 	struct counter_index *tmp_counter;
828*3ba8e31dSEran Ben Elisha 	int err = IB_MAD_RESULT_FAILURE, stats_avail = 0;
829c3779134SOr Gerlitz 
830c3779134SOr Gerlitz 	if (in_mad->mad_hdr.mgmt_class != IB_MGMT_CLASS_PERF_MGMT)
831c3779134SOr Gerlitz 		return -EINVAL;
832c3779134SOr Gerlitz 
8339616982fSEran Ben Elisha 	memset(&counter_stats, 0, sizeof(counter_stats));
834*3ba8e31dSEran Ben Elisha 	mutex_lock(&dev->counters_table[port_num - 1].mutex);
835*3ba8e31dSEran Ben Elisha 	list_for_each_entry(tmp_counter,
836*3ba8e31dSEran Ben Elisha 			    &dev->counters_table[port_num - 1].counters_list,
837*3ba8e31dSEran Ben Elisha 			    list) {
8389616982fSEran Ben Elisha 		err = mlx4_get_counter_stats(dev->dev,
839*3ba8e31dSEran Ben Elisha 					     tmp_counter->index,
8409616982fSEran Ben Elisha 					     &counter_stats, 0);
841*3ba8e31dSEran Ben Elisha 		if (err) {
842c3779134SOr Gerlitz 			err = IB_MAD_RESULT_FAILURE;
843*3ba8e31dSEran Ben Elisha 			stats_avail = 0;
844*3ba8e31dSEran Ben Elisha 			break;
845*3ba8e31dSEran Ben Elisha 		}
846*3ba8e31dSEran Ben Elisha 		stats_avail = 1;
847*3ba8e31dSEran Ben Elisha 	}
848*3ba8e31dSEran Ben Elisha 	mutex_unlock(&dev->counters_table[port_num - 1].mutex);
849*3ba8e31dSEran Ben Elisha 	if (stats_avail) {
850c3779134SOr Gerlitz 		memset(out_mad->data, 0, sizeof out_mad->data);
8519616982fSEran Ben Elisha 		switch (counter_stats.counter_mode & 0xf) {
852c3779134SOr Gerlitz 		case 0:
8539616982fSEran Ben Elisha 			edit_counter(&counter_stats,
854c3779134SOr Gerlitz 				     (void *)(out_mad->data + 40));
855c3779134SOr Gerlitz 			err = IB_MAD_RESULT_SUCCESS | IB_MAD_RESULT_REPLY;
856c3779134SOr Gerlitz 			break;
857c3779134SOr Gerlitz 		default:
858c3779134SOr Gerlitz 			err = IB_MAD_RESULT_FAILURE;
859c3779134SOr Gerlitz 		}
860c3779134SOr Gerlitz 	}
861c3779134SOr Gerlitz 
862c3779134SOr Gerlitz 	return err;
863c3779134SOr Gerlitz }
864c3779134SOr Gerlitz 
865c3779134SOr Gerlitz int mlx4_ib_process_mad(struct ib_device *ibdev, int mad_flags, u8 port_num,
866a97e2d86SIra Weiny 			const struct ib_wc *in_wc, const struct ib_grh *in_grh,
8674cd7c947SIra Weiny 			const struct ib_mad_hdr *in, size_t in_mad_size,
8684cd7c947SIra Weiny 			struct ib_mad_hdr *out, size_t *out_mad_size,
8694cd7c947SIra Weiny 			u16 *out_mad_pkey_index)
870c3779134SOr Gerlitz {
8717193a141SEran Ben Elisha 	struct mlx4_ib_dev *dev = to_mdev(ibdev);
8724cd7c947SIra Weiny 	const struct ib_mad *in_mad = (const struct ib_mad *)in;
8734cd7c947SIra Weiny 	struct ib_mad *out_mad = (struct ib_mad *)out;
87443bfb972SOr Gerlitz 	enum rdma_link_layer link = rdma_port_get_link_layer(ibdev, port_num);
8754cd7c947SIra Weiny 
8763b8ab700SIra Weiny 	if (WARN_ON_ONCE(in_mad_size != sizeof(*in_mad) ||
8773b8ab700SIra Weiny 			 *out_mad_size != sizeof(*out_mad)))
8783b8ab700SIra Weiny 		return IB_MAD_RESULT_FAILURE;
8794cd7c947SIra Weiny 
88043bfb972SOr Gerlitz 	/* iboe_process_mad() which uses the HCA flow-counters to implement IB PMA
88143bfb972SOr Gerlitz 	 * queries, should be called only by VFs and for that specific purpose
88243bfb972SOr Gerlitz 	 */
88343bfb972SOr Gerlitz 	if (link == IB_LINK_LAYER_INFINIBAND) {
88443bfb972SOr Gerlitz 		if (mlx4_is_slave(dev->dev) &&
88543bfb972SOr Gerlitz 		    in_mad->mad_hdr.mgmt_class == IB_MGMT_CLASS_PERF_MGMT &&
88643bfb972SOr Gerlitz 		    in_mad->mad_hdr.attr_id == IB_PMA_PORT_COUNTERS)
887c3779134SOr Gerlitz 			return iboe_process_mad(ibdev, mad_flags, port_num, in_wc,
888c3779134SOr Gerlitz 						in_grh, in_mad, out_mad);
88943bfb972SOr Gerlitz 
89043bfb972SOr Gerlitz 		return ib_process_mad(ibdev, mad_flags, port_num, in_wc,
89143bfb972SOr Gerlitz 				      in_grh, in_mad, out_mad);
892c3779134SOr Gerlitz 	}
89343bfb972SOr Gerlitz 
89443bfb972SOr Gerlitz 	if (link == IB_LINK_LAYER_ETHERNET)
89543bfb972SOr Gerlitz 		return iboe_process_mad(ibdev, mad_flags, port_num, in_wc,
89643bfb972SOr Gerlitz 					in_grh, in_mad, out_mad);
89743bfb972SOr Gerlitz 
89843bfb972SOr Gerlitz 	return -EINVAL;
899c3779134SOr Gerlitz }
900c3779134SOr Gerlitz 
901225c7b1fSRoland Dreier static void send_handler(struct ib_mad_agent *agent,
902225c7b1fSRoland Dreier 			 struct ib_mad_send_wc *mad_send_wc)
903225c7b1fSRoland Dreier {
904992e8e6eSJack Morgenstein 	if (mad_send_wc->send_buf->context[0])
905992e8e6eSJack Morgenstein 		ib_destroy_ah(mad_send_wc->send_buf->context[0]);
906225c7b1fSRoland Dreier 	ib_free_send_mad(mad_send_wc->send_buf);
907225c7b1fSRoland Dreier }
908225c7b1fSRoland Dreier 
909225c7b1fSRoland Dreier int mlx4_ib_mad_init(struct mlx4_ib_dev *dev)
910225c7b1fSRoland Dreier {
911225c7b1fSRoland Dreier 	struct ib_mad_agent *agent;
912225c7b1fSRoland Dreier 	int p, q;
913225c7b1fSRoland Dreier 	int ret;
914fa417f7bSEli Cohen 	enum rdma_link_layer ll;
915225c7b1fSRoland Dreier 
916fa417f7bSEli Cohen 	for (p = 0; p < dev->num_ports; ++p) {
917fa417f7bSEli Cohen 		ll = rdma_port_get_link_layer(&dev->ib_dev, p + 1);
918225c7b1fSRoland Dreier 		for (q = 0; q <= 1; ++q) {
919fa417f7bSEli Cohen 			if (ll == IB_LINK_LAYER_INFINIBAND) {
920225c7b1fSRoland Dreier 				agent = ib_register_mad_agent(&dev->ib_dev, p + 1,
921225c7b1fSRoland Dreier 							      q ? IB_QPT_GSI : IB_QPT_SMI,
922225c7b1fSRoland Dreier 							      NULL, 0, send_handler,
9230f29b46dSIra Weiny 							      NULL, NULL, 0);
924225c7b1fSRoland Dreier 				if (IS_ERR(agent)) {
925225c7b1fSRoland Dreier 					ret = PTR_ERR(agent);
926225c7b1fSRoland Dreier 					goto err;
927225c7b1fSRoland Dreier 				}
928225c7b1fSRoland Dreier 				dev->send_agent[p][q] = agent;
929fa417f7bSEli Cohen 			} else
930fa417f7bSEli Cohen 				dev->send_agent[p][q] = NULL;
931fa417f7bSEli Cohen 		}
932225c7b1fSRoland Dreier 	}
933225c7b1fSRoland Dreier 
934225c7b1fSRoland Dreier 	return 0;
935225c7b1fSRoland Dreier 
936225c7b1fSRoland Dreier err:
9377ff93f8bSYevgeny Petrilin 	for (p = 0; p < dev->num_ports; ++p)
938225c7b1fSRoland Dreier 		for (q = 0; q <= 1; ++q)
939225c7b1fSRoland Dreier 			if (dev->send_agent[p][q])
940225c7b1fSRoland Dreier 				ib_unregister_mad_agent(dev->send_agent[p][q]);
941225c7b1fSRoland Dreier 
942225c7b1fSRoland Dreier 	return ret;
943225c7b1fSRoland Dreier }
944225c7b1fSRoland Dreier 
945225c7b1fSRoland Dreier void mlx4_ib_mad_cleanup(struct mlx4_ib_dev *dev)
946225c7b1fSRoland Dreier {
947225c7b1fSRoland Dreier 	struct ib_mad_agent *agent;
948225c7b1fSRoland Dreier 	int p, q;
949225c7b1fSRoland Dreier 
9507ff93f8bSYevgeny Petrilin 	for (p = 0; p < dev->num_ports; ++p) {
951225c7b1fSRoland Dreier 		for (q = 0; q <= 1; ++q) {
952225c7b1fSRoland Dreier 			agent = dev->send_agent[p][q];
953fa417f7bSEli Cohen 			if (agent) {
954225c7b1fSRoland Dreier 				dev->send_agent[p][q] = NULL;
955225c7b1fSRoland Dreier 				ib_unregister_mad_agent(agent);
956225c7b1fSRoland Dreier 			}
957fa417f7bSEli Cohen 		}
958225c7b1fSRoland Dreier 
959225c7b1fSRoland Dreier 		if (dev->sm_ah[p])
960225c7b1fSRoland Dreier 			ib_destroy_ah(dev->sm_ah[p]);
961225c7b1fSRoland Dreier 	}
962225c7b1fSRoland Dreier }
96300f5ce99SJack Morgenstein 
9642a4fae14SJack Morgenstein static void handle_lid_change_event(struct mlx4_ib_dev *dev, u8 port_num)
9652a4fae14SJack Morgenstein {
9662a4fae14SJack Morgenstein 	mlx4_ib_dispatch_event(dev, port_num, IB_EVENT_LID_CHANGE);
9672a4fae14SJack Morgenstein 
9682a4fae14SJack Morgenstein 	if (mlx4_is_master(dev->dev) && !dev->sriov.is_going_down)
9692a4fae14SJack Morgenstein 		mlx4_gen_slaves_port_mgt_ev(dev->dev, port_num,
9702a4fae14SJack Morgenstein 					    MLX4_EQ_PORT_INFO_LID_CHANGE_MASK);
9712a4fae14SJack Morgenstein }
9722a4fae14SJack Morgenstein 
973b9c5d6a6SOren Duer static void handle_client_rereg_event(struct mlx4_ib_dev *dev, u8 port_num)
974b9c5d6a6SOren Duer {
975a0c64a17SJack Morgenstein 	/* re-configure the alias-guid and mcg's */
976b9c5d6a6SOren Duer 	if (mlx4_is_master(dev->dev)) {
977a0c64a17SJack Morgenstein 		mlx4_ib_invalidate_all_guid_record(dev, port_num);
978a0c64a17SJack Morgenstein 
9792a4fae14SJack Morgenstein 		if (!dev->sriov.is_going_down) {
980b9c5d6a6SOren Duer 			mlx4_ib_mcg_port_cleanup(&dev->sriov.demux[port_num - 1], 0);
9812a4fae14SJack Morgenstein 			mlx4_gen_slaves_port_mgt_ev(dev->dev, port_num,
9822a4fae14SJack Morgenstein 						    MLX4_EQ_PORT_INFO_CLIENT_REREG_MASK);
9832a4fae14SJack Morgenstein 		}
984b9c5d6a6SOren Duer 	}
985b9c5d6a6SOren Duer 	mlx4_ib_dispatch_event(dev, port_num, IB_EVENT_CLIENT_REREGISTER);
986b9c5d6a6SOren Duer }
987b9c5d6a6SOren Duer 
9882a4fae14SJack Morgenstein static void propagate_pkey_ev(struct mlx4_ib_dev *dev, int port_num,
9892a4fae14SJack Morgenstein 			      struct mlx4_eqe *eqe)
9902a4fae14SJack Morgenstein {
9912a4fae14SJack Morgenstein 	__propagate_pkey_ev(dev, port_num, GET_BLK_PTR_FROM_EQE(eqe),
9922a4fae14SJack Morgenstein 			    GET_MASK_FROM_EQE(eqe));
9932a4fae14SJack Morgenstein }
9942a4fae14SJack Morgenstein 
9952a4fae14SJack Morgenstein static void handle_slaves_guid_change(struct mlx4_ib_dev *dev, u8 port_num,
9962a4fae14SJack Morgenstein 				      u32 guid_tbl_blk_num, u32 change_bitmap)
9972a4fae14SJack Morgenstein {
9982a4fae14SJack Morgenstein 	struct ib_smp *in_mad  = NULL;
9992a4fae14SJack Morgenstein 	struct ib_smp *out_mad  = NULL;
10002a4fae14SJack Morgenstein 	u16 i;
10012a4fae14SJack Morgenstein 
10022a4fae14SJack Morgenstein 	if (!mlx4_is_mfunc(dev->dev) || !mlx4_is_master(dev->dev))
10032a4fae14SJack Morgenstein 		return;
10042a4fae14SJack Morgenstein 
10052a4fae14SJack Morgenstein 	in_mad  = kmalloc(sizeof *in_mad, GFP_KERNEL);
10062a4fae14SJack Morgenstein 	out_mad = kmalloc(sizeof *out_mad, GFP_KERNEL);
10072a4fae14SJack Morgenstein 	if (!in_mad || !out_mad) {
10082a4fae14SJack Morgenstein 		mlx4_ib_warn(&dev->ib_dev, "failed to allocate memory for guid info mads\n");
10092a4fae14SJack Morgenstein 		goto out;
10102a4fae14SJack Morgenstein 	}
10112a4fae14SJack Morgenstein 
10122a4fae14SJack Morgenstein 	guid_tbl_blk_num  *= 4;
10132a4fae14SJack Morgenstein 
10142a4fae14SJack Morgenstein 	for (i = 0; i < 4; i++) {
10152a4fae14SJack Morgenstein 		if (change_bitmap && (!((change_bitmap >> (8 * i)) & 0xff)))
10162a4fae14SJack Morgenstein 			continue;
10172a4fae14SJack Morgenstein 		memset(in_mad, 0, sizeof *in_mad);
10182a4fae14SJack Morgenstein 		memset(out_mad, 0, sizeof *out_mad);
10192a4fae14SJack Morgenstein 
10202a4fae14SJack Morgenstein 		in_mad->base_version  = 1;
10212a4fae14SJack Morgenstein 		in_mad->mgmt_class    = IB_MGMT_CLASS_SUBN_LID_ROUTED;
10222a4fae14SJack Morgenstein 		in_mad->class_version = 1;
10232a4fae14SJack Morgenstein 		in_mad->method        = IB_MGMT_METHOD_GET;
10242a4fae14SJack Morgenstein 		in_mad->attr_id       = IB_SMP_ATTR_GUID_INFO;
10252a4fae14SJack Morgenstein 		in_mad->attr_mod      = cpu_to_be32(guid_tbl_blk_num + i);
10262a4fae14SJack Morgenstein 
10272a4fae14SJack Morgenstein 		if (mlx4_MAD_IFC(dev,
10282a4fae14SJack Morgenstein 				 MLX4_MAD_IFC_IGNORE_KEYS | MLX4_MAD_IFC_NET_VIEW,
10292a4fae14SJack Morgenstein 				 port_num, NULL, NULL, in_mad, out_mad)) {
10302a4fae14SJack Morgenstein 			mlx4_ib_warn(&dev->ib_dev, "Failed in get GUID INFO MAD_IFC\n");
10312a4fae14SJack Morgenstein 			goto out;
10322a4fae14SJack Morgenstein 		}
10332a4fae14SJack Morgenstein 
10342a4fae14SJack Morgenstein 		mlx4_ib_update_cache_on_guid_change(dev, guid_tbl_blk_num + i,
10352a4fae14SJack Morgenstein 						    port_num,
10362a4fae14SJack Morgenstein 						    (u8 *)(&((struct ib_smp *)out_mad)->data));
10372a4fae14SJack Morgenstein 		mlx4_ib_notify_slaves_on_guid_change(dev, guid_tbl_blk_num + i,
10382a4fae14SJack Morgenstein 						     port_num,
10392a4fae14SJack Morgenstein 						     (u8 *)(&((struct ib_smp *)out_mad)->data));
10402a4fae14SJack Morgenstein 	}
10412a4fae14SJack Morgenstein 
10422a4fae14SJack Morgenstein out:
10432a4fae14SJack Morgenstein 	kfree(in_mad);
10442a4fae14SJack Morgenstein 	kfree(out_mad);
10452a4fae14SJack Morgenstein 	return;
10462a4fae14SJack Morgenstein }
10472a4fae14SJack Morgenstein 
104800f5ce99SJack Morgenstein void handle_port_mgmt_change_event(struct work_struct *work)
104900f5ce99SJack Morgenstein {
105000f5ce99SJack Morgenstein 	struct ib_event_work *ew = container_of(work, struct ib_event_work, work);
105100f5ce99SJack Morgenstein 	struct mlx4_ib_dev *dev = ew->ib_dev;
105200f5ce99SJack Morgenstein 	struct mlx4_eqe *eqe = &(ew->ib_eqe);
105300f5ce99SJack Morgenstein 	u8 port = eqe->event.port_mgmt_change.port;
105400f5ce99SJack Morgenstein 	u32 changed_attr;
10552a4fae14SJack Morgenstein 	u32 tbl_block;
10562a4fae14SJack Morgenstein 	u32 change_bitmap;
105700f5ce99SJack Morgenstein 
105800f5ce99SJack Morgenstein 	switch (eqe->subtype) {
105900f5ce99SJack Morgenstein 	case MLX4_DEV_PMC_SUBTYPE_PORT_INFO:
106000f5ce99SJack Morgenstein 		changed_attr = be32_to_cpu(eqe->event.port_mgmt_change.params.port_info.changed_attr);
106100f5ce99SJack Morgenstein 
106200f5ce99SJack Morgenstein 		/* Update the SM ah - This should be done before handling
106300f5ce99SJack Morgenstein 		   the other changed attributes so that MADs can be sent to the SM */
106400f5ce99SJack Morgenstein 		if (changed_attr & MSTR_SM_CHANGE_MASK) {
106500f5ce99SJack Morgenstein 			u16 lid = be16_to_cpu(eqe->event.port_mgmt_change.params.port_info.mstr_sm_lid);
106600f5ce99SJack Morgenstein 			u8 sl = eqe->event.port_mgmt_change.params.port_info.mstr_sm_sl & 0xf;
106700f5ce99SJack Morgenstein 			update_sm_ah(dev, port, lid, sl);
106800f5ce99SJack Morgenstein 		}
106900f5ce99SJack Morgenstein 
107000f5ce99SJack Morgenstein 		/* Check if it is a lid change event */
107100f5ce99SJack Morgenstein 		if (changed_attr & MLX4_EQ_PORT_INFO_LID_CHANGE_MASK)
10722a4fae14SJack Morgenstein 			handle_lid_change_event(dev, port);
107300f5ce99SJack Morgenstein 
107400f5ce99SJack Morgenstein 		/* Generate GUID changed event */
10752a4fae14SJack Morgenstein 		if (changed_attr & MLX4_EQ_PORT_INFO_GID_PFX_CHANGE_MASK) {
107600f5ce99SJack Morgenstein 			mlx4_ib_dispatch_event(dev, port, IB_EVENT_GID_CHANGE);
10772a4fae14SJack Morgenstein 			/*if master, notify all slaves*/
10782a4fae14SJack Morgenstein 			if (mlx4_is_master(dev->dev))
10792a4fae14SJack Morgenstein 				mlx4_gen_slaves_port_mgt_ev(dev->dev, port,
10802a4fae14SJack Morgenstein 							    MLX4_EQ_PORT_INFO_GID_PFX_CHANGE_MASK);
10812a4fae14SJack Morgenstein 		}
108200f5ce99SJack Morgenstein 
108300f5ce99SJack Morgenstein 		if (changed_attr & MLX4_EQ_PORT_INFO_CLIENT_REREG_MASK)
1084b9c5d6a6SOren Duer 			handle_client_rereg_event(dev, port);
108500f5ce99SJack Morgenstein 		break;
108600f5ce99SJack Morgenstein 
108700f5ce99SJack Morgenstein 	case MLX4_DEV_PMC_SUBTYPE_PKEY_TABLE:
108800f5ce99SJack Morgenstein 		mlx4_ib_dispatch_event(dev, port, IB_EVENT_PKEY_CHANGE);
10892a4fae14SJack Morgenstein 		if (mlx4_is_master(dev->dev) && !dev->sriov.is_going_down)
10902a4fae14SJack Morgenstein 			propagate_pkey_ev(dev, port, eqe);
109100f5ce99SJack Morgenstein 		break;
109200f5ce99SJack Morgenstein 	case MLX4_DEV_PMC_SUBTYPE_GUID_INFO:
10936634961cSJack Morgenstein 		/* paravirtualized master's guid is guid 0 -- does not change */
10946634961cSJack Morgenstein 		if (!mlx4_is_master(dev->dev))
109500f5ce99SJack Morgenstein 			mlx4_ib_dispatch_event(dev, port, IB_EVENT_GID_CHANGE);
10962a4fae14SJack Morgenstein 		/*if master, notify relevant slaves*/
10972a4fae14SJack Morgenstein 		else if (!dev->sriov.is_going_down) {
10982a4fae14SJack Morgenstein 			tbl_block = GET_BLK_PTR_FROM_EQE(eqe);
10992a4fae14SJack Morgenstein 			change_bitmap = GET_MASK_FROM_EQE(eqe);
11002a4fae14SJack Morgenstein 			handle_slaves_guid_change(dev, port, tbl_block, change_bitmap);
11012a4fae14SJack Morgenstein 		}
110200f5ce99SJack Morgenstein 		break;
110300f5ce99SJack Morgenstein 	default:
110400f5ce99SJack Morgenstein 		pr_warn("Unsupported subtype 0x%x for "
110500f5ce99SJack Morgenstein 			"Port Management Change event\n", eqe->subtype);
110600f5ce99SJack Morgenstein 	}
110700f5ce99SJack Morgenstein 
110800f5ce99SJack Morgenstein 	kfree(ew);
110900f5ce99SJack Morgenstein }
111000f5ce99SJack Morgenstein 
111100f5ce99SJack Morgenstein void mlx4_ib_dispatch_event(struct mlx4_ib_dev *dev, u8 port_num,
111200f5ce99SJack Morgenstein 			    enum ib_event_type type)
111300f5ce99SJack Morgenstein {
111400f5ce99SJack Morgenstein 	struct ib_event event;
111500f5ce99SJack Morgenstein 
111600f5ce99SJack Morgenstein 	event.device		= &dev->ib_dev;
111700f5ce99SJack Morgenstein 	event.element.port_num	= port_num;
111800f5ce99SJack Morgenstein 	event.event		= type;
111900f5ce99SJack Morgenstein 
112000f5ce99SJack Morgenstein 	ib_dispatch_event(&event);
112100f5ce99SJack Morgenstein }
1122fc06573dSJack Morgenstein 
1123fc06573dSJack Morgenstein static void mlx4_ib_tunnel_comp_handler(struct ib_cq *cq, void *arg)
1124fc06573dSJack Morgenstein {
1125fc06573dSJack Morgenstein 	unsigned long flags;
1126fc06573dSJack Morgenstein 	struct mlx4_ib_demux_pv_ctx *ctx = cq->cq_context;
1127fc06573dSJack Morgenstein 	struct mlx4_ib_dev *dev = to_mdev(ctx->ib_dev);
1128fc06573dSJack Morgenstein 	spin_lock_irqsave(&dev->sriov.going_down_lock, flags);
1129fc06573dSJack Morgenstein 	if (!dev->sriov.is_going_down && ctx->state == DEMUX_PV_STATE_ACTIVE)
1130fc06573dSJack Morgenstein 		queue_work(ctx->wq, &ctx->work);
1131fc06573dSJack Morgenstein 	spin_unlock_irqrestore(&dev->sriov.going_down_lock, flags);
1132fc06573dSJack Morgenstein }
1133fc06573dSJack Morgenstein 
1134fc06573dSJack Morgenstein static int mlx4_ib_post_pv_qp_buf(struct mlx4_ib_demux_pv_ctx *ctx,
1135fc06573dSJack Morgenstein 				  struct mlx4_ib_demux_pv_qp *tun_qp,
1136fc06573dSJack Morgenstein 				  int index)
1137fc06573dSJack Morgenstein {
1138fc06573dSJack Morgenstein 	struct ib_sge sg_list;
1139fc06573dSJack Morgenstein 	struct ib_recv_wr recv_wr, *bad_recv_wr;
1140fc06573dSJack Morgenstein 	int size;
1141fc06573dSJack Morgenstein 
1142fc06573dSJack Morgenstein 	size = (tun_qp->qp->qp_type == IB_QPT_UD) ?
1143fc06573dSJack Morgenstein 		sizeof (struct mlx4_tunnel_mad) : sizeof (struct mlx4_mad_rcv_buf);
1144fc06573dSJack Morgenstein 
1145fc06573dSJack Morgenstein 	sg_list.addr = tun_qp->ring[index].map;
1146fc06573dSJack Morgenstein 	sg_list.length = size;
11477dd97576SJason Gunthorpe 	sg_list.lkey = ctx->pd->local_dma_lkey;
1148fc06573dSJack Morgenstein 
1149fc06573dSJack Morgenstein 	recv_wr.next = NULL;
1150fc06573dSJack Morgenstein 	recv_wr.sg_list = &sg_list;
1151fc06573dSJack Morgenstein 	recv_wr.num_sge = 1;
1152fc06573dSJack Morgenstein 	recv_wr.wr_id = (u64) index | MLX4_TUN_WRID_RECV |
1153fc06573dSJack Morgenstein 		MLX4_TUN_SET_WRID_QPN(tun_qp->proxy_qpt);
1154fc06573dSJack Morgenstein 	ib_dma_sync_single_for_device(ctx->ib_dev, tun_qp->ring[index].map,
1155fc06573dSJack Morgenstein 				      size, DMA_FROM_DEVICE);
1156fc06573dSJack Morgenstein 	return ib_post_recv(tun_qp->qp, &recv_wr, &bad_recv_wr);
1157fc06573dSJack Morgenstein }
1158fc06573dSJack Morgenstein 
115937bfc7c1SJack Morgenstein static int mlx4_ib_multiplex_sa_handler(struct ib_device *ibdev, int port,
116037bfc7c1SJack Morgenstein 		int slave, struct ib_sa_mad *sa_mad)
116137bfc7c1SJack Morgenstein {
1162b9c5d6a6SOren Duer 	int ret = 0;
1163b9c5d6a6SOren Duer 
1164b9c5d6a6SOren Duer 	/* dispatch to different sa handlers */
1165b9c5d6a6SOren Duer 	switch (be16_to_cpu(sa_mad->mad_hdr.attr_id)) {
1166b9c5d6a6SOren Duer 	case IB_SA_ATTR_MC_MEMBER_REC:
1167b9c5d6a6SOren Duer 		ret = mlx4_ib_mcg_multiplex_handler(ibdev, port, slave, sa_mad);
1168b9c5d6a6SOren Duer 		break;
1169b9c5d6a6SOren Duer 	default:
1170b9c5d6a6SOren Duer 		break;
1171b9c5d6a6SOren Duer 	}
1172b9c5d6a6SOren Duer 	return ret;
117337bfc7c1SJack Morgenstein }
117437bfc7c1SJack Morgenstein 
117537bfc7c1SJack Morgenstein static int is_proxy_qp0(struct mlx4_ib_dev *dev, int qpn, int slave)
117637bfc7c1SJack Morgenstein {
117747605df9SJack Morgenstein 	int proxy_start = dev->dev->phys_caps.base_proxy_sqpn + 8 * slave;
117837bfc7c1SJack Morgenstein 
117947605df9SJack Morgenstein 	return (qpn >= proxy_start && qpn <= proxy_start + 1);
118037bfc7c1SJack Morgenstein }
118137bfc7c1SJack Morgenstein 
118237bfc7c1SJack Morgenstein 
118337bfc7c1SJack Morgenstein int mlx4_ib_send_to_wire(struct mlx4_ib_dev *dev, int slave, u8 port,
11845ea8bbfcSJack Morgenstein 			 enum ib_qp_type dest_qpt, u16 pkey_index,
11855ea8bbfcSJack Morgenstein 			 u32 remote_qpn, u32 qkey, struct ib_ah_attr *attr,
11865ea8bbfcSJack Morgenstein 			 u8 *s_mac, struct ib_mad *mad)
118737bfc7c1SJack Morgenstein {
118837bfc7c1SJack Morgenstein 	struct ib_sge list;
118937bfc7c1SJack Morgenstein 	struct ib_send_wr wr, *bad_wr;
119037bfc7c1SJack Morgenstein 	struct mlx4_ib_demux_pv_ctx *sqp_ctx;
119137bfc7c1SJack Morgenstein 	struct mlx4_ib_demux_pv_qp *sqp;
119237bfc7c1SJack Morgenstein 	struct mlx4_mad_snd_buf *sqp_mad;
119337bfc7c1SJack Morgenstein 	struct ib_ah *ah;
119437bfc7c1SJack Morgenstein 	struct ib_qp *send_qp = NULL;
119537bfc7c1SJack Morgenstein 	unsigned wire_tx_ix = 0;
119637bfc7c1SJack Morgenstein 	int ret = 0;
119737bfc7c1SJack Morgenstein 	u16 wire_pkey_ix;
119837bfc7c1SJack Morgenstein 	int src_qpnum;
119937bfc7c1SJack Morgenstein 	u8 sgid_index;
120037bfc7c1SJack Morgenstein 
120137bfc7c1SJack Morgenstein 
120237bfc7c1SJack Morgenstein 	sqp_ctx = dev->sriov.sqps[port-1];
120337bfc7c1SJack Morgenstein 
120437bfc7c1SJack Morgenstein 	/* check if proxy qp created */
120537bfc7c1SJack Morgenstein 	if (!sqp_ctx || sqp_ctx->state != DEMUX_PV_STATE_ACTIVE)
120637bfc7c1SJack Morgenstein 		return -EAGAIN;
120737bfc7c1SJack Morgenstein 
120837bfc7c1SJack Morgenstein 	if (dest_qpt == IB_QPT_SMI) {
120937bfc7c1SJack Morgenstein 		src_qpnum = 0;
121037bfc7c1SJack Morgenstein 		sqp = &sqp_ctx->qp[0];
121137bfc7c1SJack Morgenstein 		wire_pkey_ix = dev->pkeys.virt2phys_pkey[slave][port - 1][0];
121237bfc7c1SJack Morgenstein 	} else {
121337bfc7c1SJack Morgenstein 		src_qpnum = 1;
121437bfc7c1SJack Morgenstein 		sqp = &sqp_ctx->qp[1];
121537bfc7c1SJack Morgenstein 		wire_pkey_ix = dev->pkeys.virt2phys_pkey[slave][port - 1][pkey_index];
121637bfc7c1SJack Morgenstein 	}
121737bfc7c1SJack Morgenstein 
121837bfc7c1SJack Morgenstein 	send_qp = sqp->qp;
121937bfc7c1SJack Morgenstein 
122037bfc7c1SJack Morgenstein 	/* create ah */
122137bfc7c1SJack Morgenstein 	sgid_index = attr->grh.sgid_index;
122237bfc7c1SJack Morgenstein 	attr->grh.sgid_index = 0;
122337bfc7c1SJack Morgenstein 	ah = ib_create_ah(sqp_ctx->pd, attr);
122437bfc7c1SJack Morgenstein 	if (IS_ERR(ah))
122537bfc7c1SJack Morgenstein 		return -ENOMEM;
122637bfc7c1SJack Morgenstein 	attr->grh.sgid_index = sgid_index;
122737bfc7c1SJack Morgenstein 	to_mah(ah)->av.ib.gid_index = sgid_index;
122837bfc7c1SJack Morgenstein 	/* get rid of force-loopback bit */
122937bfc7c1SJack Morgenstein 	to_mah(ah)->av.ib.port_pd &= cpu_to_be32(0x7FFFFFFF);
123037bfc7c1SJack Morgenstein 	spin_lock(&sqp->tx_lock);
123137bfc7c1SJack Morgenstein 	if (sqp->tx_ix_head - sqp->tx_ix_tail >=
123237bfc7c1SJack Morgenstein 	    (MLX4_NUM_TUNNEL_BUFS - 1))
123337bfc7c1SJack Morgenstein 		ret = -EAGAIN;
123437bfc7c1SJack Morgenstein 	else
123537bfc7c1SJack Morgenstein 		wire_tx_ix = (++sqp->tx_ix_head) & (MLX4_NUM_TUNNEL_BUFS - 1);
123637bfc7c1SJack Morgenstein 	spin_unlock(&sqp->tx_lock);
123737bfc7c1SJack Morgenstein 	if (ret)
123837bfc7c1SJack Morgenstein 		goto out;
123937bfc7c1SJack Morgenstein 
124037bfc7c1SJack Morgenstein 	sqp_mad = (struct mlx4_mad_snd_buf *) (sqp->tx_ring[wire_tx_ix].buf.addr);
124137bfc7c1SJack Morgenstein 	if (sqp->tx_ring[wire_tx_ix].ah)
124237bfc7c1SJack Morgenstein 		ib_destroy_ah(sqp->tx_ring[wire_tx_ix].ah);
124337bfc7c1SJack Morgenstein 	sqp->tx_ring[wire_tx_ix].ah = ah;
124437bfc7c1SJack Morgenstein 	ib_dma_sync_single_for_cpu(&dev->ib_dev,
124537bfc7c1SJack Morgenstein 				   sqp->tx_ring[wire_tx_ix].buf.map,
124637bfc7c1SJack Morgenstein 				   sizeof (struct mlx4_mad_snd_buf),
124737bfc7c1SJack Morgenstein 				   DMA_TO_DEVICE);
124837bfc7c1SJack Morgenstein 
124937bfc7c1SJack Morgenstein 	memcpy(&sqp_mad->payload, mad, sizeof *mad);
125037bfc7c1SJack Morgenstein 
125137bfc7c1SJack Morgenstein 	ib_dma_sync_single_for_device(&dev->ib_dev,
125237bfc7c1SJack Morgenstein 				      sqp->tx_ring[wire_tx_ix].buf.map,
125337bfc7c1SJack Morgenstein 				      sizeof (struct mlx4_mad_snd_buf),
125437bfc7c1SJack Morgenstein 				      DMA_TO_DEVICE);
125537bfc7c1SJack Morgenstein 
125637bfc7c1SJack Morgenstein 	list.addr = sqp->tx_ring[wire_tx_ix].buf.map;
125737bfc7c1SJack Morgenstein 	list.length = sizeof (struct mlx4_mad_snd_buf);
12587dd97576SJason Gunthorpe 	list.lkey = sqp_ctx->pd->local_dma_lkey;
125937bfc7c1SJack Morgenstein 
126037bfc7c1SJack Morgenstein 	wr.wr.ud.ah = ah;
126137bfc7c1SJack Morgenstein 	wr.wr.ud.port_num = port;
126237bfc7c1SJack Morgenstein 	wr.wr.ud.pkey_index = wire_pkey_ix;
126337bfc7c1SJack Morgenstein 	wr.wr.ud.remote_qkey = qkey;
126437bfc7c1SJack Morgenstein 	wr.wr.ud.remote_qpn = remote_qpn;
126537bfc7c1SJack Morgenstein 	wr.next = NULL;
126637bfc7c1SJack Morgenstein 	wr.wr_id = ((u64) wire_tx_ix) | MLX4_TUN_SET_WRID_QPN(src_qpnum);
126737bfc7c1SJack Morgenstein 	wr.sg_list = &list;
126837bfc7c1SJack Morgenstein 	wr.num_sge = 1;
126937bfc7c1SJack Morgenstein 	wr.opcode = IB_WR_SEND;
127037bfc7c1SJack Morgenstein 	wr.send_flags = IB_SEND_SIGNALED;
12715ea8bbfcSJack Morgenstein 	if (s_mac)
12725ea8bbfcSJack Morgenstein 		memcpy(to_mah(ah)->av.eth.s_mac, s_mac, 6);
12735ea8bbfcSJack Morgenstein 
127437bfc7c1SJack Morgenstein 
127537bfc7c1SJack Morgenstein 	ret = ib_post_send(send_qp, &wr, &bad_wr);
127637bfc7c1SJack Morgenstein out:
127737bfc7c1SJack Morgenstein 	if (ret)
127837bfc7c1SJack Morgenstein 		ib_destroy_ah(ah);
127937bfc7c1SJack Morgenstein 	return ret;
128037bfc7c1SJack Morgenstein }
128137bfc7c1SJack Morgenstein 
1282b6ffaeffSJack Morgenstein static int get_slave_base_gid_ix(struct mlx4_ib_dev *dev, int slave, int port)
1283b6ffaeffSJack Morgenstein {
1284b6ffaeffSJack Morgenstein 	if (rdma_port_get_link_layer(&dev->ib_dev, port) == IB_LINK_LAYER_INFINIBAND)
1285b6ffaeffSJack Morgenstein 		return slave;
1286449fc488SMatan Barak 	return mlx4_get_base_gid_ix(dev->dev, slave, port);
1287b6ffaeffSJack Morgenstein }
1288b6ffaeffSJack Morgenstein 
1289b6ffaeffSJack Morgenstein static void fill_in_real_sgid_index(struct mlx4_ib_dev *dev, int slave, int port,
1290b6ffaeffSJack Morgenstein 				    struct ib_ah_attr *ah_attr)
1291b6ffaeffSJack Morgenstein {
1292b6ffaeffSJack Morgenstein 	if (rdma_port_get_link_layer(&dev->ib_dev, port) == IB_LINK_LAYER_INFINIBAND)
1293b6ffaeffSJack Morgenstein 		ah_attr->grh.sgid_index = slave;
1294b6ffaeffSJack Morgenstein 	else
1295b6ffaeffSJack Morgenstein 		ah_attr->grh.sgid_index += get_slave_base_gid_ix(dev, slave, port);
1296b6ffaeffSJack Morgenstein }
1297b6ffaeffSJack Morgenstein 
129837bfc7c1SJack Morgenstein static void mlx4_ib_multiplex_mad(struct mlx4_ib_demux_pv_ctx *ctx, struct ib_wc *wc)
129937bfc7c1SJack Morgenstein {
130037bfc7c1SJack Morgenstein 	struct mlx4_ib_dev *dev = to_mdev(ctx->ib_dev);
130137bfc7c1SJack Morgenstein 	struct mlx4_ib_demux_pv_qp *tun_qp = &ctx->qp[MLX4_TUN_WRID_QPN(wc->wr_id)];
130237bfc7c1SJack Morgenstein 	int wr_ix = wc->wr_id & (MLX4_NUM_TUNNEL_BUFS - 1);
130337bfc7c1SJack Morgenstein 	struct mlx4_tunnel_mad *tunnel = tun_qp->ring[wr_ix].addr;
130437bfc7c1SJack Morgenstein 	struct mlx4_ib_ah ah;
130537bfc7c1SJack Morgenstein 	struct ib_ah_attr ah_attr;
130637bfc7c1SJack Morgenstein 	u8 *slave_id;
130737bfc7c1SJack Morgenstein 	int slave;
1308449fc488SMatan Barak 	int port;
130937bfc7c1SJack Morgenstein 
131037bfc7c1SJack Morgenstein 	/* Get slave that sent this packet */
131147605df9SJack Morgenstein 	if (wc->src_qp < dev->dev->phys_caps.base_proxy_sqpn ||
131247605df9SJack Morgenstein 	    wc->src_qp >= dev->dev->phys_caps.base_proxy_sqpn + 8 * MLX4_MFUNC_MAX ||
131337bfc7c1SJack Morgenstein 	    (wc->src_qp & 0x1) != ctx->port - 1 ||
131437bfc7c1SJack Morgenstein 	    wc->src_qp & 0x4) {
131537bfc7c1SJack Morgenstein 		mlx4_ib_warn(ctx->ib_dev, "can't multiplex bad sqp:%d\n", wc->src_qp);
131637bfc7c1SJack Morgenstein 		return;
131737bfc7c1SJack Morgenstein 	}
131847605df9SJack Morgenstein 	slave = ((wc->src_qp & ~0x7) - dev->dev->phys_caps.base_proxy_sqpn) / 8;
131937bfc7c1SJack Morgenstein 	if (slave != ctx->slave) {
132037bfc7c1SJack Morgenstein 		mlx4_ib_warn(ctx->ib_dev, "can't multiplex bad sqp:%d: "
132137bfc7c1SJack Morgenstein 			     "belongs to another slave\n", wc->src_qp);
132237bfc7c1SJack Morgenstein 		return;
132337bfc7c1SJack Morgenstein 	}
132437bfc7c1SJack Morgenstein 
132537bfc7c1SJack Morgenstein 	/* Map transaction ID */
132637bfc7c1SJack Morgenstein 	ib_dma_sync_single_for_cpu(ctx->ib_dev, tun_qp->ring[wr_ix].map,
132737bfc7c1SJack Morgenstein 				   sizeof (struct mlx4_tunnel_mad),
132837bfc7c1SJack Morgenstein 				   DMA_FROM_DEVICE);
132937bfc7c1SJack Morgenstein 	switch (tunnel->mad.mad_hdr.method) {
133037bfc7c1SJack Morgenstein 	case IB_MGMT_METHOD_SET:
133137bfc7c1SJack Morgenstein 	case IB_MGMT_METHOD_GET:
133237bfc7c1SJack Morgenstein 	case IB_MGMT_METHOD_REPORT:
133337bfc7c1SJack Morgenstein 	case IB_SA_METHOD_GET_TABLE:
133437bfc7c1SJack Morgenstein 	case IB_SA_METHOD_DELETE:
133537bfc7c1SJack Morgenstein 	case IB_SA_METHOD_GET_MULTI:
133637bfc7c1SJack Morgenstein 	case IB_SA_METHOD_GET_TRACE_TBL:
133737bfc7c1SJack Morgenstein 		slave_id = (u8 *) &tunnel->mad.mad_hdr.tid;
133837bfc7c1SJack Morgenstein 		if (*slave_id) {
133937bfc7c1SJack Morgenstein 			mlx4_ib_warn(ctx->ib_dev, "egress mad has non-null tid msb:%d "
134037bfc7c1SJack Morgenstein 				     "class:%d slave:%d\n", *slave_id,
134137bfc7c1SJack Morgenstein 				     tunnel->mad.mad_hdr.mgmt_class, slave);
134237bfc7c1SJack Morgenstein 			return;
134337bfc7c1SJack Morgenstein 		} else
134437bfc7c1SJack Morgenstein 			*slave_id = slave;
134537bfc7c1SJack Morgenstein 	default:
134637bfc7c1SJack Morgenstein 		/* nothing */;
134737bfc7c1SJack Morgenstein 	}
134837bfc7c1SJack Morgenstein 
134937bfc7c1SJack Morgenstein 	/* Class-specific handling */
135037bfc7c1SJack Morgenstein 	switch (tunnel->mad.mad_hdr.mgmt_class) {
135197982f5aSJack Morgenstein 	case IB_MGMT_CLASS_SUBN_LID_ROUTED:
135297982f5aSJack Morgenstein 	case IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE:
135397982f5aSJack Morgenstein 		if (slave != mlx4_master_func_num(dev->dev) &&
135497982f5aSJack Morgenstein 		    !mlx4_vf_smi_enabled(dev->dev, slave, ctx->port))
135597982f5aSJack Morgenstein 			return;
135697982f5aSJack Morgenstein 		break;
135737bfc7c1SJack Morgenstein 	case IB_MGMT_CLASS_SUBN_ADM:
135837bfc7c1SJack Morgenstein 		if (mlx4_ib_multiplex_sa_handler(ctx->ib_dev, ctx->port, slave,
135937bfc7c1SJack Morgenstein 			      (struct ib_sa_mad *) &tunnel->mad))
136037bfc7c1SJack Morgenstein 			return;
136137bfc7c1SJack Morgenstein 		break;
13623cf69cc8SAmir Vadai 	case IB_MGMT_CLASS_CM:
13633cf69cc8SAmir Vadai 		if (mlx4_ib_multiplex_cm_handler(ctx->ib_dev, ctx->port, slave,
13643cf69cc8SAmir Vadai 			      (struct ib_mad *) &tunnel->mad))
13653cf69cc8SAmir Vadai 			return;
13663cf69cc8SAmir Vadai 		break;
136737bfc7c1SJack Morgenstein 	case IB_MGMT_CLASS_DEVICE_MGMT:
136837bfc7c1SJack Morgenstein 		if (tunnel->mad.mad_hdr.method != IB_MGMT_METHOD_GET &&
136937bfc7c1SJack Morgenstein 		    tunnel->mad.mad_hdr.method != IB_MGMT_METHOD_SET)
137037bfc7c1SJack Morgenstein 			return;
137137bfc7c1SJack Morgenstein 		break;
137237bfc7c1SJack Morgenstein 	default:
137337bfc7c1SJack Morgenstein 		/* Drop unsupported classes for slaves in tunnel mode */
137437bfc7c1SJack Morgenstein 		if (slave != mlx4_master_func_num(dev->dev)) {
137537bfc7c1SJack Morgenstein 			mlx4_ib_warn(ctx->ib_dev, "dropping unsupported egress mad from class:%d "
137637bfc7c1SJack Morgenstein 				     "for slave:%d\n", tunnel->mad.mad_hdr.mgmt_class, slave);
137737bfc7c1SJack Morgenstein 			return;
137837bfc7c1SJack Morgenstein 		}
137937bfc7c1SJack Morgenstein 	}
138037bfc7c1SJack Morgenstein 
138137bfc7c1SJack Morgenstein 	/* We are using standard ib_core services to send the mad, so generate a
138237bfc7c1SJack Morgenstein 	 * stadard address handle by decoding the tunnelled mlx4_ah fields */
138337bfc7c1SJack Morgenstein 	memcpy(&ah.av, &tunnel->hdr.av, sizeof (struct mlx4_av));
138437bfc7c1SJack Morgenstein 	ah.ibah.device = ctx->ib_dev;
1385430910b1SOr Gerlitz 
1386430910b1SOr Gerlitz 	port = be32_to_cpu(ah.av.ib.port_pd) >> 24;
1387430910b1SOr Gerlitz 	port = mlx4_slave_convert_port(dev->dev, slave, port);
1388430910b1SOr Gerlitz 	if (port < 0)
1389430910b1SOr Gerlitz 		return;
1390430910b1SOr Gerlitz 	ah.av.ib.port_pd = cpu_to_be32(port << 24 | (be32_to_cpu(ah.av.ib.port_pd) & 0xffffff));
1391430910b1SOr Gerlitz 
139237bfc7c1SJack Morgenstein 	mlx4_ib_query_ah(&ah.ibah, &ah_attr);
13936ee51a4eSJack Morgenstein 	if (ah_attr.ah_flags & IB_AH_GRH)
1394b6ffaeffSJack Morgenstein 		fill_in_real_sgid_index(dev, slave, ctx->port, &ah_attr);
139537bfc7c1SJack Morgenstein 
13965ea8bbfcSJack Morgenstein 	memcpy(ah_attr.dmac, tunnel->hdr.mac, 6);
13975ea8bbfcSJack Morgenstein 	ah_attr.vlan_id = be16_to_cpu(tunnel->hdr.vlan);
13985ea8bbfcSJack Morgenstein 	/* if slave have default vlan use it */
13995ea8bbfcSJack Morgenstein 	mlx4_get_slave_default_vlan(dev->dev, ctx->port, slave,
14005ea8bbfcSJack Morgenstein 				    &ah_attr.vlan_id, &ah_attr.sl);
14015ea8bbfcSJack Morgenstein 
140237bfc7c1SJack Morgenstein 	mlx4_ib_send_to_wire(dev, slave, ctx->port,
140337bfc7c1SJack Morgenstein 			     is_proxy_qp0(dev, wc->src_qp, slave) ?
140437bfc7c1SJack Morgenstein 			     IB_QPT_SMI : IB_QPT_GSI,
140537bfc7c1SJack Morgenstein 			     be16_to_cpu(tunnel->hdr.pkey_index),
140637bfc7c1SJack Morgenstein 			     be32_to_cpu(tunnel->hdr.remote_qpn),
140737bfc7c1SJack Morgenstein 			     be32_to_cpu(tunnel->hdr.qkey),
14085ea8bbfcSJack Morgenstein 			     &ah_attr, wc->smac, &tunnel->mad);
140937bfc7c1SJack Morgenstein }
141037bfc7c1SJack Morgenstein 
1411fc06573dSJack Morgenstein static int mlx4_ib_alloc_pv_bufs(struct mlx4_ib_demux_pv_ctx *ctx,
1412fc06573dSJack Morgenstein 				 enum ib_qp_type qp_type, int is_tun)
1413fc06573dSJack Morgenstein {
1414fc06573dSJack Morgenstein 	int i;
1415fc06573dSJack Morgenstein 	struct mlx4_ib_demux_pv_qp *tun_qp;
1416fc06573dSJack Morgenstein 	int rx_buf_size, tx_buf_size;
1417fc06573dSJack Morgenstein 
1418fc06573dSJack Morgenstein 	if (qp_type > IB_QPT_GSI)
1419fc06573dSJack Morgenstein 		return -EINVAL;
1420fc06573dSJack Morgenstein 
1421fc06573dSJack Morgenstein 	tun_qp = &ctx->qp[qp_type];
1422fc06573dSJack Morgenstein 
1423fc06573dSJack Morgenstein 	tun_qp->ring = kzalloc(sizeof (struct mlx4_ib_buf) * MLX4_NUM_TUNNEL_BUFS,
1424fc06573dSJack Morgenstein 			       GFP_KERNEL);
1425fc06573dSJack Morgenstein 	if (!tun_qp->ring)
1426fc06573dSJack Morgenstein 		return -ENOMEM;
1427fc06573dSJack Morgenstein 
1428fc06573dSJack Morgenstein 	tun_qp->tx_ring = kcalloc(MLX4_NUM_TUNNEL_BUFS,
1429fc06573dSJack Morgenstein 				  sizeof (struct mlx4_ib_tun_tx_buf),
1430fc06573dSJack Morgenstein 				  GFP_KERNEL);
1431fc06573dSJack Morgenstein 	if (!tun_qp->tx_ring) {
1432fc06573dSJack Morgenstein 		kfree(tun_qp->ring);
1433fc06573dSJack Morgenstein 		tun_qp->ring = NULL;
1434fc06573dSJack Morgenstein 		return -ENOMEM;
1435fc06573dSJack Morgenstein 	}
1436fc06573dSJack Morgenstein 
1437fc06573dSJack Morgenstein 	if (is_tun) {
1438fc06573dSJack Morgenstein 		rx_buf_size = sizeof (struct mlx4_tunnel_mad);
1439fc06573dSJack Morgenstein 		tx_buf_size = sizeof (struct mlx4_rcv_tunnel_mad);
1440fc06573dSJack Morgenstein 	} else {
1441fc06573dSJack Morgenstein 		rx_buf_size = sizeof (struct mlx4_mad_rcv_buf);
1442fc06573dSJack Morgenstein 		tx_buf_size = sizeof (struct mlx4_mad_snd_buf);
1443fc06573dSJack Morgenstein 	}
1444fc06573dSJack Morgenstein 
1445fc06573dSJack Morgenstein 	for (i = 0; i < MLX4_NUM_TUNNEL_BUFS; i++) {
1446fc06573dSJack Morgenstein 		tun_qp->ring[i].addr = kmalloc(rx_buf_size, GFP_KERNEL);
1447fc06573dSJack Morgenstein 		if (!tun_qp->ring[i].addr)
1448fc06573dSJack Morgenstein 			goto err;
1449fc06573dSJack Morgenstein 		tun_qp->ring[i].map = ib_dma_map_single(ctx->ib_dev,
1450fc06573dSJack Morgenstein 							tun_qp->ring[i].addr,
1451fc06573dSJack Morgenstein 							rx_buf_size,
1452fc06573dSJack Morgenstein 							DMA_FROM_DEVICE);
1453cc47d369SSebastian Ott 		if (ib_dma_mapping_error(ctx->ib_dev, tun_qp->ring[i].map)) {
1454cc47d369SSebastian Ott 			kfree(tun_qp->ring[i].addr);
1455cc47d369SSebastian Ott 			goto err;
1456cc47d369SSebastian Ott 		}
1457fc06573dSJack Morgenstein 	}
1458fc06573dSJack Morgenstein 
1459fc06573dSJack Morgenstein 	for (i = 0; i < MLX4_NUM_TUNNEL_BUFS; i++) {
1460fc06573dSJack Morgenstein 		tun_qp->tx_ring[i].buf.addr =
1461fc06573dSJack Morgenstein 			kmalloc(tx_buf_size, GFP_KERNEL);
1462fc06573dSJack Morgenstein 		if (!tun_qp->tx_ring[i].buf.addr)
1463fc06573dSJack Morgenstein 			goto tx_err;
1464fc06573dSJack Morgenstein 		tun_qp->tx_ring[i].buf.map =
1465fc06573dSJack Morgenstein 			ib_dma_map_single(ctx->ib_dev,
1466fc06573dSJack Morgenstein 					  tun_qp->tx_ring[i].buf.addr,
1467fc06573dSJack Morgenstein 					  tx_buf_size,
1468fc06573dSJack Morgenstein 					  DMA_TO_DEVICE);
1469cc47d369SSebastian Ott 		if (ib_dma_mapping_error(ctx->ib_dev,
1470cc47d369SSebastian Ott 					 tun_qp->tx_ring[i].buf.map)) {
1471cc47d369SSebastian Ott 			kfree(tun_qp->tx_ring[i].buf.addr);
1472cc47d369SSebastian Ott 			goto tx_err;
1473cc47d369SSebastian Ott 		}
1474fc06573dSJack Morgenstein 		tun_qp->tx_ring[i].ah = NULL;
1475fc06573dSJack Morgenstein 	}
1476fc06573dSJack Morgenstein 	spin_lock_init(&tun_qp->tx_lock);
1477fc06573dSJack Morgenstein 	tun_qp->tx_ix_head = 0;
1478fc06573dSJack Morgenstein 	tun_qp->tx_ix_tail = 0;
1479fc06573dSJack Morgenstein 	tun_qp->proxy_qpt = qp_type;
1480fc06573dSJack Morgenstein 
1481fc06573dSJack Morgenstein 	return 0;
1482fc06573dSJack Morgenstein 
1483fc06573dSJack Morgenstein tx_err:
1484fc06573dSJack Morgenstein 	while (i > 0) {
1485fc06573dSJack Morgenstein 		--i;
1486fc06573dSJack Morgenstein 		ib_dma_unmap_single(ctx->ib_dev, tun_qp->tx_ring[i].buf.map,
1487fc06573dSJack Morgenstein 				    tx_buf_size, DMA_TO_DEVICE);
1488fc06573dSJack Morgenstein 		kfree(tun_qp->tx_ring[i].buf.addr);
1489fc06573dSJack Morgenstein 	}
1490fc06573dSJack Morgenstein 	kfree(tun_qp->tx_ring);
1491fc06573dSJack Morgenstein 	tun_qp->tx_ring = NULL;
1492fc06573dSJack Morgenstein 	i = MLX4_NUM_TUNNEL_BUFS;
1493fc06573dSJack Morgenstein err:
1494fc06573dSJack Morgenstein 	while (i > 0) {
1495fc06573dSJack Morgenstein 		--i;
1496fc06573dSJack Morgenstein 		ib_dma_unmap_single(ctx->ib_dev, tun_qp->ring[i].map,
1497fc06573dSJack Morgenstein 				    rx_buf_size, DMA_FROM_DEVICE);
1498fc06573dSJack Morgenstein 		kfree(tun_qp->ring[i].addr);
1499fc06573dSJack Morgenstein 	}
1500fc06573dSJack Morgenstein 	kfree(tun_qp->ring);
1501fc06573dSJack Morgenstein 	tun_qp->ring = NULL;
1502fc06573dSJack Morgenstein 	return -ENOMEM;
1503fc06573dSJack Morgenstein }
1504fc06573dSJack Morgenstein 
1505fc06573dSJack Morgenstein static void mlx4_ib_free_pv_qp_bufs(struct mlx4_ib_demux_pv_ctx *ctx,
1506fc06573dSJack Morgenstein 				     enum ib_qp_type qp_type, int is_tun)
1507fc06573dSJack Morgenstein {
1508fc06573dSJack Morgenstein 	int i;
1509fc06573dSJack Morgenstein 	struct mlx4_ib_demux_pv_qp *tun_qp;
1510fc06573dSJack Morgenstein 	int rx_buf_size, tx_buf_size;
1511fc06573dSJack Morgenstein 
1512fc06573dSJack Morgenstein 	if (qp_type > IB_QPT_GSI)
1513fc06573dSJack Morgenstein 		return;
1514fc06573dSJack Morgenstein 
1515fc06573dSJack Morgenstein 	tun_qp = &ctx->qp[qp_type];
1516fc06573dSJack Morgenstein 	if (is_tun) {
1517fc06573dSJack Morgenstein 		rx_buf_size = sizeof (struct mlx4_tunnel_mad);
1518fc06573dSJack Morgenstein 		tx_buf_size = sizeof (struct mlx4_rcv_tunnel_mad);
1519fc06573dSJack Morgenstein 	} else {
1520fc06573dSJack Morgenstein 		rx_buf_size = sizeof (struct mlx4_mad_rcv_buf);
1521fc06573dSJack Morgenstein 		tx_buf_size = sizeof (struct mlx4_mad_snd_buf);
1522fc06573dSJack Morgenstein 	}
1523fc06573dSJack Morgenstein 
1524fc06573dSJack Morgenstein 
1525fc06573dSJack Morgenstein 	for (i = 0; i < MLX4_NUM_TUNNEL_BUFS; i++) {
1526fc06573dSJack Morgenstein 		ib_dma_unmap_single(ctx->ib_dev, tun_qp->ring[i].map,
1527fc06573dSJack Morgenstein 				    rx_buf_size, DMA_FROM_DEVICE);
1528fc06573dSJack Morgenstein 		kfree(tun_qp->ring[i].addr);
1529fc06573dSJack Morgenstein 	}
1530fc06573dSJack Morgenstein 
1531fc06573dSJack Morgenstein 	for (i = 0; i < MLX4_NUM_TUNNEL_BUFS; i++) {
1532fc06573dSJack Morgenstein 		ib_dma_unmap_single(ctx->ib_dev, tun_qp->tx_ring[i].buf.map,
1533fc06573dSJack Morgenstein 				    tx_buf_size, DMA_TO_DEVICE);
1534fc06573dSJack Morgenstein 		kfree(tun_qp->tx_ring[i].buf.addr);
1535fc06573dSJack Morgenstein 		if (tun_qp->tx_ring[i].ah)
1536fc06573dSJack Morgenstein 			ib_destroy_ah(tun_qp->tx_ring[i].ah);
1537fc06573dSJack Morgenstein 	}
1538fc06573dSJack Morgenstein 	kfree(tun_qp->tx_ring);
1539fc06573dSJack Morgenstein 	kfree(tun_qp->ring);
1540fc06573dSJack Morgenstein }
1541fc06573dSJack Morgenstein 
1542fc06573dSJack Morgenstein static void mlx4_ib_tunnel_comp_worker(struct work_struct *work)
1543fc06573dSJack Morgenstein {
154437bfc7c1SJack Morgenstein 	struct mlx4_ib_demux_pv_ctx *ctx;
154537bfc7c1SJack Morgenstein 	struct mlx4_ib_demux_pv_qp *tun_qp;
154637bfc7c1SJack Morgenstein 	struct ib_wc wc;
154737bfc7c1SJack Morgenstein 	int ret;
154837bfc7c1SJack Morgenstein 	ctx = container_of(work, struct mlx4_ib_demux_pv_ctx, work);
154937bfc7c1SJack Morgenstein 	ib_req_notify_cq(ctx->cq, IB_CQ_NEXT_COMP);
155037bfc7c1SJack Morgenstein 
155137bfc7c1SJack Morgenstein 	while (ib_poll_cq(ctx->cq, 1, &wc) == 1) {
155237bfc7c1SJack Morgenstein 		tun_qp = &ctx->qp[MLX4_TUN_WRID_QPN(wc.wr_id)];
155337bfc7c1SJack Morgenstein 		if (wc.status == IB_WC_SUCCESS) {
155437bfc7c1SJack Morgenstein 			switch (wc.opcode) {
155537bfc7c1SJack Morgenstein 			case IB_WC_RECV:
155637bfc7c1SJack Morgenstein 				mlx4_ib_multiplex_mad(ctx, &wc);
155737bfc7c1SJack Morgenstein 				ret = mlx4_ib_post_pv_qp_buf(ctx, tun_qp,
155837bfc7c1SJack Morgenstein 							     wc.wr_id &
155937bfc7c1SJack Morgenstein 							     (MLX4_NUM_TUNNEL_BUFS - 1));
156037bfc7c1SJack Morgenstein 				if (ret)
156137bfc7c1SJack Morgenstein 					pr_err("Failed reposting tunnel "
156237bfc7c1SJack Morgenstein 					       "buf:%lld\n", wc.wr_id);
156337bfc7c1SJack Morgenstein 				break;
156437bfc7c1SJack Morgenstein 			case IB_WC_SEND:
156537bfc7c1SJack Morgenstein 				pr_debug("received tunnel send completion:"
156637bfc7c1SJack Morgenstein 					 "wrid=0x%llx, status=0x%x\n",
156737bfc7c1SJack Morgenstein 					 wc.wr_id, wc.status);
156837bfc7c1SJack Morgenstein 				ib_destroy_ah(tun_qp->tx_ring[wc.wr_id &
156937bfc7c1SJack Morgenstein 					      (MLX4_NUM_TUNNEL_BUFS - 1)].ah);
157037bfc7c1SJack Morgenstein 				tun_qp->tx_ring[wc.wr_id & (MLX4_NUM_TUNNEL_BUFS - 1)].ah
157137bfc7c1SJack Morgenstein 					= NULL;
157237bfc7c1SJack Morgenstein 				spin_lock(&tun_qp->tx_lock);
157337bfc7c1SJack Morgenstein 				tun_qp->tx_ix_tail++;
157437bfc7c1SJack Morgenstein 				spin_unlock(&tun_qp->tx_lock);
157537bfc7c1SJack Morgenstein 
157637bfc7c1SJack Morgenstein 				break;
157737bfc7c1SJack Morgenstein 			default:
157837bfc7c1SJack Morgenstein 				break;
157937bfc7c1SJack Morgenstein 			}
158037bfc7c1SJack Morgenstein 		} else  {
158137bfc7c1SJack Morgenstein 			pr_debug("mlx4_ib: completion error in tunnel: %d."
158237bfc7c1SJack Morgenstein 				 " status = %d, wrid = 0x%llx\n",
158337bfc7c1SJack Morgenstein 				 ctx->slave, wc.status, wc.wr_id);
158437bfc7c1SJack Morgenstein 			if (!MLX4_TUN_IS_RECV(wc.wr_id)) {
158537bfc7c1SJack Morgenstein 				ib_destroy_ah(tun_qp->tx_ring[wc.wr_id &
158637bfc7c1SJack Morgenstein 					      (MLX4_NUM_TUNNEL_BUFS - 1)].ah);
158737bfc7c1SJack Morgenstein 				tun_qp->tx_ring[wc.wr_id & (MLX4_NUM_TUNNEL_BUFS - 1)].ah
158837bfc7c1SJack Morgenstein 					= NULL;
158937bfc7c1SJack Morgenstein 				spin_lock(&tun_qp->tx_lock);
159037bfc7c1SJack Morgenstein 				tun_qp->tx_ix_tail++;
159137bfc7c1SJack Morgenstein 				spin_unlock(&tun_qp->tx_lock);
159237bfc7c1SJack Morgenstein 			}
159337bfc7c1SJack Morgenstein 		}
159437bfc7c1SJack Morgenstein 	}
1595fc06573dSJack Morgenstein }
1596fc06573dSJack Morgenstein 
1597fc06573dSJack Morgenstein static void pv_qp_event_handler(struct ib_event *event, void *qp_context)
1598fc06573dSJack Morgenstein {
1599fc06573dSJack Morgenstein 	struct mlx4_ib_demux_pv_ctx *sqp = qp_context;
1600fc06573dSJack Morgenstein 
1601fc06573dSJack Morgenstein 	/* It's worse than that! He's dead, Jim! */
1602fc06573dSJack Morgenstein 	pr_err("Fatal error (%d) on a MAD QP on port %d\n",
1603fc06573dSJack Morgenstein 	       event->event, sqp->port);
1604fc06573dSJack Morgenstein }
1605fc06573dSJack Morgenstein 
1606fc06573dSJack Morgenstein static int create_pv_sqp(struct mlx4_ib_demux_pv_ctx *ctx,
1607fc06573dSJack Morgenstein 			    enum ib_qp_type qp_type, int create_tun)
1608fc06573dSJack Morgenstein {
1609fc06573dSJack Morgenstein 	int i, ret;
1610fc06573dSJack Morgenstein 	struct mlx4_ib_demux_pv_qp *tun_qp;
1611fc06573dSJack Morgenstein 	struct mlx4_ib_qp_tunnel_init_attr qp_init_attr;
1612fc06573dSJack Morgenstein 	struct ib_qp_attr attr;
1613fc06573dSJack Morgenstein 	int qp_attr_mask_INIT;
1614fc06573dSJack Morgenstein 
1615fc06573dSJack Morgenstein 	if (qp_type > IB_QPT_GSI)
1616fc06573dSJack Morgenstein 		return -EINVAL;
1617fc06573dSJack Morgenstein 
1618fc06573dSJack Morgenstein 	tun_qp = &ctx->qp[qp_type];
1619fc06573dSJack Morgenstein 
1620fc06573dSJack Morgenstein 	memset(&qp_init_attr, 0, sizeof qp_init_attr);
1621fc06573dSJack Morgenstein 	qp_init_attr.init_attr.send_cq = ctx->cq;
1622fc06573dSJack Morgenstein 	qp_init_attr.init_attr.recv_cq = ctx->cq;
1623fc06573dSJack Morgenstein 	qp_init_attr.init_attr.sq_sig_type = IB_SIGNAL_ALL_WR;
1624fc06573dSJack Morgenstein 	qp_init_attr.init_attr.cap.max_send_wr = MLX4_NUM_TUNNEL_BUFS;
1625fc06573dSJack Morgenstein 	qp_init_attr.init_attr.cap.max_recv_wr = MLX4_NUM_TUNNEL_BUFS;
1626fc06573dSJack Morgenstein 	qp_init_attr.init_attr.cap.max_send_sge = 1;
1627fc06573dSJack Morgenstein 	qp_init_attr.init_attr.cap.max_recv_sge = 1;
1628fc06573dSJack Morgenstein 	if (create_tun) {
1629fc06573dSJack Morgenstein 		qp_init_attr.init_attr.qp_type = IB_QPT_UD;
1630fc06573dSJack Morgenstein 		qp_init_attr.init_attr.create_flags = MLX4_IB_SRIOV_TUNNEL_QP;
1631fc06573dSJack Morgenstein 		qp_init_attr.port = ctx->port;
1632fc06573dSJack Morgenstein 		qp_init_attr.slave = ctx->slave;
1633fc06573dSJack Morgenstein 		qp_init_attr.proxy_qp_type = qp_type;
1634fc06573dSJack Morgenstein 		qp_attr_mask_INIT = IB_QP_STATE | IB_QP_PKEY_INDEX |
1635fc06573dSJack Morgenstein 			   IB_QP_QKEY | IB_QP_PORT;
1636fc06573dSJack Morgenstein 	} else {
1637fc06573dSJack Morgenstein 		qp_init_attr.init_attr.qp_type = qp_type;
1638fc06573dSJack Morgenstein 		qp_init_attr.init_attr.create_flags = MLX4_IB_SRIOV_SQP;
1639fc06573dSJack Morgenstein 		qp_attr_mask_INIT = IB_QP_STATE | IB_QP_PKEY_INDEX | IB_QP_QKEY;
1640fc06573dSJack Morgenstein 	}
1641fc06573dSJack Morgenstein 	qp_init_attr.init_attr.port_num = ctx->port;
1642fc06573dSJack Morgenstein 	qp_init_attr.init_attr.qp_context = ctx;
1643fc06573dSJack Morgenstein 	qp_init_attr.init_attr.event_handler = pv_qp_event_handler;
1644fc06573dSJack Morgenstein 	tun_qp->qp = ib_create_qp(ctx->pd, &qp_init_attr.init_attr);
1645fc06573dSJack Morgenstein 	if (IS_ERR(tun_qp->qp)) {
1646fc06573dSJack Morgenstein 		ret = PTR_ERR(tun_qp->qp);
1647fc06573dSJack Morgenstein 		tun_qp->qp = NULL;
1648fc06573dSJack Morgenstein 		pr_err("Couldn't create %s QP (%d)\n",
1649fc06573dSJack Morgenstein 		       create_tun ? "tunnel" : "special", ret);
1650fc06573dSJack Morgenstein 		return ret;
1651fc06573dSJack Morgenstein 	}
1652fc06573dSJack Morgenstein 
1653fc06573dSJack Morgenstein 	memset(&attr, 0, sizeof attr);
1654fc06573dSJack Morgenstein 	attr.qp_state = IB_QPS_INIT;
16553eac103fSJack Morgenstein 	ret = 0;
16563eac103fSJack Morgenstein 	if (create_tun)
16573eac103fSJack Morgenstein 		ret = find_slave_port_pkey_ix(to_mdev(ctx->ib_dev), ctx->slave,
16583eac103fSJack Morgenstein 					      ctx->port, IB_DEFAULT_PKEY_FULL,
16593eac103fSJack Morgenstein 					      &attr.pkey_index);
16603eac103fSJack Morgenstein 	if (ret || !create_tun)
1661fc06573dSJack Morgenstein 		attr.pkey_index =
1662fc06573dSJack Morgenstein 			to_mdev(ctx->ib_dev)->pkeys.virt2phys_pkey[ctx->slave][ctx->port - 1][0];
1663fc06573dSJack Morgenstein 	attr.qkey = IB_QP1_QKEY;
1664fc06573dSJack Morgenstein 	attr.port_num = ctx->port;
1665fc06573dSJack Morgenstein 	ret = ib_modify_qp(tun_qp->qp, &attr, qp_attr_mask_INIT);
1666fc06573dSJack Morgenstein 	if (ret) {
1667fc06573dSJack Morgenstein 		pr_err("Couldn't change %s qp state to INIT (%d)\n",
1668fc06573dSJack Morgenstein 		       create_tun ? "tunnel" : "special", ret);
1669fc06573dSJack Morgenstein 		goto err_qp;
1670fc06573dSJack Morgenstein 	}
1671fc06573dSJack Morgenstein 	attr.qp_state = IB_QPS_RTR;
1672fc06573dSJack Morgenstein 	ret = ib_modify_qp(tun_qp->qp, &attr, IB_QP_STATE);
1673fc06573dSJack Morgenstein 	if (ret) {
1674fc06573dSJack Morgenstein 		pr_err("Couldn't change %s qp state to RTR (%d)\n",
1675fc06573dSJack Morgenstein 		       create_tun ? "tunnel" : "special", ret);
1676fc06573dSJack Morgenstein 		goto err_qp;
1677fc06573dSJack Morgenstein 	}
1678fc06573dSJack Morgenstein 	attr.qp_state = IB_QPS_RTS;
1679fc06573dSJack Morgenstein 	attr.sq_psn = 0;
1680fc06573dSJack Morgenstein 	ret = ib_modify_qp(tun_qp->qp, &attr, IB_QP_STATE | IB_QP_SQ_PSN);
1681fc06573dSJack Morgenstein 	if (ret) {
1682fc06573dSJack Morgenstein 		pr_err("Couldn't change %s qp state to RTS (%d)\n",
1683fc06573dSJack Morgenstein 		       create_tun ? "tunnel" : "special", ret);
1684fc06573dSJack Morgenstein 		goto err_qp;
1685fc06573dSJack Morgenstein 	}
1686fc06573dSJack Morgenstein 
1687fc06573dSJack Morgenstein 	for (i = 0; i < MLX4_NUM_TUNNEL_BUFS; i++) {
1688fc06573dSJack Morgenstein 		ret = mlx4_ib_post_pv_qp_buf(ctx, tun_qp, i);
1689fc06573dSJack Morgenstein 		if (ret) {
1690fc06573dSJack Morgenstein 			pr_err(" mlx4_ib_post_pv_buf error"
1691fc06573dSJack Morgenstein 			       " (err = %d, i = %d)\n", ret, i);
1692fc06573dSJack Morgenstein 			goto err_qp;
1693fc06573dSJack Morgenstein 		}
1694fc06573dSJack Morgenstein 	}
1695fc06573dSJack Morgenstein 	return 0;
1696fc06573dSJack Morgenstein 
1697fc06573dSJack Morgenstein err_qp:
1698fc06573dSJack Morgenstein 	ib_destroy_qp(tun_qp->qp);
1699fc06573dSJack Morgenstein 	tun_qp->qp = NULL;
1700fc06573dSJack Morgenstein 	return ret;
1701fc06573dSJack Morgenstein }
1702fc06573dSJack Morgenstein 
1703fc06573dSJack Morgenstein /*
1704fc06573dSJack Morgenstein  * IB MAD completion callback for real SQPs
1705fc06573dSJack Morgenstein  */
1706fc06573dSJack Morgenstein static void mlx4_ib_sqp_comp_worker(struct work_struct *work)
1707fc06573dSJack Morgenstein {
170837bfc7c1SJack Morgenstein 	struct mlx4_ib_demux_pv_ctx *ctx;
170937bfc7c1SJack Morgenstein 	struct mlx4_ib_demux_pv_qp *sqp;
171037bfc7c1SJack Morgenstein 	struct ib_wc wc;
171137bfc7c1SJack Morgenstein 	struct ib_grh *grh;
171237bfc7c1SJack Morgenstein 	struct ib_mad *mad;
171337bfc7c1SJack Morgenstein 
171437bfc7c1SJack Morgenstein 	ctx = container_of(work, struct mlx4_ib_demux_pv_ctx, work);
171537bfc7c1SJack Morgenstein 	ib_req_notify_cq(ctx->cq, IB_CQ_NEXT_COMP);
171637bfc7c1SJack Morgenstein 
171737bfc7c1SJack Morgenstein 	while (mlx4_ib_poll_cq(ctx->cq, 1, &wc) == 1) {
171837bfc7c1SJack Morgenstein 		sqp = &ctx->qp[MLX4_TUN_WRID_QPN(wc.wr_id)];
171937bfc7c1SJack Morgenstein 		if (wc.status == IB_WC_SUCCESS) {
172037bfc7c1SJack Morgenstein 			switch (wc.opcode) {
172137bfc7c1SJack Morgenstein 			case IB_WC_SEND:
172237bfc7c1SJack Morgenstein 				ib_destroy_ah(sqp->tx_ring[wc.wr_id &
172337bfc7c1SJack Morgenstein 					      (MLX4_NUM_TUNNEL_BUFS - 1)].ah);
172437bfc7c1SJack Morgenstein 				sqp->tx_ring[wc.wr_id & (MLX4_NUM_TUNNEL_BUFS - 1)].ah
172537bfc7c1SJack Morgenstein 					= NULL;
172637bfc7c1SJack Morgenstein 				spin_lock(&sqp->tx_lock);
172737bfc7c1SJack Morgenstein 				sqp->tx_ix_tail++;
172837bfc7c1SJack Morgenstein 				spin_unlock(&sqp->tx_lock);
172937bfc7c1SJack Morgenstein 				break;
173037bfc7c1SJack Morgenstein 			case IB_WC_RECV:
173137bfc7c1SJack Morgenstein 				mad = (struct ib_mad *) &(((struct mlx4_mad_rcv_buf *)
173237bfc7c1SJack Morgenstein 						(sqp->ring[wc.wr_id &
173337bfc7c1SJack Morgenstein 						(MLX4_NUM_TUNNEL_BUFS - 1)].addr))->payload);
173437bfc7c1SJack Morgenstein 				grh = &(((struct mlx4_mad_rcv_buf *)
173537bfc7c1SJack Morgenstein 						(sqp->ring[wc.wr_id &
173637bfc7c1SJack Morgenstein 						(MLX4_NUM_TUNNEL_BUFS - 1)].addr))->grh);
173737bfc7c1SJack Morgenstein 				mlx4_ib_demux_mad(ctx->ib_dev, ctx->port, &wc, grh, mad);
173837bfc7c1SJack Morgenstein 				if (mlx4_ib_post_pv_qp_buf(ctx, sqp, wc.wr_id &
173937bfc7c1SJack Morgenstein 							   (MLX4_NUM_TUNNEL_BUFS - 1)))
174037bfc7c1SJack Morgenstein 					pr_err("Failed reposting SQP "
174137bfc7c1SJack Morgenstein 					       "buf:%lld\n", wc.wr_id);
174237bfc7c1SJack Morgenstein 				break;
174337bfc7c1SJack Morgenstein 			default:
174437bfc7c1SJack Morgenstein 				BUG_ON(1);
174537bfc7c1SJack Morgenstein 				break;
174637bfc7c1SJack Morgenstein 			}
174737bfc7c1SJack Morgenstein 		} else  {
174837bfc7c1SJack Morgenstein 			pr_debug("mlx4_ib: completion error in tunnel: %d."
174937bfc7c1SJack Morgenstein 				 " status = %d, wrid = 0x%llx\n",
175037bfc7c1SJack Morgenstein 				 ctx->slave, wc.status, wc.wr_id);
175137bfc7c1SJack Morgenstein 			if (!MLX4_TUN_IS_RECV(wc.wr_id)) {
175237bfc7c1SJack Morgenstein 				ib_destroy_ah(sqp->tx_ring[wc.wr_id &
175337bfc7c1SJack Morgenstein 					      (MLX4_NUM_TUNNEL_BUFS - 1)].ah);
175437bfc7c1SJack Morgenstein 				sqp->tx_ring[wc.wr_id & (MLX4_NUM_TUNNEL_BUFS - 1)].ah
175537bfc7c1SJack Morgenstein 					= NULL;
175637bfc7c1SJack Morgenstein 				spin_lock(&sqp->tx_lock);
175737bfc7c1SJack Morgenstein 				sqp->tx_ix_tail++;
175837bfc7c1SJack Morgenstein 				spin_unlock(&sqp->tx_lock);
175937bfc7c1SJack Morgenstein 			}
176037bfc7c1SJack Morgenstein 		}
176137bfc7c1SJack Morgenstein 	}
1762fc06573dSJack Morgenstein }
1763fc06573dSJack Morgenstein 
1764fc06573dSJack Morgenstein static int alloc_pv_object(struct mlx4_ib_dev *dev, int slave, int port,
1765fc06573dSJack Morgenstein 			       struct mlx4_ib_demux_pv_ctx **ret_ctx)
1766fc06573dSJack Morgenstein {
1767fc06573dSJack Morgenstein 	struct mlx4_ib_demux_pv_ctx *ctx;
1768fc06573dSJack Morgenstein 
1769fc06573dSJack Morgenstein 	*ret_ctx = NULL;
1770fc06573dSJack Morgenstein 	ctx = kzalloc(sizeof (struct mlx4_ib_demux_pv_ctx), GFP_KERNEL);
1771fc06573dSJack Morgenstein 	if (!ctx) {
1772fc06573dSJack Morgenstein 		pr_err("failed allocating pv resource context "
1773fc06573dSJack Morgenstein 		       "for port %d, slave %d\n", port, slave);
1774fc06573dSJack Morgenstein 		return -ENOMEM;
1775fc06573dSJack Morgenstein 	}
1776fc06573dSJack Morgenstein 
1777fc06573dSJack Morgenstein 	ctx->ib_dev = &dev->ib_dev;
1778fc06573dSJack Morgenstein 	ctx->port = port;
1779fc06573dSJack Morgenstein 	ctx->slave = slave;
1780fc06573dSJack Morgenstein 	*ret_ctx = ctx;
1781fc06573dSJack Morgenstein 	return 0;
1782fc06573dSJack Morgenstein }
1783fc06573dSJack Morgenstein 
1784fc06573dSJack Morgenstein static void free_pv_object(struct mlx4_ib_dev *dev, int slave, int port)
1785fc06573dSJack Morgenstein {
1786fc06573dSJack Morgenstein 	if (dev->sriov.demux[port - 1].tun[slave]) {
1787fc06573dSJack Morgenstein 		kfree(dev->sriov.demux[port - 1].tun[slave]);
1788fc06573dSJack Morgenstein 		dev->sriov.demux[port - 1].tun[slave] = NULL;
1789fc06573dSJack Morgenstein 	}
1790fc06573dSJack Morgenstein }
1791fc06573dSJack Morgenstein 
1792fc06573dSJack Morgenstein static int create_pv_resources(struct ib_device *ibdev, int slave, int port,
1793fc06573dSJack Morgenstein 			       int create_tun, struct mlx4_ib_demux_pv_ctx *ctx)
1794fc06573dSJack Morgenstein {
1795fc06573dSJack Morgenstein 	int ret, cq_size;
17968e37210bSMatan Barak 	struct ib_cq_init_attr cq_attr = {};
1797fc06573dSJack Morgenstein 
17983806d08cSJack Morgenstein 	if (ctx->state != DEMUX_PV_STATE_DOWN)
17993806d08cSJack Morgenstein 		return -EEXIST;
18003806d08cSJack Morgenstein 
1801fc06573dSJack Morgenstein 	ctx->state = DEMUX_PV_STATE_STARTING;
180297982f5aSJack Morgenstein 	/* have QP0 only if link layer is IB */
180397982f5aSJack Morgenstein 	if (rdma_port_get_link_layer(ibdev, ctx->port) ==
180497982f5aSJack Morgenstein 	    IB_LINK_LAYER_INFINIBAND)
1805fc06573dSJack Morgenstein 		ctx->has_smi = 1;
1806fc06573dSJack Morgenstein 
1807fc06573dSJack Morgenstein 	if (ctx->has_smi) {
1808fc06573dSJack Morgenstein 		ret = mlx4_ib_alloc_pv_bufs(ctx, IB_QPT_SMI, create_tun);
1809fc06573dSJack Morgenstein 		if (ret) {
1810fc06573dSJack Morgenstein 			pr_err("Failed allocating qp0 tunnel bufs (%d)\n", ret);
1811fc06573dSJack Morgenstein 			goto err_out;
1812fc06573dSJack Morgenstein 		}
1813fc06573dSJack Morgenstein 	}
1814fc06573dSJack Morgenstein 
1815fc06573dSJack Morgenstein 	ret = mlx4_ib_alloc_pv_bufs(ctx, IB_QPT_GSI, create_tun);
1816fc06573dSJack Morgenstein 	if (ret) {
1817fc06573dSJack Morgenstein 		pr_err("Failed allocating qp1 tunnel bufs (%d)\n", ret);
1818fc06573dSJack Morgenstein 		goto err_out_qp0;
1819fc06573dSJack Morgenstein 	}
1820fc06573dSJack Morgenstein 
1821fc06573dSJack Morgenstein 	cq_size = 2 * MLX4_NUM_TUNNEL_BUFS;
1822fc06573dSJack Morgenstein 	if (ctx->has_smi)
1823fc06573dSJack Morgenstein 		cq_size *= 2;
1824fc06573dSJack Morgenstein 
18258e37210bSMatan Barak 	cq_attr.cqe = cq_size;
1826fc06573dSJack Morgenstein 	ctx->cq = ib_create_cq(ctx->ib_dev, mlx4_ib_tunnel_comp_handler,
18278e37210bSMatan Barak 			       NULL, ctx, &cq_attr);
1828fc06573dSJack Morgenstein 	if (IS_ERR(ctx->cq)) {
1829fc06573dSJack Morgenstein 		ret = PTR_ERR(ctx->cq);
1830fc06573dSJack Morgenstein 		pr_err("Couldn't create tunnel CQ (%d)\n", ret);
1831fc06573dSJack Morgenstein 		goto err_buf;
1832fc06573dSJack Morgenstein 	}
1833fc06573dSJack Morgenstein 
1834fc06573dSJack Morgenstein 	ctx->pd = ib_alloc_pd(ctx->ib_dev);
1835fc06573dSJack Morgenstein 	if (IS_ERR(ctx->pd)) {
1836fc06573dSJack Morgenstein 		ret = PTR_ERR(ctx->pd);
1837fc06573dSJack Morgenstein 		pr_err("Couldn't create tunnel PD (%d)\n", ret);
1838fc06573dSJack Morgenstein 		goto err_cq;
1839fc06573dSJack Morgenstein 	}
1840fc06573dSJack Morgenstein 
1841fc06573dSJack Morgenstein 	if (ctx->has_smi) {
1842fc06573dSJack Morgenstein 		ret = create_pv_sqp(ctx, IB_QPT_SMI, create_tun);
1843fc06573dSJack Morgenstein 		if (ret) {
1844fc06573dSJack Morgenstein 			pr_err("Couldn't create %s QP0 (%d)\n",
1845fc06573dSJack Morgenstein 			       create_tun ? "tunnel for" : "",  ret);
18467dd97576SJason Gunthorpe 			goto err_pd;
1847fc06573dSJack Morgenstein 		}
1848fc06573dSJack Morgenstein 	}
1849fc06573dSJack Morgenstein 
1850fc06573dSJack Morgenstein 	ret = create_pv_sqp(ctx, IB_QPT_GSI, create_tun);
1851fc06573dSJack Morgenstein 	if (ret) {
1852fc06573dSJack Morgenstein 		pr_err("Couldn't create %s QP1 (%d)\n",
1853fc06573dSJack Morgenstein 		       create_tun ? "tunnel for" : "",  ret);
1854fc06573dSJack Morgenstein 		goto err_qp0;
1855fc06573dSJack Morgenstein 	}
1856fc06573dSJack Morgenstein 
1857fc06573dSJack Morgenstein 	if (create_tun)
1858fc06573dSJack Morgenstein 		INIT_WORK(&ctx->work, mlx4_ib_tunnel_comp_worker);
1859fc06573dSJack Morgenstein 	else
1860fc06573dSJack Morgenstein 		INIT_WORK(&ctx->work, mlx4_ib_sqp_comp_worker);
1861fc06573dSJack Morgenstein 
1862fc06573dSJack Morgenstein 	ctx->wq = to_mdev(ibdev)->sriov.demux[port - 1].wq;
1863fc06573dSJack Morgenstein 
1864fc06573dSJack Morgenstein 	ret = ib_req_notify_cq(ctx->cq, IB_CQ_NEXT_COMP);
1865fc06573dSJack Morgenstein 	if (ret) {
1866fc06573dSJack Morgenstein 		pr_err("Couldn't arm tunnel cq (%d)\n", ret);
1867fc06573dSJack Morgenstein 		goto err_wq;
1868fc06573dSJack Morgenstein 	}
1869fc06573dSJack Morgenstein 	ctx->state = DEMUX_PV_STATE_ACTIVE;
1870fc06573dSJack Morgenstein 	return 0;
1871fc06573dSJack Morgenstein 
1872fc06573dSJack Morgenstein err_wq:
1873fc06573dSJack Morgenstein 	ctx->wq = NULL;
1874fc06573dSJack Morgenstein 	ib_destroy_qp(ctx->qp[1].qp);
1875fc06573dSJack Morgenstein 	ctx->qp[1].qp = NULL;
1876fc06573dSJack Morgenstein 
1877fc06573dSJack Morgenstein 
1878fc06573dSJack Morgenstein err_qp0:
1879fc06573dSJack Morgenstein 	if (ctx->has_smi)
1880fc06573dSJack Morgenstein 		ib_destroy_qp(ctx->qp[0].qp);
1881fc06573dSJack Morgenstein 	ctx->qp[0].qp = NULL;
1882fc06573dSJack Morgenstein 
1883fc06573dSJack Morgenstein err_pd:
1884fc06573dSJack Morgenstein 	ib_dealloc_pd(ctx->pd);
1885fc06573dSJack Morgenstein 	ctx->pd = NULL;
1886fc06573dSJack Morgenstein 
1887fc06573dSJack Morgenstein err_cq:
1888fc06573dSJack Morgenstein 	ib_destroy_cq(ctx->cq);
1889fc06573dSJack Morgenstein 	ctx->cq = NULL;
1890fc06573dSJack Morgenstein 
1891fc06573dSJack Morgenstein err_buf:
1892fc06573dSJack Morgenstein 	mlx4_ib_free_pv_qp_bufs(ctx, IB_QPT_GSI, create_tun);
1893fc06573dSJack Morgenstein 
1894fc06573dSJack Morgenstein err_out_qp0:
1895fc06573dSJack Morgenstein 	if (ctx->has_smi)
1896fc06573dSJack Morgenstein 		mlx4_ib_free_pv_qp_bufs(ctx, IB_QPT_SMI, create_tun);
1897fc06573dSJack Morgenstein err_out:
1898fc06573dSJack Morgenstein 	ctx->state = DEMUX_PV_STATE_DOWN;
1899fc06573dSJack Morgenstein 	return ret;
1900fc06573dSJack Morgenstein }
1901fc06573dSJack Morgenstein 
1902fc06573dSJack Morgenstein static void destroy_pv_resources(struct mlx4_ib_dev *dev, int slave, int port,
1903fc06573dSJack Morgenstein 				 struct mlx4_ib_demux_pv_ctx *ctx, int flush)
1904fc06573dSJack Morgenstein {
1905fc06573dSJack Morgenstein 	if (!ctx)
1906fc06573dSJack Morgenstein 		return;
1907fc06573dSJack Morgenstein 	if (ctx->state > DEMUX_PV_STATE_DOWN) {
1908fc06573dSJack Morgenstein 		ctx->state = DEMUX_PV_STATE_DOWNING;
1909fc06573dSJack Morgenstein 		if (flush)
1910fc06573dSJack Morgenstein 			flush_workqueue(ctx->wq);
1911fc06573dSJack Morgenstein 		if (ctx->has_smi) {
1912fc06573dSJack Morgenstein 			ib_destroy_qp(ctx->qp[0].qp);
1913fc06573dSJack Morgenstein 			ctx->qp[0].qp = NULL;
1914fc06573dSJack Morgenstein 			mlx4_ib_free_pv_qp_bufs(ctx, IB_QPT_SMI, 1);
1915fc06573dSJack Morgenstein 		}
1916fc06573dSJack Morgenstein 		ib_destroy_qp(ctx->qp[1].qp);
1917fc06573dSJack Morgenstein 		ctx->qp[1].qp = NULL;
1918fc06573dSJack Morgenstein 		mlx4_ib_free_pv_qp_bufs(ctx, IB_QPT_GSI, 1);
1919fc06573dSJack Morgenstein 		ib_dealloc_pd(ctx->pd);
1920fc06573dSJack Morgenstein 		ctx->pd = NULL;
1921fc06573dSJack Morgenstein 		ib_destroy_cq(ctx->cq);
1922fc06573dSJack Morgenstein 		ctx->cq = NULL;
1923fc06573dSJack Morgenstein 		ctx->state = DEMUX_PV_STATE_DOWN;
1924fc06573dSJack Morgenstein 	}
1925fc06573dSJack Morgenstein }
1926fc06573dSJack Morgenstein 
1927fc06573dSJack Morgenstein static int mlx4_ib_tunnels_update(struct mlx4_ib_dev *dev, int slave,
1928fc06573dSJack Morgenstein 				  int port, int do_init)
1929fc06573dSJack Morgenstein {
1930fc06573dSJack Morgenstein 	int ret = 0;
1931fc06573dSJack Morgenstein 
1932fc06573dSJack Morgenstein 	if (!do_init) {
1933b9c5d6a6SOren Duer 		clean_vf_mcast(&dev->sriov.demux[port - 1], slave);
1934fc06573dSJack Morgenstein 		/* for master, destroy real sqp resources */
1935fc06573dSJack Morgenstein 		if (slave == mlx4_master_func_num(dev->dev))
1936fc06573dSJack Morgenstein 			destroy_pv_resources(dev, slave, port,
1937fc06573dSJack Morgenstein 					     dev->sriov.sqps[port - 1], 1);
1938fc06573dSJack Morgenstein 		/* destroy the tunnel qp resources */
1939fc06573dSJack Morgenstein 		destroy_pv_resources(dev, slave, port,
1940fc06573dSJack Morgenstein 				     dev->sriov.demux[port - 1].tun[slave], 1);
1941fc06573dSJack Morgenstein 		return 0;
1942fc06573dSJack Morgenstein 	}
1943fc06573dSJack Morgenstein 
1944fc06573dSJack Morgenstein 	/* create the tunnel qp resources */
1945fc06573dSJack Morgenstein 	ret = create_pv_resources(&dev->ib_dev, slave, port, 1,
1946fc06573dSJack Morgenstein 				  dev->sriov.demux[port - 1].tun[slave]);
1947fc06573dSJack Morgenstein 
1948fc06573dSJack Morgenstein 	/* for master, create the real sqp resources */
1949fc06573dSJack Morgenstein 	if (!ret && slave == mlx4_master_func_num(dev->dev))
1950fc06573dSJack Morgenstein 		ret = create_pv_resources(&dev->ib_dev, slave, port, 0,
1951fc06573dSJack Morgenstein 					  dev->sriov.sqps[port - 1]);
1952fc06573dSJack Morgenstein 	return ret;
1953fc06573dSJack Morgenstein }
1954fc06573dSJack Morgenstein 
1955fc06573dSJack Morgenstein void mlx4_ib_tunnels_update_work(struct work_struct *work)
1956fc06573dSJack Morgenstein {
1957fc06573dSJack Morgenstein 	struct mlx4_ib_demux_work *dmxw;
1958fc06573dSJack Morgenstein 
1959fc06573dSJack Morgenstein 	dmxw = container_of(work, struct mlx4_ib_demux_work, work);
1960fc06573dSJack Morgenstein 	mlx4_ib_tunnels_update(dmxw->dev, dmxw->slave, (int) dmxw->port,
1961fc06573dSJack Morgenstein 			       dmxw->do_init);
1962fc06573dSJack Morgenstein 	kfree(dmxw);
1963fc06573dSJack Morgenstein 	return;
1964fc06573dSJack Morgenstein }
1965fc06573dSJack Morgenstein 
1966fc06573dSJack Morgenstein static int mlx4_ib_alloc_demux_ctx(struct mlx4_ib_dev *dev,
1967fc06573dSJack Morgenstein 				       struct mlx4_ib_demux_ctx *ctx,
1968fc06573dSJack Morgenstein 				       int port)
1969fc06573dSJack Morgenstein {
1970fc06573dSJack Morgenstein 	char name[12];
1971fc06573dSJack Morgenstein 	int ret = 0;
1972fc06573dSJack Morgenstein 	int i;
1973fc06573dSJack Morgenstein 
1974fc06573dSJack Morgenstein 	ctx->tun = kcalloc(dev->dev->caps.sqp_demux,
1975fc06573dSJack Morgenstein 			   sizeof (struct mlx4_ib_demux_pv_ctx *), GFP_KERNEL);
1976fc06573dSJack Morgenstein 	if (!ctx->tun)
1977fc06573dSJack Morgenstein 		return -ENOMEM;
1978fc06573dSJack Morgenstein 
1979fc06573dSJack Morgenstein 	ctx->dev = dev;
1980fc06573dSJack Morgenstein 	ctx->port = port;
1981fc06573dSJack Morgenstein 	ctx->ib_dev = &dev->ib_dev;
1982fc06573dSJack Morgenstein 
1983449fc488SMatan Barak 	for (i = 0;
1984872bf2fbSYishai Hadas 	     i < min(dev->dev->caps.sqp_demux,
1985872bf2fbSYishai Hadas 	     (u16)(dev->dev->persist->num_vfs + 1));
1986449fc488SMatan Barak 	     i++) {
1987449fc488SMatan Barak 		struct mlx4_active_ports actv_ports =
1988449fc488SMatan Barak 			mlx4_get_active_ports(dev->dev, i);
1989449fc488SMatan Barak 
1990449fc488SMatan Barak 		if (!test_bit(port - 1, actv_ports.ports))
1991449fc488SMatan Barak 			continue;
1992449fc488SMatan Barak 
1993fc06573dSJack Morgenstein 		ret = alloc_pv_object(dev, i, port, &ctx->tun[i]);
1994fc06573dSJack Morgenstein 		if (ret) {
1995fc06573dSJack Morgenstein 			ret = -ENOMEM;
1996b9c5d6a6SOren Duer 			goto err_mcg;
1997fc06573dSJack Morgenstein 		}
1998fc06573dSJack Morgenstein 	}
1999fc06573dSJack Morgenstein 
2000b9c5d6a6SOren Duer 	ret = mlx4_ib_mcg_port_init(ctx);
2001b9c5d6a6SOren Duer 	if (ret) {
2002b9c5d6a6SOren Duer 		pr_err("Failed initializing mcg para-virt (%d)\n", ret);
2003b9c5d6a6SOren Duer 		goto err_mcg;
2004b9c5d6a6SOren Duer 	}
2005b9c5d6a6SOren Duer 
2006fc06573dSJack Morgenstein 	snprintf(name, sizeof name, "mlx4_ibt%d", port);
2007fc06573dSJack Morgenstein 	ctx->wq = create_singlethread_workqueue(name);
2008fc06573dSJack Morgenstein 	if (!ctx->wq) {
2009fc06573dSJack Morgenstein 		pr_err("Failed to create tunnelling WQ for port %d\n", port);
2010fc06573dSJack Morgenstein 		ret = -ENOMEM;
2011fc06573dSJack Morgenstein 		goto err_wq;
2012fc06573dSJack Morgenstein 	}
2013fc06573dSJack Morgenstein 
2014fc06573dSJack Morgenstein 	snprintf(name, sizeof name, "mlx4_ibud%d", port);
2015fc06573dSJack Morgenstein 	ctx->ud_wq = create_singlethread_workqueue(name);
2016fc06573dSJack Morgenstein 	if (!ctx->ud_wq) {
2017fc06573dSJack Morgenstein 		pr_err("Failed to create up/down WQ for port %d\n", port);
2018fc06573dSJack Morgenstein 		ret = -ENOMEM;
2019fc06573dSJack Morgenstein 		goto err_udwq;
2020fc06573dSJack Morgenstein 	}
2021fc06573dSJack Morgenstein 
2022fc06573dSJack Morgenstein 	return 0;
2023fc06573dSJack Morgenstein 
2024fc06573dSJack Morgenstein err_udwq:
2025fc06573dSJack Morgenstein 	destroy_workqueue(ctx->wq);
2026fc06573dSJack Morgenstein 	ctx->wq = NULL;
2027fc06573dSJack Morgenstein 
2028fc06573dSJack Morgenstein err_wq:
2029b9c5d6a6SOren Duer 	mlx4_ib_mcg_port_cleanup(ctx, 1);
2030b9c5d6a6SOren Duer err_mcg:
2031fc06573dSJack Morgenstein 	for (i = 0; i < dev->dev->caps.sqp_demux; i++)
2032fc06573dSJack Morgenstein 		free_pv_object(dev, i, port);
2033fc06573dSJack Morgenstein 	kfree(ctx->tun);
2034fc06573dSJack Morgenstein 	ctx->tun = NULL;
2035fc06573dSJack Morgenstein 	return ret;
2036fc06573dSJack Morgenstein }
2037fc06573dSJack Morgenstein 
2038fc06573dSJack Morgenstein static void mlx4_ib_free_sqp_ctx(struct mlx4_ib_demux_pv_ctx *sqp_ctx)
2039fc06573dSJack Morgenstein {
2040fc06573dSJack Morgenstein 	if (sqp_ctx->state > DEMUX_PV_STATE_DOWN) {
2041fc06573dSJack Morgenstein 		sqp_ctx->state = DEMUX_PV_STATE_DOWNING;
2042fc06573dSJack Morgenstein 		flush_workqueue(sqp_ctx->wq);
2043fc06573dSJack Morgenstein 		if (sqp_ctx->has_smi) {
2044fc06573dSJack Morgenstein 			ib_destroy_qp(sqp_ctx->qp[0].qp);
2045fc06573dSJack Morgenstein 			sqp_ctx->qp[0].qp = NULL;
2046fc06573dSJack Morgenstein 			mlx4_ib_free_pv_qp_bufs(sqp_ctx, IB_QPT_SMI, 0);
2047fc06573dSJack Morgenstein 		}
2048fc06573dSJack Morgenstein 		ib_destroy_qp(sqp_ctx->qp[1].qp);
2049fc06573dSJack Morgenstein 		sqp_ctx->qp[1].qp = NULL;
2050fc06573dSJack Morgenstein 		mlx4_ib_free_pv_qp_bufs(sqp_ctx, IB_QPT_GSI, 0);
2051fc06573dSJack Morgenstein 		ib_dealloc_pd(sqp_ctx->pd);
2052fc06573dSJack Morgenstein 		sqp_ctx->pd = NULL;
2053fc06573dSJack Morgenstein 		ib_destroy_cq(sqp_ctx->cq);
2054fc06573dSJack Morgenstein 		sqp_ctx->cq = NULL;
2055fc06573dSJack Morgenstein 		sqp_ctx->state = DEMUX_PV_STATE_DOWN;
2056fc06573dSJack Morgenstein 	}
2057fc06573dSJack Morgenstein }
2058fc06573dSJack Morgenstein 
2059fc06573dSJack Morgenstein static void mlx4_ib_free_demux_ctx(struct mlx4_ib_demux_ctx *ctx)
2060fc06573dSJack Morgenstein {
2061fc06573dSJack Morgenstein 	int i;
2062fc06573dSJack Morgenstein 	if (ctx) {
2063fc06573dSJack Morgenstein 		struct mlx4_ib_dev *dev = to_mdev(ctx->ib_dev);
2064b9c5d6a6SOren Duer 		mlx4_ib_mcg_port_cleanup(ctx, 1);
2065fc06573dSJack Morgenstein 		for (i = 0; i < dev->dev->caps.sqp_demux; i++) {
2066fc06573dSJack Morgenstein 			if (!ctx->tun[i])
2067fc06573dSJack Morgenstein 				continue;
2068fc06573dSJack Morgenstein 			if (ctx->tun[i]->state > DEMUX_PV_STATE_DOWN)
2069fc06573dSJack Morgenstein 				ctx->tun[i]->state = DEMUX_PV_STATE_DOWNING;
2070fc06573dSJack Morgenstein 		}
2071fc06573dSJack Morgenstein 		flush_workqueue(ctx->wq);
2072fc06573dSJack Morgenstein 		for (i = 0; i < dev->dev->caps.sqp_demux; i++) {
2073fc06573dSJack Morgenstein 			destroy_pv_resources(dev, i, ctx->port, ctx->tun[i], 0);
2074fc06573dSJack Morgenstein 			free_pv_object(dev, i, ctx->port);
2075fc06573dSJack Morgenstein 		}
2076fc06573dSJack Morgenstein 		kfree(ctx->tun);
2077fc06573dSJack Morgenstein 		destroy_workqueue(ctx->ud_wq);
2078fc06573dSJack Morgenstein 		destroy_workqueue(ctx->wq);
2079fc06573dSJack Morgenstein 	}
2080fc06573dSJack Morgenstein }
2081fc06573dSJack Morgenstein 
2082fc06573dSJack Morgenstein static void mlx4_ib_master_tunnels(struct mlx4_ib_dev *dev, int do_init)
2083fc06573dSJack Morgenstein {
2084fc06573dSJack Morgenstein 	int i;
2085fc06573dSJack Morgenstein 
2086fc06573dSJack Morgenstein 	if (!mlx4_is_master(dev->dev))
2087fc06573dSJack Morgenstein 		return;
2088fc06573dSJack Morgenstein 	/* initialize or tear down tunnel QPs for the master */
2089fc06573dSJack Morgenstein 	for (i = 0; i < dev->dev->caps.num_ports; i++)
2090fc06573dSJack Morgenstein 		mlx4_ib_tunnels_update(dev, mlx4_master_func_num(dev->dev), i + 1, do_init);
2091fc06573dSJack Morgenstein 	return;
2092fc06573dSJack Morgenstein }
2093fc06573dSJack Morgenstein 
2094fc06573dSJack Morgenstein int mlx4_ib_init_sriov(struct mlx4_ib_dev *dev)
2095fc06573dSJack Morgenstein {
2096fc06573dSJack Morgenstein 	int i = 0;
2097fc06573dSJack Morgenstein 	int err;
2098fc06573dSJack Morgenstein 
2099fc06573dSJack Morgenstein 	if (!mlx4_is_mfunc(dev->dev))
2100fc06573dSJack Morgenstein 		return 0;
2101fc06573dSJack Morgenstein 
2102fc06573dSJack Morgenstein 	dev->sriov.is_going_down = 0;
2103fc06573dSJack Morgenstein 	spin_lock_init(&dev->sriov.going_down_lock);
21043cf69cc8SAmir Vadai 	mlx4_ib_cm_paravirt_init(dev);
2105fc06573dSJack Morgenstein 
2106fc06573dSJack Morgenstein 	mlx4_ib_warn(&dev->ib_dev, "multi-function enabled\n");
2107fc06573dSJack Morgenstein 
2108fc06573dSJack Morgenstein 	if (mlx4_is_slave(dev->dev)) {
2109fc06573dSJack Morgenstein 		mlx4_ib_warn(&dev->ib_dev, "operating in qp1 tunnel mode\n");
2110fc06573dSJack Morgenstein 		return 0;
2111fc06573dSJack Morgenstein 	}
2112fc06573dSJack Morgenstein 
2113afa8fd1dSJack Morgenstein 	for (i = 0; i < dev->dev->caps.sqp_demux; i++) {
2114afa8fd1dSJack Morgenstein 		if (i == mlx4_master_func_num(dev->dev))
2115afa8fd1dSJack Morgenstein 			mlx4_put_slave_node_guid(dev->dev, i, dev->ib_dev.node_guid);
2116afa8fd1dSJack Morgenstein 		else
2117afa8fd1dSJack Morgenstein 			mlx4_put_slave_node_guid(dev->dev, i, mlx4_ib_gen_node_guid());
2118afa8fd1dSJack Morgenstein 	}
2119afa8fd1dSJack Morgenstein 
2120a0c64a17SJack Morgenstein 	err = mlx4_ib_init_alias_guid_service(dev);
2121a0c64a17SJack Morgenstein 	if (err) {
2122a0c64a17SJack Morgenstein 		mlx4_ib_warn(&dev->ib_dev, "Failed init alias guid process.\n");
2123a0c64a17SJack Morgenstein 		goto paravirt_err;
2124a0c64a17SJack Morgenstein 	}
2125c1e7e466SJack Morgenstein 	err = mlx4_ib_device_register_sysfs(dev);
2126c1e7e466SJack Morgenstein 	if (err) {
2127c1e7e466SJack Morgenstein 		mlx4_ib_warn(&dev->ib_dev, "Failed to register sysfs\n");
2128c1e7e466SJack Morgenstein 		goto sysfs_err;
2129c1e7e466SJack Morgenstein 	}
2130a0c64a17SJack Morgenstein 
2131fc06573dSJack Morgenstein 	mlx4_ib_warn(&dev->ib_dev, "initializing demux service for %d qp1 clients\n",
2132fc06573dSJack Morgenstein 		     dev->dev->caps.sqp_demux);
2133fc06573dSJack Morgenstein 	for (i = 0; i < dev->num_ports; i++) {
2134a0c64a17SJack Morgenstein 		union ib_gid gid;
2135a0c64a17SJack Morgenstein 		err = __mlx4_ib_query_gid(&dev->ib_dev, i + 1, 0, &gid, 1);
2136a0c64a17SJack Morgenstein 		if (err)
2137a0c64a17SJack Morgenstein 			goto demux_err;
2138a0c64a17SJack Morgenstein 		dev->sriov.demux[i].guid_cache[0] = gid.global.interface_id;
2139fc06573dSJack Morgenstein 		err = alloc_pv_object(dev, mlx4_master_func_num(dev->dev), i + 1,
2140fc06573dSJack Morgenstein 				      &dev->sriov.sqps[i]);
2141fc06573dSJack Morgenstein 		if (err)
2142fc06573dSJack Morgenstein 			goto demux_err;
2143fc06573dSJack Morgenstein 		err = mlx4_ib_alloc_demux_ctx(dev, &dev->sriov.demux[i], i + 1);
2144fc06573dSJack Morgenstein 		if (err)
2145cab66d12SDan Carpenter 			goto free_pv;
2146fc06573dSJack Morgenstein 	}
2147fc06573dSJack Morgenstein 	mlx4_ib_master_tunnels(dev, 1);
2148fc06573dSJack Morgenstein 	return 0;
2149fc06573dSJack Morgenstein 
2150cab66d12SDan Carpenter free_pv:
2151cab66d12SDan Carpenter 	free_pv_object(dev, mlx4_master_func_num(dev->dev), i + 1);
2152fc06573dSJack Morgenstein demux_err:
2153cab66d12SDan Carpenter 	while (--i >= 0) {
2154fc06573dSJack Morgenstein 		free_pv_object(dev, mlx4_master_func_num(dev->dev), i + 1);
2155fc06573dSJack Morgenstein 		mlx4_ib_free_demux_ctx(&dev->sriov.demux[i]);
2156fc06573dSJack Morgenstein 	}
2157c1e7e466SJack Morgenstein 	mlx4_ib_device_unregister_sysfs(dev);
2158c1e7e466SJack Morgenstein 
2159c1e7e466SJack Morgenstein sysfs_err:
2160a0c64a17SJack Morgenstein 	mlx4_ib_destroy_alias_guid_service(dev);
2161a0c64a17SJack Morgenstein 
2162a0c64a17SJack Morgenstein paravirt_err:
21633cf69cc8SAmir Vadai 	mlx4_ib_cm_paravirt_clean(dev, -1);
2164fc06573dSJack Morgenstein 
2165fc06573dSJack Morgenstein 	return err;
2166fc06573dSJack Morgenstein }
2167fc06573dSJack Morgenstein 
2168fc06573dSJack Morgenstein void mlx4_ib_close_sriov(struct mlx4_ib_dev *dev)
2169fc06573dSJack Morgenstein {
2170fc06573dSJack Morgenstein 	int i;
2171fc06573dSJack Morgenstein 	unsigned long flags;
2172fc06573dSJack Morgenstein 
2173fc06573dSJack Morgenstein 	if (!mlx4_is_mfunc(dev->dev))
2174fc06573dSJack Morgenstein 		return;
2175fc06573dSJack Morgenstein 
2176fc06573dSJack Morgenstein 	spin_lock_irqsave(&dev->sriov.going_down_lock, flags);
2177fc06573dSJack Morgenstein 	dev->sriov.is_going_down = 1;
2178fc06573dSJack Morgenstein 	spin_unlock_irqrestore(&dev->sriov.going_down_lock, flags);
21793cf69cc8SAmir Vadai 	if (mlx4_is_master(dev->dev)) {
2180fc06573dSJack Morgenstein 		for (i = 0; i < dev->num_ports; i++) {
2181fc06573dSJack Morgenstein 			flush_workqueue(dev->sriov.demux[i].ud_wq);
2182fc06573dSJack Morgenstein 			mlx4_ib_free_sqp_ctx(dev->sriov.sqps[i]);
2183fc06573dSJack Morgenstein 			kfree(dev->sriov.sqps[i]);
2184fc06573dSJack Morgenstein 			dev->sriov.sqps[i] = NULL;
2185fc06573dSJack Morgenstein 			mlx4_ib_free_demux_ctx(&dev->sriov.demux[i]);
2186fc06573dSJack Morgenstein 		}
21873cf69cc8SAmir Vadai 
21883cf69cc8SAmir Vadai 		mlx4_ib_cm_paravirt_clean(dev, -1);
2189a0c64a17SJack Morgenstein 		mlx4_ib_destroy_alias_guid_service(dev);
2190c1e7e466SJack Morgenstein 		mlx4_ib_device_unregister_sysfs(dev);
21913cf69cc8SAmir Vadai 	}
2192fc06573dSJack Morgenstein }
2193