blob: f36da994a85abec20aa1b1eab19660ce00367e6e [file] [log] [blame]
Roland Dreier225c7b12007-05-08 18:00:38 -07001/*
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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090037#include <linux/gfp.h>
Or Gerlitzc3779132011-06-15 14:51:27 +000038#include <rdma/ib_pma.h>
Roland Dreier225c7b12007-05-08 18:00:38 -070039
40#include "mlx4_ib.h"
41
42enum {
43 MLX4_IB_VENDOR_CLASS1 = 0x9,
44 MLX4_IB_VENDOR_CLASS2 = 0xa
45};
46
47int mlx4_MAD_IFC(struct mlx4_ib_dev *dev, int ignore_mkey, int ignore_bkey,
48 int port, struct ib_wc *in_wc, struct ib_grh *in_grh,
49 void *in_mad, void *response_mad)
50{
51 struct mlx4_cmd_mailbox *inmailbox, *outmailbox;
52 void *inbox;
53 int err;
54 u32 in_modifier = port;
55 u8 op_modifier = 0;
56
57 inmailbox = mlx4_alloc_cmd_mailbox(dev->dev);
58 if (IS_ERR(inmailbox))
59 return PTR_ERR(inmailbox);
60 inbox = inmailbox->buf;
61
62 outmailbox = mlx4_alloc_cmd_mailbox(dev->dev);
63 if (IS_ERR(outmailbox)) {
64 mlx4_free_cmd_mailbox(dev->dev, inmailbox);
65 return PTR_ERR(outmailbox);
66 }
67
68 memcpy(inbox, in_mad, 256);
69
70 /*
71 * Key check traps can't be generated unless we have in_wc to
72 * tell us where to send the trap.
73 */
74 if (ignore_mkey || !in_wc)
75 op_modifier |= 0x1;
76 if (ignore_bkey || !in_wc)
77 op_modifier |= 0x2;
78
79 if (in_wc) {
80 struct {
81 __be32 my_qpn;
82 u32 reserved1;
83 __be32 rqpn;
84 u8 sl;
85 u8 g_path;
86 u16 reserved2[2];
87 __be16 pkey;
88 u32 reserved3[11];
89 u8 grh[40];
90 } *ext_info;
91
92 memset(inbox + 256, 0, 256);
93 ext_info = inbox + 256;
94
95 ext_info->my_qpn = cpu_to_be32(in_wc->qp->qp_num);
96 ext_info->rqpn = cpu_to_be32(in_wc->src_qp);
97 ext_info->sl = in_wc->sl << 4;
98 ext_info->g_path = in_wc->dlid_path_bits |
99 (in_wc->wc_flags & IB_WC_GRH ? 0x80 : 0);
100 ext_info->pkey = cpu_to_be16(in_wc->pkey_index);
101
102 if (in_grh)
103 memcpy(ext_info->grh, in_grh, 40);
104
105 op_modifier |= 0x4;
106
107 in_modifier |= in_wc->slid << 16;
108 }
109
110 err = mlx4_cmd_box(dev->dev, inmailbox->dma, outmailbox->dma,
111 in_modifier, op_modifier,
112 MLX4_CMD_MAD_IFC, MLX4_CMD_TIME_CLASS_C);
113
Ilpo Järvinenfe11cb62007-08-16 01:02:07 +0300114 if (!err)
Roland Dreier225c7b12007-05-08 18:00:38 -0700115 memcpy(response_mad, outmailbox->buf, 256);
116
117 mlx4_free_cmd_mailbox(dev->dev, inmailbox);
118 mlx4_free_cmd_mailbox(dev->dev, outmailbox);
119
120 return err;
121}
122
123static void update_sm_ah(struct mlx4_ib_dev *dev, u8 port_num, u16 lid, u8 sl)
124{
125 struct ib_ah *new_ah;
126 struct ib_ah_attr ah_attr;
127
128 if (!dev->send_agent[port_num - 1][0])
129 return;
130
131 memset(&ah_attr, 0, sizeof ah_attr);
132 ah_attr.dlid = lid;
133 ah_attr.sl = sl;
134 ah_attr.port_num = port_num;
135
136 new_ah = ib_create_ah(dev->send_agent[port_num - 1][0]->qp->pd,
137 &ah_attr);
138 if (IS_ERR(new_ah))
139 return;
140
141 spin_lock(&dev->sm_lock);
142 if (dev->sm_ah[port_num - 1])
143 ib_destroy_ah(dev->sm_ah[port_num - 1]);
144 dev->sm_ah[port_num - 1] = new_ah;
145 spin_unlock(&dev->sm_lock);
146}
147
148/*
149 * Snoop SM MADs for port info and P_Key table sets, so we can
150 * synthesize LID change and P_Key change events.
151 */
Moni Shouaf0f6f342009-01-28 14:54:35 -0800152static void smp_snoop(struct ib_device *ibdev, u8 port_num, struct ib_mad *mad,
153 u16 prev_lid)
Roland Dreier225c7b12007-05-08 18:00:38 -0700154{
155 struct ib_event event;
156
157 if ((mad->mad_hdr.mgmt_class == IB_MGMT_CLASS_SUBN_LID_ROUTED ||
158 mad->mad_hdr.mgmt_class == IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE) &&
159 mad->mad_hdr.method == IB_MGMT_METHOD_SET) {
160 if (mad->mad_hdr.attr_id == IB_SMP_ATTR_PORT_INFO) {
161 struct ib_port_info *pinfo =
162 (struct ib_port_info *) ((struct ib_smp *) mad)->data;
Moni Shouaf0f6f342009-01-28 14:54:35 -0800163 u16 lid = be16_to_cpu(pinfo->lid);
Roland Dreier225c7b12007-05-08 18:00:38 -0700164
165 update_sm_ah(to_mdev(ibdev), port_num,
166 be16_to_cpu(pinfo->sm_lid),
167 pinfo->neighbormtu_mastersmsl & 0xf);
168
169 event.device = ibdev;
170 event.element.port_num = port_num;
171
Moni Shouaf0f6f342009-01-28 14:54:35 -0800172 if (pinfo->clientrereg_resv_subnetto & 0x80) {
Roland Dreier225c7b12007-05-08 18:00:38 -0700173 event.event = IB_EVENT_CLIENT_REREGISTER;
Moni Shouaf0f6f342009-01-28 14:54:35 -0800174 ib_dispatch_event(&event);
175 }
Roland Dreier225c7b12007-05-08 18:00:38 -0700176
Moni Shouaf0f6f342009-01-28 14:54:35 -0800177 if (prev_lid != lid) {
178 event.event = IB_EVENT_LID_CHANGE;
179 ib_dispatch_event(&event);
180 }
Roland Dreier225c7b12007-05-08 18:00:38 -0700181 }
182
183 if (mad->mad_hdr.attr_id == IB_SMP_ATTR_PKEY_TABLE) {
184 event.device = ibdev;
185 event.event = IB_EVENT_PKEY_CHANGE;
186 event.element.port_num = port_num;
187 ib_dispatch_event(&event);
188 }
189 }
190}
191
192static void node_desc_override(struct ib_device *dev,
193 struct ib_mad *mad)
194{
195 if ((mad->mad_hdr.mgmt_class == IB_MGMT_CLASS_SUBN_LID_ROUTED ||
196 mad->mad_hdr.mgmt_class == IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE) &&
197 mad->mad_hdr.method == IB_MGMT_METHOD_GET_RESP &&
198 mad->mad_hdr.attr_id == IB_SMP_ATTR_NODE_DESC) {
199 spin_lock(&to_mdev(dev)->sm_lock);
200 memcpy(((struct ib_smp *) mad)->data, dev->node_desc, 64);
201 spin_unlock(&to_mdev(dev)->sm_lock);
202 }
203}
204
205static void forward_trap(struct mlx4_ib_dev *dev, u8 port_num, struct ib_mad *mad)
206{
207 int qpn = mad->mad_hdr.mgmt_class != IB_MGMT_CLASS_SUBN_LID_ROUTED;
208 struct ib_mad_send_buf *send_buf;
209 struct ib_mad_agent *agent = dev->send_agent[port_num - 1][qpn];
210 int ret;
211
212 if (agent) {
213 send_buf = ib_create_send_mad(agent, qpn, 0, 0, IB_MGMT_MAD_HDR,
214 IB_MGMT_MAD_DATA, GFP_ATOMIC);
Dan Carpenter13974902011-01-10 17:42:06 -0800215 if (IS_ERR(send_buf))
216 return;
Roland Dreier225c7b12007-05-08 18:00:38 -0700217 /*
218 * We rely here on the fact that MLX QPs don't use the
219 * address handle after the send is posted (this is
220 * wrong following the IB spec strictly, but we know
221 * it's OK for our devices).
222 */
223 spin_lock(&dev->sm_lock);
224 memcpy(send_buf->mad, mad, sizeof *mad);
225 if ((send_buf->ah = dev->sm_ah[port_num - 1]))
226 ret = ib_post_send_mad(send_buf, NULL);
227 else
228 ret = -EINVAL;
229 spin_unlock(&dev->sm_lock);
230
231 if (ret)
232 ib_free_send_mad(send_buf);
233 }
234}
235
Or Gerlitzc3779132011-06-15 14:51:27 +0000236static int ib_process_mad(struct ib_device *ibdev, int mad_flags, u8 port_num,
Roland Dreier225c7b12007-05-08 18:00:38 -0700237 struct ib_wc *in_wc, struct ib_grh *in_grh,
238 struct ib_mad *in_mad, struct ib_mad *out_mad)
239{
Moni Shouaf0f6f342009-01-28 14:54:35 -0800240 u16 slid, prev_lid = 0;
Roland Dreier225c7b12007-05-08 18:00:38 -0700241 int err;
Moni Shouaf0f6f342009-01-28 14:54:35 -0800242 struct ib_port_attr pattr;
Roland Dreier225c7b12007-05-08 18:00:38 -0700243
244 slid = in_wc ? in_wc->slid : be16_to_cpu(IB_LID_PERMISSIVE);
245
246 if (in_mad->mad_hdr.method == IB_MGMT_METHOD_TRAP && slid == 0) {
247 forward_trap(to_mdev(ibdev), port_num, in_mad);
248 return IB_MAD_RESULT_SUCCESS | IB_MAD_RESULT_CONSUMED;
249 }
250
251 if (in_mad->mad_hdr.mgmt_class == IB_MGMT_CLASS_SUBN_LID_ROUTED ||
252 in_mad->mad_hdr.mgmt_class == IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE) {
253 if (in_mad->mad_hdr.method != IB_MGMT_METHOD_GET &&
254 in_mad->mad_hdr.method != IB_MGMT_METHOD_SET &&
255 in_mad->mad_hdr.method != IB_MGMT_METHOD_TRAP_REPRESS)
256 return IB_MAD_RESULT_SUCCESS;
257
258 /*
259 * Don't process SMInfo queries or vendor-specific
260 * MADs -- the SMA can't handle them.
261 */
262 if (in_mad->mad_hdr.attr_id == IB_SMP_ATTR_SM_INFO ||
263 ((in_mad->mad_hdr.attr_id & IB_SMP_ATTR_VENDOR_MASK) ==
264 IB_SMP_ATTR_VENDOR_MASK))
265 return IB_MAD_RESULT_SUCCESS;
266 } else if (in_mad->mad_hdr.mgmt_class == IB_MGMT_CLASS_PERF_MGMT ||
267 in_mad->mad_hdr.mgmt_class == MLX4_IB_VENDOR_CLASS1 ||
Eli Cohen6578cf32008-07-14 23:48:45 -0700268 in_mad->mad_hdr.mgmt_class == MLX4_IB_VENDOR_CLASS2 ||
269 in_mad->mad_hdr.mgmt_class == IB_MGMT_CLASS_CONG_MGMT) {
Roland Dreier225c7b12007-05-08 18:00:38 -0700270 if (in_mad->mad_hdr.method != IB_MGMT_METHOD_GET &&
271 in_mad->mad_hdr.method != IB_MGMT_METHOD_SET)
272 return IB_MAD_RESULT_SUCCESS;
273 } else
274 return IB_MAD_RESULT_SUCCESS;
275
Moni Shouaf0f6f342009-01-28 14:54:35 -0800276 if ((in_mad->mad_hdr.mgmt_class == IB_MGMT_CLASS_SUBN_LID_ROUTED ||
277 in_mad->mad_hdr.mgmt_class == IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE) &&
278 in_mad->mad_hdr.method == IB_MGMT_METHOD_SET &&
279 in_mad->mad_hdr.attr_id == IB_SMP_ATTR_PORT_INFO &&
280 !ib_query_port(ibdev, port_num, &pattr))
281 prev_lid = pattr.lid;
282
Roland Dreier225c7b12007-05-08 18:00:38 -0700283 err = mlx4_MAD_IFC(to_mdev(ibdev),
284 mad_flags & IB_MAD_IGNORE_MKEY,
285 mad_flags & IB_MAD_IGNORE_BKEY,
286 port_num, in_wc, in_grh, in_mad, out_mad);
287 if (err)
288 return IB_MAD_RESULT_FAILURE;
289
290 if (!out_mad->mad_hdr.status) {
Moni Shouaf0f6f342009-01-28 14:54:35 -0800291 smp_snoop(ibdev, port_num, in_mad, prev_lid);
Roland Dreier225c7b12007-05-08 18:00:38 -0700292 node_desc_override(ibdev, out_mad);
293 }
294
295 /* set return bit in status of directed route responses */
296 if (in_mad->mad_hdr.mgmt_class == IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE)
297 out_mad->mad_hdr.status |= cpu_to_be16(1 << 15);
298
299 if (in_mad->mad_hdr.method == IB_MGMT_METHOD_TRAP_REPRESS)
300 /* no response for trap repress */
301 return IB_MAD_RESULT_SUCCESS | IB_MAD_RESULT_CONSUMED;
302
303 return IB_MAD_RESULT_SUCCESS | IB_MAD_RESULT_REPLY;
304}
305
Or Gerlitzc3779132011-06-15 14:51:27 +0000306static void edit_counter(struct mlx4_counter *cnt,
307 struct ib_pma_portcounters *pma_cnt)
308{
309 pma_cnt->port_xmit_data = cpu_to_be32((be64_to_cpu(cnt->tx_bytes)>>2));
310 pma_cnt->port_rcv_data = cpu_to_be32((be64_to_cpu(cnt->rx_bytes)>>2));
311 pma_cnt->port_xmit_packets = cpu_to_be32(be64_to_cpu(cnt->tx_frames));
312 pma_cnt->port_rcv_packets = cpu_to_be32(be64_to_cpu(cnt->rx_frames));
313}
314
315static int iboe_process_mad(struct ib_device *ibdev, int mad_flags, u8 port_num,
316 struct ib_wc *in_wc, struct ib_grh *in_grh,
317 struct ib_mad *in_mad, struct ib_mad *out_mad)
318{
319 struct mlx4_cmd_mailbox *mailbox;
320 struct mlx4_ib_dev *dev = to_mdev(ibdev);
321 int err;
322 u32 inmod = dev->counters[port_num - 1] & 0xffff;
323 u8 mode;
324
325 if (in_mad->mad_hdr.mgmt_class != IB_MGMT_CLASS_PERF_MGMT)
326 return -EINVAL;
327
328 mailbox = mlx4_alloc_cmd_mailbox(dev->dev);
329 if (IS_ERR(mailbox))
330 return IB_MAD_RESULT_FAILURE;
331
332 err = mlx4_cmd_box(dev->dev, 0, mailbox->dma, inmod, 0,
333 MLX4_CMD_QUERY_IF_STAT, MLX4_CMD_TIME_CLASS_C);
334 if (err)
335 err = IB_MAD_RESULT_FAILURE;
336 else {
337 memset(out_mad->data, 0, sizeof out_mad->data);
338 mode = ((struct mlx4_counter *)mailbox->buf)->counter_mode;
339 switch (mode & 0xf) {
340 case 0:
341 edit_counter(mailbox->buf,
342 (void *)(out_mad->data + 40));
343 err = IB_MAD_RESULT_SUCCESS | IB_MAD_RESULT_REPLY;
344 break;
345 default:
346 err = IB_MAD_RESULT_FAILURE;
347 }
348 }
349
350 mlx4_free_cmd_mailbox(dev->dev, mailbox);
351
352 return err;
353}
354
355int mlx4_ib_process_mad(struct ib_device *ibdev, int mad_flags, u8 port_num,
356 struct ib_wc *in_wc, struct ib_grh *in_grh,
357 struct ib_mad *in_mad, struct ib_mad *out_mad)
358{
359 switch (rdma_port_get_link_layer(ibdev, port_num)) {
360 case IB_LINK_LAYER_INFINIBAND:
361 return ib_process_mad(ibdev, mad_flags, port_num, in_wc,
362 in_grh, in_mad, out_mad);
363 case IB_LINK_LAYER_ETHERNET:
364 return iboe_process_mad(ibdev, mad_flags, port_num, in_wc,
365 in_grh, in_mad, out_mad);
366 default:
367 return -EINVAL;
368 }
369}
370
Roland Dreier225c7b12007-05-08 18:00:38 -0700371static void send_handler(struct ib_mad_agent *agent,
372 struct ib_mad_send_wc *mad_send_wc)
373{
374 ib_free_send_mad(mad_send_wc->send_buf);
375}
376
377int mlx4_ib_mad_init(struct mlx4_ib_dev *dev)
378{
379 struct ib_mad_agent *agent;
380 int p, q;
381 int ret;
Eli Cohenfa417f72010-10-24 21:08:52 -0700382 enum rdma_link_layer ll;
Roland Dreier225c7b12007-05-08 18:00:38 -0700383
Eli Cohenfa417f72010-10-24 21:08:52 -0700384 for (p = 0; p < dev->num_ports; ++p) {
385 ll = rdma_port_get_link_layer(&dev->ib_dev, p + 1);
Roland Dreier225c7b12007-05-08 18:00:38 -0700386 for (q = 0; q <= 1; ++q) {
Eli Cohenfa417f72010-10-24 21:08:52 -0700387 if (ll == IB_LINK_LAYER_INFINIBAND) {
388 agent = ib_register_mad_agent(&dev->ib_dev, p + 1,
389 q ? IB_QPT_GSI : IB_QPT_SMI,
390 NULL, 0, send_handler,
391 NULL, NULL);
392 if (IS_ERR(agent)) {
393 ret = PTR_ERR(agent);
394 goto err;
395 }
396 dev->send_agent[p][q] = agent;
397 } else
398 dev->send_agent[p][q] = NULL;
Roland Dreier225c7b12007-05-08 18:00:38 -0700399 }
Eli Cohenfa417f72010-10-24 21:08:52 -0700400 }
Roland Dreier225c7b12007-05-08 18:00:38 -0700401
402 return 0;
403
404err:
Yevgeny Petrilin7ff93f82008-10-22 15:38:42 -0700405 for (p = 0; p < dev->num_ports; ++p)
Roland Dreier225c7b12007-05-08 18:00:38 -0700406 for (q = 0; q <= 1; ++q)
407 if (dev->send_agent[p][q])
408 ib_unregister_mad_agent(dev->send_agent[p][q]);
409
410 return ret;
411}
412
413void mlx4_ib_mad_cleanup(struct mlx4_ib_dev *dev)
414{
415 struct ib_mad_agent *agent;
416 int p, q;
417
Yevgeny Petrilin7ff93f82008-10-22 15:38:42 -0700418 for (p = 0; p < dev->num_ports; ++p) {
Roland Dreier225c7b12007-05-08 18:00:38 -0700419 for (q = 0; q <= 1; ++q) {
420 agent = dev->send_agent[p][q];
Eli Cohenfa417f72010-10-24 21:08:52 -0700421 if (agent) {
422 dev->send_agent[p][q] = NULL;
423 ib_unregister_mad_agent(agent);
424 }
Roland Dreier225c7b12007-05-08 18:00:38 -0700425 }
426
427 if (dev->sm_ah[p])
428 ib_destroy_ah(dev->sm_ah[p]);
429 }
430}