xref: /openbmc/linux/drivers/infiniband/hw/mlx4/mad.c (revision 992e8e6e8781b71fd475bd1fd0555da7dba59966)
1225c7b1fSRoland Dreier /*
2225c7b1fSRoland Dreier  * Copyright (c) 2007 Cisco Systems, Inc. All rights reserved.
3225c7b1fSRoland Dreier  *
4225c7b1fSRoland Dreier  * This software is available to you under a choice of one of two
5225c7b1fSRoland Dreier  * licenses.  You may choose to be licensed under the terms of the GNU
6225c7b1fSRoland Dreier  * General Public License (GPL) Version 2, available from the file
7225c7b1fSRoland Dreier  * COPYING in the main directory of this source tree, or the
8225c7b1fSRoland Dreier  * OpenIB.org BSD license below:
9225c7b1fSRoland Dreier  *
10225c7b1fSRoland Dreier  *     Redistribution and use in source and binary forms, with or
11225c7b1fSRoland Dreier  *     without modification, are permitted provided that the following
12225c7b1fSRoland Dreier  *     conditions are met:
13225c7b1fSRoland Dreier  *
14225c7b1fSRoland Dreier  *      - Redistributions of source code must retain the above
15225c7b1fSRoland Dreier  *        copyright notice, this list of conditions and the following
16225c7b1fSRoland Dreier  *        disclaimer.
17225c7b1fSRoland Dreier  *
18225c7b1fSRoland Dreier  *      - Redistributions in binary form must reproduce the above
19225c7b1fSRoland Dreier  *        copyright notice, this list of conditions and the following
20225c7b1fSRoland Dreier  *        disclaimer in the documentation and/or other materials
21225c7b1fSRoland Dreier  *        provided with the distribution.
22225c7b1fSRoland Dreier  *
23225c7b1fSRoland Dreier  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24225c7b1fSRoland Dreier  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25225c7b1fSRoland Dreier  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26225c7b1fSRoland Dreier  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
27225c7b1fSRoland Dreier  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
28225c7b1fSRoland Dreier  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
29225c7b1fSRoland Dreier  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
30225c7b1fSRoland Dreier  * SOFTWARE.
31225c7b1fSRoland Dreier  */
32225c7b1fSRoland Dreier 
33225c7b1fSRoland Dreier #include <rdma/ib_mad.h>
34225c7b1fSRoland Dreier #include <rdma/ib_smi.h>
3537bfc7c1SJack Morgenstein #include <rdma/ib_sa.h>
3637bfc7c1SJack Morgenstein #include <rdma/ib_cache.h>
37225c7b1fSRoland Dreier 
38225c7b1fSRoland Dreier #include <linux/mlx4/cmd.h>
395a0e3ad6STejun Heo #include <linux/gfp.h>
40c3779134SOr Gerlitz #include <rdma/ib_pma.h>
41225c7b1fSRoland Dreier 
42225c7b1fSRoland Dreier #include "mlx4_ib.h"
43225c7b1fSRoland Dreier 
44225c7b1fSRoland Dreier enum {
45225c7b1fSRoland Dreier 	MLX4_IB_VENDOR_CLASS1 = 0x9,
46225c7b1fSRoland Dreier 	MLX4_IB_VENDOR_CLASS2 = 0xa
47225c7b1fSRoland Dreier };
48225c7b1fSRoland Dreier 
49fc06573dSJack Morgenstein #define MLX4_TUN_SEND_WRID_SHIFT 34
50fc06573dSJack Morgenstein #define MLX4_TUN_QPN_SHIFT 32
51fc06573dSJack Morgenstein #define MLX4_TUN_WRID_RECV (((u64) 1) << MLX4_TUN_SEND_WRID_SHIFT)
52fc06573dSJack Morgenstein #define MLX4_TUN_SET_WRID_QPN(a) (((u64) ((a) & 0x3)) << MLX4_TUN_QPN_SHIFT)
53fc06573dSJack Morgenstein 
54fc06573dSJack Morgenstein #define MLX4_TUN_IS_RECV(a)  (((a) >>  MLX4_TUN_SEND_WRID_SHIFT) & 0x1)
55fc06573dSJack Morgenstein #define MLX4_TUN_WRID_QPN(a) (((a) >> MLX4_TUN_QPN_SHIFT) & 0x3)
56fc06573dSJack Morgenstein 
572a4fae14SJack Morgenstein  /* Port mgmt change event handling */
582a4fae14SJack Morgenstein 
592a4fae14SJack Morgenstein #define GET_BLK_PTR_FROM_EQE(eqe) be32_to_cpu(eqe->event.port_mgmt_change.params.tbl_change_info.block_ptr)
602a4fae14SJack Morgenstein #define GET_MASK_FROM_EQE(eqe) be32_to_cpu(eqe->event.port_mgmt_change.params.tbl_change_info.tbl_entries_mask)
612a4fae14SJack Morgenstein #define NUM_IDX_IN_PKEY_TBL_BLK 32
622a4fae14SJack Morgenstein #define GUID_TBL_ENTRY_SIZE 8	   /* size in bytes */
632a4fae14SJack Morgenstein #define GUID_TBL_BLK_NUM_ENTRIES 8
642a4fae14SJack Morgenstein #define GUID_TBL_BLK_SIZE (GUID_TBL_ENTRY_SIZE * GUID_TBL_BLK_NUM_ENTRIES)
652a4fae14SJack Morgenstein 
66fc06573dSJack Morgenstein struct mlx4_mad_rcv_buf {
67fc06573dSJack Morgenstein 	struct ib_grh grh;
68fc06573dSJack Morgenstein 	u8 payload[256];
69fc06573dSJack Morgenstein } __packed;
70fc06573dSJack Morgenstein 
71fc06573dSJack Morgenstein struct mlx4_mad_snd_buf {
72fc06573dSJack Morgenstein 	u8 payload[256];
73fc06573dSJack Morgenstein } __packed;
74fc06573dSJack Morgenstein 
75fc06573dSJack Morgenstein struct mlx4_tunnel_mad {
76fc06573dSJack Morgenstein 	struct ib_grh grh;
77fc06573dSJack Morgenstein 	struct mlx4_ib_tunnel_header hdr;
78fc06573dSJack Morgenstein 	struct ib_mad mad;
79fc06573dSJack Morgenstein } __packed;
80fc06573dSJack Morgenstein 
81fc06573dSJack Morgenstein struct mlx4_rcv_tunnel_mad {
82fc06573dSJack Morgenstein 	struct mlx4_rcv_tunnel_hdr hdr;
83fc06573dSJack Morgenstein 	struct ib_grh grh;
84fc06573dSJack Morgenstein 	struct ib_mad mad;
85fc06573dSJack Morgenstein } __packed;
86fc06573dSJack Morgenstein 
87b9c5d6a6SOren Duer static void handle_client_rereg_event(struct mlx4_ib_dev *dev, u8 port_num);
882a4fae14SJack Morgenstein static void handle_lid_change_event(struct mlx4_ib_dev *dev, u8 port_num);
892a4fae14SJack Morgenstein static void __propagate_pkey_ev(struct mlx4_ib_dev *dev, int port_num,
902a4fae14SJack Morgenstein 				int block, u32 change_bitmap);
91b9c5d6a6SOren Duer 
92b9c5d6a6SOren Duer __be64 mlx4_ib_get_new_demux_tid(struct mlx4_ib_demux_ctx *ctx)
93b9c5d6a6SOren Duer {
94b9c5d6a6SOren Duer 	return cpu_to_be64(atomic_inc_return(&ctx->tid)) |
95b9c5d6a6SOren Duer 		cpu_to_be64(0xff00000000000000LL);
96b9c5d6a6SOren Duer }
97b9c5d6a6SOren Duer 
980a9a0188SJack Morgenstein int mlx4_MAD_IFC(struct mlx4_ib_dev *dev, int mad_ifc_flags,
99225c7b1fSRoland Dreier 		 int port, struct ib_wc *in_wc, struct ib_grh *in_grh,
100225c7b1fSRoland Dreier 		 void *in_mad, void *response_mad)
101225c7b1fSRoland Dreier {
102225c7b1fSRoland Dreier 	struct mlx4_cmd_mailbox *inmailbox, *outmailbox;
103225c7b1fSRoland Dreier 	void *inbox;
104225c7b1fSRoland Dreier 	int err;
105225c7b1fSRoland Dreier 	u32 in_modifier = port;
106225c7b1fSRoland Dreier 	u8 op_modifier = 0;
107225c7b1fSRoland Dreier 
108225c7b1fSRoland Dreier 	inmailbox = mlx4_alloc_cmd_mailbox(dev->dev);
109225c7b1fSRoland Dreier 	if (IS_ERR(inmailbox))
110225c7b1fSRoland Dreier 		return PTR_ERR(inmailbox);
111225c7b1fSRoland Dreier 	inbox = inmailbox->buf;
112225c7b1fSRoland Dreier 
113225c7b1fSRoland Dreier 	outmailbox = mlx4_alloc_cmd_mailbox(dev->dev);
114225c7b1fSRoland Dreier 	if (IS_ERR(outmailbox)) {
115225c7b1fSRoland Dreier 		mlx4_free_cmd_mailbox(dev->dev, inmailbox);
116225c7b1fSRoland Dreier 		return PTR_ERR(outmailbox);
117225c7b1fSRoland Dreier 	}
118225c7b1fSRoland Dreier 
119225c7b1fSRoland Dreier 	memcpy(inbox, in_mad, 256);
120225c7b1fSRoland Dreier 
121225c7b1fSRoland Dreier 	/*
122225c7b1fSRoland Dreier 	 * Key check traps can't be generated unless we have in_wc to
123225c7b1fSRoland Dreier 	 * tell us where to send the trap.
124225c7b1fSRoland Dreier 	 */
1250a9a0188SJack Morgenstein 	if ((mad_ifc_flags & MLX4_MAD_IFC_IGNORE_MKEY) || !in_wc)
126225c7b1fSRoland Dreier 		op_modifier |= 0x1;
1270a9a0188SJack Morgenstein 	if ((mad_ifc_flags & MLX4_MAD_IFC_IGNORE_BKEY) || !in_wc)
128225c7b1fSRoland Dreier 		op_modifier |= 0x2;
1290a9a0188SJack Morgenstein 	if (mlx4_is_mfunc(dev->dev) &&
1300a9a0188SJack Morgenstein 	    (mad_ifc_flags & MLX4_MAD_IFC_NET_VIEW || in_wc))
1310a9a0188SJack Morgenstein 		op_modifier |= 0x8;
132225c7b1fSRoland Dreier 
133225c7b1fSRoland Dreier 	if (in_wc) {
134225c7b1fSRoland Dreier 		struct {
135225c7b1fSRoland Dreier 			__be32		my_qpn;
136225c7b1fSRoland Dreier 			u32		reserved1;
137225c7b1fSRoland Dreier 			__be32		rqpn;
138225c7b1fSRoland Dreier 			u8		sl;
139225c7b1fSRoland Dreier 			u8		g_path;
140225c7b1fSRoland Dreier 			u16		reserved2[2];
141225c7b1fSRoland Dreier 			__be16		pkey;
142225c7b1fSRoland Dreier 			u32		reserved3[11];
143225c7b1fSRoland Dreier 			u8		grh[40];
144225c7b1fSRoland Dreier 		} *ext_info;
145225c7b1fSRoland Dreier 
146225c7b1fSRoland Dreier 		memset(inbox + 256, 0, 256);
147225c7b1fSRoland Dreier 		ext_info = inbox + 256;
148225c7b1fSRoland Dreier 
149225c7b1fSRoland Dreier 		ext_info->my_qpn = cpu_to_be32(in_wc->qp->qp_num);
150225c7b1fSRoland Dreier 		ext_info->rqpn   = cpu_to_be32(in_wc->src_qp);
151225c7b1fSRoland Dreier 		ext_info->sl     = in_wc->sl << 4;
152225c7b1fSRoland Dreier 		ext_info->g_path = in_wc->dlid_path_bits |
153225c7b1fSRoland Dreier 			(in_wc->wc_flags & IB_WC_GRH ? 0x80 : 0);
154225c7b1fSRoland Dreier 		ext_info->pkey   = cpu_to_be16(in_wc->pkey_index);
155225c7b1fSRoland Dreier 
156225c7b1fSRoland Dreier 		if (in_grh)
157225c7b1fSRoland Dreier 			memcpy(ext_info->grh, in_grh, 40);
158225c7b1fSRoland Dreier 
159225c7b1fSRoland Dreier 		op_modifier |= 0x4;
160225c7b1fSRoland Dreier 
161225c7b1fSRoland Dreier 		in_modifier |= in_wc->slid << 16;
162225c7b1fSRoland Dreier 	}
163225c7b1fSRoland Dreier 
1640a9a0188SJack Morgenstein 	err = mlx4_cmd_box(dev->dev, inmailbox->dma, outmailbox->dma, in_modifier,
1650a9a0188SJack Morgenstein 			   mlx4_is_master(dev->dev) ? (op_modifier & ~0x8) : op_modifier,
166f9baff50SJack Morgenstein 			   MLX4_CMD_MAD_IFC, MLX4_CMD_TIME_CLASS_C,
1670a9a0188SJack Morgenstein 			   (op_modifier & 0x8) ? MLX4_CMD_NATIVE : MLX4_CMD_WRAPPED);
168225c7b1fSRoland Dreier 
169fe11cb6bSIlpo Järvinen 	if (!err)
170225c7b1fSRoland Dreier 		memcpy(response_mad, outmailbox->buf, 256);
171225c7b1fSRoland Dreier 
172225c7b1fSRoland Dreier 	mlx4_free_cmd_mailbox(dev->dev, inmailbox);
173225c7b1fSRoland Dreier 	mlx4_free_cmd_mailbox(dev->dev, outmailbox);
174225c7b1fSRoland Dreier 
175225c7b1fSRoland Dreier 	return err;
176225c7b1fSRoland Dreier }
177225c7b1fSRoland Dreier 
178225c7b1fSRoland Dreier static void update_sm_ah(struct mlx4_ib_dev *dev, u8 port_num, u16 lid, u8 sl)
179225c7b1fSRoland Dreier {
180225c7b1fSRoland Dreier 	struct ib_ah *new_ah;
181225c7b1fSRoland Dreier 	struct ib_ah_attr ah_attr;
182df7fba66SJack Morgenstein 	unsigned long flags;
183225c7b1fSRoland Dreier 
184225c7b1fSRoland Dreier 	if (!dev->send_agent[port_num - 1][0])
185225c7b1fSRoland Dreier 		return;
186225c7b1fSRoland Dreier 
187225c7b1fSRoland Dreier 	memset(&ah_attr, 0, sizeof ah_attr);
188225c7b1fSRoland Dreier 	ah_attr.dlid     = lid;
189225c7b1fSRoland Dreier 	ah_attr.sl       = sl;
190225c7b1fSRoland Dreier 	ah_attr.port_num = port_num;
191225c7b1fSRoland Dreier 
192225c7b1fSRoland Dreier 	new_ah = ib_create_ah(dev->send_agent[port_num - 1][0]->qp->pd,
193225c7b1fSRoland Dreier 			      &ah_attr);
194225c7b1fSRoland Dreier 	if (IS_ERR(new_ah))
195225c7b1fSRoland Dreier 		return;
196225c7b1fSRoland Dreier 
197df7fba66SJack Morgenstein 	spin_lock_irqsave(&dev->sm_lock, flags);
198225c7b1fSRoland Dreier 	if (dev->sm_ah[port_num - 1])
199225c7b1fSRoland Dreier 		ib_destroy_ah(dev->sm_ah[port_num - 1]);
200225c7b1fSRoland Dreier 	dev->sm_ah[port_num - 1] = new_ah;
201df7fba66SJack Morgenstein 	spin_unlock_irqrestore(&dev->sm_lock, flags);
202225c7b1fSRoland Dreier }
203225c7b1fSRoland Dreier 
204225c7b1fSRoland Dreier /*
20500f5ce99SJack Morgenstein  * Snoop SM MADs for port info, GUID info, and  P_Key table sets, so we can
20600f5ce99SJack Morgenstein  * synthesize LID change, Client-Rereg, GID change, and P_Key change events.
207225c7b1fSRoland Dreier  */
208f0f6f346SMoni Shoua static void smp_snoop(struct ib_device *ibdev, u8 port_num, struct ib_mad *mad,
209f0f6f346SMoni Shoua 		      u16 prev_lid)
210225c7b1fSRoland Dreier {
21100f5ce99SJack Morgenstein 	struct ib_port_info *pinfo;
21200f5ce99SJack Morgenstein 	u16 lid;
21354679e14SJack Morgenstein 	__be16 *base;
21454679e14SJack Morgenstein 	u32 bn, pkey_change_bitmap;
21554679e14SJack Morgenstein 	int i;
21654679e14SJack Morgenstein 
217225c7b1fSRoland Dreier 
21800f5ce99SJack Morgenstein 	struct mlx4_ib_dev *dev = to_mdev(ibdev);
219225c7b1fSRoland Dreier 	if ((mad->mad_hdr.mgmt_class == IB_MGMT_CLASS_SUBN_LID_ROUTED ||
220225c7b1fSRoland Dreier 	     mad->mad_hdr.mgmt_class == IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE) &&
22100f5ce99SJack Morgenstein 	    mad->mad_hdr.method == IB_MGMT_METHOD_SET)
22200f5ce99SJack Morgenstein 		switch (mad->mad_hdr.attr_id) {
22300f5ce99SJack Morgenstein 		case IB_SMP_ATTR_PORT_INFO:
22400f5ce99SJack Morgenstein 			pinfo = (struct ib_port_info *) ((struct ib_smp *) mad)->data;
22500f5ce99SJack Morgenstein 			lid = be16_to_cpu(pinfo->lid);
226225c7b1fSRoland Dreier 
22700f5ce99SJack Morgenstein 			update_sm_ah(dev, port_num,
228225c7b1fSRoland Dreier 				     be16_to_cpu(pinfo->sm_lid),
229225c7b1fSRoland Dreier 				     pinfo->neighbormtu_mastersmsl & 0xf);
230225c7b1fSRoland Dreier 
23100f5ce99SJack Morgenstein 			if (pinfo->clientrereg_resv_subnetto & 0x80)
232b9c5d6a6SOren Duer 				handle_client_rereg_event(dev, port_num);
233225c7b1fSRoland Dreier 
23400f5ce99SJack Morgenstein 			if (prev_lid != lid)
2352a4fae14SJack Morgenstein 				handle_lid_change_event(dev, port_num);
23600f5ce99SJack Morgenstein 			break;
237225c7b1fSRoland Dreier 
23800f5ce99SJack Morgenstein 		case IB_SMP_ATTR_PKEY_TABLE:
23954679e14SJack Morgenstein 			if (!mlx4_is_mfunc(dev->dev)) {
24000f5ce99SJack Morgenstein 				mlx4_ib_dispatch_event(dev, port_num,
24100f5ce99SJack Morgenstein 						       IB_EVENT_PKEY_CHANGE);
24200f5ce99SJack Morgenstein 				break;
24354679e14SJack Morgenstein 			}
24454679e14SJack Morgenstein 
2452a4fae14SJack Morgenstein 			/* at this point, we are running in the master.
2462a4fae14SJack Morgenstein 			 * Slaves do not receive SMPs.
2472a4fae14SJack Morgenstein 			 */
24854679e14SJack Morgenstein 			bn  = be32_to_cpu(((struct ib_smp *)mad)->attr_mod) & 0xFFFF;
24954679e14SJack Morgenstein 			base = (__be16 *) &(((struct ib_smp *)mad)->data[0]);
25054679e14SJack Morgenstein 			pkey_change_bitmap = 0;
25154679e14SJack Morgenstein 			for (i = 0; i < 32; i++) {
25254679e14SJack Morgenstein 				pr_debug("PKEY[%d] = x%x\n",
25354679e14SJack Morgenstein 					 i + bn*32, be16_to_cpu(base[i]));
25454679e14SJack Morgenstein 				if (be16_to_cpu(base[i]) !=
25554679e14SJack Morgenstein 				    dev->pkeys.phys_pkey_cache[port_num - 1][i + bn*32]) {
25654679e14SJack Morgenstein 					pkey_change_bitmap |= (1 << i);
25754679e14SJack Morgenstein 					dev->pkeys.phys_pkey_cache[port_num - 1][i + bn*32] =
25854679e14SJack Morgenstein 						be16_to_cpu(base[i]);
25954679e14SJack Morgenstein 				}
26054679e14SJack Morgenstein 			}
26154679e14SJack Morgenstein 			pr_debug("PKEY Change event: port=%d, "
26254679e14SJack Morgenstein 				 "block=0x%x, change_bitmap=0x%x\n",
26354679e14SJack Morgenstein 				 port_num, bn, pkey_change_bitmap);
26454679e14SJack Morgenstein 
2652a4fae14SJack Morgenstein 			if (pkey_change_bitmap) {
26654679e14SJack Morgenstein 				mlx4_ib_dispatch_event(dev, port_num,
26754679e14SJack Morgenstein 						       IB_EVENT_PKEY_CHANGE);
2682a4fae14SJack Morgenstein 				if (!dev->sriov.is_going_down)
2692a4fae14SJack Morgenstein 					__propagate_pkey_ev(dev, port_num, bn,
2702a4fae14SJack Morgenstein 							    pkey_change_bitmap);
2712a4fae14SJack Morgenstein 			}
27254679e14SJack Morgenstein 			break;
273f0f6f346SMoni Shoua 
27400f5ce99SJack Morgenstein 		case IB_SMP_ATTR_GUID_INFO:
2756634961cSJack Morgenstein 			/* paravirtualized master's guid is guid 0 -- does not change */
2766634961cSJack Morgenstein 			if (!mlx4_is_master(dev->dev))
27700f5ce99SJack Morgenstein 				mlx4_ib_dispatch_event(dev, port_num,
27800f5ce99SJack Morgenstein 						       IB_EVENT_GID_CHANGE);
2792a4fae14SJack Morgenstein 			/*if master, notify relevant slaves*/
2802a4fae14SJack Morgenstein 			if (mlx4_is_master(dev->dev) &&
2812a4fae14SJack Morgenstein 			    !dev->sriov.is_going_down) {
2822a4fae14SJack Morgenstein 				bn = be32_to_cpu(((struct ib_smp *)mad)->attr_mod);
2832a4fae14SJack Morgenstein 				mlx4_ib_update_cache_on_guid_change(dev, bn, port_num,
2842a4fae14SJack Morgenstein 								    (u8 *)(&((struct ib_smp *)mad)->data));
2852a4fae14SJack Morgenstein 				mlx4_ib_notify_slaves_on_guid_change(dev, bn, port_num,
2862a4fae14SJack Morgenstein 								     (u8 *)(&((struct ib_smp *)mad)->data));
2872a4fae14SJack Morgenstein 			}
28800f5ce99SJack Morgenstein 			break;
2892a4fae14SJack Morgenstein 
29000f5ce99SJack Morgenstein 		default:
29100f5ce99SJack Morgenstein 			break;
292225c7b1fSRoland Dreier 		}
293225c7b1fSRoland Dreier }
294225c7b1fSRoland Dreier 
2952a4fae14SJack Morgenstein static void __propagate_pkey_ev(struct mlx4_ib_dev *dev, int port_num,
2962a4fae14SJack Morgenstein 				int block, u32 change_bitmap)
2972a4fae14SJack Morgenstein {
2982a4fae14SJack Morgenstein 	int i, ix, slave, err;
2992a4fae14SJack Morgenstein 	int have_event = 0;
3002a4fae14SJack Morgenstein 
3012a4fae14SJack Morgenstein 	for (slave = 0; slave < dev->dev->caps.sqp_demux; slave++) {
3022a4fae14SJack Morgenstein 		if (slave == mlx4_master_func_num(dev->dev))
3032a4fae14SJack Morgenstein 			continue;
3042a4fae14SJack Morgenstein 		if (!mlx4_is_slave_active(dev->dev, slave))
3052a4fae14SJack Morgenstein 			continue;
3062a4fae14SJack Morgenstein 
3072a4fae14SJack Morgenstein 		have_event = 0;
3082a4fae14SJack Morgenstein 		for (i = 0; i < 32; i++) {
3092a4fae14SJack Morgenstein 			if (!(change_bitmap & (1 << i)))
3102a4fae14SJack Morgenstein 				continue;
3112a4fae14SJack Morgenstein 			for (ix = 0;
3122a4fae14SJack Morgenstein 			     ix < dev->dev->caps.pkey_table_len[port_num]; ix++) {
3132a4fae14SJack Morgenstein 				if (dev->pkeys.virt2phys_pkey[slave][port_num - 1]
3142a4fae14SJack Morgenstein 				    [ix] == i + 32 * block) {
3152a4fae14SJack Morgenstein 					err = mlx4_gen_pkey_eqe(dev->dev, slave, port_num);
3162a4fae14SJack Morgenstein 					pr_debug("propagate_pkey_ev: slave %d,"
3172a4fae14SJack Morgenstein 						 " port %d, ix %d (%d)\n",
3182a4fae14SJack Morgenstein 						 slave, port_num, ix, err);
3192a4fae14SJack Morgenstein 					have_event = 1;
3202a4fae14SJack Morgenstein 					break;
3212a4fae14SJack Morgenstein 				}
3222a4fae14SJack Morgenstein 			}
3232a4fae14SJack Morgenstein 			if (have_event)
3242a4fae14SJack Morgenstein 				break;
3252a4fae14SJack Morgenstein 		}
3262a4fae14SJack Morgenstein 	}
3272a4fae14SJack Morgenstein }
3282a4fae14SJack Morgenstein 
329225c7b1fSRoland Dreier static void node_desc_override(struct ib_device *dev,
330225c7b1fSRoland Dreier 			       struct ib_mad *mad)
331225c7b1fSRoland Dreier {
332df7fba66SJack Morgenstein 	unsigned long flags;
333df7fba66SJack Morgenstein 
334225c7b1fSRoland Dreier 	if ((mad->mad_hdr.mgmt_class == IB_MGMT_CLASS_SUBN_LID_ROUTED ||
335225c7b1fSRoland Dreier 	     mad->mad_hdr.mgmt_class == IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE) &&
336225c7b1fSRoland Dreier 	    mad->mad_hdr.method == IB_MGMT_METHOD_GET_RESP &&
337225c7b1fSRoland Dreier 	    mad->mad_hdr.attr_id == IB_SMP_ATTR_NODE_DESC) {
338df7fba66SJack Morgenstein 		spin_lock_irqsave(&to_mdev(dev)->sm_lock, flags);
339225c7b1fSRoland Dreier 		memcpy(((struct ib_smp *) mad)->data, dev->node_desc, 64);
340df7fba66SJack Morgenstein 		spin_unlock_irqrestore(&to_mdev(dev)->sm_lock, flags);
341225c7b1fSRoland Dreier 	}
342225c7b1fSRoland Dreier }
343225c7b1fSRoland Dreier 
344225c7b1fSRoland Dreier static void forward_trap(struct mlx4_ib_dev *dev, u8 port_num, struct ib_mad *mad)
345225c7b1fSRoland Dreier {
346225c7b1fSRoland Dreier 	int qpn = mad->mad_hdr.mgmt_class != IB_MGMT_CLASS_SUBN_LID_ROUTED;
347225c7b1fSRoland Dreier 	struct ib_mad_send_buf *send_buf;
348225c7b1fSRoland Dreier 	struct ib_mad_agent *agent = dev->send_agent[port_num - 1][qpn];
349225c7b1fSRoland Dreier 	int ret;
350df7fba66SJack Morgenstein 	unsigned long flags;
351225c7b1fSRoland Dreier 
352225c7b1fSRoland Dreier 	if (agent) {
353225c7b1fSRoland Dreier 		send_buf = ib_create_send_mad(agent, qpn, 0, 0, IB_MGMT_MAD_HDR,
354225c7b1fSRoland Dreier 					      IB_MGMT_MAD_DATA, GFP_ATOMIC);
35513974909SDan Carpenter 		if (IS_ERR(send_buf))
35613974909SDan Carpenter 			return;
357225c7b1fSRoland Dreier 		/*
358225c7b1fSRoland Dreier 		 * We rely here on the fact that MLX QPs don't use the
359225c7b1fSRoland Dreier 		 * address handle after the send is posted (this is
360225c7b1fSRoland Dreier 		 * wrong following the IB spec strictly, but we know
361225c7b1fSRoland Dreier 		 * it's OK for our devices).
362225c7b1fSRoland Dreier 		 */
363df7fba66SJack Morgenstein 		spin_lock_irqsave(&dev->sm_lock, flags);
364225c7b1fSRoland Dreier 		memcpy(send_buf->mad, mad, sizeof *mad);
365225c7b1fSRoland Dreier 		if ((send_buf->ah = dev->sm_ah[port_num - 1]))
366225c7b1fSRoland Dreier 			ret = ib_post_send_mad(send_buf, NULL);
367225c7b1fSRoland Dreier 		else
368225c7b1fSRoland Dreier 			ret = -EINVAL;
369df7fba66SJack Morgenstein 		spin_unlock_irqrestore(&dev->sm_lock, flags);
370225c7b1fSRoland Dreier 
371225c7b1fSRoland Dreier 		if (ret)
372225c7b1fSRoland Dreier 			ib_free_send_mad(send_buf);
373225c7b1fSRoland Dreier 	}
374225c7b1fSRoland Dreier }
375225c7b1fSRoland Dreier 
37637bfc7c1SJack Morgenstein static int mlx4_ib_demux_sa_handler(struct ib_device *ibdev, int port, int slave,
37737bfc7c1SJack Morgenstein 							     struct ib_sa_mad *sa_mad)
37837bfc7c1SJack Morgenstein {
379b9c5d6a6SOren Duer 	int ret = 0;
380b9c5d6a6SOren Duer 
381b9c5d6a6SOren Duer 	/* dispatch to different sa handlers */
382b9c5d6a6SOren Duer 	switch (be16_to_cpu(sa_mad->mad_hdr.attr_id)) {
383b9c5d6a6SOren Duer 	case IB_SA_ATTR_MC_MEMBER_REC:
384b9c5d6a6SOren Duer 		ret = mlx4_ib_mcg_demux_handler(ibdev, port, slave, sa_mad);
385b9c5d6a6SOren Duer 		break;
386b9c5d6a6SOren Duer 	default:
387b9c5d6a6SOren Duer 		break;
388b9c5d6a6SOren Duer 	}
389b9c5d6a6SOren Duer 	return ret;
39037bfc7c1SJack Morgenstein }
39137bfc7c1SJack Morgenstein 
39237bfc7c1SJack Morgenstein int mlx4_ib_find_real_gid(struct ib_device *ibdev, u8 port, __be64 guid)
39337bfc7c1SJack Morgenstein {
39437bfc7c1SJack Morgenstein 	struct mlx4_ib_dev *dev = to_mdev(ibdev);
39537bfc7c1SJack Morgenstein 	int i;
39637bfc7c1SJack Morgenstein 
39737bfc7c1SJack Morgenstein 	for (i = 0; i < dev->dev->caps.sqp_demux; i++) {
39837bfc7c1SJack Morgenstein 		if (dev->sriov.demux[port - 1].guid_cache[i] == guid)
39937bfc7c1SJack Morgenstein 			return i;
40037bfc7c1SJack Morgenstein 	}
40137bfc7c1SJack Morgenstein 	return -1;
40237bfc7c1SJack Morgenstein }
40337bfc7c1SJack Morgenstein 
40437bfc7c1SJack Morgenstein 
40537bfc7c1SJack Morgenstein static int get_pkey_phys_indices(struct mlx4_ib_dev *ibdev, u8 port, u8 ph_pkey_ix,
40637bfc7c1SJack Morgenstein 				 u8 *full_pk_ix, u8 *partial_pk_ix,
40737bfc7c1SJack Morgenstein 				 int *is_full_member)
40837bfc7c1SJack Morgenstein {
40937bfc7c1SJack Morgenstein 	u16 search_pkey;
41037bfc7c1SJack Morgenstein 	int fm;
41137bfc7c1SJack Morgenstein 	int err = 0;
41237bfc7c1SJack Morgenstein 	u16 pk;
41337bfc7c1SJack Morgenstein 
41437bfc7c1SJack Morgenstein 	err = ib_get_cached_pkey(&ibdev->ib_dev, port, ph_pkey_ix, &search_pkey);
41537bfc7c1SJack Morgenstein 	if (err)
41637bfc7c1SJack Morgenstein 		return err;
41737bfc7c1SJack Morgenstein 
41837bfc7c1SJack Morgenstein 	fm = (search_pkey & 0x8000) ? 1 : 0;
41937bfc7c1SJack Morgenstein 	if (fm) {
42037bfc7c1SJack Morgenstein 		*full_pk_ix = ph_pkey_ix;
42137bfc7c1SJack Morgenstein 		search_pkey &= 0x7FFF;
42237bfc7c1SJack Morgenstein 	} else {
42337bfc7c1SJack Morgenstein 		*partial_pk_ix = ph_pkey_ix;
42437bfc7c1SJack Morgenstein 		search_pkey |= 0x8000;
42537bfc7c1SJack Morgenstein 	}
42637bfc7c1SJack Morgenstein 
42737bfc7c1SJack Morgenstein 	if (ib_find_exact_cached_pkey(&ibdev->ib_dev, port, search_pkey, &pk))
42837bfc7c1SJack Morgenstein 		pk = 0xFFFF;
42937bfc7c1SJack Morgenstein 
43037bfc7c1SJack Morgenstein 	if (fm)
43137bfc7c1SJack Morgenstein 		*partial_pk_ix = (pk & 0xFF);
43237bfc7c1SJack Morgenstein 	else
43337bfc7c1SJack Morgenstein 		*full_pk_ix = (pk & 0xFF);
43437bfc7c1SJack Morgenstein 
43537bfc7c1SJack Morgenstein 	*is_full_member = fm;
43637bfc7c1SJack Morgenstein 	return err;
43737bfc7c1SJack Morgenstein }
43837bfc7c1SJack Morgenstein 
43937bfc7c1SJack Morgenstein int mlx4_ib_send_to_slave(struct mlx4_ib_dev *dev, int slave, u8 port,
44037bfc7c1SJack Morgenstein 			  enum ib_qp_type dest_qpt, struct ib_wc *wc,
44137bfc7c1SJack Morgenstein 			  struct ib_grh *grh, struct ib_mad *mad)
44237bfc7c1SJack Morgenstein {
44337bfc7c1SJack Morgenstein 	struct ib_sge list;
44437bfc7c1SJack Morgenstein 	struct ib_send_wr wr, *bad_wr;
44537bfc7c1SJack Morgenstein 	struct mlx4_ib_demux_pv_ctx *tun_ctx;
44637bfc7c1SJack Morgenstein 	struct mlx4_ib_demux_pv_qp *tun_qp;
44737bfc7c1SJack Morgenstein 	struct mlx4_rcv_tunnel_mad *tun_mad;
44837bfc7c1SJack Morgenstein 	struct ib_ah_attr attr;
44937bfc7c1SJack Morgenstein 	struct ib_ah *ah;
45037bfc7c1SJack Morgenstein 	struct ib_qp *src_qp = NULL;
45137bfc7c1SJack Morgenstein 	unsigned tun_tx_ix = 0;
45237bfc7c1SJack Morgenstein 	int dqpn;
45337bfc7c1SJack Morgenstein 	int ret = 0;
45437bfc7c1SJack Morgenstein 	int i;
45537bfc7c1SJack Morgenstein 	int is_full_member = 0;
45637bfc7c1SJack Morgenstein 	u16 tun_pkey_ix;
45737bfc7c1SJack Morgenstein 	u8 ph_pkey_ix, full_pk_ix = 0, partial_pk_ix = 0;
45837bfc7c1SJack Morgenstein 
45937bfc7c1SJack Morgenstein 	if (dest_qpt > IB_QPT_GSI)
46037bfc7c1SJack Morgenstein 		return -EINVAL;
46137bfc7c1SJack Morgenstein 
46237bfc7c1SJack Morgenstein 	tun_ctx = dev->sriov.demux[port-1].tun[slave];
46337bfc7c1SJack Morgenstein 
46437bfc7c1SJack Morgenstein 	/* check if proxy qp created */
46537bfc7c1SJack Morgenstein 	if (!tun_ctx || tun_ctx->state != DEMUX_PV_STATE_ACTIVE)
46637bfc7c1SJack Morgenstein 		return -EAGAIN;
46737bfc7c1SJack Morgenstein 
46837bfc7c1SJack Morgenstein 	/* QP0 forwarding only for Dom0 */
46937bfc7c1SJack Morgenstein 	if (!dest_qpt && (mlx4_master_func_num(dev->dev) != slave))
47037bfc7c1SJack Morgenstein 		return -EINVAL;
47137bfc7c1SJack Morgenstein 
47237bfc7c1SJack Morgenstein 	if (!dest_qpt)
47337bfc7c1SJack Morgenstein 		tun_qp = &tun_ctx->qp[0];
47437bfc7c1SJack Morgenstein 	else
47537bfc7c1SJack Morgenstein 		tun_qp = &tun_ctx->qp[1];
47637bfc7c1SJack Morgenstein 
47737bfc7c1SJack Morgenstein 	/* compute pkey index for slave */
47837bfc7c1SJack Morgenstein 	/* get physical pkey -- virtualized Dom0 pkey to phys*/
47937bfc7c1SJack Morgenstein 	if (dest_qpt) {
48037bfc7c1SJack Morgenstein 		ph_pkey_ix =
48137bfc7c1SJack Morgenstein 			dev->pkeys.virt2phys_pkey[mlx4_master_func_num(dev->dev)][port - 1][wc->pkey_index];
48237bfc7c1SJack Morgenstein 
48337bfc7c1SJack Morgenstein 		/* now, translate this to the slave pkey index */
48437bfc7c1SJack Morgenstein 		ret = get_pkey_phys_indices(dev, port, ph_pkey_ix, &full_pk_ix,
48537bfc7c1SJack Morgenstein 					    &partial_pk_ix, &is_full_member);
48637bfc7c1SJack Morgenstein 		if (ret)
48737bfc7c1SJack Morgenstein 			return -EINVAL;
48837bfc7c1SJack Morgenstein 
48937bfc7c1SJack Morgenstein 		for (i = 0; i < dev->dev->caps.pkey_table_len[port]; i++) {
49037bfc7c1SJack Morgenstein 			if ((dev->pkeys.virt2phys_pkey[slave][port - 1][i] == full_pk_ix) ||
49137bfc7c1SJack Morgenstein 			    (is_full_member &&
49237bfc7c1SJack Morgenstein 			     (dev->pkeys.virt2phys_pkey[slave][port - 1][i] == partial_pk_ix)))
49337bfc7c1SJack Morgenstein 				break;
49437bfc7c1SJack Morgenstein 		}
49537bfc7c1SJack Morgenstein 		if (i == dev->dev->caps.pkey_table_len[port])
49637bfc7c1SJack Morgenstein 			return -EINVAL;
49737bfc7c1SJack Morgenstein 		tun_pkey_ix = i;
49837bfc7c1SJack Morgenstein 	} else
49937bfc7c1SJack Morgenstein 		tun_pkey_ix = dev->pkeys.virt2phys_pkey[slave][port - 1][0];
50037bfc7c1SJack Morgenstein 
50137bfc7c1SJack Morgenstein 	dqpn = dev->dev->caps.sqp_start + 8 * slave + port + (dest_qpt * 2) - 1;
50237bfc7c1SJack Morgenstein 
50337bfc7c1SJack Morgenstein 	/* get tunnel tx data buf for slave */
50437bfc7c1SJack Morgenstein 	src_qp = tun_qp->qp;
50537bfc7c1SJack Morgenstein 
50637bfc7c1SJack Morgenstein 	/* create ah. Just need an empty one with the port num for the post send.
50737bfc7c1SJack Morgenstein 	 * The driver will set the force loopback bit in post_send */
50837bfc7c1SJack Morgenstein 	memset(&attr, 0, sizeof attr);
50937bfc7c1SJack Morgenstein 	attr.port_num = port;
51037bfc7c1SJack Morgenstein 	ah = ib_create_ah(tun_ctx->pd, &attr);
51137bfc7c1SJack Morgenstein 	if (IS_ERR(ah))
51237bfc7c1SJack Morgenstein 		return -ENOMEM;
51337bfc7c1SJack Morgenstein 
51437bfc7c1SJack Morgenstein 	/* allocate tunnel tx buf after pass failure returns */
51537bfc7c1SJack Morgenstein 	spin_lock(&tun_qp->tx_lock);
51637bfc7c1SJack Morgenstein 	if (tun_qp->tx_ix_head - tun_qp->tx_ix_tail >=
51737bfc7c1SJack Morgenstein 	    (MLX4_NUM_TUNNEL_BUFS - 1))
51837bfc7c1SJack Morgenstein 		ret = -EAGAIN;
51937bfc7c1SJack Morgenstein 	else
52037bfc7c1SJack Morgenstein 		tun_tx_ix = (++tun_qp->tx_ix_head) & (MLX4_NUM_TUNNEL_BUFS - 1);
52137bfc7c1SJack Morgenstein 	spin_unlock(&tun_qp->tx_lock);
52237bfc7c1SJack Morgenstein 	if (ret)
52337bfc7c1SJack Morgenstein 		goto out;
52437bfc7c1SJack Morgenstein 
52537bfc7c1SJack Morgenstein 	tun_mad = (struct mlx4_rcv_tunnel_mad *) (tun_qp->tx_ring[tun_tx_ix].buf.addr);
52637bfc7c1SJack Morgenstein 	if (tun_qp->tx_ring[tun_tx_ix].ah)
52737bfc7c1SJack Morgenstein 		ib_destroy_ah(tun_qp->tx_ring[tun_tx_ix].ah);
52837bfc7c1SJack Morgenstein 	tun_qp->tx_ring[tun_tx_ix].ah = ah;
52937bfc7c1SJack Morgenstein 	ib_dma_sync_single_for_cpu(&dev->ib_dev,
53037bfc7c1SJack Morgenstein 				   tun_qp->tx_ring[tun_tx_ix].buf.map,
53137bfc7c1SJack Morgenstein 				   sizeof (struct mlx4_rcv_tunnel_mad),
53237bfc7c1SJack Morgenstein 				   DMA_TO_DEVICE);
53337bfc7c1SJack Morgenstein 
53437bfc7c1SJack Morgenstein 	/* copy over to tunnel buffer */
53537bfc7c1SJack Morgenstein 	if (grh)
53637bfc7c1SJack Morgenstein 		memcpy(&tun_mad->grh, grh, sizeof *grh);
53737bfc7c1SJack Morgenstein 	memcpy(&tun_mad->mad, mad, sizeof *mad);
53837bfc7c1SJack Morgenstein 
53937bfc7c1SJack Morgenstein 	/* adjust tunnel data */
54037bfc7c1SJack Morgenstein 	tun_mad->hdr.pkey_index = cpu_to_be16(tun_pkey_ix);
54137bfc7c1SJack Morgenstein 	tun_mad->hdr.sl_vid = cpu_to_be16(((u16)(wc->sl)) << 12);
54237bfc7c1SJack Morgenstein 	tun_mad->hdr.slid_mac_47_32 = cpu_to_be16(wc->slid);
54337bfc7c1SJack Morgenstein 	tun_mad->hdr.flags_src_qp = cpu_to_be32(wc->src_qp & 0xFFFFFF);
54437bfc7c1SJack Morgenstein 	tun_mad->hdr.g_ml_path = (grh && (wc->wc_flags & IB_WC_GRH)) ? 0x80 : 0;
54537bfc7c1SJack Morgenstein 
54637bfc7c1SJack Morgenstein 	ib_dma_sync_single_for_device(&dev->ib_dev,
54737bfc7c1SJack Morgenstein 				      tun_qp->tx_ring[tun_tx_ix].buf.map,
54837bfc7c1SJack Morgenstein 				      sizeof (struct mlx4_rcv_tunnel_mad),
54937bfc7c1SJack Morgenstein 				      DMA_TO_DEVICE);
55037bfc7c1SJack Morgenstein 
55137bfc7c1SJack Morgenstein 	list.addr = tun_qp->tx_ring[tun_tx_ix].buf.map;
55237bfc7c1SJack Morgenstein 	list.length = sizeof (struct mlx4_rcv_tunnel_mad);
55337bfc7c1SJack Morgenstein 	list.lkey = tun_ctx->mr->lkey;
55437bfc7c1SJack Morgenstein 
55537bfc7c1SJack Morgenstein 	wr.wr.ud.ah = ah;
55637bfc7c1SJack Morgenstein 	wr.wr.ud.port_num = port;
55737bfc7c1SJack Morgenstein 	wr.wr.ud.remote_qkey = IB_QP_SET_QKEY;
55837bfc7c1SJack Morgenstein 	wr.wr.ud.remote_qpn = dqpn;
55937bfc7c1SJack Morgenstein 	wr.next = NULL;
56037bfc7c1SJack Morgenstein 	wr.wr_id = ((u64) tun_tx_ix) | MLX4_TUN_SET_WRID_QPN(dest_qpt);
56137bfc7c1SJack Morgenstein 	wr.sg_list = &list;
56237bfc7c1SJack Morgenstein 	wr.num_sge = 1;
56337bfc7c1SJack Morgenstein 	wr.opcode = IB_WR_SEND;
56437bfc7c1SJack Morgenstein 	wr.send_flags = IB_SEND_SIGNALED;
56537bfc7c1SJack Morgenstein 
56637bfc7c1SJack Morgenstein 	ret = ib_post_send(src_qp, &wr, &bad_wr);
56737bfc7c1SJack Morgenstein out:
56837bfc7c1SJack Morgenstein 	if (ret)
56937bfc7c1SJack Morgenstein 		ib_destroy_ah(ah);
57037bfc7c1SJack Morgenstein 	return ret;
57137bfc7c1SJack Morgenstein }
57237bfc7c1SJack Morgenstein 
57337bfc7c1SJack Morgenstein static int mlx4_ib_demux_mad(struct ib_device *ibdev, u8 port,
57437bfc7c1SJack Morgenstein 			struct ib_wc *wc, struct ib_grh *grh,
57537bfc7c1SJack Morgenstein 			struct ib_mad *mad)
57637bfc7c1SJack Morgenstein {
57737bfc7c1SJack Morgenstein 	struct mlx4_ib_dev *dev = to_mdev(ibdev);
57837bfc7c1SJack Morgenstein 	int err;
57937bfc7c1SJack Morgenstein 	int slave;
58037bfc7c1SJack Morgenstein 	u8 *slave_id;
58137bfc7c1SJack Morgenstein 
58237bfc7c1SJack Morgenstein 	/* Initially assume that this mad is for us */
58337bfc7c1SJack Morgenstein 	slave = mlx4_master_func_num(dev->dev);
58437bfc7c1SJack Morgenstein 
58537bfc7c1SJack Morgenstein 	/* See if the slave id is encoded in a response mad */
58637bfc7c1SJack Morgenstein 	if (mad->mad_hdr.method & 0x80) {
58737bfc7c1SJack Morgenstein 		slave_id = (u8 *) &mad->mad_hdr.tid;
58837bfc7c1SJack Morgenstein 		slave = *slave_id;
58937bfc7c1SJack Morgenstein 		if (slave != 255) /*255 indicates the dom0*/
59037bfc7c1SJack Morgenstein 			*slave_id = 0; /* remap tid */
59137bfc7c1SJack Morgenstein 	}
59237bfc7c1SJack Morgenstein 
59337bfc7c1SJack Morgenstein 	/* If a grh is present, we demux according to it */
59437bfc7c1SJack Morgenstein 	if (wc->wc_flags & IB_WC_GRH) {
59537bfc7c1SJack Morgenstein 		slave = mlx4_ib_find_real_gid(ibdev, port, grh->dgid.global.interface_id);
59637bfc7c1SJack Morgenstein 		if (slave < 0) {
59737bfc7c1SJack Morgenstein 			mlx4_ib_warn(ibdev, "failed matching grh\n");
59837bfc7c1SJack Morgenstein 			return -ENOENT;
59937bfc7c1SJack Morgenstein 		}
60037bfc7c1SJack Morgenstein 	}
60137bfc7c1SJack Morgenstein 	/* Class-specific handling */
60237bfc7c1SJack Morgenstein 	switch (mad->mad_hdr.mgmt_class) {
60337bfc7c1SJack Morgenstein 	case IB_MGMT_CLASS_SUBN_ADM:
60437bfc7c1SJack Morgenstein 		if (mlx4_ib_demux_sa_handler(ibdev, port, slave,
60537bfc7c1SJack Morgenstein 					     (struct ib_sa_mad *) mad))
60637bfc7c1SJack Morgenstein 			return 0;
60737bfc7c1SJack Morgenstein 		break;
6083cf69cc8SAmir Vadai 	case IB_MGMT_CLASS_CM:
6093cf69cc8SAmir Vadai 		if (mlx4_ib_demux_cm_handler(ibdev, port, &slave, mad))
6103cf69cc8SAmir Vadai 			return 0;
6113cf69cc8SAmir Vadai 		break;
61237bfc7c1SJack Morgenstein 	case IB_MGMT_CLASS_DEVICE_MGMT:
61337bfc7c1SJack Morgenstein 		if (mad->mad_hdr.method != IB_MGMT_METHOD_GET_RESP)
61437bfc7c1SJack Morgenstein 			return 0;
61537bfc7c1SJack Morgenstein 		break;
61637bfc7c1SJack Morgenstein 	default:
61737bfc7c1SJack Morgenstein 		/* Drop unsupported classes for slaves in tunnel mode */
61837bfc7c1SJack Morgenstein 		if (slave != mlx4_master_func_num(dev->dev)) {
61937bfc7c1SJack Morgenstein 			pr_debug("dropping unsupported ingress mad from class:%d "
62037bfc7c1SJack Morgenstein 				 "for slave:%d\n", mad->mad_hdr.mgmt_class, slave);
62137bfc7c1SJack Morgenstein 			return 0;
62237bfc7c1SJack Morgenstein 		}
62337bfc7c1SJack Morgenstein 	}
62437bfc7c1SJack Morgenstein 	/*make sure that no slave==255 was not handled yet.*/
62537bfc7c1SJack Morgenstein 	if (slave >= dev->dev->caps.sqp_demux) {
62637bfc7c1SJack Morgenstein 		mlx4_ib_warn(ibdev, "slave id: %d is bigger than allowed:%d\n",
62737bfc7c1SJack Morgenstein 			     slave, dev->dev->caps.sqp_demux);
62837bfc7c1SJack Morgenstein 		return -ENOENT;
62937bfc7c1SJack Morgenstein 	}
63037bfc7c1SJack Morgenstein 
63137bfc7c1SJack Morgenstein 	err = mlx4_ib_send_to_slave(dev, slave, port, wc->qp->qp_type, wc, grh, mad);
63237bfc7c1SJack Morgenstein 	if (err)
63337bfc7c1SJack Morgenstein 		pr_debug("failed sending to slave %d via tunnel qp (%d)\n",
63437bfc7c1SJack Morgenstein 			 slave, err);
63537bfc7c1SJack Morgenstein 	return 0;
63637bfc7c1SJack Morgenstein }
63737bfc7c1SJack Morgenstein 
638c3779134SOr Gerlitz static int ib_process_mad(struct ib_device *ibdev, int mad_flags, u8 port_num,
639225c7b1fSRoland Dreier 			struct ib_wc *in_wc, struct ib_grh *in_grh,
640225c7b1fSRoland Dreier 			struct ib_mad *in_mad, struct ib_mad *out_mad)
641225c7b1fSRoland Dreier {
642f0f6f346SMoni Shoua 	u16 slid, prev_lid = 0;
643225c7b1fSRoland Dreier 	int err;
644f0f6f346SMoni Shoua 	struct ib_port_attr pattr;
645225c7b1fSRoland Dreier 
646b1d8eb5aSJack Morgenstein 	if (in_wc && in_wc->qp->qp_num) {
647b1d8eb5aSJack Morgenstein 		pr_debug("received MAD: slid:%d sqpn:%d "
648b1d8eb5aSJack Morgenstein 			"dlid_bits:%d dqpn:%d wc_flags:0x%x, cls %x, mtd %x, atr %x\n",
649b1d8eb5aSJack Morgenstein 			in_wc->slid, in_wc->src_qp,
650b1d8eb5aSJack Morgenstein 			in_wc->dlid_path_bits,
651b1d8eb5aSJack Morgenstein 			in_wc->qp->qp_num,
652b1d8eb5aSJack Morgenstein 			in_wc->wc_flags,
653b1d8eb5aSJack Morgenstein 			in_mad->mad_hdr.mgmt_class, in_mad->mad_hdr.method,
654b1d8eb5aSJack Morgenstein 			be16_to_cpu(in_mad->mad_hdr.attr_id));
655b1d8eb5aSJack Morgenstein 		if (in_wc->wc_flags & IB_WC_GRH) {
656b1d8eb5aSJack Morgenstein 			pr_debug("sgid_hi:0x%016llx sgid_lo:0x%016llx\n",
657b1d8eb5aSJack Morgenstein 				 be64_to_cpu(in_grh->sgid.global.subnet_prefix),
658b1d8eb5aSJack Morgenstein 				 be64_to_cpu(in_grh->sgid.global.interface_id));
659b1d8eb5aSJack Morgenstein 			pr_debug("dgid_hi:0x%016llx dgid_lo:0x%016llx\n",
660b1d8eb5aSJack Morgenstein 				 be64_to_cpu(in_grh->dgid.global.subnet_prefix),
661b1d8eb5aSJack Morgenstein 				 be64_to_cpu(in_grh->dgid.global.interface_id));
662b1d8eb5aSJack Morgenstein 		}
663b1d8eb5aSJack Morgenstein 	}
664b1d8eb5aSJack Morgenstein 
665225c7b1fSRoland Dreier 	slid = in_wc ? in_wc->slid : be16_to_cpu(IB_LID_PERMISSIVE);
666225c7b1fSRoland Dreier 
667225c7b1fSRoland Dreier 	if (in_mad->mad_hdr.method == IB_MGMT_METHOD_TRAP && slid == 0) {
668225c7b1fSRoland Dreier 		forward_trap(to_mdev(ibdev), port_num, in_mad);
669225c7b1fSRoland Dreier 		return IB_MAD_RESULT_SUCCESS | IB_MAD_RESULT_CONSUMED;
670225c7b1fSRoland Dreier 	}
671225c7b1fSRoland Dreier 
672225c7b1fSRoland Dreier 	if (in_mad->mad_hdr.mgmt_class == IB_MGMT_CLASS_SUBN_LID_ROUTED ||
673225c7b1fSRoland Dreier 	    in_mad->mad_hdr.mgmt_class == IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE) {
674225c7b1fSRoland Dreier 		if (in_mad->mad_hdr.method   != IB_MGMT_METHOD_GET &&
675225c7b1fSRoland Dreier 		    in_mad->mad_hdr.method   != IB_MGMT_METHOD_SET &&
676225c7b1fSRoland Dreier 		    in_mad->mad_hdr.method   != IB_MGMT_METHOD_TRAP_REPRESS)
677225c7b1fSRoland Dreier 			return IB_MAD_RESULT_SUCCESS;
678225c7b1fSRoland Dreier 
679225c7b1fSRoland Dreier 		/*
680a6f7feaeSJack Morgenstein 		 * Don't process SMInfo queries -- the SMA can't handle them.
681225c7b1fSRoland Dreier 		 */
682a6f7feaeSJack Morgenstein 		if (in_mad->mad_hdr.attr_id == IB_SMP_ATTR_SM_INFO)
683225c7b1fSRoland Dreier 			return IB_MAD_RESULT_SUCCESS;
684225c7b1fSRoland Dreier 	} else if (in_mad->mad_hdr.mgmt_class == IB_MGMT_CLASS_PERF_MGMT ||
685225c7b1fSRoland Dreier 		   in_mad->mad_hdr.mgmt_class == MLX4_IB_VENDOR_CLASS1   ||
6866578cf33SEli Cohen 		   in_mad->mad_hdr.mgmt_class == MLX4_IB_VENDOR_CLASS2   ||
6876578cf33SEli Cohen 		   in_mad->mad_hdr.mgmt_class == IB_MGMT_CLASS_CONG_MGMT) {
688225c7b1fSRoland Dreier 		if (in_mad->mad_hdr.method  != IB_MGMT_METHOD_GET &&
689225c7b1fSRoland Dreier 		    in_mad->mad_hdr.method  != IB_MGMT_METHOD_SET)
690225c7b1fSRoland Dreier 			return IB_MAD_RESULT_SUCCESS;
691225c7b1fSRoland Dreier 	} else
692225c7b1fSRoland Dreier 		return IB_MAD_RESULT_SUCCESS;
693225c7b1fSRoland Dreier 
694f0f6f346SMoni Shoua 	if ((in_mad->mad_hdr.mgmt_class == IB_MGMT_CLASS_SUBN_LID_ROUTED ||
695f0f6f346SMoni Shoua 	     in_mad->mad_hdr.mgmt_class == IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE) &&
696f0f6f346SMoni Shoua 	    in_mad->mad_hdr.method == IB_MGMT_METHOD_SET &&
697f0f6f346SMoni Shoua 	    in_mad->mad_hdr.attr_id == IB_SMP_ATTR_PORT_INFO &&
698f0f6f346SMoni Shoua 	    !ib_query_port(ibdev, port_num, &pattr))
699f0f6f346SMoni Shoua 		prev_lid = pattr.lid;
700f0f6f346SMoni Shoua 
701225c7b1fSRoland Dreier 	err = mlx4_MAD_IFC(to_mdev(ibdev),
7020a9a0188SJack Morgenstein 			   (mad_flags & IB_MAD_IGNORE_MKEY ? MLX4_MAD_IFC_IGNORE_MKEY : 0) |
7030a9a0188SJack Morgenstein 			   (mad_flags & IB_MAD_IGNORE_BKEY ? MLX4_MAD_IFC_IGNORE_BKEY : 0) |
7040a9a0188SJack Morgenstein 			   MLX4_MAD_IFC_NET_VIEW,
705225c7b1fSRoland Dreier 			   port_num, in_wc, in_grh, in_mad, out_mad);
706225c7b1fSRoland Dreier 	if (err)
707225c7b1fSRoland Dreier 		return IB_MAD_RESULT_FAILURE;
708225c7b1fSRoland Dreier 
709225c7b1fSRoland Dreier 	if (!out_mad->mad_hdr.status) {
71000f5ce99SJack Morgenstein 		if (!(to_mdev(ibdev)->dev->caps.flags & MLX4_DEV_CAP_FLAG_PORT_MNG_CHG_EV))
711f0f6f346SMoni Shoua 			smp_snoop(ibdev, port_num, in_mad, prev_lid);
712*992e8e6eSJack Morgenstein 		/* slaves get node desc from FW */
713*992e8e6eSJack Morgenstein 		if (!mlx4_is_slave(to_mdev(ibdev)->dev))
714225c7b1fSRoland Dreier 			node_desc_override(ibdev, out_mad);
715225c7b1fSRoland Dreier 	}
716225c7b1fSRoland Dreier 
717225c7b1fSRoland Dreier 	/* set return bit in status of directed route responses */
718225c7b1fSRoland Dreier 	if (in_mad->mad_hdr.mgmt_class == IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE)
719225c7b1fSRoland Dreier 		out_mad->mad_hdr.status |= cpu_to_be16(1 << 15);
720225c7b1fSRoland Dreier 
721225c7b1fSRoland Dreier 	if (in_mad->mad_hdr.method == IB_MGMT_METHOD_TRAP_REPRESS)
722225c7b1fSRoland Dreier 		/* no response for trap repress */
723225c7b1fSRoland Dreier 		return IB_MAD_RESULT_SUCCESS | IB_MAD_RESULT_CONSUMED;
724225c7b1fSRoland Dreier 
725225c7b1fSRoland Dreier 	return IB_MAD_RESULT_SUCCESS | IB_MAD_RESULT_REPLY;
726225c7b1fSRoland Dreier }
727225c7b1fSRoland Dreier 
728c3779134SOr Gerlitz static void edit_counter(struct mlx4_counter *cnt,
729c3779134SOr Gerlitz 					struct ib_pma_portcounters *pma_cnt)
730c3779134SOr Gerlitz {
731c3779134SOr Gerlitz 	pma_cnt->port_xmit_data = cpu_to_be32((be64_to_cpu(cnt->tx_bytes)>>2));
732c3779134SOr Gerlitz 	pma_cnt->port_rcv_data  = cpu_to_be32((be64_to_cpu(cnt->rx_bytes)>>2));
733c3779134SOr Gerlitz 	pma_cnt->port_xmit_packets = cpu_to_be32(be64_to_cpu(cnt->tx_frames));
734c3779134SOr Gerlitz 	pma_cnt->port_rcv_packets  = cpu_to_be32(be64_to_cpu(cnt->rx_frames));
735c3779134SOr Gerlitz }
736c3779134SOr Gerlitz 
737c3779134SOr Gerlitz static int iboe_process_mad(struct ib_device *ibdev, int mad_flags, u8 port_num,
738c3779134SOr Gerlitz 			struct ib_wc *in_wc, struct ib_grh *in_grh,
739c3779134SOr Gerlitz 			struct ib_mad *in_mad, struct ib_mad *out_mad)
740c3779134SOr Gerlitz {
741c3779134SOr Gerlitz 	struct mlx4_cmd_mailbox *mailbox;
742c3779134SOr Gerlitz 	struct mlx4_ib_dev *dev = to_mdev(ibdev);
743c3779134SOr Gerlitz 	int err;
744c3779134SOr Gerlitz 	u32 inmod = dev->counters[port_num - 1] & 0xffff;
745c3779134SOr Gerlitz 	u8 mode;
746c3779134SOr Gerlitz 
747c3779134SOr Gerlitz 	if (in_mad->mad_hdr.mgmt_class != IB_MGMT_CLASS_PERF_MGMT)
748c3779134SOr Gerlitz 		return -EINVAL;
749c3779134SOr Gerlitz 
750c3779134SOr Gerlitz 	mailbox = mlx4_alloc_cmd_mailbox(dev->dev);
751c3779134SOr Gerlitz 	if (IS_ERR(mailbox))
752c3779134SOr Gerlitz 		return IB_MAD_RESULT_FAILURE;
753c3779134SOr Gerlitz 
754c3779134SOr Gerlitz 	err = mlx4_cmd_box(dev->dev, 0, mailbox->dma, inmod, 0,
755f9baff50SJack Morgenstein 			   MLX4_CMD_QUERY_IF_STAT, MLX4_CMD_TIME_CLASS_C,
756f9baff50SJack Morgenstein 			   MLX4_CMD_WRAPPED);
757c3779134SOr Gerlitz 	if (err)
758c3779134SOr Gerlitz 		err = IB_MAD_RESULT_FAILURE;
759c3779134SOr Gerlitz 	else {
760c3779134SOr Gerlitz 		memset(out_mad->data, 0, sizeof out_mad->data);
761c3779134SOr Gerlitz 		mode = ((struct mlx4_counter *)mailbox->buf)->counter_mode;
762c3779134SOr Gerlitz 		switch (mode & 0xf) {
763c3779134SOr Gerlitz 		case 0:
764c3779134SOr Gerlitz 			edit_counter(mailbox->buf,
765c3779134SOr Gerlitz 						(void *)(out_mad->data + 40));
766c3779134SOr Gerlitz 			err = IB_MAD_RESULT_SUCCESS | IB_MAD_RESULT_REPLY;
767c3779134SOr Gerlitz 			break;
768c3779134SOr Gerlitz 		default:
769c3779134SOr Gerlitz 			err = IB_MAD_RESULT_FAILURE;
770c3779134SOr Gerlitz 		}
771c3779134SOr Gerlitz 	}
772c3779134SOr Gerlitz 
773c3779134SOr Gerlitz 	mlx4_free_cmd_mailbox(dev->dev, mailbox);
774c3779134SOr Gerlitz 
775c3779134SOr Gerlitz 	return err;
776c3779134SOr Gerlitz }
777c3779134SOr Gerlitz 
778c3779134SOr Gerlitz int mlx4_ib_process_mad(struct ib_device *ibdev, int mad_flags, u8 port_num,
779c3779134SOr Gerlitz 			struct ib_wc *in_wc, struct ib_grh *in_grh,
780c3779134SOr Gerlitz 			struct ib_mad *in_mad, struct ib_mad *out_mad)
781c3779134SOr Gerlitz {
782c3779134SOr Gerlitz 	switch (rdma_port_get_link_layer(ibdev, port_num)) {
783c3779134SOr Gerlitz 	case IB_LINK_LAYER_INFINIBAND:
784c3779134SOr Gerlitz 		return ib_process_mad(ibdev, mad_flags, port_num, in_wc,
785c3779134SOr Gerlitz 				      in_grh, in_mad, out_mad);
786c3779134SOr Gerlitz 	case IB_LINK_LAYER_ETHERNET:
787c3779134SOr Gerlitz 		return iboe_process_mad(ibdev, mad_flags, port_num, in_wc,
788c3779134SOr Gerlitz 					  in_grh, in_mad, out_mad);
789c3779134SOr Gerlitz 	default:
790c3779134SOr Gerlitz 		return -EINVAL;
791c3779134SOr Gerlitz 	}
792c3779134SOr Gerlitz }
793c3779134SOr Gerlitz 
794225c7b1fSRoland Dreier static void send_handler(struct ib_mad_agent *agent,
795225c7b1fSRoland Dreier 			 struct ib_mad_send_wc *mad_send_wc)
796225c7b1fSRoland Dreier {
797*992e8e6eSJack Morgenstein 	if (mad_send_wc->send_buf->context[0])
798*992e8e6eSJack Morgenstein 		ib_destroy_ah(mad_send_wc->send_buf->context[0]);
799225c7b1fSRoland Dreier 	ib_free_send_mad(mad_send_wc->send_buf);
800225c7b1fSRoland Dreier }
801225c7b1fSRoland Dreier 
802225c7b1fSRoland Dreier int mlx4_ib_mad_init(struct mlx4_ib_dev *dev)
803225c7b1fSRoland Dreier {
804225c7b1fSRoland Dreier 	struct ib_mad_agent *agent;
805225c7b1fSRoland Dreier 	int p, q;
806225c7b1fSRoland Dreier 	int ret;
807fa417f7bSEli Cohen 	enum rdma_link_layer ll;
808225c7b1fSRoland Dreier 
809fa417f7bSEli Cohen 	for (p = 0; p < dev->num_ports; ++p) {
810fa417f7bSEli Cohen 		ll = rdma_port_get_link_layer(&dev->ib_dev, p + 1);
811225c7b1fSRoland Dreier 		for (q = 0; q <= 1; ++q) {
812fa417f7bSEli Cohen 			if (ll == IB_LINK_LAYER_INFINIBAND) {
813225c7b1fSRoland Dreier 				agent = ib_register_mad_agent(&dev->ib_dev, p + 1,
814225c7b1fSRoland Dreier 							      q ? IB_QPT_GSI : IB_QPT_SMI,
815225c7b1fSRoland Dreier 							      NULL, 0, send_handler,
816225c7b1fSRoland Dreier 							      NULL, NULL);
817225c7b1fSRoland Dreier 				if (IS_ERR(agent)) {
818225c7b1fSRoland Dreier 					ret = PTR_ERR(agent);
819225c7b1fSRoland Dreier 					goto err;
820225c7b1fSRoland Dreier 				}
821225c7b1fSRoland Dreier 				dev->send_agent[p][q] = agent;
822fa417f7bSEli Cohen 			} else
823fa417f7bSEli Cohen 				dev->send_agent[p][q] = NULL;
824fa417f7bSEli Cohen 		}
825225c7b1fSRoland Dreier 	}
826225c7b1fSRoland Dreier 
827225c7b1fSRoland Dreier 	return 0;
828225c7b1fSRoland Dreier 
829225c7b1fSRoland Dreier err:
8307ff93f8bSYevgeny Petrilin 	for (p = 0; p < dev->num_ports; ++p)
831225c7b1fSRoland Dreier 		for (q = 0; q <= 1; ++q)
832225c7b1fSRoland Dreier 			if (dev->send_agent[p][q])
833225c7b1fSRoland Dreier 				ib_unregister_mad_agent(dev->send_agent[p][q]);
834225c7b1fSRoland Dreier 
835225c7b1fSRoland Dreier 	return ret;
836225c7b1fSRoland Dreier }
837225c7b1fSRoland Dreier 
838225c7b1fSRoland Dreier void mlx4_ib_mad_cleanup(struct mlx4_ib_dev *dev)
839225c7b1fSRoland Dreier {
840225c7b1fSRoland Dreier 	struct ib_mad_agent *agent;
841225c7b1fSRoland Dreier 	int p, q;
842225c7b1fSRoland Dreier 
8437ff93f8bSYevgeny Petrilin 	for (p = 0; p < dev->num_ports; ++p) {
844225c7b1fSRoland Dreier 		for (q = 0; q <= 1; ++q) {
845225c7b1fSRoland Dreier 			agent = dev->send_agent[p][q];
846fa417f7bSEli Cohen 			if (agent) {
847225c7b1fSRoland Dreier 				dev->send_agent[p][q] = NULL;
848225c7b1fSRoland Dreier 				ib_unregister_mad_agent(agent);
849225c7b1fSRoland Dreier 			}
850fa417f7bSEli Cohen 		}
851225c7b1fSRoland Dreier 
852225c7b1fSRoland Dreier 		if (dev->sm_ah[p])
853225c7b1fSRoland Dreier 			ib_destroy_ah(dev->sm_ah[p]);
854225c7b1fSRoland Dreier 	}
855225c7b1fSRoland Dreier }
85600f5ce99SJack Morgenstein 
8572a4fae14SJack Morgenstein static void handle_lid_change_event(struct mlx4_ib_dev *dev, u8 port_num)
8582a4fae14SJack Morgenstein {
8592a4fae14SJack Morgenstein 	mlx4_ib_dispatch_event(dev, port_num, IB_EVENT_LID_CHANGE);
8602a4fae14SJack Morgenstein 
8612a4fae14SJack Morgenstein 	if (mlx4_is_master(dev->dev) && !dev->sriov.is_going_down)
8622a4fae14SJack Morgenstein 		mlx4_gen_slaves_port_mgt_ev(dev->dev, port_num,
8632a4fae14SJack Morgenstein 					    MLX4_EQ_PORT_INFO_LID_CHANGE_MASK);
8642a4fae14SJack Morgenstein }
8652a4fae14SJack Morgenstein 
866b9c5d6a6SOren Duer static void handle_client_rereg_event(struct mlx4_ib_dev *dev, u8 port_num)
867b9c5d6a6SOren Duer {
868a0c64a17SJack Morgenstein 	/* re-configure the alias-guid and mcg's */
869b9c5d6a6SOren Duer 	if (mlx4_is_master(dev->dev)) {
870a0c64a17SJack Morgenstein 		mlx4_ib_invalidate_all_guid_record(dev, port_num);
871a0c64a17SJack Morgenstein 
8722a4fae14SJack Morgenstein 		if (!dev->sriov.is_going_down) {
873b9c5d6a6SOren Duer 			mlx4_ib_mcg_port_cleanup(&dev->sriov.demux[port_num - 1], 0);
8742a4fae14SJack Morgenstein 			mlx4_gen_slaves_port_mgt_ev(dev->dev, port_num,
8752a4fae14SJack Morgenstein 						    MLX4_EQ_PORT_INFO_CLIENT_REREG_MASK);
8762a4fae14SJack Morgenstein 		}
877b9c5d6a6SOren Duer 	}
878b9c5d6a6SOren Duer 	mlx4_ib_dispatch_event(dev, port_num, IB_EVENT_CLIENT_REREGISTER);
879b9c5d6a6SOren Duer }
880b9c5d6a6SOren Duer 
8812a4fae14SJack Morgenstein static void propagate_pkey_ev(struct mlx4_ib_dev *dev, int port_num,
8822a4fae14SJack Morgenstein 			      struct mlx4_eqe *eqe)
8832a4fae14SJack Morgenstein {
8842a4fae14SJack Morgenstein 	__propagate_pkey_ev(dev, port_num, GET_BLK_PTR_FROM_EQE(eqe),
8852a4fae14SJack Morgenstein 			    GET_MASK_FROM_EQE(eqe));
8862a4fae14SJack Morgenstein }
8872a4fae14SJack Morgenstein 
8882a4fae14SJack Morgenstein static void handle_slaves_guid_change(struct mlx4_ib_dev *dev, u8 port_num,
8892a4fae14SJack Morgenstein 				      u32 guid_tbl_blk_num, u32 change_bitmap)
8902a4fae14SJack Morgenstein {
8912a4fae14SJack Morgenstein 	struct ib_smp *in_mad  = NULL;
8922a4fae14SJack Morgenstein 	struct ib_smp *out_mad  = NULL;
8932a4fae14SJack Morgenstein 	u16 i;
8942a4fae14SJack Morgenstein 
8952a4fae14SJack Morgenstein 	if (!mlx4_is_mfunc(dev->dev) || !mlx4_is_master(dev->dev))
8962a4fae14SJack Morgenstein 		return;
8972a4fae14SJack Morgenstein 
8982a4fae14SJack Morgenstein 	in_mad  = kmalloc(sizeof *in_mad, GFP_KERNEL);
8992a4fae14SJack Morgenstein 	out_mad = kmalloc(sizeof *out_mad, GFP_KERNEL);
9002a4fae14SJack Morgenstein 	if (!in_mad || !out_mad) {
9012a4fae14SJack Morgenstein 		mlx4_ib_warn(&dev->ib_dev, "failed to allocate memory for guid info mads\n");
9022a4fae14SJack Morgenstein 		goto out;
9032a4fae14SJack Morgenstein 	}
9042a4fae14SJack Morgenstein 
9052a4fae14SJack Morgenstein 	guid_tbl_blk_num  *= 4;
9062a4fae14SJack Morgenstein 
9072a4fae14SJack Morgenstein 	for (i = 0; i < 4; i++) {
9082a4fae14SJack Morgenstein 		if (change_bitmap && (!((change_bitmap >> (8 * i)) & 0xff)))
9092a4fae14SJack Morgenstein 			continue;
9102a4fae14SJack Morgenstein 		memset(in_mad, 0, sizeof *in_mad);
9112a4fae14SJack Morgenstein 		memset(out_mad, 0, sizeof *out_mad);
9122a4fae14SJack Morgenstein 
9132a4fae14SJack Morgenstein 		in_mad->base_version  = 1;
9142a4fae14SJack Morgenstein 		in_mad->mgmt_class    = IB_MGMT_CLASS_SUBN_LID_ROUTED;
9152a4fae14SJack Morgenstein 		in_mad->class_version = 1;
9162a4fae14SJack Morgenstein 		in_mad->method        = IB_MGMT_METHOD_GET;
9172a4fae14SJack Morgenstein 		in_mad->attr_id       = IB_SMP_ATTR_GUID_INFO;
9182a4fae14SJack Morgenstein 		in_mad->attr_mod      = cpu_to_be32(guid_tbl_blk_num + i);
9192a4fae14SJack Morgenstein 
9202a4fae14SJack Morgenstein 		if (mlx4_MAD_IFC(dev,
9212a4fae14SJack Morgenstein 				 MLX4_MAD_IFC_IGNORE_KEYS | MLX4_MAD_IFC_NET_VIEW,
9222a4fae14SJack Morgenstein 				 port_num, NULL, NULL, in_mad, out_mad)) {
9232a4fae14SJack Morgenstein 			mlx4_ib_warn(&dev->ib_dev, "Failed in get GUID INFO MAD_IFC\n");
9242a4fae14SJack Morgenstein 			goto out;
9252a4fae14SJack Morgenstein 		}
9262a4fae14SJack Morgenstein 
9272a4fae14SJack Morgenstein 		mlx4_ib_update_cache_on_guid_change(dev, guid_tbl_blk_num + i,
9282a4fae14SJack Morgenstein 						    port_num,
9292a4fae14SJack Morgenstein 						    (u8 *)(&((struct ib_smp *)out_mad)->data));
9302a4fae14SJack Morgenstein 		mlx4_ib_notify_slaves_on_guid_change(dev, guid_tbl_blk_num + i,
9312a4fae14SJack Morgenstein 						     port_num,
9322a4fae14SJack Morgenstein 						     (u8 *)(&((struct ib_smp *)out_mad)->data));
9332a4fae14SJack Morgenstein 	}
9342a4fae14SJack Morgenstein 
9352a4fae14SJack Morgenstein out:
9362a4fae14SJack Morgenstein 	kfree(in_mad);
9372a4fae14SJack Morgenstein 	kfree(out_mad);
9382a4fae14SJack Morgenstein 	return;
9392a4fae14SJack Morgenstein }
9402a4fae14SJack Morgenstein 
94100f5ce99SJack Morgenstein void handle_port_mgmt_change_event(struct work_struct *work)
94200f5ce99SJack Morgenstein {
94300f5ce99SJack Morgenstein 	struct ib_event_work *ew = container_of(work, struct ib_event_work, work);
94400f5ce99SJack Morgenstein 	struct mlx4_ib_dev *dev = ew->ib_dev;
94500f5ce99SJack Morgenstein 	struct mlx4_eqe *eqe = &(ew->ib_eqe);
94600f5ce99SJack Morgenstein 	u8 port = eqe->event.port_mgmt_change.port;
94700f5ce99SJack Morgenstein 	u32 changed_attr;
9482a4fae14SJack Morgenstein 	u32 tbl_block;
9492a4fae14SJack Morgenstein 	u32 change_bitmap;
95000f5ce99SJack Morgenstein 
95100f5ce99SJack Morgenstein 	switch (eqe->subtype) {
95200f5ce99SJack Morgenstein 	case MLX4_DEV_PMC_SUBTYPE_PORT_INFO:
95300f5ce99SJack Morgenstein 		changed_attr = be32_to_cpu(eqe->event.port_mgmt_change.params.port_info.changed_attr);
95400f5ce99SJack Morgenstein 
95500f5ce99SJack Morgenstein 		/* Update the SM ah - This should be done before handling
95600f5ce99SJack Morgenstein 		   the other changed attributes so that MADs can be sent to the SM */
95700f5ce99SJack Morgenstein 		if (changed_attr & MSTR_SM_CHANGE_MASK) {
95800f5ce99SJack Morgenstein 			u16 lid = be16_to_cpu(eqe->event.port_mgmt_change.params.port_info.mstr_sm_lid);
95900f5ce99SJack Morgenstein 			u8 sl = eqe->event.port_mgmt_change.params.port_info.mstr_sm_sl & 0xf;
96000f5ce99SJack Morgenstein 			update_sm_ah(dev, port, lid, sl);
96100f5ce99SJack Morgenstein 		}
96200f5ce99SJack Morgenstein 
96300f5ce99SJack Morgenstein 		/* Check if it is a lid change event */
96400f5ce99SJack Morgenstein 		if (changed_attr & MLX4_EQ_PORT_INFO_LID_CHANGE_MASK)
9652a4fae14SJack Morgenstein 			handle_lid_change_event(dev, port);
96600f5ce99SJack Morgenstein 
96700f5ce99SJack Morgenstein 		/* Generate GUID changed event */
9682a4fae14SJack Morgenstein 		if (changed_attr & MLX4_EQ_PORT_INFO_GID_PFX_CHANGE_MASK) {
96900f5ce99SJack Morgenstein 			mlx4_ib_dispatch_event(dev, port, IB_EVENT_GID_CHANGE);
9702a4fae14SJack Morgenstein 			/*if master, notify all slaves*/
9712a4fae14SJack Morgenstein 			if (mlx4_is_master(dev->dev))
9722a4fae14SJack Morgenstein 				mlx4_gen_slaves_port_mgt_ev(dev->dev, port,
9732a4fae14SJack Morgenstein 							    MLX4_EQ_PORT_INFO_GID_PFX_CHANGE_MASK);
9742a4fae14SJack Morgenstein 		}
97500f5ce99SJack Morgenstein 
97600f5ce99SJack Morgenstein 		if (changed_attr & MLX4_EQ_PORT_INFO_CLIENT_REREG_MASK)
977b9c5d6a6SOren Duer 			handle_client_rereg_event(dev, port);
97800f5ce99SJack Morgenstein 		break;
97900f5ce99SJack Morgenstein 
98000f5ce99SJack Morgenstein 	case MLX4_DEV_PMC_SUBTYPE_PKEY_TABLE:
98100f5ce99SJack Morgenstein 		mlx4_ib_dispatch_event(dev, port, IB_EVENT_PKEY_CHANGE);
9822a4fae14SJack Morgenstein 		if (mlx4_is_master(dev->dev) && !dev->sriov.is_going_down)
9832a4fae14SJack Morgenstein 			propagate_pkey_ev(dev, port, eqe);
98400f5ce99SJack Morgenstein 		break;
98500f5ce99SJack Morgenstein 	case MLX4_DEV_PMC_SUBTYPE_GUID_INFO:
9866634961cSJack Morgenstein 		/* paravirtualized master's guid is guid 0 -- does not change */
9876634961cSJack Morgenstein 		if (!mlx4_is_master(dev->dev))
98800f5ce99SJack Morgenstein 			mlx4_ib_dispatch_event(dev, port, IB_EVENT_GID_CHANGE);
9892a4fae14SJack Morgenstein 		/*if master, notify relevant slaves*/
9902a4fae14SJack Morgenstein 		else if (!dev->sriov.is_going_down) {
9912a4fae14SJack Morgenstein 			tbl_block = GET_BLK_PTR_FROM_EQE(eqe);
9922a4fae14SJack Morgenstein 			change_bitmap = GET_MASK_FROM_EQE(eqe);
9932a4fae14SJack Morgenstein 			handle_slaves_guid_change(dev, port, tbl_block, change_bitmap);
9942a4fae14SJack Morgenstein 		}
99500f5ce99SJack Morgenstein 		break;
99600f5ce99SJack Morgenstein 	default:
99700f5ce99SJack Morgenstein 		pr_warn("Unsupported subtype 0x%x for "
99800f5ce99SJack Morgenstein 			"Port Management Change event\n", eqe->subtype);
99900f5ce99SJack Morgenstein 	}
100000f5ce99SJack Morgenstein 
100100f5ce99SJack Morgenstein 	kfree(ew);
100200f5ce99SJack Morgenstein }
100300f5ce99SJack Morgenstein 
100400f5ce99SJack Morgenstein void mlx4_ib_dispatch_event(struct mlx4_ib_dev *dev, u8 port_num,
100500f5ce99SJack Morgenstein 			    enum ib_event_type type)
100600f5ce99SJack Morgenstein {
100700f5ce99SJack Morgenstein 	struct ib_event event;
100800f5ce99SJack Morgenstein 
100900f5ce99SJack Morgenstein 	event.device		= &dev->ib_dev;
101000f5ce99SJack Morgenstein 	event.element.port_num	= port_num;
101100f5ce99SJack Morgenstein 	event.event		= type;
101200f5ce99SJack Morgenstein 
101300f5ce99SJack Morgenstein 	ib_dispatch_event(&event);
101400f5ce99SJack Morgenstein }
1015fc06573dSJack Morgenstein 
1016fc06573dSJack Morgenstein static void mlx4_ib_tunnel_comp_handler(struct ib_cq *cq, void *arg)
1017fc06573dSJack Morgenstein {
1018fc06573dSJack Morgenstein 	unsigned long flags;
1019fc06573dSJack Morgenstein 	struct mlx4_ib_demux_pv_ctx *ctx = cq->cq_context;
1020fc06573dSJack Morgenstein 	struct mlx4_ib_dev *dev = to_mdev(ctx->ib_dev);
1021fc06573dSJack Morgenstein 	spin_lock_irqsave(&dev->sriov.going_down_lock, flags);
1022fc06573dSJack Morgenstein 	if (!dev->sriov.is_going_down && ctx->state == DEMUX_PV_STATE_ACTIVE)
1023fc06573dSJack Morgenstein 		queue_work(ctx->wq, &ctx->work);
1024fc06573dSJack Morgenstein 	spin_unlock_irqrestore(&dev->sriov.going_down_lock, flags);
1025fc06573dSJack Morgenstein }
1026fc06573dSJack Morgenstein 
1027fc06573dSJack Morgenstein static int mlx4_ib_post_pv_qp_buf(struct mlx4_ib_demux_pv_ctx *ctx,
1028fc06573dSJack Morgenstein 				  struct mlx4_ib_demux_pv_qp *tun_qp,
1029fc06573dSJack Morgenstein 				  int index)
1030fc06573dSJack Morgenstein {
1031fc06573dSJack Morgenstein 	struct ib_sge sg_list;
1032fc06573dSJack Morgenstein 	struct ib_recv_wr recv_wr, *bad_recv_wr;
1033fc06573dSJack Morgenstein 	int size;
1034fc06573dSJack Morgenstein 
1035fc06573dSJack Morgenstein 	size = (tun_qp->qp->qp_type == IB_QPT_UD) ?
1036fc06573dSJack Morgenstein 		sizeof (struct mlx4_tunnel_mad) : sizeof (struct mlx4_mad_rcv_buf);
1037fc06573dSJack Morgenstein 
1038fc06573dSJack Morgenstein 	sg_list.addr = tun_qp->ring[index].map;
1039fc06573dSJack Morgenstein 	sg_list.length = size;
1040fc06573dSJack Morgenstein 	sg_list.lkey = ctx->mr->lkey;
1041fc06573dSJack Morgenstein 
1042fc06573dSJack Morgenstein 	recv_wr.next = NULL;
1043fc06573dSJack Morgenstein 	recv_wr.sg_list = &sg_list;
1044fc06573dSJack Morgenstein 	recv_wr.num_sge = 1;
1045fc06573dSJack Morgenstein 	recv_wr.wr_id = (u64) index | MLX4_TUN_WRID_RECV |
1046fc06573dSJack Morgenstein 		MLX4_TUN_SET_WRID_QPN(tun_qp->proxy_qpt);
1047fc06573dSJack Morgenstein 	ib_dma_sync_single_for_device(ctx->ib_dev, tun_qp->ring[index].map,
1048fc06573dSJack Morgenstein 				      size, DMA_FROM_DEVICE);
1049fc06573dSJack Morgenstein 	return ib_post_recv(tun_qp->qp, &recv_wr, &bad_recv_wr);
1050fc06573dSJack Morgenstein }
1051fc06573dSJack Morgenstein 
105237bfc7c1SJack Morgenstein static int mlx4_ib_multiplex_sa_handler(struct ib_device *ibdev, int port,
105337bfc7c1SJack Morgenstein 		int slave, struct ib_sa_mad *sa_mad)
105437bfc7c1SJack Morgenstein {
1055b9c5d6a6SOren Duer 	int ret = 0;
1056b9c5d6a6SOren Duer 
1057b9c5d6a6SOren Duer 	/* dispatch to different sa handlers */
1058b9c5d6a6SOren Duer 	switch (be16_to_cpu(sa_mad->mad_hdr.attr_id)) {
1059b9c5d6a6SOren Duer 	case IB_SA_ATTR_MC_MEMBER_REC:
1060b9c5d6a6SOren Duer 		ret = mlx4_ib_mcg_multiplex_handler(ibdev, port, slave, sa_mad);
1061b9c5d6a6SOren Duer 		break;
1062b9c5d6a6SOren Duer 	default:
1063b9c5d6a6SOren Duer 		break;
1064b9c5d6a6SOren Duer 	}
1065b9c5d6a6SOren Duer 	return ret;
106637bfc7c1SJack Morgenstein }
106737bfc7c1SJack Morgenstein 
106837bfc7c1SJack Morgenstein static int is_proxy_qp0(struct mlx4_ib_dev *dev, int qpn, int slave)
106937bfc7c1SJack Morgenstein {
107037bfc7c1SJack Morgenstein 	int slave_start = dev->dev->caps.sqp_start + 8 * slave;
107137bfc7c1SJack Morgenstein 
107237bfc7c1SJack Morgenstein 	return (qpn >= slave_start && qpn <= slave_start + 1);
107337bfc7c1SJack Morgenstein }
107437bfc7c1SJack Morgenstein 
107537bfc7c1SJack Morgenstein 
107637bfc7c1SJack Morgenstein int mlx4_ib_send_to_wire(struct mlx4_ib_dev *dev, int slave, u8 port,
107737bfc7c1SJack Morgenstein 			 enum ib_qp_type dest_qpt, u16 pkey_index, u32 remote_qpn,
107837bfc7c1SJack Morgenstein 			 u32 qkey, struct ib_ah_attr *attr, struct ib_mad *mad)
107937bfc7c1SJack Morgenstein {
108037bfc7c1SJack Morgenstein 	struct ib_sge list;
108137bfc7c1SJack Morgenstein 	struct ib_send_wr wr, *bad_wr;
108237bfc7c1SJack Morgenstein 	struct mlx4_ib_demux_pv_ctx *sqp_ctx;
108337bfc7c1SJack Morgenstein 	struct mlx4_ib_demux_pv_qp *sqp;
108437bfc7c1SJack Morgenstein 	struct mlx4_mad_snd_buf *sqp_mad;
108537bfc7c1SJack Morgenstein 	struct ib_ah *ah;
108637bfc7c1SJack Morgenstein 	struct ib_qp *send_qp = NULL;
108737bfc7c1SJack Morgenstein 	unsigned wire_tx_ix = 0;
108837bfc7c1SJack Morgenstein 	int ret = 0;
108937bfc7c1SJack Morgenstein 	u16 wire_pkey_ix;
109037bfc7c1SJack Morgenstein 	int src_qpnum;
109137bfc7c1SJack Morgenstein 	u8 sgid_index;
109237bfc7c1SJack Morgenstein 
109337bfc7c1SJack Morgenstein 
109437bfc7c1SJack Morgenstein 	sqp_ctx = dev->sriov.sqps[port-1];
109537bfc7c1SJack Morgenstein 
109637bfc7c1SJack Morgenstein 	/* check if proxy qp created */
109737bfc7c1SJack Morgenstein 	if (!sqp_ctx || sqp_ctx->state != DEMUX_PV_STATE_ACTIVE)
109837bfc7c1SJack Morgenstein 		return -EAGAIN;
109937bfc7c1SJack Morgenstein 
110037bfc7c1SJack Morgenstein 	/* QP0 forwarding only for Dom0 */
110137bfc7c1SJack Morgenstein 	if (dest_qpt == IB_QPT_SMI && (mlx4_master_func_num(dev->dev) != slave))
110237bfc7c1SJack Morgenstein 		return -EINVAL;
110337bfc7c1SJack Morgenstein 
110437bfc7c1SJack Morgenstein 	if (dest_qpt == IB_QPT_SMI) {
110537bfc7c1SJack Morgenstein 		src_qpnum = 0;
110637bfc7c1SJack Morgenstein 		sqp = &sqp_ctx->qp[0];
110737bfc7c1SJack Morgenstein 		wire_pkey_ix = dev->pkeys.virt2phys_pkey[slave][port - 1][0];
110837bfc7c1SJack Morgenstein 	} else {
110937bfc7c1SJack Morgenstein 		src_qpnum = 1;
111037bfc7c1SJack Morgenstein 		sqp = &sqp_ctx->qp[1];
111137bfc7c1SJack Morgenstein 		wire_pkey_ix = dev->pkeys.virt2phys_pkey[slave][port - 1][pkey_index];
111237bfc7c1SJack Morgenstein 	}
111337bfc7c1SJack Morgenstein 
111437bfc7c1SJack Morgenstein 	send_qp = sqp->qp;
111537bfc7c1SJack Morgenstein 
111637bfc7c1SJack Morgenstein 	/* create ah */
111737bfc7c1SJack Morgenstein 	sgid_index = attr->grh.sgid_index;
111837bfc7c1SJack Morgenstein 	attr->grh.sgid_index = 0;
111937bfc7c1SJack Morgenstein 	ah = ib_create_ah(sqp_ctx->pd, attr);
112037bfc7c1SJack Morgenstein 	if (IS_ERR(ah))
112137bfc7c1SJack Morgenstein 		return -ENOMEM;
112237bfc7c1SJack Morgenstein 	attr->grh.sgid_index = sgid_index;
112337bfc7c1SJack Morgenstein 	to_mah(ah)->av.ib.gid_index = sgid_index;
112437bfc7c1SJack Morgenstein 	/* get rid of force-loopback bit */
112537bfc7c1SJack Morgenstein 	to_mah(ah)->av.ib.port_pd &= cpu_to_be32(0x7FFFFFFF);
112637bfc7c1SJack Morgenstein 	spin_lock(&sqp->tx_lock);
112737bfc7c1SJack Morgenstein 	if (sqp->tx_ix_head - sqp->tx_ix_tail >=
112837bfc7c1SJack Morgenstein 	    (MLX4_NUM_TUNNEL_BUFS - 1))
112937bfc7c1SJack Morgenstein 		ret = -EAGAIN;
113037bfc7c1SJack Morgenstein 	else
113137bfc7c1SJack Morgenstein 		wire_tx_ix = (++sqp->tx_ix_head) & (MLX4_NUM_TUNNEL_BUFS - 1);
113237bfc7c1SJack Morgenstein 	spin_unlock(&sqp->tx_lock);
113337bfc7c1SJack Morgenstein 	if (ret)
113437bfc7c1SJack Morgenstein 		goto out;
113537bfc7c1SJack Morgenstein 
113637bfc7c1SJack Morgenstein 	sqp_mad = (struct mlx4_mad_snd_buf *) (sqp->tx_ring[wire_tx_ix].buf.addr);
113737bfc7c1SJack Morgenstein 	if (sqp->tx_ring[wire_tx_ix].ah)
113837bfc7c1SJack Morgenstein 		ib_destroy_ah(sqp->tx_ring[wire_tx_ix].ah);
113937bfc7c1SJack Morgenstein 	sqp->tx_ring[wire_tx_ix].ah = ah;
114037bfc7c1SJack Morgenstein 	ib_dma_sync_single_for_cpu(&dev->ib_dev,
114137bfc7c1SJack Morgenstein 				   sqp->tx_ring[wire_tx_ix].buf.map,
114237bfc7c1SJack Morgenstein 				   sizeof (struct mlx4_mad_snd_buf),
114337bfc7c1SJack Morgenstein 				   DMA_TO_DEVICE);
114437bfc7c1SJack Morgenstein 
114537bfc7c1SJack Morgenstein 	memcpy(&sqp_mad->payload, mad, sizeof *mad);
114637bfc7c1SJack Morgenstein 
114737bfc7c1SJack Morgenstein 	ib_dma_sync_single_for_device(&dev->ib_dev,
114837bfc7c1SJack Morgenstein 				      sqp->tx_ring[wire_tx_ix].buf.map,
114937bfc7c1SJack Morgenstein 				      sizeof (struct mlx4_mad_snd_buf),
115037bfc7c1SJack Morgenstein 				      DMA_TO_DEVICE);
115137bfc7c1SJack Morgenstein 
115237bfc7c1SJack Morgenstein 	list.addr = sqp->tx_ring[wire_tx_ix].buf.map;
115337bfc7c1SJack Morgenstein 	list.length = sizeof (struct mlx4_mad_snd_buf);
115437bfc7c1SJack Morgenstein 	list.lkey = sqp_ctx->mr->lkey;
115537bfc7c1SJack Morgenstein 
115637bfc7c1SJack Morgenstein 	wr.wr.ud.ah = ah;
115737bfc7c1SJack Morgenstein 	wr.wr.ud.port_num = port;
115837bfc7c1SJack Morgenstein 	wr.wr.ud.pkey_index = wire_pkey_ix;
115937bfc7c1SJack Morgenstein 	wr.wr.ud.remote_qkey = qkey;
116037bfc7c1SJack Morgenstein 	wr.wr.ud.remote_qpn = remote_qpn;
116137bfc7c1SJack Morgenstein 	wr.next = NULL;
116237bfc7c1SJack Morgenstein 	wr.wr_id = ((u64) wire_tx_ix) | MLX4_TUN_SET_WRID_QPN(src_qpnum);
116337bfc7c1SJack Morgenstein 	wr.sg_list = &list;
116437bfc7c1SJack Morgenstein 	wr.num_sge = 1;
116537bfc7c1SJack Morgenstein 	wr.opcode = IB_WR_SEND;
116637bfc7c1SJack Morgenstein 	wr.send_flags = IB_SEND_SIGNALED;
116737bfc7c1SJack Morgenstein 
116837bfc7c1SJack Morgenstein 	ret = ib_post_send(send_qp, &wr, &bad_wr);
116937bfc7c1SJack Morgenstein out:
117037bfc7c1SJack Morgenstein 	if (ret)
117137bfc7c1SJack Morgenstein 		ib_destroy_ah(ah);
117237bfc7c1SJack Morgenstein 	return ret;
117337bfc7c1SJack Morgenstein }
117437bfc7c1SJack Morgenstein 
117537bfc7c1SJack Morgenstein static void mlx4_ib_multiplex_mad(struct mlx4_ib_demux_pv_ctx *ctx, struct ib_wc *wc)
117637bfc7c1SJack Morgenstein {
117737bfc7c1SJack Morgenstein 	struct mlx4_ib_dev *dev = to_mdev(ctx->ib_dev);
117837bfc7c1SJack Morgenstein 	struct mlx4_ib_demux_pv_qp *tun_qp = &ctx->qp[MLX4_TUN_WRID_QPN(wc->wr_id)];
117937bfc7c1SJack Morgenstein 	int wr_ix = wc->wr_id & (MLX4_NUM_TUNNEL_BUFS - 1);
118037bfc7c1SJack Morgenstein 	struct mlx4_tunnel_mad *tunnel = tun_qp->ring[wr_ix].addr;
118137bfc7c1SJack Morgenstein 	struct mlx4_ib_ah ah;
118237bfc7c1SJack Morgenstein 	struct ib_ah_attr ah_attr;
118337bfc7c1SJack Morgenstein 	u8 *slave_id;
118437bfc7c1SJack Morgenstein 	int slave;
118537bfc7c1SJack Morgenstein 
118637bfc7c1SJack Morgenstein 	/* Get slave that sent this packet */
118737bfc7c1SJack Morgenstein 	if (wc->src_qp < dev->dev->caps.sqp_start ||
118837bfc7c1SJack Morgenstein 	    wc->src_qp >= dev->dev->caps.base_tunnel_sqpn ||
118937bfc7c1SJack Morgenstein 	    (wc->src_qp & 0x1) != ctx->port - 1 ||
119037bfc7c1SJack Morgenstein 	    wc->src_qp & 0x4) {
119137bfc7c1SJack Morgenstein 		mlx4_ib_warn(ctx->ib_dev, "can't multiplex bad sqp:%d\n", wc->src_qp);
119237bfc7c1SJack Morgenstein 		return;
119337bfc7c1SJack Morgenstein 	}
119437bfc7c1SJack Morgenstein 	slave = ((wc->src_qp & ~0x7) - dev->dev->caps.sqp_start) / 8;
119537bfc7c1SJack Morgenstein 	if (slave != ctx->slave) {
119637bfc7c1SJack Morgenstein 		mlx4_ib_warn(ctx->ib_dev, "can't multiplex bad sqp:%d: "
119737bfc7c1SJack Morgenstein 			     "belongs to another slave\n", wc->src_qp);
119837bfc7c1SJack Morgenstein 		return;
119937bfc7c1SJack Morgenstein 	}
120037bfc7c1SJack Morgenstein 	if (slave != mlx4_master_func_num(dev->dev) && !(wc->src_qp & 0x2)) {
120137bfc7c1SJack Morgenstein 		mlx4_ib_warn(ctx->ib_dev, "can't multiplex bad sqp:%d: "
120237bfc7c1SJack Morgenstein 			     "non-master trying to send QP0 packets\n", wc->src_qp);
120337bfc7c1SJack Morgenstein 		return;
120437bfc7c1SJack Morgenstein 	}
120537bfc7c1SJack Morgenstein 
120637bfc7c1SJack Morgenstein 	/* Map transaction ID */
120737bfc7c1SJack Morgenstein 	ib_dma_sync_single_for_cpu(ctx->ib_dev, tun_qp->ring[wr_ix].map,
120837bfc7c1SJack Morgenstein 				   sizeof (struct mlx4_tunnel_mad),
120937bfc7c1SJack Morgenstein 				   DMA_FROM_DEVICE);
121037bfc7c1SJack Morgenstein 	switch (tunnel->mad.mad_hdr.method) {
121137bfc7c1SJack Morgenstein 	case IB_MGMT_METHOD_SET:
121237bfc7c1SJack Morgenstein 	case IB_MGMT_METHOD_GET:
121337bfc7c1SJack Morgenstein 	case IB_MGMT_METHOD_REPORT:
121437bfc7c1SJack Morgenstein 	case IB_SA_METHOD_GET_TABLE:
121537bfc7c1SJack Morgenstein 	case IB_SA_METHOD_DELETE:
121637bfc7c1SJack Morgenstein 	case IB_SA_METHOD_GET_MULTI:
121737bfc7c1SJack Morgenstein 	case IB_SA_METHOD_GET_TRACE_TBL:
121837bfc7c1SJack Morgenstein 		slave_id = (u8 *) &tunnel->mad.mad_hdr.tid;
121937bfc7c1SJack Morgenstein 		if (*slave_id) {
122037bfc7c1SJack Morgenstein 			mlx4_ib_warn(ctx->ib_dev, "egress mad has non-null tid msb:%d "
122137bfc7c1SJack Morgenstein 				     "class:%d slave:%d\n", *slave_id,
122237bfc7c1SJack Morgenstein 				     tunnel->mad.mad_hdr.mgmt_class, slave);
122337bfc7c1SJack Morgenstein 			return;
122437bfc7c1SJack Morgenstein 		} else
122537bfc7c1SJack Morgenstein 			*slave_id = slave;
122637bfc7c1SJack Morgenstein 	default:
122737bfc7c1SJack Morgenstein 		/* nothing */;
122837bfc7c1SJack Morgenstein 	}
122937bfc7c1SJack Morgenstein 
123037bfc7c1SJack Morgenstein 	/* Class-specific handling */
123137bfc7c1SJack Morgenstein 	switch (tunnel->mad.mad_hdr.mgmt_class) {
123237bfc7c1SJack Morgenstein 	case IB_MGMT_CLASS_SUBN_ADM:
123337bfc7c1SJack Morgenstein 		if (mlx4_ib_multiplex_sa_handler(ctx->ib_dev, ctx->port, slave,
123437bfc7c1SJack Morgenstein 			      (struct ib_sa_mad *) &tunnel->mad))
123537bfc7c1SJack Morgenstein 			return;
123637bfc7c1SJack Morgenstein 		break;
12373cf69cc8SAmir Vadai 	case IB_MGMT_CLASS_CM:
12383cf69cc8SAmir Vadai 		if (mlx4_ib_multiplex_cm_handler(ctx->ib_dev, ctx->port, slave,
12393cf69cc8SAmir Vadai 			      (struct ib_mad *) &tunnel->mad))
12403cf69cc8SAmir Vadai 			return;
12413cf69cc8SAmir Vadai 		break;
124237bfc7c1SJack Morgenstein 	case IB_MGMT_CLASS_DEVICE_MGMT:
124337bfc7c1SJack Morgenstein 		if (tunnel->mad.mad_hdr.method != IB_MGMT_METHOD_GET &&
124437bfc7c1SJack Morgenstein 		    tunnel->mad.mad_hdr.method != IB_MGMT_METHOD_SET)
124537bfc7c1SJack Morgenstein 			return;
124637bfc7c1SJack Morgenstein 		break;
124737bfc7c1SJack Morgenstein 	default:
124837bfc7c1SJack Morgenstein 		/* Drop unsupported classes for slaves in tunnel mode */
124937bfc7c1SJack Morgenstein 		if (slave != mlx4_master_func_num(dev->dev)) {
125037bfc7c1SJack Morgenstein 			mlx4_ib_warn(ctx->ib_dev, "dropping unsupported egress mad from class:%d "
125137bfc7c1SJack Morgenstein 				     "for slave:%d\n", tunnel->mad.mad_hdr.mgmt_class, slave);
125237bfc7c1SJack Morgenstein 			return;
125337bfc7c1SJack Morgenstein 		}
125437bfc7c1SJack Morgenstein 	}
125537bfc7c1SJack Morgenstein 
125637bfc7c1SJack Morgenstein 	/* We are using standard ib_core services to send the mad, so generate a
125737bfc7c1SJack Morgenstein 	 * stadard address handle by decoding the tunnelled mlx4_ah fields */
125837bfc7c1SJack Morgenstein 	memcpy(&ah.av, &tunnel->hdr.av, sizeof (struct mlx4_av));
125937bfc7c1SJack Morgenstein 	ah.ibah.device = ctx->ib_dev;
126037bfc7c1SJack Morgenstein 	mlx4_ib_query_ah(&ah.ibah, &ah_attr);
126137bfc7c1SJack Morgenstein 	if ((ah_attr.ah_flags & IB_AH_GRH) &&
126237bfc7c1SJack Morgenstein 	    (ah_attr.grh.sgid_index != slave)) {
126337bfc7c1SJack Morgenstein 		mlx4_ib_warn(ctx->ib_dev, "slave:%d accessed invalid sgid_index:%d\n",
126437bfc7c1SJack Morgenstein 			     slave, ah_attr.grh.sgid_index);
126537bfc7c1SJack Morgenstein 		return;
126637bfc7c1SJack Morgenstein 	}
126737bfc7c1SJack Morgenstein 
126837bfc7c1SJack Morgenstein 	mlx4_ib_send_to_wire(dev, slave, ctx->port,
126937bfc7c1SJack Morgenstein 			     is_proxy_qp0(dev, wc->src_qp, slave) ?
127037bfc7c1SJack Morgenstein 			     IB_QPT_SMI : IB_QPT_GSI,
127137bfc7c1SJack Morgenstein 			     be16_to_cpu(tunnel->hdr.pkey_index),
127237bfc7c1SJack Morgenstein 			     be32_to_cpu(tunnel->hdr.remote_qpn),
127337bfc7c1SJack Morgenstein 			     be32_to_cpu(tunnel->hdr.qkey),
127437bfc7c1SJack Morgenstein 			     &ah_attr, &tunnel->mad);
127537bfc7c1SJack Morgenstein }
127637bfc7c1SJack Morgenstein 
1277fc06573dSJack Morgenstein static int mlx4_ib_alloc_pv_bufs(struct mlx4_ib_demux_pv_ctx *ctx,
1278fc06573dSJack Morgenstein 				 enum ib_qp_type qp_type, int is_tun)
1279fc06573dSJack Morgenstein {
1280fc06573dSJack Morgenstein 	int i;
1281fc06573dSJack Morgenstein 	struct mlx4_ib_demux_pv_qp *tun_qp;
1282fc06573dSJack Morgenstein 	int rx_buf_size, tx_buf_size;
1283fc06573dSJack Morgenstein 
1284fc06573dSJack Morgenstein 	if (qp_type > IB_QPT_GSI)
1285fc06573dSJack Morgenstein 		return -EINVAL;
1286fc06573dSJack Morgenstein 
1287fc06573dSJack Morgenstein 	tun_qp = &ctx->qp[qp_type];
1288fc06573dSJack Morgenstein 
1289fc06573dSJack Morgenstein 	tun_qp->ring = kzalloc(sizeof (struct mlx4_ib_buf) * MLX4_NUM_TUNNEL_BUFS,
1290fc06573dSJack Morgenstein 			       GFP_KERNEL);
1291fc06573dSJack Morgenstein 	if (!tun_qp->ring)
1292fc06573dSJack Morgenstein 		return -ENOMEM;
1293fc06573dSJack Morgenstein 
1294fc06573dSJack Morgenstein 	tun_qp->tx_ring = kcalloc(MLX4_NUM_TUNNEL_BUFS,
1295fc06573dSJack Morgenstein 				  sizeof (struct mlx4_ib_tun_tx_buf),
1296fc06573dSJack Morgenstein 				  GFP_KERNEL);
1297fc06573dSJack Morgenstein 	if (!tun_qp->tx_ring) {
1298fc06573dSJack Morgenstein 		kfree(tun_qp->ring);
1299fc06573dSJack Morgenstein 		tun_qp->ring = NULL;
1300fc06573dSJack Morgenstein 		return -ENOMEM;
1301fc06573dSJack Morgenstein 	}
1302fc06573dSJack Morgenstein 
1303fc06573dSJack Morgenstein 	if (is_tun) {
1304fc06573dSJack Morgenstein 		rx_buf_size = sizeof (struct mlx4_tunnel_mad);
1305fc06573dSJack Morgenstein 		tx_buf_size = sizeof (struct mlx4_rcv_tunnel_mad);
1306fc06573dSJack Morgenstein 	} else {
1307fc06573dSJack Morgenstein 		rx_buf_size = sizeof (struct mlx4_mad_rcv_buf);
1308fc06573dSJack Morgenstein 		tx_buf_size = sizeof (struct mlx4_mad_snd_buf);
1309fc06573dSJack Morgenstein 	}
1310fc06573dSJack Morgenstein 
1311fc06573dSJack Morgenstein 	for (i = 0; i < MLX4_NUM_TUNNEL_BUFS; i++) {
1312fc06573dSJack Morgenstein 		tun_qp->ring[i].addr = kmalloc(rx_buf_size, GFP_KERNEL);
1313fc06573dSJack Morgenstein 		if (!tun_qp->ring[i].addr)
1314fc06573dSJack Morgenstein 			goto err;
1315fc06573dSJack Morgenstein 		tun_qp->ring[i].map = ib_dma_map_single(ctx->ib_dev,
1316fc06573dSJack Morgenstein 							tun_qp->ring[i].addr,
1317fc06573dSJack Morgenstein 							rx_buf_size,
1318fc06573dSJack Morgenstein 							DMA_FROM_DEVICE);
1319fc06573dSJack Morgenstein 	}
1320fc06573dSJack Morgenstein 
1321fc06573dSJack Morgenstein 	for (i = 0; i < MLX4_NUM_TUNNEL_BUFS; i++) {
1322fc06573dSJack Morgenstein 		tun_qp->tx_ring[i].buf.addr =
1323fc06573dSJack Morgenstein 			kmalloc(tx_buf_size, GFP_KERNEL);
1324fc06573dSJack Morgenstein 		if (!tun_qp->tx_ring[i].buf.addr)
1325fc06573dSJack Morgenstein 			goto tx_err;
1326fc06573dSJack Morgenstein 		tun_qp->tx_ring[i].buf.map =
1327fc06573dSJack Morgenstein 			ib_dma_map_single(ctx->ib_dev,
1328fc06573dSJack Morgenstein 					  tun_qp->tx_ring[i].buf.addr,
1329fc06573dSJack Morgenstein 					  tx_buf_size,
1330fc06573dSJack Morgenstein 					  DMA_TO_DEVICE);
1331fc06573dSJack Morgenstein 		tun_qp->tx_ring[i].ah = NULL;
1332fc06573dSJack Morgenstein 	}
1333fc06573dSJack Morgenstein 	spin_lock_init(&tun_qp->tx_lock);
1334fc06573dSJack Morgenstein 	tun_qp->tx_ix_head = 0;
1335fc06573dSJack Morgenstein 	tun_qp->tx_ix_tail = 0;
1336fc06573dSJack Morgenstein 	tun_qp->proxy_qpt = qp_type;
1337fc06573dSJack Morgenstein 
1338fc06573dSJack Morgenstein 	return 0;
1339fc06573dSJack Morgenstein 
1340fc06573dSJack Morgenstein tx_err:
1341fc06573dSJack Morgenstein 	while (i > 0) {
1342fc06573dSJack Morgenstein 		--i;
1343fc06573dSJack Morgenstein 		ib_dma_unmap_single(ctx->ib_dev, tun_qp->tx_ring[i].buf.map,
1344fc06573dSJack Morgenstein 				    tx_buf_size, DMA_TO_DEVICE);
1345fc06573dSJack Morgenstein 		kfree(tun_qp->tx_ring[i].buf.addr);
1346fc06573dSJack Morgenstein 	}
1347fc06573dSJack Morgenstein 	kfree(tun_qp->tx_ring);
1348fc06573dSJack Morgenstein 	tun_qp->tx_ring = NULL;
1349fc06573dSJack Morgenstein 	i = MLX4_NUM_TUNNEL_BUFS;
1350fc06573dSJack Morgenstein err:
1351fc06573dSJack Morgenstein 	while (i > 0) {
1352fc06573dSJack Morgenstein 		--i;
1353fc06573dSJack Morgenstein 		ib_dma_unmap_single(ctx->ib_dev, tun_qp->ring[i].map,
1354fc06573dSJack Morgenstein 				    rx_buf_size, DMA_FROM_DEVICE);
1355fc06573dSJack Morgenstein 		kfree(tun_qp->ring[i].addr);
1356fc06573dSJack Morgenstein 	}
1357fc06573dSJack Morgenstein 	kfree(tun_qp->ring);
1358fc06573dSJack Morgenstein 	tun_qp->ring = NULL;
1359fc06573dSJack Morgenstein 	return -ENOMEM;
1360fc06573dSJack Morgenstein }
1361fc06573dSJack Morgenstein 
1362fc06573dSJack Morgenstein static void mlx4_ib_free_pv_qp_bufs(struct mlx4_ib_demux_pv_ctx *ctx,
1363fc06573dSJack Morgenstein 				     enum ib_qp_type qp_type, int is_tun)
1364fc06573dSJack Morgenstein {
1365fc06573dSJack Morgenstein 	int i;
1366fc06573dSJack Morgenstein 	struct mlx4_ib_demux_pv_qp *tun_qp;
1367fc06573dSJack Morgenstein 	int rx_buf_size, tx_buf_size;
1368fc06573dSJack Morgenstein 
1369fc06573dSJack Morgenstein 	if (qp_type > IB_QPT_GSI)
1370fc06573dSJack Morgenstein 		return;
1371fc06573dSJack Morgenstein 
1372fc06573dSJack Morgenstein 	tun_qp = &ctx->qp[qp_type];
1373fc06573dSJack Morgenstein 	if (is_tun) {
1374fc06573dSJack Morgenstein 		rx_buf_size = sizeof (struct mlx4_tunnel_mad);
1375fc06573dSJack Morgenstein 		tx_buf_size = sizeof (struct mlx4_rcv_tunnel_mad);
1376fc06573dSJack Morgenstein 	} else {
1377fc06573dSJack Morgenstein 		rx_buf_size = sizeof (struct mlx4_mad_rcv_buf);
1378fc06573dSJack Morgenstein 		tx_buf_size = sizeof (struct mlx4_mad_snd_buf);
1379fc06573dSJack Morgenstein 	}
1380fc06573dSJack Morgenstein 
1381fc06573dSJack Morgenstein 
1382fc06573dSJack Morgenstein 	for (i = 0; i < MLX4_NUM_TUNNEL_BUFS; i++) {
1383fc06573dSJack Morgenstein 		ib_dma_unmap_single(ctx->ib_dev, tun_qp->ring[i].map,
1384fc06573dSJack Morgenstein 				    rx_buf_size, DMA_FROM_DEVICE);
1385fc06573dSJack Morgenstein 		kfree(tun_qp->ring[i].addr);
1386fc06573dSJack Morgenstein 	}
1387fc06573dSJack Morgenstein 
1388fc06573dSJack Morgenstein 	for (i = 0; i < MLX4_NUM_TUNNEL_BUFS; i++) {
1389fc06573dSJack Morgenstein 		ib_dma_unmap_single(ctx->ib_dev, tun_qp->tx_ring[i].buf.map,
1390fc06573dSJack Morgenstein 				    tx_buf_size, DMA_TO_DEVICE);
1391fc06573dSJack Morgenstein 		kfree(tun_qp->tx_ring[i].buf.addr);
1392fc06573dSJack Morgenstein 		if (tun_qp->tx_ring[i].ah)
1393fc06573dSJack Morgenstein 			ib_destroy_ah(tun_qp->tx_ring[i].ah);
1394fc06573dSJack Morgenstein 	}
1395fc06573dSJack Morgenstein 	kfree(tun_qp->tx_ring);
1396fc06573dSJack Morgenstein 	kfree(tun_qp->ring);
1397fc06573dSJack Morgenstein }
1398fc06573dSJack Morgenstein 
1399fc06573dSJack Morgenstein static void mlx4_ib_tunnel_comp_worker(struct work_struct *work)
1400fc06573dSJack Morgenstein {
140137bfc7c1SJack Morgenstein 	struct mlx4_ib_demux_pv_ctx *ctx;
140237bfc7c1SJack Morgenstein 	struct mlx4_ib_demux_pv_qp *tun_qp;
140337bfc7c1SJack Morgenstein 	struct ib_wc wc;
140437bfc7c1SJack Morgenstein 	int ret;
140537bfc7c1SJack Morgenstein 	ctx = container_of(work, struct mlx4_ib_demux_pv_ctx, work);
140637bfc7c1SJack Morgenstein 	ib_req_notify_cq(ctx->cq, IB_CQ_NEXT_COMP);
140737bfc7c1SJack Morgenstein 
140837bfc7c1SJack Morgenstein 	while (ib_poll_cq(ctx->cq, 1, &wc) == 1) {
140937bfc7c1SJack Morgenstein 		tun_qp = &ctx->qp[MLX4_TUN_WRID_QPN(wc.wr_id)];
141037bfc7c1SJack Morgenstein 		if (wc.status == IB_WC_SUCCESS) {
141137bfc7c1SJack Morgenstein 			switch (wc.opcode) {
141237bfc7c1SJack Morgenstein 			case IB_WC_RECV:
141337bfc7c1SJack Morgenstein 				mlx4_ib_multiplex_mad(ctx, &wc);
141437bfc7c1SJack Morgenstein 				ret = mlx4_ib_post_pv_qp_buf(ctx, tun_qp,
141537bfc7c1SJack Morgenstein 							     wc.wr_id &
141637bfc7c1SJack Morgenstein 							     (MLX4_NUM_TUNNEL_BUFS - 1));
141737bfc7c1SJack Morgenstein 				if (ret)
141837bfc7c1SJack Morgenstein 					pr_err("Failed reposting tunnel "
141937bfc7c1SJack Morgenstein 					       "buf:%lld\n", wc.wr_id);
142037bfc7c1SJack Morgenstein 				break;
142137bfc7c1SJack Morgenstein 			case IB_WC_SEND:
142237bfc7c1SJack Morgenstein 				pr_debug("received tunnel send completion:"
142337bfc7c1SJack Morgenstein 					 "wrid=0x%llx, status=0x%x\n",
142437bfc7c1SJack Morgenstein 					 wc.wr_id, wc.status);
142537bfc7c1SJack Morgenstein 				ib_destroy_ah(tun_qp->tx_ring[wc.wr_id &
142637bfc7c1SJack Morgenstein 					      (MLX4_NUM_TUNNEL_BUFS - 1)].ah);
142737bfc7c1SJack Morgenstein 				tun_qp->tx_ring[wc.wr_id & (MLX4_NUM_TUNNEL_BUFS - 1)].ah
142837bfc7c1SJack Morgenstein 					= NULL;
142937bfc7c1SJack Morgenstein 				spin_lock(&tun_qp->tx_lock);
143037bfc7c1SJack Morgenstein 				tun_qp->tx_ix_tail++;
143137bfc7c1SJack Morgenstein 				spin_unlock(&tun_qp->tx_lock);
143237bfc7c1SJack Morgenstein 
143337bfc7c1SJack Morgenstein 				break;
143437bfc7c1SJack Morgenstein 			default:
143537bfc7c1SJack Morgenstein 				break;
143637bfc7c1SJack Morgenstein 			}
143737bfc7c1SJack Morgenstein 		} else  {
143837bfc7c1SJack Morgenstein 			pr_debug("mlx4_ib: completion error in tunnel: %d."
143937bfc7c1SJack Morgenstein 				 " status = %d, wrid = 0x%llx\n",
144037bfc7c1SJack Morgenstein 				 ctx->slave, wc.status, wc.wr_id);
144137bfc7c1SJack Morgenstein 			if (!MLX4_TUN_IS_RECV(wc.wr_id)) {
144237bfc7c1SJack Morgenstein 				ib_destroy_ah(tun_qp->tx_ring[wc.wr_id &
144337bfc7c1SJack Morgenstein 					      (MLX4_NUM_TUNNEL_BUFS - 1)].ah);
144437bfc7c1SJack Morgenstein 				tun_qp->tx_ring[wc.wr_id & (MLX4_NUM_TUNNEL_BUFS - 1)].ah
144537bfc7c1SJack Morgenstein 					= NULL;
144637bfc7c1SJack Morgenstein 				spin_lock(&tun_qp->tx_lock);
144737bfc7c1SJack Morgenstein 				tun_qp->tx_ix_tail++;
144837bfc7c1SJack Morgenstein 				spin_unlock(&tun_qp->tx_lock);
144937bfc7c1SJack Morgenstein 			}
145037bfc7c1SJack Morgenstein 		}
145137bfc7c1SJack Morgenstein 	}
1452fc06573dSJack Morgenstein }
1453fc06573dSJack Morgenstein 
1454fc06573dSJack Morgenstein static void pv_qp_event_handler(struct ib_event *event, void *qp_context)
1455fc06573dSJack Morgenstein {
1456fc06573dSJack Morgenstein 	struct mlx4_ib_demux_pv_ctx *sqp = qp_context;
1457fc06573dSJack Morgenstein 
1458fc06573dSJack Morgenstein 	/* It's worse than that! He's dead, Jim! */
1459fc06573dSJack Morgenstein 	pr_err("Fatal error (%d) on a MAD QP on port %d\n",
1460fc06573dSJack Morgenstein 	       event->event, sqp->port);
1461fc06573dSJack Morgenstein }
1462fc06573dSJack Morgenstein 
1463fc06573dSJack Morgenstein static int create_pv_sqp(struct mlx4_ib_demux_pv_ctx *ctx,
1464fc06573dSJack Morgenstein 			    enum ib_qp_type qp_type, int create_tun)
1465fc06573dSJack Morgenstein {
1466fc06573dSJack Morgenstein 	int i, ret;
1467fc06573dSJack Morgenstein 	struct mlx4_ib_demux_pv_qp *tun_qp;
1468fc06573dSJack Morgenstein 	struct mlx4_ib_qp_tunnel_init_attr qp_init_attr;
1469fc06573dSJack Morgenstein 	struct ib_qp_attr attr;
1470fc06573dSJack Morgenstein 	int qp_attr_mask_INIT;
1471fc06573dSJack Morgenstein 
1472fc06573dSJack Morgenstein 	if (qp_type > IB_QPT_GSI)
1473fc06573dSJack Morgenstein 		return -EINVAL;
1474fc06573dSJack Morgenstein 
1475fc06573dSJack Morgenstein 	tun_qp = &ctx->qp[qp_type];
1476fc06573dSJack Morgenstein 
1477fc06573dSJack Morgenstein 	memset(&qp_init_attr, 0, sizeof qp_init_attr);
1478fc06573dSJack Morgenstein 	qp_init_attr.init_attr.send_cq = ctx->cq;
1479fc06573dSJack Morgenstein 	qp_init_attr.init_attr.recv_cq = ctx->cq;
1480fc06573dSJack Morgenstein 	qp_init_attr.init_attr.sq_sig_type = IB_SIGNAL_ALL_WR;
1481fc06573dSJack Morgenstein 	qp_init_attr.init_attr.cap.max_send_wr = MLX4_NUM_TUNNEL_BUFS;
1482fc06573dSJack Morgenstein 	qp_init_attr.init_attr.cap.max_recv_wr = MLX4_NUM_TUNNEL_BUFS;
1483fc06573dSJack Morgenstein 	qp_init_attr.init_attr.cap.max_send_sge = 1;
1484fc06573dSJack Morgenstein 	qp_init_attr.init_attr.cap.max_recv_sge = 1;
1485fc06573dSJack Morgenstein 	if (create_tun) {
1486fc06573dSJack Morgenstein 		qp_init_attr.init_attr.qp_type = IB_QPT_UD;
1487fc06573dSJack Morgenstein 		qp_init_attr.init_attr.create_flags = MLX4_IB_SRIOV_TUNNEL_QP;
1488fc06573dSJack Morgenstein 		qp_init_attr.port = ctx->port;
1489fc06573dSJack Morgenstein 		qp_init_attr.slave = ctx->slave;
1490fc06573dSJack Morgenstein 		qp_init_attr.proxy_qp_type = qp_type;
1491fc06573dSJack Morgenstein 		qp_attr_mask_INIT = IB_QP_STATE | IB_QP_PKEY_INDEX |
1492fc06573dSJack Morgenstein 			   IB_QP_QKEY | IB_QP_PORT;
1493fc06573dSJack Morgenstein 	} else {
1494fc06573dSJack Morgenstein 		qp_init_attr.init_attr.qp_type = qp_type;
1495fc06573dSJack Morgenstein 		qp_init_attr.init_attr.create_flags = MLX4_IB_SRIOV_SQP;
1496fc06573dSJack Morgenstein 		qp_attr_mask_INIT = IB_QP_STATE | IB_QP_PKEY_INDEX | IB_QP_QKEY;
1497fc06573dSJack Morgenstein 	}
1498fc06573dSJack Morgenstein 	qp_init_attr.init_attr.port_num = ctx->port;
1499fc06573dSJack Morgenstein 	qp_init_attr.init_attr.qp_context = ctx;
1500fc06573dSJack Morgenstein 	qp_init_attr.init_attr.event_handler = pv_qp_event_handler;
1501fc06573dSJack Morgenstein 	tun_qp->qp = ib_create_qp(ctx->pd, &qp_init_attr.init_attr);
1502fc06573dSJack Morgenstein 	if (IS_ERR(tun_qp->qp)) {
1503fc06573dSJack Morgenstein 		ret = PTR_ERR(tun_qp->qp);
1504fc06573dSJack Morgenstein 		tun_qp->qp = NULL;
1505fc06573dSJack Morgenstein 		pr_err("Couldn't create %s QP (%d)\n",
1506fc06573dSJack Morgenstein 		       create_tun ? "tunnel" : "special", ret);
1507fc06573dSJack Morgenstein 		return ret;
1508fc06573dSJack Morgenstein 	}
1509fc06573dSJack Morgenstein 
1510fc06573dSJack Morgenstein 	memset(&attr, 0, sizeof attr);
1511fc06573dSJack Morgenstein 	attr.qp_state = IB_QPS_INIT;
1512fc06573dSJack Morgenstein 	attr.pkey_index =
1513fc06573dSJack Morgenstein 		to_mdev(ctx->ib_dev)->pkeys.virt2phys_pkey[ctx->slave][ctx->port - 1][0];
1514fc06573dSJack Morgenstein 	attr.qkey = IB_QP1_QKEY;
1515fc06573dSJack Morgenstein 	attr.port_num = ctx->port;
1516fc06573dSJack Morgenstein 	ret = ib_modify_qp(tun_qp->qp, &attr, qp_attr_mask_INIT);
1517fc06573dSJack Morgenstein 	if (ret) {
1518fc06573dSJack Morgenstein 		pr_err("Couldn't change %s qp state to INIT (%d)\n",
1519fc06573dSJack Morgenstein 		       create_tun ? "tunnel" : "special", ret);
1520fc06573dSJack Morgenstein 		goto err_qp;
1521fc06573dSJack Morgenstein 	}
1522fc06573dSJack Morgenstein 	attr.qp_state = IB_QPS_RTR;
1523fc06573dSJack Morgenstein 	ret = ib_modify_qp(tun_qp->qp, &attr, IB_QP_STATE);
1524fc06573dSJack Morgenstein 	if (ret) {
1525fc06573dSJack Morgenstein 		pr_err("Couldn't change %s qp state to RTR (%d)\n",
1526fc06573dSJack Morgenstein 		       create_tun ? "tunnel" : "special", ret);
1527fc06573dSJack Morgenstein 		goto err_qp;
1528fc06573dSJack Morgenstein 	}
1529fc06573dSJack Morgenstein 	attr.qp_state = IB_QPS_RTS;
1530fc06573dSJack Morgenstein 	attr.sq_psn = 0;
1531fc06573dSJack Morgenstein 	ret = ib_modify_qp(tun_qp->qp, &attr, IB_QP_STATE | IB_QP_SQ_PSN);
1532fc06573dSJack Morgenstein 	if (ret) {
1533fc06573dSJack Morgenstein 		pr_err("Couldn't change %s qp state to RTS (%d)\n",
1534fc06573dSJack Morgenstein 		       create_tun ? "tunnel" : "special", ret);
1535fc06573dSJack Morgenstein 		goto err_qp;
1536fc06573dSJack Morgenstein 	}
1537fc06573dSJack Morgenstein 
1538fc06573dSJack Morgenstein 	for (i = 0; i < MLX4_NUM_TUNNEL_BUFS; i++) {
1539fc06573dSJack Morgenstein 		ret = mlx4_ib_post_pv_qp_buf(ctx, tun_qp, i);
1540fc06573dSJack Morgenstein 		if (ret) {
1541fc06573dSJack Morgenstein 			pr_err(" mlx4_ib_post_pv_buf error"
1542fc06573dSJack Morgenstein 			       " (err = %d, i = %d)\n", ret, i);
1543fc06573dSJack Morgenstein 			goto err_qp;
1544fc06573dSJack Morgenstein 		}
1545fc06573dSJack Morgenstein 	}
1546fc06573dSJack Morgenstein 	return 0;
1547fc06573dSJack Morgenstein 
1548fc06573dSJack Morgenstein err_qp:
1549fc06573dSJack Morgenstein 	ib_destroy_qp(tun_qp->qp);
1550fc06573dSJack Morgenstein 	tun_qp->qp = NULL;
1551fc06573dSJack Morgenstein 	return ret;
1552fc06573dSJack Morgenstein }
1553fc06573dSJack Morgenstein 
1554fc06573dSJack Morgenstein /*
1555fc06573dSJack Morgenstein  * IB MAD completion callback for real SQPs
1556fc06573dSJack Morgenstein  */
1557fc06573dSJack Morgenstein static void mlx4_ib_sqp_comp_worker(struct work_struct *work)
1558fc06573dSJack Morgenstein {
155937bfc7c1SJack Morgenstein 	struct mlx4_ib_demux_pv_ctx *ctx;
156037bfc7c1SJack Morgenstein 	struct mlx4_ib_demux_pv_qp *sqp;
156137bfc7c1SJack Morgenstein 	struct ib_wc wc;
156237bfc7c1SJack Morgenstein 	struct ib_grh *grh;
156337bfc7c1SJack Morgenstein 	struct ib_mad *mad;
156437bfc7c1SJack Morgenstein 
156537bfc7c1SJack Morgenstein 	ctx = container_of(work, struct mlx4_ib_demux_pv_ctx, work);
156637bfc7c1SJack Morgenstein 	ib_req_notify_cq(ctx->cq, IB_CQ_NEXT_COMP);
156737bfc7c1SJack Morgenstein 
156837bfc7c1SJack Morgenstein 	while (mlx4_ib_poll_cq(ctx->cq, 1, &wc) == 1) {
156937bfc7c1SJack Morgenstein 		sqp = &ctx->qp[MLX4_TUN_WRID_QPN(wc.wr_id)];
157037bfc7c1SJack Morgenstein 		if (wc.status == IB_WC_SUCCESS) {
157137bfc7c1SJack Morgenstein 			switch (wc.opcode) {
157237bfc7c1SJack Morgenstein 			case IB_WC_SEND:
157337bfc7c1SJack Morgenstein 				ib_destroy_ah(sqp->tx_ring[wc.wr_id &
157437bfc7c1SJack Morgenstein 					      (MLX4_NUM_TUNNEL_BUFS - 1)].ah);
157537bfc7c1SJack Morgenstein 				sqp->tx_ring[wc.wr_id & (MLX4_NUM_TUNNEL_BUFS - 1)].ah
157637bfc7c1SJack Morgenstein 					= NULL;
157737bfc7c1SJack Morgenstein 				spin_lock(&sqp->tx_lock);
157837bfc7c1SJack Morgenstein 				sqp->tx_ix_tail++;
157937bfc7c1SJack Morgenstein 				spin_unlock(&sqp->tx_lock);
158037bfc7c1SJack Morgenstein 				break;
158137bfc7c1SJack Morgenstein 			case IB_WC_RECV:
158237bfc7c1SJack Morgenstein 				mad = (struct ib_mad *) &(((struct mlx4_mad_rcv_buf *)
158337bfc7c1SJack Morgenstein 						(sqp->ring[wc.wr_id &
158437bfc7c1SJack Morgenstein 						(MLX4_NUM_TUNNEL_BUFS - 1)].addr))->payload);
158537bfc7c1SJack Morgenstein 				grh = &(((struct mlx4_mad_rcv_buf *)
158637bfc7c1SJack Morgenstein 						(sqp->ring[wc.wr_id &
158737bfc7c1SJack Morgenstein 						(MLX4_NUM_TUNNEL_BUFS - 1)].addr))->grh);
158837bfc7c1SJack Morgenstein 				mlx4_ib_demux_mad(ctx->ib_dev, ctx->port, &wc, grh, mad);
158937bfc7c1SJack Morgenstein 				if (mlx4_ib_post_pv_qp_buf(ctx, sqp, wc.wr_id &
159037bfc7c1SJack Morgenstein 							   (MLX4_NUM_TUNNEL_BUFS - 1)))
159137bfc7c1SJack Morgenstein 					pr_err("Failed reposting SQP "
159237bfc7c1SJack Morgenstein 					       "buf:%lld\n", wc.wr_id);
159337bfc7c1SJack Morgenstein 				break;
159437bfc7c1SJack Morgenstein 			default:
159537bfc7c1SJack Morgenstein 				BUG_ON(1);
159637bfc7c1SJack Morgenstein 				break;
159737bfc7c1SJack Morgenstein 			}
159837bfc7c1SJack Morgenstein 		} else  {
159937bfc7c1SJack Morgenstein 			pr_debug("mlx4_ib: completion error in tunnel: %d."
160037bfc7c1SJack Morgenstein 				 " status = %d, wrid = 0x%llx\n",
160137bfc7c1SJack Morgenstein 				 ctx->slave, wc.status, wc.wr_id);
160237bfc7c1SJack Morgenstein 			if (!MLX4_TUN_IS_RECV(wc.wr_id)) {
160337bfc7c1SJack Morgenstein 				ib_destroy_ah(sqp->tx_ring[wc.wr_id &
160437bfc7c1SJack Morgenstein 					      (MLX4_NUM_TUNNEL_BUFS - 1)].ah);
160537bfc7c1SJack Morgenstein 				sqp->tx_ring[wc.wr_id & (MLX4_NUM_TUNNEL_BUFS - 1)].ah
160637bfc7c1SJack Morgenstein 					= NULL;
160737bfc7c1SJack Morgenstein 				spin_lock(&sqp->tx_lock);
160837bfc7c1SJack Morgenstein 				sqp->tx_ix_tail++;
160937bfc7c1SJack Morgenstein 				spin_unlock(&sqp->tx_lock);
161037bfc7c1SJack Morgenstein 			}
161137bfc7c1SJack Morgenstein 		}
161237bfc7c1SJack Morgenstein 	}
1613fc06573dSJack Morgenstein }
1614fc06573dSJack Morgenstein 
1615fc06573dSJack Morgenstein static int alloc_pv_object(struct mlx4_ib_dev *dev, int slave, int port,
1616fc06573dSJack Morgenstein 			       struct mlx4_ib_demux_pv_ctx **ret_ctx)
1617fc06573dSJack Morgenstein {
1618fc06573dSJack Morgenstein 	struct mlx4_ib_demux_pv_ctx *ctx;
1619fc06573dSJack Morgenstein 
1620fc06573dSJack Morgenstein 	*ret_ctx = NULL;
1621fc06573dSJack Morgenstein 	ctx = kzalloc(sizeof (struct mlx4_ib_demux_pv_ctx), GFP_KERNEL);
1622fc06573dSJack Morgenstein 	if (!ctx) {
1623fc06573dSJack Morgenstein 		pr_err("failed allocating pv resource context "
1624fc06573dSJack Morgenstein 		       "for port %d, slave %d\n", port, slave);
1625fc06573dSJack Morgenstein 		return -ENOMEM;
1626fc06573dSJack Morgenstein 	}
1627fc06573dSJack Morgenstein 
1628fc06573dSJack Morgenstein 	ctx->ib_dev = &dev->ib_dev;
1629fc06573dSJack Morgenstein 	ctx->port = port;
1630fc06573dSJack Morgenstein 	ctx->slave = slave;
1631fc06573dSJack Morgenstein 	*ret_ctx = ctx;
1632fc06573dSJack Morgenstein 	return 0;
1633fc06573dSJack Morgenstein }
1634fc06573dSJack Morgenstein 
1635fc06573dSJack Morgenstein static void free_pv_object(struct mlx4_ib_dev *dev, int slave, int port)
1636fc06573dSJack Morgenstein {
1637fc06573dSJack Morgenstein 	if (dev->sriov.demux[port - 1].tun[slave]) {
1638fc06573dSJack Morgenstein 		kfree(dev->sriov.demux[port - 1].tun[slave]);
1639fc06573dSJack Morgenstein 		dev->sriov.demux[port - 1].tun[slave] = NULL;
1640fc06573dSJack Morgenstein 	}
1641fc06573dSJack Morgenstein }
1642fc06573dSJack Morgenstein 
1643fc06573dSJack Morgenstein static int create_pv_resources(struct ib_device *ibdev, int slave, int port,
1644fc06573dSJack Morgenstein 			       int create_tun, struct mlx4_ib_demux_pv_ctx *ctx)
1645fc06573dSJack Morgenstein {
1646fc06573dSJack Morgenstein 	int ret, cq_size;
1647fc06573dSJack Morgenstein 
1648fc06573dSJack Morgenstein 	ctx->state = DEMUX_PV_STATE_STARTING;
1649fc06573dSJack Morgenstein 	/* have QP0 only on port owner, and only if link layer is IB */
1650fc06573dSJack Morgenstein 	if (ctx->slave == mlx4_master_func_num(to_mdev(ctx->ib_dev)->dev) &&
1651fc06573dSJack Morgenstein 	    rdma_port_get_link_layer(ibdev, ctx->port) == IB_LINK_LAYER_INFINIBAND)
1652fc06573dSJack Morgenstein 		ctx->has_smi = 1;
1653fc06573dSJack Morgenstein 
1654fc06573dSJack Morgenstein 	if (ctx->has_smi) {
1655fc06573dSJack Morgenstein 		ret = mlx4_ib_alloc_pv_bufs(ctx, IB_QPT_SMI, create_tun);
1656fc06573dSJack Morgenstein 		if (ret) {
1657fc06573dSJack Morgenstein 			pr_err("Failed allocating qp0 tunnel bufs (%d)\n", ret);
1658fc06573dSJack Morgenstein 			goto err_out;
1659fc06573dSJack Morgenstein 		}
1660fc06573dSJack Morgenstein 	}
1661fc06573dSJack Morgenstein 
1662fc06573dSJack Morgenstein 	ret = mlx4_ib_alloc_pv_bufs(ctx, IB_QPT_GSI, create_tun);
1663fc06573dSJack Morgenstein 	if (ret) {
1664fc06573dSJack Morgenstein 		pr_err("Failed allocating qp1 tunnel bufs (%d)\n", ret);
1665fc06573dSJack Morgenstein 		goto err_out_qp0;
1666fc06573dSJack Morgenstein 	}
1667fc06573dSJack Morgenstein 
1668fc06573dSJack Morgenstein 	cq_size = 2 * MLX4_NUM_TUNNEL_BUFS;
1669fc06573dSJack Morgenstein 	if (ctx->has_smi)
1670fc06573dSJack Morgenstein 		cq_size *= 2;
1671fc06573dSJack Morgenstein 
1672fc06573dSJack Morgenstein 	ctx->cq = ib_create_cq(ctx->ib_dev, mlx4_ib_tunnel_comp_handler,
1673fc06573dSJack Morgenstein 			       NULL, ctx, cq_size, 0);
1674fc06573dSJack Morgenstein 	if (IS_ERR(ctx->cq)) {
1675fc06573dSJack Morgenstein 		ret = PTR_ERR(ctx->cq);
1676fc06573dSJack Morgenstein 		pr_err("Couldn't create tunnel CQ (%d)\n", ret);
1677fc06573dSJack Morgenstein 		goto err_buf;
1678fc06573dSJack Morgenstein 	}
1679fc06573dSJack Morgenstein 
1680fc06573dSJack Morgenstein 	ctx->pd = ib_alloc_pd(ctx->ib_dev);
1681fc06573dSJack Morgenstein 	if (IS_ERR(ctx->pd)) {
1682fc06573dSJack Morgenstein 		ret = PTR_ERR(ctx->pd);
1683fc06573dSJack Morgenstein 		pr_err("Couldn't create tunnel PD (%d)\n", ret);
1684fc06573dSJack Morgenstein 		goto err_cq;
1685fc06573dSJack Morgenstein 	}
1686fc06573dSJack Morgenstein 
1687fc06573dSJack Morgenstein 	ctx->mr = ib_get_dma_mr(ctx->pd, IB_ACCESS_LOCAL_WRITE);
1688fc06573dSJack Morgenstein 	if (IS_ERR(ctx->mr)) {
1689fc06573dSJack Morgenstein 		ret = PTR_ERR(ctx->mr);
1690fc06573dSJack Morgenstein 		pr_err("Couldn't get tunnel DMA MR (%d)\n", ret);
1691fc06573dSJack Morgenstein 		goto err_pd;
1692fc06573dSJack Morgenstein 	}
1693fc06573dSJack Morgenstein 
1694fc06573dSJack Morgenstein 	if (ctx->has_smi) {
1695fc06573dSJack Morgenstein 		ret = create_pv_sqp(ctx, IB_QPT_SMI, create_tun);
1696fc06573dSJack Morgenstein 		if (ret) {
1697fc06573dSJack Morgenstein 			pr_err("Couldn't create %s QP0 (%d)\n",
1698fc06573dSJack Morgenstein 			       create_tun ? "tunnel for" : "",  ret);
1699fc06573dSJack Morgenstein 			goto err_mr;
1700fc06573dSJack Morgenstein 		}
1701fc06573dSJack Morgenstein 	}
1702fc06573dSJack Morgenstein 
1703fc06573dSJack Morgenstein 	ret = create_pv_sqp(ctx, IB_QPT_GSI, create_tun);
1704fc06573dSJack Morgenstein 	if (ret) {
1705fc06573dSJack Morgenstein 		pr_err("Couldn't create %s QP1 (%d)\n",
1706fc06573dSJack Morgenstein 		       create_tun ? "tunnel for" : "",  ret);
1707fc06573dSJack Morgenstein 		goto err_qp0;
1708fc06573dSJack Morgenstein 	}
1709fc06573dSJack Morgenstein 
1710fc06573dSJack Morgenstein 	if (create_tun)
1711fc06573dSJack Morgenstein 		INIT_WORK(&ctx->work, mlx4_ib_tunnel_comp_worker);
1712fc06573dSJack Morgenstein 	else
1713fc06573dSJack Morgenstein 		INIT_WORK(&ctx->work, mlx4_ib_sqp_comp_worker);
1714fc06573dSJack Morgenstein 
1715fc06573dSJack Morgenstein 	ctx->wq = to_mdev(ibdev)->sriov.demux[port - 1].wq;
1716fc06573dSJack Morgenstein 
1717fc06573dSJack Morgenstein 	ret = ib_req_notify_cq(ctx->cq, IB_CQ_NEXT_COMP);
1718fc06573dSJack Morgenstein 	if (ret) {
1719fc06573dSJack Morgenstein 		pr_err("Couldn't arm tunnel cq (%d)\n", ret);
1720fc06573dSJack Morgenstein 		goto err_wq;
1721fc06573dSJack Morgenstein 	}
1722fc06573dSJack Morgenstein 	ctx->state = DEMUX_PV_STATE_ACTIVE;
1723fc06573dSJack Morgenstein 	return 0;
1724fc06573dSJack Morgenstein 
1725fc06573dSJack Morgenstein err_wq:
1726fc06573dSJack Morgenstein 	ctx->wq = NULL;
1727fc06573dSJack Morgenstein 	ib_destroy_qp(ctx->qp[1].qp);
1728fc06573dSJack Morgenstein 	ctx->qp[1].qp = NULL;
1729fc06573dSJack Morgenstein 
1730fc06573dSJack Morgenstein 
1731fc06573dSJack Morgenstein err_qp0:
1732fc06573dSJack Morgenstein 	if (ctx->has_smi)
1733fc06573dSJack Morgenstein 		ib_destroy_qp(ctx->qp[0].qp);
1734fc06573dSJack Morgenstein 	ctx->qp[0].qp = NULL;
1735fc06573dSJack Morgenstein 
1736fc06573dSJack Morgenstein err_mr:
1737fc06573dSJack Morgenstein 	ib_dereg_mr(ctx->mr);
1738fc06573dSJack Morgenstein 	ctx->mr = NULL;
1739fc06573dSJack Morgenstein 
1740fc06573dSJack Morgenstein err_pd:
1741fc06573dSJack Morgenstein 	ib_dealloc_pd(ctx->pd);
1742fc06573dSJack Morgenstein 	ctx->pd = NULL;
1743fc06573dSJack Morgenstein 
1744fc06573dSJack Morgenstein err_cq:
1745fc06573dSJack Morgenstein 	ib_destroy_cq(ctx->cq);
1746fc06573dSJack Morgenstein 	ctx->cq = NULL;
1747fc06573dSJack Morgenstein 
1748fc06573dSJack Morgenstein err_buf:
1749fc06573dSJack Morgenstein 	mlx4_ib_free_pv_qp_bufs(ctx, IB_QPT_GSI, create_tun);
1750fc06573dSJack Morgenstein 
1751fc06573dSJack Morgenstein err_out_qp0:
1752fc06573dSJack Morgenstein 	if (ctx->has_smi)
1753fc06573dSJack Morgenstein 		mlx4_ib_free_pv_qp_bufs(ctx, IB_QPT_SMI, create_tun);
1754fc06573dSJack Morgenstein err_out:
1755fc06573dSJack Morgenstein 	ctx->state = DEMUX_PV_STATE_DOWN;
1756fc06573dSJack Morgenstein 	return ret;
1757fc06573dSJack Morgenstein }
1758fc06573dSJack Morgenstein 
1759fc06573dSJack Morgenstein static void destroy_pv_resources(struct mlx4_ib_dev *dev, int slave, int port,
1760fc06573dSJack Morgenstein 				 struct mlx4_ib_demux_pv_ctx *ctx, int flush)
1761fc06573dSJack Morgenstein {
1762fc06573dSJack Morgenstein 	if (!ctx)
1763fc06573dSJack Morgenstein 		return;
1764fc06573dSJack Morgenstein 	if (ctx->state > DEMUX_PV_STATE_DOWN) {
1765fc06573dSJack Morgenstein 		ctx->state = DEMUX_PV_STATE_DOWNING;
1766fc06573dSJack Morgenstein 		if (flush)
1767fc06573dSJack Morgenstein 			flush_workqueue(ctx->wq);
1768fc06573dSJack Morgenstein 		if (ctx->has_smi) {
1769fc06573dSJack Morgenstein 			ib_destroy_qp(ctx->qp[0].qp);
1770fc06573dSJack Morgenstein 			ctx->qp[0].qp = NULL;
1771fc06573dSJack Morgenstein 			mlx4_ib_free_pv_qp_bufs(ctx, IB_QPT_SMI, 1);
1772fc06573dSJack Morgenstein 		}
1773fc06573dSJack Morgenstein 		ib_destroy_qp(ctx->qp[1].qp);
1774fc06573dSJack Morgenstein 		ctx->qp[1].qp = NULL;
1775fc06573dSJack Morgenstein 		mlx4_ib_free_pv_qp_bufs(ctx, IB_QPT_GSI, 1);
1776fc06573dSJack Morgenstein 		ib_dereg_mr(ctx->mr);
1777fc06573dSJack Morgenstein 		ctx->mr = NULL;
1778fc06573dSJack Morgenstein 		ib_dealloc_pd(ctx->pd);
1779fc06573dSJack Morgenstein 		ctx->pd = NULL;
1780fc06573dSJack Morgenstein 		ib_destroy_cq(ctx->cq);
1781fc06573dSJack Morgenstein 		ctx->cq = NULL;
1782fc06573dSJack Morgenstein 		ctx->state = DEMUX_PV_STATE_DOWN;
1783fc06573dSJack Morgenstein 	}
1784fc06573dSJack Morgenstein }
1785fc06573dSJack Morgenstein 
1786fc06573dSJack Morgenstein static int mlx4_ib_tunnels_update(struct mlx4_ib_dev *dev, int slave,
1787fc06573dSJack Morgenstein 				  int port, int do_init)
1788fc06573dSJack Morgenstein {
1789fc06573dSJack Morgenstein 	int ret = 0;
1790fc06573dSJack Morgenstein 
1791fc06573dSJack Morgenstein 	if (!do_init) {
1792b9c5d6a6SOren Duer 		clean_vf_mcast(&dev->sriov.demux[port - 1], slave);
1793fc06573dSJack Morgenstein 		/* for master, destroy real sqp resources */
1794fc06573dSJack Morgenstein 		if (slave == mlx4_master_func_num(dev->dev))
1795fc06573dSJack Morgenstein 			destroy_pv_resources(dev, slave, port,
1796fc06573dSJack Morgenstein 					     dev->sriov.sqps[port - 1], 1);
1797fc06573dSJack Morgenstein 		/* destroy the tunnel qp resources */
1798fc06573dSJack Morgenstein 		destroy_pv_resources(dev, slave, port,
1799fc06573dSJack Morgenstein 				     dev->sriov.demux[port - 1].tun[slave], 1);
1800fc06573dSJack Morgenstein 		return 0;
1801fc06573dSJack Morgenstein 	}
1802fc06573dSJack Morgenstein 
1803fc06573dSJack Morgenstein 	/* create the tunnel qp resources */
1804fc06573dSJack Morgenstein 	ret = create_pv_resources(&dev->ib_dev, slave, port, 1,
1805fc06573dSJack Morgenstein 				  dev->sriov.demux[port - 1].tun[slave]);
1806fc06573dSJack Morgenstein 
1807fc06573dSJack Morgenstein 	/* for master, create the real sqp resources */
1808fc06573dSJack Morgenstein 	if (!ret && slave == mlx4_master_func_num(dev->dev))
1809fc06573dSJack Morgenstein 		ret = create_pv_resources(&dev->ib_dev, slave, port, 0,
1810fc06573dSJack Morgenstein 					  dev->sriov.sqps[port - 1]);
1811fc06573dSJack Morgenstein 	return ret;
1812fc06573dSJack Morgenstein }
1813fc06573dSJack Morgenstein 
1814fc06573dSJack Morgenstein void mlx4_ib_tunnels_update_work(struct work_struct *work)
1815fc06573dSJack Morgenstein {
1816fc06573dSJack Morgenstein 	struct mlx4_ib_demux_work *dmxw;
1817fc06573dSJack Morgenstein 
1818fc06573dSJack Morgenstein 	dmxw = container_of(work, struct mlx4_ib_demux_work, work);
1819fc06573dSJack Morgenstein 	mlx4_ib_tunnels_update(dmxw->dev, dmxw->slave, (int) dmxw->port,
1820fc06573dSJack Morgenstein 			       dmxw->do_init);
1821fc06573dSJack Morgenstein 	kfree(dmxw);
1822fc06573dSJack Morgenstein 	return;
1823fc06573dSJack Morgenstein }
1824fc06573dSJack Morgenstein 
1825fc06573dSJack Morgenstein static int mlx4_ib_alloc_demux_ctx(struct mlx4_ib_dev *dev,
1826fc06573dSJack Morgenstein 				       struct mlx4_ib_demux_ctx *ctx,
1827fc06573dSJack Morgenstein 				       int port)
1828fc06573dSJack Morgenstein {
1829fc06573dSJack Morgenstein 	char name[12];
1830fc06573dSJack Morgenstein 	int ret = 0;
1831fc06573dSJack Morgenstein 	int i;
1832fc06573dSJack Morgenstein 
1833fc06573dSJack Morgenstein 	ctx->tun = kcalloc(dev->dev->caps.sqp_demux,
1834fc06573dSJack Morgenstein 			   sizeof (struct mlx4_ib_demux_pv_ctx *), GFP_KERNEL);
1835fc06573dSJack Morgenstein 	if (!ctx->tun)
1836fc06573dSJack Morgenstein 		return -ENOMEM;
1837fc06573dSJack Morgenstein 
1838fc06573dSJack Morgenstein 	ctx->dev = dev;
1839fc06573dSJack Morgenstein 	ctx->port = port;
1840fc06573dSJack Morgenstein 	ctx->ib_dev = &dev->ib_dev;
1841fc06573dSJack Morgenstein 
1842fc06573dSJack Morgenstein 	for (i = 0; i < dev->dev->caps.sqp_demux; i++) {
1843fc06573dSJack Morgenstein 		ret = alloc_pv_object(dev, i, port, &ctx->tun[i]);
1844fc06573dSJack Morgenstein 		if (ret) {
1845fc06573dSJack Morgenstein 			ret = -ENOMEM;
1846b9c5d6a6SOren Duer 			goto err_mcg;
1847fc06573dSJack Morgenstein 		}
1848fc06573dSJack Morgenstein 	}
1849fc06573dSJack Morgenstein 
1850b9c5d6a6SOren Duer 	ret = mlx4_ib_mcg_port_init(ctx);
1851b9c5d6a6SOren Duer 	if (ret) {
1852b9c5d6a6SOren Duer 		pr_err("Failed initializing mcg para-virt (%d)\n", ret);
1853b9c5d6a6SOren Duer 		goto err_mcg;
1854b9c5d6a6SOren Duer 	}
1855b9c5d6a6SOren Duer 
1856fc06573dSJack Morgenstein 	snprintf(name, sizeof name, "mlx4_ibt%d", port);
1857fc06573dSJack Morgenstein 	ctx->wq = create_singlethread_workqueue(name);
1858fc06573dSJack Morgenstein 	if (!ctx->wq) {
1859fc06573dSJack Morgenstein 		pr_err("Failed to create tunnelling WQ for port %d\n", port);
1860fc06573dSJack Morgenstein 		ret = -ENOMEM;
1861fc06573dSJack Morgenstein 		goto err_wq;
1862fc06573dSJack Morgenstein 	}
1863fc06573dSJack Morgenstein 
1864fc06573dSJack Morgenstein 	snprintf(name, sizeof name, "mlx4_ibud%d", port);
1865fc06573dSJack Morgenstein 	ctx->ud_wq = create_singlethread_workqueue(name);
1866fc06573dSJack Morgenstein 	if (!ctx->ud_wq) {
1867fc06573dSJack Morgenstein 		pr_err("Failed to create up/down WQ for port %d\n", port);
1868fc06573dSJack Morgenstein 		ret = -ENOMEM;
1869fc06573dSJack Morgenstein 		goto err_udwq;
1870fc06573dSJack Morgenstein 	}
1871fc06573dSJack Morgenstein 
1872fc06573dSJack Morgenstein 	return 0;
1873fc06573dSJack Morgenstein 
1874fc06573dSJack Morgenstein err_udwq:
1875fc06573dSJack Morgenstein 	destroy_workqueue(ctx->wq);
1876fc06573dSJack Morgenstein 	ctx->wq = NULL;
1877fc06573dSJack Morgenstein 
1878fc06573dSJack Morgenstein err_wq:
1879b9c5d6a6SOren Duer 	mlx4_ib_mcg_port_cleanup(ctx, 1);
1880b9c5d6a6SOren Duer err_mcg:
1881fc06573dSJack Morgenstein 	for (i = 0; i < dev->dev->caps.sqp_demux; i++)
1882fc06573dSJack Morgenstein 		free_pv_object(dev, i, port);
1883fc06573dSJack Morgenstein 	kfree(ctx->tun);
1884fc06573dSJack Morgenstein 	ctx->tun = NULL;
1885fc06573dSJack Morgenstein 	return ret;
1886fc06573dSJack Morgenstein }
1887fc06573dSJack Morgenstein 
1888fc06573dSJack Morgenstein static void mlx4_ib_free_sqp_ctx(struct mlx4_ib_demux_pv_ctx *sqp_ctx)
1889fc06573dSJack Morgenstein {
1890fc06573dSJack Morgenstein 	if (sqp_ctx->state > DEMUX_PV_STATE_DOWN) {
1891fc06573dSJack Morgenstein 		sqp_ctx->state = DEMUX_PV_STATE_DOWNING;
1892fc06573dSJack Morgenstein 		flush_workqueue(sqp_ctx->wq);
1893fc06573dSJack Morgenstein 		if (sqp_ctx->has_smi) {
1894fc06573dSJack Morgenstein 			ib_destroy_qp(sqp_ctx->qp[0].qp);
1895fc06573dSJack Morgenstein 			sqp_ctx->qp[0].qp = NULL;
1896fc06573dSJack Morgenstein 			mlx4_ib_free_pv_qp_bufs(sqp_ctx, IB_QPT_SMI, 0);
1897fc06573dSJack Morgenstein 		}
1898fc06573dSJack Morgenstein 		ib_destroy_qp(sqp_ctx->qp[1].qp);
1899fc06573dSJack Morgenstein 		sqp_ctx->qp[1].qp = NULL;
1900fc06573dSJack Morgenstein 		mlx4_ib_free_pv_qp_bufs(sqp_ctx, IB_QPT_GSI, 0);
1901fc06573dSJack Morgenstein 		ib_dereg_mr(sqp_ctx->mr);
1902fc06573dSJack Morgenstein 		sqp_ctx->mr = NULL;
1903fc06573dSJack Morgenstein 		ib_dealloc_pd(sqp_ctx->pd);
1904fc06573dSJack Morgenstein 		sqp_ctx->pd = NULL;
1905fc06573dSJack Morgenstein 		ib_destroy_cq(sqp_ctx->cq);
1906fc06573dSJack Morgenstein 		sqp_ctx->cq = NULL;
1907fc06573dSJack Morgenstein 		sqp_ctx->state = DEMUX_PV_STATE_DOWN;
1908fc06573dSJack Morgenstein 	}
1909fc06573dSJack Morgenstein }
1910fc06573dSJack Morgenstein 
1911fc06573dSJack Morgenstein static void mlx4_ib_free_demux_ctx(struct mlx4_ib_demux_ctx *ctx)
1912fc06573dSJack Morgenstein {
1913fc06573dSJack Morgenstein 	int i;
1914fc06573dSJack Morgenstein 	if (ctx) {
1915fc06573dSJack Morgenstein 		struct mlx4_ib_dev *dev = to_mdev(ctx->ib_dev);
1916b9c5d6a6SOren Duer 		mlx4_ib_mcg_port_cleanup(ctx, 1);
1917fc06573dSJack Morgenstein 		for (i = 0; i < dev->dev->caps.sqp_demux; i++) {
1918fc06573dSJack Morgenstein 			if (!ctx->tun[i])
1919fc06573dSJack Morgenstein 				continue;
1920fc06573dSJack Morgenstein 			if (ctx->tun[i]->state > DEMUX_PV_STATE_DOWN)
1921fc06573dSJack Morgenstein 				ctx->tun[i]->state = DEMUX_PV_STATE_DOWNING;
1922fc06573dSJack Morgenstein 		}
1923fc06573dSJack Morgenstein 		flush_workqueue(ctx->wq);
1924fc06573dSJack Morgenstein 		for (i = 0; i < dev->dev->caps.sqp_demux; i++) {
1925fc06573dSJack Morgenstein 			destroy_pv_resources(dev, i, ctx->port, ctx->tun[i], 0);
1926fc06573dSJack Morgenstein 			free_pv_object(dev, i, ctx->port);
1927fc06573dSJack Morgenstein 		}
1928fc06573dSJack Morgenstein 		kfree(ctx->tun);
1929fc06573dSJack Morgenstein 		destroy_workqueue(ctx->ud_wq);
1930fc06573dSJack Morgenstein 		destroy_workqueue(ctx->wq);
1931fc06573dSJack Morgenstein 	}
1932fc06573dSJack Morgenstein }
1933fc06573dSJack Morgenstein 
1934fc06573dSJack Morgenstein static void mlx4_ib_master_tunnels(struct mlx4_ib_dev *dev, int do_init)
1935fc06573dSJack Morgenstein {
1936fc06573dSJack Morgenstein 	int i;
1937fc06573dSJack Morgenstein 
1938fc06573dSJack Morgenstein 	if (!mlx4_is_master(dev->dev))
1939fc06573dSJack Morgenstein 		return;
1940fc06573dSJack Morgenstein 	/* initialize or tear down tunnel QPs for the master */
1941fc06573dSJack Morgenstein 	for (i = 0; i < dev->dev->caps.num_ports; i++)
1942fc06573dSJack Morgenstein 		mlx4_ib_tunnels_update(dev, mlx4_master_func_num(dev->dev), i + 1, do_init);
1943fc06573dSJack Morgenstein 	return;
1944fc06573dSJack Morgenstein }
1945fc06573dSJack Morgenstein 
1946fc06573dSJack Morgenstein int mlx4_ib_init_sriov(struct mlx4_ib_dev *dev)
1947fc06573dSJack Morgenstein {
1948fc06573dSJack Morgenstein 	int i = 0;
1949fc06573dSJack Morgenstein 	int err;
1950fc06573dSJack Morgenstein 
1951fc06573dSJack Morgenstein 	if (!mlx4_is_mfunc(dev->dev))
1952fc06573dSJack Morgenstein 		return 0;
1953fc06573dSJack Morgenstein 
1954fc06573dSJack Morgenstein 	dev->sriov.is_going_down = 0;
1955fc06573dSJack Morgenstein 	spin_lock_init(&dev->sriov.going_down_lock);
19563cf69cc8SAmir Vadai 	mlx4_ib_cm_paravirt_init(dev);
1957fc06573dSJack Morgenstein 
1958fc06573dSJack Morgenstein 	mlx4_ib_warn(&dev->ib_dev, "multi-function enabled\n");
1959fc06573dSJack Morgenstein 
1960fc06573dSJack Morgenstein 	if (mlx4_is_slave(dev->dev)) {
1961fc06573dSJack Morgenstein 		mlx4_ib_warn(&dev->ib_dev, "operating in qp1 tunnel mode\n");
1962fc06573dSJack Morgenstein 		return 0;
1963fc06573dSJack Morgenstein 	}
1964fc06573dSJack Morgenstein 
1965a0c64a17SJack Morgenstein 	err = mlx4_ib_init_alias_guid_service(dev);
1966a0c64a17SJack Morgenstein 	if (err) {
1967a0c64a17SJack Morgenstein 		mlx4_ib_warn(&dev->ib_dev, "Failed init alias guid process.\n");
1968a0c64a17SJack Morgenstein 		goto paravirt_err;
1969a0c64a17SJack Morgenstein 	}
1970c1e7e466SJack Morgenstein 	err = mlx4_ib_device_register_sysfs(dev);
1971c1e7e466SJack Morgenstein 	if (err) {
1972c1e7e466SJack Morgenstein 		mlx4_ib_warn(&dev->ib_dev, "Failed to register sysfs\n");
1973c1e7e466SJack Morgenstein 		goto sysfs_err;
1974c1e7e466SJack Morgenstein 	}
1975a0c64a17SJack Morgenstein 
1976fc06573dSJack Morgenstein 	mlx4_ib_warn(&dev->ib_dev, "initializing demux service for %d qp1 clients\n",
1977fc06573dSJack Morgenstein 		     dev->dev->caps.sqp_demux);
1978fc06573dSJack Morgenstein 	for (i = 0; i < dev->num_ports; i++) {
1979a0c64a17SJack Morgenstein 		union ib_gid gid;
1980a0c64a17SJack Morgenstein 		err = __mlx4_ib_query_gid(&dev->ib_dev, i + 1, 0, &gid, 1);
1981a0c64a17SJack Morgenstein 		if (err)
1982a0c64a17SJack Morgenstein 			goto demux_err;
1983a0c64a17SJack Morgenstein 		dev->sriov.demux[i].guid_cache[0] = gid.global.interface_id;
1984fc06573dSJack Morgenstein 		err = alloc_pv_object(dev, mlx4_master_func_num(dev->dev), i + 1,
1985fc06573dSJack Morgenstein 				      &dev->sriov.sqps[i]);
1986fc06573dSJack Morgenstein 		if (err)
1987fc06573dSJack Morgenstein 			goto demux_err;
1988fc06573dSJack Morgenstein 		err = mlx4_ib_alloc_demux_ctx(dev, &dev->sriov.demux[i], i + 1);
1989fc06573dSJack Morgenstein 		if (err)
1990fc06573dSJack Morgenstein 			goto demux_err;
1991fc06573dSJack Morgenstein 	}
1992fc06573dSJack Morgenstein 	mlx4_ib_master_tunnels(dev, 1);
1993fc06573dSJack Morgenstein 	return 0;
1994fc06573dSJack Morgenstein 
1995fc06573dSJack Morgenstein demux_err:
1996fc06573dSJack Morgenstein 	while (i > 0) {
1997fc06573dSJack Morgenstein 		free_pv_object(dev, mlx4_master_func_num(dev->dev), i + 1);
1998fc06573dSJack Morgenstein 		mlx4_ib_free_demux_ctx(&dev->sriov.demux[i]);
1999fc06573dSJack Morgenstein 		--i;
2000fc06573dSJack Morgenstein 	}
2001c1e7e466SJack Morgenstein 	mlx4_ib_device_unregister_sysfs(dev);
2002c1e7e466SJack Morgenstein 
2003c1e7e466SJack Morgenstein sysfs_err:
2004a0c64a17SJack Morgenstein 	mlx4_ib_destroy_alias_guid_service(dev);
2005a0c64a17SJack Morgenstein 
2006a0c64a17SJack Morgenstein paravirt_err:
20073cf69cc8SAmir Vadai 	mlx4_ib_cm_paravirt_clean(dev, -1);
2008fc06573dSJack Morgenstein 
2009fc06573dSJack Morgenstein 	return err;
2010fc06573dSJack Morgenstein }
2011fc06573dSJack Morgenstein 
2012fc06573dSJack Morgenstein void mlx4_ib_close_sriov(struct mlx4_ib_dev *dev)
2013fc06573dSJack Morgenstein {
2014fc06573dSJack Morgenstein 	int i;
2015fc06573dSJack Morgenstein 	unsigned long flags;
2016fc06573dSJack Morgenstein 
2017fc06573dSJack Morgenstein 	if (!mlx4_is_mfunc(dev->dev))
2018fc06573dSJack Morgenstein 		return;
2019fc06573dSJack Morgenstein 
2020fc06573dSJack Morgenstein 	spin_lock_irqsave(&dev->sriov.going_down_lock, flags);
2021fc06573dSJack Morgenstein 	dev->sriov.is_going_down = 1;
2022fc06573dSJack Morgenstein 	spin_unlock_irqrestore(&dev->sriov.going_down_lock, flags);
20233cf69cc8SAmir Vadai 	if (mlx4_is_master(dev->dev)) {
2024fc06573dSJack Morgenstein 		for (i = 0; i < dev->num_ports; i++) {
2025fc06573dSJack Morgenstein 			flush_workqueue(dev->sriov.demux[i].ud_wq);
2026fc06573dSJack Morgenstein 			mlx4_ib_free_sqp_ctx(dev->sriov.sqps[i]);
2027fc06573dSJack Morgenstein 			kfree(dev->sriov.sqps[i]);
2028fc06573dSJack Morgenstein 			dev->sriov.sqps[i] = NULL;
2029fc06573dSJack Morgenstein 			mlx4_ib_free_demux_ctx(&dev->sriov.demux[i]);
2030fc06573dSJack Morgenstein 		}
20313cf69cc8SAmir Vadai 
20323cf69cc8SAmir Vadai 		mlx4_ib_cm_paravirt_clean(dev, -1);
2033a0c64a17SJack Morgenstein 		mlx4_ib_destroy_alias_guid_service(dev);
2034c1e7e466SJack Morgenstein 		mlx4_ib_device_unregister_sysfs(dev);
20353cf69cc8SAmir Vadai 	}
2036fc06573dSJack Morgenstein }
2037