xref: /openbmc/linux/drivers/infiniband/hw/mlx4/mad.c (revision 7e24a55b2122746c2eef192296fc84624354f895)
1225c7b1fSRoland Dreier /*
2225c7b1fSRoland Dreier  * Copyright (c) 2007 Cisco Systems, Inc. All rights reserved.
3225c7b1fSRoland Dreier  *
4225c7b1fSRoland Dreier  * This software is available to you under a choice of one of two
5225c7b1fSRoland Dreier  * licenses.  You may choose to be licensed under the terms of the GNU
6225c7b1fSRoland Dreier  * General Public License (GPL) Version 2, available from the file
7225c7b1fSRoland Dreier  * COPYING in the main directory of this source tree, or the
8225c7b1fSRoland Dreier  * OpenIB.org BSD license below:
9225c7b1fSRoland Dreier  *
10225c7b1fSRoland Dreier  *     Redistribution and use in source and binary forms, with or
11225c7b1fSRoland Dreier  *     without modification, are permitted provided that the following
12225c7b1fSRoland Dreier  *     conditions are met:
13225c7b1fSRoland Dreier  *
14225c7b1fSRoland Dreier  *      - Redistributions of source code must retain the above
15225c7b1fSRoland Dreier  *        copyright notice, this list of conditions and the following
16225c7b1fSRoland Dreier  *        disclaimer.
17225c7b1fSRoland Dreier  *
18225c7b1fSRoland Dreier  *      - Redistributions in binary form must reproduce the above
19225c7b1fSRoland Dreier  *        copyright notice, this list of conditions and the following
20225c7b1fSRoland Dreier  *        disclaimer in the documentation and/or other materials
21225c7b1fSRoland Dreier  *        provided with the distribution.
22225c7b1fSRoland Dreier  *
23225c7b1fSRoland Dreier  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24225c7b1fSRoland Dreier  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25225c7b1fSRoland Dreier  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26225c7b1fSRoland Dreier  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
27225c7b1fSRoland Dreier  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
28225c7b1fSRoland Dreier  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
29225c7b1fSRoland Dreier  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
30225c7b1fSRoland Dreier  * SOFTWARE.
31225c7b1fSRoland Dreier  */
32225c7b1fSRoland Dreier 
33225c7b1fSRoland Dreier #include <rdma/ib_mad.h>
34225c7b1fSRoland Dreier #include <rdma/ib_smi.h>
3537bfc7c1SJack Morgenstein #include <rdma/ib_sa.h>
3637bfc7c1SJack Morgenstein #include <rdma/ib_cache.h>
37225c7b1fSRoland Dreier 
38afa8fd1dSJack Morgenstein #include <linux/random.h>
39225c7b1fSRoland Dreier #include <linux/mlx4/cmd.h>
405a0e3ad6STejun Heo #include <linux/gfp.h>
41c3779134SOr Gerlitz #include <rdma/ib_pma.h>
42850d8fd7SMoni Shoua #include <linux/ip.h>
43850d8fd7SMoni Shoua #include <net/ipv6.h>
44225c7b1fSRoland Dreier 
45f1b4e12aSOr Gerlitz #include <linux/mlx4/driver.h>
46225c7b1fSRoland Dreier #include "mlx4_ib.h"
47225c7b1fSRoland Dreier 
48225c7b1fSRoland Dreier enum {
49225c7b1fSRoland Dreier 	MLX4_IB_VENDOR_CLASS1 = 0x9,
50225c7b1fSRoland Dreier 	MLX4_IB_VENDOR_CLASS2 = 0xa
51225c7b1fSRoland Dreier };
52225c7b1fSRoland Dreier 
53fc06573dSJack Morgenstein #define MLX4_TUN_SEND_WRID_SHIFT 34
54fc06573dSJack Morgenstein #define MLX4_TUN_QPN_SHIFT 32
55fc06573dSJack Morgenstein #define MLX4_TUN_WRID_RECV (((u64) 1) << MLX4_TUN_SEND_WRID_SHIFT)
56fc06573dSJack Morgenstein #define MLX4_TUN_SET_WRID_QPN(a) (((u64) ((a) & 0x3)) << MLX4_TUN_QPN_SHIFT)
57fc06573dSJack Morgenstein 
58fc06573dSJack Morgenstein #define MLX4_TUN_IS_RECV(a)  (((a) >>  MLX4_TUN_SEND_WRID_SHIFT) & 0x1)
59fc06573dSJack Morgenstein #define MLX4_TUN_WRID_QPN(a) (((a) >> MLX4_TUN_QPN_SHIFT) & 0x3)
60fc06573dSJack Morgenstein 
612a4fae14SJack Morgenstein  /* Port mgmt change event handling */
622a4fae14SJack Morgenstein 
632a4fae14SJack Morgenstein #define GET_BLK_PTR_FROM_EQE(eqe) be32_to_cpu(eqe->event.port_mgmt_change.params.tbl_change_info.block_ptr)
642a4fae14SJack Morgenstein #define GET_MASK_FROM_EQE(eqe) be32_to_cpu(eqe->event.port_mgmt_change.params.tbl_change_info.tbl_entries_mask)
652a4fae14SJack Morgenstein #define NUM_IDX_IN_PKEY_TBL_BLK 32
662a4fae14SJack Morgenstein #define GUID_TBL_ENTRY_SIZE 8	   /* size in bytes */
672a4fae14SJack Morgenstein #define GUID_TBL_BLK_NUM_ENTRIES 8
682a4fae14SJack Morgenstein #define GUID_TBL_BLK_SIZE (GUID_TBL_ENTRY_SIZE * GUID_TBL_BLK_NUM_ENTRIES)
692a4fae14SJack Morgenstein 
70fc06573dSJack Morgenstein struct mlx4_mad_rcv_buf {
71fc06573dSJack Morgenstein 	struct ib_grh grh;
72fc06573dSJack Morgenstein 	u8 payload[256];
73fc06573dSJack Morgenstein } __packed;
74fc06573dSJack Morgenstein 
75fc06573dSJack Morgenstein struct mlx4_mad_snd_buf {
76fc06573dSJack Morgenstein 	u8 payload[256];
77fc06573dSJack Morgenstein } __packed;
78fc06573dSJack Morgenstein 
79fc06573dSJack Morgenstein struct mlx4_tunnel_mad {
80fc06573dSJack Morgenstein 	struct ib_grh grh;
81fc06573dSJack Morgenstein 	struct mlx4_ib_tunnel_header hdr;
82fc06573dSJack Morgenstein 	struct ib_mad mad;
83fc06573dSJack Morgenstein } __packed;
84fc06573dSJack Morgenstein 
85fc06573dSJack Morgenstein struct mlx4_rcv_tunnel_mad {
86fc06573dSJack Morgenstein 	struct mlx4_rcv_tunnel_hdr hdr;
87fc06573dSJack Morgenstein 	struct ib_grh grh;
88fc06573dSJack Morgenstein 	struct ib_mad mad;
89fc06573dSJack Morgenstein } __packed;
90fc06573dSJack Morgenstein 
911fb7f897SMark Bloch static void handle_client_rereg_event(struct mlx4_ib_dev *dev, u32 port_num);
921fb7f897SMark Bloch static void handle_lid_change_event(struct mlx4_ib_dev *dev, u32 port_num);
932a4fae14SJack Morgenstein static void __propagate_pkey_ev(struct mlx4_ib_dev *dev, int port_num,
942a4fae14SJack Morgenstein 				int block, u32 change_bitmap);
95b9c5d6a6SOren Duer 
mlx4_ib_gen_node_guid(void)96afa8fd1dSJack Morgenstein __be64 mlx4_ib_gen_node_guid(void)
97afa8fd1dSJack Morgenstein {
98afa8fd1dSJack Morgenstein #define NODE_GUID_HI	((u64) (((u64)IB_OPENIB_OUI) << 40))
99a251c17aSJason A. Donenfeld 	return cpu_to_be64(NODE_GUID_HI | get_random_u32());
100afa8fd1dSJack Morgenstein }
101afa8fd1dSJack Morgenstein 
mlx4_ib_get_new_demux_tid(struct mlx4_ib_demux_ctx * ctx)102b9c5d6a6SOren Duer __be64 mlx4_ib_get_new_demux_tid(struct mlx4_ib_demux_ctx *ctx)
103b9c5d6a6SOren Duer {
104b9c5d6a6SOren Duer 	return cpu_to_be64(atomic_inc_return(&ctx->tid)) |
105b9c5d6a6SOren Duer 		cpu_to_be64(0xff00000000000000LL);
106b9c5d6a6SOren Duer }
107b9c5d6a6SOren Duer 
mlx4_MAD_IFC(struct mlx4_ib_dev * dev,int mad_ifc_flags,int port,const struct ib_wc * in_wc,const struct ib_grh * in_grh,const void * in_mad,void * response_mad)1080a9a0188SJack Morgenstein int mlx4_MAD_IFC(struct mlx4_ib_dev *dev, int mad_ifc_flags,
109a97e2d86SIra Weiny 		 int port, const struct ib_wc *in_wc,
110a97e2d86SIra Weiny 		 const struct ib_grh *in_grh,
111a97e2d86SIra Weiny 		 const void *in_mad, void *response_mad)
112225c7b1fSRoland Dreier {
113225c7b1fSRoland Dreier 	struct mlx4_cmd_mailbox *inmailbox, *outmailbox;
114225c7b1fSRoland Dreier 	void *inbox;
115225c7b1fSRoland Dreier 	int err;
116225c7b1fSRoland Dreier 	u32 in_modifier = port;
117225c7b1fSRoland Dreier 	u8 op_modifier = 0;
118225c7b1fSRoland Dreier 
119225c7b1fSRoland Dreier 	inmailbox = mlx4_alloc_cmd_mailbox(dev->dev);
120225c7b1fSRoland Dreier 	if (IS_ERR(inmailbox))
121225c7b1fSRoland Dreier 		return PTR_ERR(inmailbox);
122225c7b1fSRoland Dreier 	inbox = inmailbox->buf;
123225c7b1fSRoland Dreier 
124225c7b1fSRoland Dreier 	outmailbox = mlx4_alloc_cmd_mailbox(dev->dev);
125225c7b1fSRoland Dreier 	if (IS_ERR(outmailbox)) {
126225c7b1fSRoland Dreier 		mlx4_free_cmd_mailbox(dev->dev, inmailbox);
127225c7b1fSRoland Dreier 		return PTR_ERR(outmailbox);
128225c7b1fSRoland Dreier 	}
129225c7b1fSRoland Dreier 
130225c7b1fSRoland Dreier 	memcpy(inbox, in_mad, 256);
131225c7b1fSRoland Dreier 
132225c7b1fSRoland Dreier 	/*
133225c7b1fSRoland Dreier 	 * Key check traps can't be generated unless we have in_wc to
134225c7b1fSRoland Dreier 	 * tell us where to send the trap.
135225c7b1fSRoland Dreier 	 */
1360a9a0188SJack Morgenstein 	if ((mad_ifc_flags & MLX4_MAD_IFC_IGNORE_MKEY) || !in_wc)
137225c7b1fSRoland Dreier 		op_modifier |= 0x1;
1380a9a0188SJack Morgenstein 	if ((mad_ifc_flags & MLX4_MAD_IFC_IGNORE_BKEY) || !in_wc)
139225c7b1fSRoland Dreier 		op_modifier |= 0x2;
1400a9a0188SJack Morgenstein 	if (mlx4_is_mfunc(dev->dev) &&
1410a9a0188SJack Morgenstein 	    (mad_ifc_flags & MLX4_MAD_IFC_NET_VIEW || in_wc))
1420a9a0188SJack Morgenstein 		op_modifier |= 0x8;
143225c7b1fSRoland Dreier 
144225c7b1fSRoland Dreier 	if (in_wc) {
145225c7b1fSRoland Dreier 		struct {
146225c7b1fSRoland Dreier 			__be32		my_qpn;
147225c7b1fSRoland Dreier 			u32		reserved1;
148225c7b1fSRoland Dreier 			__be32		rqpn;
149225c7b1fSRoland Dreier 			u8		sl;
150225c7b1fSRoland Dreier 			u8		g_path;
151225c7b1fSRoland Dreier 			u16		reserved2[2];
152225c7b1fSRoland Dreier 			__be16		pkey;
153225c7b1fSRoland Dreier 			u32		reserved3[11];
154225c7b1fSRoland Dreier 			u8		grh[40];
155225c7b1fSRoland Dreier 		} *ext_info;
156225c7b1fSRoland Dreier 
157225c7b1fSRoland Dreier 		memset(inbox + 256, 0, 256);
158225c7b1fSRoland Dreier 		ext_info = inbox + 256;
159225c7b1fSRoland Dreier 
160225c7b1fSRoland Dreier 		ext_info->my_qpn = cpu_to_be32(in_wc->qp->qp_num);
161225c7b1fSRoland Dreier 		ext_info->rqpn   = cpu_to_be32(in_wc->src_qp);
162225c7b1fSRoland Dreier 		ext_info->sl     = in_wc->sl << 4;
163225c7b1fSRoland Dreier 		ext_info->g_path = in_wc->dlid_path_bits |
164225c7b1fSRoland Dreier 			(in_wc->wc_flags & IB_WC_GRH ? 0x80 : 0);
165225c7b1fSRoland Dreier 		ext_info->pkey   = cpu_to_be16(in_wc->pkey_index);
166225c7b1fSRoland Dreier 
167225c7b1fSRoland Dreier 		if (in_grh)
168225c7b1fSRoland Dreier 			memcpy(ext_info->grh, in_grh, 40);
169225c7b1fSRoland Dreier 
170225c7b1fSRoland Dreier 		op_modifier |= 0x4;
171225c7b1fSRoland Dreier 
17262ede777SHiatt, Don 		in_modifier |= ib_lid_cpu16(in_wc->slid) << 16;
173225c7b1fSRoland Dreier 	}
174225c7b1fSRoland Dreier 
1750a9a0188SJack Morgenstein 	err = mlx4_cmd_box(dev->dev, inmailbox->dma, outmailbox->dma, in_modifier,
1760a9a0188SJack Morgenstein 			   mlx4_is_master(dev->dev) ? (op_modifier & ~0x8) : op_modifier,
177f9baff50SJack Morgenstein 			   MLX4_CMD_MAD_IFC, MLX4_CMD_TIME_CLASS_C,
1780a9a0188SJack Morgenstein 			   (op_modifier & 0x8) ? MLX4_CMD_NATIVE : MLX4_CMD_WRAPPED);
179225c7b1fSRoland Dreier 
180fe11cb6bSIlpo Järvinen 	if (!err)
181225c7b1fSRoland Dreier 		memcpy(response_mad, outmailbox->buf, 256);
182225c7b1fSRoland Dreier 
183225c7b1fSRoland Dreier 	mlx4_free_cmd_mailbox(dev->dev, inmailbox);
184225c7b1fSRoland Dreier 	mlx4_free_cmd_mailbox(dev->dev, outmailbox);
185225c7b1fSRoland Dreier 
186225c7b1fSRoland Dreier 	return err;
187225c7b1fSRoland Dreier }
188225c7b1fSRoland Dreier 
update_sm_ah(struct mlx4_ib_dev * dev,u32 port_num,u16 lid,u8 sl)1891fb7f897SMark Bloch static void update_sm_ah(struct mlx4_ib_dev *dev, u32 port_num, u16 lid, u8 sl)
190225c7b1fSRoland Dreier {
191225c7b1fSRoland Dreier 	struct ib_ah *new_ah;
19290898850SDasaratharaman Chandramouli 	struct rdma_ah_attr ah_attr;
193df7fba66SJack Morgenstein 	unsigned long flags;
194225c7b1fSRoland Dreier 
195225c7b1fSRoland Dreier 	if (!dev->send_agent[port_num - 1][0])
196225c7b1fSRoland Dreier 		return;
197225c7b1fSRoland Dreier 
198225c7b1fSRoland Dreier 	memset(&ah_attr, 0, sizeof ah_attr);
19944c58487SDasaratharaman Chandramouli 	ah_attr.type = rdma_ah_find_type(&dev->ib_dev, port_num);
200d8966fcdSDasaratharaman Chandramouli 	rdma_ah_set_dlid(&ah_attr, lid);
201d8966fcdSDasaratharaman Chandramouli 	rdma_ah_set_sl(&ah_attr, sl);
202d8966fcdSDasaratharaman Chandramouli 	rdma_ah_set_port_num(&ah_attr, port_num);
203225c7b1fSRoland Dreier 
2040a18cfe4SDasaratharaman Chandramouli 	new_ah = rdma_create_ah(dev->send_agent[port_num - 1][0]->qp->pd,
205b090c4e3SGal Pressman 				&ah_attr, 0);
206225c7b1fSRoland Dreier 	if (IS_ERR(new_ah))
207225c7b1fSRoland Dreier 		return;
208225c7b1fSRoland Dreier 
209df7fba66SJack Morgenstein 	spin_lock_irqsave(&dev->sm_lock, flags);
210225c7b1fSRoland Dreier 	if (dev->sm_ah[port_num - 1])
2112553ba21SGal Pressman 		rdma_destroy_ah(dev->sm_ah[port_num - 1], 0);
212225c7b1fSRoland Dreier 	dev->sm_ah[port_num - 1] = new_ah;
213df7fba66SJack Morgenstein 	spin_unlock_irqrestore(&dev->sm_lock, flags);
214225c7b1fSRoland Dreier }
215225c7b1fSRoland Dreier 
216225c7b1fSRoland Dreier /*
21700f5ce99SJack Morgenstein  * Snoop SM MADs for port info, GUID info, and  P_Key table sets, so we can
21800f5ce99SJack Morgenstein  * synthesize LID change, Client-Rereg, GID change, and P_Key change events.
219225c7b1fSRoland Dreier  */
smp_snoop(struct ib_device * ibdev,u32 port_num,const struct ib_mad * mad,u16 prev_lid)2201fb7f897SMark Bloch static void smp_snoop(struct ib_device *ibdev, u32 port_num,
2211fb7f897SMark Bloch 		      const struct ib_mad *mad, u16 prev_lid)
222225c7b1fSRoland Dreier {
22300f5ce99SJack Morgenstein 	struct ib_port_info *pinfo;
22400f5ce99SJack Morgenstein 	u16 lid;
22554679e14SJack Morgenstein 	__be16 *base;
22654679e14SJack Morgenstein 	u32 bn, pkey_change_bitmap;
22754679e14SJack Morgenstein 	int i;
22854679e14SJack Morgenstein 
229225c7b1fSRoland Dreier 
23000f5ce99SJack Morgenstein 	struct mlx4_ib_dev *dev = to_mdev(ibdev);
231225c7b1fSRoland Dreier 	if ((mad->mad_hdr.mgmt_class == IB_MGMT_CLASS_SUBN_LID_ROUTED ||
232225c7b1fSRoland Dreier 	     mad->mad_hdr.mgmt_class == IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE) &&
23300f5ce99SJack Morgenstein 	    mad->mad_hdr.method == IB_MGMT_METHOD_SET)
23400f5ce99SJack Morgenstein 		switch (mad->mad_hdr.attr_id) {
23500f5ce99SJack Morgenstein 		case IB_SMP_ATTR_PORT_INFO:
236fd10ed8eSJack Morgenstein 			if (dev->dev->caps.flags & MLX4_DEV_CAP_FLAG_PORT_MNG_CHG_EV)
237fd10ed8eSJack Morgenstein 				return;
23800f5ce99SJack Morgenstein 			pinfo = (struct ib_port_info *) ((struct ib_smp *) mad)->data;
23900f5ce99SJack Morgenstein 			lid = be16_to_cpu(pinfo->lid);
240225c7b1fSRoland Dreier 
24100f5ce99SJack Morgenstein 			update_sm_ah(dev, port_num,
242225c7b1fSRoland Dreier 				     be16_to_cpu(pinfo->sm_lid),
243225c7b1fSRoland Dreier 				     pinfo->neighbormtu_mastersmsl & 0xf);
244225c7b1fSRoland Dreier 
24500f5ce99SJack Morgenstein 			if (pinfo->clientrereg_resv_subnetto & 0x80)
246b9c5d6a6SOren Duer 				handle_client_rereg_event(dev, port_num);
247225c7b1fSRoland Dreier 
24800f5ce99SJack Morgenstein 			if (prev_lid != lid)
2492a4fae14SJack Morgenstein 				handle_lid_change_event(dev, port_num);
25000f5ce99SJack Morgenstein 			break;
251225c7b1fSRoland Dreier 
25200f5ce99SJack Morgenstein 		case IB_SMP_ATTR_PKEY_TABLE:
253fd10ed8eSJack Morgenstein 			if (dev->dev->caps.flags & MLX4_DEV_CAP_FLAG_PORT_MNG_CHG_EV)
254fd10ed8eSJack Morgenstein 				return;
25554679e14SJack Morgenstein 			if (!mlx4_is_mfunc(dev->dev)) {
25600f5ce99SJack Morgenstein 				mlx4_ib_dispatch_event(dev, port_num,
25700f5ce99SJack Morgenstein 						       IB_EVENT_PKEY_CHANGE);
25800f5ce99SJack Morgenstein 				break;
25954679e14SJack Morgenstein 			}
26054679e14SJack Morgenstein 
2612a4fae14SJack Morgenstein 			/* at this point, we are running in the master.
2622a4fae14SJack Morgenstein 			 * Slaves do not receive SMPs.
2632a4fae14SJack Morgenstein 			 */
26454679e14SJack Morgenstein 			bn  = be32_to_cpu(((struct ib_smp *)mad)->attr_mod) & 0xFFFF;
26554679e14SJack Morgenstein 			base = (__be16 *) &(((struct ib_smp *)mad)->data[0]);
26654679e14SJack Morgenstein 			pkey_change_bitmap = 0;
26754679e14SJack Morgenstein 			for (i = 0; i < 32; i++) {
26854679e14SJack Morgenstein 				pr_debug("PKEY[%d] = x%x\n",
26954679e14SJack Morgenstein 					 i + bn*32, be16_to_cpu(base[i]));
27054679e14SJack Morgenstein 				if (be16_to_cpu(base[i]) !=
27154679e14SJack Morgenstein 				    dev->pkeys.phys_pkey_cache[port_num - 1][i + bn*32]) {
27254679e14SJack Morgenstein 					pkey_change_bitmap |= (1 << i);
27354679e14SJack Morgenstein 					dev->pkeys.phys_pkey_cache[port_num - 1][i + bn*32] =
27454679e14SJack Morgenstein 						be16_to_cpu(base[i]);
27554679e14SJack Morgenstein 				}
27654679e14SJack Morgenstein 			}
2771fb7f897SMark Bloch 			pr_debug("PKEY Change event: port=%u, "
27854679e14SJack Morgenstein 				 "block=0x%x, change_bitmap=0x%x\n",
27954679e14SJack Morgenstein 				 port_num, bn, pkey_change_bitmap);
28054679e14SJack Morgenstein 
2812a4fae14SJack Morgenstein 			if (pkey_change_bitmap) {
28254679e14SJack Morgenstein 				mlx4_ib_dispatch_event(dev, port_num,
28354679e14SJack Morgenstein 						       IB_EVENT_PKEY_CHANGE);
2842a4fae14SJack Morgenstein 				if (!dev->sriov.is_going_down)
2852a4fae14SJack Morgenstein 					__propagate_pkey_ev(dev, port_num, bn,
2862a4fae14SJack Morgenstein 							    pkey_change_bitmap);
2872a4fae14SJack Morgenstein 			}
28854679e14SJack Morgenstein 			break;
289f0f6f346SMoni Shoua 
29000f5ce99SJack Morgenstein 		case IB_SMP_ATTR_GUID_INFO:
291fd10ed8eSJack Morgenstein 			if (dev->dev->caps.flags & MLX4_DEV_CAP_FLAG_PORT_MNG_CHG_EV)
292fd10ed8eSJack Morgenstein 				return;
2936634961cSJack Morgenstein 			/* paravirtualized master's guid is guid 0 -- does not change */
2946634961cSJack Morgenstein 			if (!mlx4_is_master(dev->dev))
29500f5ce99SJack Morgenstein 				mlx4_ib_dispatch_event(dev, port_num,
29600f5ce99SJack Morgenstein 						       IB_EVENT_GID_CHANGE);
2972a4fae14SJack Morgenstein 			/*if master, notify relevant slaves*/
2982a4fae14SJack Morgenstein 			if (mlx4_is_master(dev->dev) &&
2992a4fae14SJack Morgenstein 			    !dev->sriov.is_going_down) {
3002a4fae14SJack Morgenstein 				bn = be32_to_cpu(((struct ib_smp *)mad)->attr_mod);
3012a4fae14SJack Morgenstein 				mlx4_ib_update_cache_on_guid_change(dev, bn, port_num,
3022a4fae14SJack Morgenstein 								    (u8 *)(&((struct ib_smp *)mad)->data));
3032a4fae14SJack Morgenstein 				mlx4_ib_notify_slaves_on_guid_change(dev, bn, port_num,
3042a4fae14SJack Morgenstein 								     (u8 *)(&((struct ib_smp *)mad)->data));
3052a4fae14SJack Morgenstein 			}
30600f5ce99SJack Morgenstein 			break;
3072a4fae14SJack Morgenstein 
308fd10ed8eSJack Morgenstein 		case IB_SMP_ATTR_SL_TO_VL_TABLE:
309fd10ed8eSJack Morgenstein 			/* cache sl to vl mapping changes for use in
310fd10ed8eSJack Morgenstein 			 * filling QP1 LRH VL field when sending packets
311fd10ed8eSJack Morgenstein 			 */
312fd10ed8eSJack Morgenstein 			if (dev->dev->caps.flags & MLX4_DEV_CAP_FLAG_PORT_MNG_CHG_EV &&
313fd10ed8eSJack Morgenstein 			    dev->dev->caps.flags2 & MLX4_DEV_CAP_FLAG2_SL_TO_VL_CHANGE_EVENT)
314fd10ed8eSJack Morgenstein 				return;
315fd10ed8eSJack Morgenstein 			if (!mlx4_is_slave(dev->dev)) {
316fd10ed8eSJack Morgenstein 				union sl2vl_tbl_to_u64 sl2vl64;
317fd10ed8eSJack Morgenstein 				int jj;
318fd10ed8eSJack Morgenstein 
319fd10ed8eSJack Morgenstein 				for (jj = 0; jj < 8; jj++) {
320fd10ed8eSJack Morgenstein 					sl2vl64.sl8[jj] = ((struct ib_smp *)mad)->data[jj];
321fd10ed8eSJack Morgenstein 					pr_debug("port %u, sl2vl[%d] = %02x\n",
322fd10ed8eSJack Morgenstein 						 port_num, jj, sl2vl64.sl8[jj]);
323fd10ed8eSJack Morgenstein 				}
324fd10ed8eSJack Morgenstein 				atomic64_set(&dev->sl2vl[port_num - 1], sl2vl64.sl64);
325fd10ed8eSJack Morgenstein 			}
326fd10ed8eSJack Morgenstein 			break;
327fd10ed8eSJack Morgenstein 
32800f5ce99SJack Morgenstein 		default:
32900f5ce99SJack Morgenstein 			break;
330225c7b1fSRoland Dreier 		}
331225c7b1fSRoland Dreier }
332225c7b1fSRoland Dreier 
__propagate_pkey_ev(struct mlx4_ib_dev * dev,int port_num,int block,u32 change_bitmap)3332a4fae14SJack Morgenstein static void __propagate_pkey_ev(struct mlx4_ib_dev *dev, int port_num,
3342a4fae14SJack Morgenstein 				int block, u32 change_bitmap)
3352a4fae14SJack Morgenstein {
3362a4fae14SJack Morgenstein 	int i, ix, slave, err;
3372a4fae14SJack Morgenstein 	int have_event = 0;
3382a4fae14SJack Morgenstein 
3392a4fae14SJack Morgenstein 	for (slave = 0; slave < dev->dev->caps.sqp_demux; slave++) {
3402a4fae14SJack Morgenstein 		if (slave == mlx4_master_func_num(dev->dev))
3412a4fae14SJack Morgenstein 			continue;
3422a4fae14SJack Morgenstein 		if (!mlx4_is_slave_active(dev->dev, slave))
3432a4fae14SJack Morgenstein 			continue;
3442a4fae14SJack Morgenstein 
3452a4fae14SJack Morgenstein 		have_event = 0;
3462a4fae14SJack Morgenstein 		for (i = 0; i < 32; i++) {
3472a4fae14SJack Morgenstein 			if (!(change_bitmap & (1 << i)))
3482a4fae14SJack Morgenstein 				continue;
3492a4fae14SJack Morgenstein 			for (ix = 0;
3502a4fae14SJack Morgenstein 			     ix < dev->dev->caps.pkey_table_len[port_num]; ix++) {
3512a4fae14SJack Morgenstein 				if (dev->pkeys.virt2phys_pkey[slave][port_num - 1]
3522a4fae14SJack Morgenstein 				    [ix] == i + 32 * block) {
3532a4fae14SJack Morgenstein 					err = mlx4_gen_pkey_eqe(dev->dev, slave, port_num);
3542a4fae14SJack Morgenstein 					pr_debug("propagate_pkey_ev: slave %d,"
3552a4fae14SJack Morgenstein 						 " port %d, ix %d (%d)\n",
3562a4fae14SJack Morgenstein 						 slave, port_num, ix, err);
3572a4fae14SJack Morgenstein 					have_event = 1;
3582a4fae14SJack Morgenstein 					break;
3592a4fae14SJack Morgenstein 				}
3602a4fae14SJack Morgenstein 			}
3612a4fae14SJack Morgenstein 			if (have_event)
3622a4fae14SJack Morgenstein 				break;
3632a4fae14SJack Morgenstein 		}
3642a4fae14SJack Morgenstein 	}
3652a4fae14SJack Morgenstein }
3662a4fae14SJack Morgenstein 
node_desc_override(struct ib_device * dev,struct ib_mad * mad)367225c7b1fSRoland Dreier static void node_desc_override(struct ib_device *dev,
368225c7b1fSRoland Dreier 			       struct ib_mad *mad)
369225c7b1fSRoland Dreier {
370df7fba66SJack Morgenstein 	unsigned long flags;
371df7fba66SJack Morgenstein 
372225c7b1fSRoland Dreier 	if ((mad->mad_hdr.mgmt_class == IB_MGMT_CLASS_SUBN_LID_ROUTED ||
373225c7b1fSRoland Dreier 	     mad->mad_hdr.mgmt_class == IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE) &&
374225c7b1fSRoland Dreier 	    mad->mad_hdr.method == IB_MGMT_METHOD_GET_RESP &&
375225c7b1fSRoland Dreier 	    mad->mad_hdr.attr_id == IB_SMP_ATTR_NODE_DESC) {
376df7fba66SJack Morgenstein 		spin_lock_irqsave(&to_mdev(dev)->sm_lock, flags);
377bd99fdeaSYuval Shaia 		memcpy(((struct ib_smp *) mad)->data, dev->node_desc,
378bd99fdeaSYuval Shaia 		       IB_DEVICE_NODE_DESC_MAX);
379df7fba66SJack Morgenstein 		spin_unlock_irqrestore(&to_mdev(dev)->sm_lock, flags);
380225c7b1fSRoland Dreier 	}
381225c7b1fSRoland Dreier }
382225c7b1fSRoland Dreier 
forward_trap(struct mlx4_ib_dev * dev,u32 port_num,const struct ib_mad * mad)3831fb7f897SMark Bloch static void forward_trap(struct mlx4_ib_dev *dev, u32 port_num,
3841fb7f897SMark Bloch 			 const struct ib_mad *mad)
385225c7b1fSRoland Dreier {
386225c7b1fSRoland Dreier 	int qpn = mad->mad_hdr.mgmt_class != IB_MGMT_CLASS_SUBN_LID_ROUTED;
387225c7b1fSRoland Dreier 	struct ib_mad_send_buf *send_buf;
388225c7b1fSRoland Dreier 	struct ib_mad_agent *agent = dev->send_agent[port_num - 1][qpn];
389225c7b1fSRoland Dreier 	int ret;
390df7fba66SJack Morgenstein 	unsigned long flags;
391225c7b1fSRoland Dreier 
392225c7b1fSRoland Dreier 	if (agent) {
393225c7b1fSRoland Dreier 		send_buf = ib_create_send_mad(agent, qpn, 0, 0, IB_MGMT_MAD_HDR,
394da2dfaa3SIra Weiny 					      IB_MGMT_MAD_DATA, GFP_ATOMIC,
395da2dfaa3SIra Weiny 					      IB_MGMT_BASE_VERSION);
39613974909SDan Carpenter 		if (IS_ERR(send_buf))
39713974909SDan Carpenter 			return;
398225c7b1fSRoland Dreier 		/*
399225c7b1fSRoland Dreier 		 * We rely here on the fact that MLX QPs don't use the
400225c7b1fSRoland Dreier 		 * address handle after the send is posted (this is
401225c7b1fSRoland Dreier 		 * wrong following the IB spec strictly, but we know
402225c7b1fSRoland Dreier 		 * it's OK for our devices).
403225c7b1fSRoland Dreier 		 */
404df7fba66SJack Morgenstein 		spin_lock_irqsave(&dev->sm_lock, flags);
405225c7b1fSRoland Dreier 		memcpy(send_buf->mad, mad, sizeof *mad);
406225c7b1fSRoland Dreier 		if ((send_buf->ah = dev->sm_ah[port_num - 1]))
407225c7b1fSRoland Dreier 			ret = ib_post_send_mad(send_buf, NULL);
408225c7b1fSRoland Dreier 		else
409225c7b1fSRoland Dreier 			ret = -EINVAL;
410df7fba66SJack Morgenstein 		spin_unlock_irqrestore(&dev->sm_lock, flags);
411225c7b1fSRoland Dreier 
412225c7b1fSRoland Dreier 		if (ret)
413225c7b1fSRoland Dreier 			ib_free_send_mad(send_buf);
414225c7b1fSRoland Dreier 	}
415225c7b1fSRoland Dreier }
416225c7b1fSRoland Dreier 
mlx4_ib_demux_sa_handler(struct ib_device * ibdev,int port,int slave,struct ib_sa_mad * sa_mad)41737bfc7c1SJack Morgenstein static int mlx4_ib_demux_sa_handler(struct ib_device *ibdev, int port, int slave,
41837bfc7c1SJack Morgenstein 							     struct ib_sa_mad *sa_mad)
41937bfc7c1SJack Morgenstein {
420b9c5d6a6SOren Duer 	int ret = 0;
421b9c5d6a6SOren Duer 
422b9c5d6a6SOren Duer 	/* dispatch to different sa handlers */
423b9c5d6a6SOren Duer 	switch (be16_to_cpu(sa_mad->mad_hdr.attr_id)) {
424b9c5d6a6SOren Duer 	case IB_SA_ATTR_MC_MEMBER_REC:
425b9c5d6a6SOren Duer 		ret = mlx4_ib_mcg_demux_handler(ibdev, port, slave, sa_mad);
426b9c5d6a6SOren Duer 		break;
427b9c5d6a6SOren Duer 	default:
428b9c5d6a6SOren Duer 		break;
429b9c5d6a6SOren Duer 	}
430b9c5d6a6SOren Duer 	return ret;
43137bfc7c1SJack Morgenstein }
43237bfc7c1SJack Morgenstein 
mlx4_ib_find_real_gid(struct ib_device * ibdev,u32 port,__be64 guid)4331fb7f897SMark Bloch int mlx4_ib_find_real_gid(struct ib_device *ibdev, u32 port, __be64 guid)
43437bfc7c1SJack Morgenstein {
43537bfc7c1SJack Morgenstein 	struct mlx4_ib_dev *dev = to_mdev(ibdev);
43637bfc7c1SJack Morgenstein 	int i;
43737bfc7c1SJack Morgenstein 
43837bfc7c1SJack Morgenstein 	for (i = 0; i < dev->dev->caps.sqp_demux; i++) {
43937bfc7c1SJack Morgenstein 		if (dev->sriov.demux[port - 1].guid_cache[i] == guid)
44037bfc7c1SJack Morgenstein 			return i;
44137bfc7c1SJack Morgenstein 	}
44237bfc7c1SJack Morgenstein 	return -1;
44337bfc7c1SJack Morgenstein }
44437bfc7c1SJack Morgenstein 
44537bfc7c1SJack Morgenstein 
find_slave_port_pkey_ix(struct mlx4_ib_dev * dev,int slave,u32 port,u16 pkey,u16 * ix)4462c75d2ccSJack Morgenstein static int find_slave_port_pkey_ix(struct mlx4_ib_dev *dev, int slave,
4471fb7f897SMark Bloch 				   u32 port, u16 pkey, u16 *ix)
44837bfc7c1SJack Morgenstein {
4492c75d2ccSJack Morgenstein 	int i, ret;
4502c75d2ccSJack Morgenstein 	u8 unassigned_pkey_ix, pkey_ix, partial_ix = 0xFF;
4512c75d2ccSJack Morgenstein 	u16 slot_pkey;
45237bfc7c1SJack Morgenstein 
4532c75d2ccSJack Morgenstein 	if (slave == mlx4_master_func_num(dev->dev))
4542c75d2ccSJack Morgenstein 		return ib_find_cached_pkey(&dev->ib_dev, port, pkey, ix);
45537bfc7c1SJack Morgenstein 
4562c75d2ccSJack Morgenstein 	unassigned_pkey_ix = dev->dev->phys_caps.pkey_phys_table_len[port] - 1;
4572c75d2ccSJack Morgenstein 
4582c75d2ccSJack Morgenstein 	for (i = 0; i < dev->dev->caps.pkey_table_len[port]; i++) {
4592c75d2ccSJack Morgenstein 		if (dev->pkeys.virt2phys_pkey[slave][port - 1][i] == unassigned_pkey_ix)
4602c75d2ccSJack Morgenstein 			continue;
4612c75d2ccSJack Morgenstein 
4622c75d2ccSJack Morgenstein 		pkey_ix = dev->pkeys.virt2phys_pkey[slave][port - 1][i];
4632c75d2ccSJack Morgenstein 
4642c75d2ccSJack Morgenstein 		ret = ib_get_cached_pkey(&dev->ib_dev, port, pkey_ix, &slot_pkey);
4652c75d2ccSJack Morgenstein 		if (ret)
4662c75d2ccSJack Morgenstein 			continue;
4672c75d2ccSJack Morgenstein 		if ((slot_pkey & 0x7FFF) == (pkey & 0x7FFF)) {
4682c75d2ccSJack Morgenstein 			if (slot_pkey & 0x8000) {
4692c75d2ccSJack Morgenstein 				*ix = (u16) pkey_ix;
4702c75d2ccSJack Morgenstein 				return 0;
47137bfc7c1SJack Morgenstein 			} else {
4722c75d2ccSJack Morgenstein 				/* take first partial pkey index found */
4732c75d2ccSJack Morgenstein 				if (partial_ix == 0xFF)
4742c75d2ccSJack Morgenstein 					partial_ix = pkey_ix;
4752c75d2ccSJack Morgenstein 			}
4762c75d2ccSJack Morgenstein 		}
47737bfc7c1SJack Morgenstein 	}
47837bfc7c1SJack Morgenstein 
4792c75d2ccSJack Morgenstein 	if (partial_ix < 0xFF) {
4802c75d2ccSJack Morgenstein 		*ix = (u16) partial_ix;
4812c75d2ccSJack Morgenstein 		return 0;
4822c75d2ccSJack Morgenstein 	}
48337bfc7c1SJack Morgenstein 
4842c75d2ccSJack Morgenstein 	return -EINVAL;
48537bfc7c1SJack Morgenstein }
48637bfc7c1SJack Morgenstein 
get_gids_from_l3_hdr(struct ib_grh * grh,union ib_gid * sgid,union ib_gid * dgid)487850d8fd7SMoni Shoua static int get_gids_from_l3_hdr(struct ib_grh *grh, union ib_gid *sgid,
488850d8fd7SMoni Shoua 				union ib_gid *dgid)
489850d8fd7SMoni Shoua {
490850d8fd7SMoni Shoua 	int version = ib_get_rdma_header_version((const union rdma_network_hdr *)grh);
491850d8fd7SMoni Shoua 	enum rdma_network_type net_type;
492850d8fd7SMoni Shoua 
493850d8fd7SMoni Shoua 	if (version == 4)
494850d8fd7SMoni Shoua 		net_type = RDMA_NETWORK_IPV4;
495850d8fd7SMoni Shoua 	else if (version == 6)
496850d8fd7SMoni Shoua 		net_type = RDMA_NETWORK_IPV6;
497850d8fd7SMoni Shoua 	else
498850d8fd7SMoni Shoua 		return -EINVAL;
499850d8fd7SMoni Shoua 
500850d8fd7SMoni Shoua 	return ib_get_gids_from_rdma_hdr((union rdma_network_hdr *)grh, net_type,
501850d8fd7SMoni Shoua 					 sgid, dgid);
502850d8fd7SMoni Shoua }
503850d8fd7SMoni Shoua 
is_proxy_qp0(struct mlx4_ib_dev * dev,int qpn,int slave)50409461944SHåkon Bugge static int is_proxy_qp0(struct mlx4_ib_dev *dev, int qpn, int slave)
50509461944SHåkon Bugge {
50609461944SHåkon Bugge 	int proxy_start = dev->dev->phys_caps.base_proxy_sqpn + 8 * slave;
50709461944SHåkon Bugge 
50809461944SHåkon Bugge 	return (qpn >= proxy_start && qpn <= proxy_start + 1);
50909461944SHåkon Bugge }
51009461944SHåkon Bugge 
mlx4_ib_send_to_slave(struct mlx4_ib_dev * dev,int slave,u32 port,enum ib_qp_type dest_qpt,struct ib_wc * wc,struct ib_grh * grh,struct ib_mad * mad)5111fb7f897SMark Bloch int mlx4_ib_send_to_slave(struct mlx4_ib_dev *dev, int slave, u32 port,
51237bfc7c1SJack Morgenstein 			  enum ib_qp_type dest_qpt, struct ib_wc *wc,
51337bfc7c1SJack Morgenstein 			  struct ib_grh *grh, struct ib_mad *mad)
51437bfc7c1SJack Morgenstein {
51537bfc7c1SJack Morgenstein 	struct ib_sge list;
516e622f2f4SChristoph Hellwig 	struct ib_ud_wr wr;
517d34ac5cdSBart Van Assche 	const struct ib_send_wr *bad_wr;
51837bfc7c1SJack Morgenstein 	struct mlx4_ib_demux_pv_ctx *tun_ctx;
51937bfc7c1SJack Morgenstein 	struct mlx4_ib_demux_pv_qp *tun_qp;
52037bfc7c1SJack Morgenstein 	struct mlx4_rcv_tunnel_mad *tun_mad;
52190898850SDasaratharaman Chandramouli 	struct rdma_ah_attr attr;
52237bfc7c1SJack Morgenstein 	struct ib_ah *ah;
52337bfc7c1SJack Morgenstein 	struct ib_qp *src_qp = NULL;
52437bfc7c1SJack Morgenstein 	unsigned tun_tx_ix = 0;
52537bfc7c1SJack Morgenstein 	int dqpn;
52637bfc7c1SJack Morgenstein 	int ret = 0;
52737bfc7c1SJack Morgenstein 	u16 tun_pkey_ix;
5282c75d2ccSJack Morgenstein 	u16 cached_pkey;
5296ee51a4eSJack Morgenstein 	u8 is_eth = dev->dev->caps.port_type[port] == MLX4_PORT_TYPE_ETH;
53037bfc7c1SJack Morgenstein 
53109461944SHåkon Bugge 	if (dest_qpt > IB_QPT_GSI) {
53209461944SHåkon Bugge 		pr_debug("dest_qpt (%d) > IB_QPT_GSI\n", dest_qpt);
53337bfc7c1SJack Morgenstein 		return -EINVAL;
53409461944SHåkon Bugge 	}
53537bfc7c1SJack Morgenstein 
53637bfc7c1SJack Morgenstein 	tun_ctx = dev->sriov.demux[port-1].tun[slave];
53737bfc7c1SJack Morgenstein 
53837bfc7c1SJack Morgenstein 	/* check if proxy qp created */
53937bfc7c1SJack Morgenstein 	if (!tun_ctx || tun_ctx->state != DEMUX_PV_STATE_ACTIVE)
54037bfc7c1SJack Morgenstein 		return -EAGAIN;
54137bfc7c1SJack Morgenstein 
54237bfc7c1SJack Morgenstein 	if (!dest_qpt)
54337bfc7c1SJack Morgenstein 		tun_qp = &tun_ctx->qp[0];
54437bfc7c1SJack Morgenstein 	else
54537bfc7c1SJack Morgenstein 		tun_qp = &tun_ctx->qp[1];
54637bfc7c1SJack Morgenstein 
5472c75d2ccSJack Morgenstein 	/* compute P_Key index to put in tunnel header for slave */
54837bfc7c1SJack Morgenstein 	if (dest_qpt) {
5492c75d2ccSJack Morgenstein 		u16 pkey_ix;
5502c75d2ccSJack Morgenstein 		ret = ib_get_cached_pkey(&dev->ib_dev, port, wc->pkey_index, &cached_pkey);
55109461944SHåkon Bugge 		if (ret) {
55209461944SHåkon Bugge 			pr_debug("unable to get %s cached pkey for index %d, ret %d\n",
55309461944SHåkon Bugge 				 is_proxy_qp0(dev, wc->src_qp, slave) ? "SMI" : "GSI",
55409461944SHåkon Bugge 				 wc->pkey_index, ret);
55537bfc7c1SJack Morgenstein 			return -EINVAL;
55609461944SHåkon Bugge 		}
55737bfc7c1SJack Morgenstein 
5582c75d2ccSJack Morgenstein 		ret = find_slave_port_pkey_ix(dev, slave, port, cached_pkey, &pkey_ix);
55909461944SHåkon Bugge 		if (ret) {
56009461944SHåkon Bugge 			pr_debug("unable to get %s pkey ix for pkey 0x%x, ret %d\n",
56109461944SHåkon Bugge 				 is_proxy_qp0(dev, wc->src_qp, slave) ? "SMI" : "GSI",
56209461944SHåkon Bugge 				 cached_pkey, ret);
56337bfc7c1SJack Morgenstein 			return -EINVAL;
56409461944SHåkon Bugge 		}
5652c75d2ccSJack Morgenstein 		tun_pkey_ix = pkey_ix;
56637bfc7c1SJack Morgenstein 	} else
56737bfc7c1SJack Morgenstein 		tun_pkey_ix = dev->pkeys.virt2phys_pkey[slave][port - 1][0];
56837bfc7c1SJack Morgenstein 
56947605df9SJack Morgenstein 	dqpn = dev->dev->phys_caps.base_proxy_sqpn + 8 * slave + port + (dest_qpt * 2) - 1;
57037bfc7c1SJack Morgenstein 
57137bfc7c1SJack Morgenstein 	/* get tunnel tx data buf for slave */
57237bfc7c1SJack Morgenstein 	src_qp = tun_qp->qp;
57337bfc7c1SJack Morgenstein 
57437bfc7c1SJack Morgenstein 	/* create ah. Just need an empty one with the port num for the post send.
57537bfc7c1SJack Morgenstein 	 * The driver will set the force loopback bit in post_send */
57637bfc7c1SJack Morgenstein 	memset(&attr, 0, sizeof attr);
57744c58487SDasaratharaman Chandramouli 	attr.type = rdma_ah_find_type(&dev->ib_dev, port);
578d8966fcdSDasaratharaman Chandramouli 
579d8966fcdSDasaratharaman Chandramouli 	rdma_ah_set_port_num(&attr, port);
5806ee51a4eSJack Morgenstein 	if (is_eth) {
581850d8fd7SMoni Shoua 		union ib_gid sgid;
582d8966fcdSDasaratharaman Chandramouli 		union ib_gid dgid;
583850d8fd7SMoni Shoua 
584d8966fcdSDasaratharaman Chandramouli 		if (get_gids_from_l3_hdr(grh, &sgid, &dgid))
585850d8fd7SMoni Shoua 			return -EINVAL;
586d8966fcdSDasaratharaman Chandramouli 		rdma_ah_set_grh(&attr, &dgid, 0, 0, 0, 0);
5876ee51a4eSJack Morgenstein 	}
588b090c4e3SGal Pressman 	ah = rdma_create_ah(tun_ctx->pd, &attr, 0);
58937bfc7c1SJack Morgenstein 	if (IS_ERR(ah))
59037bfc7c1SJack Morgenstein 		return -ENOMEM;
59137bfc7c1SJack Morgenstein 
59237bfc7c1SJack Morgenstein 	/* allocate tunnel tx buf after pass failure returns */
59337bfc7c1SJack Morgenstein 	spin_lock(&tun_qp->tx_lock);
59437bfc7c1SJack Morgenstein 	if (tun_qp->tx_ix_head - tun_qp->tx_ix_tail >=
59537bfc7c1SJack Morgenstein 	    (MLX4_NUM_TUNNEL_BUFS - 1))
59637bfc7c1SJack Morgenstein 		ret = -EAGAIN;
59737bfc7c1SJack Morgenstein 	else
59837bfc7c1SJack Morgenstein 		tun_tx_ix = (++tun_qp->tx_ix_head) & (MLX4_NUM_TUNNEL_BUFS - 1);
59937bfc7c1SJack Morgenstein 	spin_unlock(&tun_qp->tx_lock);
60037bfc7c1SJack Morgenstein 	if (ret)
601a6100603SYishai Hadas 		goto end;
60237bfc7c1SJack Morgenstein 
60337bfc7c1SJack Morgenstein 	tun_mad = (struct mlx4_rcv_tunnel_mad *) (tun_qp->tx_ring[tun_tx_ix].buf.addr);
60437bfc7c1SJack Morgenstein 	if (tun_qp->tx_ring[tun_tx_ix].ah)
6052553ba21SGal Pressman 		rdma_destroy_ah(tun_qp->tx_ring[tun_tx_ix].ah, 0);
60637bfc7c1SJack Morgenstein 	tun_qp->tx_ring[tun_tx_ix].ah = ah;
60737bfc7c1SJack Morgenstein 	ib_dma_sync_single_for_cpu(&dev->ib_dev,
60837bfc7c1SJack Morgenstein 				   tun_qp->tx_ring[tun_tx_ix].buf.map,
60937bfc7c1SJack Morgenstein 				   sizeof (struct mlx4_rcv_tunnel_mad),
61037bfc7c1SJack Morgenstein 				   DMA_TO_DEVICE);
61137bfc7c1SJack Morgenstein 
61237bfc7c1SJack Morgenstein 	/* copy over to tunnel buffer */
61337bfc7c1SJack Morgenstein 	if (grh)
61437bfc7c1SJack Morgenstein 		memcpy(&tun_mad->grh, grh, sizeof *grh);
61537bfc7c1SJack Morgenstein 	memcpy(&tun_mad->mad, mad, sizeof *mad);
61637bfc7c1SJack Morgenstein 
61737bfc7c1SJack Morgenstein 	/* adjust tunnel data */
61837bfc7c1SJack Morgenstein 	tun_mad->hdr.pkey_index = cpu_to_be16(tun_pkey_ix);
61937bfc7c1SJack Morgenstein 	tun_mad->hdr.flags_src_qp = cpu_to_be32(wc->src_qp & 0xFFFFFF);
62037bfc7c1SJack Morgenstein 	tun_mad->hdr.g_ml_path = (grh && (wc->wc_flags & IB_WC_GRH)) ? 0x80 : 0;
62137bfc7c1SJack Morgenstein 
6225ea8bbfcSJack Morgenstein 	if (is_eth) {
6235ea8bbfcSJack Morgenstein 		u16 vlan = 0;
6245ea8bbfcSJack Morgenstein 		if (mlx4_get_slave_default_vlan(dev->dev, port, slave, &vlan,
6255ea8bbfcSJack Morgenstein 						NULL)) {
6265ea8bbfcSJack Morgenstein 			/* VST mode */
6275ea8bbfcSJack Morgenstein 			if (vlan != wc->vlan_id)
6285ea8bbfcSJack Morgenstein 				/* Packet vlan is not the VST-assigned vlan.
6295ea8bbfcSJack Morgenstein 				 * Drop the packet.
6305ea8bbfcSJack Morgenstein 				 */
6315ea8bbfcSJack Morgenstein 				goto out;
6325ea8bbfcSJack Morgenstein 			 else
6335ea8bbfcSJack Morgenstein 				/* Remove the vlan tag before forwarding
6345ea8bbfcSJack Morgenstein 				 * the packet to the VF.
6355ea8bbfcSJack Morgenstein 				 */
6365ea8bbfcSJack Morgenstein 				vlan = 0xffff;
6375ea8bbfcSJack Morgenstein 		} else {
6385ea8bbfcSJack Morgenstein 			vlan = wc->vlan_id;
6395ea8bbfcSJack Morgenstein 		}
6405ea8bbfcSJack Morgenstein 
6415ea8bbfcSJack Morgenstein 		tun_mad->hdr.sl_vid = cpu_to_be16(vlan);
6425ea8bbfcSJack Morgenstein 		memcpy((char *)&tun_mad->hdr.mac_31_0, &(wc->smac[0]), 4);
6435ea8bbfcSJack Morgenstein 		memcpy((char *)&tun_mad->hdr.slid_mac_47_32, &(wc->smac[4]), 2);
6445ea8bbfcSJack Morgenstein 	} else {
6455ea8bbfcSJack Morgenstein 		tun_mad->hdr.sl_vid = cpu_to_be16(((u16)(wc->sl)) << 12);
64662ede777SHiatt, Don 		tun_mad->hdr.slid_mac_47_32 = ib_lid_be16(wc->slid);
6475ea8bbfcSJack Morgenstein 	}
6485ea8bbfcSJack Morgenstein 
64937bfc7c1SJack Morgenstein 	ib_dma_sync_single_for_device(&dev->ib_dev,
65037bfc7c1SJack Morgenstein 				      tun_qp->tx_ring[tun_tx_ix].buf.map,
65137bfc7c1SJack Morgenstein 				      sizeof (struct mlx4_rcv_tunnel_mad),
65237bfc7c1SJack Morgenstein 				      DMA_TO_DEVICE);
65337bfc7c1SJack Morgenstein 
65437bfc7c1SJack Morgenstein 	list.addr = tun_qp->tx_ring[tun_tx_ix].buf.map;
65537bfc7c1SJack Morgenstein 	list.length = sizeof (struct mlx4_rcv_tunnel_mad);
6567dd97576SJason Gunthorpe 	list.lkey = tun_ctx->pd->local_dma_lkey;
65737bfc7c1SJack Morgenstein 
658e622f2f4SChristoph Hellwig 	wr.ah = ah;
659e622f2f4SChristoph Hellwig 	wr.port_num = port;
660e622f2f4SChristoph Hellwig 	wr.remote_qkey = IB_QP_SET_QKEY;
661e622f2f4SChristoph Hellwig 	wr.remote_qpn = dqpn;
662e622f2f4SChristoph Hellwig 	wr.wr.next = NULL;
663e622f2f4SChristoph Hellwig 	wr.wr.wr_id = ((u64) tun_tx_ix) | MLX4_TUN_SET_WRID_QPN(dest_qpt);
664e622f2f4SChristoph Hellwig 	wr.wr.sg_list = &list;
665e622f2f4SChristoph Hellwig 	wr.wr.num_sge = 1;
666e622f2f4SChristoph Hellwig 	wr.wr.opcode = IB_WR_SEND;
667e622f2f4SChristoph Hellwig 	wr.wr.send_flags = IB_SEND_SIGNALED;
66837bfc7c1SJack Morgenstein 
669e622f2f4SChristoph Hellwig 	ret = ib_post_send(src_qp, &wr.wr, &bad_wr);
670a6100603SYishai Hadas 	if (!ret)
671a6100603SYishai Hadas 		return 0;
67237bfc7c1SJack Morgenstein  out:
673a6100603SYishai Hadas 	spin_lock(&tun_qp->tx_lock);
674a6100603SYishai Hadas 	tun_qp->tx_ix_tail++;
675a6100603SYishai Hadas 	spin_unlock(&tun_qp->tx_lock);
676a6100603SYishai Hadas 	tun_qp->tx_ring[tun_tx_ix].ah = NULL;
677a6100603SYishai Hadas end:
6782553ba21SGal Pressman 	rdma_destroy_ah(ah, 0);
67937bfc7c1SJack Morgenstein 	return ret;
68037bfc7c1SJack Morgenstein }
68137bfc7c1SJack Morgenstein 
mlx4_ib_demux_mad(struct ib_device * ibdev,u32 port,struct ib_wc * wc,struct ib_grh * grh,struct ib_mad * mad)6821fb7f897SMark Bloch static int mlx4_ib_demux_mad(struct ib_device *ibdev, u32 port,
68337bfc7c1SJack Morgenstein 			struct ib_wc *wc, struct ib_grh *grh,
68437bfc7c1SJack Morgenstein 			struct ib_mad *mad)
68537bfc7c1SJack Morgenstein {
68637bfc7c1SJack Morgenstein 	struct mlx4_ib_dev *dev = to_mdev(ibdev);
687f1b4e12aSOr Gerlitz 	int err, other_port;
688f1b4e12aSOr Gerlitz 	int slave = -1;
68937bfc7c1SJack Morgenstein 	u8 *slave_id;
6906ee51a4eSJack Morgenstein 	int is_eth = 0;
6916ee51a4eSJack Morgenstein 
6926ee51a4eSJack Morgenstein 	if (rdma_port_get_link_layer(ibdev, port) == IB_LINK_LAYER_INFINIBAND)
6936ee51a4eSJack Morgenstein 		is_eth = 0;
6946ee51a4eSJack Morgenstein 	else
6956ee51a4eSJack Morgenstein 		is_eth = 1;
6966ee51a4eSJack Morgenstein 
6976ee51a4eSJack Morgenstein 	if (is_eth) {
698850d8fd7SMoni Shoua 		union ib_gid dgid;
699850d8fd7SMoni Shoua 		union ib_gid sgid;
700850d8fd7SMoni Shoua 
701850d8fd7SMoni Shoua 		if (get_gids_from_l3_hdr(grh, &sgid, &dgid))
702850d8fd7SMoni Shoua 			return -EINVAL;
7036ee51a4eSJack Morgenstein 		if (!(wc->wc_flags & IB_WC_GRH)) {
7046ee51a4eSJack Morgenstein 			mlx4_ib_warn(ibdev, "RoCE grh not present.\n");
7056ee51a4eSJack Morgenstein 			return -EINVAL;
7066ee51a4eSJack Morgenstein 		}
7076ee51a4eSJack Morgenstein 		if (mad->mad_hdr.mgmt_class != IB_MGMT_CLASS_CM) {
7086ee51a4eSJack Morgenstein 			mlx4_ib_warn(ibdev, "RoCE mgmt class is not CM\n");
7096ee51a4eSJack Morgenstein 			return -EINVAL;
7106ee51a4eSJack Morgenstein 		}
711850d8fd7SMoni Shoua 		err = mlx4_get_slave_from_roce_gid(dev->dev, port, dgid.raw, &slave);
712f1b4e12aSOr Gerlitz 		if (err && mlx4_is_mf_bonded(dev->dev)) {
713f1b4e12aSOr Gerlitz 			other_port = (port == 1) ? 2 : 1;
714850d8fd7SMoni Shoua 			err = mlx4_get_slave_from_roce_gid(dev->dev, other_port, dgid.raw, &slave);
715f1b4e12aSOr Gerlitz 			if (!err) {
716f1b4e12aSOr Gerlitz 				port = other_port;
717f1b4e12aSOr Gerlitz 				pr_debug("resolved slave %d from gid %pI6 wire port %d other %d\n",
718f1b4e12aSOr Gerlitz 					 slave, grh->dgid.raw, port, other_port);
719f1b4e12aSOr Gerlitz 			}
720f1b4e12aSOr Gerlitz 		}
721f1b4e12aSOr Gerlitz 		if (err) {
7226ee51a4eSJack Morgenstein 			mlx4_ib_warn(ibdev, "failed matching grh\n");
7236ee51a4eSJack Morgenstein 			return -ENOENT;
7246ee51a4eSJack Morgenstein 		}
7256ee51a4eSJack Morgenstein 		if (slave >= dev->dev->caps.sqp_demux) {
7266ee51a4eSJack Morgenstein 			mlx4_ib_warn(ibdev, "slave id: %d is bigger than allowed:%d\n",
7276ee51a4eSJack Morgenstein 				     slave, dev->dev->caps.sqp_demux);
7286ee51a4eSJack Morgenstein 			return -ENOENT;
7296ee51a4eSJack Morgenstein 		}
7306ee51a4eSJack Morgenstein 
7316ee51a4eSJack Morgenstein 		if (mlx4_ib_demux_cm_handler(ibdev, port, NULL, mad))
7326ee51a4eSJack Morgenstein 			return 0;
7336ee51a4eSJack Morgenstein 
7346ee51a4eSJack Morgenstein 		err = mlx4_ib_send_to_slave(dev, slave, port, wc->qp->qp_type, wc, grh, mad);
7356ee51a4eSJack Morgenstein 		if (err)
73609461944SHåkon Bugge 			pr_debug("failed sending %s to slave %d via tunnel qp (%d)\n",
73709461944SHåkon Bugge 				 is_proxy_qp0(dev, wc->src_qp, slave) ? "SMI" : "GSI",
7386ee51a4eSJack Morgenstein 				 slave, err);
7396ee51a4eSJack Morgenstein 		return 0;
7406ee51a4eSJack Morgenstein 	}
74137bfc7c1SJack Morgenstein 
74237bfc7c1SJack Morgenstein 	/* Initially assume that this mad is for us */
74337bfc7c1SJack Morgenstein 	slave = mlx4_master_func_num(dev->dev);
74437bfc7c1SJack Morgenstein 
74537bfc7c1SJack Morgenstein 	/* See if the slave id is encoded in a response mad */
74637bfc7c1SJack Morgenstein 	if (mad->mad_hdr.method & 0x80) {
74737bfc7c1SJack Morgenstein 		slave_id = (u8 *) &mad->mad_hdr.tid;
74837bfc7c1SJack Morgenstein 		slave = *slave_id;
74937bfc7c1SJack Morgenstein 		if (slave != 255) /*255 indicates the dom0*/
75037bfc7c1SJack Morgenstein 			*slave_id = 0; /* remap tid */
75137bfc7c1SJack Morgenstein 	}
75237bfc7c1SJack Morgenstein 
75337bfc7c1SJack Morgenstein 	/* If a grh is present, we demux according to it */
75437bfc7c1SJack Morgenstein 	if (wc->wc_flags & IB_WC_GRH) {
755befcabcdSJack Morgenstein 		if (grh->dgid.global.interface_id ==
756befcabcdSJack Morgenstein 			cpu_to_be64(IB_SA_WELL_KNOWN_GUID) &&
757befcabcdSJack Morgenstein 		    grh->dgid.global.subnet_prefix == cpu_to_be64(
758befcabcdSJack Morgenstein 			atomic64_read(&dev->sriov.demux[port - 1].subnet_prefix))) {
759befcabcdSJack Morgenstein 			slave = 0;
760befcabcdSJack Morgenstein 		} else {
761befcabcdSJack Morgenstein 			slave = mlx4_ib_find_real_gid(ibdev, port,
762befcabcdSJack Morgenstein 						      grh->dgid.global.interface_id);
76337bfc7c1SJack Morgenstein 			if (slave < 0) {
76437bfc7c1SJack Morgenstein 				mlx4_ib_warn(ibdev, "failed matching grh\n");
76537bfc7c1SJack Morgenstein 				return -ENOENT;
76637bfc7c1SJack Morgenstein 			}
76737bfc7c1SJack Morgenstein 		}
768befcabcdSJack Morgenstein 	}
76937bfc7c1SJack Morgenstein 	/* Class-specific handling */
77037bfc7c1SJack Morgenstein 	switch (mad->mad_hdr.mgmt_class) {
77197982f5aSJack Morgenstein 	case IB_MGMT_CLASS_SUBN_LID_ROUTED:
77297982f5aSJack Morgenstein 	case IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE:
77397982f5aSJack Morgenstein 		/* 255 indicates the dom0 */
77497982f5aSJack Morgenstein 		if (slave != 255 && slave != mlx4_master_func_num(dev->dev)) {
77597982f5aSJack Morgenstein 			if (!mlx4_vf_smi_enabled(dev->dev, slave, port))
77697982f5aSJack Morgenstein 				return -EPERM;
77797982f5aSJack Morgenstein 			/* for a VF. drop unsolicited MADs */
77897982f5aSJack Morgenstein 			if (!(mad->mad_hdr.method & IB_MGMT_METHOD_RESP)) {
77997982f5aSJack Morgenstein 				mlx4_ib_warn(ibdev, "demux QP0. rejecting unsolicited mad for slave %d class 0x%x, method 0x%x\n",
78097982f5aSJack Morgenstein 					     slave, mad->mad_hdr.mgmt_class,
78197982f5aSJack Morgenstein 					     mad->mad_hdr.method);
78297982f5aSJack Morgenstein 				return -EINVAL;
78397982f5aSJack Morgenstein 			}
78497982f5aSJack Morgenstein 		}
78597982f5aSJack Morgenstein 		break;
78637bfc7c1SJack Morgenstein 	case IB_MGMT_CLASS_SUBN_ADM:
78737bfc7c1SJack Morgenstein 		if (mlx4_ib_demux_sa_handler(ibdev, port, slave,
78837bfc7c1SJack Morgenstein 					     (struct ib_sa_mad *) mad))
78937bfc7c1SJack Morgenstein 			return 0;
79037bfc7c1SJack Morgenstein 		break;
7913cf69cc8SAmir Vadai 	case IB_MGMT_CLASS_CM:
7923cf69cc8SAmir Vadai 		if (mlx4_ib_demux_cm_handler(ibdev, port, &slave, mad))
7933cf69cc8SAmir Vadai 			return 0;
7943cf69cc8SAmir Vadai 		break;
79537bfc7c1SJack Morgenstein 	case IB_MGMT_CLASS_DEVICE_MGMT:
79637bfc7c1SJack Morgenstein 		if (mad->mad_hdr.method != IB_MGMT_METHOD_GET_RESP)
79737bfc7c1SJack Morgenstein 			return 0;
79837bfc7c1SJack Morgenstein 		break;
79937bfc7c1SJack Morgenstein 	default:
80037bfc7c1SJack Morgenstein 		/* Drop unsupported classes for slaves in tunnel mode */
80137bfc7c1SJack Morgenstein 		if (slave != mlx4_master_func_num(dev->dev)) {
80237bfc7c1SJack Morgenstein 			pr_debug("dropping unsupported ingress mad from class:%d "
80337bfc7c1SJack Morgenstein 				 "for slave:%d\n", mad->mad_hdr.mgmt_class, slave);
80437bfc7c1SJack Morgenstein 			return 0;
80537bfc7c1SJack Morgenstein 		}
80637bfc7c1SJack Morgenstein 	}
80737bfc7c1SJack Morgenstein 	/*make sure that no slave==255 was not handled yet.*/
80837bfc7c1SJack Morgenstein 	if (slave >= dev->dev->caps.sqp_demux) {
80937bfc7c1SJack Morgenstein 		mlx4_ib_warn(ibdev, "slave id: %d is bigger than allowed:%d\n",
81037bfc7c1SJack Morgenstein 			     slave, dev->dev->caps.sqp_demux);
81137bfc7c1SJack Morgenstein 		return -ENOENT;
81237bfc7c1SJack Morgenstein 	}
81337bfc7c1SJack Morgenstein 
81437bfc7c1SJack Morgenstein 	err = mlx4_ib_send_to_slave(dev, slave, port, wc->qp->qp_type, wc, grh, mad);
81537bfc7c1SJack Morgenstein 	if (err)
81609461944SHåkon Bugge 		pr_debug("failed sending %s to slave %d via tunnel qp (%d)\n",
81709461944SHåkon Bugge 			 is_proxy_qp0(dev, wc->src_qp, slave) ? "SMI" : "GSI",
81837bfc7c1SJack Morgenstein 			 slave, err);
81937bfc7c1SJack Morgenstein 	return 0;
82037bfc7c1SJack Morgenstein }
82137bfc7c1SJack Morgenstein 
ib_process_mad(struct ib_device * ibdev,int mad_flags,u32 port_num,const struct ib_wc * in_wc,const struct ib_grh * in_grh,const struct ib_mad * in_mad,struct ib_mad * out_mad)8221fb7f897SMark Bloch static int ib_process_mad(struct ib_device *ibdev, int mad_flags, u32 port_num,
823a97e2d86SIra Weiny 			const struct ib_wc *in_wc, const struct ib_grh *in_grh,
824a97e2d86SIra Weiny 			const struct ib_mad *in_mad, struct ib_mad *out_mad)
825225c7b1fSRoland Dreier {
826f0f6f346SMoni Shoua 	u16 slid, prev_lid = 0;
827225c7b1fSRoland Dreier 	int err;
828f0f6f346SMoni Shoua 	struct ib_port_attr pattr;
829225c7b1fSRoland Dreier 
83062ede777SHiatt, Don 	slid = in_wc ? ib_lid_cpu16(in_wc->slid) : be16_to_cpu(IB_LID_PERMISSIVE);
831225c7b1fSRoland Dreier 
832225c7b1fSRoland Dreier 	if (in_mad->mad_hdr.method == IB_MGMT_METHOD_TRAP && slid == 0) {
833225c7b1fSRoland Dreier 		forward_trap(to_mdev(ibdev), port_num, in_mad);
834225c7b1fSRoland Dreier 		return IB_MAD_RESULT_SUCCESS | IB_MAD_RESULT_CONSUMED;
835225c7b1fSRoland Dreier 	}
836225c7b1fSRoland Dreier 
837225c7b1fSRoland Dreier 	if (in_mad->mad_hdr.mgmt_class == IB_MGMT_CLASS_SUBN_LID_ROUTED ||
838225c7b1fSRoland Dreier 	    in_mad->mad_hdr.mgmt_class == IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE) {
839225c7b1fSRoland Dreier 		if (in_mad->mad_hdr.method   != IB_MGMT_METHOD_GET &&
840225c7b1fSRoland Dreier 		    in_mad->mad_hdr.method   != IB_MGMT_METHOD_SET &&
841225c7b1fSRoland Dreier 		    in_mad->mad_hdr.method   != IB_MGMT_METHOD_TRAP_REPRESS)
842225c7b1fSRoland Dreier 			return IB_MAD_RESULT_SUCCESS;
843225c7b1fSRoland Dreier 
844225c7b1fSRoland Dreier 		/*
845a6f7feaeSJack Morgenstein 		 * Don't process SMInfo queries -- the SMA can't handle them.
846225c7b1fSRoland Dreier 		 */
847a6f7feaeSJack Morgenstein 		if (in_mad->mad_hdr.attr_id == IB_SMP_ATTR_SM_INFO)
848225c7b1fSRoland Dreier 			return IB_MAD_RESULT_SUCCESS;
849225c7b1fSRoland Dreier 	} else if (in_mad->mad_hdr.mgmt_class == IB_MGMT_CLASS_PERF_MGMT ||
850225c7b1fSRoland Dreier 		   in_mad->mad_hdr.mgmt_class == MLX4_IB_VENDOR_CLASS1   ||
8516578cf33SEli Cohen 		   in_mad->mad_hdr.mgmt_class == MLX4_IB_VENDOR_CLASS2   ||
8526578cf33SEli Cohen 		   in_mad->mad_hdr.mgmt_class == IB_MGMT_CLASS_CONG_MGMT) {
853225c7b1fSRoland Dreier 		if (in_mad->mad_hdr.method  != IB_MGMT_METHOD_GET &&
854225c7b1fSRoland Dreier 		    in_mad->mad_hdr.method  != IB_MGMT_METHOD_SET)
855225c7b1fSRoland Dreier 			return IB_MAD_RESULT_SUCCESS;
856225c7b1fSRoland Dreier 	} else
857225c7b1fSRoland Dreier 		return IB_MAD_RESULT_SUCCESS;
858225c7b1fSRoland Dreier 
859f0f6f346SMoni Shoua 	if ((in_mad->mad_hdr.mgmt_class == IB_MGMT_CLASS_SUBN_LID_ROUTED ||
860f0f6f346SMoni Shoua 	     in_mad->mad_hdr.mgmt_class == IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE) &&
861f0f6f346SMoni Shoua 	    in_mad->mad_hdr.method == IB_MGMT_METHOD_SET &&
862f0f6f346SMoni Shoua 	    in_mad->mad_hdr.attr_id == IB_SMP_ATTR_PORT_INFO &&
863f0f6f346SMoni Shoua 	    !ib_query_port(ibdev, port_num, &pattr))
86462ede777SHiatt, Don 		prev_lid = ib_lid_cpu16(pattr.lid);
865f0f6f346SMoni Shoua 
866225c7b1fSRoland Dreier 	err = mlx4_MAD_IFC(to_mdev(ibdev),
8670a9a0188SJack Morgenstein 			   (mad_flags & IB_MAD_IGNORE_MKEY ? MLX4_MAD_IFC_IGNORE_MKEY : 0) |
8680a9a0188SJack Morgenstein 			   (mad_flags & IB_MAD_IGNORE_BKEY ? MLX4_MAD_IFC_IGNORE_BKEY : 0) |
8690a9a0188SJack Morgenstein 			   MLX4_MAD_IFC_NET_VIEW,
870225c7b1fSRoland Dreier 			   port_num, in_wc, in_grh, in_mad, out_mad);
871225c7b1fSRoland Dreier 	if (err)
872225c7b1fSRoland Dreier 		return IB_MAD_RESULT_FAILURE;
873225c7b1fSRoland Dreier 
874225c7b1fSRoland Dreier 	if (!out_mad->mad_hdr.status) {
875f0f6f346SMoni Shoua 		smp_snoop(ibdev, port_num, in_mad, prev_lid);
876992e8e6eSJack Morgenstein 		/* slaves get node desc from FW */
877992e8e6eSJack Morgenstein 		if (!mlx4_is_slave(to_mdev(ibdev)->dev))
878225c7b1fSRoland Dreier 			node_desc_override(ibdev, out_mad);
879225c7b1fSRoland Dreier 	}
880225c7b1fSRoland Dreier 
881225c7b1fSRoland Dreier 	/* set return bit in status of directed route responses */
882225c7b1fSRoland Dreier 	if (in_mad->mad_hdr.mgmt_class == IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE)
883225c7b1fSRoland Dreier 		out_mad->mad_hdr.status |= cpu_to_be16(1 << 15);
884225c7b1fSRoland Dreier 
885225c7b1fSRoland Dreier 	if (in_mad->mad_hdr.method == IB_MGMT_METHOD_TRAP_REPRESS)
886225c7b1fSRoland Dreier 		/* no response for trap repress */
887225c7b1fSRoland Dreier 		return IB_MAD_RESULT_SUCCESS | IB_MAD_RESULT_CONSUMED;
888225c7b1fSRoland Dreier 
889225c7b1fSRoland Dreier 	return IB_MAD_RESULT_SUCCESS | IB_MAD_RESULT_REPLY;
890225c7b1fSRoland Dreier }
891225c7b1fSRoland Dreier 
edit_counter(struct mlx4_counter * cnt,void * counters,__be16 attr_id)892c3c0c836SEran Ben Elisha static void edit_counter(struct mlx4_counter *cnt, void *counters,
893c3c0c836SEran Ben Elisha 			 __be16 attr_id)
894c3779134SOr Gerlitz {
895c3c0c836SEran Ben Elisha 	switch (attr_id) {
896c3c0c836SEran Ben Elisha 	case IB_PMA_PORT_COUNTERS:
897c3c0c836SEran Ben Elisha 	{
898c3c0c836SEran Ben Elisha 		struct ib_pma_portcounters *pma_cnt =
899c3c0c836SEran Ben Elisha 			(struct ib_pma_portcounters *)counters;
900c3c0c836SEran Ben Elisha 
90161a3855bSMajd Dibbiny 		ASSIGN_32BIT_COUNTER(pma_cnt->port_xmit_data,
90261a3855bSMajd Dibbiny 				     (be64_to_cpu(cnt->tx_bytes) >> 2));
90361a3855bSMajd Dibbiny 		ASSIGN_32BIT_COUNTER(pma_cnt->port_rcv_data,
90461a3855bSMajd Dibbiny 				     (be64_to_cpu(cnt->rx_bytes) >> 2));
90561a3855bSMajd Dibbiny 		ASSIGN_32BIT_COUNTER(pma_cnt->port_xmit_packets,
90661a3855bSMajd Dibbiny 				     be64_to_cpu(cnt->tx_frames));
90761a3855bSMajd Dibbiny 		ASSIGN_32BIT_COUNTER(pma_cnt->port_rcv_packets,
90861a3855bSMajd Dibbiny 				     be64_to_cpu(cnt->rx_frames));
909c3c0c836SEran Ben Elisha 		break;
910c3c0c836SEran Ben Elisha 	}
911c3c0c836SEran Ben Elisha 	case IB_PMA_PORT_COUNTERS_EXT:
912c3c0c836SEran Ben Elisha 	{
913c3c0c836SEran Ben Elisha 		struct ib_pma_portcounters_ext *pma_cnt_ext =
914c3c0c836SEran Ben Elisha 			(struct ib_pma_portcounters_ext *)counters;
915c3c0c836SEran Ben Elisha 
916c3c0c836SEran Ben Elisha 		pma_cnt_ext->port_xmit_data =
917c3c0c836SEran Ben Elisha 			cpu_to_be64(be64_to_cpu(cnt->tx_bytes) >> 2);
918c3c0c836SEran Ben Elisha 		pma_cnt_ext->port_rcv_data =
919c3c0c836SEran Ben Elisha 			cpu_to_be64(be64_to_cpu(cnt->rx_bytes) >> 2);
920c3c0c836SEran Ben Elisha 		pma_cnt_ext->port_xmit_packets = cnt->tx_frames;
921c3c0c836SEran Ben Elisha 		pma_cnt_ext->port_rcv_packets = cnt->rx_frames;
922c3c0c836SEran Ben Elisha 		break;
923c3c0c836SEran Ben Elisha 	}
924c3c0c836SEran Ben Elisha 	}
925c3779134SOr Gerlitz }
926c3779134SOr Gerlitz 
iboe_process_mad_port_info(void * out_mad)927c2bab619SEran Ben Elisha static int iboe_process_mad_port_info(void *out_mad)
928c2bab619SEran Ben Elisha {
929c2bab619SEran Ben Elisha 	struct ib_class_port_info cpi = {};
930c2bab619SEran Ben Elisha 
931c2bab619SEran Ben Elisha 	cpi.capability_mask = IB_PMA_CLASS_CAP_EXT_WIDTH;
932c2bab619SEran Ben Elisha 	memcpy(out_mad, &cpi, sizeof(cpi));
933c2bab619SEran Ben Elisha 	return IB_MAD_RESULT_SUCCESS | IB_MAD_RESULT_REPLY;
934c2bab619SEran Ben Elisha }
935c2bab619SEran Ben Elisha 
iboe_process_mad(struct ib_device * ibdev,int mad_flags,u32 port_num,const struct ib_wc * in_wc,const struct ib_grh * in_grh,const struct ib_mad * in_mad,struct ib_mad * out_mad)9361fb7f897SMark Bloch static int iboe_process_mad(struct ib_device *ibdev, int mad_flags,
9371fb7f897SMark Bloch 			    u32 port_num, const struct ib_wc *in_wc,
9381fb7f897SMark Bloch 			    const struct ib_grh *in_grh,
939a97e2d86SIra Weiny 			    const struct ib_mad *in_mad, struct ib_mad *out_mad)
940c3779134SOr Gerlitz {
9419616982fSEran Ben Elisha 	struct mlx4_counter counter_stats;
942c3779134SOr Gerlitz 	struct mlx4_ib_dev *dev = to_mdev(ibdev);
9433ba8e31dSEran Ben Elisha 	struct counter_index *tmp_counter;
9443ba8e31dSEran Ben Elisha 	int err = IB_MAD_RESULT_FAILURE, stats_avail = 0;
945c3779134SOr Gerlitz 
946c3779134SOr Gerlitz 	if (in_mad->mad_hdr.mgmt_class != IB_MGMT_CLASS_PERF_MGMT)
947c3779134SOr Gerlitz 		return -EINVAL;
948c3779134SOr Gerlitz 
949c2bab619SEran Ben Elisha 	if (in_mad->mad_hdr.attr_id == IB_PMA_CLASS_PORT_INFO)
950c2bab619SEran Ben Elisha 		return iboe_process_mad_port_info((void *)(out_mad->data + 40));
951c2bab619SEran Ben Elisha 
9529616982fSEran Ben Elisha 	memset(&counter_stats, 0, sizeof(counter_stats));
9533ba8e31dSEran Ben Elisha 	mutex_lock(&dev->counters_table[port_num - 1].mutex);
9543ba8e31dSEran Ben Elisha 	list_for_each_entry(tmp_counter,
9553ba8e31dSEran Ben Elisha 			    &dev->counters_table[port_num - 1].counters_list,
9563ba8e31dSEran Ben Elisha 			    list) {
9579616982fSEran Ben Elisha 		err = mlx4_get_counter_stats(dev->dev,
9583ba8e31dSEran Ben Elisha 					     tmp_counter->index,
9599616982fSEran Ben Elisha 					     &counter_stats, 0);
9603ba8e31dSEran Ben Elisha 		if (err) {
961c3779134SOr Gerlitz 			err = IB_MAD_RESULT_FAILURE;
9623ba8e31dSEran Ben Elisha 			stats_avail = 0;
9633ba8e31dSEran Ben Elisha 			break;
9643ba8e31dSEran Ben Elisha 		}
9653ba8e31dSEran Ben Elisha 		stats_avail = 1;
9663ba8e31dSEran Ben Elisha 	}
9673ba8e31dSEran Ben Elisha 	mutex_unlock(&dev->counters_table[port_num - 1].mutex);
9683ba8e31dSEran Ben Elisha 	if (stats_avail) {
9699616982fSEran Ben Elisha 		switch (counter_stats.counter_mode & 0xf) {
970c3779134SOr Gerlitz 		case 0:
9719616982fSEran Ben Elisha 			edit_counter(&counter_stats,
972c3c0c836SEran Ben Elisha 				     (void *)(out_mad->data + 40),
973c3c0c836SEran Ben Elisha 				     in_mad->mad_hdr.attr_id);
974c3779134SOr Gerlitz 			err = IB_MAD_RESULT_SUCCESS | IB_MAD_RESULT_REPLY;
975c3779134SOr Gerlitz 			break;
976c3779134SOr Gerlitz 		default:
977c3779134SOr Gerlitz 			err = IB_MAD_RESULT_FAILURE;
978c3779134SOr Gerlitz 		}
979c3779134SOr Gerlitz 	}
980c3779134SOr Gerlitz 
981c3779134SOr Gerlitz 	return err;
982c3779134SOr Gerlitz }
983c3779134SOr Gerlitz 
mlx4_ib_process_mad(struct ib_device * ibdev,int mad_flags,u32 port_num,const struct ib_wc * in_wc,const struct ib_grh * in_grh,const struct ib_mad * in,struct ib_mad * out,size_t * out_mad_size,u16 * out_mad_pkey_index)9841fb7f897SMark Bloch int mlx4_ib_process_mad(struct ib_device *ibdev, int mad_flags, u32 port_num,
985a97e2d86SIra Weiny 			const struct ib_wc *in_wc, const struct ib_grh *in_grh,
986e26e7b88SLeon Romanovsky 			const struct ib_mad *in, struct ib_mad *out,
987e26e7b88SLeon Romanovsky 			size_t *out_mad_size, u16 *out_mad_pkey_index)
988c3779134SOr Gerlitz {
9897193a141SEran Ben Elisha 	struct mlx4_ib_dev *dev = to_mdev(ibdev);
99043bfb972SOr Gerlitz 	enum rdma_link_layer link = rdma_port_get_link_layer(ibdev, port_num);
9914cd7c947SIra Weiny 
99243bfb972SOr Gerlitz 	/* iboe_process_mad() which uses the HCA flow-counters to implement IB PMA
99343bfb972SOr Gerlitz 	 * queries, should be called only by VFs and for that specific purpose
99443bfb972SOr Gerlitz 	 */
99543bfb972SOr Gerlitz 	if (link == IB_LINK_LAYER_INFINIBAND) {
99643bfb972SOr Gerlitz 		if (mlx4_is_slave(dev->dev) &&
997e26e7b88SLeon Romanovsky 		    (in->mad_hdr.mgmt_class == IB_MGMT_CLASS_PERF_MGMT &&
998e26e7b88SLeon Romanovsky 		     (in->mad_hdr.attr_id == IB_PMA_PORT_COUNTERS ||
999e26e7b88SLeon Romanovsky 		      in->mad_hdr.attr_id == IB_PMA_PORT_COUNTERS_EXT ||
1000e26e7b88SLeon Romanovsky 		      in->mad_hdr.attr_id == IB_PMA_CLASS_PORT_INFO)))
1001e26e7b88SLeon Romanovsky 			return iboe_process_mad(ibdev, mad_flags, port_num,
1002e26e7b88SLeon Romanovsky 						in_wc, in_grh, in, out);
100343bfb972SOr Gerlitz 
1004e26e7b88SLeon Romanovsky 		return ib_process_mad(ibdev, mad_flags, port_num, in_wc, in_grh,
1005e26e7b88SLeon Romanovsky 				      in, out);
1006c3779134SOr Gerlitz 	}
100743bfb972SOr Gerlitz 
100843bfb972SOr Gerlitz 	if (link == IB_LINK_LAYER_ETHERNET)
100943bfb972SOr Gerlitz 		return iboe_process_mad(ibdev, mad_flags, port_num, in_wc,
1010e26e7b88SLeon Romanovsky 					in_grh, in, out);
101143bfb972SOr Gerlitz 
101243bfb972SOr Gerlitz 	return -EINVAL;
1013c3779134SOr Gerlitz }
1014c3779134SOr Gerlitz 
send_handler(struct ib_mad_agent * agent,struct ib_mad_send_wc * mad_send_wc)1015225c7b1fSRoland Dreier static void send_handler(struct ib_mad_agent *agent,
1016225c7b1fSRoland Dreier 			 struct ib_mad_send_wc *mad_send_wc)
1017225c7b1fSRoland Dreier {
1018992e8e6eSJack Morgenstein 	if (mad_send_wc->send_buf->context[0])
10192553ba21SGal Pressman 		rdma_destroy_ah(mad_send_wc->send_buf->context[0], 0);
1020225c7b1fSRoland Dreier 	ib_free_send_mad(mad_send_wc->send_buf);
1021225c7b1fSRoland Dreier }
1022225c7b1fSRoland Dreier 
mlx4_ib_mad_init(struct mlx4_ib_dev * dev)1023225c7b1fSRoland Dreier int mlx4_ib_mad_init(struct mlx4_ib_dev *dev)
1024225c7b1fSRoland Dreier {
1025225c7b1fSRoland Dreier 	struct ib_mad_agent *agent;
1026225c7b1fSRoland Dreier 	int p, q;
1027225c7b1fSRoland Dreier 	int ret;
1028fa417f7bSEli Cohen 	enum rdma_link_layer ll;
1029225c7b1fSRoland Dreier 
1030fa417f7bSEli Cohen 	for (p = 0; p < dev->num_ports; ++p) {
1031fa417f7bSEli Cohen 		ll = rdma_port_get_link_layer(&dev->ib_dev, p + 1);
1032225c7b1fSRoland Dreier 		for (q = 0; q <= 1; ++q) {
1033fa417f7bSEli Cohen 			if (ll == IB_LINK_LAYER_INFINIBAND) {
1034225c7b1fSRoland Dreier 				agent = ib_register_mad_agent(&dev->ib_dev, p + 1,
1035225c7b1fSRoland Dreier 							      q ? IB_QPT_GSI : IB_QPT_SMI,
1036225c7b1fSRoland Dreier 							      NULL, 0, send_handler,
10370f29b46dSIra Weiny 							      NULL, NULL, 0);
1038225c7b1fSRoland Dreier 				if (IS_ERR(agent)) {
1039225c7b1fSRoland Dreier 					ret = PTR_ERR(agent);
1040225c7b1fSRoland Dreier 					goto err;
1041225c7b1fSRoland Dreier 				}
1042225c7b1fSRoland Dreier 				dev->send_agent[p][q] = agent;
1043fa417f7bSEli Cohen 			} else
1044fa417f7bSEli Cohen 				dev->send_agent[p][q] = NULL;
1045fa417f7bSEli Cohen 		}
1046225c7b1fSRoland Dreier 	}
1047225c7b1fSRoland Dreier 
1048225c7b1fSRoland Dreier 	return 0;
1049225c7b1fSRoland Dreier 
1050225c7b1fSRoland Dreier err:
10517ff93f8bSYevgeny Petrilin 	for (p = 0; p < dev->num_ports; ++p)
1052225c7b1fSRoland Dreier 		for (q = 0; q <= 1; ++q)
1053225c7b1fSRoland Dreier 			if (dev->send_agent[p][q])
1054225c7b1fSRoland Dreier 				ib_unregister_mad_agent(dev->send_agent[p][q]);
1055225c7b1fSRoland Dreier 
1056225c7b1fSRoland Dreier 	return ret;
1057225c7b1fSRoland Dreier }
1058225c7b1fSRoland Dreier 
mlx4_ib_mad_cleanup(struct mlx4_ib_dev * dev)1059225c7b1fSRoland Dreier void mlx4_ib_mad_cleanup(struct mlx4_ib_dev *dev)
1060225c7b1fSRoland Dreier {
1061225c7b1fSRoland Dreier 	struct ib_mad_agent *agent;
1062225c7b1fSRoland Dreier 	int p, q;
1063225c7b1fSRoland Dreier 
10647ff93f8bSYevgeny Petrilin 	for (p = 0; p < dev->num_ports; ++p) {
1065225c7b1fSRoland Dreier 		for (q = 0; q <= 1; ++q) {
1066225c7b1fSRoland Dreier 			agent = dev->send_agent[p][q];
1067fa417f7bSEli Cohen 			if (agent) {
1068225c7b1fSRoland Dreier 				dev->send_agent[p][q] = NULL;
1069225c7b1fSRoland Dreier 				ib_unregister_mad_agent(agent);
1070225c7b1fSRoland Dreier 			}
1071fa417f7bSEli Cohen 		}
1072225c7b1fSRoland Dreier 
1073225c7b1fSRoland Dreier 		if (dev->sm_ah[p])
10742553ba21SGal Pressman 			rdma_destroy_ah(dev->sm_ah[p], 0);
1075225c7b1fSRoland Dreier 	}
1076225c7b1fSRoland Dreier }
107700f5ce99SJack Morgenstein 
handle_lid_change_event(struct mlx4_ib_dev * dev,u32 port_num)10781fb7f897SMark Bloch static void handle_lid_change_event(struct mlx4_ib_dev *dev, u32 port_num)
10792a4fae14SJack Morgenstein {
10802a4fae14SJack Morgenstein 	mlx4_ib_dispatch_event(dev, port_num, IB_EVENT_LID_CHANGE);
10812a4fae14SJack Morgenstein 
10822a4fae14SJack Morgenstein 	if (mlx4_is_master(dev->dev) && !dev->sriov.is_going_down)
10832a4fae14SJack Morgenstein 		mlx4_gen_slaves_port_mgt_ev(dev->dev, port_num,
10842a4fae14SJack Morgenstein 					    MLX4_EQ_PORT_INFO_LID_CHANGE_MASK);
10852a4fae14SJack Morgenstein }
10862a4fae14SJack Morgenstein 
handle_client_rereg_event(struct mlx4_ib_dev * dev,u32 port_num)10871fb7f897SMark Bloch static void handle_client_rereg_event(struct mlx4_ib_dev *dev, u32 port_num)
1088b9c5d6a6SOren Duer {
1089a0c64a17SJack Morgenstein 	/* re-configure the alias-guid and mcg's */
1090b9c5d6a6SOren Duer 	if (mlx4_is_master(dev->dev)) {
1091a0c64a17SJack Morgenstein 		mlx4_ib_invalidate_all_guid_record(dev, port_num);
1092a0c64a17SJack Morgenstein 
10932a4fae14SJack Morgenstein 		if (!dev->sriov.is_going_down) {
1094b9c5d6a6SOren Duer 			mlx4_ib_mcg_port_cleanup(&dev->sriov.demux[port_num - 1], 0);
10952a4fae14SJack Morgenstein 			mlx4_gen_slaves_port_mgt_ev(dev->dev, port_num,
10962a4fae14SJack Morgenstein 						    MLX4_EQ_PORT_INFO_CLIENT_REREG_MASK);
10972a4fae14SJack Morgenstein 		}
1098b9c5d6a6SOren Duer 	}
1099fd10ed8eSJack Morgenstein 
1100fd10ed8eSJack Morgenstein 	/* Update the sl to vl table from inside client rereg
1101fd10ed8eSJack Morgenstein 	 * only if in secure-host mode (snooping is not possible)
1102fd10ed8eSJack Morgenstein 	 * and the sl-to-vl change event is not generated by FW.
1103fd10ed8eSJack Morgenstein 	 */
1104fd10ed8eSJack Morgenstein 	if (!mlx4_is_slave(dev->dev) &&
1105fd10ed8eSJack Morgenstein 	    dev->dev->flags & MLX4_FLAG_SECURE_HOST &&
1106fd10ed8eSJack Morgenstein 	    !(dev->dev->caps.flags2 & MLX4_DEV_CAP_FLAG2_SL_TO_VL_CHANGE_EVENT)) {
1107fd10ed8eSJack Morgenstein 		if (mlx4_is_master(dev->dev))
1108fd10ed8eSJack Morgenstein 			/* already in work queue from mlx4_ib_event queueing
1109fd10ed8eSJack Morgenstein 			 * mlx4_handle_port_mgmt_change_event, which calls
1110fd10ed8eSJack Morgenstein 			 * this procedure. Therefore, call sl2vl_update directly.
1111fd10ed8eSJack Morgenstein 			 */
1112fd10ed8eSJack Morgenstein 			mlx4_ib_sl2vl_update(dev, port_num);
1113fd10ed8eSJack Morgenstein 		else
1114fd10ed8eSJack Morgenstein 			mlx4_sched_ib_sl2vl_update_work(dev, port_num);
1115fd10ed8eSJack Morgenstein 	}
1116b9c5d6a6SOren Duer 	mlx4_ib_dispatch_event(dev, port_num, IB_EVENT_CLIENT_REREGISTER);
1117b9c5d6a6SOren Duer }
1118b9c5d6a6SOren Duer 
propagate_pkey_ev(struct mlx4_ib_dev * dev,int port_num,struct mlx4_eqe * eqe)11192a4fae14SJack Morgenstein static void propagate_pkey_ev(struct mlx4_ib_dev *dev, int port_num,
11202a4fae14SJack Morgenstein 			      struct mlx4_eqe *eqe)
11212a4fae14SJack Morgenstein {
11222a4fae14SJack Morgenstein 	__propagate_pkey_ev(dev, port_num, GET_BLK_PTR_FROM_EQE(eqe),
11232a4fae14SJack Morgenstein 			    GET_MASK_FROM_EQE(eqe));
11242a4fae14SJack Morgenstein }
11252a4fae14SJack Morgenstein 
handle_slaves_guid_change(struct mlx4_ib_dev * dev,u32 port_num,u32 guid_tbl_blk_num,u32 change_bitmap)11261fb7f897SMark Bloch static void handle_slaves_guid_change(struct mlx4_ib_dev *dev, u32 port_num,
11272a4fae14SJack Morgenstein 				      u32 guid_tbl_blk_num, u32 change_bitmap)
11282a4fae14SJack Morgenstein {
11292a4fae14SJack Morgenstein 	struct ib_smp *in_mad  = NULL;
11302a4fae14SJack Morgenstein 	struct ib_smp *out_mad  = NULL;
11312a4fae14SJack Morgenstein 	u16 i;
11322a4fae14SJack Morgenstein 
11332a4fae14SJack Morgenstein 	if (!mlx4_is_mfunc(dev->dev) || !mlx4_is_master(dev->dev))
11342a4fae14SJack Morgenstein 		return;
11352a4fae14SJack Morgenstein 
11362a4fae14SJack Morgenstein 	in_mad  = kmalloc(sizeof *in_mad, GFP_KERNEL);
11372a4fae14SJack Morgenstein 	out_mad = kmalloc(sizeof *out_mad, GFP_KERNEL);
113815d4626eSLeon Romanovsky 	if (!in_mad || !out_mad)
11392a4fae14SJack Morgenstein 		goto out;
11402a4fae14SJack Morgenstein 
11412a4fae14SJack Morgenstein 	guid_tbl_blk_num  *= 4;
11422a4fae14SJack Morgenstein 
11432a4fae14SJack Morgenstein 	for (i = 0; i < 4; i++) {
11442a4fae14SJack Morgenstein 		if (change_bitmap && (!((change_bitmap >> (8 * i)) & 0xff)))
11452a4fae14SJack Morgenstein 			continue;
11462a4fae14SJack Morgenstein 		memset(in_mad, 0, sizeof *in_mad);
11472a4fae14SJack Morgenstein 		memset(out_mad, 0, sizeof *out_mad);
11482a4fae14SJack Morgenstein 
11492a4fae14SJack Morgenstein 		in_mad->base_version  = 1;
11502a4fae14SJack Morgenstein 		in_mad->mgmt_class    = IB_MGMT_CLASS_SUBN_LID_ROUTED;
11512a4fae14SJack Morgenstein 		in_mad->class_version = 1;
11522a4fae14SJack Morgenstein 		in_mad->method        = IB_MGMT_METHOD_GET;
11532a4fae14SJack Morgenstein 		in_mad->attr_id       = IB_SMP_ATTR_GUID_INFO;
11542a4fae14SJack Morgenstein 		in_mad->attr_mod      = cpu_to_be32(guid_tbl_blk_num + i);
11552a4fae14SJack Morgenstein 
11562a4fae14SJack Morgenstein 		if (mlx4_MAD_IFC(dev,
11572a4fae14SJack Morgenstein 				 MLX4_MAD_IFC_IGNORE_KEYS | MLX4_MAD_IFC_NET_VIEW,
11582a4fae14SJack Morgenstein 				 port_num, NULL, NULL, in_mad, out_mad)) {
11592a4fae14SJack Morgenstein 			mlx4_ib_warn(&dev->ib_dev, "Failed in get GUID INFO MAD_IFC\n");
11602a4fae14SJack Morgenstein 			goto out;
11612a4fae14SJack Morgenstein 		}
11622a4fae14SJack Morgenstein 
11632a4fae14SJack Morgenstein 		mlx4_ib_update_cache_on_guid_change(dev, guid_tbl_blk_num + i,
11642a4fae14SJack Morgenstein 						    port_num,
11652a4fae14SJack Morgenstein 						    (u8 *)(&((struct ib_smp *)out_mad)->data));
11662a4fae14SJack Morgenstein 		mlx4_ib_notify_slaves_on_guid_change(dev, guid_tbl_blk_num + i,
11672a4fae14SJack Morgenstein 						     port_num,
11682a4fae14SJack Morgenstein 						     (u8 *)(&((struct ib_smp *)out_mad)->data));
11692a4fae14SJack Morgenstein 	}
11702a4fae14SJack Morgenstein 
11712a4fae14SJack Morgenstein out:
11722a4fae14SJack Morgenstein 	kfree(in_mad);
11732a4fae14SJack Morgenstein 	kfree(out_mad);
11742a4fae14SJack Morgenstein 	return;
11752a4fae14SJack Morgenstein }
11762a4fae14SJack Morgenstein 
handle_port_mgmt_change_event(struct work_struct * work)117700f5ce99SJack Morgenstein void handle_port_mgmt_change_event(struct work_struct *work)
117800f5ce99SJack Morgenstein {
117900f5ce99SJack Morgenstein 	struct ib_event_work *ew = container_of(work, struct ib_event_work, work);
118000f5ce99SJack Morgenstein 	struct mlx4_ib_dev *dev = ew->ib_dev;
118100f5ce99SJack Morgenstein 	struct mlx4_eqe *eqe = &(ew->ib_eqe);
11821fb7f897SMark Bloch 	u32 port = eqe->event.port_mgmt_change.port;
118300f5ce99SJack Morgenstein 	u32 changed_attr;
11842a4fae14SJack Morgenstein 	u32 tbl_block;
11852a4fae14SJack Morgenstein 	u32 change_bitmap;
118600f5ce99SJack Morgenstein 
118700f5ce99SJack Morgenstein 	switch (eqe->subtype) {
118800f5ce99SJack Morgenstein 	case MLX4_DEV_PMC_SUBTYPE_PORT_INFO:
118900f5ce99SJack Morgenstein 		changed_attr = be32_to_cpu(eqe->event.port_mgmt_change.params.port_info.changed_attr);
119000f5ce99SJack Morgenstein 
119100f5ce99SJack Morgenstein 		/* Update the SM ah - This should be done before handling
119200f5ce99SJack Morgenstein 		   the other changed attributes so that MADs can be sent to the SM */
119300f5ce99SJack Morgenstein 		if (changed_attr & MSTR_SM_CHANGE_MASK) {
119400f5ce99SJack Morgenstein 			u16 lid = be16_to_cpu(eqe->event.port_mgmt_change.params.port_info.mstr_sm_lid);
119500f5ce99SJack Morgenstein 			u8 sl = eqe->event.port_mgmt_change.params.port_info.mstr_sm_sl & 0xf;
119600f5ce99SJack Morgenstein 			update_sm_ah(dev, port, lid, sl);
119700f5ce99SJack Morgenstein 		}
119800f5ce99SJack Morgenstein 
119900f5ce99SJack Morgenstein 		/* Check if it is a lid change event */
120000f5ce99SJack Morgenstein 		if (changed_attr & MLX4_EQ_PORT_INFO_LID_CHANGE_MASK)
12012a4fae14SJack Morgenstein 			handle_lid_change_event(dev, port);
120200f5ce99SJack Morgenstein 
120300f5ce99SJack Morgenstein 		/* Generate GUID changed event */
12042a4fae14SJack Morgenstein 		if (changed_attr & MLX4_EQ_PORT_INFO_GID_PFX_CHANGE_MASK) {
12058ec07bf8SJack Morgenstein 			if (mlx4_is_master(dev->dev)) {
12068ec07bf8SJack Morgenstein 				union ib_gid gid;
12078ec07bf8SJack Morgenstein 				int err = 0;
12088ec07bf8SJack Morgenstein 
12098ec07bf8SJack Morgenstein 				if (!eqe->event.port_mgmt_change.params.port_info.gid_prefix)
12108ec07bf8SJack Morgenstein 					err = __mlx4_ib_query_gid(&dev->ib_dev, port, 0, &gid, 1);
12118ec07bf8SJack Morgenstein 				else
12128ec07bf8SJack Morgenstein 					gid.global.subnet_prefix =
12138ec07bf8SJack Morgenstein 						eqe->event.port_mgmt_change.params.port_info.gid_prefix;
12148ec07bf8SJack Morgenstein 				if (err) {
12158ec07bf8SJack Morgenstein 					pr_warn("Could not change QP1 subnet prefix for port %d: query_gid error (%d)\n",
12168ec07bf8SJack Morgenstein 						port, err);
12178ec07bf8SJack Morgenstein 				} else {
12188ec07bf8SJack Morgenstein 					pr_debug("Changing QP1 subnet prefix for port %d. old=0x%llx. new=0x%llx\n",
12198ec07bf8SJack Morgenstein 						 port,
12208ec07bf8SJack Morgenstein 						 (u64)atomic64_read(&dev->sriov.demux[port - 1].subnet_prefix),
12218ec07bf8SJack Morgenstein 						 be64_to_cpu(gid.global.subnet_prefix));
12228ec07bf8SJack Morgenstein 					atomic64_set(&dev->sriov.demux[port - 1].subnet_prefix,
12238ec07bf8SJack Morgenstein 						     be64_to_cpu(gid.global.subnet_prefix));
12248ec07bf8SJack Morgenstein 				}
12258ec07bf8SJack Morgenstein 			}
122600f5ce99SJack Morgenstein 			mlx4_ib_dispatch_event(dev, port, IB_EVENT_GID_CHANGE);
12272a4fae14SJack Morgenstein 			/*if master, notify all slaves*/
12282a4fae14SJack Morgenstein 			if (mlx4_is_master(dev->dev))
12292a4fae14SJack Morgenstein 				mlx4_gen_slaves_port_mgt_ev(dev->dev, port,
12302a4fae14SJack Morgenstein 							    MLX4_EQ_PORT_INFO_GID_PFX_CHANGE_MASK);
12312a4fae14SJack Morgenstein 		}
123200f5ce99SJack Morgenstein 
123300f5ce99SJack Morgenstein 		if (changed_attr & MLX4_EQ_PORT_INFO_CLIENT_REREG_MASK)
1234b9c5d6a6SOren Duer 			handle_client_rereg_event(dev, port);
123500f5ce99SJack Morgenstein 		break;
123600f5ce99SJack Morgenstein 
123700f5ce99SJack Morgenstein 	case MLX4_DEV_PMC_SUBTYPE_PKEY_TABLE:
123800f5ce99SJack Morgenstein 		mlx4_ib_dispatch_event(dev, port, IB_EVENT_PKEY_CHANGE);
12392a4fae14SJack Morgenstein 		if (mlx4_is_master(dev->dev) && !dev->sriov.is_going_down)
12402a4fae14SJack Morgenstein 			propagate_pkey_ev(dev, port, eqe);
124100f5ce99SJack Morgenstein 		break;
124200f5ce99SJack Morgenstein 	case MLX4_DEV_PMC_SUBTYPE_GUID_INFO:
12436634961cSJack Morgenstein 		/* paravirtualized master's guid is guid 0 -- does not change */
12446634961cSJack Morgenstein 		if (!mlx4_is_master(dev->dev))
124500f5ce99SJack Morgenstein 			mlx4_ib_dispatch_event(dev, port, IB_EVENT_GID_CHANGE);
12462a4fae14SJack Morgenstein 		/*if master, notify relevant slaves*/
12472a4fae14SJack Morgenstein 		else if (!dev->sriov.is_going_down) {
12482a4fae14SJack Morgenstein 			tbl_block = GET_BLK_PTR_FROM_EQE(eqe);
12492a4fae14SJack Morgenstein 			change_bitmap = GET_MASK_FROM_EQE(eqe);
12502a4fae14SJack Morgenstein 			handle_slaves_guid_change(dev, port, tbl_block, change_bitmap);
12512a4fae14SJack Morgenstein 		}
125200f5ce99SJack Morgenstein 		break;
1253fd10ed8eSJack Morgenstein 
1254fd10ed8eSJack Morgenstein 	case MLX4_DEV_PMC_SUBTYPE_SL_TO_VL_MAP:
1255fd10ed8eSJack Morgenstein 		/* cache sl to vl mapping changes for use in
1256fd10ed8eSJack Morgenstein 		 * filling QP1 LRH VL field when sending packets
1257fd10ed8eSJack Morgenstein 		 */
1258fd10ed8eSJack Morgenstein 		if (!mlx4_is_slave(dev->dev)) {
1259fd10ed8eSJack Morgenstein 			union sl2vl_tbl_to_u64 sl2vl64;
1260fd10ed8eSJack Morgenstein 			int jj;
1261fd10ed8eSJack Morgenstein 
1262fd10ed8eSJack Morgenstein 			for (jj = 0; jj < 8; jj++) {
1263fd10ed8eSJack Morgenstein 				sl2vl64.sl8[jj] =
1264fd10ed8eSJack Morgenstein 					eqe->event.port_mgmt_change.params.sl2vl_tbl_change_info.sl2vl_table[jj];
1265fd10ed8eSJack Morgenstein 				pr_debug("port %u, sl2vl[%d] = %02x\n",
1266fd10ed8eSJack Morgenstein 					 port, jj, sl2vl64.sl8[jj]);
1267fd10ed8eSJack Morgenstein 			}
1268fd10ed8eSJack Morgenstein 			atomic64_set(&dev->sl2vl[port - 1], sl2vl64.sl64);
1269fd10ed8eSJack Morgenstein 		}
1270fd10ed8eSJack Morgenstein 		break;
127100f5ce99SJack Morgenstein 	default:
127200f5ce99SJack Morgenstein 		pr_warn("Unsupported subtype 0x%x for "
127300f5ce99SJack Morgenstein 			"Port Management Change event\n", eqe->subtype);
127400f5ce99SJack Morgenstein 	}
127500f5ce99SJack Morgenstein 
127600f5ce99SJack Morgenstein 	kfree(ew);
127700f5ce99SJack Morgenstein }
127800f5ce99SJack Morgenstein 
mlx4_ib_dispatch_event(struct mlx4_ib_dev * dev,u32 port_num,enum ib_event_type type)12791fb7f897SMark Bloch void mlx4_ib_dispatch_event(struct mlx4_ib_dev *dev, u32 port_num,
128000f5ce99SJack Morgenstein 			    enum ib_event_type type)
128100f5ce99SJack Morgenstein {
128200f5ce99SJack Morgenstein 	struct ib_event event;
128300f5ce99SJack Morgenstein 
128400f5ce99SJack Morgenstein 	event.device		= &dev->ib_dev;
128500f5ce99SJack Morgenstein 	event.element.port_num	= port_num;
128600f5ce99SJack Morgenstein 	event.event		= type;
128700f5ce99SJack Morgenstein 
128800f5ce99SJack Morgenstein 	ib_dispatch_event(&event);
128900f5ce99SJack Morgenstein }
1290fc06573dSJack Morgenstein 
mlx4_ib_tunnel_comp_handler(struct ib_cq * cq,void * arg)1291fc06573dSJack Morgenstein static void mlx4_ib_tunnel_comp_handler(struct ib_cq *cq, void *arg)
1292fc06573dSJack Morgenstein {
1293fc06573dSJack Morgenstein 	unsigned long flags;
1294fc06573dSJack Morgenstein 	struct mlx4_ib_demux_pv_ctx *ctx = cq->cq_context;
1295fc06573dSJack Morgenstein 	struct mlx4_ib_dev *dev = to_mdev(ctx->ib_dev);
1296fc06573dSJack Morgenstein 	spin_lock_irqsave(&dev->sriov.going_down_lock, flags);
1297fc06573dSJack Morgenstein 	if (!dev->sriov.is_going_down && ctx->state == DEMUX_PV_STATE_ACTIVE)
1298fc06573dSJack Morgenstein 		queue_work(ctx->wq, &ctx->work);
1299fc06573dSJack Morgenstein 	spin_unlock_irqrestore(&dev->sriov.going_down_lock, flags);
1300fc06573dSJack Morgenstein }
1301fc06573dSJack Morgenstein 
mlx4_ib_wire_comp_handler(struct ib_cq * cq,void * arg)13027fd1507dSHåkon Bugge static void mlx4_ib_wire_comp_handler(struct ib_cq *cq, void *arg)
13037fd1507dSHåkon Bugge {
13047fd1507dSHåkon Bugge 	unsigned long flags;
13057fd1507dSHåkon Bugge 	struct mlx4_ib_demux_pv_ctx *ctx = cq->cq_context;
13067fd1507dSHåkon Bugge 	struct mlx4_ib_dev *dev = to_mdev(ctx->ib_dev);
13077fd1507dSHåkon Bugge 
13087fd1507dSHåkon Bugge 	spin_lock_irqsave(&dev->sriov.going_down_lock, flags);
13097fd1507dSHåkon Bugge 	if (!dev->sriov.is_going_down && ctx->state == DEMUX_PV_STATE_ACTIVE)
13107fd1507dSHåkon Bugge 		queue_work(ctx->wi_wq, &ctx->work);
13117fd1507dSHåkon Bugge 	spin_unlock_irqrestore(&dev->sriov.going_down_lock, flags);
13127fd1507dSHåkon Bugge }
13137fd1507dSHåkon Bugge 
mlx4_ib_post_pv_qp_buf(struct mlx4_ib_demux_pv_ctx * ctx,struct mlx4_ib_demux_pv_qp * tun_qp,int index)1314fc06573dSJack Morgenstein static int mlx4_ib_post_pv_qp_buf(struct mlx4_ib_demux_pv_ctx *ctx,
1315fc06573dSJack Morgenstein 				  struct mlx4_ib_demux_pv_qp *tun_qp,
1316fc06573dSJack Morgenstein 				  int index)
1317fc06573dSJack Morgenstein {
1318fc06573dSJack Morgenstein 	struct ib_sge sg_list;
1319d34ac5cdSBart Van Assche 	struct ib_recv_wr recv_wr;
1320d34ac5cdSBart Van Assche 	const struct ib_recv_wr *bad_recv_wr;
1321fc06573dSJack Morgenstein 	int size;
1322fc06573dSJack Morgenstein 
1323fc06573dSJack Morgenstein 	size = (tun_qp->qp->qp_type == IB_QPT_UD) ?
1324fc06573dSJack Morgenstein 		sizeof (struct mlx4_tunnel_mad) : sizeof (struct mlx4_mad_rcv_buf);
1325fc06573dSJack Morgenstein 
1326fc06573dSJack Morgenstein 	sg_list.addr = tun_qp->ring[index].map;
1327fc06573dSJack Morgenstein 	sg_list.length = size;
13287dd97576SJason Gunthorpe 	sg_list.lkey = ctx->pd->local_dma_lkey;
1329fc06573dSJack Morgenstein 
1330fc06573dSJack Morgenstein 	recv_wr.next = NULL;
1331fc06573dSJack Morgenstein 	recv_wr.sg_list = &sg_list;
1332fc06573dSJack Morgenstein 	recv_wr.num_sge = 1;
1333fc06573dSJack Morgenstein 	recv_wr.wr_id = (u64) index | MLX4_TUN_WRID_RECV |
1334fc06573dSJack Morgenstein 		MLX4_TUN_SET_WRID_QPN(tun_qp->proxy_qpt);
1335fc06573dSJack Morgenstein 	ib_dma_sync_single_for_device(ctx->ib_dev, tun_qp->ring[index].map,
1336fc06573dSJack Morgenstein 				      size, DMA_FROM_DEVICE);
1337fc06573dSJack Morgenstein 	return ib_post_recv(tun_qp->qp, &recv_wr, &bad_recv_wr);
1338fc06573dSJack Morgenstein }
1339fc06573dSJack Morgenstein 
mlx4_ib_multiplex_sa_handler(struct ib_device * ibdev,int port,int slave,struct ib_sa_mad * sa_mad)134037bfc7c1SJack Morgenstein static int mlx4_ib_multiplex_sa_handler(struct ib_device *ibdev, int port,
134137bfc7c1SJack Morgenstein 		int slave, struct ib_sa_mad *sa_mad)
134237bfc7c1SJack Morgenstein {
1343b9c5d6a6SOren Duer 	int ret = 0;
1344b9c5d6a6SOren Duer 
1345b9c5d6a6SOren Duer 	/* dispatch to different sa handlers */
1346b9c5d6a6SOren Duer 	switch (be16_to_cpu(sa_mad->mad_hdr.attr_id)) {
1347b9c5d6a6SOren Duer 	case IB_SA_ATTR_MC_MEMBER_REC:
1348b9c5d6a6SOren Duer 		ret = mlx4_ib_mcg_multiplex_handler(ibdev, port, slave, sa_mad);
1349b9c5d6a6SOren Duer 		break;
1350b9c5d6a6SOren Duer 	default:
1351b9c5d6a6SOren Duer 		break;
1352b9c5d6a6SOren Duer 	}
1353b9c5d6a6SOren Duer 	return ret;
135437bfc7c1SJack Morgenstein }
135537bfc7c1SJack Morgenstein 
mlx4_ib_send_to_wire(struct mlx4_ib_dev * dev,int slave,u32 port,enum ib_qp_type dest_qpt,u16 pkey_index,u32 remote_qpn,u32 qkey,struct rdma_ah_attr * attr,u8 * s_mac,u16 vlan_id,struct ib_mad * mad)13561fb7f897SMark Bloch int mlx4_ib_send_to_wire(struct mlx4_ib_dev *dev, int slave, u32 port,
13575ea8bbfcSJack Morgenstein 			 enum ib_qp_type dest_qpt, u16 pkey_index,
135890898850SDasaratharaman Chandramouli 			 u32 remote_qpn, u32 qkey, struct rdma_ah_attr *attr,
1359dbf727deSMatan Barak 			 u8 *s_mac, u16 vlan_id, struct ib_mad *mad)
136037bfc7c1SJack Morgenstein {
136137bfc7c1SJack Morgenstein 	struct ib_sge list;
1362e622f2f4SChristoph Hellwig 	struct ib_ud_wr wr;
1363d34ac5cdSBart Van Assche 	const struct ib_send_wr *bad_wr;
136437bfc7c1SJack Morgenstein 	struct mlx4_ib_demux_pv_ctx *sqp_ctx;
136537bfc7c1SJack Morgenstein 	struct mlx4_ib_demux_pv_qp *sqp;
136637bfc7c1SJack Morgenstein 	struct mlx4_mad_snd_buf *sqp_mad;
136737bfc7c1SJack Morgenstein 	struct ib_ah *ah;
136837bfc7c1SJack Morgenstein 	struct ib_qp *send_qp = NULL;
136937bfc7c1SJack Morgenstein 	unsigned wire_tx_ix = 0;
137037bfc7c1SJack Morgenstein 	u16 wire_pkey_ix;
137137bfc7c1SJack Morgenstein 	int src_qpnum;
1372d3456914SLeon Romanovsky 	int ret;
137337bfc7c1SJack Morgenstein 
137437bfc7c1SJack Morgenstein 	sqp_ctx = dev->sriov.sqps[port-1];
137537bfc7c1SJack Morgenstein 
137637bfc7c1SJack Morgenstein 	/* check if proxy qp created */
137737bfc7c1SJack Morgenstein 	if (!sqp_ctx || sqp_ctx->state != DEMUX_PV_STATE_ACTIVE)
137837bfc7c1SJack Morgenstein 		return -EAGAIN;
137937bfc7c1SJack Morgenstein 
138037bfc7c1SJack Morgenstein 	if (dest_qpt == IB_QPT_SMI) {
138137bfc7c1SJack Morgenstein 		src_qpnum = 0;
138237bfc7c1SJack Morgenstein 		sqp = &sqp_ctx->qp[0];
138337bfc7c1SJack Morgenstein 		wire_pkey_ix = dev->pkeys.virt2phys_pkey[slave][port - 1][0];
138437bfc7c1SJack Morgenstein 	} else {
138537bfc7c1SJack Morgenstein 		src_qpnum = 1;
138637bfc7c1SJack Morgenstein 		sqp = &sqp_ctx->qp[1];
138737bfc7c1SJack Morgenstein 		wire_pkey_ix = dev->pkeys.virt2phys_pkey[slave][port - 1][pkey_index];
138837bfc7c1SJack Morgenstein 	}
138937bfc7c1SJack Morgenstein 
139037bfc7c1SJack Morgenstein 	send_qp = sqp->qp;
139137bfc7c1SJack Morgenstein 
1392d3456914SLeon Romanovsky 	ah = rdma_zalloc_drv_obj(sqp_ctx->pd->device, ib_ah);
1393d3456914SLeon Romanovsky 	if (!ah)
1394d3456914SLeon Romanovsky 		return -ENOMEM;
1395d3456914SLeon Romanovsky 
1396d3456914SLeon Romanovsky 	ah->device = sqp_ctx->pd->device;
1397d3456914SLeon Romanovsky 	ah->pd = sqp_ctx->pd;
1398d3456914SLeon Romanovsky 
139937bfc7c1SJack Morgenstein 	/* create ah */
1400d3456914SLeon Romanovsky 	ret = mlx4_ib_create_ah_slave(ah, attr,
14015e62d5ffSJason Gunthorpe 				      rdma_ah_retrieve_grh(attr)->sgid_index,
14025e62d5ffSJason Gunthorpe 				      s_mac, vlan_id);
1403d3456914SLeon Romanovsky 	if (ret)
1404d3456914SLeon Romanovsky 		goto out;
1405d3456914SLeon Romanovsky 
140637bfc7c1SJack Morgenstein 	spin_lock(&sqp->tx_lock);
140737bfc7c1SJack Morgenstein 	if (sqp->tx_ix_head - sqp->tx_ix_tail >=
14080ae207fbSHåkon Bugge 	    (MLX4_NUM_WIRE_BUFS - 1))
140937bfc7c1SJack Morgenstein 		ret = -EAGAIN;
141037bfc7c1SJack Morgenstein 	else
14110ae207fbSHåkon Bugge 		wire_tx_ix = (++sqp->tx_ix_head) & (MLX4_NUM_WIRE_BUFS - 1);
141237bfc7c1SJack Morgenstein 	spin_unlock(&sqp->tx_lock);
141337bfc7c1SJack Morgenstein 	if (ret)
141437bfc7c1SJack Morgenstein 		goto out;
141537bfc7c1SJack Morgenstein 
141637bfc7c1SJack Morgenstein 	sqp_mad = (struct mlx4_mad_snd_buf *) (sqp->tx_ring[wire_tx_ix].buf.addr);
1417d3456914SLeon Romanovsky 	kfree(sqp->tx_ring[wire_tx_ix].ah);
141837bfc7c1SJack Morgenstein 	sqp->tx_ring[wire_tx_ix].ah = ah;
141937bfc7c1SJack Morgenstein 	ib_dma_sync_single_for_cpu(&dev->ib_dev,
142037bfc7c1SJack Morgenstein 				   sqp->tx_ring[wire_tx_ix].buf.map,
142137bfc7c1SJack Morgenstein 				   sizeof (struct mlx4_mad_snd_buf),
142237bfc7c1SJack Morgenstein 				   DMA_TO_DEVICE);
142337bfc7c1SJack Morgenstein 
142437bfc7c1SJack Morgenstein 	memcpy(&sqp_mad->payload, mad, sizeof *mad);
142537bfc7c1SJack Morgenstein 
142637bfc7c1SJack Morgenstein 	ib_dma_sync_single_for_device(&dev->ib_dev,
142737bfc7c1SJack Morgenstein 				      sqp->tx_ring[wire_tx_ix].buf.map,
142837bfc7c1SJack Morgenstein 				      sizeof (struct mlx4_mad_snd_buf),
142937bfc7c1SJack Morgenstein 				      DMA_TO_DEVICE);
143037bfc7c1SJack Morgenstein 
143137bfc7c1SJack Morgenstein 	list.addr = sqp->tx_ring[wire_tx_ix].buf.map;
143237bfc7c1SJack Morgenstein 	list.length = sizeof (struct mlx4_mad_snd_buf);
14337dd97576SJason Gunthorpe 	list.lkey = sqp_ctx->pd->local_dma_lkey;
143437bfc7c1SJack Morgenstein 
1435e622f2f4SChristoph Hellwig 	wr.ah = ah;
1436e622f2f4SChristoph Hellwig 	wr.port_num = port;
1437e622f2f4SChristoph Hellwig 	wr.pkey_index = wire_pkey_ix;
1438e622f2f4SChristoph Hellwig 	wr.remote_qkey = qkey;
1439e622f2f4SChristoph Hellwig 	wr.remote_qpn = remote_qpn;
1440e622f2f4SChristoph Hellwig 	wr.wr.next = NULL;
1441e622f2f4SChristoph Hellwig 	wr.wr.wr_id = ((u64) wire_tx_ix) | MLX4_TUN_SET_WRID_QPN(src_qpnum);
1442e622f2f4SChristoph Hellwig 	wr.wr.sg_list = &list;
1443e622f2f4SChristoph Hellwig 	wr.wr.num_sge = 1;
1444e622f2f4SChristoph Hellwig 	wr.wr.opcode = IB_WR_SEND;
1445e622f2f4SChristoph Hellwig 	wr.wr.send_flags = IB_SEND_SIGNALED;
144637bfc7c1SJack Morgenstein 
1447e622f2f4SChristoph Hellwig 	ret = ib_post_send(send_qp, &wr.wr, &bad_wr);
1448a6100603SYishai Hadas 	if (!ret)
1449a6100603SYishai Hadas 		return 0;
1450a6100603SYishai Hadas 
1451a6100603SYishai Hadas 	spin_lock(&sqp->tx_lock);
1452a6100603SYishai Hadas 	sqp->tx_ix_tail++;
1453a6100603SYishai Hadas 	spin_unlock(&sqp->tx_lock);
1454a6100603SYishai Hadas 	sqp->tx_ring[wire_tx_ix].ah = NULL;
145537bfc7c1SJack Morgenstein out:
1456d3456914SLeon Romanovsky 	kfree(ah);
145737bfc7c1SJack Morgenstein 	return ret;
145837bfc7c1SJack Morgenstein }
145937bfc7c1SJack Morgenstein 
get_slave_base_gid_ix(struct mlx4_ib_dev * dev,int slave,int port)1460b6ffaeffSJack Morgenstein static int get_slave_base_gid_ix(struct mlx4_ib_dev *dev, int slave, int port)
1461b6ffaeffSJack Morgenstein {
1462b6ffaeffSJack Morgenstein 	if (rdma_port_get_link_layer(&dev->ib_dev, port) == IB_LINK_LAYER_INFINIBAND)
1463b6ffaeffSJack Morgenstein 		return slave;
1464449fc488SMatan Barak 	return mlx4_get_base_gid_ix(dev->dev, slave, port);
1465b6ffaeffSJack Morgenstein }
1466b6ffaeffSJack Morgenstein 
fill_in_real_sgid_index(struct mlx4_ib_dev * dev,int slave,int port,struct rdma_ah_attr * ah_attr)1467b6ffaeffSJack Morgenstein static void fill_in_real_sgid_index(struct mlx4_ib_dev *dev, int slave, int port,
146890898850SDasaratharaman Chandramouli 				    struct rdma_ah_attr *ah_attr)
1469b6ffaeffSJack Morgenstein {
1470d8966fcdSDasaratharaman Chandramouli 	struct ib_global_route *grh = rdma_ah_retrieve_grh(ah_attr);
1471b6ffaeffSJack Morgenstein 	if (rdma_port_get_link_layer(&dev->ib_dev, port) == IB_LINK_LAYER_INFINIBAND)
1472d8966fcdSDasaratharaman Chandramouli 		grh->sgid_index = slave;
1473b6ffaeffSJack Morgenstein 	else
1474d8966fcdSDasaratharaman Chandramouli 		grh->sgid_index += get_slave_base_gid_ix(dev, slave, port);
1475b6ffaeffSJack Morgenstein }
1476b6ffaeffSJack Morgenstein 
mlx4_ib_multiplex_mad(struct mlx4_ib_demux_pv_ctx * ctx,struct ib_wc * wc)147737bfc7c1SJack Morgenstein static void mlx4_ib_multiplex_mad(struct mlx4_ib_demux_pv_ctx *ctx, struct ib_wc *wc)
147837bfc7c1SJack Morgenstein {
147937bfc7c1SJack Morgenstein 	struct mlx4_ib_dev *dev = to_mdev(ctx->ib_dev);
148037bfc7c1SJack Morgenstein 	struct mlx4_ib_demux_pv_qp *tun_qp = &ctx->qp[MLX4_TUN_WRID_QPN(wc->wr_id)];
148137bfc7c1SJack Morgenstein 	int wr_ix = wc->wr_id & (MLX4_NUM_TUNNEL_BUFS - 1);
148237bfc7c1SJack Morgenstein 	struct mlx4_tunnel_mad *tunnel = tun_qp->ring[wr_ix].addr;
148337bfc7c1SJack Morgenstein 	struct mlx4_ib_ah ah;
148490898850SDasaratharaman Chandramouli 	struct rdma_ah_attr ah_attr;
148537bfc7c1SJack Morgenstein 	u8 *slave_id;
148637bfc7c1SJack Morgenstein 	int slave;
1487449fc488SMatan Barak 	int port;
1488dbf727deSMatan Barak 	u16 vlan_id;
1489d8966fcdSDasaratharaman Chandramouli 	u8 qos;
1490d8966fcdSDasaratharaman Chandramouli 	u8 *dmac;
149109461944SHåkon Bugge 	int sts;
149237bfc7c1SJack Morgenstein 
149337bfc7c1SJack Morgenstein 	/* Get slave that sent this packet */
149447605df9SJack Morgenstein 	if (wc->src_qp < dev->dev->phys_caps.base_proxy_sqpn ||
149547605df9SJack Morgenstein 	    wc->src_qp >= dev->dev->phys_caps.base_proxy_sqpn + 8 * MLX4_MFUNC_MAX ||
149637bfc7c1SJack Morgenstein 	    (wc->src_qp & 0x1) != ctx->port - 1 ||
149737bfc7c1SJack Morgenstein 	    wc->src_qp & 0x4) {
149837bfc7c1SJack Morgenstein 		mlx4_ib_warn(ctx->ib_dev, "can't multiplex bad sqp:%d\n", wc->src_qp);
149937bfc7c1SJack Morgenstein 		return;
150037bfc7c1SJack Morgenstein 	}
150147605df9SJack Morgenstein 	slave = ((wc->src_qp & ~0x7) - dev->dev->phys_caps.base_proxy_sqpn) / 8;
150237bfc7c1SJack Morgenstein 	if (slave != ctx->slave) {
150337bfc7c1SJack Morgenstein 		mlx4_ib_warn(ctx->ib_dev, "can't multiplex bad sqp:%d: "
150437bfc7c1SJack Morgenstein 			     "belongs to another slave\n", wc->src_qp);
150537bfc7c1SJack Morgenstein 		return;
150637bfc7c1SJack Morgenstein 	}
150737bfc7c1SJack Morgenstein 
150837bfc7c1SJack Morgenstein 	/* Map transaction ID */
150937bfc7c1SJack Morgenstein 	ib_dma_sync_single_for_cpu(ctx->ib_dev, tun_qp->ring[wr_ix].map,
151037bfc7c1SJack Morgenstein 				   sizeof (struct mlx4_tunnel_mad),
151137bfc7c1SJack Morgenstein 				   DMA_FROM_DEVICE);
151237bfc7c1SJack Morgenstein 	switch (tunnel->mad.mad_hdr.method) {
151337bfc7c1SJack Morgenstein 	case IB_MGMT_METHOD_SET:
151437bfc7c1SJack Morgenstein 	case IB_MGMT_METHOD_GET:
151537bfc7c1SJack Morgenstein 	case IB_MGMT_METHOD_REPORT:
151637bfc7c1SJack Morgenstein 	case IB_SA_METHOD_GET_TABLE:
151737bfc7c1SJack Morgenstein 	case IB_SA_METHOD_DELETE:
151837bfc7c1SJack Morgenstein 	case IB_SA_METHOD_GET_MULTI:
151937bfc7c1SJack Morgenstein 	case IB_SA_METHOD_GET_TRACE_TBL:
152037bfc7c1SJack Morgenstein 		slave_id = (u8 *) &tunnel->mad.mad_hdr.tid;
152137bfc7c1SJack Morgenstein 		if (*slave_id) {
152237bfc7c1SJack Morgenstein 			mlx4_ib_warn(ctx->ib_dev, "egress mad has non-null tid msb:%d "
152337bfc7c1SJack Morgenstein 				     "class:%d slave:%d\n", *slave_id,
152437bfc7c1SJack Morgenstein 				     tunnel->mad.mad_hdr.mgmt_class, slave);
152537bfc7c1SJack Morgenstein 			return;
152637bfc7c1SJack Morgenstein 		} else
152737bfc7c1SJack Morgenstein 			*slave_id = slave;
1528667d457fSGustavo A. R. Silva 		break;
152937bfc7c1SJack Morgenstein 	default:
153037bfc7c1SJack Morgenstein 		/* nothing */;
153137bfc7c1SJack Morgenstein 	}
153237bfc7c1SJack Morgenstein 
153337bfc7c1SJack Morgenstein 	/* Class-specific handling */
153437bfc7c1SJack Morgenstein 	switch (tunnel->mad.mad_hdr.mgmt_class) {
153597982f5aSJack Morgenstein 	case IB_MGMT_CLASS_SUBN_LID_ROUTED:
153697982f5aSJack Morgenstein 	case IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE:
153797982f5aSJack Morgenstein 		if (slave != mlx4_master_func_num(dev->dev) &&
153897982f5aSJack Morgenstein 		    !mlx4_vf_smi_enabled(dev->dev, slave, ctx->port))
153997982f5aSJack Morgenstein 			return;
154097982f5aSJack Morgenstein 		break;
154137bfc7c1SJack Morgenstein 	case IB_MGMT_CLASS_SUBN_ADM:
154237bfc7c1SJack Morgenstein 		if (mlx4_ib_multiplex_sa_handler(ctx->ib_dev, ctx->port, slave,
154337bfc7c1SJack Morgenstein 			      (struct ib_sa_mad *) &tunnel->mad))
154437bfc7c1SJack Morgenstein 			return;
154537bfc7c1SJack Morgenstein 		break;
15463cf69cc8SAmir Vadai 	case IB_MGMT_CLASS_CM:
15473cf69cc8SAmir Vadai 		if (mlx4_ib_multiplex_cm_handler(ctx->ib_dev, ctx->port, slave,
15483cf69cc8SAmir Vadai 			      (struct ib_mad *) &tunnel->mad))
15493cf69cc8SAmir Vadai 			return;
15503cf69cc8SAmir Vadai 		break;
155137bfc7c1SJack Morgenstein 	case IB_MGMT_CLASS_DEVICE_MGMT:
155237bfc7c1SJack Morgenstein 		if (tunnel->mad.mad_hdr.method != IB_MGMT_METHOD_GET &&
155337bfc7c1SJack Morgenstein 		    tunnel->mad.mad_hdr.method != IB_MGMT_METHOD_SET)
155437bfc7c1SJack Morgenstein 			return;
155537bfc7c1SJack Morgenstein 		break;
155637bfc7c1SJack Morgenstein 	default:
155737bfc7c1SJack Morgenstein 		/* Drop unsupported classes for slaves in tunnel mode */
155837bfc7c1SJack Morgenstein 		if (slave != mlx4_master_func_num(dev->dev)) {
155937bfc7c1SJack Morgenstein 			mlx4_ib_warn(ctx->ib_dev, "dropping unsupported egress mad from class:%d "
156037bfc7c1SJack Morgenstein 				     "for slave:%d\n", tunnel->mad.mad_hdr.mgmt_class, slave);
156137bfc7c1SJack Morgenstein 			return;
156237bfc7c1SJack Morgenstein 		}
156337bfc7c1SJack Morgenstein 	}
156437bfc7c1SJack Morgenstein 
156537bfc7c1SJack Morgenstein 	/* We are using standard ib_core services to send the mad, so generate a
156637bfc7c1SJack Morgenstein 	 * stadard address handle by decoding the tunnelled mlx4_ah fields */
156737bfc7c1SJack Morgenstein 	memcpy(&ah.av, &tunnel->hdr.av, sizeof (struct mlx4_av));
156837bfc7c1SJack Morgenstein 	ah.ibah.device = ctx->ib_dev;
1569430910b1SOr Gerlitz 
1570430910b1SOr Gerlitz 	port = be32_to_cpu(ah.av.ib.port_pd) >> 24;
1571430910b1SOr Gerlitz 	port = mlx4_slave_convert_port(dev->dev, slave, port);
1572430910b1SOr Gerlitz 	if (port < 0)
1573430910b1SOr Gerlitz 		return;
1574430910b1SOr Gerlitz 	ah.av.ib.port_pd = cpu_to_be32(port << 24 | (be32_to_cpu(ah.av.ib.port_pd) & 0xffffff));
1575eed76245SJack Morgenstein 	ah.ibah.type = rdma_ah_find_type(&dev->ib_dev, port);
1576430910b1SOr Gerlitz 
157737bfc7c1SJack Morgenstein 	mlx4_ib_query_ah(&ah.ibah, &ah_attr);
1578d8966fcdSDasaratharaman Chandramouli 	if (rdma_ah_get_ah_flags(&ah_attr) & IB_AH_GRH)
1579b6ffaeffSJack Morgenstein 		fill_in_real_sgid_index(dev, slave, ctx->port, &ah_attr);
1580d8966fcdSDasaratharaman Chandramouli 	dmac = rdma_ah_retrieve_dmac(&ah_attr);
1581d8966fcdSDasaratharaman Chandramouli 	if (dmac)
1582d8966fcdSDasaratharaman Chandramouli 		memcpy(dmac, tunnel->hdr.mac, ETH_ALEN);
1583dbf727deSMatan Barak 	vlan_id = be16_to_cpu(tunnel->hdr.vlan);
15845ea8bbfcSJack Morgenstein 	/* if slave have default vlan use it */
1585d8966fcdSDasaratharaman Chandramouli 	if (mlx4_get_slave_default_vlan(dev->dev, ctx->port, slave,
1586d8966fcdSDasaratharaman Chandramouli 					&vlan_id, &qos))
1587d8966fcdSDasaratharaman Chandramouli 		rdma_ah_set_sl(&ah_attr, qos);
15885ea8bbfcSJack Morgenstein 
158909461944SHåkon Bugge 	sts = mlx4_ib_send_to_wire(dev, slave, ctx->port,
159037bfc7c1SJack Morgenstein 				   is_proxy_qp0(dev, wc->src_qp, slave) ?
159137bfc7c1SJack Morgenstein 				   IB_QPT_SMI : IB_QPT_GSI,
159237bfc7c1SJack Morgenstein 				   be16_to_cpu(tunnel->hdr.pkey_index),
159337bfc7c1SJack Morgenstein 				   be32_to_cpu(tunnel->hdr.remote_qpn),
159437bfc7c1SJack Morgenstein 				   be32_to_cpu(tunnel->hdr.qkey),
1595dbf727deSMatan Barak 				   &ah_attr, wc->smac, vlan_id, &tunnel->mad);
159609461944SHåkon Bugge 	if (sts)
159709461944SHåkon Bugge 		pr_debug("failed sending %s to wire on behalf of slave %d (%d)\n",
159809461944SHåkon Bugge 			 is_proxy_qp0(dev, wc->src_qp, slave) ? "SMI" : "GSI",
159909461944SHåkon Bugge 			 slave, sts);
160037bfc7c1SJack Morgenstein }
160137bfc7c1SJack Morgenstein 
mlx4_ib_alloc_pv_bufs(struct mlx4_ib_demux_pv_ctx * ctx,enum ib_qp_type qp_type,int is_tun)1602fc06573dSJack Morgenstein static int mlx4_ib_alloc_pv_bufs(struct mlx4_ib_demux_pv_ctx *ctx,
1603fc06573dSJack Morgenstein 				 enum ib_qp_type qp_type, int is_tun)
1604fc06573dSJack Morgenstein {
1605fc06573dSJack Morgenstein 	int i;
1606fc06573dSJack Morgenstein 	struct mlx4_ib_demux_pv_qp *tun_qp;
1607fc06573dSJack Morgenstein 	int rx_buf_size, tx_buf_size;
16080ae207fbSHåkon Bugge 	const int nmbr_bufs = is_tun ? MLX4_NUM_TUNNEL_BUFS : MLX4_NUM_WIRE_BUFS;
1609fc06573dSJack Morgenstein 
1610fc06573dSJack Morgenstein 	if (qp_type > IB_QPT_GSI)
1611fc06573dSJack Morgenstein 		return -EINVAL;
1612fc06573dSJack Morgenstein 
1613fc06573dSJack Morgenstein 	tun_qp = &ctx->qp[qp_type];
1614fc06573dSJack Morgenstein 
16150ae207fbSHåkon Bugge 	tun_qp->ring = kcalloc(nmbr_bufs,
16166396bb22SKees Cook 			       sizeof(struct mlx4_ib_buf),
1617fc06573dSJack Morgenstein 			       GFP_KERNEL);
1618fc06573dSJack Morgenstein 	if (!tun_qp->ring)
1619fc06573dSJack Morgenstein 		return -ENOMEM;
1620fc06573dSJack Morgenstein 
16210ae207fbSHåkon Bugge 	tun_qp->tx_ring = kcalloc(nmbr_bufs,
1622fc06573dSJack Morgenstein 				  sizeof (struct mlx4_ib_tun_tx_buf),
1623fc06573dSJack Morgenstein 				  GFP_KERNEL);
1624fc06573dSJack Morgenstein 	if (!tun_qp->tx_ring) {
1625fc06573dSJack Morgenstein 		kfree(tun_qp->ring);
1626fc06573dSJack Morgenstein 		tun_qp->ring = NULL;
1627fc06573dSJack Morgenstein 		return -ENOMEM;
1628fc06573dSJack Morgenstein 	}
1629fc06573dSJack Morgenstein 
1630fc06573dSJack Morgenstein 	if (is_tun) {
1631fc06573dSJack Morgenstein 		rx_buf_size = sizeof (struct mlx4_tunnel_mad);
1632fc06573dSJack Morgenstein 		tx_buf_size = sizeof (struct mlx4_rcv_tunnel_mad);
1633fc06573dSJack Morgenstein 	} else {
1634fc06573dSJack Morgenstein 		rx_buf_size = sizeof (struct mlx4_mad_rcv_buf);
1635fc06573dSJack Morgenstein 		tx_buf_size = sizeof (struct mlx4_mad_snd_buf);
1636fc06573dSJack Morgenstein 	}
1637fc06573dSJack Morgenstein 
16380ae207fbSHåkon Bugge 	for (i = 0; i < nmbr_bufs; i++) {
1639fc06573dSJack Morgenstein 		tun_qp->ring[i].addr = kmalloc(rx_buf_size, GFP_KERNEL);
1640fc06573dSJack Morgenstein 		if (!tun_qp->ring[i].addr)
1641fc06573dSJack Morgenstein 			goto err;
1642fc06573dSJack Morgenstein 		tun_qp->ring[i].map = ib_dma_map_single(ctx->ib_dev,
1643fc06573dSJack Morgenstein 							tun_qp->ring[i].addr,
1644fc06573dSJack Morgenstein 							rx_buf_size,
1645fc06573dSJack Morgenstein 							DMA_FROM_DEVICE);
1646cc47d369SSebastian Ott 		if (ib_dma_mapping_error(ctx->ib_dev, tun_qp->ring[i].map)) {
1647cc47d369SSebastian Ott 			kfree(tun_qp->ring[i].addr);
1648cc47d369SSebastian Ott 			goto err;
1649cc47d369SSebastian Ott 		}
1650fc06573dSJack Morgenstein 	}
1651fc06573dSJack Morgenstein 
16520ae207fbSHåkon Bugge 	for (i = 0; i < nmbr_bufs; i++) {
1653fc06573dSJack Morgenstein 		tun_qp->tx_ring[i].buf.addr =
1654fc06573dSJack Morgenstein 			kmalloc(tx_buf_size, GFP_KERNEL);
1655fc06573dSJack Morgenstein 		if (!tun_qp->tx_ring[i].buf.addr)
1656fc06573dSJack Morgenstein 			goto tx_err;
1657fc06573dSJack Morgenstein 		tun_qp->tx_ring[i].buf.map =
1658fc06573dSJack Morgenstein 			ib_dma_map_single(ctx->ib_dev,
1659fc06573dSJack Morgenstein 					  tun_qp->tx_ring[i].buf.addr,
1660fc06573dSJack Morgenstein 					  tx_buf_size,
1661fc06573dSJack Morgenstein 					  DMA_TO_DEVICE);
1662cc47d369SSebastian Ott 		if (ib_dma_mapping_error(ctx->ib_dev,
1663cc47d369SSebastian Ott 					 tun_qp->tx_ring[i].buf.map)) {
1664cc47d369SSebastian Ott 			kfree(tun_qp->tx_ring[i].buf.addr);
1665cc47d369SSebastian Ott 			goto tx_err;
1666cc47d369SSebastian Ott 		}
1667fc06573dSJack Morgenstein 		tun_qp->tx_ring[i].ah = NULL;
1668fc06573dSJack Morgenstein 	}
1669fc06573dSJack Morgenstein 	spin_lock_init(&tun_qp->tx_lock);
1670fc06573dSJack Morgenstein 	tun_qp->tx_ix_head = 0;
1671fc06573dSJack Morgenstein 	tun_qp->tx_ix_tail = 0;
1672fc06573dSJack Morgenstein 	tun_qp->proxy_qpt = qp_type;
1673fc06573dSJack Morgenstein 
1674fc06573dSJack Morgenstein 	return 0;
1675fc06573dSJack Morgenstein 
1676fc06573dSJack Morgenstein tx_err:
1677fc06573dSJack Morgenstein 	while (i > 0) {
1678fc06573dSJack Morgenstein 		--i;
1679fc06573dSJack Morgenstein 		ib_dma_unmap_single(ctx->ib_dev, tun_qp->tx_ring[i].buf.map,
1680fc06573dSJack Morgenstein 				    tx_buf_size, DMA_TO_DEVICE);
1681fc06573dSJack Morgenstein 		kfree(tun_qp->tx_ring[i].buf.addr);
1682fc06573dSJack Morgenstein 	}
16830ae207fbSHåkon Bugge 	i = nmbr_bufs;
1684fc06573dSJack Morgenstein err:
1685fc06573dSJack Morgenstein 	while (i > 0) {
1686fc06573dSJack Morgenstein 		--i;
1687fc06573dSJack Morgenstein 		ib_dma_unmap_single(ctx->ib_dev, tun_qp->ring[i].map,
1688fc06573dSJack Morgenstein 				    rx_buf_size, DMA_FROM_DEVICE);
1689fc06573dSJack Morgenstein 		kfree(tun_qp->ring[i].addr);
1690fc06573dSJack Morgenstein 	}
16915c1baaa8SWenwen Wang 	kfree(tun_qp->tx_ring);
16925c1baaa8SWenwen Wang 	tun_qp->tx_ring = NULL;
1693fc06573dSJack Morgenstein 	kfree(tun_qp->ring);
1694fc06573dSJack Morgenstein 	tun_qp->ring = NULL;
1695fc06573dSJack Morgenstein 	return -ENOMEM;
1696fc06573dSJack Morgenstein }
1697fc06573dSJack Morgenstein 
mlx4_ib_free_pv_qp_bufs(struct mlx4_ib_demux_pv_ctx * ctx,enum ib_qp_type qp_type,int is_tun)1698fc06573dSJack Morgenstein static void mlx4_ib_free_pv_qp_bufs(struct mlx4_ib_demux_pv_ctx *ctx,
1699fc06573dSJack Morgenstein 				     enum ib_qp_type qp_type, int is_tun)
1700fc06573dSJack Morgenstein {
1701fc06573dSJack Morgenstein 	int i;
1702fc06573dSJack Morgenstein 	struct mlx4_ib_demux_pv_qp *tun_qp;
1703fc06573dSJack Morgenstein 	int rx_buf_size, tx_buf_size;
17040ae207fbSHåkon Bugge 	const int nmbr_bufs = is_tun ? MLX4_NUM_TUNNEL_BUFS : MLX4_NUM_WIRE_BUFS;
1705fc06573dSJack Morgenstein 
1706fc06573dSJack Morgenstein 	if (qp_type > IB_QPT_GSI)
1707fc06573dSJack Morgenstein 		return;
1708fc06573dSJack Morgenstein 
1709fc06573dSJack Morgenstein 	tun_qp = &ctx->qp[qp_type];
1710fc06573dSJack Morgenstein 	if (is_tun) {
1711fc06573dSJack Morgenstein 		rx_buf_size = sizeof (struct mlx4_tunnel_mad);
1712fc06573dSJack Morgenstein 		tx_buf_size = sizeof (struct mlx4_rcv_tunnel_mad);
1713fc06573dSJack Morgenstein 	} else {
1714fc06573dSJack Morgenstein 		rx_buf_size = sizeof (struct mlx4_mad_rcv_buf);
1715fc06573dSJack Morgenstein 		tx_buf_size = sizeof (struct mlx4_mad_snd_buf);
1716fc06573dSJack Morgenstein 	}
1717fc06573dSJack Morgenstein 
1718fc06573dSJack Morgenstein 
17190ae207fbSHåkon Bugge 	for (i = 0; i < nmbr_bufs; i++) {
1720fc06573dSJack Morgenstein 		ib_dma_unmap_single(ctx->ib_dev, tun_qp->ring[i].map,
1721fc06573dSJack Morgenstein 				    rx_buf_size, DMA_FROM_DEVICE);
1722fc06573dSJack Morgenstein 		kfree(tun_qp->ring[i].addr);
1723fc06573dSJack Morgenstein 	}
1724fc06573dSJack Morgenstein 
17250ae207fbSHåkon Bugge 	for (i = 0; i < nmbr_bufs; i++) {
1726fc06573dSJack Morgenstein 		ib_dma_unmap_single(ctx->ib_dev, tun_qp->tx_ring[i].buf.map,
1727fc06573dSJack Morgenstein 				    tx_buf_size, DMA_TO_DEVICE);
1728fc06573dSJack Morgenstein 		kfree(tun_qp->tx_ring[i].buf.addr);
1729fc06573dSJack Morgenstein 		if (tun_qp->tx_ring[i].ah)
17302553ba21SGal Pressman 			rdma_destroy_ah(tun_qp->tx_ring[i].ah, 0);
1731fc06573dSJack Morgenstein 	}
1732fc06573dSJack Morgenstein 	kfree(tun_qp->tx_ring);
1733fc06573dSJack Morgenstein 	kfree(tun_qp->ring);
1734fc06573dSJack Morgenstein }
1735fc06573dSJack Morgenstein 
mlx4_ib_tunnel_comp_worker(struct work_struct * work)1736fc06573dSJack Morgenstein static void mlx4_ib_tunnel_comp_worker(struct work_struct *work)
1737fc06573dSJack Morgenstein {
173837bfc7c1SJack Morgenstein 	struct mlx4_ib_demux_pv_ctx *ctx;
173937bfc7c1SJack Morgenstein 	struct mlx4_ib_demux_pv_qp *tun_qp;
174037bfc7c1SJack Morgenstein 	struct ib_wc wc;
174137bfc7c1SJack Morgenstein 	int ret;
174237bfc7c1SJack Morgenstein 	ctx = container_of(work, struct mlx4_ib_demux_pv_ctx, work);
174337bfc7c1SJack Morgenstein 	ib_req_notify_cq(ctx->cq, IB_CQ_NEXT_COMP);
174437bfc7c1SJack Morgenstein 
174537bfc7c1SJack Morgenstein 	while (ib_poll_cq(ctx->cq, 1, &wc) == 1) {
174637bfc7c1SJack Morgenstein 		tun_qp = &ctx->qp[MLX4_TUN_WRID_QPN(wc.wr_id)];
174737bfc7c1SJack Morgenstein 		if (wc.status == IB_WC_SUCCESS) {
174837bfc7c1SJack Morgenstein 			switch (wc.opcode) {
174937bfc7c1SJack Morgenstein 			case IB_WC_RECV:
175037bfc7c1SJack Morgenstein 				mlx4_ib_multiplex_mad(ctx, &wc);
175137bfc7c1SJack Morgenstein 				ret = mlx4_ib_post_pv_qp_buf(ctx, tun_qp,
175237bfc7c1SJack Morgenstein 							     wc.wr_id &
175337bfc7c1SJack Morgenstein 							     (MLX4_NUM_TUNNEL_BUFS - 1));
175437bfc7c1SJack Morgenstein 				if (ret)
175537bfc7c1SJack Morgenstein 					pr_err("Failed reposting tunnel "
175637bfc7c1SJack Morgenstein 					       "buf:%lld\n", wc.wr_id);
175737bfc7c1SJack Morgenstein 				break;
175837bfc7c1SJack Morgenstein 			case IB_WC_SEND:
175936523159SDasaratharaman Chandramouli 				rdma_destroy_ah(tun_qp->tx_ring[wc.wr_id &
17602553ba21SGal Pressman 					      (MLX4_NUM_TUNNEL_BUFS - 1)].ah, 0);
176137bfc7c1SJack Morgenstein 				tun_qp->tx_ring[wc.wr_id & (MLX4_NUM_TUNNEL_BUFS - 1)].ah
176237bfc7c1SJack Morgenstein 					= NULL;
176337bfc7c1SJack Morgenstein 				spin_lock(&tun_qp->tx_lock);
176437bfc7c1SJack Morgenstein 				tun_qp->tx_ix_tail++;
176537bfc7c1SJack Morgenstein 				spin_unlock(&tun_qp->tx_lock);
176637bfc7c1SJack Morgenstein 
176737bfc7c1SJack Morgenstein 				break;
176837bfc7c1SJack Morgenstein 			default:
176937bfc7c1SJack Morgenstein 				break;
177037bfc7c1SJack Morgenstein 			}
177137bfc7c1SJack Morgenstein 		} else  {
177237bfc7c1SJack Morgenstein 			pr_debug("mlx4_ib: completion error in tunnel: %d."
177337bfc7c1SJack Morgenstein 				 " status = %d, wrid = 0x%llx\n",
177437bfc7c1SJack Morgenstein 				 ctx->slave, wc.status, wc.wr_id);
177537bfc7c1SJack Morgenstein 			if (!MLX4_TUN_IS_RECV(wc.wr_id)) {
177636523159SDasaratharaman Chandramouli 				rdma_destroy_ah(tun_qp->tx_ring[wc.wr_id &
17772553ba21SGal Pressman 					      (MLX4_NUM_TUNNEL_BUFS - 1)].ah, 0);
177837bfc7c1SJack Morgenstein 				tun_qp->tx_ring[wc.wr_id & (MLX4_NUM_TUNNEL_BUFS - 1)].ah
177937bfc7c1SJack Morgenstein 					= NULL;
178037bfc7c1SJack Morgenstein 				spin_lock(&tun_qp->tx_lock);
178137bfc7c1SJack Morgenstein 				tun_qp->tx_ix_tail++;
178237bfc7c1SJack Morgenstein 				spin_unlock(&tun_qp->tx_lock);
178337bfc7c1SJack Morgenstein 			}
178437bfc7c1SJack Morgenstein 		}
178537bfc7c1SJack Morgenstein 	}
1786fc06573dSJack Morgenstein }
1787fc06573dSJack Morgenstein 
pv_qp_event_handler(struct ib_event * event,void * qp_context)1788fc06573dSJack Morgenstein static void pv_qp_event_handler(struct ib_event *event, void *qp_context)
1789fc06573dSJack Morgenstein {
1790fc06573dSJack Morgenstein 	struct mlx4_ib_demux_pv_ctx *sqp = qp_context;
1791fc06573dSJack Morgenstein 
1792fc06573dSJack Morgenstein 	/* It's worse than that! He's dead, Jim! */
1793fc06573dSJack Morgenstein 	pr_err("Fatal error (%d) on a MAD QP on port %d\n",
1794fc06573dSJack Morgenstein 	       event->event, sqp->port);
1795fc06573dSJack Morgenstein }
1796fc06573dSJack Morgenstein 
create_pv_sqp(struct mlx4_ib_demux_pv_ctx * ctx,enum ib_qp_type qp_type,int create_tun)1797fc06573dSJack Morgenstein static int create_pv_sqp(struct mlx4_ib_demux_pv_ctx *ctx,
1798fc06573dSJack Morgenstein 			    enum ib_qp_type qp_type, int create_tun)
1799fc06573dSJack Morgenstein {
1800fc06573dSJack Morgenstein 	int i, ret;
1801fc06573dSJack Morgenstein 	struct mlx4_ib_demux_pv_qp *tun_qp;
1802fc06573dSJack Morgenstein 	struct mlx4_ib_qp_tunnel_init_attr qp_init_attr;
1803fc06573dSJack Morgenstein 	struct ib_qp_attr attr;
1804fc06573dSJack Morgenstein 	int qp_attr_mask_INIT;
18050ae207fbSHåkon Bugge 	const int nmbr_bufs = create_tun ? MLX4_NUM_TUNNEL_BUFS : MLX4_NUM_WIRE_BUFS;
1806fc06573dSJack Morgenstein 
1807fc06573dSJack Morgenstein 	if (qp_type > IB_QPT_GSI)
1808fc06573dSJack Morgenstein 		return -EINVAL;
1809fc06573dSJack Morgenstein 
1810fc06573dSJack Morgenstein 	tun_qp = &ctx->qp[qp_type];
1811fc06573dSJack Morgenstein 
1812fc06573dSJack Morgenstein 	memset(&qp_init_attr, 0, sizeof qp_init_attr);
1813fc06573dSJack Morgenstein 	qp_init_attr.init_attr.send_cq = ctx->cq;
1814fc06573dSJack Morgenstein 	qp_init_attr.init_attr.recv_cq = ctx->cq;
1815fc06573dSJack Morgenstein 	qp_init_attr.init_attr.sq_sig_type = IB_SIGNAL_ALL_WR;
18160ae207fbSHåkon Bugge 	qp_init_attr.init_attr.cap.max_send_wr = nmbr_bufs;
18170ae207fbSHåkon Bugge 	qp_init_attr.init_attr.cap.max_recv_wr = nmbr_bufs;
1818fc06573dSJack Morgenstein 	qp_init_attr.init_attr.cap.max_send_sge = 1;
1819fc06573dSJack Morgenstein 	qp_init_attr.init_attr.cap.max_recv_sge = 1;
1820fc06573dSJack Morgenstein 	if (create_tun) {
1821fc06573dSJack Morgenstein 		qp_init_attr.init_attr.qp_type = IB_QPT_UD;
1822fc06573dSJack Morgenstein 		qp_init_attr.init_attr.create_flags = MLX4_IB_SRIOV_TUNNEL_QP;
1823fc06573dSJack Morgenstein 		qp_init_attr.port = ctx->port;
1824fc06573dSJack Morgenstein 		qp_init_attr.slave = ctx->slave;
1825fc06573dSJack Morgenstein 		qp_init_attr.proxy_qp_type = qp_type;
1826fc06573dSJack Morgenstein 		qp_attr_mask_INIT = IB_QP_STATE | IB_QP_PKEY_INDEX |
1827fc06573dSJack Morgenstein 			   IB_QP_QKEY | IB_QP_PORT;
1828fc06573dSJack Morgenstein 	} else {
1829fc06573dSJack Morgenstein 		qp_init_attr.init_attr.qp_type = qp_type;
1830fc06573dSJack Morgenstein 		qp_init_attr.init_attr.create_flags = MLX4_IB_SRIOV_SQP;
1831fc06573dSJack Morgenstein 		qp_attr_mask_INIT = IB_QP_STATE | IB_QP_PKEY_INDEX | IB_QP_QKEY;
1832fc06573dSJack Morgenstein 	}
1833fc06573dSJack Morgenstein 	qp_init_attr.init_attr.port_num = ctx->port;
1834fc06573dSJack Morgenstein 	qp_init_attr.init_attr.qp_context = ctx;
1835fc06573dSJack Morgenstein 	qp_init_attr.init_attr.event_handler = pv_qp_event_handler;
1836fc06573dSJack Morgenstein 	tun_qp->qp = ib_create_qp(ctx->pd, &qp_init_attr.init_attr);
1837fc06573dSJack Morgenstein 	if (IS_ERR(tun_qp->qp)) {
1838fc06573dSJack Morgenstein 		ret = PTR_ERR(tun_qp->qp);
1839fc06573dSJack Morgenstein 		tun_qp->qp = NULL;
1840fc06573dSJack Morgenstein 		pr_err("Couldn't create %s QP (%d)\n",
1841fc06573dSJack Morgenstein 		       create_tun ? "tunnel" : "special", ret);
1842fc06573dSJack Morgenstein 		return ret;
1843fc06573dSJack Morgenstein 	}
1844fc06573dSJack Morgenstein 
1845fc06573dSJack Morgenstein 	memset(&attr, 0, sizeof attr);
1846fc06573dSJack Morgenstein 	attr.qp_state = IB_QPS_INIT;
18473eac103fSJack Morgenstein 	ret = 0;
18483eac103fSJack Morgenstein 	if (create_tun)
18493eac103fSJack Morgenstein 		ret = find_slave_port_pkey_ix(to_mdev(ctx->ib_dev), ctx->slave,
18503eac103fSJack Morgenstein 					      ctx->port, IB_DEFAULT_PKEY_FULL,
18513eac103fSJack Morgenstein 					      &attr.pkey_index);
18523eac103fSJack Morgenstein 	if (ret || !create_tun)
1853fc06573dSJack Morgenstein 		attr.pkey_index =
1854fc06573dSJack Morgenstein 			to_mdev(ctx->ib_dev)->pkeys.virt2phys_pkey[ctx->slave][ctx->port - 1][0];
1855fc06573dSJack Morgenstein 	attr.qkey = IB_QP1_QKEY;
1856fc06573dSJack Morgenstein 	attr.port_num = ctx->port;
1857fc06573dSJack Morgenstein 	ret = ib_modify_qp(tun_qp->qp, &attr, qp_attr_mask_INIT);
1858fc06573dSJack Morgenstein 	if (ret) {
1859fc06573dSJack Morgenstein 		pr_err("Couldn't change %s qp state to INIT (%d)\n",
1860fc06573dSJack Morgenstein 		       create_tun ? "tunnel" : "special", ret);
1861fc06573dSJack Morgenstein 		goto err_qp;
1862fc06573dSJack Morgenstein 	}
1863fc06573dSJack Morgenstein 	attr.qp_state = IB_QPS_RTR;
1864fc06573dSJack Morgenstein 	ret = ib_modify_qp(tun_qp->qp, &attr, IB_QP_STATE);
1865fc06573dSJack Morgenstein 	if (ret) {
1866fc06573dSJack Morgenstein 		pr_err("Couldn't change %s qp state to RTR (%d)\n",
1867fc06573dSJack Morgenstein 		       create_tun ? "tunnel" : "special", ret);
1868fc06573dSJack Morgenstein 		goto err_qp;
1869fc06573dSJack Morgenstein 	}
1870fc06573dSJack Morgenstein 	attr.qp_state = IB_QPS_RTS;
1871fc06573dSJack Morgenstein 	attr.sq_psn = 0;
1872fc06573dSJack Morgenstein 	ret = ib_modify_qp(tun_qp->qp, &attr, IB_QP_STATE | IB_QP_SQ_PSN);
1873fc06573dSJack Morgenstein 	if (ret) {
1874fc06573dSJack Morgenstein 		pr_err("Couldn't change %s qp state to RTS (%d)\n",
1875fc06573dSJack Morgenstein 		       create_tun ? "tunnel" : "special", ret);
1876fc06573dSJack Morgenstein 		goto err_qp;
1877fc06573dSJack Morgenstein 	}
1878fc06573dSJack Morgenstein 
18790ae207fbSHåkon Bugge 	for (i = 0; i < nmbr_bufs; i++) {
1880fc06573dSJack Morgenstein 		ret = mlx4_ib_post_pv_qp_buf(ctx, tun_qp, i);
1881fc06573dSJack Morgenstein 		if (ret) {
1882fc06573dSJack Morgenstein 			pr_err(" mlx4_ib_post_pv_buf error"
1883fc06573dSJack Morgenstein 			       " (err = %d, i = %d)\n", ret, i);
1884fc06573dSJack Morgenstein 			goto err_qp;
1885fc06573dSJack Morgenstein 		}
1886fc06573dSJack Morgenstein 	}
1887fc06573dSJack Morgenstein 	return 0;
1888fc06573dSJack Morgenstein 
1889fc06573dSJack Morgenstein err_qp:
1890fc06573dSJack Morgenstein 	ib_destroy_qp(tun_qp->qp);
1891fc06573dSJack Morgenstein 	tun_qp->qp = NULL;
1892fc06573dSJack Morgenstein 	return ret;
1893fc06573dSJack Morgenstein }
1894fc06573dSJack Morgenstein 
1895fc06573dSJack Morgenstein /*
1896fc06573dSJack Morgenstein  * IB MAD completion callback for real SQPs
1897fc06573dSJack Morgenstein  */
mlx4_ib_sqp_comp_worker(struct work_struct * work)1898fc06573dSJack Morgenstein static void mlx4_ib_sqp_comp_worker(struct work_struct *work)
1899fc06573dSJack Morgenstein {
190037bfc7c1SJack Morgenstein 	struct mlx4_ib_demux_pv_ctx *ctx;
190137bfc7c1SJack Morgenstein 	struct mlx4_ib_demux_pv_qp *sqp;
190237bfc7c1SJack Morgenstein 	struct ib_wc wc;
190337bfc7c1SJack Morgenstein 	struct ib_grh *grh;
190437bfc7c1SJack Morgenstein 	struct ib_mad *mad;
190537bfc7c1SJack Morgenstein 
190637bfc7c1SJack Morgenstein 	ctx = container_of(work, struct mlx4_ib_demux_pv_ctx, work);
190737bfc7c1SJack Morgenstein 	ib_req_notify_cq(ctx->cq, IB_CQ_NEXT_COMP);
190837bfc7c1SJack Morgenstein 
190937bfc7c1SJack Morgenstein 	while (mlx4_ib_poll_cq(ctx->cq, 1, &wc) == 1) {
191037bfc7c1SJack Morgenstein 		sqp = &ctx->qp[MLX4_TUN_WRID_QPN(wc.wr_id)];
191137bfc7c1SJack Morgenstein 		if (wc.status == IB_WC_SUCCESS) {
191237bfc7c1SJack Morgenstein 			switch (wc.opcode) {
191337bfc7c1SJack Morgenstein 			case IB_WC_SEND:
1914d3456914SLeon Romanovsky 				kfree(sqp->tx_ring[wc.wr_id &
19150ae207fbSHåkon Bugge 				      (MLX4_NUM_WIRE_BUFS - 1)].ah);
19160ae207fbSHåkon Bugge 				sqp->tx_ring[wc.wr_id & (MLX4_NUM_WIRE_BUFS - 1)].ah
191737bfc7c1SJack Morgenstein 					= NULL;
191837bfc7c1SJack Morgenstein 				spin_lock(&sqp->tx_lock);
191937bfc7c1SJack Morgenstein 				sqp->tx_ix_tail++;
192037bfc7c1SJack Morgenstein 				spin_unlock(&sqp->tx_lock);
192137bfc7c1SJack Morgenstein 				break;
192237bfc7c1SJack Morgenstein 			case IB_WC_RECV:
192337bfc7c1SJack Morgenstein 				mad = (struct ib_mad *) &(((struct mlx4_mad_rcv_buf *)
192437bfc7c1SJack Morgenstein 						(sqp->ring[wc.wr_id &
19250ae207fbSHåkon Bugge 						(MLX4_NUM_WIRE_BUFS - 1)].addr))->payload);
192637bfc7c1SJack Morgenstein 				grh = &(((struct mlx4_mad_rcv_buf *)
192737bfc7c1SJack Morgenstein 						(sqp->ring[wc.wr_id &
19280ae207fbSHåkon Bugge 						(MLX4_NUM_WIRE_BUFS - 1)].addr))->grh);
192937bfc7c1SJack Morgenstein 				mlx4_ib_demux_mad(ctx->ib_dev, ctx->port, &wc, grh, mad);
193037bfc7c1SJack Morgenstein 				if (mlx4_ib_post_pv_qp_buf(ctx, sqp, wc.wr_id &
19310ae207fbSHåkon Bugge 							   (MLX4_NUM_WIRE_BUFS - 1)))
193237bfc7c1SJack Morgenstein 					pr_err("Failed reposting SQP "
193337bfc7c1SJack Morgenstein 					       "buf:%lld\n", wc.wr_id);
193437bfc7c1SJack Morgenstein 				break;
193537bfc7c1SJack Morgenstein 			default:
193637bfc7c1SJack Morgenstein 				break;
193737bfc7c1SJack Morgenstein 			}
193837bfc7c1SJack Morgenstein 		} else  {
193937bfc7c1SJack Morgenstein 			pr_debug("mlx4_ib: completion error in tunnel: %d."
194037bfc7c1SJack Morgenstein 				 " status = %d, wrid = 0x%llx\n",
194137bfc7c1SJack Morgenstein 				 ctx->slave, wc.status, wc.wr_id);
194237bfc7c1SJack Morgenstein 			if (!MLX4_TUN_IS_RECV(wc.wr_id)) {
1943d3456914SLeon Romanovsky 				kfree(sqp->tx_ring[wc.wr_id &
19440ae207fbSHåkon Bugge 				      (MLX4_NUM_WIRE_BUFS - 1)].ah);
19450ae207fbSHåkon Bugge 				sqp->tx_ring[wc.wr_id & (MLX4_NUM_WIRE_BUFS - 1)].ah
194637bfc7c1SJack Morgenstein 					= NULL;
194737bfc7c1SJack Morgenstein 				spin_lock(&sqp->tx_lock);
194837bfc7c1SJack Morgenstein 				sqp->tx_ix_tail++;
194937bfc7c1SJack Morgenstein 				spin_unlock(&sqp->tx_lock);
195037bfc7c1SJack Morgenstein 			}
195137bfc7c1SJack Morgenstein 		}
195237bfc7c1SJack Morgenstein 	}
1953fc06573dSJack Morgenstein }
1954fc06573dSJack Morgenstein 
alloc_pv_object(struct mlx4_ib_dev * dev,int slave,int port,struct mlx4_ib_demux_pv_ctx ** ret_ctx)1955fc06573dSJack Morgenstein static int alloc_pv_object(struct mlx4_ib_dev *dev, int slave, int port,
1956fc06573dSJack Morgenstein 			       struct mlx4_ib_demux_pv_ctx **ret_ctx)
1957fc06573dSJack Morgenstein {
1958fc06573dSJack Morgenstein 	struct mlx4_ib_demux_pv_ctx *ctx;
1959fc06573dSJack Morgenstein 
1960fc06573dSJack Morgenstein 	*ret_ctx = NULL;
1961fc06573dSJack Morgenstein 	ctx = kzalloc(sizeof (struct mlx4_ib_demux_pv_ctx), GFP_KERNEL);
196215d4626eSLeon Romanovsky 	if (!ctx)
1963fc06573dSJack Morgenstein 		return -ENOMEM;
1964fc06573dSJack Morgenstein 
1965fc06573dSJack Morgenstein 	ctx->ib_dev = &dev->ib_dev;
1966fc06573dSJack Morgenstein 	ctx->port = port;
1967fc06573dSJack Morgenstein 	ctx->slave = slave;
1968fc06573dSJack Morgenstein 	*ret_ctx = ctx;
1969fc06573dSJack Morgenstein 	return 0;
1970fc06573dSJack Morgenstein }
1971fc06573dSJack Morgenstein 
free_pv_object(struct mlx4_ib_dev * dev,int slave,int port)1972fc06573dSJack Morgenstein static void free_pv_object(struct mlx4_ib_dev *dev, int slave, int port)
1973fc06573dSJack Morgenstein {
1974fc06573dSJack Morgenstein 	if (dev->sriov.demux[port - 1].tun[slave]) {
1975fc06573dSJack Morgenstein 		kfree(dev->sriov.demux[port - 1].tun[slave]);
1976fc06573dSJack Morgenstein 		dev->sriov.demux[port - 1].tun[slave] = NULL;
1977fc06573dSJack Morgenstein 	}
1978fc06573dSJack Morgenstein }
1979fc06573dSJack Morgenstein 
create_pv_resources(struct ib_device * ibdev,int slave,int port,int create_tun,struct mlx4_ib_demux_pv_ctx * ctx)1980fc06573dSJack Morgenstein static int create_pv_resources(struct ib_device *ibdev, int slave, int port,
1981fc06573dSJack Morgenstein 			       int create_tun, struct mlx4_ib_demux_pv_ctx *ctx)
1982fc06573dSJack Morgenstein {
1983fc06573dSJack Morgenstein 	int ret, cq_size;
19848e37210bSMatan Barak 	struct ib_cq_init_attr cq_attr = {};
19850ae207fbSHåkon Bugge 	const int nmbr_bufs = create_tun ? MLX4_NUM_TUNNEL_BUFS : MLX4_NUM_WIRE_BUFS;
1986fc06573dSJack Morgenstein 
19873806d08cSJack Morgenstein 	if (ctx->state != DEMUX_PV_STATE_DOWN)
19883806d08cSJack Morgenstein 		return -EEXIST;
19893806d08cSJack Morgenstein 
1990fc06573dSJack Morgenstein 	ctx->state = DEMUX_PV_STATE_STARTING;
199197982f5aSJack Morgenstein 	/* have QP0 only if link layer is IB */
199297982f5aSJack Morgenstein 	if (rdma_port_get_link_layer(ibdev, ctx->port) ==
199397982f5aSJack Morgenstein 	    IB_LINK_LAYER_INFINIBAND)
1994fc06573dSJack Morgenstein 		ctx->has_smi = 1;
1995fc06573dSJack Morgenstein 
1996fc06573dSJack Morgenstein 	if (ctx->has_smi) {
1997fc06573dSJack Morgenstein 		ret = mlx4_ib_alloc_pv_bufs(ctx, IB_QPT_SMI, create_tun);
1998fc06573dSJack Morgenstein 		if (ret) {
1999fc06573dSJack Morgenstein 			pr_err("Failed allocating qp0 tunnel bufs (%d)\n", ret);
2000fc06573dSJack Morgenstein 			goto err_out;
2001fc06573dSJack Morgenstein 		}
2002fc06573dSJack Morgenstein 	}
2003fc06573dSJack Morgenstein 
2004fc06573dSJack Morgenstein 	ret = mlx4_ib_alloc_pv_bufs(ctx, IB_QPT_GSI, create_tun);
2005fc06573dSJack Morgenstein 	if (ret) {
2006fc06573dSJack Morgenstein 		pr_err("Failed allocating qp1 tunnel bufs (%d)\n", ret);
2007fc06573dSJack Morgenstein 		goto err_out_qp0;
2008fc06573dSJack Morgenstein 	}
2009fc06573dSJack Morgenstein 
20100ae207fbSHåkon Bugge 	cq_size = 2 * nmbr_bufs;
2011fc06573dSJack Morgenstein 	if (ctx->has_smi)
2012fc06573dSJack Morgenstein 		cq_size *= 2;
2013fc06573dSJack Morgenstein 
20148e37210bSMatan Barak 	cq_attr.cqe = cq_size;
20157fd1507dSHåkon Bugge 	ctx->cq = ib_create_cq(ctx->ib_dev,
20167fd1507dSHåkon Bugge 			       create_tun ? mlx4_ib_tunnel_comp_handler : mlx4_ib_wire_comp_handler,
20178e37210bSMatan Barak 			       NULL, ctx, &cq_attr);
2018fc06573dSJack Morgenstein 	if (IS_ERR(ctx->cq)) {
2019fc06573dSJack Morgenstein 		ret = PTR_ERR(ctx->cq);
2020fc06573dSJack Morgenstein 		pr_err("Couldn't create tunnel CQ (%d)\n", ret);
2021fc06573dSJack Morgenstein 		goto err_buf;
2022fc06573dSJack Morgenstein 	}
2023fc06573dSJack Morgenstein 
2024ed082d36SChristoph Hellwig 	ctx->pd = ib_alloc_pd(ctx->ib_dev, 0);
2025fc06573dSJack Morgenstein 	if (IS_ERR(ctx->pd)) {
2026fc06573dSJack Morgenstein 		ret = PTR_ERR(ctx->pd);
2027fc06573dSJack Morgenstein 		pr_err("Couldn't create tunnel PD (%d)\n", ret);
2028fc06573dSJack Morgenstein 		goto err_cq;
2029fc06573dSJack Morgenstein 	}
2030fc06573dSJack Morgenstein 
2031fc06573dSJack Morgenstein 	if (ctx->has_smi) {
2032fc06573dSJack Morgenstein 		ret = create_pv_sqp(ctx, IB_QPT_SMI, create_tun);
2033fc06573dSJack Morgenstein 		if (ret) {
2034fc06573dSJack Morgenstein 			pr_err("Couldn't create %s QP0 (%d)\n",
2035fc06573dSJack Morgenstein 			       create_tun ? "tunnel for" : "",  ret);
20367dd97576SJason Gunthorpe 			goto err_pd;
2037fc06573dSJack Morgenstein 		}
2038fc06573dSJack Morgenstein 	}
2039fc06573dSJack Morgenstein 
2040fc06573dSJack Morgenstein 	ret = create_pv_sqp(ctx, IB_QPT_GSI, create_tun);
2041fc06573dSJack Morgenstein 	if (ret) {
2042fc06573dSJack Morgenstein 		pr_err("Couldn't create %s QP1 (%d)\n",
2043fc06573dSJack Morgenstein 		       create_tun ? "tunnel for" : "",  ret);
2044fc06573dSJack Morgenstein 		goto err_qp0;
2045fc06573dSJack Morgenstein 	}
2046fc06573dSJack Morgenstein 
2047fc06573dSJack Morgenstein 	if (create_tun)
2048fc06573dSJack Morgenstein 		INIT_WORK(&ctx->work, mlx4_ib_tunnel_comp_worker);
2049fc06573dSJack Morgenstein 	else
2050fc06573dSJack Morgenstein 		INIT_WORK(&ctx->work, mlx4_ib_sqp_comp_worker);
2051fc06573dSJack Morgenstein 
2052fc06573dSJack Morgenstein 	ctx->wq = to_mdev(ibdev)->sriov.demux[port - 1].wq;
20537fd1507dSHåkon Bugge 	ctx->wi_wq = to_mdev(ibdev)->sriov.demux[port - 1].wi_wq;
2054fc06573dSJack Morgenstein 
2055fc06573dSJack Morgenstein 	ret = ib_req_notify_cq(ctx->cq, IB_CQ_NEXT_COMP);
2056fc06573dSJack Morgenstein 	if (ret) {
2057fc06573dSJack Morgenstein 		pr_err("Couldn't arm tunnel cq (%d)\n", ret);
2058fc06573dSJack Morgenstein 		goto err_wq;
2059fc06573dSJack Morgenstein 	}
2060fc06573dSJack Morgenstein 	ctx->state = DEMUX_PV_STATE_ACTIVE;
2061fc06573dSJack Morgenstein 	return 0;
2062fc06573dSJack Morgenstein 
2063fc06573dSJack Morgenstein err_wq:
2064fc06573dSJack Morgenstein 	ctx->wq = NULL;
2065fc06573dSJack Morgenstein 	ib_destroy_qp(ctx->qp[1].qp);
2066fc06573dSJack Morgenstein 	ctx->qp[1].qp = NULL;
2067fc06573dSJack Morgenstein 
2068fc06573dSJack Morgenstein 
2069fc06573dSJack Morgenstein err_qp0:
2070fc06573dSJack Morgenstein 	if (ctx->has_smi)
2071fc06573dSJack Morgenstein 		ib_destroy_qp(ctx->qp[0].qp);
2072fc06573dSJack Morgenstein 	ctx->qp[0].qp = NULL;
2073fc06573dSJack Morgenstein 
2074fc06573dSJack Morgenstein err_pd:
2075fc06573dSJack Morgenstein 	ib_dealloc_pd(ctx->pd);
2076fc06573dSJack Morgenstein 	ctx->pd = NULL;
2077fc06573dSJack Morgenstein 
2078fc06573dSJack Morgenstein err_cq:
2079fc06573dSJack Morgenstein 	ib_destroy_cq(ctx->cq);
2080fc06573dSJack Morgenstein 	ctx->cq = NULL;
2081fc06573dSJack Morgenstein 
2082fc06573dSJack Morgenstein err_buf:
2083fc06573dSJack Morgenstein 	mlx4_ib_free_pv_qp_bufs(ctx, IB_QPT_GSI, create_tun);
2084fc06573dSJack Morgenstein 
2085fc06573dSJack Morgenstein err_out_qp0:
2086fc06573dSJack Morgenstein 	if (ctx->has_smi)
2087fc06573dSJack Morgenstein 		mlx4_ib_free_pv_qp_bufs(ctx, IB_QPT_SMI, create_tun);
2088fc06573dSJack Morgenstein err_out:
2089fc06573dSJack Morgenstein 	ctx->state = DEMUX_PV_STATE_DOWN;
2090fc06573dSJack Morgenstein 	return ret;
2091fc06573dSJack Morgenstein }
2092fc06573dSJack Morgenstein 
destroy_pv_resources(struct mlx4_ib_dev * dev,int slave,int port,struct mlx4_ib_demux_pv_ctx * ctx,int flush)2093fc06573dSJack Morgenstein static void destroy_pv_resources(struct mlx4_ib_dev *dev, int slave, int port,
2094fc06573dSJack Morgenstein 				 struct mlx4_ib_demux_pv_ctx *ctx, int flush)
2095fc06573dSJack Morgenstein {
2096fc06573dSJack Morgenstein 	if (!ctx)
2097fc06573dSJack Morgenstein 		return;
2098fc06573dSJack Morgenstein 	if (ctx->state > DEMUX_PV_STATE_DOWN) {
2099fc06573dSJack Morgenstein 		ctx->state = DEMUX_PV_STATE_DOWNING;
2100fc06573dSJack Morgenstein 		if (flush)
2101fc06573dSJack Morgenstein 			flush_workqueue(ctx->wq);
2102fc06573dSJack Morgenstein 		if (ctx->has_smi) {
2103fc06573dSJack Morgenstein 			ib_destroy_qp(ctx->qp[0].qp);
2104fc06573dSJack Morgenstein 			ctx->qp[0].qp = NULL;
2105fc06573dSJack Morgenstein 			mlx4_ib_free_pv_qp_bufs(ctx, IB_QPT_SMI, 1);
2106fc06573dSJack Morgenstein 		}
2107fc06573dSJack Morgenstein 		ib_destroy_qp(ctx->qp[1].qp);
2108fc06573dSJack Morgenstein 		ctx->qp[1].qp = NULL;
2109fc06573dSJack Morgenstein 		mlx4_ib_free_pv_qp_bufs(ctx, IB_QPT_GSI, 1);
2110fc06573dSJack Morgenstein 		ib_dealloc_pd(ctx->pd);
2111fc06573dSJack Morgenstein 		ctx->pd = NULL;
2112fc06573dSJack Morgenstein 		ib_destroy_cq(ctx->cq);
2113fc06573dSJack Morgenstein 		ctx->cq = NULL;
2114fc06573dSJack Morgenstein 		ctx->state = DEMUX_PV_STATE_DOWN;
2115fc06573dSJack Morgenstein 	}
2116fc06573dSJack Morgenstein }
2117fc06573dSJack Morgenstein 
mlx4_ib_tunnels_update(struct mlx4_ib_dev * dev,int slave,int port,int do_init)2118fc06573dSJack Morgenstein static int mlx4_ib_tunnels_update(struct mlx4_ib_dev *dev, int slave,
2119fc06573dSJack Morgenstein 				  int port, int do_init)
2120fc06573dSJack Morgenstein {
2121fc06573dSJack Morgenstein 	int ret = 0;
2122fc06573dSJack Morgenstein 
2123fc06573dSJack Morgenstein 	if (!do_init) {
2124b9c5d6a6SOren Duer 		clean_vf_mcast(&dev->sriov.demux[port - 1], slave);
2125fc06573dSJack Morgenstein 		/* for master, destroy real sqp resources */
2126fc06573dSJack Morgenstein 		if (slave == mlx4_master_func_num(dev->dev))
2127fc06573dSJack Morgenstein 			destroy_pv_resources(dev, slave, port,
2128fc06573dSJack Morgenstein 					     dev->sriov.sqps[port - 1], 1);
2129fc06573dSJack Morgenstein 		/* destroy the tunnel qp resources */
2130fc06573dSJack Morgenstein 		destroy_pv_resources(dev, slave, port,
2131fc06573dSJack Morgenstein 				     dev->sriov.demux[port - 1].tun[slave], 1);
2132fc06573dSJack Morgenstein 		return 0;
2133fc06573dSJack Morgenstein 	}
2134fc06573dSJack Morgenstein 
2135fc06573dSJack Morgenstein 	/* create the tunnel qp resources */
2136fc06573dSJack Morgenstein 	ret = create_pv_resources(&dev->ib_dev, slave, port, 1,
2137fc06573dSJack Morgenstein 				  dev->sriov.demux[port - 1].tun[slave]);
2138fc06573dSJack Morgenstein 
2139fc06573dSJack Morgenstein 	/* for master, create the real sqp resources */
2140fc06573dSJack Morgenstein 	if (!ret && slave == mlx4_master_func_num(dev->dev))
2141fc06573dSJack Morgenstein 		ret = create_pv_resources(&dev->ib_dev, slave, port, 0,
2142fc06573dSJack Morgenstein 					  dev->sriov.sqps[port - 1]);
2143fc06573dSJack Morgenstein 	return ret;
2144fc06573dSJack Morgenstein }
2145fc06573dSJack Morgenstein 
mlx4_ib_tunnels_update_work(struct work_struct * work)2146fc06573dSJack Morgenstein void mlx4_ib_tunnels_update_work(struct work_struct *work)
2147fc06573dSJack Morgenstein {
2148fc06573dSJack Morgenstein 	struct mlx4_ib_demux_work *dmxw;
2149fc06573dSJack Morgenstein 
2150fc06573dSJack Morgenstein 	dmxw = container_of(work, struct mlx4_ib_demux_work, work);
2151fc06573dSJack Morgenstein 	mlx4_ib_tunnels_update(dmxw->dev, dmxw->slave, (int) dmxw->port,
2152fc06573dSJack Morgenstein 			       dmxw->do_init);
2153fc06573dSJack Morgenstein 	kfree(dmxw);
2154fc06573dSJack Morgenstein 	return;
2155fc06573dSJack Morgenstein }
2156fc06573dSJack Morgenstein 
mlx4_ib_alloc_demux_ctx(struct mlx4_ib_dev * dev,struct mlx4_ib_demux_ctx * ctx,int port)2157fc06573dSJack Morgenstein static int mlx4_ib_alloc_demux_ctx(struct mlx4_ib_dev *dev,
2158fc06573dSJack Morgenstein 				       struct mlx4_ib_demux_ctx *ctx,
2159fc06573dSJack Morgenstein 				       int port)
2160fc06573dSJack Morgenstein {
2161*f0583d6dSLeon Romanovsky 	char name[21];
2162fc06573dSJack Morgenstein 	int ret = 0;
2163fc06573dSJack Morgenstein 	int i;
2164fc06573dSJack Morgenstein 
2165fc06573dSJack Morgenstein 	ctx->tun = kcalloc(dev->dev->caps.sqp_demux,
2166fc06573dSJack Morgenstein 			   sizeof (struct mlx4_ib_demux_pv_ctx *), GFP_KERNEL);
2167fc06573dSJack Morgenstein 	if (!ctx->tun)
2168fc06573dSJack Morgenstein 		return -ENOMEM;
2169fc06573dSJack Morgenstein 
2170fc06573dSJack Morgenstein 	ctx->dev = dev;
2171fc06573dSJack Morgenstein 	ctx->port = port;
2172fc06573dSJack Morgenstein 	ctx->ib_dev = &dev->ib_dev;
2173fc06573dSJack Morgenstein 
2174449fc488SMatan Barak 	for (i = 0;
2175872bf2fbSYishai Hadas 	     i < min(dev->dev->caps.sqp_demux,
2176872bf2fbSYishai Hadas 	     (u16)(dev->dev->persist->num_vfs + 1));
2177449fc488SMatan Barak 	     i++) {
2178449fc488SMatan Barak 		struct mlx4_active_ports actv_ports =
2179449fc488SMatan Barak 			mlx4_get_active_ports(dev->dev, i);
2180449fc488SMatan Barak 
2181449fc488SMatan Barak 		if (!test_bit(port - 1, actv_ports.ports))
2182449fc488SMatan Barak 			continue;
2183449fc488SMatan Barak 
2184fc06573dSJack Morgenstein 		ret = alloc_pv_object(dev, i, port, &ctx->tun[i]);
2185fc06573dSJack Morgenstein 		if (ret) {
2186fc06573dSJack Morgenstein 			ret = -ENOMEM;
2187b9c5d6a6SOren Duer 			goto err_mcg;
2188fc06573dSJack Morgenstein 		}
2189fc06573dSJack Morgenstein 	}
2190fc06573dSJack Morgenstein 
2191b9c5d6a6SOren Duer 	ret = mlx4_ib_mcg_port_init(ctx);
2192b9c5d6a6SOren Duer 	if (ret) {
2193b9c5d6a6SOren Duer 		pr_err("Failed initializing mcg para-virt (%d)\n", ret);
2194b9c5d6a6SOren Duer 		goto err_mcg;
2195b9c5d6a6SOren Duer 	}
2196b9c5d6a6SOren Duer 
21977fd1507dSHåkon Bugge 	snprintf(name, sizeof(name), "mlx4_ibt%d", port);
219890b14b32SBhaktipriya Shridhar 	ctx->wq = alloc_ordered_workqueue(name, WQ_MEM_RECLAIM);
2199fc06573dSJack Morgenstein 	if (!ctx->wq) {
2200fc06573dSJack Morgenstein 		pr_err("Failed to create tunnelling WQ for port %d\n", port);
2201fc06573dSJack Morgenstein 		ret = -ENOMEM;
2202fc06573dSJack Morgenstein 		goto err_wq;
2203fc06573dSJack Morgenstein 	}
2204fc06573dSJack Morgenstein 
22057fd1507dSHåkon Bugge 	snprintf(name, sizeof(name), "mlx4_ibwi%d", port);
22067fd1507dSHåkon Bugge 	ctx->wi_wq = alloc_ordered_workqueue(name, WQ_MEM_RECLAIM);
22077fd1507dSHåkon Bugge 	if (!ctx->wi_wq) {
22087fd1507dSHåkon Bugge 		pr_err("Failed to create wire WQ for port %d\n", port);
22097fd1507dSHåkon Bugge 		ret = -ENOMEM;
22107fd1507dSHåkon Bugge 		goto err_wiwq;
22117fd1507dSHåkon Bugge 	}
22127fd1507dSHåkon Bugge 
22137fd1507dSHåkon Bugge 	snprintf(name, sizeof(name), "mlx4_ibud%d", port);
221490b14b32SBhaktipriya Shridhar 	ctx->ud_wq = alloc_ordered_workqueue(name, WQ_MEM_RECLAIM);
2215fc06573dSJack Morgenstein 	if (!ctx->ud_wq) {
2216fc06573dSJack Morgenstein 		pr_err("Failed to create up/down WQ for port %d\n", port);
2217fc06573dSJack Morgenstein 		ret = -ENOMEM;
2218fc06573dSJack Morgenstein 		goto err_udwq;
2219fc06573dSJack Morgenstein 	}
2220fc06573dSJack Morgenstein 
2221fc06573dSJack Morgenstein 	return 0;
2222fc06573dSJack Morgenstein 
2223fc06573dSJack Morgenstein err_udwq:
22247fd1507dSHåkon Bugge 	destroy_workqueue(ctx->wi_wq);
22257fd1507dSHåkon Bugge 	ctx->wi_wq = NULL;
22267fd1507dSHåkon Bugge 
22277fd1507dSHåkon Bugge err_wiwq:
2228fc06573dSJack Morgenstein 	destroy_workqueue(ctx->wq);
2229fc06573dSJack Morgenstein 	ctx->wq = NULL;
2230fc06573dSJack Morgenstein 
2231fc06573dSJack Morgenstein err_wq:
2232b9c5d6a6SOren Duer 	mlx4_ib_mcg_port_cleanup(ctx, 1);
2233b9c5d6a6SOren Duer err_mcg:
2234fc06573dSJack Morgenstein 	for (i = 0; i < dev->dev->caps.sqp_demux; i++)
2235fc06573dSJack Morgenstein 		free_pv_object(dev, i, port);
2236fc06573dSJack Morgenstein 	kfree(ctx->tun);
2237fc06573dSJack Morgenstein 	ctx->tun = NULL;
2238fc06573dSJack Morgenstein 	return ret;
2239fc06573dSJack Morgenstein }
2240fc06573dSJack Morgenstein 
mlx4_ib_free_sqp_ctx(struct mlx4_ib_demux_pv_ctx * sqp_ctx)2241fc06573dSJack Morgenstein static void mlx4_ib_free_sqp_ctx(struct mlx4_ib_demux_pv_ctx *sqp_ctx)
2242fc06573dSJack Morgenstein {
2243fc06573dSJack Morgenstein 	if (sqp_ctx->state > DEMUX_PV_STATE_DOWN) {
2244fc06573dSJack Morgenstein 		sqp_ctx->state = DEMUX_PV_STATE_DOWNING;
2245fc06573dSJack Morgenstein 		flush_workqueue(sqp_ctx->wq);
2246fc06573dSJack Morgenstein 		if (sqp_ctx->has_smi) {
2247fc06573dSJack Morgenstein 			ib_destroy_qp(sqp_ctx->qp[0].qp);
2248fc06573dSJack Morgenstein 			sqp_ctx->qp[0].qp = NULL;
2249fc06573dSJack Morgenstein 			mlx4_ib_free_pv_qp_bufs(sqp_ctx, IB_QPT_SMI, 0);
2250fc06573dSJack Morgenstein 		}
2251fc06573dSJack Morgenstein 		ib_destroy_qp(sqp_ctx->qp[1].qp);
2252fc06573dSJack Morgenstein 		sqp_ctx->qp[1].qp = NULL;
2253fc06573dSJack Morgenstein 		mlx4_ib_free_pv_qp_bufs(sqp_ctx, IB_QPT_GSI, 0);
2254fc06573dSJack Morgenstein 		ib_dealloc_pd(sqp_ctx->pd);
2255fc06573dSJack Morgenstein 		sqp_ctx->pd = NULL;
2256fc06573dSJack Morgenstein 		ib_destroy_cq(sqp_ctx->cq);
2257fc06573dSJack Morgenstein 		sqp_ctx->cq = NULL;
2258fc06573dSJack Morgenstein 		sqp_ctx->state = DEMUX_PV_STATE_DOWN;
2259fc06573dSJack Morgenstein 	}
2260fc06573dSJack Morgenstein }
2261fc06573dSJack Morgenstein 
mlx4_ib_free_demux_ctx(struct mlx4_ib_demux_ctx * ctx)2262fc06573dSJack Morgenstein static void mlx4_ib_free_demux_ctx(struct mlx4_ib_demux_ctx *ctx)
2263fc06573dSJack Morgenstein {
2264fc06573dSJack Morgenstein 	int i;
2265fc06573dSJack Morgenstein 	if (ctx) {
2266fc06573dSJack Morgenstein 		struct mlx4_ib_dev *dev = to_mdev(ctx->ib_dev);
2267b9c5d6a6SOren Duer 		mlx4_ib_mcg_port_cleanup(ctx, 1);
2268fc06573dSJack Morgenstein 		for (i = 0; i < dev->dev->caps.sqp_demux; i++) {
2269fc06573dSJack Morgenstein 			if (!ctx->tun[i])
2270fc06573dSJack Morgenstein 				continue;
2271fc06573dSJack Morgenstein 			if (ctx->tun[i]->state > DEMUX_PV_STATE_DOWN)
2272fc06573dSJack Morgenstein 				ctx->tun[i]->state = DEMUX_PV_STATE_DOWNING;
2273fc06573dSJack Morgenstein 		}
2274fc06573dSJack Morgenstein 		flush_workqueue(ctx->wq);
22757fd1507dSHåkon Bugge 		flush_workqueue(ctx->wi_wq);
2276fc06573dSJack Morgenstein 		for (i = 0; i < dev->dev->caps.sqp_demux; i++) {
2277fc06573dSJack Morgenstein 			destroy_pv_resources(dev, i, ctx->port, ctx->tun[i], 0);
2278fc06573dSJack Morgenstein 			free_pv_object(dev, i, ctx->port);
2279fc06573dSJack Morgenstein 		}
2280fc06573dSJack Morgenstein 		kfree(ctx->tun);
2281fc06573dSJack Morgenstein 		destroy_workqueue(ctx->ud_wq);
22827fd1507dSHåkon Bugge 		destroy_workqueue(ctx->wi_wq);
2283fc06573dSJack Morgenstein 		destroy_workqueue(ctx->wq);
2284fc06573dSJack Morgenstein 	}
2285fc06573dSJack Morgenstein }
2286fc06573dSJack Morgenstein 
mlx4_ib_master_tunnels(struct mlx4_ib_dev * dev,int do_init)2287fc06573dSJack Morgenstein static void mlx4_ib_master_tunnels(struct mlx4_ib_dev *dev, int do_init)
2288fc06573dSJack Morgenstein {
2289fc06573dSJack Morgenstein 	int i;
2290fc06573dSJack Morgenstein 
2291fc06573dSJack Morgenstein 	if (!mlx4_is_master(dev->dev))
2292fc06573dSJack Morgenstein 		return;
2293fc06573dSJack Morgenstein 	/* initialize or tear down tunnel QPs for the master */
2294fc06573dSJack Morgenstein 	for (i = 0; i < dev->dev->caps.num_ports; i++)
2295fc06573dSJack Morgenstein 		mlx4_ib_tunnels_update(dev, mlx4_master_func_num(dev->dev), i + 1, do_init);
2296fc06573dSJack Morgenstein 	return;
2297fc06573dSJack Morgenstein }
2298fc06573dSJack Morgenstein 
mlx4_ib_init_sriov(struct mlx4_ib_dev * dev)2299fc06573dSJack Morgenstein int mlx4_ib_init_sriov(struct mlx4_ib_dev *dev)
2300fc06573dSJack Morgenstein {
2301fc06573dSJack Morgenstein 	int i = 0;
2302fc06573dSJack Morgenstein 	int err;
2303fc06573dSJack Morgenstein 
2304fc06573dSJack Morgenstein 	if (!mlx4_is_mfunc(dev->dev))
2305fc06573dSJack Morgenstein 		return 0;
2306fc06573dSJack Morgenstein 
2307fc06573dSJack Morgenstein 	dev->sriov.is_going_down = 0;
2308fc06573dSJack Morgenstein 	spin_lock_init(&dev->sriov.going_down_lock);
23093cf69cc8SAmir Vadai 	mlx4_ib_cm_paravirt_init(dev);
2310fc06573dSJack Morgenstein 
2311fc06573dSJack Morgenstein 	mlx4_ib_warn(&dev->ib_dev, "multi-function enabled\n");
2312fc06573dSJack Morgenstein 
2313fc06573dSJack Morgenstein 	if (mlx4_is_slave(dev->dev)) {
2314fc06573dSJack Morgenstein 		mlx4_ib_warn(&dev->ib_dev, "operating in qp1 tunnel mode\n");
2315fc06573dSJack Morgenstein 		return 0;
2316fc06573dSJack Morgenstein 	}
2317fc06573dSJack Morgenstein 
2318afa8fd1dSJack Morgenstein 	for (i = 0; i < dev->dev->caps.sqp_demux; i++) {
2319afa8fd1dSJack Morgenstein 		if (i == mlx4_master_func_num(dev->dev))
2320afa8fd1dSJack Morgenstein 			mlx4_put_slave_node_guid(dev->dev, i, dev->ib_dev.node_guid);
2321afa8fd1dSJack Morgenstein 		else
2322afa8fd1dSJack Morgenstein 			mlx4_put_slave_node_guid(dev->dev, i, mlx4_ib_gen_node_guid());
2323afa8fd1dSJack Morgenstein 	}
2324afa8fd1dSJack Morgenstein 
2325a0c64a17SJack Morgenstein 	err = mlx4_ib_init_alias_guid_service(dev);
2326a0c64a17SJack Morgenstein 	if (err) {
2327a0c64a17SJack Morgenstein 		mlx4_ib_warn(&dev->ib_dev, "Failed init alias guid process.\n");
2328a0c64a17SJack Morgenstein 		goto paravirt_err;
2329a0c64a17SJack Morgenstein 	}
2330c1e7e466SJack Morgenstein 	err = mlx4_ib_device_register_sysfs(dev);
2331c1e7e466SJack Morgenstein 	if (err) {
2332c1e7e466SJack Morgenstein 		mlx4_ib_warn(&dev->ib_dev, "Failed to register sysfs\n");
2333c1e7e466SJack Morgenstein 		goto sysfs_err;
2334c1e7e466SJack Morgenstein 	}
2335a0c64a17SJack Morgenstein 
2336fc06573dSJack Morgenstein 	mlx4_ib_warn(&dev->ib_dev, "initializing demux service for %d qp1 clients\n",
2337fc06573dSJack Morgenstein 		     dev->dev->caps.sqp_demux);
2338fc06573dSJack Morgenstein 	for (i = 0; i < dev->num_ports; i++) {
2339a0c64a17SJack Morgenstein 		union ib_gid gid;
2340a0c64a17SJack Morgenstein 		err = __mlx4_ib_query_gid(&dev->ib_dev, i + 1, 0, &gid, 1);
2341a0c64a17SJack Morgenstein 		if (err)
2342a0c64a17SJack Morgenstein 			goto demux_err;
2343a0c64a17SJack Morgenstein 		dev->sriov.demux[i].guid_cache[0] = gid.global.interface_id;
23448ec07bf8SJack Morgenstein 		atomic64_set(&dev->sriov.demux[i].subnet_prefix,
23458ec07bf8SJack Morgenstein 			     be64_to_cpu(gid.global.subnet_prefix));
2346fc06573dSJack Morgenstein 		err = alloc_pv_object(dev, mlx4_master_func_num(dev->dev), i + 1,
2347fc06573dSJack Morgenstein 				      &dev->sriov.sqps[i]);
2348fc06573dSJack Morgenstein 		if (err)
2349fc06573dSJack Morgenstein 			goto demux_err;
2350fc06573dSJack Morgenstein 		err = mlx4_ib_alloc_demux_ctx(dev, &dev->sriov.demux[i], i + 1);
2351fc06573dSJack Morgenstein 		if (err)
2352cab66d12SDan Carpenter 			goto free_pv;
2353fc06573dSJack Morgenstein 	}
2354fc06573dSJack Morgenstein 	mlx4_ib_master_tunnels(dev, 1);
2355fc06573dSJack Morgenstein 	return 0;
2356fc06573dSJack Morgenstein 
2357cab66d12SDan Carpenter free_pv:
2358cab66d12SDan Carpenter 	free_pv_object(dev, mlx4_master_func_num(dev->dev), i + 1);
2359fc06573dSJack Morgenstein demux_err:
2360cab66d12SDan Carpenter 	while (--i >= 0) {
2361fc06573dSJack Morgenstein 		free_pv_object(dev, mlx4_master_func_num(dev->dev), i + 1);
2362fc06573dSJack Morgenstein 		mlx4_ib_free_demux_ctx(&dev->sriov.demux[i]);
2363fc06573dSJack Morgenstein 	}
2364c1e7e466SJack Morgenstein 	mlx4_ib_device_unregister_sysfs(dev);
2365c1e7e466SJack Morgenstein 
2366c1e7e466SJack Morgenstein sysfs_err:
2367a0c64a17SJack Morgenstein 	mlx4_ib_destroy_alias_guid_service(dev);
2368a0c64a17SJack Morgenstein 
2369a0c64a17SJack Morgenstein paravirt_err:
23703cf69cc8SAmir Vadai 	mlx4_ib_cm_paravirt_clean(dev, -1);
2371fc06573dSJack Morgenstein 
2372fc06573dSJack Morgenstein 	return err;
2373fc06573dSJack Morgenstein }
2374fc06573dSJack Morgenstein 
mlx4_ib_close_sriov(struct mlx4_ib_dev * dev)2375fc06573dSJack Morgenstein void mlx4_ib_close_sriov(struct mlx4_ib_dev *dev)
2376fc06573dSJack Morgenstein {
2377fc06573dSJack Morgenstein 	int i;
2378fc06573dSJack Morgenstein 	unsigned long flags;
2379fc06573dSJack Morgenstein 
2380fc06573dSJack Morgenstein 	if (!mlx4_is_mfunc(dev->dev))
2381fc06573dSJack Morgenstein 		return;
2382fc06573dSJack Morgenstein 
2383fc06573dSJack Morgenstein 	spin_lock_irqsave(&dev->sriov.going_down_lock, flags);
2384fc06573dSJack Morgenstein 	dev->sriov.is_going_down = 1;
2385fc06573dSJack Morgenstein 	spin_unlock_irqrestore(&dev->sriov.going_down_lock, flags);
23863cf69cc8SAmir Vadai 	if (mlx4_is_master(dev->dev)) {
2387fc06573dSJack Morgenstein 		for (i = 0; i < dev->num_ports; i++) {
2388fc06573dSJack Morgenstein 			flush_workqueue(dev->sriov.demux[i].ud_wq);
2389fc06573dSJack Morgenstein 			mlx4_ib_free_sqp_ctx(dev->sriov.sqps[i]);
2390fc06573dSJack Morgenstein 			kfree(dev->sriov.sqps[i]);
2391fc06573dSJack Morgenstein 			dev->sriov.sqps[i] = NULL;
2392fc06573dSJack Morgenstein 			mlx4_ib_free_demux_ctx(&dev->sriov.demux[i]);
2393fc06573dSJack Morgenstein 		}
23943cf69cc8SAmir Vadai 
23953cf69cc8SAmir Vadai 		mlx4_ib_cm_paravirt_clean(dev, -1);
2396a0c64a17SJack Morgenstein 		mlx4_ib_destroy_alias_guid_service(dev);
2397c1e7e466SJack Morgenstein 		mlx4_ib_device_unregister_sysfs(dev);
23983cf69cc8SAmir Vadai 	}
2399fc06573dSJack Morgenstein }
2400