blob: f0fdc222770df115b5436f34b59f41970575976d [file] [log] [blame]
Giridhar Malavalia9083012010-04-12 17:59:55 -07001/*
2 * QLogic Fibre Channel HBA Driver
Chad Dupuis46152ce2012-08-22 14:21:08 -04003 * Copyright (c) 2003-2012 QLogic Corporation
Giridhar Malavalia9083012010-04-12 17:59:55 -07004 *
5 * See LICENSE.qla2xxx for copyright and licensing details.
6 */
7#include "qla_def.h"
8#include <linux/delay.h>
9#include <linux/pci.h>
Giridhar Malavali08de2842011-08-16 11:31:44 -070010#include <linux/ratelimit.h>
11#include <linux/vmalloc.h>
Andrew Vasquezff2fc422011-02-23 15:27:15 -080012#include <scsi/scsi_tcq.h>
Giridhar Malavalia9083012010-04-12 17:59:55 -070013
14#define MASK(n) ((1ULL<<(n))-1)
15#define MN_WIN(addr) (((addr & 0x1fc0000) >> 1) | \
16 ((addr >> 25) & 0x3ff))
17#define OCM_WIN(addr) (((addr & 0x1ff0000) >> 1) | \
18 ((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))
Lalit Chandivade0547fb32010-05-28 15:08:26 -070025#define BLOCK_PROTECT_BITS 0x0F
Giridhar Malavalia9083012010-04-12 17:59:55 -070026
27/* CRB window related */
28#define CRB_BLK(off) ((off >> 20) & 0x3f)
29#define CRB_SUBBLK(off) ((off >> 16) & 0xf)
30#define CRB_WINDOW_2M (0x130060)
31#define QLA82XX_PCI_CAMQM_2M_END (0x04800800UL)
32#define CRB_HI(off) ((qla82xx_crb_hub_agt[CRB_BLK(off)] << 20) | \
33 ((off) & 0xf0000))
34#define QLA82XX_PCI_CAMQM_2M_BASE (0x000ff800UL)
35#define CRB_INDIRECT_2M (0x1e0000UL)
36
Giridhar Malavalia9083012010-04-12 17:59:55 -070037#define MAX_CRB_XFORM 60
38static unsigned long crb_addr_xform[MAX_CRB_XFORM];
Saurav Kashyapfa492632012-11-21 02:40:29 -050039static int qla82xx_crb_table_initialized;
Giridhar Malavalia9083012010-04-12 17:59:55 -070040
41#define qla82xx_crb_addr_transform(name) \
42 (crb_addr_xform[QLA82XX_HW_PX_MAP_CRB_##name] = \
43 QLA82XX_HW_CRB_HUB_AGT_ADR_##name << 20)
44
45static void qla82xx_crb_addr_transform_setup(void)
46{
47 qla82xx_crb_addr_transform(XDMA);
48 qla82xx_crb_addr_transform(TIMR);
49 qla82xx_crb_addr_transform(SRE);
50 qla82xx_crb_addr_transform(SQN3);
51 qla82xx_crb_addr_transform(SQN2);
52 qla82xx_crb_addr_transform(SQN1);
53 qla82xx_crb_addr_transform(SQN0);
54 qla82xx_crb_addr_transform(SQS3);
55 qla82xx_crb_addr_transform(SQS2);
56 qla82xx_crb_addr_transform(SQS1);
57 qla82xx_crb_addr_transform(SQS0);
58 qla82xx_crb_addr_transform(RPMX7);
59 qla82xx_crb_addr_transform(RPMX6);
60 qla82xx_crb_addr_transform(RPMX5);
61 qla82xx_crb_addr_transform(RPMX4);
62 qla82xx_crb_addr_transform(RPMX3);
63 qla82xx_crb_addr_transform(RPMX2);
64 qla82xx_crb_addr_transform(RPMX1);
65 qla82xx_crb_addr_transform(RPMX0);
66 qla82xx_crb_addr_transform(ROMUSB);
67 qla82xx_crb_addr_transform(SN);
68 qla82xx_crb_addr_transform(QMN);
69 qla82xx_crb_addr_transform(QMS);
70 qla82xx_crb_addr_transform(PGNI);
71 qla82xx_crb_addr_transform(PGND);
72 qla82xx_crb_addr_transform(PGN3);
73 qla82xx_crb_addr_transform(PGN2);
74 qla82xx_crb_addr_transform(PGN1);
75 qla82xx_crb_addr_transform(PGN0);
76 qla82xx_crb_addr_transform(PGSI);
77 qla82xx_crb_addr_transform(PGSD);
78 qla82xx_crb_addr_transform(PGS3);
79 qla82xx_crb_addr_transform(PGS2);
80 qla82xx_crb_addr_transform(PGS1);
81 qla82xx_crb_addr_transform(PGS0);
82 qla82xx_crb_addr_transform(PS);
83 qla82xx_crb_addr_transform(PH);
84 qla82xx_crb_addr_transform(NIU);
85 qla82xx_crb_addr_transform(I2Q);
86 qla82xx_crb_addr_transform(EG);
87 qla82xx_crb_addr_transform(MN);
88 qla82xx_crb_addr_transform(MS);
89 qla82xx_crb_addr_transform(CAS2);
90 qla82xx_crb_addr_transform(CAS1);
91 qla82xx_crb_addr_transform(CAS0);
92 qla82xx_crb_addr_transform(CAM);
93 qla82xx_crb_addr_transform(C2C1);
94 qla82xx_crb_addr_transform(C2C0);
95 qla82xx_crb_addr_transform(SMB);
96 qla82xx_crb_addr_transform(OCM0);
97 /*
98 * Used only in P3 just define it for P2 also.
99 */
100 qla82xx_crb_addr_transform(I2C0);
101
102 qla82xx_crb_table_initialized = 1;
103}
104
Saurav Kashyapfa492632012-11-21 02:40:29 -0500105static struct crb_128M_2M_block_map crb_128M_2M_map[64] = {
Giridhar Malavalia9083012010-04-12 17:59:55 -0700106 {{{0, 0, 0, 0} } },
107 {{{1, 0x0100000, 0x0102000, 0x120000},
108 {1, 0x0110000, 0x0120000, 0x130000},
109 {1, 0x0120000, 0x0122000, 0x124000},
110 {1, 0x0130000, 0x0132000, 0x126000},
111 {1, 0x0140000, 0x0142000, 0x128000},
112 {1, 0x0150000, 0x0152000, 0x12a000},
113 {1, 0x0160000, 0x0170000, 0x110000},
114 {1, 0x0170000, 0x0172000, 0x12e000},
115 {0, 0x0000000, 0x0000000, 0x000000},
116 {0, 0x0000000, 0x0000000, 0x000000},
117 {0, 0x0000000, 0x0000000, 0x000000},
118 {0, 0x0000000, 0x0000000, 0x000000},
119 {0, 0x0000000, 0x0000000, 0x000000},
120 {0, 0x0000000, 0x0000000, 0x000000},
121 {1, 0x01e0000, 0x01e0800, 0x122000},
122 {0, 0x0000000, 0x0000000, 0x000000} } } ,
123 {{{1, 0x0200000, 0x0210000, 0x180000} } },
124 {{{0, 0, 0, 0} } },
125 {{{1, 0x0400000, 0x0401000, 0x169000} } },
126 {{{1, 0x0500000, 0x0510000, 0x140000} } },
127 {{{1, 0x0600000, 0x0610000, 0x1c0000} } },
128 {{{1, 0x0700000, 0x0704000, 0x1b8000} } },
129 {{{1, 0x0800000, 0x0802000, 0x170000},
130 {0, 0x0000000, 0x0000000, 0x000000},
131 {0, 0x0000000, 0x0000000, 0x000000},
132 {0, 0x0000000, 0x0000000, 0x000000},
133 {0, 0x0000000, 0x0000000, 0x000000},
134 {0, 0x0000000, 0x0000000, 0x000000},
135 {0, 0x0000000, 0x0000000, 0x000000},
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 {1, 0x08f0000, 0x08f2000, 0x172000} } },
145 {{{1, 0x0900000, 0x0902000, 0x174000},
146 {0, 0x0000000, 0x0000000, 0x000000},
147 {0, 0x0000000, 0x0000000, 0x000000},
148 {0, 0x0000000, 0x0000000, 0x000000},
149 {0, 0x0000000, 0x0000000, 0x000000},
150 {0, 0x0000000, 0x0000000, 0x000000},
151 {0, 0x0000000, 0x0000000, 0x000000},
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 {1, 0x09f0000, 0x09f2000, 0x176000} } },
161 {{{0, 0x0a00000, 0x0a02000, 0x178000},
162 {0, 0x0000000, 0x0000000, 0x000000},
163 {0, 0x0000000, 0x0000000, 0x000000},
164 {0, 0x0000000, 0x0000000, 0x000000},
165 {0, 0x0000000, 0x0000000, 0x000000},
166 {0, 0x0000000, 0x0000000, 0x000000},
167 {0, 0x0000000, 0x0000000, 0x000000},
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 {1, 0x0af0000, 0x0af2000, 0x17a000} } },
177 {{{0, 0x0b00000, 0x0b02000, 0x17c000},
178 {0, 0x0000000, 0x0000000, 0x000000},
179 {0, 0x0000000, 0x0000000, 0x000000},
180 {0, 0x0000000, 0x0000000, 0x000000},
181 {0, 0x0000000, 0x0000000, 0x000000},
182 {0, 0x0000000, 0x0000000, 0x000000},
183 {0, 0x0000000, 0x0000000, 0x000000},
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 {1, 0x0bf0000, 0x0bf2000, 0x17e000} } },
193 {{{1, 0x0c00000, 0x0c04000, 0x1d4000} } },
194 {{{1, 0x0d00000, 0x0d04000, 0x1a4000} } },
195 {{{1, 0x0e00000, 0x0e04000, 0x1a0000} } },
196 {{{1, 0x0f00000, 0x0f01000, 0x164000} } },
197 {{{0, 0x1000000, 0x1004000, 0x1a8000} } },
198 {{{1, 0x1100000, 0x1101000, 0x160000} } },
199 {{{1, 0x1200000, 0x1201000, 0x161000} } },
200 {{{1, 0x1300000, 0x1301000, 0x162000} } },
201 {{{1, 0x1400000, 0x1401000, 0x163000} } },
202 {{{1, 0x1500000, 0x1501000, 0x165000} } },
203 {{{1, 0x1600000, 0x1601000, 0x166000} } },
204 {{{0, 0, 0, 0} } },
205 {{{0, 0, 0, 0} } },
206 {{{0, 0, 0, 0} } },
207 {{{0, 0, 0, 0} } },
208 {{{0, 0, 0, 0} } },
209 {{{0, 0, 0, 0} } },
210 {{{1, 0x1d00000, 0x1d10000, 0x190000} } },
211 {{{1, 0x1e00000, 0x1e01000, 0x16a000} } },
212 {{{1, 0x1f00000, 0x1f10000, 0x150000} } },
213 {{{0} } },
214 {{{1, 0x2100000, 0x2102000, 0x120000},
215 {1, 0x2110000, 0x2120000, 0x130000},
216 {1, 0x2120000, 0x2122000, 0x124000},
217 {1, 0x2130000, 0x2132000, 0x126000},
218 {1, 0x2140000, 0x2142000, 0x128000},
219 {1, 0x2150000, 0x2152000, 0x12a000},
220 {1, 0x2160000, 0x2170000, 0x110000},
221 {1, 0x2170000, 0x2172000, 0x12e000},
222 {0, 0x0000000, 0x0000000, 0x000000},
223 {0, 0x0000000, 0x0000000, 0x000000},
224 {0, 0x0000000, 0x0000000, 0x000000},
225 {0, 0x0000000, 0x0000000, 0x000000},
226 {0, 0x0000000, 0x0000000, 0x000000},
227 {0, 0x0000000, 0x0000000, 0x000000},
228 {0, 0x0000000, 0x0000000, 0x000000},
229 {0, 0x0000000, 0x0000000, 0x000000} } },
230 {{{1, 0x2200000, 0x2204000, 0x1b0000} } },
231 {{{0} } },
232 {{{0} } },
233 {{{0} } },
234 {{{0} } },
235 {{{0} } },
236 {{{1, 0x2800000, 0x2804000, 0x1a4000} } },
237 {{{1, 0x2900000, 0x2901000, 0x16b000} } },
238 {{{1, 0x2a00000, 0x2a00400, 0x1ac400} } },
239 {{{1, 0x2b00000, 0x2b00400, 0x1ac800} } },
240 {{{1, 0x2c00000, 0x2c00400, 0x1acc00} } },
241 {{{1, 0x2d00000, 0x2d00400, 0x1ad000} } },
242 {{{1, 0x2e00000, 0x2e00400, 0x1ad400} } },
243 {{{1, 0x2f00000, 0x2f00400, 0x1ad800} } },
244 {{{1, 0x3000000, 0x3000400, 0x1adc00} } },
245 {{{0, 0x3100000, 0x3104000, 0x1a8000} } },
246 {{{1, 0x3200000, 0x3204000, 0x1d4000} } },
247 {{{1, 0x3300000, 0x3304000, 0x1a0000} } },
248 {{{0} } },
249 {{{1, 0x3500000, 0x3500400, 0x1ac000} } },
250 {{{1, 0x3600000, 0x3600400, 0x1ae000} } },
251 {{{1, 0x3700000, 0x3700400, 0x1ae400} } },
252 {{{1, 0x3800000, 0x3804000, 0x1d0000} } },
253 {{{1, 0x3900000, 0x3904000, 0x1b4000} } },
254 {{{1, 0x3a00000, 0x3a04000, 0x1d8000} } },
255 {{{0} } },
256 {{{0} } },
257 {{{1, 0x3d00000, 0x3d04000, 0x1dc000} } },
258 {{{1, 0x3e00000, 0x3e01000, 0x167000} } },
259 {{{1, 0x3f00000, 0x3f01000, 0x168000} } }
260};
261
262/*
263 * top 12 bits of crb internal address (hub, agent)
264 */
Saurav Kashyapfa492632012-11-21 02:40:29 -0500265static unsigned qla82xx_crb_hub_agt[64] = {
Giridhar Malavalia9083012010-04-12 17:59:55 -0700266 0,
267 QLA82XX_HW_CRB_HUB_AGT_ADR_PS,
268 QLA82XX_HW_CRB_HUB_AGT_ADR_MN,
269 QLA82XX_HW_CRB_HUB_AGT_ADR_MS,
270 0,
271 QLA82XX_HW_CRB_HUB_AGT_ADR_SRE,
272 QLA82XX_HW_CRB_HUB_AGT_ADR_NIU,
273 QLA82XX_HW_CRB_HUB_AGT_ADR_QMN,
274 QLA82XX_HW_CRB_HUB_AGT_ADR_SQN0,
275 QLA82XX_HW_CRB_HUB_AGT_ADR_SQN1,
276 QLA82XX_HW_CRB_HUB_AGT_ADR_SQN2,
277 QLA82XX_HW_CRB_HUB_AGT_ADR_SQN3,
278 QLA82XX_HW_CRB_HUB_AGT_ADR_I2Q,
279 QLA82XX_HW_CRB_HUB_AGT_ADR_TIMR,
280 QLA82XX_HW_CRB_HUB_AGT_ADR_ROMUSB,
281 QLA82XX_HW_CRB_HUB_AGT_ADR_PGN4,
282 QLA82XX_HW_CRB_HUB_AGT_ADR_XDMA,
283 QLA82XX_HW_CRB_HUB_AGT_ADR_PGN0,
284 QLA82XX_HW_CRB_HUB_AGT_ADR_PGN1,
285 QLA82XX_HW_CRB_HUB_AGT_ADR_PGN2,
286 QLA82XX_HW_CRB_HUB_AGT_ADR_PGN3,
287 QLA82XX_HW_CRB_HUB_AGT_ADR_PGND,
288 QLA82XX_HW_CRB_HUB_AGT_ADR_PGNI,
289 QLA82XX_HW_CRB_HUB_AGT_ADR_PGS0,
290 QLA82XX_HW_CRB_HUB_AGT_ADR_PGS1,
291 QLA82XX_HW_CRB_HUB_AGT_ADR_PGS2,
292 QLA82XX_HW_CRB_HUB_AGT_ADR_PGS3,
293 0,
294 QLA82XX_HW_CRB_HUB_AGT_ADR_PGSI,
295 QLA82XX_HW_CRB_HUB_AGT_ADR_SN,
296 0,
297 QLA82XX_HW_CRB_HUB_AGT_ADR_EG,
298 0,
299 QLA82XX_HW_CRB_HUB_AGT_ADR_PS,
300 QLA82XX_HW_CRB_HUB_AGT_ADR_CAM,
301 0,
302 0,
303 0,
304 0,
305 0,
306 QLA82XX_HW_CRB_HUB_AGT_ADR_TIMR,
307 0,
308 QLA82XX_HW_CRB_HUB_AGT_ADR_RPMX1,
309 QLA82XX_HW_CRB_HUB_AGT_ADR_RPMX2,
310 QLA82XX_HW_CRB_HUB_AGT_ADR_RPMX3,
311 QLA82XX_HW_CRB_HUB_AGT_ADR_RPMX4,
312 QLA82XX_HW_CRB_HUB_AGT_ADR_RPMX5,
313 QLA82XX_HW_CRB_HUB_AGT_ADR_RPMX6,
314 QLA82XX_HW_CRB_HUB_AGT_ADR_RPMX7,
315 QLA82XX_HW_CRB_HUB_AGT_ADR_XDMA,
316 QLA82XX_HW_CRB_HUB_AGT_ADR_I2Q,
317 QLA82XX_HW_CRB_HUB_AGT_ADR_ROMUSB,
318 0,
319 QLA82XX_HW_CRB_HUB_AGT_ADR_RPMX0,
320 QLA82XX_HW_CRB_HUB_AGT_ADR_RPMX8,
321 QLA82XX_HW_CRB_HUB_AGT_ADR_RPMX9,
322 QLA82XX_HW_CRB_HUB_AGT_ADR_OCM0,
323 0,
324 QLA82XX_HW_CRB_HUB_AGT_ADR_SMB,
325 QLA82XX_HW_CRB_HUB_AGT_ADR_I2C0,
326 QLA82XX_HW_CRB_HUB_AGT_ADR_I2C1,
327 0,
328 QLA82XX_HW_CRB_HUB_AGT_ADR_PGNC,
329 0,
330};
331
Giridhar Malavalif1af6202010-05-04 15:01:34 -0700332/* Device states */
Saurav Kashyapfa492632012-11-21 02:40:29 -0500333static char *q_dev_state[] = {
Giridhar Malavalif1af6202010-05-04 15:01:34 -0700334 "Unknown",
335 "Cold",
336 "Initializing",
337 "Ready",
338 "Need Reset",
339 "Need Quiescent",
340 "Failed",
341 "Quiescent",
342};
343
Giridhar Malavali08de2842011-08-16 11:31:44 -0700344char *qdev_state(uint32_t dev_state)
345{
346 return q_dev_state[dev_state];
347}
348
Giridhar Malavalia9083012010-04-12 17:59:55 -0700349/*
350 * In: 'off' is offset from CRB space in 128M pci map
351 * Out: 'off' is 2M pci map addr
352 * side effect: lock crb window
353 */
354static void
355qla82xx_pci_set_crbwindow_2M(struct qla_hw_data *ha, ulong *off)
356{
357 u32 win_read;
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700358 scsi_qla_host_t *vha = pci_get_drvdata(ha->pdev);
Giridhar Malavalia9083012010-04-12 17:59:55 -0700359
360 ha->crb_win = CRB_HI(*off);
361 writel(ha->crb_win,
Saurav Kashyapfa492632012-11-21 02:40:29 -0500362 (void __iomem *)(CRB_WINDOW_2M + ha->nx_pcibase));
Giridhar Malavalia9083012010-04-12 17:59:55 -0700363
364 /* Read back value to make sure write has gone through before trying
365 * to use it.
366 */
Saurav Kashyapfa492632012-11-21 02:40:29 -0500367 win_read = RD_REG_DWORD((void __iomem *)
368 (CRB_WINDOW_2M + ha->nx_pcibase));
Giridhar Malavalia9083012010-04-12 17:59:55 -0700369 if (win_read != ha->crb_win) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700370 ql_dbg(ql_dbg_p3p, vha, 0xb000,
371 "%s: Written crbwin (0x%x) "
372 "!= Read crbwin (0x%x), off=0x%lx.\n",
Joe Perchesd8424f62011-11-18 09:03:06 -0800373 __func__, ha->crb_win, win_read, *off);
Giridhar Malavalia9083012010-04-12 17:59:55 -0700374 }
375 *off = (*off & MASK(16)) + CRB_INDIRECT_2M + ha->nx_pcibase;
376}
377
378static inline unsigned long
379qla82xx_pci_set_crbwindow(struct qla_hw_data *ha, u64 off)
380{
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700381 scsi_qla_host_t *vha = pci_get_drvdata(ha->pdev);
Giridhar Malavalia9083012010-04-12 17:59:55 -0700382 /* See if we are currently pointing to the region we want to use next */
383 if ((off >= QLA82XX_CRB_PCIX_HOST) && (off < QLA82XX_CRB_DDR_NET)) {
384 /* No need to change window. PCIX and PCIEregs are in both
385 * regs are in both windows.
386 */
387 return off;
388 }
389
390 if ((off >= QLA82XX_CRB_PCIX_HOST) && (off < QLA82XX_CRB_PCIX_HOST2)) {
391 /* We are in first CRB window */
392 if (ha->curr_window != 0)
393 WARN_ON(1);
394 return off;
395 }
396
397 if ((off > QLA82XX_CRB_PCIX_HOST2) && (off < QLA82XX_CRB_MAX)) {
398 /* We are in second CRB window */
399 off = off - QLA82XX_CRB_PCIX_HOST2 + QLA82XX_CRB_PCIX_HOST;
400
401 if (ha->curr_window != 1)
402 return off;
403
404 /* We are in the QM or direct access
405 * register region - do nothing
406 */
407 if ((off >= QLA82XX_PCI_DIRECT_CRB) &&
408 (off < QLA82XX_PCI_CAMQM_MAX))
409 return off;
410 }
411 /* strange address given */
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700412 ql_dbg(ql_dbg_p3p, vha, 0xb001,
Joe Perchesd8424f62011-11-18 09:03:06 -0800413 "%s: Warning: unm_nic_pci_set_crbwindow "
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700414 "called with an unknown address(%llx).\n",
415 QLA2XXX_DRIVER_NAME, off);
Giridhar Malavalia9083012010-04-12 17:59:55 -0700416 return off;
417}
418
Giridhar Malavali77e334d2010-09-03 15:20:52 -0700419static int
420qla82xx_pci_get_crb_addr_2M(struct qla_hw_data *ha, ulong *off)
421{
422 struct crb_128M_2M_sub_block_map *m;
423
424 if (*off >= QLA82XX_CRB_MAX)
425 return -1;
426
427 if (*off >= QLA82XX_PCI_CAMQM && (*off < QLA82XX_PCI_CAMQM_2M_END)) {
428 *off = (*off - QLA82XX_PCI_CAMQM) +
429 QLA82XX_PCI_CAMQM_2M_BASE + ha->nx_pcibase;
430 return 0;
431 }
432
433 if (*off < QLA82XX_PCI_CRBSPACE)
434 return -1;
435
436 *off -= QLA82XX_PCI_CRBSPACE;
437
438 /* Try direct map */
439 m = &crb_128M_2M_map[CRB_BLK(*off)].sub_block[CRB_SUBBLK(*off)];
440
441 if (m->valid && (m->start_128M <= *off) && (m->end_128M > *off)) {
442 *off = *off + m->start_2M - m->start_128M + ha->nx_pcibase;
443 return 0;
444 }
445 /* Not in direct map, use crb window */
446 return 1;
447}
448
449#define CRB_WIN_LOCK_TIMEOUT 100000000
450static int qla82xx_crb_win_lock(struct qla_hw_data *ha)
451{
452 int done = 0, timeout = 0;
453
454 while (!done) {
455 /* acquire semaphore3 from PCI HW block */
456 done = qla82xx_rd_32(ha, QLA82XX_PCIE_REG(PCIE_SEM7_LOCK));
457 if (done == 1)
458 break;
459 if (timeout >= CRB_WIN_LOCK_TIMEOUT)
460 return -1;
461 timeout++;
462 }
463 qla82xx_wr_32(ha, QLA82XX_CRB_WIN_LOCK_ID, ha->portnum);
464 return 0;
465}
466
Giridhar Malavalia9083012010-04-12 17:59:55 -0700467int
468qla82xx_wr_32(struct qla_hw_data *ha, ulong off, u32 data)
469{
470 unsigned long flags = 0;
471 int rv;
472
473 rv = qla82xx_pci_get_crb_addr_2M(ha, &off);
474
475 BUG_ON(rv == -1);
476
477 if (rv == 1) {
478 write_lock_irqsave(&ha->hw_lock, flags);
479 qla82xx_crb_win_lock(ha);
480 qla82xx_pci_set_crbwindow_2M(ha, &off);
481 }
482
483 writel(data, (void __iomem *)off);
484
485 if (rv == 1) {
486 qla82xx_rd_32(ha, QLA82XX_PCIE_REG(PCIE_SEM7_UNLOCK));
487 write_unlock_irqrestore(&ha->hw_lock, flags);
488 }
489 return 0;
490}
491
492int
493qla82xx_rd_32(struct qla_hw_data *ha, ulong off)
494{
495 unsigned long flags = 0;
496 int rv;
497 u32 data;
498
499 rv = qla82xx_pci_get_crb_addr_2M(ha, &off);
500
501 BUG_ON(rv == -1);
502
503 if (rv == 1) {
504 write_lock_irqsave(&ha->hw_lock, flags);
505 qla82xx_crb_win_lock(ha);
506 qla82xx_pci_set_crbwindow_2M(ha, &off);
507 }
508 data = RD_REG_DWORD((void __iomem *)off);
509
510 if (rv == 1) {
511 qla82xx_rd_32(ha, QLA82XX_PCIE_REG(PCIE_SEM7_UNLOCK));
512 write_unlock_irqrestore(&ha->hw_lock, flags);
513 }
514 return data;
515}
516
Giridhar Malavalia9083012010-04-12 17:59:55 -0700517#define IDC_LOCK_TIMEOUT 100000000
518int qla82xx_idc_lock(struct qla_hw_data *ha)
519{
520 int i;
521 int done = 0, timeout = 0;
522
523 while (!done) {
524 /* acquire semaphore5 from PCI HW block */
525 done = qla82xx_rd_32(ha, QLA82XX_PCIE_REG(PCIE_SEM5_LOCK));
526 if (done == 1)
527 break;
528 if (timeout >= IDC_LOCK_TIMEOUT)
529 return -1;
530
531 timeout++;
532
533 /* Yield CPU */
534 if (!in_interrupt())
535 schedule();
536 else {
537 for (i = 0; i < 20; i++)
538 cpu_relax();
539 }
540 }
541
542 return 0;
543}
544
545void qla82xx_idc_unlock(struct qla_hw_data *ha)
546{
547 qla82xx_rd_32(ha, QLA82XX_PCIE_REG(PCIE_SEM5_UNLOCK));
548}
549
Giridhar Malavalia9083012010-04-12 17:59:55 -0700550/* PCI Windowing for DDR regions. */
551#define QLA82XX_ADDR_IN_RANGE(addr, low, high) \
552 (((addr) <= (high)) && ((addr) >= (low)))
553/*
554 * check memory access boundary.
555 * used by test agent. support ddr access only for now
556 */
557static unsigned long
558qla82xx_pci_mem_bound_check(struct qla_hw_data *ha,
559 unsigned long long addr, int size)
560{
561 if (!QLA82XX_ADDR_IN_RANGE(addr, QLA82XX_ADDR_DDR_NET,
562 QLA82XX_ADDR_DDR_NET_MAX) ||
563 !QLA82XX_ADDR_IN_RANGE(addr + size - 1, QLA82XX_ADDR_DDR_NET,
564 QLA82XX_ADDR_DDR_NET_MAX) ||
565 ((size != 1) && (size != 2) && (size != 4) && (size != 8)))
566 return 0;
567 else
568 return 1;
569}
570
Saurav Kashyapfa492632012-11-21 02:40:29 -0500571static int qla82xx_pci_set_window_warning_count;
Giridhar Malavalia9083012010-04-12 17:59:55 -0700572
Giridhar Malavali77e334d2010-09-03 15:20:52 -0700573static unsigned long
Giridhar Malavalia9083012010-04-12 17:59:55 -0700574qla82xx_pci_set_window(struct qla_hw_data *ha, unsigned long long addr)
575{
576 int window;
577 u32 win_read;
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700578 scsi_qla_host_t *vha = pci_get_drvdata(ha->pdev);
Giridhar Malavalia9083012010-04-12 17:59:55 -0700579
580 if (QLA82XX_ADDR_IN_RANGE(addr, QLA82XX_ADDR_DDR_NET,
581 QLA82XX_ADDR_DDR_NET_MAX)) {
582 /* DDR network side */
583 window = MN_WIN(addr);
584 ha->ddr_mn_window = window;
585 qla82xx_wr_32(ha,
586 ha->mn_win_crb | QLA82XX_PCI_CRBSPACE, window);
587 win_read = qla82xx_rd_32(ha,
588 ha->mn_win_crb | QLA82XX_PCI_CRBSPACE);
589 if ((win_read << 17) != window) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700590 ql_dbg(ql_dbg_p3p, vha, 0xb003,
591 "%s: Written MNwin (0x%x) != Read MNwin (0x%x).\n",
Giridhar Malavalia9083012010-04-12 17:59:55 -0700592 __func__, window, win_read);
593 }
594 addr = GET_MEM_OFFS_2M(addr) + QLA82XX_PCI_DDR_NET;
595 } else if (QLA82XX_ADDR_IN_RANGE(addr, QLA82XX_ADDR_OCM0,
596 QLA82XX_ADDR_OCM0_MAX)) {
597 unsigned int temp1;
598 if ((addr & 0x00ff800) == 0xff800) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700599 ql_log(ql_log_warn, vha, 0xb004,
Giridhar Malavalia9083012010-04-12 17:59:55 -0700600 "%s: QM access not handled.\n", __func__);
601 addr = -1UL;
602 }
603 window = OCM_WIN(addr);
604 ha->ddr_mn_window = window;
605 qla82xx_wr_32(ha,
606 ha->mn_win_crb | QLA82XX_PCI_CRBSPACE, window);
607 win_read = qla82xx_rd_32(ha,
608 ha->mn_win_crb | QLA82XX_PCI_CRBSPACE);
609 temp1 = ((window & 0x1FF) << 7) |
610 ((window & 0x0FFFE0000) >> 17);
611 if (win_read != temp1) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700612 ql_log(ql_log_warn, vha, 0xb005,
613 "%s: Written OCMwin (0x%x) != Read OCMwin (0x%x).\n",
Giridhar Malavalia9083012010-04-12 17:59:55 -0700614 __func__, temp1, win_read);
615 }
616 addr = GET_MEM_OFFS_2M(addr) + QLA82XX_PCI_OCM0_2M;
617
618 } else if (QLA82XX_ADDR_IN_RANGE(addr, QLA82XX_ADDR_QDR_NET,
619 QLA82XX_P3_ADDR_QDR_NET_MAX)) {
620 /* QDR network side */
621 window = MS_WIN(addr);
622 ha->qdr_sn_window = window;
623 qla82xx_wr_32(ha,
624 ha->ms_win_crb | QLA82XX_PCI_CRBSPACE, window);
625 win_read = qla82xx_rd_32(ha,
626 ha->ms_win_crb | QLA82XX_PCI_CRBSPACE);
627 if (win_read != window) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700628 ql_log(ql_log_warn, vha, 0xb006,
629 "%s: Written MSwin (0x%x) != Read MSwin (0x%x).\n",
Giridhar Malavalia9083012010-04-12 17:59:55 -0700630 __func__, window, win_read);
631 }
632 addr = GET_MEM_OFFS_2M(addr) + QLA82XX_PCI_QDR_NET;
633 } else {
634 /*
635 * peg gdb frequently accesses memory that doesn't exist,
636 * this limits the chit chat so debugging isn't slowed down.
637 */
638 if ((qla82xx_pci_set_window_warning_count++ < 8) ||
639 (qla82xx_pci_set_window_warning_count%64 == 0)) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700640 ql_log(ql_log_warn, vha, 0xb007,
641 "%s: Warning:%s Unknown address range!.\n",
642 __func__, QLA2XXX_DRIVER_NAME);
Giridhar Malavalia9083012010-04-12 17:59:55 -0700643 }
644 addr = -1UL;
645 }
646 return addr;
647}
648
649/* check if address is in the same windows as the previous access */
650static int qla82xx_pci_is_same_window(struct qla_hw_data *ha,
651 unsigned long long addr)
652{
653 int window;
654 unsigned long long qdr_max;
655
656 qdr_max = QLA82XX_P3_ADDR_QDR_NET_MAX;
657
658 /* DDR network side */
659 if (QLA82XX_ADDR_IN_RANGE(addr, QLA82XX_ADDR_DDR_NET,
660 QLA82XX_ADDR_DDR_NET_MAX))
661 BUG();
662 else if (QLA82XX_ADDR_IN_RANGE(addr, QLA82XX_ADDR_OCM0,
663 QLA82XX_ADDR_OCM0_MAX))
664 return 1;
665 else if (QLA82XX_ADDR_IN_RANGE(addr, QLA82XX_ADDR_OCM1,
666 QLA82XX_ADDR_OCM1_MAX))
667 return 1;
668 else if (QLA82XX_ADDR_IN_RANGE(addr, QLA82XX_ADDR_QDR_NET, qdr_max)) {
669 /* QDR network side */
670 window = ((addr - QLA82XX_ADDR_QDR_NET) >> 22) & 0x3f;
671 if (ha->qdr_sn_window == window)
672 return 1;
673 }
674 return 0;
675}
676
677static int qla82xx_pci_mem_read_direct(struct qla_hw_data *ha,
678 u64 off, void *data, int size)
679{
680 unsigned long flags;
Saurav Kashyapfa492632012-11-21 02:40:29 -0500681 void __iomem *addr = NULL;
Giridhar Malavalia9083012010-04-12 17:59:55 -0700682 int ret = 0;
683 u64 start;
Saurav Kashyapfa492632012-11-21 02:40:29 -0500684 uint8_t __iomem *mem_ptr = NULL;
Giridhar Malavalia9083012010-04-12 17:59:55 -0700685 unsigned long mem_base;
686 unsigned long mem_page;
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700687 scsi_qla_host_t *vha = pci_get_drvdata(ha->pdev);
Giridhar Malavalia9083012010-04-12 17:59:55 -0700688
689 write_lock_irqsave(&ha->hw_lock, flags);
690
691 /*
692 * If attempting to access unknown address or straddle hw windows,
693 * do not access.
694 */
695 start = qla82xx_pci_set_window(ha, off);
696 if ((start == -1UL) ||
697 (qla82xx_pci_is_same_window(ha, off + size - 1) == 0)) {
698 write_unlock_irqrestore(&ha->hw_lock, flags);
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700699 ql_log(ql_log_fatal, vha, 0xb008,
700 "%s out of bound pci memory "
701 "access, offset is 0x%llx.\n",
702 QLA2XXX_DRIVER_NAME, off);
Giridhar Malavalia9083012010-04-12 17:59:55 -0700703 return -1;
704 }
705
Giridhar Malavalif1af6202010-05-04 15:01:34 -0700706 write_unlock_irqrestore(&ha->hw_lock, flags);
707 mem_base = pci_resource_start(ha->pdev, 0);
708 mem_page = start & PAGE_MASK;
709 /* Map two pages whenever user tries to access addresses in two
710 * consecutive pages.
711 */
712 if (mem_page != ((start + size - 1) & PAGE_MASK))
713 mem_ptr = ioremap(mem_base + mem_page, PAGE_SIZE * 2);
714 else
715 mem_ptr = ioremap(mem_base + mem_page, PAGE_SIZE);
Saurav Kashyapfa492632012-11-21 02:40:29 -0500716 if (mem_ptr == NULL) {
Giridhar Malavalif1af6202010-05-04 15:01:34 -0700717 *(u8 *)data = 0;
718 return -1;
Giridhar Malavalia9083012010-04-12 17:59:55 -0700719 }
Giridhar Malavalif1af6202010-05-04 15:01:34 -0700720 addr = mem_ptr;
721 addr += start & (PAGE_SIZE - 1);
722 write_lock_irqsave(&ha->hw_lock, flags);
Giridhar Malavalia9083012010-04-12 17:59:55 -0700723
724 switch (size) {
725 case 1:
726 *(u8 *)data = readb(addr);
727 break;
728 case 2:
729 *(u16 *)data = readw(addr);
730 break;
731 case 4:
732 *(u32 *)data = readl(addr);
733 break;
734 case 8:
735 *(u64 *)data = readq(addr);
736 break;
737 default:
738 ret = -1;
739 break;
740 }
741 write_unlock_irqrestore(&ha->hw_lock, flags);
742
743 if (mem_ptr)
744 iounmap(mem_ptr);
745 return ret;
746}
747
748static int
749qla82xx_pci_mem_write_direct(struct qla_hw_data *ha,
750 u64 off, void *data, int size)
751{
752 unsigned long flags;
Saurav Kashyapfa492632012-11-21 02:40:29 -0500753 void __iomem *addr = NULL;
Giridhar Malavalia9083012010-04-12 17:59:55 -0700754 int ret = 0;
755 u64 start;
Saurav Kashyapfa492632012-11-21 02:40:29 -0500756 uint8_t __iomem *mem_ptr = NULL;
Giridhar Malavalia9083012010-04-12 17:59:55 -0700757 unsigned long mem_base;
758 unsigned long mem_page;
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700759 scsi_qla_host_t *vha = pci_get_drvdata(ha->pdev);
Giridhar Malavalia9083012010-04-12 17:59:55 -0700760
761 write_lock_irqsave(&ha->hw_lock, flags);
762
763 /*
764 * If attempting to access unknown address or straddle hw windows,
765 * do not access.
766 */
767 start = qla82xx_pci_set_window(ha, off);
768 if ((start == -1UL) ||
769 (qla82xx_pci_is_same_window(ha, off + size - 1) == 0)) {
770 write_unlock_irqrestore(&ha->hw_lock, flags);
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700771 ql_log(ql_log_fatal, vha, 0xb009,
772 "%s out of bount memory "
773 "access, offset is 0x%llx.\n",
774 QLA2XXX_DRIVER_NAME, off);
Giridhar Malavalia9083012010-04-12 17:59:55 -0700775 return -1;
776 }
777
Giridhar Malavalif1af6202010-05-04 15:01:34 -0700778 write_unlock_irqrestore(&ha->hw_lock, flags);
779 mem_base = pci_resource_start(ha->pdev, 0);
780 mem_page = start & PAGE_MASK;
781 /* Map two pages whenever user tries to access addresses in two
782 * consecutive pages.
783 */
784 if (mem_page != ((start + size - 1) & PAGE_MASK))
785 mem_ptr = ioremap(mem_base + mem_page, PAGE_SIZE*2);
786 else
787 mem_ptr = ioremap(mem_base + mem_page, PAGE_SIZE);
Saurav Kashyapfa492632012-11-21 02:40:29 -0500788 if (mem_ptr == NULL)
Giridhar Malavalif1af6202010-05-04 15:01:34 -0700789 return -1;
Giridhar Malavalia9083012010-04-12 17:59:55 -0700790
Giridhar Malavalif1af6202010-05-04 15:01:34 -0700791 addr = mem_ptr;
792 addr += start & (PAGE_SIZE - 1);
793 write_lock_irqsave(&ha->hw_lock, flags);
Giridhar Malavalia9083012010-04-12 17:59:55 -0700794
795 switch (size) {
796 case 1:
797 writeb(*(u8 *)data, addr);
798 break;
799 case 2:
800 writew(*(u16 *)data, addr);
801 break;
802 case 4:
803 writel(*(u32 *)data, addr);
804 break;
805 case 8:
806 writeq(*(u64 *)data, addr);
807 break;
808 default:
809 ret = -1;
810 break;
811 }
812 write_unlock_irqrestore(&ha->hw_lock, flags);
813 if (mem_ptr)
814 iounmap(mem_ptr);
815 return ret;
816}
817
Giridhar Malavalia9083012010-04-12 17:59:55 -0700818#define MTU_FUDGE_FACTOR 100
Giridhar Malavali77e334d2010-09-03 15:20:52 -0700819static unsigned long
820qla82xx_decode_crb_addr(unsigned long addr)
Giridhar Malavalia9083012010-04-12 17:59:55 -0700821{
822 int i;
823 unsigned long base_addr, offset, pci_base;
824
825 if (!qla82xx_crb_table_initialized)
826 qla82xx_crb_addr_transform_setup();
827
828 pci_base = ADDR_ERROR;
829 base_addr = addr & 0xfff00000;
830 offset = addr & 0x000fffff;
831
832 for (i = 0; i < MAX_CRB_XFORM; i++) {
833 if (crb_addr_xform[i] == base_addr) {
834 pci_base = i << 20;
835 break;
836 }
837 }
838 if (pci_base == ADDR_ERROR)
839 return pci_base;
840 return pci_base + offset;
841}
842
843static long rom_max_timeout = 100;
844static long qla82xx_rom_lock_timeout = 100;
845
Giridhar Malavali77e334d2010-09-03 15:20:52 -0700846static int
Giridhar Malavalia9083012010-04-12 17:59:55 -0700847qla82xx_rom_lock(struct qla_hw_data *ha)
848{
849 int done = 0, timeout = 0;
850
851 while (!done) {
852 /* acquire semaphore2 from PCI HW block */
853 done = qla82xx_rd_32(ha, QLA82XX_PCIE_REG(PCIE_SEM2_LOCK));
854 if (done == 1)
855 break;
856 if (timeout >= qla82xx_rom_lock_timeout)
857 return -1;
858 timeout++;
859 }
860 qla82xx_wr_32(ha, QLA82XX_ROM_LOCK_ID, ROM_LOCK_DRIVER);
861 return 0;
862}
863
Chad Dupuisd652e092011-05-10 11:30:10 -0700864static void
865qla82xx_rom_unlock(struct qla_hw_data *ha)
866{
867 qla82xx_rd_32(ha, QLA82XX_PCIE_REG(PCIE_SEM2_UNLOCK));
868}
869
Giridhar Malavali77e334d2010-09-03 15:20:52 -0700870static int
Giridhar Malavalia9083012010-04-12 17:59:55 -0700871qla82xx_wait_rom_busy(struct qla_hw_data *ha)
872{
873 long timeout = 0;
874 long done = 0 ;
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700875 scsi_qla_host_t *vha = pci_get_drvdata(ha->pdev);
Giridhar Malavalia9083012010-04-12 17:59:55 -0700876
877 while (done == 0) {
878 done = qla82xx_rd_32(ha, QLA82XX_ROMUSB_GLB_STATUS);
879 done &= 4;
880 timeout++;
881 if (timeout >= rom_max_timeout) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700882 ql_dbg(ql_dbg_p3p, vha, 0xb00a,
883 "%s: Timeout reached waiting for rom busy.\n",
884 QLA2XXX_DRIVER_NAME);
Giridhar Malavalia9083012010-04-12 17:59:55 -0700885 return -1;
886 }
887 }
888 return 0;
889}
890
Giridhar Malavali77e334d2010-09-03 15:20:52 -0700891static int
Giridhar Malavalia9083012010-04-12 17:59:55 -0700892qla82xx_wait_rom_done(struct qla_hw_data *ha)
893{
894 long timeout = 0;
895 long done = 0 ;
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700896 scsi_qla_host_t *vha = pci_get_drvdata(ha->pdev);
Giridhar Malavalia9083012010-04-12 17:59:55 -0700897
898 while (done == 0) {
899 done = qla82xx_rd_32(ha, QLA82XX_ROMUSB_GLB_STATUS);
900 done &= 2;
901 timeout++;
902 if (timeout >= rom_max_timeout) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700903 ql_dbg(ql_dbg_p3p, vha, 0xb00b,
904 "%s: Timeout reached waiting for rom done.\n",
905 QLA2XXX_DRIVER_NAME);
Giridhar Malavalia9083012010-04-12 17:59:55 -0700906 return -1;
907 }
908 }
909 return 0;
910}
911
Saurav Kashyapfa492632012-11-21 02:40:29 -0500912static int
Chad Dupuis2b29d962012-02-09 11:15:41 -0800913qla82xx_md_rw_32(struct qla_hw_data *ha, uint32_t off, u32 data, uint8_t flag)
914{
915 uint32_t off_value, rval = 0;
916
Saurav Kashyapfa492632012-11-21 02:40:29 -0500917 WRT_REG_DWORD((void __iomem *)(CRB_WINDOW_2M + ha->nx_pcibase),
Chad Dupuis2b29d962012-02-09 11:15:41 -0800918 (off & 0xFFFF0000));
919
920 /* Read back value to make sure write has gone through */
Saurav Kashyapfa492632012-11-21 02:40:29 -0500921 RD_REG_DWORD((void __iomem *)(CRB_WINDOW_2M + ha->nx_pcibase));
Chad Dupuis2b29d962012-02-09 11:15:41 -0800922 off_value = (off & 0x0000FFFF);
923
924 if (flag)
Saurav Kashyapfa492632012-11-21 02:40:29 -0500925 WRT_REG_DWORD((void __iomem *)
Chad Dupuis2b29d962012-02-09 11:15:41 -0800926 (off_value + CRB_INDIRECT_2M + ha->nx_pcibase),
927 data);
928 else
Saurav Kashyapfa492632012-11-21 02:40:29 -0500929 rval = RD_REG_DWORD((void __iomem *)
Chad Dupuis2b29d962012-02-09 11:15:41 -0800930 (off_value + CRB_INDIRECT_2M + ha->nx_pcibase));
931
932 return rval;
933}
934
Giridhar Malavali77e334d2010-09-03 15:20:52 -0700935static int
Giridhar Malavalia9083012010-04-12 17:59:55 -0700936qla82xx_do_rom_fast_read(struct qla_hw_data *ha, int addr, int *valp)
937{
Chad Dupuis2b29d962012-02-09 11:15:41 -0800938 /* Dword reads to flash. */
939 qla82xx_md_rw_32(ha, MD_DIRECT_ROM_WINDOW, (addr & 0xFFFF0000), 1);
940 *valp = qla82xx_md_rw_32(ha, MD_DIRECT_ROM_READ_BASE +
941 (addr & 0x0000FFFF), 0, 0);
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700942
Giridhar Malavalia9083012010-04-12 17:59:55 -0700943 return 0;
944}
945
Giridhar Malavali77e334d2010-09-03 15:20:52 -0700946static int
Giridhar Malavalia9083012010-04-12 17:59:55 -0700947qla82xx_rom_fast_read(struct qla_hw_data *ha, int addr, int *valp)
948{
949 int ret, loops = 0;
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700950 scsi_qla_host_t *vha = pci_get_drvdata(ha->pdev);
Giridhar Malavalia9083012010-04-12 17:59:55 -0700951
952 while ((qla82xx_rom_lock(ha) != 0) && (loops < 50000)) {
953 udelay(100);
954 schedule();
955 loops++;
956 }
957 if (loops >= 50000) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700958 ql_log(ql_log_fatal, vha, 0x00b9,
959 "Failed to aquire SEM2 lock.\n");
Giridhar Malavalia9083012010-04-12 17:59:55 -0700960 return -1;
961 }
962 ret = qla82xx_do_rom_fast_read(ha, addr, valp);
Chad Dupuisd652e092011-05-10 11:30:10 -0700963 qla82xx_rom_unlock(ha);
Giridhar Malavalia9083012010-04-12 17:59:55 -0700964 return ret;
965}
966
Giridhar Malavali77e334d2010-09-03 15:20:52 -0700967static int
Giridhar Malavalia9083012010-04-12 17:59:55 -0700968qla82xx_read_status_reg(struct qla_hw_data *ha, uint32_t *val)
969{
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700970 scsi_qla_host_t *vha = pci_get_drvdata(ha->pdev);
Giridhar Malavalia9083012010-04-12 17:59:55 -0700971 qla82xx_wr_32(ha, QLA82XX_ROMUSB_ROM_INSTR_OPCODE, M25P_INSTR_RDSR);
972 qla82xx_wait_rom_busy(ha);
973 if (qla82xx_wait_rom_done(ha)) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700974 ql_log(ql_log_warn, vha, 0xb00c,
975 "Error waiting for rom done.\n");
Giridhar Malavalia9083012010-04-12 17:59:55 -0700976 return -1;
977 }
978 *val = qla82xx_rd_32(ha, QLA82XX_ROMUSB_ROM_RDATA);
979 return 0;
980}
981
Giridhar Malavali77e334d2010-09-03 15:20:52 -0700982static int
Giridhar Malavalia9083012010-04-12 17:59:55 -0700983qla82xx_flash_wait_write_finish(struct qla_hw_data *ha)
984{
985 long timeout = 0;
986 uint32_t done = 1 ;
987 uint32_t val;
988 int ret = 0;
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700989 scsi_qla_host_t *vha = pci_get_drvdata(ha->pdev);
Giridhar Malavalia9083012010-04-12 17:59:55 -0700990
991 qla82xx_wr_32(ha, QLA82XX_ROMUSB_ROM_ABYTE_CNT, 0);
992 while ((done != 0) && (ret == 0)) {
993 ret = qla82xx_read_status_reg(ha, &val);
994 done = val & 1;
995 timeout++;
996 udelay(10);
997 cond_resched();
998 if (timeout >= 50000) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700999 ql_log(ql_log_warn, vha, 0xb00d,
1000 "Timeout reached waiting for write finish.\n");
Giridhar Malavalia9083012010-04-12 17:59:55 -07001001 return -1;
1002 }
1003 }
1004 return ret;
1005}
1006
Giridhar Malavali77e334d2010-09-03 15:20:52 -07001007static int
Giridhar Malavalia9083012010-04-12 17:59:55 -07001008qla82xx_flash_set_write_enable(struct qla_hw_data *ha)
1009{
1010 uint32_t val;
1011 qla82xx_wait_rom_busy(ha);
1012 qla82xx_wr_32(ha, QLA82XX_ROMUSB_ROM_ABYTE_CNT, 0);
1013 qla82xx_wr_32(ha, QLA82XX_ROMUSB_ROM_INSTR_OPCODE, M25P_INSTR_WREN);
1014 qla82xx_wait_rom_busy(ha);
1015 if (qla82xx_wait_rom_done(ha))
1016 return -1;
1017 if (qla82xx_read_status_reg(ha, &val) != 0)
1018 return -1;
1019 if ((val & 2) != 2)
1020 return -1;
1021 return 0;
1022}
1023
Giridhar Malavali77e334d2010-09-03 15:20:52 -07001024static int
Giridhar Malavalia9083012010-04-12 17:59:55 -07001025qla82xx_write_status_reg(struct qla_hw_data *ha, uint32_t val)
1026{
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001027 scsi_qla_host_t *vha = pci_get_drvdata(ha->pdev);
Giridhar Malavalia9083012010-04-12 17:59:55 -07001028 if (qla82xx_flash_set_write_enable(ha))
1029 return -1;
1030 qla82xx_wr_32(ha, QLA82XX_ROMUSB_ROM_WDATA, val);
1031 qla82xx_wr_32(ha, QLA82XX_ROMUSB_ROM_INSTR_OPCODE, 0x1);
1032 if (qla82xx_wait_rom_done(ha)) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001033 ql_log(ql_log_warn, vha, 0xb00e,
1034 "Error waiting for rom done.\n");
Giridhar Malavalia9083012010-04-12 17:59:55 -07001035 return -1;
1036 }
1037 return qla82xx_flash_wait_write_finish(ha);
1038}
1039
Giridhar Malavali77e334d2010-09-03 15:20:52 -07001040static int
Giridhar Malavalia9083012010-04-12 17:59:55 -07001041qla82xx_write_disable_flash(struct qla_hw_data *ha)
1042{
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001043 scsi_qla_host_t *vha = pci_get_drvdata(ha->pdev);
Giridhar Malavalia9083012010-04-12 17:59:55 -07001044 qla82xx_wr_32(ha, QLA82XX_ROMUSB_ROM_INSTR_OPCODE, M25P_INSTR_WRDI);
1045 if (qla82xx_wait_rom_done(ha)) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001046 ql_log(ql_log_warn, vha, 0xb00f,
1047 "Error waiting for rom done.\n");
Giridhar Malavalia9083012010-04-12 17:59:55 -07001048 return -1;
1049 }
1050 return 0;
1051}
1052
Giridhar Malavali77e334d2010-09-03 15:20:52 -07001053static int
Giridhar Malavalia9083012010-04-12 17:59:55 -07001054ql82xx_rom_lock_d(struct qla_hw_data *ha)
1055{
1056 int loops = 0;
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001057 scsi_qla_host_t *vha = pci_get_drvdata(ha->pdev);
1058
Giridhar Malavalia9083012010-04-12 17:59:55 -07001059 while ((qla82xx_rom_lock(ha) != 0) && (loops < 50000)) {
1060 udelay(100);
1061 cond_resched();
1062 loops++;
1063 }
1064 if (loops >= 50000) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001065 ql_log(ql_log_warn, vha, 0xb010,
1066 "ROM lock failed.\n");
Giridhar Malavalia9083012010-04-12 17:59:55 -07001067 return -1;
1068 }
Jesper Juhlcd6dbb02011-11-20 22:34:15 +01001069 return 0;
Giridhar Malavalia9083012010-04-12 17:59:55 -07001070}
1071
Giridhar Malavali77e334d2010-09-03 15:20:52 -07001072static int
Giridhar Malavalia9083012010-04-12 17:59:55 -07001073qla82xx_write_flash_dword(struct qla_hw_data *ha, uint32_t flashaddr,
1074 uint32_t data)
1075{
1076 int ret = 0;
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001077 scsi_qla_host_t *vha = pci_get_drvdata(ha->pdev);
Giridhar Malavalia9083012010-04-12 17:59:55 -07001078
1079 ret = ql82xx_rom_lock_d(ha);
1080 if (ret < 0) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001081 ql_log(ql_log_warn, vha, 0xb011,
1082 "ROM lock failed.\n");
Giridhar Malavalia9083012010-04-12 17:59:55 -07001083 return ret;
1084 }
1085
1086 if (qla82xx_flash_set_write_enable(ha))
1087 goto done_write;
1088
1089 qla82xx_wr_32(ha, QLA82XX_ROMUSB_ROM_WDATA, data);
1090 qla82xx_wr_32(ha, QLA82XX_ROMUSB_ROM_ADDRESS, flashaddr);
1091 qla82xx_wr_32(ha, QLA82XX_ROMUSB_ROM_ABYTE_CNT, 3);
1092 qla82xx_wr_32(ha, QLA82XX_ROMUSB_ROM_INSTR_OPCODE, M25P_INSTR_PP);
1093 qla82xx_wait_rom_busy(ha);
1094 if (qla82xx_wait_rom_done(ha)) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001095 ql_log(ql_log_warn, vha, 0xb012,
1096 "Error waiting for rom done.\n");
Giridhar Malavalia9083012010-04-12 17:59:55 -07001097 ret = -1;
1098 goto done_write;
1099 }
1100
1101 ret = qla82xx_flash_wait_write_finish(ha);
1102
1103done_write:
Chad Dupuisd652e092011-05-10 11:30:10 -07001104 qla82xx_rom_unlock(ha);
Giridhar Malavalia9083012010-04-12 17:59:55 -07001105 return ret;
1106}
1107
1108/* This routine does CRB initialize sequence
1109 * to put the ISP into operational state
1110 */
Giridhar Malavali77e334d2010-09-03 15:20:52 -07001111static int
1112qla82xx_pinit_from_rom(scsi_qla_host_t *vha)
Giridhar Malavalia9083012010-04-12 17:59:55 -07001113{
1114 int addr, val;
1115 int i ;
1116 struct crb_addr_pair *buf;
1117 unsigned long off;
1118 unsigned offset, n;
1119 struct qla_hw_data *ha = vha->hw;
1120
1121 struct crb_addr_pair {
1122 long addr;
1123 long data;
1124 };
1125
1126 /* Halt all the indiviual PEGs and other blocks of the ISP */
1127 qla82xx_rom_lock(ha);
Madhuranath Iyengarc9e8fd52010-12-21 16:00:19 -08001128
Giridhar Malavali02be2212011-03-30 11:46:24 -07001129 /* disable all I2Q */
1130 qla82xx_wr_32(ha, QLA82XX_CRB_I2Q + 0x10, 0x0);
1131 qla82xx_wr_32(ha, QLA82XX_CRB_I2Q + 0x14, 0x0);
1132 qla82xx_wr_32(ha, QLA82XX_CRB_I2Q + 0x18, 0x0);
1133 qla82xx_wr_32(ha, QLA82XX_CRB_I2Q + 0x1c, 0x0);
1134 qla82xx_wr_32(ha, QLA82XX_CRB_I2Q + 0x20, 0x0);
1135 qla82xx_wr_32(ha, QLA82XX_CRB_I2Q + 0x24, 0x0);
1136
1137 /* disable all niu interrupts */
Madhuranath Iyengarc9e8fd52010-12-21 16:00:19 -08001138 qla82xx_wr_32(ha, QLA82XX_CRB_NIU + 0x40, 0xff);
1139 /* disable xge rx/tx */
1140 qla82xx_wr_32(ha, QLA82XX_CRB_NIU + 0x70000, 0x00);
1141 /* disable xg1 rx/tx */
1142 qla82xx_wr_32(ha, QLA82XX_CRB_NIU + 0x80000, 0x00);
Giridhar Malavali02be2212011-03-30 11:46:24 -07001143 /* disable sideband mac */
1144 qla82xx_wr_32(ha, QLA82XX_CRB_NIU + 0x90000, 0x00);
1145 /* disable ap0 mac */
1146 qla82xx_wr_32(ha, QLA82XX_CRB_NIU + 0xa0000, 0x00);
1147 /* disable ap1 mac */
1148 qla82xx_wr_32(ha, QLA82XX_CRB_NIU + 0xb0000, 0x00);
Madhuranath Iyengarc9e8fd52010-12-21 16:00:19 -08001149
1150 /* halt sre */
1151 val = qla82xx_rd_32(ha, QLA82XX_CRB_SRE + 0x1000);
1152 qla82xx_wr_32(ha, QLA82XX_CRB_SRE + 0x1000, val & (~(0x1)));
1153
1154 /* halt epg */
1155 qla82xx_wr_32(ha, QLA82XX_CRB_EPG + 0x1300, 0x1);
1156
1157 /* halt timers */
1158 qla82xx_wr_32(ha, QLA82XX_CRB_TIMER + 0x0, 0x0);
1159 qla82xx_wr_32(ha, QLA82XX_CRB_TIMER + 0x8, 0x0);
1160 qla82xx_wr_32(ha, QLA82XX_CRB_TIMER + 0x10, 0x0);
1161 qla82xx_wr_32(ha, QLA82XX_CRB_TIMER + 0x18, 0x0);
1162 qla82xx_wr_32(ha, QLA82XX_CRB_TIMER + 0x100, 0x0);
Giridhar Malavali02be2212011-03-30 11:46:24 -07001163 qla82xx_wr_32(ha, QLA82XX_CRB_TIMER + 0x200, 0x0);
Madhuranath Iyengarc9e8fd52010-12-21 16:00:19 -08001164
1165 /* halt pegs */
1166 qla82xx_wr_32(ha, QLA82XX_CRB_PEG_NET_0 + 0x3c, 1);
1167 qla82xx_wr_32(ha, QLA82XX_CRB_PEG_NET_1 + 0x3c, 1);
1168 qla82xx_wr_32(ha, QLA82XX_CRB_PEG_NET_2 + 0x3c, 1);
1169 qla82xx_wr_32(ha, QLA82XX_CRB_PEG_NET_3 + 0x3c, 1);
1170 qla82xx_wr_32(ha, QLA82XX_CRB_PEG_NET_4 + 0x3c, 1);
Giridhar Malavali02be2212011-03-30 11:46:24 -07001171 msleep(20);
Madhuranath Iyengarc9e8fd52010-12-21 16:00:19 -08001172
1173 /* big hammer */
Giridhar Malavalia9083012010-04-12 17:59:55 -07001174 if (test_bit(ABORT_ISP_ACTIVE, &vha->dpc_flags))
1175 /* don't reset CAM block on reset */
1176 qla82xx_wr_32(ha, QLA82XX_ROMUSB_GLB_SW_RESET, 0xfeffffff);
1177 else
1178 qla82xx_wr_32(ha, QLA82XX_ROMUSB_GLB_SW_RESET, 0xffffffff);
Chad Dupuisd652e092011-05-10 11:30:10 -07001179 qla82xx_rom_unlock(ha);
Giridhar Malavalia9083012010-04-12 17:59:55 -07001180
1181 /* Read the signature value from the flash.
1182 * Offset 0: Contain signature (0xcafecafe)
1183 * Offset 4: Offset and number of addr/value pairs
1184 * that present in CRB initialize sequence
1185 */
1186 if (qla82xx_rom_fast_read(ha, 0, &n) != 0 || n != 0xcafecafeUL ||
1187 qla82xx_rom_fast_read(ha, 4, &n) != 0) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001188 ql_log(ql_log_fatal, vha, 0x006e,
1189 "Error Reading crb_init area: n: %08x.\n", n);
Giridhar Malavalia9083012010-04-12 17:59:55 -07001190 return -1;
1191 }
1192
1193 /* Offset in flash = lower 16 bits
Saurav Kashyap00adc9a2012-05-15 14:34:22 -04001194 * Number of entries = upper 16 bits
Giridhar Malavalia9083012010-04-12 17:59:55 -07001195 */
1196 offset = n & 0xffffU;
1197 n = (n >> 16) & 0xffffU;
1198
Saurav Kashyap00adc9a2012-05-15 14:34:22 -04001199 /* number of addr/value pair should not exceed 1024 entries */
Giridhar Malavalia9083012010-04-12 17:59:55 -07001200 if (n >= 1024) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001201 ql_log(ql_log_fatal, vha, 0x0071,
1202 "Card flash not initialized:n=0x%x.\n", n);
Giridhar Malavalia9083012010-04-12 17:59:55 -07001203 return -1;
1204 }
1205
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001206 ql_log(ql_log_info, vha, 0x0072,
1207 "%d CRB init values found in ROM.\n", n);
Giridhar Malavalia9083012010-04-12 17:59:55 -07001208
1209 buf = kmalloc(n * sizeof(struct crb_addr_pair), GFP_KERNEL);
1210 if (buf == NULL) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001211 ql_log(ql_log_fatal, vha, 0x010c,
1212 "Unable to allocate memory.\n");
Giridhar Malavalia9083012010-04-12 17:59:55 -07001213 return -1;
1214 }
1215
1216 for (i = 0; i < n; i++) {
1217 if (qla82xx_rom_fast_read(ha, 8*i + 4*offset, &val) != 0 ||
1218 qla82xx_rom_fast_read(ha, 8*i + 4*offset + 4, &addr) != 0) {
1219 kfree(buf);
1220 return -1;
1221 }
1222
1223 buf[i].addr = addr;
1224 buf[i].data = val;
1225 }
1226
1227 for (i = 0; i < n; i++) {
1228 /* Translate internal CRB initialization
1229 * address to PCI bus address
1230 */
1231 off = qla82xx_decode_crb_addr((unsigned long)buf[i].addr) +
1232 QLA82XX_PCI_CRBSPACE;
1233 /* Not all CRB addr/value pair to be written,
1234 * some of them are skipped
1235 */
1236
1237 /* skipping cold reboot MAGIC */
1238 if (off == QLA82XX_CAM_RAM(0x1fc))
1239 continue;
1240
1241 /* do not reset PCI */
1242 if (off == (ROMUSB_GLB + 0xbc))
1243 continue;
1244
1245 /* skip core clock, so that firmware can increase the clock */
1246 if (off == (ROMUSB_GLB + 0xc8))
1247 continue;
1248
1249 /* skip the function enable register */
1250 if (off == QLA82XX_PCIE_REG(PCIE_SETUP_FUNCTION))
1251 continue;
1252
1253 if (off == QLA82XX_PCIE_REG(PCIE_SETUP_FUNCTION2))
1254 continue;
1255
1256 if ((off & 0x0ff00000) == QLA82XX_CRB_SMB)
1257 continue;
1258
1259 if ((off & 0x0ff00000) == QLA82XX_CRB_DDR_NET)
1260 continue;
1261
1262 if (off == ADDR_ERROR) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001263 ql_log(ql_log_fatal, vha, 0x0116,
1264 "Unknow addr: 0x%08lx.\n", buf[i].addr);
Giridhar Malavalia9083012010-04-12 17:59:55 -07001265 continue;
1266 }
1267
Giridhar Malavalia9083012010-04-12 17:59:55 -07001268 qla82xx_wr_32(ha, off, buf[i].data);
1269
1270 /* ISP requires much bigger delay to settle down,
1271 * else crb_window returns 0xffffffff
1272 */
1273 if (off == QLA82XX_ROMUSB_GLB_SW_RESET)
1274 msleep(1000);
1275
1276 /* ISP requires millisec delay between
1277 * successive CRB register updation
1278 */
1279 msleep(1);
1280 }
1281
1282 kfree(buf);
1283
1284 /* Resetting the data and instruction cache */
1285 qla82xx_wr_32(ha, QLA82XX_CRB_PEG_NET_D+0xec, 0x1e);
1286 qla82xx_wr_32(ha, QLA82XX_CRB_PEG_NET_D+0x4c, 8);
1287 qla82xx_wr_32(ha, QLA82XX_CRB_PEG_NET_I+0x4c, 8);
1288
1289 /* Clear all protocol processing engines */
1290 qla82xx_wr_32(ha, QLA82XX_CRB_PEG_NET_0+0x8, 0);
1291 qla82xx_wr_32(ha, QLA82XX_CRB_PEG_NET_0+0xc, 0);
1292 qla82xx_wr_32(ha, QLA82XX_CRB_PEG_NET_1+0x8, 0);
1293 qla82xx_wr_32(ha, QLA82XX_CRB_PEG_NET_1+0xc, 0);
1294 qla82xx_wr_32(ha, QLA82XX_CRB_PEG_NET_2+0x8, 0);
1295 qla82xx_wr_32(ha, QLA82XX_CRB_PEG_NET_2+0xc, 0);
1296 qla82xx_wr_32(ha, QLA82XX_CRB_PEG_NET_3+0x8, 0);
1297 qla82xx_wr_32(ha, QLA82XX_CRB_PEG_NET_3+0xc, 0);
1298 return 0;
1299}
1300
Giridhar Malavali77e334d2010-09-03 15:20:52 -07001301static int
Giridhar Malavali77e334d2010-09-03 15:20:52 -07001302qla82xx_pci_mem_write_2M(struct qla_hw_data *ha,
1303 u64 off, void *data, int size)
1304{
1305 int i, j, ret = 0, loop, sz[2], off0;
1306 int scale, shift_amount, startword;
1307 uint32_t temp;
1308 uint64_t off8, mem_crb, tmpw, word[2] = {0, 0};
1309
1310 /*
1311 * If not MN, go check for MS or invalid.
1312 */
1313 if (off >= QLA82XX_ADDR_QDR_NET && off <= QLA82XX_P3_ADDR_QDR_NET_MAX)
1314 mem_crb = QLA82XX_CRB_QDR_NET;
1315 else {
1316 mem_crb = QLA82XX_CRB_DDR_NET;
1317 if (qla82xx_pci_mem_bound_check(ha, off, size) == 0)
1318 return qla82xx_pci_mem_write_direct(ha,
1319 off, data, size);
1320 }
1321
1322 off0 = off & 0x7;
1323 sz[0] = (size < (8 - off0)) ? size : (8 - off0);
1324 sz[1] = size - sz[0];
1325
1326 off8 = off & 0xfffffff0;
1327 loop = (((off & 0xf) + size - 1) >> 4) + 1;
1328 shift_amount = 4;
1329 scale = 2;
1330 startword = (off & 0xf)/8;
1331
1332 for (i = 0; i < loop; i++) {
1333 if (qla82xx_pci_mem_read_2M(ha, off8 +
1334 (i << shift_amount), &word[i * scale], 8))
1335 return -1;
1336 }
1337
1338 switch (size) {
1339 case 1:
1340 tmpw = *((uint8_t *)data);
1341 break;
1342 case 2:
1343 tmpw = *((uint16_t *)data);
1344 break;
1345 case 4:
1346 tmpw = *((uint32_t *)data);
1347 break;
1348 case 8:
1349 default:
1350 tmpw = *((uint64_t *)data);
1351 break;
1352 }
1353
1354 if (sz[0] == 8) {
1355 word[startword] = tmpw;
1356 } else {
1357 word[startword] &=
1358 ~((~(~0ULL << (sz[0] * 8))) << (off0 * 8));
1359 word[startword] |= tmpw << (off0 * 8);
1360 }
1361 if (sz[1] != 0) {
1362 word[startword+1] &= ~(~0ULL << (sz[1] * 8));
1363 word[startword+1] |= tmpw >> (sz[0] * 8);
1364 }
1365
Giridhar Malavali77e334d2010-09-03 15:20:52 -07001366 for (i = 0; i < loop; i++) {
1367 temp = off8 + (i << shift_amount);
1368 qla82xx_wr_32(ha, mem_crb+MIU_TEST_AGT_ADDR_LO, temp);
1369 temp = 0;
1370 qla82xx_wr_32(ha, mem_crb+MIU_TEST_AGT_ADDR_HI, temp);
1371 temp = word[i * scale] & 0xffffffff;
1372 qla82xx_wr_32(ha, mem_crb+MIU_TEST_AGT_WRDATA_LO, temp);
1373 temp = (word[i * scale] >> 32) & 0xffffffff;
1374 qla82xx_wr_32(ha, mem_crb+MIU_TEST_AGT_WRDATA_HI, temp);
1375 temp = word[i*scale + 1] & 0xffffffff;
1376 qla82xx_wr_32(ha, mem_crb +
1377 MIU_TEST_AGT_WRDATA_UPPER_LO, temp);
1378 temp = (word[i*scale + 1] >> 32) & 0xffffffff;
1379 qla82xx_wr_32(ha, mem_crb +
1380 MIU_TEST_AGT_WRDATA_UPPER_HI, temp);
1381
1382 temp = MIU_TA_CTL_ENABLE | MIU_TA_CTL_WRITE;
1383 qla82xx_wr_32(ha, mem_crb + MIU_TEST_AGT_CTRL, temp);
1384 temp = MIU_TA_CTL_START | MIU_TA_CTL_ENABLE | MIU_TA_CTL_WRITE;
1385 qla82xx_wr_32(ha, mem_crb + MIU_TEST_AGT_CTRL, temp);
1386
1387 for (j = 0; j < MAX_CTL_CHECK; j++) {
1388 temp = qla82xx_rd_32(ha, mem_crb + MIU_TEST_AGT_CTRL);
1389 if ((temp & MIU_TA_CTL_BUSY) == 0)
1390 break;
1391 }
1392
1393 if (j >= MAX_CTL_CHECK) {
1394 if (printk_ratelimit())
1395 dev_err(&ha->pdev->dev,
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001396 "failed to write through agent.\n");
Giridhar Malavali77e334d2010-09-03 15:20:52 -07001397 ret = -1;
1398 break;
1399 }
1400 }
1401
1402 return ret;
1403}
1404
1405static int
Giridhar Malavalia9083012010-04-12 17:59:55 -07001406qla82xx_fw_load_from_flash(struct qla_hw_data *ha)
1407{
1408 int i;
1409 long size = 0;
Harish Zunjarrao9c2b2972010-05-28 15:08:23 -07001410 long flashaddr = ha->flt_region_bootload << 2;
1411 long memaddr = BOOTLD_START;
Giridhar Malavalia9083012010-04-12 17:59:55 -07001412 u64 data;
1413 u32 high, low;
1414 size = (IMAGE_START - BOOTLD_START) / 8;
1415
1416 for (i = 0; i < size; i++) {
1417 if ((qla82xx_rom_fast_read(ha, flashaddr, (int *)&low)) ||
1418 (qla82xx_rom_fast_read(ha, flashaddr + 4, (int *)&high))) {
1419 return -1;
1420 }
1421 data = ((u64)high << 32) | low ;
1422 qla82xx_pci_mem_write_2M(ha, memaddr, &data, 8);
1423 flashaddr += 8;
1424 memaddr += 8;
1425
1426 if (i % 0x1000 == 0)
1427 msleep(1);
1428 }
1429 udelay(100);
1430 read_lock(&ha->hw_lock);
Giridhar Malavali37113332010-07-23 15:28:34 +05001431 qla82xx_wr_32(ha, QLA82XX_CRB_PEG_NET_0 + 0x18, 0x1020);
1432 qla82xx_wr_32(ha, QLA82XX_ROMUSB_GLB_SW_RESET, 0x80001e);
Giridhar Malavalia9083012010-04-12 17:59:55 -07001433 read_unlock(&ha->hw_lock);
1434 return 0;
1435}
1436
1437int
1438qla82xx_pci_mem_read_2M(struct qla_hw_data *ha,
1439 u64 off, void *data, int size)
1440{
1441 int i, j = 0, k, start, end, loop, sz[2], off0[2];
1442 int shift_amount;
1443 uint32_t temp;
1444 uint64_t off8, val, mem_crb, word[2] = {0, 0};
1445
1446 /*
1447 * If not MN, go check for MS or invalid.
1448 */
1449
1450 if (off >= QLA82XX_ADDR_QDR_NET && off <= QLA82XX_P3_ADDR_QDR_NET_MAX)
1451 mem_crb = QLA82XX_CRB_QDR_NET;
1452 else {
1453 mem_crb = QLA82XX_CRB_DDR_NET;
1454 if (qla82xx_pci_mem_bound_check(ha, off, size) == 0)
1455 return qla82xx_pci_mem_read_direct(ha,
1456 off, data, size);
1457 }
1458
Giridhar Malavali37113332010-07-23 15:28:34 +05001459 off8 = off & 0xfffffff0;
1460 off0[0] = off & 0xf;
1461 sz[0] = (size < (16 - off0[0])) ? size : (16 - off0[0]);
1462 shift_amount = 4;
Giridhar Malavalia9083012010-04-12 17:59:55 -07001463 loop = ((off0[0] + size - 1) >> shift_amount) + 1;
1464 off0[1] = 0;
1465 sz[1] = size - sz[0];
1466
Giridhar Malavalia9083012010-04-12 17:59:55 -07001467 for (i = 0; i < loop; i++) {
1468 temp = off8 + (i << shift_amount);
1469 qla82xx_wr_32(ha, mem_crb + MIU_TEST_AGT_ADDR_LO, temp);
1470 temp = 0;
1471 qla82xx_wr_32(ha, mem_crb + MIU_TEST_AGT_ADDR_HI, temp);
1472 temp = MIU_TA_CTL_ENABLE;
1473 qla82xx_wr_32(ha, mem_crb + MIU_TEST_AGT_CTRL, temp);
1474 temp = MIU_TA_CTL_START | MIU_TA_CTL_ENABLE;
1475 qla82xx_wr_32(ha, mem_crb + MIU_TEST_AGT_CTRL, temp);
1476
1477 for (j = 0; j < MAX_CTL_CHECK; j++) {
1478 temp = qla82xx_rd_32(ha, mem_crb + MIU_TEST_AGT_CTRL);
1479 if ((temp & MIU_TA_CTL_BUSY) == 0)
1480 break;
1481 }
1482
1483 if (j >= MAX_CTL_CHECK) {
1484 if (printk_ratelimit())
1485 dev_err(&ha->pdev->dev,
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001486 "failed to read through agent.\n");
Giridhar Malavalia9083012010-04-12 17:59:55 -07001487 break;
1488 }
1489
1490 start = off0[i] >> 2;
1491 end = (off0[i] + sz[i] - 1) >> 2;
1492 for (k = start; k <= end; k++) {
1493 temp = qla82xx_rd_32(ha,
1494 mem_crb + MIU_TEST_AGT_RDDATA(k));
1495 word[i] |= ((uint64_t)temp << (32 * (k & 1)));
1496 }
1497 }
1498
Giridhar Malavalia9083012010-04-12 17:59:55 -07001499 if (j >= MAX_CTL_CHECK)
1500 return -1;
1501
1502 if ((off0[0] & 7) == 0) {
1503 val = word[0];
1504 } else {
1505 val = ((word[0] >> (off0[0] * 8)) & (~(~0ULL << (sz[0] * 8)))) |
1506 ((word[1] & (~(~0ULL << (sz[1] * 8)))) << (sz[0] * 8));
1507 }
1508
1509 switch (size) {
1510 case 1:
1511 *(uint8_t *)data = val;
1512 break;
1513 case 2:
1514 *(uint16_t *)data = val;
1515 break;
1516 case 4:
1517 *(uint32_t *)data = val;
1518 break;
1519 case 8:
1520 *(uint64_t *)data = val;
1521 break;
1522 }
1523 return 0;
1524}
1525
Giridhar Malavalia9083012010-04-12 17:59:55 -07001526
Harish Zunjarrao9c2b2972010-05-28 15:08:23 -07001527static struct qla82xx_uri_table_desc *
1528qla82xx_get_table_desc(const u8 *unirom, int section)
1529{
1530 uint32_t i;
1531 struct qla82xx_uri_table_desc *directory =
1532 (struct qla82xx_uri_table_desc *)&unirom[0];
1533 __le32 offset;
1534 __le32 tab_type;
1535 __le32 entries = cpu_to_le32(directory->num_entries);
1536
1537 for (i = 0; i < entries; i++) {
1538 offset = cpu_to_le32(directory->findex) +
1539 (i * cpu_to_le32(directory->entry_size));
1540 tab_type = cpu_to_le32(*((u32 *)&unirom[offset] + 8));
1541
1542 if (tab_type == section)
1543 return (struct qla82xx_uri_table_desc *)&unirom[offset];
1544 }
1545
1546 return NULL;
1547}
1548
1549static struct qla82xx_uri_data_desc *
1550qla82xx_get_data_desc(struct qla_hw_data *ha,
1551 u32 section, u32 idx_offset)
1552{
1553 const u8 *unirom = ha->hablob->fw->data;
1554 int idx = cpu_to_le32(*((int *)&unirom[ha->file_prd_off] + idx_offset));
1555 struct qla82xx_uri_table_desc *tab_desc = NULL;
1556 __le32 offset;
1557
1558 tab_desc = qla82xx_get_table_desc(unirom, section);
1559 if (!tab_desc)
1560 return NULL;
1561
1562 offset = cpu_to_le32(tab_desc->findex) +
1563 (cpu_to_le32(tab_desc->entry_size) * idx);
1564
1565 return (struct qla82xx_uri_data_desc *)&unirom[offset];
1566}
1567
1568static u8 *
1569qla82xx_get_bootld_offset(struct qla_hw_data *ha)
1570{
1571 u32 offset = BOOTLD_START;
1572 struct qla82xx_uri_data_desc *uri_desc = NULL;
1573
1574 if (ha->fw_type == QLA82XX_UNIFIED_ROMIMAGE) {
1575 uri_desc = qla82xx_get_data_desc(ha,
1576 QLA82XX_URI_DIR_SECT_BOOTLD, QLA82XX_URI_BOOTLD_IDX_OFF);
1577 if (uri_desc)
1578 offset = cpu_to_le32(uri_desc->findex);
1579 }
1580
1581 return (u8 *)&ha->hablob->fw->data[offset];
1582}
1583
1584static __le32
1585qla82xx_get_fw_size(struct qla_hw_data *ha)
1586{
1587 struct qla82xx_uri_data_desc *uri_desc = NULL;
1588
1589 if (ha->fw_type == QLA82XX_UNIFIED_ROMIMAGE) {
1590 uri_desc = qla82xx_get_data_desc(ha, QLA82XX_URI_DIR_SECT_FW,
1591 QLA82XX_URI_FIRMWARE_IDX_OFF);
1592 if (uri_desc)
1593 return cpu_to_le32(uri_desc->size);
1594 }
1595
1596 return cpu_to_le32(*(u32 *)&ha->hablob->fw->data[FW_SIZE_OFFSET]);
1597}
1598
1599static u8 *
1600qla82xx_get_fw_offs(struct qla_hw_data *ha)
1601{
1602 u32 offset = IMAGE_START;
1603 struct qla82xx_uri_data_desc *uri_desc = NULL;
1604
1605 if (ha->fw_type == QLA82XX_UNIFIED_ROMIMAGE) {
1606 uri_desc = qla82xx_get_data_desc(ha, QLA82XX_URI_DIR_SECT_FW,
1607 QLA82XX_URI_FIRMWARE_IDX_OFF);
1608 if (uri_desc)
1609 offset = cpu_to_le32(uri_desc->findex);
1610 }
1611
1612 return (u8 *)&ha->hablob->fw->data[offset];
1613}
1614
Giridhar Malavalia9083012010-04-12 17:59:55 -07001615/* PCI related functions */
Giridhar Malavalia9083012010-04-12 17:59:55 -07001616int qla82xx_pci_region_offset(struct pci_dev *pdev, int region)
1617{
1618 unsigned long val = 0;
1619 u32 control;
1620
1621 switch (region) {
1622 case 0:
1623 val = 0;
1624 break;
1625 case 1:
1626 pci_read_config_dword(pdev, QLA82XX_PCI_REG_MSIX_TBL, &control);
1627 val = control + QLA82XX_MSIX_TBL_SPACE;
1628 break;
1629 }
1630 return val;
1631}
1632
Giridhar Malavalia9083012010-04-12 17:59:55 -07001633
1634int
1635qla82xx_iospace_config(struct qla_hw_data *ha)
1636{
1637 uint32_t len = 0;
1638
1639 if (pci_request_regions(ha->pdev, QLA2XXX_DRIVER_NAME)) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001640 ql_log_pci(ql_log_fatal, ha->pdev, 0x000c,
1641 "Failed to reserver selected regions.\n");
Giridhar Malavalia9083012010-04-12 17:59:55 -07001642 goto iospace_error_exit;
1643 }
1644
1645 /* Use MMIO operations for all accesses. */
1646 if (!(pci_resource_flags(ha->pdev, 0) & IORESOURCE_MEM)) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001647 ql_log_pci(ql_log_fatal, ha->pdev, 0x000d,
1648 "Region #0 not an MMIO resource, aborting.\n");
Giridhar Malavalia9083012010-04-12 17:59:55 -07001649 goto iospace_error_exit;
1650 }
1651
1652 len = pci_resource_len(ha->pdev, 0);
1653 ha->nx_pcibase =
1654 (unsigned long)ioremap(pci_resource_start(ha->pdev, 0), len);
1655 if (!ha->nx_pcibase) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001656 ql_log_pci(ql_log_fatal, ha->pdev, 0x000e,
1657 "Cannot remap pcibase MMIO, aborting.\n");
Giridhar Malavalia9083012010-04-12 17:59:55 -07001658 pci_release_regions(ha->pdev);
1659 goto iospace_error_exit;
1660 }
1661
1662 /* Mapping of IO base pointer */
1663 ha->iobase = (device_reg_t __iomem *)((uint8_t *)ha->nx_pcibase +
1664 0xbc000 + (ha->pdev->devfn << 11));
1665
1666 if (!ql2xdbwr) {
1667 ha->nxdb_wr_ptr =
1668 (unsigned long)ioremap((pci_resource_start(ha->pdev, 4) +
1669 (ha->pdev->devfn << 12)), 4);
1670 if (!ha->nxdb_wr_ptr) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001671 ql_log_pci(ql_log_fatal, ha->pdev, 0x000f,
1672 "Cannot remap MMIO, aborting.\n");
Giridhar Malavalia9083012010-04-12 17:59:55 -07001673 pci_release_regions(ha->pdev);
1674 goto iospace_error_exit;
1675 }
1676
1677 /* Mapping of IO base pointer,
1678 * door bell read and write pointer
1679 */
1680 ha->nxdb_rd_ptr = (uint8_t *) ha->nx_pcibase + (512 * 1024) +
1681 (ha->pdev->devfn * 8);
1682 } else {
1683 ha->nxdb_wr_ptr = (ha->pdev->devfn == 6 ?
1684 QLA82XX_CAMRAM_DB1 :
1685 QLA82XX_CAMRAM_DB2);
1686 }
1687
1688 ha->max_req_queues = ha->max_rsp_queues = 1;
1689 ha->msix_count = ha->max_rsp_queues + 1;
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001690 ql_dbg_pci(ql_dbg_multiq, ha->pdev, 0xc006,
1691 "nx_pci_base=%p iobase=%p "
1692 "max_req_queues=%d msix_count=%d.\n",
Joe Perchesd8424f62011-11-18 09:03:06 -08001693 (void *)ha->nx_pcibase, ha->iobase,
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001694 ha->max_req_queues, ha->msix_count);
1695 ql_dbg_pci(ql_dbg_init, ha->pdev, 0x0010,
1696 "nx_pci_base=%p iobase=%p "
1697 "max_req_queues=%d msix_count=%d.\n",
Joe Perchesd8424f62011-11-18 09:03:06 -08001698 (void *)ha->nx_pcibase, ha->iobase,
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001699 ha->max_req_queues, ha->msix_count);
Giridhar Malavalia9083012010-04-12 17:59:55 -07001700 return 0;
1701
1702iospace_error_exit:
1703 return -ENOMEM;
1704}
1705
1706/* GS related functions */
1707
1708/* Initialization related functions */
1709
1710/**
1711 * qla82xx_pci_config() - Setup ISP82xx PCI configuration registers.
1712 * @ha: HA context
1713 *
1714 * Returns 0 on success.
1715*/
1716int
1717qla82xx_pci_config(scsi_qla_host_t *vha)
1718{
1719 struct qla_hw_data *ha = vha->hw;
1720 int ret;
1721
1722 pci_set_master(ha->pdev);
1723 ret = pci_set_mwi(ha->pdev);
1724 ha->chip_revision = ha->pdev->revision;
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001725 ql_dbg(ql_dbg_init, vha, 0x0043,
Joe Perchesd8424f62011-11-18 09:03:06 -08001726 "Chip revision:%d.\n",
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001727 ha->chip_revision);
Giridhar Malavalia9083012010-04-12 17:59:55 -07001728 return 0;
1729}
1730
1731/**
1732 * qla82xx_reset_chip() - Setup ISP82xx PCI configuration registers.
1733 * @ha: HA context
1734 *
1735 * Returns 0 on success.
1736 */
1737void
1738qla82xx_reset_chip(scsi_qla_host_t *vha)
1739{
1740 struct qla_hw_data *ha = vha->hw;
1741 ha->isp_ops->disable_intrs(ha);
1742}
1743
1744void qla82xx_config_rings(struct scsi_qla_host *vha)
1745{
1746 struct qla_hw_data *ha = vha->hw;
1747 struct device_reg_82xx __iomem *reg = &ha->iobase->isp82;
1748 struct init_cb_81xx *icb;
1749 struct req_que *req = ha->req_q_map[0];
1750 struct rsp_que *rsp = ha->rsp_q_map[0];
1751
1752 /* Setup ring parameters in initialization control block. */
1753 icb = (struct init_cb_81xx *)ha->init_cb;
1754 icb->request_q_outpointer = __constant_cpu_to_le16(0);
1755 icb->response_q_inpointer = __constant_cpu_to_le16(0);
1756 icb->request_q_length = cpu_to_le16(req->length);
1757 icb->response_q_length = cpu_to_le16(rsp->length);
1758 icb->request_q_address[0] = cpu_to_le32(LSD(req->dma));
1759 icb->request_q_address[1] = cpu_to_le32(MSD(req->dma));
1760 icb->response_q_address[0] = cpu_to_le32(LSD(rsp->dma));
1761 icb->response_q_address[1] = cpu_to_le32(MSD(rsp->dma));
1762
Giridhar Malavalia9083012010-04-12 17:59:55 -07001763 WRT_REG_DWORD((unsigned long __iomem *)&reg->req_q_out[0], 0);
1764 WRT_REG_DWORD((unsigned long __iomem *)&reg->rsp_q_in[0], 0);
1765 WRT_REG_DWORD((unsigned long __iomem *)&reg->rsp_q_out[0], 0);
1766}
1767
Giridhar Malavali77e334d2010-09-03 15:20:52 -07001768static int
1769qla82xx_fw_load_from_blob(struct qla_hw_data *ha)
Giridhar Malavalia9083012010-04-12 17:59:55 -07001770{
1771 u64 *ptr64;
1772 u32 i, flashaddr, size;
1773 __le64 data;
1774
1775 size = (IMAGE_START - BOOTLD_START) / 8;
1776
Harish Zunjarrao9c2b2972010-05-28 15:08:23 -07001777 ptr64 = (u64 *)qla82xx_get_bootld_offset(ha);
Giridhar Malavalia9083012010-04-12 17:59:55 -07001778 flashaddr = BOOTLD_START;
1779
1780 for (i = 0; i < size; i++) {
1781 data = cpu_to_le64(ptr64[i]);
Harish Zunjarrao9c2b2972010-05-28 15:08:23 -07001782 if (qla82xx_pci_mem_write_2M(ha, flashaddr, &data, 8))
1783 return -EIO;
Giridhar Malavalia9083012010-04-12 17:59:55 -07001784 flashaddr += 8;
1785 }
1786
Giridhar Malavalia9083012010-04-12 17:59:55 -07001787 flashaddr = FLASH_ADDR_START;
Harish Zunjarrao9c2b2972010-05-28 15:08:23 -07001788 size = (__force u32)qla82xx_get_fw_size(ha) / 8;
1789 ptr64 = (u64 *)qla82xx_get_fw_offs(ha);
Giridhar Malavalia9083012010-04-12 17:59:55 -07001790
1791 for (i = 0; i < size; i++) {
1792 data = cpu_to_le64(ptr64[i]);
1793
1794 if (qla82xx_pci_mem_write_2M(ha, flashaddr, &data, 8))
1795 return -EIO;
1796 flashaddr += 8;
1797 }
Harish Zunjarrao9c2b2972010-05-28 15:08:23 -07001798 udelay(100);
Giridhar Malavalia9083012010-04-12 17:59:55 -07001799
1800 /* Write a magic value to CAMRAM register
1801 * at a specified offset to indicate
1802 * that all data is written and
1803 * ready for firmware to initialize.
1804 */
Harish Zunjarrao9c2b2972010-05-28 15:08:23 -07001805 qla82xx_wr_32(ha, QLA82XX_CAM_RAM(0x1fc), QLA82XX_BDINFO_MAGIC);
Giridhar Malavalia9083012010-04-12 17:59:55 -07001806
Harish Zunjarrao9c2b2972010-05-28 15:08:23 -07001807 read_lock(&ha->hw_lock);
Giridhar Malavali37113332010-07-23 15:28:34 +05001808 qla82xx_wr_32(ha, QLA82XX_CRB_PEG_NET_0 + 0x18, 0x1020);
1809 qla82xx_wr_32(ha, QLA82XX_ROMUSB_GLB_SW_RESET, 0x80001e);
Harish Zunjarrao9c2b2972010-05-28 15:08:23 -07001810 read_unlock(&ha->hw_lock);
1811 return 0;
1812}
1813
1814static int
1815qla82xx_set_product_offset(struct qla_hw_data *ha)
1816{
1817 struct qla82xx_uri_table_desc *ptab_desc = NULL;
1818 const uint8_t *unirom = ha->hablob->fw->data;
1819 uint32_t i;
1820 __le32 entries;
1821 __le32 flags, file_chiprev, offset;
1822 uint8_t chiprev = ha->chip_revision;
1823 /* Hardcoding mn_present flag for P3P */
1824 int mn_present = 0;
1825 uint32_t flagbit;
1826
1827 ptab_desc = qla82xx_get_table_desc(unirom,
1828 QLA82XX_URI_DIR_SECT_PRODUCT_TBL);
1829 if (!ptab_desc)
1830 return -1;
1831
1832 entries = cpu_to_le32(ptab_desc->num_entries);
1833
1834 for (i = 0; i < entries; i++) {
1835 offset = cpu_to_le32(ptab_desc->findex) +
1836 (i * cpu_to_le32(ptab_desc->entry_size));
1837 flags = cpu_to_le32(*((int *)&unirom[offset] +
1838 QLA82XX_URI_FLAGS_OFF));
1839 file_chiprev = cpu_to_le32(*((int *)&unirom[offset] +
1840 QLA82XX_URI_CHIP_REV_OFF));
1841
1842 flagbit = mn_present ? 1 : 2;
1843
1844 if ((chiprev == file_chiprev) && ((1ULL << flagbit) & flags)) {
1845 ha->file_prd_off = offset;
1846 return 0;
1847 }
1848 }
1849 return -1;
1850}
1851
Saurav Kashyapfa492632012-11-21 02:40:29 -05001852static int
Harish Zunjarrao9c2b2972010-05-28 15:08:23 -07001853qla82xx_validate_firmware_blob(scsi_qla_host_t *vha, uint8_t fw_type)
1854{
1855 __le32 val;
1856 uint32_t min_size;
1857 struct qla_hw_data *ha = vha->hw;
1858 const struct firmware *fw = ha->hablob->fw;
1859
1860 ha->fw_type = fw_type;
1861
1862 if (fw_type == QLA82XX_UNIFIED_ROMIMAGE) {
1863 if (qla82xx_set_product_offset(ha))
1864 return -EINVAL;
1865
1866 min_size = QLA82XX_URI_FW_MIN_SIZE;
1867 } else {
1868 val = cpu_to_le32(*(u32 *)&fw->data[QLA82XX_FW_MAGIC_OFFSET]);
1869 if ((__force u32)val != QLA82XX_BDINFO_MAGIC)
1870 return -EINVAL;
1871
1872 min_size = QLA82XX_FW_MIN_SIZE;
1873 }
1874
1875 if (fw->size < min_size)
1876 return -EINVAL;
Giridhar Malavalia9083012010-04-12 17:59:55 -07001877 return 0;
1878}
1879
Giridhar Malavali77e334d2010-09-03 15:20:52 -07001880static int
1881qla82xx_check_cmdpeg_state(struct qla_hw_data *ha)
Giridhar Malavalia9083012010-04-12 17:59:55 -07001882{
1883 u32 val = 0;
1884 int retries = 60;
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001885 scsi_qla_host_t *vha = pci_get_drvdata(ha->pdev);
Giridhar Malavalia9083012010-04-12 17:59:55 -07001886
1887 do {
1888 read_lock(&ha->hw_lock);
1889 val = qla82xx_rd_32(ha, CRB_CMDPEG_STATE);
1890 read_unlock(&ha->hw_lock);
1891
1892 switch (val) {
1893 case PHAN_INITIALIZE_COMPLETE:
1894 case PHAN_INITIALIZE_ACK:
1895 return QLA_SUCCESS;
1896 case PHAN_INITIALIZE_FAILED:
1897 break;
1898 default:
1899 break;
1900 }
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001901 ql_log(ql_log_info, vha, 0x00a8,
1902 "CRB_CMDPEG_STATE: 0x%x and retries:0x%x.\n",
1903 val, retries);
Giridhar Malavalia9083012010-04-12 17:59:55 -07001904
1905 msleep(500);
1906
1907 } while (--retries);
1908
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001909 ql_log(ql_log_fatal, vha, 0x00a9,
Giridhar Malavalia9083012010-04-12 17:59:55 -07001910 "Cmd Peg initialization failed: 0x%x.\n", val);
1911
Giridhar Malavalia9083012010-04-12 17:59:55 -07001912 val = qla82xx_rd_32(ha, QLA82XX_ROMUSB_GLB_PEGTUNE_DONE);
1913 read_lock(&ha->hw_lock);
1914 qla82xx_wr_32(ha, CRB_CMDPEG_STATE, PHAN_INITIALIZE_FAILED);
1915 read_unlock(&ha->hw_lock);
1916 return QLA_FUNCTION_FAILED;
1917}
1918
Giridhar Malavali77e334d2010-09-03 15:20:52 -07001919static int
1920qla82xx_check_rcvpeg_state(struct qla_hw_data *ha)
Giridhar Malavalia9083012010-04-12 17:59:55 -07001921{
1922 u32 val = 0;
1923 int retries = 60;
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001924 scsi_qla_host_t *vha = pci_get_drvdata(ha->pdev);
Giridhar Malavalia9083012010-04-12 17:59:55 -07001925
1926 do {
1927 read_lock(&ha->hw_lock);
1928 val = qla82xx_rd_32(ha, CRB_RCVPEG_STATE);
1929 read_unlock(&ha->hw_lock);
1930
1931 switch (val) {
1932 case PHAN_INITIALIZE_COMPLETE:
1933 case PHAN_INITIALIZE_ACK:
1934 return QLA_SUCCESS;
1935 case PHAN_INITIALIZE_FAILED:
1936 break;
1937 default:
1938 break;
1939 }
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001940 ql_log(ql_log_info, vha, 0x00ab,
1941 "CRB_RCVPEG_STATE: 0x%x and retries: 0x%x.\n",
1942 val, retries);
Giridhar Malavalia9083012010-04-12 17:59:55 -07001943
1944 msleep(500);
1945
1946 } while (--retries);
1947
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001948 ql_log(ql_log_fatal, vha, 0x00ac,
1949 "Rcv Peg initializatin failed: 0x%x.\n", val);
Giridhar Malavalia9083012010-04-12 17:59:55 -07001950 read_lock(&ha->hw_lock);
1951 qla82xx_wr_32(ha, CRB_RCVPEG_STATE, PHAN_INITIALIZE_FAILED);
1952 read_unlock(&ha->hw_lock);
1953 return QLA_FUNCTION_FAILED;
1954}
1955
1956/* ISR related functions */
Giridhar Malavalia9083012010-04-12 17:59:55 -07001957static struct qla82xx_legacy_intr_set legacy_intr[] = \
1958 QLA82XX_LEGACY_INTR_CONFIG;
1959
1960/*
1961 * qla82xx_mbx_completion() - Process mailbox command completions.
1962 * @ha: SCSI driver HA context
1963 * @mb0: Mailbox0 register
1964 */
Giridhar Malavali77e334d2010-09-03 15:20:52 -07001965static void
Giridhar Malavalia9083012010-04-12 17:59:55 -07001966qla82xx_mbx_completion(scsi_qla_host_t *vha, uint16_t mb0)
1967{
1968 uint16_t cnt;
1969 uint16_t __iomem *wptr;
1970 struct qla_hw_data *ha = vha->hw;
1971 struct device_reg_82xx __iomem *reg = &ha->iobase->isp82;
1972 wptr = (uint16_t __iomem *)&reg->mailbox_out[1];
1973
1974 /* Load return mailbox registers. */
1975 ha->flags.mbox_int = 1;
1976 ha->mailbox_out[0] = mb0;
1977
1978 for (cnt = 1; cnt < ha->mbx_count; cnt++) {
1979 ha->mailbox_out[cnt] = RD_REG_WORD(wptr);
1980 wptr++;
1981 }
1982
Chad Dupuiscfb09192011-11-18 09:03:07 -08001983 if (!ha->mcp)
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001984 ql_dbg(ql_dbg_async, vha, 0x5053,
1985 "MBX pointer ERROR.\n");
Giridhar Malavalia9083012010-04-12 17:59:55 -07001986}
1987
1988/*
1989 * qla82xx_intr_handler() - Process interrupts for the ISP23xx and ISP63xx.
1990 * @irq:
1991 * @dev_id: SCSI driver HA context
1992 * @regs:
1993 *
1994 * Called by system whenever the host adapter generates an interrupt.
1995 *
1996 * Returns handled flag.
1997 */
1998irqreturn_t
1999qla82xx_intr_handler(int irq, void *dev_id)
2000{
2001 scsi_qla_host_t *vha;
2002 struct qla_hw_data *ha;
2003 struct rsp_que *rsp;
2004 struct device_reg_82xx __iomem *reg;
2005 int status = 0, status1 = 0;
2006 unsigned long flags;
2007 unsigned long iter;
Saurav Kashyap7c3df132011-07-14 12:00:13 -07002008 uint32_t stat = 0;
Giridhar Malavalia9083012010-04-12 17:59:55 -07002009 uint16_t mb[4];
2010
2011 rsp = (struct rsp_que *) dev_id;
2012 if (!rsp) {
Giridhar Malavalib6d0d9d2012-05-15 14:34:25 -04002013 ql_log(ql_log_info, NULL, 0xb053,
Chad Dupuis3256b432012-02-09 11:15:47 -08002014 "%s: NULL response queue pointer.\n", __func__);
Giridhar Malavalia9083012010-04-12 17:59:55 -07002015 return IRQ_NONE;
2016 }
2017 ha = rsp->hw;
2018
2019 if (!ha->flags.msi_enabled) {
2020 status = qla82xx_rd_32(ha, ISR_INT_VECTOR);
2021 if (!(status & ha->nx_legacy_intr.int_vec_bit))
2022 return IRQ_NONE;
2023
2024 status1 = qla82xx_rd_32(ha, ISR_INT_STATE_REG);
2025 if (!ISR_IS_LEGACY_INTR_TRIGGERED(status1))
2026 return IRQ_NONE;
2027 }
2028
2029 /* clear the interrupt */
2030 qla82xx_wr_32(ha, ha->nx_legacy_intr.tgt_status_reg, 0xffffffff);
2031
2032 /* read twice to ensure write is flushed */
2033 qla82xx_rd_32(ha, ISR_INT_VECTOR);
2034 qla82xx_rd_32(ha, ISR_INT_VECTOR);
2035
2036 reg = &ha->iobase->isp82;
2037
2038 spin_lock_irqsave(&ha->hardware_lock, flags);
2039 vha = pci_get_drvdata(ha->pdev);
2040 for (iter = 1; iter--; ) {
2041
2042 if (RD_REG_DWORD(&reg->host_int)) {
2043 stat = RD_REG_DWORD(&reg->host_status);
Giridhar Malavalia9083012010-04-12 17:59:55 -07002044
2045 switch (stat & 0xff) {
2046 case 0x1:
2047 case 0x2:
2048 case 0x10:
2049 case 0x11:
2050 qla82xx_mbx_completion(vha, MSW(stat));
2051 status |= MBX_INTERRUPT;
2052 break;
2053 case 0x12:
2054 mb[0] = MSW(stat);
2055 mb[1] = RD_REG_WORD(&reg->mailbox_out[1]);
2056 mb[2] = RD_REG_WORD(&reg->mailbox_out[2]);
2057 mb[3] = RD_REG_WORD(&reg->mailbox_out[3]);
2058 qla2x00_async_event(vha, rsp, mb);
2059 break;
2060 case 0x13:
2061 qla24xx_process_response_queue(vha, rsp);
2062 break;
2063 default:
Saurav Kashyap7c3df132011-07-14 12:00:13 -07002064 ql_dbg(ql_dbg_async, vha, 0x5054,
2065 "Unrecognized interrupt type (%d).\n",
2066 stat & 0xff);
Giridhar Malavalia9083012010-04-12 17:59:55 -07002067 break;
2068 }
2069 }
2070 WRT_REG_DWORD(&reg->host_int, 0);
2071 }
2072 spin_unlock_irqrestore(&ha->hardware_lock, flags);
2073 if (!ha->flags.msi_enabled)
2074 qla82xx_wr_32(ha, ha->nx_legacy_intr.tgt_mask_reg, 0xfbff);
2075
2076#ifdef QL_DEBUG_LEVEL_17
2077 if (!irq && ha->flags.eeh_busy)
Saurav Kashyap7c3df132011-07-14 12:00:13 -07002078 ql_log(ql_log_warn, vha, 0x503d,
2079 "isr:status %x, cmd_flags %lx, mbox_int %x, stat %x.\n",
Giridhar Malavalia9083012010-04-12 17:59:55 -07002080 status, ha->mbx_cmd_flags, ha->flags.mbox_int, stat);
2081#endif
2082
2083 if (test_bit(MBX_INTR_WAIT, &ha->mbx_cmd_flags) &&
2084 (status & MBX_INTERRUPT) && ha->flags.mbox_int) {
2085 set_bit(MBX_INTERRUPT, &ha->mbx_cmd_flags);
2086 complete(&ha->mbx_intr_comp);
2087 }
2088 return IRQ_HANDLED;
2089}
2090
2091irqreturn_t
2092qla82xx_msix_default(int irq, void *dev_id)
2093{
2094 scsi_qla_host_t *vha;
2095 struct qla_hw_data *ha;
2096 struct rsp_que *rsp;
2097 struct device_reg_82xx __iomem *reg;
2098 int status = 0;
2099 unsigned long flags;
Saurav Kashyap7c3df132011-07-14 12:00:13 -07002100 uint32_t stat = 0;
Giridhar Malavalia9083012010-04-12 17:59:55 -07002101 uint16_t mb[4];
2102
2103 rsp = (struct rsp_que *) dev_id;
2104 if (!rsp) {
2105 printk(KERN_INFO
Saurav Kashyap7c3df132011-07-14 12:00:13 -07002106 "%s(): NULL response queue pointer.\n", __func__);
Giridhar Malavalia9083012010-04-12 17:59:55 -07002107 return IRQ_NONE;
2108 }
2109 ha = rsp->hw;
2110
2111 reg = &ha->iobase->isp82;
2112
2113 spin_lock_irqsave(&ha->hardware_lock, flags);
2114 vha = pci_get_drvdata(ha->pdev);
2115 do {
2116 if (RD_REG_DWORD(&reg->host_int)) {
2117 stat = RD_REG_DWORD(&reg->host_status);
Giridhar Malavalia9083012010-04-12 17:59:55 -07002118
2119 switch (stat & 0xff) {
2120 case 0x1:
2121 case 0x2:
2122 case 0x10:
2123 case 0x11:
2124 qla82xx_mbx_completion(vha, MSW(stat));
2125 status |= MBX_INTERRUPT;
2126 break;
2127 case 0x12:
2128 mb[0] = MSW(stat);
2129 mb[1] = RD_REG_WORD(&reg->mailbox_out[1]);
2130 mb[2] = RD_REG_WORD(&reg->mailbox_out[2]);
2131 mb[3] = RD_REG_WORD(&reg->mailbox_out[3]);
2132 qla2x00_async_event(vha, rsp, mb);
2133 break;
2134 case 0x13:
2135 qla24xx_process_response_queue(vha, rsp);
2136 break;
2137 default:
Saurav Kashyap7c3df132011-07-14 12:00:13 -07002138 ql_dbg(ql_dbg_async, vha, 0x5041,
2139 "Unrecognized interrupt type (%d).\n",
2140 stat & 0xff);
Giridhar Malavalia9083012010-04-12 17:59:55 -07002141 break;
2142 }
2143 }
2144 WRT_REG_DWORD(&reg->host_int, 0);
2145 } while (0);
2146
2147 spin_unlock_irqrestore(&ha->hardware_lock, flags);
2148
2149#ifdef QL_DEBUG_LEVEL_17
2150 if (!irq && ha->flags.eeh_busy)
Saurav Kashyap7c3df132011-07-14 12:00:13 -07002151 ql_log(ql_log_warn, vha, 0x5044,
2152 "isr:status %x, cmd_flags %lx, mbox_int %x, stat %x.\n",
2153 status, ha->mbx_cmd_flags, ha->flags.mbox_int, stat);
Giridhar Malavalia9083012010-04-12 17:59:55 -07002154#endif
2155
2156 if (test_bit(MBX_INTR_WAIT, &ha->mbx_cmd_flags) &&
2157 (status & MBX_INTERRUPT) && ha->flags.mbox_int) {
2158 set_bit(MBX_INTERRUPT, &ha->mbx_cmd_flags);
2159 complete(&ha->mbx_intr_comp);
2160 }
2161 return IRQ_HANDLED;
2162}
2163
2164irqreturn_t
2165qla82xx_msix_rsp_q(int irq, void *dev_id)
2166{
2167 scsi_qla_host_t *vha;
2168 struct qla_hw_data *ha;
2169 struct rsp_que *rsp;
2170 struct device_reg_82xx __iomem *reg;
Saurav Kashyap3553d342011-08-16 11:29:27 -07002171 unsigned long flags;
Giridhar Malavalia9083012010-04-12 17:59:55 -07002172
2173 rsp = (struct rsp_que *) dev_id;
2174 if (!rsp) {
2175 printk(KERN_INFO
Saurav Kashyap7c3df132011-07-14 12:00:13 -07002176 "%s(): NULL response queue pointer.\n", __func__);
Giridhar Malavalia9083012010-04-12 17:59:55 -07002177 return IRQ_NONE;
2178 }
2179
2180 ha = rsp->hw;
2181 reg = &ha->iobase->isp82;
Saurav Kashyap3553d342011-08-16 11:29:27 -07002182 spin_lock_irqsave(&ha->hardware_lock, flags);
Giridhar Malavalia9083012010-04-12 17:59:55 -07002183 vha = pci_get_drvdata(ha->pdev);
2184 qla24xx_process_response_queue(vha, rsp);
2185 WRT_REG_DWORD(&reg->host_int, 0);
Saurav Kashyap3553d342011-08-16 11:29:27 -07002186 spin_unlock_irqrestore(&ha->hardware_lock, flags);
Giridhar Malavalia9083012010-04-12 17:59:55 -07002187 return IRQ_HANDLED;
2188}
2189
2190void
2191qla82xx_poll(int irq, void *dev_id)
2192{
2193 scsi_qla_host_t *vha;
2194 struct qla_hw_data *ha;
2195 struct rsp_que *rsp;
2196 struct device_reg_82xx __iomem *reg;
2197 int status = 0;
2198 uint32_t stat;
2199 uint16_t mb[4];
2200 unsigned long flags;
2201
2202 rsp = (struct rsp_que *) dev_id;
2203 if (!rsp) {
2204 printk(KERN_INFO
Saurav Kashyap7c3df132011-07-14 12:00:13 -07002205 "%s(): NULL response queue pointer.\n", __func__);
Giridhar Malavalia9083012010-04-12 17:59:55 -07002206 return;
2207 }
2208 ha = rsp->hw;
2209
2210 reg = &ha->iobase->isp82;
2211 spin_lock_irqsave(&ha->hardware_lock, flags);
2212 vha = pci_get_drvdata(ha->pdev);
2213
2214 if (RD_REG_DWORD(&reg->host_int)) {
2215 stat = RD_REG_DWORD(&reg->host_status);
2216 switch (stat & 0xff) {
2217 case 0x1:
2218 case 0x2:
2219 case 0x10:
2220 case 0x11:
2221 qla82xx_mbx_completion(vha, MSW(stat));
2222 status |= MBX_INTERRUPT;
2223 break;
2224 case 0x12:
2225 mb[0] = MSW(stat);
2226 mb[1] = RD_REG_WORD(&reg->mailbox_out[1]);
2227 mb[2] = RD_REG_WORD(&reg->mailbox_out[2]);
2228 mb[3] = RD_REG_WORD(&reg->mailbox_out[3]);
2229 qla2x00_async_event(vha, rsp, mb);
2230 break;
2231 case 0x13:
2232 qla24xx_process_response_queue(vha, rsp);
2233 break;
2234 default:
Saurav Kashyap7c3df132011-07-14 12:00:13 -07002235 ql_dbg(ql_dbg_p3p, vha, 0xb013,
2236 "Unrecognized interrupt type (%d).\n",
2237 stat * 0xff);
Giridhar Malavalia9083012010-04-12 17:59:55 -07002238 break;
2239 }
2240 }
2241 WRT_REG_DWORD(&reg->host_int, 0);
2242 spin_unlock_irqrestore(&ha->hardware_lock, flags);
2243}
2244
2245void
2246qla82xx_enable_intrs(struct qla_hw_data *ha)
2247{
2248 scsi_qla_host_t *vha = pci_get_drvdata(ha->pdev);
2249 qla82xx_mbx_intr_enable(vha);
2250 spin_lock_irq(&ha->hardware_lock);
2251 qla82xx_wr_32(ha, ha->nx_legacy_intr.tgt_mask_reg, 0xfbff);
2252 spin_unlock_irq(&ha->hardware_lock);
2253 ha->interrupts_on = 1;
2254}
2255
2256void
2257qla82xx_disable_intrs(struct qla_hw_data *ha)
2258{
2259 scsi_qla_host_t *vha = pci_get_drvdata(ha->pdev);
2260 qla82xx_mbx_intr_disable(vha);
2261 spin_lock_irq(&ha->hardware_lock);
2262 qla82xx_wr_32(ha, ha->nx_legacy_intr.tgt_mask_reg, 0x0400);
2263 spin_unlock_irq(&ha->hardware_lock);
2264 ha->interrupts_on = 0;
2265}
2266
2267void qla82xx_init_flags(struct qla_hw_data *ha)
2268{
2269 struct qla82xx_legacy_intr_set *nx_legacy_intr;
2270
2271 /* ISP 8021 initializations */
2272 rwlock_init(&ha->hw_lock);
2273 ha->qdr_sn_window = -1;
2274 ha->ddr_mn_window = -1;
2275 ha->curr_window = 255;
2276 ha->portnum = PCI_FUNC(ha->pdev->devfn);
2277 nx_legacy_intr = &legacy_intr[ha->portnum];
2278 ha->nx_legacy_intr.int_vec_bit = nx_legacy_intr->int_vec_bit;
2279 ha->nx_legacy_intr.tgt_status_reg = nx_legacy_intr->tgt_status_reg;
2280 ha->nx_legacy_intr.tgt_mask_reg = nx_legacy_intr->tgt_mask_reg;
2281 ha->nx_legacy_intr.pci_int_reg = nx_legacy_intr->pci_int_reg;
2282}
2283
Lalit Chandivadea5b36322010-09-03 15:20:50 -07002284inline void
Saurav Kashyap0251ce82012-08-22 14:21:22 -04002285qla82xx_set_idc_version(scsi_qla_host_t *vha)
2286{
2287 int idc_ver;
2288 uint32_t drv_active;
2289 struct qla_hw_data *ha = vha->hw;
2290
2291 drv_active = qla82xx_rd_32(ha, QLA82XX_CRB_DRV_ACTIVE);
2292 if (drv_active == (QLA82XX_DRV_ACTIVE << (ha->portnum * 4))) {
2293 qla82xx_wr_32(ha, QLA82XX_CRB_DRV_IDC_VERSION,
2294 QLA82XX_IDC_VERSION);
2295 ql_log(ql_log_info, vha, 0xb082,
2296 "IDC version updated to %d\n", QLA82XX_IDC_VERSION);
2297 } else {
2298 idc_ver = qla82xx_rd_32(ha, QLA82XX_CRB_DRV_IDC_VERSION);
2299 if (idc_ver != QLA82XX_IDC_VERSION)
2300 ql_log(ql_log_info, vha, 0xb083,
2301 "qla2xxx driver IDC version %d is not compatible "
2302 "with IDC version %d of the other drivers\n",
2303 QLA82XX_IDC_VERSION, idc_ver);
2304 }
2305}
2306
2307inline void
Giridhar Malavalia9083012010-04-12 17:59:55 -07002308qla82xx_set_drv_active(scsi_qla_host_t *vha)
2309{
2310 uint32_t drv_active;
2311 struct qla_hw_data *ha = vha->hw;
2312
2313 drv_active = qla82xx_rd_32(ha, QLA82XX_CRB_DRV_ACTIVE);
2314
2315 /* If reset value is all FF's, initialize DRV_ACTIVE */
2316 if (drv_active == 0xffffffff) {
Giridhar Malavali77e334d2010-09-03 15:20:52 -07002317 qla82xx_wr_32(ha, QLA82XX_CRB_DRV_ACTIVE,
2318 QLA82XX_DRV_NOT_ACTIVE);
Giridhar Malavalia9083012010-04-12 17:59:55 -07002319 drv_active = qla82xx_rd_32(ha, QLA82XX_CRB_DRV_ACTIVE);
2320 }
Giridhar Malavali77e334d2010-09-03 15:20:52 -07002321 drv_active |= (QLA82XX_DRV_ACTIVE << (ha->portnum * 4));
Giridhar Malavalia9083012010-04-12 17:59:55 -07002322 qla82xx_wr_32(ha, QLA82XX_CRB_DRV_ACTIVE, drv_active);
2323}
2324
2325inline void
2326qla82xx_clear_drv_active(struct qla_hw_data *ha)
2327{
2328 uint32_t drv_active;
2329
2330 drv_active = qla82xx_rd_32(ha, QLA82XX_CRB_DRV_ACTIVE);
Giridhar Malavali77e334d2010-09-03 15:20:52 -07002331 drv_active &= ~(QLA82XX_DRV_ACTIVE << (ha->portnum * 4));
Giridhar Malavalia9083012010-04-12 17:59:55 -07002332 qla82xx_wr_32(ha, QLA82XX_CRB_DRV_ACTIVE, drv_active);
2333}
2334
2335static inline int
2336qla82xx_need_reset(struct qla_hw_data *ha)
2337{
2338 uint32_t drv_state;
2339 int rval;
2340
Santosh Vernekar7d613ac2012-08-22 14:21:03 -04002341 if (ha->flags.nic_core_reset_owner)
Giridhar Malavali08de2842011-08-16 11:31:44 -07002342 return 1;
2343 else {
2344 drv_state = qla82xx_rd_32(ha, QLA82XX_CRB_DRV_STATE);
2345 rval = drv_state & (QLA82XX_DRVST_RST_RDY << (ha->portnum * 4));
2346 return rval;
2347 }
Giridhar Malavalia9083012010-04-12 17:59:55 -07002348}
2349
2350static inline void
2351qla82xx_set_rst_ready(struct qla_hw_data *ha)
2352{
2353 uint32_t drv_state;
2354 scsi_qla_host_t *vha = pci_get_drvdata(ha->pdev);
2355
2356 drv_state = qla82xx_rd_32(ha, QLA82XX_CRB_DRV_STATE);
2357
2358 /* If reset value is all FF's, initialize DRV_STATE */
2359 if (drv_state == 0xffffffff) {
Giridhar Malavali77e334d2010-09-03 15:20:52 -07002360 qla82xx_wr_32(ha, QLA82XX_CRB_DRV_STATE, QLA82XX_DRVST_NOT_RDY);
Giridhar Malavalia9083012010-04-12 17:59:55 -07002361 drv_state = qla82xx_rd_32(ha, QLA82XX_CRB_DRV_STATE);
2362 }
2363 drv_state |= (QLA82XX_DRVST_RST_RDY << (ha->portnum * 4));
Giridhar Malavali08de2842011-08-16 11:31:44 -07002364 ql_dbg(ql_dbg_init, vha, 0x00bb,
2365 "drv_state = 0x%08x.\n", drv_state);
Giridhar Malavalia9083012010-04-12 17:59:55 -07002366 qla82xx_wr_32(ha, QLA82XX_CRB_DRV_STATE, drv_state);
2367}
2368
2369static inline void
2370qla82xx_clear_rst_ready(struct qla_hw_data *ha)
2371{
2372 uint32_t drv_state;
2373
2374 drv_state = qla82xx_rd_32(ha, QLA82XX_CRB_DRV_STATE);
2375 drv_state &= ~(QLA82XX_DRVST_RST_RDY << (ha->portnum * 4));
2376 qla82xx_wr_32(ha, QLA82XX_CRB_DRV_STATE, drv_state);
2377}
2378
2379static inline void
2380qla82xx_set_qsnt_ready(struct qla_hw_data *ha)
2381{
2382 uint32_t qsnt_state;
2383
2384 qsnt_state = qla82xx_rd_32(ha, QLA82XX_CRB_DRV_STATE);
2385 qsnt_state |= (QLA82XX_DRVST_QSNT_RDY << (ha->portnum * 4));
2386 qla82xx_wr_32(ha, QLA82XX_CRB_DRV_STATE, qsnt_state);
2387}
2388
Saurav Kashyap579d12b2010-12-21 16:00:14 -08002389void
2390qla82xx_clear_qsnt_ready(scsi_qla_host_t *vha)
2391{
2392 struct qla_hw_data *ha = vha->hw;
2393 uint32_t qsnt_state;
2394
2395 qsnt_state = qla82xx_rd_32(ha, QLA82XX_CRB_DRV_STATE);
2396 qsnt_state &= ~(QLA82XX_DRVST_QSNT_RDY << (ha->portnum * 4));
2397 qla82xx_wr_32(ha, QLA82XX_CRB_DRV_STATE, qsnt_state);
2398}
2399
Giridhar Malavali77e334d2010-09-03 15:20:52 -07002400static int
2401qla82xx_load_fw(scsi_qla_host_t *vha)
Giridhar Malavalia9083012010-04-12 17:59:55 -07002402{
2403 int rst;
2404 struct fw_blob *blob;
2405 struct qla_hw_data *ha = vha->hw;
2406
Giridhar Malavalia9083012010-04-12 17:59:55 -07002407 if (qla82xx_pinit_from_rom(vha) != QLA_SUCCESS) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07002408 ql_log(ql_log_fatal, vha, 0x009f,
2409 "Error during CRB initialization.\n");
Giridhar Malavalia9083012010-04-12 17:59:55 -07002410 return QLA_FUNCTION_FAILED;
2411 }
2412 udelay(500);
2413
2414 /* Bring QM and CAMRAM out of reset */
2415 rst = qla82xx_rd_32(ha, QLA82XX_ROMUSB_GLB_SW_RESET);
2416 rst &= ~((1 << 28) | (1 << 24));
2417 qla82xx_wr_32(ha, QLA82XX_ROMUSB_GLB_SW_RESET, rst);
2418
2419 /*
2420 * FW Load priority:
2421 * 1) Operational firmware residing in flash.
2422 * 2) Firmware via request-firmware interface (.bin file).
2423 */
2424 if (ql2xfwloadbin == 2)
2425 goto try_blob_fw;
2426
Saurav Kashyap7c3df132011-07-14 12:00:13 -07002427 ql_log(ql_log_info, vha, 0x00a0,
2428 "Attempting to load firmware from flash.\n");
Giridhar Malavalia9083012010-04-12 17:59:55 -07002429
2430 if (qla82xx_fw_load_from_flash(ha) == QLA_SUCCESS) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07002431 ql_log(ql_log_info, vha, 0x00a1,
Saurav Kashyap00adc9a2012-05-15 14:34:22 -04002432 "Firmware loaded successfully from flash.\n");
Giridhar Malavalia9083012010-04-12 17:59:55 -07002433 return QLA_SUCCESS;
Chad Dupuis875efad72011-05-10 11:30:09 -07002434 } else {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07002435 ql_log(ql_log_warn, vha, 0x0108,
2436 "Firmware load from flash failed.\n");
Giridhar Malavalia9083012010-04-12 17:59:55 -07002437 }
Chad Dupuis875efad72011-05-10 11:30:09 -07002438
Giridhar Malavalia9083012010-04-12 17:59:55 -07002439try_blob_fw:
Saurav Kashyap7c3df132011-07-14 12:00:13 -07002440 ql_log(ql_log_info, vha, 0x00a2,
2441 "Attempting to load firmware from blob.\n");
Giridhar Malavalia9083012010-04-12 17:59:55 -07002442
2443 /* Load firmware blob. */
2444 blob = ha->hablob = qla2x00_request_firmware(vha);
2445 if (!blob) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07002446 ql_log(ql_log_fatal, vha, 0x00a3,
Saurav Kashyap00adc9a2012-05-15 14:34:22 -04002447 "Firmware image not present.\n");
Giridhar Malavalia9083012010-04-12 17:59:55 -07002448 goto fw_load_failed;
2449 }
2450
Harish Zunjarrao9c2b2972010-05-28 15:08:23 -07002451 /* Validating firmware blob */
2452 if (qla82xx_validate_firmware_blob(vha,
2453 QLA82XX_FLASH_ROMIMAGE)) {
2454 /* Fallback to URI format */
2455 if (qla82xx_validate_firmware_blob(vha,
2456 QLA82XX_UNIFIED_ROMIMAGE)) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07002457 ql_log(ql_log_fatal, vha, 0x00a4,
2458 "No valid firmware image found.\n");
Harish Zunjarrao9c2b2972010-05-28 15:08:23 -07002459 return QLA_FUNCTION_FAILED;
2460 }
2461 }
2462
Giridhar Malavalia9083012010-04-12 17:59:55 -07002463 if (qla82xx_fw_load_from_blob(ha) == QLA_SUCCESS) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07002464 ql_log(ql_log_info, vha, 0x00a5,
2465 "Firmware loaded successfully from binary blob.\n");
Giridhar Malavalia9083012010-04-12 17:59:55 -07002466 return QLA_SUCCESS;
2467 } else {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07002468 ql_log(ql_log_fatal, vha, 0x00a6,
2469 "Firmware load failed for binary blob.\n");
Giridhar Malavalia9083012010-04-12 17:59:55 -07002470 blob->fw = NULL;
2471 blob = NULL;
2472 goto fw_load_failed;
2473 }
2474 return QLA_SUCCESS;
2475
2476fw_load_failed:
2477 return QLA_FUNCTION_FAILED;
2478}
2479
Lalit Chandivadea5b36322010-09-03 15:20:50 -07002480int
Giridhar Malavalia9083012010-04-12 17:59:55 -07002481qla82xx_start_firmware(scsi_qla_host_t *vha)
2482{
Giridhar Malavalia9083012010-04-12 17:59:55 -07002483 uint16_t lnk;
2484 struct qla_hw_data *ha = vha->hw;
2485
2486 /* scrub dma mask expansion register */
Giridhar Malavali77e334d2010-09-03 15:20:52 -07002487 qla82xx_wr_32(ha, CRB_DMA_SHIFT, QLA82XX_DMA_SHIFT_VALUE);
Giridhar Malavalia9083012010-04-12 17:59:55 -07002488
Giridhar Malavali37113332010-07-23 15:28:34 +05002489 /* Put both the PEG CMD and RCV PEG to default state
2490 * of 0 before resetting the hardware
2491 */
2492 qla82xx_wr_32(ha, CRB_CMDPEG_STATE, 0);
2493 qla82xx_wr_32(ha, CRB_RCVPEG_STATE, 0);
2494
Giridhar Malavalia9083012010-04-12 17:59:55 -07002495 /* Overwrite stale initialization register values */
2496 qla82xx_wr_32(ha, QLA82XX_PEG_HALT_STATUS1, 0);
2497 qla82xx_wr_32(ha, QLA82XX_PEG_HALT_STATUS2, 0);
2498
2499 if (qla82xx_load_fw(vha) != QLA_SUCCESS) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07002500 ql_log(ql_log_fatal, vha, 0x00a7,
2501 "Error trying to start fw.\n");
Giridhar Malavalia9083012010-04-12 17:59:55 -07002502 return QLA_FUNCTION_FAILED;
2503 }
2504
2505 /* Handshake with the card before we register the devices. */
2506 if (qla82xx_check_cmdpeg_state(ha) != QLA_SUCCESS) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07002507 ql_log(ql_log_fatal, vha, 0x00aa,
2508 "Error during card handshake.\n");
Giridhar Malavalia9083012010-04-12 17:59:55 -07002509 return QLA_FUNCTION_FAILED;
2510 }
2511
2512 /* Negotiated Link width */
Jiang Liu10092432012-08-20 14:23:48 -06002513 pcie_capability_read_word(ha->pdev, PCI_EXP_LNKSTA, &lnk);
Giridhar Malavalia9083012010-04-12 17:59:55 -07002514 ha->link_width = (lnk >> 4) & 0x3f;
2515
2516 /* Synchronize with Receive peg */
2517 return qla82xx_check_rcvpeg_state(ha);
2518}
2519
Giridhar Malavali77e334d2010-09-03 15:20:52 -07002520static uint32_t *
Giridhar Malavalia9083012010-04-12 17:59:55 -07002521qla82xx_read_flash_data(scsi_qla_host_t *vha, uint32_t *dwptr, uint32_t faddr,
2522 uint32_t length)
2523{
2524 uint32_t i;
2525 uint32_t val;
2526 struct qla_hw_data *ha = vha->hw;
2527
2528 /* Dword reads to flash. */
2529 for (i = 0; i < length/4; i++, faddr += 4) {
2530 if (qla82xx_rom_fast_read(ha, faddr, &val)) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07002531 ql_log(ql_log_warn, vha, 0x0106,
2532 "Do ROM fast read failed.\n");
Giridhar Malavalia9083012010-04-12 17:59:55 -07002533 goto done_read;
2534 }
2535 dwptr[i] = __constant_cpu_to_le32(val);
2536 }
2537done_read:
2538 return dwptr;
2539}
2540
Giridhar Malavali77e334d2010-09-03 15:20:52 -07002541static int
Giridhar Malavalia9083012010-04-12 17:59:55 -07002542qla82xx_unprotect_flash(struct qla_hw_data *ha)
2543{
2544 int ret;
2545 uint32_t val;
Saurav Kashyap7c3df132011-07-14 12:00:13 -07002546 scsi_qla_host_t *vha = pci_get_drvdata(ha->pdev);
Giridhar Malavalia9083012010-04-12 17:59:55 -07002547
2548 ret = ql82xx_rom_lock_d(ha);
2549 if (ret < 0) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07002550 ql_log(ql_log_warn, vha, 0xb014,
2551 "ROM Lock failed.\n");
Giridhar Malavalia9083012010-04-12 17:59:55 -07002552 return ret;
2553 }
2554
2555 ret = qla82xx_read_status_reg(ha, &val);
2556 if (ret < 0)
2557 goto done_unprotect;
2558
Lalit Chandivade0547fb32010-05-28 15:08:26 -07002559 val &= ~(BLOCK_PROTECT_BITS << 2);
Giridhar Malavalia9083012010-04-12 17:59:55 -07002560 ret = qla82xx_write_status_reg(ha, val);
2561 if (ret < 0) {
Lalit Chandivade0547fb32010-05-28 15:08:26 -07002562 val |= (BLOCK_PROTECT_BITS << 2);
Giridhar Malavalia9083012010-04-12 17:59:55 -07002563 qla82xx_write_status_reg(ha, val);
2564 }
2565
2566 if (qla82xx_write_disable_flash(ha) != 0)
Saurav Kashyap7c3df132011-07-14 12:00:13 -07002567 ql_log(ql_log_warn, vha, 0xb015,
2568 "Write disable failed.\n");
Giridhar Malavalia9083012010-04-12 17:59:55 -07002569
2570done_unprotect:
Chad Dupuisd652e092011-05-10 11:30:10 -07002571 qla82xx_rom_unlock(ha);
Giridhar Malavalia9083012010-04-12 17:59:55 -07002572 return ret;
2573}
2574
Giridhar Malavali77e334d2010-09-03 15:20:52 -07002575static int
Giridhar Malavalia9083012010-04-12 17:59:55 -07002576qla82xx_protect_flash(struct qla_hw_data *ha)
2577{
2578 int ret;
2579 uint32_t val;
Saurav Kashyap7c3df132011-07-14 12:00:13 -07002580 scsi_qla_host_t *vha = pci_get_drvdata(ha->pdev);
Giridhar Malavalia9083012010-04-12 17:59:55 -07002581
2582 ret = ql82xx_rom_lock_d(ha);
2583 if (ret < 0) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07002584 ql_log(ql_log_warn, vha, 0xb016,
2585 "ROM Lock failed.\n");
Giridhar Malavalia9083012010-04-12 17:59:55 -07002586 return ret;
2587 }
2588
2589 ret = qla82xx_read_status_reg(ha, &val);
2590 if (ret < 0)
2591 goto done_protect;
2592
Lalit Chandivade0547fb32010-05-28 15:08:26 -07002593 val |= (BLOCK_PROTECT_BITS << 2);
Giridhar Malavalia9083012010-04-12 17:59:55 -07002594 /* LOCK all sectors */
2595 ret = qla82xx_write_status_reg(ha, val);
2596 if (ret < 0)
Saurav Kashyap7c3df132011-07-14 12:00:13 -07002597 ql_log(ql_log_warn, vha, 0xb017,
2598 "Write status register failed.\n");
Giridhar Malavalia9083012010-04-12 17:59:55 -07002599
2600 if (qla82xx_write_disable_flash(ha) != 0)
Saurav Kashyap7c3df132011-07-14 12:00:13 -07002601 ql_log(ql_log_warn, vha, 0xb018,
2602 "Write disable failed.\n");
Giridhar Malavalia9083012010-04-12 17:59:55 -07002603done_protect:
Chad Dupuisd652e092011-05-10 11:30:10 -07002604 qla82xx_rom_unlock(ha);
Giridhar Malavalia9083012010-04-12 17:59:55 -07002605 return ret;
2606}
2607
Giridhar Malavali77e334d2010-09-03 15:20:52 -07002608static int
Giridhar Malavalia9083012010-04-12 17:59:55 -07002609qla82xx_erase_sector(struct qla_hw_data *ha, int addr)
2610{
2611 int ret = 0;
Saurav Kashyap7c3df132011-07-14 12:00:13 -07002612 scsi_qla_host_t *vha = pci_get_drvdata(ha->pdev);
Giridhar Malavalia9083012010-04-12 17:59:55 -07002613
2614 ret = ql82xx_rom_lock_d(ha);
2615 if (ret < 0) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07002616 ql_log(ql_log_warn, vha, 0xb019,
2617 "ROM Lock failed.\n");
Giridhar Malavalia9083012010-04-12 17:59:55 -07002618 return ret;
2619 }
2620
2621 qla82xx_flash_set_write_enable(ha);
2622 qla82xx_wr_32(ha, QLA82XX_ROMUSB_ROM_ADDRESS, addr);
2623 qla82xx_wr_32(ha, QLA82XX_ROMUSB_ROM_ABYTE_CNT, 3);
2624 qla82xx_wr_32(ha, QLA82XX_ROMUSB_ROM_INSTR_OPCODE, M25P_INSTR_SE);
2625
2626 if (qla82xx_wait_rom_done(ha)) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07002627 ql_log(ql_log_warn, vha, 0xb01a,
2628 "Error waiting for rom done.\n");
Giridhar Malavalia9083012010-04-12 17:59:55 -07002629 ret = -1;
2630 goto done;
2631 }
2632 ret = qla82xx_flash_wait_write_finish(ha);
2633done:
Chad Dupuisd652e092011-05-10 11:30:10 -07002634 qla82xx_rom_unlock(ha);
Giridhar Malavalia9083012010-04-12 17:59:55 -07002635 return ret;
2636}
2637
2638/*
2639 * Address and length are byte address
2640 */
2641uint8_t *
2642qla82xx_read_optrom_data(struct scsi_qla_host *vha, uint8_t *buf,
2643 uint32_t offset, uint32_t length)
2644{
2645 scsi_block_requests(vha->host);
2646 qla82xx_read_flash_data(vha, (uint32_t *)buf, offset, length);
2647 scsi_unblock_requests(vha->host);
2648 return buf;
2649}
2650
2651static int
2652qla82xx_write_flash_data(struct scsi_qla_host *vha, uint32_t *dwptr,
2653 uint32_t faddr, uint32_t dwords)
2654{
2655 int ret;
2656 uint32_t liter;
2657 uint32_t sec_mask, rest_addr;
2658 dma_addr_t optrom_dma;
2659 void *optrom = NULL;
2660 int page_mode = 0;
2661 struct qla_hw_data *ha = vha->hw;
2662
2663 ret = -1;
2664
2665 /* Prepare burst-capable write on supported ISPs. */
2666 if (page_mode && !(faddr & 0xfff) &&
2667 dwords > OPTROM_BURST_DWORDS) {
2668 optrom = dma_alloc_coherent(&ha->pdev->dev, OPTROM_BURST_SIZE,
2669 &optrom_dma, GFP_KERNEL);
2670 if (!optrom) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07002671 ql_log(ql_log_warn, vha, 0xb01b,
2672 "Unable to allocate memory "
Saurav Kashyap00adc9a2012-05-15 14:34:22 -04002673 "for optrom burst write (%x KB).\n",
Saurav Kashyap7c3df132011-07-14 12:00:13 -07002674 OPTROM_BURST_SIZE / 1024);
Giridhar Malavalia9083012010-04-12 17:59:55 -07002675 }
2676 }
2677
2678 rest_addr = ha->fdt_block_size - 1;
2679 sec_mask = ~rest_addr;
2680
2681 ret = qla82xx_unprotect_flash(ha);
2682 if (ret) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07002683 ql_log(ql_log_warn, vha, 0xb01c,
2684 "Unable to unprotect flash for update.\n");
Giridhar Malavalia9083012010-04-12 17:59:55 -07002685 goto write_done;
2686 }
2687
2688 for (liter = 0; liter < dwords; liter++, faddr += 4, dwptr++) {
2689 /* Are we at the beginning of a sector? */
2690 if ((faddr & rest_addr) == 0) {
2691
2692 ret = qla82xx_erase_sector(ha, faddr);
2693 if (ret) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07002694 ql_log(ql_log_warn, vha, 0xb01d,
2695 "Unable to erase sector: address=%x.\n",
2696 faddr);
Giridhar Malavalia9083012010-04-12 17:59:55 -07002697 break;
2698 }
2699 }
2700
2701 /* Go with burst-write. */
2702 if (optrom && (liter + OPTROM_BURST_DWORDS) <= dwords) {
2703 /* Copy data to DMA'ble buffer. */
2704 memcpy(optrom, dwptr, OPTROM_BURST_SIZE);
2705
2706 ret = qla2x00_load_ram(vha, optrom_dma,
2707 (ha->flash_data_off | faddr),
2708 OPTROM_BURST_DWORDS);
2709 if (ret != QLA_SUCCESS) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07002710 ql_log(ql_log_warn, vha, 0xb01e,
Giridhar Malavalia9083012010-04-12 17:59:55 -07002711 "Unable to burst-write optrom segment "
2712 "(%x/%x/%llx).\n", ret,
2713 (ha->flash_data_off | faddr),
2714 (unsigned long long)optrom_dma);
Saurav Kashyap7c3df132011-07-14 12:00:13 -07002715 ql_log(ql_log_warn, vha, 0xb01f,
Giridhar Malavalia9083012010-04-12 17:59:55 -07002716 "Reverting to slow-write.\n");
2717
2718 dma_free_coherent(&ha->pdev->dev,
2719 OPTROM_BURST_SIZE, optrom, optrom_dma);
2720 optrom = NULL;
2721 } else {
2722 liter += OPTROM_BURST_DWORDS - 1;
2723 faddr += OPTROM_BURST_DWORDS - 1;
2724 dwptr += OPTROM_BURST_DWORDS - 1;
2725 continue;
2726 }
2727 }
2728
2729 ret = qla82xx_write_flash_dword(ha, faddr,
2730 cpu_to_le32(*dwptr));
2731 if (ret) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07002732 ql_dbg(ql_dbg_p3p, vha, 0xb020,
2733 "Unable to program flash address=%x data=%x.\n",
2734 faddr, *dwptr);
Giridhar Malavalia9083012010-04-12 17:59:55 -07002735 break;
2736 }
2737 }
2738
2739 ret = qla82xx_protect_flash(ha);
2740 if (ret)
Saurav Kashyap7c3df132011-07-14 12:00:13 -07002741 ql_log(ql_log_warn, vha, 0xb021,
Giridhar Malavalia9083012010-04-12 17:59:55 -07002742 "Unable to protect flash after update.\n");
2743write_done:
2744 if (optrom)
2745 dma_free_coherent(&ha->pdev->dev,
2746 OPTROM_BURST_SIZE, optrom, optrom_dma);
2747 return ret;
2748}
2749
2750int
2751qla82xx_write_optrom_data(struct scsi_qla_host *vha, uint8_t *buf,
2752 uint32_t offset, uint32_t length)
2753{
2754 int rval;
2755
2756 /* Suspend HBA. */
2757 scsi_block_requests(vha->host);
2758 rval = qla82xx_write_flash_data(vha, (uint32_t *)buf, offset,
2759 length >> 2);
2760 scsi_unblock_requests(vha->host);
2761
2762 /* Convert return ISP82xx to generic */
2763 if (rval)
2764 rval = QLA_FUNCTION_FAILED;
2765 else
2766 rval = QLA_SUCCESS;
2767 return rval;
2768}
2769
2770void
Giridhar Malavali5162cf02011-11-18 09:03:18 -08002771qla82xx_start_iocbs(scsi_qla_host_t *vha)
Giridhar Malavalia9083012010-04-12 17:59:55 -07002772{
Giridhar Malavali5162cf02011-11-18 09:03:18 -08002773 struct qla_hw_data *ha = vha->hw;
Giridhar Malavalia9083012010-04-12 17:59:55 -07002774 struct req_que *req = ha->req_q_map[0];
2775 struct device_reg_82xx __iomem *reg;
2776 uint32_t dbval;
2777
2778 /* Adjust ring index. */
2779 req->ring_index++;
2780 if (req->ring_index == req->length) {
2781 req->ring_index = 0;
2782 req->ring_ptr = req->ring;
2783 } else
2784 req->ring_ptr++;
2785
2786 reg = &ha->iobase->isp82;
2787 dbval = 0x04 | (ha->portnum << 5);
2788
2789 dbval = dbval | (req->id << 8) | (req->ring_index << 16);
Giridhar Malavali69078692010-05-28 15:08:28 -07002790 if (ql2xdbwr)
2791 qla82xx_wr_32(ha, ha->nxdb_wr_ptr, dbval);
2792 else {
2793 WRT_REG_DWORD((unsigned long __iomem *)ha->nxdb_wr_ptr, dbval);
Giridhar Malavalia9083012010-04-12 17:59:55 -07002794 wmb();
Saurav Kashyapfa492632012-11-21 02:40:29 -05002795 while (RD_REG_DWORD((void __iomem *)ha->nxdb_rd_ptr) != dbval) {
Giridhar Malavali69078692010-05-28 15:08:28 -07002796 WRT_REG_DWORD((unsigned long __iomem *)ha->nxdb_wr_ptr,
2797 dbval);
2798 wmb();
2799 }
Giridhar Malavalia9083012010-04-12 17:59:55 -07002800 }
2801}
2802
Saurav Kashyapfa492632012-11-21 02:40:29 -05002803static void
2804qla82xx_rom_lock_recovery(struct qla_hw_data *ha)
Shyam Sundare6a42022010-09-07 20:55:32 -07002805{
Saurav Kashyap7c3df132011-07-14 12:00:13 -07002806 scsi_qla_host_t *vha = pci_get_drvdata(ha->pdev);
2807
Shyam Sundare6a42022010-09-07 20:55:32 -07002808 if (qla82xx_rom_lock(ha))
2809 /* Someone else is holding the lock. */
Saurav Kashyap7c3df132011-07-14 12:00:13 -07002810 ql_log(ql_log_info, vha, 0xb022,
2811 "Resetting rom_lock.\n");
Shyam Sundare6a42022010-09-07 20:55:32 -07002812
2813 /*
2814 * Either we got the lock, or someone
2815 * else died while holding it.
2816 * In either case, unlock.
2817 */
Chad Dupuisd652e092011-05-10 11:30:10 -07002818 qla82xx_rom_unlock(ha);
Shyam Sundare6a42022010-09-07 20:55:32 -07002819}
2820
Giridhar Malavalia9083012010-04-12 17:59:55 -07002821/*
2822 * qla82xx_device_bootstrap
2823 * Initialize device, set DEV_READY, start fw
2824 *
2825 * Note:
2826 * IDC lock must be held upon entry
2827 *
2828 * Return:
2829 * Success : 0
2830 * Failed : 1
2831 */
2832static int
2833qla82xx_device_bootstrap(scsi_qla_host_t *vha)
2834{
Shyam Sundare6a42022010-09-07 20:55:32 -07002835 int rval = QLA_SUCCESS;
2836 int i, timeout;
Giridhar Malavalia9083012010-04-12 17:59:55 -07002837 uint32_t old_count, count;
2838 struct qla_hw_data *ha = vha->hw;
Shyam Sundare6a42022010-09-07 20:55:32 -07002839 int need_reset = 0, peg_stuck = 1;
Giridhar Malavalia9083012010-04-12 17:59:55 -07002840
Shyam Sundare6a42022010-09-07 20:55:32 -07002841 need_reset = qla82xx_need_reset(ha);
Giridhar Malavalia9083012010-04-12 17:59:55 -07002842
2843 old_count = qla82xx_rd_32(ha, QLA82XX_PEG_ALIVE_COUNTER);
2844
2845 for (i = 0; i < 10; i++) {
2846 timeout = msleep_interruptible(200);
2847 if (timeout) {
2848 qla82xx_wr_32(ha, QLA82XX_CRB_DEV_STATE,
Santosh Vernekar7d613ac2012-08-22 14:21:03 -04002849 QLA8XXX_DEV_FAILED);
Giridhar Malavalia9083012010-04-12 17:59:55 -07002850 return QLA_FUNCTION_FAILED;
2851 }
2852
2853 count = qla82xx_rd_32(ha, QLA82XX_PEG_ALIVE_COUNTER);
2854 if (count != old_count)
Shyam Sundare6a42022010-09-07 20:55:32 -07002855 peg_stuck = 0;
2856 }
2857
2858 if (need_reset) {
2859 /* We are trying to perform a recovery here. */
2860 if (peg_stuck)
2861 qla82xx_rom_lock_recovery(ha);
2862 goto dev_initialize;
2863 } else {
2864 /* Start of day for this ha context. */
2865 if (peg_stuck) {
2866 /* Either we are the first or recovery in progress. */
2867 qla82xx_rom_lock_recovery(ha);
2868 goto dev_initialize;
2869 } else
2870 /* Firmware already running. */
Giridhar Malavalia9083012010-04-12 17:59:55 -07002871 goto dev_ready;
2872 }
2873
Shyam Sundare6a42022010-09-07 20:55:32 -07002874 return rval;
2875
Giridhar Malavalia9083012010-04-12 17:59:55 -07002876dev_initialize:
2877 /* set to DEV_INITIALIZING */
Saurav Kashyap7c3df132011-07-14 12:00:13 -07002878 ql_log(ql_log_info, vha, 0x009e,
2879 "HW State: INITIALIZING.\n");
Santosh Vernekar7d613ac2012-08-22 14:21:03 -04002880 qla82xx_wr_32(ha, QLA82XX_CRB_DEV_STATE, QLA8XXX_DEV_INITIALIZING);
Giridhar Malavalia9083012010-04-12 17:59:55 -07002881
2882 qla82xx_idc_unlock(ha);
2883 rval = qla82xx_start_firmware(vha);
2884 qla82xx_idc_lock(ha);
2885
2886 if (rval != QLA_SUCCESS) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07002887 ql_log(ql_log_fatal, vha, 0x00ad,
2888 "HW State: FAILED.\n");
Giridhar Malavalia9083012010-04-12 17:59:55 -07002889 qla82xx_clear_drv_active(ha);
Santosh Vernekar7d613ac2012-08-22 14:21:03 -04002890 qla82xx_wr_32(ha, QLA82XX_CRB_DEV_STATE, QLA8XXX_DEV_FAILED);
Giridhar Malavalia9083012010-04-12 17:59:55 -07002891 return rval;
2892 }
2893
2894dev_ready:
Saurav Kashyap7c3df132011-07-14 12:00:13 -07002895 ql_log(ql_log_info, vha, 0x00ae,
2896 "HW State: READY.\n");
Santosh Vernekar7d613ac2012-08-22 14:21:03 -04002897 qla82xx_wr_32(ha, QLA82XX_CRB_DEV_STATE, QLA8XXX_DEV_READY);
Giridhar Malavalia9083012010-04-12 17:59:55 -07002898
2899 return QLA_SUCCESS;
2900}
2901
Saurav Kashyap579d12b2010-12-21 16:00:14 -08002902/*
2903* qla82xx_need_qsnt_handler
2904* Code to start quiescence sequence
2905*
2906* Note:
2907* IDC lock must be held upon entry
2908*
2909* Return: void
2910*/
2911
2912static void
2913qla82xx_need_qsnt_handler(scsi_qla_host_t *vha)
2914{
2915 struct qla_hw_data *ha = vha->hw;
2916 uint32_t dev_state, drv_state, drv_active;
2917 unsigned long reset_timeout;
2918
2919 if (vha->flags.online) {
2920 /*Block any further I/O and wait for pending cmnds to complete*/
Chad Dupuis8fcd6b82012-08-22 14:21:06 -04002921 qla2x00_quiesce_io(vha);
Saurav Kashyap579d12b2010-12-21 16:00:14 -08002922 }
2923
2924 /* Set the quiescence ready bit */
2925 qla82xx_set_qsnt_ready(ha);
2926
2927 /*wait for 30 secs for other functions to ack */
2928 reset_timeout = jiffies + (30 * HZ);
2929
2930 drv_state = qla82xx_rd_32(ha, QLA82XX_CRB_DRV_STATE);
2931 drv_active = qla82xx_rd_32(ha, QLA82XX_CRB_DRV_ACTIVE);
2932 /* Its 2 that is written when qsnt is acked, moving one bit */
2933 drv_active = drv_active << 0x01;
2934
2935 while (drv_state != drv_active) {
2936
2937 if (time_after_eq(jiffies, reset_timeout)) {
2938 /* quiescence timeout, other functions didn't ack
2939 * changing the state to DEV_READY
2940 */
Saurav Kashyap7c3df132011-07-14 12:00:13 -07002941 ql_log(ql_log_info, vha, 0xb023,
Saurav Kashyap5f28d2d2012-05-15 14:34:15 -04002942 "%s : QUIESCENT TIMEOUT DRV_ACTIVE:%d "
2943 "DRV_STATE:%d.\n", QLA2XXX_DRIVER_NAME,
Saurav Kashyap7c3df132011-07-14 12:00:13 -07002944 drv_active, drv_state);
Saurav Kashyap579d12b2010-12-21 16:00:14 -08002945 qla82xx_wr_32(ha, QLA82XX_CRB_DEV_STATE,
Santosh Vernekar7d613ac2012-08-22 14:21:03 -04002946 QLA8XXX_DEV_READY);
Saurav Kashyap7c3df132011-07-14 12:00:13 -07002947 ql_log(ql_log_info, vha, 0xb025,
2948 "HW State: DEV_READY.\n");
Saurav Kashyap579d12b2010-12-21 16:00:14 -08002949 qla82xx_idc_unlock(ha);
2950 qla2x00_perform_loop_resync(vha);
2951 qla82xx_idc_lock(ha);
2952
2953 qla82xx_clear_qsnt_ready(vha);
2954 return;
2955 }
2956
2957 qla82xx_idc_unlock(ha);
2958 msleep(1000);
2959 qla82xx_idc_lock(ha);
2960
2961 drv_state = qla82xx_rd_32(ha, QLA82XX_CRB_DRV_STATE);
2962 drv_active = qla82xx_rd_32(ha, QLA82XX_CRB_DRV_ACTIVE);
2963 drv_active = drv_active << 0x01;
2964 }
2965 dev_state = qla82xx_rd_32(ha, QLA82XX_CRB_DEV_STATE);
2966 /* everyone acked so set the state to DEV_QUIESCENCE */
Santosh Vernekar7d613ac2012-08-22 14:21:03 -04002967 if (dev_state == QLA8XXX_DEV_NEED_QUIESCENT) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07002968 ql_log(ql_log_info, vha, 0xb026,
2969 "HW State: DEV_QUIESCENT.\n");
Santosh Vernekar7d613ac2012-08-22 14:21:03 -04002970 qla82xx_wr_32(ha, QLA82XX_CRB_DEV_STATE, QLA8XXX_DEV_QUIESCENT);
Saurav Kashyap579d12b2010-12-21 16:00:14 -08002971 }
2972}
2973
2974/*
2975* qla82xx_wait_for_state_change
2976* Wait for device state to change from given current state
2977*
2978* Note:
2979* IDC lock must not be held upon entry
2980*
2981* Return:
2982* Changed device state.
2983*/
2984uint32_t
2985qla82xx_wait_for_state_change(scsi_qla_host_t *vha, uint32_t curr_state)
2986{
2987 struct qla_hw_data *ha = vha->hw;
2988 uint32_t dev_state;
2989
2990 do {
2991 msleep(1000);
2992 qla82xx_idc_lock(ha);
2993 dev_state = qla82xx_rd_32(ha, QLA82XX_CRB_DEV_STATE);
2994 qla82xx_idc_unlock(ha);
2995 } while (dev_state == curr_state);
2996
2997 return dev_state;
2998}
2999
Santosh Vernekar7d613ac2012-08-22 14:21:03 -04003000void
3001qla8xxx_dev_failed_handler(scsi_qla_host_t *vha)
Giridhar Malavalia9083012010-04-12 17:59:55 -07003002{
3003 struct qla_hw_data *ha = vha->hw;
3004
3005 /* Disable the board */
Saurav Kashyap7c3df132011-07-14 12:00:13 -07003006 ql_log(ql_log_fatal, vha, 0x00b8,
3007 "Disabling the board.\n");
Giridhar Malavalia9083012010-04-12 17:59:55 -07003008
Saurav Kashyap1459c0e2012-08-22 14:21:25 -04003009 if (IS_QLA82XX(ha)) {
3010 qla82xx_clear_drv_active(ha);
3011 qla82xx_idc_unlock(ha);
3012 }
Giridhar Malavalib9637522010-05-28 15:08:15 -07003013
Giridhar Malavalia9083012010-04-12 17:59:55 -07003014 /* Set DEV_FAILED flag to disable timer */
3015 vha->device_flags |= DFLG_DEV_FAILED;
3016 qla2x00_abort_all_cmds(vha, DID_NO_CONNECT << 16);
3017 qla2x00_mark_all_devices_lost(vha, 0);
3018 vha->flags.online = 0;
3019 vha->flags.init_done = 0;
3020}
3021
3022/*
3023 * qla82xx_need_reset_handler
3024 * Code to start reset sequence
3025 *
3026 * Note:
3027 * IDC lock must be held upon entry
3028 *
3029 * Return:
3030 * Success : 0
3031 * Failed : 1
3032 */
3033static void
3034qla82xx_need_reset_handler(scsi_qla_host_t *vha)
3035{
Chad Dupuise5fdae52011-08-16 11:31:55 -07003036 uint32_t dev_state, drv_state, drv_active;
3037 uint32_t active_mask = 0;
Giridhar Malavalia9083012010-04-12 17:59:55 -07003038 unsigned long reset_timeout;
3039 struct qla_hw_data *ha = vha->hw;
3040 struct req_que *req = ha->req_q_map[0];
3041
3042 if (vha->flags.online) {
3043 qla82xx_idc_unlock(ha);
3044 qla2x00_abort_isp_cleanup(vha);
3045 ha->isp_ops->get_flash_version(vha, req->ring);
3046 ha->isp_ops->nvram_config(vha);
3047 qla82xx_idc_lock(ha);
3048 }
3049
Giridhar Malavali08de2842011-08-16 11:31:44 -07003050 drv_active = qla82xx_rd_32(ha, QLA82XX_CRB_DRV_ACTIVE);
Santosh Vernekar7d613ac2012-08-22 14:21:03 -04003051 if (!ha->flags.nic_core_reset_owner) {
Giridhar Malavali08de2842011-08-16 11:31:44 -07003052 ql_dbg(ql_dbg_p3p, vha, 0xb028,
3053 "reset_acknowledged by 0x%x\n", ha->portnum);
3054 qla82xx_set_rst_ready(ha);
3055 } else {
3056 active_mask = ~(QLA82XX_DRV_ACTIVE << (ha->portnum * 4));
3057 drv_active &= active_mask;
3058 ql_dbg(ql_dbg_p3p, vha, 0xb029,
3059 "active_mask: 0x%08x\n", active_mask);
3060 }
Giridhar Malavalia9083012010-04-12 17:59:55 -07003061
3062 /* wait for 10 seconds for reset ack from all functions */
Santosh Vernekar7d613ac2012-08-22 14:21:03 -04003063 reset_timeout = jiffies + (ha->fcoe_reset_timeout * HZ);
Giridhar Malavalia9083012010-04-12 17:59:55 -07003064
3065 drv_state = qla82xx_rd_32(ha, QLA82XX_CRB_DRV_STATE);
3066 drv_active = qla82xx_rd_32(ha, QLA82XX_CRB_DRV_ACTIVE);
Giridhar Malavali08de2842011-08-16 11:31:44 -07003067 dev_state = qla82xx_rd_32(ha, QLA82XX_CRB_DEV_STATE);
Giridhar Malavalia9083012010-04-12 17:59:55 -07003068
Giridhar Malavali08de2842011-08-16 11:31:44 -07003069 ql_dbg(ql_dbg_p3p, vha, 0xb02a,
3070 "drv_state: 0x%08x, drv_active: 0x%08x, "
3071 "dev_state: 0x%08x, active_mask: 0x%08x\n",
3072 drv_state, drv_active, dev_state, active_mask);
3073
3074 while (drv_state != drv_active &&
Santosh Vernekar7d613ac2012-08-22 14:21:03 -04003075 dev_state != QLA8XXX_DEV_INITIALIZING) {
Giridhar Malavalia9083012010-04-12 17:59:55 -07003076 if (time_after_eq(jiffies, reset_timeout)) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07003077 ql_log(ql_log_warn, vha, 0x00b5,
3078 "Reset timeout.\n");
Giridhar Malavalia9083012010-04-12 17:59:55 -07003079 break;
3080 }
3081 qla82xx_idc_unlock(ha);
3082 msleep(1000);
3083 qla82xx_idc_lock(ha);
3084 drv_state = qla82xx_rd_32(ha, QLA82XX_CRB_DRV_STATE);
3085 drv_active = qla82xx_rd_32(ha, QLA82XX_CRB_DRV_ACTIVE);
Santosh Vernekar7d613ac2012-08-22 14:21:03 -04003086 if (ha->flags.nic_core_reset_owner)
Giridhar Malavali08de2842011-08-16 11:31:44 -07003087 drv_active &= active_mask;
3088 dev_state = qla82xx_rd_32(ha, QLA82XX_CRB_DEV_STATE);
Giridhar Malavalia9083012010-04-12 17:59:55 -07003089 }
3090
Giridhar Malavali08de2842011-08-16 11:31:44 -07003091 ql_dbg(ql_dbg_p3p, vha, 0xb02b,
3092 "drv_state: 0x%08x, drv_active: 0x%08x, "
3093 "dev_state: 0x%08x, active_mask: 0x%08x\n",
3094 drv_state, drv_active, dev_state, active_mask);
3095
Saurav Kashyap7c3df132011-07-14 12:00:13 -07003096 ql_log(ql_log_info, vha, 0x00b6,
3097 "Device state is 0x%x = %s.\n",
3098 dev_state,
Giridhar Malavali08de2842011-08-16 11:31:44 -07003099 dev_state < MAX_STATES ? qdev_state(dev_state) : "Unknown");
Giridhar Malavalif1af6202010-05-04 15:01:34 -07003100
Giridhar Malavalia9083012010-04-12 17:59:55 -07003101 /* Force to DEV_COLD unless someone else is starting a reset */
Santosh Vernekar7d613ac2012-08-22 14:21:03 -04003102 if (dev_state != QLA8XXX_DEV_INITIALIZING &&
3103 dev_state != QLA8XXX_DEV_COLD) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07003104 ql_log(ql_log_info, vha, 0x00b7,
3105 "HW State: COLD/RE-INIT.\n");
Santosh Vernekar7d613ac2012-08-22 14:21:03 -04003106 qla82xx_wr_32(ha, QLA82XX_CRB_DEV_STATE, QLA8XXX_DEV_COLD);
Vikas Chaudharyf4e16482012-04-25 07:26:13 -07003107 qla82xx_set_rst_ready(ha);
Giridhar Malavali08de2842011-08-16 11:31:44 -07003108 if (ql2xmdenable) {
3109 if (qla82xx_md_collect(vha))
3110 ql_log(ql_log_warn, vha, 0xb02c,
Giridhar Malavalib6d0d9d2012-05-15 14:34:25 -04003111 "Minidump not collected.\n");
Giridhar Malavali08de2842011-08-16 11:31:44 -07003112 } else
3113 ql_log(ql_log_warn, vha, 0xb04f,
3114 "Minidump disabled.\n");
Giridhar Malavalia9083012010-04-12 17:59:55 -07003115 }
3116}
3117
Giridhar Malavali31731672011-08-16 11:31:54 -07003118int
Giridhar Malavali08de2842011-08-16 11:31:44 -07003119qla82xx_check_md_needed(scsi_qla_host_t *vha)
3120{
3121 struct qla_hw_data *ha = vha->hw;
3122 uint16_t fw_major_version, fw_minor_version, fw_subminor_version;
Giridhar Malavali31731672011-08-16 11:31:54 -07003123 int rval = QLA_SUCCESS;
Giridhar Malavali08de2842011-08-16 11:31:44 -07003124
Giridhar Malavali31731672011-08-16 11:31:54 -07003125 fw_major_version = ha->fw_major_version;
3126 fw_minor_version = ha->fw_minor_version;
3127 fw_subminor_version = ha->fw_subminor_version;
Giridhar Malavali08de2842011-08-16 11:31:44 -07003128
Giridhar Malavali6246b8a2012-02-09 11:15:34 -08003129 rval = qla2x00_get_fw_version(vha);
Giridhar Malavali31731672011-08-16 11:31:54 -07003130 if (rval != QLA_SUCCESS)
3131 return rval;
3132
3133 if (ql2xmdenable) {
3134 if (!ha->fw_dumped) {
3135 if (fw_major_version != ha->fw_major_version ||
3136 fw_minor_version != ha->fw_minor_version ||
3137 fw_subminor_version != ha->fw_subminor_version) {
Giridhar Malavali31731672011-08-16 11:31:54 -07003138 ql_log(ql_log_info, vha, 0xb02d,
3139 "Firmware version differs "
3140 "Previous version: %d:%d:%d - "
3141 "New version: %d:%d:%d\n",
Giridhar Malavali9bc3bf22012-05-15 14:34:26 -04003142 fw_major_version, fw_minor_version,
3143 fw_subminor_version,
Giridhar Malavali31731672011-08-16 11:31:54 -07003144 ha->fw_major_version,
3145 ha->fw_minor_version,
Giridhar Malavali9bc3bf22012-05-15 14:34:26 -04003146 ha->fw_subminor_version);
Giridhar Malavali31731672011-08-16 11:31:54 -07003147 /* Release MiniDump resources */
3148 qla82xx_md_free(vha);
3149 /* ALlocate MiniDump resources */
3150 qla82xx_md_prep(vha);
Giridhar Malavali2e264262011-11-18 09:03:15 -08003151 }
3152 } else
3153 ql_log(ql_log_info, vha, 0xb02e,
3154 "Firmware dump available to retrieve\n");
Giridhar Malavali31731672011-08-16 11:31:54 -07003155 }
3156 return rval;
Giridhar Malavali08de2842011-08-16 11:31:44 -07003157}
3158
3159
Saurav Kashyapfa492632012-11-21 02:40:29 -05003160static int
Giridhar Malavalia9083012010-04-12 17:59:55 -07003161qla82xx_check_fw_alive(scsi_qla_host_t *vha)
3162{
Giridhar Malavali71905752011-02-23 15:27:10 -08003163 uint32_t fw_heartbeat_counter;
3164 int status = 0;
Giridhar Malavalia9083012010-04-12 17:59:55 -07003165
Giridhar Malavali71905752011-02-23 15:27:10 -08003166 fw_heartbeat_counter = qla82xx_rd_32(vha->hw,
3167 QLA82XX_PEG_ALIVE_COUNTER);
Lalit Chandivadea5b36322010-09-03 15:20:50 -07003168 /* all 0xff, assume AER/EEH in progress, ignore */
Saurav Kashyap7c3df132011-07-14 12:00:13 -07003169 if (fw_heartbeat_counter == 0xffffffff) {
3170 ql_dbg(ql_dbg_timer, vha, 0x6003,
3171 "FW heartbeat counter is 0xffffffff, "
3172 "returning status=%d.\n", status);
Giridhar Malavali71905752011-02-23 15:27:10 -08003173 return status;
Saurav Kashyap7c3df132011-07-14 12:00:13 -07003174 }
Giridhar Malavalia9083012010-04-12 17:59:55 -07003175 if (vha->fw_heartbeat_counter == fw_heartbeat_counter) {
3176 vha->seconds_since_last_heartbeat++;
3177 /* FW not alive after 2 seconds */
3178 if (vha->seconds_since_last_heartbeat == 2) {
3179 vha->seconds_since_last_heartbeat = 0;
Giridhar Malavali71905752011-02-23 15:27:10 -08003180 status = 1;
Giridhar Malavalia9083012010-04-12 17:59:55 -07003181 }
Lalit Chandivadeefa786c2010-09-03 14:57:02 -07003182 } else
3183 vha->seconds_since_last_heartbeat = 0;
Giridhar Malavalia9083012010-04-12 17:59:55 -07003184 vha->fw_heartbeat_counter = fw_heartbeat_counter;
Saurav Kashyap7c3df132011-07-14 12:00:13 -07003185 if (status)
3186 ql_dbg(ql_dbg_timer, vha, 0x6004,
3187 "Returning status=%d.\n", status);
Giridhar Malavali71905752011-02-23 15:27:10 -08003188 return status;
Giridhar Malavalia9083012010-04-12 17:59:55 -07003189}
3190
3191/*
3192 * qla82xx_device_state_handler
3193 * Main state handler
3194 *
3195 * Note:
3196 * IDC lock must be held upon entry
3197 *
3198 * Return:
3199 * Success : 0
3200 * Failed : 1
3201 */
3202int
3203qla82xx_device_state_handler(scsi_qla_host_t *vha)
3204{
3205 uint32_t dev_state;
Giridhar Malavali92dbf272011-03-30 11:46:30 -07003206 uint32_t old_dev_state;
Giridhar Malavalia9083012010-04-12 17:59:55 -07003207 int rval = QLA_SUCCESS;
3208 unsigned long dev_init_timeout;
3209 struct qla_hw_data *ha = vha->hw;
Giridhar Malavali92dbf272011-03-30 11:46:30 -07003210 int loopcount = 0;
Giridhar Malavalia9083012010-04-12 17:59:55 -07003211
3212 qla82xx_idc_lock(ha);
Saurav Kashyap0251ce82012-08-22 14:21:22 -04003213 if (!vha->flags.init_done) {
Giridhar Malavalia9083012010-04-12 17:59:55 -07003214 qla82xx_set_drv_active(vha);
Saurav Kashyap0251ce82012-08-22 14:21:22 -04003215 qla82xx_set_idc_version(vha);
3216 }
Giridhar Malavalia9083012010-04-12 17:59:55 -07003217
Giridhar Malavalif1af6202010-05-04 15:01:34 -07003218 dev_state = qla82xx_rd_32(ha, QLA82XX_CRB_DEV_STATE);
Giridhar Malavali92dbf272011-03-30 11:46:30 -07003219 old_dev_state = dev_state;
Saurav Kashyap7c3df132011-07-14 12:00:13 -07003220 ql_log(ql_log_info, vha, 0x009b,
3221 "Device state is 0x%x = %s.\n",
3222 dev_state,
Giridhar Malavali08de2842011-08-16 11:31:44 -07003223 dev_state < MAX_STATES ? qdev_state(dev_state) : "Unknown");
Giridhar Malavalia9083012010-04-12 17:59:55 -07003224
3225 /* wait for 30 seconds for device to go ready */
Santosh Vernekar7d613ac2012-08-22 14:21:03 -04003226 dev_init_timeout = jiffies + (ha->fcoe_dev_init_timeout * HZ);
Giridhar Malavalia9083012010-04-12 17:59:55 -07003227
3228 while (1) {
3229
3230 if (time_after_eq(jiffies, dev_init_timeout)) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07003231 ql_log(ql_log_fatal, vha, 0x009c,
3232 "Device init failed.\n");
Giridhar Malavalia9083012010-04-12 17:59:55 -07003233 rval = QLA_FUNCTION_FAILED;
3234 break;
3235 }
3236 dev_state = qla82xx_rd_32(ha, QLA82XX_CRB_DEV_STATE);
Giridhar Malavali92dbf272011-03-30 11:46:30 -07003237 if (old_dev_state != dev_state) {
3238 loopcount = 0;
3239 old_dev_state = dev_state;
3240 }
3241 if (loopcount < 5) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07003242 ql_log(ql_log_info, vha, 0x009d,
3243 "Device state is 0x%x = %s.\n",
3244 dev_state,
Giridhar Malavali08de2842011-08-16 11:31:44 -07003245 dev_state < MAX_STATES ? qdev_state(dev_state) :
Saurav Kashyap7c3df132011-07-14 12:00:13 -07003246 "Unknown");
Giridhar Malavali92dbf272011-03-30 11:46:30 -07003247 }
Giridhar Malavalif1af6202010-05-04 15:01:34 -07003248
Giridhar Malavalia9083012010-04-12 17:59:55 -07003249 switch (dev_state) {
Santosh Vernekar7d613ac2012-08-22 14:21:03 -04003250 case QLA8XXX_DEV_READY:
3251 ha->flags.nic_core_reset_owner = 0;
Chad Dupuis7916bb92012-08-22 14:20:57 -04003252 goto rel_lock;
Santosh Vernekar7d613ac2012-08-22 14:21:03 -04003253 case QLA8XXX_DEV_COLD:
Giridhar Malavalia9083012010-04-12 17:59:55 -07003254 rval = qla82xx_device_bootstrap(vha);
Giridhar Malavali08de2842011-08-16 11:31:44 -07003255 break;
Santosh Vernekar7d613ac2012-08-22 14:21:03 -04003256 case QLA8XXX_DEV_INITIALIZING:
Giridhar Malavalia9083012010-04-12 17:59:55 -07003257 qla82xx_idc_unlock(ha);
3258 msleep(1000);
3259 qla82xx_idc_lock(ha);
3260 break;
Santosh Vernekar7d613ac2012-08-22 14:21:03 -04003261 case QLA8XXX_DEV_NEED_RESET:
Saurav Kashyapc8582ad2011-08-16 11:31:46 -07003262 if (!ql2xdontresethba)
3263 qla82xx_need_reset_handler(vha);
3264 else {
3265 qla82xx_idc_unlock(ha);
3266 msleep(1000);
3267 qla82xx_idc_lock(ha);
3268 }
Giridhar Malavali0060ddf2011-02-23 15:27:08 -08003269 dev_init_timeout = jiffies +
Santosh Vernekar7d613ac2012-08-22 14:21:03 -04003270 (ha->fcoe_dev_init_timeout * HZ);
Giridhar Malavalia9083012010-04-12 17:59:55 -07003271 break;
Santosh Vernekar7d613ac2012-08-22 14:21:03 -04003272 case QLA8XXX_DEV_NEED_QUIESCENT:
Saurav Kashyap579d12b2010-12-21 16:00:14 -08003273 qla82xx_need_qsnt_handler(vha);
3274 /* Reset timeout value after quiescence handler */
Santosh Vernekar7d613ac2012-08-22 14:21:03 -04003275 dev_init_timeout = jiffies + (ha->fcoe_dev_init_timeout\
Saurav Kashyap579d12b2010-12-21 16:00:14 -08003276 * HZ);
3277 break;
Santosh Vernekar7d613ac2012-08-22 14:21:03 -04003278 case QLA8XXX_DEV_QUIESCENT:
Saurav Kashyap579d12b2010-12-21 16:00:14 -08003279 /* Owner will exit and other will wait for the state
3280 * to get changed
3281 */
3282 if (ha->flags.quiesce_owner)
Chad Dupuis7916bb92012-08-22 14:20:57 -04003283 goto rel_lock;
Saurav Kashyap579d12b2010-12-21 16:00:14 -08003284
Giridhar Malavalia9083012010-04-12 17:59:55 -07003285 qla82xx_idc_unlock(ha);
3286 msleep(1000);
3287 qla82xx_idc_lock(ha);
Saurav Kashyap579d12b2010-12-21 16:00:14 -08003288
3289 /* Reset timeout value after quiescence handler */
Santosh Vernekar7d613ac2012-08-22 14:21:03 -04003290 dev_init_timeout = jiffies + (ha->fcoe_dev_init_timeout\
Saurav Kashyap579d12b2010-12-21 16:00:14 -08003291 * HZ);
Giridhar Malavalia9083012010-04-12 17:59:55 -07003292 break;
Santosh Vernekar7d613ac2012-08-22 14:21:03 -04003293 case QLA8XXX_DEV_FAILED:
3294 qla8xxx_dev_failed_handler(vha);
Giridhar Malavalia9083012010-04-12 17:59:55 -07003295 rval = QLA_FUNCTION_FAILED;
3296 goto exit;
3297 default:
3298 qla82xx_idc_unlock(ha);
3299 msleep(1000);
3300 qla82xx_idc_lock(ha);
3301 }
Giridhar Malavali92dbf272011-03-30 11:46:30 -07003302 loopcount++;
Giridhar Malavalia9083012010-04-12 17:59:55 -07003303 }
Chad Dupuis7916bb92012-08-22 14:20:57 -04003304rel_lock:
Giridhar Malavalia9083012010-04-12 17:59:55 -07003305 qla82xx_idc_unlock(ha);
Chad Dupuis7916bb92012-08-22 14:20:57 -04003306exit:
Giridhar Malavalia9083012010-04-12 17:59:55 -07003307 return rval;
3308}
3309
Giridhar Malavali5988aeb2012-05-15 14:34:12 -04003310static int qla82xx_check_temp(scsi_qla_host_t *vha)
3311{
3312 uint32_t temp, temp_state, temp_val;
3313 struct qla_hw_data *ha = vha->hw;
3314
3315 temp = qla82xx_rd_32(ha, CRB_TEMP_STATE);
3316 temp_state = qla82xx_get_temp_state(temp);
3317 temp_val = qla82xx_get_temp_val(temp);
3318
3319 if (temp_state == QLA82XX_TEMP_PANIC) {
3320 ql_log(ql_log_warn, vha, 0x600e,
3321 "Device temperature %d degrees C exceeds "
3322 " maximum allowed. Hardware has been shut down.\n",
3323 temp_val);
3324 return 1;
3325 } else if (temp_state == QLA82XX_TEMP_WARN) {
3326 ql_log(ql_log_warn, vha, 0x600f,
3327 "Device temperature %d degrees C exceeds "
3328 "operating range. Immediate action needed.\n",
3329 temp_val);
3330 }
3331 return 0;
3332}
3333
Chad Dupuisc8f65442011-11-18 09:02:17 -08003334void qla82xx_clear_pending_mbx(scsi_qla_host_t *vha)
3335{
3336 struct qla_hw_data *ha = vha->hw;
3337
3338 if (ha->flags.mbox_busy) {
3339 ha->flags.mbox_int = 1;
Giridhar Malavali8937f2f2011-11-18 09:02:18 -08003340 ha->flags.mbox_busy = 0;
Chad Dupuisc8f65442011-11-18 09:02:17 -08003341 ql_log(ql_log_warn, vha, 0x6010,
3342 "Doing premature completion of mbx command.\n");
3343 if (test_bit(MBX_INTR_WAIT, &ha->mbx_cmd_flags))
3344 complete(&ha->mbx_intr_comp);
3345 }
3346}
3347
Giridhar Malavalia9083012010-04-12 17:59:55 -07003348void qla82xx_watchdog(scsi_qla_host_t *vha)
3349{
Giridhar Malavali71905752011-02-23 15:27:10 -08003350 uint32_t dev_state, halt_status;
Giridhar Malavalia9083012010-04-12 17:59:55 -07003351 struct qla_hw_data *ha = vha->hw;
3352
Giridhar Malavalia9083012010-04-12 17:59:55 -07003353 /* don't poll if reset is going on */
Santosh Vernekar7d613ac2012-08-22 14:21:03 -04003354 if (!ha->flags.nic_core_reset_hdlr_active) {
Giridhar Malavali71905752011-02-23 15:27:10 -08003355 dev_state = qla82xx_rd_32(ha, QLA82XX_CRB_DEV_STATE);
Giridhar Malavali5988aeb2012-05-15 14:34:12 -04003356 if (qla82xx_check_temp(vha)) {
3357 set_bit(ISP_UNRECOVERABLE, &vha->dpc_flags);
3358 ha->flags.isp82xx_fw_hung = 1;
3359 qla82xx_clear_pending_mbx(vha);
Santosh Vernekar7d613ac2012-08-22 14:21:03 -04003360 } else if (dev_state == QLA8XXX_DEV_NEED_RESET &&
Giridhar Malavali71905752011-02-23 15:27:10 -08003361 !test_bit(ISP_ABORT_NEEDED, &vha->dpc_flags)) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07003362 ql_log(ql_log_warn, vha, 0x6001,
3363 "Adapter reset needed.\n");
Giridhar Malavalia9083012010-04-12 17:59:55 -07003364 set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
Santosh Vernekar7d613ac2012-08-22 14:21:03 -04003365 } else if (dev_state == QLA8XXX_DEV_NEED_QUIESCENT &&
Saurav Kashyap579d12b2010-12-21 16:00:14 -08003366 !test_bit(ISP_QUIESCE_NEEDED, &vha->dpc_flags)) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07003367 ql_log(ql_log_warn, vha, 0x6002,
3368 "Quiescent needed.\n");
Saurav Kashyap579d12b2010-12-21 16:00:14 -08003369 set_bit(ISP_QUIESCE_NEEDED, &vha->dpc_flags);
Santosh Vernekar7d613ac2012-08-22 14:21:03 -04003370 } else if (dev_state == QLA8XXX_DEV_FAILED &&
Chad Dupuis7916bb92012-08-22 14:20:57 -04003371 !test_bit(ISP_UNRECOVERABLE, &vha->dpc_flags) &&
3372 vha->flags.online == 1) {
3373 ql_log(ql_log_warn, vha, 0xb055,
3374 "Adapter state is failed. Offlining.\n");
3375 set_bit(ISP_UNRECOVERABLE, &vha->dpc_flags);
3376 ha->flags.isp82xx_fw_hung = 1;
3377 qla82xx_clear_pending_mbx(vha);
Giridhar Malavalia9083012010-04-12 17:59:55 -07003378 } else {
Giridhar Malavali71905752011-02-23 15:27:10 -08003379 if (qla82xx_check_fw_alive(vha)) {
Giridhar Malavali63154912011-11-18 09:02:19 -08003380 ql_dbg(ql_dbg_timer, vha, 0x6011,
3381 "disabling pause transmit on port 0 & 1.\n");
3382 qla82xx_wr_32(ha, QLA82XX_CRB_NIU + 0x98,
3383 CRB_NIU_XG_PAUSE_CTL_P0|CRB_NIU_XG_PAUSE_CTL_P1);
Giridhar Malavali71905752011-02-23 15:27:10 -08003384 halt_status = qla82xx_rd_32(ha,
3385 QLA82XX_PEG_HALT_STATUS1);
Giridhar Malavali63154912011-11-18 09:02:19 -08003386 ql_log(ql_log_info, vha, 0x6005,
Saurav Kashyap7c3df132011-07-14 12:00:13 -07003387 "dumping hw/fw registers:.\n "
3388 " PEG_HALT_STATUS1: 0x%x, PEG_HALT_STATUS2: 0x%x,.\n "
3389 " PEG_NET_0_PC: 0x%x, PEG_NET_1_PC: 0x%x,.\n "
3390 " PEG_NET_2_PC: 0x%x, PEG_NET_3_PC: 0x%x,.\n "
3391 " PEG_NET_4_PC: 0x%x.\n", halt_status,
Giridhar Malavali0e8edb02011-03-30 11:46:28 -07003392 qla82xx_rd_32(ha, QLA82XX_PEG_HALT_STATUS2),
3393 qla82xx_rd_32(ha,
3394 QLA82XX_CRB_PEG_NET_0 + 0x3c),
3395 qla82xx_rd_32(ha,
3396 QLA82XX_CRB_PEG_NET_1 + 0x3c),
3397 qla82xx_rd_32(ha,
3398 QLA82XX_CRB_PEG_NET_2 + 0x3c),
3399 qla82xx_rd_32(ha,
3400 QLA82XX_CRB_PEG_NET_3 + 0x3c),
3401 qla82xx_rd_32(ha,
3402 QLA82XX_CRB_PEG_NET_4 + 0x3c));
Giridhar Malavali2cc97962012-02-09 11:14:12 -08003403 if (((halt_status & 0x1fffff00) >> 8) == 0x67)
Chad Dupuis10a340e2011-11-18 09:02:16 -08003404 ql_log(ql_log_warn, vha, 0xb052,
3405 "Firmware aborted with "
3406 "error code 0x00006700. Device is "
3407 "being reset.\n");
Giridhar Malavali71905752011-02-23 15:27:10 -08003408 if (halt_status & HALT_STATUS_UNRECOVERABLE) {
3409 set_bit(ISP_UNRECOVERABLE,
3410 &vha->dpc_flags);
3411 } else {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07003412 ql_log(ql_log_info, vha, 0x6006,
3413 "Detect abort needed.\n");
Giridhar Malavali71905752011-02-23 15:27:10 -08003414 set_bit(ISP_ABORT_NEEDED,
3415 &vha->dpc_flags);
3416 }
Giridhar Malavali71905752011-02-23 15:27:10 -08003417 ha->flags.isp82xx_fw_hung = 1;
Chad Dupuisc8f65442011-11-18 09:02:17 -08003418 ql_log(ql_log_warn, vha, 0x6007, "Firmware hung.\n");
3419 qla82xx_clear_pending_mbx(vha);
Giridhar Malavali71905752011-02-23 15:27:10 -08003420 }
Giridhar Malavalia9083012010-04-12 17:59:55 -07003421 }
3422 }
3423}
3424
3425int qla82xx_load_risc(scsi_qla_host_t *vha, uint32_t *srisc_addr)
3426{
3427 int rval;
3428 rval = qla82xx_device_state_handler(vha);
3429 return rval;
3430}
3431
Giridhar Malavali08de2842011-08-16 11:31:44 -07003432void
3433qla82xx_set_reset_owner(scsi_qla_host_t *vha)
3434{
3435 struct qla_hw_data *ha = vha->hw;
3436 uint32_t dev_state;
3437
3438 dev_state = qla82xx_rd_32(ha, QLA82XX_CRB_DEV_STATE);
Santosh Vernekar7d613ac2012-08-22 14:21:03 -04003439 if (dev_state == QLA8XXX_DEV_READY) {
Giridhar Malavali08de2842011-08-16 11:31:44 -07003440 ql_log(ql_log_info, vha, 0xb02f,
3441 "HW State: NEED RESET\n");
3442 qla82xx_wr_32(ha, QLA82XX_CRB_DEV_STATE,
Santosh Vernekar7d613ac2012-08-22 14:21:03 -04003443 QLA8XXX_DEV_NEED_RESET);
3444 ha->flags.nic_core_reset_owner = 1;
Giridhar Malavali08de2842011-08-16 11:31:44 -07003445 ql_dbg(ql_dbg_p3p, vha, 0xb030,
3446 "reset_owner is 0x%x\n", ha->portnum);
3447 } else
3448 ql_log(ql_log_info, vha, 0xb031,
3449 "Device state is 0x%x = %s.\n",
3450 dev_state,
3451 dev_state < MAX_STATES ? qdev_state(dev_state) : "Unknown");
3452}
3453
Giridhar Malavalia9083012010-04-12 17:59:55 -07003454/*
3455 * qla82xx_abort_isp
3456 * Resets ISP and aborts all outstanding commands.
3457 *
3458 * Input:
3459 * ha = adapter block pointer.
3460 *
3461 * Returns:
3462 * 0 = success
3463 */
3464int
3465qla82xx_abort_isp(scsi_qla_host_t *vha)
3466{
3467 int rval;
3468 struct qla_hw_data *ha = vha->hw;
Giridhar Malavalia9083012010-04-12 17:59:55 -07003469
3470 if (vha->device_flags & DFLG_DEV_FAILED) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07003471 ql_log(ql_log_warn, vha, 0x8024,
3472 "Device in failed state, exiting.\n");
Giridhar Malavalia9083012010-04-12 17:59:55 -07003473 return QLA_SUCCESS;
3474 }
Santosh Vernekar7d613ac2012-08-22 14:21:03 -04003475 ha->flags.nic_core_reset_hdlr_active = 1;
Giridhar Malavalia9083012010-04-12 17:59:55 -07003476
3477 qla82xx_idc_lock(ha);
Giridhar Malavali08de2842011-08-16 11:31:44 -07003478 qla82xx_set_reset_owner(vha);
Giridhar Malavalia9083012010-04-12 17:59:55 -07003479 qla82xx_idc_unlock(ha);
3480
3481 rval = qla82xx_device_state_handler(vha);
3482
3483 qla82xx_idc_lock(ha);
3484 qla82xx_clear_rst_ready(ha);
3485 qla82xx_idc_unlock(ha);
3486
Santosh Vernekarcdbb0a4f2010-05-28 15:08:25 -07003487 if (rval == QLA_SUCCESS) {
Giridhar Malavali71905752011-02-23 15:27:10 -08003488 ha->flags.isp82xx_fw_hung = 0;
Santosh Vernekar7d613ac2012-08-22 14:21:03 -04003489 ha->flags.nic_core_reset_hdlr_active = 0;
Giridhar Malavalia9083012010-04-12 17:59:55 -07003490 qla82xx_restart_isp(vha);
Santosh Vernekarcdbb0a4f2010-05-28 15:08:25 -07003491 }
Giridhar Malavalif1af6202010-05-04 15:01:34 -07003492
3493 if (rval) {
3494 vha->flags.online = 1;
3495 if (test_bit(ISP_ABORT_RETRY, &vha->dpc_flags)) {
3496 if (ha->isp_abort_cnt == 0) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07003497 ql_log(ql_log_warn, vha, 0x8027,
3498 "ISP error recover failed - board "
3499 "disabled.\n");
Giridhar Malavalif1af6202010-05-04 15:01:34 -07003500 /*
3501 * The next call disables the board
3502 * completely.
3503 */
3504 ha->isp_ops->reset_adapter(vha);
3505 vha->flags.online = 0;
3506 clear_bit(ISP_ABORT_RETRY,
3507 &vha->dpc_flags);
3508 rval = QLA_SUCCESS;
3509 } else { /* schedule another ISP abort */
3510 ha->isp_abort_cnt--;
Saurav Kashyap7c3df132011-07-14 12:00:13 -07003511 ql_log(ql_log_warn, vha, 0x8036,
3512 "ISP abort - retry remaining %d.\n",
3513 ha->isp_abort_cnt);
Giridhar Malavalif1af6202010-05-04 15:01:34 -07003514 rval = QLA_FUNCTION_FAILED;
3515 }
3516 } else {
3517 ha->isp_abort_cnt = MAX_RETRIES_OF_ISP_ABORT;
Saurav Kashyap7c3df132011-07-14 12:00:13 -07003518 ql_dbg(ql_dbg_taskm, vha, 0x8029,
3519 "ISP error recovery - retrying (%d) more times.\n",
3520 ha->isp_abort_cnt);
Giridhar Malavalif1af6202010-05-04 15:01:34 -07003521 set_bit(ISP_ABORT_RETRY, &vha->dpc_flags);
3522 rval = QLA_FUNCTION_FAILED;
3523 }
3524 }
Giridhar Malavalia9083012010-04-12 17:59:55 -07003525 return rval;
3526}
3527
3528/*
3529 * qla82xx_fcoe_ctx_reset
3530 * Perform a quick reset and aborts all outstanding commands.
3531 * This will only perform an FCoE context reset and avoids a full blown
3532 * chip reset.
3533 *
3534 * Input:
3535 * ha = adapter block pointer.
3536 * is_reset_path = flag for identifying the reset path.
3537 *
3538 * Returns:
3539 * 0 = success
3540 */
3541int qla82xx_fcoe_ctx_reset(scsi_qla_host_t *vha)
3542{
3543 int rval = QLA_FUNCTION_FAILED;
3544
3545 if (vha->flags.online) {
3546 /* Abort all outstanding commands, so as to be requeued later */
3547 qla2x00_abort_isp_cleanup(vha);
3548 }
3549
3550 /* Stop currently executing firmware.
3551 * This will destroy existing FCoE context at the F/W end.
3552 */
3553 qla2x00_try_to_stop_firmware(vha);
3554
3555 /* Restart. Creates a new FCoE context on INIT_FIRMWARE. */
3556 rval = qla82xx_restart_isp(vha);
3557
3558 return rval;
3559}
3560
3561/*
3562 * qla2x00_wait_for_fcoe_ctx_reset
3563 * Wait till the FCoE context is reset.
3564 *
3565 * Note:
3566 * Does context switching here.
3567 * Release SPIN_LOCK (if any) before calling this routine.
3568 *
3569 * Return:
3570 * Success (fcoe_ctx reset is done) : 0
3571 * Failed (fcoe_ctx reset not completed within max loop timout ) : 1
3572 */
3573int qla2x00_wait_for_fcoe_ctx_reset(scsi_qla_host_t *vha)
3574{
3575 int status = QLA_FUNCTION_FAILED;
3576 unsigned long wait_reset;
3577
3578 wait_reset = jiffies + (MAX_LOOP_TIMEOUT * HZ);
3579 while ((test_bit(FCOE_CTX_RESET_NEEDED, &vha->dpc_flags) ||
3580 test_bit(ABORT_ISP_ACTIVE, &vha->dpc_flags))
3581 && time_before(jiffies, wait_reset)) {
3582
3583 set_current_state(TASK_UNINTERRUPTIBLE);
3584 schedule_timeout(HZ);
3585
3586 if (!test_bit(FCOE_CTX_RESET_NEEDED, &vha->dpc_flags) &&
3587 !test_bit(ABORT_ISP_ACTIVE, &vha->dpc_flags)) {
3588 status = QLA_SUCCESS;
3589 break;
3590 }
3591 }
Saurav Kashyap7c3df132011-07-14 12:00:13 -07003592 ql_dbg(ql_dbg_p3p, vha, 0xb027,
Joe Perchesd8424f62011-11-18 09:03:06 -08003593 "%s: status=%d.\n", __func__, status);
Giridhar Malavalia9083012010-04-12 17:59:55 -07003594
3595 return status;
3596}
Giridhar Malavali71905752011-02-23 15:27:10 -08003597
3598void
3599qla82xx_chip_reset_cleanup(scsi_qla_host_t *vha)
3600{
3601 int i;
3602 unsigned long flags;
3603 struct qla_hw_data *ha = vha->hw;
3604
3605 /* Check if 82XX firmware is alive or not
3606 * We may have arrived here from NEED_RESET
3607 * detection only
3608 */
3609 if (!ha->flags.isp82xx_fw_hung) {
3610 for (i = 0; i < 2; i++) {
3611 msleep(1000);
3612 if (qla82xx_check_fw_alive(vha)) {
3613 ha->flags.isp82xx_fw_hung = 1;
Chad Dupuisc8f65442011-11-18 09:02:17 -08003614 qla82xx_clear_pending_mbx(vha);
Giridhar Malavali71905752011-02-23 15:27:10 -08003615 break;
3616 }
3617 }
3618 }
Saurav Kashyap7c3df132011-07-14 12:00:13 -07003619 ql_dbg(ql_dbg_init, vha, 0x00b0,
3620 "Entered %s fw_hung=%d.\n",
3621 __func__, ha->flags.isp82xx_fw_hung);
Giridhar Malavali71905752011-02-23 15:27:10 -08003622
3623 /* Abort all commands gracefully if fw NOT hung */
3624 if (!ha->flags.isp82xx_fw_hung) {
3625 int cnt, que;
3626 srb_t *sp;
3627 struct req_que *req;
3628
3629 spin_lock_irqsave(&ha->hardware_lock, flags);
3630 for (que = 0; que < ha->max_req_queues; que++) {
3631 req = ha->req_q_map[que];
3632 if (!req)
3633 continue;
3634 for (cnt = 1; cnt < MAX_OUTSTANDING_COMMANDS; cnt++) {
3635 sp = req->outstanding_cmds[cnt];
3636 if (sp) {
Giridhar Malavali9ba56b92012-02-09 11:15:36 -08003637 if (!sp->u.scmd.ctx ||
Giridhar Malavali71905752011-02-23 15:27:10 -08003638 (sp->flags & SRB_FCP_CMND_DMA_VALID)) {
3639 spin_unlock_irqrestore(
3640 &ha->hardware_lock, flags);
3641 if (ha->isp_ops->abort_command(sp)) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07003642 ql_log(ql_log_info, vha,
3643 0x00b1,
3644 "mbx abort failed.\n");
Giridhar Malavali71905752011-02-23 15:27:10 -08003645 } else {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07003646 ql_log(ql_log_info, vha,
3647 0x00b2,
3648 "mbx abort success.\n");
Giridhar Malavali71905752011-02-23 15:27:10 -08003649 }
3650 spin_lock_irqsave(&ha->hardware_lock, flags);
3651 }
3652 }
3653 }
3654 }
3655 spin_unlock_irqrestore(&ha->hardware_lock, flags);
3656
3657 /* Wait for pending cmds (physical and virtual) to complete */
3658 if (!qla2x00_eh_wait_for_pending_commands(vha, 0, 0,
3659 WAIT_HOST) == QLA_SUCCESS) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07003660 ql_dbg(ql_dbg_init, vha, 0x00b3,
3661 "Done wait for "
3662 "pending commands.\n");
Giridhar Malavali71905752011-02-23 15:27:10 -08003663 }
3664 }
3665}
Giridhar Malavali08de2842011-08-16 11:31:44 -07003666
3667/* Minidump related functions */
Giridhar Malavali08de2842011-08-16 11:31:44 -07003668static int
3669qla82xx_minidump_process_control(scsi_qla_host_t *vha,
3670 qla82xx_md_entry_hdr_t *entry_hdr, uint32_t **d_ptr)
3671{
3672 struct qla_hw_data *ha = vha->hw;
3673 struct qla82xx_md_entry_crb *crb_entry;
3674 uint32_t read_value, opcode, poll_time;
3675 uint32_t addr, index, crb_addr;
3676 unsigned long wtime;
3677 struct qla82xx_md_template_hdr *tmplt_hdr;
3678 uint32_t rval = QLA_SUCCESS;
3679 int i;
3680
3681 tmplt_hdr = (struct qla82xx_md_template_hdr *)ha->md_tmplt_hdr;
3682 crb_entry = (struct qla82xx_md_entry_crb *)entry_hdr;
3683 crb_addr = crb_entry->addr;
3684
3685 for (i = 0; i < crb_entry->op_count; i++) {
3686 opcode = crb_entry->crb_ctrl.opcode;
3687 if (opcode & QLA82XX_DBG_OPCODE_WR) {
3688 qla82xx_md_rw_32(ha, crb_addr,
3689 crb_entry->value_1, 1);
3690 opcode &= ~QLA82XX_DBG_OPCODE_WR;
3691 }
3692
3693 if (opcode & QLA82XX_DBG_OPCODE_RW) {
3694 read_value = qla82xx_md_rw_32(ha, crb_addr, 0, 0);
3695 qla82xx_md_rw_32(ha, crb_addr, read_value, 1);
3696 opcode &= ~QLA82XX_DBG_OPCODE_RW;
3697 }
3698
3699 if (opcode & QLA82XX_DBG_OPCODE_AND) {
3700 read_value = qla82xx_md_rw_32(ha, crb_addr, 0, 0);
3701 read_value &= crb_entry->value_2;
3702 opcode &= ~QLA82XX_DBG_OPCODE_AND;
3703 if (opcode & QLA82XX_DBG_OPCODE_OR) {
3704 read_value |= crb_entry->value_3;
3705 opcode &= ~QLA82XX_DBG_OPCODE_OR;
3706 }
3707 qla82xx_md_rw_32(ha, crb_addr, read_value, 1);
3708 }
3709
3710 if (opcode & QLA82XX_DBG_OPCODE_OR) {
3711 read_value = qla82xx_md_rw_32(ha, crb_addr, 0, 0);
3712 read_value |= crb_entry->value_3;
3713 qla82xx_md_rw_32(ha, crb_addr, read_value, 1);
3714 opcode &= ~QLA82XX_DBG_OPCODE_OR;
3715 }
3716
3717 if (opcode & QLA82XX_DBG_OPCODE_POLL) {
3718 poll_time = crb_entry->crb_strd.poll_timeout;
3719 wtime = jiffies + poll_time;
3720 read_value = qla82xx_md_rw_32(ha, crb_addr, 0, 0);
3721
3722 do {
3723 if ((read_value & crb_entry->value_2)
3724 == crb_entry->value_1)
3725 break;
3726 else if (time_after_eq(jiffies, wtime)) {
3727 /* capturing dump failed */
3728 rval = QLA_FUNCTION_FAILED;
3729 break;
3730 } else
3731 read_value = qla82xx_md_rw_32(ha,
3732 crb_addr, 0, 0);
3733 } while (1);
3734 opcode &= ~QLA82XX_DBG_OPCODE_POLL;
3735 }
3736
3737 if (opcode & QLA82XX_DBG_OPCODE_RDSTATE) {
3738 if (crb_entry->crb_strd.state_index_a) {
3739 index = crb_entry->crb_strd.state_index_a;
3740 addr = tmplt_hdr->saved_state_array[index];
3741 } else
3742 addr = crb_addr;
3743
3744 read_value = qla82xx_md_rw_32(ha, addr, 0, 0);
3745 index = crb_entry->crb_ctrl.state_index_v;
3746 tmplt_hdr->saved_state_array[index] = read_value;
3747 opcode &= ~QLA82XX_DBG_OPCODE_RDSTATE;
3748 }
3749
3750 if (opcode & QLA82XX_DBG_OPCODE_WRSTATE) {
3751 if (crb_entry->crb_strd.state_index_a) {
3752 index = crb_entry->crb_strd.state_index_a;
3753 addr = tmplt_hdr->saved_state_array[index];
3754 } else
3755 addr = crb_addr;
3756
3757 if (crb_entry->crb_ctrl.state_index_v) {
3758 index = crb_entry->crb_ctrl.state_index_v;
3759 read_value =
3760 tmplt_hdr->saved_state_array[index];
3761 } else
3762 read_value = crb_entry->value_1;
3763
3764 qla82xx_md_rw_32(ha, addr, read_value, 1);
3765 opcode &= ~QLA82XX_DBG_OPCODE_WRSTATE;
3766 }
3767
3768 if (opcode & QLA82XX_DBG_OPCODE_MDSTATE) {
3769 index = crb_entry->crb_ctrl.state_index_v;
3770 read_value = tmplt_hdr->saved_state_array[index];
3771 read_value <<= crb_entry->crb_ctrl.shl;
3772 read_value >>= crb_entry->crb_ctrl.shr;
3773 if (crb_entry->value_2)
3774 read_value &= crb_entry->value_2;
3775 read_value |= crb_entry->value_3;
3776 read_value += crb_entry->value_1;
3777 tmplt_hdr->saved_state_array[index] = read_value;
3778 opcode &= ~QLA82XX_DBG_OPCODE_MDSTATE;
3779 }
3780 crb_addr += crb_entry->crb_strd.addr_stride;
3781 }
3782 return rval;
3783}
3784
3785static void
3786qla82xx_minidump_process_rdocm(scsi_qla_host_t *vha,
3787 qla82xx_md_entry_hdr_t *entry_hdr, uint32_t **d_ptr)
3788{
3789 struct qla_hw_data *ha = vha->hw;
3790 uint32_t r_addr, r_stride, loop_cnt, i, r_value;
3791 struct qla82xx_md_entry_rdocm *ocm_hdr;
3792 uint32_t *data_ptr = *d_ptr;
3793
3794 ocm_hdr = (struct qla82xx_md_entry_rdocm *)entry_hdr;
3795 r_addr = ocm_hdr->read_addr;
3796 r_stride = ocm_hdr->read_addr_stride;
3797 loop_cnt = ocm_hdr->op_count;
3798
3799 for (i = 0; i < loop_cnt; i++) {
Saurav Kashyapfa492632012-11-21 02:40:29 -05003800 r_value = RD_REG_DWORD((void __iomem *)
3801 (r_addr + ha->nx_pcibase));
Giridhar Malavali08de2842011-08-16 11:31:44 -07003802 *data_ptr++ = cpu_to_le32(r_value);
3803 r_addr += r_stride;
3804 }
3805 *d_ptr = data_ptr;
3806}
3807
3808static void
3809qla82xx_minidump_process_rdmux(scsi_qla_host_t *vha,
3810 qla82xx_md_entry_hdr_t *entry_hdr, uint32_t **d_ptr)
3811{
3812 struct qla_hw_data *ha = vha->hw;
3813 uint32_t r_addr, s_stride, s_addr, s_value, loop_cnt, i, r_value;
3814 struct qla82xx_md_entry_mux *mux_hdr;
3815 uint32_t *data_ptr = *d_ptr;
3816
3817 mux_hdr = (struct qla82xx_md_entry_mux *)entry_hdr;
3818 r_addr = mux_hdr->read_addr;
3819 s_addr = mux_hdr->select_addr;
3820 s_stride = mux_hdr->select_value_stride;
3821 s_value = mux_hdr->select_value;
3822 loop_cnt = mux_hdr->op_count;
3823
3824 for (i = 0; i < loop_cnt; i++) {
3825 qla82xx_md_rw_32(ha, s_addr, s_value, 1);
3826 r_value = qla82xx_md_rw_32(ha, r_addr, 0, 0);
3827 *data_ptr++ = cpu_to_le32(s_value);
3828 *data_ptr++ = cpu_to_le32(r_value);
3829 s_value += s_stride;
3830 }
3831 *d_ptr = data_ptr;
3832}
3833
3834static void
3835qla82xx_minidump_process_rdcrb(scsi_qla_host_t *vha,
3836 qla82xx_md_entry_hdr_t *entry_hdr, uint32_t **d_ptr)
3837{
3838 struct qla_hw_data *ha = vha->hw;
3839 uint32_t r_addr, r_stride, loop_cnt, i, r_value;
3840 struct qla82xx_md_entry_crb *crb_hdr;
3841 uint32_t *data_ptr = *d_ptr;
3842
3843 crb_hdr = (struct qla82xx_md_entry_crb *)entry_hdr;
3844 r_addr = crb_hdr->addr;
3845 r_stride = crb_hdr->crb_strd.addr_stride;
3846 loop_cnt = crb_hdr->op_count;
3847
3848 for (i = 0; i < loop_cnt; i++) {
3849 r_value = qla82xx_md_rw_32(ha, r_addr, 0, 0);
3850 *data_ptr++ = cpu_to_le32(r_addr);
3851 *data_ptr++ = cpu_to_le32(r_value);
3852 r_addr += r_stride;
3853 }
3854 *d_ptr = data_ptr;
3855}
3856
3857static int
3858qla82xx_minidump_process_l2tag(scsi_qla_host_t *vha,
3859 qla82xx_md_entry_hdr_t *entry_hdr, uint32_t **d_ptr)
3860{
3861 struct qla_hw_data *ha = vha->hw;
3862 uint32_t addr, r_addr, c_addr, t_r_addr;
3863 uint32_t i, k, loop_count, t_value, r_cnt, r_value;
3864 unsigned long p_wait, w_time, p_mask;
3865 uint32_t c_value_w, c_value_r;
3866 struct qla82xx_md_entry_cache *cache_hdr;
3867 int rval = QLA_FUNCTION_FAILED;
3868 uint32_t *data_ptr = *d_ptr;
3869
3870 cache_hdr = (struct qla82xx_md_entry_cache *)entry_hdr;
3871 loop_count = cache_hdr->op_count;
3872 r_addr = cache_hdr->read_addr;
3873 c_addr = cache_hdr->control_addr;
3874 c_value_w = cache_hdr->cache_ctrl.write_value;
3875
3876 t_r_addr = cache_hdr->tag_reg_addr;
3877 t_value = cache_hdr->addr_ctrl.init_tag_value;
3878 r_cnt = cache_hdr->read_ctrl.read_addr_cnt;
3879 p_wait = cache_hdr->cache_ctrl.poll_wait;
3880 p_mask = cache_hdr->cache_ctrl.poll_mask;
3881
3882 for (i = 0; i < loop_count; i++) {
3883 qla82xx_md_rw_32(ha, t_r_addr, t_value, 1);
3884 if (c_value_w)
3885 qla82xx_md_rw_32(ha, c_addr, c_value_w, 1);
3886
3887 if (p_mask) {
3888 w_time = jiffies + p_wait;
3889 do {
3890 c_value_r = qla82xx_md_rw_32(ha, c_addr, 0, 0);
3891 if ((c_value_r & p_mask) == 0)
3892 break;
3893 else if (time_after_eq(jiffies, w_time)) {
3894 /* capturing dump failed */
3895 ql_dbg(ql_dbg_p3p, vha, 0xb032,
3896 "c_value_r: 0x%x, poll_mask: 0x%lx, "
3897 "w_time: 0x%lx\n",
3898 c_value_r, p_mask, w_time);
3899 return rval;
3900 }
3901 } while (1);
3902 }
3903
3904 addr = r_addr;
3905 for (k = 0; k < r_cnt; k++) {
3906 r_value = qla82xx_md_rw_32(ha, addr, 0, 0);
3907 *data_ptr++ = cpu_to_le32(r_value);
3908 addr += cache_hdr->read_ctrl.read_addr_stride;
3909 }
3910 t_value += cache_hdr->addr_ctrl.tag_value_stride;
3911 }
3912 *d_ptr = data_ptr;
3913 return QLA_SUCCESS;
3914}
3915
3916static void
3917qla82xx_minidump_process_l1cache(scsi_qla_host_t *vha,
3918 qla82xx_md_entry_hdr_t *entry_hdr, uint32_t **d_ptr)
3919{
3920 struct qla_hw_data *ha = vha->hw;
3921 uint32_t addr, r_addr, c_addr, t_r_addr;
3922 uint32_t i, k, loop_count, t_value, r_cnt, r_value;
3923 uint32_t c_value_w;
3924 struct qla82xx_md_entry_cache *cache_hdr;
3925 uint32_t *data_ptr = *d_ptr;
3926
3927 cache_hdr = (struct qla82xx_md_entry_cache *)entry_hdr;
3928 loop_count = cache_hdr->op_count;
3929 r_addr = cache_hdr->read_addr;
3930 c_addr = cache_hdr->control_addr;
3931 c_value_w = cache_hdr->cache_ctrl.write_value;
3932
3933 t_r_addr = cache_hdr->tag_reg_addr;
3934 t_value = cache_hdr->addr_ctrl.init_tag_value;
3935 r_cnt = cache_hdr->read_ctrl.read_addr_cnt;
3936
3937 for (i = 0; i < loop_count; i++) {
3938 qla82xx_md_rw_32(ha, t_r_addr, t_value, 1);
3939 qla82xx_md_rw_32(ha, c_addr, c_value_w, 1);
3940 addr = r_addr;
3941 for (k = 0; k < r_cnt; k++) {
3942 r_value = qla82xx_md_rw_32(ha, addr, 0, 0);
3943 *data_ptr++ = cpu_to_le32(r_value);
3944 addr += cache_hdr->read_ctrl.read_addr_stride;
3945 }
3946 t_value += cache_hdr->addr_ctrl.tag_value_stride;
3947 }
3948 *d_ptr = data_ptr;
3949}
3950
3951static void
3952qla82xx_minidump_process_queue(scsi_qla_host_t *vha,
3953 qla82xx_md_entry_hdr_t *entry_hdr, uint32_t **d_ptr)
3954{
3955 struct qla_hw_data *ha = vha->hw;
3956 uint32_t s_addr, r_addr;
3957 uint32_t r_stride, r_value, r_cnt, qid = 0;
3958 uint32_t i, k, loop_cnt;
3959 struct qla82xx_md_entry_queue *q_hdr;
3960 uint32_t *data_ptr = *d_ptr;
3961
3962 q_hdr = (struct qla82xx_md_entry_queue *)entry_hdr;
3963 s_addr = q_hdr->select_addr;
3964 r_cnt = q_hdr->rd_strd.read_addr_cnt;
3965 r_stride = q_hdr->rd_strd.read_addr_stride;
3966 loop_cnt = q_hdr->op_count;
3967
3968 for (i = 0; i < loop_cnt; i++) {
3969 qla82xx_md_rw_32(ha, s_addr, qid, 1);
3970 r_addr = q_hdr->read_addr;
3971 for (k = 0; k < r_cnt; k++) {
3972 r_value = qla82xx_md_rw_32(ha, r_addr, 0, 0);
3973 *data_ptr++ = cpu_to_le32(r_value);
3974 r_addr += r_stride;
3975 }
3976 qid += q_hdr->q_strd.queue_id_stride;
3977 }
3978 *d_ptr = data_ptr;
3979}
3980
3981static void
3982qla82xx_minidump_process_rdrom(scsi_qla_host_t *vha,
3983 qla82xx_md_entry_hdr_t *entry_hdr, uint32_t **d_ptr)
3984{
3985 struct qla_hw_data *ha = vha->hw;
3986 uint32_t r_addr, r_value;
3987 uint32_t i, loop_cnt;
3988 struct qla82xx_md_entry_rdrom *rom_hdr;
3989 uint32_t *data_ptr = *d_ptr;
3990
3991 rom_hdr = (struct qla82xx_md_entry_rdrom *)entry_hdr;
3992 r_addr = rom_hdr->read_addr;
3993 loop_cnt = rom_hdr->read_data_size/sizeof(uint32_t);
3994
3995 for (i = 0; i < loop_cnt; i++) {
3996 qla82xx_md_rw_32(ha, MD_DIRECT_ROM_WINDOW,
3997 (r_addr & 0xFFFF0000), 1);
3998 r_value = qla82xx_md_rw_32(ha,
3999 MD_DIRECT_ROM_READ_BASE +
4000 (r_addr & 0x0000FFFF), 0, 0);
4001 *data_ptr++ = cpu_to_le32(r_value);
4002 r_addr += sizeof(uint32_t);
4003 }
4004 *d_ptr = data_ptr;
4005}
4006
4007static int
4008qla82xx_minidump_process_rdmem(scsi_qla_host_t *vha,
4009 qla82xx_md_entry_hdr_t *entry_hdr, uint32_t **d_ptr)
4010{
4011 struct qla_hw_data *ha = vha->hw;
4012 uint32_t r_addr, r_value, r_data;
4013 uint32_t i, j, loop_cnt;
4014 struct qla82xx_md_entry_rdmem *m_hdr;
4015 unsigned long flags;
4016 int rval = QLA_FUNCTION_FAILED;
4017 uint32_t *data_ptr = *d_ptr;
4018
4019 m_hdr = (struct qla82xx_md_entry_rdmem *)entry_hdr;
4020 r_addr = m_hdr->read_addr;
4021 loop_cnt = m_hdr->read_data_size/16;
4022
4023 if (r_addr & 0xf) {
4024 ql_log(ql_log_warn, vha, 0xb033,
Masanari Iidad6a03582012-08-22 14:20:58 -04004025 "Read addr 0x%x not 16 bytes aligned\n", r_addr);
Giridhar Malavali08de2842011-08-16 11:31:44 -07004026 return rval;
4027 }
4028
4029 if (m_hdr->read_data_size % 16) {
4030 ql_log(ql_log_warn, vha, 0xb034,
4031 "Read data[0x%x] not multiple of 16 bytes\n",
4032 m_hdr->read_data_size);
4033 return rval;
4034 }
4035
4036 ql_dbg(ql_dbg_p3p, vha, 0xb035,
4037 "[%s]: rdmem_addr: 0x%x, read_data_size: 0x%x, loop_cnt: 0x%x\n",
4038 __func__, r_addr, m_hdr->read_data_size, loop_cnt);
4039
4040 write_lock_irqsave(&ha->hw_lock, flags);
4041 for (i = 0; i < loop_cnt; i++) {
4042 qla82xx_md_rw_32(ha, MD_MIU_TEST_AGT_ADDR_LO, r_addr, 1);
4043 r_value = 0;
4044 qla82xx_md_rw_32(ha, MD_MIU_TEST_AGT_ADDR_HI, r_value, 1);
4045 r_value = MIU_TA_CTL_ENABLE;
4046 qla82xx_md_rw_32(ha, MD_MIU_TEST_AGT_CTRL, r_value, 1);
4047 r_value = MIU_TA_CTL_START | MIU_TA_CTL_ENABLE;
4048 qla82xx_md_rw_32(ha, MD_MIU_TEST_AGT_CTRL, r_value, 1);
4049
4050 for (j = 0; j < MAX_CTL_CHECK; j++) {
4051 r_value = qla82xx_md_rw_32(ha,
4052 MD_MIU_TEST_AGT_CTRL, 0, 0);
4053 if ((r_value & MIU_TA_CTL_BUSY) == 0)
4054 break;
4055 }
4056
4057 if (j >= MAX_CTL_CHECK) {
4058 printk_ratelimited(KERN_ERR
4059 "failed to read through agent\n");
4060 write_unlock_irqrestore(&ha->hw_lock, flags);
4061 return rval;
4062 }
4063
4064 for (j = 0; j < 4; j++) {
4065 r_data = qla82xx_md_rw_32(ha,
4066 MD_MIU_TEST_AGT_RDDATA[j], 0, 0);
4067 *data_ptr++ = cpu_to_le32(r_data);
4068 }
4069 r_addr += 16;
4070 }
4071 write_unlock_irqrestore(&ha->hw_lock, flags);
4072 *d_ptr = data_ptr;
4073 return QLA_SUCCESS;
4074}
4075
4076static int
4077qla82xx_validate_template_chksum(scsi_qla_host_t *vha)
4078{
4079 struct qla_hw_data *ha = vha->hw;
4080 uint64_t chksum = 0;
4081 uint32_t *d_ptr = (uint32_t *)ha->md_tmplt_hdr;
4082 int count = ha->md_template_size/sizeof(uint32_t);
4083
4084 while (count-- > 0)
4085 chksum += *d_ptr++;
4086 while (chksum >> 32)
4087 chksum = (chksum & 0xFFFFFFFF) + (chksum >> 32);
4088 return ~chksum;
4089}
4090
4091static void
4092qla82xx_mark_entry_skipped(scsi_qla_host_t *vha,
4093 qla82xx_md_entry_hdr_t *entry_hdr, int index)
4094{
4095 entry_hdr->d_ctrl.driver_flags |= QLA82XX_DBG_SKIPPED_FLAG;
4096 ql_dbg(ql_dbg_p3p, vha, 0xb036,
4097 "Skipping entry[%d]: "
4098 "ETYPE[0x%x]-ELEVEL[0x%x]\n",
4099 index, entry_hdr->entry_type,
4100 entry_hdr->d_ctrl.entry_capture_mask);
4101}
4102
4103int
4104qla82xx_md_collect(scsi_qla_host_t *vha)
4105{
4106 struct qla_hw_data *ha = vha->hw;
4107 int no_entry_hdr = 0;
4108 qla82xx_md_entry_hdr_t *entry_hdr;
4109 struct qla82xx_md_template_hdr *tmplt_hdr;
4110 uint32_t *data_ptr;
4111 uint32_t total_data_size = 0, f_capture_mask, data_collected = 0;
4112 int i = 0, rval = QLA_FUNCTION_FAILED;
4113
4114 tmplt_hdr = (struct qla82xx_md_template_hdr *)ha->md_tmplt_hdr;
4115 data_ptr = (uint32_t *)ha->md_dump;
4116
4117 if (ha->fw_dumped) {
Giridhar Malavalia8faa262012-02-09 11:15:52 -08004118 ql_log(ql_log_warn, vha, 0xb037,
4119 "Firmware has been previously dumped (%p) "
4120 "-- ignoring request.\n", ha->fw_dump);
Giridhar Malavali08de2842011-08-16 11:31:44 -07004121 goto md_failed;
4122 }
4123
4124 ha->fw_dumped = 0;
4125
4126 if (!ha->md_tmplt_hdr || !ha->md_dump) {
4127 ql_log(ql_log_warn, vha, 0xb038,
4128 "Memory not allocated for minidump capture\n");
4129 goto md_failed;
4130 }
4131
Giridhar Malavalib6d0d9d2012-05-15 14:34:25 -04004132 if (ha->flags.isp82xx_no_md_cap) {
4133 ql_log(ql_log_warn, vha, 0xb054,
4134 "Forced reset from application, "
4135 "ignore minidump capture\n");
4136 ha->flags.isp82xx_no_md_cap = 0;
4137 goto md_failed;
4138 }
4139
Giridhar Malavali08de2842011-08-16 11:31:44 -07004140 if (qla82xx_validate_template_chksum(vha)) {
4141 ql_log(ql_log_info, vha, 0xb039,
4142 "Template checksum validation error\n");
4143 goto md_failed;
4144 }
4145
4146 no_entry_hdr = tmplt_hdr->num_of_entries;
4147 ql_dbg(ql_dbg_p3p, vha, 0xb03a,
4148 "No of entry headers in Template: 0x%x\n", no_entry_hdr);
4149
4150 ql_dbg(ql_dbg_p3p, vha, 0xb03b,
4151 "Capture Mask obtained: 0x%x\n", tmplt_hdr->capture_debug_level);
4152
4153 f_capture_mask = tmplt_hdr->capture_debug_level & 0xFF;
4154
4155 /* Validate whether required debug level is set */
4156 if ((f_capture_mask & 0x3) != 0x3) {
4157 ql_log(ql_log_warn, vha, 0xb03c,
4158 "Minimum required capture mask[0x%x] level not set\n",
4159 f_capture_mask);
4160 goto md_failed;
4161 }
4162 tmplt_hdr->driver_capture_mask = ql2xmdcapmask;
4163
4164 tmplt_hdr->driver_info[0] = vha->host_no;
4165 tmplt_hdr->driver_info[1] = (QLA_DRIVER_MAJOR_VER << 24) |
4166 (QLA_DRIVER_MINOR_VER << 16) | (QLA_DRIVER_PATCH_VER << 8) |
4167 QLA_DRIVER_BETA_VER;
4168
4169 total_data_size = ha->md_dump_size;
4170
Arun Easi880fded2012-02-09 11:15:49 -08004171 ql_dbg(ql_dbg_p3p, vha, 0xb03d,
Giridhar Malavali08de2842011-08-16 11:31:44 -07004172 "Total minidump data_size 0x%x to be captured\n", total_data_size);
4173
4174 /* Check whether template obtained is valid */
4175 if (tmplt_hdr->entry_type != QLA82XX_TLHDR) {
4176 ql_log(ql_log_warn, vha, 0xb04e,
4177 "Bad template header entry type: 0x%x obtained\n",
4178 tmplt_hdr->entry_type);
4179 goto md_failed;
4180 }
4181
4182 entry_hdr = (qla82xx_md_entry_hdr_t *) \
4183 (((uint8_t *)ha->md_tmplt_hdr) + tmplt_hdr->first_entry_offset);
4184
4185 /* Walk through the entry headers */
4186 for (i = 0; i < no_entry_hdr; i++) {
4187
4188 if (data_collected > total_data_size) {
4189 ql_log(ql_log_warn, vha, 0xb03e,
4190 "More MiniDump data collected: [0x%x]\n",
4191 data_collected);
4192 goto md_failed;
4193 }
4194
4195 if (!(entry_hdr->d_ctrl.entry_capture_mask &
4196 ql2xmdcapmask)) {
4197 entry_hdr->d_ctrl.driver_flags |=
4198 QLA82XX_DBG_SKIPPED_FLAG;
4199 ql_dbg(ql_dbg_p3p, vha, 0xb03f,
4200 "Skipping entry[%d]: "
4201 "ETYPE[0x%x]-ELEVEL[0x%x]\n",
4202 i, entry_hdr->entry_type,
4203 entry_hdr->d_ctrl.entry_capture_mask);
4204 goto skip_nxt_entry;
4205 }
4206
4207 ql_dbg(ql_dbg_p3p, vha, 0xb040,
4208 "[%s]: data ptr[%d]: %p, entry_hdr: %p\n"
4209 "entry_type: 0x%x, captrue_mask: 0x%x\n",
4210 __func__, i, data_ptr, entry_hdr,
4211 entry_hdr->entry_type,
4212 entry_hdr->d_ctrl.entry_capture_mask);
4213
4214 ql_dbg(ql_dbg_p3p, vha, 0xb041,
4215 "Data collected: [0x%x], Dump size left:[0x%x]\n",
4216 data_collected, (ha->md_dump_size - data_collected));
4217
4218 /* Decode the entry type and take
4219 * required action to capture debug data */
4220 switch (entry_hdr->entry_type) {
4221 case QLA82XX_RDEND:
4222 qla82xx_mark_entry_skipped(vha, entry_hdr, i);
4223 break;
4224 case QLA82XX_CNTRL:
4225 rval = qla82xx_minidump_process_control(vha,
4226 entry_hdr, &data_ptr);
4227 if (rval != QLA_SUCCESS) {
4228 qla82xx_mark_entry_skipped(vha, entry_hdr, i);
4229 goto md_failed;
4230 }
4231 break;
4232 case QLA82XX_RDCRB:
4233 qla82xx_minidump_process_rdcrb(vha,
4234 entry_hdr, &data_ptr);
4235 break;
4236 case QLA82XX_RDMEM:
4237 rval = qla82xx_minidump_process_rdmem(vha,
4238 entry_hdr, &data_ptr);
4239 if (rval != QLA_SUCCESS) {
4240 qla82xx_mark_entry_skipped(vha, entry_hdr, i);
4241 goto md_failed;
4242 }
4243 break;
4244 case QLA82XX_BOARD:
4245 case QLA82XX_RDROM:
4246 qla82xx_minidump_process_rdrom(vha,
4247 entry_hdr, &data_ptr);
4248 break;
4249 case QLA82XX_L2DTG:
4250 case QLA82XX_L2ITG:
4251 case QLA82XX_L2DAT:
4252 case QLA82XX_L2INS:
4253 rval = qla82xx_minidump_process_l2tag(vha,
4254 entry_hdr, &data_ptr);
4255 if (rval != QLA_SUCCESS) {
4256 qla82xx_mark_entry_skipped(vha, entry_hdr, i);
4257 goto md_failed;
4258 }
4259 break;
4260 case QLA82XX_L1DAT:
4261 case QLA82XX_L1INS:
4262 qla82xx_minidump_process_l1cache(vha,
4263 entry_hdr, &data_ptr);
4264 break;
4265 case QLA82XX_RDOCM:
4266 qla82xx_minidump_process_rdocm(vha,
4267 entry_hdr, &data_ptr);
4268 break;
4269 case QLA82XX_RDMUX:
4270 qla82xx_minidump_process_rdmux(vha,
4271 entry_hdr, &data_ptr);
4272 break;
4273 case QLA82XX_QUEUE:
4274 qla82xx_minidump_process_queue(vha,
4275 entry_hdr, &data_ptr);
4276 break;
4277 case QLA82XX_RDNOP:
4278 default:
4279 qla82xx_mark_entry_skipped(vha, entry_hdr, i);
4280 break;
4281 }
4282
4283 ql_dbg(ql_dbg_p3p, vha, 0xb042,
4284 "[%s]: data ptr[%d]: %p\n", __func__, i, data_ptr);
4285
4286 data_collected = (uint8_t *)data_ptr -
4287 (uint8_t *)ha->md_dump;
4288skip_nxt_entry:
4289 entry_hdr = (qla82xx_md_entry_hdr_t *) \
4290 (((uint8_t *)entry_hdr) + entry_hdr->entry_size);
4291 }
4292
4293 if (data_collected != total_data_size) {
Arun Easi880fded2012-02-09 11:15:49 -08004294 ql_dbg(ql_dbg_p3p, vha, 0xb043,
Giridhar Malavali08de2842011-08-16 11:31:44 -07004295 "MiniDump data mismatch: Data collected: [0x%x],"
4296 "total_data_size:[0x%x]\n",
4297 data_collected, total_data_size);
4298 goto md_failed;
4299 }
4300
4301 ql_log(ql_log_info, vha, 0xb044,
4302 "Firmware dump saved to temp buffer (%ld/%p %ld/%p).\n",
4303 vha->host_no, ha->md_tmplt_hdr, vha->host_no, ha->md_dump);
4304 ha->fw_dumped = 1;
4305 qla2x00_post_uevent_work(vha, QLA_UEVENT_CODE_FW_DUMP);
4306
4307md_failed:
4308 return rval;
4309}
4310
4311int
4312qla82xx_md_alloc(scsi_qla_host_t *vha)
4313{
4314 struct qla_hw_data *ha = vha->hw;
4315 int i, k;
4316 struct qla82xx_md_template_hdr *tmplt_hdr;
4317
4318 tmplt_hdr = (struct qla82xx_md_template_hdr *)ha->md_tmplt_hdr;
4319
4320 if (ql2xmdcapmask < 0x3 || ql2xmdcapmask > 0x7F) {
4321 ql2xmdcapmask = tmplt_hdr->capture_debug_level & 0xFF;
4322 ql_log(ql_log_info, vha, 0xb045,
4323 "Forcing driver capture mask to firmware default capture mask: 0x%x.\n",
4324 ql2xmdcapmask);
4325 }
4326
4327 for (i = 0x2, k = 1; (i & QLA82XX_DEFAULT_CAP_MASK); i <<= 1, k++) {
4328 if (i & ql2xmdcapmask)
4329 ha->md_dump_size += tmplt_hdr->capture_size_array[k];
4330 }
4331
4332 if (ha->md_dump) {
4333 ql_log(ql_log_warn, vha, 0xb046,
4334 "Firmware dump previously allocated.\n");
4335 return 1;
4336 }
4337
4338 ha->md_dump = vmalloc(ha->md_dump_size);
4339 if (ha->md_dump == NULL) {
4340 ql_log(ql_log_warn, vha, 0xb047,
4341 "Unable to allocate memory for Minidump size "
4342 "(0x%x).\n", ha->md_dump_size);
4343 return 1;
4344 }
4345 return 0;
4346}
4347
4348void
4349qla82xx_md_free(scsi_qla_host_t *vha)
4350{
4351 struct qla_hw_data *ha = vha->hw;
4352
4353 /* Release the template header allocated */
4354 if (ha->md_tmplt_hdr) {
4355 ql_log(ql_log_info, vha, 0xb048,
4356 "Free MiniDump template: %p, size (%d KB)\n",
4357 ha->md_tmplt_hdr, ha->md_template_size / 1024);
4358 dma_free_coherent(&ha->pdev->dev, ha->md_template_size,
4359 ha->md_tmplt_hdr, ha->md_tmplt_hdr_dma);
Saurav Kashyapfa492632012-11-21 02:40:29 -05004360 ha->md_tmplt_hdr = NULL;
Giridhar Malavali08de2842011-08-16 11:31:44 -07004361 }
4362
4363 /* Release the template data buffer allocated */
4364 if (ha->md_dump) {
4365 ql_log(ql_log_info, vha, 0xb049,
4366 "Free MiniDump memory: %p, size (%d KB)\n",
4367 ha->md_dump, ha->md_dump_size / 1024);
4368 vfree(ha->md_dump);
4369 ha->md_dump_size = 0;
Saurav Kashyapfa492632012-11-21 02:40:29 -05004370 ha->md_dump = NULL;
Giridhar Malavali08de2842011-08-16 11:31:44 -07004371 }
4372}
4373
4374void
4375qla82xx_md_prep(scsi_qla_host_t *vha)
4376{
4377 struct qla_hw_data *ha = vha->hw;
4378 int rval;
4379
4380 /* Get Minidump template size */
4381 rval = qla82xx_md_get_template_size(vha);
4382 if (rval == QLA_SUCCESS) {
4383 ql_log(ql_log_info, vha, 0xb04a,
4384 "MiniDump Template size obtained (%d KB)\n",
4385 ha->md_template_size / 1024);
4386
4387 /* Get Minidump template */
4388 rval = qla82xx_md_get_template(vha);
4389 if (rval == QLA_SUCCESS) {
4390 ql_dbg(ql_dbg_p3p, vha, 0xb04b,
4391 "MiniDump Template obtained\n");
4392
4393 /* Allocate memory for minidump */
4394 rval = qla82xx_md_alloc(vha);
4395 if (rval == QLA_SUCCESS)
4396 ql_log(ql_log_info, vha, 0xb04c,
4397 "MiniDump memory allocated (%d KB)\n",
4398 ha->md_dump_size / 1024);
4399 else {
4400 ql_log(ql_log_info, vha, 0xb04d,
4401 "Free MiniDump template: %p, size: (%d KB)\n",
4402 ha->md_tmplt_hdr,
4403 ha->md_template_size / 1024);
4404 dma_free_coherent(&ha->pdev->dev,
4405 ha->md_template_size,
4406 ha->md_tmplt_hdr, ha->md_tmplt_hdr_dma);
Saurav Kashyapfa492632012-11-21 02:40:29 -05004407 ha->md_tmplt_hdr = NULL;
Giridhar Malavali08de2842011-08-16 11:31:44 -07004408 }
4409
4410 }
4411 }
4412}
Saurav Kashyap999916d2011-08-16 11:31:45 -07004413
4414int
4415qla82xx_beacon_on(struct scsi_qla_host *vha)
4416{
4417
4418 int rval;
4419 struct qla_hw_data *ha = vha->hw;
4420 qla82xx_idc_lock(ha);
4421 rval = qla82xx_mbx_beacon_ctl(vha, 1);
4422
4423 if (rval) {
4424 ql_log(ql_log_warn, vha, 0xb050,
4425 "mbx set led config failed in %s\n", __func__);
4426 goto exit;
4427 }
4428 ha->beacon_blink_led = 1;
4429exit:
4430 qla82xx_idc_unlock(ha);
4431 return rval;
4432}
4433
4434int
4435qla82xx_beacon_off(struct scsi_qla_host *vha)
4436{
4437
4438 int rval;
4439 struct qla_hw_data *ha = vha->hw;
4440 qla82xx_idc_lock(ha);
4441 rval = qla82xx_mbx_beacon_ctl(vha, 0);
4442
4443 if (rval) {
4444 ql_log(ql_log_warn, vha, 0xb051,
4445 "mbx set led config failed in %s\n", __func__);
4446 goto exit;
4447 }
4448 ha->beacon_blink_led = 0;
4449exit:
4450 qla82xx_idc_unlock(ha);
4451 return rval;
4452}