blob: 3bef6736d885a7bf30c84c00892e6fb102629d10 [file] [log] [blame]
Andrew Vasquezfa90c542005-10-27 11:10:08 -07001/*
2 * QLogic Fibre Channel HBA Driver
Saurav Kashyap1e633952013-02-08 01:57:54 -05003 * Copyright (c) 2003-2013 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;
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700192 scsi_qla_host_t *vha = pci_get_drvdata(ha->pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193
194 qla2x00_nv_write(ha, NVR_DATA_OUT);
195 qla2x00_nv_write(ha, 0);
196 qla2x00_nv_write(ha, 0);
197
198 for (word = 0; word < 8; word++)
199 qla2x00_nv_write(ha, NVR_DATA_OUT);
200
201 qla2x00_nv_deselect(ha);
202
203 /* Write data */
204 nv_cmd = (addr << 16) | NV_WRITE_OP;
205 nv_cmd |= data;
206 nv_cmd <<= 5;
207 for (count = 0; count < 27; count++) {
208 if (nv_cmd & BIT_31)
209 qla2x00_nv_write(ha, NVR_DATA_OUT);
210 else
211 qla2x00_nv_write(ha, 0);
212
213 nv_cmd <<= 1;
214 }
215
216 qla2x00_nv_deselect(ha);
217
218 /* Wait for NVRAM to become ready */
219 WRT_REG_WORD(&reg->nvram, NVR_SELECT);
Andrew Vasquezdcb36ce2005-11-08 14:37:06 -0800220 RD_REG_WORD(&reg->nvram); /* PCI Posting. */
Ravi Anand45aeaf12006-05-17 15:08:49 -0700221 wait_cnt = NVR_WAIT_CNT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 do {
Ravi Anand45aeaf12006-05-17 15:08:49 -0700223 if (!--wait_cnt) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700224 ql_dbg(ql_dbg_user, vha, 0x708d,
225 "NVRAM didn't go ready...\n");
Ravi Anand45aeaf12006-05-17 15:08:49 -0700226 break;
227 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 NVRAM_DELAY();
229 word = RD_REG_WORD(&reg->nvram);
230 } while ((word & NVR_DATA_IN) == 0);
231
232 qla2x00_nv_deselect(ha);
233
234 /* Disable writes */
235 qla2x00_nv_write(ha, NVR_DATA_OUT);
236 for (count = 0; count < 10; count++)
237 qla2x00_nv_write(ha, 0);
238
239 qla2x00_nv_deselect(ha);
240}
241
Andrew Vasquez459c5372005-07-06 10:31:07 -0700242static int
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -0800243qla2x00_write_nvram_word_tmo(struct qla_hw_data *ha, uint32_t addr,
244 uint16_t data, uint32_t tmo)
Andrew Vasquez459c5372005-07-06 10:31:07 -0700245{
246 int ret, count;
247 uint16_t word;
248 uint32_t nv_cmd;
249 struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
250
251 ret = QLA_SUCCESS;
252
253 qla2x00_nv_write(ha, NVR_DATA_OUT);
254 qla2x00_nv_write(ha, 0);
255 qla2x00_nv_write(ha, 0);
256
257 for (word = 0; word < 8; word++)
258 qla2x00_nv_write(ha, NVR_DATA_OUT);
259
260 qla2x00_nv_deselect(ha);
261
262 /* Write data */
263 nv_cmd = (addr << 16) | NV_WRITE_OP;
264 nv_cmd |= data;
265 nv_cmd <<= 5;
266 for (count = 0; count < 27; count++) {
267 if (nv_cmd & BIT_31)
268 qla2x00_nv_write(ha, NVR_DATA_OUT);
269 else
270 qla2x00_nv_write(ha, 0);
271
272 nv_cmd <<= 1;
273 }
274
275 qla2x00_nv_deselect(ha);
276
277 /* Wait for NVRAM to become ready */
278 WRT_REG_WORD(&reg->nvram, NVR_SELECT);
Andrew Vasquezdcb36ce2005-11-08 14:37:06 -0800279 RD_REG_WORD(&reg->nvram); /* PCI Posting. */
Andrew Vasquez459c5372005-07-06 10:31:07 -0700280 do {
281 NVRAM_DELAY();
282 word = RD_REG_WORD(&reg->nvram);
283 if (!--tmo) {
284 ret = QLA_FUNCTION_FAILED;
285 break;
286 }
287 } while ((word & NVR_DATA_IN) == 0);
288
289 qla2x00_nv_deselect(ha);
290
291 /* Disable writes */
292 qla2x00_nv_write(ha, NVR_DATA_OUT);
293 for (count = 0; count < 10; count++)
294 qla2x00_nv_write(ha, 0);
295
296 qla2x00_nv_deselect(ha);
297
298 return ret;
299}
300
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301/**
Andrew Vasquez459c5372005-07-06 10:31:07 -0700302 * qla2x00_clear_nvram_protection() -
303 * @ha: HA context
304 */
305static int
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -0800306qla2x00_clear_nvram_protection(struct qla_hw_data *ha)
Andrew Vasquez459c5372005-07-06 10:31:07 -0700307{
308 int ret, stat;
309 struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
Ravi Anand45aeaf12006-05-17 15:08:49 -0700310 uint32_t word, wait_cnt;
Andrew Vasquez459c5372005-07-06 10:31:07 -0700311 uint16_t wprot, wprot_old;
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700312 scsi_qla_host_t *vha = pci_get_drvdata(ha->pdev);
Andrew Vasquez459c5372005-07-06 10:31:07 -0700313
314 /* Clear NVRAM write protection. */
315 ret = QLA_FUNCTION_FAILED;
Ravi Anand45aeaf12006-05-17 15:08:49 -0700316
317 wprot_old = cpu_to_le16(qla2x00_get_nvram_word(ha, ha->nvram_base));
318 stat = qla2x00_write_nvram_word_tmo(ha, ha->nvram_base,
Andrew Vasquez459c5372005-07-06 10:31:07 -0700319 __constant_cpu_to_le16(0x1234), 100000);
Ravi Anand45aeaf12006-05-17 15:08:49 -0700320 wprot = cpu_to_le16(qla2x00_get_nvram_word(ha, ha->nvram_base));
321 if (stat != QLA_SUCCESS || wprot != 0x1234) {
Andrew Vasquez459c5372005-07-06 10:31:07 -0700322 /* Write enable. */
323 qla2x00_nv_write(ha, NVR_DATA_OUT);
324 qla2x00_nv_write(ha, 0);
325 qla2x00_nv_write(ha, 0);
326 for (word = 0; word < 8; word++)
327 qla2x00_nv_write(ha, NVR_DATA_OUT);
328
329 qla2x00_nv_deselect(ha);
330
331 /* Enable protection register. */
332 qla2x00_nv_write(ha, NVR_PR_ENABLE | NVR_DATA_OUT);
333 qla2x00_nv_write(ha, NVR_PR_ENABLE);
334 qla2x00_nv_write(ha, NVR_PR_ENABLE);
335 for (word = 0; word < 8; word++)
336 qla2x00_nv_write(ha, NVR_DATA_OUT | NVR_PR_ENABLE);
337
338 qla2x00_nv_deselect(ha);
339
340 /* Clear protection register (ffff is cleared). */
341 qla2x00_nv_write(ha, NVR_PR_ENABLE | NVR_DATA_OUT);
342 qla2x00_nv_write(ha, NVR_PR_ENABLE | NVR_DATA_OUT);
343 qla2x00_nv_write(ha, NVR_PR_ENABLE | NVR_DATA_OUT);
344 for (word = 0; word < 8; word++)
345 qla2x00_nv_write(ha, NVR_DATA_OUT | NVR_PR_ENABLE);
346
347 qla2x00_nv_deselect(ha);
348
349 /* Wait for NVRAM to become ready. */
350 WRT_REG_WORD(&reg->nvram, NVR_SELECT);
Andrew Vasquezdcb36ce2005-11-08 14:37:06 -0800351 RD_REG_WORD(&reg->nvram); /* PCI Posting. */
Ravi Anand45aeaf12006-05-17 15:08:49 -0700352 wait_cnt = NVR_WAIT_CNT;
Andrew Vasquez459c5372005-07-06 10:31:07 -0700353 do {
Ravi Anand45aeaf12006-05-17 15:08:49 -0700354 if (!--wait_cnt) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700355 ql_dbg(ql_dbg_user, vha, 0x708e,
356 "NVRAM didn't go ready...\n");
Ravi Anand45aeaf12006-05-17 15:08:49 -0700357 break;
358 }
Andrew Vasquez459c5372005-07-06 10:31:07 -0700359 NVRAM_DELAY();
360 word = RD_REG_WORD(&reg->nvram);
361 } while ((word & NVR_DATA_IN) == 0);
362
Ravi Anand45aeaf12006-05-17 15:08:49 -0700363 if (wait_cnt)
364 ret = QLA_SUCCESS;
Andrew Vasquez459c5372005-07-06 10:31:07 -0700365 } else
Ravi Anand45aeaf12006-05-17 15:08:49 -0700366 qla2x00_write_nvram_word(ha, ha->nvram_base, wprot_old);
Andrew Vasquez459c5372005-07-06 10:31:07 -0700367
368 return ret;
369}
370
371static void
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -0800372qla2x00_set_nvram_protection(struct qla_hw_data *ha, int stat)
Andrew Vasquez459c5372005-07-06 10:31:07 -0700373{
374 struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
Ravi Anand45aeaf12006-05-17 15:08:49 -0700375 uint32_t word, wait_cnt;
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700376 scsi_qla_host_t *vha = pci_get_drvdata(ha->pdev);
Andrew Vasquez459c5372005-07-06 10:31:07 -0700377
378 if (stat != QLA_SUCCESS)
379 return;
380
381 /* Set NVRAM write protection. */
382 /* Write enable. */
383 qla2x00_nv_write(ha, NVR_DATA_OUT);
384 qla2x00_nv_write(ha, 0);
385 qla2x00_nv_write(ha, 0);
386 for (word = 0; word < 8; word++)
387 qla2x00_nv_write(ha, NVR_DATA_OUT);
388
389 qla2x00_nv_deselect(ha);
390
391 /* Enable protection register. */
392 qla2x00_nv_write(ha, NVR_PR_ENABLE | NVR_DATA_OUT);
393 qla2x00_nv_write(ha, NVR_PR_ENABLE);
394 qla2x00_nv_write(ha, NVR_PR_ENABLE);
395 for (word = 0; word < 8; word++)
396 qla2x00_nv_write(ha, NVR_DATA_OUT | NVR_PR_ENABLE);
397
398 qla2x00_nv_deselect(ha);
399
400 /* Enable protection register. */
401 qla2x00_nv_write(ha, NVR_PR_ENABLE | NVR_DATA_OUT);
402 qla2x00_nv_write(ha, NVR_PR_ENABLE);
403 qla2x00_nv_write(ha, NVR_PR_ENABLE | NVR_DATA_OUT);
404 for (word = 0; word < 8; word++)
405 qla2x00_nv_write(ha, NVR_PR_ENABLE);
406
407 qla2x00_nv_deselect(ha);
408
409 /* Wait for NVRAM to become ready. */
410 WRT_REG_WORD(&reg->nvram, NVR_SELECT);
Andrew Vasquezdcb36ce2005-11-08 14:37:06 -0800411 RD_REG_WORD(&reg->nvram); /* PCI Posting. */
Ravi Anand45aeaf12006-05-17 15:08:49 -0700412 wait_cnt = NVR_WAIT_CNT;
Andrew Vasquez459c5372005-07-06 10:31:07 -0700413 do {
Ravi Anand45aeaf12006-05-17 15:08:49 -0700414 if (!--wait_cnt) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700415 ql_dbg(ql_dbg_user, vha, 0x708f,
416 "NVRAM didn't go ready...\n");
Ravi Anand45aeaf12006-05-17 15:08:49 -0700417 break;
418 }
Andrew Vasquez459c5372005-07-06 10:31:07 -0700419 NVRAM_DELAY();
420 word = RD_REG_WORD(&reg->nvram);
421 } while ((word & NVR_DATA_IN) == 0);
422}
423
424
425/*****************************************************************************/
426/* Flash Manipulation Routines */
427/*****************************************************************************/
428
429static inline uint32_t
Andrew Vasquez3a03eb72009-01-05 11:18:11 -0800430flash_conf_addr(struct qla_hw_data *ha, uint32_t faddr)
Andrew Vasquez459c5372005-07-06 10:31:07 -0700431{
Andrew Vasquez3a03eb72009-01-05 11:18:11 -0800432 return ha->flash_conf_off | faddr;
Andrew Vasquez459c5372005-07-06 10:31:07 -0700433}
434
435static inline uint32_t
Andrew Vasquez3a03eb72009-01-05 11:18:11 -0800436flash_data_addr(struct qla_hw_data *ha, uint32_t faddr)
Andrew Vasquez459c5372005-07-06 10:31:07 -0700437{
Andrew Vasquez3a03eb72009-01-05 11:18:11 -0800438 return ha->flash_data_off | faddr;
Andrew Vasquez459c5372005-07-06 10:31:07 -0700439}
440
441static inline uint32_t
Andrew Vasquez3a03eb72009-01-05 11:18:11 -0800442nvram_conf_addr(struct qla_hw_data *ha, uint32_t naddr)
Andrew Vasquez459c5372005-07-06 10:31:07 -0700443{
Andrew Vasquez3a03eb72009-01-05 11:18:11 -0800444 return ha->nvram_conf_off | naddr;
Andrew Vasquez459c5372005-07-06 10:31:07 -0700445}
446
447static inline uint32_t
Andrew Vasquez3a03eb72009-01-05 11:18:11 -0800448nvram_data_addr(struct qla_hw_data *ha, uint32_t naddr)
Andrew Vasquez459c5372005-07-06 10:31:07 -0700449{
Andrew Vasquez3a03eb72009-01-05 11:18:11 -0800450 return ha->nvram_data_off | naddr;
Andrew Vasquez459c5372005-07-06 10:31:07 -0700451}
452
Adrian Bunke5f82ab2006-11-08 19:55:50 -0800453static uint32_t
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -0800454qla24xx_read_flash_dword(struct qla_hw_data *ha, uint32_t addr)
Andrew Vasquez459c5372005-07-06 10:31:07 -0700455{
456 int rval;
457 uint32_t cnt, data;
458 struct device_reg_24xx __iomem *reg = &ha->iobase->isp24;
459
460 WRT_REG_DWORD(&reg->flash_addr, addr & ~FARX_DATA_FLAG);
461 /* Wait for READ cycle to complete. */
462 rval = QLA_SUCCESS;
463 for (cnt = 3000;
464 (RD_REG_DWORD(&reg->flash_addr) & FARX_DATA_FLAG) == 0 &&
465 rval == QLA_SUCCESS; cnt--) {
466 if (cnt)
467 udelay(10);
468 else
469 rval = QLA_FUNCTION_TIMEOUT;
Andrew Vasquez40a2e342007-03-12 10:41:28 -0700470 cond_resched();
Andrew Vasquez459c5372005-07-06 10:31:07 -0700471 }
472
473 /* TODO: What happens if we time out? */
474 data = 0xDEADDEAD;
475 if (rval == QLA_SUCCESS)
476 data = RD_REG_DWORD(&reg->flash_data);
477
478 return data;
479}
480
481uint32_t *
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -0800482qla24xx_read_flash_data(scsi_qla_host_t *vha, uint32_t *dwptr, uint32_t faddr,
Andrew Vasquez459c5372005-07-06 10:31:07 -0700483 uint32_t dwords)
484{
485 uint32_t i;
Andrew Vasquez3a03eb72009-01-05 11:18:11 -0800486 struct qla_hw_data *ha = vha->hw;
487
Andrew Vasquez459c5372005-07-06 10:31:07 -0700488 /* Dword reads to flash. */
489 for (i = 0; i < dwords; i++, faddr++)
Andrew Vasquez3a03eb72009-01-05 11:18:11 -0800490 dwptr[i] = cpu_to_le32(qla24xx_read_flash_dword(ha,
491 flash_data_addr(ha, faddr)));
Andrew Vasquez459c5372005-07-06 10:31:07 -0700492
Andrew Vasquez459c5372005-07-06 10:31:07 -0700493 return dwptr;
494}
495
Adrian Bunke5f82ab2006-11-08 19:55:50 -0800496static int
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -0800497qla24xx_write_flash_dword(struct qla_hw_data *ha, uint32_t addr, uint32_t data)
Andrew Vasquez459c5372005-07-06 10:31:07 -0700498{
499 int rval;
500 uint32_t cnt;
501 struct device_reg_24xx __iomem *reg = &ha->iobase->isp24;
502
503 WRT_REG_DWORD(&reg->flash_data, data);
504 RD_REG_DWORD(&reg->flash_data); /* PCI Posting. */
505 WRT_REG_DWORD(&reg->flash_addr, addr | FARX_DATA_FLAG);
506 /* Wait for Write cycle to complete. */
507 rval = QLA_SUCCESS;
508 for (cnt = 500000; (RD_REG_DWORD(&reg->flash_addr) & FARX_DATA_FLAG) &&
509 rval == QLA_SUCCESS; cnt--) {
510 if (cnt)
511 udelay(10);
512 else
513 rval = QLA_FUNCTION_TIMEOUT;
Andrew Vasquez40a2e342007-03-12 10:41:28 -0700514 cond_resched();
Andrew Vasquez459c5372005-07-06 10:31:07 -0700515 }
516 return rval;
517}
518
Adrian Bunke5f82ab2006-11-08 19:55:50 -0800519static void
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -0800520qla24xx_get_flash_manufacturer(struct qla_hw_data *ha, uint8_t *man_id,
Andrew Vasquez459c5372005-07-06 10:31:07 -0700521 uint8_t *flash_id)
522{
523 uint32_t ids;
524
Andrew Vasquez3a03eb72009-01-05 11:18:11 -0800525 ids = qla24xx_read_flash_dword(ha, flash_conf_addr(ha, 0x03ab));
Andrew Vasquez459c5372005-07-06 10:31:07 -0700526 *man_id = LSB(ids);
527 *flash_id = MSB(ids);
Ravi Anand45aeaf12006-05-17 15:08:49 -0700528
529 /* Check if man_id and flash_id are valid. */
530 if (ids != 0xDEADDEAD && (*man_id == 0 || *flash_id == 0)) {
531 /* Read information using 0x9f opcode
532 * Device ID, Mfg ID would be read in the format:
533 * <Ext Dev Info><Device ID Part2><Device ID Part 1><Mfg ID>
534 * Example: ATMEL 0x00 01 45 1F
535 * Extract MFG and Dev ID from last two bytes.
536 */
Andrew Vasquez3a03eb72009-01-05 11:18:11 -0800537 ids = qla24xx_read_flash_dword(ha, flash_conf_addr(ha, 0x009f));
Ravi Anand45aeaf12006-05-17 15:08:49 -0700538 *man_id = LSB(ids);
539 *flash_id = MSB(ids);
540 }
Andrew Vasquez459c5372005-07-06 10:31:07 -0700541}
542
Andrew Vasquezc00d8992008-09-11 21:22:49 -0700543static int
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -0800544qla2xxx_find_flt_start(scsi_qla_host_t *vha, uint32_t *start)
Andrew Vasquezc00d8992008-09-11 21:22:49 -0700545{
546 const char *loc, *locations[] = { "DEF", "PCI" };
547 uint32_t pcihdr, pcids;
548 uint32_t *dcode;
549 uint8_t *buf, *bcode, last_image;
550 uint16_t cnt, chksum, *wptr;
551 struct qla_flt_location *fltl;
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -0800552 struct qla_hw_data *ha = vha->hw;
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800553 struct req_que *req = ha->req_q_map[0];
Andrew Vasquezc00d8992008-09-11 21:22:49 -0700554
555 /*
556 * FLT-location structure resides after the last PCI region.
557 */
558
559 /* Begin with sane defaults. */
560 loc = locations[0];
Andrew Vasquez3a03eb72009-01-05 11:18:11 -0800561 *start = 0;
562 if (IS_QLA24XX_TYPE(ha))
563 *start = FA_FLASH_LAYOUT_ADDR_24;
564 else if (IS_QLA25XX(ha))
565 *start = FA_FLASH_LAYOUT_ADDR;
566 else if (IS_QLA81XX(ha))
567 *start = FA_FLASH_LAYOUT_ADDR_81;
Giridhar Malavalia9083012010-04-12 17:59:55 -0700568 else if (IS_QLA82XX(ha)) {
569 *start = FA_FLASH_LAYOUT_ADDR_82;
570 goto end;
Giridhar Malavali6246b8a2012-02-09 11:15:34 -0800571 } else if (IS_QLA83XX(ha)) {
572 *start = FA_FLASH_LAYOUT_ADDR_83;
573 goto end;
Giridhar Malavalia9083012010-04-12 17:59:55 -0700574 }
Andrew Vasquezc00d8992008-09-11 21:22:49 -0700575 /* Begin with first PCI expansion ROM header. */
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -0800576 buf = (uint8_t *)req->ring;
577 dcode = (uint32_t *)req->ring;
Andrew Vasquezc00d8992008-09-11 21:22:49 -0700578 pcihdr = 0;
579 last_image = 1;
580 do {
581 /* Verify PCI expansion ROM header. */
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -0800582 qla24xx_read_flash_data(vha, dcode, pcihdr >> 2, 0x20);
Andrew Vasquezc00d8992008-09-11 21:22:49 -0700583 bcode = buf + (pcihdr % 4);
584 if (bcode[0x0] != 0x55 || bcode[0x1] != 0xaa)
585 goto end;
586
587 /* Locate PCI data structure. */
588 pcids = pcihdr + ((bcode[0x19] << 8) | bcode[0x18]);
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -0800589 qla24xx_read_flash_data(vha, dcode, pcids >> 2, 0x20);
Andrew Vasquezc00d8992008-09-11 21:22:49 -0700590 bcode = buf + (pcihdr % 4);
591
592 /* Validate signature of PCI data structure. */
593 if (bcode[0x0] != 'P' || bcode[0x1] != 'C' ||
594 bcode[0x2] != 'I' || bcode[0x3] != 'R')
595 goto end;
596
597 last_image = bcode[0x15] & BIT_7;
598
599 /* Locate next PCI expansion ROM. */
600 pcihdr += ((bcode[0x11] << 8) | bcode[0x10]) * 512;
601 } while (!last_image);
602
603 /* Now verify FLT-location structure. */
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -0800604 fltl = (struct qla_flt_location *)req->ring;
605 qla24xx_read_flash_data(vha, dcode, pcihdr >> 2,
Andrew Vasquezc00d8992008-09-11 21:22:49 -0700606 sizeof(struct qla_flt_location) >> 2);
607 if (fltl->sig[0] != 'Q' || fltl->sig[1] != 'F' ||
608 fltl->sig[2] != 'L' || fltl->sig[3] != 'T')
609 goto end;
610
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -0800611 wptr = (uint16_t *)req->ring;
Andrew Vasquezc00d8992008-09-11 21:22:49 -0700612 cnt = sizeof(struct qla_flt_location) >> 1;
613 for (chksum = 0; cnt; cnt--)
614 chksum += le16_to_cpu(*wptr++);
615 if (chksum) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700616 ql_log(ql_log_fatal, vha, 0x0045,
Andrew Vasquezc00d8992008-09-11 21:22:49 -0700617 "Inconsistent FLTL detected: checksum=0x%x.\n", chksum);
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700618 ql_dump_buffer(ql_dbg_init + ql_dbg_buffer, vha, 0x010e,
619 buf, sizeof(struct qla_flt_location));
Andrew Vasquezc00d8992008-09-11 21:22:49 -0700620 return QLA_FUNCTION_FAILED;
621 }
622
623 /* Good data. Use specified location. */
624 loc = locations[1];
Harish Zunjarrao79c13a72009-03-24 09:08:19 -0700625 *start = (le16_to_cpu(fltl->start_hi) << 16 |
626 le16_to_cpu(fltl->start_lo)) >> 2;
Andrew Vasquezc00d8992008-09-11 21:22:49 -0700627end:
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700628 ql_dbg(ql_dbg_init, vha, 0x0046,
629 "FLTL[%s] = 0x%x.\n",
630 loc, *start);
Andrew Vasquezc00d8992008-09-11 21:22:49 -0700631 return QLA_SUCCESS;
632}
633
634static void
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -0800635qla2xxx_get_flt_info(scsi_qla_host_t *vha, uint32_t flt_addr)
Andrew Vasquezc00d8992008-09-11 21:22:49 -0700636{
637 const char *loc, *locations[] = { "DEF", "FLT" };
Andrew Vasquez3a03eb72009-01-05 11:18:11 -0800638 const uint32_t def_fw[] =
639 { FA_RISC_CODE_ADDR, FA_RISC_CODE_ADDR, FA_RISC_CODE_ADDR_81 };
640 const uint32_t def_boot[] =
641 { FA_BOOT_CODE_ADDR, FA_BOOT_CODE_ADDR, FA_BOOT_CODE_ADDR_81 };
642 const uint32_t def_vpd_nvram[] =
643 { FA_VPD_NVRAM_ADDR, FA_VPD_NVRAM_ADDR, FA_VPD_NVRAM_ADDR_81 };
Andrew Vasquez3d79038f2009-03-24 09:08:14 -0700644 const uint32_t def_vpd0[] =
645 { 0, 0, FA_VPD0_ADDR_81 };
646 const uint32_t def_vpd1[] =
647 { 0, 0, FA_VPD1_ADDR_81 };
648 const uint32_t def_nvram0[] =
649 { 0, 0, FA_NVRAM0_ADDR_81 };
650 const uint32_t def_nvram1[] =
651 { 0, 0, FA_NVRAM1_ADDR_81 };
Andrew Vasquez3a03eb72009-01-05 11:18:11 -0800652 const uint32_t def_fdt[] =
653 { FA_FLASH_DESCR_ADDR_24, FA_FLASH_DESCR_ADDR,
654 FA_FLASH_DESCR_ADDR_81 };
655 const uint32_t def_npiv_conf0[] =
656 { FA_NPIV_CONF0_ADDR_24, FA_NPIV_CONF0_ADDR,
657 FA_NPIV_CONF0_ADDR_81 };
658 const uint32_t def_npiv_conf1[] =
659 { FA_NPIV_CONF1_ADDR_24, FA_NPIV_CONF1_ADDR,
660 FA_NPIV_CONF1_ADDR_81 };
Sarang Radke09ff7012010-03-19 17:03:59 -0700661 const uint32_t fcp_prio_cfg0[] =
662 { FA_FCP_PRIO0_ADDR, FA_FCP_PRIO0_ADDR_25,
663 0 };
664 const uint32_t fcp_prio_cfg1[] =
665 { FA_FCP_PRIO1_ADDR, FA_FCP_PRIO1_ADDR_25,
666 0 };
Andrew Vasquez3a03eb72009-01-05 11:18:11 -0800667 uint32_t def;
Andrew Vasquezc00d8992008-09-11 21:22:49 -0700668 uint16_t *wptr;
669 uint16_t cnt, chksum;
670 uint32_t start;
671 struct qla_flt_header *flt;
672 struct qla_flt_region *region;
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -0800673 struct qla_hw_data *ha = vha->hw;
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800674 struct req_que *req = ha->req_q_map[0];
Andrew Vasquezc00d8992008-09-11 21:22:49 -0700675
Madhuranath Iyengar2f0f3f42010-07-23 15:28:24 +0500676 def = 0;
677 if (IS_QLA25XX(ha))
678 def = 1;
679 else if (IS_QLA81XX(ha))
680 def = 2;
Andrew Vasquezff8073f2010-12-21 16:00:16 -0800681
682 /* Assign FCP prio region since older adapters may not have FLT, or
683 FCP prio region in it's FLT.
684 */
685 ha->flt_region_fcp_prio = ha->flags.port0 ?
686 fcp_prio_cfg0[def] : fcp_prio_cfg1[def];
687
Andrew Vasquezc00d8992008-09-11 21:22:49 -0700688 ha->flt_region_flt = flt_addr;
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -0800689 wptr = (uint16_t *)req->ring;
690 flt = (struct qla_flt_header *)req->ring;
Andrew Vasquezc00d8992008-09-11 21:22:49 -0700691 region = (struct qla_flt_region *)&flt[1];
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -0800692 ha->isp_ops->read_optrom(vha, (uint8_t *)req->ring,
Andrew Vasquezc00d8992008-09-11 21:22:49 -0700693 flt_addr << 2, OPTROM_BURST_SIZE);
694 if (*wptr == __constant_cpu_to_le16(0xffff))
695 goto no_flash_data;
696 if (flt->version != __constant_cpu_to_le16(1)) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700697 ql_log(ql_log_warn, vha, 0x0047,
698 "Unsupported FLT detected: version=0x%x length=0x%x checksum=0x%x.\n",
Andrew Vasquezc00d8992008-09-11 21:22:49 -0700699 le16_to_cpu(flt->version), le16_to_cpu(flt->length),
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700700 le16_to_cpu(flt->checksum));
Andrew Vasquezc00d8992008-09-11 21:22:49 -0700701 goto no_flash_data;
702 }
703
704 cnt = (sizeof(struct qla_flt_header) + le16_to_cpu(flt->length)) >> 1;
705 for (chksum = 0; cnt; cnt--)
706 chksum += le16_to_cpu(*wptr++);
707 if (chksum) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700708 ql_log(ql_log_fatal, vha, 0x0048,
709 "Inconsistent FLT detected: version=0x%x length=0x%x checksum=0x%x.\n",
Andrew Vasquezc00d8992008-09-11 21:22:49 -0700710 le16_to_cpu(flt->version), le16_to_cpu(flt->length),
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700711 le16_to_cpu(flt->checksum));
Andrew Vasquezc00d8992008-09-11 21:22:49 -0700712 goto no_flash_data;
713 }
714
715 loc = locations[1];
716 cnt = le16_to_cpu(flt->length) / sizeof(struct qla_flt_region);
717 for ( ; cnt; cnt--, region++) {
718 /* Store addresses as DWORD offsets. */
719 start = le32_to_cpu(region->start) >> 2;
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700720 ql_dbg(ql_dbg_init, vha, 0x0049,
721 "FLT[%02x]: start=0x%x "
722 "end=0x%x size=0x%x.\n", le32_to_cpu(region->code),
723 start, le32_to_cpu(region->end) >> 2,
724 le32_to_cpu(region->size));
Andrew Vasquezc00d8992008-09-11 21:22:49 -0700725
Andrew Vasquez90886082009-02-08 20:50:14 -0800726 switch (le32_to_cpu(region->code) & 0xff) {
Giridhar Malavali6246b8a2012-02-09 11:15:34 -0800727 case FLT_REG_FCOE_FW:
728 if (!IS_QLA8031(ha))
729 break;
730 ha->flt_region_fw = start;
731 break;
Andrew Vasquezc00d8992008-09-11 21:22:49 -0700732 case FLT_REG_FW:
Giridhar Malavali6246b8a2012-02-09 11:15:34 -0800733 if (IS_QLA8031(ha))
734 break;
Andrew Vasquezc00d8992008-09-11 21:22:49 -0700735 ha->flt_region_fw = start;
736 break;
737 case FLT_REG_BOOT_CODE:
738 ha->flt_region_boot = start;
739 break;
740 case FLT_REG_VPD_0:
Giridhar Malavali6246b8a2012-02-09 11:15:34 -0800741 if (IS_QLA8031(ha))
742 break;
Andrew Vasquezc00d8992008-09-11 21:22:49 -0700743 ha->flt_region_vpd_nvram = start;
Giridhar Malavalia9083012010-04-12 17:59:55 -0700744 if (IS_QLA82XX(ha))
745 break;
Anirban Chakrabortye5b68a62009-04-06 22:33:50 -0700746 if (ha->flags.port0)
Andrew Vasquez3d79038f2009-03-24 09:08:14 -0700747 ha->flt_region_vpd = start;
748 break;
749 case FLT_REG_VPD_1:
Giridhar Malavali6246b8a2012-02-09 11:15:34 -0800750 if (IS_QLA82XX(ha) || IS_QLA8031(ha))
Giridhar Malavalia9083012010-04-12 17:59:55 -0700751 break;
Anirban Chakrabortye5b68a62009-04-06 22:33:50 -0700752 if (!ha->flags.port0)
Andrew Vasquez3d79038f2009-03-24 09:08:14 -0700753 ha->flt_region_vpd = start;
754 break;
755 case FLT_REG_NVRAM_0:
Giridhar Malavali6246b8a2012-02-09 11:15:34 -0800756 if (IS_QLA8031(ha))
757 break;
Anirban Chakrabortye5b68a62009-04-06 22:33:50 -0700758 if (ha->flags.port0)
Andrew Vasquez3d79038f2009-03-24 09:08:14 -0700759 ha->flt_region_nvram = start;
760 break;
761 case FLT_REG_NVRAM_1:
Giridhar Malavali6246b8a2012-02-09 11:15:34 -0800762 if (IS_QLA8031(ha))
763 break;
Anirban Chakrabortye5b68a62009-04-06 22:33:50 -0700764 if (!ha->flags.port0)
Andrew Vasquez3d79038f2009-03-24 09:08:14 -0700765 ha->flt_region_nvram = start;
Andrew Vasquezc00d8992008-09-11 21:22:49 -0700766 break;
767 case FLT_REG_FDT:
768 ha->flt_region_fdt = start;
769 break;
Andrew Vasquez272976c2008-09-11 21:22:50 -0700770 case FLT_REG_NPIV_CONF_0:
Anirban Chakrabortye5b68a62009-04-06 22:33:50 -0700771 if (ha->flags.port0)
Andrew Vasquez272976c2008-09-11 21:22:50 -0700772 ha->flt_region_npiv_conf = start;
773 break;
774 case FLT_REG_NPIV_CONF_1:
Anirban Chakrabortye5b68a62009-04-06 22:33:50 -0700775 if (!ha->flags.port0)
Andrew Vasquez272976c2008-09-11 21:22:50 -0700776 ha->flt_region_npiv_conf = start;
777 break;
Andrew Vasquezcbc8eb62009-06-03 09:55:17 -0700778 case FLT_REG_GOLD_FW:
779 ha->flt_region_gold_fw = start;
780 break;
Sarang Radke09ff7012010-03-19 17:03:59 -0700781 case FLT_REG_FCP_PRIO_0:
Sarang Radkebfdaa762010-03-19 17:04:01 -0700782 if (ha->flags.port0)
Sarang Radke09ff7012010-03-19 17:03:59 -0700783 ha->flt_region_fcp_prio = start;
784 break;
785 case FLT_REG_FCP_PRIO_1:
Sarang Radkebfdaa762010-03-19 17:04:01 -0700786 if (!ha->flags.port0)
Sarang Radke09ff7012010-03-19 17:03:59 -0700787 ha->flt_region_fcp_prio = start;
788 break;
Giridhar Malavalia9083012010-04-12 17:59:55 -0700789 case FLT_REG_BOOT_CODE_82XX:
790 ha->flt_region_boot = start;
791 break;
792 case FLT_REG_FW_82XX:
793 ha->flt_region_fw = start;
794 break;
795 case FLT_REG_GOLD_FW_82XX:
796 ha->flt_region_gold_fw = start;
797 break;
798 case FLT_REG_BOOTLOAD_82XX:
799 ha->flt_region_bootload = start;
800 break;
Saurav Kashyapa865c502013-02-08 01:57:43 -0500801 case FLT_REG_VPD_8XXX:
802 if (IS_CNA_CAPABLE(ha))
Giridhar Malavali6246b8a2012-02-09 11:15:34 -0800803 ha->flt_region_vpd = start;
804 break;
805 case FLT_REG_FCOE_NVRAM_0:
806 if (!IS_QLA8031(ha))
807 break;
808 if (ha->flags.port0)
809 ha->flt_region_nvram = start;
810 break;
811 case FLT_REG_FCOE_NVRAM_1:
812 if (!IS_QLA8031(ha))
813 break;
814 if (!ha->flags.port0)
815 ha->flt_region_nvram = start;
816 break;
Andrew Vasquezc00d8992008-09-11 21:22:49 -0700817 }
818 }
819 goto done;
820
821no_flash_data:
822 /* Use hardcoded defaults. */
823 loc = locations[0];
Andrew Vasquez3a03eb72009-01-05 11:18:11 -0800824 ha->flt_region_fw = def_fw[def];
825 ha->flt_region_boot = def_boot[def];
826 ha->flt_region_vpd_nvram = def_vpd_nvram[def];
Anirban Chakrabortye5b68a62009-04-06 22:33:50 -0700827 ha->flt_region_vpd = ha->flags.port0 ?
Sarang Radke09ff7012010-03-19 17:03:59 -0700828 def_vpd0[def] : def_vpd1[def];
Anirban Chakrabortye5b68a62009-04-06 22:33:50 -0700829 ha->flt_region_nvram = ha->flags.port0 ?
Sarang Radke09ff7012010-03-19 17:03:59 -0700830 def_nvram0[def] : def_nvram1[def];
Andrew Vasquez3a03eb72009-01-05 11:18:11 -0800831 ha->flt_region_fdt = def_fdt[def];
Anirban Chakrabortye5b68a62009-04-06 22:33:50 -0700832 ha->flt_region_npiv_conf = ha->flags.port0 ?
Sarang Radke09ff7012010-03-19 17:03:59 -0700833 def_npiv_conf0[def] : def_npiv_conf1[def];
Andrew Vasquezc00d8992008-09-11 21:22:49 -0700834done:
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700835 ql_dbg(ql_dbg_init, vha, 0x004a,
Giridhar Malavali6246b8a2012-02-09 11:15:34 -0800836 "FLT[%s]: boot=0x%x fw=0x%x vpd_nvram=0x%x vpd=0x%x nvram=0x%x "
837 "fdt=0x%x flt=0x%x npiv=0x%x fcp_prif_cfg=0x%x.\n",
838 loc, ha->flt_region_boot, ha->flt_region_fw,
839 ha->flt_region_vpd_nvram, ha->flt_region_vpd, ha->flt_region_nvram,
840 ha->flt_region_fdt, ha->flt_region_flt, ha->flt_region_npiv_conf,
841 ha->flt_region_fcp_prio);
Andrew Vasquezc00d8992008-09-11 21:22:49 -0700842}
843
844static void
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -0800845qla2xxx_get_fdt_info(scsi_qla_host_t *vha)
Andrew Vasquez7d232c72008-04-03 13:13:22 -0700846{
Lalit Chandivade821b3992008-10-24 15:13:44 -0700847#define FLASH_BLK_SIZE_4K 0x1000
Andrew Vasquez7d232c72008-04-03 13:13:22 -0700848#define FLASH_BLK_SIZE_32K 0x8000
849#define FLASH_BLK_SIZE_64K 0x10000
Andrew Vasquezc00d8992008-09-11 21:22:49 -0700850 const char *loc, *locations[] = { "MID", "FDT" };
Andrew Vasquez7d232c72008-04-03 13:13:22 -0700851 uint16_t cnt, chksum;
852 uint16_t *wptr;
853 struct qla_fdt_layout *fdt;
854 uint8_t man_id, flash_id;
Giridhar Malavalia9083012010-04-12 17:59:55 -0700855 uint16_t mid = 0, fid = 0;
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -0800856 struct qla_hw_data *ha = vha->hw;
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800857 struct req_que *req = ha->req_q_map[0];
Andrew Vasquez7d232c72008-04-03 13:13:22 -0700858
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -0800859 wptr = (uint16_t *)req->ring;
860 fdt = (struct qla_fdt_layout *)req->ring;
861 ha->isp_ops->read_optrom(vha, (uint8_t *)req->ring,
Andrew Vasquezc00d8992008-09-11 21:22:49 -0700862 ha->flt_region_fdt << 2, OPTROM_BURST_SIZE);
Andrew Vasquez7d232c72008-04-03 13:13:22 -0700863 if (*wptr == __constant_cpu_to_le16(0xffff))
864 goto no_flash_data;
865 if (fdt->sig[0] != 'Q' || fdt->sig[1] != 'L' || fdt->sig[2] != 'I' ||
866 fdt->sig[3] != 'D')
867 goto no_flash_data;
868
869 for (cnt = 0, chksum = 0; cnt < sizeof(struct qla_fdt_layout) >> 1;
870 cnt++)
871 chksum += le16_to_cpu(*wptr++);
872 if (chksum) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700873 ql_dbg(ql_dbg_init, vha, 0x004c,
874 "Inconsistent FDT detected:"
875 " checksum=0x%x id=%c version0x%x.\n", chksum,
876 fdt->sig[0], le16_to_cpu(fdt->version));
877 ql_dump_buffer(ql_dbg_init + ql_dbg_buffer, vha, 0x0113,
878 (uint8_t *)fdt, sizeof(*fdt));
Andrew Vasquez7d232c72008-04-03 13:13:22 -0700879 goto no_flash_data;
880 }
881
Andrew Vasquezc00d8992008-09-11 21:22:49 -0700882 loc = locations[1];
883 mid = le16_to_cpu(fdt->man_id);
884 fid = le16_to_cpu(fdt->id);
Andrew Vasquez7d232c72008-04-03 13:13:22 -0700885 ha->fdt_wrt_disable = fdt->wrt_disable_bits;
Andrew Vasquez3a03eb72009-01-05 11:18:11 -0800886 ha->fdt_erase_cmd = flash_conf_addr(ha, 0x0300 | fdt->erase_cmd);
Andrew Vasquez7d232c72008-04-03 13:13:22 -0700887 ha->fdt_block_size = le32_to_cpu(fdt->block_size);
888 if (fdt->unprotect_sec_cmd) {
Andrew Vasquez3a03eb72009-01-05 11:18:11 -0800889 ha->fdt_unprotect_sec_cmd = flash_conf_addr(ha, 0x0300 |
Andrew Vasquez7d232c72008-04-03 13:13:22 -0700890 fdt->unprotect_sec_cmd);
891 ha->fdt_protect_sec_cmd = fdt->protect_sec_cmd ?
Andrew Vasquez3a03eb72009-01-05 11:18:11 -0800892 flash_conf_addr(ha, 0x0300 | fdt->protect_sec_cmd):
893 flash_conf_addr(ha, 0x0336);
Andrew Vasquez7d232c72008-04-03 13:13:22 -0700894 }
Andrew Vasquezc00d8992008-09-11 21:22:49 -0700895 goto done;
Andrew Vasquez7d232c72008-04-03 13:13:22 -0700896no_flash_data:
Andrew Vasquezc00d8992008-09-11 21:22:49 -0700897 loc = locations[0];
Giridhar Malavalia9083012010-04-12 17:59:55 -0700898 if (IS_QLA82XX(ha)) {
899 ha->fdt_block_size = FLASH_BLK_SIZE_64K;
900 goto done;
901 }
Andrew Vasquez7d232c72008-04-03 13:13:22 -0700902 qla24xx_get_flash_manufacturer(ha, &man_id, &flash_id);
Andrew Vasquezc00d8992008-09-11 21:22:49 -0700903 mid = man_id;
904 fid = flash_id;
Andrew Vasquez7d232c72008-04-03 13:13:22 -0700905 ha->fdt_wrt_disable = 0x9c;
Andrew Vasquez3a03eb72009-01-05 11:18:11 -0800906 ha->fdt_erase_cmd = flash_conf_addr(ha, 0x03d8);
Andrew Vasquez7d232c72008-04-03 13:13:22 -0700907 switch (man_id) {
908 case 0xbf: /* STT flash. */
909 if (flash_id == 0x8e)
910 ha->fdt_block_size = FLASH_BLK_SIZE_64K;
911 else
912 ha->fdt_block_size = FLASH_BLK_SIZE_32K;
913
914 if (flash_id == 0x80)
Andrew Vasquez3a03eb72009-01-05 11:18:11 -0800915 ha->fdt_erase_cmd = flash_conf_addr(ha, 0x0352);
Andrew Vasquez7d232c72008-04-03 13:13:22 -0700916 break;
917 case 0x13: /* ST M25P80. */
918 ha->fdt_block_size = FLASH_BLK_SIZE_64K;
919 break;
920 case 0x1f: /* Atmel 26DF081A. */
Lalit Chandivade821b3992008-10-24 15:13:44 -0700921 ha->fdt_block_size = FLASH_BLK_SIZE_4K;
Andrew Vasquez3a03eb72009-01-05 11:18:11 -0800922 ha->fdt_erase_cmd = flash_conf_addr(ha, 0x0320);
923 ha->fdt_unprotect_sec_cmd = flash_conf_addr(ha, 0x0339);
924 ha->fdt_protect_sec_cmd = flash_conf_addr(ha, 0x0336);
Andrew Vasquez7d232c72008-04-03 13:13:22 -0700925 break;
926 default:
927 /* Default to 64 kb sector size. */
928 ha->fdt_block_size = FLASH_BLK_SIZE_64K;
929 break;
930 }
Andrew Vasquezc00d8992008-09-11 21:22:49 -0700931done:
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700932 ql_dbg(ql_dbg_init, vha, 0x004d,
Joe Perchesd8424f62011-11-18 09:03:06 -0800933 "FDT[%s]: (0x%x/0x%x) erase=0x%x "
934 "pr=%x wrtd=0x%x blk=0x%x.\n",
935 loc, mid, fid,
Andrew Vasquez7d232c72008-04-03 13:13:22 -0700936 ha->fdt_erase_cmd, ha->fdt_protect_sec_cmd,
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700937 ha->fdt_wrt_disable, ha->fdt_block_size);
938
Andrew Vasquez7d232c72008-04-03 13:13:22 -0700939}
940
Giridhar Malavalia9083012010-04-12 17:59:55 -0700941static void
942qla2xxx_get_idc_param(scsi_qla_host_t *vha)
943{
944#define QLA82XX_IDC_PARAM_ADDR 0x003e885c
945 uint32_t *wptr;
946 struct qla_hw_data *ha = vha->hw;
947 struct req_que *req = ha->req_q_map[0];
948
949 if (!IS_QLA82XX(ha))
950 return;
951
952 wptr = (uint32_t *)req->ring;
953 ha->isp_ops->read_optrom(vha, (uint8_t *)req->ring,
954 QLA82XX_IDC_PARAM_ADDR , 8);
955
956 if (*wptr == __constant_cpu_to_le32(0xffffffff)) {
Santosh Vernekar7d613ac2012-08-22 14:21:03 -0400957 ha->fcoe_dev_init_timeout = QLA82XX_ROM_DEV_INIT_TIMEOUT;
958 ha->fcoe_reset_timeout = QLA82XX_ROM_DRV_RESET_ACK_TIMEOUT;
Giridhar Malavalia9083012010-04-12 17:59:55 -0700959 } else {
Santosh Vernekar7d613ac2012-08-22 14:21:03 -0400960 ha->fcoe_dev_init_timeout = le32_to_cpu(*wptr++);
961 ha->fcoe_reset_timeout = le32_to_cpu(*wptr);
Giridhar Malavalia9083012010-04-12 17:59:55 -0700962 }
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700963 ql_dbg(ql_dbg_init, vha, 0x004e,
Santosh Vernekar7d613ac2012-08-22 14:21:03 -0400964 "fcoe_dev_init_timeout=%d "
965 "fcoe_reset_timeout=%d.\n", ha->fcoe_dev_init_timeout,
966 ha->fcoe_reset_timeout);
Giridhar Malavalia9083012010-04-12 17:59:55 -0700967 return;
968}
969
Andrew Vasquezc00d8992008-09-11 21:22:49 -0700970int
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -0800971qla2xxx_get_flash_info(scsi_qla_host_t *vha)
Andrew Vasquezc00d8992008-09-11 21:22:49 -0700972{
973 int ret;
974 uint32_t flt_addr;
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -0800975 struct qla_hw_data *ha = vha->hw;
Andrew Vasquezc00d8992008-09-11 21:22:49 -0700976
Giridhar Malavali6246b8a2012-02-09 11:15:34 -0800977 if (!IS_QLA24XX_TYPE(ha) && !IS_QLA25XX(ha) &&
978 !IS_CNA_CAPABLE(ha) && !IS_QLA2031(ha))
Andrew Vasquezc00d8992008-09-11 21:22:49 -0700979 return QLA_SUCCESS;
980
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -0800981 ret = qla2xxx_find_flt_start(vha, &flt_addr);
Andrew Vasquezc00d8992008-09-11 21:22:49 -0700982 if (ret != QLA_SUCCESS)
983 return ret;
984
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -0800985 qla2xxx_get_flt_info(vha, flt_addr);
986 qla2xxx_get_fdt_info(vha);
Giridhar Malavalia9083012010-04-12 17:59:55 -0700987 qla2xxx_get_idc_param(vha);
Andrew Vasquezc00d8992008-09-11 21:22:49 -0700988
989 return QLA_SUCCESS;
990}
991
Andrew Vasquez272976c2008-09-11 21:22:50 -0700992void
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -0800993qla2xxx_flash_npiv_conf(scsi_qla_host_t *vha)
Andrew Vasquez272976c2008-09-11 21:22:50 -0700994{
995#define NPIV_CONFIG_SIZE (16*1024)
996 void *data;
997 uint16_t *wptr;
998 uint16_t cnt, chksum;
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800999 int i;
Andrew Vasquez272976c2008-09-11 21:22:50 -07001000 struct qla_npiv_header hdr;
1001 struct qla_npiv_entry *entry;
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -08001002 struct qla_hw_data *ha = vha->hw;
Andrew Vasquez272976c2008-09-11 21:22:50 -07001003
Giridhar Malavali6246b8a2012-02-09 11:15:34 -08001004 if (!IS_QLA24XX_TYPE(ha) && !IS_QLA25XX(ha) &&
1005 !IS_CNA_CAPABLE(ha) && !IS_QLA2031(ha))
Andrew Vasquez272976c2008-09-11 21:22:50 -07001006 return;
1007
Santosh Vernekar7d613ac2012-08-22 14:21:03 -04001008 if (ha->flags.nic_core_reset_hdlr_active)
Giridhar Malavalia49393f2012-04-25 07:26:14 -07001009 return;
1010
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -08001011 ha->isp_ops->read_optrom(vha, (uint8_t *)&hdr,
Andrew Vasquez272976c2008-09-11 21:22:50 -07001012 ha->flt_region_npiv_conf << 2, sizeof(struct qla_npiv_header));
1013 if (hdr.version == __constant_cpu_to_le16(0xffff))
1014 return;
1015 if (hdr.version != __constant_cpu_to_le16(1)) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001016 ql_dbg(ql_dbg_user, vha, 0x7090,
1017 "Unsupported NPIV-Config "
Andrew Vasquez272976c2008-09-11 21:22:50 -07001018 "detected: version=0x%x entries=0x%x checksum=0x%x.\n",
1019 le16_to_cpu(hdr.version), le16_to_cpu(hdr.entries),
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001020 le16_to_cpu(hdr.checksum));
Andrew Vasquez272976c2008-09-11 21:22:50 -07001021 return;
1022 }
1023
1024 data = kmalloc(NPIV_CONFIG_SIZE, GFP_KERNEL);
1025 if (!data) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001026 ql_log(ql_log_warn, vha, 0x7091,
1027 "Unable to allocate memory for data.\n");
Andrew Vasquez272976c2008-09-11 21:22:50 -07001028 return;
1029 }
1030
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -08001031 ha->isp_ops->read_optrom(vha, (uint8_t *)data,
Andrew Vasquez272976c2008-09-11 21:22:50 -07001032 ha->flt_region_npiv_conf << 2, NPIV_CONFIG_SIZE);
1033
1034 cnt = (sizeof(struct qla_npiv_header) + le16_to_cpu(hdr.entries) *
1035 sizeof(struct qla_npiv_entry)) >> 1;
1036 for (wptr = data, chksum = 0; cnt; cnt--)
1037 chksum += le16_to_cpu(*wptr++);
1038 if (chksum) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001039 ql_dbg(ql_dbg_user, vha, 0x7092,
1040 "Inconsistent NPIV-Config "
Andrew Vasquez272976c2008-09-11 21:22:50 -07001041 "detected: version=0x%x entries=0x%x checksum=0x%x.\n",
1042 le16_to_cpu(hdr.version), le16_to_cpu(hdr.entries),
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001043 le16_to_cpu(hdr.checksum));
Andrew Vasquez272976c2008-09-11 21:22:50 -07001044 goto done;
1045 }
1046
1047 entry = data + sizeof(struct qla_npiv_header);
1048 cnt = le16_to_cpu(hdr.entries);
Anirban Chakraborty73208df2008-12-09 16:45:39 -08001049 for (i = 0; cnt; cnt--, entry++, i++) {
Andrew Vasquez272976c2008-09-11 21:22:50 -07001050 uint16_t flags;
1051 struct fc_vport_identifiers vid;
1052 struct fc_vport *vport;
1053
Anirban Chakraborty40859ae2009-06-03 09:55:16 -07001054 memcpy(&ha->npiv_info[i], entry, sizeof(struct qla_npiv_entry));
1055
Andrew Vasquez272976c2008-09-11 21:22:50 -07001056 flags = le16_to_cpu(entry->flags);
1057 if (flags == 0xffff)
1058 continue;
1059 if ((flags & BIT_0) == 0)
1060 continue;
1061
1062 memset(&vid, 0, sizeof(vid));
1063 vid.roles = FC_PORT_ROLE_FCP_INITIATOR;
1064 vid.vport_type = FC_PORTTYPE_NPIV;
1065 vid.disable = false;
1066 vid.port_name = wwn_to_u64(entry->port_name);
1067 vid.node_name = wwn_to_u64(entry->node_name);
1068
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001069 ql_dbg(ql_dbg_user, vha, 0x7093,
1070 "NPIV[%02x]: wwpn=%llx "
1071 "wwnn=%llx vf_id=0x%x Q_qos=0x%x F_qos=0x%x.\n", cnt,
1072 (unsigned long long)vid.port_name,
1073 (unsigned long long)vid.node_name,
1074 le16_to_cpu(entry->vf_id),
1075 entry->q_qos, entry->f_qos);
Anirban Chakraborty73208df2008-12-09 16:45:39 -08001076
1077 if (i < QLA_PRECONFIG_VPORTS) {
1078 vport = fc_vport_create(vha->host, 0, &vid);
1079 if (!vport)
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001080 ql_log(ql_log_warn, vha, 0x7094,
1081 "NPIV-Config Failed to create vport [%02x]: "
1082 "wwpn=%llx wwnn=%llx.\n", cnt,
1083 (unsigned long long)vid.port_name,
1084 (unsigned long long)vid.node_name);
Anirban Chakraborty73208df2008-12-09 16:45:39 -08001085 }
Andrew Vasquez272976c2008-09-11 21:22:50 -07001086 }
1087done:
1088 kfree(data);
1089}
1090
Joe Carnuccio1d2874d2009-03-24 09:08:06 -07001091static int
1092qla24xx_unprotect_flash(scsi_qla_host_t *vha)
Andrew Vasquezcb8dacb2008-04-03 13:13:19 -07001093{
Joe Carnuccio1d2874d2009-03-24 09:08:06 -07001094 struct qla_hw_data *ha = vha->hw;
Andrew Vasquezcb8dacb2008-04-03 13:13:19 -07001095 struct device_reg_24xx __iomem *reg = &ha->iobase->isp24;
1096
Joe Carnuccio1d2874d2009-03-24 09:08:06 -07001097 if (ha->flags.fac_supported)
1098 return qla81xx_fac_do_write_enable(vha, 1);
1099
Andrew Vasquezcb8dacb2008-04-03 13:13:19 -07001100 /* Enable flash write. */
1101 WRT_REG_DWORD(&reg->ctrl_status,
1102 RD_REG_DWORD(&reg->ctrl_status) | CSRX_FLASH_ENABLE);
1103 RD_REG_DWORD(&reg->ctrl_status); /* PCI Posting. */
1104
Andrew Vasquez7d232c72008-04-03 13:13:22 -07001105 if (!ha->fdt_wrt_disable)
Joe Carnuccio1d2874d2009-03-24 09:08:06 -07001106 goto done;
Andrew Vasquez7d232c72008-04-03 13:13:22 -07001107
Joe Carnucciob872ca42009-01-22 09:45:36 -08001108 /* Disable flash write-protection, first clear SR protection bit */
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08001109 qla24xx_write_flash_dword(ha, flash_conf_addr(ha, 0x101), 0);
Joe Carnucciob872ca42009-01-22 09:45:36 -08001110 /* Then write zero again to clear remaining SR bits.*/
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08001111 qla24xx_write_flash_dword(ha, flash_conf_addr(ha, 0x101), 0);
Joe Carnuccio1d2874d2009-03-24 09:08:06 -07001112done:
1113 return QLA_SUCCESS;
Andrew Vasquezcb8dacb2008-04-03 13:13:19 -07001114}
1115
Joe Carnuccio1d2874d2009-03-24 09:08:06 -07001116static int
1117qla24xx_protect_flash(scsi_qla_host_t *vha)
Andrew Vasquezcb8dacb2008-04-03 13:13:19 -07001118{
1119 uint32_t cnt;
Joe Carnuccio1d2874d2009-03-24 09:08:06 -07001120 struct qla_hw_data *ha = vha->hw;
Andrew Vasquezcb8dacb2008-04-03 13:13:19 -07001121 struct device_reg_24xx __iomem *reg = &ha->iobase->isp24;
1122
Joe Carnuccio1d2874d2009-03-24 09:08:06 -07001123 if (ha->flags.fac_supported)
1124 return qla81xx_fac_do_write_enable(vha, 0);
1125
Andrew Vasquez7d232c72008-04-03 13:13:22 -07001126 if (!ha->fdt_wrt_disable)
1127 goto skip_wrt_protect;
1128
Andrew Vasquezcb8dacb2008-04-03 13:13:19 -07001129 /* Enable flash write-protection and wait for completion. */
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08001130 qla24xx_write_flash_dword(ha, flash_conf_addr(ha, 0x101),
Andrew Vasquez7d232c72008-04-03 13:13:22 -07001131 ha->fdt_wrt_disable);
Andrew Vasquezcb8dacb2008-04-03 13:13:19 -07001132 for (cnt = 300; cnt &&
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08001133 qla24xx_read_flash_dword(ha, flash_conf_addr(ha, 0x005)) & BIT_0;
Andrew Vasquezcb8dacb2008-04-03 13:13:19 -07001134 cnt--) {
1135 udelay(10);
1136 }
1137
Andrew Vasquez7d232c72008-04-03 13:13:22 -07001138skip_wrt_protect:
Andrew Vasquezcb8dacb2008-04-03 13:13:19 -07001139 /* Disable flash write. */
1140 WRT_REG_DWORD(&reg->ctrl_status,
1141 RD_REG_DWORD(&reg->ctrl_status) & ~CSRX_FLASH_ENABLE);
1142 RD_REG_DWORD(&reg->ctrl_status); /* PCI Posting. */
Joe Carnuccio1d2874d2009-03-24 09:08:06 -07001143
1144 return QLA_SUCCESS;
1145}
1146
1147static int
1148qla24xx_erase_sector(scsi_qla_host_t *vha, uint32_t fdata)
1149{
1150 struct qla_hw_data *ha = vha->hw;
1151 uint32_t start, finish;
1152
1153 if (ha->flags.fac_supported) {
1154 start = fdata >> 2;
1155 finish = start + (ha->fdt_block_size >> 2) - 1;
1156 return qla81xx_fac_erase_sector(vha, flash_data_addr(ha,
1157 start), flash_data_addr(ha, finish));
1158 }
1159
1160 return qla24xx_write_flash_dword(ha, ha->fdt_erase_cmd,
1161 (fdata & 0xff00) | ((fdata << 16) & 0xff0000) |
1162 ((fdata >> 16) & 0xff));
Andrew Vasquezcb8dacb2008-04-03 13:13:19 -07001163}
1164
Adrian Bunke5f82ab2006-11-08 19:55:50 -08001165static int
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -08001166qla24xx_write_flash_data(scsi_qla_host_t *vha, uint32_t *dwptr, uint32_t faddr,
Andrew Vasquez459c5372005-07-06 10:31:07 -07001167 uint32_t dwords)
1168{
1169 int ret;
Andrew Vasquez7c283172009-01-22 09:45:34 -08001170 uint32_t liter;
Andrew Vasquez7d232c72008-04-03 13:13:22 -07001171 uint32_t sec_mask, rest_addr;
Andrew Vasquez85d0acb2009-01-22 09:45:29 -08001172 uint32_t fdata;
Andrew Vasquez338c9162007-09-20 14:07:33 -07001173 dma_addr_t optrom_dma;
1174 void *optrom = NULL;
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -08001175 struct qla_hw_data *ha = vha->hw;
Andrew Vasquez459c5372005-07-06 10:31:07 -07001176
Andrew Vasquez338c9162007-09-20 14:07:33 -07001177 /* Prepare burst-capable write on supported ISPs. */
Giridhar Malavali6246b8a2012-02-09 11:15:34 -08001178 if ((IS_QLA25XX(ha) || IS_QLA81XX(ha) || IS_QLA83XX(ha)) &&
1179 !(faddr & 0xfff) && dwords > OPTROM_BURST_DWORDS) {
Andrew Vasquez338c9162007-09-20 14:07:33 -07001180 optrom = dma_alloc_coherent(&ha->pdev->dev, OPTROM_BURST_SIZE,
1181 &optrom_dma, GFP_KERNEL);
1182 if (!optrom) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001183 ql_log(ql_log_warn, vha, 0x7095,
1184 "Unable to allocate "
1185 "memory for optrom burst write (%x KB).\n",
1186 OPTROM_BURST_SIZE / 1024);
Andrew Vasquez338c9162007-09-20 14:07:33 -07001187 }
1188 }
1189
Andrew Vasquez7d232c72008-04-03 13:13:22 -07001190 rest_addr = (ha->fdt_block_size >> 2) - 1;
Andrew Vasquez85d0acb2009-01-22 09:45:29 -08001191 sec_mask = ~rest_addr;
Andrew Vasquez459c5372005-07-06 10:31:07 -07001192
Joe Carnuccio1d2874d2009-03-24 09:08:06 -07001193 ret = qla24xx_unprotect_flash(vha);
1194 if (ret != QLA_SUCCESS) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001195 ql_log(ql_log_warn, vha, 0x7096,
Joe Carnuccio1d2874d2009-03-24 09:08:06 -07001196 "Unable to unprotect flash for update.\n");
1197 goto done;
1198 }
Andrew Vasquez459c5372005-07-06 10:31:07 -07001199
Andrew Vasquez338c9162007-09-20 14:07:33 -07001200 for (liter = 0; liter < dwords; liter++, faddr++, dwptr++) {
Andrew Vasquez85d0acb2009-01-22 09:45:29 -08001201 fdata = (faddr & sec_mask) << 2;
Ravi Anand45aeaf12006-05-17 15:08:49 -07001202
Andrew Vasquez338c9162007-09-20 14:07:33 -07001203 /* Are we at the beginning of a sector? */
Andrew Vasquez85d0acb2009-01-22 09:45:29 -08001204 if ((faddr & rest_addr) == 0) {
Andrew Vasquez7d232c72008-04-03 13:13:22 -07001205 /* Do sector unprotect. */
1206 if (ha->fdt_unprotect_sec_cmd)
Ravi Anand45aeaf12006-05-17 15:08:49 -07001207 qla24xx_write_flash_dword(ha,
Andrew Vasquez7d232c72008-04-03 13:13:22 -07001208 ha->fdt_unprotect_sec_cmd,
Ravi Anand45aeaf12006-05-17 15:08:49 -07001209 (fdata & 0xff00) | ((fdata << 16) &
1210 0xff0000) | ((fdata >> 16) & 0xff));
Joe Carnuccio1d2874d2009-03-24 09:08:06 -07001211 ret = qla24xx_erase_sector(vha, fdata);
Andrew Vasquez338c9162007-09-20 14:07:33 -07001212 if (ret != QLA_SUCCESS) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001213 ql_dbg(ql_dbg_user, vha, 0x7007,
1214 "Unable to erase erase sector: address=%x.\n",
1215 faddr);
Andrew Vasquez338c9162007-09-20 14:07:33 -07001216 break;
1217 }
Andrew Vasquez459c5372005-07-06 10:31:07 -07001218 }
Andrew Vasquez338c9162007-09-20 14:07:33 -07001219
1220 /* Go with burst-write. */
Andrew Vasquez94d6a2b2007-10-19 15:59:16 -07001221 if (optrom && (liter + OPTROM_BURST_DWORDS) <= dwords) {
Andrew Vasquez338c9162007-09-20 14:07:33 -07001222 /* Copy data to DMA'ble buffer. */
Andrew Vasquez7c283172009-01-22 09:45:34 -08001223 memcpy(optrom, dwptr, OPTROM_BURST_SIZE);
Andrew Vasquez338c9162007-09-20 14:07:33 -07001224
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -08001225 ret = qla2x00_load_ram(vha, optrom_dma,
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08001226 flash_data_addr(ha, faddr),
Andrew Vasquez338c9162007-09-20 14:07:33 -07001227 OPTROM_BURST_DWORDS);
1228 if (ret != QLA_SUCCESS) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001229 ql_log(ql_log_warn, vha, 0x7097,
Andrew Vasquez338c9162007-09-20 14:07:33 -07001230 "Unable to burst-write optrom segment "
1231 "(%x/%x/%llx).\n", ret,
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08001232 flash_data_addr(ha, faddr),
Andrew Morton875baf32007-10-16 14:28:20 -07001233 (unsigned long long)optrom_dma);
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001234 ql_log(ql_log_warn, vha, 0x7098,
Andrew Vasquez338c9162007-09-20 14:07:33 -07001235 "Reverting to slow-write.\n");
1236
1237 dma_free_coherent(&ha->pdev->dev,
1238 OPTROM_BURST_SIZE, optrom, optrom_dma);
1239 optrom = NULL;
1240 } else {
1241 liter += OPTROM_BURST_DWORDS - 1;
1242 faddr += OPTROM_BURST_DWORDS - 1;
1243 dwptr += OPTROM_BURST_DWORDS - 1;
1244 continue;
1245 }
1246 }
1247
1248 ret = qla24xx_write_flash_dword(ha,
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08001249 flash_data_addr(ha, faddr), cpu_to_le32(*dwptr));
Andrew Vasquez338c9162007-09-20 14:07:33 -07001250 if (ret != QLA_SUCCESS) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001251 ql_dbg(ql_dbg_user, vha, 0x7006,
1252 "Unable to program flash address=%x data=%x.\n",
1253 faddr, *dwptr);
Andrew Vasquez338c9162007-09-20 14:07:33 -07001254 break;
1255 }
1256
Andrew Vasquez7d232c72008-04-03 13:13:22 -07001257 /* Do sector protect. */
1258 if (ha->fdt_unprotect_sec_cmd &&
Andrew Vasquez338c9162007-09-20 14:07:33 -07001259 ((faddr & rest_addr) == rest_addr))
1260 qla24xx_write_flash_dword(ha,
Andrew Vasquez7d232c72008-04-03 13:13:22 -07001261 ha->fdt_protect_sec_cmd,
Andrew Vasquez338c9162007-09-20 14:07:33 -07001262 (fdata & 0xff00) | ((fdata << 16) &
1263 0xff0000) | ((fdata >> 16) & 0xff));
1264 }
Andrew Vasquez459c5372005-07-06 10:31:07 -07001265
Joe Carnuccio1d2874d2009-03-24 09:08:06 -07001266 ret = qla24xx_protect_flash(vha);
1267 if (ret != QLA_SUCCESS)
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001268 ql_log(ql_log_warn, vha, 0x7099,
Joe Carnuccio1d2874d2009-03-24 09:08:06 -07001269 "Unable to protect flash after update.\n");
1270done:
Andrew Vasquez338c9162007-09-20 14:07:33 -07001271 if (optrom)
1272 dma_free_coherent(&ha->pdev->dev,
1273 OPTROM_BURST_SIZE, optrom, optrom_dma);
1274
Andrew Vasquez459c5372005-07-06 10:31:07 -07001275 return ret;
1276}
1277
1278uint8_t *
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -08001279qla2x00_read_nvram_data(scsi_qla_host_t *vha, uint8_t *buf, uint32_t naddr,
Andrew Vasquez459c5372005-07-06 10:31:07 -07001280 uint32_t bytes)
1281{
1282 uint32_t i;
1283 uint16_t *wptr;
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -08001284 struct qla_hw_data *ha = vha->hw;
Andrew Vasquez459c5372005-07-06 10:31:07 -07001285
1286 /* Word reads to NVRAM via registers. */
1287 wptr = (uint16_t *)buf;
1288 qla2x00_lock_nvram_access(ha);
1289 for (i = 0; i < bytes >> 1; i++, naddr++)
1290 wptr[i] = cpu_to_le16(qla2x00_get_nvram_word(ha,
1291 naddr));
1292 qla2x00_unlock_nvram_access(ha);
1293
1294 return buf;
1295}
1296
1297uint8_t *
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -08001298qla24xx_read_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 uint32_t i;
1302 uint32_t *dwptr;
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08001303 struct qla_hw_data *ha = vha->hw;
Andrew Vasquez459c5372005-07-06 10:31:07 -07001304
Giridhar Malavalia9083012010-04-12 17:59:55 -07001305 if (IS_QLA82XX(ha))
1306 return buf;
1307
Andrew Vasquez459c5372005-07-06 10:31:07 -07001308 /* Dword reads to flash. */
1309 dwptr = (uint32_t *)buf;
1310 for (i = 0; i < bytes >> 2; i++, naddr++)
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08001311 dwptr[i] = cpu_to_le32(qla24xx_read_flash_dword(ha,
1312 nvram_data_addr(ha, naddr)));
Andrew Vasquez459c5372005-07-06 10:31:07 -07001313
Andrew Vasquez459c5372005-07-06 10:31:07 -07001314 return buf;
1315}
1316
1317int
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -08001318qla2x00_write_nvram_data(scsi_qla_host_t *vha, uint8_t *buf, uint32_t naddr,
Andrew Vasquez459c5372005-07-06 10:31:07 -07001319 uint32_t bytes)
1320{
1321 int ret, stat;
1322 uint32_t i;
1323 uint16_t *wptr;
Andrew Vasquez2c96d8d2007-10-19 15:59:15 -07001324 unsigned long flags;
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -08001325 struct qla_hw_data *ha = vha->hw;
Andrew Vasquez459c5372005-07-06 10:31:07 -07001326
1327 ret = QLA_SUCCESS;
1328
Andrew Vasquez2c96d8d2007-10-19 15:59:15 -07001329 spin_lock_irqsave(&ha->hardware_lock, flags);
Andrew Vasquez459c5372005-07-06 10:31:07 -07001330 qla2x00_lock_nvram_access(ha);
1331
1332 /* Disable NVRAM write-protection. */
1333 stat = qla2x00_clear_nvram_protection(ha);
1334
1335 wptr = (uint16_t *)buf;
1336 for (i = 0; i < bytes >> 1; i++, naddr++) {
1337 qla2x00_write_nvram_word(ha, naddr,
1338 cpu_to_le16(*wptr));
1339 wptr++;
1340 }
1341
1342 /* Enable NVRAM write-protection. */
1343 qla2x00_set_nvram_protection(ha, stat);
1344
1345 qla2x00_unlock_nvram_access(ha);
Andrew Vasquez2c96d8d2007-10-19 15:59:15 -07001346 spin_unlock_irqrestore(&ha->hardware_lock, flags);
Andrew Vasquez459c5372005-07-06 10:31:07 -07001347
1348 return ret;
1349}
1350
1351int
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -08001352qla24xx_write_nvram_data(scsi_qla_host_t *vha, uint8_t *buf, uint32_t naddr,
Andrew Vasquez459c5372005-07-06 10:31:07 -07001353 uint32_t bytes)
1354{
1355 int ret;
1356 uint32_t i;
1357 uint32_t *dwptr;
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -08001358 struct qla_hw_data *ha = vha->hw;
Andrew Vasquez459c5372005-07-06 10:31:07 -07001359 struct device_reg_24xx __iomem *reg = &ha->iobase->isp24;
1360
1361 ret = QLA_SUCCESS;
1362
Giridhar Malavalia9083012010-04-12 17:59:55 -07001363 if (IS_QLA82XX(ha))
1364 return ret;
1365
Andrew Vasquez459c5372005-07-06 10:31:07 -07001366 /* Enable flash write. */
1367 WRT_REG_DWORD(&reg->ctrl_status,
1368 RD_REG_DWORD(&reg->ctrl_status) | CSRX_FLASH_ENABLE);
1369 RD_REG_DWORD(&reg->ctrl_status); /* PCI Posting. */
1370
1371 /* Disable NVRAM write-protection. */
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08001372 qla24xx_write_flash_dword(ha, nvram_conf_addr(ha, 0x101), 0);
1373 qla24xx_write_flash_dword(ha, nvram_conf_addr(ha, 0x101), 0);
Andrew Vasquez459c5372005-07-06 10:31:07 -07001374
1375 /* Dword writes to flash. */
1376 dwptr = (uint32_t *)buf;
1377 for (i = 0; i < bytes >> 2; i++, naddr++, dwptr++) {
1378 ret = qla24xx_write_flash_dword(ha,
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08001379 nvram_data_addr(ha, naddr), cpu_to_le32(*dwptr));
Andrew Vasquez459c5372005-07-06 10:31:07 -07001380 if (ret != QLA_SUCCESS) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001381 ql_dbg(ql_dbg_user, vha, 0x709a,
Andrew Vasquez76403352009-04-06 22:33:39 -07001382 "Unable to program nvram address=%x data=%x.\n",
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001383 naddr, *dwptr);
Andrew Vasquez459c5372005-07-06 10:31:07 -07001384 break;
1385 }
1386 }
1387
1388 /* Enable NVRAM write-protection. */
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08001389 qla24xx_write_flash_dword(ha, nvram_conf_addr(ha, 0x101), 0x8c);
Andrew Vasquez459c5372005-07-06 10:31:07 -07001390
1391 /* Disable flash write. */
1392 WRT_REG_DWORD(&reg->ctrl_status,
1393 RD_REG_DWORD(&reg->ctrl_status) & ~CSRX_FLASH_ENABLE);
1394 RD_REG_DWORD(&reg->ctrl_status); /* PCI Posting. */
1395
Andrew Vasquez459c5372005-07-06 10:31:07 -07001396 return ret;
1397}
andrew.vasquez@qlogic.comf6df1442006-01-31 16:05:07 -08001398
Andrew Vasquezc3a2f0d2007-07-19 20:37:34 -07001399uint8_t *
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -08001400qla25xx_read_nvram_data(scsi_qla_host_t *vha, uint8_t *buf, uint32_t naddr,
Andrew Vasquezc3a2f0d2007-07-19 20:37:34 -07001401 uint32_t bytes)
1402{
1403 uint32_t i;
1404 uint32_t *dwptr;
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -08001405 struct qla_hw_data *ha = vha->hw;
Andrew Vasquezc3a2f0d2007-07-19 20:37:34 -07001406
1407 /* Dword reads to flash. */
1408 dwptr = (uint32_t *)buf;
1409 for (i = 0; i < bytes >> 2; i++, naddr++)
1410 dwptr[i] = cpu_to_le32(qla24xx_read_flash_dword(ha,
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08001411 flash_data_addr(ha, ha->flt_region_vpd_nvram | naddr)));
Andrew Vasquezc3a2f0d2007-07-19 20:37:34 -07001412
1413 return buf;
1414}
1415
1416int
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -08001417qla25xx_write_nvram_data(scsi_qla_host_t *vha, uint8_t *buf, uint32_t naddr,
Andrew Vasquezc3a2f0d2007-07-19 20:37:34 -07001418 uint32_t bytes)
1419{
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -08001420 struct qla_hw_data *ha = vha->hw;
Andrew Vasquez2c96d8d2007-10-19 15:59:15 -07001421#define RMW_BUFFER_SIZE (64 * 1024)
1422 uint8_t *dbuf;
1423
1424 dbuf = vmalloc(RMW_BUFFER_SIZE);
1425 if (!dbuf)
1426 return QLA_MEMORY_ALLOC_FAILED;
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -08001427 ha->isp_ops->read_optrom(vha, dbuf, ha->flt_region_vpd_nvram << 2,
Andrew Vasquez2c96d8d2007-10-19 15:59:15 -07001428 RMW_BUFFER_SIZE);
1429 memcpy(dbuf + (naddr << 2), buf, bytes);
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -08001430 ha->isp_ops->write_optrom(vha, dbuf, ha->flt_region_vpd_nvram << 2,
Andrew Vasquez2c96d8d2007-10-19 15:59:15 -07001431 RMW_BUFFER_SIZE);
1432 vfree(dbuf);
1433
1434 return QLA_SUCCESS;
Andrew Vasquezc3a2f0d2007-07-19 20:37:34 -07001435}
andrew.vasquez@qlogic.comf6df1442006-01-31 16:05:07 -08001436
1437static inline void
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -08001438qla2x00_flip_colors(struct qla_hw_data *ha, uint16_t *pflags)
andrew.vasquez@qlogic.comf6df1442006-01-31 16:05:07 -08001439{
1440 if (IS_QLA2322(ha)) {
1441 /* Flip all colors. */
1442 if (ha->beacon_color_state == QLA_LED_ALL_ON) {
1443 /* Turn off. */
1444 ha->beacon_color_state = 0;
1445 *pflags = GPIO_LED_ALL_OFF;
1446 } else {
1447 /* Turn on. */
1448 ha->beacon_color_state = QLA_LED_ALL_ON;
1449 *pflags = GPIO_LED_RGA_ON;
1450 }
1451 } else {
1452 /* Flip green led only. */
1453 if (ha->beacon_color_state == QLA_LED_GRN_ON) {
1454 /* Turn off. */
1455 ha->beacon_color_state = 0;
1456 *pflags = GPIO_LED_GREEN_OFF_AMBER_OFF;
1457 } else {
1458 /* Turn on. */
1459 ha->beacon_color_state = QLA_LED_GRN_ON;
1460 *pflags = GPIO_LED_GREEN_ON_AMBER_OFF;
1461 }
1462 }
1463}
1464
Andrew Vasquez948882f2008-01-31 12:33:44 -08001465#define PIO_REG(h, r) ((h)->pio_address + offsetof(struct device_reg_2xxx, r))
1466
andrew.vasquez@qlogic.comf6df1442006-01-31 16:05:07 -08001467void
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -08001468qla2x00_beacon_blink(struct scsi_qla_host *vha)
andrew.vasquez@qlogic.comf6df1442006-01-31 16:05:07 -08001469{
1470 uint16_t gpio_enable;
1471 uint16_t gpio_data;
1472 uint16_t led_color = 0;
1473 unsigned long flags;
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -08001474 struct qla_hw_data *ha = vha->hw;
andrew.vasquez@qlogic.comf6df1442006-01-31 16:05:07 -08001475 struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
1476
Giridhar Malavalia9083012010-04-12 17:59:55 -07001477 if (IS_QLA82XX(ha))
1478 return;
1479
andrew.vasquez@qlogic.comf6df1442006-01-31 16:05:07 -08001480 spin_lock_irqsave(&ha->hardware_lock, flags);
1481
1482 /* Save the Original GPIOE. */
1483 if (ha->pio_address) {
Andrew Vasquez948882f2008-01-31 12:33:44 -08001484 gpio_enable = RD_REG_WORD_PIO(PIO_REG(ha, gpioe));
1485 gpio_data = RD_REG_WORD_PIO(PIO_REG(ha, gpiod));
andrew.vasquez@qlogic.comf6df1442006-01-31 16:05:07 -08001486 } else {
1487 gpio_enable = RD_REG_WORD(&reg->gpioe);
1488 gpio_data = RD_REG_WORD(&reg->gpiod);
1489 }
1490
1491 /* Set the modified gpio_enable values */
1492 gpio_enable |= GPIO_LED_MASK;
1493
1494 if (ha->pio_address) {
Andrew Vasquez948882f2008-01-31 12:33:44 -08001495 WRT_REG_WORD_PIO(PIO_REG(ha, gpioe), gpio_enable);
andrew.vasquez@qlogic.comf6df1442006-01-31 16:05:07 -08001496 } else {
1497 WRT_REG_WORD(&reg->gpioe, gpio_enable);
1498 RD_REG_WORD(&reg->gpioe);
1499 }
1500
1501 qla2x00_flip_colors(ha, &led_color);
1502
1503 /* Clear out any previously set LED color. */
1504 gpio_data &= ~GPIO_LED_MASK;
1505
1506 /* Set the new input LED color to GPIOD. */
1507 gpio_data |= led_color;
1508
1509 /* Set the modified gpio_data values */
1510 if (ha->pio_address) {
Andrew Vasquez948882f2008-01-31 12:33:44 -08001511 WRT_REG_WORD_PIO(PIO_REG(ha, gpiod), gpio_data);
andrew.vasquez@qlogic.comf6df1442006-01-31 16:05:07 -08001512 } else {
1513 WRT_REG_WORD(&reg->gpiod, gpio_data);
1514 RD_REG_WORD(&reg->gpiod);
1515 }
1516
1517 spin_unlock_irqrestore(&ha->hardware_lock, flags);
1518}
1519
1520int
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -08001521qla2x00_beacon_on(struct scsi_qla_host *vha)
andrew.vasquez@qlogic.comf6df1442006-01-31 16:05:07 -08001522{
1523 uint16_t gpio_enable;
1524 uint16_t gpio_data;
1525 unsigned long flags;
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -08001526 struct qla_hw_data *ha = vha->hw;
andrew.vasquez@qlogic.comf6df1442006-01-31 16:05:07 -08001527 struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
1528
1529 ha->fw_options[1] &= ~FO1_SET_EMPHASIS_SWING;
1530 ha->fw_options[1] |= FO1_DISABLE_GPIO6_7;
1531
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -08001532 if (qla2x00_set_fw_options(vha, ha->fw_options) != QLA_SUCCESS) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001533 ql_log(ql_log_warn, vha, 0x709b,
andrew.vasquez@qlogic.comf6df1442006-01-31 16:05:07 -08001534 "Unable to update fw options (beacon on).\n");
1535 return QLA_FUNCTION_FAILED;
1536 }
1537
andrew.vasquez@qlogic.comf6df1442006-01-31 16:05:07 -08001538 /* Turn off LEDs. */
1539 spin_lock_irqsave(&ha->hardware_lock, flags);
1540 if (ha->pio_address) {
Andrew Vasquez948882f2008-01-31 12:33:44 -08001541 gpio_enable = RD_REG_WORD_PIO(PIO_REG(ha, gpioe));
1542 gpio_data = RD_REG_WORD_PIO(PIO_REG(ha, gpiod));
andrew.vasquez@qlogic.comf6df1442006-01-31 16:05:07 -08001543 } else {
1544 gpio_enable = RD_REG_WORD(&reg->gpioe);
1545 gpio_data = RD_REG_WORD(&reg->gpiod);
1546 }
1547 gpio_enable |= GPIO_LED_MASK;
1548
1549 /* Set the modified gpio_enable values. */
1550 if (ha->pio_address) {
Andrew Vasquez948882f2008-01-31 12:33:44 -08001551 WRT_REG_WORD_PIO(PIO_REG(ha, gpioe), gpio_enable);
andrew.vasquez@qlogic.comf6df1442006-01-31 16:05:07 -08001552 } else {
1553 WRT_REG_WORD(&reg->gpioe, gpio_enable);
1554 RD_REG_WORD(&reg->gpioe);
1555 }
1556
1557 /* Clear out previously set LED colour. */
1558 gpio_data &= ~GPIO_LED_MASK;
1559 if (ha->pio_address) {
Andrew Vasquez948882f2008-01-31 12:33:44 -08001560 WRT_REG_WORD_PIO(PIO_REG(ha, gpiod), gpio_data);
andrew.vasquez@qlogic.comf6df1442006-01-31 16:05:07 -08001561 } else {
1562 WRT_REG_WORD(&reg->gpiod, gpio_data);
1563 RD_REG_WORD(&reg->gpiod);
1564 }
1565 spin_unlock_irqrestore(&ha->hardware_lock, flags);
1566
1567 /*
1568 * Let the per HBA timer kick off the blinking process based on
1569 * the following flags. No need to do anything else now.
1570 */
1571 ha->beacon_blink_led = 1;
1572 ha->beacon_color_state = 0;
1573
1574 return QLA_SUCCESS;
1575}
1576
1577int
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -08001578qla2x00_beacon_off(struct scsi_qla_host *vha)
andrew.vasquez@qlogic.comf6df1442006-01-31 16:05:07 -08001579{
1580 int rval = QLA_SUCCESS;
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -08001581 struct qla_hw_data *ha = vha->hw;
andrew.vasquez@qlogic.comf6df1442006-01-31 16:05:07 -08001582
1583 ha->beacon_blink_led = 0;
1584
1585 /* Set the on flag so when it gets flipped it will be off. */
1586 if (IS_QLA2322(ha))
1587 ha->beacon_color_state = QLA_LED_ALL_ON;
1588 else
1589 ha->beacon_color_state = QLA_LED_GRN_ON;
1590
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -08001591 ha->isp_ops->beacon_blink(vha); /* This turns green LED off */
andrew.vasquez@qlogic.comf6df1442006-01-31 16:05:07 -08001592
1593 ha->fw_options[1] &= ~FO1_SET_EMPHASIS_SWING;
1594 ha->fw_options[1] &= ~FO1_DISABLE_GPIO6_7;
1595
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -08001596 rval = qla2x00_set_fw_options(vha, ha->fw_options);
andrew.vasquez@qlogic.comf6df1442006-01-31 16:05:07 -08001597 if (rval != QLA_SUCCESS)
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001598 ql_log(ql_log_warn, vha, 0x709c,
andrew.vasquez@qlogic.comf6df1442006-01-31 16:05:07 -08001599 "Unable to update fw options (beacon off).\n");
1600 return rval;
1601}
1602
1603
1604static inline void
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -08001605qla24xx_flip_colors(struct qla_hw_data *ha, uint16_t *pflags)
andrew.vasquez@qlogic.comf6df1442006-01-31 16:05:07 -08001606{
1607 /* Flip all colors. */
1608 if (ha->beacon_color_state == QLA_LED_ALL_ON) {
1609 /* Turn off. */
1610 ha->beacon_color_state = 0;
1611 *pflags = 0;
1612 } else {
1613 /* Turn on. */
1614 ha->beacon_color_state = QLA_LED_ALL_ON;
1615 *pflags = GPDX_LED_YELLOW_ON | GPDX_LED_AMBER_ON;
1616 }
1617}
1618
1619void
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -08001620qla24xx_beacon_blink(struct scsi_qla_host *vha)
andrew.vasquez@qlogic.comf6df1442006-01-31 16:05:07 -08001621{
1622 uint16_t led_color = 0;
1623 uint32_t gpio_data;
1624 unsigned long flags;
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -08001625 struct qla_hw_data *ha = vha->hw;
andrew.vasquez@qlogic.comf6df1442006-01-31 16:05:07 -08001626 struct device_reg_24xx __iomem *reg = &ha->iobase->isp24;
1627
1628 /* Save the Original GPIOD. */
1629 spin_lock_irqsave(&ha->hardware_lock, flags);
1630 gpio_data = RD_REG_DWORD(&reg->gpiod);
1631
1632 /* Enable the gpio_data reg for update. */
1633 gpio_data |= GPDX_LED_UPDATE_MASK;
1634
1635 WRT_REG_DWORD(&reg->gpiod, gpio_data);
1636 gpio_data = RD_REG_DWORD(&reg->gpiod);
1637
1638 /* Set the color bits. */
1639 qla24xx_flip_colors(ha, &led_color);
1640
1641 /* Clear out any previously set LED color. */
1642 gpio_data &= ~GPDX_LED_COLOR_MASK;
1643
1644 /* Set the new input LED color to GPIOD. */
1645 gpio_data |= led_color;
1646
1647 /* Set the modified gpio_data values. */
1648 WRT_REG_DWORD(&reg->gpiod, gpio_data);
1649 gpio_data = RD_REG_DWORD(&reg->gpiod);
1650 spin_unlock_irqrestore(&ha->hardware_lock, flags);
1651}
1652
Chad Dupuis0143d8b72012-08-22 14:21:11 -04001653static uint32_t
1654qla83xx_select_led_port(struct qla_hw_data *ha)
1655{
1656 uint32_t led_select_value = 0;
1657
1658 if (!IS_QLA83XX(ha))
1659 goto out;
1660
1661 if (ha->flags.port0)
1662 led_select_value = QLA83XX_LED_PORT0;
1663 else
1664 led_select_value = QLA83XX_LED_PORT1;
1665
1666out:
1667 return led_select_value;
1668}
1669
Giridhar Malavali6246b8a2012-02-09 11:15:34 -08001670void
1671qla83xx_beacon_blink(struct scsi_qla_host *vha)
1672{
1673 uint32_t led_select_value;
1674 struct qla_hw_data *ha = vha->hw;
1675 uint16_t led_cfg[6];
1676 uint16_t orig_led_cfg[6];
Chad Dupuis0143d8b72012-08-22 14:21:11 -04001677 uint32_t led_10_value, led_43_value;
Giridhar Malavali6246b8a2012-02-09 11:15:34 -08001678
1679 if (!IS_QLA83XX(ha) && !IS_QLA81XX(ha))
1680 return;
1681
Chad Dupuis0143d8b72012-08-22 14:21:11 -04001682 if (!ha->beacon_blink_led)
1683 return;
1684
1685 if (IS_QLA2031(ha)) {
1686 led_select_value = qla83xx_select_led_port(ha);
Giridhar Malavali6246b8a2012-02-09 11:15:34 -08001687
Santosh Vernekar7d613ac2012-08-22 14:21:03 -04001688 qla83xx_wr_reg(vha, led_select_value, 0x40002000);
1689 qla83xx_wr_reg(vha, led_select_value + 4, 0x40002000);
Giridhar Malavali6246b8a2012-02-09 11:15:34 -08001690 msleep(1000);
Santosh Vernekar7d613ac2012-08-22 14:21:03 -04001691 qla83xx_wr_reg(vha, led_select_value, 0x40004000);
1692 qla83xx_wr_reg(vha, led_select_value + 4, 0x40004000);
Chad Dupuis0143d8b72012-08-22 14:21:11 -04001693 } else if (IS_QLA8031(ha)) {
1694 led_select_value = qla83xx_select_led_port(ha);
1695
1696 qla83xx_rd_reg(vha, led_select_value, &led_10_value);
1697 qla83xx_rd_reg(vha, led_select_value + 0x10, &led_43_value);
1698 qla83xx_wr_reg(vha, led_select_value, 0x01f44000);
1699 msleep(500);
1700 qla83xx_wr_reg(vha, led_select_value, 0x400001f4);
1701 msleep(1000);
1702 qla83xx_wr_reg(vha, led_select_value, led_10_value);
1703 qla83xx_wr_reg(vha, led_select_value + 0x10, led_43_value);
1704 } else if (IS_QLA81XX(ha)) {
Giridhar Malavali6246b8a2012-02-09 11:15:34 -08001705 int rval;
1706
1707 /* Save Current */
1708 rval = qla81xx_get_led_config(vha, orig_led_cfg);
1709 /* Do the blink */
1710 if (rval == QLA_SUCCESS) {
1711 if (IS_QLA81XX(ha)) {
1712 led_cfg[0] = 0x4000;
1713 led_cfg[1] = 0x2000;
1714 led_cfg[2] = 0;
1715 led_cfg[3] = 0;
1716 led_cfg[4] = 0;
1717 led_cfg[5] = 0;
1718 } else {
1719 led_cfg[0] = 0x4000;
1720 led_cfg[1] = 0x4000;
1721 led_cfg[2] = 0x4000;
1722 led_cfg[3] = 0x2000;
1723 led_cfg[4] = 0;
1724 led_cfg[5] = 0x2000;
1725 }
1726 rval = qla81xx_set_led_config(vha, led_cfg);
1727 msleep(1000);
1728 if (IS_QLA81XX(ha)) {
1729 led_cfg[0] = 0x4000;
1730 led_cfg[1] = 0x2000;
1731 led_cfg[2] = 0;
1732 } else {
1733 led_cfg[0] = 0x4000;
1734 led_cfg[1] = 0x2000;
1735 led_cfg[2] = 0x4000;
1736 led_cfg[3] = 0x4000;
1737 led_cfg[4] = 0;
1738 led_cfg[5] = 0x2000;
1739 }
1740 rval = qla81xx_set_led_config(vha, led_cfg);
1741 }
1742 /* On exit, restore original (presumes no status change) */
1743 qla81xx_set_led_config(vha, orig_led_cfg);
1744 }
1745}
1746
andrew.vasquez@qlogic.comf6df1442006-01-31 16:05:07 -08001747int
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -08001748qla24xx_beacon_on(struct scsi_qla_host *vha)
andrew.vasquez@qlogic.comf6df1442006-01-31 16:05:07 -08001749{
1750 uint32_t gpio_data;
1751 unsigned long flags;
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -08001752 struct qla_hw_data *ha = vha->hw;
andrew.vasquez@qlogic.comf6df1442006-01-31 16:05:07 -08001753 struct device_reg_24xx __iomem *reg = &ha->iobase->isp24;
1754
Giridhar Malavalia9083012010-04-12 17:59:55 -07001755 if (IS_QLA82XX(ha))
1756 return QLA_SUCCESS;
1757
Giridhar Malavali6246b8a2012-02-09 11:15:34 -08001758 if (IS_QLA8031(ha) || IS_QLA81XX(ha))
1759 goto skip_gpio; /* let blink handle it */
1760
andrew.vasquez@qlogic.comf6df1442006-01-31 16:05:07 -08001761 if (ha->beacon_blink_led == 0) {
1762 /* Enable firmware for update */
1763 ha->fw_options[1] |= ADD_FO1_DISABLE_GPIO_LED_CTRL;
1764
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -08001765 if (qla2x00_set_fw_options(vha, ha->fw_options) != QLA_SUCCESS)
andrew.vasquez@qlogic.comf6df1442006-01-31 16:05:07 -08001766 return QLA_FUNCTION_FAILED;
1767
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -08001768 if (qla2x00_get_fw_options(vha, ha->fw_options) !=
andrew.vasquez@qlogic.comf6df1442006-01-31 16:05:07 -08001769 QLA_SUCCESS) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001770 ql_log(ql_log_warn, vha, 0x7009,
andrew.vasquez@qlogic.comf6df1442006-01-31 16:05:07 -08001771 "Unable to update fw options (beacon on).\n");
1772 return QLA_FUNCTION_FAILED;
1773 }
1774
Giridhar Malavali6246b8a2012-02-09 11:15:34 -08001775 if (IS_QLA2031(ha))
1776 goto skip_gpio;
1777
andrew.vasquez@qlogic.comf6df1442006-01-31 16:05:07 -08001778 spin_lock_irqsave(&ha->hardware_lock, flags);
1779 gpio_data = RD_REG_DWORD(&reg->gpiod);
1780
1781 /* Enable the gpio_data reg for update. */
1782 gpio_data |= GPDX_LED_UPDATE_MASK;
1783 WRT_REG_DWORD(&reg->gpiod, gpio_data);
1784 RD_REG_DWORD(&reg->gpiod);
1785
1786 spin_unlock_irqrestore(&ha->hardware_lock, flags);
1787 }
1788
1789 /* So all colors blink together. */
1790 ha->beacon_color_state = 0;
1791
Giridhar Malavali6246b8a2012-02-09 11:15:34 -08001792skip_gpio:
andrew.vasquez@qlogic.comf6df1442006-01-31 16:05:07 -08001793 /* Let the per HBA timer kick off the blinking process. */
1794 ha->beacon_blink_led = 1;
1795
1796 return QLA_SUCCESS;
1797}
1798
1799int
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -08001800qla24xx_beacon_off(struct scsi_qla_host *vha)
andrew.vasquez@qlogic.comf6df1442006-01-31 16:05:07 -08001801{
1802 uint32_t gpio_data;
1803 unsigned long flags;
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -08001804 struct qla_hw_data *ha = vha->hw;
andrew.vasquez@qlogic.comf6df1442006-01-31 16:05:07 -08001805 struct device_reg_24xx __iomem *reg = &ha->iobase->isp24;
1806
Giridhar Malavalia9083012010-04-12 17:59:55 -07001807 if (IS_QLA82XX(ha))
1808 return QLA_SUCCESS;
1809
andrew.vasquez@qlogic.comf6df1442006-01-31 16:05:07 -08001810 ha->beacon_blink_led = 0;
Giridhar Malavali6246b8a2012-02-09 11:15:34 -08001811
1812 if (IS_QLA2031(ha))
1813 goto set_fw_options;
1814
1815 if (IS_QLA8031(ha) || IS_QLA81XX(ha))
1816 return QLA_SUCCESS;
1817
andrew.vasquez@qlogic.comf6df1442006-01-31 16:05:07 -08001818 ha->beacon_color_state = QLA_LED_ALL_ON;
1819
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -08001820 ha->isp_ops->beacon_blink(vha); /* Will flip to all off. */
andrew.vasquez@qlogic.comf6df1442006-01-31 16:05:07 -08001821
1822 /* Give control back to firmware. */
1823 spin_lock_irqsave(&ha->hardware_lock, flags);
1824 gpio_data = RD_REG_DWORD(&reg->gpiod);
1825
1826 /* Disable the gpio_data reg for update. */
1827 gpio_data &= ~GPDX_LED_UPDATE_MASK;
1828 WRT_REG_DWORD(&reg->gpiod, gpio_data);
1829 RD_REG_DWORD(&reg->gpiod);
1830 spin_unlock_irqrestore(&ha->hardware_lock, flags);
1831
Giridhar Malavali6246b8a2012-02-09 11:15:34 -08001832set_fw_options:
andrew.vasquez@qlogic.comf6df1442006-01-31 16:05:07 -08001833 ha->fw_options[1] &= ~ADD_FO1_DISABLE_GPIO_LED_CTRL;
1834
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -08001835 if (qla2x00_set_fw_options(vha, ha->fw_options) != QLA_SUCCESS) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001836 ql_log(ql_log_warn, vha, 0x704d,
1837 "Unable to update fw options (beacon on).\n");
andrew.vasquez@qlogic.comf6df1442006-01-31 16:05:07 -08001838 return QLA_FUNCTION_FAILED;
1839 }
1840
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -08001841 if (qla2x00_get_fw_options(vha, ha->fw_options) != QLA_SUCCESS) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001842 ql_log(ql_log_warn, vha, 0x704e,
1843 "Unable to update fw options (beacon on).\n");
andrew.vasquez@qlogic.comf6df1442006-01-31 16:05:07 -08001844 return QLA_FUNCTION_FAILED;
1845 }
1846
1847 return QLA_SUCCESS;
1848}
andrew.vasquez@qlogic.com854165f2006-01-31 16:05:17 -08001849
1850
1851/*
1852 * Flash support routines
1853 */
1854
1855/**
1856 * qla2x00_flash_enable() - Setup flash for reading and writing.
1857 * @ha: HA context
1858 */
1859static void
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -08001860qla2x00_flash_enable(struct qla_hw_data *ha)
andrew.vasquez@qlogic.com854165f2006-01-31 16:05:17 -08001861{
1862 uint16_t data;
1863 struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
1864
1865 data = RD_REG_WORD(&reg->ctrl_status);
1866 data |= CSR_FLASH_ENABLE;
1867 WRT_REG_WORD(&reg->ctrl_status, data);
1868 RD_REG_WORD(&reg->ctrl_status); /* PCI Posting. */
1869}
1870
1871/**
1872 * qla2x00_flash_disable() - Disable flash and allow RISC to run.
1873 * @ha: HA context
1874 */
1875static void
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -08001876qla2x00_flash_disable(struct qla_hw_data *ha)
andrew.vasquez@qlogic.com854165f2006-01-31 16:05:17 -08001877{
1878 uint16_t data;
1879 struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
1880
1881 data = RD_REG_WORD(&reg->ctrl_status);
1882 data &= ~(CSR_FLASH_ENABLE);
1883 WRT_REG_WORD(&reg->ctrl_status, data);
1884 RD_REG_WORD(&reg->ctrl_status); /* PCI Posting. */
1885}
1886
1887/**
1888 * qla2x00_read_flash_byte() - Reads a byte from flash
1889 * @ha: HA context
1890 * @addr: Address in flash to read
1891 *
1892 * A word is read from the chip, but, only the lower byte is valid.
1893 *
1894 * Returns the byte read from flash @addr.
1895 */
1896static uint8_t
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -08001897qla2x00_read_flash_byte(struct qla_hw_data *ha, uint32_t addr)
andrew.vasquez@qlogic.com854165f2006-01-31 16:05:17 -08001898{
1899 uint16_t data;
1900 uint16_t bank_select;
1901 struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
1902
1903 bank_select = RD_REG_WORD(&reg->ctrl_status);
1904
1905 if (IS_QLA2322(ha) || IS_QLA6322(ha)) {
1906 /* Specify 64K address range: */
1907 /* clear out Module Select and Flash Address bits [19:16]. */
1908 bank_select &= ~0xf8;
1909 bank_select |= addr >> 12 & 0xf0;
1910 bank_select |= CSR_FLASH_64K_BANK;
1911 WRT_REG_WORD(&reg->ctrl_status, bank_select);
1912 RD_REG_WORD(&reg->ctrl_status); /* PCI Posting. */
1913
1914 WRT_REG_WORD(&reg->flash_address, (uint16_t)addr);
1915 data = RD_REG_WORD(&reg->flash_data);
1916
1917 return (uint8_t)data;
1918 }
1919
1920 /* Setup bit 16 of flash address. */
1921 if ((addr & BIT_16) && ((bank_select & CSR_FLASH_64K_BANK) == 0)) {
1922 bank_select |= CSR_FLASH_64K_BANK;
1923 WRT_REG_WORD(&reg->ctrl_status, bank_select);
1924 RD_REG_WORD(&reg->ctrl_status); /* PCI Posting. */
1925 } else if (((addr & BIT_16) == 0) &&
1926 (bank_select & CSR_FLASH_64K_BANK)) {
1927 bank_select &= ~(CSR_FLASH_64K_BANK);
1928 WRT_REG_WORD(&reg->ctrl_status, bank_select);
1929 RD_REG_WORD(&reg->ctrl_status); /* PCI Posting. */
1930 }
1931
1932 /* Always perform IO mapped accesses to the FLASH registers. */
1933 if (ha->pio_address) {
1934 uint16_t data2;
1935
Andrew Vasquez948882f2008-01-31 12:33:44 -08001936 WRT_REG_WORD_PIO(PIO_REG(ha, flash_address), (uint16_t)addr);
andrew.vasquez@qlogic.com854165f2006-01-31 16:05:17 -08001937 do {
Andrew Vasquez948882f2008-01-31 12:33:44 -08001938 data = RD_REG_WORD_PIO(PIO_REG(ha, flash_data));
andrew.vasquez@qlogic.com854165f2006-01-31 16:05:17 -08001939 barrier();
1940 cpu_relax();
Andrew Vasquez948882f2008-01-31 12:33:44 -08001941 data2 = RD_REG_WORD_PIO(PIO_REG(ha, flash_data));
andrew.vasquez@qlogic.com854165f2006-01-31 16:05:17 -08001942 } while (data != data2);
1943 } else {
1944 WRT_REG_WORD(&reg->flash_address, (uint16_t)addr);
1945 data = qla2x00_debounce_register(&reg->flash_data);
1946 }
1947
1948 return (uint8_t)data;
1949}
1950
1951/**
1952 * qla2x00_write_flash_byte() - Write a byte to flash
1953 * @ha: HA context
1954 * @addr: Address in flash to write
1955 * @data: Data to write
1956 */
1957static void
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -08001958qla2x00_write_flash_byte(struct qla_hw_data *ha, uint32_t addr, uint8_t data)
andrew.vasquez@qlogic.com854165f2006-01-31 16:05:17 -08001959{
1960 uint16_t bank_select;
1961 struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
1962
1963 bank_select = RD_REG_WORD(&reg->ctrl_status);
1964 if (IS_QLA2322(ha) || IS_QLA6322(ha)) {
1965 /* Specify 64K address range: */
1966 /* clear out Module Select and Flash Address bits [19:16]. */
1967 bank_select &= ~0xf8;
1968 bank_select |= addr >> 12 & 0xf0;
1969 bank_select |= CSR_FLASH_64K_BANK;
1970 WRT_REG_WORD(&reg->ctrl_status, bank_select);
1971 RD_REG_WORD(&reg->ctrl_status); /* PCI Posting. */
1972
1973 WRT_REG_WORD(&reg->flash_address, (uint16_t)addr);
1974 RD_REG_WORD(&reg->ctrl_status); /* PCI Posting. */
1975 WRT_REG_WORD(&reg->flash_data, (uint16_t)data);
1976 RD_REG_WORD(&reg->ctrl_status); /* PCI Posting. */
1977
1978 return;
1979 }
1980
1981 /* Setup bit 16 of flash address. */
1982 if ((addr & BIT_16) && ((bank_select & CSR_FLASH_64K_BANK) == 0)) {
1983 bank_select |= CSR_FLASH_64K_BANK;
1984 WRT_REG_WORD(&reg->ctrl_status, bank_select);
1985 RD_REG_WORD(&reg->ctrl_status); /* PCI Posting. */
1986 } else if (((addr & BIT_16) == 0) &&
1987 (bank_select & CSR_FLASH_64K_BANK)) {
1988 bank_select &= ~(CSR_FLASH_64K_BANK);
1989 WRT_REG_WORD(&reg->ctrl_status, bank_select);
1990 RD_REG_WORD(&reg->ctrl_status); /* PCI Posting. */
1991 }
1992
1993 /* Always perform IO mapped accesses to the FLASH registers. */
1994 if (ha->pio_address) {
Andrew Vasquez948882f2008-01-31 12:33:44 -08001995 WRT_REG_WORD_PIO(PIO_REG(ha, flash_address), (uint16_t)addr);
1996 WRT_REG_WORD_PIO(PIO_REG(ha, flash_data), (uint16_t)data);
andrew.vasquez@qlogic.com854165f2006-01-31 16:05:17 -08001997 } else {
1998 WRT_REG_WORD(&reg->flash_address, (uint16_t)addr);
1999 RD_REG_WORD(&reg->ctrl_status); /* PCI Posting. */
2000 WRT_REG_WORD(&reg->flash_data, (uint16_t)data);
2001 RD_REG_WORD(&reg->ctrl_status); /* PCI Posting. */
2002 }
2003}
2004
2005/**
2006 * qla2x00_poll_flash() - Polls flash for completion.
2007 * @ha: HA context
2008 * @addr: Address in flash to poll
2009 * @poll_data: Data to be polled
2010 * @man_id: Flash manufacturer ID
2011 * @flash_id: Flash ID
2012 *
2013 * This function polls the device until bit 7 of what is read matches data
2014 * bit 7 or until data bit 5 becomes a 1. If that hapens, the flash ROM timed
2015 * out (a fatal error). The flash book recommeds reading bit 7 again after
2016 * reading bit 5 as a 1.
2017 *
2018 * Returns 0 on success, else non-zero.
2019 */
2020static int
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -08002021qla2x00_poll_flash(struct qla_hw_data *ha, uint32_t addr, uint8_t poll_data,
andrew.vasquez@qlogic.com854165f2006-01-31 16:05:17 -08002022 uint8_t man_id, uint8_t flash_id)
2023{
2024 int status;
2025 uint8_t flash_data;
2026 uint32_t cnt;
2027
2028 status = 1;
2029
2030 /* Wait for 30 seconds for command to finish. */
2031 poll_data &= BIT_7;
2032 for (cnt = 3000000; cnt; cnt--) {
2033 flash_data = qla2x00_read_flash_byte(ha, addr);
2034 if ((flash_data & BIT_7) == poll_data) {
2035 status = 0;
2036 break;
2037 }
2038
2039 if (man_id != 0x40 && man_id != 0xda) {
2040 if ((flash_data & BIT_5) && cnt > 2)
2041 cnt = 2;
2042 }
2043 udelay(10);
2044 barrier();
Andrew Vasquez40a2e342007-03-12 10:41:28 -07002045 cond_resched();
andrew.vasquez@qlogic.com854165f2006-01-31 16:05:17 -08002046 }
2047 return status;
2048}
2049
andrew.vasquez@qlogic.com854165f2006-01-31 16:05:17 -08002050/**
2051 * qla2x00_program_flash_address() - Programs a flash address
2052 * @ha: HA context
2053 * @addr: Address in flash to program
2054 * @data: Data to be written in flash
2055 * @man_id: Flash manufacturer ID
2056 * @flash_id: Flash ID
2057 *
2058 * Returns 0 on success, else non-zero.
2059 */
2060static int
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -08002061qla2x00_program_flash_address(struct qla_hw_data *ha, uint32_t addr,
2062 uint8_t data, uint8_t man_id, uint8_t flash_id)
andrew.vasquez@qlogic.com854165f2006-01-31 16:05:17 -08002063{
2064 /* Write Program Command Sequence. */
2065 if (IS_OEM_001(ha)) {
2066 qla2x00_write_flash_byte(ha, 0xaaa, 0xaa);
2067 qla2x00_write_flash_byte(ha, 0x555, 0x55);
2068 qla2x00_write_flash_byte(ha, 0xaaa, 0xa0);
2069 qla2x00_write_flash_byte(ha, addr, data);
2070 } else {
2071 if (man_id == 0xda && flash_id == 0xc1) {
2072 qla2x00_write_flash_byte(ha, addr, data);
2073 if (addr & 0x7e)
2074 return 0;
2075 } else {
2076 qla2x00_write_flash_byte(ha, 0x5555, 0xaa);
2077 qla2x00_write_flash_byte(ha, 0x2aaa, 0x55);
2078 qla2x00_write_flash_byte(ha, 0x5555, 0xa0);
2079 qla2x00_write_flash_byte(ha, addr, data);
2080 }
2081 }
2082
2083 udelay(150);
2084
2085 /* Wait for write to complete. */
2086 return qla2x00_poll_flash(ha, addr, data, man_id, flash_id);
2087}
2088
2089/**
2090 * qla2x00_erase_flash() - Erase the flash.
2091 * @ha: HA context
2092 * @man_id: Flash manufacturer ID
2093 * @flash_id: Flash ID
2094 *
2095 * Returns 0 on success, else non-zero.
2096 */
2097static int
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -08002098qla2x00_erase_flash(struct qla_hw_data *ha, uint8_t man_id, uint8_t flash_id)
andrew.vasquez@qlogic.com854165f2006-01-31 16:05:17 -08002099{
2100 /* Individual Sector Erase Command Sequence */
2101 if (IS_OEM_001(ha)) {
2102 qla2x00_write_flash_byte(ha, 0xaaa, 0xaa);
2103 qla2x00_write_flash_byte(ha, 0x555, 0x55);
2104 qla2x00_write_flash_byte(ha, 0xaaa, 0x80);
2105 qla2x00_write_flash_byte(ha, 0xaaa, 0xaa);
2106 qla2x00_write_flash_byte(ha, 0x555, 0x55);
2107 qla2x00_write_flash_byte(ha, 0xaaa, 0x10);
2108 } else {
2109 qla2x00_write_flash_byte(ha, 0x5555, 0xaa);
2110 qla2x00_write_flash_byte(ha, 0x2aaa, 0x55);
2111 qla2x00_write_flash_byte(ha, 0x5555, 0x80);
2112 qla2x00_write_flash_byte(ha, 0x5555, 0xaa);
2113 qla2x00_write_flash_byte(ha, 0x2aaa, 0x55);
2114 qla2x00_write_flash_byte(ha, 0x5555, 0x10);
2115 }
2116
2117 udelay(150);
2118
2119 /* Wait for erase to complete. */
2120 return qla2x00_poll_flash(ha, 0x00, 0x80, man_id, flash_id);
2121}
2122
2123/**
2124 * qla2x00_erase_flash_sector() - Erase a flash sector.
2125 * @ha: HA context
2126 * @addr: Flash sector to erase
2127 * @sec_mask: Sector address mask
2128 * @man_id: Flash manufacturer ID
2129 * @flash_id: Flash ID
2130 *
2131 * Returns 0 on success, else non-zero.
2132 */
2133static int
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -08002134qla2x00_erase_flash_sector(struct qla_hw_data *ha, uint32_t addr,
andrew.vasquez@qlogic.com854165f2006-01-31 16:05:17 -08002135 uint32_t sec_mask, uint8_t man_id, uint8_t flash_id)
2136{
2137 /* Individual Sector Erase Command Sequence */
2138 qla2x00_write_flash_byte(ha, 0x5555, 0xaa);
2139 qla2x00_write_flash_byte(ha, 0x2aaa, 0x55);
2140 qla2x00_write_flash_byte(ha, 0x5555, 0x80);
2141 qla2x00_write_flash_byte(ha, 0x5555, 0xaa);
2142 qla2x00_write_flash_byte(ha, 0x2aaa, 0x55);
2143 if (man_id == 0x1f && flash_id == 0x13)
2144 qla2x00_write_flash_byte(ha, addr & sec_mask, 0x10);
2145 else
2146 qla2x00_write_flash_byte(ha, addr & sec_mask, 0x30);
2147
2148 udelay(150);
2149
2150 /* Wait for erase to complete. */
2151 return qla2x00_poll_flash(ha, addr, 0x80, man_id, flash_id);
2152}
2153
2154/**
2155 * qla2x00_get_flash_manufacturer() - Read manufacturer ID from flash chip.
2156 * @man_id: Flash manufacturer ID
2157 * @flash_id: Flash ID
2158 */
2159static void
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -08002160qla2x00_get_flash_manufacturer(struct qla_hw_data *ha, uint8_t *man_id,
andrew.vasquez@qlogic.com854165f2006-01-31 16:05:17 -08002161 uint8_t *flash_id)
2162{
2163 qla2x00_write_flash_byte(ha, 0x5555, 0xaa);
2164 qla2x00_write_flash_byte(ha, 0x2aaa, 0x55);
2165 qla2x00_write_flash_byte(ha, 0x5555, 0x90);
2166 *man_id = qla2x00_read_flash_byte(ha, 0x0000);
2167 *flash_id = qla2x00_read_flash_byte(ha, 0x0001);
2168 qla2x00_write_flash_byte(ha, 0x5555, 0xaa);
2169 qla2x00_write_flash_byte(ha, 0x2aaa, 0x55);
2170 qla2x00_write_flash_byte(ha, 0x5555, 0xf0);
2171}
2172
Andrew Vasquez30c47662007-01-29 10:22:21 -08002173static void
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -08002174qla2x00_read_flash_data(struct qla_hw_data *ha, uint8_t *tmp_buf,
2175 uint32_t saddr, uint32_t length)
Andrew Vasquez30c47662007-01-29 10:22:21 -08002176{
2177 struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
2178 uint32_t midpoint, ilength;
2179 uint8_t data;
2180
2181 midpoint = length / 2;
2182
2183 WRT_REG_WORD(&reg->nvram, 0);
2184 RD_REG_WORD(&reg->nvram);
2185 for (ilength = 0; ilength < length; saddr++, ilength++, tmp_buf++) {
2186 if (ilength == midpoint) {
2187 WRT_REG_WORD(&reg->nvram, NVR_SELECT);
2188 RD_REG_WORD(&reg->nvram);
2189 }
2190 data = qla2x00_read_flash_byte(ha, saddr);
2191 if (saddr % 100)
2192 udelay(10);
2193 *tmp_buf = data;
Andrew Vasquez40a2e342007-03-12 10:41:28 -07002194 cond_resched();
Andrew Vasquez30c47662007-01-29 10:22:21 -08002195 }
2196}
andrew.vasquez@qlogic.com854165f2006-01-31 16:05:17 -08002197
2198static inline void
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -08002199qla2x00_suspend_hba(struct scsi_qla_host *vha)
andrew.vasquez@qlogic.com854165f2006-01-31 16:05:17 -08002200{
2201 int cnt;
2202 unsigned long flags;
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -08002203 struct qla_hw_data *ha = vha->hw;
andrew.vasquez@qlogic.com854165f2006-01-31 16:05:17 -08002204 struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
2205
2206 /* Suspend HBA. */
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -08002207 scsi_block_requests(vha->host);
Andrew Vasquezfd34f552007-07-19 15:06:00 -07002208 ha->isp_ops->disable_intrs(ha);
andrew.vasquez@qlogic.com854165f2006-01-31 16:05:17 -08002209 set_bit(MBX_UPDATE_FLASH_ACTIVE, &ha->mbx_cmd_flags);
2210
2211 /* Pause RISC. */
2212 spin_lock_irqsave(&ha->hardware_lock, flags);
2213 WRT_REG_WORD(&reg->hccr, HCCR_PAUSE_RISC);
2214 RD_REG_WORD(&reg->hccr);
2215 if (IS_QLA2100(ha) || IS_QLA2200(ha) || IS_QLA2300(ha)) {
2216 for (cnt = 0; cnt < 30000; cnt++) {
2217 if ((RD_REG_WORD(&reg->hccr) & HCCR_RISC_PAUSE) != 0)
2218 break;
2219 udelay(100);
2220 }
2221 } else {
2222 udelay(10);
2223 }
2224 spin_unlock_irqrestore(&ha->hardware_lock, flags);
2225}
2226
2227static inline void
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -08002228qla2x00_resume_hba(struct scsi_qla_host *vha)
andrew.vasquez@qlogic.com854165f2006-01-31 16:05:17 -08002229{
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -08002230 struct qla_hw_data *ha = vha->hw;
2231
andrew.vasquez@qlogic.com854165f2006-01-31 16:05:17 -08002232 /* Resume HBA. */
2233 clear_bit(MBX_UPDATE_FLASH_ACTIVE, &ha->mbx_cmd_flags);
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -08002234 set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
2235 qla2xxx_wake_dpc(vha);
Lalit Chandivade2533cf62009-03-24 09:08:07 -07002236 qla2x00_wait_for_chip_reset(vha);
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -08002237 scsi_unblock_requests(vha->host);
andrew.vasquez@qlogic.com854165f2006-01-31 16:05:17 -08002238}
2239
2240uint8_t *
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -08002241qla2x00_read_optrom_data(struct scsi_qla_host *vha, uint8_t *buf,
andrew.vasquez@qlogic.com854165f2006-01-31 16:05:17 -08002242 uint32_t offset, uint32_t length)
2243{
andrew.vasquez@qlogic.com854165f2006-01-31 16:05:17 -08002244 uint32_t addr, midpoint;
2245 uint8_t *data;
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -08002246 struct qla_hw_data *ha = vha->hw;
andrew.vasquez@qlogic.com854165f2006-01-31 16:05:17 -08002247 struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
2248
2249 /* Suspend HBA. */
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -08002250 qla2x00_suspend_hba(vha);
andrew.vasquez@qlogic.com854165f2006-01-31 16:05:17 -08002251
2252 /* Go with read. */
andrew.vasquez@qlogic.com854165f2006-01-31 16:05:17 -08002253 midpoint = ha->optrom_size / 2;
2254
2255 qla2x00_flash_enable(ha);
2256 WRT_REG_WORD(&reg->nvram, 0);
2257 RD_REG_WORD(&reg->nvram); /* PCI Posting. */
2258 for (addr = offset, data = buf; addr < length; addr++, data++) {
2259 if (addr == midpoint) {
2260 WRT_REG_WORD(&reg->nvram, NVR_SELECT);
2261 RD_REG_WORD(&reg->nvram); /* PCI Posting. */
2262 }
2263
2264 *data = qla2x00_read_flash_byte(ha, addr);
2265 }
2266 qla2x00_flash_disable(ha);
andrew.vasquez@qlogic.com854165f2006-01-31 16:05:17 -08002267
2268 /* Resume HBA. */
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -08002269 qla2x00_resume_hba(vha);
andrew.vasquez@qlogic.com854165f2006-01-31 16:05:17 -08002270
2271 return buf;
2272}
2273
2274int
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -08002275qla2x00_write_optrom_data(struct scsi_qla_host *vha, uint8_t *buf,
andrew.vasquez@qlogic.com854165f2006-01-31 16:05:17 -08002276 uint32_t offset, uint32_t length)
2277{
2278
2279 int rval;
andrew.vasquez@qlogic.com854165f2006-01-31 16:05:17 -08002280 uint8_t man_id, flash_id, sec_number, data;
2281 uint16_t wd;
2282 uint32_t addr, liter, sec_mask, rest_addr;
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -08002283 struct qla_hw_data *ha = vha->hw;
andrew.vasquez@qlogic.com854165f2006-01-31 16:05:17 -08002284 struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
2285
2286 /* Suspend HBA. */
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -08002287 qla2x00_suspend_hba(vha);
andrew.vasquez@qlogic.com854165f2006-01-31 16:05:17 -08002288
2289 rval = QLA_SUCCESS;
2290 sec_number = 0;
2291
2292 /* Reset ISP chip. */
andrew.vasquez@qlogic.com854165f2006-01-31 16:05:17 -08002293 WRT_REG_WORD(&reg->ctrl_status, CSR_ISP_SOFT_RESET);
2294 pci_read_config_word(ha->pdev, PCI_COMMAND, &wd);
2295
2296 /* Go with write. */
2297 qla2x00_flash_enable(ha);
2298 do { /* Loop once to provide quick error exit */
2299 /* Structure of flash memory based on manufacturer */
2300 if (IS_OEM_001(ha)) {
2301 /* OEM variant with special flash part. */
2302 man_id = flash_id = 0;
2303 rest_addr = 0xffff;
2304 sec_mask = 0x10000;
2305 goto update_flash;
2306 }
2307 qla2x00_get_flash_manufacturer(ha, &man_id, &flash_id);
2308 switch (man_id) {
2309 case 0x20: /* ST flash. */
2310 if (flash_id == 0xd2 || flash_id == 0xe3) {
2311 /*
2312 * ST m29w008at part - 64kb sector size with
2313 * 32kb,8kb,8kb,16kb sectors at memory address
2314 * 0xf0000.
2315 */
2316 rest_addr = 0xffff;
2317 sec_mask = 0x10000;
2318 break;
2319 }
2320 /*
2321 * ST m29w010b part - 16kb sector size
2322 * Default to 16kb sectors
2323 */
2324 rest_addr = 0x3fff;
2325 sec_mask = 0x1c000;
2326 break;
2327 case 0x40: /* Mostel flash. */
2328 /* Mostel v29c51001 part - 512 byte sector size. */
2329 rest_addr = 0x1ff;
2330 sec_mask = 0x1fe00;
2331 break;
2332 case 0xbf: /* SST flash. */
2333 /* SST39sf10 part - 4kb sector size. */
2334 rest_addr = 0xfff;
2335 sec_mask = 0x1f000;
2336 break;
2337 case 0xda: /* Winbond flash. */
2338 /* Winbond W29EE011 part - 256 byte sector size. */
2339 rest_addr = 0x7f;
2340 sec_mask = 0x1ff80;
2341 break;
2342 case 0xc2: /* Macronix flash. */
2343 /* 64k sector size. */
2344 if (flash_id == 0x38 || flash_id == 0x4f) {
2345 rest_addr = 0xffff;
2346 sec_mask = 0x10000;
2347 break;
2348 }
2349 /* Fall through... */
2350
2351 case 0x1f: /* Atmel flash. */
2352 /* 512k sector size. */
2353 if (flash_id == 0x13) {
2354 rest_addr = 0x7fffffff;
2355 sec_mask = 0x80000000;
2356 break;
2357 }
2358 /* Fall through... */
2359
2360 case 0x01: /* AMD flash. */
2361 if (flash_id == 0x38 || flash_id == 0x40 ||
2362 flash_id == 0x4f) {
2363 /* Am29LV081 part - 64kb sector size. */
2364 /* Am29LV002BT part - 64kb sector size. */
2365 rest_addr = 0xffff;
2366 sec_mask = 0x10000;
2367 break;
2368 } else if (flash_id == 0x3e) {
2369 /*
2370 * Am29LV008b part - 64kb sector size with
2371 * 32kb,8kb,8kb,16kb sector at memory address
2372 * h0xf0000.
2373 */
2374 rest_addr = 0xffff;
2375 sec_mask = 0x10000;
2376 break;
2377 } else if (flash_id == 0x20 || flash_id == 0x6e) {
2378 /*
2379 * Am29LV010 part or AM29f010 - 16kb sector
2380 * size.
2381 */
2382 rest_addr = 0x3fff;
2383 sec_mask = 0x1c000;
2384 break;
2385 } else if (flash_id == 0x6d) {
2386 /* Am29LV001 part - 8kb sector size. */
2387 rest_addr = 0x1fff;
2388 sec_mask = 0x1e000;
2389 break;
2390 }
2391 default:
2392 /* Default to 16 kb sector size. */
2393 rest_addr = 0x3fff;
2394 sec_mask = 0x1c000;
2395 break;
2396 }
2397
2398update_flash:
2399 if (IS_QLA2322(ha) || IS_QLA6322(ha)) {
2400 if (qla2x00_erase_flash(ha, man_id, flash_id)) {
2401 rval = QLA_FUNCTION_FAILED;
2402 break;
2403 }
2404 }
2405
2406 for (addr = offset, liter = 0; liter < length; liter++,
2407 addr++) {
2408 data = buf[liter];
2409 /* Are we at the beginning of a sector? */
2410 if ((addr & rest_addr) == 0) {
2411 if (IS_QLA2322(ha) || IS_QLA6322(ha)) {
2412 if (addr >= 0x10000UL) {
2413 if (((addr >> 12) & 0xf0) &&
2414 ((man_id == 0x01 &&
2415 flash_id == 0x3e) ||
2416 (man_id == 0x20 &&
2417 flash_id == 0xd2))) {
2418 sec_number++;
2419 if (sec_number == 1) {
2420 rest_addr =
2421 0x7fff;
2422 sec_mask =
2423 0x18000;
2424 } else if (
2425 sec_number == 2 ||
2426 sec_number == 3) {
2427 rest_addr =
2428 0x1fff;
2429 sec_mask =
2430 0x1e000;
2431 } else if (
2432 sec_number == 4) {
2433 rest_addr =
2434 0x3fff;
2435 sec_mask =
2436 0x1c000;
2437 }
2438 }
2439 }
2440 } else if (addr == ha->optrom_size / 2) {
2441 WRT_REG_WORD(&reg->nvram, NVR_SELECT);
2442 RD_REG_WORD(&reg->nvram);
2443 }
2444
2445 if (flash_id == 0xda && man_id == 0xc1) {
2446 qla2x00_write_flash_byte(ha, 0x5555,
2447 0xaa);
2448 qla2x00_write_flash_byte(ha, 0x2aaa,
2449 0x55);
2450 qla2x00_write_flash_byte(ha, 0x5555,
2451 0xa0);
2452 } else if (!IS_QLA2322(ha) && !IS_QLA6322(ha)) {
2453 /* Then erase it */
2454 if (qla2x00_erase_flash_sector(ha,
2455 addr, sec_mask, man_id,
2456 flash_id)) {
2457 rval = QLA_FUNCTION_FAILED;
2458 break;
2459 }
2460 if (man_id == 0x01 && flash_id == 0x6d)
2461 sec_number++;
2462 }
2463 }
2464
2465 if (man_id == 0x01 && flash_id == 0x6d) {
2466 if (sec_number == 1 &&
2467 addr == (rest_addr - 1)) {
2468 rest_addr = 0x0fff;
2469 sec_mask = 0x1f000;
2470 } else if (sec_number == 3 && (addr & 0x7ffe)) {
2471 rest_addr = 0x3fff;
2472 sec_mask = 0x1c000;
2473 }
2474 }
2475
2476 if (qla2x00_program_flash_address(ha, addr, data,
2477 man_id, flash_id)) {
2478 rval = QLA_FUNCTION_FAILED;
2479 break;
2480 }
Andrew Vasquez40a2e342007-03-12 10:41:28 -07002481 cond_resched();
andrew.vasquez@qlogic.com854165f2006-01-31 16:05:17 -08002482 }
2483 } while (0);
2484 qla2x00_flash_disable(ha);
andrew.vasquez@qlogic.com854165f2006-01-31 16:05:17 -08002485
2486 /* Resume HBA. */
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -08002487 qla2x00_resume_hba(vha);
andrew.vasquez@qlogic.com854165f2006-01-31 16:05:17 -08002488
2489 return rval;
2490}
2491
2492uint8_t *
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -08002493qla24xx_read_optrom_data(struct scsi_qla_host *vha, uint8_t *buf,
andrew.vasquez@qlogic.com854165f2006-01-31 16:05:17 -08002494 uint32_t offset, uint32_t length)
2495{
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -08002496 struct qla_hw_data *ha = vha->hw;
2497
andrew.vasquez@qlogic.com854165f2006-01-31 16:05:17 -08002498 /* Suspend HBA. */
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -08002499 scsi_block_requests(vha->host);
andrew.vasquez@qlogic.com854165f2006-01-31 16:05:17 -08002500 set_bit(MBX_UPDATE_FLASH_ACTIVE, &ha->mbx_cmd_flags);
2501
2502 /* Go with read. */
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -08002503 qla24xx_read_flash_data(vha, (uint32_t *)buf, offset >> 2, length >> 2);
andrew.vasquez@qlogic.com854165f2006-01-31 16:05:17 -08002504
2505 /* Resume HBA. */
2506 clear_bit(MBX_UPDATE_FLASH_ACTIVE, &ha->mbx_cmd_flags);
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -08002507 scsi_unblock_requests(vha->host);
andrew.vasquez@qlogic.com854165f2006-01-31 16:05:17 -08002508
2509 return buf;
2510}
2511
2512int
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -08002513qla24xx_write_optrom_data(struct scsi_qla_host *vha, uint8_t *buf,
andrew.vasquez@qlogic.com854165f2006-01-31 16:05:17 -08002514 uint32_t offset, uint32_t length)
2515{
2516 int rval;
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -08002517 struct qla_hw_data *ha = vha->hw;
andrew.vasquez@qlogic.com854165f2006-01-31 16:05:17 -08002518
2519 /* Suspend HBA. */
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -08002520 scsi_block_requests(vha->host);
andrew.vasquez@qlogic.com854165f2006-01-31 16:05:17 -08002521 set_bit(MBX_UPDATE_FLASH_ACTIVE, &ha->mbx_cmd_flags);
2522
2523 /* Go with write. */
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -08002524 rval = qla24xx_write_flash_data(vha, (uint32_t *)buf, offset >> 2,
andrew.vasquez@qlogic.com854165f2006-01-31 16:05:17 -08002525 length >> 2);
2526
andrew.vasquez@qlogic.com854165f2006-01-31 16:05:17 -08002527 clear_bit(MBX_UPDATE_FLASH_ACTIVE, &ha->mbx_cmd_flags);
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -08002528 scsi_unblock_requests(vha->host);
andrew.vasquez@qlogic.com854165f2006-01-31 16:05:17 -08002529
2530 return rval;
2531}
Andrew Vasquez30c47662007-01-29 10:22:21 -08002532
Andrew Vasquez338c9162007-09-20 14:07:33 -07002533uint8_t *
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -08002534qla25xx_read_optrom_data(struct scsi_qla_host *vha, uint8_t *buf,
Andrew Vasquez338c9162007-09-20 14:07:33 -07002535 uint32_t offset, uint32_t length)
2536{
2537 int rval;
2538 dma_addr_t optrom_dma;
2539 void *optrom;
2540 uint8_t *pbuf;
2541 uint32_t faddr, left, burst;
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -08002542 struct qla_hw_data *ha = vha->hw;
Andrew Vasquez338c9162007-09-20 14:07:33 -07002543
Andrew Vasquez368bbe02010-01-12 12:59:49 -08002544 if (IS_QLA25XX(ha) || IS_QLA81XX(ha))
2545 goto try_fast;
Joe Carnucciob7cc1762007-09-20 14:07:35 -07002546 if (offset & 0xfff)
Andrew Vasquez338c9162007-09-20 14:07:33 -07002547 goto slow_read;
2548 if (length < OPTROM_BURST_SIZE)
2549 goto slow_read;
2550
Andrew Vasquez368bbe02010-01-12 12:59:49 -08002551try_fast:
Andrew Vasquez338c9162007-09-20 14:07:33 -07002552 optrom = dma_alloc_coherent(&ha->pdev->dev, OPTROM_BURST_SIZE,
2553 &optrom_dma, GFP_KERNEL);
2554 if (!optrom) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07002555 ql_log(ql_log_warn, vha, 0x00cc,
2556 "Unable to allocate memory for optrom burst read (%x KB).\n",
2557 OPTROM_BURST_SIZE / 1024);
Andrew Vasquez338c9162007-09-20 14:07:33 -07002558 goto slow_read;
2559 }
2560
2561 pbuf = buf;
2562 faddr = offset >> 2;
2563 left = length >> 2;
2564 burst = OPTROM_BURST_DWORDS;
2565 while (left != 0) {
2566 if (burst > left)
2567 burst = left;
2568
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -08002569 rval = qla2x00_dump_ram(vha, optrom_dma,
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08002570 flash_data_addr(ha, faddr), burst);
Andrew Vasquez338c9162007-09-20 14:07:33 -07002571 if (rval) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07002572 ql_log(ql_log_warn, vha, 0x00f5,
2573 "Unable to burst-read optrom segment (%x/%x/%llx).\n",
2574 rval, flash_data_addr(ha, faddr),
Andrew Morton875baf32007-10-16 14:28:20 -07002575 (unsigned long long)optrom_dma);
Saurav Kashyap7c3df132011-07-14 12:00:13 -07002576 ql_log(ql_log_warn, vha, 0x00f6,
Andrew Vasquez338c9162007-09-20 14:07:33 -07002577 "Reverting to slow-read.\n");
2578
2579 dma_free_coherent(&ha->pdev->dev, OPTROM_BURST_SIZE,
2580 optrom, optrom_dma);
2581 goto slow_read;
2582 }
2583
2584 memcpy(pbuf, optrom, burst * 4);
2585
2586 left -= burst;
2587 faddr += burst;
2588 pbuf += burst * 4;
2589 }
2590
2591 dma_free_coherent(&ha->pdev->dev, OPTROM_BURST_SIZE, optrom,
2592 optrom_dma);
2593
2594 return buf;
2595
2596slow_read:
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -08002597 return qla24xx_read_optrom_data(vha, buf, offset, length);
Andrew Vasquez338c9162007-09-20 14:07:33 -07002598}
2599
Andrew Vasquez30c47662007-01-29 10:22:21 -08002600/**
2601 * qla2x00_get_fcode_version() - Determine an FCODE image's version.
2602 * @ha: HA context
2603 * @pcids: Pointer to the FCODE PCI data structure
2604 *
2605 * The process of retrieving the FCODE version information is at best
2606 * described as interesting.
2607 *
2608 * Within the first 100h bytes of the image an ASCII string is present
2609 * which contains several pieces of information including the FCODE
2610 * version. Unfortunately it seems the only reliable way to retrieve
2611 * the version is by scanning for another sentinel within the string,
2612 * the FCODE build date:
2613 *
2614 * ... 2.00.02 10/17/02 ...
2615 *
2616 * Returns QLA_SUCCESS on successful retrieval of version.
2617 */
2618static void
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -08002619qla2x00_get_fcode_version(struct qla_hw_data *ha, uint32_t pcids)
Andrew Vasquez30c47662007-01-29 10:22:21 -08002620{
2621 int ret = QLA_FUNCTION_FAILED;
2622 uint32_t istart, iend, iter, vend;
2623 uint8_t do_next, rbyte, *vbyte;
2624
2625 memset(ha->fcode_revision, 0, sizeof(ha->fcode_revision));
2626
2627 /* Skip the PCI data structure. */
2628 istart = pcids +
2629 ((qla2x00_read_flash_byte(ha, pcids + 0x0B) << 8) |
2630 qla2x00_read_flash_byte(ha, pcids + 0x0A));
2631 iend = istart + 0x100;
2632 do {
2633 /* Scan for the sentinel date string...eeewww. */
2634 do_next = 0;
2635 iter = istart;
2636 while ((iter < iend) && !do_next) {
2637 iter++;
2638 if (qla2x00_read_flash_byte(ha, iter) == '/') {
2639 if (qla2x00_read_flash_byte(ha, iter + 2) ==
2640 '/')
2641 do_next++;
2642 else if (qla2x00_read_flash_byte(ha,
2643 iter + 3) == '/')
2644 do_next++;
2645 }
2646 }
2647 if (!do_next)
2648 break;
2649
2650 /* Backtrack to previous ' ' (space). */
2651 do_next = 0;
2652 while ((iter > istart) && !do_next) {
2653 iter--;
2654 if (qla2x00_read_flash_byte(ha, iter) == ' ')
2655 do_next++;
2656 }
2657 if (!do_next)
2658 break;
2659
2660 /*
2661 * Mark end of version tag, and find previous ' ' (space) or
2662 * string length (recent FCODE images -- major hack ahead!!!).
2663 */
2664 vend = iter - 1;
2665 do_next = 0;
2666 while ((iter > istart) && !do_next) {
2667 iter--;
2668 rbyte = qla2x00_read_flash_byte(ha, iter);
2669 if (rbyte == ' ' || rbyte == 0xd || rbyte == 0x10)
2670 do_next++;
2671 }
2672 if (!do_next)
2673 break;
2674
2675 /* Mark beginning of version tag, and copy data. */
2676 iter++;
2677 if ((vend - iter) &&
2678 ((vend - iter) < sizeof(ha->fcode_revision))) {
2679 vbyte = ha->fcode_revision;
2680 while (iter <= vend) {
2681 *vbyte++ = qla2x00_read_flash_byte(ha, iter);
2682 iter++;
2683 }
2684 ret = QLA_SUCCESS;
2685 }
2686 } while (0);
2687
2688 if (ret != QLA_SUCCESS)
2689 memset(ha->fcode_revision, 0, sizeof(ha->fcode_revision));
2690}
2691
2692int
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -08002693qla2x00_get_flash_version(scsi_qla_host_t *vha, void *mbuf)
Andrew Vasquez30c47662007-01-29 10:22:21 -08002694{
2695 int ret = QLA_SUCCESS;
2696 uint8_t code_type, last_image;
2697 uint32_t pcihdr, pcids;
2698 uint8_t *dbyte;
2699 uint16_t *dcode;
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -08002700 struct qla_hw_data *ha = vha->hw;
Andrew Vasquez30c47662007-01-29 10:22:21 -08002701
2702 if (!ha->pio_address || !mbuf)
2703 return QLA_FUNCTION_FAILED;
2704
2705 memset(ha->bios_revision, 0, sizeof(ha->bios_revision));
2706 memset(ha->efi_revision, 0, sizeof(ha->efi_revision));
2707 memset(ha->fcode_revision, 0, sizeof(ha->fcode_revision));
2708 memset(ha->fw_revision, 0, sizeof(ha->fw_revision));
2709
2710 qla2x00_flash_enable(ha);
2711
2712 /* Begin with first PCI expansion ROM header. */
2713 pcihdr = 0;
2714 last_image = 1;
2715 do {
2716 /* Verify PCI expansion ROM header. */
2717 if (qla2x00_read_flash_byte(ha, pcihdr) != 0x55 ||
2718 qla2x00_read_flash_byte(ha, pcihdr + 0x01) != 0xaa) {
2719 /* No signature */
Saurav Kashyap7c3df132011-07-14 12:00:13 -07002720 ql_log(ql_log_fatal, vha, 0x0050,
2721 "No matching ROM signature.\n");
Andrew Vasquez30c47662007-01-29 10:22:21 -08002722 ret = QLA_FUNCTION_FAILED;
2723 break;
2724 }
2725
2726 /* Locate PCI data structure. */
2727 pcids = pcihdr +
2728 ((qla2x00_read_flash_byte(ha, pcihdr + 0x19) << 8) |
2729 qla2x00_read_flash_byte(ha, pcihdr + 0x18));
2730
2731 /* Validate signature of PCI data structure. */
2732 if (qla2x00_read_flash_byte(ha, pcids) != 'P' ||
2733 qla2x00_read_flash_byte(ha, pcids + 0x1) != 'C' ||
2734 qla2x00_read_flash_byte(ha, pcids + 0x2) != 'I' ||
2735 qla2x00_read_flash_byte(ha, pcids + 0x3) != 'R') {
2736 /* Incorrect header. */
Saurav Kashyap7c3df132011-07-14 12:00:13 -07002737 ql_log(ql_log_fatal, vha, 0x0051,
2738 "PCI data struct not found pcir_adr=%x.\n", pcids);
Andrew Vasquez30c47662007-01-29 10:22:21 -08002739 ret = QLA_FUNCTION_FAILED;
2740 break;
2741 }
2742
2743 /* Read version */
2744 code_type = qla2x00_read_flash_byte(ha, pcids + 0x14);
2745 switch (code_type) {
2746 case ROM_CODE_TYPE_BIOS:
2747 /* Intel x86, PC-AT compatible. */
2748 ha->bios_revision[0] =
2749 qla2x00_read_flash_byte(ha, pcids + 0x12);
2750 ha->bios_revision[1] =
2751 qla2x00_read_flash_byte(ha, pcids + 0x13);
Saurav Kashyap7c3df132011-07-14 12:00:13 -07002752 ql_dbg(ql_dbg_init, vha, 0x0052,
2753 "Read BIOS %d.%d.\n",
2754 ha->bios_revision[1], ha->bios_revision[0]);
Andrew Vasquez30c47662007-01-29 10:22:21 -08002755 break;
2756 case ROM_CODE_TYPE_FCODE:
2757 /* Open Firmware standard for PCI (FCode). */
2758 /* Eeeewww... */
2759 qla2x00_get_fcode_version(ha, pcids);
2760 break;
2761 case ROM_CODE_TYPE_EFI:
2762 /* Extensible Firmware Interface (EFI). */
2763 ha->efi_revision[0] =
2764 qla2x00_read_flash_byte(ha, pcids + 0x12);
2765 ha->efi_revision[1] =
2766 qla2x00_read_flash_byte(ha, pcids + 0x13);
Saurav Kashyap7c3df132011-07-14 12:00:13 -07002767 ql_dbg(ql_dbg_init, vha, 0x0053,
2768 "Read EFI %d.%d.\n",
2769 ha->efi_revision[1], ha->efi_revision[0]);
Andrew Vasquez30c47662007-01-29 10:22:21 -08002770 break;
2771 default:
Saurav Kashyap7c3df132011-07-14 12:00:13 -07002772 ql_log(ql_log_warn, vha, 0x0054,
2773 "Unrecognized code type %x at pcids %x.\n",
2774 code_type, pcids);
Andrew Vasquez30c47662007-01-29 10:22:21 -08002775 break;
2776 }
2777
2778 last_image = qla2x00_read_flash_byte(ha, pcids + 0x15) & BIT_7;
2779
2780 /* Locate next PCI expansion ROM. */
2781 pcihdr += ((qla2x00_read_flash_byte(ha, pcids + 0x11) << 8) |
2782 qla2x00_read_flash_byte(ha, pcids + 0x10)) * 512;
2783 } while (!last_image);
2784
2785 if (IS_QLA2322(ha)) {
2786 /* Read firmware image information. */
2787 memset(ha->fw_revision, 0, sizeof(ha->fw_revision));
2788 dbyte = mbuf;
2789 memset(dbyte, 0, 8);
2790 dcode = (uint16_t *)dbyte;
2791
Andrew Vasquezc00d8992008-09-11 21:22:49 -07002792 qla2x00_read_flash_data(ha, dbyte, ha->flt_region_fw * 4 + 10,
Andrew Vasquez30c47662007-01-29 10:22:21 -08002793 8);
Saurav Kashyap7c3df132011-07-14 12:00:13 -07002794 ql_dbg(ql_dbg_init + ql_dbg_buffer, vha, 0x010a,
2795 "Dumping fw "
2796 "ver from flash:.\n");
2797 ql_dump_buffer(ql_dbg_init + ql_dbg_buffer, vha, 0x010b,
2798 (uint8_t *)dbyte, 8);
Andrew Vasquez30c47662007-01-29 10:22:21 -08002799
2800 if ((dcode[0] == 0xffff && dcode[1] == 0xffff &&
2801 dcode[2] == 0xffff && dcode[3] == 0xffff) ||
2802 (dcode[0] == 0 && dcode[1] == 0 && dcode[2] == 0 &&
2803 dcode[3] == 0)) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07002804 ql_log(ql_log_warn, vha, 0x0057,
2805 "Unrecognized fw revision at %x.\n",
2806 ha->flt_region_fw * 4);
Andrew Vasquez30c47662007-01-29 10:22:21 -08002807 } else {
2808 /* values are in big endian */
2809 ha->fw_revision[0] = dbyte[0] << 16 | dbyte[1];
2810 ha->fw_revision[1] = dbyte[2] << 16 | dbyte[3];
2811 ha->fw_revision[2] = dbyte[4] << 16 | dbyte[5];
Saurav Kashyap7c3df132011-07-14 12:00:13 -07002812 ql_dbg(ql_dbg_init, vha, 0x0058,
2813 "FW Version: "
2814 "%d.%d.%d.\n", ha->fw_revision[0],
2815 ha->fw_revision[1], ha->fw_revision[2]);
Andrew Vasquez30c47662007-01-29 10:22:21 -08002816 }
2817 }
2818
2819 qla2x00_flash_disable(ha);
2820
2821 return ret;
2822}
2823
2824int
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -08002825qla24xx_get_flash_version(scsi_qla_host_t *vha, void *mbuf)
Andrew Vasquez30c47662007-01-29 10:22:21 -08002826{
2827 int ret = QLA_SUCCESS;
2828 uint32_t pcihdr, pcids;
2829 uint32_t *dcode;
2830 uint8_t *bcode;
2831 uint8_t code_type, last_image;
2832 int i;
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -08002833 struct qla_hw_data *ha = vha->hw;
Andrew Vasquez30c47662007-01-29 10:22:21 -08002834
Giridhar Malavalia9083012010-04-12 17:59:55 -07002835 if (IS_QLA82XX(ha))
2836 return ret;
2837
Andrew Vasquez30c47662007-01-29 10:22:21 -08002838 if (!mbuf)
2839 return QLA_FUNCTION_FAILED;
2840
2841 memset(ha->bios_revision, 0, sizeof(ha->bios_revision));
2842 memset(ha->efi_revision, 0, sizeof(ha->efi_revision));
2843 memset(ha->fcode_revision, 0, sizeof(ha->fcode_revision));
2844 memset(ha->fw_revision, 0, sizeof(ha->fw_revision));
2845
2846 dcode = mbuf;
2847
2848 /* Begin with first PCI expansion ROM header. */
Harish Zunjarrao6315a5f2009-03-24 09:07:59 -07002849 pcihdr = ha->flt_region_boot << 2;
Andrew Vasquez30c47662007-01-29 10:22:21 -08002850 last_image = 1;
2851 do {
2852 /* Verify PCI expansion ROM header. */
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -08002853 qla24xx_read_flash_data(vha, dcode, pcihdr >> 2, 0x20);
Andrew Vasquez30c47662007-01-29 10:22:21 -08002854 bcode = mbuf + (pcihdr % 4);
2855 if (bcode[0x0] != 0x55 || bcode[0x1] != 0xaa) {
2856 /* No signature */
Saurav Kashyap7c3df132011-07-14 12:00:13 -07002857 ql_log(ql_log_fatal, vha, 0x0059,
2858 "No matching ROM signature.\n");
Andrew Vasquez30c47662007-01-29 10:22:21 -08002859 ret = QLA_FUNCTION_FAILED;
2860 break;
2861 }
2862
2863 /* Locate PCI data structure. */
2864 pcids = pcihdr + ((bcode[0x19] << 8) | bcode[0x18]);
2865
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -08002866 qla24xx_read_flash_data(vha, dcode, pcids >> 2, 0x20);
Andrew Vasquez30c47662007-01-29 10:22:21 -08002867 bcode = mbuf + (pcihdr % 4);
2868
2869 /* Validate signature of PCI data structure. */
2870 if (bcode[0x0] != 'P' || bcode[0x1] != 'C' ||
2871 bcode[0x2] != 'I' || bcode[0x3] != 'R') {
2872 /* Incorrect header. */
Saurav Kashyap7c3df132011-07-14 12:00:13 -07002873 ql_log(ql_log_fatal, vha, 0x005a,
2874 "PCI data struct not found pcir_adr=%x.\n", pcids);
Andrew Vasquez30c47662007-01-29 10:22:21 -08002875 ret = QLA_FUNCTION_FAILED;
2876 break;
2877 }
2878
2879 /* Read version */
2880 code_type = bcode[0x14];
2881 switch (code_type) {
2882 case ROM_CODE_TYPE_BIOS:
2883 /* Intel x86, PC-AT compatible. */
2884 ha->bios_revision[0] = bcode[0x12];
2885 ha->bios_revision[1] = bcode[0x13];
Saurav Kashyap7c3df132011-07-14 12:00:13 -07002886 ql_dbg(ql_dbg_init, vha, 0x005b,
2887 "Read BIOS %d.%d.\n",
2888 ha->bios_revision[1], ha->bios_revision[0]);
Andrew Vasquez30c47662007-01-29 10:22:21 -08002889 break;
2890 case ROM_CODE_TYPE_FCODE:
2891 /* Open Firmware standard for PCI (FCode). */
2892 ha->fcode_revision[0] = bcode[0x12];
2893 ha->fcode_revision[1] = bcode[0x13];
Saurav Kashyap7c3df132011-07-14 12:00:13 -07002894 ql_dbg(ql_dbg_init, vha, 0x005c,
2895 "Read FCODE %d.%d.\n",
2896 ha->fcode_revision[1], ha->fcode_revision[0]);
Andrew Vasquez30c47662007-01-29 10:22:21 -08002897 break;
2898 case ROM_CODE_TYPE_EFI:
2899 /* Extensible Firmware Interface (EFI). */
2900 ha->efi_revision[0] = bcode[0x12];
2901 ha->efi_revision[1] = bcode[0x13];
Saurav Kashyap7c3df132011-07-14 12:00:13 -07002902 ql_dbg(ql_dbg_init, vha, 0x005d,
2903 "Read EFI %d.%d.\n",
2904 ha->efi_revision[1], ha->efi_revision[0]);
Andrew Vasquez30c47662007-01-29 10:22:21 -08002905 break;
2906 default:
Saurav Kashyap7c3df132011-07-14 12:00:13 -07002907 ql_log(ql_log_warn, vha, 0x005e,
2908 "Unrecognized code type %x at pcids %x.\n",
2909 code_type, pcids);
Andrew Vasquez30c47662007-01-29 10:22:21 -08002910 break;
2911 }
2912
2913 last_image = bcode[0x15] & BIT_7;
2914
2915 /* Locate next PCI expansion ROM. */
2916 pcihdr += ((bcode[0x11] << 8) | bcode[0x10]) * 512;
2917 } while (!last_image);
2918
2919 /* Read firmware image information. */
2920 memset(ha->fw_revision, 0, sizeof(ha->fw_revision));
2921 dcode = mbuf;
2922
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -08002923 qla24xx_read_flash_data(vha, dcode, ha->flt_region_fw + 4, 4);
Andrew Vasquez30c47662007-01-29 10:22:21 -08002924 for (i = 0; i < 4; i++)
2925 dcode[i] = be32_to_cpu(dcode[i]);
2926
2927 if ((dcode[0] == 0xffffffff && dcode[1] == 0xffffffff &&
2928 dcode[2] == 0xffffffff && dcode[3] == 0xffffffff) ||
2929 (dcode[0] == 0 && dcode[1] == 0 && dcode[2] == 0 &&
2930 dcode[3] == 0)) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07002931 ql_log(ql_log_warn, vha, 0x005f,
2932 "Unrecognized fw revision at %x.\n",
2933 ha->flt_region_fw * 4);
Andrew Vasquez30c47662007-01-29 10:22:21 -08002934 } else {
2935 ha->fw_revision[0] = dcode[0];
2936 ha->fw_revision[1] = dcode[1];
2937 ha->fw_revision[2] = dcode[2];
2938 ha->fw_revision[3] = dcode[3];
Saurav Kashyap7c3df132011-07-14 12:00:13 -07002939 ql_dbg(ql_dbg_init, vha, 0x0060,
2940 "Firmware revision %d.%d.%d.%d.\n",
2941 ha->fw_revision[0], ha->fw_revision[1],
2942 ha->fw_revision[2], ha->fw_revision[3]);
Andrew Vasquez30c47662007-01-29 10:22:21 -08002943 }
2944
Madhuranath Iyengar0f2d9622010-07-23 15:28:26 +05002945 /* Check for golden firmware and get version if available */
2946 if (!IS_QLA81XX(ha)) {
2947 /* Golden firmware is not present in non 81XX adapters */
2948 return ret;
2949 }
2950
2951 memset(ha->gold_fw_version, 0, sizeof(ha->gold_fw_version));
2952 dcode = mbuf;
2953 ha->isp_ops->read_optrom(vha, (uint8_t *)dcode,
2954 ha->flt_region_gold_fw << 2, 32);
2955
2956 if (dcode[4] == 0xFFFFFFFF && dcode[5] == 0xFFFFFFFF &&
2957 dcode[6] == 0xFFFFFFFF && dcode[7] == 0xFFFFFFFF) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07002958 ql_log(ql_log_warn, vha, 0x0056,
2959 "Unrecognized golden fw at 0x%x.\n",
2960 ha->flt_region_gold_fw * 4);
Madhuranath Iyengar0f2d9622010-07-23 15:28:26 +05002961 return ret;
2962 }
2963
2964 for (i = 4; i < 8; i++)
2965 ha->gold_fw_version[i-4] = be32_to_cpu(dcode[i]);
2966
Andrew Vasquez30c47662007-01-29 10:22:21 -08002967 return ret;
2968}
Andrew Vasquezcb8dacb2008-04-03 13:13:19 -07002969
2970static int
Joe Carnuccio1ee27142008-07-10 16:55:53 -07002971qla2xxx_is_vpd_valid(uint8_t *pos, uint8_t *end)
2972{
2973 if (pos >= end || *pos != 0x82)
2974 return 0;
2975
2976 pos += 3 + pos[1];
2977 if (pos >= end || *pos != 0x90)
2978 return 0;
2979
2980 pos += 3 + pos[1];
2981 if (pos >= end || *pos != 0x78)
2982 return 0;
2983
2984 return 1;
2985}
2986
2987int
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -08002988qla2xxx_get_vpd_field(scsi_qla_host_t *vha, char *key, char *str, size_t size)
Joe Carnuccio1ee27142008-07-10 16:55:53 -07002989{
Anirban Chakraborty7b867cf2008-11-06 10:40:19 -08002990 struct qla_hw_data *ha = vha->hw;
Joe Carnuccio1ee27142008-07-10 16:55:53 -07002991 uint8_t *pos = ha->vpd;
2992 uint8_t *end = pos + ha->vpd_size;
2993 int len = 0;
2994
2995 if (!IS_FWI2_CAPABLE(ha) || !qla2xxx_is_vpd_valid(pos, end))
2996 return 0;
2997
2998 while (pos < end && *pos != 0x78) {
2999 len = (*pos == 0x82) ? pos[1] : pos[2];
3000
3001 if (!strncmp(pos, key, strlen(key)))
3002 break;
3003
3004 if (*pos != 0x90 && *pos != 0x91)
3005 pos += len;
3006
3007 pos += 3;
3008 }
3009
3010 if (pos < end - len && *pos != 0x78)
3011 return snprintf(str, size, "%.*s", len, pos + 3);
3012
3013 return 0;
3014}
Sarang Radke09ff7012010-03-19 17:03:59 -07003015
3016int
3017qla24xx_read_fcp_prio_cfg(scsi_qla_host_t *vha)
3018{
3019 int len, max_len;
3020 uint32_t fcp_prio_addr;
3021 struct qla_hw_data *ha = vha->hw;
3022
3023 if (!ha->fcp_prio_cfg) {
3024 ha->fcp_prio_cfg = vmalloc(FCP_PRIO_CFG_SIZE);
3025 if (!ha->fcp_prio_cfg) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07003026 ql_log(ql_log_warn, vha, 0x00d5,
3027 "Unable to allocate memory for fcp priorty data (%x).\n",
3028 FCP_PRIO_CFG_SIZE);
Sarang Radke09ff7012010-03-19 17:03:59 -07003029 return QLA_FUNCTION_FAILED;
3030 }
3031 }
3032 memset(ha->fcp_prio_cfg, 0, FCP_PRIO_CFG_SIZE);
3033
3034 fcp_prio_addr = ha->flt_region_fcp_prio;
3035
3036 /* first read the fcp priority data header from flash */
3037 ha->isp_ops->read_optrom(vha, (uint8_t *)ha->fcp_prio_cfg,
3038 fcp_prio_addr << 2, FCP_PRIO_CFG_HDR_SIZE);
3039
Saurav Kashyap7c3df132011-07-14 12:00:13 -07003040 if (!qla24xx_fcp_prio_cfg_valid(vha, ha->fcp_prio_cfg, 0))
Sarang Radke09ff7012010-03-19 17:03:59 -07003041 goto fail;
3042
3043 /* read remaining FCP CMD config data from flash */
3044 fcp_prio_addr += (FCP_PRIO_CFG_HDR_SIZE >> 2);
3045 len = ha->fcp_prio_cfg->num_entries * FCP_PRIO_CFG_ENTRY_SIZE;
3046 max_len = FCP_PRIO_CFG_SIZE - FCP_PRIO_CFG_HDR_SIZE;
3047
3048 ha->isp_ops->read_optrom(vha, (uint8_t *)&ha->fcp_prio_cfg->entry[0],
3049 fcp_prio_addr << 2, (len < max_len ? len : max_len));
3050
3051 /* revalidate the entire FCP priority config data, including entries */
Saurav Kashyap7c3df132011-07-14 12:00:13 -07003052 if (!qla24xx_fcp_prio_cfg_valid(vha, ha->fcp_prio_cfg, 1))
Sarang Radke09ff7012010-03-19 17:03:59 -07003053 goto fail;
3054
3055 ha->flags.fcp_prio_enabled = 1;
3056 return QLA_SUCCESS;
3057fail:
3058 vfree(ha->fcp_prio_cfg);
3059 ha->fcp_prio_cfg = NULL;
3060 return QLA_FUNCTION_FAILED;
3061}