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