xref: /openbmc/linux/drivers/infiniband/hw/hfi1/mad.c (revision 80483c3a)
1 /*
2  * Copyright(c) 2015, 2016 Intel Corporation.
3  *
4  * This file is provided under a dual BSD/GPLv2 license.  When using or
5  * redistributing this file, you may do so under either license.
6  *
7  * GPL LICENSE SUMMARY
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of version 2 of the GNU General Public License as
11  * published by the Free Software Foundation.
12  *
13  * This program is distributed in the hope that it will be useful, but
14  * WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * General Public License for more details.
17  *
18  * BSD LICENSE
19  *
20  * Redistribution and use in source and binary forms, with or without
21  * modification, are permitted provided that the following conditions
22  * are met:
23  *
24  *  - Redistributions of source code must retain the above copyright
25  *    notice, this list of conditions and the following disclaimer.
26  *  - Redistributions in binary form must reproduce the above copyright
27  *    notice, this list of conditions and the following disclaimer in
28  *    the documentation and/or other materials provided with the
29  *    distribution.
30  *  - Neither the name of Intel Corporation nor the names of its
31  *    contributors may be used to endorse or promote products derived
32  *    from this software without specific prior written permission.
33  *
34  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
35  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
36  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
37  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
38  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
39  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
40  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
41  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
42  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
43  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
44  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
45  *
46  */
47 
48 #include <linux/net.h>
49 #define OPA_NUM_PKEY_BLOCKS_PER_SMP (OPA_SMP_DR_DATA_SIZE \
50 			/ (OPA_PARTITION_TABLE_BLK_SIZE * sizeof(u16)))
51 
52 #include "hfi.h"
53 #include "mad.h"
54 #include "trace.h"
55 #include "qp.h"
56 
57 /* the reset value from the FM is supposed to be 0xffff, handle both */
58 #define OPA_LINK_WIDTH_RESET_OLD 0x0fff
59 #define OPA_LINK_WIDTH_RESET 0xffff
60 
61 static int reply(struct ib_mad_hdr *smp)
62 {
63 	/*
64 	 * The verbs framework will handle the directed/LID route
65 	 * packet changes.
66 	 */
67 	smp->method = IB_MGMT_METHOD_GET_RESP;
68 	if (smp->mgmt_class == IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE)
69 		smp->status |= IB_SMP_DIRECTION;
70 	return IB_MAD_RESULT_SUCCESS | IB_MAD_RESULT_REPLY;
71 }
72 
73 static inline void clear_opa_smp_data(struct opa_smp *smp)
74 {
75 	void *data = opa_get_smp_data(smp);
76 	size_t size = opa_get_smp_data_size(smp);
77 
78 	memset(data, 0, size);
79 }
80 
81 void hfi1_event_pkey_change(struct hfi1_devdata *dd, u8 port)
82 {
83 	struct ib_event event;
84 
85 	event.event = IB_EVENT_PKEY_CHANGE;
86 	event.device = &dd->verbs_dev.rdi.ibdev;
87 	event.element.port_num = port;
88 	ib_dispatch_event(&event);
89 }
90 
91 static void send_trap(struct hfi1_ibport *ibp, void *data, unsigned len)
92 {
93 	struct ib_mad_send_buf *send_buf;
94 	struct ib_mad_agent *agent;
95 	struct opa_smp *smp;
96 	int ret;
97 	unsigned long flags;
98 	unsigned long timeout;
99 	int pkey_idx;
100 	u32 qpn = ppd_from_ibp(ibp)->sm_trap_qp;
101 
102 	agent = ibp->rvp.send_agent;
103 	if (!agent)
104 		return;
105 
106 	/* o14-3.2.1 */
107 	if (ppd_from_ibp(ibp)->lstate != IB_PORT_ACTIVE)
108 		return;
109 
110 	/* o14-2 */
111 	if (ibp->rvp.trap_timeout && time_before(jiffies,
112 						 ibp->rvp.trap_timeout))
113 		return;
114 
115 	pkey_idx = hfi1_lookup_pkey_idx(ibp, LIM_MGMT_P_KEY);
116 	if (pkey_idx < 0) {
117 		pr_warn("%s: failed to find limited mgmt pkey, defaulting 0x%x\n",
118 			__func__, hfi1_get_pkey(ibp, 1));
119 		pkey_idx = 1;
120 	}
121 
122 	send_buf = ib_create_send_mad(agent, qpn, pkey_idx, 0,
123 				      IB_MGMT_MAD_HDR, IB_MGMT_MAD_DATA,
124 				      GFP_ATOMIC, IB_MGMT_BASE_VERSION);
125 	if (IS_ERR(send_buf))
126 		return;
127 
128 	smp = send_buf->mad;
129 	smp->base_version = OPA_MGMT_BASE_VERSION;
130 	smp->mgmt_class = IB_MGMT_CLASS_SUBN_LID_ROUTED;
131 	smp->class_version = OPA_SMI_CLASS_VERSION;
132 	smp->method = IB_MGMT_METHOD_TRAP;
133 	ibp->rvp.tid++;
134 	smp->tid = cpu_to_be64(ibp->rvp.tid);
135 	smp->attr_id = IB_SMP_ATTR_NOTICE;
136 	/* o14-1: smp->mkey = 0; */
137 	memcpy(smp->route.lid.data, data, len);
138 
139 	spin_lock_irqsave(&ibp->rvp.lock, flags);
140 	if (!ibp->rvp.sm_ah) {
141 		if (ibp->rvp.sm_lid != be16_to_cpu(IB_LID_PERMISSIVE)) {
142 			struct ib_ah *ah;
143 
144 			ah = hfi1_create_qp0_ah(ibp, ibp->rvp.sm_lid);
145 			if (IS_ERR(ah)) {
146 				ret = PTR_ERR(ah);
147 			} else {
148 				send_buf->ah = ah;
149 				ibp->rvp.sm_ah = ibah_to_rvtah(ah);
150 				ret = 0;
151 			}
152 		} else {
153 			ret = -EINVAL;
154 		}
155 	} else {
156 		send_buf->ah = &ibp->rvp.sm_ah->ibah;
157 		ret = 0;
158 	}
159 	spin_unlock_irqrestore(&ibp->rvp.lock, flags);
160 
161 	if (!ret)
162 		ret = ib_post_send_mad(send_buf, NULL);
163 	if (!ret) {
164 		/* 4.096 usec. */
165 		timeout = (4096 * (1UL << ibp->rvp.subnet_timeout)) / 1000;
166 		ibp->rvp.trap_timeout = jiffies + usecs_to_jiffies(timeout);
167 	} else {
168 		ib_free_send_mad(send_buf);
169 		ibp->rvp.trap_timeout = 0;
170 	}
171 }
172 
173 /*
174  * Send a bad [PQ]_Key trap (ch. 14.3.8).
175  */
176 void hfi1_bad_pqkey(struct hfi1_ibport *ibp, __be16 trap_num, u32 key, u32 sl,
177 		    u32 qp1, u32 qp2, u16 lid1, u16 lid2)
178 {
179 	struct opa_mad_notice_attr data;
180 	u32 lid = ppd_from_ibp(ibp)->lid;
181 	u32 _lid1 = lid1;
182 	u32 _lid2 = lid2;
183 
184 	memset(&data, 0, sizeof(data));
185 
186 	if (trap_num == OPA_TRAP_BAD_P_KEY)
187 		ibp->rvp.pkey_violations++;
188 	else
189 		ibp->rvp.qkey_violations++;
190 	ibp->rvp.n_pkt_drops++;
191 
192 	/* Send violation trap */
193 	data.generic_type = IB_NOTICE_TYPE_SECURITY;
194 	data.prod_type_lsb = IB_NOTICE_PROD_CA;
195 	data.trap_num = trap_num;
196 	data.issuer_lid = cpu_to_be32(lid);
197 	data.ntc_257_258.lid1 = cpu_to_be32(_lid1);
198 	data.ntc_257_258.lid2 = cpu_to_be32(_lid2);
199 	data.ntc_257_258.key = cpu_to_be32(key);
200 	data.ntc_257_258.sl = sl << 3;
201 	data.ntc_257_258.qp1 = cpu_to_be32(qp1);
202 	data.ntc_257_258.qp2 = cpu_to_be32(qp2);
203 
204 	send_trap(ibp, &data, sizeof(data));
205 }
206 
207 /*
208  * Send a bad M_Key trap (ch. 14.3.9).
209  */
210 static void bad_mkey(struct hfi1_ibport *ibp, struct ib_mad_hdr *mad,
211 		     __be64 mkey, __be32 dr_slid, u8 return_path[], u8 hop_cnt)
212 {
213 	struct opa_mad_notice_attr data;
214 	u32 lid = ppd_from_ibp(ibp)->lid;
215 
216 	memset(&data, 0, sizeof(data));
217 	/* Send violation trap */
218 	data.generic_type = IB_NOTICE_TYPE_SECURITY;
219 	data.prod_type_lsb = IB_NOTICE_PROD_CA;
220 	data.trap_num = OPA_TRAP_BAD_M_KEY;
221 	data.issuer_lid = cpu_to_be32(lid);
222 	data.ntc_256.lid = data.issuer_lid;
223 	data.ntc_256.method = mad->method;
224 	data.ntc_256.attr_id = mad->attr_id;
225 	data.ntc_256.attr_mod = mad->attr_mod;
226 	data.ntc_256.mkey = mkey;
227 	if (mad->mgmt_class == IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE) {
228 		data.ntc_256.dr_slid = dr_slid;
229 		data.ntc_256.dr_trunc_hop = IB_NOTICE_TRAP_DR_NOTICE;
230 		if (hop_cnt > ARRAY_SIZE(data.ntc_256.dr_rtn_path)) {
231 			data.ntc_256.dr_trunc_hop |=
232 				IB_NOTICE_TRAP_DR_TRUNC;
233 			hop_cnt = ARRAY_SIZE(data.ntc_256.dr_rtn_path);
234 		}
235 		data.ntc_256.dr_trunc_hop |= hop_cnt;
236 		memcpy(data.ntc_256.dr_rtn_path, return_path,
237 		       hop_cnt);
238 	}
239 
240 	send_trap(ibp, &data, sizeof(data));
241 }
242 
243 /*
244  * Send a Port Capability Mask Changed trap (ch. 14.3.11).
245  */
246 void hfi1_cap_mask_chg(struct rvt_dev_info *rdi, u8 port_num)
247 {
248 	struct opa_mad_notice_attr data;
249 	struct hfi1_ibdev *verbs_dev = dev_from_rdi(rdi);
250 	struct hfi1_devdata *dd = dd_from_dev(verbs_dev);
251 	struct hfi1_ibport *ibp = &dd->pport[port_num - 1].ibport_data;
252 	u32 lid = ppd_from_ibp(ibp)->lid;
253 
254 	memset(&data, 0, sizeof(data));
255 
256 	data.generic_type = IB_NOTICE_TYPE_INFO;
257 	data.prod_type_lsb = IB_NOTICE_PROD_CA;
258 	data.trap_num = OPA_TRAP_CHANGE_CAPABILITY;
259 	data.issuer_lid = cpu_to_be32(lid);
260 	data.ntc_144.lid = data.issuer_lid;
261 	data.ntc_144.new_cap_mask = cpu_to_be32(ibp->rvp.port_cap_flags);
262 
263 	send_trap(ibp, &data, sizeof(data));
264 }
265 
266 /*
267  * Send a System Image GUID Changed trap (ch. 14.3.12).
268  */
269 void hfi1_sys_guid_chg(struct hfi1_ibport *ibp)
270 {
271 	struct opa_mad_notice_attr data;
272 	u32 lid = ppd_from_ibp(ibp)->lid;
273 
274 	memset(&data, 0, sizeof(data));
275 
276 	data.generic_type = IB_NOTICE_TYPE_INFO;
277 	data.prod_type_lsb = IB_NOTICE_PROD_CA;
278 	data.trap_num = OPA_TRAP_CHANGE_SYSGUID;
279 	data.issuer_lid = cpu_to_be32(lid);
280 	data.ntc_145.new_sys_guid = ib_hfi1_sys_image_guid;
281 	data.ntc_145.lid = data.issuer_lid;
282 
283 	send_trap(ibp, &data, sizeof(data));
284 }
285 
286 /*
287  * Send a Node Description Changed trap (ch. 14.3.13).
288  */
289 void hfi1_node_desc_chg(struct hfi1_ibport *ibp)
290 {
291 	struct opa_mad_notice_attr data;
292 	u32 lid = ppd_from_ibp(ibp)->lid;
293 
294 	memset(&data, 0, sizeof(data));
295 
296 	data.generic_type = IB_NOTICE_TYPE_INFO;
297 	data.prod_type_lsb = IB_NOTICE_PROD_CA;
298 	data.trap_num = OPA_TRAP_CHANGE_CAPABILITY;
299 	data.issuer_lid = cpu_to_be32(lid);
300 	data.ntc_144.lid = data.issuer_lid;
301 	data.ntc_144.change_flags =
302 		cpu_to_be16(OPA_NOTICE_TRAP_NODE_DESC_CHG);
303 
304 	send_trap(ibp, &data, sizeof(data));
305 }
306 
307 static int __subn_get_opa_nodedesc(struct opa_smp *smp, u32 am,
308 				   u8 *data, struct ib_device *ibdev,
309 				   u8 port, u32 *resp_len)
310 {
311 	struct opa_node_description *nd;
312 
313 	if (am) {
314 		smp->status |= IB_SMP_INVALID_FIELD;
315 		return reply((struct ib_mad_hdr *)smp);
316 	}
317 
318 	nd = (struct opa_node_description *)data;
319 
320 	memcpy(nd->data, ibdev->node_desc, sizeof(nd->data));
321 
322 	if (resp_len)
323 		*resp_len += sizeof(*nd);
324 
325 	return reply((struct ib_mad_hdr *)smp);
326 }
327 
328 static int __subn_get_opa_nodeinfo(struct opa_smp *smp, u32 am, u8 *data,
329 				   struct ib_device *ibdev, u8 port,
330 				   u32 *resp_len)
331 {
332 	struct opa_node_info *ni;
333 	struct hfi1_devdata *dd = dd_from_ibdev(ibdev);
334 	unsigned pidx = port - 1; /* IB number port from 1, hw from 0 */
335 
336 	ni = (struct opa_node_info *)data;
337 
338 	/* GUID 0 is illegal */
339 	if (am || pidx >= dd->num_pports || dd->pport[pidx].guid == 0) {
340 		smp->status |= IB_SMP_INVALID_FIELD;
341 		return reply((struct ib_mad_hdr *)smp);
342 	}
343 
344 	ni->port_guid = cpu_to_be64(dd->pport[pidx].guid);
345 	ni->base_version = OPA_MGMT_BASE_VERSION;
346 	ni->class_version = OPA_SMI_CLASS_VERSION;
347 	ni->node_type = 1;     /* channel adapter */
348 	ni->num_ports = ibdev->phys_port_cnt;
349 	/* This is already in network order */
350 	ni->system_image_guid = ib_hfi1_sys_image_guid;
351 	/* Use first-port GUID as node */
352 	ni->node_guid = cpu_to_be64(dd->pport->guid);
353 	ni->partition_cap = cpu_to_be16(hfi1_get_npkeys(dd));
354 	ni->device_id = cpu_to_be16(dd->pcidev->device);
355 	ni->revision = cpu_to_be32(dd->minrev);
356 	ni->local_port_num = port;
357 	ni->vendor_id[0] = dd->oui1;
358 	ni->vendor_id[1] = dd->oui2;
359 	ni->vendor_id[2] = dd->oui3;
360 
361 	if (resp_len)
362 		*resp_len += sizeof(*ni);
363 
364 	return reply((struct ib_mad_hdr *)smp);
365 }
366 
367 static int subn_get_nodeinfo(struct ib_smp *smp, struct ib_device *ibdev,
368 			     u8 port)
369 {
370 	struct ib_node_info *nip = (struct ib_node_info *)&smp->data;
371 	struct hfi1_devdata *dd = dd_from_ibdev(ibdev);
372 	unsigned pidx = port - 1; /* IB number port from 1, hw from 0 */
373 
374 	/* GUID 0 is illegal */
375 	if (smp->attr_mod || pidx >= dd->num_pports ||
376 	    dd->pport[pidx].guid == 0)
377 		smp->status |= IB_SMP_INVALID_FIELD;
378 	else
379 		nip->port_guid = cpu_to_be64(dd->pport[pidx].guid);
380 
381 	nip->base_version = OPA_MGMT_BASE_VERSION;
382 	nip->class_version = OPA_SMI_CLASS_VERSION;
383 	nip->node_type = 1;     /* channel adapter */
384 	nip->num_ports = ibdev->phys_port_cnt;
385 	/* This is already in network order */
386 	nip->sys_guid = ib_hfi1_sys_image_guid;
387 	 /* Use first-port GUID as node */
388 	nip->node_guid = cpu_to_be64(dd->pport->guid);
389 	nip->partition_cap = cpu_to_be16(hfi1_get_npkeys(dd));
390 	nip->device_id = cpu_to_be16(dd->pcidev->device);
391 	nip->revision = cpu_to_be32(dd->minrev);
392 	nip->local_port_num = port;
393 	nip->vendor_id[0] = dd->oui1;
394 	nip->vendor_id[1] = dd->oui2;
395 	nip->vendor_id[2] = dd->oui3;
396 
397 	return reply((struct ib_mad_hdr *)smp);
398 }
399 
400 static void set_link_width_enabled(struct hfi1_pportdata *ppd, u32 w)
401 {
402 	(void)hfi1_set_ib_cfg(ppd, HFI1_IB_CFG_LWID_ENB, w);
403 }
404 
405 static void set_link_width_downgrade_enabled(struct hfi1_pportdata *ppd, u32 w)
406 {
407 	(void)hfi1_set_ib_cfg(ppd, HFI1_IB_CFG_LWID_DG_ENB, w);
408 }
409 
410 static void set_link_speed_enabled(struct hfi1_pportdata *ppd, u32 s)
411 {
412 	(void)hfi1_set_ib_cfg(ppd, HFI1_IB_CFG_SPD_ENB, s);
413 }
414 
415 static int check_mkey(struct hfi1_ibport *ibp, struct ib_mad_hdr *mad,
416 		      int mad_flags, __be64 mkey, __be32 dr_slid,
417 		      u8 return_path[], u8 hop_cnt)
418 {
419 	int valid_mkey = 0;
420 	int ret = 0;
421 
422 	/* Is the mkey in the process of expiring? */
423 	if (ibp->rvp.mkey_lease_timeout &&
424 	    time_after_eq(jiffies, ibp->rvp.mkey_lease_timeout)) {
425 		/* Clear timeout and mkey protection field. */
426 		ibp->rvp.mkey_lease_timeout = 0;
427 		ibp->rvp.mkeyprot = 0;
428 	}
429 
430 	if ((mad_flags & IB_MAD_IGNORE_MKEY) ||  ibp->rvp.mkey == 0 ||
431 	    ibp->rvp.mkey == mkey)
432 		valid_mkey = 1;
433 
434 	/* Unset lease timeout on any valid Get/Set/TrapRepress */
435 	if (valid_mkey && ibp->rvp.mkey_lease_timeout &&
436 	    (mad->method == IB_MGMT_METHOD_GET ||
437 	     mad->method == IB_MGMT_METHOD_SET ||
438 	     mad->method == IB_MGMT_METHOD_TRAP_REPRESS))
439 		ibp->rvp.mkey_lease_timeout = 0;
440 
441 	if (!valid_mkey) {
442 		switch (mad->method) {
443 		case IB_MGMT_METHOD_GET:
444 			/* Bad mkey not a violation below level 2 */
445 			if (ibp->rvp.mkeyprot < 2)
446 				break;
447 		case IB_MGMT_METHOD_SET:
448 		case IB_MGMT_METHOD_TRAP_REPRESS:
449 			if (ibp->rvp.mkey_violations != 0xFFFF)
450 				++ibp->rvp.mkey_violations;
451 			if (!ibp->rvp.mkey_lease_timeout &&
452 			    ibp->rvp.mkey_lease_period)
453 				ibp->rvp.mkey_lease_timeout = jiffies +
454 					ibp->rvp.mkey_lease_period * HZ;
455 			/* Generate a trap notice. */
456 			bad_mkey(ibp, mad, mkey, dr_slid, return_path,
457 				 hop_cnt);
458 			ret = 1;
459 		}
460 	}
461 
462 	return ret;
463 }
464 
465 /*
466  * The SMA caches reads from LCB registers in case the LCB is unavailable.
467  * (The LCB is unavailable in certain link states, for example.)
468  */
469 struct lcb_datum {
470 	u32 off;
471 	u64 val;
472 };
473 
474 static struct lcb_datum lcb_cache[] = {
475 	{ DC_LCB_STS_ROUND_TRIP_LTP_CNT, 0 },
476 };
477 
478 static int write_lcb_cache(u32 off, u64 val)
479 {
480 	int i;
481 
482 	for (i = 0; i < ARRAY_SIZE(lcb_cache); i++) {
483 		if (lcb_cache[i].off == off) {
484 			lcb_cache[i].val = val;
485 			return 0;
486 		}
487 	}
488 
489 	pr_warn("%s bad offset 0x%x\n", __func__, off);
490 	return -1;
491 }
492 
493 static int read_lcb_cache(u32 off, u64 *val)
494 {
495 	int i;
496 
497 	for (i = 0; i < ARRAY_SIZE(lcb_cache); i++) {
498 		if (lcb_cache[i].off == off) {
499 			*val = lcb_cache[i].val;
500 			return 0;
501 		}
502 	}
503 
504 	pr_warn("%s bad offset 0x%x\n", __func__, off);
505 	return -1;
506 }
507 
508 void read_ltp_rtt(struct hfi1_devdata *dd)
509 {
510 	u64 reg;
511 
512 	if (read_lcb_csr(dd, DC_LCB_STS_ROUND_TRIP_LTP_CNT, &reg))
513 		dd_dev_err(dd, "%s: unable to read LTP RTT\n", __func__);
514 	else
515 		write_lcb_cache(DC_LCB_STS_ROUND_TRIP_LTP_CNT, reg);
516 }
517 
518 static int __subn_get_opa_portinfo(struct opa_smp *smp, u32 am, u8 *data,
519 				   struct ib_device *ibdev, u8 port,
520 				   u32 *resp_len)
521 {
522 	int i;
523 	struct hfi1_devdata *dd;
524 	struct hfi1_pportdata *ppd;
525 	struct hfi1_ibport *ibp;
526 	struct opa_port_info *pi = (struct opa_port_info *)data;
527 	u8 mtu;
528 	u8 credit_rate;
529 	u8 is_beaconing_active;
530 	u32 state;
531 	u32 num_ports = OPA_AM_NPORT(am);
532 	u32 start_of_sm_config = OPA_AM_START_SM_CFG(am);
533 	u32 buffer_units;
534 	u64 tmp = 0;
535 
536 	if (num_ports != 1) {
537 		smp->status |= IB_SMP_INVALID_FIELD;
538 		return reply((struct ib_mad_hdr *)smp);
539 	}
540 
541 	dd = dd_from_ibdev(ibdev);
542 	/* IB numbers ports from 1, hw from 0 */
543 	ppd = dd->pport + (port - 1);
544 	ibp = &ppd->ibport_data;
545 
546 	if (ppd->vls_supported / 2 > ARRAY_SIZE(pi->neigh_mtu.pvlx_to_mtu) ||
547 	    ppd->vls_supported > ARRAY_SIZE(dd->vld)) {
548 		smp->status |= IB_SMP_INVALID_FIELD;
549 		return reply((struct ib_mad_hdr *)smp);
550 	}
551 
552 	pi->lid = cpu_to_be32(ppd->lid);
553 
554 	/* Only return the mkey if the protection field allows it. */
555 	if (!(smp->method == IB_MGMT_METHOD_GET &&
556 	      ibp->rvp.mkey != smp->mkey &&
557 	      ibp->rvp.mkeyprot == 1))
558 		pi->mkey = ibp->rvp.mkey;
559 
560 	pi->subnet_prefix = ibp->rvp.gid_prefix;
561 	pi->sm_lid = cpu_to_be32(ibp->rvp.sm_lid);
562 	pi->ib_cap_mask = cpu_to_be32(ibp->rvp.port_cap_flags);
563 	pi->mkey_lease_period = cpu_to_be16(ibp->rvp.mkey_lease_period);
564 	pi->sm_trap_qp = cpu_to_be32(ppd->sm_trap_qp);
565 	pi->sa_qp = cpu_to_be32(ppd->sa_qp);
566 
567 	pi->link_width.enabled = cpu_to_be16(ppd->link_width_enabled);
568 	pi->link_width.supported = cpu_to_be16(ppd->link_width_supported);
569 	pi->link_width.active = cpu_to_be16(ppd->link_width_active);
570 
571 	pi->link_width_downgrade.supported =
572 			cpu_to_be16(ppd->link_width_downgrade_supported);
573 	pi->link_width_downgrade.enabled =
574 			cpu_to_be16(ppd->link_width_downgrade_enabled);
575 	pi->link_width_downgrade.tx_active =
576 			cpu_to_be16(ppd->link_width_downgrade_tx_active);
577 	pi->link_width_downgrade.rx_active =
578 			cpu_to_be16(ppd->link_width_downgrade_rx_active);
579 
580 	pi->link_speed.supported = cpu_to_be16(ppd->link_speed_supported);
581 	pi->link_speed.active = cpu_to_be16(ppd->link_speed_active);
582 	pi->link_speed.enabled = cpu_to_be16(ppd->link_speed_enabled);
583 
584 	state = driver_lstate(ppd);
585 
586 	if (start_of_sm_config && (state == IB_PORT_INIT))
587 		ppd->is_sm_config_started = 1;
588 
589 	pi->port_phys_conf = (ppd->port_type & 0xf);
590 
591 	pi->port_states.ledenable_offlinereason = ppd->neighbor_normal << 4;
592 	pi->port_states.ledenable_offlinereason |=
593 		ppd->is_sm_config_started << 5;
594 	/*
595 	 * This pairs with the memory barrier in hfi1_start_led_override to
596 	 * ensure that we read the correct state of LED beaconing represented
597 	 * by led_override_timer_active
598 	 */
599 	smp_rmb();
600 	is_beaconing_active = !!atomic_read(&ppd->led_override_timer_active);
601 	pi->port_states.ledenable_offlinereason |= is_beaconing_active << 6;
602 	pi->port_states.ledenable_offlinereason |=
603 		ppd->offline_disabled_reason;
604 
605 	pi->port_states.portphysstate_portstate =
606 		(hfi1_ibphys_portstate(ppd) << 4) | state;
607 
608 	pi->mkeyprotect_lmc = (ibp->rvp.mkeyprot << 6) | ppd->lmc;
609 
610 	memset(pi->neigh_mtu.pvlx_to_mtu, 0, sizeof(pi->neigh_mtu.pvlx_to_mtu));
611 	for (i = 0; i < ppd->vls_supported; i++) {
612 		mtu = mtu_to_enum(dd->vld[i].mtu, HFI1_DEFAULT_ACTIVE_MTU);
613 		if ((i % 2) == 0)
614 			pi->neigh_mtu.pvlx_to_mtu[i / 2] |= (mtu << 4);
615 		else
616 			pi->neigh_mtu.pvlx_to_mtu[i / 2] |= mtu;
617 	}
618 	/* don't forget VL 15 */
619 	mtu = mtu_to_enum(dd->vld[15].mtu, 2048);
620 	pi->neigh_mtu.pvlx_to_mtu[15 / 2] |= mtu;
621 	pi->smsl = ibp->rvp.sm_sl & OPA_PI_MASK_SMSL;
622 	pi->operational_vls = hfi1_get_ib_cfg(ppd, HFI1_IB_CFG_OP_VLS);
623 	pi->partenforce_filterraw |=
624 		(ppd->linkinit_reason & OPA_PI_MASK_LINKINIT_REASON);
625 	if (ppd->part_enforce & HFI1_PART_ENFORCE_IN)
626 		pi->partenforce_filterraw |= OPA_PI_MASK_PARTITION_ENFORCE_IN;
627 	if (ppd->part_enforce & HFI1_PART_ENFORCE_OUT)
628 		pi->partenforce_filterraw |= OPA_PI_MASK_PARTITION_ENFORCE_OUT;
629 	pi->mkey_violations = cpu_to_be16(ibp->rvp.mkey_violations);
630 	/* P_KeyViolations are counted by hardware. */
631 	pi->pkey_violations = cpu_to_be16(ibp->rvp.pkey_violations);
632 	pi->qkey_violations = cpu_to_be16(ibp->rvp.qkey_violations);
633 
634 	pi->vl.cap = ppd->vls_supported;
635 	pi->vl.high_limit = cpu_to_be16(ibp->rvp.vl_high_limit);
636 	pi->vl.arb_high_cap = (u8)hfi1_get_ib_cfg(ppd, HFI1_IB_CFG_VL_HIGH_CAP);
637 	pi->vl.arb_low_cap = (u8)hfi1_get_ib_cfg(ppd, HFI1_IB_CFG_VL_LOW_CAP);
638 
639 	pi->clientrereg_subnettimeout = ibp->rvp.subnet_timeout;
640 
641 	pi->port_link_mode  = cpu_to_be16(OPA_PORT_LINK_MODE_OPA << 10 |
642 					  OPA_PORT_LINK_MODE_OPA << 5 |
643 					  OPA_PORT_LINK_MODE_OPA);
644 
645 	pi->port_ltp_crc_mode = cpu_to_be16(ppd->port_ltp_crc_mode);
646 
647 	pi->port_mode = cpu_to_be16(
648 				ppd->is_active_optimize_enabled ?
649 					OPA_PI_MASK_PORT_ACTIVE_OPTOMIZE : 0);
650 
651 	pi->port_packet_format.supported =
652 		cpu_to_be16(OPA_PORT_PACKET_FORMAT_9B);
653 	pi->port_packet_format.enabled =
654 		cpu_to_be16(OPA_PORT_PACKET_FORMAT_9B);
655 
656 	/* flit_control.interleave is (OPA V1, version .76):
657 	 * bits		use
658 	 * ----		---
659 	 * 2		res
660 	 * 2		DistanceSupported
661 	 * 2		DistanceEnabled
662 	 * 5		MaxNextLevelTxEnabled
663 	 * 5		MaxNestLevelRxSupported
664 	 *
665 	 * HFI supports only "distance mode 1" (see OPA V1, version .76,
666 	 * section 9.6.2), so set DistanceSupported, DistanceEnabled
667 	 * to 0x1.
668 	 */
669 	pi->flit_control.interleave = cpu_to_be16(0x1400);
670 
671 	pi->link_down_reason = ppd->local_link_down_reason.sma;
672 	pi->neigh_link_down_reason = ppd->neigh_link_down_reason.sma;
673 	pi->port_error_action = cpu_to_be32(ppd->port_error_action);
674 	pi->mtucap = mtu_to_enum(hfi1_max_mtu, IB_MTU_4096);
675 
676 	/* 32.768 usec. response time (guessing) */
677 	pi->resptimevalue = 3;
678 
679 	pi->local_port_num = port;
680 
681 	/* buffer info for FM */
682 	pi->overall_buffer_space = cpu_to_be16(dd->link_credits);
683 
684 	pi->neigh_node_guid = cpu_to_be64(ppd->neighbor_guid);
685 	pi->neigh_port_num = ppd->neighbor_port_number;
686 	pi->port_neigh_mode =
687 		(ppd->neighbor_type & OPA_PI_MASK_NEIGH_NODE_TYPE) |
688 		(ppd->mgmt_allowed ? OPA_PI_MASK_NEIGH_MGMT_ALLOWED : 0) |
689 		(ppd->neighbor_fm_security ?
690 			OPA_PI_MASK_NEIGH_FW_AUTH_BYPASS : 0);
691 
692 	/* HFIs shall always return VL15 credits to their
693 	 * neighbor in a timely manner, without any credit return pacing.
694 	 */
695 	credit_rate = 0;
696 	buffer_units  = (dd->vau) & OPA_PI_MASK_BUF_UNIT_BUF_ALLOC;
697 	buffer_units |= (dd->vcu << 3) & OPA_PI_MASK_BUF_UNIT_CREDIT_ACK;
698 	buffer_units |= (credit_rate << 6) &
699 				OPA_PI_MASK_BUF_UNIT_VL15_CREDIT_RATE;
700 	buffer_units |= (dd->vl15_init << 11) & OPA_PI_MASK_BUF_UNIT_VL15_INIT;
701 	pi->buffer_units = cpu_to_be32(buffer_units);
702 
703 	pi->opa_cap_mask = cpu_to_be16(OPA_CAP_MASK3_IsSharedSpaceSupported);
704 
705 	/* HFI supports a replay buffer 128 LTPs in size */
706 	pi->replay_depth.buffer = 0x80;
707 	/* read the cached value of DC_LCB_STS_ROUND_TRIP_LTP_CNT */
708 	read_lcb_cache(DC_LCB_STS_ROUND_TRIP_LTP_CNT, &tmp);
709 
710 	/*
711 	 * this counter is 16 bits wide, but the replay_depth.wire
712 	 * variable is only 8 bits
713 	 */
714 	if (tmp > 0xff)
715 		tmp = 0xff;
716 	pi->replay_depth.wire = tmp;
717 
718 	if (resp_len)
719 		*resp_len += sizeof(struct opa_port_info);
720 
721 	return reply((struct ib_mad_hdr *)smp);
722 }
723 
724 /**
725  * get_pkeys - return the PKEY table
726  * @dd: the hfi1_ib device
727  * @port: the IB port number
728  * @pkeys: the pkey table is placed here
729  */
730 static int get_pkeys(struct hfi1_devdata *dd, u8 port, u16 *pkeys)
731 {
732 	struct hfi1_pportdata *ppd = dd->pport + port - 1;
733 
734 	memcpy(pkeys, ppd->pkeys, sizeof(ppd->pkeys));
735 
736 	return 0;
737 }
738 
739 static int __subn_get_opa_pkeytable(struct opa_smp *smp, u32 am, u8 *data,
740 				    struct ib_device *ibdev, u8 port,
741 				    u32 *resp_len)
742 {
743 	struct hfi1_devdata *dd = dd_from_ibdev(ibdev);
744 	u32 n_blocks_req = OPA_AM_NBLK(am);
745 	u32 start_block = am & 0x7ff;
746 	__be16 *p;
747 	u16 *q;
748 	int i;
749 	u16 n_blocks_avail;
750 	unsigned npkeys = hfi1_get_npkeys(dd);
751 	size_t size;
752 
753 	if (n_blocks_req == 0) {
754 		pr_warn("OPA Get PKey AM Invalid : P = %d; B = 0x%x; N = 0x%x\n",
755 			port, start_block, n_blocks_req);
756 		smp->status |= IB_SMP_INVALID_FIELD;
757 		return reply((struct ib_mad_hdr *)smp);
758 	}
759 
760 	n_blocks_avail = (u16)(npkeys / OPA_PARTITION_TABLE_BLK_SIZE) + 1;
761 
762 	size = (n_blocks_req * OPA_PARTITION_TABLE_BLK_SIZE) * sizeof(u16);
763 
764 	if (start_block + n_blocks_req > n_blocks_avail ||
765 	    n_blocks_req > OPA_NUM_PKEY_BLOCKS_PER_SMP) {
766 		pr_warn("OPA Get PKey AM Invalid : s 0x%x; req 0x%x; "
767 			"avail 0x%x; blk/smp 0x%lx\n",
768 			start_block, n_blocks_req, n_blocks_avail,
769 			OPA_NUM_PKEY_BLOCKS_PER_SMP);
770 		smp->status |= IB_SMP_INVALID_FIELD;
771 		return reply((struct ib_mad_hdr *)smp);
772 	}
773 
774 	p = (__be16 *)data;
775 	q = (u16 *)data;
776 	/* get the real pkeys if we are requesting the first block */
777 	if (start_block == 0) {
778 		get_pkeys(dd, port, q);
779 		for (i = 0; i < npkeys; i++)
780 			p[i] = cpu_to_be16(q[i]);
781 		if (resp_len)
782 			*resp_len += size;
783 	} else {
784 		smp->status |= IB_SMP_INVALID_FIELD;
785 	}
786 	return reply((struct ib_mad_hdr *)smp);
787 }
788 
789 enum {
790 	HFI_TRANSITION_DISALLOWED,
791 	HFI_TRANSITION_IGNORED,
792 	HFI_TRANSITION_ALLOWED,
793 	HFI_TRANSITION_UNDEFINED,
794 };
795 
796 /*
797  * Use shortened names to improve readability of
798  * {logical,physical}_state_transitions
799  */
800 enum {
801 	__D = HFI_TRANSITION_DISALLOWED,
802 	__I = HFI_TRANSITION_IGNORED,
803 	__A = HFI_TRANSITION_ALLOWED,
804 	__U = HFI_TRANSITION_UNDEFINED,
805 };
806 
807 /*
808  * IB_PORTPHYSSTATE_POLLING (2) through OPA_PORTPHYSSTATE_MAX (11) are
809  * represented in physical_state_transitions.
810  */
811 #define __N_PHYSTATES (OPA_PORTPHYSSTATE_MAX - IB_PORTPHYSSTATE_POLLING + 1)
812 
813 /*
814  * Within physical_state_transitions, rows represent "old" states,
815  * columns "new" states, and physical_state_transitions.allowed[old][new]
816  * indicates if the transition from old state to new state is legal (see
817  * OPAg1v1, Table 6-4).
818  */
819 static const struct {
820 	u8 allowed[__N_PHYSTATES][__N_PHYSTATES];
821 } physical_state_transitions = {
822 	{
823 		/* 2    3    4    5    6    7    8    9   10   11 */
824 	/* 2 */	{ __A, __A, __D, __D, __D, __D, __D, __D, __D, __D },
825 	/* 3 */	{ __A, __I, __D, __D, __D, __D, __D, __D, __D, __A },
826 	/* 4 */	{ __U, __U, __U, __U, __U, __U, __U, __U, __U, __U },
827 	/* 5 */	{ __A, __A, __D, __I, __D, __D, __D, __D, __D, __D },
828 	/* 6 */	{ __U, __U, __U, __U, __U, __U, __U, __U, __U, __U },
829 	/* 7 */	{ __D, __A, __D, __D, __D, __I, __D, __D, __D, __D },
830 	/* 8 */	{ __U, __U, __U, __U, __U, __U, __U, __U, __U, __U },
831 	/* 9 */	{ __I, __A, __D, __D, __D, __D, __D, __I, __D, __D },
832 	/*10 */	{ __U, __U, __U, __U, __U, __U, __U, __U, __U, __U },
833 	/*11 */	{ __D, __A, __D, __D, __D, __D, __D, __D, __D, __I },
834 	}
835 };
836 
837 /*
838  * IB_PORT_DOWN (1) through IB_PORT_ACTIVE_DEFER (5) are represented
839  * logical_state_transitions
840  */
841 
842 #define __N_LOGICAL_STATES (IB_PORT_ACTIVE_DEFER - IB_PORT_DOWN + 1)
843 
844 /*
845  * Within logical_state_transitions rows represent "old" states,
846  * columns "new" states, and logical_state_transitions.allowed[old][new]
847  * indicates if the transition from old state to new state is legal (see
848  * OPAg1v1, Table 9-12).
849  */
850 static const struct {
851 	u8 allowed[__N_LOGICAL_STATES][__N_LOGICAL_STATES];
852 } logical_state_transitions = {
853 	{
854 		/* 1    2    3    4    5 */
855 	/* 1 */	{ __I, __D, __D, __D, __U},
856 	/* 2 */	{ __D, __I, __A, __D, __U},
857 	/* 3 */	{ __D, __D, __I, __A, __U},
858 	/* 4 */	{ __D, __D, __I, __I, __U},
859 	/* 5 */	{ __U, __U, __U, __U, __U},
860 	}
861 };
862 
863 static int logical_transition_allowed(int old, int new)
864 {
865 	if (old < IB_PORT_NOP || old > IB_PORT_ACTIVE_DEFER ||
866 	    new < IB_PORT_NOP || new > IB_PORT_ACTIVE_DEFER) {
867 		pr_warn("invalid logical state(s) (old %d new %d)\n",
868 			old, new);
869 		return HFI_TRANSITION_UNDEFINED;
870 	}
871 
872 	if (new == IB_PORT_NOP)
873 		return HFI_TRANSITION_ALLOWED; /* always allowed */
874 
875 	/* adjust states for indexing into logical_state_transitions */
876 	old -= IB_PORT_DOWN;
877 	new -= IB_PORT_DOWN;
878 
879 	if (old < 0 || new < 0)
880 		return HFI_TRANSITION_UNDEFINED;
881 	return logical_state_transitions.allowed[old][new];
882 }
883 
884 static int physical_transition_allowed(int old, int new)
885 {
886 	if (old < IB_PORTPHYSSTATE_NOP || old > OPA_PORTPHYSSTATE_MAX ||
887 	    new < IB_PORTPHYSSTATE_NOP || new > OPA_PORTPHYSSTATE_MAX) {
888 		pr_warn("invalid physical state(s) (old %d new %d)\n",
889 			old, new);
890 		return HFI_TRANSITION_UNDEFINED;
891 	}
892 
893 	if (new == IB_PORTPHYSSTATE_NOP)
894 		return HFI_TRANSITION_ALLOWED; /* always allowed */
895 
896 	/* adjust states for indexing into physical_state_transitions */
897 	old -= IB_PORTPHYSSTATE_POLLING;
898 	new -= IB_PORTPHYSSTATE_POLLING;
899 
900 	if (old < 0 || new < 0)
901 		return HFI_TRANSITION_UNDEFINED;
902 	return physical_state_transitions.allowed[old][new];
903 }
904 
905 static int port_states_transition_allowed(struct hfi1_pportdata *ppd,
906 					  u32 logical_new, u32 physical_new)
907 {
908 	u32 physical_old = driver_physical_state(ppd);
909 	u32 logical_old = driver_logical_state(ppd);
910 	int ret, logical_allowed, physical_allowed;
911 
912 	ret = logical_transition_allowed(logical_old, logical_new);
913 	logical_allowed = ret;
914 
915 	if (ret == HFI_TRANSITION_DISALLOWED ||
916 	    ret == HFI_TRANSITION_UNDEFINED) {
917 		pr_warn("invalid logical state transition %s -> %s\n",
918 			opa_lstate_name(logical_old),
919 			opa_lstate_name(logical_new));
920 		return ret;
921 	}
922 
923 	ret = physical_transition_allowed(physical_old, physical_new);
924 	physical_allowed = ret;
925 
926 	if (ret == HFI_TRANSITION_DISALLOWED ||
927 	    ret == HFI_TRANSITION_UNDEFINED) {
928 		pr_warn("invalid physical state transition %s -> %s\n",
929 			opa_pstate_name(physical_old),
930 			opa_pstate_name(physical_new));
931 		return ret;
932 	}
933 
934 	if (logical_allowed == HFI_TRANSITION_IGNORED &&
935 	    physical_allowed == HFI_TRANSITION_IGNORED)
936 		return HFI_TRANSITION_IGNORED;
937 
938 	/*
939 	 * A change request of Physical Port State from
940 	 * 'Offline' to 'Polling' should be ignored.
941 	 */
942 	if ((physical_old == OPA_PORTPHYSSTATE_OFFLINE) &&
943 	    (physical_new == IB_PORTPHYSSTATE_POLLING))
944 		return HFI_TRANSITION_IGNORED;
945 
946 	/*
947 	 * Either physical_allowed or logical_allowed is
948 	 * HFI_TRANSITION_ALLOWED.
949 	 */
950 	return HFI_TRANSITION_ALLOWED;
951 }
952 
953 static int set_port_states(struct hfi1_pportdata *ppd, struct opa_smp *smp,
954 			   u32 logical_state, u32 phys_state,
955 			   int suppress_idle_sma)
956 {
957 	struct hfi1_devdata *dd = ppd->dd;
958 	u32 link_state;
959 	int ret;
960 
961 	ret = port_states_transition_allowed(ppd, logical_state, phys_state);
962 	if (ret == HFI_TRANSITION_DISALLOWED ||
963 	    ret == HFI_TRANSITION_UNDEFINED) {
964 		/* error message emitted above */
965 		smp->status |= IB_SMP_INVALID_FIELD;
966 		return 0;
967 	}
968 
969 	if (ret == HFI_TRANSITION_IGNORED)
970 		return 0;
971 
972 	if ((phys_state != IB_PORTPHYSSTATE_NOP) &&
973 	    !(logical_state == IB_PORT_DOWN ||
974 	      logical_state == IB_PORT_NOP)){
975 		pr_warn("SubnSet(OPA_PortInfo) port state invalid: logical_state 0x%x physical_state 0x%x\n",
976 			logical_state, phys_state);
977 		smp->status |= IB_SMP_INVALID_FIELD;
978 	}
979 
980 	/*
981 	 * Logical state changes are summarized in OPAv1g1 spec.,
982 	 * Table 9-12; physical state changes are summarized in
983 	 * OPAv1g1 spec., Table 6.4.
984 	 */
985 	switch (logical_state) {
986 	case IB_PORT_NOP:
987 		if (phys_state == IB_PORTPHYSSTATE_NOP)
988 			break;
989 		/* FALLTHROUGH */
990 	case IB_PORT_DOWN:
991 		if (phys_state == IB_PORTPHYSSTATE_NOP) {
992 			link_state = HLS_DN_DOWNDEF;
993 		} else if (phys_state == IB_PORTPHYSSTATE_POLLING) {
994 			link_state = HLS_DN_POLL;
995 			set_link_down_reason(ppd, OPA_LINKDOWN_REASON_FM_BOUNCE,
996 					     0, OPA_LINKDOWN_REASON_FM_BOUNCE);
997 		} else if (phys_state == IB_PORTPHYSSTATE_DISABLED) {
998 			link_state = HLS_DN_DISABLE;
999 		} else {
1000 			pr_warn("SubnSet(OPA_PortInfo) invalid physical state 0x%x\n",
1001 				phys_state);
1002 			smp->status |= IB_SMP_INVALID_FIELD;
1003 			break;
1004 		}
1005 
1006 		if ((link_state == HLS_DN_POLL ||
1007 		     link_state == HLS_DN_DOWNDEF)) {
1008 			/*
1009 			 * Going to poll.  No matter what the current state,
1010 			 * always move offline first, then tune and start the
1011 			 * link.  This correctly handles a FM link bounce and
1012 			 * a link enable.  Going offline is a no-op if already
1013 			 * offline.
1014 			 */
1015 			set_link_state(ppd, HLS_DN_OFFLINE);
1016 			tune_serdes(ppd);
1017 			start_link(ppd);
1018 		} else {
1019 			set_link_state(ppd, link_state);
1020 		}
1021 		if (link_state == HLS_DN_DISABLE &&
1022 		    (ppd->offline_disabled_reason >
1023 		     HFI1_ODR_MASK(OPA_LINKDOWN_REASON_SMA_DISABLED) ||
1024 		     ppd->offline_disabled_reason ==
1025 		     HFI1_ODR_MASK(OPA_LINKDOWN_REASON_NONE)))
1026 			ppd->offline_disabled_reason =
1027 			HFI1_ODR_MASK(OPA_LINKDOWN_REASON_SMA_DISABLED);
1028 		/*
1029 		 * Don't send a reply if the response would be sent
1030 		 * through the disabled port.
1031 		 */
1032 		if (link_state == HLS_DN_DISABLE && smp->hop_cnt)
1033 			return IB_MAD_RESULT_SUCCESS | IB_MAD_RESULT_CONSUMED;
1034 		break;
1035 	case IB_PORT_ARMED:
1036 		ret = set_link_state(ppd, HLS_UP_ARMED);
1037 		if ((ret == 0) && (suppress_idle_sma == 0))
1038 			send_idle_sma(dd, SMA_IDLE_ARM);
1039 		break;
1040 	case IB_PORT_ACTIVE:
1041 		if (ppd->neighbor_normal) {
1042 			ret = set_link_state(ppd, HLS_UP_ACTIVE);
1043 			if (ret == 0)
1044 				send_idle_sma(dd, SMA_IDLE_ACTIVE);
1045 		} else {
1046 			pr_warn("SubnSet(OPA_PortInfo) Cannot move to Active with NeighborNormal 0\n");
1047 			smp->status |= IB_SMP_INVALID_FIELD;
1048 		}
1049 		break;
1050 	default:
1051 		pr_warn("SubnSet(OPA_PortInfo) invalid logical state 0x%x\n",
1052 			logical_state);
1053 		smp->status |= IB_SMP_INVALID_FIELD;
1054 	}
1055 
1056 	return 0;
1057 }
1058 
1059 /**
1060  * subn_set_opa_portinfo - set port information
1061  * @smp: the incoming SM packet
1062  * @ibdev: the infiniband device
1063  * @port: the port on the device
1064  *
1065  */
1066 static int __subn_set_opa_portinfo(struct opa_smp *smp, u32 am, u8 *data,
1067 				   struct ib_device *ibdev, u8 port,
1068 				   u32 *resp_len)
1069 {
1070 	struct opa_port_info *pi = (struct opa_port_info *)data;
1071 	struct ib_event event;
1072 	struct hfi1_devdata *dd;
1073 	struct hfi1_pportdata *ppd;
1074 	struct hfi1_ibport *ibp;
1075 	u8 clientrereg;
1076 	unsigned long flags;
1077 	u32 smlid, opa_lid; /* tmp vars to hold LID values */
1078 	u16 lid;
1079 	u8 ls_old, ls_new, ps_new;
1080 	u8 vls;
1081 	u8 msl;
1082 	u8 crc_enabled;
1083 	u16 lse, lwe, mtu;
1084 	u32 num_ports = OPA_AM_NPORT(am);
1085 	u32 start_of_sm_config = OPA_AM_START_SM_CFG(am);
1086 	int ret, i, invalid = 0, call_set_mtu = 0;
1087 	int call_link_downgrade_policy = 0;
1088 
1089 	if (num_ports != 1) {
1090 		smp->status |= IB_SMP_INVALID_FIELD;
1091 		return reply((struct ib_mad_hdr *)smp);
1092 	}
1093 
1094 	opa_lid = be32_to_cpu(pi->lid);
1095 	if (opa_lid & 0xFFFF0000) {
1096 		pr_warn("OPA_PortInfo lid out of range: %X\n", opa_lid);
1097 		smp->status |= IB_SMP_INVALID_FIELD;
1098 		goto get_only;
1099 	}
1100 
1101 	lid = (u16)(opa_lid & 0x0000FFFF);
1102 
1103 	smlid = be32_to_cpu(pi->sm_lid);
1104 	if (smlid & 0xFFFF0000) {
1105 		pr_warn("OPA_PortInfo SM lid out of range: %X\n", smlid);
1106 		smp->status |= IB_SMP_INVALID_FIELD;
1107 		goto get_only;
1108 	}
1109 	smlid &= 0x0000FFFF;
1110 
1111 	clientrereg = (pi->clientrereg_subnettimeout &
1112 			OPA_PI_MASK_CLIENT_REREGISTER);
1113 
1114 	dd = dd_from_ibdev(ibdev);
1115 	/* IB numbers ports from 1, hw from 0 */
1116 	ppd = dd->pport + (port - 1);
1117 	ibp = &ppd->ibport_data;
1118 	event.device = ibdev;
1119 	event.element.port_num = port;
1120 
1121 	ls_old = driver_lstate(ppd);
1122 
1123 	ibp->rvp.mkey = pi->mkey;
1124 	ibp->rvp.gid_prefix = pi->subnet_prefix;
1125 	ibp->rvp.mkey_lease_period = be16_to_cpu(pi->mkey_lease_period);
1126 
1127 	/* Must be a valid unicast LID address. */
1128 	if ((lid == 0 && ls_old > IB_PORT_INIT) ||
1129 	    lid >= be16_to_cpu(IB_MULTICAST_LID_BASE)) {
1130 		smp->status |= IB_SMP_INVALID_FIELD;
1131 		pr_warn("SubnSet(OPA_PortInfo) lid invalid 0x%x\n",
1132 			lid);
1133 	} else if (ppd->lid != lid ||
1134 		 ppd->lmc != (pi->mkeyprotect_lmc & OPA_PI_MASK_LMC)) {
1135 		if (ppd->lid != lid)
1136 			hfi1_set_uevent_bits(ppd, _HFI1_EVENT_LID_CHANGE_BIT);
1137 		if (ppd->lmc != (pi->mkeyprotect_lmc & OPA_PI_MASK_LMC))
1138 			hfi1_set_uevent_bits(ppd, _HFI1_EVENT_LMC_CHANGE_BIT);
1139 		hfi1_set_lid(ppd, lid, pi->mkeyprotect_lmc & OPA_PI_MASK_LMC);
1140 		event.event = IB_EVENT_LID_CHANGE;
1141 		ib_dispatch_event(&event);
1142 	}
1143 
1144 	msl = pi->smsl & OPA_PI_MASK_SMSL;
1145 	if (pi->partenforce_filterraw & OPA_PI_MASK_LINKINIT_REASON)
1146 		ppd->linkinit_reason =
1147 			(pi->partenforce_filterraw &
1148 			 OPA_PI_MASK_LINKINIT_REASON);
1149 	/* enable/disable SW pkey checking as per FM control */
1150 	if (pi->partenforce_filterraw & OPA_PI_MASK_PARTITION_ENFORCE_IN)
1151 		ppd->part_enforce |= HFI1_PART_ENFORCE_IN;
1152 	else
1153 		ppd->part_enforce &= ~HFI1_PART_ENFORCE_IN;
1154 
1155 	if (pi->partenforce_filterraw & OPA_PI_MASK_PARTITION_ENFORCE_OUT)
1156 		ppd->part_enforce |= HFI1_PART_ENFORCE_OUT;
1157 	else
1158 		ppd->part_enforce &= ~HFI1_PART_ENFORCE_OUT;
1159 
1160 	/* Must be a valid unicast LID address. */
1161 	if ((smlid == 0 && ls_old > IB_PORT_INIT) ||
1162 	    smlid >= be16_to_cpu(IB_MULTICAST_LID_BASE)) {
1163 		smp->status |= IB_SMP_INVALID_FIELD;
1164 		pr_warn("SubnSet(OPA_PortInfo) smlid invalid 0x%x\n", smlid);
1165 	} else if (smlid != ibp->rvp.sm_lid || msl != ibp->rvp.sm_sl) {
1166 		pr_warn("SubnSet(OPA_PortInfo) smlid 0x%x\n", smlid);
1167 		spin_lock_irqsave(&ibp->rvp.lock, flags);
1168 		if (ibp->rvp.sm_ah) {
1169 			if (smlid != ibp->rvp.sm_lid)
1170 				ibp->rvp.sm_ah->attr.dlid = smlid;
1171 			if (msl != ibp->rvp.sm_sl)
1172 				ibp->rvp.sm_ah->attr.sl = msl;
1173 		}
1174 		spin_unlock_irqrestore(&ibp->rvp.lock, flags);
1175 		if (smlid != ibp->rvp.sm_lid)
1176 			ibp->rvp.sm_lid = smlid;
1177 		if (msl != ibp->rvp.sm_sl)
1178 			ibp->rvp.sm_sl = msl;
1179 		event.event = IB_EVENT_SM_CHANGE;
1180 		ib_dispatch_event(&event);
1181 	}
1182 
1183 	if (pi->link_down_reason == 0) {
1184 		ppd->local_link_down_reason.sma = 0;
1185 		ppd->local_link_down_reason.latest = 0;
1186 	}
1187 
1188 	if (pi->neigh_link_down_reason == 0) {
1189 		ppd->neigh_link_down_reason.sma = 0;
1190 		ppd->neigh_link_down_reason.latest = 0;
1191 	}
1192 
1193 	ppd->sm_trap_qp = be32_to_cpu(pi->sm_trap_qp);
1194 	ppd->sa_qp = be32_to_cpu(pi->sa_qp);
1195 
1196 	ppd->port_error_action = be32_to_cpu(pi->port_error_action);
1197 	lwe = be16_to_cpu(pi->link_width.enabled);
1198 	if (lwe) {
1199 		if (lwe == OPA_LINK_WIDTH_RESET ||
1200 		    lwe == OPA_LINK_WIDTH_RESET_OLD)
1201 			set_link_width_enabled(ppd, ppd->link_width_supported);
1202 		else if ((lwe & ~ppd->link_width_supported) == 0)
1203 			set_link_width_enabled(ppd, lwe);
1204 		else
1205 			smp->status |= IB_SMP_INVALID_FIELD;
1206 	}
1207 	lwe = be16_to_cpu(pi->link_width_downgrade.enabled);
1208 	/* LWD.E is always applied - 0 means "disabled" */
1209 	if (lwe == OPA_LINK_WIDTH_RESET ||
1210 	    lwe == OPA_LINK_WIDTH_RESET_OLD) {
1211 		set_link_width_downgrade_enabled(ppd,
1212 						 ppd->
1213 						 link_width_downgrade_supported
1214 						 );
1215 	} else if ((lwe & ~ppd->link_width_downgrade_supported) == 0) {
1216 		/* only set and apply if something changed */
1217 		if (lwe != ppd->link_width_downgrade_enabled) {
1218 			set_link_width_downgrade_enabled(ppd, lwe);
1219 			call_link_downgrade_policy = 1;
1220 		}
1221 	} else {
1222 		smp->status |= IB_SMP_INVALID_FIELD;
1223 	}
1224 	lse = be16_to_cpu(pi->link_speed.enabled);
1225 	if (lse) {
1226 		if (lse & be16_to_cpu(pi->link_speed.supported))
1227 			set_link_speed_enabled(ppd, lse);
1228 		else
1229 			smp->status |= IB_SMP_INVALID_FIELD;
1230 	}
1231 
1232 	ibp->rvp.mkeyprot =
1233 		(pi->mkeyprotect_lmc & OPA_PI_MASK_MKEY_PROT_BIT) >> 6;
1234 	ibp->rvp.vl_high_limit = be16_to_cpu(pi->vl.high_limit) & 0xFF;
1235 	(void)hfi1_set_ib_cfg(ppd, HFI1_IB_CFG_VL_HIGH_LIMIT,
1236 				    ibp->rvp.vl_high_limit);
1237 
1238 	if (ppd->vls_supported / 2 > ARRAY_SIZE(pi->neigh_mtu.pvlx_to_mtu) ||
1239 	    ppd->vls_supported > ARRAY_SIZE(dd->vld)) {
1240 		smp->status |= IB_SMP_INVALID_FIELD;
1241 		return reply((struct ib_mad_hdr *)smp);
1242 	}
1243 	for (i = 0; i < ppd->vls_supported; i++) {
1244 		if ((i % 2) == 0)
1245 			mtu = enum_to_mtu((pi->neigh_mtu.pvlx_to_mtu[i / 2] >>
1246 					   4) & 0xF);
1247 		else
1248 			mtu = enum_to_mtu(pi->neigh_mtu.pvlx_to_mtu[i / 2] &
1249 					  0xF);
1250 		if (mtu == 0xffff) {
1251 			pr_warn("SubnSet(OPA_PortInfo) mtu invalid %d (0x%x)\n",
1252 				mtu,
1253 				(pi->neigh_mtu.pvlx_to_mtu[0] >> 4) & 0xF);
1254 			smp->status |= IB_SMP_INVALID_FIELD;
1255 			mtu = hfi1_max_mtu; /* use a valid MTU */
1256 		}
1257 		if (dd->vld[i].mtu != mtu) {
1258 			dd_dev_info(dd,
1259 				    "MTU change on vl %d from %d to %d\n",
1260 				    i, dd->vld[i].mtu, mtu);
1261 			dd->vld[i].mtu = mtu;
1262 			call_set_mtu++;
1263 		}
1264 	}
1265 	/* As per OPAV1 spec: VL15 must support and be configured
1266 	 * for operation with a 2048 or larger MTU.
1267 	 */
1268 	mtu = enum_to_mtu(pi->neigh_mtu.pvlx_to_mtu[15 / 2] & 0xF);
1269 	if (mtu < 2048 || mtu == 0xffff)
1270 		mtu = 2048;
1271 	if (dd->vld[15].mtu != mtu) {
1272 		dd_dev_info(dd,
1273 			    "MTU change on vl 15 from %d to %d\n",
1274 			    dd->vld[15].mtu, mtu);
1275 		dd->vld[15].mtu = mtu;
1276 		call_set_mtu++;
1277 	}
1278 	if (call_set_mtu)
1279 		set_mtu(ppd);
1280 
1281 	/* Set operational VLs */
1282 	vls = pi->operational_vls & OPA_PI_MASK_OPERATIONAL_VL;
1283 	if (vls) {
1284 		if (vls > ppd->vls_supported) {
1285 			pr_warn("SubnSet(OPA_PortInfo) VL's supported invalid %d\n",
1286 				pi->operational_vls);
1287 			smp->status |= IB_SMP_INVALID_FIELD;
1288 		} else {
1289 			if (hfi1_set_ib_cfg(ppd, HFI1_IB_CFG_OP_VLS,
1290 					    vls) == -EINVAL)
1291 				smp->status |= IB_SMP_INVALID_FIELD;
1292 		}
1293 	}
1294 
1295 	if (pi->mkey_violations == 0)
1296 		ibp->rvp.mkey_violations = 0;
1297 
1298 	if (pi->pkey_violations == 0)
1299 		ibp->rvp.pkey_violations = 0;
1300 
1301 	if (pi->qkey_violations == 0)
1302 		ibp->rvp.qkey_violations = 0;
1303 
1304 	ibp->rvp.subnet_timeout =
1305 		pi->clientrereg_subnettimeout & OPA_PI_MASK_SUBNET_TIMEOUT;
1306 
1307 	crc_enabled = be16_to_cpu(pi->port_ltp_crc_mode);
1308 	crc_enabled >>= 4;
1309 	crc_enabled &= 0xf;
1310 
1311 	if (crc_enabled != 0)
1312 		ppd->port_crc_mode_enabled = port_ltp_to_cap(crc_enabled);
1313 
1314 	ppd->is_active_optimize_enabled =
1315 			!!(be16_to_cpu(pi->port_mode)
1316 					& OPA_PI_MASK_PORT_ACTIVE_OPTOMIZE);
1317 
1318 	ls_new = pi->port_states.portphysstate_portstate &
1319 			OPA_PI_MASK_PORT_STATE;
1320 	ps_new = (pi->port_states.portphysstate_portstate &
1321 			OPA_PI_MASK_PORT_PHYSICAL_STATE) >> 4;
1322 
1323 	if (ls_old == IB_PORT_INIT) {
1324 		if (start_of_sm_config) {
1325 			if (ls_new == ls_old || (ls_new == IB_PORT_ARMED))
1326 				ppd->is_sm_config_started = 1;
1327 		} else if (ls_new == IB_PORT_ARMED) {
1328 			if (ppd->is_sm_config_started == 0)
1329 				invalid = 1;
1330 		}
1331 	}
1332 
1333 	/* Handle CLIENT_REREGISTER event b/c SM asked us for it */
1334 	if (clientrereg) {
1335 		event.event = IB_EVENT_CLIENT_REREGISTER;
1336 		ib_dispatch_event(&event);
1337 	}
1338 
1339 	/*
1340 	 * Do the port state change now that the other link parameters
1341 	 * have been set.
1342 	 * Changing the port physical state only makes sense if the link
1343 	 * is down or is being set to down.
1344 	 */
1345 
1346 	ret = set_port_states(ppd, smp, ls_new, ps_new, invalid);
1347 	if (ret)
1348 		return ret;
1349 
1350 	ret = __subn_get_opa_portinfo(smp, am, data, ibdev, port, resp_len);
1351 
1352 	/* restore re-reg bit per o14-12.2.1 */
1353 	pi->clientrereg_subnettimeout |= clientrereg;
1354 
1355 	/*
1356 	 * Apply the new link downgrade policy.  This may result in a link
1357 	 * bounce.  Do this after everything else so things are settled.
1358 	 * Possible problem: if setting the port state above fails, then
1359 	 * the policy change is not applied.
1360 	 */
1361 	if (call_link_downgrade_policy)
1362 		apply_link_downgrade_policy(ppd, 0);
1363 
1364 	return ret;
1365 
1366 get_only:
1367 	return __subn_get_opa_portinfo(smp, am, data, ibdev, port, resp_len);
1368 }
1369 
1370 /**
1371  * set_pkeys - set the PKEY table for ctxt 0
1372  * @dd: the hfi1_ib device
1373  * @port: the IB port number
1374  * @pkeys: the PKEY table
1375  */
1376 static int set_pkeys(struct hfi1_devdata *dd, u8 port, u16 *pkeys)
1377 {
1378 	struct hfi1_pportdata *ppd;
1379 	int i;
1380 	int changed = 0;
1381 	int update_includes_mgmt_partition = 0;
1382 
1383 	/*
1384 	 * IB port one/two always maps to context zero/one,
1385 	 * always a kernel context, no locking needed
1386 	 * If we get here with ppd setup, no need to check
1387 	 * that rcd is valid.
1388 	 */
1389 	ppd = dd->pport + (port - 1);
1390 	/*
1391 	 * If the update does not include the management pkey, don't do it.
1392 	 */
1393 	for (i = 0; i < ARRAY_SIZE(ppd->pkeys); i++) {
1394 		if (pkeys[i] == LIM_MGMT_P_KEY) {
1395 			update_includes_mgmt_partition = 1;
1396 			break;
1397 		}
1398 	}
1399 
1400 	if (!update_includes_mgmt_partition)
1401 		return 1;
1402 
1403 	for (i = 0; i < ARRAY_SIZE(ppd->pkeys); i++) {
1404 		u16 key = pkeys[i];
1405 		u16 okey = ppd->pkeys[i];
1406 
1407 		if (key == okey)
1408 			continue;
1409 		/*
1410 		 * Don't update pkeys[2], if an HFI port without MgmtAllowed
1411 		 * by neighbor is a switch.
1412 		 */
1413 		if (i == 2 && !ppd->mgmt_allowed && ppd->neighbor_type == 1)
1414 			continue;
1415 		/*
1416 		 * The SM gives us the complete PKey table. We have
1417 		 * to ensure that we put the PKeys in the matching
1418 		 * slots.
1419 		 */
1420 		ppd->pkeys[i] = key;
1421 		changed = 1;
1422 	}
1423 
1424 	if (changed) {
1425 		(void)hfi1_set_ib_cfg(ppd, HFI1_IB_CFG_PKEYS, 0);
1426 		hfi1_event_pkey_change(dd, port);
1427 	}
1428 
1429 	return 0;
1430 }
1431 
1432 static int __subn_set_opa_pkeytable(struct opa_smp *smp, u32 am, u8 *data,
1433 				    struct ib_device *ibdev, u8 port,
1434 				    u32 *resp_len)
1435 {
1436 	struct hfi1_devdata *dd = dd_from_ibdev(ibdev);
1437 	u32 n_blocks_sent = OPA_AM_NBLK(am);
1438 	u32 start_block = am & 0x7ff;
1439 	u16 *p = (u16 *)data;
1440 	__be16 *q = (__be16 *)data;
1441 	int i;
1442 	u16 n_blocks_avail;
1443 	unsigned npkeys = hfi1_get_npkeys(dd);
1444 
1445 	if (n_blocks_sent == 0) {
1446 		pr_warn("OPA Get PKey AM Invalid : P = %d; B = 0x%x; N = 0x%x\n",
1447 			port, start_block, n_blocks_sent);
1448 		smp->status |= IB_SMP_INVALID_FIELD;
1449 		return reply((struct ib_mad_hdr *)smp);
1450 	}
1451 
1452 	n_blocks_avail = (u16)(npkeys / OPA_PARTITION_TABLE_BLK_SIZE) + 1;
1453 
1454 	if (start_block + n_blocks_sent > n_blocks_avail ||
1455 	    n_blocks_sent > OPA_NUM_PKEY_BLOCKS_PER_SMP) {
1456 		pr_warn("OPA Set PKey AM Invalid : s 0x%x; req 0x%x; avail 0x%x; blk/smp 0x%lx\n",
1457 			start_block, n_blocks_sent, n_blocks_avail,
1458 			OPA_NUM_PKEY_BLOCKS_PER_SMP);
1459 		smp->status |= IB_SMP_INVALID_FIELD;
1460 		return reply((struct ib_mad_hdr *)smp);
1461 	}
1462 
1463 	for (i = 0; i < n_blocks_sent * OPA_PARTITION_TABLE_BLK_SIZE; i++)
1464 		p[i] = be16_to_cpu(q[i]);
1465 
1466 	if (start_block == 0 && set_pkeys(dd, port, p) != 0) {
1467 		smp->status |= IB_SMP_INVALID_FIELD;
1468 		return reply((struct ib_mad_hdr *)smp);
1469 	}
1470 
1471 	return __subn_get_opa_pkeytable(smp, am, data, ibdev, port, resp_len);
1472 }
1473 
1474 static int get_sc2vlt_tables(struct hfi1_devdata *dd, void *data)
1475 {
1476 	u64 *val = data;
1477 
1478 	*val++ = read_csr(dd, SEND_SC2VLT0);
1479 	*val++ = read_csr(dd, SEND_SC2VLT1);
1480 	*val++ = read_csr(dd, SEND_SC2VLT2);
1481 	*val++ = read_csr(dd, SEND_SC2VLT3);
1482 	return 0;
1483 }
1484 
1485 #define ILLEGAL_VL 12
1486 /*
1487  * filter_sc2vlt changes mappings to VL15 to ILLEGAL_VL (except
1488  * for SC15, which must map to VL15). If we don't remap things this
1489  * way it is possible for VL15 counters to increment when we try to
1490  * send on a SC which is mapped to an invalid VL.
1491  */
1492 static void filter_sc2vlt(void *data)
1493 {
1494 	int i;
1495 	u8 *pd = data;
1496 
1497 	for (i = 0; i < OPA_MAX_SCS; i++) {
1498 		if (i == 15)
1499 			continue;
1500 		if ((pd[i] & 0x1f) == 0xf)
1501 			pd[i] = ILLEGAL_VL;
1502 	}
1503 }
1504 
1505 static int set_sc2vlt_tables(struct hfi1_devdata *dd, void *data)
1506 {
1507 	u64 *val = data;
1508 
1509 	filter_sc2vlt(data);
1510 
1511 	write_csr(dd, SEND_SC2VLT0, *val++);
1512 	write_csr(dd, SEND_SC2VLT1, *val++);
1513 	write_csr(dd, SEND_SC2VLT2, *val++);
1514 	write_csr(dd, SEND_SC2VLT3, *val++);
1515 	write_seqlock_irq(&dd->sc2vl_lock);
1516 	memcpy(dd->sc2vl, data, sizeof(dd->sc2vl));
1517 	write_sequnlock_irq(&dd->sc2vl_lock);
1518 	return 0;
1519 }
1520 
1521 static int __subn_get_opa_sl_to_sc(struct opa_smp *smp, u32 am, u8 *data,
1522 				   struct ib_device *ibdev, u8 port,
1523 				   u32 *resp_len)
1524 {
1525 	struct hfi1_ibport *ibp = to_iport(ibdev, port);
1526 	u8 *p = data;
1527 	size_t size = ARRAY_SIZE(ibp->sl_to_sc); /* == 32 */
1528 	unsigned i;
1529 
1530 	if (am) {
1531 		smp->status |= IB_SMP_INVALID_FIELD;
1532 		return reply((struct ib_mad_hdr *)smp);
1533 	}
1534 
1535 	for (i = 0; i < ARRAY_SIZE(ibp->sl_to_sc); i++)
1536 		*p++ = ibp->sl_to_sc[i];
1537 
1538 	if (resp_len)
1539 		*resp_len += size;
1540 
1541 	return reply((struct ib_mad_hdr *)smp);
1542 }
1543 
1544 static int __subn_set_opa_sl_to_sc(struct opa_smp *smp, u32 am, u8 *data,
1545 				   struct ib_device *ibdev, u8 port,
1546 				   u32 *resp_len)
1547 {
1548 	struct hfi1_ibport *ibp = to_iport(ibdev, port);
1549 	u8 *p = data;
1550 	int i;
1551 	u8 sc;
1552 
1553 	if (am) {
1554 		smp->status |= IB_SMP_INVALID_FIELD;
1555 		return reply((struct ib_mad_hdr *)smp);
1556 	}
1557 
1558 	for (i = 0; i <  ARRAY_SIZE(ibp->sl_to_sc); i++) {
1559 		sc = *p++;
1560 		if (ibp->sl_to_sc[i] != sc) {
1561 			ibp->sl_to_sc[i] = sc;
1562 
1563 			/* Put all stale qps into error state */
1564 			hfi1_error_port_qps(ibp, i);
1565 		}
1566 	}
1567 
1568 	return __subn_get_opa_sl_to_sc(smp, am, data, ibdev, port, resp_len);
1569 }
1570 
1571 static int __subn_get_opa_sc_to_sl(struct opa_smp *smp, u32 am, u8 *data,
1572 				   struct ib_device *ibdev, u8 port,
1573 				   u32 *resp_len)
1574 {
1575 	struct hfi1_ibport *ibp = to_iport(ibdev, port);
1576 	u8 *p = data;
1577 	size_t size = ARRAY_SIZE(ibp->sc_to_sl); /* == 32 */
1578 	unsigned i;
1579 
1580 	if (am) {
1581 		smp->status |= IB_SMP_INVALID_FIELD;
1582 		return reply((struct ib_mad_hdr *)smp);
1583 	}
1584 
1585 	for (i = 0; i < ARRAY_SIZE(ibp->sc_to_sl); i++)
1586 		*p++ = ibp->sc_to_sl[i];
1587 
1588 	if (resp_len)
1589 		*resp_len += size;
1590 
1591 	return reply((struct ib_mad_hdr *)smp);
1592 }
1593 
1594 static int __subn_set_opa_sc_to_sl(struct opa_smp *smp, u32 am, u8 *data,
1595 				   struct ib_device *ibdev, u8 port,
1596 				   u32 *resp_len)
1597 {
1598 	struct hfi1_ibport *ibp = to_iport(ibdev, port);
1599 	u8 *p = data;
1600 	int i;
1601 
1602 	if (am) {
1603 		smp->status |= IB_SMP_INVALID_FIELD;
1604 		return reply((struct ib_mad_hdr *)smp);
1605 	}
1606 
1607 	for (i = 0; i < ARRAY_SIZE(ibp->sc_to_sl); i++)
1608 		ibp->sc_to_sl[i] = *p++;
1609 
1610 	return __subn_get_opa_sc_to_sl(smp, am, data, ibdev, port, resp_len);
1611 }
1612 
1613 static int __subn_get_opa_sc_to_vlt(struct opa_smp *smp, u32 am, u8 *data,
1614 				    struct ib_device *ibdev, u8 port,
1615 				    u32 *resp_len)
1616 {
1617 	u32 n_blocks = OPA_AM_NBLK(am);
1618 	struct hfi1_devdata *dd = dd_from_ibdev(ibdev);
1619 	void *vp = (void *)data;
1620 	size_t size = 4 * sizeof(u64);
1621 
1622 	if (n_blocks != 1) {
1623 		smp->status |= IB_SMP_INVALID_FIELD;
1624 		return reply((struct ib_mad_hdr *)smp);
1625 	}
1626 
1627 	get_sc2vlt_tables(dd, vp);
1628 
1629 	if (resp_len)
1630 		*resp_len += size;
1631 
1632 	return reply((struct ib_mad_hdr *)smp);
1633 }
1634 
1635 static int __subn_set_opa_sc_to_vlt(struct opa_smp *smp, u32 am, u8 *data,
1636 				    struct ib_device *ibdev, u8 port,
1637 				    u32 *resp_len)
1638 {
1639 	u32 n_blocks = OPA_AM_NBLK(am);
1640 	int async_update = OPA_AM_ASYNC(am);
1641 	struct hfi1_devdata *dd = dd_from_ibdev(ibdev);
1642 	void *vp = (void *)data;
1643 	struct hfi1_pportdata *ppd;
1644 	int lstate;
1645 
1646 	if (n_blocks != 1 || async_update) {
1647 		smp->status |= IB_SMP_INVALID_FIELD;
1648 		return reply((struct ib_mad_hdr *)smp);
1649 	}
1650 
1651 	/* IB numbers ports from 1, hw from 0 */
1652 	ppd = dd->pport + (port - 1);
1653 	lstate = driver_lstate(ppd);
1654 	/*
1655 	 * it's known that async_update is 0 by this point, but include
1656 	 * the explicit check for clarity
1657 	 */
1658 	if (!async_update &&
1659 	    (lstate == IB_PORT_ARMED || lstate == IB_PORT_ACTIVE)) {
1660 		smp->status |= IB_SMP_INVALID_FIELD;
1661 		return reply((struct ib_mad_hdr *)smp);
1662 	}
1663 
1664 	set_sc2vlt_tables(dd, vp);
1665 
1666 	return __subn_get_opa_sc_to_vlt(smp, am, data, ibdev, port, resp_len);
1667 }
1668 
1669 static int __subn_get_opa_sc_to_vlnt(struct opa_smp *smp, u32 am, u8 *data,
1670 				     struct ib_device *ibdev, u8 port,
1671 				     u32 *resp_len)
1672 {
1673 	u32 n_blocks = OPA_AM_NPORT(am);
1674 	struct hfi1_devdata *dd = dd_from_ibdev(ibdev);
1675 	struct hfi1_pportdata *ppd;
1676 	void *vp = (void *)data;
1677 	int size;
1678 
1679 	if (n_blocks != 1) {
1680 		smp->status |= IB_SMP_INVALID_FIELD;
1681 		return reply((struct ib_mad_hdr *)smp);
1682 	}
1683 
1684 	ppd = dd->pport + (port - 1);
1685 
1686 	size = fm_get_table(ppd, FM_TBL_SC2VLNT, vp);
1687 
1688 	if (resp_len)
1689 		*resp_len += size;
1690 
1691 	return reply((struct ib_mad_hdr *)smp);
1692 }
1693 
1694 static int __subn_set_opa_sc_to_vlnt(struct opa_smp *smp, u32 am, u8 *data,
1695 				     struct ib_device *ibdev, u8 port,
1696 				     u32 *resp_len)
1697 {
1698 	u32 n_blocks = OPA_AM_NPORT(am);
1699 	struct hfi1_devdata *dd = dd_from_ibdev(ibdev);
1700 	struct hfi1_pportdata *ppd;
1701 	void *vp = (void *)data;
1702 	int lstate;
1703 
1704 	if (n_blocks != 1) {
1705 		smp->status |= IB_SMP_INVALID_FIELD;
1706 		return reply((struct ib_mad_hdr *)smp);
1707 	}
1708 
1709 	/* IB numbers ports from 1, hw from 0 */
1710 	ppd = dd->pport + (port - 1);
1711 	lstate = driver_lstate(ppd);
1712 	if (lstate == IB_PORT_ARMED || lstate == IB_PORT_ACTIVE) {
1713 		smp->status |= IB_SMP_INVALID_FIELD;
1714 		return reply((struct ib_mad_hdr *)smp);
1715 	}
1716 
1717 	ppd = dd->pport + (port - 1);
1718 
1719 	fm_set_table(ppd, FM_TBL_SC2VLNT, vp);
1720 
1721 	return __subn_get_opa_sc_to_vlnt(smp, am, data, ibdev, port,
1722 					 resp_len);
1723 }
1724 
1725 static int __subn_get_opa_psi(struct opa_smp *smp, u32 am, u8 *data,
1726 			      struct ib_device *ibdev, u8 port,
1727 			      u32 *resp_len)
1728 {
1729 	u32 nports = OPA_AM_NPORT(am);
1730 	u32 start_of_sm_config = OPA_AM_START_SM_CFG(am);
1731 	u32 lstate;
1732 	struct hfi1_ibport *ibp;
1733 	struct hfi1_pportdata *ppd;
1734 	struct opa_port_state_info *psi = (struct opa_port_state_info *)data;
1735 
1736 	if (nports != 1) {
1737 		smp->status |= IB_SMP_INVALID_FIELD;
1738 		return reply((struct ib_mad_hdr *)smp);
1739 	}
1740 
1741 	ibp = to_iport(ibdev, port);
1742 	ppd = ppd_from_ibp(ibp);
1743 
1744 	lstate = driver_lstate(ppd);
1745 
1746 	if (start_of_sm_config && (lstate == IB_PORT_INIT))
1747 		ppd->is_sm_config_started = 1;
1748 
1749 	psi->port_states.ledenable_offlinereason = ppd->neighbor_normal << 4;
1750 	psi->port_states.ledenable_offlinereason |=
1751 		ppd->is_sm_config_started << 5;
1752 	psi->port_states.ledenable_offlinereason |=
1753 		ppd->offline_disabled_reason;
1754 
1755 	psi->port_states.portphysstate_portstate =
1756 		(hfi1_ibphys_portstate(ppd) << 4) | (lstate & 0xf);
1757 	psi->link_width_downgrade_tx_active =
1758 		cpu_to_be16(ppd->link_width_downgrade_tx_active);
1759 	psi->link_width_downgrade_rx_active =
1760 		cpu_to_be16(ppd->link_width_downgrade_rx_active);
1761 	if (resp_len)
1762 		*resp_len += sizeof(struct opa_port_state_info);
1763 
1764 	return reply((struct ib_mad_hdr *)smp);
1765 }
1766 
1767 static int __subn_set_opa_psi(struct opa_smp *smp, u32 am, u8 *data,
1768 			      struct ib_device *ibdev, u8 port,
1769 			      u32 *resp_len)
1770 {
1771 	u32 nports = OPA_AM_NPORT(am);
1772 	u32 start_of_sm_config = OPA_AM_START_SM_CFG(am);
1773 	u32 ls_old;
1774 	u8 ls_new, ps_new;
1775 	struct hfi1_ibport *ibp;
1776 	struct hfi1_pportdata *ppd;
1777 	struct opa_port_state_info *psi = (struct opa_port_state_info *)data;
1778 	int ret, invalid = 0;
1779 
1780 	if (nports != 1) {
1781 		smp->status |= IB_SMP_INVALID_FIELD;
1782 		return reply((struct ib_mad_hdr *)smp);
1783 	}
1784 
1785 	ibp = to_iport(ibdev, port);
1786 	ppd = ppd_from_ibp(ibp);
1787 
1788 	ls_old = driver_lstate(ppd);
1789 
1790 	ls_new = port_states_to_logical_state(&psi->port_states);
1791 	ps_new = port_states_to_phys_state(&psi->port_states);
1792 
1793 	if (ls_old == IB_PORT_INIT) {
1794 		if (start_of_sm_config) {
1795 			if (ls_new == ls_old || (ls_new == IB_PORT_ARMED))
1796 				ppd->is_sm_config_started = 1;
1797 		} else if (ls_new == IB_PORT_ARMED) {
1798 			if (ppd->is_sm_config_started == 0)
1799 				invalid = 1;
1800 		}
1801 	}
1802 
1803 	ret = set_port_states(ppd, smp, ls_new, ps_new, invalid);
1804 	if (ret)
1805 		return ret;
1806 
1807 	if (invalid)
1808 		smp->status |= IB_SMP_INVALID_FIELD;
1809 
1810 	return __subn_get_opa_psi(smp, am, data, ibdev, port, resp_len);
1811 }
1812 
1813 static int __subn_get_opa_cable_info(struct opa_smp *smp, u32 am, u8 *data,
1814 				     struct ib_device *ibdev, u8 port,
1815 				     u32 *resp_len)
1816 {
1817 	struct hfi1_devdata *dd = dd_from_ibdev(ibdev);
1818 	u32 addr = OPA_AM_CI_ADDR(am);
1819 	u32 len = OPA_AM_CI_LEN(am) + 1;
1820 	int ret;
1821 
1822 #define __CI_PAGE_SIZE BIT(7) /* 128 bytes */
1823 #define __CI_PAGE_MASK ~(__CI_PAGE_SIZE - 1)
1824 #define __CI_PAGE_NUM(a) ((a) & __CI_PAGE_MASK)
1825 
1826 	/*
1827 	 * check that addr is within spec, and
1828 	 * addr and (addr + len - 1) are on the same "page"
1829 	 */
1830 	if (addr >= 4096 ||
1831 	    (__CI_PAGE_NUM(addr) != __CI_PAGE_NUM(addr + len - 1))) {
1832 		smp->status |= IB_SMP_INVALID_FIELD;
1833 		return reply((struct ib_mad_hdr *)smp);
1834 	}
1835 
1836 	ret = get_cable_info(dd, port, addr, len, data);
1837 
1838 	if (ret == -ENODEV) {
1839 		smp->status |= IB_SMP_UNSUP_METH_ATTR;
1840 		return reply((struct ib_mad_hdr *)smp);
1841 	}
1842 
1843 	/* The address range for the CableInfo SMA query is wider than the
1844 	 * memory available on the QSFP cable. We want to return a valid
1845 	 * response, albeit zeroed out, for address ranges beyond available
1846 	 * memory but that are within the CableInfo query spec
1847 	 */
1848 	if (ret < 0 && ret != -ERANGE) {
1849 		smp->status |= IB_SMP_INVALID_FIELD;
1850 		return reply((struct ib_mad_hdr *)smp);
1851 	}
1852 
1853 	if (resp_len)
1854 		*resp_len += len;
1855 
1856 	return reply((struct ib_mad_hdr *)smp);
1857 }
1858 
1859 static int __subn_get_opa_bct(struct opa_smp *smp, u32 am, u8 *data,
1860 			      struct ib_device *ibdev, u8 port, u32 *resp_len)
1861 {
1862 	u32 num_ports = OPA_AM_NPORT(am);
1863 	struct hfi1_devdata *dd = dd_from_ibdev(ibdev);
1864 	struct hfi1_pportdata *ppd;
1865 	struct buffer_control *p = (struct buffer_control *)data;
1866 	int size;
1867 
1868 	if (num_ports != 1) {
1869 		smp->status |= IB_SMP_INVALID_FIELD;
1870 		return reply((struct ib_mad_hdr *)smp);
1871 	}
1872 
1873 	ppd = dd->pport + (port - 1);
1874 	size = fm_get_table(ppd, FM_TBL_BUFFER_CONTROL, p);
1875 	trace_bct_get(dd, p);
1876 	if (resp_len)
1877 		*resp_len += size;
1878 
1879 	return reply((struct ib_mad_hdr *)smp);
1880 }
1881 
1882 static int __subn_set_opa_bct(struct opa_smp *smp, u32 am, u8 *data,
1883 			      struct ib_device *ibdev, u8 port, u32 *resp_len)
1884 {
1885 	u32 num_ports = OPA_AM_NPORT(am);
1886 	struct hfi1_devdata *dd = dd_from_ibdev(ibdev);
1887 	struct hfi1_pportdata *ppd;
1888 	struct buffer_control *p = (struct buffer_control *)data;
1889 
1890 	if (num_ports != 1) {
1891 		smp->status |= IB_SMP_INVALID_FIELD;
1892 		return reply((struct ib_mad_hdr *)smp);
1893 	}
1894 	ppd = dd->pport + (port - 1);
1895 	trace_bct_set(dd, p);
1896 	if (fm_set_table(ppd, FM_TBL_BUFFER_CONTROL, p) < 0) {
1897 		smp->status |= IB_SMP_INVALID_FIELD;
1898 		return reply((struct ib_mad_hdr *)smp);
1899 	}
1900 
1901 	return __subn_get_opa_bct(smp, am, data, ibdev, port, resp_len);
1902 }
1903 
1904 static int __subn_get_opa_vl_arb(struct opa_smp *smp, u32 am, u8 *data,
1905 				 struct ib_device *ibdev, u8 port,
1906 				 u32 *resp_len)
1907 {
1908 	struct hfi1_pportdata *ppd = ppd_from_ibp(to_iport(ibdev, port));
1909 	u32 num_ports = OPA_AM_NPORT(am);
1910 	u8 section = (am & 0x00ff0000) >> 16;
1911 	u8 *p = data;
1912 	int size = 0;
1913 
1914 	if (num_ports != 1) {
1915 		smp->status |= IB_SMP_INVALID_FIELD;
1916 		return reply((struct ib_mad_hdr *)smp);
1917 	}
1918 
1919 	switch (section) {
1920 	case OPA_VLARB_LOW_ELEMENTS:
1921 		size = fm_get_table(ppd, FM_TBL_VL_LOW_ARB, p);
1922 		break;
1923 	case OPA_VLARB_HIGH_ELEMENTS:
1924 		size = fm_get_table(ppd, FM_TBL_VL_HIGH_ARB, p);
1925 		break;
1926 	case OPA_VLARB_PREEMPT_ELEMENTS:
1927 		size = fm_get_table(ppd, FM_TBL_VL_PREEMPT_ELEMS, p);
1928 		break;
1929 	case OPA_VLARB_PREEMPT_MATRIX:
1930 		size = fm_get_table(ppd, FM_TBL_VL_PREEMPT_MATRIX, p);
1931 		break;
1932 	default:
1933 		pr_warn("OPA SubnGet(VL Arb) AM Invalid : 0x%x\n",
1934 			be32_to_cpu(smp->attr_mod));
1935 		smp->status |= IB_SMP_INVALID_FIELD;
1936 		break;
1937 	}
1938 
1939 	if (size > 0 && resp_len)
1940 		*resp_len += size;
1941 
1942 	return reply((struct ib_mad_hdr *)smp);
1943 }
1944 
1945 static int __subn_set_opa_vl_arb(struct opa_smp *smp, u32 am, u8 *data,
1946 				 struct ib_device *ibdev, u8 port,
1947 				 u32 *resp_len)
1948 {
1949 	struct hfi1_pportdata *ppd = ppd_from_ibp(to_iport(ibdev, port));
1950 	u32 num_ports = OPA_AM_NPORT(am);
1951 	u8 section = (am & 0x00ff0000) >> 16;
1952 	u8 *p = data;
1953 
1954 	if (num_ports != 1) {
1955 		smp->status |= IB_SMP_INVALID_FIELD;
1956 		return reply((struct ib_mad_hdr *)smp);
1957 	}
1958 
1959 	switch (section) {
1960 	case OPA_VLARB_LOW_ELEMENTS:
1961 		(void)fm_set_table(ppd, FM_TBL_VL_LOW_ARB, p);
1962 		break;
1963 	case OPA_VLARB_HIGH_ELEMENTS:
1964 		(void)fm_set_table(ppd, FM_TBL_VL_HIGH_ARB, p);
1965 		break;
1966 	/*
1967 	 * neither OPA_VLARB_PREEMPT_ELEMENTS, or OPA_VLARB_PREEMPT_MATRIX
1968 	 * can be changed from the default values
1969 	 */
1970 	case OPA_VLARB_PREEMPT_ELEMENTS:
1971 		/* FALLTHROUGH */
1972 	case OPA_VLARB_PREEMPT_MATRIX:
1973 		smp->status |= IB_SMP_UNSUP_METH_ATTR;
1974 		break;
1975 	default:
1976 		pr_warn("OPA SubnSet(VL Arb) AM Invalid : 0x%x\n",
1977 			be32_to_cpu(smp->attr_mod));
1978 		smp->status |= IB_SMP_INVALID_FIELD;
1979 		break;
1980 	}
1981 
1982 	return __subn_get_opa_vl_arb(smp, am, data, ibdev, port, resp_len);
1983 }
1984 
1985 struct opa_pma_mad {
1986 	struct ib_mad_hdr mad_hdr;
1987 	u8 data[2024];
1988 } __packed;
1989 
1990 struct opa_class_port_info {
1991 	u8 base_version;
1992 	u8 class_version;
1993 	__be16 cap_mask;
1994 	__be32 cap_mask2_resp_time;
1995 
1996 	u8 redirect_gid[16];
1997 	__be32 redirect_tc_fl;
1998 	__be32 redirect_lid;
1999 	__be32 redirect_sl_qp;
2000 	__be32 redirect_qkey;
2001 
2002 	u8 trap_gid[16];
2003 	__be32 trap_tc_fl;
2004 	__be32 trap_lid;
2005 	__be32 trap_hl_qp;
2006 	__be32 trap_qkey;
2007 
2008 	__be16 trap_pkey;
2009 	__be16 redirect_pkey;
2010 
2011 	u8 trap_sl_rsvd;
2012 	u8 reserved[3];
2013 } __packed;
2014 
2015 struct opa_port_status_req {
2016 	__u8 port_num;
2017 	__u8 reserved[3];
2018 	__be32 vl_select_mask;
2019 };
2020 
2021 #define VL_MASK_ALL		0x000080ff
2022 
2023 struct opa_port_status_rsp {
2024 	__u8 port_num;
2025 	__u8 reserved[3];
2026 	__be32  vl_select_mask;
2027 
2028 	/* Data counters */
2029 	__be64 port_xmit_data;
2030 	__be64 port_rcv_data;
2031 	__be64 port_xmit_pkts;
2032 	__be64 port_rcv_pkts;
2033 	__be64 port_multicast_xmit_pkts;
2034 	__be64 port_multicast_rcv_pkts;
2035 	__be64 port_xmit_wait;
2036 	__be64 sw_port_congestion;
2037 	__be64 port_rcv_fecn;
2038 	__be64 port_rcv_becn;
2039 	__be64 port_xmit_time_cong;
2040 	__be64 port_xmit_wasted_bw;
2041 	__be64 port_xmit_wait_data;
2042 	__be64 port_rcv_bubble;
2043 	__be64 port_mark_fecn;
2044 	/* Error counters */
2045 	__be64 port_rcv_constraint_errors;
2046 	__be64 port_rcv_switch_relay_errors;
2047 	__be64 port_xmit_discards;
2048 	__be64 port_xmit_constraint_errors;
2049 	__be64 port_rcv_remote_physical_errors;
2050 	__be64 local_link_integrity_errors;
2051 	__be64 port_rcv_errors;
2052 	__be64 excessive_buffer_overruns;
2053 	__be64 fm_config_errors;
2054 	__be32 link_error_recovery;
2055 	__be32 link_downed;
2056 	u8 uncorrectable_errors;
2057 
2058 	u8 link_quality_indicator; /* 5res, 3bit */
2059 	u8 res2[6];
2060 	struct _vls_pctrs {
2061 		/* per-VL Data counters */
2062 		__be64 port_vl_xmit_data;
2063 		__be64 port_vl_rcv_data;
2064 		__be64 port_vl_xmit_pkts;
2065 		__be64 port_vl_rcv_pkts;
2066 		__be64 port_vl_xmit_wait;
2067 		__be64 sw_port_vl_congestion;
2068 		__be64 port_vl_rcv_fecn;
2069 		__be64 port_vl_rcv_becn;
2070 		__be64 port_xmit_time_cong;
2071 		__be64 port_vl_xmit_wasted_bw;
2072 		__be64 port_vl_xmit_wait_data;
2073 		__be64 port_vl_rcv_bubble;
2074 		__be64 port_vl_mark_fecn;
2075 		__be64 port_vl_xmit_discards;
2076 	} vls[0]; /* real array size defined by # bits set in vl_select_mask */
2077 };
2078 
2079 enum counter_selects {
2080 	CS_PORT_XMIT_DATA			= (1 << 31),
2081 	CS_PORT_RCV_DATA			= (1 << 30),
2082 	CS_PORT_XMIT_PKTS			= (1 << 29),
2083 	CS_PORT_RCV_PKTS			= (1 << 28),
2084 	CS_PORT_MCAST_XMIT_PKTS			= (1 << 27),
2085 	CS_PORT_MCAST_RCV_PKTS			= (1 << 26),
2086 	CS_PORT_XMIT_WAIT			= (1 << 25),
2087 	CS_SW_PORT_CONGESTION			= (1 << 24),
2088 	CS_PORT_RCV_FECN			= (1 << 23),
2089 	CS_PORT_RCV_BECN			= (1 << 22),
2090 	CS_PORT_XMIT_TIME_CONG			= (1 << 21),
2091 	CS_PORT_XMIT_WASTED_BW			= (1 << 20),
2092 	CS_PORT_XMIT_WAIT_DATA			= (1 << 19),
2093 	CS_PORT_RCV_BUBBLE			= (1 << 18),
2094 	CS_PORT_MARK_FECN			= (1 << 17),
2095 	CS_PORT_RCV_CONSTRAINT_ERRORS		= (1 << 16),
2096 	CS_PORT_RCV_SWITCH_RELAY_ERRORS		= (1 << 15),
2097 	CS_PORT_XMIT_DISCARDS			= (1 << 14),
2098 	CS_PORT_XMIT_CONSTRAINT_ERRORS		= (1 << 13),
2099 	CS_PORT_RCV_REMOTE_PHYSICAL_ERRORS	= (1 << 12),
2100 	CS_LOCAL_LINK_INTEGRITY_ERRORS		= (1 << 11),
2101 	CS_PORT_RCV_ERRORS			= (1 << 10),
2102 	CS_EXCESSIVE_BUFFER_OVERRUNS		= (1 << 9),
2103 	CS_FM_CONFIG_ERRORS			= (1 << 8),
2104 	CS_LINK_ERROR_RECOVERY			= (1 << 7),
2105 	CS_LINK_DOWNED				= (1 << 6),
2106 	CS_UNCORRECTABLE_ERRORS			= (1 << 5),
2107 };
2108 
2109 struct opa_clear_port_status {
2110 	__be64 port_select_mask[4];
2111 	__be32 counter_select_mask;
2112 };
2113 
2114 struct opa_aggregate {
2115 	__be16 attr_id;
2116 	__be16 err_reqlength;	/* 1 bit, 8 res, 7 bit */
2117 	__be32 attr_mod;
2118 	u8 data[0];
2119 };
2120 
2121 #define MSK_LLI 0x000000f0
2122 #define MSK_LLI_SFT 4
2123 #define MSK_LER 0x0000000f
2124 #define MSK_LER_SFT 0
2125 #define ADD_LLI 8
2126 #define ADD_LER 2
2127 
2128 /* Request contains first three fields, response contains those plus the rest */
2129 struct opa_port_data_counters_msg {
2130 	__be64 port_select_mask[4];
2131 	__be32 vl_select_mask;
2132 	__be32 resolution;
2133 
2134 	/* Response fields follow */
2135 	struct _port_dctrs {
2136 		u8 port_number;
2137 		u8 reserved2[3];
2138 		__be32 link_quality_indicator; /* 29res, 3bit */
2139 
2140 		/* Data counters */
2141 		__be64 port_xmit_data;
2142 		__be64 port_rcv_data;
2143 		__be64 port_xmit_pkts;
2144 		__be64 port_rcv_pkts;
2145 		__be64 port_multicast_xmit_pkts;
2146 		__be64 port_multicast_rcv_pkts;
2147 		__be64 port_xmit_wait;
2148 		__be64 sw_port_congestion;
2149 		__be64 port_rcv_fecn;
2150 		__be64 port_rcv_becn;
2151 		__be64 port_xmit_time_cong;
2152 		__be64 port_xmit_wasted_bw;
2153 		__be64 port_xmit_wait_data;
2154 		__be64 port_rcv_bubble;
2155 		__be64 port_mark_fecn;
2156 
2157 		__be64 port_error_counter_summary;
2158 		/* Sum of error counts/port */
2159 
2160 		struct _vls_dctrs {
2161 			/* per-VL Data counters */
2162 			__be64 port_vl_xmit_data;
2163 			__be64 port_vl_rcv_data;
2164 			__be64 port_vl_xmit_pkts;
2165 			__be64 port_vl_rcv_pkts;
2166 			__be64 port_vl_xmit_wait;
2167 			__be64 sw_port_vl_congestion;
2168 			__be64 port_vl_rcv_fecn;
2169 			__be64 port_vl_rcv_becn;
2170 			__be64 port_xmit_time_cong;
2171 			__be64 port_vl_xmit_wasted_bw;
2172 			__be64 port_vl_xmit_wait_data;
2173 			__be64 port_vl_rcv_bubble;
2174 			__be64 port_vl_mark_fecn;
2175 		} vls[0];
2176 		/* array size defined by #bits set in vl_select_mask*/
2177 	} port[1]; /* array size defined by  #ports in attribute modifier */
2178 };
2179 
2180 struct opa_port_error_counters64_msg {
2181 	/*
2182 	 * Request contains first two fields, response contains the
2183 	 * whole magilla
2184 	 */
2185 	__be64 port_select_mask[4];
2186 	__be32 vl_select_mask;
2187 
2188 	/* Response-only fields follow */
2189 	__be32 reserved1;
2190 	struct _port_ectrs {
2191 		u8 port_number;
2192 		u8 reserved2[7];
2193 		__be64 port_rcv_constraint_errors;
2194 		__be64 port_rcv_switch_relay_errors;
2195 		__be64 port_xmit_discards;
2196 		__be64 port_xmit_constraint_errors;
2197 		__be64 port_rcv_remote_physical_errors;
2198 		__be64 local_link_integrity_errors;
2199 		__be64 port_rcv_errors;
2200 		__be64 excessive_buffer_overruns;
2201 		__be64 fm_config_errors;
2202 		__be32 link_error_recovery;
2203 		__be32 link_downed;
2204 		u8 uncorrectable_errors;
2205 		u8 reserved3[7];
2206 		struct _vls_ectrs {
2207 			__be64 port_vl_xmit_discards;
2208 		} vls[0];
2209 		/* array size defined by #bits set in vl_select_mask */
2210 	} port[1]; /* array size defined by #ports in attribute modifier */
2211 };
2212 
2213 struct opa_port_error_info_msg {
2214 	__be64 port_select_mask[4];
2215 	__be32 error_info_select_mask;
2216 	__be32 reserved1;
2217 	struct _port_ei {
2218 		u8 port_number;
2219 		u8 reserved2[7];
2220 
2221 		/* PortRcvErrorInfo */
2222 		struct {
2223 			u8 status_and_code;
2224 			union {
2225 				u8 raw[17];
2226 				struct {
2227 					/* EI1to12 format */
2228 					u8 packet_flit1[8];
2229 					u8 packet_flit2[8];
2230 					u8 remaining_flit_bits12;
2231 				} ei1to12;
2232 				struct {
2233 					u8 packet_bytes[8];
2234 					u8 remaining_flit_bits;
2235 				} ei13;
2236 			} ei;
2237 			u8 reserved3[6];
2238 		} __packed port_rcv_ei;
2239 
2240 		/* ExcessiveBufferOverrunInfo */
2241 		struct {
2242 			u8 status_and_sc;
2243 			u8 reserved4[7];
2244 		} __packed excessive_buffer_overrun_ei;
2245 
2246 		/* PortXmitConstraintErrorInfo */
2247 		struct {
2248 			u8 status;
2249 			u8 reserved5;
2250 			__be16 pkey;
2251 			__be32 slid;
2252 		} __packed port_xmit_constraint_ei;
2253 
2254 		/* PortRcvConstraintErrorInfo */
2255 		struct {
2256 			u8 status;
2257 			u8 reserved6;
2258 			__be16 pkey;
2259 			__be32 slid;
2260 		} __packed port_rcv_constraint_ei;
2261 
2262 		/* PortRcvSwitchRelayErrorInfo */
2263 		struct {
2264 			u8 status_and_code;
2265 			u8 reserved7[3];
2266 			__u32 error_info;
2267 		} __packed port_rcv_switch_relay_ei;
2268 
2269 		/* UncorrectableErrorInfo */
2270 		struct {
2271 			u8 status_and_code;
2272 			u8 reserved8;
2273 		} __packed uncorrectable_ei;
2274 
2275 		/* FMConfigErrorInfo */
2276 		struct {
2277 			u8 status_and_code;
2278 			u8 error_info;
2279 		} __packed fm_config_ei;
2280 		__u32 reserved9;
2281 	} port[1]; /* actual array size defined by #ports in attr modifier */
2282 };
2283 
2284 /* opa_port_error_info_msg error_info_select_mask bit definitions */
2285 enum error_info_selects {
2286 	ES_PORT_RCV_ERROR_INFO			= (1 << 31),
2287 	ES_EXCESSIVE_BUFFER_OVERRUN_INFO	= (1 << 30),
2288 	ES_PORT_XMIT_CONSTRAINT_ERROR_INFO	= (1 << 29),
2289 	ES_PORT_RCV_CONSTRAINT_ERROR_INFO	= (1 << 28),
2290 	ES_PORT_RCV_SWITCH_RELAY_ERROR_INFO	= (1 << 27),
2291 	ES_UNCORRECTABLE_ERROR_INFO		= (1 << 26),
2292 	ES_FM_CONFIG_ERROR_INFO			= (1 << 25)
2293 };
2294 
2295 static int pma_get_opa_classportinfo(struct opa_pma_mad *pmp,
2296 				     struct ib_device *ibdev, u32 *resp_len)
2297 {
2298 	struct opa_class_port_info *p =
2299 		(struct opa_class_port_info *)pmp->data;
2300 
2301 	memset(pmp->data, 0, sizeof(pmp->data));
2302 
2303 	if (pmp->mad_hdr.attr_mod != 0)
2304 		pmp->mad_hdr.status |= IB_SMP_INVALID_FIELD;
2305 
2306 	p->base_version = OPA_MGMT_BASE_VERSION;
2307 	p->class_version = OPA_SMI_CLASS_VERSION;
2308 	/*
2309 	 * Expected response time is 4.096 usec. * 2^18 == 1.073741824 sec.
2310 	 */
2311 	p->cap_mask2_resp_time = cpu_to_be32(18);
2312 
2313 	if (resp_len)
2314 		*resp_len += sizeof(*p);
2315 
2316 	return reply((struct ib_mad_hdr *)pmp);
2317 }
2318 
2319 static void a0_portstatus(struct hfi1_pportdata *ppd,
2320 			  struct opa_port_status_rsp *rsp, u32 vl_select_mask)
2321 {
2322 	if (!is_bx(ppd->dd)) {
2323 		unsigned long vl;
2324 		u64 sum_vl_xmit_wait = 0;
2325 		u32 vl_all_mask = VL_MASK_ALL;
2326 
2327 		for_each_set_bit(vl, (unsigned long *)&(vl_all_mask),
2328 				 8 * sizeof(vl_all_mask)) {
2329 			u64 tmp = sum_vl_xmit_wait +
2330 				  read_port_cntr(ppd, C_TX_WAIT_VL,
2331 						 idx_from_vl(vl));
2332 			if (tmp < sum_vl_xmit_wait) {
2333 				/* we wrapped */
2334 				sum_vl_xmit_wait = (u64)~0;
2335 				break;
2336 			}
2337 			sum_vl_xmit_wait = tmp;
2338 		}
2339 		if (be64_to_cpu(rsp->port_xmit_wait) > sum_vl_xmit_wait)
2340 			rsp->port_xmit_wait = cpu_to_be64(sum_vl_xmit_wait);
2341 	}
2342 }
2343 
2344 static int pma_get_opa_portstatus(struct opa_pma_mad *pmp,
2345 				  struct ib_device *ibdev,
2346 				  u8 port, u32 *resp_len)
2347 {
2348 	struct opa_port_status_req *req =
2349 		(struct opa_port_status_req *)pmp->data;
2350 	struct hfi1_devdata *dd = dd_from_ibdev(ibdev);
2351 	struct opa_port_status_rsp *rsp;
2352 	u32 vl_select_mask = be32_to_cpu(req->vl_select_mask);
2353 	unsigned long vl;
2354 	size_t response_data_size;
2355 	u32 nports = be32_to_cpu(pmp->mad_hdr.attr_mod) >> 24;
2356 	u8 port_num = req->port_num;
2357 	u8 num_vls = hweight32(vl_select_mask);
2358 	struct _vls_pctrs *vlinfo;
2359 	struct hfi1_ibport *ibp = to_iport(ibdev, port);
2360 	struct hfi1_pportdata *ppd = ppd_from_ibp(ibp);
2361 	int vfi;
2362 	u64 tmp, tmp2;
2363 
2364 	response_data_size = sizeof(struct opa_port_status_rsp) +
2365 				num_vls * sizeof(struct _vls_pctrs);
2366 	if (response_data_size > sizeof(pmp->data)) {
2367 		pmp->mad_hdr.status |= OPA_PM_STATUS_REQUEST_TOO_LARGE;
2368 		return reply((struct ib_mad_hdr *)pmp);
2369 	}
2370 
2371 	if (nports != 1 || (port_num && port_num != port) ||
2372 	    num_vls > OPA_MAX_VLS || (vl_select_mask & ~VL_MASK_ALL)) {
2373 		pmp->mad_hdr.status |= IB_SMP_INVALID_FIELD;
2374 		return reply((struct ib_mad_hdr *)pmp);
2375 	}
2376 
2377 	memset(pmp->data, 0, sizeof(pmp->data));
2378 
2379 	rsp = (struct opa_port_status_rsp *)pmp->data;
2380 	if (port_num)
2381 		rsp->port_num = port_num;
2382 	else
2383 		rsp->port_num = port;
2384 
2385 	rsp->port_rcv_constraint_errors =
2386 		cpu_to_be64(read_port_cntr(ppd, C_SW_RCV_CSTR_ERR,
2387 					   CNTR_INVALID_VL));
2388 
2389 	hfi1_read_link_quality(dd, &rsp->link_quality_indicator);
2390 
2391 	rsp->vl_select_mask = cpu_to_be32(vl_select_mask);
2392 	rsp->port_xmit_data = cpu_to_be64(read_dev_cntr(dd, C_DC_XMIT_FLITS,
2393 					  CNTR_INVALID_VL));
2394 	rsp->port_rcv_data = cpu_to_be64(read_dev_cntr(dd, C_DC_RCV_FLITS,
2395 					 CNTR_INVALID_VL));
2396 	rsp->port_xmit_pkts = cpu_to_be64(read_dev_cntr(dd, C_DC_XMIT_PKTS,
2397 					  CNTR_INVALID_VL));
2398 	rsp->port_rcv_pkts = cpu_to_be64(read_dev_cntr(dd, C_DC_RCV_PKTS,
2399 					 CNTR_INVALID_VL));
2400 	rsp->port_multicast_xmit_pkts =
2401 		cpu_to_be64(read_dev_cntr(dd, C_DC_MC_XMIT_PKTS,
2402 					  CNTR_INVALID_VL));
2403 	rsp->port_multicast_rcv_pkts =
2404 		cpu_to_be64(read_dev_cntr(dd, C_DC_MC_RCV_PKTS,
2405 					  CNTR_INVALID_VL));
2406 	rsp->port_xmit_wait =
2407 		cpu_to_be64(read_port_cntr(ppd, C_TX_WAIT, CNTR_INVALID_VL));
2408 	rsp->port_rcv_fecn =
2409 		cpu_to_be64(read_dev_cntr(dd, C_DC_RCV_FCN, CNTR_INVALID_VL));
2410 	rsp->port_rcv_becn =
2411 		cpu_to_be64(read_dev_cntr(dd, C_DC_RCV_BCN, CNTR_INVALID_VL));
2412 	rsp->port_xmit_discards =
2413 		cpu_to_be64(read_port_cntr(ppd, C_SW_XMIT_DSCD,
2414 					   CNTR_INVALID_VL));
2415 	rsp->port_xmit_constraint_errors =
2416 		cpu_to_be64(read_port_cntr(ppd, C_SW_XMIT_CSTR_ERR,
2417 					   CNTR_INVALID_VL));
2418 	rsp->port_rcv_remote_physical_errors =
2419 		cpu_to_be64(read_dev_cntr(dd, C_DC_RMT_PHY_ERR,
2420 					  CNTR_INVALID_VL));
2421 	rsp->local_link_integrity_errors =
2422 		cpu_to_be64(read_dev_cntr(dd, C_DC_RX_REPLAY,
2423 					  CNTR_INVALID_VL));
2424 	tmp = read_dev_cntr(dd, C_DC_SEQ_CRC_CNT, CNTR_INVALID_VL);
2425 	tmp2 = tmp + read_dev_cntr(dd, C_DC_REINIT_FROM_PEER_CNT,
2426 				   CNTR_INVALID_VL);
2427 	if (tmp2 > (u32)UINT_MAX || tmp2 < tmp) {
2428 		/* overflow/wrapped */
2429 		rsp->link_error_recovery = cpu_to_be32(~0);
2430 	} else {
2431 		rsp->link_error_recovery = cpu_to_be32(tmp2);
2432 	}
2433 	rsp->port_rcv_errors =
2434 		cpu_to_be64(read_dev_cntr(dd, C_DC_RCV_ERR, CNTR_INVALID_VL));
2435 	rsp->excessive_buffer_overruns =
2436 		cpu_to_be64(read_dev_cntr(dd, C_RCV_OVF, CNTR_INVALID_VL));
2437 	rsp->fm_config_errors =
2438 		cpu_to_be64(read_dev_cntr(dd, C_DC_FM_CFG_ERR,
2439 					  CNTR_INVALID_VL));
2440 	rsp->link_downed = cpu_to_be32(read_port_cntr(ppd, C_SW_LINK_DOWN,
2441 						      CNTR_INVALID_VL));
2442 
2443 	/* rsp->uncorrectable_errors is 8 bits wide, and it pegs at 0xff */
2444 	tmp = read_dev_cntr(dd, C_DC_UNC_ERR, CNTR_INVALID_VL);
2445 	rsp->uncorrectable_errors = tmp < 0x100 ? (tmp & 0xff) : 0xff;
2446 
2447 	vlinfo = &rsp->vls[0];
2448 	vfi = 0;
2449 	/* The vl_select_mask has been checked above, and we know
2450 	 * that it contains only entries which represent valid VLs.
2451 	 * So in the for_each_set_bit() loop below, we don't need
2452 	 * any additional checks for vl.
2453 	 */
2454 	for_each_set_bit(vl, (unsigned long *)&(vl_select_mask),
2455 			 8 * sizeof(vl_select_mask)) {
2456 		memset(vlinfo, 0, sizeof(*vlinfo));
2457 
2458 		tmp = read_dev_cntr(dd, C_DC_RX_FLIT_VL, idx_from_vl(vl));
2459 		rsp->vls[vfi].port_vl_rcv_data = cpu_to_be64(tmp);
2460 
2461 		rsp->vls[vfi].port_vl_rcv_pkts =
2462 			cpu_to_be64(read_dev_cntr(dd, C_DC_RX_PKT_VL,
2463 						  idx_from_vl(vl)));
2464 
2465 		rsp->vls[vfi].port_vl_xmit_data =
2466 			cpu_to_be64(read_port_cntr(ppd, C_TX_FLIT_VL,
2467 						   idx_from_vl(vl)));
2468 
2469 		rsp->vls[vfi].port_vl_xmit_pkts =
2470 			cpu_to_be64(read_port_cntr(ppd, C_TX_PKT_VL,
2471 						   idx_from_vl(vl)));
2472 
2473 		rsp->vls[vfi].port_vl_xmit_wait =
2474 			cpu_to_be64(read_port_cntr(ppd, C_TX_WAIT_VL,
2475 						   idx_from_vl(vl)));
2476 
2477 		rsp->vls[vfi].port_vl_rcv_fecn =
2478 			cpu_to_be64(read_dev_cntr(dd, C_DC_RCV_FCN_VL,
2479 						  idx_from_vl(vl)));
2480 
2481 		rsp->vls[vfi].port_vl_rcv_becn =
2482 			cpu_to_be64(read_dev_cntr(dd, C_DC_RCV_BCN_VL,
2483 						  idx_from_vl(vl)));
2484 
2485 		rsp->vls[vfi].port_vl_xmit_discards =
2486 			cpu_to_be64(read_port_cntr(ppd, C_SW_XMIT_DSCD_VL,
2487 						   idx_from_vl(vl)));
2488 		vlinfo++;
2489 		vfi++;
2490 	}
2491 
2492 	a0_portstatus(ppd, rsp, vl_select_mask);
2493 
2494 	if (resp_len)
2495 		*resp_len += response_data_size;
2496 
2497 	return reply((struct ib_mad_hdr *)pmp);
2498 }
2499 
2500 static u64 get_error_counter_summary(struct ib_device *ibdev, u8 port,
2501 				     u8 res_lli, u8 res_ler)
2502 {
2503 	struct hfi1_devdata *dd = dd_from_ibdev(ibdev);
2504 	struct hfi1_ibport *ibp = to_iport(ibdev, port);
2505 	struct hfi1_pportdata *ppd = ppd_from_ibp(ibp);
2506 	u64 error_counter_summary = 0, tmp;
2507 
2508 	error_counter_summary += read_port_cntr(ppd, C_SW_RCV_CSTR_ERR,
2509 						CNTR_INVALID_VL);
2510 	/* port_rcv_switch_relay_errors is 0 for HFIs */
2511 	error_counter_summary += read_port_cntr(ppd, C_SW_XMIT_DSCD,
2512 						CNTR_INVALID_VL);
2513 	error_counter_summary += read_port_cntr(ppd, C_SW_XMIT_CSTR_ERR,
2514 						CNTR_INVALID_VL);
2515 	error_counter_summary += read_dev_cntr(dd, C_DC_RMT_PHY_ERR,
2516 					       CNTR_INVALID_VL);
2517 	/* local link integrity must be right-shifted by the lli resolution */
2518 	error_counter_summary += (read_dev_cntr(dd, C_DC_RX_REPLAY,
2519 						CNTR_INVALID_VL) >> res_lli);
2520 	/* link error recovery must b right-shifted by the ler resolution */
2521 	tmp = read_dev_cntr(dd, C_DC_SEQ_CRC_CNT, CNTR_INVALID_VL);
2522 	tmp += read_dev_cntr(dd, C_DC_REINIT_FROM_PEER_CNT, CNTR_INVALID_VL);
2523 	error_counter_summary += (tmp >> res_ler);
2524 	error_counter_summary += read_dev_cntr(dd, C_DC_RCV_ERR,
2525 					       CNTR_INVALID_VL);
2526 	error_counter_summary += read_dev_cntr(dd, C_RCV_OVF, CNTR_INVALID_VL);
2527 	error_counter_summary += read_dev_cntr(dd, C_DC_FM_CFG_ERR,
2528 					       CNTR_INVALID_VL);
2529 	/* ppd->link_downed is a 32-bit value */
2530 	error_counter_summary += read_port_cntr(ppd, C_SW_LINK_DOWN,
2531 						CNTR_INVALID_VL);
2532 	tmp = read_dev_cntr(dd, C_DC_UNC_ERR, CNTR_INVALID_VL);
2533 	/* this is an 8-bit quantity */
2534 	error_counter_summary += tmp < 0x100 ? (tmp & 0xff) : 0xff;
2535 
2536 	return error_counter_summary;
2537 }
2538 
2539 static void a0_datacounters(struct hfi1_pportdata *ppd, struct _port_dctrs *rsp,
2540 			    u32 vl_select_mask)
2541 {
2542 	if (!is_bx(ppd->dd)) {
2543 		unsigned long vl;
2544 		u64 sum_vl_xmit_wait = 0;
2545 		u32 vl_all_mask = VL_MASK_ALL;
2546 
2547 		for_each_set_bit(vl, (unsigned long *)&(vl_all_mask),
2548 				 8 * sizeof(vl_all_mask)) {
2549 			u64 tmp = sum_vl_xmit_wait +
2550 				  read_port_cntr(ppd, C_TX_WAIT_VL,
2551 						 idx_from_vl(vl));
2552 			if (tmp < sum_vl_xmit_wait) {
2553 				/* we wrapped */
2554 				sum_vl_xmit_wait = (u64)~0;
2555 				break;
2556 			}
2557 			sum_vl_xmit_wait = tmp;
2558 		}
2559 		if (be64_to_cpu(rsp->port_xmit_wait) > sum_vl_xmit_wait)
2560 			rsp->port_xmit_wait = cpu_to_be64(sum_vl_xmit_wait);
2561 	}
2562 }
2563 
2564 static void pma_get_opa_port_dctrs(struct ib_device *ibdev,
2565 				   struct _port_dctrs *rsp)
2566 {
2567 	struct hfi1_devdata *dd = dd_from_ibdev(ibdev);
2568 
2569 	rsp->port_xmit_data = cpu_to_be64(read_dev_cntr(dd, C_DC_XMIT_FLITS,
2570 						CNTR_INVALID_VL));
2571 	rsp->port_rcv_data = cpu_to_be64(read_dev_cntr(dd, C_DC_RCV_FLITS,
2572 						CNTR_INVALID_VL));
2573 	rsp->port_xmit_pkts = cpu_to_be64(read_dev_cntr(dd, C_DC_XMIT_PKTS,
2574 						CNTR_INVALID_VL));
2575 	rsp->port_rcv_pkts = cpu_to_be64(read_dev_cntr(dd, C_DC_RCV_PKTS,
2576 						CNTR_INVALID_VL));
2577 	rsp->port_multicast_xmit_pkts =
2578 		cpu_to_be64(read_dev_cntr(dd, C_DC_MC_XMIT_PKTS,
2579 					  CNTR_INVALID_VL));
2580 	rsp->port_multicast_rcv_pkts =
2581 		cpu_to_be64(read_dev_cntr(dd, C_DC_MC_RCV_PKTS,
2582 					  CNTR_INVALID_VL));
2583 }
2584 
2585 static int pma_get_opa_datacounters(struct opa_pma_mad *pmp,
2586 				    struct ib_device *ibdev,
2587 				    u8 port, u32 *resp_len)
2588 {
2589 	struct opa_port_data_counters_msg *req =
2590 		(struct opa_port_data_counters_msg *)pmp->data;
2591 	struct hfi1_devdata *dd = dd_from_ibdev(ibdev);
2592 	struct hfi1_ibport *ibp = to_iport(ibdev, port);
2593 	struct hfi1_pportdata *ppd = ppd_from_ibp(ibp);
2594 	struct _port_dctrs *rsp;
2595 	struct _vls_dctrs *vlinfo;
2596 	size_t response_data_size;
2597 	u32 num_ports;
2598 	u8 num_pslm;
2599 	u8 lq, num_vls;
2600 	u8 res_lli, res_ler;
2601 	u64 port_mask;
2602 	unsigned long port_num;
2603 	unsigned long vl;
2604 	u32 vl_select_mask;
2605 	int vfi;
2606 
2607 	num_ports = be32_to_cpu(pmp->mad_hdr.attr_mod) >> 24;
2608 	num_pslm = hweight64(be64_to_cpu(req->port_select_mask[3]));
2609 	num_vls = hweight32(be32_to_cpu(req->vl_select_mask));
2610 	vl_select_mask = be32_to_cpu(req->vl_select_mask);
2611 	res_lli = (u8)(be32_to_cpu(req->resolution) & MSK_LLI) >> MSK_LLI_SFT;
2612 	res_lli = res_lli ? res_lli + ADD_LLI : 0;
2613 	res_ler = (u8)(be32_to_cpu(req->resolution) & MSK_LER) >> MSK_LER_SFT;
2614 	res_ler = res_ler ? res_ler + ADD_LER : 0;
2615 
2616 	if (num_ports != 1 || (vl_select_mask & ~VL_MASK_ALL)) {
2617 		pmp->mad_hdr.status |= IB_SMP_INVALID_FIELD;
2618 		return reply((struct ib_mad_hdr *)pmp);
2619 	}
2620 
2621 	/* Sanity check */
2622 	response_data_size = sizeof(struct opa_port_data_counters_msg) +
2623 				num_vls * sizeof(struct _vls_dctrs);
2624 
2625 	if (response_data_size > sizeof(pmp->data)) {
2626 		pmp->mad_hdr.status |= IB_SMP_INVALID_FIELD;
2627 		return reply((struct ib_mad_hdr *)pmp);
2628 	}
2629 
2630 	/*
2631 	 * The bit set in the mask needs to be consistent with the
2632 	 * port the request came in on.
2633 	 */
2634 	port_mask = be64_to_cpu(req->port_select_mask[3]);
2635 	port_num = find_first_bit((unsigned long *)&port_mask,
2636 				  sizeof(port_mask));
2637 
2638 	if ((u8)port_num != port) {
2639 		pmp->mad_hdr.status |= IB_SMP_INVALID_FIELD;
2640 		return reply((struct ib_mad_hdr *)pmp);
2641 	}
2642 
2643 	rsp = &req->port[0];
2644 	memset(rsp, 0, sizeof(*rsp));
2645 
2646 	rsp->port_number = port;
2647 	/*
2648 	 * Note that link_quality_indicator is a 32 bit quantity in
2649 	 * 'datacounters' queries (as opposed to 'portinfo' queries,
2650 	 * where it's a byte).
2651 	 */
2652 	hfi1_read_link_quality(dd, &lq);
2653 	rsp->link_quality_indicator = cpu_to_be32((u32)lq);
2654 	pma_get_opa_port_dctrs(ibdev, rsp);
2655 
2656 	rsp->port_xmit_wait =
2657 		cpu_to_be64(read_port_cntr(ppd, C_TX_WAIT, CNTR_INVALID_VL));
2658 	rsp->port_rcv_fecn =
2659 		cpu_to_be64(read_dev_cntr(dd, C_DC_RCV_FCN, CNTR_INVALID_VL));
2660 	rsp->port_rcv_becn =
2661 		cpu_to_be64(read_dev_cntr(dd, C_DC_RCV_BCN, CNTR_INVALID_VL));
2662 	rsp->port_error_counter_summary =
2663 		cpu_to_be64(get_error_counter_summary(ibdev, port,
2664 						      res_lli, res_ler));
2665 
2666 	vlinfo = &rsp->vls[0];
2667 	vfi = 0;
2668 	/* The vl_select_mask has been checked above, and we know
2669 	 * that it contains only entries which represent valid VLs.
2670 	 * So in the for_each_set_bit() loop below, we don't need
2671 	 * any additional checks for vl.
2672 	 */
2673 	for_each_set_bit(vl, (unsigned long *)&(vl_select_mask),
2674 			 8 * sizeof(req->vl_select_mask)) {
2675 		memset(vlinfo, 0, sizeof(*vlinfo));
2676 
2677 		rsp->vls[vfi].port_vl_xmit_data =
2678 			cpu_to_be64(read_port_cntr(ppd, C_TX_FLIT_VL,
2679 						   idx_from_vl(vl)));
2680 
2681 		rsp->vls[vfi].port_vl_rcv_data =
2682 			cpu_to_be64(read_dev_cntr(dd, C_DC_RX_FLIT_VL,
2683 						  idx_from_vl(vl)));
2684 
2685 		rsp->vls[vfi].port_vl_xmit_pkts =
2686 			cpu_to_be64(read_port_cntr(ppd, C_TX_PKT_VL,
2687 						   idx_from_vl(vl)));
2688 
2689 		rsp->vls[vfi].port_vl_rcv_pkts =
2690 			cpu_to_be64(read_dev_cntr(dd, C_DC_RX_PKT_VL,
2691 						  idx_from_vl(vl)));
2692 
2693 		rsp->vls[vfi].port_vl_xmit_wait =
2694 			cpu_to_be64(read_port_cntr(ppd, C_TX_WAIT_VL,
2695 						   idx_from_vl(vl)));
2696 
2697 		rsp->vls[vfi].port_vl_rcv_fecn =
2698 			cpu_to_be64(read_dev_cntr(dd, C_DC_RCV_FCN_VL,
2699 						  idx_from_vl(vl)));
2700 		rsp->vls[vfi].port_vl_rcv_becn =
2701 			cpu_to_be64(read_dev_cntr(dd, C_DC_RCV_BCN_VL,
2702 						  idx_from_vl(vl)));
2703 
2704 		/* rsp->port_vl_xmit_time_cong is 0 for HFIs */
2705 		/* rsp->port_vl_xmit_wasted_bw ??? */
2706 		/* port_vl_xmit_wait_data - TXE (table 13-9 HFI spec) ???
2707 		 * does this differ from rsp->vls[vfi].port_vl_xmit_wait
2708 		 */
2709 		/*rsp->vls[vfi].port_vl_mark_fecn =
2710 		 *	cpu_to_be64(read_csr(dd, DCC_PRF_PORT_VL_MARK_FECN_CNT
2711 		 *		+ offset));
2712 		 */
2713 		vlinfo++;
2714 		vfi++;
2715 	}
2716 
2717 	a0_datacounters(ppd, rsp, vl_select_mask);
2718 
2719 	if (resp_len)
2720 		*resp_len += response_data_size;
2721 
2722 	return reply((struct ib_mad_hdr *)pmp);
2723 }
2724 
2725 static int pma_get_ib_portcounters_ext(struct ib_pma_mad *pmp,
2726 				       struct ib_device *ibdev, u8 port)
2727 {
2728 	struct ib_pma_portcounters_ext *p = (struct ib_pma_portcounters_ext *)
2729 						pmp->data;
2730 	struct _port_dctrs rsp;
2731 
2732 	if (pmp->mad_hdr.attr_mod != 0 || p->port_select != port) {
2733 		pmp->mad_hdr.status |= IB_SMP_INVALID_FIELD;
2734 		goto bail;
2735 	}
2736 
2737 	memset(&rsp, 0, sizeof(rsp));
2738 	pma_get_opa_port_dctrs(ibdev, &rsp);
2739 
2740 	p->port_xmit_data = rsp.port_xmit_data;
2741 	p->port_rcv_data = rsp.port_rcv_data;
2742 	p->port_xmit_packets = rsp.port_xmit_pkts;
2743 	p->port_rcv_packets = rsp.port_rcv_pkts;
2744 	p->port_unicast_xmit_packets = 0;
2745 	p->port_unicast_rcv_packets =  0;
2746 	p->port_multicast_xmit_packets = rsp.port_multicast_xmit_pkts;
2747 	p->port_multicast_rcv_packets = rsp.port_multicast_rcv_pkts;
2748 
2749 bail:
2750 	return reply((struct ib_mad_hdr *)pmp);
2751 }
2752 
2753 static void pma_get_opa_port_ectrs(struct ib_device *ibdev,
2754 				   struct _port_ectrs *rsp, u8 port)
2755 {
2756 	u64 tmp, tmp2;
2757 	struct hfi1_devdata *dd = dd_from_ibdev(ibdev);
2758 	struct hfi1_ibport *ibp = to_iport(ibdev, port);
2759 	struct hfi1_pportdata *ppd = ppd_from_ibp(ibp);
2760 
2761 	tmp = read_dev_cntr(dd, C_DC_SEQ_CRC_CNT, CNTR_INVALID_VL);
2762 	tmp2 = tmp + read_dev_cntr(dd, C_DC_REINIT_FROM_PEER_CNT,
2763 					CNTR_INVALID_VL);
2764 	if (tmp2 > (u32)UINT_MAX || tmp2 < tmp) {
2765 		/* overflow/wrapped */
2766 		rsp->link_error_recovery = cpu_to_be32(~0);
2767 	} else {
2768 		rsp->link_error_recovery = cpu_to_be32(tmp2);
2769 	}
2770 
2771 	rsp->link_downed = cpu_to_be32(read_port_cntr(ppd, C_SW_LINK_DOWN,
2772 						CNTR_INVALID_VL));
2773 	rsp->port_rcv_errors =
2774 		cpu_to_be64(read_dev_cntr(dd, C_DC_RCV_ERR, CNTR_INVALID_VL));
2775 	rsp->port_rcv_remote_physical_errors =
2776 		cpu_to_be64(read_dev_cntr(dd, C_DC_RMT_PHY_ERR,
2777 					  CNTR_INVALID_VL));
2778 	rsp->port_rcv_switch_relay_errors = 0;
2779 	rsp->port_xmit_discards =
2780 		cpu_to_be64(read_port_cntr(ppd, C_SW_XMIT_DSCD,
2781 					   CNTR_INVALID_VL));
2782 	rsp->port_xmit_constraint_errors =
2783 		cpu_to_be64(read_port_cntr(ppd, C_SW_XMIT_CSTR_ERR,
2784 					   CNTR_INVALID_VL));
2785 	rsp->port_rcv_constraint_errors =
2786 		cpu_to_be64(read_port_cntr(ppd, C_SW_RCV_CSTR_ERR,
2787 					   CNTR_INVALID_VL));
2788 	rsp->local_link_integrity_errors =
2789 		cpu_to_be64(read_dev_cntr(dd, C_DC_RX_REPLAY,
2790 					  CNTR_INVALID_VL));
2791 	rsp->excessive_buffer_overruns =
2792 		cpu_to_be64(read_dev_cntr(dd, C_RCV_OVF, CNTR_INVALID_VL));
2793 }
2794 
2795 static int pma_get_opa_porterrors(struct opa_pma_mad *pmp,
2796 				  struct ib_device *ibdev,
2797 				  u8 port, u32 *resp_len)
2798 {
2799 	size_t response_data_size;
2800 	struct _port_ectrs *rsp;
2801 	u8 port_num;
2802 	struct opa_port_error_counters64_msg *req;
2803 	struct hfi1_devdata *dd = dd_from_ibdev(ibdev);
2804 	u32 num_ports;
2805 	u8 num_pslm;
2806 	u8 num_vls;
2807 	struct hfi1_ibport *ibp;
2808 	struct hfi1_pportdata *ppd;
2809 	struct _vls_ectrs *vlinfo;
2810 	unsigned long vl;
2811 	u64 port_mask, tmp;
2812 	u32 vl_select_mask;
2813 	int vfi;
2814 
2815 	req = (struct opa_port_error_counters64_msg *)pmp->data;
2816 
2817 	num_ports = be32_to_cpu(pmp->mad_hdr.attr_mod) >> 24;
2818 
2819 	num_pslm = hweight64(be64_to_cpu(req->port_select_mask[3]));
2820 	num_vls = hweight32(be32_to_cpu(req->vl_select_mask));
2821 
2822 	if (num_ports != 1 || num_ports != num_pslm) {
2823 		pmp->mad_hdr.status |= IB_SMP_INVALID_FIELD;
2824 		return reply((struct ib_mad_hdr *)pmp);
2825 	}
2826 
2827 	response_data_size = sizeof(struct opa_port_error_counters64_msg) +
2828 				num_vls * sizeof(struct _vls_ectrs);
2829 
2830 	if (response_data_size > sizeof(pmp->data)) {
2831 		pmp->mad_hdr.status |= IB_SMP_INVALID_FIELD;
2832 		return reply((struct ib_mad_hdr *)pmp);
2833 	}
2834 	/*
2835 	 * The bit set in the mask needs to be consistent with the
2836 	 * port the request came in on.
2837 	 */
2838 	port_mask = be64_to_cpu(req->port_select_mask[3]);
2839 	port_num = find_first_bit((unsigned long *)&port_mask,
2840 				  sizeof(port_mask));
2841 
2842 	if (port_num != port) {
2843 		pmp->mad_hdr.status |= IB_SMP_INVALID_FIELD;
2844 		return reply((struct ib_mad_hdr *)pmp);
2845 	}
2846 
2847 	rsp = &req->port[0];
2848 
2849 	ibp = to_iport(ibdev, port_num);
2850 	ppd = ppd_from_ibp(ibp);
2851 
2852 	memset(rsp, 0, sizeof(*rsp));
2853 	rsp->port_number = port_num;
2854 
2855 	pma_get_opa_port_ectrs(ibdev, rsp, port_num);
2856 
2857 	rsp->port_rcv_remote_physical_errors =
2858 		cpu_to_be64(read_dev_cntr(dd, C_DC_RMT_PHY_ERR,
2859 					  CNTR_INVALID_VL));
2860 	rsp->fm_config_errors =
2861 		cpu_to_be64(read_dev_cntr(dd, C_DC_FM_CFG_ERR,
2862 					  CNTR_INVALID_VL));
2863 	tmp = read_dev_cntr(dd, C_DC_UNC_ERR, CNTR_INVALID_VL);
2864 
2865 	rsp->uncorrectable_errors = tmp < 0x100 ? (tmp & 0xff) : 0xff;
2866 	rsp->port_rcv_errors =
2867 		cpu_to_be64(read_dev_cntr(dd, C_DC_RCV_ERR, CNTR_INVALID_VL));
2868 	vlinfo = &rsp->vls[0];
2869 	vfi = 0;
2870 	vl_select_mask = be32_to_cpu(req->vl_select_mask);
2871 	for_each_set_bit(vl, (unsigned long *)&(vl_select_mask),
2872 			 8 * sizeof(req->vl_select_mask)) {
2873 		memset(vlinfo, 0, sizeof(*vlinfo));
2874 		rsp->vls[vfi].port_vl_xmit_discards =
2875 			cpu_to_be64(read_port_cntr(ppd, C_SW_XMIT_DSCD_VL,
2876 						   idx_from_vl(vl)));
2877 		vlinfo += 1;
2878 		vfi++;
2879 	}
2880 
2881 	if (resp_len)
2882 		*resp_len += response_data_size;
2883 
2884 	return reply((struct ib_mad_hdr *)pmp);
2885 }
2886 
2887 static int pma_get_ib_portcounters(struct ib_pma_mad *pmp,
2888 				   struct ib_device *ibdev, u8 port)
2889 {
2890 	struct ib_pma_portcounters *p = (struct ib_pma_portcounters *)
2891 		pmp->data;
2892 	struct _port_ectrs rsp;
2893 	u64 temp_link_overrun_errors;
2894 	u64 temp_64;
2895 	u32 temp_32;
2896 
2897 	memset(&rsp, 0, sizeof(rsp));
2898 	pma_get_opa_port_ectrs(ibdev, &rsp, port);
2899 
2900 	if (pmp->mad_hdr.attr_mod != 0 || p->port_select != port) {
2901 		pmp->mad_hdr.status |= IB_SMP_INVALID_FIELD;
2902 		goto bail;
2903 	}
2904 
2905 	p->symbol_error_counter = 0; /* N/A for OPA */
2906 
2907 	temp_32 = be32_to_cpu(rsp.link_error_recovery);
2908 	if (temp_32 > 0xFFUL)
2909 		p->link_error_recovery_counter = 0xFF;
2910 	else
2911 		p->link_error_recovery_counter = (u8)temp_32;
2912 
2913 	temp_32 = be32_to_cpu(rsp.link_downed);
2914 	if (temp_32 > 0xFFUL)
2915 		p->link_downed_counter = 0xFF;
2916 	else
2917 		p->link_downed_counter = (u8)temp_32;
2918 
2919 	temp_64 = be64_to_cpu(rsp.port_rcv_errors);
2920 	if (temp_64 > 0xFFFFUL)
2921 		p->port_rcv_errors = cpu_to_be16(0xFFFF);
2922 	else
2923 		p->port_rcv_errors = cpu_to_be16((u16)temp_64);
2924 
2925 	temp_64 = be64_to_cpu(rsp.port_rcv_remote_physical_errors);
2926 	if (temp_64 > 0xFFFFUL)
2927 		p->port_rcv_remphys_errors = cpu_to_be16(0xFFFF);
2928 	else
2929 		p->port_rcv_remphys_errors = cpu_to_be16((u16)temp_64);
2930 
2931 	temp_64 = be64_to_cpu(rsp.port_rcv_switch_relay_errors);
2932 	p->port_rcv_switch_relay_errors = cpu_to_be16((u16)temp_64);
2933 
2934 	temp_64 = be64_to_cpu(rsp.port_xmit_discards);
2935 	if (temp_64 > 0xFFFFUL)
2936 		p->port_xmit_discards = cpu_to_be16(0xFFFF);
2937 	else
2938 		p->port_xmit_discards = cpu_to_be16((u16)temp_64);
2939 
2940 	temp_64 = be64_to_cpu(rsp.port_xmit_constraint_errors);
2941 	if (temp_64 > 0xFFUL)
2942 		p->port_xmit_constraint_errors = 0xFF;
2943 	else
2944 		p->port_xmit_constraint_errors = (u8)temp_64;
2945 
2946 	temp_64 = be64_to_cpu(rsp.port_rcv_constraint_errors);
2947 	if (temp_64 > 0xFFUL)
2948 		p->port_rcv_constraint_errors = 0xFFUL;
2949 	else
2950 		p->port_rcv_constraint_errors = (u8)temp_64;
2951 
2952 	/* LocalLink: 7:4, BufferOverrun: 3:0 */
2953 	temp_64 = be64_to_cpu(rsp.local_link_integrity_errors);
2954 	if (temp_64 > 0xFUL)
2955 		temp_64 = 0xFUL;
2956 
2957 	temp_link_overrun_errors = temp_64 << 4;
2958 
2959 	temp_64 = be64_to_cpu(rsp.excessive_buffer_overruns);
2960 	if (temp_64 > 0xFUL)
2961 		temp_64 = 0xFUL;
2962 	temp_link_overrun_errors |= temp_64;
2963 
2964 	p->link_overrun_errors = (u8)temp_link_overrun_errors;
2965 
2966 	p->vl15_dropped = 0; /* N/A for OPA */
2967 
2968 bail:
2969 	return reply((struct ib_mad_hdr *)pmp);
2970 }
2971 
2972 static int pma_get_opa_errorinfo(struct opa_pma_mad *pmp,
2973 				 struct ib_device *ibdev,
2974 				 u8 port, u32 *resp_len)
2975 {
2976 	size_t response_data_size;
2977 	struct _port_ei *rsp;
2978 	struct opa_port_error_info_msg *req;
2979 	struct hfi1_devdata *dd = dd_from_ibdev(ibdev);
2980 	u64 port_mask;
2981 	u32 num_ports;
2982 	u8 port_num;
2983 	u8 num_pslm;
2984 	u64 reg;
2985 
2986 	req = (struct opa_port_error_info_msg *)pmp->data;
2987 	rsp = &req->port[0];
2988 
2989 	num_ports = OPA_AM_NPORT(be32_to_cpu(pmp->mad_hdr.attr_mod));
2990 	num_pslm = hweight64(be64_to_cpu(req->port_select_mask[3]));
2991 
2992 	memset(rsp, 0, sizeof(*rsp));
2993 
2994 	if (num_ports != 1 || num_ports != num_pslm) {
2995 		pmp->mad_hdr.status |= IB_SMP_INVALID_FIELD;
2996 		return reply((struct ib_mad_hdr *)pmp);
2997 	}
2998 
2999 	/* Sanity check */
3000 	response_data_size = sizeof(struct opa_port_error_info_msg);
3001 
3002 	if (response_data_size > sizeof(pmp->data)) {
3003 		pmp->mad_hdr.status |= IB_SMP_INVALID_FIELD;
3004 		return reply((struct ib_mad_hdr *)pmp);
3005 	}
3006 
3007 	/*
3008 	 * The bit set in the mask needs to be consistent with the port
3009 	 * the request came in on.
3010 	 */
3011 	port_mask = be64_to_cpu(req->port_select_mask[3]);
3012 	port_num = find_first_bit((unsigned long *)&port_mask,
3013 				  sizeof(port_mask));
3014 
3015 	if (port_num != port) {
3016 		pmp->mad_hdr.status |= IB_SMP_INVALID_FIELD;
3017 		return reply((struct ib_mad_hdr *)pmp);
3018 	}
3019 
3020 	/* PortRcvErrorInfo */
3021 	rsp->port_rcv_ei.status_and_code =
3022 		dd->err_info_rcvport.status_and_code;
3023 	memcpy(&rsp->port_rcv_ei.ei.ei1to12.packet_flit1,
3024 	       &dd->err_info_rcvport.packet_flit1, sizeof(u64));
3025 	memcpy(&rsp->port_rcv_ei.ei.ei1to12.packet_flit2,
3026 	       &dd->err_info_rcvport.packet_flit2, sizeof(u64));
3027 
3028 	/* ExcessiverBufferOverrunInfo */
3029 	reg = read_csr(dd, RCV_ERR_INFO);
3030 	if (reg & RCV_ERR_INFO_RCV_EXCESS_BUFFER_OVERRUN_SMASK) {
3031 		/*
3032 		 * if the RcvExcessBufferOverrun bit is set, save SC of
3033 		 * first pkt that encountered an excess buffer overrun
3034 		 */
3035 		u8 tmp = (u8)reg;
3036 
3037 		tmp &=  RCV_ERR_INFO_RCV_EXCESS_BUFFER_OVERRUN_SC_SMASK;
3038 		tmp <<= 2;
3039 		rsp->excessive_buffer_overrun_ei.status_and_sc = tmp;
3040 		/* set the status bit */
3041 		rsp->excessive_buffer_overrun_ei.status_and_sc |= 0x80;
3042 	}
3043 
3044 	rsp->port_xmit_constraint_ei.status =
3045 		dd->err_info_xmit_constraint.status;
3046 	rsp->port_xmit_constraint_ei.pkey =
3047 		cpu_to_be16(dd->err_info_xmit_constraint.pkey);
3048 	rsp->port_xmit_constraint_ei.slid =
3049 		cpu_to_be32(dd->err_info_xmit_constraint.slid);
3050 
3051 	rsp->port_rcv_constraint_ei.status =
3052 		dd->err_info_rcv_constraint.status;
3053 	rsp->port_rcv_constraint_ei.pkey =
3054 		cpu_to_be16(dd->err_info_rcv_constraint.pkey);
3055 	rsp->port_rcv_constraint_ei.slid =
3056 		cpu_to_be32(dd->err_info_rcv_constraint.slid);
3057 
3058 	/* UncorrectableErrorInfo */
3059 	rsp->uncorrectable_ei.status_and_code = dd->err_info_uncorrectable;
3060 
3061 	/* FMConfigErrorInfo */
3062 	rsp->fm_config_ei.status_and_code = dd->err_info_fmconfig;
3063 
3064 	if (resp_len)
3065 		*resp_len += response_data_size;
3066 
3067 	return reply((struct ib_mad_hdr *)pmp);
3068 }
3069 
3070 static int pma_set_opa_portstatus(struct opa_pma_mad *pmp,
3071 				  struct ib_device *ibdev,
3072 				  u8 port, u32 *resp_len)
3073 {
3074 	struct opa_clear_port_status *req =
3075 		(struct opa_clear_port_status *)pmp->data;
3076 	struct hfi1_devdata *dd = dd_from_ibdev(ibdev);
3077 	struct hfi1_ibport *ibp = to_iport(ibdev, port);
3078 	struct hfi1_pportdata *ppd = ppd_from_ibp(ibp);
3079 	u32 nports = be32_to_cpu(pmp->mad_hdr.attr_mod) >> 24;
3080 	u64 portn = be64_to_cpu(req->port_select_mask[3]);
3081 	u32 counter_select = be32_to_cpu(req->counter_select_mask);
3082 	u32 vl_select_mask = VL_MASK_ALL; /* clear all per-vl cnts */
3083 	unsigned long vl;
3084 
3085 	if ((nports != 1) || (portn != 1 << port)) {
3086 		pmp->mad_hdr.status |= IB_SMP_INVALID_FIELD;
3087 		return reply((struct ib_mad_hdr *)pmp);
3088 	}
3089 	/*
3090 	 * only counters returned by pma_get_opa_portstatus() are
3091 	 * handled, so when pma_get_opa_portstatus() gets a fix,
3092 	 * the corresponding change should be made here as well.
3093 	 */
3094 
3095 	if (counter_select & CS_PORT_XMIT_DATA)
3096 		write_dev_cntr(dd, C_DC_XMIT_FLITS, CNTR_INVALID_VL, 0);
3097 
3098 	if (counter_select & CS_PORT_RCV_DATA)
3099 		write_dev_cntr(dd, C_DC_RCV_FLITS, CNTR_INVALID_VL, 0);
3100 
3101 	if (counter_select & CS_PORT_XMIT_PKTS)
3102 		write_dev_cntr(dd, C_DC_XMIT_PKTS, CNTR_INVALID_VL, 0);
3103 
3104 	if (counter_select & CS_PORT_RCV_PKTS)
3105 		write_dev_cntr(dd, C_DC_RCV_PKTS, CNTR_INVALID_VL, 0);
3106 
3107 	if (counter_select & CS_PORT_MCAST_XMIT_PKTS)
3108 		write_dev_cntr(dd, C_DC_MC_XMIT_PKTS, CNTR_INVALID_VL, 0);
3109 
3110 	if (counter_select & CS_PORT_MCAST_RCV_PKTS)
3111 		write_dev_cntr(dd, C_DC_MC_RCV_PKTS, CNTR_INVALID_VL, 0);
3112 
3113 	if (counter_select & CS_PORT_XMIT_WAIT)
3114 		write_port_cntr(ppd, C_TX_WAIT, CNTR_INVALID_VL, 0);
3115 
3116 	/* ignore cs_sw_portCongestion for HFIs */
3117 
3118 	if (counter_select & CS_PORT_RCV_FECN)
3119 		write_dev_cntr(dd, C_DC_RCV_FCN, CNTR_INVALID_VL, 0);
3120 
3121 	if (counter_select & CS_PORT_RCV_BECN)
3122 		write_dev_cntr(dd, C_DC_RCV_BCN, CNTR_INVALID_VL, 0);
3123 
3124 	/* ignore cs_port_xmit_time_cong for HFIs */
3125 	/* ignore cs_port_xmit_wasted_bw for now */
3126 	/* ignore cs_port_xmit_wait_data for now */
3127 	if (counter_select & CS_PORT_RCV_BUBBLE)
3128 		write_dev_cntr(dd, C_DC_RCV_BBL, CNTR_INVALID_VL, 0);
3129 
3130 	/* Only applicable for switch */
3131 	/* if (counter_select & CS_PORT_MARK_FECN)
3132 	 *	write_csr(dd, DCC_PRF_PORT_MARK_FECN_CNT, 0);
3133 	 */
3134 
3135 	if (counter_select & CS_PORT_RCV_CONSTRAINT_ERRORS)
3136 		write_port_cntr(ppd, C_SW_RCV_CSTR_ERR, CNTR_INVALID_VL, 0);
3137 
3138 	/* ignore cs_port_rcv_switch_relay_errors for HFIs */
3139 	if (counter_select & CS_PORT_XMIT_DISCARDS)
3140 		write_port_cntr(ppd, C_SW_XMIT_DSCD, CNTR_INVALID_VL, 0);
3141 
3142 	if (counter_select & CS_PORT_XMIT_CONSTRAINT_ERRORS)
3143 		write_port_cntr(ppd, C_SW_XMIT_CSTR_ERR, CNTR_INVALID_VL, 0);
3144 
3145 	if (counter_select & CS_PORT_RCV_REMOTE_PHYSICAL_ERRORS)
3146 		write_dev_cntr(dd, C_DC_RMT_PHY_ERR, CNTR_INVALID_VL, 0);
3147 
3148 	if (counter_select & CS_LOCAL_LINK_INTEGRITY_ERRORS)
3149 		write_dev_cntr(dd, C_DC_RX_REPLAY, CNTR_INVALID_VL, 0);
3150 
3151 	if (counter_select & CS_LINK_ERROR_RECOVERY) {
3152 		write_dev_cntr(dd, C_DC_SEQ_CRC_CNT, CNTR_INVALID_VL, 0);
3153 		write_dev_cntr(dd, C_DC_REINIT_FROM_PEER_CNT,
3154 			       CNTR_INVALID_VL, 0);
3155 	}
3156 
3157 	if (counter_select & CS_PORT_RCV_ERRORS)
3158 		write_dev_cntr(dd, C_DC_RCV_ERR, CNTR_INVALID_VL, 0);
3159 
3160 	if (counter_select & CS_EXCESSIVE_BUFFER_OVERRUNS) {
3161 		write_dev_cntr(dd, C_RCV_OVF, CNTR_INVALID_VL, 0);
3162 		dd->rcv_ovfl_cnt = 0;
3163 	}
3164 
3165 	if (counter_select & CS_FM_CONFIG_ERRORS)
3166 		write_dev_cntr(dd, C_DC_FM_CFG_ERR, CNTR_INVALID_VL, 0);
3167 
3168 	if (counter_select & CS_LINK_DOWNED)
3169 		write_port_cntr(ppd, C_SW_LINK_DOWN, CNTR_INVALID_VL, 0);
3170 
3171 	if (counter_select & CS_UNCORRECTABLE_ERRORS)
3172 		write_dev_cntr(dd, C_DC_UNC_ERR, CNTR_INVALID_VL, 0);
3173 
3174 	for_each_set_bit(vl, (unsigned long *)&(vl_select_mask),
3175 			 8 * sizeof(vl_select_mask)) {
3176 		if (counter_select & CS_PORT_XMIT_DATA)
3177 			write_port_cntr(ppd, C_TX_FLIT_VL, idx_from_vl(vl), 0);
3178 
3179 		if (counter_select & CS_PORT_RCV_DATA)
3180 			write_dev_cntr(dd, C_DC_RX_FLIT_VL, idx_from_vl(vl), 0);
3181 
3182 		if (counter_select & CS_PORT_XMIT_PKTS)
3183 			write_port_cntr(ppd, C_TX_PKT_VL, idx_from_vl(vl), 0);
3184 
3185 		if (counter_select & CS_PORT_RCV_PKTS)
3186 			write_dev_cntr(dd, C_DC_RX_PKT_VL, idx_from_vl(vl), 0);
3187 
3188 		if (counter_select & CS_PORT_XMIT_WAIT)
3189 			write_port_cntr(ppd, C_TX_WAIT_VL, idx_from_vl(vl), 0);
3190 
3191 		/* sw_port_vl_congestion is 0 for HFIs */
3192 		if (counter_select & CS_PORT_RCV_FECN)
3193 			write_dev_cntr(dd, C_DC_RCV_FCN_VL, idx_from_vl(vl), 0);
3194 
3195 		if (counter_select & CS_PORT_RCV_BECN)
3196 			write_dev_cntr(dd, C_DC_RCV_BCN_VL, idx_from_vl(vl), 0);
3197 
3198 		/* port_vl_xmit_time_cong is 0 for HFIs */
3199 		/* port_vl_xmit_wasted_bw ??? */
3200 		/* port_vl_xmit_wait_data - TXE (table 13-9 HFI spec) ??? */
3201 		if (counter_select & CS_PORT_RCV_BUBBLE)
3202 			write_dev_cntr(dd, C_DC_RCV_BBL_VL, idx_from_vl(vl), 0);
3203 
3204 		/* if (counter_select & CS_PORT_MARK_FECN)
3205 		 *     write_csr(dd, DCC_PRF_PORT_VL_MARK_FECN_CNT + offset, 0);
3206 		 */
3207 		if (counter_select & C_SW_XMIT_DSCD_VL)
3208 			write_port_cntr(ppd, C_SW_XMIT_DSCD_VL,
3209 					idx_from_vl(vl), 0);
3210 	}
3211 
3212 	if (resp_len)
3213 		*resp_len += sizeof(*req);
3214 
3215 	return reply((struct ib_mad_hdr *)pmp);
3216 }
3217 
3218 static int pma_set_opa_errorinfo(struct opa_pma_mad *pmp,
3219 				 struct ib_device *ibdev,
3220 				 u8 port, u32 *resp_len)
3221 {
3222 	struct _port_ei *rsp;
3223 	struct opa_port_error_info_msg *req;
3224 	struct hfi1_devdata *dd = dd_from_ibdev(ibdev);
3225 	u64 port_mask;
3226 	u32 num_ports;
3227 	u8 port_num;
3228 	u8 num_pslm;
3229 	u32 error_info_select;
3230 
3231 	req = (struct opa_port_error_info_msg *)pmp->data;
3232 	rsp = &req->port[0];
3233 
3234 	num_ports = OPA_AM_NPORT(be32_to_cpu(pmp->mad_hdr.attr_mod));
3235 	num_pslm = hweight64(be64_to_cpu(req->port_select_mask[3]));
3236 
3237 	memset(rsp, 0, sizeof(*rsp));
3238 
3239 	if (num_ports != 1 || num_ports != num_pslm) {
3240 		pmp->mad_hdr.status |= IB_SMP_INVALID_FIELD;
3241 		return reply((struct ib_mad_hdr *)pmp);
3242 	}
3243 
3244 	/*
3245 	 * The bit set in the mask needs to be consistent with the port
3246 	 * the request came in on.
3247 	 */
3248 	port_mask = be64_to_cpu(req->port_select_mask[3]);
3249 	port_num = find_first_bit((unsigned long *)&port_mask,
3250 				  sizeof(port_mask));
3251 
3252 	if (port_num != port) {
3253 		pmp->mad_hdr.status |= IB_SMP_INVALID_FIELD;
3254 		return reply((struct ib_mad_hdr *)pmp);
3255 	}
3256 
3257 	error_info_select = be32_to_cpu(req->error_info_select_mask);
3258 
3259 	/* PortRcvErrorInfo */
3260 	if (error_info_select & ES_PORT_RCV_ERROR_INFO)
3261 		/* turn off status bit */
3262 		dd->err_info_rcvport.status_and_code &= ~OPA_EI_STATUS_SMASK;
3263 
3264 	/* ExcessiverBufferOverrunInfo */
3265 	if (error_info_select & ES_EXCESSIVE_BUFFER_OVERRUN_INFO)
3266 		/*
3267 		 * status bit is essentially kept in the h/w - bit 5 of
3268 		 * RCV_ERR_INFO
3269 		 */
3270 		write_csr(dd, RCV_ERR_INFO,
3271 			  RCV_ERR_INFO_RCV_EXCESS_BUFFER_OVERRUN_SMASK);
3272 
3273 	if (error_info_select & ES_PORT_XMIT_CONSTRAINT_ERROR_INFO)
3274 		dd->err_info_xmit_constraint.status &= ~OPA_EI_STATUS_SMASK;
3275 
3276 	if (error_info_select & ES_PORT_RCV_CONSTRAINT_ERROR_INFO)
3277 		dd->err_info_rcv_constraint.status &= ~OPA_EI_STATUS_SMASK;
3278 
3279 	/* UncorrectableErrorInfo */
3280 	if (error_info_select & ES_UNCORRECTABLE_ERROR_INFO)
3281 		/* turn off status bit */
3282 		dd->err_info_uncorrectable &= ~OPA_EI_STATUS_SMASK;
3283 
3284 	/* FMConfigErrorInfo */
3285 	if (error_info_select & ES_FM_CONFIG_ERROR_INFO)
3286 		/* turn off status bit */
3287 		dd->err_info_fmconfig &= ~OPA_EI_STATUS_SMASK;
3288 
3289 	if (resp_len)
3290 		*resp_len += sizeof(*req);
3291 
3292 	return reply((struct ib_mad_hdr *)pmp);
3293 }
3294 
3295 struct opa_congestion_info_attr {
3296 	__be16 congestion_info;
3297 	u8 control_table_cap;	/* Multiple of 64 entry unit CCTs */
3298 	u8 congestion_log_length;
3299 } __packed;
3300 
3301 static int __subn_get_opa_cong_info(struct opa_smp *smp, u32 am, u8 *data,
3302 				    struct ib_device *ibdev, u8 port,
3303 				    u32 *resp_len)
3304 {
3305 	struct opa_congestion_info_attr *p =
3306 		(struct opa_congestion_info_attr *)data;
3307 	struct hfi1_ibport *ibp = to_iport(ibdev, port);
3308 	struct hfi1_pportdata *ppd = ppd_from_ibp(ibp);
3309 
3310 	p->congestion_info = 0;
3311 	p->control_table_cap = ppd->cc_max_table_entries;
3312 	p->congestion_log_length = OPA_CONG_LOG_ELEMS;
3313 
3314 	if (resp_len)
3315 		*resp_len += sizeof(*p);
3316 
3317 	return reply((struct ib_mad_hdr *)smp);
3318 }
3319 
3320 static int __subn_get_opa_cong_setting(struct opa_smp *smp, u32 am,
3321 				       u8 *data, struct ib_device *ibdev,
3322 				       u8 port, u32 *resp_len)
3323 {
3324 	int i;
3325 	struct opa_congestion_setting_attr *p =
3326 		(struct opa_congestion_setting_attr *)data;
3327 	struct hfi1_ibport *ibp = to_iport(ibdev, port);
3328 	struct hfi1_pportdata *ppd = ppd_from_ibp(ibp);
3329 	struct opa_congestion_setting_entry_shadow *entries;
3330 	struct cc_state *cc_state;
3331 
3332 	rcu_read_lock();
3333 
3334 	cc_state = get_cc_state(ppd);
3335 
3336 	if (!cc_state) {
3337 		rcu_read_unlock();
3338 		return reply((struct ib_mad_hdr *)smp);
3339 	}
3340 
3341 	entries = cc_state->cong_setting.entries;
3342 	p->port_control = cpu_to_be16(cc_state->cong_setting.port_control);
3343 	p->control_map = cpu_to_be32(cc_state->cong_setting.control_map);
3344 	for (i = 0; i < OPA_MAX_SLS; i++) {
3345 		p->entries[i].ccti_increase = entries[i].ccti_increase;
3346 		p->entries[i].ccti_timer = cpu_to_be16(entries[i].ccti_timer);
3347 		p->entries[i].trigger_threshold =
3348 			entries[i].trigger_threshold;
3349 		p->entries[i].ccti_min = entries[i].ccti_min;
3350 	}
3351 
3352 	rcu_read_unlock();
3353 
3354 	if (resp_len)
3355 		*resp_len += sizeof(*p);
3356 
3357 	return reply((struct ib_mad_hdr *)smp);
3358 }
3359 
3360 /*
3361  * Apply congestion control information stored in the ppd to the
3362  * active structure.
3363  */
3364 static void apply_cc_state(struct hfi1_pportdata *ppd)
3365 {
3366 	struct cc_state *old_cc_state, *new_cc_state;
3367 
3368 	new_cc_state = kzalloc(sizeof(*new_cc_state), GFP_KERNEL);
3369 	if (!new_cc_state)
3370 		return;
3371 
3372 	/*
3373 	 * Hold the lock for updating *and* to prevent ppd information
3374 	 * from changing during the update.
3375 	 */
3376 	spin_lock(&ppd->cc_state_lock);
3377 
3378 	old_cc_state = get_cc_state_protected(ppd);
3379 	if (!old_cc_state) {
3380 		/* never active, or shutting down */
3381 		spin_unlock(&ppd->cc_state_lock);
3382 		kfree(new_cc_state);
3383 		return;
3384 	}
3385 
3386 	*new_cc_state = *old_cc_state;
3387 
3388 	new_cc_state->cct.ccti_limit = ppd->total_cct_entry - 1;
3389 	memcpy(new_cc_state->cct.entries, ppd->ccti_entries,
3390 	       ppd->total_cct_entry * sizeof(struct ib_cc_table_entry));
3391 
3392 	new_cc_state->cong_setting.port_control = IB_CC_CCS_PC_SL_BASED;
3393 	new_cc_state->cong_setting.control_map = ppd->cc_sl_control_map;
3394 	memcpy(new_cc_state->cong_setting.entries, ppd->congestion_entries,
3395 	       OPA_MAX_SLS * sizeof(struct opa_congestion_setting_entry));
3396 
3397 	rcu_assign_pointer(ppd->cc_state, new_cc_state);
3398 
3399 	spin_unlock(&ppd->cc_state_lock);
3400 
3401 	call_rcu(&old_cc_state->rcu, cc_state_reclaim);
3402 }
3403 
3404 static int __subn_set_opa_cong_setting(struct opa_smp *smp, u32 am, u8 *data,
3405 				       struct ib_device *ibdev, u8 port,
3406 				       u32 *resp_len)
3407 {
3408 	struct opa_congestion_setting_attr *p =
3409 		(struct opa_congestion_setting_attr *)data;
3410 	struct hfi1_ibport *ibp = to_iport(ibdev, port);
3411 	struct hfi1_pportdata *ppd = ppd_from_ibp(ibp);
3412 	struct opa_congestion_setting_entry_shadow *entries;
3413 	int i;
3414 
3415 	/*
3416 	 * Save details from packet into the ppd.  Hold the cc_state_lock so
3417 	 * our information is consistent with anyone trying to apply the state.
3418 	 */
3419 	spin_lock(&ppd->cc_state_lock);
3420 	ppd->cc_sl_control_map = be32_to_cpu(p->control_map);
3421 
3422 	entries = ppd->congestion_entries;
3423 	for (i = 0; i < OPA_MAX_SLS; i++) {
3424 		entries[i].ccti_increase = p->entries[i].ccti_increase;
3425 		entries[i].ccti_timer = be16_to_cpu(p->entries[i].ccti_timer);
3426 		entries[i].trigger_threshold =
3427 			p->entries[i].trigger_threshold;
3428 		entries[i].ccti_min = p->entries[i].ccti_min;
3429 	}
3430 	spin_unlock(&ppd->cc_state_lock);
3431 
3432 	/* now apply the information */
3433 	apply_cc_state(ppd);
3434 
3435 	return __subn_get_opa_cong_setting(smp, am, data, ibdev, port,
3436 					   resp_len);
3437 }
3438 
3439 static int __subn_get_opa_hfi1_cong_log(struct opa_smp *smp, u32 am,
3440 					u8 *data, struct ib_device *ibdev,
3441 					u8 port, u32 *resp_len)
3442 {
3443 	struct hfi1_ibport *ibp = to_iport(ibdev, port);
3444 	struct hfi1_pportdata *ppd = ppd_from_ibp(ibp);
3445 	struct opa_hfi1_cong_log *cong_log = (struct opa_hfi1_cong_log *)data;
3446 	s64 ts;
3447 	int i;
3448 
3449 	if (am != 0) {
3450 		smp->status |= IB_SMP_INVALID_FIELD;
3451 		return reply((struct ib_mad_hdr *)smp);
3452 	}
3453 
3454 	spin_lock_irq(&ppd->cc_log_lock);
3455 
3456 	cong_log->log_type = OPA_CC_LOG_TYPE_HFI;
3457 	cong_log->congestion_flags = 0;
3458 	cong_log->threshold_event_counter =
3459 		cpu_to_be16(ppd->threshold_event_counter);
3460 	memcpy(cong_log->threshold_cong_event_map,
3461 	       ppd->threshold_cong_event_map,
3462 	       sizeof(cong_log->threshold_cong_event_map));
3463 	/* keep timestamp in units of 1.024 usec */
3464 	ts = ktime_to_ns(ktime_get()) / 1024;
3465 	cong_log->current_time_stamp = cpu_to_be32(ts);
3466 	for (i = 0; i < OPA_CONG_LOG_ELEMS; i++) {
3467 		struct opa_hfi1_cong_log_event_internal *cce =
3468 			&ppd->cc_events[ppd->cc_mad_idx++];
3469 		if (ppd->cc_mad_idx == OPA_CONG_LOG_ELEMS)
3470 			ppd->cc_mad_idx = 0;
3471 		/*
3472 		 * Entries which are older than twice the time
3473 		 * required to wrap the counter are supposed to
3474 		 * be zeroed (CA10-49 IBTA, release 1.2.1, V1).
3475 		 */
3476 		if ((u64)(ts - cce->timestamp) > (2 * UINT_MAX))
3477 			continue;
3478 		memcpy(cong_log->events[i].local_qp_cn_entry, &cce->lqpn, 3);
3479 		memcpy(cong_log->events[i].remote_qp_number_cn_entry,
3480 		       &cce->rqpn, 3);
3481 		cong_log->events[i].sl_svc_type_cn_entry =
3482 			((cce->sl & 0x1f) << 3) | (cce->svc_type & 0x7);
3483 		cong_log->events[i].remote_lid_cn_entry =
3484 			cpu_to_be32(cce->rlid);
3485 		cong_log->events[i].timestamp_cn_entry =
3486 			cpu_to_be32(cce->timestamp);
3487 	}
3488 
3489 	/*
3490 	 * Reset threshold_cong_event_map, and threshold_event_counter
3491 	 * to 0 when log is read.
3492 	 */
3493 	memset(ppd->threshold_cong_event_map, 0x0,
3494 	       sizeof(ppd->threshold_cong_event_map));
3495 	ppd->threshold_event_counter = 0;
3496 
3497 	spin_unlock_irq(&ppd->cc_log_lock);
3498 
3499 	if (resp_len)
3500 		*resp_len += sizeof(struct opa_hfi1_cong_log);
3501 
3502 	return reply((struct ib_mad_hdr *)smp);
3503 }
3504 
3505 static int __subn_get_opa_cc_table(struct opa_smp *smp, u32 am, u8 *data,
3506 				   struct ib_device *ibdev, u8 port,
3507 				   u32 *resp_len)
3508 {
3509 	struct ib_cc_table_attr *cc_table_attr =
3510 		(struct ib_cc_table_attr *)data;
3511 	struct hfi1_ibport *ibp = to_iport(ibdev, port);
3512 	struct hfi1_pportdata *ppd = ppd_from_ibp(ibp);
3513 	u32 start_block = OPA_AM_START_BLK(am);
3514 	u32 n_blocks = OPA_AM_NBLK(am);
3515 	struct ib_cc_table_entry_shadow *entries;
3516 	int i, j;
3517 	u32 sentry, eentry;
3518 	struct cc_state *cc_state;
3519 
3520 	/* sanity check n_blocks, start_block */
3521 	if (n_blocks == 0 ||
3522 	    start_block + n_blocks > ppd->cc_max_table_entries) {
3523 		smp->status |= IB_SMP_INVALID_FIELD;
3524 		return reply((struct ib_mad_hdr *)smp);
3525 	}
3526 
3527 	rcu_read_lock();
3528 
3529 	cc_state = get_cc_state(ppd);
3530 
3531 	if (!cc_state) {
3532 		rcu_read_unlock();
3533 		return reply((struct ib_mad_hdr *)smp);
3534 	}
3535 
3536 	sentry = start_block * IB_CCT_ENTRIES;
3537 	eentry = sentry + (IB_CCT_ENTRIES * n_blocks);
3538 
3539 	cc_table_attr->ccti_limit = cpu_to_be16(cc_state->cct.ccti_limit);
3540 
3541 	entries = cc_state->cct.entries;
3542 
3543 	/* return n_blocks, though the last block may not be full */
3544 	for (j = 0, i = sentry; i < eentry; j++, i++)
3545 		cc_table_attr->ccti_entries[j].entry =
3546 			cpu_to_be16(entries[i].entry);
3547 
3548 	rcu_read_unlock();
3549 
3550 	if (resp_len)
3551 		*resp_len += sizeof(u16) * (IB_CCT_ENTRIES * n_blocks + 1);
3552 
3553 	return reply((struct ib_mad_hdr *)smp);
3554 }
3555 
3556 void cc_state_reclaim(struct rcu_head *rcu)
3557 {
3558 	struct cc_state *cc_state = container_of(rcu, struct cc_state, rcu);
3559 
3560 	kfree(cc_state);
3561 }
3562 
3563 static int __subn_set_opa_cc_table(struct opa_smp *smp, u32 am, u8 *data,
3564 				   struct ib_device *ibdev, u8 port,
3565 				   u32 *resp_len)
3566 {
3567 	struct ib_cc_table_attr *p = (struct ib_cc_table_attr *)data;
3568 	struct hfi1_ibport *ibp = to_iport(ibdev, port);
3569 	struct hfi1_pportdata *ppd = ppd_from_ibp(ibp);
3570 	u32 start_block = OPA_AM_START_BLK(am);
3571 	u32 n_blocks = OPA_AM_NBLK(am);
3572 	struct ib_cc_table_entry_shadow *entries;
3573 	int i, j;
3574 	u32 sentry, eentry;
3575 	u16 ccti_limit;
3576 
3577 	/* sanity check n_blocks, start_block */
3578 	if (n_blocks == 0 ||
3579 	    start_block + n_blocks > ppd->cc_max_table_entries) {
3580 		smp->status |= IB_SMP_INVALID_FIELD;
3581 		return reply((struct ib_mad_hdr *)smp);
3582 	}
3583 
3584 	sentry = start_block * IB_CCT_ENTRIES;
3585 	eentry = sentry + ((n_blocks - 1) * IB_CCT_ENTRIES) +
3586 		 (be16_to_cpu(p->ccti_limit)) % IB_CCT_ENTRIES + 1;
3587 
3588 	/* sanity check ccti_limit */
3589 	ccti_limit = be16_to_cpu(p->ccti_limit);
3590 	if (ccti_limit + 1 > eentry) {
3591 		smp->status |= IB_SMP_INVALID_FIELD;
3592 		return reply((struct ib_mad_hdr *)smp);
3593 	}
3594 
3595 	/*
3596 	 * Save details from packet into the ppd.  Hold the cc_state_lock so
3597 	 * our information is consistent with anyone trying to apply the state.
3598 	 */
3599 	spin_lock(&ppd->cc_state_lock);
3600 	ppd->total_cct_entry = ccti_limit + 1;
3601 	entries = ppd->ccti_entries;
3602 	for (j = 0, i = sentry; i < eentry; j++, i++)
3603 		entries[i].entry = be16_to_cpu(p->ccti_entries[j].entry);
3604 	spin_unlock(&ppd->cc_state_lock);
3605 
3606 	/* now apply the information */
3607 	apply_cc_state(ppd);
3608 
3609 	return __subn_get_opa_cc_table(smp, am, data, ibdev, port, resp_len);
3610 }
3611 
3612 struct opa_led_info {
3613 	__be32 rsvd_led_mask;
3614 	__be32 rsvd;
3615 };
3616 
3617 #define OPA_LED_SHIFT	31
3618 #define OPA_LED_MASK	BIT(OPA_LED_SHIFT)
3619 
3620 static int __subn_get_opa_led_info(struct opa_smp *smp, u32 am, u8 *data,
3621 				   struct ib_device *ibdev, u8 port,
3622 				   u32 *resp_len)
3623 {
3624 	struct hfi1_devdata *dd = dd_from_ibdev(ibdev);
3625 	struct hfi1_pportdata *ppd = dd->pport;
3626 	struct opa_led_info *p = (struct opa_led_info *)data;
3627 	u32 nport = OPA_AM_NPORT(am);
3628 	u32 is_beaconing_active;
3629 
3630 	if (nport != 1) {
3631 		smp->status |= IB_SMP_INVALID_FIELD;
3632 		return reply((struct ib_mad_hdr *)smp);
3633 	}
3634 
3635 	/*
3636 	 * This pairs with the memory barrier in hfi1_start_led_override to
3637 	 * ensure that we read the correct state of LED beaconing represented
3638 	 * by led_override_timer_active
3639 	 */
3640 	smp_rmb();
3641 	is_beaconing_active = !!atomic_read(&ppd->led_override_timer_active);
3642 	p->rsvd_led_mask = cpu_to_be32(is_beaconing_active << OPA_LED_SHIFT);
3643 
3644 	if (resp_len)
3645 		*resp_len += sizeof(struct opa_led_info);
3646 
3647 	return reply((struct ib_mad_hdr *)smp);
3648 }
3649 
3650 static int __subn_set_opa_led_info(struct opa_smp *smp, u32 am, u8 *data,
3651 				   struct ib_device *ibdev, u8 port,
3652 				   u32 *resp_len)
3653 {
3654 	struct hfi1_devdata *dd = dd_from_ibdev(ibdev);
3655 	struct opa_led_info *p = (struct opa_led_info *)data;
3656 	u32 nport = OPA_AM_NPORT(am);
3657 	int on = !!(be32_to_cpu(p->rsvd_led_mask) & OPA_LED_MASK);
3658 
3659 	if (nport != 1) {
3660 		smp->status |= IB_SMP_INVALID_FIELD;
3661 		return reply((struct ib_mad_hdr *)smp);
3662 	}
3663 
3664 	if (on)
3665 		hfi1_start_led_override(dd->pport, 2000, 1500);
3666 	else
3667 		shutdown_led_override(dd->pport);
3668 
3669 	return __subn_get_opa_led_info(smp, am, data, ibdev, port, resp_len);
3670 }
3671 
3672 static int subn_get_opa_sma(__be16 attr_id, struct opa_smp *smp, u32 am,
3673 			    u8 *data, struct ib_device *ibdev, u8 port,
3674 			    u32 *resp_len)
3675 {
3676 	int ret;
3677 	struct hfi1_ibport *ibp = to_iport(ibdev, port);
3678 
3679 	switch (attr_id) {
3680 	case IB_SMP_ATTR_NODE_DESC:
3681 		ret = __subn_get_opa_nodedesc(smp, am, data, ibdev, port,
3682 					      resp_len);
3683 		break;
3684 	case IB_SMP_ATTR_NODE_INFO:
3685 		ret = __subn_get_opa_nodeinfo(smp, am, data, ibdev, port,
3686 					      resp_len);
3687 		break;
3688 	case IB_SMP_ATTR_PORT_INFO:
3689 		ret = __subn_get_opa_portinfo(smp, am, data, ibdev, port,
3690 					      resp_len);
3691 		break;
3692 	case IB_SMP_ATTR_PKEY_TABLE:
3693 		ret = __subn_get_opa_pkeytable(smp, am, data, ibdev, port,
3694 					       resp_len);
3695 		break;
3696 	case OPA_ATTRIB_ID_SL_TO_SC_MAP:
3697 		ret = __subn_get_opa_sl_to_sc(smp, am, data, ibdev, port,
3698 					      resp_len);
3699 		break;
3700 	case OPA_ATTRIB_ID_SC_TO_SL_MAP:
3701 		ret = __subn_get_opa_sc_to_sl(smp, am, data, ibdev, port,
3702 					      resp_len);
3703 		break;
3704 	case OPA_ATTRIB_ID_SC_TO_VLT_MAP:
3705 		ret = __subn_get_opa_sc_to_vlt(smp, am, data, ibdev, port,
3706 					       resp_len);
3707 		break;
3708 	case OPA_ATTRIB_ID_SC_TO_VLNT_MAP:
3709 		ret = __subn_get_opa_sc_to_vlnt(smp, am, data, ibdev, port,
3710 						resp_len);
3711 		break;
3712 	case OPA_ATTRIB_ID_PORT_STATE_INFO:
3713 		ret = __subn_get_opa_psi(smp, am, data, ibdev, port,
3714 					 resp_len);
3715 		break;
3716 	case OPA_ATTRIB_ID_BUFFER_CONTROL_TABLE:
3717 		ret = __subn_get_opa_bct(smp, am, data, ibdev, port,
3718 					 resp_len);
3719 		break;
3720 	case OPA_ATTRIB_ID_CABLE_INFO:
3721 		ret = __subn_get_opa_cable_info(smp, am, data, ibdev, port,
3722 						resp_len);
3723 		break;
3724 	case IB_SMP_ATTR_VL_ARB_TABLE:
3725 		ret = __subn_get_opa_vl_arb(smp, am, data, ibdev, port,
3726 					    resp_len);
3727 		break;
3728 	case OPA_ATTRIB_ID_CONGESTION_INFO:
3729 		ret = __subn_get_opa_cong_info(smp, am, data, ibdev, port,
3730 					       resp_len);
3731 		break;
3732 	case OPA_ATTRIB_ID_HFI_CONGESTION_SETTING:
3733 		ret = __subn_get_opa_cong_setting(smp, am, data, ibdev,
3734 						  port, resp_len);
3735 		break;
3736 	case OPA_ATTRIB_ID_HFI_CONGESTION_LOG:
3737 		ret = __subn_get_opa_hfi1_cong_log(smp, am, data, ibdev,
3738 						   port, resp_len);
3739 		break;
3740 	case OPA_ATTRIB_ID_CONGESTION_CONTROL_TABLE:
3741 		ret = __subn_get_opa_cc_table(smp, am, data, ibdev, port,
3742 					      resp_len);
3743 		break;
3744 	case IB_SMP_ATTR_LED_INFO:
3745 		ret = __subn_get_opa_led_info(smp, am, data, ibdev, port,
3746 					      resp_len);
3747 		break;
3748 	case IB_SMP_ATTR_SM_INFO:
3749 		if (ibp->rvp.port_cap_flags & IB_PORT_SM_DISABLED)
3750 			return IB_MAD_RESULT_SUCCESS | IB_MAD_RESULT_CONSUMED;
3751 		if (ibp->rvp.port_cap_flags & IB_PORT_SM)
3752 			return IB_MAD_RESULT_SUCCESS;
3753 		/* FALLTHROUGH */
3754 	default:
3755 		smp->status |= IB_SMP_UNSUP_METH_ATTR;
3756 		ret = reply((struct ib_mad_hdr *)smp);
3757 		break;
3758 	}
3759 	return ret;
3760 }
3761 
3762 static int subn_set_opa_sma(__be16 attr_id, struct opa_smp *smp, u32 am,
3763 			    u8 *data, struct ib_device *ibdev, u8 port,
3764 			    u32 *resp_len)
3765 {
3766 	int ret;
3767 	struct hfi1_ibport *ibp = to_iport(ibdev, port);
3768 
3769 	switch (attr_id) {
3770 	case IB_SMP_ATTR_PORT_INFO:
3771 		ret = __subn_set_opa_portinfo(smp, am, data, ibdev, port,
3772 					      resp_len);
3773 		break;
3774 	case IB_SMP_ATTR_PKEY_TABLE:
3775 		ret = __subn_set_opa_pkeytable(smp, am, data, ibdev, port,
3776 					       resp_len);
3777 		break;
3778 	case OPA_ATTRIB_ID_SL_TO_SC_MAP:
3779 		ret = __subn_set_opa_sl_to_sc(smp, am, data, ibdev, port,
3780 					      resp_len);
3781 		break;
3782 	case OPA_ATTRIB_ID_SC_TO_SL_MAP:
3783 		ret = __subn_set_opa_sc_to_sl(smp, am, data, ibdev, port,
3784 					      resp_len);
3785 		break;
3786 	case OPA_ATTRIB_ID_SC_TO_VLT_MAP:
3787 		ret = __subn_set_opa_sc_to_vlt(smp, am, data, ibdev, port,
3788 					       resp_len);
3789 		break;
3790 	case OPA_ATTRIB_ID_SC_TO_VLNT_MAP:
3791 		ret = __subn_set_opa_sc_to_vlnt(smp, am, data, ibdev, port,
3792 						resp_len);
3793 		break;
3794 	case OPA_ATTRIB_ID_PORT_STATE_INFO:
3795 		ret = __subn_set_opa_psi(smp, am, data, ibdev, port,
3796 					 resp_len);
3797 		break;
3798 	case OPA_ATTRIB_ID_BUFFER_CONTROL_TABLE:
3799 		ret = __subn_set_opa_bct(smp, am, data, ibdev, port,
3800 					 resp_len);
3801 		break;
3802 	case IB_SMP_ATTR_VL_ARB_TABLE:
3803 		ret = __subn_set_opa_vl_arb(smp, am, data, ibdev, port,
3804 					    resp_len);
3805 		break;
3806 	case OPA_ATTRIB_ID_HFI_CONGESTION_SETTING:
3807 		ret = __subn_set_opa_cong_setting(smp, am, data, ibdev,
3808 						  port, resp_len);
3809 		break;
3810 	case OPA_ATTRIB_ID_CONGESTION_CONTROL_TABLE:
3811 		ret = __subn_set_opa_cc_table(smp, am, data, ibdev, port,
3812 					      resp_len);
3813 		break;
3814 	case IB_SMP_ATTR_LED_INFO:
3815 		ret = __subn_set_opa_led_info(smp, am, data, ibdev, port,
3816 					      resp_len);
3817 		break;
3818 	case IB_SMP_ATTR_SM_INFO:
3819 		if (ibp->rvp.port_cap_flags & IB_PORT_SM_DISABLED)
3820 			return IB_MAD_RESULT_SUCCESS | IB_MAD_RESULT_CONSUMED;
3821 		if (ibp->rvp.port_cap_flags & IB_PORT_SM)
3822 			return IB_MAD_RESULT_SUCCESS;
3823 		/* FALLTHROUGH */
3824 	default:
3825 		smp->status |= IB_SMP_UNSUP_METH_ATTR;
3826 		ret = reply((struct ib_mad_hdr *)smp);
3827 		break;
3828 	}
3829 	return ret;
3830 }
3831 
3832 static inline void set_aggr_error(struct opa_aggregate *ag)
3833 {
3834 	ag->err_reqlength |= cpu_to_be16(0x8000);
3835 }
3836 
3837 static int subn_get_opa_aggregate(struct opa_smp *smp,
3838 				  struct ib_device *ibdev, u8 port,
3839 				  u32 *resp_len)
3840 {
3841 	int i;
3842 	u32 num_attr = be32_to_cpu(smp->attr_mod) & 0x000000ff;
3843 	u8 *next_smp = opa_get_smp_data(smp);
3844 
3845 	if (num_attr < 1 || num_attr > 117) {
3846 		smp->status |= IB_SMP_INVALID_FIELD;
3847 		return reply((struct ib_mad_hdr *)smp);
3848 	}
3849 
3850 	for (i = 0; i < num_attr; i++) {
3851 		struct opa_aggregate *agg;
3852 		size_t agg_data_len;
3853 		size_t agg_size;
3854 		u32 am;
3855 
3856 		agg = (struct opa_aggregate *)next_smp;
3857 		agg_data_len = (be16_to_cpu(agg->err_reqlength) & 0x007f) * 8;
3858 		agg_size = sizeof(*agg) + agg_data_len;
3859 		am = be32_to_cpu(agg->attr_mod);
3860 
3861 		*resp_len += agg_size;
3862 
3863 		if (next_smp + agg_size > ((u8 *)smp) + sizeof(*smp)) {
3864 			smp->status |= IB_SMP_INVALID_FIELD;
3865 			return reply((struct ib_mad_hdr *)smp);
3866 		}
3867 
3868 		/* zero the payload for this segment */
3869 		memset(next_smp + sizeof(*agg), 0, agg_data_len);
3870 
3871 		(void)subn_get_opa_sma(agg->attr_id, smp, am, agg->data,
3872 					ibdev, port, NULL);
3873 		if (smp->status & ~IB_SMP_DIRECTION) {
3874 			set_aggr_error(agg);
3875 			return reply((struct ib_mad_hdr *)smp);
3876 		}
3877 		next_smp += agg_size;
3878 	}
3879 
3880 	return reply((struct ib_mad_hdr *)smp);
3881 }
3882 
3883 static int subn_set_opa_aggregate(struct opa_smp *smp,
3884 				  struct ib_device *ibdev, u8 port,
3885 				  u32 *resp_len)
3886 {
3887 	int i;
3888 	u32 num_attr = be32_to_cpu(smp->attr_mod) & 0x000000ff;
3889 	u8 *next_smp = opa_get_smp_data(smp);
3890 
3891 	if (num_attr < 1 || num_attr > 117) {
3892 		smp->status |= IB_SMP_INVALID_FIELD;
3893 		return reply((struct ib_mad_hdr *)smp);
3894 	}
3895 
3896 	for (i = 0; i < num_attr; i++) {
3897 		struct opa_aggregate *agg;
3898 		size_t agg_data_len;
3899 		size_t agg_size;
3900 		u32 am;
3901 
3902 		agg = (struct opa_aggregate *)next_smp;
3903 		agg_data_len = (be16_to_cpu(agg->err_reqlength) & 0x007f) * 8;
3904 		agg_size = sizeof(*agg) + agg_data_len;
3905 		am = be32_to_cpu(agg->attr_mod);
3906 
3907 		*resp_len += agg_size;
3908 
3909 		if (next_smp + agg_size > ((u8 *)smp) + sizeof(*smp)) {
3910 			smp->status |= IB_SMP_INVALID_FIELD;
3911 			return reply((struct ib_mad_hdr *)smp);
3912 		}
3913 
3914 		(void)subn_set_opa_sma(agg->attr_id, smp, am, agg->data,
3915 					ibdev, port, NULL);
3916 		if (smp->status & ~IB_SMP_DIRECTION) {
3917 			set_aggr_error(agg);
3918 			return reply((struct ib_mad_hdr *)smp);
3919 		}
3920 		next_smp += agg_size;
3921 	}
3922 
3923 	return reply((struct ib_mad_hdr *)smp);
3924 }
3925 
3926 /*
3927  * OPAv1 specifies that, on the transition to link up, these counters
3928  * are cleared:
3929  *   PortRcvErrors [*]
3930  *   LinkErrorRecovery
3931  *   LocalLinkIntegrityErrors
3932  *   ExcessiveBufferOverruns [*]
3933  *
3934  * [*] Error info associated with these counters is retained, but the
3935  * error info status is reset to 0.
3936  */
3937 void clear_linkup_counters(struct hfi1_devdata *dd)
3938 {
3939 	/* PortRcvErrors */
3940 	write_dev_cntr(dd, C_DC_RCV_ERR, CNTR_INVALID_VL, 0);
3941 	dd->err_info_rcvport.status_and_code &= ~OPA_EI_STATUS_SMASK;
3942 	/* LinkErrorRecovery */
3943 	write_dev_cntr(dd, C_DC_SEQ_CRC_CNT, CNTR_INVALID_VL, 0);
3944 	write_dev_cntr(dd, C_DC_REINIT_FROM_PEER_CNT, CNTR_INVALID_VL, 0);
3945 	/* LocalLinkIntegrityErrors */
3946 	write_dev_cntr(dd, C_DC_RX_REPLAY, CNTR_INVALID_VL, 0);
3947 	/* ExcessiveBufferOverruns */
3948 	write_dev_cntr(dd, C_RCV_OVF, CNTR_INVALID_VL, 0);
3949 	dd->rcv_ovfl_cnt = 0;
3950 	dd->err_info_xmit_constraint.status &= ~OPA_EI_STATUS_SMASK;
3951 }
3952 
3953 /*
3954  * is_local_mad() returns 1 if 'mad' is sent from, and destined to the
3955  * local node, 0 otherwise.
3956  */
3957 static int is_local_mad(struct hfi1_ibport *ibp, const struct opa_mad *mad,
3958 			const struct ib_wc *in_wc)
3959 {
3960 	struct hfi1_pportdata *ppd = ppd_from_ibp(ibp);
3961 	const struct opa_smp *smp = (const struct opa_smp *)mad;
3962 
3963 	if (smp->mgmt_class == IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE) {
3964 		return (smp->hop_cnt == 0 &&
3965 			smp->route.dr.dr_slid == OPA_LID_PERMISSIVE &&
3966 			smp->route.dr.dr_dlid == OPA_LID_PERMISSIVE);
3967 	}
3968 
3969 	return (in_wc->slid == ppd->lid);
3970 }
3971 
3972 /*
3973  * opa_local_smp_check() should only be called on MADs for which
3974  * is_local_mad() returns true. It applies the SMP checks that are
3975  * specific to SMPs which are sent from, and destined to this node.
3976  * opa_local_smp_check() returns 0 if the SMP passes its checks, 1
3977  * otherwise.
3978  *
3979  * SMPs which arrive from other nodes are instead checked by
3980  * opa_smp_check().
3981  */
3982 static int opa_local_smp_check(struct hfi1_ibport *ibp,
3983 			       const struct ib_wc *in_wc)
3984 {
3985 	struct hfi1_pportdata *ppd = ppd_from_ibp(ibp);
3986 	u16 slid = in_wc->slid;
3987 	u16 pkey;
3988 
3989 	if (in_wc->pkey_index >= ARRAY_SIZE(ppd->pkeys))
3990 		return 1;
3991 
3992 	pkey = ppd->pkeys[in_wc->pkey_index];
3993 	/*
3994 	 * We need to do the "node-local" checks specified in OPAv1,
3995 	 * rev 0.90, section 9.10.26, which are:
3996 	 *   - pkey is 0x7fff, or 0xffff
3997 	 *   - Source QPN == 0 || Destination QPN == 0
3998 	 *   - the MAD header's management class is either
3999 	 *     IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE or
4000 	 *     IB_MGMT_CLASS_SUBN_LID_ROUTED
4001 	 *   - SLID != 0
4002 	 *
4003 	 * However, we know (and so don't need to check again) that,
4004 	 * for local SMPs, the MAD stack passes MADs with:
4005 	 *   - Source QPN of 0
4006 	 *   - MAD mgmt_class is IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE
4007 	 *   - SLID is either: OPA_LID_PERMISSIVE (0xFFFFFFFF), or
4008 	 *     our own port's lid
4009 	 *
4010 	 */
4011 	if (pkey == LIM_MGMT_P_KEY || pkey == FULL_MGMT_P_KEY)
4012 		return 0;
4013 	ingress_pkey_table_fail(ppd, pkey, slid);
4014 	return 1;
4015 }
4016 
4017 static int process_subn_opa(struct ib_device *ibdev, int mad_flags,
4018 			    u8 port, const struct opa_mad *in_mad,
4019 			    struct opa_mad *out_mad,
4020 			    u32 *resp_len)
4021 {
4022 	struct opa_smp *smp = (struct opa_smp *)out_mad;
4023 	struct hfi1_ibport *ibp = to_iport(ibdev, port);
4024 	u8 *data;
4025 	u32 am;
4026 	__be16 attr_id;
4027 	int ret;
4028 
4029 	*out_mad = *in_mad;
4030 	data = opa_get_smp_data(smp);
4031 
4032 	am = be32_to_cpu(smp->attr_mod);
4033 	attr_id = smp->attr_id;
4034 	if (smp->class_version != OPA_SMI_CLASS_VERSION) {
4035 		smp->status |= IB_SMP_UNSUP_VERSION;
4036 		ret = reply((struct ib_mad_hdr *)smp);
4037 		return ret;
4038 	}
4039 	ret = check_mkey(ibp, (struct ib_mad_hdr *)smp, mad_flags, smp->mkey,
4040 			 smp->route.dr.dr_slid, smp->route.dr.return_path,
4041 			 smp->hop_cnt);
4042 	if (ret) {
4043 		u32 port_num = be32_to_cpu(smp->attr_mod);
4044 
4045 		/*
4046 		 * If this is a get/set portinfo, we already check the
4047 		 * M_Key if the MAD is for another port and the M_Key
4048 		 * is OK on the receiving port. This check is needed
4049 		 * to increment the error counters when the M_Key
4050 		 * fails to match on *both* ports.
4051 		 */
4052 		if (attr_id == IB_SMP_ATTR_PORT_INFO &&
4053 		    (smp->method == IB_MGMT_METHOD_GET ||
4054 		     smp->method == IB_MGMT_METHOD_SET) &&
4055 		    port_num && port_num <= ibdev->phys_port_cnt &&
4056 		    port != port_num)
4057 			(void)check_mkey(to_iport(ibdev, port_num),
4058 					  (struct ib_mad_hdr *)smp, 0,
4059 					  smp->mkey, smp->route.dr.dr_slid,
4060 					  smp->route.dr.return_path,
4061 					  smp->hop_cnt);
4062 		ret = IB_MAD_RESULT_FAILURE;
4063 		return ret;
4064 	}
4065 
4066 	*resp_len = opa_get_smp_header_size(smp);
4067 
4068 	switch (smp->method) {
4069 	case IB_MGMT_METHOD_GET:
4070 		switch (attr_id) {
4071 		default:
4072 			clear_opa_smp_data(smp);
4073 			ret = subn_get_opa_sma(attr_id, smp, am, data,
4074 					       ibdev, port, resp_len);
4075 			break;
4076 		case OPA_ATTRIB_ID_AGGREGATE:
4077 			ret = subn_get_opa_aggregate(smp, ibdev, port,
4078 						     resp_len);
4079 			break;
4080 		}
4081 		break;
4082 	case IB_MGMT_METHOD_SET:
4083 		switch (attr_id) {
4084 		default:
4085 			ret = subn_set_opa_sma(attr_id, smp, am, data,
4086 					       ibdev, port, resp_len);
4087 			break;
4088 		case OPA_ATTRIB_ID_AGGREGATE:
4089 			ret = subn_set_opa_aggregate(smp, ibdev, port,
4090 						     resp_len);
4091 			break;
4092 		}
4093 		break;
4094 	case IB_MGMT_METHOD_TRAP:
4095 	case IB_MGMT_METHOD_REPORT:
4096 	case IB_MGMT_METHOD_REPORT_RESP:
4097 	case IB_MGMT_METHOD_GET_RESP:
4098 		/*
4099 		 * The ib_mad module will call us to process responses
4100 		 * before checking for other consumers.
4101 		 * Just tell the caller to process it normally.
4102 		 */
4103 		ret = IB_MAD_RESULT_SUCCESS;
4104 		break;
4105 	default:
4106 		smp->status |= IB_SMP_UNSUP_METHOD;
4107 		ret = reply((struct ib_mad_hdr *)smp);
4108 		break;
4109 	}
4110 
4111 	return ret;
4112 }
4113 
4114 static int process_subn(struct ib_device *ibdev, int mad_flags,
4115 			u8 port, const struct ib_mad *in_mad,
4116 			struct ib_mad *out_mad)
4117 {
4118 	struct ib_smp *smp = (struct ib_smp *)out_mad;
4119 	struct hfi1_ibport *ibp = to_iport(ibdev, port);
4120 	int ret;
4121 
4122 	*out_mad = *in_mad;
4123 	if (smp->class_version != 1) {
4124 		smp->status |= IB_SMP_UNSUP_VERSION;
4125 		ret = reply((struct ib_mad_hdr *)smp);
4126 		return ret;
4127 	}
4128 
4129 	ret = check_mkey(ibp, (struct ib_mad_hdr *)smp, mad_flags,
4130 			 smp->mkey, (__force __be32)smp->dr_slid,
4131 			 smp->return_path, smp->hop_cnt);
4132 	if (ret) {
4133 		u32 port_num = be32_to_cpu(smp->attr_mod);
4134 
4135 		/*
4136 		 * If this is a get/set portinfo, we already check the
4137 		 * M_Key if the MAD is for another port and the M_Key
4138 		 * is OK on the receiving port. This check is needed
4139 		 * to increment the error counters when the M_Key
4140 		 * fails to match on *both* ports.
4141 		 */
4142 		if (in_mad->mad_hdr.attr_id == IB_SMP_ATTR_PORT_INFO &&
4143 		    (smp->method == IB_MGMT_METHOD_GET ||
4144 		     smp->method == IB_MGMT_METHOD_SET) &&
4145 		    port_num && port_num <= ibdev->phys_port_cnt &&
4146 		    port != port_num)
4147 			(void)check_mkey(to_iport(ibdev, port_num),
4148 					 (struct ib_mad_hdr *)smp, 0,
4149 					 smp->mkey,
4150 					 (__force __be32)smp->dr_slid,
4151 					 smp->return_path, smp->hop_cnt);
4152 		ret = IB_MAD_RESULT_FAILURE;
4153 		return ret;
4154 	}
4155 
4156 	switch (smp->method) {
4157 	case IB_MGMT_METHOD_GET:
4158 		switch (smp->attr_id) {
4159 		case IB_SMP_ATTR_NODE_INFO:
4160 			ret = subn_get_nodeinfo(smp, ibdev, port);
4161 			break;
4162 		default:
4163 			smp->status |= IB_SMP_UNSUP_METH_ATTR;
4164 			ret = reply((struct ib_mad_hdr *)smp);
4165 			break;
4166 		}
4167 		break;
4168 	}
4169 
4170 	return ret;
4171 }
4172 
4173 static int process_perf(struct ib_device *ibdev, u8 port,
4174 			const struct ib_mad *in_mad,
4175 			struct ib_mad *out_mad)
4176 {
4177 	struct ib_pma_mad *pmp = (struct ib_pma_mad *)out_mad;
4178 	struct ib_class_port_info *cpi = (struct ib_class_port_info *)
4179 						&pmp->data;
4180 	int ret = IB_MAD_RESULT_FAILURE;
4181 
4182 	*out_mad = *in_mad;
4183 	if (pmp->mad_hdr.class_version != 1) {
4184 		pmp->mad_hdr.status |= IB_SMP_UNSUP_VERSION;
4185 		ret = reply((struct ib_mad_hdr *)pmp);
4186 		return ret;
4187 	}
4188 
4189 	switch (pmp->mad_hdr.method) {
4190 	case IB_MGMT_METHOD_GET:
4191 		switch (pmp->mad_hdr.attr_id) {
4192 		case IB_PMA_PORT_COUNTERS:
4193 			ret = pma_get_ib_portcounters(pmp, ibdev, port);
4194 			break;
4195 		case IB_PMA_PORT_COUNTERS_EXT:
4196 			ret = pma_get_ib_portcounters_ext(pmp, ibdev, port);
4197 			break;
4198 		case IB_PMA_CLASS_PORT_INFO:
4199 			cpi->capability_mask = IB_PMA_CLASS_CAP_EXT_WIDTH;
4200 			ret = reply((struct ib_mad_hdr *)pmp);
4201 			break;
4202 		default:
4203 			pmp->mad_hdr.status |= IB_SMP_UNSUP_METH_ATTR;
4204 			ret = reply((struct ib_mad_hdr *)pmp);
4205 			break;
4206 		}
4207 		break;
4208 
4209 	case IB_MGMT_METHOD_SET:
4210 		if (pmp->mad_hdr.attr_id) {
4211 			pmp->mad_hdr.status |= IB_SMP_UNSUP_METH_ATTR;
4212 			ret = reply((struct ib_mad_hdr *)pmp);
4213 		}
4214 		break;
4215 
4216 	case IB_MGMT_METHOD_TRAP:
4217 	case IB_MGMT_METHOD_GET_RESP:
4218 		/*
4219 		 * The ib_mad module will call us to process responses
4220 		 * before checking for other consumers.
4221 		 * Just tell the caller to process it normally.
4222 		 */
4223 		ret = IB_MAD_RESULT_SUCCESS;
4224 		break;
4225 
4226 	default:
4227 		pmp->mad_hdr.status |= IB_SMP_UNSUP_METHOD;
4228 		ret = reply((struct ib_mad_hdr *)pmp);
4229 		break;
4230 	}
4231 
4232 	return ret;
4233 }
4234 
4235 static int process_perf_opa(struct ib_device *ibdev, u8 port,
4236 			    const struct opa_mad *in_mad,
4237 			    struct opa_mad *out_mad, u32 *resp_len)
4238 {
4239 	struct opa_pma_mad *pmp = (struct opa_pma_mad *)out_mad;
4240 	int ret;
4241 
4242 	*out_mad = *in_mad;
4243 
4244 	if (pmp->mad_hdr.class_version != OPA_SMI_CLASS_VERSION) {
4245 		pmp->mad_hdr.status |= IB_SMP_UNSUP_VERSION;
4246 		return reply((struct ib_mad_hdr *)pmp);
4247 	}
4248 
4249 	*resp_len = sizeof(pmp->mad_hdr);
4250 
4251 	switch (pmp->mad_hdr.method) {
4252 	case IB_MGMT_METHOD_GET:
4253 		switch (pmp->mad_hdr.attr_id) {
4254 		case IB_PMA_CLASS_PORT_INFO:
4255 			ret = pma_get_opa_classportinfo(pmp, ibdev, resp_len);
4256 			break;
4257 		case OPA_PM_ATTRIB_ID_PORT_STATUS:
4258 			ret = pma_get_opa_portstatus(pmp, ibdev, port,
4259 						     resp_len);
4260 			break;
4261 		case OPA_PM_ATTRIB_ID_DATA_PORT_COUNTERS:
4262 			ret = pma_get_opa_datacounters(pmp, ibdev, port,
4263 						       resp_len);
4264 			break;
4265 		case OPA_PM_ATTRIB_ID_ERROR_PORT_COUNTERS:
4266 			ret = pma_get_opa_porterrors(pmp, ibdev, port,
4267 						     resp_len);
4268 			break;
4269 		case OPA_PM_ATTRIB_ID_ERROR_INFO:
4270 			ret = pma_get_opa_errorinfo(pmp, ibdev, port,
4271 						    resp_len);
4272 			break;
4273 		default:
4274 			pmp->mad_hdr.status |= IB_SMP_UNSUP_METH_ATTR;
4275 			ret = reply((struct ib_mad_hdr *)pmp);
4276 			break;
4277 		}
4278 		break;
4279 
4280 	case IB_MGMT_METHOD_SET:
4281 		switch (pmp->mad_hdr.attr_id) {
4282 		case OPA_PM_ATTRIB_ID_CLEAR_PORT_STATUS:
4283 			ret = pma_set_opa_portstatus(pmp, ibdev, port,
4284 						     resp_len);
4285 			break;
4286 		case OPA_PM_ATTRIB_ID_ERROR_INFO:
4287 			ret = pma_set_opa_errorinfo(pmp, ibdev, port,
4288 						    resp_len);
4289 			break;
4290 		default:
4291 			pmp->mad_hdr.status |= IB_SMP_UNSUP_METH_ATTR;
4292 			ret = reply((struct ib_mad_hdr *)pmp);
4293 			break;
4294 		}
4295 		break;
4296 
4297 	case IB_MGMT_METHOD_TRAP:
4298 	case IB_MGMT_METHOD_GET_RESP:
4299 		/*
4300 		 * The ib_mad module will call us to process responses
4301 		 * before checking for other consumers.
4302 		 * Just tell the caller to process it normally.
4303 		 */
4304 		ret = IB_MAD_RESULT_SUCCESS;
4305 		break;
4306 
4307 	default:
4308 		pmp->mad_hdr.status |= IB_SMP_UNSUP_METHOD;
4309 		ret = reply((struct ib_mad_hdr *)pmp);
4310 		break;
4311 	}
4312 
4313 	return ret;
4314 }
4315 
4316 static int hfi1_process_opa_mad(struct ib_device *ibdev, int mad_flags,
4317 				u8 port, const struct ib_wc *in_wc,
4318 				const struct ib_grh *in_grh,
4319 				const struct opa_mad *in_mad,
4320 				struct opa_mad *out_mad, size_t *out_mad_size,
4321 				u16 *out_mad_pkey_index)
4322 {
4323 	int ret;
4324 	int pkey_idx;
4325 	u32 resp_len = 0;
4326 	struct hfi1_ibport *ibp = to_iport(ibdev, port);
4327 
4328 	pkey_idx = hfi1_lookup_pkey_idx(ibp, LIM_MGMT_P_KEY);
4329 	if (pkey_idx < 0) {
4330 		pr_warn("failed to find limited mgmt pkey, defaulting 0x%x\n",
4331 			hfi1_get_pkey(ibp, 1));
4332 		pkey_idx = 1;
4333 	}
4334 	*out_mad_pkey_index = (u16)pkey_idx;
4335 
4336 	switch (in_mad->mad_hdr.mgmt_class) {
4337 	case IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE:
4338 	case IB_MGMT_CLASS_SUBN_LID_ROUTED:
4339 		if (is_local_mad(ibp, in_mad, in_wc)) {
4340 			ret = opa_local_smp_check(ibp, in_wc);
4341 			if (ret)
4342 				return IB_MAD_RESULT_FAILURE;
4343 		}
4344 		ret = process_subn_opa(ibdev, mad_flags, port, in_mad,
4345 				       out_mad, &resp_len);
4346 		goto bail;
4347 	case IB_MGMT_CLASS_PERF_MGMT:
4348 		ret = process_perf_opa(ibdev, port, in_mad, out_mad,
4349 				       &resp_len);
4350 		goto bail;
4351 
4352 	default:
4353 		ret = IB_MAD_RESULT_SUCCESS;
4354 	}
4355 
4356 bail:
4357 	if (ret & IB_MAD_RESULT_REPLY)
4358 		*out_mad_size = round_up(resp_len, 8);
4359 	else if (ret & IB_MAD_RESULT_SUCCESS)
4360 		*out_mad_size = in_wc->byte_len - sizeof(struct ib_grh);
4361 
4362 	return ret;
4363 }
4364 
4365 static int hfi1_process_ib_mad(struct ib_device *ibdev, int mad_flags, u8 port,
4366 			       const struct ib_wc *in_wc,
4367 			       const struct ib_grh *in_grh,
4368 			       const struct ib_mad *in_mad,
4369 			       struct ib_mad *out_mad)
4370 {
4371 	int ret;
4372 
4373 	switch (in_mad->mad_hdr.mgmt_class) {
4374 	case IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE:
4375 	case IB_MGMT_CLASS_SUBN_LID_ROUTED:
4376 		ret = process_subn(ibdev, mad_flags, port, in_mad, out_mad);
4377 		break;
4378 	case IB_MGMT_CLASS_PERF_MGMT:
4379 		ret = process_perf(ibdev, port, in_mad, out_mad);
4380 		break;
4381 	default:
4382 		ret = IB_MAD_RESULT_SUCCESS;
4383 		break;
4384 	}
4385 
4386 	return ret;
4387 }
4388 
4389 /**
4390  * hfi1_process_mad - process an incoming MAD packet
4391  * @ibdev: the infiniband device this packet came in on
4392  * @mad_flags: MAD flags
4393  * @port: the port number this packet came in on
4394  * @in_wc: the work completion entry for this packet
4395  * @in_grh: the global route header for this packet
4396  * @in_mad: the incoming MAD
4397  * @out_mad: any outgoing MAD reply
4398  *
4399  * Returns IB_MAD_RESULT_SUCCESS if this is a MAD that we are not
4400  * interested in processing.
4401  *
4402  * Note that the verbs framework has already done the MAD sanity checks,
4403  * and hop count/pointer updating for IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE
4404  * MADs.
4405  *
4406  * This is called by the ib_mad module.
4407  */
4408 int hfi1_process_mad(struct ib_device *ibdev, int mad_flags, u8 port,
4409 		     const struct ib_wc *in_wc, const struct ib_grh *in_grh,
4410 		     const struct ib_mad_hdr *in_mad, size_t in_mad_size,
4411 		     struct ib_mad_hdr *out_mad, size_t *out_mad_size,
4412 		     u16 *out_mad_pkey_index)
4413 {
4414 	switch (in_mad->base_version) {
4415 	case OPA_MGMT_BASE_VERSION:
4416 		if (unlikely(in_mad_size != sizeof(struct opa_mad))) {
4417 			dev_err(ibdev->dma_device, "invalid in_mad_size\n");
4418 			return IB_MAD_RESULT_FAILURE;
4419 		}
4420 		return hfi1_process_opa_mad(ibdev, mad_flags, port,
4421 					    in_wc, in_grh,
4422 					    (struct opa_mad *)in_mad,
4423 					    (struct opa_mad *)out_mad,
4424 					    out_mad_size,
4425 					    out_mad_pkey_index);
4426 	case IB_MGMT_BASE_VERSION:
4427 		return hfi1_process_ib_mad(ibdev, mad_flags, port,
4428 					  in_wc, in_grh,
4429 					  (const struct ib_mad *)in_mad,
4430 					  (struct ib_mad *)out_mad);
4431 	default:
4432 		break;
4433 	}
4434 
4435 	return IB_MAD_RESULT_FAILURE;
4436 }
4437