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