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