IB/qib: Fix QP RCU sparse warnings
[cascardo/linux.git] / drivers / infiniband / hw / qib / qib_iba7322.c
1 /*
2  * Copyright (c) 2012 Intel Corporation.  All rights reserved.
3  * Copyright (c) 2008 - 2012 QLogic Corporation. All rights reserved.
4  *
5  * This software is available to you under a choice of one of two
6  * licenses.  You may choose to be licensed under the terms of the GNU
7  * General Public License (GPL) Version 2, available from the file
8  * COPYING in the main directory of this source tree, or the
9  * OpenIB.org BSD license below:
10  *
11  *     Redistribution and use in source and binary forms, with or
12  *     without modification, are permitted provided that the following
13  *     conditions are met:
14  *
15  *      - Redistributions of source code must retain the above
16  *        copyright notice, this list of conditions and the following
17  *        disclaimer.
18  *
19  *      - Redistributions in binary form must reproduce the above
20  *        copyright notice, this list of conditions and the following
21  *        disclaimer in the documentation and/or other materials
22  *        provided with the distribution.
23  *
24  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
28  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
29  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
30  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
31  * SOFTWARE.
32  */
33
34 /*
35  * This file contains all of the code that is specific to the
36  * InfiniPath 7322 chip
37  */
38
39 #include <linux/interrupt.h>
40 #include <linux/pci.h>
41 #include <linux/delay.h>
42 #include <linux/io.h>
43 #include <linux/jiffies.h>
44 #include <linux/module.h>
45 #include <rdma/ib_verbs.h>
46 #include <rdma/ib_smi.h>
47
48 #include "qib.h"
49 #include "qib_7322_regs.h"
50 #include "qib_qsfp.h"
51
52 #include "qib_mad.h"
53 #include "qib_verbs.h"
54
55 static void qib_setup_7322_setextled(struct qib_pportdata *, u32);
56 static void qib_7322_handle_hwerrors(struct qib_devdata *, char *, size_t);
57 static void sendctrl_7322_mod(struct qib_pportdata *ppd, u32 op);
58 static irqreturn_t qib_7322intr(int irq, void *data);
59 static irqreturn_t qib_7322bufavail(int irq, void *data);
60 static irqreturn_t sdma_intr(int irq, void *data);
61 static irqreturn_t sdma_idle_intr(int irq, void *data);
62 static irqreturn_t sdma_progress_intr(int irq, void *data);
63 static irqreturn_t sdma_cleanup_intr(int irq, void *data);
64 static void qib_7322_txchk_change(struct qib_devdata *, u32, u32, u32,
65                                   struct qib_ctxtdata *rcd);
66 static u8 qib_7322_phys_portstate(u64);
67 static u32 qib_7322_iblink_state(u64);
68 static void qib_set_ib_7322_lstate(struct qib_pportdata *ppd, u16 linkcmd,
69                                    u16 linitcmd);
70 static void force_h1(struct qib_pportdata *);
71 static void adj_tx_serdes(struct qib_pportdata *);
72 static u32 qib_7322_setpbc_control(struct qib_pportdata *, u32, u8, u8);
73 static void qib_7322_mini_pcs_reset(struct qib_pportdata *);
74
75 static u32 ahb_mod(struct qib_devdata *, int, int, int, u32, u32);
76 static void ibsd_wr_allchans(struct qib_pportdata *, int, unsigned, unsigned);
77 static void serdes_7322_los_enable(struct qib_pportdata *, int);
78 static int serdes_7322_init_old(struct qib_pportdata *);
79 static int serdes_7322_init_new(struct qib_pportdata *);
80
81 #define BMASK(msb, lsb) (((1 << ((msb) + 1 - (lsb))) - 1) << (lsb))
82
83 /* LE2 serdes values for different cases */
84 #define LE2_DEFAULT 5
85 #define LE2_5m 4
86 #define LE2_QME 0
87
88 /* Below is special-purpose, so only really works for the IB SerDes blocks. */
89 #define IBSD(hw_pidx) (hw_pidx + 2)
90
91 /* these are variables for documentation and experimentation purposes */
92 static const unsigned rcv_int_timeout = 375;
93 static const unsigned rcv_int_count = 16;
94 static const unsigned sdma_idle_cnt = 64;
95
96 /* Time to stop altering Rx Equalization parameters, after link up. */
97 #define RXEQ_DISABLE_MSECS 2500
98
99 /*
100  * Number of VLs we are configured to use (to allow for more
101  * credits per vl, etc.)
102  */
103 ushort qib_num_cfg_vls = 2;
104 module_param_named(num_vls, qib_num_cfg_vls, ushort, S_IRUGO);
105 MODULE_PARM_DESC(num_vls, "Set number of Virtual Lanes to use (1-8)");
106
107 static ushort qib_chase = 1;
108 module_param_named(chase, qib_chase, ushort, S_IRUGO);
109 MODULE_PARM_DESC(chase, "Enable state chase handling");
110
111 static ushort qib_long_atten = 10; /* 10 dB ~= 5m length */
112 module_param_named(long_attenuation, qib_long_atten, ushort, S_IRUGO);
113 MODULE_PARM_DESC(long_attenuation, \
114                  "attenuation cutoff (dB) for long copper cable setup");
115
116 static ushort qib_singleport;
117 module_param_named(singleport, qib_singleport, ushort, S_IRUGO);
118 MODULE_PARM_DESC(singleport, "Use only IB port 1; more per-port buffer space");
119
120 static ushort qib_krcvq01_no_msi;
121 module_param_named(krcvq01_no_msi, qib_krcvq01_no_msi, ushort, S_IRUGO);
122 MODULE_PARM_DESC(krcvq01_no_msi, "No MSI for kctx < 2");
123
124 /*
125  * Receive header queue sizes
126  */
127 static unsigned qib_rcvhdrcnt;
128 module_param_named(rcvhdrcnt, qib_rcvhdrcnt, uint, S_IRUGO);
129 MODULE_PARM_DESC(rcvhdrcnt, "receive header count");
130
131 static unsigned qib_rcvhdrsize;
132 module_param_named(rcvhdrsize, qib_rcvhdrsize, uint, S_IRUGO);
133 MODULE_PARM_DESC(rcvhdrsize, "receive header size in 32-bit words");
134
135 static unsigned qib_rcvhdrentsize;
136 module_param_named(rcvhdrentsize, qib_rcvhdrentsize, uint, S_IRUGO);
137 MODULE_PARM_DESC(rcvhdrentsize, "receive header entry size in 32-bit words");
138
139 #define MAX_ATTEN_LEN 64 /* plenty for any real system */
140 /* for read back, default index is ~5m copper cable */
141 static char txselect_list[MAX_ATTEN_LEN] = "10";
142 static struct kparam_string kp_txselect = {
143         .string = txselect_list,
144         .maxlen = MAX_ATTEN_LEN
145 };
146 static int  setup_txselect(const char *, struct kernel_param *);
147 module_param_call(txselect, setup_txselect, param_get_string,
148                   &kp_txselect, S_IWUSR | S_IRUGO);
149 MODULE_PARM_DESC(txselect, \
150                  "Tx serdes indices (for no QSFP or invalid QSFP data)");
151
152 #define BOARD_QME7342 5
153 #define BOARD_QMH7342 6
154 #define IS_QMH(dd) (SYM_FIELD((dd)->revision, Revision, BoardID) == \
155                     BOARD_QMH7342)
156 #define IS_QME(dd) (SYM_FIELD((dd)->revision, Revision, BoardID) == \
157                     BOARD_QME7342)
158
159 #define KREG_IDX(regname)     (QIB_7322_##regname##_OFFS / sizeof(u64))
160
161 #define KREG_IBPORT_IDX(regname) ((QIB_7322_##regname##_0_OFFS / sizeof(u64)))
162
163 #define MASK_ACROSS(lsb, msb) \
164         (((1ULL << ((msb) + 1 - (lsb))) - 1) << (lsb))
165
166 #define SYM_RMASK(regname, fldname) ((u64)              \
167         QIB_7322_##regname##_##fldname##_RMASK)
168
169 #define SYM_MASK(regname, fldname) ((u64)               \
170         QIB_7322_##regname##_##fldname##_RMASK <<       \
171          QIB_7322_##regname##_##fldname##_LSB)
172
173 #define SYM_FIELD(value, regname, fldname) ((u64)       \
174         (((value) >> SYM_LSB(regname, fldname)) &       \
175          SYM_RMASK(regname, fldname)))
176
177 /* useful for things like LaFifoEmpty_0...7, TxCreditOK_0...7, etc. */
178 #define SYM_FIELD_ACROSS(value, regname, fldname, nbits) \
179         (((value) >> SYM_LSB(regname, fldname)) & MASK_ACROSS(0, nbits))
180
181 #define HWE_MASK(fldname) SYM_MASK(HwErrMask, fldname##Mask)
182 #define ERR_MASK(fldname) SYM_MASK(ErrMask, fldname##Mask)
183 #define ERR_MASK_N(fldname) SYM_MASK(ErrMask_0, fldname##Mask)
184 #define INT_MASK(fldname) SYM_MASK(IntMask, fldname##IntMask)
185 #define INT_MASK_P(fldname, port) SYM_MASK(IntMask, fldname##IntMask##_##port)
186 /* Below because most, but not all, fields of IntMask have that full suffix */
187 #define INT_MASK_PM(fldname, port) SYM_MASK(IntMask, fldname##Mask##_##port)
188
189
190 #define SYM_LSB(regname, fldname) (QIB_7322_##regname##_##fldname##_LSB)
191
192 /*
193  * the size bits give us 2^N, in KB units.  0 marks as invalid,
194  * and 7 is reserved.  We currently use only 2KB and 4KB
195  */
196 #define IBA7322_TID_SZ_SHIFT QIB_7322_RcvTIDArray0_RT_BufSize_LSB
197 #define IBA7322_TID_SZ_2K (1UL<<IBA7322_TID_SZ_SHIFT) /* 2KB */
198 #define IBA7322_TID_SZ_4K (2UL<<IBA7322_TID_SZ_SHIFT) /* 4KB */
199 #define IBA7322_TID_PA_SHIFT 11U /* TID addr in chip stored w/o low bits */
200
201 #define SendIBSLIDAssignMask \
202         QIB_7322_SendIBSLIDAssign_0_SendIBSLIDAssign_15_0_RMASK
203 #define SendIBSLMCMask \
204         QIB_7322_SendIBSLIDMask_0_SendIBSLIDMask_15_0_RMASK
205
206 #define ExtLED_IB1_YEL SYM_MASK(EXTCtrl, LEDPort0YellowOn)
207 #define ExtLED_IB1_GRN SYM_MASK(EXTCtrl, LEDPort0GreenOn)
208 #define ExtLED_IB2_YEL SYM_MASK(EXTCtrl, LEDPort1YellowOn)
209 #define ExtLED_IB2_GRN SYM_MASK(EXTCtrl, LEDPort1GreenOn)
210 #define ExtLED_IB1_MASK (ExtLED_IB1_YEL | ExtLED_IB1_GRN)
211 #define ExtLED_IB2_MASK (ExtLED_IB2_YEL | ExtLED_IB2_GRN)
212
213 #define _QIB_GPIO_SDA_NUM 1
214 #define _QIB_GPIO_SCL_NUM 0
215 #define QIB_EEPROM_WEN_NUM 14
216 #define QIB_TWSI_EEPROM_DEV 0xA2 /* All Production 7322 cards. */
217
218 /* HW counter clock is at 4nsec */
219 #define QIB_7322_PSXMITWAIT_CHECK_RATE 4000
220
221 /* full speed IB port 1 only */
222 #define PORT_SPD_CAP (QIB_IB_SDR | QIB_IB_DDR | QIB_IB_QDR)
223 #define PORT_SPD_CAP_SHIFT 3
224
225 /* full speed featuremask, both ports */
226 #define DUAL_PORT_CAP (PORT_SPD_CAP | (PORT_SPD_CAP << PORT_SPD_CAP_SHIFT))
227
228 /*
229  * This file contains almost all the chip-specific register information and
230  * access functions for the FAKED QLogic InfiniPath 7322 PCI-Express chip.
231  */
232
233 /* Use defines to tie machine-generated names to lower-case names */
234 #define kr_contextcnt KREG_IDX(ContextCnt)
235 #define kr_control KREG_IDX(Control)
236 #define kr_counterregbase KREG_IDX(CntrRegBase)
237 #define kr_errclear KREG_IDX(ErrClear)
238 #define kr_errmask KREG_IDX(ErrMask)
239 #define kr_errstatus KREG_IDX(ErrStatus)
240 #define kr_extctrl KREG_IDX(EXTCtrl)
241 #define kr_extstatus KREG_IDX(EXTStatus)
242 #define kr_gpio_clear KREG_IDX(GPIOClear)
243 #define kr_gpio_mask KREG_IDX(GPIOMask)
244 #define kr_gpio_out KREG_IDX(GPIOOut)
245 #define kr_gpio_status KREG_IDX(GPIOStatus)
246 #define kr_hwdiagctrl KREG_IDX(HwDiagCtrl)
247 #define kr_debugportval KREG_IDX(DebugPortValueReg)
248 #define kr_fmask KREG_IDX(feature_mask)
249 #define kr_act_fmask KREG_IDX(active_feature_mask)
250 #define kr_hwerrclear KREG_IDX(HwErrClear)
251 #define kr_hwerrmask KREG_IDX(HwErrMask)
252 #define kr_hwerrstatus KREG_IDX(HwErrStatus)
253 #define kr_intclear KREG_IDX(IntClear)
254 #define kr_intmask KREG_IDX(IntMask)
255 #define kr_intredirect KREG_IDX(IntRedirect0)
256 #define kr_intstatus KREG_IDX(IntStatus)
257 #define kr_pagealign KREG_IDX(PageAlign)
258 #define kr_rcvavailtimeout KREG_IDX(RcvAvailTimeOut0)
259 #define kr_rcvctrl KREG_IDX(RcvCtrl) /* Common, but chip also has per-port */
260 #define kr_rcvegrbase KREG_IDX(RcvEgrBase)
261 #define kr_rcvegrcnt KREG_IDX(RcvEgrCnt)
262 #define kr_rcvhdrcnt KREG_IDX(RcvHdrCnt)
263 #define kr_rcvhdrentsize KREG_IDX(RcvHdrEntSize)
264 #define kr_rcvhdrsize KREG_IDX(RcvHdrSize)
265 #define kr_rcvtidbase KREG_IDX(RcvTIDBase)
266 #define kr_rcvtidcnt KREG_IDX(RcvTIDCnt)
267 #define kr_revision KREG_IDX(Revision)
268 #define kr_scratch KREG_IDX(Scratch)
269 #define kr_sendbuffererror KREG_IDX(SendBufErr0) /* and base for 1 and 2 */
270 #define kr_sendcheckmask KREG_IDX(SendCheckMask0) /* and 1, 2 */
271 #define kr_sendctrl KREG_IDX(SendCtrl)
272 #define kr_sendgrhcheckmask KREG_IDX(SendGRHCheckMask0) /* and 1, 2 */
273 #define kr_sendibpktmask KREG_IDX(SendIBPacketMask0) /* and 1, 2 */
274 #define kr_sendpioavailaddr KREG_IDX(SendBufAvailAddr)
275 #define kr_sendpiobufbase KREG_IDX(SendBufBase)
276 #define kr_sendpiobufcnt KREG_IDX(SendBufCnt)
277 #define kr_sendpiosize KREG_IDX(SendBufSize)
278 #define kr_sendregbase KREG_IDX(SendRegBase)
279 #define kr_sendbufavail0 KREG_IDX(SendBufAvail0)
280 #define kr_userregbase KREG_IDX(UserRegBase)
281 #define kr_intgranted KREG_IDX(Int_Granted)
282 #define kr_vecclr_wo_int KREG_IDX(vec_clr_without_int)
283 #define kr_intblocked KREG_IDX(IntBlocked)
284 #define kr_r_access KREG_IDX(SPC_JTAG_ACCESS_REG)
285
286 /*
287  * per-port kernel registers.  Access only with qib_read_kreg_port()
288  * or qib_write_kreg_port()
289  */
290 #define krp_errclear KREG_IBPORT_IDX(ErrClear)
291 #define krp_errmask KREG_IBPORT_IDX(ErrMask)
292 #define krp_errstatus KREG_IBPORT_IDX(ErrStatus)
293 #define krp_highprio_0 KREG_IBPORT_IDX(HighPriority0)
294 #define krp_highprio_limit KREG_IBPORT_IDX(HighPriorityLimit)
295 #define krp_hrtbt_guid KREG_IBPORT_IDX(HRTBT_GUID)
296 #define krp_ib_pcsconfig KREG_IBPORT_IDX(IBPCSConfig)
297 #define krp_ibcctrl_a KREG_IBPORT_IDX(IBCCtrlA)
298 #define krp_ibcctrl_b KREG_IBPORT_IDX(IBCCtrlB)
299 #define krp_ibcctrl_c KREG_IBPORT_IDX(IBCCtrlC)
300 #define krp_ibcstatus_a KREG_IBPORT_IDX(IBCStatusA)
301 #define krp_ibcstatus_b KREG_IBPORT_IDX(IBCStatusB)
302 #define krp_txestatus KREG_IBPORT_IDX(TXEStatus)
303 #define krp_lowprio_0 KREG_IBPORT_IDX(LowPriority0)
304 #define krp_ncmodectrl KREG_IBPORT_IDX(IBNCModeCtrl)
305 #define krp_partitionkey KREG_IBPORT_IDX(RcvPartitionKey)
306 #define krp_psinterval KREG_IBPORT_IDX(PSInterval)
307 #define krp_psstart KREG_IBPORT_IDX(PSStart)
308 #define krp_psstat KREG_IBPORT_IDX(PSStat)
309 #define krp_rcvbthqp KREG_IBPORT_IDX(RcvBTHQP)
310 #define krp_rcvctrl KREG_IBPORT_IDX(RcvCtrl)
311 #define krp_rcvpktledcnt KREG_IBPORT_IDX(RcvPktLEDCnt)
312 #define krp_rcvqpmaptable KREG_IBPORT_IDX(RcvQPMapTableA)
313 #define krp_rxcreditvl0 KREG_IBPORT_IDX(RxCreditVL0)
314 #define krp_rxcreditvl15 (KREG_IBPORT_IDX(RxCreditVL0)+15)
315 #define krp_sendcheckcontrol KREG_IBPORT_IDX(SendCheckControl)
316 #define krp_sendctrl KREG_IBPORT_IDX(SendCtrl)
317 #define krp_senddmabase KREG_IBPORT_IDX(SendDmaBase)
318 #define krp_senddmabufmask0 KREG_IBPORT_IDX(SendDmaBufMask0)
319 #define krp_senddmabufmask1 (KREG_IBPORT_IDX(SendDmaBufMask0) + 1)
320 #define krp_senddmabufmask2 (KREG_IBPORT_IDX(SendDmaBufMask0) + 2)
321 #define krp_senddmabuf_use0 KREG_IBPORT_IDX(SendDmaBufUsed0)
322 #define krp_senddmabuf_use1 (KREG_IBPORT_IDX(SendDmaBufUsed0) + 1)
323 #define krp_senddmabuf_use2 (KREG_IBPORT_IDX(SendDmaBufUsed0) + 2)
324 #define krp_senddmadesccnt KREG_IBPORT_IDX(SendDmaDescCnt)
325 #define krp_senddmahead KREG_IBPORT_IDX(SendDmaHead)
326 #define krp_senddmaheadaddr KREG_IBPORT_IDX(SendDmaHeadAddr)
327 #define krp_senddmaidlecnt KREG_IBPORT_IDX(SendDmaIdleCnt)
328 #define krp_senddmalengen KREG_IBPORT_IDX(SendDmaLenGen)
329 #define krp_senddmaprioritythld KREG_IBPORT_IDX(SendDmaPriorityThld)
330 #define krp_senddmareloadcnt KREG_IBPORT_IDX(SendDmaReloadCnt)
331 #define krp_senddmastatus KREG_IBPORT_IDX(SendDmaStatus)
332 #define krp_senddmatail KREG_IBPORT_IDX(SendDmaTail)
333 #define krp_sendhdrsymptom KREG_IBPORT_IDX(SendHdrErrSymptom)
334 #define krp_sendslid KREG_IBPORT_IDX(SendIBSLIDAssign)
335 #define krp_sendslidmask KREG_IBPORT_IDX(SendIBSLIDMask)
336 #define krp_ibsdtestiftx KREG_IBPORT_IDX(IB_SDTEST_IF_TX)
337 #define krp_adapt_dis_timer KREG_IBPORT_IDX(ADAPT_DISABLE_TIMER_THRESHOLD)
338 #define krp_tx_deemph_override KREG_IBPORT_IDX(IBSD_TX_DEEMPHASIS_OVERRIDE)
339 #define krp_serdesctrl KREG_IBPORT_IDX(IBSerdesCtrl)
340
341 /*
342  * Per-context kernel registers.  Access only with qib_read_kreg_ctxt()
343  * or qib_write_kreg_ctxt()
344  */
345 #define krc_rcvhdraddr KREG_IDX(RcvHdrAddr0)
346 #define krc_rcvhdrtailaddr KREG_IDX(RcvHdrTailAddr0)
347
348 /*
349  * TID Flow table, per context.  Reduces
350  * number of hdrq updates to one per flow (or on errors).
351  * context 0 and 1 share same memory, but have distinct
352  * addresses.  Since for now, we never use expected sends
353  * on kernel contexts, we don't worry about that (we initialize
354  * those entries for ctxt 0/1 on driver load twice, for example).
355  */
356 #define NUM_TIDFLOWS_CTXT 0x20 /* 0x20 per context; have to hardcode */
357 #define ur_rcvflowtable (KREG_IDX(RcvTIDFlowTable0) - KREG_IDX(RcvHdrTail0))
358
359 /* these are the error bits in the tid flows, and are W1C */
360 #define TIDFLOW_ERRBITS  ( \
361         (SYM_MASK(RcvTIDFlowTable0, GenMismatch) << \
362         SYM_LSB(RcvTIDFlowTable0, GenMismatch)) | \
363         (SYM_MASK(RcvTIDFlowTable0, SeqMismatch) << \
364         SYM_LSB(RcvTIDFlowTable0, SeqMismatch)))
365
366 /* Most (not all) Counters are per-IBport.
367  * Requires LBIntCnt is at offset 0 in the group
368  */
369 #define CREG_IDX(regname) \
370 ((QIB_7322_##regname##_0_OFFS - QIB_7322_LBIntCnt_OFFS) / sizeof(u64))
371
372 #define crp_badformat CREG_IDX(RxVersionErrCnt)
373 #define crp_err_rlen CREG_IDX(RxLenErrCnt)
374 #define crp_erricrc CREG_IDX(RxICRCErrCnt)
375 #define crp_errlink CREG_IDX(RxLinkMalformCnt)
376 #define crp_errlpcrc CREG_IDX(RxLPCRCErrCnt)
377 #define crp_errpkey CREG_IDX(RxPKeyMismatchCnt)
378 #define crp_errvcrc CREG_IDX(RxVCRCErrCnt)
379 #define crp_excessbufferovfl CREG_IDX(ExcessBufferOvflCnt)
380 #define crp_iblinkdown CREG_IDX(IBLinkDownedCnt)
381 #define crp_iblinkerrrecov CREG_IDX(IBLinkErrRecoveryCnt)
382 #define crp_ibstatuschange CREG_IDX(IBStatusChangeCnt)
383 #define crp_ibsymbolerr CREG_IDX(IBSymbolErrCnt)
384 #define crp_invalidrlen CREG_IDX(RxMaxMinLenErrCnt)
385 #define crp_locallinkintegrityerr CREG_IDX(LocalLinkIntegrityErrCnt)
386 #define crp_pktrcv CREG_IDX(RxDataPktCnt)
387 #define crp_pktrcvflowctrl CREG_IDX(RxFlowPktCnt)
388 #define crp_pktsend CREG_IDX(TxDataPktCnt)
389 #define crp_pktsendflow CREG_IDX(TxFlowPktCnt)
390 #define crp_psrcvdatacount CREG_IDX(PSRcvDataCount)
391 #define crp_psrcvpktscount CREG_IDX(PSRcvPktsCount)
392 #define crp_psxmitdatacount CREG_IDX(PSXmitDataCount)
393 #define crp_psxmitpktscount CREG_IDX(PSXmitPktsCount)
394 #define crp_psxmitwaitcount CREG_IDX(PSXmitWaitCount)
395 #define crp_rcvebp CREG_IDX(RxEBPCnt)
396 #define crp_rcvflowctrlviol CREG_IDX(RxFlowCtrlViolCnt)
397 #define crp_rcvovfl CREG_IDX(RxBufOvflCnt)
398 #define crp_rxdlidfltr CREG_IDX(RxDlidFltrCnt)
399 #define crp_rxdroppkt CREG_IDX(RxDroppedPktCnt)
400 #define crp_rxotherlocalphyerr CREG_IDX(RxOtherLocalPhyErrCnt)
401 #define crp_rxqpinvalidctxt CREG_IDX(RxQPInvalidContextCnt)
402 #define crp_rxvlerr CREG_IDX(RxVlErrCnt)
403 #define crp_sendstall CREG_IDX(TxFlowStallCnt)
404 #define crp_txdroppedpkt CREG_IDX(TxDroppedPktCnt)
405 #define crp_txhdrerr CREG_IDX(TxHeadersErrCnt)
406 #define crp_txlenerr CREG_IDX(TxLenErrCnt)
407 #define crp_txminmaxlenerr CREG_IDX(TxMaxMinLenErrCnt)
408 #define crp_txsdmadesc CREG_IDX(TxSDmaDescCnt)
409 #define crp_txunderrun CREG_IDX(TxUnderrunCnt)
410 #define crp_txunsupvl CREG_IDX(TxUnsupVLErrCnt)
411 #define crp_vl15droppedpkt CREG_IDX(RxVL15DroppedPktCnt)
412 #define crp_wordrcv CREG_IDX(RxDwordCnt)
413 #define crp_wordsend CREG_IDX(TxDwordCnt)
414 #define crp_tx_creditstalls CREG_IDX(TxCreditUpToDateTimeOut)
415
416 /* these are the (few) counters that are not port-specific */
417 #define CREG_DEVIDX(regname) ((QIB_7322_##regname##_OFFS - \
418                         QIB_7322_LBIntCnt_OFFS) / sizeof(u64))
419 #define cr_base_egrovfl CREG_DEVIDX(RxP0HdrEgrOvflCnt)
420 #define cr_lbint CREG_DEVIDX(LBIntCnt)
421 #define cr_lbstall CREG_DEVIDX(LBFlowStallCnt)
422 #define cr_pcieretrydiag CREG_DEVIDX(PcieRetryBufDiagQwordCnt)
423 #define cr_rxtidflowdrop CREG_DEVIDX(RxTidFlowDropCnt)
424 #define cr_tidfull CREG_DEVIDX(RxTIDFullErrCnt)
425 #define cr_tidinvalid CREG_DEVIDX(RxTIDValidErrCnt)
426
427 /* no chip register for # of IB ports supported, so define */
428 #define NUM_IB_PORTS 2
429
430 /* 1 VL15 buffer per hardware IB port, no register for this, so define */
431 #define NUM_VL15_BUFS NUM_IB_PORTS
432
433 /*
434  * context 0 and 1 are special, and there is no chip register that
435  * defines this value, so we have to define it here.
436  * These are all allocated to either 0 or 1 for single port
437  * hardware configuration, otherwise each gets half
438  */
439 #define KCTXT0_EGRCNT 2048
440
441 /* values for vl and port fields in PBC, 7322-specific */
442 #define PBC_PORT_SEL_LSB 26
443 #define PBC_PORT_SEL_RMASK 1
444 #define PBC_VL_NUM_LSB 27
445 #define PBC_VL_NUM_RMASK 7
446 #define PBC_7322_VL15_SEND (1ULL << 63) /* pbc; VL15, no credit check */
447 #define PBC_7322_VL15_SEND_CTRL (1ULL << 31) /* control version of same */
448
449 static u8 ib_rate_to_delay[IB_RATE_120_GBPS + 1] = {
450         [IB_RATE_2_5_GBPS] = 16,
451         [IB_RATE_5_GBPS] = 8,
452         [IB_RATE_10_GBPS] = 4,
453         [IB_RATE_20_GBPS] = 2,
454         [IB_RATE_30_GBPS] = 2,
455         [IB_RATE_40_GBPS] = 1
456 };
457
458 #define IBA7322_LINKSPEED_SHIFT SYM_LSB(IBCStatusA_0, LinkSpeedActive)
459 #define IBA7322_LINKWIDTH_SHIFT SYM_LSB(IBCStatusA_0, LinkWidthActive)
460
461 /* link training states, from IBC */
462 #define IB_7322_LT_STATE_DISABLED        0x00
463 #define IB_7322_LT_STATE_LINKUP          0x01
464 #define IB_7322_LT_STATE_POLLACTIVE      0x02
465 #define IB_7322_LT_STATE_POLLQUIET       0x03
466 #define IB_7322_LT_STATE_SLEEPDELAY      0x04
467 #define IB_7322_LT_STATE_SLEEPQUIET      0x05
468 #define IB_7322_LT_STATE_CFGDEBOUNCE     0x08
469 #define IB_7322_LT_STATE_CFGRCVFCFG      0x09
470 #define IB_7322_LT_STATE_CFGWAITRMT      0x0a
471 #define IB_7322_LT_STATE_CFGIDLE         0x0b
472 #define IB_7322_LT_STATE_RECOVERRETRAIN  0x0c
473 #define IB_7322_LT_STATE_TXREVLANES      0x0d
474 #define IB_7322_LT_STATE_RECOVERWAITRMT  0x0e
475 #define IB_7322_LT_STATE_RECOVERIDLE     0x0f
476 #define IB_7322_LT_STATE_CFGENH          0x10
477 #define IB_7322_LT_STATE_CFGTEST         0x11
478 #define IB_7322_LT_STATE_CFGWAITRMTTEST  0x12
479 #define IB_7322_LT_STATE_CFGWAITENH      0x13
480
481 /* link state machine states from IBC */
482 #define IB_7322_L_STATE_DOWN             0x0
483 #define IB_7322_L_STATE_INIT             0x1
484 #define IB_7322_L_STATE_ARM              0x2
485 #define IB_7322_L_STATE_ACTIVE           0x3
486 #define IB_7322_L_STATE_ACT_DEFER        0x4
487
488 static const u8 qib_7322_physportstate[0x20] = {
489         [IB_7322_LT_STATE_DISABLED] = IB_PHYSPORTSTATE_DISABLED,
490         [IB_7322_LT_STATE_LINKUP] = IB_PHYSPORTSTATE_LINKUP,
491         [IB_7322_LT_STATE_POLLACTIVE] = IB_PHYSPORTSTATE_POLL,
492         [IB_7322_LT_STATE_POLLQUIET] = IB_PHYSPORTSTATE_POLL,
493         [IB_7322_LT_STATE_SLEEPDELAY] = IB_PHYSPORTSTATE_SLEEP,
494         [IB_7322_LT_STATE_SLEEPQUIET] = IB_PHYSPORTSTATE_SLEEP,
495         [IB_7322_LT_STATE_CFGDEBOUNCE] = IB_PHYSPORTSTATE_CFG_TRAIN,
496         [IB_7322_LT_STATE_CFGRCVFCFG] =
497                 IB_PHYSPORTSTATE_CFG_TRAIN,
498         [IB_7322_LT_STATE_CFGWAITRMT] =
499                 IB_PHYSPORTSTATE_CFG_TRAIN,
500         [IB_7322_LT_STATE_CFGIDLE] = IB_PHYSPORTSTATE_CFG_IDLE,
501         [IB_7322_LT_STATE_RECOVERRETRAIN] =
502                 IB_PHYSPORTSTATE_LINK_ERR_RECOVER,
503         [IB_7322_LT_STATE_RECOVERWAITRMT] =
504                 IB_PHYSPORTSTATE_LINK_ERR_RECOVER,
505         [IB_7322_LT_STATE_RECOVERIDLE] =
506                 IB_PHYSPORTSTATE_LINK_ERR_RECOVER,
507         [IB_7322_LT_STATE_CFGENH] = IB_PHYSPORTSTATE_CFG_ENH,
508         [IB_7322_LT_STATE_CFGTEST] = IB_PHYSPORTSTATE_CFG_TRAIN,
509         [IB_7322_LT_STATE_CFGWAITRMTTEST] =
510                 IB_PHYSPORTSTATE_CFG_TRAIN,
511         [IB_7322_LT_STATE_CFGWAITENH] =
512                 IB_PHYSPORTSTATE_CFG_WAIT_ENH,
513         [0x14] = IB_PHYSPORTSTATE_CFG_TRAIN,
514         [0x15] = IB_PHYSPORTSTATE_CFG_TRAIN,
515         [0x16] = IB_PHYSPORTSTATE_CFG_TRAIN,
516         [0x17] = IB_PHYSPORTSTATE_CFG_TRAIN
517 };
518
519 struct qib_chip_specific {
520         u64 __iomem *cregbase;
521         u64 *cntrs;
522         spinlock_t rcvmod_lock; /* protect rcvctrl shadow changes */
523         spinlock_t gpio_lock; /* RMW of shadows/regs for ExtCtrl and GPIO */
524         u64 main_int_mask;      /* clear bits which have dedicated handlers */
525         u64 int_enable_mask;  /* for per port interrupts in single port mode */
526         u64 errormask;
527         u64 hwerrmask;
528         u64 gpio_out; /* shadow of kr_gpio_out, for rmw ops */
529         u64 gpio_mask; /* shadow the gpio mask register */
530         u64 extctrl; /* shadow the gpio output enable, etc... */
531         u32 ncntrs;
532         u32 nportcntrs;
533         u32 cntrnamelen;
534         u32 portcntrnamelen;
535         u32 numctxts;
536         u32 rcvegrcnt;
537         u32 updthresh; /* current AvailUpdThld */
538         u32 updthresh_dflt; /* default AvailUpdThld */
539         u32 r1;
540         int irq;
541         u32 num_msix_entries;
542         u32 sdmabufcnt;
543         u32 lastbuf_for_pio;
544         u32 stay_in_freeze;
545         u32 recovery_ports_initted;
546         struct qib_msix_entry *msix_entries;
547         unsigned long *sendchkenable;
548         unsigned long *sendgrhchk;
549         unsigned long *sendibchk;
550         u32 rcvavail_timeout[18];
551         char emsgbuf[128]; /* for device error interrupt msg buffer */
552 };
553
554 /* Table of entries in "human readable" form Tx Emphasis. */
555 struct txdds_ent {
556         u8 amp;
557         u8 pre;
558         u8 main;
559         u8 post;
560 };
561
562 struct vendor_txdds_ent {
563         u8 oui[QSFP_VOUI_LEN];
564         u8 *partnum;
565         struct txdds_ent sdr;
566         struct txdds_ent ddr;
567         struct txdds_ent qdr;
568 };
569
570 static void write_tx_serdes_param(struct qib_pportdata *, struct txdds_ent *);
571
572 #define TXDDS_TABLE_SZ 16 /* number of entries per speed in onchip table */
573 #define TXDDS_EXTRA_SZ 13 /* number of extra tx settings entries */
574 #define TXDDS_MFG_SZ 2    /* number of mfg tx settings entries */
575 #define SERDES_CHANS 4 /* yes, it's obvious, but one less magic number */
576
577 #define H1_FORCE_VAL 8
578 #define H1_FORCE_QME 1 /*  may be overridden via setup_txselect() */
579 #define H1_FORCE_QMH 7 /*  may be overridden via setup_txselect() */
580
581 /* The static and dynamic registers are paired, and the pairs indexed by spd */
582 #define krp_static_adapt_dis(spd) (KREG_IBPORT_IDX(ADAPT_DISABLE_STATIC_SDR) \
583         + ((spd) * 2))
584
585 #define QDR_DFE_DISABLE_DELAY 4000 /* msec after LINKUP */
586 #define QDR_STATIC_ADAPT_DOWN 0xf0f0f0f0ULL /* link down, H1-H4 QDR adapts */
587 #define QDR_STATIC_ADAPT_DOWN_R1 0ULL /* r1 link down, H1-H4 QDR adapts */
588 #define QDR_STATIC_ADAPT_INIT 0xffffffffffULL /* up, disable H0,H1-8, LE */
589 #define QDR_STATIC_ADAPT_INIT_R1 0xf0ffffffffULL /* r1 up, disable H0,H1-8 */
590
591 struct qib_chippport_specific {
592         u64 __iomem *kpregbase;
593         u64 __iomem *cpregbase;
594         u64 *portcntrs;
595         struct qib_pportdata *ppd;
596         wait_queue_head_t autoneg_wait;
597         struct delayed_work autoneg_work;
598         struct delayed_work ipg_work;
599         struct timer_list chase_timer;
600         /*
601          * these 5 fields are used to establish deltas for IB symbol
602          * errors and linkrecovery errors.  They can be reported on
603          * some chips during link negotiation prior to INIT, and with
604          * DDR when faking DDR negotiations with non-IBTA switches.
605          * The chip counters are adjusted at driver unload if there is
606          * a non-zero delta.
607          */
608         u64 ibdeltainprog;
609         u64 ibsymdelta;
610         u64 ibsymsnap;
611         u64 iblnkerrdelta;
612         u64 iblnkerrsnap;
613         u64 iblnkdownsnap;
614         u64 iblnkdowndelta;
615         u64 ibmalfdelta;
616         u64 ibmalfsnap;
617         u64 ibcctrl_a; /* krp_ibcctrl_a shadow */
618         u64 ibcctrl_b; /* krp_ibcctrl_b shadow */
619         unsigned long qdr_dfe_time;
620         unsigned long chase_end;
621         u32 autoneg_tries;
622         u32 recovery_init;
623         u32 qdr_dfe_on;
624         u32 qdr_reforce;
625         /*
626          * Per-bay per-channel rcv QMH H1 values and Tx values for QDR.
627          * entry zero is unused, to simplify indexing
628          */
629         u8 h1_val;
630         u8 no_eep;  /* txselect table index to use if no qsfp info */
631         u8 ipg_tries;
632         u8 ibmalfusesnap;
633         struct qib_qsfp_data qsfp_data;
634         char epmsgbuf[192]; /* for port error interrupt msg buffer */
635 };
636
637 static struct {
638         const char *name;
639         irq_handler_t handler;
640         int lsb;
641         int port; /* 0 if not port-specific, else port # */
642 } irq_table[] = {
643         { "", qib_7322intr, -1, 0 },
644         { " (buf avail)", qib_7322bufavail,
645                 SYM_LSB(IntStatus, SendBufAvail), 0 },
646         { " (sdma 0)", sdma_intr,
647                 SYM_LSB(IntStatus, SDmaInt_0), 1 },
648         { " (sdma 1)", sdma_intr,
649                 SYM_LSB(IntStatus, SDmaInt_1), 2 },
650         { " (sdmaI 0)", sdma_idle_intr,
651                 SYM_LSB(IntStatus, SDmaIdleInt_0), 1 },
652         { " (sdmaI 1)", sdma_idle_intr,
653                 SYM_LSB(IntStatus, SDmaIdleInt_1), 2 },
654         { " (sdmaP 0)", sdma_progress_intr,
655                 SYM_LSB(IntStatus, SDmaProgressInt_0), 1 },
656         { " (sdmaP 1)", sdma_progress_intr,
657                 SYM_LSB(IntStatus, SDmaProgressInt_1), 2 },
658         { " (sdmaC 0)", sdma_cleanup_intr,
659                 SYM_LSB(IntStatus, SDmaCleanupDone_0), 1 },
660         { " (sdmaC 1)", sdma_cleanup_intr,
661                 SYM_LSB(IntStatus, SDmaCleanupDone_1), 2 },
662 };
663
664 /* ibcctrl bits */
665 #define QLOGIC_IB_IBCC_LINKINITCMD_DISABLE 1
666 /* cycle through TS1/TS2 till OK */
667 #define QLOGIC_IB_IBCC_LINKINITCMD_POLL 2
668 /* wait for TS1, then go on */
669 #define QLOGIC_IB_IBCC_LINKINITCMD_SLEEP 3
670 #define QLOGIC_IB_IBCC_LINKINITCMD_SHIFT 16
671
672 #define QLOGIC_IB_IBCC_LINKCMD_DOWN 1           /* move to 0x11 */
673 #define QLOGIC_IB_IBCC_LINKCMD_ARMED 2          /* move to 0x21 */
674 #define QLOGIC_IB_IBCC_LINKCMD_ACTIVE 3 /* move to 0x31 */
675
676 #define BLOB_7322_IBCHG 0x101
677
678 static inline void qib_write_kreg(const struct qib_devdata *dd,
679                                   const u32 regno, u64 value);
680 static inline u32 qib_read_kreg32(const struct qib_devdata *, const u32);
681 static void write_7322_initregs(struct qib_devdata *);
682 static void write_7322_init_portregs(struct qib_pportdata *);
683 static void setup_7322_link_recovery(struct qib_pportdata *, u32);
684 static void check_7322_rxe_status(struct qib_pportdata *);
685 static u32 __iomem *qib_7322_getsendbuf(struct qib_pportdata *, u64, u32 *);
686
687 /**
688  * qib_read_ureg32 - read 32-bit virtualized per-context register
689  * @dd: device
690  * @regno: register number
691  * @ctxt: context number
692  *
693  * Return the contents of a register that is virtualized to be per context.
694  * Returns -1 on errors (not distinguishable from valid contents at
695  * runtime; we may add a separate error variable at some point).
696  */
697 static inline u32 qib_read_ureg32(const struct qib_devdata *dd,
698                                   enum qib_ureg regno, int ctxt)
699 {
700         if (!dd->kregbase || !(dd->flags & QIB_PRESENT))
701                 return 0;
702         return readl(regno + (u64 __iomem *)(
703                 (dd->ureg_align * ctxt) + (dd->userbase ?
704                  (char __iomem *)dd->userbase :
705                  (char __iomem *)dd->kregbase + dd->uregbase)));
706 }
707
708 /**
709  * qib_read_ureg - read virtualized per-context register
710  * @dd: device
711  * @regno: register number
712  * @ctxt: context number
713  *
714  * Return the contents of a register that is virtualized to be per context.
715  * Returns -1 on errors (not distinguishable from valid contents at
716  * runtime; we may add a separate error variable at some point).
717  */
718 static inline u64 qib_read_ureg(const struct qib_devdata *dd,
719                                 enum qib_ureg regno, int ctxt)
720 {
721
722         if (!dd->kregbase || !(dd->flags & QIB_PRESENT))
723                 return 0;
724         return readq(regno + (u64 __iomem *)(
725                 (dd->ureg_align * ctxt) + (dd->userbase ?
726                  (char __iomem *)dd->userbase :
727                  (char __iomem *)dd->kregbase + dd->uregbase)));
728 }
729
730 /**
731  * qib_write_ureg - write virtualized per-context register
732  * @dd: device
733  * @regno: register number
734  * @value: value
735  * @ctxt: context
736  *
737  * Write the contents of a register that is virtualized to be per context.
738  */
739 static inline void qib_write_ureg(const struct qib_devdata *dd,
740                                   enum qib_ureg regno, u64 value, int ctxt)
741 {
742         u64 __iomem *ubase;
743         if (dd->userbase)
744                 ubase = (u64 __iomem *)
745                         ((char __iomem *) dd->userbase +
746                          dd->ureg_align * ctxt);
747         else
748                 ubase = (u64 __iomem *)
749                         (dd->uregbase +
750                          (char __iomem *) dd->kregbase +
751                          dd->ureg_align * ctxt);
752
753         if (dd->kregbase && (dd->flags & QIB_PRESENT))
754                 writeq(value, &ubase[regno]);
755 }
756
757 static inline u32 qib_read_kreg32(const struct qib_devdata *dd,
758                                   const u32 regno)
759 {
760         if (!dd->kregbase || !(dd->flags & QIB_PRESENT))
761                 return -1;
762         return readl((u32 __iomem *) &dd->kregbase[regno]);
763 }
764
765 static inline u64 qib_read_kreg64(const struct qib_devdata *dd,
766                                   const u32 regno)
767 {
768         if (!dd->kregbase || !(dd->flags & QIB_PRESENT))
769                 return -1;
770         return readq(&dd->kregbase[regno]);
771 }
772
773 static inline void qib_write_kreg(const struct qib_devdata *dd,
774                                   const u32 regno, u64 value)
775 {
776         if (dd->kregbase && (dd->flags & QIB_PRESENT))
777                 writeq(value, &dd->kregbase[regno]);
778 }
779
780 /*
781  * not many sanity checks for the port-specific kernel register routines,
782  * since they are only used when it's known to be safe.
783 */
784 static inline u64 qib_read_kreg_port(const struct qib_pportdata *ppd,
785                                      const u16 regno)
786 {
787         if (!ppd->cpspec->kpregbase || !(ppd->dd->flags & QIB_PRESENT))
788                 return 0ULL;
789         return readq(&ppd->cpspec->kpregbase[regno]);
790 }
791
792 static inline void qib_write_kreg_port(const struct qib_pportdata *ppd,
793                                        const u16 regno, u64 value)
794 {
795         if (ppd->cpspec && ppd->dd && ppd->cpspec->kpregbase &&
796             (ppd->dd->flags & QIB_PRESENT))
797                 writeq(value, &ppd->cpspec->kpregbase[regno]);
798 }
799
800 /**
801  * qib_write_kreg_ctxt - write a device's per-ctxt 64-bit kernel register
802  * @dd: the qlogic_ib device
803  * @regno: the register number to write
804  * @ctxt: the context containing the register
805  * @value: the value to write
806  */
807 static inline void qib_write_kreg_ctxt(const struct qib_devdata *dd,
808                                        const u16 regno, unsigned ctxt,
809                                        u64 value)
810 {
811         qib_write_kreg(dd, regno + ctxt, value);
812 }
813
814 static inline u64 read_7322_creg(const struct qib_devdata *dd, u16 regno)
815 {
816         if (!dd->cspec->cregbase || !(dd->flags & QIB_PRESENT))
817                 return 0;
818         return readq(&dd->cspec->cregbase[regno]);
819
820
821 }
822
823 static inline u32 read_7322_creg32(const struct qib_devdata *dd, u16 regno)
824 {
825         if (!dd->cspec->cregbase || !(dd->flags & QIB_PRESENT))
826                 return 0;
827         return readl(&dd->cspec->cregbase[regno]);
828
829
830 }
831
832 static inline void write_7322_creg_port(const struct qib_pportdata *ppd,
833                                         u16 regno, u64 value)
834 {
835         if (ppd->cpspec && ppd->cpspec->cpregbase &&
836             (ppd->dd->flags & QIB_PRESENT))
837                 writeq(value, &ppd->cpspec->cpregbase[regno]);
838 }
839
840 static inline u64 read_7322_creg_port(const struct qib_pportdata *ppd,
841                                       u16 regno)
842 {
843         if (!ppd->cpspec || !ppd->cpspec->cpregbase ||
844             !(ppd->dd->flags & QIB_PRESENT))
845                 return 0;
846         return readq(&ppd->cpspec->cpregbase[regno]);
847 }
848
849 static inline u32 read_7322_creg32_port(const struct qib_pportdata *ppd,
850                                         u16 regno)
851 {
852         if (!ppd->cpspec || !ppd->cpspec->cpregbase ||
853             !(ppd->dd->flags & QIB_PRESENT))
854                 return 0;
855         return readl(&ppd->cpspec->cpregbase[regno]);
856 }
857
858 /* bits in Control register */
859 #define QLOGIC_IB_C_RESET SYM_MASK(Control, SyncReset)
860 #define QLOGIC_IB_C_SDMAFETCHPRIOEN SYM_MASK(Control, SDmaDescFetchPriorityEn)
861
862 /* bits in general interrupt regs */
863 #define QIB_I_RCVURG_LSB SYM_LSB(IntMask, RcvUrg0IntMask)
864 #define QIB_I_RCVURG_RMASK MASK_ACROSS(0, 17)
865 #define QIB_I_RCVURG_MASK (QIB_I_RCVURG_RMASK << QIB_I_RCVURG_LSB)
866 #define QIB_I_RCVAVAIL_LSB SYM_LSB(IntMask, RcvAvail0IntMask)
867 #define QIB_I_RCVAVAIL_RMASK MASK_ACROSS(0, 17)
868 #define QIB_I_RCVAVAIL_MASK (QIB_I_RCVAVAIL_RMASK << QIB_I_RCVAVAIL_LSB)
869 #define QIB_I_C_ERROR INT_MASK(Err)
870
871 #define QIB_I_SPIOSENT (INT_MASK_P(SendDone, 0) | INT_MASK_P(SendDone, 1))
872 #define QIB_I_SPIOBUFAVAIL INT_MASK(SendBufAvail)
873 #define QIB_I_GPIO INT_MASK(AssertGPIO)
874 #define QIB_I_P_SDMAINT(pidx) \
875         (INT_MASK_P(SDma, pidx) | INT_MASK_P(SDmaIdle, pidx) | \
876          INT_MASK_P(SDmaProgress, pidx) | \
877          INT_MASK_PM(SDmaCleanupDone, pidx))
878
879 /* Interrupt bits that are "per port" */
880 #define QIB_I_P_BITSEXTANT(pidx) \
881         (INT_MASK_P(Err, pidx) | INT_MASK_P(SendDone, pidx) | \
882         INT_MASK_P(SDma, pidx) | INT_MASK_P(SDmaIdle, pidx) | \
883         INT_MASK_P(SDmaProgress, pidx) | \
884         INT_MASK_PM(SDmaCleanupDone, pidx))
885
886 /* Interrupt bits that are common to a device */
887 /* currently unused: QIB_I_SPIOSENT */
888 #define QIB_I_C_BITSEXTANT \
889         (QIB_I_RCVURG_MASK | QIB_I_RCVAVAIL_MASK | \
890         QIB_I_SPIOSENT | \
891         QIB_I_C_ERROR | QIB_I_SPIOBUFAVAIL | QIB_I_GPIO)
892
893 #define QIB_I_BITSEXTANT (QIB_I_C_BITSEXTANT | \
894         QIB_I_P_BITSEXTANT(0) | QIB_I_P_BITSEXTANT(1))
895
896 /*
897  * Error bits that are "per port".
898  */
899 #define QIB_E_P_IBSTATUSCHANGED ERR_MASK_N(IBStatusChanged)
900 #define QIB_E_P_SHDR ERR_MASK_N(SHeadersErr)
901 #define QIB_E_P_VL15_BUF_MISUSE ERR_MASK_N(VL15BufMisuseErr)
902 #define QIB_E_P_SND_BUF_MISUSE ERR_MASK_N(SendBufMisuseErr)
903 #define QIB_E_P_SUNSUPVL ERR_MASK_N(SendUnsupportedVLErr)
904 #define QIB_E_P_SUNEXP_PKTNUM ERR_MASK_N(SendUnexpectedPktNumErr)
905 #define QIB_E_P_SDROP_DATA ERR_MASK_N(SendDroppedDataPktErr)
906 #define QIB_E_P_SDROP_SMP ERR_MASK_N(SendDroppedSmpPktErr)
907 #define QIB_E_P_SPKTLEN ERR_MASK_N(SendPktLenErr)
908 #define QIB_E_P_SUNDERRUN ERR_MASK_N(SendUnderRunErr)
909 #define QIB_E_P_SMAXPKTLEN ERR_MASK_N(SendMaxPktLenErr)
910 #define QIB_E_P_SMINPKTLEN ERR_MASK_N(SendMinPktLenErr)
911 #define QIB_E_P_RIBLOSTLINK ERR_MASK_N(RcvIBLostLinkErr)
912 #define QIB_E_P_RHDR ERR_MASK_N(RcvHdrErr)
913 #define QIB_E_P_RHDRLEN ERR_MASK_N(RcvHdrLenErr)
914 #define QIB_E_P_RBADTID ERR_MASK_N(RcvBadTidErr)
915 #define QIB_E_P_RBADVERSION ERR_MASK_N(RcvBadVersionErr)
916 #define QIB_E_P_RIBFLOW ERR_MASK_N(RcvIBFlowErr)
917 #define QIB_E_P_REBP ERR_MASK_N(RcvEBPErr)
918 #define QIB_E_P_RUNSUPVL ERR_MASK_N(RcvUnsupportedVLErr)
919 #define QIB_E_P_RUNEXPCHAR ERR_MASK_N(RcvUnexpectedCharErr)
920 #define QIB_E_P_RSHORTPKTLEN ERR_MASK_N(RcvShortPktLenErr)
921 #define QIB_E_P_RLONGPKTLEN ERR_MASK_N(RcvLongPktLenErr)
922 #define QIB_E_P_RMAXPKTLEN ERR_MASK_N(RcvMaxPktLenErr)
923 #define QIB_E_P_RMINPKTLEN ERR_MASK_N(RcvMinPktLenErr)
924 #define QIB_E_P_RICRC ERR_MASK_N(RcvICRCErr)
925 #define QIB_E_P_RVCRC ERR_MASK_N(RcvVCRCErr)
926 #define QIB_E_P_RFORMATERR ERR_MASK_N(RcvFormatErr)
927
928 #define QIB_E_P_SDMA1STDESC ERR_MASK_N(SDma1stDescErr)
929 #define QIB_E_P_SDMABASE ERR_MASK_N(SDmaBaseErr)
930 #define QIB_E_P_SDMADESCADDRMISALIGN ERR_MASK_N(SDmaDescAddrMisalignErr)
931 #define QIB_E_P_SDMADWEN ERR_MASK_N(SDmaDwEnErr)
932 #define QIB_E_P_SDMAGENMISMATCH ERR_MASK_N(SDmaGenMismatchErr)
933 #define QIB_E_P_SDMAHALT ERR_MASK_N(SDmaHaltErr)
934 #define QIB_E_P_SDMAMISSINGDW ERR_MASK_N(SDmaMissingDwErr)
935 #define QIB_E_P_SDMAOUTOFBOUND ERR_MASK_N(SDmaOutOfBoundErr)
936 #define QIB_E_P_SDMARPYTAG ERR_MASK_N(SDmaRpyTagErr)
937 #define QIB_E_P_SDMATAILOUTOFBOUND ERR_MASK_N(SDmaTailOutOfBoundErr)
938 #define QIB_E_P_SDMAUNEXPDATA ERR_MASK_N(SDmaUnexpDataErr)
939
940 /* Error bits that are common to a device */
941 #define QIB_E_RESET ERR_MASK(ResetNegated)
942 #define QIB_E_HARDWARE ERR_MASK(HardwareErr)
943 #define QIB_E_INVALIDADDR ERR_MASK(InvalidAddrErr)
944
945
946 /*
947  * Per chip (rather than per-port) errors.  Most either do
948  * nothing but trigger a print (because they self-recover, or
949  * always occur in tandem with other errors that handle the
950  * issue), or because they indicate errors with no recovery,
951  * but we want to know that they happened.
952  */
953 #define QIB_E_SBUF_VL15_MISUSE ERR_MASK(SBufVL15MisUseErr)
954 #define QIB_E_BADEEP ERR_MASK(InvalidEEPCmd)
955 #define QIB_E_VLMISMATCH ERR_MASK(SendVLMismatchErr)
956 #define QIB_E_ARMLAUNCH ERR_MASK(SendArmLaunchErr)
957 #define QIB_E_SPCLTRIG ERR_MASK(SendSpecialTriggerErr)
958 #define QIB_E_RRCVHDRFULL ERR_MASK(RcvHdrFullErr)
959 #define QIB_E_RRCVEGRFULL ERR_MASK(RcvEgrFullErr)
960 #define QIB_E_RCVCTXTSHARE ERR_MASK(RcvContextShareErr)
961
962 /* SDMA chip errors (not per port)
963  * QIB_E_SDMA_BUF_DUP needs no special handling, because we will also get
964  * the SDMAHALT error immediately, so we just print the dup error via the
965  * E_AUTO mechanism.  This is true of most of the per-port fatal errors
966  * as well, but since this is port-independent, by definition, it's
967  * handled a bit differently.  SDMA_VL15 and SDMA_WRONG_PORT are per
968  * packet send errors, and so are handled in the same manner as other
969  * per-packet errors.
970  */
971 #define QIB_E_SDMA_VL15 ERR_MASK(SDmaVL15Err)
972 #define QIB_E_SDMA_WRONG_PORT ERR_MASK(SDmaWrongPortErr)
973 #define QIB_E_SDMA_BUF_DUP ERR_MASK(SDmaBufMaskDuplicateErr)
974
975 /*
976  * Below functionally equivalent to legacy QLOGIC_IB_E_PKTERRS
977  * it is used to print "common" packet errors.
978  */
979 #define QIB_E_P_PKTERRS (QIB_E_P_SPKTLEN |\
980         QIB_E_P_SDROP_DATA | QIB_E_P_RVCRC |\
981         QIB_E_P_RICRC | QIB_E_P_RSHORTPKTLEN |\
982         QIB_E_P_VL15_BUF_MISUSE | QIB_E_P_SHDR | \
983         QIB_E_P_REBP)
984
985 /* Error Bits that Packet-related (Receive, per-port) */
986 #define QIB_E_P_RPKTERRS (\
987         QIB_E_P_RHDRLEN | QIB_E_P_RBADTID | \
988         QIB_E_P_RBADVERSION | QIB_E_P_RHDR | \
989         QIB_E_P_RLONGPKTLEN | QIB_E_P_RSHORTPKTLEN |\
990         QIB_E_P_RMAXPKTLEN | QIB_E_P_RMINPKTLEN | \
991         QIB_E_P_RFORMATERR | QIB_E_P_RUNSUPVL | \
992         QIB_E_P_RUNEXPCHAR | QIB_E_P_RIBFLOW | QIB_E_P_REBP)
993
994 /*
995  * Error bits that are Send-related (per port)
996  * (ARMLAUNCH excluded from E_SPKTERRS because it gets special handling).
997  * All of these potentially need to have a buffer disarmed
998  */
999 #define QIB_E_P_SPKTERRS (\
1000         QIB_E_P_SUNEXP_PKTNUM |\
1001         QIB_E_P_SDROP_DATA | QIB_E_P_SDROP_SMP |\
1002         QIB_E_P_SMAXPKTLEN |\
1003         QIB_E_P_VL15_BUF_MISUSE | QIB_E_P_SHDR | \
1004         QIB_E_P_SMINPKTLEN | QIB_E_P_SPKTLEN | \
1005         QIB_E_P_SND_BUF_MISUSE | QIB_E_P_SUNSUPVL)
1006
1007 #define QIB_E_SPKTERRS ( \
1008                 QIB_E_SBUF_VL15_MISUSE | QIB_E_VLMISMATCH | \
1009                 ERR_MASK_N(SendUnsupportedVLErr) |                      \
1010                 QIB_E_SPCLTRIG | QIB_E_SDMA_VL15 | QIB_E_SDMA_WRONG_PORT)
1011
1012 #define QIB_E_P_SDMAERRS ( \
1013         QIB_E_P_SDMAHALT | \
1014         QIB_E_P_SDMADESCADDRMISALIGN | \
1015         QIB_E_P_SDMAUNEXPDATA | \
1016         QIB_E_P_SDMAMISSINGDW | \
1017         QIB_E_P_SDMADWEN | \
1018         QIB_E_P_SDMARPYTAG | \
1019         QIB_E_P_SDMA1STDESC | \
1020         QIB_E_P_SDMABASE | \
1021         QIB_E_P_SDMATAILOUTOFBOUND | \
1022         QIB_E_P_SDMAOUTOFBOUND | \
1023         QIB_E_P_SDMAGENMISMATCH)
1024
1025 /*
1026  * This sets some bits more than once, but makes it more obvious which
1027  * bits are not handled under other categories, and the repeat definition
1028  * is not a problem.
1029  */
1030 #define QIB_E_P_BITSEXTANT ( \
1031         QIB_E_P_SPKTERRS | QIB_E_P_PKTERRS | QIB_E_P_RPKTERRS | \
1032         QIB_E_P_RIBLOSTLINK | QIB_E_P_IBSTATUSCHANGED | \
1033         QIB_E_P_SND_BUF_MISUSE | QIB_E_P_SUNDERRUN | \
1034         QIB_E_P_SHDR | QIB_E_P_VL15_BUF_MISUSE | QIB_E_P_SDMAERRS \
1035         )
1036
1037 /*
1038  * These are errors that can occur when the link
1039  * changes state while a packet is being sent or received.  This doesn't
1040  * cover things like EBP or VCRC that can be the result of a sending
1041  * having the link change state, so we receive a "known bad" packet.
1042  * All of these are "per port", so renamed:
1043  */
1044 #define QIB_E_P_LINK_PKTERRS (\
1045         QIB_E_P_SDROP_DATA | QIB_E_P_SDROP_SMP |\
1046         QIB_E_P_SMINPKTLEN | QIB_E_P_SPKTLEN |\
1047         QIB_E_P_RSHORTPKTLEN | QIB_E_P_RMINPKTLEN |\
1048         QIB_E_P_RUNEXPCHAR)
1049
1050 /*
1051  * This sets some bits more than once, but makes it more obvious which
1052  * bits are not handled under other categories (such as QIB_E_SPKTERRS),
1053  * and the repeat definition is not a problem.
1054  */
1055 #define QIB_E_C_BITSEXTANT (\
1056         QIB_E_HARDWARE | QIB_E_INVALIDADDR | QIB_E_BADEEP |\
1057         QIB_E_ARMLAUNCH | QIB_E_VLMISMATCH | QIB_E_RRCVHDRFULL |\
1058         QIB_E_RRCVEGRFULL | QIB_E_RESET | QIB_E_SBUF_VL15_MISUSE)
1059
1060 /* Likewise Neuter E_SPKT_ERRS_IGNORE */
1061 #define E_SPKT_ERRS_IGNORE 0
1062
1063 #define QIB_EXTS_MEMBIST_DISABLED \
1064         SYM_MASK(EXTStatus, MemBISTDisabled)
1065 #define QIB_EXTS_MEMBIST_ENDTEST \
1066         SYM_MASK(EXTStatus, MemBISTEndTest)
1067
1068 #define QIB_E_SPIOARMLAUNCH \
1069         ERR_MASK(SendArmLaunchErr)
1070
1071 #define IBA7322_IBCC_LINKINITCMD_MASK SYM_RMASK(IBCCtrlA_0, LinkInitCmd)
1072 #define IBA7322_IBCC_LINKCMD_SHIFT SYM_LSB(IBCCtrlA_0, LinkCmd)
1073
1074 /*
1075  * IBTA_1_2 is set when multiple speeds are enabled (normal),
1076  * and also if forced QDR (only QDR enabled).  It's enabled for the
1077  * forced QDR case so that scrambling will be enabled by the TS3
1078  * exchange, when supported by both sides of the link.
1079  */
1080 #define IBA7322_IBC_IBTA_1_2_MASK SYM_MASK(IBCCtrlB_0, IB_ENHANCED_MODE)
1081 #define IBA7322_IBC_MAX_SPEED_MASK SYM_MASK(IBCCtrlB_0, SD_SPEED)
1082 #define IBA7322_IBC_SPEED_QDR SYM_MASK(IBCCtrlB_0, SD_SPEED_QDR)
1083 #define IBA7322_IBC_SPEED_DDR SYM_MASK(IBCCtrlB_0, SD_SPEED_DDR)
1084 #define IBA7322_IBC_SPEED_SDR SYM_MASK(IBCCtrlB_0, SD_SPEED_SDR)
1085 #define IBA7322_IBC_SPEED_MASK (SYM_MASK(IBCCtrlB_0, SD_SPEED_SDR) | \
1086         SYM_MASK(IBCCtrlB_0, SD_SPEED_DDR) | SYM_MASK(IBCCtrlB_0, SD_SPEED_QDR))
1087 #define IBA7322_IBC_SPEED_LSB SYM_LSB(IBCCtrlB_0, SD_SPEED_SDR)
1088
1089 #define IBA7322_LEDBLINK_OFF_SHIFT SYM_LSB(RcvPktLEDCnt_0, OFFperiod)
1090 #define IBA7322_LEDBLINK_ON_SHIFT SYM_LSB(RcvPktLEDCnt_0, ONperiod)
1091
1092 #define IBA7322_IBC_WIDTH_AUTONEG SYM_MASK(IBCCtrlB_0, IB_NUM_CHANNELS)
1093 #define IBA7322_IBC_WIDTH_4X_ONLY (1<<SYM_LSB(IBCCtrlB_0, IB_NUM_CHANNELS))
1094 #define IBA7322_IBC_WIDTH_1X_ONLY (0<<SYM_LSB(IBCCtrlB_0, IB_NUM_CHANNELS))
1095
1096 #define IBA7322_IBC_RXPOL_MASK SYM_MASK(IBCCtrlB_0, IB_POLARITY_REV_SUPP)
1097 #define IBA7322_IBC_RXPOL_LSB SYM_LSB(IBCCtrlB_0, IB_POLARITY_REV_SUPP)
1098 #define IBA7322_IBC_HRTBT_MASK (SYM_MASK(IBCCtrlB_0, HRTBT_AUTO) | \
1099         SYM_MASK(IBCCtrlB_0, HRTBT_ENB))
1100 #define IBA7322_IBC_HRTBT_RMASK (IBA7322_IBC_HRTBT_MASK >> \
1101         SYM_LSB(IBCCtrlB_0, HRTBT_ENB))
1102 #define IBA7322_IBC_HRTBT_LSB SYM_LSB(IBCCtrlB_0, HRTBT_ENB)
1103
1104 #define IBA7322_REDIRECT_VEC_PER_REG 12
1105
1106 #define IBA7322_SENDCHK_PKEY SYM_MASK(SendCheckControl_0, PKey_En)
1107 #define IBA7322_SENDCHK_BTHQP SYM_MASK(SendCheckControl_0, BTHQP_En)
1108 #define IBA7322_SENDCHK_SLID SYM_MASK(SendCheckControl_0, SLID_En)
1109 #define IBA7322_SENDCHK_RAW_IPV6 SYM_MASK(SendCheckControl_0, RawIPV6_En)
1110 #define IBA7322_SENDCHK_MINSZ SYM_MASK(SendCheckControl_0, PacketTooSmall_En)
1111
1112 #define AUTONEG_TRIES 3 /* sequential retries to negotiate DDR */
1113
1114 #define HWE_AUTO(fldname) { .mask = SYM_MASK(HwErrMask, fldname##Mask), \
1115         .msg = #fldname , .sz = sizeof(#fldname) }
1116 #define HWE_AUTO_P(fldname, port) { .mask = SYM_MASK(HwErrMask, \
1117         fldname##Mask##_##port), .msg = #fldname , .sz = sizeof(#fldname) }
1118 static const struct qib_hwerror_msgs qib_7322_hwerror_msgs[] = {
1119         HWE_AUTO_P(IBSerdesPClkNotDetect, 1),
1120         HWE_AUTO_P(IBSerdesPClkNotDetect, 0),
1121         HWE_AUTO(PCIESerdesPClkNotDetect),
1122         HWE_AUTO(PowerOnBISTFailed),
1123         HWE_AUTO(TempsenseTholdReached),
1124         HWE_AUTO(MemoryErr),
1125         HWE_AUTO(PCIeBusParityErr),
1126         HWE_AUTO(PcieCplTimeout),
1127         HWE_AUTO(PciePoisonedTLP),
1128         HWE_AUTO_P(SDmaMemReadErr, 1),
1129         HWE_AUTO_P(SDmaMemReadErr, 0),
1130         HWE_AUTO_P(IBCBusFromSPCParityErr, 1),
1131         HWE_AUTO_P(IBCBusToSPCParityErr, 1),
1132         HWE_AUTO_P(IBCBusFromSPCParityErr, 0),
1133         HWE_AUTO(statusValidNoEop),
1134         HWE_AUTO(LATriggered),
1135         { .mask = 0, .sz = 0 }
1136 };
1137
1138 #define E_AUTO(fldname) { .mask = SYM_MASK(ErrMask, fldname##Mask), \
1139         .msg = #fldname, .sz = sizeof(#fldname) }
1140 #define E_P_AUTO(fldname) { .mask = SYM_MASK(ErrMask_0, fldname##Mask), \
1141         .msg = #fldname, .sz = sizeof(#fldname) }
1142 static const struct qib_hwerror_msgs qib_7322error_msgs[] = {
1143         E_AUTO(RcvEgrFullErr),
1144         E_AUTO(RcvHdrFullErr),
1145         E_AUTO(ResetNegated),
1146         E_AUTO(HardwareErr),
1147         E_AUTO(InvalidAddrErr),
1148         E_AUTO(SDmaVL15Err),
1149         E_AUTO(SBufVL15MisUseErr),
1150         E_AUTO(InvalidEEPCmd),
1151         E_AUTO(RcvContextShareErr),
1152         E_AUTO(SendVLMismatchErr),
1153         E_AUTO(SendArmLaunchErr),
1154         E_AUTO(SendSpecialTriggerErr),
1155         E_AUTO(SDmaWrongPortErr),
1156         E_AUTO(SDmaBufMaskDuplicateErr),
1157         { .mask = 0, .sz = 0 }
1158 };
1159
1160 static const struct  qib_hwerror_msgs qib_7322p_error_msgs[] = {
1161         E_P_AUTO(IBStatusChanged),
1162         E_P_AUTO(SHeadersErr),
1163         E_P_AUTO(VL15BufMisuseErr),
1164         /*
1165          * SDmaHaltErr is not really an error, make it clearer;
1166          */
1167         {.mask = SYM_MASK(ErrMask_0, SDmaHaltErrMask), .msg = "SDmaHalted",
1168                 .sz = 11},
1169         E_P_AUTO(SDmaDescAddrMisalignErr),
1170         E_P_AUTO(SDmaUnexpDataErr),
1171         E_P_AUTO(SDmaMissingDwErr),
1172         E_P_AUTO(SDmaDwEnErr),
1173         E_P_AUTO(SDmaRpyTagErr),
1174         E_P_AUTO(SDma1stDescErr),
1175         E_P_AUTO(SDmaBaseErr),
1176         E_P_AUTO(SDmaTailOutOfBoundErr),
1177         E_P_AUTO(SDmaOutOfBoundErr),
1178         E_P_AUTO(SDmaGenMismatchErr),
1179         E_P_AUTO(SendBufMisuseErr),
1180         E_P_AUTO(SendUnsupportedVLErr),
1181         E_P_AUTO(SendUnexpectedPktNumErr),
1182         E_P_AUTO(SendDroppedDataPktErr),
1183         E_P_AUTO(SendDroppedSmpPktErr),
1184         E_P_AUTO(SendPktLenErr),
1185         E_P_AUTO(SendUnderRunErr),
1186         E_P_AUTO(SendMaxPktLenErr),
1187         E_P_AUTO(SendMinPktLenErr),
1188         E_P_AUTO(RcvIBLostLinkErr),
1189         E_P_AUTO(RcvHdrErr),
1190         E_P_AUTO(RcvHdrLenErr),
1191         E_P_AUTO(RcvBadTidErr),
1192         E_P_AUTO(RcvBadVersionErr),
1193         E_P_AUTO(RcvIBFlowErr),
1194         E_P_AUTO(RcvEBPErr),
1195         E_P_AUTO(RcvUnsupportedVLErr),
1196         E_P_AUTO(RcvUnexpectedCharErr),
1197         E_P_AUTO(RcvShortPktLenErr),
1198         E_P_AUTO(RcvLongPktLenErr),
1199         E_P_AUTO(RcvMaxPktLenErr),
1200         E_P_AUTO(RcvMinPktLenErr),
1201         E_P_AUTO(RcvICRCErr),
1202         E_P_AUTO(RcvVCRCErr),
1203         E_P_AUTO(RcvFormatErr),
1204         { .mask = 0, .sz = 0 }
1205 };
1206
1207 /*
1208  * Below generates "auto-message" for interrupts not specific to any port or
1209  * context
1210  */
1211 #define INTR_AUTO(fldname) { .mask = SYM_MASK(IntMask, fldname##Mask), \
1212         .msg = #fldname, .sz = sizeof(#fldname) }
1213 /* Below generates "auto-message" for interrupts specific to a port */
1214 #define INTR_AUTO_P(fldname) { .mask = MASK_ACROSS(\
1215         SYM_LSB(IntMask, fldname##Mask##_0), \
1216         SYM_LSB(IntMask, fldname##Mask##_1)), \
1217         .msg = #fldname "_P", .sz = sizeof(#fldname "_P") }
1218 /* For some reason, the SerDesTrimDone bits are reversed */
1219 #define INTR_AUTO_PI(fldname) { .mask = MASK_ACROSS(\
1220         SYM_LSB(IntMask, fldname##Mask##_1), \
1221         SYM_LSB(IntMask, fldname##Mask##_0)), \
1222         .msg = #fldname "_P", .sz = sizeof(#fldname "_P") }
1223 /*
1224  * Below generates "auto-message" for interrupts specific to a context,
1225  * with ctxt-number appended
1226  */
1227 #define INTR_AUTO_C(fldname) { .mask = MASK_ACROSS(\
1228         SYM_LSB(IntMask, fldname##0IntMask), \
1229         SYM_LSB(IntMask, fldname##17IntMask)), \
1230         .msg = #fldname "_C", .sz = sizeof(#fldname "_C") }
1231
1232 static const struct  qib_hwerror_msgs qib_7322_intr_msgs[] = {
1233         INTR_AUTO_P(SDmaInt),
1234         INTR_AUTO_P(SDmaProgressInt),
1235         INTR_AUTO_P(SDmaIdleInt),
1236         INTR_AUTO_P(SDmaCleanupDone),
1237         INTR_AUTO_C(RcvUrg),
1238         INTR_AUTO_P(ErrInt),
1239         INTR_AUTO(ErrInt),      /* non-port-specific errs */
1240         INTR_AUTO(AssertGPIOInt),
1241         INTR_AUTO_P(SendDoneInt),
1242         INTR_AUTO(SendBufAvailInt),
1243         INTR_AUTO_C(RcvAvail),
1244         { .mask = 0, .sz = 0 }
1245 };
1246
1247 #define TXSYMPTOM_AUTO_P(fldname) \
1248         { .mask = SYM_MASK(SendHdrErrSymptom_0, fldname), \
1249         .msg = #fldname, .sz = sizeof(#fldname) }
1250 static const struct  qib_hwerror_msgs hdrchk_msgs[] = {
1251         TXSYMPTOM_AUTO_P(NonKeyPacket),
1252         TXSYMPTOM_AUTO_P(GRHFail),
1253         TXSYMPTOM_AUTO_P(PkeyFail),
1254         TXSYMPTOM_AUTO_P(QPFail),
1255         TXSYMPTOM_AUTO_P(SLIDFail),
1256         TXSYMPTOM_AUTO_P(RawIPV6),
1257         TXSYMPTOM_AUTO_P(PacketTooSmall),
1258         { .mask = 0, .sz = 0 }
1259 };
1260
1261 #define IBA7322_HDRHEAD_PKTINT_SHIFT 32 /* interrupt cnt in upper 32 bits */
1262
1263 /*
1264  * Called when we might have an error that is specific to a particular
1265  * PIO buffer, and may need to cancel that buffer, so it can be re-used,
1266  * because we don't need to force the update of pioavail
1267  */
1268 static void qib_disarm_7322_senderrbufs(struct qib_pportdata *ppd)
1269 {
1270         struct qib_devdata *dd = ppd->dd;
1271         u32 i;
1272         int any;
1273         u32 piobcnt = dd->piobcnt2k + dd->piobcnt4k + NUM_VL15_BUFS;
1274         u32 regcnt = (piobcnt + BITS_PER_LONG - 1) / BITS_PER_LONG;
1275         unsigned long sbuf[4];
1276
1277         /*
1278          * It's possible that sendbuffererror could have bits set; might
1279          * have already done this as a result of hardware error handling.
1280          */
1281         any = 0;
1282         for (i = 0; i < regcnt; ++i) {
1283                 sbuf[i] = qib_read_kreg64(dd, kr_sendbuffererror + i);
1284                 if (sbuf[i]) {
1285                         any = 1;
1286                         qib_write_kreg(dd, kr_sendbuffererror + i, sbuf[i]);
1287                 }
1288         }
1289
1290         if (any)
1291                 qib_disarm_piobufs_set(dd, sbuf, piobcnt);
1292 }
1293
1294 /* No txe_recover yet, if ever */
1295
1296 /* No decode__errors yet */
1297 static void err_decode(char *msg, size_t len, u64 errs,
1298                        const struct qib_hwerror_msgs *msp)
1299 {
1300         u64 these, lmask;
1301         int took, multi, n = 0;
1302
1303         while (errs && msp && msp->mask) {
1304                 multi = (msp->mask & (msp->mask - 1));
1305                 while (errs & msp->mask) {
1306                         these = (errs & msp->mask);
1307                         lmask = (these & (these - 1)) ^ these;
1308                         if (len) {
1309                                 if (n++) {
1310                                         /* separate the strings */
1311                                         *msg++ = ',';
1312                                         len--;
1313                                 }
1314                                 BUG_ON(!msp->sz);
1315                                 /* msp->sz counts the nul */
1316                                 took = min_t(size_t, msp->sz - (size_t)1, len);
1317                                 memcpy(msg,  msp->msg, took);
1318                                 len -= took;
1319                                 msg += took;
1320                                 if (len)
1321                                         *msg = '\0';
1322                         }
1323                         errs &= ~lmask;
1324                         if (len && multi) {
1325                                 /* More than one bit this mask */
1326                                 int idx = -1;
1327
1328                                 while (lmask & msp->mask) {
1329                                         ++idx;
1330                                         lmask >>= 1;
1331                                 }
1332                                 took = scnprintf(msg, len, "_%d", idx);
1333                                 len -= took;
1334                                 msg += took;
1335                         }
1336                 }
1337                 ++msp;
1338         }
1339         /* If some bits are left, show in hex. */
1340         if (len && errs)
1341                 snprintf(msg, len, "%sMORE:%llX", n ? "," : "",
1342                         (unsigned long long) errs);
1343 }
1344
1345 /* only called if r1 set */
1346 static void flush_fifo(struct qib_pportdata *ppd)
1347 {
1348         struct qib_devdata *dd = ppd->dd;
1349         u32 __iomem *piobuf;
1350         u32 bufn;
1351         u32 *hdr;
1352         u64 pbc;
1353         const unsigned hdrwords = 7;
1354         static struct qib_ib_header ibhdr = {
1355                 .lrh[0] = cpu_to_be16(0xF000 | QIB_LRH_BTH),
1356                 .lrh[1] = IB_LID_PERMISSIVE,
1357                 .lrh[2] = cpu_to_be16(hdrwords + SIZE_OF_CRC),
1358                 .lrh[3] = IB_LID_PERMISSIVE,
1359                 .u.oth.bth[0] = cpu_to_be32(
1360                         (IB_OPCODE_UD_SEND_ONLY << 24) | QIB_DEFAULT_P_KEY),
1361                 .u.oth.bth[1] = cpu_to_be32(0),
1362                 .u.oth.bth[2] = cpu_to_be32(0),
1363                 .u.oth.u.ud.deth[0] = cpu_to_be32(0),
1364                 .u.oth.u.ud.deth[1] = cpu_to_be32(0),
1365         };
1366
1367         /*
1368          * Send a dummy VL15 packet to flush the launch FIFO.
1369          * This will not actually be sent since the TxeBypassIbc bit is set.
1370          */
1371         pbc = PBC_7322_VL15_SEND |
1372                 (((u64)ppd->hw_pidx) << (PBC_PORT_SEL_LSB + 32)) |
1373                 (hdrwords + SIZE_OF_CRC);
1374         piobuf = qib_7322_getsendbuf(ppd, pbc, &bufn);
1375         if (!piobuf)
1376                 return;
1377         writeq(pbc, piobuf);
1378         hdr = (u32 *) &ibhdr;
1379         if (dd->flags & QIB_PIO_FLUSH_WC) {
1380                 qib_flush_wc();
1381                 qib_pio_copy(piobuf + 2, hdr, hdrwords - 1);
1382                 qib_flush_wc();
1383                 __raw_writel(hdr[hdrwords - 1], piobuf + hdrwords + 1);
1384                 qib_flush_wc();
1385         } else
1386                 qib_pio_copy(piobuf + 2, hdr, hdrwords);
1387         qib_sendbuf_done(dd, bufn);
1388 }
1389
1390 /*
1391  * This is called with interrupts disabled and sdma_lock held.
1392  */
1393 static void qib_7322_sdma_sendctrl(struct qib_pportdata *ppd, unsigned op)
1394 {
1395         struct qib_devdata *dd = ppd->dd;
1396         u64 set_sendctrl = 0;
1397         u64 clr_sendctrl = 0;
1398
1399         if (op & QIB_SDMA_SENDCTRL_OP_ENABLE)
1400                 set_sendctrl |= SYM_MASK(SendCtrl_0, SDmaEnable);
1401         else
1402                 clr_sendctrl |= SYM_MASK(SendCtrl_0, SDmaEnable);
1403
1404         if (op & QIB_SDMA_SENDCTRL_OP_INTENABLE)
1405                 set_sendctrl |= SYM_MASK(SendCtrl_0, SDmaIntEnable);
1406         else
1407                 clr_sendctrl |= SYM_MASK(SendCtrl_0, SDmaIntEnable);
1408
1409         if (op & QIB_SDMA_SENDCTRL_OP_HALT)
1410                 set_sendctrl |= SYM_MASK(SendCtrl_0, SDmaHalt);
1411         else
1412                 clr_sendctrl |= SYM_MASK(SendCtrl_0, SDmaHalt);
1413
1414         if (op & QIB_SDMA_SENDCTRL_OP_DRAIN)
1415                 set_sendctrl |= SYM_MASK(SendCtrl_0, TxeBypassIbc) |
1416                                 SYM_MASK(SendCtrl_0, TxeAbortIbc) |
1417                                 SYM_MASK(SendCtrl_0, TxeDrainRmFifo);
1418         else
1419                 clr_sendctrl |= SYM_MASK(SendCtrl_0, TxeBypassIbc) |
1420                                 SYM_MASK(SendCtrl_0, TxeAbortIbc) |
1421                                 SYM_MASK(SendCtrl_0, TxeDrainRmFifo);
1422
1423         spin_lock(&dd->sendctrl_lock);
1424
1425         /* If we are draining everything, block sends first */
1426         if (op & QIB_SDMA_SENDCTRL_OP_DRAIN) {
1427                 ppd->p_sendctrl &= ~SYM_MASK(SendCtrl_0, SendEnable);
1428                 qib_write_kreg_port(ppd, krp_sendctrl, ppd->p_sendctrl);
1429                 qib_write_kreg(dd, kr_scratch, 0);
1430         }
1431
1432         ppd->p_sendctrl |= set_sendctrl;
1433         ppd->p_sendctrl &= ~clr_sendctrl;
1434
1435         if (op & QIB_SDMA_SENDCTRL_OP_CLEANUP)
1436                 qib_write_kreg_port(ppd, krp_sendctrl,
1437                                     ppd->p_sendctrl |
1438                                     SYM_MASK(SendCtrl_0, SDmaCleanup));
1439         else
1440                 qib_write_kreg_port(ppd, krp_sendctrl, ppd->p_sendctrl);
1441         qib_write_kreg(dd, kr_scratch, 0);
1442
1443         if (op & QIB_SDMA_SENDCTRL_OP_DRAIN) {
1444                 ppd->p_sendctrl |= SYM_MASK(SendCtrl_0, SendEnable);
1445                 qib_write_kreg_port(ppd, krp_sendctrl, ppd->p_sendctrl);
1446                 qib_write_kreg(dd, kr_scratch, 0);
1447         }
1448
1449         spin_unlock(&dd->sendctrl_lock);
1450
1451         if ((op & QIB_SDMA_SENDCTRL_OP_DRAIN) && ppd->dd->cspec->r1)
1452                 flush_fifo(ppd);
1453 }
1454
1455 static void qib_7322_sdma_hw_clean_up(struct qib_pportdata *ppd)
1456 {
1457         __qib_sdma_process_event(ppd, qib_sdma_event_e50_hw_cleaned);
1458 }
1459
1460 static void qib_sdma_7322_setlengen(struct qib_pportdata *ppd)
1461 {
1462         /*
1463          * Set SendDmaLenGen and clear and set
1464          * the MSB of the generation count to enable generation checking
1465          * and load the internal generation counter.
1466          */
1467         qib_write_kreg_port(ppd, krp_senddmalengen, ppd->sdma_descq_cnt);
1468         qib_write_kreg_port(ppd, krp_senddmalengen,
1469                             ppd->sdma_descq_cnt |
1470                             (1ULL << QIB_7322_SendDmaLenGen_0_Generation_MSB));
1471 }
1472
1473 /*
1474  * Must be called with sdma_lock held, or before init finished.
1475  */
1476 static void qib_sdma_update_7322_tail(struct qib_pportdata *ppd, u16 tail)
1477 {
1478         /* Commit writes to memory and advance the tail on the chip */
1479         wmb();
1480         ppd->sdma_descq_tail = tail;
1481         qib_write_kreg_port(ppd, krp_senddmatail, tail);
1482 }
1483
1484 /*
1485  * This is called with interrupts disabled and sdma_lock held.
1486  */
1487 static void qib_7322_sdma_hw_start_up(struct qib_pportdata *ppd)
1488 {
1489         /*
1490          * Drain all FIFOs.
1491          * The hardware doesn't require this but we do it so that verbs
1492          * and user applications don't wait for link active to send stale
1493          * data.
1494          */
1495         sendctrl_7322_mod(ppd, QIB_SENDCTRL_FLUSH);
1496
1497         qib_sdma_7322_setlengen(ppd);
1498         qib_sdma_update_7322_tail(ppd, 0); /* Set SendDmaTail */
1499         ppd->sdma_head_dma[0] = 0;
1500         qib_7322_sdma_sendctrl(ppd,
1501                 ppd->sdma_state.current_op | QIB_SDMA_SENDCTRL_OP_CLEANUP);
1502 }
1503
1504 #define DISABLES_SDMA ( \
1505         QIB_E_P_SDMAHALT | \
1506         QIB_E_P_SDMADESCADDRMISALIGN | \
1507         QIB_E_P_SDMAMISSINGDW | \
1508         QIB_E_P_SDMADWEN | \
1509         QIB_E_P_SDMARPYTAG | \
1510         QIB_E_P_SDMA1STDESC | \
1511         QIB_E_P_SDMABASE | \
1512         QIB_E_P_SDMATAILOUTOFBOUND | \
1513         QIB_E_P_SDMAOUTOFBOUND | \
1514         QIB_E_P_SDMAGENMISMATCH)
1515
1516 static void sdma_7322_p_errors(struct qib_pportdata *ppd, u64 errs)
1517 {
1518         unsigned long flags;
1519         struct qib_devdata *dd = ppd->dd;
1520
1521         errs &= QIB_E_P_SDMAERRS;
1522
1523         if (errs & QIB_E_P_SDMAUNEXPDATA)
1524                 qib_dev_err(dd, "IB%u:%u SDmaUnexpData\n", dd->unit,
1525                             ppd->port);
1526
1527         spin_lock_irqsave(&ppd->sdma_lock, flags);
1528
1529         switch (ppd->sdma_state.current_state) {
1530         case qib_sdma_state_s00_hw_down:
1531                 break;
1532
1533         case qib_sdma_state_s10_hw_start_up_wait:
1534                 if (errs & QIB_E_P_SDMAHALT)
1535                         __qib_sdma_process_event(ppd,
1536                                 qib_sdma_event_e20_hw_started);
1537                 break;
1538
1539         case qib_sdma_state_s20_idle:
1540                 break;
1541
1542         case qib_sdma_state_s30_sw_clean_up_wait:
1543                 break;
1544
1545         case qib_sdma_state_s40_hw_clean_up_wait:
1546                 if (errs & QIB_E_P_SDMAHALT)
1547                         __qib_sdma_process_event(ppd,
1548                                 qib_sdma_event_e50_hw_cleaned);
1549                 break;
1550
1551         case qib_sdma_state_s50_hw_halt_wait:
1552                 if (errs & QIB_E_P_SDMAHALT)
1553                         __qib_sdma_process_event(ppd,
1554                                 qib_sdma_event_e60_hw_halted);
1555                 break;
1556
1557         case qib_sdma_state_s99_running:
1558                 __qib_sdma_process_event(ppd, qib_sdma_event_e7322_err_halted);
1559                 __qib_sdma_process_event(ppd, qib_sdma_event_e60_hw_halted);
1560                 break;
1561         }
1562
1563         spin_unlock_irqrestore(&ppd->sdma_lock, flags);
1564 }
1565
1566 /*
1567  * handle per-device errors (not per-port errors)
1568  */
1569 static noinline void handle_7322_errors(struct qib_devdata *dd)
1570 {
1571         char *msg;
1572         u64 iserr = 0;
1573         u64 errs;
1574         u64 mask;
1575         int log_idx;
1576
1577         qib_stats.sps_errints++;
1578         errs = qib_read_kreg64(dd, kr_errstatus);
1579         if (!errs) {
1580                 qib_devinfo(dd->pcidev, "device error interrupt, "
1581                          "but no error bits set!\n");
1582                 goto done;
1583         }
1584
1585         /* don't report errors that are masked */
1586         errs &= dd->cspec->errormask;
1587         msg = dd->cspec->emsgbuf;
1588
1589         /* do these first, they are most important */
1590         if (errs & QIB_E_HARDWARE) {
1591                 *msg = '\0';
1592                 qib_7322_handle_hwerrors(dd, msg, sizeof dd->cspec->emsgbuf);
1593         } else
1594                 for (log_idx = 0; log_idx < QIB_EEP_LOG_CNT; ++log_idx)
1595                         if (errs & dd->eep_st_masks[log_idx].errs_to_log)
1596                                 qib_inc_eeprom_err(dd, log_idx, 1);
1597
1598         if (errs & QIB_E_SPKTERRS) {
1599                 qib_disarm_7322_senderrbufs(dd->pport);
1600                 qib_stats.sps_txerrs++;
1601         } else if (errs & QIB_E_INVALIDADDR)
1602                 qib_stats.sps_txerrs++;
1603         else if (errs & QIB_E_ARMLAUNCH) {
1604                 qib_stats.sps_txerrs++;
1605                 qib_disarm_7322_senderrbufs(dd->pport);
1606         }
1607         qib_write_kreg(dd, kr_errclear, errs);
1608
1609         /*
1610          * The ones we mask off are handled specially below
1611          * or above.  Also mask SDMADISABLED by default as it
1612          * is too chatty.
1613          */
1614         mask = QIB_E_HARDWARE;
1615         *msg = '\0';
1616
1617         err_decode(msg, sizeof dd->cspec->emsgbuf, errs & ~mask,
1618                    qib_7322error_msgs);
1619
1620         /*
1621          * Getting reset is a tragedy for all ports. Mark the device
1622          * _and_ the ports as "offline" in way meaningful to each.
1623          */
1624         if (errs & QIB_E_RESET) {
1625                 int pidx;
1626
1627                 qib_dev_err(dd, "Got reset, requires re-init "
1628                             "(unload and reload driver)\n");
1629                 dd->flags &= ~QIB_INITTED;  /* needs re-init */
1630                 /* mark as having had error */
1631                 *dd->devstatusp |= QIB_STATUS_HWERROR;
1632                 for (pidx = 0; pidx < dd->num_pports; ++pidx)
1633                         if (dd->pport[pidx].link_speed_supported)
1634                                 *dd->pport[pidx].statusp &= ~QIB_STATUS_IB_CONF;
1635         }
1636
1637         if (*msg && iserr)
1638                 qib_dev_err(dd, "%s error\n", msg);
1639
1640         /*
1641          * If there were hdrq or egrfull errors, wake up any processes
1642          * waiting in poll.  We used to try to check which contexts had
1643          * the overflow, but given the cost of that and the chip reads
1644          * to support it, it's better to just wake everybody up if we
1645          * get an overflow; waiters can poll again if it's not them.
1646          */
1647         if (errs & (ERR_MASK(RcvEgrFullErr) | ERR_MASK(RcvHdrFullErr))) {
1648                 qib_handle_urcv(dd, ~0U);
1649                 if (errs & ERR_MASK(RcvEgrFullErr))
1650                         qib_stats.sps_buffull++;
1651                 else
1652                         qib_stats.sps_hdrfull++;
1653         }
1654
1655 done:
1656         return;
1657 }
1658
1659 static void qib_error_tasklet(unsigned long data)
1660 {
1661         struct qib_devdata *dd = (struct qib_devdata *)data;
1662
1663         handle_7322_errors(dd);
1664         qib_write_kreg(dd, kr_errmask, dd->cspec->errormask);
1665 }
1666
1667 static void reenable_chase(unsigned long opaque)
1668 {
1669         struct qib_pportdata *ppd = (struct qib_pportdata *)opaque;
1670
1671         ppd->cpspec->chase_timer.expires = 0;
1672         qib_set_ib_7322_lstate(ppd, QLOGIC_IB_IBCC_LINKCMD_DOWN,
1673                 QLOGIC_IB_IBCC_LINKINITCMD_POLL);
1674 }
1675
1676 static void disable_chase(struct qib_pportdata *ppd, unsigned long tnow,
1677                 u8 ibclt)
1678 {
1679         ppd->cpspec->chase_end = 0;
1680
1681         if (!qib_chase)
1682                 return;
1683
1684         qib_set_ib_7322_lstate(ppd, QLOGIC_IB_IBCC_LINKCMD_DOWN,
1685                 QLOGIC_IB_IBCC_LINKINITCMD_DISABLE);
1686         ppd->cpspec->chase_timer.expires = jiffies + QIB_CHASE_DIS_TIME;
1687         add_timer(&ppd->cpspec->chase_timer);
1688 }
1689
1690 static void handle_serdes_issues(struct qib_pportdata *ppd, u64 ibcst)
1691 {
1692         u8 ibclt;
1693         unsigned long tnow;
1694
1695         ibclt = (u8)SYM_FIELD(ibcst, IBCStatusA_0, LinkTrainingState);
1696
1697         /*
1698          * Detect and handle the state chase issue, where we can
1699          * get stuck if we are unlucky on timing on both sides of
1700          * the link.   If we are, we disable, set a timer, and
1701          * then re-enable.
1702          */
1703         switch (ibclt) {
1704         case IB_7322_LT_STATE_CFGRCVFCFG:
1705         case IB_7322_LT_STATE_CFGWAITRMT:
1706         case IB_7322_LT_STATE_TXREVLANES:
1707         case IB_7322_LT_STATE_CFGENH:
1708                 tnow = jiffies;
1709                 if (ppd->cpspec->chase_end &&
1710                      time_after(tnow, ppd->cpspec->chase_end))
1711                         disable_chase(ppd, tnow, ibclt);
1712                 else if (!ppd->cpspec->chase_end)
1713                         ppd->cpspec->chase_end = tnow + QIB_CHASE_TIME;
1714                 break;
1715         default:
1716                 ppd->cpspec->chase_end = 0;
1717                 break;
1718         }
1719
1720         if (((ibclt >= IB_7322_LT_STATE_CFGTEST &&
1721               ibclt <= IB_7322_LT_STATE_CFGWAITENH) ||
1722              ibclt == IB_7322_LT_STATE_LINKUP) &&
1723             (ibcst & SYM_MASK(IBCStatusA_0, LinkSpeedQDR))) {
1724                 force_h1(ppd);
1725                 ppd->cpspec->qdr_reforce = 1;
1726                 if (!ppd->dd->cspec->r1)
1727                         serdes_7322_los_enable(ppd, 0);
1728         } else if (ppd->cpspec->qdr_reforce &&
1729                 (ibcst & SYM_MASK(IBCStatusA_0, LinkSpeedQDR)) &&
1730                  (ibclt == IB_7322_LT_STATE_CFGENH ||
1731                 ibclt == IB_7322_LT_STATE_CFGIDLE ||
1732                 ibclt == IB_7322_LT_STATE_LINKUP))
1733                 force_h1(ppd);
1734
1735         if ((IS_QMH(ppd->dd) || IS_QME(ppd->dd)) &&
1736             ppd->link_speed_enabled == QIB_IB_QDR &&
1737             (ibclt == IB_7322_LT_STATE_CFGTEST ||
1738              ibclt == IB_7322_LT_STATE_CFGENH ||
1739              (ibclt >= IB_7322_LT_STATE_POLLACTIVE &&
1740               ibclt <= IB_7322_LT_STATE_SLEEPQUIET)))
1741                 adj_tx_serdes(ppd);
1742
1743         if (ibclt != IB_7322_LT_STATE_LINKUP) {
1744                 u8 ltstate = qib_7322_phys_portstate(ibcst);
1745                 u8 pibclt = (u8)SYM_FIELD(ppd->lastibcstat, IBCStatusA_0,
1746                                           LinkTrainingState);
1747                 if (!ppd->dd->cspec->r1 &&
1748                     pibclt == IB_7322_LT_STATE_LINKUP &&
1749                     ltstate != IB_PHYSPORTSTATE_LINK_ERR_RECOVER &&
1750                     ltstate != IB_PHYSPORTSTATE_RECOVERY_RETRAIN &&
1751                     ltstate != IB_PHYSPORTSTATE_RECOVERY_WAITRMT &&
1752                     ltstate != IB_PHYSPORTSTATE_RECOVERY_IDLE)
1753                         /* If the link went down (but no into recovery,
1754                          * turn LOS back on */
1755                         serdes_7322_los_enable(ppd, 1);
1756                 if (!ppd->cpspec->qdr_dfe_on &&
1757                     ibclt <= IB_7322_LT_STATE_SLEEPQUIET) {
1758                         ppd->cpspec->qdr_dfe_on = 1;
1759                         ppd->cpspec->qdr_dfe_time = 0;
1760                         /* On link down, reenable QDR adaptation */
1761                         qib_write_kreg_port(ppd, krp_static_adapt_dis(2),
1762                                             ppd->dd->cspec->r1 ?
1763                                             QDR_STATIC_ADAPT_DOWN_R1 :
1764                                             QDR_STATIC_ADAPT_DOWN);
1765                         printk(KERN_INFO QIB_DRV_NAME
1766                                 " IB%u:%u re-enabled QDR adaptation "
1767                                 "ibclt %x\n", ppd->dd->unit, ppd->port, ibclt);
1768                 }
1769         }
1770 }
1771
1772 static int qib_7322_set_ib_cfg(struct qib_pportdata *, int, u32);
1773
1774 /*
1775  * This is per-pport error handling.
1776  * will likely get it's own MSIx interrupt (one for each port,
1777  * although just a single handler).
1778  */
1779 static noinline void handle_7322_p_errors(struct qib_pportdata *ppd)
1780 {
1781         char *msg;
1782         u64 ignore_this_time = 0, iserr = 0, errs, fmask;
1783         struct qib_devdata *dd = ppd->dd;
1784
1785         /* do this as soon as possible */
1786         fmask = qib_read_kreg64(dd, kr_act_fmask);
1787         if (!fmask)
1788                 check_7322_rxe_status(ppd);
1789
1790         errs = qib_read_kreg_port(ppd, krp_errstatus);
1791         if (!errs)
1792                 qib_devinfo(dd->pcidev,
1793                          "Port%d error interrupt, but no error bits set!\n",
1794                          ppd->port);
1795         if (!fmask)
1796                 errs &= ~QIB_E_P_IBSTATUSCHANGED;
1797         if (!errs)
1798                 goto done;
1799
1800         msg = ppd->cpspec->epmsgbuf;
1801         *msg = '\0';
1802
1803         if (errs & ~QIB_E_P_BITSEXTANT) {
1804                 err_decode(msg, sizeof ppd->cpspec->epmsgbuf,
1805                            errs & ~QIB_E_P_BITSEXTANT, qib_7322p_error_msgs);
1806                 if (!*msg)
1807                         snprintf(msg, sizeof ppd->cpspec->epmsgbuf,
1808                                  "no others");
1809                 qib_dev_porterr(dd, ppd->port, "error interrupt with unknown"
1810                                 " errors 0x%016Lx set (and %s)\n",
1811                                 (errs & ~QIB_E_P_BITSEXTANT), msg);
1812                 *msg = '\0';
1813         }
1814
1815         if (errs & QIB_E_P_SHDR) {
1816                 u64 symptom;
1817
1818                 /* determine cause, then write to clear */
1819                 symptom = qib_read_kreg_port(ppd, krp_sendhdrsymptom);
1820                 qib_write_kreg_port(ppd, krp_sendhdrsymptom, 0);
1821                 err_decode(msg, sizeof ppd->cpspec->epmsgbuf, symptom,
1822                            hdrchk_msgs);
1823                 *msg = '\0';
1824                 /* senderrbuf cleared in SPKTERRS below */
1825         }
1826
1827         if (errs & QIB_E_P_SPKTERRS) {
1828                 if ((errs & QIB_E_P_LINK_PKTERRS) &&
1829                     !(ppd->lflags & QIBL_LINKACTIVE)) {
1830                         /*
1831                          * This can happen when trying to bring the link
1832                          * up, but the IB link changes state at the "wrong"
1833                          * time. The IB logic then complains that the packet
1834                          * isn't valid.  We don't want to confuse people, so
1835                          * we just don't print them, except at debug
1836                          */
1837                         err_decode(msg, sizeof ppd->cpspec->epmsgbuf,
1838                                    (errs & QIB_E_P_LINK_PKTERRS),
1839                                    qib_7322p_error_msgs);
1840                         *msg = '\0';
1841                         ignore_this_time = errs & QIB_E_P_LINK_PKTERRS;
1842                 }
1843                 qib_disarm_7322_senderrbufs(ppd);
1844         } else if ((errs & QIB_E_P_LINK_PKTERRS) &&
1845                    !(ppd->lflags & QIBL_LINKACTIVE)) {
1846                 /*
1847                  * This can happen when SMA is trying to bring the link
1848                  * up, but the IB link changes state at the "wrong" time.
1849                  * The IB logic then complains that the packet isn't
1850                  * valid.  We don't want to confuse people, so we just
1851                  * don't print them, except at debug
1852                  */
1853                 err_decode(msg, sizeof ppd->cpspec->epmsgbuf, errs,
1854                            qib_7322p_error_msgs);
1855                 ignore_this_time = errs & QIB_E_P_LINK_PKTERRS;
1856                 *msg = '\0';
1857         }
1858
1859         qib_write_kreg_port(ppd, krp_errclear, errs);
1860
1861         errs &= ~ignore_this_time;
1862         if (!errs)
1863                 goto done;
1864
1865         if (errs & QIB_E_P_RPKTERRS)
1866                 qib_stats.sps_rcverrs++;
1867         if (errs & QIB_E_P_SPKTERRS)
1868                 qib_stats.sps_txerrs++;
1869
1870         iserr = errs & ~(QIB_E_P_RPKTERRS | QIB_E_P_PKTERRS);
1871
1872         if (errs & QIB_E_P_SDMAERRS)
1873                 sdma_7322_p_errors(ppd, errs);
1874
1875         if (errs & QIB_E_P_IBSTATUSCHANGED) {
1876                 u64 ibcs;
1877                 u8 ltstate;
1878
1879                 ibcs = qib_read_kreg_port(ppd, krp_ibcstatus_a);
1880                 ltstate = qib_7322_phys_portstate(ibcs);
1881
1882                 if (!(ppd->lflags & QIBL_IB_AUTONEG_INPROG))
1883                         handle_serdes_issues(ppd, ibcs);
1884                 if (!(ppd->cpspec->ibcctrl_a &
1885                       SYM_MASK(IBCCtrlA_0, IBStatIntReductionEn))) {
1886                         /*
1887                          * We got our interrupt, so init code should be
1888                          * happy and not try alternatives. Now squelch
1889                          * other "chatter" from link-negotiation (pre Init)
1890                          */
1891                         ppd->cpspec->ibcctrl_a |=
1892                                 SYM_MASK(IBCCtrlA_0, IBStatIntReductionEn);
1893                         qib_write_kreg_port(ppd, krp_ibcctrl_a,
1894                                             ppd->cpspec->ibcctrl_a);
1895                 }
1896
1897                 /* Update our picture of width and speed from chip */
1898                 ppd->link_width_active =
1899                         (ibcs & SYM_MASK(IBCStatusA_0, LinkWidthActive)) ?
1900                             IB_WIDTH_4X : IB_WIDTH_1X;
1901                 ppd->link_speed_active = (ibcs & SYM_MASK(IBCStatusA_0,
1902                         LinkSpeedQDR)) ? QIB_IB_QDR : (ibcs &
1903                           SYM_MASK(IBCStatusA_0, LinkSpeedActive)) ?
1904                                    QIB_IB_DDR : QIB_IB_SDR;
1905
1906                 if ((ppd->lflags & QIBL_IB_LINK_DISABLED) && ltstate !=
1907                     IB_PHYSPORTSTATE_DISABLED)
1908                         qib_set_ib_7322_lstate(ppd, 0,
1909                                QLOGIC_IB_IBCC_LINKINITCMD_DISABLE);
1910                 else
1911                         /*
1912                          * Since going into a recovery state causes the link
1913                          * state to go down and since recovery is transitory,
1914                          * it is better if we "miss" ever seeing the link
1915                          * training state go into recovery (i.e., ignore this
1916                          * transition for link state special handling purposes)
1917                          * without updating lastibcstat.
1918                          */
1919                         if (ltstate != IB_PHYSPORTSTATE_LINK_ERR_RECOVER &&
1920                             ltstate != IB_PHYSPORTSTATE_RECOVERY_RETRAIN &&
1921                             ltstate != IB_PHYSPORTSTATE_RECOVERY_WAITRMT &&
1922                             ltstate != IB_PHYSPORTSTATE_RECOVERY_IDLE)
1923                                 qib_handle_e_ibstatuschanged(ppd, ibcs);
1924         }
1925         if (*msg && iserr)
1926                 qib_dev_porterr(dd, ppd->port, "%s error\n", msg);
1927
1928         if (ppd->state_wanted & ppd->lflags)
1929                 wake_up_interruptible(&ppd->state_wait);
1930 done:
1931         return;
1932 }
1933
1934 /* enable/disable chip from delivering interrupts */
1935 static void qib_7322_set_intr_state(struct qib_devdata *dd, u32 enable)
1936 {
1937         if (enable) {
1938                 if (dd->flags & QIB_BADINTR)
1939                         return;
1940                 qib_write_kreg(dd, kr_intmask, dd->cspec->int_enable_mask);
1941                 /* cause any pending enabled interrupts to be re-delivered */
1942                 qib_write_kreg(dd, kr_intclear, 0ULL);
1943                 if (dd->cspec->num_msix_entries) {
1944                         /* and same for MSIx */
1945                         u64 val = qib_read_kreg64(dd, kr_intgranted);
1946                         if (val)
1947                                 qib_write_kreg(dd, kr_intgranted, val);
1948                 }
1949         } else
1950                 qib_write_kreg(dd, kr_intmask, 0ULL);
1951 }
1952
1953 /*
1954  * Try to cleanup as much as possible for anything that might have gone
1955  * wrong while in freeze mode, such as pio buffers being written by user
1956  * processes (causing armlaunch), send errors due to going into freeze mode,
1957  * etc., and try to avoid causing extra interrupts while doing so.
1958  * Forcibly update the in-memory pioavail register copies after cleanup
1959  * because the chip won't do it while in freeze mode (the register values
1960  * themselves are kept correct).
1961  * Make sure that we don't lose any important interrupts by using the chip
1962  * feature that says that writing 0 to a bit in *clear that is set in
1963  * *status will cause an interrupt to be generated again (if allowed by
1964  * the *mask value).
1965  * This is in chip-specific code because of all of the register accesses,
1966  * even though the details are similar on most chips.
1967  */
1968 static void qib_7322_clear_freeze(struct qib_devdata *dd)
1969 {
1970         int pidx;
1971
1972         /* disable error interrupts, to avoid confusion */
1973         qib_write_kreg(dd, kr_errmask, 0ULL);
1974
1975         for (pidx = 0; pidx < dd->num_pports; ++pidx)
1976                 if (dd->pport[pidx].link_speed_supported)
1977                         qib_write_kreg_port(dd->pport + pidx, krp_errmask,
1978                                             0ULL);
1979
1980         /* also disable interrupts; errormask is sometimes overwriten */
1981         qib_7322_set_intr_state(dd, 0);
1982
1983         /* clear the freeze, and be sure chip saw it */
1984         qib_write_kreg(dd, kr_control, dd->control);
1985         qib_read_kreg32(dd, kr_scratch);
1986
1987         /*
1988          * Force new interrupt if any hwerr, error or interrupt bits are
1989          * still set, and clear "safe" send packet errors related to freeze
1990          * and cancelling sends.  Re-enable error interrupts before possible
1991          * force of re-interrupt on pending interrupts.
1992          */
1993         qib_write_kreg(dd, kr_hwerrclear, 0ULL);
1994         qib_write_kreg(dd, kr_errclear, E_SPKT_ERRS_IGNORE);
1995         qib_write_kreg(dd, kr_errmask, dd->cspec->errormask);
1996         /* We need to purge per-port errs and reset mask, too */
1997         for (pidx = 0; pidx < dd->num_pports; ++pidx) {
1998                 if (!dd->pport[pidx].link_speed_supported)
1999                         continue;
2000                 qib_write_kreg_port(dd->pport + pidx, krp_errclear, ~0Ull);
2001                 qib_write_kreg_port(dd->pport + pidx, krp_errmask, ~0Ull);
2002         }
2003         qib_7322_set_intr_state(dd, 1);
2004 }
2005
2006 /* no error handling to speak of */
2007 /**
2008  * qib_7322_handle_hwerrors - display hardware errors.
2009  * @dd: the qlogic_ib device
2010  * @msg: the output buffer
2011  * @msgl: the size of the output buffer
2012  *
2013  * Use same msg buffer as regular errors to avoid excessive stack
2014  * use.  Most hardware errors are catastrophic, but for right now,
2015  * we'll print them and continue.  We reuse the same message buffer as
2016  * qib_handle_errors() to avoid excessive stack usage.
2017  */
2018 static void qib_7322_handle_hwerrors(struct qib_devdata *dd, char *msg,
2019                                      size_t msgl)
2020 {
2021         u64 hwerrs;
2022         u32 ctrl;
2023         int isfatal = 0;
2024
2025         hwerrs = qib_read_kreg64(dd, kr_hwerrstatus);
2026         if (!hwerrs)
2027                 goto bail;
2028         if (hwerrs == ~0ULL) {
2029                 qib_dev_err(dd, "Read of hardware error status failed "
2030                             "(all bits set); ignoring\n");
2031                 goto bail;
2032         }
2033         qib_stats.sps_hwerrs++;
2034
2035         /* Always clear the error status register, except BIST fail */
2036         qib_write_kreg(dd, kr_hwerrclear, hwerrs &
2037                        ~HWE_MASK(PowerOnBISTFailed));
2038
2039         hwerrs &= dd->cspec->hwerrmask;
2040
2041         /* no EEPROM logging, yet */
2042
2043         if (hwerrs)
2044                 qib_devinfo(dd->pcidev, "Hardware error: hwerr=0x%llx "
2045                             "(cleared)\n", (unsigned long long) hwerrs);
2046
2047         ctrl = qib_read_kreg32(dd, kr_control);
2048         if ((ctrl & SYM_MASK(Control, FreezeMode)) && !dd->diag_client) {
2049                 /*
2050                  * No recovery yet...
2051                  */
2052                 if ((hwerrs & ~HWE_MASK(LATriggered)) ||
2053                     dd->cspec->stay_in_freeze) {
2054                         /*
2055                          * If any set that we aren't ignoring only make the
2056                          * complaint once, in case it's stuck or recurring,
2057                          * and we get here multiple times
2058                          * Force link down, so switch knows, and
2059                          * LEDs are turned off.
2060                          */
2061                         if (dd->flags & QIB_INITTED)
2062                                 isfatal = 1;
2063                 } else
2064                         qib_7322_clear_freeze(dd);
2065         }
2066
2067         if (hwerrs & HWE_MASK(PowerOnBISTFailed)) {
2068                 isfatal = 1;
2069                 strlcpy(msg, "[Memory BIST test failed, "
2070                         "InfiniPath hardware unusable]", msgl);
2071                 /* ignore from now on, so disable until driver reloaded */
2072                 dd->cspec->hwerrmask &= ~HWE_MASK(PowerOnBISTFailed);
2073                 qib_write_kreg(dd, kr_hwerrmask, dd->cspec->hwerrmask);
2074         }
2075
2076         err_decode(msg, msgl, hwerrs, qib_7322_hwerror_msgs);
2077
2078         /* Ignore esoteric PLL failures et al. */
2079
2080         qib_dev_err(dd, "%s hardware error\n", msg);
2081
2082         if (isfatal && !dd->diag_client) {
2083                 qib_dev_err(dd, "Fatal Hardware Error, no longer"
2084                             " usable, SN %.16s\n", dd->serial);
2085                 /*
2086                  * for /sys status file and user programs to print; if no
2087                  * trailing brace is copied, we'll know it was truncated.
2088                  */
2089                 if (dd->freezemsg)
2090                         snprintf(dd->freezemsg, dd->freezelen,
2091                                  "{%s}", msg);
2092                 qib_disable_after_error(dd);
2093         }
2094 bail:;
2095 }
2096
2097 /**
2098  * qib_7322_init_hwerrors - enable hardware errors
2099  * @dd: the qlogic_ib device
2100  *
2101  * now that we have finished initializing everything that might reasonably
2102  * cause a hardware error, and cleared those errors bits as they occur,
2103  * we can enable hardware errors in the mask (potentially enabling
2104  * freeze mode), and enable hardware errors as errors (along with
2105  * everything else) in errormask
2106  */
2107 static void qib_7322_init_hwerrors(struct qib_devdata *dd)
2108 {
2109         int pidx;
2110         u64 extsval;
2111
2112         extsval = qib_read_kreg64(dd, kr_extstatus);
2113         if (!(extsval & (QIB_EXTS_MEMBIST_DISABLED |
2114                          QIB_EXTS_MEMBIST_ENDTEST)))
2115                 qib_dev_err(dd, "MemBIST did not complete!\n");
2116
2117         /* never clear BIST failure, so reported on each driver load */
2118         qib_write_kreg(dd, kr_hwerrclear, ~HWE_MASK(PowerOnBISTFailed));
2119         qib_write_kreg(dd, kr_hwerrmask, dd->cspec->hwerrmask);
2120
2121         /* clear all */
2122         qib_write_kreg(dd, kr_errclear, ~0ULL);
2123         /* enable errors that are masked, at least this first time. */
2124         qib_write_kreg(dd, kr_errmask, ~0ULL);
2125         dd->cspec->errormask = qib_read_kreg64(dd, kr_errmask);
2126         for (pidx = 0; pidx < dd->num_pports; ++pidx)
2127                 if (dd->pport[pidx].link_speed_supported)
2128                         qib_write_kreg_port(dd->pport + pidx, krp_errmask,
2129                                             ~0ULL);
2130 }
2131
2132 /*
2133  * Disable and enable the armlaunch error.  Used for PIO bandwidth testing
2134  * on chips that are count-based, rather than trigger-based.  There is no
2135  * reference counting, but that's also fine, given the intended use.
2136  * Only chip-specific because it's all register accesses
2137  */
2138 static void qib_set_7322_armlaunch(struct qib_devdata *dd, u32 enable)
2139 {
2140         if (enable) {
2141                 qib_write_kreg(dd, kr_errclear, QIB_E_SPIOARMLAUNCH);
2142                 dd->cspec->errormask |= QIB_E_SPIOARMLAUNCH;
2143         } else
2144                 dd->cspec->errormask &= ~QIB_E_SPIOARMLAUNCH;
2145         qib_write_kreg(dd, kr_errmask, dd->cspec->errormask);
2146 }
2147
2148 /*
2149  * Formerly took parameter <which> in pre-shifted,
2150  * pre-merged form with LinkCmd and LinkInitCmd
2151  * together, and assuming the zero was NOP.
2152  */
2153 static void qib_set_ib_7322_lstate(struct qib_pportdata *ppd, u16 linkcmd,
2154                                    u16 linitcmd)
2155 {
2156         u64 mod_wd;
2157         struct qib_devdata *dd = ppd->dd;
2158         unsigned long flags;
2159
2160         if (linitcmd == QLOGIC_IB_IBCC_LINKINITCMD_DISABLE) {
2161                 /*
2162                  * If we are told to disable, note that so link-recovery
2163                  * code does not attempt to bring us back up.
2164                  * Also reset everything that we can, so we start
2165                  * completely clean when re-enabled (before we
2166                  * actually issue the disable to the IBC)
2167                  */
2168                 qib_7322_mini_pcs_reset(ppd);
2169                 spin_lock_irqsave(&ppd->lflags_lock, flags);
2170                 ppd->lflags |= QIBL_IB_LINK_DISABLED;
2171                 spin_unlock_irqrestore(&ppd->lflags_lock, flags);
2172         } else if (linitcmd || linkcmd == QLOGIC_IB_IBCC_LINKCMD_DOWN) {
2173                 /*
2174                  * Any other linkinitcmd will lead to LINKDOWN and then
2175                  * to INIT (if all is well), so clear flag to let
2176                  * link-recovery code attempt to bring us back up.
2177                  */
2178                 spin_lock_irqsave(&ppd->lflags_lock, flags);
2179                 ppd->lflags &= ~QIBL_IB_LINK_DISABLED;
2180                 spin_unlock_irqrestore(&ppd->lflags_lock, flags);
2181                 /*
2182                  * Clear status change interrupt reduction so the
2183                  * new state is seen.
2184                  */
2185                 ppd->cpspec->ibcctrl_a &=
2186                         ~SYM_MASK(IBCCtrlA_0, IBStatIntReductionEn);
2187         }
2188
2189         mod_wd = (linkcmd << IBA7322_IBCC_LINKCMD_SHIFT) |
2190                 (linitcmd << QLOGIC_IB_IBCC_LINKINITCMD_SHIFT);
2191
2192         qib_write_kreg_port(ppd, krp_ibcctrl_a, ppd->cpspec->ibcctrl_a |
2193                             mod_wd);
2194         /* write to chip to prevent back-to-back writes of ibc reg */
2195         qib_write_kreg(dd, kr_scratch, 0);
2196
2197 }
2198
2199 /*
2200  * The total RCV buffer memory is 64KB, used for both ports, and is
2201  * in units of 64 bytes (same as IB flow control credit unit).
2202  * The consumedVL unit in the same registers are in 32 byte units!
2203  * So, a VL15 packet needs 4.50 IB credits, and 9 rx buffer chunks,
2204  * and we can therefore allocate just 9 IB credits for 2 VL15 packets
2205  * in krp_rxcreditvl15, rather than 10.
2206  */
2207 #define RCV_BUF_UNITSZ 64
2208 #define NUM_RCV_BUF_UNITS(dd) ((64 * 1024) / (RCV_BUF_UNITSZ * dd->num_pports))
2209
2210 static void set_vls(struct qib_pportdata *ppd)
2211 {
2212         int i, numvls, totcred, cred_vl, vl0extra;
2213         struct qib_devdata *dd = ppd->dd;
2214         u64 val;
2215
2216         numvls = qib_num_vls(ppd->vls_operational);
2217
2218         /*
2219          * Set up per-VL credits. Below is kluge based on these assumptions:
2220          * 1) port is disabled at the time early_init is called.
2221          * 2) give VL15 17 credits, for two max-plausible packets.
2222          * 3) Give VL0-N the rest, with any rounding excess used for VL0
2223          */
2224         /* 2 VL15 packets @ 288 bytes each (including IB headers) */
2225         totcred = NUM_RCV_BUF_UNITS(dd);
2226         cred_vl = (2 * 288 + RCV_BUF_UNITSZ - 1) / RCV_BUF_UNITSZ;
2227         totcred -= cred_vl;
2228         qib_write_kreg_port(ppd, krp_rxcreditvl15, (u64) cred_vl);
2229         cred_vl = totcred / numvls;
2230         vl0extra = totcred - cred_vl * numvls;
2231         qib_write_kreg_port(ppd, krp_rxcreditvl0, cred_vl + vl0extra);
2232         for (i = 1; i < numvls; i++)
2233                 qib_write_kreg_port(ppd, krp_rxcreditvl0 + i, cred_vl);
2234         for (; i < 8; i++) /* no buffer space for other VLs */
2235                 qib_write_kreg_port(ppd, krp_rxcreditvl0 + i, 0);
2236
2237         /* Notify IBC that credits need to be recalculated */
2238         val = qib_read_kreg_port(ppd, krp_ibsdtestiftx);
2239         val |= SYM_MASK(IB_SDTEST_IF_TX_0, CREDIT_CHANGE);
2240         qib_write_kreg_port(ppd, krp_ibsdtestiftx, val);
2241         qib_write_kreg(dd, kr_scratch, 0ULL);
2242         val &= ~SYM_MASK(IB_SDTEST_IF_TX_0, CREDIT_CHANGE);
2243         qib_write_kreg_port(ppd, krp_ibsdtestiftx, val);
2244
2245         for (i = 0; i < numvls; i++)
2246                 val = qib_read_kreg_port(ppd, krp_rxcreditvl0 + i);
2247         val = qib_read_kreg_port(ppd, krp_rxcreditvl15);
2248
2249         /* Change the number of operational VLs */
2250         ppd->cpspec->ibcctrl_a = (ppd->cpspec->ibcctrl_a &
2251                                 ~SYM_MASK(IBCCtrlA_0, NumVLane)) |
2252                 ((u64)(numvls - 1) << SYM_LSB(IBCCtrlA_0, NumVLane));
2253         qib_write_kreg_port(ppd, krp_ibcctrl_a, ppd->cpspec->ibcctrl_a);
2254         qib_write_kreg(dd, kr_scratch, 0ULL);
2255 }
2256
2257 /*
2258  * The code that deals with actual SerDes is in serdes_7322_init().
2259  * Compared to the code for iba7220, it is minimal.
2260  */
2261 static int serdes_7322_init(struct qib_pportdata *ppd);
2262
2263 /**
2264  * qib_7322_bringup_serdes - bring up the serdes
2265  * @ppd: physical port on the qlogic_ib device
2266  */
2267 static int qib_7322_bringup_serdes(struct qib_pportdata *ppd)
2268 {
2269         struct qib_devdata *dd = ppd->dd;
2270         u64 val, guid, ibc;
2271         unsigned long flags;
2272         int ret = 0;
2273
2274         /*
2275          * SerDes model not in Pd, but still need to
2276          * set up much of IBCCtrl and IBCDDRCtrl; move elsewhere
2277          * eventually.
2278          */
2279         /* Put IBC in reset, sends disabled (should be in reset already) */
2280         ppd->cpspec->ibcctrl_a &= ~SYM_MASK(IBCCtrlA_0, IBLinkEn);
2281         qib_write_kreg_port(ppd, krp_ibcctrl_a, ppd->cpspec->ibcctrl_a);
2282         qib_write_kreg(dd, kr_scratch, 0ULL);
2283
2284         if (qib_compat_ddr_negotiate) {
2285                 ppd->cpspec->ibdeltainprog = 1;
2286                 ppd->cpspec->ibsymsnap = read_7322_creg32_port(ppd,
2287                                                 crp_ibsymbolerr);
2288                 ppd->cpspec->iblnkerrsnap = read_7322_creg32_port(ppd,
2289                                                 crp_iblinkerrrecov);
2290         }
2291
2292         /* flowcontrolwatermark is in units of KBytes */
2293         ibc = 0x5ULL << SYM_LSB(IBCCtrlA_0, FlowCtrlWaterMark);
2294         /*
2295          * Flow control is sent this often, even if no changes in
2296          * buffer space occur.  Units are 128ns for this chip.
2297          * Set to 3usec.
2298          */
2299         ibc |= 24ULL << SYM_LSB(IBCCtrlA_0, FlowCtrlPeriod);
2300         /* max error tolerance */
2301         ibc |= 0xfULL << SYM_LSB(IBCCtrlA_0, PhyerrThreshold);
2302         /* IB credit flow control. */
2303         ibc |= 0xfULL << SYM_LSB(IBCCtrlA_0, OverrunThreshold);
2304         /*
2305          * set initial max size pkt IBC will send, including ICRC; it's the
2306          * PIO buffer size in dwords, less 1; also see qib_set_mtu()
2307          */
2308         ibc |= ((u64)(ppd->ibmaxlen >> 2) + 1) <<
2309                 SYM_LSB(IBCCtrlA_0, MaxPktLen);
2310         ppd->cpspec->ibcctrl_a = ibc; /* without linkcmd or linkinitcmd! */
2311
2312         /*
2313          * Reset the PCS interface to the serdes (and also ibc, which is still
2314          * in reset from above).  Writes new value of ibcctrl_a as last step.
2315          */
2316         qib_7322_mini_pcs_reset(ppd);
2317
2318         if (!ppd->cpspec->ibcctrl_b) {
2319                 unsigned lse = ppd->link_speed_enabled;
2320
2321                 /*
2322                  * Not on re-init after reset, establish shadow
2323                  * and force initial config.
2324                  */
2325                 ppd->cpspec->ibcctrl_b = qib_read_kreg_port(ppd,
2326                                                              krp_ibcctrl_b);
2327                 ppd->cpspec->ibcctrl_b &= ~(IBA7322_IBC_SPEED_QDR |
2328                                 IBA7322_IBC_SPEED_DDR |
2329                                 IBA7322_IBC_SPEED_SDR |
2330                                 IBA7322_IBC_WIDTH_AUTONEG |
2331                                 SYM_MASK(IBCCtrlB_0, IB_LANE_REV_SUPPORTED));
2332                 if (lse & (lse - 1)) /* Muliple speeds enabled */
2333                         ppd->cpspec->ibcctrl_b |=
2334                                 (lse << IBA7322_IBC_SPEED_LSB) |
2335                                 IBA7322_IBC_IBTA_1_2_MASK |
2336                                 IBA7322_IBC_MAX_SPEED_MASK;
2337                 else
2338                         ppd->cpspec->ibcctrl_b |= (lse == QIB_IB_QDR) ?
2339                                 IBA7322_IBC_SPEED_QDR |
2340                                  IBA7322_IBC_IBTA_1_2_MASK :
2341                                 (lse == QIB_IB_DDR) ?
2342                                         IBA7322_IBC_SPEED_DDR :
2343                                         IBA7322_IBC_SPEED_SDR;
2344                 if ((ppd->link_width_enabled & (IB_WIDTH_1X | IB_WIDTH_4X)) ==
2345                     (IB_WIDTH_1X | IB_WIDTH_4X))
2346                         ppd->cpspec->ibcctrl_b |= IBA7322_IBC_WIDTH_AUTONEG;
2347                 else
2348                         ppd->cpspec->ibcctrl_b |=
2349                                 ppd->link_width_enabled == IB_WIDTH_4X ?
2350                                 IBA7322_IBC_WIDTH_4X_ONLY :
2351                                 IBA7322_IBC_WIDTH_1X_ONLY;
2352
2353                 /* always enable these on driver reload, not sticky */
2354                 ppd->cpspec->ibcctrl_b |= (IBA7322_IBC_RXPOL_MASK |
2355                         IBA7322_IBC_HRTBT_MASK);
2356         }
2357         qib_write_kreg_port(ppd, krp_ibcctrl_b, ppd->cpspec->ibcctrl_b);
2358
2359         /* setup so we have more time at CFGTEST to change H1 */
2360         val = qib_read_kreg_port(ppd, krp_ibcctrl_c);
2361         val &= ~SYM_MASK(IBCCtrlC_0, IB_FRONT_PORCH);
2362         val |= 0xfULL << SYM_LSB(IBCCtrlC_0, IB_FRONT_PORCH);
2363         qib_write_kreg_port(ppd, krp_ibcctrl_c, val);
2364
2365         serdes_7322_init(ppd);
2366
2367         guid = be64_to_cpu(ppd->guid);
2368         if (!guid) {
2369                 if (dd->base_guid)
2370                         guid = be64_to_cpu(dd->base_guid) + ppd->port - 1;
2371                 ppd->guid = cpu_to_be64(guid);
2372         }
2373
2374         qib_write_kreg_port(ppd, krp_hrtbt_guid, guid);
2375         /* write to chip to prevent back-to-back writes of ibc reg */
2376         qib_write_kreg(dd, kr_scratch, 0);
2377
2378         /* Enable port */
2379         ppd->cpspec->ibcctrl_a |= SYM_MASK(IBCCtrlA_0, IBLinkEn);
2380         set_vls(ppd);
2381
2382         /* initially come up DISABLED, without sending anything. */
2383         val = ppd->cpspec->ibcctrl_a | (QLOGIC_IB_IBCC_LINKINITCMD_DISABLE <<
2384                                         QLOGIC_IB_IBCC_LINKINITCMD_SHIFT);
2385         qib_write_kreg_port(ppd, krp_ibcctrl_a, val);
2386         qib_write_kreg(dd, kr_scratch, 0ULL);
2387         /* clear the linkinit cmds */
2388         ppd->cpspec->ibcctrl_a = val & ~SYM_MASK(IBCCtrlA_0, LinkInitCmd);
2389
2390         /* be paranoid against later code motion, etc. */
2391         spin_lock_irqsave(&dd->cspec->rcvmod_lock, flags);
2392         ppd->p_rcvctrl |= SYM_MASK(RcvCtrl_0, RcvIBPortEnable);
2393         qib_write_kreg_port(ppd, krp_rcvctrl, ppd->p_rcvctrl);
2394         spin_unlock_irqrestore(&dd->cspec->rcvmod_lock, flags);
2395
2396         /* Also enable IBSTATUSCHG interrupt.  */
2397         val = qib_read_kreg_port(ppd, krp_errmask);
2398         qib_write_kreg_port(ppd, krp_errmask,
2399                 val | ERR_MASK_N(IBStatusChanged));
2400
2401         /* Always zero until we start messing with SerDes for real */
2402         return ret;
2403 }
2404
2405 /**
2406  * qib_7322_quiet_serdes - set serdes to txidle
2407  * @dd: the qlogic_ib device
2408  * Called when driver is being unloaded
2409  */
2410 static void qib_7322_mini_quiet_serdes(struct qib_pportdata *ppd)
2411 {
2412         u64 val;
2413         unsigned long flags;
2414
2415         qib_set_ib_7322_lstate(ppd, 0, QLOGIC_IB_IBCC_LINKINITCMD_DISABLE);
2416
2417         spin_lock_irqsave(&ppd->lflags_lock, flags);
2418         ppd->lflags &= ~QIBL_IB_AUTONEG_INPROG;
2419         spin_unlock_irqrestore(&ppd->lflags_lock, flags);
2420         wake_up(&ppd->cpspec->autoneg_wait);
2421         cancel_delayed_work_sync(&ppd->cpspec->autoneg_work);
2422         if (ppd->dd->cspec->r1)
2423                 cancel_delayed_work_sync(&ppd->cpspec->ipg_work);
2424
2425         ppd->cpspec->chase_end = 0;
2426         if (ppd->cpspec->chase_timer.data) /* if initted */
2427                 del_timer_sync(&ppd->cpspec->chase_timer);
2428
2429         /*
2430          * Despite the name, actually disables IBC as well. Do it when
2431          * we are as sure as possible that no more packets can be
2432          * received, following the down and the PCS reset.
2433          * The actual disabling happens in qib_7322_mini_pci_reset(),
2434          * along with the PCS being reset.
2435          */
2436         ppd->cpspec->ibcctrl_a &= ~SYM_MASK(IBCCtrlA_0, IBLinkEn);
2437         qib_7322_mini_pcs_reset(ppd);
2438
2439         /*
2440          * Update the adjusted counters so the adjustment persists
2441          * across driver reload.
2442          */
2443         if (ppd->cpspec->ibsymdelta || ppd->cpspec->iblnkerrdelta ||
2444             ppd->cpspec->ibdeltainprog || ppd->cpspec->iblnkdowndelta) {
2445                 struct qib_devdata *dd = ppd->dd;
2446                 u64 diagc;
2447
2448                 /* enable counter writes */
2449                 diagc = qib_read_kreg64(dd, kr_hwdiagctrl);
2450                 qib_write_kreg(dd, kr_hwdiagctrl,
2451                                diagc | SYM_MASK(HwDiagCtrl, CounterWrEnable));
2452
2453                 if (ppd->cpspec->ibsymdelta || ppd->cpspec->ibdeltainprog) {
2454                         val = read_7322_creg32_port(ppd, crp_ibsymbolerr);
2455                         if (ppd->cpspec->ibdeltainprog)
2456                                 val -= val - ppd->cpspec->ibsymsnap;
2457                         val -= ppd->cpspec->ibsymdelta;
2458                         write_7322_creg_port(ppd, crp_ibsymbolerr, val);
2459                 }
2460                 if (ppd->cpspec->iblnkerrdelta || ppd->cpspec->ibdeltainprog) {
2461                         val = read_7322_creg32_port(ppd, crp_iblinkerrrecov);
2462                         if (ppd->cpspec->ibdeltainprog)
2463                                 val -= val - ppd->cpspec->iblnkerrsnap;
2464                         val -= ppd->cpspec->iblnkerrdelta;
2465                         write_7322_creg_port(ppd, crp_iblinkerrrecov, val);
2466                 }
2467                 if (ppd->cpspec->iblnkdowndelta) {
2468                         val = read_7322_creg32_port(ppd, crp_iblinkdown);
2469                         val += ppd->cpspec->iblnkdowndelta;
2470                         write_7322_creg_port(ppd, crp_iblinkdown, val);
2471                 }
2472                 /*
2473                  * No need to save ibmalfdelta since IB perfcounters
2474                  * are cleared on driver reload.
2475                  */
2476
2477                 /* and disable counter writes */
2478                 qib_write_kreg(dd, kr_hwdiagctrl, diagc);
2479         }
2480 }
2481
2482 /**
2483  * qib_setup_7322_setextled - set the state of the two external LEDs
2484  * @ppd: physical port on the qlogic_ib device
2485  * @on: whether the link is up or not
2486  *
2487  * The exact combo of LEDs if on is true is determined by looking
2488  * at the ibcstatus.
2489  *
2490  * These LEDs indicate the physical and logical state of IB link.
2491  * For this chip (at least with recommended board pinouts), LED1
2492  * is Yellow (logical state) and LED2 is Green (physical state),
2493  *
2494  * Note:  We try to match the Mellanox HCA LED behavior as best
2495  * we can.  Green indicates physical link state is OK (something is
2496  * plugged in, and we can train).
2497  * Amber indicates the link is logically up (ACTIVE).
2498  * Mellanox further blinks the amber LED to indicate data packet
2499  * activity, but we have no hardware support for that, so it would
2500  * require waking up every 10-20 msecs and checking the counters
2501  * on the chip, and then turning the LED off if appropriate.  That's
2502  * visible overhead, so not something we will do.
2503  */
2504 static void qib_setup_7322_setextled(struct qib_pportdata *ppd, u32 on)
2505 {
2506         struct qib_devdata *dd = ppd->dd;
2507         u64 extctl, ledblink = 0, val;
2508         unsigned long flags;
2509         int yel, grn;
2510
2511         /*
2512          * The diags use the LED to indicate diag info, so we leave
2513          * the external LED alone when the diags are running.
2514          */
2515         if (dd->diag_client)
2516                 return;
2517
2518         /* Allow override of LED display for, e.g. Locating system in rack */
2519         if (ppd->led_override) {
2520                 grn = (ppd->led_override & QIB_LED_PHYS);
2521                 yel = (ppd->led_override & QIB_LED_LOG);
2522         } else if (on) {
2523                 val = qib_read_kreg_port(ppd, krp_ibcstatus_a);
2524                 grn = qib_7322_phys_portstate(val) ==
2525                         IB_PHYSPORTSTATE_LINKUP;
2526                 yel = qib_7322_iblink_state(val) == IB_PORT_ACTIVE;
2527         } else {
2528                 grn = 0;
2529                 yel = 0;
2530         }
2531
2532         spin_lock_irqsave(&dd->cspec->gpio_lock, flags);
2533         extctl = dd->cspec->extctrl & (ppd->port == 1 ?
2534                 ~ExtLED_IB1_MASK : ~ExtLED_IB2_MASK);
2535         if (grn) {
2536                 extctl |= ppd->port == 1 ? ExtLED_IB1_GRN : ExtLED_IB2_GRN;
2537                 /*
2538                  * Counts are in chip clock (4ns) periods.
2539                  * This is 1/16 sec (66.6ms) on,
2540                  * 3/16 sec (187.5 ms) off, with packets rcvd.
2541                  */
2542                 ledblink = ((66600 * 1000UL / 4) << IBA7322_LEDBLINK_ON_SHIFT) |
2543                         ((187500 * 1000UL / 4) << IBA7322_LEDBLINK_OFF_SHIFT);
2544         }
2545         if (yel)
2546                 extctl |= ppd->port == 1 ? ExtLED_IB1_YEL : ExtLED_IB2_YEL;
2547         dd->cspec->extctrl = extctl;
2548         qib_write_kreg(dd, kr_extctrl, dd->cspec->extctrl);
2549         spin_unlock_irqrestore(&dd->cspec->gpio_lock, flags);
2550
2551         if (ledblink) /* blink the LED on packet receive */
2552                 qib_write_kreg_port(ppd, krp_rcvpktledcnt, ledblink);
2553 }
2554
2555 /*
2556  * Disable MSIx interrupt if enabled, call generic MSIx code
2557  * to cleanup, and clear pending MSIx interrupts.
2558  * Used for fallback to INTx, after reset, and when MSIx setup fails.
2559  */
2560 static void qib_7322_nomsix(struct qib_devdata *dd)
2561 {
2562         u64 intgranted;
2563         int n;
2564
2565         dd->cspec->main_int_mask = ~0ULL;
2566         n = dd->cspec->num_msix_entries;
2567         if (n) {
2568                 int i;
2569
2570                 dd->cspec->num_msix_entries = 0;
2571                 for (i = 0; i < n; i++) {
2572                         irq_set_affinity_hint(
2573                           dd->cspec->msix_entries[i].msix.vector, NULL);
2574                         free_cpumask_var(dd->cspec->msix_entries[i].mask);
2575                         free_irq(dd->cspec->msix_entries[i].msix.vector,
2576                            dd->cspec->msix_entries[i].arg);
2577                 }
2578                 qib_nomsix(dd);
2579         }
2580         /* make sure no MSIx interrupts are left pending */
2581         intgranted = qib_read_kreg64(dd, kr_intgranted);
2582         if (intgranted)
2583                 qib_write_kreg(dd, kr_intgranted, intgranted);
2584 }
2585
2586 static void qib_7322_free_irq(struct qib_devdata *dd)
2587 {
2588         if (dd->cspec->irq) {
2589                 free_irq(dd->cspec->irq, dd);
2590                 dd->cspec->irq = 0;
2591         }
2592         qib_7322_nomsix(dd);
2593 }
2594
2595 static void qib_setup_7322_cleanup(struct qib_devdata *dd)
2596 {
2597         int i;
2598
2599         qib_7322_free_irq(dd);
2600         kfree(dd->cspec->cntrs);
2601         kfree(dd->cspec->sendchkenable);
2602         kfree(dd->cspec->sendgrhchk);
2603         kfree(dd->cspec->sendibchk);
2604         kfree(dd->cspec->msix_entries);
2605         for (i = 0; i < dd->num_pports; i++) {
2606                 unsigned long flags;
2607                 u32 mask = QSFP_GPIO_MOD_PRS_N |
2608                         (QSFP_GPIO_MOD_PRS_N << QSFP_GPIO_PORT2_SHIFT);
2609
2610                 kfree(dd->pport[i].cpspec->portcntrs);
2611                 if (dd->flags & QIB_HAS_QSFP) {
2612                         spin_lock_irqsave(&dd->cspec->gpio_lock, flags);
2613                         dd->cspec->gpio_mask &= ~mask;
2614                         qib_write_kreg(dd, kr_gpio_mask, dd->cspec->gpio_mask);
2615                         spin_unlock_irqrestore(&dd->cspec->gpio_lock, flags);
2616                         qib_qsfp_deinit(&dd->pport[i].cpspec->qsfp_data);
2617                 }
2618                 if (dd->pport[i].ibport_data.smi_ah)
2619                         ib_destroy_ah(&dd->pport[i].ibport_data.smi_ah->ibah);
2620         }
2621 }
2622
2623 /* handle SDMA interrupts */
2624 static void sdma_7322_intr(struct qib_devdata *dd, u64 istat)
2625 {
2626         struct qib_pportdata *ppd0 = &dd->pport[0];
2627         struct qib_pportdata *ppd1 = &dd->pport[1];
2628         u64 intr0 = istat & (INT_MASK_P(SDma, 0) |
2629                 INT_MASK_P(SDmaIdle, 0) | INT_MASK_P(SDmaProgress, 0));
2630         u64 intr1 = istat & (INT_MASK_P(SDma, 1) |
2631                 INT_MASK_P(SDmaIdle, 1) | INT_MASK_P(SDmaProgress, 1));
2632
2633         if (intr0)
2634                 qib_sdma_intr(ppd0);
2635         if (intr1)
2636                 qib_sdma_intr(ppd1);
2637
2638         if (istat & INT_MASK_PM(SDmaCleanupDone, 0))
2639                 qib_sdma_process_event(ppd0, qib_sdma_event_e20_hw_started);
2640         if (istat & INT_MASK_PM(SDmaCleanupDone, 1))
2641                 qib_sdma_process_event(ppd1, qib_sdma_event_e20_hw_started);
2642 }
2643
2644 /*
2645  * Set or clear the Send buffer available interrupt enable bit.
2646  */
2647 static void qib_wantpiobuf_7322_intr(struct qib_devdata *dd, u32 needint)
2648 {
2649         unsigned long flags;
2650
2651         spin_lock_irqsave(&dd->sendctrl_lock, flags);
2652         if (needint)
2653                 dd->sendctrl |= SYM_MASK(SendCtrl, SendIntBufAvail);
2654         else
2655                 dd->sendctrl &= ~SYM_MASK(SendCtrl, SendIntBufAvail);
2656         qib_write_kreg(dd, kr_sendctrl, dd->sendctrl);
2657         qib_write_kreg(dd, kr_scratch, 0ULL);
2658         spin_unlock_irqrestore(&dd->sendctrl_lock, flags);
2659 }
2660
2661 /*
2662  * Somehow got an interrupt with reserved bits set in interrupt status.
2663  * Print a message so we know it happened, then clear them.
2664  * keep mainline interrupt handler cache-friendly
2665  */
2666 static noinline void unknown_7322_ibits(struct qib_devdata *dd, u64 istat)
2667 {
2668         u64 kills;
2669         char msg[128];
2670
2671         kills = istat & ~QIB_I_BITSEXTANT;
2672         qib_dev_err(dd, "Clearing reserved interrupt(s) 0x%016llx:"
2673                     " %s\n", (unsigned long long) kills, msg);
2674         qib_write_kreg(dd, kr_intmask, (dd->cspec->int_enable_mask & ~kills));
2675 }
2676
2677 /* keep mainline interrupt handler cache-friendly */
2678 static noinline void unknown_7322_gpio_intr(struct qib_devdata *dd)
2679 {
2680         u32 gpiostatus;
2681         int handled = 0;
2682         int pidx;
2683
2684         /*
2685          * Boards for this chip currently don't use GPIO interrupts,
2686          * so clear by writing GPIOstatus to GPIOclear, and complain
2687          * to developer.  To avoid endless repeats, clear
2688          * the bits in the mask, since there is some kind of
2689          * programming error or chip problem.
2690          */
2691         gpiostatus = qib_read_kreg32(dd, kr_gpio_status);
2692         /*
2693          * In theory, writing GPIOstatus to GPIOclear could
2694          * have a bad side-effect on some diagnostic that wanted
2695          * to poll for a status-change, but the various shadows
2696          * make that problematic at best. Diags will just suppress
2697          * all GPIO interrupts during such tests.
2698          */
2699         qib_write_kreg(dd, kr_gpio_clear, gpiostatus);
2700         /*
2701          * Check for QSFP MOD_PRS changes
2702          * only works for single port if IB1 != pidx1
2703          */
2704         for (pidx = 0; pidx < dd->num_pports && (dd->flags & QIB_HAS_QSFP);
2705              ++pidx) {
2706                 struct qib_pportdata *ppd;
2707                 struct qib_qsfp_data *qd;
2708                 u32 mask;
2709                 if (!dd->pport[pidx].link_speed_supported)
2710                         continue;
2711                 mask = QSFP_GPIO_MOD_PRS_N;
2712                 ppd = dd->pport + pidx;
2713                 mask <<= (QSFP_GPIO_PORT2_SHIFT * ppd->hw_pidx);
2714                 if (gpiostatus & dd->cspec->gpio_mask & mask) {
2715                         u64 pins;
2716                         qd = &ppd->cpspec->qsfp_data;
2717                         gpiostatus &= ~mask;
2718                         pins = qib_read_kreg64(dd, kr_extstatus);
2719                         pins >>= SYM_LSB(EXTStatus, GPIOIn);
2720                         if (!(pins & mask)) {
2721                                 ++handled;
2722                                 qd->t_insert = jiffies;
2723                                 queue_work(ib_wq, &qd->work);
2724                         }
2725                 }
2726         }
2727
2728         if (gpiostatus && !handled) {
2729                 const u32 mask = qib_read_kreg32(dd, kr_gpio_mask);
2730                 u32 gpio_irq = mask & gpiostatus;
2731
2732                 /*
2733                  * Clear any troublemakers, and update chip from shadow
2734                  */
2735                 dd->cspec->gpio_mask &= ~gpio_irq;
2736                 qib_write_kreg(dd, kr_gpio_mask, dd->cspec->gpio_mask);
2737         }
2738 }
2739
2740 /*
2741  * Handle errors and unusual events first, separate function
2742  * to improve cache hits for fast path interrupt handling.
2743  */
2744 static noinline void unlikely_7322_intr(struct qib_devdata *dd, u64 istat)
2745 {
2746         if (istat & ~QIB_I_BITSEXTANT)
2747                 unknown_7322_ibits(dd, istat);
2748         if (istat & QIB_I_GPIO)
2749                 unknown_7322_gpio_intr(dd);
2750         if (istat & QIB_I_C_ERROR) {
2751                 qib_write_kreg(dd, kr_errmask, 0ULL);
2752                 tasklet_schedule(&dd->error_tasklet);
2753         }
2754         if (istat & INT_MASK_P(Err, 0) && dd->rcd[0])
2755                 handle_7322_p_errors(dd->rcd[0]->ppd);
2756         if (istat & INT_MASK_P(Err, 1) && dd->rcd[1])
2757                 handle_7322_p_errors(dd->rcd[1]->ppd);
2758 }
2759
2760 /*
2761  * Dynamically adjust the rcv int timeout for a context based on incoming
2762  * packet rate.
2763  */
2764 static void adjust_rcv_timeout(struct qib_ctxtdata *rcd, int npkts)
2765 {
2766         struct qib_devdata *dd = rcd->dd;
2767         u32 timeout = dd->cspec->rcvavail_timeout[rcd->ctxt];
2768
2769         /*
2770          * Dynamically adjust idle timeout on chip
2771          * based on number of packets processed.
2772          */
2773         if (npkts < rcv_int_count && timeout > 2)
2774                 timeout >>= 1;
2775         else if (npkts >= rcv_int_count && timeout < rcv_int_timeout)
2776                 timeout = min(timeout << 1, rcv_int_timeout);
2777         else
2778                 return;
2779
2780         dd->cspec->rcvavail_timeout[rcd->ctxt] = timeout;
2781         qib_write_kreg(dd, kr_rcvavailtimeout + rcd->ctxt, timeout);
2782 }
2783
2784 /*
2785  * This is the main interrupt handler.
2786  * It will normally only be used for low frequency interrupts but may
2787  * have to handle all interrupts if INTx is enabled or fewer than normal
2788  * MSIx interrupts were allocated.
2789  * This routine should ignore the interrupt bits for any of the
2790  * dedicated MSIx handlers.
2791  */
2792 static irqreturn_t qib_7322intr(int irq, void *data)
2793 {
2794         struct qib_devdata *dd = data;
2795         irqreturn_t ret;
2796         u64 istat;
2797         u64 ctxtrbits;
2798         u64 rmask;
2799         unsigned i;
2800         u32 npkts;
2801
2802         if ((dd->flags & (QIB_PRESENT | QIB_BADINTR)) != QIB_PRESENT) {
2803                 /*
2804                  * This return value is not great, but we do not want the
2805                  * interrupt core code to remove our interrupt handler
2806                  * because we don't appear to be handling an interrupt
2807                  * during a chip reset.
2808                  */
2809                 ret = IRQ_HANDLED;
2810                 goto bail;
2811         }
2812
2813         istat = qib_read_kreg64(dd, kr_intstatus);
2814
2815         if (unlikely(istat == ~0ULL)) {
2816                 qib_bad_intrstatus(dd);
2817                 qib_dev_err(dd, "Interrupt status all f's, skipping\n");
2818                 /* don't know if it was our interrupt or not */
2819                 ret = IRQ_NONE;
2820                 goto bail;
2821         }
2822
2823         istat &= dd->cspec->main_int_mask;
2824         if (unlikely(!istat)) {
2825                 /* already handled, or shared and not us */
2826                 ret = IRQ_NONE;
2827                 goto bail;
2828         }
2829
2830         qib_stats.sps_ints++;
2831         if (dd->int_counter != (u32) -1)
2832                 dd->int_counter++;
2833
2834         /* handle "errors" of various kinds first, device ahead of port */
2835         if (unlikely(istat & (~QIB_I_BITSEXTANT | QIB_I_GPIO |
2836                               QIB_I_C_ERROR | INT_MASK_P(Err, 0) |
2837                               INT_MASK_P(Err, 1))))
2838                 unlikely_7322_intr(dd, istat);
2839
2840         /*
2841          * Clear the interrupt bits we found set, relatively early, so we
2842          * "know" know the chip will have seen this by the time we process
2843          * the queue, and will re-interrupt if necessary.  The processor
2844          * itself won't take the interrupt again until we return.
2845          */
2846         qib_write_kreg(dd, kr_intclear, istat);
2847
2848         /*
2849          * Handle kernel receive queues before checking for pio buffers
2850          * available since receives can overflow; piobuf waiters can afford
2851          * a few extra cycles, since they were waiting anyway.
2852          */
2853         ctxtrbits = istat & (QIB_I_RCVAVAIL_MASK | QIB_I_RCVURG_MASK);
2854         if (ctxtrbits) {
2855                 rmask = (1ULL << QIB_I_RCVAVAIL_LSB) |
2856                         (1ULL << QIB_I_RCVURG_LSB);
2857                 for (i = 0; i < dd->first_user_ctxt; i++) {
2858                         if (ctxtrbits & rmask) {
2859                                 ctxtrbits &= ~rmask;
2860                                 if (dd->rcd[i])
2861                                         qib_kreceive(dd->rcd[i], NULL, &npkts);
2862                         }
2863                         rmask <<= 1;
2864                 }
2865                 if (ctxtrbits) {
2866                         ctxtrbits = (ctxtrbits >> QIB_I_RCVAVAIL_LSB) |
2867                                 (ctxtrbits >> QIB_I_RCVURG_LSB);
2868                         qib_handle_urcv(dd, ctxtrbits);
2869                 }
2870         }
2871
2872         if (istat & (QIB_I_P_SDMAINT(0) | QIB_I_P_SDMAINT(1)))
2873                 sdma_7322_intr(dd, istat);
2874
2875         if ((istat & QIB_I_SPIOBUFAVAIL) && (dd->flags & QIB_INITTED))
2876                 qib_ib_piobufavail(dd);
2877
2878         ret = IRQ_HANDLED;
2879 bail:
2880         return ret;
2881 }
2882
2883 /*
2884  * Dedicated receive packet available interrupt handler.
2885  */
2886 static irqreturn_t qib_7322pintr(int irq, void *data)
2887 {
2888         struct qib_ctxtdata *rcd = data;
2889         struct qib_devdata *dd = rcd->dd;
2890         u32 npkts;
2891
2892         if ((dd->flags & (QIB_PRESENT | QIB_BADINTR)) != QIB_PRESENT)
2893                 /*
2894                  * This return value is not great, but we do not want the
2895                  * interrupt core code to remove our interrupt handler
2896                  * because we don't appear to be handling an interrupt
2897                  * during a chip reset.
2898                  */
2899                 return IRQ_HANDLED;
2900
2901         qib_stats.sps_ints++;
2902         if (dd->int_counter != (u32) -1)
2903                 dd->int_counter++;
2904
2905         /* Clear the interrupt bit we expect to be set. */
2906         qib_write_kreg(dd, kr_intclear, ((1ULL << QIB_I_RCVAVAIL_LSB) |
2907                        (1ULL << QIB_I_RCVURG_LSB)) << rcd->ctxt);
2908
2909         qib_kreceive(rcd, NULL, &npkts);
2910
2911         return IRQ_HANDLED;
2912 }
2913
2914 /*
2915  * Dedicated Send buffer available interrupt handler.
2916  */
2917 static irqreturn_t qib_7322bufavail(int irq, void *data)
2918 {
2919         struct qib_devdata *dd = data;
2920
2921         if ((dd->flags & (QIB_PRESENT | QIB_BADINTR)) != QIB_PRESENT)
2922                 /*
2923                  * This return value is not great, but we do not want the
2924                  * interrupt core code to remove our interrupt handler
2925                  * because we don't appear to be handling an interrupt
2926                  * during a chip reset.
2927                  */
2928                 return IRQ_HANDLED;
2929
2930         qib_stats.sps_ints++;
2931         if (dd->int_counter != (u32) -1)
2932                 dd->int_counter++;
2933
2934         /* Clear the interrupt bit we expect to be set. */
2935         qib_write_kreg(dd, kr_intclear, QIB_I_SPIOBUFAVAIL);
2936
2937         /* qib_ib_piobufavail() will clear the want PIO interrupt if needed */
2938         if (dd->flags & QIB_INITTED)
2939                 qib_ib_piobufavail(dd);
2940         else
2941                 qib_wantpiobuf_7322_intr(dd, 0);
2942
2943         return IRQ_HANDLED;
2944 }
2945
2946 /*
2947  * Dedicated Send DMA interrupt handler.
2948  */
2949 static irqreturn_t sdma_intr(int irq, void *data)
2950 {
2951         struct qib_pportdata *ppd = data;
2952         struct qib_devdata *dd = ppd->dd;
2953
2954         if ((dd->flags & (QIB_PRESENT | QIB_BADINTR)) != QIB_PRESENT)
2955                 /*
2956                  * This return value is not great, but we do not want the
2957                  * interrupt core code to remove our interrupt handler
2958                  * because we don't appear to be handling an interrupt
2959                  * during a chip reset.
2960                  */
2961                 return IRQ_HANDLED;
2962
2963         qib_stats.sps_ints++;
2964         if (dd->int_counter != (u32) -1)
2965                 dd->int_counter++;
2966
2967         /* Clear the interrupt bit we expect to be set. */
2968         qib_write_kreg(dd, kr_intclear, ppd->hw_pidx ?
2969                        INT_MASK_P(SDma, 1) : INT_MASK_P(SDma, 0));
2970         qib_sdma_intr(ppd);
2971
2972         return IRQ_HANDLED;
2973 }
2974
2975 /*
2976  * Dedicated Send DMA idle interrupt handler.
2977  */
2978 static irqreturn_t sdma_idle_intr(int irq, void *data)
2979 {
2980         struct qib_pportdata *ppd = data;
2981         struct qib_devdata *dd = ppd->dd;
2982
2983         if ((dd->flags & (QIB_PRESENT | QIB_BADINTR)) != QIB_PRESENT)
2984                 /*
2985                  * This return value is not great, but we do not want the
2986                  * interrupt core code to remove our interrupt handler
2987                  * because we don't appear to be handling an interrupt
2988                  * during a chip reset.
2989                  */
2990                 return IRQ_HANDLED;
2991
2992         qib_stats.sps_ints++;
2993         if (dd->int_counter != (u32) -1)
2994                 dd->int_counter++;
2995
2996         /* Clear the interrupt bit we expect to be set. */
2997         qib_write_kreg(dd, kr_intclear, ppd->hw_pidx ?
2998                        INT_MASK_P(SDmaIdle, 1) : INT_MASK_P(SDmaIdle, 0));
2999         qib_sdma_intr(ppd);
3000
3001         return IRQ_HANDLED;
3002 }
3003
3004 /*
3005  * Dedicated Send DMA progress interrupt handler.
3006  */
3007 static irqreturn_t sdma_progress_intr(int irq, void *data)
3008 {
3009         struct qib_pportdata *ppd = data;
3010         struct qib_devdata *dd = ppd->dd;
3011
3012         if ((dd->flags & (QIB_PRESENT | QIB_BADINTR)) != QIB_PRESENT)
3013                 /*
3014                  * This return value is not great, but we do not want the
3015                  * interrupt core code to remove our interrupt handler
3016                  * because we don't appear to be handling an interrupt
3017                  * during a chip reset.
3018                  */
3019                 return IRQ_HANDLED;
3020
3021         qib_stats.sps_ints++;
3022         if (dd->int_counter != (u32) -1)
3023                 dd->int_counter++;
3024
3025         /* Clear the interrupt bit we expect to be set. */
3026         qib_write_kreg(dd, kr_intclear, ppd->hw_pidx ?
3027                        INT_MASK_P(SDmaProgress, 1) :
3028                        INT_MASK_P(SDmaProgress, 0));
3029         qib_sdma_intr(ppd);
3030
3031         return IRQ_HANDLED;
3032 }
3033
3034 /*
3035  * Dedicated Send DMA cleanup interrupt handler.
3036  */
3037 static irqreturn_t sdma_cleanup_intr(int irq, void *data)
3038 {
3039         struct qib_pportdata *ppd = data;
3040         struct qib_devdata *dd = ppd->dd;
3041
3042         if ((dd->flags & (QIB_PRESENT | QIB_BADINTR)) != QIB_PRESENT)
3043                 /*
3044                  * This return value is not great, but we do not want the
3045                  * interrupt core code to remove our interrupt handler
3046                  * because we don't appear to be handling an interrupt
3047                  * during a chip reset.
3048                  */
3049                 return IRQ_HANDLED;
3050
3051         qib_stats.sps_ints++;
3052         if (dd->int_counter != (u32) -1)
3053                 dd->int_counter++;
3054
3055         /* Clear the interrupt bit we expect to be set. */
3056         qib_write_kreg(dd, kr_intclear, ppd->hw_pidx ?
3057                        INT_MASK_PM(SDmaCleanupDone, 1) :
3058                        INT_MASK_PM(SDmaCleanupDone, 0));
3059         qib_sdma_process_event(ppd, qib_sdma_event_e20_hw_started);
3060
3061         return IRQ_HANDLED;
3062 }
3063
3064 /*
3065  * Set up our chip-specific interrupt handler.
3066  * The interrupt type has already been setup, so
3067  * we just need to do the registration and error checking.
3068  * If we are using MSIx interrupts, we may fall back to
3069  * INTx later, if the interrupt handler doesn't get called
3070  * within 1/2 second (see verify_interrupt()).
3071  */
3072 static void qib_setup_7322_interrupt(struct qib_devdata *dd, int clearpend)
3073 {
3074         int ret, i, msixnum;
3075         u64 redirect[6];
3076         u64 mask;
3077         const struct cpumask *local_mask;
3078         int firstcpu, secondcpu = 0, currrcvcpu = 0;
3079
3080         if (!dd->num_pports)
3081                 return;
3082
3083         if (clearpend) {
3084                 /*
3085                  * if not switching interrupt types, be sure interrupts are
3086                  * disabled, and then clear anything pending at this point,
3087                  * because we are starting clean.
3088                  */
3089                 qib_7322_set_intr_state(dd, 0);
3090
3091                 /* clear the reset error, init error/hwerror mask */
3092                 qib_7322_init_hwerrors(dd);
3093
3094                 /* clear any interrupt bits that might be set */
3095                 qib_write_kreg(dd, kr_intclear, ~0ULL);
3096
3097                 /* make sure no pending MSIx intr, and clear diag reg */
3098                 qib_write_kreg(dd, kr_intgranted, ~0ULL);
3099                 qib_write_kreg(dd, kr_vecclr_wo_int, ~0ULL);
3100         }
3101
3102         if (!dd->cspec->num_msix_entries) {
3103                 /* Try to get INTx interrupt */
3104 try_intx:
3105                 if (!dd->pcidev->irq) {
3106                         qib_dev_err(dd, "irq is 0, BIOS error?  "
3107                                     "Interrupts won't work\n");
3108                         goto bail;
3109                 }
3110                 ret = request_irq(dd->pcidev->irq, qib_7322intr,
3111                                   IRQF_SHARED, QIB_DRV_NAME, dd);
3112                 if (ret) {
3113                         qib_dev_err(dd, "Couldn't setup INTx "
3114                                     "interrupt (irq=%d): %d\n",
3115                                     dd->pcidev->irq, ret);
3116                         goto bail;
3117                 }
3118                 dd->cspec->irq = dd->pcidev->irq;
3119                 dd->cspec->main_int_mask = ~0ULL;
3120                 goto bail;
3121         }
3122
3123         /* Try to get MSIx interrupts */
3124         memset(redirect, 0, sizeof redirect);
3125         mask = ~0ULL;
3126         msixnum = 0;
3127         local_mask = cpumask_of_pcibus(dd->pcidev->bus);
3128         firstcpu = cpumask_first(local_mask);
3129         if (firstcpu >= nr_cpu_ids ||
3130                         cpumask_weight(local_mask) == num_online_cpus()) {
3131                 local_mask = topology_core_cpumask(0);
3132                 firstcpu = cpumask_first(local_mask);
3133         }
3134         if (firstcpu < nr_cpu_ids) {
3135                 secondcpu = cpumask_next(firstcpu, local_mask);
3136                 if (secondcpu >= nr_cpu_ids)
3137                         secondcpu = firstcpu;
3138                 currrcvcpu = secondcpu;
3139         }
3140         for (i = 0; msixnum < dd->cspec->num_msix_entries; i++) {
3141                 irq_handler_t handler;
3142                 void *arg;
3143                 u64 val;
3144                 int lsb, reg, sh;
3145
3146                 dd->cspec->msix_entries[msixnum].
3147                         name[sizeof(dd->cspec->msix_entries[msixnum].name) - 1]
3148                         = '\0';
3149                 if (i < ARRAY_SIZE(irq_table)) {
3150                         if (irq_table[i].port) {
3151                                 /* skip if for a non-configured port */
3152                                 if (irq_table[i].port > dd->num_pports)
3153                                         continue;
3154                                 arg = dd->pport + irq_table[i].port - 1;
3155                         } else
3156                                 arg = dd;
3157                         lsb = irq_table[i].lsb;
3158                         handler = irq_table[i].handler;
3159                         snprintf(dd->cspec->msix_entries[msixnum].name,
3160                                 sizeof(dd->cspec->msix_entries[msixnum].name)
3161                                  - 1,
3162                                 QIB_DRV_NAME "%d%s", dd->unit,
3163                                 irq_table[i].name);
3164                 } else {
3165                         unsigned ctxt;
3166
3167                         ctxt = i - ARRAY_SIZE(irq_table);
3168                         /* per krcvq context receive interrupt */
3169                         arg = dd->rcd[ctxt];
3170                         if (!arg)
3171                                 continue;
3172                         if (qib_krcvq01_no_msi && ctxt < 2)
3173                                 continue;
3174                         lsb = QIB_I_RCVAVAIL_LSB + ctxt;
3175                         handler = qib_7322pintr;
3176                         snprintf(dd->cspec->msix_entries[msixnum].name,
3177                                 sizeof(dd->cspec->msix_entries[msixnum].name)
3178                                  - 1,
3179                                 QIB_DRV_NAME "%d (kctx)", dd->unit);
3180                 }
3181                 ret = request_irq(
3182                         dd->cspec->msix_entries[msixnum].msix.vector,
3183                         handler, 0, dd->cspec->msix_entries[msixnum].name,
3184                         arg);
3185                 if (ret) {
3186                         /*
3187                          * Shouldn't happen since the enable said we could
3188                          * have as many as we are trying to setup here.
3189                          */
3190                         qib_dev_err(dd, "Couldn't setup MSIx "
3191                                 "interrupt (vec=%d, irq=%d): %d\n", msixnum,
3192                                 dd->cspec->msix_entries[msixnum].msix.vector,
3193                                 ret);
3194                         qib_7322_nomsix(dd);
3195                         goto try_intx;
3196                 }
3197                 dd->cspec->msix_entries[msixnum].arg = arg;
3198                 if (lsb >= 0) {
3199                         reg = lsb / IBA7322_REDIRECT_VEC_PER_REG;
3200                         sh = (lsb % IBA7322_REDIRECT_VEC_PER_REG) *
3201                                 SYM_LSB(IntRedirect0, vec1);
3202                         mask &= ~(1ULL << lsb);
3203                         redirect[reg] |= ((u64) msixnum) << sh;
3204                 }
3205                 val = qib_read_kreg64(dd, 2 * msixnum + 1 +
3206                         (QIB_7322_MsixTable_OFFS / sizeof(u64)));
3207                 if (firstcpu < nr_cpu_ids &&
3208                         zalloc_cpumask_var(
3209                                 &dd->cspec->msix_entries[msixnum].mask,
3210                                 GFP_KERNEL)) {
3211                         if (handler == qib_7322pintr) {
3212                                 cpumask_set_cpu(currrcvcpu,
3213                                         dd->cspec->msix_entries[msixnum].mask);
3214                                 currrcvcpu = cpumask_next(currrcvcpu,
3215                                         local_mask);
3216                                 if (currrcvcpu >= nr_cpu_ids)
3217                                         currrcvcpu = secondcpu;
3218                         } else {
3219                                 cpumask_set_cpu(firstcpu,
3220                                         dd->cspec->msix_entries[msixnum].mask);
3221                         }
3222                         irq_set_affinity_hint(
3223                                 dd->cspec->msix_entries[msixnum].msix.vector,
3224                                 dd->cspec->msix_entries[msixnum].mask);
3225                 }
3226                 msixnum++;
3227         }
3228         /* Initialize the vector mapping */
3229         for (i = 0; i < ARRAY_SIZE(redirect); i++)
3230                 qib_write_kreg(dd, kr_intredirect + i, redirect[i]);
3231         dd->cspec->main_int_mask = mask;
3232         tasklet_init(&dd->error_tasklet, qib_error_tasklet,
3233                 (unsigned long)dd);
3234 bail:;
3235 }
3236
3237 /**
3238  * qib_7322_boardname - fill in the board name and note features
3239  * @dd: the qlogic_ib device
3240  *
3241  * info will be based on the board revision register
3242  */
3243 static unsigned qib_7322_boardname(struct qib_devdata *dd)
3244 {
3245         /* Will need enumeration of board-types here */
3246         char *n;
3247         u32 boardid, namelen;
3248         unsigned features = DUAL_PORT_CAP;
3249
3250         boardid = SYM_FIELD(dd->revision, Revision, BoardID);
3251
3252         switch (boardid) {
3253         case 0:
3254                 n = "InfiniPath_QLE7342_Emulation";
3255                 break;
3256         case 1:
3257                 n = "InfiniPath_QLE7340";
3258                 dd->flags |= QIB_HAS_QSFP;
3259                 features = PORT_SPD_CAP;
3260                 break;
3261         case 2:
3262                 n = "InfiniPath_QLE7342";
3263                 dd->flags |= QIB_HAS_QSFP;
3264                 break;
3265         case 3:
3266                 n = "InfiniPath_QMI7342";
3267                 break;
3268         case 4:
3269                 n = "InfiniPath_Unsupported7342";
3270                 qib_dev_err(dd, "Unsupported version of QMH7342\n");
3271                 features = 0;
3272                 break;
3273         case BOARD_QMH7342:
3274                 n = "InfiniPath_QMH7342";
3275                 features = 0x24;
3276                 break;
3277         case BOARD_QME7342:
3278                 n = "InfiniPath_QME7342";
3279                 break;
3280         case 8:
3281                 n = "InfiniPath_QME7362";
3282                 dd->flags |= QIB_HAS_QSFP;
3283                 break;
3284         case 15:
3285                 n = "InfiniPath_QLE7342_TEST";
3286                 dd->flags |= QIB_HAS_QSFP;
3287                 break;
3288         default:
3289                 n = "InfiniPath_QLE73xy_UNKNOWN";
3290                 qib_dev_err(dd, "Unknown 7322 board type %u\n", boardid);
3291                 break;
3292         }
3293         dd->board_atten = 1; /* index into txdds_Xdr */
3294
3295         namelen = strlen(n) + 1;
3296         dd->boardname = kmalloc(namelen, GFP_KERNEL);
3297         if (!dd->boardname)
3298                 qib_dev_err(dd, "Failed allocation for board name: %s\n", n);
3299         else
3300                 snprintf(dd->boardname, namelen, "%s", n);
3301
3302         snprintf(dd->boardversion, sizeof(dd->boardversion),
3303                  "ChipABI %u.%u, %s, InfiniPath%u %u.%u, SW Compat %u\n",
3304                  QIB_CHIP_VERS_MAJ, QIB_CHIP_VERS_MIN, dd->boardname,
3305                  (unsigned)SYM_FIELD(dd->revision, Revision_R, Arch),
3306                  dd->majrev, dd->minrev,
3307                  (unsigned)SYM_FIELD(dd->revision, Revision_R, SW));
3308
3309         if (qib_singleport && (features >> PORT_SPD_CAP_SHIFT) & PORT_SPD_CAP) {
3310                 qib_devinfo(dd->pcidev, "IB%u: Forced to single port mode"
3311                             " by module parameter\n", dd->unit);
3312                 features &= PORT_SPD_CAP;
3313         }
3314
3315         return features;
3316 }
3317
3318 /*
3319  * This routine sleeps, so it can only be called from user context, not
3320  * from interrupt context.
3321  */
3322 static int qib_do_7322_reset(struct qib_devdata *dd)
3323 {
3324         u64 val;
3325         u64 *msix_vecsave;
3326         int i, msix_entries, ret = 1;
3327         u16 cmdval;
3328         u8 int_line, clinesz;
3329         unsigned long flags;
3330
3331         /* Use dev_err so it shows up in logs, etc. */
3332         qib_dev_err(dd, "Resetting InfiniPath unit %u\n", dd->unit);
3333
3334         qib_pcie_getcmd(dd, &cmdval, &int_line, &clinesz);
3335
3336         msix_entries = dd->cspec->num_msix_entries;
3337
3338         /* no interrupts till re-initted */
3339         qib_7322_set_intr_state(dd, 0);
3340
3341         if (msix_entries) {
3342                 qib_7322_nomsix(dd);
3343                 /* can be up to 512 bytes, too big for stack */
3344                 msix_vecsave = kmalloc(2 * dd->cspec->num_msix_entries *
3345                         sizeof(u64), GFP_KERNEL);
3346                 if (!msix_vecsave)
3347                         qib_dev_err(dd, "No mem to save MSIx data\n");
3348         } else
3349                 msix_vecsave = NULL;
3350
3351         /*
3352          * Core PCI (as of 2.6.18) doesn't save or rewrite the full vector
3353          * info that is set up by the BIOS, so we have to save and restore
3354          * it ourselves.   There is some risk something could change it,
3355          * after we save it, but since we have disabled the MSIx, it
3356          * shouldn't be touched...
3357          */
3358         for (i = 0; i < msix_entries; i++) {
3359                 u64 vecaddr, vecdata;
3360                 vecaddr = qib_read_kreg64(dd, 2 * i +
3361                                   (QIB_7322_MsixTable_OFFS / sizeof(u64)));
3362                 vecdata = qib_read_kreg64(dd, 1 + 2 * i +
3363                                   (QIB_7322_MsixTable_OFFS / sizeof(u64)));
3364                 if (msix_vecsave) {
3365                         msix_vecsave[2 * i] = vecaddr;
3366                         /* save it without the masked bit set */
3367                         msix_vecsave[1 + 2 * i] = vecdata & ~0x100000000ULL;
3368                 }
3369         }
3370
3371         dd->pport->cpspec->ibdeltainprog = 0;
3372         dd->pport->cpspec->ibsymdelta = 0;
3373         dd->pport->cpspec->iblnkerrdelta = 0;
3374         dd->pport->cpspec->ibmalfdelta = 0;
3375         dd->int_counter = 0; /* so we check interrupts work again */
3376
3377         /*
3378          * Keep chip from being accessed until we are ready.  Use
3379          * writeq() directly, to allow the write even though QIB_PRESENT
3380          * isn't set.
3381          */
3382         dd->flags &= ~(QIB_INITTED | QIB_PRESENT | QIB_BADINTR);
3383         dd->flags |= QIB_DOING_RESET;
3384         val = dd->control | QLOGIC_IB_C_RESET;
3385         writeq(val, &dd->kregbase[kr_control]);
3386
3387         for (i = 1; i <= 5; i++) {
3388                 /*
3389                  * Allow MBIST, etc. to complete; longer on each retry.
3390                  * We sometimes get machine checks from bus timeout if no
3391                  * response, so for now, make it *really* long.
3392                  */
3393                 msleep(1000 + (1 + i) * 3000);
3394
3395                 qib_pcie_reenable(dd, cmdval, int_line, clinesz);
3396
3397                 /*
3398                  * Use readq directly, so we don't need to mark it as PRESENT
3399                  * until we get a successful indication that all is well.
3400                  */
3401                 val = readq(&dd->kregbase[kr_revision]);
3402                 if (val == dd->revision)
3403                         break;
3404                 if (i == 5) {
3405                         qib_dev_err(dd, "Failed to initialize after reset, "
3406                                     "unusable\n");
3407                         ret = 0;
3408                         goto  bail;
3409                 }
3410         }
3411
3412         dd->flags |= QIB_PRESENT; /* it's back */
3413
3414         if (msix_entries) {
3415                 /* restore the MSIx vector address and data if saved above */
3416                 for (i = 0; i < msix_entries; i++) {
3417                         dd->cspec->msix_entries[i].msix.entry = i;
3418                         if (!msix_vecsave || !msix_vecsave[2 * i])
3419                                 continue;
3420                         qib_write_kreg(dd, 2 * i +
3421                                 (QIB_7322_MsixTable_OFFS / sizeof(u64)),
3422                                 msix_vecsave[2 * i]);
3423                         qib_write_kreg(dd, 1 + 2 * i +
3424                                 (QIB_7322_MsixTable_OFFS / sizeof(u64)),
3425                                 msix_vecsave[1 + 2 * i]);
3426                 }
3427         }
3428
3429         /* initialize the remaining registers.  */
3430         for (i = 0; i < dd->num_pports; ++i)
3431                 write_7322_init_portregs(&dd->pport[i]);
3432         write_7322_initregs(dd);
3433
3434         if (qib_pcie_params(dd, dd->lbus_width,
3435                             &dd->cspec->num_msix_entries,
3436                             dd->cspec->msix_entries))
3437                 qib_dev_err(dd, "Reset failed to setup PCIe or interrupts; "
3438                                 "continuing anyway\n");
3439
3440         qib_setup_7322_interrupt(dd, 1);
3441
3442         for (i = 0; i < dd->num_pports; ++i) {
3443                 struct qib_pportdata *ppd = &dd->pport[i];
3444
3445                 spin_lock_irqsave(&ppd->lflags_lock, flags);
3446                 ppd->lflags |= QIBL_IB_FORCE_NOTIFY;
3447                 ppd->lflags &= ~QIBL_IB_AUTONEG_FAILED;
3448                 spin_unlock_irqrestore(&ppd->lflags_lock, flags);
3449         }
3450
3451 bail:
3452         dd->flags &= ~QIB_DOING_RESET; /* OK or not, no longer resetting */
3453         kfree(msix_vecsave);
3454         return ret;
3455 }
3456
3457 /**
3458  * qib_7322_put_tid - write a TID to the chip
3459  * @dd: the qlogic_ib device
3460  * @tidptr: pointer to the expected TID (in chip) to update
3461  * @tidtype: 0 for eager, 1 for expected
3462  * @pa: physical address of in memory buffer; tidinvalid if freeing
3463  */
3464 static void qib_7322_put_tid(struct qib_devdata *dd, u64 __iomem *tidptr,
3465                              u32 type, unsigned long pa)
3466 {
3467         if (!(dd->flags & QIB_PRESENT))
3468                 return;
3469         if (pa != dd->tidinvalid) {
3470                 u64 chippa = pa >> IBA7322_TID_PA_SHIFT;
3471
3472                 /* paranoia checks */
3473                 if (pa != (chippa << IBA7322_TID_PA_SHIFT)) {
3474                         qib_dev_err(dd, "Physaddr %lx not 2KB aligned!\n",
3475                                     pa);
3476                         return;
3477                 }
3478                 if (chippa >= (1UL << IBA7322_TID_SZ_SHIFT)) {
3479                         qib_dev_err(dd, "Physical page address 0x%lx "
3480                                 "larger than supported\n", pa);
3481                         return;
3482                 }
3483
3484                 if (type == RCVHQ_RCV_TYPE_EAGER)
3485                         chippa |= dd->tidtemplate;
3486                 else /* for now, always full 4KB page */
3487                         chippa |= IBA7322_TID_SZ_4K;
3488                 pa = chippa;
3489         }
3490         writeq(pa, tidptr);
3491         mmiowb();
3492 }
3493
3494 /**
3495  * qib_7322_clear_tids - clear all TID entries for a ctxt, expected and eager
3496  * @dd: the qlogic_ib device
3497  * @ctxt: the ctxt
3498  *
3499  * clear all TID entries for a ctxt, expected and eager.
3500  * Used from qib_close().
3501  */
3502 static void qib_7322_clear_tids(struct qib_devdata *dd,
3503                                 struct qib_ctxtdata *rcd)
3504 {
3505         u64 __iomem *tidbase;
3506         unsigned long tidinv;
3507         u32 ctxt;
3508         int i;
3509
3510         if (!dd->kregbase || !rcd)
3511                 return;
3512
3513         ctxt = rcd->ctxt;
3514
3515         tidinv = dd->tidinvalid;
3516         tidbase = (u64 __iomem *)
3517                 ((char __iomem *) dd->kregbase +
3518                  dd->rcvtidbase +
3519                  ctxt * dd->rcvtidcnt * sizeof(*tidbase));
3520
3521         for (i = 0; i < dd->rcvtidcnt; i++)
3522                 qib_7322_put_tid(dd, &tidbase[i], RCVHQ_RCV_TYPE_EXPECTED,
3523                                  tidinv);
3524
3525         tidbase = (u64 __iomem *)
3526                 ((char __iomem *) dd->kregbase +
3527                  dd->rcvegrbase +
3528                  rcd->rcvegr_tid_base * sizeof(*tidbase));
3529
3530         for (i = 0; i < rcd->rcvegrcnt; i++)
3531                 qib_7322_put_tid(dd, &tidbase[i], RCVHQ_RCV_TYPE_EAGER,
3532                                  tidinv);
3533 }
3534
3535 /**
3536  * qib_7322_tidtemplate - setup constants for TID updates
3537  * @dd: the qlogic_ib device
3538  *
3539  * We setup stuff that we use a lot, to avoid calculating each time
3540  */
3541 static void qib_7322_tidtemplate(struct qib_devdata *dd)
3542 {
3543         /*
3544          * For now, we always allocate 4KB buffers (at init) so we can
3545          * receive max size packets.  We may want a module parameter to
3546          * specify 2KB or 4KB and/or make it per port instead of per device
3547          * for those who want to reduce memory footprint.  Note that the
3548          * rcvhdrentsize size must be large enough to hold the largest
3549          * IB header (currently 96 bytes) that we expect to handle (plus of
3550          * course the 2 dwords of RHF).
3551          */
3552         if (dd->rcvegrbufsize == 2048)
3553                 dd->tidtemplate = IBA7322_TID_SZ_2K;
3554         else if (dd->rcvegrbufsize == 4096)
3555                 dd->tidtemplate = IBA7322_TID_SZ_4K;
3556         dd->tidinvalid = 0;
3557 }
3558
3559 /**
3560  * qib_init_7322_get_base_info - set chip-specific flags for user code
3561  * @rcd: the qlogic_ib ctxt
3562  * @kbase: qib_base_info pointer
3563  *
3564  * We set the PCIE flag because the lower bandwidth on PCIe vs
3565  * HyperTransport can affect some user packet algorithims.
3566  */
3567
3568 static int qib_7322_get_base_info(struct qib_ctxtdata *rcd,
3569                                   struct qib_base_info *kinfo)
3570 {
3571         kinfo->spi_runtime_flags |= QIB_RUNTIME_CTXT_MSB_IN_QP |
3572                 QIB_RUNTIME_PCIE | QIB_RUNTIME_NODMA_RTAIL |
3573                 QIB_RUNTIME_HDRSUPP | QIB_RUNTIME_SDMA;
3574         if (rcd->dd->cspec->r1)
3575                 kinfo->spi_runtime_flags |= QIB_RUNTIME_RCHK;
3576         if (rcd->dd->flags & QIB_USE_SPCL_TRIG)
3577                 kinfo->spi_runtime_flags |= QIB_RUNTIME_SPECIAL_TRIGGER;
3578
3579         return 0;
3580 }
3581
3582 static struct qib_message_header *
3583 qib_7322_get_msgheader(struct qib_devdata *dd, __le32 *rhf_addr)
3584 {
3585         u32 offset = qib_hdrget_offset(rhf_addr);
3586
3587         return (struct qib_message_header *)
3588                 (rhf_addr - dd->rhf_offset + offset);
3589 }
3590
3591 /*
3592  * Configure number of contexts.
3593  */
3594 static void qib_7322_config_ctxts(struct qib_devdata *dd)
3595 {
3596         unsigned long flags;
3597         u32 nchipctxts;
3598
3599         nchipctxts = qib_read_kreg32(dd, kr_contextcnt);
3600         dd->cspec->numctxts = nchipctxts;
3601         if (qib_n_krcv_queues > 1 && dd->num_pports) {
3602                 dd->first_user_ctxt = NUM_IB_PORTS +
3603                         (qib_n_krcv_queues - 1) * dd->num_pports;
3604                 if (dd->first_user_ctxt > nchipctxts)
3605                         dd->first_user_ctxt = nchipctxts;
3606                 dd->n_krcv_queues = dd->first_user_ctxt / dd->num_pports;
3607         } else {
3608                 dd->first_user_ctxt = NUM_IB_PORTS;
3609                 dd->n_krcv_queues = 1;
3610         }
3611
3612         if (!qib_cfgctxts) {
3613                 int nctxts = dd->first_user_ctxt + num_online_cpus();
3614
3615                 if (nctxts <= 6)
3616                         dd->ctxtcnt = 6;
3617                 else if (nctxts <= 10)
3618                         dd->ctxtcnt = 10;
3619                 else if (nctxts <= nchipctxts)
3620                         dd->ctxtcnt = nchipctxts;
3621         } else if (qib_cfgctxts < dd->num_pports)
3622                 dd->ctxtcnt = dd->num_pports;
3623         else if (qib_cfgctxts <= nchipctxts)
3624                 dd->ctxtcnt = qib_cfgctxts;
3625         if (!dd->ctxtcnt) /* none of the above, set to max */
3626                 dd->ctxtcnt = nchipctxts;
3627
3628         /*
3629          * Chip can be configured for 6, 10, or 18 ctxts, and choice
3630          * affects number of eager TIDs per ctxt (1K, 2K, 4K).
3631          * Lock to be paranoid about later motion, etc.
3632          */
3633         spin_lock_irqsave(&dd->cspec->rcvmod_lock, flags);
3634         if (dd->ctxtcnt > 10)
3635                 dd->rcvctrl |= 2ULL << SYM_LSB(RcvCtrl, ContextCfg);
3636         else if (dd->ctxtcnt > 6)
3637                 dd->rcvctrl |= 1ULL << SYM_LSB(RcvCtrl, ContextCfg);
3638         /* else configure for default 6 receive ctxts */
3639
3640         /* The XRC opcode is 5. */
3641         dd->rcvctrl |= 5ULL << SYM_LSB(RcvCtrl, XrcTypeCode);
3642
3643         /*
3644          * RcvCtrl *must* be written here so that the
3645          * chip understands how to change rcvegrcnt below.
3646          */
3647         qib_write_kreg(dd, kr_rcvctrl, dd->rcvctrl);
3648         spin_unlock_irqrestore(&dd->cspec->rcvmod_lock, flags);
3649
3650         /* kr_rcvegrcnt changes based on the number of contexts enabled */
3651         dd->cspec->rcvegrcnt = qib_read_kreg32(dd, kr_rcvegrcnt);
3652         if (qib_rcvhdrcnt)
3653                 dd->rcvhdrcnt = max(dd->cspec->rcvegrcnt, qib_rcvhdrcnt);
3654         else
3655                 dd->rcvhdrcnt = 2 * max(dd->cspec->rcvegrcnt,
3656                                     dd->num_pports > 1 ? 1024U : 2048U);
3657 }
3658
3659 static int qib_7322_get_ib_cfg(struct qib_pportdata *ppd, int which)
3660 {
3661
3662         int lsb, ret = 0;
3663         u64 maskr; /* right-justified mask */
3664
3665         switch (which) {
3666
3667         case QIB_IB_CFG_LWID_ENB: /* Get allowed Link-width */
3668                 ret = ppd->link_width_enabled;
3669                 goto done;
3670
3671         case QIB_IB_CFG_LWID: /* Get currently active Link-width */
3672                 ret = ppd->link_width_active;
3673                 goto done;
3674
3675         case QIB_IB_CFG_SPD_ENB: /* Get allowed Link speeds */
3676                 ret = ppd->link_speed_enabled;
3677                 goto done;
3678
3679         case QIB_IB_CFG_SPD: /* Get current Link spd */
3680                 ret = ppd->link_speed_active;
3681                 goto done;
3682
3683         case QIB_IB_CFG_RXPOL_ENB: /* Get Auto-RX-polarity enable */
3684                 lsb = SYM_LSB(IBCCtrlB_0, IB_POLARITY_REV_SUPP);
3685                 maskr = SYM_RMASK(IBCCtrlB_0, IB_POLARITY_REV_SUPP);
3686                 break;
3687
3688         case QIB_IB_CFG_LREV_ENB: /* Get Auto-Lane-reversal enable */
3689                 lsb = SYM_LSB(IBCCtrlB_0, IB_LANE_REV_SUPPORTED);
3690                 maskr = SYM_RMASK(IBCCtrlB_0, IB_LANE_REV_SUPPORTED);
3691                 break;
3692
3693         case QIB_IB_CFG_LINKLATENCY:
3694                 ret = qib_read_kreg_port(ppd, krp_ibcstatus_b) &
3695                         SYM_MASK(IBCStatusB_0, LinkRoundTripLatency);
3696                 goto done;
3697
3698         case QIB_IB_CFG_OP_VLS:
3699                 ret = ppd->vls_operational;
3700                 goto done;
3701
3702         case QIB_IB_CFG_VL_HIGH_CAP:
3703                 ret = 16;
3704                 goto done;
3705
3706         case QIB_IB_CFG_VL_LOW_CAP:
3707                 ret = 16;
3708                 goto done;
3709
3710         case QIB_IB_CFG_OVERRUN_THRESH: /* IB overrun threshold */
3711                 ret = SYM_FIELD(ppd->cpspec->ibcctrl_a, IBCCtrlA_0,
3712                                 OverrunThreshold);
3713                 goto done;
3714
3715         case QIB_IB_CFG_PHYERR_THRESH: /* IB PHY error threshold */
3716                 ret = SYM_FIELD(ppd->cpspec->ibcctrl_a, IBCCtrlA_0,
3717                                 PhyerrThreshold);
3718                 goto done;
3719
3720         case QIB_IB_CFG_LINKDEFAULT: /* IB link default (sleep/poll) */
3721                 /* will only take effect when the link state changes */
3722                 ret = (ppd->cpspec->ibcctrl_a &
3723                        SYM_MASK(IBCCtrlA_0, LinkDownDefaultState)) ?
3724                         IB_LINKINITCMD_SLEEP : IB_LINKINITCMD_POLL;
3725                 goto done;
3726
3727         case QIB_IB_CFG_HRTBT: /* Get Heartbeat off/enable/auto */
3728                 lsb = IBA7322_IBC_HRTBT_LSB;
3729                 maskr = IBA7322_IBC_HRTBT_RMASK; /* OR of AUTO and ENB */
3730                 break;
3731
3732         case QIB_IB_CFG_PMA_TICKS:
3733                 /*
3734                  * 0x00 = 10x link transfer rate or 4 nsec. for 2.5Gbs
3735                  * Since the clock is always 250MHz, the value is 3, 1 or 0.
3736                  */
3737                 if (ppd->link_speed_active == QIB_IB_QDR)
3738                         ret = 3;
3739                 else if (ppd->link_speed_active == QIB_IB_DDR)
3740                         ret = 1;
3741                 else
3742                         ret = 0;
3743                 goto done;
3744
3745         default:
3746                 ret = -EINVAL;
3747                 goto done;
3748         }
3749         ret = (int)((ppd->cpspec->ibcctrl_b >> lsb) & maskr);
3750 done:
3751         return ret;
3752 }
3753
3754 /*
3755  * Below again cribbed liberally from older version. Do not lean
3756  * heavily on it.
3757  */
3758 #define IBA7322_IBC_DLIDLMC_SHIFT QIB_7322_IBCCtrlB_0_IB_DLID_LSB
3759 #define IBA7322_IBC_DLIDLMC_MASK (QIB_7322_IBCCtrlB_0_IB_DLID_RMASK \
3760         | (QIB_7322_IBCCtrlB_0_IB_DLID_MASK_RMASK << 16))
3761
3762 static int qib_7322_set_ib_cfg(struct qib_pportdata *ppd, int which, u32 val)
3763 {
3764         struct qib_devdata *dd = ppd->dd;
3765         u64 maskr; /* right-justified mask */
3766         int lsb, ret = 0;
3767         u16 lcmd, licmd;
3768         unsigned long flags;
3769
3770         switch (which) {
3771         case QIB_IB_CFG_LIDLMC:
3772                 /*
3773                  * Set LID and LMC. Combined to avoid possible hazard
3774                  * caller puts LMC in 16MSbits, DLID in 16LSbits of val
3775                  */
3776                 lsb = IBA7322_IBC_DLIDLMC_SHIFT;
3777                 maskr = IBA7322_IBC_DLIDLMC_MASK;
3778                 /*
3779                  * For header-checking, the SLID in the packet will
3780                  * be masked with SendIBSLMCMask, and compared
3781                  * with SendIBSLIDAssignMask. Make sure we do not
3782                  * set any bits not covered by the mask, or we get
3783                  * false-positives.
3784                  */
3785                 qib_write_kreg_port(ppd, krp_sendslid,
3786                                     val & (val >> 16) & SendIBSLIDAssignMask);
3787                 qib_write_kreg_port(ppd, krp_sendslidmask,
3788                                     (val >> 16) & SendIBSLMCMask);
3789                 break;
3790
3791         case QIB_IB_CFG_LWID_ENB: /* set allowed Link-width */
3792                 ppd->link_width_enabled = val;
3793                 /* convert IB value to chip register value */
3794                 if (val == IB_WIDTH_1X)
3795                         val = 0;
3796                 else if (val == IB_WIDTH_4X)
3797                         val = 1;
3798                 else
3799                         val = 3;
3800                 maskr = SYM_RMASK(IBCCtrlB_0, IB_NUM_CHANNELS);
3801                 lsb = SYM_LSB(IBCCtrlB_0, IB_NUM_CHANNELS);
3802                 break;
3803
3804         case QIB_IB_CFG_SPD_ENB: /* set allowed Link speeds */
3805                 /*
3806                  * As with width, only write the actual register if the
3807                  * link is currently down, otherwise takes effect on next
3808                  * link change.  Since setting is being explicitly requested
3809                  * (via MAD or sysfs), clear autoneg failure status if speed
3810                  * autoneg is enabled.
3811                  */
3812                 ppd->link_speed_enabled = val;
3813                 val <<= IBA7322_IBC_SPEED_LSB;
3814                 maskr = IBA7322_IBC_SPEED_MASK | IBA7322_IBC_IBTA_1_2_MASK |
3815                         IBA7322_IBC_MAX_SPEED_MASK;
3816                 if (val & (val - 1)) {
3817                         /* Muliple speeds enabled */
3818                         val |= IBA7322_IBC_IBTA_1_2_MASK |
3819                                 IBA7322_IBC_MAX_SPEED_MASK;
3820                         spin_lock_irqsave(&ppd->lflags_lock, flags);
3821                         ppd->lflags &= ~QIBL_IB_AUTONEG_FAILED;
3822                         spin_unlock_irqrestore(&ppd->lflags_lock, flags);
3823                 } else if (val & IBA7322_IBC_SPEED_QDR)
3824                         val |= IBA7322_IBC_IBTA_1_2_MASK;
3825                 /* IBTA 1.2 mode + min/max + speed bits are contiguous */
3826                 lsb = SYM_LSB(IBCCtrlB_0, IB_ENHANCED_MODE);
3827                 break;
3828
3829         case QIB_IB_CFG_RXPOL_ENB: /* set Auto-RX-polarity enable */
3830                 lsb = SYM_LSB(IBCCtrlB_0, IB_POLARITY_REV_SUPP);
3831                 maskr = SYM_RMASK(IBCCtrlB_0, IB_POLARITY_REV_SUPP);
3832                 break;
3833
3834         case QIB_IB_CFG_LREV_ENB: /* set Auto-Lane-reversal enable */
3835                 lsb = SYM_LSB(IBCCtrlB_0, IB_LANE_REV_SUPPORTED);
3836                 maskr = SYM_RMASK(IBCCtrlB_0, IB_LANE_REV_SUPPORTED);
3837                 break;
3838
3839         case QIB_IB_CFG_OVERRUN_THRESH: /* IB overrun threshold */
3840                 maskr = SYM_FIELD(ppd->cpspec->ibcctrl_a, IBCCtrlA_0,
3841                                   OverrunThreshold);
3842                 if (maskr != val) {
3843                         ppd->cpspec->ibcctrl_a &=
3844                                 ~SYM_MASK(IBCCtrlA_0, OverrunThreshold);
3845                         ppd->cpspec->ibcctrl_a |= (u64) val <<
3846                                 SYM_LSB(IBCCtrlA_0, OverrunThreshold);
3847                         qib_write_kreg_port(ppd, krp_ibcctrl_a,
3848                                             ppd->cpspec->ibcctrl_a);
3849                         qib_write_kreg(dd, kr_scratch, 0ULL);
3850                 }
3851                 goto bail;
3852
3853         case QIB_IB_CFG_PHYERR_THRESH: /* IB PHY error threshold */
3854                 maskr = SYM_FIELD(ppd->cpspec->ibcctrl_a, IBCCtrlA_0,
3855                                   PhyerrThreshold);
3856                 if (maskr != val) {
3857                         ppd->cpspec->ibcctrl_a &=
3858                                 ~SYM_MASK(IBCCtrlA_0, PhyerrThreshold);
3859                         ppd->cpspec->ibcctrl_a |= (u64) val <<
3860                                 SYM_LSB(IBCCtrlA_0, PhyerrThreshold);
3861                         qib_write_kreg_port(ppd, krp_ibcctrl_a,
3862                                             ppd->cpspec->ibcctrl_a);
3863                         qib_write_kreg(dd, kr_scratch, 0ULL);
3864                 }
3865                 goto bail;
3866
3867         case QIB_IB_CFG_PKEYS: /* update pkeys */
3868                 maskr = (u64) ppd->pkeys[0] | ((u64) ppd->pkeys[1] << 16) |
3869                         ((u64) ppd->pkeys[2] << 32) |
3870                         ((u64) ppd->pkeys[3] << 48);
3871                 qib_write_kreg_port(ppd, krp_partitionkey, maskr);
3872                 goto bail;
3873
3874         case QIB_IB_CFG_LINKDEFAULT: /* IB link default (sleep/poll) */
3875                 /* will only take effect when the link state changes */
3876                 if (val == IB_LINKINITCMD_POLL)
3877                         ppd->cpspec->ibcctrl_a &=
3878                                 ~SYM_MASK(IBCCtrlA_0, LinkDownDefaultState);
3879                 else /* SLEEP */
3880                         ppd->cpspec->ibcctrl_a |=
3881                                 SYM_MASK(IBCCtrlA_0, LinkDownDefaultState);
3882                 qib_write_kreg_port(ppd, krp_ibcctrl_a, ppd->cpspec->ibcctrl_a);
3883                 qib_write_kreg(dd, kr_scratch, 0ULL);
3884                 goto bail;
3885
3886         case QIB_IB_CFG_MTU: /* update the MTU in IBC */
3887                 /*
3888                  * Update our housekeeping variables, and set IBC max
3889                  * size, same as init code; max IBC is max we allow in
3890                  * buffer, less the qword pbc, plus 1 for ICRC, in dwords
3891                  * Set even if it's unchanged, print debug message only
3892                  * on changes.
3893                  */
3894                 val = (ppd->ibmaxlen >> 2) + 1;
3895                 ppd->cpspec->ibcctrl_a &= ~SYM_MASK(IBCCtrlA_0, MaxPktLen);
3896                 ppd->cpspec->ibcctrl_a |= (u64)val <<
3897                         SYM_LSB(IBCCtrlA_0, MaxPktLen);
3898                 qib_write_kreg_port(ppd, krp_ibcctrl_a,
3899                                     ppd->cpspec->ibcctrl_a);
3900                 qib_write_kreg(dd, kr_scratch, 0ULL);
3901                 goto bail;
3902
3903         case QIB_IB_CFG_LSTATE: /* set the IB link state */
3904                 switch (val & 0xffff0000) {
3905                 case IB_LINKCMD_DOWN:
3906                         lcmd = QLOGIC_IB_IBCC_LINKCMD_DOWN;
3907                         ppd->cpspec->ibmalfusesnap = 1;
3908                         ppd->cpspec->ibmalfsnap = read_7322_creg32_port(ppd,
3909                                 crp_errlink);
3910                         if (!ppd->cpspec->ibdeltainprog &&
3911                             qib_compat_ddr_negotiate) {
3912                                 ppd->cpspec->ibdeltainprog = 1;
3913                                 ppd->cpspec->ibsymsnap =
3914                                         read_7322_creg32_port(ppd,
3915                                                               crp_ibsymbolerr);
3916                                 ppd->cpspec->iblnkerrsnap =
3917                                         read_7322_creg32_port(ppd,
3918                                                       crp_iblinkerrrecov);
3919                         }
3920                         break;
3921
3922                 case IB_LINKCMD_ARMED:
3923                         lcmd = QLOGIC_IB_IBCC_LINKCMD_ARMED;
3924                         if (ppd->cpspec->ibmalfusesnap) {
3925                                 ppd->cpspec->ibmalfusesnap = 0;
3926                                 ppd->cpspec->ibmalfdelta +=
3927                                         read_7322_creg32_port(ppd,
3928                                                               crp_errlink) -
3929                                         ppd->cpspec->ibmalfsnap;
3930                         }
3931                         break;
3932
3933                 case IB_LINKCMD_ACTIVE:
3934                         lcmd = QLOGIC_IB_IBCC_LINKCMD_ACTIVE;
3935                         break;
3936
3937                 default:
3938                         ret = -EINVAL;
3939                         qib_dev_err(dd, "bad linkcmd req 0x%x\n", val >> 16);
3940                         goto bail;
3941                 }
3942                 switch (val & 0xffff) {
3943                 case IB_LINKINITCMD_NOP:
3944                         licmd = 0;
3945                         break;
3946
3947                 case IB_LINKINITCMD_POLL:
3948                         licmd = QLOGIC_IB_IBCC_LINKINITCMD_POLL;
3949                         break;
3950
3951                 case IB_LINKINITCMD_SLEEP:
3952                         licmd = QLOGIC_IB_IBCC_LINKINITCMD_SLEEP;
3953                         break;
3954
3955                 case IB_LINKINITCMD_DISABLE:
3956                         licmd = QLOGIC_IB_IBCC_LINKINITCMD_DISABLE;
3957                         ppd->cpspec->chase_end = 0;
3958                         /*
3959                          * stop state chase counter and timer, if running.
3960                          * wait forpending timer, but don't clear .data (ppd)!
3961                          */
3962                         if (ppd->cpspec->chase_timer.expires) {
3963                                 del_timer_sync(&ppd->cpspec->chase_timer);
3964                                 ppd->cpspec->chase_timer.expires = 0;
3965                         }
3966                         break;
3967
3968                 default:
3969                         ret = -EINVAL;
3970                         qib_dev_err(dd, "bad linkinitcmd req 0x%x\n",
3971                                     val & 0xffff);
3972                         goto bail;
3973                 }
3974                 qib_set_ib_7322_lstate(ppd, lcmd, licmd);
3975                 goto bail;
3976
3977         case QIB_IB_CFG_OP_VLS:
3978                 if (ppd->vls_operational != val) {
3979                         ppd->vls_operational = val;
3980                         set_vls(ppd);
3981                 }
3982                 goto bail;
3983
3984         case QIB_IB_CFG_VL_HIGH_LIMIT:
3985                 qib_write_kreg_port(ppd, krp_highprio_limit, val);
3986                 goto bail;
3987
3988         case QIB_IB_CFG_HRTBT: /* set Heartbeat off/enable/auto */
3989                 if (val > 3) {
3990                         ret = -EINVAL;
3991                         goto bail;
3992                 }
3993                 lsb = IBA7322_IBC_HRTBT_LSB;
3994                 maskr = IBA7322_IBC_HRTBT_RMASK; /* OR of AUTO and ENB */
3995                 break;
3996
3997         case QIB_IB_CFG_PORT:
3998                 /* val is the port number of the switch we are connected to. */
3999                 if (ppd->dd->cspec->r1) {
4000                         cancel_delayed_work(&ppd->cpspec->ipg_work);
4001                         ppd->cpspec->ipg_tries = 0;
4002                 }
4003                 goto bail;
4004
4005         default:
4006                 ret = -EINVAL;
4007                 goto bail;
4008         }
4009         ppd->cpspec->ibcctrl_b &= ~(maskr << lsb);
4010         ppd->cpspec->ibcctrl_b |= (((u64) val & maskr) << lsb);
4011         qib_write_kreg_port(ppd, krp_ibcctrl_b, ppd->cpspec->ibcctrl_b);
4012         qib_write_kreg(dd, kr_scratch, 0);
4013 bail:
4014         return ret;
4015 }
4016
4017 static int qib_7322_set_loopback(struct qib_pportdata *ppd, const char *what)
4018 {
4019         int ret = 0;
4020         u64 val, ctrlb;
4021
4022         /* only IBC loopback, may add serdes and xgxs loopbacks later */
4023         if (!strncmp(what, "ibc", 3)) {
4024                 ppd->cpspec->ibcctrl_a |= SYM_MASK(IBCCtrlA_0,
4025                                                        Loopback);
4026                 val = 0; /* disable heart beat, so link will come up */
4027                 qib_devinfo(ppd->dd->pcidev, "Enabling IB%u:%u IBC loopback\n",
4028                          ppd->dd->unit, ppd->port);
4029         } else if (!strncmp(what, "off", 3)) {
4030                 ppd->cpspec->ibcctrl_a &= ~SYM_MASK(IBCCtrlA_0,
4031                                                         Loopback);
4032                 /* enable heart beat again */
4033                 val = IBA7322_IBC_HRTBT_RMASK << IBA7322_IBC_HRTBT_LSB;
4034                 qib_devinfo(ppd->dd->pcidev, "Disabling IB%u:%u IBC loopback "
4035                             "(normal)\n", ppd->dd->unit, ppd->port);
4036         } else
4037                 ret = -EINVAL;
4038         if (!ret) {
4039                 qib_write_kreg_port(ppd, krp_ibcctrl_a,
4040                                     ppd->cpspec->ibcctrl_a);
4041                 ctrlb = ppd->cpspec->ibcctrl_b & ~(IBA7322_IBC_HRTBT_MASK
4042                                              << IBA7322_IBC_HRTBT_LSB);
4043                 ppd->cpspec->ibcctrl_b = ctrlb | val;
4044                 qib_write_kreg_port(ppd, krp_ibcctrl_b,
4045                                     ppd->cpspec->ibcctrl_b);
4046                 qib_write_kreg(ppd->dd, kr_scratch, 0);
4047         }
4048         return ret;
4049 }
4050
4051 static void get_vl_weights(struct qib_pportdata *ppd, unsigned regno,
4052                            struct ib_vl_weight_elem *vl)
4053 {
4054         unsigned i;
4055
4056         for (i = 0; i < 16; i++, regno++, vl++) {
4057                 u32 val = qib_read_kreg_port(ppd, regno);
4058
4059                 vl->vl = (val >> SYM_LSB(LowPriority0_0, VirtualLane)) &
4060                         SYM_RMASK(LowPriority0_0, VirtualLane);
4061                 vl->weight = (val >> SYM_LSB(LowPriority0_0, Weight)) &
4062                         SYM_RMASK(LowPriority0_0, Weight);
4063         }
4064 }
4065
4066 static void set_vl_weights(struct qib_pportdata *ppd, unsigned regno,
4067                            struct ib_vl_weight_elem *vl)
4068 {
4069         unsigned i;
4070
4071         for (i = 0; i < 16; i++, regno++, vl++) {
4072                 u64 val;
4073
4074                 val = ((vl->vl & SYM_RMASK(LowPriority0_0, VirtualLane)) <<
4075                         SYM_LSB(LowPriority0_0, VirtualLane)) |
4076                       ((vl->weight & SYM_RMASK(LowPriority0_0, Weight)) <<
4077                         SYM_LSB(LowPriority0_0, Weight));
4078                 qib_write_kreg_port(ppd, regno, val);
4079         }
4080         if (!(ppd->p_sendctrl & SYM_MASK(SendCtrl_0, IBVLArbiterEn))) {
4081                 struct qib_devdata *dd = ppd->dd;
4082                 unsigned long flags;
4083
4084                 spin_lock_irqsave(&dd->sendctrl_lock, flags);
4085                 ppd->p_sendctrl |= SYM_MASK(SendCtrl_0, IBVLArbiterEn);
4086                 qib_write_kreg_port(ppd, krp_sendctrl, ppd->p_sendctrl);
4087                 qib_write_kreg(dd, kr_scratch, 0);
4088                 spin_unlock_irqrestore(&dd->sendctrl_lock, flags);
4089         }
4090 }
4091
4092 static int qib_7322_get_ib_table(struct qib_pportdata *ppd, int which, void *t)
4093 {
4094         switch (which) {
4095         case QIB_IB_TBL_VL_HIGH_ARB:
4096                 get_vl_weights(ppd, krp_highprio_0, t);
4097                 break;
4098
4099         case QIB_IB_TBL_VL_LOW_ARB:
4100                 get_vl_weights(ppd, krp_lowprio_0, t);
4101                 break;
4102
4103         default:
4104                 return -EINVAL;
4105         }
4106         return 0;
4107 }
4108
4109 static int qib_7322_set_ib_table(struct qib_pportdata *ppd, int which, void *t)
4110 {
4111         switch (which) {
4112         case QIB_IB_TBL_VL_HIGH_ARB:
4113                 set_vl_weights(ppd, krp_highprio_0, t);
4114                 break;
4115
4116         case QIB_IB_TBL_VL_LOW_ARB:
4117                 set_vl_weights(ppd, krp_lowprio_0, t);
4118                 break;
4119
4120         default:
4121                 return -EINVAL;
4122         }
4123         return 0;
4124 }
4125
4126 static void qib_update_7322_usrhead(struct qib_ctxtdata *rcd, u64 hd,
4127                                     u32 updegr, u32 egrhd, u32 npkts)
4128 {
4129         /*
4130          * Need to write timeout register before updating rcvhdrhead to ensure
4131          * that the timer is enabled on reception of a packet.
4132          */
4133         if (hd >> IBA7322_HDRHEAD_PKTINT_SHIFT)
4134                 adjust_rcv_timeout(rcd, npkts);
4135         if (updegr)
4136                 qib_write_ureg(rcd->dd, ur_rcvegrindexhead, egrhd, rcd->ctxt);
4137         mmiowb();
4138         qib_write_ureg(rcd->dd, ur_rcvhdrhead, hd, rcd->ctxt);
4139         qib_write_ureg(rcd->dd, ur_rcvhdrhead, hd, rcd->ctxt);
4140         mmiowb();
4141 }
4142
4143 static u32 qib_7322_hdrqempty(struct qib_ctxtdata *rcd)
4144 {
4145         u32 head, tail;
4146
4147         head = qib_read_ureg32(rcd->dd, ur_rcvhdrhead, rcd->ctxt);
4148         if (rcd->rcvhdrtail_kvaddr)
4149                 tail = qib_get_rcvhdrtail(rcd);
4150         else
4151                 tail = qib_read_ureg32(rcd->dd, ur_rcvhdrtail, rcd->ctxt);
4152         return head == tail;
4153 }
4154
4155 #define RCVCTRL_COMMON_MODS (QIB_RCVCTRL_CTXT_ENB | \
4156         QIB_RCVCTRL_CTXT_DIS | \
4157         QIB_RCVCTRL_TIDFLOW_ENB | \
4158         QIB_RCVCTRL_TIDFLOW_DIS | \
4159         QIB_RCVCTRL_TAILUPD_ENB | \
4160         QIB_RCVCTRL_TAILUPD_DIS | \
4161         QIB_RCVCTRL_INTRAVAIL_ENB | \
4162         QIB_RCVCTRL_INTRAVAIL_DIS | \
4163         QIB_RCVCTRL_BP_ENB | \
4164         QIB_RCVCTRL_BP_DIS)
4165
4166 #define RCVCTRL_PORT_MODS (QIB_RCVCTRL_CTXT_ENB | \
4167         QIB_RCVCTRL_CTXT_DIS | \
4168         QIB_RCVCTRL_PKEY_DIS | \
4169         QIB_RCVCTRL_PKEY_ENB)
4170
4171 /*
4172  * Modify the RCVCTRL register in chip-specific way. This
4173  * is a function because bit positions and (future) register
4174  * location is chip-specifc, but the needed operations are
4175  * generic. <op> is a bit-mask because we often want to
4176  * do multiple modifications.
4177  */
4178 static void rcvctrl_7322_mod(struct qib_pportdata *ppd, unsigned int op,
4179                              int ctxt)
4180 {
4181         struct qib_devdata *dd = ppd->dd;
4182         struct qib_ctxtdata *rcd;
4183         u64 mask, val;
4184         unsigned long flags;
4185
4186         spin_lock_irqsave(&dd->cspec->rcvmod_lock, flags);
4187
4188         if (op & QIB_RCVCTRL_TIDFLOW_ENB)
4189                 dd->rcvctrl |= SYM_MASK(RcvCtrl, TidFlowEnable);
4190         if (op & QIB_RCVCTRL_TIDFLOW_DIS)
4191                 dd->rcvctrl &= ~SYM_MASK(RcvCtrl, TidFlowEnable);
4192         if (op & QIB_RCVCTRL_TAILUPD_ENB)
4193                 dd->rcvctrl |= SYM_MASK(RcvCtrl, TailUpd);
4194         if (op & QIB_RCVCTRL_TAILUPD_DIS)
4195                 dd->rcvctrl &= ~SYM_MASK(RcvCtrl, TailUpd);
4196         if (op & QIB_RCVCTRL_PKEY_ENB)
4197                 ppd->p_rcvctrl &= ~SYM_MASK(RcvCtrl_0, RcvPartitionKeyDisable);
4198         if (op & QIB_RCVCTRL_PKEY_DIS)
4199                 ppd->p_rcvctrl |= SYM_MASK(RcvCtrl_0, RcvPartitionKeyDisable);
4200         if (ctxt < 0) {
4201                 mask = (1ULL << dd->ctxtcnt) - 1;
4202                 rcd = NULL;
4203         } else {
4204                 mask = (1ULL << ctxt);
4205                 rcd = dd->rcd[ctxt];
4206         }
4207         if ((op & QIB_RCVCTRL_CTXT_ENB) && rcd) {
4208                 ppd->p_rcvctrl |=
4209                         (mask << SYM_LSB(RcvCtrl_0, ContextEnableKernel));
4210                 if (!(dd->flags & QIB_NODMA_RTAIL)) {
4211                         op |= QIB_RCVCTRL_TAILUPD_ENB; /* need reg write */
4212                         dd->rcvctrl |= SYM_MASK(RcvCtrl, TailUpd);
4213                 }
4214                 /* Write these registers before the context is enabled. */
4215                 qib_write_kreg_ctxt(dd, krc_rcvhdrtailaddr, ctxt,
4216                                     rcd->rcvhdrqtailaddr_phys);
4217                 qib_write_kreg_ctxt(dd, krc_rcvhdraddr, ctxt,
4218                                     rcd->rcvhdrq_phys);
4219                 rcd->seq_cnt = 1;
4220         }
4221         if (op & QIB_RCVCTRL_CTXT_DIS)
4222                 ppd->p_rcvctrl &=
4223                         ~(mask << SYM_LSB(RcvCtrl_0, ContextEnableKernel));
4224         if (op & QIB_RCVCTRL_BP_ENB)
4225                 dd->rcvctrl |= mask << SYM_LSB(RcvCtrl, dontDropRHQFull);
4226         if (op & QIB_RCVCTRL_BP_DIS)
4227                 dd->rcvctrl &= ~(mask << SYM_LSB(RcvCtrl, dontDropRHQFull));
4228         if (op & QIB_RCVCTRL_INTRAVAIL_ENB)
4229                 dd->rcvctrl |= (mask << SYM_LSB(RcvCtrl, IntrAvail));
4230         if (op & QIB_RCVCTRL_INTRAVAIL_DIS)
4231                 dd->rcvctrl &= ~(mask << SYM_LSB(RcvCtrl, IntrAvail));
4232         /*
4233          * Decide which registers to write depending on the ops enabled.
4234          * Special case is "flush" (no bits set at all)
4235          * which needs to write both.
4236          */
4237         if (op == 0 || (op & RCVCTRL_COMMON_MODS))
4238                 qib_write_kreg(dd, kr_rcvctrl, dd->rcvctrl);
4239         if (op == 0 || (op & RCVCTRL_PORT_MODS))
4240                 qib_write_kreg_port(ppd, krp_rcvctrl, ppd->p_rcvctrl);
4241         if ((op & QIB_RCVCTRL_CTXT_ENB) && dd->rcd[ctxt]) {
4242                 /*
4243                  * Init the context registers also; if we were
4244                  * disabled, tail and head should both be zero
4245                  * already from the enable, but since we don't
4246                  * know, we have to do it explicitly.
4247                  */
4248                 val = qib_read_ureg32(dd, ur_rcvegrindextail, ctxt);
4249                 qib_write_ureg(dd, ur_rcvegrindexhead, val, ctxt);
4250
4251                 /* be sure enabling write seen; hd/tl should be 0 */
4252                 (void) qib_read_kreg32(dd, kr_scratch);
4253                 val = qib_read_ureg32(dd, ur_rcvhdrtail, ctxt);
4254                 dd->rcd[ctxt]->head = val;
4255                 /* If kctxt, interrupt on next receive. */
4256                 if (ctxt < dd->first_user_ctxt)
4257                         val |= dd->rhdrhead_intr_off;
4258                 qib_write_ureg(dd, ur_rcvhdrhead, val, ctxt);
4259         } else if ((op & QIB_RCVCTRL_INTRAVAIL_ENB) &&
4260                 dd->rcd[ctxt] && dd->rhdrhead_intr_off) {
4261                 /* arm rcv interrupt */
4262                 val = dd->rcd[ctxt]->head | dd->rhdrhead_intr_off;
4263                 qib_write_ureg(dd, ur_rcvhdrhead, val, ctxt);
4264         }
4265         if (op & QIB_RCVCTRL_CTXT_DIS) {
4266                 unsigned f;
4267
4268                 /* Now that the context is disabled, clear these registers. */
4269                 if (ctxt >= 0) {
4270                         qib_write_kreg_ctxt(dd, krc_rcvhdrtailaddr, ctxt, 0);
4271                         qib_write_kreg_ctxt(dd, krc_rcvhdraddr, ctxt, 0);
4272                         for (f = 0; f < NUM_TIDFLOWS_CTXT; f++)
4273                                 qib_write_ureg(dd, ur_rcvflowtable + f,
4274                                                TIDFLOW_ERRBITS, ctxt);
4275                 } else {
4276                         unsigned i;
4277
4278                         for (i = 0; i < dd->cfgctxts; i++) {
4279                                 qib_write_kreg_ctxt(dd, krc_rcvhdrtailaddr,
4280                                                     i, 0);
4281                                 qib_write_kreg_ctxt(dd, krc_rcvhdraddr, i, 0);
4282                                 for (f = 0; f < NUM_TIDFLOWS_CTXT; f++)
4283                                         qib_write_ureg(dd, ur_rcvflowtable + f,
4284                                                        TIDFLOW_ERRBITS, i);
4285                         }
4286                 }
4287         }
4288         spin_unlock_irqrestore(&dd->cspec->rcvmod_lock, flags);
4289 }
4290
4291 /*
4292  * Modify the SENDCTRL register in chip-specific way. This
4293  * is a function where there are multiple such registers with
4294  * slightly different layouts.
4295  * The chip doesn't allow back-to-back sendctrl writes, so write
4296  * the scratch register after writing sendctrl.
4297  *
4298  * Which register is written depends on the operation.
4299  * Most operate on the common register, while
4300  * SEND_ENB and SEND_DIS operate on the per-port ones.
4301  * SEND_ENB is included in common because it can change SPCL_TRIG
4302  */
4303 #define SENDCTRL_COMMON_MODS (\
4304         QIB_SENDCTRL_CLEAR | \
4305         QIB_SENDCTRL_AVAIL_DIS | \
4306         QIB_SENDCTRL_AVAIL_ENB | \
4307         QIB_SENDCTRL_AVAIL_BLIP | \
4308         QIB_SENDCTRL_DISARM | \
4309         QIB_SENDCTRL_DISARM_ALL | \
4310         QIB_SENDCTRL_SEND_ENB)
4311
4312 #define SENDCTRL_PORT_MODS (\
4313         QIB_SENDCTRL_CLEAR | \
4314         QIB_SENDCTRL_SEND_ENB | \
4315         QIB_SENDCTRL_SEND_DIS | \
4316         QIB_SENDCTRL_FLUSH)
4317
4318 static void sendctrl_7322_mod(struct qib_pportdata *ppd, u32 op)
4319 {
4320         struct qib_devdata *dd = ppd->dd;
4321         u64 tmp_dd_sendctrl;
4322         unsigned long flags;
4323
4324         spin_lock_irqsave(&dd->sendctrl_lock, flags);
4325
4326         /* First the dd ones that are "sticky", saved in shadow */
4327         if (op & QIB_SENDCTRL_CLEAR)
4328                 dd->sendctrl = 0;
4329         if (op & QIB_SENDCTRL_AVAIL_DIS)
4330                 dd->sendctrl &= ~SYM_MASK(SendCtrl, SendBufAvailUpd);
4331         else if (op & QIB_SENDCTRL_AVAIL_ENB) {
4332                 dd->sendctrl |= SYM_MASK(SendCtrl, SendBufAvailUpd);
4333                 if (dd->flags & QIB_USE_SPCL_TRIG)
4334                         dd->sendctrl |= SYM_MASK(SendCtrl, SpecialTriggerEn);
4335         }
4336
4337         /* Then the ppd ones that are "sticky", saved in shadow */
4338         if (op & QIB_SENDCTRL_SEND_DIS)
4339                 ppd->p_sendctrl &= ~SYM_MASK(SendCtrl_0, SendEnable);
4340         else if (op & QIB_SENDCTRL_SEND_ENB)
4341                 ppd->p_sendctrl |= SYM_MASK(SendCtrl_0, SendEnable);
4342
4343         if (op & QIB_SENDCTRL_DISARM_ALL) {
4344                 u32 i, last;
4345
4346                 tmp_dd_sendctrl = dd->sendctrl;
4347                 last = dd->piobcnt2k + dd->piobcnt4k + NUM_VL15_BUFS;
4348                 /*
4349                  * Disarm any buffers that are not yet launched,
4350                  * disabling updates until done.
4351                  */
4352                 tmp_dd_sendctrl &= ~SYM_MASK(SendCtrl, SendBufAvailUpd);
4353                 for (i = 0; i < last; i++) {
4354                         qib_write_kreg(dd, kr_sendctrl,
4355                                        tmp_dd_sendctrl |
4356                                        SYM_MASK(SendCtrl, Disarm) | i);
4357                         qib_write_kreg(dd, kr_scratch, 0);
4358                 }
4359         }
4360
4361         if (op & QIB_SENDCTRL_FLUSH) {
4362                 u64 tmp_ppd_sendctrl = ppd->p_sendctrl;
4363
4364                 /*
4365                  * Now drain all the fifos.  The Abort bit should never be
4366                  * needed, so for now, at least, we don't use it.
4367                  */
4368                 tmp_ppd_sendctrl |=
4369                         SYM_MASK(SendCtrl_0, TxeDrainRmFifo) |
4370                         SYM_MASK(SendCtrl_0, TxeDrainLaFifo) |
4371                         SYM_MASK(SendCtrl_0, TxeBypassIbc);
4372                 qib_write_kreg_port(ppd, krp_sendctrl, tmp_ppd_sendctrl);
4373                 qib_write_kreg(dd, kr_scratch, 0);
4374         }
4375
4376         tmp_dd_sendctrl = dd->sendctrl;
4377
4378         if (op & QIB_SENDCTRL_DISARM)
4379                 tmp_dd_sendctrl |= SYM_MASK(SendCtrl, Disarm) |
4380                         ((op & QIB_7322_SendCtrl_DisarmSendBuf_RMASK) <<
4381                          SYM_LSB(SendCtrl, DisarmSendBuf));
4382         if ((op & QIB_SENDCTRL_AVAIL_BLIP) &&
4383             (dd->sendctrl & SYM_MASK(SendCtrl, SendBufAvailUpd)))
4384                 tmp_dd_sendctrl &= ~SYM_MASK(SendCtrl, SendBufAvailUpd);
4385
4386         if (op == 0 || (op & SENDCTRL_COMMON_MODS)) {
4387                 qib_write_kreg(dd, kr_sendctrl, tmp_dd_sendctrl);
4388                 qib_write_kreg(dd, kr_scratch, 0);
4389         }
4390
4391         if (op == 0 || (op & SENDCTRL_PORT_MODS)) {
4392                 qib_write_kreg_port(ppd, krp_sendctrl, ppd->p_sendctrl);
4393                 qib_write_kreg(dd, kr_scratch, 0);
4394         }
4395
4396         if (op & QIB_SENDCTRL_AVAIL_BLIP) {
4397                 qib_write_kreg(dd, kr_sendctrl, dd->sendctrl);
4398                 qib_write_kreg(dd, kr_scratch, 0);
4399         }
4400
4401         spin_unlock_irqrestore(&dd->sendctrl_lock, flags);
4402
4403         if (op & QIB_SENDCTRL_FLUSH) {
4404                 u32 v;
4405                 /*
4406                  * ensure writes have hit chip, then do a few
4407                  * more reads, to allow DMA of pioavail registers
4408                  * to occur, so in-memory copy is in sync with
4409                  * the chip.  Not always safe to sleep.
4410                  */
4411                 v = qib_read_kreg32(dd, kr_scratch);
4412                 qib_write_kreg(dd, kr_scratch, v);
4413                 v = qib_read_kreg32(dd, kr_scratch);
4414                 qib_write_kreg(dd, kr_scratch, v);
4415                 qib_read_kreg32(dd, kr_scratch);
4416         }
4417 }
4418
4419 #define _PORT_VIRT_FLAG 0x8000U /* "virtual", need adjustments */
4420 #define _PORT_64BIT_FLAG 0x10000U /* not "virtual", but 64bit */
4421 #define _PORT_CNTR_IDXMASK 0x7fffU /* mask off flags above */
4422
4423 /**
4424  * qib_portcntr_7322 - read a per-port chip counter
4425  * @ppd: the qlogic_ib pport
4426  * @creg: the counter to read (not a chip offset)
4427  */
4428 static u64 qib_portcntr_7322(struct qib_pportdata *ppd, u32 reg)
4429 {
4430         struct qib_devdata *dd = ppd->dd;
4431         u64 ret = 0ULL;
4432         u16 creg;
4433         /* 0xffff for unimplemented or synthesized counters */
4434         static const u32 xlator[] = {
4435                 [QIBPORTCNTR_PKTSEND] = crp_pktsend | _PORT_64BIT_FLAG,
4436                 [QIBPORTCNTR_WORDSEND] = crp_wordsend | _PORT_64BIT_FLAG,
4437                 [QIBPORTCNTR_PSXMITDATA] = crp_psxmitdatacount,
4438                 [QIBPORTCNTR_PSXMITPKTS] = crp_psxmitpktscount,
4439                 [QIBPORTCNTR_PSXMITWAIT] = crp_psxmitwaitcount,
4440                 [QIBPORTCNTR_SENDSTALL] = crp_sendstall,
4441                 [QIBPORTCNTR_PKTRCV] = crp_pktrcv | _PORT_64BIT_FLAG,
4442                 [QIBPORTCNTR_PSRCVDATA] = crp_psrcvdatacount,
4443                 [QIBPORTCNTR_PSRCVPKTS] = crp_psrcvpktscount,
4444                 [QIBPORTCNTR_RCVEBP] = crp_rcvebp,
4445                 [QIBPORTCNTR_RCVOVFL] = crp_rcvovfl,
4446                 [QIBPORTCNTR_WORDRCV] = crp_wordrcv | _PORT_64BIT_FLAG,
4447                 [QIBPORTCNTR_RXDROPPKT] = 0xffff, /* not needed  for 7322 */
4448                 [QIBPORTCNTR_RXLOCALPHYERR] = crp_rxotherlocalphyerr,
4449                 [QIBPORTCNTR_RXVLERR] = crp_rxvlerr,
4450                 [QIBPORTCNTR_ERRICRC] = crp_erricrc,
4451                 [QIBPORTCNTR_ERRVCRC] = crp_errvcrc,
4452                 [QIBPORTCNTR_ERRLPCRC] = crp_errlpcrc,
4453                 [QIBPORTCNTR_BADFORMAT] = crp_badformat,
4454                 [QIBPORTCNTR_ERR_RLEN] = crp_err_rlen,
4455                 [QIBPORTCNTR_IBSYMBOLERR] = crp_ibsymbolerr,
4456                 [QIBPORTCNTR_INVALIDRLEN] = crp_invalidrlen,
4457                 [QIBPORTCNTR_UNSUPVL] = crp_txunsupvl,
4458                 [QIBPORTCNTR_EXCESSBUFOVFL] = crp_excessbufferovfl,
4459                 [QIBPORTCNTR_ERRLINK] = crp_errlink,
4460                 [QIBPORTCNTR_IBLINKDOWN] = crp_iblinkdown,
4461                 [QIBPORTCNTR_IBLINKERRRECOV] = crp_iblinkerrrecov,
4462                 [QIBPORTCNTR_LLI] = crp_locallinkintegrityerr,
4463                 [QIBPORTCNTR_VL15PKTDROP] = crp_vl15droppedpkt,
4464                 [QIBPORTCNTR_ERRPKEY] = crp_errpkey,
4465                 /*
4466                  * the next 3 aren't really counters, but were implemented
4467                  * as counters in older chips, so still get accessed as
4468                  * though they were counters from this code.
4469                  */
4470                 [QIBPORTCNTR_PSINTERVAL] = krp_psinterval,
4471                 [QIBPORTCNTR_PSSTART] = krp_psstart,
4472                 [QIBPORTCNTR_PSSTAT] = krp_psstat,
4473                 /* pseudo-counter, summed for all ports */
4474                 [QIBPORTCNTR_KHDROVFL] = 0xffff,
4475         };
4476
4477         if (reg >= ARRAY_SIZE(xlator)) {
4478                 qib_devinfo(ppd->dd->pcidev,
4479                          "Unimplemented portcounter %u\n", reg);
4480                 goto done;
4481         }
4482         creg = xlator[reg] & _PORT_CNTR_IDXMASK;
4483
4484         /* handle non-counters and special cases first */
4485         if (reg == QIBPORTCNTR_KHDROVFL) {
4486                 int i;
4487
4488                 /* sum over all kernel contexts (skip if mini_init) */
4489                 for (i = 0; dd->rcd && i < dd->first_user_ctxt; i++) {
4490                         struct qib_ctxtdata *rcd = dd->rcd[i];
4491
4492                         if (!rcd || rcd->ppd != ppd)
4493                                 continue;
4494                         ret += read_7322_creg32(dd, cr_base_egrovfl + i);
4495                 }
4496                 goto done;
4497         } else if (reg == QIBPORTCNTR_RXDROPPKT) {
4498                 /*
4499                  * Used as part of the synthesis of port_rcv_errors
4500                  * in the verbs code for IBTA counters.  Not needed for 7322,
4501                  * because all the errors are already counted by other cntrs.
4502                  */
4503                 goto done;
4504         } else if (reg == QIBPORTCNTR_PSINTERVAL ||
4505                    reg == QIBPORTCNTR_PSSTART || reg == QIBPORTCNTR_PSSTAT) {
4506                 /* were counters in older chips, now per-port kernel regs */
4507                 ret = qib_read_kreg_port(ppd, creg);
4508                 goto done;
4509         }
4510
4511         /*
4512          * Only fast increment counters are 64 bits; use 32 bit reads to
4513          * avoid two independent reads when on Opteron.
4514          */
4515         if (xlator[reg] & _PORT_64BIT_FLAG)
4516                 ret = read_7322_creg_port(ppd, creg);
4517         else
4518                 ret = read_7322_creg32_port(ppd, creg);
4519         if (creg == crp_ibsymbolerr) {
4520                 if (ppd->cpspec->ibdeltainprog)
4521                         ret -= ret - ppd->cpspec->ibsymsnap;
4522                 ret -= ppd->cpspec->ibsymdelta;
4523         } else if (creg == crp_iblinkerrrecov) {
4524                 if (ppd->cpspec->ibdeltainprog)
4525                         ret -= ret - ppd->cpspec->iblnkerrsnap;
4526                 ret -= ppd->cpspec->iblnkerrdelta;
4527         } else if (creg == crp_errlink)
4528                 ret -= ppd->cpspec->ibmalfdelta;
4529         else if (creg == crp_iblinkdown)
4530                 ret += ppd->cpspec->iblnkdowndelta;
4531 done:
4532         return ret;
4533 }
4534
4535 /*
4536  * Device counter names (not port-specific), one line per stat,
4537  * single string.  Used by utilities like ipathstats to print the stats
4538  * in a way which works for different versions of drivers, without changing
4539  * the utility.  Names need to be 12 chars or less (w/o newline), for proper
4540  * display by utility.
4541  * Non-error counters are first.
4542  * Start of "error" conters is indicated by a leading "E " on the first
4543  * "error" counter, and doesn't count in label length.
4544  * The EgrOvfl list needs to be last so we truncate them at the configured
4545  * context count for the device.
4546  * cntr7322indices contains the corresponding register indices.
4547  */
4548 static const char cntr7322names[] =
4549         "Interrupts\n"
4550         "HostBusStall\n"
4551         "E RxTIDFull\n"
4552         "RxTIDInvalid\n"
4553         "RxTIDFloDrop\n" /* 7322 only */
4554         "Ctxt0EgrOvfl\n"
4555         "Ctxt1EgrOvfl\n"
4556         "Ctxt2EgrOvfl\n"
4557         "Ctxt3EgrOvfl\n"
4558         "Ctxt4EgrOvfl\n"
4559         "Ctxt5EgrOvfl\n"
4560         "Ctxt6EgrOvfl\n"
4561         "Ctxt7EgrOvfl\n"
4562         "Ctxt8EgrOvfl\n"
4563         "Ctxt9EgrOvfl\n"
4564         "Ctx10EgrOvfl\n"
4565         "Ctx11EgrOvfl\n"
4566         "Ctx12EgrOvfl\n"
4567         "Ctx13EgrOvfl\n"
4568         "Ctx14EgrOvfl\n"
4569         "Ctx15EgrOvfl\n"
4570         "Ctx16EgrOvfl\n"
4571         "Ctx17EgrOvfl\n"
4572         ;
4573
4574 static const u32 cntr7322indices[] = {
4575         cr_lbint | _PORT_64BIT_FLAG,
4576         cr_lbstall | _PORT_64BIT_FLAG,
4577         cr_tidfull,
4578         cr_tidinvalid,
4579         cr_rxtidflowdrop,
4580         cr_base_egrovfl + 0,
4581         cr_base_egrovfl + 1,
4582         cr_base_egrovfl + 2,
4583         cr_base_egrovfl + 3,
4584         cr_base_egrovfl + 4,
4585         cr_base_egrovfl + 5,
4586         cr_base_egrovfl + 6,
4587         cr_base_egrovfl + 7,
4588         cr_base_egrovfl + 8,
4589         cr_base_egrovfl + 9,
4590         cr_base_egrovfl + 10,
4591         cr_base_egrovfl + 11,
4592         cr_base_egrovfl + 12,
4593         cr_base_egrovfl + 13,
4594         cr_base_egrovfl + 14,
4595         cr_base_egrovfl + 15,
4596         cr_base_egrovfl + 16,
4597         cr_base_egrovfl + 17,
4598 };
4599
4600 /*
4601  * same as cntr7322names and cntr7322indices, but for port-specific counters.
4602  * portcntr7322indices is somewhat complicated by some registers needing
4603  * adjustments of various kinds, and those are ORed with _PORT_VIRT_FLAG
4604  */
4605 static const char portcntr7322names[] =
4606         "TxPkt\n"
4607         "TxFlowPkt\n"
4608         "TxWords\n"
4609         "RxPkt\n"
4610         "RxFlowPkt\n"
4611         "RxWords\n"
4612         "TxFlowStall\n"
4613         "TxDmaDesc\n"  /* 7220 and 7322-only */
4614         "E RxDlidFltr\n"  /* 7220 and 7322-only */
4615         "IBStatusChng\n"
4616         "IBLinkDown\n"
4617         "IBLnkRecov\n"
4618         "IBRxLinkErr\n"
4619         "IBSymbolErr\n"
4620         "RxLLIErr\n"
4621         "RxBadFormat\n"
4622         "RxBadLen\n"
4623         "RxBufOvrfl\n"
4624         "RxEBP\n"
4625         "RxFlowCtlErr\n"
4626         "RxICRCerr\n"
4627         "RxLPCRCerr\n"
4628         "RxVCRCerr\n"
4629         "RxInvalLen\n"
4630         "RxInvalPKey\n"
4631         "RxPktDropped\n"
4632         "TxBadLength\n"
4633         "TxDropped\n"
4634         "TxInvalLen\n"
4635         "TxUnderrun\n"
4636         "TxUnsupVL\n"
4637         "RxLclPhyErr\n" /* 7220 and 7322-only from here down */
4638         "RxVL15Drop\n"
4639         "RxVlErr\n"
4640         "XcessBufOvfl\n"
4641         "RxQPBadCtxt\n" /* 7322-only from here down */
4642         "TXBadHeader\n"
4643         ;
4644
4645 static const u32 portcntr7322indices[] = {
4646         QIBPORTCNTR_PKTSEND | _PORT_VIRT_FLAG,
4647         crp_pktsendflow,
4648         QIBPORTCNTR_WORDSEND | _PORT_VIRT_FLAG,
4649         QIBPORTCNTR_PKTRCV | _PORT_VIRT_FLAG,
4650         crp_pktrcvflowctrl,
4651         QIBPORTCNTR_WORDRCV | _PORT_VIRT_FLAG,
4652         QIBPORTCNTR_SENDSTALL | _PORT_VIRT_FLAG,
4653         crp_txsdmadesc | _PORT_64BIT_FLAG,
4654         crp_rxdlidfltr,
4655         crp_ibstatuschange,
4656         QIBPORTCNTR_IBLINKDOWN | _PORT_VIRT_FLAG,
4657         QIBPORTCNTR_IBLINKERRRECOV | _PORT_VIRT_FLAG,
4658         QIBPORTCNTR_ERRLINK | _PORT_VIRT_FLAG,
4659         QIBPORTCNTR_IBSYMBOLERR | _PORT_VIRT_FLAG,
4660         QIBPORTCNTR_LLI | _PORT_VIRT_FLAG,
4661         QIBPORTCNTR_BADFORMAT | _PORT_VIRT_FLAG,
4662         QIBPORTCNTR_ERR_RLEN | _PORT_VIRT_FLAG,
4663         QIBPORTCNTR_RCVOVFL | _PORT_VIRT_FLAG,
4664         QIBPORTCNTR_RCVEBP | _PORT_VIRT_FLAG,
4665         crp_rcvflowctrlviol,
4666         QIBPORTCNTR_ERRICRC | _PORT_VIRT_FLAG,
4667         QIBPORTCNTR_ERRLPCRC | _PORT_VIRT_FLAG,
4668         QIBPORTCNTR_ERRVCRC | _PORT_VIRT_FLAG,
4669         QIBPORTCNTR_INVALIDRLEN | _PORT_VIRT_FLAG,
4670         QIBPORTCNTR_ERRPKEY | _PORT_VIRT_FLAG,
4671         QIBPORTCNTR_RXDROPPKT | _PORT_VIRT_FLAG,
4672         crp_txminmaxlenerr,
4673         crp_txdroppedpkt,
4674         crp_txlenerr,
4675         crp_txunderrun,
4676         crp_txunsupvl,
4677         QIBPORTCNTR_RXLOCALPHYERR | _PORT_VIRT_FLAG,
4678         QIBPORTCNTR_VL15PKTDROP | _PORT_VIRT_FLAG,
4679         QIBPORTCNTR_RXVLERR | _PORT_VIRT_FLAG,
4680         QIBPORTCNTR_EXCESSBUFOVFL | _PORT_VIRT_FLAG,
4681         crp_rxqpinvalidctxt,
4682         crp_txhdrerr,
4683 };
4684
4685 /* do all the setup to make the counter reads efficient later */
4686 static void init_7322_cntrnames(struct qib_devdata *dd)
4687 {
4688         int i, j = 0;
4689         char *s;
4690
4691         for (i = 0, s = (char *)cntr7322names; s && j <= dd->cfgctxts;
4692              i++) {
4693                 /* we always have at least one counter before the egrovfl */
4694                 if (!j && !strncmp("Ctxt0EgrOvfl", s + 1, 12))
4695                         j = 1;
4696                 s = strchr(s + 1, '\n');
4697                 if (s && j)
4698                         j++;
4699         }
4700         dd->cspec->ncntrs = i;
4701         if (!s)
4702                 /* full list; size is without terminating null */
4703                 dd->cspec->cntrnamelen = sizeof(cntr7322names) - 1;
4704         else
4705                 dd->cspec->cntrnamelen = 1 + s - cntr7322names;
4706         dd->cspec->cntrs = kmalloc(dd->cspec->ncntrs
4707                 * sizeof(u64), GFP_KERNEL);
4708         if (!dd->cspec->cntrs)
4709                 qib_dev_err(dd, "Failed allocation for counters\n");
4710
4711         for (i = 0, s = (char *)portcntr7322names; s; i++)
4712                 s = strchr(s + 1, '\n');
4713         dd->cspec->nportcntrs = i - 1;
4714         dd->cspec->portcntrnamelen = sizeof(portcntr7322names) - 1;
4715         for (i = 0; i < dd->num_pports; ++i) {
4716                 dd->pport[i].cpspec->portcntrs = kmalloc(dd->cspec->nportcntrs
4717                         * sizeof(u64), GFP_KERNEL);
4718                 if (!dd->pport[i].cpspec->portcntrs)
4719                         qib_dev_err(dd, "Failed allocation for"
4720                                     " portcounters\n");
4721         }
4722 }
4723
4724 static u32 qib_read_7322cntrs(struct qib_devdata *dd, loff_t pos, char **namep,
4725                               u64 **cntrp)
4726 {
4727         u32 ret;
4728
4729         if (namep) {
4730                 ret = dd->cspec->cntrnamelen;
4731                 if (pos >= ret)
4732                         ret = 0; /* final read after getting everything */
4733                 else
4734                         *namep = (char *) cntr7322names;
4735         } else {
4736                 u64 *cntr = dd->cspec->cntrs;
4737                 int i;
4738
4739                 ret = dd->cspec->ncntrs * sizeof(u64);
4740                 if (!cntr || pos >= ret) {
4741                         /* everything read, or couldn't get memory */
4742                         ret = 0;
4743                         goto done;
4744                 }
4745                 *cntrp = cntr;
4746                 for (i = 0; i < dd->cspec->ncntrs; i++)
4747                         if (cntr7322indices[i] & _PORT_64BIT_FLAG)
4748                                 *cntr++ = read_7322_creg(dd,
4749                                                          cntr7322indices[i] &
4750                                                          _PORT_CNTR_IDXMASK);
4751                         else
4752                                 *cntr++ = read_7322_creg32(dd,
4753                                                            cntr7322indices[i]);
4754         }
4755 done:
4756         return ret;
4757 }
4758
4759 static u32 qib_read_7322portcntrs(struct qib_devdata *dd, loff_t pos, u32 port,
4760                                   char **namep, u64 **cntrp)
4761 {
4762         u32 ret;
4763
4764         if (namep) {
4765                 ret = dd->cspec->portcntrnamelen;
4766                 if (pos >= ret)
4767                         ret = 0; /* final read after getting everything */
4768                 else
4769                         *namep = (char *)portcntr7322names;
4770         } else {
4771                 struct qib_pportdata *ppd = &dd->pport[port];
4772                 u64 *cntr = ppd->cpspec->portcntrs;
4773                 int i;
4774
4775                 ret = dd->cspec->nportcntrs * sizeof(u64);
4776                 if (!cntr || pos >= ret) {
4777                         /* everything read, or couldn't get memory */
4778                         ret = 0;
4779                         goto done;
4780                 }
4781                 *cntrp = cntr;
4782                 for (i = 0; i < dd->cspec->nportcntrs; i++) {
4783                         if (portcntr7322indices[i] & _PORT_VIRT_FLAG)
4784                                 *cntr++ = qib_portcntr_7322(ppd,
4785                                         portcntr7322indices[i] &
4786                                         _PORT_CNTR_IDXMASK);
4787                         else if (portcntr7322indices[i] & _PORT_64BIT_FLAG)
4788                                 *cntr++ = read_7322_creg_port(ppd,
4789                                            portcntr7322indices[i] &
4790                                             _PORT_CNTR_IDXMASK);
4791                         else
4792                                 *cntr++ = read_7322_creg32_port(ppd,
4793                                            portcntr7322indices[i]);
4794                 }
4795         }
4796 done:
4797         return ret;
4798 }
4799
4800 /**
4801  * qib_get_7322_faststats - get word counters from chip before they overflow
4802  * @opaque - contains a pointer to the qlogic_ib device qib_devdata
4803  *
4804  * VESTIGIAL IBA7322 has no "small fast counters", so the only
4805  * real purpose of this function is to maintain the notion of
4806  * "active time", which in turn is only logged into the eeprom,
4807  * which we don;t have, yet, for 7322-based boards.
4808  *
4809  * called from add_timer
4810  */
4811 static void qib_get_7322_faststats(unsigned long opaque)
4812 {
4813         struct qib_devdata *dd = (struct qib_devdata *) opaque;
4814         struct qib_pportdata *ppd;
4815         unsigned long flags;
4816         u64 traffic_wds;
4817         int pidx;
4818
4819         for (pidx = 0; pidx < dd->num_pports; ++pidx) {
4820                 ppd = dd->pport + pidx;
4821
4822                 /*
4823                  * If port isn't enabled or not operational ports, or
4824                  * diags is running (can cause memory diags to fail)
4825                  * skip this port this time.
4826                  */
4827                 if (!ppd->link_speed_supported || !(dd->flags & QIB_INITTED)
4828                     || dd->diag_client)
4829                         continue;
4830
4831                 /*
4832                  * Maintain an activity timer, based on traffic
4833                  * exceeding a threshold, so we need to check the word-counts
4834                  * even if they are 64-bit.
4835                  */
4836                 traffic_wds = qib_portcntr_7322(ppd, QIBPORTCNTR_WORDRCV) +
4837                         qib_portcntr_7322(ppd, QIBPORTCNTR_WORDSEND);
4838                 spin_lock_irqsave(&ppd->dd->eep_st_lock, flags);
4839                 traffic_wds -= ppd->dd->traffic_wds;
4840                 ppd->dd->traffic_wds += traffic_wds;
4841                 if (traffic_wds >= QIB_TRAFFIC_ACTIVE_THRESHOLD)
4842                         atomic_add(ACTIVITY_TIMER, &ppd->dd->active_time);
4843                 spin_unlock_irqrestore(&ppd->dd->eep_st_lock, flags);
4844                 if (ppd->cpspec->qdr_dfe_on && (ppd->link_speed_active &
4845                                                 QIB_IB_QDR) &&
4846                     (ppd->lflags & (QIBL_LINKINIT | QIBL_LINKARMED |
4847                                     QIBL_LINKACTIVE)) &&
4848                     ppd->cpspec->qdr_dfe_time &&
4849                     time_is_before_jiffies(ppd->cpspec->qdr_dfe_time)) {
4850                         ppd->cpspec->qdr_dfe_on = 0;
4851
4852                         qib_write_kreg_port(ppd, krp_static_adapt_dis(2),
4853                                             ppd->dd->cspec->r1 ?
4854                                             QDR_STATIC_ADAPT_INIT_R1 :
4855                                             QDR_STATIC_ADAPT_INIT);
4856                         force_h1(ppd);
4857                 }
4858         }
4859         mod_timer(&dd->stats_timer, jiffies + HZ * ACTIVITY_TIMER);
4860 }
4861
4862 /*
4863  * If we were using MSIx, try to fallback to INTx.
4864  */
4865 static int qib_7322_intr_fallback(struct qib_devdata *dd)
4866 {
4867         if (!dd->cspec->num_msix_entries)
4868                 return 0; /* already using INTx */
4869
4870         qib_devinfo(dd->pcidev, "MSIx interrupt not detected,"
4871                  " trying INTx interrupts\n");
4872         qib_7322_nomsix(dd);
4873         qib_enable_intx(dd->pcidev);
4874         qib_setup_7322_interrupt(dd, 0);
4875         return 1;
4876 }
4877
4878 /*
4879  * Reset the XGXS (between serdes and IBC).  Slightly less intrusive
4880  * than resetting the IBC or external link state, and useful in some
4881  * cases to cause some retraining.  To do this right, we reset IBC
4882  * as well, then return to previous state (which may be still in reset)
4883  * NOTE: some callers of this "know" this writes the current value
4884  * of cpspec->ibcctrl_a as part of it's operation, so if that changes,
4885  * check all callers.
4886  */
4887 static void qib_7322_mini_pcs_reset(struct qib_pportdata *ppd)
4888 {
4889         u64 val;
4890         struct qib_devdata *dd = ppd->dd;
4891         const u64 reset_bits = SYM_MASK(IBPCSConfig_0, xcv_rreset) |
4892                 SYM_MASK(IBPCSConfig_0, xcv_treset) |
4893                 SYM_MASK(IBPCSConfig_0, tx_rx_reset);
4894
4895         val = qib_read_kreg_port(ppd, krp_ib_pcsconfig);
4896         qib_write_kreg(dd, kr_hwerrmask,
4897                        dd->cspec->hwerrmask & ~HWE_MASK(statusValidNoEop));
4898         qib_write_kreg_port(ppd, krp_ibcctrl_a,
4899                             ppd->cpspec->ibcctrl_a &
4900                             ~SYM_MASK(IBCCtrlA_0, IBLinkEn));
4901
4902         qib_write_kreg_port(ppd, krp_ib_pcsconfig, val | reset_bits);
4903         qib_read_kreg32(dd, kr_scratch);
4904         qib_write_kreg_port(ppd, krp_ib_pcsconfig, val & ~reset_bits);
4905         qib_write_kreg_port(ppd, krp_ibcctrl_a, ppd->cpspec->ibcctrl_a);
4906         qib_write_kreg(dd, kr_scratch, 0ULL);
4907         qib_write_kreg(dd, kr_hwerrclear,
4908                        SYM_MASK(HwErrClear, statusValidNoEopClear));
4909         qib_write_kreg(dd, kr_hwerrmask, dd->cspec->hwerrmask);
4910 }
4911
4912 /*
4913  * This code for non-IBTA-compliant IB speed negotiation is only known to
4914  * work for the SDR to DDR transition, and only between an HCA and a switch
4915  * with recent firmware.  It is based on observed heuristics, rather than
4916  * actual knowledge of the non-compliant speed negotiation.
4917  * It has a number of hard-coded fields, since the hope is to rewrite this
4918  * when a spec is available on how the negoation is intended to work.
4919  */
4920 static void autoneg_7322_sendpkt(struct qib_pportdata *ppd, u32 *hdr,
4921                                  u32 dcnt, u32 *data)
4922 {
4923         int i;
4924         u64 pbc;
4925         u32 __iomem *piobuf;
4926         u32 pnum, control, len;
4927         struct qib_devdata *dd = ppd->dd;
4928
4929         i = 0;
4930         len = 7 + dcnt + 1; /* 7 dword header, dword data, icrc */
4931         control = qib_7322_setpbc_control(ppd, len, 0, 15);
4932         pbc = ((u64) control << 32) | len;
4933         while (!(piobuf = qib_7322_getsendbuf(ppd, pbc, &pnum))) {
4934                 if (i++ > 15)
4935                         return;
4936                 udelay(2);
4937         }
4938         /* disable header check on this packet, since it can't be valid */
4939         dd->f_txchk_change(dd, pnum, 1, TXCHK_CHG_TYPE_DIS1, NULL);
4940         writeq(pbc, piobuf);
4941         qib_flush_wc();
4942         qib_pio_copy(piobuf + 2, hdr, 7);
4943         qib_pio_copy(piobuf + 9, data, dcnt);
4944         if (dd->flags & QIB_USE_SPCL_TRIG) {
4945                 u32 spcl_off = (pnum >= dd->piobcnt2k) ? 2047 : 1023;
4946
4947                 qib_flush_wc();
4948                 __raw_writel(0xaebecede, piobuf + spcl_off);
4949         }
4950         qib_flush_wc();
4951         qib_sendbuf_done(dd, pnum);
4952         /* and re-enable hdr check */
4953         dd->f_txchk_change(dd, pnum, 1, TXCHK_CHG_TYPE_ENAB1, NULL);
4954 }
4955
4956 /*
4957  * _start packet gets sent twice at start, _done gets sent twice at end
4958  */
4959 static void qib_autoneg_7322_send(struct qib_pportdata *ppd, int which)
4960 {
4961         struct qib_devdata *dd = ppd->dd;
4962         static u32 swapped;
4963         u32 dw, i, hcnt, dcnt, *data;
4964         static u32 hdr[7] = { 0xf002ffff, 0x48ffff, 0x6400abba };
4965         static u32 madpayload_start[0x40] = {
4966                 0x1810103, 0x1, 0x0, 0x0, 0x2c90000, 0x2c9, 0x0, 0x0,
4967                 0xffffffff, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
4968                 0x1, 0x1388, 0x15e, 0x1, /* rest 0's */
4969                 };
4970         static u32 madpayload_done[0x40] = {
4971                 0x1810103, 0x1, 0x0, 0x0, 0x2c90000, 0x2c9, 0x0, 0x0,
4972                 0xffffffff, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
4973                 0x40000001, 0x1388, 0x15e, /* rest 0's */
4974                 };
4975
4976         dcnt = ARRAY_SIZE(madpayload_start);
4977         hcnt = ARRAY_SIZE(hdr);
4978         if (!swapped) {
4979                 /* for maintainability, do it at runtime */
4980                 for (i = 0; i < hcnt; i++) {
4981                         dw = (__force u32) cpu_to_be32(hdr[i]);
4982                         hdr[i] = dw;
4983                 }
4984                 for (i = 0; i < dcnt; i++) {
4985                         dw = (__force u32) cpu_to_be32(madpayload_start[i]);
4986                         madpayload_start[i] = dw;
4987                         dw = (__force u32) cpu_to_be32(madpayload_done[i]);
4988                         madpayload_done[i] = dw;
4989                 }
4990                 swapped = 1;
4991         }
4992
4993         data = which ? madpayload_done : madpayload_start;
4994
4995         autoneg_7322_sendpkt(ppd, hdr, dcnt, data);
4996         qib_read_kreg64(dd, kr_scratch);
4997         udelay(2);
4998         autoneg_7322_sendpkt(ppd, hdr, dcnt, data);
4999         qib_read_kreg64(dd, kr_scratch);
5000         udelay(2);
5001 }
5002
5003 /*
5004  * Do the absolute minimum to cause an IB speed change, and make it
5005  * ready, but don't actually trigger the change.   The caller will
5006  * do that when ready (if link is in Polling training state, it will
5007  * happen immediately, otherwise when link next goes down)
5008  *
5009  * This routine should only be used as part of the DDR autonegotation
5010  * code for devices that are not compliant with IB 1.2 (or code that
5011  * fixes things up for same).
5012  *
5013  * When link has gone down, and autoneg enabled, or autoneg has
5014  * failed and we give up until next time we set both speeds, and
5015  * then we want IBTA enabled as well as "use max enabled speed.
5016  */
5017 static void set_7322_ibspeed_fast(struct qib_pportdata *ppd, u32 speed)
5018 {
5019         u64 newctrlb;
5020         newctrlb = ppd->cpspec->ibcctrl_b & ~(IBA7322_IBC_SPEED_MASK |
5021                                     IBA7322_IBC_IBTA_1_2_MASK |
5022                                     IBA7322_IBC_MAX_SPEED_MASK);
5023
5024         if (speed & (speed - 1)) /* multiple speeds */
5025                 newctrlb |= (speed << IBA7322_IBC_SPEED_LSB) |
5026                                     IBA7322_IBC_IBTA_1_2_MASK |
5027                                     IBA7322_IBC_MAX_SPEED_MASK;
5028         else
5029                 newctrlb |= speed == QIB_IB_QDR ?
5030                         IBA7322_IBC_SPEED_QDR | IBA7322_IBC_IBTA_1_2_MASK :
5031                         ((speed == QIB_IB_DDR ?
5032                           IBA7322_IBC_SPEED_DDR : IBA7322_IBC_SPEED_SDR));
5033
5034         if (newctrlb == ppd->cpspec->ibcctrl_b)
5035                 return;
5036
5037         ppd->cpspec->ibcctrl_b = newctrlb;
5038         qib_write_kreg_port(ppd, krp_ibcctrl_b, ppd->cpspec->ibcctrl_b);
5039         qib_write_kreg(ppd->dd, kr_scratch, 0);
5040 }
5041
5042 /*
5043  * This routine is only used when we are not talking to another
5044  * IB 1.2-compliant device that we think can do DDR.
5045  * (This includes all existing switch chips as of Oct 2007.)
5046  * 1.2-compliant devices go directly to DDR prior to reaching INIT
5047  */
5048 static void try_7322_autoneg(struct qib_pportdata *ppd)
5049 {
5050         unsigned long flags;
5051
5052         spin_lock_irqsave(&ppd->lflags_lock, flags);
5053         ppd->lflags |= QIBL_IB_AUTONEG_INPROG;
5054         spin_unlock_irqrestore(&ppd->lflags_lock, flags);
5055         qib_autoneg_7322_send(ppd, 0);
5056         set_7322_ibspeed_fast(ppd, QIB_IB_DDR);
5057         qib_7322_mini_pcs_reset(ppd);
5058         /* 2 msec is minimum length of a poll cycle */
5059         queue_delayed_work(ib_wq, &ppd->cpspec->autoneg_work,
5060                            msecs_to_jiffies(2));
5061 }
5062
5063 /*
5064  * Handle the empirically determined mechanism for auto-negotiation
5065  * of DDR speed with switches.
5066  */
5067 static void autoneg_7322_work(struct work_struct *work)
5068 {
5069         struct qib_pportdata *ppd;
5070         struct qib_devdata *dd;
5071         u64 startms;
5072         u32 i;
5073         unsigned long flags;
5074
5075         ppd = container_of(work, struct qib_chippport_specific,
5076                             autoneg_work.work)->ppd;
5077         dd = ppd->dd;
5078
5079         startms = jiffies_to_msecs(jiffies);
5080
5081         /*
5082          * Busy wait for this first part, it should be at most a
5083          * few hundred usec, since we scheduled ourselves for 2msec.
5084          */
5085         for (i = 0; i < 25; i++) {
5086                 if (SYM_FIELD(ppd->lastibcstat, IBCStatusA_0, LinkState)
5087                      == IB_7322_LT_STATE_POLLQUIET) {
5088                         qib_set_linkstate(ppd, QIB_IB_LINKDOWN_DISABLE);
5089                         break;
5090                 }
5091                 udelay(100);
5092         }
5093
5094         if (!(ppd->lflags & QIBL_IB_AUTONEG_INPROG))
5095                 goto done; /* we got there early or told to stop */
5096
5097         /* we expect this to timeout */
5098         if (wait_event_timeout(ppd->cpspec->autoneg_wait,
5099                                !(ppd->lflags & QIBL_IB_AUTONEG_INPROG),
5100                                msecs_to_jiffies(90)))
5101                 goto done;
5102         qib_7322_mini_pcs_reset(ppd);
5103
5104         /* we expect this to timeout */
5105         if (wait_event_timeout(ppd->cpspec->autoneg_wait,
5106                                !(ppd->lflags & QIBL_IB_AUTONEG_INPROG),
5107                                msecs_to_jiffies(1700)))
5108                 goto done;
5109         qib_7322_mini_pcs_reset(ppd);
5110
5111         set_7322_ibspeed_fast(ppd, QIB_IB_SDR);
5112
5113         /*
5114          * Wait up to 250 msec for link to train and get to INIT at DDR;
5115          * this should terminate early.
5116          */
5117         wait_event_timeout(ppd->cpspec->autoneg_wait,
5118                 !(ppd->lflags & QIBL_IB_AUTONEG_INPROG),
5119                 msecs_to_jiffies(250));
5120 done:
5121         if (ppd->lflags & QIBL_IB_AUTONEG_INPROG) {
5122                 spin_lock_irqsave(&ppd->lflags_lock, flags);
5123                 ppd->lflags &= ~QIBL_IB_AUTONEG_INPROG;
5124                 if (ppd->cpspec->autoneg_tries == AUTONEG_TRIES) {
5125                         ppd->lflags |= QIBL_IB_AUTONEG_FAILED;
5126                         ppd->cpspec->autoneg_tries = 0;
5127                 }
5128                 spin_unlock_irqrestore(&ppd->lflags_lock, flags);
5129                 set_7322_ibspeed_fast(ppd, ppd->link_speed_enabled);
5130         }
5131 }
5132
5133 /*
5134  * This routine is used to request IPG set in the QLogic switch.
5135  * Only called if r1.
5136  */
5137 static void try_7322_ipg(struct qib_pportdata *ppd)
5138 {
5139         struct qib_ibport *ibp = &ppd->ibport_data;
5140         struct ib_mad_send_buf *send_buf;
5141         struct ib_mad_agent *agent;
5142         struct ib_smp *smp;
5143         unsigned delay;
5144         int ret;
5145
5146         agent = ibp->send_agent;
5147         if (!agent)
5148                 goto retry;
5149
5150         send_buf = ib_create_send_mad(agent, 0, 0, 0, IB_MGMT_MAD_HDR,
5151                                       IB_MGMT_MAD_DATA, GFP_ATOMIC);
5152         if (IS_ERR(send_buf))
5153                 goto retry;
5154
5155         if (!ibp->smi_ah) {
5156                 struct ib_ah *ah;
5157
5158                 ah = qib_create_qp0_ah(ibp, be16_to_cpu(IB_LID_PERMISSIVE));
5159                 if (IS_ERR(ah))
5160                         ret = PTR_ERR(ah);
5161                 else {
5162                         send_buf->ah = ah;
5163                         ibp->smi_ah = to_iah(ah);
5164                         ret = 0;
5165                 }
5166         } else {
5167                 send_buf->ah = &ibp->smi_ah->ibah;
5168                 ret = 0;
5169         }
5170
5171         smp = send_buf->mad;
5172         smp->base_version = IB_MGMT_BASE_VERSION;
5173         smp->mgmt_class = IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE;
5174         smp->class_version = 1;
5175         smp->method = IB_MGMT_METHOD_SEND;
5176         smp->hop_cnt = 1;
5177         smp->attr_id = QIB_VENDOR_IPG;
5178         smp->attr_mod = 0;
5179
5180         if (!ret)
5181                 ret = ib_post_send_mad(send_buf, NULL);
5182         if (ret)
5183                 ib_free_send_mad(send_buf);
5184 retry:
5185         delay = 2 << ppd->cpspec->ipg_tries;
5186         queue_delayed_work(ib_wq, &ppd->cpspec->ipg_work,
5187                            msecs_to_jiffies(delay));
5188 }
5189
5190 /*
5191  * Timeout handler for setting IPG.
5192  * Only called if r1.
5193  */
5194 static void ipg_7322_work(struct work_struct *work)
5195 {
5196         struct qib_pportdata *ppd;
5197
5198         ppd = container_of(work, struct qib_chippport_specific,
5199                            ipg_work.work)->ppd;
5200         if ((ppd->lflags & (QIBL_LINKINIT | QIBL_LINKARMED | QIBL_LINKACTIVE))
5201             && ++ppd->cpspec->ipg_tries <= 10)
5202                 try_7322_ipg(ppd);
5203 }
5204
5205 static u32 qib_7322_iblink_state(u64 ibcs)
5206 {
5207         u32 state = (u32)SYM_FIELD(ibcs, IBCStatusA_0, LinkState);
5208
5209         switch (state) {
5210         case IB_7322_L_STATE_INIT:
5211                 state = IB_PORT_INIT;
5212                 break;
5213         case IB_7322_L_STATE_ARM:
5214                 state = IB_PORT_ARMED;
5215                 break;
5216         case IB_7322_L_STATE_ACTIVE:
5217                 /* fall through */
5218         case IB_7322_L_STATE_ACT_DEFER:
5219                 state = IB_PORT_ACTIVE;
5220                 break;
5221         default: /* fall through */
5222         case IB_7322_L_STATE_DOWN:
5223                 state = IB_PORT_DOWN;
5224                 break;
5225         }
5226         return state;
5227 }
5228
5229 /* returns the IBTA port state, rather than the IBC link training state */
5230 static u8 qib_7322_phys_portstate(u64 ibcs)
5231 {
5232         u8 state = (u8)SYM_FIELD(ibcs, IBCStatusA_0, LinkTrainingState);
5233         return qib_7322_physportstate[state];
5234 }
5235
5236 static int qib_7322_ib_updown(struct qib_pportdata *ppd, int ibup, u64 ibcs)
5237 {
5238         int ret = 0, symadj = 0;
5239         unsigned long flags;
5240         int mult;
5241
5242         spin_lock_irqsave(&ppd->lflags_lock, flags);
5243         ppd->lflags &= ~QIBL_IB_FORCE_NOTIFY;
5244         spin_unlock_irqrestore(&ppd->lflags_lock, flags);
5245
5246         /* Update our picture of width and speed from chip */
5247         if (ibcs & SYM_MASK(IBCStatusA_0, LinkSpeedQDR)) {
5248                 ppd->link_speed_active = QIB_IB_QDR;
5249                 mult = 4;
5250         } else if (ibcs & SYM_MASK(IBCStatusA_0, LinkSpeedActive)) {
5251                 ppd->link_speed_active = QIB_IB_DDR;
5252                 mult = 2;
5253         } else {
5254                 ppd->link_speed_active = QIB_IB_SDR;
5255                 mult = 1;
5256         }
5257         if (ibcs & SYM_MASK(IBCStatusA_0, LinkWidthActive)) {
5258                 ppd->link_width_active = IB_WIDTH_4X;
5259                 mult *= 4;
5260         } else
5261                 ppd->link_width_active = IB_WIDTH_1X;
5262         ppd->delay_mult = ib_rate_to_delay[mult_to_ib_rate(mult)];
5263
5264         if (!ibup) {
5265                 u64 clr;
5266
5267                 /* Link went down. */
5268                 /* do IPG MAD again after linkdown, even if last time failed */
5269                 ppd->cpspec->ipg_tries = 0;
5270                 clr = qib_read_kreg_port(ppd, krp_ibcstatus_b) &
5271                         (SYM_MASK(IBCStatusB_0, heartbeat_timed_out) |
5272                          SYM_MASK(IBCStatusB_0, heartbeat_crosstalk));
5273                 if (clr)
5274                         qib_write_kreg_port(ppd, krp_ibcstatus_b, clr);
5275                 if (!(ppd->lflags & (QIBL_IB_AUTONEG_FAILED |
5276                                      QIBL_IB_AUTONEG_INPROG)))
5277                         set_7322_ibspeed_fast(ppd, ppd->link_speed_enabled);
5278                 if (!(ppd->lflags & QIBL_IB_AUTONEG_INPROG)) {
5279                         struct qib_qsfp_data *qd =
5280                                 &ppd->cpspec->qsfp_data;
5281                         /* unlock the Tx settings, speed may change */
5282                         qib_write_kreg_port(ppd, krp_tx_deemph_override,
5283                                 SYM_MASK(IBSD_TX_DEEMPHASIS_OVERRIDE_0,
5284                                 reset_tx_deemphasis_override));
5285                         qib_cancel_sends(ppd);
5286                         /* on link down, ensure sane pcs state */
5287                         qib_7322_mini_pcs_reset(ppd);
5288                         /* schedule the qsfp refresh which should turn the link
5289                            off */
5290                         if (ppd->dd->flags & QIB_HAS_QSFP) {
5291                                 qd->t_insert = jiffies;
5292                                 queue_work(ib_wq, &qd->work);
5293                         }
5294                         spin_lock_irqsave(&ppd->sdma_lock, flags);
5295                         if (__qib_sdma_running(ppd))
5296                                 __qib_sdma_process_event(ppd,
5297                                         qib_sdma_event_e70_go_idle);
5298                         spin_unlock_irqrestore(&ppd->sdma_lock, flags);
5299                 }
5300                 clr = read_7322_creg32_port(ppd, crp_iblinkdown);
5301                 if (clr == ppd->cpspec->iblnkdownsnap)
5302                         ppd->cpspec->iblnkdowndelta++;
5303         } else {
5304                 if (qib_compat_ddr_negotiate &&
5305                     !(ppd->lflags & (QIBL_IB_AUTONEG_FAILED |
5306                                      QIBL_IB_AUTONEG_INPROG)) &&
5307                     ppd->link_speed_active == QIB_IB_SDR &&
5308                     (ppd->link_speed_enabled & QIB_IB_DDR)
5309                     && ppd->cpspec->autoneg_tries < AUTONEG_TRIES) {
5310                         /* we are SDR, and auto-negotiation enabled */
5311                         ++ppd->cpspec->autoneg_tries;
5312                         if (!ppd->cpspec->ibdeltainprog) {
5313                                 ppd->cpspec->ibdeltainprog = 1;
5314                                 ppd->cpspec->ibsymdelta +=
5315                                         read_7322_creg32_port(ppd,
5316                                                 crp_ibsymbolerr) -
5317                                                 ppd->cpspec->ibsymsnap;
5318                                 ppd->cpspec->iblnkerrdelta +=
5319                                         read_7322_creg32_port(ppd,
5320                                                 crp_iblinkerrrecov) -
5321                                                 ppd->cpspec->iblnkerrsnap;
5322                         }
5323                         try_7322_autoneg(ppd);
5324                         ret = 1; /* no other IB status change processing */
5325                 } else if ((ppd->lflags & QIBL_IB_AUTONEG_INPROG) &&
5326                            ppd->link_speed_active == QIB_IB_SDR) {
5327                         qib_autoneg_7322_send(ppd, 1);
5328                         set_7322_ibspeed_fast(ppd, QIB_IB_DDR);
5329                         qib_7322_mini_pcs_reset(ppd);
5330                         udelay(2);
5331                         ret = 1; /* no other IB status change processing */
5332                 } else if ((ppd->lflags & QIBL_IB_AUTONEG_INPROG) &&
5333                            (ppd->link_speed_active & QIB_IB_DDR)) {
5334                         spin_lock_irqsave(&ppd->lflags_lock, flags);
5335                         ppd->lflags &= ~(QIBL_IB_AUTONEG_INPROG |
5336                                          QIBL_IB_AUTONEG_FAILED);
5337                         spin_unlock_irqrestore(&ppd->lflags_lock, flags);
5338                         ppd->cpspec->autoneg_tries = 0;
5339                         /* re-enable SDR, for next link down */
5340                         set_7322_ibspeed_fast(ppd, ppd->link_speed_enabled);
5341                         wake_up(&ppd->cpspec->autoneg_wait);
5342                         symadj = 1;
5343                 } else if (ppd->lflags & QIBL_IB_AUTONEG_FAILED) {
5344                         /*
5345                          * Clear autoneg failure flag, and do setup
5346                          * so we'll try next time link goes down and
5347                          * back to INIT (possibly connected to a
5348                          * different device).
5349                          */
5350                         spin_lock_irqsave(&ppd->lflags_lock, flags);
5351                         ppd->lflags &= ~QIBL_IB_AUTONEG_FAILED;
5352                         spin_unlock_irqrestore(&ppd->lflags_lock, flags);
5353                         ppd->cpspec->ibcctrl_b |= IBA7322_IBC_IBTA_1_2_MASK;
5354                         symadj = 1;
5355                 }
5356                 if (!(ppd->lflags & QIBL_IB_AUTONEG_INPROG)) {
5357                         symadj = 1;
5358                         if (ppd->dd->cspec->r1 && ppd->cpspec->ipg_tries <= 10)
5359                                 try_7322_ipg(ppd);
5360                         if (!ppd->cpspec->recovery_init)
5361                                 setup_7322_link_recovery(ppd, 0);
5362                         ppd->cpspec->qdr_dfe_time = jiffies +
5363                                 msecs_to_jiffies(QDR_DFE_DISABLE_DELAY);
5364                 }
5365                 ppd->cpspec->ibmalfusesnap = 0;
5366                 ppd->cpspec->ibmalfsnap = read_7322_creg32_port(ppd,
5367                         crp_errlink);
5368         }
5369         if (symadj) {
5370                 ppd->cpspec->iblnkdownsnap =
5371                         read_7322_creg32_port(ppd, crp_iblinkdown);
5372                 if (ppd->cpspec->ibdeltainprog) {
5373                         ppd->cpspec->ibdeltainprog = 0;
5374                         ppd->cpspec->ibsymdelta += read_7322_creg32_port(ppd,
5375                                 crp_ibsymbolerr) - ppd->cpspec->ibsymsnap;
5376                         ppd->cpspec->iblnkerrdelta += read_7322_creg32_port(ppd,
5377                                 crp_iblinkerrrecov) - ppd->cpspec->iblnkerrsnap;
5378                 }
5379         } else if (!ibup && qib_compat_ddr_negotiate &&
5380                    !ppd->cpspec->ibdeltainprog &&
5381                         !(ppd->lflags & QIBL_IB_AUTONEG_INPROG)) {
5382                 ppd->cpspec->ibdeltainprog = 1;
5383                 ppd->cpspec->ibsymsnap = read_7322_creg32_port(ppd,
5384                         crp_ibsymbolerr);
5385                 ppd->cpspec->iblnkerrsnap = read_7322_creg32_port(ppd,
5386                         crp_iblinkerrrecov);
5387         }
5388
5389         if (!ret)
5390                 qib_setup_7322_setextled(ppd, ibup);
5391         return ret;
5392 }
5393
5394 /*
5395  * Does read/modify/write to appropriate registers to
5396  * set output and direction bits selected by mask.
5397  * these are in their canonical postions (e.g. lsb of
5398  * dir will end up in D48 of extctrl on existing chips).
5399  * returns contents of GP Inputs.
5400  */
5401 static int gpio_7322_mod(struct qib_devdata *dd, u32 out, u32 dir, u32 mask)
5402 {
5403         u64 read_val, new_out;
5404         unsigned long flags;
5405
5406         if (mask) {
5407                 /* some bits being written, lock access to GPIO */
5408                 dir &= mask;
5409                 out &= mask;
5410                 spin_lock_irqsave(&dd->cspec->gpio_lock, flags);
5411                 dd->cspec->extctrl &= ~((u64)mask << SYM_LSB(EXTCtrl, GPIOOe));
5412                 dd->cspec->extctrl |= ((u64) dir << SYM_LSB(EXTCtrl, GPIOOe));
5413                 new_out = (dd->cspec->gpio_out & ~mask) | out;
5414
5415                 qib_write_kreg(dd, kr_extctrl, dd->cspec->extctrl);
5416                 qib_write_kreg(dd, kr_gpio_out, new_out);
5417                 dd->cspec->gpio_out = new_out;
5418                 spin_unlock_irqrestore(&dd->cspec->gpio_lock, flags);
5419         }
5420         /*
5421          * It is unlikely that a read at this time would get valid
5422          * data on a pin whose direction line was set in the same
5423          * call to this function. We include the read here because
5424          * that allows us to potentially combine a change on one pin with
5425          * a read on another, and because the old code did something like
5426          * this.
5427          */
5428         read_val = qib_read_kreg64(dd, kr_extstatus);
5429         return SYM_FIELD(read_val, EXTStatus, GPIOIn);
5430 }
5431
5432 /* Enable writes to config EEPROM, if possible. Returns previous state */
5433 static int qib_7322_eeprom_wen(struct qib_devdata *dd, int wen)
5434 {
5435         int prev_wen;
5436         u32 mask;
5437
5438         mask = 1 << QIB_EEPROM_WEN_NUM;
5439         prev_wen = ~gpio_7322_mod(dd, 0, 0, 0) >> QIB_EEPROM_WEN_NUM;
5440         gpio_7322_mod(dd, wen ? 0 : mask, mask, mask);
5441
5442         return prev_wen & 1;
5443 }
5444
5445 /*
5446  * Read fundamental info we need to use the chip.  These are
5447  * the registers that describe chip capabilities, and are
5448  * saved in shadow registers.
5449  */
5450 static void get_7322_chip_params(struct qib_devdata *dd)
5451 {
5452         u64 val;
5453         u32 piobufs;
5454         int mtu;
5455
5456         dd->palign = qib_read_kreg32(dd, kr_pagealign);
5457
5458         dd->uregbase = qib_read_kreg32(dd, kr_userregbase);
5459
5460         dd->rcvtidcnt = qib_read_kreg32(dd, kr_rcvtidcnt);
5461         dd->rcvtidbase = qib_read_kreg32(dd, kr_rcvtidbase);
5462         dd->rcvegrbase = qib_read_kreg32(dd, kr_rcvegrbase);
5463         dd->piobufbase = qib_read_kreg64(dd, kr_sendpiobufbase);
5464         dd->pio2k_bufbase = dd->piobufbase & 0xffffffff;
5465
5466         val = qib_read_kreg64(dd, kr_sendpiobufcnt);
5467         dd->piobcnt2k = val & ~0U;
5468         dd->piobcnt4k = val >> 32;
5469         val = qib_read_kreg64(dd, kr_sendpiosize);
5470         dd->piosize2k = val & ~0U;
5471         dd->piosize4k = val >> 32;
5472
5473         mtu = ib_mtu_enum_to_int(qib_ibmtu);
5474         if (mtu == -1)
5475                 mtu = QIB_DEFAULT_MTU;
5476         dd->pport[0].ibmtu = (u32)mtu;
5477         dd->pport[1].ibmtu = (u32)mtu;
5478
5479         /* these may be adjusted in init_chip_wc_pat() */
5480         dd->pio2kbase = (u32 __iomem *)
5481                 ((char __iomem *) dd->kregbase + dd->pio2k_bufbase);
5482         dd->pio4kbase = (u32 __iomem *)
5483                 ((char __iomem *) dd->kregbase +
5484                  (dd->piobufbase >> 32));
5485         /*
5486          * 4K buffers take 2 pages; we use roundup just to be
5487          * paranoid; we calculate it once here, rather than on
5488          * ever buf allocate
5489          */
5490         dd->align4k = ALIGN(dd->piosize4k, dd->palign);
5491
5492         piobufs = dd->piobcnt4k + dd->piobcnt2k + NUM_VL15_BUFS;
5493
5494         dd->pioavregs = ALIGN(piobufs, sizeof(u64) * BITS_PER_BYTE / 2) /
5495                 (sizeof(u64) * BITS_PER_BYTE / 2);
5496 }
5497
5498 /*
5499  * The chip base addresses in cspec and cpspec have to be set
5500  * after possible init_chip_wc_pat(), rather than in
5501  * get_7322_chip_params(), so split out as separate function
5502  */
5503 static void qib_7322_set_baseaddrs(struct qib_devdata *dd)
5504 {
5505         u32 cregbase;
5506         cregbase = qib_read_kreg32(dd, kr_counterregbase);
5507
5508         dd->cspec->cregbase = (u64 __iomem *)(cregbase +
5509                 (char __iomem *)dd->kregbase);
5510
5511         dd->egrtidbase = (u64 __iomem *)
5512                 ((char __iomem *) dd->kregbase + dd->rcvegrbase);
5513
5514         /* port registers are defined as relative to base of chip */
5515         dd->pport[0].cpspec->kpregbase =
5516                 (u64 __iomem *)((char __iomem *)dd->kregbase);
5517         dd->pport[1].cpspec->kpregbase =
5518                 (u64 __iomem *)(dd->palign +
5519                 (char __iomem *)dd->kregbase);
5520         dd->pport[0].cpspec->cpregbase =
5521                 (u64 __iomem *)(qib_read_kreg_port(&dd->pport[0],
5522                 kr_counterregbase) + (char __iomem *)dd->kregbase);
5523         dd->pport[1].cpspec->cpregbase =
5524                 (u64 __iomem *)(qib_read_kreg_port(&dd->pport[1],
5525                 kr_counterregbase) + (char __iomem *)dd->kregbase);
5526 }
5527
5528 /*
5529  * This is a fairly special-purpose observer, so we only support
5530  * the port-specific parts of SendCtrl
5531  */
5532
5533 #define SENDCTRL_SHADOWED (SYM_MASK(SendCtrl_0, SendEnable) |           \
5534                            SYM_MASK(SendCtrl_0, SDmaEnable) |           \
5535                            SYM_MASK(SendCtrl_0, SDmaIntEnable) |        \
5536                            SYM_MASK(SendCtrl_0, SDmaSingleDescriptor) | \
5537                            SYM_MASK(SendCtrl_0, SDmaHalt) |             \
5538                            SYM_MASK(SendCtrl_0, IBVLArbiterEn) |        \
5539                            SYM_MASK(SendCtrl_0, ForceCreditUpToDate))
5540
5541 static int sendctrl_hook(struct qib_devdata *dd,
5542                          const struct diag_observer *op, u32 offs,
5543                          u64 *data, u64 mask, int only_32)
5544 {
5545         unsigned long flags;
5546         unsigned idx;
5547         unsigned pidx;
5548         struct qib_pportdata *ppd = NULL;
5549         u64 local_data, all_bits;
5550
5551         /*
5552          * The fixed correspondence between Physical ports and pports is
5553          * severed. We need to hunt for the ppd that corresponds
5554          * to the offset we got. And we have to do that without admitting
5555          * we know the stride, apparently.
5556          */
5557         for (pidx = 0; pidx < dd->num_pports; ++pidx) {
5558                 u64 __iomem *psptr;
5559                 u32 psoffs;
5560
5561                 ppd = dd->pport + pidx;
5562                 if (!ppd->cpspec->kpregbase)
5563                         continue;
5564
5565                 psptr = ppd->cpspec->kpregbase + krp_sendctrl;
5566                 psoffs = (u32) (psptr - dd->kregbase) * sizeof(*psptr);
5567                 if (psoffs == offs)
5568                         break;
5569         }
5570
5571         /* If pport is not being managed by driver, just avoid shadows. */
5572         if (pidx >= dd->num_pports)
5573                 ppd = NULL;
5574
5575         /* In any case, "idx" is flat index in kreg space */
5576         idx = offs / sizeof(u64);
5577
5578         all_bits = ~0ULL;
5579         if (only_32)
5580                 all_bits >>= 32;
5581
5582         spin_lock_irqsave(&dd->sendctrl_lock, flags);
5583         if (!ppd || (mask & all_bits) != all_bits) {
5584                 /*
5585                  * At least some mask bits are zero, so we need
5586                  * to read. The judgement call is whether from
5587                  * reg or shadow. First-cut: read reg, and complain
5588                  * if any bits which should be shadowed are different
5589                  * from their shadowed value.
5590                  */
5591                 if (only_32)
5592                         local_data = (u64)qib_read_kreg32(dd, idx);
5593                 else
5594                         local_data = qib_read_kreg64(dd, idx);
5595                 *data = (local_data & ~mask) | (*data & mask);
5596         }
5597         if (mask) {
5598                 /*
5599                  * At least some mask bits are one, so we need
5600                  * to write, but only shadow some bits.
5601                  */
5602                 u64 sval, tval; /* Shadowed, transient */
5603
5604                 /*
5605                  * New shadow val is bits we don't want to touch,
5606                  * ORed with bits we do, that are intended for shadow.
5607                  */
5608                 if (ppd) {
5609                         sval = ppd->p_sendctrl & ~mask;
5610                         sval |= *data & SENDCTRL_SHADOWED & mask;
5611                         ppd->p_sendctrl = sval;
5612                 } else
5613                         sval = *data & SENDCTRL_SHADOWED & mask;
5614                 tval = sval | (*data & ~SENDCTRL_SHADOWED & mask);
5615                 qib_write_kreg(dd, idx, tval);
5616                 qib_write_kreg(dd, kr_scratch, 0Ull);
5617         }
5618         spin_unlock_irqrestore(&dd->sendctrl_lock, flags);
5619         return only_32 ? 4 : 8;
5620 }
5621
5622 static const struct diag_observer sendctrl_0_observer = {
5623         sendctrl_hook, KREG_IDX(SendCtrl_0) * sizeof(u64),
5624         KREG_IDX(SendCtrl_0) * sizeof(u64)
5625 };
5626
5627 static const struct diag_observer sendctrl_1_observer = {
5628         sendctrl_hook, KREG_IDX(SendCtrl_1) * sizeof(u64),
5629         KREG_IDX(SendCtrl_1) * sizeof(u64)
5630 };
5631
5632 static ushort sdma_fetch_prio = 8;
5633 module_param_named(sdma_fetch_prio, sdma_fetch_prio, ushort, S_IRUGO);
5634 MODULE_PARM_DESC(sdma_fetch_prio, "SDMA descriptor fetch priority");
5635
5636 /* Besides logging QSFP events, we set appropriate TxDDS values */
5637 static void init_txdds_table(struct qib_pportdata *ppd, int override);
5638
5639 static void qsfp_7322_event(struct work_struct *work)
5640 {
5641         struct qib_qsfp_data *qd;
5642         struct qib_pportdata *ppd;
5643         unsigned long pwrup;
5644         unsigned long flags;
5645         int ret;
5646         u32 le2;
5647
5648         qd = container_of(work, struct qib_qsfp_data, work);
5649         ppd = qd->ppd;
5650         pwrup = qd->t_insert +
5651                 msecs_to_jiffies(QSFP_PWR_LAG_MSEC - QSFP_MODPRS_LAG_MSEC);
5652
5653         /* Delay for 20 msecs to allow ModPrs resistor to setup */
5654         mdelay(QSFP_MODPRS_LAG_MSEC);
5655
5656         if (!qib_qsfp_mod_present(ppd)) {
5657                 ppd->cpspec->qsfp_data.modpresent = 0;
5658                 /* Set the physical link to disabled */
5659                 qib_set_ib_7322_lstate(ppd, 0,
5660                                        QLOGIC_IB_IBCC_LINKINITCMD_DISABLE);
5661                 spin_lock_irqsave(&ppd->lflags_lock, flags);
5662                 ppd->lflags &= ~QIBL_LINKV;
5663                 spin_unlock_irqrestore(&ppd->lflags_lock, flags);
5664         } else {
5665                 /*
5666                  * Some QSFP's not only do not respond until the full power-up
5667                  * time, but may behave badly if we try. So hold off responding
5668                  * to insertion.
5669                  */
5670                 while (1) {
5671                         if (time_is_before_jiffies(pwrup))
5672                                 break;
5673                         msleep(20);
5674                 }
5675
5676                 ret = qib_refresh_qsfp_cache(ppd, &qd->cache);
5677
5678                 /*
5679                  * Need to change LE2 back to defaults if we couldn't
5680                  * read the cable type (to handle cable swaps), so do this
5681                  * even on failure to read cable information.  We don't
5682                  * get here for QME, so IS_QME check not needed here.
5683                  */
5684                 if (!ret && !ppd->dd->cspec->r1) {
5685                         if (QSFP_IS_ACTIVE_FAR(qd->cache.tech))
5686                                 le2 = LE2_QME;
5687                         else if (qd->cache.atten[1] >= qib_long_atten &&
5688                                  QSFP_IS_CU(qd->cache.tech))
5689                                 le2 = LE2_5m;
5690                         else
5691                                 le2 = LE2_DEFAULT;
5692                 } else
5693                         le2 = LE2_DEFAULT;
5694                 ibsd_wr_allchans(ppd, 13, (le2 << 7), BMASK(9, 7));
5695                 /*
5696                  * We always change parameteters, since we can choose
5697                  * values for cables without eeproms, and the cable may have
5698                  * changed from a cable with full or partial eeprom content
5699                  * to one with partial or no content.
5700                  */
5701                 init_txdds_table(ppd, 0);
5702                 /* The physical link is being re-enabled only when the
5703                  * previous state was DISABLED and the VALID bit is not
5704                  * set. This should only happen when  the cable has been
5705                  * physically pulled. */
5706                 if (!ppd->cpspec->qsfp_data.modpresent &&
5707                     (ppd->lflags & (QIBL_LINKV | QIBL_IB_LINK_DISABLED))) {
5708                         ppd->cpspec->qsfp_data.modpresent = 1;
5709                         qib_set_ib_7322_lstate(ppd, 0,
5710                                 QLOGIC_IB_IBCC_LINKINITCMD_SLEEP);
5711                         spin_lock_irqsave(&ppd->lflags_lock, flags);
5712                         ppd->lflags |= QIBL_LINKV;
5713                         spin_unlock_irqrestore(&ppd->lflags_lock, flags);
5714                 }
5715         }
5716 }
5717
5718 /*
5719  * There is little we can do but complain to the user if QSFP
5720  * initialization fails.
5721  */
5722 static void qib_init_7322_qsfp(struct qib_pportdata *ppd)
5723 {
5724         unsigned long flags;
5725         struct qib_qsfp_data *qd = &ppd->cpspec->qsfp_data;
5726         struct qib_devdata *dd = ppd->dd;
5727         u64 mod_prs_bit = QSFP_GPIO_MOD_PRS_N;
5728
5729         mod_prs_bit <<= (QSFP_GPIO_PORT2_SHIFT * ppd->hw_pidx);
5730         qd->ppd = ppd;
5731         qib_qsfp_init(qd, qsfp_7322_event);
5732         spin_lock_irqsave(&dd->cspec->gpio_lock, flags);
5733         dd->cspec->extctrl |= (mod_prs_bit << SYM_LSB(EXTCtrl, GPIOInvert));
5734         dd->cspec->gpio_mask |= mod_prs_bit;
5735         qib_write_kreg(dd, kr_extctrl, dd->cspec->extctrl);
5736         qib_write_kreg(dd, kr_gpio_mask, dd->cspec->gpio_mask);
5737         spin_unlock_irqrestore(&dd->cspec->gpio_lock, flags);
5738 }
5739
5740 /*
5741  * called at device initialization time, and also if the txselect
5742  * module parameter is changed.  This is used for cables that don't
5743  * have valid QSFP EEPROMs (not present, or attenuation is zero).
5744  * We initialize to the default, then if there is a specific
5745  * unit,port match, we use that (and set it immediately, for the
5746  * current speed, if the link is at INIT or better).
5747  * String format is "default# unit#,port#=# ... u,p=#", separators must
5748  * be a SPACE character.  A newline terminates.  The u,p=# tuples may
5749  * optionally have "u,p=#,#", where the final # is the H1 value
5750  * The last specific match is used (actually, all are used, but last
5751  * one is the one that winds up set); if none at all, fall back on default.
5752  */
5753 static void set_no_qsfp_atten(struct qib_devdata *dd, int change)
5754 {
5755         char *nxt, *str;
5756         u32 pidx, unit, port, deflt, h1;
5757         unsigned long val;
5758         int any = 0, seth1;
5759         int txdds_size;
5760
5761         str = txselect_list;
5762
5763         /* default number is validated in setup_txselect() */
5764         deflt = simple_strtoul(str, &nxt, 0);
5765         for (pidx = 0; pidx < dd->num_pports; ++pidx)
5766                 dd->pport[pidx].cpspec->no_eep = deflt;
5767
5768         txdds_size = TXDDS_TABLE_SZ + TXDDS_EXTRA_SZ;
5769         if (IS_QME(dd) || IS_QMH(dd))
5770                 txdds_size += TXDDS_MFG_SZ;
5771
5772         while (*nxt && nxt[1]) {
5773                 str = ++nxt;
5774                 unit = simple_strtoul(str, &nxt, 0);
5775                 if (nxt == str || !*nxt || *nxt != ',') {
5776                         while (*nxt && *nxt++ != ' ') /* skip to next, if any */
5777                                 ;
5778                         continue;
5779                 }
5780                 str = ++nxt;
5781                 port = simple_strtoul(str, &nxt, 0);
5782                 if (nxt == str || *nxt != '=') {
5783                         while (*nxt && *nxt++ != ' ') /* skip to next, if any */
5784                                 ;
5785                         continue;
5786                 }
5787                 str = ++nxt;
5788                 val = simple_strtoul(str, &nxt, 0);
5789                 if (nxt == str) {
5790                         while (*nxt && *nxt++ != ' ') /* skip to next, if any */
5791                                 ;
5792                         continue;
5793                 }
5794                 if (val >= txdds_size)
5795                         continue;
5796                 seth1 = 0;
5797                 h1 = 0; /* gcc thinks it might be used uninitted */
5798                 if (*nxt == ',' && nxt[1]) {
5799                         str = ++nxt;
5800                         h1 = (u32)simple_strtoul(str, &nxt, 0);
5801                         if (nxt == str)
5802                                 while (*nxt && *nxt++ != ' ') /* skip */
5803                                         ;
5804                         else
5805                                 seth1 = 1;
5806                 }
5807                 for (pidx = 0; dd->unit == unit && pidx < dd->num_pports;
5808                      ++pidx) {
5809                         struct qib_pportdata *ppd = &dd->pport[pidx];
5810
5811                         if (ppd->port != port || !ppd->link_speed_supported)
5812                                 continue;
5813                         ppd->cpspec->no_eep = val;
5814                         if (seth1)
5815                                 ppd->cpspec->h1_val = h1;
5816                         /* now change the IBC and serdes, overriding generic */
5817                         init_txdds_table(ppd, 1);
5818                         /* Re-enable the physical state machine on mezz boards
5819                          * now that the correct settings have been set.
5820                          * QSFP boards are handles by the QSFP event handler */
5821                         if (IS_QMH(dd) || IS_QME(dd))
5822                                 qib_set_ib_7322_lstate(ppd, 0,
5823                                             QLOGIC_IB_IBCC_LINKINITCMD_SLEEP);
5824                         any++;
5825                 }
5826                 if (*nxt == '\n')
5827                         break; /* done */
5828         }
5829         if (change && !any) {
5830                 /* no specific setting, use the default.
5831                  * Change the IBC and serdes, but since it's
5832                  * general, don't override specific settings.
5833                  */
5834                 for (pidx = 0; pidx < dd->num_pports; ++pidx)
5835                         if (dd->pport[pidx].link_speed_supported)
5836                                 init_txdds_table(&dd->pport[pidx], 0);
5837         }
5838 }
5839
5840 /* handle the txselect parameter changing */
5841 static int setup_txselect(const char *str, struct kernel_param *kp)
5842 {
5843         struct qib_devdata *dd;
5844         unsigned long val;
5845         char *n;
5846         if (strlen(str) >= MAX_ATTEN_LEN) {
5847                 printk(KERN_INFO QIB_DRV_NAME " txselect_values string "
5848                        "too long\n");
5849                 return -ENOSPC;
5850         }
5851         val = simple_strtoul(str, &n, 0);
5852         if (n == str || val >= (TXDDS_TABLE_SZ + TXDDS_EXTRA_SZ +
5853                                 TXDDS_MFG_SZ)) {
5854                 printk(KERN_INFO QIB_DRV_NAME
5855                        "txselect_values must start with a number < %d\n",
5856                         TXDDS_TABLE_SZ + TXDDS_EXTRA_SZ + TXDDS_MFG_SZ);
5857                 return -EINVAL;
5858         }
5859         strcpy(txselect_list, str);
5860
5861         list_for_each_entry(dd, &qib_dev_list, list)
5862                 if (dd->deviceid == PCI_DEVICE_ID_QLOGIC_IB_7322)
5863                         set_no_qsfp_atten(dd, 1);
5864         return 0;
5865 }
5866
5867 /*
5868  * Write the final few registers that depend on some of the
5869  * init setup.  Done late in init, just before bringing up
5870  * the serdes.
5871  */
5872 static int qib_late_7322_initreg(struct qib_devdata *dd)
5873 {
5874         int ret = 0, n;
5875         u64 val;
5876
5877         qib_write_kreg(dd, kr_rcvhdrentsize, dd->rcvhdrentsize);
5878         qib_write_kreg(dd, kr_rcvhdrsize, dd->rcvhdrsize);
5879         qib_write_kreg(dd, kr_rcvhdrcnt, dd->rcvhdrcnt);
5880         qib_write_kreg(dd, kr_sendpioavailaddr, dd->pioavailregs_phys);
5881         val = qib_read_kreg64(dd, kr_sendpioavailaddr);
5882         if (val != dd->pioavailregs_phys) {
5883                 qib_dev_err(dd, "Catastrophic software error, "
5884                             "SendPIOAvailAddr written as %lx, "
5885                             "read back as %llx\n",
5886                             (unsigned long) dd->pioavailregs_phys,
5887                             (unsigned long long) val);
5888                 ret = -EINVAL;
5889         }
5890
5891         n = dd->piobcnt2k + dd->piobcnt4k + NUM_VL15_BUFS;
5892         qib_7322_txchk_change(dd, 0, n, TXCHK_CHG_TYPE_KERN, NULL);
5893         /* driver sends get pkey, lid, etc. checking also, to catch bugs */
5894         qib_7322_txchk_change(dd, 0, n, TXCHK_CHG_TYPE_ENAB1, NULL);
5895
5896         qib_register_observer(dd, &sendctrl_0_observer);
5897         qib_register_observer(dd, &sendctrl_1_observer);
5898
5899         dd->control &= ~QLOGIC_IB_C_SDMAFETCHPRIOEN;
5900         qib_write_kreg(dd, kr_control, dd->control);
5901         /*
5902          * Set SendDmaFetchPriority and init Tx params, including
5903          * QSFP handler on boards that have QSFP.
5904          * First set our default attenuation entry for cables that
5905          * don't have valid attenuation.
5906          */
5907         set_no_qsfp_atten(dd, 0);
5908         for (n = 0; n < dd->num_pports; ++n) {
5909                 struct qib_pportdata *ppd = dd->pport + n;
5910
5911                 qib_write_kreg_port(ppd, krp_senddmaprioritythld,
5912                                     sdma_fetch_prio & 0xf);
5913                 /* Initialize qsfp if present on board. */
5914                 if (dd->flags & QIB_HAS_QSFP)
5915                         qib_init_7322_qsfp(ppd);
5916         }
5917         dd->control |= QLOGIC_IB_C_SDMAFETCHPRIOEN;
5918         qib_write_kreg(dd, kr_control, dd->control);
5919
5920         return ret;
5921 }
5922
5923 /* per IB port errors.  */
5924 #define SENDCTRL_PIBP (MASK_ACROSS(0, 1) | MASK_ACROSS(3, 3) | \
5925         MASK_ACROSS(8, 15))
5926 #define RCVCTRL_PIBP (MASK_ACROSS(0, 17) | MASK_ACROSS(39, 41))
5927 #define ERRS_PIBP (MASK_ACROSS(57, 58) | MASK_ACROSS(54, 54) | \
5928         MASK_ACROSS(36, 49) | MASK_ACROSS(29, 34) | MASK_ACROSS(14, 17) | \
5929         MASK_ACROSS(0, 11))
5930
5931 /*
5932  * Write the initialization per-port registers that need to be done at
5933  * driver load and after reset completes (i.e., that aren't done as part
5934  * of other init procedures called from qib_init.c).
5935  * Some of these should be redundant on reset, but play safe.
5936  */
5937 static void write_7322_init_portregs(struct qib_pportdata *ppd)
5938 {
5939         u64 val;
5940         int i;
5941
5942         if (!ppd->link_speed_supported) {
5943                 /* no buffer credits for this port */
5944                 for (i = 1; i < 8; i++)
5945                         qib_write_kreg_port(ppd, krp_rxcreditvl0 + i, 0);
5946                 qib_write_kreg_port(ppd, krp_ibcctrl_b, 0);
5947                 qib_write_kreg(ppd->dd, kr_scratch, 0);
5948                 return;
5949         }
5950
5951         /*
5952          * Set the number of supported virtual lanes in IBC,
5953          * for flow control packet handling on unsupported VLs
5954          */
5955         val = qib_read_kreg_port(ppd, krp_ibsdtestiftx);
5956         val &= ~SYM_MASK(IB_SDTEST_IF_TX_0, VL_CAP);
5957         val |= (u64)(ppd->vls_supported - 1) <<
5958                 SYM_LSB(IB_SDTEST_IF_TX_0, VL_CAP);
5959         qib_write_kreg_port(ppd, krp_ibsdtestiftx, val);
5960
5961         qib_write_kreg_port(ppd, krp_rcvbthqp, QIB_KD_QP);
5962
5963         /* enable tx header checking */
5964         qib_write_kreg_port(ppd, krp_sendcheckcontrol, IBA7322_SENDCHK_PKEY |
5965                             IBA7322_SENDCHK_BTHQP | IBA7322_SENDCHK_SLID |
5966                             IBA7322_SENDCHK_RAW_IPV6 | IBA7322_SENDCHK_MINSZ);
5967
5968         qib_write_kreg_port(ppd, krp_ncmodectrl,
5969                 SYM_MASK(IBNCModeCtrl_0, ScrambleCapLocal));
5970
5971         /*
5972          * Unconditionally clear the bufmask bits.  If SDMA is
5973          * enabled, we'll set them appropriately later.
5974          */
5975         qib_write_kreg_port(ppd, krp_senddmabufmask0, 0);
5976         qib_write_kreg_port(ppd, krp_senddmabufmask1, 0);
5977         qib_write_kreg_port(ppd, krp_senddmabufmask2, 0);
5978         if (ppd->dd->cspec->r1)
5979                 ppd->p_sendctrl |= SYM_MASK(SendCtrl_0, ForceCreditUpToDate);
5980 }
5981
5982 /*
5983  * Write the initialization per-device registers that need to be done at
5984  * driver load and after reset completes (i.e., that aren't done as part
5985  * of other init procedures called from qib_init.c).  Also write per-port
5986  * registers that are affected by overall device config, such as QP mapping
5987  * Some of these should be redundant on reset, but play safe.
5988  */
5989 static void write_7322_initregs(struct qib_devdata *dd)
5990 {
5991         struct qib_pportdata *ppd;
5992         int i, pidx;
5993         u64 val;
5994
5995         /* Set Multicast QPs received by port 2 to map to context one. */
5996         qib_write_kreg(dd, KREG_IDX(RcvQPMulticastContext_1), 1);
5997
5998         for (pidx = 0; pidx < dd->num_pports; ++pidx) {
5999                 unsigned n, regno;
6000                 unsigned long flags;
6001
6002                 if (dd->n_krcv_queues < 2 ||
6003                         !dd->pport[pidx].link_speed_supported)
6004                         continue;
6005
6006                 ppd = &dd->pport[pidx];
6007
6008                 /* be paranoid against later code motion, etc. */
6009                 spin_lock_irqsave(&dd->cspec->rcvmod_lock, flags);
6010                 ppd->p_rcvctrl |= SYM_MASK(RcvCtrl_0, RcvQPMapEnable);
6011                 spin_unlock_irqrestore(&dd->cspec->rcvmod_lock, flags);
6012
6013                 /* Initialize QP to context mapping */
6014                 regno = krp_rcvqpmaptable;
6015                 val = 0;
6016                 if (dd->num_pports > 1)
6017                         n = dd->first_user_ctxt / dd->num_pports;
6018                 else
6019                         n = dd->first_user_ctxt - 1;
6020                 for (i = 0; i < 32; ) {
6021                         unsigned ctxt;
6022
6023                         if (dd->num_pports > 1)
6024                                 ctxt = (i % n) * dd->num_pports + pidx;
6025                         else if (i % n)
6026                                 ctxt = (i % n) + 1;
6027                         else
6028                                 ctxt = ppd->hw_pidx;
6029                         val |= ctxt << (5 * (i % 6));
6030                         i++;
6031                         if (i % 6 == 0) {
6032                                 qib_write_kreg_port(ppd, regno, val);
6033                                 val = 0;
6034                                 regno++;
6035                         }
6036                 }
6037                 qib_write_kreg_port(ppd, regno, val);
6038         }
6039
6040         /*
6041          * Setup up interrupt mitigation for kernel contexts, but
6042          * not user contexts (user contexts use interrupts when
6043          * stalled waiting for any packet, so want those interrupts
6044          * right away).
6045          */
6046         for (i = 0; i < dd->first_user_ctxt; i++) {
6047                 dd->cspec->rcvavail_timeout[i] = rcv_int_timeout;
6048                 qib_write_kreg(dd, kr_rcvavailtimeout + i, rcv_int_timeout);
6049         }
6050
6051         /*
6052          * Initialize  as (disabled) rcvflow tables.  Application code
6053          * will setup each flow as it uses the flow.
6054          * Doesn't clear any of the error bits that might be set.
6055          */
6056         val = TIDFLOW_ERRBITS; /* these are W1C */
6057         for (i = 0; i < dd->cfgctxts; i++) {
6058                 int flow;
6059                 for (flow = 0; flow < NUM_TIDFLOWS_CTXT; flow++)
6060                         qib_write_ureg(dd, ur_rcvflowtable+flow, val, i);
6061         }
6062
6063         /*
6064          * dual cards init to dual port recovery, single port cards to
6065          * the one port.  Dual port cards may later adjust to 1 port,
6066          * and then back to dual port if both ports are connected
6067          * */
6068         if (dd->num_pports)
6069                 setup_7322_link_recovery(dd->pport, dd->num_pports > 1);
6070 }
6071
6072 static int qib_init_7322_variables(struct qib_devdata *dd)
6073 {
6074         struct qib_pportdata *ppd;
6075         unsigned features, pidx, sbufcnt;
6076         int ret, mtu;
6077         u32 sbufs, updthresh;
6078
6079         /* pport structs are contiguous, allocated after devdata */
6080         ppd = (struct qib_pportdata *)(dd + 1);
6081         dd->pport = ppd;
6082         ppd[0].dd = dd;
6083         ppd[1].dd = dd;
6084
6085         dd->cspec = (struct qib_chip_specific *)(ppd + 2);
6086
6087         ppd[0].cpspec = (struct qib_chippport_specific *)(dd->cspec + 1);
6088         ppd[1].cpspec = &ppd[0].cpspec[1];
6089         ppd[0].cpspec->ppd = &ppd[0]; /* for autoneg_7322_work() */
6090         ppd[1].cpspec->ppd = &ppd[1]; /* for autoneg_7322_work() */
6091
6092         spin_lock_init(&dd->cspec->rcvmod_lock);
6093         spin_lock_init(&dd->cspec->gpio_lock);
6094
6095         /* we haven't yet set QIB_PRESENT, so use read directly */
6096         dd->revision = readq(&dd->kregbase[kr_revision]);
6097
6098         if ((dd->revision & 0xffffffffU) == 0xffffffffU) {
6099                 qib_dev_err(dd, "Revision register read failure, "
6100                             "giving up initialization\n");
6101                 ret = -ENODEV;
6102                 goto bail;
6103         }
6104         dd->flags |= QIB_PRESENT;  /* now register routines work */
6105
6106         dd->majrev = (u8) SYM_FIELD(dd->revision, Revision_R, ChipRevMajor);
6107         dd->minrev = (u8) SYM_FIELD(dd->revision, Revision_R, ChipRevMinor);
6108         dd->cspec->r1 = dd->minrev == 1;
6109
6110         get_7322_chip_params(dd);
6111         features = qib_7322_boardname(dd);
6112
6113         /* now that piobcnt2k and 4k set, we can allocate these */
6114         sbufcnt = dd->piobcnt2k + dd->piobcnt4k +
6115                 NUM_VL15_BUFS + BITS_PER_LONG - 1;
6116         sbufcnt /= BITS_PER_LONG;
6117         dd->cspec->sendchkenable = kmalloc(sbufcnt *
6118                 sizeof(*dd->cspec->sendchkenable), GFP_KERNEL);
6119         dd->cspec->sendgrhchk = kmalloc(sbufcnt *
6120                 sizeof(*dd->cspec->sendgrhchk), GFP_KERNEL);
6121         dd->cspec->sendibchk = kmalloc(sbufcnt *
6122                 sizeof(*dd->cspec->sendibchk), GFP_KERNEL);
6123         if (!dd->cspec->sendchkenable || !dd->cspec->sendgrhchk ||
6124                 !dd->cspec->sendibchk) {
6125                 qib_dev_err(dd, "Failed allocation for hdrchk bitmaps\n");
6126                 ret = -ENOMEM;
6127                 goto bail;
6128         }
6129
6130         ppd = dd->pport;
6131
6132         /*
6133          * GPIO bits for TWSI data and clock,
6134          * used for serial EEPROM.
6135          */
6136         dd->gpio_sda_num = _QIB_GPIO_SDA_NUM;
6137         dd->gpio_scl_num = _QIB_GPIO_SCL_NUM;
6138         dd->twsi_eeprom_dev = QIB_TWSI_EEPROM_DEV;
6139
6140         dd->flags |= QIB_HAS_INTX | QIB_HAS_LINK_LATENCY |
6141                 QIB_NODMA_RTAIL | QIB_HAS_VLSUPP | QIB_HAS_HDRSUPP |
6142                 QIB_HAS_THRESH_UPDATE |
6143                 (sdma_idle_cnt ? QIB_HAS_SDMA_TIMEOUT : 0);
6144         dd->flags |= qib_special_trigger ?
6145                 QIB_USE_SPCL_TRIG : QIB_HAS_SEND_DMA;
6146
6147         /*
6148          * Setup initial values.  These may change when PAT is enabled, but
6149          * we need these to do initial chip register accesses.
6150          */
6151         qib_7322_set_baseaddrs(dd);
6152
6153         mtu = ib_mtu_enum_to_int(qib_ibmtu);
6154         if (mtu == -1)
6155                 mtu = QIB_DEFAULT_MTU;
6156
6157         dd->cspec->int_enable_mask = QIB_I_BITSEXTANT;
6158         /* all hwerrors become interrupts, unless special purposed */
6159         dd->cspec->hwerrmask = ~0ULL;
6160         /*  link_recovery setup causes these errors, so ignore them,
6161          *  other than clearing them when they occur */
6162         dd->cspec->hwerrmask &=
6163                 ~(SYM_MASK(HwErrMask, IBSerdesPClkNotDetectMask_0) |
6164                   SYM_MASK(HwErrMask, IBSerdesPClkNotDetectMask_1) |
6165                   HWE_MASK(LATriggered));
6166
6167         for (pidx = 0; pidx < NUM_IB_PORTS; ++pidx) {
6168                 struct qib_chippport_specific *cp = ppd->cpspec;
6169                 ppd->link_speed_supported = features & PORT_SPD_CAP;
6170                 features >>=  PORT_SPD_CAP_SHIFT;
6171                 if (!ppd->link_speed_supported) {
6172                         /* single port mode (7340, or configured) */
6173                         dd->skip_kctxt_mask |= 1 << pidx;
6174                         if (pidx == 0) {
6175                                 /* Make sure port is disabled. */
6176                                 qib_write_kreg_port(ppd, krp_rcvctrl, 0);
6177                                 qib_write_kreg_port(ppd, krp_ibcctrl_a, 0);
6178                                 ppd[0] = ppd[1];
6179                                 dd->cspec->hwerrmask &= ~(SYM_MASK(HwErrMask,
6180                                                   IBSerdesPClkNotDetectMask_0)
6181                                                   | SYM_MASK(HwErrMask,
6182                                                   SDmaMemReadErrMask_0));
6183                                 dd->cspec->int_enable_mask &= ~(
6184                                      SYM_MASK(IntMask, SDmaCleanupDoneMask_0) |
6185                                      SYM_MASK(IntMask, SDmaIdleIntMask_0) |
6186                                      SYM_MASK(IntMask, SDmaProgressIntMask_0) |
6187                                      SYM_MASK(IntMask, SDmaIntMask_0) |
6188                                      SYM_MASK(IntMask, ErrIntMask_0) |
6189                                      SYM_MASK(IntMask, SendDoneIntMask_0));
6190                         } else {
6191                                 /* Make sure port is disabled. */
6192                                 qib_write_kreg_port(ppd, krp_rcvctrl, 0);
6193                                 qib_write_kreg_port(ppd, krp_ibcctrl_a, 0);
6194                                 dd->cspec->hwerrmask &= ~(SYM_MASK(HwErrMask,
6195                                                   IBSerdesPClkNotDetectMask_1)
6196                                                   | SYM_MASK(HwErrMask,
6197                                                   SDmaMemReadErrMask_1));
6198                                 dd->cspec->int_enable_mask &= ~(
6199                                      SYM_MASK(IntMask, SDmaCleanupDoneMask_1) |
6200                                      SYM_MASK(IntMask, SDmaIdleIntMask_1) |
6201                                      SYM_MASK(IntMask, SDmaProgressIntMask_1) |
6202                                      SYM_MASK(IntMask, SDmaIntMask_1) |
6203                                      SYM_MASK(IntMask, ErrIntMask_1) |
6204                                      SYM_MASK(IntMask, SendDoneIntMask_1));
6205                         }
6206                         continue;
6207                 }
6208
6209                 dd->num_pports++;
6210                 qib_init_pportdata(ppd, dd, pidx, dd->num_pports);
6211
6212                 ppd->link_width_supported = IB_WIDTH_1X | IB_WIDTH_4X;
6213                 ppd->link_width_enabled = IB_WIDTH_4X;
6214                 ppd->link_speed_enabled = ppd->link_speed_supported;
6215                 /*
6216                  * Set the initial values to reasonable default, will be set
6217                  * for real when link is up.
6218                  */
6219                 ppd->link_width_active = IB_WIDTH_4X;
6220                 ppd->link_speed_active = QIB_IB_SDR;
6221                 ppd->delay_mult = ib_rate_to_delay[IB_RATE_10_GBPS];
6222                 switch (qib_num_cfg_vls) {
6223                 case 1:
6224                         ppd->vls_supported = IB_VL_VL0;
6225                         break;
6226                 case 2:
6227                         ppd->vls_supported = IB_VL_VL0_1;
6228                         break;
6229                 default:
6230                         qib_devinfo(dd->pcidev,
6231                                     "Invalid num_vls %u, using 4 VLs\n",
6232                                     qib_num_cfg_vls);
6233                         qib_num_cfg_vls = 4;
6234                         /* fall through */
6235                 case 4:
6236                         ppd->vls_supported = IB_VL_VL0_3;
6237                         break;
6238                 case 8:
6239                         if (mtu <= 2048)
6240                                 ppd->vls_supported = IB_VL_VL0_7;
6241                         else {
6242                                 qib_devinfo(dd->pcidev,
6243                                             "Invalid num_vls %u for MTU %d "
6244                                             ", using 4 VLs\n",
6245                                             qib_num_cfg_vls, mtu);
6246                                 ppd->vls_supported = IB_VL_VL0_3;
6247                                 qib_num_cfg_vls = 4;
6248                         }
6249                         break;
6250                 }
6251                 ppd->vls_operational = ppd->vls_supported;
6252
6253                 init_waitqueue_head(&cp->autoneg_wait);
6254                 INIT_DELAYED_WORK(&cp->autoneg_work,
6255                                   autoneg_7322_work);
6256                 if (ppd->dd->cspec->r1)
6257                         INIT_DELAYED_WORK(&cp->ipg_work, ipg_7322_work);
6258
6259                 /*
6260                  * For Mez and similar cards, no qsfp info, so do
6261                  * the "cable info" setup here.  Can be overridden
6262                  * in adapter-specific routines.
6263                  */
6264                 if (!(dd->flags & QIB_HAS_QSFP)) {
6265                         if (!IS_QMH(dd) && !IS_QME(dd))
6266                                 qib_devinfo(dd->pcidev, "IB%u:%u: "
6267                                             "Unknown mezzanine card type\n",
6268                                             dd->unit, ppd->port);
6269                         cp->h1_val = IS_QMH(dd) ? H1_FORCE_QMH : H1_FORCE_QME;
6270                         /*
6271                          * Choose center value as default tx serdes setting
6272                          * until changed through module parameter.
6273                          */
6274                         ppd->cpspec->no_eep = IS_QMH(dd) ?
6275                                 TXDDS_TABLE_SZ + 2 : TXDDS_TABLE_SZ + 4;
6276                 } else
6277                         cp->h1_val = H1_FORCE_VAL;
6278
6279                 /* Avoid writes to chip for mini_init */
6280                 if (!qib_mini_init)
6281                         write_7322_init_portregs(ppd);
6282
6283                 init_timer(&cp->chase_timer);
6284                 cp->chase_timer.function = reenable_chase;
6285                 cp->chase_timer.data = (unsigned long)ppd;
6286
6287                 ppd++;
6288         }
6289
6290         dd->rcvhdrentsize = qib_rcvhdrentsize ?
6291                 qib_rcvhdrentsize : QIB_RCVHDR_ENTSIZE;
6292         dd->rcvhdrsize = qib_rcvhdrsize ?
6293                 qib_rcvhdrsize : QIB_DFLT_RCVHDRSIZE;
6294         dd->rhf_offset = dd->rcvhdrentsize - sizeof(u64) / sizeof(u32);
6295
6296         /* we always allocate at least 2048 bytes for eager buffers */
6297         dd->rcvegrbufsize = max(mtu, 2048);
6298         BUG_ON(!is_power_of_2(dd->rcvegrbufsize));
6299         dd->rcvegrbufsize_shift = ilog2(dd->rcvegrbufsize);
6300
6301         qib_7322_tidtemplate(dd);
6302
6303         /*
6304          * We can request a receive interrupt for 1 or
6305          * more packets from current offset.
6306          */
6307         dd->rhdrhead_intr_off =
6308                 (u64) rcv_int_count << IBA7322_HDRHEAD_PKTINT_SHIFT;
6309
6310         /* setup the stats timer; the add_timer is done at end of init */
6311         init_timer(&dd->stats_timer);
6312         dd->stats_timer.function = qib_get_7322_faststats;
6313         dd->stats_timer.data = (unsigned long) dd;
6314
6315         dd->ureg_align = 0x10000;  /* 64KB alignment */
6316
6317         dd->piosize2kmax_dwords = dd->piosize2k >> 2;
6318
6319         qib_7322_config_ctxts(dd);
6320         qib_set_ctxtcnt(dd);
6321
6322         if (qib_wc_pat) {
6323                 resource_size_t vl15off;
6324                 /*
6325                  * We do not set WC on the VL15 buffers to avoid
6326                  * a rare problem with unaligned writes from
6327                  * interrupt-flushed store buffers, so we need
6328                  * to map those separately here.  We can't solve
6329                  * this for the rarely used mtrr case.
6330                  */
6331                 ret = init_chip_wc_pat(dd, 0);
6332                 if (ret)
6333                         goto bail;
6334
6335                 /* vl15 buffers start just after the 4k buffers */
6336                 vl15off = dd->physaddr + (dd->piobufbase >> 32) +
6337                         dd->piobcnt4k * dd->align4k;
6338                 dd->piovl15base = ioremap_nocache(vl15off,
6339                                                   NUM_VL15_BUFS * dd->align4k);
6340                 if (!dd->piovl15base)
6341                         goto bail;
6342         }
6343         qib_7322_set_baseaddrs(dd); /* set chip access pointers now */
6344
6345         ret = 0;
6346         if (qib_mini_init)
6347                 goto bail;
6348         if (!dd->num_pports) {
6349                 qib_dev_err(dd, "No ports enabled, giving up initialization\n");
6350                 goto bail; /* no error, so can still figure out why err */
6351         }
6352
6353         write_7322_initregs(dd);
6354         ret = qib_create_ctxts(dd);
6355         init_7322_cntrnames(dd);
6356
6357         updthresh = 8U; /* update threshold */
6358
6359         /* use all of 4KB buffers for the kernel SDMA, zero if !SDMA.
6360          * reserve the update threshold amount for other kernel use, such
6361          * as sending SMI, MAD, and ACKs, or 3, whichever is greater,
6362          * unless we aren't enabling SDMA, in which case we want to use
6363          * all the 4k bufs for the kernel.
6364          * if this was less than the update threshold, we could wait
6365          * a long time for an update.  Coded this way because we
6366          * sometimes change the update threshold for various reasons,
6367          * and we want this to remain robust.
6368          */
6369         if (dd->flags & QIB_HAS_SEND_DMA) {
6370                 dd->cspec->sdmabufcnt = dd->piobcnt4k;
6371                 sbufs = updthresh > 3 ? updthresh : 3;
6372         } else {
6373                 dd->cspec->sdmabufcnt = 0;
6374                 sbufs = dd->piobcnt4k;
6375         }
6376         dd->cspec->lastbuf_for_pio = dd->piobcnt2k + dd->piobcnt4k -
6377                 dd->cspec->sdmabufcnt;
6378         dd->lastctxt_piobuf = dd->cspec->lastbuf_for_pio - sbufs;
6379         dd->cspec->lastbuf_for_pio--; /* range is <= , not < */
6380         dd->last_pio = dd->cspec->lastbuf_for_pio;
6381         dd->pbufsctxt = (dd->cfgctxts > dd->first_user_ctxt) ?
6382                 dd->lastctxt_piobuf / (dd->cfgctxts - dd->first_user_ctxt) : 0;
6383
6384         /*
6385          * If we have 16 user contexts, we will have 7 sbufs
6386          * per context, so reduce the update threshold to match.  We
6387          * want to update before we actually run out, at low pbufs/ctxt
6388          * so give ourselves some margin.
6389          */
6390         if (dd->pbufsctxt >= 2 && dd->pbufsctxt - 2 < updthresh)
6391                 updthresh = dd->pbufsctxt - 2;
6392         dd->cspec->updthresh_dflt = updthresh;
6393         dd->cspec->updthresh = updthresh;
6394
6395         /* before full enable, no interrupts, no locking needed */
6396         dd->sendctrl |= ((updthresh & SYM_RMASK(SendCtrl, AvailUpdThld))
6397                              << SYM_LSB(SendCtrl, AvailUpdThld)) |
6398                         SYM_MASK(SendCtrl, SendBufAvailPad64Byte);
6399
6400         dd->psxmitwait_supported = 1;
6401         dd->psxmitwait_check_rate = QIB_7322_PSXMITWAIT_CHECK_RATE;
6402 bail:
6403         if (!dd->ctxtcnt)
6404                 dd->ctxtcnt = 1; /* for other initialization code */
6405
6406         return ret;
6407 }
6408
6409 static u32 __iomem *qib_7322_getsendbuf(struct qib_pportdata *ppd, u64 pbc,
6410                                         u32 *pbufnum)
6411 {
6412         u32 first, last, plen = pbc & QIB_PBC_LENGTH_MASK;
6413         struct qib_devdata *dd = ppd->dd;
6414
6415         /* last is same for 2k and 4k, because we use 4k if all 2k busy */
6416         if (pbc & PBC_7322_VL15_SEND) {
6417                 first = dd->piobcnt2k + dd->piobcnt4k + ppd->hw_pidx;
6418                 last = first;
6419         } else {
6420                 if ((plen + 1) > dd->piosize2kmax_dwords)
6421                         first = dd->piobcnt2k;
6422                 else
6423                         first = 0;
6424                 last = dd->cspec->lastbuf_for_pio;
6425         }
6426         return qib_getsendbuf_range(dd, pbufnum, first, last);
6427 }
6428
6429 static void qib_set_cntr_7322_sample(struct qib_pportdata *ppd, u32 intv,
6430                                      u32 start)
6431 {
6432         qib_write_kreg_port(ppd, krp_psinterval, intv);
6433         qib_write_kreg_port(ppd, krp_psstart, start);
6434 }
6435
6436 /*
6437  * Must be called with sdma_lock held, or before init finished.
6438  */
6439 static void qib_sdma_set_7322_desc_cnt(struct qib_pportdata *ppd, unsigned cnt)
6440 {
6441         qib_write_kreg_port(ppd, krp_senddmadesccnt, cnt);
6442 }
6443
6444 static struct sdma_set_state_action sdma_7322_action_table[] = {
6445         [qib_sdma_state_s00_hw_down] = {
6446                 .go_s99_running_tofalse = 1,
6447                 .op_enable = 0,
6448                 .op_intenable = 0,
6449                 .op_halt = 0,
6450                 .op_drain = 0,
6451         },
6452         [qib_sdma_state_s10_hw_start_up_wait] = {
6453                 .op_enable = 0,
6454                 .op_intenable = 1,
6455                 .op_halt = 1,
6456                 .op_drain = 0,
6457         },
6458         [qib_sdma_state_s20_idle] = {
6459                 .op_enable = 1,
6460                 .op_intenable = 1,
6461                 .op_halt = 1,
6462                 .op_drain = 0,
6463         },
6464         [qib_sdma_state_s30_sw_clean_up_wait] = {
6465                 .op_enable = 0,
6466                 .op_intenable = 1,
6467                 .op_halt = 1,
6468                 .op_drain = 0,
6469         },
6470         [qib_sdma_state_s40_hw_clean_up_wait] = {
6471                 .op_enable = 1,
6472                 .op_intenable = 1,
6473                 .op_halt = 1,
6474                 .op_drain = 0,
6475         },
6476         [qib_sdma_state_s50_hw_halt_wait] = {
6477                 .op_enable = 1,
6478                 .op_intenable = 1,
6479                 .op_halt = 1,
6480                 .op_drain = 1,
6481         },
6482         [qib_sdma_state_s99_running] = {
6483                 .op_enable = 1,
6484                 .op_intenable = 1,
6485                 .op_halt = 0,
6486                 .op_drain = 0,
6487                 .go_s99_running_totrue = 1,
6488         },
6489 };
6490
6491 static void qib_7322_sdma_init_early(struct qib_pportdata *ppd)
6492 {
6493         ppd->sdma_state.set_state_action = sdma_7322_action_table;
6494 }
6495
6496 static int init_sdma_7322_regs(struct qib_pportdata *ppd)
6497 {
6498         struct qib_devdata *dd = ppd->dd;
6499         unsigned lastbuf, erstbuf;
6500         u64 senddmabufmask[3] = { 0 };
6501         int n, ret = 0;
6502
6503         qib_write_kreg_port(ppd, krp_senddmabase, ppd->sdma_descq_phys);
6504         qib_sdma_7322_setlengen(ppd);
6505         qib_sdma_update_7322_tail(ppd, 0); /* Set SendDmaTail */
6506         qib_write_kreg_port(ppd, krp_senddmareloadcnt, sdma_idle_cnt);
6507         qib_write_kreg_port(ppd, krp_senddmadesccnt, 0);
6508         qib_write_kreg_port(ppd, krp_senddmaheadaddr, ppd->sdma_head_phys);
6509
6510         if (dd->num_pports)
6511                 n = dd->cspec->sdmabufcnt / dd->num_pports; /* no remainder */
6512         else
6513                 n = dd->cspec->sdmabufcnt; /* failsafe for init */
6514         erstbuf = (dd->piobcnt2k + dd->piobcnt4k) -
6515                 ((dd->num_pports == 1 || ppd->port == 2) ? n :
6516                 dd->cspec->sdmabufcnt);
6517         lastbuf = erstbuf + n;
6518
6519         ppd->sdma_state.first_sendbuf = erstbuf;
6520         ppd->sdma_state.last_sendbuf = lastbuf;
6521         for (; erstbuf < lastbuf; ++erstbuf) {
6522                 unsigned word = erstbuf / BITS_PER_LONG;
6523                 unsigned bit = erstbuf & (BITS_PER_LONG - 1);
6524
6525                 BUG_ON(word >= 3);
6526                 senddmabufmask[word] |= 1ULL << bit;
6527         }
6528         qib_write_kreg_port(ppd, krp_senddmabufmask0, senddmabufmask[0]);
6529         qib_write_kreg_port(ppd, krp_senddmabufmask1, senddmabufmask[1]);
6530         qib_write_kreg_port(ppd, krp_senddmabufmask2, senddmabufmask[2]);
6531         return ret;
6532 }
6533
6534 /* sdma_lock must be held */
6535 static u16 qib_sdma_7322_gethead(struct qib_pportdata *ppd)
6536 {
6537         struct qib_devdata *dd = ppd->dd;
6538         int sane;
6539         int use_dmahead;
6540         u16 swhead;
6541         u16 swtail;
6542         u16 cnt;
6543         u16 hwhead;
6544
6545         use_dmahead = __qib_sdma_running(ppd) &&
6546                 (dd->flags & QIB_HAS_SDMA_TIMEOUT);
6547 retry:
6548         hwhead = use_dmahead ?
6549                 (u16) le64_to_cpu(*ppd->sdma_head_dma) :
6550                 (u16) qib_read_kreg_port(ppd, krp_senddmahead);
6551
6552         swhead = ppd->sdma_descq_head;
6553         swtail = ppd->sdma_descq_tail;
6554         cnt = ppd->sdma_descq_cnt;
6555
6556         if (swhead < swtail)
6557                 /* not wrapped */
6558                 sane = (hwhead >= swhead) & (hwhead <= swtail);
6559         else if (swhead > swtail)
6560                 /* wrapped around */
6561                 sane = ((hwhead >= swhead) && (hwhead < cnt)) ||
6562                         (hwhead <= swtail);
6563         else
6564                 /* empty */
6565                 sane = (hwhead == swhead);
6566
6567         if (unlikely(!sane)) {
6568                 if (use_dmahead) {
6569                         /* try one more time, directly from the register */
6570                         use_dmahead = 0;
6571                         goto retry;
6572                 }
6573                 /* proceed as if no progress */
6574                 hwhead = swhead;
6575         }
6576
6577         return hwhead;
6578 }
6579
6580 static int qib_sdma_7322_busy(struct qib_pportdata *ppd)
6581 {
6582         u64 hwstatus = qib_read_kreg_port(ppd, krp_senddmastatus);
6583
6584         return (hwstatus & SYM_MASK(SendDmaStatus_0, ScoreBoardDrainInProg)) ||
6585                (hwstatus & SYM_MASK(SendDmaStatus_0, HaltInProg)) ||
6586                !(hwstatus & SYM_MASK(SendDmaStatus_0, InternalSDmaHalt)) ||
6587                !(hwstatus & SYM_MASK(SendDmaStatus_0, ScbEmpty));
6588 }
6589
6590 /*
6591  * Compute the amount of delay before sending the next packet if the
6592  * port's send rate differs from the static rate set for the QP.
6593  * The delay affects the next packet and the amount of the delay is
6594  * based on the length of the this packet.
6595  */
6596 static u32 qib_7322_setpbc_control(struct qib_pportdata *ppd, u32 plen,
6597                                    u8 srate, u8 vl)
6598 {
6599         u8 snd_mult = ppd->delay_mult;
6600         u8 rcv_mult = ib_rate_to_delay[srate];
6601         u32 ret;
6602
6603         ret = rcv_mult > snd_mult ? ((plen + 1) >> 1) * snd_mult : 0;
6604
6605         /* Indicate VL15, else set the VL in the control word */
6606         if (vl == 15)
6607                 ret |= PBC_7322_VL15_SEND_CTRL;
6608         else
6609                 ret |= vl << PBC_VL_NUM_LSB;
6610         ret |= ((u32)(ppd->hw_pidx)) << PBC_PORT_SEL_LSB;
6611
6612         return ret;
6613 }
6614
6615 /*
6616  * Enable the per-port VL15 send buffers for use.
6617  * They follow the rest of the buffers, without a config parameter.
6618  * This was in initregs, but that is done before the shadow
6619  * is set up, and this has to be done after the shadow is
6620  * set up.
6621  */
6622 static void qib_7322_initvl15_bufs(struct qib_devdata *dd)
6623 {
6624         unsigned vl15bufs;
6625
6626         vl15bufs = dd->piobcnt2k + dd->piobcnt4k;
6627         qib_chg_pioavailkernel(dd, vl15bufs, NUM_VL15_BUFS,
6628                                TXCHK_CHG_TYPE_KERN, NULL);
6629 }
6630
6631 static void qib_7322_init_ctxt(struct qib_ctxtdata *rcd)
6632 {
6633         if (rcd->ctxt < NUM_IB_PORTS) {
6634                 if (rcd->dd->num_pports > 1) {
6635                         rcd->rcvegrcnt = KCTXT0_EGRCNT / 2;
6636                         rcd->rcvegr_tid_base = rcd->ctxt ? rcd->rcvegrcnt : 0;
6637                 } else {
6638                         rcd->rcvegrcnt = KCTXT0_EGRCNT;
6639                         rcd->rcvegr_tid_base = 0;
6640                 }
6641         } else {
6642                 rcd->rcvegrcnt = rcd->dd->cspec->rcvegrcnt;
6643                 rcd->rcvegr_tid_base = KCTXT0_EGRCNT +
6644                         (rcd->ctxt - NUM_IB_PORTS) * rcd->rcvegrcnt;
6645         }
6646 }
6647
6648 #define QTXSLEEPS 5000
6649 static void qib_7322_txchk_change(struct qib_devdata *dd, u32 start,
6650                                   u32 len, u32 which, struct qib_ctxtdata *rcd)
6651 {
6652         int i;
6653         const int last = start + len - 1;
6654         const int lastr = last / BITS_PER_LONG;
6655         u32 sleeps = 0;
6656         int wait = rcd != NULL;
6657         unsigned long flags;
6658
6659         while (wait) {
6660                 unsigned long shadow;
6661                 int cstart, previ = -1;
6662
6663                 /*
6664                  * when flipping from kernel to user, we can't change
6665                  * the checking type if the buffer is allocated to the
6666                  * driver.   It's OK the other direction, because it's
6667                  * from close, and we have just disarm'ed all the
6668                  * buffers.  All the kernel to kernel changes are also
6669                  * OK.
6670                  */
6671                 for (cstart = start; cstart <= last; cstart++) {
6672                         i = ((2 * cstart) + QLOGIC_IB_SENDPIOAVAIL_BUSY_SHIFT)
6673                                 / BITS_PER_LONG;
6674                         if (i != previ) {
6675                                 shadow = (unsigned long)
6676                                         le64_to_cpu(dd->pioavailregs_dma[i]);
6677                                 previ = i;
6678                         }
6679                         if (test_bit(((2 * cstart) +
6680                                       QLOGIC_IB_SENDPIOAVAIL_BUSY_SHIFT)
6681                                      % BITS_PER_LONG, &shadow))
6682                                 break;
6683                 }
6684
6685                 if (cstart > last)
6686                         break;
6687
6688                 if (sleeps == QTXSLEEPS)
6689                         break;
6690                 /* make sure we see an updated copy next time around */
6691                 sendctrl_7322_mod(dd->pport, QIB_SENDCTRL_AVAIL_BLIP);
6692                 sleeps++;
6693                 msleep(20);
6694         }
6695
6696         switch (which) {
6697         case TXCHK_CHG_TYPE_DIS1:
6698                 /*
6699                  * disable checking on a range; used by diags; just
6700                  * one buffer, but still written generically
6701                  */
6702                 for (i = start; i <= last; i++)
6703                         clear_bit(i, dd->cspec->sendchkenable);
6704                 break;
6705
6706         case TXCHK_CHG_TYPE_ENAB1:
6707                 /*
6708                  * (re)enable checking on a range; used by diags; just
6709                  * one buffer, but still written generically; read
6710                  * scratch to be sure buffer actually triggered, not
6711                  * just flushed from processor.
6712                  */
6713                 qib_read_kreg32(dd, kr_scratch);
6714                 for (i = start; i <= last; i++)
6715                         set_bit(i, dd->cspec->sendchkenable);
6716                 break;
6717
6718         case TXCHK_CHG_TYPE_KERN:
6719                 /* usable by kernel */
6720                 for (i = start; i <= last; i++) {
6721                         set_bit(i, dd->cspec->sendibchk);
6722                         clear_bit(i, dd->cspec->sendgrhchk);
6723                 }
6724                 spin_lock_irqsave(&dd->uctxt_lock, flags);
6725                 /* see if we need to raise avail update threshold */
6726                 for (i = dd->first_user_ctxt;
6727                      dd->cspec->updthresh != dd->cspec->updthresh_dflt
6728                      && i < dd->cfgctxts; i++)
6729                         if (dd->rcd[i] && dd->rcd[i]->subctxt_cnt &&
6730                            ((dd->rcd[i]->piocnt / dd->rcd[i]->subctxt_cnt) - 1)
6731                            < dd->cspec->updthresh_dflt)
6732                                 break;
6733                 spin_unlock_irqrestore(&dd->uctxt_lock, flags);
6734                 if (i == dd->cfgctxts) {
6735                         spin_lock_irqsave(&dd->sendctrl_lock, flags);
6736                         dd->cspec->updthresh = dd->cspec->updthresh_dflt;
6737                         dd->sendctrl &= ~SYM_MASK(SendCtrl, AvailUpdThld);
6738                         dd->sendctrl |= (dd->cspec->updthresh &
6739                                          SYM_RMASK(SendCtrl, AvailUpdThld)) <<
6740                                            SYM_LSB(SendCtrl, AvailUpdThld);
6741                         spin_unlock_irqrestore(&dd->sendctrl_lock, flags);
6742                         sendctrl_7322_mod(dd->pport, QIB_SENDCTRL_AVAIL_BLIP);
6743                 }
6744                 break;
6745
6746         case TXCHK_CHG_TYPE_USER:
6747                 /* for user process */
6748                 for (i = start; i <= last; i++) {
6749                         clear_bit(i, dd->cspec->sendibchk);
6750                         set_bit(i, dd->cspec->sendgrhchk);
6751                 }
6752                 spin_lock_irqsave(&dd->sendctrl_lock, flags);
6753                 if (rcd && rcd->subctxt_cnt && ((rcd->piocnt
6754                         / rcd->subctxt_cnt) - 1) < dd->cspec->updthresh) {
6755                         dd->cspec->updthresh = (rcd->piocnt /
6756                                                 rcd->subctxt_cnt) - 1;
6757                         dd->sendctrl &= ~SYM_MASK(SendCtrl, AvailUpdThld);
6758                         dd->sendctrl |= (dd->cspec->updthresh &
6759                                         SYM_RMASK(SendCtrl, AvailUpdThld))
6760                                         << SYM_LSB(SendCtrl, AvailUpdThld);
6761                         spin_unlock_irqrestore(&dd->sendctrl_lock, flags);
6762                         sendctrl_7322_mod(dd->pport, QIB_SENDCTRL_AVAIL_BLIP);
6763                 } else
6764                         spin_unlock_irqrestore(&dd->sendctrl_lock, flags);
6765                 break;
6766
6767         default:
6768                 break;
6769         }
6770
6771         for (i = start / BITS_PER_LONG; which >= 2 && i <= lastr; ++i)
6772                 qib_write_kreg(dd, kr_sendcheckmask + i,
6773                                dd->cspec->sendchkenable[i]);
6774
6775         for (i = start / BITS_PER_LONG; which < 2 && i <= lastr; ++i) {
6776                 qib_write_kreg(dd, kr_sendgrhcheckmask + i,
6777                                dd->cspec->sendgrhchk[i]);
6778                 qib_write_kreg(dd, kr_sendibpktmask + i,
6779                                dd->cspec->sendibchk[i]);
6780         }
6781
6782         /*
6783          * Be sure whatever we did was seen by the chip and acted upon,
6784          * before we return.  Mostly important for which >= 2.
6785          */
6786         qib_read_kreg32(dd, kr_scratch);
6787 }
6788
6789
6790 /* useful for trigger analyzers, etc. */
6791 static void writescratch(struct qib_devdata *dd, u32 val)
6792 {
6793         qib_write_kreg(dd, kr_scratch, val);
6794 }
6795
6796 /* Dummy for now, use chip regs soon */
6797 static int qib_7322_tempsense_rd(struct qib_devdata *dd, int regnum)
6798 {
6799         return -ENXIO;
6800 }
6801
6802 /**
6803  * qib_init_iba7322_funcs - set up the chip-specific function pointers
6804  * @dev: the pci_dev for qlogic_ib device
6805  * @ent: pci_device_id struct for this dev
6806  *
6807  * Also allocates, inits, and returns the devdata struct for this
6808  * device instance
6809  *
6810  * This is global, and is called directly at init to set up the
6811  * chip-specific function pointers for later use.
6812  */
6813 struct qib_devdata *qib_init_iba7322_funcs(struct pci_dev *pdev,
6814                                            const struct pci_device_id *ent)
6815 {
6816         struct qib_devdata *dd;
6817         int ret, i;
6818         u32 tabsize, actual_cnt = 0;
6819
6820         dd = qib_alloc_devdata(pdev,
6821                 NUM_IB_PORTS * sizeof(struct qib_pportdata) +
6822                 sizeof(struct qib_chip_specific) +
6823                 NUM_IB_PORTS * sizeof(struct qib_chippport_specific));
6824         if (IS_ERR(dd))
6825                 goto bail;
6826
6827         dd->f_bringup_serdes    = qib_7322_bringup_serdes;
6828         dd->f_cleanup           = qib_setup_7322_cleanup;
6829         dd->f_clear_tids        = qib_7322_clear_tids;
6830         dd->f_free_irq          = qib_7322_free_irq;
6831         dd->f_get_base_info     = qib_7322_get_base_info;
6832         dd->f_get_msgheader     = qib_7322_get_msgheader;
6833         dd->f_getsendbuf        = qib_7322_getsendbuf;
6834         dd->f_gpio_mod          = gpio_7322_mod;
6835         dd->f_eeprom_wen        = qib_7322_eeprom_wen;
6836         dd->f_hdrqempty         = qib_7322_hdrqempty;
6837         dd->f_ib_updown         = qib_7322_ib_updown;
6838         dd->f_init_ctxt         = qib_7322_init_ctxt;
6839         dd->f_initvl15_bufs     = qib_7322_initvl15_bufs;
6840         dd->f_intr_fallback     = qib_7322_intr_fallback;
6841         dd->f_late_initreg      = qib_late_7322_initreg;
6842         dd->f_setpbc_control    = qib_7322_setpbc_control;
6843         dd->f_portcntr          = qib_portcntr_7322;
6844         dd->f_put_tid           = qib_7322_put_tid;
6845         dd->f_quiet_serdes      = qib_7322_mini_quiet_serdes;
6846         dd->f_rcvctrl           = rcvctrl_7322_mod;
6847         dd->f_read_cntrs        = qib_read_7322cntrs;
6848         dd->f_read_portcntrs    = qib_read_7322portcntrs;
6849         dd->f_reset             = qib_do_7322_reset;
6850         dd->f_init_sdma_regs    = init_sdma_7322_regs;
6851         dd->f_sdma_busy         = qib_sdma_7322_busy;
6852         dd->f_sdma_gethead      = qib_sdma_7322_gethead;
6853         dd->f_sdma_sendctrl     = qib_7322_sdma_sendctrl;
6854         dd->f_sdma_set_desc_cnt = qib_sdma_set_7322_desc_cnt;
6855         dd->f_sdma_update_tail  = qib_sdma_update_7322_tail;
6856         dd->f_sendctrl          = sendctrl_7322_mod;
6857         dd->f_set_armlaunch     = qib_set_7322_armlaunch;
6858         dd->f_set_cntr_sample   = qib_set_cntr_7322_sample;
6859         dd->f_iblink_state      = qib_7322_iblink_state;
6860         dd->f_ibphys_portstate  = qib_7322_phys_portstate;
6861         dd->f_get_ib_cfg        = qib_7322_get_ib_cfg;
6862         dd->f_set_ib_cfg        = qib_7322_set_ib_cfg;
6863         dd->f_set_ib_loopback   = qib_7322_set_loopback;
6864         dd->f_get_ib_table      = qib_7322_get_ib_table;
6865         dd->f_set_ib_table      = qib_7322_set_ib_table;
6866         dd->f_set_intr_state    = qib_7322_set_intr_state;
6867         dd->f_setextled         = qib_setup_7322_setextled;
6868         dd->f_txchk_change      = qib_7322_txchk_change;
6869         dd->f_update_usrhead    = qib_update_7322_usrhead;
6870         dd->f_wantpiobuf_intr   = qib_wantpiobuf_7322_intr;
6871         dd->f_xgxs_reset        = qib_7322_mini_pcs_reset;
6872         dd->f_sdma_hw_clean_up  = qib_7322_sdma_hw_clean_up;
6873         dd->f_sdma_hw_start_up  = qib_7322_sdma_hw_start_up;
6874         dd->f_sdma_init_early   = qib_7322_sdma_init_early;
6875         dd->f_writescratch      = writescratch;
6876         dd->f_tempsense_rd      = qib_7322_tempsense_rd;
6877         /*
6878          * Do remaining PCIe setup and save PCIe values in dd.
6879          * Any error printing is already done by the init code.
6880          * On return, we have the chip mapped, but chip registers
6881          * are not set up until start of qib_init_7322_variables.
6882          */
6883         ret = qib_pcie_ddinit(dd, pdev, ent);
6884         if (ret < 0)
6885                 goto bail_free;
6886
6887         /* initialize chip-specific variables */
6888         ret = qib_init_7322_variables(dd);
6889         if (ret)
6890                 goto bail_cleanup;
6891
6892         if (qib_mini_init || !dd->num_pports)
6893                 goto bail;
6894
6895         /*
6896          * Determine number of vectors we want; depends on port count
6897          * and number of configured kernel receive queues actually used.
6898          * Should also depend on whether sdma is enabled or not, but
6899          * that's such a rare testing case it's not worth worrying about.
6900          */
6901         tabsize = dd->first_user_ctxt + ARRAY_SIZE(irq_table);
6902         for (i = 0; i < tabsize; i++)
6903                 if ((i < ARRAY_SIZE(irq_table) &&
6904                      irq_table[i].port <= dd->num_pports) ||
6905                     (i >= ARRAY_SIZE(irq_table) &&
6906                      dd->rcd[i - ARRAY_SIZE(irq_table)]))
6907                         actual_cnt++;
6908         /* reduce by ctxt's < 2 */
6909         if (qib_krcvq01_no_msi)
6910                 actual_cnt -= dd->num_pports;
6911
6912         tabsize = actual_cnt;
6913         dd->cspec->msix_entries = kmalloc(tabsize *
6914                         sizeof(struct qib_msix_entry), GFP_KERNEL);
6915         if (!dd->cspec->msix_entries) {
6916                 qib_dev_err(dd, "No memory for MSIx table\n");
6917                 tabsize = 0;
6918         }
6919         for (i = 0; i < tabsize; i++)
6920                 dd->cspec->msix_entries[i].msix.entry = i;
6921
6922         if (qib_pcie_params(dd, 8, &tabsize, dd->cspec->msix_entries))
6923                 qib_dev_err(dd, "Failed to setup PCIe or interrupts; "
6924                             "continuing anyway\n");
6925         /* may be less than we wanted, if not enough available */
6926         dd->cspec->num_msix_entries = tabsize;
6927
6928         /* setup interrupt handler */
6929         qib_setup_7322_interrupt(dd, 1);
6930
6931         /* clear diagctrl register, in case diags were running and crashed */
6932         qib_write_kreg(dd, kr_hwdiagctrl, 0);
6933
6934         goto bail;
6935
6936 bail_cleanup:
6937         qib_pcie_ddcleanup(dd);
6938 bail_free:
6939         qib_free_devdata(dd);
6940         dd = ERR_PTR(ret);
6941 bail:
6942         return dd;
6943 }
6944
6945 /*
6946  * Set the table entry at the specified index from the table specifed.
6947  * There are 3 * TXDDS_TABLE_SZ entries in all per port, with the first
6948  * TXDDS_TABLE_SZ for SDR, the next for DDR, and the last for QDR.
6949  * 'idx' below addresses the correct entry, while its 4 LSBs select the
6950  * corresponding entry (one of TXDDS_TABLE_SZ) from the selected table.
6951  */
6952 #define DDS_ENT_AMP_LSB 14
6953 #define DDS_ENT_MAIN_LSB 9
6954 #define DDS_ENT_POST_LSB 5
6955 #define DDS_ENT_PRE_XTRA_LSB 3
6956 #define DDS_ENT_PRE_LSB 0
6957
6958 /*
6959  * Set one entry in the TxDDS table for spec'd port
6960  * ridx picks one of the entries, while tp points
6961  * to the appropriate table entry.
6962  */
6963 static void set_txdds(struct qib_pportdata *ppd, int ridx,
6964                       const struct txdds_ent *tp)
6965 {
6966         struct qib_devdata *dd = ppd->dd;
6967         u32 pack_ent;
6968         int regidx;
6969
6970         /* Get correct offset in chip-space, and in source table */
6971         regidx = KREG_IBPORT_IDX(IBSD_DDS_MAP_TABLE) + ridx;
6972         /*
6973          * We do not use qib_write_kreg_port() because it was intended
6974          * only for registers in the lower "port specific" pages.
6975          * So do index calculation  by hand.
6976          */
6977         if (ppd->hw_pidx)
6978                 regidx += (dd->palign / sizeof(u64));
6979
6980         pack_ent = tp->amp << DDS_ENT_AMP_LSB;
6981         pack_ent |= tp->main << DDS_ENT_MAIN_LSB;
6982         pack_ent |= tp->pre << DDS_ENT_PRE_LSB;
6983         pack_ent |= tp->post << DDS_ENT_POST_LSB;
6984         qib_write_kreg(dd, regidx, pack_ent);
6985         /* Prevent back-to-back writes by hitting scratch */
6986         qib_write_kreg(ppd->dd, kr_scratch, 0);
6987 }
6988
6989 static const struct vendor_txdds_ent vendor_txdds[] = {
6990         { /* Amphenol 1m 30awg NoEq */
6991                 { 0x41, 0x50, 0x48 }, "584470002       ",
6992                 { 10,  0,  0,  5 }, { 10,  0,  0,  9 }, {  7,  1,  0, 13 },
6993         },
6994         { /* Amphenol 3m 28awg NoEq */
6995                 { 0x41, 0x50, 0x48 }, "584470004       ",
6996                 {  0,  0,  0,  8 }, {  0,  0,  0, 11 }, {  0,  1,  7, 15 },
6997         },
6998         { /* Finisar 3m OM2 Optical */
6999                 { 0x00, 0x90, 0x65 }, "FCBG410QB1C03-QL",
7000                 {  0,  0,  0,  3 }, {  0,  0,  0,  4 }, {  0,  0,  0, 13 },
7001         },
7002         { /* Finisar 30m OM2 Optical */
7003                 { 0x00, 0x90, 0x65 }, "FCBG410QB1C30-QL",
7004                 {  0,  0,  0,  1 }, {  0,  0,  0,  5 }, {  0,  0,  0, 11 },
7005         },
7006         { /* Finisar Default OM2 Optical */
7007                 { 0x00, 0x90, 0x65 }, NULL,
7008                 {  0,  0,  0,  2 }, {  0,  0,  0,  5 }, {  0,  0,  0, 12 },
7009         },
7010         { /* Gore 1m 30awg NoEq */
7011                 { 0x00, 0x21, 0x77 }, "QSN3300-1       ",
7012                 {  0,  0,  0,  6 }, {  0,  0,  0,  9 }, {  0,  1,  0, 15 },
7013         },
7014         { /* Gore 2m 30awg NoEq */
7015                 { 0x00, 0x21, 0x77 }, "QSN3300-2       ",
7016                 {  0,  0,  0,  8 }, {  0,  0,  0, 10 }, {  0,  1,  7, 15 },
7017         },
7018         { /* Gore 1m 28awg NoEq */
7019                 { 0x00, 0x21, 0x77 }, "QSN3800-1       ",
7020                 {  0,  0,  0,  6 }, {  0,  0,  0,  8 }, {  0,  1,  0, 15 },
7021         },
7022         { /* Gore 3m 28awg NoEq */
7023                 { 0x00, 0x21, 0x77 }, "QSN3800-3       ",
7024                 {  0,  0,  0,  9 }, {  0,  0,  0, 13 }, {  0,  1,  7, 15 },
7025         },
7026         { /* Gore 5m 24awg Eq */
7027                 { 0x00, 0x21, 0x77 }, "QSN7000-5       ",
7028                 {  0,  0,  0,  7 }, {  0,  0,  0,  9 }, {  0,  1,  3, 15 },
7029         },
7030         { /* Gore 7m 24awg Eq */
7031                 { 0x00, 0x21, 0x77 }, "QSN7000-7       ",
7032                 {  0,  0,  0,  9 }, {  0,  0,  0, 11 }, {  0,  2,  6, 15 },
7033         },
7034         { /* Gore 5m 26awg Eq */
7035                 { 0x00, 0x21, 0x77 }, "QSN7600-5       ",
7036                 {  0,  0,  0,  8 }, {  0,  0,  0, 11 }, {  0,  1,  9, 13 },
7037         },
7038         { /* Gore 7m 26awg Eq */
7039                 { 0x00, 0x21, 0x77 }, "QSN7600-7       ",
7040                 {  0,  0,  0,  8 }, {  0,  0,  0, 11 }, {  10,  1,  8, 15 },
7041         },
7042         { /* Intersil 12m 24awg Active */
7043                 { 0x00, 0x30, 0xB4 }, "QLX4000CQSFP1224",
7044                 {  0,  0,  0,  2 }, {  0,  0,  0,  5 }, {  0,  3,  0,  9 },
7045         },
7046         { /* Intersil 10m 28awg Active */
7047                 { 0x00, 0x30, 0xB4 }, "QLX4000CQSFP1028",
7048                 {  0,  0,  0,  6 }, {  0,  0,  0,  4 }, {  0,  2,  0,  2 },
7049         },
7050         { /* Intersil 7m 30awg Active */
7051                 { 0x00, 0x30, 0xB4 }, "QLX4000CQSFP0730",
7052                 {  0,  0,  0,  6 }, {  0,  0,  0,  4 }, {  0,  1,  0,  3 },
7053         },
7054         { /* Intersil 5m 32awg Active */
7055                 { 0x00, 0x30, 0xB4 }, "QLX4000CQSFP0532",
7056                 {  0,  0,  0,  6 }, {  0,  0,  0,  6 }, {  0,  2,  0,  8 },
7057         },
7058         { /* Intersil Default Active */
7059                 { 0x00, 0x30, 0xB4 }, NULL,
7060                 {  0,  0,  0,  6 }, {  0,  0,  0,  5 }, {  0,  2,  0,  5 },
7061         },
7062         { /* Luxtera 20m Active Optical */
7063                 { 0x00, 0x25, 0x63 }, NULL,
7064                 {  0,  0,  0,  5 }, {  0,  0,  0,  8 }, {  0,  2,  0,  12 },
7065         },
7066         { /* Molex 1M Cu loopback */
7067                 { 0x00, 0x09, 0x3A }, "74763-0025      ",
7068                 {  2,  2,  6, 15 }, {  2,  2,  6, 15 }, {  2,  2,  6, 15 },
7069         },
7070         { /* Molex 2m 28awg NoEq */
7071                 { 0x00, 0x09, 0x3A }, "74757-2201      ",
7072                 {  0,  0,  0,  6 }, {  0,  0,  0,  9 }, {  0,  1,  1, 15 },
7073         },
7074 };
7075
7076 static const struct txdds_ent txdds_sdr[TXDDS_TABLE_SZ] = {
7077         /* amp, pre, main, post */
7078         {  2, 2, 15,  6 },      /* Loopback */
7079         {  0, 0,  0,  1 },      /*  2 dB */
7080         {  0, 0,  0,  2 },      /*  3 dB */
7081         {  0, 0,  0,  3 },      /*  4 dB */
7082         {  0, 0,  0,  4 },      /*  5 dB */
7083         {  0, 0,  0,  5 },      /*  6 dB */
7084         {  0, 0,  0,  6 },      /*  7 dB */
7085         {  0, 0,  0,  7 },      /*  8 dB */
7086         {  0, 0,  0,  8 },      /*  9 dB */
7087         {  0, 0,  0,  9 },      /* 10 dB */
7088         {  0, 0,  0, 10 },      /* 11 dB */
7089         {  0, 0,  0, 11 },      /* 12 dB */
7090         {  0, 0,  0, 12 },      /* 13 dB */
7091         {  0, 0,  0, 13 },      /* 14 dB */
7092         {  0, 0,  0, 14 },      /* 15 dB */
7093         {  0, 0,  0, 15 },      /* 16 dB */
7094 };
7095
7096 static const struct txdds_ent txdds_ddr[TXDDS_TABLE_SZ] = {
7097         /* amp, pre, main, post */
7098         {  2, 2, 15,  6 },      /* Loopback */
7099         {  0, 0,  0,  8 },      /*  2 dB */
7100         {  0, 0,  0,  8 },      /*  3 dB */
7101         {  0, 0,  0,  9 },      /*  4 dB */
7102         {  0, 0,  0,  9 },      /*  5 dB */
7103         {  0, 0,  0, 10 },      /*  6 dB */
7104         {  0, 0,  0, 10 },      /*  7 dB */
7105         {  0, 0,  0, 11 },      /*  8 dB */
7106         {  0, 0,  0, 11 },      /*  9 dB */
7107         {  0, 0,  0, 12 },      /* 10 dB */
7108         {  0, 0,  0, 12 },      /* 11 dB */
7109         {  0, 0,  0, 13 },      /* 12 dB */
7110         {  0, 0,  0, 13 },      /* 13 dB */
7111         {  0, 0,  0, 14 },      /* 14 dB */
7112         {  0, 0,  0, 14 },      /* 15 dB */
7113         {  0, 0,  0, 15 },      /* 16 dB */
7114 };
7115
7116 static const struct txdds_ent txdds_qdr[TXDDS_TABLE_SZ] = {
7117         /* amp, pre, main, post */
7118         {  2, 2, 15,  6 },      /* Loopback */
7119         {  0, 1,  0,  7 },      /*  2 dB (also QMH7342) */
7120         {  0, 1,  0,  9 },      /*  3 dB (also QMH7342) */
7121         {  0, 1,  0, 11 },      /*  4 dB */
7122         {  0, 1,  0, 13 },      /*  5 dB */
7123         {  0, 1,  0, 15 },      /*  6 dB */
7124         {  0, 1,  3, 15 },      /*  7 dB */
7125         {  0, 1,  7, 15 },      /*  8 dB */
7126         {  0, 1,  7, 15 },      /*  9 dB */
7127         {  0, 1,  8, 15 },      /* 10 dB */
7128         {  0, 1,  9, 15 },      /* 11 dB */
7129         {  0, 1, 10, 15 },      /* 12 dB */
7130         {  0, 2,  6, 15 },      /* 13 dB */
7131         {  0, 2,  7, 15 },      /* 14 dB */
7132         {  0, 2,  8, 15 },      /* 15 dB */
7133         {  0, 2,  9, 15 },      /* 16 dB */
7134 };
7135
7136 /*
7137  * extra entries for use with txselect, for indices >= TXDDS_TABLE_SZ.
7138  * These are mostly used for mez cards going through connectors
7139  * and backplane traces, but can be used to add other "unusual"
7140  * table values as well.
7141  */
7142 static const struct txdds_ent txdds_extra_sdr[TXDDS_EXTRA_SZ] = {
7143         /* amp, pre, main, post */
7144         {  0, 0, 0,  1 },       /* QMH7342 backplane settings */
7145         {  0, 0, 0,  1 },       /* QMH7342 backplane settings */
7146         {  0, 0, 0,  2 },       /* QMH7342 backplane settings */
7147         {  0, 0, 0,  2 },       /* QMH7342 backplane settings */
7148         {  0, 0, 0, 11 },       /* QME7342 backplane settings */
7149         {  0, 0, 0, 11 },       /* QME7342 backplane settings */
7150         {  0, 0, 0, 11 },       /* QME7342 backplane settings */
7151         {  0, 0, 0, 11 },       /* QME7342 backplane settings */
7152         {  0, 0, 0, 11 },       /* QME7342 backplane settings */
7153         {  0, 0, 0, 11 },       /* QME7342 backplane settings */
7154         {  0, 0, 0, 11 },       /* QME7342 backplane settings */
7155         {  0, 0, 0,  3 },       /* QMH7342 backplane settings */
7156         {  0, 0, 0,  4 },       /* QMH7342 backplane settings */
7157 };
7158
7159 static const struct txdds_ent txdds_extra_ddr[TXDDS_EXTRA_SZ] = {
7160         /* amp, pre, main, post */
7161         {  0, 0, 0,  7 },       /* QMH7342 backplane settings */
7162         {  0, 0, 0,  7 },       /* QMH7342 backplane settings */
7163         {  0, 0, 0,  8 },       /* QMH7342 backplane settings */
7164         {  0, 0, 0,  8 },       /* QMH7342 backplane settings */
7165         {  0, 0, 0, 13 },       /* QME7342 backplane settings */
7166         {  0, 0, 0, 13 },       /* QME7342 backplane settings */
7167         {  0, 0, 0, 13 },       /* QME7342 backplane settings */
7168         {  0, 0, 0, 13 },       /* QME7342 backplane settings */
7169         {  0, 0, 0, 13 },       /* QME7342 backplane settings */
7170         {  0, 0, 0, 13 },       /* QME7342 backplane settings */
7171         {  0, 0, 0, 13 },       /* QME7342 backplane settings */
7172         {  0, 0, 0,  9 },       /* QMH7342 backplane settings */
7173         {  0, 0, 0, 10 },       /* QMH7342 backplane settings */
7174 };
7175
7176 static const struct txdds_ent txdds_extra_qdr[TXDDS_EXTRA_SZ] = {
7177         /* amp, pre, main, post */
7178         {  0, 1,  0,  4 },      /* QMH7342 backplane settings */
7179         {  0, 1,  0,  5 },      /* QMH7342 backplane settings */
7180         {  0, 1,  0,  6 },      /* QMH7342 backplane settings */
7181         {  0, 1,  0,  8 },      /* QMH7342 backplane settings */
7182         {  0, 1, 12, 10 },      /* QME7342 backplane setting */
7183         {  0, 1, 12, 11 },      /* QME7342 backplane setting */
7184         {  0, 1, 12, 12 },      /* QME7342 backplane setting */
7185         {  0, 1, 12, 14 },      /* QME7342 backplane setting */
7186         {  0, 1, 12,  6 },      /* QME7342 backplane setting */
7187         {  0, 1, 12,  7 },      /* QME7342 backplane setting */
7188         {  0, 1, 12,  8 },      /* QME7342 backplane setting */
7189         {  0, 1,  0, 10 },      /* QMH7342 backplane settings */
7190         {  0, 1,  0, 12 },      /* QMH7342 backplane settings */
7191 };
7192
7193 static const struct txdds_ent txdds_extra_mfg[TXDDS_MFG_SZ] = {
7194         /* amp, pre, main, post */
7195         { 0, 0, 0, 0 },         /* QME7342 mfg settings */
7196         { 0, 0, 0, 6 },         /* QME7342 P2 mfg settings */
7197 };
7198
7199 static const struct txdds_ent *get_atten_table(const struct txdds_ent *txdds,
7200                                                unsigned atten)
7201 {
7202         /*
7203          * The attenuation table starts at 2dB for entry 1,
7204          * with entry 0 being the loopback entry.
7205          */
7206         if (atten <= 2)
7207                 atten = 1;
7208         else if (atten > TXDDS_TABLE_SZ)
7209                 atten = TXDDS_TABLE_SZ - 1;
7210         else
7211                 atten--;
7212         return txdds + atten;
7213 }
7214
7215 /*
7216  * if override is set, the module parameter txselect has a value
7217  * for this specific port, so use it, rather than our normal mechanism.
7218  */
7219 static void find_best_ent(struct qib_pportdata *ppd,
7220                           const struct txdds_ent **sdr_dds,
7221                           const struct txdds_ent **ddr_dds,
7222                           const struct txdds_ent **qdr_dds, int override)
7223 {
7224         struct qib_qsfp_cache *qd = &ppd->cpspec->qsfp_data.cache;
7225         int idx;
7226
7227         /* Search table of known cables */
7228         for (idx = 0; !override && idx < ARRAY_SIZE(vendor_txdds); ++idx) {
7229                 const struct vendor_txdds_ent *v = vendor_txdds + idx;
7230
7231                 if (!memcmp(v->oui, qd->oui, QSFP_VOUI_LEN) &&
7232                     (!v->partnum ||
7233                      !memcmp(v->partnum, qd->partnum, QSFP_PN_LEN))) {
7234                         *sdr_dds = &v->sdr;
7235                         *ddr_dds = &v->ddr;
7236                         *qdr_dds = &v->qdr;
7237                         return;
7238                 }
7239         }
7240
7241         /* Active cables don't have attenuation so we only set SERDES
7242          * settings to account for the attenuation of the board traces. */
7243         if (!override && QSFP_IS_ACTIVE(qd->tech)) {
7244                 *sdr_dds = txdds_sdr + ppd->dd->board_atten;
7245                 *ddr_dds = txdds_ddr + ppd->dd->board_atten;
7246                 *qdr_dds = txdds_qdr + ppd->dd->board_atten;
7247                 return;
7248         }
7249
7250         if (!override && QSFP_HAS_ATTEN(qd->tech) && (qd->atten[0] ||
7251                                                       qd->atten[1])) {
7252                 *sdr_dds = get_atten_table(txdds_sdr, qd->atten[0]);
7253                 *ddr_dds = get_atten_table(txdds_ddr, qd->atten[0]);
7254                 *qdr_dds = get_atten_table(txdds_qdr, qd->atten[1]);
7255                 return;
7256         } else if (ppd->cpspec->no_eep < TXDDS_TABLE_SZ) {
7257                 /*
7258                  * If we have no (or incomplete) data from the cable
7259                  * EEPROM, or no QSFP, or override is set, use the
7260                  * module parameter value to index into the attentuation
7261                  * table.
7262                  */
7263                 idx = ppd->cpspec->no_eep;
7264                 *sdr_dds = &txdds_sdr[idx];
7265                 *ddr_dds = &txdds_ddr[idx];
7266                 *qdr_dds = &txdds_qdr[idx];
7267         } else if (ppd->cpspec->no_eep < (TXDDS_TABLE_SZ + TXDDS_EXTRA_SZ)) {
7268                 /* similar to above, but index into the "extra" table. */
7269                 idx = ppd->cpspec->no_eep - TXDDS_TABLE_SZ;
7270                 *sdr_dds = &txdds_extra_sdr[idx];
7271                 *ddr_dds = &txdds_extra_ddr[idx];
7272                 *qdr_dds = &txdds_extra_qdr[idx];
7273         } else if ((IS_QME(ppd->dd) || IS_QMH(ppd->dd)) &&
7274                    ppd->cpspec->no_eep < (TXDDS_TABLE_SZ + TXDDS_EXTRA_SZ +
7275                                           TXDDS_MFG_SZ)) {
7276                 idx = ppd->cpspec->no_eep - (TXDDS_TABLE_SZ + TXDDS_EXTRA_SZ);
7277                 printk(KERN_INFO QIB_DRV_NAME
7278                         " IB%u:%u use idx %u into txdds_mfg\n",
7279                         ppd->dd->unit, ppd->port, idx);
7280                 *sdr_dds = &txdds_extra_mfg[idx];
7281                 *ddr_dds = &txdds_extra_mfg[idx];
7282                 *qdr_dds = &txdds_extra_mfg[idx];
7283         } else {
7284                 /* this shouldn't happen, it's range checked */
7285                 *sdr_dds = txdds_sdr + qib_long_atten;
7286                 *ddr_dds = txdds_ddr + qib_long_atten;
7287                 *qdr_dds = txdds_qdr + qib_long_atten;
7288         }
7289 }
7290
7291 static void init_txdds_table(struct qib_pportdata *ppd, int override)
7292 {
7293         const struct txdds_ent *sdr_dds, *ddr_dds, *qdr_dds;
7294         struct txdds_ent *dds;
7295         int idx;
7296         int single_ent = 0;
7297
7298         find_best_ent(ppd, &sdr_dds, &ddr_dds, &qdr_dds, override);
7299
7300         /* for mez cards or override, use the selected value for all entries */
7301         if (!(ppd->dd->flags & QIB_HAS_QSFP) || override)
7302                 single_ent = 1;
7303
7304         /* Fill in the first entry with the best entry found. */
7305         set_txdds(ppd, 0, sdr_dds);
7306         set_txdds(ppd, TXDDS_TABLE_SZ, ddr_dds);
7307         set_txdds(ppd, 2 * TXDDS_TABLE_SZ, qdr_dds);
7308         if (ppd->lflags & (QIBL_LINKINIT | QIBL_LINKARMED |
7309                 QIBL_LINKACTIVE)) {
7310                 dds = (struct txdds_ent *)(ppd->link_speed_active ==
7311                                            QIB_IB_QDR ?  qdr_dds :
7312                                            (ppd->link_speed_active ==
7313                                             QIB_IB_DDR ? ddr_dds : sdr_dds));
7314                 write_tx_serdes_param(ppd, dds);
7315         }
7316
7317         /* Fill in the remaining entries with the default table values. */
7318         for (idx = 1; idx < ARRAY_SIZE(txdds_sdr); ++idx) {
7319                 set_txdds(ppd, idx, single_ent ? sdr_dds : txdds_sdr + idx);
7320                 set_txdds(ppd, idx + TXDDS_TABLE_SZ,
7321                           single_ent ? ddr_dds : txdds_ddr + idx);
7322                 set_txdds(ppd, idx + 2 * TXDDS_TABLE_SZ,
7323                           single_ent ? qdr_dds : txdds_qdr + idx);
7324         }
7325 }
7326
7327 #define KR_AHB_ACC KREG_IDX(ahb_access_ctrl)
7328 #define KR_AHB_TRANS KREG_IDX(ahb_transaction_reg)
7329 #define AHB_TRANS_RDY SYM_MASK(ahb_transaction_reg, ahb_rdy)
7330 #define AHB_ADDR_LSB SYM_LSB(ahb_transaction_reg, ahb_address)
7331 #define AHB_DATA_LSB SYM_LSB(ahb_transaction_reg, ahb_data)
7332 #define AHB_WR SYM_MASK(ahb_transaction_reg, write_not_read)
7333 #define AHB_TRANS_TRIES 10
7334
7335 /*
7336  * The chan argument is 0=chan0, 1=chan1, 2=pll, 3=chan2, 4=chan4,
7337  * 5=subsystem which is why most calls have "chan + chan >> 1"
7338  * for the channel argument.
7339  */
7340 static u32 ahb_mod(struct qib_devdata *dd, int quad, int chan, int addr,
7341                     u32 data, u32 mask)
7342 {
7343         u32 rd_data, wr_data, sz_mask;
7344         u64 trans, acc, prev_acc;
7345         u32 ret = 0xBAD0BAD;
7346         int tries;
7347
7348         prev_acc = qib_read_kreg64(dd, KR_AHB_ACC);
7349         /* From this point on, make sure we return access */
7350         acc = (quad << 1) | 1;
7351         qib_write_kreg(dd, KR_AHB_ACC, acc);
7352
7353         for (tries = 1; tries < AHB_TRANS_TRIES; ++tries) {
7354                 trans = qib_read_kreg64(dd, KR_AHB_TRANS);
7355                 if (trans & AHB_TRANS_RDY)
7356                         break;
7357         }
7358         if (tries >= AHB_TRANS_TRIES) {
7359                 qib_dev_err(dd, "No ahb_rdy in %d tries\n", AHB_TRANS_TRIES);
7360                 goto bail;
7361         }
7362
7363         /* If mask is not all 1s, we need to read, but different SerDes
7364          * entities have different sizes
7365          */
7366         sz_mask = (1UL << ((quad == 1) ? 32 : 16)) - 1;
7367         wr_data = data & mask & sz_mask;
7368         if ((~mask & sz_mask) != 0) {
7369                 trans = ((chan << 6) | addr) << (AHB_ADDR_LSB + 1);
7370                 qib_write_kreg(dd, KR_AHB_TRANS, trans);
7371
7372                 for (tries = 1; tries < AHB_TRANS_TRIES; ++tries) {
7373                         trans = qib_read_kreg64(dd, KR_AHB_TRANS);
7374                         if (trans & AHB_TRANS_RDY)
7375                                 break;
7376                 }
7377                 if (tries >= AHB_TRANS_TRIES) {
7378                         qib_dev_err(dd, "No Rd ahb_rdy in %d tries\n",
7379                                     AHB_TRANS_TRIES);
7380                         goto bail;
7381                 }
7382                 /* Re-read in case host split reads and read data first */
7383                 trans = qib_read_kreg64(dd, KR_AHB_TRANS);
7384                 rd_data = (uint32_t)(trans >> AHB_DATA_LSB);
7385                 wr_data |= (rd_data & ~mask & sz_mask);
7386         }
7387
7388         /* If mask is not zero, we need to write. */
7389         if (mask & sz_mask) {
7390                 trans = ((chan << 6) | addr) << (AHB_ADDR_LSB + 1);
7391                 trans |= ((uint64_t)wr_data << AHB_DATA_LSB);
7392                 trans |= AHB_WR;
7393                 qib_write_kreg(dd, KR_AHB_TRANS, trans);
7394
7395                 for (tries = 1; tries < AHB_TRANS_TRIES; ++tries) {
7396                         trans = qib_read_kreg64(dd, KR_AHB_TRANS);
7397                         if (trans & AHB_TRANS_RDY)
7398                                 break;
7399                 }
7400                 if (tries >= AHB_TRANS_TRIES) {
7401                         qib_dev_err(dd, "No Wr ahb_rdy in %d tries\n",
7402                                     AHB_TRANS_TRIES);
7403                         goto bail;
7404                 }
7405         }
7406         ret = wr_data;
7407 bail:
7408         qib_write_kreg(dd, KR_AHB_ACC, prev_acc);
7409         return ret;
7410 }
7411
7412 static void ibsd_wr_allchans(struct qib_pportdata *ppd, int addr, unsigned data,
7413                              unsigned mask)
7414 {
7415         struct qib_devdata *dd = ppd->dd;
7416         int chan;
7417         u32 rbc;
7418
7419         for (chan = 0; chan < SERDES_CHANS; ++chan) {
7420                 ahb_mod(dd, IBSD(ppd->hw_pidx), (chan + (chan >> 1)), addr,
7421                         data, mask);
7422                 rbc = ahb_mod(dd, IBSD(ppd->hw_pidx), (chan + (chan >> 1)),
7423                               addr, 0, 0);
7424         }
7425 }
7426
7427 static void serdes_7322_los_enable(struct qib_pportdata *ppd, int enable)
7428 {
7429         u64 data = qib_read_kreg_port(ppd, krp_serdesctrl);
7430         u8 state = SYM_FIELD(data, IBSerdesCtrl_0, RXLOSEN);
7431
7432         if (enable && !state) {
7433                 printk(KERN_INFO QIB_DRV_NAME " IB%u:%u Turning LOS on\n",
7434                         ppd->dd->unit, ppd->port);
7435                 data |= SYM_MASK(IBSerdesCtrl_0, RXLOSEN);
7436         } else if (!enable && state) {
7437                 printk(KERN_INFO QIB_DRV_NAME " IB%u:%u Turning LOS off\n",
7438                         ppd->dd->unit, ppd->port);
7439                 data &= ~SYM_MASK(IBSerdesCtrl_0, RXLOSEN);
7440         }
7441         qib_write_kreg_port(ppd, krp_serdesctrl, data);
7442 }
7443
7444 static int serdes_7322_init(struct qib_pportdata *ppd)
7445 {
7446         int ret = 0;
7447         if (ppd->dd->cspec->r1)
7448                 ret = serdes_7322_init_old(ppd);
7449         else
7450                 ret = serdes_7322_init_new(ppd);
7451         return ret;
7452 }
7453
7454 static int serdes_7322_init_old(struct qib_pportdata *ppd)
7455 {
7456         u32 le_val;
7457
7458         /*
7459          * Initialize the Tx DDS tables.  Also done every QSFP event,
7460          * for adapters with QSFP
7461          */
7462         init_txdds_table(ppd, 0);
7463
7464         /* ensure no tx overrides from earlier driver loads */
7465         qib_write_kreg_port(ppd, krp_tx_deemph_override,
7466                 SYM_MASK(IBSD_TX_DEEMPHASIS_OVERRIDE_0,
7467                 reset_tx_deemphasis_override));
7468
7469         /* Patch some SerDes defaults to "Better for IB" */
7470         /* Timing Loop Bandwidth: cdr_timing[11:9] = 0 */
7471         ibsd_wr_allchans(ppd, 2, 0, BMASK(11, 9));
7472
7473         /* Termination: rxtermctrl_r2d addr 11 bits [12:11] = 1 */
7474         ibsd_wr_allchans(ppd, 11, (1 << 11), BMASK(12, 11));
7475         /* Enable LE2: rxle2en_r2a addr 13 bit [6] = 1 */
7476         ibsd_wr_allchans(ppd, 13, (1 << 6), (1 << 6));
7477
7478         /* May be overridden in qsfp_7322_event */
7479         le_val = IS_QME(ppd->dd) ? LE2_QME : LE2_DEFAULT;
7480         ibsd_wr_allchans(ppd, 13, (le_val << 7), BMASK(9, 7));
7481
7482         /* enable LE1 adaptation for all but QME, which is disabled */
7483         le_val = IS_QME(ppd->dd) ? 0 : 1;
7484         ibsd_wr_allchans(ppd, 13, (le_val << 5), (1 << 5));
7485
7486         /* Clear cmode-override, may be set from older driver */
7487         ahb_mod(ppd->dd, IBSD(ppd->hw_pidx), 5, 10, 0 << 14, 1 << 14);
7488
7489         /* Timing Recovery: rxtapsel addr 5 bits [9:8] = 0 */
7490         ibsd_wr_allchans(ppd, 5, (0 << 8), BMASK(9, 8));
7491
7492         /* setup LoS params; these are subsystem, so chan == 5 */
7493         /* LoS filter threshold_count on, ch 0-3, set to 8 */
7494         ahb_mod(ppd->dd, IBSD(ppd->hw_pidx), 5, 5, 8 << 11, BMASK(14, 11));
7495         ahb_mod(ppd->dd, IBSD(ppd->hw_pidx), 5, 7, 8 << 4, BMASK(7, 4));
7496         ahb_mod(ppd->dd, IBSD(ppd->hw_pidx), 5, 8, 8 << 11, BMASK(14, 11));
7497         ahb_mod(ppd->dd, IBSD(ppd->hw_pidx), 5, 10, 8 << 4, BMASK(7, 4));
7498
7499         /* LoS filter threshold_count off, ch 0-3, set to 4 */
7500         ahb_mod(ppd->dd, IBSD(ppd->hw_pidx), 5, 6, 4 << 0, BMASK(3, 0));
7501         ahb_mod(ppd->dd, IBSD(ppd->hw_pidx), 5, 7, 4 << 8, BMASK(11, 8));
7502         ahb_mod(ppd->dd, IBSD(ppd->hw_pidx), 5, 9, 4 << 0, BMASK(3, 0));
7503         ahb_mod(ppd->dd, IBSD(ppd->hw_pidx), 5, 10, 4 << 8, BMASK(11, 8));
7504
7505         /* LoS filter select enabled */
7506         ahb_mod(ppd->dd, IBSD(ppd->hw_pidx), 5, 9, 1 << 15, 1 << 15);
7507
7508         /* LoS target data:  SDR=4, DDR=2, QDR=1 */
7509         ibsd_wr_allchans(ppd, 14, (1 << 3), BMASK(5, 3)); /* QDR */
7510         ibsd_wr_allchans(ppd, 20, (2 << 10), BMASK(12, 10)); /* DDR */
7511         ibsd_wr_allchans(ppd, 20, (4 << 13), BMASK(15, 13)); /* SDR */
7512
7513         serdes_7322_los_enable(ppd, 1);
7514
7515         /* rxbistena; set 0 to avoid effects of it switch later */
7516         ibsd_wr_allchans(ppd, 9, 0 << 15, 1 << 15);
7517
7518         /* Configure 4 DFE taps, and only they adapt */
7519         ibsd_wr_allchans(ppd, 16, 0 << 0, BMASK(1, 0));
7520
7521         /* gain hi stop 32 (22) (6:1) lo stop 7 (10:7) target 22 (13) (15:11) */
7522         le_val = (ppd->dd->cspec->r1 || IS_QME(ppd->dd)) ? 0xb6c0 : 0x6bac;
7523         ibsd_wr_allchans(ppd, 21, le_val, 0xfffe);
7524
7525         /*
7526          * Set receive adaptation mode.  SDR and DDR adaptation are
7527          * always on, and QDR is initially enabled; later disabled.
7528          */
7529         qib_write_kreg_port(ppd, krp_static_adapt_dis(0), 0ULL);
7530         qib_write_kreg_port(ppd, krp_static_adapt_dis(1), 0ULL);
7531         qib_write_kreg_port(ppd, krp_static_adapt_dis(2),
7532                             ppd->dd->cspec->r1 ?
7533                             QDR_STATIC_ADAPT_DOWN_R1 : QDR_STATIC_ADAPT_DOWN);
7534         ppd->cpspec->qdr_dfe_on = 1;
7535
7536         /* FLoop LOS gate: PPM filter  enabled */
7537         ibsd_wr_allchans(ppd, 38, 0 << 10, 1 << 10);
7538
7539         /* rx offset center enabled */
7540         ibsd_wr_allchans(ppd, 12, 1 << 4, 1 << 4);
7541
7542         if (!ppd->dd->cspec->r1) {
7543                 ibsd_wr_allchans(ppd, 12, 1 << 12, 1 << 12);
7544                 ibsd_wr_allchans(ppd, 12, 2 << 8, 0x0f << 8);
7545         }
7546
7547         /* Set the frequency loop bandwidth to 15 */
7548         ibsd_wr_allchans(ppd, 2, 15 << 5, BMASK(8, 5));
7549
7550         return 0;
7551 }
7552
7553 static int serdes_7322_init_new(struct qib_pportdata *ppd)
7554 {
7555         unsigned long tend;
7556         u32 le_val, rxcaldone;
7557         int chan, chan_done = (1 << SERDES_CHANS) - 1;
7558
7559         /* Clear cmode-override, may be set from older driver */
7560         ahb_mod(ppd->dd, IBSD(ppd->hw_pidx), 5, 10, 0 << 14, 1 << 14);
7561
7562         /* ensure no tx overrides from earlier driver loads */
7563         qib_write_kreg_port(ppd, krp_tx_deemph_override,
7564                 SYM_MASK(IBSD_TX_DEEMPHASIS_OVERRIDE_0,
7565                 reset_tx_deemphasis_override));
7566
7567         /* START OF LSI SUGGESTED SERDES BRINGUP */
7568         /* Reset - Calibration Setup */
7569         /*       Stop DFE adaptaion */
7570         ibsd_wr_allchans(ppd, 1, 0, BMASK(9, 1));
7571         /*       Disable LE1 */
7572         ibsd_wr_allchans(ppd, 13, 0, BMASK(5, 5));
7573         /*       Disable autoadapt for LE1 */
7574         ibsd_wr_allchans(ppd, 1, 0, BMASK(15, 15));
7575         /*       Disable LE2 */
7576         ibsd_wr_allchans(ppd, 13, 0, BMASK(6, 6));
7577         /*       Disable VGA */
7578         ibsd_wr_allchans(ppd, 5, 0, BMASK(0, 0));
7579         /*       Disable AFE Offset Cancel */
7580         ibsd_wr_allchans(ppd, 12, 0, BMASK(12, 12));
7581         /*       Disable Timing Loop */
7582         ibsd_wr_allchans(ppd, 2, 0, BMASK(3, 3));
7583         /*       Disable Frequency Loop */
7584         ibsd_wr_allchans(ppd, 2, 0, BMASK(4, 4));
7585         /*       Disable Baseline Wander Correction */
7586         ibsd_wr_allchans(ppd, 13, 0, BMASK(13, 13));
7587         /*       Disable RX Calibration */
7588         ibsd_wr_allchans(ppd, 4, 0, BMASK(10, 10));
7589         /*       Disable RX Offset Calibration */
7590         ibsd_wr_allchans(ppd, 12, 0, BMASK(4, 4));
7591         /*       Select BB CDR */
7592         ibsd_wr_allchans(ppd, 2, (1 << 15), BMASK(15, 15));
7593         /*       CDR Step Size */
7594         ibsd_wr_allchans(ppd, 5, 0, BMASK(9, 8));
7595         /*       Enable phase Calibration */
7596         ibsd_wr_allchans(ppd, 12, (1 << 5), BMASK(5, 5));
7597         /*       DFE Bandwidth [2:14-12] */
7598         ibsd_wr_allchans(ppd, 2, (4 << 12), BMASK(14, 12));
7599         /*       DFE Config (4 taps only) */
7600         ibsd_wr_allchans(ppd, 16, 0, BMASK(1, 0));
7601         /*       Gain Loop Bandwidth */
7602         if (!ppd->dd->cspec->r1) {
7603                 ibsd_wr_allchans(ppd, 12, 1 << 12, BMASK(12, 12));
7604                 ibsd_wr_allchans(ppd, 12, 2 << 8, BMASK(11, 8));
7605         } else {
7606                 ibsd_wr_allchans(ppd, 19, (3 << 11), BMASK(13, 11));
7607         }
7608         /*       Baseline Wander Correction Gain [13:4-0] (leave as default) */
7609         /*       Baseline Wander Correction Gain [3:7-5] (leave as default) */
7610         /*       Data Rate Select [5:7-6] (leave as default) */
7611         /*       RX Parallel Word Width [3:10-8] (leave as default) */
7612
7613         /* RX REST */
7614         /*       Single- or Multi-channel reset */
7615         /*       RX Analog reset */
7616         /*       RX Digital reset */
7617         ibsd_wr_allchans(ppd, 0, 0, BMASK(15, 13));
7618         msleep(20);
7619         /*       RX Analog reset */
7620         ibsd_wr_allchans(ppd, 0, (1 << 14), BMASK(14, 14));
7621         msleep(20);
7622         /*       RX Digital reset */
7623         ibsd_wr_allchans(ppd, 0, (1 << 13), BMASK(13, 13));
7624         msleep(20);
7625
7626         /* setup LoS params; these are subsystem, so chan == 5 */
7627         /* LoS filter threshold_count on, ch 0-3, set to 8 */
7628         ahb_mod(ppd->dd, IBSD(ppd->hw_pidx), 5, 5, 8 << 11, BMASK(14, 11));
7629         ahb_mod(ppd->dd, IBSD(ppd->hw_pidx), 5, 7, 8 << 4, BMASK(7, 4));
7630         ahb_mod(ppd->dd, IBSD(ppd->hw_pidx), 5, 8, 8 << 11, BMASK(14, 11));
7631         ahb_mod(ppd->dd, IBSD(ppd->hw_pidx), 5, 10, 8 << 4, BMASK(7, 4));
7632
7633         /* LoS filter threshold_count off, ch 0-3, set to 4 */
7634         ahb_mod(ppd->dd, IBSD(ppd->hw_pidx), 5, 6, 4 << 0, BMASK(3, 0));
7635         ahb_mod(ppd->dd, IBSD(ppd->hw_pidx), 5, 7, 4 << 8, BMASK(11, 8));
7636         ahb_mod(ppd->dd, IBSD(ppd->hw_pidx), 5, 9, 4 << 0, BMASK(3, 0));
7637         ahb_mod(ppd->dd, IBSD(ppd->hw_pidx), 5, 10, 4 << 8, BMASK(11, 8));
7638
7639         /* LoS filter select enabled */
7640         ahb_mod(ppd->dd, IBSD(ppd->hw_pidx), 5, 9, 1 << 15, 1 << 15);
7641
7642         /* LoS target data:  SDR=4, DDR=2, QDR=1 */
7643         ibsd_wr_allchans(ppd, 14, (1 << 3), BMASK(5, 3)); /* QDR */
7644         ibsd_wr_allchans(ppd, 20, (2 << 10), BMASK(12, 10)); /* DDR */
7645         ibsd_wr_allchans(ppd, 20, (4 << 13), BMASK(15, 13)); /* SDR */
7646
7647         /* Turn on LOS on initial SERDES init */
7648         serdes_7322_los_enable(ppd, 1);
7649         /* FLoop LOS gate: PPM filter  enabled */
7650         ibsd_wr_allchans(ppd, 38, 0 << 10, 1 << 10);
7651
7652         /* RX LATCH CALIBRATION */
7653         /*       Enable Eyefinder Phase Calibration latch */
7654         ibsd_wr_allchans(ppd, 15, 1, BMASK(0, 0));
7655         /*       Enable RX Offset Calibration latch */
7656         ibsd_wr_allchans(ppd, 12, (1 << 4), BMASK(4, 4));
7657         msleep(20);
7658         /*       Start Calibration */
7659         ibsd_wr_allchans(ppd, 4, (1 << 10), BMASK(10, 10));
7660         tend = jiffies + msecs_to_jiffies(500);
7661         while (chan_done && !time_is_before_jiffies(tend)) {
7662                 msleep(20);
7663                 for (chan = 0; chan < SERDES_CHANS; ++chan) {
7664                         rxcaldone = ahb_mod(ppd->dd, IBSD(ppd->hw_pidx),
7665                                             (chan + (chan >> 1)),
7666                                             25, 0, 0);
7667                         if ((~rxcaldone & (u32)BMASK(9, 9)) == 0 &&
7668                             (~chan_done & (1 << chan)) == 0)
7669                                 chan_done &= ~(1 << chan);
7670                 }
7671         }
7672         if (chan_done) {
7673                 printk(KERN_INFO QIB_DRV_NAME
7674                          " Serdes %d calibration not done after .5 sec: 0x%x\n",
7675                          IBSD(ppd->hw_pidx), chan_done);
7676         } else {
7677                 for (chan = 0; chan < SERDES_CHANS; ++chan) {
7678                         rxcaldone = ahb_mod(ppd->dd, IBSD(ppd->hw_pidx),
7679                                             (chan + (chan >> 1)),
7680                                             25, 0, 0);
7681                         if ((~rxcaldone & (u32)BMASK(10, 10)) == 0)
7682                                 printk(KERN_INFO QIB_DRV_NAME
7683                                          " Serdes %d chan %d calibration "
7684                                          "failed\n", IBSD(ppd->hw_pidx), chan);
7685                 }
7686         }
7687
7688         /*       Turn off Calibration */
7689         ibsd_wr_allchans(ppd, 4, 0, BMASK(10, 10));
7690         msleep(20);
7691
7692         /* BRING RX UP */
7693         /*       Set LE2 value (May be overridden in qsfp_7322_event) */
7694         le_val = IS_QME(ppd->dd) ? LE2_QME : LE2_DEFAULT;
7695         ibsd_wr_allchans(ppd, 13, (le_val << 7), BMASK(9, 7));
7696         /*       Set LE2 Loop bandwidth */
7697         ibsd_wr_allchans(ppd, 3, (7 << 5), BMASK(7, 5));
7698         /*       Enable LE2 */
7699         ibsd_wr_allchans(ppd, 13, (1 << 6), BMASK(6, 6));
7700         msleep(20);
7701         /*       Enable H0 only */
7702         ibsd_wr_allchans(ppd, 1, 1, BMASK(9, 1));
7703         /* gain hi stop 32 (22) (6:1) lo stop 7 (10:7) target 22 (13) (15:11) */
7704         le_val = (ppd->dd->cspec->r1 || IS_QME(ppd->dd)) ? 0xb6c0 : 0x6bac;
7705         ibsd_wr_allchans(ppd, 21, le_val, 0xfffe);
7706         /*       Enable VGA */
7707         ibsd_wr_allchans(ppd, 5, 0, BMASK(0, 0));
7708         msleep(20);
7709         /*       Set Frequency Loop Bandwidth */
7710         ibsd_wr_allchans(ppd, 2, (15 << 5), BMASK(8, 5));
7711         /*       Enable Frequency Loop */
7712         ibsd_wr_allchans(ppd, 2, (1 << 4), BMASK(4, 4));
7713         /*       Set Timing Loop Bandwidth */
7714         ibsd_wr_allchans(ppd, 2, 0, BMASK(11, 9));
7715         /*       Enable Timing Loop */
7716         ibsd_wr_allchans(ppd, 2, (1 << 3), BMASK(3, 3));
7717         msleep(50);
7718         /*       Enable DFE
7719          *       Set receive adaptation mode.  SDR and DDR adaptation are
7720          *       always on, and QDR is initially enabled; later disabled.
7721          */
7722         qib_write_kreg_port(ppd, krp_static_adapt_dis(0), 0ULL);
7723         qib_write_kreg_port(ppd, krp_static_adapt_dis(1), 0ULL);
7724         qib_write_kreg_port(ppd, krp_static_adapt_dis(2),
7725                             ppd->dd->cspec->r1 ?
7726                             QDR_STATIC_ADAPT_DOWN_R1 : QDR_STATIC_ADAPT_DOWN);
7727         ppd->cpspec->qdr_dfe_on = 1;
7728         /*       Disable LE1  */
7729         ibsd_wr_allchans(ppd, 13, (0 << 5), (1 << 5));
7730         /*       Disable auto adapt for LE1 */
7731         ibsd_wr_allchans(ppd, 1, (0 << 15), BMASK(15, 15));
7732         msleep(20);
7733         /*       Enable AFE Offset Cancel */
7734         ibsd_wr_allchans(ppd, 12, (1 << 12), BMASK(12, 12));
7735         /*       Enable Baseline Wander Correction */
7736         ibsd_wr_allchans(ppd, 12, (1 << 13), BMASK(13, 13));
7737         /* Termination: rxtermctrl_r2d addr 11 bits [12:11] = 1 */
7738         ibsd_wr_allchans(ppd, 11, (1 << 11), BMASK(12, 11));
7739         /* VGA output common mode */
7740         ibsd_wr_allchans(ppd, 12, (3 << 2), BMASK(3, 2));
7741
7742         /*
7743          * Initialize the Tx DDS tables.  Also done every QSFP event,
7744          * for adapters with QSFP
7745          */
7746         init_txdds_table(ppd, 0);
7747
7748         return 0;
7749 }
7750
7751 /* start adjust QMH serdes parameters */
7752
7753 static void set_man_code(struct qib_pportdata *ppd, int chan, int code)
7754 {
7755         ahb_mod(ppd->dd, IBSD(ppd->hw_pidx), (chan + (chan >> 1)),
7756                 9, code << 9, 0x3f << 9);
7757 }
7758
7759 static void set_man_mode_h1(struct qib_pportdata *ppd, int chan,
7760         int enable, u32 tapenable)
7761 {
7762         if (enable)
7763                 ahb_mod(ppd->dd, IBSD(ppd->hw_pidx), (chan + (chan >> 1)),
7764                         1, 3 << 10, 0x1f << 10);
7765         else
7766                 ahb_mod(ppd->dd, IBSD(ppd->hw_pidx), (chan + (chan >> 1)),
7767                         1, 0, 0x1f << 10);
7768 }
7769
7770 /* Set clock to 1, 0, 1, 0 */
7771 static void clock_man(struct qib_pportdata *ppd, int chan)
7772 {
7773         ahb_mod(ppd->dd, IBSD(ppd->hw_pidx), (chan + (chan >> 1)),
7774                 4, 0x4000, 0x4000);
7775         ahb_mod(ppd->dd, IBSD(ppd->hw_pidx), (chan + (chan >> 1)),
7776                 4, 0, 0x4000);
7777         ahb_mod(ppd->dd, IBSD(ppd->hw_pidx), (chan + (chan >> 1)),
7778                 4, 0x4000, 0x4000);
7779         ahb_mod(ppd->dd, IBSD(ppd->hw_pidx), (chan + (chan >> 1)),
7780                 4, 0, 0x4000);
7781 }
7782
7783 /*
7784  * write the current Tx serdes pre,post,main,amp settings into the serdes.
7785  * The caller must pass the settings appropriate for the current speed,
7786  * or not care if they are correct for the current speed.
7787  */
7788 static void write_tx_serdes_param(struct qib_pportdata *ppd,
7789                                   struct txdds_ent *txdds)
7790 {
7791         u64 deemph;
7792
7793         deemph = qib_read_kreg_port(ppd, krp_tx_deemph_override);
7794         /* field names for amp, main, post, pre, respectively */
7795         deemph &= ~(SYM_MASK(IBSD_TX_DEEMPHASIS_OVERRIDE_0, txampcntl_d2a) |
7796                     SYM_MASK(IBSD_TX_DEEMPHASIS_OVERRIDE_0, txc0_ena) |
7797                     SYM_MASK(IBSD_TX_DEEMPHASIS_OVERRIDE_0, txcp1_ena) |
7798                     SYM_MASK(IBSD_TX_DEEMPHASIS_OVERRIDE_0, txcn1_ena));
7799
7800         deemph |= SYM_MASK(IBSD_TX_DEEMPHASIS_OVERRIDE_0,
7801                            tx_override_deemphasis_select);
7802         deemph |= (txdds->amp & SYM_RMASK(IBSD_TX_DEEMPHASIS_OVERRIDE_0,
7803                     txampcntl_d2a)) << SYM_LSB(IBSD_TX_DEEMPHASIS_OVERRIDE_0,
7804                                        txampcntl_d2a);
7805         deemph |= (txdds->main & SYM_RMASK(IBSD_TX_DEEMPHASIS_OVERRIDE_0,
7806                      txc0_ena)) << SYM_LSB(IBSD_TX_DEEMPHASIS_OVERRIDE_0,
7807                                    txc0_ena);
7808         deemph |= (txdds->post & SYM_RMASK(IBSD_TX_DEEMPHASIS_OVERRIDE_0,
7809                      txcp1_ena)) << SYM_LSB(IBSD_TX_DEEMPHASIS_OVERRIDE_0,
7810                                     txcp1_ena);
7811         deemph |= (txdds->pre & SYM_RMASK(IBSD_TX_DEEMPHASIS_OVERRIDE_0,
7812                      txcn1_ena)) << SYM_LSB(IBSD_TX_DEEMPHASIS_OVERRIDE_0,
7813                                     txcn1_ena);
7814         qib_write_kreg_port(ppd, krp_tx_deemph_override, deemph);
7815 }
7816
7817 /*
7818  * Set the parameters for mez cards on link bounce, so they are
7819  * always exactly what was requested.  Similar logic to init_txdds
7820  * but does just the serdes.
7821  */
7822 static void adj_tx_serdes(struct qib_pportdata *ppd)
7823 {
7824         const struct txdds_ent *sdr_dds, *ddr_dds, *qdr_dds;
7825         struct txdds_ent *dds;
7826
7827         find_best_ent(ppd, &sdr_dds, &ddr_dds, &qdr_dds, 1);
7828         dds = (struct txdds_ent *)(ppd->link_speed_active == QIB_IB_QDR ?
7829                 qdr_dds : (ppd->link_speed_active == QIB_IB_DDR ?
7830                                 ddr_dds : sdr_dds));
7831         write_tx_serdes_param(ppd, dds);
7832 }
7833
7834 /* set QDR forced value for H1, if needed */
7835 static void force_h1(struct qib_pportdata *ppd)
7836 {
7837         int chan;
7838
7839         ppd->cpspec->qdr_reforce = 0;
7840         if (!ppd->dd->cspec->r1)
7841                 return;
7842
7843         for (chan = 0; chan < SERDES_CHANS; chan++) {
7844                 set_man_mode_h1(ppd, chan, 1, 0);
7845                 set_man_code(ppd, chan, ppd->cpspec->h1_val);
7846                 clock_man(ppd, chan);
7847                 set_man_mode_h1(ppd, chan, 0, 0);
7848         }
7849 }
7850
7851 #define SJA_EN SYM_MASK(SPC_JTAG_ACCESS_REG, SPC_JTAG_ACCESS_EN)
7852 #define BISTEN_LSB SYM_LSB(SPC_JTAG_ACCESS_REG, bist_en)
7853
7854 #define R_OPCODE_LSB 3
7855 #define R_OP_NOP 0
7856 #define R_OP_SHIFT 2
7857 #define R_OP_UPDATE 3
7858 #define R_TDI_LSB 2
7859 #define R_TDO_LSB 1
7860 #define R_RDY 1
7861
7862 static int qib_r_grab(struct qib_devdata *dd)
7863 {
7864         u64 val;
7865         val = SJA_EN;
7866         qib_write_kreg(dd, kr_r_access, val);
7867         qib_read_kreg32(dd, kr_scratch);
7868         return 0;
7869 }
7870
7871 /* qib_r_wait_for_rdy() not only waits for the ready bit, it
7872  * returns the current state of R_TDO
7873  */
7874 static int qib_r_wait_for_rdy(struct qib_devdata *dd)
7875 {
7876         u64 val;
7877         int timeout;
7878         for (timeout = 0; timeout < 100 ; ++timeout) {
7879                 val = qib_read_kreg32(dd, kr_r_access);
7880                 if (val & R_RDY)
7881                         return (val >> R_TDO_LSB) & 1;
7882         }
7883         return -1;
7884 }
7885
7886 static int qib_r_shift(struct qib_devdata *dd, int bisten,
7887                        int len, u8 *inp, u8 *outp)
7888 {
7889         u64 valbase, val;
7890         int ret, pos;
7891
7892         valbase = SJA_EN | (bisten << BISTEN_LSB) |
7893                 (R_OP_SHIFT << R_OPCODE_LSB);
7894         ret = qib_r_wait_for_rdy(dd);
7895         if (ret < 0)
7896                 goto bail;
7897         for (pos = 0; pos < len; ++pos) {
7898                 val = valbase;
7899                 if (outp) {
7900                         outp[pos >> 3] &= ~(1 << (pos & 7));
7901                         outp[pos >> 3] |= (ret << (pos & 7));
7902                 }
7903                 if (inp) {
7904                         int tdi = inp[pos >> 3] >> (pos & 7);
7905                         val |= ((tdi & 1) << R_TDI_LSB);
7906                 }
7907                 qib_write_kreg(dd, kr_r_access, val);
7908                 qib_read_kreg32(dd, kr_scratch);
7909                 ret = qib_r_wait_for_rdy(dd);
7910                 if (ret < 0)
7911                         break;
7912         }
7913         /* Restore to NOP between operations. */
7914         val =  SJA_EN | (bisten << BISTEN_LSB);
7915         qib_write_kreg(dd, kr_r_access, val);
7916         qib_read_kreg32(dd, kr_scratch);
7917         ret = qib_r_wait_for_rdy(dd);
7918
7919         if (ret >= 0)
7920                 ret = pos;
7921 bail:
7922         return ret;
7923 }
7924
7925 static int qib_r_update(struct qib_devdata *dd, int bisten)
7926 {
7927         u64 val;
7928         int ret;
7929
7930         val = SJA_EN | (bisten << BISTEN_LSB) | (R_OP_UPDATE << R_OPCODE_LSB);
7931         ret = qib_r_wait_for_rdy(dd);
7932         if (ret >= 0) {
7933                 qib_write_kreg(dd, kr_r_access, val);
7934                 qib_read_kreg32(dd, kr_scratch);
7935         }
7936         return ret;
7937 }
7938
7939 #define BISTEN_PORT_SEL 15
7940 #define LEN_PORT_SEL 625
7941 #define BISTEN_AT 17
7942 #define LEN_AT 156
7943 #define BISTEN_ETM 16
7944 #define LEN_ETM 632
7945
7946 #define BIT2BYTE(x) (((x) +  BITS_PER_BYTE - 1) / BITS_PER_BYTE)
7947
7948 /* these are common for all IB port use cases. */
7949 static u8 reset_at[BIT2BYTE(LEN_AT)] = {
7950         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
7951         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x20, 0x00,
7952 };
7953 static u8 reset_atetm[BIT2BYTE(LEN_ETM)] = {
7954         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
7955         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
7956         0x00, 0x00, 0x00, 0x80, 0xe3, 0x81, 0x73, 0x3c, 0x70, 0x8e,
7957         0x07, 0xce, 0xf1, 0xc0, 0x39, 0x1e, 0x38, 0xc7, 0x03, 0xe7,
7958         0x78, 0xe0, 0x1c, 0x0f, 0x9c, 0x7f, 0x80, 0x73, 0x0f, 0x70,
7959         0xde, 0x01, 0xce, 0x39, 0xc0, 0xf9, 0x06, 0x38, 0xd7, 0x00,
7960         0xe7, 0x19, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
7961         0x00, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00,
7962 };
7963 static u8 at[BIT2BYTE(LEN_AT)] = {
7964         0x00, 0x00, 0x18, 0x00, 0x00, 0x00, 0x18, 0x00, 0x00, 0x00,
7965         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x20, 0x00,
7966 };
7967
7968 /* used for IB1 or IB2, only one in use */
7969 static u8 atetm_1port[BIT2BYTE(LEN_ETM)] = {
7970         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
7971         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
7972         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
7973         0x00, 0x10, 0xf2, 0x80, 0x83, 0x1e, 0x38, 0x00, 0x00, 0x00,
7974         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
7975         0x00, 0x00, 0x50, 0xf4, 0x41, 0x00, 0x18, 0x78, 0xc8, 0x03,
7976         0x07, 0x7b, 0xa0, 0x3e, 0x00, 0x02, 0x00, 0x00, 0x18, 0x00,
7977         0x18, 0x00, 0x00, 0x00, 0x00, 0x4b, 0x00, 0x00, 0x00,
7978 };
7979
7980 /* used when both IB1 and IB2 are in use */
7981 static u8 atetm_2port[BIT2BYTE(LEN_ETM)] = {
7982         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
7983         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x79,
7984         0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
7985         0x00, 0x00, 0xf8, 0x80, 0x83, 0x1e, 0x38, 0xe0, 0x03, 0x05,
7986         0x7b, 0xa0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80,
7987         0xa2, 0x0f, 0x50, 0xf4, 0x41, 0x00, 0x18, 0x78, 0xd1, 0x07,
7988         0x02, 0x7c, 0x80, 0x3e, 0x00, 0x02, 0x00, 0x00, 0x3e, 0x00,
7989         0x02, 0x00, 0x00, 0x00, 0x00, 0x64, 0x00, 0x00, 0x00,
7990 };
7991
7992 /* used when only IB1 is in use */
7993 static u8 portsel_port1[BIT2BYTE(LEN_PORT_SEL)] = {
7994         0x32, 0x65, 0xa4, 0x7b, 0x10, 0x98, 0xdc, 0xfe, 0x13, 0x13,
7995         0x13, 0x13, 0x13, 0x13, 0x13, 0x13, 0x73, 0x0c, 0x0c, 0x0c,
7996         0x0c, 0x0c, 0x13, 0x13, 0x13, 0x13, 0x13, 0x13, 0x13, 0x13,
7997         0x13, 0x78, 0x78, 0x13, 0x13, 0x13, 0x13, 0x13, 0x13, 0x13,
7998         0x14, 0x14, 0x14, 0x14, 0x14, 0x14, 0x14, 0x14, 0x74, 0x32,
7999         0x32, 0x32, 0x32, 0x32, 0x14, 0x14, 0x14, 0x14, 0x14, 0x14,
8000         0x14, 0x14, 0x14, 0x14, 0x14, 0x14, 0x14, 0x14, 0x14, 0x14,
8001         0x14, 0x14, 0x9f, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00,
8002 };
8003
8004 /* used when only IB2 is in use */
8005 static u8 portsel_port2[BIT2BYTE(LEN_PORT_SEL)] = {
8006         0x32, 0x65, 0xa4, 0x7b, 0x10, 0x98, 0xdc, 0xfe, 0x39, 0x39,
8007         0x39, 0x39, 0x39, 0x39, 0x39, 0x39, 0x73, 0x32, 0x32, 0x32,
8008         0x32, 0x32, 0x39, 0x39, 0x39, 0x39, 0x39, 0x39, 0x39, 0x39,
8009         0x39, 0x78, 0x78, 0x39, 0x39, 0x39, 0x39, 0x39, 0x39, 0x39,
8010         0x3a, 0x3a, 0x3a, 0x3a, 0x3a, 0x3a, 0x3a, 0x3a, 0x74, 0x32,
8011         0x32, 0x32, 0x32, 0x32, 0x3a, 0x3a, 0x3a, 0x3a, 0x3a, 0x3a,
8012         0x3a, 0x3a, 0x3a, 0x3a, 0x3a, 0x3a, 0x3a, 0x3a, 0x3a, 0x3a,
8013         0x3a, 0x3a, 0x9f, 0x01, 0x00, 0x00, 0x00, 0x00, 0x01,
8014 };
8015
8016 /* used when both IB1 and IB2 are in use */
8017 static u8 portsel_2port[BIT2BYTE(LEN_PORT_SEL)] = {
8018         0x32, 0xba, 0x54, 0x76, 0x10, 0x98, 0xdc, 0xfe, 0x13, 0x13,
8019         0x13, 0x13, 0x13, 0x13, 0x13, 0x13, 0x73, 0x0c, 0x0c, 0x0c,
8020         0x0c, 0x0c, 0x13, 0x13, 0x13, 0x13, 0x13, 0x13, 0x13, 0x13,
8021         0x13, 0x13, 0x13, 0x13, 0x13, 0x13, 0x13, 0x13, 0x13, 0x13,
8022         0x14, 0x14, 0x14, 0x14, 0x14, 0x14, 0x14, 0x14, 0x74, 0x32,
8023         0x32, 0x32, 0x32, 0x32, 0x14, 0x14, 0x14, 0x14, 0x14, 0x3a,
8024         0x3a, 0x14, 0x14, 0x14, 0x14, 0x14, 0x14, 0x14, 0x14, 0x14,
8025         0x14, 0x14, 0x9f, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00,
8026 };
8027
8028 /*
8029  * Do setup to properly handle IB link recovery; if port is zero, we
8030  * are initializing to cover both ports; otherwise we are initializing
8031  * to cover a single port card, or the port has reached INIT and we may
8032  * need to switch coverage types.
8033  */
8034 static void setup_7322_link_recovery(struct qib_pportdata *ppd, u32 both)
8035 {
8036         u8 *portsel, *etm;
8037         struct qib_devdata *dd = ppd->dd;
8038
8039         if (!ppd->dd->cspec->r1)
8040                 return;
8041         if (!both) {
8042                 dd->cspec->recovery_ports_initted++;
8043                 ppd->cpspec->recovery_init = 1;
8044         }
8045         if (!both && dd->cspec->recovery_ports_initted == 1) {
8046                 portsel = ppd->port == 1 ? portsel_port1 : portsel_port2;
8047                 etm = atetm_1port;
8048         } else {
8049                 portsel = portsel_2port;
8050                 etm = atetm_2port;
8051         }
8052
8053         if (qib_r_grab(dd) < 0 ||
8054                 qib_r_shift(dd, BISTEN_ETM, LEN_ETM, reset_atetm, NULL) < 0 ||
8055                 qib_r_update(dd, BISTEN_ETM) < 0 ||
8056                 qib_r_shift(dd, BISTEN_AT, LEN_AT, reset_at, NULL) < 0 ||
8057                 qib_r_update(dd, BISTEN_AT) < 0 ||
8058                 qib_r_shift(dd, BISTEN_PORT_SEL, LEN_PORT_SEL,
8059                             portsel, NULL) < 0 ||
8060                 qib_r_update(dd, BISTEN_PORT_SEL) < 0 ||
8061                 qib_r_shift(dd, BISTEN_AT, LEN_AT, at, NULL) < 0 ||
8062                 qib_r_update(dd, BISTEN_AT) < 0 ||
8063                 qib_r_shift(dd, BISTEN_ETM, LEN_ETM, etm, NULL) < 0 ||
8064                 qib_r_update(dd, BISTEN_ETM) < 0)
8065                 qib_dev_err(dd, "Failed IB link recovery setup\n");
8066 }
8067
8068 static void check_7322_rxe_status(struct qib_pportdata *ppd)
8069 {
8070         struct qib_devdata *dd = ppd->dd;
8071         u64 fmask;
8072
8073         if (dd->cspec->recovery_ports_initted != 1)
8074                 return; /* rest doesn't apply to dualport */
8075         qib_write_kreg(dd, kr_control, dd->control |
8076                        SYM_MASK(Control, FreezeMode));
8077         (void)qib_read_kreg64(dd, kr_scratch);
8078         udelay(3); /* ibcreset asserted 400ns, be sure that's over */
8079         fmask = qib_read_kreg64(dd, kr_act_fmask);
8080         if (!fmask) {
8081                 /*
8082                  * require a powercycle before we'll work again, and make
8083                  * sure we get no more interrupts, and don't turn off
8084                  * freeze.
8085                  */
8086                 ppd->dd->cspec->stay_in_freeze = 1;
8087                 qib_7322_set_intr_state(ppd->dd, 0);
8088                 qib_write_kreg(dd, kr_fmask, 0ULL);
8089                 qib_dev_err(dd, "HCA unusable until powercycled\n");
8090                 return; /* eventually reset */
8091         }
8092
8093         qib_write_kreg(ppd->dd, kr_hwerrclear,
8094             SYM_MASK(HwErrClear, IBSerdesPClkNotDetectClear_1));
8095
8096         /* don't do the full clear_freeze(), not needed for this */
8097         qib_write_kreg(dd, kr_control, dd->control);
8098         qib_read_kreg32(dd, kr_scratch);
8099         /* take IBC out of reset */
8100         if (ppd->link_speed_supported) {
8101                 ppd->cpspec->ibcctrl_a &=
8102                         ~SYM_MASK(IBCCtrlA_0, IBStatIntReductionEn);
8103                 qib_write_kreg_port(ppd, krp_ibcctrl_a,
8104                                     ppd->cpspec->ibcctrl_a);
8105                 qib_read_kreg32(dd, kr_scratch);
8106                 if (ppd->lflags & QIBL_IB_LINK_DISABLED)
8107                         qib_set_ib_7322_lstate(ppd, 0,
8108                                 QLOGIC_IB_IBCC_LINKINITCMD_DISABLE);
8109         }
8110 }