Merge HEAD from ../linux-2.6
[pandora-kernel.git] / drivers / infiniband / hw / ipath / ipath_mad.c
1 /*
2  * Copyright (c) 2005, 2006 PathScale, 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_smi.h>
34
35 #include "ipath_kernel.h"
36 #include "ipath_verbs.h"
37 #include "ips_common.h"
38
39 #define IB_SMP_UNSUP_VERSION    __constant_htons(0x0004)
40 #define IB_SMP_UNSUP_METHOD     __constant_htons(0x0008)
41 #define IB_SMP_UNSUP_METH_ATTR  __constant_htons(0x000C)
42 #define IB_SMP_INVALID_FIELD    __constant_htons(0x001C)
43
44 static int reply(struct ib_smp *smp)
45 {
46         /*
47          * The verbs framework will handle the directed/LID route
48          * packet changes.
49          */
50         smp->method = IB_MGMT_METHOD_GET_RESP;
51         if (smp->mgmt_class == IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE)
52                 smp->status |= IB_SMP_DIRECTION;
53         return IB_MAD_RESULT_SUCCESS | IB_MAD_RESULT_REPLY;
54 }
55
56 static int recv_subn_get_nodedescription(struct ib_smp *smp,
57                                          struct ib_device *ibdev)
58 {
59         if (smp->attr_mod)
60                 smp->status |= IB_SMP_INVALID_FIELD;
61
62         strncpy(smp->data, ibdev->node_desc, sizeof(smp->data));
63
64         return reply(smp);
65 }
66
67 struct nodeinfo {
68         u8 base_version;
69         u8 class_version;
70         u8 node_type;
71         u8 num_ports;
72         __be64 sys_guid;
73         __be64 node_guid;
74         __be64 port_guid;
75         __be16 partition_cap;
76         __be16 device_id;
77         __be32 revision;
78         u8 local_port_num;
79         u8 vendor_id[3];
80 } __attribute__ ((packed));
81
82 static int recv_subn_get_nodeinfo(struct ib_smp *smp,
83                                   struct ib_device *ibdev, u8 port)
84 {
85         struct nodeinfo *nip = (struct nodeinfo *)&smp->data;
86         struct ipath_devdata *dd = to_idev(ibdev)->dd;
87         u32 vendor, boardid, majrev, minrev;
88
89         if (smp->attr_mod)
90                 smp->status |= IB_SMP_INVALID_FIELD;
91
92         nip->base_version = 1;
93         nip->class_version = 1;
94         nip->node_type = 1;     /* channel adapter */
95         /*
96          * XXX The num_ports value will need a layer function to get
97          * the value if we ever have more than one IB port on a chip.
98          * We will also need to get the GUID for the port.
99          */
100         nip->num_ports = ibdev->phys_port_cnt;
101         /* This is already in network order */
102         nip->sys_guid = to_idev(ibdev)->sys_image_guid;
103         nip->node_guid = ipath_layer_get_guid(dd);
104         nip->port_guid = nip->sys_guid;
105         nip->partition_cap = cpu_to_be16(ipath_layer_get_npkeys(dd));
106         nip->device_id = cpu_to_be16(ipath_layer_get_deviceid(dd));
107         ipath_layer_query_device(dd, &vendor, &boardid, &majrev, &minrev);
108         nip->revision = cpu_to_be32((majrev << 16) | minrev);
109         nip->local_port_num = port;
110         nip->vendor_id[0] = 0;
111         nip->vendor_id[1] = vendor >> 8;
112         nip->vendor_id[2] = vendor;
113
114         return reply(smp);
115 }
116
117 static int recv_subn_get_guidinfo(struct ib_smp *smp,
118                                   struct ib_device *ibdev)
119 {
120         u32 startgx = 8 * be32_to_cpu(smp->attr_mod);
121         __be64 *p = (__be64 *) smp->data;
122
123         /* 32 blocks of 8 64-bit GUIDs per block */
124
125         memset(smp->data, 0, sizeof(smp->data));
126
127         /*
128          * We only support one GUID for now.  If this changes, the
129          * portinfo.guid_cap field needs to be updated too.
130          */
131         if (startgx == 0)
132                 /* The first is a copy of the read-only HW GUID. */
133                 *p = ipath_layer_get_guid(to_idev(ibdev)->dd);
134         else
135                 smp->status |= IB_SMP_INVALID_FIELD;
136
137         return reply(smp);
138 }
139
140 static int recv_subn_get_portinfo(struct ib_smp *smp,
141                                   struct ib_device *ibdev, u8 port)
142 {
143         struct ipath_ibdev *dev;
144         struct ib_port_info *pip = (struct ib_port_info *)smp->data;
145         u16 lid;
146         u8 ibcstat;
147         u8 mtu;
148         int ret;
149
150         if (be32_to_cpu(smp->attr_mod) > ibdev->phys_port_cnt) {
151                 smp->status |= IB_SMP_INVALID_FIELD;
152                 ret = reply(smp);
153                 goto bail;
154         }
155
156         dev = to_idev(ibdev);
157
158         /* Clear all fields.  Only set the non-zero fields. */
159         memset(smp->data, 0, sizeof(smp->data));
160
161         /* Only return the mkey if the protection field allows it. */
162         if (smp->method == IB_MGMT_METHOD_SET || dev->mkey == smp->mkey ||
163             (dev->mkeyprot_resv_lmc >> 6) == 0)
164                 pip->mkey = dev->mkey;
165         pip->gid_prefix = dev->gid_prefix;
166         lid = ipath_layer_get_lid(dev->dd);
167         pip->lid = lid ? cpu_to_be16(lid) : IB_LID_PERMISSIVE;
168         pip->sm_lid = cpu_to_be16(dev->sm_lid);
169         pip->cap_mask = cpu_to_be32(dev->port_cap_flags);
170         /* pip->diag_code; */
171         pip->mkey_lease_period = cpu_to_be16(dev->mkey_lease_period);
172         pip->local_port_num = port;
173         pip->link_width_enabled = dev->link_width_enabled;
174         pip->link_width_supported = 3;  /* 1x or 4x */
175         pip->link_width_active = 2;     /* 4x */
176         pip->linkspeed_portstate = 0x10;        /* 2.5Gbps */
177         ibcstat = ipath_layer_get_lastibcstat(dev->dd);
178         pip->linkspeed_portstate |= ((ibcstat >> 4) & 0x3) + 1;
179         pip->portphysstate_linkdown =
180                 (ipath_cvt_physportstate[ibcstat & 0xf] << 4) |
181                 (ipath_layer_get_linkdowndefaultstate(dev->dd) ? 1 : 2);
182         pip->mkeyprot_resv_lmc = dev->mkeyprot_resv_lmc;
183         pip->linkspeedactive_enabled = 0x11;    /* 2.5Gbps, 2.5Gbps */
184         switch (ipath_layer_get_ibmtu(dev->dd)) {
185         case 4096:
186                 mtu = IB_MTU_4096;
187                 break;
188         case 2048:
189                 mtu = IB_MTU_2048;
190                 break;
191         case 1024:
192                 mtu = IB_MTU_1024;
193                 break;
194         case 512:
195                 mtu = IB_MTU_512;
196                 break;
197         case 256:
198                 mtu = IB_MTU_256;
199                 break;
200         default:                /* oops, something is wrong */
201                 mtu = IB_MTU_2048;
202                 break;
203         }
204         pip->neighbormtu_mastersmsl = (mtu << 4) | dev->sm_sl;
205         pip->vlcap_inittype = 0x10;     /* VLCap = VL0, InitType = 0 */
206         pip->vl_high_limit = dev->vl_high_limit;
207         /* pip->vl_arb_high_cap; // only one VL */
208         /* pip->vl_arb_low_cap; // only one VL */
209         /* InitTypeReply = 0 */
210         pip->inittypereply_mtucap = IB_MTU_4096;
211         // HCAs ignore VLStallCount and HOQLife
212         /* pip->vlstallcnt_hoqlife; */
213         pip->operationalvl_pei_peo_fpi_fpo = 0x10;      /* OVLs = 1 */
214         pip->mkey_violations = cpu_to_be16(dev->mkey_violations);
215         /* P_KeyViolations are counted by hardware. */
216         pip->pkey_violations =
217                 cpu_to_be16((ipath_layer_get_cr_errpkey(dev->dd) -
218                              dev->n_pkey_violations) & 0xFFFF);
219         pip->qkey_violations = cpu_to_be16(dev->qkey_violations);
220         /* Only the hardware GUID is supported for now */
221         pip->guid_cap = 1;
222         pip->clientrereg_resv_subnetto = dev->subnet_timeout;
223         /* 32.768 usec. response time (guessing) */
224         pip->resv_resptimevalue = 3;
225         pip->localphyerrors_overrunerrors =
226                 (ipath_layer_get_phyerrthreshold(dev->dd) << 4) |
227                 ipath_layer_get_overrunthreshold(dev->dd);
228         /* pip->max_credit_hint; */
229         /* pip->link_roundtrip_latency[3]; */
230
231         ret = reply(smp);
232
233 bail:
234         return ret;
235 }
236
237 static int recv_subn_get_pkeytable(struct ib_smp *smp,
238                                    struct ib_device *ibdev)
239 {
240         u32 startpx = 32 * (be32_to_cpu(smp->attr_mod) & 0xffff);
241         u16 *p = (u16 *) smp->data;
242         __be16 *q = (__be16 *) smp->data;
243
244         /* 64 blocks of 32 16-bit P_Key entries */
245
246         memset(smp->data, 0, sizeof(smp->data));
247         if (startpx == 0) {
248                 struct ipath_ibdev *dev = to_idev(ibdev);
249                 unsigned i, n = ipath_layer_get_npkeys(dev->dd);
250
251                 ipath_layer_get_pkeys(dev->dd, p);
252
253                 for (i = 0; i < n; i++)
254                         q[i] = cpu_to_be16(p[i]);
255         } else
256                 smp->status |= IB_SMP_INVALID_FIELD;
257
258         return reply(smp);
259 }
260
261 static int recv_subn_set_guidinfo(struct ib_smp *smp,
262                                   struct ib_device *ibdev)
263 {
264         /* The only GUID we support is the first read-only entry. */
265         return recv_subn_get_guidinfo(smp, ibdev);
266 }
267
268 /**
269  * recv_subn_set_portinfo - set port information
270  * @smp: the incoming SM packet
271  * @ibdev: the infiniband device
272  * @port: the port on the device
273  *
274  * Set Portinfo (see ch. 14.2.5.6).
275  */
276 static int recv_subn_set_portinfo(struct ib_smp *smp,
277                                   struct ib_device *ibdev, u8 port)
278 {
279         struct ib_port_info *pip = (struct ib_port_info *)smp->data;
280         struct ib_event event;
281         struct ipath_ibdev *dev;
282         u32 flags;
283         char clientrereg = 0;
284         u16 lid, smlid;
285         u8 lwe;
286         u8 lse;
287         u8 state;
288         u16 lstate;
289         u32 mtu;
290         int ret;
291
292         if (be32_to_cpu(smp->attr_mod) > ibdev->phys_port_cnt)
293                 goto err;
294
295         dev = to_idev(ibdev);
296         event.device = ibdev;
297         event.element.port_num = port;
298
299         dev->mkey = pip->mkey;
300         dev->gid_prefix = pip->gid_prefix;
301         dev->mkey_lease_period = be16_to_cpu(pip->mkey_lease_period);
302
303         lid = be16_to_cpu(pip->lid);
304         if (lid != ipath_layer_get_lid(dev->dd)) {
305                 /* Must be a valid unicast LID address. */
306                 if (lid == 0 || lid >= IPS_MULTICAST_LID_BASE)
307                         goto err;
308                 ipath_set_sps_lid(dev->dd, lid, pip->mkeyprot_resv_lmc & 7);
309                 event.event = IB_EVENT_LID_CHANGE;
310                 ib_dispatch_event(&event);
311         }
312
313         smlid = be16_to_cpu(pip->sm_lid);
314         if (smlid != dev->sm_lid) {
315                 /* Must be a valid unicast LID address. */
316                 if (smlid == 0 || smlid >= IPS_MULTICAST_LID_BASE)
317                         goto err;
318                 dev->sm_lid = smlid;
319                 event.event = IB_EVENT_SM_CHANGE;
320                 ib_dispatch_event(&event);
321         }
322
323         /* Only 4x supported but allow 1x or 4x to be set (see 14.2.6.6). */
324         lwe = pip->link_width_enabled;
325         if ((lwe >= 4 && lwe <= 8) || (lwe >= 0xC && lwe <= 0xFE))
326                 goto err;
327         if (lwe == 0xFF)
328                 dev->link_width_enabled = 3;    /* 1x or 4x */
329         else if (lwe)
330                 dev->link_width_enabled = lwe;
331
332         /* Only 2.5 Gbs supported. */
333         lse = pip->linkspeedactive_enabled & 0xF;
334         if (lse >= 2 && lse <= 0xE)
335                 goto err;
336
337         /* Set link down default state. */
338         switch (pip->portphysstate_linkdown & 0xF) {
339         case 0: /* NOP */
340                 break;
341         case 1: /* SLEEP */
342                 if (ipath_layer_set_linkdowndefaultstate(dev->dd, 1))
343                         goto err;
344                 break;
345         case 2: /* POLL */
346                 if (ipath_layer_set_linkdowndefaultstate(dev->dd, 0))
347                         goto err;
348                 break;
349         default:
350                 goto err;
351         }
352
353         dev->mkeyprot_resv_lmc = pip->mkeyprot_resv_lmc;
354         dev->vl_high_limit = pip->vl_high_limit;
355
356         switch ((pip->neighbormtu_mastersmsl >> 4) & 0xF) {
357         case IB_MTU_256:
358                 mtu = 256;
359                 break;
360         case IB_MTU_512:
361                 mtu = 512;
362                 break;
363         case IB_MTU_1024:
364                 mtu = 1024;
365                 break;
366         case IB_MTU_2048:
367                 mtu = 2048;
368                 break;
369         case IB_MTU_4096:
370                 mtu = 4096;
371                 break;
372         default:
373                 /* XXX We have already partially updated our state! */
374                 goto err;
375         }
376         ipath_layer_set_mtu(dev->dd, mtu);
377
378         dev->sm_sl = pip->neighbormtu_mastersmsl & 0xF;
379
380         /* We only support VL0 */
381         if (((pip->operationalvl_pei_peo_fpi_fpo >> 4) & 0xF) > 1)
382                 goto err;
383
384         if (pip->mkey_violations == 0)
385                 dev->mkey_violations = 0;
386
387         /*
388          * Hardware counter can't be reset so snapshot and subtract
389          * later.
390          */
391         if (pip->pkey_violations == 0)
392                 dev->n_pkey_violations =
393                         ipath_layer_get_cr_errpkey(dev->dd);
394
395         if (pip->qkey_violations == 0)
396                 dev->qkey_violations = 0;
397
398         if (ipath_layer_set_phyerrthreshold(
399                     dev->dd,
400                     (pip->localphyerrors_overrunerrors >> 4) & 0xF))
401                 goto err;
402
403         if (ipath_layer_set_overrunthreshold(
404                     dev->dd,
405                     (pip->localphyerrors_overrunerrors & 0xF)))
406                 goto err;
407
408         dev->subnet_timeout = pip->clientrereg_resv_subnetto & 0x1F;
409
410         if (pip->clientrereg_resv_subnetto & 0x80) {
411                 clientrereg = 1;
412                 event.event = IB_EVENT_CLIENT_REREGISTER;
413                 ib_dispatch_event(&event);
414         }
415
416         /*
417          * Do the port state change now that the other link parameters
418          * have been set.
419          * Changing the port physical state only makes sense if the link
420          * is down or is being set to down.
421          */
422         state = pip->linkspeed_portstate & 0xF;
423         flags = ipath_layer_get_flags(dev->dd);
424         lstate = (pip->portphysstate_linkdown >> 4) & 0xF;
425         if (lstate && !(state == IB_PORT_DOWN || state == IB_PORT_NOP))
426                 goto err;
427
428         /*
429          * Only state changes of DOWN, ARM, and ACTIVE are valid
430          * and must be in the correct state to take effect (see 7.2.6).
431          */
432         switch (state) {
433         case IB_PORT_NOP:
434                 if (lstate == 0)
435                         break;
436                 /* FALLTHROUGH */
437         case IB_PORT_DOWN:
438                 if (lstate == 0)
439                         if (ipath_layer_get_linkdowndefaultstate(dev->dd))
440                                 lstate = IPATH_IB_LINKDOWN_SLEEP;
441                         else
442                                 lstate = IPATH_IB_LINKDOWN;
443                 else if (lstate == 1)
444                         lstate = IPATH_IB_LINKDOWN_SLEEP;
445                 else if (lstate == 2)
446                         lstate = IPATH_IB_LINKDOWN;
447                 else if (lstate == 3)
448                         lstate = IPATH_IB_LINKDOWN_DISABLE;
449                 else
450                         goto err;
451                 ipath_layer_set_linkstate(dev->dd, lstate);
452                 if (flags & IPATH_LINKACTIVE) {
453                         event.event = IB_EVENT_PORT_ERR;
454                         ib_dispatch_event(&event);
455                 }
456                 break;
457         case IB_PORT_ARMED:
458                 if (!(flags & (IPATH_LINKINIT | IPATH_LINKACTIVE)))
459                         break;
460                 ipath_layer_set_linkstate(dev->dd, IPATH_IB_LINKARM);
461                 if (flags & IPATH_LINKACTIVE) {
462                         event.event = IB_EVENT_PORT_ERR;
463                         ib_dispatch_event(&event);
464                 }
465                 break;
466         case IB_PORT_ACTIVE:
467                 if (!(flags & IPATH_LINKARMED))
468                         break;
469                 ipath_layer_set_linkstate(dev->dd, IPATH_IB_LINKACTIVE);
470                 event.event = IB_EVENT_PORT_ACTIVE;
471                 ib_dispatch_event(&event);
472                 break;
473         default:
474                 /* XXX We have already partially updated our state! */
475                 goto err;
476         }
477
478         ret = recv_subn_get_portinfo(smp, ibdev, port);
479
480         if (clientrereg)
481                 pip->clientrereg_resv_subnetto |= 0x80;
482
483         goto done;
484
485 err:
486         smp->status |= IB_SMP_INVALID_FIELD;
487         ret = recv_subn_get_portinfo(smp, ibdev, port);
488
489 done:
490         return ret;
491 }
492
493 static int recv_subn_set_pkeytable(struct ib_smp *smp,
494                                    struct ib_device *ibdev)
495 {
496         u32 startpx = 32 * (be32_to_cpu(smp->attr_mod) & 0xffff);
497         __be16 *p = (__be16 *) smp->data;
498         u16 *q = (u16 *) smp->data;
499         struct ipath_ibdev *dev = to_idev(ibdev);
500         unsigned i, n = ipath_layer_get_npkeys(dev->dd);
501
502         for (i = 0; i < n; i++)
503                 q[i] = be16_to_cpu(p[i]);
504
505         if (startpx != 0 ||
506             ipath_layer_set_pkeys(dev->dd, q) != 0)
507                 smp->status |= IB_SMP_INVALID_FIELD;
508
509         return recv_subn_get_pkeytable(smp, ibdev);
510 }
511
512 #define IB_PMA_CLASS_PORT_INFO          __constant_htons(0x0001)
513 #define IB_PMA_PORT_SAMPLES_CONTROL     __constant_htons(0x0010)
514 #define IB_PMA_PORT_SAMPLES_RESULT      __constant_htons(0x0011)
515 #define IB_PMA_PORT_COUNTERS            __constant_htons(0x0012)
516 #define IB_PMA_PORT_COUNTERS_EXT        __constant_htons(0x001D)
517 #define IB_PMA_PORT_SAMPLES_RESULT_EXT  __constant_htons(0x001E)
518
519 struct ib_perf {
520         u8 base_version;
521         u8 mgmt_class;
522         u8 class_version;
523         u8 method;
524         __be16 status;
525         __be16 unused;
526         __be64 tid;
527         __be16 attr_id;
528         __be16 resv;
529         __be32 attr_mod;
530         u8 reserved[40];
531         u8 data[192];
532 } __attribute__ ((packed));
533
534 struct ib_pma_classportinfo {
535         u8 base_version;
536         u8 class_version;
537         __be16 cap_mask;
538         u8 reserved[3];
539         u8 resp_time_value;     /* only lower 5 bits */
540         union ib_gid redirect_gid;
541         __be32 redirect_tc_sl_fl;       /* 8, 4, 20 bits respectively */
542         __be16 redirect_lid;
543         __be16 redirect_pkey;
544         __be32 redirect_qp;     /* only lower 24 bits */
545         __be32 redirect_qkey;
546         union ib_gid trap_gid;
547         __be32 trap_tc_sl_fl;   /* 8, 4, 20 bits respectively */
548         __be16 trap_lid;
549         __be16 trap_pkey;
550         __be32 trap_hl_qp;      /* 8, 24 bits respectively */
551         __be32 trap_qkey;
552 } __attribute__ ((packed));
553
554 struct ib_pma_portsamplescontrol {
555         u8 opcode;
556         u8 port_select;
557         u8 tick;
558         u8 counter_width;       /* only lower 3 bits */
559         __be32 counter_mask0_9; /* 2, 10 * 3, bits */
560         __be16 counter_mask10_14;       /* 1, 5 * 3, bits */
561         u8 sample_mechanisms;
562         u8 sample_status;       /* only lower 2 bits */
563         __be64 option_mask;
564         __be64 vendor_mask;
565         __be32 sample_start;
566         __be32 sample_interval;
567         __be16 tag;
568         __be16 counter_select[15];
569 } __attribute__ ((packed));
570
571 struct ib_pma_portsamplesresult {
572         __be16 tag;
573         __be16 sample_status;   /* only lower 2 bits */
574         __be32 counter[15];
575 } __attribute__ ((packed));
576
577 struct ib_pma_portsamplesresult_ext {
578         __be16 tag;
579         __be16 sample_status;   /* only lower 2 bits */
580         __be32 extended_width;  /* only upper 2 bits */
581         __be64 counter[15];
582 } __attribute__ ((packed));
583
584 struct ib_pma_portcounters {
585         u8 reserved;
586         u8 port_select;
587         __be16 counter_select;
588         __be16 symbol_error_counter;
589         u8 link_error_recovery_counter;
590         u8 link_downed_counter;
591         __be16 port_rcv_errors;
592         __be16 port_rcv_remphys_errors;
593         __be16 port_rcv_switch_relay_errors;
594         __be16 port_xmit_discards;
595         u8 port_xmit_constraint_errors;
596         u8 port_rcv_constraint_errors;
597         u8 reserved1;
598         u8 lli_ebor_errors;     /* 4, 4, bits */
599         __be16 reserved2;
600         __be16 vl15_dropped;
601         __be32 port_xmit_data;
602         __be32 port_rcv_data;
603         __be32 port_xmit_packets;
604         __be32 port_rcv_packets;
605 } __attribute__ ((packed));
606
607 #define IB_PMA_SEL_SYMBOL_ERROR                 __constant_htons(0x0001)
608 #define IB_PMA_SEL_LINK_ERROR_RECOVERY          __constant_htons(0x0002)
609 #define IB_PMA_SEL_LINK_DOWNED                  __constant_htons(0x0004)
610 #define IB_PMA_SEL_PORT_RCV_ERRORS              __constant_htons(0x0008)
611 #define IB_PMA_SEL_PORT_RCV_REMPHYS_ERRORS      __constant_htons(0x0010)
612 #define IB_PMA_SEL_PORT_XMIT_DISCARDS           __constant_htons(0x0040)
613 #define IB_PMA_SEL_PORT_XMIT_DATA               __constant_htons(0x1000)
614 #define IB_PMA_SEL_PORT_RCV_DATA                __constant_htons(0x2000)
615 #define IB_PMA_SEL_PORT_XMIT_PACKETS            __constant_htons(0x4000)
616 #define IB_PMA_SEL_PORT_RCV_PACKETS             __constant_htons(0x8000)
617
618 struct ib_pma_portcounters_ext {
619         u8 reserved;
620         u8 port_select;
621         __be16 counter_select;
622         __be32 reserved1;
623         __be64 port_xmit_data;
624         __be64 port_rcv_data;
625         __be64 port_xmit_packets;
626         __be64 port_rcv_packets;
627         __be64 port_unicast_xmit_packets;
628         __be64 port_unicast_rcv_packets;
629         __be64 port_multicast_xmit_packets;
630         __be64 port_multicast_rcv_packets;
631 } __attribute__ ((packed));
632
633 #define IB_PMA_SELX_PORT_XMIT_DATA              __constant_htons(0x0001)
634 #define IB_PMA_SELX_PORT_RCV_DATA               __constant_htons(0x0002)
635 #define IB_PMA_SELX_PORT_XMIT_PACKETS           __constant_htons(0x0004)
636 #define IB_PMA_SELX_PORT_RCV_PACKETS            __constant_htons(0x0008)
637 #define IB_PMA_SELX_PORT_UNI_XMIT_PACKETS       __constant_htons(0x0010)
638 #define IB_PMA_SELX_PORT_UNI_RCV_PACKETS        __constant_htons(0x0020)
639 #define IB_PMA_SELX_PORT_MULTI_XMIT_PACKETS     __constant_htons(0x0040)
640 #define IB_PMA_SELX_PORT_MULTI_RCV_PACKETS      __constant_htons(0x0080)
641
642 static int recv_pma_get_classportinfo(struct ib_perf *pmp)
643 {
644         struct ib_pma_classportinfo *p =
645                 (struct ib_pma_classportinfo *)pmp->data;
646
647         memset(pmp->data, 0, sizeof(pmp->data));
648
649         if (pmp->attr_mod != 0)
650                 pmp->status |= IB_SMP_INVALID_FIELD;
651
652         /* Indicate AllPortSelect is valid (only one port anyway) */
653         p->cap_mask = __constant_cpu_to_be16(1 << 8);
654         p->base_version = 1;
655         p->class_version = 1;
656         /*
657          * Expected response time is 4.096 usec. * 2^18 == 1.073741824
658          * sec.
659          */
660         p->resp_time_value = 18;
661
662         return reply((struct ib_smp *) pmp);
663 }
664
665 /*
666  * The PortSamplesControl.CounterMasks field is an array of 3 bit fields
667  * which specify the N'th counter's capabilities. See ch. 16.1.3.2.
668  * We support 5 counters which only count the mandatory quantities.
669  */
670 #define COUNTER_MASK(q, n) (q << ((9 - n) * 3))
671 #define COUNTER_MASK0_9 \
672         __constant_cpu_to_be32(COUNTER_MASK(1, 0) | \
673                                COUNTER_MASK(1, 1) | \
674                                COUNTER_MASK(1, 2) | \
675                                COUNTER_MASK(1, 3) | \
676                                COUNTER_MASK(1, 4))
677
678 static int recv_pma_get_portsamplescontrol(struct ib_perf *pmp,
679                                            struct ib_device *ibdev, u8 port)
680 {
681         struct ib_pma_portsamplescontrol *p =
682                 (struct ib_pma_portsamplescontrol *)pmp->data;
683         struct ipath_ibdev *dev = to_idev(ibdev);
684         unsigned long flags;
685         u8 port_select = p->port_select;
686
687         memset(pmp->data, 0, sizeof(pmp->data));
688
689         p->port_select = port_select;
690         if (pmp->attr_mod != 0 ||
691             (port_select != port && port_select != 0xFF))
692                 pmp->status |= IB_SMP_INVALID_FIELD;
693         /*
694          * Ticks are 10x the link transfer period which for 2.5Gbs is 4
695          * nsec.  0 == 4 nsec., 1 == 8 nsec., ..., 255 == 1020 nsec.  Sample
696          * intervals are counted in ticks.  Since we use Linux timers, that
697          * count in jiffies, we can't sample for less than 1000 ticks if HZ
698          * == 1000 (4000 ticks if HZ is 250).
699          */
700         /* XXX This is WRONG. */
701         p->tick = 250;          /* 1 usec. */
702         p->counter_width = 4;   /* 32 bit counters */
703         p->counter_mask0_9 = COUNTER_MASK0_9;
704         spin_lock_irqsave(&dev->pending_lock, flags);
705         p->sample_status = dev->pma_sample_status;
706         p->sample_start = cpu_to_be32(dev->pma_sample_start);
707         p->sample_interval = cpu_to_be32(dev->pma_sample_interval);
708         p->tag = cpu_to_be16(dev->pma_tag);
709         p->counter_select[0] = dev->pma_counter_select[0];
710         p->counter_select[1] = dev->pma_counter_select[1];
711         p->counter_select[2] = dev->pma_counter_select[2];
712         p->counter_select[3] = dev->pma_counter_select[3];
713         p->counter_select[4] = dev->pma_counter_select[4];
714         spin_unlock_irqrestore(&dev->pending_lock, flags);
715
716         return reply((struct ib_smp *) pmp);
717 }
718
719 static int recv_pma_set_portsamplescontrol(struct ib_perf *pmp,
720                                            struct ib_device *ibdev, u8 port)
721 {
722         struct ib_pma_portsamplescontrol *p =
723                 (struct ib_pma_portsamplescontrol *)pmp->data;
724         struct ipath_ibdev *dev = to_idev(ibdev);
725         unsigned long flags;
726         u32 start;
727         int ret;
728
729         if (pmp->attr_mod != 0 ||
730             (p->port_select != port && p->port_select != 0xFF)) {
731                 pmp->status |= IB_SMP_INVALID_FIELD;
732                 ret = reply((struct ib_smp *) pmp);
733                 goto bail;
734         }
735
736         start = be32_to_cpu(p->sample_start);
737         if (start != 0) {
738                 spin_lock_irqsave(&dev->pending_lock, flags);
739                 if (dev->pma_sample_status == IB_PMA_SAMPLE_STATUS_DONE) {
740                         dev->pma_sample_status =
741                                 IB_PMA_SAMPLE_STATUS_STARTED;
742                         dev->pma_sample_start = start;
743                         dev->pma_sample_interval =
744                                 be32_to_cpu(p->sample_interval);
745                         dev->pma_tag = be16_to_cpu(p->tag);
746                         if (p->counter_select[0])
747                                 dev->pma_counter_select[0] =
748                                         p->counter_select[0];
749                         if (p->counter_select[1])
750                                 dev->pma_counter_select[1] =
751                                         p->counter_select[1];
752                         if (p->counter_select[2])
753                                 dev->pma_counter_select[2] =
754                                         p->counter_select[2];
755                         if (p->counter_select[3])
756                                 dev->pma_counter_select[3] =
757                                         p->counter_select[3];
758                         if (p->counter_select[4])
759                                 dev->pma_counter_select[4] =
760                                         p->counter_select[4];
761                 }
762                 spin_unlock_irqrestore(&dev->pending_lock, flags);
763         }
764         ret = recv_pma_get_portsamplescontrol(pmp, ibdev, port);
765
766 bail:
767         return ret;
768 }
769
770 static u64 get_counter(struct ipath_ibdev *dev, __be16 sel)
771 {
772         u64 ret;
773
774         switch (sel) {
775         case IB_PMA_PORT_XMIT_DATA:
776                 ret = dev->ipath_sword;
777                 break;
778         case IB_PMA_PORT_RCV_DATA:
779                 ret = dev->ipath_rword;
780                 break;
781         case IB_PMA_PORT_XMIT_PKTS:
782                 ret = dev->ipath_spkts;
783                 break;
784         case IB_PMA_PORT_RCV_PKTS:
785                 ret = dev->ipath_rpkts;
786                 break;
787         case IB_PMA_PORT_XMIT_WAIT:
788                 ret = dev->ipath_xmit_wait;
789                 break;
790         default:
791                 ret = 0;
792         }
793
794         return ret;
795 }
796
797 static int recv_pma_get_portsamplesresult(struct ib_perf *pmp,
798                                           struct ib_device *ibdev)
799 {
800         struct ib_pma_portsamplesresult *p =
801                 (struct ib_pma_portsamplesresult *)pmp->data;
802         struct ipath_ibdev *dev = to_idev(ibdev);
803         int i;
804
805         memset(pmp->data, 0, sizeof(pmp->data));
806         p->tag = cpu_to_be16(dev->pma_tag);
807         p->sample_status = cpu_to_be16(dev->pma_sample_status);
808         for (i = 0; i < ARRAY_SIZE(dev->pma_counter_select); i++)
809                 p->counter[i] = cpu_to_be32(
810                         get_counter(dev, dev->pma_counter_select[i]));
811
812         return reply((struct ib_smp *) pmp);
813 }
814
815 static int recv_pma_get_portsamplesresult_ext(struct ib_perf *pmp,
816                                               struct ib_device *ibdev)
817 {
818         struct ib_pma_portsamplesresult_ext *p =
819                 (struct ib_pma_portsamplesresult_ext *)pmp->data;
820         struct ipath_ibdev *dev = to_idev(ibdev);
821         int i;
822
823         memset(pmp->data, 0, sizeof(pmp->data));
824         p->tag = cpu_to_be16(dev->pma_tag);
825         p->sample_status = cpu_to_be16(dev->pma_sample_status);
826         /* 64 bits */
827         p->extended_width = __constant_cpu_to_be32(0x80000000);
828         for (i = 0; i < ARRAY_SIZE(dev->pma_counter_select); i++)
829                 p->counter[i] = cpu_to_be64(
830                         get_counter(dev, dev->pma_counter_select[i]));
831
832         return reply((struct ib_smp *) pmp);
833 }
834
835 static int recv_pma_get_portcounters(struct ib_perf *pmp,
836                                      struct ib_device *ibdev, u8 port)
837 {
838         struct ib_pma_portcounters *p = (struct ib_pma_portcounters *)
839                 pmp->data;
840         struct ipath_ibdev *dev = to_idev(ibdev);
841         struct ipath_layer_counters cntrs;
842         u8 port_select = p->port_select;
843
844         ipath_layer_get_counters(dev->dd, &cntrs);
845
846         /* Adjust counters for any resets done. */
847         cntrs.symbol_error_counter -= dev->n_symbol_error_counter;
848         cntrs.link_error_recovery_counter -=
849                 dev->n_link_error_recovery_counter;
850         cntrs.link_downed_counter -= dev->n_link_downed_counter;
851         cntrs.port_rcv_errors += dev->rcv_errors;
852         cntrs.port_rcv_errors -= dev->n_port_rcv_errors;
853         cntrs.port_rcv_remphys_errors -= dev->n_port_rcv_remphys_errors;
854         cntrs.port_xmit_discards -= dev->n_port_xmit_discards;
855         cntrs.port_xmit_data -= dev->n_port_xmit_data;
856         cntrs.port_rcv_data -= dev->n_port_rcv_data;
857         cntrs.port_xmit_packets -= dev->n_port_xmit_packets;
858         cntrs.port_rcv_packets -= dev->n_port_rcv_packets;
859
860         memset(pmp->data, 0, sizeof(pmp->data));
861
862         p->port_select = port_select;
863         if (pmp->attr_mod != 0 ||
864             (port_select != port && port_select != 0xFF))
865                 pmp->status |= IB_SMP_INVALID_FIELD;
866
867         if (cntrs.symbol_error_counter > 0xFFFFUL)
868                 p->symbol_error_counter = __constant_cpu_to_be16(0xFFFF);
869         else
870                 p->symbol_error_counter =
871                         cpu_to_be16((u16)cntrs.symbol_error_counter);
872         if (cntrs.link_error_recovery_counter > 0xFFUL)
873                 p->link_error_recovery_counter = 0xFF;
874         else
875                 p->link_error_recovery_counter =
876                         (u8)cntrs.link_error_recovery_counter;
877         if (cntrs.link_downed_counter > 0xFFUL)
878                 p->link_downed_counter = 0xFF;
879         else
880                 p->link_downed_counter = (u8)cntrs.link_downed_counter;
881         if (cntrs.port_rcv_errors > 0xFFFFUL)
882                 p->port_rcv_errors = __constant_cpu_to_be16(0xFFFF);
883         else
884                 p->port_rcv_errors =
885                         cpu_to_be16((u16) cntrs.port_rcv_errors);
886         if (cntrs.port_rcv_remphys_errors > 0xFFFFUL)
887                 p->port_rcv_remphys_errors = __constant_cpu_to_be16(0xFFFF);
888         else
889                 p->port_rcv_remphys_errors =
890                         cpu_to_be16((u16)cntrs.port_rcv_remphys_errors);
891         if (cntrs.port_xmit_discards > 0xFFFFUL)
892                 p->port_xmit_discards = __constant_cpu_to_be16(0xFFFF);
893         else
894                 p->port_xmit_discards =
895                         cpu_to_be16((u16)cntrs.port_xmit_discards);
896         if (cntrs.port_xmit_data > 0xFFFFFFFFUL)
897                 p->port_xmit_data = __constant_cpu_to_be32(0xFFFFFFFF);
898         else
899                 p->port_xmit_data = cpu_to_be32((u32)cntrs.port_xmit_data);
900         if (cntrs.port_rcv_data > 0xFFFFFFFFUL)
901                 p->port_rcv_data = __constant_cpu_to_be32(0xFFFFFFFF);
902         else
903                 p->port_rcv_data = cpu_to_be32((u32)cntrs.port_rcv_data);
904         if (cntrs.port_xmit_packets > 0xFFFFFFFFUL)
905                 p->port_xmit_packets = __constant_cpu_to_be32(0xFFFFFFFF);
906         else
907                 p->port_xmit_packets =
908                         cpu_to_be32((u32)cntrs.port_xmit_packets);
909         if (cntrs.port_rcv_packets > 0xFFFFFFFFUL)
910                 p->port_rcv_packets = __constant_cpu_to_be32(0xFFFFFFFF);
911         else
912                 p->port_rcv_packets =
913                         cpu_to_be32((u32) cntrs.port_rcv_packets);
914
915         return reply((struct ib_smp *) pmp);
916 }
917
918 static int recv_pma_get_portcounters_ext(struct ib_perf *pmp,
919                                          struct ib_device *ibdev, u8 port)
920 {
921         struct ib_pma_portcounters_ext *p =
922                 (struct ib_pma_portcounters_ext *)pmp->data;
923         struct ipath_ibdev *dev = to_idev(ibdev);
924         u64 swords, rwords, spkts, rpkts, xwait;
925         u8 port_select = p->port_select;
926
927         ipath_layer_snapshot_counters(dev->dd, &swords, &rwords, &spkts,
928                                       &rpkts, &xwait);
929
930         /* Adjust counters for any resets done. */
931         swords -= dev->n_port_xmit_data;
932         rwords -= dev->n_port_rcv_data;
933         spkts -= dev->n_port_xmit_packets;
934         rpkts -= dev->n_port_rcv_packets;
935
936         memset(pmp->data, 0, sizeof(pmp->data));
937
938         p->port_select = port_select;
939         if (pmp->attr_mod != 0 ||
940             (port_select != port && port_select != 0xFF))
941                 pmp->status |= IB_SMP_INVALID_FIELD;
942
943         p->port_xmit_data = cpu_to_be64(swords);
944         p->port_rcv_data = cpu_to_be64(rwords);
945         p->port_xmit_packets = cpu_to_be64(spkts);
946         p->port_rcv_packets = cpu_to_be64(rpkts);
947         p->port_unicast_xmit_packets = cpu_to_be64(dev->n_unicast_xmit);
948         p->port_unicast_rcv_packets = cpu_to_be64(dev->n_unicast_rcv);
949         p->port_multicast_xmit_packets = cpu_to_be64(dev->n_multicast_xmit);
950         p->port_multicast_rcv_packets = cpu_to_be64(dev->n_multicast_rcv);
951
952         return reply((struct ib_smp *) pmp);
953 }
954
955 static int recv_pma_set_portcounters(struct ib_perf *pmp,
956                                      struct ib_device *ibdev, u8 port)
957 {
958         struct ib_pma_portcounters *p = (struct ib_pma_portcounters *)
959                 pmp->data;
960         struct ipath_ibdev *dev = to_idev(ibdev);
961         struct ipath_layer_counters cntrs;
962
963         /*
964          * Since the HW doesn't support clearing counters, we save the
965          * current count and subtract it from future responses.
966          */
967         ipath_layer_get_counters(dev->dd, &cntrs);
968
969         if (p->counter_select & IB_PMA_SEL_SYMBOL_ERROR)
970                 dev->n_symbol_error_counter = cntrs.symbol_error_counter;
971
972         if (p->counter_select & IB_PMA_SEL_LINK_ERROR_RECOVERY)
973                 dev->n_link_error_recovery_counter =
974                         cntrs.link_error_recovery_counter;
975
976         if (p->counter_select & IB_PMA_SEL_LINK_DOWNED)
977                 dev->n_link_downed_counter = cntrs.link_downed_counter;
978
979         if (p->counter_select & IB_PMA_SEL_PORT_RCV_ERRORS)
980                 dev->n_port_rcv_errors =
981                         cntrs.port_rcv_errors + dev->rcv_errors;
982
983         if (p->counter_select & IB_PMA_SEL_PORT_RCV_REMPHYS_ERRORS)
984                 dev->n_port_rcv_remphys_errors =
985                         cntrs.port_rcv_remphys_errors;
986
987         if (p->counter_select & IB_PMA_SEL_PORT_XMIT_DISCARDS)
988                 dev->n_port_xmit_discards = cntrs.port_xmit_discards;
989
990         if (p->counter_select & IB_PMA_SEL_PORT_XMIT_DATA)
991                 dev->n_port_xmit_data = cntrs.port_xmit_data;
992
993         if (p->counter_select & IB_PMA_SEL_PORT_RCV_DATA)
994                 dev->n_port_rcv_data = cntrs.port_rcv_data;
995
996         if (p->counter_select & IB_PMA_SEL_PORT_XMIT_PACKETS)
997                 dev->n_port_xmit_packets = cntrs.port_xmit_packets;
998
999         if (p->counter_select & IB_PMA_SEL_PORT_RCV_PACKETS)
1000                 dev->n_port_rcv_packets = cntrs.port_rcv_packets;
1001
1002         return recv_pma_get_portcounters(pmp, ibdev, port);
1003 }
1004
1005 static int recv_pma_set_portcounters_ext(struct ib_perf *pmp,
1006                                          struct ib_device *ibdev, u8 port)
1007 {
1008         struct ib_pma_portcounters *p = (struct ib_pma_portcounters *)
1009                 pmp->data;
1010         struct ipath_ibdev *dev = to_idev(ibdev);
1011         u64 swords, rwords, spkts, rpkts, xwait;
1012
1013         ipath_layer_snapshot_counters(dev->dd, &swords, &rwords, &spkts,
1014                                       &rpkts, &xwait);
1015
1016         if (p->counter_select & IB_PMA_SELX_PORT_XMIT_DATA)
1017                 dev->n_port_xmit_data = swords;
1018
1019         if (p->counter_select & IB_PMA_SELX_PORT_RCV_DATA)
1020                 dev->n_port_rcv_data = rwords;
1021
1022         if (p->counter_select & IB_PMA_SELX_PORT_XMIT_PACKETS)
1023                 dev->n_port_xmit_packets = spkts;
1024
1025         if (p->counter_select & IB_PMA_SELX_PORT_RCV_PACKETS)
1026                 dev->n_port_rcv_packets = rpkts;
1027
1028         if (p->counter_select & IB_PMA_SELX_PORT_UNI_XMIT_PACKETS)
1029                 dev->n_unicast_xmit = 0;
1030
1031         if (p->counter_select & IB_PMA_SELX_PORT_UNI_RCV_PACKETS)
1032                 dev->n_unicast_rcv = 0;
1033
1034         if (p->counter_select & IB_PMA_SELX_PORT_MULTI_XMIT_PACKETS)
1035                 dev->n_multicast_xmit = 0;
1036
1037         if (p->counter_select & IB_PMA_SELX_PORT_MULTI_RCV_PACKETS)
1038                 dev->n_multicast_rcv = 0;
1039
1040         return recv_pma_get_portcounters_ext(pmp, ibdev, port);
1041 }
1042
1043 static int process_subn(struct ib_device *ibdev, int mad_flags,
1044                         u8 port_num, struct ib_mad *in_mad,
1045                         struct ib_mad *out_mad)
1046 {
1047         struct ib_smp *smp = (struct ib_smp *)out_mad;
1048         struct ipath_ibdev *dev = to_idev(ibdev);
1049         int ret;
1050
1051         *out_mad = *in_mad;
1052         if (smp->class_version != 1) {
1053                 smp->status |= IB_SMP_UNSUP_VERSION;
1054                 ret = reply(smp);
1055                 goto bail;
1056         }
1057
1058         /* Is the mkey in the process of expiring? */
1059         if (dev->mkey_lease_timeout && jiffies >= dev->mkey_lease_timeout) {
1060                 /* Clear timeout and mkey protection field. */
1061                 dev->mkey_lease_timeout = 0;
1062                 dev->mkeyprot_resv_lmc &= 0x3F;
1063         }
1064
1065         /*
1066          * M_Key checking depends on
1067          * Portinfo:M_Key_protect_bits
1068          */
1069         if ((mad_flags & IB_MAD_IGNORE_MKEY) == 0 && dev->mkey != 0 &&
1070             dev->mkey != smp->mkey &&
1071             (smp->method == IB_MGMT_METHOD_SET ||
1072              (smp->method == IB_MGMT_METHOD_GET &&
1073               (dev->mkeyprot_resv_lmc >> 7) != 0))) {
1074                 if (dev->mkey_violations != 0xFFFF)
1075                         ++dev->mkey_violations;
1076                 if (dev->mkey_lease_timeout ||
1077                     dev->mkey_lease_period == 0) {
1078                         ret = IB_MAD_RESULT_SUCCESS |
1079                                 IB_MAD_RESULT_CONSUMED;
1080                         goto bail;
1081                 }
1082                 dev->mkey_lease_timeout = jiffies +
1083                         dev->mkey_lease_period * HZ;
1084                 /* Future: Generate a trap notice. */
1085                 ret = IB_MAD_RESULT_SUCCESS | IB_MAD_RESULT_CONSUMED;
1086                 goto bail;
1087         } else if (dev->mkey_lease_timeout)
1088                 dev->mkey_lease_timeout = 0;
1089
1090         switch (smp->method) {
1091         case IB_MGMT_METHOD_GET:
1092                 switch (smp->attr_id) {
1093                 case IB_SMP_ATTR_NODE_DESC:
1094                         ret = recv_subn_get_nodedescription(smp, ibdev);
1095                         goto bail;
1096                 case IB_SMP_ATTR_NODE_INFO:
1097                         ret = recv_subn_get_nodeinfo(smp, ibdev, port_num);
1098                         goto bail;
1099                 case IB_SMP_ATTR_GUID_INFO:
1100                         ret = recv_subn_get_guidinfo(smp, ibdev);
1101                         goto bail;
1102                 case IB_SMP_ATTR_PORT_INFO:
1103                         ret = recv_subn_get_portinfo(smp, ibdev, port_num);
1104                         goto bail;
1105                 case IB_SMP_ATTR_PKEY_TABLE:
1106                         ret = recv_subn_get_pkeytable(smp, ibdev);
1107                         goto bail;
1108                 case IB_SMP_ATTR_SM_INFO:
1109                         if (dev->port_cap_flags & IB_PORT_SM_DISABLED) {
1110                                 ret = IB_MAD_RESULT_SUCCESS |
1111                                         IB_MAD_RESULT_CONSUMED;
1112                                 goto bail;
1113                         }
1114                         if (dev->port_cap_flags & IB_PORT_SM) {
1115                                 ret = IB_MAD_RESULT_SUCCESS;
1116                                 goto bail;
1117                         }
1118                         /* FALLTHROUGH */
1119                 default:
1120                         smp->status |= IB_SMP_UNSUP_METH_ATTR;
1121                         ret = reply(smp);
1122                         goto bail;
1123                 }
1124
1125         case IB_MGMT_METHOD_SET:
1126                 switch (smp->attr_id) {
1127                 case IB_SMP_ATTR_GUID_INFO:
1128                         ret = recv_subn_set_guidinfo(smp, ibdev);
1129                         goto bail;
1130                 case IB_SMP_ATTR_PORT_INFO:
1131                         ret = recv_subn_set_portinfo(smp, ibdev, port_num);
1132                         goto bail;
1133                 case IB_SMP_ATTR_PKEY_TABLE:
1134                         ret = recv_subn_set_pkeytable(smp, ibdev);
1135                         goto bail;
1136                 case IB_SMP_ATTR_SM_INFO:
1137                         if (dev->port_cap_flags & IB_PORT_SM_DISABLED) {
1138                                 ret = IB_MAD_RESULT_SUCCESS |
1139                                         IB_MAD_RESULT_CONSUMED;
1140                                 goto bail;
1141                         }
1142                         if (dev->port_cap_flags & IB_PORT_SM) {
1143                                 ret = IB_MAD_RESULT_SUCCESS;
1144                                 goto bail;
1145                         }
1146                         /* FALLTHROUGH */
1147                 default:
1148                         smp->status |= IB_SMP_UNSUP_METH_ATTR;
1149                         ret = reply(smp);
1150                         goto bail;
1151                 }
1152
1153         case IB_MGMT_METHOD_GET_RESP:
1154                 /*
1155                  * The ib_mad module will call us to process responses
1156                  * before checking for other consumers.
1157                  * Just tell the caller to process it normally.
1158                  */
1159                 ret = IB_MAD_RESULT_FAILURE;
1160                 goto bail;
1161         default:
1162                 smp->status |= IB_SMP_UNSUP_METHOD;
1163                 ret = reply(smp);
1164         }
1165
1166 bail:
1167         return ret;
1168 }
1169
1170 static int process_perf(struct ib_device *ibdev, u8 port_num,
1171                         struct ib_mad *in_mad,
1172                         struct ib_mad *out_mad)
1173 {
1174         struct ib_perf *pmp = (struct ib_perf *)out_mad;
1175         int ret;
1176
1177         *out_mad = *in_mad;
1178         if (pmp->class_version != 1) {
1179                 pmp->status |= IB_SMP_UNSUP_VERSION;
1180                 ret = reply((struct ib_smp *) pmp);
1181                 goto bail;
1182         }
1183
1184         switch (pmp->method) {
1185         case IB_MGMT_METHOD_GET:
1186                 switch (pmp->attr_id) {
1187                 case IB_PMA_CLASS_PORT_INFO:
1188                         ret = recv_pma_get_classportinfo(pmp);
1189                         goto bail;
1190                 case IB_PMA_PORT_SAMPLES_CONTROL:
1191                         ret = recv_pma_get_portsamplescontrol(pmp, ibdev,
1192                                                               port_num);
1193                         goto bail;
1194                 case IB_PMA_PORT_SAMPLES_RESULT:
1195                         ret = recv_pma_get_portsamplesresult(pmp, ibdev);
1196                         goto bail;
1197                 case IB_PMA_PORT_SAMPLES_RESULT_EXT:
1198                         ret = recv_pma_get_portsamplesresult_ext(pmp,
1199                                                                  ibdev);
1200                         goto bail;
1201                 case IB_PMA_PORT_COUNTERS:
1202                         ret = recv_pma_get_portcounters(pmp, ibdev,
1203                                                         port_num);
1204                         goto bail;
1205                 case IB_PMA_PORT_COUNTERS_EXT:
1206                         ret = recv_pma_get_portcounters_ext(pmp, ibdev,
1207                                                             port_num);
1208                         goto bail;
1209                 default:
1210                         pmp->status |= IB_SMP_UNSUP_METH_ATTR;
1211                         ret = reply((struct ib_smp *) pmp);
1212                         goto bail;
1213                 }
1214
1215         case IB_MGMT_METHOD_SET:
1216                 switch (pmp->attr_id) {
1217                 case IB_PMA_PORT_SAMPLES_CONTROL:
1218                         ret = recv_pma_set_portsamplescontrol(pmp, ibdev,
1219                                                               port_num);
1220                         goto bail;
1221                 case IB_PMA_PORT_COUNTERS:
1222                         ret = recv_pma_set_portcounters(pmp, ibdev,
1223                                                         port_num);
1224                         goto bail;
1225                 case IB_PMA_PORT_COUNTERS_EXT:
1226                         ret = recv_pma_set_portcounters_ext(pmp, ibdev,
1227                                                             port_num);
1228                         goto bail;
1229                 default:
1230                         pmp->status |= IB_SMP_UNSUP_METH_ATTR;
1231                         ret = reply((struct ib_smp *) pmp);
1232                         goto bail;
1233                 }
1234
1235         case IB_MGMT_METHOD_GET_RESP:
1236                 /*
1237                  * The ib_mad module will call us to process responses
1238                  * before checking for other consumers.
1239                  * Just tell the caller to process it normally.
1240                  */
1241                 ret = IB_MAD_RESULT_FAILURE;
1242                 goto bail;
1243         default:
1244                 pmp->status |= IB_SMP_UNSUP_METHOD;
1245                 ret = reply((struct ib_smp *) pmp);
1246         }
1247
1248 bail:
1249         return ret;
1250 }
1251
1252 /**
1253  * ipath_process_mad - process an incoming MAD packet
1254  * @ibdev: the infiniband device this packet came in on
1255  * @mad_flags: MAD flags
1256  * @port_num: the port number this packet came in on
1257  * @in_wc: the work completion entry for this packet
1258  * @in_grh: the global route header for this packet
1259  * @in_mad: the incoming MAD
1260  * @out_mad: any outgoing MAD reply
1261  *
1262  * Returns IB_MAD_RESULT_SUCCESS if this is a MAD that we are not
1263  * interested in processing.
1264  *
1265  * Note that the verbs framework has already done the MAD sanity checks,
1266  * and hop count/pointer updating for IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE
1267  * MADs.
1268  *
1269  * This is called by the ib_mad module.
1270  */
1271 int ipath_process_mad(struct ib_device *ibdev, int mad_flags, u8 port_num,
1272                       struct ib_wc *in_wc, struct ib_grh *in_grh,
1273                       struct ib_mad *in_mad, struct ib_mad *out_mad)
1274 {
1275         struct ipath_ibdev *dev = to_idev(ibdev);
1276         int ret;
1277
1278         /*
1279          * Snapshot current HW counters to "clear" them.
1280          * This should be done when the driver is loaded except that for
1281          * some reason we get a zillion errors when brining up the link.
1282          */
1283         if (dev->rcv_errors == 0) {
1284                 struct ipath_layer_counters cntrs;
1285
1286                 ipath_layer_get_counters(to_idev(ibdev)->dd, &cntrs);
1287                 dev->rcv_errors++;
1288                 dev->n_symbol_error_counter = cntrs.symbol_error_counter;
1289                 dev->n_link_error_recovery_counter =
1290                         cntrs.link_error_recovery_counter;
1291                 dev->n_link_downed_counter = cntrs.link_downed_counter;
1292                 dev->n_port_rcv_errors = cntrs.port_rcv_errors + 1;
1293                 dev->n_port_rcv_remphys_errors =
1294                         cntrs.port_rcv_remphys_errors;
1295                 dev->n_port_xmit_discards = cntrs.port_xmit_discards;
1296                 dev->n_port_xmit_data = cntrs.port_xmit_data;
1297                 dev->n_port_rcv_data = cntrs.port_rcv_data;
1298                 dev->n_port_xmit_packets = cntrs.port_xmit_packets;
1299                 dev->n_port_rcv_packets = cntrs.port_rcv_packets;
1300         }
1301         switch (in_mad->mad_hdr.mgmt_class) {
1302         case IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE:
1303         case IB_MGMT_CLASS_SUBN_LID_ROUTED:
1304                 ret = process_subn(ibdev, mad_flags, port_num,
1305                                    in_mad, out_mad);
1306                 goto bail;
1307         case IB_MGMT_CLASS_PERF_MGMT:
1308                 ret = process_perf(ibdev, port_num, in_mad, out_mad);
1309                 goto bail;
1310         default:
1311                 ret = IB_MAD_RESULT_SUCCESS;
1312         }
1313
1314 bail:
1315         return ret;
1316 }