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