xref: /openbmc/linux/drivers/infiniband/hw/mlx4/mad.c (revision 6578cf33989a594bab37af988d45d87812b946b8)
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>
35225c7b1fSRoland Dreier 
36225c7b1fSRoland Dreier #include <linux/mlx4/cmd.h>
37225c7b1fSRoland Dreier 
38225c7b1fSRoland Dreier #include "mlx4_ib.h"
39225c7b1fSRoland Dreier 
40225c7b1fSRoland Dreier enum {
41225c7b1fSRoland Dreier 	MLX4_IB_VENDOR_CLASS1 = 0x9,
42225c7b1fSRoland Dreier 	MLX4_IB_VENDOR_CLASS2 = 0xa
43225c7b1fSRoland Dreier };
44225c7b1fSRoland Dreier 
45225c7b1fSRoland Dreier int mlx4_MAD_IFC(struct mlx4_ib_dev *dev, int ignore_mkey, int ignore_bkey,
46225c7b1fSRoland Dreier 		 int port, struct ib_wc *in_wc, struct ib_grh *in_grh,
47225c7b1fSRoland Dreier 		 void *in_mad, void *response_mad)
48225c7b1fSRoland Dreier {
49225c7b1fSRoland Dreier 	struct mlx4_cmd_mailbox *inmailbox, *outmailbox;
50225c7b1fSRoland Dreier 	void *inbox;
51225c7b1fSRoland Dreier 	int err;
52225c7b1fSRoland Dreier 	u32 in_modifier = port;
53225c7b1fSRoland Dreier 	u8 op_modifier = 0;
54225c7b1fSRoland Dreier 
55225c7b1fSRoland Dreier 	inmailbox = mlx4_alloc_cmd_mailbox(dev->dev);
56225c7b1fSRoland Dreier 	if (IS_ERR(inmailbox))
57225c7b1fSRoland Dreier 		return PTR_ERR(inmailbox);
58225c7b1fSRoland Dreier 	inbox = inmailbox->buf;
59225c7b1fSRoland Dreier 
60225c7b1fSRoland Dreier 	outmailbox = mlx4_alloc_cmd_mailbox(dev->dev);
61225c7b1fSRoland Dreier 	if (IS_ERR(outmailbox)) {
62225c7b1fSRoland Dreier 		mlx4_free_cmd_mailbox(dev->dev, inmailbox);
63225c7b1fSRoland Dreier 		return PTR_ERR(outmailbox);
64225c7b1fSRoland Dreier 	}
65225c7b1fSRoland Dreier 
66225c7b1fSRoland Dreier 	memcpy(inbox, in_mad, 256);
67225c7b1fSRoland Dreier 
68225c7b1fSRoland Dreier 	/*
69225c7b1fSRoland Dreier 	 * Key check traps can't be generated unless we have in_wc to
70225c7b1fSRoland Dreier 	 * tell us where to send the trap.
71225c7b1fSRoland Dreier 	 */
72225c7b1fSRoland Dreier 	if (ignore_mkey || !in_wc)
73225c7b1fSRoland Dreier 		op_modifier |= 0x1;
74225c7b1fSRoland Dreier 	if (ignore_bkey || !in_wc)
75225c7b1fSRoland Dreier 		op_modifier |= 0x2;
76225c7b1fSRoland Dreier 
77225c7b1fSRoland Dreier 	if (in_wc) {
78225c7b1fSRoland Dreier 		struct {
79225c7b1fSRoland Dreier 			__be32		my_qpn;
80225c7b1fSRoland Dreier 			u32		reserved1;
81225c7b1fSRoland Dreier 			__be32		rqpn;
82225c7b1fSRoland Dreier 			u8		sl;
83225c7b1fSRoland Dreier 			u8		g_path;
84225c7b1fSRoland Dreier 			u16		reserved2[2];
85225c7b1fSRoland Dreier 			__be16		pkey;
86225c7b1fSRoland Dreier 			u32		reserved3[11];
87225c7b1fSRoland Dreier 			u8		grh[40];
88225c7b1fSRoland Dreier 		} *ext_info;
89225c7b1fSRoland Dreier 
90225c7b1fSRoland Dreier 		memset(inbox + 256, 0, 256);
91225c7b1fSRoland Dreier 		ext_info = inbox + 256;
92225c7b1fSRoland Dreier 
93225c7b1fSRoland Dreier 		ext_info->my_qpn = cpu_to_be32(in_wc->qp->qp_num);
94225c7b1fSRoland Dreier 		ext_info->rqpn   = cpu_to_be32(in_wc->src_qp);
95225c7b1fSRoland Dreier 		ext_info->sl     = in_wc->sl << 4;
96225c7b1fSRoland Dreier 		ext_info->g_path = in_wc->dlid_path_bits |
97225c7b1fSRoland Dreier 			(in_wc->wc_flags & IB_WC_GRH ? 0x80 : 0);
98225c7b1fSRoland Dreier 		ext_info->pkey   = cpu_to_be16(in_wc->pkey_index);
99225c7b1fSRoland Dreier 
100225c7b1fSRoland Dreier 		if (in_grh)
101225c7b1fSRoland Dreier 			memcpy(ext_info->grh, in_grh, 40);
102225c7b1fSRoland Dreier 
103225c7b1fSRoland Dreier 		op_modifier |= 0x4;
104225c7b1fSRoland Dreier 
105225c7b1fSRoland Dreier 		in_modifier |= in_wc->slid << 16;
106225c7b1fSRoland Dreier 	}
107225c7b1fSRoland Dreier 
108225c7b1fSRoland Dreier 	err = mlx4_cmd_box(dev->dev, inmailbox->dma, outmailbox->dma,
109225c7b1fSRoland Dreier 			   in_modifier, op_modifier,
110225c7b1fSRoland Dreier 			   MLX4_CMD_MAD_IFC, MLX4_CMD_TIME_CLASS_C);
111225c7b1fSRoland Dreier 
112fe11cb6bSIlpo Järvinen 	if (!err)
113225c7b1fSRoland Dreier 		memcpy(response_mad, outmailbox->buf, 256);
114225c7b1fSRoland Dreier 
115225c7b1fSRoland Dreier 	mlx4_free_cmd_mailbox(dev->dev, inmailbox);
116225c7b1fSRoland Dreier 	mlx4_free_cmd_mailbox(dev->dev, outmailbox);
117225c7b1fSRoland Dreier 
118225c7b1fSRoland Dreier 	return err;
119225c7b1fSRoland Dreier }
120225c7b1fSRoland Dreier 
121225c7b1fSRoland Dreier static void update_sm_ah(struct mlx4_ib_dev *dev, u8 port_num, u16 lid, u8 sl)
122225c7b1fSRoland Dreier {
123225c7b1fSRoland Dreier 	struct ib_ah *new_ah;
124225c7b1fSRoland Dreier 	struct ib_ah_attr ah_attr;
125225c7b1fSRoland Dreier 
126225c7b1fSRoland Dreier 	if (!dev->send_agent[port_num - 1][0])
127225c7b1fSRoland Dreier 		return;
128225c7b1fSRoland Dreier 
129225c7b1fSRoland Dreier 	memset(&ah_attr, 0, sizeof ah_attr);
130225c7b1fSRoland Dreier 	ah_attr.dlid     = lid;
131225c7b1fSRoland Dreier 	ah_attr.sl       = sl;
132225c7b1fSRoland Dreier 	ah_attr.port_num = port_num;
133225c7b1fSRoland Dreier 
134225c7b1fSRoland Dreier 	new_ah = ib_create_ah(dev->send_agent[port_num - 1][0]->qp->pd,
135225c7b1fSRoland Dreier 			      &ah_attr);
136225c7b1fSRoland Dreier 	if (IS_ERR(new_ah))
137225c7b1fSRoland Dreier 		return;
138225c7b1fSRoland Dreier 
139225c7b1fSRoland Dreier 	spin_lock(&dev->sm_lock);
140225c7b1fSRoland Dreier 	if (dev->sm_ah[port_num - 1])
141225c7b1fSRoland Dreier 		ib_destroy_ah(dev->sm_ah[port_num - 1]);
142225c7b1fSRoland Dreier 	dev->sm_ah[port_num - 1] = new_ah;
143225c7b1fSRoland Dreier 	spin_unlock(&dev->sm_lock);
144225c7b1fSRoland Dreier }
145225c7b1fSRoland Dreier 
146225c7b1fSRoland Dreier /*
147225c7b1fSRoland Dreier  * Snoop SM MADs for port info and P_Key table sets, so we can
148225c7b1fSRoland Dreier  * synthesize LID change and P_Key change events.
149225c7b1fSRoland Dreier  */
150225c7b1fSRoland Dreier static void smp_snoop(struct ib_device *ibdev, u8 port_num, struct ib_mad *mad)
151225c7b1fSRoland Dreier {
152225c7b1fSRoland Dreier 	struct ib_event event;
153225c7b1fSRoland Dreier 
154225c7b1fSRoland Dreier 	if ((mad->mad_hdr.mgmt_class == IB_MGMT_CLASS_SUBN_LID_ROUTED ||
155225c7b1fSRoland Dreier 	     mad->mad_hdr.mgmt_class == IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE) &&
156225c7b1fSRoland Dreier 	    mad->mad_hdr.method == IB_MGMT_METHOD_SET) {
157225c7b1fSRoland Dreier 		if (mad->mad_hdr.attr_id == IB_SMP_ATTR_PORT_INFO) {
158225c7b1fSRoland Dreier 			struct ib_port_info *pinfo =
159225c7b1fSRoland Dreier 				(struct ib_port_info *) ((struct ib_smp *) mad)->data;
160225c7b1fSRoland Dreier 
161225c7b1fSRoland Dreier 			update_sm_ah(to_mdev(ibdev), port_num,
162225c7b1fSRoland Dreier 				     be16_to_cpu(pinfo->sm_lid),
163225c7b1fSRoland Dreier 				     pinfo->neighbormtu_mastersmsl & 0xf);
164225c7b1fSRoland Dreier 
165225c7b1fSRoland Dreier 			event.device	       = ibdev;
166225c7b1fSRoland Dreier 			event.element.port_num = port_num;
167225c7b1fSRoland Dreier 
168225c7b1fSRoland Dreier 			if (pinfo->clientrereg_resv_subnetto & 0x80)
169225c7b1fSRoland Dreier 				event.event    = IB_EVENT_CLIENT_REREGISTER;
170225c7b1fSRoland Dreier 			else
171225c7b1fSRoland Dreier 				event.event    = IB_EVENT_LID_CHANGE;
172225c7b1fSRoland Dreier 
173225c7b1fSRoland Dreier 			ib_dispatch_event(&event);
174225c7b1fSRoland Dreier 		}
175225c7b1fSRoland Dreier 
176225c7b1fSRoland Dreier 		if (mad->mad_hdr.attr_id == IB_SMP_ATTR_PKEY_TABLE) {
177225c7b1fSRoland Dreier 			event.device	       = ibdev;
178225c7b1fSRoland Dreier 			event.event	       = IB_EVENT_PKEY_CHANGE;
179225c7b1fSRoland Dreier 			event.element.port_num = port_num;
180225c7b1fSRoland Dreier 			ib_dispatch_event(&event);
181225c7b1fSRoland Dreier 		}
182225c7b1fSRoland Dreier 	}
183225c7b1fSRoland Dreier }
184225c7b1fSRoland Dreier 
185225c7b1fSRoland Dreier static void node_desc_override(struct ib_device *dev,
186225c7b1fSRoland Dreier 			       struct ib_mad *mad)
187225c7b1fSRoland Dreier {
188225c7b1fSRoland Dreier 	if ((mad->mad_hdr.mgmt_class == IB_MGMT_CLASS_SUBN_LID_ROUTED ||
189225c7b1fSRoland Dreier 	     mad->mad_hdr.mgmt_class == IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE) &&
190225c7b1fSRoland Dreier 	    mad->mad_hdr.method == IB_MGMT_METHOD_GET_RESP &&
191225c7b1fSRoland Dreier 	    mad->mad_hdr.attr_id == IB_SMP_ATTR_NODE_DESC) {
192225c7b1fSRoland Dreier 		spin_lock(&to_mdev(dev)->sm_lock);
193225c7b1fSRoland Dreier 		memcpy(((struct ib_smp *) mad)->data, dev->node_desc, 64);
194225c7b1fSRoland Dreier 		spin_unlock(&to_mdev(dev)->sm_lock);
195225c7b1fSRoland Dreier 	}
196225c7b1fSRoland Dreier }
197225c7b1fSRoland Dreier 
198225c7b1fSRoland Dreier static void forward_trap(struct mlx4_ib_dev *dev, u8 port_num, struct ib_mad *mad)
199225c7b1fSRoland Dreier {
200225c7b1fSRoland Dreier 	int qpn = mad->mad_hdr.mgmt_class != IB_MGMT_CLASS_SUBN_LID_ROUTED;
201225c7b1fSRoland Dreier 	struct ib_mad_send_buf *send_buf;
202225c7b1fSRoland Dreier 	struct ib_mad_agent *agent = dev->send_agent[port_num - 1][qpn];
203225c7b1fSRoland Dreier 	int ret;
204225c7b1fSRoland Dreier 
205225c7b1fSRoland Dreier 	if (agent) {
206225c7b1fSRoland Dreier 		send_buf = ib_create_send_mad(agent, qpn, 0, 0, IB_MGMT_MAD_HDR,
207225c7b1fSRoland Dreier 					      IB_MGMT_MAD_DATA, GFP_ATOMIC);
208225c7b1fSRoland Dreier 		/*
209225c7b1fSRoland Dreier 		 * We rely here on the fact that MLX QPs don't use the
210225c7b1fSRoland Dreier 		 * address handle after the send is posted (this is
211225c7b1fSRoland Dreier 		 * wrong following the IB spec strictly, but we know
212225c7b1fSRoland Dreier 		 * it's OK for our devices).
213225c7b1fSRoland Dreier 		 */
214225c7b1fSRoland Dreier 		spin_lock(&dev->sm_lock);
215225c7b1fSRoland Dreier 		memcpy(send_buf->mad, mad, sizeof *mad);
216225c7b1fSRoland Dreier 		if ((send_buf->ah = dev->sm_ah[port_num - 1]))
217225c7b1fSRoland Dreier 			ret = ib_post_send_mad(send_buf, NULL);
218225c7b1fSRoland Dreier 		else
219225c7b1fSRoland Dreier 			ret = -EINVAL;
220225c7b1fSRoland Dreier 		spin_unlock(&dev->sm_lock);
221225c7b1fSRoland Dreier 
222225c7b1fSRoland Dreier 		if (ret)
223225c7b1fSRoland Dreier 			ib_free_send_mad(send_buf);
224225c7b1fSRoland Dreier 	}
225225c7b1fSRoland Dreier }
226225c7b1fSRoland Dreier 
227225c7b1fSRoland Dreier int mlx4_ib_process_mad(struct ib_device *ibdev, int mad_flags,	u8 port_num,
228225c7b1fSRoland Dreier 			struct ib_wc *in_wc, struct ib_grh *in_grh,
229225c7b1fSRoland Dreier 			struct ib_mad *in_mad, struct ib_mad *out_mad)
230225c7b1fSRoland Dreier {
231225c7b1fSRoland Dreier 	u16 slid;
232225c7b1fSRoland Dreier 	int err;
233225c7b1fSRoland Dreier 
234225c7b1fSRoland Dreier 	slid = in_wc ? in_wc->slid : be16_to_cpu(IB_LID_PERMISSIVE);
235225c7b1fSRoland Dreier 
236225c7b1fSRoland Dreier 	if (in_mad->mad_hdr.method == IB_MGMT_METHOD_TRAP && slid == 0) {
237225c7b1fSRoland Dreier 		forward_trap(to_mdev(ibdev), port_num, in_mad);
238225c7b1fSRoland Dreier 		return IB_MAD_RESULT_SUCCESS | IB_MAD_RESULT_CONSUMED;
239225c7b1fSRoland Dreier 	}
240225c7b1fSRoland Dreier 
241225c7b1fSRoland Dreier 	if (in_mad->mad_hdr.mgmt_class == IB_MGMT_CLASS_SUBN_LID_ROUTED ||
242225c7b1fSRoland Dreier 	    in_mad->mad_hdr.mgmt_class == IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE) {
243225c7b1fSRoland Dreier 		if (in_mad->mad_hdr.method   != IB_MGMT_METHOD_GET &&
244225c7b1fSRoland Dreier 		    in_mad->mad_hdr.method   != IB_MGMT_METHOD_SET &&
245225c7b1fSRoland Dreier 		    in_mad->mad_hdr.method   != IB_MGMT_METHOD_TRAP_REPRESS)
246225c7b1fSRoland Dreier 			return IB_MAD_RESULT_SUCCESS;
247225c7b1fSRoland Dreier 
248225c7b1fSRoland Dreier 		/*
249225c7b1fSRoland Dreier 		 * Don't process SMInfo queries or vendor-specific
250225c7b1fSRoland Dreier 		 * MADs -- the SMA can't handle them.
251225c7b1fSRoland Dreier 		 */
252225c7b1fSRoland Dreier 		if (in_mad->mad_hdr.attr_id == IB_SMP_ATTR_SM_INFO ||
253225c7b1fSRoland Dreier 		    ((in_mad->mad_hdr.attr_id & IB_SMP_ATTR_VENDOR_MASK) ==
254225c7b1fSRoland Dreier 		     IB_SMP_ATTR_VENDOR_MASK))
255225c7b1fSRoland Dreier 			return IB_MAD_RESULT_SUCCESS;
256225c7b1fSRoland Dreier 	} else if (in_mad->mad_hdr.mgmt_class == IB_MGMT_CLASS_PERF_MGMT ||
257225c7b1fSRoland Dreier 		   in_mad->mad_hdr.mgmt_class == MLX4_IB_VENDOR_CLASS1   ||
258*6578cf33SEli Cohen 		   in_mad->mad_hdr.mgmt_class == MLX4_IB_VENDOR_CLASS2   ||
259*6578cf33SEli Cohen 		   in_mad->mad_hdr.mgmt_class == IB_MGMT_CLASS_CONG_MGMT) {
260225c7b1fSRoland Dreier 		if (in_mad->mad_hdr.method  != IB_MGMT_METHOD_GET &&
261225c7b1fSRoland Dreier 		    in_mad->mad_hdr.method  != IB_MGMT_METHOD_SET)
262225c7b1fSRoland Dreier 			return IB_MAD_RESULT_SUCCESS;
263225c7b1fSRoland Dreier 	} else
264225c7b1fSRoland Dreier 		return IB_MAD_RESULT_SUCCESS;
265225c7b1fSRoland Dreier 
266225c7b1fSRoland Dreier 	err = mlx4_MAD_IFC(to_mdev(ibdev),
267225c7b1fSRoland Dreier 			   mad_flags & IB_MAD_IGNORE_MKEY,
268225c7b1fSRoland Dreier 			   mad_flags & IB_MAD_IGNORE_BKEY,
269225c7b1fSRoland Dreier 			   port_num, in_wc, in_grh, in_mad, out_mad);
270225c7b1fSRoland Dreier 	if (err)
271225c7b1fSRoland Dreier 		return IB_MAD_RESULT_FAILURE;
272225c7b1fSRoland Dreier 
273225c7b1fSRoland Dreier 	if (!out_mad->mad_hdr.status) {
274225c7b1fSRoland Dreier 		smp_snoop(ibdev, port_num, in_mad);
275225c7b1fSRoland Dreier 		node_desc_override(ibdev, out_mad);
276225c7b1fSRoland Dreier 	}
277225c7b1fSRoland Dreier 
278225c7b1fSRoland Dreier 	/* set return bit in status of directed route responses */
279225c7b1fSRoland Dreier 	if (in_mad->mad_hdr.mgmt_class == IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE)
280225c7b1fSRoland Dreier 		out_mad->mad_hdr.status |= cpu_to_be16(1 << 15);
281225c7b1fSRoland Dreier 
282225c7b1fSRoland Dreier 	if (in_mad->mad_hdr.method == IB_MGMT_METHOD_TRAP_REPRESS)
283225c7b1fSRoland Dreier 		/* no response for trap repress */
284225c7b1fSRoland Dreier 		return IB_MAD_RESULT_SUCCESS | IB_MAD_RESULT_CONSUMED;
285225c7b1fSRoland Dreier 
286225c7b1fSRoland Dreier 	return IB_MAD_RESULT_SUCCESS | IB_MAD_RESULT_REPLY;
287225c7b1fSRoland Dreier }
288225c7b1fSRoland Dreier 
289225c7b1fSRoland Dreier static void send_handler(struct ib_mad_agent *agent,
290225c7b1fSRoland Dreier 			 struct ib_mad_send_wc *mad_send_wc)
291225c7b1fSRoland Dreier {
292225c7b1fSRoland Dreier 	ib_free_send_mad(mad_send_wc->send_buf);
293225c7b1fSRoland Dreier }
294225c7b1fSRoland Dreier 
295225c7b1fSRoland Dreier int mlx4_ib_mad_init(struct mlx4_ib_dev *dev)
296225c7b1fSRoland Dreier {
297225c7b1fSRoland Dreier 	struct ib_mad_agent *agent;
298225c7b1fSRoland Dreier 	int p, q;
299225c7b1fSRoland Dreier 	int ret;
300225c7b1fSRoland Dreier 
301225c7b1fSRoland Dreier 	for (p = 0; p < dev->dev->caps.num_ports; ++p)
302225c7b1fSRoland Dreier 		for (q = 0; q <= 1; ++q) {
303225c7b1fSRoland Dreier 			agent = ib_register_mad_agent(&dev->ib_dev, p + 1,
304225c7b1fSRoland Dreier 						      q ? IB_QPT_GSI : IB_QPT_SMI,
305225c7b1fSRoland Dreier 						      NULL, 0, send_handler,
306225c7b1fSRoland Dreier 						      NULL, NULL);
307225c7b1fSRoland Dreier 			if (IS_ERR(agent)) {
308225c7b1fSRoland Dreier 				ret = PTR_ERR(agent);
309225c7b1fSRoland Dreier 				goto err;
310225c7b1fSRoland Dreier 			}
311225c7b1fSRoland Dreier 			dev->send_agent[p][q] = agent;
312225c7b1fSRoland Dreier 		}
313225c7b1fSRoland Dreier 
314225c7b1fSRoland Dreier 	return 0;
315225c7b1fSRoland Dreier 
316225c7b1fSRoland Dreier err:
317225c7b1fSRoland Dreier 	for (p = 0; p < dev->dev->caps.num_ports; ++p)
318225c7b1fSRoland Dreier 		for (q = 0; q <= 1; ++q)
319225c7b1fSRoland Dreier 			if (dev->send_agent[p][q])
320225c7b1fSRoland Dreier 				ib_unregister_mad_agent(dev->send_agent[p][q]);
321225c7b1fSRoland Dreier 
322225c7b1fSRoland Dreier 	return ret;
323225c7b1fSRoland Dreier }
324225c7b1fSRoland Dreier 
325225c7b1fSRoland Dreier void mlx4_ib_mad_cleanup(struct mlx4_ib_dev *dev)
326225c7b1fSRoland Dreier {
327225c7b1fSRoland Dreier 	struct ib_mad_agent *agent;
328225c7b1fSRoland Dreier 	int p, q;
329225c7b1fSRoland Dreier 
330225c7b1fSRoland Dreier 	for (p = 0; p < dev->dev->caps.num_ports; ++p) {
331225c7b1fSRoland Dreier 		for (q = 0; q <= 1; ++q) {
332225c7b1fSRoland Dreier 			agent = dev->send_agent[p][q];
333225c7b1fSRoland Dreier 			dev->send_agent[p][q] = NULL;
334225c7b1fSRoland Dreier 			ib_unregister_mad_agent(agent);
335225c7b1fSRoland Dreier 		}
336225c7b1fSRoland Dreier 
337225c7b1fSRoland Dreier 		if (dev->sm_ah[p])
338225c7b1fSRoland Dreier 			ib_destroy_ah(dev->sm_ah[p]);
339225c7b1fSRoland Dreier 	}
340225c7b1fSRoland Dreier }
341