blob: 54380b434b304eddde918a94ee833e18be6a35f6 [file] [log] [blame]
Giridhar Malavalia9083012010-04-12 17:59:55 -07001/*
2 * QLogic Fibre Channel HBA Driver
Armen Baloyanbd21eaf2014-04-11 16:54:24 -04003 * Copyright (c) 2003-2014 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/*
Bart Van Assche8dfa4b5a2015-07-09 07:24:50 -0700350 * In: 'off_in' is offset from CRB space in 128M pci map
351 * Out: 'off_out' is 2M pci map addr
Giridhar Malavalia9083012010-04-12 17:59:55 -0700352 * side effect: lock crb window
353 */
354static void
Bart Van Assche8dfa4b5a2015-07-09 07:24:50 -0700355qla82xx_pci_set_crbwindow_2M(struct qla_hw_data *ha, ulong off_in,
356 void __iomem **off_out)
Giridhar Malavalia9083012010-04-12 17:59:55 -0700357{
358 u32 win_read;
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700359 scsi_qla_host_t *vha = pci_get_drvdata(ha->pdev);
Giridhar Malavalia9083012010-04-12 17:59:55 -0700360
Bart Van Assche8dfa4b5a2015-07-09 07:24:50 -0700361 ha->crb_win = CRB_HI(off_in);
362 writel(ha->crb_win, 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 */
Bart Van Assche8dfa4b5a2015-07-09 07:24:50 -0700367 win_read = RD_REG_DWORD(CRB_WINDOW_2M + ha->nx_pcibase);
Giridhar Malavalia9083012010-04-12 17:59:55 -0700368 if (win_read != ha->crb_win) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700369 ql_dbg(ql_dbg_p3p, vha, 0xb000,
370 "%s: Written crbwin (0x%x) "
371 "!= Read crbwin (0x%x), off=0x%lx.\n",
Bart Van Assche8dfa4b5a2015-07-09 07:24:50 -0700372 __func__, ha->crb_win, win_read, off_in);
Giridhar Malavalia9083012010-04-12 17:59:55 -0700373 }
Bart Van Assche8dfa4b5a2015-07-09 07:24:50 -0700374 *off_out = (off_in & MASK(16)) + CRB_INDIRECT_2M + ha->nx_pcibase;
Giridhar Malavalia9083012010-04-12 17:59:55 -0700375}
376
377static inline unsigned long
378qla82xx_pci_set_crbwindow(struct qla_hw_data *ha, u64 off)
379{
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700380 scsi_qla_host_t *vha = pci_get_drvdata(ha->pdev);
Giridhar Malavalia9083012010-04-12 17:59:55 -0700381 /* See if we are currently pointing to the region we want to use next */
382 if ((off >= QLA82XX_CRB_PCIX_HOST) && (off < QLA82XX_CRB_DDR_NET)) {
383 /* No need to change window. PCIX and PCIEregs are in both
384 * regs are in both windows.
385 */
386 return off;
387 }
388
389 if ((off >= QLA82XX_CRB_PCIX_HOST) && (off < QLA82XX_CRB_PCIX_HOST2)) {
390 /* We are in first CRB window */
391 if (ha->curr_window != 0)
392 WARN_ON(1);
393 return off;
394 }
395
396 if ((off > QLA82XX_CRB_PCIX_HOST2) && (off < QLA82XX_CRB_MAX)) {
397 /* We are in second CRB window */
398 off = off - QLA82XX_CRB_PCIX_HOST2 + QLA82XX_CRB_PCIX_HOST;
399
400 if (ha->curr_window != 1)
401 return off;
402
403 /* We are in the QM or direct access
404 * register region - do nothing
405 */
406 if ((off >= QLA82XX_PCI_DIRECT_CRB) &&
407 (off < QLA82XX_PCI_CAMQM_MAX))
408 return off;
409 }
410 /* strange address given */
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700411 ql_dbg(ql_dbg_p3p, vha, 0xb001,
Joe Perchesd8424f62011-11-18 09:03:06 -0800412 "%s: Warning: unm_nic_pci_set_crbwindow "
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700413 "called with an unknown address(%llx).\n",
414 QLA2XXX_DRIVER_NAME, off);
Giridhar Malavalia9083012010-04-12 17:59:55 -0700415 return off;
416}
417
Giridhar Malavali77e334d2010-09-03 15:20:52 -0700418static int
Bart Van Assche8dfa4b5a2015-07-09 07:24:50 -0700419qla82xx_pci_get_crb_addr_2M(struct qla_hw_data *ha, ulong off_in,
420 void __iomem **off_out)
Giridhar Malavali77e334d2010-09-03 15:20:52 -0700421{
422 struct crb_128M_2M_sub_block_map *m;
423
Bart Van Assche8dfa4b5a2015-07-09 07:24:50 -0700424 if (off_in >= QLA82XX_CRB_MAX)
Giridhar Malavali77e334d2010-09-03 15:20:52 -0700425 return -1;
426
Bart Van Assche8dfa4b5a2015-07-09 07:24:50 -0700427 if (off_in >= QLA82XX_PCI_CAMQM && off_in < QLA82XX_PCI_CAMQM_2M_END) {
428 *off_out = (off_in - QLA82XX_PCI_CAMQM) +
Giridhar Malavali77e334d2010-09-03 15:20:52 -0700429 QLA82XX_PCI_CAMQM_2M_BASE + ha->nx_pcibase;
430 return 0;
431 }
432
Bart Van Assche8dfa4b5a2015-07-09 07:24:50 -0700433 if (off_in < QLA82XX_PCI_CRBSPACE)
Giridhar Malavali77e334d2010-09-03 15:20:52 -0700434 return -1;
435
Bart Van Assche0874f8e2015-11-16 15:26:51 -0800436 off_in -= QLA82XX_PCI_CRBSPACE;
Giridhar Malavali77e334d2010-09-03 15:20:52 -0700437
438 /* Try direct map */
Bart Van Assche8dfa4b5a2015-07-09 07:24:50 -0700439 m = &crb_128M_2M_map[CRB_BLK(off_in)].sub_block[CRB_SUBBLK(off_in)];
Giridhar Malavali77e334d2010-09-03 15:20:52 -0700440
Bart Van Assche8dfa4b5a2015-07-09 07:24:50 -0700441 if (m->valid && (m->start_128M <= off_in) && (m->end_128M > off_in)) {
442 *off_out = off_in + m->start_2M - m->start_128M + ha->nx_pcibase;
Giridhar Malavali77e334d2010-09-03 15:20:52 -0700443 return 0;
444 }
445 /* Not in direct map, use crb window */
Bart Van Assche0874f8e2015-11-16 15:26:51 -0800446 *off_out = (void __iomem *)off_in;
Giridhar Malavali77e334d2010-09-03 15:20:52 -0700447 return 1;
448}
449
450#define CRB_WIN_LOCK_TIMEOUT 100000000
451static int qla82xx_crb_win_lock(struct qla_hw_data *ha)
452{
453 int done = 0, timeout = 0;
454
455 while (!done) {
456 /* acquire semaphore3 from PCI HW block */
457 done = qla82xx_rd_32(ha, QLA82XX_PCIE_REG(PCIE_SEM7_LOCK));
458 if (done == 1)
459 break;
460 if (timeout >= CRB_WIN_LOCK_TIMEOUT)
461 return -1;
462 timeout++;
463 }
464 qla82xx_wr_32(ha, QLA82XX_CRB_WIN_LOCK_ID, ha->portnum);
465 return 0;
466}
467
Giridhar Malavalia9083012010-04-12 17:59:55 -0700468int
Bart Van Assche8dfa4b5a2015-07-09 07:24:50 -0700469qla82xx_wr_32(struct qla_hw_data *ha, ulong off_in, u32 data)
Giridhar Malavalia9083012010-04-12 17:59:55 -0700470{
Bart Van Assche8dfa4b5a2015-07-09 07:24:50 -0700471 void __iomem *off;
Giridhar Malavalia9083012010-04-12 17:59:55 -0700472 unsigned long flags = 0;
473 int rv;
474
Bart Van Assche8dfa4b5a2015-07-09 07:24:50 -0700475 rv = qla82xx_pci_get_crb_addr_2M(ha, off_in, &off);
Giridhar Malavalia9083012010-04-12 17:59:55 -0700476
477 BUG_ON(rv == -1);
478
479 if (rv == 1) {
Bart Van Assche8d163662015-07-09 07:25:46 -0700480#ifndef __CHECKER__
Giridhar Malavalia9083012010-04-12 17:59:55 -0700481 write_lock_irqsave(&ha->hw_lock, flags);
Bart Van Assche8d163662015-07-09 07:25:46 -0700482#endif
Giridhar Malavalia9083012010-04-12 17:59:55 -0700483 qla82xx_crb_win_lock(ha);
Bart Van Assche8dfa4b5a2015-07-09 07:24:50 -0700484 qla82xx_pci_set_crbwindow_2M(ha, off_in, &off);
Giridhar Malavalia9083012010-04-12 17:59:55 -0700485 }
486
487 writel(data, (void __iomem *)off);
488
489 if (rv == 1) {
490 qla82xx_rd_32(ha, QLA82XX_PCIE_REG(PCIE_SEM7_UNLOCK));
Bart Van Assche8d163662015-07-09 07:25:46 -0700491#ifndef __CHECKER__
Giridhar Malavalia9083012010-04-12 17:59:55 -0700492 write_unlock_irqrestore(&ha->hw_lock, flags);
Bart Van Assche8d163662015-07-09 07:25:46 -0700493#endif
Giridhar Malavalia9083012010-04-12 17:59:55 -0700494 }
495 return 0;
496}
497
498int
Bart Van Assche8dfa4b5a2015-07-09 07:24:50 -0700499qla82xx_rd_32(struct qla_hw_data *ha, ulong off_in)
Giridhar Malavalia9083012010-04-12 17:59:55 -0700500{
Bart Van Assche8dfa4b5a2015-07-09 07:24:50 -0700501 void __iomem *off;
Giridhar Malavalia9083012010-04-12 17:59:55 -0700502 unsigned long flags = 0;
503 int rv;
504 u32 data;
505
Bart Van Assche8dfa4b5a2015-07-09 07:24:50 -0700506 rv = qla82xx_pci_get_crb_addr_2M(ha, off_in, &off);
Giridhar Malavalia9083012010-04-12 17:59:55 -0700507
508 BUG_ON(rv == -1);
509
510 if (rv == 1) {
Bart Van Assche8d163662015-07-09 07:25:46 -0700511#ifndef __CHECKER__
Giridhar Malavalia9083012010-04-12 17:59:55 -0700512 write_lock_irqsave(&ha->hw_lock, flags);
Bart Van Assche8d163662015-07-09 07:25:46 -0700513#endif
Giridhar Malavalia9083012010-04-12 17:59:55 -0700514 qla82xx_crb_win_lock(ha);
Bart Van Assche8dfa4b5a2015-07-09 07:24:50 -0700515 qla82xx_pci_set_crbwindow_2M(ha, off_in, &off);
Giridhar Malavalia9083012010-04-12 17:59:55 -0700516 }
Bart Van Assche8dfa4b5a2015-07-09 07:24:50 -0700517 data = RD_REG_DWORD(off);
Giridhar Malavalia9083012010-04-12 17:59:55 -0700518
519 if (rv == 1) {
520 qla82xx_rd_32(ha, QLA82XX_PCIE_REG(PCIE_SEM7_UNLOCK));
Bart Van Assche8d163662015-07-09 07:25:46 -0700521#ifndef __CHECKER__
Giridhar Malavalia9083012010-04-12 17:59:55 -0700522 write_unlock_irqrestore(&ha->hw_lock, flags);
Bart Van Assche8d163662015-07-09 07:25:46 -0700523#endif
Giridhar Malavalia9083012010-04-12 17:59:55 -0700524 }
525 return data;
526}
527
Giridhar Malavalia9083012010-04-12 17:59:55 -0700528#define IDC_LOCK_TIMEOUT 100000000
529int qla82xx_idc_lock(struct qla_hw_data *ha)
530{
531 int i;
532 int done = 0, timeout = 0;
533
534 while (!done) {
535 /* acquire semaphore5 from PCI HW block */
536 done = qla82xx_rd_32(ha, QLA82XX_PCIE_REG(PCIE_SEM5_LOCK));
537 if (done == 1)
538 break;
539 if (timeout >= IDC_LOCK_TIMEOUT)
540 return -1;
541
542 timeout++;
543
544 /* Yield CPU */
545 if (!in_interrupt())
546 schedule();
547 else {
548 for (i = 0; i < 20; i++)
549 cpu_relax();
550 }
551 }
552
553 return 0;
554}
555
556void qla82xx_idc_unlock(struct qla_hw_data *ha)
557{
558 qla82xx_rd_32(ha, QLA82XX_PCIE_REG(PCIE_SEM5_UNLOCK));
559}
560
Giridhar Malavalia9083012010-04-12 17:59:55 -0700561/*
562 * check memory access boundary.
563 * used by test agent. support ddr access only for now
564 */
565static unsigned long
566qla82xx_pci_mem_bound_check(struct qla_hw_data *ha,
567 unsigned long long addr, int size)
568{
Bart Van Asschedf3f4cd2015-07-09 07:23:46 -0700569 if (!addr_in_range(addr, QLA82XX_ADDR_DDR_NET,
Giridhar Malavalia9083012010-04-12 17:59:55 -0700570 QLA82XX_ADDR_DDR_NET_MAX) ||
Bart Van Asschedf3f4cd2015-07-09 07:23:46 -0700571 !addr_in_range(addr + size - 1, QLA82XX_ADDR_DDR_NET,
Giridhar Malavalia9083012010-04-12 17:59:55 -0700572 QLA82XX_ADDR_DDR_NET_MAX) ||
573 ((size != 1) && (size != 2) && (size != 4) && (size != 8)))
574 return 0;
575 else
576 return 1;
577}
578
Saurav Kashyapfa492632012-11-21 02:40:29 -0500579static int qla82xx_pci_set_window_warning_count;
Giridhar Malavalia9083012010-04-12 17:59:55 -0700580
Giridhar Malavali77e334d2010-09-03 15:20:52 -0700581static unsigned long
Giridhar Malavalia9083012010-04-12 17:59:55 -0700582qla82xx_pci_set_window(struct qla_hw_data *ha, unsigned long long addr)
583{
584 int window;
585 u32 win_read;
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700586 scsi_qla_host_t *vha = pci_get_drvdata(ha->pdev);
Giridhar Malavalia9083012010-04-12 17:59:55 -0700587
Bart Van Asschedf3f4cd2015-07-09 07:23:46 -0700588 if (addr_in_range(addr, QLA82XX_ADDR_DDR_NET,
Giridhar Malavalia9083012010-04-12 17:59:55 -0700589 QLA82XX_ADDR_DDR_NET_MAX)) {
590 /* DDR network side */
591 window = MN_WIN(addr);
592 ha->ddr_mn_window = window;
593 qla82xx_wr_32(ha,
594 ha->mn_win_crb | QLA82XX_PCI_CRBSPACE, window);
595 win_read = qla82xx_rd_32(ha,
596 ha->mn_win_crb | QLA82XX_PCI_CRBSPACE);
597 if ((win_read << 17) != window) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700598 ql_dbg(ql_dbg_p3p, vha, 0xb003,
599 "%s: Written MNwin (0x%x) != Read MNwin (0x%x).\n",
Giridhar Malavalia9083012010-04-12 17:59:55 -0700600 __func__, window, win_read);
601 }
602 addr = GET_MEM_OFFS_2M(addr) + QLA82XX_PCI_DDR_NET;
Bart Van Asschedf3f4cd2015-07-09 07:23:46 -0700603 } else if (addr_in_range(addr, QLA82XX_ADDR_OCM0,
Giridhar Malavalia9083012010-04-12 17:59:55 -0700604 QLA82XX_ADDR_OCM0_MAX)) {
605 unsigned int temp1;
606 if ((addr & 0x00ff800) == 0xff800) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700607 ql_log(ql_log_warn, vha, 0xb004,
Giridhar Malavalia9083012010-04-12 17:59:55 -0700608 "%s: QM access not handled.\n", __func__);
609 addr = -1UL;
610 }
611 window = OCM_WIN(addr);
612 ha->ddr_mn_window = window;
613 qla82xx_wr_32(ha,
614 ha->mn_win_crb | QLA82XX_PCI_CRBSPACE, window);
615 win_read = qla82xx_rd_32(ha,
616 ha->mn_win_crb | QLA82XX_PCI_CRBSPACE);
617 temp1 = ((window & 0x1FF) << 7) |
618 ((window & 0x0FFFE0000) >> 17);
619 if (win_read != temp1) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700620 ql_log(ql_log_warn, vha, 0xb005,
621 "%s: Written OCMwin (0x%x) != Read OCMwin (0x%x).\n",
Giridhar Malavalia9083012010-04-12 17:59:55 -0700622 __func__, temp1, win_read);
623 }
624 addr = GET_MEM_OFFS_2M(addr) + QLA82XX_PCI_OCM0_2M;
625
Bart Van Asschedf3f4cd2015-07-09 07:23:46 -0700626 } else if (addr_in_range(addr, QLA82XX_ADDR_QDR_NET,
Giridhar Malavalia9083012010-04-12 17:59:55 -0700627 QLA82XX_P3_ADDR_QDR_NET_MAX)) {
628 /* QDR network side */
629 window = MS_WIN(addr);
630 ha->qdr_sn_window = window;
631 qla82xx_wr_32(ha,
632 ha->ms_win_crb | QLA82XX_PCI_CRBSPACE, window);
633 win_read = qla82xx_rd_32(ha,
634 ha->ms_win_crb | QLA82XX_PCI_CRBSPACE);
635 if (win_read != window) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700636 ql_log(ql_log_warn, vha, 0xb006,
637 "%s: Written MSwin (0x%x) != Read MSwin (0x%x).\n",
Giridhar Malavalia9083012010-04-12 17:59:55 -0700638 __func__, window, win_read);
639 }
640 addr = GET_MEM_OFFS_2M(addr) + QLA82XX_PCI_QDR_NET;
641 } else {
642 /*
643 * peg gdb frequently accesses memory that doesn't exist,
644 * this limits the chit chat so debugging isn't slowed down.
645 */
646 if ((qla82xx_pci_set_window_warning_count++ < 8) ||
647 (qla82xx_pci_set_window_warning_count%64 == 0)) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700648 ql_log(ql_log_warn, vha, 0xb007,
649 "%s: Warning:%s Unknown address range!.\n",
650 __func__, QLA2XXX_DRIVER_NAME);
Giridhar Malavalia9083012010-04-12 17:59:55 -0700651 }
652 addr = -1UL;
653 }
654 return addr;
655}
656
657/* check if address is in the same windows as the previous access */
658static int qla82xx_pci_is_same_window(struct qla_hw_data *ha,
659 unsigned long long addr)
660{
661 int window;
662 unsigned long long qdr_max;
663
664 qdr_max = QLA82XX_P3_ADDR_QDR_NET_MAX;
665
666 /* DDR network side */
Bart Van Asschedf3f4cd2015-07-09 07:23:46 -0700667 if (addr_in_range(addr, QLA82XX_ADDR_DDR_NET,
Giridhar Malavalia9083012010-04-12 17:59:55 -0700668 QLA82XX_ADDR_DDR_NET_MAX))
669 BUG();
Bart Van Asschedf3f4cd2015-07-09 07:23:46 -0700670 else if (addr_in_range(addr, QLA82XX_ADDR_OCM0,
Giridhar Malavalia9083012010-04-12 17:59:55 -0700671 QLA82XX_ADDR_OCM0_MAX))
672 return 1;
Bart Van Asschedf3f4cd2015-07-09 07:23:46 -0700673 else if (addr_in_range(addr, QLA82XX_ADDR_OCM1,
Giridhar Malavalia9083012010-04-12 17:59:55 -0700674 QLA82XX_ADDR_OCM1_MAX))
675 return 1;
Bart Van Asschedf3f4cd2015-07-09 07:23:46 -0700676 else if (addr_in_range(addr, QLA82XX_ADDR_QDR_NET, qdr_max)) {
Giridhar Malavalia9083012010-04-12 17:59:55 -0700677 /* QDR network side */
678 window = ((addr - QLA82XX_ADDR_QDR_NET) >> 22) & 0x3f;
679 if (ha->qdr_sn_window == window)
680 return 1;
681 }
682 return 0;
683}
684
685static int qla82xx_pci_mem_read_direct(struct qla_hw_data *ha,
686 u64 off, void *data, int size)
687{
688 unsigned long flags;
Saurav Kashyapfa492632012-11-21 02:40:29 -0500689 void __iomem *addr = NULL;
Giridhar Malavalia9083012010-04-12 17:59:55 -0700690 int ret = 0;
691 u64 start;
Saurav Kashyapfa492632012-11-21 02:40:29 -0500692 uint8_t __iomem *mem_ptr = NULL;
Giridhar Malavalia9083012010-04-12 17:59:55 -0700693 unsigned long mem_base;
694 unsigned long mem_page;
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700695 scsi_qla_host_t *vha = pci_get_drvdata(ha->pdev);
Giridhar Malavalia9083012010-04-12 17:59:55 -0700696
697 write_lock_irqsave(&ha->hw_lock, flags);
698
699 /*
700 * If attempting to access unknown address or straddle hw windows,
701 * do not access.
702 */
703 start = qla82xx_pci_set_window(ha, off);
704 if ((start == -1UL) ||
705 (qla82xx_pci_is_same_window(ha, off + size - 1) == 0)) {
706 write_unlock_irqrestore(&ha->hw_lock, flags);
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700707 ql_log(ql_log_fatal, vha, 0xb008,
708 "%s out of bound pci memory "
709 "access, offset is 0x%llx.\n",
710 QLA2XXX_DRIVER_NAME, off);
Giridhar Malavalia9083012010-04-12 17:59:55 -0700711 return -1;
712 }
713
Giridhar Malavalif1af6202010-05-04 15:01:34 -0700714 write_unlock_irqrestore(&ha->hw_lock, flags);
715 mem_base = pci_resource_start(ha->pdev, 0);
716 mem_page = start & PAGE_MASK;
717 /* Map two pages whenever user tries to access addresses in two
718 * consecutive pages.
719 */
720 if (mem_page != ((start + size - 1) & PAGE_MASK))
721 mem_ptr = ioremap(mem_base + mem_page, PAGE_SIZE * 2);
722 else
723 mem_ptr = ioremap(mem_base + mem_page, PAGE_SIZE);
Saurav Kashyapfa492632012-11-21 02:40:29 -0500724 if (mem_ptr == NULL) {
Giridhar Malavalif1af6202010-05-04 15:01:34 -0700725 *(u8 *)data = 0;
726 return -1;
Giridhar Malavalia9083012010-04-12 17:59:55 -0700727 }
Giridhar Malavalif1af6202010-05-04 15:01:34 -0700728 addr = mem_ptr;
729 addr += start & (PAGE_SIZE - 1);
730 write_lock_irqsave(&ha->hw_lock, flags);
Giridhar Malavalia9083012010-04-12 17:59:55 -0700731
732 switch (size) {
733 case 1:
734 *(u8 *)data = readb(addr);
735 break;
736 case 2:
737 *(u16 *)data = readw(addr);
738 break;
739 case 4:
740 *(u32 *)data = readl(addr);
741 break;
742 case 8:
743 *(u64 *)data = readq(addr);
744 break;
745 default:
746 ret = -1;
747 break;
748 }
749 write_unlock_irqrestore(&ha->hw_lock, flags);
750
751 if (mem_ptr)
752 iounmap(mem_ptr);
753 return ret;
754}
755
756static int
757qla82xx_pci_mem_write_direct(struct qla_hw_data *ha,
758 u64 off, void *data, int size)
759{
760 unsigned long flags;
Saurav Kashyapfa492632012-11-21 02:40:29 -0500761 void __iomem *addr = NULL;
Giridhar Malavalia9083012010-04-12 17:59:55 -0700762 int ret = 0;
763 u64 start;
Saurav Kashyapfa492632012-11-21 02:40:29 -0500764 uint8_t __iomem *mem_ptr = NULL;
Giridhar Malavalia9083012010-04-12 17:59:55 -0700765 unsigned long mem_base;
766 unsigned long mem_page;
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700767 scsi_qla_host_t *vha = pci_get_drvdata(ha->pdev);
Giridhar Malavalia9083012010-04-12 17:59:55 -0700768
769 write_lock_irqsave(&ha->hw_lock, flags);
770
771 /*
772 * If attempting to access unknown address or straddle hw windows,
773 * do not access.
774 */
775 start = qla82xx_pci_set_window(ha, off);
776 if ((start == -1UL) ||
777 (qla82xx_pci_is_same_window(ha, off + size - 1) == 0)) {
778 write_unlock_irqrestore(&ha->hw_lock, flags);
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700779 ql_log(ql_log_fatal, vha, 0xb009,
780 "%s out of bount memory "
781 "access, offset is 0x%llx.\n",
782 QLA2XXX_DRIVER_NAME, off);
Giridhar Malavalia9083012010-04-12 17:59:55 -0700783 return -1;
784 }
785
Giridhar Malavalif1af6202010-05-04 15:01:34 -0700786 write_unlock_irqrestore(&ha->hw_lock, flags);
787 mem_base = pci_resource_start(ha->pdev, 0);
788 mem_page = start & PAGE_MASK;
789 /* Map two pages whenever user tries to access addresses in two
790 * consecutive pages.
791 */
792 if (mem_page != ((start + size - 1) & PAGE_MASK))
793 mem_ptr = ioremap(mem_base + mem_page, PAGE_SIZE*2);
794 else
795 mem_ptr = ioremap(mem_base + mem_page, PAGE_SIZE);
Saurav Kashyapfa492632012-11-21 02:40:29 -0500796 if (mem_ptr == NULL)
Giridhar Malavalif1af6202010-05-04 15:01:34 -0700797 return -1;
Giridhar Malavalia9083012010-04-12 17:59:55 -0700798
Giridhar Malavalif1af6202010-05-04 15:01:34 -0700799 addr = mem_ptr;
800 addr += start & (PAGE_SIZE - 1);
801 write_lock_irqsave(&ha->hw_lock, flags);
Giridhar Malavalia9083012010-04-12 17:59:55 -0700802
803 switch (size) {
804 case 1:
805 writeb(*(u8 *)data, addr);
806 break;
807 case 2:
808 writew(*(u16 *)data, addr);
809 break;
810 case 4:
811 writel(*(u32 *)data, addr);
812 break;
813 case 8:
814 writeq(*(u64 *)data, addr);
815 break;
816 default:
817 ret = -1;
818 break;
819 }
820 write_unlock_irqrestore(&ha->hw_lock, flags);
821 if (mem_ptr)
822 iounmap(mem_ptr);
823 return ret;
824}
825
Giridhar Malavalia9083012010-04-12 17:59:55 -0700826#define MTU_FUDGE_FACTOR 100
Giridhar Malavali77e334d2010-09-03 15:20:52 -0700827static unsigned long
828qla82xx_decode_crb_addr(unsigned long addr)
Giridhar Malavalia9083012010-04-12 17:59:55 -0700829{
830 int i;
831 unsigned long base_addr, offset, pci_base;
832
833 if (!qla82xx_crb_table_initialized)
834 qla82xx_crb_addr_transform_setup();
835
836 pci_base = ADDR_ERROR;
837 base_addr = addr & 0xfff00000;
838 offset = addr & 0x000fffff;
839
840 for (i = 0; i < MAX_CRB_XFORM; i++) {
841 if (crb_addr_xform[i] == base_addr) {
842 pci_base = i << 20;
843 break;
844 }
845 }
846 if (pci_base == ADDR_ERROR)
847 return pci_base;
848 return pci_base + offset;
849}
850
851static long rom_max_timeout = 100;
852static long qla82xx_rom_lock_timeout = 100;
853
Giridhar Malavali77e334d2010-09-03 15:20:52 -0700854static int
Giridhar Malavalia9083012010-04-12 17:59:55 -0700855qla82xx_rom_lock(struct qla_hw_data *ha)
856{
857 int done = 0, timeout = 0;
Saurav Kashyap6c315552013-02-08 01:57:53 -0500858 uint32_t lock_owner = 0;
Atul Deshmukh27f4b722014-04-11 16:54:26 -0400859 scsi_qla_host_t *vha = pci_get_drvdata(ha->pdev);
Giridhar Malavalia9083012010-04-12 17:59:55 -0700860
861 while (!done) {
862 /* acquire semaphore2 from PCI HW block */
863 done = qla82xx_rd_32(ha, QLA82XX_PCIE_REG(PCIE_SEM2_LOCK));
864 if (done == 1)
865 break;
Saurav Kashyap6c315552013-02-08 01:57:53 -0500866 if (timeout >= qla82xx_rom_lock_timeout) {
867 lock_owner = qla82xx_rd_32(ha, QLA82XX_ROM_LOCK_ID);
Sawan Chandak7ab3d962014-09-25 05:17:04 -0400868 ql_dbg(ql_dbg_p3p, vha, 0xb157,
Atul Deshmukh27f4b722014-04-11 16:54:26 -0400869 "%s: Simultaneous flash access by following ports, active port = %d: accessing port = %d",
870 __func__, ha->portnum, lock_owner);
Giridhar Malavalia9083012010-04-12 17:59:55 -0700871 return -1;
Saurav Kashyap6c315552013-02-08 01:57:53 -0500872 }
Giridhar Malavalia9083012010-04-12 17:59:55 -0700873 timeout++;
874 }
Hiral Patel4babb902014-04-11 16:54:19 -0400875 qla82xx_wr_32(ha, QLA82XX_ROM_LOCK_ID, ha->portnum);
Giridhar Malavalia9083012010-04-12 17:59:55 -0700876 return 0;
877}
878
Chad Dupuisd652e092011-05-10 11:30:10 -0700879static void
880qla82xx_rom_unlock(struct qla_hw_data *ha)
881{
Hiral Patel4babb902014-04-11 16:54:19 -0400882 qla82xx_wr_32(ha, QLA82XX_ROM_LOCK_ID, 0xffffffff);
Chad Dupuisd652e092011-05-10 11:30:10 -0700883 qla82xx_rd_32(ha, QLA82XX_PCIE_REG(PCIE_SEM2_UNLOCK));
884}
885
Giridhar Malavali77e334d2010-09-03 15:20:52 -0700886static int
Giridhar Malavalia9083012010-04-12 17:59:55 -0700887qla82xx_wait_rom_busy(struct qla_hw_data *ha)
888{
889 long timeout = 0;
890 long done = 0 ;
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700891 scsi_qla_host_t *vha = pci_get_drvdata(ha->pdev);
Giridhar Malavalia9083012010-04-12 17:59:55 -0700892
893 while (done == 0) {
894 done = qla82xx_rd_32(ha, QLA82XX_ROMUSB_GLB_STATUS);
895 done &= 4;
896 timeout++;
897 if (timeout >= rom_max_timeout) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700898 ql_dbg(ql_dbg_p3p, vha, 0xb00a,
899 "%s: Timeout reached waiting for rom busy.\n",
900 QLA2XXX_DRIVER_NAME);
Giridhar Malavalia9083012010-04-12 17:59:55 -0700901 return -1;
902 }
903 }
904 return 0;
905}
906
Giridhar Malavali77e334d2010-09-03 15:20:52 -0700907static int
Giridhar Malavalia9083012010-04-12 17:59:55 -0700908qla82xx_wait_rom_done(struct qla_hw_data *ha)
909{
910 long timeout = 0;
911 long done = 0 ;
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700912 scsi_qla_host_t *vha = pci_get_drvdata(ha->pdev);
Giridhar Malavalia9083012010-04-12 17:59:55 -0700913
914 while (done == 0) {
915 done = qla82xx_rd_32(ha, QLA82XX_ROMUSB_GLB_STATUS);
916 done &= 2;
917 timeout++;
918 if (timeout >= rom_max_timeout) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700919 ql_dbg(ql_dbg_p3p, vha, 0xb00b,
920 "%s: Timeout reached waiting for rom done.\n",
921 QLA2XXX_DRIVER_NAME);
Giridhar Malavalia9083012010-04-12 17:59:55 -0700922 return -1;
923 }
924 }
925 return 0;
926}
927
Saurav Kashyapfa492632012-11-21 02:40:29 -0500928static int
Chad Dupuis2b29d962012-02-09 11:15:41 -0800929qla82xx_md_rw_32(struct qla_hw_data *ha, uint32_t off, u32 data, uint8_t flag)
930{
931 uint32_t off_value, rval = 0;
932
Bart Van Assche8dfa4b5a2015-07-09 07:24:50 -0700933 WRT_REG_DWORD(CRB_WINDOW_2M + ha->nx_pcibase, off & 0xFFFF0000);
Chad Dupuis2b29d962012-02-09 11:15:41 -0800934
935 /* Read back value to make sure write has gone through */
Bart Van Assche8dfa4b5a2015-07-09 07:24:50 -0700936 RD_REG_DWORD(CRB_WINDOW_2M + ha->nx_pcibase);
Chad Dupuis2b29d962012-02-09 11:15:41 -0800937 off_value = (off & 0x0000FFFF);
938
939 if (flag)
Bart Van Assche8dfa4b5a2015-07-09 07:24:50 -0700940 WRT_REG_DWORD(off_value + CRB_INDIRECT_2M + ha->nx_pcibase,
941 data);
Chad Dupuis2b29d962012-02-09 11:15:41 -0800942 else
Bart Van Assche8dfa4b5a2015-07-09 07:24:50 -0700943 rval = RD_REG_DWORD(off_value + CRB_INDIRECT_2M +
944 ha->nx_pcibase);
Chad Dupuis2b29d962012-02-09 11:15:41 -0800945
946 return rval;
947}
948
Giridhar Malavali77e334d2010-09-03 15:20:52 -0700949static int
Giridhar Malavalia9083012010-04-12 17:59:55 -0700950qla82xx_do_rom_fast_read(struct qla_hw_data *ha, int addr, int *valp)
951{
Chad Dupuis2b29d962012-02-09 11:15:41 -0800952 /* Dword reads to flash. */
953 qla82xx_md_rw_32(ha, MD_DIRECT_ROM_WINDOW, (addr & 0xFFFF0000), 1);
954 *valp = qla82xx_md_rw_32(ha, MD_DIRECT_ROM_READ_BASE +
955 (addr & 0x0000FFFF), 0, 0);
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700956
Giridhar Malavalia9083012010-04-12 17:59:55 -0700957 return 0;
958}
959
Giridhar Malavali77e334d2010-09-03 15:20:52 -0700960static int
Giridhar Malavalia9083012010-04-12 17:59:55 -0700961qla82xx_rom_fast_read(struct qla_hw_data *ha, int addr, int *valp)
962{
963 int ret, loops = 0;
Hiral Patel4babb902014-04-11 16:54:19 -0400964 uint32_t lock_owner = 0;
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700965 scsi_qla_host_t *vha = pci_get_drvdata(ha->pdev);
Giridhar Malavalia9083012010-04-12 17:59:55 -0700966
967 while ((qla82xx_rom_lock(ha) != 0) && (loops < 50000)) {
968 udelay(100);
969 schedule();
970 loops++;
971 }
972 if (loops >= 50000) {
Hiral Patel4babb902014-04-11 16:54:19 -0400973 lock_owner = qla82xx_rd_32(ha, QLA82XX_ROM_LOCK_ID);
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700974 ql_log(ql_log_fatal, vha, 0x00b9,
Hiral Patel4babb902014-04-11 16:54:19 -0400975 "Failed to acquire SEM2 lock, Lock Owner %u.\n",
976 lock_owner);
Giridhar Malavalia9083012010-04-12 17:59:55 -0700977 return -1;
978 }
979 ret = qla82xx_do_rom_fast_read(ha, addr, valp);
Chad Dupuisd652e092011-05-10 11:30:10 -0700980 qla82xx_rom_unlock(ha);
Giridhar Malavalia9083012010-04-12 17:59:55 -0700981 return ret;
982}
983
Giridhar Malavali77e334d2010-09-03 15:20:52 -0700984static int
Giridhar Malavalia9083012010-04-12 17:59:55 -0700985qla82xx_read_status_reg(struct qla_hw_data *ha, uint32_t *val)
986{
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700987 scsi_qla_host_t *vha = pci_get_drvdata(ha->pdev);
Giridhar Malavalia9083012010-04-12 17:59:55 -0700988 qla82xx_wr_32(ha, QLA82XX_ROMUSB_ROM_INSTR_OPCODE, M25P_INSTR_RDSR);
989 qla82xx_wait_rom_busy(ha);
990 if (qla82xx_wait_rom_done(ha)) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700991 ql_log(ql_log_warn, vha, 0xb00c,
992 "Error waiting for rom done.\n");
Giridhar Malavalia9083012010-04-12 17:59:55 -0700993 return -1;
994 }
995 *val = qla82xx_rd_32(ha, QLA82XX_ROMUSB_ROM_RDATA);
996 return 0;
997}
998
Giridhar Malavali77e334d2010-09-03 15:20:52 -0700999static int
Giridhar Malavalia9083012010-04-12 17:59:55 -07001000qla82xx_flash_wait_write_finish(struct qla_hw_data *ha)
1001{
1002 long timeout = 0;
1003 uint32_t done = 1 ;
1004 uint32_t val;
1005 int ret = 0;
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001006 scsi_qla_host_t *vha = pci_get_drvdata(ha->pdev);
Giridhar Malavalia9083012010-04-12 17:59:55 -07001007
1008 qla82xx_wr_32(ha, QLA82XX_ROMUSB_ROM_ABYTE_CNT, 0);
1009 while ((done != 0) && (ret == 0)) {
1010 ret = qla82xx_read_status_reg(ha, &val);
1011 done = val & 1;
1012 timeout++;
1013 udelay(10);
1014 cond_resched();
1015 if (timeout >= 50000) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001016 ql_log(ql_log_warn, vha, 0xb00d,
1017 "Timeout reached waiting for write finish.\n");
Giridhar Malavalia9083012010-04-12 17:59:55 -07001018 return -1;
1019 }
1020 }
1021 return ret;
1022}
1023
Giridhar Malavali77e334d2010-09-03 15:20:52 -07001024static int
Giridhar Malavalia9083012010-04-12 17:59:55 -07001025qla82xx_flash_set_write_enable(struct qla_hw_data *ha)
1026{
1027 uint32_t val;
1028 qla82xx_wait_rom_busy(ha);
1029 qla82xx_wr_32(ha, QLA82XX_ROMUSB_ROM_ABYTE_CNT, 0);
1030 qla82xx_wr_32(ha, QLA82XX_ROMUSB_ROM_INSTR_OPCODE, M25P_INSTR_WREN);
1031 qla82xx_wait_rom_busy(ha);
1032 if (qla82xx_wait_rom_done(ha))
1033 return -1;
1034 if (qla82xx_read_status_reg(ha, &val) != 0)
1035 return -1;
1036 if ((val & 2) != 2)
1037 return -1;
1038 return 0;
1039}
1040
Giridhar Malavali77e334d2010-09-03 15:20:52 -07001041static int
Giridhar Malavalia9083012010-04-12 17:59:55 -07001042qla82xx_write_status_reg(struct qla_hw_data *ha, uint32_t val)
1043{
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001044 scsi_qla_host_t *vha = pci_get_drvdata(ha->pdev);
Giridhar Malavalia9083012010-04-12 17:59:55 -07001045 if (qla82xx_flash_set_write_enable(ha))
1046 return -1;
1047 qla82xx_wr_32(ha, QLA82XX_ROMUSB_ROM_WDATA, val);
1048 qla82xx_wr_32(ha, QLA82XX_ROMUSB_ROM_INSTR_OPCODE, 0x1);
1049 if (qla82xx_wait_rom_done(ha)) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001050 ql_log(ql_log_warn, vha, 0xb00e,
1051 "Error waiting for rom done.\n");
Giridhar Malavalia9083012010-04-12 17:59:55 -07001052 return -1;
1053 }
1054 return qla82xx_flash_wait_write_finish(ha);
1055}
1056
Giridhar Malavali77e334d2010-09-03 15:20:52 -07001057static int
Giridhar Malavalia9083012010-04-12 17:59:55 -07001058qla82xx_write_disable_flash(struct qla_hw_data *ha)
1059{
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001060 scsi_qla_host_t *vha = pci_get_drvdata(ha->pdev);
Giridhar Malavalia9083012010-04-12 17:59:55 -07001061 qla82xx_wr_32(ha, QLA82XX_ROMUSB_ROM_INSTR_OPCODE, M25P_INSTR_WRDI);
1062 if (qla82xx_wait_rom_done(ha)) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001063 ql_log(ql_log_warn, vha, 0xb00f,
1064 "Error waiting for rom done.\n");
Giridhar Malavalia9083012010-04-12 17:59:55 -07001065 return -1;
1066 }
1067 return 0;
1068}
1069
Giridhar Malavali77e334d2010-09-03 15:20:52 -07001070static int
Giridhar Malavalia9083012010-04-12 17:59:55 -07001071ql82xx_rom_lock_d(struct qla_hw_data *ha)
1072{
1073 int loops = 0;
Hiral Patel4babb902014-04-11 16:54:19 -04001074 uint32_t lock_owner = 0;
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001075 scsi_qla_host_t *vha = pci_get_drvdata(ha->pdev);
1076
Giridhar Malavalia9083012010-04-12 17:59:55 -07001077 while ((qla82xx_rom_lock(ha) != 0) && (loops < 50000)) {
1078 udelay(100);
1079 cond_resched();
1080 loops++;
1081 }
1082 if (loops >= 50000) {
Hiral Patel4babb902014-04-11 16:54:19 -04001083 lock_owner = qla82xx_rd_32(ha, QLA82XX_ROM_LOCK_ID);
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001084 ql_log(ql_log_warn, vha, 0xb010,
Hiral Patel4babb902014-04-11 16:54:19 -04001085 "ROM lock failed, Lock Owner %u.\n", lock_owner);
Giridhar Malavalia9083012010-04-12 17:59:55 -07001086 return -1;
1087 }
Jesper Juhlcd6dbb02011-11-20 22:34:15 +01001088 return 0;
Giridhar Malavalia9083012010-04-12 17:59:55 -07001089}
1090
Giridhar Malavali77e334d2010-09-03 15:20:52 -07001091static int
Giridhar Malavalia9083012010-04-12 17:59:55 -07001092qla82xx_write_flash_dword(struct qla_hw_data *ha, uint32_t flashaddr,
1093 uint32_t data)
1094{
1095 int ret = 0;
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001096 scsi_qla_host_t *vha = pci_get_drvdata(ha->pdev);
Giridhar Malavalia9083012010-04-12 17:59:55 -07001097
1098 ret = ql82xx_rom_lock_d(ha);
1099 if (ret < 0) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001100 ql_log(ql_log_warn, vha, 0xb011,
1101 "ROM lock failed.\n");
Giridhar Malavalia9083012010-04-12 17:59:55 -07001102 return ret;
1103 }
1104
1105 if (qla82xx_flash_set_write_enable(ha))
1106 goto done_write;
1107
1108 qla82xx_wr_32(ha, QLA82XX_ROMUSB_ROM_WDATA, data);
1109 qla82xx_wr_32(ha, QLA82XX_ROMUSB_ROM_ADDRESS, flashaddr);
1110 qla82xx_wr_32(ha, QLA82XX_ROMUSB_ROM_ABYTE_CNT, 3);
1111 qla82xx_wr_32(ha, QLA82XX_ROMUSB_ROM_INSTR_OPCODE, M25P_INSTR_PP);
1112 qla82xx_wait_rom_busy(ha);
1113 if (qla82xx_wait_rom_done(ha)) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001114 ql_log(ql_log_warn, vha, 0xb012,
1115 "Error waiting for rom done.\n");
Giridhar Malavalia9083012010-04-12 17:59:55 -07001116 ret = -1;
1117 goto done_write;
1118 }
1119
1120 ret = qla82xx_flash_wait_write_finish(ha);
1121
1122done_write:
Chad Dupuisd652e092011-05-10 11:30:10 -07001123 qla82xx_rom_unlock(ha);
Giridhar Malavalia9083012010-04-12 17:59:55 -07001124 return ret;
1125}
1126
1127/* This routine does CRB initialize sequence
1128 * to put the ISP into operational state
1129 */
Giridhar Malavali77e334d2010-09-03 15:20:52 -07001130static int
1131qla82xx_pinit_from_rom(scsi_qla_host_t *vha)
Giridhar Malavalia9083012010-04-12 17:59:55 -07001132{
1133 int addr, val;
1134 int i ;
1135 struct crb_addr_pair *buf;
1136 unsigned long off;
1137 unsigned offset, n;
1138 struct qla_hw_data *ha = vha->hw;
1139
1140 struct crb_addr_pair {
1141 long addr;
1142 long data;
1143 };
1144
Masanari Iidaa7201012012-11-21 02:40:42 -05001145 /* Halt all the individual PEGs and other blocks of the ISP */
Giridhar Malavalia9083012010-04-12 17:59:55 -07001146 qla82xx_rom_lock(ha);
Madhuranath Iyengarc9e8fd52010-12-21 16:00:19 -08001147
Giridhar Malavali02be2212011-03-30 11:46:24 -07001148 /* disable all I2Q */
1149 qla82xx_wr_32(ha, QLA82XX_CRB_I2Q + 0x10, 0x0);
1150 qla82xx_wr_32(ha, QLA82XX_CRB_I2Q + 0x14, 0x0);
1151 qla82xx_wr_32(ha, QLA82XX_CRB_I2Q + 0x18, 0x0);
1152 qla82xx_wr_32(ha, QLA82XX_CRB_I2Q + 0x1c, 0x0);
1153 qla82xx_wr_32(ha, QLA82XX_CRB_I2Q + 0x20, 0x0);
1154 qla82xx_wr_32(ha, QLA82XX_CRB_I2Q + 0x24, 0x0);
1155
1156 /* disable all niu interrupts */
Madhuranath Iyengarc9e8fd52010-12-21 16:00:19 -08001157 qla82xx_wr_32(ha, QLA82XX_CRB_NIU + 0x40, 0xff);
1158 /* disable xge rx/tx */
1159 qla82xx_wr_32(ha, QLA82XX_CRB_NIU + 0x70000, 0x00);
1160 /* disable xg1 rx/tx */
1161 qla82xx_wr_32(ha, QLA82XX_CRB_NIU + 0x80000, 0x00);
Giridhar Malavali02be2212011-03-30 11:46:24 -07001162 /* disable sideband mac */
1163 qla82xx_wr_32(ha, QLA82XX_CRB_NIU + 0x90000, 0x00);
1164 /* disable ap0 mac */
1165 qla82xx_wr_32(ha, QLA82XX_CRB_NIU + 0xa0000, 0x00);
1166 /* disable ap1 mac */
1167 qla82xx_wr_32(ha, QLA82XX_CRB_NIU + 0xb0000, 0x00);
Madhuranath Iyengarc9e8fd52010-12-21 16:00:19 -08001168
1169 /* halt sre */
1170 val = qla82xx_rd_32(ha, QLA82XX_CRB_SRE + 0x1000);
1171 qla82xx_wr_32(ha, QLA82XX_CRB_SRE + 0x1000, val & (~(0x1)));
1172
1173 /* halt epg */
1174 qla82xx_wr_32(ha, QLA82XX_CRB_EPG + 0x1300, 0x1);
1175
1176 /* halt timers */
1177 qla82xx_wr_32(ha, QLA82XX_CRB_TIMER + 0x0, 0x0);
1178 qla82xx_wr_32(ha, QLA82XX_CRB_TIMER + 0x8, 0x0);
1179 qla82xx_wr_32(ha, QLA82XX_CRB_TIMER + 0x10, 0x0);
1180 qla82xx_wr_32(ha, QLA82XX_CRB_TIMER + 0x18, 0x0);
1181 qla82xx_wr_32(ha, QLA82XX_CRB_TIMER + 0x100, 0x0);
Giridhar Malavali02be2212011-03-30 11:46:24 -07001182 qla82xx_wr_32(ha, QLA82XX_CRB_TIMER + 0x200, 0x0);
Madhuranath Iyengarc9e8fd52010-12-21 16:00:19 -08001183
1184 /* halt pegs */
1185 qla82xx_wr_32(ha, QLA82XX_CRB_PEG_NET_0 + 0x3c, 1);
1186 qla82xx_wr_32(ha, QLA82XX_CRB_PEG_NET_1 + 0x3c, 1);
1187 qla82xx_wr_32(ha, QLA82XX_CRB_PEG_NET_2 + 0x3c, 1);
1188 qla82xx_wr_32(ha, QLA82XX_CRB_PEG_NET_3 + 0x3c, 1);
1189 qla82xx_wr_32(ha, QLA82XX_CRB_PEG_NET_4 + 0x3c, 1);
Giridhar Malavali02be2212011-03-30 11:46:24 -07001190 msleep(20);
Madhuranath Iyengarc9e8fd52010-12-21 16:00:19 -08001191
1192 /* big hammer */
Giridhar Malavalia9083012010-04-12 17:59:55 -07001193 if (test_bit(ABORT_ISP_ACTIVE, &vha->dpc_flags))
1194 /* don't reset CAM block on reset */
1195 qla82xx_wr_32(ha, QLA82XX_ROMUSB_GLB_SW_RESET, 0xfeffffff);
1196 else
1197 qla82xx_wr_32(ha, QLA82XX_ROMUSB_GLB_SW_RESET, 0xffffffff);
Chad Dupuisd652e092011-05-10 11:30:10 -07001198 qla82xx_rom_unlock(ha);
Giridhar Malavalia9083012010-04-12 17:59:55 -07001199
1200 /* Read the signature value from the flash.
1201 * Offset 0: Contain signature (0xcafecafe)
1202 * Offset 4: Offset and number of addr/value pairs
1203 * that present in CRB initialize sequence
1204 */
1205 if (qla82xx_rom_fast_read(ha, 0, &n) != 0 || n != 0xcafecafeUL ||
1206 qla82xx_rom_fast_read(ha, 4, &n) != 0) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001207 ql_log(ql_log_fatal, vha, 0x006e,
1208 "Error Reading crb_init area: n: %08x.\n", n);
Giridhar Malavalia9083012010-04-12 17:59:55 -07001209 return -1;
1210 }
1211
1212 /* Offset in flash = lower 16 bits
Saurav Kashyap00adc9a2012-05-15 14:34:22 -04001213 * Number of entries = upper 16 bits
Giridhar Malavalia9083012010-04-12 17:59:55 -07001214 */
1215 offset = n & 0xffffU;
1216 n = (n >> 16) & 0xffffU;
1217
Saurav Kashyap00adc9a2012-05-15 14:34:22 -04001218 /* number of addr/value pair should not exceed 1024 entries */
Giridhar Malavalia9083012010-04-12 17:59:55 -07001219 if (n >= 1024) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001220 ql_log(ql_log_fatal, vha, 0x0071,
1221 "Card flash not initialized:n=0x%x.\n", n);
Giridhar Malavalia9083012010-04-12 17:59:55 -07001222 return -1;
1223 }
1224
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001225 ql_log(ql_log_info, vha, 0x0072,
1226 "%d CRB init values found in ROM.\n", n);
Giridhar Malavalia9083012010-04-12 17:59:55 -07001227
1228 buf = kmalloc(n * sizeof(struct crb_addr_pair), GFP_KERNEL);
1229 if (buf == NULL) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001230 ql_log(ql_log_fatal, vha, 0x010c,
1231 "Unable to allocate memory.\n");
Bart Van Assche5cfe8d52016-03-30 16:25:21 -07001232 return -ENOMEM;
Giridhar Malavalia9083012010-04-12 17:59:55 -07001233 }
1234
1235 for (i = 0; i < n; i++) {
1236 if (qla82xx_rom_fast_read(ha, 8*i + 4*offset, &val) != 0 ||
1237 qla82xx_rom_fast_read(ha, 8*i + 4*offset + 4, &addr) != 0) {
1238 kfree(buf);
1239 return -1;
1240 }
1241
1242 buf[i].addr = addr;
1243 buf[i].data = val;
1244 }
1245
1246 for (i = 0; i < n; i++) {
1247 /* Translate internal CRB initialization
1248 * address to PCI bus address
1249 */
1250 off = qla82xx_decode_crb_addr((unsigned long)buf[i].addr) +
1251 QLA82XX_PCI_CRBSPACE;
1252 /* Not all CRB addr/value pair to be written,
1253 * some of them are skipped
1254 */
1255
1256 /* skipping cold reboot MAGIC */
1257 if (off == QLA82XX_CAM_RAM(0x1fc))
1258 continue;
1259
1260 /* do not reset PCI */
1261 if (off == (ROMUSB_GLB + 0xbc))
1262 continue;
1263
1264 /* skip core clock, so that firmware can increase the clock */
1265 if (off == (ROMUSB_GLB + 0xc8))
1266 continue;
1267
1268 /* skip the function enable register */
1269 if (off == QLA82XX_PCIE_REG(PCIE_SETUP_FUNCTION))
1270 continue;
1271
1272 if (off == QLA82XX_PCIE_REG(PCIE_SETUP_FUNCTION2))
1273 continue;
1274
1275 if ((off & 0x0ff00000) == QLA82XX_CRB_SMB)
1276 continue;
1277
1278 if ((off & 0x0ff00000) == QLA82XX_CRB_DDR_NET)
1279 continue;
1280
1281 if (off == ADDR_ERROR) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001282 ql_log(ql_log_fatal, vha, 0x0116,
Masanari Iidad939be32015-02-27 23:52:31 +09001283 "Unknown addr: 0x%08lx.\n", buf[i].addr);
Giridhar Malavalia9083012010-04-12 17:59:55 -07001284 continue;
1285 }
1286
Giridhar Malavalia9083012010-04-12 17:59:55 -07001287 qla82xx_wr_32(ha, off, buf[i].data);
1288
1289 /* ISP requires much bigger delay to settle down,
1290 * else crb_window returns 0xffffffff
1291 */
1292 if (off == QLA82XX_ROMUSB_GLB_SW_RESET)
1293 msleep(1000);
1294
1295 /* ISP requires millisec delay between
1296 * successive CRB register updation
1297 */
1298 msleep(1);
1299 }
1300
1301 kfree(buf);
1302
1303 /* Resetting the data and instruction cache */
1304 qla82xx_wr_32(ha, QLA82XX_CRB_PEG_NET_D+0xec, 0x1e);
1305 qla82xx_wr_32(ha, QLA82XX_CRB_PEG_NET_D+0x4c, 8);
1306 qla82xx_wr_32(ha, QLA82XX_CRB_PEG_NET_I+0x4c, 8);
1307
1308 /* Clear all protocol processing engines */
1309 qla82xx_wr_32(ha, QLA82XX_CRB_PEG_NET_0+0x8, 0);
1310 qla82xx_wr_32(ha, QLA82XX_CRB_PEG_NET_0+0xc, 0);
1311 qla82xx_wr_32(ha, QLA82XX_CRB_PEG_NET_1+0x8, 0);
1312 qla82xx_wr_32(ha, QLA82XX_CRB_PEG_NET_1+0xc, 0);
1313 qla82xx_wr_32(ha, QLA82XX_CRB_PEG_NET_2+0x8, 0);
1314 qla82xx_wr_32(ha, QLA82XX_CRB_PEG_NET_2+0xc, 0);
1315 qla82xx_wr_32(ha, QLA82XX_CRB_PEG_NET_3+0x8, 0);
1316 qla82xx_wr_32(ha, QLA82XX_CRB_PEG_NET_3+0xc, 0);
1317 return 0;
1318}
1319
Giridhar Malavali77e334d2010-09-03 15:20:52 -07001320static int
Giridhar Malavali77e334d2010-09-03 15:20:52 -07001321qla82xx_pci_mem_write_2M(struct qla_hw_data *ha,
1322 u64 off, void *data, int size)
1323{
1324 int i, j, ret = 0, loop, sz[2], off0;
1325 int scale, shift_amount, startword;
1326 uint32_t temp;
1327 uint64_t off8, mem_crb, tmpw, word[2] = {0, 0};
1328
1329 /*
1330 * If not MN, go check for MS or invalid.
1331 */
1332 if (off >= QLA82XX_ADDR_QDR_NET && off <= QLA82XX_P3_ADDR_QDR_NET_MAX)
1333 mem_crb = QLA82XX_CRB_QDR_NET;
1334 else {
1335 mem_crb = QLA82XX_CRB_DDR_NET;
1336 if (qla82xx_pci_mem_bound_check(ha, off, size) == 0)
1337 return qla82xx_pci_mem_write_direct(ha,
1338 off, data, size);
1339 }
1340
1341 off0 = off & 0x7;
1342 sz[0] = (size < (8 - off0)) ? size : (8 - off0);
1343 sz[1] = size - sz[0];
1344
1345 off8 = off & 0xfffffff0;
1346 loop = (((off & 0xf) + size - 1) >> 4) + 1;
1347 shift_amount = 4;
1348 scale = 2;
1349 startword = (off & 0xf)/8;
1350
1351 for (i = 0; i < loop; i++) {
1352 if (qla82xx_pci_mem_read_2M(ha, off8 +
1353 (i << shift_amount), &word[i * scale], 8))
1354 return -1;
1355 }
1356
1357 switch (size) {
1358 case 1:
1359 tmpw = *((uint8_t *)data);
1360 break;
1361 case 2:
1362 tmpw = *((uint16_t *)data);
1363 break;
1364 case 4:
1365 tmpw = *((uint32_t *)data);
1366 break;
1367 case 8:
1368 default:
1369 tmpw = *((uint64_t *)data);
1370 break;
1371 }
1372
1373 if (sz[0] == 8) {
1374 word[startword] = tmpw;
1375 } else {
1376 word[startword] &=
1377 ~((~(~0ULL << (sz[0] * 8))) << (off0 * 8));
1378 word[startword] |= tmpw << (off0 * 8);
1379 }
1380 if (sz[1] != 0) {
1381 word[startword+1] &= ~(~0ULL << (sz[1] * 8));
1382 word[startword+1] |= tmpw >> (sz[0] * 8);
1383 }
1384
Giridhar Malavali77e334d2010-09-03 15:20:52 -07001385 for (i = 0; i < loop; i++) {
1386 temp = off8 + (i << shift_amount);
1387 qla82xx_wr_32(ha, mem_crb+MIU_TEST_AGT_ADDR_LO, temp);
1388 temp = 0;
1389 qla82xx_wr_32(ha, mem_crb+MIU_TEST_AGT_ADDR_HI, temp);
1390 temp = word[i * scale] & 0xffffffff;
1391 qla82xx_wr_32(ha, mem_crb+MIU_TEST_AGT_WRDATA_LO, temp);
1392 temp = (word[i * scale] >> 32) & 0xffffffff;
1393 qla82xx_wr_32(ha, mem_crb+MIU_TEST_AGT_WRDATA_HI, temp);
1394 temp = word[i*scale + 1] & 0xffffffff;
1395 qla82xx_wr_32(ha, mem_crb +
1396 MIU_TEST_AGT_WRDATA_UPPER_LO, temp);
1397 temp = (word[i*scale + 1] >> 32) & 0xffffffff;
1398 qla82xx_wr_32(ha, mem_crb +
1399 MIU_TEST_AGT_WRDATA_UPPER_HI, temp);
1400
1401 temp = MIU_TA_CTL_ENABLE | MIU_TA_CTL_WRITE;
1402 qla82xx_wr_32(ha, mem_crb + MIU_TEST_AGT_CTRL, temp);
1403 temp = MIU_TA_CTL_START | MIU_TA_CTL_ENABLE | MIU_TA_CTL_WRITE;
1404 qla82xx_wr_32(ha, mem_crb + MIU_TEST_AGT_CTRL, temp);
1405
1406 for (j = 0; j < MAX_CTL_CHECK; j++) {
1407 temp = qla82xx_rd_32(ha, mem_crb + MIU_TEST_AGT_CTRL);
1408 if ((temp & MIU_TA_CTL_BUSY) == 0)
1409 break;
1410 }
1411
1412 if (j >= MAX_CTL_CHECK) {
1413 if (printk_ratelimit())
1414 dev_err(&ha->pdev->dev,
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001415 "failed to write through agent.\n");
Giridhar Malavali77e334d2010-09-03 15:20:52 -07001416 ret = -1;
1417 break;
1418 }
1419 }
1420
1421 return ret;
1422}
1423
1424static int
Giridhar Malavalia9083012010-04-12 17:59:55 -07001425qla82xx_fw_load_from_flash(struct qla_hw_data *ha)
1426{
1427 int i;
1428 long size = 0;
Harish Zunjarrao9c2b2972010-05-28 15:08:23 -07001429 long flashaddr = ha->flt_region_bootload << 2;
1430 long memaddr = BOOTLD_START;
Giridhar Malavalia9083012010-04-12 17:59:55 -07001431 u64 data;
1432 u32 high, low;
1433 size = (IMAGE_START - BOOTLD_START) / 8;
1434
1435 for (i = 0; i < size; i++) {
1436 if ((qla82xx_rom_fast_read(ha, flashaddr, (int *)&low)) ||
1437 (qla82xx_rom_fast_read(ha, flashaddr + 4, (int *)&high))) {
1438 return -1;
1439 }
1440 data = ((u64)high << 32) | low ;
1441 qla82xx_pci_mem_write_2M(ha, memaddr, &data, 8);
1442 flashaddr += 8;
1443 memaddr += 8;
1444
1445 if (i % 0x1000 == 0)
1446 msleep(1);
1447 }
1448 udelay(100);
1449 read_lock(&ha->hw_lock);
Giridhar Malavali37113332010-07-23 15:28:34 +05001450 qla82xx_wr_32(ha, QLA82XX_CRB_PEG_NET_0 + 0x18, 0x1020);
1451 qla82xx_wr_32(ha, QLA82XX_ROMUSB_GLB_SW_RESET, 0x80001e);
Giridhar Malavalia9083012010-04-12 17:59:55 -07001452 read_unlock(&ha->hw_lock);
1453 return 0;
1454}
1455
1456int
1457qla82xx_pci_mem_read_2M(struct qla_hw_data *ha,
1458 u64 off, void *data, int size)
1459{
1460 int i, j = 0, k, start, end, loop, sz[2], off0[2];
1461 int shift_amount;
1462 uint32_t temp;
1463 uint64_t off8, val, mem_crb, word[2] = {0, 0};
1464
1465 /*
1466 * If not MN, go check for MS or invalid.
1467 */
1468
1469 if (off >= QLA82XX_ADDR_QDR_NET && off <= QLA82XX_P3_ADDR_QDR_NET_MAX)
1470 mem_crb = QLA82XX_CRB_QDR_NET;
1471 else {
1472 mem_crb = QLA82XX_CRB_DDR_NET;
1473 if (qla82xx_pci_mem_bound_check(ha, off, size) == 0)
1474 return qla82xx_pci_mem_read_direct(ha,
1475 off, data, size);
1476 }
1477
Giridhar Malavali37113332010-07-23 15:28:34 +05001478 off8 = off & 0xfffffff0;
1479 off0[0] = off & 0xf;
1480 sz[0] = (size < (16 - off0[0])) ? size : (16 - off0[0]);
1481 shift_amount = 4;
Giridhar Malavalia9083012010-04-12 17:59:55 -07001482 loop = ((off0[0] + size - 1) >> shift_amount) + 1;
1483 off0[1] = 0;
1484 sz[1] = size - sz[0];
1485
Giridhar Malavalia9083012010-04-12 17:59:55 -07001486 for (i = 0; i < loop; i++) {
1487 temp = off8 + (i << shift_amount);
1488 qla82xx_wr_32(ha, mem_crb + MIU_TEST_AGT_ADDR_LO, temp);
1489 temp = 0;
1490 qla82xx_wr_32(ha, mem_crb + MIU_TEST_AGT_ADDR_HI, temp);
1491 temp = MIU_TA_CTL_ENABLE;
1492 qla82xx_wr_32(ha, mem_crb + MIU_TEST_AGT_CTRL, temp);
1493 temp = MIU_TA_CTL_START | MIU_TA_CTL_ENABLE;
1494 qla82xx_wr_32(ha, mem_crb + MIU_TEST_AGT_CTRL, temp);
1495
1496 for (j = 0; j < MAX_CTL_CHECK; j++) {
1497 temp = qla82xx_rd_32(ha, mem_crb + MIU_TEST_AGT_CTRL);
1498 if ((temp & MIU_TA_CTL_BUSY) == 0)
1499 break;
1500 }
1501
1502 if (j >= MAX_CTL_CHECK) {
1503 if (printk_ratelimit())
1504 dev_err(&ha->pdev->dev,
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001505 "failed to read through agent.\n");
Giridhar Malavalia9083012010-04-12 17:59:55 -07001506 break;
1507 }
1508
1509 start = off0[i] >> 2;
1510 end = (off0[i] + sz[i] - 1) >> 2;
1511 for (k = start; k <= end; k++) {
1512 temp = qla82xx_rd_32(ha,
1513 mem_crb + MIU_TEST_AGT_RDDATA(k));
1514 word[i] |= ((uint64_t)temp << (32 * (k & 1)));
1515 }
1516 }
1517
Giridhar Malavalia9083012010-04-12 17:59:55 -07001518 if (j >= MAX_CTL_CHECK)
1519 return -1;
1520
1521 if ((off0[0] & 7) == 0) {
1522 val = word[0];
1523 } else {
1524 val = ((word[0] >> (off0[0] * 8)) & (~(~0ULL << (sz[0] * 8)))) |
1525 ((word[1] & (~(~0ULL << (sz[1] * 8)))) << (sz[0] * 8));
1526 }
1527
1528 switch (size) {
1529 case 1:
1530 *(uint8_t *)data = val;
1531 break;
1532 case 2:
1533 *(uint16_t *)data = val;
1534 break;
1535 case 4:
1536 *(uint32_t *)data = val;
1537 break;
1538 case 8:
1539 *(uint64_t *)data = val;
1540 break;
1541 }
1542 return 0;
1543}
1544
Giridhar Malavalia9083012010-04-12 17:59:55 -07001545
Harish Zunjarrao9c2b2972010-05-28 15:08:23 -07001546static struct qla82xx_uri_table_desc *
1547qla82xx_get_table_desc(const u8 *unirom, int section)
1548{
1549 uint32_t i;
1550 struct qla82xx_uri_table_desc *directory =
1551 (struct qla82xx_uri_table_desc *)&unirom[0];
1552 __le32 offset;
1553 __le32 tab_type;
1554 __le32 entries = cpu_to_le32(directory->num_entries);
1555
1556 for (i = 0; i < entries; i++) {
1557 offset = cpu_to_le32(directory->findex) +
1558 (i * cpu_to_le32(directory->entry_size));
1559 tab_type = cpu_to_le32(*((u32 *)&unirom[offset] + 8));
1560
1561 if (tab_type == section)
1562 return (struct qla82xx_uri_table_desc *)&unirom[offset];
1563 }
1564
1565 return NULL;
1566}
1567
1568static struct qla82xx_uri_data_desc *
1569qla82xx_get_data_desc(struct qla_hw_data *ha,
1570 u32 section, u32 idx_offset)
1571{
1572 const u8 *unirom = ha->hablob->fw->data;
1573 int idx = cpu_to_le32(*((int *)&unirom[ha->file_prd_off] + idx_offset));
1574 struct qla82xx_uri_table_desc *tab_desc = NULL;
1575 __le32 offset;
1576
1577 tab_desc = qla82xx_get_table_desc(unirom, section);
1578 if (!tab_desc)
1579 return NULL;
1580
1581 offset = cpu_to_le32(tab_desc->findex) +
1582 (cpu_to_le32(tab_desc->entry_size) * idx);
1583
1584 return (struct qla82xx_uri_data_desc *)&unirom[offset];
1585}
1586
1587static u8 *
1588qla82xx_get_bootld_offset(struct qla_hw_data *ha)
1589{
1590 u32 offset = BOOTLD_START;
1591 struct qla82xx_uri_data_desc *uri_desc = NULL;
1592
1593 if (ha->fw_type == QLA82XX_UNIFIED_ROMIMAGE) {
1594 uri_desc = qla82xx_get_data_desc(ha,
1595 QLA82XX_URI_DIR_SECT_BOOTLD, QLA82XX_URI_BOOTLD_IDX_OFF);
1596 if (uri_desc)
1597 offset = cpu_to_le32(uri_desc->findex);
1598 }
1599
1600 return (u8 *)&ha->hablob->fw->data[offset];
1601}
1602
1603static __le32
1604qla82xx_get_fw_size(struct qla_hw_data *ha)
1605{
1606 struct qla82xx_uri_data_desc *uri_desc = NULL;
1607
1608 if (ha->fw_type == QLA82XX_UNIFIED_ROMIMAGE) {
1609 uri_desc = qla82xx_get_data_desc(ha, QLA82XX_URI_DIR_SECT_FW,
1610 QLA82XX_URI_FIRMWARE_IDX_OFF);
1611 if (uri_desc)
1612 return cpu_to_le32(uri_desc->size);
1613 }
1614
1615 return cpu_to_le32(*(u32 *)&ha->hablob->fw->data[FW_SIZE_OFFSET]);
1616}
1617
1618static u8 *
1619qla82xx_get_fw_offs(struct qla_hw_data *ha)
1620{
1621 u32 offset = IMAGE_START;
1622 struct qla82xx_uri_data_desc *uri_desc = NULL;
1623
1624 if (ha->fw_type == QLA82XX_UNIFIED_ROMIMAGE) {
1625 uri_desc = qla82xx_get_data_desc(ha, QLA82XX_URI_DIR_SECT_FW,
1626 QLA82XX_URI_FIRMWARE_IDX_OFF);
1627 if (uri_desc)
1628 offset = cpu_to_le32(uri_desc->findex);
1629 }
1630
1631 return (u8 *)&ha->hablob->fw->data[offset];
1632}
1633
Giridhar Malavalia9083012010-04-12 17:59:55 -07001634/* PCI related functions */
Giridhar Malavalia9083012010-04-12 17:59:55 -07001635int qla82xx_pci_region_offset(struct pci_dev *pdev, int region)
1636{
1637 unsigned long val = 0;
1638 u32 control;
1639
1640 switch (region) {
1641 case 0:
1642 val = 0;
1643 break;
1644 case 1:
1645 pci_read_config_dword(pdev, QLA82XX_PCI_REG_MSIX_TBL, &control);
1646 val = control + QLA82XX_MSIX_TBL_SPACE;
1647 break;
1648 }
1649 return val;
1650}
1651
Giridhar Malavalia9083012010-04-12 17:59:55 -07001652
1653int
1654qla82xx_iospace_config(struct qla_hw_data *ha)
1655{
1656 uint32_t len = 0;
1657
1658 if (pci_request_regions(ha->pdev, QLA2XXX_DRIVER_NAME)) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001659 ql_log_pci(ql_log_fatal, ha->pdev, 0x000c,
1660 "Failed to reserver selected regions.\n");
Giridhar Malavalia9083012010-04-12 17:59:55 -07001661 goto iospace_error_exit;
1662 }
1663
1664 /* Use MMIO operations for all accesses. */
1665 if (!(pci_resource_flags(ha->pdev, 0) & IORESOURCE_MEM)) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001666 ql_log_pci(ql_log_fatal, ha->pdev, 0x000d,
1667 "Region #0 not an MMIO resource, aborting.\n");
Giridhar Malavalia9083012010-04-12 17:59:55 -07001668 goto iospace_error_exit;
1669 }
1670
1671 len = pci_resource_len(ha->pdev, 0);
Bart Van Assche8dfa4b5a2015-07-09 07:24:50 -07001672 ha->nx_pcibase = ioremap(pci_resource_start(ha->pdev, 0), len);
Giridhar Malavalia9083012010-04-12 17:59:55 -07001673 if (!ha->nx_pcibase) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001674 ql_log_pci(ql_log_fatal, ha->pdev, 0x000e,
1675 "Cannot remap pcibase MMIO, aborting.\n");
Giridhar Malavalia9083012010-04-12 17:59:55 -07001676 goto iospace_error_exit;
1677 }
1678
1679 /* Mapping of IO base pointer */
Atul Deshmukh7ec0eff2013-08-27 01:37:28 -04001680 if (IS_QLA8044(ha)) {
Bart Van Assche8dfa4b5a2015-07-09 07:24:50 -07001681 ha->iobase = ha->nx_pcibase;
Atul Deshmukh7ec0eff2013-08-27 01:37:28 -04001682 } else if (IS_QLA82XX(ha)) {
Bart Van Assche8dfa4b5a2015-07-09 07:24:50 -07001683 ha->iobase = ha->nx_pcibase + 0xbc000 + (ha->pdev->devfn << 11);
Atul Deshmukh7ec0eff2013-08-27 01:37:28 -04001684 }
Giridhar Malavalia9083012010-04-12 17:59:55 -07001685
1686 if (!ql2xdbwr) {
Bart Van Assche8dfa4b5a2015-07-09 07:24:50 -07001687 ha->nxdb_wr_ptr = ioremap((pci_resource_start(ha->pdev, 4) +
Giridhar Malavalia9083012010-04-12 17:59:55 -07001688 (ha->pdev->devfn << 12)), 4);
1689 if (!ha->nxdb_wr_ptr) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001690 ql_log_pci(ql_log_fatal, ha->pdev, 0x000f,
1691 "Cannot remap MMIO, aborting.\n");
Giridhar Malavalia9083012010-04-12 17:59:55 -07001692 goto iospace_error_exit;
1693 }
1694
1695 /* Mapping of IO base pointer,
1696 * door bell read and write pointer
1697 */
Bart Van Assche8dfa4b5a2015-07-09 07:24:50 -07001698 ha->nxdb_rd_ptr = ha->nx_pcibase + (512 * 1024) +
Giridhar Malavalia9083012010-04-12 17:59:55 -07001699 (ha->pdev->devfn * 8);
1700 } else {
Bart Van Assche8dfa4b5a2015-07-09 07:24:50 -07001701 ha->nxdb_wr_ptr = (void __iomem *)(ha->pdev->devfn == 6 ?
Giridhar Malavalia9083012010-04-12 17:59:55 -07001702 QLA82XX_CAMRAM_DB1 :
1703 QLA82XX_CAMRAM_DB2);
1704 }
1705
1706 ha->max_req_queues = ha->max_rsp_queues = 1;
1707 ha->msix_count = ha->max_rsp_queues + 1;
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001708 ql_dbg_pci(ql_dbg_multiq, ha->pdev, 0xc006,
1709 "nx_pci_base=%p iobase=%p "
1710 "max_req_queues=%d msix_count=%d.\n",
Bart Van Assche8dfa4b5a2015-07-09 07:24:50 -07001711 ha->nx_pcibase, ha->iobase,
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001712 ha->max_req_queues, ha->msix_count);
1713 ql_dbg_pci(ql_dbg_init, ha->pdev, 0x0010,
1714 "nx_pci_base=%p iobase=%p "
1715 "max_req_queues=%d msix_count=%d.\n",
Bart Van Assche8dfa4b5a2015-07-09 07:24:50 -07001716 ha->nx_pcibase, ha->iobase,
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001717 ha->max_req_queues, ha->msix_count);
Giridhar Malavalia9083012010-04-12 17:59:55 -07001718 return 0;
1719
1720iospace_error_exit:
1721 return -ENOMEM;
1722}
1723
1724/* GS related functions */
1725
1726/* Initialization related functions */
1727
1728/**
1729 * qla82xx_pci_config() - Setup ISP82xx PCI configuration registers.
1730 * @ha: HA context
1731 *
1732 * Returns 0 on success.
1733*/
1734int
1735qla82xx_pci_config(scsi_qla_host_t *vha)
1736{
1737 struct qla_hw_data *ha = vha->hw;
1738 int ret;
1739
1740 pci_set_master(ha->pdev);
1741 ret = pci_set_mwi(ha->pdev);
1742 ha->chip_revision = ha->pdev->revision;
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001743 ql_dbg(ql_dbg_init, vha, 0x0043,
Bart Van Assche52c82822015-07-09 07:23:26 -07001744 "Chip revision:%d; pci_set_mwi() returned %d.\n",
1745 ha->chip_revision, ret);
Giridhar Malavalia9083012010-04-12 17:59:55 -07001746 return 0;
1747}
1748
1749/**
1750 * qla82xx_reset_chip() - Setup ISP82xx PCI configuration registers.
1751 * @ha: HA context
1752 *
1753 * Returns 0 on success.
1754 */
1755void
1756qla82xx_reset_chip(scsi_qla_host_t *vha)
1757{
1758 struct qla_hw_data *ha = vha->hw;
1759 ha->isp_ops->disable_intrs(ha);
1760}
1761
1762void qla82xx_config_rings(struct scsi_qla_host *vha)
1763{
1764 struct qla_hw_data *ha = vha->hw;
1765 struct device_reg_82xx __iomem *reg = &ha->iobase->isp82;
1766 struct init_cb_81xx *icb;
1767 struct req_que *req = ha->req_q_map[0];
1768 struct rsp_que *rsp = ha->rsp_q_map[0];
1769
1770 /* Setup ring parameters in initialization control block. */
1771 icb = (struct init_cb_81xx *)ha->init_cb;
Bart Van Asschead950362015-07-09 07:24:08 -07001772 icb->request_q_outpointer = cpu_to_le16(0);
1773 icb->response_q_inpointer = cpu_to_le16(0);
Giridhar Malavalia9083012010-04-12 17:59:55 -07001774 icb->request_q_length = cpu_to_le16(req->length);
1775 icb->response_q_length = cpu_to_le16(rsp->length);
1776 icb->request_q_address[0] = cpu_to_le32(LSD(req->dma));
1777 icb->request_q_address[1] = cpu_to_le32(MSD(req->dma));
1778 icb->response_q_address[0] = cpu_to_le32(LSD(rsp->dma));
1779 icb->response_q_address[1] = cpu_to_le32(MSD(rsp->dma));
1780
Bart Van Assche8dfa4b5a2015-07-09 07:24:50 -07001781 WRT_REG_DWORD(&reg->req_q_out[0], 0);
1782 WRT_REG_DWORD(&reg->rsp_q_in[0], 0);
1783 WRT_REG_DWORD(&reg->rsp_q_out[0], 0);
Giridhar Malavalia9083012010-04-12 17:59:55 -07001784}
1785
Giridhar Malavali77e334d2010-09-03 15:20:52 -07001786static int
1787qla82xx_fw_load_from_blob(struct qla_hw_data *ha)
Giridhar Malavalia9083012010-04-12 17:59:55 -07001788{
1789 u64 *ptr64;
1790 u32 i, flashaddr, size;
1791 __le64 data;
1792
1793 size = (IMAGE_START - BOOTLD_START) / 8;
1794
Harish Zunjarrao9c2b2972010-05-28 15:08:23 -07001795 ptr64 = (u64 *)qla82xx_get_bootld_offset(ha);
Giridhar Malavalia9083012010-04-12 17:59:55 -07001796 flashaddr = BOOTLD_START;
1797
1798 for (i = 0; i < size; i++) {
1799 data = cpu_to_le64(ptr64[i]);
Harish Zunjarrao9c2b2972010-05-28 15:08:23 -07001800 if (qla82xx_pci_mem_write_2M(ha, flashaddr, &data, 8))
1801 return -EIO;
Giridhar Malavalia9083012010-04-12 17:59:55 -07001802 flashaddr += 8;
1803 }
1804
Giridhar Malavalia9083012010-04-12 17:59:55 -07001805 flashaddr = FLASH_ADDR_START;
Harish Zunjarrao9c2b2972010-05-28 15:08:23 -07001806 size = (__force u32)qla82xx_get_fw_size(ha) / 8;
1807 ptr64 = (u64 *)qla82xx_get_fw_offs(ha);
Giridhar Malavalia9083012010-04-12 17:59:55 -07001808
1809 for (i = 0; i < size; i++) {
1810 data = cpu_to_le64(ptr64[i]);
1811
1812 if (qla82xx_pci_mem_write_2M(ha, flashaddr, &data, 8))
1813 return -EIO;
1814 flashaddr += 8;
1815 }
Harish Zunjarrao9c2b2972010-05-28 15:08:23 -07001816 udelay(100);
Giridhar Malavalia9083012010-04-12 17:59:55 -07001817
1818 /* Write a magic value to CAMRAM register
1819 * at a specified offset to indicate
1820 * that all data is written and
1821 * ready for firmware to initialize.
1822 */
Harish Zunjarrao9c2b2972010-05-28 15:08:23 -07001823 qla82xx_wr_32(ha, QLA82XX_CAM_RAM(0x1fc), QLA82XX_BDINFO_MAGIC);
Giridhar Malavalia9083012010-04-12 17:59:55 -07001824
Harish Zunjarrao9c2b2972010-05-28 15:08:23 -07001825 read_lock(&ha->hw_lock);
Giridhar Malavali37113332010-07-23 15:28:34 +05001826 qla82xx_wr_32(ha, QLA82XX_CRB_PEG_NET_0 + 0x18, 0x1020);
1827 qla82xx_wr_32(ha, QLA82XX_ROMUSB_GLB_SW_RESET, 0x80001e);
Harish Zunjarrao9c2b2972010-05-28 15:08:23 -07001828 read_unlock(&ha->hw_lock);
1829 return 0;
1830}
1831
1832static int
1833qla82xx_set_product_offset(struct qla_hw_data *ha)
1834{
1835 struct qla82xx_uri_table_desc *ptab_desc = NULL;
1836 const uint8_t *unirom = ha->hablob->fw->data;
1837 uint32_t i;
1838 __le32 entries;
1839 __le32 flags, file_chiprev, offset;
1840 uint8_t chiprev = ha->chip_revision;
1841 /* Hardcoding mn_present flag for P3P */
1842 int mn_present = 0;
1843 uint32_t flagbit;
1844
1845 ptab_desc = qla82xx_get_table_desc(unirom,
1846 QLA82XX_URI_DIR_SECT_PRODUCT_TBL);
Bart Van Assche8d2b21d2015-06-04 15:58:09 -07001847 if (!ptab_desc)
Harish Zunjarrao9c2b2972010-05-28 15:08:23 -07001848 return -1;
1849
1850 entries = cpu_to_le32(ptab_desc->num_entries);
1851
1852 for (i = 0; i < entries; i++) {
1853 offset = cpu_to_le32(ptab_desc->findex) +
1854 (i * cpu_to_le32(ptab_desc->entry_size));
1855 flags = cpu_to_le32(*((int *)&unirom[offset] +
1856 QLA82XX_URI_FLAGS_OFF));
1857 file_chiprev = cpu_to_le32(*((int *)&unirom[offset] +
1858 QLA82XX_URI_CHIP_REV_OFF));
1859
1860 flagbit = mn_present ? 1 : 2;
1861
1862 if ((chiprev == file_chiprev) && ((1ULL << flagbit) & flags)) {
1863 ha->file_prd_off = offset;
1864 return 0;
1865 }
1866 }
1867 return -1;
1868}
1869
Saurav Kashyapfa492632012-11-21 02:40:29 -05001870static int
Harish Zunjarrao9c2b2972010-05-28 15:08:23 -07001871qla82xx_validate_firmware_blob(scsi_qla_host_t *vha, uint8_t fw_type)
1872{
1873 __le32 val;
1874 uint32_t min_size;
1875 struct qla_hw_data *ha = vha->hw;
1876 const struct firmware *fw = ha->hablob->fw;
1877
1878 ha->fw_type = fw_type;
1879
1880 if (fw_type == QLA82XX_UNIFIED_ROMIMAGE) {
1881 if (qla82xx_set_product_offset(ha))
1882 return -EINVAL;
1883
1884 min_size = QLA82XX_URI_FW_MIN_SIZE;
1885 } else {
1886 val = cpu_to_le32(*(u32 *)&fw->data[QLA82XX_FW_MAGIC_OFFSET]);
1887 if ((__force u32)val != QLA82XX_BDINFO_MAGIC)
1888 return -EINVAL;
1889
1890 min_size = QLA82XX_FW_MIN_SIZE;
1891 }
1892
1893 if (fw->size < min_size)
1894 return -EINVAL;
Giridhar Malavalia9083012010-04-12 17:59:55 -07001895 return 0;
1896}
1897
Giridhar Malavali77e334d2010-09-03 15:20:52 -07001898static int
1899qla82xx_check_cmdpeg_state(struct qla_hw_data *ha)
Giridhar Malavalia9083012010-04-12 17:59:55 -07001900{
1901 u32 val = 0;
1902 int retries = 60;
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001903 scsi_qla_host_t *vha = pci_get_drvdata(ha->pdev);
Giridhar Malavalia9083012010-04-12 17:59:55 -07001904
1905 do {
1906 read_lock(&ha->hw_lock);
1907 val = qla82xx_rd_32(ha, CRB_CMDPEG_STATE);
1908 read_unlock(&ha->hw_lock);
1909
1910 switch (val) {
1911 case PHAN_INITIALIZE_COMPLETE:
1912 case PHAN_INITIALIZE_ACK:
1913 return QLA_SUCCESS;
1914 case PHAN_INITIALIZE_FAILED:
1915 break;
1916 default:
1917 break;
1918 }
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001919 ql_log(ql_log_info, vha, 0x00a8,
1920 "CRB_CMDPEG_STATE: 0x%x and retries:0x%x.\n",
1921 val, retries);
Giridhar Malavalia9083012010-04-12 17:59:55 -07001922
1923 msleep(500);
1924
1925 } while (--retries);
1926
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001927 ql_log(ql_log_fatal, vha, 0x00a9,
Giridhar Malavalia9083012010-04-12 17:59:55 -07001928 "Cmd Peg initialization failed: 0x%x.\n", val);
1929
Giridhar Malavalia9083012010-04-12 17:59:55 -07001930 val = qla82xx_rd_32(ha, QLA82XX_ROMUSB_GLB_PEGTUNE_DONE);
1931 read_lock(&ha->hw_lock);
1932 qla82xx_wr_32(ha, CRB_CMDPEG_STATE, PHAN_INITIALIZE_FAILED);
1933 read_unlock(&ha->hw_lock);
1934 return QLA_FUNCTION_FAILED;
1935}
1936
Giridhar Malavali77e334d2010-09-03 15:20:52 -07001937static int
1938qla82xx_check_rcvpeg_state(struct qla_hw_data *ha)
Giridhar Malavalia9083012010-04-12 17:59:55 -07001939{
1940 u32 val = 0;
1941 int retries = 60;
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001942 scsi_qla_host_t *vha = pci_get_drvdata(ha->pdev);
Giridhar Malavalia9083012010-04-12 17:59:55 -07001943
1944 do {
1945 read_lock(&ha->hw_lock);
1946 val = qla82xx_rd_32(ha, CRB_RCVPEG_STATE);
1947 read_unlock(&ha->hw_lock);
1948
1949 switch (val) {
1950 case PHAN_INITIALIZE_COMPLETE:
1951 case PHAN_INITIALIZE_ACK:
1952 return QLA_SUCCESS;
1953 case PHAN_INITIALIZE_FAILED:
1954 break;
1955 default:
1956 break;
1957 }
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001958 ql_log(ql_log_info, vha, 0x00ab,
1959 "CRB_RCVPEG_STATE: 0x%x and retries: 0x%x.\n",
1960 val, retries);
Giridhar Malavalia9083012010-04-12 17:59:55 -07001961
1962 msleep(500);
1963
1964 } while (--retries);
1965
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001966 ql_log(ql_log_fatal, vha, 0x00ac,
1967 "Rcv Peg initializatin failed: 0x%x.\n", val);
Giridhar Malavalia9083012010-04-12 17:59:55 -07001968 read_lock(&ha->hw_lock);
1969 qla82xx_wr_32(ha, CRB_RCVPEG_STATE, PHAN_INITIALIZE_FAILED);
1970 read_unlock(&ha->hw_lock);
1971 return QLA_FUNCTION_FAILED;
1972}
1973
1974/* ISR related functions */
Giridhar Malavalia9083012010-04-12 17:59:55 -07001975static struct qla82xx_legacy_intr_set legacy_intr[] = \
1976 QLA82XX_LEGACY_INTR_CONFIG;
1977
1978/*
1979 * qla82xx_mbx_completion() - Process mailbox command completions.
1980 * @ha: SCSI driver HA context
1981 * @mb0: Mailbox0 register
1982 */
Atul Deshmukh7ec0eff2013-08-27 01:37:28 -04001983void
Giridhar Malavalia9083012010-04-12 17:59:55 -07001984qla82xx_mbx_completion(scsi_qla_host_t *vha, uint16_t mb0)
1985{
1986 uint16_t cnt;
1987 uint16_t __iomem *wptr;
1988 struct qla_hw_data *ha = vha->hw;
1989 struct device_reg_82xx __iomem *reg = &ha->iobase->isp82;
1990 wptr = (uint16_t __iomem *)&reg->mailbox_out[1];
1991
1992 /* Load return mailbox registers. */
1993 ha->flags.mbox_int = 1;
1994 ha->mailbox_out[0] = mb0;
1995
1996 for (cnt = 1; cnt < ha->mbx_count; cnt++) {
1997 ha->mailbox_out[cnt] = RD_REG_WORD(wptr);
1998 wptr++;
1999 }
2000
Chad Dupuiscfb09192011-11-18 09:03:07 -08002001 if (!ha->mcp)
Saurav Kashyap7c3df132011-07-14 12:00:13 -07002002 ql_dbg(ql_dbg_async, vha, 0x5053,
2003 "MBX pointer ERROR.\n");
Giridhar Malavalia9083012010-04-12 17:59:55 -07002004}
2005
2006/*
2007 * qla82xx_intr_handler() - Process interrupts for the ISP23xx and ISP63xx.
2008 * @irq:
2009 * @dev_id: SCSI driver HA context
2010 * @regs:
2011 *
2012 * Called by system whenever the host adapter generates an interrupt.
2013 *
2014 * Returns handled flag.
2015 */
2016irqreturn_t
2017qla82xx_intr_handler(int irq, void *dev_id)
2018{
2019 scsi_qla_host_t *vha;
2020 struct qla_hw_data *ha;
2021 struct rsp_que *rsp;
2022 struct device_reg_82xx __iomem *reg;
2023 int status = 0, status1 = 0;
2024 unsigned long flags;
2025 unsigned long iter;
Saurav Kashyap7c3df132011-07-14 12:00:13 -07002026 uint32_t stat = 0;
Giridhar Malavalia9083012010-04-12 17:59:55 -07002027 uint16_t mb[4];
2028
2029 rsp = (struct rsp_que *) dev_id;
2030 if (!rsp) {
Giridhar Malavalib6d0d9d2012-05-15 14:34:25 -04002031 ql_log(ql_log_info, NULL, 0xb053,
Chad Dupuis3256b432012-02-09 11:15:47 -08002032 "%s: NULL response queue pointer.\n", __func__);
Giridhar Malavalia9083012010-04-12 17:59:55 -07002033 return IRQ_NONE;
2034 }
2035 ha = rsp->hw;
2036
2037 if (!ha->flags.msi_enabled) {
2038 status = qla82xx_rd_32(ha, ISR_INT_VECTOR);
2039 if (!(status & ha->nx_legacy_intr.int_vec_bit))
2040 return IRQ_NONE;
2041
2042 status1 = qla82xx_rd_32(ha, ISR_INT_STATE_REG);
2043 if (!ISR_IS_LEGACY_INTR_TRIGGERED(status1))
2044 return IRQ_NONE;
2045 }
2046
2047 /* clear the interrupt */
2048 qla82xx_wr_32(ha, ha->nx_legacy_intr.tgt_status_reg, 0xffffffff);
2049
2050 /* read twice to ensure write is flushed */
2051 qla82xx_rd_32(ha, ISR_INT_VECTOR);
2052 qla82xx_rd_32(ha, ISR_INT_VECTOR);
2053
2054 reg = &ha->iobase->isp82;
2055
2056 spin_lock_irqsave(&ha->hardware_lock, flags);
2057 vha = pci_get_drvdata(ha->pdev);
2058 for (iter = 1; iter--; ) {
2059
2060 if (RD_REG_DWORD(&reg->host_int)) {
2061 stat = RD_REG_DWORD(&reg->host_status);
Giridhar Malavalia9083012010-04-12 17:59:55 -07002062
2063 switch (stat & 0xff) {
2064 case 0x1:
2065 case 0x2:
2066 case 0x10:
2067 case 0x11:
2068 qla82xx_mbx_completion(vha, MSW(stat));
2069 status |= MBX_INTERRUPT;
2070 break;
2071 case 0x12:
2072 mb[0] = MSW(stat);
2073 mb[1] = RD_REG_WORD(&reg->mailbox_out[1]);
2074 mb[2] = RD_REG_WORD(&reg->mailbox_out[2]);
2075 mb[3] = RD_REG_WORD(&reg->mailbox_out[3]);
2076 qla2x00_async_event(vha, rsp, mb);
2077 break;
2078 case 0x13:
2079 qla24xx_process_response_queue(vha, rsp);
2080 break;
2081 default:
Saurav Kashyap7c3df132011-07-14 12:00:13 -07002082 ql_dbg(ql_dbg_async, vha, 0x5054,
2083 "Unrecognized interrupt type (%d).\n",
2084 stat & 0xff);
Giridhar Malavalia9083012010-04-12 17:59:55 -07002085 break;
2086 }
2087 }
2088 WRT_REG_DWORD(&reg->host_int, 0);
2089 }
Giridhar Malavalia9083012010-04-12 17:59:55 -07002090
gurinder.shergill@hp.com36439832013-04-23 10:13:17 -07002091 qla2x00_handle_mbx_completion(ha, status);
2092 spin_unlock_irqrestore(&ha->hardware_lock, flags);
2093
2094 if (!ha->flags.msi_enabled)
2095 qla82xx_wr_32(ha, ha->nx_legacy_intr.tgt_mask_reg, 0xfbff);
2096
Giridhar Malavalia9083012010-04-12 17:59:55 -07002097 return IRQ_HANDLED;
2098}
2099
2100irqreturn_t
2101qla82xx_msix_default(int irq, void *dev_id)
2102{
2103 scsi_qla_host_t *vha;
2104 struct qla_hw_data *ha;
2105 struct rsp_que *rsp;
2106 struct device_reg_82xx __iomem *reg;
2107 int status = 0;
2108 unsigned long flags;
Saurav Kashyap7c3df132011-07-14 12:00:13 -07002109 uint32_t stat = 0;
Chad Dupuisf3ddac12013-10-30 03:38:16 -04002110 uint32_t host_int = 0;
Giridhar Malavalia9083012010-04-12 17:59:55 -07002111 uint16_t mb[4];
2112
2113 rsp = (struct rsp_que *) dev_id;
2114 if (!rsp) {
2115 printk(KERN_INFO
Saurav Kashyap7c3df132011-07-14 12:00:13 -07002116 "%s(): NULL response queue pointer.\n", __func__);
Giridhar Malavalia9083012010-04-12 17:59:55 -07002117 return IRQ_NONE;
2118 }
2119 ha = rsp->hw;
2120
2121 reg = &ha->iobase->isp82;
2122
2123 spin_lock_irqsave(&ha->hardware_lock, flags);
2124 vha = pci_get_drvdata(ha->pdev);
2125 do {
Chad Dupuisf3ddac12013-10-30 03:38:16 -04002126 host_int = RD_REG_DWORD(&reg->host_int);
Joe Lawrencec821e0d2014-08-26 17:11:41 -04002127 if (qla2x00_check_reg32_for_disconnect(vha, host_int))
Chad Dupuisf3ddac12013-10-30 03:38:16 -04002128 break;
2129 if (host_int) {
Giridhar Malavalia9083012010-04-12 17:59:55 -07002130 stat = RD_REG_DWORD(&reg->host_status);
Giridhar Malavalia9083012010-04-12 17:59:55 -07002131
2132 switch (stat & 0xff) {
2133 case 0x1:
2134 case 0x2:
2135 case 0x10:
2136 case 0x11:
2137 qla82xx_mbx_completion(vha, MSW(stat));
2138 status |= MBX_INTERRUPT;
2139 break;
2140 case 0x12:
2141 mb[0] = MSW(stat);
2142 mb[1] = RD_REG_WORD(&reg->mailbox_out[1]);
2143 mb[2] = RD_REG_WORD(&reg->mailbox_out[2]);
2144 mb[3] = RD_REG_WORD(&reg->mailbox_out[3]);
2145 qla2x00_async_event(vha, rsp, mb);
2146 break;
2147 case 0x13:
2148 qla24xx_process_response_queue(vha, rsp);
2149 break;
2150 default:
Saurav Kashyap7c3df132011-07-14 12:00:13 -07002151 ql_dbg(ql_dbg_async, vha, 0x5041,
2152 "Unrecognized interrupt type (%d).\n",
2153 stat & 0xff);
Giridhar Malavalia9083012010-04-12 17:59:55 -07002154 break;
2155 }
2156 }
2157 WRT_REG_DWORD(&reg->host_int, 0);
2158 } while (0);
2159
gurinder.shergill@hp.com36439832013-04-23 10:13:17 -07002160 qla2x00_handle_mbx_completion(ha, status);
2161 spin_unlock_irqrestore(&ha->hardware_lock, flags);
2162
Giridhar Malavalia9083012010-04-12 17:59:55 -07002163 return IRQ_HANDLED;
2164}
2165
2166irqreturn_t
2167qla82xx_msix_rsp_q(int irq, void *dev_id)
2168{
2169 scsi_qla_host_t *vha;
2170 struct qla_hw_data *ha;
2171 struct rsp_que *rsp;
2172 struct device_reg_82xx __iomem *reg;
Saurav Kashyap3553d342011-08-16 11:29:27 -07002173 unsigned long flags;
Chad Dupuisf3ddac12013-10-30 03:38:16 -04002174 uint32_t host_int = 0;
Giridhar Malavalia9083012010-04-12 17:59:55 -07002175
2176 rsp = (struct rsp_que *) dev_id;
2177 if (!rsp) {
2178 printk(KERN_INFO
Saurav Kashyap7c3df132011-07-14 12:00:13 -07002179 "%s(): NULL response queue pointer.\n", __func__);
Giridhar Malavalia9083012010-04-12 17:59:55 -07002180 return IRQ_NONE;
2181 }
2182
2183 ha = rsp->hw;
2184 reg = &ha->iobase->isp82;
Saurav Kashyap3553d342011-08-16 11:29:27 -07002185 spin_lock_irqsave(&ha->hardware_lock, flags);
Giridhar Malavalia9083012010-04-12 17:59:55 -07002186 vha = pci_get_drvdata(ha->pdev);
Chad Dupuisf3ddac12013-10-30 03:38:16 -04002187 host_int = RD_REG_DWORD(&reg->host_int);
Joe Lawrencec821e0d2014-08-26 17:11:41 -04002188 if (qla2x00_check_reg32_for_disconnect(vha, host_int))
Chad Dupuisf3ddac12013-10-30 03:38:16 -04002189 goto out;
Giridhar Malavalia9083012010-04-12 17:59:55 -07002190 qla24xx_process_response_queue(vha, rsp);
2191 WRT_REG_DWORD(&reg->host_int, 0);
Chad Dupuisf3ddac12013-10-30 03:38:16 -04002192out:
Saurav Kashyap3553d342011-08-16 11:29:27 -07002193 spin_unlock_irqrestore(&ha->hardware_lock, flags);
Giridhar Malavalia9083012010-04-12 17:59:55 -07002194 return IRQ_HANDLED;
2195}
2196
2197void
2198qla82xx_poll(int irq, void *dev_id)
2199{
2200 scsi_qla_host_t *vha;
2201 struct qla_hw_data *ha;
2202 struct rsp_que *rsp;
2203 struct device_reg_82xx __iomem *reg;
2204 int status = 0;
2205 uint32_t stat;
Chad Dupuisf3ddac12013-10-30 03:38:16 -04002206 uint32_t host_int = 0;
Giridhar Malavalia9083012010-04-12 17:59:55 -07002207 uint16_t mb[4];
2208 unsigned long flags;
2209
2210 rsp = (struct rsp_que *) dev_id;
2211 if (!rsp) {
2212 printk(KERN_INFO
Saurav Kashyap7c3df132011-07-14 12:00:13 -07002213 "%s(): NULL response queue pointer.\n", __func__);
Giridhar Malavalia9083012010-04-12 17:59:55 -07002214 return;
2215 }
2216 ha = rsp->hw;
2217
2218 reg = &ha->iobase->isp82;
2219 spin_lock_irqsave(&ha->hardware_lock, flags);
2220 vha = pci_get_drvdata(ha->pdev);
2221
Chad Dupuisf3ddac12013-10-30 03:38:16 -04002222 host_int = RD_REG_DWORD(&reg->host_int);
Joe Lawrencec821e0d2014-08-26 17:11:41 -04002223 if (qla2x00_check_reg32_for_disconnect(vha, host_int))
Chad Dupuisf3ddac12013-10-30 03:38:16 -04002224 goto out;
2225 if (host_int) {
Giridhar Malavalia9083012010-04-12 17:59:55 -07002226 stat = RD_REG_DWORD(&reg->host_status);
2227 switch (stat & 0xff) {
2228 case 0x1:
2229 case 0x2:
2230 case 0x10:
2231 case 0x11:
2232 qla82xx_mbx_completion(vha, MSW(stat));
2233 status |= MBX_INTERRUPT;
2234 break;
2235 case 0x12:
2236 mb[0] = MSW(stat);
2237 mb[1] = RD_REG_WORD(&reg->mailbox_out[1]);
2238 mb[2] = RD_REG_WORD(&reg->mailbox_out[2]);
2239 mb[3] = RD_REG_WORD(&reg->mailbox_out[3]);
2240 qla2x00_async_event(vha, rsp, mb);
2241 break;
2242 case 0x13:
2243 qla24xx_process_response_queue(vha, rsp);
2244 break;
2245 default:
Saurav Kashyap7c3df132011-07-14 12:00:13 -07002246 ql_dbg(ql_dbg_p3p, vha, 0xb013,
2247 "Unrecognized interrupt type (%d).\n",
2248 stat * 0xff);
Giridhar Malavalia9083012010-04-12 17:59:55 -07002249 break;
2250 }
Atul Deshmukh02a9ae62013-10-30 03:38:20 -04002251 WRT_REG_DWORD(&reg->host_int, 0);
Giridhar Malavalia9083012010-04-12 17:59:55 -07002252 }
Chad Dupuisf3ddac12013-10-30 03:38:16 -04002253out:
Giridhar Malavalia9083012010-04-12 17:59:55 -07002254 spin_unlock_irqrestore(&ha->hardware_lock, flags);
2255}
2256
2257void
2258qla82xx_enable_intrs(struct qla_hw_data *ha)
2259{
2260 scsi_qla_host_t *vha = pci_get_drvdata(ha->pdev);
2261 qla82xx_mbx_intr_enable(vha);
2262 spin_lock_irq(&ha->hardware_lock);
Atul Deshmukh7ec0eff2013-08-27 01:37:28 -04002263 if (IS_QLA8044(ha))
2264 qla8044_wr_reg(ha, LEG_INTR_MASK_OFFSET, 0);
2265 else
2266 qla82xx_wr_32(ha, ha->nx_legacy_intr.tgt_mask_reg, 0xfbff);
Giridhar Malavalia9083012010-04-12 17:59:55 -07002267 spin_unlock_irq(&ha->hardware_lock);
2268 ha->interrupts_on = 1;
2269}
2270
2271void
2272qla82xx_disable_intrs(struct qla_hw_data *ha)
2273{
2274 scsi_qla_host_t *vha = pci_get_drvdata(ha->pdev);
2275 qla82xx_mbx_intr_disable(vha);
2276 spin_lock_irq(&ha->hardware_lock);
Atul Deshmukh7ec0eff2013-08-27 01:37:28 -04002277 if (IS_QLA8044(ha))
2278 qla8044_wr_reg(ha, LEG_INTR_MASK_OFFSET, 1);
2279 else
2280 qla82xx_wr_32(ha, ha->nx_legacy_intr.tgt_mask_reg, 0x0400);
Giridhar Malavalia9083012010-04-12 17:59:55 -07002281 spin_unlock_irq(&ha->hardware_lock);
2282 ha->interrupts_on = 0;
2283}
2284
2285void qla82xx_init_flags(struct qla_hw_data *ha)
2286{
2287 struct qla82xx_legacy_intr_set *nx_legacy_intr;
2288
2289 /* ISP 8021 initializations */
2290 rwlock_init(&ha->hw_lock);
2291 ha->qdr_sn_window = -1;
2292 ha->ddr_mn_window = -1;
2293 ha->curr_window = 255;
2294 ha->portnum = PCI_FUNC(ha->pdev->devfn);
2295 nx_legacy_intr = &legacy_intr[ha->portnum];
2296 ha->nx_legacy_intr.int_vec_bit = nx_legacy_intr->int_vec_bit;
2297 ha->nx_legacy_intr.tgt_status_reg = nx_legacy_intr->tgt_status_reg;
2298 ha->nx_legacy_intr.tgt_mask_reg = nx_legacy_intr->tgt_mask_reg;
2299 ha->nx_legacy_intr.pci_int_reg = nx_legacy_intr->pci_int_reg;
2300}
2301
Bart Van Assche2374dd22015-07-09 07:23:02 -07002302static inline void
Saurav Kashyap0251ce82012-08-22 14:21:22 -04002303qla82xx_set_idc_version(scsi_qla_host_t *vha)
2304{
2305 int idc_ver;
2306 uint32_t drv_active;
2307 struct qla_hw_data *ha = vha->hw;
2308
2309 drv_active = qla82xx_rd_32(ha, QLA82XX_CRB_DRV_ACTIVE);
2310 if (drv_active == (QLA82XX_DRV_ACTIVE << (ha->portnum * 4))) {
2311 qla82xx_wr_32(ha, QLA82XX_CRB_DRV_IDC_VERSION,
2312 QLA82XX_IDC_VERSION);
2313 ql_log(ql_log_info, vha, 0xb082,
2314 "IDC version updated to %d\n", QLA82XX_IDC_VERSION);
2315 } else {
2316 idc_ver = qla82xx_rd_32(ha, QLA82XX_CRB_DRV_IDC_VERSION);
2317 if (idc_ver != QLA82XX_IDC_VERSION)
2318 ql_log(ql_log_info, vha, 0xb083,
2319 "qla2xxx driver IDC version %d is not compatible "
2320 "with IDC version %d of the other drivers\n",
2321 QLA82XX_IDC_VERSION, idc_ver);
2322 }
2323}
2324
2325inline void
Giridhar Malavalia9083012010-04-12 17:59:55 -07002326qla82xx_set_drv_active(scsi_qla_host_t *vha)
2327{
2328 uint32_t drv_active;
2329 struct qla_hw_data *ha = vha->hw;
2330
2331 drv_active = qla82xx_rd_32(ha, QLA82XX_CRB_DRV_ACTIVE);
2332
2333 /* If reset value is all FF's, initialize DRV_ACTIVE */
2334 if (drv_active == 0xffffffff) {
Giridhar Malavali77e334d2010-09-03 15:20:52 -07002335 qla82xx_wr_32(ha, QLA82XX_CRB_DRV_ACTIVE,
2336 QLA82XX_DRV_NOT_ACTIVE);
Giridhar Malavalia9083012010-04-12 17:59:55 -07002337 drv_active = qla82xx_rd_32(ha, QLA82XX_CRB_DRV_ACTIVE);
2338 }
Giridhar Malavali77e334d2010-09-03 15:20:52 -07002339 drv_active |= (QLA82XX_DRV_ACTIVE << (ha->portnum * 4));
Giridhar Malavalia9083012010-04-12 17:59:55 -07002340 qla82xx_wr_32(ha, QLA82XX_CRB_DRV_ACTIVE, drv_active);
2341}
2342
2343inline void
2344qla82xx_clear_drv_active(struct qla_hw_data *ha)
2345{
2346 uint32_t drv_active;
2347
2348 drv_active = qla82xx_rd_32(ha, QLA82XX_CRB_DRV_ACTIVE);
Giridhar Malavali77e334d2010-09-03 15:20:52 -07002349 drv_active &= ~(QLA82XX_DRV_ACTIVE << (ha->portnum * 4));
Giridhar Malavalia9083012010-04-12 17:59:55 -07002350 qla82xx_wr_32(ha, QLA82XX_CRB_DRV_ACTIVE, drv_active);
2351}
2352
2353static inline int
2354qla82xx_need_reset(struct qla_hw_data *ha)
2355{
2356 uint32_t drv_state;
2357 int rval;
2358
Santosh Vernekar7d613ac2012-08-22 14:21:03 -04002359 if (ha->flags.nic_core_reset_owner)
Giridhar Malavali08de2842011-08-16 11:31:44 -07002360 return 1;
2361 else {
2362 drv_state = qla82xx_rd_32(ha, QLA82XX_CRB_DRV_STATE);
2363 rval = drv_state & (QLA82XX_DRVST_RST_RDY << (ha->portnum * 4));
2364 return rval;
2365 }
Giridhar Malavalia9083012010-04-12 17:59:55 -07002366}
2367
2368static inline void
2369qla82xx_set_rst_ready(struct qla_hw_data *ha)
2370{
2371 uint32_t drv_state;
2372 scsi_qla_host_t *vha = pci_get_drvdata(ha->pdev);
2373
2374 drv_state = qla82xx_rd_32(ha, QLA82XX_CRB_DRV_STATE);
2375
2376 /* If reset value is all FF's, initialize DRV_STATE */
2377 if (drv_state == 0xffffffff) {
Giridhar Malavali77e334d2010-09-03 15:20:52 -07002378 qla82xx_wr_32(ha, QLA82XX_CRB_DRV_STATE, QLA82XX_DRVST_NOT_RDY);
Giridhar Malavalia9083012010-04-12 17:59:55 -07002379 drv_state = qla82xx_rd_32(ha, QLA82XX_CRB_DRV_STATE);
2380 }
2381 drv_state |= (QLA82XX_DRVST_RST_RDY << (ha->portnum * 4));
Giridhar Malavali08de2842011-08-16 11:31:44 -07002382 ql_dbg(ql_dbg_init, vha, 0x00bb,
2383 "drv_state = 0x%08x.\n", drv_state);
Giridhar Malavalia9083012010-04-12 17:59:55 -07002384 qla82xx_wr_32(ha, QLA82XX_CRB_DRV_STATE, drv_state);
2385}
2386
2387static inline void
2388qla82xx_clear_rst_ready(struct qla_hw_data *ha)
2389{
2390 uint32_t drv_state;
2391
2392 drv_state = qla82xx_rd_32(ha, QLA82XX_CRB_DRV_STATE);
2393 drv_state &= ~(QLA82XX_DRVST_RST_RDY << (ha->portnum * 4));
2394 qla82xx_wr_32(ha, QLA82XX_CRB_DRV_STATE, drv_state);
2395}
2396
2397static inline void
2398qla82xx_set_qsnt_ready(struct qla_hw_data *ha)
2399{
2400 uint32_t qsnt_state;
2401
2402 qsnt_state = qla82xx_rd_32(ha, QLA82XX_CRB_DRV_STATE);
2403 qsnt_state |= (QLA82XX_DRVST_QSNT_RDY << (ha->portnum * 4));
2404 qla82xx_wr_32(ha, QLA82XX_CRB_DRV_STATE, qsnt_state);
2405}
2406
Saurav Kashyap579d12b2010-12-21 16:00:14 -08002407void
2408qla82xx_clear_qsnt_ready(scsi_qla_host_t *vha)
2409{
2410 struct qla_hw_data *ha = vha->hw;
2411 uint32_t qsnt_state;
2412
2413 qsnt_state = qla82xx_rd_32(ha, QLA82XX_CRB_DRV_STATE);
2414 qsnt_state &= ~(QLA82XX_DRVST_QSNT_RDY << (ha->portnum * 4));
2415 qla82xx_wr_32(ha, QLA82XX_CRB_DRV_STATE, qsnt_state);
2416}
2417
Giridhar Malavali77e334d2010-09-03 15:20:52 -07002418static int
2419qla82xx_load_fw(scsi_qla_host_t *vha)
Giridhar Malavalia9083012010-04-12 17:59:55 -07002420{
2421 int rst;
2422 struct fw_blob *blob;
2423 struct qla_hw_data *ha = vha->hw;
2424
Giridhar Malavalia9083012010-04-12 17:59:55 -07002425 if (qla82xx_pinit_from_rom(vha) != QLA_SUCCESS) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07002426 ql_log(ql_log_fatal, vha, 0x009f,
2427 "Error during CRB initialization.\n");
Giridhar Malavalia9083012010-04-12 17:59:55 -07002428 return QLA_FUNCTION_FAILED;
2429 }
2430 udelay(500);
2431
2432 /* Bring QM and CAMRAM out of reset */
2433 rst = qla82xx_rd_32(ha, QLA82XX_ROMUSB_GLB_SW_RESET);
2434 rst &= ~((1 << 28) | (1 << 24));
2435 qla82xx_wr_32(ha, QLA82XX_ROMUSB_GLB_SW_RESET, rst);
2436
2437 /*
2438 * FW Load priority:
2439 * 1) Operational firmware residing in flash.
2440 * 2) Firmware via request-firmware interface (.bin file).
2441 */
2442 if (ql2xfwloadbin == 2)
2443 goto try_blob_fw;
2444
Saurav Kashyap7c3df132011-07-14 12:00:13 -07002445 ql_log(ql_log_info, vha, 0x00a0,
2446 "Attempting to load firmware from flash.\n");
Giridhar Malavalia9083012010-04-12 17:59:55 -07002447
2448 if (qla82xx_fw_load_from_flash(ha) == QLA_SUCCESS) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07002449 ql_log(ql_log_info, vha, 0x00a1,
Saurav Kashyap00adc9a2012-05-15 14:34:22 -04002450 "Firmware loaded successfully from flash.\n");
Giridhar Malavalia9083012010-04-12 17:59:55 -07002451 return QLA_SUCCESS;
Chad Dupuis875efad72011-05-10 11:30:09 -07002452 } else {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07002453 ql_log(ql_log_warn, vha, 0x0108,
2454 "Firmware load from flash failed.\n");
Giridhar Malavalia9083012010-04-12 17:59:55 -07002455 }
Chad Dupuis875efad72011-05-10 11:30:09 -07002456
Giridhar Malavalia9083012010-04-12 17:59:55 -07002457try_blob_fw:
Saurav Kashyap7c3df132011-07-14 12:00:13 -07002458 ql_log(ql_log_info, vha, 0x00a2,
2459 "Attempting to load firmware from blob.\n");
Giridhar Malavalia9083012010-04-12 17:59:55 -07002460
2461 /* Load firmware blob. */
2462 blob = ha->hablob = qla2x00_request_firmware(vha);
2463 if (!blob) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07002464 ql_log(ql_log_fatal, vha, 0x00a3,
Saurav Kashyap00adc9a2012-05-15 14:34:22 -04002465 "Firmware image not present.\n");
Giridhar Malavalia9083012010-04-12 17:59:55 -07002466 goto fw_load_failed;
2467 }
2468
Harish Zunjarrao9c2b2972010-05-28 15:08:23 -07002469 /* Validating firmware blob */
2470 if (qla82xx_validate_firmware_blob(vha,
2471 QLA82XX_FLASH_ROMIMAGE)) {
2472 /* Fallback to URI format */
2473 if (qla82xx_validate_firmware_blob(vha,
2474 QLA82XX_UNIFIED_ROMIMAGE)) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07002475 ql_log(ql_log_fatal, vha, 0x00a4,
2476 "No valid firmware image found.\n");
Harish Zunjarrao9c2b2972010-05-28 15:08:23 -07002477 return QLA_FUNCTION_FAILED;
2478 }
2479 }
2480
Giridhar Malavalia9083012010-04-12 17:59:55 -07002481 if (qla82xx_fw_load_from_blob(ha) == QLA_SUCCESS) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07002482 ql_log(ql_log_info, vha, 0x00a5,
2483 "Firmware loaded successfully from binary blob.\n");
Giridhar Malavalia9083012010-04-12 17:59:55 -07002484 return QLA_SUCCESS;
Giridhar Malavalia9083012010-04-12 17:59:55 -07002485 }
Bart Van Assche8a318fe2015-07-09 07:25:25 -07002486
2487 ql_log(ql_log_fatal, vha, 0x00a6,
2488 "Firmware load failed for binary blob.\n");
2489 blob->fw = NULL;
2490 blob = NULL;
Giridhar Malavalia9083012010-04-12 17:59:55 -07002491
2492fw_load_failed:
2493 return QLA_FUNCTION_FAILED;
2494}
2495
Lalit Chandivadea5b36322010-09-03 15:20:50 -07002496int
Giridhar Malavalia9083012010-04-12 17:59:55 -07002497qla82xx_start_firmware(scsi_qla_host_t *vha)
2498{
Giridhar Malavalia9083012010-04-12 17:59:55 -07002499 uint16_t lnk;
2500 struct qla_hw_data *ha = vha->hw;
2501
2502 /* scrub dma mask expansion register */
Giridhar Malavali77e334d2010-09-03 15:20:52 -07002503 qla82xx_wr_32(ha, CRB_DMA_SHIFT, QLA82XX_DMA_SHIFT_VALUE);
Giridhar Malavalia9083012010-04-12 17:59:55 -07002504
Giridhar Malavali37113332010-07-23 15:28:34 +05002505 /* Put both the PEG CMD and RCV PEG to default state
2506 * of 0 before resetting the hardware
2507 */
2508 qla82xx_wr_32(ha, CRB_CMDPEG_STATE, 0);
2509 qla82xx_wr_32(ha, CRB_RCVPEG_STATE, 0);
2510
Giridhar Malavalia9083012010-04-12 17:59:55 -07002511 /* Overwrite stale initialization register values */
2512 qla82xx_wr_32(ha, QLA82XX_PEG_HALT_STATUS1, 0);
2513 qla82xx_wr_32(ha, QLA82XX_PEG_HALT_STATUS2, 0);
2514
2515 if (qla82xx_load_fw(vha) != QLA_SUCCESS) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07002516 ql_log(ql_log_fatal, vha, 0x00a7,
2517 "Error trying to start fw.\n");
Giridhar Malavalia9083012010-04-12 17:59:55 -07002518 return QLA_FUNCTION_FAILED;
2519 }
2520
2521 /* Handshake with the card before we register the devices. */
2522 if (qla82xx_check_cmdpeg_state(ha) != QLA_SUCCESS) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07002523 ql_log(ql_log_fatal, vha, 0x00aa,
2524 "Error during card handshake.\n");
Giridhar Malavalia9083012010-04-12 17:59:55 -07002525 return QLA_FUNCTION_FAILED;
2526 }
2527
2528 /* Negotiated Link width */
Jiang Liu10092432012-08-20 14:23:48 -06002529 pcie_capability_read_word(ha->pdev, PCI_EXP_LNKSTA, &lnk);
Giridhar Malavalia9083012010-04-12 17:59:55 -07002530 ha->link_width = (lnk >> 4) & 0x3f;
2531
2532 /* Synchronize with Receive peg */
2533 return qla82xx_check_rcvpeg_state(ha);
2534}
2535
Giridhar Malavali77e334d2010-09-03 15:20:52 -07002536static uint32_t *
Giridhar Malavalia9083012010-04-12 17:59:55 -07002537qla82xx_read_flash_data(scsi_qla_host_t *vha, uint32_t *dwptr, uint32_t faddr,
2538 uint32_t length)
2539{
2540 uint32_t i;
2541 uint32_t val;
2542 struct qla_hw_data *ha = vha->hw;
2543
2544 /* Dword reads to flash. */
2545 for (i = 0; i < length/4; i++, faddr += 4) {
2546 if (qla82xx_rom_fast_read(ha, faddr, &val)) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07002547 ql_log(ql_log_warn, vha, 0x0106,
2548 "Do ROM fast read failed.\n");
Giridhar Malavalia9083012010-04-12 17:59:55 -07002549 goto done_read;
2550 }
Bart Van Asschead950362015-07-09 07:24:08 -07002551 dwptr[i] = cpu_to_le32(val);
Giridhar Malavalia9083012010-04-12 17:59:55 -07002552 }
2553done_read:
2554 return dwptr;
2555}
2556
Giridhar Malavali77e334d2010-09-03 15:20:52 -07002557static int
Giridhar Malavalia9083012010-04-12 17:59:55 -07002558qla82xx_unprotect_flash(struct qla_hw_data *ha)
2559{
2560 int ret;
2561 uint32_t val;
Saurav Kashyap7c3df132011-07-14 12:00:13 -07002562 scsi_qla_host_t *vha = pci_get_drvdata(ha->pdev);
Giridhar Malavalia9083012010-04-12 17:59:55 -07002563
2564 ret = ql82xx_rom_lock_d(ha);
2565 if (ret < 0) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07002566 ql_log(ql_log_warn, vha, 0xb014,
2567 "ROM Lock failed.\n");
Giridhar Malavalia9083012010-04-12 17:59:55 -07002568 return ret;
2569 }
2570
2571 ret = qla82xx_read_status_reg(ha, &val);
2572 if (ret < 0)
2573 goto done_unprotect;
2574
Lalit Chandivade0547fb32010-05-28 15:08:26 -07002575 val &= ~(BLOCK_PROTECT_BITS << 2);
Giridhar Malavalia9083012010-04-12 17:59:55 -07002576 ret = qla82xx_write_status_reg(ha, val);
2577 if (ret < 0) {
Lalit Chandivade0547fb32010-05-28 15:08:26 -07002578 val |= (BLOCK_PROTECT_BITS << 2);
Giridhar Malavalia9083012010-04-12 17:59:55 -07002579 qla82xx_write_status_reg(ha, val);
2580 }
2581
2582 if (qla82xx_write_disable_flash(ha) != 0)
Saurav Kashyap7c3df132011-07-14 12:00:13 -07002583 ql_log(ql_log_warn, vha, 0xb015,
2584 "Write disable failed.\n");
Giridhar Malavalia9083012010-04-12 17:59:55 -07002585
2586done_unprotect:
Chad Dupuisd652e092011-05-10 11:30:10 -07002587 qla82xx_rom_unlock(ha);
Giridhar Malavalia9083012010-04-12 17:59:55 -07002588 return ret;
2589}
2590
Giridhar Malavali77e334d2010-09-03 15:20:52 -07002591static int
Giridhar Malavalia9083012010-04-12 17:59:55 -07002592qla82xx_protect_flash(struct qla_hw_data *ha)
2593{
2594 int ret;
2595 uint32_t val;
Saurav Kashyap7c3df132011-07-14 12:00:13 -07002596 scsi_qla_host_t *vha = pci_get_drvdata(ha->pdev);
Giridhar Malavalia9083012010-04-12 17:59:55 -07002597
2598 ret = ql82xx_rom_lock_d(ha);
2599 if (ret < 0) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07002600 ql_log(ql_log_warn, vha, 0xb016,
2601 "ROM Lock failed.\n");
Giridhar Malavalia9083012010-04-12 17:59:55 -07002602 return ret;
2603 }
2604
2605 ret = qla82xx_read_status_reg(ha, &val);
2606 if (ret < 0)
2607 goto done_protect;
2608
Lalit Chandivade0547fb32010-05-28 15:08:26 -07002609 val |= (BLOCK_PROTECT_BITS << 2);
Giridhar Malavalia9083012010-04-12 17:59:55 -07002610 /* LOCK all sectors */
2611 ret = qla82xx_write_status_reg(ha, val);
2612 if (ret < 0)
Saurav Kashyap7c3df132011-07-14 12:00:13 -07002613 ql_log(ql_log_warn, vha, 0xb017,
2614 "Write status register failed.\n");
Giridhar Malavalia9083012010-04-12 17:59:55 -07002615
2616 if (qla82xx_write_disable_flash(ha) != 0)
Saurav Kashyap7c3df132011-07-14 12:00:13 -07002617 ql_log(ql_log_warn, vha, 0xb018,
2618 "Write disable failed.\n");
Giridhar Malavalia9083012010-04-12 17:59:55 -07002619done_protect:
Chad Dupuisd652e092011-05-10 11:30:10 -07002620 qla82xx_rom_unlock(ha);
Giridhar Malavalia9083012010-04-12 17:59:55 -07002621 return ret;
2622}
2623
Giridhar Malavali77e334d2010-09-03 15:20:52 -07002624static int
Giridhar Malavalia9083012010-04-12 17:59:55 -07002625qla82xx_erase_sector(struct qla_hw_data *ha, int addr)
2626{
2627 int ret = 0;
Saurav Kashyap7c3df132011-07-14 12:00:13 -07002628 scsi_qla_host_t *vha = pci_get_drvdata(ha->pdev);
Giridhar Malavalia9083012010-04-12 17:59:55 -07002629
2630 ret = ql82xx_rom_lock_d(ha);
2631 if (ret < 0) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07002632 ql_log(ql_log_warn, vha, 0xb019,
2633 "ROM Lock failed.\n");
Giridhar Malavalia9083012010-04-12 17:59:55 -07002634 return ret;
2635 }
2636
2637 qla82xx_flash_set_write_enable(ha);
2638 qla82xx_wr_32(ha, QLA82XX_ROMUSB_ROM_ADDRESS, addr);
2639 qla82xx_wr_32(ha, QLA82XX_ROMUSB_ROM_ABYTE_CNT, 3);
2640 qla82xx_wr_32(ha, QLA82XX_ROMUSB_ROM_INSTR_OPCODE, M25P_INSTR_SE);
2641
2642 if (qla82xx_wait_rom_done(ha)) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07002643 ql_log(ql_log_warn, vha, 0xb01a,
2644 "Error waiting for rom done.\n");
Giridhar Malavalia9083012010-04-12 17:59:55 -07002645 ret = -1;
2646 goto done;
2647 }
2648 ret = qla82xx_flash_wait_write_finish(ha);
2649done:
Chad Dupuisd652e092011-05-10 11:30:10 -07002650 qla82xx_rom_unlock(ha);
Giridhar Malavalia9083012010-04-12 17:59:55 -07002651 return ret;
2652}
2653
2654/*
2655 * Address and length are byte address
2656 */
2657uint8_t *
2658qla82xx_read_optrom_data(struct scsi_qla_host *vha, uint8_t *buf,
2659 uint32_t offset, uint32_t length)
2660{
2661 scsi_block_requests(vha->host);
2662 qla82xx_read_flash_data(vha, (uint32_t *)buf, offset, length);
2663 scsi_unblock_requests(vha->host);
2664 return buf;
2665}
2666
2667static int
2668qla82xx_write_flash_data(struct scsi_qla_host *vha, uint32_t *dwptr,
2669 uint32_t faddr, uint32_t dwords)
2670{
2671 int ret;
2672 uint32_t liter;
Bart Van Assche52c82822015-07-09 07:23:26 -07002673 uint32_t rest_addr;
Giridhar Malavalia9083012010-04-12 17:59:55 -07002674 dma_addr_t optrom_dma;
2675 void *optrom = NULL;
2676 int page_mode = 0;
2677 struct qla_hw_data *ha = vha->hw;
2678
2679 ret = -1;
2680
2681 /* Prepare burst-capable write on supported ISPs. */
2682 if (page_mode && !(faddr & 0xfff) &&
2683 dwords > OPTROM_BURST_DWORDS) {
2684 optrom = dma_alloc_coherent(&ha->pdev->dev, OPTROM_BURST_SIZE,
2685 &optrom_dma, GFP_KERNEL);
2686 if (!optrom) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07002687 ql_log(ql_log_warn, vha, 0xb01b,
2688 "Unable to allocate memory "
Saurav Kashyap00adc9a2012-05-15 14:34:22 -04002689 "for optrom burst write (%x KB).\n",
Saurav Kashyap7c3df132011-07-14 12:00:13 -07002690 OPTROM_BURST_SIZE / 1024);
Giridhar Malavalia9083012010-04-12 17:59:55 -07002691 }
2692 }
2693
2694 rest_addr = ha->fdt_block_size - 1;
Giridhar Malavalia9083012010-04-12 17:59:55 -07002695
2696 ret = qla82xx_unprotect_flash(ha);
2697 if (ret) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07002698 ql_log(ql_log_warn, vha, 0xb01c,
2699 "Unable to unprotect flash for update.\n");
Giridhar Malavalia9083012010-04-12 17:59:55 -07002700 goto write_done;
2701 }
2702
2703 for (liter = 0; liter < dwords; liter++, faddr += 4, dwptr++) {
2704 /* Are we at the beginning of a sector? */
2705 if ((faddr & rest_addr) == 0) {
2706
2707 ret = qla82xx_erase_sector(ha, faddr);
2708 if (ret) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07002709 ql_log(ql_log_warn, vha, 0xb01d,
2710 "Unable to erase sector: address=%x.\n",
2711 faddr);
Giridhar Malavalia9083012010-04-12 17:59:55 -07002712 break;
2713 }
2714 }
2715
2716 /* Go with burst-write. */
2717 if (optrom && (liter + OPTROM_BURST_DWORDS) <= dwords) {
2718 /* Copy data to DMA'ble buffer. */
2719 memcpy(optrom, dwptr, OPTROM_BURST_SIZE);
2720
2721 ret = qla2x00_load_ram(vha, optrom_dma,
2722 (ha->flash_data_off | faddr),
2723 OPTROM_BURST_DWORDS);
2724 if (ret != QLA_SUCCESS) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07002725 ql_log(ql_log_warn, vha, 0xb01e,
Giridhar Malavalia9083012010-04-12 17:59:55 -07002726 "Unable to burst-write optrom segment "
2727 "(%x/%x/%llx).\n", ret,
2728 (ha->flash_data_off | faddr),
2729 (unsigned long long)optrom_dma);
Saurav Kashyap7c3df132011-07-14 12:00:13 -07002730 ql_log(ql_log_warn, vha, 0xb01f,
Giridhar Malavalia9083012010-04-12 17:59:55 -07002731 "Reverting to slow-write.\n");
2732
2733 dma_free_coherent(&ha->pdev->dev,
2734 OPTROM_BURST_SIZE, optrom, optrom_dma);
2735 optrom = NULL;
2736 } else {
2737 liter += OPTROM_BURST_DWORDS - 1;
2738 faddr += OPTROM_BURST_DWORDS - 1;
2739 dwptr += OPTROM_BURST_DWORDS - 1;
2740 continue;
2741 }
2742 }
2743
2744 ret = qla82xx_write_flash_dword(ha, faddr,
2745 cpu_to_le32(*dwptr));
2746 if (ret) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07002747 ql_dbg(ql_dbg_p3p, vha, 0xb020,
2748 "Unable to program flash address=%x data=%x.\n",
2749 faddr, *dwptr);
Giridhar Malavalia9083012010-04-12 17:59:55 -07002750 break;
2751 }
2752 }
2753
2754 ret = qla82xx_protect_flash(ha);
2755 if (ret)
Saurav Kashyap7c3df132011-07-14 12:00:13 -07002756 ql_log(ql_log_warn, vha, 0xb021,
Giridhar Malavalia9083012010-04-12 17:59:55 -07002757 "Unable to protect flash after update.\n");
2758write_done:
2759 if (optrom)
2760 dma_free_coherent(&ha->pdev->dev,
2761 OPTROM_BURST_SIZE, optrom, optrom_dma);
2762 return ret;
2763}
2764
2765int
2766qla82xx_write_optrom_data(struct scsi_qla_host *vha, uint8_t *buf,
2767 uint32_t offset, uint32_t length)
2768{
2769 int rval;
2770
2771 /* Suspend HBA. */
2772 scsi_block_requests(vha->host);
2773 rval = qla82xx_write_flash_data(vha, (uint32_t *)buf, offset,
2774 length >> 2);
2775 scsi_unblock_requests(vha->host);
2776
2777 /* Convert return ISP82xx to generic */
2778 if (rval)
2779 rval = QLA_FUNCTION_FAILED;
2780 else
2781 rval = QLA_SUCCESS;
2782 return rval;
2783}
2784
2785void
Giridhar Malavali5162cf02011-11-18 09:03:18 -08002786qla82xx_start_iocbs(scsi_qla_host_t *vha)
Giridhar Malavalia9083012010-04-12 17:59:55 -07002787{
Giridhar Malavali5162cf02011-11-18 09:03:18 -08002788 struct qla_hw_data *ha = vha->hw;
Giridhar Malavalia9083012010-04-12 17:59:55 -07002789 struct req_que *req = ha->req_q_map[0];
Giridhar Malavalia9083012010-04-12 17:59:55 -07002790 uint32_t dbval;
2791
2792 /* Adjust ring index. */
2793 req->ring_index++;
2794 if (req->ring_index == req->length) {
2795 req->ring_index = 0;
2796 req->ring_ptr = req->ring;
2797 } else
2798 req->ring_ptr++;
2799
Giridhar Malavalia9083012010-04-12 17:59:55 -07002800 dbval = 0x04 | (ha->portnum << 5);
2801
2802 dbval = dbval | (req->id << 8) | (req->ring_index << 16);
Giridhar Malavali69078692010-05-28 15:08:28 -07002803 if (ql2xdbwr)
Bart Van Assche8dfa4b5a2015-07-09 07:24:50 -07002804 qla82xx_wr_32(ha, (unsigned long)ha->nxdb_wr_ptr, dbval);
Giridhar Malavali69078692010-05-28 15:08:28 -07002805 else {
Bart Van Assche8dfa4b5a2015-07-09 07:24:50 -07002806 WRT_REG_DWORD(ha->nxdb_wr_ptr, dbval);
Giridhar Malavalia9083012010-04-12 17:59:55 -07002807 wmb();
Bart Van Assche8dfa4b5a2015-07-09 07:24:50 -07002808 while (RD_REG_DWORD(ha->nxdb_rd_ptr) != dbval) {
2809 WRT_REG_DWORD(ha->nxdb_wr_ptr, dbval);
Giridhar Malavali69078692010-05-28 15:08:28 -07002810 wmb();
2811 }
Giridhar Malavalia9083012010-04-12 17:59:55 -07002812 }
2813}
2814
Saurav Kashyapfa492632012-11-21 02:40:29 -05002815static void
2816qla82xx_rom_lock_recovery(struct qla_hw_data *ha)
Shyam Sundare6a42022010-09-07 20:55:32 -07002817{
Saurav Kashyap7c3df132011-07-14 12:00:13 -07002818 scsi_qla_host_t *vha = pci_get_drvdata(ha->pdev);
Hiral Patel4babb902014-04-11 16:54:19 -04002819 uint32_t lock_owner = 0;
Saurav Kashyap7c3df132011-07-14 12:00:13 -07002820
Hiral Patel4babb902014-04-11 16:54:19 -04002821 if (qla82xx_rom_lock(ha)) {
2822 lock_owner = qla82xx_rd_32(ha, QLA82XX_ROM_LOCK_ID);
Shyam Sundare6a42022010-09-07 20:55:32 -07002823 /* Someone else is holding the lock. */
Saurav Kashyap7c3df132011-07-14 12:00:13 -07002824 ql_log(ql_log_info, vha, 0xb022,
Hiral Patel4babb902014-04-11 16:54:19 -04002825 "Resetting rom_lock, Lock Owner %u.\n", lock_owner);
2826 }
Shyam Sundare6a42022010-09-07 20:55:32 -07002827 /*
2828 * Either we got the lock, or someone
2829 * else died while holding it.
2830 * In either case, unlock.
2831 */
Chad Dupuisd652e092011-05-10 11:30:10 -07002832 qla82xx_rom_unlock(ha);
Shyam Sundare6a42022010-09-07 20:55:32 -07002833}
2834
Giridhar Malavalia9083012010-04-12 17:59:55 -07002835/*
2836 * qla82xx_device_bootstrap
2837 * Initialize device, set DEV_READY, start fw
2838 *
2839 * Note:
2840 * IDC lock must be held upon entry
2841 *
2842 * Return:
2843 * Success : 0
2844 * Failed : 1
2845 */
2846static int
2847qla82xx_device_bootstrap(scsi_qla_host_t *vha)
2848{
Shyam Sundare6a42022010-09-07 20:55:32 -07002849 int rval = QLA_SUCCESS;
Tej Prakash03d32f92014-04-11 16:54:39 -04002850 int i;
Giridhar Malavalia9083012010-04-12 17:59:55 -07002851 uint32_t old_count, count;
2852 struct qla_hw_data *ha = vha->hw;
Tej Prakash03d32f92014-04-11 16:54:39 -04002853 int need_reset = 0;
Giridhar Malavalia9083012010-04-12 17:59:55 -07002854
Shyam Sundare6a42022010-09-07 20:55:32 -07002855 need_reset = qla82xx_need_reset(ha);
Giridhar Malavalia9083012010-04-12 17:59:55 -07002856
Shyam Sundare6a42022010-09-07 20:55:32 -07002857 if (need_reset) {
2858 /* We are trying to perform a recovery here. */
Tej Prakash03d32f92014-04-11 16:54:39 -04002859 if (ha->flags.isp82xx_fw_hung)
Shyam Sundare6a42022010-09-07 20:55:32 -07002860 qla82xx_rom_lock_recovery(ha);
Shyam Sundare6a42022010-09-07 20:55:32 -07002861 } else {
Tej Prakash03d32f92014-04-11 16:54:39 -04002862 old_count = qla82xx_rd_32(ha, QLA82XX_PEG_ALIVE_COUNTER);
2863 for (i = 0; i < 10; i++) {
2864 msleep(200);
2865 count = qla82xx_rd_32(ha, QLA82XX_PEG_ALIVE_COUNTER);
2866 if (count != old_count) {
2867 rval = QLA_SUCCESS;
2868 goto dev_ready;
2869 }
2870 }
2871 qla82xx_rom_lock_recovery(ha);
Giridhar Malavalia9083012010-04-12 17:59:55 -07002872 }
2873
Giridhar Malavalia9083012010-04-12 17:59:55 -07002874 /* set to DEV_INITIALIZING */
Saurav Kashyap7c3df132011-07-14 12:00:13 -07002875 ql_log(ql_log_info, vha, 0x009e,
2876 "HW State: INITIALIZING.\n");
Santosh Vernekar7d613ac2012-08-22 14:21:03 -04002877 qla82xx_wr_32(ha, QLA82XX_CRB_DEV_STATE, QLA8XXX_DEV_INITIALIZING);
Giridhar Malavalia9083012010-04-12 17:59:55 -07002878
2879 qla82xx_idc_unlock(ha);
2880 rval = qla82xx_start_firmware(vha);
2881 qla82xx_idc_lock(ha);
2882
2883 if (rval != QLA_SUCCESS) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07002884 ql_log(ql_log_fatal, vha, 0x00ad,
2885 "HW State: FAILED.\n");
Giridhar Malavalia9083012010-04-12 17:59:55 -07002886 qla82xx_clear_drv_active(ha);
Santosh Vernekar7d613ac2012-08-22 14:21:03 -04002887 qla82xx_wr_32(ha, QLA82XX_CRB_DEV_STATE, QLA8XXX_DEV_FAILED);
Giridhar Malavalia9083012010-04-12 17:59:55 -07002888 return rval;
2889 }
2890
2891dev_ready:
Saurav Kashyap7c3df132011-07-14 12:00:13 -07002892 ql_log(ql_log_info, vha, 0x00ae,
2893 "HW State: READY.\n");
Santosh Vernekar7d613ac2012-08-22 14:21:03 -04002894 qla82xx_wr_32(ha, QLA82XX_CRB_DEV_STATE, QLA8XXX_DEV_READY);
Giridhar Malavalia9083012010-04-12 17:59:55 -07002895
2896 return QLA_SUCCESS;
2897}
2898
Saurav Kashyap579d12b2010-12-21 16:00:14 -08002899/*
2900* qla82xx_need_qsnt_handler
2901* Code to start quiescence sequence
2902*
2903* Note:
2904* IDC lock must be held upon entry
2905*
2906* Return: void
2907*/
2908
2909static void
2910qla82xx_need_qsnt_handler(scsi_qla_host_t *vha)
2911{
2912 struct qla_hw_data *ha = vha->hw;
2913 uint32_t dev_state, drv_state, drv_active;
2914 unsigned long reset_timeout;
2915
2916 if (vha->flags.online) {
2917 /*Block any further I/O and wait for pending cmnds to complete*/
Chad Dupuis8fcd6b82012-08-22 14:21:06 -04002918 qla2x00_quiesce_io(vha);
Saurav Kashyap579d12b2010-12-21 16:00:14 -08002919 }
2920
2921 /* Set the quiescence ready bit */
2922 qla82xx_set_qsnt_ready(ha);
2923
2924 /*wait for 30 secs for other functions to ack */
2925 reset_timeout = jiffies + (30 * HZ);
2926
2927 drv_state = qla82xx_rd_32(ha, QLA82XX_CRB_DRV_STATE);
2928 drv_active = qla82xx_rd_32(ha, QLA82XX_CRB_DRV_ACTIVE);
2929 /* Its 2 that is written when qsnt is acked, moving one bit */
2930 drv_active = drv_active << 0x01;
2931
2932 while (drv_state != drv_active) {
2933
2934 if (time_after_eq(jiffies, reset_timeout)) {
2935 /* quiescence timeout, other functions didn't ack
2936 * changing the state to DEV_READY
2937 */
Saurav Kashyap7c3df132011-07-14 12:00:13 -07002938 ql_log(ql_log_info, vha, 0xb023,
Saurav Kashyap5f28d2d2012-05-15 14:34:15 -04002939 "%s : QUIESCENT TIMEOUT DRV_ACTIVE:%d "
2940 "DRV_STATE:%d.\n", QLA2XXX_DRIVER_NAME,
Saurav Kashyap7c3df132011-07-14 12:00:13 -07002941 drv_active, drv_state);
Saurav Kashyap579d12b2010-12-21 16:00:14 -08002942 qla82xx_wr_32(ha, QLA82XX_CRB_DEV_STATE,
Santosh Vernekar7d613ac2012-08-22 14:21:03 -04002943 QLA8XXX_DEV_READY);
Saurav Kashyap7c3df132011-07-14 12:00:13 -07002944 ql_log(ql_log_info, vha, 0xb025,
2945 "HW State: DEV_READY.\n");
Saurav Kashyap579d12b2010-12-21 16:00:14 -08002946 qla82xx_idc_unlock(ha);
2947 qla2x00_perform_loop_resync(vha);
2948 qla82xx_idc_lock(ha);
2949
2950 qla82xx_clear_qsnt_ready(vha);
2951 return;
2952 }
2953
2954 qla82xx_idc_unlock(ha);
2955 msleep(1000);
2956 qla82xx_idc_lock(ha);
2957
2958 drv_state = qla82xx_rd_32(ha, QLA82XX_CRB_DRV_STATE);
2959 drv_active = qla82xx_rd_32(ha, QLA82XX_CRB_DRV_ACTIVE);
2960 drv_active = drv_active << 0x01;
2961 }
2962 dev_state = qla82xx_rd_32(ha, QLA82XX_CRB_DEV_STATE);
2963 /* everyone acked so set the state to DEV_QUIESCENCE */
Santosh Vernekar7d613ac2012-08-22 14:21:03 -04002964 if (dev_state == QLA8XXX_DEV_NEED_QUIESCENT) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07002965 ql_log(ql_log_info, vha, 0xb026,
2966 "HW State: DEV_QUIESCENT.\n");
Santosh Vernekar7d613ac2012-08-22 14:21:03 -04002967 qla82xx_wr_32(ha, QLA82XX_CRB_DEV_STATE, QLA8XXX_DEV_QUIESCENT);
Saurav Kashyap579d12b2010-12-21 16:00:14 -08002968 }
2969}
2970
2971/*
2972* qla82xx_wait_for_state_change
2973* Wait for device state to change from given current state
2974*
2975* Note:
2976* IDC lock must not be held upon entry
2977*
2978* Return:
2979* Changed device state.
2980*/
2981uint32_t
2982qla82xx_wait_for_state_change(scsi_qla_host_t *vha, uint32_t curr_state)
2983{
2984 struct qla_hw_data *ha = vha->hw;
2985 uint32_t dev_state;
2986
2987 do {
2988 msleep(1000);
2989 qla82xx_idc_lock(ha);
2990 dev_state = qla82xx_rd_32(ha, QLA82XX_CRB_DEV_STATE);
2991 qla82xx_idc_unlock(ha);
2992 } while (dev_state == curr_state);
2993
2994 return dev_state;
2995}
2996
Santosh Vernekar7d613ac2012-08-22 14:21:03 -04002997void
2998qla8xxx_dev_failed_handler(scsi_qla_host_t *vha)
Giridhar Malavalia9083012010-04-12 17:59:55 -07002999{
3000 struct qla_hw_data *ha = vha->hw;
3001
3002 /* Disable the board */
Saurav Kashyap7c3df132011-07-14 12:00:13 -07003003 ql_log(ql_log_fatal, vha, 0x00b8,
3004 "Disabling the board.\n");
Giridhar Malavalia9083012010-04-12 17:59:55 -07003005
Saurav Kashyap1459c0e2012-08-22 14:21:25 -04003006 if (IS_QLA82XX(ha)) {
3007 qla82xx_clear_drv_active(ha);
3008 qla82xx_idc_unlock(ha);
Atul Deshmukh7ec0eff2013-08-27 01:37:28 -04003009 } else if (IS_QLA8044(ha)) {
Saurav Kashyapc41afc92013-11-07 02:54:56 -05003010 qla8044_clear_drv_active(ha);
Atul Deshmukh7ec0eff2013-08-27 01:37:28 -04003011 qla8044_idc_unlock(ha);
Saurav Kashyap1459c0e2012-08-22 14:21:25 -04003012 }
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) {
Saurav Kashyapedaa5c72014-04-11 16:54:14 -04003135 if ((fw_major_version != ha->fw_major_version ||
Giridhar Malavali31731672011-08-16 11:31:54 -07003136 fw_minor_version != ha->fw_minor_version ||
Saurav Kashyapedaa5c72014-04-11 16:54:14 -04003137 fw_subminor_version != ha->fw_subminor_version) ||
3138 (ha->prev_minidump_failed)) {
Atul Deshmukh7ec0eff2013-08-27 01:37:28 -04003139 ql_dbg(ql_dbg_p3p, vha, 0xb02d,
Saurav Kashyapedaa5c72014-04-11 16:54:14 -04003140 "Firmware version differs Previous version: %d:%d:%d - New version: %d:%d:%d, prev_minidump_failed: %d.\n",
Giridhar Malavali9bc3bf22012-05-15 14:34:26 -04003141 fw_major_version, fw_minor_version,
3142 fw_subminor_version,
Giridhar Malavali31731672011-08-16 11:31:54 -07003143 ha->fw_major_version,
3144 ha->fw_minor_version,
Saurav Kashyapedaa5c72014-04-11 16:54:14 -04003145 ha->fw_subminor_version,
3146 ha->prev_minidump_failed);
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
Joe Carnuccio1ae47cf2013-08-27 01:37:36 -04003334int qla82xx_read_temperature(scsi_qla_host_t *vha)
3335{
3336 uint32_t temp;
3337
3338 temp = qla82xx_rd_32(vha->hw, CRB_TEMP_STATE);
3339 return qla82xx_get_temp_val(temp);
3340}
3341
Chad Dupuisc8f65442011-11-18 09:02:17 -08003342void qla82xx_clear_pending_mbx(scsi_qla_host_t *vha)
3343{
3344 struct qla_hw_data *ha = vha->hw;
3345
3346 if (ha->flags.mbox_busy) {
3347 ha->flags.mbox_int = 1;
Giridhar Malavali8937f2f2011-11-18 09:02:18 -08003348 ha->flags.mbox_busy = 0;
Chad Dupuisc8f65442011-11-18 09:02:17 -08003349 ql_log(ql_log_warn, vha, 0x6010,
3350 "Doing premature completion of mbx command.\n");
gurinder.shergill@hp.com36439832013-04-23 10:13:17 -07003351 if (test_and_clear_bit(MBX_INTR_WAIT, &ha->mbx_cmd_flags))
Chad Dupuisc8f65442011-11-18 09:02:17 -08003352 complete(&ha->mbx_intr_comp);
3353 }
3354}
3355
Giridhar Malavalia9083012010-04-12 17:59:55 -07003356void qla82xx_watchdog(scsi_qla_host_t *vha)
3357{
Giridhar Malavali71905752011-02-23 15:27:10 -08003358 uint32_t dev_state, halt_status;
Giridhar Malavalia9083012010-04-12 17:59:55 -07003359 struct qla_hw_data *ha = vha->hw;
3360
Giridhar Malavalia9083012010-04-12 17:59:55 -07003361 /* don't poll if reset is going on */
Santosh Vernekar7d613ac2012-08-22 14:21:03 -04003362 if (!ha->flags.nic_core_reset_hdlr_active) {
Giridhar Malavali71905752011-02-23 15:27:10 -08003363 dev_state = qla82xx_rd_32(ha, QLA82XX_CRB_DEV_STATE);
Giridhar Malavali5988aeb2012-05-15 14:34:12 -04003364 if (qla82xx_check_temp(vha)) {
3365 set_bit(ISP_UNRECOVERABLE, &vha->dpc_flags);
3366 ha->flags.isp82xx_fw_hung = 1;
3367 qla82xx_clear_pending_mbx(vha);
Santosh Vernekar7d613ac2012-08-22 14:21:03 -04003368 } else if (dev_state == QLA8XXX_DEV_NEED_RESET &&
Giridhar Malavali71905752011-02-23 15:27:10 -08003369 !test_bit(ISP_ABORT_NEEDED, &vha->dpc_flags)) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07003370 ql_log(ql_log_warn, vha, 0x6001,
3371 "Adapter reset needed.\n");
Giridhar Malavalia9083012010-04-12 17:59:55 -07003372 set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
Santosh Vernekar7d613ac2012-08-22 14:21:03 -04003373 } else if (dev_state == QLA8XXX_DEV_NEED_QUIESCENT &&
Saurav Kashyap579d12b2010-12-21 16:00:14 -08003374 !test_bit(ISP_QUIESCE_NEEDED, &vha->dpc_flags)) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07003375 ql_log(ql_log_warn, vha, 0x6002,
3376 "Quiescent needed.\n");
Saurav Kashyap579d12b2010-12-21 16:00:14 -08003377 set_bit(ISP_QUIESCE_NEEDED, &vha->dpc_flags);
Santosh Vernekar7d613ac2012-08-22 14:21:03 -04003378 } else if (dev_state == QLA8XXX_DEV_FAILED &&
Chad Dupuis7916bb92012-08-22 14:20:57 -04003379 !test_bit(ISP_UNRECOVERABLE, &vha->dpc_flags) &&
3380 vha->flags.online == 1) {
3381 ql_log(ql_log_warn, vha, 0xb055,
3382 "Adapter state is failed. Offlining.\n");
3383 set_bit(ISP_UNRECOVERABLE, &vha->dpc_flags);
3384 ha->flags.isp82xx_fw_hung = 1;
3385 qla82xx_clear_pending_mbx(vha);
Giridhar Malavalia9083012010-04-12 17:59:55 -07003386 } else {
Giridhar Malavali71905752011-02-23 15:27:10 -08003387 if (qla82xx_check_fw_alive(vha)) {
Giridhar Malavali63154912011-11-18 09:02:19 -08003388 ql_dbg(ql_dbg_timer, vha, 0x6011,
3389 "disabling pause transmit on port 0 & 1.\n");
3390 qla82xx_wr_32(ha, QLA82XX_CRB_NIU + 0x98,
3391 CRB_NIU_XG_PAUSE_CTL_P0|CRB_NIU_XG_PAUSE_CTL_P1);
Giridhar Malavali71905752011-02-23 15:27:10 -08003392 halt_status = qla82xx_rd_32(ha,
3393 QLA82XX_PEG_HALT_STATUS1);
Giridhar Malavali63154912011-11-18 09:02:19 -08003394 ql_log(ql_log_info, vha, 0x6005,
Saurav Kashyap7c3df132011-07-14 12:00:13 -07003395 "dumping hw/fw registers:.\n "
3396 " PEG_HALT_STATUS1: 0x%x, PEG_HALT_STATUS2: 0x%x,.\n "
3397 " PEG_NET_0_PC: 0x%x, PEG_NET_1_PC: 0x%x,.\n "
3398 " PEG_NET_2_PC: 0x%x, PEG_NET_3_PC: 0x%x,.\n "
3399 " PEG_NET_4_PC: 0x%x.\n", halt_status,
Giridhar Malavali0e8edb02011-03-30 11:46:28 -07003400 qla82xx_rd_32(ha, QLA82XX_PEG_HALT_STATUS2),
3401 qla82xx_rd_32(ha,
3402 QLA82XX_CRB_PEG_NET_0 + 0x3c),
3403 qla82xx_rd_32(ha,
3404 QLA82XX_CRB_PEG_NET_1 + 0x3c),
3405 qla82xx_rd_32(ha,
3406 QLA82XX_CRB_PEG_NET_2 + 0x3c),
3407 qla82xx_rd_32(ha,
3408 QLA82XX_CRB_PEG_NET_3 + 0x3c),
3409 qla82xx_rd_32(ha,
3410 QLA82XX_CRB_PEG_NET_4 + 0x3c));
Giridhar Malavali2cc97962012-02-09 11:14:12 -08003411 if (((halt_status & 0x1fffff00) >> 8) == 0x67)
Chad Dupuis10a340e2011-11-18 09:02:16 -08003412 ql_log(ql_log_warn, vha, 0xb052,
3413 "Firmware aborted with "
3414 "error code 0x00006700. Device is "
3415 "being reset.\n");
Giridhar Malavali71905752011-02-23 15:27:10 -08003416 if (halt_status & HALT_STATUS_UNRECOVERABLE) {
3417 set_bit(ISP_UNRECOVERABLE,
3418 &vha->dpc_flags);
3419 } else {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07003420 ql_log(ql_log_info, vha, 0x6006,
3421 "Detect abort needed.\n");
Giridhar Malavali71905752011-02-23 15:27:10 -08003422 set_bit(ISP_ABORT_NEEDED,
3423 &vha->dpc_flags);
3424 }
Giridhar Malavali71905752011-02-23 15:27:10 -08003425 ha->flags.isp82xx_fw_hung = 1;
Chad Dupuisc8f65442011-11-18 09:02:17 -08003426 ql_log(ql_log_warn, vha, 0x6007, "Firmware hung.\n");
3427 qla82xx_clear_pending_mbx(vha);
Giridhar Malavali71905752011-02-23 15:27:10 -08003428 }
Giridhar Malavalia9083012010-04-12 17:59:55 -07003429 }
3430 }
3431}
3432
3433int qla82xx_load_risc(scsi_qla_host_t *vha, uint32_t *srisc_addr)
3434{
Atul Deshmukh7ec0eff2013-08-27 01:37:28 -04003435 int rval = -1;
3436 struct qla_hw_data *ha = vha->hw;
3437
3438 if (IS_QLA82XX(ha))
3439 rval = qla82xx_device_state_handler(vha);
3440 else if (IS_QLA8044(ha)) {
3441 qla8044_idc_lock(ha);
3442 /* Decide the reset ownership */
3443 qla83xx_reset_ownership(vha);
3444 qla8044_idc_unlock(ha);
3445 rval = qla8044_device_state_handler(vha);
3446 }
Giridhar Malavalia9083012010-04-12 17:59:55 -07003447 return rval;
3448}
3449
Giridhar Malavali08de2842011-08-16 11:31:44 -07003450void
3451qla82xx_set_reset_owner(scsi_qla_host_t *vha)
3452{
3453 struct qla_hw_data *ha = vha->hw;
Atul Deshmukh7ec0eff2013-08-27 01:37:28 -04003454 uint32_t dev_state = 0;
Giridhar Malavali08de2842011-08-16 11:31:44 -07003455
Atul Deshmukh7ec0eff2013-08-27 01:37:28 -04003456 if (IS_QLA82XX(ha))
3457 dev_state = qla82xx_rd_32(ha, QLA82XX_CRB_DEV_STATE);
3458 else if (IS_QLA8044(ha))
3459 dev_state = qla8044_rd_direct(vha, QLA8044_CRB_DEV_STATE_INDEX);
3460
Santosh Vernekar7d613ac2012-08-22 14:21:03 -04003461 if (dev_state == QLA8XXX_DEV_READY) {
Giridhar Malavali08de2842011-08-16 11:31:44 -07003462 ql_log(ql_log_info, vha, 0xb02f,
3463 "HW State: NEED RESET\n");
Atul Deshmukh7ec0eff2013-08-27 01:37:28 -04003464 if (IS_QLA82XX(ha)) {
3465 qla82xx_wr_32(ha, QLA82XX_CRB_DEV_STATE,
3466 QLA8XXX_DEV_NEED_RESET);
3467 ha->flags.nic_core_reset_owner = 1;
3468 ql_dbg(ql_dbg_p3p, vha, 0xb030,
3469 "reset_owner is 0x%x\n", ha->portnum);
3470 } else if (IS_QLA8044(ha))
3471 qla8044_wr_direct(vha, QLA8044_CRB_DEV_STATE_INDEX,
3472 QLA8XXX_DEV_NEED_RESET);
Giridhar Malavali08de2842011-08-16 11:31:44 -07003473 } else
3474 ql_log(ql_log_info, vha, 0xb031,
3475 "Device state is 0x%x = %s.\n",
3476 dev_state,
3477 dev_state < MAX_STATES ? qdev_state(dev_state) : "Unknown");
3478}
3479
Giridhar Malavalia9083012010-04-12 17:59:55 -07003480/*
3481 * qla82xx_abort_isp
3482 * Resets ISP and aborts all outstanding commands.
3483 *
3484 * Input:
3485 * ha = adapter block pointer.
3486 *
3487 * Returns:
3488 * 0 = success
3489 */
3490int
3491qla82xx_abort_isp(scsi_qla_host_t *vha)
3492{
Atul Deshmukh7ec0eff2013-08-27 01:37:28 -04003493 int rval = -1;
Giridhar Malavalia9083012010-04-12 17:59:55 -07003494 struct qla_hw_data *ha = vha->hw;
Giridhar Malavalia9083012010-04-12 17:59:55 -07003495
3496 if (vha->device_flags & DFLG_DEV_FAILED) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07003497 ql_log(ql_log_warn, vha, 0x8024,
3498 "Device in failed state, exiting.\n");
Giridhar Malavalia9083012010-04-12 17:59:55 -07003499 return QLA_SUCCESS;
3500 }
Santosh Vernekar7d613ac2012-08-22 14:21:03 -04003501 ha->flags.nic_core_reset_hdlr_active = 1;
Giridhar Malavalia9083012010-04-12 17:59:55 -07003502
3503 qla82xx_idc_lock(ha);
Giridhar Malavali08de2842011-08-16 11:31:44 -07003504 qla82xx_set_reset_owner(vha);
Giridhar Malavalia9083012010-04-12 17:59:55 -07003505 qla82xx_idc_unlock(ha);
3506
Atul Deshmukh7ec0eff2013-08-27 01:37:28 -04003507 if (IS_QLA82XX(ha))
3508 rval = qla82xx_device_state_handler(vha);
3509 else if (IS_QLA8044(ha)) {
3510 qla8044_idc_lock(ha);
3511 /* Decide the reset ownership */
3512 qla83xx_reset_ownership(vha);
3513 qla8044_idc_unlock(ha);
3514 rval = qla8044_device_state_handler(vha);
3515 }
Giridhar Malavalia9083012010-04-12 17:59:55 -07003516
3517 qla82xx_idc_lock(ha);
3518 qla82xx_clear_rst_ready(ha);
3519 qla82xx_idc_unlock(ha);
3520
Santosh Vernekarcdbb0a4f2010-05-28 15:08:25 -07003521 if (rval == QLA_SUCCESS) {
Giridhar Malavali71905752011-02-23 15:27:10 -08003522 ha->flags.isp82xx_fw_hung = 0;
Santosh Vernekar7d613ac2012-08-22 14:21:03 -04003523 ha->flags.nic_core_reset_hdlr_active = 0;
Giridhar Malavalia9083012010-04-12 17:59:55 -07003524 qla82xx_restart_isp(vha);
Santosh Vernekarcdbb0a4f2010-05-28 15:08:25 -07003525 }
Giridhar Malavalif1af6202010-05-04 15:01:34 -07003526
3527 if (rval) {
3528 vha->flags.online = 1;
3529 if (test_bit(ISP_ABORT_RETRY, &vha->dpc_flags)) {
3530 if (ha->isp_abort_cnt == 0) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07003531 ql_log(ql_log_warn, vha, 0x8027,
3532 "ISP error recover failed - board "
3533 "disabled.\n");
Giridhar Malavalif1af6202010-05-04 15:01:34 -07003534 /*
3535 * The next call disables the board
3536 * completely.
3537 */
3538 ha->isp_ops->reset_adapter(vha);
3539 vha->flags.online = 0;
3540 clear_bit(ISP_ABORT_RETRY,
3541 &vha->dpc_flags);
3542 rval = QLA_SUCCESS;
3543 } else { /* schedule another ISP abort */
3544 ha->isp_abort_cnt--;
Saurav Kashyap7c3df132011-07-14 12:00:13 -07003545 ql_log(ql_log_warn, vha, 0x8036,
3546 "ISP abort - retry remaining %d.\n",
3547 ha->isp_abort_cnt);
Giridhar Malavalif1af6202010-05-04 15:01:34 -07003548 rval = QLA_FUNCTION_FAILED;
3549 }
3550 } else {
3551 ha->isp_abort_cnt = MAX_RETRIES_OF_ISP_ABORT;
Saurav Kashyap7c3df132011-07-14 12:00:13 -07003552 ql_dbg(ql_dbg_taskm, vha, 0x8029,
3553 "ISP error recovery - retrying (%d) more times.\n",
3554 ha->isp_abort_cnt);
Giridhar Malavalif1af6202010-05-04 15:01:34 -07003555 set_bit(ISP_ABORT_RETRY, &vha->dpc_flags);
3556 rval = QLA_FUNCTION_FAILED;
3557 }
3558 }
Giridhar Malavalia9083012010-04-12 17:59:55 -07003559 return rval;
3560}
3561
3562/*
3563 * qla82xx_fcoe_ctx_reset
3564 * Perform a quick reset and aborts all outstanding commands.
3565 * This will only perform an FCoE context reset and avoids a full blown
3566 * chip reset.
3567 *
3568 * Input:
3569 * ha = adapter block pointer.
3570 * is_reset_path = flag for identifying the reset path.
3571 *
3572 * Returns:
3573 * 0 = success
3574 */
3575int qla82xx_fcoe_ctx_reset(scsi_qla_host_t *vha)
3576{
3577 int rval = QLA_FUNCTION_FAILED;
3578
3579 if (vha->flags.online) {
3580 /* Abort all outstanding commands, so as to be requeued later */
3581 qla2x00_abort_isp_cleanup(vha);
3582 }
3583
3584 /* Stop currently executing firmware.
3585 * This will destroy existing FCoE context at the F/W end.
3586 */
3587 qla2x00_try_to_stop_firmware(vha);
3588
3589 /* Restart. Creates a new FCoE context on INIT_FIRMWARE. */
3590 rval = qla82xx_restart_isp(vha);
3591
3592 return rval;
3593}
3594
3595/*
3596 * qla2x00_wait_for_fcoe_ctx_reset
3597 * Wait till the FCoE context is reset.
3598 *
3599 * Note:
3600 * Does context switching here.
3601 * Release SPIN_LOCK (if any) before calling this routine.
3602 *
3603 * Return:
3604 * Success (fcoe_ctx reset is done) : 0
3605 * Failed (fcoe_ctx reset not completed within max loop timout ) : 1
3606 */
3607int qla2x00_wait_for_fcoe_ctx_reset(scsi_qla_host_t *vha)
3608{
3609 int status = QLA_FUNCTION_FAILED;
3610 unsigned long wait_reset;
3611
3612 wait_reset = jiffies + (MAX_LOOP_TIMEOUT * HZ);
3613 while ((test_bit(FCOE_CTX_RESET_NEEDED, &vha->dpc_flags) ||
3614 test_bit(ABORT_ISP_ACTIVE, &vha->dpc_flags))
3615 && time_before(jiffies, wait_reset)) {
3616
3617 set_current_state(TASK_UNINTERRUPTIBLE);
3618 schedule_timeout(HZ);
3619
3620 if (!test_bit(FCOE_CTX_RESET_NEEDED, &vha->dpc_flags) &&
3621 !test_bit(ABORT_ISP_ACTIVE, &vha->dpc_flags)) {
3622 status = QLA_SUCCESS;
3623 break;
3624 }
3625 }
Saurav Kashyap7c3df132011-07-14 12:00:13 -07003626 ql_dbg(ql_dbg_p3p, vha, 0xb027,
Joe Perchesd8424f62011-11-18 09:03:06 -08003627 "%s: status=%d.\n", __func__, status);
Giridhar Malavalia9083012010-04-12 17:59:55 -07003628
3629 return status;
3630}
Giridhar Malavali71905752011-02-23 15:27:10 -08003631
3632void
3633qla82xx_chip_reset_cleanup(scsi_qla_host_t *vha)
3634{
Atul Deshmukh7ec0eff2013-08-27 01:37:28 -04003635 int i, fw_state = 0;
Giridhar Malavali71905752011-02-23 15:27:10 -08003636 unsigned long flags;
3637 struct qla_hw_data *ha = vha->hw;
3638
3639 /* Check if 82XX firmware is alive or not
3640 * We may have arrived here from NEED_RESET
3641 * detection only
3642 */
3643 if (!ha->flags.isp82xx_fw_hung) {
3644 for (i = 0; i < 2; i++) {
3645 msleep(1000);
Atul Deshmukh7ec0eff2013-08-27 01:37:28 -04003646 if (IS_QLA82XX(ha))
3647 fw_state = qla82xx_check_fw_alive(vha);
3648 else if (IS_QLA8044(ha))
3649 fw_state = qla8044_check_fw_alive(vha);
3650 if (fw_state) {
Giridhar Malavali71905752011-02-23 15:27:10 -08003651 ha->flags.isp82xx_fw_hung = 1;
Chad Dupuisc8f65442011-11-18 09:02:17 -08003652 qla82xx_clear_pending_mbx(vha);
Giridhar Malavali71905752011-02-23 15:27:10 -08003653 break;
3654 }
3655 }
3656 }
Saurav Kashyap7c3df132011-07-14 12:00:13 -07003657 ql_dbg(ql_dbg_init, vha, 0x00b0,
3658 "Entered %s fw_hung=%d.\n",
3659 __func__, ha->flags.isp82xx_fw_hung);
Giridhar Malavali71905752011-02-23 15:27:10 -08003660
3661 /* Abort all commands gracefully if fw NOT hung */
3662 if (!ha->flags.isp82xx_fw_hung) {
3663 int cnt, que;
3664 srb_t *sp;
3665 struct req_que *req;
3666
3667 spin_lock_irqsave(&ha->hardware_lock, flags);
3668 for (que = 0; que < ha->max_req_queues; que++) {
3669 req = ha->req_q_map[que];
3670 if (!req)
3671 continue;
Chad Dupuis8d93f552013-01-30 03:34:37 -05003672 for (cnt = 1; cnt < req->num_outstanding_cmds; cnt++) {
Giridhar Malavali71905752011-02-23 15:27:10 -08003673 sp = req->outstanding_cmds[cnt];
3674 if (sp) {
Giridhar Malavaliaf13b702014-04-11 16:54:12 -04003675 if ((!sp->u.scmd.ctx ||
3676 (sp->flags &
3677 SRB_FCP_CMND_DMA_VALID)) &&
3678 !ha->flags.isp82xx_fw_hung) {
Giridhar Malavali71905752011-02-23 15:27:10 -08003679 spin_unlock_irqrestore(
3680 &ha->hardware_lock, flags);
3681 if (ha->isp_ops->abort_command(sp)) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07003682 ql_log(ql_log_info, vha,
3683 0x00b1,
3684 "mbx abort failed.\n");
Giridhar Malavali71905752011-02-23 15:27:10 -08003685 } else {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07003686 ql_log(ql_log_info, vha,
3687 0x00b2,
3688 "mbx abort success.\n");
Giridhar Malavali71905752011-02-23 15:27:10 -08003689 }
3690 spin_lock_irqsave(&ha->hardware_lock, flags);
3691 }
3692 }
3693 }
3694 }
3695 spin_unlock_irqrestore(&ha->hardware_lock, flags);
3696
3697 /* Wait for pending cmds (physical and virtual) to complete */
3698 if (!qla2x00_eh_wait_for_pending_commands(vha, 0, 0,
3699 WAIT_HOST) == QLA_SUCCESS) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07003700 ql_dbg(ql_dbg_init, vha, 0x00b3,
3701 "Done wait for "
3702 "pending commands.\n");
Giridhar Malavali71905752011-02-23 15:27:10 -08003703 }
3704 }
3705}
Giridhar Malavali08de2842011-08-16 11:31:44 -07003706
3707/* Minidump related functions */
Giridhar Malavali08de2842011-08-16 11:31:44 -07003708static int
3709qla82xx_minidump_process_control(scsi_qla_host_t *vha,
3710 qla82xx_md_entry_hdr_t *entry_hdr, uint32_t **d_ptr)
3711{
3712 struct qla_hw_data *ha = vha->hw;
3713 struct qla82xx_md_entry_crb *crb_entry;
3714 uint32_t read_value, opcode, poll_time;
3715 uint32_t addr, index, crb_addr;
3716 unsigned long wtime;
3717 struct qla82xx_md_template_hdr *tmplt_hdr;
3718 uint32_t rval = QLA_SUCCESS;
3719 int i;
3720
3721 tmplt_hdr = (struct qla82xx_md_template_hdr *)ha->md_tmplt_hdr;
3722 crb_entry = (struct qla82xx_md_entry_crb *)entry_hdr;
3723 crb_addr = crb_entry->addr;
3724
3725 for (i = 0; i < crb_entry->op_count; i++) {
3726 opcode = crb_entry->crb_ctrl.opcode;
3727 if (opcode & QLA82XX_DBG_OPCODE_WR) {
3728 qla82xx_md_rw_32(ha, crb_addr,
3729 crb_entry->value_1, 1);
3730 opcode &= ~QLA82XX_DBG_OPCODE_WR;
3731 }
3732
3733 if (opcode & QLA82XX_DBG_OPCODE_RW) {
3734 read_value = qla82xx_md_rw_32(ha, crb_addr, 0, 0);
3735 qla82xx_md_rw_32(ha, crb_addr, read_value, 1);
3736 opcode &= ~QLA82XX_DBG_OPCODE_RW;
3737 }
3738
3739 if (opcode & QLA82XX_DBG_OPCODE_AND) {
3740 read_value = qla82xx_md_rw_32(ha, crb_addr, 0, 0);
3741 read_value &= crb_entry->value_2;
3742 opcode &= ~QLA82XX_DBG_OPCODE_AND;
3743 if (opcode & QLA82XX_DBG_OPCODE_OR) {
3744 read_value |= crb_entry->value_3;
3745 opcode &= ~QLA82XX_DBG_OPCODE_OR;
3746 }
3747 qla82xx_md_rw_32(ha, crb_addr, read_value, 1);
3748 }
3749
3750 if (opcode & QLA82XX_DBG_OPCODE_OR) {
3751 read_value = qla82xx_md_rw_32(ha, crb_addr, 0, 0);
3752 read_value |= crb_entry->value_3;
3753 qla82xx_md_rw_32(ha, crb_addr, read_value, 1);
3754 opcode &= ~QLA82XX_DBG_OPCODE_OR;
3755 }
3756
3757 if (opcode & QLA82XX_DBG_OPCODE_POLL) {
3758 poll_time = crb_entry->crb_strd.poll_timeout;
3759 wtime = jiffies + poll_time;
3760 read_value = qla82xx_md_rw_32(ha, crb_addr, 0, 0);
3761
3762 do {
3763 if ((read_value & crb_entry->value_2)
3764 == crb_entry->value_1)
3765 break;
3766 else if (time_after_eq(jiffies, wtime)) {
3767 /* capturing dump failed */
3768 rval = QLA_FUNCTION_FAILED;
3769 break;
3770 } else
3771 read_value = qla82xx_md_rw_32(ha,
3772 crb_addr, 0, 0);
3773 } while (1);
3774 opcode &= ~QLA82XX_DBG_OPCODE_POLL;
3775 }
3776
3777 if (opcode & QLA82XX_DBG_OPCODE_RDSTATE) {
3778 if (crb_entry->crb_strd.state_index_a) {
3779 index = crb_entry->crb_strd.state_index_a;
3780 addr = tmplt_hdr->saved_state_array[index];
3781 } else
3782 addr = crb_addr;
3783
3784 read_value = qla82xx_md_rw_32(ha, addr, 0, 0);
3785 index = crb_entry->crb_ctrl.state_index_v;
3786 tmplt_hdr->saved_state_array[index] = read_value;
3787 opcode &= ~QLA82XX_DBG_OPCODE_RDSTATE;
3788 }
3789
3790 if (opcode & QLA82XX_DBG_OPCODE_WRSTATE) {
3791 if (crb_entry->crb_strd.state_index_a) {
3792 index = crb_entry->crb_strd.state_index_a;
3793 addr = tmplt_hdr->saved_state_array[index];
3794 } else
3795 addr = crb_addr;
3796
3797 if (crb_entry->crb_ctrl.state_index_v) {
3798 index = crb_entry->crb_ctrl.state_index_v;
3799 read_value =
3800 tmplt_hdr->saved_state_array[index];
3801 } else
3802 read_value = crb_entry->value_1;
3803
3804 qla82xx_md_rw_32(ha, addr, read_value, 1);
3805 opcode &= ~QLA82XX_DBG_OPCODE_WRSTATE;
3806 }
3807
3808 if (opcode & QLA82XX_DBG_OPCODE_MDSTATE) {
3809 index = crb_entry->crb_ctrl.state_index_v;
3810 read_value = tmplt_hdr->saved_state_array[index];
3811 read_value <<= crb_entry->crb_ctrl.shl;
3812 read_value >>= crb_entry->crb_ctrl.shr;
3813 if (crb_entry->value_2)
3814 read_value &= crb_entry->value_2;
3815 read_value |= crb_entry->value_3;
3816 read_value += crb_entry->value_1;
3817 tmplt_hdr->saved_state_array[index] = read_value;
3818 opcode &= ~QLA82XX_DBG_OPCODE_MDSTATE;
3819 }
3820 crb_addr += crb_entry->crb_strd.addr_stride;
3821 }
3822 return rval;
3823}
3824
3825static void
3826qla82xx_minidump_process_rdocm(scsi_qla_host_t *vha,
3827 qla82xx_md_entry_hdr_t *entry_hdr, uint32_t **d_ptr)
3828{
3829 struct qla_hw_data *ha = vha->hw;
3830 uint32_t r_addr, r_stride, loop_cnt, i, r_value;
3831 struct qla82xx_md_entry_rdocm *ocm_hdr;
3832 uint32_t *data_ptr = *d_ptr;
3833
3834 ocm_hdr = (struct qla82xx_md_entry_rdocm *)entry_hdr;
3835 r_addr = ocm_hdr->read_addr;
3836 r_stride = ocm_hdr->read_addr_stride;
3837 loop_cnt = ocm_hdr->op_count;
3838
3839 for (i = 0; i < loop_cnt; i++) {
Bart Van Assche8dfa4b5a2015-07-09 07:24:50 -07003840 r_value = RD_REG_DWORD(r_addr + ha->nx_pcibase);
Giridhar Malavali08de2842011-08-16 11:31:44 -07003841 *data_ptr++ = cpu_to_le32(r_value);
3842 r_addr += r_stride;
3843 }
3844 *d_ptr = data_ptr;
3845}
3846
3847static void
3848qla82xx_minidump_process_rdmux(scsi_qla_host_t *vha,
3849 qla82xx_md_entry_hdr_t *entry_hdr, uint32_t **d_ptr)
3850{
3851 struct qla_hw_data *ha = vha->hw;
3852 uint32_t r_addr, s_stride, s_addr, s_value, loop_cnt, i, r_value;
3853 struct qla82xx_md_entry_mux *mux_hdr;
3854 uint32_t *data_ptr = *d_ptr;
3855
3856 mux_hdr = (struct qla82xx_md_entry_mux *)entry_hdr;
3857 r_addr = mux_hdr->read_addr;
3858 s_addr = mux_hdr->select_addr;
3859 s_stride = mux_hdr->select_value_stride;
3860 s_value = mux_hdr->select_value;
3861 loop_cnt = mux_hdr->op_count;
3862
3863 for (i = 0; i < loop_cnt; i++) {
3864 qla82xx_md_rw_32(ha, s_addr, s_value, 1);
3865 r_value = qla82xx_md_rw_32(ha, r_addr, 0, 0);
3866 *data_ptr++ = cpu_to_le32(s_value);
3867 *data_ptr++ = cpu_to_le32(r_value);
3868 s_value += s_stride;
3869 }
3870 *d_ptr = data_ptr;
3871}
3872
3873static void
3874qla82xx_minidump_process_rdcrb(scsi_qla_host_t *vha,
3875 qla82xx_md_entry_hdr_t *entry_hdr, uint32_t **d_ptr)
3876{
3877 struct qla_hw_data *ha = vha->hw;
3878 uint32_t r_addr, r_stride, loop_cnt, i, r_value;
3879 struct qla82xx_md_entry_crb *crb_hdr;
3880 uint32_t *data_ptr = *d_ptr;
3881
3882 crb_hdr = (struct qla82xx_md_entry_crb *)entry_hdr;
3883 r_addr = crb_hdr->addr;
3884 r_stride = crb_hdr->crb_strd.addr_stride;
3885 loop_cnt = crb_hdr->op_count;
3886
3887 for (i = 0; i < loop_cnt; i++) {
3888 r_value = qla82xx_md_rw_32(ha, r_addr, 0, 0);
3889 *data_ptr++ = cpu_to_le32(r_addr);
3890 *data_ptr++ = cpu_to_le32(r_value);
3891 r_addr += r_stride;
3892 }
3893 *d_ptr = data_ptr;
3894}
3895
3896static int
3897qla82xx_minidump_process_l2tag(scsi_qla_host_t *vha,
3898 qla82xx_md_entry_hdr_t *entry_hdr, uint32_t **d_ptr)
3899{
3900 struct qla_hw_data *ha = vha->hw;
3901 uint32_t addr, r_addr, c_addr, t_r_addr;
3902 uint32_t i, k, loop_count, t_value, r_cnt, r_value;
3903 unsigned long p_wait, w_time, p_mask;
3904 uint32_t c_value_w, c_value_r;
3905 struct qla82xx_md_entry_cache *cache_hdr;
3906 int rval = QLA_FUNCTION_FAILED;
3907 uint32_t *data_ptr = *d_ptr;
3908
3909 cache_hdr = (struct qla82xx_md_entry_cache *)entry_hdr;
3910 loop_count = cache_hdr->op_count;
3911 r_addr = cache_hdr->read_addr;
3912 c_addr = cache_hdr->control_addr;
3913 c_value_w = cache_hdr->cache_ctrl.write_value;
3914
3915 t_r_addr = cache_hdr->tag_reg_addr;
3916 t_value = cache_hdr->addr_ctrl.init_tag_value;
3917 r_cnt = cache_hdr->read_ctrl.read_addr_cnt;
3918 p_wait = cache_hdr->cache_ctrl.poll_wait;
3919 p_mask = cache_hdr->cache_ctrl.poll_mask;
3920
3921 for (i = 0; i < loop_count; i++) {
3922 qla82xx_md_rw_32(ha, t_r_addr, t_value, 1);
3923 if (c_value_w)
3924 qla82xx_md_rw_32(ha, c_addr, c_value_w, 1);
3925
3926 if (p_mask) {
3927 w_time = jiffies + p_wait;
3928 do {
3929 c_value_r = qla82xx_md_rw_32(ha, c_addr, 0, 0);
3930 if ((c_value_r & p_mask) == 0)
3931 break;
3932 else if (time_after_eq(jiffies, w_time)) {
3933 /* capturing dump failed */
3934 ql_dbg(ql_dbg_p3p, vha, 0xb032,
3935 "c_value_r: 0x%x, poll_mask: 0x%lx, "
3936 "w_time: 0x%lx\n",
3937 c_value_r, p_mask, w_time);
3938 return rval;
3939 }
3940 } while (1);
3941 }
3942
3943 addr = r_addr;
3944 for (k = 0; k < r_cnt; k++) {
3945 r_value = qla82xx_md_rw_32(ha, addr, 0, 0);
3946 *data_ptr++ = cpu_to_le32(r_value);
3947 addr += cache_hdr->read_ctrl.read_addr_stride;
3948 }
3949 t_value += cache_hdr->addr_ctrl.tag_value_stride;
3950 }
3951 *d_ptr = data_ptr;
3952 return QLA_SUCCESS;
3953}
3954
3955static void
3956qla82xx_minidump_process_l1cache(scsi_qla_host_t *vha,
3957 qla82xx_md_entry_hdr_t *entry_hdr, uint32_t **d_ptr)
3958{
3959 struct qla_hw_data *ha = vha->hw;
3960 uint32_t addr, r_addr, c_addr, t_r_addr;
3961 uint32_t i, k, loop_count, t_value, r_cnt, r_value;
3962 uint32_t c_value_w;
3963 struct qla82xx_md_entry_cache *cache_hdr;
3964 uint32_t *data_ptr = *d_ptr;
3965
3966 cache_hdr = (struct qla82xx_md_entry_cache *)entry_hdr;
3967 loop_count = cache_hdr->op_count;
3968 r_addr = cache_hdr->read_addr;
3969 c_addr = cache_hdr->control_addr;
3970 c_value_w = cache_hdr->cache_ctrl.write_value;
3971
3972 t_r_addr = cache_hdr->tag_reg_addr;
3973 t_value = cache_hdr->addr_ctrl.init_tag_value;
3974 r_cnt = cache_hdr->read_ctrl.read_addr_cnt;
3975
3976 for (i = 0; i < loop_count; i++) {
3977 qla82xx_md_rw_32(ha, t_r_addr, t_value, 1);
3978 qla82xx_md_rw_32(ha, c_addr, c_value_w, 1);
3979 addr = r_addr;
3980 for (k = 0; k < r_cnt; k++) {
3981 r_value = qla82xx_md_rw_32(ha, addr, 0, 0);
3982 *data_ptr++ = cpu_to_le32(r_value);
3983 addr += cache_hdr->read_ctrl.read_addr_stride;
3984 }
3985 t_value += cache_hdr->addr_ctrl.tag_value_stride;
3986 }
3987 *d_ptr = data_ptr;
3988}
3989
3990static void
3991qla82xx_minidump_process_queue(scsi_qla_host_t *vha,
3992 qla82xx_md_entry_hdr_t *entry_hdr, uint32_t **d_ptr)
3993{
3994 struct qla_hw_data *ha = vha->hw;
3995 uint32_t s_addr, r_addr;
3996 uint32_t r_stride, r_value, r_cnt, qid = 0;
3997 uint32_t i, k, loop_cnt;
3998 struct qla82xx_md_entry_queue *q_hdr;
3999 uint32_t *data_ptr = *d_ptr;
4000
4001 q_hdr = (struct qla82xx_md_entry_queue *)entry_hdr;
4002 s_addr = q_hdr->select_addr;
4003 r_cnt = q_hdr->rd_strd.read_addr_cnt;
4004 r_stride = q_hdr->rd_strd.read_addr_stride;
4005 loop_cnt = q_hdr->op_count;
4006
4007 for (i = 0; i < loop_cnt; i++) {
4008 qla82xx_md_rw_32(ha, s_addr, qid, 1);
4009 r_addr = q_hdr->read_addr;
4010 for (k = 0; k < r_cnt; k++) {
4011 r_value = qla82xx_md_rw_32(ha, r_addr, 0, 0);
4012 *data_ptr++ = cpu_to_le32(r_value);
4013 r_addr += r_stride;
4014 }
4015 qid += q_hdr->q_strd.queue_id_stride;
4016 }
4017 *d_ptr = data_ptr;
4018}
4019
4020static void
4021qla82xx_minidump_process_rdrom(scsi_qla_host_t *vha,
4022 qla82xx_md_entry_hdr_t *entry_hdr, uint32_t **d_ptr)
4023{
4024 struct qla_hw_data *ha = vha->hw;
4025 uint32_t r_addr, r_value;
4026 uint32_t i, loop_cnt;
4027 struct qla82xx_md_entry_rdrom *rom_hdr;
4028 uint32_t *data_ptr = *d_ptr;
4029
4030 rom_hdr = (struct qla82xx_md_entry_rdrom *)entry_hdr;
4031 r_addr = rom_hdr->read_addr;
4032 loop_cnt = rom_hdr->read_data_size/sizeof(uint32_t);
4033
4034 for (i = 0; i < loop_cnt; i++) {
4035 qla82xx_md_rw_32(ha, MD_DIRECT_ROM_WINDOW,
4036 (r_addr & 0xFFFF0000), 1);
4037 r_value = qla82xx_md_rw_32(ha,
4038 MD_DIRECT_ROM_READ_BASE +
4039 (r_addr & 0x0000FFFF), 0, 0);
4040 *data_ptr++ = cpu_to_le32(r_value);
4041 r_addr += sizeof(uint32_t);
4042 }
4043 *d_ptr = data_ptr;
4044}
4045
4046static int
4047qla82xx_minidump_process_rdmem(scsi_qla_host_t *vha,
4048 qla82xx_md_entry_hdr_t *entry_hdr, uint32_t **d_ptr)
4049{
4050 struct qla_hw_data *ha = vha->hw;
4051 uint32_t r_addr, r_value, r_data;
4052 uint32_t i, j, loop_cnt;
4053 struct qla82xx_md_entry_rdmem *m_hdr;
4054 unsigned long flags;
4055 int rval = QLA_FUNCTION_FAILED;
4056 uint32_t *data_ptr = *d_ptr;
4057
4058 m_hdr = (struct qla82xx_md_entry_rdmem *)entry_hdr;
4059 r_addr = m_hdr->read_addr;
4060 loop_cnt = m_hdr->read_data_size/16;
4061
4062 if (r_addr & 0xf) {
4063 ql_log(ql_log_warn, vha, 0xb033,
Masanari Iidad6a03582012-08-22 14:20:58 -04004064 "Read addr 0x%x not 16 bytes aligned\n", r_addr);
Giridhar Malavali08de2842011-08-16 11:31:44 -07004065 return rval;
4066 }
4067
4068 if (m_hdr->read_data_size % 16) {
4069 ql_log(ql_log_warn, vha, 0xb034,
4070 "Read data[0x%x] not multiple of 16 bytes\n",
4071 m_hdr->read_data_size);
4072 return rval;
4073 }
4074
4075 ql_dbg(ql_dbg_p3p, vha, 0xb035,
4076 "[%s]: rdmem_addr: 0x%x, read_data_size: 0x%x, loop_cnt: 0x%x\n",
4077 __func__, r_addr, m_hdr->read_data_size, loop_cnt);
4078
4079 write_lock_irqsave(&ha->hw_lock, flags);
4080 for (i = 0; i < loop_cnt; i++) {
4081 qla82xx_md_rw_32(ha, MD_MIU_TEST_AGT_ADDR_LO, r_addr, 1);
4082 r_value = 0;
4083 qla82xx_md_rw_32(ha, MD_MIU_TEST_AGT_ADDR_HI, r_value, 1);
4084 r_value = MIU_TA_CTL_ENABLE;
4085 qla82xx_md_rw_32(ha, MD_MIU_TEST_AGT_CTRL, r_value, 1);
4086 r_value = MIU_TA_CTL_START | MIU_TA_CTL_ENABLE;
4087 qla82xx_md_rw_32(ha, MD_MIU_TEST_AGT_CTRL, r_value, 1);
4088
4089 for (j = 0; j < MAX_CTL_CHECK; j++) {
4090 r_value = qla82xx_md_rw_32(ha,
4091 MD_MIU_TEST_AGT_CTRL, 0, 0);
4092 if ((r_value & MIU_TA_CTL_BUSY) == 0)
4093 break;
4094 }
4095
4096 if (j >= MAX_CTL_CHECK) {
4097 printk_ratelimited(KERN_ERR
4098 "failed to read through agent\n");
4099 write_unlock_irqrestore(&ha->hw_lock, flags);
4100 return rval;
4101 }
4102
4103 for (j = 0; j < 4; j++) {
4104 r_data = qla82xx_md_rw_32(ha,
4105 MD_MIU_TEST_AGT_RDDATA[j], 0, 0);
4106 *data_ptr++ = cpu_to_le32(r_data);
4107 }
4108 r_addr += 16;
4109 }
4110 write_unlock_irqrestore(&ha->hw_lock, flags);
4111 *d_ptr = data_ptr;
4112 return QLA_SUCCESS;
4113}
4114
Atul Deshmukh7ec0eff2013-08-27 01:37:28 -04004115int
Giridhar Malavali08de2842011-08-16 11:31:44 -07004116qla82xx_validate_template_chksum(scsi_qla_host_t *vha)
4117{
4118 struct qla_hw_data *ha = vha->hw;
4119 uint64_t chksum = 0;
4120 uint32_t *d_ptr = (uint32_t *)ha->md_tmplt_hdr;
4121 int count = ha->md_template_size/sizeof(uint32_t);
4122
4123 while (count-- > 0)
4124 chksum += *d_ptr++;
4125 while (chksum >> 32)
4126 chksum = (chksum & 0xFFFFFFFF) + (chksum >> 32);
4127 return ~chksum;
4128}
4129
4130static void
4131qla82xx_mark_entry_skipped(scsi_qla_host_t *vha,
4132 qla82xx_md_entry_hdr_t *entry_hdr, int index)
4133{
4134 entry_hdr->d_ctrl.driver_flags |= QLA82XX_DBG_SKIPPED_FLAG;
4135 ql_dbg(ql_dbg_p3p, vha, 0xb036,
4136 "Skipping entry[%d]: "
4137 "ETYPE[0x%x]-ELEVEL[0x%x]\n",
4138 index, entry_hdr->entry_type,
4139 entry_hdr->d_ctrl.entry_capture_mask);
4140}
4141
4142int
4143qla82xx_md_collect(scsi_qla_host_t *vha)
4144{
4145 struct qla_hw_data *ha = vha->hw;
4146 int no_entry_hdr = 0;
4147 qla82xx_md_entry_hdr_t *entry_hdr;
4148 struct qla82xx_md_template_hdr *tmplt_hdr;
4149 uint32_t *data_ptr;
4150 uint32_t total_data_size = 0, f_capture_mask, data_collected = 0;
4151 int i = 0, rval = QLA_FUNCTION_FAILED;
4152
4153 tmplt_hdr = (struct qla82xx_md_template_hdr *)ha->md_tmplt_hdr;
4154 data_ptr = (uint32_t *)ha->md_dump;
4155
4156 if (ha->fw_dumped) {
Giridhar Malavalia8faa262012-02-09 11:15:52 -08004157 ql_log(ql_log_warn, vha, 0xb037,
4158 "Firmware has been previously dumped (%p) "
4159 "-- ignoring request.\n", ha->fw_dump);
Giridhar Malavali08de2842011-08-16 11:31:44 -07004160 goto md_failed;
4161 }
4162
4163 ha->fw_dumped = 0;
4164
4165 if (!ha->md_tmplt_hdr || !ha->md_dump) {
4166 ql_log(ql_log_warn, vha, 0xb038,
4167 "Memory not allocated for minidump capture\n");
4168 goto md_failed;
4169 }
4170
Giridhar Malavalib6d0d9d2012-05-15 14:34:25 -04004171 if (ha->flags.isp82xx_no_md_cap) {
4172 ql_log(ql_log_warn, vha, 0xb054,
4173 "Forced reset from application, "
4174 "ignore minidump capture\n");
4175 ha->flags.isp82xx_no_md_cap = 0;
4176 goto md_failed;
4177 }
4178
Giridhar Malavali08de2842011-08-16 11:31:44 -07004179 if (qla82xx_validate_template_chksum(vha)) {
4180 ql_log(ql_log_info, vha, 0xb039,
4181 "Template checksum validation error\n");
4182 goto md_failed;
4183 }
4184
4185 no_entry_hdr = tmplt_hdr->num_of_entries;
4186 ql_dbg(ql_dbg_p3p, vha, 0xb03a,
4187 "No of entry headers in Template: 0x%x\n", no_entry_hdr);
4188
4189 ql_dbg(ql_dbg_p3p, vha, 0xb03b,
4190 "Capture Mask obtained: 0x%x\n", tmplt_hdr->capture_debug_level);
4191
4192 f_capture_mask = tmplt_hdr->capture_debug_level & 0xFF;
4193
4194 /* Validate whether required debug level is set */
4195 if ((f_capture_mask & 0x3) != 0x3) {
4196 ql_log(ql_log_warn, vha, 0xb03c,
4197 "Minimum required capture mask[0x%x] level not set\n",
4198 f_capture_mask);
4199 goto md_failed;
4200 }
4201 tmplt_hdr->driver_capture_mask = ql2xmdcapmask;
4202
4203 tmplt_hdr->driver_info[0] = vha->host_no;
4204 tmplt_hdr->driver_info[1] = (QLA_DRIVER_MAJOR_VER << 24) |
4205 (QLA_DRIVER_MINOR_VER << 16) | (QLA_DRIVER_PATCH_VER << 8) |
4206 QLA_DRIVER_BETA_VER;
4207
4208 total_data_size = ha->md_dump_size;
4209
Arun Easi880fded2012-02-09 11:15:49 -08004210 ql_dbg(ql_dbg_p3p, vha, 0xb03d,
Giridhar Malavali08de2842011-08-16 11:31:44 -07004211 "Total minidump data_size 0x%x to be captured\n", total_data_size);
4212
4213 /* Check whether template obtained is valid */
4214 if (tmplt_hdr->entry_type != QLA82XX_TLHDR) {
4215 ql_log(ql_log_warn, vha, 0xb04e,
4216 "Bad template header entry type: 0x%x obtained\n",
4217 tmplt_hdr->entry_type);
4218 goto md_failed;
4219 }
4220
4221 entry_hdr = (qla82xx_md_entry_hdr_t *) \
4222 (((uint8_t *)ha->md_tmplt_hdr) + tmplt_hdr->first_entry_offset);
4223
4224 /* Walk through the entry headers */
4225 for (i = 0; i < no_entry_hdr; i++) {
4226
4227 if (data_collected > total_data_size) {
4228 ql_log(ql_log_warn, vha, 0xb03e,
4229 "More MiniDump data collected: [0x%x]\n",
4230 data_collected);
4231 goto md_failed;
4232 }
4233
4234 if (!(entry_hdr->d_ctrl.entry_capture_mask &
4235 ql2xmdcapmask)) {
4236 entry_hdr->d_ctrl.driver_flags |=
4237 QLA82XX_DBG_SKIPPED_FLAG;
4238 ql_dbg(ql_dbg_p3p, vha, 0xb03f,
4239 "Skipping entry[%d]: "
4240 "ETYPE[0x%x]-ELEVEL[0x%x]\n",
4241 i, entry_hdr->entry_type,
4242 entry_hdr->d_ctrl.entry_capture_mask);
4243 goto skip_nxt_entry;
4244 }
4245
4246 ql_dbg(ql_dbg_p3p, vha, 0xb040,
4247 "[%s]: data ptr[%d]: %p, entry_hdr: %p\n"
4248 "entry_type: 0x%x, captrue_mask: 0x%x\n",
4249 __func__, i, data_ptr, entry_hdr,
4250 entry_hdr->entry_type,
4251 entry_hdr->d_ctrl.entry_capture_mask);
4252
4253 ql_dbg(ql_dbg_p3p, vha, 0xb041,
4254 "Data collected: [0x%x], Dump size left:[0x%x]\n",
4255 data_collected, (ha->md_dump_size - data_collected));
4256
4257 /* Decode the entry type and take
4258 * required action to capture debug data */
4259 switch (entry_hdr->entry_type) {
4260 case QLA82XX_RDEND:
4261 qla82xx_mark_entry_skipped(vha, entry_hdr, i);
4262 break;
4263 case QLA82XX_CNTRL:
4264 rval = qla82xx_minidump_process_control(vha,
4265 entry_hdr, &data_ptr);
4266 if (rval != QLA_SUCCESS) {
4267 qla82xx_mark_entry_skipped(vha, entry_hdr, i);
4268 goto md_failed;
4269 }
4270 break;
4271 case QLA82XX_RDCRB:
4272 qla82xx_minidump_process_rdcrb(vha,
4273 entry_hdr, &data_ptr);
4274 break;
4275 case QLA82XX_RDMEM:
4276 rval = qla82xx_minidump_process_rdmem(vha,
4277 entry_hdr, &data_ptr);
4278 if (rval != QLA_SUCCESS) {
4279 qla82xx_mark_entry_skipped(vha, entry_hdr, i);
4280 goto md_failed;
4281 }
4282 break;
4283 case QLA82XX_BOARD:
4284 case QLA82XX_RDROM:
4285 qla82xx_minidump_process_rdrom(vha,
4286 entry_hdr, &data_ptr);
4287 break;
4288 case QLA82XX_L2DTG:
4289 case QLA82XX_L2ITG:
4290 case QLA82XX_L2DAT:
4291 case QLA82XX_L2INS:
4292 rval = qla82xx_minidump_process_l2tag(vha,
4293 entry_hdr, &data_ptr);
4294 if (rval != QLA_SUCCESS) {
4295 qla82xx_mark_entry_skipped(vha, entry_hdr, i);
4296 goto md_failed;
4297 }
4298 break;
4299 case QLA82XX_L1DAT:
4300 case QLA82XX_L1INS:
4301 qla82xx_minidump_process_l1cache(vha,
4302 entry_hdr, &data_ptr);
4303 break;
4304 case QLA82XX_RDOCM:
4305 qla82xx_minidump_process_rdocm(vha,
4306 entry_hdr, &data_ptr);
4307 break;
4308 case QLA82XX_RDMUX:
4309 qla82xx_minidump_process_rdmux(vha,
4310 entry_hdr, &data_ptr);
4311 break;
4312 case QLA82XX_QUEUE:
4313 qla82xx_minidump_process_queue(vha,
4314 entry_hdr, &data_ptr);
4315 break;
4316 case QLA82XX_RDNOP:
4317 default:
4318 qla82xx_mark_entry_skipped(vha, entry_hdr, i);
4319 break;
4320 }
4321
4322 ql_dbg(ql_dbg_p3p, vha, 0xb042,
4323 "[%s]: data ptr[%d]: %p\n", __func__, i, data_ptr);
4324
4325 data_collected = (uint8_t *)data_ptr -
4326 (uint8_t *)ha->md_dump;
4327skip_nxt_entry:
4328 entry_hdr = (qla82xx_md_entry_hdr_t *) \
4329 (((uint8_t *)entry_hdr) + entry_hdr->entry_size);
4330 }
4331
4332 if (data_collected != total_data_size) {
Arun Easi880fded2012-02-09 11:15:49 -08004333 ql_dbg(ql_dbg_p3p, vha, 0xb043,
Giridhar Malavali08de2842011-08-16 11:31:44 -07004334 "MiniDump data mismatch: Data collected: [0x%x],"
4335 "total_data_size:[0x%x]\n",
4336 data_collected, total_data_size);
4337 goto md_failed;
4338 }
4339
4340 ql_log(ql_log_info, vha, 0xb044,
4341 "Firmware dump saved to temp buffer (%ld/%p %ld/%p).\n",
4342 vha->host_no, ha->md_tmplt_hdr, vha->host_no, ha->md_dump);
4343 ha->fw_dumped = 1;
4344 qla2x00_post_uevent_work(vha, QLA_UEVENT_CODE_FW_DUMP);
4345
4346md_failed:
4347 return rval;
4348}
4349
4350int
4351qla82xx_md_alloc(scsi_qla_host_t *vha)
4352{
4353 struct qla_hw_data *ha = vha->hw;
4354 int i, k;
4355 struct qla82xx_md_template_hdr *tmplt_hdr;
4356
4357 tmplt_hdr = (struct qla82xx_md_template_hdr *)ha->md_tmplt_hdr;
4358
4359 if (ql2xmdcapmask < 0x3 || ql2xmdcapmask > 0x7F) {
4360 ql2xmdcapmask = tmplt_hdr->capture_debug_level & 0xFF;
4361 ql_log(ql_log_info, vha, 0xb045,
4362 "Forcing driver capture mask to firmware default capture mask: 0x%x.\n",
4363 ql2xmdcapmask);
4364 }
4365
4366 for (i = 0x2, k = 1; (i & QLA82XX_DEFAULT_CAP_MASK); i <<= 1, k++) {
4367 if (i & ql2xmdcapmask)
4368 ha->md_dump_size += tmplt_hdr->capture_size_array[k];
4369 }
4370
4371 if (ha->md_dump) {
4372 ql_log(ql_log_warn, vha, 0xb046,
4373 "Firmware dump previously allocated.\n");
4374 return 1;
4375 }
4376
4377 ha->md_dump = vmalloc(ha->md_dump_size);
4378 if (ha->md_dump == NULL) {
4379 ql_log(ql_log_warn, vha, 0xb047,
4380 "Unable to allocate memory for Minidump size "
4381 "(0x%x).\n", ha->md_dump_size);
4382 return 1;
4383 }
4384 return 0;
4385}
4386
4387void
4388qla82xx_md_free(scsi_qla_host_t *vha)
4389{
4390 struct qla_hw_data *ha = vha->hw;
4391
4392 /* Release the template header allocated */
4393 if (ha->md_tmplt_hdr) {
4394 ql_log(ql_log_info, vha, 0xb048,
4395 "Free MiniDump template: %p, size (%d KB)\n",
4396 ha->md_tmplt_hdr, ha->md_template_size / 1024);
4397 dma_free_coherent(&ha->pdev->dev, ha->md_template_size,
4398 ha->md_tmplt_hdr, ha->md_tmplt_hdr_dma);
Saurav Kashyapfa492632012-11-21 02:40:29 -05004399 ha->md_tmplt_hdr = NULL;
Giridhar Malavali08de2842011-08-16 11:31:44 -07004400 }
4401
4402 /* Release the template data buffer allocated */
4403 if (ha->md_dump) {
4404 ql_log(ql_log_info, vha, 0xb049,
4405 "Free MiniDump memory: %p, size (%d KB)\n",
4406 ha->md_dump, ha->md_dump_size / 1024);
4407 vfree(ha->md_dump);
4408 ha->md_dump_size = 0;
Saurav Kashyapfa492632012-11-21 02:40:29 -05004409 ha->md_dump = NULL;
Giridhar Malavali08de2842011-08-16 11:31:44 -07004410 }
4411}
4412
4413void
4414qla82xx_md_prep(scsi_qla_host_t *vha)
4415{
4416 struct qla_hw_data *ha = vha->hw;
4417 int rval;
4418
4419 /* Get Minidump template size */
4420 rval = qla82xx_md_get_template_size(vha);
4421 if (rval == QLA_SUCCESS) {
4422 ql_log(ql_log_info, vha, 0xb04a,
4423 "MiniDump Template size obtained (%d KB)\n",
4424 ha->md_template_size / 1024);
4425
4426 /* Get Minidump template */
Atul Deshmukh7ec0eff2013-08-27 01:37:28 -04004427 if (IS_QLA8044(ha))
4428 rval = qla8044_md_get_template(vha);
4429 else
4430 rval = qla82xx_md_get_template(vha);
4431
Giridhar Malavali08de2842011-08-16 11:31:44 -07004432 if (rval == QLA_SUCCESS) {
4433 ql_dbg(ql_dbg_p3p, vha, 0xb04b,
4434 "MiniDump Template obtained\n");
4435
4436 /* Allocate memory for minidump */
4437 rval = qla82xx_md_alloc(vha);
4438 if (rval == QLA_SUCCESS)
4439 ql_log(ql_log_info, vha, 0xb04c,
4440 "MiniDump memory allocated (%d KB)\n",
4441 ha->md_dump_size / 1024);
4442 else {
4443 ql_log(ql_log_info, vha, 0xb04d,
4444 "Free MiniDump template: %p, size: (%d KB)\n",
4445 ha->md_tmplt_hdr,
4446 ha->md_template_size / 1024);
4447 dma_free_coherent(&ha->pdev->dev,
4448 ha->md_template_size,
4449 ha->md_tmplt_hdr, ha->md_tmplt_hdr_dma);
Saurav Kashyapfa492632012-11-21 02:40:29 -05004450 ha->md_tmplt_hdr = NULL;
Giridhar Malavali08de2842011-08-16 11:31:44 -07004451 }
4452
4453 }
4454 }
4455}
Saurav Kashyap999916d2011-08-16 11:31:45 -07004456
4457int
4458qla82xx_beacon_on(struct scsi_qla_host *vha)
4459{
4460
4461 int rval;
4462 struct qla_hw_data *ha = vha->hw;
4463 qla82xx_idc_lock(ha);
4464 rval = qla82xx_mbx_beacon_ctl(vha, 1);
4465
4466 if (rval) {
4467 ql_log(ql_log_warn, vha, 0xb050,
4468 "mbx set led config failed in %s\n", __func__);
4469 goto exit;
4470 }
4471 ha->beacon_blink_led = 1;
4472exit:
4473 qla82xx_idc_unlock(ha);
4474 return rval;
4475}
4476
4477int
4478qla82xx_beacon_off(struct scsi_qla_host *vha)
4479{
4480
4481 int rval;
4482 struct qla_hw_data *ha = vha->hw;
4483 qla82xx_idc_lock(ha);
4484 rval = qla82xx_mbx_beacon_ctl(vha, 0);
4485
4486 if (rval) {
4487 ql_log(ql_log_warn, vha, 0xb051,
4488 "mbx set led config failed in %s\n", __func__);
4489 goto exit;
4490 }
4491 ha->beacon_blink_led = 0;
4492exit:
4493 qla82xx_idc_unlock(ha);
4494 return rval;
4495}
Chad Dupuisa1b23c52014-02-26 04:15:12 -05004496
4497void
4498qla82xx_fw_dump(scsi_qla_host_t *vha, int hardware_locked)
4499{
4500 struct qla_hw_data *ha = vha->hw;
4501
4502 if (!ha->allow_cna_fw_dump)
4503 return;
4504
4505 scsi_block_requests(vha->host);
4506 ha->flags.isp82xx_no_md_cap = 1;
4507 qla82xx_idc_lock(ha);
4508 qla82xx_set_reset_owner(vha);
4509 qla82xx_idc_unlock(ha);
4510 qla2x00_wait_for_chip_reset(vha);
4511 scsi_unblock_requests(vha->host);
4512}