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