blob: 84b039f9e9b5a17b21aa05af2209b201dffde7d8 [file] [log] [blame]
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301/*
2 * QLogic iSCSI HBA Driver
Vikas Chaudhary7d01d062010-12-02 22:12:51 -08003 * Copyright (c) 2003-2010 QLogic Corporation
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05304 *
5 * See LICENSE.qla4xxx for copyright and licensing details.
6 */
7#include <linux/delay.h>
Jiri Slabya6751cc2010-09-14 14:12:54 +02008#include <linux/io.h>
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05309#include <linux/pci.h>
Tej Parkash068237c82012-05-18 04:41:44 -040010#include <linux/ratelimit.h>
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +053011#include "ql4_def.h"
12#include "ql4_glbl.h"
13
Hitoshi Mitake797a7962012-02-07 11:45:33 +090014#include <asm-generic/io-64-nonatomic-lo-hi.h>
15
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +053016#define MASK(n) DMA_BIT_MASK(n)
17#define MN_WIN(addr) (((addr & 0x1fc0000) >> 1) | ((addr >> 25) & 0x3ff))
18#define OCM_WIN(addr) (((addr & 0x1ff0000) >> 1) | ((addr >> 25) & 0x3ff))
19#define MS_WIN(addr) (addr & 0x0ffc0000)
20#define QLA82XX_PCI_MN_2M (0)
21#define QLA82XX_PCI_MS_2M (0x80000)
22#define QLA82XX_PCI_OCM0_2M (0xc0000)
23#define VALID_OCM_ADDR(addr) (((addr) & 0x3f800) != 0x3f800)
24#define GET_MEM_OFFS_2M(addr) (addr & MASK(18))
25
26/* CRB window related */
27#define CRB_BLK(off) ((off >> 20) & 0x3f)
28#define CRB_SUBBLK(off) ((off >> 16) & 0xf)
29#define CRB_WINDOW_2M (0x130060)
Vikas Chaudhary7664a1f2012-08-22 07:55:00 -040030#define CRB_HI(off) ((qla4_82xx_crb_hub_agt[CRB_BLK(off)] << 20) | \
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +053031 ((off) & 0xf0000))
32#define QLA82XX_PCI_CAMQM_2M_END (0x04800800UL)
33#define QLA82XX_PCI_CAMQM_2M_BASE (0x000ff800UL)
34#define CRB_INDIRECT_2M (0x1e0000UL)
35
36static inline void __iomem *
37qla4_8xxx_pci_base_offsetfset(struct scsi_qla_host *ha, unsigned long off)
38{
39 if ((off < ha->first_page_group_end) &&
40 (off >= ha->first_page_group_start))
41 return (void __iomem *)(ha->nx_pcibase + off);
42
43 return NULL;
44}
45
46#define MAX_CRB_XFORM 60
47static unsigned long crb_addr_xform[MAX_CRB_XFORM];
48static int qla4_8xxx_crb_table_initialized;
49
50#define qla4_8xxx_crb_addr_transform(name) \
51 (crb_addr_xform[QLA82XX_HW_PX_MAP_CRB_##name] = \
52 QLA82XX_HW_CRB_HUB_AGT_ADR_##name << 20)
53static void
Vikas Chaudharyf8086f42012-08-22 07:54:59 -040054qla4_82xx_crb_addr_transform_setup(void)
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +053055{
56 qla4_8xxx_crb_addr_transform(XDMA);
57 qla4_8xxx_crb_addr_transform(TIMR);
58 qla4_8xxx_crb_addr_transform(SRE);
59 qla4_8xxx_crb_addr_transform(SQN3);
60 qla4_8xxx_crb_addr_transform(SQN2);
61 qla4_8xxx_crb_addr_transform(SQN1);
62 qla4_8xxx_crb_addr_transform(SQN0);
63 qla4_8xxx_crb_addr_transform(SQS3);
64 qla4_8xxx_crb_addr_transform(SQS2);
65 qla4_8xxx_crb_addr_transform(SQS1);
66 qla4_8xxx_crb_addr_transform(SQS0);
67 qla4_8xxx_crb_addr_transform(RPMX7);
68 qla4_8xxx_crb_addr_transform(RPMX6);
69 qla4_8xxx_crb_addr_transform(RPMX5);
70 qla4_8xxx_crb_addr_transform(RPMX4);
71 qla4_8xxx_crb_addr_transform(RPMX3);
72 qla4_8xxx_crb_addr_transform(RPMX2);
73 qla4_8xxx_crb_addr_transform(RPMX1);
74 qla4_8xxx_crb_addr_transform(RPMX0);
75 qla4_8xxx_crb_addr_transform(ROMUSB);
76 qla4_8xxx_crb_addr_transform(SN);
77 qla4_8xxx_crb_addr_transform(QMN);
78 qla4_8xxx_crb_addr_transform(QMS);
79 qla4_8xxx_crb_addr_transform(PGNI);
80 qla4_8xxx_crb_addr_transform(PGND);
81 qla4_8xxx_crb_addr_transform(PGN3);
82 qla4_8xxx_crb_addr_transform(PGN2);
83 qla4_8xxx_crb_addr_transform(PGN1);
84 qla4_8xxx_crb_addr_transform(PGN0);
85 qla4_8xxx_crb_addr_transform(PGSI);
86 qla4_8xxx_crb_addr_transform(PGSD);
87 qla4_8xxx_crb_addr_transform(PGS3);
88 qla4_8xxx_crb_addr_transform(PGS2);
89 qla4_8xxx_crb_addr_transform(PGS1);
90 qla4_8xxx_crb_addr_transform(PGS0);
91 qla4_8xxx_crb_addr_transform(PS);
92 qla4_8xxx_crb_addr_transform(PH);
93 qla4_8xxx_crb_addr_transform(NIU);
94 qla4_8xxx_crb_addr_transform(I2Q);
95 qla4_8xxx_crb_addr_transform(EG);
96 qla4_8xxx_crb_addr_transform(MN);
97 qla4_8xxx_crb_addr_transform(MS);
98 qla4_8xxx_crb_addr_transform(CAS2);
99 qla4_8xxx_crb_addr_transform(CAS1);
100 qla4_8xxx_crb_addr_transform(CAS0);
101 qla4_8xxx_crb_addr_transform(CAM);
102 qla4_8xxx_crb_addr_transform(C2C1);
103 qla4_8xxx_crb_addr_transform(C2C0);
104 qla4_8xxx_crb_addr_transform(SMB);
105 qla4_8xxx_crb_addr_transform(OCM0);
106 qla4_8xxx_crb_addr_transform(I2C0);
107
108 qla4_8xxx_crb_table_initialized = 1;
109}
110
111static struct crb_128M_2M_block_map crb_128M_2M_map[64] = {
112 {{{0, 0, 0, 0} } }, /* 0: PCI */
113 {{{1, 0x0100000, 0x0102000, 0x120000}, /* 1: PCIE */
114 {1, 0x0110000, 0x0120000, 0x130000},
115 {1, 0x0120000, 0x0122000, 0x124000},
116 {1, 0x0130000, 0x0132000, 0x126000},
117 {1, 0x0140000, 0x0142000, 0x128000},
118 {1, 0x0150000, 0x0152000, 0x12a000},
119 {1, 0x0160000, 0x0170000, 0x110000},
120 {1, 0x0170000, 0x0172000, 0x12e000},
121 {0, 0x0000000, 0x0000000, 0x000000},
122 {0, 0x0000000, 0x0000000, 0x000000},
123 {0, 0x0000000, 0x0000000, 0x000000},
124 {0, 0x0000000, 0x0000000, 0x000000},
125 {0, 0x0000000, 0x0000000, 0x000000},
126 {0, 0x0000000, 0x0000000, 0x000000},
127 {1, 0x01e0000, 0x01e0800, 0x122000},
128 {0, 0x0000000, 0x0000000, 0x000000} } },
129 {{{1, 0x0200000, 0x0210000, 0x180000} } },/* 2: MN */
130 {{{0, 0, 0, 0} } }, /* 3: */
131 {{{1, 0x0400000, 0x0401000, 0x169000} } },/* 4: P2NR1 */
132 {{{1, 0x0500000, 0x0510000, 0x140000} } },/* 5: SRE */
133 {{{1, 0x0600000, 0x0610000, 0x1c0000} } },/* 6: NIU */
134 {{{1, 0x0700000, 0x0704000, 0x1b8000} } },/* 7: QM */
135 {{{1, 0x0800000, 0x0802000, 0x170000}, /* 8: SQM0 */
136 {0, 0x0000000, 0x0000000, 0x000000},
137 {0, 0x0000000, 0x0000000, 0x000000},
138 {0, 0x0000000, 0x0000000, 0x000000},
139 {0, 0x0000000, 0x0000000, 0x000000},
140 {0, 0x0000000, 0x0000000, 0x000000},
141 {0, 0x0000000, 0x0000000, 0x000000},
142 {0, 0x0000000, 0x0000000, 0x000000},
143 {0, 0x0000000, 0x0000000, 0x000000},
144 {0, 0x0000000, 0x0000000, 0x000000},
145 {0, 0x0000000, 0x0000000, 0x000000},
146 {0, 0x0000000, 0x0000000, 0x000000},
147 {0, 0x0000000, 0x0000000, 0x000000},
148 {0, 0x0000000, 0x0000000, 0x000000},
149 {0, 0x0000000, 0x0000000, 0x000000},
150 {1, 0x08f0000, 0x08f2000, 0x172000} } },
151 {{{1, 0x0900000, 0x0902000, 0x174000}, /* 9: SQM1*/
152 {0, 0x0000000, 0x0000000, 0x000000},
153 {0, 0x0000000, 0x0000000, 0x000000},
154 {0, 0x0000000, 0x0000000, 0x000000},
155 {0, 0x0000000, 0x0000000, 0x000000},
156 {0, 0x0000000, 0x0000000, 0x000000},
157 {0, 0x0000000, 0x0000000, 0x000000},
158 {0, 0x0000000, 0x0000000, 0x000000},
159 {0, 0x0000000, 0x0000000, 0x000000},
160 {0, 0x0000000, 0x0000000, 0x000000},
161 {0, 0x0000000, 0x0000000, 0x000000},
162 {0, 0x0000000, 0x0000000, 0x000000},
163 {0, 0x0000000, 0x0000000, 0x000000},
164 {0, 0x0000000, 0x0000000, 0x000000},
165 {0, 0x0000000, 0x0000000, 0x000000},
166 {1, 0x09f0000, 0x09f2000, 0x176000} } },
167 {{{0, 0x0a00000, 0x0a02000, 0x178000}, /* 10: SQM2*/
168 {0, 0x0000000, 0x0000000, 0x000000},
169 {0, 0x0000000, 0x0000000, 0x000000},
170 {0, 0x0000000, 0x0000000, 0x000000},
171 {0, 0x0000000, 0x0000000, 0x000000},
172 {0, 0x0000000, 0x0000000, 0x000000},
173 {0, 0x0000000, 0x0000000, 0x000000},
174 {0, 0x0000000, 0x0000000, 0x000000},
175 {0, 0x0000000, 0x0000000, 0x000000},
176 {0, 0x0000000, 0x0000000, 0x000000},
177 {0, 0x0000000, 0x0000000, 0x000000},
178 {0, 0x0000000, 0x0000000, 0x000000},
179 {0, 0x0000000, 0x0000000, 0x000000},
180 {0, 0x0000000, 0x0000000, 0x000000},
181 {0, 0x0000000, 0x0000000, 0x000000},
182 {1, 0x0af0000, 0x0af2000, 0x17a000} } },
183 {{{0, 0x0b00000, 0x0b02000, 0x17c000}, /* 11: SQM3*/
184 {0, 0x0000000, 0x0000000, 0x000000},
185 {0, 0x0000000, 0x0000000, 0x000000},
186 {0, 0x0000000, 0x0000000, 0x000000},
187 {0, 0x0000000, 0x0000000, 0x000000},
188 {0, 0x0000000, 0x0000000, 0x000000},
189 {0, 0x0000000, 0x0000000, 0x000000},
190 {0, 0x0000000, 0x0000000, 0x000000},
191 {0, 0x0000000, 0x0000000, 0x000000},
192 {0, 0x0000000, 0x0000000, 0x000000},
193 {0, 0x0000000, 0x0000000, 0x000000},
194 {0, 0x0000000, 0x0000000, 0x000000},
195 {0, 0x0000000, 0x0000000, 0x000000},
196 {0, 0x0000000, 0x0000000, 0x000000},
197 {0, 0x0000000, 0x0000000, 0x000000},
198 {1, 0x0bf0000, 0x0bf2000, 0x17e000} } },
199 {{{1, 0x0c00000, 0x0c04000, 0x1d4000} } },/* 12: I2Q */
200 {{{1, 0x0d00000, 0x0d04000, 0x1a4000} } },/* 13: TMR */
201 {{{1, 0x0e00000, 0x0e04000, 0x1a0000} } },/* 14: ROMUSB */
202 {{{1, 0x0f00000, 0x0f01000, 0x164000} } },/* 15: PEG4 */
203 {{{0, 0x1000000, 0x1004000, 0x1a8000} } },/* 16: XDMA */
204 {{{1, 0x1100000, 0x1101000, 0x160000} } },/* 17: PEG0 */
205 {{{1, 0x1200000, 0x1201000, 0x161000} } },/* 18: PEG1 */
206 {{{1, 0x1300000, 0x1301000, 0x162000} } },/* 19: PEG2 */
207 {{{1, 0x1400000, 0x1401000, 0x163000} } },/* 20: PEG3 */
208 {{{1, 0x1500000, 0x1501000, 0x165000} } },/* 21: P2ND */
209 {{{1, 0x1600000, 0x1601000, 0x166000} } },/* 22: P2NI */
210 {{{0, 0, 0, 0} } }, /* 23: */
211 {{{0, 0, 0, 0} } }, /* 24: */
212 {{{0, 0, 0, 0} } }, /* 25: */
213 {{{0, 0, 0, 0} } }, /* 26: */
214 {{{0, 0, 0, 0} } }, /* 27: */
215 {{{0, 0, 0, 0} } }, /* 28: */
216 {{{1, 0x1d00000, 0x1d10000, 0x190000} } },/* 29: MS */
217 {{{1, 0x1e00000, 0x1e01000, 0x16a000} } },/* 30: P2NR2 */
218 {{{1, 0x1f00000, 0x1f10000, 0x150000} } },/* 31: EPG */
219 {{{0} } }, /* 32: PCI */
220 {{{1, 0x2100000, 0x2102000, 0x120000}, /* 33: PCIE */
221 {1, 0x2110000, 0x2120000, 0x130000},
222 {1, 0x2120000, 0x2122000, 0x124000},
223 {1, 0x2130000, 0x2132000, 0x126000},
224 {1, 0x2140000, 0x2142000, 0x128000},
225 {1, 0x2150000, 0x2152000, 0x12a000},
226 {1, 0x2160000, 0x2170000, 0x110000},
227 {1, 0x2170000, 0x2172000, 0x12e000},
228 {0, 0x0000000, 0x0000000, 0x000000},
229 {0, 0x0000000, 0x0000000, 0x000000},
230 {0, 0x0000000, 0x0000000, 0x000000},
231 {0, 0x0000000, 0x0000000, 0x000000},
232 {0, 0x0000000, 0x0000000, 0x000000},
233 {0, 0x0000000, 0x0000000, 0x000000},
234 {0, 0x0000000, 0x0000000, 0x000000},
235 {0, 0x0000000, 0x0000000, 0x000000} } },
236 {{{1, 0x2200000, 0x2204000, 0x1b0000} } },/* 34: CAM */
237 {{{0} } }, /* 35: */
238 {{{0} } }, /* 36: */
239 {{{0} } }, /* 37: */
240 {{{0} } }, /* 38: */
241 {{{0} } }, /* 39: */
242 {{{1, 0x2800000, 0x2804000, 0x1a4000} } },/* 40: TMR */
243 {{{1, 0x2900000, 0x2901000, 0x16b000} } },/* 41: P2NR3 */
244 {{{1, 0x2a00000, 0x2a00400, 0x1ac400} } },/* 42: RPMX1 */
245 {{{1, 0x2b00000, 0x2b00400, 0x1ac800} } },/* 43: RPMX2 */
246 {{{1, 0x2c00000, 0x2c00400, 0x1acc00} } },/* 44: RPMX3 */
247 {{{1, 0x2d00000, 0x2d00400, 0x1ad000} } },/* 45: RPMX4 */
248 {{{1, 0x2e00000, 0x2e00400, 0x1ad400} } },/* 46: RPMX5 */
249 {{{1, 0x2f00000, 0x2f00400, 0x1ad800} } },/* 47: RPMX6 */
250 {{{1, 0x3000000, 0x3000400, 0x1adc00} } },/* 48: RPMX7 */
251 {{{0, 0x3100000, 0x3104000, 0x1a8000} } },/* 49: XDMA */
252 {{{1, 0x3200000, 0x3204000, 0x1d4000} } },/* 50: I2Q */
253 {{{1, 0x3300000, 0x3304000, 0x1a0000} } },/* 51: ROMUSB */
254 {{{0} } }, /* 52: */
255 {{{1, 0x3500000, 0x3500400, 0x1ac000} } },/* 53: RPMX0 */
256 {{{1, 0x3600000, 0x3600400, 0x1ae000} } },/* 54: RPMX8 */
257 {{{1, 0x3700000, 0x3700400, 0x1ae400} } },/* 55: RPMX9 */
258 {{{1, 0x3800000, 0x3804000, 0x1d0000} } },/* 56: OCM0 */
259 {{{1, 0x3900000, 0x3904000, 0x1b4000} } },/* 57: CRYPTO */
260 {{{1, 0x3a00000, 0x3a04000, 0x1d8000} } },/* 58: SMB */
261 {{{0} } }, /* 59: I2C0 */
262 {{{0} } }, /* 60: I2C1 */
263 {{{1, 0x3d00000, 0x3d04000, 0x1dc000} } },/* 61: LPC */
264 {{{1, 0x3e00000, 0x3e01000, 0x167000} } },/* 62: P2NC */
265 {{{1, 0x3f00000, 0x3f01000, 0x168000} } } /* 63: P2NR0 */
266};
267
268/*
269 * top 12 bits of crb internal address (hub, agent)
270 */
Vikas Chaudhary7664a1f2012-08-22 07:55:00 -0400271static unsigned qla4_82xx_crb_hub_agt[64] = {
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +0530272 0,
273 QLA82XX_HW_CRB_HUB_AGT_ADR_PS,
274 QLA82XX_HW_CRB_HUB_AGT_ADR_MN,
275 QLA82XX_HW_CRB_HUB_AGT_ADR_MS,
276 0,
277 QLA82XX_HW_CRB_HUB_AGT_ADR_SRE,
278 QLA82XX_HW_CRB_HUB_AGT_ADR_NIU,
279 QLA82XX_HW_CRB_HUB_AGT_ADR_QMN,
280 QLA82XX_HW_CRB_HUB_AGT_ADR_SQN0,
281 QLA82XX_HW_CRB_HUB_AGT_ADR_SQN1,
282 QLA82XX_HW_CRB_HUB_AGT_ADR_SQN2,
283 QLA82XX_HW_CRB_HUB_AGT_ADR_SQN3,
284 QLA82XX_HW_CRB_HUB_AGT_ADR_I2Q,
285 QLA82XX_HW_CRB_HUB_AGT_ADR_TIMR,
286 QLA82XX_HW_CRB_HUB_AGT_ADR_ROMUSB,
287 QLA82XX_HW_CRB_HUB_AGT_ADR_PGN4,
288 QLA82XX_HW_CRB_HUB_AGT_ADR_XDMA,
289 QLA82XX_HW_CRB_HUB_AGT_ADR_PGN0,
290 QLA82XX_HW_CRB_HUB_AGT_ADR_PGN1,
291 QLA82XX_HW_CRB_HUB_AGT_ADR_PGN2,
292 QLA82XX_HW_CRB_HUB_AGT_ADR_PGN3,
293 QLA82XX_HW_CRB_HUB_AGT_ADR_PGND,
294 QLA82XX_HW_CRB_HUB_AGT_ADR_PGNI,
295 QLA82XX_HW_CRB_HUB_AGT_ADR_PGS0,
296 QLA82XX_HW_CRB_HUB_AGT_ADR_PGS1,
297 QLA82XX_HW_CRB_HUB_AGT_ADR_PGS2,
298 QLA82XX_HW_CRB_HUB_AGT_ADR_PGS3,
299 0,
300 QLA82XX_HW_CRB_HUB_AGT_ADR_PGSI,
301 QLA82XX_HW_CRB_HUB_AGT_ADR_SN,
302 0,
303 QLA82XX_HW_CRB_HUB_AGT_ADR_EG,
304 0,
305 QLA82XX_HW_CRB_HUB_AGT_ADR_PS,
306 QLA82XX_HW_CRB_HUB_AGT_ADR_CAM,
307 0,
308 0,
309 0,
310 0,
311 0,
312 QLA82XX_HW_CRB_HUB_AGT_ADR_TIMR,
313 0,
314 QLA82XX_HW_CRB_HUB_AGT_ADR_RPMX1,
315 QLA82XX_HW_CRB_HUB_AGT_ADR_RPMX2,
316 QLA82XX_HW_CRB_HUB_AGT_ADR_RPMX3,
317 QLA82XX_HW_CRB_HUB_AGT_ADR_RPMX4,
318 QLA82XX_HW_CRB_HUB_AGT_ADR_RPMX5,
319 QLA82XX_HW_CRB_HUB_AGT_ADR_RPMX6,
320 QLA82XX_HW_CRB_HUB_AGT_ADR_RPMX7,
321 QLA82XX_HW_CRB_HUB_AGT_ADR_XDMA,
322 QLA82XX_HW_CRB_HUB_AGT_ADR_I2Q,
323 QLA82XX_HW_CRB_HUB_AGT_ADR_ROMUSB,
324 0,
325 QLA82XX_HW_CRB_HUB_AGT_ADR_RPMX0,
326 QLA82XX_HW_CRB_HUB_AGT_ADR_RPMX8,
327 QLA82XX_HW_CRB_HUB_AGT_ADR_RPMX9,
328 QLA82XX_HW_CRB_HUB_AGT_ADR_OCM0,
329 0,
330 QLA82XX_HW_CRB_HUB_AGT_ADR_SMB,
331 QLA82XX_HW_CRB_HUB_AGT_ADR_I2C0,
332 QLA82XX_HW_CRB_HUB_AGT_ADR_I2C1,
333 0,
334 QLA82XX_HW_CRB_HUB_AGT_ADR_PGNC,
335 0,
336};
337
338/* Device states */
339static char *qdev_state[] = {
340 "Unknown",
341 "Cold",
342 "Initializing",
343 "Ready",
344 "Need Reset",
345 "Need Quiescent",
346 "Failed",
347 "Quiescent",
348};
349
350/*
351 * In: 'off' is offset from CRB space in 128M pci map
352 * Out: 'off' is 2M pci map addr
353 * side effect: lock crb window
354 */
355static void
Vikas Chaudharyf8086f42012-08-22 07:54:59 -0400356qla4_82xx_pci_set_crbwindow_2M(struct scsi_qla_host *ha, ulong *off)
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +0530357{
358 u32 win_read;
359
360 ha->crb_win = CRB_HI(*off);
361 writel(ha->crb_win,
362 (void __iomem *)(CRB_WINDOW_2M + ha->nx_pcibase));
363
364 /* Read back value to make sure write has gone through before trying
365 * to use it. */
366 win_read = readl((void __iomem *)(CRB_WINDOW_2M + ha->nx_pcibase));
367 if (win_read != ha->crb_win) {
368 DEBUG2(ql4_printk(KERN_INFO, ha,
369 "%s: Written crbwin (0x%x) != Read crbwin (0x%x),"
370 " off=0x%lx\n", __func__, ha->crb_win, win_read, *off));
371 }
372 *off = (*off & MASK(16)) + CRB_INDIRECT_2M + ha->nx_pcibase;
373}
374
375void
Vikas Chaudharyf8086f42012-08-22 07:54:59 -0400376qla4_82xx_wr_32(struct scsi_qla_host *ha, ulong off, u32 data)
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +0530377{
378 unsigned long flags = 0;
379 int rv;
380
Vikas Chaudharyf8086f42012-08-22 07:54:59 -0400381 rv = qla4_82xx_pci_get_crb_addr_2M(ha, &off);
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +0530382
383 BUG_ON(rv == -1);
384
385 if (rv == 1) {
386 write_lock_irqsave(&ha->hw_lock, flags);
Vikas Chaudharyf8086f42012-08-22 07:54:59 -0400387 qla4_82xx_crb_win_lock(ha);
388 qla4_82xx_pci_set_crbwindow_2M(ha, &off);
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +0530389 }
390
391 writel(data, (void __iomem *)off);
392
393 if (rv == 1) {
Vikas Chaudharyf8086f42012-08-22 07:54:59 -0400394 qla4_82xx_crb_win_unlock(ha);
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +0530395 write_unlock_irqrestore(&ha->hw_lock, flags);
396 }
397}
398
399int
Vikas Chaudharyf8086f42012-08-22 07:54:59 -0400400qla4_82xx_rd_32(struct scsi_qla_host *ha, ulong off)
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +0530401{
402 unsigned long flags = 0;
403 int rv;
404 u32 data;
405
Vikas Chaudharyf8086f42012-08-22 07:54:59 -0400406 rv = qla4_82xx_pci_get_crb_addr_2M(ha, &off);
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +0530407
408 BUG_ON(rv == -1);
409
410 if (rv == 1) {
411 write_lock_irqsave(&ha->hw_lock, flags);
Vikas Chaudharyf8086f42012-08-22 07:54:59 -0400412 qla4_82xx_crb_win_lock(ha);
413 qla4_82xx_pci_set_crbwindow_2M(ha, &off);
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +0530414 }
415 data = readl((void __iomem *)off);
416
417 if (rv == 1) {
Vikas Chaudharyf8086f42012-08-22 07:54:59 -0400418 qla4_82xx_crb_win_unlock(ha);
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +0530419 write_unlock_irqrestore(&ha->hw_lock, flags);
420 }
421 return data;
422}
423
Tej Parkash068237c82012-05-18 04:41:44 -0400424/* Minidump related functions */
425static int qla4_8xxx_md_rw_32(struct scsi_qla_host *ha, uint32_t off,
426 u32 data, uint8_t flag)
427{
428 uint32_t win_read, off_value, rval = QLA_SUCCESS;
429
430 off_value = off & 0xFFFF0000;
431 writel(off_value, (void __iomem *)(CRB_WINDOW_2M + ha->nx_pcibase));
432
433 /* Read back value to make sure write has gone through before trying
434 * to use it.
435 */
436 win_read = readl((void __iomem *)(CRB_WINDOW_2M + ha->nx_pcibase));
437 if (win_read != off_value) {
438 DEBUG2(ql4_printk(KERN_INFO, ha,
439 "%s: Written (0x%x) != Read (0x%x), off=0x%x\n",
440 __func__, off_value, win_read, off));
441 return QLA_ERROR;
442 }
443
444 off_value = off & 0x0000FFFF;
445
446 if (flag)
447 writel(data, (void __iomem *)(off_value + CRB_INDIRECT_2M +
448 ha->nx_pcibase));
449 else
450 rval = readl((void __iomem *)(off_value + CRB_INDIRECT_2M +
451 ha->nx_pcibase));
452
453 return rval;
454}
455
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +0530456#define CRB_WIN_LOCK_TIMEOUT 100000000
457
Vikas Chaudharyf8086f42012-08-22 07:54:59 -0400458int qla4_82xx_crb_win_lock(struct scsi_qla_host *ha)
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +0530459{
460 int i;
461 int done = 0, timeout = 0;
462
463 while (!done) {
464 /* acquire semaphore3 from PCI HW block */
Vikas Chaudharyf8086f42012-08-22 07:54:59 -0400465 done = qla4_82xx_rd_32(ha, QLA82XX_PCIE_REG(PCIE_SEM7_LOCK));
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +0530466 if (done == 1)
467 break;
468 if (timeout >= CRB_WIN_LOCK_TIMEOUT)
469 return -1;
470
471 timeout++;
472
473 /* Yield CPU */
474 if (!in_interrupt())
475 schedule();
476 else {
477 for (i = 0; i < 20; i++)
478 cpu_relax(); /*This a nop instr on i386*/
479 }
480 }
Vikas Chaudharyf8086f42012-08-22 07:54:59 -0400481 qla4_82xx_wr_32(ha, QLA82XX_CRB_WIN_LOCK_ID, ha->func_num);
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +0530482 return 0;
483}
484
Vikas Chaudharyf8086f42012-08-22 07:54:59 -0400485void qla4_82xx_crb_win_unlock(struct scsi_qla_host *ha)
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +0530486{
Vikas Chaudharyf8086f42012-08-22 07:54:59 -0400487 qla4_82xx_rd_32(ha, QLA82XX_PCIE_REG(PCIE_SEM7_UNLOCK));
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +0530488}
489
490#define IDC_LOCK_TIMEOUT 100000000
491
492/**
Vikas Chaudharyf8086f42012-08-22 07:54:59 -0400493 * qla4_82xx_idc_lock - hw_lock
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +0530494 * @ha: pointer to adapter structure
495 *
496 * General purpose lock used to synchronize access to
497 * CRB_DEV_STATE, CRB_DEV_REF_COUNT, etc.
498 **/
Vikas Chaudharyf8086f42012-08-22 07:54:59 -0400499int qla4_82xx_idc_lock(struct scsi_qla_host *ha)
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +0530500{
501 int i;
502 int done = 0, timeout = 0;
503
504 while (!done) {
505 /* acquire semaphore5 from PCI HW block */
Vikas Chaudharyf8086f42012-08-22 07:54:59 -0400506 done = qla4_82xx_rd_32(ha, QLA82XX_PCIE_REG(PCIE_SEM5_LOCK));
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +0530507 if (done == 1)
508 break;
509 if (timeout >= IDC_LOCK_TIMEOUT)
510 return -1;
511
512 timeout++;
513
514 /* Yield CPU */
515 if (!in_interrupt())
516 schedule();
517 else {
518 for (i = 0; i < 20; i++)
519 cpu_relax(); /*This a nop instr on i386*/
520 }
521 }
522 return 0;
523}
524
Vikas Chaudharyf8086f42012-08-22 07:54:59 -0400525void qla4_82xx_idc_unlock(struct scsi_qla_host *ha)
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +0530526{
Vikas Chaudharyf8086f42012-08-22 07:54:59 -0400527 qla4_82xx_rd_32(ha, QLA82XX_PCIE_REG(PCIE_SEM5_UNLOCK));
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +0530528}
529
530int
Vikas Chaudharyf8086f42012-08-22 07:54:59 -0400531qla4_82xx_pci_get_crb_addr_2M(struct scsi_qla_host *ha, ulong *off)
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +0530532{
533 struct crb_128M_2M_sub_block_map *m;
534
535 if (*off >= QLA82XX_CRB_MAX)
536 return -1;
537
538 if (*off >= QLA82XX_PCI_CAMQM && (*off < QLA82XX_PCI_CAMQM_2M_END)) {
539 *off = (*off - QLA82XX_PCI_CAMQM) +
540 QLA82XX_PCI_CAMQM_2M_BASE + ha->nx_pcibase;
541 return 0;
542 }
543
544 if (*off < QLA82XX_PCI_CRBSPACE)
545 return -1;
546
547 *off -= QLA82XX_PCI_CRBSPACE;
548 /*
549 * Try direct map
550 */
551
552 m = &crb_128M_2M_map[CRB_BLK(*off)].sub_block[CRB_SUBBLK(*off)];
553
554 if (m->valid && (m->start_128M <= *off) && (m->end_128M > *off)) {
555 *off = *off + m->start_2M - m->start_128M + ha->nx_pcibase;
556 return 0;
557 }
558
559 /*
560 * Not in direct map, use crb window
561 */
562 return 1;
563}
564
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +0530565/*
566* check memory access boundary.
567* used by test agent. support ddr access only for now
568*/
569static unsigned long
Vikas Chaudharyf8086f42012-08-22 07:54:59 -0400570qla4_82xx_pci_mem_bound_check(struct scsi_qla_host *ha,
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +0530571 unsigned long long addr, int size)
572{
Vikas Chaudharyde8c72d2012-08-22 09:14:24 -0400573 if (!QLA8XXX_ADDR_IN_RANGE(addr, QLA8XXX_ADDR_DDR_NET,
574 QLA8XXX_ADDR_DDR_NET_MAX) ||
575 !QLA8XXX_ADDR_IN_RANGE(addr + size - 1,
576 QLA8XXX_ADDR_DDR_NET, QLA8XXX_ADDR_DDR_NET_MAX) ||
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +0530577 ((size != 1) && (size != 2) && (size != 4) && (size != 8))) {
578 return 0;
579 }
580 return 1;
581}
582
Vikas Chaudhary7664a1f2012-08-22 07:55:00 -0400583static int qla4_82xx_pci_set_window_warning_count;
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +0530584
585static unsigned long
Vikas Chaudharyf8086f42012-08-22 07:54:59 -0400586qla4_82xx_pci_set_window(struct scsi_qla_host *ha, unsigned long long addr)
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +0530587{
588 int window;
589 u32 win_read;
590
Vikas Chaudharyde8c72d2012-08-22 09:14:24 -0400591 if (QLA8XXX_ADDR_IN_RANGE(addr, QLA8XXX_ADDR_DDR_NET,
592 QLA8XXX_ADDR_DDR_NET_MAX)) {
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +0530593 /* DDR network side */
594 window = MN_WIN(addr);
595 ha->ddr_mn_window = window;
Vikas Chaudharyf8086f42012-08-22 07:54:59 -0400596 qla4_82xx_wr_32(ha, ha->mn_win_crb |
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +0530597 QLA82XX_PCI_CRBSPACE, window);
Vikas Chaudharyf8086f42012-08-22 07:54:59 -0400598 win_read = qla4_82xx_rd_32(ha, ha->mn_win_crb |
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +0530599 QLA82XX_PCI_CRBSPACE);
600 if ((win_read << 17) != window) {
601 ql4_printk(KERN_WARNING, ha,
602 "%s: Written MNwin (0x%x) != Read MNwin (0x%x)\n",
603 __func__, window, win_read);
604 }
605 addr = GET_MEM_OFFS_2M(addr) + QLA82XX_PCI_DDR_NET;
Vikas Chaudharyde8c72d2012-08-22 09:14:24 -0400606 } else if (QLA8XXX_ADDR_IN_RANGE(addr, QLA8XXX_ADDR_OCM0,
607 QLA8XXX_ADDR_OCM0_MAX)) {
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +0530608 unsigned int temp1;
609 /* if bits 19:18&17:11 are on */
610 if ((addr & 0x00ff800) == 0xff800) {
611 printk("%s: QM access not handled.\n", __func__);
612 addr = -1UL;
613 }
614
615 window = OCM_WIN(addr);
616 ha->ddr_mn_window = window;
Vikas Chaudharyf8086f42012-08-22 07:54:59 -0400617 qla4_82xx_wr_32(ha, ha->mn_win_crb |
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +0530618 QLA82XX_PCI_CRBSPACE, window);
Vikas Chaudharyf8086f42012-08-22 07:54:59 -0400619 win_read = qla4_82xx_rd_32(ha, ha->mn_win_crb |
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +0530620 QLA82XX_PCI_CRBSPACE);
621 temp1 = ((window & 0x1FF) << 7) |
622 ((window & 0x0FFFE0000) >> 17);
623 if (win_read != temp1) {
624 printk("%s: Written OCMwin (0x%x) != Read"
625 " OCMwin (0x%x)\n", __func__, temp1, win_read);
626 }
627 addr = GET_MEM_OFFS_2M(addr) + QLA82XX_PCI_OCM0_2M;
628
Vikas Chaudharyde8c72d2012-08-22 09:14:24 -0400629 } else if (QLA8XXX_ADDR_IN_RANGE(addr, QLA8XXX_ADDR_QDR_NET,
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +0530630 QLA82XX_P3_ADDR_QDR_NET_MAX)) {
631 /* QDR network side */
632 window = MS_WIN(addr);
633 ha->qdr_sn_window = window;
Vikas Chaudharyf8086f42012-08-22 07:54:59 -0400634 qla4_82xx_wr_32(ha, ha->ms_win_crb |
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +0530635 QLA82XX_PCI_CRBSPACE, window);
Vikas Chaudharyf8086f42012-08-22 07:54:59 -0400636 win_read = qla4_82xx_rd_32(ha,
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +0530637 ha->ms_win_crb | QLA82XX_PCI_CRBSPACE);
638 if (win_read != window) {
639 printk("%s: Written MSwin (0x%x) != Read "
640 "MSwin (0x%x)\n", __func__, window, win_read);
641 }
642 addr = GET_MEM_OFFS_2M(addr) + QLA82XX_PCI_QDR_NET;
643
644 } else {
645 /*
646 * peg gdb frequently accesses memory that doesn't exist,
647 * this limits the chit chat so debugging isn't slowed down.
648 */
Vikas Chaudhary7664a1f2012-08-22 07:55:00 -0400649 if ((qla4_82xx_pci_set_window_warning_count++ < 8) ||
650 (qla4_82xx_pci_set_window_warning_count%64 == 0)) {
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +0530651 printk("%s: Warning:%s Unknown address range!\n",
652 __func__, DRIVER_NAME);
653 }
654 addr = -1UL;
655 }
656 return addr;
657}
658
659/* check if address is in the same windows as the previous access */
Vikas Chaudharyf8086f42012-08-22 07:54:59 -0400660static int qla4_82xx_pci_is_same_window(struct scsi_qla_host *ha,
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +0530661 unsigned long long addr)
662{
663 int window;
664 unsigned long long qdr_max;
665
666 qdr_max = QLA82XX_P3_ADDR_QDR_NET_MAX;
667
Vikas Chaudharyde8c72d2012-08-22 09:14:24 -0400668 if (QLA8XXX_ADDR_IN_RANGE(addr, QLA8XXX_ADDR_DDR_NET,
669 QLA8XXX_ADDR_DDR_NET_MAX)) {
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +0530670 /* DDR network side */
671 BUG(); /* MN access can not come here */
Vikas Chaudharyde8c72d2012-08-22 09:14:24 -0400672 } else if (QLA8XXX_ADDR_IN_RANGE(addr, QLA8XXX_ADDR_OCM0,
673 QLA8XXX_ADDR_OCM0_MAX)) {
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +0530674 return 1;
Vikas Chaudharyde8c72d2012-08-22 09:14:24 -0400675 } else if (QLA8XXX_ADDR_IN_RANGE(addr, QLA8XXX_ADDR_OCM1,
676 QLA8XXX_ADDR_OCM1_MAX)) {
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +0530677 return 1;
Vikas Chaudharyde8c72d2012-08-22 09:14:24 -0400678 } else if (QLA8XXX_ADDR_IN_RANGE(addr, QLA8XXX_ADDR_QDR_NET,
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +0530679 qdr_max)) {
680 /* QDR network side */
Vikas Chaudharyde8c72d2012-08-22 09:14:24 -0400681 window = ((addr - QLA8XXX_ADDR_QDR_NET) >> 22) & 0x3f;
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +0530682 if (ha->qdr_sn_window == window)
683 return 1;
684 }
685
686 return 0;
687}
688
Vikas Chaudharyf8086f42012-08-22 07:54:59 -0400689static int qla4_82xx_pci_mem_read_direct(struct scsi_qla_host *ha,
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +0530690 u64 off, void *data, int size)
691{
692 unsigned long flags;
693 void __iomem *addr;
694 int ret = 0;
695 u64 start;
696 void __iomem *mem_ptr = NULL;
697 unsigned long mem_base;
698 unsigned long mem_page;
699
700 write_lock_irqsave(&ha->hw_lock, flags);
701
702 /*
703 * If attempting to access unknown address or straddle hw windows,
704 * do not access.
705 */
Vikas Chaudharyf8086f42012-08-22 07:54:59 -0400706 start = qla4_82xx_pci_set_window(ha, off);
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +0530707 if ((start == -1UL) ||
Vikas Chaudharyf8086f42012-08-22 07:54:59 -0400708 (qla4_82xx_pci_is_same_window(ha, off + size - 1) == 0)) {
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +0530709 write_unlock_irqrestore(&ha->hw_lock, flags);
710 printk(KERN_ERR"%s out of bound pci memory access. "
711 "offset is 0x%llx\n", DRIVER_NAME, off);
712 return -1;
713 }
714
715 addr = qla4_8xxx_pci_base_offsetfset(ha, start);
716 if (!addr) {
717 write_unlock_irqrestore(&ha->hw_lock, flags);
718 mem_base = pci_resource_start(ha->pdev, 0);
719 mem_page = start & PAGE_MASK;
720 /* Map two pages whenever user tries to access addresses in two
721 consecutive pages.
722 */
723 if (mem_page != ((start + size - 1) & PAGE_MASK))
724 mem_ptr = ioremap(mem_base + mem_page, PAGE_SIZE * 2);
725 else
726 mem_ptr = ioremap(mem_base + mem_page, PAGE_SIZE);
727
728 if (mem_ptr == NULL) {
729 *(u8 *)data = 0;
730 return -1;
731 }
732 addr = mem_ptr;
733 addr += start & (PAGE_SIZE - 1);
734 write_lock_irqsave(&ha->hw_lock, flags);
735 }
736
737 switch (size) {
738 case 1:
739 *(u8 *)data = readb(addr);
740 break;
741 case 2:
742 *(u16 *)data = readw(addr);
743 break;
744 case 4:
745 *(u32 *)data = readl(addr);
746 break;
747 case 8:
748 *(u64 *)data = readq(addr);
749 break;
750 default:
751 ret = -1;
752 break;
753 }
754 write_unlock_irqrestore(&ha->hw_lock, flags);
755
756 if (mem_ptr)
757 iounmap(mem_ptr);
758 return ret;
759}
760
761static int
Vikas Chaudharyf8086f42012-08-22 07:54:59 -0400762qla4_82xx_pci_mem_write_direct(struct scsi_qla_host *ha, u64 off,
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +0530763 void *data, int size)
764{
765 unsigned long flags;
766 void __iomem *addr;
767 int ret = 0;
768 u64 start;
769 void __iomem *mem_ptr = NULL;
770 unsigned long mem_base;
771 unsigned long mem_page;
772
773 write_lock_irqsave(&ha->hw_lock, flags);
774
775 /*
776 * If attempting to access unknown address or straddle hw windows,
777 * do not access.
778 */
Vikas Chaudharyf8086f42012-08-22 07:54:59 -0400779 start = qla4_82xx_pci_set_window(ha, off);
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +0530780 if ((start == -1UL) ||
Vikas Chaudharyf8086f42012-08-22 07:54:59 -0400781 (qla4_82xx_pci_is_same_window(ha, off + size - 1) == 0)) {
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +0530782 write_unlock_irqrestore(&ha->hw_lock, flags);
783 printk(KERN_ERR"%s out of bound pci memory access. "
784 "offset is 0x%llx\n", DRIVER_NAME, off);
785 return -1;
786 }
787
788 addr = qla4_8xxx_pci_base_offsetfset(ha, start);
789 if (!addr) {
790 write_unlock_irqrestore(&ha->hw_lock, flags);
791 mem_base = pci_resource_start(ha->pdev, 0);
792 mem_page = start & PAGE_MASK;
793 /* Map two pages whenever user tries to access addresses in two
794 consecutive pages.
795 */
796 if (mem_page != ((start + size - 1) & PAGE_MASK))
797 mem_ptr = ioremap(mem_base + mem_page, PAGE_SIZE*2);
798 else
799 mem_ptr = ioremap(mem_base + mem_page, PAGE_SIZE);
800 if (mem_ptr == NULL)
801 return -1;
802
803 addr = mem_ptr;
804 addr += start & (PAGE_SIZE - 1);
805 write_lock_irqsave(&ha->hw_lock, flags);
806 }
807
808 switch (size) {
809 case 1:
810 writeb(*(u8 *)data, addr);
811 break;
812 case 2:
813 writew(*(u16 *)data, addr);
814 break;
815 case 4:
816 writel(*(u32 *)data, addr);
817 break;
818 case 8:
819 writeq(*(u64 *)data, addr);
820 break;
821 default:
822 ret = -1;
823 break;
824 }
825 write_unlock_irqrestore(&ha->hw_lock, flags);
826 if (mem_ptr)
827 iounmap(mem_ptr);
828 return ret;
829}
830
831#define MTU_FUDGE_FACTOR 100
832
833static unsigned long
Vikas Chaudharyf8086f42012-08-22 07:54:59 -0400834qla4_82xx_decode_crb_addr(unsigned long addr)
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +0530835{
836 int i;
837 unsigned long base_addr, offset, pci_base;
838
839 if (!qla4_8xxx_crb_table_initialized)
Vikas Chaudharyf8086f42012-08-22 07:54:59 -0400840 qla4_82xx_crb_addr_transform_setup();
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +0530841
842 pci_base = ADDR_ERROR;
843 base_addr = addr & 0xfff00000;
844 offset = addr & 0x000fffff;
845
846 for (i = 0; i < MAX_CRB_XFORM; i++) {
847 if (crb_addr_xform[i] == base_addr) {
848 pci_base = i << 20;
849 break;
850 }
851 }
852 if (pci_base == ADDR_ERROR)
853 return pci_base;
854 else
855 return pci_base + offset;
856}
857
858static long rom_max_timeout = 100;
Vikas Chaudhary7664a1f2012-08-22 07:55:00 -0400859static long qla4_82xx_rom_lock_timeout = 100;
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +0530860
861static int
Vikas Chaudharyf8086f42012-08-22 07:54:59 -0400862qla4_82xx_rom_lock(struct scsi_qla_host *ha)
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +0530863{
864 int i;
865 int done = 0, timeout = 0;
866
867 while (!done) {
868 /* acquire semaphore2 from PCI HW block */
869
Vikas Chaudharyf8086f42012-08-22 07:54:59 -0400870 done = qla4_82xx_rd_32(ha, QLA82XX_PCIE_REG(PCIE_SEM2_LOCK));
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +0530871 if (done == 1)
872 break;
Vikas Chaudhary7664a1f2012-08-22 07:55:00 -0400873 if (timeout >= qla4_82xx_rom_lock_timeout)
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +0530874 return -1;
875
876 timeout++;
877
878 /* Yield CPU */
879 if (!in_interrupt())
880 schedule();
881 else {
882 for (i = 0; i < 20; i++)
883 cpu_relax(); /*This a nop instr on i386*/
884 }
885 }
Vikas Chaudharyf8086f42012-08-22 07:54:59 -0400886 qla4_82xx_wr_32(ha, QLA82XX_ROM_LOCK_ID, ROM_LOCK_DRIVER);
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +0530887 return 0;
888}
889
890static void
Vikas Chaudharyf8086f42012-08-22 07:54:59 -0400891qla4_82xx_rom_unlock(struct scsi_qla_host *ha)
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +0530892{
Vikas Chaudharyf8086f42012-08-22 07:54:59 -0400893 qla4_82xx_rd_32(ha, QLA82XX_PCIE_REG(PCIE_SEM2_UNLOCK));
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +0530894}
895
896static int
Vikas Chaudharyf8086f42012-08-22 07:54:59 -0400897qla4_82xx_wait_rom_done(struct scsi_qla_host *ha)
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +0530898{
899 long timeout = 0;
900 long done = 0 ;
901
902 while (done == 0) {
Vikas Chaudharyf8086f42012-08-22 07:54:59 -0400903 done = qla4_82xx_rd_32(ha, QLA82XX_ROMUSB_GLB_STATUS);
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +0530904 done &= 2;
905 timeout++;
906 if (timeout >= rom_max_timeout) {
907 printk("%s: Timeout reached waiting for rom done",
908 DRIVER_NAME);
909 return -1;
910 }
911 }
912 return 0;
913}
914
915static int
Vikas Chaudharyf8086f42012-08-22 07:54:59 -0400916qla4_82xx_do_rom_fast_read(struct scsi_qla_host *ha, int addr, int *valp)
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +0530917{
Vikas Chaudharyf8086f42012-08-22 07:54:59 -0400918 qla4_82xx_wr_32(ha, QLA82XX_ROMUSB_ROM_ADDRESS, addr);
919 qla4_82xx_wr_32(ha, QLA82XX_ROMUSB_ROM_DUMMY_BYTE_CNT, 0);
920 qla4_82xx_wr_32(ha, QLA82XX_ROMUSB_ROM_ABYTE_CNT, 3);
921 qla4_82xx_wr_32(ha, QLA82XX_ROMUSB_ROM_INSTR_OPCODE, 0xb);
922 if (qla4_82xx_wait_rom_done(ha)) {
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +0530923 printk("%s: Error waiting for rom done\n", DRIVER_NAME);
924 return -1;
925 }
926 /* reset abyte_cnt and dummy_byte_cnt */
Vikas Chaudharyf8086f42012-08-22 07:54:59 -0400927 qla4_82xx_wr_32(ha, QLA82XX_ROMUSB_ROM_DUMMY_BYTE_CNT, 0);
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +0530928 udelay(10);
Vikas Chaudharyf8086f42012-08-22 07:54:59 -0400929 qla4_82xx_wr_32(ha, QLA82XX_ROMUSB_ROM_ABYTE_CNT, 0);
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +0530930
Vikas Chaudharyf8086f42012-08-22 07:54:59 -0400931 *valp = qla4_82xx_rd_32(ha, QLA82XX_ROMUSB_ROM_RDATA);
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +0530932 return 0;
933}
934
935static int
Vikas Chaudharyf8086f42012-08-22 07:54:59 -0400936qla4_82xx_rom_fast_read(struct scsi_qla_host *ha, int addr, int *valp)
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +0530937{
938 int ret, loops = 0;
939
Vikas Chaudharyf8086f42012-08-22 07:54:59 -0400940 while ((qla4_82xx_rom_lock(ha) != 0) && (loops < 50000)) {
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +0530941 udelay(100);
942 loops++;
943 }
944 if (loops >= 50000) {
Vikas Chaudharyf8086f42012-08-22 07:54:59 -0400945 ql4_printk(KERN_WARNING, ha, "%s: qla4_82xx_rom_lock failed\n",
946 DRIVER_NAME);
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +0530947 return -1;
948 }
Vikas Chaudharyf8086f42012-08-22 07:54:59 -0400949 ret = qla4_82xx_do_rom_fast_read(ha, addr, valp);
950 qla4_82xx_rom_unlock(ha);
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +0530951 return ret;
952}
953
954/**
955 * This routine does CRB initialize sequence
956 * to put the ISP into operational state
957 **/
958static int
Vikas Chaudharyf8086f42012-08-22 07:54:59 -0400959qla4_82xx_pinit_from_rom(struct scsi_qla_host *ha, int verbose)
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +0530960{
961 int addr, val;
962 int i ;
963 struct crb_addr_pair *buf;
964 unsigned long off;
965 unsigned offset, n;
966
967 struct crb_addr_pair {
968 long addr;
969 long data;
970 };
971
972 /* Halt all the indiviual PEGs and other blocks of the ISP */
Vikas Chaudharyf8086f42012-08-22 07:54:59 -0400973 qla4_82xx_rom_lock(ha);
Swapnil Naglea1fc26b2010-12-02 22:12:15 -0800974
Vikas Chaudharycb744282011-05-17 23:17:04 -0700975 /* disable all I2Q */
Vikas Chaudharyf8086f42012-08-22 07:54:59 -0400976 qla4_82xx_wr_32(ha, QLA82XX_CRB_I2Q + 0x10, 0x0);
977 qla4_82xx_wr_32(ha, QLA82XX_CRB_I2Q + 0x14, 0x0);
978 qla4_82xx_wr_32(ha, QLA82XX_CRB_I2Q + 0x18, 0x0);
979 qla4_82xx_wr_32(ha, QLA82XX_CRB_I2Q + 0x1c, 0x0);
980 qla4_82xx_wr_32(ha, QLA82XX_CRB_I2Q + 0x20, 0x0);
981 qla4_82xx_wr_32(ha, QLA82XX_CRB_I2Q + 0x24, 0x0);
Vikas Chaudharycb744282011-05-17 23:17:04 -0700982
983 /* disable all niu interrupts */
Vikas Chaudharyf8086f42012-08-22 07:54:59 -0400984 qla4_82xx_wr_32(ha, QLA82XX_CRB_NIU + 0x40, 0xff);
Swapnil Naglea1fc26b2010-12-02 22:12:15 -0800985 /* disable xge rx/tx */
Vikas Chaudharyf8086f42012-08-22 07:54:59 -0400986 qla4_82xx_wr_32(ha, QLA82XX_CRB_NIU + 0x70000, 0x00);
Swapnil Naglea1fc26b2010-12-02 22:12:15 -0800987 /* disable xg1 rx/tx */
Vikas Chaudharyf8086f42012-08-22 07:54:59 -0400988 qla4_82xx_wr_32(ha, QLA82XX_CRB_NIU + 0x80000, 0x00);
Vikas Chaudharycb744282011-05-17 23:17:04 -0700989 /* disable sideband mac */
Vikas Chaudharyf8086f42012-08-22 07:54:59 -0400990 qla4_82xx_wr_32(ha, QLA82XX_CRB_NIU + 0x90000, 0x00);
Vikas Chaudharycb744282011-05-17 23:17:04 -0700991 /* disable ap0 mac */
Vikas Chaudharyf8086f42012-08-22 07:54:59 -0400992 qla4_82xx_wr_32(ha, QLA82XX_CRB_NIU + 0xa0000, 0x00);
Vikas Chaudharycb744282011-05-17 23:17:04 -0700993 /* disable ap1 mac */
Vikas Chaudharyf8086f42012-08-22 07:54:59 -0400994 qla4_82xx_wr_32(ha, QLA82XX_CRB_NIU + 0xb0000, 0x00);
Swapnil Naglea1fc26b2010-12-02 22:12:15 -0800995
996 /* halt sre */
Vikas Chaudharyf8086f42012-08-22 07:54:59 -0400997 val = qla4_82xx_rd_32(ha, QLA82XX_CRB_SRE + 0x1000);
998 qla4_82xx_wr_32(ha, QLA82XX_CRB_SRE + 0x1000, val & (~(0x1)));
Swapnil Naglea1fc26b2010-12-02 22:12:15 -0800999
1000 /* halt epg */
Vikas Chaudharyf8086f42012-08-22 07:54:59 -04001001 qla4_82xx_wr_32(ha, QLA82XX_CRB_EPG + 0x1300, 0x1);
Swapnil Naglea1fc26b2010-12-02 22:12:15 -08001002
1003 /* halt timers */
Vikas Chaudharyf8086f42012-08-22 07:54:59 -04001004 qla4_82xx_wr_32(ha, QLA82XX_CRB_TIMER + 0x0, 0x0);
1005 qla4_82xx_wr_32(ha, QLA82XX_CRB_TIMER + 0x8, 0x0);
1006 qla4_82xx_wr_32(ha, QLA82XX_CRB_TIMER + 0x10, 0x0);
1007 qla4_82xx_wr_32(ha, QLA82XX_CRB_TIMER + 0x18, 0x0);
1008 qla4_82xx_wr_32(ha, QLA82XX_CRB_TIMER + 0x100, 0x0);
1009 qla4_82xx_wr_32(ha, QLA82XX_CRB_TIMER + 0x200, 0x0);
Swapnil Naglea1fc26b2010-12-02 22:12:15 -08001010
1011 /* halt pegs */
Vikas Chaudharyf8086f42012-08-22 07:54:59 -04001012 qla4_82xx_wr_32(ha, QLA82XX_CRB_PEG_NET_0 + 0x3c, 1);
1013 qla4_82xx_wr_32(ha, QLA82XX_CRB_PEG_NET_1 + 0x3c, 1);
1014 qla4_82xx_wr_32(ha, QLA82XX_CRB_PEG_NET_2 + 0x3c, 1);
1015 qla4_82xx_wr_32(ha, QLA82XX_CRB_PEG_NET_3 + 0x3c, 1);
1016 qla4_82xx_wr_32(ha, QLA82XX_CRB_PEG_NET_4 + 0x3c, 1);
Vikas Chaudharycb744282011-05-17 23:17:04 -07001017 msleep(5);
Swapnil Naglea1fc26b2010-12-02 22:12:15 -08001018
1019 /* big hammer */
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301020 if (test_bit(DPC_RESET_HA, &ha->dpc_flags))
1021 /* don't reset CAM block on reset */
Vikas Chaudharyf8086f42012-08-22 07:54:59 -04001022 qla4_82xx_wr_32(ha, QLA82XX_ROMUSB_GLB_SW_RESET, 0xfeffffff);
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301023 else
Vikas Chaudharyf8086f42012-08-22 07:54:59 -04001024 qla4_82xx_wr_32(ha, QLA82XX_ROMUSB_GLB_SW_RESET, 0xffffffff);
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301025
Vikas Chaudharyf8086f42012-08-22 07:54:59 -04001026 qla4_82xx_rom_unlock(ha);
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301027
1028 /* Read the signature value from the flash.
1029 * Offset 0: Contain signature (0xcafecafe)
1030 * Offset 4: Offset and number of addr/value pairs
1031 * that present in CRB initialize sequence
1032 */
Vikas Chaudharyf8086f42012-08-22 07:54:59 -04001033 if (qla4_82xx_rom_fast_read(ha, 0, &n) != 0 || n != 0xcafecafeUL ||
1034 qla4_82xx_rom_fast_read(ha, 4, &n) != 0) {
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301035 ql4_printk(KERN_WARNING, ha,
1036 "[ERROR] Reading crb_init area: n: %08x\n", n);
1037 return -1;
1038 }
1039
1040 /* Offset in flash = lower 16 bits
1041 * Number of enteries = upper 16 bits
1042 */
1043 offset = n & 0xffffU;
1044 n = (n >> 16) & 0xffffU;
1045
1046 /* number of addr/value pair should not exceed 1024 enteries */
1047 if (n >= 1024) {
1048 ql4_printk(KERN_WARNING, ha,
1049 "%s: %s:n=0x%x [ERROR] Card flash not initialized.\n",
1050 DRIVER_NAME, __func__, n);
1051 return -1;
1052 }
1053
1054 ql4_printk(KERN_INFO, ha,
1055 "%s: %d CRB init values found in ROM.\n", DRIVER_NAME, n);
1056
1057 buf = kmalloc(n * sizeof(struct crb_addr_pair), GFP_KERNEL);
1058 if (buf == NULL) {
1059 ql4_printk(KERN_WARNING, ha,
1060 "%s: [ERROR] Unable to malloc memory.\n", DRIVER_NAME);
1061 return -1;
1062 }
1063
1064 for (i = 0; i < n; i++) {
Vikas Chaudharyf8086f42012-08-22 07:54:59 -04001065 if (qla4_82xx_rom_fast_read(ha, 8*i + 4*offset, &val) != 0 ||
1066 qla4_82xx_rom_fast_read(ha, 8*i + 4*offset + 4, &addr) !=
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301067 0) {
1068 kfree(buf);
1069 return -1;
1070 }
1071
1072 buf[i].addr = addr;
1073 buf[i].data = val;
1074 }
1075
1076 for (i = 0; i < n; i++) {
1077 /* Translate internal CRB initialization
1078 * address to PCI bus address
1079 */
Vikas Chaudharyf8086f42012-08-22 07:54:59 -04001080 off = qla4_82xx_decode_crb_addr((unsigned long)buf[i].addr) +
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301081 QLA82XX_PCI_CRBSPACE;
1082 /* Not all CRB addr/value pair to be written,
1083 * some of them are skipped
1084 */
1085
1086 /* skip if LS bit is set*/
1087 if (off & 0x1) {
1088 DEBUG2(ql4_printk(KERN_WARNING, ha,
1089 "Skip CRB init replay for offset = 0x%lx\n", off));
1090 continue;
1091 }
1092
1093 /* skipping cold reboot MAGIC */
1094 if (off == QLA82XX_CAM_RAM(0x1fc))
1095 continue;
1096
1097 /* do not reset PCI */
1098 if (off == (ROMUSB_GLB + 0xbc))
1099 continue;
1100
1101 /* skip core clock, so that firmware can increase the clock */
1102 if (off == (ROMUSB_GLB + 0xc8))
1103 continue;
1104
1105 /* skip the function enable register */
1106 if (off == QLA82XX_PCIE_REG(PCIE_SETUP_FUNCTION))
1107 continue;
1108
1109 if (off == QLA82XX_PCIE_REG(PCIE_SETUP_FUNCTION2))
1110 continue;
1111
1112 if ((off & 0x0ff00000) == QLA82XX_CRB_SMB)
1113 continue;
1114
1115 if ((off & 0x0ff00000) == QLA82XX_CRB_DDR_NET)
1116 continue;
1117
1118 if (off == ADDR_ERROR) {
1119 ql4_printk(KERN_WARNING, ha,
1120 "%s: [ERROR] Unknown addr: 0x%08lx\n",
1121 DRIVER_NAME, buf[i].addr);
1122 continue;
1123 }
1124
Vikas Chaudharyf8086f42012-08-22 07:54:59 -04001125 qla4_82xx_wr_32(ha, off, buf[i].data);
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301126
1127 /* ISP requires much bigger delay to settle down,
1128 * else crb_window returns 0xffffffff
1129 */
1130 if (off == QLA82XX_ROMUSB_GLB_SW_RESET)
1131 msleep(1000);
1132
1133 /* ISP requires millisec delay between
1134 * successive CRB register updation
1135 */
1136 msleep(1);
1137 }
1138
1139 kfree(buf);
1140
1141 /* Resetting the data and instruction cache */
Vikas Chaudharyf8086f42012-08-22 07:54:59 -04001142 qla4_82xx_wr_32(ha, QLA82XX_CRB_PEG_NET_D+0xec, 0x1e);
1143 qla4_82xx_wr_32(ha, QLA82XX_CRB_PEG_NET_D+0x4c, 8);
1144 qla4_82xx_wr_32(ha, QLA82XX_CRB_PEG_NET_I+0x4c, 8);
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301145
1146 /* Clear all protocol processing engines */
Vikas Chaudharyf8086f42012-08-22 07:54:59 -04001147 qla4_82xx_wr_32(ha, QLA82XX_CRB_PEG_NET_0+0x8, 0);
1148 qla4_82xx_wr_32(ha, QLA82XX_CRB_PEG_NET_0+0xc, 0);
1149 qla4_82xx_wr_32(ha, QLA82XX_CRB_PEG_NET_1+0x8, 0);
1150 qla4_82xx_wr_32(ha, QLA82XX_CRB_PEG_NET_1+0xc, 0);
1151 qla4_82xx_wr_32(ha, QLA82XX_CRB_PEG_NET_2+0x8, 0);
1152 qla4_82xx_wr_32(ha, QLA82XX_CRB_PEG_NET_2+0xc, 0);
1153 qla4_82xx_wr_32(ha, QLA82XX_CRB_PEG_NET_3+0x8, 0);
1154 qla4_82xx_wr_32(ha, QLA82XX_CRB_PEG_NET_3+0xc, 0);
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301155
1156 return 0;
1157}
1158
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301159static int
Vikas Chaudharyf8086f42012-08-22 07:54:59 -04001160qla4_82xx_load_from_flash(struct scsi_qla_host *ha, uint32_t image_start)
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301161{
Lalit Chandivade4cd83cb2010-12-02 22:12:40 -08001162 int i, rval = 0;
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301163 long size = 0;
1164 long flashaddr, memaddr;
1165 u64 data;
1166 u32 high, low;
1167
1168 flashaddr = memaddr = ha->hw.flt_region_bootload;
Lalit Chandivade4cd83cb2010-12-02 22:12:40 -08001169 size = (image_start - flashaddr) / 8;
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301170
1171 DEBUG2(printk("scsi%ld: %s: bootldr=0x%lx, fw_image=0x%x\n",
1172 ha->host_no, __func__, flashaddr, image_start));
1173
1174 for (i = 0; i < size; i++) {
Vikas Chaudharyf8086f42012-08-22 07:54:59 -04001175 if ((qla4_82xx_rom_fast_read(ha, flashaddr, (int *)&low)) ||
1176 (qla4_82xx_rom_fast_read(ha, flashaddr + 4,
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301177 (int *)&high))) {
Lalit Chandivade4cd83cb2010-12-02 22:12:40 -08001178 rval = -1;
1179 goto exit_load_from_flash;
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301180 }
1181 data = ((u64)high << 32) | low ;
Vikas Chaudharyf8086f42012-08-22 07:54:59 -04001182 rval = qla4_82xx_pci_mem_write_2M(ha, memaddr, &data, 8);
Lalit Chandivade4cd83cb2010-12-02 22:12:40 -08001183 if (rval)
1184 goto exit_load_from_flash;
1185
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301186 flashaddr += 8;
1187 memaddr += 8;
1188
Lalit Chandivade4cd83cb2010-12-02 22:12:40 -08001189 if (i % 0x1000 == 0)
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301190 msleep(1);
1191
1192 }
1193
1194 udelay(100);
1195
1196 read_lock(&ha->hw_lock);
Vikas Chaudharyf8086f42012-08-22 07:54:59 -04001197 qla4_82xx_wr_32(ha, QLA82XX_CRB_PEG_NET_0 + 0x18, 0x1020);
1198 qla4_82xx_wr_32(ha, QLA82XX_ROMUSB_GLB_SW_RESET, 0x80001e);
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301199 read_unlock(&ha->hw_lock);
1200
Lalit Chandivade4cd83cb2010-12-02 22:12:40 -08001201exit_load_from_flash:
1202 return rval;
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301203}
1204
Vikas Chaudharyf8086f42012-08-22 07:54:59 -04001205static int qla4_82xx_load_fw(struct scsi_qla_host *ha, uint32_t image_start)
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301206{
1207 u32 rst;
1208
Vikas Chaudharyf8086f42012-08-22 07:54:59 -04001209 qla4_82xx_wr_32(ha, CRB_CMDPEG_STATE, 0);
1210 if (qla4_82xx_pinit_from_rom(ha, 0) != QLA_SUCCESS) {
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301211 printk(KERN_WARNING "%s: Error during CRB Initialization\n",
1212 __func__);
1213 return QLA_ERROR;
1214 }
1215
1216 udelay(500);
1217
1218 /* at this point, QM is in reset. This could be a problem if there are
1219 * incoming d* transition queue messages. QM/PCIE could wedge.
1220 * To get around this, QM is brought out of reset.
1221 */
1222
Vikas Chaudharyf8086f42012-08-22 07:54:59 -04001223 rst = qla4_82xx_rd_32(ha, QLA82XX_ROMUSB_GLB_SW_RESET);
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301224 /* unreset qm */
1225 rst &= ~(1 << 28);
Vikas Chaudharyf8086f42012-08-22 07:54:59 -04001226 qla4_82xx_wr_32(ha, QLA82XX_ROMUSB_GLB_SW_RESET, rst);
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301227
Vikas Chaudharyf8086f42012-08-22 07:54:59 -04001228 if (qla4_82xx_load_from_flash(ha, image_start)) {
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301229 printk("%s: Error trying to load fw from flash!\n", __func__);
1230 return QLA_ERROR;
1231 }
1232
1233 return QLA_SUCCESS;
1234}
1235
1236int
Vikas Chaudharyf8086f42012-08-22 07:54:59 -04001237qla4_82xx_pci_mem_read_2M(struct scsi_qla_host *ha,
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301238 u64 off, void *data, int size)
1239{
1240 int i, j = 0, k, start, end, loop, sz[2], off0[2];
1241 int shift_amount;
1242 uint32_t temp;
1243 uint64_t off8, val, mem_crb, word[2] = {0, 0};
1244
1245 /*
1246 * If not MN, go check for MS or invalid.
1247 */
1248
Vikas Chaudharyde8c72d2012-08-22 09:14:24 -04001249 if (off >= QLA8XXX_ADDR_QDR_NET && off <= QLA82XX_P3_ADDR_QDR_NET_MAX)
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301250 mem_crb = QLA82XX_CRB_QDR_NET;
1251 else {
1252 mem_crb = QLA82XX_CRB_DDR_NET;
Vikas Chaudharyf8086f42012-08-22 07:54:59 -04001253 if (qla4_82xx_pci_mem_bound_check(ha, off, size) == 0)
1254 return qla4_82xx_pci_mem_read_direct(ha,
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301255 off, data, size);
1256 }
1257
1258
1259 off8 = off & 0xfffffff0;
1260 off0[0] = off & 0xf;
1261 sz[0] = (size < (16 - off0[0])) ? size : (16 - off0[0]);
1262 shift_amount = 4;
1263
1264 loop = ((off0[0] + size - 1) >> shift_amount) + 1;
1265 off0[1] = 0;
1266 sz[1] = size - sz[0];
1267
1268 for (i = 0; i < loop; i++) {
1269 temp = off8 + (i << shift_amount);
Vikas Chaudharyf8086f42012-08-22 07:54:59 -04001270 qla4_82xx_wr_32(ha, mem_crb + MIU_TEST_AGT_ADDR_LO, temp);
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301271 temp = 0;
Vikas Chaudharyf8086f42012-08-22 07:54:59 -04001272 qla4_82xx_wr_32(ha, mem_crb + MIU_TEST_AGT_ADDR_HI, temp);
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301273 temp = MIU_TA_CTL_ENABLE;
Vikas Chaudharyf8086f42012-08-22 07:54:59 -04001274 qla4_82xx_wr_32(ha, mem_crb + MIU_TEST_AGT_CTRL, temp);
Vikas Chaudharyc38fa3a2012-08-22 07:55:03 -04001275 temp = MIU_TA_CTL_START_ENABLE;
Vikas Chaudharyf8086f42012-08-22 07:54:59 -04001276 qla4_82xx_wr_32(ha, mem_crb + MIU_TEST_AGT_CTRL, temp);
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301277
1278 for (j = 0; j < MAX_CTL_CHECK; j++) {
Vikas Chaudharyf8086f42012-08-22 07:54:59 -04001279 temp = qla4_82xx_rd_32(ha, mem_crb + MIU_TEST_AGT_CTRL);
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301280 if ((temp & MIU_TA_CTL_BUSY) == 0)
1281 break;
1282 }
1283
1284 if (j >= MAX_CTL_CHECK) {
Tej Parkash068237c82012-05-18 04:41:44 -04001285 printk_ratelimited(KERN_ERR
1286 "%s: failed to read through agent\n",
1287 __func__);
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301288 break;
1289 }
1290
1291 start = off0[i] >> 2;
1292 end = (off0[i] + sz[i] - 1) >> 2;
1293 for (k = start; k <= end; k++) {
Vikas Chaudharyf8086f42012-08-22 07:54:59 -04001294 temp = qla4_82xx_rd_32(ha,
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301295 mem_crb + MIU_TEST_AGT_RDDATA(k));
1296 word[i] |= ((uint64_t)temp << (32 * (k & 1)));
1297 }
1298 }
1299
1300 if (j >= MAX_CTL_CHECK)
1301 return -1;
1302
1303 if ((off0[0] & 7) == 0) {
1304 val = word[0];
1305 } else {
1306 val = ((word[0] >> (off0[0] * 8)) & (~(~0ULL << (sz[0] * 8)))) |
1307 ((word[1] & (~(~0ULL << (sz[1] * 8)))) << (sz[0] * 8));
1308 }
1309
1310 switch (size) {
1311 case 1:
1312 *(uint8_t *)data = val;
1313 break;
1314 case 2:
1315 *(uint16_t *)data = val;
1316 break;
1317 case 4:
1318 *(uint32_t *)data = val;
1319 break;
1320 case 8:
1321 *(uint64_t *)data = val;
1322 break;
1323 }
1324 return 0;
1325}
1326
1327int
Vikas Chaudharyf8086f42012-08-22 07:54:59 -04001328qla4_82xx_pci_mem_write_2M(struct scsi_qla_host *ha,
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301329 u64 off, void *data, int size)
1330{
1331 int i, j, ret = 0, loop, sz[2], off0;
1332 int scale, shift_amount, startword;
1333 uint32_t temp;
1334 uint64_t off8, mem_crb, tmpw, word[2] = {0, 0};
1335
1336 /*
1337 * If not MN, go check for MS or invalid.
1338 */
Vikas Chaudharyde8c72d2012-08-22 09:14:24 -04001339 if (off >= QLA8XXX_ADDR_QDR_NET && off <= QLA82XX_P3_ADDR_QDR_NET_MAX)
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301340 mem_crb = QLA82XX_CRB_QDR_NET;
1341 else {
1342 mem_crb = QLA82XX_CRB_DDR_NET;
Vikas Chaudharyf8086f42012-08-22 07:54:59 -04001343 if (qla4_82xx_pci_mem_bound_check(ha, off, size) == 0)
1344 return qla4_82xx_pci_mem_write_direct(ha,
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301345 off, data, size);
1346 }
1347
1348 off0 = off & 0x7;
1349 sz[0] = (size < (8 - off0)) ? size : (8 - off0);
1350 sz[1] = size - sz[0];
1351
1352 off8 = off & 0xfffffff0;
1353 loop = (((off & 0xf) + size - 1) >> 4) + 1;
1354 shift_amount = 4;
1355 scale = 2;
1356 startword = (off & 0xf)/8;
1357
1358 for (i = 0; i < loop; i++) {
Vikas Chaudharyf8086f42012-08-22 07:54:59 -04001359 if (qla4_82xx_pci_mem_read_2M(ha, off8 +
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301360 (i << shift_amount), &word[i * scale], 8))
1361 return -1;
1362 }
1363
1364 switch (size) {
1365 case 1:
1366 tmpw = *((uint8_t *)data);
1367 break;
1368 case 2:
1369 tmpw = *((uint16_t *)data);
1370 break;
1371 case 4:
1372 tmpw = *((uint32_t *)data);
1373 break;
1374 case 8:
1375 default:
1376 tmpw = *((uint64_t *)data);
1377 break;
1378 }
1379
1380 if (sz[0] == 8)
1381 word[startword] = tmpw;
1382 else {
1383 word[startword] &=
1384 ~((~(~0ULL << (sz[0] * 8))) << (off0 * 8));
1385 word[startword] |= tmpw << (off0 * 8);
1386 }
1387
1388 if (sz[1] != 0) {
1389 word[startword+1] &= ~(~0ULL << (sz[1] * 8));
1390 word[startword+1] |= tmpw >> (sz[0] * 8);
1391 }
1392
1393 for (i = 0; i < loop; i++) {
1394 temp = off8 + (i << shift_amount);
Vikas Chaudharyf8086f42012-08-22 07:54:59 -04001395 qla4_82xx_wr_32(ha, mem_crb+MIU_TEST_AGT_ADDR_LO, temp);
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301396 temp = 0;
Vikas Chaudharyf8086f42012-08-22 07:54:59 -04001397 qla4_82xx_wr_32(ha, mem_crb+MIU_TEST_AGT_ADDR_HI, temp);
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301398 temp = word[i * scale] & 0xffffffff;
Vikas Chaudharyf8086f42012-08-22 07:54:59 -04001399 qla4_82xx_wr_32(ha, mem_crb+MIU_TEST_AGT_WRDATA_LO, temp);
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301400 temp = (word[i * scale] >> 32) & 0xffffffff;
Vikas Chaudharyf8086f42012-08-22 07:54:59 -04001401 qla4_82xx_wr_32(ha, mem_crb+MIU_TEST_AGT_WRDATA_HI, temp);
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301402 temp = word[i*scale + 1] & 0xffffffff;
Vikas Chaudharyf8086f42012-08-22 07:54:59 -04001403 qla4_82xx_wr_32(ha, mem_crb + MIU_TEST_AGT_WRDATA_UPPER_LO,
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301404 temp);
1405 temp = (word[i*scale + 1] >> 32) & 0xffffffff;
Vikas Chaudharyf8086f42012-08-22 07:54:59 -04001406 qla4_82xx_wr_32(ha, mem_crb + MIU_TEST_AGT_WRDATA_UPPER_HI,
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301407 temp);
1408
Vikas Chaudharyc38fa3a2012-08-22 07:55:03 -04001409 temp = MIU_TA_CTL_WRITE_ENABLE;
Vikas Chaudharyf8086f42012-08-22 07:54:59 -04001410 qla4_82xx_wr_32(ha, mem_crb+MIU_TEST_AGT_CTRL, temp);
Vikas Chaudharyc38fa3a2012-08-22 07:55:03 -04001411 temp = MIU_TA_CTL_WRITE_START;
Vikas Chaudharyf8086f42012-08-22 07:54:59 -04001412 qla4_82xx_wr_32(ha, mem_crb+MIU_TEST_AGT_CTRL, temp);
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301413
1414 for (j = 0; j < MAX_CTL_CHECK; j++) {
Vikas Chaudharyf8086f42012-08-22 07:54:59 -04001415 temp = qla4_82xx_rd_32(ha, mem_crb + MIU_TEST_AGT_CTRL);
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301416 if ((temp & MIU_TA_CTL_BUSY) == 0)
1417 break;
1418 }
1419
1420 if (j >= MAX_CTL_CHECK) {
1421 if (printk_ratelimit())
1422 ql4_printk(KERN_ERR, ha,
Tej Parkash068237c82012-05-18 04:41:44 -04001423 "%s: failed to read through agent\n",
1424 __func__);
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301425 ret = -1;
1426 break;
1427 }
1428 }
1429
1430 return ret;
1431}
1432
Vikas Chaudharyf8086f42012-08-22 07:54:59 -04001433static int qla4_82xx_cmdpeg_ready(struct scsi_qla_host *ha, int pegtune_val)
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301434{
1435 u32 val = 0;
1436 int retries = 60;
1437
1438 if (!pegtune_val) {
1439 do {
Vikas Chaudharyf8086f42012-08-22 07:54:59 -04001440 val = qla4_82xx_rd_32(ha, CRB_CMDPEG_STATE);
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301441 if ((val == PHAN_INITIALIZE_COMPLETE) ||
1442 (val == PHAN_INITIALIZE_ACK))
1443 return 0;
1444 set_current_state(TASK_UNINTERRUPTIBLE);
1445 schedule_timeout(500);
1446
1447 } while (--retries);
1448
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301449 if (!retries) {
Vikas Chaudharyf8086f42012-08-22 07:54:59 -04001450 pegtune_val = qla4_82xx_rd_32(ha,
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301451 QLA82XX_ROMUSB_GLB_PEGTUNE_DONE);
1452 printk(KERN_WARNING "%s: init failed, "
1453 "pegtune_val = %x\n", __func__, pegtune_val);
1454 return -1;
1455 }
1456 }
1457 return 0;
1458}
1459
Vikas Chaudharyf8086f42012-08-22 07:54:59 -04001460static int qla4_82xx_rcvpeg_ready(struct scsi_qla_host *ha)
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301461{
1462 uint32_t state = 0;
1463 int loops = 0;
1464
1465 /* Window 1 call */
1466 read_lock(&ha->hw_lock);
Vikas Chaudharyf8086f42012-08-22 07:54:59 -04001467 state = qla4_82xx_rd_32(ha, CRB_RCVPEG_STATE);
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301468 read_unlock(&ha->hw_lock);
1469
1470 while ((state != PHAN_PEG_RCV_INITIALIZED) && (loops < 30000)) {
1471 udelay(100);
1472 /* Window 1 call */
1473 read_lock(&ha->hw_lock);
Vikas Chaudharyf8086f42012-08-22 07:54:59 -04001474 state = qla4_82xx_rd_32(ha, CRB_RCVPEG_STATE);
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301475 read_unlock(&ha->hw_lock);
1476
1477 loops++;
1478 }
1479
1480 if (loops >= 30000) {
1481 DEBUG2(ql4_printk(KERN_INFO, ha,
1482 "Receive Peg initialization not complete: 0x%x.\n", state));
1483 return QLA_ERROR;
1484 }
1485
1486 return QLA_SUCCESS;
1487}
1488
Andrew Morton626115c2010-08-19 14:13:42 -07001489void
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301490qla4_8xxx_set_drv_active(struct scsi_qla_host *ha)
1491{
1492 uint32_t drv_active;
1493
Vikas Chaudharyf8086f42012-08-22 07:54:59 -04001494 drv_active = qla4_82xx_rd_32(ha, QLA82XX_CRB_DRV_ACTIVE);
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301495 drv_active |= (1 << (ha->func_num * 4));
Tej Parkash068237c82012-05-18 04:41:44 -04001496 ql4_printk(KERN_INFO, ha, "%s(%ld): drv_active: 0x%08x\n",
1497 __func__, ha->host_no, drv_active);
Vikas Chaudharyf8086f42012-08-22 07:54:59 -04001498 qla4_82xx_wr_32(ha, QLA82XX_CRB_DRV_ACTIVE, drv_active);
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301499}
1500
1501void
1502qla4_8xxx_clear_drv_active(struct scsi_qla_host *ha)
1503{
1504 uint32_t drv_active;
1505
Vikas Chaudharyf8086f42012-08-22 07:54:59 -04001506 drv_active = qla4_82xx_rd_32(ha, QLA82XX_CRB_DRV_ACTIVE);
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301507 drv_active &= ~(1 << (ha->func_num * 4));
Tej Parkash068237c82012-05-18 04:41:44 -04001508 ql4_printk(KERN_INFO, ha, "%s(%ld): drv_active: 0x%08x\n",
1509 __func__, ha->host_no, drv_active);
Vikas Chaudharyf8086f42012-08-22 07:54:59 -04001510 qla4_82xx_wr_32(ha, QLA82XX_CRB_DRV_ACTIVE, drv_active);
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301511}
1512
1513static inline int
1514qla4_8xxx_need_reset(struct scsi_qla_host *ha)
1515{
Lalit Chandivade2232be02010-07-30 14:38:47 +05301516 uint32_t drv_state, drv_active;
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301517 int rval;
1518
Vikas Chaudharyf8086f42012-08-22 07:54:59 -04001519 drv_active = qla4_82xx_rd_32(ha, QLA82XX_CRB_DRV_ACTIVE);
1520 drv_state = qla4_82xx_rd_32(ha, QLA82XX_CRB_DRV_STATE);
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301521 rval = drv_state & (1 << (ha->func_num * 4));
Lalit Chandivade2232be02010-07-30 14:38:47 +05301522 if ((test_bit(AF_EEH_BUSY, &ha->flags)) && drv_active)
1523 rval = 1;
1524
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301525 return rval;
1526}
1527
1528static inline void
1529qla4_8xxx_set_rst_ready(struct scsi_qla_host *ha)
1530{
1531 uint32_t drv_state;
1532
Vikas Chaudharyf8086f42012-08-22 07:54:59 -04001533 drv_state = qla4_82xx_rd_32(ha, QLA82XX_CRB_DRV_STATE);
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301534 drv_state |= (1 << (ha->func_num * 4));
Tej Parkash068237c82012-05-18 04:41:44 -04001535 ql4_printk(KERN_INFO, ha, "%s(%ld): drv_state: 0x%08x\n",
1536 __func__, ha->host_no, drv_state);
Vikas Chaudharyf8086f42012-08-22 07:54:59 -04001537 qla4_82xx_wr_32(ha, QLA82XX_CRB_DRV_STATE, drv_state);
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301538}
1539
1540static inline void
1541qla4_8xxx_clear_rst_ready(struct scsi_qla_host *ha)
1542{
1543 uint32_t drv_state;
1544
Vikas Chaudharyf8086f42012-08-22 07:54:59 -04001545 drv_state = qla4_82xx_rd_32(ha, QLA82XX_CRB_DRV_STATE);
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301546 drv_state &= ~(1 << (ha->func_num * 4));
Tej Parkash068237c82012-05-18 04:41:44 -04001547 ql4_printk(KERN_INFO, ha, "%s(%ld): drv_state: 0x%08x\n",
1548 __func__, ha->host_no, drv_state);
Vikas Chaudharyf8086f42012-08-22 07:54:59 -04001549 qla4_82xx_wr_32(ha, QLA82XX_CRB_DRV_STATE, drv_state);
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301550}
1551
1552static inline void
1553qla4_8xxx_set_qsnt_ready(struct scsi_qla_host *ha)
1554{
1555 uint32_t qsnt_state;
1556
Vikas Chaudharyf8086f42012-08-22 07:54:59 -04001557 qsnt_state = qla4_82xx_rd_32(ha, QLA82XX_CRB_DRV_STATE);
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301558 qsnt_state |= (2 << (ha->func_num * 4));
Vikas Chaudharyf8086f42012-08-22 07:54:59 -04001559 qla4_82xx_wr_32(ha, QLA82XX_CRB_DRV_STATE, qsnt_state);
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301560}
1561
1562
1563static int
Vikas Chaudharyf8086f42012-08-22 07:54:59 -04001564qla4_82xx_start_firmware(struct scsi_qla_host *ha, uint32_t image_start)
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301565{
1566 int pcie_cap;
1567 uint16_t lnk;
1568
1569 /* scrub dma mask expansion register */
Vikas Chaudharyf8086f42012-08-22 07:54:59 -04001570 qla4_82xx_wr_32(ha, CRB_DMA_SHIFT, 0x55555555);
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301571
1572 /* Overwrite stale initialization register values */
Vikas Chaudharyf8086f42012-08-22 07:54:59 -04001573 qla4_82xx_wr_32(ha, CRB_CMDPEG_STATE, 0);
1574 qla4_82xx_wr_32(ha, CRB_RCVPEG_STATE, 0);
1575 qla4_82xx_wr_32(ha, QLA82XX_PEG_HALT_STATUS1, 0);
1576 qla4_82xx_wr_32(ha, QLA82XX_PEG_HALT_STATUS2, 0);
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301577
Vikas Chaudharyf8086f42012-08-22 07:54:59 -04001578 if (qla4_82xx_load_fw(ha, image_start) != QLA_SUCCESS) {
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301579 printk("%s: Error trying to start fw!\n", __func__);
1580 return QLA_ERROR;
1581 }
1582
1583 /* Handshake with the card before we register the devices. */
Vikas Chaudharyf8086f42012-08-22 07:54:59 -04001584 if (qla4_82xx_cmdpeg_ready(ha, 0) != QLA_SUCCESS) {
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301585 printk("%s: Error during card handshake!\n", __func__);
1586 return QLA_ERROR;
1587 }
1588
1589 /* Negotiated Link width */
Jon Mason983bfb52012-07-10 14:57:55 -07001590 pcie_cap = pci_pcie_cap(ha->pdev);
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301591 pci_read_config_word(ha->pdev, pcie_cap + PCI_EXP_LNKSTA, &lnk);
1592 ha->link_width = (lnk >> 4) & 0x3f;
1593
1594 /* Synchronize with Receive peg */
Vikas Chaudharyf8086f42012-08-22 07:54:59 -04001595 return qla4_82xx_rcvpeg_ready(ha);
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301596}
1597
1598static int
Vikas Chaudharyf8086f42012-08-22 07:54:59 -04001599qla4_82xx_try_start_fw(struct scsi_qla_host *ha)
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301600{
1601 int rval = QLA_ERROR;
1602
1603 /*
1604 * FW Load priority:
1605 * 1) Operational firmware residing in flash.
1606 * 2) Fail
1607 */
1608
1609 ql4_printk(KERN_INFO, ha,
1610 "FW: Retrieving flash offsets from FLT/FDT ...\n");
1611 rval = qla4_8xxx_get_flash_info(ha);
1612 if (rval != QLA_SUCCESS)
1613 return rval;
1614
1615 ql4_printk(KERN_INFO, ha,
1616 "FW: Attempting to load firmware from flash...\n");
Vikas Chaudharyf8086f42012-08-22 07:54:59 -04001617 rval = qla4_82xx_start_firmware(ha, ha->hw.flt_region_fw);
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301618
Vikas Chaudharyf581a3f2010-10-06 22:47:48 -07001619 if (rval != QLA_SUCCESS) {
1620 ql4_printk(KERN_ERR, ha, "FW: Load firmware from flash"
1621 " FAILED...\n");
1622 return rval;
1623 }
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05301624
1625 return rval;
1626}
1627
Vikas Chaudharyf8086f42012-08-22 07:54:59 -04001628static void qla4_82xx_rom_lock_recovery(struct scsi_qla_host *ha)
Shyam Sundarb25ee662010-10-06 22:50:51 -07001629{
Vikas Chaudharyf8086f42012-08-22 07:54:59 -04001630 if (qla4_82xx_rom_lock(ha)) {
Shyam Sundarb25ee662010-10-06 22:50:51 -07001631 /* Someone else is holding the lock. */
1632 dev_info(&ha->pdev->dev, "Resetting rom_lock\n");
1633 }
1634
1635 /*
1636 * Either we got the lock, or someone
1637 * else died while holding it.
1638 * In either case, unlock.
1639 */
Vikas Chaudharyf8086f42012-08-22 07:54:59 -04001640 qla4_82xx_rom_unlock(ha);
Shyam Sundarb25ee662010-10-06 22:50:51 -07001641}
1642
Tej Parkash068237c82012-05-18 04:41:44 -04001643static void qla4_8xxx_minidump_process_rdcrb(struct scsi_qla_host *ha,
Vikas Chaudhary7664a1f2012-08-22 07:55:00 -04001644 struct qla8xxx_minidump_entry_hdr *entry_hdr,
Tej Parkash068237c82012-05-18 04:41:44 -04001645 uint32_t **d_ptr)
1646{
1647 uint32_t r_addr, r_stride, loop_cnt, i, r_value;
Vikas Chaudhary7664a1f2012-08-22 07:55:00 -04001648 struct qla8xxx_minidump_entry_crb *crb_hdr;
Tej Parkash068237c82012-05-18 04:41:44 -04001649 uint32_t *data_ptr = *d_ptr;
1650
1651 DEBUG2(ql4_printk(KERN_INFO, ha, "Entering fn: %s\n", __func__));
Vikas Chaudhary7664a1f2012-08-22 07:55:00 -04001652 crb_hdr = (struct qla8xxx_minidump_entry_crb *)entry_hdr;
Tej Parkash068237c82012-05-18 04:41:44 -04001653 r_addr = crb_hdr->addr;
1654 r_stride = crb_hdr->crb_strd.addr_stride;
1655 loop_cnt = crb_hdr->op_count;
1656
1657 for (i = 0; i < loop_cnt; i++) {
1658 r_value = qla4_8xxx_md_rw_32(ha, r_addr, 0, 0);
1659 *data_ptr++ = cpu_to_le32(r_addr);
1660 *data_ptr++ = cpu_to_le32(r_value);
1661 r_addr += r_stride;
1662 }
1663 *d_ptr = data_ptr;
1664}
1665
1666static int qla4_8xxx_minidump_process_l2tag(struct scsi_qla_host *ha,
Vikas Chaudhary7664a1f2012-08-22 07:55:00 -04001667 struct qla8xxx_minidump_entry_hdr *entry_hdr,
Tej Parkash068237c82012-05-18 04:41:44 -04001668 uint32_t **d_ptr)
1669{
1670 uint32_t addr, r_addr, c_addr, t_r_addr;
1671 uint32_t i, k, loop_count, t_value, r_cnt, r_value;
1672 unsigned long p_wait, w_time, p_mask;
1673 uint32_t c_value_w, c_value_r;
Vikas Chaudhary7664a1f2012-08-22 07:55:00 -04001674 struct qla8xxx_minidump_entry_cache *cache_hdr;
Tej Parkash068237c82012-05-18 04:41:44 -04001675 int rval = QLA_ERROR;
1676 uint32_t *data_ptr = *d_ptr;
1677
1678 DEBUG2(ql4_printk(KERN_INFO, ha, "Entering fn: %s\n", __func__));
Vikas Chaudhary7664a1f2012-08-22 07:55:00 -04001679 cache_hdr = (struct qla8xxx_minidump_entry_cache *)entry_hdr;
Tej Parkash068237c82012-05-18 04:41:44 -04001680
1681 loop_count = cache_hdr->op_count;
1682 r_addr = cache_hdr->read_addr;
1683 c_addr = cache_hdr->control_addr;
1684 c_value_w = cache_hdr->cache_ctrl.write_value;
1685
1686 t_r_addr = cache_hdr->tag_reg_addr;
1687 t_value = cache_hdr->addr_ctrl.init_tag_value;
1688 r_cnt = cache_hdr->read_ctrl.read_addr_cnt;
1689 p_wait = cache_hdr->cache_ctrl.poll_wait;
1690 p_mask = cache_hdr->cache_ctrl.poll_mask;
1691
1692 for (i = 0; i < loop_count; i++) {
1693 qla4_8xxx_md_rw_32(ha, t_r_addr, t_value, 1);
1694
1695 if (c_value_w)
1696 qla4_8xxx_md_rw_32(ha, c_addr, c_value_w, 1);
1697
1698 if (p_mask) {
1699 w_time = jiffies + p_wait;
1700 do {
1701 c_value_r = qla4_8xxx_md_rw_32(ha, c_addr,
1702 0, 0);
1703 if ((c_value_r & p_mask) == 0) {
1704 break;
1705 } else if (time_after_eq(jiffies, w_time)) {
1706 /* capturing dump failed */
1707 return rval;
1708 }
1709 } while (1);
1710 }
1711
1712 addr = r_addr;
1713 for (k = 0; k < r_cnt; k++) {
1714 r_value = qla4_8xxx_md_rw_32(ha, addr, 0, 0);
1715 *data_ptr++ = cpu_to_le32(r_value);
1716 addr += cache_hdr->read_ctrl.read_addr_stride;
1717 }
1718
1719 t_value += cache_hdr->addr_ctrl.tag_value_stride;
1720 }
1721 *d_ptr = data_ptr;
1722 return QLA_SUCCESS;
1723}
1724
1725static int qla4_8xxx_minidump_process_control(struct scsi_qla_host *ha,
Vikas Chaudhary7664a1f2012-08-22 07:55:00 -04001726 struct qla8xxx_minidump_entry_hdr *entry_hdr)
Tej Parkash068237c82012-05-18 04:41:44 -04001727{
Vikas Chaudhary7664a1f2012-08-22 07:55:00 -04001728 struct qla8xxx_minidump_entry_crb *crb_entry;
Tej Parkash068237c82012-05-18 04:41:44 -04001729 uint32_t read_value, opcode, poll_time, addr, index, rval = QLA_SUCCESS;
1730 uint32_t crb_addr;
1731 unsigned long wtime;
1732 struct qla4_8xxx_minidump_template_hdr *tmplt_hdr;
1733 int i;
1734
1735 DEBUG2(ql4_printk(KERN_INFO, ha, "Entering fn: %s\n", __func__));
1736 tmplt_hdr = (struct qla4_8xxx_minidump_template_hdr *)
1737 ha->fw_dump_tmplt_hdr;
Vikas Chaudhary7664a1f2012-08-22 07:55:00 -04001738 crb_entry = (struct qla8xxx_minidump_entry_crb *)entry_hdr;
Tej Parkash068237c82012-05-18 04:41:44 -04001739
1740 crb_addr = crb_entry->addr;
1741 for (i = 0; i < crb_entry->op_count; i++) {
1742 opcode = crb_entry->crb_ctrl.opcode;
Vikas Chaudharyde8c72d2012-08-22 09:14:24 -04001743 if (opcode & QLA8XXX_DBG_OPCODE_WR) {
Tej Parkash068237c82012-05-18 04:41:44 -04001744 qla4_8xxx_md_rw_32(ha, crb_addr,
1745 crb_entry->value_1, 1);
Vikas Chaudharyde8c72d2012-08-22 09:14:24 -04001746 opcode &= ~QLA8XXX_DBG_OPCODE_WR;
Tej Parkash068237c82012-05-18 04:41:44 -04001747 }
Vikas Chaudharyde8c72d2012-08-22 09:14:24 -04001748 if (opcode & QLA8XXX_DBG_OPCODE_RW) {
Tej Parkash068237c82012-05-18 04:41:44 -04001749 read_value = qla4_8xxx_md_rw_32(ha, crb_addr, 0, 0);
1750 qla4_8xxx_md_rw_32(ha, crb_addr, read_value, 1);
Vikas Chaudharyde8c72d2012-08-22 09:14:24 -04001751 opcode &= ~QLA8XXX_DBG_OPCODE_RW;
Tej Parkash068237c82012-05-18 04:41:44 -04001752 }
Vikas Chaudharyde8c72d2012-08-22 09:14:24 -04001753 if (opcode & QLA8XXX_DBG_OPCODE_AND) {
Tej Parkash068237c82012-05-18 04:41:44 -04001754 read_value = qla4_8xxx_md_rw_32(ha, crb_addr, 0, 0);
1755 read_value &= crb_entry->value_2;
Vikas Chaudharyde8c72d2012-08-22 09:14:24 -04001756 opcode &= ~QLA8XXX_DBG_OPCODE_AND;
1757 if (opcode & QLA8XXX_DBG_OPCODE_OR) {
Tej Parkash068237c82012-05-18 04:41:44 -04001758 read_value |= crb_entry->value_3;
Vikas Chaudharyde8c72d2012-08-22 09:14:24 -04001759 opcode &= ~QLA8XXX_DBG_OPCODE_OR;
Tej Parkash068237c82012-05-18 04:41:44 -04001760 }
1761 qla4_8xxx_md_rw_32(ha, crb_addr, read_value, 1);
1762 }
Vikas Chaudharyde8c72d2012-08-22 09:14:24 -04001763 if (opcode & QLA8XXX_DBG_OPCODE_OR) {
Tej Parkash068237c82012-05-18 04:41:44 -04001764 read_value = qla4_8xxx_md_rw_32(ha, crb_addr, 0, 0);
1765 read_value |= crb_entry->value_3;
1766 qla4_8xxx_md_rw_32(ha, crb_addr, read_value, 1);
Vikas Chaudharyde8c72d2012-08-22 09:14:24 -04001767 opcode &= ~QLA8XXX_DBG_OPCODE_OR;
Tej Parkash068237c82012-05-18 04:41:44 -04001768 }
Vikas Chaudharyde8c72d2012-08-22 09:14:24 -04001769 if (opcode & QLA8XXX_DBG_OPCODE_POLL) {
Tej Parkash068237c82012-05-18 04:41:44 -04001770 poll_time = crb_entry->crb_strd.poll_timeout;
1771 wtime = jiffies + poll_time;
1772 read_value = qla4_8xxx_md_rw_32(ha, crb_addr, 0, 0);
1773
1774 do {
1775 if ((read_value & crb_entry->value_2) ==
1776 crb_entry->value_1)
1777 break;
1778 else if (time_after_eq(jiffies, wtime)) {
1779 /* capturing dump failed */
1780 rval = QLA_ERROR;
1781 break;
1782 } else
1783 read_value = qla4_8xxx_md_rw_32(ha,
1784 crb_addr, 0, 0);
1785 } while (1);
Vikas Chaudharyde8c72d2012-08-22 09:14:24 -04001786 opcode &= ~QLA8XXX_DBG_OPCODE_POLL;
Tej Parkash068237c82012-05-18 04:41:44 -04001787 }
1788
Vikas Chaudharyde8c72d2012-08-22 09:14:24 -04001789 if (opcode & QLA8XXX_DBG_OPCODE_RDSTATE) {
Tej Parkash068237c82012-05-18 04:41:44 -04001790 if (crb_entry->crb_strd.state_index_a) {
1791 index = crb_entry->crb_strd.state_index_a;
1792 addr = tmplt_hdr->saved_state_array[index];
1793 } else {
1794 addr = crb_addr;
1795 }
1796
1797 read_value = qla4_8xxx_md_rw_32(ha, addr, 0, 0);
1798 index = crb_entry->crb_ctrl.state_index_v;
1799 tmplt_hdr->saved_state_array[index] = read_value;
Vikas Chaudharyde8c72d2012-08-22 09:14:24 -04001800 opcode &= ~QLA8XXX_DBG_OPCODE_RDSTATE;
Tej Parkash068237c82012-05-18 04:41:44 -04001801 }
1802
Vikas Chaudharyde8c72d2012-08-22 09:14:24 -04001803 if (opcode & QLA8XXX_DBG_OPCODE_WRSTATE) {
Tej Parkash068237c82012-05-18 04:41:44 -04001804 if (crb_entry->crb_strd.state_index_a) {
1805 index = crb_entry->crb_strd.state_index_a;
1806 addr = tmplt_hdr->saved_state_array[index];
1807 } else {
1808 addr = crb_addr;
1809 }
1810
1811 if (crb_entry->crb_ctrl.state_index_v) {
1812 index = crb_entry->crb_ctrl.state_index_v;
1813 read_value =
1814 tmplt_hdr->saved_state_array[index];
1815 } else {
1816 read_value = crb_entry->value_1;
1817 }
1818
1819 qla4_8xxx_md_rw_32(ha, addr, read_value, 1);
Vikas Chaudharyde8c72d2012-08-22 09:14:24 -04001820 opcode &= ~QLA8XXX_DBG_OPCODE_WRSTATE;
Tej Parkash068237c82012-05-18 04:41:44 -04001821 }
1822
Vikas Chaudharyde8c72d2012-08-22 09:14:24 -04001823 if (opcode & QLA8XXX_DBG_OPCODE_MDSTATE) {
Tej Parkash068237c82012-05-18 04:41:44 -04001824 index = crb_entry->crb_ctrl.state_index_v;
1825 read_value = tmplt_hdr->saved_state_array[index];
1826 read_value <<= crb_entry->crb_ctrl.shl;
1827 read_value >>= crb_entry->crb_ctrl.shr;
1828 if (crb_entry->value_2)
1829 read_value &= crb_entry->value_2;
1830 read_value |= crb_entry->value_3;
1831 read_value += crb_entry->value_1;
1832 tmplt_hdr->saved_state_array[index] = read_value;
Vikas Chaudharyde8c72d2012-08-22 09:14:24 -04001833 opcode &= ~QLA8XXX_DBG_OPCODE_MDSTATE;
Tej Parkash068237c82012-05-18 04:41:44 -04001834 }
1835 crb_addr += crb_entry->crb_strd.addr_stride;
1836 }
1837 DEBUG2(ql4_printk(KERN_INFO, ha, "Leaving fn: %s\n", __func__));
1838 return rval;
1839}
1840
1841static void qla4_8xxx_minidump_process_rdocm(struct scsi_qla_host *ha,
Vikas Chaudhary7664a1f2012-08-22 07:55:00 -04001842 struct qla8xxx_minidump_entry_hdr *entry_hdr,
Tej Parkash068237c82012-05-18 04:41:44 -04001843 uint32_t **d_ptr)
1844{
1845 uint32_t r_addr, r_stride, loop_cnt, i, r_value;
Vikas Chaudhary7664a1f2012-08-22 07:55:00 -04001846 struct qla8xxx_minidump_entry_rdocm *ocm_hdr;
Tej Parkash068237c82012-05-18 04:41:44 -04001847 uint32_t *data_ptr = *d_ptr;
1848
1849 DEBUG2(ql4_printk(KERN_INFO, ha, "Entering fn: %s\n", __func__));
Vikas Chaudhary7664a1f2012-08-22 07:55:00 -04001850 ocm_hdr = (struct qla8xxx_minidump_entry_rdocm *)entry_hdr;
Tej Parkash068237c82012-05-18 04:41:44 -04001851 r_addr = ocm_hdr->read_addr;
1852 r_stride = ocm_hdr->read_addr_stride;
1853 loop_cnt = ocm_hdr->op_count;
1854
1855 DEBUG2(ql4_printk(KERN_INFO, ha,
1856 "[%s]: r_addr: 0x%x, r_stride: 0x%x, loop_cnt: 0x%x\n",
1857 __func__, r_addr, r_stride, loop_cnt));
1858
1859 for (i = 0; i < loop_cnt; i++) {
1860 r_value = readl((void __iomem *)(r_addr + ha->nx_pcibase));
1861 *data_ptr++ = cpu_to_le32(r_value);
1862 r_addr += r_stride;
1863 }
1864 DEBUG2(ql4_printk(KERN_INFO, ha, "Leaving fn: %s datacount: 0x%lx\n",
Vikas Chaudhary26fdf922012-08-07 07:57:14 -04001865 __func__, (long unsigned int) (loop_cnt * sizeof(uint32_t))));
Tej Parkash068237c82012-05-18 04:41:44 -04001866 *d_ptr = data_ptr;
1867}
1868
1869static void qla4_8xxx_minidump_process_rdmux(struct scsi_qla_host *ha,
Vikas Chaudhary7664a1f2012-08-22 07:55:00 -04001870 struct qla8xxx_minidump_entry_hdr *entry_hdr,
Tej Parkash068237c82012-05-18 04:41:44 -04001871 uint32_t **d_ptr)
1872{
1873 uint32_t r_addr, s_stride, s_addr, s_value, loop_cnt, i, r_value;
Vikas Chaudhary7664a1f2012-08-22 07:55:00 -04001874 struct qla8xxx_minidump_entry_mux *mux_hdr;
Tej Parkash068237c82012-05-18 04:41:44 -04001875 uint32_t *data_ptr = *d_ptr;
1876
1877 DEBUG2(ql4_printk(KERN_INFO, ha, "Entering fn: %s\n", __func__));
Vikas Chaudhary7664a1f2012-08-22 07:55:00 -04001878 mux_hdr = (struct qla8xxx_minidump_entry_mux *)entry_hdr;
Tej Parkash068237c82012-05-18 04:41:44 -04001879 r_addr = mux_hdr->read_addr;
1880 s_addr = mux_hdr->select_addr;
1881 s_stride = mux_hdr->select_value_stride;
1882 s_value = mux_hdr->select_value;
1883 loop_cnt = mux_hdr->op_count;
1884
1885 for (i = 0; i < loop_cnt; i++) {
1886 qla4_8xxx_md_rw_32(ha, s_addr, s_value, 1);
1887 r_value = qla4_8xxx_md_rw_32(ha, r_addr, 0, 0);
1888 *data_ptr++ = cpu_to_le32(s_value);
1889 *data_ptr++ = cpu_to_le32(r_value);
1890 s_value += s_stride;
1891 }
1892 *d_ptr = data_ptr;
1893}
1894
1895static void qla4_8xxx_minidump_process_l1cache(struct scsi_qla_host *ha,
Vikas Chaudhary7664a1f2012-08-22 07:55:00 -04001896 struct qla8xxx_minidump_entry_hdr *entry_hdr,
Tej Parkash068237c82012-05-18 04:41:44 -04001897 uint32_t **d_ptr)
1898{
1899 uint32_t addr, r_addr, c_addr, t_r_addr;
1900 uint32_t i, k, loop_count, t_value, r_cnt, r_value;
1901 uint32_t c_value_w;
Vikas Chaudhary7664a1f2012-08-22 07:55:00 -04001902 struct qla8xxx_minidump_entry_cache *cache_hdr;
Tej Parkash068237c82012-05-18 04:41:44 -04001903 uint32_t *data_ptr = *d_ptr;
1904
Vikas Chaudhary7664a1f2012-08-22 07:55:00 -04001905 cache_hdr = (struct qla8xxx_minidump_entry_cache *)entry_hdr;
Tej Parkash068237c82012-05-18 04:41:44 -04001906 loop_count = cache_hdr->op_count;
1907 r_addr = cache_hdr->read_addr;
1908 c_addr = cache_hdr->control_addr;
1909 c_value_w = cache_hdr->cache_ctrl.write_value;
1910
1911 t_r_addr = cache_hdr->tag_reg_addr;
1912 t_value = cache_hdr->addr_ctrl.init_tag_value;
1913 r_cnt = cache_hdr->read_ctrl.read_addr_cnt;
1914
1915 for (i = 0; i < loop_count; i++) {
1916 qla4_8xxx_md_rw_32(ha, t_r_addr, t_value, 1);
1917 qla4_8xxx_md_rw_32(ha, c_addr, c_value_w, 1);
1918 addr = r_addr;
1919 for (k = 0; k < r_cnt; k++) {
1920 r_value = qla4_8xxx_md_rw_32(ha, addr, 0, 0);
1921 *data_ptr++ = cpu_to_le32(r_value);
1922 addr += cache_hdr->read_ctrl.read_addr_stride;
1923 }
1924 t_value += cache_hdr->addr_ctrl.tag_value_stride;
1925 }
1926 *d_ptr = data_ptr;
1927}
1928
1929static void qla4_8xxx_minidump_process_queue(struct scsi_qla_host *ha,
Vikas Chaudhary7664a1f2012-08-22 07:55:00 -04001930 struct qla8xxx_minidump_entry_hdr *entry_hdr,
Tej Parkash068237c82012-05-18 04:41:44 -04001931 uint32_t **d_ptr)
1932{
1933 uint32_t s_addr, r_addr;
1934 uint32_t r_stride, r_value, r_cnt, qid = 0;
1935 uint32_t i, k, loop_cnt;
Vikas Chaudhary7664a1f2012-08-22 07:55:00 -04001936 struct qla8xxx_minidump_entry_queue *q_hdr;
Tej Parkash068237c82012-05-18 04:41:44 -04001937 uint32_t *data_ptr = *d_ptr;
1938
1939 DEBUG2(ql4_printk(KERN_INFO, ha, "Entering fn: %s\n", __func__));
Vikas Chaudhary7664a1f2012-08-22 07:55:00 -04001940 q_hdr = (struct qla8xxx_minidump_entry_queue *)entry_hdr;
Tej Parkash068237c82012-05-18 04:41:44 -04001941 s_addr = q_hdr->select_addr;
1942 r_cnt = q_hdr->rd_strd.read_addr_cnt;
1943 r_stride = q_hdr->rd_strd.read_addr_stride;
1944 loop_cnt = q_hdr->op_count;
1945
1946 for (i = 0; i < loop_cnt; i++) {
1947 qla4_8xxx_md_rw_32(ha, s_addr, qid, 1);
1948 r_addr = q_hdr->read_addr;
1949 for (k = 0; k < r_cnt; k++) {
1950 r_value = qla4_8xxx_md_rw_32(ha, r_addr, 0, 0);
1951 *data_ptr++ = cpu_to_le32(r_value);
1952 r_addr += r_stride;
1953 }
1954 qid += q_hdr->q_strd.queue_id_stride;
1955 }
1956 *d_ptr = data_ptr;
1957}
1958
1959#define MD_DIRECT_ROM_WINDOW 0x42110030
1960#define MD_DIRECT_ROM_READ_BASE 0x42150000
1961
Vikas Chaudharyf8086f42012-08-22 07:54:59 -04001962static void qla4_82xx_minidump_process_rdrom(struct scsi_qla_host *ha,
Vikas Chaudhary7664a1f2012-08-22 07:55:00 -04001963 struct qla8xxx_minidump_entry_hdr *entry_hdr,
Tej Parkash068237c82012-05-18 04:41:44 -04001964 uint32_t **d_ptr)
1965{
1966 uint32_t r_addr, r_value;
1967 uint32_t i, loop_cnt;
Vikas Chaudhary7664a1f2012-08-22 07:55:00 -04001968 struct qla8xxx_minidump_entry_rdrom *rom_hdr;
Tej Parkash068237c82012-05-18 04:41:44 -04001969 uint32_t *data_ptr = *d_ptr;
1970
1971 DEBUG2(ql4_printk(KERN_INFO, ha, "Entering fn: %s\n", __func__));
Vikas Chaudhary7664a1f2012-08-22 07:55:00 -04001972 rom_hdr = (struct qla8xxx_minidump_entry_rdrom *)entry_hdr;
Tej Parkash068237c82012-05-18 04:41:44 -04001973 r_addr = rom_hdr->read_addr;
1974 loop_cnt = rom_hdr->read_data_size/sizeof(uint32_t);
1975
1976 DEBUG2(ql4_printk(KERN_INFO, ha,
1977 "[%s]: flash_addr: 0x%x, read_data_size: 0x%x\n",
1978 __func__, r_addr, loop_cnt));
1979
1980 for (i = 0; i < loop_cnt; i++) {
1981 qla4_8xxx_md_rw_32(ha, MD_DIRECT_ROM_WINDOW,
1982 (r_addr & 0xFFFF0000), 1);
1983 r_value = qla4_8xxx_md_rw_32(ha,
1984 MD_DIRECT_ROM_READ_BASE +
1985 (r_addr & 0x0000FFFF), 0, 0);
1986 *data_ptr++ = cpu_to_le32(r_value);
1987 r_addr += sizeof(uint32_t);
1988 }
1989 *d_ptr = data_ptr;
1990}
1991
1992#define MD_MIU_TEST_AGT_CTRL 0x41000090
1993#define MD_MIU_TEST_AGT_ADDR_LO 0x41000094
1994#define MD_MIU_TEST_AGT_ADDR_HI 0x41000098
1995
1996static int qla4_8xxx_minidump_process_rdmem(struct scsi_qla_host *ha,
Vikas Chaudhary7664a1f2012-08-22 07:55:00 -04001997 struct qla8xxx_minidump_entry_hdr *entry_hdr,
Tej Parkash068237c82012-05-18 04:41:44 -04001998 uint32_t **d_ptr)
1999{
2000 uint32_t r_addr, r_value, r_data;
2001 uint32_t i, j, loop_cnt;
Vikas Chaudhary7664a1f2012-08-22 07:55:00 -04002002 struct qla8xxx_minidump_entry_rdmem *m_hdr;
Tej Parkash068237c82012-05-18 04:41:44 -04002003 unsigned long flags;
2004 uint32_t *data_ptr = *d_ptr;
2005
2006 DEBUG2(ql4_printk(KERN_INFO, ha, "Entering fn: %s\n", __func__));
Vikas Chaudhary7664a1f2012-08-22 07:55:00 -04002007 m_hdr = (struct qla8xxx_minidump_entry_rdmem *)entry_hdr;
Tej Parkash068237c82012-05-18 04:41:44 -04002008 r_addr = m_hdr->read_addr;
2009 loop_cnt = m_hdr->read_data_size/16;
2010
2011 DEBUG2(ql4_printk(KERN_INFO, ha,
2012 "[%s]: Read addr: 0x%x, read_data_size: 0x%x\n",
2013 __func__, r_addr, m_hdr->read_data_size));
2014
2015 if (r_addr & 0xf) {
2016 DEBUG2(ql4_printk(KERN_INFO, ha,
2017 "[%s]: Read addr 0x%x not 16 bytes alligned\n",
2018 __func__, r_addr));
2019 return QLA_ERROR;
2020 }
2021
2022 if (m_hdr->read_data_size % 16) {
2023 DEBUG2(ql4_printk(KERN_INFO, ha,
2024 "[%s]: Read data[0x%x] not multiple of 16 bytes\n",
2025 __func__, m_hdr->read_data_size));
2026 return QLA_ERROR;
2027 }
2028
2029 DEBUG2(ql4_printk(KERN_INFO, ha,
2030 "[%s]: rdmem_addr: 0x%x, read_data_size: 0x%x, loop_cnt: 0x%x\n",
2031 __func__, r_addr, m_hdr->read_data_size, loop_cnt));
2032
2033 write_lock_irqsave(&ha->hw_lock, flags);
2034 for (i = 0; i < loop_cnt; i++) {
2035 qla4_8xxx_md_rw_32(ha, MD_MIU_TEST_AGT_ADDR_LO, r_addr, 1);
2036 r_value = 0;
2037 qla4_8xxx_md_rw_32(ha, MD_MIU_TEST_AGT_ADDR_HI, r_value, 1);
2038 r_value = MIU_TA_CTL_ENABLE;
2039 qla4_8xxx_md_rw_32(ha, MD_MIU_TEST_AGT_CTRL, r_value, 1);
Vikas Chaudharyc38fa3a2012-08-22 07:55:03 -04002040 r_value = MIU_TA_CTL_START_ENABLE;
Tej Parkash068237c82012-05-18 04:41:44 -04002041 qla4_8xxx_md_rw_32(ha, MD_MIU_TEST_AGT_CTRL, r_value, 1);
2042
2043 for (j = 0; j < MAX_CTL_CHECK; j++) {
2044 r_value = qla4_8xxx_md_rw_32(ha, MD_MIU_TEST_AGT_CTRL,
2045 0, 0);
2046 if ((r_value & MIU_TA_CTL_BUSY) == 0)
2047 break;
2048 }
2049
2050 if (j >= MAX_CTL_CHECK) {
2051 printk_ratelimited(KERN_ERR
2052 "%s: failed to read through agent\n",
2053 __func__);
2054 write_unlock_irqrestore(&ha->hw_lock, flags);
2055 return QLA_SUCCESS;
2056 }
2057
2058 for (j = 0; j < 4; j++) {
2059 r_data = qla4_8xxx_md_rw_32(ha,
2060 MD_MIU_TEST_AGT_RDDATA[j],
2061 0, 0);
2062 *data_ptr++ = cpu_to_le32(r_data);
2063 }
2064
2065 r_addr += 16;
2066 }
2067 write_unlock_irqrestore(&ha->hw_lock, flags);
2068
2069 DEBUG2(ql4_printk(KERN_INFO, ha, "Leaving fn: %s datacount: 0x%x\n",
2070 __func__, (loop_cnt * 16)));
2071
2072 *d_ptr = data_ptr;
2073 return QLA_SUCCESS;
2074}
2075
Vikas Chaudhary5e9bcec2012-08-22 07:55:01 -04002076static void qla4_8xxx_mark_entry_skipped(struct scsi_qla_host *ha,
Vikas Chaudhary7664a1f2012-08-22 07:55:00 -04002077 struct qla8xxx_minidump_entry_hdr *entry_hdr,
Tej Parkash068237c82012-05-18 04:41:44 -04002078 int index)
2079{
Vikas Chaudharyde8c72d2012-08-22 09:14:24 -04002080 entry_hdr->d_ctrl.driver_flags |= QLA8XXX_DBG_SKIPPED_FLAG;
Tej Parkash068237c82012-05-18 04:41:44 -04002081 DEBUG2(ql4_printk(KERN_INFO, ha,
2082 "scsi(%ld): Skipping entry[%d]: ETYPE[0x%x]-ELEVEL[0x%x]\n",
2083 ha->host_no, index, entry_hdr->entry_type,
2084 entry_hdr->d_ctrl.entry_capture_mask));
2085}
2086
2087/**
Vikas Chaudharyf8086f42012-08-22 07:54:59 -04002088 * qla4_8xxx_collect_md_data - Retrieve firmware minidump data.
Tej Parkash068237c82012-05-18 04:41:44 -04002089 * @ha: pointer to adapter structure
2090 **/
2091static int qla4_8xxx_collect_md_data(struct scsi_qla_host *ha)
2092{
2093 int num_entry_hdr = 0;
Vikas Chaudhary7664a1f2012-08-22 07:55:00 -04002094 struct qla8xxx_minidump_entry_hdr *entry_hdr;
Tej Parkash068237c82012-05-18 04:41:44 -04002095 struct qla4_8xxx_minidump_template_hdr *tmplt_hdr;
2096 uint32_t *data_ptr;
2097 uint32_t data_collected = 0;
2098 int i, rval = QLA_ERROR;
2099 uint64_t now;
2100 uint32_t timestamp;
2101
2102 if (!ha->fw_dump) {
2103 ql4_printk(KERN_INFO, ha, "%s(%ld) No buffer to dump\n",
2104 __func__, ha->host_no);
2105 return rval;
2106 }
2107
2108 tmplt_hdr = (struct qla4_8xxx_minidump_template_hdr *)
2109 ha->fw_dump_tmplt_hdr;
2110 data_ptr = (uint32_t *)((uint8_t *)ha->fw_dump +
2111 ha->fw_dump_tmplt_size);
2112 data_collected += ha->fw_dump_tmplt_size;
2113
2114 num_entry_hdr = tmplt_hdr->num_of_entries;
2115 ql4_printk(KERN_INFO, ha, "[%s]: starting data ptr: %p\n",
2116 __func__, data_ptr);
2117 ql4_printk(KERN_INFO, ha,
2118 "[%s]: no of entry headers in Template: 0x%x\n",
2119 __func__, num_entry_hdr);
2120 ql4_printk(KERN_INFO, ha, "[%s]: Capture Mask obtained: 0x%x\n",
2121 __func__, ha->fw_dump_capture_mask);
2122 ql4_printk(KERN_INFO, ha, "[%s]: Total_data_size 0x%x, %d obtained\n",
2123 __func__, ha->fw_dump_size, ha->fw_dump_size);
2124
2125 /* Update current timestamp before taking dump */
2126 now = get_jiffies_64();
2127 timestamp = (u32)(jiffies_to_msecs(now) / 1000);
2128 tmplt_hdr->driver_timestamp = timestamp;
2129
Vikas Chaudhary7664a1f2012-08-22 07:55:00 -04002130 entry_hdr = (struct qla8xxx_minidump_entry_hdr *)
Tej Parkash068237c82012-05-18 04:41:44 -04002131 (((uint8_t *)ha->fw_dump_tmplt_hdr) +
2132 tmplt_hdr->first_entry_offset);
2133
2134 /* Walk through the entry headers - validate/perform required action */
2135 for (i = 0; i < num_entry_hdr; i++) {
2136 if (data_collected >= ha->fw_dump_size) {
2137 ql4_printk(KERN_INFO, ha,
2138 "Data collected: [0x%x], Total Dump size: [0x%x]\n",
2139 data_collected, ha->fw_dump_size);
2140 return rval;
2141 }
2142
2143 if (!(entry_hdr->d_ctrl.entry_capture_mask &
2144 ha->fw_dump_capture_mask)) {
2145 entry_hdr->d_ctrl.driver_flags |=
Vikas Chaudharyde8c72d2012-08-22 09:14:24 -04002146 QLA8XXX_DBG_SKIPPED_FLAG;
Tej Parkash068237c82012-05-18 04:41:44 -04002147 goto skip_nxt_entry;
2148 }
2149
2150 DEBUG2(ql4_printk(KERN_INFO, ha,
2151 "Data collected: [0x%x], Dump size left:[0x%x]\n",
2152 data_collected,
2153 (ha->fw_dump_size - data_collected)));
2154
2155 /* Decode the entry type and take required action to capture
2156 * debug data
2157 */
2158 switch (entry_hdr->entry_type) {
Vikas Chaudharyde8c72d2012-08-22 09:14:24 -04002159 case QLA8XXX_RDEND:
Vikas Chaudhary5e9bcec2012-08-22 07:55:01 -04002160 qla4_8xxx_mark_entry_skipped(ha, entry_hdr, i);
Tej Parkash068237c82012-05-18 04:41:44 -04002161 break;
Vikas Chaudharyde8c72d2012-08-22 09:14:24 -04002162 case QLA8XXX_CNTRL:
Tej Parkash068237c82012-05-18 04:41:44 -04002163 rval = qla4_8xxx_minidump_process_control(ha,
2164 entry_hdr);
2165 if (rval != QLA_SUCCESS) {
Vikas Chaudhary5e9bcec2012-08-22 07:55:01 -04002166 qla4_8xxx_mark_entry_skipped(ha, entry_hdr, i);
Tej Parkash068237c82012-05-18 04:41:44 -04002167 goto md_failed;
2168 }
2169 break;
Vikas Chaudharyde8c72d2012-08-22 09:14:24 -04002170 case QLA8XXX_RDCRB:
Tej Parkash068237c82012-05-18 04:41:44 -04002171 qla4_8xxx_minidump_process_rdcrb(ha, entry_hdr,
2172 &data_ptr);
2173 break;
Vikas Chaudharyde8c72d2012-08-22 09:14:24 -04002174 case QLA8XXX_RDMEM:
Tej Parkash068237c82012-05-18 04:41:44 -04002175 rval = qla4_8xxx_minidump_process_rdmem(ha, entry_hdr,
2176 &data_ptr);
2177 if (rval != QLA_SUCCESS) {
Vikas Chaudhary5e9bcec2012-08-22 07:55:01 -04002178 qla4_8xxx_mark_entry_skipped(ha, entry_hdr, i);
Tej Parkash068237c82012-05-18 04:41:44 -04002179 goto md_failed;
2180 }
2181 break;
Vikas Chaudharyde8c72d2012-08-22 09:14:24 -04002182 case QLA8XXX_BOARD:
2183 case QLA8XXX_RDROM:
Vikas Chaudharyf8086f42012-08-22 07:54:59 -04002184 qla4_82xx_minidump_process_rdrom(ha, entry_hdr,
Tej Parkash068237c82012-05-18 04:41:44 -04002185 &data_ptr);
2186 break;
Vikas Chaudharyde8c72d2012-08-22 09:14:24 -04002187 case QLA8XXX_L2DTG:
2188 case QLA8XXX_L2ITG:
2189 case QLA8XXX_L2DAT:
2190 case QLA8XXX_L2INS:
Tej Parkash068237c82012-05-18 04:41:44 -04002191 rval = qla4_8xxx_minidump_process_l2tag(ha, entry_hdr,
2192 &data_ptr);
2193 if (rval != QLA_SUCCESS) {
Vikas Chaudhary5e9bcec2012-08-22 07:55:01 -04002194 qla4_8xxx_mark_entry_skipped(ha, entry_hdr, i);
Tej Parkash068237c82012-05-18 04:41:44 -04002195 goto md_failed;
2196 }
2197 break;
Vikas Chaudharyde8c72d2012-08-22 09:14:24 -04002198 case QLA8XXX_L1DAT:
2199 case QLA8XXX_L1INS:
Tej Parkash068237c82012-05-18 04:41:44 -04002200 qla4_8xxx_minidump_process_l1cache(ha, entry_hdr,
2201 &data_ptr);
2202 break;
Vikas Chaudharyde8c72d2012-08-22 09:14:24 -04002203 case QLA8XXX_RDOCM:
Tej Parkash068237c82012-05-18 04:41:44 -04002204 qla4_8xxx_minidump_process_rdocm(ha, entry_hdr,
2205 &data_ptr);
2206 break;
Vikas Chaudharyde8c72d2012-08-22 09:14:24 -04002207 case QLA8XXX_RDMUX:
Tej Parkash068237c82012-05-18 04:41:44 -04002208 qla4_8xxx_minidump_process_rdmux(ha, entry_hdr,
2209 &data_ptr);
2210 break;
Vikas Chaudharyde8c72d2012-08-22 09:14:24 -04002211 case QLA8XXX_QUEUE:
Tej Parkash068237c82012-05-18 04:41:44 -04002212 qla4_8xxx_minidump_process_queue(ha, entry_hdr,
2213 &data_ptr);
2214 break;
Vikas Chaudharyde8c72d2012-08-22 09:14:24 -04002215 case QLA8XXX_RDNOP:
Tej Parkash068237c82012-05-18 04:41:44 -04002216 default:
Vikas Chaudhary5e9bcec2012-08-22 07:55:01 -04002217 qla4_8xxx_mark_entry_skipped(ha, entry_hdr, i);
Tej Parkash068237c82012-05-18 04:41:44 -04002218 break;
2219 }
2220
2221 data_collected = (uint8_t *)data_ptr -
2222 ((uint8_t *)((uint8_t *)ha->fw_dump +
2223 ha->fw_dump_tmplt_size));
2224skip_nxt_entry:
2225 /* next entry in the template */
Vikas Chaudhary7664a1f2012-08-22 07:55:00 -04002226 entry_hdr = (struct qla8xxx_minidump_entry_hdr *)
Tej Parkash068237c82012-05-18 04:41:44 -04002227 (((uint8_t *)entry_hdr) +
2228 entry_hdr->entry_size);
2229 }
2230
2231 if ((data_collected + ha->fw_dump_tmplt_size) != ha->fw_dump_size) {
2232 ql4_printk(KERN_INFO, ha,
2233 "Dump data mismatch: Data collected: [0x%x], total_data_size:[0x%x]\n",
2234 data_collected, ha->fw_dump_size);
2235 goto md_failed;
2236 }
2237
2238 DEBUG2(ql4_printk(KERN_INFO, ha, "Leaving fn: %s Last entry: 0x%x\n",
2239 __func__, i));
2240md_failed:
2241 return rval;
2242}
2243
2244/**
2245 * qla4_8xxx_uevent_emit - Send uevent when the firmware dump is ready.
2246 * @ha: pointer to adapter structure
2247 **/
2248static void qla4_8xxx_uevent_emit(struct scsi_qla_host *ha, u32 code)
2249{
2250 char event_string[40];
2251 char *envp[] = { event_string, NULL };
2252
2253 switch (code) {
2254 case QL4_UEVENT_CODE_FW_DUMP:
2255 snprintf(event_string, sizeof(event_string), "FW_DUMP=%ld",
2256 ha->host_no);
2257 break;
2258 default:
2259 /*do nothing*/
2260 break;
2261 }
2262
2263 kobject_uevent_env(&(&ha->pdev->dev)->kobj, KOBJ_CHANGE, envp);
2264}
2265
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302266/**
2267 * qla4_8xxx_device_bootstrap - Initialize device, set DEV_READY, start fw
2268 * @ha: pointer to adapter structure
2269 *
2270 * Note: IDC lock must be held upon entry
2271 **/
2272static int
2273qla4_8xxx_device_bootstrap(struct scsi_qla_host *ha)
2274{
Shyam Sundarb25ee662010-10-06 22:50:51 -07002275 int rval = QLA_ERROR;
2276 int i, timeout;
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302277 uint32_t old_count, count;
Shyam Sundarb25ee662010-10-06 22:50:51 -07002278 int need_reset = 0, peg_stuck = 1;
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302279
Shyam Sundarb25ee662010-10-06 22:50:51 -07002280 need_reset = qla4_8xxx_need_reset(ha);
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302281
Vikas Chaudharyf8086f42012-08-22 07:54:59 -04002282 old_count = qla4_82xx_rd_32(ha, QLA82XX_PEG_ALIVE_COUNTER);
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302283
2284 for (i = 0; i < 10; i++) {
2285 timeout = msleep_interruptible(200);
2286 if (timeout) {
Vikas Chaudharyf8086f42012-08-22 07:54:59 -04002287 qla4_82xx_wr_32(ha, QLA82XX_CRB_DEV_STATE,
Vikas Chaudharyde8c72d2012-08-22 09:14:24 -04002288 QLA8XXX_DEV_FAILED);
Shyam Sundarb25ee662010-10-06 22:50:51 -07002289 return rval;
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302290 }
2291
Vikas Chaudharyf8086f42012-08-22 07:54:59 -04002292 count = qla4_82xx_rd_32(ha, QLA82XX_PEG_ALIVE_COUNTER);
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302293 if (count != old_count)
Shyam Sundarb25ee662010-10-06 22:50:51 -07002294 peg_stuck = 0;
2295 }
2296
2297 if (need_reset) {
2298 /* We are trying to perform a recovery here. */
2299 if (peg_stuck)
Vikas Chaudharyf8086f42012-08-22 07:54:59 -04002300 qla4_82xx_rom_lock_recovery(ha);
Shyam Sundarb25ee662010-10-06 22:50:51 -07002301 goto dev_initialize;
2302 } else {
2303 /* Start of day for this ha context. */
2304 if (peg_stuck) {
2305 /* Either we are the first or recovery in progress. */
Vikas Chaudharyf8086f42012-08-22 07:54:59 -04002306 qla4_82xx_rom_lock_recovery(ha);
Shyam Sundarb25ee662010-10-06 22:50:51 -07002307 goto dev_initialize;
2308 } else {
2309 /* Firmware already running. */
2310 rval = QLA_SUCCESS;
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302311 goto dev_ready;
Shyam Sundarb25ee662010-10-06 22:50:51 -07002312 }
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302313 }
2314
2315dev_initialize:
2316 /* set to DEV_INITIALIZING */
2317 ql4_printk(KERN_INFO, ha, "HW State: INITIALIZING\n");
Vikas Chaudharyde8c72d2012-08-22 09:14:24 -04002318 qla4_82xx_wr_32(ha, QLA82XX_CRB_DEV_STATE, QLA8XXX_DEV_INITIALIZING);
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302319
2320 /* Driver that sets device state to initializating sets IDC version */
Vikas Chaudharyf8086f42012-08-22 07:54:59 -04002321 qla4_82xx_wr_32(ha, QLA82XX_CRB_DRV_IDC_VERSION, QLA82XX_IDC_VERSION);
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302322
Vikas Chaudharyf8086f42012-08-22 07:54:59 -04002323 qla4_82xx_idc_unlock(ha);
Tej Parkash068237c82012-05-18 04:41:44 -04002324 if (ql4xenablemd && test_bit(AF_FW_RECOVERY, &ha->flags) &&
2325 !test_and_set_bit(AF_82XX_FW_DUMPED, &ha->flags)) {
2326 if (!qla4_8xxx_collect_md_data(ha)) {
2327 qla4_8xxx_uevent_emit(ha, QL4_UEVENT_CODE_FW_DUMP);
2328 } else {
2329 ql4_printk(KERN_INFO, ha, "Unable to collect minidump\n");
2330 clear_bit(AF_82XX_FW_DUMPED, &ha->flags);
2331 }
2332 }
Vikas Chaudharyf8086f42012-08-22 07:54:59 -04002333 rval = qla4_82xx_try_start_fw(ha);
2334 qla4_82xx_idc_lock(ha);
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302335
2336 if (rval != QLA_SUCCESS) {
2337 ql4_printk(KERN_INFO, ha, "HW State: FAILED\n");
2338 qla4_8xxx_clear_drv_active(ha);
Vikas Chaudharyde8c72d2012-08-22 09:14:24 -04002339 qla4_82xx_wr_32(ha, QLA82XX_CRB_DEV_STATE, QLA8XXX_DEV_FAILED);
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302340 return rval;
2341 }
2342
2343dev_ready:
2344 ql4_printk(KERN_INFO, ha, "HW State: READY\n");
Vikas Chaudharyde8c72d2012-08-22 09:14:24 -04002345 qla4_82xx_wr_32(ha, QLA82XX_CRB_DEV_STATE, QLA8XXX_DEV_READY);
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302346
Shyam Sundarb25ee662010-10-06 22:50:51 -07002347 return rval;
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302348}
2349
2350/**
Vikas Chaudharyf8086f42012-08-22 07:54:59 -04002351 * qla4_82xx_need_reset_handler - Code to start reset sequence
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302352 * @ha: pointer to adapter structure
2353 *
2354 * Note: IDC lock must be held upon entry
2355 **/
2356static void
Vikas Chaudharyf8086f42012-08-22 07:54:59 -04002357qla4_82xx_need_reset_handler(struct scsi_qla_host *ha)
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302358{
2359 uint32_t dev_state, drv_state, drv_active;
Tej Parkash068237c82012-05-18 04:41:44 -04002360 uint32_t active_mask = 0xFFFFFFFF;
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302361 unsigned long reset_timeout;
2362
2363 ql4_printk(KERN_INFO, ha,
2364 "Performing ISP error recovery\n");
2365
2366 if (test_and_clear_bit(AF_ONLINE, &ha->flags)) {
Vikas Chaudharyf8086f42012-08-22 07:54:59 -04002367 qla4_82xx_idc_unlock(ha);
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302368 ha->isp_ops->disable_intrs(ha);
Vikas Chaudharyf8086f42012-08-22 07:54:59 -04002369 qla4_82xx_idc_lock(ha);
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302370 }
2371
Vikas Chaudharyde8c72d2012-08-22 09:14:24 -04002372 if (!test_bit(AF_8XXX_RST_OWNER, &ha->flags)) {
Tej Parkash068237c82012-05-18 04:41:44 -04002373 DEBUG2(ql4_printk(KERN_INFO, ha,
2374 "%s(%ld): reset acknowledged\n",
2375 __func__, ha->host_no));
2376 qla4_8xxx_set_rst_ready(ha);
2377 } else {
2378 active_mask = (~(1 << (ha->func_num * 4)));
2379 }
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302380
2381 /* wait for 10 seconds for reset ack from all functions */
2382 reset_timeout = jiffies + (ha->nx_reset_timeout * HZ);
2383
Vikas Chaudharyf8086f42012-08-22 07:54:59 -04002384 drv_state = qla4_82xx_rd_32(ha, QLA82XX_CRB_DRV_STATE);
2385 drv_active = qla4_82xx_rd_32(ha, QLA82XX_CRB_DRV_ACTIVE);
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302386
2387 ql4_printk(KERN_INFO, ha,
2388 "%s(%ld): drv_state = 0x%x, drv_active = 0x%x\n",
2389 __func__, ha->host_no, drv_state, drv_active);
2390
Tej Parkash068237c82012-05-18 04:41:44 -04002391 while (drv_state != (drv_active & active_mask)) {
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302392 if (time_after_eq(jiffies, reset_timeout)) {
Tej Parkash068237c82012-05-18 04:41:44 -04002393 ql4_printk(KERN_INFO, ha,
2394 "%s: RESET TIMEOUT! drv_state: 0x%08x, drv_active: 0x%08x\n",
2395 DRIVER_NAME, drv_state, drv_active);
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302396 break;
2397 }
2398
Tej Parkash068237c82012-05-18 04:41:44 -04002399 /*
2400 * When reset_owner times out, check which functions
2401 * acked/did not ack
2402 */
Vikas Chaudharyde8c72d2012-08-22 09:14:24 -04002403 if (test_bit(AF_8XXX_RST_OWNER, &ha->flags)) {
Tej Parkash068237c82012-05-18 04:41:44 -04002404 ql4_printk(KERN_INFO, ha,
2405 "%s(%ld): drv_state = 0x%x, drv_active = 0x%x\n",
2406 __func__, ha->host_no, drv_state,
2407 drv_active);
2408 }
Vikas Chaudharyf8086f42012-08-22 07:54:59 -04002409 qla4_82xx_idc_unlock(ha);
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302410 msleep(1000);
Vikas Chaudharyf8086f42012-08-22 07:54:59 -04002411 qla4_82xx_idc_lock(ha);
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302412
Vikas Chaudharyf8086f42012-08-22 07:54:59 -04002413 drv_state = qla4_82xx_rd_32(ha, QLA82XX_CRB_DRV_STATE);
2414 drv_active = qla4_82xx_rd_32(ha, QLA82XX_CRB_DRV_ACTIVE);
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302415 }
2416
Tej Parkash068237c82012-05-18 04:41:44 -04002417 /* Clear RESET OWNER as we are not going to use it any further */
Vikas Chaudharyde8c72d2012-08-22 09:14:24 -04002418 clear_bit(AF_8XXX_RST_OWNER, &ha->flags);
Tej Parkash068237c82012-05-18 04:41:44 -04002419
Vikas Chaudharyf8086f42012-08-22 07:54:59 -04002420 dev_state = qla4_82xx_rd_32(ha, QLA82XX_CRB_DEV_STATE);
Tej Parkash068237c82012-05-18 04:41:44 -04002421 ql4_printk(KERN_INFO, ha, "Device state is 0x%x = %s\n", dev_state,
2422 dev_state < MAX_STATES ? qdev_state[dev_state] : "Unknown");
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302423
2424 /* Force to DEV_COLD unless someone else is starting a reset */
Vikas Chaudharyde8c72d2012-08-22 09:14:24 -04002425 if (dev_state != QLA8XXX_DEV_INITIALIZING) {
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302426 ql4_printk(KERN_INFO, ha, "HW State: COLD/RE-INIT\n");
Vikas Chaudharyde8c72d2012-08-22 09:14:24 -04002427 qla4_82xx_wr_32(ha, QLA82XX_CRB_DEV_STATE, QLA8XXX_DEV_COLD);
Tej Parkash068237c82012-05-18 04:41:44 -04002428 qla4_8xxx_set_rst_ready(ha);
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302429 }
2430}
2431
2432/**
2433 * qla4_8xxx_need_qsnt_handler - Code to start qsnt
2434 * @ha: pointer to adapter structure
2435 **/
2436void
2437qla4_8xxx_need_qsnt_handler(struct scsi_qla_host *ha)
2438{
Vikas Chaudharyf8086f42012-08-22 07:54:59 -04002439 qla4_82xx_idc_lock(ha);
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302440 qla4_8xxx_set_qsnt_ready(ha);
Vikas Chaudharyf8086f42012-08-22 07:54:59 -04002441 qla4_82xx_idc_unlock(ha);
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302442}
2443
2444/**
2445 * qla4_8xxx_device_state_handler - Adapter state machine
2446 * @ha: pointer to host adapter structure.
2447 *
2448 * Note: IDC lock must be UNLOCKED upon entry
2449 **/
2450int qla4_8xxx_device_state_handler(struct scsi_qla_host *ha)
2451{
2452 uint32_t dev_state;
2453 int rval = QLA_SUCCESS;
2454 unsigned long dev_init_timeout;
2455
Nilesh Javalie3f37d12011-12-01 22:42:11 -08002456 if (!test_bit(AF_INIT_DONE, &ha->flags)) {
Vikas Chaudharyf8086f42012-08-22 07:54:59 -04002457 qla4_82xx_idc_lock(ha);
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302458 qla4_8xxx_set_drv_active(ha);
Vikas Chaudharyf8086f42012-08-22 07:54:59 -04002459 qla4_82xx_idc_unlock(ha);
Nilesh Javalie3f37d12011-12-01 22:42:11 -08002460 }
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302461
Vikas Chaudharyf8086f42012-08-22 07:54:59 -04002462 dev_state = qla4_82xx_rd_32(ha, QLA82XX_CRB_DEV_STATE);
Tej Parkash068237c82012-05-18 04:41:44 -04002463 DEBUG2(ql4_printk(KERN_INFO, ha, "Device state is 0x%x = %s\n",
2464 dev_state, dev_state < MAX_STATES ?
2465 qdev_state[dev_state] : "Unknown"));
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302466
2467 /* wait for 30 seconds for device to go ready */
2468 dev_init_timeout = jiffies + (ha->nx_dev_init_timeout * HZ);
2469
Vikas Chaudharyf8086f42012-08-22 07:54:59 -04002470 qla4_82xx_idc_lock(ha);
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302471 while (1) {
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302472
2473 if (time_after_eq(jiffies, dev_init_timeout)) {
Tej Parkash068237c82012-05-18 04:41:44 -04002474 ql4_printk(KERN_WARNING, ha,
2475 "%s: Device Init Failed 0x%x = %s\n",
2476 DRIVER_NAME,
2477 dev_state, dev_state < MAX_STATES ?
2478 qdev_state[dev_state] : "Unknown");
Vikas Chaudharyf8086f42012-08-22 07:54:59 -04002479 qla4_82xx_wr_32(ha, QLA82XX_CRB_DEV_STATE,
Vikas Chaudharyde8c72d2012-08-22 09:14:24 -04002480 QLA8XXX_DEV_FAILED);
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302481 }
2482
Vikas Chaudharyf8086f42012-08-22 07:54:59 -04002483 dev_state = qla4_82xx_rd_32(ha, QLA82XX_CRB_DEV_STATE);
Tej Parkash068237c82012-05-18 04:41:44 -04002484 ql4_printk(KERN_INFO, ha, "Device state is 0x%x = %s\n",
2485 dev_state, dev_state < MAX_STATES ?
2486 qdev_state[dev_state] : "Unknown");
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302487
2488 /* NOTE: Make sure idc unlocked upon exit of switch statement */
2489 switch (dev_state) {
Vikas Chaudharyde8c72d2012-08-22 09:14:24 -04002490 case QLA8XXX_DEV_READY:
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302491 goto exit;
Vikas Chaudharyde8c72d2012-08-22 09:14:24 -04002492 case QLA8XXX_DEV_COLD:
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302493 rval = qla4_8xxx_device_bootstrap(ha);
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302494 goto exit;
Vikas Chaudharyde8c72d2012-08-22 09:14:24 -04002495 case QLA8XXX_DEV_INITIALIZING:
Vikas Chaudharyf8086f42012-08-22 07:54:59 -04002496 qla4_82xx_idc_unlock(ha);
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302497 msleep(1000);
Vikas Chaudharyf8086f42012-08-22 07:54:59 -04002498 qla4_82xx_idc_lock(ha);
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302499 break;
Vikas Chaudharyde8c72d2012-08-22 09:14:24 -04002500 case QLA8XXX_DEV_NEED_RESET:
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302501 if (!ql4xdontresethba) {
Vikas Chaudharyf8086f42012-08-22 07:54:59 -04002502 qla4_82xx_need_reset_handler(ha);
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302503 /* Update timeout value after need
2504 * reset handler */
2505 dev_init_timeout = jiffies +
2506 (ha->nx_dev_init_timeout * HZ);
Mike Hernandez9acf7532011-12-01 22:42:07 -08002507 } else {
Vikas Chaudharyf8086f42012-08-22 07:54:59 -04002508 qla4_82xx_idc_unlock(ha);
Mike Hernandez9acf7532011-12-01 22:42:07 -08002509 msleep(1000);
Vikas Chaudharyf8086f42012-08-22 07:54:59 -04002510 qla4_82xx_idc_lock(ha);
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302511 }
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302512 break;
Vikas Chaudharyde8c72d2012-08-22 09:14:24 -04002513 case QLA8XXX_DEV_NEED_QUIESCENT:
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302514 /* idc locked/unlocked in handler */
2515 qla4_8xxx_need_qsnt_handler(ha);
Nilesh Javalie3f37d12011-12-01 22:42:11 -08002516 break;
Vikas Chaudharyde8c72d2012-08-22 09:14:24 -04002517 case QLA8XXX_DEV_QUIESCENT:
Vikas Chaudharyf8086f42012-08-22 07:54:59 -04002518 qla4_82xx_idc_unlock(ha);
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302519 msleep(1000);
Vikas Chaudharyf8086f42012-08-22 07:54:59 -04002520 qla4_82xx_idc_lock(ha);
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302521 break;
Vikas Chaudharyde8c72d2012-08-22 09:14:24 -04002522 case QLA8XXX_DEV_FAILED:
Vikas Chaudharyf8086f42012-08-22 07:54:59 -04002523 qla4_82xx_idc_unlock(ha);
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302524 qla4xxx_dead_adapter_cleanup(ha);
2525 rval = QLA_ERROR;
Vikas Chaudharyf8086f42012-08-22 07:54:59 -04002526 qla4_82xx_idc_lock(ha);
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302527 goto exit;
2528 default:
Vikas Chaudharyf8086f42012-08-22 07:54:59 -04002529 qla4_82xx_idc_unlock(ha);
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302530 qla4xxx_dead_adapter_cleanup(ha);
2531 rval = QLA_ERROR;
Vikas Chaudharyf8086f42012-08-22 07:54:59 -04002532 qla4_82xx_idc_lock(ha);
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302533 goto exit;
2534 }
2535 }
2536exit:
Vikas Chaudharyf8086f42012-08-22 07:54:59 -04002537 qla4_82xx_idc_unlock(ha);
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302538 return rval;
2539}
2540
2541int qla4_8xxx_load_risc(struct scsi_qla_host *ha)
2542{
2543 int retval;
Sarang Radke78764992012-01-11 02:44:18 -08002544
2545 /* clear the interrupt */
Vikas Chaudhary7664a1f2012-08-22 07:55:00 -04002546 writel(0, &ha->qla4_82xx_reg->host_int);
2547 readl(&ha->qla4_82xx_reg->host_int);
Sarang Radke78764992012-01-11 02:44:18 -08002548
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302549 retval = qla4_8xxx_device_state_handler(ha);
2550
Vikas Chaudharyf581a3f2010-10-06 22:47:48 -07002551 if (retval == QLA_SUCCESS && !test_bit(AF_INIT_DONE, &ha->flags))
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302552 retval = qla4xxx_request_irqs(ha);
Vikas Chaudharyf581a3f2010-10-06 22:47:48 -07002553
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302554 return retval;
2555}
2556
2557/*****************************************************************************/
2558/* Flash Manipulation Routines */
2559/*****************************************************************************/
2560
2561#define OPTROM_BURST_SIZE 0x1000
2562#define OPTROM_BURST_DWORDS (OPTROM_BURST_SIZE / 4)
2563
2564#define FARX_DATA_FLAG BIT_31
2565#define FARX_ACCESS_FLASH_CONF 0x7FFD0000
2566#define FARX_ACCESS_FLASH_DATA 0x7FF00000
2567
2568static inline uint32_t
2569flash_conf_addr(struct ql82xx_hw_data *hw, uint32_t faddr)
2570{
2571 return hw->flash_conf_off | faddr;
2572}
2573
2574static inline uint32_t
2575flash_data_addr(struct ql82xx_hw_data *hw, uint32_t faddr)
2576{
2577 return hw->flash_data_off | faddr;
2578}
2579
2580static uint32_t *
Vikas Chaudharyf8086f42012-08-22 07:54:59 -04002581qla4_82xx_read_flash_data(struct scsi_qla_host *ha, uint32_t *dwptr,
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302582 uint32_t faddr, uint32_t length)
2583{
2584 uint32_t i;
2585 uint32_t val;
2586 int loops = 0;
Vikas Chaudharyf8086f42012-08-22 07:54:59 -04002587 while ((qla4_82xx_rom_lock(ha) != 0) && (loops < 50000)) {
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302588 udelay(100);
2589 cond_resched();
2590 loops++;
2591 }
2592 if (loops >= 50000) {
2593 ql4_printk(KERN_WARNING, ha, "ROM lock failed\n");
2594 return dwptr;
2595 }
2596
2597 /* Dword reads to flash. */
2598 for (i = 0; i < length/4; i++, faddr += 4) {
Vikas Chaudharyf8086f42012-08-22 07:54:59 -04002599 if (qla4_82xx_do_rom_fast_read(ha, faddr, &val)) {
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302600 ql4_printk(KERN_WARNING, ha,
2601 "Do ROM fast read failed\n");
2602 goto done_read;
2603 }
2604 dwptr[i] = __constant_cpu_to_le32(val);
2605 }
2606
2607done_read:
Vikas Chaudharyf8086f42012-08-22 07:54:59 -04002608 qla4_82xx_rom_unlock(ha);
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302609 return dwptr;
2610}
2611
2612/**
2613 * Address and length are byte address
2614 **/
2615static uint8_t *
Vikas Chaudharyf8086f42012-08-22 07:54:59 -04002616qla4_82xx_read_optrom_data(struct scsi_qla_host *ha, uint8_t *buf,
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302617 uint32_t offset, uint32_t length)
2618{
Vikas Chaudharyf8086f42012-08-22 07:54:59 -04002619 qla4_82xx_read_flash_data(ha, (uint32_t *)buf, offset, length);
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302620 return buf;
2621}
2622
2623static int
2624qla4_8xxx_find_flt_start(struct scsi_qla_host *ha, uint32_t *start)
2625{
2626 const char *loc, *locations[] = { "DEF", "PCI" };
2627
2628 /*
2629 * FLT-location structure resides after the last PCI region.
2630 */
2631
2632 /* Begin with sane defaults. */
2633 loc = locations[0];
2634 *start = FA_FLASH_LAYOUT_ADDR_82;
2635
2636 DEBUG2(ql4_printk(KERN_INFO, ha, "FLTL[%s] = 0x%x.\n", loc, *start));
2637 return QLA_SUCCESS;
2638}
2639
2640static void
2641qla4_8xxx_get_flt_info(struct scsi_qla_host *ha, uint32_t flt_addr)
2642{
2643 const char *loc, *locations[] = { "DEF", "FLT" };
2644 uint16_t *wptr;
2645 uint16_t cnt, chksum;
2646 uint32_t start;
2647 struct qla_flt_header *flt;
2648 struct qla_flt_region *region;
2649 struct ql82xx_hw_data *hw = &ha->hw;
2650
2651 hw->flt_region_flt = flt_addr;
2652 wptr = (uint16_t *)ha->request_ring;
2653 flt = (struct qla_flt_header *)ha->request_ring;
2654 region = (struct qla_flt_region *)&flt[1];
Vikas Chaudharyf8086f42012-08-22 07:54:59 -04002655 qla4_82xx_read_optrom_data(ha, (uint8_t *)ha->request_ring,
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302656 flt_addr << 2, OPTROM_BURST_SIZE);
2657 if (*wptr == __constant_cpu_to_le16(0xffff))
2658 goto no_flash_data;
2659 if (flt->version != __constant_cpu_to_le16(1)) {
2660 DEBUG2(ql4_printk(KERN_INFO, ha, "Unsupported FLT detected: "
2661 "version=0x%x length=0x%x checksum=0x%x.\n",
2662 le16_to_cpu(flt->version), le16_to_cpu(flt->length),
2663 le16_to_cpu(flt->checksum)));
2664 goto no_flash_data;
2665 }
2666
2667 cnt = (sizeof(struct qla_flt_header) + le16_to_cpu(flt->length)) >> 1;
2668 for (chksum = 0; cnt; cnt--)
2669 chksum += le16_to_cpu(*wptr++);
2670 if (chksum) {
2671 DEBUG2(ql4_printk(KERN_INFO, ha, "Inconsistent FLT detected: "
2672 "version=0x%x length=0x%x checksum=0x%x.\n",
2673 le16_to_cpu(flt->version), le16_to_cpu(flt->length),
2674 chksum));
2675 goto no_flash_data;
2676 }
2677
2678 loc = locations[1];
2679 cnt = le16_to_cpu(flt->length) / sizeof(struct qla_flt_region);
2680 for ( ; cnt; cnt--, region++) {
2681 /* Store addresses as DWORD offsets. */
2682 start = le32_to_cpu(region->start) >> 2;
2683
2684 DEBUG3(ql4_printk(KERN_DEBUG, ha, "FLT[%02x]: start=0x%x "
2685 "end=0x%x size=0x%x.\n", le32_to_cpu(region->code), start,
2686 le32_to_cpu(region->end) >> 2, le32_to_cpu(region->size)));
2687
2688 switch (le32_to_cpu(region->code) & 0xff) {
2689 case FLT_REG_FDT:
2690 hw->flt_region_fdt = start;
2691 break;
2692 case FLT_REG_BOOT_CODE_82:
2693 hw->flt_region_boot = start;
2694 break;
2695 case FLT_REG_FW_82:
Nilesh Javali93823952011-10-07 16:55:39 -07002696 case FLT_REG_FW_82_1:
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302697 hw->flt_region_fw = start;
2698 break;
2699 case FLT_REG_BOOTLOAD_82:
2700 hw->flt_region_bootload = start;
2701 break;
Manish Rangankar2a991c22011-07-25 13:48:55 -05002702 case FLT_REG_ISCSI_PARAM:
2703 hw->flt_iscsi_param = start;
2704 break;
Lalit Chandivade45494152011-10-07 16:55:42 -07002705 case FLT_REG_ISCSI_CHAP:
2706 hw->flt_region_chap = start;
2707 hw->flt_chap_size = le32_to_cpu(region->size);
2708 break;
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302709 }
2710 }
2711 goto done;
2712
2713no_flash_data:
2714 /* Use hardcoded defaults. */
2715 loc = locations[0];
2716
2717 hw->flt_region_fdt = FA_FLASH_DESCR_ADDR_82;
2718 hw->flt_region_boot = FA_BOOT_CODE_ADDR_82;
2719 hw->flt_region_bootload = FA_BOOT_LOAD_ADDR_82;
2720 hw->flt_region_fw = FA_RISC_CODE_ADDR_82;
Lalit Chandivade45494152011-10-07 16:55:42 -07002721 hw->flt_region_chap = FA_FLASH_ISCSI_CHAP;
2722 hw->flt_chap_size = FA_FLASH_CHAP_SIZE;
2723
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302724done:
2725 DEBUG2(ql4_printk(KERN_INFO, ha, "FLT[%s]: flt=0x%x fdt=0x%x "
2726 "boot=0x%x bootload=0x%x fw=0x%x\n", loc, hw->flt_region_flt,
2727 hw->flt_region_fdt, hw->flt_region_boot, hw->flt_region_bootload,
2728 hw->flt_region_fw));
2729}
2730
2731static void
Vikas Chaudharyf8086f42012-08-22 07:54:59 -04002732qla4_82xx_get_fdt_info(struct scsi_qla_host *ha)
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302733{
2734#define FLASH_BLK_SIZE_4K 0x1000
2735#define FLASH_BLK_SIZE_32K 0x8000
2736#define FLASH_BLK_SIZE_64K 0x10000
2737 const char *loc, *locations[] = { "MID", "FDT" };
2738 uint16_t cnt, chksum;
2739 uint16_t *wptr;
2740 struct qla_fdt_layout *fdt;
Vikas Chaudhary3c3e2102010-08-09 05:14:07 -07002741 uint16_t mid = 0;
2742 uint16_t fid = 0;
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302743 struct ql82xx_hw_data *hw = &ha->hw;
2744
2745 hw->flash_conf_off = FARX_ACCESS_FLASH_CONF;
2746 hw->flash_data_off = FARX_ACCESS_FLASH_DATA;
2747
2748 wptr = (uint16_t *)ha->request_ring;
2749 fdt = (struct qla_fdt_layout *)ha->request_ring;
Vikas Chaudharyf8086f42012-08-22 07:54:59 -04002750 qla4_82xx_read_optrom_data(ha, (uint8_t *)ha->request_ring,
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302751 hw->flt_region_fdt << 2, OPTROM_BURST_SIZE);
2752
2753 if (*wptr == __constant_cpu_to_le16(0xffff))
2754 goto no_flash_data;
2755
2756 if (fdt->sig[0] != 'Q' || fdt->sig[1] != 'L' || fdt->sig[2] != 'I' ||
2757 fdt->sig[3] != 'D')
2758 goto no_flash_data;
2759
2760 for (cnt = 0, chksum = 0; cnt < sizeof(struct qla_fdt_layout) >> 1;
2761 cnt++)
2762 chksum += le16_to_cpu(*wptr++);
2763
2764 if (chksum) {
2765 DEBUG2(ql4_printk(KERN_INFO, ha, "Inconsistent FDT detected: "
2766 "checksum=0x%x id=%c version=0x%x.\n", chksum, fdt->sig[0],
2767 le16_to_cpu(fdt->version)));
2768 goto no_flash_data;
2769 }
2770
2771 loc = locations[1];
2772 mid = le16_to_cpu(fdt->man_id);
2773 fid = le16_to_cpu(fdt->id);
2774 hw->fdt_wrt_disable = fdt->wrt_disable_bits;
2775 hw->fdt_erase_cmd = flash_conf_addr(hw, 0x0300 | fdt->erase_cmd);
2776 hw->fdt_block_size = le32_to_cpu(fdt->block_size);
2777
2778 if (fdt->unprotect_sec_cmd) {
2779 hw->fdt_unprotect_sec_cmd = flash_conf_addr(hw, 0x0300 |
2780 fdt->unprotect_sec_cmd);
2781 hw->fdt_protect_sec_cmd = fdt->protect_sec_cmd ?
2782 flash_conf_addr(hw, 0x0300 | fdt->protect_sec_cmd) :
2783 flash_conf_addr(hw, 0x0336);
2784 }
2785 goto done;
2786
2787no_flash_data:
2788 loc = locations[0];
2789 hw->fdt_block_size = FLASH_BLK_SIZE_64K;
2790done:
2791 DEBUG2(ql4_printk(KERN_INFO, ha, "FDT[%s]: (0x%x/0x%x) erase=0x%x "
2792 "pro=%x upro=%x wrtd=0x%x blk=0x%x.\n", loc, mid, fid,
2793 hw->fdt_erase_cmd, hw->fdt_protect_sec_cmd,
2794 hw->fdt_unprotect_sec_cmd, hw->fdt_wrt_disable,
2795 hw->fdt_block_size));
2796}
2797
2798static void
Vikas Chaudharyf8086f42012-08-22 07:54:59 -04002799qla4_82xx_get_idc_param(struct scsi_qla_host *ha)
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302800{
2801#define QLA82XX_IDC_PARAM_ADDR 0x003e885c
2802 uint32_t *wptr;
2803
2804 if (!is_qla8022(ha))
2805 return;
2806 wptr = (uint32_t *)ha->request_ring;
Vikas Chaudharyf8086f42012-08-22 07:54:59 -04002807 qla4_82xx_read_optrom_data(ha, (uint8_t *)ha->request_ring,
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302808 QLA82XX_IDC_PARAM_ADDR , 8);
2809
2810 if (*wptr == __constant_cpu_to_le32(0xffffffff)) {
2811 ha->nx_dev_init_timeout = ROM_DEV_INIT_TIMEOUT;
2812 ha->nx_reset_timeout = ROM_DRV_RESET_ACK_TIMEOUT;
2813 } else {
2814 ha->nx_dev_init_timeout = le32_to_cpu(*wptr++);
2815 ha->nx_reset_timeout = le32_to_cpu(*wptr);
2816 }
2817
2818 DEBUG2(ql4_printk(KERN_DEBUG, ha,
2819 "ha->nx_dev_init_timeout = %d\n", ha->nx_dev_init_timeout));
2820 DEBUG2(ql4_printk(KERN_DEBUG, ha,
2821 "ha->nx_reset_timeout = %d\n", ha->nx_reset_timeout));
2822 return;
2823}
2824
2825int
2826qla4_8xxx_get_flash_info(struct scsi_qla_host *ha)
2827{
2828 int ret;
2829 uint32_t flt_addr;
2830
2831 ret = qla4_8xxx_find_flt_start(ha, &flt_addr);
2832 if (ret != QLA_SUCCESS)
2833 return ret;
2834
2835 qla4_8xxx_get_flt_info(ha, flt_addr);
Vikas Chaudharyf8086f42012-08-22 07:54:59 -04002836 qla4_82xx_get_fdt_info(ha);
2837 qla4_82xx_get_idc_param(ha);
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302838
2839 return QLA_SUCCESS;
2840}
2841
2842/**
2843 * qla4_8xxx_stop_firmware - stops firmware on specified adapter instance
2844 * @ha: pointer to host adapter structure.
2845 *
2846 * Remarks:
2847 * For iSCSI, throws away all I/O and AENs into bit bucket, so they will
2848 * not be available after successful return. Driver must cleanup potential
2849 * outstanding I/O's after calling this funcion.
2850 **/
2851int
2852qla4_8xxx_stop_firmware(struct scsi_qla_host *ha)
2853{
2854 int status;
2855 uint32_t mbox_cmd[MBOX_REG_COUNT];
2856 uint32_t mbox_sts[MBOX_REG_COUNT];
2857
2858 memset(&mbox_cmd, 0, sizeof(mbox_cmd));
2859 memset(&mbox_sts, 0, sizeof(mbox_sts));
2860
2861 mbox_cmd[0] = MBOX_CMD_STOP_FW;
2862 status = qla4xxx_mailbox_command(ha, MBOX_REG_COUNT, 1,
2863 &mbox_cmd[0], &mbox_sts[0]);
2864
2865 DEBUG2(printk("scsi%ld: %s: status = %d\n", ha->host_no,
2866 __func__, status));
2867 return status;
2868}
2869
2870/**
Vikas Chaudharyf8086f42012-08-22 07:54:59 -04002871 * qla4_82xx_isp_reset - Resets ISP and aborts all outstanding commands.
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302872 * @ha: pointer to host adapter structure.
2873 **/
2874int
Vikas Chaudharyf8086f42012-08-22 07:54:59 -04002875qla4_82xx_isp_reset(struct scsi_qla_host *ha)
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302876{
2877 int rval;
2878 uint32_t dev_state;
2879
Vikas Chaudharyf8086f42012-08-22 07:54:59 -04002880 qla4_82xx_idc_lock(ha);
2881 dev_state = qla4_82xx_rd_32(ha, QLA82XX_CRB_DEV_STATE);
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302882
Vikas Chaudharyde8c72d2012-08-22 09:14:24 -04002883 if (dev_state == QLA8XXX_DEV_READY) {
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302884 ql4_printk(KERN_INFO, ha, "HW State: NEED RESET\n");
Vikas Chaudharyf8086f42012-08-22 07:54:59 -04002885 qla4_82xx_wr_32(ha, QLA82XX_CRB_DEV_STATE,
Vikas Chaudharyde8c72d2012-08-22 09:14:24 -04002886 QLA8XXX_DEV_NEED_RESET);
2887 set_bit(AF_8XXX_RST_OWNER, &ha->flags);
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302888 } else
2889 ql4_printk(KERN_INFO, ha, "HW State: DEVICE INITIALIZING\n");
2890
Vikas Chaudharyf8086f42012-08-22 07:54:59 -04002891 qla4_82xx_idc_unlock(ha);
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302892
2893 rval = qla4_8xxx_device_state_handler(ha);
2894
Vikas Chaudharyf8086f42012-08-22 07:54:59 -04002895 qla4_82xx_idc_lock(ha);
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302896 qla4_8xxx_clear_rst_ready(ha);
Vikas Chaudharyf8086f42012-08-22 07:54:59 -04002897 qla4_82xx_idc_unlock(ha);
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302898
Tej Parkash068237c82012-05-18 04:41:44 -04002899 if (rval == QLA_SUCCESS) {
Vikas Chaudharyf8086f42012-08-22 07:54:59 -04002900 ql4_printk(KERN_INFO, ha, "Clearing AF_RECOVERY in qla4_82xx_isp_reset\n");
Nilesh Javali21033632010-07-30 14:28:07 +05302901 clear_bit(AF_FW_RECOVERY, &ha->flags);
Tej Parkash068237c82012-05-18 04:41:44 -04002902 }
Nilesh Javali21033632010-07-30 14:28:07 +05302903
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302904 return rval;
2905}
2906
2907/**
2908 * qla4_8xxx_get_sys_info - get adapter MAC address(es) and serial number
2909 * @ha: pointer to host adapter structure.
2910 *
2911 **/
2912int qla4_8xxx_get_sys_info(struct scsi_qla_host *ha)
2913{
2914 uint32_t mbox_cmd[MBOX_REG_COUNT];
2915 uint32_t mbox_sts[MBOX_REG_COUNT];
2916 struct mbx_sys_info *sys_info;
2917 dma_addr_t sys_info_dma;
2918 int status = QLA_ERROR;
2919
2920 sys_info = dma_alloc_coherent(&ha->pdev->dev, sizeof(*sys_info),
2921 &sys_info_dma, GFP_KERNEL);
2922 if (sys_info == NULL) {
2923 DEBUG2(printk("scsi%ld: %s: Unable to allocate dma buffer.\n",
2924 ha->host_no, __func__));
2925 return status;
2926 }
2927
2928 memset(sys_info, 0, sizeof(*sys_info));
2929 memset(&mbox_cmd, 0, sizeof(mbox_cmd));
2930 memset(&mbox_sts, 0, sizeof(mbox_sts));
2931
2932 mbox_cmd[0] = MBOX_CMD_GET_SYS_INFO;
2933 mbox_cmd[1] = LSDW(sys_info_dma);
2934 mbox_cmd[2] = MSDW(sys_info_dma);
2935 mbox_cmd[4] = sizeof(*sys_info);
2936
2937 if (qla4xxx_mailbox_command(ha, MBOX_REG_COUNT, 6, &mbox_cmd[0],
2938 &mbox_sts[0]) != QLA_SUCCESS) {
2939 DEBUG2(printk("scsi%ld: %s: GET_SYS_INFO failed\n",
2940 ha->host_no, __func__));
2941 goto exit_validate_mac82;
2942 }
2943
Vikas Chaudhary2ccdf0d2010-07-30 14:27:45 +05302944 /* Make sure we receive the minimum required data to cache internally */
2945 if (mbox_sts[4] < offsetof(struct mbx_sys_info, reserved)) {
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302946 DEBUG2(printk("scsi%ld: %s: GET_SYS_INFO data receive"
2947 " error (%x)\n", ha->host_no, __func__, mbox_sts[4]));
2948 goto exit_validate_mac82;
2949
2950 }
2951
2952 /* Save M.A.C. address & serial_number */
Manish Rangankar2a991c22011-07-25 13:48:55 -05002953 ha->port_num = sys_info->port_num;
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302954 memcpy(ha->my_mac, &sys_info->mac_addr[0],
2955 min(sizeof(ha->my_mac), sizeof(sys_info->mac_addr)));
2956 memcpy(ha->serial_number, &sys_info->serial_number,
2957 min(sizeof(ha->serial_number), sizeof(sys_info->serial_number)));
Vikas Chaudhary91ec7ce2011-08-01 03:26:17 -07002958 memcpy(ha->model_name, &sys_info->board_id_str,
2959 min(sizeof(ha->model_name), sizeof(sys_info->board_id_str)));
2960 ha->phy_port_cnt = sys_info->phys_port_cnt;
2961 ha->phy_port_num = sys_info->port_num;
2962 ha->iscsi_pci_func_cnt = sys_info->iscsi_pci_func_cnt;
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05302963
2964 DEBUG2(printk("scsi%ld: %s: "
2965 "mac %02x:%02x:%02x:%02x:%02x:%02x "
2966 "serial %s\n", ha->host_no, __func__,
2967 ha->my_mac[0], ha->my_mac[1], ha->my_mac[2],
2968 ha->my_mac[3], ha->my_mac[4], ha->my_mac[5],
2969 ha->serial_number));
2970
2971 status = QLA_SUCCESS;
2972
2973exit_validate_mac82:
2974 dma_free_coherent(&ha->pdev->dev, sizeof(*sys_info), sys_info,
2975 sys_info_dma);
2976 return status;
2977}
2978
2979/* Interrupt handling helpers. */
2980
2981static int
2982qla4_8xxx_mbx_intr_enable(struct scsi_qla_host *ha)
2983{
2984 uint32_t mbox_cmd[MBOX_REG_COUNT];
2985 uint32_t mbox_sts[MBOX_REG_COUNT];
2986
2987 DEBUG2(ql4_printk(KERN_INFO, ha, "%s\n", __func__));
2988
2989 memset(&mbox_cmd, 0, sizeof(mbox_cmd));
2990 memset(&mbox_sts, 0, sizeof(mbox_sts));
2991 mbox_cmd[0] = MBOX_CMD_ENABLE_INTRS;
2992 mbox_cmd[1] = INTR_ENABLE;
2993 if (qla4xxx_mailbox_command(ha, MBOX_REG_COUNT, 1, &mbox_cmd[0],
2994 &mbox_sts[0]) != QLA_SUCCESS) {
2995 DEBUG2(ql4_printk(KERN_INFO, ha,
2996 "%s: MBOX_CMD_ENABLE_INTRS failed (0x%04x)\n",
2997 __func__, mbox_sts[0]));
2998 return QLA_ERROR;
2999 }
3000 return QLA_SUCCESS;
3001}
3002
3003static int
3004qla4_8xxx_mbx_intr_disable(struct scsi_qla_host *ha)
3005{
3006 uint32_t mbox_cmd[MBOX_REG_COUNT];
3007 uint32_t mbox_sts[MBOX_REG_COUNT];
3008
3009 DEBUG2(ql4_printk(KERN_INFO, ha, "%s\n", __func__));
3010
3011 memset(&mbox_cmd, 0, sizeof(mbox_cmd));
3012 memset(&mbox_sts, 0, sizeof(mbox_sts));
3013 mbox_cmd[0] = MBOX_CMD_ENABLE_INTRS;
3014 mbox_cmd[1] = INTR_DISABLE;
3015 if (qla4xxx_mailbox_command(ha, MBOX_REG_COUNT, 1, &mbox_cmd[0],
3016 &mbox_sts[0]) != QLA_SUCCESS) {
3017 DEBUG2(ql4_printk(KERN_INFO, ha,
3018 "%s: MBOX_CMD_ENABLE_INTRS failed (0x%04x)\n",
3019 __func__, mbox_sts[0]));
3020 return QLA_ERROR;
3021 }
3022
3023 return QLA_SUCCESS;
3024}
3025
3026void
Vikas Chaudharyf8086f42012-08-22 07:54:59 -04003027qla4_82xx_enable_intrs(struct scsi_qla_host *ha)
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05303028{
3029 qla4_8xxx_mbx_intr_enable(ha);
3030
3031 spin_lock_irq(&ha->hardware_lock);
3032 /* BIT 10 - reset */
Vikas Chaudharyf8086f42012-08-22 07:54:59 -04003033 qla4_82xx_wr_32(ha, ha->nx_legacy_intr.tgt_mask_reg, 0xfbff);
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05303034 spin_unlock_irq(&ha->hardware_lock);
3035 set_bit(AF_INTERRUPTS_ON, &ha->flags);
3036}
3037
3038void
Vikas Chaudharyf8086f42012-08-22 07:54:59 -04003039qla4_82xx_disable_intrs(struct scsi_qla_host *ha)
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05303040{
Sarang Radke5fa8b572011-03-23 08:07:33 -07003041 if (test_and_clear_bit(AF_INTERRUPTS_ON, &ha->flags))
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05303042 qla4_8xxx_mbx_intr_disable(ha);
3043
3044 spin_lock_irq(&ha->hardware_lock);
3045 /* BIT 10 - set */
Vikas Chaudharyf8086f42012-08-22 07:54:59 -04003046 qla4_82xx_wr_32(ha, ha->nx_legacy_intr.tgt_mask_reg, 0x0400);
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05303047 spin_unlock_irq(&ha->hardware_lock);
Vikas Chaudharyf4f5df22010-07-28 15:53:44 +05303048}
3049
3050struct ql4_init_msix_entry {
3051 uint16_t entry;
3052 uint16_t index;
3053 const char *name;
3054 irq_handler_t handler;
3055};
3056
3057static struct ql4_init_msix_entry qla4_8xxx_msix_entries[QLA_MSIX_ENTRIES] = {
3058 { QLA_MSIX_DEFAULT, QLA_MIDX_DEFAULT,
3059 "qla4xxx (default)",
3060 (irq_handler_t)qla4_8xxx_default_intr_handler },
3061 { QLA_MSIX_RSP_Q, QLA_MIDX_RSP_Q,
3062 "qla4xxx (rsp_q)", (irq_handler_t)qla4_8xxx_msix_rsp_q },
3063};
3064
3065void
3066qla4_8xxx_disable_msix(struct scsi_qla_host *ha)
3067{
3068 int i;
3069 struct ql4_msix_entry *qentry;
3070
3071 for (i = 0; i < QLA_MSIX_ENTRIES; i++) {
3072 qentry = &ha->msix_entries[qla4_8xxx_msix_entries[i].index];
3073 if (qentry->have_irq) {
3074 free_irq(qentry->msix_vector, ha);
3075 DEBUG2(ql4_printk(KERN_INFO, ha, "%s: %s\n",
3076 __func__, qla4_8xxx_msix_entries[i].name));
3077 }
3078 }
3079 pci_disable_msix(ha->pdev);
3080 clear_bit(AF_MSIX_ENABLED, &ha->flags);
3081}
3082
3083int
3084qla4_8xxx_enable_msix(struct scsi_qla_host *ha)
3085{
3086 int i, ret;
3087 struct msix_entry entries[QLA_MSIX_ENTRIES];
3088 struct ql4_msix_entry *qentry;
3089
3090 for (i = 0; i < QLA_MSIX_ENTRIES; i++)
3091 entries[i].entry = qla4_8xxx_msix_entries[i].entry;
3092
3093 ret = pci_enable_msix(ha->pdev, entries, ARRAY_SIZE(entries));
3094 if (ret) {
3095 ql4_printk(KERN_WARNING, ha,
3096 "MSI-X: Failed to enable support -- %d/%d\n",
3097 QLA_MSIX_ENTRIES, ret);
3098 goto msix_out;
3099 }
3100 set_bit(AF_MSIX_ENABLED, &ha->flags);
3101
3102 for (i = 0; i < QLA_MSIX_ENTRIES; i++) {
3103 qentry = &ha->msix_entries[qla4_8xxx_msix_entries[i].index];
3104 qentry->msix_vector = entries[i].vector;
3105 qentry->msix_entry = entries[i].entry;
3106 qentry->have_irq = 0;
3107 ret = request_irq(qentry->msix_vector,
3108 qla4_8xxx_msix_entries[i].handler, 0,
3109 qla4_8xxx_msix_entries[i].name, ha);
3110 if (ret) {
3111 ql4_printk(KERN_WARNING, ha,
3112 "MSI-X: Unable to register handler -- %x/%d.\n",
3113 qla4_8xxx_msix_entries[i].index, ret);
3114 qla4_8xxx_disable_msix(ha);
3115 goto msix_out;
3116 }
3117 qentry->have_irq = 1;
3118 DEBUG2(ql4_printk(KERN_INFO, ha, "%s: %s\n",
3119 __func__, qla4_8xxx_msix_entries[i].name));
3120 }
3121msix_out:
3122 return ret;
3123}