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