blob: 5b24af86aaf06cfedb0610c9294a0e0d6aa86adf [file] [log] [blame]
Andrew Vasquezfa90c542005-10-27 11:10:08 -07001/*
2 * QLogic Fibre Channel HBA Driver
Andrew Vasquez01e58d82008-04-03 13:13:13 -07003 * Copyright (c) 2003-2008 QLogic Corporation
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
Andrew Vasquezfa90c542005-10-27 11:10:08 -07005 * See LICENSE.qla2xxx for copyright and licensing details.
6 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007#include "qla_def.h"
8
9#include <linux/delay.h>
Andrew Vasquez2c96d8d2007-10-19 15:59:15 -070010#include <linux/vmalloc.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070011#include <asm/uaccess.h>
12
Linus Torvalds1da177e2005-04-16 15:20:36 -070013/*
14 * NVRAM support routines
15 */
16
17/**
Andrew Vasquezfa2a1ce2005-07-06 10:32:07 -070018 * qla2x00_lock_nvram_access() -
Linus Torvalds1da177e2005-04-16 15:20:36 -070019 * @ha: HA context
20 */
Adrian Bunka824ebb2008-01-17 09:02:15 -080021static void
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -080022qla2x00_lock_nvram_access(struct qla_hw_data *ha)
Linus Torvalds1da177e2005-04-16 15:20:36 -070023{
24 uint16_t data;
Andrew Vasquez3d716442005-07-06 10:30:26 -070025 struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
Linus Torvalds1da177e2005-04-16 15:20:36 -070026
27 if (!IS_QLA2100(ha) && !IS_QLA2200(ha) && !IS_QLA2300(ha)) {
28 data = RD_REG_WORD(&reg->nvram);
29 while (data & NVR_BUSY) {
30 udelay(100);
31 data = RD_REG_WORD(&reg->nvram);
32 }
33
34 /* Lock resource */
35 WRT_REG_WORD(&reg->u.isp2300.host_semaphore, 0x1);
36 RD_REG_WORD(&reg->u.isp2300.host_semaphore);
37 udelay(5);
38 data = RD_REG_WORD(&reg->u.isp2300.host_semaphore);
39 while ((data & BIT_0) == 0) {
40 /* Lock failed */
41 udelay(100);
42 WRT_REG_WORD(&reg->u.isp2300.host_semaphore, 0x1);
43 RD_REG_WORD(&reg->u.isp2300.host_semaphore);
44 udelay(5);
45 data = RD_REG_WORD(&reg->u.isp2300.host_semaphore);
46 }
47 }
48}
49
50/**
Andrew Vasquezfa2a1ce2005-07-06 10:32:07 -070051 * qla2x00_unlock_nvram_access() -
Linus Torvalds1da177e2005-04-16 15:20:36 -070052 * @ha: HA context
53 */
Adrian Bunka824ebb2008-01-17 09:02:15 -080054static void
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -080055qla2x00_unlock_nvram_access(struct qla_hw_data *ha)
Linus Torvalds1da177e2005-04-16 15:20:36 -070056{
Andrew Vasquez3d716442005-07-06 10:30:26 -070057 struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
Linus Torvalds1da177e2005-04-16 15:20:36 -070058
59 if (!IS_QLA2100(ha) && !IS_QLA2200(ha) && !IS_QLA2300(ha)) {
60 WRT_REG_WORD(&reg->u.isp2300.host_semaphore, 0);
61 RD_REG_WORD(&reg->u.isp2300.host_semaphore);
62 }
63}
64
65/**
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -080066 * qla2x00_nv_write() - Prepare for NVRAM read/write operation.
67 * @ha: HA context
68 * @data: Serial interface selector
69 */
70static void
71qla2x00_nv_write(struct qla_hw_data *ha, uint16_t data)
72{
73 struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
74
75 WRT_REG_WORD(&reg->nvram, data | NVR_SELECT | NVR_WRT_ENABLE);
76 RD_REG_WORD(&reg->nvram); /* PCI Posting. */
77 NVRAM_DELAY();
78 WRT_REG_WORD(&reg->nvram, data | NVR_SELECT | NVR_CLOCK |
79 NVR_WRT_ENABLE);
80 RD_REG_WORD(&reg->nvram); /* PCI Posting. */
81 NVRAM_DELAY();
82 WRT_REG_WORD(&reg->nvram, data | NVR_SELECT | NVR_WRT_ENABLE);
83 RD_REG_WORD(&reg->nvram); /* PCI Posting. */
84 NVRAM_DELAY();
85}
86
87/**
88 * qla2x00_nvram_request() - Sends read command to NVRAM and gets data from
89 * NVRAM.
90 * @ha: HA context
91 * @nv_cmd: NVRAM command
92 *
93 * Bit definitions for NVRAM command:
94 *
95 * Bit 26 = start bit
96 * Bit 25, 24 = opcode
97 * Bit 23-16 = address
98 * Bit 15-0 = write data
99 *
100 * Returns the word read from nvram @addr.
101 */
102static uint16_t
103qla2x00_nvram_request(struct qla_hw_data *ha, uint32_t nv_cmd)
104{
105 uint8_t cnt;
106 struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
107 uint16_t data = 0;
108 uint16_t reg_data;
109
110 /* Send command to NVRAM. */
111 nv_cmd <<= 5;
112 for (cnt = 0; cnt < 11; cnt++) {
113 if (nv_cmd & BIT_31)
114 qla2x00_nv_write(ha, NVR_DATA_OUT);
115 else
116 qla2x00_nv_write(ha, 0);
117 nv_cmd <<= 1;
118 }
119
120 /* Read data from NVRAM. */
121 for (cnt = 0; cnt < 16; cnt++) {
122 WRT_REG_WORD(&reg->nvram, NVR_SELECT | NVR_CLOCK);
123 RD_REG_WORD(&reg->nvram); /* PCI Posting. */
124 NVRAM_DELAY();
125 data <<= 1;
126 reg_data = RD_REG_WORD(&reg->nvram);
127 if (reg_data & NVR_DATA_IN)
128 data |= BIT_0;
129 WRT_REG_WORD(&reg->nvram, NVR_SELECT);
130 RD_REG_WORD(&reg->nvram); /* PCI Posting. */
131 NVRAM_DELAY();
132 }
133
134 /* Deselect chip. */
135 WRT_REG_WORD(&reg->nvram, NVR_DESELECT);
136 RD_REG_WORD(&reg->nvram); /* PCI Posting. */
137 NVRAM_DELAY();
138
139 return data;
140}
141
142
143/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 * qla2x00_get_nvram_word() - Calculates word position in NVRAM and calls the
145 * request routine to get the word from NVRAM.
146 * @ha: HA context
147 * @addr: Address in NVRAM to read
148 *
149 * Returns the word read from nvram @addr.
150 */
Adrian Bunka824ebb2008-01-17 09:02:15 -0800151static uint16_t
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -0800152qla2x00_get_nvram_word(struct qla_hw_data *ha, uint32_t addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153{
154 uint16_t data;
155 uint32_t nv_cmd;
156
157 nv_cmd = addr << 16;
158 nv_cmd |= NV_READ_OP;
159 data = qla2x00_nvram_request(ha, nv_cmd);
160
161 return (data);
162}
163
164/**
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -0800165 * qla2x00_nv_deselect() - Deselect NVRAM operations.
166 * @ha: HA context
167 */
168static void
169qla2x00_nv_deselect(struct qla_hw_data *ha)
170{
171 struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
172
173 WRT_REG_WORD(&reg->nvram, NVR_DESELECT);
174 RD_REG_WORD(&reg->nvram); /* PCI Posting. */
175 NVRAM_DELAY();
176}
177
178/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 * qla2x00_write_nvram_word() - Write NVRAM data.
180 * @ha: HA context
181 * @addr: Address in NVRAM to write
182 * @data: word to program
183 */
Adrian Bunka824ebb2008-01-17 09:02:15 -0800184static void
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -0800185qla2x00_write_nvram_word(struct qla_hw_data *ha, uint32_t addr, uint16_t data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186{
187 int count;
188 uint16_t word;
Ravi Anand45aeaf12006-05-17 15:08:49 -0700189 uint32_t nv_cmd, wait_cnt;
Andrew Vasquez3d716442005-07-06 10:30:26 -0700190 struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191
192 qla2x00_nv_write(ha, NVR_DATA_OUT);
193 qla2x00_nv_write(ha, 0);
194 qla2x00_nv_write(ha, 0);
195
196 for (word = 0; word < 8; word++)
197 qla2x00_nv_write(ha, NVR_DATA_OUT);
198
199 qla2x00_nv_deselect(ha);
200
201 /* Write data */
202 nv_cmd = (addr << 16) | NV_WRITE_OP;
203 nv_cmd |= data;
204 nv_cmd <<= 5;
205 for (count = 0; count < 27; count++) {
206 if (nv_cmd & BIT_31)
207 qla2x00_nv_write(ha, NVR_DATA_OUT);
208 else
209 qla2x00_nv_write(ha, 0);
210
211 nv_cmd <<= 1;
212 }
213
214 qla2x00_nv_deselect(ha);
215
216 /* Wait for NVRAM to become ready */
217 WRT_REG_WORD(&reg->nvram, NVR_SELECT);
Andrew Vasquezdcb36ce2005-11-08 14:37:06 -0800218 RD_REG_WORD(&reg->nvram); /* PCI Posting. */
Ravi Anand45aeaf12006-05-17 15:08:49 -0700219 wait_cnt = NVR_WAIT_CNT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 do {
Ravi Anand45aeaf12006-05-17 15:08:49 -0700221 if (!--wait_cnt) {
222 DEBUG9_10(printk("%s(%ld): NVRAM didn't go ready...\n",
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -0800223 __func__, vha->host_no));
Ravi Anand45aeaf12006-05-17 15:08:49 -0700224 break;
225 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 NVRAM_DELAY();
227 word = RD_REG_WORD(&reg->nvram);
228 } while ((word & NVR_DATA_IN) == 0);
229
230 qla2x00_nv_deselect(ha);
231
232 /* Disable writes */
233 qla2x00_nv_write(ha, NVR_DATA_OUT);
234 for (count = 0; count < 10; count++)
235 qla2x00_nv_write(ha, 0);
236
237 qla2x00_nv_deselect(ha);
238}
239
Andrew Vasquez459c5372005-07-06 10:31:07 -0700240static int
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -0800241qla2x00_write_nvram_word_tmo(struct qla_hw_data *ha, uint32_t addr,
242 uint16_t data, uint32_t tmo)
Andrew Vasquez459c5372005-07-06 10:31:07 -0700243{
244 int ret, count;
245 uint16_t word;
246 uint32_t nv_cmd;
247 struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
248
249 ret = QLA_SUCCESS;
250
251 qla2x00_nv_write(ha, NVR_DATA_OUT);
252 qla2x00_nv_write(ha, 0);
253 qla2x00_nv_write(ha, 0);
254
255 for (word = 0; word < 8; word++)
256 qla2x00_nv_write(ha, NVR_DATA_OUT);
257
258 qla2x00_nv_deselect(ha);
259
260 /* Write data */
261 nv_cmd = (addr << 16) | NV_WRITE_OP;
262 nv_cmd |= data;
263 nv_cmd <<= 5;
264 for (count = 0; count < 27; count++) {
265 if (nv_cmd & BIT_31)
266 qla2x00_nv_write(ha, NVR_DATA_OUT);
267 else
268 qla2x00_nv_write(ha, 0);
269
270 nv_cmd <<= 1;
271 }
272
273 qla2x00_nv_deselect(ha);
274
275 /* Wait for NVRAM to become ready */
276 WRT_REG_WORD(&reg->nvram, NVR_SELECT);
Andrew Vasquezdcb36ce2005-11-08 14:37:06 -0800277 RD_REG_WORD(&reg->nvram); /* PCI Posting. */
Andrew Vasquez459c5372005-07-06 10:31:07 -0700278 do {
279 NVRAM_DELAY();
280 word = RD_REG_WORD(&reg->nvram);
281 if (!--tmo) {
282 ret = QLA_FUNCTION_FAILED;
283 break;
284 }
285 } while ((word & NVR_DATA_IN) == 0);
286
287 qla2x00_nv_deselect(ha);
288
289 /* Disable writes */
290 qla2x00_nv_write(ha, NVR_DATA_OUT);
291 for (count = 0; count < 10; count++)
292 qla2x00_nv_write(ha, 0);
293
294 qla2x00_nv_deselect(ha);
295
296 return ret;
297}
298
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299/**
Andrew Vasquez459c5372005-07-06 10:31:07 -0700300 * qla2x00_clear_nvram_protection() -
301 * @ha: HA context
302 */
303static int
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -0800304qla2x00_clear_nvram_protection(struct qla_hw_data *ha)
Andrew Vasquez459c5372005-07-06 10:31:07 -0700305{
306 int ret, stat;
307 struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
Ravi Anand45aeaf12006-05-17 15:08:49 -0700308 uint32_t word, wait_cnt;
Andrew Vasquez459c5372005-07-06 10:31:07 -0700309 uint16_t wprot, wprot_old;
310
311 /* Clear NVRAM write protection. */
312 ret = QLA_FUNCTION_FAILED;
Ravi Anand45aeaf12006-05-17 15:08:49 -0700313
314 wprot_old = cpu_to_le16(qla2x00_get_nvram_word(ha, ha->nvram_base));
315 stat = qla2x00_write_nvram_word_tmo(ha, ha->nvram_base,
Andrew Vasquez459c5372005-07-06 10:31:07 -0700316 __constant_cpu_to_le16(0x1234), 100000);
Ravi Anand45aeaf12006-05-17 15:08:49 -0700317 wprot = cpu_to_le16(qla2x00_get_nvram_word(ha, ha->nvram_base));
318 if (stat != QLA_SUCCESS || wprot != 0x1234) {
Andrew Vasquez459c5372005-07-06 10:31:07 -0700319 /* Write enable. */
320 qla2x00_nv_write(ha, NVR_DATA_OUT);
321 qla2x00_nv_write(ha, 0);
322 qla2x00_nv_write(ha, 0);
323 for (word = 0; word < 8; word++)
324 qla2x00_nv_write(ha, NVR_DATA_OUT);
325
326 qla2x00_nv_deselect(ha);
327
328 /* Enable protection register. */
329 qla2x00_nv_write(ha, NVR_PR_ENABLE | NVR_DATA_OUT);
330 qla2x00_nv_write(ha, NVR_PR_ENABLE);
331 qla2x00_nv_write(ha, NVR_PR_ENABLE);
332 for (word = 0; word < 8; word++)
333 qla2x00_nv_write(ha, NVR_DATA_OUT | NVR_PR_ENABLE);
334
335 qla2x00_nv_deselect(ha);
336
337 /* Clear protection register (ffff is cleared). */
338 qla2x00_nv_write(ha, NVR_PR_ENABLE | NVR_DATA_OUT);
339 qla2x00_nv_write(ha, NVR_PR_ENABLE | NVR_DATA_OUT);
340 qla2x00_nv_write(ha, NVR_PR_ENABLE | NVR_DATA_OUT);
341 for (word = 0; word < 8; word++)
342 qla2x00_nv_write(ha, NVR_DATA_OUT | NVR_PR_ENABLE);
343
344 qla2x00_nv_deselect(ha);
345
346 /* Wait for NVRAM to become ready. */
347 WRT_REG_WORD(&reg->nvram, NVR_SELECT);
Andrew Vasquezdcb36ce2005-11-08 14:37:06 -0800348 RD_REG_WORD(&reg->nvram); /* PCI Posting. */
Ravi Anand45aeaf12006-05-17 15:08:49 -0700349 wait_cnt = NVR_WAIT_CNT;
Andrew Vasquez459c5372005-07-06 10:31:07 -0700350 do {
Ravi Anand45aeaf12006-05-17 15:08:49 -0700351 if (!--wait_cnt) {
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -0800352 DEBUG9_10(qla_printk(
353 "NVRAM didn't go ready...\n"));
Ravi Anand45aeaf12006-05-17 15:08:49 -0700354 break;
355 }
Andrew Vasquez459c5372005-07-06 10:31:07 -0700356 NVRAM_DELAY();
357 word = RD_REG_WORD(&reg->nvram);
358 } while ((word & NVR_DATA_IN) == 0);
359
Ravi Anand45aeaf12006-05-17 15:08:49 -0700360 if (wait_cnt)
361 ret = QLA_SUCCESS;
Andrew Vasquez459c5372005-07-06 10:31:07 -0700362 } else
Ravi Anand45aeaf12006-05-17 15:08:49 -0700363 qla2x00_write_nvram_word(ha, ha->nvram_base, wprot_old);
Andrew Vasquez459c5372005-07-06 10:31:07 -0700364
365 return ret;
366}
367
368static void
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -0800369qla2x00_set_nvram_protection(struct qla_hw_data *ha, int stat)
Andrew Vasquez459c5372005-07-06 10:31:07 -0700370{
371 struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
Ravi Anand45aeaf12006-05-17 15:08:49 -0700372 uint32_t word, wait_cnt;
Andrew Vasquez459c5372005-07-06 10:31:07 -0700373
374 if (stat != QLA_SUCCESS)
375 return;
376
377 /* Set NVRAM write protection. */
378 /* Write enable. */
379 qla2x00_nv_write(ha, NVR_DATA_OUT);
380 qla2x00_nv_write(ha, 0);
381 qla2x00_nv_write(ha, 0);
382 for (word = 0; word < 8; word++)
383 qla2x00_nv_write(ha, NVR_DATA_OUT);
384
385 qla2x00_nv_deselect(ha);
386
387 /* Enable protection register. */
388 qla2x00_nv_write(ha, NVR_PR_ENABLE | NVR_DATA_OUT);
389 qla2x00_nv_write(ha, NVR_PR_ENABLE);
390 qla2x00_nv_write(ha, NVR_PR_ENABLE);
391 for (word = 0; word < 8; word++)
392 qla2x00_nv_write(ha, NVR_DATA_OUT | NVR_PR_ENABLE);
393
394 qla2x00_nv_deselect(ha);
395
396 /* Enable protection register. */
397 qla2x00_nv_write(ha, NVR_PR_ENABLE | NVR_DATA_OUT);
398 qla2x00_nv_write(ha, NVR_PR_ENABLE);
399 qla2x00_nv_write(ha, NVR_PR_ENABLE | NVR_DATA_OUT);
400 for (word = 0; word < 8; word++)
401 qla2x00_nv_write(ha, NVR_PR_ENABLE);
402
403 qla2x00_nv_deselect(ha);
404
405 /* Wait for NVRAM to become ready. */
406 WRT_REG_WORD(&reg->nvram, NVR_SELECT);
Andrew Vasquezdcb36ce2005-11-08 14:37:06 -0800407 RD_REG_WORD(&reg->nvram); /* PCI Posting. */
Ravi Anand45aeaf12006-05-17 15:08:49 -0700408 wait_cnt = NVR_WAIT_CNT;
Andrew Vasquez459c5372005-07-06 10:31:07 -0700409 do {
Ravi Anand45aeaf12006-05-17 15:08:49 -0700410 if (!--wait_cnt) {
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -0800411 DEBUG9_10(qla_printk("NVRAM didn't go ready...\n"));
Ravi Anand45aeaf12006-05-17 15:08:49 -0700412 break;
413 }
Andrew Vasquez459c5372005-07-06 10:31:07 -0700414 NVRAM_DELAY();
415 word = RD_REG_WORD(&reg->nvram);
416 } while ((word & NVR_DATA_IN) == 0);
417}
418
419
420/*****************************************************************************/
421/* Flash Manipulation Routines */
422/*****************************************************************************/
423
Andrew Vasquez338c9162007-09-20 14:07:33 -0700424#define OPTROM_BURST_SIZE 0x1000
425#define OPTROM_BURST_DWORDS (OPTROM_BURST_SIZE / 4)
426
Andrew Vasquez459c5372005-07-06 10:31:07 -0700427static inline uint32_t
Andrew Vasquez3a03eb72009-01-05 11:18:11 -0800428flash_conf_addr(struct qla_hw_data *ha, uint32_t faddr)
Andrew Vasquez459c5372005-07-06 10:31:07 -0700429{
Andrew Vasquez3a03eb72009-01-05 11:18:11 -0800430 return ha->flash_conf_off | faddr;
Andrew Vasquez459c5372005-07-06 10:31:07 -0700431}
432
433static inline uint32_t
Andrew Vasquez3a03eb72009-01-05 11:18:11 -0800434flash_data_addr(struct qla_hw_data *ha, uint32_t faddr)
Andrew Vasquez459c5372005-07-06 10:31:07 -0700435{
Andrew Vasquez3a03eb72009-01-05 11:18:11 -0800436 return ha->flash_data_off | faddr;
Andrew Vasquez459c5372005-07-06 10:31:07 -0700437}
438
439static inline uint32_t
Andrew Vasquez3a03eb72009-01-05 11:18:11 -0800440nvram_conf_addr(struct qla_hw_data *ha, uint32_t naddr)
Andrew Vasquez459c5372005-07-06 10:31:07 -0700441{
Andrew Vasquez3a03eb72009-01-05 11:18:11 -0800442 return ha->nvram_conf_off | naddr;
Andrew Vasquez459c5372005-07-06 10:31:07 -0700443}
444
445static inline uint32_t
Andrew Vasquez3a03eb72009-01-05 11:18:11 -0800446nvram_data_addr(struct qla_hw_data *ha, uint32_t naddr)
Andrew Vasquez459c5372005-07-06 10:31:07 -0700447{
Andrew Vasquez3a03eb72009-01-05 11:18:11 -0800448 return ha->nvram_data_off | naddr;
Andrew Vasquez459c5372005-07-06 10:31:07 -0700449}
450
Adrian Bunke5f82ab2006-11-08 19:55:50 -0800451static uint32_t
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -0800452qla24xx_read_flash_dword(struct qla_hw_data *ha, uint32_t addr)
Andrew Vasquez459c5372005-07-06 10:31:07 -0700453{
454 int rval;
455 uint32_t cnt, data;
456 struct device_reg_24xx __iomem *reg = &ha->iobase->isp24;
457
458 WRT_REG_DWORD(&reg->flash_addr, addr & ~FARX_DATA_FLAG);
459 /* Wait for READ cycle to complete. */
460 rval = QLA_SUCCESS;
461 for (cnt = 3000;
462 (RD_REG_DWORD(&reg->flash_addr) & FARX_DATA_FLAG) == 0 &&
463 rval == QLA_SUCCESS; cnt--) {
464 if (cnt)
465 udelay(10);
466 else
467 rval = QLA_FUNCTION_TIMEOUT;
Andrew Vasquez40a2e342007-03-12 10:41:28 -0700468 cond_resched();
Andrew Vasquez459c5372005-07-06 10:31:07 -0700469 }
470
471 /* TODO: What happens if we time out? */
472 data = 0xDEADDEAD;
473 if (rval == QLA_SUCCESS)
474 data = RD_REG_DWORD(&reg->flash_data);
475
476 return data;
477}
478
479uint32_t *
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -0800480qla24xx_read_flash_data(scsi_qla_host_t *vha, uint32_t *dwptr, uint32_t faddr,
Andrew Vasquez459c5372005-07-06 10:31:07 -0700481 uint32_t dwords)
482{
483 uint32_t i;
Andrew Vasquez3a03eb72009-01-05 11:18:11 -0800484 struct qla_hw_data *ha = vha->hw;
485
Andrew Vasquez459c5372005-07-06 10:31:07 -0700486 /* Dword reads to flash. */
487 for (i = 0; i < dwords; i++, faddr++)
Andrew Vasquez3a03eb72009-01-05 11:18:11 -0800488 dwptr[i] = cpu_to_le32(qla24xx_read_flash_dword(ha,
489 flash_data_addr(ha, faddr)));
Andrew Vasquez459c5372005-07-06 10:31:07 -0700490
Andrew Vasquez459c5372005-07-06 10:31:07 -0700491 return dwptr;
492}
493
Adrian Bunke5f82ab2006-11-08 19:55:50 -0800494static int
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -0800495qla24xx_write_flash_dword(struct qla_hw_data *ha, uint32_t addr, uint32_t data)
Andrew Vasquez459c5372005-07-06 10:31:07 -0700496{
497 int rval;
498 uint32_t cnt;
499 struct device_reg_24xx __iomem *reg = &ha->iobase->isp24;
500
501 WRT_REG_DWORD(&reg->flash_data, data);
502 RD_REG_DWORD(&reg->flash_data); /* PCI Posting. */
503 WRT_REG_DWORD(&reg->flash_addr, addr | FARX_DATA_FLAG);
504 /* Wait for Write cycle to complete. */
505 rval = QLA_SUCCESS;
506 for (cnt = 500000; (RD_REG_DWORD(&reg->flash_addr) & FARX_DATA_FLAG) &&
507 rval == QLA_SUCCESS; cnt--) {
508 if (cnt)
509 udelay(10);
510 else
511 rval = QLA_FUNCTION_TIMEOUT;
Andrew Vasquez40a2e342007-03-12 10:41:28 -0700512 cond_resched();
Andrew Vasquez459c5372005-07-06 10:31:07 -0700513 }
514 return rval;
515}
516
Adrian Bunke5f82ab2006-11-08 19:55:50 -0800517static void
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -0800518qla24xx_get_flash_manufacturer(struct qla_hw_data *ha, uint8_t *man_id,
Andrew Vasquez459c5372005-07-06 10:31:07 -0700519 uint8_t *flash_id)
520{
521 uint32_t ids;
522
Andrew Vasquez3a03eb72009-01-05 11:18:11 -0800523 ids = qla24xx_read_flash_dword(ha, flash_conf_addr(ha, 0x03ab));
Andrew Vasquez459c5372005-07-06 10:31:07 -0700524 *man_id = LSB(ids);
525 *flash_id = MSB(ids);
Ravi Anand45aeaf12006-05-17 15:08:49 -0700526
527 /* Check if man_id and flash_id are valid. */
528 if (ids != 0xDEADDEAD && (*man_id == 0 || *flash_id == 0)) {
529 /* Read information using 0x9f opcode
530 * Device ID, Mfg ID would be read in the format:
531 * <Ext Dev Info><Device ID Part2><Device ID Part 1><Mfg ID>
532 * Example: ATMEL 0x00 01 45 1F
533 * Extract MFG and Dev ID from last two bytes.
534 */
Andrew Vasquez3a03eb72009-01-05 11:18:11 -0800535 ids = qla24xx_read_flash_dword(ha, flash_conf_addr(ha, 0x009f));
Ravi Anand45aeaf12006-05-17 15:08:49 -0700536 *man_id = LSB(ids);
537 *flash_id = MSB(ids);
538 }
Andrew Vasquez459c5372005-07-06 10:31:07 -0700539}
540
Andrew Vasquezc00d8992008-09-11 21:22:49 -0700541static int
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -0800542qla2xxx_find_flt_start(scsi_qla_host_t *vha, uint32_t *start)
Andrew Vasquezc00d8992008-09-11 21:22:49 -0700543{
544 const char *loc, *locations[] = { "DEF", "PCI" };
545 uint32_t pcihdr, pcids;
546 uint32_t *dcode;
547 uint8_t *buf, *bcode, last_image;
548 uint16_t cnt, chksum, *wptr;
549 struct qla_flt_location *fltl;
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -0800550 struct qla_hw_data *ha = vha->hw;
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800551 struct req_que *req = ha->req_q_map[0];
Andrew Vasquezc00d8992008-09-11 21:22:49 -0700552
553 /*
554 * FLT-location structure resides after the last PCI region.
555 */
556
557 /* Begin with sane defaults. */
558 loc = locations[0];
Andrew Vasquez3a03eb72009-01-05 11:18:11 -0800559 *start = 0;
560 if (IS_QLA24XX_TYPE(ha))
561 *start = FA_FLASH_LAYOUT_ADDR_24;
562 else if (IS_QLA25XX(ha))
563 *start = FA_FLASH_LAYOUT_ADDR;
564 else if (IS_QLA81XX(ha))
565 *start = FA_FLASH_LAYOUT_ADDR_81;
Andrew Vasquezc00d8992008-09-11 21:22:49 -0700566 /* Begin with first PCI expansion ROM header. */
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -0800567 buf = (uint8_t *)req->ring;
568 dcode = (uint32_t *)req->ring;
Andrew Vasquezc00d8992008-09-11 21:22:49 -0700569 pcihdr = 0;
570 last_image = 1;
571 do {
572 /* Verify PCI expansion ROM header. */
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -0800573 qla24xx_read_flash_data(vha, dcode, pcihdr >> 2, 0x20);
Andrew Vasquezc00d8992008-09-11 21:22:49 -0700574 bcode = buf + (pcihdr % 4);
575 if (bcode[0x0] != 0x55 || bcode[0x1] != 0xaa)
576 goto end;
577
578 /* Locate PCI data structure. */
579 pcids = pcihdr + ((bcode[0x19] << 8) | bcode[0x18]);
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -0800580 qla24xx_read_flash_data(vha, dcode, pcids >> 2, 0x20);
Andrew Vasquezc00d8992008-09-11 21:22:49 -0700581 bcode = buf + (pcihdr % 4);
582
583 /* Validate signature of PCI data structure. */
584 if (bcode[0x0] != 'P' || bcode[0x1] != 'C' ||
585 bcode[0x2] != 'I' || bcode[0x3] != 'R')
586 goto end;
587
588 last_image = bcode[0x15] & BIT_7;
589
590 /* Locate next PCI expansion ROM. */
591 pcihdr += ((bcode[0x11] << 8) | bcode[0x10]) * 512;
592 } while (!last_image);
593
594 /* Now verify FLT-location structure. */
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -0800595 fltl = (struct qla_flt_location *)req->ring;
596 qla24xx_read_flash_data(vha, dcode, pcihdr >> 2,
Andrew Vasquezc00d8992008-09-11 21:22:49 -0700597 sizeof(struct qla_flt_location) >> 2);
598 if (fltl->sig[0] != 'Q' || fltl->sig[1] != 'F' ||
599 fltl->sig[2] != 'L' || fltl->sig[3] != 'T')
600 goto end;
601
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -0800602 wptr = (uint16_t *)req->ring;
Andrew Vasquezc00d8992008-09-11 21:22:49 -0700603 cnt = sizeof(struct qla_flt_location) >> 1;
604 for (chksum = 0; cnt; cnt--)
605 chksum += le16_to_cpu(*wptr++);
606 if (chksum) {
607 qla_printk(KERN_ERR, ha,
608 "Inconsistent FLTL detected: checksum=0x%x.\n", chksum);
609 qla2x00_dump_buffer(buf, sizeof(struct qla_flt_location));
610 return QLA_FUNCTION_FAILED;
611 }
612
613 /* Good data. Use specified location. */
614 loc = locations[1];
615 *start = le16_to_cpu(fltl->start_hi) << 16 |
616 le16_to_cpu(fltl->start_lo);
617end:
618 DEBUG2(qla_printk(KERN_DEBUG, ha, "FLTL[%s] = 0x%x.\n", loc, *start));
619 return QLA_SUCCESS;
620}
621
622static void
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -0800623qla2xxx_get_flt_info(scsi_qla_host_t *vha, uint32_t flt_addr)
Andrew Vasquezc00d8992008-09-11 21:22:49 -0700624{
625 const char *loc, *locations[] = { "DEF", "FLT" };
Andrew Vasquez3a03eb72009-01-05 11:18:11 -0800626 const uint32_t def_fw[] =
627 { FA_RISC_CODE_ADDR, FA_RISC_CODE_ADDR, FA_RISC_CODE_ADDR_81 };
628 const uint32_t def_boot[] =
629 { FA_BOOT_CODE_ADDR, FA_BOOT_CODE_ADDR, FA_BOOT_CODE_ADDR_81 };
630 const uint32_t def_vpd_nvram[] =
631 { FA_VPD_NVRAM_ADDR, FA_VPD_NVRAM_ADDR, FA_VPD_NVRAM_ADDR_81 };
632 const uint32_t def_fdt[] =
633 { FA_FLASH_DESCR_ADDR_24, FA_FLASH_DESCR_ADDR,
634 FA_FLASH_DESCR_ADDR_81 };
635 const uint32_t def_npiv_conf0[] =
636 { FA_NPIV_CONF0_ADDR_24, FA_NPIV_CONF0_ADDR,
637 FA_NPIV_CONF0_ADDR_81 };
638 const uint32_t def_npiv_conf1[] =
639 { FA_NPIV_CONF1_ADDR_24, FA_NPIV_CONF1_ADDR,
640 FA_NPIV_CONF1_ADDR_81 };
641 uint32_t def;
Andrew Vasquezc00d8992008-09-11 21:22:49 -0700642 uint16_t *wptr;
643 uint16_t cnt, chksum;
644 uint32_t start;
645 struct qla_flt_header *flt;
646 struct qla_flt_region *region;
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -0800647 struct qla_hw_data *ha = vha->hw;
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800648 struct req_que *req = ha->req_q_map[0];
Andrew Vasquezc00d8992008-09-11 21:22:49 -0700649
650 ha->flt_region_flt = flt_addr;
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -0800651 wptr = (uint16_t *)req->ring;
652 flt = (struct qla_flt_header *)req->ring;
Andrew Vasquezc00d8992008-09-11 21:22:49 -0700653 region = (struct qla_flt_region *)&flt[1];
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -0800654 ha->isp_ops->read_optrom(vha, (uint8_t *)req->ring,
Andrew Vasquezc00d8992008-09-11 21:22:49 -0700655 flt_addr << 2, OPTROM_BURST_SIZE);
656 if (*wptr == __constant_cpu_to_le16(0xffff))
657 goto no_flash_data;
658 if (flt->version != __constant_cpu_to_le16(1)) {
659 DEBUG2(qla_printk(KERN_INFO, ha, "Unsupported FLT detected: "
660 "version=0x%x length=0x%x checksum=0x%x.\n",
661 le16_to_cpu(flt->version), le16_to_cpu(flt->length),
662 le16_to_cpu(flt->checksum)));
663 goto no_flash_data;
664 }
665
666 cnt = (sizeof(struct qla_flt_header) + le16_to_cpu(flt->length)) >> 1;
667 for (chksum = 0; cnt; cnt--)
668 chksum += le16_to_cpu(*wptr++);
669 if (chksum) {
670 DEBUG2(qla_printk(KERN_INFO, ha, "Inconsistent FLT detected: "
671 "version=0x%x length=0x%x checksum=0x%x.\n",
672 le16_to_cpu(flt->version), le16_to_cpu(flt->length),
673 chksum));
674 goto no_flash_data;
675 }
676
677 loc = locations[1];
678 cnt = le16_to_cpu(flt->length) / sizeof(struct qla_flt_region);
679 for ( ; cnt; cnt--, region++) {
680 /* Store addresses as DWORD offsets. */
681 start = le32_to_cpu(region->start) >> 2;
682
683 DEBUG3(qla_printk(KERN_DEBUG, ha, "FLT[%02x]: start=0x%x "
684 "end=0x%x size=0x%x.\n", le32_to_cpu(region->code), start,
685 le32_to_cpu(region->end) >> 2, le32_to_cpu(region->size)));
686
Andrew Vasquez90886082009-02-08 20:50:14 -0800687 switch (le32_to_cpu(region->code) & 0xff) {
Andrew Vasquezc00d8992008-09-11 21:22:49 -0700688 case FLT_REG_FW:
689 ha->flt_region_fw = start;
690 break;
691 case FLT_REG_BOOT_CODE:
692 ha->flt_region_boot = start;
693 break;
694 case FLT_REG_VPD_0:
695 ha->flt_region_vpd_nvram = start;
696 break;
697 case FLT_REG_FDT:
698 ha->flt_region_fdt = start;
699 break;
Andrew Vasquez272976c2008-09-11 21:22:50 -0700700 case FLT_REG_NPIV_CONF_0:
Andrew Vasquez3a03eb72009-01-05 11:18:11 -0800701 if (!(PCI_FUNC(ha->pdev->devfn) & 1))
Andrew Vasquez272976c2008-09-11 21:22:50 -0700702 ha->flt_region_npiv_conf = start;
703 break;
704 case FLT_REG_NPIV_CONF_1:
Andrew Vasquez3a03eb72009-01-05 11:18:11 -0800705 if (PCI_FUNC(ha->pdev->devfn) & 1)
Andrew Vasquez272976c2008-09-11 21:22:50 -0700706 ha->flt_region_npiv_conf = start;
707 break;
Andrew Vasquezc00d8992008-09-11 21:22:49 -0700708 }
709 }
710 goto done;
711
712no_flash_data:
713 /* Use hardcoded defaults. */
714 loc = locations[0];
Andrew Vasquez3a03eb72009-01-05 11:18:11 -0800715 def = 0;
716 if (IS_QLA24XX_TYPE(ha))
717 def = 0;
718 else if (IS_QLA25XX(ha))
719 def = 1;
720 else if (IS_QLA81XX(ha))
721 def = 2;
722 ha->flt_region_fw = def_fw[def];
723 ha->flt_region_boot = def_boot[def];
724 ha->flt_region_vpd_nvram = def_vpd_nvram[def];
725 ha->flt_region_fdt = def_fdt[def];
726 ha->flt_region_npiv_conf = !(PCI_FUNC(ha->pdev->devfn) & 1) ?
727 def_npiv_conf0[def]: def_npiv_conf1[def];
Andrew Vasquezc00d8992008-09-11 21:22:49 -0700728done:
729 DEBUG2(qla_printk(KERN_DEBUG, ha, "FLT[%s]: boot=0x%x fw=0x%x "
Andrew Vasquez1ded85e2009-01-05 11:18:05 -0800730 "vpd_nvram=0x%x fdt=0x%x flt=0x%x npiv=0x%x.\n", loc,
Andrew Vasquezc00d8992008-09-11 21:22:49 -0700731 ha->flt_region_boot, ha->flt_region_fw, ha->flt_region_vpd_nvram,
Andrew Vasquez1ded85e2009-01-05 11:18:05 -0800732 ha->flt_region_fdt, ha->flt_region_flt, ha->flt_region_npiv_conf));
Andrew Vasquezc00d8992008-09-11 21:22:49 -0700733}
734
735static void
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -0800736qla2xxx_get_fdt_info(scsi_qla_host_t *vha)
Andrew Vasquez7d232c72008-04-03 13:13:22 -0700737{
Lalit Chandivade821b3992008-10-24 15:13:44 -0700738#define FLASH_BLK_SIZE_4K 0x1000
Andrew Vasquez7d232c72008-04-03 13:13:22 -0700739#define FLASH_BLK_SIZE_32K 0x8000
740#define FLASH_BLK_SIZE_64K 0x10000
Andrew Vasquezc00d8992008-09-11 21:22:49 -0700741 const char *loc, *locations[] = { "MID", "FDT" };
Andrew Vasquez7d232c72008-04-03 13:13:22 -0700742 uint16_t cnt, chksum;
743 uint16_t *wptr;
744 struct qla_fdt_layout *fdt;
745 uint8_t man_id, flash_id;
Andrew Vasquezc00d8992008-09-11 21:22:49 -0700746 uint16_t mid, fid;
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -0800747 struct qla_hw_data *ha = vha->hw;
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800748 struct req_que *req = ha->req_q_map[0];
Andrew Vasquez7d232c72008-04-03 13:13:22 -0700749
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -0800750 wptr = (uint16_t *)req->ring;
751 fdt = (struct qla_fdt_layout *)req->ring;
752 ha->isp_ops->read_optrom(vha, (uint8_t *)req->ring,
Andrew Vasquezc00d8992008-09-11 21:22:49 -0700753 ha->flt_region_fdt << 2, OPTROM_BURST_SIZE);
Andrew Vasquez7d232c72008-04-03 13:13:22 -0700754 if (*wptr == __constant_cpu_to_le16(0xffff))
755 goto no_flash_data;
756 if (fdt->sig[0] != 'Q' || fdt->sig[1] != 'L' || fdt->sig[2] != 'I' ||
757 fdt->sig[3] != 'D')
758 goto no_flash_data;
759
760 for (cnt = 0, chksum = 0; cnt < sizeof(struct qla_fdt_layout) >> 1;
761 cnt++)
762 chksum += le16_to_cpu(*wptr++);
763 if (chksum) {
764 DEBUG2(qla_printk(KERN_INFO, ha, "Inconsistent FDT detected: "
765 "checksum=0x%x id=%c version=0x%x.\n", chksum, fdt->sig[0],
766 le16_to_cpu(fdt->version)));
767 DEBUG9(qla2x00_dump_buffer((uint8_t *)fdt, sizeof(*fdt)));
768 goto no_flash_data;
769 }
770
Andrew Vasquezc00d8992008-09-11 21:22:49 -0700771 loc = locations[1];
772 mid = le16_to_cpu(fdt->man_id);
773 fid = le16_to_cpu(fdt->id);
Andrew Vasquez7d232c72008-04-03 13:13:22 -0700774 ha->fdt_wrt_disable = fdt->wrt_disable_bits;
Andrew Vasquez3a03eb72009-01-05 11:18:11 -0800775 ha->fdt_erase_cmd = flash_conf_addr(ha, 0x0300 | fdt->erase_cmd);
Andrew Vasquez7d232c72008-04-03 13:13:22 -0700776 ha->fdt_block_size = le32_to_cpu(fdt->block_size);
777 if (fdt->unprotect_sec_cmd) {
Andrew Vasquez3a03eb72009-01-05 11:18:11 -0800778 ha->fdt_unprotect_sec_cmd = flash_conf_addr(ha, 0x0300 |
Andrew Vasquez7d232c72008-04-03 13:13:22 -0700779 fdt->unprotect_sec_cmd);
780 ha->fdt_protect_sec_cmd = fdt->protect_sec_cmd ?
Andrew Vasquez3a03eb72009-01-05 11:18:11 -0800781 flash_conf_addr(ha, 0x0300 | fdt->protect_sec_cmd):
782 flash_conf_addr(ha, 0x0336);
Andrew Vasquez7d232c72008-04-03 13:13:22 -0700783 }
Andrew Vasquezc00d8992008-09-11 21:22:49 -0700784 goto done;
Andrew Vasquez7d232c72008-04-03 13:13:22 -0700785no_flash_data:
Andrew Vasquezc00d8992008-09-11 21:22:49 -0700786 loc = locations[0];
Andrew Vasquez7d232c72008-04-03 13:13:22 -0700787 qla24xx_get_flash_manufacturer(ha, &man_id, &flash_id);
Andrew Vasquezc00d8992008-09-11 21:22:49 -0700788 mid = man_id;
789 fid = flash_id;
Andrew Vasquez7d232c72008-04-03 13:13:22 -0700790 ha->fdt_wrt_disable = 0x9c;
Andrew Vasquez3a03eb72009-01-05 11:18:11 -0800791 ha->fdt_erase_cmd = flash_conf_addr(ha, 0x03d8);
Andrew Vasquez7d232c72008-04-03 13:13:22 -0700792 switch (man_id) {
793 case 0xbf: /* STT flash. */
794 if (flash_id == 0x8e)
795 ha->fdt_block_size = FLASH_BLK_SIZE_64K;
796 else
797 ha->fdt_block_size = FLASH_BLK_SIZE_32K;
798
799 if (flash_id == 0x80)
Andrew Vasquez3a03eb72009-01-05 11:18:11 -0800800 ha->fdt_erase_cmd = flash_conf_addr(ha, 0x0352);
Andrew Vasquez7d232c72008-04-03 13:13:22 -0700801 break;
802 case 0x13: /* ST M25P80. */
803 ha->fdt_block_size = FLASH_BLK_SIZE_64K;
804 break;
805 case 0x1f: /* Atmel 26DF081A. */
Lalit Chandivade821b3992008-10-24 15:13:44 -0700806 ha->fdt_block_size = FLASH_BLK_SIZE_4K;
Andrew Vasquez3a03eb72009-01-05 11:18:11 -0800807 ha->fdt_erase_cmd = flash_conf_addr(ha, 0x0320);
808 ha->fdt_unprotect_sec_cmd = flash_conf_addr(ha, 0x0339);
809 ha->fdt_protect_sec_cmd = flash_conf_addr(ha, 0x0336);
Andrew Vasquez7d232c72008-04-03 13:13:22 -0700810 break;
811 default:
812 /* Default to 64 kb sector size. */
813 ha->fdt_block_size = FLASH_BLK_SIZE_64K;
814 break;
815 }
Andrew Vasquezc00d8992008-09-11 21:22:49 -0700816done:
817 DEBUG2(qla_printk(KERN_DEBUG, ha, "FDT[%s]: (0x%x/0x%x) erase=0x%x "
Lalit Chandivade821b3992008-10-24 15:13:44 -0700818 "pro=%x upro=%x wrtd=0x%x blk=0x%x.\n", loc, mid, fid,
Andrew Vasquez7d232c72008-04-03 13:13:22 -0700819 ha->fdt_erase_cmd, ha->fdt_protect_sec_cmd,
Lalit Chandivade821b3992008-10-24 15:13:44 -0700820 ha->fdt_unprotect_sec_cmd, ha->fdt_wrt_disable,
Andrew Vasquez7d232c72008-04-03 13:13:22 -0700821 ha->fdt_block_size));
822}
823
Andrew Vasquezc00d8992008-09-11 21:22:49 -0700824int
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -0800825qla2xxx_get_flash_info(scsi_qla_host_t *vha)
Andrew Vasquezc00d8992008-09-11 21:22:49 -0700826{
827 int ret;
828 uint32_t flt_addr;
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -0800829 struct qla_hw_data *ha = vha->hw;
Andrew Vasquezc00d8992008-09-11 21:22:49 -0700830
Andrew Vasquez3a03eb72009-01-05 11:18:11 -0800831 if (!IS_QLA24XX_TYPE(ha) && !IS_QLA25XX(ha) && !IS_QLA81XX(ha))
Andrew Vasquezc00d8992008-09-11 21:22:49 -0700832 return QLA_SUCCESS;
833
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -0800834 ret = qla2xxx_find_flt_start(vha, &flt_addr);
Andrew Vasquezc00d8992008-09-11 21:22:49 -0700835 if (ret != QLA_SUCCESS)
836 return ret;
837
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -0800838 qla2xxx_get_flt_info(vha, flt_addr);
839 qla2xxx_get_fdt_info(vha);
Andrew Vasquezc00d8992008-09-11 21:22:49 -0700840
841 return QLA_SUCCESS;
842}
843
Andrew Vasquez272976c2008-09-11 21:22:50 -0700844void
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -0800845qla2xxx_flash_npiv_conf(scsi_qla_host_t *vha)
Andrew Vasquez272976c2008-09-11 21:22:50 -0700846{
847#define NPIV_CONFIG_SIZE (16*1024)
848 void *data;
849 uint16_t *wptr;
850 uint16_t cnt, chksum;
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800851 int i;
Andrew Vasquez272976c2008-09-11 21:22:50 -0700852 struct qla_npiv_header hdr;
853 struct qla_npiv_entry *entry;
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -0800854 struct qla_hw_data *ha = vha->hw;
Andrew Vasquez272976c2008-09-11 21:22:50 -0700855
Andrew Vasquez3a03eb72009-01-05 11:18:11 -0800856 if (!IS_QLA24XX_TYPE(ha) && !IS_QLA25XX(ha) && !IS_QLA81XX(ha))
Andrew Vasquez272976c2008-09-11 21:22:50 -0700857 return;
858
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -0800859 ha->isp_ops->read_optrom(vha, (uint8_t *)&hdr,
Andrew Vasquez272976c2008-09-11 21:22:50 -0700860 ha->flt_region_npiv_conf << 2, sizeof(struct qla_npiv_header));
861 if (hdr.version == __constant_cpu_to_le16(0xffff))
862 return;
863 if (hdr.version != __constant_cpu_to_le16(1)) {
864 DEBUG2(qla_printk(KERN_INFO, ha, "Unsupported NPIV-Config "
865 "detected: version=0x%x entries=0x%x checksum=0x%x.\n",
866 le16_to_cpu(hdr.version), le16_to_cpu(hdr.entries),
867 le16_to_cpu(hdr.checksum)));
868 return;
869 }
870
871 data = kmalloc(NPIV_CONFIG_SIZE, GFP_KERNEL);
872 if (!data) {
873 DEBUG2(qla_printk(KERN_INFO, ha, "NPIV-Config: Unable to "
874 "allocate memory.\n"));
875 return;
876 }
877
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -0800878 ha->isp_ops->read_optrom(vha, (uint8_t *)data,
Andrew Vasquez272976c2008-09-11 21:22:50 -0700879 ha->flt_region_npiv_conf << 2, NPIV_CONFIG_SIZE);
880
881 cnt = (sizeof(struct qla_npiv_header) + le16_to_cpu(hdr.entries) *
882 sizeof(struct qla_npiv_entry)) >> 1;
883 for (wptr = data, chksum = 0; cnt; cnt--)
884 chksum += le16_to_cpu(*wptr++);
885 if (chksum) {
886 DEBUG2(qla_printk(KERN_INFO, ha, "Inconsistent NPIV-Config "
887 "detected: version=0x%x entries=0x%x checksum=0x%x.\n",
888 le16_to_cpu(hdr.version), le16_to_cpu(hdr.entries),
889 chksum));
890 goto done;
891 }
892
893 entry = data + sizeof(struct qla_npiv_header);
894 cnt = le16_to_cpu(hdr.entries);
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800895 for (i = 0; cnt; cnt--, entry++, i++) {
Andrew Vasquez272976c2008-09-11 21:22:50 -0700896 uint16_t flags;
897 struct fc_vport_identifiers vid;
898 struct fc_vport *vport;
899
900 flags = le16_to_cpu(entry->flags);
901 if (flags == 0xffff)
902 continue;
903 if ((flags & BIT_0) == 0)
904 continue;
905
906 memset(&vid, 0, sizeof(vid));
907 vid.roles = FC_PORT_ROLE_FCP_INITIATOR;
908 vid.vport_type = FC_PORTTYPE_NPIV;
909 vid.disable = false;
910 vid.port_name = wwn_to_u64(entry->port_name);
911 vid.node_name = wwn_to_u64(entry->node_name);
912
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800913 memcpy(&ha->npiv_info[i], entry, sizeof(struct qla_npiv_entry));
Andrew Vasquez272976c2008-09-11 21:22:50 -0700914
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800915 DEBUG2(qla_printk(KERN_DEBUG, ha, "NPIV[%02x]: wwpn=%llx "
916 "wwnn=%llx vf_id=0x%x Q_qos=0x%x F_qos=0x%x.\n", cnt,
917 vid.port_name, vid.node_name, le16_to_cpu(entry->vf_id),
918 entry->q_qos, entry->f_qos));
919
920 if (i < QLA_PRECONFIG_VPORTS) {
921 vport = fc_vport_create(vha->host, 0, &vid);
922 if (!vport)
923 qla_printk(KERN_INFO, ha,
924 "NPIV-Config: Failed to create vport [%02x]: "
925 "wwpn=%llx wwnn=%llx.\n", cnt,
926 vid.port_name, vid.node_name);
927 }
Andrew Vasquez272976c2008-09-11 21:22:50 -0700928 }
929done:
930 kfree(data);
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800931 ha->npiv_info = NULL;
Andrew Vasquez272976c2008-09-11 21:22:50 -0700932}
933
Andrew Vasquezcb8dacb2008-04-03 13:13:19 -0700934static void
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -0800935qla24xx_unprotect_flash(struct qla_hw_data *ha)
Andrew Vasquezcb8dacb2008-04-03 13:13:19 -0700936{
937 struct device_reg_24xx __iomem *reg = &ha->iobase->isp24;
938
939 /* Enable flash write. */
940 WRT_REG_DWORD(&reg->ctrl_status,
941 RD_REG_DWORD(&reg->ctrl_status) | CSRX_FLASH_ENABLE);
942 RD_REG_DWORD(&reg->ctrl_status); /* PCI Posting. */
943
Andrew Vasquez7d232c72008-04-03 13:13:22 -0700944 if (!ha->fdt_wrt_disable)
945 return;
946
Joe Carnucciob872ca42009-01-22 09:45:36 -0800947 /* Disable flash write-protection, first clear SR protection bit */
Andrew Vasquez3a03eb72009-01-05 11:18:11 -0800948 qla24xx_write_flash_dword(ha, flash_conf_addr(ha, 0x101), 0);
Joe Carnucciob872ca42009-01-22 09:45:36 -0800949 /* Then write zero again to clear remaining SR bits.*/
Andrew Vasquez3a03eb72009-01-05 11:18:11 -0800950 qla24xx_write_flash_dword(ha, flash_conf_addr(ha, 0x101), 0);
Andrew Vasquezcb8dacb2008-04-03 13:13:19 -0700951}
952
953static void
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -0800954qla24xx_protect_flash(struct qla_hw_data *ha)
Andrew Vasquezcb8dacb2008-04-03 13:13:19 -0700955{
956 uint32_t cnt;
957 struct device_reg_24xx __iomem *reg = &ha->iobase->isp24;
958
Andrew Vasquez7d232c72008-04-03 13:13:22 -0700959 if (!ha->fdt_wrt_disable)
960 goto skip_wrt_protect;
961
Andrew Vasquezcb8dacb2008-04-03 13:13:19 -0700962 /* Enable flash write-protection and wait for completion. */
Andrew Vasquez3a03eb72009-01-05 11:18:11 -0800963 qla24xx_write_flash_dword(ha, flash_conf_addr(ha, 0x101),
Andrew Vasquez7d232c72008-04-03 13:13:22 -0700964 ha->fdt_wrt_disable);
Andrew Vasquezcb8dacb2008-04-03 13:13:19 -0700965 for (cnt = 300; cnt &&
Andrew Vasquez3a03eb72009-01-05 11:18:11 -0800966 qla24xx_read_flash_dword(ha, flash_conf_addr(ha, 0x005)) & BIT_0;
Andrew Vasquezcb8dacb2008-04-03 13:13:19 -0700967 cnt--) {
968 udelay(10);
969 }
970
Andrew Vasquez7d232c72008-04-03 13:13:22 -0700971skip_wrt_protect:
Andrew Vasquezcb8dacb2008-04-03 13:13:19 -0700972 /* Disable flash write. */
973 WRT_REG_DWORD(&reg->ctrl_status,
974 RD_REG_DWORD(&reg->ctrl_status) & ~CSRX_FLASH_ENABLE);
975 RD_REG_DWORD(&reg->ctrl_status); /* PCI Posting. */
976}
977
Adrian Bunke5f82ab2006-11-08 19:55:50 -0800978static int
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -0800979qla24xx_write_flash_data(scsi_qla_host_t *vha, uint32_t *dwptr, uint32_t faddr,
Andrew Vasquez459c5372005-07-06 10:31:07 -0700980 uint32_t dwords)
981{
982 int ret;
Andrew Vasquez7c283172009-01-22 09:45:34 -0800983 uint32_t liter;
Andrew Vasquez7d232c72008-04-03 13:13:22 -0700984 uint32_t sec_mask, rest_addr;
Andrew Vasquez85d0acb2009-01-22 09:45:29 -0800985 uint32_t fdata;
Andrew Vasquez338c9162007-09-20 14:07:33 -0700986 dma_addr_t optrom_dma;
987 void *optrom = NULL;
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -0800988 struct qla_hw_data *ha = vha->hw;
Andrew Vasquez459c5372005-07-06 10:31:07 -0700989
990 ret = QLA_SUCCESS;
991
Andrew Vasquez338c9162007-09-20 14:07:33 -0700992 /* Prepare burst-capable write on supported ISPs. */
Andrew Vasquez3a03eb72009-01-05 11:18:11 -0800993 if ((IS_QLA25XX(ha) || IS_QLA81XX(ha)) && !(faddr & 0xfff) &&
Andrew Vasquez338c9162007-09-20 14:07:33 -0700994 dwords > OPTROM_BURST_DWORDS) {
995 optrom = dma_alloc_coherent(&ha->pdev->dev, OPTROM_BURST_SIZE,
996 &optrom_dma, GFP_KERNEL);
997 if (!optrom) {
998 qla_printk(KERN_DEBUG, ha,
999 "Unable to allocate memory for optrom burst write "
1000 "(%x KB).\n", OPTROM_BURST_SIZE / 1024);
1001 }
1002 }
1003
Andrew Vasquez7d232c72008-04-03 13:13:22 -07001004 rest_addr = (ha->fdt_block_size >> 2) - 1;
Andrew Vasquez85d0acb2009-01-22 09:45:29 -08001005 sec_mask = ~rest_addr;
Andrew Vasquez459c5372005-07-06 10:31:07 -07001006
Andrew Vasquezcb8dacb2008-04-03 13:13:19 -07001007 qla24xx_unprotect_flash(ha);
Andrew Vasquez459c5372005-07-06 10:31:07 -07001008
Andrew Vasquez338c9162007-09-20 14:07:33 -07001009 for (liter = 0; liter < dwords; liter++, faddr++, dwptr++) {
Andrew Vasquez85d0acb2009-01-22 09:45:29 -08001010 fdata = (faddr & sec_mask) << 2;
Ravi Anand45aeaf12006-05-17 15:08:49 -07001011
Andrew Vasquez338c9162007-09-20 14:07:33 -07001012 /* Are we at the beginning of a sector? */
Andrew Vasquez85d0acb2009-01-22 09:45:29 -08001013 if ((faddr & rest_addr) == 0) {
Andrew Vasquez7d232c72008-04-03 13:13:22 -07001014 /* Do sector unprotect. */
1015 if (ha->fdt_unprotect_sec_cmd)
Ravi Anand45aeaf12006-05-17 15:08:49 -07001016 qla24xx_write_flash_dword(ha,
Andrew Vasquez7d232c72008-04-03 13:13:22 -07001017 ha->fdt_unprotect_sec_cmd,
Ravi Anand45aeaf12006-05-17 15:08:49 -07001018 (fdata & 0xff00) | ((fdata << 16) &
1019 0xff0000) | ((fdata >> 16) & 0xff));
Andrew Vasquez7d232c72008-04-03 13:13:22 -07001020 ret = qla24xx_write_flash_dword(ha, ha->fdt_erase_cmd,
Andrew Vasquez338c9162007-09-20 14:07:33 -07001021 (fdata & 0xff00) |((fdata << 16) &
1022 0xff0000) | ((fdata >> 16) & 0xff));
1023 if (ret != QLA_SUCCESS) {
Joe Carnucciob872ca42009-01-22 09:45:36 -08001024 DEBUG9(qla_printk("Unable to erase sector: "
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -08001025 "address=%x.\n", faddr));
Andrew Vasquez338c9162007-09-20 14:07:33 -07001026 break;
1027 }
Andrew Vasquez459c5372005-07-06 10:31:07 -07001028 }
Andrew Vasquez338c9162007-09-20 14:07:33 -07001029
1030 /* Go with burst-write. */
Andrew Vasquez94d6a2b2007-10-19 15:59:16 -07001031 if (optrom && (liter + OPTROM_BURST_DWORDS) <= dwords) {
Andrew Vasquez338c9162007-09-20 14:07:33 -07001032 /* Copy data to DMA'ble buffer. */
Andrew Vasquez7c283172009-01-22 09:45:34 -08001033 memcpy(optrom, dwptr, OPTROM_BURST_SIZE);
Andrew Vasquez338c9162007-09-20 14:07:33 -07001034
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -08001035 ret = qla2x00_load_ram(vha, optrom_dma,
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08001036 flash_data_addr(ha, faddr),
Andrew Vasquez338c9162007-09-20 14:07:33 -07001037 OPTROM_BURST_DWORDS);
1038 if (ret != QLA_SUCCESS) {
1039 qla_printk(KERN_WARNING, ha,
1040 "Unable to burst-write optrom segment "
1041 "(%x/%x/%llx).\n", ret,
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08001042 flash_data_addr(ha, faddr),
Andrew Morton875baf32007-10-16 14:28:20 -07001043 (unsigned long long)optrom_dma);
Andrew Vasquez338c9162007-09-20 14:07:33 -07001044 qla_printk(KERN_WARNING, ha,
1045 "Reverting to slow-write.\n");
1046
1047 dma_free_coherent(&ha->pdev->dev,
1048 OPTROM_BURST_SIZE, optrom, optrom_dma);
1049 optrom = NULL;
1050 } else {
1051 liter += OPTROM_BURST_DWORDS - 1;
1052 faddr += OPTROM_BURST_DWORDS - 1;
1053 dwptr += OPTROM_BURST_DWORDS - 1;
1054 continue;
1055 }
1056 }
1057
1058 ret = qla24xx_write_flash_dword(ha,
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08001059 flash_data_addr(ha, faddr), cpu_to_le32(*dwptr));
Andrew Vasquez338c9162007-09-20 14:07:33 -07001060 if (ret != QLA_SUCCESS) {
1061 DEBUG9(printk("%s(%ld) Unable to program flash "
1062 "address=%x data=%x.\n", __func__,
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -08001063 vha->host_no, faddr, *dwptr));
Andrew Vasquez338c9162007-09-20 14:07:33 -07001064 break;
1065 }
1066
Andrew Vasquez7d232c72008-04-03 13:13:22 -07001067 /* Do sector protect. */
1068 if (ha->fdt_unprotect_sec_cmd &&
Andrew Vasquez338c9162007-09-20 14:07:33 -07001069 ((faddr & rest_addr) == rest_addr))
1070 qla24xx_write_flash_dword(ha,
Andrew Vasquez7d232c72008-04-03 13:13:22 -07001071 ha->fdt_protect_sec_cmd,
Andrew Vasquez338c9162007-09-20 14:07:33 -07001072 (fdata & 0xff00) | ((fdata << 16) &
1073 0xff0000) | ((fdata >> 16) & 0xff));
1074 }
Andrew Vasquez459c5372005-07-06 10:31:07 -07001075
Andrew Vasquezcb8dacb2008-04-03 13:13:19 -07001076 qla24xx_protect_flash(ha);
Andrew Vasquez459c5372005-07-06 10:31:07 -07001077
Andrew Vasquez338c9162007-09-20 14:07:33 -07001078 if (optrom)
1079 dma_free_coherent(&ha->pdev->dev,
1080 OPTROM_BURST_SIZE, optrom, optrom_dma);
1081
Andrew Vasquez459c5372005-07-06 10:31:07 -07001082 return ret;
1083}
1084
1085uint8_t *
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -08001086qla2x00_read_nvram_data(scsi_qla_host_t *vha, uint8_t *buf, uint32_t naddr,
Andrew Vasquez459c5372005-07-06 10:31:07 -07001087 uint32_t bytes)
1088{
1089 uint32_t i;
1090 uint16_t *wptr;
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -08001091 struct qla_hw_data *ha = vha->hw;
Andrew Vasquez459c5372005-07-06 10:31:07 -07001092
1093 /* Word reads to NVRAM via registers. */
1094 wptr = (uint16_t *)buf;
1095 qla2x00_lock_nvram_access(ha);
1096 for (i = 0; i < bytes >> 1; i++, naddr++)
1097 wptr[i] = cpu_to_le16(qla2x00_get_nvram_word(ha,
1098 naddr));
1099 qla2x00_unlock_nvram_access(ha);
1100
1101 return buf;
1102}
1103
1104uint8_t *
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -08001105qla24xx_read_nvram_data(scsi_qla_host_t *vha, uint8_t *buf, uint32_t naddr,
Andrew Vasquez459c5372005-07-06 10:31:07 -07001106 uint32_t bytes)
1107{
1108 uint32_t i;
1109 uint32_t *dwptr;
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08001110 struct qla_hw_data *ha = vha->hw;
Andrew Vasquez459c5372005-07-06 10:31:07 -07001111
1112 /* Dword reads to flash. */
1113 dwptr = (uint32_t *)buf;
1114 for (i = 0; i < bytes >> 2; i++, naddr++)
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08001115 dwptr[i] = cpu_to_le32(qla24xx_read_flash_dword(ha,
1116 nvram_data_addr(ha, naddr)));
Andrew Vasquez459c5372005-07-06 10:31:07 -07001117
Andrew Vasquez459c5372005-07-06 10:31:07 -07001118 return buf;
1119}
1120
1121int
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -08001122qla2x00_write_nvram_data(scsi_qla_host_t *vha, uint8_t *buf, uint32_t naddr,
Andrew Vasquez459c5372005-07-06 10:31:07 -07001123 uint32_t bytes)
1124{
1125 int ret, stat;
1126 uint32_t i;
1127 uint16_t *wptr;
Andrew Vasquez2c96d8d2007-10-19 15:59:15 -07001128 unsigned long flags;
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -08001129 struct qla_hw_data *ha = vha->hw;
Andrew Vasquez459c5372005-07-06 10:31:07 -07001130
1131 ret = QLA_SUCCESS;
1132
Andrew Vasquez2c96d8d2007-10-19 15:59:15 -07001133 spin_lock_irqsave(&ha->hardware_lock, flags);
Andrew Vasquez459c5372005-07-06 10:31:07 -07001134 qla2x00_lock_nvram_access(ha);
1135
1136 /* Disable NVRAM write-protection. */
1137 stat = qla2x00_clear_nvram_protection(ha);
1138
1139 wptr = (uint16_t *)buf;
1140 for (i = 0; i < bytes >> 1; i++, naddr++) {
1141 qla2x00_write_nvram_word(ha, naddr,
1142 cpu_to_le16(*wptr));
1143 wptr++;
1144 }
1145
1146 /* Enable NVRAM write-protection. */
1147 qla2x00_set_nvram_protection(ha, stat);
1148
1149 qla2x00_unlock_nvram_access(ha);
Andrew Vasquez2c96d8d2007-10-19 15:59:15 -07001150 spin_unlock_irqrestore(&ha->hardware_lock, flags);
Andrew Vasquez459c5372005-07-06 10:31:07 -07001151
1152 return ret;
1153}
1154
1155int
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -08001156qla24xx_write_nvram_data(scsi_qla_host_t *vha, uint8_t *buf, uint32_t naddr,
Andrew Vasquez459c5372005-07-06 10:31:07 -07001157 uint32_t bytes)
1158{
1159 int ret;
1160 uint32_t i;
1161 uint32_t *dwptr;
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -08001162 struct qla_hw_data *ha = vha->hw;
Andrew Vasquez459c5372005-07-06 10:31:07 -07001163 struct device_reg_24xx __iomem *reg = &ha->iobase->isp24;
1164
1165 ret = QLA_SUCCESS;
1166
Andrew Vasquez459c5372005-07-06 10:31:07 -07001167 /* Enable flash write. */
1168 WRT_REG_DWORD(&reg->ctrl_status,
1169 RD_REG_DWORD(&reg->ctrl_status) | CSRX_FLASH_ENABLE);
1170 RD_REG_DWORD(&reg->ctrl_status); /* PCI Posting. */
1171
1172 /* Disable NVRAM write-protection. */
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08001173 qla24xx_write_flash_dword(ha, nvram_conf_addr(ha, 0x101), 0);
1174 qla24xx_write_flash_dword(ha, nvram_conf_addr(ha, 0x101), 0);
Andrew Vasquez459c5372005-07-06 10:31:07 -07001175
1176 /* Dword writes to flash. */
1177 dwptr = (uint32_t *)buf;
1178 for (i = 0; i < bytes >> 2; i++, naddr++, dwptr++) {
1179 ret = qla24xx_write_flash_dword(ha,
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08001180 nvram_data_addr(ha, naddr), cpu_to_le32(*dwptr));
Andrew Vasquez459c5372005-07-06 10:31:07 -07001181 if (ret != QLA_SUCCESS) {
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -08001182 DEBUG9(qla_printk("Unable to program nvram address=%x "
1183 "data=%x.\n", naddr, *dwptr));
Andrew Vasquez459c5372005-07-06 10:31:07 -07001184 break;
1185 }
1186 }
1187
1188 /* Enable NVRAM write-protection. */
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08001189 qla24xx_write_flash_dword(ha, nvram_conf_addr(ha, 0x101), 0x8c);
Andrew Vasquez459c5372005-07-06 10:31:07 -07001190
1191 /* Disable flash write. */
1192 WRT_REG_DWORD(&reg->ctrl_status,
1193 RD_REG_DWORD(&reg->ctrl_status) & ~CSRX_FLASH_ENABLE);
1194 RD_REG_DWORD(&reg->ctrl_status); /* PCI Posting. */
1195
Andrew Vasquez459c5372005-07-06 10:31:07 -07001196 return ret;
1197}
andrew.vasquez@qlogic.comf6df1442006-01-31 16:05:07 -08001198
Andrew Vasquezc3a2f0d2007-07-19 20:37:34 -07001199uint8_t *
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -08001200qla25xx_read_nvram_data(scsi_qla_host_t *vha, uint8_t *buf, uint32_t naddr,
Andrew Vasquezc3a2f0d2007-07-19 20:37:34 -07001201 uint32_t bytes)
1202{
1203 uint32_t i;
1204 uint32_t *dwptr;
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -08001205 struct qla_hw_data *ha = vha->hw;
Andrew Vasquezc3a2f0d2007-07-19 20:37:34 -07001206
1207 /* Dword reads to flash. */
1208 dwptr = (uint32_t *)buf;
1209 for (i = 0; i < bytes >> 2; i++, naddr++)
1210 dwptr[i] = cpu_to_le32(qla24xx_read_flash_dword(ha,
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08001211 flash_data_addr(ha, ha->flt_region_vpd_nvram | naddr)));
Andrew Vasquezc3a2f0d2007-07-19 20:37:34 -07001212
1213 return buf;
1214}
1215
1216int
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -08001217qla25xx_write_nvram_data(scsi_qla_host_t *vha, uint8_t *buf, uint32_t naddr,
Andrew Vasquezc3a2f0d2007-07-19 20:37:34 -07001218 uint32_t bytes)
1219{
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -08001220 struct qla_hw_data *ha = vha->hw;
Andrew Vasquez2c96d8d2007-10-19 15:59:15 -07001221#define RMW_BUFFER_SIZE (64 * 1024)
1222 uint8_t *dbuf;
1223
1224 dbuf = vmalloc(RMW_BUFFER_SIZE);
1225 if (!dbuf)
1226 return QLA_MEMORY_ALLOC_FAILED;
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -08001227 ha->isp_ops->read_optrom(vha, dbuf, ha->flt_region_vpd_nvram << 2,
Andrew Vasquez2c96d8d2007-10-19 15:59:15 -07001228 RMW_BUFFER_SIZE);
1229 memcpy(dbuf + (naddr << 2), buf, bytes);
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -08001230 ha->isp_ops->write_optrom(vha, dbuf, ha->flt_region_vpd_nvram << 2,
Andrew Vasquez2c96d8d2007-10-19 15:59:15 -07001231 RMW_BUFFER_SIZE);
1232 vfree(dbuf);
1233
1234 return QLA_SUCCESS;
Andrew Vasquezc3a2f0d2007-07-19 20:37:34 -07001235}
andrew.vasquez@qlogic.comf6df1442006-01-31 16:05:07 -08001236
1237static inline void
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -08001238qla2x00_flip_colors(struct qla_hw_data *ha, uint16_t *pflags)
andrew.vasquez@qlogic.comf6df1442006-01-31 16:05:07 -08001239{
1240 if (IS_QLA2322(ha)) {
1241 /* Flip all colors. */
1242 if (ha->beacon_color_state == QLA_LED_ALL_ON) {
1243 /* Turn off. */
1244 ha->beacon_color_state = 0;
1245 *pflags = GPIO_LED_ALL_OFF;
1246 } else {
1247 /* Turn on. */
1248 ha->beacon_color_state = QLA_LED_ALL_ON;
1249 *pflags = GPIO_LED_RGA_ON;
1250 }
1251 } else {
1252 /* Flip green led only. */
1253 if (ha->beacon_color_state == QLA_LED_GRN_ON) {
1254 /* Turn off. */
1255 ha->beacon_color_state = 0;
1256 *pflags = GPIO_LED_GREEN_OFF_AMBER_OFF;
1257 } else {
1258 /* Turn on. */
1259 ha->beacon_color_state = QLA_LED_GRN_ON;
1260 *pflags = GPIO_LED_GREEN_ON_AMBER_OFF;
1261 }
1262 }
1263}
1264
Andrew Vasquez948882f2008-01-31 12:33:44 -08001265#define PIO_REG(h, r) ((h)->pio_address + offsetof(struct device_reg_2xxx, r))
1266
andrew.vasquez@qlogic.comf6df1442006-01-31 16:05:07 -08001267void
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -08001268qla2x00_beacon_blink(struct scsi_qla_host *vha)
andrew.vasquez@qlogic.comf6df1442006-01-31 16:05:07 -08001269{
1270 uint16_t gpio_enable;
1271 uint16_t gpio_data;
1272 uint16_t led_color = 0;
1273 unsigned long flags;
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -08001274 struct qla_hw_data *ha = vha->hw;
andrew.vasquez@qlogic.comf6df1442006-01-31 16:05:07 -08001275 struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
1276
andrew.vasquez@qlogic.comf6df1442006-01-31 16:05:07 -08001277 spin_lock_irqsave(&ha->hardware_lock, flags);
1278
1279 /* Save the Original GPIOE. */
1280 if (ha->pio_address) {
Andrew Vasquez948882f2008-01-31 12:33:44 -08001281 gpio_enable = RD_REG_WORD_PIO(PIO_REG(ha, gpioe));
1282 gpio_data = RD_REG_WORD_PIO(PIO_REG(ha, gpiod));
andrew.vasquez@qlogic.comf6df1442006-01-31 16:05:07 -08001283 } else {
1284 gpio_enable = RD_REG_WORD(&reg->gpioe);
1285 gpio_data = RD_REG_WORD(&reg->gpiod);
1286 }
1287
1288 /* Set the modified gpio_enable values */
1289 gpio_enable |= GPIO_LED_MASK;
1290
1291 if (ha->pio_address) {
Andrew Vasquez948882f2008-01-31 12:33:44 -08001292 WRT_REG_WORD_PIO(PIO_REG(ha, gpioe), gpio_enable);
andrew.vasquez@qlogic.comf6df1442006-01-31 16:05:07 -08001293 } else {
1294 WRT_REG_WORD(&reg->gpioe, gpio_enable);
1295 RD_REG_WORD(&reg->gpioe);
1296 }
1297
1298 qla2x00_flip_colors(ha, &led_color);
1299
1300 /* Clear out any previously set LED color. */
1301 gpio_data &= ~GPIO_LED_MASK;
1302
1303 /* Set the new input LED color to GPIOD. */
1304 gpio_data |= led_color;
1305
1306 /* Set the modified gpio_data values */
1307 if (ha->pio_address) {
Andrew Vasquez948882f2008-01-31 12:33:44 -08001308 WRT_REG_WORD_PIO(PIO_REG(ha, gpiod), gpio_data);
andrew.vasquez@qlogic.comf6df1442006-01-31 16:05:07 -08001309 } else {
1310 WRT_REG_WORD(&reg->gpiod, gpio_data);
1311 RD_REG_WORD(&reg->gpiod);
1312 }
1313
1314 spin_unlock_irqrestore(&ha->hardware_lock, flags);
1315}
1316
1317int
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -08001318qla2x00_beacon_on(struct scsi_qla_host *vha)
andrew.vasquez@qlogic.comf6df1442006-01-31 16:05:07 -08001319{
1320 uint16_t gpio_enable;
1321 uint16_t gpio_data;
1322 unsigned long flags;
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -08001323 struct qla_hw_data *ha = vha->hw;
andrew.vasquez@qlogic.comf6df1442006-01-31 16:05:07 -08001324 struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
1325
1326 ha->fw_options[1] &= ~FO1_SET_EMPHASIS_SWING;
1327 ha->fw_options[1] |= FO1_DISABLE_GPIO6_7;
1328
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -08001329 if (qla2x00_set_fw_options(vha, ha->fw_options) != QLA_SUCCESS) {
andrew.vasquez@qlogic.comf6df1442006-01-31 16:05:07 -08001330 qla_printk(KERN_WARNING, ha,
1331 "Unable to update fw options (beacon on).\n");
1332 return QLA_FUNCTION_FAILED;
1333 }
1334
andrew.vasquez@qlogic.comf6df1442006-01-31 16:05:07 -08001335 /* Turn off LEDs. */
1336 spin_lock_irqsave(&ha->hardware_lock, flags);
1337 if (ha->pio_address) {
Andrew Vasquez948882f2008-01-31 12:33:44 -08001338 gpio_enable = RD_REG_WORD_PIO(PIO_REG(ha, gpioe));
1339 gpio_data = RD_REG_WORD_PIO(PIO_REG(ha, gpiod));
andrew.vasquez@qlogic.comf6df1442006-01-31 16:05:07 -08001340 } else {
1341 gpio_enable = RD_REG_WORD(&reg->gpioe);
1342 gpio_data = RD_REG_WORD(&reg->gpiod);
1343 }
1344 gpio_enable |= GPIO_LED_MASK;
1345
1346 /* Set the modified gpio_enable values. */
1347 if (ha->pio_address) {
Andrew Vasquez948882f2008-01-31 12:33:44 -08001348 WRT_REG_WORD_PIO(PIO_REG(ha, gpioe), gpio_enable);
andrew.vasquez@qlogic.comf6df1442006-01-31 16:05:07 -08001349 } else {
1350 WRT_REG_WORD(&reg->gpioe, gpio_enable);
1351 RD_REG_WORD(&reg->gpioe);
1352 }
1353
1354 /* Clear out previously set LED colour. */
1355 gpio_data &= ~GPIO_LED_MASK;
1356 if (ha->pio_address) {
Andrew Vasquez948882f2008-01-31 12:33:44 -08001357 WRT_REG_WORD_PIO(PIO_REG(ha, gpiod), gpio_data);
andrew.vasquez@qlogic.comf6df1442006-01-31 16:05:07 -08001358 } else {
1359 WRT_REG_WORD(&reg->gpiod, gpio_data);
1360 RD_REG_WORD(&reg->gpiod);
1361 }
1362 spin_unlock_irqrestore(&ha->hardware_lock, flags);
1363
1364 /*
1365 * Let the per HBA timer kick off the blinking process based on
1366 * the following flags. No need to do anything else now.
1367 */
1368 ha->beacon_blink_led = 1;
1369 ha->beacon_color_state = 0;
1370
1371 return QLA_SUCCESS;
1372}
1373
1374int
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -08001375qla2x00_beacon_off(struct scsi_qla_host *vha)
andrew.vasquez@qlogic.comf6df1442006-01-31 16:05:07 -08001376{
1377 int rval = QLA_SUCCESS;
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -08001378 struct qla_hw_data *ha = vha->hw;
andrew.vasquez@qlogic.comf6df1442006-01-31 16:05:07 -08001379
1380 ha->beacon_blink_led = 0;
1381
1382 /* Set the on flag so when it gets flipped it will be off. */
1383 if (IS_QLA2322(ha))
1384 ha->beacon_color_state = QLA_LED_ALL_ON;
1385 else
1386 ha->beacon_color_state = QLA_LED_GRN_ON;
1387
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -08001388 ha->isp_ops->beacon_blink(vha); /* This turns green LED off */
andrew.vasquez@qlogic.comf6df1442006-01-31 16:05:07 -08001389
1390 ha->fw_options[1] &= ~FO1_SET_EMPHASIS_SWING;
1391 ha->fw_options[1] &= ~FO1_DISABLE_GPIO6_7;
1392
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -08001393 rval = qla2x00_set_fw_options(vha, ha->fw_options);
andrew.vasquez@qlogic.comf6df1442006-01-31 16:05:07 -08001394 if (rval != QLA_SUCCESS)
1395 qla_printk(KERN_WARNING, ha,
1396 "Unable to update fw options (beacon off).\n");
1397 return rval;
1398}
1399
1400
1401static inline void
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -08001402qla24xx_flip_colors(struct qla_hw_data *ha, uint16_t *pflags)
andrew.vasquez@qlogic.comf6df1442006-01-31 16:05:07 -08001403{
1404 /* Flip all colors. */
1405 if (ha->beacon_color_state == QLA_LED_ALL_ON) {
1406 /* Turn off. */
1407 ha->beacon_color_state = 0;
1408 *pflags = 0;
1409 } else {
1410 /* Turn on. */
1411 ha->beacon_color_state = QLA_LED_ALL_ON;
1412 *pflags = GPDX_LED_YELLOW_ON | GPDX_LED_AMBER_ON;
1413 }
1414}
1415
1416void
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -08001417qla24xx_beacon_blink(struct scsi_qla_host *vha)
andrew.vasquez@qlogic.comf6df1442006-01-31 16:05:07 -08001418{
1419 uint16_t led_color = 0;
1420 uint32_t gpio_data;
1421 unsigned long flags;
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -08001422 struct qla_hw_data *ha = vha->hw;
andrew.vasquez@qlogic.comf6df1442006-01-31 16:05:07 -08001423 struct device_reg_24xx __iomem *reg = &ha->iobase->isp24;
1424
1425 /* Save the Original GPIOD. */
1426 spin_lock_irqsave(&ha->hardware_lock, flags);
1427 gpio_data = RD_REG_DWORD(&reg->gpiod);
1428
1429 /* Enable the gpio_data reg for update. */
1430 gpio_data |= GPDX_LED_UPDATE_MASK;
1431
1432 WRT_REG_DWORD(&reg->gpiod, gpio_data);
1433 gpio_data = RD_REG_DWORD(&reg->gpiod);
1434
1435 /* Set the color bits. */
1436 qla24xx_flip_colors(ha, &led_color);
1437
1438 /* Clear out any previously set LED color. */
1439 gpio_data &= ~GPDX_LED_COLOR_MASK;
1440
1441 /* Set the new input LED color to GPIOD. */
1442 gpio_data |= led_color;
1443
1444 /* Set the modified gpio_data values. */
1445 WRT_REG_DWORD(&reg->gpiod, gpio_data);
1446 gpio_data = RD_REG_DWORD(&reg->gpiod);
1447 spin_unlock_irqrestore(&ha->hardware_lock, flags);
1448}
1449
1450int
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -08001451qla24xx_beacon_on(struct scsi_qla_host *vha)
andrew.vasquez@qlogic.comf6df1442006-01-31 16:05:07 -08001452{
1453 uint32_t gpio_data;
1454 unsigned long flags;
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -08001455 struct qla_hw_data *ha = vha->hw;
andrew.vasquez@qlogic.comf6df1442006-01-31 16:05:07 -08001456 struct device_reg_24xx __iomem *reg = &ha->iobase->isp24;
1457
1458 if (ha->beacon_blink_led == 0) {
1459 /* Enable firmware for update */
1460 ha->fw_options[1] |= ADD_FO1_DISABLE_GPIO_LED_CTRL;
1461
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -08001462 if (qla2x00_set_fw_options(vha, ha->fw_options) != QLA_SUCCESS)
andrew.vasquez@qlogic.comf6df1442006-01-31 16:05:07 -08001463 return QLA_FUNCTION_FAILED;
1464
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -08001465 if (qla2x00_get_fw_options(vha, ha->fw_options) !=
andrew.vasquez@qlogic.comf6df1442006-01-31 16:05:07 -08001466 QLA_SUCCESS) {
1467 qla_printk(KERN_WARNING, ha,
1468 "Unable to update fw options (beacon on).\n");
1469 return QLA_FUNCTION_FAILED;
1470 }
1471
1472 spin_lock_irqsave(&ha->hardware_lock, flags);
1473 gpio_data = RD_REG_DWORD(&reg->gpiod);
1474
1475 /* Enable the gpio_data reg for update. */
1476 gpio_data |= GPDX_LED_UPDATE_MASK;
1477 WRT_REG_DWORD(&reg->gpiod, gpio_data);
1478 RD_REG_DWORD(&reg->gpiod);
1479
1480 spin_unlock_irqrestore(&ha->hardware_lock, flags);
1481 }
1482
1483 /* So all colors blink together. */
1484 ha->beacon_color_state = 0;
1485
1486 /* Let the per HBA timer kick off the blinking process. */
1487 ha->beacon_blink_led = 1;
1488
1489 return QLA_SUCCESS;
1490}
1491
1492int
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -08001493qla24xx_beacon_off(struct scsi_qla_host *vha)
andrew.vasquez@qlogic.comf6df1442006-01-31 16:05:07 -08001494{
1495 uint32_t gpio_data;
1496 unsigned long flags;
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -08001497 struct qla_hw_data *ha = vha->hw;
andrew.vasquez@qlogic.comf6df1442006-01-31 16:05:07 -08001498 struct device_reg_24xx __iomem *reg = &ha->iobase->isp24;
1499
1500 ha->beacon_blink_led = 0;
1501 ha->beacon_color_state = QLA_LED_ALL_ON;
1502
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -08001503 ha->isp_ops->beacon_blink(vha); /* Will flip to all off. */
andrew.vasquez@qlogic.comf6df1442006-01-31 16:05:07 -08001504
1505 /* Give control back to firmware. */
1506 spin_lock_irqsave(&ha->hardware_lock, flags);
1507 gpio_data = RD_REG_DWORD(&reg->gpiod);
1508
1509 /* Disable the gpio_data reg for update. */
1510 gpio_data &= ~GPDX_LED_UPDATE_MASK;
1511 WRT_REG_DWORD(&reg->gpiod, gpio_data);
1512 RD_REG_DWORD(&reg->gpiod);
1513 spin_unlock_irqrestore(&ha->hardware_lock, flags);
1514
1515 ha->fw_options[1] &= ~ADD_FO1_DISABLE_GPIO_LED_CTRL;
1516
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -08001517 if (qla2x00_set_fw_options(vha, ha->fw_options) != QLA_SUCCESS) {
andrew.vasquez@qlogic.comf6df1442006-01-31 16:05:07 -08001518 qla_printk(KERN_WARNING, ha,
1519 "Unable to update fw options (beacon off).\n");
1520 return QLA_FUNCTION_FAILED;
1521 }
1522
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -08001523 if (qla2x00_get_fw_options(vha, ha->fw_options) != QLA_SUCCESS) {
andrew.vasquez@qlogic.comf6df1442006-01-31 16:05:07 -08001524 qla_printk(KERN_WARNING, ha,
1525 "Unable to get fw options (beacon off).\n");
1526 return QLA_FUNCTION_FAILED;
1527 }
1528
1529 return QLA_SUCCESS;
1530}
andrew.vasquez@qlogic.com854165f2006-01-31 16:05:17 -08001531
1532
1533/*
1534 * Flash support routines
1535 */
1536
1537/**
1538 * qla2x00_flash_enable() - Setup flash for reading and writing.
1539 * @ha: HA context
1540 */
1541static void
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -08001542qla2x00_flash_enable(struct qla_hw_data *ha)
andrew.vasquez@qlogic.com854165f2006-01-31 16:05:17 -08001543{
1544 uint16_t data;
1545 struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
1546
1547 data = RD_REG_WORD(&reg->ctrl_status);
1548 data |= CSR_FLASH_ENABLE;
1549 WRT_REG_WORD(&reg->ctrl_status, data);
1550 RD_REG_WORD(&reg->ctrl_status); /* PCI Posting. */
1551}
1552
1553/**
1554 * qla2x00_flash_disable() - Disable flash and allow RISC to run.
1555 * @ha: HA context
1556 */
1557static void
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -08001558qla2x00_flash_disable(struct qla_hw_data *ha)
andrew.vasquez@qlogic.com854165f2006-01-31 16:05:17 -08001559{
1560 uint16_t data;
1561 struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
1562
1563 data = RD_REG_WORD(&reg->ctrl_status);
1564 data &= ~(CSR_FLASH_ENABLE);
1565 WRT_REG_WORD(&reg->ctrl_status, data);
1566 RD_REG_WORD(&reg->ctrl_status); /* PCI Posting. */
1567}
1568
1569/**
1570 * qla2x00_read_flash_byte() - Reads a byte from flash
1571 * @ha: HA context
1572 * @addr: Address in flash to read
1573 *
1574 * A word is read from the chip, but, only the lower byte is valid.
1575 *
1576 * Returns the byte read from flash @addr.
1577 */
1578static uint8_t
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -08001579qla2x00_read_flash_byte(struct qla_hw_data *ha, uint32_t addr)
andrew.vasquez@qlogic.com854165f2006-01-31 16:05:17 -08001580{
1581 uint16_t data;
1582 uint16_t bank_select;
1583 struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
1584
1585 bank_select = RD_REG_WORD(&reg->ctrl_status);
1586
1587 if (IS_QLA2322(ha) || IS_QLA6322(ha)) {
1588 /* Specify 64K address range: */
1589 /* clear out Module Select and Flash Address bits [19:16]. */
1590 bank_select &= ~0xf8;
1591 bank_select |= addr >> 12 & 0xf0;
1592 bank_select |= CSR_FLASH_64K_BANK;
1593 WRT_REG_WORD(&reg->ctrl_status, bank_select);
1594 RD_REG_WORD(&reg->ctrl_status); /* PCI Posting. */
1595
1596 WRT_REG_WORD(&reg->flash_address, (uint16_t)addr);
1597 data = RD_REG_WORD(&reg->flash_data);
1598
1599 return (uint8_t)data;
1600 }
1601
1602 /* Setup bit 16 of flash address. */
1603 if ((addr & BIT_16) && ((bank_select & CSR_FLASH_64K_BANK) == 0)) {
1604 bank_select |= CSR_FLASH_64K_BANK;
1605 WRT_REG_WORD(&reg->ctrl_status, bank_select);
1606 RD_REG_WORD(&reg->ctrl_status); /* PCI Posting. */
1607 } else if (((addr & BIT_16) == 0) &&
1608 (bank_select & CSR_FLASH_64K_BANK)) {
1609 bank_select &= ~(CSR_FLASH_64K_BANK);
1610 WRT_REG_WORD(&reg->ctrl_status, bank_select);
1611 RD_REG_WORD(&reg->ctrl_status); /* PCI Posting. */
1612 }
1613
1614 /* Always perform IO mapped accesses to the FLASH registers. */
1615 if (ha->pio_address) {
1616 uint16_t data2;
1617
Andrew Vasquez948882f2008-01-31 12:33:44 -08001618 WRT_REG_WORD_PIO(PIO_REG(ha, flash_address), (uint16_t)addr);
andrew.vasquez@qlogic.com854165f2006-01-31 16:05:17 -08001619 do {
Andrew Vasquez948882f2008-01-31 12:33:44 -08001620 data = RD_REG_WORD_PIO(PIO_REG(ha, flash_data));
andrew.vasquez@qlogic.com854165f2006-01-31 16:05:17 -08001621 barrier();
1622 cpu_relax();
Andrew Vasquez948882f2008-01-31 12:33:44 -08001623 data2 = RD_REG_WORD_PIO(PIO_REG(ha, flash_data));
andrew.vasquez@qlogic.com854165f2006-01-31 16:05:17 -08001624 } while (data != data2);
1625 } else {
1626 WRT_REG_WORD(&reg->flash_address, (uint16_t)addr);
1627 data = qla2x00_debounce_register(&reg->flash_data);
1628 }
1629
1630 return (uint8_t)data;
1631}
1632
1633/**
1634 * qla2x00_write_flash_byte() - Write a byte to flash
1635 * @ha: HA context
1636 * @addr: Address in flash to write
1637 * @data: Data to write
1638 */
1639static void
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -08001640qla2x00_write_flash_byte(struct qla_hw_data *ha, uint32_t addr, uint8_t data)
andrew.vasquez@qlogic.com854165f2006-01-31 16:05:17 -08001641{
1642 uint16_t bank_select;
1643 struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
1644
1645 bank_select = RD_REG_WORD(&reg->ctrl_status);
1646 if (IS_QLA2322(ha) || IS_QLA6322(ha)) {
1647 /* Specify 64K address range: */
1648 /* clear out Module Select and Flash Address bits [19:16]. */
1649 bank_select &= ~0xf8;
1650 bank_select |= addr >> 12 & 0xf0;
1651 bank_select |= CSR_FLASH_64K_BANK;
1652 WRT_REG_WORD(&reg->ctrl_status, bank_select);
1653 RD_REG_WORD(&reg->ctrl_status); /* PCI Posting. */
1654
1655 WRT_REG_WORD(&reg->flash_address, (uint16_t)addr);
1656 RD_REG_WORD(&reg->ctrl_status); /* PCI Posting. */
1657 WRT_REG_WORD(&reg->flash_data, (uint16_t)data);
1658 RD_REG_WORD(&reg->ctrl_status); /* PCI Posting. */
1659
1660 return;
1661 }
1662
1663 /* Setup bit 16 of flash address. */
1664 if ((addr & BIT_16) && ((bank_select & CSR_FLASH_64K_BANK) == 0)) {
1665 bank_select |= CSR_FLASH_64K_BANK;
1666 WRT_REG_WORD(&reg->ctrl_status, bank_select);
1667 RD_REG_WORD(&reg->ctrl_status); /* PCI Posting. */
1668 } else if (((addr & BIT_16) == 0) &&
1669 (bank_select & CSR_FLASH_64K_BANK)) {
1670 bank_select &= ~(CSR_FLASH_64K_BANK);
1671 WRT_REG_WORD(&reg->ctrl_status, bank_select);
1672 RD_REG_WORD(&reg->ctrl_status); /* PCI Posting. */
1673 }
1674
1675 /* Always perform IO mapped accesses to the FLASH registers. */
1676 if (ha->pio_address) {
Andrew Vasquez948882f2008-01-31 12:33:44 -08001677 WRT_REG_WORD_PIO(PIO_REG(ha, flash_address), (uint16_t)addr);
1678 WRT_REG_WORD_PIO(PIO_REG(ha, flash_data), (uint16_t)data);
andrew.vasquez@qlogic.com854165f2006-01-31 16:05:17 -08001679 } else {
1680 WRT_REG_WORD(&reg->flash_address, (uint16_t)addr);
1681 RD_REG_WORD(&reg->ctrl_status); /* PCI Posting. */
1682 WRT_REG_WORD(&reg->flash_data, (uint16_t)data);
1683 RD_REG_WORD(&reg->ctrl_status); /* PCI Posting. */
1684 }
1685}
1686
1687/**
1688 * qla2x00_poll_flash() - Polls flash for completion.
1689 * @ha: HA context
1690 * @addr: Address in flash to poll
1691 * @poll_data: Data to be polled
1692 * @man_id: Flash manufacturer ID
1693 * @flash_id: Flash ID
1694 *
1695 * This function polls the device until bit 7 of what is read matches data
1696 * bit 7 or until data bit 5 becomes a 1. If that hapens, the flash ROM timed
1697 * out (a fatal error). The flash book recommeds reading bit 7 again after
1698 * reading bit 5 as a 1.
1699 *
1700 * Returns 0 on success, else non-zero.
1701 */
1702static int
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -08001703qla2x00_poll_flash(struct qla_hw_data *ha, uint32_t addr, uint8_t poll_data,
andrew.vasquez@qlogic.com854165f2006-01-31 16:05:17 -08001704 uint8_t man_id, uint8_t flash_id)
1705{
1706 int status;
1707 uint8_t flash_data;
1708 uint32_t cnt;
1709
1710 status = 1;
1711
1712 /* Wait for 30 seconds for command to finish. */
1713 poll_data &= BIT_7;
1714 for (cnt = 3000000; cnt; cnt--) {
1715 flash_data = qla2x00_read_flash_byte(ha, addr);
1716 if ((flash_data & BIT_7) == poll_data) {
1717 status = 0;
1718 break;
1719 }
1720
1721 if (man_id != 0x40 && man_id != 0xda) {
1722 if ((flash_data & BIT_5) && cnt > 2)
1723 cnt = 2;
1724 }
1725 udelay(10);
1726 barrier();
Andrew Vasquez40a2e342007-03-12 10:41:28 -07001727 cond_resched();
andrew.vasquez@qlogic.com854165f2006-01-31 16:05:17 -08001728 }
1729 return status;
1730}
1731
andrew.vasquez@qlogic.com854165f2006-01-31 16:05:17 -08001732/**
1733 * qla2x00_program_flash_address() - Programs a flash address
1734 * @ha: HA context
1735 * @addr: Address in flash to program
1736 * @data: Data to be written in flash
1737 * @man_id: Flash manufacturer ID
1738 * @flash_id: Flash ID
1739 *
1740 * Returns 0 on success, else non-zero.
1741 */
1742static int
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -08001743qla2x00_program_flash_address(struct qla_hw_data *ha, uint32_t addr,
1744 uint8_t data, uint8_t man_id, uint8_t flash_id)
andrew.vasquez@qlogic.com854165f2006-01-31 16:05:17 -08001745{
1746 /* Write Program Command Sequence. */
1747 if (IS_OEM_001(ha)) {
1748 qla2x00_write_flash_byte(ha, 0xaaa, 0xaa);
1749 qla2x00_write_flash_byte(ha, 0x555, 0x55);
1750 qla2x00_write_flash_byte(ha, 0xaaa, 0xa0);
1751 qla2x00_write_flash_byte(ha, addr, data);
1752 } else {
1753 if (man_id == 0xda && flash_id == 0xc1) {
1754 qla2x00_write_flash_byte(ha, addr, data);
1755 if (addr & 0x7e)
1756 return 0;
1757 } else {
1758 qla2x00_write_flash_byte(ha, 0x5555, 0xaa);
1759 qla2x00_write_flash_byte(ha, 0x2aaa, 0x55);
1760 qla2x00_write_flash_byte(ha, 0x5555, 0xa0);
1761 qla2x00_write_flash_byte(ha, addr, data);
1762 }
1763 }
1764
1765 udelay(150);
1766
1767 /* Wait for write to complete. */
1768 return qla2x00_poll_flash(ha, addr, data, man_id, flash_id);
1769}
1770
1771/**
1772 * qla2x00_erase_flash() - Erase the flash.
1773 * @ha: HA context
1774 * @man_id: Flash manufacturer ID
1775 * @flash_id: Flash ID
1776 *
1777 * Returns 0 on success, else non-zero.
1778 */
1779static int
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -08001780qla2x00_erase_flash(struct qla_hw_data *ha, uint8_t man_id, uint8_t flash_id)
andrew.vasquez@qlogic.com854165f2006-01-31 16:05:17 -08001781{
1782 /* Individual Sector Erase Command Sequence */
1783 if (IS_OEM_001(ha)) {
1784 qla2x00_write_flash_byte(ha, 0xaaa, 0xaa);
1785 qla2x00_write_flash_byte(ha, 0x555, 0x55);
1786 qla2x00_write_flash_byte(ha, 0xaaa, 0x80);
1787 qla2x00_write_flash_byte(ha, 0xaaa, 0xaa);
1788 qla2x00_write_flash_byte(ha, 0x555, 0x55);
1789 qla2x00_write_flash_byte(ha, 0xaaa, 0x10);
1790 } else {
1791 qla2x00_write_flash_byte(ha, 0x5555, 0xaa);
1792 qla2x00_write_flash_byte(ha, 0x2aaa, 0x55);
1793 qla2x00_write_flash_byte(ha, 0x5555, 0x80);
1794 qla2x00_write_flash_byte(ha, 0x5555, 0xaa);
1795 qla2x00_write_flash_byte(ha, 0x2aaa, 0x55);
1796 qla2x00_write_flash_byte(ha, 0x5555, 0x10);
1797 }
1798
1799 udelay(150);
1800
1801 /* Wait for erase to complete. */
1802 return qla2x00_poll_flash(ha, 0x00, 0x80, man_id, flash_id);
1803}
1804
1805/**
1806 * qla2x00_erase_flash_sector() - Erase a flash sector.
1807 * @ha: HA context
1808 * @addr: Flash sector to erase
1809 * @sec_mask: Sector address mask
1810 * @man_id: Flash manufacturer ID
1811 * @flash_id: Flash ID
1812 *
1813 * Returns 0 on success, else non-zero.
1814 */
1815static int
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -08001816qla2x00_erase_flash_sector(struct qla_hw_data *ha, uint32_t addr,
andrew.vasquez@qlogic.com854165f2006-01-31 16:05:17 -08001817 uint32_t sec_mask, uint8_t man_id, uint8_t flash_id)
1818{
1819 /* Individual Sector Erase Command Sequence */
1820 qla2x00_write_flash_byte(ha, 0x5555, 0xaa);
1821 qla2x00_write_flash_byte(ha, 0x2aaa, 0x55);
1822 qla2x00_write_flash_byte(ha, 0x5555, 0x80);
1823 qla2x00_write_flash_byte(ha, 0x5555, 0xaa);
1824 qla2x00_write_flash_byte(ha, 0x2aaa, 0x55);
1825 if (man_id == 0x1f && flash_id == 0x13)
1826 qla2x00_write_flash_byte(ha, addr & sec_mask, 0x10);
1827 else
1828 qla2x00_write_flash_byte(ha, addr & sec_mask, 0x30);
1829
1830 udelay(150);
1831
1832 /* Wait for erase to complete. */
1833 return qla2x00_poll_flash(ha, addr, 0x80, man_id, flash_id);
1834}
1835
1836/**
1837 * qla2x00_get_flash_manufacturer() - Read manufacturer ID from flash chip.
1838 * @man_id: Flash manufacturer ID
1839 * @flash_id: Flash ID
1840 */
1841static void
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -08001842qla2x00_get_flash_manufacturer(struct qla_hw_data *ha, uint8_t *man_id,
andrew.vasquez@qlogic.com854165f2006-01-31 16:05:17 -08001843 uint8_t *flash_id)
1844{
1845 qla2x00_write_flash_byte(ha, 0x5555, 0xaa);
1846 qla2x00_write_flash_byte(ha, 0x2aaa, 0x55);
1847 qla2x00_write_flash_byte(ha, 0x5555, 0x90);
1848 *man_id = qla2x00_read_flash_byte(ha, 0x0000);
1849 *flash_id = qla2x00_read_flash_byte(ha, 0x0001);
1850 qla2x00_write_flash_byte(ha, 0x5555, 0xaa);
1851 qla2x00_write_flash_byte(ha, 0x2aaa, 0x55);
1852 qla2x00_write_flash_byte(ha, 0x5555, 0xf0);
1853}
1854
Andrew Vasquez30c47662007-01-29 10:22:21 -08001855static void
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -08001856qla2x00_read_flash_data(struct qla_hw_data *ha, uint8_t *tmp_buf,
1857 uint32_t saddr, uint32_t length)
Andrew Vasquez30c47662007-01-29 10:22:21 -08001858{
1859 struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
1860 uint32_t midpoint, ilength;
1861 uint8_t data;
1862
1863 midpoint = length / 2;
1864
1865 WRT_REG_WORD(&reg->nvram, 0);
1866 RD_REG_WORD(&reg->nvram);
1867 for (ilength = 0; ilength < length; saddr++, ilength++, tmp_buf++) {
1868 if (ilength == midpoint) {
1869 WRT_REG_WORD(&reg->nvram, NVR_SELECT);
1870 RD_REG_WORD(&reg->nvram);
1871 }
1872 data = qla2x00_read_flash_byte(ha, saddr);
1873 if (saddr % 100)
1874 udelay(10);
1875 *tmp_buf = data;
Andrew Vasquez40a2e342007-03-12 10:41:28 -07001876 cond_resched();
Andrew Vasquez30c47662007-01-29 10:22:21 -08001877 }
1878}
andrew.vasquez@qlogic.com854165f2006-01-31 16:05:17 -08001879
1880static inline void
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -08001881qla2x00_suspend_hba(struct scsi_qla_host *vha)
andrew.vasquez@qlogic.com854165f2006-01-31 16:05:17 -08001882{
1883 int cnt;
1884 unsigned long flags;
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -08001885 struct qla_hw_data *ha = vha->hw;
andrew.vasquez@qlogic.com854165f2006-01-31 16:05:17 -08001886 struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
1887
1888 /* Suspend HBA. */
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -08001889 scsi_block_requests(vha->host);
Andrew Vasquezfd34f552007-07-19 15:06:00 -07001890 ha->isp_ops->disable_intrs(ha);
andrew.vasquez@qlogic.com854165f2006-01-31 16:05:17 -08001891 set_bit(MBX_UPDATE_FLASH_ACTIVE, &ha->mbx_cmd_flags);
1892
1893 /* Pause RISC. */
1894 spin_lock_irqsave(&ha->hardware_lock, flags);
1895 WRT_REG_WORD(&reg->hccr, HCCR_PAUSE_RISC);
1896 RD_REG_WORD(&reg->hccr);
1897 if (IS_QLA2100(ha) || IS_QLA2200(ha) || IS_QLA2300(ha)) {
1898 for (cnt = 0; cnt < 30000; cnt++) {
1899 if ((RD_REG_WORD(&reg->hccr) & HCCR_RISC_PAUSE) != 0)
1900 break;
1901 udelay(100);
1902 }
1903 } else {
1904 udelay(10);
1905 }
1906 spin_unlock_irqrestore(&ha->hardware_lock, flags);
1907}
1908
1909static inline void
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -08001910qla2x00_resume_hba(struct scsi_qla_host *vha)
andrew.vasquez@qlogic.com854165f2006-01-31 16:05:17 -08001911{
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -08001912 struct qla_hw_data *ha = vha->hw;
1913
andrew.vasquez@qlogic.com854165f2006-01-31 16:05:17 -08001914 /* Resume HBA. */
1915 clear_bit(MBX_UPDATE_FLASH_ACTIVE, &ha->mbx_cmd_flags);
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -08001916 set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
1917 qla2xxx_wake_dpc(vha);
1918 qla2x00_wait_for_hba_online(vha);
1919 scsi_unblock_requests(vha->host);
andrew.vasquez@qlogic.com854165f2006-01-31 16:05:17 -08001920}
1921
1922uint8_t *
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -08001923qla2x00_read_optrom_data(struct scsi_qla_host *vha, uint8_t *buf,
andrew.vasquez@qlogic.com854165f2006-01-31 16:05:17 -08001924 uint32_t offset, uint32_t length)
1925{
andrew.vasquez@qlogic.com854165f2006-01-31 16:05:17 -08001926 uint32_t addr, midpoint;
1927 uint8_t *data;
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -08001928 struct qla_hw_data *ha = vha->hw;
andrew.vasquez@qlogic.com854165f2006-01-31 16:05:17 -08001929 struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
1930
1931 /* Suspend HBA. */
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -08001932 qla2x00_suspend_hba(vha);
andrew.vasquez@qlogic.com854165f2006-01-31 16:05:17 -08001933
1934 /* Go with read. */
andrew.vasquez@qlogic.com854165f2006-01-31 16:05:17 -08001935 midpoint = ha->optrom_size / 2;
1936
1937 qla2x00_flash_enable(ha);
1938 WRT_REG_WORD(&reg->nvram, 0);
1939 RD_REG_WORD(&reg->nvram); /* PCI Posting. */
1940 for (addr = offset, data = buf; addr < length; addr++, data++) {
1941 if (addr == midpoint) {
1942 WRT_REG_WORD(&reg->nvram, NVR_SELECT);
1943 RD_REG_WORD(&reg->nvram); /* PCI Posting. */
1944 }
1945
1946 *data = qla2x00_read_flash_byte(ha, addr);
1947 }
1948 qla2x00_flash_disable(ha);
andrew.vasquez@qlogic.com854165f2006-01-31 16:05:17 -08001949
1950 /* Resume HBA. */
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -08001951 qla2x00_resume_hba(vha);
andrew.vasquez@qlogic.com854165f2006-01-31 16:05:17 -08001952
1953 return buf;
1954}
1955
1956int
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -08001957qla2x00_write_optrom_data(struct scsi_qla_host *vha, uint8_t *buf,
andrew.vasquez@qlogic.com854165f2006-01-31 16:05:17 -08001958 uint32_t offset, uint32_t length)
1959{
1960
1961 int rval;
andrew.vasquez@qlogic.com854165f2006-01-31 16:05:17 -08001962 uint8_t man_id, flash_id, sec_number, data;
1963 uint16_t wd;
1964 uint32_t addr, liter, sec_mask, rest_addr;
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -08001965 struct qla_hw_data *ha = vha->hw;
andrew.vasquez@qlogic.com854165f2006-01-31 16:05:17 -08001966 struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
1967
1968 /* Suspend HBA. */
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -08001969 qla2x00_suspend_hba(vha);
andrew.vasquez@qlogic.com854165f2006-01-31 16:05:17 -08001970
1971 rval = QLA_SUCCESS;
1972 sec_number = 0;
1973
1974 /* Reset ISP chip. */
andrew.vasquez@qlogic.com854165f2006-01-31 16:05:17 -08001975 WRT_REG_WORD(&reg->ctrl_status, CSR_ISP_SOFT_RESET);
1976 pci_read_config_word(ha->pdev, PCI_COMMAND, &wd);
1977
1978 /* Go with write. */
1979 qla2x00_flash_enable(ha);
1980 do { /* Loop once to provide quick error exit */
1981 /* Structure of flash memory based on manufacturer */
1982 if (IS_OEM_001(ha)) {
1983 /* OEM variant with special flash part. */
1984 man_id = flash_id = 0;
1985 rest_addr = 0xffff;
1986 sec_mask = 0x10000;
1987 goto update_flash;
1988 }
1989 qla2x00_get_flash_manufacturer(ha, &man_id, &flash_id);
1990 switch (man_id) {
1991 case 0x20: /* ST flash. */
1992 if (flash_id == 0xd2 || flash_id == 0xe3) {
1993 /*
1994 * ST m29w008at part - 64kb sector size with
1995 * 32kb,8kb,8kb,16kb sectors at memory address
1996 * 0xf0000.
1997 */
1998 rest_addr = 0xffff;
1999 sec_mask = 0x10000;
2000 break;
2001 }
2002 /*
2003 * ST m29w010b part - 16kb sector size
2004 * Default to 16kb sectors
2005 */
2006 rest_addr = 0x3fff;
2007 sec_mask = 0x1c000;
2008 break;
2009 case 0x40: /* Mostel flash. */
2010 /* Mostel v29c51001 part - 512 byte sector size. */
2011 rest_addr = 0x1ff;
2012 sec_mask = 0x1fe00;
2013 break;
2014 case 0xbf: /* SST flash. */
2015 /* SST39sf10 part - 4kb sector size. */
2016 rest_addr = 0xfff;
2017 sec_mask = 0x1f000;
2018 break;
2019 case 0xda: /* Winbond flash. */
2020 /* Winbond W29EE011 part - 256 byte sector size. */
2021 rest_addr = 0x7f;
2022 sec_mask = 0x1ff80;
2023 break;
2024 case 0xc2: /* Macronix flash. */
2025 /* 64k sector size. */
2026 if (flash_id == 0x38 || flash_id == 0x4f) {
2027 rest_addr = 0xffff;
2028 sec_mask = 0x10000;
2029 break;
2030 }
2031 /* Fall through... */
2032
2033 case 0x1f: /* Atmel flash. */
2034 /* 512k sector size. */
2035 if (flash_id == 0x13) {
2036 rest_addr = 0x7fffffff;
2037 sec_mask = 0x80000000;
2038 break;
2039 }
2040 /* Fall through... */
2041
2042 case 0x01: /* AMD flash. */
2043 if (flash_id == 0x38 || flash_id == 0x40 ||
2044 flash_id == 0x4f) {
2045 /* Am29LV081 part - 64kb sector size. */
2046 /* Am29LV002BT part - 64kb sector size. */
2047 rest_addr = 0xffff;
2048 sec_mask = 0x10000;
2049 break;
2050 } else if (flash_id == 0x3e) {
2051 /*
2052 * Am29LV008b part - 64kb sector size with
2053 * 32kb,8kb,8kb,16kb sector at memory address
2054 * h0xf0000.
2055 */
2056 rest_addr = 0xffff;
2057 sec_mask = 0x10000;
2058 break;
2059 } else if (flash_id == 0x20 || flash_id == 0x6e) {
2060 /*
2061 * Am29LV010 part or AM29f010 - 16kb sector
2062 * size.
2063 */
2064 rest_addr = 0x3fff;
2065 sec_mask = 0x1c000;
2066 break;
2067 } else if (flash_id == 0x6d) {
2068 /* Am29LV001 part - 8kb sector size. */
2069 rest_addr = 0x1fff;
2070 sec_mask = 0x1e000;
2071 break;
2072 }
2073 default:
2074 /* Default to 16 kb sector size. */
2075 rest_addr = 0x3fff;
2076 sec_mask = 0x1c000;
2077 break;
2078 }
2079
2080update_flash:
2081 if (IS_QLA2322(ha) || IS_QLA6322(ha)) {
2082 if (qla2x00_erase_flash(ha, man_id, flash_id)) {
2083 rval = QLA_FUNCTION_FAILED;
2084 break;
2085 }
2086 }
2087
2088 for (addr = offset, liter = 0; liter < length; liter++,
2089 addr++) {
2090 data = buf[liter];
2091 /* Are we at the beginning of a sector? */
2092 if ((addr & rest_addr) == 0) {
2093 if (IS_QLA2322(ha) || IS_QLA6322(ha)) {
2094 if (addr >= 0x10000UL) {
2095 if (((addr >> 12) & 0xf0) &&
2096 ((man_id == 0x01 &&
2097 flash_id == 0x3e) ||
2098 (man_id == 0x20 &&
2099 flash_id == 0xd2))) {
2100 sec_number++;
2101 if (sec_number == 1) {
2102 rest_addr =
2103 0x7fff;
2104 sec_mask =
2105 0x18000;
2106 } else if (
2107 sec_number == 2 ||
2108 sec_number == 3) {
2109 rest_addr =
2110 0x1fff;
2111 sec_mask =
2112 0x1e000;
2113 } else if (
2114 sec_number == 4) {
2115 rest_addr =
2116 0x3fff;
2117 sec_mask =
2118 0x1c000;
2119 }
2120 }
2121 }
2122 } else if (addr == ha->optrom_size / 2) {
2123 WRT_REG_WORD(&reg->nvram, NVR_SELECT);
2124 RD_REG_WORD(&reg->nvram);
2125 }
2126
2127 if (flash_id == 0xda && man_id == 0xc1) {
2128 qla2x00_write_flash_byte(ha, 0x5555,
2129 0xaa);
2130 qla2x00_write_flash_byte(ha, 0x2aaa,
2131 0x55);
2132 qla2x00_write_flash_byte(ha, 0x5555,
2133 0xa0);
2134 } else if (!IS_QLA2322(ha) && !IS_QLA6322(ha)) {
2135 /* Then erase it */
2136 if (qla2x00_erase_flash_sector(ha,
2137 addr, sec_mask, man_id,
2138 flash_id)) {
2139 rval = QLA_FUNCTION_FAILED;
2140 break;
2141 }
2142 if (man_id == 0x01 && flash_id == 0x6d)
2143 sec_number++;
2144 }
2145 }
2146
2147 if (man_id == 0x01 && flash_id == 0x6d) {
2148 if (sec_number == 1 &&
2149 addr == (rest_addr - 1)) {
2150 rest_addr = 0x0fff;
2151 sec_mask = 0x1f000;
2152 } else if (sec_number == 3 && (addr & 0x7ffe)) {
2153 rest_addr = 0x3fff;
2154 sec_mask = 0x1c000;
2155 }
2156 }
2157
2158 if (qla2x00_program_flash_address(ha, addr, data,
2159 man_id, flash_id)) {
2160 rval = QLA_FUNCTION_FAILED;
2161 break;
2162 }
Andrew Vasquez40a2e342007-03-12 10:41:28 -07002163 cond_resched();
andrew.vasquez@qlogic.com854165f2006-01-31 16:05:17 -08002164 }
2165 } while (0);
2166 qla2x00_flash_disable(ha);
andrew.vasquez@qlogic.com854165f2006-01-31 16:05:17 -08002167
2168 /* Resume HBA. */
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -08002169 qla2x00_resume_hba(vha);
andrew.vasquez@qlogic.com854165f2006-01-31 16:05:17 -08002170
2171 return rval;
2172}
2173
2174uint8_t *
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -08002175qla24xx_read_optrom_data(struct scsi_qla_host *vha, uint8_t *buf,
andrew.vasquez@qlogic.com854165f2006-01-31 16:05:17 -08002176 uint32_t offset, uint32_t length)
2177{
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -08002178 struct qla_hw_data *ha = vha->hw;
2179
andrew.vasquez@qlogic.com854165f2006-01-31 16:05:17 -08002180 /* Suspend HBA. */
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -08002181 scsi_block_requests(vha->host);
andrew.vasquez@qlogic.com854165f2006-01-31 16:05:17 -08002182 set_bit(MBX_UPDATE_FLASH_ACTIVE, &ha->mbx_cmd_flags);
2183
2184 /* Go with read. */
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -08002185 qla24xx_read_flash_data(vha, (uint32_t *)buf, offset >> 2, length >> 2);
andrew.vasquez@qlogic.com854165f2006-01-31 16:05:17 -08002186
2187 /* Resume HBA. */
2188 clear_bit(MBX_UPDATE_FLASH_ACTIVE, &ha->mbx_cmd_flags);
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -08002189 scsi_unblock_requests(vha->host);
andrew.vasquez@qlogic.com854165f2006-01-31 16:05:17 -08002190
2191 return buf;
2192}
2193
2194int
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -08002195qla24xx_write_optrom_data(struct scsi_qla_host *vha, uint8_t *buf,
andrew.vasquez@qlogic.com854165f2006-01-31 16:05:17 -08002196 uint32_t offset, uint32_t length)
2197{
2198 int rval;
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -08002199 struct qla_hw_data *ha = vha->hw;
andrew.vasquez@qlogic.com854165f2006-01-31 16:05:17 -08002200
2201 /* Suspend HBA. */
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -08002202 scsi_block_requests(vha->host);
andrew.vasquez@qlogic.com854165f2006-01-31 16:05:17 -08002203 set_bit(MBX_UPDATE_FLASH_ACTIVE, &ha->mbx_cmd_flags);
2204
2205 /* Go with write. */
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -08002206 rval = qla24xx_write_flash_data(vha, (uint32_t *)buf, offset >> 2,
andrew.vasquez@qlogic.com854165f2006-01-31 16:05:17 -08002207 length >> 2);
2208
2209 /* Resume HBA -- RISC reset needed. */
2210 clear_bit(MBX_UPDATE_FLASH_ACTIVE, &ha->mbx_cmd_flags);
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -08002211 set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
2212 qla2xxx_wake_dpc(vha);
2213 qla2x00_wait_for_hba_online(vha);
2214 scsi_unblock_requests(vha->host);
andrew.vasquez@qlogic.com854165f2006-01-31 16:05:17 -08002215
2216 return rval;
2217}
Andrew Vasquez30c47662007-01-29 10:22:21 -08002218
Andrew Vasquez338c9162007-09-20 14:07:33 -07002219uint8_t *
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -08002220qla25xx_read_optrom_data(struct scsi_qla_host *vha, uint8_t *buf,
Andrew Vasquez338c9162007-09-20 14:07:33 -07002221 uint32_t offset, uint32_t length)
2222{
2223 int rval;
2224 dma_addr_t optrom_dma;
2225 void *optrom;
2226 uint8_t *pbuf;
2227 uint32_t faddr, left, burst;
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -08002228 struct qla_hw_data *ha = vha->hw;
Andrew Vasquez338c9162007-09-20 14:07:33 -07002229
Joe Carnucciob7cc1762007-09-20 14:07:35 -07002230 if (offset & 0xfff)
Andrew Vasquez338c9162007-09-20 14:07:33 -07002231 goto slow_read;
2232 if (length < OPTROM_BURST_SIZE)
2233 goto slow_read;
2234
2235 optrom = dma_alloc_coherent(&ha->pdev->dev, OPTROM_BURST_SIZE,
2236 &optrom_dma, GFP_KERNEL);
2237 if (!optrom) {
2238 qla_printk(KERN_DEBUG, ha,
2239 "Unable to allocate memory for optrom burst read "
2240 "(%x KB).\n", OPTROM_BURST_SIZE / 1024);
2241
2242 goto slow_read;
2243 }
2244
2245 pbuf = buf;
2246 faddr = offset >> 2;
2247 left = length >> 2;
2248 burst = OPTROM_BURST_DWORDS;
2249 while (left != 0) {
2250 if (burst > left)
2251 burst = left;
2252
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -08002253 rval = qla2x00_dump_ram(vha, optrom_dma,
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08002254 flash_data_addr(ha, faddr), burst);
Andrew Vasquez338c9162007-09-20 14:07:33 -07002255 if (rval) {
2256 qla_printk(KERN_WARNING, ha,
2257 "Unable to burst-read optrom segment "
2258 "(%x/%x/%llx).\n", rval,
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08002259 flash_data_addr(ha, faddr),
Andrew Morton875baf32007-10-16 14:28:20 -07002260 (unsigned long long)optrom_dma);
Andrew Vasquez338c9162007-09-20 14:07:33 -07002261 qla_printk(KERN_WARNING, ha,
2262 "Reverting to slow-read.\n");
2263
2264 dma_free_coherent(&ha->pdev->dev, OPTROM_BURST_SIZE,
2265 optrom, optrom_dma);
2266 goto slow_read;
2267 }
2268
2269 memcpy(pbuf, optrom, burst * 4);
2270
2271 left -= burst;
2272 faddr += burst;
2273 pbuf += burst * 4;
2274 }
2275
2276 dma_free_coherent(&ha->pdev->dev, OPTROM_BURST_SIZE, optrom,
2277 optrom_dma);
2278
2279 return buf;
2280
2281slow_read:
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -08002282 return qla24xx_read_optrom_data(vha, buf, offset, length);
Andrew Vasquez338c9162007-09-20 14:07:33 -07002283}
2284
Andrew Vasquez30c47662007-01-29 10:22:21 -08002285/**
2286 * qla2x00_get_fcode_version() - Determine an FCODE image's version.
2287 * @ha: HA context
2288 * @pcids: Pointer to the FCODE PCI data structure
2289 *
2290 * The process of retrieving the FCODE version information is at best
2291 * described as interesting.
2292 *
2293 * Within the first 100h bytes of the image an ASCII string is present
2294 * which contains several pieces of information including the FCODE
2295 * version. Unfortunately it seems the only reliable way to retrieve
2296 * the version is by scanning for another sentinel within the string,
2297 * the FCODE build date:
2298 *
2299 * ... 2.00.02 10/17/02 ...
2300 *
2301 * Returns QLA_SUCCESS on successful retrieval of version.
2302 */
2303static void
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -08002304qla2x00_get_fcode_version(struct qla_hw_data *ha, uint32_t pcids)
Andrew Vasquez30c47662007-01-29 10:22:21 -08002305{
2306 int ret = QLA_FUNCTION_FAILED;
2307 uint32_t istart, iend, iter, vend;
2308 uint8_t do_next, rbyte, *vbyte;
2309
2310 memset(ha->fcode_revision, 0, sizeof(ha->fcode_revision));
2311
2312 /* Skip the PCI data structure. */
2313 istart = pcids +
2314 ((qla2x00_read_flash_byte(ha, pcids + 0x0B) << 8) |
2315 qla2x00_read_flash_byte(ha, pcids + 0x0A));
2316 iend = istart + 0x100;
2317 do {
2318 /* Scan for the sentinel date string...eeewww. */
2319 do_next = 0;
2320 iter = istart;
2321 while ((iter < iend) && !do_next) {
2322 iter++;
2323 if (qla2x00_read_flash_byte(ha, iter) == '/') {
2324 if (qla2x00_read_flash_byte(ha, iter + 2) ==
2325 '/')
2326 do_next++;
2327 else if (qla2x00_read_flash_byte(ha,
2328 iter + 3) == '/')
2329 do_next++;
2330 }
2331 }
2332 if (!do_next)
2333 break;
2334
2335 /* Backtrack to previous ' ' (space). */
2336 do_next = 0;
2337 while ((iter > istart) && !do_next) {
2338 iter--;
2339 if (qla2x00_read_flash_byte(ha, iter) == ' ')
2340 do_next++;
2341 }
2342 if (!do_next)
2343 break;
2344
2345 /*
2346 * Mark end of version tag, and find previous ' ' (space) or
2347 * string length (recent FCODE images -- major hack ahead!!!).
2348 */
2349 vend = iter - 1;
2350 do_next = 0;
2351 while ((iter > istart) && !do_next) {
2352 iter--;
2353 rbyte = qla2x00_read_flash_byte(ha, iter);
2354 if (rbyte == ' ' || rbyte == 0xd || rbyte == 0x10)
2355 do_next++;
2356 }
2357 if (!do_next)
2358 break;
2359
2360 /* Mark beginning of version tag, and copy data. */
2361 iter++;
2362 if ((vend - iter) &&
2363 ((vend - iter) < sizeof(ha->fcode_revision))) {
2364 vbyte = ha->fcode_revision;
2365 while (iter <= vend) {
2366 *vbyte++ = qla2x00_read_flash_byte(ha, iter);
2367 iter++;
2368 }
2369 ret = QLA_SUCCESS;
2370 }
2371 } while (0);
2372
2373 if (ret != QLA_SUCCESS)
2374 memset(ha->fcode_revision, 0, sizeof(ha->fcode_revision));
2375}
2376
2377int
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -08002378qla2x00_get_flash_version(scsi_qla_host_t *vha, void *mbuf)
Andrew Vasquez30c47662007-01-29 10:22:21 -08002379{
2380 int ret = QLA_SUCCESS;
2381 uint8_t code_type, last_image;
2382 uint32_t pcihdr, pcids;
2383 uint8_t *dbyte;
2384 uint16_t *dcode;
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -08002385 struct qla_hw_data *ha = vha->hw;
Andrew Vasquez30c47662007-01-29 10:22:21 -08002386
2387 if (!ha->pio_address || !mbuf)
2388 return QLA_FUNCTION_FAILED;
2389
2390 memset(ha->bios_revision, 0, sizeof(ha->bios_revision));
2391 memset(ha->efi_revision, 0, sizeof(ha->efi_revision));
2392 memset(ha->fcode_revision, 0, sizeof(ha->fcode_revision));
2393 memset(ha->fw_revision, 0, sizeof(ha->fw_revision));
2394
2395 qla2x00_flash_enable(ha);
2396
2397 /* Begin with first PCI expansion ROM header. */
2398 pcihdr = 0;
2399 last_image = 1;
2400 do {
2401 /* Verify PCI expansion ROM header. */
2402 if (qla2x00_read_flash_byte(ha, pcihdr) != 0x55 ||
2403 qla2x00_read_flash_byte(ha, pcihdr + 0x01) != 0xaa) {
2404 /* No signature */
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -08002405 DEBUG2(qla_printk(KERN_DEBUG, ha, "No matching ROM "
2406 "signature.\n"));
Andrew Vasquez30c47662007-01-29 10:22:21 -08002407 ret = QLA_FUNCTION_FAILED;
2408 break;
2409 }
2410
2411 /* Locate PCI data structure. */
2412 pcids = pcihdr +
2413 ((qla2x00_read_flash_byte(ha, pcihdr + 0x19) << 8) |
2414 qla2x00_read_flash_byte(ha, pcihdr + 0x18));
2415
2416 /* Validate signature of PCI data structure. */
2417 if (qla2x00_read_flash_byte(ha, pcids) != 'P' ||
2418 qla2x00_read_flash_byte(ha, pcids + 0x1) != 'C' ||
2419 qla2x00_read_flash_byte(ha, pcids + 0x2) != 'I' ||
2420 qla2x00_read_flash_byte(ha, pcids + 0x3) != 'R') {
2421 /* Incorrect header. */
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -08002422 DEBUG2(qla_printk(KERN_INFO, ha, "PCI data struct not "
2423 "found pcir_adr=%x.\n", pcids));
Andrew Vasquez30c47662007-01-29 10:22:21 -08002424 ret = QLA_FUNCTION_FAILED;
2425 break;
2426 }
2427
2428 /* Read version */
2429 code_type = qla2x00_read_flash_byte(ha, pcids + 0x14);
2430 switch (code_type) {
2431 case ROM_CODE_TYPE_BIOS:
2432 /* Intel x86, PC-AT compatible. */
2433 ha->bios_revision[0] =
2434 qla2x00_read_flash_byte(ha, pcids + 0x12);
2435 ha->bios_revision[1] =
2436 qla2x00_read_flash_byte(ha, pcids + 0x13);
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -08002437 DEBUG3(qla_printk(KERN_DEBUG, ha, "read BIOS %d.%d.\n",
Andrew Vasquez30c47662007-01-29 10:22:21 -08002438 ha->bios_revision[1], ha->bios_revision[0]));
2439 break;
2440 case ROM_CODE_TYPE_FCODE:
2441 /* Open Firmware standard for PCI (FCode). */
2442 /* Eeeewww... */
2443 qla2x00_get_fcode_version(ha, pcids);
2444 break;
2445 case ROM_CODE_TYPE_EFI:
2446 /* Extensible Firmware Interface (EFI). */
2447 ha->efi_revision[0] =
2448 qla2x00_read_flash_byte(ha, pcids + 0x12);
2449 ha->efi_revision[1] =
2450 qla2x00_read_flash_byte(ha, pcids + 0x13);
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -08002451 DEBUG3(qla_printk(KERN_DEBUG, ha, "read EFI %d.%d.\n",
Andrew Vasquez30c47662007-01-29 10:22:21 -08002452 ha->efi_revision[1], ha->efi_revision[0]));
2453 break;
2454 default:
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -08002455 DEBUG2(qla_printk(KERN_INFO, ha, "Unrecognized code "
2456 "type %x at pcids %x.\n", code_type, pcids));
Andrew Vasquez30c47662007-01-29 10:22:21 -08002457 break;
2458 }
2459
2460 last_image = qla2x00_read_flash_byte(ha, pcids + 0x15) & BIT_7;
2461
2462 /* Locate next PCI expansion ROM. */
2463 pcihdr += ((qla2x00_read_flash_byte(ha, pcids + 0x11) << 8) |
2464 qla2x00_read_flash_byte(ha, pcids + 0x10)) * 512;
2465 } while (!last_image);
2466
2467 if (IS_QLA2322(ha)) {
2468 /* Read firmware image information. */
2469 memset(ha->fw_revision, 0, sizeof(ha->fw_revision));
2470 dbyte = mbuf;
2471 memset(dbyte, 0, 8);
2472 dcode = (uint16_t *)dbyte;
2473
Andrew Vasquezc00d8992008-09-11 21:22:49 -07002474 qla2x00_read_flash_data(ha, dbyte, ha->flt_region_fw * 4 + 10,
Andrew Vasquez30c47662007-01-29 10:22:21 -08002475 8);
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -08002476 DEBUG3(qla_printk(KERN_DEBUG, ha, "dumping fw ver from "
2477 "flash:\n"));
Andrew Vasquez30c47662007-01-29 10:22:21 -08002478 DEBUG3(qla2x00_dump_buffer((uint8_t *)dbyte, 8));
2479
2480 if ((dcode[0] == 0xffff && dcode[1] == 0xffff &&
2481 dcode[2] == 0xffff && dcode[3] == 0xffff) ||
2482 (dcode[0] == 0 && dcode[1] == 0 && dcode[2] == 0 &&
2483 dcode[3] == 0)) {
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -08002484 DEBUG2(qla_printk(KERN_INFO, ha, "Unrecognized fw "
2485 "revision at %x.\n", ha->flt_region_fw * 4));
Andrew Vasquez30c47662007-01-29 10:22:21 -08002486 } else {
2487 /* values are in big endian */
2488 ha->fw_revision[0] = dbyte[0] << 16 | dbyte[1];
2489 ha->fw_revision[1] = dbyte[2] << 16 | dbyte[3];
2490 ha->fw_revision[2] = dbyte[4] << 16 | dbyte[5];
2491 }
2492 }
2493
2494 qla2x00_flash_disable(ha);
2495
2496 return ret;
2497}
2498
2499int
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -08002500qla24xx_get_flash_version(scsi_qla_host_t *vha, void *mbuf)
Andrew Vasquez30c47662007-01-29 10:22:21 -08002501{
2502 int ret = QLA_SUCCESS;
2503 uint32_t pcihdr, pcids;
2504 uint32_t *dcode;
2505 uint8_t *bcode;
2506 uint8_t code_type, last_image;
2507 int i;
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -08002508 struct qla_hw_data *ha = vha->hw;
Andrew Vasquez30c47662007-01-29 10:22:21 -08002509
2510 if (!mbuf)
2511 return QLA_FUNCTION_FAILED;
2512
2513 memset(ha->bios_revision, 0, sizeof(ha->bios_revision));
2514 memset(ha->efi_revision, 0, sizeof(ha->efi_revision));
2515 memset(ha->fcode_revision, 0, sizeof(ha->fcode_revision));
2516 memset(ha->fw_revision, 0, sizeof(ha->fw_revision));
2517
2518 dcode = mbuf;
2519
2520 /* Begin with first PCI expansion ROM header. */
Harish Zunjarrao6315a5f2009-03-24 09:07:59 -07002521 pcihdr = ha->flt_region_boot << 2;
Andrew Vasquez30c47662007-01-29 10:22:21 -08002522 last_image = 1;
2523 do {
2524 /* Verify PCI expansion ROM header. */
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -08002525 qla24xx_read_flash_data(vha, dcode, pcihdr >> 2, 0x20);
Andrew Vasquez30c47662007-01-29 10:22:21 -08002526 bcode = mbuf + (pcihdr % 4);
2527 if (bcode[0x0] != 0x55 || bcode[0x1] != 0xaa) {
2528 /* No signature */
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -08002529 DEBUG2(qla_printk(KERN_DEBUG, ha, "No matching ROM "
2530 "signature.\n"));
Andrew Vasquez30c47662007-01-29 10:22:21 -08002531 ret = QLA_FUNCTION_FAILED;
2532 break;
2533 }
2534
2535 /* Locate PCI data structure. */
2536 pcids = pcihdr + ((bcode[0x19] << 8) | bcode[0x18]);
2537
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -08002538 qla24xx_read_flash_data(vha, dcode, pcids >> 2, 0x20);
Andrew Vasquez30c47662007-01-29 10:22:21 -08002539 bcode = mbuf + (pcihdr % 4);
2540
2541 /* Validate signature of PCI data structure. */
2542 if (bcode[0x0] != 'P' || bcode[0x1] != 'C' ||
2543 bcode[0x2] != 'I' || bcode[0x3] != 'R') {
2544 /* Incorrect header. */
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -08002545 DEBUG2(qla_printk(KERN_INFO, ha, "PCI data struct not "
2546 "found pcir_adr=%x.\n", pcids));
Andrew Vasquez30c47662007-01-29 10:22:21 -08002547 ret = QLA_FUNCTION_FAILED;
2548 break;
2549 }
2550
2551 /* Read version */
2552 code_type = bcode[0x14];
2553 switch (code_type) {
2554 case ROM_CODE_TYPE_BIOS:
2555 /* Intel x86, PC-AT compatible. */
2556 ha->bios_revision[0] = bcode[0x12];
2557 ha->bios_revision[1] = bcode[0x13];
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -08002558 DEBUG3(qla_printk(KERN_DEBUG, ha, "read BIOS %d.%d.\n",
Andrew Vasquez30c47662007-01-29 10:22:21 -08002559 ha->bios_revision[1], ha->bios_revision[0]));
2560 break;
2561 case ROM_CODE_TYPE_FCODE:
2562 /* Open Firmware standard for PCI (FCode). */
2563 ha->fcode_revision[0] = bcode[0x12];
2564 ha->fcode_revision[1] = bcode[0x13];
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -08002565 DEBUG3(qla_printk(KERN_DEBUG, ha, "read FCODE %d.%d.\n",
Andrew Vasquez30c47662007-01-29 10:22:21 -08002566 ha->fcode_revision[1], ha->fcode_revision[0]));
2567 break;
2568 case ROM_CODE_TYPE_EFI:
2569 /* Extensible Firmware Interface (EFI). */
2570 ha->efi_revision[0] = bcode[0x12];
2571 ha->efi_revision[1] = bcode[0x13];
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -08002572 DEBUG3(qla_printk(KERN_DEBUG, ha, "read EFI %d.%d.\n",
Andrew Vasquez30c47662007-01-29 10:22:21 -08002573 ha->efi_revision[1], ha->efi_revision[0]));
2574 break;
2575 default:
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -08002576 DEBUG2(qla_printk(KERN_INFO, ha, "Unrecognized code "
2577 "type %x at pcids %x.\n", code_type, pcids));
Andrew Vasquez30c47662007-01-29 10:22:21 -08002578 break;
2579 }
2580
2581 last_image = bcode[0x15] & BIT_7;
2582
2583 /* Locate next PCI expansion ROM. */
2584 pcihdr += ((bcode[0x11] << 8) | bcode[0x10]) * 512;
2585 } while (!last_image);
2586
2587 /* Read firmware image information. */
2588 memset(ha->fw_revision, 0, sizeof(ha->fw_revision));
2589 dcode = mbuf;
2590
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -08002591 qla24xx_read_flash_data(vha, dcode, ha->flt_region_fw + 4, 4);
Andrew Vasquez30c47662007-01-29 10:22:21 -08002592 for (i = 0; i < 4; i++)
2593 dcode[i] = be32_to_cpu(dcode[i]);
2594
2595 if ((dcode[0] == 0xffffffff && dcode[1] == 0xffffffff &&
2596 dcode[2] == 0xffffffff && dcode[3] == 0xffffffff) ||
2597 (dcode[0] == 0 && dcode[1] == 0 && dcode[2] == 0 &&
2598 dcode[3] == 0)) {
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -08002599 DEBUG2(qla_printk(KERN_INFO, ha, "Unrecognized fw "
2600 "revision at %x.\n", ha->flt_region_fw * 4));
Andrew Vasquez30c47662007-01-29 10:22:21 -08002601 } else {
2602 ha->fw_revision[0] = dcode[0];
2603 ha->fw_revision[1] = dcode[1];
2604 ha->fw_revision[2] = dcode[2];
2605 ha->fw_revision[3] = dcode[3];
2606 }
2607
2608 return ret;
2609}
Andrew Vasquezcb8dacb2008-04-03 13:13:19 -07002610
2611static int
Joe Carnuccio1ee27142008-07-10 16:55:53 -07002612qla2xxx_is_vpd_valid(uint8_t *pos, uint8_t *end)
2613{
2614 if (pos >= end || *pos != 0x82)
2615 return 0;
2616
2617 pos += 3 + pos[1];
2618 if (pos >= end || *pos != 0x90)
2619 return 0;
2620
2621 pos += 3 + pos[1];
2622 if (pos >= end || *pos != 0x78)
2623 return 0;
2624
2625 return 1;
2626}
2627
2628int
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -08002629qla2xxx_get_vpd_field(scsi_qla_host_t *vha, char *key, char *str, size_t size)
Joe Carnuccio1ee27142008-07-10 16:55:53 -07002630{
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -08002631 struct qla_hw_data *ha = vha->hw;
Joe Carnuccio1ee27142008-07-10 16:55:53 -07002632 uint8_t *pos = ha->vpd;
2633 uint8_t *end = pos + ha->vpd_size;
2634 int len = 0;
2635
2636 if (!IS_FWI2_CAPABLE(ha) || !qla2xxx_is_vpd_valid(pos, end))
2637 return 0;
2638
2639 while (pos < end && *pos != 0x78) {
2640 len = (*pos == 0x82) ? pos[1] : pos[2];
2641
2642 if (!strncmp(pos, key, strlen(key)))
2643 break;
2644
2645 if (*pos != 0x90 && *pos != 0x91)
2646 pos += len;
2647
2648 pos += 3;
2649 }
2650
2651 if (pos < end - len && *pos != 0x78)
2652 return snprintf(str, size, "%.*s", len, pos + 3);
2653
2654 return 0;
2655}