Merge git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux-2.6
[wrt350n-kernel.git] / drivers / infiniband / hw / ipath / ipath_mad.c
blob30a50b2b90a5260e6e2f5b604180f9c7d05eb1f3
1 /*
2 * Copyright (c) 2006, 2007 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 __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)
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 strncpy(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 */
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.
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;
118 return reply(smp);
121 static int recv_subn_get_guidinfo(struct ib_smp *smp,
122 struct ib_device *ibdev)
124 u32 startgx = 8 * be32_to_cpu(smp->attr_mod);
125 __be64 *p = (__be64 *) smp->data;
127 /* 32 blocks of 8 64-bit GUIDs per block */
129 memset(smp->data, 0, sizeof(smp->data));
132 * We only support one GUID for now. If this changes, the
133 * portinfo.guid_cap field needs to be updated too.
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;
146 return reply(smp);
150 static int get_overrunthreshold(struct ipath_devdata *dd)
152 return (dd->ipath_ibcctrl >>
153 INFINIPATH_IBCC_OVERRUNTHRESHOLD_SHIFT) &
154 INFINIPATH_IBCC_OVERRUNTHRESHOLD_MASK;
158 * set_overrunthreshold - set the overrun threshold
159 * @dd: the infinipath device
160 * @n: the new threshold
162 * Note that this will only take effect when the link state changes.
164 static int set_overrunthreshold(struct ipath_devdata *dd, unsigned n)
166 unsigned v;
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);
179 return 0;
182 static int get_phyerrthreshold(struct ipath_devdata *dd)
184 return (dd->ipath_ibcctrl >>
185 INFINIPATH_IBCC_PHYERRTHRESHOLD_SHIFT) &
186 INFINIPATH_IBCC_PHYERRTHRESHOLD_MASK;
190 * set_phyerrthreshold - set the physical error threshold
191 * @dd: the infinipath device
192 * @n: the new threshold
194 * Note that this will only take effect when the link state changes.
196 static int set_phyerrthreshold(struct ipath_devdata *dd, unsigned n)
198 unsigned v;
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);
211 return 0;
215 * get_linkdowndefaultstate - get the default linkdown state
216 * @dd: the infinipath device
218 * Returns zero if the default is POLL, 1 if the default is SLEEP.
220 static int get_linkdowndefaultstate(struct ipath_devdata *dd)
222 return !!(dd->ipath_ibcctrl & INFINIPATH_IBCC_LINKDOWNDEFAULTSTATE);
225 static int recv_subn_get_portinfo(struct ib_smp *smp,
226 struct ib_device *ibdev, u8 port)
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;
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;
241 dev = to_idev(ibdev);
243 /* Clear all fields. Only set the non-zero fields. */
244 memset(smp->data, 0, sizeof(smp->data));
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;
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 */
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.
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]; */
321 ret = reply(smp);
323 bail:
324 return ret;
328 * get_pkeys - return the PKEY table for port 0
329 * @dd: the infinipath device
330 * @pkeys: the pkey table is placed here
332 static int get_pkeys(struct ipath_devdata *dd, u16 * pkeys)
334 struct ipath_portdata *pd = dd->ipath_pd[0];
336 memcpy(pkeys, pd->port_pkeys, sizeof(pd->port_pkeys));
338 return 0;
341 static int recv_subn_get_pkeytable(struct ib_smp *smp,
342 struct ib_device *ibdev)
344 u32 startpx = 32 * (be32_to_cpu(smp->attr_mod) & 0xffff);
345 u16 *p = (u16 *) smp->data;
346 __be16 *q = (__be16 *) smp->data;
348 /* 64 blocks of 32 16-bit P_Key entries */
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);
355 get_pkeys(dev->dd, p);
357 for (i = 0; i < n; i++)
358 q[i] = cpu_to_be16(p[i]);
359 } else
360 smp->status |= IB_SMP_INVALID_FIELD;
362 return reply(smp);
365 static int recv_subn_set_guidinfo(struct ib_smp *smp,
366 struct ib_device *ibdev)
368 /* The only GUID we support is the first read-only entry. */
369 return recv_subn_get_guidinfo(smp, ibdev);
373 * set_linkdowndefaultstate - set the default linkdown state
374 * @dd: the infinipath device
375 * @sleep: the new state
377 * Note that this will only take effect when the link state changes.
379 static int set_linkdowndefaultstate(struct ipath_devdata *dd, int sleep)
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;
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
396 * Set Portinfo (see ch. 14.2.5.6).
398 static int recv_subn_set_portinfo(struct ib_smp *smp,
399 struct ib_device *ibdev, u8 port)
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;
414 if (be32_to_cpu(smp->attr_mod) > ibdev->phys_port_cnt)
415 goto err;
417 dev = to_idev(ibdev);
418 dd = dev->dd;
419 event.device = ibdev;
420 event.element.port_num = port;
422 dev->mkey = pip->mkey;
423 dev->gid_prefix = pip->gid_prefix;
424 dev->mkey_lease_period = be16_to_cpu(pip->mkey_lease_period);
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);
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);
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;
456 /* Only 2.5 Gbs supported. */
457 lse = pip->linkspeedactive_enabled & 0xF;
458 if (lse >= 2 && lse <= 0xE)
459 goto err;
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;
477 dev->mkeyprot = pip->mkeyprot_resv_lmc >> 6;
478 dev->vl_high_limit = pip->vl_high_limit;
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;
500 ipath_set_mtu(dd, mtu);
502 dev->sm_sl = pip->neighbormtu_mastersmsl & 0xF;
504 /* We only support VL0 */
505 if (((pip->operationalvl_pei_peo_fpi_fpo >> 4) & 0xF) > 1)
506 goto err;
508 if (pip->mkey_violations == 0)
509 dev->mkey_violations = 0;
512 * Hardware counter can't be reset so snapshot and subtract
513 * later.
515 if (pip->pkey_violations == 0)
516 dev->z_pkey_violations = ipath_get_cr_errpkey(dd);
518 if (pip->qkey_violations == 0)
519 dev->qkey_violations = 0;
521 ore = pip->localphyerrors_overrunerrors;
522 if (set_phyerrthreshold(dd, (ore >> 4) & 0xF))
523 goto err;
525 if (set_overrunthreshold(dd, (ore & 0xF)))
526 goto err;
528 dev->subnet_timeout = pip->clientrereg_resv_subnetto & 0x1F;
530 if (pip->clientrereg_resv_subnetto & 0x80) {
531 clientrereg = 1;
532 event.event = IB_EVENT_CLIENT_REREGISTER;
533 ib_dispatch_event(&event);
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.
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;
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).
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 <<<<<<< HEAD:drivers/infiniband/hw/ipath/ipath_mad.c
559 if (get_linkdowndefaultstate(dd))
560 lstate = IPATH_IB_LINKDOWN_SLEEP;
561 else
562 lstate = IPATH_IB_LINKDOWN;
563 =======
564 lstate = IPATH_IB_LINKDOWN_ONLY;
565 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/infiniband/hw/ipath/ipath_mad.c
566 else if (lstate == 1)
567 lstate = IPATH_IB_LINKDOWN_SLEEP;
568 else if (lstate == 2)
569 lstate = IPATH_IB_LINKDOWN;
570 else if (lstate == 3)
571 lstate = IPATH_IB_LINKDOWN_DISABLE;
572 else
573 goto err;
574 ipath_set_linkstate(dd, lstate);
575 <<<<<<< HEAD:drivers/infiniband/hw/ipath/ipath_mad.c
576 =======
577 ipath_wait_linkstate(dd, IPATH_LINKINIT | IPATH_LINKARMED |
578 IPATH_LINKACTIVE, 1000);
579 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/infiniband/hw/ipath/ipath_mad.c
580 break;
581 case IB_PORT_ARMED:
582 ipath_set_linkstate(dd, IPATH_IB_LINKARM);
583 break;
584 case IB_PORT_ACTIVE:
585 ipath_set_linkstate(dd, IPATH_IB_LINKACTIVE);
586 break;
587 default:
588 /* XXX We have already partially updated our state! */
589 goto err;
592 ret = recv_subn_get_portinfo(smp, ibdev, port);
594 if (clientrereg)
595 pip->clientrereg_resv_subnetto |= 0x80;
597 goto done;
599 err:
600 smp->status |= IB_SMP_INVALID_FIELD;
601 ret = recv_subn_get_portinfo(smp, ibdev, port);
603 done:
604 return ret;
608 * rm_pkey - decrecment the reference count for the given PKEY
609 * @dd: the infinipath device
610 * @key: the PKEY index
612 * Return true if this was the last reference and the hardware table entry
613 * needs to be changed.
615 static int rm_pkey(struct ipath_devdata *dd, u16 key)
617 int i;
618 int ret;
620 for (i = 0; i < ARRAY_SIZE(dd->ipath_pkeys); i++) {
621 if (dd->ipath_pkeys[i] != key)
622 continue;
623 if (atomic_dec_and_test(&dd->ipath_pkeyrefs[i])) {
624 dd->ipath_pkeys[i] = 0;
625 ret = 1;
626 goto bail;
628 break;
631 ret = 0;
633 bail:
634 return ret;
638 * add_pkey - add the given PKEY to the hardware table
639 * @dd: the infinipath device
640 * @key: the PKEY
642 * Return an error code if unable to add the entry, zero if no change,
643 * or 1 if the hardware PKEY register needs to be updated.
645 static int add_pkey(struct ipath_devdata *dd, u16 key)
647 int i;
648 u16 lkey = key & 0x7FFF;
649 int any = 0;
650 int ret;
652 if (lkey == 0x7FFF) {
653 ret = 0;
654 goto bail;
657 /* Look for an empty slot or a matching PKEY. */
658 for (i = 0; i < ARRAY_SIZE(dd->ipath_pkeys); i++) {
659 if (!dd->ipath_pkeys[i]) {
660 any++;
661 continue;
663 /* If it matches exactly, try to increment the ref count */
664 if (dd->ipath_pkeys[i] == key) {
665 if (atomic_inc_return(&dd->ipath_pkeyrefs[i]) > 1) {
666 ret = 0;
667 goto bail;
669 /* Lost the race. Look for an empty slot below. */
670 atomic_dec(&dd->ipath_pkeyrefs[i]);
671 any++;
674 * It makes no sense to have both the limited and unlimited
675 * PKEY set at the same time since the unlimited one will
676 * disable the limited one.
678 if ((dd->ipath_pkeys[i] & 0x7FFF) == lkey) {
679 ret = -EEXIST;
680 goto bail;
683 if (!any) {
684 ret = -EBUSY;
685 goto bail;
687 for (i = 0; i < ARRAY_SIZE(dd->ipath_pkeys); i++) {
688 if (!dd->ipath_pkeys[i] &&
689 atomic_inc_return(&dd->ipath_pkeyrefs[i]) == 1) {
690 /* for ipathstats, etc. */
691 ipath_stats.sps_pkeys[i] = lkey;
692 dd->ipath_pkeys[i] = key;
693 ret = 1;
694 goto bail;
697 ret = -EBUSY;
699 bail:
700 return ret;
704 * set_pkeys - set the PKEY table for port 0
705 * @dd: the infinipath device
706 * @pkeys: the PKEY table
708 static int set_pkeys(struct ipath_devdata *dd, u16 *pkeys)
710 struct ipath_portdata *pd;
711 int i;
712 int changed = 0;
714 pd = dd->ipath_pd[0];
716 for (i = 0; i < ARRAY_SIZE(pd->port_pkeys); i++) {
717 u16 key = pkeys[i];
718 u16 okey = pd->port_pkeys[i];
720 if (key == okey)
721 continue;
723 * The value of this PKEY table entry is changing.
724 * Remove the old entry in the hardware's array of PKEYs.
726 if (okey & 0x7FFF)
727 changed |= rm_pkey(dd, okey);
728 if (key & 0x7FFF) {
729 int ret = add_pkey(dd, key);
731 if (ret < 0)
732 key = 0;
733 else
734 changed |= ret;
736 pd->port_pkeys[i] = key;
738 if (changed) {
739 u64 pkey;
741 pkey = (u64) dd->ipath_pkeys[0] |
742 ((u64) dd->ipath_pkeys[1] << 16) |
743 ((u64) dd->ipath_pkeys[2] << 32) |
744 ((u64) dd->ipath_pkeys[3] << 48);
745 ipath_cdbg(VERBOSE, "p0 new pkey reg %llx\n",
746 (unsigned long long) pkey);
747 ipath_write_kreg(dd, dd->ipath_kregs->kr_partitionkey,
748 pkey);
750 return 0;
753 static int recv_subn_set_pkeytable(struct ib_smp *smp,
754 struct ib_device *ibdev)
756 u32 startpx = 32 * (be32_to_cpu(smp->attr_mod) & 0xffff);
757 __be16 *p = (__be16 *) smp->data;
758 u16 *q = (u16 *) smp->data;
759 struct ipath_ibdev *dev = to_idev(ibdev);
760 unsigned i, n = ipath_get_npkeys(dev->dd);
762 for (i = 0; i < n; i++)
763 q[i] = be16_to_cpu(p[i]);
765 if (startpx != 0 || set_pkeys(dev->dd, q) != 0)
766 smp->status |= IB_SMP_INVALID_FIELD;
768 return recv_subn_get_pkeytable(smp, ibdev);
771 #define IB_PMA_CLASS_PORT_INFO __constant_htons(0x0001)
772 #define IB_PMA_PORT_SAMPLES_CONTROL __constant_htons(0x0010)
773 #define IB_PMA_PORT_SAMPLES_RESULT __constant_htons(0x0011)
774 #define IB_PMA_PORT_COUNTERS __constant_htons(0x0012)
775 #define IB_PMA_PORT_COUNTERS_EXT __constant_htons(0x001D)
776 #define IB_PMA_PORT_SAMPLES_RESULT_EXT __constant_htons(0x001E)
778 struct ib_perf {
779 u8 base_version;
780 u8 mgmt_class;
781 u8 class_version;
782 u8 method;
783 __be16 status;
784 __be16 unused;
785 __be64 tid;
786 __be16 attr_id;
787 __be16 resv;
788 __be32 attr_mod;
789 u8 reserved[40];
790 u8 data[192];
791 } __attribute__ ((packed));
793 struct ib_pma_classportinfo {
794 u8 base_version;
795 u8 class_version;
796 __be16 cap_mask;
797 u8 reserved[3];
798 u8 resp_time_value; /* only lower 5 bits */
799 union ib_gid redirect_gid;
800 __be32 redirect_tc_sl_fl; /* 8, 4, 20 bits respectively */
801 __be16 redirect_lid;
802 __be16 redirect_pkey;
803 __be32 redirect_qp; /* only lower 24 bits */
804 __be32 redirect_qkey;
805 union ib_gid trap_gid;
806 __be32 trap_tc_sl_fl; /* 8, 4, 20 bits respectively */
807 __be16 trap_lid;
808 __be16 trap_pkey;
809 __be32 trap_hl_qp; /* 8, 24 bits respectively */
810 __be32 trap_qkey;
811 } __attribute__ ((packed));
813 struct ib_pma_portsamplescontrol {
814 u8 opcode;
815 u8 port_select;
816 u8 tick;
817 u8 counter_width; /* only lower 3 bits */
818 __be32 counter_mask0_9; /* 2, 10 * 3, bits */
819 __be16 counter_mask10_14; /* 1, 5 * 3, bits */
820 u8 sample_mechanisms;
821 u8 sample_status; /* only lower 2 bits */
822 __be64 option_mask;
823 __be64 vendor_mask;
824 __be32 sample_start;
825 __be32 sample_interval;
826 __be16 tag;
827 __be16 counter_select[15];
828 } __attribute__ ((packed));
830 struct ib_pma_portsamplesresult {
831 __be16 tag;
832 __be16 sample_status; /* only lower 2 bits */
833 __be32 counter[15];
834 } __attribute__ ((packed));
836 struct ib_pma_portsamplesresult_ext {
837 __be16 tag;
838 __be16 sample_status; /* only lower 2 bits */
839 __be32 extended_width; /* only upper 2 bits */
840 __be64 counter[15];
841 } __attribute__ ((packed));
843 struct ib_pma_portcounters {
844 u8 reserved;
845 u8 port_select;
846 __be16 counter_select;
847 __be16 symbol_error_counter;
848 u8 link_error_recovery_counter;
849 u8 link_downed_counter;
850 __be16 port_rcv_errors;
851 __be16 port_rcv_remphys_errors;
852 __be16 port_rcv_switch_relay_errors;
853 __be16 port_xmit_discards;
854 u8 port_xmit_constraint_errors;
855 u8 port_rcv_constraint_errors;
856 u8 reserved1;
857 u8 lli_ebor_errors; /* 4, 4, bits */
858 __be16 reserved2;
859 __be16 vl15_dropped;
860 __be32 port_xmit_data;
861 __be32 port_rcv_data;
862 __be32 port_xmit_packets;
863 __be32 port_rcv_packets;
864 } __attribute__ ((packed));
866 #define IB_PMA_SEL_SYMBOL_ERROR __constant_htons(0x0001)
867 #define IB_PMA_SEL_LINK_ERROR_RECOVERY __constant_htons(0x0002)
868 #define IB_PMA_SEL_LINK_DOWNED __constant_htons(0x0004)
869 #define IB_PMA_SEL_PORT_RCV_ERRORS __constant_htons(0x0008)
870 #define IB_PMA_SEL_PORT_RCV_REMPHYS_ERRORS __constant_htons(0x0010)
871 #define IB_PMA_SEL_PORT_XMIT_DISCARDS __constant_htons(0x0040)
872 #define IB_PMA_SEL_LOCAL_LINK_INTEGRITY_ERRORS __constant_htons(0x0200)
873 #define IB_PMA_SEL_EXCESSIVE_BUFFER_OVERRUNS __constant_htons(0x0400)
874 #define IB_PMA_SEL_PORT_VL15_DROPPED __constant_htons(0x0800)
875 #define IB_PMA_SEL_PORT_XMIT_DATA __constant_htons(0x1000)
876 #define IB_PMA_SEL_PORT_RCV_DATA __constant_htons(0x2000)
877 #define IB_PMA_SEL_PORT_XMIT_PACKETS __constant_htons(0x4000)
878 #define IB_PMA_SEL_PORT_RCV_PACKETS __constant_htons(0x8000)
880 struct ib_pma_portcounters_ext {
881 u8 reserved;
882 u8 port_select;
883 __be16 counter_select;
884 __be32 reserved1;
885 __be64 port_xmit_data;
886 __be64 port_rcv_data;
887 __be64 port_xmit_packets;
888 __be64 port_rcv_packets;
889 __be64 port_unicast_xmit_packets;
890 __be64 port_unicast_rcv_packets;
891 __be64 port_multicast_xmit_packets;
892 __be64 port_multicast_rcv_packets;
893 } __attribute__ ((packed));
895 #define IB_PMA_SELX_PORT_XMIT_DATA __constant_htons(0x0001)
896 #define IB_PMA_SELX_PORT_RCV_DATA __constant_htons(0x0002)
897 #define IB_PMA_SELX_PORT_XMIT_PACKETS __constant_htons(0x0004)
898 #define IB_PMA_SELX_PORT_RCV_PACKETS __constant_htons(0x0008)
899 #define IB_PMA_SELX_PORT_UNI_XMIT_PACKETS __constant_htons(0x0010)
900 #define IB_PMA_SELX_PORT_UNI_RCV_PACKETS __constant_htons(0x0020)
901 #define IB_PMA_SELX_PORT_MULTI_XMIT_PACKETS __constant_htons(0x0040)
902 #define IB_PMA_SELX_PORT_MULTI_RCV_PACKETS __constant_htons(0x0080)
904 static int recv_pma_get_classportinfo(struct ib_perf *pmp)
906 struct ib_pma_classportinfo *p =
907 (struct ib_pma_classportinfo *)pmp->data;
909 memset(pmp->data, 0, sizeof(pmp->data));
911 if (pmp->attr_mod != 0)
912 pmp->status |= IB_SMP_INVALID_FIELD;
914 /* Indicate AllPortSelect is valid (only one port anyway) */
915 p->cap_mask = __constant_cpu_to_be16(1 << 8);
916 p->base_version = 1;
917 p->class_version = 1;
919 * Expected response time is 4.096 usec. * 2^18 == 1.073741824
920 * sec.
922 p->resp_time_value = 18;
924 return reply((struct ib_smp *) pmp);
928 * The PortSamplesControl.CounterMasks field is an array of 3 bit fields
929 * which specify the N'th counter's capabilities. See ch. 16.1.3.2.
930 * We support 5 counters which only count the mandatory quantities.
932 #define COUNTER_MASK(q, n) (q << ((9 - n) * 3))
933 #define COUNTER_MASK0_9 \
934 __constant_cpu_to_be32(COUNTER_MASK(1, 0) | \
935 COUNTER_MASK(1, 1) | \
936 COUNTER_MASK(1, 2) | \
937 COUNTER_MASK(1, 3) | \
938 COUNTER_MASK(1, 4))
940 static int recv_pma_get_portsamplescontrol(struct ib_perf *pmp,
941 struct ib_device *ibdev, u8 port)
943 struct ib_pma_portsamplescontrol *p =
944 (struct ib_pma_portsamplescontrol *)pmp->data;
945 struct ipath_ibdev *dev = to_idev(ibdev);
946 struct ipath_cregs const *crp = dev->dd->ipath_cregs;
947 unsigned long flags;
948 u8 port_select = p->port_select;
950 memset(pmp->data, 0, sizeof(pmp->data));
952 p->port_select = port_select;
953 if (pmp->attr_mod != 0 ||
954 (port_select != port && port_select != 0xFF))
955 pmp->status |= IB_SMP_INVALID_FIELD;
957 * Ticks are 10x the link transfer period which for 2.5Gbs is 4
958 * nsec. 0 == 4 nsec., 1 == 8 nsec., ..., 255 == 1020 nsec. Sample
959 * intervals are counted in ticks. Since we use Linux timers, that
960 * count in jiffies, we can't sample for less than 1000 ticks if HZ
961 * == 1000 (4000 ticks if HZ is 250).
963 /* XXX This is WRONG. */
964 p->tick = 250; /* 1 usec. */
965 p->counter_width = 4; /* 32 bit counters */
966 p->counter_mask0_9 = COUNTER_MASK0_9;
967 spin_lock_irqsave(&dev->pending_lock, flags);
968 if (crp->cr_psstat)
969 p->sample_status = ipath_read_creg32(dev->dd, crp->cr_psstat);
970 else
971 p->sample_status = dev->pma_sample_status;
972 p->sample_start = cpu_to_be32(dev->pma_sample_start);
973 p->sample_interval = cpu_to_be32(dev->pma_sample_interval);
974 p->tag = cpu_to_be16(dev->pma_tag);
975 p->counter_select[0] = dev->pma_counter_select[0];
976 p->counter_select[1] = dev->pma_counter_select[1];
977 p->counter_select[2] = dev->pma_counter_select[2];
978 p->counter_select[3] = dev->pma_counter_select[3];
979 p->counter_select[4] = dev->pma_counter_select[4];
980 spin_unlock_irqrestore(&dev->pending_lock, flags);
982 return reply((struct ib_smp *) pmp);
985 static int recv_pma_set_portsamplescontrol(struct ib_perf *pmp,
986 struct ib_device *ibdev, u8 port)
988 struct ib_pma_portsamplescontrol *p =
989 (struct ib_pma_portsamplescontrol *)pmp->data;
990 struct ipath_ibdev *dev = to_idev(ibdev);
991 struct ipath_cregs const *crp = dev->dd->ipath_cregs;
992 unsigned long flags;
993 u8 status;
994 int ret;
996 if (pmp->attr_mod != 0 ||
997 (p->port_select != port && p->port_select != 0xFF)) {
998 pmp->status |= IB_SMP_INVALID_FIELD;
999 ret = reply((struct ib_smp *) pmp);
1000 goto bail;
1003 spin_lock_irqsave(&dev->pending_lock, flags);
1004 if (crp->cr_psstat)
1005 status = ipath_read_creg32(dev->dd, crp->cr_psstat);
1006 else
1007 status = dev->pma_sample_status;
1008 if (status == IB_PMA_SAMPLE_STATUS_DONE) {
1009 dev->pma_sample_start = be32_to_cpu(p->sample_start);
1010 dev->pma_sample_interval = be32_to_cpu(p->sample_interval);
1011 dev->pma_tag = be16_to_cpu(p->tag);
1012 dev->pma_counter_select[0] = p->counter_select[0];
1013 dev->pma_counter_select[1] = p->counter_select[1];
1014 dev->pma_counter_select[2] = p->counter_select[2];
1015 dev->pma_counter_select[3] = p->counter_select[3];
1016 dev->pma_counter_select[4] = p->counter_select[4];
1017 if (crp->cr_psstat) {
1018 ipath_write_creg(dev->dd, crp->cr_psinterval,
1019 dev->pma_sample_interval);
1020 ipath_write_creg(dev->dd, crp->cr_psstart,
1021 dev->pma_sample_start);
1022 } else
1023 dev->pma_sample_status = IB_PMA_SAMPLE_STATUS_STARTED;
1025 spin_unlock_irqrestore(&dev->pending_lock, flags);
1027 ret = recv_pma_get_portsamplescontrol(pmp, ibdev, port);
1029 bail:
1030 return ret;
1033 static u64 get_counter(struct ipath_ibdev *dev,
1034 struct ipath_cregs const *crp,
1035 __be16 sel)
1037 u64 ret;
1039 switch (sel) {
1040 case IB_PMA_PORT_XMIT_DATA:
1041 ret = (crp->cr_psxmitdatacount) ?
1042 ipath_read_creg32(dev->dd, crp->cr_psxmitdatacount) :
1043 dev->ipath_sword;
1044 break;
1045 case IB_PMA_PORT_RCV_DATA:
1046 ret = (crp->cr_psrcvdatacount) ?
1047 ipath_read_creg32(dev->dd, crp->cr_psrcvdatacount) :
1048 dev->ipath_rword;
1049 break;
1050 case IB_PMA_PORT_XMIT_PKTS:
1051 ret = (crp->cr_psxmitpktscount) ?
1052 ipath_read_creg32(dev->dd, crp->cr_psxmitpktscount) :
1053 dev->ipath_spkts;
1054 break;
1055 case IB_PMA_PORT_RCV_PKTS:
1056 ret = (crp->cr_psrcvpktscount) ?
1057 ipath_read_creg32(dev->dd, crp->cr_psrcvpktscount) :
1058 dev->ipath_rpkts;
1059 break;
1060 case IB_PMA_PORT_XMIT_WAIT:
1061 ret = (crp->cr_psxmitwaitcount) ?
1062 ipath_read_creg32(dev->dd, crp->cr_psxmitwaitcount) :
1063 dev->ipath_xmit_wait;
1064 break;
1065 default:
1066 ret = 0;
1069 return ret;
1072 static int recv_pma_get_portsamplesresult(struct ib_perf *pmp,
1073 struct ib_device *ibdev)
1075 struct ib_pma_portsamplesresult *p =
1076 (struct ib_pma_portsamplesresult *)pmp->data;
1077 struct ipath_ibdev *dev = to_idev(ibdev);
1078 struct ipath_cregs const *crp = dev->dd->ipath_cregs;
1079 u8 status;
1080 int i;
1082 memset(pmp->data, 0, sizeof(pmp->data));
1083 p->tag = cpu_to_be16(dev->pma_tag);
1084 if (crp->cr_psstat)
1085 status = ipath_read_creg32(dev->dd, crp->cr_psstat);
1086 else
1087 status = dev->pma_sample_status;
1088 p->sample_status = cpu_to_be16(status);
1089 for (i = 0; i < ARRAY_SIZE(dev->pma_counter_select); i++)
1090 p->counter[i] = (status != IB_PMA_SAMPLE_STATUS_DONE) ? 0 :
1091 cpu_to_be32(
1092 get_counter(dev, crp, dev->pma_counter_select[i]));
1094 return reply((struct ib_smp *) pmp);
1097 static int recv_pma_get_portsamplesresult_ext(struct ib_perf *pmp,
1098 struct ib_device *ibdev)
1100 struct ib_pma_portsamplesresult_ext *p =
1101 (struct ib_pma_portsamplesresult_ext *)pmp->data;
1102 struct ipath_ibdev *dev = to_idev(ibdev);
1103 struct ipath_cregs const *crp = dev->dd->ipath_cregs;
1104 u8 status;
1105 int i;
1107 memset(pmp->data, 0, sizeof(pmp->data));
1108 p->tag = cpu_to_be16(dev->pma_tag);
1109 if (crp->cr_psstat)
1110 status = ipath_read_creg32(dev->dd, crp->cr_psstat);
1111 else
1112 status = dev->pma_sample_status;
1113 p->sample_status = cpu_to_be16(status);
1114 /* 64 bits */
1115 p->extended_width = __constant_cpu_to_be32(0x80000000);
1116 for (i = 0; i < ARRAY_SIZE(dev->pma_counter_select); i++)
1117 p->counter[i] = (status != IB_PMA_SAMPLE_STATUS_DONE) ? 0 :
1118 cpu_to_be64(
1119 get_counter(dev, crp, dev->pma_counter_select[i]));
1121 return reply((struct ib_smp *) pmp);
1124 static int recv_pma_get_portcounters(struct ib_perf *pmp,
1125 struct ib_device *ibdev, u8 port)
1127 struct ib_pma_portcounters *p = (struct ib_pma_portcounters *)
1128 pmp->data;
1129 struct ipath_ibdev *dev = to_idev(ibdev);
1130 struct ipath_verbs_counters cntrs;
1131 u8 port_select = p->port_select;
1133 ipath_get_counters(dev->dd, &cntrs);
1135 /* Adjust counters for any resets done. */
1136 cntrs.symbol_error_counter -= dev->z_symbol_error_counter;
1137 cntrs.link_error_recovery_counter -=
1138 dev->z_link_error_recovery_counter;
1139 cntrs.link_downed_counter -= dev->z_link_downed_counter;
1140 cntrs.port_rcv_errors += dev->rcv_errors;
1141 cntrs.port_rcv_errors -= dev->z_port_rcv_errors;
1142 cntrs.port_rcv_remphys_errors -= dev->z_port_rcv_remphys_errors;
1143 cntrs.port_xmit_discards -= dev->z_port_xmit_discards;
1144 cntrs.port_xmit_data -= dev->z_port_xmit_data;
1145 cntrs.port_rcv_data -= dev->z_port_rcv_data;
1146 cntrs.port_xmit_packets -= dev->z_port_xmit_packets;
1147 cntrs.port_rcv_packets -= dev->z_port_rcv_packets;
1148 cntrs.local_link_integrity_errors -=
1149 dev->z_local_link_integrity_errors;
1150 cntrs.excessive_buffer_overrun_errors -=
1151 dev->z_excessive_buffer_overrun_errors;
1152 cntrs.vl15_dropped -= dev->z_vl15_dropped;
1153 cntrs.vl15_dropped += dev->n_vl15_dropped;
1155 memset(pmp->data, 0, sizeof(pmp->data));
1157 p->port_select = port_select;
1158 if (pmp->attr_mod != 0 ||
1159 (port_select != port && port_select != 0xFF))
1160 pmp->status |= IB_SMP_INVALID_FIELD;
1162 if (cntrs.symbol_error_counter > 0xFFFFUL)
1163 p->symbol_error_counter = __constant_cpu_to_be16(0xFFFF);
1164 else
1165 p->symbol_error_counter =
1166 cpu_to_be16((u16)cntrs.symbol_error_counter);
1167 if (cntrs.link_error_recovery_counter > 0xFFUL)
1168 p->link_error_recovery_counter = 0xFF;
1169 else
1170 p->link_error_recovery_counter =
1171 (u8)cntrs.link_error_recovery_counter;
1172 if (cntrs.link_downed_counter > 0xFFUL)
1173 p->link_downed_counter = 0xFF;
1174 else
1175 p->link_downed_counter = (u8)cntrs.link_downed_counter;
1176 if (cntrs.port_rcv_errors > 0xFFFFUL)
1177 p->port_rcv_errors = __constant_cpu_to_be16(0xFFFF);
1178 else
1179 p->port_rcv_errors =
1180 cpu_to_be16((u16) cntrs.port_rcv_errors);
1181 if (cntrs.port_rcv_remphys_errors > 0xFFFFUL)
1182 p->port_rcv_remphys_errors = __constant_cpu_to_be16(0xFFFF);
1183 else
1184 p->port_rcv_remphys_errors =
1185 cpu_to_be16((u16)cntrs.port_rcv_remphys_errors);
1186 if (cntrs.port_xmit_discards > 0xFFFFUL)
1187 p->port_xmit_discards = __constant_cpu_to_be16(0xFFFF);
1188 else
1189 p->port_xmit_discards =
1190 cpu_to_be16((u16)cntrs.port_xmit_discards);
1191 if (cntrs.local_link_integrity_errors > 0xFUL)
1192 cntrs.local_link_integrity_errors = 0xFUL;
1193 if (cntrs.excessive_buffer_overrun_errors > 0xFUL)
1194 cntrs.excessive_buffer_overrun_errors = 0xFUL;
1195 p->lli_ebor_errors = (cntrs.local_link_integrity_errors << 4) |
1196 cntrs.excessive_buffer_overrun_errors;
1197 if (cntrs.vl15_dropped > 0xFFFFUL)
1198 p->vl15_dropped = __constant_cpu_to_be16(0xFFFF);
1199 else
1200 p->vl15_dropped = cpu_to_be16((u16)cntrs.vl15_dropped);
1201 if (cntrs.port_xmit_data > 0xFFFFFFFFUL)
1202 p->port_xmit_data = __constant_cpu_to_be32(0xFFFFFFFF);
1203 else
1204 p->port_xmit_data = cpu_to_be32((u32)cntrs.port_xmit_data);
1205 if (cntrs.port_rcv_data > 0xFFFFFFFFUL)
1206 p->port_rcv_data = __constant_cpu_to_be32(0xFFFFFFFF);
1207 else
1208 p->port_rcv_data = cpu_to_be32((u32)cntrs.port_rcv_data);
1209 if (cntrs.port_xmit_packets > 0xFFFFFFFFUL)
1210 p->port_xmit_packets = __constant_cpu_to_be32(0xFFFFFFFF);
1211 else
1212 p->port_xmit_packets =
1213 cpu_to_be32((u32)cntrs.port_xmit_packets);
1214 if (cntrs.port_rcv_packets > 0xFFFFFFFFUL)
1215 p->port_rcv_packets = __constant_cpu_to_be32(0xFFFFFFFF);
1216 else
1217 p->port_rcv_packets =
1218 cpu_to_be32((u32) cntrs.port_rcv_packets);
1220 return reply((struct ib_smp *) pmp);
1223 static int recv_pma_get_portcounters_ext(struct ib_perf *pmp,
1224 struct ib_device *ibdev, u8 port)
1226 struct ib_pma_portcounters_ext *p =
1227 (struct ib_pma_portcounters_ext *)pmp->data;
1228 struct ipath_ibdev *dev = to_idev(ibdev);
1229 u64 swords, rwords, spkts, rpkts, xwait;
1230 u8 port_select = p->port_select;
1232 ipath_snapshot_counters(dev->dd, &swords, &rwords, &spkts,
1233 &rpkts, &xwait);
1235 /* Adjust counters for any resets done. */
1236 swords -= dev->z_port_xmit_data;
1237 rwords -= dev->z_port_rcv_data;
1238 spkts -= dev->z_port_xmit_packets;
1239 rpkts -= dev->z_port_rcv_packets;
1241 memset(pmp->data, 0, sizeof(pmp->data));
1243 p->port_select = port_select;
1244 if (pmp->attr_mod != 0 ||
1245 (port_select != port && port_select != 0xFF))
1246 pmp->status |= IB_SMP_INVALID_FIELD;
1248 p->port_xmit_data = cpu_to_be64(swords);
1249 p->port_rcv_data = cpu_to_be64(rwords);
1250 p->port_xmit_packets = cpu_to_be64(spkts);
1251 p->port_rcv_packets = cpu_to_be64(rpkts);
1252 p->port_unicast_xmit_packets = cpu_to_be64(dev->n_unicast_xmit);
1253 p->port_unicast_rcv_packets = cpu_to_be64(dev->n_unicast_rcv);
1254 p->port_multicast_xmit_packets = cpu_to_be64(dev->n_multicast_xmit);
1255 p->port_multicast_rcv_packets = cpu_to_be64(dev->n_multicast_rcv);
1257 return reply((struct ib_smp *) pmp);
1260 static int recv_pma_set_portcounters(struct ib_perf *pmp,
1261 struct ib_device *ibdev, u8 port)
1263 struct ib_pma_portcounters *p = (struct ib_pma_portcounters *)
1264 pmp->data;
1265 struct ipath_ibdev *dev = to_idev(ibdev);
1266 struct ipath_verbs_counters cntrs;
1269 * Since the HW doesn't support clearing counters, we save the
1270 * current count and subtract it from future responses.
1272 ipath_get_counters(dev->dd, &cntrs);
1274 if (p->counter_select & IB_PMA_SEL_SYMBOL_ERROR)
1275 dev->z_symbol_error_counter = cntrs.symbol_error_counter;
1277 if (p->counter_select & IB_PMA_SEL_LINK_ERROR_RECOVERY)
1278 dev->z_link_error_recovery_counter =
1279 cntrs.link_error_recovery_counter;
1281 if (p->counter_select & IB_PMA_SEL_LINK_DOWNED)
1282 dev->z_link_downed_counter = cntrs.link_downed_counter;
1284 if (p->counter_select & IB_PMA_SEL_PORT_RCV_ERRORS)
1285 dev->z_port_rcv_errors =
1286 cntrs.port_rcv_errors + dev->rcv_errors;
1288 if (p->counter_select & IB_PMA_SEL_PORT_RCV_REMPHYS_ERRORS)
1289 dev->z_port_rcv_remphys_errors =
1290 cntrs.port_rcv_remphys_errors;
1292 if (p->counter_select & IB_PMA_SEL_PORT_XMIT_DISCARDS)
1293 dev->z_port_xmit_discards = cntrs.port_xmit_discards;
1295 if (p->counter_select & IB_PMA_SEL_LOCAL_LINK_INTEGRITY_ERRORS)
1296 dev->z_local_link_integrity_errors =
1297 cntrs.local_link_integrity_errors;
1299 if (p->counter_select & IB_PMA_SEL_EXCESSIVE_BUFFER_OVERRUNS)
1300 dev->z_excessive_buffer_overrun_errors =
1301 cntrs.excessive_buffer_overrun_errors;
1303 if (p->counter_select & IB_PMA_SEL_PORT_VL15_DROPPED) {
1304 dev->n_vl15_dropped = 0;
1305 dev->z_vl15_dropped = cntrs.vl15_dropped;
1308 if (p->counter_select & IB_PMA_SEL_PORT_XMIT_DATA)
1309 dev->z_port_xmit_data = cntrs.port_xmit_data;
1311 if (p->counter_select & IB_PMA_SEL_PORT_RCV_DATA)
1312 dev->z_port_rcv_data = cntrs.port_rcv_data;
1314 if (p->counter_select & IB_PMA_SEL_PORT_XMIT_PACKETS)
1315 dev->z_port_xmit_packets = cntrs.port_xmit_packets;
1317 if (p->counter_select & IB_PMA_SEL_PORT_RCV_PACKETS)
1318 dev->z_port_rcv_packets = cntrs.port_rcv_packets;
1320 return recv_pma_get_portcounters(pmp, ibdev, port);
1323 static int recv_pma_set_portcounters_ext(struct ib_perf *pmp,
1324 struct ib_device *ibdev, u8 port)
1326 struct ib_pma_portcounters *p = (struct ib_pma_portcounters *)
1327 pmp->data;
1328 struct ipath_ibdev *dev = to_idev(ibdev);
1329 u64 swords, rwords, spkts, rpkts, xwait;
1331 ipath_snapshot_counters(dev->dd, &swords, &rwords, &spkts,
1332 &rpkts, &xwait);
1334 if (p->counter_select & IB_PMA_SELX_PORT_XMIT_DATA)
1335 dev->z_port_xmit_data = swords;
1337 if (p->counter_select & IB_PMA_SELX_PORT_RCV_DATA)
1338 dev->z_port_rcv_data = rwords;
1340 if (p->counter_select & IB_PMA_SELX_PORT_XMIT_PACKETS)
1341 dev->z_port_xmit_packets = spkts;
1343 if (p->counter_select & IB_PMA_SELX_PORT_RCV_PACKETS)
1344 dev->z_port_rcv_packets = rpkts;
1346 if (p->counter_select & IB_PMA_SELX_PORT_UNI_XMIT_PACKETS)
1347 dev->n_unicast_xmit = 0;
1349 if (p->counter_select & IB_PMA_SELX_PORT_UNI_RCV_PACKETS)
1350 dev->n_unicast_rcv = 0;
1352 if (p->counter_select & IB_PMA_SELX_PORT_MULTI_XMIT_PACKETS)
1353 dev->n_multicast_xmit = 0;
1355 if (p->counter_select & IB_PMA_SELX_PORT_MULTI_RCV_PACKETS)
1356 dev->n_multicast_rcv = 0;
1358 return recv_pma_get_portcounters_ext(pmp, ibdev, port);
1361 static int process_subn(struct ib_device *ibdev, int mad_flags,
1362 u8 port_num, struct ib_mad *in_mad,
1363 struct ib_mad *out_mad)
1365 struct ib_smp *smp = (struct ib_smp *)out_mad;
1366 struct ipath_ibdev *dev = to_idev(ibdev);
1367 int ret;
1369 *out_mad = *in_mad;
1370 if (smp->class_version != 1) {
1371 smp->status |= IB_SMP_UNSUP_VERSION;
1372 ret = reply(smp);
1373 goto bail;
1376 /* Is the mkey in the process of expiring? */
1377 if (dev->mkey_lease_timeout && jiffies >= dev->mkey_lease_timeout) {
1378 /* Clear timeout and mkey protection field. */
1379 dev->mkey_lease_timeout = 0;
1380 dev->mkeyprot = 0;
1384 * M_Key checking depends on
1385 * Portinfo:M_Key_protect_bits
1387 if ((mad_flags & IB_MAD_IGNORE_MKEY) == 0 && dev->mkey != 0 &&
1388 dev->mkey != smp->mkey &&
1389 (smp->method == IB_MGMT_METHOD_SET ||
1390 (smp->method == IB_MGMT_METHOD_GET &&
1391 dev->mkeyprot >= 2))) {
1392 if (dev->mkey_violations != 0xFFFF)
1393 ++dev->mkey_violations;
1394 if (dev->mkey_lease_timeout ||
1395 dev->mkey_lease_period == 0) {
1396 ret = IB_MAD_RESULT_SUCCESS |
1397 IB_MAD_RESULT_CONSUMED;
1398 goto bail;
1400 dev->mkey_lease_timeout = jiffies +
1401 dev->mkey_lease_period * HZ;
1402 /* Future: Generate a trap notice. */
1403 ret = IB_MAD_RESULT_SUCCESS | IB_MAD_RESULT_CONSUMED;
1404 goto bail;
1405 } else if (dev->mkey_lease_timeout)
1406 dev->mkey_lease_timeout = 0;
1408 switch (smp->method) {
1409 case IB_MGMT_METHOD_GET:
1410 switch (smp->attr_id) {
1411 case IB_SMP_ATTR_NODE_DESC:
1412 ret = recv_subn_get_nodedescription(smp, ibdev);
1413 goto bail;
1414 case IB_SMP_ATTR_NODE_INFO:
1415 ret = recv_subn_get_nodeinfo(smp, ibdev, port_num);
1416 goto bail;
1417 case IB_SMP_ATTR_GUID_INFO:
1418 ret = recv_subn_get_guidinfo(smp, ibdev);
1419 goto bail;
1420 case IB_SMP_ATTR_PORT_INFO:
1421 ret = recv_subn_get_portinfo(smp, ibdev, port_num);
1422 goto bail;
1423 case IB_SMP_ATTR_PKEY_TABLE:
1424 ret = recv_subn_get_pkeytable(smp, ibdev);
1425 goto bail;
1426 case IB_SMP_ATTR_SM_INFO:
1427 if (dev->port_cap_flags & IB_PORT_SM_DISABLED) {
1428 ret = IB_MAD_RESULT_SUCCESS |
1429 IB_MAD_RESULT_CONSUMED;
1430 goto bail;
1432 if (dev->port_cap_flags & IB_PORT_SM) {
1433 ret = IB_MAD_RESULT_SUCCESS;
1434 goto bail;
1436 /* FALLTHROUGH */
1437 default:
1438 smp->status |= IB_SMP_UNSUP_METH_ATTR;
1439 ret = reply(smp);
1440 goto bail;
1443 case IB_MGMT_METHOD_SET:
1444 switch (smp->attr_id) {
1445 case IB_SMP_ATTR_GUID_INFO:
1446 ret = recv_subn_set_guidinfo(smp, ibdev);
1447 goto bail;
1448 case IB_SMP_ATTR_PORT_INFO:
1449 ret = recv_subn_set_portinfo(smp, ibdev, port_num);
1450 goto bail;
1451 case IB_SMP_ATTR_PKEY_TABLE:
1452 ret = recv_subn_set_pkeytable(smp, ibdev);
1453 goto bail;
1454 case IB_SMP_ATTR_SM_INFO:
1455 if (dev->port_cap_flags & IB_PORT_SM_DISABLED) {
1456 ret = IB_MAD_RESULT_SUCCESS |
1457 IB_MAD_RESULT_CONSUMED;
1458 goto bail;
1460 if (dev->port_cap_flags & IB_PORT_SM) {
1461 ret = IB_MAD_RESULT_SUCCESS;
1462 goto bail;
1464 /* FALLTHROUGH */
1465 default:
1466 smp->status |= IB_SMP_UNSUP_METH_ATTR;
1467 ret = reply(smp);
1468 goto bail;
1471 case IB_MGMT_METHOD_GET_RESP:
1473 * The ib_mad module will call us to process responses
1474 * before checking for other consumers.
1475 * Just tell the caller to process it normally.
1477 ret = IB_MAD_RESULT_SUCCESS;
1478 goto bail;
1479 default:
1480 smp->status |= IB_SMP_UNSUP_METHOD;
1481 ret = reply(smp);
1484 bail:
1485 return ret;
1488 static int process_perf(struct ib_device *ibdev, u8 port_num,
1489 struct ib_mad *in_mad,
1490 struct ib_mad *out_mad)
1492 struct ib_perf *pmp = (struct ib_perf *)out_mad;
1493 int ret;
1495 *out_mad = *in_mad;
1496 if (pmp->class_version != 1) {
1497 pmp->status |= IB_SMP_UNSUP_VERSION;
1498 ret = reply((struct ib_smp *) pmp);
1499 goto bail;
1502 switch (pmp->method) {
1503 case IB_MGMT_METHOD_GET:
1504 switch (pmp->attr_id) {
1505 case IB_PMA_CLASS_PORT_INFO:
1506 ret = recv_pma_get_classportinfo(pmp);
1507 goto bail;
1508 case IB_PMA_PORT_SAMPLES_CONTROL:
1509 ret = recv_pma_get_portsamplescontrol(pmp, ibdev,
1510 port_num);
1511 goto bail;
1512 case IB_PMA_PORT_SAMPLES_RESULT:
1513 ret = recv_pma_get_portsamplesresult(pmp, ibdev);
1514 goto bail;
1515 case IB_PMA_PORT_SAMPLES_RESULT_EXT:
1516 ret = recv_pma_get_portsamplesresult_ext(pmp,
1517 ibdev);
1518 goto bail;
1519 case IB_PMA_PORT_COUNTERS:
1520 ret = recv_pma_get_portcounters(pmp, ibdev,
1521 port_num);
1522 goto bail;
1523 case IB_PMA_PORT_COUNTERS_EXT:
1524 ret = recv_pma_get_portcounters_ext(pmp, ibdev,
1525 port_num);
1526 goto bail;
1527 default:
1528 pmp->status |= IB_SMP_UNSUP_METH_ATTR;
1529 ret = reply((struct ib_smp *) pmp);
1530 goto bail;
1533 case IB_MGMT_METHOD_SET:
1534 switch (pmp->attr_id) {
1535 case IB_PMA_PORT_SAMPLES_CONTROL:
1536 ret = recv_pma_set_portsamplescontrol(pmp, ibdev,
1537 port_num);
1538 goto bail;
1539 case IB_PMA_PORT_COUNTERS:
1540 ret = recv_pma_set_portcounters(pmp, ibdev,
1541 port_num);
1542 goto bail;
1543 case IB_PMA_PORT_COUNTERS_EXT:
1544 ret = recv_pma_set_portcounters_ext(pmp, ibdev,
1545 port_num);
1546 goto bail;
1547 default:
1548 pmp->status |= IB_SMP_UNSUP_METH_ATTR;
1549 ret = reply((struct ib_smp *) pmp);
1550 goto bail;
1553 case IB_MGMT_METHOD_GET_RESP:
1555 * The ib_mad module will call us to process responses
1556 * before checking for other consumers.
1557 * Just tell the caller to process it normally.
1559 ret = IB_MAD_RESULT_SUCCESS;
1560 goto bail;
1561 default:
1562 pmp->status |= IB_SMP_UNSUP_METHOD;
1563 ret = reply((struct ib_smp *) pmp);
1566 bail:
1567 return ret;
1571 * ipath_process_mad - process an incoming MAD packet
1572 * @ibdev: the infiniband device this packet came in on
1573 * @mad_flags: MAD flags
1574 * @port_num: the port number this packet came in on
1575 * @in_wc: the work completion entry for this packet
1576 * @in_grh: the global route header for this packet
1577 * @in_mad: the incoming MAD
1578 * @out_mad: any outgoing MAD reply
1580 * Returns IB_MAD_RESULT_SUCCESS if this is a MAD that we are not
1581 * interested in processing.
1583 * Note that the verbs framework has already done the MAD sanity checks,
1584 * and hop count/pointer updating for IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE
1585 * MADs.
1587 * This is called by the ib_mad module.
1589 int ipath_process_mad(struct ib_device *ibdev, int mad_flags, u8 port_num,
1590 struct ib_wc *in_wc, struct ib_grh *in_grh,
1591 struct ib_mad *in_mad, struct ib_mad *out_mad)
1593 int ret;
1595 switch (in_mad->mad_hdr.mgmt_class) {
1596 case IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE:
1597 case IB_MGMT_CLASS_SUBN_LID_ROUTED:
1598 ret = process_subn(ibdev, mad_flags, port_num,
1599 in_mad, out_mad);
1600 goto bail;
1601 case IB_MGMT_CLASS_PERF_MGMT:
1602 ret = process_perf(ibdev, port_num, in_mad, out_mad);
1603 goto bail;
1604 default:
1605 ret = IB_MAD_RESULT_SUCCESS;
1608 bail:
1609 return ret;