18c2ecf20Sopenharmony_ci/*
28c2ecf20Sopenharmony_ci * Copyright (c) 2012 Intel Corporation.  All rights reserved.
38c2ecf20Sopenharmony_ci * Copyright (c) 2006 - 2012 QLogic Corporation. All rights reserved.
48c2ecf20Sopenharmony_ci * Copyright (c) 2005, 2006 PathScale, Inc. All rights reserved.
58c2ecf20Sopenharmony_ci *
68c2ecf20Sopenharmony_ci * This software is available to you under a choice of one of two
78c2ecf20Sopenharmony_ci * licenses.  You may choose to be licensed under the terms of the GNU
88c2ecf20Sopenharmony_ci * General Public License (GPL) Version 2, available from the file
98c2ecf20Sopenharmony_ci * COPYING in the main directory of this source tree, or the
108c2ecf20Sopenharmony_ci * OpenIB.org BSD license below:
118c2ecf20Sopenharmony_ci *
128c2ecf20Sopenharmony_ci *     Redistribution and use in source and binary forms, with or
138c2ecf20Sopenharmony_ci *     without modification, are permitted provided that the following
148c2ecf20Sopenharmony_ci *     conditions are met:
158c2ecf20Sopenharmony_ci *
168c2ecf20Sopenharmony_ci *      - Redistributions of source code must retain the above
178c2ecf20Sopenharmony_ci *        copyright notice, this list of conditions and the following
188c2ecf20Sopenharmony_ci *        disclaimer.
198c2ecf20Sopenharmony_ci *
208c2ecf20Sopenharmony_ci *      - Redistributions in binary form must reproduce the above
218c2ecf20Sopenharmony_ci *        copyright notice, this list of conditions and the following
228c2ecf20Sopenharmony_ci *        disclaimer in the documentation and/or other materials
238c2ecf20Sopenharmony_ci *        provided with the distribution.
248c2ecf20Sopenharmony_ci *
258c2ecf20Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
268c2ecf20Sopenharmony_ci * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
278c2ecf20Sopenharmony_ci * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
288c2ecf20Sopenharmony_ci * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
298c2ecf20Sopenharmony_ci * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
308c2ecf20Sopenharmony_ci * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
318c2ecf20Sopenharmony_ci * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
328c2ecf20Sopenharmony_ci * SOFTWARE.
338c2ecf20Sopenharmony_ci */
348c2ecf20Sopenharmony_ci
358c2ecf20Sopenharmony_ci#include <rdma/ib_smi.h>
368c2ecf20Sopenharmony_ci
378c2ecf20Sopenharmony_ci#include "qib.h"
388c2ecf20Sopenharmony_ci#include "qib_mad.h"
398c2ecf20Sopenharmony_ci
408c2ecf20Sopenharmony_cistatic int reply(struct ib_smp *smp)
418c2ecf20Sopenharmony_ci{
428c2ecf20Sopenharmony_ci	/*
438c2ecf20Sopenharmony_ci	 * The verbs framework will handle the directed/LID route
448c2ecf20Sopenharmony_ci	 * packet changes.
458c2ecf20Sopenharmony_ci	 */
468c2ecf20Sopenharmony_ci	smp->method = IB_MGMT_METHOD_GET_RESP;
478c2ecf20Sopenharmony_ci	if (smp->mgmt_class == IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE)
488c2ecf20Sopenharmony_ci		smp->status |= IB_SMP_DIRECTION;
498c2ecf20Sopenharmony_ci	return IB_MAD_RESULT_SUCCESS | IB_MAD_RESULT_REPLY;
508c2ecf20Sopenharmony_ci}
518c2ecf20Sopenharmony_ci
528c2ecf20Sopenharmony_cistatic int reply_failure(struct ib_smp *smp)
538c2ecf20Sopenharmony_ci{
548c2ecf20Sopenharmony_ci	/*
558c2ecf20Sopenharmony_ci	 * The verbs framework will handle the directed/LID route
568c2ecf20Sopenharmony_ci	 * packet changes.
578c2ecf20Sopenharmony_ci	 */
588c2ecf20Sopenharmony_ci	smp->method = IB_MGMT_METHOD_GET_RESP;
598c2ecf20Sopenharmony_ci	if (smp->mgmt_class == IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE)
608c2ecf20Sopenharmony_ci		smp->status |= IB_SMP_DIRECTION;
618c2ecf20Sopenharmony_ci	return IB_MAD_RESULT_FAILURE | IB_MAD_RESULT_REPLY;
628c2ecf20Sopenharmony_ci}
638c2ecf20Sopenharmony_ci
648c2ecf20Sopenharmony_cistatic void qib_send_trap(struct qib_ibport *ibp, void *data, unsigned len)
658c2ecf20Sopenharmony_ci{
668c2ecf20Sopenharmony_ci	struct ib_mad_send_buf *send_buf;
678c2ecf20Sopenharmony_ci	struct ib_mad_agent *agent;
688c2ecf20Sopenharmony_ci	struct ib_smp *smp;
698c2ecf20Sopenharmony_ci	int ret;
708c2ecf20Sopenharmony_ci	unsigned long flags;
718c2ecf20Sopenharmony_ci	unsigned long timeout;
728c2ecf20Sopenharmony_ci
738c2ecf20Sopenharmony_ci	agent = ibp->rvp.send_agent;
748c2ecf20Sopenharmony_ci	if (!agent)
758c2ecf20Sopenharmony_ci		return;
768c2ecf20Sopenharmony_ci
778c2ecf20Sopenharmony_ci	/* o14-3.2.1 */
788c2ecf20Sopenharmony_ci	if (!(ppd_from_ibp(ibp)->lflags & QIBL_LINKACTIVE))
798c2ecf20Sopenharmony_ci		return;
808c2ecf20Sopenharmony_ci
818c2ecf20Sopenharmony_ci	/* o14-2 */
828c2ecf20Sopenharmony_ci	if (ibp->rvp.trap_timeout &&
838c2ecf20Sopenharmony_ci	    time_before(jiffies, ibp->rvp.trap_timeout))
848c2ecf20Sopenharmony_ci		return;
858c2ecf20Sopenharmony_ci
868c2ecf20Sopenharmony_ci	send_buf = ib_create_send_mad(agent, 0, 0, 0, IB_MGMT_MAD_HDR,
878c2ecf20Sopenharmony_ci				      IB_MGMT_MAD_DATA, GFP_ATOMIC,
888c2ecf20Sopenharmony_ci				      IB_MGMT_BASE_VERSION);
898c2ecf20Sopenharmony_ci	if (IS_ERR(send_buf))
908c2ecf20Sopenharmony_ci		return;
918c2ecf20Sopenharmony_ci
928c2ecf20Sopenharmony_ci	smp = send_buf->mad;
938c2ecf20Sopenharmony_ci	smp->base_version = IB_MGMT_BASE_VERSION;
948c2ecf20Sopenharmony_ci	smp->mgmt_class = IB_MGMT_CLASS_SUBN_LID_ROUTED;
958c2ecf20Sopenharmony_ci	smp->class_version = 1;
968c2ecf20Sopenharmony_ci	smp->method = IB_MGMT_METHOD_TRAP;
978c2ecf20Sopenharmony_ci	ibp->rvp.tid++;
988c2ecf20Sopenharmony_ci	smp->tid = cpu_to_be64(ibp->rvp.tid);
998c2ecf20Sopenharmony_ci	smp->attr_id = IB_SMP_ATTR_NOTICE;
1008c2ecf20Sopenharmony_ci	/* o14-1: smp->mkey = 0; */
1018c2ecf20Sopenharmony_ci	memcpy(smp->data, data, len);
1028c2ecf20Sopenharmony_ci
1038c2ecf20Sopenharmony_ci	spin_lock_irqsave(&ibp->rvp.lock, flags);
1048c2ecf20Sopenharmony_ci	if (!ibp->rvp.sm_ah) {
1058c2ecf20Sopenharmony_ci		if (ibp->rvp.sm_lid != be16_to_cpu(IB_LID_PERMISSIVE)) {
1068c2ecf20Sopenharmony_ci			struct ib_ah *ah;
1078c2ecf20Sopenharmony_ci
1088c2ecf20Sopenharmony_ci			ah = qib_create_qp0_ah(ibp, (u16)ibp->rvp.sm_lid);
1098c2ecf20Sopenharmony_ci			if (IS_ERR(ah))
1108c2ecf20Sopenharmony_ci				ret = PTR_ERR(ah);
1118c2ecf20Sopenharmony_ci			else {
1128c2ecf20Sopenharmony_ci				send_buf->ah = ah;
1138c2ecf20Sopenharmony_ci				ibp->rvp.sm_ah = ibah_to_rvtah(ah);
1148c2ecf20Sopenharmony_ci				ret = 0;
1158c2ecf20Sopenharmony_ci			}
1168c2ecf20Sopenharmony_ci		} else
1178c2ecf20Sopenharmony_ci			ret = -EINVAL;
1188c2ecf20Sopenharmony_ci	} else {
1198c2ecf20Sopenharmony_ci		send_buf->ah = &ibp->rvp.sm_ah->ibah;
1208c2ecf20Sopenharmony_ci		ret = 0;
1218c2ecf20Sopenharmony_ci	}
1228c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&ibp->rvp.lock, flags);
1238c2ecf20Sopenharmony_ci
1248c2ecf20Sopenharmony_ci	if (!ret)
1258c2ecf20Sopenharmony_ci		ret = ib_post_send_mad(send_buf, NULL);
1268c2ecf20Sopenharmony_ci	if (!ret) {
1278c2ecf20Sopenharmony_ci		/* 4.096 usec. */
1288c2ecf20Sopenharmony_ci		timeout = (4096 * (1UL << ibp->rvp.subnet_timeout)) / 1000;
1298c2ecf20Sopenharmony_ci		ibp->rvp.trap_timeout = jiffies + usecs_to_jiffies(timeout);
1308c2ecf20Sopenharmony_ci	} else {
1318c2ecf20Sopenharmony_ci		ib_free_send_mad(send_buf);
1328c2ecf20Sopenharmony_ci		ibp->rvp.trap_timeout = 0;
1338c2ecf20Sopenharmony_ci	}
1348c2ecf20Sopenharmony_ci}
1358c2ecf20Sopenharmony_ci
1368c2ecf20Sopenharmony_ci/*
1378c2ecf20Sopenharmony_ci * Send a bad P_Key trap (ch. 14.3.8).
1388c2ecf20Sopenharmony_ci */
1398c2ecf20Sopenharmony_civoid qib_bad_pkey(struct qib_ibport *ibp, u32 key, u32 sl,
1408c2ecf20Sopenharmony_ci		  u32 qp1, u32 qp2, __be16 lid1, __be16 lid2)
1418c2ecf20Sopenharmony_ci{
1428c2ecf20Sopenharmony_ci	struct ib_mad_notice_attr data;
1438c2ecf20Sopenharmony_ci
1448c2ecf20Sopenharmony_ci	ibp->rvp.n_pkt_drops++;
1458c2ecf20Sopenharmony_ci	ibp->rvp.pkey_violations++;
1468c2ecf20Sopenharmony_ci
1478c2ecf20Sopenharmony_ci	/* Send violation trap */
1488c2ecf20Sopenharmony_ci	data.generic_type = IB_NOTICE_TYPE_SECURITY;
1498c2ecf20Sopenharmony_ci	data.prod_type_msb = 0;
1508c2ecf20Sopenharmony_ci	data.prod_type_lsb = IB_NOTICE_PROD_CA;
1518c2ecf20Sopenharmony_ci	data.trap_num = IB_NOTICE_TRAP_BAD_PKEY;
1528c2ecf20Sopenharmony_ci	data.issuer_lid = cpu_to_be16(ppd_from_ibp(ibp)->lid);
1538c2ecf20Sopenharmony_ci	data.toggle_count = 0;
1548c2ecf20Sopenharmony_ci	memset(&data.details, 0, sizeof(data.details));
1558c2ecf20Sopenharmony_ci	data.details.ntc_257_258.lid1 = lid1;
1568c2ecf20Sopenharmony_ci	data.details.ntc_257_258.lid2 = lid2;
1578c2ecf20Sopenharmony_ci	data.details.ntc_257_258.key = cpu_to_be32(key);
1588c2ecf20Sopenharmony_ci	data.details.ntc_257_258.sl_qp1 = cpu_to_be32((sl << 28) | qp1);
1598c2ecf20Sopenharmony_ci	data.details.ntc_257_258.qp2 = cpu_to_be32(qp2);
1608c2ecf20Sopenharmony_ci
1618c2ecf20Sopenharmony_ci	qib_send_trap(ibp, &data, sizeof(data));
1628c2ecf20Sopenharmony_ci}
1638c2ecf20Sopenharmony_ci
1648c2ecf20Sopenharmony_ci/*
1658c2ecf20Sopenharmony_ci * Send a bad M_Key trap (ch. 14.3.9).
1668c2ecf20Sopenharmony_ci */
1678c2ecf20Sopenharmony_cistatic void qib_bad_mkey(struct qib_ibport *ibp, struct ib_smp *smp)
1688c2ecf20Sopenharmony_ci{
1698c2ecf20Sopenharmony_ci	struct ib_mad_notice_attr data;
1708c2ecf20Sopenharmony_ci
1718c2ecf20Sopenharmony_ci	/* Send violation trap */
1728c2ecf20Sopenharmony_ci	data.generic_type = IB_NOTICE_TYPE_SECURITY;
1738c2ecf20Sopenharmony_ci	data.prod_type_msb = 0;
1748c2ecf20Sopenharmony_ci	data.prod_type_lsb = IB_NOTICE_PROD_CA;
1758c2ecf20Sopenharmony_ci	data.trap_num = IB_NOTICE_TRAP_BAD_MKEY;
1768c2ecf20Sopenharmony_ci	data.issuer_lid = cpu_to_be16(ppd_from_ibp(ibp)->lid);
1778c2ecf20Sopenharmony_ci	data.toggle_count = 0;
1788c2ecf20Sopenharmony_ci	memset(&data.details, 0, sizeof(data.details));
1798c2ecf20Sopenharmony_ci	data.details.ntc_256.lid = data.issuer_lid;
1808c2ecf20Sopenharmony_ci	data.details.ntc_256.method = smp->method;
1818c2ecf20Sopenharmony_ci	data.details.ntc_256.attr_id = smp->attr_id;
1828c2ecf20Sopenharmony_ci	data.details.ntc_256.attr_mod = smp->attr_mod;
1838c2ecf20Sopenharmony_ci	data.details.ntc_256.mkey = smp->mkey;
1848c2ecf20Sopenharmony_ci	if (smp->mgmt_class == IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE) {
1858c2ecf20Sopenharmony_ci		u8 hop_cnt;
1868c2ecf20Sopenharmony_ci
1878c2ecf20Sopenharmony_ci		data.details.ntc_256.dr_slid = smp->dr_slid;
1888c2ecf20Sopenharmony_ci		data.details.ntc_256.dr_trunc_hop = IB_NOTICE_TRAP_DR_NOTICE;
1898c2ecf20Sopenharmony_ci		hop_cnt = smp->hop_cnt;
1908c2ecf20Sopenharmony_ci		if (hop_cnt > ARRAY_SIZE(data.details.ntc_256.dr_rtn_path)) {
1918c2ecf20Sopenharmony_ci			data.details.ntc_256.dr_trunc_hop |=
1928c2ecf20Sopenharmony_ci				IB_NOTICE_TRAP_DR_TRUNC;
1938c2ecf20Sopenharmony_ci			hop_cnt = ARRAY_SIZE(data.details.ntc_256.dr_rtn_path);
1948c2ecf20Sopenharmony_ci		}
1958c2ecf20Sopenharmony_ci		data.details.ntc_256.dr_trunc_hop |= hop_cnt;
1968c2ecf20Sopenharmony_ci		memcpy(data.details.ntc_256.dr_rtn_path, smp->return_path,
1978c2ecf20Sopenharmony_ci		       hop_cnt);
1988c2ecf20Sopenharmony_ci	}
1998c2ecf20Sopenharmony_ci
2008c2ecf20Sopenharmony_ci	qib_send_trap(ibp, &data, sizeof(data));
2018c2ecf20Sopenharmony_ci}
2028c2ecf20Sopenharmony_ci
2038c2ecf20Sopenharmony_ci/*
2048c2ecf20Sopenharmony_ci * Send a Port Capability Mask Changed trap (ch. 14.3.11).
2058c2ecf20Sopenharmony_ci */
2068c2ecf20Sopenharmony_civoid qib_cap_mask_chg(struct rvt_dev_info *rdi, u8 port_num)
2078c2ecf20Sopenharmony_ci{
2088c2ecf20Sopenharmony_ci	struct qib_ibdev *ibdev = container_of(rdi, struct qib_ibdev, rdi);
2098c2ecf20Sopenharmony_ci	struct qib_devdata *dd = dd_from_dev(ibdev);
2108c2ecf20Sopenharmony_ci	struct qib_ibport *ibp = &dd->pport[port_num - 1].ibport_data;
2118c2ecf20Sopenharmony_ci	struct ib_mad_notice_attr data;
2128c2ecf20Sopenharmony_ci
2138c2ecf20Sopenharmony_ci	data.generic_type = IB_NOTICE_TYPE_INFO;
2148c2ecf20Sopenharmony_ci	data.prod_type_msb = 0;
2158c2ecf20Sopenharmony_ci	data.prod_type_lsb = IB_NOTICE_PROD_CA;
2168c2ecf20Sopenharmony_ci	data.trap_num = IB_NOTICE_TRAP_CAP_MASK_CHG;
2178c2ecf20Sopenharmony_ci	data.issuer_lid = cpu_to_be16(ppd_from_ibp(ibp)->lid);
2188c2ecf20Sopenharmony_ci	data.toggle_count = 0;
2198c2ecf20Sopenharmony_ci	memset(&data.details, 0, sizeof(data.details));
2208c2ecf20Sopenharmony_ci	data.details.ntc_144.lid = data.issuer_lid;
2218c2ecf20Sopenharmony_ci	data.details.ntc_144.new_cap_mask =
2228c2ecf20Sopenharmony_ci					cpu_to_be32(ibp->rvp.port_cap_flags);
2238c2ecf20Sopenharmony_ci	qib_send_trap(ibp, &data, sizeof(data));
2248c2ecf20Sopenharmony_ci}
2258c2ecf20Sopenharmony_ci
2268c2ecf20Sopenharmony_ci/*
2278c2ecf20Sopenharmony_ci * Send a System Image GUID Changed trap (ch. 14.3.12).
2288c2ecf20Sopenharmony_ci */
2298c2ecf20Sopenharmony_civoid qib_sys_guid_chg(struct qib_ibport *ibp)
2308c2ecf20Sopenharmony_ci{
2318c2ecf20Sopenharmony_ci	struct ib_mad_notice_attr data;
2328c2ecf20Sopenharmony_ci
2338c2ecf20Sopenharmony_ci	data.generic_type = IB_NOTICE_TYPE_INFO;
2348c2ecf20Sopenharmony_ci	data.prod_type_msb = 0;
2358c2ecf20Sopenharmony_ci	data.prod_type_lsb = IB_NOTICE_PROD_CA;
2368c2ecf20Sopenharmony_ci	data.trap_num = IB_NOTICE_TRAP_SYS_GUID_CHG;
2378c2ecf20Sopenharmony_ci	data.issuer_lid = cpu_to_be16(ppd_from_ibp(ibp)->lid);
2388c2ecf20Sopenharmony_ci	data.toggle_count = 0;
2398c2ecf20Sopenharmony_ci	memset(&data.details, 0, sizeof(data.details));
2408c2ecf20Sopenharmony_ci	data.details.ntc_145.lid = data.issuer_lid;
2418c2ecf20Sopenharmony_ci	data.details.ntc_145.new_sys_guid = ib_qib_sys_image_guid;
2428c2ecf20Sopenharmony_ci
2438c2ecf20Sopenharmony_ci	qib_send_trap(ibp, &data, sizeof(data));
2448c2ecf20Sopenharmony_ci}
2458c2ecf20Sopenharmony_ci
2468c2ecf20Sopenharmony_ci/*
2478c2ecf20Sopenharmony_ci * Send a Node Description Changed trap (ch. 14.3.13).
2488c2ecf20Sopenharmony_ci */
2498c2ecf20Sopenharmony_civoid qib_node_desc_chg(struct qib_ibport *ibp)
2508c2ecf20Sopenharmony_ci{
2518c2ecf20Sopenharmony_ci	struct ib_mad_notice_attr data;
2528c2ecf20Sopenharmony_ci
2538c2ecf20Sopenharmony_ci	data.generic_type = IB_NOTICE_TYPE_INFO;
2548c2ecf20Sopenharmony_ci	data.prod_type_msb = 0;
2558c2ecf20Sopenharmony_ci	data.prod_type_lsb = IB_NOTICE_PROD_CA;
2568c2ecf20Sopenharmony_ci	data.trap_num = IB_NOTICE_TRAP_CAP_MASK_CHG;
2578c2ecf20Sopenharmony_ci	data.issuer_lid = cpu_to_be16(ppd_from_ibp(ibp)->lid);
2588c2ecf20Sopenharmony_ci	data.toggle_count = 0;
2598c2ecf20Sopenharmony_ci	memset(&data.details, 0, sizeof(data.details));
2608c2ecf20Sopenharmony_ci	data.details.ntc_144.lid = data.issuer_lid;
2618c2ecf20Sopenharmony_ci	data.details.ntc_144.local_changes = 1;
2628c2ecf20Sopenharmony_ci	data.details.ntc_144.change_flags = IB_NOTICE_TRAP_NODE_DESC_CHG;
2638c2ecf20Sopenharmony_ci
2648c2ecf20Sopenharmony_ci	qib_send_trap(ibp, &data, sizeof(data));
2658c2ecf20Sopenharmony_ci}
2668c2ecf20Sopenharmony_ci
2678c2ecf20Sopenharmony_cistatic int subn_get_nodedescription(struct ib_smp *smp,
2688c2ecf20Sopenharmony_ci				    struct ib_device *ibdev)
2698c2ecf20Sopenharmony_ci{
2708c2ecf20Sopenharmony_ci	if (smp->attr_mod)
2718c2ecf20Sopenharmony_ci		smp->status |= IB_SMP_INVALID_FIELD;
2728c2ecf20Sopenharmony_ci
2738c2ecf20Sopenharmony_ci	memcpy(smp->data, ibdev->node_desc, sizeof(smp->data));
2748c2ecf20Sopenharmony_ci
2758c2ecf20Sopenharmony_ci	return reply(smp);
2768c2ecf20Sopenharmony_ci}
2778c2ecf20Sopenharmony_ci
2788c2ecf20Sopenharmony_cistatic int subn_get_nodeinfo(struct ib_smp *smp, struct ib_device *ibdev,
2798c2ecf20Sopenharmony_ci			     u8 port)
2808c2ecf20Sopenharmony_ci{
2818c2ecf20Sopenharmony_ci	struct ib_node_info *nip = (struct ib_node_info *)&smp->data;
2828c2ecf20Sopenharmony_ci	struct qib_devdata *dd = dd_from_ibdev(ibdev);
2838c2ecf20Sopenharmony_ci	u32 majrev, minrev;
2848c2ecf20Sopenharmony_ci	unsigned pidx = port - 1; /* IB number port from 1, hdw from 0 */
2858c2ecf20Sopenharmony_ci
2868c2ecf20Sopenharmony_ci	/* GUID 0 is illegal */
2878c2ecf20Sopenharmony_ci	if (smp->attr_mod || pidx >= dd->num_pports ||
2888c2ecf20Sopenharmony_ci	    dd->pport[pidx].guid == 0)
2898c2ecf20Sopenharmony_ci		smp->status |= IB_SMP_INVALID_FIELD;
2908c2ecf20Sopenharmony_ci	else
2918c2ecf20Sopenharmony_ci		nip->port_guid = dd->pport[pidx].guid;
2928c2ecf20Sopenharmony_ci
2938c2ecf20Sopenharmony_ci	nip->base_version = 1;
2948c2ecf20Sopenharmony_ci	nip->class_version = 1;
2958c2ecf20Sopenharmony_ci	nip->node_type = 1;     /* channel adapter */
2968c2ecf20Sopenharmony_ci	nip->num_ports = ibdev->phys_port_cnt;
2978c2ecf20Sopenharmony_ci	/* This is already in network order */
2988c2ecf20Sopenharmony_ci	nip->sys_guid = ib_qib_sys_image_guid;
2998c2ecf20Sopenharmony_ci	nip->node_guid = dd->pport->guid; /* Use first-port GUID as node */
3008c2ecf20Sopenharmony_ci	nip->partition_cap = cpu_to_be16(qib_get_npkeys(dd));
3018c2ecf20Sopenharmony_ci	nip->device_id = cpu_to_be16(dd->deviceid);
3028c2ecf20Sopenharmony_ci	majrev = dd->majrev;
3038c2ecf20Sopenharmony_ci	minrev = dd->minrev;
3048c2ecf20Sopenharmony_ci	nip->revision = cpu_to_be32((majrev << 16) | minrev);
3058c2ecf20Sopenharmony_ci	nip->local_port_num = port;
3068c2ecf20Sopenharmony_ci	nip->vendor_id[0] = QIB_SRC_OUI_1;
3078c2ecf20Sopenharmony_ci	nip->vendor_id[1] = QIB_SRC_OUI_2;
3088c2ecf20Sopenharmony_ci	nip->vendor_id[2] = QIB_SRC_OUI_3;
3098c2ecf20Sopenharmony_ci
3108c2ecf20Sopenharmony_ci	return reply(smp);
3118c2ecf20Sopenharmony_ci}
3128c2ecf20Sopenharmony_ci
3138c2ecf20Sopenharmony_cistatic int subn_get_guidinfo(struct ib_smp *smp, struct ib_device *ibdev,
3148c2ecf20Sopenharmony_ci			     u8 port)
3158c2ecf20Sopenharmony_ci{
3168c2ecf20Sopenharmony_ci	struct qib_devdata *dd = dd_from_ibdev(ibdev);
3178c2ecf20Sopenharmony_ci	u32 startgx = 8 * be32_to_cpu(smp->attr_mod);
3188c2ecf20Sopenharmony_ci	__be64 *p = (__be64 *) smp->data;
3198c2ecf20Sopenharmony_ci	unsigned pidx = port - 1; /* IB number port from 1, hdw from 0 */
3208c2ecf20Sopenharmony_ci
3218c2ecf20Sopenharmony_ci	/* 32 blocks of 8 64-bit GUIDs per block */
3228c2ecf20Sopenharmony_ci
3238c2ecf20Sopenharmony_ci	memset(smp->data, 0, sizeof(smp->data));
3248c2ecf20Sopenharmony_ci
3258c2ecf20Sopenharmony_ci	if (startgx == 0 && pidx < dd->num_pports) {
3268c2ecf20Sopenharmony_ci		struct qib_pportdata *ppd = dd->pport + pidx;
3278c2ecf20Sopenharmony_ci		struct qib_ibport *ibp = &ppd->ibport_data;
3288c2ecf20Sopenharmony_ci		__be64 g = ppd->guid;
3298c2ecf20Sopenharmony_ci		unsigned i;
3308c2ecf20Sopenharmony_ci
3318c2ecf20Sopenharmony_ci		/* GUID 0 is illegal */
3328c2ecf20Sopenharmony_ci		if (g == 0)
3338c2ecf20Sopenharmony_ci			smp->status |= IB_SMP_INVALID_FIELD;
3348c2ecf20Sopenharmony_ci		else {
3358c2ecf20Sopenharmony_ci			/* The first is a copy of the read-only HW GUID. */
3368c2ecf20Sopenharmony_ci			p[0] = g;
3378c2ecf20Sopenharmony_ci			for (i = 1; i < QIB_GUIDS_PER_PORT; i++)
3388c2ecf20Sopenharmony_ci				p[i] = ibp->guids[i - 1];
3398c2ecf20Sopenharmony_ci		}
3408c2ecf20Sopenharmony_ci	} else
3418c2ecf20Sopenharmony_ci		smp->status |= IB_SMP_INVALID_FIELD;
3428c2ecf20Sopenharmony_ci
3438c2ecf20Sopenharmony_ci	return reply(smp);
3448c2ecf20Sopenharmony_ci}
3458c2ecf20Sopenharmony_ci
3468c2ecf20Sopenharmony_cistatic void set_link_width_enabled(struct qib_pportdata *ppd, u32 w)
3478c2ecf20Sopenharmony_ci{
3488c2ecf20Sopenharmony_ci	(void) ppd->dd->f_set_ib_cfg(ppd, QIB_IB_CFG_LWID_ENB, w);
3498c2ecf20Sopenharmony_ci}
3508c2ecf20Sopenharmony_ci
3518c2ecf20Sopenharmony_cistatic void set_link_speed_enabled(struct qib_pportdata *ppd, u32 s)
3528c2ecf20Sopenharmony_ci{
3538c2ecf20Sopenharmony_ci	(void) ppd->dd->f_set_ib_cfg(ppd, QIB_IB_CFG_SPD_ENB, s);
3548c2ecf20Sopenharmony_ci}
3558c2ecf20Sopenharmony_ci
3568c2ecf20Sopenharmony_cistatic int get_overrunthreshold(struct qib_pportdata *ppd)
3578c2ecf20Sopenharmony_ci{
3588c2ecf20Sopenharmony_ci	return ppd->dd->f_get_ib_cfg(ppd, QIB_IB_CFG_OVERRUN_THRESH);
3598c2ecf20Sopenharmony_ci}
3608c2ecf20Sopenharmony_ci
3618c2ecf20Sopenharmony_ci/**
3628c2ecf20Sopenharmony_ci * set_overrunthreshold - set the overrun threshold
3638c2ecf20Sopenharmony_ci * @ppd: the physical port data
3648c2ecf20Sopenharmony_ci * @n: the new threshold
3658c2ecf20Sopenharmony_ci *
3668c2ecf20Sopenharmony_ci * Note that this will only take effect when the link state changes.
3678c2ecf20Sopenharmony_ci */
3688c2ecf20Sopenharmony_cistatic int set_overrunthreshold(struct qib_pportdata *ppd, unsigned n)
3698c2ecf20Sopenharmony_ci{
3708c2ecf20Sopenharmony_ci	(void) ppd->dd->f_set_ib_cfg(ppd, QIB_IB_CFG_OVERRUN_THRESH,
3718c2ecf20Sopenharmony_ci					 (u32)n);
3728c2ecf20Sopenharmony_ci	return 0;
3738c2ecf20Sopenharmony_ci}
3748c2ecf20Sopenharmony_ci
3758c2ecf20Sopenharmony_cistatic int get_phyerrthreshold(struct qib_pportdata *ppd)
3768c2ecf20Sopenharmony_ci{
3778c2ecf20Sopenharmony_ci	return ppd->dd->f_get_ib_cfg(ppd, QIB_IB_CFG_PHYERR_THRESH);
3788c2ecf20Sopenharmony_ci}
3798c2ecf20Sopenharmony_ci
3808c2ecf20Sopenharmony_ci/**
3818c2ecf20Sopenharmony_ci * set_phyerrthreshold - set the physical error threshold
3828c2ecf20Sopenharmony_ci * @ppd: the physical port data
3838c2ecf20Sopenharmony_ci * @n: the new threshold
3848c2ecf20Sopenharmony_ci *
3858c2ecf20Sopenharmony_ci * Note that this will only take effect when the link state changes.
3868c2ecf20Sopenharmony_ci */
3878c2ecf20Sopenharmony_cistatic int set_phyerrthreshold(struct qib_pportdata *ppd, unsigned n)
3888c2ecf20Sopenharmony_ci{
3898c2ecf20Sopenharmony_ci	(void) ppd->dd->f_set_ib_cfg(ppd, QIB_IB_CFG_PHYERR_THRESH,
3908c2ecf20Sopenharmony_ci					 (u32)n);
3918c2ecf20Sopenharmony_ci	return 0;
3928c2ecf20Sopenharmony_ci}
3938c2ecf20Sopenharmony_ci
3948c2ecf20Sopenharmony_ci/**
3958c2ecf20Sopenharmony_ci * get_linkdowndefaultstate - get the default linkdown state
3968c2ecf20Sopenharmony_ci * @ppd: the physical port data
3978c2ecf20Sopenharmony_ci *
3988c2ecf20Sopenharmony_ci * Returns zero if the default is POLL, 1 if the default is SLEEP.
3998c2ecf20Sopenharmony_ci */
4008c2ecf20Sopenharmony_cistatic int get_linkdowndefaultstate(struct qib_pportdata *ppd)
4018c2ecf20Sopenharmony_ci{
4028c2ecf20Sopenharmony_ci	return ppd->dd->f_get_ib_cfg(ppd, QIB_IB_CFG_LINKDEFAULT) ==
4038c2ecf20Sopenharmony_ci		IB_LINKINITCMD_SLEEP;
4048c2ecf20Sopenharmony_ci}
4058c2ecf20Sopenharmony_ci
4068c2ecf20Sopenharmony_cistatic int check_mkey(struct qib_ibport *ibp, struct ib_smp *smp, int mad_flags)
4078c2ecf20Sopenharmony_ci{
4088c2ecf20Sopenharmony_ci	int valid_mkey = 0;
4098c2ecf20Sopenharmony_ci	int ret = 0;
4108c2ecf20Sopenharmony_ci
4118c2ecf20Sopenharmony_ci	/* Is the mkey in the process of expiring? */
4128c2ecf20Sopenharmony_ci	if (ibp->rvp.mkey_lease_timeout &&
4138c2ecf20Sopenharmony_ci	    time_after_eq(jiffies, ibp->rvp.mkey_lease_timeout)) {
4148c2ecf20Sopenharmony_ci		/* Clear timeout and mkey protection field. */
4158c2ecf20Sopenharmony_ci		ibp->rvp.mkey_lease_timeout = 0;
4168c2ecf20Sopenharmony_ci		ibp->rvp.mkeyprot = 0;
4178c2ecf20Sopenharmony_ci	}
4188c2ecf20Sopenharmony_ci
4198c2ecf20Sopenharmony_ci	if ((mad_flags & IB_MAD_IGNORE_MKEY) ||  ibp->rvp.mkey == 0 ||
4208c2ecf20Sopenharmony_ci	    ibp->rvp.mkey == smp->mkey)
4218c2ecf20Sopenharmony_ci		valid_mkey = 1;
4228c2ecf20Sopenharmony_ci
4238c2ecf20Sopenharmony_ci	/* Unset lease timeout on any valid Get/Set/TrapRepress */
4248c2ecf20Sopenharmony_ci	if (valid_mkey && ibp->rvp.mkey_lease_timeout &&
4258c2ecf20Sopenharmony_ci	    (smp->method == IB_MGMT_METHOD_GET ||
4268c2ecf20Sopenharmony_ci	     smp->method == IB_MGMT_METHOD_SET ||
4278c2ecf20Sopenharmony_ci	     smp->method == IB_MGMT_METHOD_TRAP_REPRESS))
4288c2ecf20Sopenharmony_ci		ibp->rvp.mkey_lease_timeout = 0;
4298c2ecf20Sopenharmony_ci
4308c2ecf20Sopenharmony_ci	if (!valid_mkey) {
4318c2ecf20Sopenharmony_ci		switch (smp->method) {
4328c2ecf20Sopenharmony_ci		case IB_MGMT_METHOD_GET:
4338c2ecf20Sopenharmony_ci			/* Bad mkey not a violation below level 2 */
4348c2ecf20Sopenharmony_ci			if (ibp->rvp.mkeyprot < 2)
4358c2ecf20Sopenharmony_ci				break;
4368c2ecf20Sopenharmony_ci			fallthrough;
4378c2ecf20Sopenharmony_ci		case IB_MGMT_METHOD_SET:
4388c2ecf20Sopenharmony_ci		case IB_MGMT_METHOD_TRAP_REPRESS:
4398c2ecf20Sopenharmony_ci			if (ibp->rvp.mkey_violations != 0xFFFF)
4408c2ecf20Sopenharmony_ci				++ibp->rvp.mkey_violations;
4418c2ecf20Sopenharmony_ci			if (!ibp->rvp.mkey_lease_timeout &&
4428c2ecf20Sopenharmony_ci			    ibp->rvp.mkey_lease_period)
4438c2ecf20Sopenharmony_ci				ibp->rvp.mkey_lease_timeout = jiffies +
4448c2ecf20Sopenharmony_ci					ibp->rvp.mkey_lease_period * HZ;
4458c2ecf20Sopenharmony_ci			/* Generate a trap notice. */
4468c2ecf20Sopenharmony_ci			qib_bad_mkey(ibp, smp);
4478c2ecf20Sopenharmony_ci			ret = 1;
4488c2ecf20Sopenharmony_ci		}
4498c2ecf20Sopenharmony_ci	}
4508c2ecf20Sopenharmony_ci
4518c2ecf20Sopenharmony_ci	return ret;
4528c2ecf20Sopenharmony_ci}
4538c2ecf20Sopenharmony_ci
4548c2ecf20Sopenharmony_cistatic int subn_get_portinfo(struct ib_smp *smp, struct ib_device *ibdev,
4558c2ecf20Sopenharmony_ci			     u8 port)
4568c2ecf20Sopenharmony_ci{
4578c2ecf20Sopenharmony_ci	struct qib_devdata *dd;
4588c2ecf20Sopenharmony_ci	struct qib_pportdata *ppd;
4598c2ecf20Sopenharmony_ci	struct qib_ibport *ibp;
4608c2ecf20Sopenharmony_ci	struct ib_port_info *pip = (struct ib_port_info *)smp->data;
4618c2ecf20Sopenharmony_ci	u8 mtu;
4628c2ecf20Sopenharmony_ci	int ret;
4638c2ecf20Sopenharmony_ci	u32 state;
4648c2ecf20Sopenharmony_ci	u32 port_num = be32_to_cpu(smp->attr_mod);
4658c2ecf20Sopenharmony_ci
4668c2ecf20Sopenharmony_ci	if (port_num == 0)
4678c2ecf20Sopenharmony_ci		port_num = port;
4688c2ecf20Sopenharmony_ci	else {
4698c2ecf20Sopenharmony_ci		if (port_num > ibdev->phys_port_cnt) {
4708c2ecf20Sopenharmony_ci			smp->status |= IB_SMP_INVALID_FIELD;
4718c2ecf20Sopenharmony_ci			ret = reply(smp);
4728c2ecf20Sopenharmony_ci			goto bail;
4738c2ecf20Sopenharmony_ci		}
4748c2ecf20Sopenharmony_ci		if (port_num != port) {
4758c2ecf20Sopenharmony_ci			ibp = to_iport(ibdev, port_num);
4768c2ecf20Sopenharmony_ci			ret = check_mkey(ibp, smp, 0);
4778c2ecf20Sopenharmony_ci			if (ret) {
4788c2ecf20Sopenharmony_ci				ret = IB_MAD_RESULT_FAILURE;
4798c2ecf20Sopenharmony_ci				goto bail;
4808c2ecf20Sopenharmony_ci			}
4818c2ecf20Sopenharmony_ci		}
4828c2ecf20Sopenharmony_ci	}
4838c2ecf20Sopenharmony_ci
4848c2ecf20Sopenharmony_ci	dd = dd_from_ibdev(ibdev);
4858c2ecf20Sopenharmony_ci	/* IB numbers ports from 1, hdw from 0 */
4868c2ecf20Sopenharmony_ci	ppd = dd->pport + (port_num - 1);
4878c2ecf20Sopenharmony_ci	ibp = &ppd->ibport_data;
4888c2ecf20Sopenharmony_ci
4898c2ecf20Sopenharmony_ci	/* Clear all fields.  Only set the non-zero fields. */
4908c2ecf20Sopenharmony_ci	memset(smp->data, 0, sizeof(smp->data));
4918c2ecf20Sopenharmony_ci
4928c2ecf20Sopenharmony_ci	/* Only return the mkey if the protection field allows it. */
4938c2ecf20Sopenharmony_ci	if (!(smp->method == IB_MGMT_METHOD_GET &&
4948c2ecf20Sopenharmony_ci	      ibp->rvp.mkey != smp->mkey &&
4958c2ecf20Sopenharmony_ci	      ibp->rvp.mkeyprot == 1))
4968c2ecf20Sopenharmony_ci		pip->mkey = ibp->rvp.mkey;
4978c2ecf20Sopenharmony_ci	pip->gid_prefix = ibp->rvp.gid_prefix;
4988c2ecf20Sopenharmony_ci	pip->lid = cpu_to_be16(ppd->lid);
4998c2ecf20Sopenharmony_ci	pip->sm_lid = cpu_to_be16((u16)ibp->rvp.sm_lid);
5008c2ecf20Sopenharmony_ci	pip->cap_mask = cpu_to_be32(ibp->rvp.port_cap_flags);
5018c2ecf20Sopenharmony_ci	/* pip->diag_code; */
5028c2ecf20Sopenharmony_ci	pip->mkey_lease_period = cpu_to_be16(ibp->rvp.mkey_lease_period);
5038c2ecf20Sopenharmony_ci	pip->local_port_num = port;
5048c2ecf20Sopenharmony_ci	pip->link_width_enabled = ppd->link_width_enabled;
5058c2ecf20Sopenharmony_ci	pip->link_width_supported = ppd->link_width_supported;
5068c2ecf20Sopenharmony_ci	pip->link_width_active = ppd->link_width_active;
5078c2ecf20Sopenharmony_ci	state = dd->f_iblink_state(ppd->lastibcstat);
5088c2ecf20Sopenharmony_ci	pip->linkspeed_portstate = ppd->link_speed_supported << 4 | state;
5098c2ecf20Sopenharmony_ci
5108c2ecf20Sopenharmony_ci	pip->portphysstate_linkdown =
5118c2ecf20Sopenharmony_ci		(dd->f_ibphys_portstate(ppd->lastibcstat) << 4) |
5128c2ecf20Sopenharmony_ci		(get_linkdowndefaultstate(ppd) ? 1 : 2);
5138c2ecf20Sopenharmony_ci	pip->mkeyprot_resv_lmc = (ibp->rvp.mkeyprot << 6) | ppd->lmc;
5148c2ecf20Sopenharmony_ci	pip->linkspeedactive_enabled = (ppd->link_speed_active << 4) |
5158c2ecf20Sopenharmony_ci		ppd->link_speed_enabled;
5168c2ecf20Sopenharmony_ci	switch (ppd->ibmtu) {
5178c2ecf20Sopenharmony_ci	default: /* something is wrong; fall through */
5188c2ecf20Sopenharmony_ci	case 4096:
5198c2ecf20Sopenharmony_ci		mtu = IB_MTU_4096;
5208c2ecf20Sopenharmony_ci		break;
5218c2ecf20Sopenharmony_ci	case 2048:
5228c2ecf20Sopenharmony_ci		mtu = IB_MTU_2048;
5238c2ecf20Sopenharmony_ci		break;
5248c2ecf20Sopenharmony_ci	case 1024:
5258c2ecf20Sopenharmony_ci		mtu = IB_MTU_1024;
5268c2ecf20Sopenharmony_ci		break;
5278c2ecf20Sopenharmony_ci	case 512:
5288c2ecf20Sopenharmony_ci		mtu = IB_MTU_512;
5298c2ecf20Sopenharmony_ci		break;
5308c2ecf20Sopenharmony_ci	case 256:
5318c2ecf20Sopenharmony_ci		mtu = IB_MTU_256;
5328c2ecf20Sopenharmony_ci		break;
5338c2ecf20Sopenharmony_ci	}
5348c2ecf20Sopenharmony_ci	pip->neighbormtu_mastersmsl = (mtu << 4) | ibp->rvp.sm_sl;
5358c2ecf20Sopenharmony_ci	pip->vlcap_inittype = ppd->vls_supported << 4;  /* InitType = 0 */
5368c2ecf20Sopenharmony_ci	pip->vl_high_limit = ibp->rvp.vl_high_limit;
5378c2ecf20Sopenharmony_ci	pip->vl_arb_high_cap =
5388c2ecf20Sopenharmony_ci		dd->f_get_ib_cfg(ppd, QIB_IB_CFG_VL_HIGH_CAP);
5398c2ecf20Sopenharmony_ci	pip->vl_arb_low_cap =
5408c2ecf20Sopenharmony_ci		dd->f_get_ib_cfg(ppd, QIB_IB_CFG_VL_LOW_CAP);
5418c2ecf20Sopenharmony_ci	/* InitTypeReply = 0 */
5428c2ecf20Sopenharmony_ci	pip->inittypereply_mtucap = qib_ibmtu ? qib_ibmtu : IB_MTU_4096;
5438c2ecf20Sopenharmony_ci	/* HCAs ignore VLStallCount and HOQLife */
5448c2ecf20Sopenharmony_ci	/* pip->vlstallcnt_hoqlife; */
5458c2ecf20Sopenharmony_ci	pip->operationalvl_pei_peo_fpi_fpo =
5468c2ecf20Sopenharmony_ci		dd->f_get_ib_cfg(ppd, QIB_IB_CFG_OP_VLS) << 4;
5478c2ecf20Sopenharmony_ci	pip->mkey_violations = cpu_to_be16(ibp->rvp.mkey_violations);
5488c2ecf20Sopenharmony_ci	/* P_KeyViolations are counted by hardware. */
5498c2ecf20Sopenharmony_ci	pip->pkey_violations = cpu_to_be16(ibp->rvp.pkey_violations);
5508c2ecf20Sopenharmony_ci	pip->qkey_violations = cpu_to_be16(ibp->rvp.qkey_violations);
5518c2ecf20Sopenharmony_ci	/* Only the hardware GUID is supported for now */
5528c2ecf20Sopenharmony_ci	pip->guid_cap = QIB_GUIDS_PER_PORT;
5538c2ecf20Sopenharmony_ci	pip->clientrereg_resv_subnetto = ibp->rvp.subnet_timeout;
5548c2ecf20Sopenharmony_ci	/* 32.768 usec. response time (guessing) */
5558c2ecf20Sopenharmony_ci	pip->resv_resptimevalue = 3;
5568c2ecf20Sopenharmony_ci	pip->localphyerrors_overrunerrors =
5578c2ecf20Sopenharmony_ci		(get_phyerrthreshold(ppd) << 4) |
5588c2ecf20Sopenharmony_ci		get_overrunthreshold(ppd);
5598c2ecf20Sopenharmony_ci	/* pip->max_credit_hint; */
5608c2ecf20Sopenharmony_ci	if (ibp->rvp.port_cap_flags & IB_PORT_LINK_LATENCY_SUP) {
5618c2ecf20Sopenharmony_ci		u32 v;
5628c2ecf20Sopenharmony_ci
5638c2ecf20Sopenharmony_ci		v = dd->f_get_ib_cfg(ppd, QIB_IB_CFG_LINKLATENCY);
5648c2ecf20Sopenharmony_ci		pip->link_roundtrip_latency[0] = v >> 16;
5658c2ecf20Sopenharmony_ci		pip->link_roundtrip_latency[1] = v >> 8;
5668c2ecf20Sopenharmony_ci		pip->link_roundtrip_latency[2] = v;
5678c2ecf20Sopenharmony_ci	}
5688c2ecf20Sopenharmony_ci
5698c2ecf20Sopenharmony_ci	ret = reply(smp);
5708c2ecf20Sopenharmony_ci
5718c2ecf20Sopenharmony_cibail:
5728c2ecf20Sopenharmony_ci	return ret;
5738c2ecf20Sopenharmony_ci}
5748c2ecf20Sopenharmony_ci
5758c2ecf20Sopenharmony_ci/**
5768c2ecf20Sopenharmony_ci * get_pkeys - return the PKEY table
5778c2ecf20Sopenharmony_ci * @dd: the qlogic_ib device
5788c2ecf20Sopenharmony_ci * @port: the IB port number
5798c2ecf20Sopenharmony_ci * @pkeys: the pkey table is placed here
5808c2ecf20Sopenharmony_ci */
5818c2ecf20Sopenharmony_cistatic int get_pkeys(struct qib_devdata *dd, u8 port, u16 *pkeys)
5828c2ecf20Sopenharmony_ci{
5838c2ecf20Sopenharmony_ci	struct qib_pportdata *ppd = dd->pport + port - 1;
5848c2ecf20Sopenharmony_ci	/*
5858c2ecf20Sopenharmony_ci	 * always a kernel context, no locking needed.
5868c2ecf20Sopenharmony_ci	 * If we get here with ppd setup, no need to check
5878c2ecf20Sopenharmony_ci	 * that pd is valid.
5888c2ecf20Sopenharmony_ci	 */
5898c2ecf20Sopenharmony_ci	struct qib_ctxtdata *rcd = dd->rcd[ppd->hw_pidx];
5908c2ecf20Sopenharmony_ci
5918c2ecf20Sopenharmony_ci	memcpy(pkeys, rcd->pkeys, sizeof(rcd->pkeys));
5928c2ecf20Sopenharmony_ci
5938c2ecf20Sopenharmony_ci	return 0;
5948c2ecf20Sopenharmony_ci}
5958c2ecf20Sopenharmony_ci
5968c2ecf20Sopenharmony_cistatic int subn_get_pkeytable(struct ib_smp *smp, struct ib_device *ibdev,
5978c2ecf20Sopenharmony_ci			      u8 port)
5988c2ecf20Sopenharmony_ci{
5998c2ecf20Sopenharmony_ci	u32 startpx = 32 * (be32_to_cpu(smp->attr_mod) & 0xffff);
6008c2ecf20Sopenharmony_ci	u16 *p = (u16 *) smp->data;
6018c2ecf20Sopenharmony_ci	__be16 *q = (__be16 *) smp->data;
6028c2ecf20Sopenharmony_ci
6038c2ecf20Sopenharmony_ci	/* 64 blocks of 32 16-bit P_Key entries */
6048c2ecf20Sopenharmony_ci
6058c2ecf20Sopenharmony_ci	memset(smp->data, 0, sizeof(smp->data));
6068c2ecf20Sopenharmony_ci	if (startpx == 0) {
6078c2ecf20Sopenharmony_ci		struct qib_devdata *dd = dd_from_ibdev(ibdev);
6088c2ecf20Sopenharmony_ci		unsigned i, n = qib_get_npkeys(dd);
6098c2ecf20Sopenharmony_ci
6108c2ecf20Sopenharmony_ci		get_pkeys(dd, port, p);
6118c2ecf20Sopenharmony_ci
6128c2ecf20Sopenharmony_ci		for (i = 0; i < n; i++)
6138c2ecf20Sopenharmony_ci			q[i] = cpu_to_be16(p[i]);
6148c2ecf20Sopenharmony_ci	} else
6158c2ecf20Sopenharmony_ci		smp->status |= IB_SMP_INVALID_FIELD;
6168c2ecf20Sopenharmony_ci
6178c2ecf20Sopenharmony_ci	return reply(smp);
6188c2ecf20Sopenharmony_ci}
6198c2ecf20Sopenharmony_ci
6208c2ecf20Sopenharmony_cistatic int subn_set_guidinfo(struct ib_smp *smp, struct ib_device *ibdev,
6218c2ecf20Sopenharmony_ci			     u8 port)
6228c2ecf20Sopenharmony_ci{
6238c2ecf20Sopenharmony_ci	struct qib_devdata *dd = dd_from_ibdev(ibdev);
6248c2ecf20Sopenharmony_ci	u32 startgx = 8 * be32_to_cpu(smp->attr_mod);
6258c2ecf20Sopenharmony_ci	__be64 *p = (__be64 *) smp->data;
6268c2ecf20Sopenharmony_ci	unsigned pidx = port - 1; /* IB number port from 1, hdw from 0 */
6278c2ecf20Sopenharmony_ci
6288c2ecf20Sopenharmony_ci	/* 32 blocks of 8 64-bit GUIDs per block */
6298c2ecf20Sopenharmony_ci
6308c2ecf20Sopenharmony_ci	if (startgx == 0 && pidx < dd->num_pports) {
6318c2ecf20Sopenharmony_ci		struct qib_pportdata *ppd = dd->pport + pidx;
6328c2ecf20Sopenharmony_ci		struct qib_ibport *ibp = &ppd->ibport_data;
6338c2ecf20Sopenharmony_ci		unsigned i;
6348c2ecf20Sopenharmony_ci
6358c2ecf20Sopenharmony_ci		/* The first entry is read-only. */
6368c2ecf20Sopenharmony_ci		for (i = 1; i < QIB_GUIDS_PER_PORT; i++)
6378c2ecf20Sopenharmony_ci			ibp->guids[i - 1] = p[i];
6388c2ecf20Sopenharmony_ci	} else
6398c2ecf20Sopenharmony_ci		smp->status |= IB_SMP_INVALID_FIELD;
6408c2ecf20Sopenharmony_ci
6418c2ecf20Sopenharmony_ci	/* The only GUID we support is the first read-only entry. */
6428c2ecf20Sopenharmony_ci	return subn_get_guidinfo(smp, ibdev, port);
6438c2ecf20Sopenharmony_ci}
6448c2ecf20Sopenharmony_ci
6458c2ecf20Sopenharmony_ci/**
6468c2ecf20Sopenharmony_ci * subn_set_portinfo - set port information
6478c2ecf20Sopenharmony_ci * @smp: the incoming SM packet
6488c2ecf20Sopenharmony_ci * @ibdev: the infiniband device
6498c2ecf20Sopenharmony_ci * @port: the port on the device
6508c2ecf20Sopenharmony_ci *
6518c2ecf20Sopenharmony_ci * Set Portinfo (see ch. 14.2.5.6).
6528c2ecf20Sopenharmony_ci */
6538c2ecf20Sopenharmony_cistatic int subn_set_portinfo(struct ib_smp *smp, struct ib_device *ibdev,
6548c2ecf20Sopenharmony_ci			     u8 port)
6558c2ecf20Sopenharmony_ci{
6568c2ecf20Sopenharmony_ci	struct ib_port_info *pip = (struct ib_port_info *)smp->data;
6578c2ecf20Sopenharmony_ci	struct ib_event event;
6588c2ecf20Sopenharmony_ci	struct qib_devdata *dd;
6598c2ecf20Sopenharmony_ci	struct qib_pportdata *ppd;
6608c2ecf20Sopenharmony_ci	struct qib_ibport *ibp;
6618c2ecf20Sopenharmony_ci	u8 clientrereg = (pip->clientrereg_resv_subnetto & 0x80);
6628c2ecf20Sopenharmony_ci	unsigned long flags;
6638c2ecf20Sopenharmony_ci	u16 lid, smlid;
6648c2ecf20Sopenharmony_ci	u8 lwe;
6658c2ecf20Sopenharmony_ci	u8 lse;
6668c2ecf20Sopenharmony_ci	u8 state;
6678c2ecf20Sopenharmony_ci	u8 vls;
6688c2ecf20Sopenharmony_ci	u8 msl;
6698c2ecf20Sopenharmony_ci	u16 lstate;
6708c2ecf20Sopenharmony_ci	int ret, ore, mtu;
6718c2ecf20Sopenharmony_ci	u32 port_num = be32_to_cpu(smp->attr_mod);
6728c2ecf20Sopenharmony_ci
6738c2ecf20Sopenharmony_ci	if (port_num == 0)
6748c2ecf20Sopenharmony_ci		port_num = port;
6758c2ecf20Sopenharmony_ci	else {
6768c2ecf20Sopenharmony_ci		if (port_num > ibdev->phys_port_cnt)
6778c2ecf20Sopenharmony_ci			goto err;
6788c2ecf20Sopenharmony_ci		/* Port attributes can only be set on the receiving port */
6798c2ecf20Sopenharmony_ci		if (port_num != port)
6808c2ecf20Sopenharmony_ci			goto get_only;
6818c2ecf20Sopenharmony_ci	}
6828c2ecf20Sopenharmony_ci
6838c2ecf20Sopenharmony_ci	dd = dd_from_ibdev(ibdev);
6848c2ecf20Sopenharmony_ci	/* IB numbers ports from 1, hdw from 0 */
6858c2ecf20Sopenharmony_ci	ppd = dd->pport + (port_num - 1);
6868c2ecf20Sopenharmony_ci	ibp = &ppd->ibport_data;
6878c2ecf20Sopenharmony_ci	event.device = ibdev;
6888c2ecf20Sopenharmony_ci	event.element.port_num = port;
6898c2ecf20Sopenharmony_ci
6908c2ecf20Sopenharmony_ci	ibp->rvp.mkey = pip->mkey;
6918c2ecf20Sopenharmony_ci	ibp->rvp.gid_prefix = pip->gid_prefix;
6928c2ecf20Sopenharmony_ci	ibp->rvp.mkey_lease_period = be16_to_cpu(pip->mkey_lease_period);
6938c2ecf20Sopenharmony_ci
6948c2ecf20Sopenharmony_ci	lid = be16_to_cpu(pip->lid);
6958c2ecf20Sopenharmony_ci	/* Must be a valid unicast LID address. */
6968c2ecf20Sopenharmony_ci	if (lid == 0 || lid >= be16_to_cpu(IB_MULTICAST_LID_BASE))
6978c2ecf20Sopenharmony_ci		smp->status |= IB_SMP_INVALID_FIELD;
6988c2ecf20Sopenharmony_ci	else if (ppd->lid != lid || ppd->lmc != (pip->mkeyprot_resv_lmc & 7)) {
6998c2ecf20Sopenharmony_ci		if (ppd->lid != lid)
7008c2ecf20Sopenharmony_ci			qib_set_uevent_bits(ppd, _QIB_EVENT_LID_CHANGE_BIT);
7018c2ecf20Sopenharmony_ci		if (ppd->lmc != (pip->mkeyprot_resv_lmc & 7))
7028c2ecf20Sopenharmony_ci			qib_set_uevent_bits(ppd, _QIB_EVENT_LMC_CHANGE_BIT);
7038c2ecf20Sopenharmony_ci		qib_set_lid(ppd, lid, pip->mkeyprot_resv_lmc & 7);
7048c2ecf20Sopenharmony_ci		event.event = IB_EVENT_LID_CHANGE;
7058c2ecf20Sopenharmony_ci		ib_dispatch_event(&event);
7068c2ecf20Sopenharmony_ci	}
7078c2ecf20Sopenharmony_ci
7088c2ecf20Sopenharmony_ci	smlid = be16_to_cpu(pip->sm_lid);
7098c2ecf20Sopenharmony_ci	msl = pip->neighbormtu_mastersmsl & 0xF;
7108c2ecf20Sopenharmony_ci	/* Must be a valid unicast LID address. */
7118c2ecf20Sopenharmony_ci	if (smlid == 0 || smlid >= be16_to_cpu(IB_MULTICAST_LID_BASE))
7128c2ecf20Sopenharmony_ci		smp->status |= IB_SMP_INVALID_FIELD;
7138c2ecf20Sopenharmony_ci	else if (smlid != ibp->rvp.sm_lid || msl != ibp->rvp.sm_sl) {
7148c2ecf20Sopenharmony_ci		spin_lock_irqsave(&ibp->rvp.lock, flags);
7158c2ecf20Sopenharmony_ci		if (ibp->rvp.sm_ah) {
7168c2ecf20Sopenharmony_ci			if (smlid != ibp->rvp.sm_lid)
7178c2ecf20Sopenharmony_ci				rdma_ah_set_dlid(&ibp->rvp.sm_ah->attr,
7188c2ecf20Sopenharmony_ci						 smlid);
7198c2ecf20Sopenharmony_ci			if (msl != ibp->rvp.sm_sl)
7208c2ecf20Sopenharmony_ci				rdma_ah_set_sl(&ibp->rvp.sm_ah->attr, msl);
7218c2ecf20Sopenharmony_ci		}
7228c2ecf20Sopenharmony_ci		spin_unlock_irqrestore(&ibp->rvp.lock, flags);
7238c2ecf20Sopenharmony_ci		if (smlid != ibp->rvp.sm_lid)
7248c2ecf20Sopenharmony_ci			ibp->rvp.sm_lid = smlid;
7258c2ecf20Sopenharmony_ci		if (msl != ibp->rvp.sm_sl)
7268c2ecf20Sopenharmony_ci			ibp->rvp.sm_sl = msl;
7278c2ecf20Sopenharmony_ci		event.event = IB_EVENT_SM_CHANGE;
7288c2ecf20Sopenharmony_ci		ib_dispatch_event(&event);
7298c2ecf20Sopenharmony_ci	}
7308c2ecf20Sopenharmony_ci
7318c2ecf20Sopenharmony_ci	/* Allow 1x or 4x to be set (see 14.2.6.6). */
7328c2ecf20Sopenharmony_ci	lwe = pip->link_width_enabled;
7338c2ecf20Sopenharmony_ci	if (lwe) {
7348c2ecf20Sopenharmony_ci		if (lwe == 0xFF)
7358c2ecf20Sopenharmony_ci			set_link_width_enabled(ppd, ppd->link_width_supported);
7368c2ecf20Sopenharmony_ci		else if (lwe >= 16 || (lwe & ~ppd->link_width_supported))
7378c2ecf20Sopenharmony_ci			smp->status |= IB_SMP_INVALID_FIELD;
7388c2ecf20Sopenharmony_ci		else if (lwe != ppd->link_width_enabled)
7398c2ecf20Sopenharmony_ci			set_link_width_enabled(ppd, lwe);
7408c2ecf20Sopenharmony_ci	}
7418c2ecf20Sopenharmony_ci
7428c2ecf20Sopenharmony_ci	lse = pip->linkspeedactive_enabled & 0xF;
7438c2ecf20Sopenharmony_ci	if (lse) {
7448c2ecf20Sopenharmony_ci		/*
7458c2ecf20Sopenharmony_ci		 * The IB 1.2 spec. only allows link speed values
7468c2ecf20Sopenharmony_ci		 * 1, 3, 5, 7, 15.  1.2.1 extended to allow specific
7478c2ecf20Sopenharmony_ci		 * speeds.
7488c2ecf20Sopenharmony_ci		 */
7498c2ecf20Sopenharmony_ci		if (lse == 15)
7508c2ecf20Sopenharmony_ci			set_link_speed_enabled(ppd,
7518c2ecf20Sopenharmony_ci					       ppd->link_speed_supported);
7528c2ecf20Sopenharmony_ci		else if (lse >= 8 || (lse & ~ppd->link_speed_supported))
7538c2ecf20Sopenharmony_ci			smp->status |= IB_SMP_INVALID_FIELD;
7548c2ecf20Sopenharmony_ci		else if (lse != ppd->link_speed_enabled)
7558c2ecf20Sopenharmony_ci			set_link_speed_enabled(ppd, lse);
7568c2ecf20Sopenharmony_ci	}
7578c2ecf20Sopenharmony_ci
7588c2ecf20Sopenharmony_ci	/* Set link down default state. */
7598c2ecf20Sopenharmony_ci	switch (pip->portphysstate_linkdown & 0xF) {
7608c2ecf20Sopenharmony_ci	case 0: /* NOP */
7618c2ecf20Sopenharmony_ci		break;
7628c2ecf20Sopenharmony_ci	case 1: /* SLEEP */
7638c2ecf20Sopenharmony_ci		(void) dd->f_set_ib_cfg(ppd, QIB_IB_CFG_LINKDEFAULT,
7648c2ecf20Sopenharmony_ci					IB_LINKINITCMD_SLEEP);
7658c2ecf20Sopenharmony_ci		break;
7668c2ecf20Sopenharmony_ci	case 2: /* POLL */
7678c2ecf20Sopenharmony_ci		(void) dd->f_set_ib_cfg(ppd, QIB_IB_CFG_LINKDEFAULT,
7688c2ecf20Sopenharmony_ci					IB_LINKINITCMD_POLL);
7698c2ecf20Sopenharmony_ci		break;
7708c2ecf20Sopenharmony_ci	default:
7718c2ecf20Sopenharmony_ci		smp->status |= IB_SMP_INVALID_FIELD;
7728c2ecf20Sopenharmony_ci	}
7738c2ecf20Sopenharmony_ci
7748c2ecf20Sopenharmony_ci	ibp->rvp.mkeyprot = pip->mkeyprot_resv_lmc >> 6;
7758c2ecf20Sopenharmony_ci	ibp->rvp.vl_high_limit = pip->vl_high_limit;
7768c2ecf20Sopenharmony_ci	(void) dd->f_set_ib_cfg(ppd, QIB_IB_CFG_VL_HIGH_LIMIT,
7778c2ecf20Sopenharmony_ci				    ibp->rvp.vl_high_limit);
7788c2ecf20Sopenharmony_ci
7798c2ecf20Sopenharmony_ci	mtu = ib_mtu_enum_to_int((pip->neighbormtu_mastersmsl >> 4) & 0xF);
7808c2ecf20Sopenharmony_ci	if (mtu == -1)
7818c2ecf20Sopenharmony_ci		smp->status |= IB_SMP_INVALID_FIELD;
7828c2ecf20Sopenharmony_ci	else
7838c2ecf20Sopenharmony_ci		qib_set_mtu(ppd, mtu);
7848c2ecf20Sopenharmony_ci
7858c2ecf20Sopenharmony_ci	/* Set operational VLs */
7868c2ecf20Sopenharmony_ci	vls = (pip->operationalvl_pei_peo_fpi_fpo >> 4) & 0xF;
7878c2ecf20Sopenharmony_ci	if (vls) {
7888c2ecf20Sopenharmony_ci		if (vls > ppd->vls_supported)
7898c2ecf20Sopenharmony_ci			smp->status |= IB_SMP_INVALID_FIELD;
7908c2ecf20Sopenharmony_ci		else
7918c2ecf20Sopenharmony_ci			(void) dd->f_set_ib_cfg(ppd, QIB_IB_CFG_OP_VLS, vls);
7928c2ecf20Sopenharmony_ci	}
7938c2ecf20Sopenharmony_ci
7948c2ecf20Sopenharmony_ci	if (pip->mkey_violations == 0)
7958c2ecf20Sopenharmony_ci		ibp->rvp.mkey_violations = 0;
7968c2ecf20Sopenharmony_ci
7978c2ecf20Sopenharmony_ci	if (pip->pkey_violations == 0)
7988c2ecf20Sopenharmony_ci		ibp->rvp.pkey_violations = 0;
7998c2ecf20Sopenharmony_ci
8008c2ecf20Sopenharmony_ci	if (pip->qkey_violations == 0)
8018c2ecf20Sopenharmony_ci		ibp->rvp.qkey_violations = 0;
8028c2ecf20Sopenharmony_ci
8038c2ecf20Sopenharmony_ci	ore = pip->localphyerrors_overrunerrors;
8048c2ecf20Sopenharmony_ci	if (set_phyerrthreshold(ppd, (ore >> 4) & 0xF))
8058c2ecf20Sopenharmony_ci		smp->status |= IB_SMP_INVALID_FIELD;
8068c2ecf20Sopenharmony_ci
8078c2ecf20Sopenharmony_ci	if (set_overrunthreshold(ppd, (ore & 0xF)))
8088c2ecf20Sopenharmony_ci		smp->status |= IB_SMP_INVALID_FIELD;
8098c2ecf20Sopenharmony_ci
8108c2ecf20Sopenharmony_ci	ibp->rvp.subnet_timeout = pip->clientrereg_resv_subnetto & 0x1F;
8118c2ecf20Sopenharmony_ci
8128c2ecf20Sopenharmony_ci	/*
8138c2ecf20Sopenharmony_ci	 * Do the port state change now that the other link parameters
8148c2ecf20Sopenharmony_ci	 * have been set.
8158c2ecf20Sopenharmony_ci	 * Changing the port physical state only makes sense if the link
8168c2ecf20Sopenharmony_ci	 * is down or is being set to down.
8178c2ecf20Sopenharmony_ci	 */
8188c2ecf20Sopenharmony_ci	state = pip->linkspeed_portstate & 0xF;
8198c2ecf20Sopenharmony_ci	lstate = (pip->portphysstate_linkdown >> 4) & 0xF;
8208c2ecf20Sopenharmony_ci	if (lstate && !(state == IB_PORT_DOWN || state == IB_PORT_NOP))
8218c2ecf20Sopenharmony_ci		smp->status |= IB_SMP_INVALID_FIELD;
8228c2ecf20Sopenharmony_ci
8238c2ecf20Sopenharmony_ci	/*
8248c2ecf20Sopenharmony_ci	 * Only state changes of DOWN, ARM, and ACTIVE are valid
8258c2ecf20Sopenharmony_ci	 * and must be in the correct state to take effect (see 7.2.6).
8268c2ecf20Sopenharmony_ci	 */
8278c2ecf20Sopenharmony_ci	switch (state) {
8288c2ecf20Sopenharmony_ci	case IB_PORT_NOP:
8298c2ecf20Sopenharmony_ci		if (lstate == 0)
8308c2ecf20Sopenharmony_ci			break;
8318c2ecf20Sopenharmony_ci		fallthrough;
8328c2ecf20Sopenharmony_ci	case IB_PORT_DOWN:
8338c2ecf20Sopenharmony_ci		if (lstate == 0)
8348c2ecf20Sopenharmony_ci			lstate = QIB_IB_LINKDOWN_ONLY;
8358c2ecf20Sopenharmony_ci		else if (lstate == 1)
8368c2ecf20Sopenharmony_ci			lstate = QIB_IB_LINKDOWN_SLEEP;
8378c2ecf20Sopenharmony_ci		else if (lstate == 2)
8388c2ecf20Sopenharmony_ci			lstate = QIB_IB_LINKDOWN;
8398c2ecf20Sopenharmony_ci		else if (lstate == 3)
8408c2ecf20Sopenharmony_ci			lstate = QIB_IB_LINKDOWN_DISABLE;
8418c2ecf20Sopenharmony_ci		else {
8428c2ecf20Sopenharmony_ci			smp->status |= IB_SMP_INVALID_FIELD;
8438c2ecf20Sopenharmony_ci			break;
8448c2ecf20Sopenharmony_ci		}
8458c2ecf20Sopenharmony_ci		spin_lock_irqsave(&ppd->lflags_lock, flags);
8468c2ecf20Sopenharmony_ci		ppd->lflags &= ~QIBL_LINKV;
8478c2ecf20Sopenharmony_ci		spin_unlock_irqrestore(&ppd->lflags_lock, flags);
8488c2ecf20Sopenharmony_ci		qib_set_linkstate(ppd, lstate);
8498c2ecf20Sopenharmony_ci		/*
8508c2ecf20Sopenharmony_ci		 * Don't send a reply if the response would be sent
8518c2ecf20Sopenharmony_ci		 * through the disabled port.
8528c2ecf20Sopenharmony_ci		 */
8538c2ecf20Sopenharmony_ci		if (lstate == QIB_IB_LINKDOWN_DISABLE && smp->hop_cnt) {
8548c2ecf20Sopenharmony_ci			ret = IB_MAD_RESULT_SUCCESS | IB_MAD_RESULT_CONSUMED;
8558c2ecf20Sopenharmony_ci			goto done;
8568c2ecf20Sopenharmony_ci		}
8578c2ecf20Sopenharmony_ci		qib_wait_linkstate(ppd, QIBL_LINKV, 10);
8588c2ecf20Sopenharmony_ci		break;
8598c2ecf20Sopenharmony_ci	case IB_PORT_ARMED:
8608c2ecf20Sopenharmony_ci		qib_set_linkstate(ppd, QIB_IB_LINKARM);
8618c2ecf20Sopenharmony_ci		break;
8628c2ecf20Sopenharmony_ci	case IB_PORT_ACTIVE:
8638c2ecf20Sopenharmony_ci		qib_set_linkstate(ppd, QIB_IB_LINKACTIVE);
8648c2ecf20Sopenharmony_ci		break;
8658c2ecf20Sopenharmony_ci	default:
8668c2ecf20Sopenharmony_ci		smp->status |= IB_SMP_INVALID_FIELD;
8678c2ecf20Sopenharmony_ci	}
8688c2ecf20Sopenharmony_ci
8698c2ecf20Sopenharmony_ci	if (clientrereg) {
8708c2ecf20Sopenharmony_ci		event.event = IB_EVENT_CLIENT_REREGISTER;
8718c2ecf20Sopenharmony_ci		ib_dispatch_event(&event);
8728c2ecf20Sopenharmony_ci	}
8738c2ecf20Sopenharmony_ci
8748c2ecf20Sopenharmony_ci	/* restore re-reg bit per o14-12.2.1 */
8758c2ecf20Sopenharmony_ci	pip->clientrereg_resv_subnetto |= clientrereg;
8768c2ecf20Sopenharmony_ci
8778c2ecf20Sopenharmony_ci	goto get_only;
8788c2ecf20Sopenharmony_ci
8798c2ecf20Sopenharmony_cierr:
8808c2ecf20Sopenharmony_ci	smp->status |= IB_SMP_INVALID_FIELD;
8818c2ecf20Sopenharmony_ciget_only:
8828c2ecf20Sopenharmony_ci	ret = subn_get_portinfo(smp, ibdev, port);
8838c2ecf20Sopenharmony_cidone:
8848c2ecf20Sopenharmony_ci	return ret;
8858c2ecf20Sopenharmony_ci}
8868c2ecf20Sopenharmony_ci
8878c2ecf20Sopenharmony_ci/**
8888c2ecf20Sopenharmony_ci * rm_pkey - decrecment the reference count for the given PKEY
8898c2ecf20Sopenharmony_ci * @dd: the qlogic_ib device
8908c2ecf20Sopenharmony_ci * @key: the PKEY index
8918c2ecf20Sopenharmony_ci *
8928c2ecf20Sopenharmony_ci * Return true if this was the last reference and the hardware table entry
8938c2ecf20Sopenharmony_ci * needs to be changed.
8948c2ecf20Sopenharmony_ci */
8958c2ecf20Sopenharmony_cistatic int rm_pkey(struct qib_pportdata *ppd, u16 key)
8968c2ecf20Sopenharmony_ci{
8978c2ecf20Sopenharmony_ci	int i;
8988c2ecf20Sopenharmony_ci	int ret;
8998c2ecf20Sopenharmony_ci
9008c2ecf20Sopenharmony_ci	for (i = 0; i < ARRAY_SIZE(ppd->pkeys); i++) {
9018c2ecf20Sopenharmony_ci		if (ppd->pkeys[i] != key)
9028c2ecf20Sopenharmony_ci			continue;
9038c2ecf20Sopenharmony_ci		if (atomic_dec_and_test(&ppd->pkeyrefs[i])) {
9048c2ecf20Sopenharmony_ci			ppd->pkeys[i] = 0;
9058c2ecf20Sopenharmony_ci			ret = 1;
9068c2ecf20Sopenharmony_ci			goto bail;
9078c2ecf20Sopenharmony_ci		}
9088c2ecf20Sopenharmony_ci		break;
9098c2ecf20Sopenharmony_ci	}
9108c2ecf20Sopenharmony_ci
9118c2ecf20Sopenharmony_ci	ret = 0;
9128c2ecf20Sopenharmony_ci
9138c2ecf20Sopenharmony_cibail:
9148c2ecf20Sopenharmony_ci	return ret;
9158c2ecf20Sopenharmony_ci}
9168c2ecf20Sopenharmony_ci
9178c2ecf20Sopenharmony_ci/**
9188c2ecf20Sopenharmony_ci * add_pkey - add the given PKEY to the hardware table
9198c2ecf20Sopenharmony_ci * @dd: the qlogic_ib device
9208c2ecf20Sopenharmony_ci * @key: the PKEY
9218c2ecf20Sopenharmony_ci *
9228c2ecf20Sopenharmony_ci * Return an error code if unable to add the entry, zero if no change,
9238c2ecf20Sopenharmony_ci * or 1 if the hardware PKEY register needs to be updated.
9248c2ecf20Sopenharmony_ci */
9258c2ecf20Sopenharmony_cistatic int add_pkey(struct qib_pportdata *ppd, u16 key)
9268c2ecf20Sopenharmony_ci{
9278c2ecf20Sopenharmony_ci	int i;
9288c2ecf20Sopenharmony_ci	u16 lkey = key & 0x7FFF;
9298c2ecf20Sopenharmony_ci	int any = 0;
9308c2ecf20Sopenharmony_ci	int ret;
9318c2ecf20Sopenharmony_ci
9328c2ecf20Sopenharmony_ci	if (lkey == 0x7FFF) {
9338c2ecf20Sopenharmony_ci		ret = 0;
9348c2ecf20Sopenharmony_ci		goto bail;
9358c2ecf20Sopenharmony_ci	}
9368c2ecf20Sopenharmony_ci
9378c2ecf20Sopenharmony_ci	/* Look for an empty slot or a matching PKEY. */
9388c2ecf20Sopenharmony_ci	for (i = 0; i < ARRAY_SIZE(ppd->pkeys); i++) {
9398c2ecf20Sopenharmony_ci		if (!ppd->pkeys[i]) {
9408c2ecf20Sopenharmony_ci			any++;
9418c2ecf20Sopenharmony_ci			continue;
9428c2ecf20Sopenharmony_ci		}
9438c2ecf20Sopenharmony_ci		/* If it matches exactly, try to increment the ref count */
9448c2ecf20Sopenharmony_ci		if (ppd->pkeys[i] == key) {
9458c2ecf20Sopenharmony_ci			if (atomic_inc_return(&ppd->pkeyrefs[i]) > 1) {
9468c2ecf20Sopenharmony_ci				ret = 0;
9478c2ecf20Sopenharmony_ci				goto bail;
9488c2ecf20Sopenharmony_ci			}
9498c2ecf20Sopenharmony_ci			/* Lost the race. Look for an empty slot below. */
9508c2ecf20Sopenharmony_ci			atomic_dec(&ppd->pkeyrefs[i]);
9518c2ecf20Sopenharmony_ci			any++;
9528c2ecf20Sopenharmony_ci		}
9538c2ecf20Sopenharmony_ci		/*
9548c2ecf20Sopenharmony_ci		 * It makes no sense to have both the limited and unlimited
9558c2ecf20Sopenharmony_ci		 * PKEY set at the same time since the unlimited one will
9568c2ecf20Sopenharmony_ci		 * disable the limited one.
9578c2ecf20Sopenharmony_ci		 */
9588c2ecf20Sopenharmony_ci		if ((ppd->pkeys[i] & 0x7FFF) == lkey) {
9598c2ecf20Sopenharmony_ci			ret = -EEXIST;
9608c2ecf20Sopenharmony_ci			goto bail;
9618c2ecf20Sopenharmony_ci		}
9628c2ecf20Sopenharmony_ci	}
9638c2ecf20Sopenharmony_ci	if (!any) {
9648c2ecf20Sopenharmony_ci		ret = -EBUSY;
9658c2ecf20Sopenharmony_ci		goto bail;
9668c2ecf20Sopenharmony_ci	}
9678c2ecf20Sopenharmony_ci	for (i = 0; i < ARRAY_SIZE(ppd->pkeys); i++) {
9688c2ecf20Sopenharmony_ci		if (!ppd->pkeys[i] &&
9698c2ecf20Sopenharmony_ci		    atomic_inc_return(&ppd->pkeyrefs[i]) == 1) {
9708c2ecf20Sopenharmony_ci			/* for qibstats, etc. */
9718c2ecf20Sopenharmony_ci			ppd->pkeys[i] = key;
9728c2ecf20Sopenharmony_ci			ret = 1;
9738c2ecf20Sopenharmony_ci			goto bail;
9748c2ecf20Sopenharmony_ci		}
9758c2ecf20Sopenharmony_ci	}
9768c2ecf20Sopenharmony_ci	ret = -EBUSY;
9778c2ecf20Sopenharmony_ci
9788c2ecf20Sopenharmony_cibail:
9798c2ecf20Sopenharmony_ci	return ret;
9808c2ecf20Sopenharmony_ci}
9818c2ecf20Sopenharmony_ci
9828c2ecf20Sopenharmony_ci/**
9838c2ecf20Sopenharmony_ci * set_pkeys - set the PKEY table for ctxt 0
9848c2ecf20Sopenharmony_ci * @dd: the qlogic_ib device
9858c2ecf20Sopenharmony_ci * @port: the IB port number
9868c2ecf20Sopenharmony_ci * @pkeys: the PKEY table
9878c2ecf20Sopenharmony_ci */
9888c2ecf20Sopenharmony_cistatic int set_pkeys(struct qib_devdata *dd, u8 port, u16 *pkeys)
9898c2ecf20Sopenharmony_ci{
9908c2ecf20Sopenharmony_ci	struct qib_pportdata *ppd;
9918c2ecf20Sopenharmony_ci	struct qib_ctxtdata *rcd;
9928c2ecf20Sopenharmony_ci	int i;
9938c2ecf20Sopenharmony_ci	int changed = 0;
9948c2ecf20Sopenharmony_ci
9958c2ecf20Sopenharmony_ci	/*
9968c2ecf20Sopenharmony_ci	 * IB port one/two always maps to context zero/one,
9978c2ecf20Sopenharmony_ci	 * always a kernel context, no locking needed
9988c2ecf20Sopenharmony_ci	 * If we get here with ppd setup, no need to check
9998c2ecf20Sopenharmony_ci	 * that rcd is valid.
10008c2ecf20Sopenharmony_ci	 */
10018c2ecf20Sopenharmony_ci	ppd = dd->pport + (port - 1);
10028c2ecf20Sopenharmony_ci	rcd = dd->rcd[ppd->hw_pidx];
10038c2ecf20Sopenharmony_ci
10048c2ecf20Sopenharmony_ci	for (i = 0; i < ARRAY_SIZE(rcd->pkeys); i++) {
10058c2ecf20Sopenharmony_ci		u16 key = pkeys[i];
10068c2ecf20Sopenharmony_ci		u16 okey = rcd->pkeys[i];
10078c2ecf20Sopenharmony_ci
10088c2ecf20Sopenharmony_ci		if (key == okey)
10098c2ecf20Sopenharmony_ci			continue;
10108c2ecf20Sopenharmony_ci		/*
10118c2ecf20Sopenharmony_ci		 * The value of this PKEY table entry is changing.
10128c2ecf20Sopenharmony_ci		 * Remove the old entry in the hardware's array of PKEYs.
10138c2ecf20Sopenharmony_ci		 */
10148c2ecf20Sopenharmony_ci		if (okey & 0x7FFF)
10158c2ecf20Sopenharmony_ci			changed |= rm_pkey(ppd, okey);
10168c2ecf20Sopenharmony_ci		if (key & 0x7FFF) {
10178c2ecf20Sopenharmony_ci			int ret = add_pkey(ppd, key);
10188c2ecf20Sopenharmony_ci
10198c2ecf20Sopenharmony_ci			if (ret < 0)
10208c2ecf20Sopenharmony_ci				key = 0;
10218c2ecf20Sopenharmony_ci			else
10228c2ecf20Sopenharmony_ci				changed |= ret;
10238c2ecf20Sopenharmony_ci		}
10248c2ecf20Sopenharmony_ci		rcd->pkeys[i] = key;
10258c2ecf20Sopenharmony_ci	}
10268c2ecf20Sopenharmony_ci	if (changed) {
10278c2ecf20Sopenharmony_ci		struct ib_event event;
10288c2ecf20Sopenharmony_ci
10298c2ecf20Sopenharmony_ci		(void) dd->f_set_ib_cfg(ppd, QIB_IB_CFG_PKEYS, 0);
10308c2ecf20Sopenharmony_ci
10318c2ecf20Sopenharmony_ci		event.event = IB_EVENT_PKEY_CHANGE;
10328c2ecf20Sopenharmony_ci		event.device = &dd->verbs_dev.rdi.ibdev;
10338c2ecf20Sopenharmony_ci		event.element.port_num = port;
10348c2ecf20Sopenharmony_ci		ib_dispatch_event(&event);
10358c2ecf20Sopenharmony_ci	}
10368c2ecf20Sopenharmony_ci	return 0;
10378c2ecf20Sopenharmony_ci}
10388c2ecf20Sopenharmony_ci
10398c2ecf20Sopenharmony_cistatic int subn_set_pkeytable(struct ib_smp *smp, struct ib_device *ibdev,
10408c2ecf20Sopenharmony_ci			      u8 port)
10418c2ecf20Sopenharmony_ci{
10428c2ecf20Sopenharmony_ci	u32 startpx = 32 * (be32_to_cpu(smp->attr_mod) & 0xffff);
10438c2ecf20Sopenharmony_ci	__be16 *p = (__be16 *) smp->data;
10448c2ecf20Sopenharmony_ci	u16 *q = (u16 *) smp->data;
10458c2ecf20Sopenharmony_ci	struct qib_devdata *dd = dd_from_ibdev(ibdev);
10468c2ecf20Sopenharmony_ci	unsigned i, n = qib_get_npkeys(dd);
10478c2ecf20Sopenharmony_ci
10488c2ecf20Sopenharmony_ci	for (i = 0; i < n; i++)
10498c2ecf20Sopenharmony_ci		q[i] = be16_to_cpu(p[i]);
10508c2ecf20Sopenharmony_ci
10518c2ecf20Sopenharmony_ci	if (startpx != 0 || set_pkeys(dd, port, q) != 0)
10528c2ecf20Sopenharmony_ci		smp->status |= IB_SMP_INVALID_FIELD;
10538c2ecf20Sopenharmony_ci
10548c2ecf20Sopenharmony_ci	return subn_get_pkeytable(smp, ibdev, port);
10558c2ecf20Sopenharmony_ci}
10568c2ecf20Sopenharmony_ci
10578c2ecf20Sopenharmony_cistatic int subn_get_sl_to_vl(struct ib_smp *smp, struct ib_device *ibdev,
10588c2ecf20Sopenharmony_ci			     u8 port)
10598c2ecf20Sopenharmony_ci{
10608c2ecf20Sopenharmony_ci	struct qib_ibport *ibp = to_iport(ibdev, port);
10618c2ecf20Sopenharmony_ci	u8 *p = (u8 *) smp->data;
10628c2ecf20Sopenharmony_ci	unsigned i;
10638c2ecf20Sopenharmony_ci
10648c2ecf20Sopenharmony_ci	memset(smp->data, 0, sizeof(smp->data));
10658c2ecf20Sopenharmony_ci
10668c2ecf20Sopenharmony_ci	if (!(ibp->rvp.port_cap_flags & IB_PORT_SL_MAP_SUP))
10678c2ecf20Sopenharmony_ci		smp->status |= IB_SMP_UNSUP_METHOD;
10688c2ecf20Sopenharmony_ci	else
10698c2ecf20Sopenharmony_ci		for (i = 0; i < ARRAY_SIZE(ibp->sl_to_vl); i += 2)
10708c2ecf20Sopenharmony_ci			*p++ = (ibp->sl_to_vl[i] << 4) | ibp->sl_to_vl[i + 1];
10718c2ecf20Sopenharmony_ci
10728c2ecf20Sopenharmony_ci	return reply(smp);
10738c2ecf20Sopenharmony_ci}
10748c2ecf20Sopenharmony_ci
10758c2ecf20Sopenharmony_cistatic int subn_set_sl_to_vl(struct ib_smp *smp, struct ib_device *ibdev,
10768c2ecf20Sopenharmony_ci			     u8 port)
10778c2ecf20Sopenharmony_ci{
10788c2ecf20Sopenharmony_ci	struct qib_ibport *ibp = to_iport(ibdev, port);
10798c2ecf20Sopenharmony_ci	u8 *p = (u8 *) smp->data;
10808c2ecf20Sopenharmony_ci	unsigned i;
10818c2ecf20Sopenharmony_ci
10828c2ecf20Sopenharmony_ci	if (!(ibp->rvp.port_cap_flags & IB_PORT_SL_MAP_SUP)) {
10838c2ecf20Sopenharmony_ci		smp->status |= IB_SMP_UNSUP_METHOD;
10848c2ecf20Sopenharmony_ci		return reply(smp);
10858c2ecf20Sopenharmony_ci	}
10868c2ecf20Sopenharmony_ci
10878c2ecf20Sopenharmony_ci	for (i = 0; i < ARRAY_SIZE(ibp->sl_to_vl); i += 2, p++) {
10888c2ecf20Sopenharmony_ci		ibp->sl_to_vl[i] = *p >> 4;
10898c2ecf20Sopenharmony_ci		ibp->sl_to_vl[i + 1] = *p & 0xF;
10908c2ecf20Sopenharmony_ci	}
10918c2ecf20Sopenharmony_ci	qib_set_uevent_bits(ppd_from_ibp(to_iport(ibdev, port)),
10928c2ecf20Sopenharmony_ci			    _QIB_EVENT_SL2VL_CHANGE_BIT);
10938c2ecf20Sopenharmony_ci
10948c2ecf20Sopenharmony_ci	return subn_get_sl_to_vl(smp, ibdev, port);
10958c2ecf20Sopenharmony_ci}
10968c2ecf20Sopenharmony_ci
10978c2ecf20Sopenharmony_cistatic int subn_get_vl_arb(struct ib_smp *smp, struct ib_device *ibdev,
10988c2ecf20Sopenharmony_ci			   u8 port)
10998c2ecf20Sopenharmony_ci{
11008c2ecf20Sopenharmony_ci	unsigned which = be32_to_cpu(smp->attr_mod) >> 16;
11018c2ecf20Sopenharmony_ci	struct qib_pportdata *ppd = ppd_from_ibp(to_iport(ibdev, port));
11028c2ecf20Sopenharmony_ci
11038c2ecf20Sopenharmony_ci	memset(smp->data, 0, sizeof(smp->data));
11048c2ecf20Sopenharmony_ci
11058c2ecf20Sopenharmony_ci	if (ppd->vls_supported == IB_VL_VL0)
11068c2ecf20Sopenharmony_ci		smp->status |= IB_SMP_UNSUP_METHOD;
11078c2ecf20Sopenharmony_ci	else if (which == IB_VLARB_LOWPRI_0_31)
11088c2ecf20Sopenharmony_ci		(void) ppd->dd->f_get_ib_table(ppd, QIB_IB_TBL_VL_LOW_ARB,
11098c2ecf20Sopenharmony_ci						   smp->data);
11108c2ecf20Sopenharmony_ci	else if (which == IB_VLARB_HIGHPRI_0_31)
11118c2ecf20Sopenharmony_ci		(void) ppd->dd->f_get_ib_table(ppd, QIB_IB_TBL_VL_HIGH_ARB,
11128c2ecf20Sopenharmony_ci						   smp->data);
11138c2ecf20Sopenharmony_ci	else
11148c2ecf20Sopenharmony_ci		smp->status |= IB_SMP_INVALID_FIELD;
11158c2ecf20Sopenharmony_ci
11168c2ecf20Sopenharmony_ci	return reply(smp);
11178c2ecf20Sopenharmony_ci}
11188c2ecf20Sopenharmony_ci
11198c2ecf20Sopenharmony_cistatic int subn_set_vl_arb(struct ib_smp *smp, struct ib_device *ibdev,
11208c2ecf20Sopenharmony_ci			   u8 port)
11218c2ecf20Sopenharmony_ci{
11228c2ecf20Sopenharmony_ci	unsigned which = be32_to_cpu(smp->attr_mod) >> 16;
11238c2ecf20Sopenharmony_ci	struct qib_pportdata *ppd = ppd_from_ibp(to_iport(ibdev, port));
11248c2ecf20Sopenharmony_ci
11258c2ecf20Sopenharmony_ci	if (ppd->vls_supported == IB_VL_VL0)
11268c2ecf20Sopenharmony_ci		smp->status |= IB_SMP_UNSUP_METHOD;
11278c2ecf20Sopenharmony_ci	else if (which == IB_VLARB_LOWPRI_0_31)
11288c2ecf20Sopenharmony_ci		(void) ppd->dd->f_set_ib_table(ppd, QIB_IB_TBL_VL_LOW_ARB,
11298c2ecf20Sopenharmony_ci						   smp->data);
11308c2ecf20Sopenharmony_ci	else if (which == IB_VLARB_HIGHPRI_0_31)
11318c2ecf20Sopenharmony_ci		(void) ppd->dd->f_set_ib_table(ppd, QIB_IB_TBL_VL_HIGH_ARB,
11328c2ecf20Sopenharmony_ci						   smp->data);
11338c2ecf20Sopenharmony_ci	else
11348c2ecf20Sopenharmony_ci		smp->status |= IB_SMP_INVALID_FIELD;
11358c2ecf20Sopenharmony_ci
11368c2ecf20Sopenharmony_ci	return subn_get_vl_arb(smp, ibdev, port);
11378c2ecf20Sopenharmony_ci}
11388c2ecf20Sopenharmony_ci
11398c2ecf20Sopenharmony_cistatic int subn_trap_repress(struct ib_smp *smp, struct ib_device *ibdev,
11408c2ecf20Sopenharmony_ci			     u8 port)
11418c2ecf20Sopenharmony_ci{
11428c2ecf20Sopenharmony_ci	/*
11438c2ecf20Sopenharmony_ci	 * For now, we only send the trap once so no need to process this.
11448c2ecf20Sopenharmony_ci	 * o13-6, o13-7,
11458c2ecf20Sopenharmony_ci	 * o14-3.a4 The SMA shall not send any message in response to a valid
11468c2ecf20Sopenharmony_ci	 * SubnTrapRepress() message.
11478c2ecf20Sopenharmony_ci	 */
11488c2ecf20Sopenharmony_ci	return IB_MAD_RESULT_SUCCESS | IB_MAD_RESULT_CONSUMED;
11498c2ecf20Sopenharmony_ci}
11508c2ecf20Sopenharmony_ci
11518c2ecf20Sopenharmony_cistatic int pma_get_classportinfo(struct ib_pma_mad *pmp,
11528c2ecf20Sopenharmony_ci				 struct ib_device *ibdev)
11538c2ecf20Sopenharmony_ci{
11548c2ecf20Sopenharmony_ci	struct ib_class_port_info *p =
11558c2ecf20Sopenharmony_ci		(struct ib_class_port_info *)pmp->data;
11568c2ecf20Sopenharmony_ci	struct qib_devdata *dd = dd_from_ibdev(ibdev);
11578c2ecf20Sopenharmony_ci
11588c2ecf20Sopenharmony_ci	memset(pmp->data, 0, sizeof(pmp->data));
11598c2ecf20Sopenharmony_ci
11608c2ecf20Sopenharmony_ci	if (pmp->mad_hdr.attr_mod != 0)
11618c2ecf20Sopenharmony_ci		pmp->mad_hdr.status |= IB_SMP_INVALID_FIELD;
11628c2ecf20Sopenharmony_ci
11638c2ecf20Sopenharmony_ci	/* Note that AllPortSelect is not valid */
11648c2ecf20Sopenharmony_ci	p->base_version = 1;
11658c2ecf20Sopenharmony_ci	p->class_version = 1;
11668c2ecf20Sopenharmony_ci	p->capability_mask = IB_PMA_CLASS_CAP_EXT_WIDTH;
11678c2ecf20Sopenharmony_ci	/*
11688c2ecf20Sopenharmony_ci	 * Set the most significant bit of CM2 to indicate support for
11698c2ecf20Sopenharmony_ci	 * congestion statistics
11708c2ecf20Sopenharmony_ci	 */
11718c2ecf20Sopenharmony_ci	ib_set_cpi_capmask2(p,
11728c2ecf20Sopenharmony_ci			    dd->psxmitwait_supported <<
11738c2ecf20Sopenharmony_ci			    (31 - IB_CLASS_PORT_INFO_RESP_TIME_FIELD_SIZE));
11748c2ecf20Sopenharmony_ci	/*
11758c2ecf20Sopenharmony_ci	 * Expected response time is 4.096 usec. * 2^18 == 1.073741824 sec.
11768c2ecf20Sopenharmony_ci	 */
11778c2ecf20Sopenharmony_ci	ib_set_cpi_resp_time(p, 18);
11788c2ecf20Sopenharmony_ci
11798c2ecf20Sopenharmony_ci	return reply((struct ib_smp *) pmp);
11808c2ecf20Sopenharmony_ci}
11818c2ecf20Sopenharmony_ci
11828c2ecf20Sopenharmony_cistatic int pma_get_portsamplescontrol(struct ib_pma_mad *pmp,
11838c2ecf20Sopenharmony_ci				      struct ib_device *ibdev, u8 port)
11848c2ecf20Sopenharmony_ci{
11858c2ecf20Sopenharmony_ci	struct ib_pma_portsamplescontrol *p =
11868c2ecf20Sopenharmony_ci		(struct ib_pma_portsamplescontrol *)pmp->data;
11878c2ecf20Sopenharmony_ci	struct qib_ibdev *dev = to_idev(ibdev);
11888c2ecf20Sopenharmony_ci	struct qib_devdata *dd = dd_from_dev(dev);
11898c2ecf20Sopenharmony_ci	struct qib_ibport *ibp = to_iport(ibdev, port);
11908c2ecf20Sopenharmony_ci	struct qib_pportdata *ppd = ppd_from_ibp(ibp);
11918c2ecf20Sopenharmony_ci	unsigned long flags;
11928c2ecf20Sopenharmony_ci	u8 port_select = p->port_select;
11938c2ecf20Sopenharmony_ci
11948c2ecf20Sopenharmony_ci	memset(pmp->data, 0, sizeof(pmp->data));
11958c2ecf20Sopenharmony_ci
11968c2ecf20Sopenharmony_ci	p->port_select = port_select;
11978c2ecf20Sopenharmony_ci	if (pmp->mad_hdr.attr_mod != 0 || port_select != port) {
11988c2ecf20Sopenharmony_ci		pmp->mad_hdr.status |= IB_SMP_INVALID_FIELD;
11998c2ecf20Sopenharmony_ci		goto bail;
12008c2ecf20Sopenharmony_ci	}
12018c2ecf20Sopenharmony_ci	spin_lock_irqsave(&ibp->rvp.lock, flags);
12028c2ecf20Sopenharmony_ci	p->tick = dd->f_get_ib_cfg(ppd, QIB_IB_CFG_PMA_TICKS);
12038c2ecf20Sopenharmony_ci	p->sample_status = dd->f_portcntr(ppd, QIBPORTCNTR_PSSTAT);
12048c2ecf20Sopenharmony_ci	p->counter_width = 4;   /* 32 bit counters */
12058c2ecf20Sopenharmony_ci	p->counter_mask0_9 = COUNTER_MASK0_9;
12068c2ecf20Sopenharmony_ci	p->sample_start = cpu_to_be32(ibp->rvp.pma_sample_start);
12078c2ecf20Sopenharmony_ci	p->sample_interval = cpu_to_be32(ibp->rvp.pma_sample_interval);
12088c2ecf20Sopenharmony_ci	p->tag = cpu_to_be16(ibp->rvp.pma_tag);
12098c2ecf20Sopenharmony_ci	p->counter_select[0] = ibp->rvp.pma_counter_select[0];
12108c2ecf20Sopenharmony_ci	p->counter_select[1] = ibp->rvp.pma_counter_select[1];
12118c2ecf20Sopenharmony_ci	p->counter_select[2] = ibp->rvp.pma_counter_select[2];
12128c2ecf20Sopenharmony_ci	p->counter_select[3] = ibp->rvp.pma_counter_select[3];
12138c2ecf20Sopenharmony_ci	p->counter_select[4] = ibp->rvp.pma_counter_select[4];
12148c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&ibp->rvp.lock, flags);
12158c2ecf20Sopenharmony_ci
12168c2ecf20Sopenharmony_cibail:
12178c2ecf20Sopenharmony_ci	return reply((struct ib_smp *) pmp);
12188c2ecf20Sopenharmony_ci}
12198c2ecf20Sopenharmony_ci
12208c2ecf20Sopenharmony_cistatic int pma_set_portsamplescontrol(struct ib_pma_mad *pmp,
12218c2ecf20Sopenharmony_ci				      struct ib_device *ibdev, u8 port)
12228c2ecf20Sopenharmony_ci{
12238c2ecf20Sopenharmony_ci	struct ib_pma_portsamplescontrol *p =
12248c2ecf20Sopenharmony_ci		(struct ib_pma_portsamplescontrol *)pmp->data;
12258c2ecf20Sopenharmony_ci	struct qib_ibdev *dev = to_idev(ibdev);
12268c2ecf20Sopenharmony_ci	struct qib_devdata *dd = dd_from_dev(dev);
12278c2ecf20Sopenharmony_ci	struct qib_ibport *ibp = to_iport(ibdev, port);
12288c2ecf20Sopenharmony_ci	struct qib_pportdata *ppd = ppd_from_ibp(ibp);
12298c2ecf20Sopenharmony_ci	unsigned long flags;
12308c2ecf20Sopenharmony_ci	u8 status, xmit_flags;
12318c2ecf20Sopenharmony_ci	int ret;
12328c2ecf20Sopenharmony_ci
12338c2ecf20Sopenharmony_ci	if (pmp->mad_hdr.attr_mod != 0 || p->port_select != port) {
12348c2ecf20Sopenharmony_ci		pmp->mad_hdr.status |= IB_SMP_INVALID_FIELD;
12358c2ecf20Sopenharmony_ci		ret = reply((struct ib_smp *) pmp);
12368c2ecf20Sopenharmony_ci		goto bail;
12378c2ecf20Sopenharmony_ci	}
12388c2ecf20Sopenharmony_ci
12398c2ecf20Sopenharmony_ci	spin_lock_irqsave(&ibp->rvp.lock, flags);
12408c2ecf20Sopenharmony_ci
12418c2ecf20Sopenharmony_ci	/* Port Sampling code owns the PS* HW counters */
12428c2ecf20Sopenharmony_ci	xmit_flags = ppd->cong_stats.flags;
12438c2ecf20Sopenharmony_ci	ppd->cong_stats.flags = IB_PMA_CONG_HW_CONTROL_SAMPLE;
12448c2ecf20Sopenharmony_ci	status = dd->f_portcntr(ppd, QIBPORTCNTR_PSSTAT);
12458c2ecf20Sopenharmony_ci	if (status == IB_PMA_SAMPLE_STATUS_DONE ||
12468c2ecf20Sopenharmony_ci	    (status == IB_PMA_SAMPLE_STATUS_RUNNING &&
12478c2ecf20Sopenharmony_ci	     xmit_flags == IB_PMA_CONG_HW_CONTROL_TIMER)) {
12488c2ecf20Sopenharmony_ci		ibp->rvp.pma_sample_start = be32_to_cpu(p->sample_start);
12498c2ecf20Sopenharmony_ci		ibp->rvp.pma_sample_interval = be32_to_cpu(p->sample_interval);
12508c2ecf20Sopenharmony_ci		ibp->rvp.pma_tag = be16_to_cpu(p->tag);
12518c2ecf20Sopenharmony_ci		ibp->rvp.pma_counter_select[0] = p->counter_select[0];
12528c2ecf20Sopenharmony_ci		ibp->rvp.pma_counter_select[1] = p->counter_select[1];
12538c2ecf20Sopenharmony_ci		ibp->rvp.pma_counter_select[2] = p->counter_select[2];
12548c2ecf20Sopenharmony_ci		ibp->rvp.pma_counter_select[3] = p->counter_select[3];
12558c2ecf20Sopenharmony_ci		ibp->rvp.pma_counter_select[4] = p->counter_select[4];
12568c2ecf20Sopenharmony_ci		dd->f_set_cntr_sample(ppd, ibp->rvp.pma_sample_interval,
12578c2ecf20Sopenharmony_ci				      ibp->rvp.pma_sample_start);
12588c2ecf20Sopenharmony_ci	}
12598c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&ibp->rvp.lock, flags);
12608c2ecf20Sopenharmony_ci
12618c2ecf20Sopenharmony_ci	ret = pma_get_portsamplescontrol(pmp, ibdev, port);
12628c2ecf20Sopenharmony_ci
12638c2ecf20Sopenharmony_cibail:
12648c2ecf20Sopenharmony_ci	return ret;
12658c2ecf20Sopenharmony_ci}
12668c2ecf20Sopenharmony_ci
12678c2ecf20Sopenharmony_cistatic u64 get_counter(struct qib_ibport *ibp, struct qib_pportdata *ppd,
12688c2ecf20Sopenharmony_ci		       __be16 sel)
12698c2ecf20Sopenharmony_ci{
12708c2ecf20Sopenharmony_ci	u64 ret;
12718c2ecf20Sopenharmony_ci
12728c2ecf20Sopenharmony_ci	switch (sel) {
12738c2ecf20Sopenharmony_ci	case IB_PMA_PORT_XMIT_DATA:
12748c2ecf20Sopenharmony_ci		ret = ppd->dd->f_portcntr(ppd, QIBPORTCNTR_PSXMITDATA);
12758c2ecf20Sopenharmony_ci		break;
12768c2ecf20Sopenharmony_ci	case IB_PMA_PORT_RCV_DATA:
12778c2ecf20Sopenharmony_ci		ret = ppd->dd->f_portcntr(ppd, QIBPORTCNTR_PSRCVDATA);
12788c2ecf20Sopenharmony_ci		break;
12798c2ecf20Sopenharmony_ci	case IB_PMA_PORT_XMIT_PKTS:
12808c2ecf20Sopenharmony_ci		ret = ppd->dd->f_portcntr(ppd, QIBPORTCNTR_PSXMITPKTS);
12818c2ecf20Sopenharmony_ci		break;
12828c2ecf20Sopenharmony_ci	case IB_PMA_PORT_RCV_PKTS:
12838c2ecf20Sopenharmony_ci		ret = ppd->dd->f_portcntr(ppd, QIBPORTCNTR_PSRCVPKTS);
12848c2ecf20Sopenharmony_ci		break;
12858c2ecf20Sopenharmony_ci	case IB_PMA_PORT_XMIT_WAIT:
12868c2ecf20Sopenharmony_ci		ret = ppd->dd->f_portcntr(ppd, QIBPORTCNTR_PSXMITWAIT);
12878c2ecf20Sopenharmony_ci		break;
12888c2ecf20Sopenharmony_ci	default:
12898c2ecf20Sopenharmony_ci		ret = 0;
12908c2ecf20Sopenharmony_ci	}
12918c2ecf20Sopenharmony_ci
12928c2ecf20Sopenharmony_ci	return ret;
12938c2ecf20Sopenharmony_ci}
12948c2ecf20Sopenharmony_ci
12958c2ecf20Sopenharmony_ci/* This function assumes that the xmit_wait lock is already held */
12968c2ecf20Sopenharmony_cistatic u64 xmit_wait_get_value_delta(struct qib_pportdata *ppd)
12978c2ecf20Sopenharmony_ci{
12988c2ecf20Sopenharmony_ci	u32 delta;
12998c2ecf20Sopenharmony_ci
13008c2ecf20Sopenharmony_ci	delta = get_counter(&ppd->ibport_data, ppd,
13018c2ecf20Sopenharmony_ci			    IB_PMA_PORT_XMIT_WAIT);
13028c2ecf20Sopenharmony_ci	return ppd->cong_stats.counter + delta;
13038c2ecf20Sopenharmony_ci}
13048c2ecf20Sopenharmony_ci
13058c2ecf20Sopenharmony_cistatic void cache_hw_sample_counters(struct qib_pportdata *ppd)
13068c2ecf20Sopenharmony_ci{
13078c2ecf20Sopenharmony_ci	struct qib_ibport *ibp = &ppd->ibport_data;
13088c2ecf20Sopenharmony_ci
13098c2ecf20Sopenharmony_ci	ppd->cong_stats.counter_cache.psxmitdata =
13108c2ecf20Sopenharmony_ci		get_counter(ibp, ppd, IB_PMA_PORT_XMIT_DATA);
13118c2ecf20Sopenharmony_ci	ppd->cong_stats.counter_cache.psrcvdata =
13128c2ecf20Sopenharmony_ci		get_counter(ibp, ppd, IB_PMA_PORT_RCV_DATA);
13138c2ecf20Sopenharmony_ci	ppd->cong_stats.counter_cache.psxmitpkts =
13148c2ecf20Sopenharmony_ci		get_counter(ibp, ppd, IB_PMA_PORT_XMIT_PKTS);
13158c2ecf20Sopenharmony_ci	ppd->cong_stats.counter_cache.psrcvpkts =
13168c2ecf20Sopenharmony_ci		get_counter(ibp, ppd, IB_PMA_PORT_RCV_PKTS);
13178c2ecf20Sopenharmony_ci	ppd->cong_stats.counter_cache.psxmitwait =
13188c2ecf20Sopenharmony_ci		get_counter(ibp, ppd, IB_PMA_PORT_XMIT_WAIT);
13198c2ecf20Sopenharmony_ci}
13208c2ecf20Sopenharmony_ci
13218c2ecf20Sopenharmony_cistatic u64 get_cache_hw_sample_counters(struct qib_pportdata *ppd,
13228c2ecf20Sopenharmony_ci					__be16 sel)
13238c2ecf20Sopenharmony_ci{
13248c2ecf20Sopenharmony_ci	u64 ret;
13258c2ecf20Sopenharmony_ci
13268c2ecf20Sopenharmony_ci	switch (sel) {
13278c2ecf20Sopenharmony_ci	case IB_PMA_PORT_XMIT_DATA:
13288c2ecf20Sopenharmony_ci		ret = ppd->cong_stats.counter_cache.psxmitdata;
13298c2ecf20Sopenharmony_ci		break;
13308c2ecf20Sopenharmony_ci	case IB_PMA_PORT_RCV_DATA:
13318c2ecf20Sopenharmony_ci		ret = ppd->cong_stats.counter_cache.psrcvdata;
13328c2ecf20Sopenharmony_ci		break;
13338c2ecf20Sopenharmony_ci	case IB_PMA_PORT_XMIT_PKTS:
13348c2ecf20Sopenharmony_ci		ret = ppd->cong_stats.counter_cache.psxmitpkts;
13358c2ecf20Sopenharmony_ci		break;
13368c2ecf20Sopenharmony_ci	case IB_PMA_PORT_RCV_PKTS:
13378c2ecf20Sopenharmony_ci		ret = ppd->cong_stats.counter_cache.psrcvpkts;
13388c2ecf20Sopenharmony_ci		break;
13398c2ecf20Sopenharmony_ci	case IB_PMA_PORT_XMIT_WAIT:
13408c2ecf20Sopenharmony_ci		ret = ppd->cong_stats.counter_cache.psxmitwait;
13418c2ecf20Sopenharmony_ci		break;
13428c2ecf20Sopenharmony_ci	default:
13438c2ecf20Sopenharmony_ci		ret = 0;
13448c2ecf20Sopenharmony_ci	}
13458c2ecf20Sopenharmony_ci
13468c2ecf20Sopenharmony_ci	return ret;
13478c2ecf20Sopenharmony_ci}
13488c2ecf20Sopenharmony_ci
13498c2ecf20Sopenharmony_cistatic int pma_get_portsamplesresult(struct ib_pma_mad *pmp,
13508c2ecf20Sopenharmony_ci				     struct ib_device *ibdev, u8 port)
13518c2ecf20Sopenharmony_ci{
13528c2ecf20Sopenharmony_ci	struct ib_pma_portsamplesresult *p =
13538c2ecf20Sopenharmony_ci		(struct ib_pma_portsamplesresult *)pmp->data;
13548c2ecf20Sopenharmony_ci	struct qib_ibdev *dev = to_idev(ibdev);
13558c2ecf20Sopenharmony_ci	struct qib_devdata *dd = dd_from_dev(dev);
13568c2ecf20Sopenharmony_ci	struct qib_ibport *ibp = to_iport(ibdev, port);
13578c2ecf20Sopenharmony_ci	struct qib_pportdata *ppd = ppd_from_ibp(ibp);
13588c2ecf20Sopenharmony_ci	unsigned long flags;
13598c2ecf20Sopenharmony_ci	u8 status;
13608c2ecf20Sopenharmony_ci	int i;
13618c2ecf20Sopenharmony_ci
13628c2ecf20Sopenharmony_ci	memset(pmp->data, 0, sizeof(pmp->data));
13638c2ecf20Sopenharmony_ci	spin_lock_irqsave(&ibp->rvp.lock, flags);
13648c2ecf20Sopenharmony_ci	p->tag = cpu_to_be16(ibp->rvp.pma_tag);
13658c2ecf20Sopenharmony_ci	if (ppd->cong_stats.flags == IB_PMA_CONG_HW_CONTROL_TIMER)
13668c2ecf20Sopenharmony_ci		p->sample_status = IB_PMA_SAMPLE_STATUS_DONE;
13678c2ecf20Sopenharmony_ci	else {
13688c2ecf20Sopenharmony_ci		status = dd->f_portcntr(ppd, QIBPORTCNTR_PSSTAT);
13698c2ecf20Sopenharmony_ci		p->sample_status = cpu_to_be16(status);
13708c2ecf20Sopenharmony_ci		if (status == IB_PMA_SAMPLE_STATUS_DONE) {
13718c2ecf20Sopenharmony_ci			cache_hw_sample_counters(ppd);
13728c2ecf20Sopenharmony_ci			ppd->cong_stats.counter =
13738c2ecf20Sopenharmony_ci				xmit_wait_get_value_delta(ppd);
13748c2ecf20Sopenharmony_ci			dd->f_set_cntr_sample(ppd,
13758c2ecf20Sopenharmony_ci					      QIB_CONG_TIMER_PSINTERVAL, 0);
13768c2ecf20Sopenharmony_ci			ppd->cong_stats.flags = IB_PMA_CONG_HW_CONTROL_TIMER;
13778c2ecf20Sopenharmony_ci		}
13788c2ecf20Sopenharmony_ci	}
13798c2ecf20Sopenharmony_ci	for (i = 0; i < ARRAY_SIZE(ibp->rvp.pma_counter_select); i++)
13808c2ecf20Sopenharmony_ci		p->counter[i] = cpu_to_be32(
13818c2ecf20Sopenharmony_ci			get_cache_hw_sample_counters(
13828c2ecf20Sopenharmony_ci				ppd, ibp->rvp.pma_counter_select[i]));
13838c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&ibp->rvp.lock, flags);
13848c2ecf20Sopenharmony_ci
13858c2ecf20Sopenharmony_ci	return reply((struct ib_smp *) pmp);
13868c2ecf20Sopenharmony_ci}
13878c2ecf20Sopenharmony_ci
13888c2ecf20Sopenharmony_cistatic int pma_get_portsamplesresult_ext(struct ib_pma_mad *pmp,
13898c2ecf20Sopenharmony_ci					 struct ib_device *ibdev, u8 port)
13908c2ecf20Sopenharmony_ci{
13918c2ecf20Sopenharmony_ci	struct ib_pma_portsamplesresult_ext *p =
13928c2ecf20Sopenharmony_ci		(struct ib_pma_portsamplesresult_ext *)pmp->data;
13938c2ecf20Sopenharmony_ci	struct qib_ibdev *dev = to_idev(ibdev);
13948c2ecf20Sopenharmony_ci	struct qib_devdata *dd = dd_from_dev(dev);
13958c2ecf20Sopenharmony_ci	struct qib_ibport *ibp = to_iport(ibdev, port);
13968c2ecf20Sopenharmony_ci	struct qib_pportdata *ppd = ppd_from_ibp(ibp);
13978c2ecf20Sopenharmony_ci	unsigned long flags;
13988c2ecf20Sopenharmony_ci	u8 status;
13998c2ecf20Sopenharmony_ci	int i;
14008c2ecf20Sopenharmony_ci
14018c2ecf20Sopenharmony_ci	/* Port Sampling code owns the PS* HW counters */
14028c2ecf20Sopenharmony_ci	memset(pmp->data, 0, sizeof(pmp->data));
14038c2ecf20Sopenharmony_ci	spin_lock_irqsave(&ibp->rvp.lock, flags);
14048c2ecf20Sopenharmony_ci	p->tag = cpu_to_be16(ibp->rvp.pma_tag);
14058c2ecf20Sopenharmony_ci	if (ppd->cong_stats.flags == IB_PMA_CONG_HW_CONTROL_TIMER)
14068c2ecf20Sopenharmony_ci		p->sample_status = IB_PMA_SAMPLE_STATUS_DONE;
14078c2ecf20Sopenharmony_ci	else {
14088c2ecf20Sopenharmony_ci		status = dd->f_portcntr(ppd, QIBPORTCNTR_PSSTAT);
14098c2ecf20Sopenharmony_ci		p->sample_status = cpu_to_be16(status);
14108c2ecf20Sopenharmony_ci		/* 64 bits */
14118c2ecf20Sopenharmony_ci		p->extended_width = cpu_to_be32(0x80000000);
14128c2ecf20Sopenharmony_ci		if (status == IB_PMA_SAMPLE_STATUS_DONE) {
14138c2ecf20Sopenharmony_ci			cache_hw_sample_counters(ppd);
14148c2ecf20Sopenharmony_ci			ppd->cong_stats.counter =
14158c2ecf20Sopenharmony_ci				xmit_wait_get_value_delta(ppd);
14168c2ecf20Sopenharmony_ci			dd->f_set_cntr_sample(ppd,
14178c2ecf20Sopenharmony_ci					      QIB_CONG_TIMER_PSINTERVAL, 0);
14188c2ecf20Sopenharmony_ci			ppd->cong_stats.flags = IB_PMA_CONG_HW_CONTROL_TIMER;
14198c2ecf20Sopenharmony_ci		}
14208c2ecf20Sopenharmony_ci	}
14218c2ecf20Sopenharmony_ci	for (i = 0; i < ARRAY_SIZE(ibp->rvp.pma_counter_select); i++)
14228c2ecf20Sopenharmony_ci		p->counter[i] = cpu_to_be64(
14238c2ecf20Sopenharmony_ci			get_cache_hw_sample_counters(
14248c2ecf20Sopenharmony_ci				ppd, ibp->rvp.pma_counter_select[i]));
14258c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&ibp->rvp.lock, flags);
14268c2ecf20Sopenharmony_ci
14278c2ecf20Sopenharmony_ci	return reply((struct ib_smp *) pmp);
14288c2ecf20Sopenharmony_ci}
14298c2ecf20Sopenharmony_ci
14308c2ecf20Sopenharmony_cistatic int pma_get_portcounters(struct ib_pma_mad *pmp,
14318c2ecf20Sopenharmony_ci				struct ib_device *ibdev, u8 port)
14328c2ecf20Sopenharmony_ci{
14338c2ecf20Sopenharmony_ci	struct ib_pma_portcounters *p = (struct ib_pma_portcounters *)
14348c2ecf20Sopenharmony_ci		pmp->data;
14358c2ecf20Sopenharmony_ci	struct qib_ibport *ibp = to_iport(ibdev, port);
14368c2ecf20Sopenharmony_ci	struct qib_pportdata *ppd = ppd_from_ibp(ibp);
14378c2ecf20Sopenharmony_ci	struct qib_verbs_counters cntrs;
14388c2ecf20Sopenharmony_ci	u8 port_select = p->port_select;
14398c2ecf20Sopenharmony_ci
14408c2ecf20Sopenharmony_ci	qib_get_counters(ppd, &cntrs);
14418c2ecf20Sopenharmony_ci
14428c2ecf20Sopenharmony_ci	/* Adjust counters for any resets done. */
14438c2ecf20Sopenharmony_ci	cntrs.symbol_error_counter -= ibp->z_symbol_error_counter;
14448c2ecf20Sopenharmony_ci	cntrs.link_error_recovery_counter -=
14458c2ecf20Sopenharmony_ci		ibp->z_link_error_recovery_counter;
14468c2ecf20Sopenharmony_ci	cntrs.link_downed_counter -= ibp->z_link_downed_counter;
14478c2ecf20Sopenharmony_ci	cntrs.port_rcv_errors -= ibp->z_port_rcv_errors;
14488c2ecf20Sopenharmony_ci	cntrs.port_rcv_remphys_errors -= ibp->z_port_rcv_remphys_errors;
14498c2ecf20Sopenharmony_ci	cntrs.port_xmit_discards -= ibp->z_port_xmit_discards;
14508c2ecf20Sopenharmony_ci	cntrs.port_xmit_data -= ibp->z_port_xmit_data;
14518c2ecf20Sopenharmony_ci	cntrs.port_rcv_data -= ibp->z_port_rcv_data;
14528c2ecf20Sopenharmony_ci	cntrs.port_xmit_packets -= ibp->z_port_xmit_packets;
14538c2ecf20Sopenharmony_ci	cntrs.port_rcv_packets -= ibp->z_port_rcv_packets;
14548c2ecf20Sopenharmony_ci	cntrs.local_link_integrity_errors -=
14558c2ecf20Sopenharmony_ci		ibp->z_local_link_integrity_errors;
14568c2ecf20Sopenharmony_ci	cntrs.excessive_buffer_overrun_errors -=
14578c2ecf20Sopenharmony_ci		ibp->z_excessive_buffer_overrun_errors;
14588c2ecf20Sopenharmony_ci	cntrs.vl15_dropped -= ibp->z_vl15_dropped;
14598c2ecf20Sopenharmony_ci	cntrs.vl15_dropped += ibp->rvp.n_vl15_dropped;
14608c2ecf20Sopenharmony_ci
14618c2ecf20Sopenharmony_ci	memset(pmp->data, 0, sizeof(pmp->data));
14628c2ecf20Sopenharmony_ci
14638c2ecf20Sopenharmony_ci	p->port_select = port_select;
14648c2ecf20Sopenharmony_ci	if (pmp->mad_hdr.attr_mod != 0 || port_select != port)
14658c2ecf20Sopenharmony_ci		pmp->mad_hdr.status |= IB_SMP_INVALID_FIELD;
14668c2ecf20Sopenharmony_ci
14678c2ecf20Sopenharmony_ci	if (cntrs.symbol_error_counter > 0xFFFFUL)
14688c2ecf20Sopenharmony_ci		p->symbol_error_counter = cpu_to_be16(0xFFFF);
14698c2ecf20Sopenharmony_ci	else
14708c2ecf20Sopenharmony_ci		p->symbol_error_counter =
14718c2ecf20Sopenharmony_ci			cpu_to_be16((u16)cntrs.symbol_error_counter);
14728c2ecf20Sopenharmony_ci	if (cntrs.link_error_recovery_counter > 0xFFUL)
14738c2ecf20Sopenharmony_ci		p->link_error_recovery_counter = 0xFF;
14748c2ecf20Sopenharmony_ci	else
14758c2ecf20Sopenharmony_ci		p->link_error_recovery_counter =
14768c2ecf20Sopenharmony_ci			(u8)cntrs.link_error_recovery_counter;
14778c2ecf20Sopenharmony_ci	if (cntrs.link_downed_counter > 0xFFUL)
14788c2ecf20Sopenharmony_ci		p->link_downed_counter = 0xFF;
14798c2ecf20Sopenharmony_ci	else
14808c2ecf20Sopenharmony_ci		p->link_downed_counter = (u8)cntrs.link_downed_counter;
14818c2ecf20Sopenharmony_ci	if (cntrs.port_rcv_errors > 0xFFFFUL)
14828c2ecf20Sopenharmony_ci		p->port_rcv_errors = cpu_to_be16(0xFFFF);
14838c2ecf20Sopenharmony_ci	else
14848c2ecf20Sopenharmony_ci		p->port_rcv_errors =
14858c2ecf20Sopenharmony_ci			cpu_to_be16((u16) cntrs.port_rcv_errors);
14868c2ecf20Sopenharmony_ci	if (cntrs.port_rcv_remphys_errors > 0xFFFFUL)
14878c2ecf20Sopenharmony_ci		p->port_rcv_remphys_errors = cpu_to_be16(0xFFFF);
14888c2ecf20Sopenharmony_ci	else
14898c2ecf20Sopenharmony_ci		p->port_rcv_remphys_errors =
14908c2ecf20Sopenharmony_ci			cpu_to_be16((u16)cntrs.port_rcv_remphys_errors);
14918c2ecf20Sopenharmony_ci	if (cntrs.port_xmit_discards > 0xFFFFUL)
14928c2ecf20Sopenharmony_ci		p->port_xmit_discards = cpu_to_be16(0xFFFF);
14938c2ecf20Sopenharmony_ci	else
14948c2ecf20Sopenharmony_ci		p->port_xmit_discards =
14958c2ecf20Sopenharmony_ci			cpu_to_be16((u16)cntrs.port_xmit_discards);
14968c2ecf20Sopenharmony_ci	if (cntrs.local_link_integrity_errors > 0xFUL)
14978c2ecf20Sopenharmony_ci		cntrs.local_link_integrity_errors = 0xFUL;
14988c2ecf20Sopenharmony_ci	if (cntrs.excessive_buffer_overrun_errors > 0xFUL)
14998c2ecf20Sopenharmony_ci		cntrs.excessive_buffer_overrun_errors = 0xFUL;
15008c2ecf20Sopenharmony_ci	p->link_overrun_errors = (cntrs.local_link_integrity_errors << 4) |
15018c2ecf20Sopenharmony_ci		cntrs.excessive_buffer_overrun_errors;
15028c2ecf20Sopenharmony_ci	if (cntrs.vl15_dropped > 0xFFFFUL)
15038c2ecf20Sopenharmony_ci		p->vl15_dropped = cpu_to_be16(0xFFFF);
15048c2ecf20Sopenharmony_ci	else
15058c2ecf20Sopenharmony_ci		p->vl15_dropped = cpu_to_be16((u16)cntrs.vl15_dropped);
15068c2ecf20Sopenharmony_ci	if (cntrs.port_xmit_data > 0xFFFFFFFFUL)
15078c2ecf20Sopenharmony_ci		p->port_xmit_data = cpu_to_be32(0xFFFFFFFF);
15088c2ecf20Sopenharmony_ci	else
15098c2ecf20Sopenharmony_ci		p->port_xmit_data = cpu_to_be32((u32)cntrs.port_xmit_data);
15108c2ecf20Sopenharmony_ci	if (cntrs.port_rcv_data > 0xFFFFFFFFUL)
15118c2ecf20Sopenharmony_ci		p->port_rcv_data = cpu_to_be32(0xFFFFFFFF);
15128c2ecf20Sopenharmony_ci	else
15138c2ecf20Sopenharmony_ci		p->port_rcv_data = cpu_to_be32((u32)cntrs.port_rcv_data);
15148c2ecf20Sopenharmony_ci	if (cntrs.port_xmit_packets > 0xFFFFFFFFUL)
15158c2ecf20Sopenharmony_ci		p->port_xmit_packets = cpu_to_be32(0xFFFFFFFF);
15168c2ecf20Sopenharmony_ci	else
15178c2ecf20Sopenharmony_ci		p->port_xmit_packets =
15188c2ecf20Sopenharmony_ci			cpu_to_be32((u32)cntrs.port_xmit_packets);
15198c2ecf20Sopenharmony_ci	if (cntrs.port_rcv_packets > 0xFFFFFFFFUL)
15208c2ecf20Sopenharmony_ci		p->port_rcv_packets = cpu_to_be32(0xFFFFFFFF);
15218c2ecf20Sopenharmony_ci	else
15228c2ecf20Sopenharmony_ci		p->port_rcv_packets =
15238c2ecf20Sopenharmony_ci			cpu_to_be32((u32) cntrs.port_rcv_packets);
15248c2ecf20Sopenharmony_ci
15258c2ecf20Sopenharmony_ci	return reply((struct ib_smp *) pmp);
15268c2ecf20Sopenharmony_ci}
15278c2ecf20Sopenharmony_ci
15288c2ecf20Sopenharmony_cistatic int pma_get_portcounters_cong(struct ib_pma_mad *pmp,
15298c2ecf20Sopenharmony_ci				     struct ib_device *ibdev, u8 port)
15308c2ecf20Sopenharmony_ci{
15318c2ecf20Sopenharmony_ci	/* Congestion PMA packets start at offset 24 not 64 */
15328c2ecf20Sopenharmony_ci	struct ib_pma_portcounters_cong *p =
15338c2ecf20Sopenharmony_ci		(struct ib_pma_portcounters_cong *)pmp->reserved;
15348c2ecf20Sopenharmony_ci	struct qib_verbs_counters cntrs;
15358c2ecf20Sopenharmony_ci	struct qib_ibport *ibp = to_iport(ibdev, port);
15368c2ecf20Sopenharmony_ci	struct qib_pportdata *ppd = ppd_from_ibp(ibp);
15378c2ecf20Sopenharmony_ci	struct qib_devdata *dd = dd_from_ppd(ppd);
15388c2ecf20Sopenharmony_ci	u32 port_select = be32_to_cpu(pmp->mad_hdr.attr_mod) & 0xFF;
15398c2ecf20Sopenharmony_ci	u64 xmit_wait_counter;
15408c2ecf20Sopenharmony_ci	unsigned long flags;
15418c2ecf20Sopenharmony_ci
15428c2ecf20Sopenharmony_ci	/*
15438c2ecf20Sopenharmony_ci	 * This check is performed only in the GET method because the
15448c2ecf20Sopenharmony_ci	 * SET method ends up calling this anyway.
15458c2ecf20Sopenharmony_ci	 */
15468c2ecf20Sopenharmony_ci	if (!dd->psxmitwait_supported)
15478c2ecf20Sopenharmony_ci		pmp->mad_hdr.status |= IB_SMP_UNSUP_METH_ATTR;
15488c2ecf20Sopenharmony_ci	if (port_select != port)
15498c2ecf20Sopenharmony_ci		pmp->mad_hdr.status |= IB_SMP_INVALID_FIELD;
15508c2ecf20Sopenharmony_ci
15518c2ecf20Sopenharmony_ci	qib_get_counters(ppd, &cntrs);
15528c2ecf20Sopenharmony_ci	spin_lock_irqsave(&ppd->ibport_data.rvp.lock, flags);
15538c2ecf20Sopenharmony_ci	xmit_wait_counter = xmit_wait_get_value_delta(ppd);
15548c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&ppd->ibport_data.rvp.lock, flags);
15558c2ecf20Sopenharmony_ci
15568c2ecf20Sopenharmony_ci	/* Adjust counters for any resets done. */
15578c2ecf20Sopenharmony_ci	cntrs.symbol_error_counter -= ibp->z_symbol_error_counter;
15588c2ecf20Sopenharmony_ci	cntrs.link_error_recovery_counter -=
15598c2ecf20Sopenharmony_ci		ibp->z_link_error_recovery_counter;
15608c2ecf20Sopenharmony_ci	cntrs.link_downed_counter -= ibp->z_link_downed_counter;
15618c2ecf20Sopenharmony_ci	cntrs.port_rcv_errors -= ibp->z_port_rcv_errors;
15628c2ecf20Sopenharmony_ci	cntrs.port_rcv_remphys_errors -=
15638c2ecf20Sopenharmony_ci		ibp->z_port_rcv_remphys_errors;
15648c2ecf20Sopenharmony_ci	cntrs.port_xmit_discards -= ibp->z_port_xmit_discards;
15658c2ecf20Sopenharmony_ci	cntrs.local_link_integrity_errors -=
15668c2ecf20Sopenharmony_ci		ibp->z_local_link_integrity_errors;
15678c2ecf20Sopenharmony_ci	cntrs.excessive_buffer_overrun_errors -=
15688c2ecf20Sopenharmony_ci		ibp->z_excessive_buffer_overrun_errors;
15698c2ecf20Sopenharmony_ci	cntrs.vl15_dropped -= ibp->z_vl15_dropped;
15708c2ecf20Sopenharmony_ci	cntrs.vl15_dropped += ibp->rvp.n_vl15_dropped;
15718c2ecf20Sopenharmony_ci	cntrs.port_xmit_data -= ibp->z_port_xmit_data;
15728c2ecf20Sopenharmony_ci	cntrs.port_rcv_data -= ibp->z_port_rcv_data;
15738c2ecf20Sopenharmony_ci	cntrs.port_xmit_packets -= ibp->z_port_xmit_packets;
15748c2ecf20Sopenharmony_ci	cntrs.port_rcv_packets -= ibp->z_port_rcv_packets;
15758c2ecf20Sopenharmony_ci
15768c2ecf20Sopenharmony_ci	memset(pmp->reserved, 0, sizeof(pmp->reserved));
15778c2ecf20Sopenharmony_ci	memset(pmp->data, 0, sizeof(pmp->data));
15788c2ecf20Sopenharmony_ci
15798c2ecf20Sopenharmony_ci	/*
15808c2ecf20Sopenharmony_ci	 * Set top 3 bits to indicate interval in picoseconds in
15818c2ecf20Sopenharmony_ci	 * remaining bits.
15828c2ecf20Sopenharmony_ci	 */
15838c2ecf20Sopenharmony_ci	p->port_check_rate =
15848c2ecf20Sopenharmony_ci		cpu_to_be16((QIB_XMIT_RATE_PICO << 13) |
15858c2ecf20Sopenharmony_ci			    (dd->psxmitwait_check_rate &
15868c2ecf20Sopenharmony_ci			     ~(QIB_XMIT_RATE_PICO << 13)));
15878c2ecf20Sopenharmony_ci	p->port_adr_events = cpu_to_be64(0);
15888c2ecf20Sopenharmony_ci	p->port_xmit_wait = cpu_to_be64(xmit_wait_counter);
15898c2ecf20Sopenharmony_ci	p->port_xmit_data = cpu_to_be64(cntrs.port_xmit_data);
15908c2ecf20Sopenharmony_ci	p->port_rcv_data = cpu_to_be64(cntrs.port_rcv_data);
15918c2ecf20Sopenharmony_ci	p->port_xmit_packets =
15928c2ecf20Sopenharmony_ci		cpu_to_be64(cntrs.port_xmit_packets);
15938c2ecf20Sopenharmony_ci	p->port_rcv_packets =
15948c2ecf20Sopenharmony_ci		cpu_to_be64(cntrs.port_rcv_packets);
15958c2ecf20Sopenharmony_ci	if (cntrs.symbol_error_counter > 0xFFFFUL)
15968c2ecf20Sopenharmony_ci		p->symbol_error_counter = cpu_to_be16(0xFFFF);
15978c2ecf20Sopenharmony_ci	else
15988c2ecf20Sopenharmony_ci		p->symbol_error_counter =
15998c2ecf20Sopenharmony_ci			cpu_to_be16(
16008c2ecf20Sopenharmony_ci				(u16)cntrs.symbol_error_counter);
16018c2ecf20Sopenharmony_ci	if (cntrs.link_error_recovery_counter > 0xFFUL)
16028c2ecf20Sopenharmony_ci		p->link_error_recovery_counter = 0xFF;
16038c2ecf20Sopenharmony_ci	else
16048c2ecf20Sopenharmony_ci		p->link_error_recovery_counter =
16058c2ecf20Sopenharmony_ci			(u8)cntrs.link_error_recovery_counter;
16068c2ecf20Sopenharmony_ci	if (cntrs.link_downed_counter > 0xFFUL)
16078c2ecf20Sopenharmony_ci		p->link_downed_counter = 0xFF;
16088c2ecf20Sopenharmony_ci	else
16098c2ecf20Sopenharmony_ci		p->link_downed_counter =
16108c2ecf20Sopenharmony_ci			(u8)cntrs.link_downed_counter;
16118c2ecf20Sopenharmony_ci	if (cntrs.port_rcv_errors > 0xFFFFUL)
16128c2ecf20Sopenharmony_ci		p->port_rcv_errors = cpu_to_be16(0xFFFF);
16138c2ecf20Sopenharmony_ci	else
16148c2ecf20Sopenharmony_ci		p->port_rcv_errors =
16158c2ecf20Sopenharmony_ci			cpu_to_be16((u16) cntrs.port_rcv_errors);
16168c2ecf20Sopenharmony_ci	if (cntrs.port_rcv_remphys_errors > 0xFFFFUL)
16178c2ecf20Sopenharmony_ci		p->port_rcv_remphys_errors = cpu_to_be16(0xFFFF);
16188c2ecf20Sopenharmony_ci	else
16198c2ecf20Sopenharmony_ci		p->port_rcv_remphys_errors =
16208c2ecf20Sopenharmony_ci			cpu_to_be16(
16218c2ecf20Sopenharmony_ci				(u16)cntrs.port_rcv_remphys_errors);
16228c2ecf20Sopenharmony_ci	if (cntrs.port_xmit_discards > 0xFFFFUL)
16238c2ecf20Sopenharmony_ci		p->port_xmit_discards = cpu_to_be16(0xFFFF);
16248c2ecf20Sopenharmony_ci	else
16258c2ecf20Sopenharmony_ci		p->port_xmit_discards =
16268c2ecf20Sopenharmony_ci			cpu_to_be16((u16)cntrs.port_xmit_discards);
16278c2ecf20Sopenharmony_ci	if (cntrs.local_link_integrity_errors > 0xFUL)
16288c2ecf20Sopenharmony_ci		cntrs.local_link_integrity_errors = 0xFUL;
16298c2ecf20Sopenharmony_ci	if (cntrs.excessive_buffer_overrun_errors > 0xFUL)
16308c2ecf20Sopenharmony_ci		cntrs.excessive_buffer_overrun_errors = 0xFUL;
16318c2ecf20Sopenharmony_ci	p->link_overrun_errors = (cntrs.local_link_integrity_errors << 4) |
16328c2ecf20Sopenharmony_ci		cntrs.excessive_buffer_overrun_errors;
16338c2ecf20Sopenharmony_ci	if (cntrs.vl15_dropped > 0xFFFFUL)
16348c2ecf20Sopenharmony_ci		p->vl15_dropped = cpu_to_be16(0xFFFF);
16358c2ecf20Sopenharmony_ci	else
16368c2ecf20Sopenharmony_ci		p->vl15_dropped = cpu_to_be16((u16)cntrs.vl15_dropped);
16378c2ecf20Sopenharmony_ci
16388c2ecf20Sopenharmony_ci	return reply((struct ib_smp *)pmp);
16398c2ecf20Sopenharmony_ci}
16408c2ecf20Sopenharmony_ci
16418c2ecf20Sopenharmony_cistatic void qib_snapshot_pmacounters(
16428c2ecf20Sopenharmony_ci	struct qib_ibport *ibp,
16438c2ecf20Sopenharmony_ci	struct qib_pma_counters *pmacounters)
16448c2ecf20Sopenharmony_ci{
16458c2ecf20Sopenharmony_ci	struct qib_pma_counters *p;
16468c2ecf20Sopenharmony_ci	int cpu;
16478c2ecf20Sopenharmony_ci
16488c2ecf20Sopenharmony_ci	memset(pmacounters, 0, sizeof(*pmacounters));
16498c2ecf20Sopenharmony_ci	for_each_possible_cpu(cpu) {
16508c2ecf20Sopenharmony_ci		p = per_cpu_ptr(ibp->pmastats, cpu);
16518c2ecf20Sopenharmony_ci		pmacounters->n_unicast_xmit += p->n_unicast_xmit;
16528c2ecf20Sopenharmony_ci		pmacounters->n_unicast_rcv += p->n_unicast_rcv;
16538c2ecf20Sopenharmony_ci		pmacounters->n_multicast_xmit += p->n_multicast_xmit;
16548c2ecf20Sopenharmony_ci		pmacounters->n_multicast_rcv += p->n_multicast_rcv;
16558c2ecf20Sopenharmony_ci	}
16568c2ecf20Sopenharmony_ci}
16578c2ecf20Sopenharmony_ci
16588c2ecf20Sopenharmony_cistatic int pma_get_portcounters_ext(struct ib_pma_mad *pmp,
16598c2ecf20Sopenharmony_ci				    struct ib_device *ibdev, u8 port)
16608c2ecf20Sopenharmony_ci{
16618c2ecf20Sopenharmony_ci	struct ib_pma_portcounters_ext *p =
16628c2ecf20Sopenharmony_ci		(struct ib_pma_portcounters_ext *)pmp->data;
16638c2ecf20Sopenharmony_ci	struct qib_ibport *ibp = to_iport(ibdev, port);
16648c2ecf20Sopenharmony_ci	struct qib_pportdata *ppd = ppd_from_ibp(ibp);
16658c2ecf20Sopenharmony_ci	u64 swords, rwords, spkts, rpkts, xwait;
16668c2ecf20Sopenharmony_ci	struct qib_pma_counters pma;
16678c2ecf20Sopenharmony_ci	u8 port_select = p->port_select;
16688c2ecf20Sopenharmony_ci
16698c2ecf20Sopenharmony_ci	memset(pmp->data, 0, sizeof(pmp->data));
16708c2ecf20Sopenharmony_ci
16718c2ecf20Sopenharmony_ci	p->port_select = port_select;
16728c2ecf20Sopenharmony_ci	if (pmp->mad_hdr.attr_mod != 0 || port_select != port) {
16738c2ecf20Sopenharmony_ci		pmp->mad_hdr.status |= IB_SMP_INVALID_FIELD;
16748c2ecf20Sopenharmony_ci		goto bail;
16758c2ecf20Sopenharmony_ci	}
16768c2ecf20Sopenharmony_ci
16778c2ecf20Sopenharmony_ci	qib_snapshot_counters(ppd, &swords, &rwords, &spkts, &rpkts, &xwait);
16788c2ecf20Sopenharmony_ci
16798c2ecf20Sopenharmony_ci	/* Adjust counters for any resets done. */
16808c2ecf20Sopenharmony_ci	swords -= ibp->z_port_xmit_data;
16818c2ecf20Sopenharmony_ci	rwords -= ibp->z_port_rcv_data;
16828c2ecf20Sopenharmony_ci	spkts -= ibp->z_port_xmit_packets;
16838c2ecf20Sopenharmony_ci	rpkts -= ibp->z_port_rcv_packets;
16848c2ecf20Sopenharmony_ci
16858c2ecf20Sopenharmony_ci	p->port_xmit_data = cpu_to_be64(swords);
16868c2ecf20Sopenharmony_ci	p->port_rcv_data = cpu_to_be64(rwords);
16878c2ecf20Sopenharmony_ci	p->port_xmit_packets = cpu_to_be64(spkts);
16888c2ecf20Sopenharmony_ci	p->port_rcv_packets = cpu_to_be64(rpkts);
16898c2ecf20Sopenharmony_ci
16908c2ecf20Sopenharmony_ci	qib_snapshot_pmacounters(ibp, &pma);
16918c2ecf20Sopenharmony_ci
16928c2ecf20Sopenharmony_ci	p->port_unicast_xmit_packets = cpu_to_be64(pma.n_unicast_xmit
16938c2ecf20Sopenharmony_ci		- ibp->z_unicast_xmit);
16948c2ecf20Sopenharmony_ci	p->port_unicast_rcv_packets = cpu_to_be64(pma.n_unicast_rcv
16958c2ecf20Sopenharmony_ci		- ibp->z_unicast_rcv);
16968c2ecf20Sopenharmony_ci	p->port_multicast_xmit_packets = cpu_to_be64(pma.n_multicast_xmit
16978c2ecf20Sopenharmony_ci		- ibp->z_multicast_xmit);
16988c2ecf20Sopenharmony_ci	p->port_multicast_rcv_packets = cpu_to_be64(pma.n_multicast_rcv
16998c2ecf20Sopenharmony_ci		- ibp->z_multicast_rcv);
17008c2ecf20Sopenharmony_ci
17018c2ecf20Sopenharmony_cibail:
17028c2ecf20Sopenharmony_ci	return reply((struct ib_smp *) pmp);
17038c2ecf20Sopenharmony_ci}
17048c2ecf20Sopenharmony_ci
17058c2ecf20Sopenharmony_cistatic int pma_set_portcounters(struct ib_pma_mad *pmp,
17068c2ecf20Sopenharmony_ci				struct ib_device *ibdev, u8 port)
17078c2ecf20Sopenharmony_ci{
17088c2ecf20Sopenharmony_ci	struct ib_pma_portcounters *p = (struct ib_pma_portcounters *)
17098c2ecf20Sopenharmony_ci		pmp->data;
17108c2ecf20Sopenharmony_ci	struct qib_ibport *ibp = to_iport(ibdev, port);
17118c2ecf20Sopenharmony_ci	struct qib_pportdata *ppd = ppd_from_ibp(ibp);
17128c2ecf20Sopenharmony_ci	struct qib_verbs_counters cntrs;
17138c2ecf20Sopenharmony_ci
17148c2ecf20Sopenharmony_ci	/*
17158c2ecf20Sopenharmony_ci	 * Since the HW doesn't support clearing counters, we save the
17168c2ecf20Sopenharmony_ci	 * current count and subtract it from future responses.
17178c2ecf20Sopenharmony_ci	 */
17188c2ecf20Sopenharmony_ci	qib_get_counters(ppd, &cntrs);
17198c2ecf20Sopenharmony_ci
17208c2ecf20Sopenharmony_ci	if (p->counter_select & IB_PMA_SEL_SYMBOL_ERROR)
17218c2ecf20Sopenharmony_ci		ibp->z_symbol_error_counter = cntrs.symbol_error_counter;
17228c2ecf20Sopenharmony_ci
17238c2ecf20Sopenharmony_ci	if (p->counter_select & IB_PMA_SEL_LINK_ERROR_RECOVERY)
17248c2ecf20Sopenharmony_ci		ibp->z_link_error_recovery_counter =
17258c2ecf20Sopenharmony_ci			cntrs.link_error_recovery_counter;
17268c2ecf20Sopenharmony_ci
17278c2ecf20Sopenharmony_ci	if (p->counter_select & IB_PMA_SEL_LINK_DOWNED)
17288c2ecf20Sopenharmony_ci		ibp->z_link_downed_counter = cntrs.link_downed_counter;
17298c2ecf20Sopenharmony_ci
17308c2ecf20Sopenharmony_ci	if (p->counter_select & IB_PMA_SEL_PORT_RCV_ERRORS)
17318c2ecf20Sopenharmony_ci		ibp->z_port_rcv_errors = cntrs.port_rcv_errors;
17328c2ecf20Sopenharmony_ci
17338c2ecf20Sopenharmony_ci	if (p->counter_select & IB_PMA_SEL_PORT_RCV_REMPHYS_ERRORS)
17348c2ecf20Sopenharmony_ci		ibp->z_port_rcv_remphys_errors =
17358c2ecf20Sopenharmony_ci			cntrs.port_rcv_remphys_errors;
17368c2ecf20Sopenharmony_ci
17378c2ecf20Sopenharmony_ci	if (p->counter_select & IB_PMA_SEL_PORT_XMIT_DISCARDS)
17388c2ecf20Sopenharmony_ci		ibp->z_port_xmit_discards = cntrs.port_xmit_discards;
17398c2ecf20Sopenharmony_ci
17408c2ecf20Sopenharmony_ci	if (p->counter_select & IB_PMA_SEL_LOCAL_LINK_INTEGRITY_ERRORS)
17418c2ecf20Sopenharmony_ci		ibp->z_local_link_integrity_errors =
17428c2ecf20Sopenharmony_ci			cntrs.local_link_integrity_errors;
17438c2ecf20Sopenharmony_ci
17448c2ecf20Sopenharmony_ci	if (p->counter_select & IB_PMA_SEL_EXCESSIVE_BUFFER_OVERRUNS)
17458c2ecf20Sopenharmony_ci		ibp->z_excessive_buffer_overrun_errors =
17468c2ecf20Sopenharmony_ci			cntrs.excessive_buffer_overrun_errors;
17478c2ecf20Sopenharmony_ci
17488c2ecf20Sopenharmony_ci	if (p->counter_select & IB_PMA_SEL_PORT_VL15_DROPPED) {
17498c2ecf20Sopenharmony_ci		ibp->rvp.n_vl15_dropped = 0;
17508c2ecf20Sopenharmony_ci		ibp->z_vl15_dropped = cntrs.vl15_dropped;
17518c2ecf20Sopenharmony_ci	}
17528c2ecf20Sopenharmony_ci
17538c2ecf20Sopenharmony_ci	if (p->counter_select & IB_PMA_SEL_PORT_XMIT_DATA)
17548c2ecf20Sopenharmony_ci		ibp->z_port_xmit_data = cntrs.port_xmit_data;
17558c2ecf20Sopenharmony_ci
17568c2ecf20Sopenharmony_ci	if (p->counter_select & IB_PMA_SEL_PORT_RCV_DATA)
17578c2ecf20Sopenharmony_ci		ibp->z_port_rcv_data = cntrs.port_rcv_data;
17588c2ecf20Sopenharmony_ci
17598c2ecf20Sopenharmony_ci	if (p->counter_select & IB_PMA_SEL_PORT_XMIT_PACKETS)
17608c2ecf20Sopenharmony_ci		ibp->z_port_xmit_packets = cntrs.port_xmit_packets;
17618c2ecf20Sopenharmony_ci
17628c2ecf20Sopenharmony_ci	if (p->counter_select & IB_PMA_SEL_PORT_RCV_PACKETS)
17638c2ecf20Sopenharmony_ci		ibp->z_port_rcv_packets = cntrs.port_rcv_packets;
17648c2ecf20Sopenharmony_ci
17658c2ecf20Sopenharmony_ci	return pma_get_portcounters(pmp, ibdev, port);
17668c2ecf20Sopenharmony_ci}
17678c2ecf20Sopenharmony_ci
17688c2ecf20Sopenharmony_cistatic int pma_set_portcounters_cong(struct ib_pma_mad *pmp,
17698c2ecf20Sopenharmony_ci				     struct ib_device *ibdev, u8 port)
17708c2ecf20Sopenharmony_ci{
17718c2ecf20Sopenharmony_ci	struct qib_ibport *ibp = to_iport(ibdev, port);
17728c2ecf20Sopenharmony_ci	struct qib_pportdata *ppd = ppd_from_ibp(ibp);
17738c2ecf20Sopenharmony_ci	struct qib_devdata *dd = dd_from_ppd(ppd);
17748c2ecf20Sopenharmony_ci	struct qib_verbs_counters cntrs;
17758c2ecf20Sopenharmony_ci	u32 counter_select = (be32_to_cpu(pmp->mad_hdr.attr_mod) >> 24) & 0xFF;
17768c2ecf20Sopenharmony_ci	int ret = 0;
17778c2ecf20Sopenharmony_ci	unsigned long flags;
17788c2ecf20Sopenharmony_ci
17798c2ecf20Sopenharmony_ci	qib_get_counters(ppd, &cntrs);
17808c2ecf20Sopenharmony_ci	/* Get counter values before we save them */
17818c2ecf20Sopenharmony_ci	ret = pma_get_portcounters_cong(pmp, ibdev, port);
17828c2ecf20Sopenharmony_ci
17838c2ecf20Sopenharmony_ci	if (counter_select & IB_PMA_SEL_CONG_XMIT) {
17848c2ecf20Sopenharmony_ci		spin_lock_irqsave(&ppd->ibport_data.rvp.lock, flags);
17858c2ecf20Sopenharmony_ci		ppd->cong_stats.counter = 0;
17868c2ecf20Sopenharmony_ci		dd->f_set_cntr_sample(ppd, QIB_CONG_TIMER_PSINTERVAL,
17878c2ecf20Sopenharmony_ci				      0x0);
17888c2ecf20Sopenharmony_ci		spin_unlock_irqrestore(&ppd->ibport_data.rvp.lock, flags);
17898c2ecf20Sopenharmony_ci	}
17908c2ecf20Sopenharmony_ci	if (counter_select & IB_PMA_SEL_CONG_PORT_DATA) {
17918c2ecf20Sopenharmony_ci		ibp->z_port_xmit_data = cntrs.port_xmit_data;
17928c2ecf20Sopenharmony_ci		ibp->z_port_rcv_data = cntrs.port_rcv_data;
17938c2ecf20Sopenharmony_ci		ibp->z_port_xmit_packets = cntrs.port_xmit_packets;
17948c2ecf20Sopenharmony_ci		ibp->z_port_rcv_packets = cntrs.port_rcv_packets;
17958c2ecf20Sopenharmony_ci	}
17968c2ecf20Sopenharmony_ci	if (counter_select & IB_PMA_SEL_CONG_ALL) {
17978c2ecf20Sopenharmony_ci		ibp->z_symbol_error_counter =
17988c2ecf20Sopenharmony_ci			cntrs.symbol_error_counter;
17998c2ecf20Sopenharmony_ci		ibp->z_link_error_recovery_counter =
18008c2ecf20Sopenharmony_ci			cntrs.link_error_recovery_counter;
18018c2ecf20Sopenharmony_ci		ibp->z_link_downed_counter =
18028c2ecf20Sopenharmony_ci			cntrs.link_downed_counter;
18038c2ecf20Sopenharmony_ci		ibp->z_port_rcv_errors = cntrs.port_rcv_errors;
18048c2ecf20Sopenharmony_ci		ibp->z_port_rcv_remphys_errors =
18058c2ecf20Sopenharmony_ci			cntrs.port_rcv_remphys_errors;
18068c2ecf20Sopenharmony_ci		ibp->z_port_xmit_discards =
18078c2ecf20Sopenharmony_ci			cntrs.port_xmit_discards;
18088c2ecf20Sopenharmony_ci		ibp->z_local_link_integrity_errors =
18098c2ecf20Sopenharmony_ci			cntrs.local_link_integrity_errors;
18108c2ecf20Sopenharmony_ci		ibp->z_excessive_buffer_overrun_errors =
18118c2ecf20Sopenharmony_ci			cntrs.excessive_buffer_overrun_errors;
18128c2ecf20Sopenharmony_ci		ibp->rvp.n_vl15_dropped = 0;
18138c2ecf20Sopenharmony_ci		ibp->z_vl15_dropped = cntrs.vl15_dropped;
18148c2ecf20Sopenharmony_ci	}
18158c2ecf20Sopenharmony_ci
18168c2ecf20Sopenharmony_ci	return ret;
18178c2ecf20Sopenharmony_ci}
18188c2ecf20Sopenharmony_ci
18198c2ecf20Sopenharmony_cistatic int pma_set_portcounters_ext(struct ib_pma_mad *pmp,
18208c2ecf20Sopenharmony_ci				    struct ib_device *ibdev, u8 port)
18218c2ecf20Sopenharmony_ci{
18228c2ecf20Sopenharmony_ci	struct ib_pma_portcounters *p = (struct ib_pma_portcounters *)
18238c2ecf20Sopenharmony_ci		pmp->data;
18248c2ecf20Sopenharmony_ci	struct qib_ibport *ibp = to_iport(ibdev, port);
18258c2ecf20Sopenharmony_ci	struct qib_pportdata *ppd = ppd_from_ibp(ibp);
18268c2ecf20Sopenharmony_ci	u64 swords, rwords, spkts, rpkts, xwait;
18278c2ecf20Sopenharmony_ci	struct qib_pma_counters pma;
18288c2ecf20Sopenharmony_ci
18298c2ecf20Sopenharmony_ci	qib_snapshot_counters(ppd, &swords, &rwords, &spkts, &rpkts, &xwait);
18308c2ecf20Sopenharmony_ci
18318c2ecf20Sopenharmony_ci	if (p->counter_select & IB_PMA_SELX_PORT_XMIT_DATA)
18328c2ecf20Sopenharmony_ci		ibp->z_port_xmit_data = swords;
18338c2ecf20Sopenharmony_ci
18348c2ecf20Sopenharmony_ci	if (p->counter_select & IB_PMA_SELX_PORT_RCV_DATA)
18358c2ecf20Sopenharmony_ci		ibp->z_port_rcv_data = rwords;
18368c2ecf20Sopenharmony_ci
18378c2ecf20Sopenharmony_ci	if (p->counter_select & IB_PMA_SELX_PORT_XMIT_PACKETS)
18388c2ecf20Sopenharmony_ci		ibp->z_port_xmit_packets = spkts;
18398c2ecf20Sopenharmony_ci
18408c2ecf20Sopenharmony_ci	if (p->counter_select & IB_PMA_SELX_PORT_RCV_PACKETS)
18418c2ecf20Sopenharmony_ci		ibp->z_port_rcv_packets = rpkts;
18428c2ecf20Sopenharmony_ci
18438c2ecf20Sopenharmony_ci	qib_snapshot_pmacounters(ibp, &pma);
18448c2ecf20Sopenharmony_ci
18458c2ecf20Sopenharmony_ci	if (p->counter_select & IB_PMA_SELX_PORT_UNI_XMIT_PACKETS)
18468c2ecf20Sopenharmony_ci		ibp->z_unicast_xmit = pma.n_unicast_xmit;
18478c2ecf20Sopenharmony_ci
18488c2ecf20Sopenharmony_ci	if (p->counter_select & IB_PMA_SELX_PORT_UNI_RCV_PACKETS)
18498c2ecf20Sopenharmony_ci		ibp->z_unicast_rcv = pma.n_unicast_rcv;
18508c2ecf20Sopenharmony_ci
18518c2ecf20Sopenharmony_ci	if (p->counter_select & IB_PMA_SELX_PORT_MULTI_XMIT_PACKETS)
18528c2ecf20Sopenharmony_ci		ibp->z_multicast_xmit = pma.n_multicast_xmit;
18538c2ecf20Sopenharmony_ci
18548c2ecf20Sopenharmony_ci	if (p->counter_select & IB_PMA_SELX_PORT_MULTI_RCV_PACKETS)
18558c2ecf20Sopenharmony_ci		ibp->z_multicast_rcv = pma.n_multicast_rcv;
18568c2ecf20Sopenharmony_ci
18578c2ecf20Sopenharmony_ci	return pma_get_portcounters_ext(pmp, ibdev, port);
18588c2ecf20Sopenharmony_ci}
18598c2ecf20Sopenharmony_ci
18608c2ecf20Sopenharmony_cistatic int process_subn(struct ib_device *ibdev, int mad_flags,
18618c2ecf20Sopenharmony_ci			u8 port, const struct ib_mad *in_mad,
18628c2ecf20Sopenharmony_ci			struct ib_mad *out_mad)
18638c2ecf20Sopenharmony_ci{
18648c2ecf20Sopenharmony_ci	struct ib_smp *smp = (struct ib_smp *)out_mad;
18658c2ecf20Sopenharmony_ci	struct qib_ibport *ibp = to_iport(ibdev, port);
18668c2ecf20Sopenharmony_ci	struct qib_pportdata *ppd = ppd_from_ibp(ibp);
18678c2ecf20Sopenharmony_ci	int ret;
18688c2ecf20Sopenharmony_ci
18698c2ecf20Sopenharmony_ci	*out_mad = *in_mad;
18708c2ecf20Sopenharmony_ci	if (smp->class_version != 1) {
18718c2ecf20Sopenharmony_ci		smp->status |= IB_SMP_UNSUP_VERSION;
18728c2ecf20Sopenharmony_ci		ret = reply(smp);
18738c2ecf20Sopenharmony_ci		goto bail;
18748c2ecf20Sopenharmony_ci	}
18758c2ecf20Sopenharmony_ci
18768c2ecf20Sopenharmony_ci	ret = check_mkey(ibp, smp, mad_flags);
18778c2ecf20Sopenharmony_ci	if (ret) {
18788c2ecf20Sopenharmony_ci		u32 port_num = be32_to_cpu(smp->attr_mod);
18798c2ecf20Sopenharmony_ci
18808c2ecf20Sopenharmony_ci		/*
18818c2ecf20Sopenharmony_ci		 * If this is a get/set portinfo, we already check the
18828c2ecf20Sopenharmony_ci		 * M_Key if the MAD is for another port and the M_Key
18838c2ecf20Sopenharmony_ci		 * is OK on the receiving port. This check is needed
18848c2ecf20Sopenharmony_ci		 * to increment the error counters when the M_Key
18858c2ecf20Sopenharmony_ci		 * fails to match on *both* ports.
18868c2ecf20Sopenharmony_ci		 */
18878c2ecf20Sopenharmony_ci		if (in_mad->mad_hdr.attr_id == IB_SMP_ATTR_PORT_INFO &&
18888c2ecf20Sopenharmony_ci		    (smp->method == IB_MGMT_METHOD_GET ||
18898c2ecf20Sopenharmony_ci		     smp->method == IB_MGMT_METHOD_SET) &&
18908c2ecf20Sopenharmony_ci		    port_num && port_num <= ibdev->phys_port_cnt &&
18918c2ecf20Sopenharmony_ci		    port != port_num)
18928c2ecf20Sopenharmony_ci			(void) check_mkey(to_iport(ibdev, port_num), smp, 0);
18938c2ecf20Sopenharmony_ci		ret = IB_MAD_RESULT_FAILURE;
18948c2ecf20Sopenharmony_ci		goto bail;
18958c2ecf20Sopenharmony_ci	}
18968c2ecf20Sopenharmony_ci
18978c2ecf20Sopenharmony_ci	switch (smp->method) {
18988c2ecf20Sopenharmony_ci	case IB_MGMT_METHOD_GET:
18998c2ecf20Sopenharmony_ci		switch (smp->attr_id) {
19008c2ecf20Sopenharmony_ci		case IB_SMP_ATTR_NODE_DESC:
19018c2ecf20Sopenharmony_ci			ret = subn_get_nodedescription(smp, ibdev);
19028c2ecf20Sopenharmony_ci			goto bail;
19038c2ecf20Sopenharmony_ci		case IB_SMP_ATTR_NODE_INFO:
19048c2ecf20Sopenharmony_ci			ret = subn_get_nodeinfo(smp, ibdev, port);
19058c2ecf20Sopenharmony_ci			goto bail;
19068c2ecf20Sopenharmony_ci		case IB_SMP_ATTR_GUID_INFO:
19078c2ecf20Sopenharmony_ci			ret = subn_get_guidinfo(smp, ibdev, port);
19088c2ecf20Sopenharmony_ci			goto bail;
19098c2ecf20Sopenharmony_ci		case IB_SMP_ATTR_PORT_INFO:
19108c2ecf20Sopenharmony_ci			ret = subn_get_portinfo(smp, ibdev, port);
19118c2ecf20Sopenharmony_ci			goto bail;
19128c2ecf20Sopenharmony_ci		case IB_SMP_ATTR_PKEY_TABLE:
19138c2ecf20Sopenharmony_ci			ret = subn_get_pkeytable(smp, ibdev, port);
19148c2ecf20Sopenharmony_ci			goto bail;
19158c2ecf20Sopenharmony_ci		case IB_SMP_ATTR_SL_TO_VL_TABLE:
19168c2ecf20Sopenharmony_ci			ret = subn_get_sl_to_vl(smp, ibdev, port);
19178c2ecf20Sopenharmony_ci			goto bail;
19188c2ecf20Sopenharmony_ci		case IB_SMP_ATTR_VL_ARB_TABLE:
19198c2ecf20Sopenharmony_ci			ret = subn_get_vl_arb(smp, ibdev, port);
19208c2ecf20Sopenharmony_ci			goto bail;
19218c2ecf20Sopenharmony_ci		case IB_SMP_ATTR_SM_INFO:
19228c2ecf20Sopenharmony_ci			if (ibp->rvp.port_cap_flags & IB_PORT_SM_DISABLED) {
19238c2ecf20Sopenharmony_ci				ret = IB_MAD_RESULT_SUCCESS |
19248c2ecf20Sopenharmony_ci					IB_MAD_RESULT_CONSUMED;
19258c2ecf20Sopenharmony_ci				goto bail;
19268c2ecf20Sopenharmony_ci			}
19278c2ecf20Sopenharmony_ci			if (ibp->rvp.port_cap_flags & IB_PORT_SM) {
19288c2ecf20Sopenharmony_ci				ret = IB_MAD_RESULT_SUCCESS;
19298c2ecf20Sopenharmony_ci				goto bail;
19308c2ecf20Sopenharmony_ci			}
19318c2ecf20Sopenharmony_ci			fallthrough;
19328c2ecf20Sopenharmony_ci		default:
19338c2ecf20Sopenharmony_ci			smp->status |= IB_SMP_UNSUP_METH_ATTR;
19348c2ecf20Sopenharmony_ci			ret = reply(smp);
19358c2ecf20Sopenharmony_ci			goto bail;
19368c2ecf20Sopenharmony_ci		}
19378c2ecf20Sopenharmony_ci
19388c2ecf20Sopenharmony_ci	case IB_MGMT_METHOD_SET:
19398c2ecf20Sopenharmony_ci		switch (smp->attr_id) {
19408c2ecf20Sopenharmony_ci		case IB_SMP_ATTR_GUID_INFO:
19418c2ecf20Sopenharmony_ci			ret = subn_set_guidinfo(smp, ibdev, port);
19428c2ecf20Sopenharmony_ci			goto bail;
19438c2ecf20Sopenharmony_ci		case IB_SMP_ATTR_PORT_INFO:
19448c2ecf20Sopenharmony_ci			ret = subn_set_portinfo(smp, ibdev, port);
19458c2ecf20Sopenharmony_ci			goto bail;
19468c2ecf20Sopenharmony_ci		case IB_SMP_ATTR_PKEY_TABLE:
19478c2ecf20Sopenharmony_ci			ret = subn_set_pkeytable(smp, ibdev, port);
19488c2ecf20Sopenharmony_ci			goto bail;
19498c2ecf20Sopenharmony_ci		case IB_SMP_ATTR_SL_TO_VL_TABLE:
19508c2ecf20Sopenharmony_ci			ret = subn_set_sl_to_vl(smp, ibdev, port);
19518c2ecf20Sopenharmony_ci			goto bail;
19528c2ecf20Sopenharmony_ci		case IB_SMP_ATTR_VL_ARB_TABLE:
19538c2ecf20Sopenharmony_ci			ret = subn_set_vl_arb(smp, ibdev, port);
19548c2ecf20Sopenharmony_ci			goto bail;
19558c2ecf20Sopenharmony_ci		case IB_SMP_ATTR_SM_INFO:
19568c2ecf20Sopenharmony_ci			if (ibp->rvp.port_cap_flags & IB_PORT_SM_DISABLED) {
19578c2ecf20Sopenharmony_ci				ret = IB_MAD_RESULT_SUCCESS |
19588c2ecf20Sopenharmony_ci					IB_MAD_RESULT_CONSUMED;
19598c2ecf20Sopenharmony_ci				goto bail;
19608c2ecf20Sopenharmony_ci			}
19618c2ecf20Sopenharmony_ci			if (ibp->rvp.port_cap_flags & IB_PORT_SM) {
19628c2ecf20Sopenharmony_ci				ret = IB_MAD_RESULT_SUCCESS;
19638c2ecf20Sopenharmony_ci				goto bail;
19648c2ecf20Sopenharmony_ci			}
19658c2ecf20Sopenharmony_ci			fallthrough;
19668c2ecf20Sopenharmony_ci		default:
19678c2ecf20Sopenharmony_ci			smp->status |= IB_SMP_UNSUP_METH_ATTR;
19688c2ecf20Sopenharmony_ci			ret = reply(smp);
19698c2ecf20Sopenharmony_ci			goto bail;
19708c2ecf20Sopenharmony_ci		}
19718c2ecf20Sopenharmony_ci
19728c2ecf20Sopenharmony_ci	case IB_MGMT_METHOD_TRAP_REPRESS:
19738c2ecf20Sopenharmony_ci		if (smp->attr_id == IB_SMP_ATTR_NOTICE)
19748c2ecf20Sopenharmony_ci			ret = subn_trap_repress(smp, ibdev, port);
19758c2ecf20Sopenharmony_ci		else {
19768c2ecf20Sopenharmony_ci			smp->status |= IB_SMP_UNSUP_METH_ATTR;
19778c2ecf20Sopenharmony_ci			ret = reply(smp);
19788c2ecf20Sopenharmony_ci		}
19798c2ecf20Sopenharmony_ci		goto bail;
19808c2ecf20Sopenharmony_ci
19818c2ecf20Sopenharmony_ci	case IB_MGMT_METHOD_TRAP:
19828c2ecf20Sopenharmony_ci	case IB_MGMT_METHOD_REPORT:
19838c2ecf20Sopenharmony_ci	case IB_MGMT_METHOD_REPORT_RESP:
19848c2ecf20Sopenharmony_ci	case IB_MGMT_METHOD_GET_RESP:
19858c2ecf20Sopenharmony_ci		/*
19868c2ecf20Sopenharmony_ci		 * The ib_mad module will call us to process responses
19878c2ecf20Sopenharmony_ci		 * before checking for other consumers.
19888c2ecf20Sopenharmony_ci		 * Just tell the caller to process it normally.
19898c2ecf20Sopenharmony_ci		 */
19908c2ecf20Sopenharmony_ci		ret = IB_MAD_RESULT_SUCCESS;
19918c2ecf20Sopenharmony_ci		goto bail;
19928c2ecf20Sopenharmony_ci
19938c2ecf20Sopenharmony_ci	case IB_MGMT_METHOD_SEND:
19948c2ecf20Sopenharmony_ci		if (ib_get_smp_direction(smp) &&
19958c2ecf20Sopenharmony_ci		    smp->attr_id == QIB_VENDOR_IPG) {
19968c2ecf20Sopenharmony_ci			ppd->dd->f_set_ib_cfg(ppd, QIB_IB_CFG_PORT,
19978c2ecf20Sopenharmony_ci					      smp->data[0]);
19988c2ecf20Sopenharmony_ci			ret = IB_MAD_RESULT_SUCCESS | IB_MAD_RESULT_CONSUMED;
19998c2ecf20Sopenharmony_ci		} else
20008c2ecf20Sopenharmony_ci			ret = IB_MAD_RESULT_SUCCESS;
20018c2ecf20Sopenharmony_ci		goto bail;
20028c2ecf20Sopenharmony_ci
20038c2ecf20Sopenharmony_ci	default:
20048c2ecf20Sopenharmony_ci		smp->status |= IB_SMP_UNSUP_METHOD;
20058c2ecf20Sopenharmony_ci		ret = reply(smp);
20068c2ecf20Sopenharmony_ci	}
20078c2ecf20Sopenharmony_ci
20088c2ecf20Sopenharmony_cibail:
20098c2ecf20Sopenharmony_ci	return ret;
20108c2ecf20Sopenharmony_ci}
20118c2ecf20Sopenharmony_ci
20128c2ecf20Sopenharmony_cistatic int process_perf(struct ib_device *ibdev, u8 port,
20138c2ecf20Sopenharmony_ci			const struct ib_mad *in_mad,
20148c2ecf20Sopenharmony_ci			struct ib_mad *out_mad)
20158c2ecf20Sopenharmony_ci{
20168c2ecf20Sopenharmony_ci	struct ib_pma_mad *pmp = (struct ib_pma_mad *)out_mad;
20178c2ecf20Sopenharmony_ci	int ret;
20188c2ecf20Sopenharmony_ci
20198c2ecf20Sopenharmony_ci	*out_mad = *in_mad;
20208c2ecf20Sopenharmony_ci	if (pmp->mad_hdr.class_version != 1) {
20218c2ecf20Sopenharmony_ci		pmp->mad_hdr.status |= IB_SMP_UNSUP_VERSION;
20228c2ecf20Sopenharmony_ci		ret = reply((struct ib_smp *) pmp);
20238c2ecf20Sopenharmony_ci		goto bail;
20248c2ecf20Sopenharmony_ci	}
20258c2ecf20Sopenharmony_ci
20268c2ecf20Sopenharmony_ci	switch (pmp->mad_hdr.method) {
20278c2ecf20Sopenharmony_ci	case IB_MGMT_METHOD_GET:
20288c2ecf20Sopenharmony_ci		switch (pmp->mad_hdr.attr_id) {
20298c2ecf20Sopenharmony_ci		case IB_PMA_CLASS_PORT_INFO:
20308c2ecf20Sopenharmony_ci			ret = pma_get_classportinfo(pmp, ibdev);
20318c2ecf20Sopenharmony_ci			goto bail;
20328c2ecf20Sopenharmony_ci		case IB_PMA_PORT_SAMPLES_CONTROL:
20338c2ecf20Sopenharmony_ci			ret = pma_get_portsamplescontrol(pmp, ibdev, port);
20348c2ecf20Sopenharmony_ci			goto bail;
20358c2ecf20Sopenharmony_ci		case IB_PMA_PORT_SAMPLES_RESULT:
20368c2ecf20Sopenharmony_ci			ret = pma_get_portsamplesresult(pmp, ibdev, port);
20378c2ecf20Sopenharmony_ci			goto bail;
20388c2ecf20Sopenharmony_ci		case IB_PMA_PORT_SAMPLES_RESULT_EXT:
20398c2ecf20Sopenharmony_ci			ret = pma_get_portsamplesresult_ext(pmp, ibdev, port);
20408c2ecf20Sopenharmony_ci			goto bail;
20418c2ecf20Sopenharmony_ci		case IB_PMA_PORT_COUNTERS:
20428c2ecf20Sopenharmony_ci			ret = pma_get_portcounters(pmp, ibdev, port);
20438c2ecf20Sopenharmony_ci			goto bail;
20448c2ecf20Sopenharmony_ci		case IB_PMA_PORT_COUNTERS_EXT:
20458c2ecf20Sopenharmony_ci			ret = pma_get_portcounters_ext(pmp, ibdev, port);
20468c2ecf20Sopenharmony_ci			goto bail;
20478c2ecf20Sopenharmony_ci		case IB_PMA_PORT_COUNTERS_CONG:
20488c2ecf20Sopenharmony_ci			ret = pma_get_portcounters_cong(pmp, ibdev, port);
20498c2ecf20Sopenharmony_ci			goto bail;
20508c2ecf20Sopenharmony_ci		default:
20518c2ecf20Sopenharmony_ci			pmp->mad_hdr.status |= IB_SMP_UNSUP_METH_ATTR;
20528c2ecf20Sopenharmony_ci			ret = reply((struct ib_smp *) pmp);
20538c2ecf20Sopenharmony_ci			goto bail;
20548c2ecf20Sopenharmony_ci		}
20558c2ecf20Sopenharmony_ci
20568c2ecf20Sopenharmony_ci	case IB_MGMT_METHOD_SET:
20578c2ecf20Sopenharmony_ci		switch (pmp->mad_hdr.attr_id) {
20588c2ecf20Sopenharmony_ci		case IB_PMA_PORT_SAMPLES_CONTROL:
20598c2ecf20Sopenharmony_ci			ret = pma_set_portsamplescontrol(pmp, ibdev, port);
20608c2ecf20Sopenharmony_ci			goto bail;
20618c2ecf20Sopenharmony_ci		case IB_PMA_PORT_COUNTERS:
20628c2ecf20Sopenharmony_ci			ret = pma_set_portcounters(pmp, ibdev, port);
20638c2ecf20Sopenharmony_ci			goto bail;
20648c2ecf20Sopenharmony_ci		case IB_PMA_PORT_COUNTERS_EXT:
20658c2ecf20Sopenharmony_ci			ret = pma_set_portcounters_ext(pmp, ibdev, port);
20668c2ecf20Sopenharmony_ci			goto bail;
20678c2ecf20Sopenharmony_ci		case IB_PMA_PORT_COUNTERS_CONG:
20688c2ecf20Sopenharmony_ci			ret = pma_set_portcounters_cong(pmp, ibdev, port);
20698c2ecf20Sopenharmony_ci			goto bail;
20708c2ecf20Sopenharmony_ci		default:
20718c2ecf20Sopenharmony_ci			pmp->mad_hdr.status |= IB_SMP_UNSUP_METH_ATTR;
20728c2ecf20Sopenharmony_ci			ret = reply((struct ib_smp *) pmp);
20738c2ecf20Sopenharmony_ci			goto bail;
20748c2ecf20Sopenharmony_ci		}
20758c2ecf20Sopenharmony_ci
20768c2ecf20Sopenharmony_ci	case IB_MGMT_METHOD_TRAP:
20778c2ecf20Sopenharmony_ci	case IB_MGMT_METHOD_GET_RESP:
20788c2ecf20Sopenharmony_ci		/*
20798c2ecf20Sopenharmony_ci		 * The ib_mad module will call us to process responses
20808c2ecf20Sopenharmony_ci		 * before checking for other consumers.
20818c2ecf20Sopenharmony_ci		 * Just tell the caller to process it normally.
20828c2ecf20Sopenharmony_ci		 */
20838c2ecf20Sopenharmony_ci		ret = IB_MAD_RESULT_SUCCESS;
20848c2ecf20Sopenharmony_ci		goto bail;
20858c2ecf20Sopenharmony_ci
20868c2ecf20Sopenharmony_ci	default:
20878c2ecf20Sopenharmony_ci		pmp->mad_hdr.status |= IB_SMP_UNSUP_METHOD;
20888c2ecf20Sopenharmony_ci		ret = reply((struct ib_smp *) pmp);
20898c2ecf20Sopenharmony_ci	}
20908c2ecf20Sopenharmony_ci
20918c2ecf20Sopenharmony_cibail:
20928c2ecf20Sopenharmony_ci	return ret;
20938c2ecf20Sopenharmony_ci}
20948c2ecf20Sopenharmony_ci
20958c2ecf20Sopenharmony_cistatic int cc_get_classportinfo(struct ib_cc_mad *ccp,
20968c2ecf20Sopenharmony_ci				struct ib_device *ibdev)
20978c2ecf20Sopenharmony_ci{
20988c2ecf20Sopenharmony_ci	struct ib_cc_classportinfo_attr *p =
20998c2ecf20Sopenharmony_ci		(struct ib_cc_classportinfo_attr *)ccp->mgmt_data;
21008c2ecf20Sopenharmony_ci
21018c2ecf20Sopenharmony_ci	p->base_version = 1;
21028c2ecf20Sopenharmony_ci	p->class_version = 1;
21038c2ecf20Sopenharmony_ci	p->cap_mask = 0;
21048c2ecf20Sopenharmony_ci
21058c2ecf20Sopenharmony_ci	/*
21068c2ecf20Sopenharmony_ci	 * Expected response time is 4.096 usec. * 2^18 == 1.073741824 sec.
21078c2ecf20Sopenharmony_ci	 */
21088c2ecf20Sopenharmony_ci	p->resp_time_value = 18;
21098c2ecf20Sopenharmony_ci
21108c2ecf20Sopenharmony_ci	return reply((struct ib_smp *) ccp);
21118c2ecf20Sopenharmony_ci}
21128c2ecf20Sopenharmony_ci
21138c2ecf20Sopenharmony_cistatic int cc_get_congestion_info(struct ib_cc_mad *ccp,
21148c2ecf20Sopenharmony_ci				struct ib_device *ibdev, u8 port)
21158c2ecf20Sopenharmony_ci{
21168c2ecf20Sopenharmony_ci	struct ib_cc_info_attr *p =
21178c2ecf20Sopenharmony_ci		(struct ib_cc_info_attr *)ccp->mgmt_data;
21188c2ecf20Sopenharmony_ci	struct qib_ibport *ibp = to_iport(ibdev, port);
21198c2ecf20Sopenharmony_ci	struct qib_pportdata *ppd = ppd_from_ibp(ibp);
21208c2ecf20Sopenharmony_ci
21218c2ecf20Sopenharmony_ci	p->congestion_info = 0;
21228c2ecf20Sopenharmony_ci	p->control_table_cap = ppd->cc_max_table_entries;
21238c2ecf20Sopenharmony_ci
21248c2ecf20Sopenharmony_ci	return reply((struct ib_smp *) ccp);
21258c2ecf20Sopenharmony_ci}
21268c2ecf20Sopenharmony_ci
21278c2ecf20Sopenharmony_cistatic int cc_get_congestion_setting(struct ib_cc_mad *ccp,
21288c2ecf20Sopenharmony_ci				struct ib_device *ibdev, u8 port)
21298c2ecf20Sopenharmony_ci{
21308c2ecf20Sopenharmony_ci	int i;
21318c2ecf20Sopenharmony_ci	struct ib_cc_congestion_setting_attr *p =
21328c2ecf20Sopenharmony_ci		(struct ib_cc_congestion_setting_attr *)ccp->mgmt_data;
21338c2ecf20Sopenharmony_ci	struct qib_ibport *ibp = to_iport(ibdev, port);
21348c2ecf20Sopenharmony_ci	struct qib_pportdata *ppd = ppd_from_ibp(ibp);
21358c2ecf20Sopenharmony_ci	struct ib_cc_congestion_entry_shadow *entries;
21368c2ecf20Sopenharmony_ci
21378c2ecf20Sopenharmony_ci	spin_lock(&ppd->cc_shadow_lock);
21388c2ecf20Sopenharmony_ci
21398c2ecf20Sopenharmony_ci	entries = ppd->congestion_entries_shadow->entries;
21408c2ecf20Sopenharmony_ci	p->port_control = cpu_to_be16(
21418c2ecf20Sopenharmony_ci		ppd->congestion_entries_shadow->port_control);
21428c2ecf20Sopenharmony_ci	p->control_map = cpu_to_be16(
21438c2ecf20Sopenharmony_ci		ppd->congestion_entries_shadow->control_map);
21448c2ecf20Sopenharmony_ci	for (i = 0; i < IB_CC_CCS_ENTRIES; i++) {
21458c2ecf20Sopenharmony_ci		p->entries[i].ccti_increase = entries[i].ccti_increase;
21468c2ecf20Sopenharmony_ci		p->entries[i].ccti_timer = cpu_to_be16(entries[i].ccti_timer);
21478c2ecf20Sopenharmony_ci		p->entries[i].trigger_threshold = entries[i].trigger_threshold;
21488c2ecf20Sopenharmony_ci		p->entries[i].ccti_min = entries[i].ccti_min;
21498c2ecf20Sopenharmony_ci	}
21508c2ecf20Sopenharmony_ci
21518c2ecf20Sopenharmony_ci	spin_unlock(&ppd->cc_shadow_lock);
21528c2ecf20Sopenharmony_ci
21538c2ecf20Sopenharmony_ci	return reply((struct ib_smp *) ccp);
21548c2ecf20Sopenharmony_ci}
21558c2ecf20Sopenharmony_ci
21568c2ecf20Sopenharmony_cistatic int cc_get_congestion_control_table(struct ib_cc_mad *ccp,
21578c2ecf20Sopenharmony_ci				struct ib_device *ibdev, u8 port)
21588c2ecf20Sopenharmony_ci{
21598c2ecf20Sopenharmony_ci	struct ib_cc_table_attr *p =
21608c2ecf20Sopenharmony_ci		(struct ib_cc_table_attr *)ccp->mgmt_data;
21618c2ecf20Sopenharmony_ci	struct qib_ibport *ibp = to_iport(ibdev, port);
21628c2ecf20Sopenharmony_ci	struct qib_pportdata *ppd = ppd_from_ibp(ibp);
21638c2ecf20Sopenharmony_ci	u32 cct_block_index = be32_to_cpu(ccp->attr_mod);
21648c2ecf20Sopenharmony_ci	u32 max_cct_block;
21658c2ecf20Sopenharmony_ci	u32 cct_entry;
21668c2ecf20Sopenharmony_ci	struct ib_cc_table_entry_shadow *entries;
21678c2ecf20Sopenharmony_ci	int i;
21688c2ecf20Sopenharmony_ci
21698c2ecf20Sopenharmony_ci	/* Is the table index more than what is supported? */
21708c2ecf20Sopenharmony_ci	if (cct_block_index > IB_CC_TABLE_CAP_DEFAULT - 1)
21718c2ecf20Sopenharmony_ci		goto bail;
21728c2ecf20Sopenharmony_ci
21738c2ecf20Sopenharmony_ci	spin_lock(&ppd->cc_shadow_lock);
21748c2ecf20Sopenharmony_ci
21758c2ecf20Sopenharmony_ci	max_cct_block =
21768c2ecf20Sopenharmony_ci		(ppd->ccti_entries_shadow->ccti_last_entry + 1)/IB_CCT_ENTRIES;
21778c2ecf20Sopenharmony_ci	max_cct_block = max_cct_block ? max_cct_block - 1 : 0;
21788c2ecf20Sopenharmony_ci
21798c2ecf20Sopenharmony_ci	if (cct_block_index > max_cct_block) {
21808c2ecf20Sopenharmony_ci		spin_unlock(&ppd->cc_shadow_lock);
21818c2ecf20Sopenharmony_ci		goto bail;
21828c2ecf20Sopenharmony_ci	}
21838c2ecf20Sopenharmony_ci
21848c2ecf20Sopenharmony_ci	ccp->attr_mod = cpu_to_be32(cct_block_index);
21858c2ecf20Sopenharmony_ci
21868c2ecf20Sopenharmony_ci	cct_entry = IB_CCT_ENTRIES * (cct_block_index + 1);
21878c2ecf20Sopenharmony_ci
21888c2ecf20Sopenharmony_ci	cct_entry--;
21898c2ecf20Sopenharmony_ci
21908c2ecf20Sopenharmony_ci	p->ccti_limit = cpu_to_be16(cct_entry);
21918c2ecf20Sopenharmony_ci
21928c2ecf20Sopenharmony_ci	entries = &ppd->ccti_entries_shadow->
21938c2ecf20Sopenharmony_ci			entries[IB_CCT_ENTRIES * cct_block_index];
21948c2ecf20Sopenharmony_ci	cct_entry %= IB_CCT_ENTRIES;
21958c2ecf20Sopenharmony_ci
21968c2ecf20Sopenharmony_ci	for (i = 0; i <= cct_entry; i++)
21978c2ecf20Sopenharmony_ci		p->ccti_entries[i].entry = cpu_to_be16(entries[i].entry);
21988c2ecf20Sopenharmony_ci
21998c2ecf20Sopenharmony_ci	spin_unlock(&ppd->cc_shadow_lock);
22008c2ecf20Sopenharmony_ci
22018c2ecf20Sopenharmony_ci	return reply((struct ib_smp *) ccp);
22028c2ecf20Sopenharmony_ci
22038c2ecf20Sopenharmony_cibail:
22048c2ecf20Sopenharmony_ci	return reply_failure((struct ib_smp *) ccp);
22058c2ecf20Sopenharmony_ci}
22068c2ecf20Sopenharmony_ci
22078c2ecf20Sopenharmony_cistatic int cc_set_congestion_setting(struct ib_cc_mad *ccp,
22088c2ecf20Sopenharmony_ci				struct ib_device *ibdev, u8 port)
22098c2ecf20Sopenharmony_ci{
22108c2ecf20Sopenharmony_ci	struct ib_cc_congestion_setting_attr *p =
22118c2ecf20Sopenharmony_ci		(struct ib_cc_congestion_setting_attr *)ccp->mgmt_data;
22128c2ecf20Sopenharmony_ci	struct qib_ibport *ibp = to_iport(ibdev, port);
22138c2ecf20Sopenharmony_ci	struct qib_pportdata *ppd = ppd_from_ibp(ibp);
22148c2ecf20Sopenharmony_ci	int i;
22158c2ecf20Sopenharmony_ci
22168c2ecf20Sopenharmony_ci	ppd->cc_sl_control_map = be16_to_cpu(p->control_map);
22178c2ecf20Sopenharmony_ci
22188c2ecf20Sopenharmony_ci	for (i = 0; i < IB_CC_CCS_ENTRIES; i++) {
22198c2ecf20Sopenharmony_ci		ppd->congestion_entries[i].ccti_increase =
22208c2ecf20Sopenharmony_ci			p->entries[i].ccti_increase;
22218c2ecf20Sopenharmony_ci
22228c2ecf20Sopenharmony_ci		ppd->congestion_entries[i].ccti_timer =
22238c2ecf20Sopenharmony_ci			be16_to_cpu(p->entries[i].ccti_timer);
22248c2ecf20Sopenharmony_ci
22258c2ecf20Sopenharmony_ci		ppd->congestion_entries[i].trigger_threshold =
22268c2ecf20Sopenharmony_ci			p->entries[i].trigger_threshold;
22278c2ecf20Sopenharmony_ci
22288c2ecf20Sopenharmony_ci		ppd->congestion_entries[i].ccti_min =
22298c2ecf20Sopenharmony_ci			p->entries[i].ccti_min;
22308c2ecf20Sopenharmony_ci	}
22318c2ecf20Sopenharmony_ci
22328c2ecf20Sopenharmony_ci	return reply((struct ib_smp *) ccp);
22338c2ecf20Sopenharmony_ci}
22348c2ecf20Sopenharmony_ci
22358c2ecf20Sopenharmony_cistatic int cc_set_congestion_control_table(struct ib_cc_mad *ccp,
22368c2ecf20Sopenharmony_ci				struct ib_device *ibdev, u8 port)
22378c2ecf20Sopenharmony_ci{
22388c2ecf20Sopenharmony_ci	struct ib_cc_table_attr *p =
22398c2ecf20Sopenharmony_ci		(struct ib_cc_table_attr *)ccp->mgmt_data;
22408c2ecf20Sopenharmony_ci	struct qib_ibport *ibp = to_iport(ibdev, port);
22418c2ecf20Sopenharmony_ci	struct qib_pportdata *ppd = ppd_from_ibp(ibp);
22428c2ecf20Sopenharmony_ci	u32 cct_block_index = be32_to_cpu(ccp->attr_mod);
22438c2ecf20Sopenharmony_ci	u32 cct_entry;
22448c2ecf20Sopenharmony_ci	struct ib_cc_table_entry_shadow *entries;
22458c2ecf20Sopenharmony_ci	int i;
22468c2ecf20Sopenharmony_ci
22478c2ecf20Sopenharmony_ci	/* Is the table index more than what is supported? */
22488c2ecf20Sopenharmony_ci	if (cct_block_index > IB_CC_TABLE_CAP_DEFAULT - 1)
22498c2ecf20Sopenharmony_ci		goto bail;
22508c2ecf20Sopenharmony_ci
22518c2ecf20Sopenharmony_ci	/* If this packet is the first in the sequence then
22528c2ecf20Sopenharmony_ci	 * zero the total table entry count.
22538c2ecf20Sopenharmony_ci	 */
22548c2ecf20Sopenharmony_ci	if (be16_to_cpu(p->ccti_limit) < IB_CCT_ENTRIES)
22558c2ecf20Sopenharmony_ci		ppd->total_cct_entry = 0;
22568c2ecf20Sopenharmony_ci
22578c2ecf20Sopenharmony_ci	cct_entry = (be16_to_cpu(p->ccti_limit))%IB_CCT_ENTRIES;
22588c2ecf20Sopenharmony_ci
22598c2ecf20Sopenharmony_ci	/* ccti_limit is 0 to 63 */
22608c2ecf20Sopenharmony_ci	ppd->total_cct_entry += (cct_entry + 1);
22618c2ecf20Sopenharmony_ci
22628c2ecf20Sopenharmony_ci	if (ppd->total_cct_entry > ppd->cc_supported_table_entries)
22638c2ecf20Sopenharmony_ci		goto bail;
22648c2ecf20Sopenharmony_ci
22658c2ecf20Sopenharmony_ci	ppd->ccti_limit = be16_to_cpu(p->ccti_limit);
22668c2ecf20Sopenharmony_ci
22678c2ecf20Sopenharmony_ci	entries = ppd->ccti_entries + (IB_CCT_ENTRIES * cct_block_index);
22688c2ecf20Sopenharmony_ci
22698c2ecf20Sopenharmony_ci	for (i = 0; i <= cct_entry; i++)
22708c2ecf20Sopenharmony_ci		entries[i].entry = be16_to_cpu(p->ccti_entries[i].entry);
22718c2ecf20Sopenharmony_ci
22728c2ecf20Sopenharmony_ci	spin_lock(&ppd->cc_shadow_lock);
22738c2ecf20Sopenharmony_ci
22748c2ecf20Sopenharmony_ci	ppd->ccti_entries_shadow->ccti_last_entry = ppd->total_cct_entry - 1;
22758c2ecf20Sopenharmony_ci	memcpy(ppd->ccti_entries_shadow->entries, ppd->ccti_entries,
22768c2ecf20Sopenharmony_ci		(ppd->total_cct_entry * sizeof(struct ib_cc_table_entry)));
22778c2ecf20Sopenharmony_ci
22788c2ecf20Sopenharmony_ci	ppd->congestion_entries_shadow->port_control = IB_CC_CCS_PC_SL_BASED;
22798c2ecf20Sopenharmony_ci	ppd->congestion_entries_shadow->control_map = ppd->cc_sl_control_map;
22808c2ecf20Sopenharmony_ci	memcpy(ppd->congestion_entries_shadow->entries, ppd->congestion_entries,
22818c2ecf20Sopenharmony_ci		IB_CC_CCS_ENTRIES * sizeof(struct ib_cc_congestion_entry));
22828c2ecf20Sopenharmony_ci
22838c2ecf20Sopenharmony_ci	spin_unlock(&ppd->cc_shadow_lock);
22848c2ecf20Sopenharmony_ci
22858c2ecf20Sopenharmony_ci	return reply((struct ib_smp *) ccp);
22868c2ecf20Sopenharmony_ci
22878c2ecf20Sopenharmony_cibail:
22888c2ecf20Sopenharmony_ci	return reply_failure((struct ib_smp *) ccp);
22898c2ecf20Sopenharmony_ci}
22908c2ecf20Sopenharmony_ci
22918c2ecf20Sopenharmony_cistatic int process_cc(struct ib_device *ibdev, int mad_flags,
22928c2ecf20Sopenharmony_ci			u8 port, const struct ib_mad *in_mad,
22938c2ecf20Sopenharmony_ci			struct ib_mad *out_mad)
22948c2ecf20Sopenharmony_ci{
22958c2ecf20Sopenharmony_ci	struct ib_cc_mad *ccp = (struct ib_cc_mad *)out_mad;
22968c2ecf20Sopenharmony_ci	*out_mad = *in_mad;
22978c2ecf20Sopenharmony_ci
22988c2ecf20Sopenharmony_ci	if (ccp->class_version != 2) {
22998c2ecf20Sopenharmony_ci		ccp->status |= IB_SMP_UNSUP_VERSION;
23008c2ecf20Sopenharmony_ci		return reply((struct ib_smp *)ccp);
23018c2ecf20Sopenharmony_ci	}
23028c2ecf20Sopenharmony_ci
23038c2ecf20Sopenharmony_ci	switch (ccp->method) {
23048c2ecf20Sopenharmony_ci	case IB_MGMT_METHOD_GET:
23058c2ecf20Sopenharmony_ci		switch (ccp->attr_id) {
23068c2ecf20Sopenharmony_ci		case IB_CC_ATTR_CLASSPORTINFO:
23078c2ecf20Sopenharmony_ci			return cc_get_classportinfo(ccp, ibdev);
23088c2ecf20Sopenharmony_ci		case IB_CC_ATTR_CONGESTION_INFO:
23098c2ecf20Sopenharmony_ci			return cc_get_congestion_info(ccp, ibdev, port);
23108c2ecf20Sopenharmony_ci		case IB_CC_ATTR_CA_CONGESTION_SETTING:
23118c2ecf20Sopenharmony_ci			return cc_get_congestion_setting(ccp, ibdev, port);
23128c2ecf20Sopenharmony_ci		case IB_CC_ATTR_CONGESTION_CONTROL_TABLE:
23138c2ecf20Sopenharmony_ci			return cc_get_congestion_control_table(ccp, ibdev, port);
23148c2ecf20Sopenharmony_ci		default:
23158c2ecf20Sopenharmony_ci			ccp->status |= IB_SMP_UNSUP_METH_ATTR;
23168c2ecf20Sopenharmony_ci			return reply((struct ib_smp *) ccp);
23178c2ecf20Sopenharmony_ci		}
23188c2ecf20Sopenharmony_ci	case IB_MGMT_METHOD_SET:
23198c2ecf20Sopenharmony_ci		switch (ccp->attr_id) {
23208c2ecf20Sopenharmony_ci		case IB_CC_ATTR_CA_CONGESTION_SETTING:
23218c2ecf20Sopenharmony_ci			return cc_set_congestion_setting(ccp, ibdev, port);
23228c2ecf20Sopenharmony_ci		case IB_CC_ATTR_CONGESTION_CONTROL_TABLE:
23238c2ecf20Sopenharmony_ci			return cc_set_congestion_control_table(ccp, ibdev, port);
23248c2ecf20Sopenharmony_ci		default:
23258c2ecf20Sopenharmony_ci			ccp->status |= IB_SMP_UNSUP_METH_ATTR;
23268c2ecf20Sopenharmony_ci			return reply((struct ib_smp *) ccp);
23278c2ecf20Sopenharmony_ci		}
23288c2ecf20Sopenharmony_ci	case IB_MGMT_METHOD_GET_RESP:
23298c2ecf20Sopenharmony_ci		/*
23308c2ecf20Sopenharmony_ci		 * The ib_mad module will call us to process responses
23318c2ecf20Sopenharmony_ci		 * before checking for other consumers.
23328c2ecf20Sopenharmony_ci		 * Just tell the caller to process it normally.
23338c2ecf20Sopenharmony_ci		 */
23348c2ecf20Sopenharmony_ci		return IB_MAD_RESULT_SUCCESS;
23358c2ecf20Sopenharmony_ci	}
23368c2ecf20Sopenharmony_ci
23378c2ecf20Sopenharmony_ci	/* method is unsupported */
23388c2ecf20Sopenharmony_ci	ccp->status |= IB_SMP_UNSUP_METHOD;
23398c2ecf20Sopenharmony_ci	return reply((struct ib_smp *) ccp);
23408c2ecf20Sopenharmony_ci}
23418c2ecf20Sopenharmony_ci
23428c2ecf20Sopenharmony_ci/**
23438c2ecf20Sopenharmony_ci * qib_process_mad - process an incoming MAD packet
23448c2ecf20Sopenharmony_ci * @ibdev: the infiniband device this packet came in on
23458c2ecf20Sopenharmony_ci * @mad_flags: MAD flags
23468c2ecf20Sopenharmony_ci * @port: the port number this packet came in on
23478c2ecf20Sopenharmony_ci * @in_wc: the work completion entry for this packet
23488c2ecf20Sopenharmony_ci * @in_grh: the global route header for this packet
23498c2ecf20Sopenharmony_ci * @in_mad: the incoming MAD
23508c2ecf20Sopenharmony_ci * @out_mad: any outgoing MAD reply
23518c2ecf20Sopenharmony_ci *
23528c2ecf20Sopenharmony_ci * Returns IB_MAD_RESULT_SUCCESS if this is a MAD that we are not
23538c2ecf20Sopenharmony_ci * interested in processing.
23548c2ecf20Sopenharmony_ci *
23558c2ecf20Sopenharmony_ci * Note that the verbs framework has already done the MAD sanity checks,
23568c2ecf20Sopenharmony_ci * and hop count/pointer updating for IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE
23578c2ecf20Sopenharmony_ci * MADs.
23588c2ecf20Sopenharmony_ci *
23598c2ecf20Sopenharmony_ci * This is called by the ib_mad module.
23608c2ecf20Sopenharmony_ci */
23618c2ecf20Sopenharmony_ciint qib_process_mad(struct ib_device *ibdev, int mad_flags, u8 port,
23628c2ecf20Sopenharmony_ci		    const struct ib_wc *in_wc, const struct ib_grh *in_grh,
23638c2ecf20Sopenharmony_ci		    const struct ib_mad *in, struct ib_mad *out,
23648c2ecf20Sopenharmony_ci		    size_t *out_mad_size, u16 *out_mad_pkey_index)
23658c2ecf20Sopenharmony_ci{
23668c2ecf20Sopenharmony_ci	int ret;
23678c2ecf20Sopenharmony_ci	struct qib_ibport *ibp = to_iport(ibdev, port);
23688c2ecf20Sopenharmony_ci	struct qib_pportdata *ppd = ppd_from_ibp(ibp);
23698c2ecf20Sopenharmony_ci
23708c2ecf20Sopenharmony_ci	switch (in->mad_hdr.mgmt_class) {
23718c2ecf20Sopenharmony_ci	case IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE:
23728c2ecf20Sopenharmony_ci	case IB_MGMT_CLASS_SUBN_LID_ROUTED:
23738c2ecf20Sopenharmony_ci		ret = process_subn(ibdev, mad_flags, port, in, out);
23748c2ecf20Sopenharmony_ci		goto bail;
23758c2ecf20Sopenharmony_ci
23768c2ecf20Sopenharmony_ci	case IB_MGMT_CLASS_PERF_MGMT:
23778c2ecf20Sopenharmony_ci		ret = process_perf(ibdev, port, in, out);
23788c2ecf20Sopenharmony_ci		goto bail;
23798c2ecf20Sopenharmony_ci
23808c2ecf20Sopenharmony_ci	case IB_MGMT_CLASS_CONG_MGMT:
23818c2ecf20Sopenharmony_ci		if (!ppd->congestion_entries_shadow ||
23828c2ecf20Sopenharmony_ci			 !qib_cc_table_size) {
23838c2ecf20Sopenharmony_ci			ret = IB_MAD_RESULT_SUCCESS;
23848c2ecf20Sopenharmony_ci			goto bail;
23858c2ecf20Sopenharmony_ci		}
23868c2ecf20Sopenharmony_ci		ret = process_cc(ibdev, mad_flags, port, in, out);
23878c2ecf20Sopenharmony_ci		goto bail;
23888c2ecf20Sopenharmony_ci
23898c2ecf20Sopenharmony_ci	default:
23908c2ecf20Sopenharmony_ci		ret = IB_MAD_RESULT_SUCCESS;
23918c2ecf20Sopenharmony_ci	}
23928c2ecf20Sopenharmony_ci
23938c2ecf20Sopenharmony_cibail:
23948c2ecf20Sopenharmony_ci	return ret;
23958c2ecf20Sopenharmony_ci}
23968c2ecf20Sopenharmony_ci
23978c2ecf20Sopenharmony_cistatic void xmit_wait_timer_func(struct timer_list *t)
23988c2ecf20Sopenharmony_ci{
23998c2ecf20Sopenharmony_ci	struct qib_pportdata *ppd = from_timer(ppd, t, cong_stats.timer);
24008c2ecf20Sopenharmony_ci	struct qib_devdata *dd = dd_from_ppd(ppd);
24018c2ecf20Sopenharmony_ci	unsigned long flags;
24028c2ecf20Sopenharmony_ci	u8 status;
24038c2ecf20Sopenharmony_ci
24048c2ecf20Sopenharmony_ci	spin_lock_irqsave(&ppd->ibport_data.rvp.lock, flags);
24058c2ecf20Sopenharmony_ci	if (ppd->cong_stats.flags == IB_PMA_CONG_HW_CONTROL_SAMPLE) {
24068c2ecf20Sopenharmony_ci		status = dd->f_portcntr(ppd, QIBPORTCNTR_PSSTAT);
24078c2ecf20Sopenharmony_ci		if (status == IB_PMA_SAMPLE_STATUS_DONE) {
24088c2ecf20Sopenharmony_ci			/* save counter cache */
24098c2ecf20Sopenharmony_ci			cache_hw_sample_counters(ppd);
24108c2ecf20Sopenharmony_ci			ppd->cong_stats.flags = IB_PMA_CONG_HW_CONTROL_TIMER;
24118c2ecf20Sopenharmony_ci		} else
24128c2ecf20Sopenharmony_ci			goto done;
24138c2ecf20Sopenharmony_ci	}
24148c2ecf20Sopenharmony_ci	ppd->cong_stats.counter = xmit_wait_get_value_delta(ppd);
24158c2ecf20Sopenharmony_ci	dd->f_set_cntr_sample(ppd, QIB_CONG_TIMER_PSINTERVAL, 0x0);
24168c2ecf20Sopenharmony_cidone:
24178c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&ppd->ibport_data.rvp.lock, flags);
24188c2ecf20Sopenharmony_ci	mod_timer(&ppd->cong_stats.timer, jiffies + HZ);
24198c2ecf20Sopenharmony_ci}
24208c2ecf20Sopenharmony_ci
24218c2ecf20Sopenharmony_civoid qib_notify_create_mad_agent(struct rvt_dev_info *rdi, int port_idx)
24228c2ecf20Sopenharmony_ci{
24238c2ecf20Sopenharmony_ci	struct qib_ibdev *ibdev = container_of(rdi, struct qib_ibdev, rdi);
24248c2ecf20Sopenharmony_ci	struct qib_devdata *dd = container_of(ibdev,
24258c2ecf20Sopenharmony_ci					      struct qib_devdata, verbs_dev);
24268c2ecf20Sopenharmony_ci
24278c2ecf20Sopenharmony_ci	/* Initialize xmit_wait structure */
24288c2ecf20Sopenharmony_ci	dd->pport[port_idx].cong_stats.counter = 0;
24298c2ecf20Sopenharmony_ci	timer_setup(&dd->pport[port_idx].cong_stats.timer,
24308c2ecf20Sopenharmony_ci		    xmit_wait_timer_func, 0);
24318c2ecf20Sopenharmony_ci	dd->pport[port_idx].cong_stats.timer.expires = 0;
24328c2ecf20Sopenharmony_ci	add_timer(&dd->pport[port_idx].cong_stats.timer);
24338c2ecf20Sopenharmony_ci}
24348c2ecf20Sopenharmony_ci
24358c2ecf20Sopenharmony_civoid qib_notify_free_mad_agent(struct rvt_dev_info *rdi, int port_idx)
24368c2ecf20Sopenharmony_ci{
24378c2ecf20Sopenharmony_ci	struct qib_ibdev *ibdev = container_of(rdi, struct qib_ibdev, rdi);
24388c2ecf20Sopenharmony_ci	struct qib_devdata *dd = container_of(ibdev,
24398c2ecf20Sopenharmony_ci					      struct qib_devdata, verbs_dev);
24408c2ecf20Sopenharmony_ci
24418c2ecf20Sopenharmony_ci	if (dd->pport[port_idx].cong_stats.timer.function)
24428c2ecf20Sopenharmony_ci		del_timer_sync(&dd->pport[port_idx].cong_stats.timer);
24438c2ecf20Sopenharmony_ci
24448c2ecf20Sopenharmony_ci	if (dd->pport[port_idx].ibport_data.smi_ah)
24458c2ecf20Sopenharmony_ci		rdma_destroy_ah(&dd->pport[port_idx].ibport_data.smi_ah->ibah,
24468c2ecf20Sopenharmony_ci				RDMA_DESTROY_AH_SLEEPABLE);
24478c2ecf20Sopenharmony_ci}
2448