blob: 4b9e11cc561b8d16359bcbd3a5212fa8afabb940 [file] [log] [blame]
Ralph Campbellf9315512010-05-23 21:44:54 -07001/*
2 * Copyright (c) 2006, 2007, 2008, 2009, 2010 QLogic Corporation.
3 * All rights reserved.
4 * Copyright (c) 2005, 2006 PathScale, Inc. All rights reserved.
5 *
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:
11 *
12 * Redistribution and use in source and binary forms, with or
13 * without modification, are permitted provided that the following
14 * conditions are met:
15 *
16 * - Redistributions of source code must retain the above
17 * copyright notice, this list of conditions and the following
18 * disclaimer.
19 *
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.
24 *
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.
33 */
34
35#include <rdma/ib_smi.h>
36
37#include "qib.h"
38#include "qib_mad.h"
39
40static int reply(struct ib_smp *smp)
41{
42 /*
43 * The verbs framework will handle the directed/LID route
44 * packet changes.
45 */
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;
50}
51
52static void qib_send_trap(struct qib_ibport *ibp, void *data, unsigned len)
53{
54 struct ib_mad_send_buf *send_buf;
55 struct ib_mad_agent *agent;
56 struct ib_smp *smp;
57 int ret;
58 unsigned long flags;
59 unsigned long timeout;
60
61 agent = ibp->send_agent;
62 if (!agent)
63 return;
64
65 /* o14-3.2.1 */
66 if (!(ppd_from_ibp(ibp)->lflags & QIBL_LINKACTIVE))
67 return;
68
69 /* o14-2 */
70 if (ibp->trap_timeout && time_before(jiffies, ibp->trap_timeout))
71 return;
72
73 send_buf = ib_create_send_mad(agent, 0, 0, 0, IB_MGMT_MAD_HDR,
74 IB_MGMT_MAD_DATA, GFP_ATOMIC);
75 if (IS_ERR(send_buf))
76 return;
77
78 smp = send_buf->mad;
79 smp->base_version = IB_MGMT_BASE_VERSION;
80 smp->mgmt_class = IB_MGMT_CLASS_SUBN_LID_ROUTED;
81 smp->class_version = 1;
82 smp->method = IB_MGMT_METHOD_TRAP;
83 ibp->tid++;
84 smp->tid = cpu_to_be64(ibp->tid);
85 smp->attr_id = IB_SMP_ATTR_NOTICE;
86 /* o14-1: smp->mkey = 0; */
87 memcpy(smp->data, data, len);
88
89 spin_lock_irqsave(&ibp->lock, flags);
90 if (!ibp->sm_ah) {
91 if (ibp->sm_lid != be16_to_cpu(IB_LID_PERMISSIVE)) {
92 struct ib_ah *ah;
93 struct ib_ah_attr attr;
94
95 memset(&attr, 0, sizeof attr);
96 attr.dlid = ibp->sm_lid;
97 attr.port_num = ppd_from_ibp(ibp)->port;
98 ah = ib_create_ah(ibp->qp0->ibqp.pd, &attr);
99 if (IS_ERR(ah))
100 ret = -EINVAL;
101 else {
102 send_buf->ah = ah;
103 ibp->sm_ah = to_iah(ah);
104 ret = 0;
105 }
106 } else
107 ret = -EINVAL;
108 } else {
109 send_buf->ah = &ibp->sm_ah->ibah;
110 ret = 0;
111 }
112 spin_unlock_irqrestore(&ibp->lock, flags);
113
114 if (!ret)
115 ret = ib_post_send_mad(send_buf, NULL);
116 if (!ret) {
117 /* 4.096 usec. */
118 timeout = (4096 * (1UL << ibp->subnet_timeout)) / 1000;
119 ibp->trap_timeout = jiffies + usecs_to_jiffies(timeout);
120 } else {
121 ib_free_send_mad(send_buf);
122 ibp->trap_timeout = 0;
123 }
124}
125
126/*
127 * Send a bad [PQ]_Key trap (ch. 14.3.8).
128 */
129void qib_bad_pqkey(struct qib_ibport *ibp, __be16 trap_num, u32 key, u32 sl,
130 u32 qp1, u32 qp2, __be16 lid1, __be16 lid2)
131{
132 struct ib_mad_notice_attr data;
133
134 if (trap_num == IB_NOTICE_TRAP_BAD_PKEY)
135 ibp->pkey_violations++;
136 else
137 ibp->qkey_violations++;
138 ibp->n_pkt_drops++;
139
140 /* Send violation trap */
141 data.generic_type = IB_NOTICE_TYPE_SECURITY;
142 data.prod_type_msb = 0;
143 data.prod_type_lsb = IB_NOTICE_PROD_CA;
144 data.trap_num = trap_num;
145 data.issuer_lid = cpu_to_be16(ppd_from_ibp(ibp)->lid);
146 data.toggle_count = 0;
147 memset(&data.details, 0, sizeof data.details);
148 data.details.ntc_257_258.lid1 = lid1;
149 data.details.ntc_257_258.lid2 = lid2;
150 data.details.ntc_257_258.key = cpu_to_be32(key);
151 data.details.ntc_257_258.sl_qp1 = cpu_to_be32((sl << 28) | qp1);
152 data.details.ntc_257_258.qp2 = cpu_to_be32(qp2);
153
154 qib_send_trap(ibp, &data, sizeof data);
155}
156
157/*
158 * Send a bad M_Key trap (ch. 14.3.9).
159 */
160static void qib_bad_mkey(struct qib_ibport *ibp, struct ib_smp *smp)
161{
162 struct ib_mad_notice_attr data;
163
164 /* Send violation trap */
165 data.generic_type = IB_NOTICE_TYPE_SECURITY;
166 data.prod_type_msb = 0;
167 data.prod_type_lsb = IB_NOTICE_PROD_CA;
168 data.trap_num = IB_NOTICE_TRAP_BAD_MKEY;
169 data.issuer_lid = cpu_to_be16(ppd_from_ibp(ibp)->lid);
170 data.toggle_count = 0;
171 memset(&data.details, 0, sizeof data.details);
172 data.details.ntc_256.lid = data.issuer_lid;
173 data.details.ntc_256.method = smp->method;
174 data.details.ntc_256.attr_id = smp->attr_id;
175 data.details.ntc_256.attr_mod = smp->attr_mod;
176 data.details.ntc_256.mkey = smp->mkey;
177 if (smp->mgmt_class == IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE) {
178 u8 hop_cnt;
179
180 data.details.ntc_256.dr_slid = smp->dr_slid;
181 data.details.ntc_256.dr_trunc_hop = IB_NOTICE_TRAP_DR_NOTICE;
182 hop_cnt = smp->hop_cnt;
183 if (hop_cnt > ARRAY_SIZE(data.details.ntc_256.dr_rtn_path)) {
184 data.details.ntc_256.dr_trunc_hop |=
185 IB_NOTICE_TRAP_DR_TRUNC;
186 hop_cnt = ARRAY_SIZE(data.details.ntc_256.dr_rtn_path);
187 }
188 data.details.ntc_256.dr_trunc_hop |= hop_cnt;
189 memcpy(data.details.ntc_256.dr_rtn_path, smp->return_path,
190 hop_cnt);
191 }
192
193 qib_send_trap(ibp, &data, sizeof data);
194}
195
196/*
197 * Send a Port Capability Mask Changed trap (ch. 14.3.11).
198 */
199void qib_cap_mask_chg(struct qib_ibport *ibp)
200{
201 struct ib_mad_notice_attr data;
202
203 data.generic_type = IB_NOTICE_TYPE_INFO;
204 data.prod_type_msb = 0;
205 data.prod_type_lsb = IB_NOTICE_PROD_CA;
206 data.trap_num = IB_NOTICE_TRAP_CAP_MASK_CHG;
207 data.issuer_lid = cpu_to_be16(ppd_from_ibp(ibp)->lid);
208 data.toggle_count = 0;
209 memset(&data.details, 0, sizeof data.details);
210 data.details.ntc_144.lid = data.issuer_lid;
211 data.details.ntc_144.new_cap_mask = cpu_to_be32(ibp->port_cap_flags);
212
213 qib_send_trap(ibp, &data, sizeof data);
214}
215
216/*
217 * Send a System Image GUID Changed trap (ch. 14.3.12).
218 */
219void qib_sys_guid_chg(struct qib_ibport *ibp)
220{
221 struct ib_mad_notice_attr data;
222
223 data.generic_type = IB_NOTICE_TYPE_INFO;
224 data.prod_type_msb = 0;
225 data.prod_type_lsb = IB_NOTICE_PROD_CA;
226 data.trap_num = IB_NOTICE_TRAP_SYS_GUID_CHG;
227 data.issuer_lid = cpu_to_be16(ppd_from_ibp(ibp)->lid);
228 data.toggle_count = 0;
229 memset(&data.details, 0, sizeof data.details);
230 data.details.ntc_145.lid = data.issuer_lid;
231 data.details.ntc_145.new_sys_guid = ib_qib_sys_image_guid;
232
233 qib_send_trap(ibp, &data, sizeof data);
234}
235
236/*
237 * Send a Node Description Changed trap (ch. 14.3.13).
238 */
239void qib_node_desc_chg(struct qib_ibport *ibp)
240{
241 struct ib_mad_notice_attr data;
242
243 data.generic_type = IB_NOTICE_TYPE_INFO;
244 data.prod_type_msb = 0;
245 data.prod_type_lsb = IB_NOTICE_PROD_CA;
246 data.trap_num = IB_NOTICE_TRAP_CAP_MASK_CHG;
247 data.issuer_lid = cpu_to_be16(ppd_from_ibp(ibp)->lid);
248 data.toggle_count = 0;
249 memset(&data.details, 0, sizeof data.details);
250 data.details.ntc_144.lid = data.issuer_lid;
251 data.details.ntc_144.local_changes = 1;
252 data.details.ntc_144.change_flags = IB_NOTICE_TRAP_NODE_DESC_CHG;
253
254 qib_send_trap(ibp, &data, sizeof data);
255}
256
257static int subn_get_nodedescription(struct ib_smp *smp,
258 struct ib_device *ibdev)
259{
260 if (smp->attr_mod)
261 smp->status |= IB_SMP_INVALID_FIELD;
262
263 memcpy(smp->data, ibdev->node_desc, sizeof(smp->data));
264
265 return reply(smp);
266}
267
268static int subn_get_nodeinfo(struct ib_smp *smp, struct ib_device *ibdev,
269 u8 port)
270{
271 struct ib_node_info *nip = (struct ib_node_info *)&smp->data;
272 struct qib_devdata *dd = dd_from_ibdev(ibdev);
273 u32 vendor, majrev, minrev;
274 unsigned pidx = port - 1; /* IB number port from 1, hdw from 0 */
275
276 /* GUID 0 is illegal */
277 if (smp->attr_mod || pidx >= dd->num_pports ||
278 dd->pport[pidx].guid == 0)
279 smp->status |= IB_SMP_INVALID_FIELD;
280 else
281 nip->port_guid = dd->pport[pidx].guid;
282
283 nip->base_version = 1;
284 nip->class_version = 1;
285 nip->node_type = 1; /* channel adapter */
286 nip->num_ports = ibdev->phys_port_cnt;
287 /* This is already in network order */
288 nip->sys_guid = ib_qib_sys_image_guid;
289 nip->node_guid = dd->pport->guid; /* Use first-port GUID as node */
290 nip->partition_cap = cpu_to_be16(qib_get_npkeys(dd));
291 nip->device_id = cpu_to_be16(dd->deviceid);
292 majrev = dd->majrev;
293 minrev = dd->minrev;
294 nip->revision = cpu_to_be32((majrev << 16) | minrev);
295 nip->local_port_num = port;
296 vendor = dd->vendorid;
297 nip->vendor_id[0] = QIB_SRC_OUI_1;
298 nip->vendor_id[1] = QIB_SRC_OUI_2;
299 nip->vendor_id[2] = QIB_SRC_OUI_3;
300
301 return reply(smp);
302}
303
304static int subn_get_guidinfo(struct ib_smp *smp, struct ib_device *ibdev,
305 u8 port)
306{
307 struct qib_devdata *dd = dd_from_ibdev(ibdev);
308 u32 startgx = 8 * be32_to_cpu(smp->attr_mod);
309 __be64 *p = (__be64 *) smp->data;
310 unsigned pidx = port - 1; /* IB number port from 1, hdw from 0 */
311
312 /* 32 blocks of 8 64-bit GUIDs per block */
313
314 memset(smp->data, 0, sizeof(smp->data));
315
316 if (startgx == 0 && pidx < dd->num_pports) {
317 struct qib_pportdata *ppd = dd->pport + pidx;
318 struct qib_ibport *ibp = &ppd->ibport_data;
319 __be64 g = ppd->guid;
320 unsigned i;
321
322 /* GUID 0 is illegal */
323 if (g == 0)
324 smp->status |= IB_SMP_INVALID_FIELD;
325 else {
326 /* The first is a copy of the read-only HW GUID. */
327 p[0] = g;
328 for (i = 1; i < QIB_GUIDS_PER_PORT; i++)
329 p[i] = ibp->guids[i - 1];
330 }
331 } else
332 smp->status |= IB_SMP_INVALID_FIELD;
333
334 return reply(smp);
335}
336
337static void set_link_width_enabled(struct qib_pportdata *ppd, u32 w)
338{
339 (void) ppd->dd->f_set_ib_cfg(ppd, QIB_IB_CFG_LWID_ENB, w);
340}
341
342static void set_link_speed_enabled(struct qib_pportdata *ppd, u32 s)
343{
344 (void) ppd->dd->f_set_ib_cfg(ppd, QIB_IB_CFG_SPD_ENB, s);
345}
346
347static int get_overrunthreshold(struct qib_pportdata *ppd)
348{
349 return ppd->dd->f_get_ib_cfg(ppd, QIB_IB_CFG_OVERRUN_THRESH);
350}
351
352/**
353 * set_overrunthreshold - set the overrun threshold
354 * @ppd: the physical port data
355 * @n: the new threshold
356 *
357 * Note that this will only take effect when the link state changes.
358 */
359static int set_overrunthreshold(struct qib_pportdata *ppd, unsigned n)
360{
361 (void) ppd->dd->f_set_ib_cfg(ppd, QIB_IB_CFG_OVERRUN_THRESH,
362 (u32)n);
363 return 0;
364}
365
366static int get_phyerrthreshold(struct qib_pportdata *ppd)
367{
368 return ppd->dd->f_get_ib_cfg(ppd, QIB_IB_CFG_PHYERR_THRESH);
369}
370
371/**
372 * set_phyerrthreshold - set the physical error threshold
373 * @ppd: the physical port data
374 * @n: the new threshold
375 *
376 * Note that this will only take effect when the link state changes.
377 */
378static int set_phyerrthreshold(struct qib_pportdata *ppd, unsigned n)
379{
380 (void) ppd->dd->f_set_ib_cfg(ppd, QIB_IB_CFG_PHYERR_THRESH,
381 (u32)n);
382 return 0;
383}
384
385/**
386 * get_linkdowndefaultstate - get the default linkdown state
387 * @ppd: the physical port data
388 *
389 * Returns zero if the default is POLL, 1 if the default is SLEEP.
390 */
391static int get_linkdowndefaultstate(struct qib_pportdata *ppd)
392{
393 return ppd->dd->f_get_ib_cfg(ppd, QIB_IB_CFG_LINKDEFAULT) ==
394 IB_LINKINITCMD_SLEEP;
395}
396
397static int check_mkey(struct qib_ibport *ibp, struct ib_smp *smp, int mad_flags)
398{
399 int ret = 0;
400
401 /* Is the mkey in the process of expiring? */
402 if (ibp->mkey_lease_timeout &&
403 time_after_eq(jiffies, ibp->mkey_lease_timeout)) {
404 /* Clear timeout and mkey protection field. */
405 ibp->mkey_lease_timeout = 0;
406 ibp->mkeyprot = 0;
407 }
408
409 /* M_Key checking depends on Portinfo:M_Key_protect_bits */
410 if ((mad_flags & IB_MAD_IGNORE_MKEY) == 0 && ibp->mkey != 0 &&
411 ibp->mkey != smp->mkey &&
412 (smp->method == IB_MGMT_METHOD_SET ||
413 smp->method == IB_MGMT_METHOD_TRAP_REPRESS ||
414 (smp->method == IB_MGMT_METHOD_GET && ibp->mkeyprot >= 2))) {
415 if (ibp->mkey_violations != 0xFFFF)
416 ++ibp->mkey_violations;
417 if (!ibp->mkey_lease_timeout && ibp->mkey_lease_period)
418 ibp->mkey_lease_timeout = jiffies +
419 ibp->mkey_lease_period * HZ;
420 /* Generate a trap notice. */
421 qib_bad_mkey(ibp, smp);
422 ret = IB_MAD_RESULT_SUCCESS | IB_MAD_RESULT_CONSUMED;
423 } else if (ibp->mkey_lease_timeout)
424 ibp->mkey_lease_timeout = 0;
425
426 return ret;
427}
428
429static int subn_get_portinfo(struct ib_smp *smp, struct ib_device *ibdev,
430 u8 port)
431{
432 struct qib_devdata *dd;
433 struct qib_pportdata *ppd;
434 struct qib_ibport *ibp;
435 struct ib_port_info *pip = (struct ib_port_info *)smp->data;
436 u16 lid;
437 u8 mtu;
438 int ret;
439 u32 state;
440 u32 port_num = be32_to_cpu(smp->attr_mod);
441
442 if (port_num == 0)
443 port_num = port;
444 else {
445 if (port_num > ibdev->phys_port_cnt) {
446 smp->status |= IB_SMP_INVALID_FIELD;
447 ret = reply(smp);
448 goto bail;
449 }
450 if (port_num != port) {
451 ibp = to_iport(ibdev, port_num);
452 ret = check_mkey(ibp, smp, 0);
453 if (ret)
454 goto bail;
455 }
456 }
457
458 dd = dd_from_ibdev(ibdev);
459 /* IB numbers ports from 1, hdw from 0 */
460 ppd = dd->pport + (port_num - 1);
461 ibp = &ppd->ibport_data;
462
463 /* Clear all fields. Only set the non-zero fields. */
464 memset(smp->data, 0, sizeof(smp->data));
465
466 /* Only return the mkey if the protection field allows it. */
467 if (smp->method == IB_MGMT_METHOD_SET || ibp->mkey == smp->mkey ||
468 ibp->mkeyprot == 0)
469 pip->mkey = ibp->mkey;
470 pip->gid_prefix = ibp->gid_prefix;
471 lid = ppd->lid;
472 pip->lid = lid ? cpu_to_be16(lid) : IB_LID_PERMISSIVE;
473 pip->sm_lid = cpu_to_be16(ibp->sm_lid);
474 pip->cap_mask = cpu_to_be32(ibp->port_cap_flags);
475 /* pip->diag_code; */
476 pip->mkey_lease_period = cpu_to_be16(ibp->mkey_lease_period);
477 pip->local_port_num = port;
478 pip->link_width_enabled = ppd->link_width_enabled;
479 pip->link_width_supported = ppd->link_width_supported;
480 pip->link_width_active = ppd->link_width_active;
481 state = dd->f_iblink_state(ppd->lastibcstat);
482 pip->linkspeed_portstate = ppd->link_speed_supported << 4 | state;
483
484 pip->portphysstate_linkdown =
485 (dd->f_ibphys_portstate(ppd->lastibcstat) << 4) |
486 (get_linkdowndefaultstate(ppd) ? 1 : 2);
487 pip->mkeyprot_resv_lmc = (ibp->mkeyprot << 6) | ppd->lmc;
488 pip->linkspeedactive_enabled = (ppd->link_speed_active << 4) |
489 ppd->link_speed_enabled;
490 switch (ppd->ibmtu) {
491 default: /* something is wrong; fall through */
492 case 4096:
493 mtu = IB_MTU_4096;
494 break;
495 case 2048:
496 mtu = IB_MTU_2048;
497 break;
498 case 1024:
499 mtu = IB_MTU_1024;
500 break;
501 case 512:
502 mtu = IB_MTU_512;
503 break;
504 case 256:
505 mtu = IB_MTU_256;
506 break;
507 }
508 pip->neighbormtu_mastersmsl = (mtu << 4) | ibp->sm_sl;
509 pip->vlcap_inittype = ppd->vls_supported << 4; /* InitType = 0 */
510 pip->vl_high_limit = ibp->vl_high_limit;
511 pip->vl_arb_high_cap =
512 dd->f_get_ib_cfg(ppd, QIB_IB_CFG_VL_HIGH_CAP);
513 pip->vl_arb_low_cap =
514 dd->f_get_ib_cfg(ppd, QIB_IB_CFG_VL_LOW_CAP);
515 /* InitTypeReply = 0 */
516 pip->inittypereply_mtucap = qib_ibmtu ? qib_ibmtu : IB_MTU_4096;
517 /* HCAs ignore VLStallCount and HOQLife */
518 /* pip->vlstallcnt_hoqlife; */
519 pip->operationalvl_pei_peo_fpi_fpo =
520 dd->f_get_ib_cfg(ppd, QIB_IB_CFG_OP_VLS) << 4;
521 pip->mkey_violations = cpu_to_be16(ibp->mkey_violations);
522 /* P_KeyViolations are counted by hardware. */
523 pip->pkey_violations = cpu_to_be16(ibp->pkey_violations);
524 pip->qkey_violations = cpu_to_be16(ibp->qkey_violations);
525 /* Only the hardware GUID is supported for now */
526 pip->guid_cap = QIB_GUIDS_PER_PORT;
527 pip->clientrereg_resv_subnetto = ibp->subnet_timeout;
528 /* 32.768 usec. response time (guessing) */
529 pip->resv_resptimevalue = 3;
530 pip->localphyerrors_overrunerrors =
531 (get_phyerrthreshold(ppd) << 4) |
532 get_overrunthreshold(ppd);
533 /* pip->max_credit_hint; */
534 if (ibp->port_cap_flags & IB_PORT_LINK_LATENCY_SUP) {
535 u32 v;
536
537 v = dd->f_get_ib_cfg(ppd, QIB_IB_CFG_LINKLATENCY);
538 pip->link_roundtrip_latency[0] = v >> 16;
539 pip->link_roundtrip_latency[1] = v >> 8;
540 pip->link_roundtrip_latency[2] = v;
541 }
542
543 ret = reply(smp);
544
545bail:
546 return ret;
547}
548
549/**
550 * get_pkeys - return the PKEY table
551 * @dd: the qlogic_ib device
552 * @port: the IB port number
553 * @pkeys: the pkey table is placed here
554 */
555static int get_pkeys(struct qib_devdata *dd, u8 port, u16 *pkeys)
556{
557 struct qib_pportdata *ppd = dd->pport + port - 1;
558 /*
559 * always a kernel context, no locking needed.
560 * If we get here with ppd setup, no need to check
561 * that pd is valid.
562 */
563 struct qib_ctxtdata *rcd = dd->rcd[ppd->hw_pidx];
564
565 memcpy(pkeys, rcd->pkeys, sizeof(rcd->pkeys));
566
567 return 0;
568}
569
570static int subn_get_pkeytable(struct ib_smp *smp, struct ib_device *ibdev,
571 u8 port)
572{
573 u32 startpx = 32 * (be32_to_cpu(smp->attr_mod) & 0xffff);
574 u16 *p = (u16 *) smp->data;
575 __be16 *q = (__be16 *) smp->data;
576
577 /* 64 blocks of 32 16-bit P_Key entries */
578
579 memset(smp->data, 0, sizeof(smp->data));
580 if (startpx == 0) {
581 struct qib_devdata *dd = dd_from_ibdev(ibdev);
582 unsigned i, n = qib_get_npkeys(dd);
583
584 get_pkeys(dd, port, p);
585
586 for (i = 0; i < n; i++)
587 q[i] = cpu_to_be16(p[i]);
588 } else
589 smp->status |= IB_SMP_INVALID_FIELD;
590
591 return reply(smp);
592}
593
594static int subn_set_guidinfo(struct ib_smp *smp, struct ib_device *ibdev,
595 u8 port)
596{
597 struct qib_devdata *dd = dd_from_ibdev(ibdev);
598 u32 startgx = 8 * be32_to_cpu(smp->attr_mod);
599 __be64 *p = (__be64 *) smp->data;
600 unsigned pidx = port - 1; /* IB number port from 1, hdw from 0 */
601
602 /* 32 blocks of 8 64-bit GUIDs per block */
603
604 if (startgx == 0 && pidx < dd->num_pports) {
605 struct qib_pportdata *ppd = dd->pport + pidx;
606 struct qib_ibport *ibp = &ppd->ibport_data;
607 unsigned i;
608
609 /* The first entry is read-only. */
610 for (i = 1; i < QIB_GUIDS_PER_PORT; i++)
611 ibp->guids[i - 1] = p[i];
612 } else
613 smp->status |= IB_SMP_INVALID_FIELD;
614
615 /* The only GUID we support is the first read-only entry. */
616 return subn_get_guidinfo(smp, ibdev, port);
617}
618
619/**
620 * subn_set_portinfo - set port information
621 * @smp: the incoming SM packet
622 * @ibdev: the infiniband device
623 * @port: the port on the device
624 *
625 * Set Portinfo (see ch. 14.2.5.6).
626 */
627static int subn_set_portinfo(struct ib_smp *smp, struct ib_device *ibdev,
628 u8 port)
629{
630 struct ib_port_info *pip = (struct ib_port_info *)smp->data;
631 struct ib_event event;
632 struct qib_devdata *dd;
633 struct qib_pportdata *ppd;
634 struct qib_ibport *ibp;
635 char clientrereg = 0;
636 unsigned long flags;
637 u16 lid, smlid;
638 u8 lwe;
639 u8 lse;
640 u8 state;
641 u8 vls;
642 u8 msl;
643 u16 lstate;
644 int ret, ore, mtu;
645 u32 port_num = be32_to_cpu(smp->attr_mod);
646
647 if (port_num == 0)
648 port_num = port;
649 else {
650 if (port_num > ibdev->phys_port_cnt)
651 goto err;
652 /* Port attributes can only be set on the receiving port */
653 if (port_num != port)
654 goto get_only;
655 }
656
657 dd = dd_from_ibdev(ibdev);
658 /* IB numbers ports from 1, hdw from 0 */
659 ppd = dd->pport + (port_num - 1);
660 ibp = &ppd->ibport_data;
661 event.device = ibdev;
662 event.element.port_num = port;
663
664 ibp->mkey = pip->mkey;
665 ibp->gid_prefix = pip->gid_prefix;
666 ibp->mkey_lease_period = be16_to_cpu(pip->mkey_lease_period);
667
668 lid = be16_to_cpu(pip->lid);
669 /* Must be a valid unicast LID address. */
670 if (lid == 0 || lid >= QIB_MULTICAST_LID_BASE)
Mike Marciniszyn3c9e5f42011-01-10 17:42:19 -0800671 smp->status |= IB_SMP_INVALID_FIELD;
672 else if (ppd->lid != lid || ppd->lmc != (pip->mkeyprot_resv_lmc & 7)) {
Ralph Campbellf9315512010-05-23 21:44:54 -0700673 if (ppd->lid != lid)
674 qib_set_uevent_bits(ppd, _QIB_EVENT_LID_CHANGE_BIT);
675 if (ppd->lmc != (pip->mkeyprot_resv_lmc & 7))
676 qib_set_uevent_bits(ppd, _QIB_EVENT_LMC_CHANGE_BIT);
677 qib_set_lid(ppd, lid, pip->mkeyprot_resv_lmc & 7);
678 event.event = IB_EVENT_LID_CHANGE;
679 ib_dispatch_event(&event);
680 }
681
682 smlid = be16_to_cpu(pip->sm_lid);
683 msl = pip->neighbormtu_mastersmsl & 0xF;
684 /* Must be a valid unicast LID address. */
685 if (smlid == 0 || smlid >= QIB_MULTICAST_LID_BASE)
Mike Marciniszyn3c9e5f42011-01-10 17:42:19 -0800686 smp->status |= IB_SMP_INVALID_FIELD;
687 else if (smlid != ibp->sm_lid || msl != ibp->sm_sl) {
Ralph Campbellf9315512010-05-23 21:44:54 -0700688 spin_lock_irqsave(&ibp->lock, flags);
689 if (ibp->sm_ah) {
690 if (smlid != ibp->sm_lid)
691 ibp->sm_ah->attr.dlid = smlid;
692 if (msl != ibp->sm_sl)
693 ibp->sm_ah->attr.sl = msl;
694 }
695 spin_unlock_irqrestore(&ibp->lock, flags);
696 if (smlid != ibp->sm_lid)
697 ibp->sm_lid = smlid;
698 if (msl != ibp->sm_sl)
699 ibp->sm_sl = msl;
700 event.event = IB_EVENT_SM_CHANGE;
701 ib_dispatch_event(&event);
702 }
703
704 /* Allow 1x or 4x to be set (see 14.2.6.6). */
705 lwe = pip->link_width_enabled;
706 if (lwe) {
707 if (lwe == 0xFF)
Mitko Haralanovcc7fb052011-02-22 16:56:37 -0800708 set_link_width_enabled(ppd, ppd->link_width_supported);
Ralph Campbellf9315512010-05-23 21:44:54 -0700709 else if (lwe >= 16 || (lwe & ~ppd->link_width_supported))
Mike Marciniszyn3c9e5f42011-01-10 17:42:19 -0800710 smp->status |= IB_SMP_INVALID_FIELD;
711 else if (lwe != ppd->link_width_enabled)
712 set_link_width_enabled(ppd, lwe);
Ralph Campbellf9315512010-05-23 21:44:54 -0700713 }
714
715 lse = pip->linkspeedactive_enabled & 0xF;
716 if (lse) {
717 /*
718 * The IB 1.2 spec. only allows link speed values
719 * 1, 3, 5, 7, 15. 1.2.1 extended to allow specific
720 * speeds.
721 */
722 if (lse == 15)
Mitko Haralanovcc7fb052011-02-22 16:56:37 -0800723 set_link_speed_enabled(ppd,
724 ppd->link_speed_supported);
Ralph Campbellf9315512010-05-23 21:44:54 -0700725 else if (lse >= 8 || (lse & ~ppd->link_speed_supported))
Mike Marciniszyn3c9e5f42011-01-10 17:42:19 -0800726 smp->status |= IB_SMP_INVALID_FIELD;
727 else if (lse != ppd->link_speed_enabled)
728 set_link_speed_enabled(ppd, lse);
Ralph Campbellf9315512010-05-23 21:44:54 -0700729 }
730
731 /* Set link down default state. */
732 switch (pip->portphysstate_linkdown & 0xF) {
733 case 0: /* NOP */
734 break;
735 case 1: /* SLEEP */
736 (void) dd->f_set_ib_cfg(ppd, QIB_IB_CFG_LINKDEFAULT,
737 IB_LINKINITCMD_SLEEP);
738 break;
739 case 2: /* POLL */
740 (void) dd->f_set_ib_cfg(ppd, QIB_IB_CFG_LINKDEFAULT,
741 IB_LINKINITCMD_POLL);
742 break;
743 default:
Mike Marciniszyn3c9e5f42011-01-10 17:42:19 -0800744 smp->status |= IB_SMP_INVALID_FIELD;
Ralph Campbellf9315512010-05-23 21:44:54 -0700745 }
746
747 ibp->mkeyprot = pip->mkeyprot_resv_lmc >> 6;
748 ibp->vl_high_limit = pip->vl_high_limit;
749 (void) dd->f_set_ib_cfg(ppd, QIB_IB_CFG_VL_HIGH_LIMIT,
750 ibp->vl_high_limit);
751
752 mtu = ib_mtu_enum_to_int((pip->neighbormtu_mastersmsl >> 4) & 0xF);
753 if (mtu == -1)
Mike Marciniszyn3c9e5f42011-01-10 17:42:19 -0800754 smp->status |= IB_SMP_INVALID_FIELD;
755 else
756 qib_set_mtu(ppd, mtu);
Ralph Campbellf9315512010-05-23 21:44:54 -0700757
758 /* Set operational VLs */
759 vls = (pip->operationalvl_pei_peo_fpi_fpo >> 4) & 0xF;
760 if (vls) {
761 if (vls > ppd->vls_supported)
Mike Marciniszyn3c9e5f42011-01-10 17:42:19 -0800762 smp->status |= IB_SMP_INVALID_FIELD;
763 else
764 (void) dd->f_set_ib_cfg(ppd, QIB_IB_CFG_OP_VLS, vls);
Ralph Campbellf9315512010-05-23 21:44:54 -0700765 }
766
767 if (pip->mkey_violations == 0)
768 ibp->mkey_violations = 0;
769
770 if (pip->pkey_violations == 0)
771 ibp->pkey_violations = 0;
772
773 if (pip->qkey_violations == 0)
774 ibp->qkey_violations = 0;
775
776 ore = pip->localphyerrors_overrunerrors;
777 if (set_phyerrthreshold(ppd, (ore >> 4) & 0xF))
Mike Marciniszyn3c9e5f42011-01-10 17:42:19 -0800778 smp->status |= IB_SMP_INVALID_FIELD;
Ralph Campbellf9315512010-05-23 21:44:54 -0700779
780 if (set_overrunthreshold(ppd, (ore & 0xF)))
Mike Marciniszyn3c9e5f42011-01-10 17:42:19 -0800781 smp->status |= IB_SMP_INVALID_FIELD;
Ralph Campbellf9315512010-05-23 21:44:54 -0700782
783 ibp->subnet_timeout = pip->clientrereg_resv_subnetto & 0x1F;
784
785 if (pip->clientrereg_resv_subnetto & 0x80) {
786 clientrereg = 1;
787 event.event = IB_EVENT_CLIENT_REREGISTER;
788 ib_dispatch_event(&event);
789 }
790
791 /*
792 * Do the port state change now that the other link parameters
793 * have been set.
794 * Changing the port physical state only makes sense if the link
795 * is down or is being set to down.
796 */
797 state = pip->linkspeed_portstate & 0xF;
798 lstate = (pip->portphysstate_linkdown >> 4) & 0xF;
799 if (lstate && !(state == IB_PORT_DOWN || state == IB_PORT_NOP))
Mike Marciniszyn3c9e5f42011-01-10 17:42:19 -0800800 smp->status |= IB_SMP_INVALID_FIELD;
Ralph Campbellf9315512010-05-23 21:44:54 -0700801
802 /*
803 * Only state changes of DOWN, ARM, and ACTIVE are valid
804 * and must be in the correct state to take effect (see 7.2.6).
805 */
806 switch (state) {
807 case IB_PORT_NOP:
808 if (lstate == 0)
809 break;
810 /* FALLTHROUGH */
811 case IB_PORT_DOWN:
812 if (lstate == 0)
813 lstate = QIB_IB_LINKDOWN_ONLY;
814 else if (lstate == 1)
815 lstate = QIB_IB_LINKDOWN_SLEEP;
816 else if (lstate == 2)
817 lstate = QIB_IB_LINKDOWN;
818 else if (lstate == 3)
819 lstate = QIB_IB_LINKDOWN_DISABLE;
Mike Marciniszyn3c9e5f42011-01-10 17:42:19 -0800820 else {
821 smp->status |= IB_SMP_INVALID_FIELD;
822 break;
823 }
Ralph Campbellf9315512010-05-23 21:44:54 -0700824 spin_lock_irqsave(&ppd->lflags_lock, flags);
825 ppd->lflags &= ~QIBL_LINKV;
826 spin_unlock_irqrestore(&ppd->lflags_lock, flags);
827 qib_set_linkstate(ppd, lstate);
828 /*
829 * Don't send a reply if the response would be sent
830 * through the disabled port.
831 */
832 if (lstate == QIB_IB_LINKDOWN_DISABLE && smp->hop_cnt) {
833 ret = IB_MAD_RESULT_SUCCESS | IB_MAD_RESULT_CONSUMED;
834 goto done;
835 }
836 qib_wait_linkstate(ppd, QIBL_LINKV, 10);
837 break;
838 case IB_PORT_ARMED:
839 qib_set_linkstate(ppd, QIB_IB_LINKARM);
840 break;
841 case IB_PORT_ACTIVE:
842 qib_set_linkstate(ppd, QIB_IB_LINKACTIVE);
843 break;
844 default:
Mike Marciniszyn3c9e5f42011-01-10 17:42:19 -0800845 smp->status |= IB_SMP_INVALID_FIELD;
Ralph Campbellf9315512010-05-23 21:44:54 -0700846 }
847
848 ret = subn_get_portinfo(smp, ibdev, port);
849
850 if (clientrereg)
851 pip->clientrereg_resv_subnetto |= 0x80;
852
Mitko Haralanovcc7fb052011-02-22 16:56:37 -0800853 goto get_only;
Ralph Campbellf9315512010-05-23 21:44:54 -0700854
855err:
856 smp->status |= IB_SMP_INVALID_FIELD;
857get_only:
858 ret = subn_get_portinfo(smp, ibdev, port);
859done:
860 return ret;
861}
862
863/**
864 * rm_pkey - decrecment the reference count for the given PKEY
865 * @dd: the qlogic_ib device
866 * @key: the PKEY index
867 *
868 * Return true if this was the last reference and the hardware table entry
869 * needs to be changed.
870 */
871static int rm_pkey(struct qib_pportdata *ppd, u16 key)
872{
873 int i;
874 int ret;
875
876 for (i = 0; i < ARRAY_SIZE(ppd->pkeys); i++) {
877 if (ppd->pkeys[i] != key)
878 continue;
879 if (atomic_dec_and_test(&ppd->pkeyrefs[i])) {
880 ppd->pkeys[i] = 0;
881 ret = 1;
882 goto bail;
883 }
884 break;
885 }
886
887 ret = 0;
888
889bail:
890 return ret;
891}
892
893/**
894 * add_pkey - add the given PKEY to the hardware table
895 * @dd: the qlogic_ib device
896 * @key: the PKEY
897 *
898 * Return an error code if unable to add the entry, zero if no change,
899 * or 1 if the hardware PKEY register needs to be updated.
900 */
901static int add_pkey(struct qib_pportdata *ppd, u16 key)
902{
903 int i;
904 u16 lkey = key & 0x7FFF;
905 int any = 0;
906 int ret;
907
908 if (lkey == 0x7FFF) {
909 ret = 0;
910 goto bail;
911 }
912
913 /* Look for an empty slot or a matching PKEY. */
914 for (i = 0; i < ARRAY_SIZE(ppd->pkeys); i++) {
915 if (!ppd->pkeys[i]) {
916 any++;
917 continue;
918 }
919 /* If it matches exactly, try to increment the ref count */
920 if (ppd->pkeys[i] == key) {
921 if (atomic_inc_return(&ppd->pkeyrefs[i]) > 1) {
922 ret = 0;
923 goto bail;
924 }
925 /* Lost the race. Look for an empty slot below. */
926 atomic_dec(&ppd->pkeyrefs[i]);
927 any++;
928 }
929 /*
930 * It makes no sense to have both the limited and unlimited
931 * PKEY set at the same time since the unlimited one will
932 * disable the limited one.
933 */
934 if ((ppd->pkeys[i] & 0x7FFF) == lkey) {
935 ret = -EEXIST;
936 goto bail;
937 }
938 }
939 if (!any) {
940 ret = -EBUSY;
941 goto bail;
942 }
943 for (i = 0; i < ARRAY_SIZE(ppd->pkeys); i++) {
944 if (!ppd->pkeys[i] &&
945 atomic_inc_return(&ppd->pkeyrefs[i]) == 1) {
946 /* for qibstats, etc. */
947 ppd->pkeys[i] = key;
948 ret = 1;
949 goto bail;
950 }
951 }
952 ret = -EBUSY;
953
954bail:
955 return ret;
956}
957
958/**
959 * set_pkeys - set the PKEY table for ctxt 0
960 * @dd: the qlogic_ib device
961 * @port: the IB port number
962 * @pkeys: the PKEY table
963 */
964static int set_pkeys(struct qib_devdata *dd, u8 port, u16 *pkeys)
965{
966 struct qib_pportdata *ppd;
967 struct qib_ctxtdata *rcd;
968 int i;
969 int changed = 0;
970
971 /*
972 * IB port one/two always maps to context zero/one,
973 * always a kernel context, no locking needed
974 * If we get here with ppd setup, no need to check
975 * that rcd is valid.
976 */
977 ppd = dd->pport + (port - 1);
978 rcd = dd->rcd[ppd->hw_pidx];
979
980 for (i = 0; i < ARRAY_SIZE(rcd->pkeys); i++) {
981 u16 key = pkeys[i];
982 u16 okey = rcd->pkeys[i];
983
984 if (key == okey)
985 continue;
986 /*
987 * The value of this PKEY table entry is changing.
988 * Remove the old entry in the hardware's array of PKEYs.
989 */
990 if (okey & 0x7FFF)
991 changed |= rm_pkey(ppd, okey);
992 if (key & 0x7FFF) {
993 int ret = add_pkey(ppd, key);
994
995 if (ret < 0)
996 key = 0;
997 else
998 changed |= ret;
999 }
1000 rcd->pkeys[i] = key;
1001 }
1002 if (changed) {
1003 struct ib_event event;
1004
1005 (void) dd->f_set_ib_cfg(ppd, QIB_IB_CFG_PKEYS, 0);
1006
1007 event.event = IB_EVENT_PKEY_CHANGE;
1008 event.device = &dd->verbs_dev.ibdev;
1009 event.element.port_num = 1;
1010 ib_dispatch_event(&event);
1011 }
1012 return 0;
1013}
1014
1015static int subn_set_pkeytable(struct ib_smp *smp, struct ib_device *ibdev,
1016 u8 port)
1017{
1018 u32 startpx = 32 * (be32_to_cpu(smp->attr_mod) & 0xffff);
1019 __be16 *p = (__be16 *) smp->data;
1020 u16 *q = (u16 *) smp->data;
1021 struct qib_devdata *dd = dd_from_ibdev(ibdev);
1022 unsigned i, n = qib_get_npkeys(dd);
1023
1024 for (i = 0; i < n; i++)
1025 q[i] = be16_to_cpu(p[i]);
1026
1027 if (startpx != 0 || set_pkeys(dd, port, q) != 0)
1028 smp->status |= IB_SMP_INVALID_FIELD;
1029
1030 return subn_get_pkeytable(smp, ibdev, port);
1031}
1032
1033static int subn_get_sl_to_vl(struct ib_smp *smp, struct ib_device *ibdev,
1034 u8 port)
1035{
1036 struct qib_ibport *ibp = to_iport(ibdev, port);
1037 u8 *p = (u8 *) smp->data;
1038 unsigned i;
1039
1040 memset(smp->data, 0, sizeof(smp->data));
1041
1042 if (!(ibp->port_cap_flags & IB_PORT_SL_MAP_SUP))
1043 smp->status |= IB_SMP_UNSUP_METHOD;
1044 else
1045 for (i = 0; i < ARRAY_SIZE(ibp->sl_to_vl); i += 2)
1046 *p++ = (ibp->sl_to_vl[i] << 4) | ibp->sl_to_vl[i + 1];
1047
1048 return reply(smp);
1049}
1050
1051static int subn_set_sl_to_vl(struct ib_smp *smp, struct ib_device *ibdev,
1052 u8 port)
1053{
1054 struct qib_ibport *ibp = to_iport(ibdev, port);
1055 u8 *p = (u8 *) smp->data;
1056 unsigned i;
1057
1058 if (!(ibp->port_cap_flags & IB_PORT_SL_MAP_SUP)) {
1059 smp->status |= IB_SMP_UNSUP_METHOD;
1060 return reply(smp);
1061 }
1062
1063 for (i = 0; i < ARRAY_SIZE(ibp->sl_to_vl); i += 2, p++) {
1064 ibp->sl_to_vl[i] = *p >> 4;
1065 ibp->sl_to_vl[i + 1] = *p & 0xF;
1066 }
1067 qib_set_uevent_bits(ppd_from_ibp(to_iport(ibdev, port)),
1068 _QIB_EVENT_SL2VL_CHANGE_BIT);
1069
1070 return subn_get_sl_to_vl(smp, ibdev, port);
1071}
1072
1073static int subn_get_vl_arb(struct ib_smp *smp, struct ib_device *ibdev,
1074 u8 port)
1075{
1076 unsigned which = be32_to_cpu(smp->attr_mod) >> 16;
1077 struct qib_pportdata *ppd = ppd_from_ibp(to_iport(ibdev, port));
1078
1079 memset(smp->data, 0, sizeof(smp->data));
1080
1081 if (ppd->vls_supported == IB_VL_VL0)
1082 smp->status |= IB_SMP_UNSUP_METHOD;
1083 else if (which == IB_VLARB_LOWPRI_0_31)
1084 (void) ppd->dd->f_get_ib_table(ppd, QIB_IB_TBL_VL_LOW_ARB,
1085 smp->data);
1086 else if (which == IB_VLARB_HIGHPRI_0_31)
1087 (void) ppd->dd->f_get_ib_table(ppd, QIB_IB_TBL_VL_HIGH_ARB,
1088 smp->data);
1089 else
1090 smp->status |= IB_SMP_INVALID_FIELD;
1091
1092 return reply(smp);
1093}
1094
1095static int subn_set_vl_arb(struct ib_smp *smp, struct ib_device *ibdev,
1096 u8 port)
1097{
1098 unsigned which = be32_to_cpu(smp->attr_mod) >> 16;
1099 struct qib_pportdata *ppd = ppd_from_ibp(to_iport(ibdev, port));
1100
1101 if (ppd->vls_supported == IB_VL_VL0)
1102 smp->status |= IB_SMP_UNSUP_METHOD;
1103 else if (which == IB_VLARB_LOWPRI_0_31)
1104 (void) ppd->dd->f_set_ib_table(ppd, QIB_IB_TBL_VL_LOW_ARB,
1105 smp->data);
1106 else if (which == IB_VLARB_HIGHPRI_0_31)
1107 (void) ppd->dd->f_set_ib_table(ppd, QIB_IB_TBL_VL_HIGH_ARB,
1108 smp->data);
1109 else
1110 smp->status |= IB_SMP_INVALID_FIELD;
1111
1112 return subn_get_vl_arb(smp, ibdev, port);
1113}
1114
1115static int subn_trap_repress(struct ib_smp *smp, struct ib_device *ibdev,
1116 u8 port)
1117{
1118 /*
1119 * For now, we only send the trap once so no need to process this.
1120 * o13-6, o13-7,
1121 * o14-3.a4 The SMA shall not send any message in response to a valid
1122 * SubnTrapRepress() message.
1123 */
1124 return IB_MAD_RESULT_SUCCESS | IB_MAD_RESULT_CONSUMED;
1125}
1126
1127static int pma_get_classportinfo(struct ib_perf *pmp,
1128 struct ib_device *ibdev)
1129{
1130 struct ib_pma_classportinfo *p =
1131 (struct ib_pma_classportinfo *)pmp->data;
1132 struct qib_devdata *dd = dd_from_ibdev(ibdev);
1133
1134 memset(pmp->data, 0, sizeof(pmp->data));
1135
1136 if (pmp->attr_mod != 0)
1137 pmp->status |= IB_SMP_INVALID_FIELD;
1138
1139 /* Note that AllPortSelect is not valid */
1140 p->base_version = 1;
1141 p->class_version = 1;
1142 p->cap_mask = IB_PMA_CLASS_CAP_EXT_WIDTH;
1143 /*
1144 * Set the most significant bit of CM2 to indicate support for
1145 * congestion statistics
1146 */
1147 p->reserved[0] = dd->psxmitwait_supported << 7;
1148 /*
1149 * Expected response time is 4.096 usec. * 2^18 == 1.073741824 sec.
1150 */
1151 p->resp_time_value = 18;
1152
1153 return reply((struct ib_smp *) pmp);
1154}
1155
1156static int pma_get_portsamplescontrol(struct ib_perf *pmp,
1157 struct ib_device *ibdev, u8 port)
1158{
1159 struct ib_pma_portsamplescontrol *p =
1160 (struct ib_pma_portsamplescontrol *)pmp->data;
1161 struct qib_ibdev *dev = to_idev(ibdev);
1162 struct qib_devdata *dd = dd_from_dev(dev);
1163 struct qib_ibport *ibp = to_iport(ibdev, port);
1164 struct qib_pportdata *ppd = ppd_from_ibp(ibp);
1165 unsigned long flags;
1166 u8 port_select = p->port_select;
1167
1168 memset(pmp->data, 0, sizeof(pmp->data));
1169
1170 p->port_select = port_select;
1171 if (pmp->attr_mod != 0 || port_select != port) {
1172 pmp->status |= IB_SMP_INVALID_FIELD;
1173 goto bail;
1174 }
1175 spin_lock_irqsave(&ibp->lock, flags);
1176 p->tick = dd->f_get_ib_cfg(ppd, QIB_IB_CFG_PMA_TICKS);
1177 p->sample_status = dd->f_portcntr(ppd, QIBPORTCNTR_PSSTAT);
1178 p->counter_width = 4; /* 32 bit counters */
1179 p->counter_mask0_9 = COUNTER_MASK0_9;
1180 p->sample_start = cpu_to_be32(ibp->pma_sample_start);
1181 p->sample_interval = cpu_to_be32(ibp->pma_sample_interval);
1182 p->tag = cpu_to_be16(ibp->pma_tag);
1183 p->counter_select[0] = ibp->pma_counter_select[0];
1184 p->counter_select[1] = ibp->pma_counter_select[1];
1185 p->counter_select[2] = ibp->pma_counter_select[2];
1186 p->counter_select[3] = ibp->pma_counter_select[3];
1187 p->counter_select[4] = ibp->pma_counter_select[4];
1188 spin_unlock_irqrestore(&ibp->lock, flags);
1189
1190bail:
1191 return reply((struct ib_smp *) pmp);
1192}
1193
1194static int pma_set_portsamplescontrol(struct ib_perf *pmp,
1195 struct ib_device *ibdev, u8 port)
1196{
1197 struct ib_pma_portsamplescontrol *p =
1198 (struct ib_pma_portsamplescontrol *)pmp->data;
1199 struct qib_ibdev *dev = to_idev(ibdev);
1200 struct qib_devdata *dd = dd_from_dev(dev);
1201 struct qib_ibport *ibp = to_iport(ibdev, port);
1202 struct qib_pportdata *ppd = ppd_from_ibp(ibp);
1203 unsigned long flags;
1204 u8 status, xmit_flags;
1205 int ret;
1206
1207 if (pmp->attr_mod != 0 || p->port_select != port) {
1208 pmp->status |= IB_SMP_INVALID_FIELD;
1209 ret = reply((struct ib_smp *) pmp);
1210 goto bail;
1211 }
1212
1213 spin_lock_irqsave(&ibp->lock, flags);
1214
1215 /* Port Sampling code owns the PS* HW counters */
1216 xmit_flags = ppd->cong_stats.flags;
1217 ppd->cong_stats.flags = IB_PMA_CONG_HW_CONTROL_SAMPLE;
1218 status = dd->f_portcntr(ppd, QIBPORTCNTR_PSSTAT);
1219 if (status == IB_PMA_SAMPLE_STATUS_DONE ||
1220 (status == IB_PMA_SAMPLE_STATUS_RUNNING &&
1221 xmit_flags == IB_PMA_CONG_HW_CONTROL_TIMER)) {
1222 ibp->pma_sample_start = be32_to_cpu(p->sample_start);
1223 ibp->pma_sample_interval = be32_to_cpu(p->sample_interval);
1224 ibp->pma_tag = be16_to_cpu(p->tag);
1225 ibp->pma_counter_select[0] = p->counter_select[0];
1226 ibp->pma_counter_select[1] = p->counter_select[1];
1227 ibp->pma_counter_select[2] = p->counter_select[2];
1228 ibp->pma_counter_select[3] = p->counter_select[3];
1229 ibp->pma_counter_select[4] = p->counter_select[4];
1230 dd->f_set_cntr_sample(ppd, ibp->pma_sample_interval,
1231 ibp->pma_sample_start);
1232 }
1233 spin_unlock_irqrestore(&ibp->lock, flags);
1234
1235 ret = pma_get_portsamplescontrol(pmp, ibdev, port);
1236
1237bail:
1238 return ret;
1239}
1240
1241static u64 get_counter(struct qib_ibport *ibp, struct qib_pportdata *ppd,
1242 __be16 sel)
1243{
1244 u64 ret;
1245
1246 switch (sel) {
1247 case IB_PMA_PORT_XMIT_DATA:
1248 ret = ppd->dd->f_portcntr(ppd, QIBPORTCNTR_PSXMITDATA);
1249 break;
1250 case IB_PMA_PORT_RCV_DATA:
1251 ret = ppd->dd->f_portcntr(ppd, QIBPORTCNTR_PSRCVDATA);
1252 break;
1253 case IB_PMA_PORT_XMIT_PKTS:
1254 ret = ppd->dd->f_portcntr(ppd, QIBPORTCNTR_PSXMITPKTS);
1255 break;
1256 case IB_PMA_PORT_RCV_PKTS:
1257 ret = ppd->dd->f_portcntr(ppd, QIBPORTCNTR_PSRCVPKTS);
1258 break;
1259 case IB_PMA_PORT_XMIT_WAIT:
1260 ret = ppd->dd->f_portcntr(ppd, QIBPORTCNTR_PSXMITWAIT);
1261 break;
1262 default:
1263 ret = 0;
1264 }
1265
1266 return ret;
1267}
1268
1269/* This function assumes that the xmit_wait lock is already held */
1270static u64 xmit_wait_get_value_delta(struct qib_pportdata *ppd)
1271{
1272 u32 delta;
1273
1274 delta = get_counter(&ppd->ibport_data, ppd,
1275 IB_PMA_PORT_XMIT_WAIT);
1276 return ppd->cong_stats.counter + delta;
1277}
1278
1279static void cache_hw_sample_counters(struct qib_pportdata *ppd)
1280{
1281 struct qib_ibport *ibp = &ppd->ibport_data;
1282
1283 ppd->cong_stats.counter_cache.psxmitdata =
1284 get_counter(ibp, ppd, IB_PMA_PORT_XMIT_DATA);
1285 ppd->cong_stats.counter_cache.psrcvdata =
1286 get_counter(ibp, ppd, IB_PMA_PORT_RCV_DATA);
1287 ppd->cong_stats.counter_cache.psxmitpkts =
1288 get_counter(ibp, ppd, IB_PMA_PORT_XMIT_PKTS);
1289 ppd->cong_stats.counter_cache.psrcvpkts =
1290 get_counter(ibp, ppd, IB_PMA_PORT_RCV_PKTS);
1291 ppd->cong_stats.counter_cache.psxmitwait =
1292 get_counter(ibp, ppd, IB_PMA_PORT_XMIT_WAIT);
1293}
1294
1295static u64 get_cache_hw_sample_counters(struct qib_pportdata *ppd,
1296 __be16 sel)
1297{
1298 u64 ret;
1299
1300 switch (sel) {
1301 case IB_PMA_PORT_XMIT_DATA:
1302 ret = ppd->cong_stats.counter_cache.psxmitdata;
1303 break;
1304 case IB_PMA_PORT_RCV_DATA:
1305 ret = ppd->cong_stats.counter_cache.psrcvdata;
1306 break;
1307 case IB_PMA_PORT_XMIT_PKTS:
1308 ret = ppd->cong_stats.counter_cache.psxmitpkts;
1309 break;
1310 case IB_PMA_PORT_RCV_PKTS:
1311 ret = ppd->cong_stats.counter_cache.psrcvpkts;
1312 break;
1313 case IB_PMA_PORT_XMIT_WAIT:
1314 ret = ppd->cong_stats.counter_cache.psxmitwait;
1315 break;
1316 default:
1317 ret = 0;
1318 }
1319
1320 return ret;
1321}
1322
1323static int pma_get_portsamplesresult(struct ib_perf *pmp,
1324 struct ib_device *ibdev, u8 port)
1325{
1326 struct ib_pma_portsamplesresult *p =
1327 (struct ib_pma_portsamplesresult *)pmp->data;
1328 struct qib_ibdev *dev = to_idev(ibdev);
1329 struct qib_devdata *dd = dd_from_dev(dev);
1330 struct qib_ibport *ibp = to_iport(ibdev, port);
1331 struct qib_pportdata *ppd = ppd_from_ibp(ibp);
1332 unsigned long flags;
1333 u8 status;
1334 int i;
1335
1336 memset(pmp->data, 0, sizeof(pmp->data));
1337 spin_lock_irqsave(&ibp->lock, flags);
1338 p->tag = cpu_to_be16(ibp->pma_tag);
1339 if (ppd->cong_stats.flags == IB_PMA_CONG_HW_CONTROL_TIMER)
1340 p->sample_status = IB_PMA_SAMPLE_STATUS_DONE;
1341 else {
1342 status = dd->f_portcntr(ppd, QIBPORTCNTR_PSSTAT);
1343 p->sample_status = cpu_to_be16(status);
1344 if (status == IB_PMA_SAMPLE_STATUS_DONE) {
1345 cache_hw_sample_counters(ppd);
1346 ppd->cong_stats.counter =
1347 xmit_wait_get_value_delta(ppd);
1348 dd->f_set_cntr_sample(ppd,
1349 QIB_CONG_TIMER_PSINTERVAL, 0);
1350 ppd->cong_stats.flags = IB_PMA_CONG_HW_CONTROL_TIMER;
1351 }
1352 }
1353 for (i = 0; i < ARRAY_SIZE(ibp->pma_counter_select); i++)
1354 p->counter[i] = cpu_to_be32(
1355 get_cache_hw_sample_counters(
1356 ppd, ibp->pma_counter_select[i]));
1357 spin_unlock_irqrestore(&ibp->lock, flags);
1358
1359 return reply((struct ib_smp *) pmp);
1360}
1361
1362static int pma_get_portsamplesresult_ext(struct ib_perf *pmp,
1363 struct ib_device *ibdev, u8 port)
1364{
1365 struct ib_pma_portsamplesresult_ext *p =
1366 (struct ib_pma_portsamplesresult_ext *)pmp->data;
1367 struct qib_ibdev *dev = to_idev(ibdev);
1368 struct qib_devdata *dd = dd_from_dev(dev);
1369 struct qib_ibport *ibp = to_iport(ibdev, port);
1370 struct qib_pportdata *ppd = ppd_from_ibp(ibp);
1371 unsigned long flags;
1372 u8 status;
1373 int i;
1374
1375 /* Port Sampling code owns the PS* HW counters */
1376 memset(pmp->data, 0, sizeof(pmp->data));
1377 spin_lock_irqsave(&ibp->lock, flags);
1378 p->tag = cpu_to_be16(ibp->pma_tag);
1379 if (ppd->cong_stats.flags == IB_PMA_CONG_HW_CONTROL_TIMER)
1380 p->sample_status = IB_PMA_SAMPLE_STATUS_DONE;
1381 else {
1382 status = dd->f_portcntr(ppd, QIBPORTCNTR_PSSTAT);
1383 p->sample_status = cpu_to_be16(status);
1384 /* 64 bits */
1385 p->extended_width = cpu_to_be32(0x80000000);
1386 if (status == IB_PMA_SAMPLE_STATUS_DONE) {
1387 cache_hw_sample_counters(ppd);
1388 ppd->cong_stats.counter =
1389 xmit_wait_get_value_delta(ppd);
1390 dd->f_set_cntr_sample(ppd,
1391 QIB_CONG_TIMER_PSINTERVAL, 0);
1392 ppd->cong_stats.flags = IB_PMA_CONG_HW_CONTROL_TIMER;
1393 }
1394 }
1395 for (i = 0; i < ARRAY_SIZE(ibp->pma_counter_select); i++)
1396 p->counter[i] = cpu_to_be64(
1397 get_cache_hw_sample_counters(
1398 ppd, ibp->pma_counter_select[i]));
1399 spin_unlock_irqrestore(&ibp->lock, flags);
1400
1401 return reply((struct ib_smp *) pmp);
1402}
1403
1404static int pma_get_portcounters(struct ib_perf *pmp,
1405 struct ib_device *ibdev, u8 port)
1406{
1407 struct ib_pma_portcounters *p = (struct ib_pma_portcounters *)
1408 pmp->data;
1409 struct qib_ibport *ibp = to_iport(ibdev, port);
1410 struct qib_pportdata *ppd = ppd_from_ibp(ibp);
1411 struct qib_verbs_counters cntrs;
1412 u8 port_select = p->port_select;
1413
1414 qib_get_counters(ppd, &cntrs);
1415
1416 /* Adjust counters for any resets done. */
1417 cntrs.symbol_error_counter -= ibp->z_symbol_error_counter;
1418 cntrs.link_error_recovery_counter -=
1419 ibp->z_link_error_recovery_counter;
1420 cntrs.link_downed_counter -= ibp->z_link_downed_counter;
1421 cntrs.port_rcv_errors -= ibp->z_port_rcv_errors;
1422 cntrs.port_rcv_remphys_errors -= ibp->z_port_rcv_remphys_errors;
1423 cntrs.port_xmit_discards -= ibp->z_port_xmit_discards;
1424 cntrs.port_xmit_data -= ibp->z_port_xmit_data;
1425 cntrs.port_rcv_data -= ibp->z_port_rcv_data;
1426 cntrs.port_xmit_packets -= ibp->z_port_xmit_packets;
1427 cntrs.port_rcv_packets -= ibp->z_port_rcv_packets;
1428 cntrs.local_link_integrity_errors -=
1429 ibp->z_local_link_integrity_errors;
1430 cntrs.excessive_buffer_overrun_errors -=
1431 ibp->z_excessive_buffer_overrun_errors;
1432 cntrs.vl15_dropped -= ibp->z_vl15_dropped;
1433 cntrs.vl15_dropped += ibp->n_vl15_dropped;
1434
1435 memset(pmp->data, 0, sizeof(pmp->data));
1436
1437 p->port_select = port_select;
1438 if (pmp->attr_mod != 0 || port_select != port)
1439 pmp->status |= IB_SMP_INVALID_FIELD;
1440
1441 if (cntrs.symbol_error_counter > 0xFFFFUL)
1442 p->symbol_error_counter = cpu_to_be16(0xFFFF);
1443 else
1444 p->symbol_error_counter =
1445 cpu_to_be16((u16)cntrs.symbol_error_counter);
1446 if (cntrs.link_error_recovery_counter > 0xFFUL)
1447 p->link_error_recovery_counter = 0xFF;
1448 else
1449 p->link_error_recovery_counter =
1450 (u8)cntrs.link_error_recovery_counter;
1451 if (cntrs.link_downed_counter > 0xFFUL)
1452 p->link_downed_counter = 0xFF;
1453 else
1454 p->link_downed_counter = (u8)cntrs.link_downed_counter;
1455 if (cntrs.port_rcv_errors > 0xFFFFUL)
1456 p->port_rcv_errors = cpu_to_be16(0xFFFF);
1457 else
1458 p->port_rcv_errors =
1459 cpu_to_be16((u16) cntrs.port_rcv_errors);
1460 if (cntrs.port_rcv_remphys_errors > 0xFFFFUL)
1461 p->port_rcv_remphys_errors = cpu_to_be16(0xFFFF);
1462 else
1463 p->port_rcv_remphys_errors =
1464 cpu_to_be16((u16)cntrs.port_rcv_remphys_errors);
1465 if (cntrs.port_xmit_discards > 0xFFFFUL)
1466 p->port_xmit_discards = cpu_to_be16(0xFFFF);
1467 else
1468 p->port_xmit_discards =
1469 cpu_to_be16((u16)cntrs.port_xmit_discards);
1470 if (cntrs.local_link_integrity_errors > 0xFUL)
1471 cntrs.local_link_integrity_errors = 0xFUL;
1472 if (cntrs.excessive_buffer_overrun_errors > 0xFUL)
1473 cntrs.excessive_buffer_overrun_errors = 0xFUL;
1474 p->lli_ebor_errors = (cntrs.local_link_integrity_errors << 4) |
1475 cntrs.excessive_buffer_overrun_errors;
1476 if (cntrs.vl15_dropped > 0xFFFFUL)
1477 p->vl15_dropped = cpu_to_be16(0xFFFF);
1478 else
1479 p->vl15_dropped = cpu_to_be16((u16)cntrs.vl15_dropped);
1480 if (cntrs.port_xmit_data > 0xFFFFFFFFUL)
1481 p->port_xmit_data = cpu_to_be32(0xFFFFFFFF);
1482 else
1483 p->port_xmit_data = cpu_to_be32((u32)cntrs.port_xmit_data);
1484 if (cntrs.port_rcv_data > 0xFFFFFFFFUL)
1485 p->port_rcv_data = cpu_to_be32(0xFFFFFFFF);
1486 else
1487 p->port_rcv_data = cpu_to_be32((u32)cntrs.port_rcv_data);
1488 if (cntrs.port_xmit_packets > 0xFFFFFFFFUL)
1489 p->port_xmit_packets = cpu_to_be32(0xFFFFFFFF);
1490 else
1491 p->port_xmit_packets =
1492 cpu_to_be32((u32)cntrs.port_xmit_packets);
1493 if (cntrs.port_rcv_packets > 0xFFFFFFFFUL)
1494 p->port_rcv_packets = cpu_to_be32(0xFFFFFFFF);
1495 else
1496 p->port_rcv_packets =
1497 cpu_to_be32((u32) cntrs.port_rcv_packets);
1498
1499 return reply((struct ib_smp *) pmp);
1500}
1501
1502static int pma_get_portcounters_cong(struct ib_perf *pmp,
1503 struct ib_device *ibdev, u8 port)
1504{
1505 /* Congestion PMA packets start at offset 24 not 64 */
1506 struct ib_pma_portcounters_cong *p =
1507 (struct ib_pma_portcounters_cong *)pmp->reserved;
1508 struct qib_verbs_counters cntrs;
1509 struct qib_ibport *ibp = to_iport(ibdev, port);
1510 struct qib_pportdata *ppd = ppd_from_ibp(ibp);
1511 struct qib_devdata *dd = dd_from_ppd(ppd);
1512 u32 port_select = be32_to_cpu(pmp->attr_mod) & 0xFF;
1513 u64 xmit_wait_counter;
1514 unsigned long flags;
1515
1516 /*
1517 * This check is performed only in the GET method because the
1518 * SET method ends up calling this anyway.
1519 */
1520 if (!dd->psxmitwait_supported)
1521 pmp->status |= IB_SMP_UNSUP_METH_ATTR;
1522 if (port_select != port)
1523 pmp->status |= IB_SMP_INVALID_FIELD;
1524
1525 qib_get_counters(ppd, &cntrs);
1526 spin_lock_irqsave(&ppd->ibport_data.lock, flags);
1527 xmit_wait_counter = xmit_wait_get_value_delta(ppd);
1528 spin_unlock_irqrestore(&ppd->ibport_data.lock, flags);
1529
1530 /* Adjust counters for any resets done. */
1531 cntrs.symbol_error_counter -= ibp->z_symbol_error_counter;
1532 cntrs.link_error_recovery_counter -=
1533 ibp->z_link_error_recovery_counter;
1534 cntrs.link_downed_counter -= ibp->z_link_downed_counter;
1535 cntrs.port_rcv_errors -= ibp->z_port_rcv_errors;
1536 cntrs.port_rcv_remphys_errors -=
1537 ibp->z_port_rcv_remphys_errors;
1538 cntrs.port_xmit_discards -= ibp->z_port_xmit_discards;
1539 cntrs.local_link_integrity_errors -=
1540 ibp->z_local_link_integrity_errors;
1541 cntrs.excessive_buffer_overrun_errors -=
1542 ibp->z_excessive_buffer_overrun_errors;
1543 cntrs.vl15_dropped -= ibp->z_vl15_dropped;
1544 cntrs.vl15_dropped += ibp->n_vl15_dropped;
1545 cntrs.port_xmit_data -= ibp->z_port_xmit_data;
1546 cntrs.port_rcv_data -= ibp->z_port_rcv_data;
1547 cntrs.port_xmit_packets -= ibp->z_port_xmit_packets;
1548 cntrs.port_rcv_packets -= ibp->z_port_rcv_packets;
1549
1550 memset(pmp->reserved, 0, sizeof(pmp->reserved) +
1551 sizeof(pmp->data));
1552
1553 /*
1554 * Set top 3 bits to indicate interval in picoseconds in
1555 * remaining bits.
1556 */
1557 p->port_check_rate =
1558 cpu_to_be16((QIB_XMIT_RATE_PICO << 13) |
1559 (dd->psxmitwait_check_rate &
1560 ~(QIB_XMIT_RATE_PICO << 13)));
1561 p->port_adr_events = cpu_to_be64(0);
1562 p->port_xmit_wait = cpu_to_be64(xmit_wait_counter);
1563 p->port_xmit_data = cpu_to_be64(cntrs.port_xmit_data);
1564 p->port_rcv_data = cpu_to_be64(cntrs.port_rcv_data);
1565 p->port_xmit_packets =
1566 cpu_to_be64(cntrs.port_xmit_packets);
1567 p->port_rcv_packets =
1568 cpu_to_be64(cntrs.port_rcv_packets);
1569 if (cntrs.symbol_error_counter > 0xFFFFUL)
1570 p->symbol_error_counter = cpu_to_be16(0xFFFF);
1571 else
1572 p->symbol_error_counter =
1573 cpu_to_be16(
1574 (u16)cntrs.symbol_error_counter);
1575 if (cntrs.link_error_recovery_counter > 0xFFUL)
1576 p->link_error_recovery_counter = 0xFF;
1577 else
1578 p->link_error_recovery_counter =
1579 (u8)cntrs.link_error_recovery_counter;
1580 if (cntrs.link_downed_counter > 0xFFUL)
1581 p->link_downed_counter = 0xFF;
1582 else
1583 p->link_downed_counter =
1584 (u8)cntrs.link_downed_counter;
1585 if (cntrs.port_rcv_errors > 0xFFFFUL)
1586 p->port_rcv_errors = cpu_to_be16(0xFFFF);
1587 else
1588 p->port_rcv_errors =
1589 cpu_to_be16((u16) cntrs.port_rcv_errors);
1590 if (cntrs.port_rcv_remphys_errors > 0xFFFFUL)
1591 p->port_rcv_remphys_errors = cpu_to_be16(0xFFFF);
1592 else
1593 p->port_rcv_remphys_errors =
1594 cpu_to_be16(
1595 (u16)cntrs.port_rcv_remphys_errors);
1596 if (cntrs.port_xmit_discards > 0xFFFFUL)
1597 p->port_xmit_discards = cpu_to_be16(0xFFFF);
1598 else
1599 p->port_xmit_discards =
1600 cpu_to_be16((u16)cntrs.port_xmit_discards);
1601 if (cntrs.local_link_integrity_errors > 0xFUL)
1602 cntrs.local_link_integrity_errors = 0xFUL;
1603 if (cntrs.excessive_buffer_overrun_errors > 0xFUL)
1604 cntrs.excessive_buffer_overrun_errors = 0xFUL;
1605 p->lli_ebor_errors = (cntrs.local_link_integrity_errors << 4) |
1606 cntrs.excessive_buffer_overrun_errors;
1607 if (cntrs.vl15_dropped > 0xFFFFUL)
1608 p->vl15_dropped = cpu_to_be16(0xFFFF);
1609 else
1610 p->vl15_dropped = cpu_to_be16((u16)cntrs.vl15_dropped);
1611
1612 return reply((struct ib_smp *)pmp);
1613}
1614
1615static int pma_get_portcounters_ext(struct ib_perf *pmp,
1616 struct ib_device *ibdev, u8 port)
1617{
1618 struct ib_pma_portcounters_ext *p =
1619 (struct ib_pma_portcounters_ext *)pmp->data;
1620 struct qib_ibport *ibp = to_iport(ibdev, port);
1621 struct qib_pportdata *ppd = ppd_from_ibp(ibp);
1622 u64 swords, rwords, spkts, rpkts, xwait;
1623 u8 port_select = p->port_select;
1624
1625 memset(pmp->data, 0, sizeof(pmp->data));
1626
1627 p->port_select = port_select;
1628 if (pmp->attr_mod != 0 || port_select != port) {
1629 pmp->status |= IB_SMP_INVALID_FIELD;
1630 goto bail;
1631 }
1632
1633 qib_snapshot_counters(ppd, &swords, &rwords, &spkts, &rpkts, &xwait);
1634
1635 /* Adjust counters for any resets done. */
1636 swords -= ibp->z_port_xmit_data;
1637 rwords -= ibp->z_port_rcv_data;
1638 spkts -= ibp->z_port_xmit_packets;
1639 rpkts -= ibp->z_port_rcv_packets;
1640
1641 p->port_xmit_data = cpu_to_be64(swords);
1642 p->port_rcv_data = cpu_to_be64(rwords);
1643 p->port_xmit_packets = cpu_to_be64(spkts);
1644 p->port_rcv_packets = cpu_to_be64(rpkts);
1645 p->port_unicast_xmit_packets = cpu_to_be64(ibp->n_unicast_xmit);
1646 p->port_unicast_rcv_packets = cpu_to_be64(ibp->n_unicast_rcv);
1647 p->port_multicast_xmit_packets = cpu_to_be64(ibp->n_multicast_xmit);
1648 p->port_multicast_rcv_packets = cpu_to_be64(ibp->n_multicast_rcv);
1649
1650bail:
1651 return reply((struct ib_smp *) pmp);
1652}
1653
1654static int pma_set_portcounters(struct ib_perf *pmp,
1655 struct ib_device *ibdev, u8 port)
1656{
1657 struct ib_pma_portcounters *p = (struct ib_pma_portcounters *)
1658 pmp->data;
1659 struct qib_ibport *ibp = to_iport(ibdev, port);
1660 struct qib_pportdata *ppd = ppd_from_ibp(ibp);
1661 struct qib_verbs_counters cntrs;
1662
1663 /*
1664 * Since the HW doesn't support clearing counters, we save the
1665 * current count and subtract it from future responses.
1666 */
1667 qib_get_counters(ppd, &cntrs);
1668
1669 if (p->counter_select & IB_PMA_SEL_SYMBOL_ERROR)
1670 ibp->z_symbol_error_counter = cntrs.symbol_error_counter;
1671
1672 if (p->counter_select & IB_PMA_SEL_LINK_ERROR_RECOVERY)
1673 ibp->z_link_error_recovery_counter =
1674 cntrs.link_error_recovery_counter;
1675
1676 if (p->counter_select & IB_PMA_SEL_LINK_DOWNED)
1677 ibp->z_link_downed_counter = cntrs.link_downed_counter;
1678
1679 if (p->counter_select & IB_PMA_SEL_PORT_RCV_ERRORS)
1680 ibp->z_port_rcv_errors = cntrs.port_rcv_errors;
1681
1682 if (p->counter_select & IB_PMA_SEL_PORT_RCV_REMPHYS_ERRORS)
1683 ibp->z_port_rcv_remphys_errors =
1684 cntrs.port_rcv_remphys_errors;
1685
1686 if (p->counter_select & IB_PMA_SEL_PORT_XMIT_DISCARDS)
1687 ibp->z_port_xmit_discards = cntrs.port_xmit_discards;
1688
1689 if (p->counter_select & IB_PMA_SEL_LOCAL_LINK_INTEGRITY_ERRORS)
1690 ibp->z_local_link_integrity_errors =
1691 cntrs.local_link_integrity_errors;
1692
1693 if (p->counter_select & IB_PMA_SEL_EXCESSIVE_BUFFER_OVERRUNS)
1694 ibp->z_excessive_buffer_overrun_errors =
1695 cntrs.excessive_buffer_overrun_errors;
1696
1697 if (p->counter_select & IB_PMA_SEL_PORT_VL15_DROPPED) {
1698 ibp->n_vl15_dropped = 0;
1699 ibp->z_vl15_dropped = cntrs.vl15_dropped;
1700 }
1701
1702 if (p->counter_select & IB_PMA_SEL_PORT_XMIT_DATA)
1703 ibp->z_port_xmit_data = cntrs.port_xmit_data;
1704
1705 if (p->counter_select & IB_PMA_SEL_PORT_RCV_DATA)
1706 ibp->z_port_rcv_data = cntrs.port_rcv_data;
1707
1708 if (p->counter_select & IB_PMA_SEL_PORT_XMIT_PACKETS)
1709 ibp->z_port_xmit_packets = cntrs.port_xmit_packets;
1710
1711 if (p->counter_select & IB_PMA_SEL_PORT_RCV_PACKETS)
1712 ibp->z_port_rcv_packets = cntrs.port_rcv_packets;
1713
1714 return pma_get_portcounters(pmp, ibdev, port);
1715}
1716
1717static int pma_set_portcounters_cong(struct ib_perf *pmp,
1718 struct ib_device *ibdev, u8 port)
1719{
1720 struct qib_ibport *ibp = to_iport(ibdev, port);
1721 struct qib_pportdata *ppd = ppd_from_ibp(ibp);
1722 struct qib_devdata *dd = dd_from_ppd(ppd);
1723 struct qib_verbs_counters cntrs;
1724 u32 counter_select = (be32_to_cpu(pmp->attr_mod) >> 24) & 0xFF;
1725 int ret = 0;
1726 unsigned long flags;
1727
1728 qib_get_counters(ppd, &cntrs);
1729 /* Get counter values before we save them */
1730 ret = pma_get_portcounters_cong(pmp, ibdev, port);
1731
1732 if (counter_select & IB_PMA_SEL_CONG_XMIT) {
1733 spin_lock_irqsave(&ppd->ibport_data.lock, flags);
1734 ppd->cong_stats.counter = 0;
1735 dd->f_set_cntr_sample(ppd, QIB_CONG_TIMER_PSINTERVAL,
1736 0x0);
1737 spin_unlock_irqrestore(&ppd->ibport_data.lock, flags);
1738 }
1739 if (counter_select & IB_PMA_SEL_CONG_PORT_DATA) {
1740 ibp->z_port_xmit_data = cntrs.port_xmit_data;
1741 ibp->z_port_rcv_data = cntrs.port_rcv_data;
1742 ibp->z_port_xmit_packets = cntrs.port_xmit_packets;
1743 ibp->z_port_rcv_packets = cntrs.port_rcv_packets;
1744 }
1745 if (counter_select & IB_PMA_SEL_CONG_ALL) {
1746 ibp->z_symbol_error_counter =
1747 cntrs.symbol_error_counter;
1748 ibp->z_link_error_recovery_counter =
1749 cntrs.link_error_recovery_counter;
1750 ibp->z_link_downed_counter =
1751 cntrs.link_downed_counter;
1752 ibp->z_port_rcv_errors = cntrs.port_rcv_errors;
1753 ibp->z_port_rcv_remphys_errors =
1754 cntrs.port_rcv_remphys_errors;
1755 ibp->z_port_xmit_discards =
1756 cntrs.port_xmit_discards;
1757 ibp->z_local_link_integrity_errors =
1758 cntrs.local_link_integrity_errors;
1759 ibp->z_excessive_buffer_overrun_errors =
1760 cntrs.excessive_buffer_overrun_errors;
1761 ibp->n_vl15_dropped = 0;
1762 ibp->z_vl15_dropped = cntrs.vl15_dropped;
1763 }
1764
1765 return ret;
1766}
1767
1768static int pma_set_portcounters_ext(struct ib_perf *pmp,
1769 struct ib_device *ibdev, u8 port)
1770{
1771 struct ib_pma_portcounters *p = (struct ib_pma_portcounters *)
1772 pmp->data;
1773 struct qib_ibport *ibp = to_iport(ibdev, port);
1774 struct qib_pportdata *ppd = ppd_from_ibp(ibp);
1775 u64 swords, rwords, spkts, rpkts, xwait;
1776
1777 qib_snapshot_counters(ppd, &swords, &rwords, &spkts, &rpkts, &xwait);
1778
1779 if (p->counter_select & IB_PMA_SELX_PORT_XMIT_DATA)
1780 ibp->z_port_xmit_data = swords;
1781
1782 if (p->counter_select & IB_PMA_SELX_PORT_RCV_DATA)
1783 ibp->z_port_rcv_data = rwords;
1784
1785 if (p->counter_select & IB_PMA_SELX_PORT_XMIT_PACKETS)
1786 ibp->z_port_xmit_packets = spkts;
1787
1788 if (p->counter_select & IB_PMA_SELX_PORT_RCV_PACKETS)
1789 ibp->z_port_rcv_packets = rpkts;
1790
1791 if (p->counter_select & IB_PMA_SELX_PORT_UNI_XMIT_PACKETS)
1792 ibp->n_unicast_xmit = 0;
1793
1794 if (p->counter_select & IB_PMA_SELX_PORT_UNI_RCV_PACKETS)
1795 ibp->n_unicast_rcv = 0;
1796
1797 if (p->counter_select & IB_PMA_SELX_PORT_MULTI_XMIT_PACKETS)
1798 ibp->n_multicast_xmit = 0;
1799
1800 if (p->counter_select & IB_PMA_SELX_PORT_MULTI_RCV_PACKETS)
1801 ibp->n_multicast_rcv = 0;
1802
1803 return pma_get_portcounters_ext(pmp, ibdev, port);
1804}
1805
1806static int process_subn(struct ib_device *ibdev, int mad_flags,
1807 u8 port, struct ib_mad *in_mad,
1808 struct ib_mad *out_mad)
1809{
1810 struct ib_smp *smp = (struct ib_smp *)out_mad;
1811 struct qib_ibport *ibp = to_iport(ibdev, port);
1812 struct qib_pportdata *ppd = ppd_from_ibp(ibp);
1813 int ret;
1814
1815 *out_mad = *in_mad;
1816 if (smp->class_version != 1) {
1817 smp->status |= IB_SMP_UNSUP_VERSION;
1818 ret = reply(smp);
1819 goto bail;
1820 }
1821
1822 ret = check_mkey(ibp, smp, mad_flags);
1823 if (ret) {
1824 u32 port_num = be32_to_cpu(smp->attr_mod);
1825
1826 /*
1827 * If this is a get/set portinfo, we already check the
1828 * M_Key if the MAD is for another port and the M_Key
1829 * is OK on the receiving port. This check is needed
1830 * to increment the error counters when the M_Key
1831 * fails to match on *both* ports.
1832 */
1833 if (in_mad->mad_hdr.attr_id == IB_SMP_ATTR_PORT_INFO &&
1834 (smp->method == IB_MGMT_METHOD_GET ||
1835 smp->method == IB_MGMT_METHOD_SET) &&
1836 port_num && port_num <= ibdev->phys_port_cnt &&
1837 port != port_num)
1838 (void) check_mkey(to_iport(ibdev, port_num), smp, 0);
1839 goto bail;
1840 }
1841
1842 switch (smp->method) {
1843 case IB_MGMT_METHOD_GET:
1844 switch (smp->attr_id) {
1845 case IB_SMP_ATTR_NODE_DESC:
1846 ret = subn_get_nodedescription(smp, ibdev);
1847 goto bail;
1848 case IB_SMP_ATTR_NODE_INFO:
1849 ret = subn_get_nodeinfo(smp, ibdev, port);
1850 goto bail;
1851 case IB_SMP_ATTR_GUID_INFO:
1852 ret = subn_get_guidinfo(smp, ibdev, port);
1853 goto bail;
1854 case IB_SMP_ATTR_PORT_INFO:
1855 ret = subn_get_portinfo(smp, ibdev, port);
1856 goto bail;
1857 case IB_SMP_ATTR_PKEY_TABLE:
1858 ret = subn_get_pkeytable(smp, ibdev, port);
1859 goto bail;
1860 case IB_SMP_ATTR_SL_TO_VL_TABLE:
1861 ret = subn_get_sl_to_vl(smp, ibdev, port);
1862 goto bail;
1863 case IB_SMP_ATTR_VL_ARB_TABLE:
1864 ret = subn_get_vl_arb(smp, ibdev, port);
1865 goto bail;
1866 case IB_SMP_ATTR_SM_INFO:
1867 if (ibp->port_cap_flags & IB_PORT_SM_DISABLED) {
1868 ret = IB_MAD_RESULT_SUCCESS |
1869 IB_MAD_RESULT_CONSUMED;
1870 goto bail;
1871 }
1872 if (ibp->port_cap_flags & IB_PORT_SM) {
1873 ret = IB_MAD_RESULT_SUCCESS;
1874 goto bail;
1875 }
1876 /* FALLTHROUGH */
1877 default:
1878 smp->status |= IB_SMP_UNSUP_METH_ATTR;
1879 ret = reply(smp);
1880 goto bail;
1881 }
1882
1883 case IB_MGMT_METHOD_SET:
1884 switch (smp->attr_id) {
1885 case IB_SMP_ATTR_GUID_INFO:
1886 ret = subn_set_guidinfo(smp, ibdev, port);
1887 goto bail;
1888 case IB_SMP_ATTR_PORT_INFO:
1889 ret = subn_set_portinfo(smp, ibdev, port);
1890 goto bail;
1891 case IB_SMP_ATTR_PKEY_TABLE:
1892 ret = subn_set_pkeytable(smp, ibdev, port);
1893 goto bail;
1894 case IB_SMP_ATTR_SL_TO_VL_TABLE:
1895 ret = subn_set_sl_to_vl(smp, ibdev, port);
1896 goto bail;
1897 case IB_SMP_ATTR_VL_ARB_TABLE:
1898 ret = subn_set_vl_arb(smp, ibdev, port);
1899 goto bail;
1900 case IB_SMP_ATTR_SM_INFO:
1901 if (ibp->port_cap_flags & IB_PORT_SM_DISABLED) {
1902 ret = IB_MAD_RESULT_SUCCESS |
1903 IB_MAD_RESULT_CONSUMED;
1904 goto bail;
1905 }
1906 if (ibp->port_cap_flags & IB_PORT_SM) {
1907 ret = IB_MAD_RESULT_SUCCESS;
1908 goto bail;
1909 }
1910 /* FALLTHROUGH */
1911 default:
1912 smp->status |= IB_SMP_UNSUP_METH_ATTR;
1913 ret = reply(smp);
1914 goto bail;
1915 }
1916
1917 case IB_MGMT_METHOD_TRAP_REPRESS:
1918 if (smp->attr_id == IB_SMP_ATTR_NOTICE)
1919 ret = subn_trap_repress(smp, ibdev, port);
1920 else {
1921 smp->status |= IB_SMP_UNSUP_METH_ATTR;
1922 ret = reply(smp);
1923 }
1924 goto bail;
1925
1926 case IB_MGMT_METHOD_TRAP:
1927 case IB_MGMT_METHOD_REPORT:
1928 case IB_MGMT_METHOD_REPORT_RESP:
1929 case IB_MGMT_METHOD_GET_RESP:
1930 /*
1931 * The ib_mad module will call us to process responses
1932 * before checking for other consumers.
1933 * Just tell the caller to process it normally.
1934 */
1935 ret = IB_MAD_RESULT_SUCCESS;
1936 goto bail;
1937
1938 case IB_MGMT_METHOD_SEND:
1939 if (ib_get_smp_direction(smp) &&
1940 smp->attr_id == QIB_VENDOR_IPG) {
1941 ppd->dd->f_set_ib_cfg(ppd, QIB_IB_CFG_PORT,
1942 smp->data[0]);
1943 ret = IB_MAD_RESULT_SUCCESS | IB_MAD_RESULT_CONSUMED;
1944 } else
1945 ret = IB_MAD_RESULT_SUCCESS;
1946 goto bail;
1947
1948 default:
1949 smp->status |= IB_SMP_UNSUP_METHOD;
1950 ret = reply(smp);
1951 }
1952
1953bail:
1954 return ret;
1955}
1956
1957static int process_perf(struct ib_device *ibdev, u8 port,
1958 struct ib_mad *in_mad,
1959 struct ib_mad *out_mad)
1960{
1961 struct ib_perf *pmp = (struct ib_perf *)out_mad;
1962 int ret;
1963
1964 *out_mad = *in_mad;
1965 if (pmp->class_version != 1) {
1966 pmp->status |= IB_SMP_UNSUP_VERSION;
1967 ret = reply((struct ib_smp *) pmp);
1968 goto bail;
1969 }
1970
1971 switch (pmp->method) {
1972 case IB_MGMT_METHOD_GET:
1973 switch (pmp->attr_id) {
1974 case IB_PMA_CLASS_PORT_INFO:
1975 ret = pma_get_classportinfo(pmp, ibdev);
1976 goto bail;
1977 case IB_PMA_PORT_SAMPLES_CONTROL:
1978 ret = pma_get_portsamplescontrol(pmp, ibdev, port);
1979 goto bail;
1980 case IB_PMA_PORT_SAMPLES_RESULT:
1981 ret = pma_get_portsamplesresult(pmp, ibdev, port);
1982 goto bail;
1983 case IB_PMA_PORT_SAMPLES_RESULT_EXT:
1984 ret = pma_get_portsamplesresult_ext(pmp, ibdev, port);
1985 goto bail;
1986 case IB_PMA_PORT_COUNTERS:
1987 ret = pma_get_portcounters(pmp, ibdev, port);
1988 goto bail;
1989 case IB_PMA_PORT_COUNTERS_EXT:
1990 ret = pma_get_portcounters_ext(pmp, ibdev, port);
1991 goto bail;
1992 case IB_PMA_PORT_COUNTERS_CONG:
1993 ret = pma_get_portcounters_cong(pmp, ibdev, port);
1994 goto bail;
1995 default:
1996 pmp->status |= IB_SMP_UNSUP_METH_ATTR;
1997 ret = reply((struct ib_smp *) pmp);
1998 goto bail;
1999 }
2000
2001 case IB_MGMT_METHOD_SET:
2002 switch (pmp->attr_id) {
2003 case IB_PMA_PORT_SAMPLES_CONTROL:
2004 ret = pma_set_portsamplescontrol(pmp, ibdev, port);
2005 goto bail;
2006 case IB_PMA_PORT_COUNTERS:
2007 ret = pma_set_portcounters(pmp, ibdev, port);
2008 goto bail;
2009 case IB_PMA_PORT_COUNTERS_EXT:
2010 ret = pma_set_portcounters_ext(pmp, ibdev, port);
2011 goto bail;
2012 case IB_PMA_PORT_COUNTERS_CONG:
2013 ret = pma_set_portcounters_cong(pmp, ibdev, port);
2014 goto bail;
2015 default:
2016 pmp->status |= IB_SMP_UNSUP_METH_ATTR;
2017 ret = reply((struct ib_smp *) pmp);
2018 goto bail;
2019 }
2020
2021 case IB_MGMT_METHOD_TRAP:
2022 case IB_MGMT_METHOD_GET_RESP:
2023 /*
2024 * The ib_mad module will call us to process responses
2025 * before checking for other consumers.
2026 * Just tell the caller to process it normally.
2027 */
2028 ret = IB_MAD_RESULT_SUCCESS;
2029 goto bail;
2030
2031 default:
2032 pmp->status |= IB_SMP_UNSUP_METHOD;
2033 ret = reply((struct ib_smp *) pmp);
2034 }
2035
2036bail:
2037 return ret;
2038}
2039
2040/**
2041 * qib_process_mad - process an incoming MAD packet
2042 * @ibdev: the infiniband device this packet came in on
2043 * @mad_flags: MAD flags
2044 * @port: the port number this packet came in on
2045 * @in_wc: the work completion entry for this packet
2046 * @in_grh: the global route header for this packet
2047 * @in_mad: the incoming MAD
2048 * @out_mad: any outgoing MAD reply
2049 *
2050 * Returns IB_MAD_RESULT_SUCCESS if this is a MAD that we are not
2051 * interested in processing.
2052 *
2053 * Note that the verbs framework has already done the MAD sanity checks,
2054 * and hop count/pointer updating for IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE
2055 * MADs.
2056 *
2057 * This is called by the ib_mad module.
2058 */
2059int qib_process_mad(struct ib_device *ibdev, int mad_flags, u8 port,
2060 struct ib_wc *in_wc, struct ib_grh *in_grh,
2061 struct ib_mad *in_mad, struct ib_mad *out_mad)
2062{
2063 int ret;
2064
2065 switch (in_mad->mad_hdr.mgmt_class) {
2066 case IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE:
2067 case IB_MGMT_CLASS_SUBN_LID_ROUTED:
2068 ret = process_subn(ibdev, mad_flags, port, in_mad, out_mad);
2069 goto bail;
2070
2071 case IB_MGMT_CLASS_PERF_MGMT:
2072 ret = process_perf(ibdev, port, in_mad, out_mad);
2073 goto bail;
2074
2075 default:
2076 ret = IB_MAD_RESULT_SUCCESS;
2077 }
2078
2079bail:
2080 return ret;
2081}
2082
2083static void send_handler(struct ib_mad_agent *agent,
2084 struct ib_mad_send_wc *mad_send_wc)
2085{
2086 ib_free_send_mad(mad_send_wc->send_buf);
2087}
2088
2089static void xmit_wait_timer_func(unsigned long opaque)
2090{
2091 struct qib_pportdata *ppd = (struct qib_pportdata *)opaque;
2092 struct qib_devdata *dd = dd_from_ppd(ppd);
2093 unsigned long flags;
2094 u8 status;
2095
2096 spin_lock_irqsave(&ppd->ibport_data.lock, flags);
2097 if (ppd->cong_stats.flags == IB_PMA_CONG_HW_CONTROL_SAMPLE) {
2098 status = dd->f_portcntr(ppd, QIBPORTCNTR_PSSTAT);
2099 if (status == IB_PMA_SAMPLE_STATUS_DONE) {
2100 /* save counter cache */
2101 cache_hw_sample_counters(ppd);
2102 ppd->cong_stats.flags = IB_PMA_CONG_HW_CONTROL_TIMER;
2103 } else
2104 goto done;
2105 }
2106 ppd->cong_stats.counter = xmit_wait_get_value_delta(ppd);
2107 dd->f_set_cntr_sample(ppd, QIB_CONG_TIMER_PSINTERVAL, 0x0);
2108done:
2109 spin_unlock_irqrestore(&ppd->ibport_data.lock, flags);
2110 mod_timer(&ppd->cong_stats.timer, jiffies + HZ);
2111}
2112
2113int qib_create_agents(struct qib_ibdev *dev)
2114{
2115 struct qib_devdata *dd = dd_from_dev(dev);
2116 struct ib_mad_agent *agent;
2117 struct qib_ibport *ibp;
2118 int p;
2119 int ret;
2120
2121 for (p = 0; p < dd->num_pports; p++) {
2122 ibp = &dd->pport[p].ibport_data;
2123 agent = ib_register_mad_agent(&dev->ibdev, p + 1, IB_QPT_SMI,
2124 NULL, 0, send_handler,
2125 NULL, NULL);
2126 if (IS_ERR(agent)) {
2127 ret = PTR_ERR(agent);
2128 goto err;
2129 }
2130
2131 /* Initialize xmit_wait structure */
2132 dd->pport[p].cong_stats.counter = 0;
2133 init_timer(&dd->pport[p].cong_stats.timer);
2134 dd->pport[p].cong_stats.timer.function = xmit_wait_timer_func;
2135 dd->pport[p].cong_stats.timer.data =
2136 (unsigned long)(&dd->pport[p]);
2137 dd->pport[p].cong_stats.timer.expires = 0;
2138 add_timer(&dd->pport[p].cong_stats.timer);
2139
2140 ibp->send_agent = agent;
2141 }
2142
2143 return 0;
2144
2145err:
2146 for (p = 0; p < dd->num_pports; p++) {
2147 ibp = &dd->pport[p].ibport_data;
2148 if (ibp->send_agent) {
2149 agent = ibp->send_agent;
2150 ibp->send_agent = NULL;
2151 ib_unregister_mad_agent(agent);
2152 }
2153 }
2154
2155 return ret;
2156}
2157
2158void qib_free_agents(struct qib_ibdev *dev)
2159{
2160 struct qib_devdata *dd = dd_from_dev(dev);
2161 struct ib_mad_agent *agent;
2162 struct qib_ibport *ibp;
2163 int p;
2164
2165 for (p = 0; p < dd->num_pports; p++) {
2166 ibp = &dd->pport[p].ibport_data;
2167 if (ibp->send_agent) {
2168 agent = ibp->send_agent;
2169 ibp->send_agent = NULL;
2170 ib_unregister_mad_agent(agent);
2171 }
2172 if (ibp->sm_ah) {
2173 ib_destroy_ah(&ibp->sm_ah->ibah);
2174 ibp->sm_ah = NULL;
2175 }
2176 if (dd->pport[p].cong_stats.timer.data)
2177 del_timer_sync(&dd->pport[p].cong_stats.timer);
2178 }
2179}