Merge branch 'core/topology' of git://git.kernel.org/pub/scm/linux/kernel/git/tip...
[pandora-kernel.git] / drivers / infiniband / hw / ipath / ipath_mad.c
1 /*
2  * Copyright (c) 2006, 2007, 2008 QLogic Corporation. 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         /* GUID 0 is illegal */
91         if (smp->attr_mod || (dd->ipath_guid == 0))
92                 smp->status |= IB_SMP_INVALID_FIELD;
93
94         nip->base_version = 1;
95         nip->class_version = 1;
96         nip->node_type = 1;     /* channel adapter */
97         /*
98          * XXX The num_ports value will need a layer function to get
99          * the value if we ever have more than one IB port on a chip.
100          * We will also need to get the GUID for the port.
101          */
102         nip->num_ports = ibdev->phys_port_cnt;
103         /* This is already in network order */
104         nip->sys_guid = to_idev(ibdev)->sys_image_guid;
105         nip->node_guid = dd->ipath_guid;
106         nip->port_guid = dd->ipath_guid;
107         nip->partition_cap = cpu_to_be16(ipath_get_npkeys(dd));
108         nip->device_id = cpu_to_be16(dd->ipath_deviceid);
109         majrev = dd->ipath_majrev;
110         minrev = dd->ipath_minrev;
111         nip->revision = cpu_to_be32((majrev << 16) | minrev);
112         nip->local_port_num = port;
113         vendor = dd->ipath_vendorid;
114         nip->vendor_id[0] = IPATH_SRC_OUI_1;
115         nip->vendor_id[1] = IPATH_SRC_OUI_2;
116         nip->vendor_id[2] = IPATH_SRC_OUI_3;
117
118         return reply(smp);
119 }
120
121 static int recv_subn_get_guidinfo(struct ib_smp *smp,
122                                   struct ib_device *ibdev)
123 {
124         u32 startgx = 8 * be32_to_cpu(smp->attr_mod);
125         __be64 *p = (__be64 *) smp->data;
126
127         /* 32 blocks of 8 64-bit GUIDs per block */
128
129         memset(smp->data, 0, sizeof(smp->data));
130
131         /*
132          * We only support one GUID for now.  If this changes, the
133          * portinfo.guid_cap field needs to be updated too.
134          */
135         if (startgx == 0) {
136                 __be64 g = to_idev(ibdev)->dd->ipath_guid;
137                 if (g == 0)
138                         /* GUID 0 is illegal */
139                         smp->status |= IB_SMP_INVALID_FIELD;
140                 else
141                         /* The first is a copy of the read-only HW GUID. */
142                         *p = g;
143         } else
144                 smp->status |= IB_SMP_INVALID_FIELD;
145
146         return reply(smp);
147 }
148
149 static void set_link_width_enabled(struct ipath_devdata *dd, u32 w)
150 {
151         (void) dd->ipath_f_set_ib_cfg(dd, IPATH_IB_CFG_LWID_ENB, w);
152 }
153
154 static void set_link_speed_enabled(struct ipath_devdata *dd, u32 s)
155 {
156         (void) dd->ipath_f_set_ib_cfg(dd, IPATH_IB_CFG_SPD_ENB, s);
157 }
158
159 static int get_overrunthreshold(struct ipath_devdata *dd)
160 {
161         return (dd->ipath_ibcctrl >>
162                 INFINIPATH_IBCC_OVERRUNTHRESHOLD_SHIFT) &
163                 INFINIPATH_IBCC_OVERRUNTHRESHOLD_MASK;
164 }
165
166 /**
167  * set_overrunthreshold - set the overrun threshold
168  * @dd: the infinipath device
169  * @n: the new threshold
170  *
171  * Note that this will only take effect when the link state changes.
172  */
173 static int set_overrunthreshold(struct ipath_devdata *dd, unsigned n)
174 {
175         unsigned v;
176
177         v = (dd->ipath_ibcctrl >> INFINIPATH_IBCC_OVERRUNTHRESHOLD_SHIFT) &
178                 INFINIPATH_IBCC_OVERRUNTHRESHOLD_MASK;
179         if (v != n) {
180                 dd->ipath_ibcctrl &=
181                         ~(INFINIPATH_IBCC_OVERRUNTHRESHOLD_MASK <<
182                           INFINIPATH_IBCC_OVERRUNTHRESHOLD_SHIFT);
183                 dd->ipath_ibcctrl |=
184                         (u64) n << INFINIPATH_IBCC_OVERRUNTHRESHOLD_SHIFT;
185                 ipath_write_kreg(dd, dd->ipath_kregs->kr_ibcctrl,
186                                  dd->ipath_ibcctrl);
187         }
188         return 0;
189 }
190
191 static int get_phyerrthreshold(struct ipath_devdata *dd)
192 {
193         return (dd->ipath_ibcctrl >>
194                 INFINIPATH_IBCC_PHYERRTHRESHOLD_SHIFT) &
195                 INFINIPATH_IBCC_PHYERRTHRESHOLD_MASK;
196 }
197
198 /**
199  * set_phyerrthreshold - set the physical error threshold
200  * @dd: the infinipath device
201  * @n: the new threshold
202  *
203  * Note that this will only take effect when the link state changes.
204  */
205 static int set_phyerrthreshold(struct ipath_devdata *dd, unsigned n)
206 {
207         unsigned v;
208
209         v = (dd->ipath_ibcctrl >> INFINIPATH_IBCC_PHYERRTHRESHOLD_SHIFT) &
210                 INFINIPATH_IBCC_PHYERRTHRESHOLD_MASK;
211         if (v != n) {
212                 dd->ipath_ibcctrl &=
213                         ~(INFINIPATH_IBCC_PHYERRTHRESHOLD_MASK <<
214                           INFINIPATH_IBCC_PHYERRTHRESHOLD_SHIFT);
215                 dd->ipath_ibcctrl |=
216                         (u64) n << INFINIPATH_IBCC_PHYERRTHRESHOLD_SHIFT;
217                 ipath_write_kreg(dd, dd->ipath_kregs->kr_ibcctrl,
218                                  dd->ipath_ibcctrl);
219         }
220         return 0;
221 }
222
223 /**
224  * get_linkdowndefaultstate - get the default linkdown state
225  * @dd: the infinipath device
226  *
227  * Returns zero if the default is POLL, 1 if the default is SLEEP.
228  */
229 static int get_linkdowndefaultstate(struct ipath_devdata *dd)
230 {
231         return !!(dd->ipath_ibcctrl & INFINIPATH_IBCC_LINKDOWNDEFAULTSTATE);
232 }
233
234 static int recv_subn_get_portinfo(struct ib_smp *smp,
235                                   struct ib_device *ibdev, u8 port)
236 {
237         struct ipath_ibdev *dev;
238         struct ipath_devdata *dd;
239         struct ib_port_info *pip = (struct ib_port_info *)smp->data;
240         u16 lid;
241         u8 ibcstat;
242         u8 mtu;
243         int ret;
244
245         if (be32_to_cpu(smp->attr_mod) > ibdev->phys_port_cnt) {
246                 smp->status |= IB_SMP_INVALID_FIELD;
247                 ret = reply(smp);
248                 goto bail;
249         }
250
251         dev = to_idev(ibdev);
252         dd = dev->dd;
253
254         /* Clear all fields.  Only set the non-zero fields. */
255         memset(smp->data, 0, sizeof(smp->data));
256
257         /* Only return the mkey if the protection field allows it. */
258         if (smp->method == IB_MGMT_METHOD_SET || dev->mkey == smp->mkey ||
259             dev->mkeyprot == 0)
260                 pip->mkey = dev->mkey;
261         pip->gid_prefix = dev->gid_prefix;
262         lid = dd->ipath_lid;
263         pip->lid = lid ? cpu_to_be16(lid) : IB_LID_PERMISSIVE;
264         pip->sm_lid = cpu_to_be16(dev->sm_lid);
265         pip->cap_mask = cpu_to_be32(dev->port_cap_flags);
266         /* pip->diag_code; */
267         pip->mkey_lease_period = cpu_to_be16(dev->mkey_lease_period);
268         pip->local_port_num = port;
269         pip->link_width_enabled = dd->ipath_link_width_enabled;
270         pip->link_width_supported = dd->ipath_link_width_supported;
271         pip->link_width_active = dd->ipath_link_width_active;
272         pip->linkspeed_portstate = dd->ipath_link_speed_supported << 4;
273         ibcstat = dd->ipath_lastibcstat;
274         /* map LinkState to IB portinfo values.  */
275         pip->linkspeed_portstate |= ipath_ib_linkstate(dd, ibcstat) + 1;
276
277         pip->portphysstate_linkdown =
278                 (ipath_cvt_physportstate[ibcstat & dd->ibcs_lts_mask] << 4) |
279                 (get_linkdowndefaultstate(dd) ? 1 : 2);
280         pip->mkeyprot_resv_lmc = (dev->mkeyprot << 6) | dd->ipath_lmc;
281         pip->linkspeedactive_enabled = (dd->ipath_link_speed_active << 4) |
282                 dd->ipath_link_speed_enabled;
283         switch (dd->ipath_ibmtu) {
284         case 4096:
285                 mtu = IB_MTU_4096;
286                 break;
287         case 2048:
288                 mtu = IB_MTU_2048;
289                 break;
290         case 1024:
291                 mtu = IB_MTU_1024;
292                 break;
293         case 512:
294                 mtu = IB_MTU_512;
295                 break;
296         case 256:
297                 mtu = IB_MTU_256;
298                 break;
299         default:                /* oops, something is wrong */
300                 mtu = IB_MTU_2048;
301                 break;
302         }
303         pip->neighbormtu_mastersmsl = (mtu << 4) | dev->sm_sl;
304         pip->vlcap_inittype = 0x10;     /* VLCap = VL0, InitType = 0 */
305         pip->vl_high_limit = dev->vl_high_limit;
306         /* pip->vl_arb_high_cap; // only one VL */
307         /* pip->vl_arb_low_cap; // only one VL */
308         /* InitTypeReply = 0 */
309         /* our mtu cap depends on whether 4K MTU enabled or not */
310         pip->inittypereply_mtucap = ipath_mtu4096 ? IB_MTU_4096 : IB_MTU_2048;
311         /* HCAs ignore VLStallCount and HOQLife */
312         /* pip->vlstallcnt_hoqlife; */
313         pip->operationalvl_pei_peo_fpi_fpo = 0x10;      /* OVLs = 1 */
314         pip->mkey_violations = cpu_to_be16(dev->mkey_violations);
315         /* P_KeyViolations are counted by hardware. */
316         pip->pkey_violations =
317                 cpu_to_be16((ipath_get_cr_errpkey(dd) -
318                              dev->z_pkey_violations) & 0xFFFF);
319         pip->qkey_violations = cpu_to_be16(dev->qkey_violations);
320         /* Only the hardware GUID is supported for now */
321         pip->guid_cap = 1;
322         pip->clientrereg_resv_subnetto = dev->subnet_timeout;
323         /* 32.768 usec. response time (guessing) */
324         pip->resv_resptimevalue = 3;
325         pip->localphyerrors_overrunerrors =
326                 (get_phyerrthreshold(dd) << 4) |
327                 get_overrunthreshold(dd);
328         /* pip->max_credit_hint; */
329         if (dev->port_cap_flags & IB_PORT_LINK_LATENCY_SUP) {
330                 u32 v;
331
332                 v = dd->ipath_f_get_ib_cfg(dd, IPATH_IB_CFG_LINKLATENCY);
333                 pip->link_roundtrip_latency[0] = v >> 16;
334                 pip->link_roundtrip_latency[1] = v >> 8;
335                 pip->link_roundtrip_latency[2] = v;
336         }
337
338         ret = reply(smp);
339
340 bail:
341         return ret;
342 }
343
344 /**
345  * get_pkeys - return the PKEY table for port 0
346  * @dd: the infinipath device
347  * @pkeys: the pkey table is placed here
348  */
349 static int get_pkeys(struct ipath_devdata *dd, u16 * pkeys)
350 {
351         struct ipath_portdata *pd = dd->ipath_pd[0];
352
353         memcpy(pkeys, pd->port_pkeys, sizeof(pd->port_pkeys));
354
355         return 0;
356 }
357
358 static int recv_subn_get_pkeytable(struct ib_smp *smp,
359                                    struct ib_device *ibdev)
360 {
361         u32 startpx = 32 * (be32_to_cpu(smp->attr_mod) & 0xffff);
362         u16 *p = (u16 *) smp->data;
363         __be16 *q = (__be16 *) smp->data;
364
365         /* 64 blocks of 32 16-bit P_Key entries */
366
367         memset(smp->data, 0, sizeof(smp->data));
368         if (startpx == 0) {
369                 struct ipath_ibdev *dev = to_idev(ibdev);
370                 unsigned i, n = ipath_get_npkeys(dev->dd);
371
372                 get_pkeys(dev->dd, p);
373
374                 for (i = 0; i < n; i++)
375                         q[i] = cpu_to_be16(p[i]);
376         } else
377                 smp->status |= IB_SMP_INVALID_FIELD;
378
379         return reply(smp);
380 }
381
382 static int recv_subn_set_guidinfo(struct ib_smp *smp,
383                                   struct ib_device *ibdev)
384 {
385         /* The only GUID we support is the first read-only entry. */
386         return recv_subn_get_guidinfo(smp, ibdev);
387 }
388
389 /**
390  * set_linkdowndefaultstate - set the default linkdown state
391  * @dd: the infinipath device
392  * @sleep: the new state
393  *
394  * Note that this will only take effect when the link state changes.
395  */
396 static int set_linkdowndefaultstate(struct ipath_devdata *dd, int sleep)
397 {
398         if (sleep)
399                 dd->ipath_ibcctrl |= INFINIPATH_IBCC_LINKDOWNDEFAULTSTATE;
400         else
401                 dd->ipath_ibcctrl &= ~INFINIPATH_IBCC_LINKDOWNDEFAULTSTATE;
402         ipath_write_kreg(dd, dd->ipath_kregs->kr_ibcctrl,
403                          dd->ipath_ibcctrl);
404         return 0;
405 }
406
407 /**
408  * recv_subn_set_portinfo - set port information
409  * @smp: the incoming SM packet
410  * @ibdev: the infiniband device
411  * @port: the port on the device
412  *
413  * Set Portinfo (see ch. 14.2.5.6).
414  */
415 static int recv_subn_set_portinfo(struct ib_smp *smp,
416                                   struct ib_device *ibdev, u8 port)
417 {
418         struct ib_port_info *pip = (struct ib_port_info *)smp->data;
419         struct ib_event event;
420         struct ipath_ibdev *dev;
421         struct ipath_devdata *dd;
422         char clientrereg = 0;
423         u16 lid, smlid;
424         u8 lwe;
425         u8 lse;
426         u8 state;
427         u16 lstate;
428         u32 mtu;
429         int ret, ore;
430
431         if (be32_to_cpu(smp->attr_mod) > ibdev->phys_port_cnt)
432                 goto err;
433
434         dev = to_idev(ibdev);
435         dd = dev->dd;
436         event.device = ibdev;
437         event.element.port_num = port;
438
439         dev->mkey = pip->mkey;
440         dev->gid_prefix = pip->gid_prefix;
441         dev->mkey_lease_period = be16_to_cpu(pip->mkey_lease_period);
442
443         lid = be16_to_cpu(pip->lid);
444         if (dd->ipath_lid != lid ||
445             dd->ipath_lmc != (pip->mkeyprot_resv_lmc & 7)) {
446                 /* Must be a valid unicast LID address. */
447                 if (lid == 0 || lid >= IPATH_MULTICAST_LID_BASE)
448                         goto err;
449                 ipath_set_lid(dd, lid, pip->mkeyprot_resv_lmc & 7);
450                 event.event = IB_EVENT_LID_CHANGE;
451                 ib_dispatch_event(&event);
452         }
453
454         smlid = be16_to_cpu(pip->sm_lid);
455         if (smlid != dev->sm_lid) {
456                 /* Must be a valid unicast LID address. */
457                 if (smlid == 0 || smlid >= IPATH_MULTICAST_LID_BASE)
458                         goto err;
459                 dev->sm_lid = smlid;
460                 event.event = IB_EVENT_SM_CHANGE;
461                 ib_dispatch_event(&event);
462         }
463
464         /* Allow 1x or 4x to be set (see 14.2.6.6). */
465         lwe = pip->link_width_enabled;
466         if (lwe) {
467                 if (lwe == 0xFF)
468                         lwe = dd->ipath_link_width_supported;
469                 else if (lwe >= 16 || (lwe & ~dd->ipath_link_width_supported))
470                         goto err;
471                 set_link_width_enabled(dd, lwe);
472         }
473
474         /* Allow 2.5 or 5.0 Gbs. */
475         lse = pip->linkspeedactive_enabled & 0xF;
476         if (lse) {
477                 if (lse == 15)
478                         lse = dd->ipath_link_speed_supported;
479                 else if (lse >= 8 || (lse & ~dd->ipath_link_speed_supported))
480                         goto err;
481                 set_link_speed_enabled(dd, lse);
482         }
483
484         /* Set link down default state. */
485         switch (pip->portphysstate_linkdown & 0xF) {
486         case 0: /* NOP */
487                 break;
488         case 1: /* SLEEP */
489                 if (set_linkdowndefaultstate(dd, 1))
490                         goto err;
491                 break;
492         case 2: /* POLL */
493                 if (set_linkdowndefaultstate(dd, 0))
494                         goto err;
495                 break;
496         default:
497                 goto err;
498         }
499
500         dev->mkeyprot = pip->mkeyprot_resv_lmc >> 6;
501         dev->vl_high_limit = pip->vl_high_limit;
502
503         switch ((pip->neighbormtu_mastersmsl >> 4) & 0xF) {
504         case IB_MTU_256:
505                 mtu = 256;
506                 break;
507         case IB_MTU_512:
508                 mtu = 512;
509                 break;
510         case IB_MTU_1024:
511                 mtu = 1024;
512                 break;
513         case IB_MTU_2048:
514                 mtu = 2048;
515                 break;
516         case IB_MTU_4096:
517                 if (!ipath_mtu4096)
518                         goto err;
519                 mtu = 4096;
520                 break;
521         default:
522                 /* XXX We have already partially updated our state! */
523                 goto err;
524         }
525         ipath_set_mtu(dd, mtu);
526
527         dev->sm_sl = pip->neighbormtu_mastersmsl & 0xF;
528
529         /* We only support VL0 */
530         if (((pip->operationalvl_pei_peo_fpi_fpo >> 4) & 0xF) > 1)
531                 goto err;
532
533         if (pip->mkey_violations == 0)
534                 dev->mkey_violations = 0;
535
536         /*
537          * Hardware counter can't be reset so snapshot and subtract
538          * later.
539          */
540         if (pip->pkey_violations == 0)
541                 dev->z_pkey_violations = ipath_get_cr_errpkey(dd);
542
543         if (pip->qkey_violations == 0)
544                 dev->qkey_violations = 0;
545
546         ore = pip->localphyerrors_overrunerrors;
547         if (set_phyerrthreshold(dd, (ore >> 4) & 0xF))
548                 goto err;
549
550         if (set_overrunthreshold(dd, (ore & 0xF)))
551                 goto err;
552
553         dev->subnet_timeout = pip->clientrereg_resv_subnetto & 0x1F;
554
555         if (pip->clientrereg_resv_subnetto & 0x80) {
556                 clientrereg = 1;
557                 event.event = IB_EVENT_CLIENT_REREGISTER;
558                 ib_dispatch_event(&event);
559         }
560
561         /*
562          * Do the port state change now that the other link parameters
563          * have been set.
564          * Changing the port physical state only makes sense if the link
565          * is down or is being set to down.
566          */
567         state = pip->linkspeed_portstate & 0xF;
568         lstate = (pip->portphysstate_linkdown >> 4) & 0xF;
569         if (lstate && !(state == IB_PORT_DOWN || state == IB_PORT_NOP))
570                 goto err;
571
572         /*
573          * Only state changes of DOWN, ARM, and ACTIVE are valid
574          * and must be in the correct state to take effect (see 7.2.6).
575          */
576         switch (state) {
577         case IB_PORT_NOP:
578                 if (lstate == 0)
579                         break;
580                 /* FALLTHROUGH */
581         case IB_PORT_DOWN:
582                 if (lstate == 0)
583                         lstate = IPATH_IB_LINKDOWN_ONLY;
584                 else if (lstate == 1)
585                         lstate = IPATH_IB_LINKDOWN_SLEEP;
586                 else if (lstate == 2)
587                         lstate = IPATH_IB_LINKDOWN;
588                 else if (lstate == 3)
589                         lstate = IPATH_IB_LINKDOWN_DISABLE;
590                 else
591                         goto err;
592                 ipath_set_linkstate(dd, lstate);
593                 if (lstate == IPATH_IB_LINKDOWN_DISABLE) {
594                         ret = IB_MAD_RESULT_SUCCESS | IB_MAD_RESULT_CONSUMED;
595                         goto done;
596                 }
597                 ipath_wait_linkstate(dd, IPATH_LINKINIT | IPATH_LINKARMED |
598                                 IPATH_LINKACTIVE, 1000);
599                 break;
600         case IB_PORT_ARMED:
601                 ipath_set_linkstate(dd, IPATH_IB_LINKARM);
602                 break;
603         case IB_PORT_ACTIVE:
604                 ipath_set_linkstate(dd, IPATH_IB_LINKACTIVE);
605                 break;
606         default:
607                 /* XXX We have already partially updated our state! */
608                 goto err;
609         }
610
611         ret = recv_subn_get_portinfo(smp, ibdev, port);
612
613         if (clientrereg)
614                 pip->clientrereg_resv_subnetto |= 0x80;
615
616         goto done;
617
618 err:
619         smp->status |= IB_SMP_INVALID_FIELD;
620         ret = recv_subn_get_portinfo(smp, ibdev, port);
621
622 done:
623         return ret;
624 }
625
626 /**
627  * rm_pkey - decrecment the reference count for the given PKEY
628  * @dd: the infinipath device
629  * @key: the PKEY index
630  *
631  * Return true if this was the last reference and the hardware table entry
632  * needs to be changed.
633  */
634 static int rm_pkey(struct ipath_devdata *dd, u16 key)
635 {
636         int i;
637         int ret;
638
639         for (i = 0; i < ARRAY_SIZE(dd->ipath_pkeys); i++) {
640                 if (dd->ipath_pkeys[i] != key)
641                         continue;
642                 if (atomic_dec_and_test(&dd->ipath_pkeyrefs[i])) {
643                         dd->ipath_pkeys[i] = 0;
644                         ret = 1;
645                         goto bail;
646                 }
647                 break;
648         }
649
650         ret = 0;
651
652 bail:
653         return ret;
654 }
655
656 /**
657  * add_pkey - add the given PKEY to the hardware table
658  * @dd: the infinipath device
659  * @key: the PKEY
660  *
661  * Return an error code if unable to add the entry, zero if no change,
662  * or 1 if the hardware PKEY register needs to be updated.
663  */
664 static int add_pkey(struct ipath_devdata *dd, u16 key)
665 {
666         int i;
667         u16 lkey = key & 0x7FFF;
668         int any = 0;
669         int ret;
670
671         if (lkey == 0x7FFF) {
672                 ret = 0;
673                 goto bail;
674         }
675
676         /* Look for an empty slot or a matching PKEY. */
677         for (i = 0; i < ARRAY_SIZE(dd->ipath_pkeys); i++) {
678                 if (!dd->ipath_pkeys[i]) {
679                         any++;
680                         continue;
681                 }
682                 /* If it matches exactly, try to increment the ref count */
683                 if (dd->ipath_pkeys[i] == key) {
684                         if (atomic_inc_return(&dd->ipath_pkeyrefs[i]) > 1) {
685                                 ret = 0;
686                                 goto bail;
687                         }
688                         /* Lost the race. Look for an empty slot below. */
689                         atomic_dec(&dd->ipath_pkeyrefs[i]);
690                         any++;
691                 }
692                 /*
693                  * It makes no sense to have both the limited and unlimited
694                  * PKEY set at the same time since the unlimited one will
695                  * disable the limited one.
696                  */
697                 if ((dd->ipath_pkeys[i] & 0x7FFF) == lkey) {
698                         ret = -EEXIST;
699                         goto bail;
700                 }
701         }
702         if (!any) {
703                 ret = -EBUSY;
704                 goto bail;
705         }
706         for (i = 0; i < ARRAY_SIZE(dd->ipath_pkeys); i++) {
707                 if (!dd->ipath_pkeys[i] &&
708                     atomic_inc_return(&dd->ipath_pkeyrefs[i]) == 1) {
709                         /* for ipathstats, etc. */
710                         ipath_stats.sps_pkeys[i] = lkey;
711                         dd->ipath_pkeys[i] = key;
712                         ret = 1;
713                         goto bail;
714                 }
715         }
716         ret = -EBUSY;
717
718 bail:
719         return ret;
720 }
721
722 /**
723  * set_pkeys - set the PKEY table for port 0
724  * @dd: the infinipath device
725  * @pkeys: the PKEY table
726  */
727 static int set_pkeys(struct ipath_devdata *dd, u16 *pkeys)
728 {
729         struct ipath_portdata *pd;
730         int i;
731         int changed = 0;
732
733         pd = dd->ipath_pd[0];
734
735         for (i = 0; i < ARRAY_SIZE(pd->port_pkeys); i++) {
736                 u16 key = pkeys[i];
737                 u16 okey = pd->port_pkeys[i];
738
739                 if (key == okey)
740                         continue;
741                 /*
742                  * The value of this PKEY table entry is changing.
743                  * Remove the old entry in the hardware's array of PKEYs.
744                  */
745                 if (okey & 0x7FFF)
746                         changed |= rm_pkey(dd, okey);
747                 if (key & 0x7FFF) {
748                         int ret = add_pkey(dd, key);
749
750                         if (ret < 0)
751                                 key = 0;
752                         else
753                                 changed |= ret;
754                 }
755                 pd->port_pkeys[i] = key;
756         }
757         if (changed) {
758                 u64 pkey;
759
760                 pkey = (u64) dd->ipath_pkeys[0] |
761                         ((u64) dd->ipath_pkeys[1] << 16) |
762                         ((u64) dd->ipath_pkeys[2] << 32) |
763                         ((u64) dd->ipath_pkeys[3] << 48);
764                 ipath_cdbg(VERBOSE, "p0 new pkey reg %llx\n",
765                            (unsigned long long) pkey);
766                 ipath_write_kreg(dd, dd->ipath_kregs->kr_partitionkey,
767                                  pkey);
768         }
769         return 0;
770 }
771
772 static int recv_subn_set_pkeytable(struct ib_smp *smp,
773                                    struct ib_device *ibdev)
774 {
775         u32 startpx = 32 * (be32_to_cpu(smp->attr_mod) & 0xffff);
776         __be16 *p = (__be16 *) smp->data;
777         u16 *q = (u16 *) smp->data;
778         struct ipath_ibdev *dev = to_idev(ibdev);
779         unsigned i, n = ipath_get_npkeys(dev->dd);
780
781         for (i = 0; i < n; i++)
782                 q[i] = be16_to_cpu(p[i]);
783
784         if (startpx != 0 || set_pkeys(dev->dd, q) != 0)
785                 smp->status |= IB_SMP_INVALID_FIELD;
786
787         return recv_subn_get_pkeytable(smp, ibdev);
788 }
789
790 #define IB_PMA_CLASS_PORT_INFO          __constant_htons(0x0001)
791 #define IB_PMA_PORT_SAMPLES_CONTROL     __constant_htons(0x0010)
792 #define IB_PMA_PORT_SAMPLES_RESULT      __constant_htons(0x0011)
793 #define IB_PMA_PORT_COUNTERS            __constant_htons(0x0012)
794 #define IB_PMA_PORT_COUNTERS_EXT        __constant_htons(0x001D)
795 #define IB_PMA_PORT_SAMPLES_RESULT_EXT  __constant_htons(0x001E)
796
797 struct ib_perf {
798         u8 base_version;
799         u8 mgmt_class;
800         u8 class_version;
801         u8 method;
802         __be16 status;
803         __be16 unused;
804         __be64 tid;
805         __be16 attr_id;
806         __be16 resv;
807         __be32 attr_mod;
808         u8 reserved[40];
809         u8 data[192];
810 } __attribute__ ((packed));
811
812 struct ib_pma_classportinfo {
813         u8 base_version;
814         u8 class_version;
815         __be16 cap_mask;
816         u8 reserved[3];
817         u8 resp_time_value;     /* only lower 5 bits */
818         union ib_gid redirect_gid;
819         __be32 redirect_tc_sl_fl;       /* 8, 4, 20 bits respectively */
820         __be16 redirect_lid;
821         __be16 redirect_pkey;
822         __be32 redirect_qp;     /* only lower 24 bits */
823         __be32 redirect_qkey;
824         union ib_gid trap_gid;
825         __be32 trap_tc_sl_fl;   /* 8, 4, 20 bits respectively */
826         __be16 trap_lid;
827         __be16 trap_pkey;
828         __be32 trap_hl_qp;      /* 8, 24 bits respectively */
829         __be32 trap_qkey;
830 } __attribute__ ((packed));
831
832 struct ib_pma_portsamplescontrol {
833         u8 opcode;
834         u8 port_select;
835         u8 tick;
836         u8 counter_width;       /* only lower 3 bits */
837         __be32 counter_mask0_9; /* 2, 10 * 3, bits */
838         __be16 counter_mask10_14;       /* 1, 5 * 3, bits */
839         u8 sample_mechanisms;
840         u8 sample_status;       /* only lower 2 bits */
841         __be64 option_mask;
842         __be64 vendor_mask;
843         __be32 sample_start;
844         __be32 sample_interval;
845         __be16 tag;
846         __be16 counter_select[15];
847 } __attribute__ ((packed));
848
849 struct ib_pma_portsamplesresult {
850         __be16 tag;
851         __be16 sample_status;   /* only lower 2 bits */
852         __be32 counter[15];
853 } __attribute__ ((packed));
854
855 struct ib_pma_portsamplesresult_ext {
856         __be16 tag;
857         __be16 sample_status;   /* only lower 2 bits */
858         __be32 extended_width;  /* only upper 2 bits */
859         __be64 counter[15];
860 } __attribute__ ((packed));
861
862 struct ib_pma_portcounters {
863         u8 reserved;
864         u8 port_select;
865         __be16 counter_select;
866         __be16 symbol_error_counter;
867         u8 link_error_recovery_counter;
868         u8 link_downed_counter;
869         __be16 port_rcv_errors;
870         __be16 port_rcv_remphys_errors;
871         __be16 port_rcv_switch_relay_errors;
872         __be16 port_xmit_discards;
873         u8 port_xmit_constraint_errors;
874         u8 port_rcv_constraint_errors;
875         u8 reserved1;
876         u8 lli_ebor_errors;     /* 4, 4, bits */
877         __be16 reserved2;
878         __be16 vl15_dropped;
879         __be32 port_xmit_data;
880         __be32 port_rcv_data;
881         __be32 port_xmit_packets;
882         __be32 port_rcv_packets;
883 } __attribute__ ((packed));
884
885 #define IB_PMA_SEL_SYMBOL_ERROR                 __constant_htons(0x0001)
886 #define IB_PMA_SEL_LINK_ERROR_RECOVERY          __constant_htons(0x0002)
887 #define IB_PMA_SEL_LINK_DOWNED                  __constant_htons(0x0004)
888 #define IB_PMA_SEL_PORT_RCV_ERRORS              __constant_htons(0x0008)
889 #define IB_PMA_SEL_PORT_RCV_REMPHYS_ERRORS      __constant_htons(0x0010)
890 #define IB_PMA_SEL_PORT_XMIT_DISCARDS           __constant_htons(0x0040)
891 #define IB_PMA_SEL_LOCAL_LINK_INTEGRITY_ERRORS  __constant_htons(0x0200)
892 #define IB_PMA_SEL_EXCESSIVE_BUFFER_OVERRUNS    __constant_htons(0x0400)
893 #define IB_PMA_SEL_PORT_VL15_DROPPED            __constant_htons(0x0800)
894 #define IB_PMA_SEL_PORT_XMIT_DATA               __constant_htons(0x1000)
895 #define IB_PMA_SEL_PORT_RCV_DATA                __constant_htons(0x2000)
896 #define IB_PMA_SEL_PORT_XMIT_PACKETS            __constant_htons(0x4000)
897 #define IB_PMA_SEL_PORT_RCV_PACKETS             __constant_htons(0x8000)
898
899 struct ib_pma_portcounters_ext {
900         u8 reserved;
901         u8 port_select;
902         __be16 counter_select;
903         __be32 reserved1;
904         __be64 port_xmit_data;
905         __be64 port_rcv_data;
906         __be64 port_xmit_packets;
907         __be64 port_rcv_packets;
908         __be64 port_unicast_xmit_packets;
909         __be64 port_unicast_rcv_packets;
910         __be64 port_multicast_xmit_packets;
911         __be64 port_multicast_rcv_packets;
912 } __attribute__ ((packed));
913
914 #define IB_PMA_SELX_PORT_XMIT_DATA              __constant_htons(0x0001)
915 #define IB_PMA_SELX_PORT_RCV_DATA               __constant_htons(0x0002)
916 #define IB_PMA_SELX_PORT_XMIT_PACKETS           __constant_htons(0x0004)
917 #define IB_PMA_SELX_PORT_RCV_PACKETS            __constant_htons(0x0008)
918 #define IB_PMA_SELX_PORT_UNI_XMIT_PACKETS       __constant_htons(0x0010)
919 #define IB_PMA_SELX_PORT_UNI_RCV_PACKETS        __constant_htons(0x0020)
920 #define IB_PMA_SELX_PORT_MULTI_XMIT_PACKETS     __constant_htons(0x0040)
921 #define IB_PMA_SELX_PORT_MULTI_RCV_PACKETS      __constant_htons(0x0080)
922
923 static int recv_pma_get_classportinfo(struct ib_perf *pmp)
924 {
925         struct ib_pma_classportinfo *p =
926                 (struct ib_pma_classportinfo *)pmp->data;
927
928         memset(pmp->data, 0, sizeof(pmp->data));
929
930         if (pmp->attr_mod != 0)
931                 pmp->status |= IB_SMP_INVALID_FIELD;
932
933         /* Indicate AllPortSelect is valid (only one port anyway) */
934         p->cap_mask = __constant_cpu_to_be16(1 << 8);
935         p->base_version = 1;
936         p->class_version = 1;
937         /*
938          * Expected response time is 4.096 usec. * 2^18 == 1.073741824
939          * sec.
940          */
941         p->resp_time_value = 18;
942
943         return reply((struct ib_smp *) pmp);
944 }
945
946 /*
947  * The PortSamplesControl.CounterMasks field is an array of 3 bit fields
948  * which specify the N'th counter's capabilities. See ch. 16.1.3.2.
949  * We support 5 counters which only count the mandatory quantities.
950  */
951 #define COUNTER_MASK(q, n) (q << ((9 - n) * 3))
952 #define COUNTER_MASK0_9 \
953         __constant_cpu_to_be32(COUNTER_MASK(1, 0) | \
954                                COUNTER_MASK(1, 1) | \
955                                COUNTER_MASK(1, 2) | \
956                                COUNTER_MASK(1, 3) | \
957                                COUNTER_MASK(1, 4))
958
959 static int recv_pma_get_portsamplescontrol(struct ib_perf *pmp,
960                                            struct ib_device *ibdev, u8 port)
961 {
962         struct ib_pma_portsamplescontrol *p =
963                 (struct ib_pma_portsamplescontrol *)pmp->data;
964         struct ipath_ibdev *dev = to_idev(ibdev);
965         struct ipath_cregs const *crp = dev->dd->ipath_cregs;
966         unsigned long flags;
967         u8 port_select = p->port_select;
968
969         memset(pmp->data, 0, sizeof(pmp->data));
970
971         p->port_select = port_select;
972         if (pmp->attr_mod != 0 ||
973             (port_select != port && port_select != 0xFF))
974                 pmp->status |= IB_SMP_INVALID_FIELD;
975         /*
976          * Ticks are 10x the link transfer period which for 2.5Gbs is 4
977          * nsec.  0 == 4 nsec., 1 == 8 nsec., ..., 255 == 1020 nsec.  Sample
978          * intervals are counted in ticks.  Since we use Linux timers, that
979          * count in jiffies, we can't sample for less than 1000 ticks if HZ
980          * == 1000 (4000 ticks if HZ is 250).  link_speed_active returns 2 for
981          * DDR, 1 for SDR, set the tick to 1 for DDR, 0 for SDR on chips that
982          * have hardware support for delaying packets.
983          */
984         if (crp->cr_psstat)
985                 p->tick = dev->dd->ipath_link_speed_active - 1;
986         else
987                 p->tick = 250;          /* 1 usec. */
988         p->counter_width = 4;   /* 32 bit counters */
989         p->counter_mask0_9 = COUNTER_MASK0_9;
990         spin_lock_irqsave(&dev->pending_lock, flags);
991         if (crp->cr_psstat)
992                 p->sample_status = ipath_read_creg32(dev->dd, crp->cr_psstat);
993         else
994                 p->sample_status = dev->pma_sample_status;
995         p->sample_start = cpu_to_be32(dev->pma_sample_start);
996         p->sample_interval = cpu_to_be32(dev->pma_sample_interval);
997         p->tag = cpu_to_be16(dev->pma_tag);
998         p->counter_select[0] = dev->pma_counter_select[0];
999         p->counter_select[1] = dev->pma_counter_select[1];
1000         p->counter_select[2] = dev->pma_counter_select[2];
1001         p->counter_select[3] = dev->pma_counter_select[3];
1002         p->counter_select[4] = dev->pma_counter_select[4];
1003         spin_unlock_irqrestore(&dev->pending_lock, flags);
1004
1005         return reply((struct ib_smp *) pmp);
1006 }
1007
1008 static int recv_pma_set_portsamplescontrol(struct ib_perf *pmp,
1009                                            struct ib_device *ibdev, u8 port)
1010 {
1011         struct ib_pma_portsamplescontrol *p =
1012                 (struct ib_pma_portsamplescontrol *)pmp->data;
1013         struct ipath_ibdev *dev = to_idev(ibdev);
1014         struct ipath_cregs const *crp = dev->dd->ipath_cregs;
1015         unsigned long flags;
1016         u8 status;
1017         int ret;
1018
1019         if (pmp->attr_mod != 0 ||
1020             (p->port_select != port && p->port_select != 0xFF)) {
1021                 pmp->status |= IB_SMP_INVALID_FIELD;
1022                 ret = reply((struct ib_smp *) pmp);
1023                 goto bail;
1024         }
1025
1026         spin_lock_irqsave(&dev->pending_lock, flags);
1027         if (crp->cr_psstat)
1028                 status = ipath_read_creg32(dev->dd, crp->cr_psstat);
1029         else
1030                 status = dev->pma_sample_status;
1031         if (status == IB_PMA_SAMPLE_STATUS_DONE) {
1032                 dev->pma_sample_start = be32_to_cpu(p->sample_start);
1033                 dev->pma_sample_interval = be32_to_cpu(p->sample_interval);
1034                 dev->pma_tag = be16_to_cpu(p->tag);
1035                 dev->pma_counter_select[0] = p->counter_select[0];
1036                 dev->pma_counter_select[1] = p->counter_select[1];
1037                 dev->pma_counter_select[2] = p->counter_select[2];
1038                 dev->pma_counter_select[3] = p->counter_select[3];
1039                 dev->pma_counter_select[4] = p->counter_select[4];
1040                 if (crp->cr_psstat) {
1041                         ipath_write_creg(dev->dd, crp->cr_psinterval,
1042                                          dev->pma_sample_interval);
1043                         ipath_write_creg(dev->dd, crp->cr_psstart,
1044                                          dev->pma_sample_start);
1045                 } else
1046                         dev->pma_sample_status = IB_PMA_SAMPLE_STATUS_STARTED;
1047         }
1048         spin_unlock_irqrestore(&dev->pending_lock, flags);
1049
1050         ret = recv_pma_get_portsamplescontrol(pmp, ibdev, port);
1051
1052 bail:
1053         return ret;
1054 }
1055
1056 static u64 get_counter(struct ipath_ibdev *dev,
1057                        struct ipath_cregs const *crp,
1058                        __be16 sel)
1059 {
1060         u64 ret;
1061
1062         switch (sel) {
1063         case IB_PMA_PORT_XMIT_DATA:
1064                 ret = (crp->cr_psxmitdatacount) ?
1065                         ipath_read_creg32(dev->dd, crp->cr_psxmitdatacount) :
1066                         dev->ipath_sword;
1067                 break;
1068         case IB_PMA_PORT_RCV_DATA:
1069                 ret = (crp->cr_psrcvdatacount) ?
1070                         ipath_read_creg32(dev->dd, crp->cr_psrcvdatacount) :
1071                         dev->ipath_rword;
1072                 break;
1073         case IB_PMA_PORT_XMIT_PKTS:
1074                 ret = (crp->cr_psxmitpktscount) ?
1075                         ipath_read_creg32(dev->dd, crp->cr_psxmitpktscount) :
1076                         dev->ipath_spkts;
1077                 break;
1078         case IB_PMA_PORT_RCV_PKTS:
1079                 ret = (crp->cr_psrcvpktscount) ?
1080                         ipath_read_creg32(dev->dd, crp->cr_psrcvpktscount) :
1081                         dev->ipath_rpkts;
1082                 break;
1083         case IB_PMA_PORT_XMIT_WAIT:
1084                 ret = (crp->cr_psxmitwaitcount) ?
1085                         ipath_read_creg32(dev->dd, crp->cr_psxmitwaitcount) :
1086                         dev->ipath_xmit_wait;
1087                 break;
1088         default:
1089                 ret = 0;
1090         }
1091
1092         return ret;
1093 }
1094
1095 static int recv_pma_get_portsamplesresult(struct ib_perf *pmp,
1096                                           struct ib_device *ibdev)
1097 {
1098         struct ib_pma_portsamplesresult *p =
1099                 (struct ib_pma_portsamplesresult *)pmp->data;
1100         struct ipath_ibdev *dev = to_idev(ibdev);
1101         struct ipath_cregs const *crp = dev->dd->ipath_cregs;
1102         u8 status;
1103         int i;
1104
1105         memset(pmp->data, 0, sizeof(pmp->data));
1106         p->tag = cpu_to_be16(dev->pma_tag);
1107         if (crp->cr_psstat)
1108                 status = ipath_read_creg32(dev->dd, crp->cr_psstat);
1109         else
1110                 status = dev->pma_sample_status;
1111         p->sample_status = cpu_to_be16(status);
1112         for (i = 0; i < ARRAY_SIZE(dev->pma_counter_select); i++)
1113                 p->counter[i] = (status != IB_PMA_SAMPLE_STATUS_DONE) ? 0 :
1114                     cpu_to_be32(
1115                         get_counter(dev, crp, dev->pma_counter_select[i]));
1116
1117         return reply((struct ib_smp *) pmp);
1118 }
1119
1120 static int recv_pma_get_portsamplesresult_ext(struct ib_perf *pmp,
1121                                               struct ib_device *ibdev)
1122 {
1123         struct ib_pma_portsamplesresult_ext *p =
1124                 (struct ib_pma_portsamplesresult_ext *)pmp->data;
1125         struct ipath_ibdev *dev = to_idev(ibdev);
1126         struct ipath_cregs const *crp = dev->dd->ipath_cregs;
1127         u8 status;
1128         int i;
1129
1130         memset(pmp->data, 0, sizeof(pmp->data));
1131         p->tag = cpu_to_be16(dev->pma_tag);
1132         if (crp->cr_psstat)
1133                 status = ipath_read_creg32(dev->dd, crp->cr_psstat);
1134         else
1135                 status = dev->pma_sample_status;
1136         p->sample_status = cpu_to_be16(status);
1137         /* 64 bits */
1138         p->extended_width = __constant_cpu_to_be32(0x80000000);
1139         for (i = 0; i < ARRAY_SIZE(dev->pma_counter_select); i++)
1140                 p->counter[i] = (status != IB_PMA_SAMPLE_STATUS_DONE) ? 0 :
1141                     cpu_to_be64(
1142                         get_counter(dev, crp, dev->pma_counter_select[i]));
1143
1144         return reply((struct ib_smp *) pmp);
1145 }
1146
1147 static int recv_pma_get_portcounters(struct ib_perf *pmp,
1148                                      struct ib_device *ibdev, u8 port)
1149 {
1150         struct ib_pma_portcounters *p = (struct ib_pma_portcounters *)
1151                 pmp->data;
1152         struct ipath_ibdev *dev = to_idev(ibdev);
1153         struct ipath_verbs_counters cntrs;
1154         u8 port_select = p->port_select;
1155
1156         ipath_get_counters(dev->dd, &cntrs);
1157
1158         /* Adjust counters for any resets done. */
1159         cntrs.symbol_error_counter -= dev->z_symbol_error_counter;
1160         cntrs.link_error_recovery_counter -=
1161                 dev->z_link_error_recovery_counter;
1162         cntrs.link_downed_counter -= dev->z_link_downed_counter;
1163         cntrs.port_rcv_errors += dev->rcv_errors;
1164         cntrs.port_rcv_errors -= dev->z_port_rcv_errors;
1165         cntrs.port_rcv_remphys_errors -= dev->z_port_rcv_remphys_errors;
1166         cntrs.port_xmit_discards -= dev->z_port_xmit_discards;
1167         cntrs.port_xmit_data -= dev->z_port_xmit_data;
1168         cntrs.port_rcv_data -= dev->z_port_rcv_data;
1169         cntrs.port_xmit_packets -= dev->z_port_xmit_packets;
1170         cntrs.port_rcv_packets -= dev->z_port_rcv_packets;
1171         cntrs.local_link_integrity_errors -=
1172                 dev->z_local_link_integrity_errors;
1173         cntrs.excessive_buffer_overrun_errors -=
1174                 dev->z_excessive_buffer_overrun_errors;
1175         cntrs.vl15_dropped -= dev->z_vl15_dropped;
1176         cntrs.vl15_dropped += dev->n_vl15_dropped;
1177
1178         memset(pmp->data, 0, sizeof(pmp->data));
1179
1180         p->port_select = port_select;
1181         if (pmp->attr_mod != 0 ||
1182             (port_select != port && port_select != 0xFF))
1183                 pmp->status |= IB_SMP_INVALID_FIELD;
1184
1185         if (cntrs.symbol_error_counter > 0xFFFFUL)
1186                 p->symbol_error_counter = __constant_cpu_to_be16(0xFFFF);
1187         else
1188                 p->symbol_error_counter =
1189                         cpu_to_be16((u16)cntrs.symbol_error_counter);
1190         if (cntrs.link_error_recovery_counter > 0xFFUL)
1191                 p->link_error_recovery_counter = 0xFF;
1192         else
1193                 p->link_error_recovery_counter =
1194                         (u8)cntrs.link_error_recovery_counter;
1195         if (cntrs.link_downed_counter > 0xFFUL)
1196                 p->link_downed_counter = 0xFF;
1197         else
1198                 p->link_downed_counter = (u8)cntrs.link_downed_counter;
1199         if (cntrs.port_rcv_errors > 0xFFFFUL)
1200                 p->port_rcv_errors = __constant_cpu_to_be16(0xFFFF);
1201         else
1202                 p->port_rcv_errors =
1203                         cpu_to_be16((u16) cntrs.port_rcv_errors);
1204         if (cntrs.port_rcv_remphys_errors > 0xFFFFUL)
1205                 p->port_rcv_remphys_errors = __constant_cpu_to_be16(0xFFFF);
1206         else
1207                 p->port_rcv_remphys_errors =
1208                         cpu_to_be16((u16)cntrs.port_rcv_remphys_errors);
1209         if (cntrs.port_xmit_discards > 0xFFFFUL)
1210                 p->port_xmit_discards = __constant_cpu_to_be16(0xFFFF);
1211         else
1212                 p->port_xmit_discards =
1213                         cpu_to_be16((u16)cntrs.port_xmit_discards);
1214         if (cntrs.local_link_integrity_errors > 0xFUL)
1215                 cntrs.local_link_integrity_errors = 0xFUL;
1216         if (cntrs.excessive_buffer_overrun_errors > 0xFUL)
1217                 cntrs.excessive_buffer_overrun_errors = 0xFUL;
1218         p->lli_ebor_errors = (cntrs.local_link_integrity_errors << 4) |
1219                 cntrs.excessive_buffer_overrun_errors;
1220         if (cntrs.vl15_dropped > 0xFFFFUL)
1221                 p->vl15_dropped = __constant_cpu_to_be16(0xFFFF);
1222         else
1223                 p->vl15_dropped = cpu_to_be16((u16)cntrs.vl15_dropped);
1224         if (cntrs.port_xmit_data > 0xFFFFFFFFUL)
1225                 p->port_xmit_data = __constant_cpu_to_be32(0xFFFFFFFF);
1226         else
1227                 p->port_xmit_data = cpu_to_be32((u32)cntrs.port_xmit_data);
1228         if (cntrs.port_rcv_data > 0xFFFFFFFFUL)
1229                 p->port_rcv_data = __constant_cpu_to_be32(0xFFFFFFFF);
1230         else
1231                 p->port_rcv_data = cpu_to_be32((u32)cntrs.port_rcv_data);
1232         if (cntrs.port_xmit_packets > 0xFFFFFFFFUL)
1233                 p->port_xmit_packets = __constant_cpu_to_be32(0xFFFFFFFF);
1234         else
1235                 p->port_xmit_packets =
1236                         cpu_to_be32((u32)cntrs.port_xmit_packets);
1237         if (cntrs.port_rcv_packets > 0xFFFFFFFFUL)
1238                 p->port_rcv_packets = __constant_cpu_to_be32(0xFFFFFFFF);
1239         else
1240                 p->port_rcv_packets =
1241                         cpu_to_be32((u32) cntrs.port_rcv_packets);
1242
1243         return reply((struct ib_smp *) pmp);
1244 }
1245
1246 static int recv_pma_get_portcounters_ext(struct ib_perf *pmp,
1247                                          struct ib_device *ibdev, u8 port)
1248 {
1249         struct ib_pma_portcounters_ext *p =
1250                 (struct ib_pma_portcounters_ext *)pmp->data;
1251         struct ipath_ibdev *dev = to_idev(ibdev);
1252         u64 swords, rwords, spkts, rpkts, xwait;
1253         u8 port_select = p->port_select;
1254
1255         ipath_snapshot_counters(dev->dd, &swords, &rwords, &spkts,
1256                                 &rpkts, &xwait);
1257
1258         /* Adjust counters for any resets done. */
1259         swords -= dev->z_port_xmit_data;
1260         rwords -= dev->z_port_rcv_data;
1261         spkts -= dev->z_port_xmit_packets;
1262         rpkts -= dev->z_port_rcv_packets;
1263
1264         memset(pmp->data, 0, sizeof(pmp->data));
1265
1266         p->port_select = port_select;
1267         if (pmp->attr_mod != 0 ||
1268             (port_select != port && port_select != 0xFF))
1269                 pmp->status |= IB_SMP_INVALID_FIELD;
1270
1271         p->port_xmit_data = cpu_to_be64(swords);
1272         p->port_rcv_data = cpu_to_be64(rwords);
1273         p->port_xmit_packets = cpu_to_be64(spkts);
1274         p->port_rcv_packets = cpu_to_be64(rpkts);
1275         p->port_unicast_xmit_packets = cpu_to_be64(dev->n_unicast_xmit);
1276         p->port_unicast_rcv_packets = cpu_to_be64(dev->n_unicast_rcv);
1277         p->port_multicast_xmit_packets = cpu_to_be64(dev->n_multicast_xmit);
1278         p->port_multicast_rcv_packets = cpu_to_be64(dev->n_multicast_rcv);
1279
1280         return reply((struct ib_smp *) pmp);
1281 }
1282
1283 static int recv_pma_set_portcounters(struct ib_perf *pmp,
1284                                      struct ib_device *ibdev, u8 port)
1285 {
1286         struct ib_pma_portcounters *p = (struct ib_pma_portcounters *)
1287                 pmp->data;
1288         struct ipath_ibdev *dev = to_idev(ibdev);
1289         struct ipath_verbs_counters cntrs;
1290
1291         /*
1292          * Since the HW doesn't support clearing counters, we save the
1293          * current count and subtract it from future responses.
1294          */
1295         ipath_get_counters(dev->dd, &cntrs);
1296
1297         if (p->counter_select & IB_PMA_SEL_SYMBOL_ERROR)
1298                 dev->z_symbol_error_counter = cntrs.symbol_error_counter;
1299
1300         if (p->counter_select & IB_PMA_SEL_LINK_ERROR_RECOVERY)
1301                 dev->z_link_error_recovery_counter =
1302                         cntrs.link_error_recovery_counter;
1303
1304         if (p->counter_select & IB_PMA_SEL_LINK_DOWNED)
1305                 dev->z_link_downed_counter = cntrs.link_downed_counter;
1306
1307         if (p->counter_select & IB_PMA_SEL_PORT_RCV_ERRORS)
1308                 dev->z_port_rcv_errors =
1309                         cntrs.port_rcv_errors + dev->rcv_errors;
1310
1311         if (p->counter_select & IB_PMA_SEL_PORT_RCV_REMPHYS_ERRORS)
1312                 dev->z_port_rcv_remphys_errors =
1313                         cntrs.port_rcv_remphys_errors;
1314
1315         if (p->counter_select & IB_PMA_SEL_PORT_XMIT_DISCARDS)
1316                 dev->z_port_xmit_discards = cntrs.port_xmit_discards;
1317
1318         if (p->counter_select & IB_PMA_SEL_LOCAL_LINK_INTEGRITY_ERRORS)
1319                 dev->z_local_link_integrity_errors =
1320                         cntrs.local_link_integrity_errors;
1321
1322         if (p->counter_select & IB_PMA_SEL_EXCESSIVE_BUFFER_OVERRUNS)
1323                 dev->z_excessive_buffer_overrun_errors =
1324                         cntrs.excessive_buffer_overrun_errors;
1325
1326         if (p->counter_select & IB_PMA_SEL_PORT_VL15_DROPPED) {
1327                 dev->n_vl15_dropped = 0;
1328                 dev->z_vl15_dropped = cntrs.vl15_dropped;
1329         }
1330
1331         if (p->counter_select & IB_PMA_SEL_PORT_XMIT_DATA)
1332                 dev->z_port_xmit_data = cntrs.port_xmit_data;
1333
1334         if (p->counter_select & IB_PMA_SEL_PORT_RCV_DATA)
1335                 dev->z_port_rcv_data = cntrs.port_rcv_data;
1336
1337         if (p->counter_select & IB_PMA_SEL_PORT_XMIT_PACKETS)
1338                 dev->z_port_xmit_packets = cntrs.port_xmit_packets;
1339
1340         if (p->counter_select & IB_PMA_SEL_PORT_RCV_PACKETS)
1341                 dev->z_port_rcv_packets = cntrs.port_rcv_packets;
1342
1343         return recv_pma_get_portcounters(pmp, ibdev, port);
1344 }
1345
1346 static int recv_pma_set_portcounters_ext(struct ib_perf *pmp,
1347                                          struct ib_device *ibdev, u8 port)
1348 {
1349         struct ib_pma_portcounters *p = (struct ib_pma_portcounters *)
1350                 pmp->data;
1351         struct ipath_ibdev *dev = to_idev(ibdev);
1352         u64 swords, rwords, spkts, rpkts, xwait;
1353
1354         ipath_snapshot_counters(dev->dd, &swords, &rwords, &spkts,
1355                                 &rpkts, &xwait);
1356
1357         if (p->counter_select & IB_PMA_SELX_PORT_XMIT_DATA)
1358                 dev->z_port_xmit_data = swords;
1359
1360         if (p->counter_select & IB_PMA_SELX_PORT_RCV_DATA)
1361                 dev->z_port_rcv_data = rwords;
1362
1363         if (p->counter_select & IB_PMA_SELX_PORT_XMIT_PACKETS)
1364                 dev->z_port_xmit_packets = spkts;
1365
1366         if (p->counter_select & IB_PMA_SELX_PORT_RCV_PACKETS)
1367                 dev->z_port_rcv_packets = rpkts;
1368
1369         if (p->counter_select & IB_PMA_SELX_PORT_UNI_XMIT_PACKETS)
1370                 dev->n_unicast_xmit = 0;
1371
1372         if (p->counter_select & IB_PMA_SELX_PORT_UNI_RCV_PACKETS)
1373                 dev->n_unicast_rcv = 0;
1374
1375         if (p->counter_select & IB_PMA_SELX_PORT_MULTI_XMIT_PACKETS)
1376                 dev->n_multicast_xmit = 0;
1377
1378         if (p->counter_select & IB_PMA_SELX_PORT_MULTI_RCV_PACKETS)
1379                 dev->n_multicast_rcv = 0;
1380
1381         return recv_pma_get_portcounters_ext(pmp, ibdev, port);
1382 }
1383
1384 static int process_subn(struct ib_device *ibdev, int mad_flags,
1385                         u8 port_num, struct ib_mad *in_mad,
1386                         struct ib_mad *out_mad)
1387 {
1388         struct ib_smp *smp = (struct ib_smp *)out_mad;
1389         struct ipath_ibdev *dev = to_idev(ibdev);
1390         int ret;
1391
1392         *out_mad = *in_mad;
1393         if (smp->class_version != 1) {
1394                 smp->status |= IB_SMP_UNSUP_VERSION;
1395                 ret = reply(smp);
1396                 goto bail;
1397         }
1398
1399         /* Is the mkey in the process of expiring? */
1400         if (dev->mkey_lease_timeout &&
1401             time_after_eq(jiffies, dev->mkey_lease_timeout)) {
1402                 /* Clear timeout and mkey protection field. */
1403                 dev->mkey_lease_timeout = 0;
1404                 dev->mkeyprot = 0;
1405         }
1406
1407         /*
1408          * M_Key checking depends on
1409          * Portinfo:M_Key_protect_bits
1410          */
1411         if ((mad_flags & IB_MAD_IGNORE_MKEY) == 0 && dev->mkey != 0 &&
1412             dev->mkey != smp->mkey &&
1413             (smp->method == IB_MGMT_METHOD_SET ||
1414              (smp->method == IB_MGMT_METHOD_GET &&
1415               dev->mkeyprot >= 2))) {
1416                 if (dev->mkey_violations != 0xFFFF)
1417                         ++dev->mkey_violations;
1418                 if (dev->mkey_lease_timeout ||
1419                     dev->mkey_lease_period == 0) {
1420                         ret = IB_MAD_RESULT_SUCCESS |
1421                                 IB_MAD_RESULT_CONSUMED;
1422                         goto bail;
1423                 }
1424                 dev->mkey_lease_timeout = jiffies +
1425                         dev->mkey_lease_period * HZ;
1426                 /* Future: Generate a trap notice. */
1427                 ret = IB_MAD_RESULT_SUCCESS | IB_MAD_RESULT_CONSUMED;
1428                 goto bail;
1429         } else if (dev->mkey_lease_timeout)
1430                 dev->mkey_lease_timeout = 0;
1431
1432         switch (smp->method) {
1433         case IB_MGMT_METHOD_GET:
1434                 switch (smp->attr_id) {
1435                 case IB_SMP_ATTR_NODE_DESC:
1436                         ret = recv_subn_get_nodedescription(smp, ibdev);
1437                         goto bail;
1438                 case IB_SMP_ATTR_NODE_INFO:
1439                         ret = recv_subn_get_nodeinfo(smp, ibdev, port_num);
1440                         goto bail;
1441                 case IB_SMP_ATTR_GUID_INFO:
1442                         ret = recv_subn_get_guidinfo(smp, ibdev);
1443                         goto bail;
1444                 case IB_SMP_ATTR_PORT_INFO:
1445                         ret = recv_subn_get_portinfo(smp, ibdev, port_num);
1446                         goto bail;
1447                 case IB_SMP_ATTR_PKEY_TABLE:
1448                         ret = recv_subn_get_pkeytable(smp, ibdev);
1449                         goto bail;
1450                 case IB_SMP_ATTR_SM_INFO:
1451                         if (dev->port_cap_flags & IB_PORT_SM_DISABLED) {
1452                                 ret = IB_MAD_RESULT_SUCCESS |
1453                                         IB_MAD_RESULT_CONSUMED;
1454                                 goto bail;
1455                         }
1456                         if (dev->port_cap_flags & IB_PORT_SM) {
1457                                 ret = IB_MAD_RESULT_SUCCESS;
1458                                 goto bail;
1459                         }
1460                         /* FALLTHROUGH */
1461                 default:
1462                         smp->status |= IB_SMP_UNSUP_METH_ATTR;
1463                         ret = reply(smp);
1464                         goto bail;
1465                 }
1466
1467         case IB_MGMT_METHOD_SET:
1468                 switch (smp->attr_id) {
1469                 case IB_SMP_ATTR_GUID_INFO:
1470                         ret = recv_subn_set_guidinfo(smp, ibdev);
1471                         goto bail;
1472                 case IB_SMP_ATTR_PORT_INFO:
1473                         ret = recv_subn_set_portinfo(smp, ibdev, port_num);
1474                         goto bail;
1475                 case IB_SMP_ATTR_PKEY_TABLE:
1476                         ret = recv_subn_set_pkeytable(smp, ibdev);
1477                         goto bail;
1478                 case IB_SMP_ATTR_SM_INFO:
1479                         if (dev->port_cap_flags & IB_PORT_SM_DISABLED) {
1480                                 ret = IB_MAD_RESULT_SUCCESS |
1481                                         IB_MAD_RESULT_CONSUMED;
1482                                 goto bail;
1483                         }
1484                         if (dev->port_cap_flags & IB_PORT_SM) {
1485                                 ret = IB_MAD_RESULT_SUCCESS;
1486                                 goto bail;
1487                         }
1488                         /* FALLTHROUGH */
1489                 default:
1490                         smp->status |= IB_SMP_UNSUP_METH_ATTR;
1491                         ret = reply(smp);
1492                         goto bail;
1493                 }
1494
1495         case IB_MGMT_METHOD_TRAP:
1496         case IB_MGMT_METHOD_REPORT:
1497         case IB_MGMT_METHOD_REPORT_RESP:
1498         case IB_MGMT_METHOD_TRAP_REPRESS:
1499         case IB_MGMT_METHOD_GET_RESP:
1500                 /*
1501                  * The ib_mad module will call us to process responses
1502                  * before checking for other consumers.
1503                  * Just tell the caller to process it normally.
1504                  */
1505                 ret = IB_MAD_RESULT_SUCCESS;
1506                 goto bail;
1507         default:
1508                 smp->status |= IB_SMP_UNSUP_METHOD;
1509                 ret = reply(smp);
1510         }
1511
1512 bail:
1513         return ret;
1514 }
1515
1516 static int process_perf(struct ib_device *ibdev, u8 port_num,
1517                         struct ib_mad *in_mad,
1518                         struct ib_mad *out_mad)
1519 {
1520         struct ib_perf *pmp = (struct ib_perf *)out_mad;
1521         int ret;
1522
1523         *out_mad = *in_mad;
1524         if (pmp->class_version != 1) {
1525                 pmp->status |= IB_SMP_UNSUP_VERSION;
1526                 ret = reply((struct ib_smp *) pmp);
1527                 goto bail;
1528         }
1529
1530         switch (pmp->method) {
1531         case IB_MGMT_METHOD_GET:
1532                 switch (pmp->attr_id) {
1533                 case IB_PMA_CLASS_PORT_INFO:
1534                         ret = recv_pma_get_classportinfo(pmp);
1535                         goto bail;
1536                 case IB_PMA_PORT_SAMPLES_CONTROL:
1537                         ret = recv_pma_get_portsamplescontrol(pmp, ibdev,
1538                                                               port_num);
1539                         goto bail;
1540                 case IB_PMA_PORT_SAMPLES_RESULT:
1541                         ret = recv_pma_get_portsamplesresult(pmp, ibdev);
1542                         goto bail;
1543                 case IB_PMA_PORT_SAMPLES_RESULT_EXT:
1544                         ret = recv_pma_get_portsamplesresult_ext(pmp,
1545                                                                  ibdev);
1546                         goto bail;
1547                 case IB_PMA_PORT_COUNTERS:
1548                         ret = recv_pma_get_portcounters(pmp, ibdev,
1549                                                         port_num);
1550                         goto bail;
1551                 case IB_PMA_PORT_COUNTERS_EXT:
1552                         ret = recv_pma_get_portcounters_ext(pmp, ibdev,
1553                                                             port_num);
1554                         goto bail;
1555                 default:
1556                         pmp->status |= IB_SMP_UNSUP_METH_ATTR;
1557                         ret = reply((struct ib_smp *) pmp);
1558                         goto bail;
1559                 }
1560
1561         case IB_MGMT_METHOD_SET:
1562                 switch (pmp->attr_id) {
1563                 case IB_PMA_PORT_SAMPLES_CONTROL:
1564                         ret = recv_pma_set_portsamplescontrol(pmp, ibdev,
1565                                                               port_num);
1566                         goto bail;
1567                 case IB_PMA_PORT_COUNTERS:
1568                         ret = recv_pma_set_portcounters(pmp, ibdev,
1569                                                         port_num);
1570                         goto bail;
1571                 case IB_PMA_PORT_COUNTERS_EXT:
1572                         ret = recv_pma_set_portcounters_ext(pmp, ibdev,
1573                                                             port_num);
1574                         goto bail;
1575                 default:
1576                         pmp->status |= IB_SMP_UNSUP_METH_ATTR;
1577                         ret = reply((struct ib_smp *) pmp);
1578                         goto bail;
1579                 }
1580
1581         case IB_MGMT_METHOD_GET_RESP:
1582                 /*
1583                  * The ib_mad module will call us to process responses
1584                  * before checking for other consumers.
1585                  * Just tell the caller to process it normally.
1586                  */
1587                 ret = IB_MAD_RESULT_SUCCESS;
1588                 goto bail;
1589         default:
1590                 pmp->status |= IB_SMP_UNSUP_METHOD;
1591                 ret = reply((struct ib_smp *) pmp);
1592         }
1593
1594 bail:
1595         return ret;
1596 }
1597
1598 /**
1599  * ipath_process_mad - process an incoming MAD packet
1600  * @ibdev: the infiniband device this packet came in on
1601  * @mad_flags: MAD flags
1602  * @port_num: the port number this packet came in on
1603  * @in_wc: the work completion entry for this packet
1604  * @in_grh: the global route header for this packet
1605  * @in_mad: the incoming MAD
1606  * @out_mad: any outgoing MAD reply
1607  *
1608  * Returns IB_MAD_RESULT_SUCCESS if this is a MAD that we are not
1609  * interested in processing.
1610  *
1611  * Note that the verbs framework has already done the MAD sanity checks,
1612  * and hop count/pointer updating for IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE
1613  * MADs.
1614  *
1615  * This is called by the ib_mad module.
1616  */
1617 int ipath_process_mad(struct ib_device *ibdev, int mad_flags, u8 port_num,
1618                       struct ib_wc *in_wc, struct ib_grh *in_grh,
1619                       struct ib_mad *in_mad, struct ib_mad *out_mad)
1620 {
1621         int ret;
1622
1623         switch (in_mad->mad_hdr.mgmt_class) {
1624         case IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE:
1625         case IB_MGMT_CLASS_SUBN_LID_ROUTED:
1626                 ret = process_subn(ibdev, mad_flags, port_num,
1627                                    in_mad, out_mad);
1628                 goto bail;
1629         case IB_MGMT_CLASS_PERF_MGMT:
1630                 ret = process_perf(ibdev, port_num, in_mad, out_mad);
1631                 goto bail;
1632         default:
1633                 ret = IB_MAD_RESULT_SUCCESS;
1634         }
1635
1636 bail:
1637         return ret;
1638 }