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