]> git.openfabrics.org - ~shefty/rdma-dev.git/blob - drivers/infiniband/hw/qib/qib_mad.c
IB/qib: Fix QP RCU sparse warnings
[~shefty/rdma-dev.git] / drivers / infiniband / hw / qib / qib_mad.c
1 /*
2  * Copyright (c) 2012 Intel Corporation.  All rights reserved.
3  * Copyright (c) 2006 - 2012 QLogic Corporation. All rights reserved.
4  * Copyright (c) 2005, 2006 PathScale, Inc. All rights reserved.
5  *
6  * This software is available to you under a choice of one of two
7  * licenses.  You may choose to be licensed under the terms of the GNU
8  * General Public License (GPL) Version 2, available from the file
9  * COPYING in the main directory of this source tree, or the
10  * OpenIB.org BSD license below:
11  *
12  *     Redistribution and use in source and binary forms, with or
13  *     without modification, are permitted provided that the following
14  *     conditions are met:
15  *
16  *      - Redistributions of source code must retain the above
17  *        copyright notice, this list of conditions and the following
18  *        disclaimer.
19  *
20  *      - Redistributions in binary form must reproduce the above
21  *        copyright notice, this list of conditions and the following
22  *        disclaimer in the documentation and/or other materials
23  *        provided with the distribution.
24  *
25  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
26  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
27  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
28  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
29  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
30  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
31  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
32  * SOFTWARE.
33  */
34
35 #include <rdma/ib_smi.h>
36
37 #include "qib.h"
38 #include "qib_mad.h"
39
40 static int reply(struct ib_smp *smp)
41 {
42         /*
43          * The verbs framework will handle the directed/LID route
44          * packet changes.
45          */
46         smp->method = IB_MGMT_METHOD_GET_RESP;
47         if (smp->mgmt_class == IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE)
48                 smp->status |= IB_SMP_DIRECTION;
49         return IB_MAD_RESULT_SUCCESS | IB_MAD_RESULT_REPLY;
50 }
51
52 static void qib_send_trap(struct qib_ibport *ibp, void *data, unsigned len)
53 {
54         struct ib_mad_send_buf *send_buf;
55         struct ib_mad_agent *agent;
56         struct ib_smp *smp;
57         int ret;
58         unsigned long flags;
59         unsigned long timeout;
60
61         agent = ibp->send_agent;
62         if (!agent)
63                 return;
64
65         /* o14-3.2.1 */
66         if (!(ppd_from_ibp(ibp)->lflags & QIBL_LINKACTIVE))
67                 return;
68
69         /* o14-2 */
70         if (ibp->trap_timeout && time_before(jiffies, ibp->trap_timeout))
71                 return;
72
73         send_buf = ib_create_send_mad(agent, 0, 0, 0, IB_MGMT_MAD_HDR,
74                                       IB_MGMT_MAD_DATA, GFP_ATOMIC);
75         if (IS_ERR(send_buf))
76                 return;
77
78         smp = send_buf->mad;
79         smp->base_version = IB_MGMT_BASE_VERSION;
80         smp->mgmt_class = IB_MGMT_CLASS_SUBN_LID_ROUTED;
81         smp->class_version = 1;
82         smp->method = IB_MGMT_METHOD_TRAP;
83         ibp->tid++;
84         smp->tid = cpu_to_be64(ibp->tid);
85         smp->attr_id = IB_SMP_ATTR_NOTICE;
86         /* o14-1: smp->mkey = 0; */
87         memcpy(smp->data, data, len);
88
89         spin_lock_irqsave(&ibp->lock, flags);
90         if (!ibp->sm_ah) {
91                 if (ibp->sm_lid != be16_to_cpu(IB_LID_PERMISSIVE)) {
92                         struct ib_ah *ah;
93
94                         ah = qib_create_qp0_ah(ibp, ibp->sm_lid);
95                         if (IS_ERR(ah))
96                                 ret = PTR_ERR(ah);
97                         else {
98                                 send_buf->ah = ah;
99                                 ibp->sm_ah = to_iah(ah);
100                                 ret = 0;
101                         }
102                 } else
103                         ret = -EINVAL;
104         } else {
105                 send_buf->ah = &ibp->sm_ah->ibah;
106                 ret = 0;
107         }
108         spin_unlock_irqrestore(&ibp->lock, flags);
109
110         if (!ret)
111                 ret = ib_post_send_mad(send_buf, NULL);
112         if (!ret) {
113                 /* 4.096 usec. */
114                 timeout = (4096 * (1UL << ibp->subnet_timeout)) / 1000;
115                 ibp->trap_timeout = jiffies + usecs_to_jiffies(timeout);
116         } else {
117                 ib_free_send_mad(send_buf);
118                 ibp->trap_timeout = 0;
119         }
120 }
121
122 /*
123  * Send a bad [PQ]_Key trap (ch. 14.3.8).
124  */
125 void qib_bad_pqkey(struct qib_ibport *ibp, __be16 trap_num, u32 key, u32 sl,
126                    u32 qp1, u32 qp2, __be16 lid1, __be16 lid2)
127 {
128         struct ib_mad_notice_attr data;
129
130         if (trap_num == IB_NOTICE_TRAP_BAD_PKEY)
131                 ibp->pkey_violations++;
132         else
133                 ibp->qkey_violations++;
134         ibp->n_pkt_drops++;
135
136         /* Send violation trap */
137         data.generic_type = IB_NOTICE_TYPE_SECURITY;
138         data.prod_type_msb = 0;
139         data.prod_type_lsb = IB_NOTICE_PROD_CA;
140         data.trap_num = trap_num;
141         data.issuer_lid = cpu_to_be16(ppd_from_ibp(ibp)->lid);
142         data.toggle_count = 0;
143         memset(&data.details, 0, sizeof data.details);
144         data.details.ntc_257_258.lid1 = lid1;
145         data.details.ntc_257_258.lid2 = lid2;
146         data.details.ntc_257_258.key = cpu_to_be32(key);
147         data.details.ntc_257_258.sl_qp1 = cpu_to_be32((sl << 28) | qp1);
148         data.details.ntc_257_258.qp2 = cpu_to_be32(qp2);
149
150         qib_send_trap(ibp, &data, sizeof data);
151 }
152
153 /*
154  * Send a bad M_Key trap (ch. 14.3.9).
155  */
156 static void qib_bad_mkey(struct qib_ibport *ibp, struct ib_smp *smp)
157 {
158         struct ib_mad_notice_attr data;
159
160         /* Send violation trap */
161         data.generic_type = IB_NOTICE_TYPE_SECURITY;
162         data.prod_type_msb = 0;
163         data.prod_type_lsb = IB_NOTICE_PROD_CA;
164         data.trap_num = IB_NOTICE_TRAP_BAD_MKEY;
165         data.issuer_lid = cpu_to_be16(ppd_from_ibp(ibp)->lid);
166         data.toggle_count = 0;
167         memset(&data.details, 0, sizeof data.details);
168         data.details.ntc_256.lid = data.issuer_lid;
169         data.details.ntc_256.method = smp->method;
170         data.details.ntc_256.attr_id = smp->attr_id;
171         data.details.ntc_256.attr_mod = smp->attr_mod;
172         data.details.ntc_256.mkey = smp->mkey;
173         if (smp->mgmt_class == IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE) {
174                 u8 hop_cnt;
175
176                 data.details.ntc_256.dr_slid = smp->dr_slid;
177                 data.details.ntc_256.dr_trunc_hop = IB_NOTICE_TRAP_DR_NOTICE;
178                 hop_cnt = smp->hop_cnt;
179                 if (hop_cnt > ARRAY_SIZE(data.details.ntc_256.dr_rtn_path)) {
180                         data.details.ntc_256.dr_trunc_hop |=
181                                 IB_NOTICE_TRAP_DR_TRUNC;
182                         hop_cnt = ARRAY_SIZE(data.details.ntc_256.dr_rtn_path);
183                 }
184                 data.details.ntc_256.dr_trunc_hop |= hop_cnt;
185                 memcpy(data.details.ntc_256.dr_rtn_path, smp->return_path,
186                        hop_cnt);
187         }
188
189         qib_send_trap(ibp, &data, sizeof data);
190 }
191
192 /*
193  * Send a Port Capability Mask Changed trap (ch. 14.3.11).
194  */
195 void qib_cap_mask_chg(struct qib_ibport *ibp)
196 {
197         struct ib_mad_notice_attr data;
198
199         data.generic_type = IB_NOTICE_TYPE_INFO;
200         data.prod_type_msb = 0;
201         data.prod_type_lsb = IB_NOTICE_PROD_CA;
202         data.trap_num = IB_NOTICE_TRAP_CAP_MASK_CHG;
203         data.issuer_lid = cpu_to_be16(ppd_from_ibp(ibp)->lid);
204         data.toggle_count = 0;
205         memset(&data.details, 0, sizeof data.details);
206         data.details.ntc_144.lid = data.issuer_lid;
207         data.details.ntc_144.new_cap_mask = cpu_to_be32(ibp->port_cap_flags);
208
209         qib_send_trap(ibp, &data, sizeof data);
210 }
211
212 /*
213  * Send a System Image GUID Changed trap (ch. 14.3.12).
214  */
215 void qib_sys_guid_chg(struct qib_ibport *ibp)
216 {
217         struct ib_mad_notice_attr data;
218
219         data.generic_type = IB_NOTICE_TYPE_INFO;
220         data.prod_type_msb = 0;
221         data.prod_type_lsb = IB_NOTICE_PROD_CA;
222         data.trap_num = IB_NOTICE_TRAP_SYS_GUID_CHG;
223         data.issuer_lid = cpu_to_be16(ppd_from_ibp(ibp)->lid);
224         data.toggle_count = 0;
225         memset(&data.details, 0, sizeof data.details);
226         data.details.ntc_145.lid = data.issuer_lid;
227         data.details.ntc_145.new_sys_guid = ib_qib_sys_image_guid;
228
229         qib_send_trap(ibp, &data, sizeof data);
230 }
231
232 /*
233  * Send a Node Description Changed trap (ch. 14.3.13).
234  */
235 void qib_node_desc_chg(struct qib_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.local_changes = 1;
248         data.details.ntc_144.change_flags = IB_NOTICE_TRAP_NODE_DESC_CHG;
249
250         qib_send_trap(ibp, &data, sizeof data);
251 }
252
253 static int subn_get_nodedescription(struct ib_smp *smp,
254                                     struct ib_device *ibdev)
255 {
256         if (smp->attr_mod)
257                 smp->status |= IB_SMP_INVALID_FIELD;
258
259         memcpy(smp->data, ibdev->node_desc, sizeof(smp->data));
260
261         return reply(smp);
262 }
263
264 static int subn_get_nodeinfo(struct ib_smp *smp, struct ib_device *ibdev,
265                              u8 port)
266 {
267         struct ib_node_info *nip = (struct ib_node_info *)&smp->data;
268         struct qib_devdata *dd = dd_from_ibdev(ibdev);
269         u32 vendor, majrev, minrev;
270         unsigned pidx = port - 1; /* IB number port from 1, hdw from 0 */
271
272         /* GUID 0 is illegal */
273         if (smp->attr_mod || pidx >= dd->num_pports ||
274             dd->pport[pidx].guid == 0)
275                 smp->status |= IB_SMP_INVALID_FIELD;
276         else
277                 nip->port_guid = dd->pport[pidx].guid;
278
279         nip->base_version = 1;
280         nip->class_version = 1;
281         nip->node_type = 1;     /* channel adapter */
282         nip->num_ports = ibdev->phys_port_cnt;
283         /* This is already in network order */
284         nip->sys_guid = ib_qib_sys_image_guid;
285         nip->node_guid = dd->pport->guid; /* Use first-port GUID as node */
286         nip->partition_cap = cpu_to_be16(qib_get_npkeys(dd));
287         nip->device_id = cpu_to_be16(dd->deviceid);
288         majrev = dd->majrev;
289         minrev = dd->minrev;
290         nip->revision = cpu_to_be32((majrev << 16) | minrev);
291         nip->local_port_num = port;
292         vendor = dd->vendorid;
293         nip->vendor_id[0] = QIB_SRC_OUI_1;
294         nip->vendor_id[1] = QIB_SRC_OUI_2;
295         nip->vendor_id[2] = QIB_SRC_OUI_3;
296
297         return reply(smp);
298 }
299
300 static int subn_get_guidinfo(struct ib_smp *smp, struct ib_device *ibdev,
301                              u8 port)
302 {
303         struct qib_devdata *dd = dd_from_ibdev(ibdev);
304         u32 startgx = 8 * be32_to_cpu(smp->attr_mod);
305         __be64 *p = (__be64 *) smp->data;
306         unsigned pidx = port - 1; /* IB number port from 1, hdw from 0 */
307
308         /* 32 blocks of 8 64-bit GUIDs per block */
309
310         memset(smp->data, 0, sizeof(smp->data));
311
312         if (startgx == 0 && pidx < dd->num_pports) {
313                 struct qib_pportdata *ppd = dd->pport + pidx;
314                 struct qib_ibport *ibp = &ppd->ibport_data;
315                 __be64 g = ppd->guid;
316                 unsigned i;
317
318                 /* GUID 0 is illegal */
319                 if (g == 0)
320                         smp->status |= IB_SMP_INVALID_FIELD;
321                 else {
322                         /* The first is a copy of the read-only HW GUID. */
323                         p[0] = g;
324                         for (i = 1; i < QIB_GUIDS_PER_PORT; i++)
325                                 p[i] = ibp->guids[i - 1];
326                 }
327         } else
328                 smp->status |= IB_SMP_INVALID_FIELD;
329
330         return reply(smp);
331 }
332
333 static void set_link_width_enabled(struct qib_pportdata *ppd, u32 w)
334 {
335         (void) ppd->dd->f_set_ib_cfg(ppd, QIB_IB_CFG_LWID_ENB, w);
336 }
337
338 static void set_link_speed_enabled(struct qib_pportdata *ppd, u32 s)
339 {
340         (void) ppd->dd->f_set_ib_cfg(ppd, QIB_IB_CFG_SPD_ENB, s);
341 }
342
343 static int get_overrunthreshold(struct qib_pportdata *ppd)
344 {
345         return ppd->dd->f_get_ib_cfg(ppd, QIB_IB_CFG_OVERRUN_THRESH);
346 }
347
348 /**
349  * set_overrunthreshold - set the overrun threshold
350  * @ppd: the physical port data
351  * @n: the new threshold
352  *
353  * Note that this will only take effect when the link state changes.
354  */
355 static int set_overrunthreshold(struct qib_pportdata *ppd, unsigned n)
356 {
357         (void) ppd->dd->f_set_ib_cfg(ppd, QIB_IB_CFG_OVERRUN_THRESH,
358                                          (u32)n);
359         return 0;
360 }
361
362 static int get_phyerrthreshold(struct qib_pportdata *ppd)
363 {
364         return ppd->dd->f_get_ib_cfg(ppd, QIB_IB_CFG_PHYERR_THRESH);
365 }
366
367 /**
368  * set_phyerrthreshold - set the physical error threshold
369  * @ppd: the physical port data
370  * @n: the new threshold
371  *
372  * Note that this will only take effect when the link state changes.
373  */
374 static int set_phyerrthreshold(struct qib_pportdata *ppd, unsigned n)
375 {
376         (void) ppd->dd->f_set_ib_cfg(ppd, QIB_IB_CFG_PHYERR_THRESH,
377                                          (u32)n);
378         return 0;
379 }
380
381 /**
382  * get_linkdowndefaultstate - get the default linkdown state
383  * @ppd: the physical port data
384  *
385  * Returns zero if the default is POLL, 1 if the default is SLEEP.
386  */
387 static int get_linkdowndefaultstate(struct qib_pportdata *ppd)
388 {
389         return ppd->dd->f_get_ib_cfg(ppd, QIB_IB_CFG_LINKDEFAULT) ==
390                 IB_LINKINITCMD_SLEEP;
391 }
392
393 static int check_mkey(struct qib_ibport *ibp, struct ib_smp *smp, int mad_flags)
394 {
395         int valid_mkey = 0;
396         int ret = 0;
397
398         /* Is the mkey in the process of expiring? */
399         if (ibp->mkey_lease_timeout &&
400             time_after_eq(jiffies, ibp->mkey_lease_timeout)) {
401                 /* Clear timeout and mkey protection field. */
402                 ibp->mkey_lease_timeout = 0;
403                 ibp->mkeyprot = 0;
404         }
405
406         if ((mad_flags & IB_MAD_IGNORE_MKEY) ||  ibp->mkey == 0 ||
407             ibp->mkey == smp->mkey)
408                 valid_mkey = 1;
409
410         /* Unset lease timeout on any valid Get/Set/TrapRepress */
411         if (valid_mkey && ibp->mkey_lease_timeout &&
412             (smp->method == IB_MGMT_METHOD_GET ||
413              smp->method == IB_MGMT_METHOD_SET ||
414              smp->method == IB_MGMT_METHOD_TRAP_REPRESS))
415                 ibp->mkey_lease_timeout = 0;
416
417         if (!valid_mkey) {
418                 switch (smp->method) {
419                 case IB_MGMT_METHOD_GET:
420                         /* Bad mkey not a violation below level 2 */
421                         if (ibp->mkeyprot < 2)
422                                 break;
423                 case IB_MGMT_METHOD_SET:
424                 case IB_MGMT_METHOD_TRAP_REPRESS:
425                         if (ibp->mkey_violations != 0xFFFF)
426                                 ++ibp->mkey_violations;
427                         if (!ibp->mkey_lease_timeout && ibp->mkey_lease_period)
428                                 ibp->mkey_lease_timeout = jiffies +
429                                         ibp->mkey_lease_period * HZ;
430                         /* Generate a trap notice. */
431                         qib_bad_mkey(ibp, smp);
432                         ret = 1;
433                 }
434         }
435
436         return ret;
437 }
438
439 static int subn_get_portinfo(struct ib_smp *smp, struct ib_device *ibdev,
440                              u8 port)
441 {
442         struct qib_devdata *dd;
443         struct qib_pportdata *ppd;
444         struct qib_ibport *ibp;
445         struct ib_port_info *pip = (struct ib_port_info *)smp->data;
446         u8 mtu;
447         int ret;
448         u32 state;
449         u32 port_num = be32_to_cpu(smp->attr_mod);
450
451         if (port_num == 0)
452                 port_num = port;
453         else {
454                 if (port_num > ibdev->phys_port_cnt) {
455                         smp->status |= IB_SMP_INVALID_FIELD;
456                         ret = reply(smp);
457                         goto bail;
458                 }
459                 if (port_num != port) {
460                         ibp = to_iport(ibdev, port_num);
461                         ret = check_mkey(ibp, smp, 0);
462                         if (ret)
463                                 ret = IB_MAD_RESULT_FAILURE;
464                                 goto bail;
465                 }
466         }
467
468         dd = dd_from_ibdev(ibdev);
469         /* IB numbers ports from 1, hdw from 0 */
470         ppd = dd->pport + (port_num - 1);
471         ibp = &ppd->ibport_data;
472
473         /* Clear all fields.  Only set the non-zero fields. */
474         memset(smp->data, 0, sizeof(smp->data));
475
476         /* Only return the mkey if the protection field allows it. */
477         if (!(smp->method == IB_MGMT_METHOD_GET &&
478               ibp->mkey != smp->mkey &&
479               ibp->mkeyprot == 1))
480                 pip->mkey = ibp->mkey;
481         pip->gid_prefix = ibp->gid_prefix;
482         pip->lid = cpu_to_be16(ppd->lid);
483         pip->sm_lid = cpu_to_be16(ibp->sm_lid);
484         pip->cap_mask = cpu_to_be32(ibp->port_cap_flags);
485         /* pip->diag_code; */
486         pip->mkey_lease_period = cpu_to_be16(ibp->mkey_lease_period);
487         pip->local_port_num = port;
488         pip->link_width_enabled = ppd->link_width_enabled;
489         pip->link_width_supported = ppd->link_width_supported;
490         pip->link_width_active = ppd->link_width_active;
491         state = dd->f_iblink_state(ppd->lastibcstat);
492         pip->linkspeed_portstate = ppd->link_speed_supported << 4 | state;
493
494         pip->portphysstate_linkdown =
495                 (dd->f_ibphys_portstate(ppd->lastibcstat) << 4) |
496                 (get_linkdowndefaultstate(ppd) ? 1 : 2);
497         pip->mkeyprot_resv_lmc = (ibp->mkeyprot << 6) | ppd->lmc;
498         pip->linkspeedactive_enabled = (ppd->link_speed_active << 4) |
499                 ppd->link_speed_enabled;
500         switch (ppd->ibmtu) {
501         default: /* something is wrong; fall through */
502         case 4096:
503                 mtu = IB_MTU_4096;
504                 break;
505         case 2048:
506                 mtu = IB_MTU_2048;
507                 break;
508         case 1024:
509                 mtu = IB_MTU_1024;
510                 break;
511         case 512:
512                 mtu = IB_MTU_512;
513                 break;
514         case 256:
515                 mtu = IB_MTU_256;
516                 break;
517         }
518         pip->neighbormtu_mastersmsl = (mtu << 4) | ibp->sm_sl;
519         pip->vlcap_inittype = ppd->vls_supported << 4;  /* InitType = 0 */
520         pip->vl_high_limit = ibp->vl_high_limit;
521         pip->vl_arb_high_cap =
522                 dd->f_get_ib_cfg(ppd, QIB_IB_CFG_VL_HIGH_CAP);
523         pip->vl_arb_low_cap =
524                 dd->f_get_ib_cfg(ppd, QIB_IB_CFG_VL_LOW_CAP);
525         /* InitTypeReply = 0 */
526         pip->inittypereply_mtucap = qib_ibmtu ? qib_ibmtu : IB_MTU_4096;
527         /* HCAs ignore VLStallCount and HOQLife */
528         /* pip->vlstallcnt_hoqlife; */
529         pip->operationalvl_pei_peo_fpi_fpo =
530                 dd->f_get_ib_cfg(ppd, QIB_IB_CFG_OP_VLS) << 4;
531         pip->mkey_violations = cpu_to_be16(ibp->mkey_violations);
532         /* P_KeyViolations are counted by hardware. */
533         pip->pkey_violations = cpu_to_be16(ibp->pkey_violations);
534         pip->qkey_violations = cpu_to_be16(ibp->qkey_violations);
535         /* Only the hardware GUID is supported for now */
536         pip->guid_cap = QIB_GUIDS_PER_PORT;
537         pip->clientrereg_resv_subnetto = ibp->subnet_timeout;
538         /* 32.768 usec. response time (guessing) */
539         pip->resv_resptimevalue = 3;
540         pip->localphyerrors_overrunerrors =
541                 (get_phyerrthreshold(ppd) << 4) |
542                 get_overrunthreshold(ppd);
543         /* pip->max_credit_hint; */
544         if (ibp->port_cap_flags & IB_PORT_LINK_LATENCY_SUP) {
545                 u32 v;
546
547                 v = dd->f_get_ib_cfg(ppd, QIB_IB_CFG_LINKLATENCY);
548                 pip->link_roundtrip_latency[0] = v >> 16;
549                 pip->link_roundtrip_latency[1] = v >> 8;
550                 pip->link_roundtrip_latency[2] = v;
551         }
552
553         ret = reply(smp);
554
555 bail:
556         return ret;
557 }
558
559 /**
560  * get_pkeys - return the PKEY table
561  * @dd: the qlogic_ib device
562  * @port: the IB port number
563  * @pkeys: the pkey table is placed here
564  */
565 static int get_pkeys(struct qib_devdata *dd, u8 port, u16 *pkeys)
566 {
567         struct qib_pportdata *ppd = dd->pport + port - 1;
568         /*
569          * always a kernel context, no locking needed.
570          * If we get here with ppd setup, no need to check
571          * that pd is valid.
572          */
573         struct qib_ctxtdata *rcd = dd->rcd[ppd->hw_pidx];
574
575         memcpy(pkeys, rcd->pkeys, sizeof(rcd->pkeys));
576
577         return 0;
578 }
579
580 static int subn_get_pkeytable(struct ib_smp *smp, struct ib_device *ibdev,
581                               u8 port)
582 {
583         u32 startpx = 32 * (be32_to_cpu(smp->attr_mod) & 0xffff);
584         u16 *p = (u16 *) smp->data;
585         __be16 *q = (__be16 *) smp->data;
586
587         /* 64 blocks of 32 16-bit P_Key entries */
588
589         memset(smp->data, 0, sizeof(smp->data));
590         if (startpx == 0) {
591                 struct qib_devdata *dd = dd_from_ibdev(ibdev);
592                 unsigned i, n = qib_get_npkeys(dd);
593
594                 get_pkeys(dd, port, p);
595
596                 for (i = 0; i < n; i++)
597                         q[i] = cpu_to_be16(p[i]);
598         } else
599                 smp->status |= IB_SMP_INVALID_FIELD;
600
601         return reply(smp);
602 }
603
604 static int subn_set_guidinfo(struct ib_smp *smp, struct ib_device *ibdev,
605                              u8 port)
606 {
607         struct qib_devdata *dd = dd_from_ibdev(ibdev);
608         u32 startgx = 8 * be32_to_cpu(smp->attr_mod);
609         __be64 *p = (__be64 *) smp->data;
610         unsigned pidx = port - 1; /* IB number port from 1, hdw from 0 */
611
612         /* 32 blocks of 8 64-bit GUIDs per block */
613
614         if (startgx == 0 && pidx < dd->num_pports) {
615                 struct qib_pportdata *ppd = dd->pport + pidx;
616                 struct qib_ibport *ibp = &ppd->ibport_data;
617                 unsigned i;
618
619                 /* The first entry is read-only. */
620                 for (i = 1; i < QIB_GUIDS_PER_PORT; i++)
621                         ibp->guids[i - 1] = p[i];
622         } else
623                 smp->status |= IB_SMP_INVALID_FIELD;
624
625         /* The only GUID we support is the first read-only entry. */
626         return subn_get_guidinfo(smp, ibdev, port);
627 }
628
629 /**
630  * subn_set_portinfo - set port information
631  * @smp: the incoming SM packet
632  * @ibdev: the infiniband device
633  * @port: the port on the device
634  *
635  * Set Portinfo (see ch. 14.2.5.6).
636  */
637 static int subn_set_portinfo(struct ib_smp *smp, struct ib_device *ibdev,
638                              u8 port)
639 {
640         struct ib_port_info *pip = (struct ib_port_info *)smp->data;
641         struct ib_event event;
642         struct qib_devdata *dd;
643         struct qib_pportdata *ppd;
644         struct qib_ibport *ibp;
645         u8 clientrereg = (pip->clientrereg_resv_subnetto & 0x80);
646         unsigned long flags;
647         u16 lid, smlid;
648         u8 lwe;
649         u8 lse;
650         u8 state;
651         u8 vls;
652         u8 msl;
653         u16 lstate;
654         int ret, ore, mtu;
655         u32 port_num = be32_to_cpu(smp->attr_mod);
656
657         if (port_num == 0)
658                 port_num = port;
659         else {
660                 if (port_num > ibdev->phys_port_cnt)
661                         goto err;
662                 /* Port attributes can only be set on the receiving port */
663                 if (port_num != port)
664                         goto get_only;
665         }
666
667         dd = dd_from_ibdev(ibdev);
668         /* IB numbers ports from 1, hdw from 0 */
669         ppd = dd->pport + (port_num - 1);
670         ibp = &ppd->ibport_data;
671         event.device = ibdev;
672         event.element.port_num = port;
673
674         ibp->mkey = pip->mkey;
675         ibp->gid_prefix = pip->gid_prefix;
676         ibp->mkey_lease_period = be16_to_cpu(pip->mkey_lease_period);
677
678         lid = be16_to_cpu(pip->lid);
679         /* Must be a valid unicast LID address. */
680         if (lid == 0 || lid >= QIB_MULTICAST_LID_BASE)
681                 smp->status |= IB_SMP_INVALID_FIELD;
682         else if (ppd->lid != lid || ppd->lmc != (pip->mkeyprot_resv_lmc & 7)) {
683                 if (ppd->lid != lid)
684                         qib_set_uevent_bits(ppd, _QIB_EVENT_LID_CHANGE_BIT);
685                 if (ppd->lmc != (pip->mkeyprot_resv_lmc & 7))
686                         qib_set_uevent_bits(ppd, _QIB_EVENT_LMC_CHANGE_BIT);
687                 qib_set_lid(ppd, lid, pip->mkeyprot_resv_lmc & 7);
688                 event.event = IB_EVENT_LID_CHANGE;
689                 ib_dispatch_event(&event);
690         }
691
692         smlid = be16_to_cpu(pip->sm_lid);
693         msl = pip->neighbormtu_mastersmsl & 0xF;
694         /* Must be a valid unicast LID address. */
695         if (smlid == 0 || smlid >= QIB_MULTICAST_LID_BASE)
696                 smp->status |= IB_SMP_INVALID_FIELD;
697         else if (smlid != ibp->sm_lid || msl != ibp->sm_sl) {
698                 spin_lock_irqsave(&ibp->lock, flags);
699                 if (ibp->sm_ah) {
700                         if (smlid != ibp->sm_lid)
701                                 ibp->sm_ah->attr.dlid = smlid;
702                         if (msl != ibp->sm_sl)
703                                 ibp->sm_ah->attr.sl = msl;
704                 }
705                 spin_unlock_irqrestore(&ibp->lock, flags);
706                 if (smlid != ibp->sm_lid)
707                         ibp->sm_lid = smlid;
708                 if (msl != ibp->sm_sl)
709                         ibp->sm_sl = msl;
710                 event.event = IB_EVENT_SM_CHANGE;
711                 ib_dispatch_event(&event);
712         }
713
714         /* Allow 1x or 4x to be set (see 14.2.6.6). */
715         lwe = pip->link_width_enabled;
716         if (lwe) {
717                 if (lwe == 0xFF)
718                         set_link_width_enabled(ppd, ppd->link_width_supported);
719                 else if (lwe >= 16 || (lwe & ~ppd->link_width_supported))
720                         smp->status |= IB_SMP_INVALID_FIELD;
721                 else if (lwe != ppd->link_width_enabled)
722                         set_link_width_enabled(ppd, lwe);
723         }
724
725         lse = pip->linkspeedactive_enabled & 0xF;
726         if (lse) {
727                 /*
728                  * The IB 1.2 spec. only allows link speed values
729                  * 1, 3, 5, 7, 15.  1.2.1 extended to allow specific
730                  * speeds.
731                  */
732                 if (lse == 15)
733                         set_link_speed_enabled(ppd,
734                                                ppd->link_speed_supported);
735                 else if (lse >= 8 || (lse & ~ppd->link_speed_supported))
736                         smp->status |= IB_SMP_INVALID_FIELD;
737                 else if (lse != ppd->link_speed_enabled)
738                         set_link_speed_enabled(ppd, lse);
739         }
740
741         /* Set link down default state. */
742         switch (pip->portphysstate_linkdown & 0xF) {
743         case 0: /* NOP */
744                 break;
745         case 1: /* SLEEP */
746                 (void) dd->f_set_ib_cfg(ppd, QIB_IB_CFG_LINKDEFAULT,
747                                         IB_LINKINITCMD_SLEEP);
748                 break;
749         case 2: /* POLL */
750                 (void) dd->f_set_ib_cfg(ppd, QIB_IB_CFG_LINKDEFAULT,
751                                         IB_LINKINITCMD_POLL);
752                 break;
753         default:
754                 smp->status |= IB_SMP_INVALID_FIELD;
755         }
756
757         ibp->mkeyprot = pip->mkeyprot_resv_lmc >> 6;
758         ibp->vl_high_limit = pip->vl_high_limit;
759         (void) dd->f_set_ib_cfg(ppd, QIB_IB_CFG_VL_HIGH_LIMIT,
760                                     ibp->vl_high_limit);
761
762         mtu = ib_mtu_enum_to_int((pip->neighbormtu_mastersmsl >> 4) & 0xF);
763         if (mtu == -1)
764                 smp->status |= IB_SMP_INVALID_FIELD;
765         else
766                 qib_set_mtu(ppd, mtu);
767
768         /* Set operational VLs */
769         vls = (pip->operationalvl_pei_peo_fpi_fpo >> 4) & 0xF;
770         if (vls) {
771                 if (vls > ppd->vls_supported)
772                         smp->status |= IB_SMP_INVALID_FIELD;
773                 else
774                         (void) dd->f_set_ib_cfg(ppd, QIB_IB_CFG_OP_VLS, vls);
775         }
776
777         if (pip->mkey_violations == 0)
778                 ibp->mkey_violations = 0;
779
780         if (pip->pkey_violations == 0)
781                 ibp->pkey_violations = 0;
782
783         if (pip->qkey_violations == 0)
784                 ibp->qkey_violations = 0;
785
786         ore = pip->localphyerrors_overrunerrors;
787         if (set_phyerrthreshold(ppd, (ore >> 4) & 0xF))
788                 smp->status |= IB_SMP_INVALID_FIELD;
789
790         if (set_overrunthreshold(ppd, (ore & 0xF)))
791                 smp->status |= IB_SMP_INVALID_FIELD;
792
793         ibp->subnet_timeout = pip->clientrereg_resv_subnetto & 0x1F;
794
795         /*
796          * Do the port state change now that the other link parameters
797          * have been set.
798          * Changing the port physical state only makes sense if the link
799          * is down or is being set to down.
800          */
801         state = pip->linkspeed_portstate & 0xF;
802         lstate = (pip->portphysstate_linkdown >> 4) & 0xF;
803         if (lstate && !(state == IB_PORT_DOWN || state == IB_PORT_NOP))
804                 smp->status |= IB_SMP_INVALID_FIELD;
805
806         /*
807          * Only state changes of DOWN, ARM, and ACTIVE are valid
808          * and must be in the correct state to take effect (see 7.2.6).
809          */
810         switch (state) {
811         case IB_PORT_NOP:
812                 if (lstate == 0)
813                         break;
814                 /* FALLTHROUGH */
815         case IB_PORT_DOWN:
816                 if (lstate == 0)
817                         lstate = QIB_IB_LINKDOWN_ONLY;
818                 else if (lstate == 1)
819                         lstate = QIB_IB_LINKDOWN_SLEEP;
820                 else if (lstate == 2)
821                         lstate = QIB_IB_LINKDOWN;
822                 else if (lstate == 3)
823                         lstate = QIB_IB_LINKDOWN_DISABLE;
824                 else {
825                         smp->status |= IB_SMP_INVALID_FIELD;
826                         break;
827                 }
828                 spin_lock_irqsave(&ppd->lflags_lock, flags);
829                 ppd->lflags &= ~QIBL_LINKV;
830                 spin_unlock_irqrestore(&ppd->lflags_lock, flags);
831                 qib_set_linkstate(ppd, lstate);
832                 /*
833                  * Don't send a reply if the response would be sent
834                  * through the disabled port.
835                  */
836                 if (lstate == QIB_IB_LINKDOWN_DISABLE && smp->hop_cnt) {
837                         ret = IB_MAD_RESULT_SUCCESS | IB_MAD_RESULT_CONSUMED;
838                         goto done;
839                 }
840                 qib_wait_linkstate(ppd, QIBL_LINKV, 10);
841                 break;
842         case IB_PORT_ARMED:
843                 qib_set_linkstate(ppd, QIB_IB_LINKARM);
844                 break;
845         case IB_PORT_ACTIVE:
846                 qib_set_linkstate(ppd, QIB_IB_LINKACTIVE);
847                 break;
848         default:
849                 smp->status |= IB_SMP_INVALID_FIELD;
850         }
851
852         if (clientrereg) {
853                 event.event = IB_EVENT_CLIENT_REREGISTER;
854                 ib_dispatch_event(&event);
855         }
856
857         ret = subn_get_portinfo(smp, ibdev, port);
858
859         /* restore re-reg bit per o14-12.2.1 */
860         pip->clientrereg_resv_subnetto |= clientrereg;
861
862         goto get_only;
863
864 err:
865         smp->status |= IB_SMP_INVALID_FIELD;
866 get_only:
867         ret = subn_get_portinfo(smp, ibdev, port);
868 done:
869         return ret;
870 }
871
872 /**
873  * rm_pkey - decrecment the reference count for the given PKEY
874  * @dd: the qlogic_ib device
875  * @key: the PKEY index
876  *
877  * Return true if this was the last reference and the hardware table entry
878  * needs to be changed.
879  */
880 static int rm_pkey(struct qib_pportdata *ppd, u16 key)
881 {
882         int i;
883         int ret;
884
885         for (i = 0; i < ARRAY_SIZE(ppd->pkeys); i++) {
886                 if (ppd->pkeys[i] != key)
887                         continue;
888                 if (atomic_dec_and_test(&ppd->pkeyrefs[i])) {
889                         ppd->pkeys[i] = 0;
890                         ret = 1;
891                         goto bail;
892                 }
893                 break;
894         }
895
896         ret = 0;
897
898 bail:
899         return ret;
900 }
901
902 /**
903  * add_pkey - add the given PKEY to the hardware table
904  * @dd: the qlogic_ib device
905  * @key: the PKEY
906  *
907  * Return an error code if unable to add the entry, zero if no change,
908  * or 1 if the hardware PKEY register needs to be updated.
909  */
910 static int add_pkey(struct qib_pportdata *ppd, u16 key)
911 {
912         int i;
913         u16 lkey = key & 0x7FFF;
914         int any = 0;
915         int ret;
916
917         if (lkey == 0x7FFF) {
918                 ret = 0;
919                 goto bail;
920         }
921
922         /* Look for an empty slot or a matching PKEY. */
923         for (i = 0; i < ARRAY_SIZE(ppd->pkeys); i++) {
924                 if (!ppd->pkeys[i]) {
925                         any++;
926                         continue;
927                 }
928                 /* If it matches exactly, try to increment the ref count */
929                 if (ppd->pkeys[i] == key) {
930                         if (atomic_inc_return(&ppd->pkeyrefs[i]) > 1) {
931                                 ret = 0;
932                                 goto bail;
933                         }
934                         /* Lost the race. Look for an empty slot below. */
935                         atomic_dec(&ppd->pkeyrefs[i]);
936                         any++;
937                 }
938                 /*
939                  * It makes no sense to have both the limited and unlimited
940                  * PKEY set at the same time since the unlimited one will
941                  * disable the limited one.
942                  */
943                 if ((ppd->pkeys[i] & 0x7FFF) == lkey) {
944                         ret = -EEXIST;
945                         goto bail;
946                 }
947         }
948         if (!any) {
949                 ret = -EBUSY;
950                 goto bail;
951         }
952         for (i = 0; i < ARRAY_SIZE(ppd->pkeys); i++) {
953                 if (!ppd->pkeys[i] &&
954                     atomic_inc_return(&ppd->pkeyrefs[i]) == 1) {
955                         /* for qibstats, etc. */
956                         ppd->pkeys[i] = key;
957                         ret = 1;
958                         goto bail;
959                 }
960         }
961         ret = -EBUSY;
962
963 bail:
964         return ret;
965 }
966
967 /**
968  * set_pkeys - set the PKEY table for ctxt 0
969  * @dd: the qlogic_ib device
970  * @port: the IB port number
971  * @pkeys: the PKEY table
972  */
973 static int set_pkeys(struct qib_devdata *dd, u8 port, u16 *pkeys)
974 {
975         struct qib_pportdata *ppd;
976         struct qib_ctxtdata *rcd;
977         int i;
978         int changed = 0;
979
980         /*
981          * IB port one/two always maps to context zero/one,
982          * always a kernel context, no locking needed
983          * If we get here with ppd setup, no need to check
984          * that rcd is valid.
985          */
986         ppd = dd->pport + (port - 1);
987         rcd = dd->rcd[ppd->hw_pidx];
988
989         for (i = 0; i < ARRAY_SIZE(rcd->pkeys); i++) {
990                 u16 key = pkeys[i];
991                 u16 okey = rcd->pkeys[i];
992
993                 if (key == okey)
994                         continue;
995                 /*
996                  * The value of this PKEY table entry is changing.
997                  * Remove the old entry in the hardware's array of PKEYs.
998                  */
999                 if (okey & 0x7FFF)
1000                         changed |= rm_pkey(ppd, okey);
1001                 if (key & 0x7FFF) {
1002                         int ret = add_pkey(ppd, key);
1003
1004                         if (ret < 0)
1005                                 key = 0;
1006                         else
1007                                 changed |= ret;
1008                 }
1009                 rcd->pkeys[i] = key;
1010         }
1011         if (changed) {
1012                 struct ib_event event;
1013
1014                 (void) dd->f_set_ib_cfg(ppd, QIB_IB_CFG_PKEYS, 0);
1015
1016                 event.event = IB_EVENT_PKEY_CHANGE;
1017                 event.device = &dd->verbs_dev.ibdev;
1018                 event.element.port_num = 1;
1019                 ib_dispatch_event(&event);
1020         }
1021         return 0;
1022 }
1023
1024 static int subn_set_pkeytable(struct ib_smp *smp, struct ib_device *ibdev,
1025                               u8 port)
1026 {
1027         u32 startpx = 32 * (be32_to_cpu(smp->attr_mod) & 0xffff);
1028         __be16 *p = (__be16 *) smp->data;
1029         u16 *q = (u16 *) smp->data;
1030         struct qib_devdata *dd = dd_from_ibdev(ibdev);
1031         unsigned i, n = qib_get_npkeys(dd);
1032
1033         for (i = 0; i < n; i++)
1034                 q[i] = be16_to_cpu(p[i]);
1035
1036         if (startpx != 0 || set_pkeys(dd, port, q) != 0)
1037                 smp->status |= IB_SMP_INVALID_FIELD;
1038
1039         return subn_get_pkeytable(smp, ibdev, port);
1040 }
1041
1042 static int subn_get_sl_to_vl(struct ib_smp *smp, struct ib_device *ibdev,
1043                              u8 port)
1044 {
1045         struct qib_ibport *ibp = to_iport(ibdev, port);
1046         u8 *p = (u8 *) smp->data;
1047         unsigned i;
1048
1049         memset(smp->data, 0, sizeof(smp->data));
1050
1051         if (!(ibp->port_cap_flags & IB_PORT_SL_MAP_SUP))
1052                 smp->status |= IB_SMP_UNSUP_METHOD;
1053         else
1054                 for (i = 0; i < ARRAY_SIZE(ibp->sl_to_vl); i += 2)
1055                         *p++ = (ibp->sl_to_vl[i] << 4) | ibp->sl_to_vl[i + 1];
1056
1057         return reply(smp);
1058 }
1059
1060 static int subn_set_sl_to_vl(struct ib_smp *smp, struct ib_device *ibdev,
1061                              u8 port)
1062 {
1063         struct qib_ibport *ibp = to_iport(ibdev, port);
1064         u8 *p = (u8 *) smp->data;
1065         unsigned i;
1066
1067         if (!(ibp->port_cap_flags & IB_PORT_SL_MAP_SUP)) {
1068                 smp->status |= IB_SMP_UNSUP_METHOD;
1069                 return reply(smp);
1070         }
1071
1072         for (i = 0; i < ARRAY_SIZE(ibp->sl_to_vl); i += 2, p++) {
1073                 ibp->sl_to_vl[i] = *p >> 4;
1074                 ibp->sl_to_vl[i + 1] = *p & 0xF;
1075         }
1076         qib_set_uevent_bits(ppd_from_ibp(to_iport(ibdev, port)),
1077                             _QIB_EVENT_SL2VL_CHANGE_BIT);
1078
1079         return subn_get_sl_to_vl(smp, ibdev, port);
1080 }
1081
1082 static int subn_get_vl_arb(struct ib_smp *smp, struct ib_device *ibdev,
1083                            u8 port)
1084 {
1085         unsigned which = be32_to_cpu(smp->attr_mod) >> 16;
1086         struct qib_pportdata *ppd = ppd_from_ibp(to_iport(ibdev, port));
1087
1088         memset(smp->data, 0, sizeof(smp->data));
1089
1090         if (ppd->vls_supported == IB_VL_VL0)
1091                 smp->status |= IB_SMP_UNSUP_METHOD;
1092         else if (which == IB_VLARB_LOWPRI_0_31)
1093                 (void) ppd->dd->f_get_ib_table(ppd, QIB_IB_TBL_VL_LOW_ARB,
1094                                                    smp->data);
1095         else if (which == IB_VLARB_HIGHPRI_0_31)
1096                 (void) ppd->dd->f_get_ib_table(ppd, QIB_IB_TBL_VL_HIGH_ARB,
1097                                                    smp->data);
1098         else
1099                 smp->status |= IB_SMP_INVALID_FIELD;
1100
1101         return reply(smp);
1102 }
1103
1104 static int subn_set_vl_arb(struct ib_smp *smp, struct ib_device *ibdev,
1105                            u8 port)
1106 {
1107         unsigned which = be32_to_cpu(smp->attr_mod) >> 16;
1108         struct qib_pportdata *ppd = ppd_from_ibp(to_iport(ibdev, port));
1109
1110         if (ppd->vls_supported == IB_VL_VL0)
1111                 smp->status |= IB_SMP_UNSUP_METHOD;
1112         else if (which == IB_VLARB_LOWPRI_0_31)
1113                 (void) ppd->dd->f_set_ib_table(ppd, QIB_IB_TBL_VL_LOW_ARB,
1114                                                    smp->data);
1115         else if (which == IB_VLARB_HIGHPRI_0_31)
1116                 (void) ppd->dd->f_set_ib_table(ppd, QIB_IB_TBL_VL_HIGH_ARB,
1117                                                    smp->data);
1118         else
1119                 smp->status |= IB_SMP_INVALID_FIELD;
1120
1121         return subn_get_vl_arb(smp, ibdev, port);
1122 }
1123
1124 static int subn_trap_repress(struct ib_smp *smp, struct ib_device *ibdev,
1125                              u8 port)
1126 {
1127         /*
1128          * For now, we only send the trap once so no need to process this.
1129          * o13-6, o13-7,
1130          * o14-3.a4 The SMA shall not send any message in response to a valid
1131          * SubnTrapRepress() message.
1132          */
1133         return IB_MAD_RESULT_SUCCESS | IB_MAD_RESULT_CONSUMED;
1134 }
1135
1136 static int pma_get_classportinfo(struct ib_pma_mad *pmp,
1137                                  struct ib_device *ibdev)
1138 {
1139         struct ib_class_port_info *p =
1140                 (struct ib_class_port_info *)pmp->data;
1141         struct qib_devdata *dd = dd_from_ibdev(ibdev);
1142
1143         memset(pmp->data, 0, sizeof(pmp->data));
1144
1145         if (pmp->mad_hdr.attr_mod != 0)
1146                 pmp->mad_hdr.status |= IB_SMP_INVALID_FIELD;
1147
1148         /* Note that AllPortSelect is not valid */
1149         p->base_version = 1;
1150         p->class_version = 1;
1151         p->capability_mask = IB_PMA_CLASS_CAP_EXT_WIDTH;
1152         /*
1153          * Set the most significant bit of CM2 to indicate support for
1154          * congestion statistics
1155          */
1156         p->reserved[0] = dd->psxmitwait_supported << 7;
1157         /*
1158          * Expected response time is 4.096 usec. * 2^18 == 1.073741824 sec.
1159          */
1160         p->resp_time_value = 18;
1161
1162         return reply((struct ib_smp *) pmp);
1163 }
1164
1165 static int pma_get_portsamplescontrol(struct ib_pma_mad *pmp,
1166                                       struct ib_device *ibdev, u8 port)
1167 {
1168         struct ib_pma_portsamplescontrol *p =
1169                 (struct ib_pma_portsamplescontrol *)pmp->data;
1170         struct qib_ibdev *dev = to_idev(ibdev);
1171         struct qib_devdata *dd = dd_from_dev(dev);
1172         struct qib_ibport *ibp = to_iport(ibdev, port);
1173         struct qib_pportdata *ppd = ppd_from_ibp(ibp);
1174         unsigned long flags;
1175         u8 port_select = p->port_select;
1176
1177         memset(pmp->data, 0, sizeof(pmp->data));
1178
1179         p->port_select = port_select;
1180         if (pmp->mad_hdr.attr_mod != 0 || port_select != port) {
1181                 pmp->mad_hdr.status |= IB_SMP_INVALID_FIELD;
1182                 goto bail;
1183         }
1184         spin_lock_irqsave(&ibp->lock, flags);
1185         p->tick = dd->f_get_ib_cfg(ppd, QIB_IB_CFG_PMA_TICKS);
1186         p->sample_status = dd->f_portcntr(ppd, QIBPORTCNTR_PSSTAT);
1187         p->counter_width = 4;   /* 32 bit counters */
1188         p->counter_mask0_9 = COUNTER_MASK0_9;
1189         p->sample_start = cpu_to_be32(ibp->pma_sample_start);
1190         p->sample_interval = cpu_to_be32(ibp->pma_sample_interval);
1191         p->tag = cpu_to_be16(ibp->pma_tag);
1192         p->counter_select[0] = ibp->pma_counter_select[0];
1193         p->counter_select[1] = ibp->pma_counter_select[1];
1194         p->counter_select[2] = ibp->pma_counter_select[2];
1195         p->counter_select[3] = ibp->pma_counter_select[3];
1196         p->counter_select[4] = ibp->pma_counter_select[4];
1197         spin_unlock_irqrestore(&ibp->lock, flags);
1198
1199 bail:
1200         return reply((struct ib_smp *) pmp);
1201 }
1202
1203 static int pma_set_portsamplescontrol(struct ib_pma_mad *pmp,
1204                                       struct ib_device *ibdev, u8 port)
1205 {
1206         struct ib_pma_portsamplescontrol *p =
1207                 (struct ib_pma_portsamplescontrol *)pmp->data;
1208         struct qib_ibdev *dev = to_idev(ibdev);
1209         struct qib_devdata *dd = dd_from_dev(dev);
1210         struct qib_ibport *ibp = to_iport(ibdev, port);
1211         struct qib_pportdata *ppd = ppd_from_ibp(ibp);
1212         unsigned long flags;
1213         u8 status, xmit_flags;
1214         int ret;
1215
1216         if (pmp->mad_hdr.attr_mod != 0 || p->port_select != port) {
1217                 pmp->mad_hdr.status |= IB_SMP_INVALID_FIELD;
1218                 ret = reply((struct ib_smp *) pmp);
1219                 goto bail;
1220         }
1221
1222         spin_lock_irqsave(&ibp->lock, flags);
1223
1224         /* Port Sampling code owns the PS* HW counters */
1225         xmit_flags = ppd->cong_stats.flags;
1226         ppd->cong_stats.flags = IB_PMA_CONG_HW_CONTROL_SAMPLE;
1227         status = dd->f_portcntr(ppd, QIBPORTCNTR_PSSTAT);
1228         if (status == IB_PMA_SAMPLE_STATUS_DONE ||
1229             (status == IB_PMA_SAMPLE_STATUS_RUNNING &&
1230              xmit_flags == IB_PMA_CONG_HW_CONTROL_TIMER)) {
1231                 ibp->pma_sample_start = be32_to_cpu(p->sample_start);
1232                 ibp->pma_sample_interval = be32_to_cpu(p->sample_interval);
1233                 ibp->pma_tag = be16_to_cpu(p->tag);
1234                 ibp->pma_counter_select[0] = p->counter_select[0];
1235                 ibp->pma_counter_select[1] = p->counter_select[1];
1236                 ibp->pma_counter_select[2] = p->counter_select[2];
1237                 ibp->pma_counter_select[3] = p->counter_select[3];
1238                 ibp->pma_counter_select[4] = p->counter_select[4];
1239                 dd->f_set_cntr_sample(ppd, ibp->pma_sample_interval,
1240                                       ibp->pma_sample_start);
1241         }
1242         spin_unlock_irqrestore(&ibp->lock, flags);
1243
1244         ret = pma_get_portsamplescontrol(pmp, ibdev, port);
1245
1246 bail:
1247         return ret;
1248 }
1249
1250 static u64 get_counter(struct qib_ibport *ibp, struct qib_pportdata *ppd,
1251                        __be16 sel)
1252 {
1253         u64 ret;
1254
1255         switch (sel) {
1256         case IB_PMA_PORT_XMIT_DATA:
1257                 ret = ppd->dd->f_portcntr(ppd, QIBPORTCNTR_PSXMITDATA);
1258                 break;
1259         case IB_PMA_PORT_RCV_DATA:
1260                 ret = ppd->dd->f_portcntr(ppd, QIBPORTCNTR_PSRCVDATA);
1261                 break;
1262         case IB_PMA_PORT_XMIT_PKTS:
1263                 ret = ppd->dd->f_portcntr(ppd, QIBPORTCNTR_PSXMITPKTS);
1264                 break;
1265         case IB_PMA_PORT_RCV_PKTS:
1266                 ret = ppd->dd->f_portcntr(ppd, QIBPORTCNTR_PSRCVPKTS);
1267                 break;
1268         case IB_PMA_PORT_XMIT_WAIT:
1269                 ret = ppd->dd->f_portcntr(ppd, QIBPORTCNTR_PSXMITWAIT);
1270                 break;
1271         default:
1272                 ret = 0;
1273         }
1274
1275         return ret;
1276 }
1277
1278 /* This function assumes that the xmit_wait lock is already held */
1279 static u64 xmit_wait_get_value_delta(struct qib_pportdata *ppd)
1280 {
1281         u32 delta;
1282
1283         delta = get_counter(&ppd->ibport_data, ppd,
1284                             IB_PMA_PORT_XMIT_WAIT);
1285         return ppd->cong_stats.counter + delta;
1286 }
1287
1288 static void cache_hw_sample_counters(struct qib_pportdata *ppd)
1289 {
1290         struct qib_ibport *ibp = &ppd->ibport_data;
1291
1292         ppd->cong_stats.counter_cache.psxmitdata =
1293                 get_counter(ibp, ppd, IB_PMA_PORT_XMIT_DATA);
1294         ppd->cong_stats.counter_cache.psrcvdata =
1295                 get_counter(ibp, ppd, IB_PMA_PORT_RCV_DATA);
1296         ppd->cong_stats.counter_cache.psxmitpkts =
1297                 get_counter(ibp, ppd, IB_PMA_PORT_XMIT_PKTS);
1298         ppd->cong_stats.counter_cache.psrcvpkts =
1299                 get_counter(ibp, ppd, IB_PMA_PORT_RCV_PKTS);
1300         ppd->cong_stats.counter_cache.psxmitwait =
1301                 get_counter(ibp, ppd, IB_PMA_PORT_XMIT_WAIT);
1302 }
1303
1304 static u64 get_cache_hw_sample_counters(struct qib_pportdata *ppd,
1305                                         __be16 sel)
1306 {
1307         u64 ret;
1308
1309         switch (sel) {
1310         case IB_PMA_PORT_XMIT_DATA:
1311                 ret = ppd->cong_stats.counter_cache.psxmitdata;
1312                 break;
1313         case IB_PMA_PORT_RCV_DATA:
1314                 ret = ppd->cong_stats.counter_cache.psrcvdata;
1315                 break;
1316         case IB_PMA_PORT_XMIT_PKTS:
1317                 ret = ppd->cong_stats.counter_cache.psxmitpkts;
1318                 break;
1319         case IB_PMA_PORT_RCV_PKTS:
1320                 ret = ppd->cong_stats.counter_cache.psrcvpkts;
1321                 break;
1322         case IB_PMA_PORT_XMIT_WAIT:
1323                 ret = ppd->cong_stats.counter_cache.psxmitwait;
1324                 break;
1325         default:
1326                 ret = 0;
1327         }
1328
1329         return ret;
1330 }
1331
1332 static int pma_get_portsamplesresult(struct ib_pma_mad *pmp,
1333                                      struct ib_device *ibdev, u8 port)
1334 {
1335         struct ib_pma_portsamplesresult *p =
1336                 (struct ib_pma_portsamplesresult *)pmp->data;
1337         struct qib_ibdev *dev = to_idev(ibdev);
1338         struct qib_devdata *dd = dd_from_dev(dev);
1339         struct qib_ibport *ibp = to_iport(ibdev, port);
1340         struct qib_pportdata *ppd = ppd_from_ibp(ibp);
1341         unsigned long flags;
1342         u8 status;
1343         int i;
1344
1345         memset(pmp->data, 0, sizeof(pmp->data));
1346         spin_lock_irqsave(&ibp->lock, flags);
1347         p->tag = cpu_to_be16(ibp->pma_tag);
1348         if (ppd->cong_stats.flags == IB_PMA_CONG_HW_CONTROL_TIMER)
1349                 p->sample_status = IB_PMA_SAMPLE_STATUS_DONE;
1350         else {
1351                 status = dd->f_portcntr(ppd, QIBPORTCNTR_PSSTAT);
1352                 p->sample_status = cpu_to_be16(status);
1353                 if (status == IB_PMA_SAMPLE_STATUS_DONE) {
1354                         cache_hw_sample_counters(ppd);
1355                         ppd->cong_stats.counter =
1356                                 xmit_wait_get_value_delta(ppd);
1357                         dd->f_set_cntr_sample(ppd,
1358                                               QIB_CONG_TIMER_PSINTERVAL, 0);
1359                         ppd->cong_stats.flags = IB_PMA_CONG_HW_CONTROL_TIMER;
1360                 }
1361         }
1362         for (i = 0; i < ARRAY_SIZE(ibp->pma_counter_select); i++)
1363                 p->counter[i] = cpu_to_be32(
1364                         get_cache_hw_sample_counters(
1365                                 ppd, ibp->pma_counter_select[i]));
1366         spin_unlock_irqrestore(&ibp->lock, flags);
1367
1368         return reply((struct ib_smp *) pmp);
1369 }
1370
1371 static int pma_get_portsamplesresult_ext(struct ib_pma_mad *pmp,
1372                                          struct ib_device *ibdev, u8 port)
1373 {
1374         struct ib_pma_portsamplesresult_ext *p =
1375                 (struct ib_pma_portsamplesresult_ext *)pmp->data;
1376         struct qib_ibdev *dev = to_idev(ibdev);
1377         struct qib_devdata *dd = dd_from_dev(dev);
1378         struct qib_ibport *ibp = to_iport(ibdev, port);
1379         struct qib_pportdata *ppd = ppd_from_ibp(ibp);
1380         unsigned long flags;
1381         u8 status;
1382         int i;
1383
1384         /* Port Sampling code owns the PS* HW counters */
1385         memset(pmp->data, 0, sizeof(pmp->data));
1386         spin_lock_irqsave(&ibp->lock, flags);
1387         p->tag = cpu_to_be16(ibp->pma_tag);
1388         if (ppd->cong_stats.flags == IB_PMA_CONG_HW_CONTROL_TIMER)
1389                 p->sample_status = IB_PMA_SAMPLE_STATUS_DONE;
1390         else {
1391                 status = dd->f_portcntr(ppd, QIBPORTCNTR_PSSTAT);
1392                 p->sample_status = cpu_to_be16(status);
1393                 /* 64 bits */
1394                 p->extended_width = cpu_to_be32(0x80000000);
1395                 if (status == IB_PMA_SAMPLE_STATUS_DONE) {
1396                         cache_hw_sample_counters(ppd);
1397                         ppd->cong_stats.counter =
1398                                 xmit_wait_get_value_delta(ppd);
1399                         dd->f_set_cntr_sample(ppd,
1400                                               QIB_CONG_TIMER_PSINTERVAL, 0);
1401                         ppd->cong_stats.flags = IB_PMA_CONG_HW_CONTROL_TIMER;
1402                 }
1403         }
1404         for (i = 0; i < ARRAY_SIZE(ibp->pma_counter_select); i++)
1405                 p->counter[i] = cpu_to_be64(
1406                         get_cache_hw_sample_counters(
1407                                 ppd, ibp->pma_counter_select[i]));
1408         spin_unlock_irqrestore(&ibp->lock, flags);
1409
1410         return reply((struct ib_smp *) pmp);
1411 }
1412
1413 static int pma_get_portcounters(struct ib_pma_mad *pmp,
1414                                 struct ib_device *ibdev, u8 port)
1415 {
1416         struct ib_pma_portcounters *p = (struct ib_pma_portcounters *)
1417                 pmp->data;
1418         struct qib_ibport *ibp = to_iport(ibdev, port);
1419         struct qib_pportdata *ppd = ppd_from_ibp(ibp);
1420         struct qib_verbs_counters cntrs;
1421         u8 port_select = p->port_select;
1422
1423         qib_get_counters(ppd, &cntrs);
1424
1425         /* Adjust counters for any resets done. */
1426         cntrs.symbol_error_counter -= ibp->z_symbol_error_counter;
1427         cntrs.link_error_recovery_counter -=
1428                 ibp->z_link_error_recovery_counter;
1429         cntrs.link_downed_counter -= ibp->z_link_downed_counter;
1430         cntrs.port_rcv_errors -= ibp->z_port_rcv_errors;
1431         cntrs.port_rcv_remphys_errors -= ibp->z_port_rcv_remphys_errors;
1432         cntrs.port_xmit_discards -= ibp->z_port_xmit_discards;
1433         cntrs.port_xmit_data -= ibp->z_port_xmit_data;
1434         cntrs.port_rcv_data -= ibp->z_port_rcv_data;
1435         cntrs.port_xmit_packets -= ibp->z_port_xmit_packets;
1436         cntrs.port_rcv_packets -= ibp->z_port_rcv_packets;
1437         cntrs.local_link_integrity_errors -=
1438                 ibp->z_local_link_integrity_errors;
1439         cntrs.excessive_buffer_overrun_errors -=
1440                 ibp->z_excessive_buffer_overrun_errors;
1441         cntrs.vl15_dropped -= ibp->z_vl15_dropped;
1442         cntrs.vl15_dropped += ibp->n_vl15_dropped;
1443
1444         memset(pmp->data, 0, sizeof(pmp->data));
1445
1446         p->port_select = port_select;
1447         if (pmp->mad_hdr.attr_mod != 0 || port_select != port)
1448                 pmp->mad_hdr.status |= IB_SMP_INVALID_FIELD;
1449
1450         if (cntrs.symbol_error_counter > 0xFFFFUL)
1451                 p->symbol_error_counter = cpu_to_be16(0xFFFF);
1452         else
1453                 p->symbol_error_counter =
1454                         cpu_to_be16((u16)cntrs.symbol_error_counter);
1455         if (cntrs.link_error_recovery_counter > 0xFFUL)
1456                 p->link_error_recovery_counter = 0xFF;
1457         else
1458                 p->link_error_recovery_counter =
1459                         (u8)cntrs.link_error_recovery_counter;
1460         if (cntrs.link_downed_counter > 0xFFUL)
1461                 p->link_downed_counter = 0xFF;
1462         else
1463                 p->link_downed_counter = (u8)cntrs.link_downed_counter;
1464         if (cntrs.port_rcv_errors > 0xFFFFUL)
1465                 p->port_rcv_errors = cpu_to_be16(0xFFFF);
1466         else
1467                 p->port_rcv_errors =
1468                         cpu_to_be16((u16) cntrs.port_rcv_errors);
1469         if (cntrs.port_rcv_remphys_errors > 0xFFFFUL)
1470                 p->port_rcv_remphys_errors = cpu_to_be16(0xFFFF);
1471         else
1472                 p->port_rcv_remphys_errors =
1473                         cpu_to_be16((u16)cntrs.port_rcv_remphys_errors);
1474         if (cntrs.port_xmit_discards > 0xFFFFUL)
1475                 p->port_xmit_discards = cpu_to_be16(0xFFFF);
1476         else
1477                 p->port_xmit_discards =
1478                         cpu_to_be16((u16)cntrs.port_xmit_discards);
1479         if (cntrs.local_link_integrity_errors > 0xFUL)
1480                 cntrs.local_link_integrity_errors = 0xFUL;
1481         if (cntrs.excessive_buffer_overrun_errors > 0xFUL)
1482                 cntrs.excessive_buffer_overrun_errors = 0xFUL;
1483         p->link_overrun_errors = (cntrs.local_link_integrity_errors << 4) |
1484                 cntrs.excessive_buffer_overrun_errors;
1485         if (cntrs.vl15_dropped > 0xFFFFUL)
1486                 p->vl15_dropped = cpu_to_be16(0xFFFF);
1487         else
1488                 p->vl15_dropped = cpu_to_be16((u16)cntrs.vl15_dropped);
1489         if (cntrs.port_xmit_data > 0xFFFFFFFFUL)
1490                 p->port_xmit_data = cpu_to_be32(0xFFFFFFFF);
1491         else
1492                 p->port_xmit_data = cpu_to_be32((u32)cntrs.port_xmit_data);
1493         if (cntrs.port_rcv_data > 0xFFFFFFFFUL)
1494                 p->port_rcv_data = cpu_to_be32(0xFFFFFFFF);
1495         else
1496                 p->port_rcv_data = cpu_to_be32((u32)cntrs.port_rcv_data);
1497         if (cntrs.port_xmit_packets > 0xFFFFFFFFUL)
1498                 p->port_xmit_packets = cpu_to_be32(0xFFFFFFFF);
1499         else
1500                 p->port_xmit_packets =
1501                         cpu_to_be32((u32)cntrs.port_xmit_packets);
1502         if (cntrs.port_rcv_packets > 0xFFFFFFFFUL)
1503                 p->port_rcv_packets = cpu_to_be32(0xFFFFFFFF);
1504         else
1505                 p->port_rcv_packets =
1506                         cpu_to_be32((u32) cntrs.port_rcv_packets);
1507
1508         return reply((struct ib_smp *) pmp);
1509 }
1510
1511 static int pma_get_portcounters_cong(struct ib_pma_mad *pmp,
1512                                      struct ib_device *ibdev, u8 port)
1513 {
1514         /* Congestion PMA packets start at offset 24 not 64 */
1515         struct ib_pma_portcounters_cong *p =
1516                 (struct ib_pma_portcounters_cong *)pmp->reserved;
1517         struct qib_verbs_counters cntrs;
1518         struct qib_ibport *ibp = to_iport(ibdev, port);
1519         struct qib_pportdata *ppd = ppd_from_ibp(ibp);
1520         struct qib_devdata *dd = dd_from_ppd(ppd);
1521         u32 port_select = be32_to_cpu(pmp->mad_hdr.attr_mod) & 0xFF;
1522         u64 xmit_wait_counter;
1523         unsigned long flags;
1524
1525         /*
1526          * This check is performed only in the GET method because the
1527          * SET method ends up calling this anyway.
1528          */
1529         if (!dd->psxmitwait_supported)
1530                 pmp->mad_hdr.status |= IB_SMP_UNSUP_METH_ATTR;
1531         if (port_select != port)
1532                 pmp->mad_hdr.status |= IB_SMP_INVALID_FIELD;
1533
1534         qib_get_counters(ppd, &cntrs);
1535         spin_lock_irqsave(&ppd->ibport_data.lock, flags);
1536         xmit_wait_counter = xmit_wait_get_value_delta(ppd);
1537         spin_unlock_irqrestore(&ppd->ibport_data.lock, flags);
1538
1539         /* Adjust counters for any resets done. */
1540         cntrs.symbol_error_counter -= ibp->z_symbol_error_counter;
1541         cntrs.link_error_recovery_counter -=
1542                 ibp->z_link_error_recovery_counter;
1543         cntrs.link_downed_counter -= ibp->z_link_downed_counter;
1544         cntrs.port_rcv_errors -= ibp->z_port_rcv_errors;
1545         cntrs.port_rcv_remphys_errors -=
1546                 ibp->z_port_rcv_remphys_errors;
1547         cntrs.port_xmit_discards -= ibp->z_port_xmit_discards;
1548         cntrs.local_link_integrity_errors -=
1549                 ibp->z_local_link_integrity_errors;
1550         cntrs.excessive_buffer_overrun_errors -=
1551                 ibp->z_excessive_buffer_overrun_errors;
1552         cntrs.vl15_dropped -= ibp->z_vl15_dropped;
1553         cntrs.vl15_dropped += ibp->n_vl15_dropped;
1554         cntrs.port_xmit_data -= ibp->z_port_xmit_data;
1555         cntrs.port_rcv_data -= ibp->z_port_rcv_data;
1556         cntrs.port_xmit_packets -= ibp->z_port_xmit_packets;
1557         cntrs.port_rcv_packets -= ibp->z_port_rcv_packets;
1558
1559         memset(pmp->reserved, 0, sizeof(pmp->reserved) +
1560                sizeof(pmp->data));
1561
1562         /*
1563          * Set top 3 bits to indicate interval in picoseconds in
1564          * remaining bits.
1565          */
1566         p->port_check_rate =
1567                 cpu_to_be16((QIB_XMIT_RATE_PICO << 13) |
1568                             (dd->psxmitwait_check_rate &
1569                              ~(QIB_XMIT_RATE_PICO << 13)));
1570         p->port_adr_events = cpu_to_be64(0);
1571         p->port_xmit_wait = cpu_to_be64(xmit_wait_counter);
1572         p->port_xmit_data = cpu_to_be64(cntrs.port_xmit_data);
1573         p->port_rcv_data = cpu_to_be64(cntrs.port_rcv_data);
1574         p->port_xmit_packets =
1575                 cpu_to_be64(cntrs.port_xmit_packets);
1576         p->port_rcv_packets =
1577                 cpu_to_be64(cntrs.port_rcv_packets);
1578         if (cntrs.symbol_error_counter > 0xFFFFUL)
1579                 p->symbol_error_counter = cpu_to_be16(0xFFFF);
1580         else
1581                 p->symbol_error_counter =
1582                         cpu_to_be16(
1583                                 (u16)cntrs.symbol_error_counter);
1584         if (cntrs.link_error_recovery_counter > 0xFFUL)
1585                 p->link_error_recovery_counter = 0xFF;
1586         else
1587                 p->link_error_recovery_counter =
1588                         (u8)cntrs.link_error_recovery_counter;
1589         if (cntrs.link_downed_counter > 0xFFUL)
1590                 p->link_downed_counter = 0xFF;
1591         else
1592                 p->link_downed_counter =
1593                         (u8)cntrs.link_downed_counter;
1594         if (cntrs.port_rcv_errors > 0xFFFFUL)
1595                 p->port_rcv_errors = cpu_to_be16(0xFFFF);
1596         else
1597                 p->port_rcv_errors =
1598                         cpu_to_be16((u16) cntrs.port_rcv_errors);
1599         if (cntrs.port_rcv_remphys_errors > 0xFFFFUL)
1600                 p->port_rcv_remphys_errors = cpu_to_be16(0xFFFF);
1601         else
1602                 p->port_rcv_remphys_errors =
1603                         cpu_to_be16(
1604                                 (u16)cntrs.port_rcv_remphys_errors);
1605         if (cntrs.port_xmit_discards > 0xFFFFUL)
1606                 p->port_xmit_discards = cpu_to_be16(0xFFFF);
1607         else
1608                 p->port_xmit_discards =
1609                         cpu_to_be16((u16)cntrs.port_xmit_discards);
1610         if (cntrs.local_link_integrity_errors > 0xFUL)
1611                 cntrs.local_link_integrity_errors = 0xFUL;
1612         if (cntrs.excessive_buffer_overrun_errors > 0xFUL)
1613                 cntrs.excessive_buffer_overrun_errors = 0xFUL;
1614         p->link_overrun_errors = (cntrs.local_link_integrity_errors << 4) |
1615                 cntrs.excessive_buffer_overrun_errors;
1616         if (cntrs.vl15_dropped > 0xFFFFUL)
1617                 p->vl15_dropped = cpu_to_be16(0xFFFF);
1618         else
1619                 p->vl15_dropped = cpu_to_be16((u16)cntrs.vl15_dropped);
1620
1621         return reply((struct ib_smp *)pmp);
1622 }
1623
1624 static int pma_get_portcounters_ext(struct ib_pma_mad *pmp,
1625                                     struct ib_device *ibdev, u8 port)
1626 {
1627         struct ib_pma_portcounters_ext *p =
1628                 (struct ib_pma_portcounters_ext *)pmp->data;
1629         struct qib_ibport *ibp = to_iport(ibdev, port);
1630         struct qib_pportdata *ppd = ppd_from_ibp(ibp);
1631         u64 swords, rwords, spkts, rpkts, xwait;
1632         u8 port_select = p->port_select;
1633
1634         memset(pmp->data, 0, sizeof(pmp->data));
1635
1636         p->port_select = port_select;
1637         if (pmp->mad_hdr.attr_mod != 0 || port_select != port) {
1638                 pmp->mad_hdr.status |= IB_SMP_INVALID_FIELD;
1639                 goto bail;
1640         }
1641
1642         qib_snapshot_counters(ppd, &swords, &rwords, &spkts, &rpkts, &xwait);
1643
1644         /* Adjust counters for any resets done. */
1645         swords -= ibp->z_port_xmit_data;
1646         rwords -= ibp->z_port_rcv_data;
1647         spkts -= ibp->z_port_xmit_packets;
1648         rpkts -= ibp->z_port_rcv_packets;
1649
1650         p->port_xmit_data = cpu_to_be64(swords);
1651         p->port_rcv_data = cpu_to_be64(rwords);
1652         p->port_xmit_packets = cpu_to_be64(spkts);
1653         p->port_rcv_packets = cpu_to_be64(rpkts);
1654         p->port_unicast_xmit_packets = cpu_to_be64(ibp->n_unicast_xmit);
1655         p->port_unicast_rcv_packets = cpu_to_be64(ibp->n_unicast_rcv);
1656         p->port_multicast_xmit_packets = cpu_to_be64(ibp->n_multicast_xmit);
1657         p->port_multicast_rcv_packets = cpu_to_be64(ibp->n_multicast_rcv);
1658
1659 bail:
1660         return reply((struct ib_smp *) pmp);
1661 }
1662
1663 static int pma_set_portcounters(struct ib_pma_mad *pmp,
1664                                 struct ib_device *ibdev, u8 port)
1665 {
1666         struct ib_pma_portcounters *p = (struct ib_pma_portcounters *)
1667                 pmp->data;
1668         struct qib_ibport *ibp = to_iport(ibdev, port);
1669         struct qib_pportdata *ppd = ppd_from_ibp(ibp);
1670         struct qib_verbs_counters cntrs;
1671
1672         /*
1673          * Since the HW doesn't support clearing counters, we save the
1674          * current count and subtract it from future responses.
1675          */
1676         qib_get_counters(ppd, &cntrs);
1677
1678         if (p->counter_select & IB_PMA_SEL_SYMBOL_ERROR)
1679                 ibp->z_symbol_error_counter = cntrs.symbol_error_counter;
1680
1681         if (p->counter_select & IB_PMA_SEL_LINK_ERROR_RECOVERY)
1682                 ibp->z_link_error_recovery_counter =
1683                         cntrs.link_error_recovery_counter;
1684
1685         if (p->counter_select & IB_PMA_SEL_LINK_DOWNED)
1686                 ibp->z_link_downed_counter = cntrs.link_downed_counter;
1687
1688         if (p->counter_select & IB_PMA_SEL_PORT_RCV_ERRORS)
1689                 ibp->z_port_rcv_errors = cntrs.port_rcv_errors;
1690
1691         if (p->counter_select & IB_PMA_SEL_PORT_RCV_REMPHYS_ERRORS)
1692                 ibp->z_port_rcv_remphys_errors =
1693                         cntrs.port_rcv_remphys_errors;
1694
1695         if (p->counter_select & IB_PMA_SEL_PORT_XMIT_DISCARDS)
1696                 ibp->z_port_xmit_discards = cntrs.port_xmit_discards;
1697
1698         if (p->counter_select & IB_PMA_SEL_LOCAL_LINK_INTEGRITY_ERRORS)
1699                 ibp->z_local_link_integrity_errors =
1700                         cntrs.local_link_integrity_errors;
1701
1702         if (p->counter_select & IB_PMA_SEL_EXCESSIVE_BUFFER_OVERRUNS)
1703                 ibp->z_excessive_buffer_overrun_errors =
1704                         cntrs.excessive_buffer_overrun_errors;
1705
1706         if (p->counter_select & IB_PMA_SEL_PORT_VL15_DROPPED) {
1707                 ibp->n_vl15_dropped = 0;
1708                 ibp->z_vl15_dropped = cntrs.vl15_dropped;
1709         }
1710
1711         if (p->counter_select & IB_PMA_SEL_PORT_XMIT_DATA)
1712                 ibp->z_port_xmit_data = cntrs.port_xmit_data;
1713
1714         if (p->counter_select & IB_PMA_SEL_PORT_RCV_DATA)
1715                 ibp->z_port_rcv_data = cntrs.port_rcv_data;
1716
1717         if (p->counter_select & IB_PMA_SEL_PORT_XMIT_PACKETS)
1718                 ibp->z_port_xmit_packets = cntrs.port_xmit_packets;
1719
1720         if (p->counter_select & IB_PMA_SEL_PORT_RCV_PACKETS)
1721                 ibp->z_port_rcv_packets = cntrs.port_rcv_packets;
1722
1723         return pma_get_portcounters(pmp, ibdev, port);
1724 }
1725
1726 static int pma_set_portcounters_cong(struct ib_pma_mad *pmp,
1727                                      struct ib_device *ibdev, u8 port)
1728 {
1729         struct qib_ibport *ibp = to_iport(ibdev, port);
1730         struct qib_pportdata *ppd = ppd_from_ibp(ibp);
1731         struct qib_devdata *dd = dd_from_ppd(ppd);
1732         struct qib_verbs_counters cntrs;
1733         u32 counter_select = (be32_to_cpu(pmp->mad_hdr.attr_mod) >> 24) & 0xFF;
1734         int ret = 0;
1735         unsigned long flags;
1736
1737         qib_get_counters(ppd, &cntrs);
1738         /* Get counter values before we save them */
1739         ret = pma_get_portcounters_cong(pmp, ibdev, port);
1740
1741         if (counter_select & IB_PMA_SEL_CONG_XMIT) {
1742                 spin_lock_irqsave(&ppd->ibport_data.lock, flags);
1743                 ppd->cong_stats.counter = 0;
1744                 dd->f_set_cntr_sample(ppd, QIB_CONG_TIMER_PSINTERVAL,
1745                                       0x0);
1746                 spin_unlock_irqrestore(&ppd->ibport_data.lock, flags);
1747         }
1748         if (counter_select & IB_PMA_SEL_CONG_PORT_DATA) {
1749                 ibp->z_port_xmit_data = cntrs.port_xmit_data;
1750                 ibp->z_port_rcv_data = cntrs.port_rcv_data;
1751                 ibp->z_port_xmit_packets = cntrs.port_xmit_packets;
1752                 ibp->z_port_rcv_packets = cntrs.port_rcv_packets;
1753         }
1754         if (counter_select & IB_PMA_SEL_CONG_ALL) {
1755                 ibp->z_symbol_error_counter =
1756                         cntrs.symbol_error_counter;
1757                 ibp->z_link_error_recovery_counter =
1758                         cntrs.link_error_recovery_counter;
1759                 ibp->z_link_downed_counter =
1760                         cntrs.link_downed_counter;
1761                 ibp->z_port_rcv_errors = cntrs.port_rcv_errors;
1762                 ibp->z_port_rcv_remphys_errors =
1763                         cntrs.port_rcv_remphys_errors;
1764                 ibp->z_port_xmit_discards =
1765                         cntrs.port_xmit_discards;
1766                 ibp->z_local_link_integrity_errors =
1767                         cntrs.local_link_integrity_errors;
1768                 ibp->z_excessive_buffer_overrun_errors =
1769                         cntrs.excessive_buffer_overrun_errors;
1770                 ibp->n_vl15_dropped = 0;
1771                 ibp->z_vl15_dropped = cntrs.vl15_dropped;
1772         }
1773
1774         return ret;
1775 }
1776
1777 static int pma_set_portcounters_ext(struct ib_pma_mad *pmp,
1778                                     struct ib_device *ibdev, u8 port)
1779 {
1780         struct ib_pma_portcounters *p = (struct ib_pma_portcounters *)
1781                 pmp->data;
1782         struct qib_ibport *ibp = to_iport(ibdev, port);
1783         struct qib_pportdata *ppd = ppd_from_ibp(ibp);
1784         u64 swords, rwords, spkts, rpkts, xwait;
1785
1786         qib_snapshot_counters(ppd, &swords, &rwords, &spkts, &rpkts, &xwait);
1787
1788         if (p->counter_select & IB_PMA_SELX_PORT_XMIT_DATA)
1789                 ibp->z_port_xmit_data = swords;
1790
1791         if (p->counter_select & IB_PMA_SELX_PORT_RCV_DATA)
1792                 ibp->z_port_rcv_data = rwords;
1793
1794         if (p->counter_select & IB_PMA_SELX_PORT_XMIT_PACKETS)
1795                 ibp->z_port_xmit_packets = spkts;
1796
1797         if (p->counter_select & IB_PMA_SELX_PORT_RCV_PACKETS)
1798                 ibp->z_port_rcv_packets = rpkts;
1799
1800         if (p->counter_select & IB_PMA_SELX_PORT_UNI_XMIT_PACKETS)
1801                 ibp->n_unicast_xmit = 0;
1802
1803         if (p->counter_select & IB_PMA_SELX_PORT_UNI_RCV_PACKETS)
1804                 ibp->n_unicast_rcv = 0;
1805
1806         if (p->counter_select & IB_PMA_SELX_PORT_MULTI_XMIT_PACKETS)
1807                 ibp->n_multicast_xmit = 0;
1808
1809         if (p->counter_select & IB_PMA_SELX_PORT_MULTI_RCV_PACKETS)
1810                 ibp->n_multicast_rcv = 0;
1811
1812         return pma_get_portcounters_ext(pmp, ibdev, port);
1813 }
1814
1815 static int process_subn(struct ib_device *ibdev, int mad_flags,
1816                         u8 port, struct ib_mad *in_mad,
1817                         struct ib_mad *out_mad)
1818 {
1819         struct ib_smp *smp = (struct ib_smp *)out_mad;
1820         struct qib_ibport *ibp = to_iport(ibdev, port);
1821         struct qib_pportdata *ppd = ppd_from_ibp(ibp);
1822         int ret;
1823
1824         *out_mad = *in_mad;
1825         if (smp->class_version != 1) {
1826                 smp->status |= IB_SMP_UNSUP_VERSION;
1827                 ret = reply(smp);
1828                 goto bail;
1829         }
1830
1831         ret = check_mkey(ibp, smp, mad_flags);
1832         if (ret) {
1833                 u32 port_num = be32_to_cpu(smp->attr_mod);
1834
1835                 /*
1836                  * If this is a get/set portinfo, we already check the
1837                  * M_Key if the MAD is for another port and the M_Key
1838                  * is OK on the receiving port. This check is needed
1839                  * to increment the error counters when the M_Key
1840                  * fails to match on *both* ports.
1841                  */
1842                 if (in_mad->mad_hdr.attr_id == IB_SMP_ATTR_PORT_INFO &&
1843                     (smp->method == IB_MGMT_METHOD_GET ||
1844                      smp->method == IB_MGMT_METHOD_SET) &&
1845                     port_num && port_num <= ibdev->phys_port_cnt &&
1846                     port != port_num)
1847                         (void) check_mkey(to_iport(ibdev, port_num), smp, 0);
1848                 ret = IB_MAD_RESULT_FAILURE;
1849                 goto bail;
1850         }
1851
1852         switch (smp->method) {
1853         case IB_MGMT_METHOD_GET:
1854                 switch (smp->attr_id) {
1855                 case IB_SMP_ATTR_NODE_DESC:
1856                         ret = subn_get_nodedescription(smp, ibdev);
1857                         goto bail;
1858                 case IB_SMP_ATTR_NODE_INFO:
1859                         ret = subn_get_nodeinfo(smp, ibdev, port);
1860                         goto bail;
1861                 case IB_SMP_ATTR_GUID_INFO:
1862                         ret = subn_get_guidinfo(smp, ibdev, port);
1863                         goto bail;
1864                 case IB_SMP_ATTR_PORT_INFO:
1865                         ret = subn_get_portinfo(smp, ibdev, port);
1866                         goto bail;
1867                 case IB_SMP_ATTR_PKEY_TABLE:
1868                         ret = subn_get_pkeytable(smp, ibdev, port);
1869                         goto bail;
1870                 case IB_SMP_ATTR_SL_TO_VL_TABLE:
1871                         ret = subn_get_sl_to_vl(smp, ibdev, port);
1872                         goto bail;
1873                 case IB_SMP_ATTR_VL_ARB_TABLE:
1874                         ret = subn_get_vl_arb(smp, ibdev, port);
1875                         goto bail;
1876                 case IB_SMP_ATTR_SM_INFO:
1877                         if (ibp->port_cap_flags & IB_PORT_SM_DISABLED) {
1878                                 ret = IB_MAD_RESULT_SUCCESS |
1879                                         IB_MAD_RESULT_CONSUMED;
1880                                 goto bail;
1881                         }
1882                         if (ibp->port_cap_flags & IB_PORT_SM) {
1883                                 ret = IB_MAD_RESULT_SUCCESS;
1884                                 goto bail;
1885                         }
1886                         /* FALLTHROUGH */
1887                 default:
1888                         smp->status |= IB_SMP_UNSUP_METH_ATTR;
1889                         ret = reply(smp);
1890                         goto bail;
1891                 }
1892
1893         case IB_MGMT_METHOD_SET:
1894                 switch (smp->attr_id) {
1895                 case IB_SMP_ATTR_GUID_INFO:
1896                         ret = subn_set_guidinfo(smp, ibdev, port);
1897                         goto bail;
1898                 case IB_SMP_ATTR_PORT_INFO:
1899                         ret = subn_set_portinfo(smp, ibdev, port);
1900                         goto bail;
1901                 case IB_SMP_ATTR_PKEY_TABLE:
1902                         ret = subn_set_pkeytable(smp, ibdev, port);
1903                         goto bail;
1904                 case IB_SMP_ATTR_SL_TO_VL_TABLE:
1905                         ret = subn_set_sl_to_vl(smp, ibdev, port);
1906                         goto bail;
1907                 case IB_SMP_ATTR_VL_ARB_TABLE:
1908                         ret = subn_set_vl_arb(smp, ibdev, port);
1909                         goto bail;
1910                 case IB_SMP_ATTR_SM_INFO:
1911                         if (ibp->port_cap_flags & IB_PORT_SM_DISABLED) {
1912                                 ret = IB_MAD_RESULT_SUCCESS |
1913                                         IB_MAD_RESULT_CONSUMED;
1914                                 goto bail;
1915                         }
1916                         if (ibp->port_cap_flags & IB_PORT_SM) {
1917                                 ret = IB_MAD_RESULT_SUCCESS;
1918                                 goto bail;
1919                         }
1920                         /* FALLTHROUGH */
1921                 default:
1922                         smp->status |= IB_SMP_UNSUP_METH_ATTR;
1923                         ret = reply(smp);
1924                         goto bail;
1925                 }
1926
1927         case IB_MGMT_METHOD_TRAP_REPRESS:
1928                 if (smp->attr_id == IB_SMP_ATTR_NOTICE)
1929                         ret = subn_trap_repress(smp, ibdev, port);
1930                 else {
1931                         smp->status |= IB_SMP_UNSUP_METH_ATTR;
1932                         ret = reply(smp);
1933                 }
1934                 goto bail;
1935
1936         case IB_MGMT_METHOD_TRAP:
1937         case IB_MGMT_METHOD_REPORT:
1938         case IB_MGMT_METHOD_REPORT_RESP:
1939         case IB_MGMT_METHOD_GET_RESP:
1940                 /*
1941                  * The ib_mad module will call us to process responses
1942                  * before checking for other consumers.
1943                  * Just tell the caller to process it normally.
1944                  */
1945                 ret = IB_MAD_RESULT_SUCCESS;
1946                 goto bail;
1947
1948         case IB_MGMT_METHOD_SEND:
1949                 if (ib_get_smp_direction(smp) &&
1950                     smp->attr_id == QIB_VENDOR_IPG) {
1951                         ppd->dd->f_set_ib_cfg(ppd, QIB_IB_CFG_PORT,
1952                                               smp->data[0]);
1953                         ret = IB_MAD_RESULT_SUCCESS | IB_MAD_RESULT_CONSUMED;
1954                 } else
1955                         ret = IB_MAD_RESULT_SUCCESS;
1956                 goto bail;
1957
1958         default:
1959                 smp->status |= IB_SMP_UNSUP_METHOD;
1960                 ret = reply(smp);
1961         }
1962
1963 bail:
1964         return ret;
1965 }
1966
1967 static int process_perf(struct ib_device *ibdev, u8 port,
1968                         struct ib_mad *in_mad,
1969                         struct ib_mad *out_mad)
1970 {
1971         struct ib_pma_mad *pmp = (struct ib_pma_mad *)out_mad;
1972         int ret;
1973
1974         *out_mad = *in_mad;
1975         if (pmp->mad_hdr.class_version != 1) {
1976                 pmp->mad_hdr.status |= IB_SMP_UNSUP_VERSION;
1977                 ret = reply((struct ib_smp *) pmp);
1978                 goto bail;
1979         }
1980
1981         switch (pmp->mad_hdr.method) {
1982         case IB_MGMT_METHOD_GET:
1983                 switch (pmp->mad_hdr.attr_id) {
1984                 case IB_PMA_CLASS_PORT_INFO:
1985                         ret = pma_get_classportinfo(pmp, ibdev);
1986                         goto bail;
1987                 case IB_PMA_PORT_SAMPLES_CONTROL:
1988                         ret = pma_get_portsamplescontrol(pmp, ibdev, port);
1989                         goto bail;
1990                 case IB_PMA_PORT_SAMPLES_RESULT:
1991                         ret = pma_get_portsamplesresult(pmp, ibdev, port);
1992                         goto bail;
1993                 case IB_PMA_PORT_SAMPLES_RESULT_EXT:
1994                         ret = pma_get_portsamplesresult_ext(pmp, ibdev, port);
1995                         goto bail;
1996                 case IB_PMA_PORT_COUNTERS:
1997                         ret = pma_get_portcounters(pmp, ibdev, port);
1998                         goto bail;
1999                 case IB_PMA_PORT_COUNTERS_EXT:
2000                         ret = pma_get_portcounters_ext(pmp, ibdev, port);
2001                         goto bail;
2002                 case IB_PMA_PORT_COUNTERS_CONG:
2003                         ret = pma_get_portcounters_cong(pmp, ibdev, port);
2004                         goto bail;
2005                 default:
2006                         pmp->mad_hdr.status |= IB_SMP_UNSUP_METH_ATTR;
2007                         ret = reply((struct ib_smp *) pmp);
2008                         goto bail;
2009                 }
2010
2011         case IB_MGMT_METHOD_SET:
2012                 switch (pmp->mad_hdr.attr_id) {
2013                 case IB_PMA_PORT_SAMPLES_CONTROL:
2014                         ret = pma_set_portsamplescontrol(pmp, ibdev, port);
2015                         goto bail;
2016                 case IB_PMA_PORT_COUNTERS:
2017                         ret = pma_set_portcounters(pmp, ibdev, port);
2018                         goto bail;
2019                 case IB_PMA_PORT_COUNTERS_EXT:
2020                         ret = pma_set_portcounters_ext(pmp, ibdev, port);
2021                         goto bail;
2022                 case IB_PMA_PORT_COUNTERS_CONG:
2023                         ret = pma_set_portcounters_cong(pmp, ibdev, port);
2024                         goto bail;
2025                 default:
2026                         pmp->mad_hdr.status |= IB_SMP_UNSUP_METH_ATTR;
2027                         ret = reply((struct ib_smp *) pmp);
2028                         goto bail;
2029                 }
2030
2031         case IB_MGMT_METHOD_TRAP:
2032         case IB_MGMT_METHOD_GET_RESP:
2033                 /*
2034                  * The ib_mad module will call us to process responses
2035                  * before checking for other consumers.
2036                  * Just tell the caller to process it normally.
2037                  */
2038                 ret = IB_MAD_RESULT_SUCCESS;
2039                 goto bail;
2040
2041         default:
2042                 pmp->mad_hdr.status |= IB_SMP_UNSUP_METHOD;
2043                 ret = reply((struct ib_smp *) pmp);
2044         }
2045
2046 bail:
2047         return ret;
2048 }
2049
2050 /**
2051  * qib_process_mad - process an incoming MAD packet
2052  * @ibdev: the infiniband device this packet came in on
2053  * @mad_flags: MAD flags
2054  * @port: the port number this packet came in on
2055  * @in_wc: the work completion entry for this packet
2056  * @in_grh: the global route header for this packet
2057  * @in_mad: the incoming MAD
2058  * @out_mad: any outgoing MAD reply
2059  *
2060  * Returns IB_MAD_RESULT_SUCCESS if this is a MAD that we are not
2061  * interested in processing.
2062  *
2063  * Note that the verbs framework has already done the MAD sanity checks,
2064  * and hop count/pointer updating for IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE
2065  * MADs.
2066  *
2067  * This is called by the ib_mad module.
2068  */
2069 int qib_process_mad(struct ib_device *ibdev, int mad_flags, u8 port,
2070                     struct ib_wc *in_wc, struct ib_grh *in_grh,
2071                     struct ib_mad *in_mad, struct ib_mad *out_mad)
2072 {
2073         int ret;
2074
2075         switch (in_mad->mad_hdr.mgmt_class) {
2076         case IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE:
2077         case IB_MGMT_CLASS_SUBN_LID_ROUTED:
2078                 ret = process_subn(ibdev, mad_flags, port, in_mad, out_mad);
2079                 goto bail;
2080
2081         case IB_MGMT_CLASS_PERF_MGMT:
2082                 ret = process_perf(ibdev, port, in_mad, out_mad);
2083                 goto bail;
2084
2085         default:
2086                 ret = IB_MAD_RESULT_SUCCESS;
2087         }
2088
2089 bail:
2090         return ret;
2091 }
2092
2093 static void send_handler(struct ib_mad_agent *agent,
2094                          struct ib_mad_send_wc *mad_send_wc)
2095 {
2096         ib_free_send_mad(mad_send_wc->send_buf);
2097 }
2098
2099 static void xmit_wait_timer_func(unsigned long opaque)
2100 {
2101         struct qib_pportdata *ppd = (struct qib_pportdata *)opaque;
2102         struct qib_devdata *dd = dd_from_ppd(ppd);
2103         unsigned long flags;
2104         u8 status;
2105
2106         spin_lock_irqsave(&ppd->ibport_data.lock, flags);
2107         if (ppd->cong_stats.flags == IB_PMA_CONG_HW_CONTROL_SAMPLE) {
2108                 status = dd->f_portcntr(ppd, QIBPORTCNTR_PSSTAT);
2109                 if (status == IB_PMA_SAMPLE_STATUS_DONE) {
2110                         /* save counter cache */
2111                         cache_hw_sample_counters(ppd);
2112                         ppd->cong_stats.flags = IB_PMA_CONG_HW_CONTROL_TIMER;
2113                 } else
2114                         goto done;
2115         }
2116         ppd->cong_stats.counter = xmit_wait_get_value_delta(ppd);
2117         dd->f_set_cntr_sample(ppd, QIB_CONG_TIMER_PSINTERVAL, 0x0);
2118 done:
2119         spin_unlock_irqrestore(&ppd->ibport_data.lock, flags);
2120         mod_timer(&ppd->cong_stats.timer, jiffies + HZ);
2121 }
2122
2123 int qib_create_agents(struct qib_ibdev *dev)
2124 {
2125         struct qib_devdata *dd = dd_from_dev(dev);
2126         struct ib_mad_agent *agent;
2127         struct qib_ibport *ibp;
2128         int p;
2129         int ret;
2130
2131         for (p = 0; p < dd->num_pports; p++) {
2132                 ibp = &dd->pport[p].ibport_data;
2133                 agent = ib_register_mad_agent(&dev->ibdev, p + 1, IB_QPT_SMI,
2134                                               NULL, 0, send_handler,
2135                                               NULL, NULL);
2136                 if (IS_ERR(agent)) {
2137                         ret = PTR_ERR(agent);
2138                         goto err;
2139                 }
2140
2141                 /* Initialize xmit_wait structure */
2142                 dd->pport[p].cong_stats.counter = 0;
2143                 init_timer(&dd->pport[p].cong_stats.timer);
2144                 dd->pport[p].cong_stats.timer.function = xmit_wait_timer_func;
2145                 dd->pport[p].cong_stats.timer.data =
2146                         (unsigned long)(&dd->pport[p]);
2147                 dd->pport[p].cong_stats.timer.expires = 0;
2148                 add_timer(&dd->pport[p].cong_stats.timer);
2149
2150                 ibp->send_agent = agent;
2151         }
2152
2153         return 0;
2154
2155 err:
2156         for (p = 0; p < dd->num_pports; p++) {
2157                 ibp = &dd->pport[p].ibport_data;
2158                 if (ibp->send_agent) {
2159                         agent = ibp->send_agent;
2160                         ibp->send_agent = NULL;
2161                         ib_unregister_mad_agent(agent);
2162                 }
2163         }
2164
2165         return ret;
2166 }
2167
2168 void qib_free_agents(struct qib_ibdev *dev)
2169 {
2170         struct qib_devdata *dd = dd_from_dev(dev);
2171         struct ib_mad_agent *agent;
2172         struct qib_ibport *ibp;
2173         int p;
2174
2175         for (p = 0; p < dd->num_pports; p++) {
2176                 ibp = &dd->pport[p].ibport_data;
2177                 if (ibp->send_agent) {
2178                         agent = ibp->send_agent;
2179                         ibp->send_agent = NULL;
2180                         ib_unregister_mad_agent(agent);
2181                 }
2182                 if (ibp->sm_ah) {
2183                         ib_destroy_ah(&ibp->sm_ah->ibah);
2184                         ibp->sm_ah = NULL;
2185                 }
2186                 if (dd->pport[p].cong_stats.timer.data)
2187                         del_timer_sync(&dd->pport[p].cong_stats.timer);
2188         }
2189 }