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