blob: ce32322f1e19ae095a7e52c0a09f2b8529d6caf9 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Andrew Vasquezfa90c542005-10-27 11:10:08 -07002 * QLogic Fibre Channel HBA Driver
3 * Copyright (c) 2003-2005 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.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 */
7#include "qla_def.h"
8
9#include <linux/delay.h>
Andrew Vasquez01071092005-07-06 10:31:37 -070010#include <linux/vmalloc.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070011
12#include "qla_devtbl.h"
13
14/* XXX(hch): this is ugly, but we don't want to pull in exioctl.h */
15#ifndef EXT_IS_LUN_BIT_SET
16#define EXT_IS_LUN_BIT_SET(P,L) \
17 (((P)->mask[L/8] & (0x80 >> (L%8)))?1:0)
18#define EXT_SET_LUN_BIT(P,L) \
19 ((P)->mask[L/8] |= (0x80 >> (L%8)))
20#endif
21
22/*
23* QLogic ISP2x00 Hardware Support Function Prototypes.
24*/
Linus Torvalds1da177e2005-04-16 15:20:36 -070025static int qla2x00_isp_firmware(scsi_qla_host_t *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070026static void qla2x00_resize_request_q(scsi_qla_host_t *);
27static int qla2x00_setup_chip(scsi_qla_host_t *);
28static void qla2x00_init_response_q_entries(scsi_qla_host_t *);
29static int qla2x00_init_rings(scsi_qla_host_t *);
30static int qla2x00_fw_ready(scsi_qla_host_t *);
31static int qla2x00_configure_hba(scsi_qla_host_t *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070032static int qla2x00_configure_loop(scsi_qla_host_t *);
33static int qla2x00_configure_local_loop(scsi_qla_host_t *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070034static int qla2x00_configure_fabric(scsi_qla_host_t *);
35static int qla2x00_find_all_fabric_devs(scsi_qla_host_t *, struct list_head *);
36static int qla2x00_device_resync(scsi_qla_host_t *);
37static int qla2x00_fabric_dev_login(scsi_qla_host_t *, fc_port_t *,
38 uint16_t *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070039
40static int qla2x00_restart_isp(scsi_qla_host_t *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
42/****************************************************************************/
43/* QLogic ISP2x00 Hardware Support Functions. */
44/****************************************************************************/
45
46/*
47* qla2x00_initialize_adapter
48* Initialize board.
49*
50* Input:
51* ha = adapter block pointer.
52*
53* Returns:
54* 0 = success
55*/
56int
57qla2x00_initialize_adapter(scsi_qla_host_t *ha)
58{
59 int rval;
60 uint8_t restart_risc = 0;
61 uint8_t retry;
62 uint32_t wait_time;
63
64 /* Clear adapter flags. */
65 ha->flags.online = 0;
66 ha->flags.reset_active = 0;
67 atomic_set(&ha->loop_down_timer, LOOP_DOWN_TIME);
68 atomic_set(&ha->loop_state, LOOP_DOWN);
69 ha->device_flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070070 ha->dpc_flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070071 ha->flags.management_server_logged_in = 0;
72 ha->marker_needed = 0;
73 ha->mbx_flags = 0;
74 ha->isp_abort_cnt = 0;
75 ha->beacon_blink_led = 0;
Andrew Vasquezcca53352005-08-26 19:08:30 -070076 set_bit(REGISTER_FDMI_NEEDED, &ha->dpc_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -070077
Andrew Vasquez01071092005-07-06 10:31:37 -070078 qla_printk(KERN_INFO, ha, "Configuring PCI space...\n");
Andrew Vasquezabbd8872005-07-06 10:30:05 -070079 rval = ha->isp_ops.pci_config(ha);
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 if (rval) {
81 DEBUG2(printk("scsi(%ld): Unable to configure PCI space=n",
82 ha->host_no));
83 return (rval);
84 }
85
Andrew Vasquezabbd8872005-07-06 10:30:05 -070086 ha->isp_ops.reset_chip(ha);
Linus Torvalds1da177e2005-04-16 15:20:36 -070087
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 qla_printk(KERN_INFO, ha, "Configure NVRAM parameters...\n");
Andrew Vasquez01071092005-07-06 10:31:37 -070089
Andrew Vasquezabbd8872005-07-06 10:30:05 -070090 ha->isp_ops.nvram_config(ha);
Linus Torvalds1da177e2005-04-16 15:20:36 -070091
Andrew Vasquezd4c760c2006-06-23 16:10:39 -070092 if (ha->flags.disable_serdes) {
93 /* Mask HBA via NVRAM settings? */
94 qla_printk(KERN_INFO, ha, "Masking HBA WWPN "
95 "%02x%02x%02x%02x%02x%02x%02x%02x (via NVRAM).\n",
96 ha->port_name[0], ha->port_name[1],
97 ha->port_name[2], ha->port_name[3],
98 ha->port_name[4], ha->port_name[5],
99 ha->port_name[6], ha->port_name[7]);
100 return QLA_FUNCTION_FAILED;
101 }
102
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 qla_printk(KERN_INFO, ha, "Verifying loaded RISC code...\n");
104
105 retry = 10;
106 /*
107 * Try to configure the loop.
108 */
109 do {
110 restart_risc = 0;
111
112 /* If firmware needs to be loaded */
113 if (qla2x00_isp_firmware(ha) != QLA_SUCCESS) {
Andrew Vasquezabbd8872005-07-06 10:30:05 -0700114 if ((rval = ha->isp_ops.chip_diag(ha)) == QLA_SUCCESS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 rval = qla2x00_setup_chip(ha);
116 }
117 }
118
119 if (rval == QLA_SUCCESS &&
120 (rval = qla2x00_init_rings(ha)) == QLA_SUCCESS) {
121check_fw_ready_again:
122 /*
Andrew Vasquezfa2a1ce2005-07-06 10:32:07 -0700123 * Wait for a successful LIP up to a maximum
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 * of (in seconds): RISC login timeout value,
125 * RISC retry count value, and port down retry
Andrew Vasquezfa2a1ce2005-07-06 10:32:07 -0700126 * value OR a minimum of 4 seconds OR If no
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 * cable, only 5 seconds.
128 */
129 rval = qla2x00_fw_ready(ha);
130 if (rval == QLA_SUCCESS) {
131 clear_bit(RESET_MARKER_NEEDED, &ha->dpc_flags);
132
Andrew Vasquez01071092005-07-06 10:31:37 -0700133 /* Issue a marker after FW becomes ready. */
134 qla2x00_marker(ha, 0, 0, MK_SYNC_ALL);
135
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 /*
137 * Wait at most MAX_TARGET RSCNs for a stable
138 * link.
139 */
140 wait_time = 256;
141 do {
142 clear_bit(LOOP_RESYNC_NEEDED,
143 &ha->dpc_flags);
144 rval = qla2x00_configure_loop(ha);
145
146 if (test_and_clear_bit(ISP_ABORT_NEEDED,
147 &ha->dpc_flags)) {
148 restart_risc = 1;
149 break;
150 }
151
152 /*
153 * If loop state change while we were
154 * discoverying devices then wait for
155 * LIP to complete
156 */
157
Ravi Anand33135aa2005-11-08 14:37:20 -0800158 if (atomic_read(&ha->loop_state) !=
159 LOOP_READY && retry--) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 goto check_fw_ready_again;
161 }
162 wait_time--;
163 } while (!atomic_read(&ha->loop_down_timer) &&
164 retry &&
165 wait_time &&
166 (test_bit(LOOP_RESYNC_NEEDED,
167 &ha->dpc_flags)));
168
169 if (wait_time == 0)
170 rval = QLA_FUNCTION_FAILED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 } else if (ha->device_flags & DFLG_NO_CABLE)
172 /* If no cable, then all is good. */
173 rval = QLA_SUCCESS;
174 }
175 } while (restart_risc && retry--);
176
177 if (rval == QLA_SUCCESS) {
178 clear_bit(RESET_MARKER_NEEDED, &ha->dpc_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 qla2x00_marker(ha, 0, 0, MK_SYNC_ALL);
180 ha->marker_needed = 0;
181
182 ha->flags.online = 1;
183 } else {
184 DEBUG2_3(printk("%s(): **** FAILED ****\n", __func__));
185 }
186
187 return (rval);
188}
189
190/**
Andrew Vasquezabbd8872005-07-06 10:30:05 -0700191 * qla2100_pci_config() - Setup ISP21xx PCI configuration registers.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 * @ha: HA context
193 *
194 * Returns 0 on success.
195 */
Andrew Vasquezabbd8872005-07-06 10:30:05 -0700196int
197qla2100_pci_config(scsi_qla_host_t *ha)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198{
Andrew Vasquezabbd8872005-07-06 10:30:05 -0700199 uint16_t w, mwi;
Adam Kropelin27b2f672005-09-16 19:28:20 -0700200 uint32_t d;
Andrew Vasquezabbd8872005-07-06 10:30:05 -0700201 unsigned long flags;
Andrew Vasquez3d716442005-07-06 10:30:26 -0700202 struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 pci_set_master(ha->pdev);
205 mwi = 0;
206 if (pci_set_mwi(ha->pdev))
207 mwi = PCI_COMMAND_INVALIDATE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 pci_read_config_word(ha->pdev, PCI_COMMAND, &w);
210 w |= mwi | (PCI_COMMAND_PARITY | PCI_COMMAND_SERR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 pci_write_config_word(ha->pdev, PCI_COMMAND, w);
212
213 /* Reset expansion ROM address decode enable */
Adam Kropelin27b2f672005-09-16 19:28:20 -0700214 pci_read_config_dword(ha->pdev, PCI_ROM_ADDRESS, &d);
215 d &= ~PCI_ROM_ADDRESS_ENABLE;
216 pci_write_config_dword(ha->pdev, PCI_ROM_ADDRESS, d);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217
Andrew Vasquezabbd8872005-07-06 10:30:05 -0700218 /* Get PCI bus information. */
219 spin_lock_irqsave(&ha->hardware_lock, flags);
Andrew Vasquez3d716442005-07-06 10:30:26 -0700220 ha->pci_attr = RD_REG_WORD(&reg->ctrl_status);
Andrew Vasquezabbd8872005-07-06 10:30:05 -0700221 spin_unlock_irqrestore(&ha->hardware_lock, flags);
222
223 return QLA_SUCCESS;
224}
225
226/**
227 * qla2300_pci_config() - Setup ISP23xx PCI configuration registers.
228 * @ha: HA context
229 *
230 * Returns 0 on success.
231 */
232int
233qla2300_pci_config(scsi_qla_host_t *ha)
234{
235 uint16_t w, mwi;
Adam Kropelin27b2f672005-09-16 19:28:20 -0700236 uint32_t d;
Andrew Vasquezabbd8872005-07-06 10:30:05 -0700237 unsigned long flags = 0;
238 uint32_t cnt;
Andrew Vasquez3d716442005-07-06 10:30:26 -0700239 struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
Andrew Vasquezabbd8872005-07-06 10:30:05 -0700240
Andrew Vasquezabbd8872005-07-06 10:30:05 -0700241 pci_set_master(ha->pdev);
242 mwi = 0;
243 if (pci_set_mwi(ha->pdev))
244 mwi = PCI_COMMAND_INVALIDATE;
Andrew Vasquezabbd8872005-07-06 10:30:05 -0700245
246 pci_read_config_word(ha->pdev, PCI_COMMAND, &w);
247 w |= mwi | (PCI_COMMAND_PARITY | PCI_COMMAND_SERR);
248
249 if (IS_QLA2322(ha) || IS_QLA6322(ha))
250 w &= ~PCI_COMMAND_INTX_DISABLE;
251
252 /*
253 * If this is a 2300 card and not 2312, reset the
254 * COMMAND_INVALIDATE due to a bug in the 2300. Unfortunately,
255 * the 2310 also reports itself as a 2300 so we need to get the
256 * fb revision level -- a 6 indicates it really is a 2300 and
257 * not a 2310.
258 */
259 if (IS_QLA2300(ha)) {
260 spin_lock_irqsave(&ha->hardware_lock, flags);
261
262 /* Pause RISC. */
Andrew Vasquez3d716442005-07-06 10:30:26 -0700263 WRT_REG_WORD(&reg->hccr, HCCR_PAUSE_RISC);
Andrew Vasquezabbd8872005-07-06 10:30:05 -0700264 for (cnt = 0; cnt < 30000; cnt++) {
Andrew Vasquez3d716442005-07-06 10:30:26 -0700265 if ((RD_REG_WORD(&reg->hccr) & HCCR_RISC_PAUSE) != 0)
Andrew Vasquezabbd8872005-07-06 10:30:05 -0700266 break;
267
268 udelay(10);
269 }
270
271 /* Select FPM registers. */
Andrew Vasquez3d716442005-07-06 10:30:26 -0700272 WRT_REG_WORD(&reg->ctrl_status, 0x20);
273 RD_REG_WORD(&reg->ctrl_status);
Andrew Vasquezabbd8872005-07-06 10:30:05 -0700274
275 /* Get the fb rev level */
Andrew Vasquez3d716442005-07-06 10:30:26 -0700276 ha->fb_rev = RD_FB_CMD_REG(ha, reg);
Andrew Vasquezabbd8872005-07-06 10:30:05 -0700277
278 if (ha->fb_rev == FPM_2300)
279 w &= ~PCI_COMMAND_INVALIDATE;
280
281 /* Deselect FPM registers. */
Andrew Vasquez3d716442005-07-06 10:30:26 -0700282 WRT_REG_WORD(&reg->ctrl_status, 0x0);
283 RD_REG_WORD(&reg->ctrl_status);
Andrew Vasquezabbd8872005-07-06 10:30:05 -0700284
285 /* Release RISC module. */
Andrew Vasquez3d716442005-07-06 10:30:26 -0700286 WRT_REG_WORD(&reg->hccr, HCCR_RELEASE_RISC);
Andrew Vasquezabbd8872005-07-06 10:30:05 -0700287 for (cnt = 0; cnt < 30000; cnt++) {
Andrew Vasquez3d716442005-07-06 10:30:26 -0700288 if ((RD_REG_WORD(&reg->hccr) & HCCR_RISC_PAUSE) == 0)
Andrew Vasquezabbd8872005-07-06 10:30:05 -0700289 break;
290
291 udelay(10);
292 }
293
294 spin_unlock_irqrestore(&ha->hardware_lock, flags);
295 }
296 pci_write_config_word(ha->pdev, PCI_COMMAND, w);
297
298 pci_write_config_byte(ha->pdev, PCI_LATENCY_TIMER, 0x80);
299
300 /* Reset expansion ROM address decode enable */
Adam Kropelin27b2f672005-09-16 19:28:20 -0700301 pci_read_config_dword(ha->pdev, PCI_ROM_ADDRESS, &d);
302 d &= ~PCI_ROM_ADDRESS_ENABLE;
303 pci_write_config_dword(ha->pdev, PCI_ROM_ADDRESS, d);
Andrew Vasquezabbd8872005-07-06 10:30:05 -0700304
305 /* Get PCI bus information. */
306 spin_lock_irqsave(&ha->hardware_lock, flags);
Andrew Vasquez3d716442005-07-06 10:30:26 -0700307 ha->pci_attr = RD_REG_WORD(&reg->ctrl_status);
Andrew Vasquezabbd8872005-07-06 10:30:05 -0700308 spin_unlock_irqrestore(&ha->hardware_lock, flags);
309
310 return QLA_SUCCESS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311}
312
313/**
Andrew Vasquez01071092005-07-06 10:31:37 -0700314 * qla24xx_pci_config() - Setup ISP24xx PCI configuration registers.
315 * @ha: HA context
316 *
317 * Returns 0 on success.
318 */
319int
320qla24xx_pci_config(scsi_qla_host_t *ha)
321{
322 uint16_t w, mwi;
Adam Kropelin27b2f672005-09-16 19:28:20 -0700323 uint32_t d;
Andrew Vasquez01071092005-07-06 10:31:37 -0700324 unsigned long flags = 0;
325 struct device_reg_24xx __iomem *reg = &ha->iobase->isp24;
326 int pcix_cmd_reg, pcie_dctl_reg;
327
328 pci_set_master(ha->pdev);
329 mwi = 0;
330 if (pci_set_mwi(ha->pdev))
331 mwi = PCI_COMMAND_INVALIDATE;
Andrew Vasquez01071092005-07-06 10:31:37 -0700332
333 pci_read_config_word(ha->pdev, PCI_COMMAND, &w);
334 w |= mwi | (PCI_COMMAND_PARITY | PCI_COMMAND_SERR);
335 w &= ~PCI_COMMAND_INTX_DISABLE;
336 pci_write_config_word(ha->pdev, PCI_COMMAND, w);
337
338 pci_write_config_byte(ha->pdev, PCI_LATENCY_TIMER, 0x80);
339
340 /* PCI-X -- adjust Maximum Memory Read Byte Count (2048). */
341 pcix_cmd_reg = pci_find_capability(ha->pdev, PCI_CAP_ID_PCIX);
342 if (pcix_cmd_reg) {
343 uint16_t pcix_cmd;
344
345 pcix_cmd_reg += PCI_X_CMD;
346 pci_read_config_word(ha->pdev, pcix_cmd_reg, &pcix_cmd);
347 pcix_cmd &= ~PCI_X_CMD_MAX_READ;
348 pcix_cmd |= 0x0008;
349 pci_write_config_word(ha->pdev, pcix_cmd_reg, pcix_cmd);
350 }
351
352 /* PCIe -- adjust Maximum Read Request Size (2048). */
353 pcie_dctl_reg = pci_find_capability(ha->pdev, PCI_CAP_ID_EXP);
354 if (pcie_dctl_reg) {
355 uint16_t pcie_dctl;
356
357 pcie_dctl_reg += PCI_EXP_DEVCTL;
358 pci_read_config_word(ha->pdev, pcie_dctl_reg, &pcie_dctl);
359 pcie_dctl &= ~PCI_EXP_DEVCTL_READRQ;
360 pcie_dctl |= 0x4000;
361 pci_write_config_word(ha->pdev, pcie_dctl_reg, pcie_dctl);
362 }
363
364 /* Reset expansion ROM address decode enable */
Adam Kropelin27b2f672005-09-16 19:28:20 -0700365 pci_read_config_dword(ha->pdev, PCI_ROM_ADDRESS, &d);
366 d &= ~PCI_ROM_ADDRESS_ENABLE;
367 pci_write_config_dword(ha->pdev, PCI_ROM_ADDRESS, d);
Andrew Vasquez01071092005-07-06 10:31:37 -0700368
369 /* Get PCI bus information. */
370 spin_lock_irqsave(&ha->hardware_lock, flags);
371 ha->pci_attr = RD_REG_DWORD(&reg->ctrl_status);
372 spin_unlock_irqrestore(&ha->hardware_lock, flags);
373
374 return QLA_SUCCESS;
375}
376
377/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 * qla2x00_isp_firmware() - Choose firmware image.
379 * @ha: HA context
380 *
381 * Returns 0 on success.
382 */
383static int
384qla2x00_isp_firmware(scsi_qla_host_t *ha)
385{
386 int rval;
387
388 /* Assume loading risc code */
Andrew Vasquezfa2a1ce2005-07-06 10:32:07 -0700389 rval = QLA_FUNCTION_FAILED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390
391 if (ha->flags.disable_risc_code_load) {
392 DEBUG2(printk("scsi(%ld): RISC CODE NOT loaded\n",
393 ha->host_no));
394 qla_printk(KERN_INFO, ha, "RISC CODE NOT loaded\n");
395
396 /* Verify checksum of loaded RISC code. */
Andrew Vasquez441d1072006-05-17 15:09:34 -0700397 rval = qla2x00_verify_checksum(ha, ha->fw_srisc_address);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 }
399
400 if (rval) {
401 DEBUG2_3(printk("scsi(%ld): **** Load RISC code ****\n",
402 ha->host_no));
403 }
404
405 return (rval);
406}
407
408/**
409 * qla2x00_reset_chip() - Reset ISP chip.
410 * @ha: HA context
411 *
412 * Returns 0 on success.
413 */
Andrew Vasquezabbd8872005-07-06 10:30:05 -0700414void
Andrew Vasquezfa2a1ce2005-07-06 10:32:07 -0700415qla2x00_reset_chip(scsi_qla_host_t *ha)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416{
417 unsigned long flags = 0;
Andrew Vasquez3d716442005-07-06 10:30:26 -0700418 struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 uint32_t cnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 uint16_t cmd;
421
Andrew Vasquezabbd8872005-07-06 10:30:05 -0700422 ha->isp_ops.disable_intrs(ha);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423
424 spin_lock_irqsave(&ha->hardware_lock, flags);
425
426 /* Turn off master enable */
427 cmd = 0;
428 pci_read_config_word(ha->pdev, PCI_COMMAND, &cmd);
429 cmd &= ~PCI_COMMAND_MASTER;
430 pci_write_config_word(ha->pdev, PCI_COMMAND, cmd);
431
432 if (!IS_QLA2100(ha)) {
433 /* Pause RISC. */
434 WRT_REG_WORD(&reg->hccr, HCCR_PAUSE_RISC);
435 if (IS_QLA2200(ha) || IS_QLA2300(ha)) {
436 for (cnt = 0; cnt < 30000; cnt++) {
437 if ((RD_REG_WORD(&reg->hccr) &
438 HCCR_RISC_PAUSE) != 0)
439 break;
440 udelay(100);
441 }
442 } else {
443 RD_REG_WORD(&reg->hccr); /* PCI Posting. */
444 udelay(10);
445 }
446
447 /* Select FPM registers. */
448 WRT_REG_WORD(&reg->ctrl_status, 0x20);
449 RD_REG_WORD(&reg->ctrl_status); /* PCI Posting. */
450
451 /* FPM Soft Reset. */
452 WRT_REG_WORD(&reg->fpm_diag_config, 0x100);
453 RD_REG_WORD(&reg->fpm_diag_config); /* PCI Posting. */
454
455 /* Toggle Fpm Reset. */
456 if (!IS_QLA2200(ha)) {
457 WRT_REG_WORD(&reg->fpm_diag_config, 0x0);
458 RD_REG_WORD(&reg->fpm_diag_config); /* PCI Posting. */
459 }
460
461 /* Select frame buffer registers. */
462 WRT_REG_WORD(&reg->ctrl_status, 0x10);
463 RD_REG_WORD(&reg->ctrl_status); /* PCI Posting. */
464
465 /* Reset frame buffer FIFOs. */
466 if (IS_QLA2200(ha)) {
467 WRT_FB_CMD_REG(ha, reg, 0xa000);
468 RD_FB_CMD_REG(ha, reg); /* PCI Posting. */
469 } else {
470 WRT_FB_CMD_REG(ha, reg, 0x00fc);
471
472 /* Read back fb_cmd until zero or 3 seconds max */
473 for (cnt = 0; cnt < 3000; cnt++) {
474 if ((RD_FB_CMD_REG(ha, reg) & 0xff) == 0)
475 break;
476 udelay(100);
477 }
478 }
479
480 /* Select RISC module registers. */
481 WRT_REG_WORD(&reg->ctrl_status, 0);
482 RD_REG_WORD(&reg->ctrl_status); /* PCI Posting. */
483
484 /* Reset RISC processor. */
485 WRT_REG_WORD(&reg->hccr, HCCR_RESET_RISC);
486 RD_REG_WORD(&reg->hccr); /* PCI Posting. */
487
488 /* Release RISC processor. */
489 WRT_REG_WORD(&reg->hccr, HCCR_RELEASE_RISC);
490 RD_REG_WORD(&reg->hccr); /* PCI Posting. */
491 }
492
493 WRT_REG_WORD(&reg->hccr, HCCR_CLR_RISC_INT);
494 WRT_REG_WORD(&reg->hccr, HCCR_CLR_HOST_INT);
495
496 /* Reset ISP chip. */
497 WRT_REG_WORD(&reg->ctrl_status, CSR_ISP_SOFT_RESET);
498
499 /* Wait for RISC to recover from reset. */
500 if (IS_QLA2100(ha) || IS_QLA2200(ha) || IS_QLA2300(ha)) {
501 /*
502 * It is necessary to for a delay here since the card doesn't
503 * respond to PCI reads during a reset. On some architectures
504 * this will result in an MCA.
505 */
506 udelay(20);
507 for (cnt = 30000; cnt; cnt--) {
508 if ((RD_REG_WORD(&reg->ctrl_status) &
509 CSR_ISP_SOFT_RESET) == 0)
510 break;
511 udelay(100);
512 }
513 } else
514 udelay(10);
515
516 /* Reset RISC processor. */
517 WRT_REG_WORD(&reg->hccr, HCCR_RESET_RISC);
518
519 WRT_REG_WORD(&reg->semaphore, 0);
520
521 /* Release RISC processor. */
522 WRT_REG_WORD(&reg->hccr, HCCR_RELEASE_RISC);
523 RD_REG_WORD(&reg->hccr); /* PCI Posting. */
524
525 if (IS_QLA2100(ha) || IS_QLA2200(ha) || IS_QLA2300(ha)) {
526 for (cnt = 0; cnt < 30000; cnt++) {
Andrew Vasquezffb39f02006-05-17 15:09:06 -0700527 if (RD_MAILBOX_REG(ha, reg, 0) != MBS_BUSY)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529
530 udelay(100);
531 }
532 } else
533 udelay(100);
534
535 /* Turn on master enable */
536 cmd |= PCI_COMMAND_MASTER;
537 pci_write_config_word(ha->pdev, PCI_COMMAND, cmd);
538
539 /* Disable RISC pause on FPM parity error. */
540 if (!IS_QLA2100(ha)) {
541 WRT_REG_WORD(&reg->hccr, HCCR_DISABLE_PARITY_PAUSE);
542 RD_REG_WORD(&reg->hccr); /* PCI Posting. */
543 }
544
545 spin_unlock_irqrestore(&ha->hardware_lock, flags);
546}
547
548/**
Andrew Vasquez88c26662005-07-08 17:59:26 -0700549 * qla24xx_reset_risc() - Perform full reset of ISP24xx RISC.
Andrew Vasquez01071092005-07-06 10:31:37 -0700550 * @ha: HA context
551 *
552 * Returns 0 on success.
553 */
Andrew Vasquez88c26662005-07-08 17:59:26 -0700554static inline void
555qla24xx_reset_risc(scsi_qla_host_t *ha)
Andrew Vasquez01071092005-07-06 10:31:37 -0700556{
557 unsigned long flags = 0;
558 struct device_reg_24xx __iomem *reg = &ha->iobase->isp24;
559 uint32_t cnt, d2;
Andrew Vasquez335a1cc2005-11-08 14:37:48 -0800560 uint16_t wd;
Andrew Vasquez01071092005-07-06 10:31:37 -0700561
Andrew Vasquez01071092005-07-06 10:31:37 -0700562 spin_lock_irqsave(&ha->hardware_lock, flags);
563
564 /* Reset RISC. */
565 WRT_REG_DWORD(&reg->ctrl_status, CSRX_DMA_SHUTDOWN|MWB_4096_BYTES);
566 for (cnt = 0; cnt < 30000; cnt++) {
567 if ((RD_REG_DWORD(&reg->ctrl_status) & CSRX_DMA_ACTIVE) == 0)
568 break;
569
570 udelay(10);
571 }
572
573 WRT_REG_DWORD(&reg->ctrl_status,
574 CSRX_ISP_SOFT_RESET|CSRX_DMA_SHUTDOWN|MWB_4096_BYTES);
Andrew Vasquez335a1cc2005-11-08 14:37:48 -0800575 pci_read_config_word(ha->pdev, PCI_COMMAND, &wd);
Andrew Vasquez88c26662005-07-08 17:59:26 -0700576
Andrew Vasquez335a1cc2005-11-08 14:37:48 -0800577 udelay(100);
Andrew Vasquez88c26662005-07-08 17:59:26 -0700578 /* Wait for firmware to complete NVRAM accesses. */
Andrew Vasquez88c26662005-07-08 17:59:26 -0700579 d2 = (uint32_t) RD_REG_WORD(&reg->mailbox0);
580 for (cnt = 10000 ; cnt && d2; cnt--) {
581 udelay(5);
582 d2 = (uint32_t) RD_REG_WORD(&reg->mailbox0);
583 barrier();
584 }
585
Andrew Vasquez335a1cc2005-11-08 14:37:48 -0800586 /* Wait for soft-reset to complete. */
Andrew Vasquez01071092005-07-06 10:31:37 -0700587 d2 = RD_REG_DWORD(&reg->ctrl_status);
588 for (cnt = 6000000 ; cnt && (d2 & CSRX_ISP_SOFT_RESET); cnt--) {
589 udelay(5);
590 d2 = RD_REG_DWORD(&reg->ctrl_status);
591 barrier();
592 }
593
594 WRT_REG_DWORD(&reg->hccr, HCCRX_SET_RISC_RESET);
595 RD_REG_DWORD(&reg->hccr);
596
597 WRT_REG_DWORD(&reg->hccr, HCCRX_REL_RISC_PAUSE);
598 RD_REG_DWORD(&reg->hccr);
599
600 WRT_REG_DWORD(&reg->hccr, HCCRX_CLR_RISC_RESET);
601 RD_REG_DWORD(&reg->hccr);
602
603 d2 = (uint32_t) RD_REG_WORD(&reg->mailbox0);
604 for (cnt = 6000000 ; cnt && d2; cnt--) {
605 udelay(5);
606 d2 = (uint32_t) RD_REG_WORD(&reg->mailbox0);
607 barrier();
608 }
609
610 spin_unlock_irqrestore(&ha->hardware_lock, flags);
611}
612
613/**
Andrew Vasquez88c26662005-07-08 17:59:26 -0700614 * qla24xx_reset_chip() - Reset ISP24xx chip.
615 * @ha: HA context
616 *
617 * Returns 0 on success.
618 */
619void
620qla24xx_reset_chip(scsi_qla_host_t *ha)
621{
622 ha->isp_ops.disable_intrs(ha);
623
624 /* Perform RISC reset. */
625 qla24xx_reset_risc(ha);
626}
627
628/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 * qla2x00_chip_diag() - Test chip for proper operation.
630 * @ha: HA context
631 *
632 * Returns 0 on success.
633 */
Andrew Vasquezabbd8872005-07-06 10:30:05 -0700634int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635qla2x00_chip_diag(scsi_qla_host_t *ha)
636{
637 int rval;
Andrew Vasquez3d716442005-07-06 10:30:26 -0700638 struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 unsigned long flags = 0;
640 uint16_t data;
641 uint32_t cnt;
642 uint16_t mb[5];
643
644 /* Assume a failed state */
645 rval = QLA_FUNCTION_FAILED;
646
647 DEBUG3(printk("scsi(%ld): Testing device at %lx.\n",
648 ha->host_no, (u_long)&reg->flash_address));
649
650 spin_lock_irqsave(&ha->hardware_lock, flags);
651
652 /* Reset ISP chip. */
653 WRT_REG_WORD(&reg->ctrl_status, CSR_ISP_SOFT_RESET);
654
655 /*
656 * We need to have a delay here since the card will not respond while
657 * in reset causing an MCA on some architectures.
658 */
659 udelay(20);
660 data = qla2x00_debounce_register(&reg->ctrl_status);
661 for (cnt = 6000000 ; cnt && (data & CSR_ISP_SOFT_RESET); cnt--) {
662 udelay(5);
663 data = RD_REG_WORD(&reg->ctrl_status);
664 barrier();
665 }
666
667 if (!cnt)
668 goto chip_diag_failed;
669
670 DEBUG3(printk("scsi(%ld): Reset register cleared by chip reset\n",
671 ha->host_no));
672
673 /* Reset RISC processor. */
674 WRT_REG_WORD(&reg->hccr, HCCR_RESET_RISC);
675 WRT_REG_WORD(&reg->hccr, HCCR_RELEASE_RISC);
676
677 /* Workaround for QLA2312 PCI parity error */
678 if (IS_QLA2100(ha) || IS_QLA2200(ha) || IS_QLA2300(ha)) {
679 data = qla2x00_debounce_register(MAILBOX_REG(ha, reg, 0));
680 for (cnt = 6000000; cnt && (data == MBS_BUSY); cnt--) {
681 udelay(5);
682 data = RD_MAILBOX_REG(ha, reg, 0);
Andrew Vasquezfa2a1ce2005-07-06 10:32:07 -0700683 barrier();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 }
685 } else
686 udelay(10);
687
688 if (!cnt)
689 goto chip_diag_failed;
690
691 /* Check product ID of chip */
692 DEBUG3(printk("scsi(%ld): Checking product ID of chip\n", ha->host_no));
693
694 mb[1] = RD_MAILBOX_REG(ha, reg, 1);
695 mb[2] = RD_MAILBOX_REG(ha, reg, 2);
696 mb[3] = RD_MAILBOX_REG(ha, reg, 3);
697 mb[4] = qla2x00_debounce_register(MAILBOX_REG(ha, reg, 4));
698 if (mb[1] != PROD_ID_1 || (mb[2] != PROD_ID_2 && mb[2] != PROD_ID_2a) ||
699 mb[3] != PROD_ID_3) {
700 qla_printk(KERN_WARNING, ha,
701 "Wrong product ID = 0x%x,0x%x,0x%x\n", mb[1], mb[2], mb[3]);
702
703 goto chip_diag_failed;
704 }
705 ha->product_id[0] = mb[1];
706 ha->product_id[1] = mb[2];
707 ha->product_id[2] = mb[3];
708 ha->product_id[3] = mb[4];
709
710 /* Adjust fw RISC transfer size */
711 if (ha->request_q_length > 1024)
712 ha->fw_transfer_size = REQUEST_ENTRY_SIZE * 1024;
713 else
714 ha->fw_transfer_size = REQUEST_ENTRY_SIZE *
715 ha->request_q_length;
716
717 if (IS_QLA2200(ha) &&
718 RD_MAILBOX_REG(ha, reg, 7) == QLA2200A_RISC_ROM_VER) {
719 /* Limit firmware transfer size with a 2200A */
720 DEBUG3(printk("scsi(%ld): Found QLA2200A chip.\n",
721 ha->host_no));
722
andrew.vasquez@qlogic.comea5b6382006-03-09 14:27:08 -0800723 ha->device_type |= DT_ISP2200A;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 ha->fw_transfer_size = 128;
725 }
726
727 /* Wrap Incoming Mailboxes Test. */
728 spin_unlock_irqrestore(&ha->hardware_lock, flags);
729
730 DEBUG3(printk("scsi(%ld): Checking mailboxes.\n", ha->host_no));
731 rval = qla2x00_mbx_reg_test(ha);
732 if (rval) {
733 DEBUG(printk("scsi(%ld): Failed mailbox send register test\n",
734 ha->host_no));
735 qla_printk(KERN_WARNING, ha,
736 "Failed mailbox send register test\n");
737 }
738 else {
739 /* Flag a successful rval */
740 rval = QLA_SUCCESS;
741 }
742 spin_lock_irqsave(&ha->hardware_lock, flags);
743
744chip_diag_failed:
745 if (rval)
746 DEBUG2_3(printk("scsi(%ld): Chip diagnostics **** FAILED "
747 "****\n", ha->host_no));
748
749 spin_unlock_irqrestore(&ha->hardware_lock, flags);
750
751 return (rval);
752}
753
754/**
Andrew Vasquez01071092005-07-06 10:31:37 -0700755 * qla24xx_chip_diag() - Test ISP24xx for proper operation.
756 * @ha: HA context
757 *
758 * Returns 0 on success.
759 */
760int
761qla24xx_chip_diag(scsi_qla_host_t *ha)
762{
763 int rval;
Andrew Vasquez01071092005-07-06 10:31:37 -0700764
Andrew Vasquez88c26662005-07-08 17:59:26 -0700765 /* Perform RISC reset. */
766 qla24xx_reset_risc(ha);
Andrew Vasquez01071092005-07-06 10:31:37 -0700767
768 ha->fw_transfer_size = REQUEST_ENTRY_SIZE * 1024;
769
770 rval = qla2x00_mbx_reg_test(ha);
771 if (rval) {
772 DEBUG(printk("scsi(%ld): Failed mailbox send register test\n",
773 ha->host_no));
774 qla_printk(KERN_WARNING, ha,
775 "Failed mailbox send register test\n");
776 } else {
777 /* Flag a successful rval */
778 rval = QLA_SUCCESS;
779 }
780
781 return rval;
782}
783
Andrew Vasqueza7a167b2006-06-23 16:10:29 -0700784void
Andrew Vasquez01071092005-07-06 10:31:37 -0700785qla2x00_alloc_fw_dump(scsi_qla_host_t *ha)
786{
Andrew Vasqueza7a167b2006-06-23 16:10:29 -0700787 int rval;
788 uint32_t dump_size, fixed_size, mem_size, req_q_size, rsp_q_size,
789 eft_size;
790 dma_addr_t eft_dma;
791 void *eft;
Andrew Vasquezd4e3e042006-05-17 15:09:50 -0700792
Andrew Vasqueza7a167b2006-06-23 16:10:29 -0700793 if (ha->fw_dump) {
794 qla_printk(KERN_WARNING, ha,
795 "Firmware dump previously allocated.\n");
796 return;
Andrew Vasquezd4e3e042006-05-17 15:09:50 -0700797 }
798
Andrew Vasqueza7a167b2006-06-23 16:10:29 -0700799 ha->fw_dumped = 0;
800 fixed_size = mem_size = eft_size = 0;
801 if (IS_QLA2100(ha) || IS_QLA2200(ha)) {
802 fixed_size = sizeof(struct qla2100_fw_dump);
803 } else if (IS_QLA23XX(ha)) {
804 fixed_size = offsetof(struct qla2300_fw_dump, data_ram);
805 mem_size = (ha->fw_memory_size - 0x11000 + 1) *
806 sizeof(uint16_t);
807 } else if (IS_QLA24XX(ha) || IS_QLA54XX(ha)) {
808 fixed_size = offsetof(struct qla24xx_fw_dump, ext_mem);
809 mem_size = (ha->fw_memory_size - 0x100000 + 1) *
810 sizeof(uint32_t);
811
812 /* Allocate memory for Extended Trace Buffer. */
813 eft = dma_alloc_coherent(&ha->pdev->dev, EFT_SIZE, &eft_dma,
814 GFP_KERNEL);
815 if (!eft) {
816 qla_printk(KERN_WARNING, ha, "Unable to allocate "
817 "(%d KB) for EFT.\n", EFT_SIZE / 1024);
818 goto cont_alloc;
819 }
820
821 rval = qla2x00_trace_control(ha, TC_ENABLE, eft_dma,
822 EFT_NUM_BUFFERS);
823 if (rval) {
824 qla_printk(KERN_WARNING, ha, "Unable to initialize "
825 "EFT (%d).\n", rval);
826 dma_free_coherent(&ha->pdev->dev, EFT_SIZE, eft,
827 eft_dma);
828 goto cont_alloc;
829 }
830
831 qla_printk(KERN_INFO, ha, "Allocated (%d KB) for EFT...\n",
832 EFT_SIZE / 1024);
833
834 eft_size = EFT_SIZE;
835 memset(eft, 0, eft_size);
836 ha->eft_dma = eft_dma;
837 ha->eft = eft;
838 }
839cont_alloc:
840 req_q_size = ha->request_q_length * sizeof(request_t);
841 rsp_q_size = ha->response_q_length * sizeof(response_t);
842
843 dump_size = offsetof(struct qla2xxx_fw_dump, isp);
844 dump_size += fixed_size + mem_size + req_q_size + rsp_q_size +
845 eft_size;
846
Andrew Vasquezd4e3e042006-05-17 15:09:50 -0700847 ha->fw_dump = vmalloc(dump_size);
Andrew Vasqueza7a167b2006-06-23 16:10:29 -0700848 if (!ha->fw_dump) {
Andrew Vasquez01071092005-07-06 10:31:37 -0700849 qla_printk(KERN_WARNING, ha, "Unable to allocate (%d KB) for "
Andrew Vasquezd4e3e042006-05-17 15:09:50 -0700850 "firmware dump!!!\n", dump_size / 1024);
Andrew Vasqueza7a167b2006-06-23 16:10:29 -0700851
852 if (ha->eft) {
853 dma_free_coherent(&ha->pdev->dev, eft_size, ha->eft,
854 ha->eft_dma);
855 ha->eft = NULL;
856 ha->eft_dma = 0;
857 }
858 return;
859 }
860
861 qla_printk(KERN_INFO, ha, "Allocated (%d KB) for firmware dump...\n",
862 dump_size / 1024);
863
864 ha->fw_dump_len = dump_size;
865 ha->fw_dump->signature[0] = 'Q';
866 ha->fw_dump->signature[1] = 'L';
867 ha->fw_dump->signature[2] = 'G';
868 ha->fw_dump->signature[3] = 'C';
869 ha->fw_dump->version = __constant_htonl(1);
870
871 ha->fw_dump->fixed_size = htonl(fixed_size);
872 ha->fw_dump->mem_size = htonl(mem_size);
873 ha->fw_dump->req_q_size = htonl(req_q_size);
874 ha->fw_dump->rsp_q_size = htonl(rsp_q_size);
875
876 ha->fw_dump->eft_size = htonl(eft_size);
877 ha->fw_dump->eft_addr_l = htonl(LSD(ha->eft_dma));
878 ha->fw_dump->eft_addr_h = htonl(MSD(ha->eft_dma));
879
880 ha->fw_dump->header_size =
881 htonl(offsetof(struct qla2xxx_fw_dump, isp));
Andrew Vasquez01071092005-07-06 10:31:37 -0700882}
883
884/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885 * qla2x00_resize_request_q() - Resize request queue given available ISP memory.
886 * @ha: HA context
887 *
888 * Returns 0 on success.
889 */
890static void
891qla2x00_resize_request_q(scsi_qla_host_t *ha)
892{
893 int rval;
894 uint16_t fw_iocb_cnt = 0;
895 uint16_t request_q_length = REQUEST_ENTRY_CNT_2XXX_EXT_MEM;
896 dma_addr_t request_dma;
897 request_t *request_ring;
898
899 /* Valid only on recent ISPs. */
900 if (IS_QLA2100(ha) || IS_QLA2200(ha))
901 return;
902
903 /* Retrieve IOCB counts available to the firmware. */
904 rval = qla2x00_get_resource_cnts(ha, NULL, NULL, NULL, &fw_iocb_cnt);
905 if (rval)
906 return;
907 /* No point in continuing if current settings are sufficient. */
908 if (fw_iocb_cnt < 1024)
909 return;
910 if (ha->request_q_length >= request_q_length)
911 return;
912
913 /* Attempt to claim larger area for request queue. */
914 request_ring = dma_alloc_coherent(&ha->pdev->dev,
915 (request_q_length + 1) * sizeof(request_t), &request_dma,
916 GFP_KERNEL);
917 if (request_ring == NULL)
918 return;
919
920 /* Resize successful, report extensions. */
921 qla_printk(KERN_INFO, ha, "Extended memory detected (%d KB)...\n",
922 (ha->fw_memory_size + 1) / 1024);
923 qla_printk(KERN_INFO, ha, "Resizing request queue depth "
924 "(%d -> %d)...\n", ha->request_q_length, request_q_length);
925
926 /* Clear old allocations. */
927 dma_free_coherent(&ha->pdev->dev,
928 (ha->request_q_length + 1) * sizeof(request_t), ha->request_ring,
929 ha->request_dma);
930
931 /* Begin using larger queue. */
932 ha->request_q_length = request_q_length;
933 ha->request_ring = request_ring;
934 ha->request_dma = request_dma;
935}
936
937/**
938 * qla2x00_setup_chip() - Load and start RISC firmware.
939 * @ha: HA context
940 *
941 * Returns 0 on success.
942 */
943static int
944qla2x00_setup_chip(scsi_qla_host_t *ha)
945{
Andrew Vasquez01071092005-07-06 10:31:37 -0700946 int rval;
947 uint32_t srisc_address = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948
949 /* Load firmware sequences */
Andrew Vasquez01071092005-07-06 10:31:37 -0700950 rval = ha->isp_ops.load_risc(ha, &srisc_address);
951 if (rval == QLA_SUCCESS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 DEBUG(printk("scsi(%ld): Verifying Checksum of loaded RISC "
953 "code.\n", ha->host_no));
954
Andrew Vasquez01071092005-07-06 10:31:37 -0700955 rval = qla2x00_verify_checksum(ha, srisc_address);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 if (rval == QLA_SUCCESS) {
957 /* Start firmware execution. */
958 DEBUG(printk("scsi(%ld): Checksum OK, start "
959 "firmware.\n", ha->host_no));
960
Andrew Vasquez01071092005-07-06 10:31:37 -0700961 rval = qla2x00_execute_fw(ha, srisc_address);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962 /* Retrieve firmware information. */
963 if (rval == QLA_SUCCESS && ha->fw_major_version == 0) {
964 qla2x00_get_fw_version(ha,
965 &ha->fw_major_version,
966 &ha->fw_minor_version,
967 &ha->fw_subminor_version,
968 &ha->fw_attributes, &ha->fw_memory_size);
969 qla2x00_resize_request_q(ha);
Andrew Vasqueza7a167b2006-06-23 16:10:29 -0700970
971 if (ql2xallocfwdump)
972 qla2x00_alloc_fw_dump(ha);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973 }
974 } else {
975 DEBUG2(printk(KERN_INFO
976 "scsi(%ld): ISP Firmware failed checksum.\n",
977 ha->host_no));
978 }
979 }
980
981 if (rval) {
982 DEBUG2_3(printk("scsi(%ld): Setup chip **** FAILED ****.\n",
983 ha->host_no));
984 }
985
986 return (rval);
987}
988
989/**
990 * qla2x00_init_response_q_entries() - Initializes response queue entries.
991 * @ha: HA context
992 *
993 * Beginning of request ring has initialization control block already built
994 * by nvram config routine.
995 *
996 * Returns 0 on success.
997 */
998static void
999qla2x00_init_response_q_entries(scsi_qla_host_t *ha)
1000{
1001 uint16_t cnt;
1002 response_t *pkt;
1003
1004 pkt = ha->response_ring_ptr;
1005 for (cnt = 0; cnt < ha->response_q_length; cnt++) {
1006 pkt->signature = RESPONSE_PROCESSED;
1007 pkt++;
1008 }
1009
1010}
1011
1012/**
1013 * qla2x00_update_fw_options() - Read and process firmware options.
1014 * @ha: HA context
1015 *
1016 * Returns 0 on success.
1017 */
Andrew Vasquezabbd8872005-07-06 10:30:05 -07001018void
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019qla2x00_update_fw_options(scsi_qla_host_t *ha)
1020{
1021 uint16_t swing, emphasis, tx_sens, rx_sens;
1022
1023 memset(ha->fw_options, 0, sizeof(ha->fw_options));
1024 qla2x00_get_fw_options(ha, ha->fw_options);
1025
1026 if (IS_QLA2100(ha) || IS_QLA2200(ha))
1027 return;
1028
1029 /* Serial Link options. */
1030 DEBUG3(printk("scsi(%ld): Serial link options:\n",
1031 ha->host_no));
1032 DEBUG3(qla2x00_dump_buffer((uint8_t *)&ha->fw_seriallink_options,
1033 sizeof(ha->fw_seriallink_options)));
1034
1035 ha->fw_options[1] &= ~FO1_SET_EMPHASIS_SWING;
1036 if (ha->fw_seriallink_options[3] & BIT_2) {
1037 ha->fw_options[1] |= FO1_SET_EMPHASIS_SWING;
1038
1039 /* 1G settings */
1040 swing = ha->fw_seriallink_options[2] & (BIT_2 | BIT_1 | BIT_0);
1041 emphasis = (ha->fw_seriallink_options[2] &
1042 (BIT_4 | BIT_3)) >> 3;
1043 tx_sens = ha->fw_seriallink_options[0] &
Andrew Vasquezfa2a1ce2005-07-06 10:32:07 -07001044 (BIT_3 | BIT_2 | BIT_1 | BIT_0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045 rx_sens = (ha->fw_seriallink_options[0] &
1046 (BIT_7 | BIT_6 | BIT_5 | BIT_4)) >> 4;
1047 ha->fw_options[10] = (emphasis << 14) | (swing << 8);
1048 if (IS_QLA2300(ha) || IS_QLA2312(ha) || IS_QLA6312(ha)) {
1049 if (rx_sens == 0x0)
1050 rx_sens = 0x3;
1051 ha->fw_options[10] |= (tx_sens << 4) | rx_sens;
1052 } else if (IS_QLA2322(ha) || IS_QLA6322(ha))
1053 ha->fw_options[10] |= BIT_5 |
1054 ((rx_sens & (BIT_1 | BIT_0)) << 2) |
1055 (tx_sens & (BIT_1 | BIT_0));
1056
1057 /* 2G settings */
1058 swing = (ha->fw_seriallink_options[2] &
1059 (BIT_7 | BIT_6 | BIT_5)) >> 5;
1060 emphasis = ha->fw_seriallink_options[3] & (BIT_1 | BIT_0);
1061 tx_sens = ha->fw_seriallink_options[1] &
Andrew Vasquezfa2a1ce2005-07-06 10:32:07 -07001062 (BIT_3 | BIT_2 | BIT_1 | BIT_0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063 rx_sens = (ha->fw_seriallink_options[1] &
1064 (BIT_7 | BIT_6 | BIT_5 | BIT_4)) >> 4;
1065 ha->fw_options[11] = (emphasis << 14) | (swing << 8);
1066 if (IS_QLA2300(ha) || IS_QLA2312(ha) || IS_QLA6312(ha)) {
1067 if (rx_sens == 0x0)
1068 rx_sens = 0x3;
1069 ha->fw_options[11] |= (tx_sens << 4) | rx_sens;
1070 } else if (IS_QLA2322(ha) || IS_QLA6322(ha))
1071 ha->fw_options[11] |= BIT_5 |
1072 ((rx_sens & (BIT_1 | BIT_0)) << 2) |
1073 (tx_sens & (BIT_1 | BIT_0));
1074 }
1075
1076 /* FCP2 options. */
1077 /* Return command IOCBs without waiting for an ABTS to complete. */
1078 ha->fw_options[3] |= BIT_13;
1079
1080 /* LED scheme. */
1081 if (ha->flags.enable_led_scheme)
1082 ha->fw_options[2] |= BIT_12;
1083
andrew.vasquez@qlogic.com48c02fd2006-03-09 14:27:18 -08001084 /* Detect ISP6312. */
1085 if (IS_QLA6312(ha))
1086 ha->fw_options[2] |= BIT_13;
1087
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088 /* Update firmware options. */
1089 qla2x00_set_fw_options(ha, ha->fw_options);
1090}
1091
Andrew Vasquezabbd8872005-07-06 10:30:05 -07001092void
Andrew Vasquez01071092005-07-06 10:31:37 -07001093qla24xx_update_fw_options(scsi_qla_host_t *ha)
1094{
1095 int rval;
1096
1097 /* Update Serial Link options. */
andrew.vasquez@qlogic.comf94097e2006-01-13 17:05:32 -08001098 if ((le16_to_cpu(ha->fw_seriallink_options24[0]) & BIT_0) == 0)
Andrew Vasquez01071092005-07-06 10:31:37 -07001099 return;
1100
andrew.vasquez@qlogic.comf94097e2006-01-13 17:05:32 -08001101 rval = qla2x00_set_serdes_params(ha,
1102 le16_to_cpu(ha->fw_seriallink_options24[1]),
1103 le16_to_cpu(ha->fw_seriallink_options24[2]),
1104 le16_to_cpu(ha->fw_seriallink_options24[3]));
Andrew Vasquez01071092005-07-06 10:31:37 -07001105 if (rval != QLA_SUCCESS) {
1106 qla_printk(KERN_WARNING, ha,
1107 "Unable to update Serial Link options (%x).\n", rval);
1108 }
1109}
1110
1111void
Andrew Vasquezabbd8872005-07-06 10:30:05 -07001112qla2x00_config_rings(struct scsi_qla_host *ha)
1113{
Andrew Vasquez3d716442005-07-06 10:30:26 -07001114 struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
Andrew Vasquezabbd8872005-07-06 10:30:05 -07001115
1116 /* Setup ring parameters in initialization control block. */
1117 ha->init_cb->request_q_outpointer = __constant_cpu_to_le16(0);
1118 ha->init_cb->response_q_inpointer = __constant_cpu_to_le16(0);
1119 ha->init_cb->request_q_length = cpu_to_le16(ha->request_q_length);
1120 ha->init_cb->response_q_length = cpu_to_le16(ha->response_q_length);
1121 ha->init_cb->request_q_address[0] = cpu_to_le32(LSD(ha->request_dma));
1122 ha->init_cb->request_q_address[1] = cpu_to_le32(MSD(ha->request_dma));
1123 ha->init_cb->response_q_address[0] = cpu_to_le32(LSD(ha->response_dma));
1124 ha->init_cb->response_q_address[1] = cpu_to_le32(MSD(ha->response_dma));
1125
1126 WRT_REG_WORD(ISP_REQ_Q_IN(ha, reg), 0);
1127 WRT_REG_WORD(ISP_REQ_Q_OUT(ha, reg), 0);
1128 WRT_REG_WORD(ISP_RSP_Q_IN(ha, reg), 0);
1129 WRT_REG_WORD(ISP_RSP_Q_OUT(ha, reg), 0);
1130 RD_REG_WORD(ISP_RSP_Q_OUT(ha, reg)); /* PCI Posting. */
1131}
1132
Andrew Vasquez01071092005-07-06 10:31:37 -07001133void
1134qla24xx_config_rings(struct scsi_qla_host *ha)
1135{
1136 struct device_reg_24xx __iomem *reg = &ha->iobase->isp24;
1137 struct init_cb_24xx *icb;
1138
1139 /* Setup ring parameters in initialization control block. */
1140 icb = (struct init_cb_24xx *)ha->init_cb;
1141 icb->request_q_outpointer = __constant_cpu_to_le16(0);
1142 icb->response_q_inpointer = __constant_cpu_to_le16(0);
1143 icb->request_q_length = cpu_to_le16(ha->request_q_length);
1144 icb->response_q_length = cpu_to_le16(ha->response_q_length);
1145 icb->request_q_address[0] = cpu_to_le32(LSD(ha->request_dma));
1146 icb->request_q_address[1] = cpu_to_le32(MSD(ha->request_dma));
1147 icb->response_q_address[0] = cpu_to_le32(LSD(ha->response_dma));
1148 icb->response_q_address[1] = cpu_to_le32(MSD(ha->response_dma));
1149
1150 WRT_REG_DWORD(&reg->req_q_in, 0);
1151 WRT_REG_DWORD(&reg->req_q_out, 0);
1152 WRT_REG_DWORD(&reg->rsp_q_in, 0);
1153 WRT_REG_DWORD(&reg->rsp_q_out, 0);
1154 RD_REG_DWORD(&reg->rsp_q_out);
1155}
1156
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157/**
1158 * qla2x00_init_rings() - Initializes firmware.
1159 * @ha: HA context
1160 *
1161 * Beginning of request ring has initialization control block already built
1162 * by nvram config routine.
1163 *
1164 * Returns 0 on success.
1165 */
1166static int
1167qla2x00_init_rings(scsi_qla_host_t *ha)
1168{
1169 int rval;
1170 unsigned long flags = 0;
1171 int cnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172
1173 spin_lock_irqsave(&ha->hardware_lock, flags);
1174
1175 /* Clear outstanding commands array. */
1176 for (cnt = 0; cnt < MAX_OUTSTANDING_COMMANDS; cnt++)
1177 ha->outstanding_cmds[cnt] = NULL;
1178
1179 ha->current_outstanding_cmd = 0;
1180
1181 /* Clear RSCN queue. */
1182 ha->rscn_in_ptr = 0;
1183 ha->rscn_out_ptr = 0;
1184
1185 /* Initialize firmware. */
1186 ha->request_ring_ptr = ha->request_ring;
1187 ha->req_ring_index = 0;
1188 ha->req_q_cnt = ha->request_q_length;
1189 ha->response_ring_ptr = ha->response_ring;
1190 ha->rsp_ring_index = 0;
1191
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192 /* Initialize response queue entries */
1193 qla2x00_init_response_q_entries(ha);
1194
Andrew Vasquezabbd8872005-07-06 10:30:05 -07001195 ha->isp_ops.config_rings(ha);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196
1197 spin_unlock_irqrestore(&ha->hardware_lock, flags);
1198
1199 /* Update any ISP specific firmware options before initialization. */
Andrew Vasquezabbd8872005-07-06 10:30:05 -07001200 ha->isp_ops.update_fw_options(ha);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201
1202 DEBUG(printk("scsi(%ld): Issue init firmware.\n", ha->host_no));
Andrew Vasquez01071092005-07-06 10:31:37 -07001203 rval = qla2x00_init_firmware(ha, ha->init_cb_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204 if (rval) {
1205 DEBUG2_3(printk("scsi(%ld): Init firmware **** FAILED ****.\n",
1206 ha->host_no));
1207 } else {
1208 DEBUG3(printk("scsi(%ld): Init firmware -- success.\n",
1209 ha->host_no));
1210 }
1211
1212 return (rval);
1213}
1214
1215/**
1216 * qla2x00_fw_ready() - Waits for firmware ready.
1217 * @ha: HA context
1218 *
1219 * Returns 0 on success.
1220 */
1221static int
1222qla2x00_fw_ready(scsi_qla_host_t *ha)
1223{
1224 int rval;
1225 unsigned long wtime, mtime;
1226 uint16_t min_wait; /* Minimum wait time if loop is down */
1227 uint16_t wait_time; /* Wait time if loop is coming ready */
1228 uint16_t fw_state;
1229
1230 rval = QLA_SUCCESS;
1231
1232 /* 20 seconds for loop down. */
Andrew Vasquezfa2a1ce2005-07-06 10:32:07 -07001233 min_wait = 20;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234
1235 /*
1236 * Firmware should take at most one RATOV to login, plus 5 seconds for
1237 * our own processing.
1238 */
1239 if ((wait_time = (ha->retry_count*ha->login_timeout) + 5) < min_wait) {
1240 wait_time = min_wait;
1241 }
1242
1243 /* Min wait time if loop down */
1244 mtime = jiffies + (min_wait * HZ);
1245
1246 /* wait time before firmware ready */
1247 wtime = jiffies + (wait_time * HZ);
1248
1249 /* Wait for ISP to finish LIP */
1250 if (!ha->flags.init_done)
1251 qla_printk(KERN_INFO, ha, "Waiting for LIP to complete...\n");
1252
1253 DEBUG3(printk("scsi(%ld): Waiting for LIP to complete...\n",
1254 ha->host_no));
1255
1256 do {
1257 rval = qla2x00_get_firmware_state(ha, &fw_state);
1258 if (rval == QLA_SUCCESS) {
1259 if (fw_state < FSTATE_LOSS_OF_SYNC) {
1260 ha->device_flags &= ~DFLG_NO_CABLE;
1261 }
1262 if (fw_state == FSTATE_READY) {
1263 DEBUG(printk("scsi(%ld): F/W Ready - OK \n",
1264 ha->host_no));
1265
1266 qla2x00_get_retry_cnt(ha, &ha->retry_count,
1267 &ha->login_timeout, &ha->r_a_tov);
1268
1269 rval = QLA_SUCCESS;
1270 break;
1271 }
1272
1273 rval = QLA_FUNCTION_FAILED;
1274
1275 if (atomic_read(&ha->loop_down_timer) &&
Andrew Vasquez7d7abc72006-06-23 16:11:17 -07001276 fw_state != FSTATE_READY) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277 /* Loop down. Timeout on min_wait for states
Andrew Vasquezfa2a1ce2005-07-06 10:32:07 -07001278 * other than Wait for Login.
1279 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280 if (time_after_eq(jiffies, mtime)) {
1281 qla_printk(KERN_INFO, ha,
1282 "Cable is unplugged...\n");
1283
1284 ha->device_flags |= DFLG_NO_CABLE;
1285 break;
1286 }
1287 }
1288 } else {
1289 /* Mailbox cmd failed. Timeout on min_wait. */
1290 if (time_after_eq(jiffies, mtime))
1291 break;
1292 }
1293
1294 if (time_after_eq(jiffies, wtime))
1295 break;
1296
1297 /* Delay for a while */
1298 msleep(500);
1299
1300 DEBUG3(printk("scsi(%ld): fw_state=%x curr time=%lx.\n",
1301 ha->host_no, fw_state, jiffies));
1302 } while (1);
1303
1304 DEBUG(printk("scsi(%ld): fw_state=%x curr time=%lx.\n",
1305 ha->host_no, fw_state, jiffies));
1306
1307 if (rval) {
1308 DEBUG2_3(printk("scsi(%ld): Firmware ready **** FAILED ****.\n",
1309 ha->host_no));
1310 }
1311
1312 return (rval);
1313}
1314
1315/*
1316* qla2x00_configure_hba
1317* Setup adapter context.
1318*
1319* Input:
1320* ha = adapter state pointer.
1321*
1322* Returns:
1323* 0 = success
1324*
1325* Context:
1326* Kernel context.
1327*/
1328static int
1329qla2x00_configure_hba(scsi_qla_host_t *ha)
1330{
1331 int rval;
1332 uint16_t loop_id;
1333 uint16_t topo;
1334 uint8_t al_pa;
1335 uint8_t area;
1336 uint8_t domain;
1337 char connect_type[22];
1338
1339 /* Get host addresses. */
1340 rval = qla2x00_get_adapter_id(ha,
1341 &loop_id, &al_pa, &area, &domain, &topo);
1342 if (rval != QLA_SUCCESS) {
Andrew Vasquez23443b12005-12-06 10:57:06 -08001343 if (LOOP_TRANSITION(ha) || atomic_read(&ha->loop_down_timer) ||
Ravi Anand33135aa2005-11-08 14:37:20 -08001344 (rval == QLA_COMMAND_ERROR && loop_id == 0x7)) {
1345 DEBUG2(printk("%s(%ld) Loop is in a transition state\n",
1346 __func__, ha->host_no));
1347 } else {
1348 qla_printk(KERN_WARNING, ha,
1349 "ERROR -- Unable to get host loop ID.\n");
1350 set_bit(ISP_ABORT_NEEDED, &ha->dpc_flags);
1351 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352 return (rval);
1353 }
1354
1355 if (topo == 4) {
1356 qla_printk(KERN_INFO, ha,
1357 "Cannot get topology - retrying.\n");
1358 return (QLA_FUNCTION_FAILED);
1359 }
1360
1361 ha->loop_id = loop_id;
1362
1363 /* initialize */
1364 ha->min_external_loopid = SNS_FIRST_LOOP_ID;
1365 ha->operating_mode = LOOP;
1366
1367 switch (topo) {
1368 case 0:
1369 DEBUG3(printk("scsi(%ld): HBA in NL topology.\n",
1370 ha->host_no));
1371 ha->current_topology = ISP_CFG_NL;
1372 strcpy(connect_type, "(Loop)");
1373 break;
1374
1375 case 1:
1376 DEBUG3(printk("scsi(%ld): HBA in FL topology.\n",
1377 ha->host_no));
1378 ha->current_topology = ISP_CFG_FL;
1379 strcpy(connect_type, "(FL_Port)");
1380 break;
1381
1382 case 2:
1383 DEBUG3(printk("scsi(%ld): HBA in N P2P topology.\n",
1384 ha->host_no));
1385 ha->operating_mode = P2P;
1386 ha->current_topology = ISP_CFG_N;
1387 strcpy(connect_type, "(N_Port-to-N_Port)");
1388 break;
1389
1390 case 3:
1391 DEBUG3(printk("scsi(%ld): HBA in F P2P topology.\n",
1392 ha->host_no));
1393 ha->operating_mode = P2P;
1394 ha->current_topology = ISP_CFG_F;
1395 strcpy(connect_type, "(F_Port)");
1396 break;
1397
1398 default:
1399 DEBUG3(printk("scsi(%ld): HBA in unknown topology %x. "
1400 "Using NL.\n",
1401 ha->host_no, topo));
1402 ha->current_topology = ISP_CFG_NL;
1403 strcpy(connect_type, "(Loop)");
1404 break;
1405 }
1406
1407 /* Save Host port and loop ID. */
1408 /* byte order - Big Endian */
1409 ha->d_id.b.domain = domain;
1410 ha->d_id.b.area = area;
1411 ha->d_id.b.al_pa = al_pa;
1412
1413 if (!ha->flags.init_done)
1414 qla_printk(KERN_INFO, ha,
1415 "Topology - %s, Host Loop address 0x%x\n",
1416 connect_type, ha->loop_id);
1417
1418 if (rval) {
1419 DEBUG2_3(printk("scsi(%ld): FAILED.\n", ha->host_no));
1420 } else {
1421 DEBUG3(printk("scsi(%ld): exiting normally.\n", ha->host_no));
1422 }
1423
1424 return(rval);
1425}
1426
1427/*
1428* NVRAM configuration for ISP 2xxx
1429*
1430* Input:
1431* ha = adapter block pointer.
1432*
1433* Output:
1434* initialization control block in response_ring
1435* host adapters parameters in host adapter block
1436*
1437* Returns:
1438* 0 = success.
1439*/
Andrew Vasquezabbd8872005-07-06 10:30:05 -07001440int
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441qla2x00_nvram_config(scsi_qla_host_t *ha)
1442{
Andrew Vasquez01071092005-07-06 10:31:37 -07001443 int rval;
1444 uint8_t chksum = 0;
1445 uint16_t cnt;
1446 uint8_t *dptr1, *dptr2;
1447 init_cb_t *icb = ha->init_cb;
1448 nvram_t *nv = (nvram_t *)ha->request_ring;
1449 uint8_t *ptr = (uint8_t *)ha->request_ring;
Andrew Vasquez3d716442005-07-06 10:30:26 -07001450 struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451
1452 rval = QLA_SUCCESS;
1453
1454 /* Determine NVRAM starting address. */
Andrew Vasquez01071092005-07-06 10:31:37 -07001455 ha->nvram_size = sizeof(nvram_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456 ha->nvram_base = 0;
1457 if (!IS_QLA2100(ha) && !IS_QLA2200(ha) && !IS_QLA2300(ha))
1458 if ((RD_REG_WORD(&reg->ctrl_status) >> 14) == 1)
1459 ha->nvram_base = 0x80;
1460
1461 /* Get NVRAM data and calculate checksum. */
Andrew Vasquez01071092005-07-06 10:31:37 -07001462 ha->isp_ops.read_nvram(ha, ptr, ha->nvram_base, ha->nvram_size);
1463 for (cnt = 0, chksum = 0; cnt < ha->nvram_size; cnt++)
1464 chksum += *ptr++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465
1466 DEBUG5(printk("scsi(%ld): Contents of NVRAM\n", ha->host_no));
1467 DEBUG5(qla2x00_dump_buffer((uint8_t *)ha->request_ring,
Andrew Vasquez01071092005-07-06 10:31:37 -07001468 ha->nvram_size));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469
1470 /* Bad NVRAM data, set defaults parameters. */
1471 if (chksum || nv->id[0] != 'I' || nv->id[1] != 'S' ||
1472 nv->id[2] != 'P' || nv->id[3] != ' ' || nv->nvram_version < 1) {
1473 /* Reset NVRAM data. */
1474 qla_printk(KERN_WARNING, ha, "Inconsistent NVRAM detected: "
1475 "checksum=0x%x id=%c version=0x%x.\n", chksum, nv->id[0],
1476 nv->nvram_version);
1477 qla_printk(KERN_WARNING, ha, "Falling back to functioning (yet "
1478 "invalid -- WWPN) defaults.\n");
1479
1480 /*
1481 * Set default initialization control block.
1482 */
Andrew Vasquez01071092005-07-06 10:31:37 -07001483 memset(nv, 0, ha->nvram_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484 nv->parameter_block_version = ICB_VERSION;
1485
1486 if (IS_QLA23XX(ha)) {
1487 nv->firmware_options[0] = BIT_2 | BIT_1;
1488 nv->firmware_options[1] = BIT_7 | BIT_5;
1489 nv->add_firmware_options[0] = BIT_5;
1490 nv->add_firmware_options[1] = BIT_5 | BIT_4;
1491 nv->frame_payload_size = __constant_cpu_to_le16(2048);
1492 nv->special_options[1] = BIT_7;
1493 } else if (IS_QLA2200(ha)) {
1494 nv->firmware_options[0] = BIT_2 | BIT_1;
1495 nv->firmware_options[1] = BIT_7 | BIT_5;
1496 nv->add_firmware_options[0] = BIT_5;
1497 nv->add_firmware_options[1] = BIT_5 | BIT_4;
1498 nv->frame_payload_size = __constant_cpu_to_le16(1024);
1499 } else if (IS_QLA2100(ha)) {
1500 nv->firmware_options[0] = BIT_3 | BIT_1;
1501 nv->firmware_options[1] = BIT_5;
1502 nv->frame_payload_size = __constant_cpu_to_le16(1024);
1503 }
1504
1505 nv->max_iocb_allocation = __constant_cpu_to_le16(256);
1506 nv->execution_throttle = __constant_cpu_to_le16(16);
1507 nv->retry_count = 8;
1508 nv->retry_delay = 1;
1509
1510 nv->port_name[0] = 33;
1511 nv->port_name[3] = 224;
1512 nv->port_name[4] = 139;
1513
1514 nv->login_timeout = 4;
1515
1516 /*
1517 * Set default host adapter parameters
1518 */
1519 nv->host_p[1] = BIT_2;
1520 nv->reset_delay = 5;
1521 nv->port_down_retry_count = 8;
1522 nv->max_luns_per_target = __constant_cpu_to_le16(8);
1523 nv->link_down_timeout = 60;
1524
1525 rval = 1;
1526 }
1527
1528#if defined(CONFIG_IA64_GENERIC) || defined(CONFIG_IA64_SGI_SN2)
1529 /*
1530 * The SN2 does not provide BIOS emulation which means you can't change
1531 * potentially bogus BIOS settings. Force the use of default settings
1532 * for link rate and frame size. Hope that the rest of the settings
1533 * are valid.
1534 */
1535 if (ia64_platform_is("sn2")) {
1536 nv->frame_payload_size = __constant_cpu_to_le16(2048);
1537 if (IS_QLA23XX(ha))
1538 nv->special_options[1] = BIT_7;
1539 }
1540#endif
1541
1542 /* Reset Initialization control block */
Andrew Vasquez01071092005-07-06 10:31:37 -07001543 memset(icb, 0, ha->init_cb_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544
1545 /*
1546 * Setup driver NVRAM options.
1547 */
1548 nv->firmware_options[0] |= (BIT_6 | BIT_1);
1549 nv->firmware_options[0] &= ~(BIT_5 | BIT_4);
1550 nv->firmware_options[1] |= (BIT_5 | BIT_0);
1551 nv->firmware_options[1] &= ~BIT_4;
1552
1553 if (IS_QLA23XX(ha)) {
1554 nv->firmware_options[0] |= BIT_2;
1555 nv->firmware_options[0] &= ~BIT_3;
Andrew Vasquez01071092005-07-06 10:31:37 -07001556 nv->add_firmware_options[1] |= BIT_5 | BIT_4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557
1558 if (IS_QLA2300(ha)) {
1559 if (ha->fb_rev == FPM_2310) {
1560 strcpy(ha->model_number, "QLA2310");
1561 } else {
1562 strcpy(ha->model_number, "QLA2300");
1563 }
1564 } else {
1565 if (rval == 0 &&
1566 memcmp(nv->model_number, BINZERO,
1567 sizeof(nv->model_number)) != 0) {
1568 char *st, *en;
1569
1570 strncpy(ha->model_number, nv->model_number,
1571 sizeof(nv->model_number));
1572 st = en = ha->model_number;
1573 en += sizeof(nv->model_number) - 1;
1574 while (en > st) {
1575 if (*en != 0x20 && *en != 0x00)
1576 break;
1577 *en-- = '\0';
1578 }
1579 } else {
1580 uint16_t index;
1581
1582 index = (ha->pdev->subsystem_device & 0xff);
1583 if (index < QLA_MODEL_NAMES) {
1584 strcpy(ha->model_number,
andrew.vasquez@qlogic.com13289622006-03-09 14:27:24 -08001585 qla2x00_model_name[index * 2]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586 ha->model_desc =
andrew.vasquez@qlogic.com13289622006-03-09 14:27:24 -08001587 qla2x00_model_name[index * 2 + 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588 } else {
1589 strcpy(ha->model_number, "QLA23xx");
1590 }
1591 }
1592 }
1593 } else if (IS_QLA2200(ha)) {
1594 nv->firmware_options[0] |= BIT_2;
1595 /*
1596 * 'Point-to-point preferred, else loop' is not a safe
1597 * connection mode setting.
1598 */
1599 if ((nv->add_firmware_options[0] & (BIT_6 | BIT_5 | BIT_4)) ==
1600 (BIT_5 | BIT_4)) {
1601 /* Force 'loop preferred, else point-to-point'. */
1602 nv->add_firmware_options[0] &= ~(BIT_6 | BIT_5 | BIT_4);
1603 nv->add_firmware_options[0] |= BIT_5;
1604 }
1605 strcpy(ha->model_number, "QLA22xx");
1606 } else /*if (IS_QLA2100(ha))*/ {
1607 strcpy(ha->model_number, "QLA2100");
1608 }
1609
1610 /*
1611 * Copy over NVRAM RISC parameter block to initialization control block.
1612 */
1613 dptr1 = (uint8_t *)icb;
1614 dptr2 = (uint8_t *)&nv->parameter_block_version;
1615 cnt = (uint8_t *)&icb->request_q_outpointer - (uint8_t *)&icb->version;
1616 while (cnt--)
1617 *dptr1++ = *dptr2++;
1618
1619 /* Copy 2nd half. */
1620 dptr1 = (uint8_t *)icb->add_firmware_options;
1621 cnt = (uint8_t *)icb->reserved_3 - (uint8_t *)icb->add_firmware_options;
1622 while (cnt--)
1623 *dptr1++ = *dptr2++;
1624
Andrew Vasquez5341e862006-05-17 15:09:16 -07001625 /* Use alternate WWN? */
1626 if (nv->host_p[1] & BIT_7) {
1627 memcpy(icb->node_name, nv->alternate_node_name, WWN_SIZE);
1628 memcpy(icb->port_name, nv->alternate_port_name, WWN_SIZE);
1629 }
1630
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631 /* Prepare nodename */
1632 if ((icb->firmware_options[1] & BIT_6) == 0) {
1633 /*
1634 * Firmware will apply the following mask if the nodename was
1635 * not provided.
1636 */
1637 memcpy(icb->node_name, icb->port_name, WWN_SIZE);
1638 icb->node_name[0] &= 0xF0;
1639 }
1640
1641 /*
1642 * Set host adapter parameters.
1643 */
Andrew Vasquez01819442006-06-23 16:11:10 -07001644 if (nv->host_p[0] & BIT_7)
1645 extended_error_logging = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646 ha->flags.disable_risc_code_load = ((nv->host_p[0] & BIT_4) ? 1 : 0);
1647 /* Always load RISC code on non ISP2[12]00 chips. */
1648 if (!IS_QLA2100(ha) && !IS_QLA2200(ha))
1649 ha->flags.disable_risc_code_load = 0;
1650 ha->flags.enable_lip_reset = ((nv->host_p[1] & BIT_1) ? 1 : 0);
1651 ha->flags.enable_lip_full_login = ((nv->host_p[1] & BIT_2) ? 1 : 0);
1652 ha->flags.enable_target_reset = ((nv->host_p[1] & BIT_3) ? 1 : 0);
Andrew Vasquez06c22bd2005-08-26 19:09:00 -07001653 ha->flags.enable_led_scheme = (nv->special_options[1] & BIT_4) ? 1 : 0;
Andrew Vasquezd4c760c2006-06-23 16:10:39 -07001654 ha->flags.disable_serdes = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655
1656 ha->operating_mode =
1657 (icb->add_firmware_options[0] & (BIT_6 | BIT_5 | BIT_4)) >> 4;
1658
1659 memcpy(ha->fw_seriallink_options, nv->seriallink_options,
1660 sizeof(ha->fw_seriallink_options));
1661
1662 /* save HBA serial number */
1663 ha->serial0 = icb->port_name[5];
1664 ha->serial1 = icb->port_name[6];
1665 ha->serial2 = icb->port_name[7];
Andrew Vasquez3d716442005-07-06 10:30:26 -07001666 ha->node_name = icb->node_name;
1667 ha->port_name = icb->port_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668
1669 icb->execution_throttle = __constant_cpu_to_le16(0xFFFF);
1670
1671 ha->retry_count = nv->retry_count;
1672
1673 /* Set minimum login_timeout to 4 seconds. */
1674 if (nv->login_timeout < ql2xlogintimeout)
1675 nv->login_timeout = ql2xlogintimeout;
1676 if (nv->login_timeout < 4)
1677 nv->login_timeout = 4;
1678 ha->login_timeout = nv->login_timeout;
1679 icb->login_timeout = nv->login_timeout;
1680
1681 /* Set minimum RATOV to 200 tenths of a second. */
1682 ha->r_a_tov = 200;
1683
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684 ha->loop_reset_delay = nv->reset_delay;
1685
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686 /* Link Down Timeout = 0:
1687 *
1688 * When Port Down timer expires we will start returning
1689 * I/O's to OS with "DID_NO_CONNECT".
1690 *
1691 * Link Down Timeout != 0:
1692 *
1693 * The driver waits for the link to come up after link down
1694 * before returning I/Os to OS with "DID_NO_CONNECT".
Andrew Vasquezfa2a1ce2005-07-06 10:32:07 -07001695 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696 if (nv->link_down_timeout == 0) {
1697 ha->loop_down_abort_time =
Andrew Vasquez 354d6b22005-04-23 02:47:27 -04001698 (LOOP_DOWN_TIME - LOOP_DOWN_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699 } else {
1700 ha->link_down_timeout = nv->link_down_timeout;
1701 ha->loop_down_abort_time =
1702 (LOOP_DOWN_TIME - ha->link_down_timeout);
Andrew Vasquezfa2a1ce2005-07-06 10:32:07 -07001703 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705 /*
1706 * Need enough time to try and get the port back.
1707 */
1708 ha->port_down_retry_count = nv->port_down_retry_count;
1709 if (qlport_down_retry)
1710 ha->port_down_retry_count = qlport_down_retry;
1711 /* Set login_retry_count */
1712 ha->login_retry_count = nv->retry_count;
1713 if (ha->port_down_retry_count == nv->port_down_retry_count &&
1714 ha->port_down_retry_count > 3)
1715 ha->login_retry_count = ha->port_down_retry_count;
1716 else if (ha->port_down_retry_count > (int)ha->login_retry_count)
1717 ha->login_retry_count = ha->port_down_retry_count;
1718 if (ql2xloginretrycount)
1719 ha->login_retry_count = ql2xloginretrycount;
1720
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721 icb->lun_enables = __constant_cpu_to_le16(0);
1722 icb->command_resource_count = 0;
1723 icb->immediate_notify_resource_count = 0;
1724 icb->timeout = __constant_cpu_to_le16(0);
1725
1726 if (IS_QLA2100(ha) || IS_QLA2200(ha)) {
1727 /* Enable RIO */
1728 icb->firmware_options[0] &= ~BIT_3;
1729 icb->add_firmware_options[0] &=
1730 ~(BIT_3 | BIT_2 | BIT_1 | BIT_0);
1731 icb->add_firmware_options[0] |= BIT_2;
1732 icb->response_accumulation_timer = 3;
1733 icb->interrupt_delay_timer = 5;
1734
1735 ha->flags.process_response_queue = 1;
1736 } else {
Andrew Vasquez4fdfefe2005-10-27 11:09:48 -07001737 /* Enable ZIO. */
1738 if (!ha->flags.init_done) {
1739 ha->zio_mode = icb->add_firmware_options[0] &
1740 (BIT_3 | BIT_2 | BIT_1 | BIT_0);
1741 ha->zio_timer = icb->interrupt_delay_timer ?
1742 icb->interrupt_delay_timer: 2;
1743 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744 icb->add_firmware_options[0] &=
1745 ~(BIT_3 | BIT_2 | BIT_1 | BIT_0);
Andrew Vasquez4fdfefe2005-10-27 11:09:48 -07001746 ha->flags.process_response_queue = 0;
1747 if (ha->zio_mode != QLA_ZIO_DISABLED) {
andrew.vasquez@qlogic.com4a59f712006-03-09 14:27:39 -08001748 ha->zio_mode = QLA_ZIO_MODE_6;
1749
Andrew Vasquez4fdfefe2005-10-27 11:09:48 -07001750 DEBUG2(printk("scsi(%ld): ZIO mode %d enabled; timer "
1751 "delay (%d us).\n", ha->host_no, ha->zio_mode,
1752 ha->zio_timer * 100));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753 qla_printk(KERN_INFO, ha,
Andrew Vasquez4fdfefe2005-10-27 11:09:48 -07001754 "ZIO mode %d enabled; timer delay (%d us).\n",
1755 ha->zio_mode, ha->zio_timer * 100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756
Andrew Vasquez4fdfefe2005-10-27 11:09:48 -07001757 icb->add_firmware_options[0] |= (uint8_t)ha->zio_mode;
1758 icb->interrupt_delay_timer = (uint8_t)ha->zio_timer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759 ha->flags.process_response_queue = 1;
1760 }
1761 }
1762
1763 if (rval) {
1764 DEBUG2_3(printk(KERN_WARNING
1765 "scsi(%ld): NVRAM configuration failed!\n", ha->host_no));
1766 }
1767 return (rval);
1768}
1769
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04001770static void
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04001771qla2x00_rport_del(void *data)
1772{
1773 fc_port_t *fcport = data;
andrew.vasquez@qlogic.comd97994d2006-01-20 14:53:13 -08001774 struct fc_rport *rport;
1775 unsigned long flags;
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04001776
andrew.vasquez@qlogic.comd97994d2006-01-20 14:53:13 -08001777 spin_lock_irqsave(&fcport->rport_lock, flags);
1778 rport = fcport->drport;
1779 fcport->drport = NULL;
1780 spin_unlock_irqrestore(&fcport->rport_lock, flags);
1781 if (rport)
1782 fc_remote_port_delete(rport);
1783
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04001784}
1785
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786/**
1787 * qla2x00_alloc_fcport() - Allocate a generic fcport.
1788 * @ha: HA context
1789 * @flags: allocation flags
1790 *
1791 * Returns a pointer to the allocated fcport, or NULL, if none available.
1792 */
1793fc_port_t *
Al Viroc53033f2005-10-21 03:22:08 -04001794qla2x00_alloc_fcport(scsi_qla_host_t *ha, gfp_t flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795{
1796 fc_port_t *fcport;
1797
1798 fcport = kmalloc(sizeof(fc_port_t), flags);
1799 if (fcport == NULL)
1800 return (fcport);
1801
1802 /* Setup fcport template structure. */
1803 memset(fcport, 0, sizeof (fc_port_t));
1804 fcport->ha = ha;
1805 fcport->port_type = FCT_UNKNOWN;
1806 fcport->loop_id = FC_NO_LOOP_ID;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807 atomic_set(&fcport->state, FCS_UNCONFIGURED);
1808 fcport->flags = FCF_RLC_SUPPORT;
Andrew Vasquezad3e0ed2005-08-26 19:08:10 -07001809 fcport->supported_classes = FC_COS_UNSPECIFIED;
andrew.vasquez@qlogic.comd97994d2006-01-20 14:53:13 -08001810 spin_lock_init(&fcport->rport_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811
1812 return (fcport);
1813}
1814
1815/*
1816 * qla2x00_configure_loop
1817 * Updates Fibre Channel Device Database with what is actually on loop.
1818 *
1819 * Input:
1820 * ha = adapter block pointer.
1821 *
1822 * Returns:
1823 * 0 = success.
1824 * 1 = error.
1825 * 2 = database was full and device was not configured.
1826 */
1827static int
Andrew Vasquezfa2a1ce2005-07-06 10:32:07 -07001828qla2x00_configure_loop(scsi_qla_host_t *ha)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829{
1830 int rval;
1831 unsigned long flags, save_flags;
1832
1833 rval = QLA_SUCCESS;
1834
1835 /* Get Initiator ID */
1836 if (test_bit(LOCAL_LOOP_UPDATE, &ha->dpc_flags)) {
1837 rval = qla2x00_configure_hba(ha);
1838 if (rval != QLA_SUCCESS) {
1839 DEBUG(printk("scsi(%ld): Unable to configure HBA.\n",
1840 ha->host_no));
1841 return (rval);
1842 }
1843 }
1844
1845 save_flags = flags = ha->dpc_flags;
1846 DEBUG(printk("scsi(%ld): Configure loop -- dpc flags =0x%lx\n",
1847 ha->host_no, flags));
1848
1849 /*
1850 * If we have both an RSCN and PORT UPDATE pending then handle them
1851 * both at the same time.
1852 */
1853 clear_bit(LOCAL_LOOP_UPDATE, &ha->dpc_flags);
1854 clear_bit(RSCN_UPDATE, &ha->dpc_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855
1856 /* Determine what we need to do */
1857 if (ha->current_topology == ISP_CFG_FL &&
1858 (test_bit(LOCAL_LOOP_UPDATE, &flags))) {
1859
1860 ha->flags.rscn_queue_overflow = 1;
1861 set_bit(RSCN_UPDATE, &flags);
1862
1863 } else if (ha->current_topology == ISP_CFG_F &&
1864 (test_bit(LOCAL_LOOP_UPDATE, &flags))) {
1865
1866 ha->flags.rscn_queue_overflow = 1;
1867 set_bit(RSCN_UPDATE, &flags);
1868 clear_bit(LOCAL_LOOP_UPDATE, &flags);
1869
Andrew Vasquez21333b42006-05-17 15:09:56 -07001870 } else if (ha->current_topology == ISP_CFG_N) {
1871 clear_bit(RSCN_UPDATE, &flags);
1872
Linus Torvalds1da177e2005-04-16 15:20:36 -07001873 } else if (!ha->flags.online ||
1874 (test_bit(ABORT_ISP_ACTIVE, &flags))) {
1875
1876 ha->flags.rscn_queue_overflow = 1;
1877 set_bit(RSCN_UPDATE, &flags);
1878 set_bit(LOCAL_LOOP_UPDATE, &flags);
1879 }
1880
1881 if (test_bit(LOCAL_LOOP_UPDATE, &flags)) {
1882 if (test_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags)) {
1883 rval = QLA_FUNCTION_FAILED;
1884 } else {
1885 rval = qla2x00_configure_local_loop(ha);
1886 }
1887 }
1888
1889 if (rval == QLA_SUCCESS && test_bit(RSCN_UPDATE, &flags)) {
Andrew Vasquez23443b12005-12-06 10:57:06 -08001890 if (LOOP_TRANSITION(ha)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891 rval = QLA_FUNCTION_FAILED;
1892 } else {
1893 rval = qla2x00_configure_fabric(ha);
1894 }
1895 }
1896
1897 if (rval == QLA_SUCCESS) {
1898 if (atomic_read(&ha->loop_down_timer) ||
1899 test_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags)) {
1900 rval = QLA_FUNCTION_FAILED;
1901 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902 atomic_set(&ha->loop_state, LOOP_READY);
1903
1904 DEBUG(printk("scsi(%ld): LOOP READY\n", ha->host_no));
1905 }
1906 }
1907
1908 if (rval) {
1909 DEBUG2_3(printk("%s(%ld): *** FAILED ***\n",
1910 __func__, ha->host_no));
1911 } else {
1912 DEBUG3(printk("%s: exiting normally\n", __func__));
1913 }
1914
1915 /* Restore state if a resync event occured during processing */
1916 if (test_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags)) {
1917 if (test_bit(LOCAL_LOOP_UPDATE, &save_flags))
1918 set_bit(LOCAL_LOOP_UPDATE, &ha->dpc_flags);
1919 if (test_bit(RSCN_UPDATE, &save_flags))
1920 set_bit(RSCN_UPDATE, &ha->dpc_flags);
1921 }
1922
1923 return (rval);
1924}
1925
1926
1927
1928/*
1929 * qla2x00_configure_local_loop
1930 * Updates Fibre Channel Device Database with local loop devices.
1931 *
1932 * Input:
1933 * ha = adapter block pointer.
1934 *
1935 * Returns:
1936 * 0 = success.
1937 */
1938static int
Andrew Vasquezfa2a1ce2005-07-06 10:32:07 -07001939qla2x00_configure_local_loop(scsi_qla_host_t *ha)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940{
1941 int rval, rval2;
1942 int found_devs;
1943 int found;
1944 fc_port_t *fcport, *new_fcport;
1945
1946 uint16_t index;
1947 uint16_t entries;
1948 char *id_iter;
1949 uint16_t loop_id;
1950 uint8_t domain, area, al_pa;
1951
1952 found_devs = 0;
1953 new_fcport = NULL;
1954 entries = MAX_FIBRE_DEVICES;
1955
1956 DEBUG3(printk("scsi(%ld): Getting FCAL position map\n", ha->host_no));
1957 DEBUG3(qla2x00_get_fcal_position_map(ha, NULL));
1958
1959 /* Get list of logged in devices. */
1960 memset(ha->gid_list, 0, GID_LIST_SIZE);
1961 rval = qla2x00_get_id_list(ha, ha->gid_list, ha->gid_list_dma,
1962 &entries);
1963 if (rval != QLA_SUCCESS)
1964 goto cleanup_allocation;
1965
1966 DEBUG3(printk("scsi(%ld): Entries in ID list (%d)\n",
1967 ha->host_no, entries));
1968 DEBUG3(qla2x00_dump_buffer((uint8_t *)ha->gid_list,
1969 entries * sizeof(struct gid_list_info)));
1970
1971 /* Allocate temporary fcport for any new fcports discovered. */
1972 new_fcport = qla2x00_alloc_fcport(ha, GFP_KERNEL);
1973 if (new_fcport == NULL) {
1974 rval = QLA_MEMORY_ALLOC_FAILED;
1975 goto cleanup_allocation;
1976 }
1977 new_fcport->flags &= ~FCF_FABRIC_DEVICE;
1978
1979 /*
1980 * Mark local devices that were present with FCF_DEVICE_LOST for now.
1981 */
1982 list_for_each_entry(fcport, &ha->fcports, list) {
1983 if (atomic_read(&fcport->state) == FCS_ONLINE &&
1984 fcport->port_type != FCT_BROADCAST &&
1985 (fcport->flags & FCF_FABRIC_DEVICE) == 0) {
1986
1987 DEBUG(printk("scsi(%ld): Marking port lost, "
1988 "loop_id=0x%04x\n",
1989 ha->host_no, fcport->loop_id));
1990
1991 atomic_set(&fcport->state, FCS_DEVICE_LOST);
1992 fcport->flags &= ~FCF_FARP_DONE;
1993 }
1994 }
1995
1996 /* Add devices to port list. */
1997 id_iter = (char *)ha->gid_list;
1998 for (index = 0; index < entries; index++) {
1999 domain = ((struct gid_list_info *)id_iter)->domain;
2000 area = ((struct gid_list_info *)id_iter)->area;
2001 al_pa = ((struct gid_list_info *)id_iter)->al_pa;
Andrew Vasquezabbd8872005-07-06 10:30:05 -07002002 if (IS_QLA2100(ha) || IS_QLA2200(ha))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002003 loop_id = (uint16_t)
2004 ((struct gid_list_info *)id_iter)->loop_id_2100;
Andrew Vasquezabbd8872005-07-06 10:30:05 -07002005 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002006 loop_id = le16_to_cpu(
2007 ((struct gid_list_info *)id_iter)->loop_id);
Andrew Vasquezabbd8872005-07-06 10:30:05 -07002008 id_iter += ha->gid_list_info_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009
2010 /* Bypass reserved domain fields. */
2011 if ((domain & 0xf0) == 0xf0)
2012 continue;
2013
2014 /* Bypass if not same domain and area of adapter. */
Andrew Vasquezf7d289f2005-08-26 19:08:40 -07002015 if (area && domain &&
2016 (area != ha->d_id.b.area || domain != ha->d_id.b.domain))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017 continue;
2018
2019 /* Bypass invalid local loop ID. */
2020 if (loop_id > LAST_LOCAL_LOOP_ID)
2021 continue;
2022
2023 /* Fill in member data. */
2024 new_fcport->d_id.b.domain = domain;
2025 new_fcport->d_id.b.area = area;
2026 new_fcport->d_id.b.al_pa = al_pa;
2027 new_fcport->loop_id = loop_id;
2028 rval2 = qla2x00_get_port_database(ha, new_fcport, 0);
2029 if (rval2 != QLA_SUCCESS) {
2030 DEBUG2(printk("scsi(%ld): Failed to retrieve fcport "
2031 "information -- get_port_database=%x, "
2032 "loop_id=0x%04x\n",
2033 ha->host_no, rval2, new_fcport->loop_id));
andrew.vasquez@qlogic.comc9d02ac2006-01-13 17:05:26 -08002034 DEBUG2(printk("scsi(%ld): Scheduling resync...\n",
2035 ha->host_no));
2036 set_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037 continue;
2038 }
2039
2040 /* Check for matching device in port list. */
2041 found = 0;
2042 fcport = NULL;
2043 list_for_each_entry(fcport, &ha->fcports, list) {
2044 if (memcmp(new_fcport->port_name, fcport->port_name,
2045 WWN_SIZE))
2046 continue;
2047
2048 fcport->flags &= ~(FCF_FABRIC_DEVICE |
2049 FCF_PERSISTENT_BOUND);
2050 fcport->loop_id = new_fcport->loop_id;
2051 fcport->port_type = new_fcport->port_type;
2052 fcport->d_id.b24 = new_fcport->d_id.b24;
2053 memcpy(fcport->node_name, new_fcport->node_name,
2054 WWN_SIZE);
2055
2056 found++;
2057 break;
2058 }
2059
2060 if (!found) {
2061 /* New device, add to fcports list. */
2062 new_fcport->flags &= ~FCF_PERSISTENT_BOUND;
2063 list_add_tail(&new_fcport->list, &ha->fcports);
2064
2065 /* Allocate a new replacement fcport. */
2066 fcport = new_fcport;
2067 new_fcport = qla2x00_alloc_fcport(ha, GFP_KERNEL);
2068 if (new_fcport == NULL) {
2069 rval = QLA_MEMORY_ALLOC_FAILED;
2070 goto cleanup_allocation;
2071 }
2072 new_fcport->flags &= ~FCF_FABRIC_DEVICE;
2073 }
2074
2075 qla2x00_update_fcport(ha, fcport);
2076
2077 found_devs++;
2078 }
2079
2080cleanup_allocation:
Jesper Juhlc9475cb2005-11-07 01:01:26 -08002081 kfree(new_fcport);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002082
2083 if (rval != QLA_SUCCESS) {
2084 DEBUG2(printk("scsi(%ld): Configure local loop error exit: "
2085 "rval=%x\n", ha->host_no, rval));
2086 }
2087
2088 if (found_devs) {
2089 ha->device_flags |= DFLG_LOCAL_DEVICES;
2090 ha->device_flags &= ~DFLG_RETRY_LOCAL_DEVICES;
2091 }
2092
2093 return (rval);
2094}
2095
2096static void
Andrew Vasquezfa2a1ce2005-07-06 10:32:07 -07002097qla2x00_probe_for_all_luns(scsi_qla_host_t *ha)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002098{
2099 fc_port_t *fcport;
2100
andrew.vasquez@qlogic.comd97994d2006-01-20 14:53:13 -08002101 qla2x00_mark_all_devices_lost(ha, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002102 list_for_each_entry(fcport, &ha->fcports, list) {
2103 if (fcport->port_type != FCT_TARGET)
2104 continue;
2105
Andrew Vasquezfa2a1ce2005-07-06 10:32:07 -07002106 qla2x00_update_fcport(ha, fcport);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002107 }
2108}
2109
2110/*
2111 * qla2x00_update_fcport
2112 * Updates device on list.
2113 *
2114 * Input:
2115 * ha = adapter block pointer.
2116 * fcport = port structure pointer.
2117 *
2118 * Return:
2119 * 0 - Success
2120 * BIT_0 - error
2121 *
2122 * Context:
2123 * Kernel context.
2124 */
andrew.vasquez@qlogic.com052c40c2006-01-20 14:53:19 -08002125void
Linus Torvalds1da177e2005-04-16 15:20:36 -07002126qla2x00_update_fcport(scsi_qla_host_t *ha, fc_port_t *fcport)
2127{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002128 fcport->ha = ha;
2129 fcport->login_retry = 0;
2130 fcport->port_login_retry_count = ha->port_down_retry_count *
2131 PORT_RETRY_TIME;
2132 atomic_set(&fcport->port_down_timer, ha->port_down_retry_count *
2133 PORT_RETRY_TIME);
2134 fcport->flags &= ~FCF_LOGIN_NEEDED;
2135
Linus Torvalds1da177e2005-04-16 15:20:36 -07002136 atomic_set(&fcport->state, FCS_ONLINE);
bdf79622005-04-17 15:06:53 -05002137
8482e112005-04-17 15:04:54 -05002138 if (ha->flags.init_done)
2139 qla2x00_reg_remote_port(ha, fcport);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002140}
2141
8482e112005-04-17 15:04:54 -05002142void
2143qla2x00_reg_remote_port(scsi_qla_host_t *ha, fc_port_t *fcport)
2144{
2145 struct fc_rport_identifiers rport_ids;
bdf79622005-04-17 15:06:53 -05002146 struct fc_rport *rport;
andrew.vasquez@qlogic.comd97994d2006-01-20 14:53:13 -08002147 unsigned long flags;
8482e112005-04-17 15:04:54 -05002148
andrew.vasquez@qlogic.comd97994d2006-01-20 14:53:13 -08002149 if (fcport->drport)
2150 qla2x00_rport_del(fcport);
2151 if (fcport->rport)
2152 return;
8482e112005-04-17 15:04:54 -05002153
Andrew Vasquezf8b02a82005-08-31 15:21:20 -07002154 rport_ids.node_name = wwn_to_u64(fcport->node_name);
2155 rport_ids.port_name = wwn_to_u64(fcport->port_name);
8482e112005-04-17 15:04:54 -05002156 rport_ids.port_id = fcport->d_id.b.domain << 16 |
2157 fcport->d_id.b.area << 8 | fcport->d_id.b.al_pa;
2158 rport_ids.roles = FC_RPORT_ROLE_UNKNOWN;
andrew.vasquez@qlogic.comd97994d2006-01-20 14:53:13 -08002159 rport = fc_remote_port_add(ha->host, 0, &rport_ids);
Andrew Vasquez77d74142005-07-08 18:00:36 -07002160 if (!rport) {
2161 qla_printk(KERN_WARNING, ha,
2162 "Unable to allocate fc remote port!\n");
2163 return;
2164 }
andrew.vasquez@qlogic.comd97994d2006-01-20 14:53:13 -08002165 spin_lock_irqsave(&fcport->rport_lock, flags);
2166 fcport->rport = rport;
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04002167 *((fc_port_t **)rport->dd_data) = fcport;
andrew.vasquez@qlogic.comd97994d2006-01-20 14:53:13 -08002168 spin_unlock_irqrestore(&fcport->rport_lock, flags);
2169
Andrew Vasquezad3e0ed2005-08-26 19:08:10 -07002170 rport->supported_classes = fcport->supported_classes;
Andrew Vasquez77d74142005-07-08 18:00:36 -07002171
2172 rport_ids.roles = FC_RPORT_ROLE_UNKNOWN;
8482e112005-04-17 15:04:54 -05002173 if (fcport->port_type == FCT_INITIATOR)
2174 rport_ids.roles |= FC_RPORT_ROLE_FCP_INITIATOR;
2175 if (fcport->port_type == FCT_TARGET)
2176 rport_ids.roles |= FC_RPORT_ROLE_FCP_TARGET;
Andrew Vasquez77d74142005-07-08 18:00:36 -07002177 fc_remote_port_rolechg(rport, rport_ids.roles);
bdf79622005-04-17 15:06:53 -05002178
Andrew Vasquezcc4731f2005-07-06 10:32:37 -07002179 if (rport->scsi_target_id != -1 &&
2180 rport->scsi_target_id < ha->host->max_id)
bdf79622005-04-17 15:06:53 -05002181 fcport->os_target_id = rport->scsi_target_id;
8482e112005-04-17 15:04:54 -05002182}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002183
2184/*
2185 * qla2x00_configure_fabric
2186 * Setup SNS devices with loop ID's.
2187 *
2188 * Input:
2189 * ha = adapter block pointer.
2190 *
2191 * Returns:
2192 * 0 = success.
2193 * BIT_0 = error
2194 */
2195static int
2196qla2x00_configure_fabric(scsi_qla_host_t *ha)
2197{
2198 int rval, rval2;
2199 fc_port_t *fcport, *fcptemp;
2200 uint16_t next_loopid;
2201 uint16_t mb[MAILBOX_REGISTER_COUNT];
Andrew Vasquez01071092005-07-06 10:31:37 -07002202 uint16_t loop_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002203 LIST_HEAD(new_fcports);
2204
2205 /* If FL port exists, then SNS is present */
andrew.vasquez@qlogic.com044cc6c2006-03-09 14:27:13 -08002206 if (IS_QLA24XX(ha) || IS_QLA54XX(ha))
Andrew Vasquez01071092005-07-06 10:31:37 -07002207 loop_id = NPH_F_PORT;
2208 else
2209 loop_id = SNS_FL_PORT;
2210 rval = qla2x00_get_port_name(ha, loop_id, NULL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002211 if (rval != QLA_SUCCESS) {
2212 DEBUG2(printk("scsi(%ld): MBC_GET_PORT_NAME Failed, No FL "
2213 "Port\n", ha->host_no));
2214
2215 ha->device_flags &= ~SWITCH_FOUND;
2216 return (QLA_SUCCESS);
2217 }
2218
2219 /* Mark devices that need re-synchronization. */
2220 rval2 = qla2x00_device_resync(ha);
2221 if (rval2 == QLA_RSCNS_HANDLED) {
2222 /* No point doing the scan, just continue. */
2223 return (QLA_SUCCESS);
2224 }
2225 do {
Andrew Vasquezcca53352005-08-26 19:08:30 -07002226 /* FDMI support. */
2227 if (ql2xfdmienable &&
2228 test_and_clear_bit(REGISTER_FDMI_NEEDED, &ha->dpc_flags))
2229 qla2x00_fdmi_register(ha);
2230
Linus Torvalds1da177e2005-04-16 15:20:36 -07002231 /* Ensure we are logged into the SNS. */
andrew.vasquez@qlogic.com044cc6c2006-03-09 14:27:13 -08002232 if (IS_QLA24XX(ha) || IS_QLA54XX(ha))
Andrew Vasquez01071092005-07-06 10:31:37 -07002233 loop_id = NPH_SNS;
2234 else
2235 loop_id = SIMPLE_NAME_SERVER;
2236 ha->isp_ops.fabric_login(ha, loop_id, 0xff, 0xff,
Andrew Vasquezabbd8872005-07-06 10:30:05 -07002237 0xfc, mb, BIT_1 | BIT_0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002238 if (mb[0] != MBS_COMMAND_COMPLETE) {
2239 DEBUG2(qla_printk(KERN_INFO, ha,
2240 "Failed SNS login: loop_id=%x mb[0]=%x mb[1]=%x "
Andrew Vasquez01071092005-07-06 10:31:37 -07002241 "mb[2]=%x mb[6]=%x mb[7]=%x\n", loop_id,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002242 mb[0], mb[1], mb[2], mb[6], mb[7]));
2243 return (QLA_SUCCESS);
2244 }
2245
2246 if (test_and_clear_bit(REGISTER_FC4_NEEDED, &ha->dpc_flags)) {
2247 if (qla2x00_rft_id(ha)) {
2248 /* EMPTY */
2249 DEBUG2(printk("scsi(%ld): Register FC-4 "
2250 "TYPE failed.\n", ha->host_no));
2251 }
2252 if (qla2x00_rff_id(ha)) {
2253 /* EMPTY */
2254 DEBUG2(printk("scsi(%ld): Register FC-4 "
2255 "Features failed.\n", ha->host_no));
2256 }
2257 if (qla2x00_rnn_id(ha)) {
2258 /* EMPTY */
2259 DEBUG2(printk("scsi(%ld): Register Node Name "
2260 "failed.\n", ha->host_no));
2261 } else if (qla2x00_rsnn_nn(ha)) {
2262 /* EMPTY */
2263 DEBUG2(printk("scsi(%ld): Register Symbolic "
2264 "Node Name failed.\n", ha->host_no));
2265 }
2266 }
2267
2268 rval = qla2x00_find_all_fabric_devs(ha, &new_fcports);
2269 if (rval != QLA_SUCCESS)
2270 break;
2271
2272 /*
2273 * Logout all previous fabric devices marked lost, except
2274 * tape devices.
2275 */
2276 list_for_each_entry(fcport, &ha->fcports, list) {
2277 if (test_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags))
2278 break;
2279
2280 if ((fcport->flags & FCF_FABRIC_DEVICE) == 0)
2281 continue;
2282
2283 if (atomic_read(&fcport->state) == FCS_DEVICE_LOST) {
2284 qla2x00_mark_device_lost(ha, fcport,
andrew.vasquez@qlogic.comd97994d2006-01-20 14:53:13 -08002285 ql2xplogiabsentdevice, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002286 if (fcport->loop_id != FC_NO_LOOP_ID &&
2287 (fcport->flags & FCF_TAPE_PRESENT) == 0 &&
2288 fcport->port_type != FCT_INITIATOR &&
2289 fcport->port_type != FCT_BROADCAST) {
Andrew Vasquezabbd8872005-07-06 10:30:05 -07002290 ha->isp_ops.fabric_logout(ha,
Andrew Vasquez1c7c6352005-07-06 10:30:57 -07002291 fcport->loop_id,
2292 fcport->d_id.b.domain,
2293 fcport->d_id.b.area,
2294 fcport->d_id.b.al_pa);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002295 fcport->loop_id = FC_NO_LOOP_ID;
2296 }
2297 }
2298 }
2299
2300 /* Starting free loop ID. */
2301 next_loopid = ha->min_external_loopid;
2302
2303 /*
2304 * Scan through our port list and login entries that need to be
2305 * logged in.
2306 */
2307 list_for_each_entry(fcport, &ha->fcports, list) {
2308 if (atomic_read(&ha->loop_down_timer) ||
2309 test_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags))
2310 break;
2311
2312 if ((fcport->flags & FCF_FABRIC_DEVICE) == 0 ||
2313 (fcport->flags & FCF_LOGIN_NEEDED) == 0)
2314 continue;
2315
2316 if (fcport->loop_id == FC_NO_LOOP_ID) {
2317 fcport->loop_id = next_loopid;
2318 rval = qla2x00_find_new_loop_id(ha, fcport);
2319 if (rval != QLA_SUCCESS) {
2320 /* Ran out of IDs to use */
2321 break;
2322 }
2323 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002324 /* Login and update database */
2325 qla2x00_fabric_dev_login(ha, fcport, &next_loopid);
2326 }
2327
2328 /* Exit if out of loop IDs. */
2329 if (rval != QLA_SUCCESS) {
2330 break;
2331 }
2332
2333 /*
2334 * Login and add the new devices to our port list.
2335 */
2336 list_for_each_entry_safe(fcport, fcptemp, &new_fcports, list) {
2337 if (atomic_read(&ha->loop_down_timer) ||
2338 test_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags))
2339 break;
2340
2341 /* Find a new loop ID to use. */
2342 fcport->loop_id = next_loopid;
2343 rval = qla2x00_find_new_loop_id(ha, fcport);
2344 if (rval != QLA_SUCCESS) {
2345 /* Ran out of IDs to use */
2346 break;
2347 }
2348
Linus Torvalds1da177e2005-04-16 15:20:36 -07002349 /* Remove device from the new list and add it to DB */
2350 list_del(&fcport->list);
2351 list_add_tail(&fcport->list, &ha->fcports);
bdf79622005-04-17 15:06:53 -05002352
2353 /* Login and update database */
2354 qla2x00_fabric_dev_login(ha, fcport, &next_loopid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002355 }
2356 } while (0);
2357
2358 /* Free all new device structures not processed. */
2359 list_for_each_entry_safe(fcport, fcptemp, &new_fcports, list) {
2360 list_del(&fcport->list);
2361 kfree(fcport);
2362 }
2363
2364 if (rval) {
2365 DEBUG2(printk("scsi(%ld): Configure fabric error exit: "
2366 "rval=%d\n", ha->host_no, rval));
2367 }
2368
2369 return (rval);
2370}
2371
2372
2373/*
2374 * qla2x00_find_all_fabric_devs
2375 *
2376 * Input:
2377 * ha = adapter block pointer.
2378 * dev = database device entry pointer.
2379 *
2380 * Returns:
2381 * 0 = success.
2382 *
2383 * Context:
2384 * Kernel context.
2385 */
2386static int
2387qla2x00_find_all_fabric_devs(scsi_qla_host_t *ha, struct list_head *new_fcports)
2388{
2389 int rval;
2390 uint16_t loop_id;
2391 fc_port_t *fcport, *new_fcport, *fcptemp;
2392 int found;
2393
2394 sw_info_t *swl;
2395 int swl_idx;
2396 int first_dev, last_dev;
2397 port_id_t wrap, nxt_d_id;
2398
2399 rval = QLA_SUCCESS;
2400
2401 /* Try GID_PT to get device list, else GAN. */
2402 swl = kmalloc(sizeof(sw_info_t) * MAX_FIBRE_DEVICES, GFP_ATOMIC);
2403 if (swl == NULL) {
2404 /*EMPTY*/
2405 DEBUG2(printk("scsi(%ld): GID_PT allocations failed, fallback "
2406 "on GA_NXT\n", ha->host_no));
2407 } else {
2408 memset(swl, 0, sizeof(sw_info_t) * MAX_FIBRE_DEVICES);
2409 if (qla2x00_gid_pt(ha, swl) != QLA_SUCCESS) {
2410 kfree(swl);
2411 swl = NULL;
2412 } else if (qla2x00_gpn_id(ha, swl) != QLA_SUCCESS) {
2413 kfree(swl);
2414 swl = NULL;
2415 } else if (qla2x00_gnn_id(ha, swl) != QLA_SUCCESS) {
2416 kfree(swl);
2417 swl = NULL;
2418 }
2419 }
2420 swl_idx = 0;
2421
2422 /* Allocate temporary fcport for any new fcports discovered. */
2423 new_fcport = qla2x00_alloc_fcport(ha, GFP_KERNEL);
2424 if (new_fcport == NULL) {
Jesper Juhlc9475cb2005-11-07 01:01:26 -08002425 kfree(swl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002426 return (QLA_MEMORY_ALLOC_FAILED);
2427 }
2428 new_fcport->flags |= (FCF_FABRIC_DEVICE | FCF_LOGIN_NEEDED);
2429
2430 /* Set start port ID scan at adapter ID. */
2431 first_dev = 1;
2432 last_dev = 0;
2433
2434 /* Starting free loop ID. */
2435 loop_id = ha->min_external_loopid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002436 for (; loop_id <= ha->last_loop_id; loop_id++) {
Andrew Vasquez3d716442005-07-06 10:30:26 -07002437 if (qla2x00_is_reserved_id(ha, loop_id))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002438 continue;
2439
Andrew Vasquez23443b12005-12-06 10:57:06 -08002440 if (atomic_read(&ha->loop_down_timer) || LOOP_TRANSITION(ha))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002441 break;
2442
2443 if (swl != NULL) {
2444 if (last_dev) {
2445 wrap.b24 = new_fcport->d_id.b24;
2446 } else {
2447 new_fcport->d_id.b24 = swl[swl_idx].d_id.b24;
2448 memcpy(new_fcport->node_name,
2449 swl[swl_idx].node_name, WWN_SIZE);
2450 memcpy(new_fcport->port_name,
2451 swl[swl_idx].port_name, WWN_SIZE);
2452
2453 if (swl[swl_idx].d_id.b.rsvd_1 != 0) {
2454 last_dev = 1;
2455 }
2456 swl_idx++;
2457 }
2458 } else {
2459 /* Send GA_NXT to the switch */
2460 rval = qla2x00_ga_nxt(ha, new_fcport);
2461 if (rval != QLA_SUCCESS) {
2462 qla_printk(KERN_WARNING, ha,
2463 "SNS scan failed -- assuming zero-entry "
2464 "result...\n");
2465 list_for_each_entry_safe(fcport, fcptemp,
2466 new_fcports, list) {
2467 list_del(&fcport->list);
2468 kfree(fcport);
2469 }
2470 rval = QLA_SUCCESS;
2471 break;
2472 }
2473 }
2474
2475 /* If wrap on switch device list, exit. */
2476 if (first_dev) {
2477 wrap.b24 = new_fcport->d_id.b24;
2478 first_dev = 0;
2479 } else if (new_fcport->d_id.b24 == wrap.b24) {
2480 DEBUG2(printk("scsi(%ld): device wrap (%02x%02x%02x)\n",
2481 ha->host_no, new_fcport->d_id.b.domain,
2482 new_fcport->d_id.b.area, new_fcport->d_id.b.al_pa));
2483 break;
2484 }
2485
2486 /* Bypass if host adapter. */
2487 if (new_fcport->d_id.b24 == ha->d_id.b24)
2488 continue;
2489
Andrew Vasquezf7d289f2005-08-26 19:08:40 -07002490 /* Bypass if same domain and area of adapter. */
2491 if (((new_fcport->d_id.b24 & 0xffff00) ==
2492 (ha->d_id.b24 & 0xffff00)) && ha->current_topology ==
2493 ISP_CFG_FL)
2494 continue;
2495
Linus Torvalds1da177e2005-04-16 15:20:36 -07002496 /* Bypass reserved domain fields. */
2497 if ((new_fcport->d_id.b.domain & 0xf0) == 0xf0)
2498 continue;
2499
2500 /* Locate matching device in database. */
2501 found = 0;
2502 list_for_each_entry(fcport, &ha->fcports, list) {
2503 if (memcmp(new_fcport->port_name, fcport->port_name,
2504 WWN_SIZE))
2505 continue;
2506
2507 found++;
2508
2509 /*
2510 * If address the same and state FCS_ONLINE, nothing
2511 * changed.
2512 */
2513 if (fcport->d_id.b24 == new_fcport->d_id.b24 &&
2514 atomic_read(&fcport->state) == FCS_ONLINE) {
2515 break;
2516 }
2517
2518 /*
2519 * If device was not a fabric device before.
2520 */
2521 if ((fcport->flags & FCF_FABRIC_DEVICE) == 0) {
2522 fcport->d_id.b24 = new_fcport->d_id.b24;
2523 fcport->loop_id = FC_NO_LOOP_ID;
2524 fcport->flags |= (FCF_FABRIC_DEVICE |
2525 FCF_LOGIN_NEEDED);
2526 fcport->flags &= ~FCF_PERSISTENT_BOUND;
2527 break;
2528 }
2529
2530 /*
2531 * Port ID changed or device was marked to be updated;
2532 * Log it out if still logged in and mark it for
2533 * relogin later.
2534 */
2535 fcport->d_id.b24 = new_fcport->d_id.b24;
2536 fcport->flags |= FCF_LOGIN_NEEDED;
2537 if (fcport->loop_id != FC_NO_LOOP_ID &&
2538 (fcport->flags & FCF_TAPE_PRESENT) == 0 &&
2539 fcport->port_type != FCT_INITIATOR &&
2540 fcport->port_type != FCT_BROADCAST) {
Andrew Vasquez1c7c6352005-07-06 10:30:57 -07002541 ha->isp_ops.fabric_logout(ha, fcport->loop_id,
2542 fcport->d_id.b.domain, fcport->d_id.b.area,
2543 fcport->d_id.b.al_pa);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002544 fcport->loop_id = FC_NO_LOOP_ID;
2545 }
2546
2547 break;
2548 }
2549
2550 if (found)
2551 continue;
2552
2553 /* If device was not in our fcports list, then add it. */
2554 list_add_tail(&new_fcport->list, new_fcports);
2555
2556 /* Allocate a new replacement fcport. */
2557 nxt_d_id.b24 = new_fcport->d_id.b24;
2558 new_fcport = qla2x00_alloc_fcport(ha, GFP_KERNEL);
2559 if (new_fcport == NULL) {
Jesper Juhlc9475cb2005-11-07 01:01:26 -08002560 kfree(swl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002561 return (QLA_MEMORY_ALLOC_FAILED);
2562 }
2563 new_fcport->flags |= (FCF_FABRIC_DEVICE | FCF_LOGIN_NEEDED);
2564 new_fcport->d_id.b24 = nxt_d_id.b24;
2565 }
2566
Jesper Juhlc9475cb2005-11-07 01:01:26 -08002567 kfree(swl);
2568 kfree(new_fcport);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002569
2570 if (!list_empty(new_fcports))
2571 ha->device_flags |= DFLG_FABRIC_DEVICES;
2572
2573 return (rval);
2574}
2575
2576/*
2577 * qla2x00_find_new_loop_id
2578 * Scan through our port list and find a new usable loop ID.
2579 *
2580 * Input:
2581 * ha: adapter state pointer.
2582 * dev: port structure pointer.
2583 *
2584 * Returns:
2585 * qla2x00 local function return status code.
2586 *
2587 * Context:
2588 * Kernel context.
2589 */
2590int
2591qla2x00_find_new_loop_id(scsi_qla_host_t *ha, fc_port_t *dev)
2592{
2593 int rval;
2594 int found;
2595 fc_port_t *fcport;
2596 uint16_t first_loop_id;
2597
2598 rval = QLA_SUCCESS;
2599
2600 /* Save starting loop ID. */
2601 first_loop_id = dev->loop_id;
2602
2603 for (;;) {
2604 /* Skip loop ID if already used by adapter. */
2605 if (dev->loop_id == ha->loop_id) {
2606 dev->loop_id++;
2607 }
2608
2609 /* Skip reserved loop IDs. */
Andrew Vasquez3d716442005-07-06 10:30:26 -07002610 while (qla2x00_is_reserved_id(ha, dev->loop_id)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002611 dev->loop_id++;
2612 }
2613
2614 /* Reset loop ID if passed the end. */
2615 if (dev->loop_id > ha->last_loop_id) {
2616 /* first loop ID. */
2617 dev->loop_id = ha->min_external_loopid;
2618 }
2619
2620 /* Check for loop ID being already in use. */
2621 found = 0;
2622 fcport = NULL;
2623 list_for_each_entry(fcport, &ha->fcports, list) {
2624 if (fcport->loop_id == dev->loop_id && fcport != dev) {
2625 /* ID possibly in use */
2626 found++;
2627 break;
2628 }
2629 }
2630
2631 /* If not in use then it is free to use. */
2632 if (!found) {
2633 break;
2634 }
2635
2636 /* ID in use. Try next value. */
2637 dev->loop_id++;
2638
2639 /* If wrap around. No free ID to use. */
2640 if (dev->loop_id == first_loop_id) {
2641 dev->loop_id = FC_NO_LOOP_ID;
2642 rval = QLA_FUNCTION_FAILED;
2643 break;
2644 }
2645 }
2646
2647 return (rval);
2648}
2649
2650/*
2651 * qla2x00_device_resync
2652 * Marks devices in the database that needs resynchronization.
2653 *
2654 * Input:
2655 * ha = adapter block pointer.
2656 *
2657 * Context:
2658 * Kernel context.
2659 */
2660static int
Andrew Vasquezfa2a1ce2005-07-06 10:32:07 -07002661qla2x00_device_resync(scsi_qla_host_t *ha)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002662{
2663 int rval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002664 uint32_t mask;
2665 fc_port_t *fcport;
2666 uint32_t rscn_entry;
2667 uint8_t rscn_out_iter;
2668 uint8_t format;
2669 port_id_t d_id;
2670
2671 rval = QLA_RSCNS_HANDLED;
2672
2673 while (ha->rscn_out_ptr != ha->rscn_in_ptr ||
2674 ha->flags.rscn_queue_overflow) {
2675
2676 rscn_entry = ha->rscn_queue[ha->rscn_out_ptr];
2677 format = MSB(MSW(rscn_entry));
2678 d_id.b.domain = LSB(MSW(rscn_entry));
2679 d_id.b.area = MSB(LSW(rscn_entry));
2680 d_id.b.al_pa = LSB(LSW(rscn_entry));
2681
2682 DEBUG(printk("scsi(%ld): RSCN queue entry[%d] = "
2683 "[%02x/%02x%02x%02x].\n",
2684 ha->host_no, ha->rscn_out_ptr, format, d_id.b.domain,
2685 d_id.b.area, d_id.b.al_pa));
2686
2687 ha->rscn_out_ptr++;
2688 if (ha->rscn_out_ptr == MAX_RSCN_COUNT)
2689 ha->rscn_out_ptr = 0;
2690
2691 /* Skip duplicate entries. */
2692 for (rscn_out_iter = ha->rscn_out_ptr;
2693 !ha->flags.rscn_queue_overflow &&
2694 rscn_out_iter != ha->rscn_in_ptr;
2695 rscn_out_iter = (rscn_out_iter ==
2696 (MAX_RSCN_COUNT - 1)) ? 0: rscn_out_iter + 1) {
2697
2698 if (rscn_entry != ha->rscn_queue[rscn_out_iter])
2699 break;
2700
2701 DEBUG(printk("scsi(%ld): Skipping duplicate RSCN queue "
2702 "entry found at [%d].\n", ha->host_no,
2703 rscn_out_iter));
2704
2705 ha->rscn_out_ptr = rscn_out_iter;
2706 }
2707
2708 /* Queue overflow, set switch default case. */
2709 if (ha->flags.rscn_queue_overflow) {
2710 DEBUG(printk("scsi(%ld): device_resync: rscn "
2711 "overflow.\n", ha->host_no));
2712
2713 format = 3;
2714 ha->flags.rscn_queue_overflow = 0;
2715 }
2716
2717 switch (format) {
2718 case 0:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002719 mask = 0xffffff;
2720 break;
2721 case 1:
2722 mask = 0xffff00;
2723 break;
2724 case 2:
2725 mask = 0xff0000;
2726 break;
2727 default:
2728 mask = 0x0;
2729 d_id.b24 = 0;
2730 ha->rscn_out_ptr = ha->rscn_in_ptr;
2731 break;
2732 }
2733
2734 rval = QLA_SUCCESS;
2735
Linus Torvalds1da177e2005-04-16 15:20:36 -07002736 list_for_each_entry(fcport, &ha->fcports, list) {
2737 if ((fcport->flags & FCF_FABRIC_DEVICE) == 0 ||
2738 (fcport->d_id.b24 & mask) != d_id.b24 ||
2739 fcport->port_type == FCT_BROADCAST)
2740 continue;
2741
2742 if (atomic_read(&fcport->state) == FCS_ONLINE) {
2743 if (format != 3 ||
2744 fcport->port_type != FCT_INITIATOR) {
andrew.vasquez@qlogic.comd97994d2006-01-20 14:53:13 -08002745 qla2x00_mark_device_lost(ha, fcport,
2746 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002747 }
2748 }
2749 fcport->flags &= ~FCF_FARP_DONE;
2750 }
2751 }
2752 return (rval);
2753}
2754
2755/*
2756 * qla2x00_fabric_dev_login
2757 * Login fabric target device and update FC port database.
2758 *
2759 * Input:
2760 * ha: adapter state pointer.
2761 * fcport: port structure list pointer.
2762 * next_loopid: contains value of a new loop ID that can be used
2763 * by the next login attempt.
2764 *
2765 * Returns:
2766 * qla2x00 local function return status code.
2767 *
2768 * Context:
2769 * Kernel context.
2770 */
2771static int
2772qla2x00_fabric_dev_login(scsi_qla_host_t *ha, fc_port_t *fcport,
2773 uint16_t *next_loopid)
2774{
2775 int rval;
2776 int retry;
Andrew Vasquez01071092005-07-06 10:31:37 -07002777 uint8_t opts;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002778
2779 rval = QLA_SUCCESS;
2780 retry = 0;
2781
2782 rval = qla2x00_fabric_login(ha, fcport, next_loopid);
2783 if (rval == QLA_SUCCESS) {
Andrew Vasquez01071092005-07-06 10:31:37 -07002784 /* Send an ADISC to tape devices.*/
2785 opts = 0;
2786 if (fcport->flags & FCF_TAPE_PRESENT)
2787 opts |= BIT_1;
2788 rval = qla2x00_get_port_database(ha, fcport, opts);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002789 if (rval != QLA_SUCCESS) {
Andrew Vasquez1c7c6352005-07-06 10:30:57 -07002790 ha->isp_ops.fabric_logout(ha, fcport->loop_id,
2791 fcport->d_id.b.domain, fcport->d_id.b.area,
2792 fcport->d_id.b.al_pa);
andrew.vasquez@qlogic.comd97994d2006-01-20 14:53:13 -08002793 qla2x00_mark_device_lost(ha, fcport, 1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002794 } else {
2795 qla2x00_update_fcport(ha, fcport);
2796 }
2797 }
2798
2799 return (rval);
2800}
2801
2802/*
2803 * qla2x00_fabric_login
2804 * Issue fabric login command.
2805 *
2806 * Input:
2807 * ha = adapter block pointer.
2808 * device = pointer to FC device type structure.
2809 *
2810 * Returns:
2811 * 0 - Login successfully
2812 * 1 - Login failed
2813 * 2 - Initiator device
2814 * 3 - Fatal error
2815 */
2816int
2817qla2x00_fabric_login(scsi_qla_host_t *ha, fc_port_t *fcport,
2818 uint16_t *next_loopid)
2819{
2820 int rval;
2821 int retry;
2822 uint16_t tmp_loopid;
2823 uint16_t mb[MAILBOX_REGISTER_COUNT];
2824
2825 retry = 0;
2826 tmp_loopid = 0;
2827
2828 for (;;) {
2829 DEBUG(printk("scsi(%ld): Trying Fabric Login w/loop id 0x%04x "
2830 "for port %02x%02x%02x.\n",
2831 ha->host_no, fcport->loop_id, fcport->d_id.b.domain,
2832 fcport->d_id.b.area, fcport->d_id.b.al_pa));
2833
2834 /* Login fcport on switch. */
Andrew Vasquezabbd8872005-07-06 10:30:05 -07002835 ha->isp_ops.fabric_login(ha, fcport->loop_id,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002836 fcport->d_id.b.domain, fcport->d_id.b.area,
2837 fcport->d_id.b.al_pa, mb, BIT_0);
2838 if (mb[0] == MBS_PORT_ID_USED) {
2839 /*
2840 * Device has another loop ID. The firmware team
Andrew Vasquez01071092005-07-06 10:31:37 -07002841 * recommends the driver perform an implicit login with
2842 * the specified ID again. The ID we just used is save
2843 * here so we return with an ID that can be tried by
2844 * the next login.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002845 */
2846 retry++;
2847 tmp_loopid = fcport->loop_id;
2848 fcport->loop_id = mb[1];
2849
2850 DEBUG(printk("Fabric Login: port in use - next "
2851 "loop id=0x%04x, port Id=%02x%02x%02x.\n",
2852 fcport->loop_id, fcport->d_id.b.domain,
2853 fcport->d_id.b.area, fcport->d_id.b.al_pa));
2854
2855 } else if (mb[0] == MBS_COMMAND_COMPLETE) {
2856 /*
2857 * Login succeeded.
2858 */
2859 if (retry) {
2860 /* A retry occurred before. */
2861 *next_loopid = tmp_loopid;
2862 } else {
2863 /*
2864 * No retry occurred before. Just increment the
2865 * ID value for next login.
2866 */
2867 *next_loopid = (fcport->loop_id + 1);
2868 }
2869
2870 if (mb[1] & BIT_0) {
2871 fcport->port_type = FCT_INITIATOR;
2872 } else {
2873 fcport->port_type = FCT_TARGET;
2874 if (mb[1] & BIT_1) {
2875 fcport->flags |= FCF_TAPE_PRESENT;
2876 }
2877 }
2878
Andrew Vasquezad3e0ed2005-08-26 19:08:10 -07002879 if (mb[10] & BIT_0)
2880 fcport->supported_classes |= FC_COS_CLASS2;
2881 if (mb[10] & BIT_1)
2882 fcport->supported_classes |= FC_COS_CLASS3;
2883
Linus Torvalds1da177e2005-04-16 15:20:36 -07002884 rval = QLA_SUCCESS;
2885 break;
2886 } else if (mb[0] == MBS_LOOP_ID_USED) {
2887 /*
2888 * Loop ID already used, try next loop ID.
2889 */
2890 fcport->loop_id++;
2891 rval = qla2x00_find_new_loop_id(ha, fcport);
2892 if (rval != QLA_SUCCESS) {
2893 /* Ran out of loop IDs to use */
2894 break;
2895 }
2896 } else if (mb[0] == MBS_COMMAND_ERROR) {
2897 /*
2898 * Firmware possibly timed out during login. If NO
2899 * retries are left to do then the device is declared
2900 * dead.
2901 */
2902 *next_loopid = fcport->loop_id;
Andrew Vasquez1c7c6352005-07-06 10:30:57 -07002903 ha->isp_ops.fabric_logout(ha, fcport->loop_id,
2904 fcport->d_id.b.domain, fcport->d_id.b.area,
2905 fcport->d_id.b.al_pa);
andrew.vasquez@qlogic.comd97994d2006-01-20 14:53:13 -08002906 qla2x00_mark_device_lost(ha, fcport, 1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002907
2908 rval = 1;
2909 break;
2910 } else {
2911 /*
2912 * unrecoverable / not handled error
2913 */
2914 DEBUG2(printk("%s(%ld): failed=%x port_id=%02x%02x%02x "
Andrew Vasquezfa2a1ce2005-07-06 10:32:07 -07002915 "loop_id=%x jiffies=%lx.\n",
2916 __func__, ha->host_no, mb[0],
Linus Torvalds1da177e2005-04-16 15:20:36 -07002917 fcport->d_id.b.domain, fcport->d_id.b.area,
2918 fcport->d_id.b.al_pa, fcport->loop_id, jiffies));
2919
2920 *next_loopid = fcport->loop_id;
Andrew Vasquez1c7c6352005-07-06 10:30:57 -07002921 ha->isp_ops.fabric_logout(ha, fcport->loop_id,
2922 fcport->d_id.b.domain, fcport->d_id.b.area,
2923 fcport->d_id.b.al_pa);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002924 fcport->loop_id = FC_NO_LOOP_ID;
Andrew Vasquez0eedfcf2005-10-27 11:09:38 -07002925 fcport->login_retry = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002926
2927 rval = 3;
2928 break;
2929 }
2930 }
2931
2932 return (rval);
2933}
2934
2935/*
2936 * qla2x00_local_device_login
2937 * Issue local device login command.
2938 *
2939 * Input:
2940 * ha = adapter block pointer.
2941 * loop_id = loop id of device to login to.
2942 *
2943 * Returns (Where's the #define!!!!):
2944 * 0 - Login successfully
2945 * 1 - Login failed
2946 * 3 - Fatal error
2947 */
2948int
andrew.vasquez@qlogic.com9a52a572006-03-09 14:27:44 -08002949qla2x00_local_device_login(scsi_qla_host_t *ha, fc_port_t *fcport)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002950{
2951 int rval;
2952 uint16_t mb[MAILBOX_REGISTER_COUNT];
2953
2954 memset(mb, 0, sizeof(mb));
andrew.vasquez@qlogic.com9a52a572006-03-09 14:27:44 -08002955 rval = qla2x00_login_local_device(ha, fcport, mb, BIT_0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002956 if (rval == QLA_SUCCESS) {
2957 /* Interrogate mailbox registers for any errors */
2958 if (mb[0] == MBS_COMMAND_ERROR)
2959 rval = 1;
2960 else if (mb[0] == MBS_COMMAND_PARAMETER_ERROR)
2961 /* device not in PCB table */
2962 rval = 3;
2963 }
2964
2965 return (rval);
2966}
2967
2968/*
2969 * qla2x00_loop_resync
2970 * Resync with fibre channel devices.
2971 *
2972 * Input:
2973 * ha = adapter block pointer.
2974 *
2975 * Returns:
2976 * 0 = success
2977 */
2978int
Andrew Vasquezfa2a1ce2005-07-06 10:32:07 -07002979qla2x00_loop_resync(scsi_qla_host_t *ha)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002980{
2981 int rval;
2982 uint32_t wait_time;
2983
2984 rval = QLA_SUCCESS;
2985
2986 atomic_set(&ha->loop_state, LOOP_UPDATE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002987 clear_bit(ISP_ABORT_RETRY, &ha->dpc_flags);
2988 if (ha->flags.online) {
2989 if (!(rval = qla2x00_fw_ready(ha))) {
2990 /* Wait at most MAX_TARGET RSCNs for a stable link. */
2991 wait_time = 256;
2992 do {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002993 atomic_set(&ha->loop_state, LOOP_UPDATE);
2994
Andrew Vasquez01071092005-07-06 10:31:37 -07002995 /* Issue a marker after FW becomes ready. */
2996 qla2x00_marker(ha, 0, 0, MK_SYNC_ALL);
2997 ha->marker_needed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002998
2999 /* Remap devices on Loop. */
3000 clear_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags);
3001
3002 qla2x00_configure_loop(ha);
3003 wait_time--;
3004 } while (!atomic_read(&ha->loop_down_timer) &&
3005 !(test_bit(ISP_ABORT_NEEDED, &ha->dpc_flags)) &&
3006 wait_time &&
3007 (test_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags)));
3008 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003009 }
3010
3011 if (test_bit(ISP_ABORT_NEEDED, &ha->dpc_flags)) {
3012 return (QLA_FUNCTION_FAILED);
3013 }
3014
3015 if (rval) {
3016 DEBUG2_3(printk("%s(): **** FAILED ****\n", __func__));
3017 }
3018
3019 return (rval);
3020}
3021
Linus Torvalds1da177e2005-04-16 15:20:36 -07003022void
3023qla2x00_rescan_fcports(scsi_qla_host_t *ha)
3024{
3025 int rescan_done;
3026 fc_port_t *fcport;
3027
3028 rescan_done = 0;
3029 list_for_each_entry(fcport, &ha->fcports, list) {
3030 if ((fcport->flags & FCF_RESCAN_NEEDED) == 0)
3031 continue;
3032
3033 qla2x00_update_fcport(ha, fcport);
3034 fcport->flags &= ~FCF_RESCAN_NEEDED;
3035
3036 rescan_done = 1;
3037 }
Andrew Vasquezfa2a1ce2005-07-06 10:32:07 -07003038 qla2x00_probe_for_all_luns(ha);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003039}
3040
andrew.vasquez@qlogic.comd97994d2006-01-20 14:53:13 -08003041void
3042qla2x00_update_fcports(scsi_qla_host_t *ha)
3043{
3044 fc_port_t *fcport;
3045
3046 /* Go with deferred removal of rport references. */
3047 list_for_each_entry(fcport, &ha->fcports, list)
3048 if (fcport->drport)
3049 qla2x00_rport_del(fcport);
3050}
3051
Linus Torvalds1da177e2005-04-16 15:20:36 -07003052/*
3053* qla2x00_abort_isp
3054* Resets ISP and aborts all outstanding commands.
3055*
3056* Input:
3057* ha = adapter block pointer.
3058*
3059* Returns:
3060* 0 = success
3061*/
3062int
3063qla2x00_abort_isp(scsi_qla_host_t *ha)
3064{
3065 unsigned long flags = 0;
3066 uint16_t cnt;
3067 srb_t *sp;
3068 uint8_t status = 0;
3069
3070 if (ha->flags.online) {
3071 ha->flags.online = 0;
3072 clear_bit(ISP_ABORT_NEEDED, &ha->dpc_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003073
3074 qla_printk(KERN_INFO, ha,
3075 "Performing ISP error recovery - ha= %p.\n", ha);
Andrew Vasquezabbd8872005-07-06 10:30:05 -07003076 ha->isp_ops.reset_chip(ha);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003077
3078 atomic_set(&ha->loop_down_timer, LOOP_DOWN_TIME);
3079 if (atomic_read(&ha->loop_state) != LOOP_DOWN) {
3080 atomic_set(&ha->loop_state, LOOP_DOWN);
andrew.vasquez@qlogic.comd97994d2006-01-20 14:53:13 -08003081 qla2x00_mark_all_devices_lost(ha, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003082 } else {
3083 if (!atomic_read(&ha->loop_down_timer))
3084 atomic_set(&ha->loop_down_timer,
3085 LOOP_DOWN_TIME);
3086 }
3087
3088 spin_lock_irqsave(&ha->hardware_lock, flags);
3089 /* Requeue all commands in outstanding command list. */
3090 for (cnt = 1; cnt < MAX_OUTSTANDING_COMMANDS; cnt++) {
3091 sp = ha->outstanding_cmds[cnt];
3092 if (sp) {
3093 ha->outstanding_cmds[cnt] = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003094 sp->flags = 0;
f4f051e2005-04-17 15:02:26 -05003095 sp->cmd->result = DID_RESET << 16;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003096 sp->cmd->host_scribble = (unsigned char *)NULL;
f4f051e2005-04-17 15:02:26 -05003097 qla2x00_sp_compl(ha, sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003098 }
3099 }
3100 spin_unlock_irqrestore(&ha->hardware_lock, flags);
3101
Andrew Vasquezabbd8872005-07-06 10:30:05 -07003102 ha->isp_ops.nvram_config(ha);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003103
3104 if (!qla2x00_restart_isp(ha)) {
3105 clear_bit(RESET_MARKER_NEEDED, &ha->dpc_flags);
3106
3107 if (!atomic_read(&ha->loop_down_timer)) {
3108 /*
3109 * Issue marker command only when we are going
3110 * to start the I/O .
3111 */
3112 ha->marker_needed = 1;
3113 }
3114
3115 ha->flags.online = 1;
3116
Andrew Vasquezabbd8872005-07-06 10:30:05 -07003117 ha->isp_ops.enable_intrs(ha);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003118
Andrew Vasquezfa2a1ce2005-07-06 10:32:07 -07003119 ha->isp_abort_cnt = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003120 clear_bit(ISP_ABORT_RETRY, &ha->dpc_flags);
3121 } else { /* failed the ISP abort */
3122 ha->flags.online = 1;
3123 if (test_bit(ISP_ABORT_RETRY, &ha->dpc_flags)) {
3124 if (ha->isp_abort_cnt == 0) {
3125 qla_printk(KERN_WARNING, ha,
3126 "ISP error recovery failed - "
3127 "board disabled\n");
Andrew Vasquezfa2a1ce2005-07-06 10:32:07 -07003128 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003129 * The next call disables the board
3130 * completely.
3131 */
Andrew Vasquezabbd8872005-07-06 10:30:05 -07003132 ha->isp_ops.reset_adapter(ha);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003133 ha->flags.online = 0;
3134 clear_bit(ISP_ABORT_RETRY,
3135 &ha->dpc_flags);
3136 status = 0;
3137 } else { /* schedule another ISP abort */
3138 ha->isp_abort_cnt--;
3139 DEBUG(printk("qla%ld: ISP abort - "
Andrew Vasquez01071092005-07-06 10:31:37 -07003140 "retry remaining %d\n",
Andrew Vasquez744f11fd2006-06-23 16:11:05 -07003141 ha->host_no, ha->isp_abort_cnt));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003142 status = 1;
3143 }
3144 } else {
3145 ha->isp_abort_cnt = MAX_RETRIES_OF_ISP_ABORT;
3146 DEBUG(printk("qla2x00(%ld): ISP error recovery "
3147 "- retrying (%d) more times\n",
Andrew Vasquez744f11fd2006-06-23 16:11:05 -07003148 ha->host_no, ha->isp_abort_cnt));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003149 set_bit(ISP_ABORT_RETRY, &ha->dpc_flags);
3150 status = 1;
3151 }
3152 }
Andrew Vasquezfa2a1ce2005-07-06 10:32:07 -07003153
Linus Torvalds1da177e2005-04-16 15:20:36 -07003154 }
3155
3156 if (status) {
3157 qla_printk(KERN_INFO, ha,
3158 "qla2x00_abort_isp: **** FAILED ****\n");
3159 } else {
3160 DEBUG(printk(KERN_INFO
3161 "qla2x00_abort_isp(%ld): exiting.\n",
Andrew Vasquez744f11fd2006-06-23 16:11:05 -07003162 ha->host_no));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003163 }
3164
3165 return(status);
3166}
3167
3168/*
3169* qla2x00_restart_isp
3170* restarts the ISP after a reset
3171*
3172* Input:
3173* ha = adapter block pointer.
3174*
3175* Returns:
3176* 0 = success
3177*/
3178static int
3179qla2x00_restart_isp(scsi_qla_host_t *ha)
3180{
3181 uint8_t status = 0;
Andrew Vasquez3d716442005-07-06 10:30:26 -07003182 struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003183 unsigned long flags = 0;
3184 uint32_t wait_time;
3185
3186 /* If firmware needs to be loaded */
3187 if (qla2x00_isp_firmware(ha)) {
3188 ha->flags.online = 0;
Andrew Vasquezabbd8872005-07-06 10:30:05 -07003189 if (!(status = ha->isp_ops.chip_diag(ha))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003190 if (IS_QLA2100(ha) || IS_QLA2200(ha)) {
3191 status = qla2x00_setup_chip(ha);
3192 goto done;
3193 }
3194
Linus Torvalds1da177e2005-04-16 15:20:36 -07003195 spin_lock_irqsave(&ha->hardware_lock, flags);
3196
andrew.vasquez@qlogic.com044cc6c2006-03-09 14:27:13 -08003197 if (!IS_QLA24XX(ha) && !IS_QLA54XX(ha)) {
Andrew Vasquez01071092005-07-06 10:31:37 -07003198 /*
3199 * Disable SRAM, Instruction RAM and GP RAM
3200 * parity.
3201 */
3202 WRT_REG_WORD(&reg->hccr,
3203 (HCCR_ENABLE_PARITY + 0x0));
3204 RD_REG_WORD(&reg->hccr);
3205 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003206
3207 spin_unlock_irqrestore(&ha->hardware_lock, flags);
Andrew Vasquezfa2a1ce2005-07-06 10:32:07 -07003208
Linus Torvalds1da177e2005-04-16 15:20:36 -07003209 status = qla2x00_setup_chip(ha);
3210
3211 spin_lock_irqsave(&ha->hardware_lock, flags);
Andrew Vasquezfa2a1ce2005-07-06 10:32:07 -07003212
andrew.vasquez@qlogic.com044cc6c2006-03-09 14:27:13 -08003213 if (!IS_QLA24XX(ha) && !IS_QLA54XX(ha)) {
Andrew Vasquez01071092005-07-06 10:31:37 -07003214 /* Enable proper parity */
3215 if (IS_QLA2300(ha))
3216 /* SRAM parity */
3217 WRT_REG_WORD(&reg->hccr,
3218 (HCCR_ENABLE_PARITY + 0x1));
3219 else
3220 /*
3221 * SRAM, Instruction RAM and GP RAM
3222 * parity.
3223 */
3224 WRT_REG_WORD(&reg->hccr,
3225 (HCCR_ENABLE_PARITY + 0x7));
3226 RD_REG_WORD(&reg->hccr);
3227 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003228
3229 spin_unlock_irqrestore(&ha->hardware_lock, flags);
3230 }
3231 }
3232
3233 done:
3234 if (!status && !(status = qla2x00_init_rings(ha))) {
3235 clear_bit(RESET_MARKER_NEEDED, &ha->dpc_flags);
3236 if (!(status = qla2x00_fw_ready(ha))) {
3237 DEBUG(printk("%s(): Start configure loop, "
Andrew Vasquez744f11fd2006-06-23 16:11:05 -07003238 "status = %d\n", __func__, status));
Andrew Vasquez01071092005-07-06 10:31:37 -07003239
3240 /* Issue a marker after FW becomes ready. */
3241 qla2x00_marker(ha, 0, 0, MK_SYNC_ALL);
3242
Linus Torvalds1da177e2005-04-16 15:20:36 -07003243 ha->flags.online = 1;
3244 /* Wait at most MAX_TARGET RSCNs for a stable link. */
3245 wait_time = 256;
3246 do {
3247 clear_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags);
3248 qla2x00_configure_loop(ha);
3249 wait_time--;
3250 } while (!atomic_read(&ha->loop_down_timer) &&
3251 !(test_bit(ISP_ABORT_NEEDED, &ha->dpc_flags)) &&
3252 wait_time &&
3253 (test_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags)));
3254 }
3255
3256 /* if no cable then assume it's good */
Andrew Vasquezfa2a1ce2005-07-06 10:32:07 -07003257 if ((ha->device_flags & DFLG_NO_CABLE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003258 status = 0;
3259
3260 DEBUG(printk("%s(): Configure loop done, status = 0x%x\n",
3261 __func__,
Andrew Vasquez744f11fd2006-06-23 16:11:05 -07003262 status));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003263 }
3264 return (status);
3265}
3266
3267/*
3268* qla2x00_reset_adapter
3269* Reset adapter.
3270*
3271* Input:
3272* ha = adapter block pointer.
3273*/
Andrew Vasquezabbd8872005-07-06 10:30:05 -07003274void
Linus Torvalds1da177e2005-04-16 15:20:36 -07003275qla2x00_reset_adapter(scsi_qla_host_t *ha)
3276{
3277 unsigned long flags = 0;
Andrew Vasquez3d716442005-07-06 10:30:26 -07003278 struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003279
3280 ha->flags.online = 0;
Andrew Vasquezabbd8872005-07-06 10:30:05 -07003281 ha->isp_ops.disable_intrs(ha);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003282
Linus Torvalds1da177e2005-04-16 15:20:36 -07003283 spin_lock_irqsave(&ha->hardware_lock, flags);
3284 WRT_REG_WORD(&reg->hccr, HCCR_RESET_RISC);
3285 RD_REG_WORD(&reg->hccr); /* PCI Posting. */
3286 WRT_REG_WORD(&reg->hccr, HCCR_RELEASE_RISC);
3287 RD_REG_WORD(&reg->hccr); /* PCI Posting. */
3288 spin_unlock_irqrestore(&ha->hardware_lock, flags);
3289}
Andrew Vasquez01071092005-07-06 10:31:37 -07003290
3291void
3292qla24xx_reset_adapter(scsi_qla_host_t *ha)
3293{
3294 unsigned long flags = 0;
3295 struct device_reg_24xx __iomem *reg = &ha->iobase->isp24;
3296
3297 ha->flags.online = 0;
3298 ha->isp_ops.disable_intrs(ha);
3299
3300 spin_lock_irqsave(&ha->hardware_lock, flags);
3301 WRT_REG_DWORD(&reg->hccr, HCCRX_SET_RISC_RESET);
3302 RD_REG_DWORD(&reg->hccr);
3303 WRT_REG_DWORD(&reg->hccr, HCCRX_REL_RISC_PAUSE);
3304 RD_REG_DWORD(&reg->hccr);
3305 spin_unlock_irqrestore(&ha->hardware_lock, flags);
3306}
3307
3308int
3309qla24xx_nvram_config(scsi_qla_host_t *ha)
3310{
3311 int rval;
3312 struct init_cb_24xx *icb;
3313 struct nvram_24xx *nv;
3314 uint32_t *dptr;
3315 uint8_t *dptr1, *dptr2;
3316 uint32_t chksum;
3317 uint16_t cnt;
3318
3319 rval = QLA_SUCCESS;
3320 icb = (struct init_cb_24xx *)ha->init_cb;
3321 nv = (struct nvram_24xx *)ha->request_ring;
3322
3323 /* Determine NVRAM starting address. */
3324 ha->nvram_size = sizeof(struct nvram_24xx);
3325 ha->nvram_base = FA_NVRAM_FUNC0_ADDR;
andrew.vasquez@qlogic.com6f641792006-03-09 14:27:34 -08003326 ha->vpd_size = FA_NVRAM_VPD_SIZE;
3327 ha->vpd_base = FA_NVRAM_VPD0_ADDR;
3328 if (PCI_FUNC(ha->pdev->devfn)) {
Andrew Vasquez01071092005-07-06 10:31:37 -07003329 ha->nvram_base = FA_NVRAM_FUNC1_ADDR;
andrew.vasquez@qlogic.com6f641792006-03-09 14:27:34 -08003330 ha->vpd_base = FA_NVRAM_VPD1_ADDR;
3331 }
Andrew Vasquez01071092005-07-06 10:31:37 -07003332
3333 /* Get NVRAM data and calculate checksum. */
3334 dptr = (uint32_t *)nv;
3335 ha->isp_ops.read_nvram(ha, (uint8_t *)dptr, ha->nvram_base,
3336 ha->nvram_size);
3337 for (cnt = 0, chksum = 0; cnt < ha->nvram_size >> 2; cnt++)
3338 chksum += le32_to_cpu(*dptr++);
3339
3340 DEBUG5(printk("scsi(%ld): Contents of NVRAM\n", ha->host_no));
3341 DEBUG5(qla2x00_dump_buffer((uint8_t *)ha->request_ring,
3342 ha->nvram_size));
3343
3344 /* Bad NVRAM data, set defaults parameters. */
3345 if (chksum || nv->id[0] != 'I' || nv->id[1] != 'S' || nv->id[2] != 'P'
3346 || nv->id[3] != ' ' ||
3347 nv->nvram_version < __constant_cpu_to_le16(ICB_VERSION)) {
3348 /* Reset NVRAM data. */
3349 qla_printk(KERN_WARNING, ha, "Inconsistent NVRAM detected: "
3350 "checksum=0x%x id=%c version=0x%x.\n", chksum, nv->id[0],
3351 le16_to_cpu(nv->nvram_version));
3352 qla_printk(KERN_WARNING, ha, "Falling back to functioning (yet "
3353 "invalid -- WWPN) defaults.\n");
3354
3355 /*
3356 * Set default initialization control block.
3357 */
3358 memset(nv, 0, ha->nvram_size);
3359 nv->nvram_version = __constant_cpu_to_le16(ICB_VERSION);
3360 nv->version = __constant_cpu_to_le16(ICB_VERSION);
3361 nv->frame_payload_size = __constant_cpu_to_le16(2048);
3362 nv->execution_throttle = __constant_cpu_to_le16(0xFFFF);
3363 nv->exchange_count = __constant_cpu_to_le16(0);
3364 nv->hard_address = __constant_cpu_to_le16(124);
3365 nv->port_name[0] = 0x21;
3366 nv->port_name[1] = 0x00 + PCI_FUNC(ha->pdev->devfn);
3367 nv->port_name[2] = 0x00;
3368 nv->port_name[3] = 0xe0;
3369 nv->port_name[4] = 0x8b;
3370 nv->port_name[5] = 0x1c;
3371 nv->port_name[6] = 0x55;
3372 nv->port_name[7] = 0x86;
3373 nv->node_name[0] = 0x20;
3374 nv->node_name[1] = 0x00;
3375 nv->node_name[2] = 0x00;
3376 nv->node_name[3] = 0xe0;
3377 nv->node_name[4] = 0x8b;
3378 nv->node_name[5] = 0x1c;
3379 nv->node_name[6] = 0x55;
3380 nv->node_name[7] = 0x86;
3381 nv->login_retry_count = __constant_cpu_to_le16(8);
Andrew Vasquez01071092005-07-06 10:31:37 -07003382 nv->interrupt_delay_timer = __constant_cpu_to_le16(0);
3383 nv->login_timeout = __constant_cpu_to_le16(0);
3384 nv->firmware_options_1 =
3385 __constant_cpu_to_le32(BIT_14|BIT_13|BIT_2|BIT_1);
3386 nv->firmware_options_2 = __constant_cpu_to_le32(2 << 4);
3387 nv->firmware_options_2 |= __constant_cpu_to_le32(BIT_12);
3388 nv->firmware_options_3 = __constant_cpu_to_le32(2 << 13);
3389 nv->host_p = __constant_cpu_to_le32(BIT_11|BIT_10);
3390 nv->efi_parameters = __constant_cpu_to_le32(0);
3391 nv->reset_delay = 5;
3392 nv->max_luns_per_target = __constant_cpu_to_le16(128);
3393 nv->port_down_retry_count = __constant_cpu_to_le16(30);
3394 nv->link_down_timeout = __constant_cpu_to_le16(30);
3395
3396 rval = 1;
3397 }
3398
3399 /* Reset Initialization control block */
3400 memset(icb, 0, sizeof(struct init_cb_24xx));
3401
3402 /* Copy 1st segment. */
3403 dptr1 = (uint8_t *)icb;
3404 dptr2 = (uint8_t *)&nv->version;
3405 cnt = (uint8_t *)&icb->response_q_inpointer - (uint8_t *)&icb->version;
3406 while (cnt--)
3407 *dptr1++ = *dptr2++;
3408
3409 icb->login_retry_count = nv->login_retry_count;
Andrew Vasquez3ea66e22006-06-23 16:11:27 -07003410 icb->link_down_on_nos = nv->link_down_on_nos;
Andrew Vasquez01071092005-07-06 10:31:37 -07003411
3412 /* Copy 2nd segment. */
3413 dptr1 = (uint8_t *)&icb->interrupt_delay_timer;
3414 dptr2 = (uint8_t *)&nv->interrupt_delay_timer;
3415 cnt = (uint8_t *)&icb->reserved_3 -
3416 (uint8_t *)&icb->interrupt_delay_timer;
3417 while (cnt--)
3418 *dptr1++ = *dptr2++;
3419
3420 /*
3421 * Setup driver NVRAM options.
3422 */
3423 if (memcmp(nv->model_name, BINZERO, sizeof(nv->model_name)) != 0) {
3424 char *st, *en;
3425 uint16_t index;
3426
3427 strncpy(ha->model_number, nv->model_name,
3428 sizeof(nv->model_name));
3429 st = en = ha->model_number;
3430 en += sizeof(nv->model_name) - 1;
3431 while (en > st) {
3432 if (*en != 0x20 && *en != 0x00)
3433 break;
3434 *en-- = '\0';
3435 }
3436
3437 index = (ha->pdev->subsystem_device & 0xff);
3438 if (index < QLA_MODEL_NAMES)
andrew.vasquez@qlogic.com13289622006-03-09 14:27:24 -08003439 ha->model_desc = qla2x00_model_name[index * 2 + 1];
Andrew Vasquez01071092005-07-06 10:31:37 -07003440 } else
3441 strcpy(ha->model_number, "QLA2462");
3442
Andrew Vasquez5341e862006-05-17 15:09:16 -07003443 /* Use alternate WWN? */
3444 if (nv->host_p & __constant_cpu_to_le32(BIT_15)) {
3445 memcpy(icb->node_name, nv->alternate_node_name, WWN_SIZE);
3446 memcpy(icb->port_name, nv->alternate_port_name, WWN_SIZE);
3447 }
3448
Andrew Vasquez01071092005-07-06 10:31:37 -07003449 /* Prepare nodename */
Andrew Vasquezfd0e7e42006-05-17 15:09:11 -07003450 if ((icb->firmware_options_1 & __constant_cpu_to_le32(BIT_14)) == 0) {
Andrew Vasquez01071092005-07-06 10:31:37 -07003451 /*
3452 * Firmware will apply the following mask if the nodename was
3453 * not provided.
3454 */
3455 memcpy(icb->node_name, icb->port_name, WWN_SIZE);
3456 icb->node_name[0] &= 0xF0;
3457 }
3458
3459 /* Set host adapter parameters. */
3460 ha->flags.disable_risc_code_load = 0;
3461 ha->flags.enable_lip_reset = 1;
3462 ha->flags.enable_lip_full_login = 1;
3463 ha->flags.enable_target_reset = 1;
3464 ha->flags.enable_led_scheme = 0;
Andrew Vasquezd4c760c2006-06-23 16:10:39 -07003465 ha->flags.disable_serdes = le32_to_cpu(nv->host_p) & BIT_5 ? 1: 0;
Andrew Vasquez01071092005-07-06 10:31:37 -07003466
Andrew Vasquezfd0e7e42006-05-17 15:09:11 -07003467 ha->operating_mode = (le32_to_cpu(icb->firmware_options_2) &
3468 (BIT_6 | BIT_5 | BIT_4)) >> 4;
Andrew Vasquez01071092005-07-06 10:31:37 -07003469
3470 memcpy(ha->fw_seriallink_options24, nv->seriallink_options,
3471 sizeof(ha->fw_seriallink_options24));
3472
3473 /* save HBA serial number */
3474 ha->serial0 = icb->port_name[5];
3475 ha->serial1 = icb->port_name[6];
3476 ha->serial2 = icb->port_name[7];
3477 ha->node_name = icb->node_name;
3478 ha->port_name = icb->port_name;
3479
andrew.vasquez@qlogic.combc8fb3c2006-01-13 17:05:42 -08003480 icb->execution_throttle = __constant_cpu_to_le16(0xFFFF);
3481
Andrew Vasquez01071092005-07-06 10:31:37 -07003482 ha->retry_count = le16_to_cpu(nv->login_retry_count);
3483
3484 /* Set minimum login_timeout to 4 seconds. */
3485 if (le16_to_cpu(nv->login_timeout) < ql2xlogintimeout)
3486 nv->login_timeout = cpu_to_le16(ql2xlogintimeout);
3487 if (le16_to_cpu(nv->login_timeout) < 4)
3488 nv->login_timeout = __constant_cpu_to_le16(4);
3489 ha->login_timeout = le16_to_cpu(nv->login_timeout);
3490 icb->login_timeout = cpu_to_le16(nv->login_timeout);
3491
3492 /* Set minimum RATOV to 200 tenths of a second. */
3493 ha->r_a_tov = 200;
3494
3495 ha->loop_reset_delay = nv->reset_delay;
3496
3497 /* Link Down Timeout = 0:
3498 *
3499 * When Port Down timer expires we will start returning
3500 * I/O's to OS with "DID_NO_CONNECT".
3501 *
3502 * Link Down Timeout != 0:
3503 *
3504 * The driver waits for the link to come up after link down
3505 * before returning I/Os to OS with "DID_NO_CONNECT".
3506 */
3507 if (le16_to_cpu(nv->link_down_timeout) == 0) {
3508 ha->loop_down_abort_time =
3509 (LOOP_DOWN_TIME - LOOP_DOWN_TIMEOUT);
3510 } else {
3511 ha->link_down_timeout = le16_to_cpu(nv->link_down_timeout);
3512 ha->loop_down_abort_time =
3513 (LOOP_DOWN_TIME - ha->link_down_timeout);
3514 }
3515
3516 /* Need enough time to try and get the port back. */
3517 ha->port_down_retry_count = le16_to_cpu(nv->port_down_retry_count);
3518 if (qlport_down_retry)
3519 ha->port_down_retry_count = qlport_down_retry;
3520
3521 /* Set login_retry_count */
3522 ha->login_retry_count = le16_to_cpu(nv->login_retry_count);
3523 if (ha->port_down_retry_count ==
3524 le16_to_cpu(nv->port_down_retry_count) &&
3525 ha->port_down_retry_count > 3)
3526 ha->login_retry_count = ha->port_down_retry_count;
3527 else if (ha->port_down_retry_count > (int)ha->login_retry_count)
3528 ha->login_retry_count = ha->port_down_retry_count;
3529 if (ql2xloginretrycount)
3530 ha->login_retry_count = ql2xloginretrycount;
3531
Andrew Vasquez4fdfefe2005-10-27 11:09:48 -07003532 /* Enable ZIO. */
3533 if (!ha->flags.init_done) {
3534 ha->zio_mode = le32_to_cpu(icb->firmware_options_2) &
3535 (BIT_3 | BIT_2 | BIT_1 | BIT_0);
3536 ha->zio_timer = le16_to_cpu(icb->interrupt_delay_timer) ?
3537 le16_to_cpu(icb->interrupt_delay_timer): 2;
3538 }
3539 icb->firmware_options_2 &= __constant_cpu_to_le32(
3540 ~(BIT_3 | BIT_2 | BIT_1 | BIT_0));
3541 ha->flags.process_response_queue = 0;
3542 if (ha->zio_mode != QLA_ZIO_DISABLED) {
andrew.vasquez@qlogic.com4a59f712006-03-09 14:27:39 -08003543 ha->zio_mode = QLA_ZIO_MODE_6;
3544
Andrew Vasquez4fdfefe2005-10-27 11:09:48 -07003545 DEBUG2(printk("scsi(%ld): ZIO mode %d enabled; timer delay "
3546 "(%d us).\n", ha->host_no, ha->zio_mode,
3547 ha->zio_timer * 100));
3548 qla_printk(KERN_INFO, ha,
3549 "ZIO mode %d enabled; timer delay (%d us).\n",
3550 ha->zio_mode, ha->zio_timer * 100);
3551
3552 icb->firmware_options_2 |= cpu_to_le32(
3553 (uint32_t)ha->zio_mode);
3554 icb->interrupt_delay_timer = cpu_to_le16(ha->zio_timer);
3555 ha->flags.process_response_queue = 1;
3556 }
3557
Andrew Vasquez01071092005-07-06 10:31:37 -07003558 if (rval) {
3559 DEBUG2_3(printk(KERN_WARNING
3560 "scsi(%ld): NVRAM configuration failed!\n", ha->host_no));
3561 }
3562 return (rval);
3563}
3564
Andrew Vasquezd1c61902006-05-17 15:09:00 -07003565int
3566qla24xx_load_risc_flash(scsi_qla_host_t *ha, uint32_t *srisc_addr)
3567{
3568 int rval;
3569 int segments, fragment;
3570 uint32_t faddr;
3571 uint32_t *dcode, dlen;
3572 uint32_t risc_addr;
3573 uint32_t risc_size;
3574 uint32_t i;
3575
3576 rval = QLA_SUCCESS;
3577
3578 segments = FA_RISC_CODE_SEGMENTS;
3579 faddr = FA_RISC_CODE_ADDR;
3580 dcode = (uint32_t *)ha->request_ring;
3581 *srisc_addr = 0;
3582
3583 /* Validate firmware image by checking version. */
3584 qla24xx_read_flash_data(ha, dcode, faddr + 4, 4);
3585 for (i = 0; i < 4; i++)
3586 dcode[i] = be32_to_cpu(dcode[i]);
3587 if ((dcode[0] == 0xffffffff && dcode[1] == 0xffffffff &&
3588 dcode[2] == 0xffffffff && dcode[3] == 0xffffffff) ||
3589 (dcode[0] == 0 && dcode[1] == 0 && dcode[2] == 0 &&
3590 dcode[3] == 0)) {
3591 qla_printk(KERN_WARNING, ha,
3592 "Unable to verify integrity of flash firmware image!\n");
3593 qla_printk(KERN_WARNING, ha,
3594 "Firmware data: %08x %08x %08x %08x!\n", dcode[0],
3595 dcode[1], dcode[2], dcode[3]);
3596
3597 return QLA_FUNCTION_FAILED;
3598 }
3599
3600 while (segments && rval == QLA_SUCCESS) {
3601 /* Read segment's load information. */
3602 qla24xx_read_flash_data(ha, dcode, faddr, 4);
3603
3604 risc_addr = be32_to_cpu(dcode[2]);
3605 *srisc_addr = *srisc_addr == 0 ? risc_addr : *srisc_addr;
3606 risc_size = be32_to_cpu(dcode[3]);
3607
3608 fragment = 0;
3609 while (risc_size > 0 && rval == QLA_SUCCESS) {
3610 dlen = (uint32_t)(ha->fw_transfer_size >> 2);
3611 if (dlen > risc_size)
3612 dlen = risc_size;
3613
3614 DEBUG7(printk("scsi(%ld): Loading risc segment@ risc "
3615 "addr %x, number of dwords 0x%x, offset 0x%x.\n",
3616 ha->host_no, risc_addr, dlen, faddr));
3617
3618 qla24xx_read_flash_data(ha, dcode, faddr, dlen);
3619 for (i = 0; i < dlen; i++)
3620 dcode[i] = swab32(dcode[i]);
3621
3622 rval = qla2x00_load_ram(ha, ha->request_dma, risc_addr,
3623 dlen);
3624 if (rval) {
3625 DEBUG(printk("scsi(%ld):[ERROR] Failed to load "
3626 "segment %d of firmware\n", ha->host_no,
3627 fragment));
3628 qla_printk(KERN_WARNING, ha,
3629 "[ERROR] Failed to load segment %d of "
3630 "firmware\n", fragment);
3631 break;
3632 }
3633
3634 faddr += dlen;
3635 risc_addr += dlen;
3636 risc_size -= dlen;
3637 fragment++;
3638 }
3639
3640 /* Next segment. */
3641 segments--;
3642 }
3643
3644 return rval;
3645}
3646
Andrew Vasquezd1c61902006-05-17 15:09:00 -07003647#define QLA_FW_URL "ftp://ftp.qlogic.com/outgoing/linux/firmware/"
3648
Andrew Vasquez01071092005-07-06 10:31:37 -07003649int
Andrew Vasquez54333832005-11-09 15:49:04 -08003650qla2x00_load_risc(scsi_qla_host_t *ha, uint32_t *srisc_addr)
3651{
3652 int rval;
3653 int i, fragment;
3654 uint16_t *wcode, *fwcode;
3655 uint32_t risc_addr, risc_size, fwclen, wlen, *seg;
3656 struct fw_blob *blob;
3657
3658 /* Load firmware blob. */
3659 blob = qla2x00_request_firmware(ha);
3660 if (!blob) {
3661 qla_printk(KERN_ERR, ha, "Firmware image unavailable.\n");
Andrew Vasquezd1c61902006-05-17 15:09:00 -07003662 qla_printk(KERN_ERR, ha, "Firmware images can be retrieved "
3663 "from: " QLA_FW_URL ".\n");
Andrew Vasquez54333832005-11-09 15:49:04 -08003664 return QLA_FUNCTION_FAILED;
3665 }
3666
3667 rval = QLA_SUCCESS;
3668
3669 wcode = (uint16_t *)ha->request_ring;
3670 *srisc_addr = 0;
3671 fwcode = (uint16_t *)blob->fw->data;
3672 fwclen = 0;
3673
3674 /* Validate firmware image by checking version. */
3675 if (blob->fw->size < 8 * sizeof(uint16_t)) {
3676 qla_printk(KERN_WARNING, ha,
3677 "Unable to verify integrity of firmware image (%Zd)!\n",
3678 blob->fw->size);
3679 goto fail_fw_integrity;
3680 }
3681 for (i = 0; i < 4; i++)
3682 wcode[i] = be16_to_cpu(fwcode[i + 4]);
3683 if ((wcode[0] == 0xffff && wcode[1] == 0xffff && wcode[2] == 0xffff &&
3684 wcode[3] == 0xffff) || (wcode[0] == 0 && wcode[1] == 0 &&
3685 wcode[2] == 0 && wcode[3] == 0)) {
3686 qla_printk(KERN_WARNING, ha,
3687 "Unable to verify integrity of firmware image!\n");
3688 qla_printk(KERN_WARNING, ha,
3689 "Firmware data: %04x %04x %04x %04x!\n", wcode[0],
3690 wcode[1], wcode[2], wcode[3]);
3691 goto fail_fw_integrity;
3692 }
3693
3694 seg = blob->segs;
3695 while (*seg && rval == QLA_SUCCESS) {
3696 risc_addr = *seg;
3697 *srisc_addr = *srisc_addr == 0 ? *seg : *srisc_addr;
3698 risc_size = be16_to_cpu(fwcode[3]);
3699
3700 /* Validate firmware image size. */
3701 fwclen += risc_size * sizeof(uint16_t);
3702 if (blob->fw->size < fwclen) {
3703 qla_printk(KERN_WARNING, ha,
3704 "Unable to verify integrity of firmware image "
3705 "(%Zd)!\n", blob->fw->size);
3706 goto fail_fw_integrity;
3707 }
3708
3709 fragment = 0;
3710 while (risc_size > 0 && rval == QLA_SUCCESS) {
3711 wlen = (uint16_t)(ha->fw_transfer_size >> 1);
3712 if (wlen > risc_size)
3713 wlen = risc_size;
3714
3715 DEBUG7(printk("scsi(%ld): Loading risc segment@ risc "
3716 "addr %x, number of words 0x%x.\n", ha->host_no,
3717 risc_addr, wlen));
3718
3719 for (i = 0; i < wlen; i++)
3720 wcode[i] = swab16(fwcode[i]);
3721
3722 rval = qla2x00_load_ram(ha, ha->request_dma, risc_addr,
3723 wlen);
3724 if (rval) {
3725 DEBUG(printk("scsi(%ld):[ERROR] Failed to load "
3726 "segment %d of firmware\n", ha->host_no,
3727 fragment));
3728 qla_printk(KERN_WARNING, ha,
3729 "[ERROR] Failed to load segment %d of "
3730 "firmware\n", fragment);
3731 break;
3732 }
3733
3734 fwcode += wlen;
3735 risc_addr += wlen;
3736 risc_size -= wlen;
3737 fragment++;
3738 }
3739
3740 /* Next segment. */
3741 seg++;
3742 }
3743 return rval;
3744
3745fail_fw_integrity:
3746 return QLA_FUNCTION_FAILED;
3747}
3748
3749int
3750qla24xx_load_risc(scsi_qla_host_t *ha, uint32_t *srisc_addr)
Andrew Vasquez01071092005-07-06 10:31:37 -07003751{
3752 int rval;
3753 int segments, fragment;
3754 uint32_t *dcode, dlen;
3755 uint32_t risc_addr;
3756 uint32_t risc_size;
3757 uint32_t i;
Andrew Vasquez54333832005-11-09 15:49:04 -08003758 struct fw_blob *blob;
Andrew Vasquez01071092005-07-06 10:31:37 -07003759 uint32_t *fwcode, fwclen;
3760
Andrew Vasquez54333832005-11-09 15:49:04 -08003761 /* Load firmware blob. */
3762 blob = qla2x00_request_firmware(ha);
3763 if (!blob) {
3764 qla_printk(KERN_ERR, ha, "Firmware image unavailable.\n");
Andrew Vasquezd1c61902006-05-17 15:09:00 -07003765 qla_printk(KERN_ERR, ha, "Firmware images can be retrieved "
3766 "from: " QLA_FW_URL ".\n");
3767
3768 /* Try to load RISC code from flash. */
3769 qla_printk(KERN_ERR, ha, "Attempting to load (potentially "
3770 "outdated) firmware from flash.\n");
3771 return qla24xx_load_risc_flash(ha, srisc_addr);
Andrew Vasquez01071092005-07-06 10:31:37 -07003772 }
3773
3774 rval = QLA_SUCCESS;
3775
3776 segments = FA_RISC_CODE_SEGMENTS;
3777 dcode = (uint32_t *)ha->request_ring;
3778 *srisc_addr = 0;
Andrew Vasquez54333832005-11-09 15:49:04 -08003779 fwcode = (uint32_t *)blob->fw->data;
Andrew Vasquez01071092005-07-06 10:31:37 -07003780 fwclen = 0;
3781
3782 /* Validate firmware image by checking version. */
Andrew Vasquez54333832005-11-09 15:49:04 -08003783 if (blob->fw->size < 8 * sizeof(uint32_t)) {
Andrew Vasquez01071092005-07-06 10:31:37 -07003784 qla_printk(KERN_WARNING, ha,
Andrew Vasquez54333832005-11-09 15:49:04 -08003785 "Unable to verify integrity of firmware image (%Zd)!\n",
3786 blob->fw->size);
Andrew Vasquez01071092005-07-06 10:31:37 -07003787 goto fail_fw_integrity;
3788 }
3789 for (i = 0; i < 4; i++)
3790 dcode[i] = be32_to_cpu(fwcode[i + 4]);
3791 if ((dcode[0] == 0xffffffff && dcode[1] == 0xffffffff &&
3792 dcode[2] == 0xffffffff && dcode[3] == 0xffffffff) ||
3793 (dcode[0] == 0 && dcode[1] == 0 && dcode[2] == 0 &&
3794 dcode[3] == 0)) {
3795 qla_printk(KERN_WARNING, ha,
Andrew Vasquez54333832005-11-09 15:49:04 -08003796 "Unable to verify integrity of firmware image!\n");
Andrew Vasquez01071092005-07-06 10:31:37 -07003797 qla_printk(KERN_WARNING, ha,
3798 "Firmware data: %08x %08x %08x %08x!\n", dcode[0],
3799 dcode[1], dcode[2], dcode[3]);
3800 goto fail_fw_integrity;
3801 }
3802
3803 while (segments && rval == QLA_SUCCESS) {
3804 risc_addr = be32_to_cpu(fwcode[2]);
3805 *srisc_addr = *srisc_addr == 0 ? risc_addr : *srisc_addr;
3806 risc_size = be32_to_cpu(fwcode[3]);
3807
3808 /* Validate firmware image size. */
3809 fwclen += risc_size * sizeof(uint32_t);
Andrew Vasquez54333832005-11-09 15:49:04 -08003810 if (blob->fw->size < fwclen) {
Andrew Vasquez01071092005-07-06 10:31:37 -07003811 qla_printk(KERN_WARNING, ha,
Andrew Vasquez54333832005-11-09 15:49:04 -08003812 "Unable to verify integrity of firmware image "
3813 "(%Zd)!\n", blob->fw->size);
3814
Andrew Vasquez01071092005-07-06 10:31:37 -07003815 goto fail_fw_integrity;
3816 }
3817
3818 fragment = 0;
3819 while (risc_size > 0 && rval == QLA_SUCCESS) {
3820 dlen = (uint32_t)(ha->fw_transfer_size >> 2);
3821 if (dlen > risc_size)
3822 dlen = risc_size;
3823
3824 DEBUG7(printk("scsi(%ld): Loading risc segment@ risc "
3825 "addr %x, number of dwords 0x%x.\n", ha->host_no,
3826 risc_addr, dlen));
3827
3828 for (i = 0; i < dlen; i++)
3829 dcode[i] = swab32(fwcode[i]);
3830
andrew.vasquez@qlogic.com590f98e2006-01-13 17:05:37 -08003831 rval = qla2x00_load_ram(ha, ha->request_dma, risc_addr,
3832 dlen);
Andrew Vasquez01071092005-07-06 10:31:37 -07003833 if (rval) {
3834 DEBUG(printk("scsi(%ld):[ERROR] Failed to load "
3835 "segment %d of firmware\n", ha->host_no,
3836 fragment));
3837 qla_printk(KERN_WARNING, ha,
3838 "[ERROR] Failed to load segment %d of "
3839 "firmware\n", fragment);
3840 break;
3841 }
3842
3843 fwcode += dlen;
3844 risc_addr += dlen;
3845 risc_size -= dlen;
3846 fragment++;
3847 }
3848
3849 /* Next segment. */
3850 segments--;
3851 }
Andrew Vasquez01071092005-07-06 10:31:37 -07003852 return rval;
3853
3854fail_fw_integrity:
Andrew Vasquez01071092005-07-06 10:31:37 -07003855 return QLA_FUNCTION_FAILED;
Andrew Vasquez01071092005-07-06 10:31:37 -07003856}