xref: /OK3568_Linux_fs/kernel/drivers/infiniband/hw/qib/qib_iba6120.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun /*
2*4882a593Smuzhiyun  * Copyright (c) 2013 - 2017 Intel Corporation. All rights reserved.
3*4882a593Smuzhiyun  * Copyright (c) 2006, 2007, 2008, 2009, 2010 QLogic Corporation.
4*4882a593Smuzhiyun  * All rights reserved.
5*4882a593Smuzhiyun  * Copyright (c) 2003, 2004, 2005, 2006 PathScale, Inc. All rights reserved.
6*4882a593Smuzhiyun  *
7*4882a593Smuzhiyun  * This software is available to you under a choice of one of two
8*4882a593Smuzhiyun  * licenses.  You may choose to be licensed under the terms of the GNU
9*4882a593Smuzhiyun  * General Public License (GPL) Version 2, available from the file
10*4882a593Smuzhiyun  * COPYING in the main directory of this source tree, or the
11*4882a593Smuzhiyun  * OpenIB.org BSD license below:
12*4882a593Smuzhiyun  *
13*4882a593Smuzhiyun  *     Redistribution and use in source and binary forms, with or
14*4882a593Smuzhiyun  *     without modification, are permitted provided that the following
15*4882a593Smuzhiyun  *     conditions are met:
16*4882a593Smuzhiyun  *
17*4882a593Smuzhiyun  *      - Redistributions of source code must retain the above
18*4882a593Smuzhiyun  *        copyright notice, this list of conditions and the following
19*4882a593Smuzhiyun  *        disclaimer.
20*4882a593Smuzhiyun  *
21*4882a593Smuzhiyun  *      - Redistributions in binary form must reproduce the above
22*4882a593Smuzhiyun  *        copyright notice, this list of conditions and the following
23*4882a593Smuzhiyun  *        disclaimer in the documentation and/or other materials
24*4882a593Smuzhiyun  *        provided with the distribution.
25*4882a593Smuzhiyun  *
26*4882a593Smuzhiyun  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
27*4882a593Smuzhiyun  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
28*4882a593Smuzhiyun  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
29*4882a593Smuzhiyun  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
30*4882a593Smuzhiyun  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
31*4882a593Smuzhiyun  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
32*4882a593Smuzhiyun  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
33*4882a593Smuzhiyun  * SOFTWARE.
34*4882a593Smuzhiyun  */
35*4882a593Smuzhiyun /*
36*4882a593Smuzhiyun  * This file contains all of the code that is specific to the
37*4882a593Smuzhiyun  * QLogic_IB 6120 PCIe chip.
38*4882a593Smuzhiyun  */
39*4882a593Smuzhiyun 
40*4882a593Smuzhiyun #include <linux/interrupt.h>
41*4882a593Smuzhiyun #include <linux/pci.h>
42*4882a593Smuzhiyun #include <linux/delay.h>
43*4882a593Smuzhiyun #include <rdma/ib_verbs.h>
44*4882a593Smuzhiyun 
45*4882a593Smuzhiyun #include "qib.h"
46*4882a593Smuzhiyun #include "qib_6120_regs.h"
47*4882a593Smuzhiyun 
48*4882a593Smuzhiyun static void qib_6120_setup_setextled(struct qib_pportdata *, u32);
49*4882a593Smuzhiyun static void sendctrl_6120_mod(struct qib_pportdata *ppd, u32 op);
50*4882a593Smuzhiyun static u8 qib_6120_phys_portstate(u64);
51*4882a593Smuzhiyun static u32 qib_6120_iblink_state(u64);
52*4882a593Smuzhiyun 
53*4882a593Smuzhiyun /*
54*4882a593Smuzhiyun  * This file contains all the chip-specific register information and
55*4882a593Smuzhiyun  * access functions for the Intel Intel_IB PCI-Express chip.
56*4882a593Smuzhiyun  *
57*4882a593Smuzhiyun  */
58*4882a593Smuzhiyun 
59*4882a593Smuzhiyun /* KREG_IDX uses machine-generated #defines */
60*4882a593Smuzhiyun #define KREG_IDX(regname) (QIB_6120_##regname##_OFFS / sizeof(u64))
61*4882a593Smuzhiyun 
62*4882a593Smuzhiyun /* Use defines to tie machine-generated names to lower-case names */
63*4882a593Smuzhiyun #define kr_extctrl KREG_IDX(EXTCtrl)
64*4882a593Smuzhiyun #define kr_extstatus KREG_IDX(EXTStatus)
65*4882a593Smuzhiyun #define kr_gpio_clear KREG_IDX(GPIOClear)
66*4882a593Smuzhiyun #define kr_gpio_mask KREG_IDX(GPIOMask)
67*4882a593Smuzhiyun #define kr_gpio_out KREG_IDX(GPIOOut)
68*4882a593Smuzhiyun #define kr_gpio_status KREG_IDX(GPIOStatus)
69*4882a593Smuzhiyun #define kr_rcvctrl KREG_IDX(RcvCtrl)
70*4882a593Smuzhiyun #define kr_sendctrl KREG_IDX(SendCtrl)
71*4882a593Smuzhiyun #define kr_partitionkey KREG_IDX(RcvPartitionKey)
72*4882a593Smuzhiyun #define kr_hwdiagctrl KREG_IDX(HwDiagCtrl)
73*4882a593Smuzhiyun #define kr_ibcstatus KREG_IDX(IBCStatus)
74*4882a593Smuzhiyun #define kr_ibcctrl KREG_IDX(IBCCtrl)
75*4882a593Smuzhiyun #define kr_sendbuffererror KREG_IDX(SendBufErr0)
76*4882a593Smuzhiyun #define kr_rcvbthqp KREG_IDX(RcvBTHQP)
77*4882a593Smuzhiyun #define kr_counterregbase KREG_IDX(CntrRegBase)
78*4882a593Smuzhiyun #define kr_palign KREG_IDX(PageAlign)
79*4882a593Smuzhiyun #define kr_rcvegrbase KREG_IDX(RcvEgrBase)
80*4882a593Smuzhiyun #define kr_rcvegrcnt KREG_IDX(RcvEgrCnt)
81*4882a593Smuzhiyun #define kr_rcvhdrcnt KREG_IDX(RcvHdrCnt)
82*4882a593Smuzhiyun #define kr_rcvhdrentsize KREG_IDX(RcvHdrEntSize)
83*4882a593Smuzhiyun #define kr_rcvhdrsize KREG_IDX(RcvHdrSize)
84*4882a593Smuzhiyun #define kr_rcvtidbase KREG_IDX(RcvTIDBase)
85*4882a593Smuzhiyun #define kr_rcvtidcnt KREG_IDX(RcvTIDCnt)
86*4882a593Smuzhiyun #define kr_scratch KREG_IDX(Scratch)
87*4882a593Smuzhiyun #define kr_sendctrl KREG_IDX(SendCtrl)
88*4882a593Smuzhiyun #define kr_sendpioavailaddr KREG_IDX(SendPIOAvailAddr)
89*4882a593Smuzhiyun #define kr_sendpiobufbase KREG_IDX(SendPIOBufBase)
90*4882a593Smuzhiyun #define kr_sendpiobufcnt KREG_IDX(SendPIOBufCnt)
91*4882a593Smuzhiyun #define kr_sendpiosize KREG_IDX(SendPIOSize)
92*4882a593Smuzhiyun #define kr_sendregbase KREG_IDX(SendRegBase)
93*4882a593Smuzhiyun #define kr_userregbase KREG_IDX(UserRegBase)
94*4882a593Smuzhiyun #define kr_control KREG_IDX(Control)
95*4882a593Smuzhiyun #define kr_intclear KREG_IDX(IntClear)
96*4882a593Smuzhiyun #define kr_intmask KREG_IDX(IntMask)
97*4882a593Smuzhiyun #define kr_intstatus KREG_IDX(IntStatus)
98*4882a593Smuzhiyun #define kr_errclear KREG_IDX(ErrClear)
99*4882a593Smuzhiyun #define kr_errmask KREG_IDX(ErrMask)
100*4882a593Smuzhiyun #define kr_errstatus KREG_IDX(ErrStatus)
101*4882a593Smuzhiyun #define kr_hwerrclear KREG_IDX(HwErrClear)
102*4882a593Smuzhiyun #define kr_hwerrmask KREG_IDX(HwErrMask)
103*4882a593Smuzhiyun #define kr_hwerrstatus KREG_IDX(HwErrStatus)
104*4882a593Smuzhiyun #define kr_revision KREG_IDX(Revision)
105*4882a593Smuzhiyun #define kr_portcnt KREG_IDX(PortCnt)
106*4882a593Smuzhiyun #define kr_serdes_cfg0 KREG_IDX(SerdesCfg0)
107*4882a593Smuzhiyun #define kr_serdes_cfg1 (kr_serdes_cfg0 + 1)
108*4882a593Smuzhiyun #define kr_serdes_stat KREG_IDX(SerdesStat)
109*4882a593Smuzhiyun #define kr_xgxs_cfg KREG_IDX(XGXSCfg)
110*4882a593Smuzhiyun 
111*4882a593Smuzhiyun /* These must only be written via qib_write_kreg_ctxt() */
112*4882a593Smuzhiyun #define kr_rcvhdraddr KREG_IDX(RcvHdrAddr0)
113*4882a593Smuzhiyun #define kr_rcvhdrtailaddr KREG_IDX(RcvHdrTailAddr0)
114*4882a593Smuzhiyun 
115*4882a593Smuzhiyun #define CREG_IDX(regname) ((QIB_6120_##regname##_OFFS - \
116*4882a593Smuzhiyun 			QIB_6120_LBIntCnt_OFFS) / sizeof(u64))
117*4882a593Smuzhiyun 
118*4882a593Smuzhiyun #define cr_badformat CREG_IDX(RxBadFormatCnt)
119*4882a593Smuzhiyun #define cr_erricrc CREG_IDX(RxICRCErrCnt)
120*4882a593Smuzhiyun #define cr_errlink CREG_IDX(RxLinkProblemCnt)
121*4882a593Smuzhiyun #define cr_errlpcrc CREG_IDX(RxLPCRCErrCnt)
122*4882a593Smuzhiyun #define cr_errpkey CREG_IDX(RxPKeyMismatchCnt)
123*4882a593Smuzhiyun #define cr_rcvflowctrl_err CREG_IDX(RxFlowCtrlErrCnt)
124*4882a593Smuzhiyun #define cr_err_rlen CREG_IDX(RxLenErrCnt)
125*4882a593Smuzhiyun #define cr_errslen CREG_IDX(TxLenErrCnt)
126*4882a593Smuzhiyun #define cr_errtidfull CREG_IDX(RxTIDFullErrCnt)
127*4882a593Smuzhiyun #define cr_errtidvalid CREG_IDX(RxTIDValidErrCnt)
128*4882a593Smuzhiyun #define cr_errvcrc CREG_IDX(RxVCRCErrCnt)
129*4882a593Smuzhiyun #define cr_ibstatuschange CREG_IDX(IBStatusChangeCnt)
130*4882a593Smuzhiyun #define cr_lbint CREG_IDX(LBIntCnt)
131*4882a593Smuzhiyun #define cr_invalidrlen CREG_IDX(RxMaxMinLenErrCnt)
132*4882a593Smuzhiyun #define cr_invalidslen CREG_IDX(TxMaxMinLenErrCnt)
133*4882a593Smuzhiyun #define cr_lbflowstall CREG_IDX(LBFlowStallCnt)
134*4882a593Smuzhiyun #define cr_pktrcv CREG_IDX(RxDataPktCnt)
135*4882a593Smuzhiyun #define cr_pktrcvflowctrl CREG_IDX(RxFlowPktCnt)
136*4882a593Smuzhiyun #define cr_pktsend CREG_IDX(TxDataPktCnt)
137*4882a593Smuzhiyun #define cr_pktsendflow CREG_IDX(TxFlowPktCnt)
138*4882a593Smuzhiyun #define cr_portovfl CREG_IDX(RxP0HdrEgrOvflCnt)
139*4882a593Smuzhiyun #define cr_rcvebp CREG_IDX(RxEBPCnt)
140*4882a593Smuzhiyun #define cr_rcvovfl CREG_IDX(RxBufOvflCnt)
141*4882a593Smuzhiyun #define cr_senddropped CREG_IDX(TxDroppedPktCnt)
142*4882a593Smuzhiyun #define cr_sendstall CREG_IDX(TxFlowStallCnt)
143*4882a593Smuzhiyun #define cr_sendunderrun CREG_IDX(TxUnderrunCnt)
144*4882a593Smuzhiyun #define cr_wordrcv CREG_IDX(RxDwordCnt)
145*4882a593Smuzhiyun #define cr_wordsend CREG_IDX(TxDwordCnt)
146*4882a593Smuzhiyun #define cr_txunsupvl CREG_IDX(TxUnsupVLErrCnt)
147*4882a593Smuzhiyun #define cr_rxdroppkt CREG_IDX(RxDroppedPktCnt)
148*4882a593Smuzhiyun #define cr_iblinkerrrecov CREG_IDX(IBLinkErrRecoveryCnt)
149*4882a593Smuzhiyun #define cr_iblinkdown CREG_IDX(IBLinkDownedCnt)
150*4882a593Smuzhiyun #define cr_ibsymbolerr CREG_IDX(IBSymbolErrCnt)
151*4882a593Smuzhiyun 
152*4882a593Smuzhiyun #define SYM_RMASK(regname, fldname) ((u64)              \
153*4882a593Smuzhiyun 	QIB_6120_##regname##_##fldname##_RMASK)
154*4882a593Smuzhiyun #define SYM_MASK(regname, fldname) ((u64)               \
155*4882a593Smuzhiyun 	QIB_6120_##regname##_##fldname##_RMASK <<       \
156*4882a593Smuzhiyun 	 QIB_6120_##regname##_##fldname##_LSB)
157*4882a593Smuzhiyun #define SYM_LSB(regname, fldname) (QIB_6120_##regname##_##fldname##_LSB)
158*4882a593Smuzhiyun 
159*4882a593Smuzhiyun #define SYM_FIELD(value, regname, fldname) ((u64) \
160*4882a593Smuzhiyun 	(((value) >> SYM_LSB(regname, fldname)) & \
161*4882a593Smuzhiyun 	 SYM_RMASK(regname, fldname)))
162*4882a593Smuzhiyun #define ERR_MASK(fldname) SYM_MASK(ErrMask, fldname##Mask)
163*4882a593Smuzhiyun #define HWE_MASK(fldname) SYM_MASK(HwErrMask, fldname##Mask)
164*4882a593Smuzhiyun 
165*4882a593Smuzhiyun /* link training states, from IBC */
166*4882a593Smuzhiyun #define IB_6120_LT_STATE_DISABLED        0x00
167*4882a593Smuzhiyun #define IB_6120_LT_STATE_LINKUP          0x01
168*4882a593Smuzhiyun #define IB_6120_LT_STATE_POLLACTIVE      0x02
169*4882a593Smuzhiyun #define IB_6120_LT_STATE_POLLQUIET       0x03
170*4882a593Smuzhiyun #define IB_6120_LT_STATE_SLEEPDELAY      0x04
171*4882a593Smuzhiyun #define IB_6120_LT_STATE_SLEEPQUIET      0x05
172*4882a593Smuzhiyun #define IB_6120_LT_STATE_CFGDEBOUNCE     0x08
173*4882a593Smuzhiyun #define IB_6120_LT_STATE_CFGRCVFCFG      0x09
174*4882a593Smuzhiyun #define IB_6120_LT_STATE_CFGWAITRMT      0x0a
175*4882a593Smuzhiyun #define IB_6120_LT_STATE_CFGIDLE 0x0b
176*4882a593Smuzhiyun #define IB_6120_LT_STATE_RECOVERRETRAIN  0x0c
177*4882a593Smuzhiyun #define IB_6120_LT_STATE_RECOVERWAITRMT  0x0e
178*4882a593Smuzhiyun #define IB_6120_LT_STATE_RECOVERIDLE     0x0f
179*4882a593Smuzhiyun 
180*4882a593Smuzhiyun /* link state machine states from IBC */
181*4882a593Smuzhiyun #define IB_6120_L_STATE_DOWN             0x0
182*4882a593Smuzhiyun #define IB_6120_L_STATE_INIT             0x1
183*4882a593Smuzhiyun #define IB_6120_L_STATE_ARM              0x2
184*4882a593Smuzhiyun #define IB_6120_L_STATE_ACTIVE           0x3
185*4882a593Smuzhiyun #define IB_6120_L_STATE_ACT_DEFER        0x4
186*4882a593Smuzhiyun 
187*4882a593Smuzhiyun static const u8 qib_6120_physportstate[0x20] = {
188*4882a593Smuzhiyun 	[IB_6120_LT_STATE_DISABLED] = IB_PHYSPORTSTATE_DISABLED,
189*4882a593Smuzhiyun 	[IB_6120_LT_STATE_LINKUP] = IB_PHYSPORTSTATE_LINKUP,
190*4882a593Smuzhiyun 	[IB_6120_LT_STATE_POLLACTIVE] = IB_PHYSPORTSTATE_POLL,
191*4882a593Smuzhiyun 	[IB_6120_LT_STATE_POLLQUIET] = IB_PHYSPORTSTATE_POLL,
192*4882a593Smuzhiyun 	[IB_6120_LT_STATE_SLEEPDELAY] = IB_PHYSPORTSTATE_SLEEP,
193*4882a593Smuzhiyun 	[IB_6120_LT_STATE_SLEEPQUIET] = IB_PHYSPORTSTATE_SLEEP,
194*4882a593Smuzhiyun 	[IB_6120_LT_STATE_CFGDEBOUNCE] =
195*4882a593Smuzhiyun 		IB_PHYSPORTSTATE_CFG_TRAIN,
196*4882a593Smuzhiyun 	[IB_6120_LT_STATE_CFGRCVFCFG] =
197*4882a593Smuzhiyun 		IB_PHYSPORTSTATE_CFG_TRAIN,
198*4882a593Smuzhiyun 	[IB_6120_LT_STATE_CFGWAITRMT] =
199*4882a593Smuzhiyun 		IB_PHYSPORTSTATE_CFG_TRAIN,
200*4882a593Smuzhiyun 	[IB_6120_LT_STATE_CFGIDLE] = IB_PHYSPORTSTATE_CFG_TRAIN,
201*4882a593Smuzhiyun 	[IB_6120_LT_STATE_RECOVERRETRAIN] =
202*4882a593Smuzhiyun 		IB_PHYSPORTSTATE_LINK_ERR_RECOVER,
203*4882a593Smuzhiyun 	[IB_6120_LT_STATE_RECOVERWAITRMT] =
204*4882a593Smuzhiyun 		IB_PHYSPORTSTATE_LINK_ERR_RECOVER,
205*4882a593Smuzhiyun 	[IB_6120_LT_STATE_RECOVERIDLE] =
206*4882a593Smuzhiyun 		IB_PHYSPORTSTATE_LINK_ERR_RECOVER,
207*4882a593Smuzhiyun 	[0x10] = IB_PHYSPORTSTATE_CFG_TRAIN,
208*4882a593Smuzhiyun 	[0x11] = IB_PHYSPORTSTATE_CFG_TRAIN,
209*4882a593Smuzhiyun 	[0x12] = IB_PHYSPORTSTATE_CFG_TRAIN,
210*4882a593Smuzhiyun 	[0x13] = IB_PHYSPORTSTATE_CFG_TRAIN,
211*4882a593Smuzhiyun 	[0x14] = IB_PHYSPORTSTATE_CFG_TRAIN,
212*4882a593Smuzhiyun 	[0x15] = IB_PHYSPORTSTATE_CFG_TRAIN,
213*4882a593Smuzhiyun 	[0x16] = IB_PHYSPORTSTATE_CFG_TRAIN,
214*4882a593Smuzhiyun 	[0x17] = IB_PHYSPORTSTATE_CFG_TRAIN
215*4882a593Smuzhiyun };
216*4882a593Smuzhiyun 
217*4882a593Smuzhiyun 
218*4882a593Smuzhiyun struct qib_chip_specific {
219*4882a593Smuzhiyun 	u64 __iomem *cregbase;
220*4882a593Smuzhiyun 	u64 *cntrs;
221*4882a593Smuzhiyun 	u64 *portcntrs;
222*4882a593Smuzhiyun 	void *dummy_hdrq;   /* used after ctxt close */
223*4882a593Smuzhiyun 	dma_addr_t dummy_hdrq_phys;
224*4882a593Smuzhiyun 	spinlock_t kernel_tid_lock; /* no back to back kernel TID writes */
225*4882a593Smuzhiyun 	spinlock_t user_tid_lock; /* no back to back user TID writes */
226*4882a593Smuzhiyun 	spinlock_t rcvmod_lock; /* protect rcvctrl shadow changes */
227*4882a593Smuzhiyun 	spinlock_t gpio_lock; /* RMW of shadows/regs for ExtCtrl and GPIO */
228*4882a593Smuzhiyun 	u64 hwerrmask;
229*4882a593Smuzhiyun 	u64 errormask;
230*4882a593Smuzhiyun 	u64 gpio_out; /* shadow of kr_gpio_out, for rmw ops */
231*4882a593Smuzhiyun 	u64 gpio_mask; /* shadow the gpio mask register */
232*4882a593Smuzhiyun 	u64 extctrl; /* shadow the gpio output enable, etc... */
233*4882a593Smuzhiyun 	/*
234*4882a593Smuzhiyun 	 * these 5 fields are used to establish deltas for IB symbol
235*4882a593Smuzhiyun 	 * errors and linkrecovery errors.  They can be reported on
236*4882a593Smuzhiyun 	 * some chips during link negotiation prior to INIT, and with
237*4882a593Smuzhiyun 	 * DDR when faking DDR negotiations with non-IBTA switches.
238*4882a593Smuzhiyun 	 * The chip counters are adjusted at driver unload if there is
239*4882a593Smuzhiyun 	 * a non-zero delta.
240*4882a593Smuzhiyun 	 */
241*4882a593Smuzhiyun 	u64 ibdeltainprog;
242*4882a593Smuzhiyun 	u64 ibsymdelta;
243*4882a593Smuzhiyun 	u64 ibsymsnap;
244*4882a593Smuzhiyun 	u64 iblnkerrdelta;
245*4882a593Smuzhiyun 	u64 iblnkerrsnap;
246*4882a593Smuzhiyun 	u64 ibcctrl; /* shadow for kr_ibcctrl */
247*4882a593Smuzhiyun 	u32 lastlinkrecov; /* link recovery issue */
248*4882a593Smuzhiyun 	u32 cntrnamelen;
249*4882a593Smuzhiyun 	u32 portcntrnamelen;
250*4882a593Smuzhiyun 	u32 ncntrs;
251*4882a593Smuzhiyun 	u32 nportcntrs;
252*4882a593Smuzhiyun 	/* used with gpio interrupts to implement IB counters */
253*4882a593Smuzhiyun 	u32 rxfc_unsupvl_errs;
254*4882a593Smuzhiyun 	u32 overrun_thresh_errs;
255*4882a593Smuzhiyun 	/*
256*4882a593Smuzhiyun 	 * these count only cases where _successive_ LocalLinkIntegrity
257*4882a593Smuzhiyun 	 * errors were seen in the receive headers of IB standard packets
258*4882a593Smuzhiyun 	 */
259*4882a593Smuzhiyun 	u32 lli_errs;
260*4882a593Smuzhiyun 	u32 lli_counter;
261*4882a593Smuzhiyun 	u64 lli_thresh;
262*4882a593Smuzhiyun 	u64 sword; /* total dwords sent (sample result) */
263*4882a593Smuzhiyun 	u64 rword; /* total dwords received (sample result) */
264*4882a593Smuzhiyun 	u64 spkts; /* total packets sent (sample result) */
265*4882a593Smuzhiyun 	u64 rpkts; /* total packets received (sample result) */
266*4882a593Smuzhiyun 	u64 xmit_wait; /* # of ticks no data sent (sample result) */
267*4882a593Smuzhiyun 	struct timer_list pma_timer;
268*4882a593Smuzhiyun 	struct qib_pportdata *ppd;
269*4882a593Smuzhiyun 	char emsgbuf[128];
270*4882a593Smuzhiyun 	char bitsmsgbuf[64];
271*4882a593Smuzhiyun 	u8 pma_sample_status;
272*4882a593Smuzhiyun };
273*4882a593Smuzhiyun 
274*4882a593Smuzhiyun /* ibcctrl bits */
275*4882a593Smuzhiyun #define QLOGIC_IB_IBCC_LINKINITCMD_DISABLE 1
276*4882a593Smuzhiyun /* cycle through TS1/TS2 till OK */
277*4882a593Smuzhiyun #define QLOGIC_IB_IBCC_LINKINITCMD_POLL 2
278*4882a593Smuzhiyun /* wait for TS1, then go on */
279*4882a593Smuzhiyun #define QLOGIC_IB_IBCC_LINKINITCMD_SLEEP 3
280*4882a593Smuzhiyun #define QLOGIC_IB_IBCC_LINKINITCMD_SHIFT 16
281*4882a593Smuzhiyun 
282*4882a593Smuzhiyun #define QLOGIC_IB_IBCC_LINKCMD_DOWN 1           /* move to 0x11 */
283*4882a593Smuzhiyun #define QLOGIC_IB_IBCC_LINKCMD_ARMED 2          /* move to 0x21 */
284*4882a593Smuzhiyun #define QLOGIC_IB_IBCC_LINKCMD_ACTIVE 3 /* move to 0x31 */
285*4882a593Smuzhiyun #define QLOGIC_IB_IBCC_LINKCMD_SHIFT 18
286*4882a593Smuzhiyun 
287*4882a593Smuzhiyun /*
288*4882a593Smuzhiyun  * We could have a single register get/put routine, that takes a group type,
289*4882a593Smuzhiyun  * but this is somewhat clearer and cleaner.  It also gives us some error
290*4882a593Smuzhiyun  * checking.  64 bit register reads should always work, but are inefficient
291*4882a593Smuzhiyun  * on opteron (the northbridge always generates 2 separate HT 32 bit reads),
292*4882a593Smuzhiyun  * so we use kreg32 wherever possible.  User register and counter register
293*4882a593Smuzhiyun  * reads are always 32 bit reads, so only one form of those routines.
294*4882a593Smuzhiyun  */
295*4882a593Smuzhiyun 
296*4882a593Smuzhiyun /**
297*4882a593Smuzhiyun  * qib_read_ureg32 - read 32-bit virtualized per-context register
298*4882a593Smuzhiyun  * @dd: device
299*4882a593Smuzhiyun  * @regno: register number
300*4882a593Smuzhiyun  * @ctxt: context number
301*4882a593Smuzhiyun  *
302*4882a593Smuzhiyun  * Return the contents of a register that is virtualized to be per context.
303*4882a593Smuzhiyun  * Returns -1 on errors (not distinguishable from valid contents at
304*4882a593Smuzhiyun  * runtime; we may add a separate error variable at some point).
305*4882a593Smuzhiyun  */
qib_read_ureg32(const struct qib_devdata * dd,enum qib_ureg regno,int ctxt)306*4882a593Smuzhiyun static inline u32 qib_read_ureg32(const struct qib_devdata *dd,
307*4882a593Smuzhiyun 				  enum qib_ureg regno, int ctxt)
308*4882a593Smuzhiyun {
309*4882a593Smuzhiyun 	if (!dd->kregbase || !(dd->flags & QIB_PRESENT))
310*4882a593Smuzhiyun 		return 0;
311*4882a593Smuzhiyun 
312*4882a593Smuzhiyun 	if (dd->userbase)
313*4882a593Smuzhiyun 		return readl(regno + (u64 __iomem *)
314*4882a593Smuzhiyun 			     ((char __iomem *)dd->userbase +
315*4882a593Smuzhiyun 			      dd->ureg_align * ctxt));
316*4882a593Smuzhiyun 	else
317*4882a593Smuzhiyun 		return readl(regno + (u64 __iomem *)
318*4882a593Smuzhiyun 			     (dd->uregbase +
319*4882a593Smuzhiyun 			      (char __iomem *)dd->kregbase +
320*4882a593Smuzhiyun 			      dd->ureg_align * ctxt));
321*4882a593Smuzhiyun }
322*4882a593Smuzhiyun 
323*4882a593Smuzhiyun /**
324*4882a593Smuzhiyun  * qib_write_ureg - write 32-bit virtualized per-context register
325*4882a593Smuzhiyun  * @dd: device
326*4882a593Smuzhiyun  * @regno: register number
327*4882a593Smuzhiyun  * @value: value
328*4882a593Smuzhiyun  * @ctxt: context
329*4882a593Smuzhiyun  *
330*4882a593Smuzhiyun  * Write the contents of a register that is virtualized to be per context.
331*4882a593Smuzhiyun  */
qib_write_ureg(const struct qib_devdata * dd,enum qib_ureg regno,u64 value,int ctxt)332*4882a593Smuzhiyun static inline void qib_write_ureg(const struct qib_devdata *dd,
333*4882a593Smuzhiyun 				  enum qib_ureg regno, u64 value, int ctxt)
334*4882a593Smuzhiyun {
335*4882a593Smuzhiyun 	u64 __iomem *ubase;
336*4882a593Smuzhiyun 
337*4882a593Smuzhiyun 	if (dd->userbase)
338*4882a593Smuzhiyun 		ubase = (u64 __iomem *)
339*4882a593Smuzhiyun 			((char __iomem *) dd->userbase +
340*4882a593Smuzhiyun 			 dd->ureg_align * ctxt);
341*4882a593Smuzhiyun 	else
342*4882a593Smuzhiyun 		ubase = (u64 __iomem *)
343*4882a593Smuzhiyun 			(dd->uregbase +
344*4882a593Smuzhiyun 			 (char __iomem *) dd->kregbase +
345*4882a593Smuzhiyun 			 dd->ureg_align * ctxt);
346*4882a593Smuzhiyun 
347*4882a593Smuzhiyun 	if (dd->kregbase && (dd->flags & QIB_PRESENT))
348*4882a593Smuzhiyun 		writeq(value, &ubase[regno]);
349*4882a593Smuzhiyun }
350*4882a593Smuzhiyun 
qib_read_kreg32(const struct qib_devdata * dd,const u16 regno)351*4882a593Smuzhiyun static inline u32 qib_read_kreg32(const struct qib_devdata *dd,
352*4882a593Smuzhiyun 				  const u16 regno)
353*4882a593Smuzhiyun {
354*4882a593Smuzhiyun 	if (!dd->kregbase || !(dd->flags & QIB_PRESENT))
355*4882a593Smuzhiyun 		return -1;
356*4882a593Smuzhiyun 	return readl((u32 __iomem *)&dd->kregbase[regno]);
357*4882a593Smuzhiyun }
358*4882a593Smuzhiyun 
qib_read_kreg64(const struct qib_devdata * dd,const u16 regno)359*4882a593Smuzhiyun static inline u64 qib_read_kreg64(const struct qib_devdata *dd,
360*4882a593Smuzhiyun 				  const u16 regno)
361*4882a593Smuzhiyun {
362*4882a593Smuzhiyun 	if (!dd->kregbase || !(dd->flags & QIB_PRESENT))
363*4882a593Smuzhiyun 		return -1;
364*4882a593Smuzhiyun 
365*4882a593Smuzhiyun 	return readq(&dd->kregbase[regno]);
366*4882a593Smuzhiyun }
367*4882a593Smuzhiyun 
qib_write_kreg(const struct qib_devdata * dd,const u16 regno,u64 value)368*4882a593Smuzhiyun static inline void qib_write_kreg(const struct qib_devdata *dd,
369*4882a593Smuzhiyun 				  const u16 regno, u64 value)
370*4882a593Smuzhiyun {
371*4882a593Smuzhiyun 	if (dd->kregbase && (dd->flags & QIB_PRESENT))
372*4882a593Smuzhiyun 		writeq(value, &dd->kregbase[regno]);
373*4882a593Smuzhiyun }
374*4882a593Smuzhiyun 
375*4882a593Smuzhiyun /**
376*4882a593Smuzhiyun  * qib_write_kreg_ctxt - write a device's per-ctxt 64-bit kernel register
377*4882a593Smuzhiyun  * @dd: the qlogic_ib device
378*4882a593Smuzhiyun  * @regno: the register number to write
379*4882a593Smuzhiyun  * @ctxt: the context containing the register
380*4882a593Smuzhiyun  * @value: the value to write
381*4882a593Smuzhiyun  */
qib_write_kreg_ctxt(const struct qib_devdata * dd,const u16 regno,unsigned ctxt,u64 value)382*4882a593Smuzhiyun static inline void qib_write_kreg_ctxt(const struct qib_devdata *dd,
383*4882a593Smuzhiyun 				       const u16 regno, unsigned ctxt,
384*4882a593Smuzhiyun 				       u64 value)
385*4882a593Smuzhiyun {
386*4882a593Smuzhiyun 	qib_write_kreg(dd, regno + ctxt, value);
387*4882a593Smuzhiyun }
388*4882a593Smuzhiyun 
write_6120_creg(const struct qib_devdata * dd,u16 regno,u64 value)389*4882a593Smuzhiyun static inline void write_6120_creg(const struct qib_devdata *dd,
390*4882a593Smuzhiyun 				   u16 regno, u64 value)
391*4882a593Smuzhiyun {
392*4882a593Smuzhiyun 	if (dd->cspec->cregbase && (dd->flags & QIB_PRESENT))
393*4882a593Smuzhiyun 		writeq(value, &dd->cspec->cregbase[regno]);
394*4882a593Smuzhiyun }
395*4882a593Smuzhiyun 
read_6120_creg(const struct qib_devdata * dd,u16 regno)396*4882a593Smuzhiyun static inline u64 read_6120_creg(const struct qib_devdata *dd, u16 regno)
397*4882a593Smuzhiyun {
398*4882a593Smuzhiyun 	if (!dd->cspec->cregbase || !(dd->flags & QIB_PRESENT))
399*4882a593Smuzhiyun 		return 0;
400*4882a593Smuzhiyun 	return readq(&dd->cspec->cregbase[regno]);
401*4882a593Smuzhiyun }
402*4882a593Smuzhiyun 
read_6120_creg32(const struct qib_devdata * dd,u16 regno)403*4882a593Smuzhiyun static inline u32 read_6120_creg32(const struct qib_devdata *dd, u16 regno)
404*4882a593Smuzhiyun {
405*4882a593Smuzhiyun 	if (!dd->cspec->cregbase || !(dd->flags & QIB_PRESENT))
406*4882a593Smuzhiyun 		return 0;
407*4882a593Smuzhiyun 	return readl(&dd->cspec->cregbase[regno]);
408*4882a593Smuzhiyun }
409*4882a593Smuzhiyun 
410*4882a593Smuzhiyun /* kr_control bits */
411*4882a593Smuzhiyun #define QLOGIC_IB_C_RESET 1U
412*4882a593Smuzhiyun 
413*4882a593Smuzhiyun /* kr_intstatus, kr_intclear, kr_intmask bits */
414*4882a593Smuzhiyun #define QLOGIC_IB_I_RCVURG_MASK ((1U << 5) - 1)
415*4882a593Smuzhiyun #define QLOGIC_IB_I_RCVURG_SHIFT 0
416*4882a593Smuzhiyun #define QLOGIC_IB_I_RCVAVAIL_MASK ((1U << 5) - 1)
417*4882a593Smuzhiyun #define QLOGIC_IB_I_RCVAVAIL_SHIFT 12
418*4882a593Smuzhiyun 
419*4882a593Smuzhiyun #define QLOGIC_IB_C_FREEZEMODE 0x00000002
420*4882a593Smuzhiyun #define QLOGIC_IB_C_LINKENABLE 0x00000004
421*4882a593Smuzhiyun #define QLOGIC_IB_I_ERROR               0x0000000080000000ULL
422*4882a593Smuzhiyun #define QLOGIC_IB_I_SPIOSENT            0x0000000040000000ULL
423*4882a593Smuzhiyun #define QLOGIC_IB_I_SPIOBUFAVAIL        0x0000000020000000ULL
424*4882a593Smuzhiyun #define QLOGIC_IB_I_GPIO                0x0000000010000000ULL
425*4882a593Smuzhiyun #define QLOGIC_IB_I_BITSEXTANT \
426*4882a593Smuzhiyun 		((QLOGIC_IB_I_RCVURG_MASK << QLOGIC_IB_I_RCVURG_SHIFT) | \
427*4882a593Smuzhiyun 		(QLOGIC_IB_I_RCVAVAIL_MASK << \
428*4882a593Smuzhiyun 		 QLOGIC_IB_I_RCVAVAIL_SHIFT) | \
429*4882a593Smuzhiyun 		QLOGIC_IB_I_ERROR | QLOGIC_IB_I_SPIOSENT | \
430*4882a593Smuzhiyun 		QLOGIC_IB_I_SPIOBUFAVAIL | QLOGIC_IB_I_GPIO)
431*4882a593Smuzhiyun 
432*4882a593Smuzhiyun /* kr_hwerrclear, kr_hwerrmask, kr_hwerrstatus, bits */
433*4882a593Smuzhiyun #define QLOGIC_IB_HWE_PCIEMEMPARITYERR_MASK  0x000000000000003fULL
434*4882a593Smuzhiyun #define QLOGIC_IB_HWE_PCIEMEMPARITYERR_SHIFT 0
435*4882a593Smuzhiyun #define QLOGIC_IB_HWE_PCIEPOISONEDTLP      0x0000000010000000ULL
436*4882a593Smuzhiyun #define QLOGIC_IB_HWE_PCIECPLTIMEOUT       0x0000000020000000ULL
437*4882a593Smuzhiyun #define QLOGIC_IB_HWE_PCIEBUSPARITYXTLH    0x0000000040000000ULL
438*4882a593Smuzhiyun #define QLOGIC_IB_HWE_PCIEBUSPARITYXADM    0x0000000080000000ULL
439*4882a593Smuzhiyun #define QLOGIC_IB_HWE_PCIEBUSPARITYRADM    0x0000000100000000ULL
440*4882a593Smuzhiyun #define QLOGIC_IB_HWE_COREPLL_FBSLIP       0x0080000000000000ULL
441*4882a593Smuzhiyun #define QLOGIC_IB_HWE_COREPLL_RFSLIP       0x0100000000000000ULL
442*4882a593Smuzhiyun #define QLOGIC_IB_HWE_PCIE1PLLFAILED       0x0400000000000000ULL
443*4882a593Smuzhiyun #define QLOGIC_IB_HWE_PCIE0PLLFAILED       0x0800000000000000ULL
444*4882a593Smuzhiyun #define QLOGIC_IB_HWE_SERDESPLLFAILED      0x1000000000000000ULL
445*4882a593Smuzhiyun 
446*4882a593Smuzhiyun 
447*4882a593Smuzhiyun /* kr_extstatus bits */
448*4882a593Smuzhiyun #define QLOGIC_IB_EXTS_FREQSEL 0x2
449*4882a593Smuzhiyun #define QLOGIC_IB_EXTS_SERDESSEL 0x4
450*4882a593Smuzhiyun #define QLOGIC_IB_EXTS_MEMBIST_ENDTEST     0x0000000000004000
451*4882a593Smuzhiyun #define QLOGIC_IB_EXTS_MEMBIST_FOUND       0x0000000000008000
452*4882a593Smuzhiyun 
453*4882a593Smuzhiyun /* kr_xgxsconfig bits */
454*4882a593Smuzhiyun #define QLOGIC_IB_XGXS_RESET          0x5ULL
455*4882a593Smuzhiyun 
456*4882a593Smuzhiyun #define _QIB_GPIO_SDA_NUM 1
457*4882a593Smuzhiyun #define _QIB_GPIO_SCL_NUM 0
458*4882a593Smuzhiyun 
459*4882a593Smuzhiyun /* Bits in GPIO for the added IB link interrupts */
460*4882a593Smuzhiyun #define GPIO_RXUVL_BIT 3
461*4882a593Smuzhiyun #define GPIO_OVRUN_BIT 4
462*4882a593Smuzhiyun #define GPIO_LLI_BIT 5
463*4882a593Smuzhiyun #define GPIO_ERRINTR_MASK 0x38
464*4882a593Smuzhiyun 
465*4882a593Smuzhiyun 
466*4882a593Smuzhiyun #define QLOGIC_IB_RT_BUFSIZE_MASK 0xe0000000ULL
467*4882a593Smuzhiyun #define QLOGIC_IB_RT_BUFSIZE_SHIFTVAL(tid) \
468*4882a593Smuzhiyun 	((((tid) & QLOGIC_IB_RT_BUFSIZE_MASK) >> 29) + 11 - 1)
469*4882a593Smuzhiyun #define QLOGIC_IB_RT_BUFSIZE(tid) (1 << QLOGIC_IB_RT_BUFSIZE_SHIFTVAL(tid))
470*4882a593Smuzhiyun #define QLOGIC_IB_RT_IS_VALID(tid) \
471*4882a593Smuzhiyun 	(((tid) & QLOGIC_IB_RT_BUFSIZE_MASK) && \
472*4882a593Smuzhiyun 	 ((((tid) & QLOGIC_IB_RT_BUFSIZE_MASK) != QLOGIC_IB_RT_BUFSIZE_MASK)))
473*4882a593Smuzhiyun #define QLOGIC_IB_RT_ADDR_MASK 0x1FFFFFFFULL /* 29 bits valid */
474*4882a593Smuzhiyun #define QLOGIC_IB_RT_ADDR_SHIFT 10
475*4882a593Smuzhiyun 
476*4882a593Smuzhiyun #define QLOGIC_IB_R_INTRAVAIL_SHIFT 16
477*4882a593Smuzhiyun #define QLOGIC_IB_R_TAILUPD_SHIFT 31
478*4882a593Smuzhiyun #define IBA6120_R_PKEY_DIS_SHIFT 30
479*4882a593Smuzhiyun 
480*4882a593Smuzhiyun #define PBC_6120_VL15_SEND_CTRL (1ULL << 31) /* pbc; VL15; link_buf only */
481*4882a593Smuzhiyun 
482*4882a593Smuzhiyun #define IBCBUSFRSPCPARITYERR HWE_MASK(IBCBusFromSPCParityErr)
483*4882a593Smuzhiyun #define IBCBUSTOSPCPARITYERR HWE_MASK(IBCBusToSPCParityErr)
484*4882a593Smuzhiyun 
485*4882a593Smuzhiyun #define SYM_MASK_BIT(regname, fldname, bit) ((u64) \
486*4882a593Smuzhiyun 	((1ULL << (SYM_LSB(regname, fldname) + (bit)))))
487*4882a593Smuzhiyun 
488*4882a593Smuzhiyun #define TXEMEMPARITYERR_PIOBUF \
489*4882a593Smuzhiyun 	SYM_MASK_BIT(HwErrMask, TXEMemParityErrMask, 0)
490*4882a593Smuzhiyun #define TXEMEMPARITYERR_PIOPBC \
491*4882a593Smuzhiyun 	SYM_MASK_BIT(HwErrMask, TXEMemParityErrMask, 1)
492*4882a593Smuzhiyun #define TXEMEMPARITYERR_PIOLAUNCHFIFO \
493*4882a593Smuzhiyun 	SYM_MASK_BIT(HwErrMask, TXEMemParityErrMask, 2)
494*4882a593Smuzhiyun 
495*4882a593Smuzhiyun #define RXEMEMPARITYERR_RCVBUF \
496*4882a593Smuzhiyun 	SYM_MASK_BIT(HwErrMask, RXEMemParityErrMask, 0)
497*4882a593Smuzhiyun #define RXEMEMPARITYERR_LOOKUPQ \
498*4882a593Smuzhiyun 	SYM_MASK_BIT(HwErrMask, RXEMemParityErrMask, 1)
499*4882a593Smuzhiyun #define RXEMEMPARITYERR_EXPTID \
500*4882a593Smuzhiyun 	SYM_MASK_BIT(HwErrMask, RXEMemParityErrMask, 2)
501*4882a593Smuzhiyun #define RXEMEMPARITYERR_EAGERTID \
502*4882a593Smuzhiyun 	SYM_MASK_BIT(HwErrMask, RXEMemParityErrMask, 3)
503*4882a593Smuzhiyun #define RXEMEMPARITYERR_FLAGBUF \
504*4882a593Smuzhiyun 	SYM_MASK_BIT(HwErrMask, RXEMemParityErrMask, 4)
505*4882a593Smuzhiyun #define RXEMEMPARITYERR_DATAINFO \
506*4882a593Smuzhiyun 	SYM_MASK_BIT(HwErrMask, RXEMemParityErrMask, 5)
507*4882a593Smuzhiyun #define RXEMEMPARITYERR_HDRINFO \
508*4882a593Smuzhiyun 	SYM_MASK_BIT(HwErrMask, RXEMemParityErrMask, 6)
509*4882a593Smuzhiyun 
510*4882a593Smuzhiyun /* 6120 specific hardware errors... */
511*4882a593Smuzhiyun static const struct qib_hwerror_msgs qib_6120_hwerror_msgs[] = {
512*4882a593Smuzhiyun 	/* generic hardware errors */
513*4882a593Smuzhiyun 	QLOGIC_IB_HWE_MSG(IBCBUSFRSPCPARITYERR, "QIB2IB Parity"),
514*4882a593Smuzhiyun 	QLOGIC_IB_HWE_MSG(IBCBUSTOSPCPARITYERR, "IB2QIB Parity"),
515*4882a593Smuzhiyun 
516*4882a593Smuzhiyun 	QLOGIC_IB_HWE_MSG(TXEMEMPARITYERR_PIOBUF,
517*4882a593Smuzhiyun 			  "TXE PIOBUF Memory Parity"),
518*4882a593Smuzhiyun 	QLOGIC_IB_HWE_MSG(TXEMEMPARITYERR_PIOPBC,
519*4882a593Smuzhiyun 			  "TXE PIOPBC Memory Parity"),
520*4882a593Smuzhiyun 	QLOGIC_IB_HWE_MSG(TXEMEMPARITYERR_PIOLAUNCHFIFO,
521*4882a593Smuzhiyun 			  "TXE PIOLAUNCHFIFO Memory Parity"),
522*4882a593Smuzhiyun 
523*4882a593Smuzhiyun 	QLOGIC_IB_HWE_MSG(RXEMEMPARITYERR_RCVBUF,
524*4882a593Smuzhiyun 			  "RXE RCVBUF Memory Parity"),
525*4882a593Smuzhiyun 	QLOGIC_IB_HWE_MSG(RXEMEMPARITYERR_LOOKUPQ,
526*4882a593Smuzhiyun 			  "RXE LOOKUPQ Memory Parity"),
527*4882a593Smuzhiyun 	QLOGIC_IB_HWE_MSG(RXEMEMPARITYERR_EAGERTID,
528*4882a593Smuzhiyun 			  "RXE EAGERTID Memory Parity"),
529*4882a593Smuzhiyun 	QLOGIC_IB_HWE_MSG(RXEMEMPARITYERR_EXPTID,
530*4882a593Smuzhiyun 			  "RXE EXPTID Memory Parity"),
531*4882a593Smuzhiyun 	QLOGIC_IB_HWE_MSG(RXEMEMPARITYERR_FLAGBUF,
532*4882a593Smuzhiyun 			  "RXE FLAGBUF Memory Parity"),
533*4882a593Smuzhiyun 	QLOGIC_IB_HWE_MSG(RXEMEMPARITYERR_DATAINFO,
534*4882a593Smuzhiyun 			  "RXE DATAINFO Memory Parity"),
535*4882a593Smuzhiyun 	QLOGIC_IB_HWE_MSG(RXEMEMPARITYERR_HDRINFO,
536*4882a593Smuzhiyun 			  "RXE HDRINFO Memory Parity"),
537*4882a593Smuzhiyun 
538*4882a593Smuzhiyun 	/* chip-specific hardware errors */
539*4882a593Smuzhiyun 	QLOGIC_IB_HWE_MSG(QLOGIC_IB_HWE_PCIEPOISONEDTLP,
540*4882a593Smuzhiyun 			  "PCIe Poisoned TLP"),
541*4882a593Smuzhiyun 	QLOGIC_IB_HWE_MSG(QLOGIC_IB_HWE_PCIECPLTIMEOUT,
542*4882a593Smuzhiyun 			  "PCIe completion timeout"),
543*4882a593Smuzhiyun 	/*
544*4882a593Smuzhiyun 	 * In practice, it's unlikely wthat we'll see PCIe PLL, or bus
545*4882a593Smuzhiyun 	 * parity or memory parity error failures, because most likely we
546*4882a593Smuzhiyun 	 * won't be able to talk to the core of the chip.  Nonetheless, we
547*4882a593Smuzhiyun 	 * might see them, if they are in parts of the PCIe core that aren't
548*4882a593Smuzhiyun 	 * essential.
549*4882a593Smuzhiyun 	 */
550*4882a593Smuzhiyun 	QLOGIC_IB_HWE_MSG(QLOGIC_IB_HWE_PCIE1PLLFAILED,
551*4882a593Smuzhiyun 			  "PCIePLL1"),
552*4882a593Smuzhiyun 	QLOGIC_IB_HWE_MSG(QLOGIC_IB_HWE_PCIE0PLLFAILED,
553*4882a593Smuzhiyun 			  "PCIePLL0"),
554*4882a593Smuzhiyun 	QLOGIC_IB_HWE_MSG(QLOGIC_IB_HWE_PCIEBUSPARITYXTLH,
555*4882a593Smuzhiyun 			  "PCIe XTLH core parity"),
556*4882a593Smuzhiyun 	QLOGIC_IB_HWE_MSG(QLOGIC_IB_HWE_PCIEBUSPARITYXADM,
557*4882a593Smuzhiyun 			  "PCIe ADM TX core parity"),
558*4882a593Smuzhiyun 	QLOGIC_IB_HWE_MSG(QLOGIC_IB_HWE_PCIEBUSPARITYRADM,
559*4882a593Smuzhiyun 			  "PCIe ADM RX core parity"),
560*4882a593Smuzhiyun 	QLOGIC_IB_HWE_MSG(QLOGIC_IB_HWE_SERDESPLLFAILED,
561*4882a593Smuzhiyun 			  "SerDes PLL"),
562*4882a593Smuzhiyun };
563*4882a593Smuzhiyun 
564*4882a593Smuzhiyun #define TXE_PIO_PARITY (TXEMEMPARITYERR_PIOBUF | TXEMEMPARITYERR_PIOPBC)
565*4882a593Smuzhiyun #define _QIB_PLL_FAIL (QLOGIC_IB_HWE_COREPLL_FBSLIP |   \
566*4882a593Smuzhiyun 		QLOGIC_IB_HWE_COREPLL_RFSLIP)
567*4882a593Smuzhiyun 
568*4882a593Smuzhiyun 	/* variables for sanity checking interrupt and errors */
569*4882a593Smuzhiyun #define IB_HWE_BITSEXTANT \
570*4882a593Smuzhiyun 	(HWE_MASK(RXEMemParityErr) |					\
571*4882a593Smuzhiyun 	 HWE_MASK(TXEMemParityErr) |					\
572*4882a593Smuzhiyun 	 (QLOGIC_IB_HWE_PCIEMEMPARITYERR_MASK <<			\
573*4882a593Smuzhiyun 	  QLOGIC_IB_HWE_PCIEMEMPARITYERR_SHIFT) |			\
574*4882a593Smuzhiyun 	 QLOGIC_IB_HWE_PCIE1PLLFAILED |					\
575*4882a593Smuzhiyun 	 QLOGIC_IB_HWE_PCIE0PLLFAILED |					\
576*4882a593Smuzhiyun 	 QLOGIC_IB_HWE_PCIEPOISONEDTLP |				\
577*4882a593Smuzhiyun 	 QLOGIC_IB_HWE_PCIECPLTIMEOUT |					\
578*4882a593Smuzhiyun 	 QLOGIC_IB_HWE_PCIEBUSPARITYXTLH |				\
579*4882a593Smuzhiyun 	 QLOGIC_IB_HWE_PCIEBUSPARITYXADM |				\
580*4882a593Smuzhiyun 	 QLOGIC_IB_HWE_PCIEBUSPARITYRADM |				\
581*4882a593Smuzhiyun 	 HWE_MASK(PowerOnBISTFailed) |					\
582*4882a593Smuzhiyun 	 QLOGIC_IB_HWE_COREPLL_FBSLIP |					\
583*4882a593Smuzhiyun 	 QLOGIC_IB_HWE_COREPLL_RFSLIP |					\
584*4882a593Smuzhiyun 	 QLOGIC_IB_HWE_SERDESPLLFAILED |				\
585*4882a593Smuzhiyun 	 HWE_MASK(IBCBusToSPCParityErr) |				\
586*4882a593Smuzhiyun 	 HWE_MASK(IBCBusFromSPCParityErr))
587*4882a593Smuzhiyun 
588*4882a593Smuzhiyun #define IB_E_BITSEXTANT \
589*4882a593Smuzhiyun 	(ERR_MASK(RcvFormatErr) | ERR_MASK(RcvVCRCErr) |		\
590*4882a593Smuzhiyun 	 ERR_MASK(RcvICRCErr) | ERR_MASK(RcvMinPktLenErr) |		\
591*4882a593Smuzhiyun 	 ERR_MASK(RcvMaxPktLenErr) | ERR_MASK(RcvLongPktLenErr) |	\
592*4882a593Smuzhiyun 	 ERR_MASK(RcvShortPktLenErr) | ERR_MASK(RcvUnexpectedCharErr) | \
593*4882a593Smuzhiyun 	 ERR_MASK(RcvUnsupportedVLErr) | ERR_MASK(RcvEBPErr) |		\
594*4882a593Smuzhiyun 	 ERR_MASK(RcvIBFlowErr) | ERR_MASK(RcvBadVersionErr) |		\
595*4882a593Smuzhiyun 	 ERR_MASK(RcvEgrFullErr) | ERR_MASK(RcvHdrFullErr) |		\
596*4882a593Smuzhiyun 	 ERR_MASK(RcvBadTidErr) | ERR_MASK(RcvHdrLenErr) |		\
597*4882a593Smuzhiyun 	 ERR_MASK(RcvHdrErr) | ERR_MASK(RcvIBLostLinkErr) |		\
598*4882a593Smuzhiyun 	 ERR_MASK(SendMinPktLenErr) | ERR_MASK(SendMaxPktLenErr) |	\
599*4882a593Smuzhiyun 	 ERR_MASK(SendUnderRunErr) | ERR_MASK(SendPktLenErr) |		\
600*4882a593Smuzhiyun 	 ERR_MASK(SendDroppedSmpPktErr) |				\
601*4882a593Smuzhiyun 	 ERR_MASK(SendDroppedDataPktErr) |				\
602*4882a593Smuzhiyun 	 ERR_MASK(SendPioArmLaunchErr) |				\
603*4882a593Smuzhiyun 	 ERR_MASK(SendUnexpectedPktNumErr) |				\
604*4882a593Smuzhiyun 	 ERR_MASK(SendUnsupportedVLErr) | ERR_MASK(IBStatusChanged) |	\
605*4882a593Smuzhiyun 	 ERR_MASK(InvalidAddrErr) | ERR_MASK(ResetNegated) |		\
606*4882a593Smuzhiyun 	 ERR_MASK(HardwareErr))
607*4882a593Smuzhiyun 
608*4882a593Smuzhiyun #define QLOGIC_IB_E_PKTERRS ( \
609*4882a593Smuzhiyun 		ERR_MASK(SendPktLenErr) |				\
610*4882a593Smuzhiyun 		ERR_MASK(SendDroppedDataPktErr) |			\
611*4882a593Smuzhiyun 		ERR_MASK(RcvVCRCErr) |					\
612*4882a593Smuzhiyun 		ERR_MASK(RcvICRCErr) |					\
613*4882a593Smuzhiyun 		ERR_MASK(RcvShortPktLenErr) |				\
614*4882a593Smuzhiyun 		ERR_MASK(RcvEBPErr))
615*4882a593Smuzhiyun 
616*4882a593Smuzhiyun /* These are all rcv-related errors which we want to count for stats */
617*4882a593Smuzhiyun #define E_SUM_PKTERRS						\
618*4882a593Smuzhiyun 	(ERR_MASK(RcvHdrLenErr) | ERR_MASK(RcvBadTidErr) |		\
619*4882a593Smuzhiyun 	 ERR_MASK(RcvBadVersionErr) | ERR_MASK(RcvHdrErr) |		\
620*4882a593Smuzhiyun 	 ERR_MASK(RcvLongPktLenErr) | ERR_MASK(RcvShortPktLenErr) |	\
621*4882a593Smuzhiyun 	 ERR_MASK(RcvMaxPktLenErr) | ERR_MASK(RcvMinPktLenErr) |	\
622*4882a593Smuzhiyun 	 ERR_MASK(RcvFormatErr) | ERR_MASK(RcvUnsupportedVLErr) |	\
623*4882a593Smuzhiyun 	 ERR_MASK(RcvUnexpectedCharErr) | ERR_MASK(RcvEBPErr))
624*4882a593Smuzhiyun 
625*4882a593Smuzhiyun /* These are all send-related errors which we want to count for stats */
626*4882a593Smuzhiyun #define E_SUM_ERRS							\
627*4882a593Smuzhiyun 	(ERR_MASK(SendPioArmLaunchErr) |				\
628*4882a593Smuzhiyun 	 ERR_MASK(SendUnexpectedPktNumErr) |				\
629*4882a593Smuzhiyun 	 ERR_MASK(SendDroppedDataPktErr) |				\
630*4882a593Smuzhiyun 	 ERR_MASK(SendDroppedSmpPktErr) |				\
631*4882a593Smuzhiyun 	 ERR_MASK(SendMaxPktLenErr) | ERR_MASK(SendUnsupportedVLErr) |	\
632*4882a593Smuzhiyun 	 ERR_MASK(SendMinPktLenErr) | ERR_MASK(SendPktLenErr) |		\
633*4882a593Smuzhiyun 	 ERR_MASK(InvalidAddrErr))
634*4882a593Smuzhiyun 
635*4882a593Smuzhiyun /*
636*4882a593Smuzhiyun  * this is similar to E_SUM_ERRS, but can't ignore armlaunch, don't ignore
637*4882a593Smuzhiyun  * errors not related to freeze and cancelling buffers.  Can't ignore
638*4882a593Smuzhiyun  * armlaunch because could get more while still cleaning up, and need
639*4882a593Smuzhiyun  * to cancel those as they happen.
640*4882a593Smuzhiyun  */
641*4882a593Smuzhiyun #define E_SPKT_ERRS_IGNORE \
642*4882a593Smuzhiyun 	(ERR_MASK(SendDroppedDataPktErr) |				\
643*4882a593Smuzhiyun 	 ERR_MASK(SendDroppedSmpPktErr) |				\
644*4882a593Smuzhiyun 	 ERR_MASK(SendMaxPktLenErr) | ERR_MASK(SendMinPktLenErr) |	\
645*4882a593Smuzhiyun 	 ERR_MASK(SendPktLenErr))
646*4882a593Smuzhiyun 
647*4882a593Smuzhiyun /*
648*4882a593Smuzhiyun  * these are errors that can occur when the link changes state while
649*4882a593Smuzhiyun  * a packet is being sent or received.  This doesn't cover things
650*4882a593Smuzhiyun  * like EBP or VCRC that can be the result of a sending having the
651*4882a593Smuzhiyun  * link change state, so we receive a "known bad" packet.
652*4882a593Smuzhiyun  */
653*4882a593Smuzhiyun #define E_SUM_LINK_PKTERRS		\
654*4882a593Smuzhiyun 	(ERR_MASK(SendDroppedDataPktErr) |				\
655*4882a593Smuzhiyun 	 ERR_MASK(SendDroppedSmpPktErr) |				\
656*4882a593Smuzhiyun 	 ERR_MASK(SendMinPktLenErr) | ERR_MASK(SendPktLenErr) |		\
657*4882a593Smuzhiyun 	 ERR_MASK(RcvShortPktLenErr) | ERR_MASK(RcvMinPktLenErr) |	\
658*4882a593Smuzhiyun 	 ERR_MASK(RcvUnexpectedCharErr))
659*4882a593Smuzhiyun 
660*4882a593Smuzhiyun static void qib_6120_put_tid_2(struct qib_devdata *, u64 __iomem *,
661*4882a593Smuzhiyun 			       u32, unsigned long);
662*4882a593Smuzhiyun 
663*4882a593Smuzhiyun /*
664*4882a593Smuzhiyun  * On platforms using this chip, and not having ordered WC stores, we
665*4882a593Smuzhiyun  * can get TXE parity errors due to speculative reads to the PIO buffers,
666*4882a593Smuzhiyun  * and this, due to a chip issue can result in (many) false parity error
667*4882a593Smuzhiyun  * reports.  So it's a debug print on those, and an info print on systems
668*4882a593Smuzhiyun  * where the speculative reads don't occur.
669*4882a593Smuzhiyun  */
qib_6120_txe_recover(struct qib_devdata * dd)670*4882a593Smuzhiyun static void qib_6120_txe_recover(struct qib_devdata *dd)
671*4882a593Smuzhiyun {
672*4882a593Smuzhiyun 	if (!qib_unordered_wc())
673*4882a593Smuzhiyun 		qib_devinfo(dd->pcidev,
674*4882a593Smuzhiyun 			    "Recovering from TXE PIO parity error\n");
675*4882a593Smuzhiyun }
676*4882a593Smuzhiyun 
677*4882a593Smuzhiyun /* enable/disable chip from delivering interrupts */
qib_6120_set_intr_state(struct qib_devdata * dd,u32 enable)678*4882a593Smuzhiyun static void qib_6120_set_intr_state(struct qib_devdata *dd, u32 enable)
679*4882a593Smuzhiyun {
680*4882a593Smuzhiyun 	if (enable) {
681*4882a593Smuzhiyun 		if (dd->flags & QIB_BADINTR)
682*4882a593Smuzhiyun 			return;
683*4882a593Smuzhiyun 		qib_write_kreg(dd, kr_intmask, ~0ULL);
684*4882a593Smuzhiyun 		/* force re-interrupt of any pending interrupts. */
685*4882a593Smuzhiyun 		qib_write_kreg(dd, kr_intclear, 0ULL);
686*4882a593Smuzhiyun 	} else
687*4882a593Smuzhiyun 		qib_write_kreg(dd, kr_intmask, 0ULL);
688*4882a593Smuzhiyun }
689*4882a593Smuzhiyun 
690*4882a593Smuzhiyun /*
691*4882a593Smuzhiyun  * Try to cleanup as much as possible for anything that might have gone
692*4882a593Smuzhiyun  * wrong while in freeze mode, such as pio buffers being written by user
693*4882a593Smuzhiyun  * processes (causing armlaunch), send errors due to going into freeze mode,
694*4882a593Smuzhiyun  * etc., and try to avoid causing extra interrupts while doing so.
695*4882a593Smuzhiyun  * Forcibly update the in-memory pioavail register copies after cleanup
696*4882a593Smuzhiyun  * because the chip won't do it while in freeze mode (the register values
697*4882a593Smuzhiyun  * themselves are kept correct).
698*4882a593Smuzhiyun  * Make sure that we don't lose any important interrupts by using the chip
699*4882a593Smuzhiyun  * feature that says that writing 0 to a bit in *clear that is set in
700*4882a593Smuzhiyun  * *status will cause an interrupt to be generated again (if allowed by
701*4882a593Smuzhiyun  * the *mask value).
702*4882a593Smuzhiyun  * This is in chip-specific code because of all of the register accesses,
703*4882a593Smuzhiyun  * even though the details are similar on most chips
704*4882a593Smuzhiyun  */
qib_6120_clear_freeze(struct qib_devdata * dd)705*4882a593Smuzhiyun static void qib_6120_clear_freeze(struct qib_devdata *dd)
706*4882a593Smuzhiyun {
707*4882a593Smuzhiyun 	/* disable error interrupts, to avoid confusion */
708*4882a593Smuzhiyun 	qib_write_kreg(dd, kr_errmask, 0ULL);
709*4882a593Smuzhiyun 
710*4882a593Smuzhiyun 	/* also disable interrupts; errormask is sometimes overwritten */
711*4882a593Smuzhiyun 	qib_6120_set_intr_state(dd, 0);
712*4882a593Smuzhiyun 
713*4882a593Smuzhiyun 	qib_cancel_sends(dd->pport);
714*4882a593Smuzhiyun 
715*4882a593Smuzhiyun 	/* clear the freeze, and be sure chip saw it */
716*4882a593Smuzhiyun 	qib_write_kreg(dd, kr_control, dd->control);
717*4882a593Smuzhiyun 	qib_read_kreg32(dd, kr_scratch);
718*4882a593Smuzhiyun 
719*4882a593Smuzhiyun 	/* force in-memory update now we are out of freeze */
720*4882a593Smuzhiyun 	qib_force_pio_avail_update(dd);
721*4882a593Smuzhiyun 
722*4882a593Smuzhiyun 	/*
723*4882a593Smuzhiyun 	 * force new interrupt if any hwerr, error or interrupt bits are
724*4882a593Smuzhiyun 	 * still set, and clear "safe" send packet errors related to freeze
725*4882a593Smuzhiyun 	 * and cancelling sends.  Re-enable error interrupts before possible
726*4882a593Smuzhiyun 	 * force of re-interrupt on pending interrupts.
727*4882a593Smuzhiyun 	 */
728*4882a593Smuzhiyun 	qib_write_kreg(dd, kr_hwerrclear, 0ULL);
729*4882a593Smuzhiyun 	qib_write_kreg(dd, kr_errclear, E_SPKT_ERRS_IGNORE);
730*4882a593Smuzhiyun 	qib_write_kreg(dd, kr_errmask, dd->cspec->errormask);
731*4882a593Smuzhiyun 	qib_6120_set_intr_state(dd, 1);
732*4882a593Smuzhiyun }
733*4882a593Smuzhiyun 
734*4882a593Smuzhiyun /**
735*4882a593Smuzhiyun  * qib_handle_6120_hwerrors - display hardware errors.
736*4882a593Smuzhiyun  * @dd: the qlogic_ib device
737*4882a593Smuzhiyun  * @msg: the output buffer
738*4882a593Smuzhiyun  * @msgl: the size of the output buffer
739*4882a593Smuzhiyun  *
740*4882a593Smuzhiyun  * Use same msg buffer as regular errors to avoid excessive stack
741*4882a593Smuzhiyun  * use.  Most hardware errors are catastrophic, but for right now,
742*4882a593Smuzhiyun  * we'll print them and continue.  Reuse the same message buffer as
743*4882a593Smuzhiyun  * handle_6120_errors() to avoid excessive stack usage.
744*4882a593Smuzhiyun  */
qib_handle_6120_hwerrors(struct qib_devdata * dd,char * msg,size_t msgl)745*4882a593Smuzhiyun static void qib_handle_6120_hwerrors(struct qib_devdata *dd, char *msg,
746*4882a593Smuzhiyun 				     size_t msgl)
747*4882a593Smuzhiyun {
748*4882a593Smuzhiyun 	u64 hwerrs;
749*4882a593Smuzhiyun 	u32 bits, ctrl;
750*4882a593Smuzhiyun 	int isfatal = 0;
751*4882a593Smuzhiyun 	char *bitsmsg;
752*4882a593Smuzhiyun 
753*4882a593Smuzhiyun 	hwerrs = qib_read_kreg64(dd, kr_hwerrstatus);
754*4882a593Smuzhiyun 	if (!hwerrs)
755*4882a593Smuzhiyun 		return;
756*4882a593Smuzhiyun 	if (hwerrs == ~0ULL) {
757*4882a593Smuzhiyun 		qib_dev_err(dd,
758*4882a593Smuzhiyun 			"Read of hardware error status failed (all bits set); ignoring\n");
759*4882a593Smuzhiyun 		return;
760*4882a593Smuzhiyun 	}
761*4882a593Smuzhiyun 	qib_stats.sps_hwerrs++;
762*4882a593Smuzhiyun 
763*4882a593Smuzhiyun 	/* Always clear the error status register, except MEMBISTFAIL,
764*4882a593Smuzhiyun 	 * regardless of whether we continue or stop using the chip.
765*4882a593Smuzhiyun 	 * We want that set so we know it failed, even across driver reload.
766*4882a593Smuzhiyun 	 * We'll still ignore it in the hwerrmask.  We do this partly for
767*4882a593Smuzhiyun 	 * diagnostics, but also for support */
768*4882a593Smuzhiyun 	qib_write_kreg(dd, kr_hwerrclear,
769*4882a593Smuzhiyun 		       hwerrs & ~HWE_MASK(PowerOnBISTFailed));
770*4882a593Smuzhiyun 
771*4882a593Smuzhiyun 	hwerrs &= dd->cspec->hwerrmask;
772*4882a593Smuzhiyun 
773*4882a593Smuzhiyun 	/*
774*4882a593Smuzhiyun 	 * Make sure we get this much out, unless told to be quiet,
775*4882a593Smuzhiyun 	 * or it's occurred within the last 5 seconds.
776*4882a593Smuzhiyun 	 */
777*4882a593Smuzhiyun 	if (hwerrs & ~(TXE_PIO_PARITY | RXEMEMPARITYERR_EAGERTID))
778*4882a593Smuzhiyun 		qib_devinfo(dd->pcidev,
779*4882a593Smuzhiyun 			"Hardware error: hwerr=0x%llx (cleared)\n",
780*4882a593Smuzhiyun 			(unsigned long long) hwerrs);
781*4882a593Smuzhiyun 
782*4882a593Smuzhiyun 	if (hwerrs & ~IB_HWE_BITSEXTANT)
783*4882a593Smuzhiyun 		qib_dev_err(dd,
784*4882a593Smuzhiyun 			"hwerror interrupt with unknown errors %llx set\n",
785*4882a593Smuzhiyun 			(unsigned long long)(hwerrs & ~IB_HWE_BITSEXTANT));
786*4882a593Smuzhiyun 
787*4882a593Smuzhiyun 	ctrl = qib_read_kreg32(dd, kr_control);
788*4882a593Smuzhiyun 	if ((ctrl & QLOGIC_IB_C_FREEZEMODE) && !dd->diag_client) {
789*4882a593Smuzhiyun 		/*
790*4882a593Smuzhiyun 		 * Parity errors in send memory are recoverable,
791*4882a593Smuzhiyun 		 * just cancel the send (if indicated in * sendbuffererror),
792*4882a593Smuzhiyun 		 * count the occurrence, unfreeze (if no other handled
793*4882a593Smuzhiyun 		 * hardware error bits are set), and continue. They can
794*4882a593Smuzhiyun 		 * occur if a processor speculative read is done to the PIO
795*4882a593Smuzhiyun 		 * buffer while we are sending a packet, for example.
796*4882a593Smuzhiyun 		 */
797*4882a593Smuzhiyun 		if (hwerrs & TXE_PIO_PARITY) {
798*4882a593Smuzhiyun 			qib_6120_txe_recover(dd);
799*4882a593Smuzhiyun 			hwerrs &= ~TXE_PIO_PARITY;
800*4882a593Smuzhiyun 		}
801*4882a593Smuzhiyun 
802*4882a593Smuzhiyun 		if (!hwerrs) {
803*4882a593Smuzhiyun 			static u32 freeze_cnt;
804*4882a593Smuzhiyun 
805*4882a593Smuzhiyun 			freeze_cnt++;
806*4882a593Smuzhiyun 			qib_6120_clear_freeze(dd);
807*4882a593Smuzhiyun 		} else
808*4882a593Smuzhiyun 			isfatal = 1;
809*4882a593Smuzhiyun 	}
810*4882a593Smuzhiyun 
811*4882a593Smuzhiyun 	*msg = '\0';
812*4882a593Smuzhiyun 
813*4882a593Smuzhiyun 	if (hwerrs & HWE_MASK(PowerOnBISTFailed)) {
814*4882a593Smuzhiyun 		isfatal = 1;
815*4882a593Smuzhiyun 		strlcat(msg,
816*4882a593Smuzhiyun 			"[Memory BIST test failed, InfiniPath hardware unusable]",
817*4882a593Smuzhiyun 			msgl);
818*4882a593Smuzhiyun 		/* ignore from now on, so disable until driver reloaded */
819*4882a593Smuzhiyun 		dd->cspec->hwerrmask &= ~HWE_MASK(PowerOnBISTFailed);
820*4882a593Smuzhiyun 		qib_write_kreg(dd, kr_hwerrmask, dd->cspec->hwerrmask);
821*4882a593Smuzhiyun 	}
822*4882a593Smuzhiyun 
823*4882a593Smuzhiyun 	qib_format_hwerrors(hwerrs, qib_6120_hwerror_msgs,
824*4882a593Smuzhiyun 			    ARRAY_SIZE(qib_6120_hwerror_msgs), msg, msgl);
825*4882a593Smuzhiyun 
826*4882a593Smuzhiyun 	bitsmsg = dd->cspec->bitsmsgbuf;
827*4882a593Smuzhiyun 	if (hwerrs & (QLOGIC_IB_HWE_PCIEMEMPARITYERR_MASK <<
828*4882a593Smuzhiyun 		      QLOGIC_IB_HWE_PCIEMEMPARITYERR_SHIFT)) {
829*4882a593Smuzhiyun 		bits = (u32) ((hwerrs >>
830*4882a593Smuzhiyun 			       QLOGIC_IB_HWE_PCIEMEMPARITYERR_SHIFT) &
831*4882a593Smuzhiyun 			      QLOGIC_IB_HWE_PCIEMEMPARITYERR_MASK);
832*4882a593Smuzhiyun 		snprintf(bitsmsg, sizeof(dd->cspec->bitsmsgbuf),
833*4882a593Smuzhiyun 			 "[PCIe Mem Parity Errs %x] ", bits);
834*4882a593Smuzhiyun 		strlcat(msg, bitsmsg, msgl);
835*4882a593Smuzhiyun 	}
836*4882a593Smuzhiyun 
837*4882a593Smuzhiyun 	if (hwerrs & _QIB_PLL_FAIL) {
838*4882a593Smuzhiyun 		isfatal = 1;
839*4882a593Smuzhiyun 		snprintf(bitsmsg, sizeof(dd->cspec->bitsmsgbuf),
840*4882a593Smuzhiyun 			 "[PLL failed (%llx), InfiniPath hardware unusable]",
841*4882a593Smuzhiyun 			 (unsigned long long) hwerrs & _QIB_PLL_FAIL);
842*4882a593Smuzhiyun 		strlcat(msg, bitsmsg, msgl);
843*4882a593Smuzhiyun 		/* ignore from now on, so disable until driver reloaded */
844*4882a593Smuzhiyun 		dd->cspec->hwerrmask &= ~(hwerrs & _QIB_PLL_FAIL);
845*4882a593Smuzhiyun 		qib_write_kreg(dd, kr_hwerrmask, dd->cspec->hwerrmask);
846*4882a593Smuzhiyun 	}
847*4882a593Smuzhiyun 
848*4882a593Smuzhiyun 	if (hwerrs & QLOGIC_IB_HWE_SERDESPLLFAILED) {
849*4882a593Smuzhiyun 		/*
850*4882a593Smuzhiyun 		 * If it occurs, it is left masked since the external
851*4882a593Smuzhiyun 		 * interface is unused
852*4882a593Smuzhiyun 		 */
853*4882a593Smuzhiyun 		dd->cspec->hwerrmask &= ~QLOGIC_IB_HWE_SERDESPLLFAILED;
854*4882a593Smuzhiyun 		qib_write_kreg(dd, kr_hwerrmask, dd->cspec->hwerrmask);
855*4882a593Smuzhiyun 	}
856*4882a593Smuzhiyun 
857*4882a593Smuzhiyun 	if (hwerrs)
858*4882a593Smuzhiyun 		/*
859*4882a593Smuzhiyun 		 * if any set that we aren't ignoring; only
860*4882a593Smuzhiyun 		 * make the complaint once, in case it's stuck
861*4882a593Smuzhiyun 		 * or recurring, and we get here multiple
862*4882a593Smuzhiyun 		 * times.
863*4882a593Smuzhiyun 		 */
864*4882a593Smuzhiyun 		qib_dev_err(dd, "%s hardware error\n", msg);
865*4882a593Smuzhiyun 	else
866*4882a593Smuzhiyun 		*msg = 0; /* recovered from all of them */
867*4882a593Smuzhiyun 
868*4882a593Smuzhiyun 	if (isfatal && !dd->diag_client) {
869*4882a593Smuzhiyun 		qib_dev_err(dd,
870*4882a593Smuzhiyun 			"Fatal Hardware Error, no longer usable, SN %.16s\n",
871*4882a593Smuzhiyun 			dd->serial);
872*4882a593Smuzhiyun 		/*
873*4882a593Smuzhiyun 		 * for /sys status file and user programs to print; if no
874*4882a593Smuzhiyun 		 * trailing brace is copied, we'll know it was truncated.
875*4882a593Smuzhiyun 		 */
876*4882a593Smuzhiyun 		if (dd->freezemsg)
877*4882a593Smuzhiyun 			snprintf(dd->freezemsg, dd->freezelen,
878*4882a593Smuzhiyun 				 "{%s}", msg);
879*4882a593Smuzhiyun 		qib_disable_after_error(dd);
880*4882a593Smuzhiyun 	}
881*4882a593Smuzhiyun }
882*4882a593Smuzhiyun 
883*4882a593Smuzhiyun /*
884*4882a593Smuzhiyun  * Decode the error status into strings, deciding whether to always
885*4882a593Smuzhiyun  * print * it or not depending on "normal packet errors" vs everything
886*4882a593Smuzhiyun  * else.   Return 1 if "real" errors, otherwise 0 if only packet
887*4882a593Smuzhiyun  * errors, so caller can decide what to print with the string.
888*4882a593Smuzhiyun  */
qib_decode_6120_err(struct qib_devdata * dd,char * buf,size_t blen,u64 err)889*4882a593Smuzhiyun static int qib_decode_6120_err(struct qib_devdata *dd, char *buf, size_t blen,
890*4882a593Smuzhiyun 			       u64 err)
891*4882a593Smuzhiyun {
892*4882a593Smuzhiyun 	int iserr = 1;
893*4882a593Smuzhiyun 
894*4882a593Smuzhiyun 	*buf = '\0';
895*4882a593Smuzhiyun 	if (err & QLOGIC_IB_E_PKTERRS) {
896*4882a593Smuzhiyun 		if (!(err & ~QLOGIC_IB_E_PKTERRS))
897*4882a593Smuzhiyun 			iserr = 0;
898*4882a593Smuzhiyun 		if ((err & ERR_MASK(RcvICRCErr)) &&
899*4882a593Smuzhiyun 		    !(err&(ERR_MASK(RcvVCRCErr)|ERR_MASK(RcvEBPErr))))
900*4882a593Smuzhiyun 			strlcat(buf, "CRC ", blen);
901*4882a593Smuzhiyun 		if (!iserr)
902*4882a593Smuzhiyun 			goto done;
903*4882a593Smuzhiyun 	}
904*4882a593Smuzhiyun 	if (err & ERR_MASK(RcvHdrLenErr))
905*4882a593Smuzhiyun 		strlcat(buf, "rhdrlen ", blen);
906*4882a593Smuzhiyun 	if (err & ERR_MASK(RcvBadTidErr))
907*4882a593Smuzhiyun 		strlcat(buf, "rbadtid ", blen);
908*4882a593Smuzhiyun 	if (err & ERR_MASK(RcvBadVersionErr))
909*4882a593Smuzhiyun 		strlcat(buf, "rbadversion ", blen);
910*4882a593Smuzhiyun 	if (err & ERR_MASK(RcvHdrErr))
911*4882a593Smuzhiyun 		strlcat(buf, "rhdr ", blen);
912*4882a593Smuzhiyun 	if (err & ERR_MASK(RcvLongPktLenErr))
913*4882a593Smuzhiyun 		strlcat(buf, "rlongpktlen ", blen);
914*4882a593Smuzhiyun 	if (err & ERR_MASK(RcvMaxPktLenErr))
915*4882a593Smuzhiyun 		strlcat(buf, "rmaxpktlen ", blen);
916*4882a593Smuzhiyun 	if (err & ERR_MASK(RcvMinPktLenErr))
917*4882a593Smuzhiyun 		strlcat(buf, "rminpktlen ", blen);
918*4882a593Smuzhiyun 	if (err & ERR_MASK(SendMinPktLenErr))
919*4882a593Smuzhiyun 		strlcat(buf, "sminpktlen ", blen);
920*4882a593Smuzhiyun 	if (err & ERR_MASK(RcvFormatErr))
921*4882a593Smuzhiyun 		strlcat(buf, "rformaterr ", blen);
922*4882a593Smuzhiyun 	if (err & ERR_MASK(RcvUnsupportedVLErr))
923*4882a593Smuzhiyun 		strlcat(buf, "runsupvl ", blen);
924*4882a593Smuzhiyun 	if (err & ERR_MASK(RcvUnexpectedCharErr))
925*4882a593Smuzhiyun 		strlcat(buf, "runexpchar ", blen);
926*4882a593Smuzhiyun 	if (err & ERR_MASK(RcvIBFlowErr))
927*4882a593Smuzhiyun 		strlcat(buf, "ribflow ", blen);
928*4882a593Smuzhiyun 	if (err & ERR_MASK(SendUnderRunErr))
929*4882a593Smuzhiyun 		strlcat(buf, "sunderrun ", blen);
930*4882a593Smuzhiyun 	if (err & ERR_MASK(SendPioArmLaunchErr))
931*4882a593Smuzhiyun 		strlcat(buf, "spioarmlaunch ", blen);
932*4882a593Smuzhiyun 	if (err & ERR_MASK(SendUnexpectedPktNumErr))
933*4882a593Smuzhiyun 		strlcat(buf, "sunexperrpktnum ", blen);
934*4882a593Smuzhiyun 	if (err & ERR_MASK(SendDroppedSmpPktErr))
935*4882a593Smuzhiyun 		strlcat(buf, "sdroppedsmppkt ", blen);
936*4882a593Smuzhiyun 	if (err & ERR_MASK(SendMaxPktLenErr))
937*4882a593Smuzhiyun 		strlcat(buf, "smaxpktlen ", blen);
938*4882a593Smuzhiyun 	if (err & ERR_MASK(SendUnsupportedVLErr))
939*4882a593Smuzhiyun 		strlcat(buf, "sunsupVL ", blen);
940*4882a593Smuzhiyun 	if (err & ERR_MASK(InvalidAddrErr))
941*4882a593Smuzhiyun 		strlcat(buf, "invalidaddr ", blen);
942*4882a593Smuzhiyun 	if (err & ERR_MASK(RcvEgrFullErr))
943*4882a593Smuzhiyun 		strlcat(buf, "rcvegrfull ", blen);
944*4882a593Smuzhiyun 	if (err & ERR_MASK(RcvHdrFullErr))
945*4882a593Smuzhiyun 		strlcat(buf, "rcvhdrfull ", blen);
946*4882a593Smuzhiyun 	if (err & ERR_MASK(IBStatusChanged))
947*4882a593Smuzhiyun 		strlcat(buf, "ibcstatuschg ", blen);
948*4882a593Smuzhiyun 	if (err & ERR_MASK(RcvIBLostLinkErr))
949*4882a593Smuzhiyun 		strlcat(buf, "riblostlink ", blen);
950*4882a593Smuzhiyun 	if (err & ERR_MASK(HardwareErr))
951*4882a593Smuzhiyun 		strlcat(buf, "hardware ", blen);
952*4882a593Smuzhiyun 	if (err & ERR_MASK(ResetNegated))
953*4882a593Smuzhiyun 		strlcat(buf, "reset ", blen);
954*4882a593Smuzhiyun done:
955*4882a593Smuzhiyun 	return iserr;
956*4882a593Smuzhiyun }
957*4882a593Smuzhiyun 
958*4882a593Smuzhiyun /*
959*4882a593Smuzhiyun  * Called when we might have an error that is specific to a particular
960*4882a593Smuzhiyun  * PIO buffer, and may need to cancel that buffer, so it can be re-used.
961*4882a593Smuzhiyun  */
qib_disarm_6120_senderrbufs(struct qib_pportdata * ppd)962*4882a593Smuzhiyun static void qib_disarm_6120_senderrbufs(struct qib_pportdata *ppd)
963*4882a593Smuzhiyun {
964*4882a593Smuzhiyun 	unsigned long sbuf[2];
965*4882a593Smuzhiyun 	struct qib_devdata *dd = ppd->dd;
966*4882a593Smuzhiyun 
967*4882a593Smuzhiyun 	/*
968*4882a593Smuzhiyun 	 * It's possible that sendbuffererror could have bits set; might
969*4882a593Smuzhiyun 	 * have already done this as a result of hardware error handling.
970*4882a593Smuzhiyun 	 */
971*4882a593Smuzhiyun 	sbuf[0] = qib_read_kreg64(dd, kr_sendbuffererror);
972*4882a593Smuzhiyun 	sbuf[1] = qib_read_kreg64(dd, kr_sendbuffererror + 1);
973*4882a593Smuzhiyun 
974*4882a593Smuzhiyun 	if (sbuf[0] || sbuf[1])
975*4882a593Smuzhiyun 		qib_disarm_piobufs_set(dd, sbuf,
976*4882a593Smuzhiyun 				       dd->piobcnt2k + dd->piobcnt4k);
977*4882a593Smuzhiyun }
978*4882a593Smuzhiyun 
chk_6120_linkrecovery(struct qib_devdata * dd,u64 ibcs)979*4882a593Smuzhiyun static int chk_6120_linkrecovery(struct qib_devdata *dd, u64 ibcs)
980*4882a593Smuzhiyun {
981*4882a593Smuzhiyun 	int ret = 1;
982*4882a593Smuzhiyun 	u32 ibstate = qib_6120_iblink_state(ibcs);
983*4882a593Smuzhiyun 	u32 linkrecov = read_6120_creg32(dd, cr_iblinkerrrecov);
984*4882a593Smuzhiyun 
985*4882a593Smuzhiyun 	if (linkrecov != dd->cspec->lastlinkrecov) {
986*4882a593Smuzhiyun 		/* and no more until active again */
987*4882a593Smuzhiyun 		dd->cspec->lastlinkrecov = 0;
988*4882a593Smuzhiyun 		qib_set_linkstate(dd->pport, QIB_IB_LINKDOWN);
989*4882a593Smuzhiyun 		ret = 0;
990*4882a593Smuzhiyun 	}
991*4882a593Smuzhiyun 	if (ibstate == IB_PORT_ACTIVE)
992*4882a593Smuzhiyun 		dd->cspec->lastlinkrecov =
993*4882a593Smuzhiyun 			read_6120_creg32(dd, cr_iblinkerrrecov);
994*4882a593Smuzhiyun 	return ret;
995*4882a593Smuzhiyun }
996*4882a593Smuzhiyun 
handle_6120_errors(struct qib_devdata * dd,u64 errs)997*4882a593Smuzhiyun static void handle_6120_errors(struct qib_devdata *dd, u64 errs)
998*4882a593Smuzhiyun {
999*4882a593Smuzhiyun 	char *msg;
1000*4882a593Smuzhiyun 	u64 ignore_this_time = 0;
1001*4882a593Smuzhiyun 	u64 iserr = 0;
1002*4882a593Smuzhiyun 	struct qib_pportdata *ppd = dd->pport;
1003*4882a593Smuzhiyun 	u64 mask;
1004*4882a593Smuzhiyun 
1005*4882a593Smuzhiyun 	/* don't report errors that are masked */
1006*4882a593Smuzhiyun 	errs &= dd->cspec->errormask;
1007*4882a593Smuzhiyun 	msg = dd->cspec->emsgbuf;
1008*4882a593Smuzhiyun 
1009*4882a593Smuzhiyun 	/* do these first, they are most important */
1010*4882a593Smuzhiyun 	if (errs & ERR_MASK(HardwareErr))
1011*4882a593Smuzhiyun 		qib_handle_6120_hwerrors(dd, msg, sizeof(dd->cspec->emsgbuf));
1012*4882a593Smuzhiyun 
1013*4882a593Smuzhiyun 	if (errs & ~IB_E_BITSEXTANT)
1014*4882a593Smuzhiyun 		qib_dev_err(dd,
1015*4882a593Smuzhiyun 			"error interrupt with unknown errors %llx set\n",
1016*4882a593Smuzhiyun 			(unsigned long long) (errs & ~IB_E_BITSEXTANT));
1017*4882a593Smuzhiyun 
1018*4882a593Smuzhiyun 	if (errs & E_SUM_ERRS) {
1019*4882a593Smuzhiyun 		qib_disarm_6120_senderrbufs(ppd);
1020*4882a593Smuzhiyun 		if ((errs & E_SUM_LINK_PKTERRS) &&
1021*4882a593Smuzhiyun 		    !(ppd->lflags & QIBL_LINKACTIVE)) {
1022*4882a593Smuzhiyun 			/*
1023*4882a593Smuzhiyun 			 * This can happen when trying to bring the link
1024*4882a593Smuzhiyun 			 * up, but the IB link changes state at the "wrong"
1025*4882a593Smuzhiyun 			 * time. The IB logic then complains that the packet
1026*4882a593Smuzhiyun 			 * isn't valid.  We don't want to confuse people, so
1027*4882a593Smuzhiyun 			 * we just don't print them, except at debug
1028*4882a593Smuzhiyun 			 */
1029*4882a593Smuzhiyun 			ignore_this_time = errs & E_SUM_LINK_PKTERRS;
1030*4882a593Smuzhiyun 		}
1031*4882a593Smuzhiyun 	} else if ((errs & E_SUM_LINK_PKTERRS) &&
1032*4882a593Smuzhiyun 		   !(ppd->lflags & QIBL_LINKACTIVE)) {
1033*4882a593Smuzhiyun 		/*
1034*4882a593Smuzhiyun 		 * This can happen when SMA is trying to bring the link
1035*4882a593Smuzhiyun 		 * up, but the IB link changes state at the "wrong" time.
1036*4882a593Smuzhiyun 		 * The IB logic then complains that the packet isn't
1037*4882a593Smuzhiyun 		 * valid.  We don't want to confuse people, so we just
1038*4882a593Smuzhiyun 		 * don't print them, except at debug
1039*4882a593Smuzhiyun 		 */
1040*4882a593Smuzhiyun 		ignore_this_time = errs & E_SUM_LINK_PKTERRS;
1041*4882a593Smuzhiyun 	}
1042*4882a593Smuzhiyun 
1043*4882a593Smuzhiyun 	qib_write_kreg(dd, kr_errclear, errs);
1044*4882a593Smuzhiyun 
1045*4882a593Smuzhiyun 	errs &= ~ignore_this_time;
1046*4882a593Smuzhiyun 	if (!errs)
1047*4882a593Smuzhiyun 		goto done;
1048*4882a593Smuzhiyun 
1049*4882a593Smuzhiyun 	/*
1050*4882a593Smuzhiyun 	 * The ones we mask off are handled specially below
1051*4882a593Smuzhiyun 	 * or above.
1052*4882a593Smuzhiyun 	 */
1053*4882a593Smuzhiyun 	mask = ERR_MASK(IBStatusChanged) | ERR_MASK(RcvEgrFullErr) |
1054*4882a593Smuzhiyun 		ERR_MASK(RcvHdrFullErr) | ERR_MASK(HardwareErr);
1055*4882a593Smuzhiyun 	qib_decode_6120_err(dd, msg, sizeof(dd->cspec->emsgbuf), errs & ~mask);
1056*4882a593Smuzhiyun 
1057*4882a593Smuzhiyun 	if (errs & E_SUM_PKTERRS)
1058*4882a593Smuzhiyun 		qib_stats.sps_rcverrs++;
1059*4882a593Smuzhiyun 	if (errs & E_SUM_ERRS)
1060*4882a593Smuzhiyun 		qib_stats.sps_txerrs++;
1061*4882a593Smuzhiyun 
1062*4882a593Smuzhiyun 	iserr = errs & ~(E_SUM_PKTERRS | QLOGIC_IB_E_PKTERRS);
1063*4882a593Smuzhiyun 
1064*4882a593Smuzhiyun 	if (errs & ERR_MASK(IBStatusChanged)) {
1065*4882a593Smuzhiyun 		u64 ibcs = qib_read_kreg64(dd, kr_ibcstatus);
1066*4882a593Smuzhiyun 		u32 ibstate = qib_6120_iblink_state(ibcs);
1067*4882a593Smuzhiyun 		int handle = 1;
1068*4882a593Smuzhiyun 
1069*4882a593Smuzhiyun 		if (ibstate != IB_PORT_INIT && dd->cspec->lastlinkrecov)
1070*4882a593Smuzhiyun 			handle = chk_6120_linkrecovery(dd, ibcs);
1071*4882a593Smuzhiyun 		/*
1072*4882a593Smuzhiyun 		 * Since going into a recovery state causes the link state
1073*4882a593Smuzhiyun 		 * to go down and since recovery is transitory, it is better
1074*4882a593Smuzhiyun 		 * if we "miss" ever seeing the link training state go into
1075*4882a593Smuzhiyun 		 * recovery (i.e., ignore this transition for link state
1076*4882a593Smuzhiyun 		 * special handling purposes) without updating lastibcstat.
1077*4882a593Smuzhiyun 		 */
1078*4882a593Smuzhiyun 		if (handle && qib_6120_phys_portstate(ibcs) ==
1079*4882a593Smuzhiyun 					    IB_PHYSPORTSTATE_LINK_ERR_RECOVER)
1080*4882a593Smuzhiyun 			handle = 0;
1081*4882a593Smuzhiyun 		if (handle)
1082*4882a593Smuzhiyun 			qib_handle_e_ibstatuschanged(ppd, ibcs);
1083*4882a593Smuzhiyun 	}
1084*4882a593Smuzhiyun 
1085*4882a593Smuzhiyun 	if (errs & ERR_MASK(ResetNegated)) {
1086*4882a593Smuzhiyun 		qib_dev_err(dd,
1087*4882a593Smuzhiyun 			"Got reset, requires re-init (unload and reload driver)\n");
1088*4882a593Smuzhiyun 		dd->flags &= ~QIB_INITTED;  /* needs re-init */
1089*4882a593Smuzhiyun 		/* mark as having had error */
1090*4882a593Smuzhiyun 		*dd->devstatusp |= QIB_STATUS_HWERROR;
1091*4882a593Smuzhiyun 		*dd->pport->statusp &= ~QIB_STATUS_IB_CONF;
1092*4882a593Smuzhiyun 	}
1093*4882a593Smuzhiyun 
1094*4882a593Smuzhiyun 	if (*msg && iserr)
1095*4882a593Smuzhiyun 		qib_dev_porterr(dd, ppd->port, "%s error\n", msg);
1096*4882a593Smuzhiyun 
1097*4882a593Smuzhiyun 	if (ppd->state_wanted & ppd->lflags)
1098*4882a593Smuzhiyun 		wake_up_interruptible(&ppd->state_wait);
1099*4882a593Smuzhiyun 
1100*4882a593Smuzhiyun 	/*
1101*4882a593Smuzhiyun 	 * If there were hdrq or egrfull errors, wake up any processes
1102*4882a593Smuzhiyun 	 * waiting in poll.  We used to try to check which contexts had
1103*4882a593Smuzhiyun 	 * the overflow, but given the cost of that and the chip reads
1104*4882a593Smuzhiyun 	 * to support it, it's better to just wake everybody up if we
1105*4882a593Smuzhiyun 	 * get an overflow; waiters can poll again if it's not them.
1106*4882a593Smuzhiyun 	 */
1107*4882a593Smuzhiyun 	if (errs & (ERR_MASK(RcvEgrFullErr) | ERR_MASK(RcvHdrFullErr))) {
1108*4882a593Smuzhiyun 		qib_handle_urcv(dd, ~0U);
1109*4882a593Smuzhiyun 		if (errs & ERR_MASK(RcvEgrFullErr))
1110*4882a593Smuzhiyun 			qib_stats.sps_buffull++;
1111*4882a593Smuzhiyun 		else
1112*4882a593Smuzhiyun 			qib_stats.sps_hdrfull++;
1113*4882a593Smuzhiyun 	}
1114*4882a593Smuzhiyun done:
1115*4882a593Smuzhiyun 	return;
1116*4882a593Smuzhiyun }
1117*4882a593Smuzhiyun 
1118*4882a593Smuzhiyun /**
1119*4882a593Smuzhiyun  * qib_6120_init_hwerrors - enable hardware errors
1120*4882a593Smuzhiyun  * @dd: the qlogic_ib device
1121*4882a593Smuzhiyun  *
1122*4882a593Smuzhiyun  * now that we have finished initializing everything that might reasonably
1123*4882a593Smuzhiyun  * cause a hardware error, and cleared those errors bits as they occur,
1124*4882a593Smuzhiyun  * we can enable hardware errors in the mask (potentially enabling
1125*4882a593Smuzhiyun  * freeze mode), and enable hardware errors as errors (along with
1126*4882a593Smuzhiyun  * everything else) in errormask
1127*4882a593Smuzhiyun  */
qib_6120_init_hwerrors(struct qib_devdata * dd)1128*4882a593Smuzhiyun static void qib_6120_init_hwerrors(struct qib_devdata *dd)
1129*4882a593Smuzhiyun {
1130*4882a593Smuzhiyun 	u64 val;
1131*4882a593Smuzhiyun 	u64 extsval;
1132*4882a593Smuzhiyun 
1133*4882a593Smuzhiyun 	extsval = qib_read_kreg64(dd, kr_extstatus);
1134*4882a593Smuzhiyun 
1135*4882a593Smuzhiyun 	if (!(extsval & QLOGIC_IB_EXTS_MEMBIST_ENDTEST))
1136*4882a593Smuzhiyun 		qib_dev_err(dd, "MemBIST did not complete!\n");
1137*4882a593Smuzhiyun 
1138*4882a593Smuzhiyun 	/* init so all hwerrors interrupt, and enter freeze, ajdust below */
1139*4882a593Smuzhiyun 	val = ~0ULL;
1140*4882a593Smuzhiyun 	if (dd->minrev < 2) {
1141*4882a593Smuzhiyun 		/*
1142*4882a593Smuzhiyun 		 * Avoid problem with internal interface bus parity
1143*4882a593Smuzhiyun 		 * checking. Fixed in Rev2.
1144*4882a593Smuzhiyun 		 */
1145*4882a593Smuzhiyun 		val &= ~QLOGIC_IB_HWE_PCIEBUSPARITYRADM;
1146*4882a593Smuzhiyun 	}
1147*4882a593Smuzhiyun 	/* avoid some intel cpu's speculative read freeze mode issue */
1148*4882a593Smuzhiyun 	val &= ~TXEMEMPARITYERR_PIOBUF;
1149*4882a593Smuzhiyun 
1150*4882a593Smuzhiyun 	dd->cspec->hwerrmask = val;
1151*4882a593Smuzhiyun 
1152*4882a593Smuzhiyun 	qib_write_kreg(dd, kr_hwerrclear, ~HWE_MASK(PowerOnBISTFailed));
1153*4882a593Smuzhiyun 	qib_write_kreg(dd, kr_hwerrmask, dd->cspec->hwerrmask);
1154*4882a593Smuzhiyun 
1155*4882a593Smuzhiyun 	/* clear all */
1156*4882a593Smuzhiyun 	qib_write_kreg(dd, kr_errclear, ~0ULL);
1157*4882a593Smuzhiyun 	/* enable errors that are masked, at least this first time. */
1158*4882a593Smuzhiyun 	qib_write_kreg(dd, kr_errmask, ~0ULL);
1159*4882a593Smuzhiyun 	dd->cspec->errormask = qib_read_kreg64(dd, kr_errmask);
1160*4882a593Smuzhiyun 	/* clear any interrupts up to this point (ints still not enabled) */
1161*4882a593Smuzhiyun 	qib_write_kreg(dd, kr_intclear, ~0ULL);
1162*4882a593Smuzhiyun 
1163*4882a593Smuzhiyun 	qib_write_kreg(dd, kr_rcvbthqp,
1164*4882a593Smuzhiyun 		       dd->qpn_mask << (QIB_6120_RcvBTHQP_BTHQP_Mask_LSB - 1) |
1165*4882a593Smuzhiyun 		       QIB_KD_QP);
1166*4882a593Smuzhiyun }
1167*4882a593Smuzhiyun 
1168*4882a593Smuzhiyun /*
1169*4882a593Smuzhiyun  * Disable and enable the armlaunch error.  Used for PIO bandwidth testing
1170*4882a593Smuzhiyun  * on chips that are count-based, rather than trigger-based.  There is no
1171*4882a593Smuzhiyun  * reference counting, but that's also fine, given the intended use.
1172*4882a593Smuzhiyun  * Only chip-specific because it's all register accesses
1173*4882a593Smuzhiyun  */
qib_set_6120_armlaunch(struct qib_devdata * dd,u32 enable)1174*4882a593Smuzhiyun static void qib_set_6120_armlaunch(struct qib_devdata *dd, u32 enable)
1175*4882a593Smuzhiyun {
1176*4882a593Smuzhiyun 	if (enable) {
1177*4882a593Smuzhiyun 		qib_write_kreg(dd, kr_errclear,
1178*4882a593Smuzhiyun 			       ERR_MASK(SendPioArmLaunchErr));
1179*4882a593Smuzhiyun 		dd->cspec->errormask |= ERR_MASK(SendPioArmLaunchErr);
1180*4882a593Smuzhiyun 	} else
1181*4882a593Smuzhiyun 		dd->cspec->errormask &= ~ERR_MASK(SendPioArmLaunchErr);
1182*4882a593Smuzhiyun 	qib_write_kreg(dd, kr_errmask, dd->cspec->errormask);
1183*4882a593Smuzhiyun }
1184*4882a593Smuzhiyun 
1185*4882a593Smuzhiyun /*
1186*4882a593Smuzhiyun  * Formerly took parameter <which> in pre-shifted,
1187*4882a593Smuzhiyun  * pre-merged form with LinkCmd and LinkInitCmd
1188*4882a593Smuzhiyun  * together, and assuming the zero was NOP.
1189*4882a593Smuzhiyun  */
qib_set_ib_6120_lstate(struct qib_pportdata * ppd,u16 linkcmd,u16 linitcmd)1190*4882a593Smuzhiyun static void qib_set_ib_6120_lstate(struct qib_pportdata *ppd, u16 linkcmd,
1191*4882a593Smuzhiyun 				   u16 linitcmd)
1192*4882a593Smuzhiyun {
1193*4882a593Smuzhiyun 	u64 mod_wd;
1194*4882a593Smuzhiyun 	struct qib_devdata *dd = ppd->dd;
1195*4882a593Smuzhiyun 	unsigned long flags;
1196*4882a593Smuzhiyun 
1197*4882a593Smuzhiyun 	if (linitcmd == QLOGIC_IB_IBCC_LINKINITCMD_DISABLE) {
1198*4882a593Smuzhiyun 		/*
1199*4882a593Smuzhiyun 		 * If we are told to disable, note that so link-recovery
1200*4882a593Smuzhiyun 		 * code does not attempt to bring us back up.
1201*4882a593Smuzhiyun 		 */
1202*4882a593Smuzhiyun 		spin_lock_irqsave(&ppd->lflags_lock, flags);
1203*4882a593Smuzhiyun 		ppd->lflags |= QIBL_IB_LINK_DISABLED;
1204*4882a593Smuzhiyun 		spin_unlock_irqrestore(&ppd->lflags_lock, flags);
1205*4882a593Smuzhiyun 	} else if (linitcmd || linkcmd == QLOGIC_IB_IBCC_LINKCMD_DOWN) {
1206*4882a593Smuzhiyun 		/*
1207*4882a593Smuzhiyun 		 * Any other linkinitcmd will lead to LINKDOWN and then
1208*4882a593Smuzhiyun 		 * to INIT (if all is well), so clear flag to let
1209*4882a593Smuzhiyun 		 * link-recovery code attempt to bring us back up.
1210*4882a593Smuzhiyun 		 */
1211*4882a593Smuzhiyun 		spin_lock_irqsave(&ppd->lflags_lock, flags);
1212*4882a593Smuzhiyun 		ppd->lflags &= ~QIBL_IB_LINK_DISABLED;
1213*4882a593Smuzhiyun 		spin_unlock_irqrestore(&ppd->lflags_lock, flags);
1214*4882a593Smuzhiyun 	}
1215*4882a593Smuzhiyun 
1216*4882a593Smuzhiyun 	mod_wd = (linkcmd << QLOGIC_IB_IBCC_LINKCMD_SHIFT) |
1217*4882a593Smuzhiyun 		(linitcmd << QLOGIC_IB_IBCC_LINKINITCMD_SHIFT);
1218*4882a593Smuzhiyun 
1219*4882a593Smuzhiyun 	qib_write_kreg(dd, kr_ibcctrl, dd->cspec->ibcctrl | mod_wd);
1220*4882a593Smuzhiyun 	/* write to chip to prevent back-to-back writes of control reg */
1221*4882a593Smuzhiyun 	qib_write_kreg(dd, kr_scratch, 0);
1222*4882a593Smuzhiyun }
1223*4882a593Smuzhiyun 
1224*4882a593Smuzhiyun /**
1225*4882a593Smuzhiyun  * qib_6120_bringup_serdes - bring up the serdes
1226*4882a593Smuzhiyun  * @dd: the qlogic_ib device
1227*4882a593Smuzhiyun  */
qib_6120_bringup_serdes(struct qib_pportdata * ppd)1228*4882a593Smuzhiyun static int qib_6120_bringup_serdes(struct qib_pportdata *ppd)
1229*4882a593Smuzhiyun {
1230*4882a593Smuzhiyun 	struct qib_devdata *dd = ppd->dd;
1231*4882a593Smuzhiyun 	u64 val, config1, prev_val, hwstat, ibc;
1232*4882a593Smuzhiyun 
1233*4882a593Smuzhiyun 	/* Put IBC in reset, sends disabled */
1234*4882a593Smuzhiyun 	dd->control &= ~QLOGIC_IB_C_LINKENABLE;
1235*4882a593Smuzhiyun 	qib_write_kreg(dd, kr_control, 0ULL);
1236*4882a593Smuzhiyun 
1237*4882a593Smuzhiyun 	dd->cspec->ibdeltainprog = 1;
1238*4882a593Smuzhiyun 	dd->cspec->ibsymsnap = read_6120_creg32(dd, cr_ibsymbolerr);
1239*4882a593Smuzhiyun 	dd->cspec->iblnkerrsnap = read_6120_creg32(dd, cr_iblinkerrrecov);
1240*4882a593Smuzhiyun 
1241*4882a593Smuzhiyun 	/* flowcontrolwatermark is in units of KBytes */
1242*4882a593Smuzhiyun 	ibc = 0x5ULL << SYM_LSB(IBCCtrl, FlowCtrlWaterMark);
1243*4882a593Smuzhiyun 	/*
1244*4882a593Smuzhiyun 	 * How often flowctrl sent.  More or less in usecs; balance against
1245*4882a593Smuzhiyun 	 * watermark value, so that in theory senders always get a flow
1246*4882a593Smuzhiyun 	 * control update in time to not let the IB link go idle.
1247*4882a593Smuzhiyun 	 */
1248*4882a593Smuzhiyun 	ibc |= 0x3ULL << SYM_LSB(IBCCtrl, FlowCtrlPeriod);
1249*4882a593Smuzhiyun 	/* max error tolerance */
1250*4882a593Smuzhiyun 	dd->cspec->lli_thresh = 0xf;
1251*4882a593Smuzhiyun 	ibc |= (u64) dd->cspec->lli_thresh << SYM_LSB(IBCCtrl, PhyerrThreshold);
1252*4882a593Smuzhiyun 	/* use "real" buffer space for */
1253*4882a593Smuzhiyun 	ibc |= 4ULL << SYM_LSB(IBCCtrl, CreditScale);
1254*4882a593Smuzhiyun 	/* IB credit flow control. */
1255*4882a593Smuzhiyun 	ibc |= 0xfULL << SYM_LSB(IBCCtrl, OverrunThreshold);
1256*4882a593Smuzhiyun 	/*
1257*4882a593Smuzhiyun 	 * set initial max size pkt IBC will send, including ICRC; it's the
1258*4882a593Smuzhiyun 	 * PIO buffer size in dwords, less 1; also see qib_set_mtu()
1259*4882a593Smuzhiyun 	 */
1260*4882a593Smuzhiyun 	ibc |= ((u64)(ppd->ibmaxlen >> 2) + 1) << SYM_LSB(IBCCtrl, MaxPktLen);
1261*4882a593Smuzhiyun 	dd->cspec->ibcctrl = ibc; /* without linkcmd or linkinitcmd! */
1262*4882a593Smuzhiyun 
1263*4882a593Smuzhiyun 	/* initially come up waiting for TS1, without sending anything. */
1264*4882a593Smuzhiyun 	val = dd->cspec->ibcctrl | (QLOGIC_IB_IBCC_LINKINITCMD_DISABLE <<
1265*4882a593Smuzhiyun 		QLOGIC_IB_IBCC_LINKINITCMD_SHIFT);
1266*4882a593Smuzhiyun 	qib_write_kreg(dd, kr_ibcctrl, val);
1267*4882a593Smuzhiyun 
1268*4882a593Smuzhiyun 	val = qib_read_kreg64(dd, kr_serdes_cfg0);
1269*4882a593Smuzhiyun 	config1 = qib_read_kreg64(dd, kr_serdes_cfg1);
1270*4882a593Smuzhiyun 
1271*4882a593Smuzhiyun 	/*
1272*4882a593Smuzhiyun 	 * Force reset on, also set rxdetect enable.  Must do before reading
1273*4882a593Smuzhiyun 	 * serdesstatus at least for simulation, or some of the bits in
1274*4882a593Smuzhiyun 	 * serdes status will come back as undefined and cause simulation
1275*4882a593Smuzhiyun 	 * failures
1276*4882a593Smuzhiyun 	 */
1277*4882a593Smuzhiyun 	val |= SYM_MASK(SerdesCfg0, ResetPLL) |
1278*4882a593Smuzhiyun 		SYM_MASK(SerdesCfg0, RxDetEnX) |
1279*4882a593Smuzhiyun 		(SYM_MASK(SerdesCfg0, L1PwrDnA) |
1280*4882a593Smuzhiyun 		 SYM_MASK(SerdesCfg0, L1PwrDnB) |
1281*4882a593Smuzhiyun 		 SYM_MASK(SerdesCfg0, L1PwrDnC) |
1282*4882a593Smuzhiyun 		 SYM_MASK(SerdesCfg0, L1PwrDnD));
1283*4882a593Smuzhiyun 	qib_write_kreg(dd, kr_serdes_cfg0, val);
1284*4882a593Smuzhiyun 	/* be sure chip saw it */
1285*4882a593Smuzhiyun 	qib_read_kreg64(dd, kr_scratch);
1286*4882a593Smuzhiyun 	udelay(5);              /* need pll reset set at least for a bit */
1287*4882a593Smuzhiyun 	/*
1288*4882a593Smuzhiyun 	 * after PLL is reset, set the per-lane Resets and TxIdle and
1289*4882a593Smuzhiyun 	 * clear the PLL reset and rxdetect (to get falling edge).
1290*4882a593Smuzhiyun 	 * Leave L1PWR bits set (permanently)
1291*4882a593Smuzhiyun 	 */
1292*4882a593Smuzhiyun 	val &= ~(SYM_MASK(SerdesCfg0, RxDetEnX) |
1293*4882a593Smuzhiyun 		 SYM_MASK(SerdesCfg0, ResetPLL) |
1294*4882a593Smuzhiyun 		 (SYM_MASK(SerdesCfg0, L1PwrDnA) |
1295*4882a593Smuzhiyun 		  SYM_MASK(SerdesCfg0, L1PwrDnB) |
1296*4882a593Smuzhiyun 		  SYM_MASK(SerdesCfg0, L1PwrDnC) |
1297*4882a593Smuzhiyun 		  SYM_MASK(SerdesCfg0, L1PwrDnD)));
1298*4882a593Smuzhiyun 	val |= (SYM_MASK(SerdesCfg0, ResetA) |
1299*4882a593Smuzhiyun 		SYM_MASK(SerdesCfg0, ResetB) |
1300*4882a593Smuzhiyun 		SYM_MASK(SerdesCfg0, ResetC) |
1301*4882a593Smuzhiyun 		SYM_MASK(SerdesCfg0, ResetD)) |
1302*4882a593Smuzhiyun 		SYM_MASK(SerdesCfg0, TxIdeEnX);
1303*4882a593Smuzhiyun 	qib_write_kreg(dd, kr_serdes_cfg0, val);
1304*4882a593Smuzhiyun 	/* be sure chip saw it */
1305*4882a593Smuzhiyun 	(void) qib_read_kreg64(dd, kr_scratch);
1306*4882a593Smuzhiyun 	/* need PLL reset clear for at least 11 usec before lane
1307*4882a593Smuzhiyun 	 * resets cleared; give it a few more to be sure */
1308*4882a593Smuzhiyun 	udelay(15);
1309*4882a593Smuzhiyun 	val &= ~((SYM_MASK(SerdesCfg0, ResetA) |
1310*4882a593Smuzhiyun 		  SYM_MASK(SerdesCfg0, ResetB) |
1311*4882a593Smuzhiyun 		  SYM_MASK(SerdesCfg0, ResetC) |
1312*4882a593Smuzhiyun 		  SYM_MASK(SerdesCfg0, ResetD)) |
1313*4882a593Smuzhiyun 		 SYM_MASK(SerdesCfg0, TxIdeEnX));
1314*4882a593Smuzhiyun 
1315*4882a593Smuzhiyun 	qib_write_kreg(dd, kr_serdes_cfg0, val);
1316*4882a593Smuzhiyun 	/* be sure chip saw it */
1317*4882a593Smuzhiyun 	(void) qib_read_kreg64(dd, kr_scratch);
1318*4882a593Smuzhiyun 
1319*4882a593Smuzhiyun 	val = qib_read_kreg64(dd, kr_xgxs_cfg);
1320*4882a593Smuzhiyun 	prev_val = val;
1321*4882a593Smuzhiyun 	if (val & QLOGIC_IB_XGXS_RESET)
1322*4882a593Smuzhiyun 		val &= ~QLOGIC_IB_XGXS_RESET;
1323*4882a593Smuzhiyun 	if (SYM_FIELD(val, XGXSCfg, polarity_inv) != ppd->rx_pol_inv) {
1324*4882a593Smuzhiyun 		/* need to compensate for Tx inversion in partner */
1325*4882a593Smuzhiyun 		val &= ~SYM_MASK(XGXSCfg, polarity_inv);
1326*4882a593Smuzhiyun 		val |= (u64)ppd->rx_pol_inv << SYM_LSB(XGXSCfg, polarity_inv);
1327*4882a593Smuzhiyun 	}
1328*4882a593Smuzhiyun 	if (val != prev_val)
1329*4882a593Smuzhiyun 		qib_write_kreg(dd, kr_xgxs_cfg, val);
1330*4882a593Smuzhiyun 
1331*4882a593Smuzhiyun 	val = qib_read_kreg64(dd, kr_serdes_cfg0);
1332*4882a593Smuzhiyun 
1333*4882a593Smuzhiyun 	/* clear current and de-emphasis bits */
1334*4882a593Smuzhiyun 	config1 &= ~0x0ffffffff00ULL;
1335*4882a593Smuzhiyun 	/* set current to 20ma */
1336*4882a593Smuzhiyun 	config1 |= 0x00000000000ULL;
1337*4882a593Smuzhiyun 	/* set de-emphasis to -5.68dB */
1338*4882a593Smuzhiyun 	config1 |= 0x0cccc000000ULL;
1339*4882a593Smuzhiyun 	qib_write_kreg(dd, kr_serdes_cfg1, config1);
1340*4882a593Smuzhiyun 
1341*4882a593Smuzhiyun 	/* base and port guid same for single port */
1342*4882a593Smuzhiyun 	ppd->guid = dd->base_guid;
1343*4882a593Smuzhiyun 
1344*4882a593Smuzhiyun 	/*
1345*4882a593Smuzhiyun 	 * the process of setting and un-resetting the serdes normally
1346*4882a593Smuzhiyun 	 * causes a serdes PLL error, so check for that and clear it
1347*4882a593Smuzhiyun 	 * here.  Also clearr hwerr bit in errstatus, but not others.
1348*4882a593Smuzhiyun 	 */
1349*4882a593Smuzhiyun 	hwstat = qib_read_kreg64(dd, kr_hwerrstatus);
1350*4882a593Smuzhiyun 	if (hwstat) {
1351*4882a593Smuzhiyun 		/* should just have PLL, clear all set, in an case */
1352*4882a593Smuzhiyun 		qib_write_kreg(dd, kr_hwerrclear, hwstat);
1353*4882a593Smuzhiyun 		qib_write_kreg(dd, kr_errclear, ERR_MASK(HardwareErr));
1354*4882a593Smuzhiyun 	}
1355*4882a593Smuzhiyun 
1356*4882a593Smuzhiyun 	dd->control |= QLOGIC_IB_C_LINKENABLE;
1357*4882a593Smuzhiyun 	dd->control &= ~QLOGIC_IB_C_FREEZEMODE;
1358*4882a593Smuzhiyun 	qib_write_kreg(dd, kr_control, dd->control);
1359*4882a593Smuzhiyun 
1360*4882a593Smuzhiyun 	return 0;
1361*4882a593Smuzhiyun }
1362*4882a593Smuzhiyun 
1363*4882a593Smuzhiyun /**
1364*4882a593Smuzhiyun  * qib_6120_quiet_serdes - set serdes to txidle
1365*4882a593Smuzhiyun  * @ppd: physical port of the qlogic_ib device
1366*4882a593Smuzhiyun  * Called when driver is being unloaded
1367*4882a593Smuzhiyun  */
qib_6120_quiet_serdes(struct qib_pportdata * ppd)1368*4882a593Smuzhiyun static void qib_6120_quiet_serdes(struct qib_pportdata *ppd)
1369*4882a593Smuzhiyun {
1370*4882a593Smuzhiyun 	struct qib_devdata *dd = ppd->dd;
1371*4882a593Smuzhiyun 	u64 val;
1372*4882a593Smuzhiyun 
1373*4882a593Smuzhiyun 	qib_set_ib_6120_lstate(ppd, 0, QLOGIC_IB_IBCC_LINKINITCMD_DISABLE);
1374*4882a593Smuzhiyun 
1375*4882a593Smuzhiyun 	/* disable IBC */
1376*4882a593Smuzhiyun 	dd->control &= ~QLOGIC_IB_C_LINKENABLE;
1377*4882a593Smuzhiyun 	qib_write_kreg(dd, kr_control,
1378*4882a593Smuzhiyun 		       dd->control | QLOGIC_IB_C_FREEZEMODE);
1379*4882a593Smuzhiyun 
1380*4882a593Smuzhiyun 	if (dd->cspec->ibsymdelta || dd->cspec->iblnkerrdelta ||
1381*4882a593Smuzhiyun 	    dd->cspec->ibdeltainprog) {
1382*4882a593Smuzhiyun 		u64 diagc;
1383*4882a593Smuzhiyun 
1384*4882a593Smuzhiyun 		/* enable counter writes */
1385*4882a593Smuzhiyun 		diagc = qib_read_kreg64(dd, kr_hwdiagctrl);
1386*4882a593Smuzhiyun 		qib_write_kreg(dd, kr_hwdiagctrl,
1387*4882a593Smuzhiyun 			       diagc | SYM_MASK(HwDiagCtrl, CounterWrEnable));
1388*4882a593Smuzhiyun 
1389*4882a593Smuzhiyun 		if (dd->cspec->ibsymdelta || dd->cspec->ibdeltainprog) {
1390*4882a593Smuzhiyun 			val = read_6120_creg32(dd, cr_ibsymbolerr);
1391*4882a593Smuzhiyun 			if (dd->cspec->ibdeltainprog)
1392*4882a593Smuzhiyun 				val -= val - dd->cspec->ibsymsnap;
1393*4882a593Smuzhiyun 			val -= dd->cspec->ibsymdelta;
1394*4882a593Smuzhiyun 			write_6120_creg(dd, cr_ibsymbolerr, val);
1395*4882a593Smuzhiyun 		}
1396*4882a593Smuzhiyun 		if (dd->cspec->iblnkerrdelta || dd->cspec->ibdeltainprog) {
1397*4882a593Smuzhiyun 			val = read_6120_creg32(dd, cr_iblinkerrrecov);
1398*4882a593Smuzhiyun 			if (dd->cspec->ibdeltainprog)
1399*4882a593Smuzhiyun 				val -= val - dd->cspec->iblnkerrsnap;
1400*4882a593Smuzhiyun 			val -= dd->cspec->iblnkerrdelta;
1401*4882a593Smuzhiyun 			write_6120_creg(dd, cr_iblinkerrrecov, val);
1402*4882a593Smuzhiyun 		}
1403*4882a593Smuzhiyun 
1404*4882a593Smuzhiyun 		/* and disable counter writes */
1405*4882a593Smuzhiyun 		qib_write_kreg(dd, kr_hwdiagctrl, diagc);
1406*4882a593Smuzhiyun 	}
1407*4882a593Smuzhiyun 
1408*4882a593Smuzhiyun 	val = qib_read_kreg64(dd, kr_serdes_cfg0);
1409*4882a593Smuzhiyun 	val |= SYM_MASK(SerdesCfg0, TxIdeEnX);
1410*4882a593Smuzhiyun 	qib_write_kreg(dd, kr_serdes_cfg0, val);
1411*4882a593Smuzhiyun }
1412*4882a593Smuzhiyun 
1413*4882a593Smuzhiyun /**
1414*4882a593Smuzhiyun  * qib_6120_setup_setextled - set the state of the two external LEDs
1415*4882a593Smuzhiyun  * @dd: the qlogic_ib device
1416*4882a593Smuzhiyun  * @on: whether the link is up or not
1417*4882a593Smuzhiyun  *
1418*4882a593Smuzhiyun  * The exact combo of LEDs if on is true is determined by looking
1419*4882a593Smuzhiyun  * at the ibcstatus.
1420*4882a593Smuzhiyun  * These LEDs indicate the physical and logical state of IB link.
1421*4882a593Smuzhiyun  * For this chip (at least with recommended board pinouts), LED1
1422*4882a593Smuzhiyun  * is Yellow (logical state) and LED2 is Green (physical state),
1423*4882a593Smuzhiyun  *
1424*4882a593Smuzhiyun  * Note:  We try to match the Mellanox HCA LED behavior as best
1425*4882a593Smuzhiyun  * we can.  Green indicates physical link state is OK (something is
1426*4882a593Smuzhiyun  * plugged in, and we can train).
1427*4882a593Smuzhiyun  * Amber indicates the link is logically up (ACTIVE).
1428*4882a593Smuzhiyun  * Mellanox further blinks the amber LED to indicate data packet
1429*4882a593Smuzhiyun  * activity, but we have no hardware support for that, so it would
1430*4882a593Smuzhiyun  * require waking up every 10-20 msecs and checking the counters
1431*4882a593Smuzhiyun  * on the chip, and then turning the LED off if appropriate.  That's
1432*4882a593Smuzhiyun  * visible overhead, so not something we will do.
1433*4882a593Smuzhiyun  *
1434*4882a593Smuzhiyun  */
qib_6120_setup_setextled(struct qib_pportdata * ppd,u32 on)1435*4882a593Smuzhiyun static void qib_6120_setup_setextled(struct qib_pportdata *ppd, u32 on)
1436*4882a593Smuzhiyun {
1437*4882a593Smuzhiyun 	u64 extctl, val, lst, ltst;
1438*4882a593Smuzhiyun 	unsigned long flags;
1439*4882a593Smuzhiyun 	struct qib_devdata *dd = ppd->dd;
1440*4882a593Smuzhiyun 
1441*4882a593Smuzhiyun 	/*
1442*4882a593Smuzhiyun 	 * The diags use the LED to indicate diag info, so we leave
1443*4882a593Smuzhiyun 	 * the external LED alone when the diags are running.
1444*4882a593Smuzhiyun 	 */
1445*4882a593Smuzhiyun 	if (dd->diag_client)
1446*4882a593Smuzhiyun 		return;
1447*4882a593Smuzhiyun 
1448*4882a593Smuzhiyun 	/* Allow override of LED display for, e.g. Locating system in rack */
1449*4882a593Smuzhiyun 	if (ppd->led_override) {
1450*4882a593Smuzhiyun 		ltst = (ppd->led_override & QIB_LED_PHYS) ?
1451*4882a593Smuzhiyun 			IB_PHYSPORTSTATE_LINKUP : IB_PHYSPORTSTATE_DISABLED,
1452*4882a593Smuzhiyun 		lst = (ppd->led_override & QIB_LED_LOG) ?
1453*4882a593Smuzhiyun 			IB_PORT_ACTIVE : IB_PORT_DOWN;
1454*4882a593Smuzhiyun 	} else if (on) {
1455*4882a593Smuzhiyun 		val = qib_read_kreg64(dd, kr_ibcstatus);
1456*4882a593Smuzhiyun 		ltst = qib_6120_phys_portstate(val);
1457*4882a593Smuzhiyun 		lst = qib_6120_iblink_state(val);
1458*4882a593Smuzhiyun 	} else {
1459*4882a593Smuzhiyun 		ltst = 0;
1460*4882a593Smuzhiyun 		lst = 0;
1461*4882a593Smuzhiyun 	}
1462*4882a593Smuzhiyun 
1463*4882a593Smuzhiyun 	spin_lock_irqsave(&dd->cspec->gpio_lock, flags);
1464*4882a593Smuzhiyun 	extctl = dd->cspec->extctrl & ~(SYM_MASK(EXTCtrl, LEDPriPortGreenOn) |
1465*4882a593Smuzhiyun 				 SYM_MASK(EXTCtrl, LEDPriPortYellowOn));
1466*4882a593Smuzhiyun 
1467*4882a593Smuzhiyun 	if (ltst == IB_PHYSPORTSTATE_LINKUP)
1468*4882a593Smuzhiyun 		extctl |= SYM_MASK(EXTCtrl, LEDPriPortYellowOn);
1469*4882a593Smuzhiyun 	if (lst == IB_PORT_ACTIVE)
1470*4882a593Smuzhiyun 		extctl |= SYM_MASK(EXTCtrl, LEDPriPortGreenOn);
1471*4882a593Smuzhiyun 	dd->cspec->extctrl = extctl;
1472*4882a593Smuzhiyun 	qib_write_kreg(dd, kr_extctrl, extctl);
1473*4882a593Smuzhiyun 	spin_unlock_irqrestore(&dd->cspec->gpio_lock, flags);
1474*4882a593Smuzhiyun }
1475*4882a593Smuzhiyun 
1476*4882a593Smuzhiyun /**
1477*4882a593Smuzhiyun  * qib_6120_setup_cleanup - clean up any per-chip chip-specific stuff
1478*4882a593Smuzhiyun  * @dd: the qlogic_ib device
1479*4882a593Smuzhiyun  *
1480*4882a593Smuzhiyun  * This is called during driver unload.
1481*4882a593Smuzhiyun */
qib_6120_setup_cleanup(struct qib_devdata * dd)1482*4882a593Smuzhiyun static void qib_6120_setup_cleanup(struct qib_devdata *dd)
1483*4882a593Smuzhiyun {
1484*4882a593Smuzhiyun 	qib_free_irq(dd);
1485*4882a593Smuzhiyun 	kfree(dd->cspec->cntrs);
1486*4882a593Smuzhiyun 	kfree(dd->cspec->portcntrs);
1487*4882a593Smuzhiyun 	if (dd->cspec->dummy_hdrq) {
1488*4882a593Smuzhiyun 		dma_free_coherent(&dd->pcidev->dev,
1489*4882a593Smuzhiyun 				  ALIGN(dd->rcvhdrcnt *
1490*4882a593Smuzhiyun 					dd->rcvhdrentsize *
1491*4882a593Smuzhiyun 					sizeof(u32), PAGE_SIZE),
1492*4882a593Smuzhiyun 				  dd->cspec->dummy_hdrq,
1493*4882a593Smuzhiyun 				  dd->cspec->dummy_hdrq_phys);
1494*4882a593Smuzhiyun 		dd->cspec->dummy_hdrq = NULL;
1495*4882a593Smuzhiyun 	}
1496*4882a593Smuzhiyun }
1497*4882a593Smuzhiyun 
qib_wantpiobuf_6120_intr(struct qib_devdata * dd,u32 needint)1498*4882a593Smuzhiyun static void qib_wantpiobuf_6120_intr(struct qib_devdata *dd, u32 needint)
1499*4882a593Smuzhiyun {
1500*4882a593Smuzhiyun 	unsigned long flags;
1501*4882a593Smuzhiyun 
1502*4882a593Smuzhiyun 	spin_lock_irqsave(&dd->sendctrl_lock, flags);
1503*4882a593Smuzhiyun 	if (needint)
1504*4882a593Smuzhiyun 		dd->sendctrl |= SYM_MASK(SendCtrl, PIOIntBufAvail);
1505*4882a593Smuzhiyun 	else
1506*4882a593Smuzhiyun 		dd->sendctrl &= ~SYM_MASK(SendCtrl, PIOIntBufAvail);
1507*4882a593Smuzhiyun 	qib_write_kreg(dd, kr_sendctrl, dd->sendctrl);
1508*4882a593Smuzhiyun 	qib_write_kreg(dd, kr_scratch, 0ULL);
1509*4882a593Smuzhiyun 	spin_unlock_irqrestore(&dd->sendctrl_lock, flags);
1510*4882a593Smuzhiyun }
1511*4882a593Smuzhiyun 
1512*4882a593Smuzhiyun /*
1513*4882a593Smuzhiyun  * handle errors and unusual events first, separate function
1514*4882a593Smuzhiyun  * to improve cache hits for fast path interrupt handling
1515*4882a593Smuzhiyun  */
unlikely_6120_intr(struct qib_devdata * dd,u64 istat)1516*4882a593Smuzhiyun static noinline void unlikely_6120_intr(struct qib_devdata *dd, u64 istat)
1517*4882a593Smuzhiyun {
1518*4882a593Smuzhiyun 	if (unlikely(istat & ~QLOGIC_IB_I_BITSEXTANT))
1519*4882a593Smuzhiyun 		qib_dev_err(dd, "interrupt with unknown interrupts %Lx set\n",
1520*4882a593Smuzhiyun 			    istat & ~QLOGIC_IB_I_BITSEXTANT);
1521*4882a593Smuzhiyun 
1522*4882a593Smuzhiyun 	if (istat & QLOGIC_IB_I_ERROR) {
1523*4882a593Smuzhiyun 		u64 estat = 0;
1524*4882a593Smuzhiyun 
1525*4882a593Smuzhiyun 		qib_stats.sps_errints++;
1526*4882a593Smuzhiyun 		estat = qib_read_kreg64(dd, kr_errstatus);
1527*4882a593Smuzhiyun 		if (!estat)
1528*4882a593Smuzhiyun 			qib_devinfo(dd->pcidev,
1529*4882a593Smuzhiyun 				"error interrupt (%Lx), but no error bits set!\n",
1530*4882a593Smuzhiyun 				istat);
1531*4882a593Smuzhiyun 		handle_6120_errors(dd, estat);
1532*4882a593Smuzhiyun 	}
1533*4882a593Smuzhiyun 
1534*4882a593Smuzhiyun 	if (istat & QLOGIC_IB_I_GPIO) {
1535*4882a593Smuzhiyun 		u32 gpiostatus;
1536*4882a593Smuzhiyun 		u32 to_clear = 0;
1537*4882a593Smuzhiyun 
1538*4882a593Smuzhiyun 		/*
1539*4882a593Smuzhiyun 		 * GPIO_3..5 on IBA6120 Rev2 chips indicate
1540*4882a593Smuzhiyun 		 * errors that we need to count.
1541*4882a593Smuzhiyun 		 */
1542*4882a593Smuzhiyun 		gpiostatus = qib_read_kreg32(dd, kr_gpio_status);
1543*4882a593Smuzhiyun 		/* First the error-counter case. */
1544*4882a593Smuzhiyun 		if (gpiostatus & GPIO_ERRINTR_MASK) {
1545*4882a593Smuzhiyun 			/* want to clear the bits we see asserted. */
1546*4882a593Smuzhiyun 			to_clear |= (gpiostatus & GPIO_ERRINTR_MASK);
1547*4882a593Smuzhiyun 
1548*4882a593Smuzhiyun 			/*
1549*4882a593Smuzhiyun 			 * Count appropriately, clear bits out of our copy,
1550*4882a593Smuzhiyun 			 * as they have been "handled".
1551*4882a593Smuzhiyun 			 */
1552*4882a593Smuzhiyun 			if (gpiostatus & (1 << GPIO_RXUVL_BIT))
1553*4882a593Smuzhiyun 				dd->cspec->rxfc_unsupvl_errs++;
1554*4882a593Smuzhiyun 			if (gpiostatus & (1 << GPIO_OVRUN_BIT))
1555*4882a593Smuzhiyun 				dd->cspec->overrun_thresh_errs++;
1556*4882a593Smuzhiyun 			if (gpiostatus & (1 << GPIO_LLI_BIT))
1557*4882a593Smuzhiyun 				dd->cspec->lli_errs++;
1558*4882a593Smuzhiyun 			gpiostatus &= ~GPIO_ERRINTR_MASK;
1559*4882a593Smuzhiyun 		}
1560*4882a593Smuzhiyun 		if (gpiostatus) {
1561*4882a593Smuzhiyun 			/*
1562*4882a593Smuzhiyun 			 * Some unexpected bits remain. If they could have
1563*4882a593Smuzhiyun 			 * caused the interrupt, complain and clear.
1564*4882a593Smuzhiyun 			 * To avoid repetition of this condition, also clear
1565*4882a593Smuzhiyun 			 * the mask. It is almost certainly due to error.
1566*4882a593Smuzhiyun 			 */
1567*4882a593Smuzhiyun 			const u32 mask = qib_read_kreg32(dd, kr_gpio_mask);
1568*4882a593Smuzhiyun 
1569*4882a593Smuzhiyun 			/*
1570*4882a593Smuzhiyun 			 * Also check that the chip reflects our shadow,
1571*4882a593Smuzhiyun 			 * and report issues, If they caused the interrupt.
1572*4882a593Smuzhiyun 			 * we will suppress by refreshing from the shadow.
1573*4882a593Smuzhiyun 			 */
1574*4882a593Smuzhiyun 			if (mask & gpiostatus) {
1575*4882a593Smuzhiyun 				to_clear |= (gpiostatus & mask);
1576*4882a593Smuzhiyun 				dd->cspec->gpio_mask &= ~(gpiostatus & mask);
1577*4882a593Smuzhiyun 				qib_write_kreg(dd, kr_gpio_mask,
1578*4882a593Smuzhiyun 					       dd->cspec->gpio_mask);
1579*4882a593Smuzhiyun 			}
1580*4882a593Smuzhiyun 		}
1581*4882a593Smuzhiyun 		if (to_clear)
1582*4882a593Smuzhiyun 			qib_write_kreg(dd, kr_gpio_clear, (u64) to_clear);
1583*4882a593Smuzhiyun 	}
1584*4882a593Smuzhiyun }
1585*4882a593Smuzhiyun 
qib_6120intr(int irq,void * data)1586*4882a593Smuzhiyun static irqreturn_t qib_6120intr(int irq, void *data)
1587*4882a593Smuzhiyun {
1588*4882a593Smuzhiyun 	struct qib_devdata *dd = data;
1589*4882a593Smuzhiyun 	irqreturn_t ret;
1590*4882a593Smuzhiyun 	u32 istat, ctxtrbits, rmask, crcs = 0;
1591*4882a593Smuzhiyun 	unsigned i;
1592*4882a593Smuzhiyun 
1593*4882a593Smuzhiyun 	if ((dd->flags & (QIB_PRESENT | QIB_BADINTR)) != QIB_PRESENT) {
1594*4882a593Smuzhiyun 		/*
1595*4882a593Smuzhiyun 		 * This return value is not great, but we do not want the
1596*4882a593Smuzhiyun 		 * interrupt core code to remove our interrupt handler
1597*4882a593Smuzhiyun 		 * because we don't appear to be handling an interrupt
1598*4882a593Smuzhiyun 		 * during a chip reset.
1599*4882a593Smuzhiyun 		 */
1600*4882a593Smuzhiyun 		ret = IRQ_HANDLED;
1601*4882a593Smuzhiyun 		goto bail;
1602*4882a593Smuzhiyun 	}
1603*4882a593Smuzhiyun 
1604*4882a593Smuzhiyun 	istat = qib_read_kreg32(dd, kr_intstatus);
1605*4882a593Smuzhiyun 
1606*4882a593Smuzhiyun 	if (unlikely(!istat)) {
1607*4882a593Smuzhiyun 		ret = IRQ_NONE; /* not our interrupt, or already handled */
1608*4882a593Smuzhiyun 		goto bail;
1609*4882a593Smuzhiyun 	}
1610*4882a593Smuzhiyun 	if (unlikely(istat == -1)) {
1611*4882a593Smuzhiyun 		qib_bad_intrstatus(dd);
1612*4882a593Smuzhiyun 		/* don't know if it was our interrupt or not */
1613*4882a593Smuzhiyun 		ret = IRQ_NONE;
1614*4882a593Smuzhiyun 		goto bail;
1615*4882a593Smuzhiyun 	}
1616*4882a593Smuzhiyun 
1617*4882a593Smuzhiyun 	this_cpu_inc(*dd->int_counter);
1618*4882a593Smuzhiyun 
1619*4882a593Smuzhiyun 	if (unlikely(istat & (~QLOGIC_IB_I_BITSEXTANT |
1620*4882a593Smuzhiyun 			      QLOGIC_IB_I_GPIO | QLOGIC_IB_I_ERROR)))
1621*4882a593Smuzhiyun 		unlikely_6120_intr(dd, istat);
1622*4882a593Smuzhiyun 
1623*4882a593Smuzhiyun 	/*
1624*4882a593Smuzhiyun 	 * Clear the interrupt bits we found set, relatively early, so we
1625*4882a593Smuzhiyun 	 * "know" know the chip will have seen this by the time we process
1626*4882a593Smuzhiyun 	 * the queue, and will re-interrupt if necessary.  The processor
1627*4882a593Smuzhiyun 	 * itself won't take the interrupt again until we return.
1628*4882a593Smuzhiyun 	 */
1629*4882a593Smuzhiyun 	qib_write_kreg(dd, kr_intclear, istat);
1630*4882a593Smuzhiyun 
1631*4882a593Smuzhiyun 	/*
1632*4882a593Smuzhiyun 	 * Handle kernel receive queues before checking for pio buffers
1633*4882a593Smuzhiyun 	 * available since receives can overflow; piobuf waiters can afford
1634*4882a593Smuzhiyun 	 * a few extra cycles, since they were waiting anyway.
1635*4882a593Smuzhiyun 	 */
1636*4882a593Smuzhiyun 	ctxtrbits = istat &
1637*4882a593Smuzhiyun 		((QLOGIC_IB_I_RCVAVAIL_MASK << QLOGIC_IB_I_RCVAVAIL_SHIFT) |
1638*4882a593Smuzhiyun 		 (QLOGIC_IB_I_RCVURG_MASK << QLOGIC_IB_I_RCVURG_SHIFT));
1639*4882a593Smuzhiyun 	if (ctxtrbits) {
1640*4882a593Smuzhiyun 		rmask = (1U << QLOGIC_IB_I_RCVAVAIL_SHIFT) |
1641*4882a593Smuzhiyun 			(1U << QLOGIC_IB_I_RCVURG_SHIFT);
1642*4882a593Smuzhiyun 		for (i = 0; i < dd->first_user_ctxt; i++) {
1643*4882a593Smuzhiyun 			if (ctxtrbits & rmask) {
1644*4882a593Smuzhiyun 				ctxtrbits &= ~rmask;
1645*4882a593Smuzhiyun 				crcs += qib_kreceive(dd->rcd[i],
1646*4882a593Smuzhiyun 						     &dd->cspec->lli_counter,
1647*4882a593Smuzhiyun 						     NULL);
1648*4882a593Smuzhiyun 			}
1649*4882a593Smuzhiyun 			rmask <<= 1;
1650*4882a593Smuzhiyun 		}
1651*4882a593Smuzhiyun 		if (crcs) {
1652*4882a593Smuzhiyun 			u32 cntr = dd->cspec->lli_counter;
1653*4882a593Smuzhiyun 
1654*4882a593Smuzhiyun 			cntr += crcs;
1655*4882a593Smuzhiyun 			if (cntr) {
1656*4882a593Smuzhiyun 				if (cntr > dd->cspec->lli_thresh) {
1657*4882a593Smuzhiyun 					dd->cspec->lli_counter = 0;
1658*4882a593Smuzhiyun 					dd->cspec->lli_errs++;
1659*4882a593Smuzhiyun 				} else
1660*4882a593Smuzhiyun 					dd->cspec->lli_counter += cntr;
1661*4882a593Smuzhiyun 			}
1662*4882a593Smuzhiyun 		}
1663*4882a593Smuzhiyun 
1664*4882a593Smuzhiyun 
1665*4882a593Smuzhiyun 		if (ctxtrbits) {
1666*4882a593Smuzhiyun 			ctxtrbits =
1667*4882a593Smuzhiyun 				(ctxtrbits >> QLOGIC_IB_I_RCVAVAIL_SHIFT) |
1668*4882a593Smuzhiyun 				(ctxtrbits >> QLOGIC_IB_I_RCVURG_SHIFT);
1669*4882a593Smuzhiyun 			qib_handle_urcv(dd, ctxtrbits);
1670*4882a593Smuzhiyun 		}
1671*4882a593Smuzhiyun 	}
1672*4882a593Smuzhiyun 
1673*4882a593Smuzhiyun 	if ((istat & QLOGIC_IB_I_SPIOBUFAVAIL) && (dd->flags & QIB_INITTED))
1674*4882a593Smuzhiyun 		qib_ib_piobufavail(dd);
1675*4882a593Smuzhiyun 
1676*4882a593Smuzhiyun 	ret = IRQ_HANDLED;
1677*4882a593Smuzhiyun bail:
1678*4882a593Smuzhiyun 	return ret;
1679*4882a593Smuzhiyun }
1680*4882a593Smuzhiyun 
1681*4882a593Smuzhiyun /*
1682*4882a593Smuzhiyun  * Set up our chip-specific interrupt handler
1683*4882a593Smuzhiyun  * The interrupt type has already been setup, so
1684*4882a593Smuzhiyun  * we just need to do the registration and error checking.
1685*4882a593Smuzhiyun  */
qib_setup_6120_interrupt(struct qib_devdata * dd)1686*4882a593Smuzhiyun static void qib_setup_6120_interrupt(struct qib_devdata *dd)
1687*4882a593Smuzhiyun {
1688*4882a593Smuzhiyun 	int ret;
1689*4882a593Smuzhiyun 
1690*4882a593Smuzhiyun 	/*
1691*4882a593Smuzhiyun 	 * If the chip supports added error indication via GPIO pins,
1692*4882a593Smuzhiyun 	 * enable interrupts on those bits so the interrupt routine
1693*4882a593Smuzhiyun 	 * can count the events. Also set flag so interrupt routine
1694*4882a593Smuzhiyun 	 * can know they are expected.
1695*4882a593Smuzhiyun 	 */
1696*4882a593Smuzhiyun 	if (SYM_FIELD(dd->revision, Revision_R,
1697*4882a593Smuzhiyun 		      ChipRevMinor) > 1) {
1698*4882a593Smuzhiyun 		/* Rev2+ reports extra errors via internal GPIO pins */
1699*4882a593Smuzhiyun 		dd->cspec->gpio_mask |= GPIO_ERRINTR_MASK;
1700*4882a593Smuzhiyun 		qib_write_kreg(dd, kr_gpio_mask, dd->cspec->gpio_mask);
1701*4882a593Smuzhiyun 	}
1702*4882a593Smuzhiyun 
1703*4882a593Smuzhiyun 	ret = pci_request_irq(dd->pcidev, 0, qib_6120intr, NULL, dd,
1704*4882a593Smuzhiyun 			      QIB_DRV_NAME);
1705*4882a593Smuzhiyun 	if (ret)
1706*4882a593Smuzhiyun 		qib_dev_err(dd,
1707*4882a593Smuzhiyun 			    "Couldn't setup interrupt (irq=%d): %d\n",
1708*4882a593Smuzhiyun 			    pci_irq_vector(dd->pcidev, 0), ret);
1709*4882a593Smuzhiyun }
1710*4882a593Smuzhiyun 
1711*4882a593Smuzhiyun /**
1712*4882a593Smuzhiyun  * pe_boardname - fill in the board name
1713*4882a593Smuzhiyun  * @dd: the qlogic_ib device
1714*4882a593Smuzhiyun  *
1715*4882a593Smuzhiyun  * info is based on the board revision register
1716*4882a593Smuzhiyun  */
pe_boardname(struct qib_devdata * dd)1717*4882a593Smuzhiyun static void pe_boardname(struct qib_devdata *dd)
1718*4882a593Smuzhiyun {
1719*4882a593Smuzhiyun 	u32 boardid;
1720*4882a593Smuzhiyun 
1721*4882a593Smuzhiyun 	boardid = SYM_FIELD(dd->revision, Revision,
1722*4882a593Smuzhiyun 			    BoardID);
1723*4882a593Smuzhiyun 
1724*4882a593Smuzhiyun 	switch (boardid) {
1725*4882a593Smuzhiyun 	case 2:
1726*4882a593Smuzhiyun 		dd->boardname = "InfiniPath_QLE7140";
1727*4882a593Smuzhiyun 		break;
1728*4882a593Smuzhiyun 	default:
1729*4882a593Smuzhiyun 		qib_dev_err(dd, "Unknown 6120 board with ID %u\n", boardid);
1730*4882a593Smuzhiyun 		dd->boardname = "Unknown_InfiniPath_6120";
1731*4882a593Smuzhiyun 		break;
1732*4882a593Smuzhiyun 	}
1733*4882a593Smuzhiyun 
1734*4882a593Smuzhiyun 	if (dd->majrev != 4 || !dd->minrev || dd->minrev > 2)
1735*4882a593Smuzhiyun 		qib_dev_err(dd,
1736*4882a593Smuzhiyun 			    "Unsupported InfiniPath hardware revision %u.%u!\n",
1737*4882a593Smuzhiyun 			    dd->majrev, dd->minrev);
1738*4882a593Smuzhiyun 
1739*4882a593Smuzhiyun 	snprintf(dd->boardversion, sizeof(dd->boardversion),
1740*4882a593Smuzhiyun 		 "ChipABI %u.%u, %s, InfiniPath%u %u.%u, SW Compat %u\n",
1741*4882a593Smuzhiyun 		 QIB_CHIP_VERS_MAJ, QIB_CHIP_VERS_MIN, dd->boardname,
1742*4882a593Smuzhiyun 		 (unsigned int)SYM_FIELD(dd->revision, Revision_R, Arch),
1743*4882a593Smuzhiyun 		 dd->majrev, dd->minrev,
1744*4882a593Smuzhiyun 		 (unsigned int)SYM_FIELD(dd->revision, Revision_R, SW));
1745*4882a593Smuzhiyun }
1746*4882a593Smuzhiyun 
1747*4882a593Smuzhiyun /*
1748*4882a593Smuzhiyun  * This routine sleeps, so it can only be called from user context, not
1749*4882a593Smuzhiyun  * from interrupt context.  If we need interrupt context, we can split
1750*4882a593Smuzhiyun  * it into two routines.
1751*4882a593Smuzhiyun  */
qib_6120_setup_reset(struct qib_devdata * dd)1752*4882a593Smuzhiyun static int qib_6120_setup_reset(struct qib_devdata *dd)
1753*4882a593Smuzhiyun {
1754*4882a593Smuzhiyun 	u64 val;
1755*4882a593Smuzhiyun 	int i;
1756*4882a593Smuzhiyun 	int ret;
1757*4882a593Smuzhiyun 	u16 cmdval;
1758*4882a593Smuzhiyun 	u8 int_line, clinesz;
1759*4882a593Smuzhiyun 
1760*4882a593Smuzhiyun 	qib_pcie_getcmd(dd, &cmdval, &int_line, &clinesz);
1761*4882a593Smuzhiyun 
1762*4882a593Smuzhiyun 	/* Use ERROR so it shows up in logs, etc. */
1763*4882a593Smuzhiyun 	qib_dev_err(dd, "Resetting InfiniPath unit %u\n", dd->unit);
1764*4882a593Smuzhiyun 
1765*4882a593Smuzhiyun 	/* no interrupts till re-initted */
1766*4882a593Smuzhiyun 	qib_6120_set_intr_state(dd, 0);
1767*4882a593Smuzhiyun 
1768*4882a593Smuzhiyun 	dd->cspec->ibdeltainprog = 0;
1769*4882a593Smuzhiyun 	dd->cspec->ibsymdelta = 0;
1770*4882a593Smuzhiyun 	dd->cspec->iblnkerrdelta = 0;
1771*4882a593Smuzhiyun 
1772*4882a593Smuzhiyun 	/*
1773*4882a593Smuzhiyun 	 * Keep chip from being accessed until we are ready.  Use
1774*4882a593Smuzhiyun 	 * writeq() directly, to allow the write even though QIB_PRESENT
1775*4882a593Smuzhiyun 	 * isn't set.
1776*4882a593Smuzhiyun 	 */
1777*4882a593Smuzhiyun 	dd->flags &= ~(QIB_INITTED | QIB_PRESENT);
1778*4882a593Smuzhiyun 	/* so we check interrupts work again */
1779*4882a593Smuzhiyun 	dd->z_int_counter = qib_int_counter(dd);
1780*4882a593Smuzhiyun 	val = dd->control | QLOGIC_IB_C_RESET;
1781*4882a593Smuzhiyun 	writeq(val, &dd->kregbase[kr_control]);
1782*4882a593Smuzhiyun 	mb(); /* prevent compiler re-ordering around actual reset */
1783*4882a593Smuzhiyun 
1784*4882a593Smuzhiyun 	for (i = 1; i <= 5; i++) {
1785*4882a593Smuzhiyun 		/*
1786*4882a593Smuzhiyun 		 * Allow MBIST, etc. to complete; longer on each retry.
1787*4882a593Smuzhiyun 		 * We sometimes get machine checks from bus timeout if no
1788*4882a593Smuzhiyun 		 * response, so for now, make it *really* long.
1789*4882a593Smuzhiyun 		 */
1790*4882a593Smuzhiyun 		msleep(1000 + (1 + i) * 2000);
1791*4882a593Smuzhiyun 
1792*4882a593Smuzhiyun 		qib_pcie_reenable(dd, cmdval, int_line, clinesz);
1793*4882a593Smuzhiyun 
1794*4882a593Smuzhiyun 		/*
1795*4882a593Smuzhiyun 		 * Use readq directly, so we don't need to mark it as PRESENT
1796*4882a593Smuzhiyun 		 * until we get a successful indication that all is well.
1797*4882a593Smuzhiyun 		 */
1798*4882a593Smuzhiyun 		val = readq(&dd->kregbase[kr_revision]);
1799*4882a593Smuzhiyun 		if (val == dd->revision) {
1800*4882a593Smuzhiyun 			dd->flags |= QIB_PRESENT; /* it's back */
1801*4882a593Smuzhiyun 			ret = qib_reinit_intr(dd);
1802*4882a593Smuzhiyun 			goto bail;
1803*4882a593Smuzhiyun 		}
1804*4882a593Smuzhiyun 	}
1805*4882a593Smuzhiyun 	ret = 0; /* failed */
1806*4882a593Smuzhiyun 
1807*4882a593Smuzhiyun bail:
1808*4882a593Smuzhiyun 	if (ret) {
1809*4882a593Smuzhiyun 		if (qib_pcie_params(dd, dd->lbus_width, NULL))
1810*4882a593Smuzhiyun 			qib_dev_err(dd,
1811*4882a593Smuzhiyun 				"Reset failed to setup PCIe or interrupts; continuing anyway\n");
1812*4882a593Smuzhiyun 		/* clear the reset error, init error/hwerror mask */
1813*4882a593Smuzhiyun 		qib_6120_init_hwerrors(dd);
1814*4882a593Smuzhiyun 		/* for Rev2 error interrupts; nop for rev 1 */
1815*4882a593Smuzhiyun 		qib_write_kreg(dd, kr_gpio_mask, dd->cspec->gpio_mask);
1816*4882a593Smuzhiyun 		/* clear the reset error, init error/hwerror mask */
1817*4882a593Smuzhiyun 		qib_6120_init_hwerrors(dd);
1818*4882a593Smuzhiyun 	}
1819*4882a593Smuzhiyun 	return ret;
1820*4882a593Smuzhiyun }
1821*4882a593Smuzhiyun 
1822*4882a593Smuzhiyun /**
1823*4882a593Smuzhiyun  * qib_6120_put_tid - write a TID in chip
1824*4882a593Smuzhiyun  * @dd: the qlogic_ib device
1825*4882a593Smuzhiyun  * @tidptr: pointer to the expected TID (in chip) to update
1826*4882a593Smuzhiyun  * @tidtype: RCVHQ_RCV_TYPE_EAGER (1) for eager, RCVHQ_RCV_TYPE_EXPECTED (0)
1827*4882a593Smuzhiyun  * for expected
1828*4882a593Smuzhiyun  * @pa: physical address of in memory buffer; tidinvalid if freeing
1829*4882a593Smuzhiyun  *
1830*4882a593Smuzhiyun  * This exists as a separate routine to allow for special locking etc.
1831*4882a593Smuzhiyun  * It's used for both the full cleanup on exit, as well as the normal
1832*4882a593Smuzhiyun  * setup and teardown.
1833*4882a593Smuzhiyun  */
qib_6120_put_tid(struct qib_devdata * dd,u64 __iomem * tidptr,u32 type,unsigned long pa)1834*4882a593Smuzhiyun static void qib_6120_put_tid(struct qib_devdata *dd, u64 __iomem *tidptr,
1835*4882a593Smuzhiyun 			     u32 type, unsigned long pa)
1836*4882a593Smuzhiyun {
1837*4882a593Smuzhiyun 	u32 __iomem *tidp32 = (u32 __iomem *)tidptr;
1838*4882a593Smuzhiyun 	unsigned long flags;
1839*4882a593Smuzhiyun 	int tidx;
1840*4882a593Smuzhiyun 	spinlock_t *tidlockp; /* select appropriate spinlock */
1841*4882a593Smuzhiyun 
1842*4882a593Smuzhiyun 	if (!dd->kregbase)
1843*4882a593Smuzhiyun 		return;
1844*4882a593Smuzhiyun 
1845*4882a593Smuzhiyun 	if (pa != dd->tidinvalid) {
1846*4882a593Smuzhiyun 		if (pa & ((1U << 11) - 1)) {
1847*4882a593Smuzhiyun 			qib_dev_err(dd, "Physaddr %lx not 2KB aligned!\n",
1848*4882a593Smuzhiyun 				    pa);
1849*4882a593Smuzhiyun 			return;
1850*4882a593Smuzhiyun 		}
1851*4882a593Smuzhiyun 		pa >>= 11;
1852*4882a593Smuzhiyun 		if (pa & ~QLOGIC_IB_RT_ADDR_MASK) {
1853*4882a593Smuzhiyun 			qib_dev_err(dd,
1854*4882a593Smuzhiyun 				"Physical page address 0x%lx larger than supported\n",
1855*4882a593Smuzhiyun 				pa);
1856*4882a593Smuzhiyun 			return;
1857*4882a593Smuzhiyun 		}
1858*4882a593Smuzhiyun 
1859*4882a593Smuzhiyun 		if (type == RCVHQ_RCV_TYPE_EAGER)
1860*4882a593Smuzhiyun 			pa |= dd->tidtemplate;
1861*4882a593Smuzhiyun 		else /* for now, always full 4KB page */
1862*4882a593Smuzhiyun 			pa |= 2 << 29;
1863*4882a593Smuzhiyun 	}
1864*4882a593Smuzhiyun 
1865*4882a593Smuzhiyun 	/*
1866*4882a593Smuzhiyun 	 * Avoid chip issue by writing the scratch register
1867*4882a593Smuzhiyun 	 * before and after the TID, and with an io write barrier.
1868*4882a593Smuzhiyun 	 * We use a spinlock around the writes, so they can't intermix
1869*4882a593Smuzhiyun 	 * with other TID (eager or expected) writes (the chip problem
1870*4882a593Smuzhiyun 	 * is triggered by back to back TID writes). Unfortunately, this
1871*4882a593Smuzhiyun 	 * call can be done from interrupt level for the ctxt 0 eager TIDs,
1872*4882a593Smuzhiyun 	 * so we have to use irqsave locks.
1873*4882a593Smuzhiyun 	 */
1874*4882a593Smuzhiyun 	/*
1875*4882a593Smuzhiyun 	 * Assumes tidptr always > egrtidbase
1876*4882a593Smuzhiyun 	 * if type == RCVHQ_RCV_TYPE_EAGER.
1877*4882a593Smuzhiyun 	 */
1878*4882a593Smuzhiyun 	tidx = tidptr - dd->egrtidbase;
1879*4882a593Smuzhiyun 
1880*4882a593Smuzhiyun 	tidlockp = (type == RCVHQ_RCV_TYPE_EAGER && tidx < dd->rcvhdrcnt)
1881*4882a593Smuzhiyun 		? &dd->cspec->kernel_tid_lock : &dd->cspec->user_tid_lock;
1882*4882a593Smuzhiyun 	spin_lock_irqsave(tidlockp, flags);
1883*4882a593Smuzhiyun 	qib_write_kreg(dd, kr_scratch, 0xfeeddeaf);
1884*4882a593Smuzhiyun 	writel(pa, tidp32);
1885*4882a593Smuzhiyun 	qib_write_kreg(dd, kr_scratch, 0xdeadbeef);
1886*4882a593Smuzhiyun 	spin_unlock_irqrestore(tidlockp, flags);
1887*4882a593Smuzhiyun }
1888*4882a593Smuzhiyun 
1889*4882a593Smuzhiyun /**
1890*4882a593Smuzhiyun  * qib_6120_put_tid_2 - write a TID in chip, Revision 2 or higher
1891*4882a593Smuzhiyun  * @dd: the qlogic_ib device
1892*4882a593Smuzhiyun  * @tidptr: pointer to the expected TID (in chip) to update
1893*4882a593Smuzhiyun  * @tidtype: RCVHQ_RCV_TYPE_EAGER (1) for eager, RCVHQ_RCV_TYPE_EXPECTED (0)
1894*4882a593Smuzhiyun  * for expected
1895*4882a593Smuzhiyun  * @pa: physical address of in memory buffer; tidinvalid if freeing
1896*4882a593Smuzhiyun  *
1897*4882a593Smuzhiyun  * This exists as a separate routine to allow for selection of the
1898*4882a593Smuzhiyun  * appropriate "flavor". The static calls in cleanup just use the
1899*4882a593Smuzhiyun  * revision-agnostic form, as they are not performance critical.
1900*4882a593Smuzhiyun  */
qib_6120_put_tid_2(struct qib_devdata * dd,u64 __iomem * tidptr,u32 type,unsigned long pa)1901*4882a593Smuzhiyun static void qib_6120_put_tid_2(struct qib_devdata *dd, u64 __iomem *tidptr,
1902*4882a593Smuzhiyun 			       u32 type, unsigned long pa)
1903*4882a593Smuzhiyun {
1904*4882a593Smuzhiyun 	u32 __iomem *tidp32 = (u32 __iomem *)tidptr;
1905*4882a593Smuzhiyun 
1906*4882a593Smuzhiyun 	if (!dd->kregbase)
1907*4882a593Smuzhiyun 		return;
1908*4882a593Smuzhiyun 
1909*4882a593Smuzhiyun 	if (pa != dd->tidinvalid) {
1910*4882a593Smuzhiyun 		if (pa & ((1U << 11) - 1)) {
1911*4882a593Smuzhiyun 			qib_dev_err(dd, "Physaddr %lx not 2KB aligned!\n",
1912*4882a593Smuzhiyun 				    pa);
1913*4882a593Smuzhiyun 			return;
1914*4882a593Smuzhiyun 		}
1915*4882a593Smuzhiyun 		pa >>= 11;
1916*4882a593Smuzhiyun 		if (pa & ~QLOGIC_IB_RT_ADDR_MASK) {
1917*4882a593Smuzhiyun 			qib_dev_err(dd,
1918*4882a593Smuzhiyun 				"Physical page address 0x%lx larger than supported\n",
1919*4882a593Smuzhiyun 				pa);
1920*4882a593Smuzhiyun 			return;
1921*4882a593Smuzhiyun 		}
1922*4882a593Smuzhiyun 
1923*4882a593Smuzhiyun 		if (type == RCVHQ_RCV_TYPE_EAGER)
1924*4882a593Smuzhiyun 			pa |= dd->tidtemplate;
1925*4882a593Smuzhiyun 		else /* for now, always full 4KB page */
1926*4882a593Smuzhiyun 			pa |= 2 << 29;
1927*4882a593Smuzhiyun 	}
1928*4882a593Smuzhiyun 	writel(pa, tidp32);
1929*4882a593Smuzhiyun }
1930*4882a593Smuzhiyun 
1931*4882a593Smuzhiyun 
1932*4882a593Smuzhiyun /**
1933*4882a593Smuzhiyun  * qib_6120_clear_tids - clear all TID entries for a context, expected and eager
1934*4882a593Smuzhiyun  * @dd: the qlogic_ib device
1935*4882a593Smuzhiyun  * @ctxt: the context
1936*4882a593Smuzhiyun  *
1937*4882a593Smuzhiyun  * clear all TID entries for a context, expected and eager.
1938*4882a593Smuzhiyun  * Used from qib_close().  On this chip, TIDs are only 32 bits,
1939*4882a593Smuzhiyun  * not 64, but they are still on 64 bit boundaries, so tidbase
1940*4882a593Smuzhiyun  * is declared as u64 * for the pointer math, even though we write 32 bits
1941*4882a593Smuzhiyun  */
qib_6120_clear_tids(struct qib_devdata * dd,struct qib_ctxtdata * rcd)1942*4882a593Smuzhiyun static void qib_6120_clear_tids(struct qib_devdata *dd,
1943*4882a593Smuzhiyun 				struct qib_ctxtdata *rcd)
1944*4882a593Smuzhiyun {
1945*4882a593Smuzhiyun 	u64 __iomem *tidbase;
1946*4882a593Smuzhiyun 	unsigned long tidinv;
1947*4882a593Smuzhiyun 	u32 ctxt;
1948*4882a593Smuzhiyun 	int i;
1949*4882a593Smuzhiyun 
1950*4882a593Smuzhiyun 	if (!dd->kregbase || !rcd)
1951*4882a593Smuzhiyun 		return;
1952*4882a593Smuzhiyun 
1953*4882a593Smuzhiyun 	ctxt = rcd->ctxt;
1954*4882a593Smuzhiyun 
1955*4882a593Smuzhiyun 	tidinv = dd->tidinvalid;
1956*4882a593Smuzhiyun 	tidbase = (u64 __iomem *)
1957*4882a593Smuzhiyun 		((char __iomem *)(dd->kregbase) +
1958*4882a593Smuzhiyun 		 dd->rcvtidbase +
1959*4882a593Smuzhiyun 		 ctxt * dd->rcvtidcnt * sizeof(*tidbase));
1960*4882a593Smuzhiyun 
1961*4882a593Smuzhiyun 	for (i = 0; i < dd->rcvtidcnt; i++)
1962*4882a593Smuzhiyun 		/* use func pointer because could be one of two funcs */
1963*4882a593Smuzhiyun 		dd->f_put_tid(dd, &tidbase[i], RCVHQ_RCV_TYPE_EXPECTED,
1964*4882a593Smuzhiyun 				  tidinv);
1965*4882a593Smuzhiyun 
1966*4882a593Smuzhiyun 	tidbase = (u64 __iomem *)
1967*4882a593Smuzhiyun 		((char __iomem *)(dd->kregbase) +
1968*4882a593Smuzhiyun 		 dd->rcvegrbase +
1969*4882a593Smuzhiyun 		 rcd->rcvegr_tid_base * sizeof(*tidbase));
1970*4882a593Smuzhiyun 
1971*4882a593Smuzhiyun 	for (i = 0; i < rcd->rcvegrcnt; i++)
1972*4882a593Smuzhiyun 		/* use func pointer because could be one of two funcs */
1973*4882a593Smuzhiyun 		dd->f_put_tid(dd, &tidbase[i], RCVHQ_RCV_TYPE_EAGER,
1974*4882a593Smuzhiyun 				  tidinv);
1975*4882a593Smuzhiyun }
1976*4882a593Smuzhiyun 
1977*4882a593Smuzhiyun /**
1978*4882a593Smuzhiyun  * qib_6120_tidtemplate - setup constants for TID updates
1979*4882a593Smuzhiyun  * @dd: the qlogic_ib device
1980*4882a593Smuzhiyun  *
1981*4882a593Smuzhiyun  * We setup stuff that we use a lot, to avoid calculating each time
1982*4882a593Smuzhiyun  */
qib_6120_tidtemplate(struct qib_devdata * dd)1983*4882a593Smuzhiyun static void qib_6120_tidtemplate(struct qib_devdata *dd)
1984*4882a593Smuzhiyun {
1985*4882a593Smuzhiyun 	u32 egrsize = dd->rcvegrbufsize;
1986*4882a593Smuzhiyun 
1987*4882a593Smuzhiyun 	/*
1988*4882a593Smuzhiyun 	 * For now, we always allocate 4KB buffers (at init) so we can
1989*4882a593Smuzhiyun 	 * receive max size packets.  We may want a module parameter to
1990*4882a593Smuzhiyun 	 * specify 2KB or 4KB and/or make be per ctxt instead of per device
1991*4882a593Smuzhiyun 	 * for those who want to reduce memory footprint.  Note that the
1992*4882a593Smuzhiyun 	 * rcvhdrentsize size must be large enough to hold the largest
1993*4882a593Smuzhiyun 	 * IB header (currently 96 bytes) that we expect to handle (plus of
1994*4882a593Smuzhiyun 	 * course the 2 dwords of RHF).
1995*4882a593Smuzhiyun 	 */
1996*4882a593Smuzhiyun 	if (egrsize == 2048)
1997*4882a593Smuzhiyun 		dd->tidtemplate = 1U << 29;
1998*4882a593Smuzhiyun 	else if (egrsize == 4096)
1999*4882a593Smuzhiyun 		dd->tidtemplate = 2U << 29;
2000*4882a593Smuzhiyun 	dd->tidinvalid = 0;
2001*4882a593Smuzhiyun }
2002*4882a593Smuzhiyun 
qib_unordered_wc(void)2003*4882a593Smuzhiyun int __attribute__((weak)) qib_unordered_wc(void)
2004*4882a593Smuzhiyun {
2005*4882a593Smuzhiyun 	return 0;
2006*4882a593Smuzhiyun }
2007*4882a593Smuzhiyun 
2008*4882a593Smuzhiyun /**
2009*4882a593Smuzhiyun  * qib_6120_get_base_info - set chip-specific flags for user code
2010*4882a593Smuzhiyun  * @rcd: the qlogic_ib ctxt
2011*4882a593Smuzhiyun  * @kbase: qib_base_info pointer
2012*4882a593Smuzhiyun  *
2013*4882a593Smuzhiyun  * We set the PCIE flag because the lower bandwidth on PCIe vs
2014*4882a593Smuzhiyun  * HyperTransport can affect some user packet algorithms.
2015*4882a593Smuzhiyun  */
qib_6120_get_base_info(struct qib_ctxtdata * rcd,struct qib_base_info * kinfo)2016*4882a593Smuzhiyun static int qib_6120_get_base_info(struct qib_ctxtdata *rcd,
2017*4882a593Smuzhiyun 				  struct qib_base_info *kinfo)
2018*4882a593Smuzhiyun {
2019*4882a593Smuzhiyun 	if (qib_unordered_wc())
2020*4882a593Smuzhiyun 		kinfo->spi_runtime_flags |= QIB_RUNTIME_FORCE_WC_ORDER;
2021*4882a593Smuzhiyun 
2022*4882a593Smuzhiyun 	kinfo->spi_runtime_flags |= QIB_RUNTIME_PCIE |
2023*4882a593Smuzhiyun 		QIB_RUNTIME_FORCE_PIOAVAIL | QIB_RUNTIME_PIO_REGSWAPPED;
2024*4882a593Smuzhiyun 	return 0;
2025*4882a593Smuzhiyun }
2026*4882a593Smuzhiyun 
2027*4882a593Smuzhiyun 
2028*4882a593Smuzhiyun static struct qib_message_header *
qib_6120_get_msgheader(struct qib_devdata * dd,__le32 * rhf_addr)2029*4882a593Smuzhiyun qib_6120_get_msgheader(struct qib_devdata *dd, __le32 *rhf_addr)
2030*4882a593Smuzhiyun {
2031*4882a593Smuzhiyun 	return (struct qib_message_header *)
2032*4882a593Smuzhiyun 		&rhf_addr[sizeof(u64) / sizeof(u32)];
2033*4882a593Smuzhiyun }
2034*4882a593Smuzhiyun 
qib_6120_config_ctxts(struct qib_devdata * dd)2035*4882a593Smuzhiyun static void qib_6120_config_ctxts(struct qib_devdata *dd)
2036*4882a593Smuzhiyun {
2037*4882a593Smuzhiyun 	dd->ctxtcnt = qib_read_kreg32(dd, kr_portcnt);
2038*4882a593Smuzhiyun 	if (qib_n_krcv_queues > 1) {
2039*4882a593Smuzhiyun 		dd->first_user_ctxt = qib_n_krcv_queues * dd->num_pports;
2040*4882a593Smuzhiyun 		if (dd->first_user_ctxt > dd->ctxtcnt)
2041*4882a593Smuzhiyun 			dd->first_user_ctxt = dd->ctxtcnt;
2042*4882a593Smuzhiyun 		dd->qpn_mask = dd->first_user_ctxt <= 2 ? 2 : 6;
2043*4882a593Smuzhiyun 	} else
2044*4882a593Smuzhiyun 		dd->first_user_ctxt = dd->num_pports;
2045*4882a593Smuzhiyun 	dd->n_krcv_queues = dd->first_user_ctxt;
2046*4882a593Smuzhiyun }
2047*4882a593Smuzhiyun 
qib_update_6120_usrhead(struct qib_ctxtdata * rcd,u64 hd,u32 updegr,u32 egrhd,u32 npkts)2048*4882a593Smuzhiyun static void qib_update_6120_usrhead(struct qib_ctxtdata *rcd, u64 hd,
2049*4882a593Smuzhiyun 				    u32 updegr, u32 egrhd, u32 npkts)
2050*4882a593Smuzhiyun {
2051*4882a593Smuzhiyun 	if (updegr)
2052*4882a593Smuzhiyun 		qib_write_ureg(rcd->dd, ur_rcvegrindexhead, egrhd, rcd->ctxt);
2053*4882a593Smuzhiyun 	qib_write_ureg(rcd->dd, ur_rcvhdrhead, hd, rcd->ctxt);
2054*4882a593Smuzhiyun }
2055*4882a593Smuzhiyun 
qib_6120_hdrqempty(struct qib_ctxtdata * rcd)2056*4882a593Smuzhiyun static u32 qib_6120_hdrqempty(struct qib_ctxtdata *rcd)
2057*4882a593Smuzhiyun {
2058*4882a593Smuzhiyun 	u32 head, tail;
2059*4882a593Smuzhiyun 
2060*4882a593Smuzhiyun 	head = qib_read_ureg32(rcd->dd, ur_rcvhdrhead, rcd->ctxt);
2061*4882a593Smuzhiyun 	if (rcd->rcvhdrtail_kvaddr)
2062*4882a593Smuzhiyun 		tail = qib_get_rcvhdrtail(rcd);
2063*4882a593Smuzhiyun 	else
2064*4882a593Smuzhiyun 		tail = qib_read_ureg32(rcd->dd, ur_rcvhdrtail, rcd->ctxt);
2065*4882a593Smuzhiyun 	return head == tail;
2066*4882a593Smuzhiyun }
2067*4882a593Smuzhiyun 
2068*4882a593Smuzhiyun /*
2069*4882a593Smuzhiyun  * Used when we close any ctxt, for DMA already in flight
2070*4882a593Smuzhiyun  * at close.  Can't be done until we know hdrq size, so not
2071*4882a593Smuzhiyun  * early in chip init.
2072*4882a593Smuzhiyun  */
alloc_dummy_hdrq(struct qib_devdata * dd)2073*4882a593Smuzhiyun static void alloc_dummy_hdrq(struct qib_devdata *dd)
2074*4882a593Smuzhiyun {
2075*4882a593Smuzhiyun 	dd->cspec->dummy_hdrq = dma_alloc_coherent(&dd->pcidev->dev,
2076*4882a593Smuzhiyun 					dd->rcd[0]->rcvhdrq_size,
2077*4882a593Smuzhiyun 					&dd->cspec->dummy_hdrq_phys,
2078*4882a593Smuzhiyun 					GFP_ATOMIC | __GFP_COMP);
2079*4882a593Smuzhiyun 	if (!dd->cspec->dummy_hdrq) {
2080*4882a593Smuzhiyun 		qib_devinfo(dd->pcidev, "Couldn't allocate dummy hdrq\n");
2081*4882a593Smuzhiyun 		/* fallback to just 0'ing */
2082*4882a593Smuzhiyun 		dd->cspec->dummy_hdrq_phys = 0UL;
2083*4882a593Smuzhiyun 	}
2084*4882a593Smuzhiyun }
2085*4882a593Smuzhiyun 
2086*4882a593Smuzhiyun /*
2087*4882a593Smuzhiyun  * Modify the RCVCTRL register in chip-specific way. This
2088*4882a593Smuzhiyun  * is a function because bit positions and (future) register
2089*4882a593Smuzhiyun  * location is chip-specific, but the needed operations are
2090*4882a593Smuzhiyun  * generic. <op> is a bit-mask because we often want to
2091*4882a593Smuzhiyun  * do multiple modifications.
2092*4882a593Smuzhiyun  */
rcvctrl_6120_mod(struct qib_pportdata * ppd,unsigned int op,int ctxt)2093*4882a593Smuzhiyun static void rcvctrl_6120_mod(struct qib_pportdata *ppd, unsigned int op,
2094*4882a593Smuzhiyun 			     int ctxt)
2095*4882a593Smuzhiyun {
2096*4882a593Smuzhiyun 	struct qib_devdata *dd = ppd->dd;
2097*4882a593Smuzhiyun 	u64 mask, val;
2098*4882a593Smuzhiyun 	unsigned long flags;
2099*4882a593Smuzhiyun 
2100*4882a593Smuzhiyun 	spin_lock_irqsave(&dd->cspec->rcvmod_lock, flags);
2101*4882a593Smuzhiyun 
2102*4882a593Smuzhiyun 	if (op & QIB_RCVCTRL_TAILUPD_ENB)
2103*4882a593Smuzhiyun 		dd->rcvctrl |= (1ULL << QLOGIC_IB_R_TAILUPD_SHIFT);
2104*4882a593Smuzhiyun 	if (op & QIB_RCVCTRL_TAILUPD_DIS)
2105*4882a593Smuzhiyun 		dd->rcvctrl &= ~(1ULL << QLOGIC_IB_R_TAILUPD_SHIFT);
2106*4882a593Smuzhiyun 	if (op & QIB_RCVCTRL_PKEY_ENB)
2107*4882a593Smuzhiyun 		dd->rcvctrl &= ~(1ULL << IBA6120_R_PKEY_DIS_SHIFT);
2108*4882a593Smuzhiyun 	if (op & QIB_RCVCTRL_PKEY_DIS)
2109*4882a593Smuzhiyun 		dd->rcvctrl |= (1ULL << IBA6120_R_PKEY_DIS_SHIFT);
2110*4882a593Smuzhiyun 	if (ctxt < 0)
2111*4882a593Smuzhiyun 		mask = (1ULL << dd->ctxtcnt) - 1;
2112*4882a593Smuzhiyun 	else
2113*4882a593Smuzhiyun 		mask = (1ULL << ctxt);
2114*4882a593Smuzhiyun 	if (op & QIB_RCVCTRL_CTXT_ENB) {
2115*4882a593Smuzhiyun 		/* always done for specific ctxt */
2116*4882a593Smuzhiyun 		dd->rcvctrl |= (mask << SYM_LSB(RcvCtrl, PortEnable));
2117*4882a593Smuzhiyun 		if (!(dd->flags & QIB_NODMA_RTAIL))
2118*4882a593Smuzhiyun 			dd->rcvctrl |= 1ULL << QLOGIC_IB_R_TAILUPD_SHIFT;
2119*4882a593Smuzhiyun 		/* Write these registers before the context is enabled. */
2120*4882a593Smuzhiyun 		qib_write_kreg_ctxt(dd, kr_rcvhdrtailaddr, ctxt,
2121*4882a593Smuzhiyun 			dd->rcd[ctxt]->rcvhdrqtailaddr_phys);
2122*4882a593Smuzhiyun 		qib_write_kreg_ctxt(dd, kr_rcvhdraddr, ctxt,
2123*4882a593Smuzhiyun 			dd->rcd[ctxt]->rcvhdrq_phys);
2124*4882a593Smuzhiyun 
2125*4882a593Smuzhiyun 		if (ctxt == 0 && !dd->cspec->dummy_hdrq)
2126*4882a593Smuzhiyun 			alloc_dummy_hdrq(dd);
2127*4882a593Smuzhiyun 	}
2128*4882a593Smuzhiyun 	if (op & QIB_RCVCTRL_CTXT_DIS)
2129*4882a593Smuzhiyun 		dd->rcvctrl &= ~(mask << SYM_LSB(RcvCtrl, PortEnable));
2130*4882a593Smuzhiyun 	if (op & QIB_RCVCTRL_INTRAVAIL_ENB)
2131*4882a593Smuzhiyun 		dd->rcvctrl |= (mask << QLOGIC_IB_R_INTRAVAIL_SHIFT);
2132*4882a593Smuzhiyun 	if (op & QIB_RCVCTRL_INTRAVAIL_DIS)
2133*4882a593Smuzhiyun 		dd->rcvctrl &= ~(mask << QLOGIC_IB_R_INTRAVAIL_SHIFT);
2134*4882a593Smuzhiyun 	qib_write_kreg(dd, kr_rcvctrl, dd->rcvctrl);
2135*4882a593Smuzhiyun 	if ((op & QIB_RCVCTRL_INTRAVAIL_ENB) && dd->rhdrhead_intr_off) {
2136*4882a593Smuzhiyun 		/* arm rcv interrupt */
2137*4882a593Smuzhiyun 		val = qib_read_ureg32(dd, ur_rcvhdrhead, ctxt) |
2138*4882a593Smuzhiyun 			dd->rhdrhead_intr_off;
2139*4882a593Smuzhiyun 		qib_write_ureg(dd, ur_rcvhdrhead, val, ctxt);
2140*4882a593Smuzhiyun 	}
2141*4882a593Smuzhiyun 	if (op & QIB_RCVCTRL_CTXT_ENB) {
2142*4882a593Smuzhiyun 		/*
2143*4882a593Smuzhiyun 		 * Init the context registers also; if we were
2144*4882a593Smuzhiyun 		 * disabled, tail and head should both be zero
2145*4882a593Smuzhiyun 		 * already from the enable, but since we don't
2146*4882a593Smuzhiyun 		 * know, we have to do it explicitly.
2147*4882a593Smuzhiyun 		 */
2148*4882a593Smuzhiyun 		val = qib_read_ureg32(dd, ur_rcvegrindextail, ctxt);
2149*4882a593Smuzhiyun 		qib_write_ureg(dd, ur_rcvegrindexhead, val, ctxt);
2150*4882a593Smuzhiyun 
2151*4882a593Smuzhiyun 		val = qib_read_ureg32(dd, ur_rcvhdrtail, ctxt);
2152*4882a593Smuzhiyun 		dd->rcd[ctxt]->head = val;
2153*4882a593Smuzhiyun 		/* If kctxt, interrupt on next receive. */
2154*4882a593Smuzhiyun 		if (ctxt < dd->first_user_ctxt)
2155*4882a593Smuzhiyun 			val |= dd->rhdrhead_intr_off;
2156*4882a593Smuzhiyun 		qib_write_ureg(dd, ur_rcvhdrhead, val, ctxt);
2157*4882a593Smuzhiyun 	}
2158*4882a593Smuzhiyun 	if (op & QIB_RCVCTRL_CTXT_DIS) {
2159*4882a593Smuzhiyun 		/*
2160*4882a593Smuzhiyun 		 * Be paranoid, and never write 0's to these, just use an
2161*4882a593Smuzhiyun 		 * unused page.  Of course,
2162*4882a593Smuzhiyun 		 * rcvhdraddr points to a large chunk of memory, so this
2163*4882a593Smuzhiyun 		 * could still trash things, but at least it won't trash
2164*4882a593Smuzhiyun 		 * page 0, and by disabling the ctxt, it should stop "soon",
2165*4882a593Smuzhiyun 		 * even if a packet or two is in already in flight after we
2166*4882a593Smuzhiyun 		 * disabled the ctxt.  Only 6120 has this issue.
2167*4882a593Smuzhiyun 		 */
2168*4882a593Smuzhiyun 		if (ctxt >= 0) {
2169*4882a593Smuzhiyun 			qib_write_kreg_ctxt(dd, kr_rcvhdrtailaddr, ctxt,
2170*4882a593Smuzhiyun 					    dd->cspec->dummy_hdrq_phys);
2171*4882a593Smuzhiyun 			qib_write_kreg_ctxt(dd, kr_rcvhdraddr, ctxt,
2172*4882a593Smuzhiyun 					    dd->cspec->dummy_hdrq_phys);
2173*4882a593Smuzhiyun 		} else {
2174*4882a593Smuzhiyun 			unsigned i;
2175*4882a593Smuzhiyun 
2176*4882a593Smuzhiyun 			for (i = 0; i < dd->cfgctxts; i++) {
2177*4882a593Smuzhiyun 				qib_write_kreg_ctxt(dd, kr_rcvhdrtailaddr,
2178*4882a593Smuzhiyun 					    i, dd->cspec->dummy_hdrq_phys);
2179*4882a593Smuzhiyun 				qib_write_kreg_ctxt(dd, kr_rcvhdraddr,
2180*4882a593Smuzhiyun 					    i, dd->cspec->dummy_hdrq_phys);
2181*4882a593Smuzhiyun 			}
2182*4882a593Smuzhiyun 		}
2183*4882a593Smuzhiyun 	}
2184*4882a593Smuzhiyun 	spin_unlock_irqrestore(&dd->cspec->rcvmod_lock, flags);
2185*4882a593Smuzhiyun }
2186*4882a593Smuzhiyun 
2187*4882a593Smuzhiyun /*
2188*4882a593Smuzhiyun  * Modify the SENDCTRL register in chip-specific way. This
2189*4882a593Smuzhiyun  * is a function there may be multiple such registers with
2190*4882a593Smuzhiyun  * slightly different layouts. Only operations actually used
2191*4882a593Smuzhiyun  * are implemented yet.
2192*4882a593Smuzhiyun  * Chip requires no back-back sendctrl writes, so write
2193*4882a593Smuzhiyun  * scratch register after writing sendctrl
2194*4882a593Smuzhiyun  */
sendctrl_6120_mod(struct qib_pportdata * ppd,u32 op)2195*4882a593Smuzhiyun static void sendctrl_6120_mod(struct qib_pportdata *ppd, u32 op)
2196*4882a593Smuzhiyun {
2197*4882a593Smuzhiyun 	struct qib_devdata *dd = ppd->dd;
2198*4882a593Smuzhiyun 	u64 tmp_dd_sendctrl;
2199*4882a593Smuzhiyun 	unsigned long flags;
2200*4882a593Smuzhiyun 
2201*4882a593Smuzhiyun 	spin_lock_irqsave(&dd->sendctrl_lock, flags);
2202*4882a593Smuzhiyun 
2203*4882a593Smuzhiyun 	/* First the ones that are "sticky", saved in shadow */
2204*4882a593Smuzhiyun 	if (op & QIB_SENDCTRL_CLEAR)
2205*4882a593Smuzhiyun 		dd->sendctrl = 0;
2206*4882a593Smuzhiyun 	if (op & QIB_SENDCTRL_SEND_DIS)
2207*4882a593Smuzhiyun 		dd->sendctrl &= ~SYM_MASK(SendCtrl, PIOEnable);
2208*4882a593Smuzhiyun 	else if (op & QIB_SENDCTRL_SEND_ENB)
2209*4882a593Smuzhiyun 		dd->sendctrl |= SYM_MASK(SendCtrl, PIOEnable);
2210*4882a593Smuzhiyun 	if (op & QIB_SENDCTRL_AVAIL_DIS)
2211*4882a593Smuzhiyun 		dd->sendctrl &= ~SYM_MASK(SendCtrl, PIOBufAvailUpd);
2212*4882a593Smuzhiyun 	else if (op & QIB_SENDCTRL_AVAIL_ENB)
2213*4882a593Smuzhiyun 		dd->sendctrl |= SYM_MASK(SendCtrl, PIOBufAvailUpd);
2214*4882a593Smuzhiyun 
2215*4882a593Smuzhiyun 	if (op & QIB_SENDCTRL_DISARM_ALL) {
2216*4882a593Smuzhiyun 		u32 i, last;
2217*4882a593Smuzhiyun 
2218*4882a593Smuzhiyun 		tmp_dd_sendctrl = dd->sendctrl;
2219*4882a593Smuzhiyun 		/*
2220*4882a593Smuzhiyun 		 * disarm any that are not yet launched, disabling sends
2221*4882a593Smuzhiyun 		 * and updates until done.
2222*4882a593Smuzhiyun 		 */
2223*4882a593Smuzhiyun 		last = dd->piobcnt2k + dd->piobcnt4k;
2224*4882a593Smuzhiyun 		tmp_dd_sendctrl &=
2225*4882a593Smuzhiyun 			~(SYM_MASK(SendCtrl, PIOEnable) |
2226*4882a593Smuzhiyun 			  SYM_MASK(SendCtrl, PIOBufAvailUpd));
2227*4882a593Smuzhiyun 		for (i = 0; i < last; i++) {
2228*4882a593Smuzhiyun 			qib_write_kreg(dd, kr_sendctrl, tmp_dd_sendctrl |
2229*4882a593Smuzhiyun 				       SYM_MASK(SendCtrl, Disarm) | i);
2230*4882a593Smuzhiyun 			qib_write_kreg(dd, kr_scratch, 0);
2231*4882a593Smuzhiyun 		}
2232*4882a593Smuzhiyun 	}
2233*4882a593Smuzhiyun 
2234*4882a593Smuzhiyun 	tmp_dd_sendctrl = dd->sendctrl;
2235*4882a593Smuzhiyun 
2236*4882a593Smuzhiyun 	if (op & QIB_SENDCTRL_FLUSH)
2237*4882a593Smuzhiyun 		tmp_dd_sendctrl |= SYM_MASK(SendCtrl, Abort);
2238*4882a593Smuzhiyun 	if (op & QIB_SENDCTRL_DISARM)
2239*4882a593Smuzhiyun 		tmp_dd_sendctrl |= SYM_MASK(SendCtrl, Disarm) |
2240*4882a593Smuzhiyun 			((op & QIB_6120_SendCtrl_DisarmPIOBuf_RMASK) <<
2241*4882a593Smuzhiyun 			 SYM_LSB(SendCtrl, DisarmPIOBuf));
2242*4882a593Smuzhiyun 	if (op & QIB_SENDCTRL_AVAIL_BLIP)
2243*4882a593Smuzhiyun 		tmp_dd_sendctrl &= ~SYM_MASK(SendCtrl, PIOBufAvailUpd);
2244*4882a593Smuzhiyun 
2245*4882a593Smuzhiyun 	qib_write_kreg(dd, kr_sendctrl, tmp_dd_sendctrl);
2246*4882a593Smuzhiyun 	qib_write_kreg(dd, kr_scratch, 0);
2247*4882a593Smuzhiyun 
2248*4882a593Smuzhiyun 	if (op & QIB_SENDCTRL_AVAIL_BLIP) {
2249*4882a593Smuzhiyun 		qib_write_kreg(dd, kr_sendctrl, dd->sendctrl);
2250*4882a593Smuzhiyun 		qib_write_kreg(dd, kr_scratch, 0);
2251*4882a593Smuzhiyun 	}
2252*4882a593Smuzhiyun 
2253*4882a593Smuzhiyun 	spin_unlock_irqrestore(&dd->sendctrl_lock, flags);
2254*4882a593Smuzhiyun 
2255*4882a593Smuzhiyun 	if (op & QIB_SENDCTRL_FLUSH) {
2256*4882a593Smuzhiyun 		u32 v;
2257*4882a593Smuzhiyun 		/*
2258*4882a593Smuzhiyun 		 * ensure writes have hit chip, then do a few
2259*4882a593Smuzhiyun 		 * more reads, to allow DMA of pioavail registers
2260*4882a593Smuzhiyun 		 * to occur, so in-memory copy is in sync with
2261*4882a593Smuzhiyun 		 * the chip.  Not always safe to sleep.
2262*4882a593Smuzhiyun 		 */
2263*4882a593Smuzhiyun 		v = qib_read_kreg32(dd, kr_scratch);
2264*4882a593Smuzhiyun 		qib_write_kreg(dd, kr_scratch, v);
2265*4882a593Smuzhiyun 		v = qib_read_kreg32(dd, kr_scratch);
2266*4882a593Smuzhiyun 		qib_write_kreg(dd, kr_scratch, v);
2267*4882a593Smuzhiyun 		qib_read_kreg32(dd, kr_scratch);
2268*4882a593Smuzhiyun 	}
2269*4882a593Smuzhiyun }
2270*4882a593Smuzhiyun 
2271*4882a593Smuzhiyun /**
2272*4882a593Smuzhiyun  * qib_portcntr_6120 - read a per-port counter
2273*4882a593Smuzhiyun  * @dd: the qlogic_ib device
2274*4882a593Smuzhiyun  * @creg: the counter to snapshot
2275*4882a593Smuzhiyun  */
qib_portcntr_6120(struct qib_pportdata * ppd,u32 reg)2276*4882a593Smuzhiyun static u64 qib_portcntr_6120(struct qib_pportdata *ppd, u32 reg)
2277*4882a593Smuzhiyun {
2278*4882a593Smuzhiyun 	u64 ret = 0ULL;
2279*4882a593Smuzhiyun 	struct qib_devdata *dd = ppd->dd;
2280*4882a593Smuzhiyun 	u16 creg;
2281*4882a593Smuzhiyun 	/* 0xffff for unimplemented or synthesized counters */
2282*4882a593Smuzhiyun 	static const u16 xlator[] = {
2283*4882a593Smuzhiyun 		[QIBPORTCNTR_PKTSEND] = cr_pktsend,
2284*4882a593Smuzhiyun 		[QIBPORTCNTR_WORDSEND] = cr_wordsend,
2285*4882a593Smuzhiyun 		[QIBPORTCNTR_PSXMITDATA] = 0xffff,
2286*4882a593Smuzhiyun 		[QIBPORTCNTR_PSXMITPKTS] = 0xffff,
2287*4882a593Smuzhiyun 		[QIBPORTCNTR_PSXMITWAIT] = 0xffff,
2288*4882a593Smuzhiyun 		[QIBPORTCNTR_SENDSTALL] = cr_sendstall,
2289*4882a593Smuzhiyun 		[QIBPORTCNTR_PKTRCV] = cr_pktrcv,
2290*4882a593Smuzhiyun 		[QIBPORTCNTR_PSRCVDATA] = 0xffff,
2291*4882a593Smuzhiyun 		[QIBPORTCNTR_PSRCVPKTS] = 0xffff,
2292*4882a593Smuzhiyun 		[QIBPORTCNTR_RCVEBP] = cr_rcvebp,
2293*4882a593Smuzhiyun 		[QIBPORTCNTR_RCVOVFL] = cr_rcvovfl,
2294*4882a593Smuzhiyun 		[QIBPORTCNTR_WORDRCV] = cr_wordrcv,
2295*4882a593Smuzhiyun 		[QIBPORTCNTR_RXDROPPKT] = cr_rxdroppkt,
2296*4882a593Smuzhiyun 		[QIBPORTCNTR_RXLOCALPHYERR] = 0xffff,
2297*4882a593Smuzhiyun 		[QIBPORTCNTR_RXVLERR] = 0xffff,
2298*4882a593Smuzhiyun 		[QIBPORTCNTR_ERRICRC] = cr_erricrc,
2299*4882a593Smuzhiyun 		[QIBPORTCNTR_ERRVCRC] = cr_errvcrc,
2300*4882a593Smuzhiyun 		[QIBPORTCNTR_ERRLPCRC] = cr_errlpcrc,
2301*4882a593Smuzhiyun 		[QIBPORTCNTR_BADFORMAT] = cr_badformat,
2302*4882a593Smuzhiyun 		[QIBPORTCNTR_ERR_RLEN] = cr_err_rlen,
2303*4882a593Smuzhiyun 		[QIBPORTCNTR_IBSYMBOLERR] = cr_ibsymbolerr,
2304*4882a593Smuzhiyun 		[QIBPORTCNTR_INVALIDRLEN] = cr_invalidrlen,
2305*4882a593Smuzhiyun 		[QIBPORTCNTR_UNSUPVL] = cr_txunsupvl,
2306*4882a593Smuzhiyun 		[QIBPORTCNTR_EXCESSBUFOVFL] = 0xffff,
2307*4882a593Smuzhiyun 		[QIBPORTCNTR_ERRLINK] = cr_errlink,
2308*4882a593Smuzhiyun 		[QIBPORTCNTR_IBLINKDOWN] = cr_iblinkdown,
2309*4882a593Smuzhiyun 		[QIBPORTCNTR_IBLINKERRRECOV] = cr_iblinkerrrecov,
2310*4882a593Smuzhiyun 		[QIBPORTCNTR_LLI] = 0xffff,
2311*4882a593Smuzhiyun 		[QIBPORTCNTR_PSINTERVAL] = 0xffff,
2312*4882a593Smuzhiyun 		[QIBPORTCNTR_PSSTART] = 0xffff,
2313*4882a593Smuzhiyun 		[QIBPORTCNTR_PSSTAT] = 0xffff,
2314*4882a593Smuzhiyun 		[QIBPORTCNTR_VL15PKTDROP] = 0xffff,
2315*4882a593Smuzhiyun 		[QIBPORTCNTR_ERRPKEY] = cr_errpkey,
2316*4882a593Smuzhiyun 		[QIBPORTCNTR_KHDROVFL] = 0xffff,
2317*4882a593Smuzhiyun 	};
2318*4882a593Smuzhiyun 
2319*4882a593Smuzhiyun 	if (reg >= ARRAY_SIZE(xlator)) {
2320*4882a593Smuzhiyun 		qib_devinfo(ppd->dd->pcidev,
2321*4882a593Smuzhiyun 			 "Unimplemented portcounter %u\n", reg);
2322*4882a593Smuzhiyun 		goto done;
2323*4882a593Smuzhiyun 	}
2324*4882a593Smuzhiyun 	creg = xlator[reg];
2325*4882a593Smuzhiyun 
2326*4882a593Smuzhiyun 	/* handle counters requests not implemented as chip counters */
2327*4882a593Smuzhiyun 	if (reg == QIBPORTCNTR_LLI)
2328*4882a593Smuzhiyun 		ret = dd->cspec->lli_errs;
2329*4882a593Smuzhiyun 	else if (reg == QIBPORTCNTR_EXCESSBUFOVFL)
2330*4882a593Smuzhiyun 		ret = dd->cspec->overrun_thresh_errs;
2331*4882a593Smuzhiyun 	else if (reg == QIBPORTCNTR_KHDROVFL) {
2332*4882a593Smuzhiyun 		int i;
2333*4882a593Smuzhiyun 
2334*4882a593Smuzhiyun 		/* sum over all kernel contexts */
2335*4882a593Smuzhiyun 		for (i = 0; i < dd->first_user_ctxt; i++)
2336*4882a593Smuzhiyun 			ret += read_6120_creg32(dd, cr_portovfl + i);
2337*4882a593Smuzhiyun 	} else if (reg == QIBPORTCNTR_PSSTAT)
2338*4882a593Smuzhiyun 		ret = dd->cspec->pma_sample_status;
2339*4882a593Smuzhiyun 	if (creg == 0xffff)
2340*4882a593Smuzhiyun 		goto done;
2341*4882a593Smuzhiyun 
2342*4882a593Smuzhiyun 	/*
2343*4882a593Smuzhiyun 	 * only fast incrementing counters are 64bit; use 32 bit reads to
2344*4882a593Smuzhiyun 	 * avoid two independent reads when on opteron
2345*4882a593Smuzhiyun 	 */
2346*4882a593Smuzhiyun 	if (creg == cr_wordsend || creg == cr_wordrcv ||
2347*4882a593Smuzhiyun 	    creg == cr_pktsend || creg == cr_pktrcv)
2348*4882a593Smuzhiyun 		ret = read_6120_creg(dd, creg);
2349*4882a593Smuzhiyun 	else
2350*4882a593Smuzhiyun 		ret = read_6120_creg32(dd, creg);
2351*4882a593Smuzhiyun 	if (creg == cr_ibsymbolerr) {
2352*4882a593Smuzhiyun 		if (dd->cspec->ibdeltainprog)
2353*4882a593Smuzhiyun 			ret -= ret - dd->cspec->ibsymsnap;
2354*4882a593Smuzhiyun 		ret -= dd->cspec->ibsymdelta;
2355*4882a593Smuzhiyun 	} else if (creg == cr_iblinkerrrecov) {
2356*4882a593Smuzhiyun 		if (dd->cspec->ibdeltainprog)
2357*4882a593Smuzhiyun 			ret -= ret - dd->cspec->iblnkerrsnap;
2358*4882a593Smuzhiyun 		ret -= dd->cspec->iblnkerrdelta;
2359*4882a593Smuzhiyun 	}
2360*4882a593Smuzhiyun 	if (reg == QIBPORTCNTR_RXDROPPKT) /* add special cased count */
2361*4882a593Smuzhiyun 		ret += dd->cspec->rxfc_unsupvl_errs;
2362*4882a593Smuzhiyun 
2363*4882a593Smuzhiyun done:
2364*4882a593Smuzhiyun 	return ret;
2365*4882a593Smuzhiyun }
2366*4882a593Smuzhiyun 
2367*4882a593Smuzhiyun /*
2368*4882a593Smuzhiyun  * Device counter names (not port-specific), one line per stat,
2369*4882a593Smuzhiyun  * single string.  Used by utilities like ipathstats to print the stats
2370*4882a593Smuzhiyun  * in a way which works for different versions of drivers, without changing
2371*4882a593Smuzhiyun  * the utility.  Names need to be 12 chars or less (w/o newline), for proper
2372*4882a593Smuzhiyun  * display by utility.
2373*4882a593Smuzhiyun  * Non-error counters are first.
2374*4882a593Smuzhiyun  * Start of "error" conters is indicated by a leading "E " on the first
2375*4882a593Smuzhiyun  * "error" counter, and doesn't count in label length.
2376*4882a593Smuzhiyun  * The EgrOvfl list needs to be last so we truncate them at the configured
2377*4882a593Smuzhiyun  * context count for the device.
2378*4882a593Smuzhiyun  * cntr6120indices contains the corresponding register indices.
2379*4882a593Smuzhiyun  */
2380*4882a593Smuzhiyun static const char cntr6120names[] =
2381*4882a593Smuzhiyun 	"Interrupts\n"
2382*4882a593Smuzhiyun 	"HostBusStall\n"
2383*4882a593Smuzhiyun 	"E RxTIDFull\n"
2384*4882a593Smuzhiyun 	"RxTIDInvalid\n"
2385*4882a593Smuzhiyun 	"Ctxt0EgrOvfl\n"
2386*4882a593Smuzhiyun 	"Ctxt1EgrOvfl\n"
2387*4882a593Smuzhiyun 	"Ctxt2EgrOvfl\n"
2388*4882a593Smuzhiyun 	"Ctxt3EgrOvfl\n"
2389*4882a593Smuzhiyun 	"Ctxt4EgrOvfl\n";
2390*4882a593Smuzhiyun 
2391*4882a593Smuzhiyun static const size_t cntr6120indices[] = {
2392*4882a593Smuzhiyun 	cr_lbint,
2393*4882a593Smuzhiyun 	cr_lbflowstall,
2394*4882a593Smuzhiyun 	cr_errtidfull,
2395*4882a593Smuzhiyun 	cr_errtidvalid,
2396*4882a593Smuzhiyun 	cr_portovfl + 0,
2397*4882a593Smuzhiyun 	cr_portovfl + 1,
2398*4882a593Smuzhiyun 	cr_portovfl + 2,
2399*4882a593Smuzhiyun 	cr_portovfl + 3,
2400*4882a593Smuzhiyun 	cr_portovfl + 4,
2401*4882a593Smuzhiyun };
2402*4882a593Smuzhiyun 
2403*4882a593Smuzhiyun /*
2404*4882a593Smuzhiyun  * same as cntr6120names and cntr6120indices, but for port-specific counters.
2405*4882a593Smuzhiyun  * portcntr6120indices is somewhat complicated by some registers needing
2406*4882a593Smuzhiyun  * adjustments of various kinds, and those are ORed with _PORT_VIRT_FLAG
2407*4882a593Smuzhiyun  */
2408*4882a593Smuzhiyun static const char portcntr6120names[] =
2409*4882a593Smuzhiyun 	"TxPkt\n"
2410*4882a593Smuzhiyun 	"TxFlowPkt\n"
2411*4882a593Smuzhiyun 	"TxWords\n"
2412*4882a593Smuzhiyun 	"RxPkt\n"
2413*4882a593Smuzhiyun 	"RxFlowPkt\n"
2414*4882a593Smuzhiyun 	"RxWords\n"
2415*4882a593Smuzhiyun 	"TxFlowStall\n"
2416*4882a593Smuzhiyun 	"E IBStatusChng\n"
2417*4882a593Smuzhiyun 	"IBLinkDown\n"
2418*4882a593Smuzhiyun 	"IBLnkRecov\n"
2419*4882a593Smuzhiyun 	"IBRxLinkErr\n"
2420*4882a593Smuzhiyun 	"IBSymbolErr\n"
2421*4882a593Smuzhiyun 	"RxLLIErr\n"
2422*4882a593Smuzhiyun 	"RxBadFormat\n"
2423*4882a593Smuzhiyun 	"RxBadLen\n"
2424*4882a593Smuzhiyun 	"RxBufOvrfl\n"
2425*4882a593Smuzhiyun 	"RxEBP\n"
2426*4882a593Smuzhiyun 	"RxFlowCtlErr\n"
2427*4882a593Smuzhiyun 	"RxICRCerr\n"
2428*4882a593Smuzhiyun 	"RxLPCRCerr\n"
2429*4882a593Smuzhiyun 	"RxVCRCerr\n"
2430*4882a593Smuzhiyun 	"RxInvalLen\n"
2431*4882a593Smuzhiyun 	"RxInvalPKey\n"
2432*4882a593Smuzhiyun 	"RxPktDropped\n"
2433*4882a593Smuzhiyun 	"TxBadLength\n"
2434*4882a593Smuzhiyun 	"TxDropped\n"
2435*4882a593Smuzhiyun 	"TxInvalLen\n"
2436*4882a593Smuzhiyun 	"TxUnderrun\n"
2437*4882a593Smuzhiyun 	"TxUnsupVL\n"
2438*4882a593Smuzhiyun 	;
2439*4882a593Smuzhiyun 
2440*4882a593Smuzhiyun #define _PORT_VIRT_FLAG 0x8000 /* "virtual", need adjustments */
2441*4882a593Smuzhiyun static const size_t portcntr6120indices[] = {
2442*4882a593Smuzhiyun 	QIBPORTCNTR_PKTSEND | _PORT_VIRT_FLAG,
2443*4882a593Smuzhiyun 	cr_pktsendflow,
2444*4882a593Smuzhiyun 	QIBPORTCNTR_WORDSEND | _PORT_VIRT_FLAG,
2445*4882a593Smuzhiyun 	QIBPORTCNTR_PKTRCV | _PORT_VIRT_FLAG,
2446*4882a593Smuzhiyun 	cr_pktrcvflowctrl,
2447*4882a593Smuzhiyun 	QIBPORTCNTR_WORDRCV | _PORT_VIRT_FLAG,
2448*4882a593Smuzhiyun 	QIBPORTCNTR_SENDSTALL | _PORT_VIRT_FLAG,
2449*4882a593Smuzhiyun 	cr_ibstatuschange,
2450*4882a593Smuzhiyun 	QIBPORTCNTR_IBLINKDOWN | _PORT_VIRT_FLAG,
2451*4882a593Smuzhiyun 	QIBPORTCNTR_IBLINKERRRECOV | _PORT_VIRT_FLAG,
2452*4882a593Smuzhiyun 	QIBPORTCNTR_ERRLINK | _PORT_VIRT_FLAG,
2453*4882a593Smuzhiyun 	QIBPORTCNTR_IBSYMBOLERR | _PORT_VIRT_FLAG,
2454*4882a593Smuzhiyun 	QIBPORTCNTR_LLI | _PORT_VIRT_FLAG,
2455*4882a593Smuzhiyun 	QIBPORTCNTR_BADFORMAT | _PORT_VIRT_FLAG,
2456*4882a593Smuzhiyun 	QIBPORTCNTR_ERR_RLEN | _PORT_VIRT_FLAG,
2457*4882a593Smuzhiyun 	QIBPORTCNTR_RCVOVFL | _PORT_VIRT_FLAG,
2458*4882a593Smuzhiyun 	QIBPORTCNTR_RCVEBP | _PORT_VIRT_FLAG,
2459*4882a593Smuzhiyun 	cr_rcvflowctrl_err,
2460*4882a593Smuzhiyun 	QIBPORTCNTR_ERRICRC | _PORT_VIRT_FLAG,
2461*4882a593Smuzhiyun 	QIBPORTCNTR_ERRLPCRC | _PORT_VIRT_FLAG,
2462*4882a593Smuzhiyun 	QIBPORTCNTR_ERRVCRC | _PORT_VIRT_FLAG,
2463*4882a593Smuzhiyun 	QIBPORTCNTR_INVALIDRLEN | _PORT_VIRT_FLAG,
2464*4882a593Smuzhiyun 	QIBPORTCNTR_ERRPKEY | _PORT_VIRT_FLAG,
2465*4882a593Smuzhiyun 	QIBPORTCNTR_RXDROPPKT | _PORT_VIRT_FLAG,
2466*4882a593Smuzhiyun 	cr_invalidslen,
2467*4882a593Smuzhiyun 	cr_senddropped,
2468*4882a593Smuzhiyun 	cr_errslen,
2469*4882a593Smuzhiyun 	cr_sendunderrun,
2470*4882a593Smuzhiyun 	cr_txunsupvl,
2471*4882a593Smuzhiyun };
2472*4882a593Smuzhiyun 
2473*4882a593Smuzhiyun /* do all the setup to make the counter reads efficient later */
init_6120_cntrnames(struct qib_devdata * dd)2474*4882a593Smuzhiyun static void init_6120_cntrnames(struct qib_devdata *dd)
2475*4882a593Smuzhiyun {
2476*4882a593Smuzhiyun 	int i, j = 0;
2477*4882a593Smuzhiyun 	char *s;
2478*4882a593Smuzhiyun 
2479*4882a593Smuzhiyun 	for (i = 0, s = (char *)cntr6120names; s && j <= dd->cfgctxts;
2480*4882a593Smuzhiyun 	     i++) {
2481*4882a593Smuzhiyun 		/* we always have at least one counter before the egrovfl */
2482*4882a593Smuzhiyun 		if (!j && !strncmp("Ctxt0EgrOvfl", s + 1, 12))
2483*4882a593Smuzhiyun 			j = 1;
2484*4882a593Smuzhiyun 		s = strchr(s + 1, '\n');
2485*4882a593Smuzhiyun 		if (s && j)
2486*4882a593Smuzhiyun 			j++;
2487*4882a593Smuzhiyun 	}
2488*4882a593Smuzhiyun 	dd->cspec->ncntrs = i;
2489*4882a593Smuzhiyun 	if (!s)
2490*4882a593Smuzhiyun 		/* full list; size is without terminating null */
2491*4882a593Smuzhiyun 		dd->cspec->cntrnamelen = sizeof(cntr6120names) - 1;
2492*4882a593Smuzhiyun 	else
2493*4882a593Smuzhiyun 		dd->cspec->cntrnamelen = 1 + s - cntr6120names;
2494*4882a593Smuzhiyun 	dd->cspec->cntrs = kmalloc_array(dd->cspec->ncntrs, sizeof(u64),
2495*4882a593Smuzhiyun 					 GFP_KERNEL);
2496*4882a593Smuzhiyun 
2497*4882a593Smuzhiyun 	for (i = 0, s = (char *)portcntr6120names; s; i++)
2498*4882a593Smuzhiyun 		s = strchr(s + 1, '\n');
2499*4882a593Smuzhiyun 	dd->cspec->nportcntrs = i - 1;
2500*4882a593Smuzhiyun 	dd->cspec->portcntrnamelen = sizeof(portcntr6120names) - 1;
2501*4882a593Smuzhiyun 	dd->cspec->portcntrs = kmalloc_array(dd->cspec->nportcntrs,
2502*4882a593Smuzhiyun 					     sizeof(u64),
2503*4882a593Smuzhiyun 					     GFP_KERNEL);
2504*4882a593Smuzhiyun }
2505*4882a593Smuzhiyun 
qib_read_6120cntrs(struct qib_devdata * dd,loff_t pos,char ** namep,u64 ** cntrp)2506*4882a593Smuzhiyun static u32 qib_read_6120cntrs(struct qib_devdata *dd, loff_t pos, char **namep,
2507*4882a593Smuzhiyun 			      u64 **cntrp)
2508*4882a593Smuzhiyun {
2509*4882a593Smuzhiyun 	u32 ret;
2510*4882a593Smuzhiyun 
2511*4882a593Smuzhiyun 	if (namep) {
2512*4882a593Smuzhiyun 		ret = dd->cspec->cntrnamelen;
2513*4882a593Smuzhiyun 		if (pos >= ret)
2514*4882a593Smuzhiyun 			ret = 0; /* final read after getting everything */
2515*4882a593Smuzhiyun 		else
2516*4882a593Smuzhiyun 			*namep = (char *)cntr6120names;
2517*4882a593Smuzhiyun 	} else {
2518*4882a593Smuzhiyun 		u64 *cntr = dd->cspec->cntrs;
2519*4882a593Smuzhiyun 		int i;
2520*4882a593Smuzhiyun 
2521*4882a593Smuzhiyun 		ret = dd->cspec->ncntrs * sizeof(u64);
2522*4882a593Smuzhiyun 		if (!cntr || pos >= ret) {
2523*4882a593Smuzhiyun 			/* everything read, or couldn't get memory */
2524*4882a593Smuzhiyun 			ret = 0;
2525*4882a593Smuzhiyun 			goto done;
2526*4882a593Smuzhiyun 		}
2527*4882a593Smuzhiyun 		if (pos >= ret) {
2528*4882a593Smuzhiyun 			ret = 0; /* final read after getting everything */
2529*4882a593Smuzhiyun 			goto done;
2530*4882a593Smuzhiyun 		}
2531*4882a593Smuzhiyun 		*cntrp = cntr;
2532*4882a593Smuzhiyun 		for (i = 0; i < dd->cspec->ncntrs; i++)
2533*4882a593Smuzhiyun 			*cntr++ = read_6120_creg32(dd, cntr6120indices[i]);
2534*4882a593Smuzhiyun 	}
2535*4882a593Smuzhiyun done:
2536*4882a593Smuzhiyun 	return ret;
2537*4882a593Smuzhiyun }
2538*4882a593Smuzhiyun 
qib_read_6120portcntrs(struct qib_devdata * dd,loff_t pos,u32 port,char ** namep,u64 ** cntrp)2539*4882a593Smuzhiyun static u32 qib_read_6120portcntrs(struct qib_devdata *dd, loff_t pos, u32 port,
2540*4882a593Smuzhiyun 				  char **namep, u64 **cntrp)
2541*4882a593Smuzhiyun {
2542*4882a593Smuzhiyun 	u32 ret;
2543*4882a593Smuzhiyun 
2544*4882a593Smuzhiyun 	if (namep) {
2545*4882a593Smuzhiyun 		ret = dd->cspec->portcntrnamelen;
2546*4882a593Smuzhiyun 		if (pos >= ret)
2547*4882a593Smuzhiyun 			ret = 0; /* final read after getting everything */
2548*4882a593Smuzhiyun 		else
2549*4882a593Smuzhiyun 			*namep = (char *)portcntr6120names;
2550*4882a593Smuzhiyun 	} else {
2551*4882a593Smuzhiyun 		u64 *cntr = dd->cspec->portcntrs;
2552*4882a593Smuzhiyun 		struct qib_pportdata *ppd = &dd->pport[port];
2553*4882a593Smuzhiyun 		int i;
2554*4882a593Smuzhiyun 
2555*4882a593Smuzhiyun 		ret = dd->cspec->nportcntrs * sizeof(u64);
2556*4882a593Smuzhiyun 		if (!cntr || pos >= ret) {
2557*4882a593Smuzhiyun 			/* everything read, or couldn't get memory */
2558*4882a593Smuzhiyun 			ret = 0;
2559*4882a593Smuzhiyun 			goto done;
2560*4882a593Smuzhiyun 		}
2561*4882a593Smuzhiyun 		*cntrp = cntr;
2562*4882a593Smuzhiyun 		for (i = 0; i < dd->cspec->nportcntrs; i++) {
2563*4882a593Smuzhiyun 			if (portcntr6120indices[i] & _PORT_VIRT_FLAG)
2564*4882a593Smuzhiyun 				*cntr++ = qib_portcntr_6120(ppd,
2565*4882a593Smuzhiyun 					portcntr6120indices[i] &
2566*4882a593Smuzhiyun 					~_PORT_VIRT_FLAG);
2567*4882a593Smuzhiyun 			else
2568*4882a593Smuzhiyun 				*cntr++ = read_6120_creg32(dd,
2569*4882a593Smuzhiyun 					   portcntr6120indices[i]);
2570*4882a593Smuzhiyun 		}
2571*4882a593Smuzhiyun 	}
2572*4882a593Smuzhiyun done:
2573*4882a593Smuzhiyun 	return ret;
2574*4882a593Smuzhiyun }
2575*4882a593Smuzhiyun 
qib_chk_6120_errormask(struct qib_devdata * dd)2576*4882a593Smuzhiyun static void qib_chk_6120_errormask(struct qib_devdata *dd)
2577*4882a593Smuzhiyun {
2578*4882a593Smuzhiyun 	static u32 fixed;
2579*4882a593Smuzhiyun 	u32 ctrl;
2580*4882a593Smuzhiyun 	unsigned long errormask;
2581*4882a593Smuzhiyun 	unsigned long hwerrs;
2582*4882a593Smuzhiyun 
2583*4882a593Smuzhiyun 	if (!dd->cspec->errormask || !(dd->flags & QIB_INITTED))
2584*4882a593Smuzhiyun 		return;
2585*4882a593Smuzhiyun 
2586*4882a593Smuzhiyun 	errormask = qib_read_kreg64(dd, kr_errmask);
2587*4882a593Smuzhiyun 
2588*4882a593Smuzhiyun 	if (errormask == dd->cspec->errormask)
2589*4882a593Smuzhiyun 		return;
2590*4882a593Smuzhiyun 	fixed++;
2591*4882a593Smuzhiyun 
2592*4882a593Smuzhiyun 	hwerrs = qib_read_kreg64(dd, kr_hwerrstatus);
2593*4882a593Smuzhiyun 	ctrl = qib_read_kreg32(dd, kr_control);
2594*4882a593Smuzhiyun 
2595*4882a593Smuzhiyun 	qib_write_kreg(dd, kr_errmask,
2596*4882a593Smuzhiyun 		dd->cspec->errormask);
2597*4882a593Smuzhiyun 
2598*4882a593Smuzhiyun 	if ((hwerrs & dd->cspec->hwerrmask) ||
2599*4882a593Smuzhiyun 	    (ctrl & QLOGIC_IB_C_FREEZEMODE)) {
2600*4882a593Smuzhiyun 		qib_write_kreg(dd, kr_hwerrclear, 0ULL);
2601*4882a593Smuzhiyun 		qib_write_kreg(dd, kr_errclear, 0ULL);
2602*4882a593Smuzhiyun 		/* force re-interrupt of pending events, just in case */
2603*4882a593Smuzhiyun 		qib_write_kreg(dd, kr_intclear, 0ULL);
2604*4882a593Smuzhiyun 		qib_devinfo(dd->pcidev,
2605*4882a593Smuzhiyun 			 "errormask fixed(%u) %lx->%lx, ctrl %x hwerr %lx\n",
2606*4882a593Smuzhiyun 			 fixed, errormask, (unsigned long)dd->cspec->errormask,
2607*4882a593Smuzhiyun 			 ctrl, hwerrs);
2608*4882a593Smuzhiyun 	}
2609*4882a593Smuzhiyun }
2610*4882a593Smuzhiyun 
2611*4882a593Smuzhiyun /**
2612*4882a593Smuzhiyun  * qib_get_faststats - get word counters from chip before they overflow
2613*4882a593Smuzhiyun  * @opaque - contains a pointer to the qlogic_ib device qib_devdata
2614*4882a593Smuzhiyun  *
2615*4882a593Smuzhiyun  * This needs more work; in particular, decision on whether we really
2616*4882a593Smuzhiyun  * need traffic_wds done the way it is
2617*4882a593Smuzhiyun  * called from add_timer
2618*4882a593Smuzhiyun  */
qib_get_6120_faststats(struct timer_list * t)2619*4882a593Smuzhiyun static void qib_get_6120_faststats(struct timer_list *t)
2620*4882a593Smuzhiyun {
2621*4882a593Smuzhiyun 	struct qib_devdata *dd = from_timer(dd, t, stats_timer);
2622*4882a593Smuzhiyun 	struct qib_pportdata *ppd = dd->pport;
2623*4882a593Smuzhiyun 	unsigned long flags;
2624*4882a593Smuzhiyun 	u64 traffic_wds;
2625*4882a593Smuzhiyun 
2626*4882a593Smuzhiyun 	/*
2627*4882a593Smuzhiyun 	 * don't access the chip while running diags, or memory diags can
2628*4882a593Smuzhiyun 	 * fail
2629*4882a593Smuzhiyun 	 */
2630*4882a593Smuzhiyun 	if (!(dd->flags & QIB_INITTED) || dd->diag_client)
2631*4882a593Smuzhiyun 		/* but re-arm the timer, for diags case; won't hurt other */
2632*4882a593Smuzhiyun 		goto done;
2633*4882a593Smuzhiyun 
2634*4882a593Smuzhiyun 	/*
2635*4882a593Smuzhiyun 	 * We now try to maintain an activity timer, based on traffic
2636*4882a593Smuzhiyun 	 * exceeding a threshold, so we need to check the word-counts
2637*4882a593Smuzhiyun 	 * even if they are 64-bit.
2638*4882a593Smuzhiyun 	 */
2639*4882a593Smuzhiyun 	traffic_wds = qib_portcntr_6120(ppd, cr_wordsend) +
2640*4882a593Smuzhiyun 		qib_portcntr_6120(ppd, cr_wordrcv);
2641*4882a593Smuzhiyun 	spin_lock_irqsave(&dd->eep_st_lock, flags);
2642*4882a593Smuzhiyun 	traffic_wds -= dd->traffic_wds;
2643*4882a593Smuzhiyun 	dd->traffic_wds += traffic_wds;
2644*4882a593Smuzhiyun 	spin_unlock_irqrestore(&dd->eep_st_lock, flags);
2645*4882a593Smuzhiyun 
2646*4882a593Smuzhiyun 	qib_chk_6120_errormask(dd);
2647*4882a593Smuzhiyun done:
2648*4882a593Smuzhiyun 	mod_timer(&dd->stats_timer, jiffies + HZ * ACTIVITY_TIMER);
2649*4882a593Smuzhiyun }
2650*4882a593Smuzhiyun 
2651*4882a593Smuzhiyun /* no interrupt fallback for these chips */
qib_6120_nointr_fallback(struct qib_devdata * dd)2652*4882a593Smuzhiyun static int qib_6120_nointr_fallback(struct qib_devdata *dd)
2653*4882a593Smuzhiyun {
2654*4882a593Smuzhiyun 	return 0;
2655*4882a593Smuzhiyun }
2656*4882a593Smuzhiyun 
2657*4882a593Smuzhiyun /*
2658*4882a593Smuzhiyun  * reset the XGXS (between serdes and IBC).  Slightly less intrusive
2659*4882a593Smuzhiyun  * than resetting the IBC or external link state, and useful in some
2660*4882a593Smuzhiyun  * cases to cause some retraining.  To do this right, we reset IBC
2661*4882a593Smuzhiyun  * as well.
2662*4882a593Smuzhiyun  */
qib_6120_xgxs_reset(struct qib_pportdata * ppd)2663*4882a593Smuzhiyun static void qib_6120_xgxs_reset(struct qib_pportdata *ppd)
2664*4882a593Smuzhiyun {
2665*4882a593Smuzhiyun 	u64 val, prev_val;
2666*4882a593Smuzhiyun 	struct qib_devdata *dd = ppd->dd;
2667*4882a593Smuzhiyun 
2668*4882a593Smuzhiyun 	prev_val = qib_read_kreg64(dd, kr_xgxs_cfg);
2669*4882a593Smuzhiyun 	val = prev_val | QLOGIC_IB_XGXS_RESET;
2670*4882a593Smuzhiyun 	prev_val &= ~QLOGIC_IB_XGXS_RESET; /* be sure */
2671*4882a593Smuzhiyun 	qib_write_kreg(dd, kr_control,
2672*4882a593Smuzhiyun 		       dd->control & ~QLOGIC_IB_C_LINKENABLE);
2673*4882a593Smuzhiyun 	qib_write_kreg(dd, kr_xgxs_cfg, val);
2674*4882a593Smuzhiyun 	qib_read_kreg32(dd, kr_scratch);
2675*4882a593Smuzhiyun 	qib_write_kreg(dd, kr_xgxs_cfg, prev_val);
2676*4882a593Smuzhiyun 	qib_write_kreg(dd, kr_control, dd->control);
2677*4882a593Smuzhiyun }
2678*4882a593Smuzhiyun 
qib_6120_get_ib_cfg(struct qib_pportdata * ppd,int which)2679*4882a593Smuzhiyun static int qib_6120_get_ib_cfg(struct qib_pportdata *ppd, int which)
2680*4882a593Smuzhiyun {
2681*4882a593Smuzhiyun 	int ret;
2682*4882a593Smuzhiyun 
2683*4882a593Smuzhiyun 	switch (which) {
2684*4882a593Smuzhiyun 	case QIB_IB_CFG_LWID:
2685*4882a593Smuzhiyun 		ret = ppd->link_width_active;
2686*4882a593Smuzhiyun 		break;
2687*4882a593Smuzhiyun 
2688*4882a593Smuzhiyun 	case QIB_IB_CFG_SPD:
2689*4882a593Smuzhiyun 		ret = ppd->link_speed_active;
2690*4882a593Smuzhiyun 		break;
2691*4882a593Smuzhiyun 
2692*4882a593Smuzhiyun 	case QIB_IB_CFG_LWID_ENB:
2693*4882a593Smuzhiyun 		ret = ppd->link_width_enabled;
2694*4882a593Smuzhiyun 		break;
2695*4882a593Smuzhiyun 
2696*4882a593Smuzhiyun 	case QIB_IB_CFG_SPD_ENB:
2697*4882a593Smuzhiyun 		ret = ppd->link_speed_enabled;
2698*4882a593Smuzhiyun 		break;
2699*4882a593Smuzhiyun 
2700*4882a593Smuzhiyun 	case QIB_IB_CFG_OP_VLS:
2701*4882a593Smuzhiyun 		ret = ppd->vls_operational;
2702*4882a593Smuzhiyun 		break;
2703*4882a593Smuzhiyun 
2704*4882a593Smuzhiyun 	case QIB_IB_CFG_VL_HIGH_CAP:
2705*4882a593Smuzhiyun 		ret = 0;
2706*4882a593Smuzhiyun 		break;
2707*4882a593Smuzhiyun 
2708*4882a593Smuzhiyun 	case QIB_IB_CFG_VL_LOW_CAP:
2709*4882a593Smuzhiyun 		ret = 0;
2710*4882a593Smuzhiyun 		break;
2711*4882a593Smuzhiyun 
2712*4882a593Smuzhiyun 	case QIB_IB_CFG_OVERRUN_THRESH: /* IB overrun threshold */
2713*4882a593Smuzhiyun 		ret = SYM_FIELD(ppd->dd->cspec->ibcctrl, IBCCtrl,
2714*4882a593Smuzhiyun 				OverrunThreshold);
2715*4882a593Smuzhiyun 		break;
2716*4882a593Smuzhiyun 
2717*4882a593Smuzhiyun 	case QIB_IB_CFG_PHYERR_THRESH: /* IB PHY error threshold */
2718*4882a593Smuzhiyun 		ret = SYM_FIELD(ppd->dd->cspec->ibcctrl, IBCCtrl,
2719*4882a593Smuzhiyun 				PhyerrThreshold);
2720*4882a593Smuzhiyun 		break;
2721*4882a593Smuzhiyun 
2722*4882a593Smuzhiyun 	case QIB_IB_CFG_LINKDEFAULT: /* IB link default (sleep/poll) */
2723*4882a593Smuzhiyun 		/* will only take effect when the link state changes */
2724*4882a593Smuzhiyun 		ret = (ppd->dd->cspec->ibcctrl &
2725*4882a593Smuzhiyun 		       SYM_MASK(IBCCtrl, LinkDownDefaultState)) ?
2726*4882a593Smuzhiyun 			IB_LINKINITCMD_SLEEP : IB_LINKINITCMD_POLL;
2727*4882a593Smuzhiyun 		break;
2728*4882a593Smuzhiyun 
2729*4882a593Smuzhiyun 	case QIB_IB_CFG_HRTBT: /* Get Heartbeat off/enable/auto */
2730*4882a593Smuzhiyun 		ret = 0; /* no heartbeat on this chip */
2731*4882a593Smuzhiyun 		break;
2732*4882a593Smuzhiyun 
2733*4882a593Smuzhiyun 	case QIB_IB_CFG_PMA_TICKS:
2734*4882a593Smuzhiyun 		ret = 250; /* 1 usec. */
2735*4882a593Smuzhiyun 		break;
2736*4882a593Smuzhiyun 
2737*4882a593Smuzhiyun 	default:
2738*4882a593Smuzhiyun 		ret =  -EINVAL;
2739*4882a593Smuzhiyun 		break;
2740*4882a593Smuzhiyun 	}
2741*4882a593Smuzhiyun 	return ret;
2742*4882a593Smuzhiyun }
2743*4882a593Smuzhiyun 
2744*4882a593Smuzhiyun /*
2745*4882a593Smuzhiyun  * We assume range checking is already done, if needed.
2746*4882a593Smuzhiyun  */
qib_6120_set_ib_cfg(struct qib_pportdata * ppd,int which,u32 val)2747*4882a593Smuzhiyun static int qib_6120_set_ib_cfg(struct qib_pportdata *ppd, int which, u32 val)
2748*4882a593Smuzhiyun {
2749*4882a593Smuzhiyun 	struct qib_devdata *dd = ppd->dd;
2750*4882a593Smuzhiyun 	int ret = 0;
2751*4882a593Smuzhiyun 	u64 val64;
2752*4882a593Smuzhiyun 	u16 lcmd, licmd;
2753*4882a593Smuzhiyun 
2754*4882a593Smuzhiyun 	switch (which) {
2755*4882a593Smuzhiyun 	case QIB_IB_CFG_LWID_ENB:
2756*4882a593Smuzhiyun 		ppd->link_width_enabled = val;
2757*4882a593Smuzhiyun 		break;
2758*4882a593Smuzhiyun 
2759*4882a593Smuzhiyun 	case QIB_IB_CFG_SPD_ENB:
2760*4882a593Smuzhiyun 		ppd->link_speed_enabled = val;
2761*4882a593Smuzhiyun 		break;
2762*4882a593Smuzhiyun 
2763*4882a593Smuzhiyun 	case QIB_IB_CFG_OVERRUN_THRESH: /* IB overrun threshold */
2764*4882a593Smuzhiyun 		val64 = SYM_FIELD(dd->cspec->ibcctrl, IBCCtrl,
2765*4882a593Smuzhiyun 				  OverrunThreshold);
2766*4882a593Smuzhiyun 		if (val64 != val) {
2767*4882a593Smuzhiyun 			dd->cspec->ibcctrl &=
2768*4882a593Smuzhiyun 				~SYM_MASK(IBCCtrl, OverrunThreshold);
2769*4882a593Smuzhiyun 			dd->cspec->ibcctrl |= (u64) val <<
2770*4882a593Smuzhiyun 				SYM_LSB(IBCCtrl, OverrunThreshold);
2771*4882a593Smuzhiyun 			qib_write_kreg(dd, kr_ibcctrl, dd->cspec->ibcctrl);
2772*4882a593Smuzhiyun 			qib_write_kreg(dd, kr_scratch, 0);
2773*4882a593Smuzhiyun 		}
2774*4882a593Smuzhiyun 		break;
2775*4882a593Smuzhiyun 
2776*4882a593Smuzhiyun 	case QIB_IB_CFG_PHYERR_THRESH: /* IB PHY error threshold */
2777*4882a593Smuzhiyun 		val64 = SYM_FIELD(dd->cspec->ibcctrl, IBCCtrl,
2778*4882a593Smuzhiyun 				  PhyerrThreshold);
2779*4882a593Smuzhiyun 		if (val64 != val) {
2780*4882a593Smuzhiyun 			dd->cspec->ibcctrl &=
2781*4882a593Smuzhiyun 				~SYM_MASK(IBCCtrl, PhyerrThreshold);
2782*4882a593Smuzhiyun 			dd->cspec->ibcctrl |= (u64) val <<
2783*4882a593Smuzhiyun 				SYM_LSB(IBCCtrl, PhyerrThreshold);
2784*4882a593Smuzhiyun 			qib_write_kreg(dd, kr_ibcctrl, dd->cspec->ibcctrl);
2785*4882a593Smuzhiyun 			qib_write_kreg(dd, kr_scratch, 0);
2786*4882a593Smuzhiyun 		}
2787*4882a593Smuzhiyun 		break;
2788*4882a593Smuzhiyun 
2789*4882a593Smuzhiyun 	case QIB_IB_CFG_PKEYS: /* update pkeys */
2790*4882a593Smuzhiyun 		val64 = (u64) ppd->pkeys[0] | ((u64) ppd->pkeys[1] << 16) |
2791*4882a593Smuzhiyun 			((u64) ppd->pkeys[2] << 32) |
2792*4882a593Smuzhiyun 			((u64) ppd->pkeys[3] << 48);
2793*4882a593Smuzhiyun 		qib_write_kreg(dd, kr_partitionkey, val64);
2794*4882a593Smuzhiyun 		break;
2795*4882a593Smuzhiyun 
2796*4882a593Smuzhiyun 	case QIB_IB_CFG_LINKDEFAULT: /* IB link default (sleep/poll) */
2797*4882a593Smuzhiyun 		/* will only take effect when the link state changes */
2798*4882a593Smuzhiyun 		if (val == IB_LINKINITCMD_POLL)
2799*4882a593Smuzhiyun 			dd->cspec->ibcctrl &=
2800*4882a593Smuzhiyun 				~SYM_MASK(IBCCtrl, LinkDownDefaultState);
2801*4882a593Smuzhiyun 		else /* SLEEP */
2802*4882a593Smuzhiyun 			dd->cspec->ibcctrl |=
2803*4882a593Smuzhiyun 				SYM_MASK(IBCCtrl, LinkDownDefaultState);
2804*4882a593Smuzhiyun 		qib_write_kreg(dd, kr_ibcctrl, dd->cspec->ibcctrl);
2805*4882a593Smuzhiyun 		qib_write_kreg(dd, kr_scratch, 0);
2806*4882a593Smuzhiyun 		break;
2807*4882a593Smuzhiyun 
2808*4882a593Smuzhiyun 	case QIB_IB_CFG_MTU: /* update the MTU in IBC */
2809*4882a593Smuzhiyun 		/*
2810*4882a593Smuzhiyun 		 * Update our housekeeping variables, and set IBC max
2811*4882a593Smuzhiyun 		 * size, same as init code; max IBC is max we allow in
2812*4882a593Smuzhiyun 		 * buffer, less the qword pbc, plus 1 for ICRC, in dwords
2813*4882a593Smuzhiyun 		 * Set even if it's unchanged, print debug message only
2814*4882a593Smuzhiyun 		 * on changes.
2815*4882a593Smuzhiyun 		 */
2816*4882a593Smuzhiyun 		val = (ppd->ibmaxlen >> 2) + 1;
2817*4882a593Smuzhiyun 		dd->cspec->ibcctrl &= ~SYM_MASK(IBCCtrl, MaxPktLen);
2818*4882a593Smuzhiyun 		dd->cspec->ibcctrl |= (u64)val <<
2819*4882a593Smuzhiyun 			SYM_LSB(IBCCtrl, MaxPktLen);
2820*4882a593Smuzhiyun 		qib_write_kreg(dd, kr_ibcctrl, dd->cspec->ibcctrl);
2821*4882a593Smuzhiyun 		qib_write_kreg(dd, kr_scratch, 0);
2822*4882a593Smuzhiyun 		break;
2823*4882a593Smuzhiyun 
2824*4882a593Smuzhiyun 	case QIB_IB_CFG_LSTATE: /* set the IB link state */
2825*4882a593Smuzhiyun 		switch (val & 0xffff0000) {
2826*4882a593Smuzhiyun 		case IB_LINKCMD_DOWN:
2827*4882a593Smuzhiyun 			lcmd = QLOGIC_IB_IBCC_LINKCMD_DOWN;
2828*4882a593Smuzhiyun 			if (!dd->cspec->ibdeltainprog) {
2829*4882a593Smuzhiyun 				dd->cspec->ibdeltainprog = 1;
2830*4882a593Smuzhiyun 				dd->cspec->ibsymsnap =
2831*4882a593Smuzhiyun 					read_6120_creg32(dd, cr_ibsymbolerr);
2832*4882a593Smuzhiyun 				dd->cspec->iblnkerrsnap =
2833*4882a593Smuzhiyun 					read_6120_creg32(dd, cr_iblinkerrrecov);
2834*4882a593Smuzhiyun 			}
2835*4882a593Smuzhiyun 			break;
2836*4882a593Smuzhiyun 
2837*4882a593Smuzhiyun 		case IB_LINKCMD_ARMED:
2838*4882a593Smuzhiyun 			lcmd = QLOGIC_IB_IBCC_LINKCMD_ARMED;
2839*4882a593Smuzhiyun 			break;
2840*4882a593Smuzhiyun 
2841*4882a593Smuzhiyun 		case IB_LINKCMD_ACTIVE:
2842*4882a593Smuzhiyun 			lcmd = QLOGIC_IB_IBCC_LINKCMD_ACTIVE;
2843*4882a593Smuzhiyun 			break;
2844*4882a593Smuzhiyun 
2845*4882a593Smuzhiyun 		default:
2846*4882a593Smuzhiyun 			ret = -EINVAL;
2847*4882a593Smuzhiyun 			qib_dev_err(dd, "bad linkcmd req 0x%x\n", val >> 16);
2848*4882a593Smuzhiyun 			goto bail;
2849*4882a593Smuzhiyun 		}
2850*4882a593Smuzhiyun 		switch (val & 0xffff) {
2851*4882a593Smuzhiyun 		case IB_LINKINITCMD_NOP:
2852*4882a593Smuzhiyun 			licmd = 0;
2853*4882a593Smuzhiyun 			break;
2854*4882a593Smuzhiyun 
2855*4882a593Smuzhiyun 		case IB_LINKINITCMD_POLL:
2856*4882a593Smuzhiyun 			licmd = QLOGIC_IB_IBCC_LINKINITCMD_POLL;
2857*4882a593Smuzhiyun 			break;
2858*4882a593Smuzhiyun 
2859*4882a593Smuzhiyun 		case IB_LINKINITCMD_SLEEP:
2860*4882a593Smuzhiyun 			licmd = QLOGIC_IB_IBCC_LINKINITCMD_SLEEP;
2861*4882a593Smuzhiyun 			break;
2862*4882a593Smuzhiyun 
2863*4882a593Smuzhiyun 		case IB_LINKINITCMD_DISABLE:
2864*4882a593Smuzhiyun 			licmd = QLOGIC_IB_IBCC_LINKINITCMD_DISABLE;
2865*4882a593Smuzhiyun 			break;
2866*4882a593Smuzhiyun 
2867*4882a593Smuzhiyun 		default:
2868*4882a593Smuzhiyun 			ret = -EINVAL;
2869*4882a593Smuzhiyun 			qib_dev_err(dd, "bad linkinitcmd req 0x%x\n",
2870*4882a593Smuzhiyun 				    val & 0xffff);
2871*4882a593Smuzhiyun 			goto bail;
2872*4882a593Smuzhiyun 		}
2873*4882a593Smuzhiyun 		qib_set_ib_6120_lstate(ppd, lcmd, licmd);
2874*4882a593Smuzhiyun 		goto bail;
2875*4882a593Smuzhiyun 
2876*4882a593Smuzhiyun 	case QIB_IB_CFG_HRTBT:
2877*4882a593Smuzhiyun 		ret = -EINVAL;
2878*4882a593Smuzhiyun 		break;
2879*4882a593Smuzhiyun 
2880*4882a593Smuzhiyun 	default:
2881*4882a593Smuzhiyun 		ret = -EINVAL;
2882*4882a593Smuzhiyun 	}
2883*4882a593Smuzhiyun bail:
2884*4882a593Smuzhiyun 	return ret;
2885*4882a593Smuzhiyun }
2886*4882a593Smuzhiyun 
qib_6120_set_loopback(struct qib_pportdata * ppd,const char * what)2887*4882a593Smuzhiyun static int qib_6120_set_loopback(struct qib_pportdata *ppd, const char *what)
2888*4882a593Smuzhiyun {
2889*4882a593Smuzhiyun 	int ret = 0;
2890*4882a593Smuzhiyun 
2891*4882a593Smuzhiyun 	if (!strncmp(what, "ibc", 3)) {
2892*4882a593Smuzhiyun 		ppd->dd->cspec->ibcctrl |= SYM_MASK(IBCCtrl, Loopback);
2893*4882a593Smuzhiyun 		qib_devinfo(ppd->dd->pcidev, "Enabling IB%u:%u IBC loopback\n",
2894*4882a593Smuzhiyun 			 ppd->dd->unit, ppd->port);
2895*4882a593Smuzhiyun 	} else if (!strncmp(what, "off", 3)) {
2896*4882a593Smuzhiyun 		ppd->dd->cspec->ibcctrl &= ~SYM_MASK(IBCCtrl, Loopback);
2897*4882a593Smuzhiyun 		qib_devinfo(ppd->dd->pcidev,
2898*4882a593Smuzhiyun 			"Disabling IB%u:%u IBC loopback (normal)\n",
2899*4882a593Smuzhiyun 			ppd->dd->unit, ppd->port);
2900*4882a593Smuzhiyun 	} else
2901*4882a593Smuzhiyun 		ret = -EINVAL;
2902*4882a593Smuzhiyun 	if (!ret) {
2903*4882a593Smuzhiyun 		qib_write_kreg(ppd->dd, kr_ibcctrl, ppd->dd->cspec->ibcctrl);
2904*4882a593Smuzhiyun 		qib_write_kreg(ppd->dd, kr_scratch, 0);
2905*4882a593Smuzhiyun 	}
2906*4882a593Smuzhiyun 	return ret;
2907*4882a593Smuzhiyun }
2908*4882a593Smuzhiyun 
pma_6120_timer(struct timer_list * t)2909*4882a593Smuzhiyun static void pma_6120_timer(struct timer_list *t)
2910*4882a593Smuzhiyun {
2911*4882a593Smuzhiyun 	struct qib_chip_specific *cs = from_timer(cs, t, pma_timer);
2912*4882a593Smuzhiyun 	struct qib_pportdata *ppd = cs->ppd;
2913*4882a593Smuzhiyun 	struct qib_ibport *ibp = &ppd->ibport_data;
2914*4882a593Smuzhiyun 	unsigned long flags;
2915*4882a593Smuzhiyun 
2916*4882a593Smuzhiyun 	spin_lock_irqsave(&ibp->rvp.lock, flags);
2917*4882a593Smuzhiyun 	if (cs->pma_sample_status == IB_PMA_SAMPLE_STATUS_STARTED) {
2918*4882a593Smuzhiyun 		cs->pma_sample_status = IB_PMA_SAMPLE_STATUS_RUNNING;
2919*4882a593Smuzhiyun 		qib_snapshot_counters(ppd, &cs->sword, &cs->rword,
2920*4882a593Smuzhiyun 				      &cs->spkts, &cs->rpkts, &cs->xmit_wait);
2921*4882a593Smuzhiyun 		mod_timer(&cs->pma_timer,
2922*4882a593Smuzhiyun 		      jiffies + usecs_to_jiffies(ibp->rvp.pma_sample_interval));
2923*4882a593Smuzhiyun 	} else if (cs->pma_sample_status == IB_PMA_SAMPLE_STATUS_RUNNING) {
2924*4882a593Smuzhiyun 		u64 ta, tb, tc, td, te;
2925*4882a593Smuzhiyun 
2926*4882a593Smuzhiyun 		cs->pma_sample_status = IB_PMA_SAMPLE_STATUS_DONE;
2927*4882a593Smuzhiyun 		qib_snapshot_counters(ppd, &ta, &tb, &tc, &td, &te);
2928*4882a593Smuzhiyun 
2929*4882a593Smuzhiyun 		cs->sword = ta - cs->sword;
2930*4882a593Smuzhiyun 		cs->rword = tb - cs->rword;
2931*4882a593Smuzhiyun 		cs->spkts = tc - cs->spkts;
2932*4882a593Smuzhiyun 		cs->rpkts = td - cs->rpkts;
2933*4882a593Smuzhiyun 		cs->xmit_wait = te - cs->xmit_wait;
2934*4882a593Smuzhiyun 	}
2935*4882a593Smuzhiyun 	spin_unlock_irqrestore(&ibp->rvp.lock, flags);
2936*4882a593Smuzhiyun }
2937*4882a593Smuzhiyun 
2938*4882a593Smuzhiyun /*
2939*4882a593Smuzhiyun  * Note that the caller has the ibp->rvp.lock held.
2940*4882a593Smuzhiyun  */
qib_set_cntr_6120_sample(struct qib_pportdata * ppd,u32 intv,u32 start)2941*4882a593Smuzhiyun static void qib_set_cntr_6120_sample(struct qib_pportdata *ppd, u32 intv,
2942*4882a593Smuzhiyun 				     u32 start)
2943*4882a593Smuzhiyun {
2944*4882a593Smuzhiyun 	struct qib_chip_specific *cs = ppd->dd->cspec;
2945*4882a593Smuzhiyun 
2946*4882a593Smuzhiyun 	if (start && intv) {
2947*4882a593Smuzhiyun 		cs->pma_sample_status = IB_PMA_SAMPLE_STATUS_STARTED;
2948*4882a593Smuzhiyun 		mod_timer(&cs->pma_timer, jiffies + usecs_to_jiffies(start));
2949*4882a593Smuzhiyun 	} else if (intv) {
2950*4882a593Smuzhiyun 		cs->pma_sample_status = IB_PMA_SAMPLE_STATUS_RUNNING;
2951*4882a593Smuzhiyun 		qib_snapshot_counters(ppd, &cs->sword, &cs->rword,
2952*4882a593Smuzhiyun 				      &cs->spkts, &cs->rpkts, &cs->xmit_wait);
2953*4882a593Smuzhiyun 		mod_timer(&cs->pma_timer, jiffies + usecs_to_jiffies(intv));
2954*4882a593Smuzhiyun 	} else {
2955*4882a593Smuzhiyun 		cs->pma_sample_status = IB_PMA_SAMPLE_STATUS_DONE;
2956*4882a593Smuzhiyun 		cs->sword = 0;
2957*4882a593Smuzhiyun 		cs->rword = 0;
2958*4882a593Smuzhiyun 		cs->spkts = 0;
2959*4882a593Smuzhiyun 		cs->rpkts = 0;
2960*4882a593Smuzhiyun 		cs->xmit_wait = 0;
2961*4882a593Smuzhiyun 	}
2962*4882a593Smuzhiyun }
2963*4882a593Smuzhiyun 
qib_6120_iblink_state(u64 ibcs)2964*4882a593Smuzhiyun static u32 qib_6120_iblink_state(u64 ibcs)
2965*4882a593Smuzhiyun {
2966*4882a593Smuzhiyun 	u32 state = (u32)SYM_FIELD(ibcs, IBCStatus, LinkState);
2967*4882a593Smuzhiyun 
2968*4882a593Smuzhiyun 	switch (state) {
2969*4882a593Smuzhiyun 	case IB_6120_L_STATE_INIT:
2970*4882a593Smuzhiyun 		state = IB_PORT_INIT;
2971*4882a593Smuzhiyun 		break;
2972*4882a593Smuzhiyun 	case IB_6120_L_STATE_ARM:
2973*4882a593Smuzhiyun 		state = IB_PORT_ARMED;
2974*4882a593Smuzhiyun 		break;
2975*4882a593Smuzhiyun 	case IB_6120_L_STATE_ACTIVE:
2976*4882a593Smuzhiyun 	case IB_6120_L_STATE_ACT_DEFER:
2977*4882a593Smuzhiyun 		state = IB_PORT_ACTIVE;
2978*4882a593Smuzhiyun 		break;
2979*4882a593Smuzhiyun 	default:
2980*4882a593Smuzhiyun 		fallthrough;
2981*4882a593Smuzhiyun 	case IB_6120_L_STATE_DOWN:
2982*4882a593Smuzhiyun 		state = IB_PORT_DOWN;
2983*4882a593Smuzhiyun 		break;
2984*4882a593Smuzhiyun 	}
2985*4882a593Smuzhiyun 	return state;
2986*4882a593Smuzhiyun }
2987*4882a593Smuzhiyun 
2988*4882a593Smuzhiyun /* returns the IBTA port state, rather than the IBC link training state */
qib_6120_phys_portstate(u64 ibcs)2989*4882a593Smuzhiyun static u8 qib_6120_phys_portstate(u64 ibcs)
2990*4882a593Smuzhiyun {
2991*4882a593Smuzhiyun 	u8 state = (u8)SYM_FIELD(ibcs, IBCStatus, LinkTrainingState);
2992*4882a593Smuzhiyun 	return qib_6120_physportstate[state];
2993*4882a593Smuzhiyun }
2994*4882a593Smuzhiyun 
qib_6120_ib_updown(struct qib_pportdata * ppd,int ibup,u64 ibcs)2995*4882a593Smuzhiyun static int qib_6120_ib_updown(struct qib_pportdata *ppd, int ibup, u64 ibcs)
2996*4882a593Smuzhiyun {
2997*4882a593Smuzhiyun 	unsigned long flags;
2998*4882a593Smuzhiyun 
2999*4882a593Smuzhiyun 	spin_lock_irqsave(&ppd->lflags_lock, flags);
3000*4882a593Smuzhiyun 	ppd->lflags &= ~QIBL_IB_FORCE_NOTIFY;
3001*4882a593Smuzhiyun 	spin_unlock_irqrestore(&ppd->lflags_lock, flags);
3002*4882a593Smuzhiyun 
3003*4882a593Smuzhiyun 	if (ibup) {
3004*4882a593Smuzhiyun 		if (ppd->dd->cspec->ibdeltainprog) {
3005*4882a593Smuzhiyun 			ppd->dd->cspec->ibdeltainprog = 0;
3006*4882a593Smuzhiyun 			ppd->dd->cspec->ibsymdelta +=
3007*4882a593Smuzhiyun 				read_6120_creg32(ppd->dd, cr_ibsymbolerr) -
3008*4882a593Smuzhiyun 					ppd->dd->cspec->ibsymsnap;
3009*4882a593Smuzhiyun 			ppd->dd->cspec->iblnkerrdelta +=
3010*4882a593Smuzhiyun 				read_6120_creg32(ppd->dd, cr_iblinkerrrecov) -
3011*4882a593Smuzhiyun 					ppd->dd->cspec->iblnkerrsnap;
3012*4882a593Smuzhiyun 		}
3013*4882a593Smuzhiyun 		qib_hol_init(ppd);
3014*4882a593Smuzhiyun 	} else {
3015*4882a593Smuzhiyun 		ppd->dd->cspec->lli_counter = 0;
3016*4882a593Smuzhiyun 		if (!ppd->dd->cspec->ibdeltainprog) {
3017*4882a593Smuzhiyun 			ppd->dd->cspec->ibdeltainprog = 1;
3018*4882a593Smuzhiyun 			ppd->dd->cspec->ibsymsnap =
3019*4882a593Smuzhiyun 				read_6120_creg32(ppd->dd, cr_ibsymbolerr);
3020*4882a593Smuzhiyun 			ppd->dd->cspec->iblnkerrsnap =
3021*4882a593Smuzhiyun 				read_6120_creg32(ppd->dd, cr_iblinkerrrecov);
3022*4882a593Smuzhiyun 		}
3023*4882a593Smuzhiyun 		qib_hol_down(ppd);
3024*4882a593Smuzhiyun 	}
3025*4882a593Smuzhiyun 
3026*4882a593Smuzhiyun 	qib_6120_setup_setextled(ppd, ibup);
3027*4882a593Smuzhiyun 
3028*4882a593Smuzhiyun 	return 0;
3029*4882a593Smuzhiyun }
3030*4882a593Smuzhiyun 
3031*4882a593Smuzhiyun /* Does read/modify/write to appropriate registers to
3032*4882a593Smuzhiyun  * set output and direction bits selected by mask.
3033*4882a593Smuzhiyun  * these are in their canonical postions (e.g. lsb of
3034*4882a593Smuzhiyun  * dir will end up in D48 of extctrl on existing chips).
3035*4882a593Smuzhiyun  * returns contents of GP Inputs.
3036*4882a593Smuzhiyun  */
gpio_6120_mod(struct qib_devdata * dd,u32 out,u32 dir,u32 mask)3037*4882a593Smuzhiyun static int gpio_6120_mod(struct qib_devdata *dd, u32 out, u32 dir, u32 mask)
3038*4882a593Smuzhiyun {
3039*4882a593Smuzhiyun 	u64 read_val, new_out;
3040*4882a593Smuzhiyun 	unsigned long flags;
3041*4882a593Smuzhiyun 
3042*4882a593Smuzhiyun 	if (mask) {
3043*4882a593Smuzhiyun 		/* some bits being written, lock access to GPIO */
3044*4882a593Smuzhiyun 		dir &= mask;
3045*4882a593Smuzhiyun 		out &= mask;
3046*4882a593Smuzhiyun 		spin_lock_irqsave(&dd->cspec->gpio_lock, flags);
3047*4882a593Smuzhiyun 		dd->cspec->extctrl &= ~((u64)mask << SYM_LSB(EXTCtrl, GPIOOe));
3048*4882a593Smuzhiyun 		dd->cspec->extctrl |= ((u64) dir << SYM_LSB(EXTCtrl, GPIOOe));
3049*4882a593Smuzhiyun 		new_out = (dd->cspec->gpio_out & ~mask) | out;
3050*4882a593Smuzhiyun 
3051*4882a593Smuzhiyun 		qib_write_kreg(dd, kr_extctrl, dd->cspec->extctrl);
3052*4882a593Smuzhiyun 		qib_write_kreg(dd, kr_gpio_out, new_out);
3053*4882a593Smuzhiyun 		dd->cspec->gpio_out = new_out;
3054*4882a593Smuzhiyun 		spin_unlock_irqrestore(&dd->cspec->gpio_lock, flags);
3055*4882a593Smuzhiyun 	}
3056*4882a593Smuzhiyun 	/*
3057*4882a593Smuzhiyun 	 * It is unlikely that a read at this time would get valid
3058*4882a593Smuzhiyun 	 * data on a pin whose direction line was set in the same
3059*4882a593Smuzhiyun 	 * call to this function. We include the read here because
3060*4882a593Smuzhiyun 	 * that allows us to potentially combine a change on one pin with
3061*4882a593Smuzhiyun 	 * a read on another, and because the old code did something like
3062*4882a593Smuzhiyun 	 * this.
3063*4882a593Smuzhiyun 	 */
3064*4882a593Smuzhiyun 	read_val = qib_read_kreg64(dd, kr_extstatus);
3065*4882a593Smuzhiyun 	return SYM_FIELD(read_val, EXTStatus, GPIOIn);
3066*4882a593Smuzhiyun }
3067*4882a593Smuzhiyun 
3068*4882a593Smuzhiyun /*
3069*4882a593Smuzhiyun  * Read fundamental info we need to use the chip.  These are
3070*4882a593Smuzhiyun  * the registers that describe chip capabilities, and are
3071*4882a593Smuzhiyun  * saved in shadow registers.
3072*4882a593Smuzhiyun  */
get_6120_chip_params(struct qib_devdata * dd)3073*4882a593Smuzhiyun static void get_6120_chip_params(struct qib_devdata *dd)
3074*4882a593Smuzhiyun {
3075*4882a593Smuzhiyun 	u64 val;
3076*4882a593Smuzhiyun 	u32 piobufs;
3077*4882a593Smuzhiyun 	int mtu;
3078*4882a593Smuzhiyun 
3079*4882a593Smuzhiyun 	dd->uregbase = qib_read_kreg32(dd, kr_userregbase);
3080*4882a593Smuzhiyun 
3081*4882a593Smuzhiyun 	dd->rcvtidcnt = qib_read_kreg32(dd, kr_rcvtidcnt);
3082*4882a593Smuzhiyun 	dd->rcvtidbase = qib_read_kreg32(dd, kr_rcvtidbase);
3083*4882a593Smuzhiyun 	dd->rcvegrbase = qib_read_kreg32(dd, kr_rcvegrbase);
3084*4882a593Smuzhiyun 	dd->palign = qib_read_kreg32(dd, kr_palign);
3085*4882a593Smuzhiyun 	dd->piobufbase = qib_read_kreg64(dd, kr_sendpiobufbase);
3086*4882a593Smuzhiyun 	dd->pio2k_bufbase = dd->piobufbase & 0xffffffff;
3087*4882a593Smuzhiyun 
3088*4882a593Smuzhiyun 	dd->rcvhdrcnt = qib_read_kreg32(dd, kr_rcvegrcnt);
3089*4882a593Smuzhiyun 
3090*4882a593Smuzhiyun 	val = qib_read_kreg64(dd, kr_sendpiosize);
3091*4882a593Smuzhiyun 	dd->piosize2k = val & ~0U;
3092*4882a593Smuzhiyun 	dd->piosize4k = val >> 32;
3093*4882a593Smuzhiyun 
3094*4882a593Smuzhiyun 	mtu = ib_mtu_enum_to_int(qib_ibmtu);
3095*4882a593Smuzhiyun 	if (mtu == -1)
3096*4882a593Smuzhiyun 		mtu = QIB_DEFAULT_MTU;
3097*4882a593Smuzhiyun 	dd->pport->ibmtu = (u32)mtu;
3098*4882a593Smuzhiyun 
3099*4882a593Smuzhiyun 	val = qib_read_kreg64(dd, kr_sendpiobufcnt);
3100*4882a593Smuzhiyun 	dd->piobcnt2k = val & ~0U;
3101*4882a593Smuzhiyun 	dd->piobcnt4k = val >> 32;
3102*4882a593Smuzhiyun 	dd->last_pio = dd->piobcnt4k + dd->piobcnt2k - 1;
3103*4882a593Smuzhiyun 	/* these may be adjusted in init_chip_wc_pat() */
3104*4882a593Smuzhiyun 	dd->pio2kbase = (u32 __iomem *)
3105*4882a593Smuzhiyun 		(((char __iomem *)dd->kregbase) + dd->pio2k_bufbase);
3106*4882a593Smuzhiyun 	if (dd->piobcnt4k) {
3107*4882a593Smuzhiyun 		dd->pio4kbase = (u32 __iomem *)
3108*4882a593Smuzhiyun 			(((char __iomem *) dd->kregbase) +
3109*4882a593Smuzhiyun 			 (dd->piobufbase >> 32));
3110*4882a593Smuzhiyun 		/*
3111*4882a593Smuzhiyun 		 * 4K buffers take 2 pages; we use roundup just to be
3112*4882a593Smuzhiyun 		 * paranoid; we calculate it once here, rather than on
3113*4882a593Smuzhiyun 		 * ever buf allocate
3114*4882a593Smuzhiyun 		 */
3115*4882a593Smuzhiyun 		dd->align4k = ALIGN(dd->piosize4k, dd->palign);
3116*4882a593Smuzhiyun 	}
3117*4882a593Smuzhiyun 
3118*4882a593Smuzhiyun 	piobufs = dd->piobcnt4k + dd->piobcnt2k;
3119*4882a593Smuzhiyun 
3120*4882a593Smuzhiyun 	dd->pioavregs = ALIGN(piobufs, sizeof(u64) * BITS_PER_BYTE / 2) /
3121*4882a593Smuzhiyun 		(sizeof(u64) * BITS_PER_BYTE / 2);
3122*4882a593Smuzhiyun }
3123*4882a593Smuzhiyun 
3124*4882a593Smuzhiyun /*
3125*4882a593Smuzhiyun  * The chip base addresses in cspec and cpspec have to be set
3126*4882a593Smuzhiyun  * after possible init_chip_wc_pat(), rather than in
3127*4882a593Smuzhiyun  * get_6120_chip_params(), so split out as separate function
3128*4882a593Smuzhiyun  */
set_6120_baseaddrs(struct qib_devdata * dd)3129*4882a593Smuzhiyun static void set_6120_baseaddrs(struct qib_devdata *dd)
3130*4882a593Smuzhiyun {
3131*4882a593Smuzhiyun 	u32 cregbase;
3132*4882a593Smuzhiyun 
3133*4882a593Smuzhiyun 	cregbase = qib_read_kreg32(dd, kr_counterregbase);
3134*4882a593Smuzhiyun 	dd->cspec->cregbase = (u64 __iomem *)
3135*4882a593Smuzhiyun 		((char __iomem *) dd->kregbase + cregbase);
3136*4882a593Smuzhiyun 
3137*4882a593Smuzhiyun 	dd->egrtidbase = (u64 __iomem *)
3138*4882a593Smuzhiyun 		((char __iomem *) dd->kregbase + dd->rcvegrbase);
3139*4882a593Smuzhiyun }
3140*4882a593Smuzhiyun 
3141*4882a593Smuzhiyun /*
3142*4882a593Smuzhiyun  * Write the final few registers that depend on some of the
3143*4882a593Smuzhiyun  * init setup.  Done late in init, just before bringing up
3144*4882a593Smuzhiyun  * the serdes.
3145*4882a593Smuzhiyun  */
qib_late_6120_initreg(struct qib_devdata * dd)3146*4882a593Smuzhiyun static int qib_late_6120_initreg(struct qib_devdata *dd)
3147*4882a593Smuzhiyun {
3148*4882a593Smuzhiyun 	int ret = 0;
3149*4882a593Smuzhiyun 	u64 val;
3150*4882a593Smuzhiyun 
3151*4882a593Smuzhiyun 	qib_write_kreg(dd, kr_rcvhdrentsize, dd->rcvhdrentsize);
3152*4882a593Smuzhiyun 	qib_write_kreg(dd, kr_rcvhdrsize, dd->rcvhdrsize);
3153*4882a593Smuzhiyun 	qib_write_kreg(dd, kr_rcvhdrcnt, dd->rcvhdrcnt);
3154*4882a593Smuzhiyun 	qib_write_kreg(dd, kr_sendpioavailaddr, dd->pioavailregs_phys);
3155*4882a593Smuzhiyun 	val = qib_read_kreg64(dd, kr_sendpioavailaddr);
3156*4882a593Smuzhiyun 	if (val != dd->pioavailregs_phys) {
3157*4882a593Smuzhiyun 		qib_dev_err(dd,
3158*4882a593Smuzhiyun 			"Catastrophic software error, SendPIOAvailAddr written as %lx, read back as %llx\n",
3159*4882a593Smuzhiyun 			(unsigned long) dd->pioavailregs_phys,
3160*4882a593Smuzhiyun 			(unsigned long long) val);
3161*4882a593Smuzhiyun 		ret = -EINVAL;
3162*4882a593Smuzhiyun 	}
3163*4882a593Smuzhiyun 	return ret;
3164*4882a593Smuzhiyun }
3165*4882a593Smuzhiyun 
init_6120_variables(struct qib_devdata * dd)3166*4882a593Smuzhiyun static int init_6120_variables(struct qib_devdata *dd)
3167*4882a593Smuzhiyun {
3168*4882a593Smuzhiyun 	int ret = 0;
3169*4882a593Smuzhiyun 	struct qib_pportdata *ppd;
3170*4882a593Smuzhiyun 	u32 sbufs;
3171*4882a593Smuzhiyun 
3172*4882a593Smuzhiyun 	ppd = (struct qib_pportdata *)(dd + 1);
3173*4882a593Smuzhiyun 	dd->pport = ppd;
3174*4882a593Smuzhiyun 	dd->num_pports = 1;
3175*4882a593Smuzhiyun 
3176*4882a593Smuzhiyun 	dd->cspec = (struct qib_chip_specific *)(ppd + dd->num_pports);
3177*4882a593Smuzhiyun 	dd->cspec->ppd = ppd;
3178*4882a593Smuzhiyun 	ppd->cpspec = NULL; /* not used in this chip */
3179*4882a593Smuzhiyun 
3180*4882a593Smuzhiyun 	spin_lock_init(&dd->cspec->kernel_tid_lock);
3181*4882a593Smuzhiyun 	spin_lock_init(&dd->cspec->user_tid_lock);
3182*4882a593Smuzhiyun 	spin_lock_init(&dd->cspec->rcvmod_lock);
3183*4882a593Smuzhiyun 	spin_lock_init(&dd->cspec->gpio_lock);
3184*4882a593Smuzhiyun 
3185*4882a593Smuzhiyun 	/* we haven't yet set QIB_PRESENT, so use read directly */
3186*4882a593Smuzhiyun 	dd->revision = readq(&dd->kregbase[kr_revision]);
3187*4882a593Smuzhiyun 
3188*4882a593Smuzhiyun 	if ((dd->revision & 0xffffffffU) == 0xffffffffU) {
3189*4882a593Smuzhiyun 		qib_dev_err(dd,
3190*4882a593Smuzhiyun 			"Revision register read failure, giving up initialization\n");
3191*4882a593Smuzhiyun 		ret = -ENODEV;
3192*4882a593Smuzhiyun 		goto bail;
3193*4882a593Smuzhiyun 	}
3194*4882a593Smuzhiyun 	dd->flags |= QIB_PRESENT;  /* now register routines work */
3195*4882a593Smuzhiyun 
3196*4882a593Smuzhiyun 	dd->majrev = (u8) SYM_FIELD(dd->revision, Revision_R,
3197*4882a593Smuzhiyun 				    ChipRevMajor);
3198*4882a593Smuzhiyun 	dd->minrev = (u8) SYM_FIELD(dd->revision, Revision_R,
3199*4882a593Smuzhiyun 				    ChipRevMinor);
3200*4882a593Smuzhiyun 
3201*4882a593Smuzhiyun 	get_6120_chip_params(dd);
3202*4882a593Smuzhiyun 	pe_boardname(dd); /* fill in boardname */
3203*4882a593Smuzhiyun 
3204*4882a593Smuzhiyun 	/*
3205*4882a593Smuzhiyun 	 * GPIO bits for TWSI data and clock,
3206*4882a593Smuzhiyun 	 * used for serial EEPROM.
3207*4882a593Smuzhiyun 	 */
3208*4882a593Smuzhiyun 	dd->gpio_sda_num = _QIB_GPIO_SDA_NUM;
3209*4882a593Smuzhiyun 	dd->gpio_scl_num = _QIB_GPIO_SCL_NUM;
3210*4882a593Smuzhiyun 	dd->twsi_eeprom_dev = QIB_TWSI_NO_DEV;
3211*4882a593Smuzhiyun 
3212*4882a593Smuzhiyun 	if (qib_unordered_wc())
3213*4882a593Smuzhiyun 		dd->flags |= QIB_PIO_FLUSH_WC;
3214*4882a593Smuzhiyun 
3215*4882a593Smuzhiyun 	ret = qib_init_pportdata(ppd, dd, 0, 1);
3216*4882a593Smuzhiyun 	if (ret)
3217*4882a593Smuzhiyun 		goto bail;
3218*4882a593Smuzhiyun 	ppd->link_width_supported = IB_WIDTH_1X | IB_WIDTH_4X;
3219*4882a593Smuzhiyun 	ppd->link_speed_supported = QIB_IB_SDR;
3220*4882a593Smuzhiyun 	ppd->link_width_enabled = IB_WIDTH_4X;
3221*4882a593Smuzhiyun 	ppd->link_speed_enabled = ppd->link_speed_supported;
3222*4882a593Smuzhiyun 	/* these can't change for this chip, so set once */
3223*4882a593Smuzhiyun 	ppd->link_width_active = ppd->link_width_enabled;
3224*4882a593Smuzhiyun 	ppd->link_speed_active = ppd->link_speed_enabled;
3225*4882a593Smuzhiyun 	ppd->vls_supported = IB_VL_VL0;
3226*4882a593Smuzhiyun 	ppd->vls_operational = ppd->vls_supported;
3227*4882a593Smuzhiyun 
3228*4882a593Smuzhiyun 	dd->rcvhdrentsize = QIB_RCVHDR_ENTSIZE;
3229*4882a593Smuzhiyun 	dd->rcvhdrsize = QIB_DFLT_RCVHDRSIZE;
3230*4882a593Smuzhiyun 	dd->rhf_offset = 0;
3231*4882a593Smuzhiyun 
3232*4882a593Smuzhiyun 	/* we always allocate at least 2048 bytes for eager buffers */
3233*4882a593Smuzhiyun 	ret = ib_mtu_enum_to_int(qib_ibmtu);
3234*4882a593Smuzhiyun 	dd->rcvegrbufsize = ret != -1 ? max(ret, 2048) : QIB_DEFAULT_MTU;
3235*4882a593Smuzhiyun 	dd->rcvegrbufsize_shift = ilog2(dd->rcvegrbufsize);
3236*4882a593Smuzhiyun 
3237*4882a593Smuzhiyun 	qib_6120_tidtemplate(dd);
3238*4882a593Smuzhiyun 
3239*4882a593Smuzhiyun 	/*
3240*4882a593Smuzhiyun 	 * We can request a receive interrupt for 1 or
3241*4882a593Smuzhiyun 	 * more packets from current offset.  For now, we set this
3242*4882a593Smuzhiyun 	 * up for a single packet.
3243*4882a593Smuzhiyun 	 */
3244*4882a593Smuzhiyun 	dd->rhdrhead_intr_off = 1ULL << 32;
3245*4882a593Smuzhiyun 
3246*4882a593Smuzhiyun 	/* setup the stats timer; the add_timer is done at end of init */
3247*4882a593Smuzhiyun 	timer_setup(&dd->stats_timer, qib_get_6120_faststats, 0);
3248*4882a593Smuzhiyun 	timer_setup(&dd->cspec->pma_timer, pma_6120_timer, 0);
3249*4882a593Smuzhiyun 
3250*4882a593Smuzhiyun 	dd->ureg_align = qib_read_kreg32(dd, kr_palign);
3251*4882a593Smuzhiyun 
3252*4882a593Smuzhiyun 	dd->piosize2kmax_dwords = dd->piosize2k >> 2;
3253*4882a593Smuzhiyun 	qib_6120_config_ctxts(dd);
3254*4882a593Smuzhiyun 	qib_set_ctxtcnt(dd);
3255*4882a593Smuzhiyun 
3256*4882a593Smuzhiyun 	ret = init_chip_wc_pat(dd, 0);
3257*4882a593Smuzhiyun 	if (ret)
3258*4882a593Smuzhiyun 		goto bail;
3259*4882a593Smuzhiyun 	set_6120_baseaddrs(dd); /* set chip access pointers now */
3260*4882a593Smuzhiyun 
3261*4882a593Smuzhiyun 	ret = 0;
3262*4882a593Smuzhiyun 	if (qib_mini_init)
3263*4882a593Smuzhiyun 		goto bail;
3264*4882a593Smuzhiyun 
3265*4882a593Smuzhiyun 	qib_num_cfg_vls = 1; /* if any 6120's, only one VL */
3266*4882a593Smuzhiyun 
3267*4882a593Smuzhiyun 	ret = qib_create_ctxts(dd);
3268*4882a593Smuzhiyun 	init_6120_cntrnames(dd);
3269*4882a593Smuzhiyun 
3270*4882a593Smuzhiyun 	/* use all of 4KB buffers for the kernel, otherwise 16 */
3271*4882a593Smuzhiyun 	sbufs = dd->piobcnt4k ?  dd->piobcnt4k : 16;
3272*4882a593Smuzhiyun 
3273*4882a593Smuzhiyun 	dd->lastctxt_piobuf = dd->piobcnt2k + dd->piobcnt4k - sbufs;
3274*4882a593Smuzhiyun 	dd->pbufsctxt = dd->lastctxt_piobuf /
3275*4882a593Smuzhiyun 		(dd->cfgctxts - dd->first_user_ctxt);
3276*4882a593Smuzhiyun 
3277*4882a593Smuzhiyun 	if (ret)
3278*4882a593Smuzhiyun 		goto bail;
3279*4882a593Smuzhiyun bail:
3280*4882a593Smuzhiyun 	return ret;
3281*4882a593Smuzhiyun }
3282*4882a593Smuzhiyun 
3283*4882a593Smuzhiyun /*
3284*4882a593Smuzhiyun  * For this chip, we want to use the same buffer every time
3285*4882a593Smuzhiyun  * when we are trying to bring the link up (they are always VL15
3286*4882a593Smuzhiyun  * packets).  At that link state the packet should always go out immediately
3287*4882a593Smuzhiyun  * (or at least be discarded at the tx interface if the link is down).
3288*4882a593Smuzhiyun  * If it doesn't, and the buffer isn't available, that means some other
3289*4882a593Smuzhiyun  * sender has gotten ahead of us, and is preventing our packet from going
3290*4882a593Smuzhiyun  * out.  In that case, we flush all packets, and try again.  If that still
3291*4882a593Smuzhiyun  * fails, we fail the request, and hope things work the next time around.
3292*4882a593Smuzhiyun  *
3293*4882a593Smuzhiyun  * We don't need very complicated heuristics on whether the packet had
3294*4882a593Smuzhiyun  * time to go out or not, since even at SDR 1X, it goes out in very short
3295*4882a593Smuzhiyun  * time periods, covered by the chip reads done here and as part of the
3296*4882a593Smuzhiyun  * flush.
3297*4882a593Smuzhiyun  */
get_6120_link_buf(struct qib_pportdata * ppd,u32 * bnum)3298*4882a593Smuzhiyun static u32 __iomem *get_6120_link_buf(struct qib_pportdata *ppd, u32 *bnum)
3299*4882a593Smuzhiyun {
3300*4882a593Smuzhiyun 	u32 __iomem *buf;
3301*4882a593Smuzhiyun 	u32 lbuf = ppd->dd->piobcnt2k + ppd->dd->piobcnt4k - 1;
3302*4882a593Smuzhiyun 
3303*4882a593Smuzhiyun 	/*
3304*4882a593Smuzhiyun 	 * always blip to get avail list updated, since it's almost
3305*4882a593Smuzhiyun 	 * always needed, and is fairly cheap.
3306*4882a593Smuzhiyun 	 */
3307*4882a593Smuzhiyun 	sendctrl_6120_mod(ppd->dd->pport, QIB_SENDCTRL_AVAIL_BLIP);
3308*4882a593Smuzhiyun 	qib_read_kreg64(ppd->dd, kr_scratch); /* extra chip flush */
3309*4882a593Smuzhiyun 	buf = qib_getsendbuf_range(ppd->dd, bnum, lbuf, lbuf);
3310*4882a593Smuzhiyun 	if (buf)
3311*4882a593Smuzhiyun 		goto done;
3312*4882a593Smuzhiyun 
3313*4882a593Smuzhiyun 	sendctrl_6120_mod(ppd, QIB_SENDCTRL_DISARM_ALL | QIB_SENDCTRL_FLUSH |
3314*4882a593Smuzhiyun 			  QIB_SENDCTRL_AVAIL_BLIP);
3315*4882a593Smuzhiyun 	ppd->dd->upd_pio_shadow  = 1; /* update our idea of what's busy */
3316*4882a593Smuzhiyun 	qib_read_kreg64(ppd->dd, kr_scratch); /* extra chip flush */
3317*4882a593Smuzhiyun 	buf = qib_getsendbuf_range(ppd->dd, bnum, lbuf, lbuf);
3318*4882a593Smuzhiyun done:
3319*4882a593Smuzhiyun 	return buf;
3320*4882a593Smuzhiyun }
3321*4882a593Smuzhiyun 
qib_6120_getsendbuf(struct qib_pportdata * ppd,u64 pbc,u32 * pbufnum)3322*4882a593Smuzhiyun static u32 __iomem *qib_6120_getsendbuf(struct qib_pportdata *ppd, u64 pbc,
3323*4882a593Smuzhiyun 					u32 *pbufnum)
3324*4882a593Smuzhiyun {
3325*4882a593Smuzhiyun 	u32 first, last, plen = pbc & QIB_PBC_LENGTH_MASK;
3326*4882a593Smuzhiyun 	struct qib_devdata *dd = ppd->dd;
3327*4882a593Smuzhiyun 	u32 __iomem *buf;
3328*4882a593Smuzhiyun 
3329*4882a593Smuzhiyun 	if (((pbc >> 32) & PBC_6120_VL15_SEND_CTRL) &&
3330*4882a593Smuzhiyun 		!(ppd->lflags & (QIBL_IB_AUTONEG_INPROG | QIBL_LINKACTIVE)))
3331*4882a593Smuzhiyun 		buf = get_6120_link_buf(ppd, pbufnum);
3332*4882a593Smuzhiyun 	else {
3333*4882a593Smuzhiyun 
3334*4882a593Smuzhiyun 		if ((plen + 1) > dd->piosize2kmax_dwords)
3335*4882a593Smuzhiyun 			first = dd->piobcnt2k;
3336*4882a593Smuzhiyun 		else
3337*4882a593Smuzhiyun 			first = 0;
3338*4882a593Smuzhiyun 		/* try 4k if all 2k busy, so same last for both sizes */
3339*4882a593Smuzhiyun 		last = dd->piobcnt2k + dd->piobcnt4k - 1;
3340*4882a593Smuzhiyun 		buf = qib_getsendbuf_range(dd, pbufnum, first, last);
3341*4882a593Smuzhiyun 	}
3342*4882a593Smuzhiyun 	return buf;
3343*4882a593Smuzhiyun }
3344*4882a593Smuzhiyun 
init_sdma_6120_regs(struct qib_pportdata * ppd)3345*4882a593Smuzhiyun static int init_sdma_6120_regs(struct qib_pportdata *ppd)
3346*4882a593Smuzhiyun {
3347*4882a593Smuzhiyun 	return -ENODEV;
3348*4882a593Smuzhiyun }
3349*4882a593Smuzhiyun 
qib_sdma_6120_gethead(struct qib_pportdata * ppd)3350*4882a593Smuzhiyun static u16 qib_sdma_6120_gethead(struct qib_pportdata *ppd)
3351*4882a593Smuzhiyun {
3352*4882a593Smuzhiyun 	return 0;
3353*4882a593Smuzhiyun }
3354*4882a593Smuzhiyun 
qib_sdma_6120_busy(struct qib_pportdata * ppd)3355*4882a593Smuzhiyun static int qib_sdma_6120_busy(struct qib_pportdata *ppd)
3356*4882a593Smuzhiyun {
3357*4882a593Smuzhiyun 	return 0;
3358*4882a593Smuzhiyun }
3359*4882a593Smuzhiyun 
qib_sdma_update_6120_tail(struct qib_pportdata * ppd,u16 tail)3360*4882a593Smuzhiyun static void qib_sdma_update_6120_tail(struct qib_pportdata *ppd, u16 tail)
3361*4882a593Smuzhiyun {
3362*4882a593Smuzhiyun }
3363*4882a593Smuzhiyun 
qib_6120_sdma_sendctrl(struct qib_pportdata * ppd,unsigned op)3364*4882a593Smuzhiyun static void qib_6120_sdma_sendctrl(struct qib_pportdata *ppd, unsigned op)
3365*4882a593Smuzhiyun {
3366*4882a593Smuzhiyun }
3367*4882a593Smuzhiyun 
qib_sdma_set_6120_desc_cnt(struct qib_pportdata * ppd,unsigned cnt)3368*4882a593Smuzhiyun static void qib_sdma_set_6120_desc_cnt(struct qib_pportdata *ppd, unsigned cnt)
3369*4882a593Smuzhiyun {
3370*4882a593Smuzhiyun }
3371*4882a593Smuzhiyun 
3372*4882a593Smuzhiyun /*
3373*4882a593Smuzhiyun  * the pbc doesn't need a VL15 indicator, but we need it for link_buf.
3374*4882a593Smuzhiyun  * The chip ignores the bit if set.
3375*4882a593Smuzhiyun  */
qib_6120_setpbc_control(struct qib_pportdata * ppd,u32 plen,u8 srate,u8 vl)3376*4882a593Smuzhiyun static u32 qib_6120_setpbc_control(struct qib_pportdata *ppd, u32 plen,
3377*4882a593Smuzhiyun 				   u8 srate, u8 vl)
3378*4882a593Smuzhiyun {
3379*4882a593Smuzhiyun 	return vl == 15 ? PBC_6120_VL15_SEND_CTRL : 0;
3380*4882a593Smuzhiyun }
3381*4882a593Smuzhiyun 
qib_6120_initvl15_bufs(struct qib_devdata * dd)3382*4882a593Smuzhiyun static void qib_6120_initvl15_bufs(struct qib_devdata *dd)
3383*4882a593Smuzhiyun {
3384*4882a593Smuzhiyun }
3385*4882a593Smuzhiyun 
qib_6120_init_ctxt(struct qib_ctxtdata * rcd)3386*4882a593Smuzhiyun static void qib_6120_init_ctxt(struct qib_ctxtdata *rcd)
3387*4882a593Smuzhiyun {
3388*4882a593Smuzhiyun 	rcd->rcvegrcnt = rcd->dd->rcvhdrcnt;
3389*4882a593Smuzhiyun 	rcd->rcvegr_tid_base = rcd->ctxt * rcd->rcvegrcnt;
3390*4882a593Smuzhiyun }
3391*4882a593Smuzhiyun 
qib_6120_txchk_change(struct qib_devdata * dd,u32 start,u32 len,u32 avail,struct qib_ctxtdata * rcd)3392*4882a593Smuzhiyun static void qib_6120_txchk_change(struct qib_devdata *dd, u32 start,
3393*4882a593Smuzhiyun 	u32 len, u32 avail, struct qib_ctxtdata *rcd)
3394*4882a593Smuzhiyun {
3395*4882a593Smuzhiyun }
3396*4882a593Smuzhiyun 
writescratch(struct qib_devdata * dd,u32 val)3397*4882a593Smuzhiyun static void writescratch(struct qib_devdata *dd, u32 val)
3398*4882a593Smuzhiyun {
3399*4882a593Smuzhiyun 	(void) qib_write_kreg(dd, kr_scratch, val);
3400*4882a593Smuzhiyun }
3401*4882a593Smuzhiyun 
qib_6120_tempsense_rd(struct qib_devdata * dd,int regnum)3402*4882a593Smuzhiyun static int qib_6120_tempsense_rd(struct qib_devdata *dd, int regnum)
3403*4882a593Smuzhiyun {
3404*4882a593Smuzhiyun 	return -ENXIO;
3405*4882a593Smuzhiyun }
3406*4882a593Smuzhiyun 
3407*4882a593Smuzhiyun #ifdef CONFIG_INFINIBAND_QIB_DCA
qib_6120_notify_dca(struct qib_devdata * dd,unsigned long event)3408*4882a593Smuzhiyun static int qib_6120_notify_dca(struct qib_devdata *dd, unsigned long event)
3409*4882a593Smuzhiyun {
3410*4882a593Smuzhiyun 	return 0;
3411*4882a593Smuzhiyun }
3412*4882a593Smuzhiyun #endif
3413*4882a593Smuzhiyun 
3414*4882a593Smuzhiyun /* Dummy function, as 6120 boards never disable EEPROM Write */
qib_6120_eeprom_wen(struct qib_devdata * dd,int wen)3415*4882a593Smuzhiyun static int qib_6120_eeprom_wen(struct qib_devdata *dd, int wen)
3416*4882a593Smuzhiyun {
3417*4882a593Smuzhiyun 	return 1;
3418*4882a593Smuzhiyun }
3419*4882a593Smuzhiyun 
3420*4882a593Smuzhiyun /**
3421*4882a593Smuzhiyun  * qib_init_iba6120_funcs - set up the chip-specific function pointers
3422*4882a593Smuzhiyun  * @pdev: pci_dev of the qlogic_ib device
3423*4882a593Smuzhiyun  * @ent: pci_device_id matching this chip
3424*4882a593Smuzhiyun  *
3425*4882a593Smuzhiyun  * This is global, and is called directly at init to set up the
3426*4882a593Smuzhiyun  * chip-specific function pointers for later use.
3427*4882a593Smuzhiyun  *
3428*4882a593Smuzhiyun  * It also allocates/partially-inits the qib_devdata struct for
3429*4882a593Smuzhiyun  * this device.
3430*4882a593Smuzhiyun  */
qib_init_iba6120_funcs(struct pci_dev * pdev,const struct pci_device_id * ent)3431*4882a593Smuzhiyun struct qib_devdata *qib_init_iba6120_funcs(struct pci_dev *pdev,
3432*4882a593Smuzhiyun 					   const struct pci_device_id *ent)
3433*4882a593Smuzhiyun {
3434*4882a593Smuzhiyun 	struct qib_devdata *dd;
3435*4882a593Smuzhiyun 	int ret;
3436*4882a593Smuzhiyun 
3437*4882a593Smuzhiyun 	dd = qib_alloc_devdata(pdev, sizeof(struct qib_pportdata) +
3438*4882a593Smuzhiyun 			       sizeof(struct qib_chip_specific));
3439*4882a593Smuzhiyun 	if (IS_ERR(dd))
3440*4882a593Smuzhiyun 		goto bail;
3441*4882a593Smuzhiyun 
3442*4882a593Smuzhiyun 	dd->f_bringup_serdes    = qib_6120_bringup_serdes;
3443*4882a593Smuzhiyun 	dd->f_cleanup           = qib_6120_setup_cleanup;
3444*4882a593Smuzhiyun 	dd->f_clear_tids        = qib_6120_clear_tids;
3445*4882a593Smuzhiyun 	dd->f_free_irq          = qib_free_irq;
3446*4882a593Smuzhiyun 	dd->f_get_base_info     = qib_6120_get_base_info;
3447*4882a593Smuzhiyun 	dd->f_get_msgheader     = qib_6120_get_msgheader;
3448*4882a593Smuzhiyun 	dd->f_getsendbuf        = qib_6120_getsendbuf;
3449*4882a593Smuzhiyun 	dd->f_gpio_mod          = gpio_6120_mod;
3450*4882a593Smuzhiyun 	dd->f_eeprom_wen	= qib_6120_eeprom_wen;
3451*4882a593Smuzhiyun 	dd->f_hdrqempty         = qib_6120_hdrqempty;
3452*4882a593Smuzhiyun 	dd->f_ib_updown         = qib_6120_ib_updown;
3453*4882a593Smuzhiyun 	dd->f_init_ctxt         = qib_6120_init_ctxt;
3454*4882a593Smuzhiyun 	dd->f_initvl15_bufs     = qib_6120_initvl15_bufs;
3455*4882a593Smuzhiyun 	dd->f_intr_fallback     = qib_6120_nointr_fallback;
3456*4882a593Smuzhiyun 	dd->f_late_initreg      = qib_late_6120_initreg;
3457*4882a593Smuzhiyun 	dd->f_setpbc_control    = qib_6120_setpbc_control;
3458*4882a593Smuzhiyun 	dd->f_portcntr          = qib_portcntr_6120;
3459*4882a593Smuzhiyun 	dd->f_put_tid           = (dd->minrev >= 2) ?
3460*4882a593Smuzhiyun 				      qib_6120_put_tid_2 :
3461*4882a593Smuzhiyun 				      qib_6120_put_tid;
3462*4882a593Smuzhiyun 	dd->f_quiet_serdes      = qib_6120_quiet_serdes;
3463*4882a593Smuzhiyun 	dd->f_rcvctrl           = rcvctrl_6120_mod;
3464*4882a593Smuzhiyun 	dd->f_read_cntrs        = qib_read_6120cntrs;
3465*4882a593Smuzhiyun 	dd->f_read_portcntrs    = qib_read_6120portcntrs;
3466*4882a593Smuzhiyun 	dd->f_reset             = qib_6120_setup_reset;
3467*4882a593Smuzhiyun 	dd->f_init_sdma_regs    = init_sdma_6120_regs;
3468*4882a593Smuzhiyun 	dd->f_sdma_busy         = qib_sdma_6120_busy;
3469*4882a593Smuzhiyun 	dd->f_sdma_gethead      = qib_sdma_6120_gethead;
3470*4882a593Smuzhiyun 	dd->f_sdma_sendctrl     = qib_6120_sdma_sendctrl;
3471*4882a593Smuzhiyun 	dd->f_sdma_set_desc_cnt = qib_sdma_set_6120_desc_cnt;
3472*4882a593Smuzhiyun 	dd->f_sdma_update_tail  = qib_sdma_update_6120_tail;
3473*4882a593Smuzhiyun 	dd->f_sendctrl          = sendctrl_6120_mod;
3474*4882a593Smuzhiyun 	dd->f_set_armlaunch     = qib_set_6120_armlaunch;
3475*4882a593Smuzhiyun 	dd->f_set_cntr_sample   = qib_set_cntr_6120_sample;
3476*4882a593Smuzhiyun 	dd->f_iblink_state      = qib_6120_iblink_state;
3477*4882a593Smuzhiyun 	dd->f_ibphys_portstate  = qib_6120_phys_portstate;
3478*4882a593Smuzhiyun 	dd->f_get_ib_cfg        = qib_6120_get_ib_cfg;
3479*4882a593Smuzhiyun 	dd->f_set_ib_cfg        = qib_6120_set_ib_cfg;
3480*4882a593Smuzhiyun 	dd->f_set_ib_loopback   = qib_6120_set_loopback;
3481*4882a593Smuzhiyun 	dd->f_set_intr_state    = qib_6120_set_intr_state;
3482*4882a593Smuzhiyun 	dd->f_setextled         = qib_6120_setup_setextled;
3483*4882a593Smuzhiyun 	dd->f_txchk_change      = qib_6120_txchk_change;
3484*4882a593Smuzhiyun 	dd->f_update_usrhead    = qib_update_6120_usrhead;
3485*4882a593Smuzhiyun 	dd->f_wantpiobuf_intr   = qib_wantpiobuf_6120_intr;
3486*4882a593Smuzhiyun 	dd->f_xgxs_reset        = qib_6120_xgxs_reset;
3487*4882a593Smuzhiyun 	dd->f_writescratch      = writescratch;
3488*4882a593Smuzhiyun 	dd->f_tempsense_rd	= qib_6120_tempsense_rd;
3489*4882a593Smuzhiyun #ifdef CONFIG_INFINIBAND_QIB_DCA
3490*4882a593Smuzhiyun 	dd->f_notify_dca = qib_6120_notify_dca;
3491*4882a593Smuzhiyun #endif
3492*4882a593Smuzhiyun 	/*
3493*4882a593Smuzhiyun 	 * Do remaining pcie setup and save pcie values in dd.
3494*4882a593Smuzhiyun 	 * Any error printing is already done by the init code.
3495*4882a593Smuzhiyun 	 * On return, we have the chip mapped and accessible,
3496*4882a593Smuzhiyun 	 * but chip registers are not set up until start of
3497*4882a593Smuzhiyun 	 * init_6120_variables.
3498*4882a593Smuzhiyun 	 */
3499*4882a593Smuzhiyun 	ret = qib_pcie_ddinit(dd, pdev, ent);
3500*4882a593Smuzhiyun 	if (ret < 0)
3501*4882a593Smuzhiyun 		goto bail_free;
3502*4882a593Smuzhiyun 
3503*4882a593Smuzhiyun 	/* initialize chip-specific variables */
3504*4882a593Smuzhiyun 	ret = init_6120_variables(dd);
3505*4882a593Smuzhiyun 	if (ret)
3506*4882a593Smuzhiyun 		goto bail_cleanup;
3507*4882a593Smuzhiyun 
3508*4882a593Smuzhiyun 	if (qib_mini_init)
3509*4882a593Smuzhiyun 		goto bail;
3510*4882a593Smuzhiyun 
3511*4882a593Smuzhiyun 	if (qib_pcie_params(dd, 8, NULL))
3512*4882a593Smuzhiyun 		qib_dev_err(dd,
3513*4882a593Smuzhiyun 			"Failed to setup PCIe or interrupts; continuing anyway\n");
3514*4882a593Smuzhiyun 	/* clear diagctrl register, in case diags were running and crashed */
3515*4882a593Smuzhiyun 	qib_write_kreg(dd, kr_hwdiagctrl, 0);
3516*4882a593Smuzhiyun 
3517*4882a593Smuzhiyun 	if (qib_read_kreg64(dd, kr_hwerrstatus) &
3518*4882a593Smuzhiyun 	    QLOGIC_IB_HWE_SERDESPLLFAILED)
3519*4882a593Smuzhiyun 		qib_write_kreg(dd, kr_hwerrclear,
3520*4882a593Smuzhiyun 			       QLOGIC_IB_HWE_SERDESPLLFAILED);
3521*4882a593Smuzhiyun 
3522*4882a593Smuzhiyun 	/* setup interrupt handler (interrupt type handled above) */
3523*4882a593Smuzhiyun 	qib_setup_6120_interrupt(dd);
3524*4882a593Smuzhiyun 	/* Note that qpn_mask is set by qib_6120_config_ctxts() first */
3525*4882a593Smuzhiyun 	qib_6120_init_hwerrors(dd);
3526*4882a593Smuzhiyun 
3527*4882a593Smuzhiyun 	goto bail;
3528*4882a593Smuzhiyun 
3529*4882a593Smuzhiyun bail_cleanup:
3530*4882a593Smuzhiyun 	qib_pcie_ddcleanup(dd);
3531*4882a593Smuzhiyun bail_free:
3532*4882a593Smuzhiyun 	qib_free_devdata(dd);
3533*4882a593Smuzhiyun 	dd = ERR_PTR(ret);
3534*4882a593Smuzhiyun bail:
3535*4882a593Smuzhiyun 	return dd;
3536*4882a593Smuzhiyun }
3537