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