IB/mlx4: Properly initialize GRH TClass and FlowLabel in AHs
[pandora-kernel.git] / drivers / infiniband / hw / mlx4 / mad.c
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 #include <rdma/ib_pma.h>
39
40 #include "mlx4_ib.h"
41
42 enum {
43         MLX4_IB_VENDOR_CLASS1 = 0x9,
44         MLX4_IB_VENDOR_CLASS2 = 0xa
45 };
46
47 /* Counters should be saturate once they reach their maximum value */
48 #define ASSIGN_32BIT_COUNTER(counter, value) do {\
49         if ((value) > (u32)~0U)                  \
50                 counter = cpu_to_be32((u32)~0U); \
51         else                                     \
52                 counter = cpu_to_be32(value);    \
53 } while (0)
54
55 int mlx4_MAD_IFC(struct mlx4_ib_dev *dev, int ignore_mkey, int ignore_bkey,
56                  int port, struct ib_wc *in_wc, struct ib_grh *in_grh,
57                  void *in_mad, void *response_mad)
58 {
59         struct mlx4_cmd_mailbox *inmailbox, *outmailbox;
60         void *inbox;
61         int err;
62         u32 in_modifier = port;
63         u8 op_modifier = 0;
64
65         inmailbox = mlx4_alloc_cmd_mailbox(dev->dev);
66         if (IS_ERR(inmailbox))
67                 return PTR_ERR(inmailbox);
68         inbox = inmailbox->buf;
69
70         outmailbox = mlx4_alloc_cmd_mailbox(dev->dev);
71         if (IS_ERR(outmailbox)) {
72                 mlx4_free_cmd_mailbox(dev->dev, inmailbox);
73                 return PTR_ERR(outmailbox);
74         }
75
76         memcpy(inbox, in_mad, 256);
77
78         /*
79          * Key check traps can't be generated unless we have in_wc to
80          * tell us where to send the trap.
81          */
82         if (ignore_mkey || !in_wc)
83                 op_modifier |= 0x1;
84         if (ignore_bkey || !in_wc)
85                 op_modifier |= 0x2;
86
87         if (in_wc) {
88                 struct {
89                         __be32          my_qpn;
90                         u32             reserved1;
91                         __be32          rqpn;
92                         u8              sl;
93                         u8              g_path;
94                         u16             reserved2[2];
95                         __be16          pkey;
96                         u32             reserved3[11];
97                         u8              grh[40];
98                 } *ext_info;
99
100                 memset(inbox + 256, 0, 256);
101                 ext_info = inbox + 256;
102
103                 ext_info->my_qpn = cpu_to_be32(in_wc->qp->qp_num);
104                 ext_info->rqpn   = cpu_to_be32(in_wc->src_qp);
105                 ext_info->sl     = in_wc->sl << 4;
106                 ext_info->g_path = in_wc->dlid_path_bits |
107                         (in_wc->wc_flags & IB_WC_GRH ? 0x80 : 0);
108                 ext_info->pkey   = cpu_to_be16(in_wc->pkey_index);
109
110                 if (in_grh)
111                         memcpy(ext_info->grh, in_grh, 40);
112
113                 op_modifier |= 0x4;
114
115                 in_modifier |= in_wc->slid << 16;
116         }
117
118         err = mlx4_cmd_box(dev->dev, inmailbox->dma, outmailbox->dma,
119                            in_modifier, op_modifier,
120                            MLX4_CMD_MAD_IFC, MLX4_CMD_TIME_CLASS_C);
121
122         if (!err)
123                 memcpy(response_mad, outmailbox->buf, 256);
124
125         mlx4_free_cmd_mailbox(dev->dev, inmailbox);
126         mlx4_free_cmd_mailbox(dev->dev, outmailbox);
127
128         return err;
129 }
130
131 static void update_sm_ah(struct mlx4_ib_dev *dev, u8 port_num, u16 lid, u8 sl)
132 {
133         struct ib_ah *new_ah;
134         struct ib_ah_attr ah_attr;
135
136         if (!dev->send_agent[port_num - 1][0])
137                 return;
138
139         memset(&ah_attr, 0, sizeof ah_attr);
140         ah_attr.dlid     = lid;
141         ah_attr.sl       = sl;
142         ah_attr.port_num = port_num;
143
144         new_ah = ib_create_ah(dev->send_agent[port_num - 1][0]->qp->pd,
145                               &ah_attr);
146         if (IS_ERR(new_ah))
147                 return;
148
149         spin_lock(&dev->sm_lock);
150         if (dev->sm_ah[port_num - 1])
151                 ib_destroy_ah(dev->sm_ah[port_num - 1]);
152         dev->sm_ah[port_num - 1] = new_ah;
153         spin_unlock(&dev->sm_lock);
154 }
155
156 /*
157  * Snoop SM MADs for port info and P_Key table sets, so we can
158  * synthesize LID change and P_Key change events.
159  */
160 static void smp_snoop(struct ib_device *ibdev, u8 port_num, struct ib_mad *mad,
161                                 u16 prev_lid)
162 {
163         struct ib_event event;
164
165         if ((mad->mad_hdr.mgmt_class == IB_MGMT_CLASS_SUBN_LID_ROUTED ||
166              mad->mad_hdr.mgmt_class == IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE) &&
167             mad->mad_hdr.method == IB_MGMT_METHOD_SET) {
168                 if (mad->mad_hdr.attr_id == IB_SMP_ATTR_PORT_INFO) {
169                         struct ib_port_info *pinfo =
170                                 (struct ib_port_info *) ((struct ib_smp *) mad)->data;
171                         u16 lid = be16_to_cpu(pinfo->lid);
172
173                         update_sm_ah(to_mdev(ibdev), port_num,
174                                      be16_to_cpu(pinfo->sm_lid),
175                                      pinfo->neighbormtu_mastersmsl & 0xf);
176
177                         event.device           = ibdev;
178                         event.element.port_num = port_num;
179
180                         if (pinfo->clientrereg_resv_subnetto & 0x80) {
181                                 event.event    = IB_EVENT_CLIENT_REREGISTER;
182                                 ib_dispatch_event(&event);
183                         }
184
185                         if (prev_lid != lid) {
186                                 event.event    = IB_EVENT_LID_CHANGE;
187                                 ib_dispatch_event(&event);
188                         }
189                 }
190
191                 if (mad->mad_hdr.attr_id == IB_SMP_ATTR_PKEY_TABLE) {
192                         event.device           = ibdev;
193                         event.event            = IB_EVENT_PKEY_CHANGE;
194                         event.element.port_num = port_num;
195                         ib_dispatch_event(&event);
196                 }
197         }
198 }
199
200 static void node_desc_override(struct ib_device *dev,
201                                struct ib_mad *mad)
202 {
203         if ((mad->mad_hdr.mgmt_class == IB_MGMT_CLASS_SUBN_LID_ROUTED ||
204              mad->mad_hdr.mgmt_class == IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE) &&
205             mad->mad_hdr.method == IB_MGMT_METHOD_GET_RESP &&
206             mad->mad_hdr.attr_id == IB_SMP_ATTR_NODE_DESC) {
207                 spin_lock(&to_mdev(dev)->sm_lock);
208                 memcpy(((struct ib_smp *) mad)->data, dev->node_desc, 64);
209                 spin_unlock(&to_mdev(dev)->sm_lock);
210         }
211 }
212
213 static void forward_trap(struct mlx4_ib_dev *dev, u8 port_num, struct ib_mad *mad)
214 {
215         int qpn = mad->mad_hdr.mgmt_class != IB_MGMT_CLASS_SUBN_LID_ROUTED;
216         struct ib_mad_send_buf *send_buf;
217         struct ib_mad_agent *agent = dev->send_agent[port_num - 1][qpn];
218         int ret;
219
220         if (agent) {
221                 send_buf = ib_create_send_mad(agent, qpn, 0, 0, IB_MGMT_MAD_HDR,
222                                               IB_MGMT_MAD_DATA, GFP_ATOMIC);
223                 if (IS_ERR(send_buf))
224                         return;
225                 /*
226                  * We rely here on the fact that MLX QPs don't use the
227                  * address handle after the send is posted (this is
228                  * wrong following the IB spec strictly, but we know
229                  * it's OK for our devices).
230                  */
231                 spin_lock(&dev->sm_lock);
232                 memcpy(send_buf->mad, mad, sizeof *mad);
233                 if ((send_buf->ah = dev->sm_ah[port_num - 1]))
234                         ret = ib_post_send_mad(send_buf, NULL);
235                 else
236                         ret = -EINVAL;
237                 spin_unlock(&dev->sm_lock);
238
239                 if (ret)
240                         ib_free_send_mad(send_buf);
241         }
242 }
243
244 static int ib_process_mad(struct ib_device *ibdev, int mad_flags, u8 port_num,
245                         struct ib_wc *in_wc, struct ib_grh *in_grh,
246                         struct ib_mad *in_mad, struct ib_mad *out_mad)
247 {
248         u16 slid, prev_lid = 0;
249         int err;
250         struct ib_port_attr pattr;
251
252         slid = in_wc ? in_wc->slid : be16_to_cpu(IB_LID_PERMISSIVE);
253
254         if (in_mad->mad_hdr.method == IB_MGMT_METHOD_TRAP && slid == 0) {
255                 forward_trap(to_mdev(ibdev), port_num, in_mad);
256                 return IB_MAD_RESULT_SUCCESS | IB_MAD_RESULT_CONSUMED;
257         }
258
259         if (in_mad->mad_hdr.mgmt_class == IB_MGMT_CLASS_SUBN_LID_ROUTED ||
260             in_mad->mad_hdr.mgmt_class == IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE) {
261                 if (in_mad->mad_hdr.method   != IB_MGMT_METHOD_GET &&
262                     in_mad->mad_hdr.method   != IB_MGMT_METHOD_SET &&
263                     in_mad->mad_hdr.method   != IB_MGMT_METHOD_TRAP_REPRESS)
264                         return IB_MAD_RESULT_SUCCESS;
265
266                 /*
267                  * Don't process SMInfo queries -- the SMA can't handle them.
268                  */
269                 if (in_mad->mad_hdr.attr_id == IB_SMP_ATTR_SM_INFO)
270                         return IB_MAD_RESULT_SUCCESS;
271         } else if (in_mad->mad_hdr.mgmt_class == IB_MGMT_CLASS_PERF_MGMT ||
272                    in_mad->mad_hdr.mgmt_class == MLX4_IB_VENDOR_CLASS1   ||
273                    in_mad->mad_hdr.mgmt_class == MLX4_IB_VENDOR_CLASS2   ||
274                    in_mad->mad_hdr.mgmt_class == IB_MGMT_CLASS_CONG_MGMT) {
275                 if (in_mad->mad_hdr.method  != IB_MGMT_METHOD_GET &&
276                     in_mad->mad_hdr.method  != IB_MGMT_METHOD_SET)
277                         return IB_MAD_RESULT_SUCCESS;
278         } else
279                 return IB_MAD_RESULT_SUCCESS;
280
281         if ((in_mad->mad_hdr.mgmt_class == IB_MGMT_CLASS_SUBN_LID_ROUTED ||
282              in_mad->mad_hdr.mgmt_class == IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE) &&
283             in_mad->mad_hdr.method == IB_MGMT_METHOD_SET &&
284             in_mad->mad_hdr.attr_id == IB_SMP_ATTR_PORT_INFO &&
285             !ib_query_port(ibdev, port_num, &pattr))
286                 prev_lid = pattr.lid;
287
288         err = mlx4_MAD_IFC(to_mdev(ibdev),
289                            mad_flags & IB_MAD_IGNORE_MKEY,
290                            mad_flags & IB_MAD_IGNORE_BKEY,
291                            port_num, in_wc, in_grh, in_mad, out_mad);
292         if (err)
293                 return IB_MAD_RESULT_FAILURE;
294
295         if (!out_mad->mad_hdr.status) {
296                 smp_snoop(ibdev, port_num, in_mad, prev_lid);
297                 node_desc_override(ibdev, out_mad);
298         }
299
300         /* set return bit in status of directed route responses */
301         if (in_mad->mad_hdr.mgmt_class == IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE)
302                 out_mad->mad_hdr.status |= cpu_to_be16(1 << 15);
303
304         if (in_mad->mad_hdr.method == IB_MGMT_METHOD_TRAP_REPRESS)
305                 /* no response for trap repress */
306                 return IB_MAD_RESULT_SUCCESS | IB_MAD_RESULT_CONSUMED;
307
308         return IB_MAD_RESULT_SUCCESS | IB_MAD_RESULT_REPLY;
309 }
310
311 static void edit_counter(struct mlx4_counter *cnt,
312                                         struct ib_pma_portcounters *pma_cnt)
313 {
314         ASSIGN_32BIT_COUNTER(pma_cnt->port_xmit_data,
315                              (be64_to_cpu(cnt->tx_bytes) >> 2));
316         ASSIGN_32BIT_COUNTER(pma_cnt->port_rcv_data,
317                              (be64_to_cpu(cnt->rx_bytes) >> 2));
318         ASSIGN_32BIT_COUNTER(pma_cnt->port_xmit_packets,
319                              be64_to_cpu(cnt->tx_frames));
320         ASSIGN_32BIT_COUNTER(pma_cnt->port_rcv_packets,
321                              be64_to_cpu(cnt->rx_frames));
322 }
323
324 static int iboe_process_mad(struct ib_device *ibdev, int mad_flags, u8 port_num,
325                         struct ib_wc *in_wc, struct ib_grh *in_grh,
326                         struct ib_mad *in_mad, struct ib_mad *out_mad)
327 {
328         struct mlx4_cmd_mailbox *mailbox;
329         struct mlx4_ib_dev *dev = to_mdev(ibdev);
330         int err;
331         u32 inmod = dev->counters[port_num - 1] & 0xffff;
332         u8 mode;
333
334         if (in_mad->mad_hdr.mgmt_class != IB_MGMT_CLASS_PERF_MGMT)
335                 return -EINVAL;
336
337         mailbox = mlx4_alloc_cmd_mailbox(dev->dev);
338         if (IS_ERR(mailbox))
339                 return IB_MAD_RESULT_FAILURE;
340
341         err = mlx4_cmd_box(dev->dev, 0, mailbox->dma, inmod, 0,
342                            MLX4_CMD_QUERY_IF_STAT, MLX4_CMD_TIME_CLASS_C);
343         if (err)
344                 err = IB_MAD_RESULT_FAILURE;
345         else {
346                 memset(out_mad->data, 0, sizeof out_mad->data);
347                 mode = ((struct mlx4_counter *)mailbox->buf)->counter_mode;
348                 switch (mode & 0xf) {
349                 case 0:
350                         edit_counter(mailbox->buf,
351                                                 (void *)(out_mad->data + 40));
352                         err = IB_MAD_RESULT_SUCCESS | IB_MAD_RESULT_REPLY;
353                         break;
354                 default:
355                         err = IB_MAD_RESULT_FAILURE;
356                 }
357         }
358
359         mlx4_free_cmd_mailbox(dev->dev, mailbox);
360
361         return err;
362 }
363
364 int mlx4_ib_process_mad(struct ib_device *ibdev, int mad_flags, u8 port_num,
365                         struct ib_wc *in_wc, struct ib_grh *in_grh,
366                         struct ib_mad *in_mad, struct ib_mad *out_mad)
367 {
368         switch (rdma_port_get_link_layer(ibdev, port_num)) {
369         case IB_LINK_LAYER_INFINIBAND:
370                 return ib_process_mad(ibdev, mad_flags, port_num, in_wc,
371                                       in_grh, in_mad, out_mad);
372         case IB_LINK_LAYER_ETHERNET:
373                 return iboe_process_mad(ibdev, mad_flags, port_num, in_wc,
374                                           in_grh, in_mad, out_mad);
375         default:
376                 return -EINVAL;
377         }
378 }
379
380 static void send_handler(struct ib_mad_agent *agent,
381                          struct ib_mad_send_wc *mad_send_wc)
382 {
383         ib_free_send_mad(mad_send_wc->send_buf);
384 }
385
386 int mlx4_ib_mad_init(struct mlx4_ib_dev *dev)
387 {
388         struct ib_mad_agent *agent;
389         int p, q;
390         int ret;
391         enum rdma_link_layer ll;
392
393         for (p = 0; p < dev->num_ports; ++p) {
394                 ll = rdma_port_get_link_layer(&dev->ib_dev, p + 1);
395                 for (q = 0; q <= 1; ++q) {
396                         if (ll == IB_LINK_LAYER_INFINIBAND) {
397                                 agent = ib_register_mad_agent(&dev->ib_dev, p + 1,
398                                                               q ? IB_QPT_GSI : IB_QPT_SMI,
399                                                               NULL, 0, send_handler,
400                                                               NULL, NULL);
401                                 if (IS_ERR(agent)) {
402                                         ret = PTR_ERR(agent);
403                                         goto err;
404                                 }
405                                 dev->send_agent[p][q] = agent;
406                         } else
407                                 dev->send_agent[p][q] = NULL;
408                 }
409         }
410
411         return 0;
412
413 err:
414         for (p = 0; p < dev->num_ports; ++p)
415                 for (q = 0; q <= 1; ++q)
416                         if (dev->send_agent[p][q])
417                                 ib_unregister_mad_agent(dev->send_agent[p][q]);
418
419         return ret;
420 }
421
422 void mlx4_ib_mad_cleanup(struct mlx4_ib_dev *dev)
423 {
424         struct ib_mad_agent *agent;
425         int p, q;
426
427         for (p = 0; p < dev->num_ports; ++p) {
428                 for (q = 0; q <= 1; ++q) {
429                         agent = dev->send_agent[p][q];
430                         if (agent) {
431                                 dev->send_agent[p][q] = NULL;
432                                 ib_unregister_mad_agent(agent);
433                         }
434                 }
435
436                 if (dev->sm_ah[p])
437                         ib_destroy_ah(dev->sm_ah[p]);
438         }
439 }