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