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