mlx4: Use port management change event instead of smp_snoop
[linux-2.6-block.git] / drivers / infiniband / hw / mlx4 / mad.c
CommitLineData
225c7b1f
RD
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>
5a0e3ad6 37#include <linux/gfp.h>
c3779134 38#include <rdma/ib_pma.h>
225c7b1f
RD
39
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,
f9baff50
JM
112 MLX4_CMD_MAD_IFC, MLX4_CMD_TIME_CLASS_C,
113 MLX4_CMD_NATIVE);
225c7b1f 114
fe11cb6b 115 if (!err)
225c7b1f
RD
116 memcpy(response_mad, outmailbox->buf, 256);
117
118 mlx4_free_cmd_mailbox(dev->dev, inmailbox);
119 mlx4_free_cmd_mailbox(dev->dev, outmailbox);
120
121 return err;
122}
123
124static void update_sm_ah(struct mlx4_ib_dev *dev, u8 port_num, u16 lid, u8 sl)
125{
126 struct ib_ah *new_ah;
127 struct ib_ah_attr ah_attr;
128
129 if (!dev->send_agent[port_num - 1][0])
130 return;
131
132 memset(&ah_attr, 0, sizeof ah_attr);
133 ah_attr.dlid = lid;
134 ah_attr.sl = sl;
135 ah_attr.port_num = port_num;
136
137 new_ah = ib_create_ah(dev->send_agent[port_num - 1][0]->qp->pd,
138 &ah_attr);
139 if (IS_ERR(new_ah))
140 return;
141
142 spin_lock(&dev->sm_lock);
143 if (dev->sm_ah[port_num - 1])
144 ib_destroy_ah(dev->sm_ah[port_num - 1]);
145 dev->sm_ah[port_num - 1] = new_ah;
146 spin_unlock(&dev->sm_lock);
147}
148
149/*
00f5ce99
JM
150 * Snoop SM MADs for port info, GUID info, and P_Key table sets, so we can
151 * synthesize LID change, Client-Rereg, GID change, and P_Key change events.
225c7b1f 152 */
f0f6f346 153static void smp_snoop(struct ib_device *ibdev, u8 port_num, struct ib_mad *mad,
00f5ce99 154 u16 prev_lid)
225c7b1f 155{
00f5ce99
JM
156 struct ib_port_info *pinfo;
157 u16 lid;
225c7b1f 158
00f5ce99 159 struct mlx4_ib_dev *dev = to_mdev(ibdev);
225c7b1f
RD
160 if ((mad->mad_hdr.mgmt_class == IB_MGMT_CLASS_SUBN_LID_ROUTED ||
161 mad->mad_hdr.mgmt_class == IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE) &&
00f5ce99
JM
162 mad->mad_hdr.method == IB_MGMT_METHOD_SET)
163 switch (mad->mad_hdr.attr_id) {
164 case IB_SMP_ATTR_PORT_INFO:
165 pinfo = (struct ib_port_info *) ((struct ib_smp *) mad)->data;
166 lid = be16_to_cpu(pinfo->lid);
225c7b1f 167
00f5ce99 168 update_sm_ah(dev, port_num,
225c7b1f
RD
169 be16_to_cpu(pinfo->sm_lid),
170 pinfo->neighbormtu_mastersmsl & 0xf);
171
00f5ce99
JM
172 if (pinfo->clientrereg_resv_subnetto & 0x80)
173 mlx4_ib_dispatch_event(dev, port_num,
174 IB_EVENT_CLIENT_REREGISTER);
225c7b1f 175
00f5ce99
JM
176 if (prev_lid != lid)
177 mlx4_ib_dispatch_event(dev, port_num,
178 IB_EVENT_LID_CHANGE);
179 break;
225c7b1f 180
00f5ce99
JM
181 case IB_SMP_ATTR_PKEY_TABLE:
182 mlx4_ib_dispatch_event(dev, port_num,
183 IB_EVENT_PKEY_CHANGE);
184 break;
225c7b1f 185
00f5ce99
JM
186 case IB_SMP_ATTR_GUID_INFO:
187 mlx4_ib_dispatch_event(dev, port_num,
188 IB_EVENT_GID_CHANGE);
189 break;
190 default:
191 break;
225c7b1f 192 }
225c7b1f
RD
193}
194
195static void node_desc_override(struct ib_device *dev,
196 struct ib_mad *mad)
197{
198 if ((mad->mad_hdr.mgmt_class == IB_MGMT_CLASS_SUBN_LID_ROUTED ||
199 mad->mad_hdr.mgmt_class == IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE) &&
200 mad->mad_hdr.method == IB_MGMT_METHOD_GET_RESP &&
201 mad->mad_hdr.attr_id == IB_SMP_ATTR_NODE_DESC) {
202 spin_lock(&to_mdev(dev)->sm_lock);
203 memcpy(((struct ib_smp *) mad)->data, dev->node_desc, 64);
204 spin_unlock(&to_mdev(dev)->sm_lock);
205 }
206}
207
208static void forward_trap(struct mlx4_ib_dev *dev, u8 port_num, struct ib_mad *mad)
209{
210 int qpn = mad->mad_hdr.mgmt_class != IB_MGMT_CLASS_SUBN_LID_ROUTED;
211 struct ib_mad_send_buf *send_buf;
212 struct ib_mad_agent *agent = dev->send_agent[port_num - 1][qpn];
213 int ret;
214
215 if (agent) {
216 send_buf = ib_create_send_mad(agent, qpn, 0, 0, IB_MGMT_MAD_HDR,
217 IB_MGMT_MAD_DATA, GFP_ATOMIC);
13974909
DC
218 if (IS_ERR(send_buf))
219 return;
225c7b1f
RD
220 /*
221 * We rely here on the fact that MLX QPs don't use the
222 * address handle after the send is posted (this is
223 * wrong following the IB spec strictly, but we know
224 * it's OK for our devices).
225 */
226 spin_lock(&dev->sm_lock);
227 memcpy(send_buf->mad, mad, sizeof *mad);
228 if ((send_buf->ah = dev->sm_ah[port_num - 1]))
229 ret = ib_post_send_mad(send_buf, NULL);
230 else
231 ret = -EINVAL;
232 spin_unlock(&dev->sm_lock);
233
234 if (ret)
235 ib_free_send_mad(send_buf);
236 }
237}
238
c3779134 239static int ib_process_mad(struct ib_device *ibdev, int mad_flags, u8 port_num,
225c7b1f
RD
240 struct ib_wc *in_wc, struct ib_grh *in_grh,
241 struct ib_mad *in_mad, struct ib_mad *out_mad)
242{
f0f6f346 243 u16 slid, prev_lid = 0;
225c7b1f 244 int err;
f0f6f346 245 struct ib_port_attr pattr;
225c7b1f 246
b1d8eb5a
JM
247 if (in_wc && in_wc->qp->qp_num) {
248 pr_debug("received MAD: slid:%d sqpn:%d "
249 "dlid_bits:%d dqpn:%d wc_flags:0x%x, cls %x, mtd %x, atr %x\n",
250 in_wc->slid, in_wc->src_qp,
251 in_wc->dlid_path_bits,
252 in_wc->qp->qp_num,
253 in_wc->wc_flags,
254 in_mad->mad_hdr.mgmt_class, in_mad->mad_hdr.method,
255 be16_to_cpu(in_mad->mad_hdr.attr_id));
256 if (in_wc->wc_flags & IB_WC_GRH) {
257 pr_debug("sgid_hi:0x%016llx sgid_lo:0x%016llx\n",
258 be64_to_cpu(in_grh->sgid.global.subnet_prefix),
259 be64_to_cpu(in_grh->sgid.global.interface_id));
260 pr_debug("dgid_hi:0x%016llx dgid_lo:0x%016llx\n",
261 be64_to_cpu(in_grh->dgid.global.subnet_prefix),
262 be64_to_cpu(in_grh->dgid.global.interface_id));
263 }
264 }
265
225c7b1f
RD
266 slid = in_wc ? in_wc->slid : be16_to_cpu(IB_LID_PERMISSIVE);
267
268 if (in_mad->mad_hdr.method == IB_MGMT_METHOD_TRAP && slid == 0) {
269 forward_trap(to_mdev(ibdev), port_num, in_mad);
270 return IB_MAD_RESULT_SUCCESS | IB_MAD_RESULT_CONSUMED;
271 }
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 if (in_mad->mad_hdr.method != IB_MGMT_METHOD_GET &&
276 in_mad->mad_hdr.method != IB_MGMT_METHOD_SET &&
277 in_mad->mad_hdr.method != IB_MGMT_METHOD_TRAP_REPRESS)
278 return IB_MAD_RESULT_SUCCESS;
279
280 /*
a6f7feae 281 * Don't process SMInfo queries -- the SMA can't handle them.
225c7b1f 282 */
a6f7feae 283 if (in_mad->mad_hdr.attr_id == IB_SMP_ATTR_SM_INFO)
225c7b1f
RD
284 return IB_MAD_RESULT_SUCCESS;
285 } else if (in_mad->mad_hdr.mgmt_class == IB_MGMT_CLASS_PERF_MGMT ||
286 in_mad->mad_hdr.mgmt_class == MLX4_IB_VENDOR_CLASS1 ||
6578cf33
EC
287 in_mad->mad_hdr.mgmt_class == MLX4_IB_VENDOR_CLASS2 ||
288 in_mad->mad_hdr.mgmt_class == IB_MGMT_CLASS_CONG_MGMT) {
225c7b1f
RD
289 if (in_mad->mad_hdr.method != IB_MGMT_METHOD_GET &&
290 in_mad->mad_hdr.method != IB_MGMT_METHOD_SET)
291 return IB_MAD_RESULT_SUCCESS;
292 } else
293 return IB_MAD_RESULT_SUCCESS;
294
f0f6f346
MS
295 if ((in_mad->mad_hdr.mgmt_class == IB_MGMT_CLASS_SUBN_LID_ROUTED ||
296 in_mad->mad_hdr.mgmt_class == IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE) &&
297 in_mad->mad_hdr.method == IB_MGMT_METHOD_SET &&
298 in_mad->mad_hdr.attr_id == IB_SMP_ATTR_PORT_INFO &&
299 !ib_query_port(ibdev, port_num, &pattr))
300 prev_lid = pattr.lid;
301
225c7b1f
RD
302 err = mlx4_MAD_IFC(to_mdev(ibdev),
303 mad_flags & IB_MAD_IGNORE_MKEY,
304 mad_flags & IB_MAD_IGNORE_BKEY,
305 port_num, in_wc, in_grh, in_mad, out_mad);
306 if (err)
307 return IB_MAD_RESULT_FAILURE;
308
309 if (!out_mad->mad_hdr.status) {
00f5ce99
JM
310 if (!(to_mdev(ibdev)->dev->caps.flags & MLX4_DEV_CAP_FLAG_PORT_MNG_CHG_EV))
311 smp_snoop(ibdev, port_num, in_mad, prev_lid);
225c7b1f
RD
312 node_desc_override(ibdev, out_mad);
313 }
314
315 /* set return bit in status of directed route responses */
316 if (in_mad->mad_hdr.mgmt_class == IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE)
317 out_mad->mad_hdr.status |= cpu_to_be16(1 << 15);
318
319 if (in_mad->mad_hdr.method == IB_MGMT_METHOD_TRAP_REPRESS)
320 /* no response for trap repress */
321 return IB_MAD_RESULT_SUCCESS | IB_MAD_RESULT_CONSUMED;
322
323 return IB_MAD_RESULT_SUCCESS | IB_MAD_RESULT_REPLY;
324}
325
c3779134
OG
326static void edit_counter(struct mlx4_counter *cnt,
327 struct ib_pma_portcounters *pma_cnt)
328{
329 pma_cnt->port_xmit_data = cpu_to_be32((be64_to_cpu(cnt->tx_bytes)>>2));
330 pma_cnt->port_rcv_data = cpu_to_be32((be64_to_cpu(cnt->rx_bytes)>>2));
331 pma_cnt->port_xmit_packets = cpu_to_be32(be64_to_cpu(cnt->tx_frames));
332 pma_cnt->port_rcv_packets = cpu_to_be32(be64_to_cpu(cnt->rx_frames));
333}
334
335static int iboe_process_mad(struct ib_device *ibdev, int mad_flags, u8 port_num,
336 struct ib_wc *in_wc, struct ib_grh *in_grh,
337 struct ib_mad *in_mad, struct ib_mad *out_mad)
338{
339 struct mlx4_cmd_mailbox *mailbox;
340 struct mlx4_ib_dev *dev = to_mdev(ibdev);
341 int err;
342 u32 inmod = dev->counters[port_num - 1] & 0xffff;
343 u8 mode;
344
345 if (in_mad->mad_hdr.mgmt_class != IB_MGMT_CLASS_PERF_MGMT)
346 return -EINVAL;
347
348 mailbox = mlx4_alloc_cmd_mailbox(dev->dev);
349 if (IS_ERR(mailbox))
350 return IB_MAD_RESULT_FAILURE;
351
352 err = mlx4_cmd_box(dev->dev, 0, mailbox->dma, inmod, 0,
f9baff50
JM
353 MLX4_CMD_QUERY_IF_STAT, MLX4_CMD_TIME_CLASS_C,
354 MLX4_CMD_WRAPPED);
c3779134
OG
355 if (err)
356 err = IB_MAD_RESULT_FAILURE;
357 else {
358 memset(out_mad->data, 0, sizeof out_mad->data);
359 mode = ((struct mlx4_counter *)mailbox->buf)->counter_mode;
360 switch (mode & 0xf) {
361 case 0:
362 edit_counter(mailbox->buf,
363 (void *)(out_mad->data + 40));
364 err = IB_MAD_RESULT_SUCCESS | IB_MAD_RESULT_REPLY;
365 break;
366 default:
367 err = IB_MAD_RESULT_FAILURE;
368 }
369 }
370
371 mlx4_free_cmd_mailbox(dev->dev, mailbox);
372
373 return err;
374}
375
376int mlx4_ib_process_mad(struct ib_device *ibdev, int mad_flags, u8 port_num,
377 struct ib_wc *in_wc, struct ib_grh *in_grh,
378 struct ib_mad *in_mad, struct ib_mad *out_mad)
379{
380 switch (rdma_port_get_link_layer(ibdev, port_num)) {
381 case IB_LINK_LAYER_INFINIBAND:
382 return ib_process_mad(ibdev, mad_flags, port_num, in_wc,
383 in_grh, in_mad, out_mad);
384 case IB_LINK_LAYER_ETHERNET:
385 return iboe_process_mad(ibdev, mad_flags, port_num, in_wc,
386 in_grh, in_mad, out_mad);
387 default:
388 return -EINVAL;
389 }
390}
391
225c7b1f
RD
392static void send_handler(struct ib_mad_agent *agent,
393 struct ib_mad_send_wc *mad_send_wc)
394{
395 ib_free_send_mad(mad_send_wc->send_buf);
396}
397
398int mlx4_ib_mad_init(struct mlx4_ib_dev *dev)
399{
400 struct ib_mad_agent *agent;
401 int p, q;
402 int ret;
fa417f7b 403 enum rdma_link_layer ll;
225c7b1f 404
fa417f7b
EC
405 for (p = 0; p < dev->num_ports; ++p) {
406 ll = rdma_port_get_link_layer(&dev->ib_dev, p + 1);
225c7b1f 407 for (q = 0; q <= 1; ++q) {
fa417f7b
EC
408 if (ll == IB_LINK_LAYER_INFINIBAND) {
409 agent = ib_register_mad_agent(&dev->ib_dev, p + 1,
410 q ? IB_QPT_GSI : IB_QPT_SMI,
411 NULL, 0, send_handler,
412 NULL, NULL);
413 if (IS_ERR(agent)) {
414 ret = PTR_ERR(agent);
415 goto err;
416 }
417 dev->send_agent[p][q] = agent;
418 } else
419 dev->send_agent[p][q] = NULL;
225c7b1f 420 }
fa417f7b 421 }
225c7b1f
RD
422
423 return 0;
424
425err:
7ff93f8b 426 for (p = 0; p < dev->num_ports; ++p)
225c7b1f
RD
427 for (q = 0; q <= 1; ++q)
428 if (dev->send_agent[p][q])
429 ib_unregister_mad_agent(dev->send_agent[p][q]);
430
431 return ret;
432}
433
434void mlx4_ib_mad_cleanup(struct mlx4_ib_dev *dev)
435{
436 struct ib_mad_agent *agent;
437 int p, q;
438
7ff93f8b 439 for (p = 0; p < dev->num_ports; ++p) {
225c7b1f
RD
440 for (q = 0; q <= 1; ++q) {
441 agent = dev->send_agent[p][q];
fa417f7b
EC
442 if (agent) {
443 dev->send_agent[p][q] = NULL;
444 ib_unregister_mad_agent(agent);
445 }
225c7b1f
RD
446 }
447
448 if (dev->sm_ah[p])
449 ib_destroy_ah(dev->sm_ah[p]);
450 }
451}
00f5ce99
JM
452
453void handle_port_mgmt_change_event(struct work_struct *work)
454{
455 struct ib_event_work *ew = container_of(work, struct ib_event_work, work);
456 struct mlx4_ib_dev *dev = ew->ib_dev;
457 struct mlx4_eqe *eqe = &(ew->ib_eqe);
458 u8 port = eqe->event.port_mgmt_change.port;
459 u32 changed_attr;
460
461 switch (eqe->subtype) {
462 case MLX4_DEV_PMC_SUBTYPE_PORT_INFO:
463 changed_attr = be32_to_cpu(eqe->event.port_mgmt_change.params.port_info.changed_attr);
464
465 /* Update the SM ah - This should be done before handling
466 the other changed attributes so that MADs can be sent to the SM */
467 if (changed_attr & MSTR_SM_CHANGE_MASK) {
468 u16 lid = be16_to_cpu(eqe->event.port_mgmt_change.params.port_info.mstr_sm_lid);
469 u8 sl = eqe->event.port_mgmt_change.params.port_info.mstr_sm_sl & 0xf;
470 update_sm_ah(dev, port, lid, sl);
471 }
472
473 /* Check if it is a lid change event */
474 if (changed_attr & MLX4_EQ_PORT_INFO_LID_CHANGE_MASK)
475 mlx4_ib_dispatch_event(dev, port, IB_EVENT_LID_CHANGE);
476
477 /* Generate GUID changed event */
478 if (changed_attr & MLX4_EQ_PORT_INFO_GID_PFX_CHANGE_MASK)
479 mlx4_ib_dispatch_event(dev, port, IB_EVENT_GID_CHANGE);
480
481 if (changed_attr & MLX4_EQ_PORT_INFO_CLIENT_REREG_MASK)
482 mlx4_ib_dispatch_event(dev, port,
483 IB_EVENT_CLIENT_REREGISTER);
484 break;
485
486 case MLX4_DEV_PMC_SUBTYPE_PKEY_TABLE:
487 mlx4_ib_dispatch_event(dev, port, IB_EVENT_PKEY_CHANGE);
488 break;
489 case MLX4_DEV_PMC_SUBTYPE_GUID_INFO:
490 mlx4_ib_dispatch_event(dev, port, IB_EVENT_GID_CHANGE);
491 break;
492 default:
493 pr_warn("Unsupported subtype 0x%x for "
494 "Port Management Change event\n", eqe->subtype);
495 }
496
497 kfree(ew);
498}
499
500void mlx4_ib_dispatch_event(struct mlx4_ib_dev *dev, u8 port_num,
501 enum ib_event_type type)
502{
503 struct ib_event event;
504
505 event.device = &dev->ib_dev;
506 event.element.port_num = port_num;
507 event.event = type;
508
509 ib_dispatch_event(&event);
510}