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