xref: /linux/drivers/infiniband/hw/qib/qib_mad.c (revision 2ba9268dd603d23e17643437b2246acb6844953b)
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.
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 
40 static 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 
52 static int reply_failure(struct ib_smp *smp)
53 {
54 	/*
55 	 * The verbs framework will handle the directed/LID route
56 	 * packet changes.
57 	 */
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;
62 }
63 
64 static void qib_send_trap(struct qib_ibport *ibp, void *data, unsigned len)
65 {
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;
72 
73 	agent = ibp->send_agent;
74 	if (!agent)
75 		return;
76 
77 	/* o14-3.2.1 */
78 	if (!(ppd_from_ibp(ibp)->lflags & QIBL_LINKACTIVE))
79 		return;
80 
81 	/* o14-2 */
82 	if (ibp->trap_timeout && time_before(jiffies, ibp->trap_timeout))
83 		return;
84 
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;
89 
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);
100 
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;
105 
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;
113 			}
114 		} else
115 			ret = -EINVAL;
116 	} else {
117 		send_buf->ah = &ibp->sm_ah->ibah;
118 		ret = 0;
119 	}
120 	spin_unlock_irqrestore(&ibp->lock, flags);
121 
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;
131 	}
132 }
133 
134 /*
135  * Send a bad [PQ]_Key trap (ch. 14.3.8).
136  */
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)
139 {
140 	struct ib_mad_notice_attr data;
141 
142 	if (trap_num == IB_NOTICE_TRAP_BAD_PKEY)
143 		ibp->pkey_violations++;
144 	else
145 		ibp->qkey_violations++;
146 	ibp->n_pkt_drops++;
147 
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);
161 
162 	qib_send_trap(ibp, &data, sizeof(data));
163 }
164 
165 /*
166  * Send a bad M_Key trap (ch. 14.3.9).
167  */
168 static void qib_bad_mkey(struct qib_ibport *ibp, struct ib_smp *smp)
169 {
170 	struct ib_mad_notice_attr data;
171 
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;
187 
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);
195 		}
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);
199 	}
200 
201 	qib_send_trap(ibp, &data, sizeof(data));
202 }
203 
204 /*
205  * Send a Port Capability Mask Changed trap (ch. 14.3.11).
206  */
207 void qib_cap_mask_chg(struct qib_ibport *ibp)
208 {
209 	struct ib_mad_notice_attr data;
210 
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);
220 
221 	qib_send_trap(ibp, &data, sizeof(data));
222 }
223 
224 /*
225  * Send a System Image GUID Changed trap (ch. 14.3.12).
226  */
227 void qib_sys_guid_chg(struct qib_ibport *ibp)
228 {
229 	struct ib_mad_notice_attr data;
230 
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;
240 
241 	qib_send_trap(ibp, &data, sizeof(data));
242 }
243 
244 /*
245  * Send a Node Description Changed trap (ch. 14.3.13).
246  */
247 void qib_node_desc_chg(struct qib_ibport *ibp)
248 {
249 	struct ib_mad_notice_attr data;
250 
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;
261 
262 	qib_send_trap(ibp, &data, sizeof(data));
263 }
264 
265 static int subn_get_nodedescription(struct ib_smp *smp,
266 				    struct ib_device *ibdev)
267 {
268 	if (smp->attr_mod)
269 		smp->status |= IB_SMP_INVALID_FIELD;
270 
271 	memcpy(smp->data, ibdev->node_desc, sizeof(smp->data));
272 
273 	return reply(smp);
274 }
275 
276 static int subn_get_nodeinfo(struct ib_smp *smp, struct ib_device *ibdev,
277 			     u8 port)
278 {
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 */
283 
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;
290 
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;
308 
309 	return reply(smp);
310 }
311 
312 static int subn_get_guidinfo(struct ib_smp *smp, struct ib_device *ibdev,
313 			     u8 port)
314 {
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 */
319 
320 	/* 32 blocks of 8 64-bit GUIDs per block */
321 
322 	memset(smp->data, 0, sizeof(smp->data));
323 
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;
329 
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];
338 		}
339 	} else
340 		smp->status |= IB_SMP_INVALID_FIELD;
341 
342 	return reply(smp);
343 }
344 
345 static void set_link_width_enabled(struct qib_pportdata *ppd, u32 w)
346 {
347 	(void) ppd->dd->f_set_ib_cfg(ppd, QIB_IB_CFG_LWID_ENB, w);
348 }
349 
350 static void set_link_speed_enabled(struct qib_pportdata *ppd, u32 s)
351 {
352 	(void) ppd->dd->f_set_ib_cfg(ppd, QIB_IB_CFG_SPD_ENB, s);
353 }
354 
355 static int get_overrunthreshold(struct qib_pportdata *ppd)
356 {
357 	return ppd->dd->f_get_ib_cfg(ppd, QIB_IB_CFG_OVERRUN_THRESH);
358 }
359 
360 /**
361  * set_overrunthreshold - set the overrun threshold
362  * @ppd: the physical port data
363  * @n: the new threshold
364  *
365  * Note that this will only take effect when the link state changes.
366  */
367 static int set_overrunthreshold(struct qib_pportdata *ppd, unsigned n)
368 {
369 	(void) ppd->dd->f_set_ib_cfg(ppd, QIB_IB_CFG_OVERRUN_THRESH,
370 					 (u32)n);
371 	return 0;
372 }
373 
374 static int get_phyerrthreshold(struct qib_pportdata *ppd)
375 {
376 	return ppd->dd->f_get_ib_cfg(ppd, QIB_IB_CFG_PHYERR_THRESH);
377 }
378 
379 /**
380  * set_phyerrthreshold - set the physical error threshold
381  * @ppd: the physical port data
382  * @n: the new threshold
383  *
384  * Note that this will only take effect when the link state changes.
385  */
386 static int set_phyerrthreshold(struct qib_pportdata *ppd, unsigned n)
387 {
388 	(void) ppd->dd->f_set_ib_cfg(ppd, QIB_IB_CFG_PHYERR_THRESH,
389 					 (u32)n);
390 	return 0;
391 }
392 
393 /**
394  * get_linkdowndefaultstate - get the default linkdown state
395  * @ppd: the physical port data
396  *
397  * Returns zero if the default is POLL, 1 if the default is SLEEP.
398  */
399 static int get_linkdowndefaultstate(struct qib_pportdata *ppd)
400 {
401 	return ppd->dd->f_get_ib_cfg(ppd, QIB_IB_CFG_LINKDEFAULT) ==
402 		IB_LINKINITCMD_SLEEP;
403 }
404 
405 static int check_mkey(struct qib_ibport *ibp, struct ib_smp *smp, int mad_flags)
406 {
407 	int valid_mkey = 0;
408 	int ret = 0;
409 
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;
416 	}
417 
418 	if ((mad_flags & IB_MAD_IGNORE_MKEY) ||  ibp->mkey == 0 ||
419 	    ibp->mkey == smp->mkey)
420 		valid_mkey = 1;
421 
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;
428 
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;
445 		}
446 	}
447 
448 	return ret;
449 }
450 
451 static int subn_get_portinfo(struct ib_smp *smp, struct ib_device *ibdev,
452 			     u8 port)
453 {
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);
462 
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;
470 		}
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;
477 			}
478 		}
479 	}
480 
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;
485 
486 	/* Clear all fields.  Only set the non-zero fields. */
487 	memset(smp->data, 0, sizeof(smp->data));
488 
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;
506 
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;
530 	}
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;
559 
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;
564 	}
565 
566 	ret = reply(smp);
567 
568 bail:
569 	return ret;
570 }
571 
572 /**
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
577  */
578 static int get_pkeys(struct qib_devdata *dd, u8 port, u16 *pkeys)
579 {
580 	struct qib_pportdata *ppd = dd->pport + port - 1;
581 	/*
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.
585 	 */
586 	struct qib_ctxtdata *rcd = dd->rcd[ppd->hw_pidx];
587 
588 	memcpy(pkeys, rcd->pkeys, sizeof(rcd->pkeys));
589 
590 	return 0;
591 }
592 
593 static int subn_get_pkeytable(struct ib_smp *smp, struct ib_device *ibdev,
594 			      u8 port)
595 {
596 	u32 startpx = 32 * (be32_to_cpu(smp->attr_mod) & 0xffff);
597 	u16 *p = (u16 *) smp->data;
598 	__be16 *q = (__be16 *) smp->data;
599 
600 	/* 64 blocks of 32 16-bit P_Key entries */
601 
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);
606 
607 		get_pkeys(dd, port, p);
608 
609 		for (i = 0; i < n; i++)
610 			q[i] = cpu_to_be16(p[i]);
611 	} else
612 		smp->status |= IB_SMP_INVALID_FIELD;
613 
614 	return reply(smp);
615 }
616 
617 static int subn_set_guidinfo(struct ib_smp *smp, struct ib_device *ibdev,
618 			     u8 port)
619 {
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 */
624 
625 	/* 32 blocks of 8 64-bit GUIDs per block */
626 
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;
631 
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;
637 
638 	/* The only GUID we support is the first read-only entry. */
639 	return subn_get_guidinfo(smp, ibdev, port);
640 }
641 
642 /**
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
647  *
648  * Set Portinfo (see ch. 14.2.5.6).
649  */
650 static int subn_set_portinfo(struct ib_smp *smp, struct ib_device *ibdev,
651 			     u8 port)
652 {
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);
669 
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;
678 	}
679 
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;
686 
687 	ibp->mkey = pip->mkey;
688 	ibp->gid_prefix = pip->gid_prefix;
689 	ibp->mkey_lease_period = be16_to_cpu(pip->mkey_lease_period);
690 
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);
703 	}
704 
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;
717 		}
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);
725 	}
726 
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);
736 	}
737 
738 	lse = pip->linkspeedactive_enabled & 0xF;
739 	if (lse) {
740 		/*
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.
744 		 */
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);
752 	}
753 
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;
768 	}
769 
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);
774 
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);
780 
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);
788 	}
789 
790 	if (pip->mkey_violations == 0)
791 		ibp->mkey_violations = 0;
792 
793 	if (pip->pkey_violations == 0)
794 		ibp->pkey_violations = 0;
795 
796 	if (pip->qkey_violations == 0)
797 		ibp->qkey_violations = 0;
798 
799 	ore = pip->localphyerrors_overrunerrors;
800 	if (set_phyerrthreshold(ppd, (ore >> 4) & 0xF))
801 		smp->status |= IB_SMP_INVALID_FIELD;
802 
803 	if (set_overrunthreshold(ppd, (ore & 0xF)))
804 		smp->status |= IB_SMP_INVALID_FIELD;
805 
806 	ibp->subnet_timeout = pip->clientrereg_resv_subnetto & 0x1F;
807 
808 	/*
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.
813 	 */
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;
818 
819 	/*
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).
822 	 */
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;
840 		}
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);
845 		/*
846 		 * Don't send a reply if the response would be sent
847 		 * through the disabled port.
848 		 */
849 		if (lstate == QIB_IB_LINKDOWN_DISABLE && smp->hop_cnt) {
850 			ret = IB_MAD_RESULT_SUCCESS | IB_MAD_RESULT_CONSUMED;
851 			goto done;
852 		}
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;
863 	}
864 
865 	if (clientrereg) {
866 		event.event = IB_EVENT_CLIENT_REREGISTER;
867 		ib_dispatch_event(&event);
868 	}
869 
870 	ret = subn_get_portinfo(smp, ibdev, port);
871 
872 	/* restore re-reg bit per o14-12.2.1 */
873 	pip->clientrereg_resv_subnetto |= clientrereg;
874 
875 	goto get_only;
876 
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;
883 }
884 
885 /**
886  * rm_pkey - decrecment the reference count for the given PKEY
887  * @dd: the qlogic_ib device
888  * @key: the PKEY index
889  *
890  * Return true if this was the last reference and the hardware table entry
891  * needs to be changed.
892  */
893 static int rm_pkey(struct qib_pportdata *ppd, u16 key)
894 {
895 	int i;
896 	int ret;
897 
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;
905 		}
906 		break;
907 	}
908 
909 	ret = 0;
910 
911 bail:
912 	return ret;
913 }
914 
915 /**
916  * add_pkey - add the given PKEY to the hardware table
917  * @dd: the qlogic_ib device
918  * @key: the PKEY
919  *
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.
922  */
923 static int add_pkey(struct qib_pportdata *ppd, u16 key)
924 {
925 	int i;
926 	u16 lkey = key & 0x7FFF;
927 	int any = 0;
928 	int ret;
929 
930 	if (lkey == 0x7FFF) {
931 		ret = 0;
932 		goto bail;
933 	}
934 
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;
940 		}
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;
946 			}
947 			/* Lost the race. Look for an empty slot below. */
948 			atomic_dec(&ppd->pkeyrefs[i]);
949 			any++;
950 		}
951 		/*
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.
955 		 */
956 		if ((ppd->pkeys[i] & 0x7FFF) == lkey) {
957 			ret = -EEXIST;
958 			goto bail;
959 		}
960 	}
961 	if (!any) {
962 		ret = -EBUSY;
963 		goto bail;
964 	}
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;
972 		}
973 	}
974 	ret = -EBUSY;
975 
976 bail:
977 	return ret;
978 }
979 
980 /**
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
985  */
986 static int set_pkeys(struct qib_devdata *dd, u8 port, u16 *pkeys)
987 {
988 	struct qib_pportdata *ppd;
989 	struct qib_ctxtdata *rcd;
990 	int i;
991 	int changed = 0;
992 
993 	/*
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.
998 	 */
999 	ppd = dd->pport + (port - 1);
1000 	rcd = dd->rcd[ppd->hw_pidx];
1001 
1002 	for (i = 0; i < ARRAY_SIZE(rcd->pkeys); i++) {
1003 		u16 key = pkeys[i];
1004 		u16 okey = rcd->pkeys[i];
1005 
1006 		if (key == okey)
1007 			continue;
1008 		/*
1009 		 * The value of this PKEY table entry is changing.
1010 		 * Remove the old entry in the hardware's array of PKEYs.
1011 		 */
1012 		if (okey & 0x7FFF)
1013 			changed |= rm_pkey(ppd, okey);
1014 		if (key & 0x7FFF) {
1015 			int ret = add_pkey(ppd, key);
1016 
1017 			if (ret < 0)
1018 				key = 0;
1019 			else
1020 				changed |= ret;
1021 		}
1022 		rcd->pkeys[i] = key;
1023 	}
1024 	if (changed) {
1025 		struct ib_event event;
1026 
1027 		(void) dd->f_set_ib_cfg(ppd, QIB_IB_CFG_PKEYS, 0);
1028 
1029 		event.event = IB_EVENT_PKEY_CHANGE;
1030 		event.device = &dd->verbs_dev.ibdev;
1031 		event.element.port_num = port;
1032 		ib_dispatch_event(&event);
1033 	}
1034 	return 0;
1035 }
1036 
1037 static int subn_set_pkeytable(struct ib_smp *smp, struct ib_device *ibdev,
1038 			      u8 port)
1039 {
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);
1045 
1046 	for (i = 0; i < n; i++)
1047 		q[i] = be16_to_cpu(p[i]);
1048 
1049 	if (startpx != 0 || set_pkeys(dd, port, q) != 0)
1050 		smp->status |= IB_SMP_INVALID_FIELD;
1051 
1052 	return subn_get_pkeytable(smp, ibdev, port);
1053 }
1054 
1055 static int subn_get_sl_to_vl(struct ib_smp *smp, struct ib_device *ibdev,
1056 			     u8 port)
1057 {
1058 	struct qib_ibport *ibp = to_iport(ibdev, port);
1059 	u8 *p = (u8 *) smp->data;
1060 	unsigned i;
1061 
1062 	memset(smp->data, 0, sizeof(smp->data));
1063 
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];
1069 
1070 	return reply(smp);
1071 }
1072 
1073 static int subn_set_sl_to_vl(struct ib_smp *smp, struct ib_device *ibdev,
1074 			     u8 port)
1075 {
1076 	struct qib_ibport *ibp = to_iport(ibdev, port);
1077 	u8 *p = (u8 *) smp->data;
1078 	unsigned i;
1079 
1080 	if (!(ibp->port_cap_flags & IB_PORT_SL_MAP_SUP)) {
1081 		smp->status |= IB_SMP_UNSUP_METHOD;
1082 		return reply(smp);
1083 	}
1084 
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;
1088 	}
1089 	qib_set_uevent_bits(ppd_from_ibp(to_iport(ibdev, port)),
1090 			    _QIB_EVENT_SL2VL_CHANGE_BIT);
1091 
1092 	return subn_get_sl_to_vl(smp, ibdev, port);
1093 }
1094 
1095 static int subn_get_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 	memset(smp->data, 0, sizeof(smp->data));
1102 
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;
1113 
1114 	return reply(smp);
1115 }
1116 
1117 static int subn_set_vl_arb(struct ib_smp *smp, struct ib_device *ibdev,
1118 			   u8 port)
1119 {
1120 	unsigned which = be32_to_cpu(smp->attr_mod) >> 16;
1121 	struct qib_pportdata *ppd = ppd_from_ibp(to_iport(ibdev, port));
1122 
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;
1133 
1134 	return subn_get_vl_arb(smp, ibdev, port);
1135 }
1136 
1137 static int subn_trap_repress(struct ib_smp *smp, struct ib_device *ibdev,
1138 			     u8 port)
1139 {
1140 	/*
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.
1145 	 */
1146 	return IB_MAD_RESULT_SUCCESS | IB_MAD_RESULT_CONSUMED;
1147 }
1148 
1149 static int pma_get_classportinfo(struct ib_pma_mad *pmp,
1150 				 struct ib_device *ibdev)
1151 {
1152 	struct ib_class_port_info *p =
1153 		(struct ib_class_port_info *)pmp->data;
1154 	struct qib_devdata *dd = dd_from_ibdev(ibdev);
1155 
1156 	memset(pmp->data, 0, sizeof(pmp->data));
1157 
1158 	if (pmp->mad_hdr.attr_mod != 0)
1159 		pmp->mad_hdr.status |= IB_SMP_INVALID_FIELD;
1160 
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;
1165 	/*
1166 	 * Set the most significant bit of CM2 to indicate support for
1167 	 * congestion statistics
1168 	 */
1169 	p->reserved[0] = dd->psxmitwait_supported << 7;
1170 	/*
1171 	 * Expected response time is 4.096 usec. * 2^18 == 1.073741824 sec.
1172 	 */
1173 	p->resp_time_value = 18;
1174 
1175 	return reply((struct ib_smp *) pmp);
1176 }
1177 
1178 static int pma_get_portsamplescontrol(struct ib_pma_mad *pmp,
1179 				      struct ib_device *ibdev, u8 port)
1180 {
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;
1189 
1190 	memset(pmp->data, 0, sizeof(pmp->data));
1191 
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;
1196 	}
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);
1211 
1212 bail:
1213 	return reply((struct ib_smp *) pmp);
1214 }
1215 
1216 static int pma_set_portsamplescontrol(struct ib_pma_mad *pmp,
1217 				      struct ib_device *ibdev, u8 port)
1218 {
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;
1228 
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;
1233 	}
1234 
1235 	spin_lock_irqsave(&ibp->lock, flags);
1236 
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);
1254 	}
1255 	spin_unlock_irqrestore(&ibp->lock, flags);
1256 
1257 	ret = pma_get_portsamplescontrol(pmp, ibdev, port);
1258 
1259 bail:
1260 	return ret;
1261 }
1262 
1263 static u64 get_counter(struct qib_ibport *ibp, struct qib_pportdata *ppd,
1264 		       __be16 sel)
1265 {
1266 	u64 ret;
1267 
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;
1286 	}
1287 
1288 	return ret;
1289 }
1290 
1291 /* This function assumes that the xmit_wait lock is already held */
1292 static u64 xmit_wait_get_value_delta(struct qib_pportdata *ppd)
1293 {
1294 	u32 delta;
1295 
1296 	delta = get_counter(&ppd->ibport_data, ppd,
1297 			    IB_PMA_PORT_XMIT_WAIT);
1298 	return ppd->cong_stats.counter + delta;
1299 }
1300 
1301 static void cache_hw_sample_counters(struct qib_pportdata *ppd)
1302 {
1303 	struct qib_ibport *ibp = &ppd->ibport_data;
1304 
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);
1315 }
1316 
1317 static u64 get_cache_hw_sample_counters(struct qib_pportdata *ppd,
1318 					__be16 sel)
1319 {
1320 	u64 ret;
1321 
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;
1340 	}
1341 
1342 	return ret;
1343 }
1344 
1345 static int pma_get_portsamplesresult(struct ib_pma_mad *pmp,
1346 				     struct ib_device *ibdev, u8 port)
1347 {
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;
1357 
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;
1373 		}
1374 	}
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);
1380 
1381 	return reply((struct ib_smp *) pmp);
1382 }
1383 
1384 static int pma_get_portsamplesresult_ext(struct ib_pma_mad *pmp,
1385 					 struct ib_device *ibdev, u8 port)
1386 {
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;
1396 
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;
1415 		}
1416 	}
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);
1422 
1423 	return reply((struct ib_smp *) pmp);
1424 }
1425 
1426 static int pma_get_portcounters(struct ib_pma_mad *pmp,
1427 				struct ib_device *ibdev, u8 port)
1428 {
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;
1435 
1436 	qib_get_counters(ppd, &cntrs);
1437 
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;
1456 
1457 	memset(pmp->data, 0, sizeof(pmp->data));
1458 
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;
1462 
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);
1520 
1521 	return reply((struct ib_smp *) pmp);
1522 }
1523 
1524 static int pma_get_portcounters_cong(struct ib_pma_mad *pmp,
1525 				     struct ib_device *ibdev, u8 port)
1526 {
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;
1537 
1538 	/*
1539 	 * This check is performed only in the GET method because the
1540 	 * SET method ends up calling this anyway.
1541 	 */
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;
1546 
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);
1551 
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;
1571 
1572 	memset(pmp->reserved, 0, sizeof(pmp->reserved) +
1573 	       sizeof(pmp->data));
1574 
1575 	/*
1576 	 * Set top 3 bits to indicate interval in picoseconds in
1577 	 * remaining bits.
1578 	 */
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);
1633 
1634 	return reply((struct ib_smp *)pmp);
1635 }
1636 
1637 static void qib_snapshot_pmacounters(
1638 	struct qib_ibport *ibp,
1639 	struct qib_pma_counters *pmacounters)
1640 {
1641 	struct qib_pma_counters *p;
1642 	int cpu;
1643 
1644 	memset(pmacounters, 0, sizeof(*pmacounters));
1645 	for_each_possible_cpu(cpu) {
1646 		p = per_cpu_ptr(ibp->pmastats, cpu);
1647 		pmacounters->n_unicast_xmit += p->n_unicast_xmit;
1648 		pmacounters->n_unicast_rcv += p->n_unicast_rcv;
1649 		pmacounters->n_multicast_xmit += p->n_multicast_xmit;
1650 		pmacounters->n_multicast_rcv += p->n_multicast_rcv;
1651 	}
1652 }
1653 
1654 static int pma_get_portcounters_ext(struct ib_pma_mad *pmp,
1655 				    struct ib_device *ibdev, u8 port)
1656 {
1657 	struct ib_pma_portcounters_ext *p =
1658 		(struct ib_pma_portcounters_ext *)pmp->data;
1659 	struct qib_ibport *ibp = to_iport(ibdev, port);
1660 	struct qib_pportdata *ppd = ppd_from_ibp(ibp);
1661 	u64 swords, rwords, spkts, rpkts, xwait;
1662 	struct qib_pma_counters pma;
1663 	u8 port_select = p->port_select;
1664 
1665 	memset(pmp->data, 0, sizeof(pmp->data));
1666 
1667 	p->port_select = port_select;
1668 	if (pmp->mad_hdr.attr_mod != 0 || port_select != port) {
1669 		pmp->mad_hdr.status |= IB_SMP_INVALID_FIELD;
1670 		goto bail;
1671 	}
1672 
1673 	qib_snapshot_counters(ppd, &swords, &rwords, &spkts, &rpkts, &xwait);
1674 
1675 	/* Adjust counters for any resets done. */
1676 	swords -= ibp->z_port_xmit_data;
1677 	rwords -= ibp->z_port_rcv_data;
1678 	spkts -= ibp->z_port_xmit_packets;
1679 	rpkts -= ibp->z_port_rcv_packets;
1680 
1681 	p->port_xmit_data = cpu_to_be64(swords);
1682 	p->port_rcv_data = cpu_to_be64(rwords);
1683 	p->port_xmit_packets = cpu_to_be64(spkts);
1684 	p->port_rcv_packets = cpu_to_be64(rpkts);
1685 
1686 	qib_snapshot_pmacounters(ibp, &pma);
1687 
1688 	p->port_unicast_xmit_packets = cpu_to_be64(pma.n_unicast_xmit
1689 		- ibp->z_unicast_xmit);
1690 	p->port_unicast_rcv_packets = cpu_to_be64(pma.n_unicast_rcv
1691 		- ibp->z_unicast_rcv);
1692 	p->port_multicast_xmit_packets = cpu_to_be64(pma.n_multicast_xmit
1693 		- ibp->z_multicast_xmit);
1694 	p->port_multicast_rcv_packets = cpu_to_be64(pma.n_multicast_rcv
1695 		- ibp->z_multicast_rcv);
1696 
1697 bail:
1698 	return reply((struct ib_smp *) pmp);
1699 }
1700 
1701 static int pma_set_portcounters(struct ib_pma_mad *pmp,
1702 				struct ib_device *ibdev, u8 port)
1703 {
1704 	struct ib_pma_portcounters *p = (struct ib_pma_portcounters *)
1705 		pmp->data;
1706 	struct qib_ibport *ibp = to_iport(ibdev, port);
1707 	struct qib_pportdata *ppd = ppd_from_ibp(ibp);
1708 	struct qib_verbs_counters cntrs;
1709 
1710 	/*
1711 	 * Since the HW doesn't support clearing counters, we save the
1712 	 * current count and subtract it from future responses.
1713 	 */
1714 	qib_get_counters(ppd, &cntrs);
1715 
1716 	if (p->counter_select & IB_PMA_SEL_SYMBOL_ERROR)
1717 		ibp->z_symbol_error_counter = cntrs.symbol_error_counter;
1718 
1719 	if (p->counter_select & IB_PMA_SEL_LINK_ERROR_RECOVERY)
1720 		ibp->z_link_error_recovery_counter =
1721 			cntrs.link_error_recovery_counter;
1722 
1723 	if (p->counter_select & IB_PMA_SEL_LINK_DOWNED)
1724 		ibp->z_link_downed_counter = cntrs.link_downed_counter;
1725 
1726 	if (p->counter_select & IB_PMA_SEL_PORT_RCV_ERRORS)
1727 		ibp->z_port_rcv_errors = cntrs.port_rcv_errors;
1728 
1729 	if (p->counter_select & IB_PMA_SEL_PORT_RCV_REMPHYS_ERRORS)
1730 		ibp->z_port_rcv_remphys_errors =
1731 			cntrs.port_rcv_remphys_errors;
1732 
1733 	if (p->counter_select & IB_PMA_SEL_PORT_XMIT_DISCARDS)
1734 		ibp->z_port_xmit_discards = cntrs.port_xmit_discards;
1735 
1736 	if (p->counter_select & IB_PMA_SEL_LOCAL_LINK_INTEGRITY_ERRORS)
1737 		ibp->z_local_link_integrity_errors =
1738 			cntrs.local_link_integrity_errors;
1739 
1740 	if (p->counter_select & IB_PMA_SEL_EXCESSIVE_BUFFER_OVERRUNS)
1741 		ibp->z_excessive_buffer_overrun_errors =
1742 			cntrs.excessive_buffer_overrun_errors;
1743 
1744 	if (p->counter_select & IB_PMA_SEL_PORT_VL15_DROPPED) {
1745 		ibp->n_vl15_dropped = 0;
1746 		ibp->z_vl15_dropped = cntrs.vl15_dropped;
1747 	}
1748 
1749 	if (p->counter_select & IB_PMA_SEL_PORT_XMIT_DATA)
1750 		ibp->z_port_xmit_data = cntrs.port_xmit_data;
1751 
1752 	if (p->counter_select & IB_PMA_SEL_PORT_RCV_DATA)
1753 		ibp->z_port_rcv_data = cntrs.port_rcv_data;
1754 
1755 	if (p->counter_select & IB_PMA_SEL_PORT_XMIT_PACKETS)
1756 		ibp->z_port_xmit_packets = cntrs.port_xmit_packets;
1757 
1758 	if (p->counter_select & IB_PMA_SEL_PORT_RCV_PACKETS)
1759 		ibp->z_port_rcv_packets = cntrs.port_rcv_packets;
1760 
1761 	return pma_get_portcounters(pmp, ibdev, port);
1762 }
1763 
1764 static int pma_set_portcounters_cong(struct ib_pma_mad *pmp,
1765 				     struct ib_device *ibdev, u8 port)
1766 {
1767 	struct qib_ibport *ibp = to_iport(ibdev, port);
1768 	struct qib_pportdata *ppd = ppd_from_ibp(ibp);
1769 	struct qib_devdata *dd = dd_from_ppd(ppd);
1770 	struct qib_verbs_counters cntrs;
1771 	u32 counter_select = (be32_to_cpu(pmp->mad_hdr.attr_mod) >> 24) & 0xFF;
1772 	int ret = 0;
1773 	unsigned long flags;
1774 
1775 	qib_get_counters(ppd, &cntrs);
1776 	/* Get counter values before we save them */
1777 	ret = pma_get_portcounters_cong(pmp, ibdev, port);
1778 
1779 	if (counter_select & IB_PMA_SEL_CONG_XMIT) {
1780 		spin_lock_irqsave(&ppd->ibport_data.lock, flags);
1781 		ppd->cong_stats.counter = 0;
1782 		dd->f_set_cntr_sample(ppd, QIB_CONG_TIMER_PSINTERVAL,
1783 				      0x0);
1784 		spin_unlock_irqrestore(&ppd->ibport_data.lock, flags);
1785 	}
1786 	if (counter_select & IB_PMA_SEL_CONG_PORT_DATA) {
1787 		ibp->z_port_xmit_data = cntrs.port_xmit_data;
1788 		ibp->z_port_rcv_data = cntrs.port_rcv_data;
1789 		ibp->z_port_xmit_packets = cntrs.port_xmit_packets;
1790 		ibp->z_port_rcv_packets = cntrs.port_rcv_packets;
1791 	}
1792 	if (counter_select & IB_PMA_SEL_CONG_ALL) {
1793 		ibp->z_symbol_error_counter =
1794 			cntrs.symbol_error_counter;
1795 		ibp->z_link_error_recovery_counter =
1796 			cntrs.link_error_recovery_counter;
1797 		ibp->z_link_downed_counter =
1798 			cntrs.link_downed_counter;
1799 		ibp->z_port_rcv_errors = cntrs.port_rcv_errors;
1800 		ibp->z_port_rcv_remphys_errors =
1801 			cntrs.port_rcv_remphys_errors;
1802 		ibp->z_port_xmit_discards =
1803 			cntrs.port_xmit_discards;
1804 		ibp->z_local_link_integrity_errors =
1805 			cntrs.local_link_integrity_errors;
1806 		ibp->z_excessive_buffer_overrun_errors =
1807 			cntrs.excessive_buffer_overrun_errors;
1808 		ibp->n_vl15_dropped = 0;
1809 		ibp->z_vl15_dropped = cntrs.vl15_dropped;
1810 	}
1811 
1812 	return ret;
1813 }
1814 
1815 static int pma_set_portcounters_ext(struct ib_pma_mad *pmp,
1816 				    struct ib_device *ibdev, u8 port)
1817 {
1818 	struct ib_pma_portcounters *p = (struct ib_pma_portcounters *)
1819 		pmp->data;
1820 	struct qib_ibport *ibp = to_iport(ibdev, port);
1821 	struct qib_pportdata *ppd = ppd_from_ibp(ibp);
1822 	u64 swords, rwords, spkts, rpkts, xwait;
1823 	struct qib_pma_counters pma;
1824 
1825 	qib_snapshot_counters(ppd, &swords, &rwords, &spkts, &rpkts, &xwait);
1826 
1827 	if (p->counter_select & IB_PMA_SELX_PORT_XMIT_DATA)
1828 		ibp->z_port_xmit_data = swords;
1829 
1830 	if (p->counter_select & IB_PMA_SELX_PORT_RCV_DATA)
1831 		ibp->z_port_rcv_data = rwords;
1832 
1833 	if (p->counter_select & IB_PMA_SELX_PORT_XMIT_PACKETS)
1834 		ibp->z_port_xmit_packets = spkts;
1835 
1836 	if (p->counter_select & IB_PMA_SELX_PORT_RCV_PACKETS)
1837 		ibp->z_port_rcv_packets = rpkts;
1838 
1839 	qib_snapshot_pmacounters(ibp, &pma);
1840 
1841 	if (p->counter_select & IB_PMA_SELX_PORT_UNI_XMIT_PACKETS)
1842 		ibp->z_unicast_xmit = pma.n_unicast_xmit;
1843 
1844 	if (p->counter_select & IB_PMA_SELX_PORT_UNI_RCV_PACKETS)
1845 		ibp->z_unicast_rcv = pma.n_unicast_rcv;
1846 
1847 	if (p->counter_select & IB_PMA_SELX_PORT_MULTI_XMIT_PACKETS)
1848 		ibp->z_multicast_xmit = pma.n_multicast_xmit;
1849 
1850 	if (p->counter_select & IB_PMA_SELX_PORT_MULTI_RCV_PACKETS)
1851 		ibp->z_multicast_rcv = pma.n_multicast_rcv;
1852 
1853 	return pma_get_portcounters_ext(pmp, ibdev, port);
1854 }
1855 
1856 static int process_subn(struct ib_device *ibdev, int mad_flags,
1857 			u8 port, struct ib_mad *in_mad,
1858 			struct ib_mad *out_mad)
1859 {
1860 	struct ib_smp *smp = (struct ib_smp *)out_mad;
1861 	struct qib_ibport *ibp = to_iport(ibdev, port);
1862 	struct qib_pportdata *ppd = ppd_from_ibp(ibp);
1863 	int ret;
1864 
1865 	*out_mad = *in_mad;
1866 	if (smp->class_version != 1) {
1867 		smp->status |= IB_SMP_UNSUP_VERSION;
1868 		ret = reply(smp);
1869 		goto bail;
1870 	}
1871 
1872 	ret = check_mkey(ibp, smp, mad_flags);
1873 	if (ret) {
1874 		u32 port_num = be32_to_cpu(smp->attr_mod);
1875 
1876 		/*
1877 		 * If this is a get/set portinfo, we already check the
1878 		 * M_Key if the MAD is for another port and the M_Key
1879 		 * is OK on the receiving port. This check is needed
1880 		 * to increment the error counters when the M_Key
1881 		 * fails to match on *both* ports.
1882 		 */
1883 		if (in_mad->mad_hdr.attr_id == IB_SMP_ATTR_PORT_INFO &&
1884 		    (smp->method == IB_MGMT_METHOD_GET ||
1885 		     smp->method == IB_MGMT_METHOD_SET) &&
1886 		    port_num && port_num <= ibdev->phys_port_cnt &&
1887 		    port != port_num)
1888 			(void) check_mkey(to_iport(ibdev, port_num), smp, 0);
1889 		ret = IB_MAD_RESULT_FAILURE;
1890 		goto bail;
1891 	}
1892 
1893 	switch (smp->method) {
1894 	case IB_MGMT_METHOD_GET:
1895 		switch (smp->attr_id) {
1896 		case IB_SMP_ATTR_NODE_DESC:
1897 			ret = subn_get_nodedescription(smp, ibdev);
1898 			goto bail;
1899 		case IB_SMP_ATTR_NODE_INFO:
1900 			ret = subn_get_nodeinfo(smp, ibdev, port);
1901 			goto bail;
1902 		case IB_SMP_ATTR_GUID_INFO:
1903 			ret = subn_get_guidinfo(smp, ibdev, port);
1904 			goto bail;
1905 		case IB_SMP_ATTR_PORT_INFO:
1906 			ret = subn_get_portinfo(smp, ibdev, port);
1907 			goto bail;
1908 		case IB_SMP_ATTR_PKEY_TABLE:
1909 			ret = subn_get_pkeytable(smp, ibdev, port);
1910 			goto bail;
1911 		case IB_SMP_ATTR_SL_TO_VL_TABLE:
1912 			ret = subn_get_sl_to_vl(smp, ibdev, port);
1913 			goto bail;
1914 		case IB_SMP_ATTR_VL_ARB_TABLE:
1915 			ret = subn_get_vl_arb(smp, ibdev, port);
1916 			goto bail;
1917 		case IB_SMP_ATTR_SM_INFO:
1918 			if (ibp->port_cap_flags & IB_PORT_SM_DISABLED) {
1919 				ret = IB_MAD_RESULT_SUCCESS |
1920 					IB_MAD_RESULT_CONSUMED;
1921 				goto bail;
1922 			}
1923 			if (ibp->port_cap_flags & IB_PORT_SM) {
1924 				ret = IB_MAD_RESULT_SUCCESS;
1925 				goto bail;
1926 			}
1927 			/* FALLTHROUGH */
1928 		default:
1929 			smp->status |= IB_SMP_UNSUP_METH_ATTR;
1930 			ret = reply(smp);
1931 			goto bail;
1932 		}
1933 
1934 	case IB_MGMT_METHOD_SET:
1935 		switch (smp->attr_id) {
1936 		case IB_SMP_ATTR_GUID_INFO:
1937 			ret = subn_set_guidinfo(smp, ibdev, port);
1938 			goto bail;
1939 		case IB_SMP_ATTR_PORT_INFO:
1940 			ret = subn_set_portinfo(smp, ibdev, port);
1941 			goto bail;
1942 		case IB_SMP_ATTR_PKEY_TABLE:
1943 			ret = subn_set_pkeytable(smp, ibdev, port);
1944 			goto bail;
1945 		case IB_SMP_ATTR_SL_TO_VL_TABLE:
1946 			ret = subn_set_sl_to_vl(smp, ibdev, port);
1947 			goto bail;
1948 		case IB_SMP_ATTR_VL_ARB_TABLE:
1949 			ret = subn_set_vl_arb(smp, ibdev, port);
1950 			goto bail;
1951 		case IB_SMP_ATTR_SM_INFO:
1952 			if (ibp->port_cap_flags & IB_PORT_SM_DISABLED) {
1953 				ret = IB_MAD_RESULT_SUCCESS |
1954 					IB_MAD_RESULT_CONSUMED;
1955 				goto bail;
1956 			}
1957 			if (ibp->port_cap_flags & IB_PORT_SM) {
1958 				ret = IB_MAD_RESULT_SUCCESS;
1959 				goto bail;
1960 			}
1961 			/* FALLTHROUGH */
1962 		default:
1963 			smp->status |= IB_SMP_UNSUP_METH_ATTR;
1964 			ret = reply(smp);
1965 			goto bail;
1966 		}
1967 
1968 	case IB_MGMT_METHOD_TRAP_REPRESS:
1969 		if (smp->attr_id == IB_SMP_ATTR_NOTICE)
1970 			ret = subn_trap_repress(smp, ibdev, port);
1971 		else {
1972 			smp->status |= IB_SMP_UNSUP_METH_ATTR;
1973 			ret = reply(smp);
1974 		}
1975 		goto bail;
1976 
1977 	case IB_MGMT_METHOD_TRAP:
1978 	case IB_MGMT_METHOD_REPORT:
1979 	case IB_MGMT_METHOD_REPORT_RESP:
1980 	case IB_MGMT_METHOD_GET_RESP:
1981 		/*
1982 		 * The ib_mad module will call us to process responses
1983 		 * before checking for other consumers.
1984 		 * Just tell the caller to process it normally.
1985 		 */
1986 		ret = IB_MAD_RESULT_SUCCESS;
1987 		goto bail;
1988 
1989 	case IB_MGMT_METHOD_SEND:
1990 		if (ib_get_smp_direction(smp) &&
1991 		    smp->attr_id == QIB_VENDOR_IPG) {
1992 			ppd->dd->f_set_ib_cfg(ppd, QIB_IB_CFG_PORT,
1993 					      smp->data[0]);
1994 			ret = IB_MAD_RESULT_SUCCESS | IB_MAD_RESULT_CONSUMED;
1995 		} else
1996 			ret = IB_MAD_RESULT_SUCCESS;
1997 		goto bail;
1998 
1999 	default:
2000 		smp->status |= IB_SMP_UNSUP_METHOD;
2001 		ret = reply(smp);
2002 	}
2003 
2004 bail:
2005 	return ret;
2006 }
2007 
2008 static int process_perf(struct ib_device *ibdev, u8 port,
2009 			struct ib_mad *in_mad,
2010 			struct ib_mad *out_mad)
2011 {
2012 	struct ib_pma_mad *pmp = (struct ib_pma_mad *)out_mad;
2013 	int ret;
2014 
2015 	*out_mad = *in_mad;
2016 	if (pmp->mad_hdr.class_version != 1) {
2017 		pmp->mad_hdr.status |= IB_SMP_UNSUP_VERSION;
2018 		ret = reply((struct ib_smp *) pmp);
2019 		goto bail;
2020 	}
2021 
2022 	switch (pmp->mad_hdr.method) {
2023 	case IB_MGMT_METHOD_GET:
2024 		switch (pmp->mad_hdr.attr_id) {
2025 		case IB_PMA_CLASS_PORT_INFO:
2026 			ret = pma_get_classportinfo(pmp, ibdev);
2027 			goto bail;
2028 		case IB_PMA_PORT_SAMPLES_CONTROL:
2029 			ret = pma_get_portsamplescontrol(pmp, ibdev, port);
2030 			goto bail;
2031 		case IB_PMA_PORT_SAMPLES_RESULT:
2032 			ret = pma_get_portsamplesresult(pmp, ibdev, port);
2033 			goto bail;
2034 		case IB_PMA_PORT_SAMPLES_RESULT_EXT:
2035 			ret = pma_get_portsamplesresult_ext(pmp, ibdev, port);
2036 			goto bail;
2037 		case IB_PMA_PORT_COUNTERS:
2038 			ret = pma_get_portcounters(pmp, ibdev, port);
2039 			goto bail;
2040 		case IB_PMA_PORT_COUNTERS_EXT:
2041 			ret = pma_get_portcounters_ext(pmp, ibdev, port);
2042 			goto bail;
2043 		case IB_PMA_PORT_COUNTERS_CONG:
2044 			ret = pma_get_portcounters_cong(pmp, ibdev, port);
2045 			goto bail;
2046 		default:
2047 			pmp->mad_hdr.status |= IB_SMP_UNSUP_METH_ATTR;
2048 			ret = reply((struct ib_smp *) pmp);
2049 			goto bail;
2050 		}
2051 
2052 	case IB_MGMT_METHOD_SET:
2053 		switch (pmp->mad_hdr.attr_id) {
2054 		case IB_PMA_PORT_SAMPLES_CONTROL:
2055 			ret = pma_set_portsamplescontrol(pmp, ibdev, port);
2056 			goto bail;
2057 		case IB_PMA_PORT_COUNTERS:
2058 			ret = pma_set_portcounters(pmp, ibdev, port);
2059 			goto bail;
2060 		case IB_PMA_PORT_COUNTERS_EXT:
2061 			ret = pma_set_portcounters_ext(pmp, ibdev, port);
2062 			goto bail;
2063 		case IB_PMA_PORT_COUNTERS_CONG:
2064 			ret = pma_set_portcounters_cong(pmp, ibdev, port);
2065 			goto bail;
2066 		default:
2067 			pmp->mad_hdr.status |= IB_SMP_UNSUP_METH_ATTR;
2068 			ret = reply((struct ib_smp *) pmp);
2069 			goto bail;
2070 		}
2071 
2072 	case IB_MGMT_METHOD_TRAP:
2073 	case IB_MGMT_METHOD_GET_RESP:
2074 		/*
2075 		 * The ib_mad module will call us to process responses
2076 		 * before checking for other consumers.
2077 		 * Just tell the caller to process it normally.
2078 		 */
2079 		ret = IB_MAD_RESULT_SUCCESS;
2080 		goto bail;
2081 
2082 	default:
2083 		pmp->mad_hdr.status |= IB_SMP_UNSUP_METHOD;
2084 		ret = reply((struct ib_smp *) pmp);
2085 	}
2086 
2087 bail:
2088 	return ret;
2089 }
2090 
2091 static int cc_get_classportinfo(struct ib_cc_mad *ccp,
2092 				struct ib_device *ibdev)
2093 {
2094 	struct ib_cc_classportinfo_attr *p =
2095 		(struct ib_cc_classportinfo_attr *)ccp->mgmt_data;
2096 
2097 	memset(ccp->mgmt_data, 0, sizeof(ccp->mgmt_data));
2098 
2099 	p->base_version = 1;
2100 	p->class_version = 1;
2101 	p->cap_mask = 0;
2102 
2103 	/*
2104 	 * Expected response time is 4.096 usec. * 2^18 == 1.073741824 sec.
2105 	 */
2106 	p->resp_time_value = 18;
2107 
2108 	return reply((struct ib_smp *) ccp);
2109 }
2110 
2111 static int cc_get_congestion_info(struct ib_cc_mad *ccp,
2112 				struct ib_device *ibdev, u8 port)
2113 {
2114 	struct ib_cc_info_attr *p =
2115 		(struct ib_cc_info_attr *)ccp->mgmt_data;
2116 	struct qib_ibport *ibp = to_iport(ibdev, port);
2117 	struct qib_pportdata *ppd = ppd_from_ibp(ibp);
2118 
2119 	memset(ccp->mgmt_data, 0, sizeof(ccp->mgmt_data));
2120 
2121 	p->congestion_info = 0;
2122 	p->control_table_cap = ppd->cc_max_table_entries;
2123 
2124 	return reply((struct ib_smp *) ccp);
2125 }
2126 
2127 static int cc_get_congestion_setting(struct ib_cc_mad *ccp,
2128 				struct ib_device *ibdev, u8 port)
2129 {
2130 	int i;
2131 	struct ib_cc_congestion_setting_attr *p =
2132 		(struct ib_cc_congestion_setting_attr *)ccp->mgmt_data;
2133 	struct qib_ibport *ibp = to_iport(ibdev, port);
2134 	struct qib_pportdata *ppd = ppd_from_ibp(ibp);
2135 	struct ib_cc_congestion_entry_shadow *entries;
2136 
2137 	memset(ccp->mgmt_data, 0, sizeof(ccp->mgmt_data));
2138 
2139 	spin_lock(&ppd->cc_shadow_lock);
2140 
2141 	entries = ppd->congestion_entries_shadow->entries;
2142 	p->port_control = cpu_to_be16(
2143 		ppd->congestion_entries_shadow->port_control);
2144 	p->control_map = cpu_to_be16(
2145 		ppd->congestion_entries_shadow->control_map);
2146 	for (i = 0; i < IB_CC_CCS_ENTRIES; i++) {
2147 		p->entries[i].ccti_increase = entries[i].ccti_increase;
2148 		p->entries[i].ccti_timer = cpu_to_be16(entries[i].ccti_timer);
2149 		p->entries[i].trigger_threshold = entries[i].trigger_threshold;
2150 		p->entries[i].ccti_min = entries[i].ccti_min;
2151 	}
2152 
2153 	spin_unlock(&ppd->cc_shadow_lock);
2154 
2155 	return reply((struct ib_smp *) ccp);
2156 }
2157 
2158 static int cc_get_congestion_control_table(struct ib_cc_mad *ccp,
2159 				struct ib_device *ibdev, u8 port)
2160 {
2161 	struct ib_cc_table_attr *p =
2162 		(struct ib_cc_table_attr *)ccp->mgmt_data;
2163 	struct qib_ibport *ibp = to_iport(ibdev, port);
2164 	struct qib_pportdata *ppd = ppd_from_ibp(ibp);
2165 	u32 cct_block_index = be32_to_cpu(ccp->attr_mod);
2166 	u32 max_cct_block;
2167 	u32 cct_entry;
2168 	struct ib_cc_table_entry_shadow *entries;
2169 	int i;
2170 
2171 	/* Is the table index more than what is supported? */
2172 	if (cct_block_index > IB_CC_TABLE_CAP_DEFAULT - 1)
2173 		goto bail;
2174 
2175 	memset(ccp->mgmt_data, 0, sizeof(ccp->mgmt_data));
2176 
2177 	spin_lock(&ppd->cc_shadow_lock);
2178 
2179 	max_cct_block =
2180 		(ppd->ccti_entries_shadow->ccti_last_entry + 1)/IB_CCT_ENTRIES;
2181 	max_cct_block = max_cct_block ? max_cct_block - 1 : 0;
2182 
2183 	if (cct_block_index > max_cct_block) {
2184 		spin_unlock(&ppd->cc_shadow_lock);
2185 		goto bail;
2186 	}
2187 
2188 	ccp->attr_mod = cpu_to_be32(cct_block_index);
2189 
2190 	cct_entry = IB_CCT_ENTRIES * (cct_block_index + 1);
2191 
2192 	cct_entry--;
2193 
2194 	p->ccti_limit = cpu_to_be16(cct_entry);
2195 
2196 	entries = &ppd->ccti_entries_shadow->
2197 			entries[IB_CCT_ENTRIES * cct_block_index];
2198 	cct_entry %= IB_CCT_ENTRIES;
2199 
2200 	for (i = 0; i <= cct_entry; i++)
2201 		p->ccti_entries[i].entry = cpu_to_be16(entries[i].entry);
2202 
2203 	spin_unlock(&ppd->cc_shadow_lock);
2204 
2205 	return reply((struct ib_smp *) ccp);
2206 
2207 bail:
2208 	return reply_failure((struct ib_smp *) ccp);
2209 }
2210 
2211 static int cc_set_congestion_setting(struct ib_cc_mad *ccp,
2212 				struct ib_device *ibdev, u8 port)
2213 {
2214 	struct ib_cc_congestion_setting_attr *p =
2215 		(struct ib_cc_congestion_setting_attr *)ccp->mgmt_data;
2216 	struct qib_ibport *ibp = to_iport(ibdev, port);
2217 	struct qib_pportdata *ppd = ppd_from_ibp(ibp);
2218 	int i;
2219 
2220 	ppd->cc_sl_control_map = be16_to_cpu(p->control_map);
2221 
2222 	for (i = 0; i < IB_CC_CCS_ENTRIES; i++) {
2223 		ppd->congestion_entries[i].ccti_increase =
2224 			p->entries[i].ccti_increase;
2225 
2226 		ppd->congestion_entries[i].ccti_timer =
2227 			be16_to_cpu(p->entries[i].ccti_timer);
2228 
2229 		ppd->congestion_entries[i].trigger_threshold =
2230 			p->entries[i].trigger_threshold;
2231 
2232 		ppd->congestion_entries[i].ccti_min =
2233 			p->entries[i].ccti_min;
2234 	}
2235 
2236 	return reply((struct ib_smp *) ccp);
2237 }
2238 
2239 static int cc_set_congestion_control_table(struct ib_cc_mad *ccp,
2240 				struct ib_device *ibdev, u8 port)
2241 {
2242 	struct ib_cc_table_attr *p =
2243 		(struct ib_cc_table_attr *)ccp->mgmt_data;
2244 	struct qib_ibport *ibp = to_iport(ibdev, port);
2245 	struct qib_pportdata *ppd = ppd_from_ibp(ibp);
2246 	u32 cct_block_index = be32_to_cpu(ccp->attr_mod);
2247 	u32 cct_entry;
2248 	struct ib_cc_table_entry_shadow *entries;
2249 	int i;
2250 
2251 	/* Is the table index more than what is supported? */
2252 	if (cct_block_index > IB_CC_TABLE_CAP_DEFAULT - 1)
2253 		goto bail;
2254 
2255 	/* If this packet is the first in the sequence then
2256 	 * zero the total table entry count.
2257 	 */
2258 	if (be16_to_cpu(p->ccti_limit) < IB_CCT_ENTRIES)
2259 		ppd->total_cct_entry = 0;
2260 
2261 	cct_entry = (be16_to_cpu(p->ccti_limit))%IB_CCT_ENTRIES;
2262 
2263 	/* ccti_limit is 0 to 63 */
2264 	ppd->total_cct_entry += (cct_entry + 1);
2265 
2266 	if (ppd->total_cct_entry > ppd->cc_supported_table_entries)
2267 		goto bail;
2268 
2269 	ppd->ccti_limit = be16_to_cpu(p->ccti_limit);
2270 
2271 	entries = ppd->ccti_entries + (IB_CCT_ENTRIES * cct_block_index);
2272 
2273 	for (i = 0; i <= cct_entry; i++)
2274 		entries[i].entry = be16_to_cpu(p->ccti_entries[i].entry);
2275 
2276 	spin_lock(&ppd->cc_shadow_lock);
2277 
2278 	ppd->ccti_entries_shadow->ccti_last_entry = ppd->total_cct_entry - 1;
2279 	memcpy(ppd->ccti_entries_shadow->entries, ppd->ccti_entries,
2280 		(ppd->total_cct_entry * sizeof(struct ib_cc_table_entry)));
2281 
2282 	ppd->congestion_entries_shadow->port_control = IB_CC_CCS_PC_SL_BASED;
2283 	ppd->congestion_entries_shadow->control_map = ppd->cc_sl_control_map;
2284 	memcpy(ppd->congestion_entries_shadow->entries, ppd->congestion_entries,
2285 		IB_CC_CCS_ENTRIES * sizeof(struct ib_cc_congestion_entry));
2286 
2287 	spin_unlock(&ppd->cc_shadow_lock);
2288 
2289 	return reply((struct ib_smp *) ccp);
2290 
2291 bail:
2292 	return reply_failure((struct ib_smp *) ccp);
2293 }
2294 
2295 static int check_cc_key(struct qib_ibport *ibp,
2296 			struct ib_cc_mad *ccp, int mad_flags)
2297 {
2298 	return 0;
2299 }
2300 
2301 static int process_cc(struct ib_device *ibdev, int mad_flags,
2302 			u8 port, struct ib_mad *in_mad,
2303 			struct ib_mad *out_mad)
2304 {
2305 	struct ib_cc_mad *ccp = (struct ib_cc_mad *)out_mad;
2306 	struct qib_ibport *ibp = to_iport(ibdev, port);
2307 	int ret;
2308 
2309 	*out_mad = *in_mad;
2310 
2311 	if (ccp->class_version != 2) {
2312 		ccp->status |= IB_SMP_UNSUP_VERSION;
2313 		ret = reply((struct ib_smp *)ccp);
2314 		goto bail;
2315 	}
2316 
2317 	ret = check_cc_key(ibp, ccp, mad_flags);
2318 	if (ret)
2319 		goto bail;
2320 
2321 	switch (ccp->method) {
2322 	case IB_MGMT_METHOD_GET:
2323 		switch (ccp->attr_id) {
2324 		case IB_CC_ATTR_CLASSPORTINFO:
2325 			ret = cc_get_classportinfo(ccp, ibdev);
2326 			goto bail;
2327 
2328 		case IB_CC_ATTR_CONGESTION_INFO:
2329 			ret = cc_get_congestion_info(ccp, ibdev, port);
2330 			goto bail;
2331 
2332 		case IB_CC_ATTR_CA_CONGESTION_SETTING:
2333 			ret = cc_get_congestion_setting(ccp, ibdev, port);
2334 			goto bail;
2335 
2336 		case IB_CC_ATTR_CONGESTION_CONTROL_TABLE:
2337 			ret = cc_get_congestion_control_table(ccp, ibdev, port);
2338 			goto bail;
2339 
2340 			/* FALLTHROUGH */
2341 		default:
2342 			ccp->status |= IB_SMP_UNSUP_METH_ATTR;
2343 			ret = reply((struct ib_smp *) ccp);
2344 			goto bail;
2345 		}
2346 
2347 	case IB_MGMT_METHOD_SET:
2348 		switch (ccp->attr_id) {
2349 		case IB_CC_ATTR_CA_CONGESTION_SETTING:
2350 			ret = cc_set_congestion_setting(ccp, ibdev, port);
2351 			goto bail;
2352 
2353 		case IB_CC_ATTR_CONGESTION_CONTROL_TABLE:
2354 			ret = cc_set_congestion_control_table(ccp, ibdev, port);
2355 			goto bail;
2356 
2357 			/* FALLTHROUGH */
2358 		default:
2359 			ccp->status |= IB_SMP_UNSUP_METH_ATTR;
2360 			ret = reply((struct ib_smp *) ccp);
2361 			goto bail;
2362 		}
2363 
2364 	case IB_MGMT_METHOD_GET_RESP:
2365 		/*
2366 		 * The ib_mad module will call us to process responses
2367 		 * before checking for other consumers.
2368 		 * Just tell the caller to process it normally.
2369 		 */
2370 		ret = IB_MAD_RESULT_SUCCESS;
2371 		goto bail;
2372 
2373 	case IB_MGMT_METHOD_TRAP:
2374 	default:
2375 		ccp->status |= IB_SMP_UNSUP_METHOD;
2376 		ret = reply((struct ib_smp *) ccp);
2377 	}
2378 
2379 bail:
2380 	return ret;
2381 }
2382 
2383 /**
2384  * qib_process_mad - process an incoming MAD packet
2385  * @ibdev: the infiniband device this packet came in on
2386  * @mad_flags: MAD flags
2387  * @port: the port number this packet came in on
2388  * @in_wc: the work completion entry for this packet
2389  * @in_grh: the global route header for this packet
2390  * @in_mad: the incoming MAD
2391  * @out_mad: any outgoing MAD reply
2392  *
2393  * Returns IB_MAD_RESULT_SUCCESS if this is a MAD that we are not
2394  * interested in processing.
2395  *
2396  * Note that the verbs framework has already done the MAD sanity checks,
2397  * and hop count/pointer updating for IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE
2398  * MADs.
2399  *
2400  * This is called by the ib_mad module.
2401  */
2402 int qib_process_mad(struct ib_device *ibdev, int mad_flags, u8 port,
2403 		    struct ib_wc *in_wc, struct ib_grh *in_grh,
2404 		    struct ib_mad *in_mad, struct ib_mad *out_mad)
2405 {
2406 	int ret;
2407 	struct qib_ibport *ibp = to_iport(ibdev, port);
2408 	struct qib_pportdata *ppd = ppd_from_ibp(ibp);
2409 
2410 	switch (in_mad->mad_hdr.mgmt_class) {
2411 	case IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE:
2412 	case IB_MGMT_CLASS_SUBN_LID_ROUTED:
2413 		ret = process_subn(ibdev, mad_flags, port, in_mad, out_mad);
2414 		goto bail;
2415 
2416 	case IB_MGMT_CLASS_PERF_MGMT:
2417 		ret = process_perf(ibdev, port, in_mad, out_mad);
2418 		goto bail;
2419 
2420 	case IB_MGMT_CLASS_CONG_MGMT:
2421 		if (!ppd->congestion_entries_shadow ||
2422 			 !qib_cc_table_size) {
2423 			ret = IB_MAD_RESULT_SUCCESS;
2424 			goto bail;
2425 		}
2426 		ret = process_cc(ibdev, mad_flags, port, in_mad, out_mad);
2427 		goto bail;
2428 
2429 	default:
2430 		ret = IB_MAD_RESULT_SUCCESS;
2431 	}
2432 
2433 bail:
2434 	return ret;
2435 }
2436 
2437 static void send_handler(struct ib_mad_agent *agent,
2438 			 struct ib_mad_send_wc *mad_send_wc)
2439 {
2440 	ib_free_send_mad(mad_send_wc->send_buf);
2441 }
2442 
2443 static void xmit_wait_timer_func(unsigned long opaque)
2444 {
2445 	struct qib_pportdata *ppd = (struct qib_pportdata *)opaque;
2446 	struct qib_devdata *dd = dd_from_ppd(ppd);
2447 	unsigned long flags;
2448 	u8 status;
2449 
2450 	spin_lock_irqsave(&ppd->ibport_data.lock, flags);
2451 	if (ppd->cong_stats.flags == IB_PMA_CONG_HW_CONTROL_SAMPLE) {
2452 		status = dd->f_portcntr(ppd, QIBPORTCNTR_PSSTAT);
2453 		if (status == IB_PMA_SAMPLE_STATUS_DONE) {
2454 			/* save counter cache */
2455 			cache_hw_sample_counters(ppd);
2456 			ppd->cong_stats.flags = IB_PMA_CONG_HW_CONTROL_TIMER;
2457 		} else
2458 			goto done;
2459 	}
2460 	ppd->cong_stats.counter = xmit_wait_get_value_delta(ppd);
2461 	dd->f_set_cntr_sample(ppd, QIB_CONG_TIMER_PSINTERVAL, 0x0);
2462 done:
2463 	spin_unlock_irqrestore(&ppd->ibport_data.lock, flags);
2464 	mod_timer(&ppd->cong_stats.timer, jiffies + HZ);
2465 }
2466 
2467 int qib_create_agents(struct qib_ibdev *dev)
2468 {
2469 	struct qib_devdata *dd = dd_from_dev(dev);
2470 	struct ib_mad_agent *agent;
2471 	struct qib_ibport *ibp;
2472 	int p;
2473 	int ret;
2474 
2475 	for (p = 0; p < dd->num_pports; p++) {
2476 		ibp = &dd->pport[p].ibport_data;
2477 		agent = ib_register_mad_agent(&dev->ibdev, p + 1, IB_QPT_SMI,
2478 					      NULL, 0, send_handler,
2479 					      NULL, NULL, 0);
2480 		if (IS_ERR(agent)) {
2481 			ret = PTR_ERR(agent);
2482 			goto err;
2483 		}
2484 
2485 		/* Initialize xmit_wait structure */
2486 		dd->pport[p].cong_stats.counter = 0;
2487 		init_timer(&dd->pport[p].cong_stats.timer);
2488 		dd->pport[p].cong_stats.timer.function = xmit_wait_timer_func;
2489 		dd->pport[p].cong_stats.timer.data =
2490 			(unsigned long)(&dd->pport[p]);
2491 		dd->pport[p].cong_stats.timer.expires = 0;
2492 		add_timer(&dd->pport[p].cong_stats.timer);
2493 
2494 		ibp->send_agent = agent;
2495 	}
2496 
2497 	return 0;
2498 
2499 err:
2500 	for (p = 0; p < dd->num_pports; p++) {
2501 		ibp = &dd->pport[p].ibport_data;
2502 		if (ibp->send_agent) {
2503 			agent = ibp->send_agent;
2504 			ibp->send_agent = NULL;
2505 			ib_unregister_mad_agent(agent);
2506 		}
2507 	}
2508 
2509 	return ret;
2510 }
2511 
2512 void qib_free_agents(struct qib_ibdev *dev)
2513 {
2514 	struct qib_devdata *dd = dd_from_dev(dev);
2515 	struct ib_mad_agent *agent;
2516 	struct qib_ibport *ibp;
2517 	int p;
2518 
2519 	for (p = 0; p < dd->num_pports; p++) {
2520 		ibp = &dd->pport[p].ibport_data;
2521 		if (ibp->send_agent) {
2522 			agent = ibp->send_agent;
2523 			ibp->send_agent = NULL;
2524 			ib_unregister_mad_agent(agent);
2525 		}
2526 		if (ibp->sm_ah) {
2527 			ib_destroy_ah(&ibp->sm_ah->ibah);
2528 			ibp->sm_ah = NULL;
2529 		}
2530 		if (dd->pport[p].cong_stats.timer.data)
2531 			del_timer_sync(&dd->pport[p].cong_stats.timer);
2532 	}
2533 }
2534