blob: 22070621206c78d6eec28666dad84e3a6a756329 [file] [log] [blame]
Andrew Vasquezfa90c542005-10-27 11:10:08 -07001/*
2 * QLogic Fibre Channel HBA Driver
Giridhar Malavalide7c5d02010-07-23 15:28:36 +05003 * Copyright (c) 2003-2010 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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090010#include <linux/slab.h>
Andrew Vasquez2c96d8d2007-10-19 15:59:15 -070011#include <linux/vmalloc.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070012#include <asm/uaccess.h>
13
Linus Torvalds1da177e2005-04-16 15:20:36 -070014/*
15 * NVRAM support routines
16 */
17
18/**
Andrew Vasquezfa2a1ce2005-07-06 10:32:07 -070019 * qla2x00_lock_nvram_access() -
Linus Torvalds1da177e2005-04-16 15:20:36 -070020 * @ha: HA context
21 */
Adrian Bunka824ebb2008-01-17 09:02:15 -080022static void
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -080023qla2x00_lock_nvram_access(struct qla_hw_data *ha)
Linus Torvalds1da177e2005-04-16 15:20:36 -070024{
25 uint16_t data;
Andrew Vasquez3d716442005-07-06 10:30:26 -070026 struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
Linus Torvalds1da177e2005-04-16 15:20:36 -070027
28 if (!IS_QLA2100(ha) && !IS_QLA2200(ha) && !IS_QLA2300(ha)) {
29 data = RD_REG_WORD(&reg->nvram);
30 while (data & NVR_BUSY) {
31 udelay(100);
32 data = RD_REG_WORD(&reg->nvram);
33 }
34
35 /* Lock resource */
36 WRT_REG_WORD(&reg->u.isp2300.host_semaphore, 0x1);
37 RD_REG_WORD(&reg->u.isp2300.host_semaphore);
38 udelay(5);
39 data = RD_REG_WORD(&reg->u.isp2300.host_semaphore);
40 while ((data & BIT_0) == 0) {
41 /* Lock failed */
42 udelay(100);
43 WRT_REG_WORD(&reg->u.isp2300.host_semaphore, 0x1);
44 RD_REG_WORD(&reg->u.isp2300.host_semaphore);
45 udelay(5);
46 data = RD_REG_WORD(&reg->u.isp2300.host_semaphore);
47 }
48 }
49}
50
51/**
Andrew Vasquezfa2a1ce2005-07-06 10:32:07 -070052 * qla2x00_unlock_nvram_access() -
Linus Torvalds1da177e2005-04-16 15:20:36 -070053 * @ha: HA context
54 */
Adrian Bunka824ebb2008-01-17 09:02:15 -080055static void
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -080056qla2x00_unlock_nvram_access(struct qla_hw_data *ha)
Linus Torvalds1da177e2005-04-16 15:20:36 -070057{
Andrew Vasquez3d716442005-07-06 10:30:26 -070058 struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
Linus Torvalds1da177e2005-04-16 15:20:36 -070059
60 if (!IS_QLA2100(ha) && !IS_QLA2200(ha) && !IS_QLA2300(ha)) {
61 WRT_REG_WORD(&reg->u.isp2300.host_semaphore, 0);
62 RD_REG_WORD(&reg->u.isp2300.host_semaphore);
63 }
64}
65
66/**
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -080067 * qla2x00_nv_write() - Prepare for NVRAM read/write operation.
68 * @ha: HA context
69 * @data: Serial interface selector
70 */
71static void
72qla2x00_nv_write(struct qla_hw_data *ha, uint16_t data)
73{
74 struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
75
76 WRT_REG_WORD(&reg->nvram, data | NVR_SELECT | NVR_WRT_ENABLE);
77 RD_REG_WORD(&reg->nvram); /* PCI Posting. */
78 NVRAM_DELAY();
79 WRT_REG_WORD(&reg->nvram, data | NVR_SELECT | NVR_CLOCK |
80 NVR_WRT_ENABLE);
81 RD_REG_WORD(&reg->nvram); /* PCI Posting. */
82 NVRAM_DELAY();
83 WRT_REG_WORD(&reg->nvram, data | NVR_SELECT | NVR_WRT_ENABLE);
84 RD_REG_WORD(&reg->nvram); /* PCI Posting. */
85 NVRAM_DELAY();
86}
87
88/**
89 * qla2x00_nvram_request() - Sends read command to NVRAM and gets data from
90 * NVRAM.
91 * @ha: HA context
92 * @nv_cmd: NVRAM command
93 *
94 * Bit definitions for NVRAM command:
95 *
96 * Bit 26 = start bit
97 * Bit 25, 24 = opcode
98 * Bit 23-16 = address
99 * Bit 15-0 = write data
100 *
101 * Returns the word read from nvram @addr.
102 */
103static uint16_t
104qla2x00_nvram_request(struct qla_hw_data *ha, uint32_t nv_cmd)
105{
106 uint8_t cnt;
107 struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
108 uint16_t data = 0;
109 uint16_t reg_data;
110
111 /* Send command to NVRAM. */
112 nv_cmd <<= 5;
113 for (cnt = 0; cnt < 11; cnt++) {
114 if (nv_cmd & BIT_31)
115 qla2x00_nv_write(ha, NVR_DATA_OUT);
116 else
117 qla2x00_nv_write(ha, 0);
118 nv_cmd <<= 1;
119 }
120
121 /* Read data from NVRAM. */
122 for (cnt = 0; cnt < 16; cnt++) {
123 WRT_REG_WORD(&reg->nvram, NVR_SELECT | NVR_CLOCK);
124 RD_REG_WORD(&reg->nvram); /* PCI Posting. */
125 NVRAM_DELAY();
126 data <<= 1;
127 reg_data = RD_REG_WORD(&reg->nvram);
128 if (reg_data & NVR_DATA_IN)
129 data |= BIT_0;
130 WRT_REG_WORD(&reg->nvram, NVR_SELECT);
131 RD_REG_WORD(&reg->nvram); /* PCI Posting. */
132 NVRAM_DELAY();
133 }
134
135 /* Deselect chip. */
136 WRT_REG_WORD(&reg->nvram, NVR_DESELECT);
137 RD_REG_WORD(&reg->nvram); /* PCI Posting. */
138 NVRAM_DELAY();
139
140 return data;
141}
142
143
144/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 * qla2x00_get_nvram_word() - Calculates word position in NVRAM and calls the
146 * request routine to get the word from NVRAM.
147 * @ha: HA context
148 * @addr: Address in NVRAM to read
149 *
150 * Returns the word read from nvram @addr.
151 */
Adrian Bunka824ebb2008-01-17 09:02:15 -0800152static uint16_t
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -0800153qla2x00_get_nvram_word(struct qla_hw_data *ha, uint32_t addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154{
155 uint16_t data;
156 uint32_t nv_cmd;
157
158 nv_cmd = addr << 16;
159 nv_cmd |= NV_READ_OP;
160 data = qla2x00_nvram_request(ha, nv_cmd);
161
162 return (data);
163}
164
165/**
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -0800166 * qla2x00_nv_deselect() - Deselect NVRAM operations.
167 * @ha: HA context
168 */
169static void
170qla2x00_nv_deselect(struct qla_hw_data *ha)
171{
172 struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
173
174 WRT_REG_WORD(&reg->nvram, NVR_DESELECT);
175 RD_REG_WORD(&reg->nvram); /* PCI Posting. */
176 NVRAM_DELAY();
177}
178
179/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 * qla2x00_write_nvram_word() - Write NVRAM data.
181 * @ha: HA context
182 * @addr: Address in NVRAM to write
183 * @data: word to program
184 */
Adrian Bunka824ebb2008-01-17 09:02:15 -0800185static void
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -0800186qla2x00_write_nvram_word(struct qla_hw_data *ha, uint32_t addr, uint16_t data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187{
188 int count;
189 uint16_t word;
Ravi Anand45aeaf12006-05-17 15:08:49 -0700190 uint32_t nv_cmd, wait_cnt;
Andrew Vasquez3d716442005-07-06 10:30:26 -0700191 struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192
193 qla2x00_nv_write(ha, NVR_DATA_OUT);
194 qla2x00_nv_write(ha, 0);
195 qla2x00_nv_write(ha, 0);
196
197 for (word = 0; word < 8; word++)
198 qla2x00_nv_write(ha, NVR_DATA_OUT);
199
200 qla2x00_nv_deselect(ha);
201
202 /* Write data */
203 nv_cmd = (addr << 16) | NV_WRITE_OP;
204 nv_cmd |= data;
205 nv_cmd <<= 5;
206 for (count = 0; count < 27; count++) {
207 if (nv_cmd & BIT_31)
208 qla2x00_nv_write(ha, NVR_DATA_OUT);
209 else
210 qla2x00_nv_write(ha, 0);
211
212 nv_cmd <<= 1;
213 }
214
215 qla2x00_nv_deselect(ha);
216
217 /* Wait for NVRAM to become ready */
218 WRT_REG_WORD(&reg->nvram, NVR_SELECT);
Andrew Vasquezdcb36ce2005-11-08 14:37:06 -0800219 RD_REG_WORD(&reg->nvram); /* PCI Posting. */
Ravi Anand45aeaf12006-05-17 15:08:49 -0700220 wait_cnt = NVR_WAIT_CNT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 do {
Ravi Anand45aeaf12006-05-17 15:08:49 -0700222 if (!--wait_cnt) {
Andrew Vasquez76403352009-04-06 22:33:39 -0700223 DEBUG9_10(qla_printk(KERN_WARNING, ha,
224 "NVRAM didn't go ready...\n"));
Ravi Anand45aeaf12006-05-17 15:08:49 -0700225 break;
226 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 NVRAM_DELAY();
228 word = RD_REG_WORD(&reg->nvram);
229 } while ((word & NVR_DATA_IN) == 0);
230
231 qla2x00_nv_deselect(ha);
232
233 /* Disable writes */
234 qla2x00_nv_write(ha, NVR_DATA_OUT);
235 for (count = 0; count < 10; count++)
236 qla2x00_nv_write(ha, 0);
237
238 qla2x00_nv_deselect(ha);
239}
240
Andrew Vasquez459c5372005-07-06 10:31:07 -0700241static int
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -0800242qla2x00_write_nvram_word_tmo(struct qla_hw_data *ha, uint32_t addr,
243 uint16_t data, uint32_t tmo)
Andrew Vasquez459c5372005-07-06 10:31:07 -0700244{
245 int ret, count;
246 uint16_t word;
247 uint32_t nv_cmd;
248 struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
249
250 ret = QLA_SUCCESS;
251
252 qla2x00_nv_write(ha, NVR_DATA_OUT);
253 qla2x00_nv_write(ha, 0);
254 qla2x00_nv_write(ha, 0);
255
256 for (word = 0; word < 8; word++)
257 qla2x00_nv_write(ha, NVR_DATA_OUT);
258
259 qla2x00_nv_deselect(ha);
260
261 /* Write data */
262 nv_cmd = (addr << 16) | NV_WRITE_OP;
263 nv_cmd |= data;
264 nv_cmd <<= 5;
265 for (count = 0; count < 27; count++) {
266 if (nv_cmd & BIT_31)
267 qla2x00_nv_write(ha, NVR_DATA_OUT);
268 else
269 qla2x00_nv_write(ha, 0);
270
271 nv_cmd <<= 1;
272 }
273
274 qla2x00_nv_deselect(ha);
275
276 /* Wait for NVRAM to become ready */
277 WRT_REG_WORD(&reg->nvram, NVR_SELECT);
Andrew Vasquezdcb36ce2005-11-08 14:37:06 -0800278 RD_REG_WORD(&reg->nvram); /* PCI Posting. */
Andrew Vasquez459c5372005-07-06 10:31:07 -0700279 do {
280 NVRAM_DELAY();
281 word = RD_REG_WORD(&reg->nvram);
282 if (!--tmo) {
283 ret = QLA_FUNCTION_FAILED;
284 break;
285 }
286 } while ((word & NVR_DATA_IN) == 0);
287
288 qla2x00_nv_deselect(ha);
289
290 /* Disable writes */
291 qla2x00_nv_write(ha, NVR_DATA_OUT);
292 for (count = 0; count < 10; count++)
293 qla2x00_nv_write(ha, 0);
294
295 qla2x00_nv_deselect(ha);
296
297 return ret;
298}
299
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300/**
Andrew Vasquez459c5372005-07-06 10:31:07 -0700301 * qla2x00_clear_nvram_protection() -
302 * @ha: HA context
303 */
304static int
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -0800305qla2x00_clear_nvram_protection(struct qla_hw_data *ha)
Andrew Vasquez459c5372005-07-06 10:31:07 -0700306{
307 int ret, stat;
308 struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
Ravi Anand45aeaf12006-05-17 15:08:49 -0700309 uint32_t word, wait_cnt;
Andrew Vasquez459c5372005-07-06 10:31:07 -0700310 uint16_t wprot, wprot_old;
311
312 /* Clear NVRAM write protection. */
313 ret = QLA_FUNCTION_FAILED;
Ravi Anand45aeaf12006-05-17 15:08:49 -0700314
315 wprot_old = cpu_to_le16(qla2x00_get_nvram_word(ha, ha->nvram_base));
316 stat = qla2x00_write_nvram_word_tmo(ha, ha->nvram_base,
Andrew Vasquez459c5372005-07-06 10:31:07 -0700317 __constant_cpu_to_le16(0x1234), 100000);
Ravi Anand45aeaf12006-05-17 15:08:49 -0700318 wprot = cpu_to_le16(qla2x00_get_nvram_word(ha, ha->nvram_base));
319 if (stat != QLA_SUCCESS || wprot != 0x1234) {
Andrew Vasquez459c5372005-07-06 10:31:07 -0700320 /* Write enable. */
321 qla2x00_nv_write(ha, NVR_DATA_OUT);
322 qla2x00_nv_write(ha, 0);
323 qla2x00_nv_write(ha, 0);
324 for (word = 0; word < 8; word++)
325 qla2x00_nv_write(ha, NVR_DATA_OUT);
326
327 qla2x00_nv_deselect(ha);
328
329 /* Enable protection register. */
330 qla2x00_nv_write(ha, NVR_PR_ENABLE | NVR_DATA_OUT);
331 qla2x00_nv_write(ha, NVR_PR_ENABLE);
332 qla2x00_nv_write(ha, NVR_PR_ENABLE);
333 for (word = 0; word < 8; word++)
334 qla2x00_nv_write(ha, NVR_DATA_OUT | NVR_PR_ENABLE);
335
336 qla2x00_nv_deselect(ha);
337
338 /* Clear protection register (ffff is cleared). */
339 qla2x00_nv_write(ha, NVR_PR_ENABLE | NVR_DATA_OUT);
340 qla2x00_nv_write(ha, NVR_PR_ENABLE | NVR_DATA_OUT);
341 qla2x00_nv_write(ha, NVR_PR_ENABLE | NVR_DATA_OUT);
342 for (word = 0; word < 8; word++)
343 qla2x00_nv_write(ha, NVR_DATA_OUT | NVR_PR_ENABLE);
344
345 qla2x00_nv_deselect(ha);
346
347 /* Wait for NVRAM to become ready. */
348 WRT_REG_WORD(&reg->nvram, NVR_SELECT);
Andrew Vasquezdcb36ce2005-11-08 14:37:06 -0800349 RD_REG_WORD(&reg->nvram); /* PCI Posting. */
Ravi Anand45aeaf12006-05-17 15:08:49 -0700350 wait_cnt = NVR_WAIT_CNT;
Andrew Vasquez459c5372005-07-06 10:31:07 -0700351 do {
Ravi Anand45aeaf12006-05-17 15:08:49 -0700352 if (!--wait_cnt) {
Andrew Vasquez76403352009-04-06 22:33:39 -0700353 DEBUG9_10(qla_printk(KERN_WARNING, ha,
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -0800354 "NVRAM didn't go ready...\n"));
Ravi Anand45aeaf12006-05-17 15:08:49 -0700355 break;
356 }
Andrew Vasquez459c5372005-07-06 10:31:07 -0700357 NVRAM_DELAY();
358 word = RD_REG_WORD(&reg->nvram);
359 } while ((word & NVR_DATA_IN) == 0);
360
Ravi Anand45aeaf12006-05-17 15:08:49 -0700361 if (wait_cnt)
362 ret = QLA_SUCCESS;
Andrew Vasquez459c5372005-07-06 10:31:07 -0700363 } else
Ravi Anand45aeaf12006-05-17 15:08:49 -0700364 qla2x00_write_nvram_word(ha, ha->nvram_base, wprot_old);
Andrew Vasquez459c5372005-07-06 10:31:07 -0700365
366 return ret;
367}
368
369static void
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -0800370qla2x00_set_nvram_protection(struct qla_hw_data *ha, int stat)
Andrew Vasquez459c5372005-07-06 10:31:07 -0700371{
372 struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
Ravi Anand45aeaf12006-05-17 15:08:49 -0700373 uint32_t word, wait_cnt;
Andrew Vasquez459c5372005-07-06 10:31:07 -0700374
375 if (stat != QLA_SUCCESS)
376 return;
377
378 /* Set NVRAM write protection. */
379 /* Write enable. */
380 qla2x00_nv_write(ha, NVR_DATA_OUT);
381 qla2x00_nv_write(ha, 0);
382 qla2x00_nv_write(ha, 0);
383 for (word = 0; word < 8; word++)
384 qla2x00_nv_write(ha, NVR_DATA_OUT);
385
386 qla2x00_nv_deselect(ha);
387
388 /* Enable protection register. */
389 qla2x00_nv_write(ha, NVR_PR_ENABLE | NVR_DATA_OUT);
390 qla2x00_nv_write(ha, NVR_PR_ENABLE);
391 qla2x00_nv_write(ha, NVR_PR_ENABLE);
392 for (word = 0; word < 8; word++)
393 qla2x00_nv_write(ha, NVR_DATA_OUT | NVR_PR_ENABLE);
394
395 qla2x00_nv_deselect(ha);
396
397 /* Enable protection register. */
398 qla2x00_nv_write(ha, NVR_PR_ENABLE | NVR_DATA_OUT);
399 qla2x00_nv_write(ha, NVR_PR_ENABLE);
400 qla2x00_nv_write(ha, NVR_PR_ENABLE | NVR_DATA_OUT);
401 for (word = 0; word < 8; word++)
402 qla2x00_nv_write(ha, NVR_PR_ENABLE);
403
404 qla2x00_nv_deselect(ha);
405
406 /* Wait for NVRAM to become ready. */
407 WRT_REG_WORD(&reg->nvram, NVR_SELECT);
Andrew Vasquezdcb36ce2005-11-08 14:37:06 -0800408 RD_REG_WORD(&reg->nvram); /* PCI Posting. */
Ravi Anand45aeaf12006-05-17 15:08:49 -0700409 wait_cnt = NVR_WAIT_CNT;
Andrew Vasquez459c5372005-07-06 10:31:07 -0700410 do {
Ravi Anand45aeaf12006-05-17 15:08:49 -0700411 if (!--wait_cnt) {
Andrew Vasquez76403352009-04-06 22:33:39 -0700412 DEBUG9_10(qla_printk(KERN_WARNING, ha,
413 "NVRAM didn't go ready...\n"));
Ravi Anand45aeaf12006-05-17 15:08:49 -0700414 break;
415 }
Andrew Vasquez459c5372005-07-06 10:31:07 -0700416 NVRAM_DELAY();
417 word = RD_REG_WORD(&reg->nvram);
418 } while ((word & NVR_DATA_IN) == 0);
419}
420
421
422/*****************************************************************************/
423/* Flash Manipulation Routines */
424/*****************************************************************************/
425
426static inline uint32_t
Andrew Vasquez3a03eb72009-01-05 11:18:11 -0800427flash_conf_addr(struct qla_hw_data *ha, uint32_t faddr)
Andrew Vasquez459c5372005-07-06 10:31:07 -0700428{
Andrew Vasquez3a03eb72009-01-05 11:18:11 -0800429 return ha->flash_conf_off | faddr;
Andrew Vasquez459c5372005-07-06 10:31:07 -0700430}
431
432static inline uint32_t
Andrew Vasquez3a03eb72009-01-05 11:18:11 -0800433flash_data_addr(struct qla_hw_data *ha, uint32_t faddr)
Andrew Vasquez459c5372005-07-06 10:31:07 -0700434{
Andrew Vasquez3a03eb72009-01-05 11:18:11 -0800435 return ha->flash_data_off | faddr;
Andrew Vasquez459c5372005-07-06 10:31:07 -0700436}
437
438static inline uint32_t
Andrew Vasquez3a03eb72009-01-05 11:18:11 -0800439nvram_conf_addr(struct qla_hw_data *ha, uint32_t naddr)
Andrew Vasquez459c5372005-07-06 10:31:07 -0700440{
Andrew Vasquez3a03eb72009-01-05 11:18:11 -0800441 return ha->nvram_conf_off | naddr;
Andrew Vasquez459c5372005-07-06 10:31:07 -0700442}
443
444static inline uint32_t
Andrew Vasquez3a03eb72009-01-05 11:18:11 -0800445nvram_data_addr(struct qla_hw_data *ha, uint32_t naddr)
Andrew Vasquez459c5372005-07-06 10:31:07 -0700446{
Andrew Vasquez3a03eb72009-01-05 11:18:11 -0800447 return ha->nvram_data_off | naddr;
Andrew Vasquez459c5372005-07-06 10:31:07 -0700448}
449
Adrian Bunke5f82ab2006-11-08 19:55:50 -0800450static uint32_t
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -0800451qla24xx_read_flash_dword(struct qla_hw_data *ha, uint32_t addr)
Andrew Vasquez459c5372005-07-06 10:31:07 -0700452{
453 int rval;
454 uint32_t cnt, data;
455 struct device_reg_24xx __iomem *reg = &ha->iobase->isp24;
456
457 WRT_REG_DWORD(&reg->flash_addr, addr & ~FARX_DATA_FLAG);
458 /* Wait for READ cycle to complete. */
459 rval = QLA_SUCCESS;
460 for (cnt = 3000;
461 (RD_REG_DWORD(&reg->flash_addr) & FARX_DATA_FLAG) == 0 &&
462 rval == QLA_SUCCESS; cnt--) {
463 if (cnt)
464 udelay(10);
465 else
466 rval = QLA_FUNCTION_TIMEOUT;
Andrew Vasquez40a2e342007-03-12 10:41:28 -0700467 cond_resched();
Andrew Vasquez459c5372005-07-06 10:31:07 -0700468 }
469
470 /* TODO: What happens if we time out? */
471 data = 0xDEADDEAD;
472 if (rval == QLA_SUCCESS)
473 data = RD_REG_DWORD(&reg->flash_data);
474
475 return data;
476}
477
478uint32_t *
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -0800479qla24xx_read_flash_data(scsi_qla_host_t *vha, uint32_t *dwptr, uint32_t faddr,
Andrew Vasquez459c5372005-07-06 10:31:07 -0700480 uint32_t dwords)
481{
482 uint32_t i;
Andrew Vasquez3a03eb72009-01-05 11:18:11 -0800483 struct qla_hw_data *ha = vha->hw;
484
Andrew Vasquez459c5372005-07-06 10:31:07 -0700485 /* Dword reads to flash. */
486 for (i = 0; i < dwords; i++, faddr++)
Andrew Vasquez3a03eb72009-01-05 11:18:11 -0800487 dwptr[i] = cpu_to_le32(qla24xx_read_flash_dword(ha,
488 flash_data_addr(ha, faddr)));
Andrew Vasquez459c5372005-07-06 10:31:07 -0700489
Andrew Vasquez459c5372005-07-06 10:31:07 -0700490 return dwptr;
491}
492
Adrian Bunke5f82ab2006-11-08 19:55:50 -0800493static int
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -0800494qla24xx_write_flash_dword(struct qla_hw_data *ha, uint32_t addr, uint32_t data)
Andrew Vasquez459c5372005-07-06 10:31:07 -0700495{
496 int rval;
497 uint32_t cnt;
498 struct device_reg_24xx __iomem *reg = &ha->iobase->isp24;
499
500 WRT_REG_DWORD(&reg->flash_data, data);
501 RD_REG_DWORD(&reg->flash_data); /* PCI Posting. */
502 WRT_REG_DWORD(&reg->flash_addr, addr | FARX_DATA_FLAG);
503 /* Wait for Write cycle to complete. */
504 rval = QLA_SUCCESS;
505 for (cnt = 500000; (RD_REG_DWORD(&reg->flash_addr) & FARX_DATA_FLAG) &&
506 rval == QLA_SUCCESS; cnt--) {
507 if (cnt)
508 udelay(10);
509 else
510 rval = QLA_FUNCTION_TIMEOUT;
Andrew Vasquez40a2e342007-03-12 10:41:28 -0700511 cond_resched();
Andrew Vasquez459c5372005-07-06 10:31:07 -0700512 }
513 return rval;
514}
515
Adrian Bunke5f82ab2006-11-08 19:55:50 -0800516static void
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -0800517qla24xx_get_flash_manufacturer(struct qla_hw_data *ha, uint8_t *man_id,
Andrew Vasquez459c5372005-07-06 10:31:07 -0700518 uint8_t *flash_id)
519{
520 uint32_t ids;
521
Andrew Vasquez3a03eb72009-01-05 11:18:11 -0800522 ids = qla24xx_read_flash_dword(ha, flash_conf_addr(ha, 0x03ab));
Andrew Vasquez459c5372005-07-06 10:31:07 -0700523 *man_id = LSB(ids);
524 *flash_id = MSB(ids);
Ravi Anand45aeaf12006-05-17 15:08:49 -0700525
526 /* Check if man_id and flash_id are valid. */
527 if (ids != 0xDEADDEAD && (*man_id == 0 || *flash_id == 0)) {
528 /* Read information using 0x9f opcode
529 * Device ID, Mfg ID would be read in the format:
530 * <Ext Dev Info><Device ID Part2><Device ID Part 1><Mfg ID>
531 * Example: ATMEL 0x00 01 45 1F
532 * Extract MFG and Dev ID from last two bytes.
533 */
Andrew Vasquez3a03eb72009-01-05 11:18:11 -0800534 ids = qla24xx_read_flash_dword(ha, flash_conf_addr(ha, 0x009f));
Ravi Anand45aeaf12006-05-17 15:08:49 -0700535 *man_id = LSB(ids);
536 *flash_id = MSB(ids);
537 }
Andrew Vasquez459c5372005-07-06 10:31:07 -0700538}
539
Andrew Vasquezc00d8992008-09-11 21:22:49 -0700540static int
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -0800541qla2xxx_find_flt_start(scsi_qla_host_t *vha, uint32_t *start)
Andrew Vasquezc00d8992008-09-11 21:22:49 -0700542{
543 const char *loc, *locations[] = { "DEF", "PCI" };
544 uint32_t pcihdr, pcids;
545 uint32_t *dcode;
546 uint8_t *buf, *bcode, last_image;
547 uint16_t cnt, chksum, *wptr;
548 struct qla_flt_location *fltl;
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -0800549 struct qla_hw_data *ha = vha->hw;
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800550 struct req_que *req = ha->req_q_map[0];
Andrew Vasquezc00d8992008-09-11 21:22:49 -0700551
552 /*
553 * FLT-location structure resides after the last PCI region.
554 */
555
556 /* Begin with sane defaults. */
557 loc = locations[0];
Andrew Vasquez3a03eb72009-01-05 11:18:11 -0800558 *start = 0;
559 if (IS_QLA24XX_TYPE(ha))
560 *start = FA_FLASH_LAYOUT_ADDR_24;
561 else if (IS_QLA25XX(ha))
562 *start = FA_FLASH_LAYOUT_ADDR;
563 else if (IS_QLA81XX(ha))
564 *start = FA_FLASH_LAYOUT_ADDR_81;
Giridhar Malavalia9083012010-04-12 17:59:55 -0700565 else if (IS_QLA82XX(ha)) {
566 *start = FA_FLASH_LAYOUT_ADDR_82;
567 goto end;
568 }
Andrew Vasquezc00d8992008-09-11 21:22:49 -0700569 /* Begin with first PCI expansion ROM header. */
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -0800570 buf = (uint8_t *)req->ring;
571 dcode = (uint32_t *)req->ring;
Andrew Vasquezc00d8992008-09-11 21:22:49 -0700572 pcihdr = 0;
573 last_image = 1;
574 do {
575 /* Verify PCI expansion ROM header. */
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -0800576 qla24xx_read_flash_data(vha, dcode, pcihdr >> 2, 0x20);
Andrew Vasquezc00d8992008-09-11 21:22:49 -0700577 bcode = buf + (pcihdr % 4);
578 if (bcode[0x0] != 0x55 || bcode[0x1] != 0xaa)
579 goto end;
580
581 /* Locate PCI data structure. */
582 pcids = pcihdr + ((bcode[0x19] << 8) | bcode[0x18]);
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -0800583 qla24xx_read_flash_data(vha, dcode, pcids >> 2, 0x20);
Andrew Vasquezc00d8992008-09-11 21:22:49 -0700584 bcode = buf + (pcihdr % 4);
585
586 /* Validate signature of PCI data structure. */
587 if (bcode[0x0] != 'P' || bcode[0x1] != 'C' ||
588 bcode[0x2] != 'I' || bcode[0x3] != 'R')
589 goto end;
590
591 last_image = bcode[0x15] & BIT_7;
592
593 /* Locate next PCI expansion ROM. */
594 pcihdr += ((bcode[0x11] << 8) | bcode[0x10]) * 512;
595 } while (!last_image);
596
597 /* Now verify FLT-location structure. */
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -0800598 fltl = (struct qla_flt_location *)req->ring;
599 qla24xx_read_flash_data(vha, dcode, pcihdr >> 2,
Andrew Vasquezc00d8992008-09-11 21:22:49 -0700600 sizeof(struct qla_flt_location) >> 2);
601 if (fltl->sig[0] != 'Q' || fltl->sig[1] != 'F' ||
602 fltl->sig[2] != 'L' || fltl->sig[3] != 'T')
603 goto end;
604
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -0800605 wptr = (uint16_t *)req->ring;
Andrew Vasquezc00d8992008-09-11 21:22:49 -0700606 cnt = sizeof(struct qla_flt_location) >> 1;
607 for (chksum = 0; cnt; cnt--)
608 chksum += le16_to_cpu(*wptr++);
609 if (chksum) {
610 qla_printk(KERN_ERR, ha,
611 "Inconsistent FLTL detected: checksum=0x%x.\n", chksum);
612 qla2x00_dump_buffer(buf, sizeof(struct qla_flt_location));
613 return QLA_FUNCTION_FAILED;
614 }
615
616 /* Good data. Use specified location. */
617 loc = locations[1];
Harish Zunjarrao79c13a72009-03-24 09:08:19 -0700618 *start = (le16_to_cpu(fltl->start_hi) << 16 |
619 le16_to_cpu(fltl->start_lo)) >> 2;
Andrew Vasquezc00d8992008-09-11 21:22:49 -0700620end:
621 DEBUG2(qla_printk(KERN_DEBUG, ha, "FLTL[%s] = 0x%x.\n", loc, *start));
622 return QLA_SUCCESS;
623}
624
625static void
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -0800626qla2xxx_get_flt_info(scsi_qla_host_t *vha, uint32_t flt_addr)
Andrew Vasquezc00d8992008-09-11 21:22:49 -0700627{
628 const char *loc, *locations[] = { "DEF", "FLT" };
Andrew Vasquez3a03eb72009-01-05 11:18:11 -0800629 const uint32_t def_fw[] =
630 { FA_RISC_CODE_ADDR, FA_RISC_CODE_ADDR, FA_RISC_CODE_ADDR_81 };
631 const uint32_t def_boot[] =
632 { FA_BOOT_CODE_ADDR, FA_BOOT_CODE_ADDR, FA_BOOT_CODE_ADDR_81 };
633 const uint32_t def_vpd_nvram[] =
634 { FA_VPD_NVRAM_ADDR, FA_VPD_NVRAM_ADDR, FA_VPD_NVRAM_ADDR_81 };
Andrew Vasquez3d79038f2009-03-24 09:08:14 -0700635 const uint32_t def_vpd0[] =
636 { 0, 0, FA_VPD0_ADDR_81 };
637 const uint32_t def_vpd1[] =
638 { 0, 0, FA_VPD1_ADDR_81 };
639 const uint32_t def_nvram0[] =
640 { 0, 0, FA_NVRAM0_ADDR_81 };
641 const uint32_t def_nvram1[] =
642 { 0, 0, FA_NVRAM1_ADDR_81 };
Andrew Vasquez3a03eb72009-01-05 11:18:11 -0800643 const uint32_t def_fdt[] =
644 { FA_FLASH_DESCR_ADDR_24, FA_FLASH_DESCR_ADDR,
645 FA_FLASH_DESCR_ADDR_81 };
646 const uint32_t def_npiv_conf0[] =
647 { FA_NPIV_CONF0_ADDR_24, FA_NPIV_CONF0_ADDR,
648 FA_NPIV_CONF0_ADDR_81 };
649 const uint32_t def_npiv_conf1[] =
650 { FA_NPIV_CONF1_ADDR_24, FA_NPIV_CONF1_ADDR,
651 FA_NPIV_CONF1_ADDR_81 };
Sarang Radke09ff7012010-03-19 17:03:59 -0700652 const uint32_t fcp_prio_cfg0[] =
653 { FA_FCP_PRIO0_ADDR, FA_FCP_PRIO0_ADDR_25,
654 0 };
655 const uint32_t fcp_prio_cfg1[] =
656 { FA_FCP_PRIO1_ADDR, FA_FCP_PRIO1_ADDR_25,
657 0 };
Andrew Vasquez3a03eb72009-01-05 11:18:11 -0800658 uint32_t def;
Andrew Vasquezc00d8992008-09-11 21:22:49 -0700659 uint16_t *wptr;
660 uint16_t cnt, chksum;
661 uint32_t start;
662 struct qla_flt_header *flt;
663 struct qla_flt_region *region;
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -0800664 struct qla_hw_data *ha = vha->hw;
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800665 struct req_que *req = ha->req_q_map[0];
Andrew Vasquezc00d8992008-09-11 21:22:49 -0700666
Madhuranath Iyengar2f0f3f42010-07-23 15:28:24 +0500667 def = 0;
668 if (IS_QLA25XX(ha))
669 def = 1;
670 else if (IS_QLA81XX(ha))
671 def = 2;
Andrew Vasquezff8073f2010-12-21 16:00:16 -0800672
673 /* Assign FCP prio region since older adapters may not have FLT, or
674 FCP prio region in it's FLT.
675 */
676 ha->flt_region_fcp_prio = ha->flags.port0 ?
677 fcp_prio_cfg0[def] : fcp_prio_cfg1[def];
678
Andrew Vasquezc00d8992008-09-11 21:22:49 -0700679 ha->flt_region_flt = flt_addr;
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -0800680 wptr = (uint16_t *)req->ring;
681 flt = (struct qla_flt_header *)req->ring;
Andrew Vasquezc00d8992008-09-11 21:22:49 -0700682 region = (struct qla_flt_region *)&flt[1];
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -0800683 ha->isp_ops->read_optrom(vha, (uint8_t *)req->ring,
Andrew Vasquezc00d8992008-09-11 21:22:49 -0700684 flt_addr << 2, OPTROM_BURST_SIZE);
685 if (*wptr == __constant_cpu_to_le16(0xffff))
686 goto no_flash_data;
687 if (flt->version != __constant_cpu_to_le16(1)) {
688 DEBUG2(qla_printk(KERN_INFO, ha, "Unsupported FLT detected: "
689 "version=0x%x length=0x%x checksum=0x%x.\n",
690 le16_to_cpu(flt->version), le16_to_cpu(flt->length),
691 le16_to_cpu(flt->checksum)));
692 goto no_flash_data;
693 }
694
695 cnt = (sizeof(struct qla_flt_header) + le16_to_cpu(flt->length)) >> 1;
696 for (chksum = 0; cnt; cnt--)
697 chksum += le16_to_cpu(*wptr++);
698 if (chksum) {
699 DEBUG2(qla_printk(KERN_INFO, ha, "Inconsistent FLT detected: "
700 "version=0x%x length=0x%x checksum=0x%x.\n",
701 le16_to_cpu(flt->version), le16_to_cpu(flt->length),
702 chksum));
703 goto no_flash_data;
704 }
705
706 loc = locations[1];
707 cnt = le16_to_cpu(flt->length) / sizeof(struct qla_flt_region);
708 for ( ; cnt; cnt--, region++) {
709 /* Store addresses as DWORD offsets. */
710 start = le32_to_cpu(region->start) >> 2;
711
712 DEBUG3(qla_printk(KERN_DEBUG, ha, "FLT[%02x]: start=0x%x "
713 "end=0x%x size=0x%x.\n", le32_to_cpu(region->code), start,
714 le32_to_cpu(region->end) >> 2, le32_to_cpu(region->size)));
715
Andrew Vasquez90886082009-02-08 20:50:14 -0800716 switch (le32_to_cpu(region->code) & 0xff) {
Andrew Vasquezc00d8992008-09-11 21:22:49 -0700717 case FLT_REG_FW:
718 ha->flt_region_fw = start;
719 break;
720 case FLT_REG_BOOT_CODE:
721 ha->flt_region_boot = start;
722 break;
723 case FLT_REG_VPD_0:
724 ha->flt_region_vpd_nvram = start;
Giridhar Malavalia9083012010-04-12 17:59:55 -0700725 if (IS_QLA82XX(ha))
726 break;
Anirban Chakrabortye5b68a62009-04-06 22:33:50 -0700727 if (ha->flags.port0)
Andrew Vasquez3d79038f2009-03-24 09:08:14 -0700728 ha->flt_region_vpd = start;
729 break;
730 case FLT_REG_VPD_1:
Giridhar Malavalia9083012010-04-12 17:59:55 -0700731 if (IS_QLA82XX(ha))
732 break;
Anirban Chakrabortye5b68a62009-04-06 22:33:50 -0700733 if (!ha->flags.port0)
Andrew Vasquez3d79038f2009-03-24 09:08:14 -0700734 ha->flt_region_vpd = start;
735 break;
736 case FLT_REG_NVRAM_0:
Anirban Chakrabortye5b68a62009-04-06 22:33:50 -0700737 if (ha->flags.port0)
Andrew Vasquez3d79038f2009-03-24 09:08:14 -0700738 ha->flt_region_nvram = start;
739 break;
740 case FLT_REG_NVRAM_1:
Anirban Chakrabortye5b68a62009-04-06 22:33:50 -0700741 if (!ha->flags.port0)
Andrew Vasquez3d79038f2009-03-24 09:08:14 -0700742 ha->flt_region_nvram = start;
Andrew Vasquezc00d8992008-09-11 21:22:49 -0700743 break;
744 case FLT_REG_FDT:
745 ha->flt_region_fdt = start;
746 break;
Andrew Vasquez272976c2008-09-11 21:22:50 -0700747 case FLT_REG_NPIV_CONF_0:
Anirban Chakrabortye5b68a62009-04-06 22:33:50 -0700748 if (ha->flags.port0)
Andrew Vasquez272976c2008-09-11 21:22:50 -0700749 ha->flt_region_npiv_conf = start;
750 break;
751 case FLT_REG_NPIV_CONF_1:
Anirban Chakrabortye5b68a62009-04-06 22:33:50 -0700752 if (!ha->flags.port0)
Andrew Vasquez272976c2008-09-11 21:22:50 -0700753 ha->flt_region_npiv_conf = start;
754 break;
Andrew Vasquezcbc8eb62009-06-03 09:55:17 -0700755 case FLT_REG_GOLD_FW:
756 ha->flt_region_gold_fw = start;
757 break;
Sarang Radke09ff7012010-03-19 17:03:59 -0700758 case FLT_REG_FCP_PRIO_0:
Sarang Radkebfdaa762010-03-19 17:04:01 -0700759 if (ha->flags.port0)
Sarang Radke09ff7012010-03-19 17:03:59 -0700760 ha->flt_region_fcp_prio = start;
761 break;
762 case FLT_REG_FCP_PRIO_1:
Sarang Radkebfdaa762010-03-19 17:04:01 -0700763 if (!ha->flags.port0)
Sarang Radke09ff7012010-03-19 17:03:59 -0700764 ha->flt_region_fcp_prio = start;
765 break;
Giridhar Malavalia9083012010-04-12 17:59:55 -0700766 case FLT_REG_BOOT_CODE_82XX:
767 ha->flt_region_boot = start;
768 break;
769 case FLT_REG_FW_82XX:
770 ha->flt_region_fw = start;
771 break;
772 case FLT_REG_GOLD_FW_82XX:
773 ha->flt_region_gold_fw = start;
774 break;
775 case FLT_REG_BOOTLOAD_82XX:
776 ha->flt_region_bootload = start;
777 break;
778 case FLT_REG_VPD_82XX:
779 ha->flt_region_vpd = start;
780 break;
Andrew Vasquezc00d8992008-09-11 21:22:49 -0700781 }
782 }
783 goto done;
784
785no_flash_data:
786 /* Use hardcoded defaults. */
787 loc = locations[0];
Andrew Vasquez3a03eb72009-01-05 11:18:11 -0800788 ha->flt_region_fw = def_fw[def];
789 ha->flt_region_boot = def_boot[def];
790 ha->flt_region_vpd_nvram = def_vpd_nvram[def];
Anirban Chakrabortye5b68a62009-04-06 22:33:50 -0700791 ha->flt_region_vpd = ha->flags.port0 ?
Sarang Radke09ff7012010-03-19 17:03:59 -0700792 def_vpd0[def] : def_vpd1[def];
Anirban Chakrabortye5b68a62009-04-06 22:33:50 -0700793 ha->flt_region_nvram = ha->flags.port0 ?
Sarang Radke09ff7012010-03-19 17:03:59 -0700794 def_nvram0[def] : def_nvram1[def];
Andrew Vasquez3a03eb72009-01-05 11:18:11 -0800795 ha->flt_region_fdt = def_fdt[def];
Anirban Chakrabortye5b68a62009-04-06 22:33:50 -0700796 ha->flt_region_npiv_conf = ha->flags.port0 ?
Sarang Radke09ff7012010-03-19 17:03:59 -0700797 def_npiv_conf0[def] : def_npiv_conf1[def];
Andrew Vasquezc00d8992008-09-11 21:22:49 -0700798done:
799 DEBUG2(qla_printk(KERN_DEBUG, ha, "FLT[%s]: boot=0x%x fw=0x%x "
Andrew Vasquez3d79038f2009-03-24 09:08:14 -0700800 "vpd_nvram=0x%x vpd=0x%x nvram=0x%x fdt=0x%x flt=0x%x "
Madhuranath Iyengar2f0f3f42010-07-23 15:28:24 +0500801 "npiv=0x%x. fcp_prio_cfg=0x%x\n", loc, ha->flt_region_boot,
802 ha->flt_region_fw, ha->flt_region_vpd_nvram, ha->flt_region_vpd,
803 ha->flt_region_nvram, ha->flt_region_fdt, ha->flt_region_flt,
804 ha->flt_region_npiv_conf, ha->flt_region_fcp_prio));
Andrew Vasquezc00d8992008-09-11 21:22:49 -0700805}
806
807static void
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -0800808qla2xxx_get_fdt_info(scsi_qla_host_t *vha)
Andrew Vasquez7d232c72008-04-03 13:13:22 -0700809{
Lalit Chandivade821b3992008-10-24 15:13:44 -0700810#define FLASH_BLK_SIZE_4K 0x1000
Andrew Vasquez7d232c72008-04-03 13:13:22 -0700811#define FLASH_BLK_SIZE_32K 0x8000
812#define FLASH_BLK_SIZE_64K 0x10000
Andrew Vasquezc00d8992008-09-11 21:22:49 -0700813 const char *loc, *locations[] = { "MID", "FDT" };
Andrew Vasquez7d232c72008-04-03 13:13:22 -0700814 uint16_t cnt, chksum;
815 uint16_t *wptr;
816 struct qla_fdt_layout *fdt;
817 uint8_t man_id, flash_id;
Giridhar Malavalia9083012010-04-12 17:59:55 -0700818 uint16_t mid = 0, fid = 0;
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -0800819 struct qla_hw_data *ha = vha->hw;
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800820 struct req_que *req = ha->req_q_map[0];
Andrew Vasquez7d232c72008-04-03 13:13:22 -0700821
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -0800822 wptr = (uint16_t *)req->ring;
823 fdt = (struct qla_fdt_layout *)req->ring;
824 ha->isp_ops->read_optrom(vha, (uint8_t *)req->ring,
Andrew Vasquezc00d8992008-09-11 21:22:49 -0700825 ha->flt_region_fdt << 2, OPTROM_BURST_SIZE);
Andrew Vasquez7d232c72008-04-03 13:13:22 -0700826 if (*wptr == __constant_cpu_to_le16(0xffff))
827 goto no_flash_data;
828 if (fdt->sig[0] != 'Q' || fdt->sig[1] != 'L' || fdt->sig[2] != 'I' ||
829 fdt->sig[3] != 'D')
830 goto no_flash_data;
831
832 for (cnt = 0, chksum = 0; cnt < sizeof(struct qla_fdt_layout) >> 1;
833 cnt++)
834 chksum += le16_to_cpu(*wptr++);
835 if (chksum) {
836 DEBUG2(qla_printk(KERN_INFO, ha, "Inconsistent FDT detected: "
837 "checksum=0x%x id=%c version=0x%x.\n", chksum, fdt->sig[0],
838 le16_to_cpu(fdt->version)));
839 DEBUG9(qla2x00_dump_buffer((uint8_t *)fdt, sizeof(*fdt)));
840 goto no_flash_data;
841 }
842
Andrew Vasquezc00d8992008-09-11 21:22:49 -0700843 loc = locations[1];
844 mid = le16_to_cpu(fdt->man_id);
845 fid = le16_to_cpu(fdt->id);
Andrew Vasquez7d232c72008-04-03 13:13:22 -0700846 ha->fdt_wrt_disable = fdt->wrt_disable_bits;
Andrew Vasquez3a03eb72009-01-05 11:18:11 -0800847 ha->fdt_erase_cmd = flash_conf_addr(ha, 0x0300 | fdt->erase_cmd);
Andrew Vasquez7d232c72008-04-03 13:13:22 -0700848 ha->fdt_block_size = le32_to_cpu(fdt->block_size);
849 if (fdt->unprotect_sec_cmd) {
Andrew Vasquez3a03eb72009-01-05 11:18:11 -0800850 ha->fdt_unprotect_sec_cmd = flash_conf_addr(ha, 0x0300 |
Andrew Vasquez7d232c72008-04-03 13:13:22 -0700851 fdt->unprotect_sec_cmd);
852 ha->fdt_protect_sec_cmd = fdt->protect_sec_cmd ?
Andrew Vasquez3a03eb72009-01-05 11:18:11 -0800853 flash_conf_addr(ha, 0x0300 | fdt->protect_sec_cmd):
854 flash_conf_addr(ha, 0x0336);
Andrew Vasquez7d232c72008-04-03 13:13:22 -0700855 }
Andrew Vasquezc00d8992008-09-11 21:22:49 -0700856 goto done;
Andrew Vasquez7d232c72008-04-03 13:13:22 -0700857no_flash_data:
Andrew Vasquezc00d8992008-09-11 21:22:49 -0700858 loc = locations[0];
Giridhar Malavalia9083012010-04-12 17:59:55 -0700859 if (IS_QLA82XX(ha)) {
860 ha->fdt_block_size = FLASH_BLK_SIZE_64K;
861 goto done;
862 }
Andrew Vasquez7d232c72008-04-03 13:13:22 -0700863 qla24xx_get_flash_manufacturer(ha, &man_id, &flash_id);
Andrew Vasquezc00d8992008-09-11 21:22:49 -0700864 mid = man_id;
865 fid = flash_id;
Andrew Vasquez7d232c72008-04-03 13:13:22 -0700866 ha->fdt_wrt_disable = 0x9c;
Andrew Vasquez3a03eb72009-01-05 11:18:11 -0800867 ha->fdt_erase_cmd = flash_conf_addr(ha, 0x03d8);
Andrew Vasquez7d232c72008-04-03 13:13:22 -0700868 switch (man_id) {
869 case 0xbf: /* STT flash. */
870 if (flash_id == 0x8e)
871 ha->fdt_block_size = FLASH_BLK_SIZE_64K;
872 else
873 ha->fdt_block_size = FLASH_BLK_SIZE_32K;
874
875 if (flash_id == 0x80)
Andrew Vasquez3a03eb72009-01-05 11:18:11 -0800876 ha->fdt_erase_cmd = flash_conf_addr(ha, 0x0352);
Andrew Vasquez7d232c72008-04-03 13:13:22 -0700877 break;
878 case 0x13: /* ST M25P80. */
879 ha->fdt_block_size = FLASH_BLK_SIZE_64K;
880 break;
881 case 0x1f: /* Atmel 26DF081A. */
Lalit Chandivade821b3992008-10-24 15:13:44 -0700882 ha->fdt_block_size = FLASH_BLK_SIZE_4K;
Andrew Vasquez3a03eb72009-01-05 11:18:11 -0800883 ha->fdt_erase_cmd = flash_conf_addr(ha, 0x0320);
884 ha->fdt_unprotect_sec_cmd = flash_conf_addr(ha, 0x0339);
885 ha->fdt_protect_sec_cmd = flash_conf_addr(ha, 0x0336);
Andrew Vasquez7d232c72008-04-03 13:13:22 -0700886 break;
887 default:
888 /* Default to 64 kb sector size. */
889 ha->fdt_block_size = FLASH_BLK_SIZE_64K;
890 break;
891 }
Andrew Vasquezc00d8992008-09-11 21:22:49 -0700892done:
893 DEBUG2(qla_printk(KERN_DEBUG, ha, "FDT[%s]: (0x%x/0x%x) erase=0x%x "
Lalit Chandivade821b3992008-10-24 15:13:44 -0700894 "pro=%x upro=%x wrtd=0x%x blk=0x%x.\n", loc, mid, fid,
Andrew Vasquez7d232c72008-04-03 13:13:22 -0700895 ha->fdt_erase_cmd, ha->fdt_protect_sec_cmd,
Lalit Chandivade821b3992008-10-24 15:13:44 -0700896 ha->fdt_unprotect_sec_cmd, ha->fdt_wrt_disable,
Andrew Vasquez7d232c72008-04-03 13:13:22 -0700897 ha->fdt_block_size));
898}
899
Giridhar Malavalia9083012010-04-12 17:59:55 -0700900static void
901qla2xxx_get_idc_param(scsi_qla_host_t *vha)
902{
903#define QLA82XX_IDC_PARAM_ADDR 0x003e885c
904 uint32_t *wptr;
905 struct qla_hw_data *ha = vha->hw;
906 struct req_que *req = ha->req_q_map[0];
907
908 if (!IS_QLA82XX(ha))
909 return;
910
911 wptr = (uint32_t *)req->ring;
912 ha->isp_ops->read_optrom(vha, (uint8_t *)req->ring,
913 QLA82XX_IDC_PARAM_ADDR , 8);
914
915 if (*wptr == __constant_cpu_to_le32(0xffffffff)) {
916 ha->nx_dev_init_timeout = QLA82XX_ROM_DEV_INIT_TIMEOUT;
917 ha->nx_reset_timeout = QLA82XX_ROM_DRV_RESET_ACK_TIMEOUT;
918 } else {
919 ha->nx_dev_init_timeout = le32_to_cpu(*wptr++);
920 ha->nx_reset_timeout = le32_to_cpu(*wptr);
921 }
922 return;
923}
924
Andrew Vasquezc00d8992008-09-11 21:22:49 -0700925int
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -0800926qla2xxx_get_flash_info(scsi_qla_host_t *vha)
Andrew Vasquezc00d8992008-09-11 21:22:49 -0700927{
928 int ret;
929 uint32_t flt_addr;
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -0800930 struct qla_hw_data *ha = vha->hw;
Andrew Vasquezc00d8992008-09-11 21:22:49 -0700931
Giridhar Malavalia9083012010-04-12 17:59:55 -0700932 if (!IS_QLA24XX_TYPE(ha) && !IS_QLA25XX(ha) && !IS_QLA8XXX_TYPE(ha))
Andrew Vasquezc00d8992008-09-11 21:22:49 -0700933 return QLA_SUCCESS;
934
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -0800935 ret = qla2xxx_find_flt_start(vha, &flt_addr);
Andrew Vasquezc00d8992008-09-11 21:22:49 -0700936 if (ret != QLA_SUCCESS)
937 return ret;
938
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -0800939 qla2xxx_get_flt_info(vha, flt_addr);
940 qla2xxx_get_fdt_info(vha);
Giridhar Malavalia9083012010-04-12 17:59:55 -0700941 qla2xxx_get_idc_param(vha);
Andrew Vasquezc00d8992008-09-11 21:22:49 -0700942
943 return QLA_SUCCESS;
944}
945
Andrew Vasquez272976c2008-09-11 21:22:50 -0700946void
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -0800947qla2xxx_flash_npiv_conf(scsi_qla_host_t *vha)
Andrew Vasquez272976c2008-09-11 21:22:50 -0700948{
949#define NPIV_CONFIG_SIZE (16*1024)
950 void *data;
951 uint16_t *wptr;
952 uint16_t cnt, chksum;
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800953 int i;
Andrew Vasquez272976c2008-09-11 21:22:50 -0700954 struct qla_npiv_header hdr;
955 struct qla_npiv_entry *entry;
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -0800956 struct qla_hw_data *ha = vha->hw;
Andrew Vasquez272976c2008-09-11 21:22:50 -0700957
Giridhar Malavalia9083012010-04-12 17:59:55 -0700958 if (!IS_QLA24XX_TYPE(ha) && !IS_QLA25XX(ha) && !IS_QLA8XXX_TYPE(ha))
Andrew Vasquez272976c2008-09-11 21:22:50 -0700959 return;
960
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -0800961 ha->isp_ops->read_optrom(vha, (uint8_t *)&hdr,
Andrew Vasquez272976c2008-09-11 21:22:50 -0700962 ha->flt_region_npiv_conf << 2, sizeof(struct qla_npiv_header));
963 if (hdr.version == __constant_cpu_to_le16(0xffff))
964 return;
965 if (hdr.version != __constant_cpu_to_le16(1)) {
966 DEBUG2(qla_printk(KERN_INFO, ha, "Unsupported NPIV-Config "
967 "detected: version=0x%x entries=0x%x checksum=0x%x.\n",
968 le16_to_cpu(hdr.version), le16_to_cpu(hdr.entries),
969 le16_to_cpu(hdr.checksum)));
970 return;
971 }
972
973 data = kmalloc(NPIV_CONFIG_SIZE, GFP_KERNEL);
974 if (!data) {
975 DEBUG2(qla_printk(KERN_INFO, ha, "NPIV-Config: Unable to "
976 "allocate memory.\n"));
977 return;
978 }
979
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -0800980 ha->isp_ops->read_optrom(vha, (uint8_t *)data,
Andrew Vasquez272976c2008-09-11 21:22:50 -0700981 ha->flt_region_npiv_conf << 2, NPIV_CONFIG_SIZE);
982
983 cnt = (sizeof(struct qla_npiv_header) + le16_to_cpu(hdr.entries) *
984 sizeof(struct qla_npiv_entry)) >> 1;
985 for (wptr = data, chksum = 0; cnt; cnt--)
986 chksum += le16_to_cpu(*wptr++);
987 if (chksum) {
988 DEBUG2(qla_printk(KERN_INFO, ha, "Inconsistent NPIV-Config "
989 "detected: version=0x%x entries=0x%x checksum=0x%x.\n",
990 le16_to_cpu(hdr.version), le16_to_cpu(hdr.entries),
991 chksum));
992 goto done;
993 }
994
995 entry = data + sizeof(struct qla_npiv_header);
996 cnt = le16_to_cpu(hdr.entries);
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800997 for (i = 0; cnt; cnt--, entry++, i++) {
Andrew Vasquez272976c2008-09-11 21:22:50 -0700998 uint16_t flags;
999 struct fc_vport_identifiers vid;
1000 struct fc_vport *vport;
1001
Anirban Chakraborty40859ae2009-06-03 09:55:16 -07001002 memcpy(&ha->npiv_info[i], entry, sizeof(struct qla_npiv_entry));
1003
Andrew Vasquez272976c2008-09-11 21:22:50 -07001004 flags = le16_to_cpu(entry->flags);
1005 if (flags == 0xffff)
1006 continue;
1007 if ((flags & BIT_0) == 0)
1008 continue;
1009
1010 memset(&vid, 0, sizeof(vid));
1011 vid.roles = FC_PORT_ROLE_FCP_INITIATOR;
1012 vid.vport_type = FC_PORTTYPE_NPIV;
1013 vid.disable = false;
1014 vid.port_name = wwn_to_u64(entry->port_name);
1015 vid.node_name = wwn_to_u64(entry->node_name);
1016
Anirban Chakraborty40859ae2009-06-03 09:55:16 -07001017 DEBUG2(qla_printk(KERN_INFO, ha, "NPIV[%02x]: wwpn=%llx "
Anirban Chakraborty73208df2008-12-09 16:45:39 -08001018 "wwnn=%llx vf_id=0x%x Q_qos=0x%x F_qos=0x%x.\n", cnt,
Randy Dunlap18020ba2009-06-10 12:56:58 -07001019 (unsigned long long)vid.port_name,
1020 (unsigned long long)vid.node_name,
1021 le16_to_cpu(entry->vf_id),
Anirban Chakraborty73208df2008-12-09 16:45:39 -08001022 entry->q_qos, entry->f_qos));
1023
1024 if (i < QLA_PRECONFIG_VPORTS) {
1025 vport = fc_vport_create(vha->host, 0, &vid);
1026 if (!vport)
1027 qla_printk(KERN_INFO, ha,
1028 "NPIV-Config: Failed to create vport [%02x]: "
1029 "wwpn=%llx wwnn=%llx.\n", cnt,
Randy Dunlap18020ba2009-06-10 12:56:58 -07001030 (unsigned long long)vid.port_name,
1031 (unsigned long long)vid.node_name);
Anirban Chakraborty73208df2008-12-09 16:45:39 -08001032 }
Andrew Vasquez272976c2008-09-11 21:22:50 -07001033 }
1034done:
1035 kfree(data);
1036}
1037
Joe Carnuccio1d2874d2009-03-24 09:08:06 -07001038static int
1039qla24xx_unprotect_flash(scsi_qla_host_t *vha)
Andrew Vasquezcb8dacb2008-04-03 13:13:19 -07001040{
Joe Carnuccio1d2874d2009-03-24 09:08:06 -07001041 struct qla_hw_data *ha = vha->hw;
Andrew Vasquezcb8dacb2008-04-03 13:13:19 -07001042 struct device_reg_24xx __iomem *reg = &ha->iobase->isp24;
1043
Joe Carnuccio1d2874d2009-03-24 09:08:06 -07001044 if (ha->flags.fac_supported)
1045 return qla81xx_fac_do_write_enable(vha, 1);
1046
Andrew Vasquezcb8dacb2008-04-03 13:13:19 -07001047 /* Enable flash write. */
1048 WRT_REG_DWORD(&reg->ctrl_status,
1049 RD_REG_DWORD(&reg->ctrl_status) | CSRX_FLASH_ENABLE);
1050 RD_REG_DWORD(&reg->ctrl_status); /* PCI Posting. */
1051
Andrew Vasquez7d232c72008-04-03 13:13:22 -07001052 if (!ha->fdt_wrt_disable)
Joe Carnuccio1d2874d2009-03-24 09:08:06 -07001053 goto done;
Andrew Vasquez7d232c72008-04-03 13:13:22 -07001054
Joe Carnucciob872ca42009-01-22 09:45:36 -08001055 /* Disable flash write-protection, first clear SR protection bit */
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08001056 qla24xx_write_flash_dword(ha, flash_conf_addr(ha, 0x101), 0);
Joe Carnucciob872ca42009-01-22 09:45:36 -08001057 /* Then write zero again to clear remaining SR bits.*/
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08001058 qla24xx_write_flash_dword(ha, flash_conf_addr(ha, 0x101), 0);
Joe Carnuccio1d2874d2009-03-24 09:08:06 -07001059done:
1060 return QLA_SUCCESS;
Andrew Vasquezcb8dacb2008-04-03 13:13:19 -07001061}
1062
Joe Carnuccio1d2874d2009-03-24 09:08:06 -07001063static int
1064qla24xx_protect_flash(scsi_qla_host_t *vha)
Andrew Vasquezcb8dacb2008-04-03 13:13:19 -07001065{
1066 uint32_t cnt;
Joe Carnuccio1d2874d2009-03-24 09:08:06 -07001067 struct qla_hw_data *ha = vha->hw;
Andrew Vasquezcb8dacb2008-04-03 13:13:19 -07001068 struct device_reg_24xx __iomem *reg = &ha->iobase->isp24;
1069
Joe Carnuccio1d2874d2009-03-24 09:08:06 -07001070 if (ha->flags.fac_supported)
1071 return qla81xx_fac_do_write_enable(vha, 0);
1072
Andrew Vasquez7d232c72008-04-03 13:13:22 -07001073 if (!ha->fdt_wrt_disable)
1074 goto skip_wrt_protect;
1075
Andrew Vasquezcb8dacb2008-04-03 13:13:19 -07001076 /* Enable flash write-protection and wait for completion. */
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08001077 qla24xx_write_flash_dword(ha, flash_conf_addr(ha, 0x101),
Andrew Vasquez7d232c72008-04-03 13:13:22 -07001078 ha->fdt_wrt_disable);
Andrew Vasquezcb8dacb2008-04-03 13:13:19 -07001079 for (cnt = 300; cnt &&
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08001080 qla24xx_read_flash_dword(ha, flash_conf_addr(ha, 0x005)) & BIT_0;
Andrew Vasquezcb8dacb2008-04-03 13:13:19 -07001081 cnt--) {
1082 udelay(10);
1083 }
1084
Andrew Vasquez7d232c72008-04-03 13:13:22 -07001085skip_wrt_protect:
Andrew Vasquezcb8dacb2008-04-03 13:13:19 -07001086 /* Disable flash write. */
1087 WRT_REG_DWORD(&reg->ctrl_status,
1088 RD_REG_DWORD(&reg->ctrl_status) & ~CSRX_FLASH_ENABLE);
1089 RD_REG_DWORD(&reg->ctrl_status); /* PCI Posting. */
Joe Carnuccio1d2874d2009-03-24 09:08:06 -07001090
1091 return QLA_SUCCESS;
1092}
1093
1094static int
1095qla24xx_erase_sector(scsi_qla_host_t *vha, uint32_t fdata)
1096{
1097 struct qla_hw_data *ha = vha->hw;
1098 uint32_t start, finish;
1099
1100 if (ha->flags.fac_supported) {
1101 start = fdata >> 2;
1102 finish = start + (ha->fdt_block_size >> 2) - 1;
1103 return qla81xx_fac_erase_sector(vha, flash_data_addr(ha,
1104 start), flash_data_addr(ha, finish));
1105 }
1106
1107 return qla24xx_write_flash_dword(ha, ha->fdt_erase_cmd,
1108 (fdata & 0xff00) | ((fdata << 16) & 0xff0000) |
1109 ((fdata >> 16) & 0xff));
Andrew Vasquezcb8dacb2008-04-03 13:13:19 -07001110}
1111
Adrian Bunke5f82ab2006-11-08 19:55:50 -08001112static int
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -08001113qla24xx_write_flash_data(scsi_qla_host_t *vha, uint32_t *dwptr, uint32_t faddr,
Andrew Vasquez459c5372005-07-06 10:31:07 -07001114 uint32_t dwords)
1115{
1116 int ret;
Andrew Vasquez7c283172009-01-22 09:45:34 -08001117 uint32_t liter;
Andrew Vasquez7d232c72008-04-03 13:13:22 -07001118 uint32_t sec_mask, rest_addr;
Andrew Vasquez85d0acb2009-01-22 09:45:29 -08001119 uint32_t fdata;
Andrew Vasquez338c9162007-09-20 14:07:33 -07001120 dma_addr_t optrom_dma;
1121 void *optrom = NULL;
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -08001122 struct qla_hw_data *ha = vha->hw;
Andrew Vasquez459c5372005-07-06 10:31:07 -07001123
Andrew Vasquez338c9162007-09-20 14:07:33 -07001124 /* Prepare burst-capable write on supported ISPs. */
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08001125 if ((IS_QLA25XX(ha) || IS_QLA81XX(ha)) && !(faddr & 0xfff) &&
Andrew Vasquez338c9162007-09-20 14:07:33 -07001126 dwords > OPTROM_BURST_DWORDS) {
1127 optrom = dma_alloc_coherent(&ha->pdev->dev, OPTROM_BURST_SIZE,
1128 &optrom_dma, GFP_KERNEL);
1129 if (!optrom) {
1130 qla_printk(KERN_DEBUG, ha,
1131 "Unable to allocate memory for optrom burst write "
1132 "(%x KB).\n", OPTROM_BURST_SIZE / 1024);
1133 }
1134 }
1135
Andrew Vasquez7d232c72008-04-03 13:13:22 -07001136 rest_addr = (ha->fdt_block_size >> 2) - 1;
Andrew Vasquez85d0acb2009-01-22 09:45:29 -08001137 sec_mask = ~rest_addr;
Andrew Vasquez459c5372005-07-06 10:31:07 -07001138
Joe Carnuccio1d2874d2009-03-24 09:08:06 -07001139 ret = qla24xx_unprotect_flash(vha);
1140 if (ret != QLA_SUCCESS) {
1141 qla_printk(KERN_WARNING, ha,
1142 "Unable to unprotect flash for update.\n");
1143 goto done;
1144 }
Andrew Vasquez459c5372005-07-06 10:31:07 -07001145
Andrew Vasquez338c9162007-09-20 14:07:33 -07001146 for (liter = 0; liter < dwords; liter++, faddr++, dwptr++) {
Andrew Vasquez85d0acb2009-01-22 09:45:29 -08001147 fdata = (faddr & sec_mask) << 2;
Ravi Anand45aeaf12006-05-17 15:08:49 -07001148
Andrew Vasquez338c9162007-09-20 14:07:33 -07001149 /* Are we at the beginning of a sector? */
Andrew Vasquez85d0acb2009-01-22 09:45:29 -08001150 if ((faddr & rest_addr) == 0) {
Andrew Vasquez7d232c72008-04-03 13:13:22 -07001151 /* Do sector unprotect. */
1152 if (ha->fdt_unprotect_sec_cmd)
Ravi Anand45aeaf12006-05-17 15:08:49 -07001153 qla24xx_write_flash_dword(ha,
Andrew Vasquez7d232c72008-04-03 13:13:22 -07001154 ha->fdt_unprotect_sec_cmd,
Ravi Anand45aeaf12006-05-17 15:08:49 -07001155 (fdata & 0xff00) | ((fdata << 16) &
1156 0xff0000) | ((fdata >> 16) & 0xff));
Joe Carnuccio1d2874d2009-03-24 09:08:06 -07001157 ret = qla24xx_erase_sector(vha, fdata);
Andrew Vasquez338c9162007-09-20 14:07:33 -07001158 if (ret != QLA_SUCCESS) {
Andrew Vasquez76403352009-04-06 22:33:39 -07001159 DEBUG9(qla_printk(KERN_WARNING, ha,
1160 "Unable to erase sector: address=%x.\n",
1161 faddr));
Andrew Vasquez338c9162007-09-20 14:07:33 -07001162 break;
1163 }
Andrew Vasquez459c5372005-07-06 10:31:07 -07001164 }
Andrew Vasquez338c9162007-09-20 14:07:33 -07001165
1166 /* Go with burst-write. */
Andrew Vasquez94d6a2b2007-10-19 15:59:16 -07001167 if (optrom && (liter + OPTROM_BURST_DWORDS) <= dwords) {
Andrew Vasquez338c9162007-09-20 14:07:33 -07001168 /* Copy data to DMA'ble buffer. */
Andrew Vasquez7c283172009-01-22 09:45:34 -08001169 memcpy(optrom, dwptr, OPTROM_BURST_SIZE);
Andrew Vasquez338c9162007-09-20 14:07:33 -07001170
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -08001171 ret = qla2x00_load_ram(vha, optrom_dma,
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08001172 flash_data_addr(ha, faddr),
Andrew Vasquez338c9162007-09-20 14:07:33 -07001173 OPTROM_BURST_DWORDS);
1174 if (ret != QLA_SUCCESS) {
1175 qla_printk(KERN_WARNING, ha,
1176 "Unable to burst-write optrom segment "
1177 "(%x/%x/%llx).\n", ret,
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08001178 flash_data_addr(ha, faddr),
Andrew Morton875baf32007-10-16 14:28:20 -07001179 (unsigned long long)optrom_dma);
Andrew Vasquez338c9162007-09-20 14:07:33 -07001180 qla_printk(KERN_WARNING, ha,
1181 "Reverting to slow-write.\n");
1182
1183 dma_free_coherent(&ha->pdev->dev,
1184 OPTROM_BURST_SIZE, optrom, optrom_dma);
1185 optrom = NULL;
1186 } else {
1187 liter += OPTROM_BURST_DWORDS - 1;
1188 faddr += OPTROM_BURST_DWORDS - 1;
1189 dwptr += OPTROM_BURST_DWORDS - 1;
1190 continue;
1191 }
1192 }
1193
1194 ret = qla24xx_write_flash_dword(ha,
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08001195 flash_data_addr(ha, faddr), cpu_to_le32(*dwptr));
Andrew Vasquez338c9162007-09-20 14:07:33 -07001196 if (ret != QLA_SUCCESS) {
1197 DEBUG9(printk("%s(%ld) Unable to program flash "
1198 "address=%x data=%x.\n", __func__,
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -08001199 vha->host_no, faddr, *dwptr));
Andrew Vasquez338c9162007-09-20 14:07:33 -07001200 break;
1201 }
1202
Andrew Vasquez7d232c72008-04-03 13:13:22 -07001203 /* Do sector protect. */
1204 if (ha->fdt_unprotect_sec_cmd &&
Andrew Vasquez338c9162007-09-20 14:07:33 -07001205 ((faddr & rest_addr) == rest_addr))
1206 qla24xx_write_flash_dword(ha,
Andrew Vasquez7d232c72008-04-03 13:13:22 -07001207 ha->fdt_protect_sec_cmd,
Andrew Vasquez338c9162007-09-20 14:07:33 -07001208 (fdata & 0xff00) | ((fdata << 16) &
1209 0xff0000) | ((fdata >> 16) & 0xff));
1210 }
Andrew Vasquez459c5372005-07-06 10:31:07 -07001211
Joe Carnuccio1d2874d2009-03-24 09:08:06 -07001212 ret = qla24xx_protect_flash(vha);
1213 if (ret != QLA_SUCCESS)
1214 qla_printk(KERN_WARNING, ha,
1215 "Unable to protect flash after update.\n");
1216done:
Andrew Vasquez338c9162007-09-20 14:07:33 -07001217 if (optrom)
1218 dma_free_coherent(&ha->pdev->dev,
1219 OPTROM_BURST_SIZE, optrom, optrom_dma);
1220
Andrew Vasquez459c5372005-07-06 10:31:07 -07001221 return ret;
1222}
1223
1224uint8_t *
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -08001225qla2x00_read_nvram_data(scsi_qla_host_t *vha, uint8_t *buf, uint32_t naddr,
Andrew Vasquez459c5372005-07-06 10:31:07 -07001226 uint32_t bytes)
1227{
1228 uint32_t i;
1229 uint16_t *wptr;
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -08001230 struct qla_hw_data *ha = vha->hw;
Andrew Vasquez459c5372005-07-06 10:31:07 -07001231
1232 /* Word reads to NVRAM via registers. */
1233 wptr = (uint16_t *)buf;
1234 qla2x00_lock_nvram_access(ha);
1235 for (i = 0; i < bytes >> 1; i++, naddr++)
1236 wptr[i] = cpu_to_le16(qla2x00_get_nvram_word(ha,
1237 naddr));
1238 qla2x00_unlock_nvram_access(ha);
1239
1240 return buf;
1241}
1242
1243uint8_t *
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -08001244qla24xx_read_nvram_data(scsi_qla_host_t *vha, uint8_t *buf, uint32_t naddr,
Andrew Vasquez459c5372005-07-06 10:31:07 -07001245 uint32_t bytes)
1246{
1247 uint32_t i;
1248 uint32_t *dwptr;
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08001249 struct qla_hw_data *ha = vha->hw;
Andrew Vasquez459c5372005-07-06 10:31:07 -07001250
Giridhar Malavalia9083012010-04-12 17:59:55 -07001251 if (IS_QLA82XX(ha))
1252 return buf;
1253
Andrew Vasquez459c5372005-07-06 10:31:07 -07001254 /* Dword reads to flash. */
1255 dwptr = (uint32_t *)buf;
1256 for (i = 0; i < bytes >> 2; i++, naddr++)
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08001257 dwptr[i] = cpu_to_le32(qla24xx_read_flash_dword(ha,
1258 nvram_data_addr(ha, naddr)));
Andrew Vasquez459c5372005-07-06 10:31:07 -07001259
Andrew Vasquez459c5372005-07-06 10:31:07 -07001260 return buf;
1261}
1262
1263int
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -08001264qla2x00_write_nvram_data(scsi_qla_host_t *vha, uint8_t *buf, uint32_t naddr,
Andrew Vasquez459c5372005-07-06 10:31:07 -07001265 uint32_t bytes)
1266{
1267 int ret, stat;
1268 uint32_t i;
1269 uint16_t *wptr;
Andrew Vasquez2c96d8d2007-10-19 15:59:15 -07001270 unsigned long flags;
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -08001271 struct qla_hw_data *ha = vha->hw;
Andrew Vasquez459c5372005-07-06 10:31:07 -07001272
1273 ret = QLA_SUCCESS;
1274
Andrew Vasquez2c96d8d2007-10-19 15:59:15 -07001275 spin_lock_irqsave(&ha->hardware_lock, flags);
Andrew Vasquez459c5372005-07-06 10:31:07 -07001276 qla2x00_lock_nvram_access(ha);
1277
1278 /* Disable NVRAM write-protection. */
1279 stat = qla2x00_clear_nvram_protection(ha);
1280
1281 wptr = (uint16_t *)buf;
1282 for (i = 0; i < bytes >> 1; i++, naddr++) {
1283 qla2x00_write_nvram_word(ha, naddr,
1284 cpu_to_le16(*wptr));
1285 wptr++;
1286 }
1287
1288 /* Enable NVRAM write-protection. */
1289 qla2x00_set_nvram_protection(ha, stat);
1290
1291 qla2x00_unlock_nvram_access(ha);
Andrew Vasquez2c96d8d2007-10-19 15:59:15 -07001292 spin_unlock_irqrestore(&ha->hardware_lock, flags);
Andrew Vasquez459c5372005-07-06 10:31:07 -07001293
1294 return ret;
1295}
1296
1297int
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -08001298qla24xx_write_nvram_data(scsi_qla_host_t *vha, uint8_t *buf, uint32_t naddr,
Andrew Vasquez459c5372005-07-06 10:31:07 -07001299 uint32_t bytes)
1300{
1301 int ret;
1302 uint32_t i;
1303 uint32_t *dwptr;
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -08001304 struct qla_hw_data *ha = vha->hw;
Andrew Vasquez459c5372005-07-06 10:31:07 -07001305 struct device_reg_24xx __iomem *reg = &ha->iobase->isp24;
1306
1307 ret = QLA_SUCCESS;
1308
Giridhar Malavalia9083012010-04-12 17:59:55 -07001309 if (IS_QLA82XX(ha))
1310 return ret;
1311
Andrew Vasquez459c5372005-07-06 10:31:07 -07001312 /* Enable flash write. */
1313 WRT_REG_DWORD(&reg->ctrl_status,
1314 RD_REG_DWORD(&reg->ctrl_status) | CSRX_FLASH_ENABLE);
1315 RD_REG_DWORD(&reg->ctrl_status); /* PCI Posting. */
1316
1317 /* Disable NVRAM write-protection. */
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08001318 qla24xx_write_flash_dword(ha, nvram_conf_addr(ha, 0x101), 0);
1319 qla24xx_write_flash_dword(ha, nvram_conf_addr(ha, 0x101), 0);
Andrew Vasquez459c5372005-07-06 10:31:07 -07001320
1321 /* Dword writes to flash. */
1322 dwptr = (uint32_t *)buf;
1323 for (i = 0; i < bytes >> 2; i++, naddr++, dwptr++) {
1324 ret = qla24xx_write_flash_dword(ha,
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08001325 nvram_data_addr(ha, naddr), cpu_to_le32(*dwptr));
Andrew Vasquez459c5372005-07-06 10:31:07 -07001326 if (ret != QLA_SUCCESS) {
Andrew Vasquez76403352009-04-06 22:33:39 -07001327 DEBUG9(qla_printk(KERN_WARNING, ha,
1328 "Unable to program nvram address=%x data=%x.\n",
1329 naddr, *dwptr));
Andrew Vasquez459c5372005-07-06 10:31:07 -07001330 break;
1331 }
1332 }
1333
1334 /* Enable NVRAM write-protection. */
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08001335 qla24xx_write_flash_dword(ha, nvram_conf_addr(ha, 0x101), 0x8c);
Andrew Vasquez459c5372005-07-06 10:31:07 -07001336
1337 /* Disable flash write. */
1338 WRT_REG_DWORD(&reg->ctrl_status,
1339 RD_REG_DWORD(&reg->ctrl_status) & ~CSRX_FLASH_ENABLE);
1340 RD_REG_DWORD(&reg->ctrl_status); /* PCI Posting. */
1341
Andrew Vasquez459c5372005-07-06 10:31:07 -07001342 return ret;
1343}
andrew.vasquez@qlogic.comf6df1442006-01-31 16:05:07 -08001344
Andrew Vasquezc3a2f0d2007-07-19 20:37:34 -07001345uint8_t *
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -08001346qla25xx_read_nvram_data(scsi_qla_host_t *vha, uint8_t *buf, uint32_t naddr,
Andrew Vasquezc3a2f0d2007-07-19 20:37:34 -07001347 uint32_t bytes)
1348{
1349 uint32_t i;
1350 uint32_t *dwptr;
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -08001351 struct qla_hw_data *ha = vha->hw;
Andrew Vasquezc3a2f0d2007-07-19 20:37:34 -07001352
1353 /* Dword reads to flash. */
1354 dwptr = (uint32_t *)buf;
1355 for (i = 0; i < bytes >> 2; i++, naddr++)
1356 dwptr[i] = cpu_to_le32(qla24xx_read_flash_dword(ha,
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08001357 flash_data_addr(ha, ha->flt_region_vpd_nvram | naddr)));
Andrew Vasquezc3a2f0d2007-07-19 20:37:34 -07001358
1359 return buf;
1360}
1361
1362int
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -08001363qla25xx_write_nvram_data(scsi_qla_host_t *vha, uint8_t *buf, uint32_t naddr,
Andrew Vasquezc3a2f0d2007-07-19 20:37:34 -07001364 uint32_t bytes)
1365{
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -08001366 struct qla_hw_data *ha = vha->hw;
Andrew Vasquez2c96d8d2007-10-19 15:59:15 -07001367#define RMW_BUFFER_SIZE (64 * 1024)
1368 uint8_t *dbuf;
1369
1370 dbuf = vmalloc(RMW_BUFFER_SIZE);
1371 if (!dbuf)
1372 return QLA_MEMORY_ALLOC_FAILED;
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -08001373 ha->isp_ops->read_optrom(vha, dbuf, ha->flt_region_vpd_nvram << 2,
Andrew Vasquez2c96d8d2007-10-19 15:59:15 -07001374 RMW_BUFFER_SIZE);
1375 memcpy(dbuf + (naddr << 2), buf, bytes);
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -08001376 ha->isp_ops->write_optrom(vha, dbuf, ha->flt_region_vpd_nvram << 2,
Andrew Vasquez2c96d8d2007-10-19 15:59:15 -07001377 RMW_BUFFER_SIZE);
1378 vfree(dbuf);
1379
1380 return QLA_SUCCESS;
Andrew Vasquezc3a2f0d2007-07-19 20:37:34 -07001381}
andrew.vasquez@qlogic.comf6df1442006-01-31 16:05:07 -08001382
1383static inline void
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -08001384qla2x00_flip_colors(struct qla_hw_data *ha, uint16_t *pflags)
andrew.vasquez@qlogic.comf6df1442006-01-31 16:05:07 -08001385{
1386 if (IS_QLA2322(ha)) {
1387 /* Flip all colors. */
1388 if (ha->beacon_color_state == QLA_LED_ALL_ON) {
1389 /* Turn off. */
1390 ha->beacon_color_state = 0;
1391 *pflags = GPIO_LED_ALL_OFF;
1392 } else {
1393 /* Turn on. */
1394 ha->beacon_color_state = QLA_LED_ALL_ON;
1395 *pflags = GPIO_LED_RGA_ON;
1396 }
1397 } else {
1398 /* Flip green led only. */
1399 if (ha->beacon_color_state == QLA_LED_GRN_ON) {
1400 /* Turn off. */
1401 ha->beacon_color_state = 0;
1402 *pflags = GPIO_LED_GREEN_OFF_AMBER_OFF;
1403 } else {
1404 /* Turn on. */
1405 ha->beacon_color_state = QLA_LED_GRN_ON;
1406 *pflags = GPIO_LED_GREEN_ON_AMBER_OFF;
1407 }
1408 }
1409}
1410
Andrew Vasquez948882f2008-01-31 12:33:44 -08001411#define PIO_REG(h, r) ((h)->pio_address + offsetof(struct device_reg_2xxx, r))
1412
andrew.vasquez@qlogic.comf6df1442006-01-31 16:05:07 -08001413void
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -08001414qla2x00_beacon_blink(struct scsi_qla_host *vha)
andrew.vasquez@qlogic.comf6df1442006-01-31 16:05:07 -08001415{
1416 uint16_t gpio_enable;
1417 uint16_t gpio_data;
1418 uint16_t led_color = 0;
1419 unsigned long flags;
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -08001420 struct qla_hw_data *ha = vha->hw;
andrew.vasquez@qlogic.comf6df1442006-01-31 16:05:07 -08001421 struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
1422
Giridhar Malavalia9083012010-04-12 17:59:55 -07001423 if (IS_QLA82XX(ha))
1424 return;
1425
andrew.vasquez@qlogic.comf6df1442006-01-31 16:05:07 -08001426 spin_lock_irqsave(&ha->hardware_lock, flags);
1427
1428 /* Save the Original GPIOE. */
1429 if (ha->pio_address) {
Andrew Vasquez948882f2008-01-31 12:33:44 -08001430 gpio_enable = RD_REG_WORD_PIO(PIO_REG(ha, gpioe));
1431 gpio_data = RD_REG_WORD_PIO(PIO_REG(ha, gpiod));
andrew.vasquez@qlogic.comf6df1442006-01-31 16:05:07 -08001432 } else {
1433 gpio_enable = RD_REG_WORD(&reg->gpioe);
1434 gpio_data = RD_REG_WORD(&reg->gpiod);
1435 }
1436
1437 /* Set the modified gpio_enable values */
1438 gpio_enable |= GPIO_LED_MASK;
1439
1440 if (ha->pio_address) {
Andrew Vasquez948882f2008-01-31 12:33:44 -08001441 WRT_REG_WORD_PIO(PIO_REG(ha, gpioe), gpio_enable);
andrew.vasquez@qlogic.comf6df1442006-01-31 16:05:07 -08001442 } else {
1443 WRT_REG_WORD(&reg->gpioe, gpio_enable);
1444 RD_REG_WORD(&reg->gpioe);
1445 }
1446
1447 qla2x00_flip_colors(ha, &led_color);
1448
1449 /* Clear out any previously set LED color. */
1450 gpio_data &= ~GPIO_LED_MASK;
1451
1452 /* Set the new input LED color to GPIOD. */
1453 gpio_data |= led_color;
1454
1455 /* Set the modified gpio_data values */
1456 if (ha->pio_address) {
Andrew Vasquez948882f2008-01-31 12:33:44 -08001457 WRT_REG_WORD_PIO(PIO_REG(ha, gpiod), gpio_data);
andrew.vasquez@qlogic.comf6df1442006-01-31 16:05:07 -08001458 } else {
1459 WRT_REG_WORD(&reg->gpiod, gpio_data);
1460 RD_REG_WORD(&reg->gpiod);
1461 }
1462
1463 spin_unlock_irqrestore(&ha->hardware_lock, flags);
1464}
1465
1466int
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -08001467qla2x00_beacon_on(struct scsi_qla_host *vha)
andrew.vasquez@qlogic.comf6df1442006-01-31 16:05:07 -08001468{
1469 uint16_t gpio_enable;
1470 uint16_t gpio_data;
1471 unsigned long flags;
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -08001472 struct qla_hw_data *ha = vha->hw;
andrew.vasquez@qlogic.comf6df1442006-01-31 16:05:07 -08001473 struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
1474
1475 ha->fw_options[1] &= ~FO1_SET_EMPHASIS_SWING;
1476 ha->fw_options[1] |= FO1_DISABLE_GPIO6_7;
1477
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -08001478 if (qla2x00_set_fw_options(vha, ha->fw_options) != QLA_SUCCESS) {
andrew.vasquez@qlogic.comf6df1442006-01-31 16:05:07 -08001479 qla_printk(KERN_WARNING, ha,
1480 "Unable to update fw options (beacon on).\n");
1481 return QLA_FUNCTION_FAILED;
1482 }
1483
andrew.vasquez@qlogic.comf6df1442006-01-31 16:05:07 -08001484 /* Turn off LEDs. */
1485 spin_lock_irqsave(&ha->hardware_lock, flags);
1486 if (ha->pio_address) {
Andrew Vasquez948882f2008-01-31 12:33:44 -08001487 gpio_enable = RD_REG_WORD_PIO(PIO_REG(ha, gpioe));
1488 gpio_data = RD_REG_WORD_PIO(PIO_REG(ha, gpiod));
andrew.vasquez@qlogic.comf6df1442006-01-31 16:05:07 -08001489 } else {
1490 gpio_enable = RD_REG_WORD(&reg->gpioe);
1491 gpio_data = RD_REG_WORD(&reg->gpiod);
1492 }
1493 gpio_enable |= GPIO_LED_MASK;
1494
1495 /* Set the modified gpio_enable values. */
1496 if (ha->pio_address) {
Andrew Vasquez948882f2008-01-31 12:33:44 -08001497 WRT_REG_WORD_PIO(PIO_REG(ha, gpioe), gpio_enable);
andrew.vasquez@qlogic.comf6df1442006-01-31 16:05:07 -08001498 } else {
1499 WRT_REG_WORD(&reg->gpioe, gpio_enable);
1500 RD_REG_WORD(&reg->gpioe);
1501 }
1502
1503 /* Clear out previously set LED colour. */
1504 gpio_data &= ~GPIO_LED_MASK;
1505 if (ha->pio_address) {
Andrew Vasquez948882f2008-01-31 12:33:44 -08001506 WRT_REG_WORD_PIO(PIO_REG(ha, gpiod), gpio_data);
andrew.vasquez@qlogic.comf6df1442006-01-31 16:05:07 -08001507 } else {
1508 WRT_REG_WORD(&reg->gpiod, gpio_data);
1509 RD_REG_WORD(&reg->gpiod);
1510 }
1511 spin_unlock_irqrestore(&ha->hardware_lock, flags);
1512
1513 /*
1514 * Let the per HBA timer kick off the blinking process based on
1515 * the following flags. No need to do anything else now.
1516 */
1517 ha->beacon_blink_led = 1;
1518 ha->beacon_color_state = 0;
1519
1520 return QLA_SUCCESS;
1521}
1522
1523int
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -08001524qla2x00_beacon_off(struct scsi_qla_host *vha)
andrew.vasquez@qlogic.comf6df1442006-01-31 16:05:07 -08001525{
1526 int rval = QLA_SUCCESS;
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -08001527 struct qla_hw_data *ha = vha->hw;
andrew.vasquez@qlogic.comf6df1442006-01-31 16:05:07 -08001528
1529 ha->beacon_blink_led = 0;
1530
1531 /* Set the on flag so when it gets flipped it will be off. */
1532 if (IS_QLA2322(ha))
1533 ha->beacon_color_state = QLA_LED_ALL_ON;
1534 else
1535 ha->beacon_color_state = QLA_LED_GRN_ON;
1536
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -08001537 ha->isp_ops->beacon_blink(vha); /* This turns green LED off */
andrew.vasquez@qlogic.comf6df1442006-01-31 16:05:07 -08001538
1539 ha->fw_options[1] &= ~FO1_SET_EMPHASIS_SWING;
1540 ha->fw_options[1] &= ~FO1_DISABLE_GPIO6_7;
1541
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -08001542 rval = qla2x00_set_fw_options(vha, ha->fw_options);
andrew.vasquez@qlogic.comf6df1442006-01-31 16:05:07 -08001543 if (rval != QLA_SUCCESS)
1544 qla_printk(KERN_WARNING, ha,
1545 "Unable to update fw options (beacon off).\n");
1546 return rval;
1547}
1548
1549
1550static inline void
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -08001551qla24xx_flip_colors(struct qla_hw_data *ha, uint16_t *pflags)
andrew.vasquez@qlogic.comf6df1442006-01-31 16:05:07 -08001552{
1553 /* Flip all colors. */
1554 if (ha->beacon_color_state == QLA_LED_ALL_ON) {
1555 /* Turn off. */
1556 ha->beacon_color_state = 0;
1557 *pflags = 0;
1558 } else {
1559 /* Turn on. */
1560 ha->beacon_color_state = QLA_LED_ALL_ON;
1561 *pflags = GPDX_LED_YELLOW_ON | GPDX_LED_AMBER_ON;
1562 }
1563}
1564
1565void
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -08001566qla24xx_beacon_blink(struct scsi_qla_host *vha)
andrew.vasquez@qlogic.comf6df1442006-01-31 16:05:07 -08001567{
1568 uint16_t led_color = 0;
1569 uint32_t gpio_data;
1570 unsigned long flags;
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -08001571 struct qla_hw_data *ha = vha->hw;
andrew.vasquez@qlogic.comf6df1442006-01-31 16:05:07 -08001572 struct device_reg_24xx __iomem *reg = &ha->iobase->isp24;
1573
1574 /* Save the Original GPIOD. */
1575 spin_lock_irqsave(&ha->hardware_lock, flags);
1576 gpio_data = RD_REG_DWORD(&reg->gpiod);
1577
1578 /* Enable the gpio_data reg for update. */
1579 gpio_data |= GPDX_LED_UPDATE_MASK;
1580
1581 WRT_REG_DWORD(&reg->gpiod, gpio_data);
1582 gpio_data = RD_REG_DWORD(&reg->gpiod);
1583
1584 /* Set the color bits. */
1585 qla24xx_flip_colors(ha, &led_color);
1586
1587 /* Clear out any previously set LED color. */
1588 gpio_data &= ~GPDX_LED_COLOR_MASK;
1589
1590 /* Set the new input LED color to GPIOD. */
1591 gpio_data |= led_color;
1592
1593 /* Set the modified gpio_data values. */
1594 WRT_REG_DWORD(&reg->gpiod, gpio_data);
1595 gpio_data = RD_REG_DWORD(&reg->gpiod);
1596 spin_unlock_irqrestore(&ha->hardware_lock, flags);
1597}
1598
1599int
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -08001600qla24xx_beacon_on(struct scsi_qla_host *vha)
andrew.vasquez@qlogic.comf6df1442006-01-31 16:05:07 -08001601{
1602 uint32_t gpio_data;
1603 unsigned long flags;
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -08001604 struct qla_hw_data *ha = vha->hw;
andrew.vasquez@qlogic.comf6df1442006-01-31 16:05:07 -08001605 struct device_reg_24xx __iomem *reg = &ha->iobase->isp24;
1606
Giridhar Malavalia9083012010-04-12 17:59:55 -07001607 if (IS_QLA82XX(ha))
1608 return QLA_SUCCESS;
1609
andrew.vasquez@qlogic.comf6df1442006-01-31 16:05:07 -08001610 if (ha->beacon_blink_led == 0) {
1611 /* Enable firmware for update */
1612 ha->fw_options[1] |= ADD_FO1_DISABLE_GPIO_LED_CTRL;
1613
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -08001614 if (qla2x00_set_fw_options(vha, ha->fw_options) != QLA_SUCCESS)
andrew.vasquez@qlogic.comf6df1442006-01-31 16:05:07 -08001615 return QLA_FUNCTION_FAILED;
1616
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -08001617 if (qla2x00_get_fw_options(vha, ha->fw_options) !=
andrew.vasquez@qlogic.comf6df1442006-01-31 16:05:07 -08001618 QLA_SUCCESS) {
1619 qla_printk(KERN_WARNING, ha,
1620 "Unable to update fw options (beacon on).\n");
1621 return QLA_FUNCTION_FAILED;
1622 }
1623
1624 spin_lock_irqsave(&ha->hardware_lock, flags);
1625 gpio_data = RD_REG_DWORD(&reg->gpiod);
1626
1627 /* Enable the gpio_data reg for update. */
1628 gpio_data |= GPDX_LED_UPDATE_MASK;
1629 WRT_REG_DWORD(&reg->gpiod, gpio_data);
1630 RD_REG_DWORD(&reg->gpiod);
1631
1632 spin_unlock_irqrestore(&ha->hardware_lock, flags);
1633 }
1634
1635 /* So all colors blink together. */
1636 ha->beacon_color_state = 0;
1637
1638 /* Let the per HBA timer kick off the blinking process. */
1639 ha->beacon_blink_led = 1;
1640
1641 return QLA_SUCCESS;
1642}
1643
1644int
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -08001645qla24xx_beacon_off(struct scsi_qla_host *vha)
andrew.vasquez@qlogic.comf6df1442006-01-31 16:05:07 -08001646{
1647 uint32_t gpio_data;
1648 unsigned long flags;
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -08001649 struct qla_hw_data *ha = vha->hw;
andrew.vasquez@qlogic.comf6df1442006-01-31 16:05:07 -08001650 struct device_reg_24xx __iomem *reg = &ha->iobase->isp24;
1651
Giridhar Malavalia9083012010-04-12 17:59:55 -07001652 if (IS_QLA82XX(ha))
1653 return QLA_SUCCESS;
1654
andrew.vasquez@qlogic.comf6df1442006-01-31 16:05:07 -08001655 ha->beacon_blink_led = 0;
1656 ha->beacon_color_state = QLA_LED_ALL_ON;
1657
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -08001658 ha->isp_ops->beacon_blink(vha); /* Will flip to all off. */
andrew.vasquez@qlogic.comf6df1442006-01-31 16:05:07 -08001659
1660 /* Give control back to firmware. */
1661 spin_lock_irqsave(&ha->hardware_lock, flags);
1662 gpio_data = RD_REG_DWORD(&reg->gpiod);
1663
1664 /* Disable the gpio_data reg for update. */
1665 gpio_data &= ~GPDX_LED_UPDATE_MASK;
1666 WRT_REG_DWORD(&reg->gpiod, gpio_data);
1667 RD_REG_DWORD(&reg->gpiod);
1668 spin_unlock_irqrestore(&ha->hardware_lock, flags);
1669
1670 ha->fw_options[1] &= ~ADD_FO1_DISABLE_GPIO_LED_CTRL;
1671
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -08001672 if (qla2x00_set_fw_options(vha, ha->fw_options) != QLA_SUCCESS) {
andrew.vasquez@qlogic.comf6df1442006-01-31 16:05:07 -08001673 qla_printk(KERN_WARNING, ha,
1674 "Unable to update fw options (beacon off).\n");
1675 return QLA_FUNCTION_FAILED;
1676 }
1677
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -08001678 if (qla2x00_get_fw_options(vha, ha->fw_options) != QLA_SUCCESS) {
andrew.vasquez@qlogic.comf6df1442006-01-31 16:05:07 -08001679 qla_printk(KERN_WARNING, ha,
1680 "Unable to get fw options (beacon off).\n");
1681 return QLA_FUNCTION_FAILED;
1682 }
1683
1684 return QLA_SUCCESS;
1685}
andrew.vasquez@qlogic.com854165f2006-01-31 16:05:17 -08001686
1687
1688/*
1689 * Flash support routines
1690 */
1691
1692/**
1693 * qla2x00_flash_enable() - Setup flash for reading and writing.
1694 * @ha: HA context
1695 */
1696static void
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -08001697qla2x00_flash_enable(struct qla_hw_data *ha)
andrew.vasquez@qlogic.com854165f2006-01-31 16:05:17 -08001698{
1699 uint16_t data;
1700 struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
1701
1702 data = RD_REG_WORD(&reg->ctrl_status);
1703 data |= CSR_FLASH_ENABLE;
1704 WRT_REG_WORD(&reg->ctrl_status, data);
1705 RD_REG_WORD(&reg->ctrl_status); /* PCI Posting. */
1706}
1707
1708/**
1709 * qla2x00_flash_disable() - Disable flash and allow RISC to run.
1710 * @ha: HA context
1711 */
1712static void
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -08001713qla2x00_flash_disable(struct qla_hw_data *ha)
andrew.vasquez@qlogic.com854165f2006-01-31 16:05:17 -08001714{
1715 uint16_t data;
1716 struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
1717
1718 data = RD_REG_WORD(&reg->ctrl_status);
1719 data &= ~(CSR_FLASH_ENABLE);
1720 WRT_REG_WORD(&reg->ctrl_status, data);
1721 RD_REG_WORD(&reg->ctrl_status); /* PCI Posting. */
1722}
1723
1724/**
1725 * qla2x00_read_flash_byte() - Reads a byte from flash
1726 * @ha: HA context
1727 * @addr: Address in flash to read
1728 *
1729 * A word is read from the chip, but, only the lower byte is valid.
1730 *
1731 * Returns the byte read from flash @addr.
1732 */
1733static uint8_t
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -08001734qla2x00_read_flash_byte(struct qla_hw_data *ha, uint32_t addr)
andrew.vasquez@qlogic.com854165f2006-01-31 16:05:17 -08001735{
1736 uint16_t data;
1737 uint16_t bank_select;
1738 struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
1739
1740 bank_select = RD_REG_WORD(&reg->ctrl_status);
1741
1742 if (IS_QLA2322(ha) || IS_QLA6322(ha)) {
1743 /* Specify 64K address range: */
1744 /* clear out Module Select and Flash Address bits [19:16]. */
1745 bank_select &= ~0xf8;
1746 bank_select |= addr >> 12 & 0xf0;
1747 bank_select |= CSR_FLASH_64K_BANK;
1748 WRT_REG_WORD(&reg->ctrl_status, bank_select);
1749 RD_REG_WORD(&reg->ctrl_status); /* PCI Posting. */
1750
1751 WRT_REG_WORD(&reg->flash_address, (uint16_t)addr);
1752 data = RD_REG_WORD(&reg->flash_data);
1753
1754 return (uint8_t)data;
1755 }
1756
1757 /* Setup bit 16 of flash address. */
1758 if ((addr & BIT_16) && ((bank_select & CSR_FLASH_64K_BANK) == 0)) {
1759 bank_select |= CSR_FLASH_64K_BANK;
1760 WRT_REG_WORD(&reg->ctrl_status, bank_select);
1761 RD_REG_WORD(&reg->ctrl_status); /* PCI Posting. */
1762 } else if (((addr & BIT_16) == 0) &&
1763 (bank_select & CSR_FLASH_64K_BANK)) {
1764 bank_select &= ~(CSR_FLASH_64K_BANK);
1765 WRT_REG_WORD(&reg->ctrl_status, bank_select);
1766 RD_REG_WORD(&reg->ctrl_status); /* PCI Posting. */
1767 }
1768
1769 /* Always perform IO mapped accesses to the FLASH registers. */
1770 if (ha->pio_address) {
1771 uint16_t data2;
1772
Andrew Vasquez948882f2008-01-31 12:33:44 -08001773 WRT_REG_WORD_PIO(PIO_REG(ha, flash_address), (uint16_t)addr);
andrew.vasquez@qlogic.com854165f2006-01-31 16:05:17 -08001774 do {
Andrew Vasquez948882f2008-01-31 12:33:44 -08001775 data = RD_REG_WORD_PIO(PIO_REG(ha, flash_data));
andrew.vasquez@qlogic.com854165f2006-01-31 16:05:17 -08001776 barrier();
1777 cpu_relax();
Andrew Vasquez948882f2008-01-31 12:33:44 -08001778 data2 = RD_REG_WORD_PIO(PIO_REG(ha, flash_data));
andrew.vasquez@qlogic.com854165f2006-01-31 16:05:17 -08001779 } while (data != data2);
1780 } else {
1781 WRT_REG_WORD(&reg->flash_address, (uint16_t)addr);
1782 data = qla2x00_debounce_register(&reg->flash_data);
1783 }
1784
1785 return (uint8_t)data;
1786}
1787
1788/**
1789 * qla2x00_write_flash_byte() - Write a byte to flash
1790 * @ha: HA context
1791 * @addr: Address in flash to write
1792 * @data: Data to write
1793 */
1794static void
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -08001795qla2x00_write_flash_byte(struct qla_hw_data *ha, uint32_t addr, uint8_t data)
andrew.vasquez@qlogic.com854165f2006-01-31 16:05:17 -08001796{
1797 uint16_t bank_select;
1798 struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
1799
1800 bank_select = RD_REG_WORD(&reg->ctrl_status);
1801 if (IS_QLA2322(ha) || IS_QLA6322(ha)) {
1802 /* Specify 64K address range: */
1803 /* clear out Module Select and Flash Address bits [19:16]. */
1804 bank_select &= ~0xf8;
1805 bank_select |= addr >> 12 & 0xf0;
1806 bank_select |= CSR_FLASH_64K_BANK;
1807 WRT_REG_WORD(&reg->ctrl_status, bank_select);
1808 RD_REG_WORD(&reg->ctrl_status); /* PCI Posting. */
1809
1810 WRT_REG_WORD(&reg->flash_address, (uint16_t)addr);
1811 RD_REG_WORD(&reg->ctrl_status); /* PCI Posting. */
1812 WRT_REG_WORD(&reg->flash_data, (uint16_t)data);
1813 RD_REG_WORD(&reg->ctrl_status); /* PCI Posting. */
1814
1815 return;
1816 }
1817
1818 /* Setup bit 16 of flash address. */
1819 if ((addr & BIT_16) && ((bank_select & CSR_FLASH_64K_BANK) == 0)) {
1820 bank_select |= CSR_FLASH_64K_BANK;
1821 WRT_REG_WORD(&reg->ctrl_status, bank_select);
1822 RD_REG_WORD(&reg->ctrl_status); /* PCI Posting. */
1823 } else if (((addr & BIT_16) == 0) &&
1824 (bank_select & CSR_FLASH_64K_BANK)) {
1825 bank_select &= ~(CSR_FLASH_64K_BANK);
1826 WRT_REG_WORD(&reg->ctrl_status, bank_select);
1827 RD_REG_WORD(&reg->ctrl_status); /* PCI Posting. */
1828 }
1829
1830 /* Always perform IO mapped accesses to the FLASH registers. */
1831 if (ha->pio_address) {
Andrew Vasquez948882f2008-01-31 12:33:44 -08001832 WRT_REG_WORD_PIO(PIO_REG(ha, flash_address), (uint16_t)addr);
1833 WRT_REG_WORD_PIO(PIO_REG(ha, flash_data), (uint16_t)data);
andrew.vasquez@qlogic.com854165f2006-01-31 16:05:17 -08001834 } else {
1835 WRT_REG_WORD(&reg->flash_address, (uint16_t)addr);
1836 RD_REG_WORD(&reg->ctrl_status); /* PCI Posting. */
1837 WRT_REG_WORD(&reg->flash_data, (uint16_t)data);
1838 RD_REG_WORD(&reg->ctrl_status); /* PCI Posting. */
1839 }
1840}
1841
1842/**
1843 * qla2x00_poll_flash() - Polls flash for completion.
1844 * @ha: HA context
1845 * @addr: Address in flash to poll
1846 * @poll_data: Data to be polled
1847 * @man_id: Flash manufacturer ID
1848 * @flash_id: Flash ID
1849 *
1850 * This function polls the device until bit 7 of what is read matches data
1851 * bit 7 or until data bit 5 becomes a 1. If that hapens, the flash ROM timed
1852 * out (a fatal error). The flash book recommeds reading bit 7 again after
1853 * reading bit 5 as a 1.
1854 *
1855 * Returns 0 on success, else non-zero.
1856 */
1857static int
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -08001858qla2x00_poll_flash(struct qla_hw_data *ha, uint32_t addr, uint8_t poll_data,
andrew.vasquez@qlogic.com854165f2006-01-31 16:05:17 -08001859 uint8_t man_id, uint8_t flash_id)
1860{
1861 int status;
1862 uint8_t flash_data;
1863 uint32_t cnt;
1864
1865 status = 1;
1866
1867 /* Wait for 30 seconds for command to finish. */
1868 poll_data &= BIT_7;
1869 for (cnt = 3000000; cnt; cnt--) {
1870 flash_data = qla2x00_read_flash_byte(ha, addr);
1871 if ((flash_data & BIT_7) == poll_data) {
1872 status = 0;
1873 break;
1874 }
1875
1876 if (man_id != 0x40 && man_id != 0xda) {
1877 if ((flash_data & BIT_5) && cnt > 2)
1878 cnt = 2;
1879 }
1880 udelay(10);
1881 barrier();
Andrew Vasquez40a2e342007-03-12 10:41:28 -07001882 cond_resched();
andrew.vasquez@qlogic.com854165f2006-01-31 16:05:17 -08001883 }
1884 return status;
1885}
1886
andrew.vasquez@qlogic.com854165f2006-01-31 16:05:17 -08001887/**
1888 * qla2x00_program_flash_address() - Programs a flash address
1889 * @ha: HA context
1890 * @addr: Address in flash to program
1891 * @data: Data to be written in flash
1892 * @man_id: Flash manufacturer ID
1893 * @flash_id: Flash ID
1894 *
1895 * Returns 0 on success, else non-zero.
1896 */
1897static int
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -08001898qla2x00_program_flash_address(struct qla_hw_data *ha, uint32_t addr,
1899 uint8_t data, uint8_t man_id, uint8_t flash_id)
andrew.vasquez@qlogic.com854165f2006-01-31 16:05:17 -08001900{
1901 /* Write Program Command Sequence. */
1902 if (IS_OEM_001(ha)) {
1903 qla2x00_write_flash_byte(ha, 0xaaa, 0xaa);
1904 qla2x00_write_flash_byte(ha, 0x555, 0x55);
1905 qla2x00_write_flash_byte(ha, 0xaaa, 0xa0);
1906 qla2x00_write_flash_byte(ha, addr, data);
1907 } else {
1908 if (man_id == 0xda && flash_id == 0xc1) {
1909 qla2x00_write_flash_byte(ha, addr, data);
1910 if (addr & 0x7e)
1911 return 0;
1912 } else {
1913 qla2x00_write_flash_byte(ha, 0x5555, 0xaa);
1914 qla2x00_write_flash_byte(ha, 0x2aaa, 0x55);
1915 qla2x00_write_flash_byte(ha, 0x5555, 0xa0);
1916 qla2x00_write_flash_byte(ha, addr, data);
1917 }
1918 }
1919
1920 udelay(150);
1921
1922 /* Wait for write to complete. */
1923 return qla2x00_poll_flash(ha, addr, data, man_id, flash_id);
1924}
1925
1926/**
1927 * qla2x00_erase_flash() - Erase the flash.
1928 * @ha: HA context
1929 * @man_id: Flash manufacturer ID
1930 * @flash_id: Flash ID
1931 *
1932 * Returns 0 on success, else non-zero.
1933 */
1934static int
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -08001935qla2x00_erase_flash(struct qla_hw_data *ha, uint8_t man_id, uint8_t flash_id)
andrew.vasquez@qlogic.com854165f2006-01-31 16:05:17 -08001936{
1937 /* Individual Sector Erase Command Sequence */
1938 if (IS_OEM_001(ha)) {
1939 qla2x00_write_flash_byte(ha, 0xaaa, 0xaa);
1940 qla2x00_write_flash_byte(ha, 0x555, 0x55);
1941 qla2x00_write_flash_byte(ha, 0xaaa, 0x80);
1942 qla2x00_write_flash_byte(ha, 0xaaa, 0xaa);
1943 qla2x00_write_flash_byte(ha, 0x555, 0x55);
1944 qla2x00_write_flash_byte(ha, 0xaaa, 0x10);
1945 } else {
1946 qla2x00_write_flash_byte(ha, 0x5555, 0xaa);
1947 qla2x00_write_flash_byte(ha, 0x2aaa, 0x55);
1948 qla2x00_write_flash_byte(ha, 0x5555, 0x80);
1949 qla2x00_write_flash_byte(ha, 0x5555, 0xaa);
1950 qla2x00_write_flash_byte(ha, 0x2aaa, 0x55);
1951 qla2x00_write_flash_byte(ha, 0x5555, 0x10);
1952 }
1953
1954 udelay(150);
1955
1956 /* Wait for erase to complete. */
1957 return qla2x00_poll_flash(ha, 0x00, 0x80, man_id, flash_id);
1958}
1959
1960/**
1961 * qla2x00_erase_flash_sector() - Erase a flash sector.
1962 * @ha: HA context
1963 * @addr: Flash sector to erase
1964 * @sec_mask: Sector address mask
1965 * @man_id: Flash manufacturer ID
1966 * @flash_id: Flash ID
1967 *
1968 * Returns 0 on success, else non-zero.
1969 */
1970static int
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -08001971qla2x00_erase_flash_sector(struct qla_hw_data *ha, uint32_t addr,
andrew.vasquez@qlogic.com854165f2006-01-31 16:05:17 -08001972 uint32_t sec_mask, uint8_t man_id, uint8_t flash_id)
1973{
1974 /* Individual Sector Erase Command Sequence */
1975 qla2x00_write_flash_byte(ha, 0x5555, 0xaa);
1976 qla2x00_write_flash_byte(ha, 0x2aaa, 0x55);
1977 qla2x00_write_flash_byte(ha, 0x5555, 0x80);
1978 qla2x00_write_flash_byte(ha, 0x5555, 0xaa);
1979 qla2x00_write_flash_byte(ha, 0x2aaa, 0x55);
1980 if (man_id == 0x1f && flash_id == 0x13)
1981 qla2x00_write_flash_byte(ha, addr & sec_mask, 0x10);
1982 else
1983 qla2x00_write_flash_byte(ha, addr & sec_mask, 0x30);
1984
1985 udelay(150);
1986
1987 /* Wait for erase to complete. */
1988 return qla2x00_poll_flash(ha, addr, 0x80, man_id, flash_id);
1989}
1990
1991/**
1992 * qla2x00_get_flash_manufacturer() - Read manufacturer ID from flash chip.
1993 * @man_id: Flash manufacturer ID
1994 * @flash_id: Flash ID
1995 */
1996static void
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -08001997qla2x00_get_flash_manufacturer(struct qla_hw_data *ha, uint8_t *man_id,
andrew.vasquez@qlogic.com854165f2006-01-31 16:05:17 -08001998 uint8_t *flash_id)
1999{
2000 qla2x00_write_flash_byte(ha, 0x5555, 0xaa);
2001 qla2x00_write_flash_byte(ha, 0x2aaa, 0x55);
2002 qla2x00_write_flash_byte(ha, 0x5555, 0x90);
2003 *man_id = qla2x00_read_flash_byte(ha, 0x0000);
2004 *flash_id = qla2x00_read_flash_byte(ha, 0x0001);
2005 qla2x00_write_flash_byte(ha, 0x5555, 0xaa);
2006 qla2x00_write_flash_byte(ha, 0x2aaa, 0x55);
2007 qla2x00_write_flash_byte(ha, 0x5555, 0xf0);
2008}
2009
Andrew Vasquez30c47662007-01-29 10:22:21 -08002010static void
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -08002011qla2x00_read_flash_data(struct qla_hw_data *ha, uint8_t *tmp_buf,
2012 uint32_t saddr, uint32_t length)
Andrew Vasquez30c47662007-01-29 10:22:21 -08002013{
2014 struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
2015 uint32_t midpoint, ilength;
2016 uint8_t data;
2017
2018 midpoint = length / 2;
2019
2020 WRT_REG_WORD(&reg->nvram, 0);
2021 RD_REG_WORD(&reg->nvram);
2022 for (ilength = 0; ilength < length; saddr++, ilength++, tmp_buf++) {
2023 if (ilength == midpoint) {
2024 WRT_REG_WORD(&reg->nvram, NVR_SELECT);
2025 RD_REG_WORD(&reg->nvram);
2026 }
2027 data = qla2x00_read_flash_byte(ha, saddr);
2028 if (saddr % 100)
2029 udelay(10);
2030 *tmp_buf = data;
Andrew Vasquez40a2e342007-03-12 10:41:28 -07002031 cond_resched();
Andrew Vasquez30c47662007-01-29 10:22:21 -08002032 }
2033}
andrew.vasquez@qlogic.com854165f2006-01-31 16:05:17 -08002034
2035static inline void
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -08002036qla2x00_suspend_hba(struct scsi_qla_host *vha)
andrew.vasquez@qlogic.com854165f2006-01-31 16:05:17 -08002037{
2038 int cnt;
2039 unsigned long flags;
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -08002040 struct qla_hw_data *ha = vha->hw;
andrew.vasquez@qlogic.com854165f2006-01-31 16:05:17 -08002041 struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
2042
2043 /* Suspend HBA. */
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -08002044 scsi_block_requests(vha->host);
Andrew Vasquezfd34f552007-07-19 15:06:00 -07002045 ha->isp_ops->disable_intrs(ha);
andrew.vasquez@qlogic.com854165f2006-01-31 16:05:17 -08002046 set_bit(MBX_UPDATE_FLASH_ACTIVE, &ha->mbx_cmd_flags);
2047
2048 /* Pause RISC. */
2049 spin_lock_irqsave(&ha->hardware_lock, flags);
2050 WRT_REG_WORD(&reg->hccr, HCCR_PAUSE_RISC);
2051 RD_REG_WORD(&reg->hccr);
2052 if (IS_QLA2100(ha) || IS_QLA2200(ha) || IS_QLA2300(ha)) {
2053 for (cnt = 0; cnt < 30000; cnt++) {
2054 if ((RD_REG_WORD(&reg->hccr) & HCCR_RISC_PAUSE) != 0)
2055 break;
2056 udelay(100);
2057 }
2058 } else {
2059 udelay(10);
2060 }
2061 spin_unlock_irqrestore(&ha->hardware_lock, flags);
2062}
2063
2064static inline void
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -08002065qla2x00_resume_hba(struct scsi_qla_host *vha)
andrew.vasquez@qlogic.com854165f2006-01-31 16:05:17 -08002066{
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -08002067 struct qla_hw_data *ha = vha->hw;
2068
andrew.vasquez@qlogic.com854165f2006-01-31 16:05:17 -08002069 /* Resume HBA. */
2070 clear_bit(MBX_UPDATE_FLASH_ACTIVE, &ha->mbx_cmd_flags);
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -08002071 set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
2072 qla2xxx_wake_dpc(vha);
Lalit Chandivade2533cf62009-03-24 09:08:07 -07002073 qla2x00_wait_for_chip_reset(vha);
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -08002074 scsi_unblock_requests(vha->host);
andrew.vasquez@qlogic.com854165f2006-01-31 16:05:17 -08002075}
2076
2077uint8_t *
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -08002078qla2x00_read_optrom_data(struct scsi_qla_host *vha, uint8_t *buf,
andrew.vasquez@qlogic.com854165f2006-01-31 16:05:17 -08002079 uint32_t offset, uint32_t length)
2080{
andrew.vasquez@qlogic.com854165f2006-01-31 16:05:17 -08002081 uint32_t addr, midpoint;
2082 uint8_t *data;
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -08002083 struct qla_hw_data *ha = vha->hw;
andrew.vasquez@qlogic.com854165f2006-01-31 16:05:17 -08002084 struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
2085
2086 /* Suspend HBA. */
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -08002087 qla2x00_suspend_hba(vha);
andrew.vasquez@qlogic.com854165f2006-01-31 16:05:17 -08002088
2089 /* Go with read. */
andrew.vasquez@qlogic.com854165f2006-01-31 16:05:17 -08002090 midpoint = ha->optrom_size / 2;
2091
2092 qla2x00_flash_enable(ha);
2093 WRT_REG_WORD(&reg->nvram, 0);
2094 RD_REG_WORD(&reg->nvram); /* PCI Posting. */
2095 for (addr = offset, data = buf; addr < length; addr++, data++) {
2096 if (addr == midpoint) {
2097 WRT_REG_WORD(&reg->nvram, NVR_SELECT);
2098 RD_REG_WORD(&reg->nvram); /* PCI Posting. */
2099 }
2100
2101 *data = qla2x00_read_flash_byte(ha, addr);
2102 }
2103 qla2x00_flash_disable(ha);
andrew.vasquez@qlogic.com854165f2006-01-31 16:05:17 -08002104
2105 /* Resume HBA. */
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -08002106 qla2x00_resume_hba(vha);
andrew.vasquez@qlogic.com854165f2006-01-31 16:05:17 -08002107
2108 return buf;
2109}
2110
2111int
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -08002112qla2x00_write_optrom_data(struct scsi_qla_host *vha, uint8_t *buf,
andrew.vasquez@qlogic.com854165f2006-01-31 16:05:17 -08002113 uint32_t offset, uint32_t length)
2114{
2115
2116 int rval;
andrew.vasquez@qlogic.com854165f2006-01-31 16:05:17 -08002117 uint8_t man_id, flash_id, sec_number, data;
2118 uint16_t wd;
2119 uint32_t addr, liter, sec_mask, rest_addr;
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -08002120 struct qla_hw_data *ha = vha->hw;
andrew.vasquez@qlogic.com854165f2006-01-31 16:05:17 -08002121 struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
2122
2123 /* Suspend HBA. */
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -08002124 qla2x00_suspend_hba(vha);
andrew.vasquez@qlogic.com854165f2006-01-31 16:05:17 -08002125
2126 rval = QLA_SUCCESS;
2127 sec_number = 0;
2128
2129 /* Reset ISP chip. */
andrew.vasquez@qlogic.com854165f2006-01-31 16:05:17 -08002130 WRT_REG_WORD(&reg->ctrl_status, CSR_ISP_SOFT_RESET);
2131 pci_read_config_word(ha->pdev, PCI_COMMAND, &wd);
2132
2133 /* Go with write. */
2134 qla2x00_flash_enable(ha);
2135 do { /* Loop once to provide quick error exit */
2136 /* Structure of flash memory based on manufacturer */
2137 if (IS_OEM_001(ha)) {
2138 /* OEM variant with special flash part. */
2139 man_id = flash_id = 0;
2140 rest_addr = 0xffff;
2141 sec_mask = 0x10000;
2142 goto update_flash;
2143 }
2144 qla2x00_get_flash_manufacturer(ha, &man_id, &flash_id);
2145 switch (man_id) {
2146 case 0x20: /* ST flash. */
2147 if (flash_id == 0xd2 || flash_id == 0xe3) {
2148 /*
2149 * ST m29w008at part - 64kb sector size with
2150 * 32kb,8kb,8kb,16kb sectors at memory address
2151 * 0xf0000.
2152 */
2153 rest_addr = 0xffff;
2154 sec_mask = 0x10000;
2155 break;
2156 }
2157 /*
2158 * ST m29w010b part - 16kb sector size
2159 * Default to 16kb sectors
2160 */
2161 rest_addr = 0x3fff;
2162 sec_mask = 0x1c000;
2163 break;
2164 case 0x40: /* Mostel flash. */
2165 /* Mostel v29c51001 part - 512 byte sector size. */
2166 rest_addr = 0x1ff;
2167 sec_mask = 0x1fe00;
2168 break;
2169 case 0xbf: /* SST flash. */
2170 /* SST39sf10 part - 4kb sector size. */
2171 rest_addr = 0xfff;
2172 sec_mask = 0x1f000;
2173 break;
2174 case 0xda: /* Winbond flash. */
2175 /* Winbond W29EE011 part - 256 byte sector size. */
2176 rest_addr = 0x7f;
2177 sec_mask = 0x1ff80;
2178 break;
2179 case 0xc2: /* Macronix flash. */
2180 /* 64k sector size. */
2181 if (flash_id == 0x38 || flash_id == 0x4f) {
2182 rest_addr = 0xffff;
2183 sec_mask = 0x10000;
2184 break;
2185 }
2186 /* Fall through... */
2187
2188 case 0x1f: /* Atmel flash. */
2189 /* 512k sector size. */
2190 if (flash_id == 0x13) {
2191 rest_addr = 0x7fffffff;
2192 sec_mask = 0x80000000;
2193 break;
2194 }
2195 /* Fall through... */
2196
2197 case 0x01: /* AMD flash. */
2198 if (flash_id == 0x38 || flash_id == 0x40 ||
2199 flash_id == 0x4f) {
2200 /* Am29LV081 part - 64kb sector size. */
2201 /* Am29LV002BT part - 64kb sector size. */
2202 rest_addr = 0xffff;
2203 sec_mask = 0x10000;
2204 break;
2205 } else if (flash_id == 0x3e) {
2206 /*
2207 * Am29LV008b part - 64kb sector size with
2208 * 32kb,8kb,8kb,16kb sector at memory address
2209 * h0xf0000.
2210 */
2211 rest_addr = 0xffff;
2212 sec_mask = 0x10000;
2213 break;
2214 } else if (flash_id == 0x20 || flash_id == 0x6e) {
2215 /*
2216 * Am29LV010 part or AM29f010 - 16kb sector
2217 * size.
2218 */
2219 rest_addr = 0x3fff;
2220 sec_mask = 0x1c000;
2221 break;
2222 } else if (flash_id == 0x6d) {
2223 /* Am29LV001 part - 8kb sector size. */
2224 rest_addr = 0x1fff;
2225 sec_mask = 0x1e000;
2226 break;
2227 }
2228 default:
2229 /* Default to 16 kb sector size. */
2230 rest_addr = 0x3fff;
2231 sec_mask = 0x1c000;
2232 break;
2233 }
2234
2235update_flash:
2236 if (IS_QLA2322(ha) || IS_QLA6322(ha)) {
2237 if (qla2x00_erase_flash(ha, man_id, flash_id)) {
2238 rval = QLA_FUNCTION_FAILED;
2239 break;
2240 }
2241 }
2242
2243 for (addr = offset, liter = 0; liter < length; liter++,
2244 addr++) {
2245 data = buf[liter];
2246 /* Are we at the beginning of a sector? */
2247 if ((addr & rest_addr) == 0) {
2248 if (IS_QLA2322(ha) || IS_QLA6322(ha)) {
2249 if (addr >= 0x10000UL) {
2250 if (((addr >> 12) & 0xf0) &&
2251 ((man_id == 0x01 &&
2252 flash_id == 0x3e) ||
2253 (man_id == 0x20 &&
2254 flash_id == 0xd2))) {
2255 sec_number++;
2256 if (sec_number == 1) {
2257 rest_addr =
2258 0x7fff;
2259 sec_mask =
2260 0x18000;
2261 } else if (
2262 sec_number == 2 ||
2263 sec_number == 3) {
2264 rest_addr =
2265 0x1fff;
2266 sec_mask =
2267 0x1e000;
2268 } else if (
2269 sec_number == 4) {
2270 rest_addr =
2271 0x3fff;
2272 sec_mask =
2273 0x1c000;
2274 }
2275 }
2276 }
2277 } else if (addr == ha->optrom_size / 2) {
2278 WRT_REG_WORD(&reg->nvram, NVR_SELECT);
2279 RD_REG_WORD(&reg->nvram);
2280 }
2281
2282 if (flash_id == 0xda && man_id == 0xc1) {
2283 qla2x00_write_flash_byte(ha, 0x5555,
2284 0xaa);
2285 qla2x00_write_flash_byte(ha, 0x2aaa,
2286 0x55);
2287 qla2x00_write_flash_byte(ha, 0x5555,
2288 0xa0);
2289 } else if (!IS_QLA2322(ha) && !IS_QLA6322(ha)) {
2290 /* Then erase it */
2291 if (qla2x00_erase_flash_sector(ha,
2292 addr, sec_mask, man_id,
2293 flash_id)) {
2294 rval = QLA_FUNCTION_FAILED;
2295 break;
2296 }
2297 if (man_id == 0x01 && flash_id == 0x6d)
2298 sec_number++;
2299 }
2300 }
2301
2302 if (man_id == 0x01 && flash_id == 0x6d) {
2303 if (sec_number == 1 &&
2304 addr == (rest_addr - 1)) {
2305 rest_addr = 0x0fff;
2306 sec_mask = 0x1f000;
2307 } else if (sec_number == 3 && (addr & 0x7ffe)) {
2308 rest_addr = 0x3fff;
2309 sec_mask = 0x1c000;
2310 }
2311 }
2312
2313 if (qla2x00_program_flash_address(ha, addr, data,
2314 man_id, flash_id)) {
2315 rval = QLA_FUNCTION_FAILED;
2316 break;
2317 }
Andrew Vasquez40a2e342007-03-12 10:41:28 -07002318 cond_resched();
andrew.vasquez@qlogic.com854165f2006-01-31 16:05:17 -08002319 }
2320 } while (0);
2321 qla2x00_flash_disable(ha);
andrew.vasquez@qlogic.com854165f2006-01-31 16:05:17 -08002322
2323 /* Resume HBA. */
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -08002324 qla2x00_resume_hba(vha);
andrew.vasquez@qlogic.com854165f2006-01-31 16:05:17 -08002325
2326 return rval;
2327}
2328
2329uint8_t *
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -08002330qla24xx_read_optrom_data(struct scsi_qla_host *vha, uint8_t *buf,
andrew.vasquez@qlogic.com854165f2006-01-31 16:05:17 -08002331 uint32_t offset, uint32_t length)
2332{
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -08002333 struct qla_hw_data *ha = vha->hw;
2334
andrew.vasquez@qlogic.com854165f2006-01-31 16:05:17 -08002335 /* Suspend HBA. */
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -08002336 scsi_block_requests(vha->host);
andrew.vasquez@qlogic.com854165f2006-01-31 16:05:17 -08002337 set_bit(MBX_UPDATE_FLASH_ACTIVE, &ha->mbx_cmd_flags);
2338
2339 /* Go with read. */
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -08002340 qla24xx_read_flash_data(vha, (uint32_t *)buf, offset >> 2, length >> 2);
andrew.vasquez@qlogic.com854165f2006-01-31 16:05:17 -08002341
2342 /* Resume HBA. */
2343 clear_bit(MBX_UPDATE_FLASH_ACTIVE, &ha->mbx_cmd_flags);
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -08002344 scsi_unblock_requests(vha->host);
andrew.vasquez@qlogic.com854165f2006-01-31 16:05:17 -08002345
2346 return buf;
2347}
2348
2349int
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -08002350qla24xx_write_optrom_data(struct scsi_qla_host *vha, uint8_t *buf,
andrew.vasquez@qlogic.com854165f2006-01-31 16:05:17 -08002351 uint32_t offset, uint32_t length)
2352{
2353 int rval;
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -08002354 struct qla_hw_data *ha = vha->hw;
andrew.vasquez@qlogic.com854165f2006-01-31 16:05:17 -08002355
2356 /* Suspend HBA. */
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -08002357 scsi_block_requests(vha->host);
andrew.vasquez@qlogic.com854165f2006-01-31 16:05:17 -08002358 set_bit(MBX_UPDATE_FLASH_ACTIVE, &ha->mbx_cmd_flags);
2359
2360 /* Go with write. */
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -08002361 rval = qla24xx_write_flash_data(vha, (uint32_t *)buf, offset >> 2,
andrew.vasquez@qlogic.com854165f2006-01-31 16:05:17 -08002362 length >> 2);
2363
andrew.vasquez@qlogic.com854165f2006-01-31 16:05:17 -08002364 clear_bit(MBX_UPDATE_FLASH_ACTIVE, &ha->mbx_cmd_flags);
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -08002365 scsi_unblock_requests(vha->host);
andrew.vasquez@qlogic.com854165f2006-01-31 16:05:17 -08002366
2367 return rval;
2368}
Andrew Vasquez30c47662007-01-29 10:22:21 -08002369
Andrew Vasquez338c9162007-09-20 14:07:33 -07002370uint8_t *
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -08002371qla25xx_read_optrom_data(struct scsi_qla_host *vha, uint8_t *buf,
Andrew Vasquez338c9162007-09-20 14:07:33 -07002372 uint32_t offset, uint32_t length)
2373{
2374 int rval;
2375 dma_addr_t optrom_dma;
2376 void *optrom;
2377 uint8_t *pbuf;
2378 uint32_t faddr, left, burst;
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -08002379 struct qla_hw_data *ha = vha->hw;
Andrew Vasquez338c9162007-09-20 14:07:33 -07002380
Andrew Vasquez368bbe02010-01-12 12:59:49 -08002381 if (IS_QLA25XX(ha) || IS_QLA81XX(ha))
2382 goto try_fast;
Joe Carnucciob7cc1762007-09-20 14:07:35 -07002383 if (offset & 0xfff)
Andrew Vasquez338c9162007-09-20 14:07:33 -07002384 goto slow_read;
2385 if (length < OPTROM_BURST_SIZE)
2386 goto slow_read;
2387
Andrew Vasquez368bbe02010-01-12 12:59:49 -08002388try_fast:
Andrew Vasquez338c9162007-09-20 14:07:33 -07002389 optrom = dma_alloc_coherent(&ha->pdev->dev, OPTROM_BURST_SIZE,
2390 &optrom_dma, GFP_KERNEL);
2391 if (!optrom) {
2392 qla_printk(KERN_DEBUG, ha,
2393 "Unable to allocate memory for optrom burst read "
2394 "(%x KB).\n", OPTROM_BURST_SIZE / 1024);
2395
2396 goto slow_read;
2397 }
2398
2399 pbuf = buf;
2400 faddr = offset >> 2;
2401 left = length >> 2;
2402 burst = OPTROM_BURST_DWORDS;
2403 while (left != 0) {
2404 if (burst > left)
2405 burst = left;
2406
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -08002407 rval = qla2x00_dump_ram(vha, optrom_dma,
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08002408 flash_data_addr(ha, faddr), burst);
Andrew Vasquez338c9162007-09-20 14:07:33 -07002409 if (rval) {
2410 qla_printk(KERN_WARNING, ha,
2411 "Unable to burst-read optrom segment "
2412 "(%x/%x/%llx).\n", rval,
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08002413 flash_data_addr(ha, faddr),
Andrew Morton875baf32007-10-16 14:28:20 -07002414 (unsigned long long)optrom_dma);
Andrew Vasquez338c9162007-09-20 14:07:33 -07002415 qla_printk(KERN_WARNING, ha,
2416 "Reverting to slow-read.\n");
2417
2418 dma_free_coherent(&ha->pdev->dev, OPTROM_BURST_SIZE,
2419 optrom, optrom_dma);
2420 goto slow_read;
2421 }
2422
2423 memcpy(pbuf, optrom, burst * 4);
2424
2425 left -= burst;
2426 faddr += burst;
2427 pbuf += burst * 4;
2428 }
2429
2430 dma_free_coherent(&ha->pdev->dev, OPTROM_BURST_SIZE, optrom,
2431 optrom_dma);
2432
2433 return buf;
2434
2435slow_read:
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -08002436 return qla24xx_read_optrom_data(vha, buf, offset, length);
Andrew Vasquez338c9162007-09-20 14:07:33 -07002437}
2438
Andrew Vasquez30c47662007-01-29 10:22:21 -08002439/**
2440 * qla2x00_get_fcode_version() - Determine an FCODE image's version.
2441 * @ha: HA context
2442 * @pcids: Pointer to the FCODE PCI data structure
2443 *
2444 * The process of retrieving the FCODE version information is at best
2445 * described as interesting.
2446 *
2447 * Within the first 100h bytes of the image an ASCII string is present
2448 * which contains several pieces of information including the FCODE
2449 * version. Unfortunately it seems the only reliable way to retrieve
2450 * the version is by scanning for another sentinel within the string,
2451 * the FCODE build date:
2452 *
2453 * ... 2.00.02 10/17/02 ...
2454 *
2455 * Returns QLA_SUCCESS on successful retrieval of version.
2456 */
2457static void
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -08002458qla2x00_get_fcode_version(struct qla_hw_data *ha, uint32_t pcids)
Andrew Vasquez30c47662007-01-29 10:22:21 -08002459{
2460 int ret = QLA_FUNCTION_FAILED;
2461 uint32_t istart, iend, iter, vend;
2462 uint8_t do_next, rbyte, *vbyte;
2463
2464 memset(ha->fcode_revision, 0, sizeof(ha->fcode_revision));
2465
2466 /* Skip the PCI data structure. */
2467 istart = pcids +
2468 ((qla2x00_read_flash_byte(ha, pcids + 0x0B) << 8) |
2469 qla2x00_read_flash_byte(ha, pcids + 0x0A));
2470 iend = istart + 0x100;
2471 do {
2472 /* Scan for the sentinel date string...eeewww. */
2473 do_next = 0;
2474 iter = istart;
2475 while ((iter < iend) && !do_next) {
2476 iter++;
2477 if (qla2x00_read_flash_byte(ha, iter) == '/') {
2478 if (qla2x00_read_flash_byte(ha, iter + 2) ==
2479 '/')
2480 do_next++;
2481 else if (qla2x00_read_flash_byte(ha,
2482 iter + 3) == '/')
2483 do_next++;
2484 }
2485 }
2486 if (!do_next)
2487 break;
2488
2489 /* Backtrack to previous ' ' (space). */
2490 do_next = 0;
2491 while ((iter > istart) && !do_next) {
2492 iter--;
2493 if (qla2x00_read_flash_byte(ha, iter) == ' ')
2494 do_next++;
2495 }
2496 if (!do_next)
2497 break;
2498
2499 /*
2500 * Mark end of version tag, and find previous ' ' (space) or
2501 * string length (recent FCODE images -- major hack ahead!!!).
2502 */
2503 vend = iter - 1;
2504 do_next = 0;
2505 while ((iter > istart) && !do_next) {
2506 iter--;
2507 rbyte = qla2x00_read_flash_byte(ha, iter);
2508 if (rbyte == ' ' || rbyte == 0xd || rbyte == 0x10)
2509 do_next++;
2510 }
2511 if (!do_next)
2512 break;
2513
2514 /* Mark beginning of version tag, and copy data. */
2515 iter++;
2516 if ((vend - iter) &&
2517 ((vend - iter) < sizeof(ha->fcode_revision))) {
2518 vbyte = ha->fcode_revision;
2519 while (iter <= vend) {
2520 *vbyte++ = qla2x00_read_flash_byte(ha, iter);
2521 iter++;
2522 }
2523 ret = QLA_SUCCESS;
2524 }
2525 } while (0);
2526
2527 if (ret != QLA_SUCCESS)
2528 memset(ha->fcode_revision, 0, sizeof(ha->fcode_revision));
2529}
2530
2531int
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -08002532qla2x00_get_flash_version(scsi_qla_host_t *vha, void *mbuf)
Andrew Vasquez30c47662007-01-29 10:22:21 -08002533{
2534 int ret = QLA_SUCCESS;
2535 uint8_t code_type, last_image;
2536 uint32_t pcihdr, pcids;
2537 uint8_t *dbyte;
2538 uint16_t *dcode;
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -08002539 struct qla_hw_data *ha = vha->hw;
Andrew Vasquez30c47662007-01-29 10:22:21 -08002540
2541 if (!ha->pio_address || !mbuf)
2542 return QLA_FUNCTION_FAILED;
2543
2544 memset(ha->bios_revision, 0, sizeof(ha->bios_revision));
2545 memset(ha->efi_revision, 0, sizeof(ha->efi_revision));
2546 memset(ha->fcode_revision, 0, sizeof(ha->fcode_revision));
2547 memset(ha->fw_revision, 0, sizeof(ha->fw_revision));
2548
2549 qla2x00_flash_enable(ha);
2550
2551 /* Begin with first PCI expansion ROM header. */
2552 pcihdr = 0;
2553 last_image = 1;
2554 do {
2555 /* Verify PCI expansion ROM header. */
2556 if (qla2x00_read_flash_byte(ha, pcihdr) != 0x55 ||
2557 qla2x00_read_flash_byte(ha, pcihdr + 0x01) != 0xaa) {
2558 /* No signature */
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -08002559 DEBUG2(qla_printk(KERN_DEBUG, ha, "No matching ROM "
2560 "signature.\n"));
Andrew Vasquez30c47662007-01-29 10:22:21 -08002561 ret = QLA_FUNCTION_FAILED;
2562 break;
2563 }
2564
2565 /* Locate PCI data structure. */
2566 pcids = pcihdr +
2567 ((qla2x00_read_flash_byte(ha, pcihdr + 0x19) << 8) |
2568 qla2x00_read_flash_byte(ha, pcihdr + 0x18));
2569
2570 /* Validate signature of PCI data structure. */
2571 if (qla2x00_read_flash_byte(ha, pcids) != 'P' ||
2572 qla2x00_read_flash_byte(ha, pcids + 0x1) != 'C' ||
2573 qla2x00_read_flash_byte(ha, pcids + 0x2) != 'I' ||
2574 qla2x00_read_flash_byte(ha, pcids + 0x3) != 'R') {
2575 /* Incorrect header. */
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -08002576 DEBUG2(qla_printk(KERN_INFO, ha, "PCI data struct not "
2577 "found pcir_adr=%x.\n", pcids));
Andrew Vasquez30c47662007-01-29 10:22:21 -08002578 ret = QLA_FUNCTION_FAILED;
2579 break;
2580 }
2581
2582 /* Read version */
2583 code_type = qla2x00_read_flash_byte(ha, pcids + 0x14);
2584 switch (code_type) {
2585 case ROM_CODE_TYPE_BIOS:
2586 /* Intel x86, PC-AT compatible. */
2587 ha->bios_revision[0] =
2588 qla2x00_read_flash_byte(ha, pcids + 0x12);
2589 ha->bios_revision[1] =
2590 qla2x00_read_flash_byte(ha, pcids + 0x13);
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -08002591 DEBUG3(qla_printk(KERN_DEBUG, ha, "read BIOS %d.%d.\n",
Andrew Vasquez30c47662007-01-29 10:22:21 -08002592 ha->bios_revision[1], ha->bios_revision[0]));
2593 break;
2594 case ROM_CODE_TYPE_FCODE:
2595 /* Open Firmware standard for PCI (FCode). */
2596 /* Eeeewww... */
2597 qla2x00_get_fcode_version(ha, pcids);
2598 break;
2599 case ROM_CODE_TYPE_EFI:
2600 /* Extensible Firmware Interface (EFI). */
2601 ha->efi_revision[0] =
2602 qla2x00_read_flash_byte(ha, pcids + 0x12);
2603 ha->efi_revision[1] =
2604 qla2x00_read_flash_byte(ha, pcids + 0x13);
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -08002605 DEBUG3(qla_printk(KERN_DEBUG, ha, "read EFI %d.%d.\n",
Andrew Vasquez30c47662007-01-29 10:22:21 -08002606 ha->efi_revision[1], ha->efi_revision[0]));
2607 break;
2608 default:
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -08002609 DEBUG2(qla_printk(KERN_INFO, ha, "Unrecognized code "
2610 "type %x at pcids %x.\n", code_type, pcids));
Andrew Vasquez30c47662007-01-29 10:22:21 -08002611 break;
2612 }
2613
2614 last_image = qla2x00_read_flash_byte(ha, pcids + 0x15) & BIT_7;
2615
2616 /* Locate next PCI expansion ROM. */
2617 pcihdr += ((qla2x00_read_flash_byte(ha, pcids + 0x11) << 8) |
2618 qla2x00_read_flash_byte(ha, pcids + 0x10)) * 512;
2619 } while (!last_image);
2620
2621 if (IS_QLA2322(ha)) {
2622 /* Read firmware image information. */
2623 memset(ha->fw_revision, 0, sizeof(ha->fw_revision));
2624 dbyte = mbuf;
2625 memset(dbyte, 0, 8);
2626 dcode = (uint16_t *)dbyte;
2627
Andrew Vasquezc00d8992008-09-11 21:22:49 -07002628 qla2x00_read_flash_data(ha, dbyte, ha->flt_region_fw * 4 + 10,
Andrew Vasquez30c47662007-01-29 10:22:21 -08002629 8);
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -08002630 DEBUG3(qla_printk(KERN_DEBUG, ha, "dumping fw ver from "
2631 "flash:\n"));
Andrew Vasquez30c47662007-01-29 10:22:21 -08002632 DEBUG3(qla2x00_dump_buffer((uint8_t *)dbyte, 8));
2633
2634 if ((dcode[0] == 0xffff && dcode[1] == 0xffff &&
2635 dcode[2] == 0xffff && dcode[3] == 0xffff) ||
2636 (dcode[0] == 0 && dcode[1] == 0 && dcode[2] == 0 &&
2637 dcode[3] == 0)) {
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -08002638 DEBUG2(qla_printk(KERN_INFO, ha, "Unrecognized fw "
2639 "revision at %x.\n", ha->flt_region_fw * 4));
Andrew Vasquez30c47662007-01-29 10:22:21 -08002640 } else {
2641 /* values are in big endian */
2642 ha->fw_revision[0] = dbyte[0] << 16 | dbyte[1];
2643 ha->fw_revision[1] = dbyte[2] << 16 | dbyte[3];
2644 ha->fw_revision[2] = dbyte[4] << 16 | dbyte[5];
2645 }
2646 }
2647
2648 qla2x00_flash_disable(ha);
2649
2650 return ret;
2651}
2652
2653int
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -08002654qla24xx_get_flash_version(scsi_qla_host_t *vha, void *mbuf)
Andrew Vasquez30c47662007-01-29 10:22:21 -08002655{
2656 int ret = QLA_SUCCESS;
2657 uint32_t pcihdr, pcids;
2658 uint32_t *dcode;
2659 uint8_t *bcode;
2660 uint8_t code_type, last_image;
2661 int i;
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -08002662 struct qla_hw_data *ha = vha->hw;
Andrew Vasquez30c47662007-01-29 10:22:21 -08002663
Giridhar Malavalia9083012010-04-12 17:59:55 -07002664 if (IS_QLA82XX(ha))
2665 return ret;
2666
Andrew Vasquez30c47662007-01-29 10:22:21 -08002667 if (!mbuf)
2668 return QLA_FUNCTION_FAILED;
2669
2670 memset(ha->bios_revision, 0, sizeof(ha->bios_revision));
2671 memset(ha->efi_revision, 0, sizeof(ha->efi_revision));
2672 memset(ha->fcode_revision, 0, sizeof(ha->fcode_revision));
2673 memset(ha->fw_revision, 0, sizeof(ha->fw_revision));
2674
2675 dcode = mbuf;
2676
2677 /* Begin with first PCI expansion ROM header. */
Harish Zunjarrao6315a5f2009-03-24 09:07:59 -07002678 pcihdr = ha->flt_region_boot << 2;
Andrew Vasquez30c47662007-01-29 10:22:21 -08002679 last_image = 1;
2680 do {
2681 /* Verify PCI expansion ROM header. */
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -08002682 qla24xx_read_flash_data(vha, dcode, pcihdr >> 2, 0x20);
Andrew Vasquez30c47662007-01-29 10:22:21 -08002683 bcode = mbuf + (pcihdr % 4);
2684 if (bcode[0x0] != 0x55 || bcode[0x1] != 0xaa) {
2685 /* No signature */
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -08002686 DEBUG2(qla_printk(KERN_DEBUG, ha, "No matching ROM "
2687 "signature.\n"));
Andrew Vasquez30c47662007-01-29 10:22:21 -08002688 ret = QLA_FUNCTION_FAILED;
2689 break;
2690 }
2691
2692 /* Locate PCI data structure. */
2693 pcids = pcihdr + ((bcode[0x19] << 8) | bcode[0x18]);
2694
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -08002695 qla24xx_read_flash_data(vha, dcode, pcids >> 2, 0x20);
Andrew Vasquez30c47662007-01-29 10:22:21 -08002696 bcode = mbuf + (pcihdr % 4);
2697
2698 /* Validate signature of PCI data structure. */
2699 if (bcode[0x0] != 'P' || bcode[0x1] != 'C' ||
2700 bcode[0x2] != 'I' || bcode[0x3] != 'R') {
2701 /* Incorrect header. */
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -08002702 DEBUG2(qla_printk(KERN_INFO, ha, "PCI data struct not "
2703 "found pcir_adr=%x.\n", pcids));
Andrew Vasquez30c47662007-01-29 10:22:21 -08002704 ret = QLA_FUNCTION_FAILED;
2705 break;
2706 }
2707
2708 /* Read version */
2709 code_type = bcode[0x14];
2710 switch (code_type) {
2711 case ROM_CODE_TYPE_BIOS:
2712 /* Intel x86, PC-AT compatible. */
2713 ha->bios_revision[0] = bcode[0x12];
2714 ha->bios_revision[1] = bcode[0x13];
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -08002715 DEBUG3(qla_printk(KERN_DEBUG, ha, "read BIOS %d.%d.\n",
Andrew Vasquez30c47662007-01-29 10:22:21 -08002716 ha->bios_revision[1], ha->bios_revision[0]));
2717 break;
2718 case ROM_CODE_TYPE_FCODE:
2719 /* Open Firmware standard for PCI (FCode). */
2720 ha->fcode_revision[0] = bcode[0x12];
2721 ha->fcode_revision[1] = bcode[0x13];
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -08002722 DEBUG3(qla_printk(KERN_DEBUG, ha, "read FCODE %d.%d.\n",
Andrew Vasquez30c47662007-01-29 10:22:21 -08002723 ha->fcode_revision[1], ha->fcode_revision[0]));
2724 break;
2725 case ROM_CODE_TYPE_EFI:
2726 /* Extensible Firmware Interface (EFI). */
2727 ha->efi_revision[0] = bcode[0x12];
2728 ha->efi_revision[1] = bcode[0x13];
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -08002729 DEBUG3(qla_printk(KERN_DEBUG, ha, "read EFI %d.%d.\n",
Andrew Vasquez30c47662007-01-29 10:22:21 -08002730 ha->efi_revision[1], ha->efi_revision[0]));
2731 break;
2732 default:
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -08002733 DEBUG2(qla_printk(KERN_INFO, ha, "Unrecognized code "
2734 "type %x at pcids %x.\n", code_type, pcids));
Andrew Vasquez30c47662007-01-29 10:22:21 -08002735 break;
2736 }
2737
2738 last_image = bcode[0x15] & BIT_7;
2739
2740 /* Locate next PCI expansion ROM. */
2741 pcihdr += ((bcode[0x11] << 8) | bcode[0x10]) * 512;
2742 } while (!last_image);
2743
2744 /* Read firmware image information. */
2745 memset(ha->fw_revision, 0, sizeof(ha->fw_revision));
2746 dcode = mbuf;
2747
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -08002748 qla24xx_read_flash_data(vha, dcode, ha->flt_region_fw + 4, 4);
Andrew Vasquez30c47662007-01-29 10:22:21 -08002749 for (i = 0; i < 4; i++)
2750 dcode[i] = be32_to_cpu(dcode[i]);
2751
2752 if ((dcode[0] == 0xffffffff && dcode[1] == 0xffffffff &&
2753 dcode[2] == 0xffffffff && dcode[3] == 0xffffffff) ||
2754 (dcode[0] == 0 && dcode[1] == 0 && dcode[2] == 0 &&
2755 dcode[3] == 0)) {
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -08002756 DEBUG2(qla_printk(KERN_INFO, ha, "Unrecognized fw "
2757 "revision at %x.\n", ha->flt_region_fw * 4));
Andrew Vasquez30c47662007-01-29 10:22:21 -08002758 } else {
2759 ha->fw_revision[0] = dcode[0];
2760 ha->fw_revision[1] = dcode[1];
2761 ha->fw_revision[2] = dcode[2];
2762 ha->fw_revision[3] = dcode[3];
2763 }
2764
Madhuranath Iyengar0f2d9622010-07-23 15:28:26 +05002765 /* Check for golden firmware and get version if available */
2766 if (!IS_QLA81XX(ha)) {
2767 /* Golden firmware is not present in non 81XX adapters */
2768 return ret;
2769 }
2770
2771 memset(ha->gold_fw_version, 0, sizeof(ha->gold_fw_version));
2772 dcode = mbuf;
2773 ha->isp_ops->read_optrom(vha, (uint8_t *)dcode,
2774 ha->flt_region_gold_fw << 2, 32);
2775
2776 if (dcode[4] == 0xFFFFFFFF && dcode[5] == 0xFFFFFFFF &&
2777 dcode[6] == 0xFFFFFFFF && dcode[7] == 0xFFFFFFFF) {
2778 DEBUG2(qla_printk(KERN_INFO, ha,
2779 "%s(%ld): Unrecognized golden fw at 0x%x.\n",
2780 __func__, vha->host_no, ha->flt_region_gold_fw * 4));
2781 return ret;
2782 }
2783
2784 for (i = 4; i < 8; i++)
2785 ha->gold_fw_version[i-4] = be32_to_cpu(dcode[i]);
2786
Andrew Vasquez30c47662007-01-29 10:22:21 -08002787 return ret;
2788}
Andrew Vasquezcb8dacb2008-04-03 13:13:19 -07002789
2790static int
Joe Carnuccio1ee27142008-07-10 16:55:53 -07002791qla2xxx_is_vpd_valid(uint8_t *pos, uint8_t *end)
2792{
2793 if (pos >= end || *pos != 0x82)
2794 return 0;
2795
2796 pos += 3 + pos[1];
2797 if (pos >= end || *pos != 0x90)
2798 return 0;
2799
2800 pos += 3 + pos[1];
2801 if (pos >= end || *pos != 0x78)
2802 return 0;
2803
2804 return 1;
2805}
2806
2807int
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -08002808qla2xxx_get_vpd_field(scsi_qla_host_t *vha, char *key, char *str, size_t size)
Joe Carnuccio1ee27142008-07-10 16:55:53 -07002809{
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -08002810 struct qla_hw_data *ha = vha->hw;
Joe Carnuccio1ee27142008-07-10 16:55:53 -07002811 uint8_t *pos = ha->vpd;
2812 uint8_t *end = pos + ha->vpd_size;
2813 int len = 0;
2814
2815 if (!IS_FWI2_CAPABLE(ha) || !qla2xxx_is_vpd_valid(pos, end))
2816 return 0;
2817
2818 while (pos < end && *pos != 0x78) {
2819 len = (*pos == 0x82) ? pos[1] : pos[2];
2820
2821 if (!strncmp(pos, key, strlen(key)))
2822 break;
2823
2824 if (*pos != 0x90 && *pos != 0x91)
2825 pos += len;
2826
2827 pos += 3;
2828 }
2829
2830 if (pos < end - len && *pos != 0x78)
2831 return snprintf(str, size, "%.*s", len, pos + 3);
2832
2833 return 0;
2834}
Sarang Radke09ff7012010-03-19 17:03:59 -07002835
2836int
2837qla24xx_read_fcp_prio_cfg(scsi_qla_host_t *vha)
2838{
2839 int len, max_len;
2840 uint32_t fcp_prio_addr;
2841 struct qla_hw_data *ha = vha->hw;
2842
2843 if (!ha->fcp_prio_cfg) {
2844 ha->fcp_prio_cfg = vmalloc(FCP_PRIO_CFG_SIZE);
2845 if (!ha->fcp_prio_cfg) {
2846 qla_printk(KERN_WARNING, ha,
2847 "Unable to allocate memory for fcp priority data "
2848 "(%x).\n", FCP_PRIO_CFG_SIZE);
2849 return QLA_FUNCTION_FAILED;
2850 }
2851 }
2852 memset(ha->fcp_prio_cfg, 0, FCP_PRIO_CFG_SIZE);
2853
2854 fcp_prio_addr = ha->flt_region_fcp_prio;
2855
2856 /* first read the fcp priority data header from flash */
2857 ha->isp_ops->read_optrom(vha, (uint8_t *)ha->fcp_prio_cfg,
2858 fcp_prio_addr << 2, FCP_PRIO_CFG_HDR_SIZE);
2859
2860 if (!qla24xx_fcp_prio_cfg_valid(ha->fcp_prio_cfg, 0))
2861 goto fail;
2862
2863 /* read remaining FCP CMD config data from flash */
2864 fcp_prio_addr += (FCP_PRIO_CFG_HDR_SIZE >> 2);
2865 len = ha->fcp_prio_cfg->num_entries * FCP_PRIO_CFG_ENTRY_SIZE;
2866 max_len = FCP_PRIO_CFG_SIZE - FCP_PRIO_CFG_HDR_SIZE;
2867
2868 ha->isp_ops->read_optrom(vha, (uint8_t *)&ha->fcp_prio_cfg->entry[0],
2869 fcp_prio_addr << 2, (len < max_len ? len : max_len));
2870
2871 /* revalidate the entire FCP priority config data, including entries */
2872 if (!qla24xx_fcp_prio_cfg_valid(ha->fcp_prio_cfg, 1))
2873 goto fail;
2874
2875 ha->flags.fcp_prio_enabled = 1;
2876 return QLA_SUCCESS;
2877fail:
2878 vfree(ha->fcp_prio_cfg);
2879 ha->fcp_prio_cfg = NULL;
2880 return QLA_FUNCTION_FAILED;
2881}