Linux 3.11-rc3
[cris-mirror.git] / drivers / infiniband / hw / qib / qib_mad.c
blobccb119143d20568eb0570ab5b1360a78ca888e08
1 /*
2 * Copyright (c) 2012 Intel Corporation. All rights reserved.
3 * Copyright (c) 2006 - 2012 QLogic Corporation. All rights reserved.
4 * Copyright (c) 2005, 2006 PathScale, Inc. All rights reserved.
6 * This software is available to you under a choice of one of two
7 * licenses. You may choose to be licensed under the terms of the GNU
8 * General Public License (GPL) Version 2, available from the file
9 * COPYING in the main directory of this source tree, or the
10 * OpenIB.org BSD license below:
12 * Redistribution and use in source and binary forms, with or
13 * without modification, are permitted provided that the following
14 * conditions are met:
16 * - Redistributions of source code must retain the above
17 * copyright notice, this list of conditions and the following
18 * disclaimer.
20 * - Redistributions in binary form must reproduce the above
21 * copyright notice, this list of conditions and the following
22 * disclaimer in the documentation and/or other materials
23 * provided with the distribution.
25 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
26 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
27 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
28 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
29 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
30 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
31 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
32 * SOFTWARE.
35 #include <rdma/ib_smi.h>
37 #include "qib.h"
38 #include "qib_mad.h"
40 static int reply(struct ib_smp *smp)
43 * The verbs framework will handle the directed/LID route
44 * packet changes.
46 smp->method = IB_MGMT_METHOD_GET_RESP;
47 if (smp->mgmt_class == IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE)
48 smp->status |= IB_SMP_DIRECTION;
49 return IB_MAD_RESULT_SUCCESS | IB_MAD_RESULT_REPLY;
52 static int reply_failure(struct ib_smp *smp)
55 * The verbs framework will handle the directed/LID route
56 * packet changes.
58 smp->method = IB_MGMT_METHOD_GET_RESP;
59 if (smp->mgmt_class == IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE)
60 smp->status |= IB_SMP_DIRECTION;
61 return IB_MAD_RESULT_FAILURE | IB_MAD_RESULT_REPLY;
64 static void qib_send_trap(struct qib_ibport *ibp, void *data, unsigned len)
66 struct ib_mad_send_buf *send_buf;
67 struct ib_mad_agent *agent;
68 struct ib_smp *smp;
69 int ret;
70 unsigned long flags;
71 unsigned long timeout;
73 agent = ibp->send_agent;
74 if (!agent)
75 return;
77 /* o14-3.2.1 */
78 if (!(ppd_from_ibp(ibp)->lflags & QIBL_LINKACTIVE))
79 return;
81 /* o14-2 */
82 if (ibp->trap_timeout && time_before(jiffies, ibp->trap_timeout))
83 return;
85 send_buf = ib_create_send_mad(agent, 0, 0, 0, IB_MGMT_MAD_HDR,
86 IB_MGMT_MAD_DATA, GFP_ATOMIC);
87 if (IS_ERR(send_buf))
88 return;
90 smp = send_buf->mad;
91 smp->base_version = IB_MGMT_BASE_VERSION;
92 smp->mgmt_class = IB_MGMT_CLASS_SUBN_LID_ROUTED;
93 smp->class_version = 1;
94 smp->method = IB_MGMT_METHOD_TRAP;
95 ibp->tid++;
96 smp->tid = cpu_to_be64(ibp->tid);
97 smp->attr_id = IB_SMP_ATTR_NOTICE;
98 /* o14-1: smp->mkey = 0; */
99 memcpy(smp->data, data, len);
101 spin_lock_irqsave(&ibp->lock, flags);
102 if (!ibp->sm_ah) {
103 if (ibp->sm_lid != be16_to_cpu(IB_LID_PERMISSIVE)) {
104 struct ib_ah *ah;
106 ah = qib_create_qp0_ah(ibp, ibp->sm_lid);
107 if (IS_ERR(ah))
108 ret = PTR_ERR(ah);
109 else {
110 send_buf->ah = ah;
111 ibp->sm_ah = to_iah(ah);
112 ret = 0;
114 } else
115 ret = -EINVAL;
116 } else {
117 send_buf->ah = &ibp->sm_ah->ibah;
118 ret = 0;
120 spin_unlock_irqrestore(&ibp->lock, flags);
122 if (!ret)
123 ret = ib_post_send_mad(send_buf, NULL);
124 if (!ret) {
125 /* 4.096 usec. */
126 timeout = (4096 * (1UL << ibp->subnet_timeout)) / 1000;
127 ibp->trap_timeout = jiffies + usecs_to_jiffies(timeout);
128 } else {
129 ib_free_send_mad(send_buf);
130 ibp->trap_timeout = 0;
135 * Send a bad [PQ]_Key trap (ch. 14.3.8).
137 void qib_bad_pqkey(struct qib_ibport *ibp, __be16 trap_num, u32 key, u32 sl,
138 u32 qp1, u32 qp2, __be16 lid1, __be16 lid2)
140 struct ib_mad_notice_attr data;
142 if (trap_num == IB_NOTICE_TRAP_BAD_PKEY)
143 ibp->pkey_violations++;
144 else
145 ibp->qkey_violations++;
146 ibp->n_pkt_drops++;
148 /* Send violation trap */
149 data.generic_type = IB_NOTICE_TYPE_SECURITY;
150 data.prod_type_msb = 0;
151 data.prod_type_lsb = IB_NOTICE_PROD_CA;
152 data.trap_num = trap_num;
153 data.issuer_lid = cpu_to_be16(ppd_from_ibp(ibp)->lid);
154 data.toggle_count = 0;
155 memset(&data.details, 0, sizeof data.details);
156 data.details.ntc_257_258.lid1 = lid1;
157 data.details.ntc_257_258.lid2 = lid2;
158 data.details.ntc_257_258.key = cpu_to_be32(key);
159 data.details.ntc_257_258.sl_qp1 = cpu_to_be32((sl << 28) | qp1);
160 data.details.ntc_257_258.qp2 = cpu_to_be32(qp2);
162 qib_send_trap(ibp, &data, sizeof data);
166 * Send a bad M_Key trap (ch. 14.3.9).
168 static void qib_bad_mkey(struct qib_ibport *ibp, struct ib_smp *smp)
170 struct ib_mad_notice_attr data;
172 /* Send violation trap */
173 data.generic_type = IB_NOTICE_TYPE_SECURITY;
174 data.prod_type_msb = 0;
175 data.prod_type_lsb = IB_NOTICE_PROD_CA;
176 data.trap_num = IB_NOTICE_TRAP_BAD_MKEY;
177 data.issuer_lid = cpu_to_be16(ppd_from_ibp(ibp)->lid);
178 data.toggle_count = 0;
179 memset(&data.details, 0, sizeof data.details);
180 data.details.ntc_256.lid = data.issuer_lid;
181 data.details.ntc_256.method = smp->method;
182 data.details.ntc_256.attr_id = smp->attr_id;
183 data.details.ntc_256.attr_mod = smp->attr_mod;
184 data.details.ntc_256.mkey = smp->mkey;
185 if (smp->mgmt_class == IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE) {
186 u8 hop_cnt;
188 data.details.ntc_256.dr_slid = smp->dr_slid;
189 data.details.ntc_256.dr_trunc_hop = IB_NOTICE_TRAP_DR_NOTICE;
190 hop_cnt = smp->hop_cnt;
191 if (hop_cnt > ARRAY_SIZE(data.details.ntc_256.dr_rtn_path)) {
192 data.details.ntc_256.dr_trunc_hop |=
193 IB_NOTICE_TRAP_DR_TRUNC;
194 hop_cnt = ARRAY_SIZE(data.details.ntc_256.dr_rtn_path);
196 data.details.ntc_256.dr_trunc_hop |= hop_cnt;
197 memcpy(data.details.ntc_256.dr_rtn_path, smp->return_path,
198 hop_cnt);
201 qib_send_trap(ibp, &data, sizeof data);
205 * Send a Port Capability Mask Changed trap (ch. 14.3.11).
207 void qib_cap_mask_chg(struct qib_ibport *ibp)
209 struct ib_mad_notice_attr data;
211 data.generic_type = IB_NOTICE_TYPE_INFO;
212 data.prod_type_msb = 0;
213 data.prod_type_lsb = IB_NOTICE_PROD_CA;
214 data.trap_num = IB_NOTICE_TRAP_CAP_MASK_CHG;
215 data.issuer_lid = cpu_to_be16(ppd_from_ibp(ibp)->lid);
216 data.toggle_count = 0;
217 memset(&data.details, 0, sizeof data.details);
218 data.details.ntc_144.lid = data.issuer_lid;
219 data.details.ntc_144.new_cap_mask = cpu_to_be32(ibp->port_cap_flags);
221 qib_send_trap(ibp, &data, sizeof data);
225 * Send a System Image GUID Changed trap (ch. 14.3.12).
227 void qib_sys_guid_chg(struct qib_ibport *ibp)
229 struct ib_mad_notice_attr data;
231 data.generic_type = IB_NOTICE_TYPE_INFO;
232 data.prod_type_msb = 0;
233 data.prod_type_lsb = IB_NOTICE_PROD_CA;
234 data.trap_num = IB_NOTICE_TRAP_SYS_GUID_CHG;
235 data.issuer_lid = cpu_to_be16(ppd_from_ibp(ibp)->lid);
236 data.toggle_count = 0;
237 memset(&data.details, 0, sizeof data.details);
238 data.details.ntc_145.lid = data.issuer_lid;
239 data.details.ntc_145.new_sys_guid = ib_qib_sys_image_guid;
241 qib_send_trap(ibp, &data, sizeof data);
245 * Send a Node Description Changed trap (ch. 14.3.13).
247 void qib_node_desc_chg(struct qib_ibport *ibp)
249 struct ib_mad_notice_attr data;
251 data.generic_type = IB_NOTICE_TYPE_INFO;
252 data.prod_type_msb = 0;
253 data.prod_type_lsb = IB_NOTICE_PROD_CA;
254 data.trap_num = IB_NOTICE_TRAP_CAP_MASK_CHG;
255 data.issuer_lid = cpu_to_be16(ppd_from_ibp(ibp)->lid);
256 data.toggle_count = 0;
257 memset(&data.details, 0, sizeof data.details);
258 data.details.ntc_144.lid = data.issuer_lid;
259 data.details.ntc_144.local_changes = 1;
260 data.details.ntc_144.change_flags = IB_NOTICE_TRAP_NODE_DESC_CHG;
262 qib_send_trap(ibp, &data, sizeof data);
265 static int subn_get_nodedescription(struct ib_smp *smp,
266 struct ib_device *ibdev)
268 if (smp->attr_mod)
269 smp->status |= IB_SMP_INVALID_FIELD;
271 memcpy(smp->data, ibdev->node_desc, sizeof(smp->data));
273 return reply(smp);
276 static int subn_get_nodeinfo(struct ib_smp *smp, struct ib_device *ibdev,
277 u8 port)
279 struct ib_node_info *nip = (struct ib_node_info *)&smp->data;
280 struct qib_devdata *dd = dd_from_ibdev(ibdev);
281 u32 vendor, majrev, minrev;
282 unsigned pidx = port - 1; /* IB number port from 1, hdw from 0 */
284 /* GUID 0 is illegal */
285 if (smp->attr_mod || pidx >= dd->num_pports ||
286 dd->pport[pidx].guid == 0)
287 smp->status |= IB_SMP_INVALID_FIELD;
288 else
289 nip->port_guid = dd->pport[pidx].guid;
291 nip->base_version = 1;
292 nip->class_version = 1;
293 nip->node_type = 1; /* channel adapter */
294 nip->num_ports = ibdev->phys_port_cnt;
295 /* This is already in network order */
296 nip->sys_guid = ib_qib_sys_image_guid;
297 nip->node_guid = dd->pport->guid; /* Use first-port GUID as node */
298 nip->partition_cap = cpu_to_be16(qib_get_npkeys(dd));
299 nip->device_id = cpu_to_be16(dd->deviceid);
300 majrev = dd->majrev;
301 minrev = dd->minrev;
302 nip->revision = cpu_to_be32((majrev << 16) | minrev);
303 nip->local_port_num = port;
304 vendor = dd->vendorid;
305 nip->vendor_id[0] = QIB_SRC_OUI_1;
306 nip->vendor_id[1] = QIB_SRC_OUI_2;
307 nip->vendor_id[2] = QIB_SRC_OUI_3;
309 return reply(smp);
312 static int subn_get_guidinfo(struct ib_smp *smp, struct ib_device *ibdev,
313 u8 port)
315 struct qib_devdata *dd = dd_from_ibdev(ibdev);
316 u32 startgx = 8 * be32_to_cpu(smp->attr_mod);
317 __be64 *p = (__be64 *) smp->data;
318 unsigned pidx = port - 1; /* IB number port from 1, hdw from 0 */
320 /* 32 blocks of 8 64-bit GUIDs per block */
322 memset(smp->data, 0, sizeof(smp->data));
324 if (startgx == 0 && pidx < dd->num_pports) {
325 struct qib_pportdata *ppd = dd->pport + pidx;
326 struct qib_ibport *ibp = &ppd->ibport_data;
327 __be64 g = ppd->guid;
328 unsigned i;
330 /* GUID 0 is illegal */
331 if (g == 0)
332 smp->status |= IB_SMP_INVALID_FIELD;
333 else {
334 /* The first is a copy of the read-only HW GUID. */
335 p[0] = g;
336 for (i = 1; i < QIB_GUIDS_PER_PORT; i++)
337 p[i] = ibp->guids[i - 1];
339 } else
340 smp->status |= IB_SMP_INVALID_FIELD;
342 return reply(smp);
345 static void set_link_width_enabled(struct qib_pportdata *ppd, u32 w)
347 (void) ppd->dd->f_set_ib_cfg(ppd, QIB_IB_CFG_LWID_ENB, w);
350 static void set_link_speed_enabled(struct qib_pportdata *ppd, u32 s)
352 (void) ppd->dd->f_set_ib_cfg(ppd, QIB_IB_CFG_SPD_ENB, s);
355 static int get_overrunthreshold(struct qib_pportdata *ppd)
357 return ppd->dd->f_get_ib_cfg(ppd, QIB_IB_CFG_OVERRUN_THRESH);
361 * set_overrunthreshold - set the overrun threshold
362 * @ppd: the physical port data
363 * @n: the new threshold
365 * Note that this will only take effect when the link state changes.
367 static int set_overrunthreshold(struct qib_pportdata *ppd, unsigned n)
369 (void) ppd->dd->f_set_ib_cfg(ppd, QIB_IB_CFG_OVERRUN_THRESH,
370 (u32)n);
371 return 0;
374 static int get_phyerrthreshold(struct qib_pportdata *ppd)
376 return ppd->dd->f_get_ib_cfg(ppd, QIB_IB_CFG_PHYERR_THRESH);
380 * set_phyerrthreshold - set the physical error threshold
381 * @ppd: the physical port data
382 * @n: the new threshold
384 * Note that this will only take effect when the link state changes.
386 static int set_phyerrthreshold(struct qib_pportdata *ppd, unsigned n)
388 (void) ppd->dd->f_set_ib_cfg(ppd, QIB_IB_CFG_PHYERR_THRESH,
389 (u32)n);
390 return 0;
394 * get_linkdowndefaultstate - get the default linkdown state
395 * @ppd: the physical port data
397 * Returns zero if the default is POLL, 1 if the default is SLEEP.
399 static int get_linkdowndefaultstate(struct qib_pportdata *ppd)
401 return ppd->dd->f_get_ib_cfg(ppd, QIB_IB_CFG_LINKDEFAULT) ==
402 IB_LINKINITCMD_SLEEP;
405 static int check_mkey(struct qib_ibport *ibp, struct ib_smp *smp, int mad_flags)
407 int valid_mkey = 0;
408 int ret = 0;
410 /* Is the mkey in the process of expiring? */
411 if (ibp->mkey_lease_timeout &&
412 time_after_eq(jiffies, ibp->mkey_lease_timeout)) {
413 /* Clear timeout and mkey protection field. */
414 ibp->mkey_lease_timeout = 0;
415 ibp->mkeyprot = 0;
418 if ((mad_flags & IB_MAD_IGNORE_MKEY) || ibp->mkey == 0 ||
419 ibp->mkey == smp->mkey)
420 valid_mkey = 1;
422 /* Unset lease timeout on any valid Get/Set/TrapRepress */
423 if (valid_mkey && ibp->mkey_lease_timeout &&
424 (smp->method == IB_MGMT_METHOD_GET ||
425 smp->method == IB_MGMT_METHOD_SET ||
426 smp->method == IB_MGMT_METHOD_TRAP_REPRESS))
427 ibp->mkey_lease_timeout = 0;
429 if (!valid_mkey) {
430 switch (smp->method) {
431 case IB_MGMT_METHOD_GET:
432 /* Bad mkey not a violation below level 2 */
433 if (ibp->mkeyprot < 2)
434 break;
435 case IB_MGMT_METHOD_SET:
436 case IB_MGMT_METHOD_TRAP_REPRESS:
437 if (ibp->mkey_violations != 0xFFFF)
438 ++ibp->mkey_violations;
439 if (!ibp->mkey_lease_timeout && ibp->mkey_lease_period)
440 ibp->mkey_lease_timeout = jiffies +
441 ibp->mkey_lease_period * HZ;
442 /* Generate a trap notice. */
443 qib_bad_mkey(ibp, smp);
444 ret = 1;
448 return ret;
451 static int subn_get_portinfo(struct ib_smp *smp, struct ib_device *ibdev,
452 u8 port)
454 struct qib_devdata *dd;
455 struct qib_pportdata *ppd;
456 struct qib_ibport *ibp;
457 struct ib_port_info *pip = (struct ib_port_info *)smp->data;
458 u8 mtu;
459 int ret;
460 u32 state;
461 u32 port_num = be32_to_cpu(smp->attr_mod);
463 if (port_num == 0)
464 port_num = port;
465 else {
466 if (port_num > ibdev->phys_port_cnt) {
467 smp->status |= IB_SMP_INVALID_FIELD;
468 ret = reply(smp);
469 goto bail;
471 if (port_num != port) {
472 ibp = to_iport(ibdev, port_num);
473 ret = check_mkey(ibp, smp, 0);
474 if (ret) {
475 ret = IB_MAD_RESULT_FAILURE;
476 goto bail;
481 dd = dd_from_ibdev(ibdev);
482 /* IB numbers ports from 1, hdw from 0 */
483 ppd = dd->pport + (port_num - 1);
484 ibp = &ppd->ibport_data;
486 /* Clear all fields. Only set the non-zero fields. */
487 memset(smp->data, 0, sizeof(smp->data));
489 /* Only return the mkey if the protection field allows it. */
490 if (!(smp->method == IB_MGMT_METHOD_GET &&
491 ibp->mkey != smp->mkey &&
492 ibp->mkeyprot == 1))
493 pip->mkey = ibp->mkey;
494 pip->gid_prefix = ibp->gid_prefix;
495 pip->lid = cpu_to_be16(ppd->lid);
496 pip->sm_lid = cpu_to_be16(ibp->sm_lid);
497 pip->cap_mask = cpu_to_be32(ibp->port_cap_flags);
498 /* pip->diag_code; */
499 pip->mkey_lease_period = cpu_to_be16(ibp->mkey_lease_period);
500 pip->local_port_num = port;
501 pip->link_width_enabled = ppd->link_width_enabled;
502 pip->link_width_supported = ppd->link_width_supported;
503 pip->link_width_active = ppd->link_width_active;
504 state = dd->f_iblink_state(ppd->lastibcstat);
505 pip->linkspeed_portstate = ppd->link_speed_supported << 4 | state;
507 pip->portphysstate_linkdown =
508 (dd->f_ibphys_portstate(ppd->lastibcstat) << 4) |
509 (get_linkdowndefaultstate(ppd) ? 1 : 2);
510 pip->mkeyprot_resv_lmc = (ibp->mkeyprot << 6) | ppd->lmc;
511 pip->linkspeedactive_enabled = (ppd->link_speed_active << 4) |
512 ppd->link_speed_enabled;
513 switch (ppd->ibmtu) {
514 default: /* something is wrong; fall through */
515 case 4096:
516 mtu = IB_MTU_4096;
517 break;
518 case 2048:
519 mtu = IB_MTU_2048;
520 break;
521 case 1024:
522 mtu = IB_MTU_1024;
523 break;
524 case 512:
525 mtu = IB_MTU_512;
526 break;
527 case 256:
528 mtu = IB_MTU_256;
529 break;
531 pip->neighbormtu_mastersmsl = (mtu << 4) | ibp->sm_sl;
532 pip->vlcap_inittype = ppd->vls_supported << 4; /* InitType = 0 */
533 pip->vl_high_limit = ibp->vl_high_limit;
534 pip->vl_arb_high_cap =
535 dd->f_get_ib_cfg(ppd, QIB_IB_CFG_VL_HIGH_CAP);
536 pip->vl_arb_low_cap =
537 dd->f_get_ib_cfg(ppd, QIB_IB_CFG_VL_LOW_CAP);
538 /* InitTypeReply = 0 */
539 pip->inittypereply_mtucap = qib_ibmtu ? qib_ibmtu : IB_MTU_4096;
540 /* HCAs ignore VLStallCount and HOQLife */
541 /* pip->vlstallcnt_hoqlife; */
542 pip->operationalvl_pei_peo_fpi_fpo =
543 dd->f_get_ib_cfg(ppd, QIB_IB_CFG_OP_VLS) << 4;
544 pip->mkey_violations = cpu_to_be16(ibp->mkey_violations);
545 /* P_KeyViolations are counted by hardware. */
546 pip->pkey_violations = cpu_to_be16(ibp->pkey_violations);
547 pip->qkey_violations = cpu_to_be16(ibp->qkey_violations);
548 /* Only the hardware GUID is supported for now */
549 pip->guid_cap = QIB_GUIDS_PER_PORT;
550 pip->clientrereg_resv_subnetto = ibp->subnet_timeout;
551 /* 32.768 usec. response time (guessing) */
552 pip->resv_resptimevalue = 3;
553 pip->localphyerrors_overrunerrors =
554 (get_phyerrthreshold(ppd) << 4) |
555 get_overrunthreshold(ppd);
556 /* pip->max_credit_hint; */
557 if (ibp->port_cap_flags & IB_PORT_LINK_LATENCY_SUP) {
558 u32 v;
560 v = dd->f_get_ib_cfg(ppd, QIB_IB_CFG_LINKLATENCY);
561 pip->link_roundtrip_latency[0] = v >> 16;
562 pip->link_roundtrip_latency[1] = v >> 8;
563 pip->link_roundtrip_latency[2] = v;
566 ret = reply(smp);
568 bail:
569 return ret;
573 * get_pkeys - return the PKEY table
574 * @dd: the qlogic_ib device
575 * @port: the IB port number
576 * @pkeys: the pkey table is placed here
578 static int get_pkeys(struct qib_devdata *dd, u8 port, u16 *pkeys)
580 struct qib_pportdata *ppd = dd->pport + port - 1;
582 * always a kernel context, no locking needed.
583 * If we get here with ppd setup, no need to check
584 * that pd is valid.
586 struct qib_ctxtdata *rcd = dd->rcd[ppd->hw_pidx];
588 memcpy(pkeys, rcd->pkeys, sizeof(rcd->pkeys));
590 return 0;
593 static int subn_get_pkeytable(struct ib_smp *smp, struct ib_device *ibdev,
594 u8 port)
596 u32 startpx = 32 * (be32_to_cpu(smp->attr_mod) & 0xffff);
597 u16 *p = (u16 *) smp->data;
598 __be16 *q = (__be16 *) smp->data;
600 /* 64 blocks of 32 16-bit P_Key entries */
602 memset(smp->data, 0, sizeof(smp->data));
603 if (startpx == 0) {
604 struct qib_devdata *dd = dd_from_ibdev(ibdev);
605 unsigned i, n = qib_get_npkeys(dd);
607 get_pkeys(dd, port, p);
609 for (i = 0; i < n; i++)
610 q[i] = cpu_to_be16(p[i]);
611 } else
612 smp->status |= IB_SMP_INVALID_FIELD;
614 return reply(smp);
617 static int subn_set_guidinfo(struct ib_smp *smp, struct ib_device *ibdev,
618 u8 port)
620 struct qib_devdata *dd = dd_from_ibdev(ibdev);
621 u32 startgx = 8 * be32_to_cpu(smp->attr_mod);
622 __be64 *p = (__be64 *) smp->data;
623 unsigned pidx = port - 1; /* IB number port from 1, hdw from 0 */
625 /* 32 blocks of 8 64-bit GUIDs per block */
627 if (startgx == 0 && pidx < dd->num_pports) {
628 struct qib_pportdata *ppd = dd->pport + pidx;
629 struct qib_ibport *ibp = &ppd->ibport_data;
630 unsigned i;
632 /* The first entry is read-only. */
633 for (i = 1; i < QIB_GUIDS_PER_PORT; i++)
634 ibp->guids[i - 1] = p[i];
635 } else
636 smp->status |= IB_SMP_INVALID_FIELD;
638 /* The only GUID we support is the first read-only entry. */
639 return subn_get_guidinfo(smp, ibdev, port);
643 * subn_set_portinfo - set port information
644 * @smp: the incoming SM packet
645 * @ibdev: the infiniband device
646 * @port: the port on the device
648 * Set Portinfo (see ch. 14.2.5.6).
650 static int subn_set_portinfo(struct ib_smp *smp, struct ib_device *ibdev,
651 u8 port)
653 struct ib_port_info *pip = (struct ib_port_info *)smp->data;
654 struct ib_event event;
655 struct qib_devdata *dd;
656 struct qib_pportdata *ppd;
657 struct qib_ibport *ibp;
658 u8 clientrereg = (pip->clientrereg_resv_subnetto & 0x80);
659 unsigned long flags;
660 u16 lid, smlid;
661 u8 lwe;
662 u8 lse;
663 u8 state;
664 u8 vls;
665 u8 msl;
666 u16 lstate;
667 int ret, ore, mtu;
668 u32 port_num = be32_to_cpu(smp->attr_mod);
670 if (port_num == 0)
671 port_num = port;
672 else {
673 if (port_num > ibdev->phys_port_cnt)
674 goto err;
675 /* Port attributes can only be set on the receiving port */
676 if (port_num != port)
677 goto get_only;
680 dd = dd_from_ibdev(ibdev);
681 /* IB numbers ports from 1, hdw from 0 */
682 ppd = dd->pport + (port_num - 1);
683 ibp = &ppd->ibport_data;
684 event.device = ibdev;
685 event.element.port_num = port;
687 ibp->mkey = pip->mkey;
688 ibp->gid_prefix = pip->gid_prefix;
689 ibp->mkey_lease_period = be16_to_cpu(pip->mkey_lease_period);
691 lid = be16_to_cpu(pip->lid);
692 /* Must be a valid unicast LID address. */
693 if (lid == 0 || lid >= QIB_MULTICAST_LID_BASE)
694 smp->status |= IB_SMP_INVALID_FIELD;
695 else if (ppd->lid != lid || ppd->lmc != (pip->mkeyprot_resv_lmc & 7)) {
696 if (ppd->lid != lid)
697 qib_set_uevent_bits(ppd, _QIB_EVENT_LID_CHANGE_BIT);
698 if (ppd->lmc != (pip->mkeyprot_resv_lmc & 7))
699 qib_set_uevent_bits(ppd, _QIB_EVENT_LMC_CHANGE_BIT);
700 qib_set_lid(ppd, lid, pip->mkeyprot_resv_lmc & 7);
701 event.event = IB_EVENT_LID_CHANGE;
702 ib_dispatch_event(&event);
705 smlid = be16_to_cpu(pip->sm_lid);
706 msl = pip->neighbormtu_mastersmsl & 0xF;
707 /* Must be a valid unicast LID address. */
708 if (smlid == 0 || smlid >= QIB_MULTICAST_LID_BASE)
709 smp->status |= IB_SMP_INVALID_FIELD;
710 else if (smlid != ibp->sm_lid || msl != ibp->sm_sl) {
711 spin_lock_irqsave(&ibp->lock, flags);
712 if (ibp->sm_ah) {
713 if (smlid != ibp->sm_lid)
714 ibp->sm_ah->attr.dlid = smlid;
715 if (msl != ibp->sm_sl)
716 ibp->sm_ah->attr.sl = msl;
718 spin_unlock_irqrestore(&ibp->lock, flags);
719 if (smlid != ibp->sm_lid)
720 ibp->sm_lid = smlid;
721 if (msl != ibp->sm_sl)
722 ibp->sm_sl = msl;
723 event.event = IB_EVENT_SM_CHANGE;
724 ib_dispatch_event(&event);
727 /* Allow 1x or 4x to be set (see 14.2.6.6). */
728 lwe = pip->link_width_enabled;
729 if (lwe) {
730 if (lwe == 0xFF)
731 set_link_width_enabled(ppd, ppd->link_width_supported);
732 else if (lwe >= 16 || (lwe & ~ppd->link_width_supported))
733 smp->status |= IB_SMP_INVALID_FIELD;
734 else if (lwe != ppd->link_width_enabled)
735 set_link_width_enabled(ppd, lwe);
738 lse = pip->linkspeedactive_enabled & 0xF;
739 if (lse) {
741 * The IB 1.2 spec. only allows link speed values
742 * 1, 3, 5, 7, 15. 1.2.1 extended to allow specific
743 * speeds.
745 if (lse == 15)
746 set_link_speed_enabled(ppd,
747 ppd->link_speed_supported);
748 else if (lse >= 8 || (lse & ~ppd->link_speed_supported))
749 smp->status |= IB_SMP_INVALID_FIELD;
750 else if (lse != ppd->link_speed_enabled)
751 set_link_speed_enabled(ppd, lse);
754 /* Set link down default state. */
755 switch (pip->portphysstate_linkdown & 0xF) {
756 case 0: /* NOP */
757 break;
758 case 1: /* SLEEP */
759 (void) dd->f_set_ib_cfg(ppd, QIB_IB_CFG_LINKDEFAULT,
760 IB_LINKINITCMD_SLEEP);
761 break;
762 case 2: /* POLL */
763 (void) dd->f_set_ib_cfg(ppd, QIB_IB_CFG_LINKDEFAULT,
764 IB_LINKINITCMD_POLL);
765 break;
766 default:
767 smp->status |= IB_SMP_INVALID_FIELD;
770 ibp->mkeyprot = pip->mkeyprot_resv_lmc >> 6;
771 ibp->vl_high_limit = pip->vl_high_limit;
772 (void) dd->f_set_ib_cfg(ppd, QIB_IB_CFG_VL_HIGH_LIMIT,
773 ibp->vl_high_limit);
775 mtu = ib_mtu_enum_to_int((pip->neighbormtu_mastersmsl >> 4) & 0xF);
776 if (mtu == -1)
777 smp->status |= IB_SMP_INVALID_FIELD;
778 else
779 qib_set_mtu(ppd, mtu);
781 /* Set operational VLs */
782 vls = (pip->operationalvl_pei_peo_fpi_fpo >> 4) & 0xF;
783 if (vls) {
784 if (vls > ppd->vls_supported)
785 smp->status |= IB_SMP_INVALID_FIELD;
786 else
787 (void) dd->f_set_ib_cfg(ppd, QIB_IB_CFG_OP_VLS, vls);
790 if (pip->mkey_violations == 0)
791 ibp->mkey_violations = 0;
793 if (pip->pkey_violations == 0)
794 ibp->pkey_violations = 0;
796 if (pip->qkey_violations == 0)
797 ibp->qkey_violations = 0;
799 ore = pip->localphyerrors_overrunerrors;
800 if (set_phyerrthreshold(ppd, (ore >> 4) & 0xF))
801 smp->status |= IB_SMP_INVALID_FIELD;
803 if (set_overrunthreshold(ppd, (ore & 0xF)))
804 smp->status |= IB_SMP_INVALID_FIELD;
806 ibp->subnet_timeout = pip->clientrereg_resv_subnetto & 0x1F;
809 * Do the port state change now that the other link parameters
810 * have been set.
811 * Changing the port physical state only makes sense if the link
812 * is down or is being set to down.
814 state = pip->linkspeed_portstate & 0xF;
815 lstate = (pip->portphysstate_linkdown >> 4) & 0xF;
816 if (lstate && !(state == IB_PORT_DOWN || state == IB_PORT_NOP))
817 smp->status |= IB_SMP_INVALID_FIELD;
820 * Only state changes of DOWN, ARM, and ACTIVE are valid
821 * and must be in the correct state to take effect (see 7.2.6).
823 switch (state) {
824 case IB_PORT_NOP:
825 if (lstate == 0)
826 break;
827 /* FALLTHROUGH */
828 case IB_PORT_DOWN:
829 if (lstate == 0)
830 lstate = QIB_IB_LINKDOWN_ONLY;
831 else if (lstate == 1)
832 lstate = QIB_IB_LINKDOWN_SLEEP;
833 else if (lstate == 2)
834 lstate = QIB_IB_LINKDOWN;
835 else if (lstate == 3)
836 lstate = QIB_IB_LINKDOWN_DISABLE;
837 else {
838 smp->status |= IB_SMP_INVALID_FIELD;
839 break;
841 spin_lock_irqsave(&ppd->lflags_lock, flags);
842 ppd->lflags &= ~QIBL_LINKV;
843 spin_unlock_irqrestore(&ppd->lflags_lock, flags);
844 qib_set_linkstate(ppd, lstate);
846 * Don't send a reply if the response would be sent
847 * through the disabled port.
849 if (lstate == QIB_IB_LINKDOWN_DISABLE && smp->hop_cnt) {
850 ret = IB_MAD_RESULT_SUCCESS | IB_MAD_RESULT_CONSUMED;
851 goto done;
853 qib_wait_linkstate(ppd, QIBL_LINKV, 10);
854 break;
855 case IB_PORT_ARMED:
856 qib_set_linkstate(ppd, QIB_IB_LINKARM);
857 break;
858 case IB_PORT_ACTIVE:
859 qib_set_linkstate(ppd, QIB_IB_LINKACTIVE);
860 break;
861 default:
862 smp->status |= IB_SMP_INVALID_FIELD;
865 if (clientrereg) {
866 event.event = IB_EVENT_CLIENT_REREGISTER;
867 ib_dispatch_event(&event);
870 ret = subn_get_portinfo(smp, ibdev, port);
872 /* restore re-reg bit per o14-12.2.1 */
873 pip->clientrereg_resv_subnetto |= clientrereg;
875 goto get_only;
877 err:
878 smp->status |= IB_SMP_INVALID_FIELD;
879 get_only:
880 ret = subn_get_portinfo(smp, ibdev, port);
881 done:
882 return ret;
886 * rm_pkey - decrecment the reference count for the given PKEY
887 * @dd: the qlogic_ib device
888 * @key: the PKEY index
890 * Return true if this was the last reference and the hardware table entry
891 * needs to be changed.
893 static int rm_pkey(struct qib_pportdata *ppd, u16 key)
895 int i;
896 int ret;
898 for (i = 0; i < ARRAY_SIZE(ppd->pkeys); i++) {
899 if (ppd->pkeys[i] != key)
900 continue;
901 if (atomic_dec_and_test(&ppd->pkeyrefs[i])) {
902 ppd->pkeys[i] = 0;
903 ret = 1;
904 goto bail;
906 break;
909 ret = 0;
911 bail:
912 return ret;
916 * add_pkey - add the given PKEY to the hardware table
917 * @dd: the qlogic_ib device
918 * @key: the PKEY
920 * Return an error code if unable to add the entry, zero if no change,
921 * or 1 if the hardware PKEY register needs to be updated.
923 static int add_pkey(struct qib_pportdata *ppd, u16 key)
925 int i;
926 u16 lkey = key & 0x7FFF;
927 int any = 0;
928 int ret;
930 if (lkey == 0x7FFF) {
931 ret = 0;
932 goto bail;
935 /* Look for an empty slot or a matching PKEY. */
936 for (i = 0; i < ARRAY_SIZE(ppd->pkeys); i++) {
937 if (!ppd->pkeys[i]) {
938 any++;
939 continue;
941 /* If it matches exactly, try to increment the ref count */
942 if (ppd->pkeys[i] == key) {
943 if (atomic_inc_return(&ppd->pkeyrefs[i]) > 1) {
944 ret = 0;
945 goto bail;
947 /* Lost the race. Look for an empty slot below. */
948 atomic_dec(&ppd->pkeyrefs[i]);
949 any++;
952 * It makes no sense to have both the limited and unlimited
953 * PKEY set at the same time since the unlimited one will
954 * disable the limited one.
956 if ((ppd->pkeys[i] & 0x7FFF) == lkey) {
957 ret = -EEXIST;
958 goto bail;
961 if (!any) {
962 ret = -EBUSY;
963 goto bail;
965 for (i = 0; i < ARRAY_SIZE(ppd->pkeys); i++) {
966 if (!ppd->pkeys[i] &&
967 atomic_inc_return(&ppd->pkeyrefs[i]) == 1) {
968 /* for qibstats, etc. */
969 ppd->pkeys[i] = key;
970 ret = 1;
971 goto bail;
974 ret = -EBUSY;
976 bail:
977 return ret;
981 * set_pkeys - set the PKEY table for ctxt 0
982 * @dd: the qlogic_ib device
983 * @port: the IB port number
984 * @pkeys: the PKEY table
986 static int set_pkeys(struct qib_devdata *dd, u8 port, u16 *pkeys)
988 struct qib_pportdata *ppd;
989 struct qib_ctxtdata *rcd;
990 int i;
991 int changed = 0;
994 * IB port one/two always maps to context zero/one,
995 * always a kernel context, no locking needed
996 * If we get here with ppd setup, no need to check
997 * that rcd is valid.
999 ppd = dd->pport + (port - 1);
1000 rcd = dd->rcd[ppd->hw_pidx];
1002 for (i = 0; i < ARRAY_SIZE(rcd->pkeys); i++) {
1003 u16 key = pkeys[i];
1004 u16 okey = rcd->pkeys[i];
1006 if (key == okey)
1007 continue;
1009 * The value of this PKEY table entry is changing.
1010 * Remove the old entry in the hardware's array of PKEYs.
1012 if (okey & 0x7FFF)
1013 changed |= rm_pkey(ppd, okey);
1014 if (key & 0x7FFF) {
1015 int ret = add_pkey(ppd, key);
1017 if (ret < 0)
1018 key = 0;
1019 else
1020 changed |= ret;
1022 rcd->pkeys[i] = key;
1024 if (changed) {
1025 struct ib_event event;
1027 (void) dd->f_set_ib_cfg(ppd, QIB_IB_CFG_PKEYS, 0);
1029 event.event = IB_EVENT_PKEY_CHANGE;
1030 event.device = &dd->verbs_dev.ibdev;
1031 event.element.port_num = 1;
1032 ib_dispatch_event(&event);
1034 return 0;
1037 static int subn_set_pkeytable(struct ib_smp *smp, struct ib_device *ibdev,
1038 u8 port)
1040 u32 startpx = 32 * (be32_to_cpu(smp->attr_mod) & 0xffff);
1041 __be16 *p = (__be16 *) smp->data;
1042 u16 *q = (u16 *) smp->data;
1043 struct qib_devdata *dd = dd_from_ibdev(ibdev);
1044 unsigned i, n = qib_get_npkeys(dd);
1046 for (i = 0; i < n; i++)
1047 q[i] = be16_to_cpu(p[i]);
1049 if (startpx != 0 || set_pkeys(dd, port, q) != 0)
1050 smp->status |= IB_SMP_INVALID_FIELD;
1052 return subn_get_pkeytable(smp, ibdev, port);
1055 static int subn_get_sl_to_vl(struct ib_smp *smp, struct ib_device *ibdev,
1056 u8 port)
1058 struct qib_ibport *ibp = to_iport(ibdev, port);
1059 u8 *p = (u8 *) smp->data;
1060 unsigned i;
1062 memset(smp->data, 0, sizeof(smp->data));
1064 if (!(ibp->port_cap_flags & IB_PORT_SL_MAP_SUP))
1065 smp->status |= IB_SMP_UNSUP_METHOD;
1066 else
1067 for (i = 0; i < ARRAY_SIZE(ibp->sl_to_vl); i += 2)
1068 *p++ = (ibp->sl_to_vl[i] << 4) | ibp->sl_to_vl[i + 1];
1070 return reply(smp);
1073 static int subn_set_sl_to_vl(struct ib_smp *smp, struct ib_device *ibdev,
1074 u8 port)
1076 struct qib_ibport *ibp = to_iport(ibdev, port);
1077 u8 *p = (u8 *) smp->data;
1078 unsigned i;
1080 if (!(ibp->port_cap_flags & IB_PORT_SL_MAP_SUP)) {
1081 smp->status |= IB_SMP_UNSUP_METHOD;
1082 return reply(smp);
1085 for (i = 0; i < ARRAY_SIZE(ibp->sl_to_vl); i += 2, p++) {
1086 ibp->sl_to_vl[i] = *p >> 4;
1087 ibp->sl_to_vl[i + 1] = *p & 0xF;
1089 qib_set_uevent_bits(ppd_from_ibp(to_iport(ibdev, port)),
1090 _QIB_EVENT_SL2VL_CHANGE_BIT);
1092 return subn_get_sl_to_vl(smp, ibdev, port);
1095 static int subn_get_vl_arb(struct ib_smp *smp, struct ib_device *ibdev,
1096 u8 port)
1098 unsigned which = be32_to_cpu(smp->attr_mod) >> 16;
1099 struct qib_pportdata *ppd = ppd_from_ibp(to_iport(ibdev, port));
1101 memset(smp->data, 0, sizeof(smp->data));
1103 if (ppd->vls_supported == IB_VL_VL0)
1104 smp->status |= IB_SMP_UNSUP_METHOD;
1105 else if (which == IB_VLARB_LOWPRI_0_31)
1106 (void) ppd->dd->f_get_ib_table(ppd, QIB_IB_TBL_VL_LOW_ARB,
1107 smp->data);
1108 else if (which == IB_VLARB_HIGHPRI_0_31)
1109 (void) ppd->dd->f_get_ib_table(ppd, QIB_IB_TBL_VL_HIGH_ARB,
1110 smp->data);
1111 else
1112 smp->status |= IB_SMP_INVALID_FIELD;
1114 return reply(smp);
1117 static int subn_set_vl_arb(struct ib_smp *smp, struct ib_device *ibdev,
1118 u8 port)
1120 unsigned which = be32_to_cpu(smp->attr_mod) >> 16;
1121 struct qib_pportdata *ppd = ppd_from_ibp(to_iport(ibdev, port));
1123 if (ppd->vls_supported == IB_VL_VL0)
1124 smp->status |= IB_SMP_UNSUP_METHOD;
1125 else if (which == IB_VLARB_LOWPRI_0_31)
1126 (void) ppd->dd->f_set_ib_table(ppd, QIB_IB_TBL_VL_LOW_ARB,
1127 smp->data);
1128 else if (which == IB_VLARB_HIGHPRI_0_31)
1129 (void) ppd->dd->f_set_ib_table(ppd, QIB_IB_TBL_VL_HIGH_ARB,
1130 smp->data);
1131 else
1132 smp->status |= IB_SMP_INVALID_FIELD;
1134 return subn_get_vl_arb(smp, ibdev, port);
1137 static int subn_trap_repress(struct ib_smp *smp, struct ib_device *ibdev,
1138 u8 port)
1141 * For now, we only send the trap once so no need to process this.
1142 * o13-6, o13-7,
1143 * o14-3.a4 The SMA shall not send any message in response to a valid
1144 * SubnTrapRepress() message.
1146 return IB_MAD_RESULT_SUCCESS | IB_MAD_RESULT_CONSUMED;
1149 static int pma_get_classportinfo(struct ib_pma_mad *pmp,
1150 struct ib_device *ibdev)
1152 struct ib_class_port_info *p =
1153 (struct ib_class_port_info *)pmp->data;
1154 struct qib_devdata *dd = dd_from_ibdev(ibdev);
1156 memset(pmp->data, 0, sizeof(pmp->data));
1158 if (pmp->mad_hdr.attr_mod != 0)
1159 pmp->mad_hdr.status |= IB_SMP_INVALID_FIELD;
1161 /* Note that AllPortSelect is not valid */
1162 p->base_version = 1;
1163 p->class_version = 1;
1164 p->capability_mask = IB_PMA_CLASS_CAP_EXT_WIDTH;
1166 * Set the most significant bit of CM2 to indicate support for
1167 * congestion statistics
1169 p->reserved[0] = dd->psxmitwait_supported << 7;
1171 * Expected response time is 4.096 usec. * 2^18 == 1.073741824 sec.
1173 p->resp_time_value = 18;
1175 return reply((struct ib_smp *) pmp);
1178 static int pma_get_portsamplescontrol(struct ib_pma_mad *pmp,
1179 struct ib_device *ibdev, u8 port)
1181 struct ib_pma_portsamplescontrol *p =
1182 (struct ib_pma_portsamplescontrol *)pmp->data;
1183 struct qib_ibdev *dev = to_idev(ibdev);
1184 struct qib_devdata *dd = dd_from_dev(dev);
1185 struct qib_ibport *ibp = to_iport(ibdev, port);
1186 struct qib_pportdata *ppd = ppd_from_ibp(ibp);
1187 unsigned long flags;
1188 u8 port_select = p->port_select;
1190 memset(pmp->data, 0, sizeof(pmp->data));
1192 p->port_select = port_select;
1193 if (pmp->mad_hdr.attr_mod != 0 || port_select != port) {
1194 pmp->mad_hdr.status |= IB_SMP_INVALID_FIELD;
1195 goto bail;
1197 spin_lock_irqsave(&ibp->lock, flags);
1198 p->tick = dd->f_get_ib_cfg(ppd, QIB_IB_CFG_PMA_TICKS);
1199 p->sample_status = dd->f_portcntr(ppd, QIBPORTCNTR_PSSTAT);
1200 p->counter_width = 4; /* 32 bit counters */
1201 p->counter_mask0_9 = COUNTER_MASK0_9;
1202 p->sample_start = cpu_to_be32(ibp->pma_sample_start);
1203 p->sample_interval = cpu_to_be32(ibp->pma_sample_interval);
1204 p->tag = cpu_to_be16(ibp->pma_tag);
1205 p->counter_select[0] = ibp->pma_counter_select[0];
1206 p->counter_select[1] = ibp->pma_counter_select[1];
1207 p->counter_select[2] = ibp->pma_counter_select[2];
1208 p->counter_select[3] = ibp->pma_counter_select[3];
1209 p->counter_select[4] = ibp->pma_counter_select[4];
1210 spin_unlock_irqrestore(&ibp->lock, flags);
1212 bail:
1213 return reply((struct ib_smp *) pmp);
1216 static int pma_set_portsamplescontrol(struct ib_pma_mad *pmp,
1217 struct ib_device *ibdev, u8 port)
1219 struct ib_pma_portsamplescontrol *p =
1220 (struct ib_pma_portsamplescontrol *)pmp->data;
1221 struct qib_ibdev *dev = to_idev(ibdev);
1222 struct qib_devdata *dd = dd_from_dev(dev);
1223 struct qib_ibport *ibp = to_iport(ibdev, port);
1224 struct qib_pportdata *ppd = ppd_from_ibp(ibp);
1225 unsigned long flags;
1226 u8 status, xmit_flags;
1227 int ret;
1229 if (pmp->mad_hdr.attr_mod != 0 || p->port_select != port) {
1230 pmp->mad_hdr.status |= IB_SMP_INVALID_FIELD;
1231 ret = reply((struct ib_smp *) pmp);
1232 goto bail;
1235 spin_lock_irqsave(&ibp->lock, flags);
1237 /* Port Sampling code owns the PS* HW counters */
1238 xmit_flags = ppd->cong_stats.flags;
1239 ppd->cong_stats.flags = IB_PMA_CONG_HW_CONTROL_SAMPLE;
1240 status = dd->f_portcntr(ppd, QIBPORTCNTR_PSSTAT);
1241 if (status == IB_PMA_SAMPLE_STATUS_DONE ||
1242 (status == IB_PMA_SAMPLE_STATUS_RUNNING &&
1243 xmit_flags == IB_PMA_CONG_HW_CONTROL_TIMER)) {
1244 ibp->pma_sample_start = be32_to_cpu(p->sample_start);
1245 ibp->pma_sample_interval = be32_to_cpu(p->sample_interval);
1246 ibp->pma_tag = be16_to_cpu(p->tag);
1247 ibp->pma_counter_select[0] = p->counter_select[0];
1248 ibp->pma_counter_select[1] = p->counter_select[1];
1249 ibp->pma_counter_select[2] = p->counter_select[2];
1250 ibp->pma_counter_select[3] = p->counter_select[3];
1251 ibp->pma_counter_select[4] = p->counter_select[4];
1252 dd->f_set_cntr_sample(ppd, ibp->pma_sample_interval,
1253 ibp->pma_sample_start);
1255 spin_unlock_irqrestore(&ibp->lock, flags);
1257 ret = pma_get_portsamplescontrol(pmp, ibdev, port);
1259 bail:
1260 return ret;
1263 static u64 get_counter(struct qib_ibport *ibp, struct qib_pportdata *ppd,
1264 __be16 sel)
1266 u64 ret;
1268 switch (sel) {
1269 case IB_PMA_PORT_XMIT_DATA:
1270 ret = ppd->dd->f_portcntr(ppd, QIBPORTCNTR_PSXMITDATA);
1271 break;
1272 case IB_PMA_PORT_RCV_DATA:
1273 ret = ppd->dd->f_portcntr(ppd, QIBPORTCNTR_PSRCVDATA);
1274 break;
1275 case IB_PMA_PORT_XMIT_PKTS:
1276 ret = ppd->dd->f_portcntr(ppd, QIBPORTCNTR_PSXMITPKTS);
1277 break;
1278 case IB_PMA_PORT_RCV_PKTS:
1279 ret = ppd->dd->f_portcntr(ppd, QIBPORTCNTR_PSRCVPKTS);
1280 break;
1281 case IB_PMA_PORT_XMIT_WAIT:
1282 ret = ppd->dd->f_portcntr(ppd, QIBPORTCNTR_PSXMITWAIT);
1283 break;
1284 default:
1285 ret = 0;
1288 return ret;
1291 /* This function assumes that the xmit_wait lock is already held */
1292 static u64 xmit_wait_get_value_delta(struct qib_pportdata *ppd)
1294 u32 delta;
1296 delta = get_counter(&ppd->ibport_data, ppd,
1297 IB_PMA_PORT_XMIT_WAIT);
1298 return ppd->cong_stats.counter + delta;
1301 static void cache_hw_sample_counters(struct qib_pportdata *ppd)
1303 struct qib_ibport *ibp = &ppd->ibport_data;
1305 ppd->cong_stats.counter_cache.psxmitdata =
1306 get_counter(ibp, ppd, IB_PMA_PORT_XMIT_DATA);
1307 ppd->cong_stats.counter_cache.psrcvdata =
1308 get_counter(ibp, ppd, IB_PMA_PORT_RCV_DATA);
1309 ppd->cong_stats.counter_cache.psxmitpkts =
1310 get_counter(ibp, ppd, IB_PMA_PORT_XMIT_PKTS);
1311 ppd->cong_stats.counter_cache.psrcvpkts =
1312 get_counter(ibp, ppd, IB_PMA_PORT_RCV_PKTS);
1313 ppd->cong_stats.counter_cache.psxmitwait =
1314 get_counter(ibp, ppd, IB_PMA_PORT_XMIT_WAIT);
1317 static u64 get_cache_hw_sample_counters(struct qib_pportdata *ppd,
1318 __be16 sel)
1320 u64 ret;
1322 switch (sel) {
1323 case IB_PMA_PORT_XMIT_DATA:
1324 ret = ppd->cong_stats.counter_cache.psxmitdata;
1325 break;
1326 case IB_PMA_PORT_RCV_DATA:
1327 ret = ppd->cong_stats.counter_cache.psrcvdata;
1328 break;
1329 case IB_PMA_PORT_XMIT_PKTS:
1330 ret = ppd->cong_stats.counter_cache.psxmitpkts;
1331 break;
1332 case IB_PMA_PORT_RCV_PKTS:
1333 ret = ppd->cong_stats.counter_cache.psrcvpkts;
1334 break;
1335 case IB_PMA_PORT_XMIT_WAIT:
1336 ret = ppd->cong_stats.counter_cache.psxmitwait;
1337 break;
1338 default:
1339 ret = 0;
1342 return ret;
1345 static int pma_get_portsamplesresult(struct ib_pma_mad *pmp,
1346 struct ib_device *ibdev, u8 port)
1348 struct ib_pma_portsamplesresult *p =
1349 (struct ib_pma_portsamplesresult *)pmp->data;
1350 struct qib_ibdev *dev = to_idev(ibdev);
1351 struct qib_devdata *dd = dd_from_dev(dev);
1352 struct qib_ibport *ibp = to_iport(ibdev, port);
1353 struct qib_pportdata *ppd = ppd_from_ibp(ibp);
1354 unsigned long flags;
1355 u8 status;
1356 int i;
1358 memset(pmp->data, 0, sizeof(pmp->data));
1359 spin_lock_irqsave(&ibp->lock, flags);
1360 p->tag = cpu_to_be16(ibp->pma_tag);
1361 if (ppd->cong_stats.flags == IB_PMA_CONG_HW_CONTROL_TIMER)
1362 p->sample_status = IB_PMA_SAMPLE_STATUS_DONE;
1363 else {
1364 status = dd->f_portcntr(ppd, QIBPORTCNTR_PSSTAT);
1365 p->sample_status = cpu_to_be16(status);
1366 if (status == IB_PMA_SAMPLE_STATUS_DONE) {
1367 cache_hw_sample_counters(ppd);
1368 ppd->cong_stats.counter =
1369 xmit_wait_get_value_delta(ppd);
1370 dd->f_set_cntr_sample(ppd,
1371 QIB_CONG_TIMER_PSINTERVAL, 0);
1372 ppd->cong_stats.flags = IB_PMA_CONG_HW_CONTROL_TIMER;
1375 for (i = 0; i < ARRAY_SIZE(ibp->pma_counter_select); i++)
1376 p->counter[i] = cpu_to_be32(
1377 get_cache_hw_sample_counters(
1378 ppd, ibp->pma_counter_select[i]));
1379 spin_unlock_irqrestore(&ibp->lock, flags);
1381 return reply((struct ib_smp *) pmp);
1384 static int pma_get_portsamplesresult_ext(struct ib_pma_mad *pmp,
1385 struct ib_device *ibdev, u8 port)
1387 struct ib_pma_portsamplesresult_ext *p =
1388 (struct ib_pma_portsamplesresult_ext *)pmp->data;
1389 struct qib_ibdev *dev = to_idev(ibdev);
1390 struct qib_devdata *dd = dd_from_dev(dev);
1391 struct qib_ibport *ibp = to_iport(ibdev, port);
1392 struct qib_pportdata *ppd = ppd_from_ibp(ibp);
1393 unsigned long flags;
1394 u8 status;
1395 int i;
1397 /* Port Sampling code owns the PS* HW counters */
1398 memset(pmp->data, 0, sizeof(pmp->data));
1399 spin_lock_irqsave(&ibp->lock, flags);
1400 p->tag = cpu_to_be16(ibp->pma_tag);
1401 if (ppd->cong_stats.flags == IB_PMA_CONG_HW_CONTROL_TIMER)
1402 p->sample_status = IB_PMA_SAMPLE_STATUS_DONE;
1403 else {
1404 status = dd->f_portcntr(ppd, QIBPORTCNTR_PSSTAT);
1405 p->sample_status = cpu_to_be16(status);
1406 /* 64 bits */
1407 p->extended_width = cpu_to_be32(0x80000000);
1408 if (status == IB_PMA_SAMPLE_STATUS_DONE) {
1409 cache_hw_sample_counters(ppd);
1410 ppd->cong_stats.counter =
1411 xmit_wait_get_value_delta(ppd);
1412 dd->f_set_cntr_sample(ppd,
1413 QIB_CONG_TIMER_PSINTERVAL, 0);
1414 ppd->cong_stats.flags = IB_PMA_CONG_HW_CONTROL_TIMER;
1417 for (i = 0; i < ARRAY_SIZE(ibp->pma_counter_select); i++)
1418 p->counter[i] = cpu_to_be64(
1419 get_cache_hw_sample_counters(
1420 ppd, ibp->pma_counter_select[i]));
1421 spin_unlock_irqrestore(&ibp->lock, flags);
1423 return reply((struct ib_smp *) pmp);
1426 static int pma_get_portcounters(struct ib_pma_mad *pmp,
1427 struct ib_device *ibdev, u8 port)
1429 struct ib_pma_portcounters *p = (struct ib_pma_portcounters *)
1430 pmp->data;
1431 struct qib_ibport *ibp = to_iport(ibdev, port);
1432 struct qib_pportdata *ppd = ppd_from_ibp(ibp);
1433 struct qib_verbs_counters cntrs;
1434 u8 port_select = p->port_select;
1436 qib_get_counters(ppd, &cntrs);
1438 /* Adjust counters for any resets done. */
1439 cntrs.symbol_error_counter -= ibp->z_symbol_error_counter;
1440 cntrs.link_error_recovery_counter -=
1441 ibp->z_link_error_recovery_counter;
1442 cntrs.link_downed_counter -= ibp->z_link_downed_counter;
1443 cntrs.port_rcv_errors -= ibp->z_port_rcv_errors;
1444 cntrs.port_rcv_remphys_errors -= ibp->z_port_rcv_remphys_errors;
1445 cntrs.port_xmit_discards -= ibp->z_port_xmit_discards;
1446 cntrs.port_xmit_data -= ibp->z_port_xmit_data;
1447 cntrs.port_rcv_data -= ibp->z_port_rcv_data;
1448 cntrs.port_xmit_packets -= ibp->z_port_xmit_packets;
1449 cntrs.port_rcv_packets -= ibp->z_port_rcv_packets;
1450 cntrs.local_link_integrity_errors -=
1451 ibp->z_local_link_integrity_errors;
1452 cntrs.excessive_buffer_overrun_errors -=
1453 ibp->z_excessive_buffer_overrun_errors;
1454 cntrs.vl15_dropped -= ibp->z_vl15_dropped;
1455 cntrs.vl15_dropped += ibp->n_vl15_dropped;
1457 memset(pmp->data, 0, sizeof(pmp->data));
1459 p->port_select = port_select;
1460 if (pmp->mad_hdr.attr_mod != 0 || port_select != port)
1461 pmp->mad_hdr.status |= IB_SMP_INVALID_FIELD;
1463 if (cntrs.symbol_error_counter > 0xFFFFUL)
1464 p->symbol_error_counter = cpu_to_be16(0xFFFF);
1465 else
1466 p->symbol_error_counter =
1467 cpu_to_be16((u16)cntrs.symbol_error_counter);
1468 if (cntrs.link_error_recovery_counter > 0xFFUL)
1469 p->link_error_recovery_counter = 0xFF;
1470 else
1471 p->link_error_recovery_counter =
1472 (u8)cntrs.link_error_recovery_counter;
1473 if (cntrs.link_downed_counter > 0xFFUL)
1474 p->link_downed_counter = 0xFF;
1475 else
1476 p->link_downed_counter = (u8)cntrs.link_downed_counter;
1477 if (cntrs.port_rcv_errors > 0xFFFFUL)
1478 p->port_rcv_errors = cpu_to_be16(0xFFFF);
1479 else
1480 p->port_rcv_errors =
1481 cpu_to_be16((u16) cntrs.port_rcv_errors);
1482 if (cntrs.port_rcv_remphys_errors > 0xFFFFUL)
1483 p->port_rcv_remphys_errors = cpu_to_be16(0xFFFF);
1484 else
1485 p->port_rcv_remphys_errors =
1486 cpu_to_be16((u16)cntrs.port_rcv_remphys_errors);
1487 if (cntrs.port_xmit_discards > 0xFFFFUL)
1488 p->port_xmit_discards = cpu_to_be16(0xFFFF);
1489 else
1490 p->port_xmit_discards =
1491 cpu_to_be16((u16)cntrs.port_xmit_discards);
1492 if (cntrs.local_link_integrity_errors > 0xFUL)
1493 cntrs.local_link_integrity_errors = 0xFUL;
1494 if (cntrs.excessive_buffer_overrun_errors > 0xFUL)
1495 cntrs.excessive_buffer_overrun_errors = 0xFUL;
1496 p->link_overrun_errors = (cntrs.local_link_integrity_errors << 4) |
1497 cntrs.excessive_buffer_overrun_errors;
1498 if (cntrs.vl15_dropped > 0xFFFFUL)
1499 p->vl15_dropped = cpu_to_be16(0xFFFF);
1500 else
1501 p->vl15_dropped = cpu_to_be16((u16)cntrs.vl15_dropped);
1502 if (cntrs.port_xmit_data > 0xFFFFFFFFUL)
1503 p->port_xmit_data = cpu_to_be32(0xFFFFFFFF);
1504 else
1505 p->port_xmit_data = cpu_to_be32((u32)cntrs.port_xmit_data);
1506 if (cntrs.port_rcv_data > 0xFFFFFFFFUL)
1507 p->port_rcv_data = cpu_to_be32(0xFFFFFFFF);
1508 else
1509 p->port_rcv_data = cpu_to_be32((u32)cntrs.port_rcv_data);
1510 if (cntrs.port_xmit_packets > 0xFFFFFFFFUL)
1511 p->port_xmit_packets = cpu_to_be32(0xFFFFFFFF);
1512 else
1513 p->port_xmit_packets =
1514 cpu_to_be32((u32)cntrs.port_xmit_packets);
1515 if (cntrs.port_rcv_packets > 0xFFFFFFFFUL)
1516 p->port_rcv_packets = cpu_to_be32(0xFFFFFFFF);
1517 else
1518 p->port_rcv_packets =
1519 cpu_to_be32((u32) cntrs.port_rcv_packets);
1521 return reply((struct ib_smp *) pmp);
1524 static int pma_get_portcounters_cong(struct ib_pma_mad *pmp,
1525 struct ib_device *ibdev, u8 port)
1527 /* Congestion PMA packets start at offset 24 not 64 */
1528 struct ib_pma_portcounters_cong *p =
1529 (struct ib_pma_portcounters_cong *)pmp->reserved;
1530 struct qib_verbs_counters cntrs;
1531 struct qib_ibport *ibp = to_iport(ibdev, port);
1532 struct qib_pportdata *ppd = ppd_from_ibp(ibp);
1533 struct qib_devdata *dd = dd_from_ppd(ppd);
1534 u32 port_select = be32_to_cpu(pmp->mad_hdr.attr_mod) & 0xFF;
1535 u64 xmit_wait_counter;
1536 unsigned long flags;
1539 * This check is performed only in the GET method because the
1540 * SET method ends up calling this anyway.
1542 if (!dd->psxmitwait_supported)
1543 pmp->mad_hdr.status |= IB_SMP_UNSUP_METH_ATTR;
1544 if (port_select != port)
1545 pmp->mad_hdr.status |= IB_SMP_INVALID_FIELD;
1547 qib_get_counters(ppd, &cntrs);
1548 spin_lock_irqsave(&ppd->ibport_data.lock, flags);
1549 xmit_wait_counter = xmit_wait_get_value_delta(ppd);
1550 spin_unlock_irqrestore(&ppd->ibport_data.lock, flags);
1552 /* Adjust counters for any resets done. */
1553 cntrs.symbol_error_counter -= ibp->z_symbol_error_counter;
1554 cntrs.link_error_recovery_counter -=
1555 ibp->z_link_error_recovery_counter;
1556 cntrs.link_downed_counter -= ibp->z_link_downed_counter;
1557 cntrs.port_rcv_errors -= ibp->z_port_rcv_errors;
1558 cntrs.port_rcv_remphys_errors -=
1559 ibp->z_port_rcv_remphys_errors;
1560 cntrs.port_xmit_discards -= ibp->z_port_xmit_discards;
1561 cntrs.local_link_integrity_errors -=
1562 ibp->z_local_link_integrity_errors;
1563 cntrs.excessive_buffer_overrun_errors -=
1564 ibp->z_excessive_buffer_overrun_errors;
1565 cntrs.vl15_dropped -= ibp->z_vl15_dropped;
1566 cntrs.vl15_dropped += ibp->n_vl15_dropped;
1567 cntrs.port_xmit_data -= ibp->z_port_xmit_data;
1568 cntrs.port_rcv_data -= ibp->z_port_rcv_data;
1569 cntrs.port_xmit_packets -= ibp->z_port_xmit_packets;
1570 cntrs.port_rcv_packets -= ibp->z_port_rcv_packets;
1572 memset(pmp->reserved, 0, sizeof(pmp->reserved) +
1573 sizeof(pmp->data));
1576 * Set top 3 bits to indicate interval in picoseconds in
1577 * remaining bits.
1579 p->port_check_rate =
1580 cpu_to_be16((QIB_XMIT_RATE_PICO << 13) |
1581 (dd->psxmitwait_check_rate &
1582 ~(QIB_XMIT_RATE_PICO << 13)));
1583 p->port_adr_events = cpu_to_be64(0);
1584 p->port_xmit_wait = cpu_to_be64(xmit_wait_counter);
1585 p->port_xmit_data = cpu_to_be64(cntrs.port_xmit_data);
1586 p->port_rcv_data = cpu_to_be64(cntrs.port_rcv_data);
1587 p->port_xmit_packets =
1588 cpu_to_be64(cntrs.port_xmit_packets);
1589 p->port_rcv_packets =
1590 cpu_to_be64(cntrs.port_rcv_packets);
1591 if (cntrs.symbol_error_counter > 0xFFFFUL)
1592 p->symbol_error_counter = cpu_to_be16(0xFFFF);
1593 else
1594 p->symbol_error_counter =
1595 cpu_to_be16(
1596 (u16)cntrs.symbol_error_counter);
1597 if (cntrs.link_error_recovery_counter > 0xFFUL)
1598 p->link_error_recovery_counter = 0xFF;
1599 else
1600 p->link_error_recovery_counter =
1601 (u8)cntrs.link_error_recovery_counter;
1602 if (cntrs.link_downed_counter > 0xFFUL)
1603 p->link_downed_counter = 0xFF;
1604 else
1605 p->link_downed_counter =
1606 (u8)cntrs.link_downed_counter;
1607 if (cntrs.port_rcv_errors > 0xFFFFUL)
1608 p->port_rcv_errors = cpu_to_be16(0xFFFF);
1609 else
1610 p->port_rcv_errors =
1611 cpu_to_be16((u16) cntrs.port_rcv_errors);
1612 if (cntrs.port_rcv_remphys_errors > 0xFFFFUL)
1613 p->port_rcv_remphys_errors = cpu_to_be16(0xFFFF);
1614 else
1615 p->port_rcv_remphys_errors =
1616 cpu_to_be16(
1617 (u16)cntrs.port_rcv_remphys_errors);
1618 if (cntrs.port_xmit_discards > 0xFFFFUL)
1619 p->port_xmit_discards = cpu_to_be16(0xFFFF);
1620 else
1621 p->port_xmit_discards =
1622 cpu_to_be16((u16)cntrs.port_xmit_discards);
1623 if (cntrs.local_link_integrity_errors > 0xFUL)
1624 cntrs.local_link_integrity_errors = 0xFUL;
1625 if (cntrs.excessive_buffer_overrun_errors > 0xFUL)
1626 cntrs.excessive_buffer_overrun_errors = 0xFUL;
1627 p->link_overrun_errors = (cntrs.local_link_integrity_errors << 4) |
1628 cntrs.excessive_buffer_overrun_errors;
1629 if (cntrs.vl15_dropped > 0xFFFFUL)
1630 p->vl15_dropped = cpu_to_be16(0xFFFF);
1631 else
1632 p->vl15_dropped = cpu_to_be16((u16)cntrs.vl15_dropped);
1634 return reply((struct ib_smp *)pmp);
1637 static int pma_get_portcounters_ext(struct ib_pma_mad *pmp,
1638 struct ib_device *ibdev, u8 port)
1640 struct ib_pma_portcounters_ext *p =
1641 (struct ib_pma_portcounters_ext *)pmp->data;
1642 struct qib_ibport *ibp = to_iport(ibdev, port);
1643 struct qib_pportdata *ppd = ppd_from_ibp(ibp);
1644 u64 swords, rwords, spkts, rpkts, xwait;
1645 u8 port_select = p->port_select;
1647 memset(pmp->data, 0, sizeof(pmp->data));
1649 p->port_select = port_select;
1650 if (pmp->mad_hdr.attr_mod != 0 || port_select != port) {
1651 pmp->mad_hdr.status |= IB_SMP_INVALID_FIELD;
1652 goto bail;
1655 qib_snapshot_counters(ppd, &swords, &rwords, &spkts, &rpkts, &xwait);
1657 /* Adjust counters for any resets done. */
1658 swords -= ibp->z_port_xmit_data;
1659 rwords -= ibp->z_port_rcv_data;
1660 spkts -= ibp->z_port_xmit_packets;
1661 rpkts -= ibp->z_port_rcv_packets;
1663 p->port_xmit_data = cpu_to_be64(swords);
1664 p->port_rcv_data = cpu_to_be64(rwords);
1665 p->port_xmit_packets = cpu_to_be64(spkts);
1666 p->port_rcv_packets = cpu_to_be64(rpkts);
1667 p->port_unicast_xmit_packets = cpu_to_be64(ibp->n_unicast_xmit);
1668 p->port_unicast_rcv_packets = cpu_to_be64(ibp->n_unicast_rcv);
1669 p->port_multicast_xmit_packets = cpu_to_be64(ibp->n_multicast_xmit);
1670 p->port_multicast_rcv_packets = cpu_to_be64(ibp->n_multicast_rcv);
1672 bail:
1673 return reply((struct ib_smp *) pmp);
1676 static int pma_set_portcounters(struct ib_pma_mad *pmp,
1677 struct ib_device *ibdev, u8 port)
1679 struct ib_pma_portcounters *p = (struct ib_pma_portcounters *)
1680 pmp->data;
1681 struct qib_ibport *ibp = to_iport(ibdev, port);
1682 struct qib_pportdata *ppd = ppd_from_ibp(ibp);
1683 struct qib_verbs_counters cntrs;
1686 * Since the HW doesn't support clearing counters, we save the
1687 * current count and subtract it from future responses.
1689 qib_get_counters(ppd, &cntrs);
1691 if (p->counter_select & IB_PMA_SEL_SYMBOL_ERROR)
1692 ibp->z_symbol_error_counter = cntrs.symbol_error_counter;
1694 if (p->counter_select & IB_PMA_SEL_LINK_ERROR_RECOVERY)
1695 ibp->z_link_error_recovery_counter =
1696 cntrs.link_error_recovery_counter;
1698 if (p->counter_select & IB_PMA_SEL_LINK_DOWNED)
1699 ibp->z_link_downed_counter = cntrs.link_downed_counter;
1701 if (p->counter_select & IB_PMA_SEL_PORT_RCV_ERRORS)
1702 ibp->z_port_rcv_errors = cntrs.port_rcv_errors;
1704 if (p->counter_select & IB_PMA_SEL_PORT_RCV_REMPHYS_ERRORS)
1705 ibp->z_port_rcv_remphys_errors =
1706 cntrs.port_rcv_remphys_errors;
1708 if (p->counter_select & IB_PMA_SEL_PORT_XMIT_DISCARDS)
1709 ibp->z_port_xmit_discards = cntrs.port_xmit_discards;
1711 if (p->counter_select & IB_PMA_SEL_LOCAL_LINK_INTEGRITY_ERRORS)
1712 ibp->z_local_link_integrity_errors =
1713 cntrs.local_link_integrity_errors;
1715 if (p->counter_select & IB_PMA_SEL_EXCESSIVE_BUFFER_OVERRUNS)
1716 ibp->z_excessive_buffer_overrun_errors =
1717 cntrs.excessive_buffer_overrun_errors;
1719 if (p->counter_select & IB_PMA_SEL_PORT_VL15_DROPPED) {
1720 ibp->n_vl15_dropped = 0;
1721 ibp->z_vl15_dropped = cntrs.vl15_dropped;
1724 if (p->counter_select & IB_PMA_SEL_PORT_XMIT_DATA)
1725 ibp->z_port_xmit_data = cntrs.port_xmit_data;
1727 if (p->counter_select & IB_PMA_SEL_PORT_RCV_DATA)
1728 ibp->z_port_rcv_data = cntrs.port_rcv_data;
1730 if (p->counter_select & IB_PMA_SEL_PORT_XMIT_PACKETS)
1731 ibp->z_port_xmit_packets = cntrs.port_xmit_packets;
1733 if (p->counter_select & IB_PMA_SEL_PORT_RCV_PACKETS)
1734 ibp->z_port_rcv_packets = cntrs.port_rcv_packets;
1736 return pma_get_portcounters(pmp, ibdev, port);
1739 static int pma_set_portcounters_cong(struct ib_pma_mad *pmp,
1740 struct ib_device *ibdev, u8 port)
1742 struct qib_ibport *ibp = to_iport(ibdev, port);
1743 struct qib_pportdata *ppd = ppd_from_ibp(ibp);
1744 struct qib_devdata *dd = dd_from_ppd(ppd);
1745 struct qib_verbs_counters cntrs;
1746 u32 counter_select = (be32_to_cpu(pmp->mad_hdr.attr_mod) >> 24) & 0xFF;
1747 int ret = 0;
1748 unsigned long flags;
1750 qib_get_counters(ppd, &cntrs);
1751 /* Get counter values before we save them */
1752 ret = pma_get_portcounters_cong(pmp, ibdev, port);
1754 if (counter_select & IB_PMA_SEL_CONG_XMIT) {
1755 spin_lock_irqsave(&ppd->ibport_data.lock, flags);
1756 ppd->cong_stats.counter = 0;
1757 dd->f_set_cntr_sample(ppd, QIB_CONG_TIMER_PSINTERVAL,
1758 0x0);
1759 spin_unlock_irqrestore(&ppd->ibport_data.lock, flags);
1761 if (counter_select & IB_PMA_SEL_CONG_PORT_DATA) {
1762 ibp->z_port_xmit_data = cntrs.port_xmit_data;
1763 ibp->z_port_rcv_data = cntrs.port_rcv_data;
1764 ibp->z_port_xmit_packets = cntrs.port_xmit_packets;
1765 ibp->z_port_rcv_packets = cntrs.port_rcv_packets;
1767 if (counter_select & IB_PMA_SEL_CONG_ALL) {
1768 ibp->z_symbol_error_counter =
1769 cntrs.symbol_error_counter;
1770 ibp->z_link_error_recovery_counter =
1771 cntrs.link_error_recovery_counter;
1772 ibp->z_link_downed_counter =
1773 cntrs.link_downed_counter;
1774 ibp->z_port_rcv_errors = cntrs.port_rcv_errors;
1775 ibp->z_port_rcv_remphys_errors =
1776 cntrs.port_rcv_remphys_errors;
1777 ibp->z_port_xmit_discards =
1778 cntrs.port_xmit_discards;
1779 ibp->z_local_link_integrity_errors =
1780 cntrs.local_link_integrity_errors;
1781 ibp->z_excessive_buffer_overrun_errors =
1782 cntrs.excessive_buffer_overrun_errors;
1783 ibp->n_vl15_dropped = 0;
1784 ibp->z_vl15_dropped = cntrs.vl15_dropped;
1787 return ret;
1790 static int pma_set_portcounters_ext(struct ib_pma_mad *pmp,
1791 struct ib_device *ibdev, u8 port)
1793 struct ib_pma_portcounters *p = (struct ib_pma_portcounters *)
1794 pmp->data;
1795 struct qib_ibport *ibp = to_iport(ibdev, port);
1796 struct qib_pportdata *ppd = ppd_from_ibp(ibp);
1797 u64 swords, rwords, spkts, rpkts, xwait;
1799 qib_snapshot_counters(ppd, &swords, &rwords, &spkts, &rpkts, &xwait);
1801 if (p->counter_select & IB_PMA_SELX_PORT_XMIT_DATA)
1802 ibp->z_port_xmit_data = swords;
1804 if (p->counter_select & IB_PMA_SELX_PORT_RCV_DATA)
1805 ibp->z_port_rcv_data = rwords;
1807 if (p->counter_select & IB_PMA_SELX_PORT_XMIT_PACKETS)
1808 ibp->z_port_xmit_packets = spkts;
1810 if (p->counter_select & IB_PMA_SELX_PORT_RCV_PACKETS)
1811 ibp->z_port_rcv_packets = rpkts;
1813 if (p->counter_select & IB_PMA_SELX_PORT_UNI_XMIT_PACKETS)
1814 ibp->n_unicast_xmit = 0;
1816 if (p->counter_select & IB_PMA_SELX_PORT_UNI_RCV_PACKETS)
1817 ibp->n_unicast_rcv = 0;
1819 if (p->counter_select & IB_PMA_SELX_PORT_MULTI_XMIT_PACKETS)
1820 ibp->n_multicast_xmit = 0;
1822 if (p->counter_select & IB_PMA_SELX_PORT_MULTI_RCV_PACKETS)
1823 ibp->n_multicast_rcv = 0;
1825 return pma_get_portcounters_ext(pmp, ibdev, port);
1828 static int process_subn(struct ib_device *ibdev, int mad_flags,
1829 u8 port, struct ib_mad *in_mad,
1830 struct ib_mad *out_mad)
1832 struct ib_smp *smp = (struct ib_smp *)out_mad;
1833 struct qib_ibport *ibp = to_iport(ibdev, port);
1834 struct qib_pportdata *ppd = ppd_from_ibp(ibp);
1835 int ret;
1837 *out_mad = *in_mad;
1838 if (smp->class_version != 1) {
1839 smp->status |= IB_SMP_UNSUP_VERSION;
1840 ret = reply(smp);
1841 goto bail;
1844 ret = check_mkey(ibp, smp, mad_flags);
1845 if (ret) {
1846 u32 port_num = be32_to_cpu(smp->attr_mod);
1849 * If this is a get/set portinfo, we already check the
1850 * M_Key if the MAD is for another port and the M_Key
1851 * is OK on the receiving port. This check is needed
1852 * to increment the error counters when the M_Key
1853 * fails to match on *both* ports.
1855 if (in_mad->mad_hdr.attr_id == IB_SMP_ATTR_PORT_INFO &&
1856 (smp->method == IB_MGMT_METHOD_GET ||
1857 smp->method == IB_MGMT_METHOD_SET) &&
1858 port_num && port_num <= ibdev->phys_port_cnt &&
1859 port != port_num)
1860 (void) check_mkey(to_iport(ibdev, port_num), smp, 0);
1861 ret = IB_MAD_RESULT_FAILURE;
1862 goto bail;
1865 switch (smp->method) {
1866 case IB_MGMT_METHOD_GET:
1867 switch (smp->attr_id) {
1868 case IB_SMP_ATTR_NODE_DESC:
1869 ret = subn_get_nodedescription(smp, ibdev);
1870 goto bail;
1871 case IB_SMP_ATTR_NODE_INFO:
1872 ret = subn_get_nodeinfo(smp, ibdev, port);
1873 goto bail;
1874 case IB_SMP_ATTR_GUID_INFO:
1875 ret = subn_get_guidinfo(smp, ibdev, port);
1876 goto bail;
1877 case IB_SMP_ATTR_PORT_INFO:
1878 ret = subn_get_portinfo(smp, ibdev, port);
1879 goto bail;
1880 case IB_SMP_ATTR_PKEY_TABLE:
1881 ret = subn_get_pkeytable(smp, ibdev, port);
1882 goto bail;
1883 case IB_SMP_ATTR_SL_TO_VL_TABLE:
1884 ret = subn_get_sl_to_vl(smp, ibdev, port);
1885 goto bail;
1886 case IB_SMP_ATTR_VL_ARB_TABLE:
1887 ret = subn_get_vl_arb(smp, ibdev, port);
1888 goto bail;
1889 case IB_SMP_ATTR_SM_INFO:
1890 if (ibp->port_cap_flags & IB_PORT_SM_DISABLED) {
1891 ret = IB_MAD_RESULT_SUCCESS |
1892 IB_MAD_RESULT_CONSUMED;
1893 goto bail;
1895 if (ibp->port_cap_flags & IB_PORT_SM) {
1896 ret = IB_MAD_RESULT_SUCCESS;
1897 goto bail;
1899 /* FALLTHROUGH */
1900 default:
1901 smp->status |= IB_SMP_UNSUP_METH_ATTR;
1902 ret = reply(smp);
1903 goto bail;
1906 case IB_MGMT_METHOD_SET:
1907 switch (smp->attr_id) {
1908 case IB_SMP_ATTR_GUID_INFO:
1909 ret = subn_set_guidinfo(smp, ibdev, port);
1910 goto bail;
1911 case IB_SMP_ATTR_PORT_INFO:
1912 ret = subn_set_portinfo(smp, ibdev, port);
1913 goto bail;
1914 case IB_SMP_ATTR_PKEY_TABLE:
1915 ret = subn_set_pkeytable(smp, ibdev, port);
1916 goto bail;
1917 case IB_SMP_ATTR_SL_TO_VL_TABLE:
1918 ret = subn_set_sl_to_vl(smp, ibdev, port);
1919 goto bail;
1920 case IB_SMP_ATTR_VL_ARB_TABLE:
1921 ret = subn_set_vl_arb(smp, ibdev, port);
1922 goto bail;
1923 case IB_SMP_ATTR_SM_INFO:
1924 if (ibp->port_cap_flags & IB_PORT_SM_DISABLED) {
1925 ret = IB_MAD_RESULT_SUCCESS |
1926 IB_MAD_RESULT_CONSUMED;
1927 goto bail;
1929 if (ibp->port_cap_flags & IB_PORT_SM) {
1930 ret = IB_MAD_RESULT_SUCCESS;
1931 goto bail;
1933 /* FALLTHROUGH */
1934 default:
1935 smp->status |= IB_SMP_UNSUP_METH_ATTR;
1936 ret = reply(smp);
1937 goto bail;
1940 case IB_MGMT_METHOD_TRAP_REPRESS:
1941 if (smp->attr_id == IB_SMP_ATTR_NOTICE)
1942 ret = subn_trap_repress(smp, ibdev, port);
1943 else {
1944 smp->status |= IB_SMP_UNSUP_METH_ATTR;
1945 ret = reply(smp);
1947 goto bail;
1949 case IB_MGMT_METHOD_TRAP:
1950 case IB_MGMT_METHOD_REPORT:
1951 case IB_MGMT_METHOD_REPORT_RESP:
1952 case IB_MGMT_METHOD_GET_RESP:
1954 * The ib_mad module will call us to process responses
1955 * before checking for other consumers.
1956 * Just tell the caller to process it normally.
1958 ret = IB_MAD_RESULT_SUCCESS;
1959 goto bail;
1961 case IB_MGMT_METHOD_SEND:
1962 if (ib_get_smp_direction(smp) &&
1963 smp->attr_id == QIB_VENDOR_IPG) {
1964 ppd->dd->f_set_ib_cfg(ppd, QIB_IB_CFG_PORT,
1965 smp->data[0]);
1966 ret = IB_MAD_RESULT_SUCCESS | IB_MAD_RESULT_CONSUMED;
1967 } else
1968 ret = IB_MAD_RESULT_SUCCESS;
1969 goto bail;
1971 default:
1972 smp->status |= IB_SMP_UNSUP_METHOD;
1973 ret = reply(smp);
1976 bail:
1977 return ret;
1980 static int process_perf(struct ib_device *ibdev, u8 port,
1981 struct ib_mad *in_mad,
1982 struct ib_mad *out_mad)
1984 struct ib_pma_mad *pmp = (struct ib_pma_mad *)out_mad;
1985 int ret;
1987 *out_mad = *in_mad;
1988 if (pmp->mad_hdr.class_version != 1) {
1989 pmp->mad_hdr.status |= IB_SMP_UNSUP_VERSION;
1990 ret = reply((struct ib_smp *) pmp);
1991 goto bail;
1994 switch (pmp->mad_hdr.method) {
1995 case IB_MGMT_METHOD_GET:
1996 switch (pmp->mad_hdr.attr_id) {
1997 case IB_PMA_CLASS_PORT_INFO:
1998 ret = pma_get_classportinfo(pmp, ibdev);
1999 goto bail;
2000 case IB_PMA_PORT_SAMPLES_CONTROL:
2001 ret = pma_get_portsamplescontrol(pmp, ibdev, port);
2002 goto bail;
2003 case IB_PMA_PORT_SAMPLES_RESULT:
2004 ret = pma_get_portsamplesresult(pmp, ibdev, port);
2005 goto bail;
2006 case IB_PMA_PORT_SAMPLES_RESULT_EXT:
2007 ret = pma_get_portsamplesresult_ext(pmp, ibdev, port);
2008 goto bail;
2009 case IB_PMA_PORT_COUNTERS:
2010 ret = pma_get_portcounters(pmp, ibdev, port);
2011 goto bail;
2012 case IB_PMA_PORT_COUNTERS_EXT:
2013 ret = pma_get_portcounters_ext(pmp, ibdev, port);
2014 goto bail;
2015 case IB_PMA_PORT_COUNTERS_CONG:
2016 ret = pma_get_portcounters_cong(pmp, ibdev, port);
2017 goto bail;
2018 default:
2019 pmp->mad_hdr.status |= IB_SMP_UNSUP_METH_ATTR;
2020 ret = reply((struct ib_smp *) pmp);
2021 goto bail;
2024 case IB_MGMT_METHOD_SET:
2025 switch (pmp->mad_hdr.attr_id) {
2026 case IB_PMA_PORT_SAMPLES_CONTROL:
2027 ret = pma_set_portsamplescontrol(pmp, ibdev, port);
2028 goto bail;
2029 case IB_PMA_PORT_COUNTERS:
2030 ret = pma_set_portcounters(pmp, ibdev, port);
2031 goto bail;
2032 case IB_PMA_PORT_COUNTERS_EXT:
2033 ret = pma_set_portcounters_ext(pmp, ibdev, port);
2034 goto bail;
2035 case IB_PMA_PORT_COUNTERS_CONG:
2036 ret = pma_set_portcounters_cong(pmp, ibdev, port);
2037 goto bail;
2038 default:
2039 pmp->mad_hdr.status |= IB_SMP_UNSUP_METH_ATTR;
2040 ret = reply((struct ib_smp *) pmp);
2041 goto bail;
2044 case IB_MGMT_METHOD_TRAP:
2045 case IB_MGMT_METHOD_GET_RESP:
2047 * The ib_mad module will call us to process responses
2048 * before checking for other consumers.
2049 * Just tell the caller to process it normally.
2051 ret = IB_MAD_RESULT_SUCCESS;
2052 goto bail;
2054 default:
2055 pmp->mad_hdr.status |= IB_SMP_UNSUP_METHOD;
2056 ret = reply((struct ib_smp *) pmp);
2059 bail:
2060 return ret;
2063 static int cc_get_classportinfo(struct ib_cc_mad *ccp,
2064 struct ib_device *ibdev)
2066 struct ib_cc_classportinfo_attr *p =
2067 (struct ib_cc_classportinfo_attr *)ccp->mgmt_data;
2069 memset(ccp->mgmt_data, 0, sizeof(ccp->mgmt_data));
2071 p->base_version = 1;
2072 p->class_version = 1;
2073 p->cap_mask = 0;
2076 * Expected response time is 4.096 usec. * 2^18 == 1.073741824 sec.
2078 p->resp_time_value = 18;
2080 return reply((struct ib_smp *) ccp);
2083 static int cc_get_congestion_info(struct ib_cc_mad *ccp,
2084 struct ib_device *ibdev, u8 port)
2086 struct ib_cc_info_attr *p =
2087 (struct ib_cc_info_attr *)ccp->mgmt_data;
2088 struct qib_ibport *ibp = to_iport(ibdev, port);
2089 struct qib_pportdata *ppd = ppd_from_ibp(ibp);
2091 memset(ccp->mgmt_data, 0, sizeof(ccp->mgmt_data));
2093 p->congestion_info = 0;
2094 p->control_table_cap = ppd->cc_max_table_entries;
2096 return reply((struct ib_smp *) ccp);
2099 static int cc_get_congestion_setting(struct ib_cc_mad *ccp,
2100 struct ib_device *ibdev, u8 port)
2102 int i;
2103 struct ib_cc_congestion_setting_attr *p =
2104 (struct ib_cc_congestion_setting_attr *)ccp->mgmt_data;
2105 struct qib_ibport *ibp = to_iport(ibdev, port);
2106 struct qib_pportdata *ppd = ppd_from_ibp(ibp);
2107 struct ib_cc_congestion_entry_shadow *entries;
2109 memset(ccp->mgmt_data, 0, sizeof(ccp->mgmt_data));
2111 spin_lock(&ppd->cc_shadow_lock);
2113 entries = ppd->congestion_entries_shadow->entries;
2114 p->port_control = cpu_to_be16(
2115 ppd->congestion_entries_shadow->port_control);
2116 p->control_map = cpu_to_be16(
2117 ppd->congestion_entries_shadow->control_map);
2118 for (i = 0; i < IB_CC_CCS_ENTRIES; i++) {
2119 p->entries[i].ccti_increase = entries[i].ccti_increase;
2120 p->entries[i].ccti_timer = cpu_to_be16(entries[i].ccti_timer);
2121 p->entries[i].trigger_threshold = entries[i].trigger_threshold;
2122 p->entries[i].ccti_min = entries[i].ccti_min;
2125 spin_unlock(&ppd->cc_shadow_lock);
2127 return reply((struct ib_smp *) ccp);
2130 static int cc_get_congestion_control_table(struct ib_cc_mad *ccp,
2131 struct ib_device *ibdev, u8 port)
2133 struct ib_cc_table_attr *p =
2134 (struct ib_cc_table_attr *)ccp->mgmt_data;
2135 struct qib_ibport *ibp = to_iport(ibdev, port);
2136 struct qib_pportdata *ppd = ppd_from_ibp(ibp);
2137 u32 cct_block_index = be32_to_cpu(ccp->attr_mod);
2138 u32 max_cct_block;
2139 u32 cct_entry;
2140 struct ib_cc_table_entry_shadow *entries;
2141 int i;
2143 /* Is the table index more than what is supported? */
2144 if (cct_block_index > IB_CC_TABLE_CAP_DEFAULT - 1)
2145 goto bail;
2147 memset(ccp->mgmt_data, 0, sizeof(ccp->mgmt_data));
2149 spin_lock(&ppd->cc_shadow_lock);
2151 max_cct_block =
2152 (ppd->ccti_entries_shadow->ccti_last_entry + 1)/IB_CCT_ENTRIES;
2153 max_cct_block = max_cct_block ? max_cct_block - 1 : 0;
2155 if (cct_block_index > max_cct_block) {
2156 spin_unlock(&ppd->cc_shadow_lock);
2157 goto bail;
2160 ccp->attr_mod = cpu_to_be32(cct_block_index);
2162 cct_entry = IB_CCT_ENTRIES * (cct_block_index + 1);
2164 cct_entry--;
2166 p->ccti_limit = cpu_to_be16(cct_entry);
2168 entries = &ppd->ccti_entries_shadow->
2169 entries[IB_CCT_ENTRIES * cct_block_index];
2170 cct_entry %= IB_CCT_ENTRIES;
2172 for (i = 0; i <= cct_entry; i++)
2173 p->ccti_entries[i].entry = cpu_to_be16(entries[i].entry);
2175 spin_unlock(&ppd->cc_shadow_lock);
2177 return reply((struct ib_smp *) ccp);
2179 bail:
2180 return reply_failure((struct ib_smp *) ccp);
2183 static int cc_set_congestion_setting(struct ib_cc_mad *ccp,
2184 struct ib_device *ibdev, u8 port)
2186 struct ib_cc_congestion_setting_attr *p =
2187 (struct ib_cc_congestion_setting_attr *)ccp->mgmt_data;
2188 struct qib_ibport *ibp = to_iport(ibdev, port);
2189 struct qib_pportdata *ppd = ppd_from_ibp(ibp);
2190 int i;
2192 ppd->cc_sl_control_map = be16_to_cpu(p->control_map);
2194 for (i = 0; i < IB_CC_CCS_ENTRIES; i++) {
2195 ppd->congestion_entries[i].ccti_increase =
2196 p->entries[i].ccti_increase;
2198 ppd->congestion_entries[i].ccti_timer =
2199 be16_to_cpu(p->entries[i].ccti_timer);
2201 ppd->congestion_entries[i].trigger_threshold =
2202 p->entries[i].trigger_threshold;
2204 ppd->congestion_entries[i].ccti_min =
2205 p->entries[i].ccti_min;
2208 return reply((struct ib_smp *) ccp);
2211 static int cc_set_congestion_control_table(struct ib_cc_mad *ccp,
2212 struct ib_device *ibdev, u8 port)
2214 struct ib_cc_table_attr *p =
2215 (struct ib_cc_table_attr *)ccp->mgmt_data;
2216 struct qib_ibport *ibp = to_iport(ibdev, port);
2217 struct qib_pportdata *ppd = ppd_from_ibp(ibp);
2218 u32 cct_block_index = be32_to_cpu(ccp->attr_mod);
2219 u32 cct_entry;
2220 struct ib_cc_table_entry_shadow *entries;
2221 int i;
2223 /* Is the table index more than what is supported? */
2224 if (cct_block_index > IB_CC_TABLE_CAP_DEFAULT - 1)
2225 goto bail;
2227 /* If this packet is the first in the sequence then
2228 * zero the total table entry count.
2230 if (be16_to_cpu(p->ccti_limit) < IB_CCT_ENTRIES)
2231 ppd->total_cct_entry = 0;
2233 cct_entry = (be16_to_cpu(p->ccti_limit))%IB_CCT_ENTRIES;
2235 /* ccti_limit is 0 to 63 */
2236 ppd->total_cct_entry += (cct_entry + 1);
2238 if (ppd->total_cct_entry > ppd->cc_supported_table_entries)
2239 goto bail;
2241 ppd->ccti_limit = be16_to_cpu(p->ccti_limit);
2243 entries = ppd->ccti_entries + (IB_CCT_ENTRIES * cct_block_index);
2245 for (i = 0; i <= cct_entry; i++)
2246 entries[i].entry = be16_to_cpu(p->ccti_entries[i].entry);
2248 spin_lock(&ppd->cc_shadow_lock);
2250 ppd->ccti_entries_shadow->ccti_last_entry = ppd->total_cct_entry - 1;
2251 memcpy(ppd->ccti_entries_shadow->entries, ppd->ccti_entries,
2252 (ppd->total_cct_entry * sizeof(struct ib_cc_table_entry)));
2254 ppd->congestion_entries_shadow->port_control = IB_CC_CCS_PC_SL_BASED;
2255 ppd->congestion_entries_shadow->control_map = ppd->cc_sl_control_map;
2256 memcpy(ppd->congestion_entries_shadow->entries, ppd->congestion_entries,
2257 IB_CC_CCS_ENTRIES * sizeof(struct ib_cc_congestion_entry));
2259 spin_unlock(&ppd->cc_shadow_lock);
2261 return reply((struct ib_smp *) ccp);
2263 bail:
2264 return reply_failure((struct ib_smp *) ccp);
2267 static int check_cc_key(struct qib_ibport *ibp,
2268 struct ib_cc_mad *ccp, int mad_flags)
2270 return 0;
2273 static int process_cc(struct ib_device *ibdev, int mad_flags,
2274 u8 port, struct ib_mad *in_mad,
2275 struct ib_mad *out_mad)
2277 struct ib_cc_mad *ccp = (struct ib_cc_mad *)out_mad;
2278 struct qib_ibport *ibp = to_iport(ibdev, port);
2279 int ret;
2281 *out_mad = *in_mad;
2283 if (ccp->class_version != 2) {
2284 ccp->status |= IB_SMP_UNSUP_VERSION;
2285 ret = reply((struct ib_smp *)ccp);
2286 goto bail;
2289 ret = check_cc_key(ibp, ccp, mad_flags);
2290 if (ret)
2291 goto bail;
2293 switch (ccp->method) {
2294 case IB_MGMT_METHOD_GET:
2295 switch (ccp->attr_id) {
2296 case IB_CC_ATTR_CLASSPORTINFO:
2297 ret = cc_get_classportinfo(ccp, ibdev);
2298 goto bail;
2300 case IB_CC_ATTR_CONGESTION_INFO:
2301 ret = cc_get_congestion_info(ccp, ibdev, port);
2302 goto bail;
2304 case IB_CC_ATTR_CA_CONGESTION_SETTING:
2305 ret = cc_get_congestion_setting(ccp, ibdev, port);
2306 goto bail;
2308 case IB_CC_ATTR_CONGESTION_CONTROL_TABLE:
2309 ret = cc_get_congestion_control_table(ccp, ibdev, port);
2310 goto bail;
2312 /* FALLTHROUGH */
2313 default:
2314 ccp->status |= IB_SMP_UNSUP_METH_ATTR;
2315 ret = reply((struct ib_smp *) ccp);
2316 goto bail;
2319 case IB_MGMT_METHOD_SET:
2320 switch (ccp->attr_id) {
2321 case IB_CC_ATTR_CA_CONGESTION_SETTING:
2322 ret = cc_set_congestion_setting(ccp, ibdev, port);
2323 goto bail;
2325 case IB_CC_ATTR_CONGESTION_CONTROL_TABLE:
2326 ret = cc_set_congestion_control_table(ccp, ibdev, port);
2327 goto bail;
2329 /* FALLTHROUGH */
2330 default:
2331 ccp->status |= IB_SMP_UNSUP_METH_ATTR;
2332 ret = reply((struct ib_smp *) ccp);
2333 goto bail;
2336 case IB_MGMT_METHOD_GET_RESP:
2338 * The ib_mad module will call us to process responses
2339 * before checking for other consumers.
2340 * Just tell the caller to process it normally.
2342 ret = IB_MAD_RESULT_SUCCESS;
2343 goto bail;
2345 case IB_MGMT_METHOD_TRAP:
2346 default:
2347 ccp->status |= IB_SMP_UNSUP_METHOD;
2348 ret = reply((struct ib_smp *) ccp);
2351 bail:
2352 return ret;
2356 * qib_process_mad - process an incoming MAD packet
2357 * @ibdev: the infiniband device this packet came in on
2358 * @mad_flags: MAD flags
2359 * @port: the port number this packet came in on
2360 * @in_wc: the work completion entry for this packet
2361 * @in_grh: the global route header for this packet
2362 * @in_mad: the incoming MAD
2363 * @out_mad: any outgoing MAD reply
2365 * Returns IB_MAD_RESULT_SUCCESS if this is a MAD that we are not
2366 * interested in processing.
2368 * Note that the verbs framework has already done the MAD sanity checks,
2369 * and hop count/pointer updating for IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE
2370 * MADs.
2372 * This is called by the ib_mad module.
2374 int qib_process_mad(struct ib_device *ibdev, int mad_flags, u8 port,
2375 struct ib_wc *in_wc, struct ib_grh *in_grh,
2376 struct ib_mad *in_mad, struct ib_mad *out_mad)
2378 int ret;
2379 struct qib_ibport *ibp = to_iport(ibdev, port);
2380 struct qib_pportdata *ppd = ppd_from_ibp(ibp);
2382 switch (in_mad->mad_hdr.mgmt_class) {
2383 case IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE:
2384 case IB_MGMT_CLASS_SUBN_LID_ROUTED:
2385 ret = process_subn(ibdev, mad_flags, port, in_mad, out_mad);
2386 goto bail;
2388 case IB_MGMT_CLASS_PERF_MGMT:
2389 ret = process_perf(ibdev, port, in_mad, out_mad);
2390 goto bail;
2392 case IB_MGMT_CLASS_CONG_MGMT:
2393 if (!ppd->congestion_entries_shadow ||
2394 !qib_cc_table_size) {
2395 ret = IB_MAD_RESULT_SUCCESS;
2396 goto bail;
2398 ret = process_cc(ibdev, mad_flags, port, in_mad, out_mad);
2399 goto bail;
2401 default:
2402 ret = IB_MAD_RESULT_SUCCESS;
2405 bail:
2406 return ret;
2409 static void send_handler(struct ib_mad_agent *agent,
2410 struct ib_mad_send_wc *mad_send_wc)
2412 ib_free_send_mad(mad_send_wc->send_buf);
2415 static void xmit_wait_timer_func(unsigned long opaque)
2417 struct qib_pportdata *ppd = (struct qib_pportdata *)opaque;
2418 struct qib_devdata *dd = dd_from_ppd(ppd);
2419 unsigned long flags;
2420 u8 status;
2422 spin_lock_irqsave(&ppd->ibport_data.lock, flags);
2423 if (ppd->cong_stats.flags == IB_PMA_CONG_HW_CONTROL_SAMPLE) {
2424 status = dd->f_portcntr(ppd, QIBPORTCNTR_PSSTAT);
2425 if (status == IB_PMA_SAMPLE_STATUS_DONE) {
2426 /* save counter cache */
2427 cache_hw_sample_counters(ppd);
2428 ppd->cong_stats.flags = IB_PMA_CONG_HW_CONTROL_TIMER;
2429 } else
2430 goto done;
2432 ppd->cong_stats.counter = xmit_wait_get_value_delta(ppd);
2433 dd->f_set_cntr_sample(ppd, QIB_CONG_TIMER_PSINTERVAL, 0x0);
2434 done:
2435 spin_unlock_irqrestore(&ppd->ibport_data.lock, flags);
2436 mod_timer(&ppd->cong_stats.timer, jiffies + HZ);
2439 int qib_create_agents(struct qib_ibdev *dev)
2441 struct qib_devdata *dd = dd_from_dev(dev);
2442 struct ib_mad_agent *agent;
2443 struct qib_ibport *ibp;
2444 int p;
2445 int ret;
2447 for (p = 0; p < dd->num_pports; p++) {
2448 ibp = &dd->pport[p].ibport_data;
2449 agent = ib_register_mad_agent(&dev->ibdev, p + 1, IB_QPT_SMI,
2450 NULL, 0, send_handler,
2451 NULL, NULL);
2452 if (IS_ERR(agent)) {
2453 ret = PTR_ERR(agent);
2454 goto err;
2457 /* Initialize xmit_wait structure */
2458 dd->pport[p].cong_stats.counter = 0;
2459 init_timer(&dd->pport[p].cong_stats.timer);
2460 dd->pport[p].cong_stats.timer.function = xmit_wait_timer_func;
2461 dd->pport[p].cong_stats.timer.data =
2462 (unsigned long)(&dd->pport[p]);
2463 dd->pport[p].cong_stats.timer.expires = 0;
2464 add_timer(&dd->pport[p].cong_stats.timer);
2466 ibp->send_agent = agent;
2469 return 0;
2471 err:
2472 for (p = 0; p < dd->num_pports; p++) {
2473 ibp = &dd->pport[p].ibport_data;
2474 if (ibp->send_agent) {
2475 agent = ibp->send_agent;
2476 ibp->send_agent = NULL;
2477 ib_unregister_mad_agent(agent);
2481 return ret;
2484 void qib_free_agents(struct qib_ibdev *dev)
2486 struct qib_devdata *dd = dd_from_dev(dev);
2487 struct ib_mad_agent *agent;
2488 struct qib_ibport *ibp;
2489 int p;
2491 for (p = 0; p < dd->num_pports; p++) {
2492 ibp = &dd->pport[p].ibport_data;
2493 if (ibp->send_agent) {
2494 agent = ibp->send_agent;
2495 ibp->send_agent = NULL;
2496 ib_unregister_mad_agent(agent);
2498 if (ibp->sm_ah) {
2499 ib_destroy_ah(&ibp->sm_ah->ibah);
2500 ibp->sm_ah = NULL;
2502 if (dd->pport[p].cong_stats.timer.data)
2503 del_timer_sync(&dd->pport[p].cong_stats.timer);