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