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