xref: /openbmc/linux/drivers/infiniband/hw/mlx4/mad.c (revision 1fa6ac37)
1 /*
2  * Copyright (c) 2007 Cisco Systems, Inc. All rights reserved.
3  *
4  * This software is available to you under a choice of one of two
5  * licenses.  You may choose to be licensed under the terms of the GNU
6  * General Public License (GPL) Version 2, available from the file
7  * COPYING in the main directory of this source tree, or the
8  * OpenIB.org BSD license below:
9  *
10  *     Redistribution and use in source and binary forms, with or
11  *     without modification, are permitted provided that the following
12  *     conditions are met:
13  *
14  *      - Redistributions of source code must retain the above
15  *        copyright notice, this list of conditions and the following
16  *        disclaimer.
17  *
18  *      - Redistributions in binary form must reproduce the above
19  *        copyright notice, this list of conditions and the following
20  *        disclaimer in the documentation and/or other materials
21  *        provided with the distribution.
22  *
23  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
27  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
28  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
29  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
30  * SOFTWARE.
31  */
32 
33 #include <rdma/ib_mad.h>
34 #include <rdma/ib_smi.h>
35 
36 #include <linux/mlx4/cmd.h>
37 #include <linux/gfp.h>
38 
39 #include "mlx4_ib.h"
40 
41 enum {
42 	MLX4_IB_VENDOR_CLASS1 = 0x9,
43 	MLX4_IB_VENDOR_CLASS2 = 0xa
44 };
45 
46 int mlx4_MAD_IFC(struct mlx4_ib_dev *dev, int ignore_mkey, int ignore_bkey,
47 		 int port, struct ib_wc *in_wc, struct ib_grh *in_grh,
48 		 void *in_mad, void *response_mad)
49 {
50 	struct mlx4_cmd_mailbox *inmailbox, *outmailbox;
51 	void *inbox;
52 	int err;
53 	u32 in_modifier = port;
54 	u8 op_modifier = 0;
55 
56 	inmailbox = mlx4_alloc_cmd_mailbox(dev->dev);
57 	if (IS_ERR(inmailbox))
58 		return PTR_ERR(inmailbox);
59 	inbox = inmailbox->buf;
60 
61 	outmailbox = mlx4_alloc_cmd_mailbox(dev->dev);
62 	if (IS_ERR(outmailbox)) {
63 		mlx4_free_cmd_mailbox(dev->dev, inmailbox);
64 		return PTR_ERR(outmailbox);
65 	}
66 
67 	memcpy(inbox, in_mad, 256);
68 
69 	/*
70 	 * Key check traps can't be generated unless we have in_wc to
71 	 * tell us where to send the trap.
72 	 */
73 	if (ignore_mkey || !in_wc)
74 		op_modifier |= 0x1;
75 	if (ignore_bkey || !in_wc)
76 		op_modifier |= 0x2;
77 
78 	if (in_wc) {
79 		struct {
80 			__be32		my_qpn;
81 			u32		reserved1;
82 			__be32		rqpn;
83 			u8		sl;
84 			u8		g_path;
85 			u16		reserved2[2];
86 			__be16		pkey;
87 			u32		reserved3[11];
88 			u8		grh[40];
89 		} *ext_info;
90 
91 		memset(inbox + 256, 0, 256);
92 		ext_info = inbox + 256;
93 
94 		ext_info->my_qpn = cpu_to_be32(in_wc->qp->qp_num);
95 		ext_info->rqpn   = cpu_to_be32(in_wc->src_qp);
96 		ext_info->sl     = in_wc->sl << 4;
97 		ext_info->g_path = in_wc->dlid_path_bits |
98 			(in_wc->wc_flags & IB_WC_GRH ? 0x80 : 0);
99 		ext_info->pkey   = cpu_to_be16(in_wc->pkey_index);
100 
101 		if (in_grh)
102 			memcpy(ext_info->grh, in_grh, 40);
103 
104 		op_modifier |= 0x4;
105 
106 		in_modifier |= in_wc->slid << 16;
107 	}
108 
109 	err = mlx4_cmd_box(dev->dev, inmailbox->dma, outmailbox->dma,
110 			   in_modifier, op_modifier,
111 			   MLX4_CMD_MAD_IFC, MLX4_CMD_TIME_CLASS_C);
112 
113 	if (!err)
114 		memcpy(response_mad, outmailbox->buf, 256);
115 
116 	mlx4_free_cmd_mailbox(dev->dev, inmailbox);
117 	mlx4_free_cmd_mailbox(dev->dev, outmailbox);
118 
119 	return err;
120 }
121 
122 static void update_sm_ah(struct mlx4_ib_dev *dev, u8 port_num, u16 lid, u8 sl)
123 {
124 	struct ib_ah *new_ah;
125 	struct ib_ah_attr ah_attr;
126 
127 	if (!dev->send_agent[port_num - 1][0])
128 		return;
129 
130 	memset(&ah_attr, 0, sizeof ah_attr);
131 	ah_attr.dlid     = lid;
132 	ah_attr.sl       = sl;
133 	ah_attr.port_num = port_num;
134 
135 	new_ah = ib_create_ah(dev->send_agent[port_num - 1][0]->qp->pd,
136 			      &ah_attr);
137 	if (IS_ERR(new_ah))
138 		return;
139 
140 	spin_lock(&dev->sm_lock);
141 	if (dev->sm_ah[port_num - 1])
142 		ib_destroy_ah(dev->sm_ah[port_num - 1]);
143 	dev->sm_ah[port_num - 1] = new_ah;
144 	spin_unlock(&dev->sm_lock);
145 }
146 
147 /*
148  * Snoop SM MADs for port info and P_Key table sets, so we can
149  * synthesize LID change and P_Key change events.
150  */
151 static void smp_snoop(struct ib_device *ibdev, u8 port_num, struct ib_mad *mad,
152 				u16 prev_lid)
153 {
154 	struct ib_event event;
155 
156 	if ((mad->mad_hdr.mgmt_class == IB_MGMT_CLASS_SUBN_LID_ROUTED ||
157 	     mad->mad_hdr.mgmt_class == IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE) &&
158 	    mad->mad_hdr.method == IB_MGMT_METHOD_SET) {
159 		if (mad->mad_hdr.attr_id == IB_SMP_ATTR_PORT_INFO) {
160 			struct ib_port_info *pinfo =
161 				(struct ib_port_info *) ((struct ib_smp *) mad)->data;
162 			u16 lid = be16_to_cpu(pinfo->lid);
163 
164 			update_sm_ah(to_mdev(ibdev), port_num,
165 				     be16_to_cpu(pinfo->sm_lid),
166 				     pinfo->neighbormtu_mastersmsl & 0xf);
167 
168 			event.device	       = ibdev;
169 			event.element.port_num = port_num;
170 
171 			if (pinfo->clientrereg_resv_subnetto & 0x80) {
172 				event.event    = IB_EVENT_CLIENT_REREGISTER;
173 				ib_dispatch_event(&event);
174 			}
175 
176 			if (prev_lid != lid) {
177 				event.event    = IB_EVENT_LID_CHANGE;
178 				ib_dispatch_event(&event);
179 			}
180 		}
181 
182 		if (mad->mad_hdr.attr_id == IB_SMP_ATTR_PKEY_TABLE) {
183 			event.device	       = ibdev;
184 			event.event	       = IB_EVENT_PKEY_CHANGE;
185 			event.element.port_num = port_num;
186 			ib_dispatch_event(&event);
187 		}
188 	}
189 }
190 
191 static void node_desc_override(struct ib_device *dev,
192 			       struct ib_mad *mad)
193 {
194 	if ((mad->mad_hdr.mgmt_class == IB_MGMT_CLASS_SUBN_LID_ROUTED ||
195 	     mad->mad_hdr.mgmt_class == IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE) &&
196 	    mad->mad_hdr.method == IB_MGMT_METHOD_GET_RESP &&
197 	    mad->mad_hdr.attr_id == IB_SMP_ATTR_NODE_DESC) {
198 		spin_lock(&to_mdev(dev)->sm_lock);
199 		memcpy(((struct ib_smp *) mad)->data, dev->node_desc, 64);
200 		spin_unlock(&to_mdev(dev)->sm_lock);
201 	}
202 }
203 
204 static void forward_trap(struct mlx4_ib_dev *dev, u8 port_num, struct ib_mad *mad)
205 {
206 	int qpn = mad->mad_hdr.mgmt_class != IB_MGMT_CLASS_SUBN_LID_ROUTED;
207 	struct ib_mad_send_buf *send_buf;
208 	struct ib_mad_agent *agent = dev->send_agent[port_num - 1][qpn];
209 	int ret;
210 
211 	if (agent) {
212 		send_buf = ib_create_send_mad(agent, qpn, 0, 0, IB_MGMT_MAD_HDR,
213 					      IB_MGMT_MAD_DATA, GFP_ATOMIC);
214 		/*
215 		 * We rely here on the fact that MLX QPs don't use the
216 		 * address handle after the send is posted (this is
217 		 * wrong following the IB spec strictly, but we know
218 		 * it's OK for our devices).
219 		 */
220 		spin_lock(&dev->sm_lock);
221 		memcpy(send_buf->mad, mad, sizeof *mad);
222 		if ((send_buf->ah = dev->sm_ah[port_num - 1]))
223 			ret = ib_post_send_mad(send_buf, NULL);
224 		else
225 			ret = -EINVAL;
226 		spin_unlock(&dev->sm_lock);
227 
228 		if (ret)
229 			ib_free_send_mad(send_buf);
230 	}
231 }
232 
233 int mlx4_ib_process_mad(struct ib_device *ibdev, int mad_flags,	u8 port_num,
234 			struct ib_wc *in_wc, struct ib_grh *in_grh,
235 			struct ib_mad *in_mad, struct ib_mad *out_mad)
236 {
237 	u16 slid, prev_lid = 0;
238 	int err;
239 	struct ib_port_attr pattr;
240 
241 	slid = in_wc ? in_wc->slid : be16_to_cpu(IB_LID_PERMISSIVE);
242 
243 	if (in_mad->mad_hdr.method == IB_MGMT_METHOD_TRAP && slid == 0) {
244 		forward_trap(to_mdev(ibdev), port_num, in_mad);
245 		return IB_MAD_RESULT_SUCCESS | IB_MAD_RESULT_CONSUMED;
246 	}
247 
248 	if (in_mad->mad_hdr.mgmt_class == IB_MGMT_CLASS_SUBN_LID_ROUTED ||
249 	    in_mad->mad_hdr.mgmt_class == IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE) {
250 		if (in_mad->mad_hdr.method   != IB_MGMT_METHOD_GET &&
251 		    in_mad->mad_hdr.method   != IB_MGMT_METHOD_SET &&
252 		    in_mad->mad_hdr.method   != IB_MGMT_METHOD_TRAP_REPRESS)
253 			return IB_MAD_RESULT_SUCCESS;
254 
255 		/*
256 		 * Don't process SMInfo queries or vendor-specific
257 		 * MADs -- the SMA can't handle them.
258 		 */
259 		if (in_mad->mad_hdr.attr_id == IB_SMP_ATTR_SM_INFO ||
260 		    ((in_mad->mad_hdr.attr_id & IB_SMP_ATTR_VENDOR_MASK) ==
261 		     IB_SMP_ATTR_VENDOR_MASK))
262 			return IB_MAD_RESULT_SUCCESS;
263 	} else if (in_mad->mad_hdr.mgmt_class == IB_MGMT_CLASS_PERF_MGMT ||
264 		   in_mad->mad_hdr.mgmt_class == MLX4_IB_VENDOR_CLASS1   ||
265 		   in_mad->mad_hdr.mgmt_class == MLX4_IB_VENDOR_CLASS2   ||
266 		   in_mad->mad_hdr.mgmt_class == IB_MGMT_CLASS_CONG_MGMT) {
267 		if (in_mad->mad_hdr.method  != IB_MGMT_METHOD_GET &&
268 		    in_mad->mad_hdr.method  != IB_MGMT_METHOD_SET)
269 			return IB_MAD_RESULT_SUCCESS;
270 	} else
271 		return IB_MAD_RESULT_SUCCESS;
272 
273 	if ((in_mad->mad_hdr.mgmt_class == IB_MGMT_CLASS_SUBN_LID_ROUTED ||
274 	     in_mad->mad_hdr.mgmt_class == IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE) &&
275 	    in_mad->mad_hdr.method == IB_MGMT_METHOD_SET &&
276 	    in_mad->mad_hdr.attr_id == IB_SMP_ATTR_PORT_INFO &&
277 	    !ib_query_port(ibdev, port_num, &pattr))
278 		prev_lid = pattr.lid;
279 
280 	err = mlx4_MAD_IFC(to_mdev(ibdev),
281 			   mad_flags & IB_MAD_IGNORE_MKEY,
282 			   mad_flags & IB_MAD_IGNORE_BKEY,
283 			   port_num, in_wc, in_grh, in_mad, out_mad);
284 	if (err)
285 		return IB_MAD_RESULT_FAILURE;
286 
287 	if (!out_mad->mad_hdr.status) {
288 		smp_snoop(ibdev, port_num, in_mad, prev_lid);
289 		node_desc_override(ibdev, out_mad);
290 	}
291 
292 	/* set return bit in status of directed route responses */
293 	if (in_mad->mad_hdr.mgmt_class == IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE)
294 		out_mad->mad_hdr.status |= cpu_to_be16(1 << 15);
295 
296 	if (in_mad->mad_hdr.method == IB_MGMT_METHOD_TRAP_REPRESS)
297 		/* no response for trap repress */
298 		return IB_MAD_RESULT_SUCCESS | IB_MAD_RESULT_CONSUMED;
299 
300 	return IB_MAD_RESULT_SUCCESS | IB_MAD_RESULT_REPLY;
301 }
302 
303 static void send_handler(struct ib_mad_agent *agent,
304 			 struct ib_mad_send_wc *mad_send_wc)
305 {
306 	ib_free_send_mad(mad_send_wc->send_buf);
307 }
308 
309 int mlx4_ib_mad_init(struct mlx4_ib_dev *dev)
310 {
311 	struct ib_mad_agent *agent;
312 	int p, q;
313 	int ret;
314 
315 	for (p = 0; p < dev->num_ports; ++p)
316 		for (q = 0; q <= 1; ++q) {
317 			agent = ib_register_mad_agent(&dev->ib_dev, p + 1,
318 						      q ? IB_QPT_GSI : IB_QPT_SMI,
319 						      NULL, 0, send_handler,
320 						      NULL, NULL);
321 			if (IS_ERR(agent)) {
322 				ret = PTR_ERR(agent);
323 				goto err;
324 			}
325 			dev->send_agent[p][q] = agent;
326 		}
327 
328 	return 0;
329 
330 err:
331 	for (p = 0; p < dev->num_ports; ++p)
332 		for (q = 0; q <= 1; ++q)
333 			if (dev->send_agent[p][q])
334 				ib_unregister_mad_agent(dev->send_agent[p][q]);
335 
336 	return ret;
337 }
338 
339 void mlx4_ib_mad_cleanup(struct mlx4_ib_dev *dev)
340 {
341 	struct ib_mad_agent *agent;
342 	int p, q;
343 
344 	for (p = 0; p < dev->num_ports; ++p) {
345 		for (q = 0; q <= 1; ++q) {
346 			agent = dev->send_agent[p][q];
347 			dev->send_agent[p][q] = NULL;
348 			ib_unregister_mad_agent(agent);
349 		}
350 
351 		if (dev->sm_ah[p])
352 			ib_destroy_ah(dev->sm_ah[p]);
353 	}
354 }
355