blob: b4fbef5bf409c50ea20b9650427be1caa878f477 [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 Vasquez0107109e2005-07-06 10:31:37 -070010#include <linux/vmalloc.h>
8482e1182005-04-17 15:04:54 -050011#include <scsi/scsi_transport_fc.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070012
13#include "qla_devtbl.h"
14
15/* XXX(hch): this is ugly, but we don't want to pull in exioctl.h */
16#ifndef EXT_IS_LUN_BIT_SET
17#define EXT_IS_LUN_BIT_SET(P,L) \
18 (((P)->mask[L/8] & (0x80 >> (L%8)))?1:0)
19#define EXT_SET_LUN_BIT(P,L) \
20 ((P)->mask[L/8] |= (0x80 >> (L%8)))
21#endif
22
23/*
24* QLogic ISP2x00 Hardware Support Function Prototypes.
25*/
Linus Torvalds1da177e2005-04-16 15:20:36 -070026static int qla2x00_isp_firmware(scsi_qla_host_t *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070027static void qla2x00_resize_request_q(scsi_qla_host_t *);
28static int qla2x00_setup_chip(scsi_qla_host_t *);
29static void qla2x00_init_response_q_entries(scsi_qla_host_t *);
30static int qla2x00_init_rings(scsi_qla_host_t *);
31static int qla2x00_fw_ready(scsi_qla_host_t *);
32static int qla2x00_configure_hba(scsi_qla_host_t *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070033static int qla2x00_configure_loop(scsi_qla_host_t *);
34static int qla2x00_configure_local_loop(scsi_qla_host_t *);
35static void qla2x00_update_fcport(scsi_qla_host_t *, fc_port_t *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070036static int qla2x00_configure_fabric(scsi_qla_host_t *);
37static int qla2x00_find_all_fabric_devs(scsi_qla_host_t *, struct list_head *);
38static int qla2x00_device_resync(scsi_qla_host_t *);
39static int qla2x00_fabric_dev_login(scsi_qla_host_t *, fc_port_t *,
40 uint16_t *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
42static int qla2x00_restart_isp(scsi_qla_host_t *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070043
44/****************************************************************************/
45/* QLogic ISP2x00 Hardware Support Functions. */
46/****************************************************************************/
47
48/*
49* qla2x00_initialize_adapter
50* Initialize board.
51*
52* Input:
53* ha = adapter block pointer.
54*
55* Returns:
56* 0 = success
57*/
58int
59qla2x00_initialize_adapter(scsi_qla_host_t *ha)
60{
61 int rval;
62 uint8_t restart_risc = 0;
63 uint8_t retry;
64 uint32_t wait_time;
65
66 /* Clear adapter flags. */
67 ha->flags.online = 0;
68 ha->flags.reset_active = 0;
69 atomic_set(&ha->loop_down_timer, LOOP_DOWN_TIME);
70 atomic_set(&ha->loop_state, LOOP_DOWN);
71 ha->device_flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070072 ha->dpc_flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070073 ha->flags.management_server_logged_in = 0;
74 ha->marker_needed = 0;
75 ha->mbx_flags = 0;
76 ha->isp_abort_cnt = 0;
77 ha->beacon_blink_led = 0;
Andrew Vasquezcca53352005-08-26 19:08:30 -070078 set_bit(REGISTER_FDMI_NEEDED, &ha->dpc_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -070079
Andrew Vasquez0107109e2005-07-06 10:31:37 -070080 qla_printk(KERN_INFO, ha, "Configuring PCI space...\n");
Andrew Vasquezabbd8872005-07-06 10:30:05 -070081 rval = ha->isp_ops.pci_config(ha);
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 if (rval) {
83 DEBUG2(printk("scsi(%ld): Unable to configure PCI space=n",
84 ha->host_no));
85 return (rval);
86 }
87
Andrew Vasquezabbd8872005-07-06 10:30:05 -070088 ha->isp_ops.reset_chip(ha);
Linus Torvalds1da177e2005-04-16 15:20:36 -070089
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 qla_printk(KERN_INFO, ha, "Configure NVRAM parameters...\n");
Andrew Vasquez0107109e2005-07-06 10:31:37 -070091
Andrew Vasquezabbd8872005-07-06 10:30:05 -070092 ha->isp_ops.nvram_config(ha);
Linus Torvalds1da177e2005-04-16 15:20:36 -070093
94 qla_printk(KERN_INFO, ha, "Verifying loaded RISC code...\n");
95
96 retry = 10;
97 /*
98 * Try to configure the loop.
99 */
100 do {
101 restart_risc = 0;
102
103 /* If firmware needs to be loaded */
104 if (qla2x00_isp_firmware(ha) != QLA_SUCCESS) {
Andrew Vasquezabbd8872005-07-06 10:30:05 -0700105 if ((rval = ha->isp_ops.chip_diag(ha)) == QLA_SUCCESS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 rval = qla2x00_setup_chip(ha);
107 }
108 }
109
110 if (rval == QLA_SUCCESS &&
111 (rval = qla2x00_init_rings(ha)) == QLA_SUCCESS) {
112check_fw_ready_again:
113 /*
Andrew Vasquezfa2a1ce2005-07-06 10:32:07 -0700114 * Wait for a successful LIP up to a maximum
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 * of (in seconds): RISC login timeout value,
116 * RISC retry count value, and port down retry
Andrew Vasquezfa2a1ce2005-07-06 10:32:07 -0700117 * value OR a minimum of 4 seconds OR If no
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 * cable, only 5 seconds.
119 */
120 rval = qla2x00_fw_ready(ha);
121 if (rval == QLA_SUCCESS) {
122 clear_bit(RESET_MARKER_NEEDED, &ha->dpc_flags);
123
Andrew Vasquez0107109e2005-07-06 10:31:37 -0700124 /* Issue a marker after FW becomes ready. */
125 qla2x00_marker(ha, 0, 0, MK_SYNC_ALL);
126
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 /*
128 * Wait at most MAX_TARGET RSCNs for a stable
129 * link.
130 */
131 wait_time = 256;
132 do {
133 clear_bit(LOOP_RESYNC_NEEDED,
134 &ha->dpc_flags);
135 rval = qla2x00_configure_loop(ha);
136
137 if (test_and_clear_bit(ISP_ABORT_NEEDED,
138 &ha->dpc_flags)) {
139 restart_risc = 1;
140 break;
141 }
142
143 /*
144 * If loop state change while we were
145 * discoverying devices then wait for
146 * LIP to complete
147 */
148
Ravi Anand33135aa2005-11-08 14:37:20 -0800149 if (atomic_read(&ha->loop_state) !=
150 LOOP_READY && retry--) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 goto check_fw_ready_again;
152 }
153 wait_time--;
154 } while (!atomic_read(&ha->loop_down_timer) &&
155 retry &&
156 wait_time &&
157 (test_bit(LOOP_RESYNC_NEEDED,
158 &ha->dpc_flags)));
159
160 if (wait_time == 0)
161 rval = QLA_FUNCTION_FAILED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 } else if (ha->device_flags & DFLG_NO_CABLE)
163 /* If no cable, then all is good. */
164 rval = QLA_SUCCESS;
165 }
166 } while (restart_risc && retry--);
167
168 if (rval == QLA_SUCCESS) {
169 clear_bit(RESET_MARKER_NEEDED, &ha->dpc_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 qla2x00_marker(ha, 0, 0, MK_SYNC_ALL);
171 ha->marker_needed = 0;
172
173 ha->flags.online = 1;
174 } else {
175 DEBUG2_3(printk("%s(): **** FAILED ****\n", __func__));
176 }
177
178 return (rval);
179}
180
181/**
Andrew Vasquezabbd8872005-07-06 10:30:05 -0700182 * qla2100_pci_config() - Setup ISP21xx PCI configuration registers.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 * @ha: HA context
184 *
185 * Returns 0 on success.
186 */
Andrew Vasquezabbd8872005-07-06 10:30:05 -0700187int
188qla2100_pci_config(scsi_qla_host_t *ha)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189{
Andrew Vasquezabbd8872005-07-06 10:30:05 -0700190 uint16_t w, mwi;
Adam Kropelin27b2f672005-09-16 19:28:20 -0700191 uint32_t d;
Andrew Vasquezabbd8872005-07-06 10:30:05 -0700192 unsigned long flags;
Andrew Vasquez3d716442005-07-06 10:30:26 -0700193 struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 pci_set_master(ha->pdev);
196 mwi = 0;
197 if (pci_set_mwi(ha->pdev))
198 mwi = PCI_COMMAND_INVALIDATE;
199 pci_read_config_word(ha->pdev, PCI_REVISION_ID, &ha->revision);
200
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 pci_read_config_word(ha->pdev, PCI_COMMAND, &w);
202 w |= mwi | (PCI_COMMAND_PARITY | PCI_COMMAND_SERR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 pci_write_config_word(ha->pdev, PCI_COMMAND, w);
204
205 /* Reset expansion ROM address decode enable */
Adam Kropelin27b2f672005-09-16 19:28:20 -0700206 pci_read_config_dword(ha->pdev, PCI_ROM_ADDRESS, &d);
207 d &= ~PCI_ROM_ADDRESS_ENABLE;
208 pci_write_config_dword(ha->pdev, PCI_ROM_ADDRESS, d);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209
Andrew Vasquezabbd8872005-07-06 10:30:05 -0700210 /* Get PCI bus information. */
211 spin_lock_irqsave(&ha->hardware_lock, flags);
Andrew Vasquez3d716442005-07-06 10:30:26 -0700212 ha->pci_attr = RD_REG_WORD(&reg->ctrl_status);
Andrew Vasquezabbd8872005-07-06 10:30:05 -0700213 spin_unlock_irqrestore(&ha->hardware_lock, flags);
214
215 return QLA_SUCCESS;
216}
217
218/**
219 * qla2300_pci_config() - Setup ISP23xx PCI configuration registers.
220 * @ha: HA context
221 *
222 * Returns 0 on success.
223 */
224int
225qla2300_pci_config(scsi_qla_host_t *ha)
226{
227 uint16_t w, mwi;
Adam Kropelin27b2f672005-09-16 19:28:20 -0700228 uint32_t d;
Andrew Vasquezabbd8872005-07-06 10:30:05 -0700229 unsigned long flags = 0;
230 uint32_t cnt;
Andrew Vasquez3d716442005-07-06 10:30:26 -0700231 struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
Andrew Vasquezabbd8872005-07-06 10:30:05 -0700232
Andrew Vasquezabbd8872005-07-06 10:30:05 -0700233 pci_set_master(ha->pdev);
234 mwi = 0;
235 if (pci_set_mwi(ha->pdev))
236 mwi = PCI_COMMAND_INVALIDATE;
237 pci_read_config_word(ha->pdev, PCI_REVISION_ID, &ha->revision);
238
239 pci_read_config_word(ha->pdev, PCI_COMMAND, &w);
240 w |= mwi | (PCI_COMMAND_PARITY | PCI_COMMAND_SERR);
241
242 if (IS_QLA2322(ha) || IS_QLA6322(ha))
243 w &= ~PCI_COMMAND_INTX_DISABLE;
244
245 /*
246 * If this is a 2300 card and not 2312, reset the
247 * COMMAND_INVALIDATE due to a bug in the 2300. Unfortunately,
248 * the 2310 also reports itself as a 2300 so we need to get the
249 * fb revision level -- a 6 indicates it really is a 2300 and
250 * not a 2310.
251 */
252 if (IS_QLA2300(ha)) {
253 spin_lock_irqsave(&ha->hardware_lock, flags);
254
255 /* Pause RISC. */
Andrew Vasquez3d716442005-07-06 10:30:26 -0700256 WRT_REG_WORD(&reg->hccr, HCCR_PAUSE_RISC);
Andrew Vasquezabbd8872005-07-06 10:30:05 -0700257 for (cnt = 0; cnt < 30000; cnt++) {
Andrew Vasquez3d716442005-07-06 10:30:26 -0700258 if ((RD_REG_WORD(&reg->hccr) & HCCR_RISC_PAUSE) != 0)
Andrew Vasquezabbd8872005-07-06 10:30:05 -0700259 break;
260
261 udelay(10);
262 }
263
264 /* Select FPM registers. */
Andrew Vasquez3d716442005-07-06 10:30:26 -0700265 WRT_REG_WORD(&reg->ctrl_status, 0x20);
266 RD_REG_WORD(&reg->ctrl_status);
Andrew Vasquezabbd8872005-07-06 10:30:05 -0700267
268 /* Get the fb rev level */
Andrew Vasquez3d716442005-07-06 10:30:26 -0700269 ha->fb_rev = RD_FB_CMD_REG(ha, reg);
Andrew Vasquezabbd8872005-07-06 10:30:05 -0700270
271 if (ha->fb_rev == FPM_2300)
272 w &= ~PCI_COMMAND_INVALIDATE;
273
274 /* Deselect FPM registers. */
Andrew Vasquez3d716442005-07-06 10:30:26 -0700275 WRT_REG_WORD(&reg->ctrl_status, 0x0);
276 RD_REG_WORD(&reg->ctrl_status);
Andrew Vasquezabbd8872005-07-06 10:30:05 -0700277
278 /* Release RISC module. */
Andrew Vasquez3d716442005-07-06 10:30:26 -0700279 WRT_REG_WORD(&reg->hccr, HCCR_RELEASE_RISC);
Andrew Vasquezabbd8872005-07-06 10:30:05 -0700280 for (cnt = 0; cnt < 30000; cnt++) {
Andrew Vasquez3d716442005-07-06 10:30:26 -0700281 if ((RD_REG_WORD(&reg->hccr) & HCCR_RISC_PAUSE) == 0)
Andrew Vasquezabbd8872005-07-06 10:30:05 -0700282 break;
283
284 udelay(10);
285 }
286
287 spin_unlock_irqrestore(&ha->hardware_lock, flags);
288 }
289 pci_write_config_word(ha->pdev, PCI_COMMAND, w);
290
291 pci_write_config_byte(ha->pdev, PCI_LATENCY_TIMER, 0x80);
292
293 /* Reset expansion ROM address decode enable */
Adam Kropelin27b2f672005-09-16 19:28:20 -0700294 pci_read_config_dword(ha->pdev, PCI_ROM_ADDRESS, &d);
295 d &= ~PCI_ROM_ADDRESS_ENABLE;
296 pci_write_config_dword(ha->pdev, PCI_ROM_ADDRESS, d);
Andrew Vasquezabbd8872005-07-06 10:30:05 -0700297
298 /* Get PCI bus information. */
299 spin_lock_irqsave(&ha->hardware_lock, flags);
Andrew Vasquez3d716442005-07-06 10:30:26 -0700300 ha->pci_attr = RD_REG_WORD(&reg->ctrl_status);
Andrew Vasquezabbd8872005-07-06 10:30:05 -0700301 spin_unlock_irqrestore(&ha->hardware_lock, flags);
302
303 return QLA_SUCCESS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304}
305
306/**
Andrew Vasquez0107109e2005-07-06 10:31:37 -0700307 * qla24xx_pci_config() - Setup ISP24xx PCI configuration registers.
308 * @ha: HA context
309 *
310 * Returns 0 on success.
311 */
312int
313qla24xx_pci_config(scsi_qla_host_t *ha)
314{
315 uint16_t w, mwi;
Adam Kropelin27b2f672005-09-16 19:28:20 -0700316 uint32_t d;
Andrew Vasquez0107109e2005-07-06 10:31:37 -0700317 unsigned long flags = 0;
318 struct device_reg_24xx __iomem *reg = &ha->iobase->isp24;
319 int pcix_cmd_reg, pcie_dctl_reg;
320
321 pci_set_master(ha->pdev);
322 mwi = 0;
323 if (pci_set_mwi(ha->pdev))
324 mwi = PCI_COMMAND_INVALIDATE;
325 pci_read_config_word(ha->pdev, PCI_REVISION_ID, &ha->revision);
326
327 pci_read_config_word(ha->pdev, PCI_COMMAND, &w);
328 w |= mwi | (PCI_COMMAND_PARITY | PCI_COMMAND_SERR);
329 w &= ~PCI_COMMAND_INTX_DISABLE;
330 pci_write_config_word(ha->pdev, PCI_COMMAND, w);
331
332 pci_write_config_byte(ha->pdev, PCI_LATENCY_TIMER, 0x80);
333
334 /* PCI-X -- adjust Maximum Memory Read Byte Count (2048). */
335 pcix_cmd_reg = pci_find_capability(ha->pdev, PCI_CAP_ID_PCIX);
336 if (pcix_cmd_reg) {
337 uint16_t pcix_cmd;
338
339 pcix_cmd_reg += PCI_X_CMD;
340 pci_read_config_word(ha->pdev, pcix_cmd_reg, &pcix_cmd);
341 pcix_cmd &= ~PCI_X_CMD_MAX_READ;
342 pcix_cmd |= 0x0008;
343 pci_write_config_word(ha->pdev, pcix_cmd_reg, pcix_cmd);
344 }
345
346 /* PCIe -- adjust Maximum Read Request Size (2048). */
347 pcie_dctl_reg = pci_find_capability(ha->pdev, PCI_CAP_ID_EXP);
348 if (pcie_dctl_reg) {
349 uint16_t pcie_dctl;
350
351 pcie_dctl_reg += PCI_EXP_DEVCTL;
352 pci_read_config_word(ha->pdev, pcie_dctl_reg, &pcie_dctl);
353 pcie_dctl &= ~PCI_EXP_DEVCTL_READRQ;
354 pcie_dctl |= 0x4000;
355 pci_write_config_word(ha->pdev, pcie_dctl_reg, pcie_dctl);
356 }
357
358 /* Reset expansion ROM address decode enable */
Adam Kropelin27b2f672005-09-16 19:28:20 -0700359 pci_read_config_dword(ha->pdev, PCI_ROM_ADDRESS, &d);
360 d &= ~PCI_ROM_ADDRESS_ENABLE;
361 pci_write_config_dword(ha->pdev, PCI_ROM_ADDRESS, d);
Andrew Vasquez0107109e2005-07-06 10:31:37 -0700362
363 /* Get PCI bus information. */
364 spin_lock_irqsave(&ha->hardware_lock, flags);
365 ha->pci_attr = RD_REG_DWORD(&reg->ctrl_status);
366 spin_unlock_irqrestore(&ha->hardware_lock, flags);
367
368 return QLA_SUCCESS;
369}
370
371/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 * qla2x00_isp_firmware() - Choose firmware image.
373 * @ha: HA context
374 *
375 * Returns 0 on success.
376 */
377static int
378qla2x00_isp_firmware(scsi_qla_host_t *ha)
379{
380 int rval;
381
382 /* Assume loading risc code */
Andrew Vasquezfa2a1ce2005-07-06 10:32:07 -0700383 rval = QLA_FUNCTION_FAILED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384
385 if (ha->flags.disable_risc_code_load) {
386 DEBUG2(printk("scsi(%ld): RISC CODE NOT loaded\n",
387 ha->host_no));
388 qla_printk(KERN_INFO, ha, "RISC CODE NOT loaded\n");
389
390 /* Verify checksum of loaded RISC code. */
Andrew Vasquez1c7c6352005-07-06 10:30:57 -0700391 rval = qla2x00_verify_checksum(ha,
392 IS_QLA24XX(ha) || IS_QLA25XX(ha) ? RISC_SADDRESS :
393 *ha->brd_info->fw_info[0].fwstart);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 }
395
396 if (rval) {
397 DEBUG2_3(printk("scsi(%ld): **** Load RISC code ****\n",
398 ha->host_no));
399 }
400
401 return (rval);
402}
403
404/**
405 * qla2x00_reset_chip() - Reset ISP chip.
406 * @ha: HA context
407 *
408 * Returns 0 on success.
409 */
Andrew Vasquezabbd8872005-07-06 10:30:05 -0700410void
Andrew Vasquezfa2a1ce2005-07-06 10:32:07 -0700411qla2x00_reset_chip(scsi_qla_host_t *ha)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412{
413 unsigned long flags = 0;
Andrew Vasquez3d716442005-07-06 10:30:26 -0700414 struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 uint32_t cnt;
416 unsigned long mbx_flags = 0;
417 uint16_t cmd;
418
Andrew Vasquezabbd8872005-07-06 10:30:05 -0700419 ha->isp_ops.disable_intrs(ha);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420
421 spin_lock_irqsave(&ha->hardware_lock, flags);
422
423 /* Turn off master enable */
424 cmd = 0;
425 pci_read_config_word(ha->pdev, PCI_COMMAND, &cmd);
426 cmd &= ~PCI_COMMAND_MASTER;
427 pci_write_config_word(ha->pdev, PCI_COMMAND, cmd);
428
429 if (!IS_QLA2100(ha)) {
430 /* Pause RISC. */
431 WRT_REG_WORD(&reg->hccr, HCCR_PAUSE_RISC);
432 if (IS_QLA2200(ha) || IS_QLA2300(ha)) {
433 for (cnt = 0; cnt < 30000; cnt++) {
434 if ((RD_REG_WORD(&reg->hccr) &
435 HCCR_RISC_PAUSE) != 0)
436 break;
437 udelay(100);
438 }
439 } else {
440 RD_REG_WORD(&reg->hccr); /* PCI Posting. */
441 udelay(10);
442 }
443
444 /* Select FPM registers. */
445 WRT_REG_WORD(&reg->ctrl_status, 0x20);
446 RD_REG_WORD(&reg->ctrl_status); /* PCI Posting. */
447
448 /* FPM Soft Reset. */
449 WRT_REG_WORD(&reg->fpm_diag_config, 0x100);
450 RD_REG_WORD(&reg->fpm_diag_config); /* PCI Posting. */
451
452 /* Toggle Fpm Reset. */
453 if (!IS_QLA2200(ha)) {
454 WRT_REG_WORD(&reg->fpm_diag_config, 0x0);
455 RD_REG_WORD(&reg->fpm_diag_config); /* PCI Posting. */
456 }
457
458 /* Select frame buffer registers. */
459 WRT_REG_WORD(&reg->ctrl_status, 0x10);
460 RD_REG_WORD(&reg->ctrl_status); /* PCI Posting. */
461
462 /* Reset frame buffer FIFOs. */
463 if (IS_QLA2200(ha)) {
464 WRT_FB_CMD_REG(ha, reg, 0xa000);
465 RD_FB_CMD_REG(ha, reg); /* PCI Posting. */
466 } else {
467 WRT_FB_CMD_REG(ha, reg, 0x00fc);
468
469 /* Read back fb_cmd until zero or 3 seconds max */
470 for (cnt = 0; cnt < 3000; cnt++) {
471 if ((RD_FB_CMD_REG(ha, reg) & 0xff) == 0)
472 break;
473 udelay(100);
474 }
475 }
476
477 /* Select RISC module registers. */
478 WRT_REG_WORD(&reg->ctrl_status, 0);
479 RD_REG_WORD(&reg->ctrl_status); /* PCI Posting. */
480
481 /* Reset RISC processor. */
482 WRT_REG_WORD(&reg->hccr, HCCR_RESET_RISC);
483 RD_REG_WORD(&reg->hccr); /* PCI Posting. */
484
485 /* Release RISC processor. */
486 WRT_REG_WORD(&reg->hccr, HCCR_RELEASE_RISC);
487 RD_REG_WORD(&reg->hccr); /* PCI Posting. */
488 }
489
490 WRT_REG_WORD(&reg->hccr, HCCR_CLR_RISC_INT);
491 WRT_REG_WORD(&reg->hccr, HCCR_CLR_HOST_INT);
492
493 /* Reset ISP chip. */
494 WRT_REG_WORD(&reg->ctrl_status, CSR_ISP_SOFT_RESET);
495
496 /* Wait for RISC to recover from reset. */
497 if (IS_QLA2100(ha) || IS_QLA2200(ha) || IS_QLA2300(ha)) {
498 /*
499 * It is necessary to for a delay here since the card doesn't
500 * respond to PCI reads during a reset. On some architectures
501 * this will result in an MCA.
502 */
503 udelay(20);
504 for (cnt = 30000; cnt; cnt--) {
505 if ((RD_REG_WORD(&reg->ctrl_status) &
506 CSR_ISP_SOFT_RESET) == 0)
507 break;
508 udelay(100);
509 }
510 } else
511 udelay(10);
512
513 /* Reset RISC processor. */
514 WRT_REG_WORD(&reg->hccr, HCCR_RESET_RISC);
515
516 WRT_REG_WORD(&reg->semaphore, 0);
517
518 /* Release RISC processor. */
519 WRT_REG_WORD(&reg->hccr, HCCR_RELEASE_RISC);
520 RD_REG_WORD(&reg->hccr); /* PCI Posting. */
521
522 if (IS_QLA2100(ha) || IS_QLA2200(ha) || IS_QLA2300(ha)) {
523 for (cnt = 0; cnt < 30000; cnt++) {
524 if (!(test_bit(ABORT_ISP_ACTIVE, &ha->dpc_flags)))
525 spin_lock_irqsave(&ha->mbx_reg_lock, mbx_flags);
526
527 if (RD_MAILBOX_REG(ha, reg, 0) != MBS_BUSY) {
528 if (!(test_bit(ABORT_ISP_ACTIVE,
529 &ha->dpc_flags)))
530 spin_unlock_irqrestore(
531 &ha->mbx_reg_lock, mbx_flags);
532 break;
533 }
534
535 if (!(test_bit(ABORT_ISP_ACTIVE, &ha->dpc_flags)))
536 spin_unlock_irqrestore(&ha->mbx_reg_lock,
537 mbx_flags);
538
539 udelay(100);
540 }
541 } else
542 udelay(100);
543
544 /* Turn on master enable */
545 cmd |= PCI_COMMAND_MASTER;
546 pci_write_config_word(ha->pdev, PCI_COMMAND, cmd);
547
548 /* Disable RISC pause on FPM parity error. */
549 if (!IS_QLA2100(ha)) {
550 WRT_REG_WORD(&reg->hccr, HCCR_DISABLE_PARITY_PAUSE);
551 RD_REG_WORD(&reg->hccr); /* PCI Posting. */
552 }
553
554 spin_unlock_irqrestore(&ha->hardware_lock, flags);
555}
556
557/**
Andrew Vasquez88c26662005-07-08 17:59:26 -0700558 * qla24xx_reset_risc() - Perform full reset of ISP24xx RISC.
Andrew Vasquez0107109e2005-07-06 10:31:37 -0700559 * @ha: HA context
560 *
561 * Returns 0 on success.
562 */
Andrew Vasquez88c26662005-07-08 17:59:26 -0700563static inline void
564qla24xx_reset_risc(scsi_qla_host_t *ha)
Andrew Vasquez0107109e2005-07-06 10:31:37 -0700565{
566 unsigned long flags = 0;
567 struct device_reg_24xx __iomem *reg = &ha->iobase->isp24;
568 uint32_t cnt, d2;
Andrew Vasquez335a1cc2005-11-08 14:37:48 -0800569 uint16_t wd;
Andrew Vasquez0107109e2005-07-06 10:31:37 -0700570
Andrew Vasquez0107109e2005-07-06 10:31:37 -0700571 spin_lock_irqsave(&ha->hardware_lock, flags);
572
573 /* Reset RISC. */
574 WRT_REG_DWORD(&reg->ctrl_status, CSRX_DMA_SHUTDOWN|MWB_4096_BYTES);
575 for (cnt = 0; cnt < 30000; cnt++) {
576 if ((RD_REG_DWORD(&reg->ctrl_status) & CSRX_DMA_ACTIVE) == 0)
577 break;
578
579 udelay(10);
580 }
581
582 WRT_REG_DWORD(&reg->ctrl_status,
583 CSRX_ISP_SOFT_RESET|CSRX_DMA_SHUTDOWN|MWB_4096_BYTES);
Andrew Vasquez335a1cc2005-11-08 14:37:48 -0800584 pci_read_config_word(ha->pdev, PCI_COMMAND, &wd);
Andrew Vasquez88c26662005-07-08 17:59:26 -0700585
Andrew Vasquez335a1cc2005-11-08 14:37:48 -0800586 udelay(100);
Andrew Vasquez88c26662005-07-08 17:59:26 -0700587 /* Wait for firmware to complete NVRAM accesses. */
Andrew Vasquez88c26662005-07-08 17:59:26 -0700588 d2 = (uint32_t) RD_REG_WORD(&reg->mailbox0);
589 for (cnt = 10000 ; cnt && d2; cnt--) {
590 udelay(5);
591 d2 = (uint32_t) RD_REG_WORD(&reg->mailbox0);
592 barrier();
593 }
594
Andrew Vasquez335a1cc2005-11-08 14:37:48 -0800595 /* Wait for soft-reset to complete. */
Andrew Vasquez0107109e2005-07-06 10:31:37 -0700596 d2 = RD_REG_DWORD(&reg->ctrl_status);
597 for (cnt = 6000000 ; cnt && (d2 & CSRX_ISP_SOFT_RESET); cnt--) {
598 udelay(5);
599 d2 = RD_REG_DWORD(&reg->ctrl_status);
600 barrier();
601 }
602
603 WRT_REG_DWORD(&reg->hccr, HCCRX_SET_RISC_RESET);
604 RD_REG_DWORD(&reg->hccr);
605
606 WRT_REG_DWORD(&reg->hccr, HCCRX_REL_RISC_PAUSE);
607 RD_REG_DWORD(&reg->hccr);
608
609 WRT_REG_DWORD(&reg->hccr, HCCRX_CLR_RISC_RESET);
610 RD_REG_DWORD(&reg->hccr);
611
612 d2 = (uint32_t) RD_REG_WORD(&reg->mailbox0);
613 for (cnt = 6000000 ; cnt && d2; cnt--) {
614 udelay(5);
615 d2 = (uint32_t) RD_REG_WORD(&reg->mailbox0);
616 barrier();
617 }
618
619 spin_unlock_irqrestore(&ha->hardware_lock, flags);
620}
621
622/**
Andrew Vasquez88c26662005-07-08 17:59:26 -0700623 * qla24xx_reset_chip() - Reset ISP24xx chip.
624 * @ha: HA context
625 *
626 * Returns 0 on success.
627 */
628void
629qla24xx_reset_chip(scsi_qla_host_t *ha)
630{
631 ha->isp_ops.disable_intrs(ha);
632
633 /* Perform RISC reset. */
634 qla24xx_reset_risc(ha);
635}
636
637/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 * qla2x00_chip_diag() - Test chip for proper operation.
639 * @ha: HA context
640 *
641 * Returns 0 on success.
642 */
Andrew Vasquezabbd8872005-07-06 10:30:05 -0700643int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644qla2x00_chip_diag(scsi_qla_host_t *ha)
645{
646 int rval;
Andrew Vasquez3d716442005-07-06 10:30:26 -0700647 struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 unsigned long flags = 0;
649 uint16_t data;
650 uint32_t cnt;
651 uint16_t mb[5];
652
653 /* Assume a failed state */
654 rval = QLA_FUNCTION_FAILED;
655
656 DEBUG3(printk("scsi(%ld): Testing device at %lx.\n",
657 ha->host_no, (u_long)&reg->flash_address));
658
659 spin_lock_irqsave(&ha->hardware_lock, flags);
660
661 /* Reset ISP chip. */
662 WRT_REG_WORD(&reg->ctrl_status, CSR_ISP_SOFT_RESET);
663
664 /*
665 * We need to have a delay here since the card will not respond while
666 * in reset causing an MCA on some architectures.
667 */
668 udelay(20);
669 data = qla2x00_debounce_register(&reg->ctrl_status);
670 for (cnt = 6000000 ; cnt && (data & CSR_ISP_SOFT_RESET); cnt--) {
671 udelay(5);
672 data = RD_REG_WORD(&reg->ctrl_status);
673 barrier();
674 }
675
676 if (!cnt)
677 goto chip_diag_failed;
678
679 DEBUG3(printk("scsi(%ld): Reset register cleared by chip reset\n",
680 ha->host_no));
681
682 /* Reset RISC processor. */
683 WRT_REG_WORD(&reg->hccr, HCCR_RESET_RISC);
684 WRT_REG_WORD(&reg->hccr, HCCR_RELEASE_RISC);
685
686 /* Workaround for QLA2312 PCI parity error */
687 if (IS_QLA2100(ha) || IS_QLA2200(ha) || IS_QLA2300(ha)) {
688 data = qla2x00_debounce_register(MAILBOX_REG(ha, reg, 0));
689 for (cnt = 6000000; cnt && (data == MBS_BUSY); cnt--) {
690 udelay(5);
691 data = RD_MAILBOX_REG(ha, reg, 0);
Andrew Vasquezfa2a1ce2005-07-06 10:32:07 -0700692 barrier();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 }
694 } else
695 udelay(10);
696
697 if (!cnt)
698 goto chip_diag_failed;
699
700 /* Check product ID of chip */
701 DEBUG3(printk("scsi(%ld): Checking product ID of chip\n", ha->host_no));
702
703 mb[1] = RD_MAILBOX_REG(ha, reg, 1);
704 mb[2] = RD_MAILBOX_REG(ha, reg, 2);
705 mb[3] = RD_MAILBOX_REG(ha, reg, 3);
706 mb[4] = qla2x00_debounce_register(MAILBOX_REG(ha, reg, 4));
707 if (mb[1] != PROD_ID_1 || (mb[2] != PROD_ID_2 && mb[2] != PROD_ID_2a) ||
708 mb[3] != PROD_ID_3) {
709 qla_printk(KERN_WARNING, ha,
710 "Wrong product ID = 0x%x,0x%x,0x%x\n", mb[1], mb[2], mb[3]);
711
712 goto chip_diag_failed;
713 }
714 ha->product_id[0] = mb[1];
715 ha->product_id[1] = mb[2];
716 ha->product_id[2] = mb[3];
717 ha->product_id[3] = mb[4];
718
719 /* Adjust fw RISC transfer size */
720 if (ha->request_q_length > 1024)
721 ha->fw_transfer_size = REQUEST_ENTRY_SIZE * 1024;
722 else
723 ha->fw_transfer_size = REQUEST_ENTRY_SIZE *
724 ha->request_q_length;
725
726 if (IS_QLA2200(ha) &&
727 RD_MAILBOX_REG(ha, reg, 7) == QLA2200A_RISC_ROM_VER) {
728 /* Limit firmware transfer size with a 2200A */
729 DEBUG3(printk("scsi(%ld): Found QLA2200A chip.\n",
730 ha->host_no));
731
732 ha->fw_transfer_size = 128;
733 }
734
735 /* Wrap Incoming Mailboxes Test. */
736 spin_unlock_irqrestore(&ha->hardware_lock, flags);
737
738 DEBUG3(printk("scsi(%ld): Checking mailboxes.\n", ha->host_no));
739 rval = qla2x00_mbx_reg_test(ha);
740 if (rval) {
741 DEBUG(printk("scsi(%ld): Failed mailbox send register test\n",
742 ha->host_no));
743 qla_printk(KERN_WARNING, ha,
744 "Failed mailbox send register test\n");
745 }
746 else {
747 /* Flag a successful rval */
748 rval = QLA_SUCCESS;
749 }
750 spin_lock_irqsave(&ha->hardware_lock, flags);
751
752chip_diag_failed:
753 if (rval)
754 DEBUG2_3(printk("scsi(%ld): Chip diagnostics **** FAILED "
755 "****\n", ha->host_no));
756
757 spin_unlock_irqrestore(&ha->hardware_lock, flags);
758
759 return (rval);
760}
761
762/**
Andrew Vasquez0107109e2005-07-06 10:31:37 -0700763 * qla24xx_chip_diag() - Test ISP24xx for proper operation.
764 * @ha: HA context
765 *
766 * Returns 0 on success.
767 */
768int
769qla24xx_chip_diag(scsi_qla_host_t *ha)
770{
771 int rval;
Andrew Vasquez0107109e2005-07-06 10:31:37 -0700772
Andrew Vasquez88c26662005-07-08 17:59:26 -0700773 /* Perform RISC reset. */
774 qla24xx_reset_risc(ha);
Andrew Vasquez0107109e2005-07-06 10:31:37 -0700775
776 ha->fw_transfer_size = REQUEST_ENTRY_SIZE * 1024;
777
778 rval = qla2x00_mbx_reg_test(ha);
779 if (rval) {
780 DEBUG(printk("scsi(%ld): Failed mailbox send register test\n",
781 ha->host_no));
782 qla_printk(KERN_WARNING, ha,
783 "Failed mailbox send register test\n");
784 } else {
785 /* Flag a successful rval */
786 rval = QLA_SUCCESS;
787 }
788
789 return rval;
790}
791
792static void
793qla2x00_alloc_fw_dump(scsi_qla_host_t *ha)
794{
795 ha->fw_dumped = 0;
796 ha->fw_dump24_len = sizeof(struct qla24xx_fw_dump);
797 ha->fw_dump24_len += (ha->fw_memory_size - 0x100000) * sizeof(uint32_t);
798 ha->fw_dump24 = vmalloc(ha->fw_dump24_len);
799 if (ha->fw_dump24)
800 qla_printk(KERN_INFO, ha, "Allocated (%d KB) for firmware "
801 "dump...\n", ha->fw_dump24_len / 1024);
802 else
803 qla_printk(KERN_WARNING, ha, "Unable to allocate (%d KB) for "
804 "firmware dump!!!\n", ha->fw_dump24_len / 1024);
805}
806
807/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 * qla2x00_resize_request_q() - Resize request queue given available ISP memory.
809 * @ha: HA context
810 *
811 * Returns 0 on success.
812 */
813static void
814qla2x00_resize_request_q(scsi_qla_host_t *ha)
815{
816 int rval;
817 uint16_t fw_iocb_cnt = 0;
818 uint16_t request_q_length = REQUEST_ENTRY_CNT_2XXX_EXT_MEM;
819 dma_addr_t request_dma;
820 request_t *request_ring;
821
822 /* Valid only on recent ISPs. */
823 if (IS_QLA2100(ha) || IS_QLA2200(ha))
824 return;
825
Andrew Vasquez0107109e2005-07-06 10:31:37 -0700826 if (IS_QLA24XX(ha) || IS_QLA25XX(ha))
827 qla2x00_alloc_fw_dump(ha);
828
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 /* Retrieve IOCB counts available to the firmware. */
830 rval = qla2x00_get_resource_cnts(ha, NULL, NULL, NULL, &fw_iocb_cnt);
831 if (rval)
832 return;
833 /* No point in continuing if current settings are sufficient. */
834 if (fw_iocb_cnt < 1024)
835 return;
836 if (ha->request_q_length >= request_q_length)
837 return;
838
839 /* Attempt to claim larger area for request queue. */
840 request_ring = dma_alloc_coherent(&ha->pdev->dev,
841 (request_q_length + 1) * sizeof(request_t), &request_dma,
842 GFP_KERNEL);
843 if (request_ring == NULL)
844 return;
845
846 /* Resize successful, report extensions. */
847 qla_printk(KERN_INFO, ha, "Extended memory detected (%d KB)...\n",
848 (ha->fw_memory_size + 1) / 1024);
849 qla_printk(KERN_INFO, ha, "Resizing request queue depth "
850 "(%d -> %d)...\n", ha->request_q_length, request_q_length);
851
852 /* Clear old allocations. */
853 dma_free_coherent(&ha->pdev->dev,
854 (ha->request_q_length + 1) * sizeof(request_t), ha->request_ring,
855 ha->request_dma);
856
857 /* Begin using larger queue. */
858 ha->request_q_length = request_q_length;
859 ha->request_ring = request_ring;
860 ha->request_dma = request_dma;
861}
862
863/**
864 * qla2x00_setup_chip() - Load and start RISC firmware.
865 * @ha: HA context
866 *
867 * Returns 0 on success.
868 */
869static int
870qla2x00_setup_chip(scsi_qla_host_t *ha)
871{
Andrew Vasquez0107109e2005-07-06 10:31:37 -0700872 int rval;
873 uint32_t srisc_address = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874
875 /* Load firmware sequences */
Andrew Vasquez0107109e2005-07-06 10:31:37 -0700876 rval = ha->isp_ops.load_risc(ha, &srisc_address);
877 if (rval == QLA_SUCCESS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 DEBUG(printk("scsi(%ld): Verifying Checksum of loaded RISC "
879 "code.\n", ha->host_no));
880
Andrew Vasquez0107109e2005-07-06 10:31:37 -0700881 rval = qla2x00_verify_checksum(ha, srisc_address);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882 if (rval == QLA_SUCCESS) {
883 /* Start firmware execution. */
884 DEBUG(printk("scsi(%ld): Checksum OK, start "
885 "firmware.\n", ha->host_no));
886
Andrew Vasquez0107109e2005-07-06 10:31:37 -0700887 rval = qla2x00_execute_fw(ha, srisc_address);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888 /* Retrieve firmware information. */
889 if (rval == QLA_SUCCESS && ha->fw_major_version == 0) {
890 qla2x00_get_fw_version(ha,
891 &ha->fw_major_version,
892 &ha->fw_minor_version,
893 &ha->fw_subminor_version,
894 &ha->fw_attributes, &ha->fw_memory_size);
895 qla2x00_resize_request_q(ha);
896 }
897 } else {
898 DEBUG2(printk(KERN_INFO
899 "scsi(%ld): ISP Firmware failed checksum.\n",
900 ha->host_no));
901 }
902 }
903
904 if (rval) {
905 DEBUG2_3(printk("scsi(%ld): Setup chip **** FAILED ****.\n",
906 ha->host_no));
907 }
908
909 return (rval);
910}
911
912/**
913 * qla2x00_init_response_q_entries() - Initializes response queue entries.
914 * @ha: HA context
915 *
916 * Beginning of request ring has initialization control block already built
917 * by nvram config routine.
918 *
919 * Returns 0 on success.
920 */
921static void
922qla2x00_init_response_q_entries(scsi_qla_host_t *ha)
923{
924 uint16_t cnt;
925 response_t *pkt;
926
927 pkt = ha->response_ring_ptr;
928 for (cnt = 0; cnt < ha->response_q_length; cnt++) {
929 pkt->signature = RESPONSE_PROCESSED;
930 pkt++;
931 }
932
933}
934
935/**
936 * qla2x00_update_fw_options() - Read and process firmware options.
937 * @ha: HA context
938 *
939 * Returns 0 on success.
940 */
Andrew Vasquezabbd8872005-07-06 10:30:05 -0700941void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942qla2x00_update_fw_options(scsi_qla_host_t *ha)
943{
944 uint16_t swing, emphasis, tx_sens, rx_sens;
945
946 memset(ha->fw_options, 0, sizeof(ha->fw_options));
947 qla2x00_get_fw_options(ha, ha->fw_options);
948
949 if (IS_QLA2100(ha) || IS_QLA2200(ha))
950 return;
951
952 /* Serial Link options. */
953 DEBUG3(printk("scsi(%ld): Serial link options:\n",
954 ha->host_no));
955 DEBUG3(qla2x00_dump_buffer((uint8_t *)&ha->fw_seriallink_options,
956 sizeof(ha->fw_seriallink_options)));
957
958 ha->fw_options[1] &= ~FO1_SET_EMPHASIS_SWING;
959 if (ha->fw_seriallink_options[3] & BIT_2) {
960 ha->fw_options[1] |= FO1_SET_EMPHASIS_SWING;
961
962 /* 1G settings */
963 swing = ha->fw_seriallink_options[2] & (BIT_2 | BIT_1 | BIT_0);
964 emphasis = (ha->fw_seriallink_options[2] &
965 (BIT_4 | BIT_3)) >> 3;
966 tx_sens = ha->fw_seriallink_options[0] &
Andrew Vasquezfa2a1ce2005-07-06 10:32:07 -0700967 (BIT_3 | BIT_2 | BIT_1 | BIT_0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 rx_sens = (ha->fw_seriallink_options[0] &
969 (BIT_7 | BIT_6 | BIT_5 | BIT_4)) >> 4;
970 ha->fw_options[10] = (emphasis << 14) | (swing << 8);
971 if (IS_QLA2300(ha) || IS_QLA2312(ha) || IS_QLA6312(ha)) {
972 if (rx_sens == 0x0)
973 rx_sens = 0x3;
974 ha->fw_options[10] |= (tx_sens << 4) | rx_sens;
975 } else if (IS_QLA2322(ha) || IS_QLA6322(ha))
976 ha->fw_options[10] |= BIT_5 |
977 ((rx_sens & (BIT_1 | BIT_0)) << 2) |
978 (tx_sens & (BIT_1 | BIT_0));
979
980 /* 2G settings */
981 swing = (ha->fw_seriallink_options[2] &
982 (BIT_7 | BIT_6 | BIT_5)) >> 5;
983 emphasis = ha->fw_seriallink_options[3] & (BIT_1 | BIT_0);
984 tx_sens = ha->fw_seriallink_options[1] &
Andrew Vasquezfa2a1ce2005-07-06 10:32:07 -0700985 (BIT_3 | BIT_2 | BIT_1 | BIT_0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986 rx_sens = (ha->fw_seriallink_options[1] &
987 (BIT_7 | BIT_6 | BIT_5 | BIT_4)) >> 4;
988 ha->fw_options[11] = (emphasis << 14) | (swing << 8);
989 if (IS_QLA2300(ha) || IS_QLA2312(ha) || IS_QLA6312(ha)) {
990 if (rx_sens == 0x0)
991 rx_sens = 0x3;
992 ha->fw_options[11] |= (tx_sens << 4) | rx_sens;
993 } else if (IS_QLA2322(ha) || IS_QLA6322(ha))
994 ha->fw_options[11] |= BIT_5 |
995 ((rx_sens & (BIT_1 | BIT_0)) << 2) |
996 (tx_sens & (BIT_1 | BIT_0));
997 }
998
999 /* FCP2 options. */
1000 /* Return command IOCBs without waiting for an ABTS to complete. */
1001 ha->fw_options[3] |= BIT_13;
1002
1003 /* LED scheme. */
1004 if (ha->flags.enable_led_scheme)
1005 ha->fw_options[2] |= BIT_12;
1006
1007 /* Update firmware options. */
1008 qla2x00_set_fw_options(ha, ha->fw_options);
1009}
1010
Andrew Vasquezabbd8872005-07-06 10:30:05 -07001011void
Andrew Vasquez0107109e2005-07-06 10:31:37 -07001012qla24xx_update_fw_options(scsi_qla_host_t *ha)
1013{
1014 int rval;
1015
1016 /* Update Serial Link options. */
andrew.vasquez@qlogic.comf94097e2006-01-13 17:05:32 -08001017 if ((le16_to_cpu(ha->fw_seriallink_options24[0]) & BIT_0) == 0)
Andrew Vasquez0107109e2005-07-06 10:31:37 -07001018 return;
1019
andrew.vasquez@qlogic.comf94097e2006-01-13 17:05:32 -08001020 rval = qla2x00_set_serdes_params(ha,
1021 le16_to_cpu(ha->fw_seriallink_options24[1]),
1022 le16_to_cpu(ha->fw_seriallink_options24[2]),
1023 le16_to_cpu(ha->fw_seriallink_options24[3]));
Andrew Vasquez0107109e2005-07-06 10:31:37 -07001024 if (rval != QLA_SUCCESS) {
1025 qla_printk(KERN_WARNING, ha,
1026 "Unable to update Serial Link options (%x).\n", rval);
1027 }
1028}
1029
1030void
Andrew Vasquezabbd8872005-07-06 10:30:05 -07001031qla2x00_config_rings(struct scsi_qla_host *ha)
1032{
Andrew Vasquez3d716442005-07-06 10:30:26 -07001033 struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
Andrew Vasquezabbd8872005-07-06 10:30:05 -07001034
1035 /* Setup ring parameters in initialization control block. */
1036 ha->init_cb->request_q_outpointer = __constant_cpu_to_le16(0);
1037 ha->init_cb->response_q_inpointer = __constant_cpu_to_le16(0);
1038 ha->init_cb->request_q_length = cpu_to_le16(ha->request_q_length);
1039 ha->init_cb->response_q_length = cpu_to_le16(ha->response_q_length);
1040 ha->init_cb->request_q_address[0] = cpu_to_le32(LSD(ha->request_dma));
1041 ha->init_cb->request_q_address[1] = cpu_to_le32(MSD(ha->request_dma));
1042 ha->init_cb->response_q_address[0] = cpu_to_le32(LSD(ha->response_dma));
1043 ha->init_cb->response_q_address[1] = cpu_to_le32(MSD(ha->response_dma));
1044
1045 WRT_REG_WORD(ISP_REQ_Q_IN(ha, reg), 0);
1046 WRT_REG_WORD(ISP_REQ_Q_OUT(ha, reg), 0);
1047 WRT_REG_WORD(ISP_RSP_Q_IN(ha, reg), 0);
1048 WRT_REG_WORD(ISP_RSP_Q_OUT(ha, reg), 0);
1049 RD_REG_WORD(ISP_RSP_Q_OUT(ha, reg)); /* PCI Posting. */
1050}
1051
Andrew Vasquez0107109e2005-07-06 10:31:37 -07001052void
1053qla24xx_config_rings(struct scsi_qla_host *ha)
1054{
1055 struct device_reg_24xx __iomem *reg = &ha->iobase->isp24;
1056 struct init_cb_24xx *icb;
1057
1058 /* Setup ring parameters in initialization control block. */
1059 icb = (struct init_cb_24xx *)ha->init_cb;
1060 icb->request_q_outpointer = __constant_cpu_to_le16(0);
1061 icb->response_q_inpointer = __constant_cpu_to_le16(0);
1062 icb->request_q_length = cpu_to_le16(ha->request_q_length);
1063 icb->response_q_length = cpu_to_le16(ha->response_q_length);
1064 icb->request_q_address[0] = cpu_to_le32(LSD(ha->request_dma));
1065 icb->request_q_address[1] = cpu_to_le32(MSD(ha->request_dma));
1066 icb->response_q_address[0] = cpu_to_le32(LSD(ha->response_dma));
1067 icb->response_q_address[1] = cpu_to_le32(MSD(ha->response_dma));
1068
1069 WRT_REG_DWORD(&reg->req_q_in, 0);
1070 WRT_REG_DWORD(&reg->req_q_out, 0);
1071 WRT_REG_DWORD(&reg->rsp_q_in, 0);
1072 WRT_REG_DWORD(&reg->rsp_q_out, 0);
1073 RD_REG_DWORD(&reg->rsp_q_out);
1074}
1075
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076/**
1077 * qla2x00_init_rings() - Initializes firmware.
1078 * @ha: HA context
1079 *
1080 * Beginning of request ring has initialization control block already built
1081 * by nvram config routine.
1082 *
1083 * Returns 0 on success.
1084 */
1085static int
1086qla2x00_init_rings(scsi_qla_host_t *ha)
1087{
1088 int rval;
1089 unsigned long flags = 0;
1090 int cnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091
1092 spin_lock_irqsave(&ha->hardware_lock, flags);
1093
1094 /* Clear outstanding commands array. */
1095 for (cnt = 0; cnt < MAX_OUTSTANDING_COMMANDS; cnt++)
1096 ha->outstanding_cmds[cnt] = NULL;
1097
1098 ha->current_outstanding_cmd = 0;
1099
1100 /* Clear RSCN queue. */
1101 ha->rscn_in_ptr = 0;
1102 ha->rscn_out_ptr = 0;
1103
1104 /* Initialize firmware. */
1105 ha->request_ring_ptr = ha->request_ring;
1106 ha->req_ring_index = 0;
1107 ha->req_q_cnt = ha->request_q_length;
1108 ha->response_ring_ptr = ha->response_ring;
1109 ha->rsp_ring_index = 0;
1110
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111 /* Initialize response queue entries */
1112 qla2x00_init_response_q_entries(ha);
1113
Andrew Vasquezabbd8872005-07-06 10:30:05 -07001114 ha->isp_ops.config_rings(ha);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115
1116 spin_unlock_irqrestore(&ha->hardware_lock, flags);
1117
1118 /* Update any ISP specific firmware options before initialization. */
Andrew Vasquezabbd8872005-07-06 10:30:05 -07001119 ha->isp_ops.update_fw_options(ha);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120
1121 DEBUG(printk("scsi(%ld): Issue init firmware.\n", ha->host_no));
Andrew Vasquez0107109e2005-07-06 10:31:37 -07001122 rval = qla2x00_init_firmware(ha, ha->init_cb_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123 if (rval) {
1124 DEBUG2_3(printk("scsi(%ld): Init firmware **** FAILED ****.\n",
1125 ha->host_no));
1126 } else {
1127 DEBUG3(printk("scsi(%ld): Init firmware -- success.\n",
1128 ha->host_no));
1129 }
1130
1131 return (rval);
1132}
1133
1134/**
1135 * qla2x00_fw_ready() - Waits for firmware ready.
1136 * @ha: HA context
1137 *
1138 * Returns 0 on success.
1139 */
1140static int
1141qla2x00_fw_ready(scsi_qla_host_t *ha)
1142{
1143 int rval;
1144 unsigned long wtime, mtime;
1145 uint16_t min_wait; /* Minimum wait time if loop is down */
1146 uint16_t wait_time; /* Wait time if loop is coming ready */
1147 uint16_t fw_state;
1148
1149 rval = QLA_SUCCESS;
1150
1151 /* 20 seconds for loop down. */
Andrew Vasquezfa2a1ce2005-07-06 10:32:07 -07001152 min_wait = 20;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153
1154 /*
1155 * Firmware should take at most one RATOV to login, plus 5 seconds for
1156 * our own processing.
1157 */
1158 if ((wait_time = (ha->retry_count*ha->login_timeout) + 5) < min_wait) {
1159 wait_time = min_wait;
1160 }
1161
1162 /* Min wait time if loop down */
1163 mtime = jiffies + (min_wait * HZ);
1164
1165 /* wait time before firmware ready */
1166 wtime = jiffies + (wait_time * HZ);
1167
1168 /* Wait for ISP to finish LIP */
1169 if (!ha->flags.init_done)
1170 qla_printk(KERN_INFO, ha, "Waiting for LIP to complete...\n");
1171
1172 DEBUG3(printk("scsi(%ld): Waiting for LIP to complete...\n",
1173 ha->host_no));
1174
1175 do {
1176 rval = qla2x00_get_firmware_state(ha, &fw_state);
1177 if (rval == QLA_SUCCESS) {
1178 if (fw_state < FSTATE_LOSS_OF_SYNC) {
1179 ha->device_flags &= ~DFLG_NO_CABLE;
1180 }
1181 if (fw_state == FSTATE_READY) {
1182 DEBUG(printk("scsi(%ld): F/W Ready - OK \n",
1183 ha->host_no));
1184
1185 qla2x00_get_retry_cnt(ha, &ha->retry_count,
1186 &ha->login_timeout, &ha->r_a_tov);
1187
1188 rval = QLA_SUCCESS;
1189 break;
1190 }
1191
1192 rval = QLA_FUNCTION_FAILED;
1193
1194 if (atomic_read(&ha->loop_down_timer) &&
1195 (fw_state >= FSTATE_LOSS_OF_SYNC ||
1196 fw_state == FSTATE_WAIT_AL_PA)) {
1197 /* Loop down. Timeout on min_wait for states
Andrew Vasquezfa2a1ce2005-07-06 10:32:07 -07001198 * other than Wait for Login.
1199 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200 if (time_after_eq(jiffies, mtime)) {
1201 qla_printk(KERN_INFO, ha,
1202 "Cable is unplugged...\n");
1203
1204 ha->device_flags |= DFLG_NO_CABLE;
1205 break;
1206 }
1207 }
1208 } else {
1209 /* Mailbox cmd failed. Timeout on min_wait. */
1210 if (time_after_eq(jiffies, mtime))
1211 break;
1212 }
1213
1214 if (time_after_eq(jiffies, wtime))
1215 break;
1216
1217 /* Delay for a while */
1218 msleep(500);
1219
1220 DEBUG3(printk("scsi(%ld): fw_state=%x curr time=%lx.\n",
1221 ha->host_no, fw_state, jiffies));
1222 } while (1);
1223
1224 DEBUG(printk("scsi(%ld): fw_state=%x curr time=%lx.\n",
1225 ha->host_no, fw_state, jiffies));
1226
1227 if (rval) {
1228 DEBUG2_3(printk("scsi(%ld): Firmware ready **** FAILED ****.\n",
1229 ha->host_no));
1230 }
1231
1232 return (rval);
1233}
1234
1235/*
1236* qla2x00_configure_hba
1237* Setup adapter context.
1238*
1239* Input:
1240* ha = adapter state pointer.
1241*
1242* Returns:
1243* 0 = success
1244*
1245* Context:
1246* Kernel context.
1247*/
1248static int
1249qla2x00_configure_hba(scsi_qla_host_t *ha)
1250{
1251 int rval;
1252 uint16_t loop_id;
1253 uint16_t topo;
1254 uint8_t al_pa;
1255 uint8_t area;
1256 uint8_t domain;
1257 char connect_type[22];
1258
1259 /* Get host addresses. */
1260 rval = qla2x00_get_adapter_id(ha,
1261 &loop_id, &al_pa, &area, &domain, &topo);
1262 if (rval != QLA_SUCCESS) {
Andrew Vasquez23443b12005-12-06 10:57:06 -08001263 if (LOOP_TRANSITION(ha) || atomic_read(&ha->loop_down_timer) ||
Ravi Anand33135aa2005-11-08 14:37:20 -08001264 (rval == QLA_COMMAND_ERROR && loop_id == 0x7)) {
1265 DEBUG2(printk("%s(%ld) Loop is in a transition state\n",
1266 __func__, ha->host_no));
1267 } else {
1268 qla_printk(KERN_WARNING, ha,
1269 "ERROR -- Unable to get host loop ID.\n");
1270 set_bit(ISP_ABORT_NEEDED, &ha->dpc_flags);
1271 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272 return (rval);
1273 }
1274
1275 if (topo == 4) {
1276 qla_printk(KERN_INFO, ha,
1277 "Cannot get topology - retrying.\n");
1278 return (QLA_FUNCTION_FAILED);
1279 }
1280
1281 ha->loop_id = loop_id;
1282
1283 /* initialize */
1284 ha->min_external_loopid = SNS_FIRST_LOOP_ID;
1285 ha->operating_mode = LOOP;
1286
1287 switch (topo) {
1288 case 0:
1289 DEBUG3(printk("scsi(%ld): HBA in NL topology.\n",
1290 ha->host_no));
1291 ha->current_topology = ISP_CFG_NL;
1292 strcpy(connect_type, "(Loop)");
1293 break;
1294
1295 case 1:
1296 DEBUG3(printk("scsi(%ld): HBA in FL topology.\n",
1297 ha->host_no));
1298 ha->current_topology = ISP_CFG_FL;
1299 strcpy(connect_type, "(FL_Port)");
1300 break;
1301
1302 case 2:
1303 DEBUG3(printk("scsi(%ld): HBA in N P2P topology.\n",
1304 ha->host_no));
1305 ha->operating_mode = P2P;
1306 ha->current_topology = ISP_CFG_N;
1307 strcpy(connect_type, "(N_Port-to-N_Port)");
1308 break;
1309
1310 case 3:
1311 DEBUG3(printk("scsi(%ld): HBA in F P2P topology.\n",
1312 ha->host_no));
1313 ha->operating_mode = P2P;
1314 ha->current_topology = ISP_CFG_F;
1315 strcpy(connect_type, "(F_Port)");
1316 break;
1317
1318 default:
1319 DEBUG3(printk("scsi(%ld): HBA in unknown topology %x. "
1320 "Using NL.\n",
1321 ha->host_no, topo));
1322 ha->current_topology = ISP_CFG_NL;
1323 strcpy(connect_type, "(Loop)");
1324 break;
1325 }
1326
1327 /* Save Host port and loop ID. */
1328 /* byte order - Big Endian */
1329 ha->d_id.b.domain = domain;
1330 ha->d_id.b.area = area;
1331 ha->d_id.b.al_pa = al_pa;
1332
1333 if (!ha->flags.init_done)
1334 qla_printk(KERN_INFO, ha,
1335 "Topology - %s, Host Loop address 0x%x\n",
1336 connect_type, ha->loop_id);
1337
1338 if (rval) {
1339 DEBUG2_3(printk("scsi(%ld): FAILED.\n", ha->host_no));
1340 } else {
1341 DEBUG3(printk("scsi(%ld): exiting normally.\n", ha->host_no));
1342 }
1343
1344 return(rval);
1345}
1346
1347/*
1348* NVRAM configuration for ISP 2xxx
1349*
1350* Input:
1351* ha = adapter block pointer.
1352*
1353* Output:
1354* initialization control block in response_ring
1355* host adapters parameters in host adapter block
1356*
1357* Returns:
1358* 0 = success.
1359*/
Andrew Vasquezabbd8872005-07-06 10:30:05 -07001360int
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361qla2x00_nvram_config(scsi_qla_host_t *ha)
1362{
Andrew Vasquez0107109e2005-07-06 10:31:37 -07001363 int rval;
1364 uint8_t chksum = 0;
1365 uint16_t cnt;
1366 uint8_t *dptr1, *dptr2;
1367 init_cb_t *icb = ha->init_cb;
1368 nvram_t *nv = (nvram_t *)ha->request_ring;
1369 uint8_t *ptr = (uint8_t *)ha->request_ring;
Andrew Vasquez3d716442005-07-06 10:30:26 -07001370 struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371
1372 rval = QLA_SUCCESS;
1373
1374 /* Determine NVRAM starting address. */
Andrew Vasquez0107109e2005-07-06 10:31:37 -07001375 ha->nvram_size = sizeof(nvram_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376 ha->nvram_base = 0;
1377 if (!IS_QLA2100(ha) && !IS_QLA2200(ha) && !IS_QLA2300(ha))
1378 if ((RD_REG_WORD(&reg->ctrl_status) >> 14) == 1)
1379 ha->nvram_base = 0x80;
1380
1381 /* Get NVRAM data and calculate checksum. */
Andrew Vasquez0107109e2005-07-06 10:31:37 -07001382 ha->isp_ops.read_nvram(ha, ptr, ha->nvram_base, ha->nvram_size);
1383 for (cnt = 0, chksum = 0; cnt < ha->nvram_size; cnt++)
1384 chksum += *ptr++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385
1386 DEBUG5(printk("scsi(%ld): Contents of NVRAM\n", ha->host_no));
1387 DEBUG5(qla2x00_dump_buffer((uint8_t *)ha->request_ring,
Andrew Vasquez0107109e2005-07-06 10:31:37 -07001388 ha->nvram_size));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389
1390 /* Bad NVRAM data, set defaults parameters. */
1391 if (chksum || nv->id[0] != 'I' || nv->id[1] != 'S' ||
1392 nv->id[2] != 'P' || nv->id[3] != ' ' || nv->nvram_version < 1) {
1393 /* Reset NVRAM data. */
1394 qla_printk(KERN_WARNING, ha, "Inconsistent NVRAM detected: "
1395 "checksum=0x%x id=%c version=0x%x.\n", chksum, nv->id[0],
1396 nv->nvram_version);
1397 qla_printk(KERN_WARNING, ha, "Falling back to functioning (yet "
1398 "invalid -- WWPN) defaults.\n");
1399
1400 /*
1401 * Set default initialization control block.
1402 */
Andrew Vasquez0107109e2005-07-06 10:31:37 -07001403 memset(nv, 0, ha->nvram_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404 nv->parameter_block_version = ICB_VERSION;
1405
1406 if (IS_QLA23XX(ha)) {
1407 nv->firmware_options[0] = BIT_2 | BIT_1;
1408 nv->firmware_options[1] = BIT_7 | BIT_5;
1409 nv->add_firmware_options[0] = BIT_5;
1410 nv->add_firmware_options[1] = BIT_5 | BIT_4;
1411 nv->frame_payload_size = __constant_cpu_to_le16(2048);
1412 nv->special_options[1] = BIT_7;
1413 } else if (IS_QLA2200(ha)) {
1414 nv->firmware_options[0] = BIT_2 | BIT_1;
1415 nv->firmware_options[1] = BIT_7 | BIT_5;
1416 nv->add_firmware_options[0] = BIT_5;
1417 nv->add_firmware_options[1] = BIT_5 | BIT_4;
1418 nv->frame_payload_size = __constant_cpu_to_le16(1024);
1419 } else if (IS_QLA2100(ha)) {
1420 nv->firmware_options[0] = BIT_3 | BIT_1;
1421 nv->firmware_options[1] = BIT_5;
1422 nv->frame_payload_size = __constant_cpu_to_le16(1024);
1423 }
1424
1425 nv->max_iocb_allocation = __constant_cpu_to_le16(256);
1426 nv->execution_throttle = __constant_cpu_to_le16(16);
1427 nv->retry_count = 8;
1428 nv->retry_delay = 1;
1429
1430 nv->port_name[0] = 33;
1431 nv->port_name[3] = 224;
1432 nv->port_name[4] = 139;
1433
1434 nv->login_timeout = 4;
1435
1436 /*
1437 * Set default host adapter parameters
1438 */
1439 nv->host_p[1] = BIT_2;
1440 nv->reset_delay = 5;
1441 nv->port_down_retry_count = 8;
1442 nv->max_luns_per_target = __constant_cpu_to_le16(8);
1443 nv->link_down_timeout = 60;
1444
1445 rval = 1;
1446 }
1447
1448#if defined(CONFIG_IA64_GENERIC) || defined(CONFIG_IA64_SGI_SN2)
1449 /*
1450 * The SN2 does not provide BIOS emulation which means you can't change
1451 * potentially bogus BIOS settings. Force the use of default settings
1452 * for link rate and frame size. Hope that the rest of the settings
1453 * are valid.
1454 */
1455 if (ia64_platform_is("sn2")) {
1456 nv->frame_payload_size = __constant_cpu_to_le16(2048);
1457 if (IS_QLA23XX(ha))
1458 nv->special_options[1] = BIT_7;
1459 }
1460#endif
1461
1462 /* Reset Initialization control block */
Andrew Vasquez0107109e2005-07-06 10:31:37 -07001463 memset(icb, 0, ha->init_cb_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464
1465 /*
1466 * Setup driver NVRAM options.
1467 */
1468 nv->firmware_options[0] |= (BIT_6 | BIT_1);
1469 nv->firmware_options[0] &= ~(BIT_5 | BIT_4);
1470 nv->firmware_options[1] |= (BIT_5 | BIT_0);
1471 nv->firmware_options[1] &= ~BIT_4;
1472
1473 if (IS_QLA23XX(ha)) {
1474 nv->firmware_options[0] |= BIT_2;
1475 nv->firmware_options[0] &= ~BIT_3;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07001476 nv->add_firmware_options[1] |= BIT_5 | BIT_4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477
1478 if (IS_QLA2300(ha)) {
1479 if (ha->fb_rev == FPM_2310) {
1480 strcpy(ha->model_number, "QLA2310");
1481 } else {
1482 strcpy(ha->model_number, "QLA2300");
1483 }
1484 } else {
1485 if (rval == 0 &&
1486 memcmp(nv->model_number, BINZERO,
1487 sizeof(nv->model_number)) != 0) {
1488 char *st, *en;
1489
1490 strncpy(ha->model_number, nv->model_number,
1491 sizeof(nv->model_number));
1492 st = en = ha->model_number;
1493 en += sizeof(nv->model_number) - 1;
1494 while (en > st) {
1495 if (*en != 0x20 && *en != 0x00)
1496 break;
1497 *en-- = '\0';
1498 }
1499 } else {
1500 uint16_t index;
1501
1502 index = (ha->pdev->subsystem_device & 0xff);
1503 if (index < QLA_MODEL_NAMES) {
1504 strcpy(ha->model_number,
1505 qla2x00_model_name[index]);
1506 ha->model_desc =
1507 qla2x00_model_desc[index];
1508 } else {
1509 strcpy(ha->model_number, "QLA23xx");
1510 }
1511 }
1512 }
1513 } else if (IS_QLA2200(ha)) {
1514 nv->firmware_options[0] |= BIT_2;
1515 /*
1516 * 'Point-to-point preferred, else loop' is not a safe
1517 * connection mode setting.
1518 */
1519 if ((nv->add_firmware_options[0] & (BIT_6 | BIT_5 | BIT_4)) ==
1520 (BIT_5 | BIT_4)) {
1521 /* Force 'loop preferred, else point-to-point'. */
1522 nv->add_firmware_options[0] &= ~(BIT_6 | BIT_5 | BIT_4);
1523 nv->add_firmware_options[0] |= BIT_5;
1524 }
1525 strcpy(ha->model_number, "QLA22xx");
1526 } else /*if (IS_QLA2100(ha))*/ {
1527 strcpy(ha->model_number, "QLA2100");
1528 }
1529
1530 /*
1531 * Copy over NVRAM RISC parameter block to initialization control block.
1532 */
1533 dptr1 = (uint8_t *)icb;
1534 dptr2 = (uint8_t *)&nv->parameter_block_version;
1535 cnt = (uint8_t *)&icb->request_q_outpointer - (uint8_t *)&icb->version;
1536 while (cnt--)
1537 *dptr1++ = *dptr2++;
1538
1539 /* Copy 2nd half. */
1540 dptr1 = (uint8_t *)icb->add_firmware_options;
1541 cnt = (uint8_t *)icb->reserved_3 - (uint8_t *)icb->add_firmware_options;
1542 while (cnt--)
1543 *dptr1++ = *dptr2++;
1544
1545 /* Prepare nodename */
1546 if ((icb->firmware_options[1] & BIT_6) == 0) {
1547 /*
1548 * Firmware will apply the following mask if the nodename was
1549 * not provided.
1550 */
1551 memcpy(icb->node_name, icb->port_name, WWN_SIZE);
1552 icb->node_name[0] &= 0xF0;
1553 }
1554
1555 /*
1556 * Set host adapter parameters.
1557 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558 ha->flags.disable_risc_code_load = ((nv->host_p[0] & BIT_4) ? 1 : 0);
1559 /* Always load RISC code on non ISP2[12]00 chips. */
1560 if (!IS_QLA2100(ha) && !IS_QLA2200(ha))
1561 ha->flags.disable_risc_code_load = 0;
1562 ha->flags.enable_lip_reset = ((nv->host_p[1] & BIT_1) ? 1 : 0);
1563 ha->flags.enable_lip_full_login = ((nv->host_p[1] & BIT_2) ? 1 : 0);
1564 ha->flags.enable_target_reset = ((nv->host_p[1] & BIT_3) ? 1 : 0);
Andrew Vasquez06c22bd2005-08-26 19:09:00 -07001565 ha->flags.enable_led_scheme = (nv->special_options[1] & BIT_4) ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566
1567 ha->operating_mode =
1568 (icb->add_firmware_options[0] & (BIT_6 | BIT_5 | BIT_4)) >> 4;
1569
1570 memcpy(ha->fw_seriallink_options, nv->seriallink_options,
1571 sizeof(ha->fw_seriallink_options));
1572
1573 /* save HBA serial number */
1574 ha->serial0 = icb->port_name[5];
1575 ha->serial1 = icb->port_name[6];
1576 ha->serial2 = icb->port_name[7];
Andrew Vasquez3d716442005-07-06 10:30:26 -07001577 ha->node_name = icb->node_name;
1578 ha->port_name = icb->port_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579
1580 icb->execution_throttle = __constant_cpu_to_le16(0xFFFF);
1581
1582 ha->retry_count = nv->retry_count;
1583
1584 /* Set minimum login_timeout to 4 seconds. */
1585 if (nv->login_timeout < ql2xlogintimeout)
1586 nv->login_timeout = ql2xlogintimeout;
1587 if (nv->login_timeout < 4)
1588 nv->login_timeout = 4;
1589 ha->login_timeout = nv->login_timeout;
1590 icb->login_timeout = nv->login_timeout;
1591
1592 /* Set minimum RATOV to 200 tenths of a second. */
1593 ha->r_a_tov = 200;
1594
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595 ha->loop_reset_delay = nv->reset_delay;
1596
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597 /* Link Down Timeout = 0:
1598 *
1599 * When Port Down timer expires we will start returning
1600 * I/O's to OS with "DID_NO_CONNECT".
1601 *
1602 * Link Down Timeout != 0:
1603 *
1604 * The driver waits for the link to come up after link down
1605 * before returning I/Os to OS with "DID_NO_CONNECT".
Andrew Vasquezfa2a1ce2005-07-06 10:32:07 -07001606 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607 if (nv->link_down_timeout == 0) {
1608 ha->loop_down_abort_time =
Andrew Vasquez 354d6b22005-04-23 02:47:27 -04001609 (LOOP_DOWN_TIME - LOOP_DOWN_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610 } else {
1611 ha->link_down_timeout = nv->link_down_timeout;
1612 ha->loop_down_abort_time =
1613 (LOOP_DOWN_TIME - ha->link_down_timeout);
Andrew Vasquezfa2a1ce2005-07-06 10:32:07 -07001614 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616 /*
1617 * Need enough time to try and get the port back.
1618 */
1619 ha->port_down_retry_count = nv->port_down_retry_count;
1620 if (qlport_down_retry)
1621 ha->port_down_retry_count = qlport_down_retry;
1622 /* Set login_retry_count */
1623 ha->login_retry_count = nv->retry_count;
1624 if (ha->port_down_retry_count == nv->port_down_retry_count &&
1625 ha->port_down_retry_count > 3)
1626 ha->login_retry_count = ha->port_down_retry_count;
1627 else if (ha->port_down_retry_count > (int)ha->login_retry_count)
1628 ha->login_retry_count = ha->port_down_retry_count;
1629 if (ql2xloginretrycount)
1630 ha->login_retry_count = ql2xloginretrycount;
1631
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632 icb->lun_enables = __constant_cpu_to_le16(0);
1633 icb->command_resource_count = 0;
1634 icb->immediate_notify_resource_count = 0;
1635 icb->timeout = __constant_cpu_to_le16(0);
1636
1637 if (IS_QLA2100(ha) || IS_QLA2200(ha)) {
1638 /* Enable RIO */
1639 icb->firmware_options[0] &= ~BIT_3;
1640 icb->add_firmware_options[0] &=
1641 ~(BIT_3 | BIT_2 | BIT_1 | BIT_0);
1642 icb->add_firmware_options[0] |= BIT_2;
1643 icb->response_accumulation_timer = 3;
1644 icb->interrupt_delay_timer = 5;
1645
1646 ha->flags.process_response_queue = 1;
1647 } else {
Andrew Vasquez4fdfefe2005-10-27 11:09:48 -07001648 /* Enable ZIO. */
1649 if (!ha->flags.init_done) {
1650 ha->zio_mode = icb->add_firmware_options[0] &
1651 (BIT_3 | BIT_2 | BIT_1 | BIT_0);
1652 ha->zio_timer = icb->interrupt_delay_timer ?
1653 icb->interrupt_delay_timer: 2;
1654 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655 icb->add_firmware_options[0] &=
1656 ~(BIT_3 | BIT_2 | BIT_1 | BIT_0);
Andrew Vasquez4fdfefe2005-10-27 11:09:48 -07001657 ha->flags.process_response_queue = 0;
1658 if (ha->zio_mode != QLA_ZIO_DISABLED) {
1659 DEBUG2(printk("scsi(%ld): ZIO mode %d enabled; timer "
1660 "delay (%d us).\n", ha->host_no, ha->zio_mode,
1661 ha->zio_timer * 100));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662 qla_printk(KERN_INFO, ha,
Andrew Vasquez4fdfefe2005-10-27 11:09:48 -07001663 "ZIO mode %d enabled; timer delay (%d us).\n",
1664 ha->zio_mode, ha->zio_timer * 100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665
Andrew Vasquez4fdfefe2005-10-27 11:09:48 -07001666 icb->add_firmware_options[0] |= (uint8_t)ha->zio_mode;
1667 icb->interrupt_delay_timer = (uint8_t)ha->zio_timer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668 ha->flags.process_response_queue = 1;
1669 }
1670 }
1671
1672 if (rval) {
1673 DEBUG2_3(printk(KERN_WARNING
1674 "scsi(%ld): NVRAM configuration failed!\n", ha->host_no));
1675 }
1676 return (rval);
1677}
1678
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04001679static void
1680qla2x00_rport_add(void *data)
1681{
1682 fc_port_t *fcport = data;
1683
1684 qla2x00_reg_remote_port(fcport->ha, fcport);
1685}
1686
1687static void
1688qla2x00_rport_del(void *data)
1689{
1690 fc_port_t *fcport = data;
1691
1692 if (fcport->rport)
1693 fc_remote_port_delete(fcport->rport);
1694 fcport->rport = NULL;
1695}
1696
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697/**
1698 * qla2x00_alloc_fcport() - Allocate a generic fcport.
1699 * @ha: HA context
1700 * @flags: allocation flags
1701 *
1702 * Returns a pointer to the allocated fcport, or NULL, if none available.
1703 */
1704fc_port_t *
Al Viroc53033f2005-10-21 03:22:08 -04001705qla2x00_alloc_fcport(scsi_qla_host_t *ha, gfp_t flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706{
1707 fc_port_t *fcport;
1708
1709 fcport = kmalloc(sizeof(fc_port_t), flags);
1710 if (fcport == NULL)
1711 return (fcport);
1712
1713 /* Setup fcport template structure. */
1714 memset(fcport, 0, sizeof (fc_port_t));
1715 fcport->ha = ha;
1716 fcport->port_type = FCT_UNKNOWN;
1717 fcport->loop_id = FC_NO_LOOP_ID;
1718 fcport->iodesc_idx_sent = IODESC_INVALID_INDEX;
1719 atomic_set(&fcport->state, FCS_UNCONFIGURED);
1720 fcport->flags = FCF_RLC_SUPPORT;
Andrew Vasquezad3e0ed2005-08-26 19:08:10 -07001721 fcport->supported_classes = FC_COS_UNSPECIFIED;
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04001722 INIT_WORK(&fcport->rport_add_work, qla2x00_rport_add, fcport);
1723 INIT_WORK(&fcport->rport_del_work, qla2x00_rport_del, fcport);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724
1725 return (fcport);
1726}
1727
1728/*
1729 * qla2x00_configure_loop
1730 * Updates Fibre Channel Device Database with what is actually on loop.
1731 *
1732 * Input:
1733 * ha = adapter block pointer.
1734 *
1735 * Returns:
1736 * 0 = success.
1737 * 1 = error.
1738 * 2 = database was full and device was not configured.
1739 */
1740static int
Andrew Vasquezfa2a1ce2005-07-06 10:32:07 -07001741qla2x00_configure_loop(scsi_qla_host_t *ha)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742{
1743 int rval;
1744 unsigned long flags, save_flags;
1745
1746 rval = QLA_SUCCESS;
1747
1748 /* Get Initiator ID */
1749 if (test_bit(LOCAL_LOOP_UPDATE, &ha->dpc_flags)) {
1750 rval = qla2x00_configure_hba(ha);
1751 if (rval != QLA_SUCCESS) {
1752 DEBUG(printk("scsi(%ld): Unable to configure HBA.\n",
1753 ha->host_no));
1754 return (rval);
1755 }
1756 }
1757
1758 save_flags = flags = ha->dpc_flags;
1759 DEBUG(printk("scsi(%ld): Configure loop -- dpc flags =0x%lx\n",
1760 ha->host_no, flags));
1761
1762 /*
1763 * If we have both an RSCN and PORT UPDATE pending then handle them
1764 * both at the same time.
1765 */
1766 clear_bit(LOCAL_LOOP_UPDATE, &ha->dpc_flags);
1767 clear_bit(RSCN_UPDATE, &ha->dpc_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768
1769 /* Determine what we need to do */
1770 if (ha->current_topology == ISP_CFG_FL &&
1771 (test_bit(LOCAL_LOOP_UPDATE, &flags))) {
1772
1773 ha->flags.rscn_queue_overflow = 1;
1774 set_bit(RSCN_UPDATE, &flags);
1775
1776 } else if (ha->current_topology == ISP_CFG_F &&
1777 (test_bit(LOCAL_LOOP_UPDATE, &flags))) {
1778
1779 ha->flags.rscn_queue_overflow = 1;
1780 set_bit(RSCN_UPDATE, &flags);
1781 clear_bit(LOCAL_LOOP_UPDATE, &flags);
1782
1783 } else if (!ha->flags.online ||
1784 (test_bit(ABORT_ISP_ACTIVE, &flags))) {
1785
1786 ha->flags.rscn_queue_overflow = 1;
1787 set_bit(RSCN_UPDATE, &flags);
1788 set_bit(LOCAL_LOOP_UPDATE, &flags);
1789 }
1790
1791 if (test_bit(LOCAL_LOOP_UPDATE, &flags)) {
1792 if (test_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags)) {
1793 rval = QLA_FUNCTION_FAILED;
1794 } else {
1795 rval = qla2x00_configure_local_loop(ha);
1796 }
1797 }
1798
1799 if (rval == QLA_SUCCESS && test_bit(RSCN_UPDATE, &flags)) {
Andrew Vasquez23443b12005-12-06 10:57:06 -08001800 if (LOOP_TRANSITION(ha)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801 rval = QLA_FUNCTION_FAILED;
1802 } else {
1803 rval = qla2x00_configure_fabric(ha);
1804 }
1805 }
1806
1807 if (rval == QLA_SUCCESS) {
1808 if (atomic_read(&ha->loop_down_timer) ||
1809 test_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags)) {
1810 rval = QLA_FUNCTION_FAILED;
1811 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812 atomic_set(&ha->loop_state, LOOP_READY);
1813
1814 DEBUG(printk("scsi(%ld): LOOP READY\n", ha->host_no));
1815 }
1816 }
1817
1818 if (rval) {
1819 DEBUG2_3(printk("%s(%ld): *** FAILED ***\n",
1820 __func__, ha->host_no));
1821 } else {
1822 DEBUG3(printk("%s: exiting normally\n", __func__));
1823 }
1824
1825 /* Restore state if a resync event occured during processing */
1826 if (test_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags)) {
1827 if (test_bit(LOCAL_LOOP_UPDATE, &save_flags))
1828 set_bit(LOCAL_LOOP_UPDATE, &ha->dpc_flags);
1829 if (test_bit(RSCN_UPDATE, &save_flags))
1830 set_bit(RSCN_UPDATE, &ha->dpc_flags);
1831 }
1832
1833 return (rval);
1834}
1835
1836
1837
1838/*
1839 * qla2x00_configure_local_loop
1840 * Updates Fibre Channel Device Database with local loop devices.
1841 *
1842 * Input:
1843 * ha = adapter block pointer.
1844 *
1845 * Returns:
1846 * 0 = success.
1847 */
1848static int
Andrew Vasquezfa2a1ce2005-07-06 10:32:07 -07001849qla2x00_configure_local_loop(scsi_qla_host_t *ha)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850{
1851 int rval, rval2;
1852 int found_devs;
1853 int found;
1854 fc_port_t *fcport, *new_fcport;
1855
1856 uint16_t index;
1857 uint16_t entries;
1858 char *id_iter;
1859 uint16_t loop_id;
1860 uint8_t domain, area, al_pa;
1861
1862 found_devs = 0;
1863 new_fcport = NULL;
1864 entries = MAX_FIBRE_DEVICES;
1865
1866 DEBUG3(printk("scsi(%ld): Getting FCAL position map\n", ha->host_no));
1867 DEBUG3(qla2x00_get_fcal_position_map(ha, NULL));
1868
1869 /* Get list of logged in devices. */
1870 memset(ha->gid_list, 0, GID_LIST_SIZE);
1871 rval = qla2x00_get_id_list(ha, ha->gid_list, ha->gid_list_dma,
1872 &entries);
1873 if (rval != QLA_SUCCESS)
1874 goto cleanup_allocation;
1875
1876 DEBUG3(printk("scsi(%ld): Entries in ID list (%d)\n",
1877 ha->host_no, entries));
1878 DEBUG3(qla2x00_dump_buffer((uint8_t *)ha->gid_list,
1879 entries * sizeof(struct gid_list_info)));
1880
1881 /* Allocate temporary fcport for any new fcports discovered. */
1882 new_fcport = qla2x00_alloc_fcport(ha, GFP_KERNEL);
1883 if (new_fcport == NULL) {
1884 rval = QLA_MEMORY_ALLOC_FAILED;
1885 goto cleanup_allocation;
1886 }
1887 new_fcport->flags &= ~FCF_FABRIC_DEVICE;
1888
1889 /*
1890 * Mark local devices that were present with FCF_DEVICE_LOST for now.
1891 */
1892 list_for_each_entry(fcport, &ha->fcports, list) {
1893 if (atomic_read(&fcport->state) == FCS_ONLINE &&
1894 fcport->port_type != FCT_BROADCAST &&
1895 (fcport->flags & FCF_FABRIC_DEVICE) == 0) {
1896
1897 DEBUG(printk("scsi(%ld): Marking port lost, "
1898 "loop_id=0x%04x\n",
1899 ha->host_no, fcport->loop_id));
1900
1901 atomic_set(&fcport->state, FCS_DEVICE_LOST);
1902 fcport->flags &= ~FCF_FARP_DONE;
1903 }
1904 }
1905
1906 /* Add devices to port list. */
1907 id_iter = (char *)ha->gid_list;
1908 for (index = 0; index < entries; index++) {
1909 domain = ((struct gid_list_info *)id_iter)->domain;
1910 area = ((struct gid_list_info *)id_iter)->area;
1911 al_pa = ((struct gid_list_info *)id_iter)->al_pa;
Andrew Vasquezabbd8872005-07-06 10:30:05 -07001912 if (IS_QLA2100(ha) || IS_QLA2200(ha))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913 loop_id = (uint16_t)
1914 ((struct gid_list_info *)id_iter)->loop_id_2100;
Andrew Vasquezabbd8872005-07-06 10:30:05 -07001915 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001916 loop_id = le16_to_cpu(
1917 ((struct gid_list_info *)id_iter)->loop_id);
Andrew Vasquezabbd8872005-07-06 10:30:05 -07001918 id_iter += ha->gid_list_info_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919
1920 /* Bypass reserved domain fields. */
1921 if ((domain & 0xf0) == 0xf0)
1922 continue;
1923
1924 /* Bypass if not same domain and area of adapter. */
Andrew Vasquezf7d289f2005-08-26 19:08:40 -07001925 if (area && domain &&
1926 (area != ha->d_id.b.area || domain != ha->d_id.b.domain))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927 continue;
1928
1929 /* Bypass invalid local loop ID. */
1930 if (loop_id > LAST_LOCAL_LOOP_ID)
1931 continue;
1932
1933 /* Fill in member data. */
1934 new_fcport->d_id.b.domain = domain;
1935 new_fcport->d_id.b.area = area;
1936 new_fcport->d_id.b.al_pa = al_pa;
1937 new_fcport->loop_id = loop_id;
1938 rval2 = qla2x00_get_port_database(ha, new_fcport, 0);
1939 if (rval2 != QLA_SUCCESS) {
1940 DEBUG2(printk("scsi(%ld): Failed to retrieve fcport "
1941 "information -- get_port_database=%x, "
1942 "loop_id=0x%04x\n",
1943 ha->host_no, rval2, new_fcport->loop_id));
andrew.vasquez@qlogic.comc9d02ac2006-01-13 17:05:26 -08001944 DEBUG2(printk("scsi(%ld): Scheduling resync...\n",
1945 ha->host_no));
1946 set_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947 continue;
1948 }
1949
1950 /* Check for matching device in port list. */
1951 found = 0;
1952 fcport = NULL;
1953 list_for_each_entry(fcport, &ha->fcports, list) {
1954 if (memcmp(new_fcport->port_name, fcport->port_name,
1955 WWN_SIZE))
1956 continue;
1957
1958 fcport->flags &= ~(FCF_FABRIC_DEVICE |
1959 FCF_PERSISTENT_BOUND);
1960 fcport->loop_id = new_fcport->loop_id;
1961 fcport->port_type = new_fcport->port_type;
1962 fcport->d_id.b24 = new_fcport->d_id.b24;
1963 memcpy(fcport->node_name, new_fcport->node_name,
1964 WWN_SIZE);
1965
1966 found++;
1967 break;
1968 }
1969
1970 if (!found) {
1971 /* New device, add to fcports list. */
1972 new_fcport->flags &= ~FCF_PERSISTENT_BOUND;
1973 list_add_tail(&new_fcport->list, &ha->fcports);
1974
1975 /* Allocate a new replacement fcport. */
1976 fcport = new_fcport;
1977 new_fcport = qla2x00_alloc_fcport(ha, GFP_KERNEL);
1978 if (new_fcport == NULL) {
1979 rval = QLA_MEMORY_ALLOC_FAILED;
1980 goto cleanup_allocation;
1981 }
1982 new_fcport->flags &= ~FCF_FABRIC_DEVICE;
1983 }
1984
1985 qla2x00_update_fcport(ha, fcport);
1986
1987 found_devs++;
1988 }
1989
1990cleanup_allocation:
Jesper Juhlc9475cb2005-11-07 01:01:26 -08001991 kfree(new_fcport);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992
1993 if (rval != QLA_SUCCESS) {
1994 DEBUG2(printk("scsi(%ld): Configure local loop error exit: "
1995 "rval=%x\n", ha->host_no, rval));
1996 }
1997
1998 if (found_devs) {
1999 ha->device_flags |= DFLG_LOCAL_DEVICES;
2000 ha->device_flags &= ~DFLG_RETRY_LOCAL_DEVICES;
2001 }
2002
2003 return (rval);
2004}
2005
2006static void
Andrew Vasquezfa2a1ce2005-07-06 10:32:07 -07002007qla2x00_probe_for_all_luns(scsi_qla_host_t *ha)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002008{
2009 fc_port_t *fcport;
2010
Andrew Vasquezfa2a1ce2005-07-06 10:32:07 -07002011 qla2x00_mark_all_devices_lost(ha);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012 list_for_each_entry(fcport, &ha->fcports, list) {
2013 if (fcport->port_type != FCT_TARGET)
2014 continue;
2015
Andrew Vasquezfa2a1ce2005-07-06 10:32:07 -07002016 qla2x00_update_fcport(ha, fcport);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017 }
2018}
2019
2020/*
2021 * qla2x00_update_fcport
2022 * Updates device on list.
2023 *
2024 * Input:
2025 * ha = adapter block pointer.
2026 * fcport = port structure pointer.
2027 *
2028 * Return:
2029 * 0 - Success
2030 * BIT_0 - error
2031 *
2032 * Context:
2033 * Kernel context.
2034 */
2035static void
2036qla2x00_update_fcport(scsi_qla_host_t *ha, fc_port_t *fcport)
2037{
2038 uint16_t index;
2039 unsigned long flags;
2040 srb_t *sp;
2041
2042 fcport->ha = ha;
2043 fcport->login_retry = 0;
2044 fcport->port_login_retry_count = ha->port_down_retry_count *
2045 PORT_RETRY_TIME;
2046 atomic_set(&fcport->port_down_timer, ha->port_down_retry_count *
2047 PORT_RETRY_TIME);
2048 fcport->flags &= ~FCF_LOGIN_NEEDED;
2049
2050 /*
2051 * Check for outstanding cmd on tape Bypass LUN discovery if active
2052 * command on tape.
2053 */
2054 if (fcport->flags & FCF_TAPE_PRESENT) {
2055 spin_lock_irqsave(&ha->hardware_lock, flags);
2056 for (index = 1; index < MAX_OUTSTANDING_COMMANDS; index++) {
bdf79622005-04-17 15:06:53 -05002057 fc_port_t *sfcp;
2058
Linus Torvalds1da177e2005-04-16 15:20:36 -07002059 if ((sp = ha->outstanding_cmds[index]) != 0) {
bdf79622005-04-17 15:06:53 -05002060 sfcp = sp->fcport;
2061 if (sfcp == fcport) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062 atomic_set(&fcport->state, FCS_ONLINE);
2063 spin_unlock_irqrestore(
2064 &ha->hardware_lock, flags);
2065 return;
2066 }
2067 }
2068 }
2069 spin_unlock_irqrestore(&ha->hardware_lock, flags);
2070 }
2071
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072 if (fcport->port_type == FCT_INITIATOR ||
bdf79622005-04-17 15:06:53 -05002073 fcport->port_type == FCT_BROADCAST)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002074 fcport->device_type = TYPE_PROCESSOR;
bdf79622005-04-17 15:06:53 -05002075
Linus Torvalds1da177e2005-04-16 15:20:36 -07002076 atomic_set(&fcport->state, FCS_ONLINE);
bdf79622005-04-17 15:06:53 -05002077
8482e1182005-04-17 15:04:54 -05002078 if (ha->flags.init_done)
2079 qla2x00_reg_remote_port(ha, fcport);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002080}
2081
8482e1182005-04-17 15:04:54 -05002082void
2083qla2x00_reg_remote_port(scsi_qla_host_t *ha, fc_port_t *fcport)
2084{
2085 struct fc_rport_identifiers rport_ids;
bdf79622005-04-17 15:06:53 -05002086 struct fc_rport *rport;
8482e1182005-04-17 15:04:54 -05002087
2088 if (fcport->rport) {
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04002089 fc_remote_port_delete(fcport->rport);
2090 fcport->rport = NULL;
8482e1182005-04-17 15:04:54 -05002091 }
2092
Andrew Vasquezf8b02a82005-08-31 15:21:20 -07002093 rport_ids.node_name = wwn_to_u64(fcport->node_name);
2094 rport_ids.port_name = wwn_to_u64(fcport->port_name);
8482e1182005-04-17 15:04:54 -05002095 rport_ids.port_id = fcport->d_id.b.domain << 16 |
2096 fcport->d_id.b.area << 8 | fcport->d_id.b.al_pa;
2097 rport_ids.roles = FC_RPORT_ROLE_UNKNOWN;
Andrew Vasquez77d74142005-07-08 18:00:36 -07002098 fcport->rport = rport = fc_remote_port_add(ha->host, 0, &rport_ids);
2099 if (!rport) {
2100 qla_printk(KERN_WARNING, ha,
2101 "Unable to allocate fc remote port!\n");
2102 return;
2103 }
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04002104 *((fc_port_t **)rport->dd_data) = fcport;
Andrew Vasquezad3e0ed2005-08-26 19:08:10 -07002105 rport->supported_classes = fcport->supported_classes;
Andrew Vasquez77d74142005-07-08 18:00:36 -07002106
2107 rport_ids.roles = FC_RPORT_ROLE_UNKNOWN;
8482e1182005-04-17 15:04:54 -05002108 if (fcport->port_type == FCT_INITIATOR)
2109 rport_ids.roles |= FC_RPORT_ROLE_FCP_INITIATOR;
2110 if (fcport->port_type == FCT_TARGET)
2111 rport_ids.roles |= FC_RPORT_ROLE_FCP_TARGET;
Andrew Vasquez77d74142005-07-08 18:00:36 -07002112 fc_remote_port_rolechg(rport, rport_ids.roles);
bdf79622005-04-17 15:06:53 -05002113
Andrew Vasquezcc4731f2005-07-06 10:32:37 -07002114 if (rport->scsi_target_id != -1 &&
2115 rport->scsi_target_id < ha->host->max_id)
bdf79622005-04-17 15:06:53 -05002116 fcport->os_target_id = rport->scsi_target_id;
8482e1182005-04-17 15:04:54 -05002117}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002118
2119/*
2120 * qla2x00_configure_fabric
2121 * Setup SNS devices with loop ID's.
2122 *
2123 * Input:
2124 * ha = adapter block pointer.
2125 *
2126 * Returns:
2127 * 0 = success.
2128 * BIT_0 = error
2129 */
2130static int
2131qla2x00_configure_fabric(scsi_qla_host_t *ha)
2132{
2133 int rval, rval2;
2134 fc_port_t *fcport, *fcptemp;
2135 uint16_t next_loopid;
2136 uint16_t mb[MAILBOX_REGISTER_COUNT];
Andrew Vasquez0107109e2005-07-06 10:31:37 -07002137 uint16_t loop_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002138 LIST_HEAD(new_fcports);
2139
2140 /* If FL port exists, then SNS is present */
Andrew Vasquez0107109e2005-07-06 10:31:37 -07002141 if (IS_QLA24XX(ha) || IS_QLA25XX(ha))
2142 loop_id = NPH_F_PORT;
2143 else
2144 loop_id = SNS_FL_PORT;
2145 rval = qla2x00_get_port_name(ha, loop_id, NULL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002146 if (rval != QLA_SUCCESS) {
2147 DEBUG2(printk("scsi(%ld): MBC_GET_PORT_NAME Failed, No FL "
2148 "Port\n", ha->host_no));
2149
2150 ha->device_flags &= ~SWITCH_FOUND;
2151 return (QLA_SUCCESS);
2152 }
2153
2154 /* Mark devices that need re-synchronization. */
2155 rval2 = qla2x00_device_resync(ha);
2156 if (rval2 == QLA_RSCNS_HANDLED) {
2157 /* No point doing the scan, just continue. */
2158 return (QLA_SUCCESS);
2159 }
2160 do {
Andrew Vasquezcca53352005-08-26 19:08:30 -07002161 /* FDMI support. */
2162 if (ql2xfdmienable &&
2163 test_and_clear_bit(REGISTER_FDMI_NEEDED, &ha->dpc_flags))
2164 qla2x00_fdmi_register(ha);
2165
Linus Torvalds1da177e2005-04-16 15:20:36 -07002166 /* Ensure we are logged into the SNS. */
Andrew Vasquez0107109e2005-07-06 10:31:37 -07002167 if (IS_QLA24XX(ha) || IS_QLA25XX(ha))
2168 loop_id = NPH_SNS;
2169 else
2170 loop_id = SIMPLE_NAME_SERVER;
2171 ha->isp_ops.fabric_login(ha, loop_id, 0xff, 0xff,
Andrew Vasquezabbd8872005-07-06 10:30:05 -07002172 0xfc, mb, BIT_1 | BIT_0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002173 if (mb[0] != MBS_COMMAND_COMPLETE) {
2174 DEBUG2(qla_printk(KERN_INFO, ha,
2175 "Failed SNS login: loop_id=%x mb[0]=%x mb[1]=%x "
Andrew Vasquez0107109e2005-07-06 10:31:37 -07002176 "mb[2]=%x mb[6]=%x mb[7]=%x\n", loop_id,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002177 mb[0], mb[1], mb[2], mb[6], mb[7]));
2178 return (QLA_SUCCESS);
2179 }
2180
2181 if (test_and_clear_bit(REGISTER_FC4_NEEDED, &ha->dpc_flags)) {
2182 if (qla2x00_rft_id(ha)) {
2183 /* EMPTY */
2184 DEBUG2(printk("scsi(%ld): Register FC-4 "
2185 "TYPE failed.\n", ha->host_no));
2186 }
2187 if (qla2x00_rff_id(ha)) {
2188 /* EMPTY */
2189 DEBUG2(printk("scsi(%ld): Register FC-4 "
2190 "Features failed.\n", ha->host_no));
2191 }
2192 if (qla2x00_rnn_id(ha)) {
2193 /* EMPTY */
2194 DEBUG2(printk("scsi(%ld): Register Node Name "
2195 "failed.\n", ha->host_no));
2196 } else if (qla2x00_rsnn_nn(ha)) {
2197 /* EMPTY */
2198 DEBUG2(printk("scsi(%ld): Register Symbolic "
2199 "Node Name failed.\n", ha->host_no));
2200 }
2201 }
2202
2203 rval = qla2x00_find_all_fabric_devs(ha, &new_fcports);
2204 if (rval != QLA_SUCCESS)
2205 break;
2206
2207 /*
2208 * Logout all previous fabric devices marked lost, except
2209 * tape devices.
2210 */
2211 list_for_each_entry(fcport, &ha->fcports, list) {
2212 if (test_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags))
2213 break;
2214
2215 if ((fcport->flags & FCF_FABRIC_DEVICE) == 0)
2216 continue;
2217
2218 if (atomic_read(&fcport->state) == FCS_DEVICE_LOST) {
2219 qla2x00_mark_device_lost(ha, fcport,
2220 ql2xplogiabsentdevice);
2221 if (fcport->loop_id != FC_NO_LOOP_ID &&
2222 (fcport->flags & FCF_TAPE_PRESENT) == 0 &&
2223 fcport->port_type != FCT_INITIATOR &&
2224 fcport->port_type != FCT_BROADCAST) {
2225
Andrew Vasquezabbd8872005-07-06 10:30:05 -07002226 ha->isp_ops.fabric_logout(ha,
Andrew Vasquez1c7c6352005-07-06 10:30:57 -07002227 fcport->loop_id,
2228 fcport->d_id.b.domain,
2229 fcport->d_id.b.area,
2230 fcport->d_id.b.al_pa);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002231 fcport->loop_id = FC_NO_LOOP_ID;
2232 }
2233 }
2234 }
2235
2236 /* Starting free loop ID. */
2237 next_loopid = ha->min_external_loopid;
2238
2239 /*
2240 * Scan through our port list and login entries that need to be
2241 * logged in.
2242 */
2243 list_for_each_entry(fcport, &ha->fcports, list) {
2244 if (atomic_read(&ha->loop_down_timer) ||
2245 test_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags))
2246 break;
2247
2248 if ((fcport->flags & FCF_FABRIC_DEVICE) == 0 ||
2249 (fcport->flags & FCF_LOGIN_NEEDED) == 0)
2250 continue;
2251
2252 if (fcport->loop_id == FC_NO_LOOP_ID) {
2253 fcport->loop_id = next_loopid;
2254 rval = qla2x00_find_new_loop_id(ha, fcport);
2255 if (rval != QLA_SUCCESS) {
2256 /* Ran out of IDs to use */
2257 break;
2258 }
2259 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002260 /* Login and update database */
2261 qla2x00_fabric_dev_login(ha, fcport, &next_loopid);
2262 }
2263
2264 /* Exit if out of loop IDs. */
2265 if (rval != QLA_SUCCESS) {
2266 break;
2267 }
2268
2269 /*
2270 * Login and add the new devices to our port list.
2271 */
2272 list_for_each_entry_safe(fcport, fcptemp, &new_fcports, list) {
2273 if (atomic_read(&ha->loop_down_timer) ||
2274 test_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags))
2275 break;
2276
2277 /* Find a new loop ID to use. */
2278 fcport->loop_id = next_loopid;
2279 rval = qla2x00_find_new_loop_id(ha, fcport);
2280 if (rval != QLA_SUCCESS) {
2281 /* Ran out of IDs to use */
2282 break;
2283 }
2284
Linus Torvalds1da177e2005-04-16 15:20:36 -07002285 /* Remove device from the new list and add it to DB */
2286 list_del(&fcport->list);
2287 list_add_tail(&fcport->list, &ha->fcports);
bdf79622005-04-17 15:06:53 -05002288
2289 /* Login and update database */
2290 qla2x00_fabric_dev_login(ha, fcport, &next_loopid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002291 }
2292 } while (0);
2293
2294 /* Free all new device structures not processed. */
2295 list_for_each_entry_safe(fcport, fcptemp, &new_fcports, list) {
2296 list_del(&fcport->list);
2297 kfree(fcport);
2298 }
2299
2300 if (rval) {
2301 DEBUG2(printk("scsi(%ld): Configure fabric error exit: "
2302 "rval=%d\n", ha->host_no, rval));
2303 }
2304
2305 return (rval);
2306}
2307
2308
2309/*
2310 * qla2x00_find_all_fabric_devs
2311 *
2312 * Input:
2313 * ha = adapter block pointer.
2314 * dev = database device entry pointer.
2315 *
2316 * Returns:
2317 * 0 = success.
2318 *
2319 * Context:
2320 * Kernel context.
2321 */
2322static int
2323qla2x00_find_all_fabric_devs(scsi_qla_host_t *ha, struct list_head *new_fcports)
2324{
2325 int rval;
2326 uint16_t loop_id;
2327 fc_port_t *fcport, *new_fcport, *fcptemp;
2328 int found;
2329
2330 sw_info_t *swl;
2331 int swl_idx;
2332 int first_dev, last_dev;
2333 port_id_t wrap, nxt_d_id;
2334
2335 rval = QLA_SUCCESS;
2336
2337 /* Try GID_PT to get device list, else GAN. */
2338 swl = kmalloc(sizeof(sw_info_t) * MAX_FIBRE_DEVICES, GFP_ATOMIC);
2339 if (swl == NULL) {
2340 /*EMPTY*/
2341 DEBUG2(printk("scsi(%ld): GID_PT allocations failed, fallback "
2342 "on GA_NXT\n", ha->host_no));
2343 } else {
2344 memset(swl, 0, sizeof(sw_info_t) * MAX_FIBRE_DEVICES);
2345 if (qla2x00_gid_pt(ha, swl) != QLA_SUCCESS) {
2346 kfree(swl);
2347 swl = NULL;
2348 } else if (qla2x00_gpn_id(ha, swl) != QLA_SUCCESS) {
2349 kfree(swl);
2350 swl = NULL;
2351 } else if (qla2x00_gnn_id(ha, swl) != QLA_SUCCESS) {
2352 kfree(swl);
2353 swl = NULL;
2354 }
2355 }
2356 swl_idx = 0;
2357
2358 /* Allocate temporary fcport for any new fcports discovered. */
2359 new_fcport = qla2x00_alloc_fcport(ha, GFP_KERNEL);
2360 if (new_fcport == NULL) {
Jesper Juhlc9475cb2005-11-07 01:01:26 -08002361 kfree(swl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002362 return (QLA_MEMORY_ALLOC_FAILED);
2363 }
2364 new_fcport->flags |= (FCF_FABRIC_DEVICE | FCF_LOGIN_NEEDED);
2365
2366 /* Set start port ID scan at adapter ID. */
2367 first_dev = 1;
2368 last_dev = 0;
2369
2370 /* Starting free loop ID. */
2371 loop_id = ha->min_external_loopid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002372 for (; loop_id <= ha->last_loop_id; loop_id++) {
Andrew Vasquez3d716442005-07-06 10:30:26 -07002373 if (qla2x00_is_reserved_id(ha, loop_id))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002374 continue;
2375
Andrew Vasquez23443b12005-12-06 10:57:06 -08002376 if (atomic_read(&ha->loop_down_timer) || LOOP_TRANSITION(ha))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002377 break;
2378
2379 if (swl != NULL) {
2380 if (last_dev) {
2381 wrap.b24 = new_fcport->d_id.b24;
2382 } else {
2383 new_fcport->d_id.b24 = swl[swl_idx].d_id.b24;
2384 memcpy(new_fcport->node_name,
2385 swl[swl_idx].node_name, WWN_SIZE);
2386 memcpy(new_fcport->port_name,
2387 swl[swl_idx].port_name, WWN_SIZE);
2388
2389 if (swl[swl_idx].d_id.b.rsvd_1 != 0) {
2390 last_dev = 1;
2391 }
2392 swl_idx++;
2393 }
2394 } else {
2395 /* Send GA_NXT to the switch */
2396 rval = qla2x00_ga_nxt(ha, new_fcport);
2397 if (rval != QLA_SUCCESS) {
2398 qla_printk(KERN_WARNING, ha,
2399 "SNS scan failed -- assuming zero-entry "
2400 "result...\n");
2401 list_for_each_entry_safe(fcport, fcptemp,
2402 new_fcports, list) {
2403 list_del(&fcport->list);
2404 kfree(fcport);
2405 }
2406 rval = QLA_SUCCESS;
2407 break;
2408 }
2409 }
2410
2411 /* If wrap on switch device list, exit. */
2412 if (first_dev) {
2413 wrap.b24 = new_fcport->d_id.b24;
2414 first_dev = 0;
2415 } else if (new_fcport->d_id.b24 == wrap.b24) {
2416 DEBUG2(printk("scsi(%ld): device wrap (%02x%02x%02x)\n",
2417 ha->host_no, new_fcport->d_id.b.domain,
2418 new_fcport->d_id.b.area, new_fcport->d_id.b.al_pa));
2419 break;
2420 }
2421
2422 /* Bypass if host adapter. */
2423 if (new_fcport->d_id.b24 == ha->d_id.b24)
2424 continue;
2425
Andrew Vasquezf7d289f2005-08-26 19:08:40 -07002426 /* Bypass if same domain and area of adapter. */
2427 if (((new_fcport->d_id.b24 & 0xffff00) ==
2428 (ha->d_id.b24 & 0xffff00)) && ha->current_topology ==
2429 ISP_CFG_FL)
2430 continue;
2431
Linus Torvalds1da177e2005-04-16 15:20:36 -07002432 /* Bypass reserved domain fields. */
2433 if ((new_fcport->d_id.b.domain & 0xf0) == 0xf0)
2434 continue;
2435
2436 /* Locate matching device in database. */
2437 found = 0;
2438 list_for_each_entry(fcport, &ha->fcports, list) {
2439 if (memcmp(new_fcport->port_name, fcport->port_name,
2440 WWN_SIZE))
2441 continue;
2442
2443 found++;
2444
2445 /*
2446 * If address the same and state FCS_ONLINE, nothing
2447 * changed.
2448 */
2449 if (fcport->d_id.b24 == new_fcport->d_id.b24 &&
2450 atomic_read(&fcport->state) == FCS_ONLINE) {
2451 break;
2452 }
2453
2454 /*
2455 * If device was not a fabric device before.
2456 */
2457 if ((fcport->flags & FCF_FABRIC_DEVICE) == 0) {
2458 fcport->d_id.b24 = new_fcport->d_id.b24;
2459 fcport->loop_id = FC_NO_LOOP_ID;
2460 fcport->flags |= (FCF_FABRIC_DEVICE |
2461 FCF_LOGIN_NEEDED);
2462 fcport->flags &= ~FCF_PERSISTENT_BOUND;
2463 break;
2464 }
2465
2466 /*
2467 * Port ID changed or device was marked to be updated;
2468 * Log it out if still logged in and mark it for
2469 * relogin later.
2470 */
2471 fcport->d_id.b24 = new_fcport->d_id.b24;
2472 fcport->flags |= FCF_LOGIN_NEEDED;
2473 if (fcport->loop_id != FC_NO_LOOP_ID &&
2474 (fcport->flags & FCF_TAPE_PRESENT) == 0 &&
2475 fcport->port_type != FCT_INITIATOR &&
2476 fcport->port_type != FCT_BROADCAST) {
Andrew Vasquez1c7c6352005-07-06 10:30:57 -07002477 ha->isp_ops.fabric_logout(ha, fcport->loop_id,
2478 fcport->d_id.b.domain, fcport->d_id.b.area,
2479 fcport->d_id.b.al_pa);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002480 fcport->loop_id = FC_NO_LOOP_ID;
2481 }
2482
2483 break;
2484 }
2485
2486 if (found)
2487 continue;
2488
2489 /* If device was not in our fcports list, then add it. */
2490 list_add_tail(&new_fcport->list, new_fcports);
2491
2492 /* Allocate a new replacement fcport. */
2493 nxt_d_id.b24 = new_fcport->d_id.b24;
2494 new_fcport = qla2x00_alloc_fcport(ha, GFP_KERNEL);
2495 if (new_fcport == NULL) {
Jesper Juhlc9475cb2005-11-07 01:01:26 -08002496 kfree(swl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002497 return (QLA_MEMORY_ALLOC_FAILED);
2498 }
2499 new_fcport->flags |= (FCF_FABRIC_DEVICE | FCF_LOGIN_NEEDED);
2500 new_fcport->d_id.b24 = nxt_d_id.b24;
2501 }
2502
Jesper Juhlc9475cb2005-11-07 01:01:26 -08002503 kfree(swl);
2504 kfree(new_fcport);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002505
2506 if (!list_empty(new_fcports))
2507 ha->device_flags |= DFLG_FABRIC_DEVICES;
2508
2509 return (rval);
2510}
2511
2512/*
2513 * qla2x00_find_new_loop_id
2514 * Scan through our port list and find a new usable loop ID.
2515 *
2516 * Input:
2517 * ha: adapter state pointer.
2518 * dev: port structure pointer.
2519 *
2520 * Returns:
2521 * qla2x00 local function return status code.
2522 *
2523 * Context:
2524 * Kernel context.
2525 */
2526int
2527qla2x00_find_new_loop_id(scsi_qla_host_t *ha, fc_port_t *dev)
2528{
2529 int rval;
2530 int found;
2531 fc_port_t *fcport;
2532 uint16_t first_loop_id;
2533
2534 rval = QLA_SUCCESS;
2535
2536 /* Save starting loop ID. */
2537 first_loop_id = dev->loop_id;
2538
2539 for (;;) {
2540 /* Skip loop ID if already used by adapter. */
2541 if (dev->loop_id == ha->loop_id) {
2542 dev->loop_id++;
2543 }
2544
2545 /* Skip reserved loop IDs. */
Andrew Vasquez3d716442005-07-06 10:30:26 -07002546 while (qla2x00_is_reserved_id(ha, dev->loop_id)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002547 dev->loop_id++;
2548 }
2549
2550 /* Reset loop ID if passed the end. */
2551 if (dev->loop_id > ha->last_loop_id) {
2552 /* first loop ID. */
2553 dev->loop_id = ha->min_external_loopid;
2554 }
2555
2556 /* Check for loop ID being already in use. */
2557 found = 0;
2558 fcport = NULL;
2559 list_for_each_entry(fcport, &ha->fcports, list) {
2560 if (fcport->loop_id == dev->loop_id && fcport != dev) {
2561 /* ID possibly in use */
2562 found++;
2563 break;
2564 }
2565 }
2566
2567 /* If not in use then it is free to use. */
2568 if (!found) {
2569 break;
2570 }
2571
2572 /* ID in use. Try next value. */
2573 dev->loop_id++;
2574
2575 /* If wrap around. No free ID to use. */
2576 if (dev->loop_id == first_loop_id) {
2577 dev->loop_id = FC_NO_LOOP_ID;
2578 rval = QLA_FUNCTION_FAILED;
2579 break;
2580 }
2581 }
2582
2583 return (rval);
2584}
2585
2586/*
2587 * qla2x00_device_resync
2588 * Marks devices in the database that needs resynchronization.
2589 *
2590 * Input:
2591 * ha = adapter block pointer.
2592 *
2593 * Context:
2594 * Kernel context.
2595 */
2596static int
Andrew Vasquezfa2a1ce2005-07-06 10:32:07 -07002597qla2x00_device_resync(scsi_qla_host_t *ha)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002598{
2599 int rval;
2600 int rval2;
2601 uint32_t mask;
2602 fc_port_t *fcport;
2603 uint32_t rscn_entry;
2604 uint8_t rscn_out_iter;
2605 uint8_t format;
2606 port_id_t d_id;
2607
2608 rval = QLA_RSCNS_HANDLED;
2609
2610 while (ha->rscn_out_ptr != ha->rscn_in_ptr ||
2611 ha->flags.rscn_queue_overflow) {
2612
2613 rscn_entry = ha->rscn_queue[ha->rscn_out_ptr];
2614 format = MSB(MSW(rscn_entry));
2615 d_id.b.domain = LSB(MSW(rscn_entry));
2616 d_id.b.area = MSB(LSW(rscn_entry));
2617 d_id.b.al_pa = LSB(LSW(rscn_entry));
2618
2619 DEBUG(printk("scsi(%ld): RSCN queue entry[%d] = "
2620 "[%02x/%02x%02x%02x].\n",
2621 ha->host_no, ha->rscn_out_ptr, format, d_id.b.domain,
2622 d_id.b.area, d_id.b.al_pa));
2623
2624 ha->rscn_out_ptr++;
2625 if (ha->rscn_out_ptr == MAX_RSCN_COUNT)
2626 ha->rscn_out_ptr = 0;
2627
2628 /* Skip duplicate entries. */
2629 for (rscn_out_iter = ha->rscn_out_ptr;
2630 !ha->flags.rscn_queue_overflow &&
2631 rscn_out_iter != ha->rscn_in_ptr;
2632 rscn_out_iter = (rscn_out_iter ==
2633 (MAX_RSCN_COUNT - 1)) ? 0: rscn_out_iter + 1) {
2634
2635 if (rscn_entry != ha->rscn_queue[rscn_out_iter])
2636 break;
2637
2638 DEBUG(printk("scsi(%ld): Skipping duplicate RSCN queue "
2639 "entry found at [%d].\n", ha->host_no,
2640 rscn_out_iter));
2641
2642 ha->rscn_out_ptr = rscn_out_iter;
2643 }
2644
2645 /* Queue overflow, set switch default case. */
2646 if (ha->flags.rscn_queue_overflow) {
2647 DEBUG(printk("scsi(%ld): device_resync: rscn "
2648 "overflow.\n", ha->host_no));
2649
2650 format = 3;
2651 ha->flags.rscn_queue_overflow = 0;
2652 }
2653
2654 switch (format) {
2655 case 0:
2656 if (!IS_QLA2100(ha) && !IS_QLA2200(ha) &&
2657 !IS_QLA6312(ha) && !IS_QLA6322(ha) &&
Andrew Vasquez0107109e2005-07-06 10:31:37 -07002658 !IS_QLA24XX(ha) && !IS_QLA25XX(ha) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07002659 ha->flags.init_done) {
2660 /* Handle port RSCN via asyncronous IOCBs */
2661 rval2 = qla2x00_handle_port_rscn(ha, rscn_entry,
2662 NULL, 0);
2663 if (rval2 == QLA_SUCCESS)
2664 continue;
2665 }
2666 mask = 0xffffff;
2667 break;
2668 case 1:
2669 mask = 0xffff00;
2670 break;
2671 case 2:
2672 mask = 0xff0000;
2673 break;
2674 default:
2675 mask = 0x0;
2676 d_id.b24 = 0;
2677 ha->rscn_out_ptr = ha->rscn_in_ptr;
2678 break;
2679 }
2680
2681 rval = QLA_SUCCESS;
2682
2683 /* Abort any outstanding IO descriptors. */
2684 if (!IS_QLA2100(ha) && !IS_QLA2200(ha))
2685 qla2x00_cancel_io_descriptors(ha);
2686
2687 list_for_each_entry(fcport, &ha->fcports, list) {
2688 if ((fcport->flags & FCF_FABRIC_DEVICE) == 0 ||
2689 (fcport->d_id.b24 & mask) != d_id.b24 ||
2690 fcport->port_type == FCT_BROADCAST)
2691 continue;
2692
2693 if (atomic_read(&fcport->state) == FCS_ONLINE) {
2694 if (format != 3 ||
2695 fcport->port_type != FCT_INITIATOR) {
8482e1182005-04-17 15:04:54 -05002696 qla2x00_mark_device_lost(ha, fcport, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002697 }
2698 }
2699 fcport->flags &= ~FCF_FARP_DONE;
2700 }
2701 }
2702 return (rval);
2703}
2704
2705/*
2706 * qla2x00_fabric_dev_login
2707 * Login fabric target device and update FC port database.
2708 *
2709 * Input:
2710 * ha: adapter state pointer.
2711 * fcport: port structure list pointer.
2712 * next_loopid: contains value of a new loop ID that can be used
2713 * by the next login attempt.
2714 *
2715 * Returns:
2716 * qla2x00 local function return status code.
2717 *
2718 * Context:
2719 * Kernel context.
2720 */
2721static int
2722qla2x00_fabric_dev_login(scsi_qla_host_t *ha, fc_port_t *fcport,
2723 uint16_t *next_loopid)
2724{
2725 int rval;
2726 int retry;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07002727 uint8_t opts;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002728
2729 rval = QLA_SUCCESS;
2730 retry = 0;
2731
2732 rval = qla2x00_fabric_login(ha, fcport, next_loopid);
2733 if (rval == QLA_SUCCESS) {
Andrew Vasquez0107109e2005-07-06 10:31:37 -07002734 /* Send an ADISC to tape devices.*/
2735 opts = 0;
2736 if (fcport->flags & FCF_TAPE_PRESENT)
2737 opts |= BIT_1;
2738 rval = qla2x00_get_port_database(ha, fcport, opts);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002739 if (rval != QLA_SUCCESS) {
Andrew Vasquez1c7c6352005-07-06 10:30:57 -07002740 ha->isp_ops.fabric_logout(ha, fcport->loop_id,
2741 fcport->d_id.b.domain, fcport->d_id.b.area,
2742 fcport->d_id.b.al_pa);
Andrew Vasquez0107109e2005-07-06 10:31:37 -07002743 qla2x00_mark_device_lost(ha, fcport, 1);
2744
Linus Torvalds1da177e2005-04-16 15:20:36 -07002745 } else {
2746 qla2x00_update_fcport(ha, fcport);
2747 }
2748 }
2749
2750 return (rval);
2751}
2752
2753/*
2754 * qla2x00_fabric_login
2755 * Issue fabric login command.
2756 *
2757 * Input:
2758 * ha = adapter block pointer.
2759 * device = pointer to FC device type structure.
2760 *
2761 * Returns:
2762 * 0 - Login successfully
2763 * 1 - Login failed
2764 * 2 - Initiator device
2765 * 3 - Fatal error
2766 */
2767int
2768qla2x00_fabric_login(scsi_qla_host_t *ha, fc_port_t *fcport,
2769 uint16_t *next_loopid)
2770{
2771 int rval;
2772 int retry;
2773 uint16_t tmp_loopid;
2774 uint16_t mb[MAILBOX_REGISTER_COUNT];
2775
2776 retry = 0;
2777 tmp_loopid = 0;
2778
2779 for (;;) {
2780 DEBUG(printk("scsi(%ld): Trying Fabric Login w/loop id 0x%04x "
2781 "for port %02x%02x%02x.\n",
2782 ha->host_no, fcport->loop_id, fcport->d_id.b.domain,
2783 fcport->d_id.b.area, fcport->d_id.b.al_pa));
2784
2785 /* Login fcport on switch. */
Andrew Vasquezabbd8872005-07-06 10:30:05 -07002786 ha->isp_ops.fabric_login(ha, fcport->loop_id,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002787 fcport->d_id.b.domain, fcport->d_id.b.area,
2788 fcport->d_id.b.al_pa, mb, BIT_0);
2789 if (mb[0] == MBS_PORT_ID_USED) {
2790 /*
2791 * Device has another loop ID. The firmware team
Andrew Vasquez0107109e2005-07-06 10:31:37 -07002792 * recommends the driver perform an implicit login with
2793 * the specified ID again. The ID we just used is save
2794 * here so we return with an ID that can be tried by
2795 * the next login.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002796 */
2797 retry++;
2798 tmp_loopid = fcport->loop_id;
2799 fcport->loop_id = mb[1];
2800
2801 DEBUG(printk("Fabric Login: port in use - next "
2802 "loop id=0x%04x, port Id=%02x%02x%02x.\n",
2803 fcport->loop_id, fcport->d_id.b.domain,
2804 fcport->d_id.b.area, fcport->d_id.b.al_pa));
2805
2806 } else if (mb[0] == MBS_COMMAND_COMPLETE) {
2807 /*
2808 * Login succeeded.
2809 */
2810 if (retry) {
2811 /* A retry occurred before. */
2812 *next_loopid = tmp_loopid;
2813 } else {
2814 /*
2815 * No retry occurred before. Just increment the
2816 * ID value for next login.
2817 */
2818 *next_loopid = (fcport->loop_id + 1);
2819 }
2820
2821 if (mb[1] & BIT_0) {
2822 fcport->port_type = FCT_INITIATOR;
2823 } else {
2824 fcport->port_type = FCT_TARGET;
2825 if (mb[1] & BIT_1) {
2826 fcport->flags |= FCF_TAPE_PRESENT;
2827 }
2828 }
2829
Andrew Vasquezad3e0ed2005-08-26 19:08:10 -07002830 if (mb[10] & BIT_0)
2831 fcport->supported_classes |= FC_COS_CLASS2;
2832 if (mb[10] & BIT_1)
2833 fcport->supported_classes |= FC_COS_CLASS3;
2834
Linus Torvalds1da177e2005-04-16 15:20:36 -07002835 rval = QLA_SUCCESS;
2836 break;
2837 } else if (mb[0] == MBS_LOOP_ID_USED) {
2838 /*
2839 * Loop ID already used, try next loop ID.
2840 */
2841 fcport->loop_id++;
2842 rval = qla2x00_find_new_loop_id(ha, fcport);
2843 if (rval != QLA_SUCCESS) {
2844 /* Ran out of loop IDs to use */
2845 break;
2846 }
2847 } else if (mb[0] == MBS_COMMAND_ERROR) {
2848 /*
2849 * Firmware possibly timed out during login. If NO
2850 * retries are left to do then the device is declared
2851 * dead.
2852 */
2853 *next_loopid = fcport->loop_id;
Andrew Vasquez1c7c6352005-07-06 10:30:57 -07002854 ha->isp_ops.fabric_logout(ha, fcport->loop_id,
2855 fcport->d_id.b.domain, fcport->d_id.b.area,
2856 fcport->d_id.b.al_pa);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002857 qla2x00_mark_device_lost(ha, fcport, 1);
2858
2859 rval = 1;
2860 break;
2861 } else {
2862 /*
2863 * unrecoverable / not handled error
2864 */
2865 DEBUG2(printk("%s(%ld): failed=%x port_id=%02x%02x%02x "
Andrew Vasquezfa2a1ce2005-07-06 10:32:07 -07002866 "loop_id=%x jiffies=%lx.\n",
2867 __func__, ha->host_no, mb[0],
Linus Torvalds1da177e2005-04-16 15:20:36 -07002868 fcport->d_id.b.domain, fcport->d_id.b.area,
2869 fcport->d_id.b.al_pa, fcport->loop_id, jiffies));
2870
2871 *next_loopid = fcport->loop_id;
Andrew Vasquez1c7c6352005-07-06 10:30:57 -07002872 ha->isp_ops.fabric_logout(ha, fcport->loop_id,
2873 fcport->d_id.b.domain, fcport->d_id.b.area,
2874 fcport->d_id.b.al_pa);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002875 fcport->loop_id = FC_NO_LOOP_ID;
Andrew Vasquez0eedfcf2005-10-27 11:09:38 -07002876 fcport->login_retry = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002877
2878 rval = 3;
2879 break;
2880 }
2881 }
2882
2883 return (rval);
2884}
2885
2886/*
2887 * qla2x00_local_device_login
2888 * Issue local device login command.
2889 *
2890 * Input:
2891 * ha = adapter block pointer.
2892 * loop_id = loop id of device to login to.
2893 *
2894 * Returns (Where's the #define!!!!):
2895 * 0 - Login successfully
2896 * 1 - Login failed
2897 * 3 - Fatal error
2898 */
2899int
2900qla2x00_local_device_login(scsi_qla_host_t *ha, uint16_t loop_id)
2901{
2902 int rval;
2903 uint16_t mb[MAILBOX_REGISTER_COUNT];
2904
2905 memset(mb, 0, sizeof(mb));
2906 rval = qla2x00_login_local_device(ha, loop_id, mb, BIT_0);
2907 if (rval == QLA_SUCCESS) {
2908 /* Interrogate mailbox registers for any errors */
2909 if (mb[0] == MBS_COMMAND_ERROR)
2910 rval = 1;
2911 else if (mb[0] == MBS_COMMAND_PARAMETER_ERROR)
2912 /* device not in PCB table */
2913 rval = 3;
2914 }
2915
2916 return (rval);
2917}
2918
2919/*
2920 * qla2x00_loop_resync
2921 * Resync with fibre channel devices.
2922 *
2923 * Input:
2924 * ha = adapter block pointer.
2925 *
2926 * Returns:
2927 * 0 = success
2928 */
2929int
Andrew Vasquezfa2a1ce2005-07-06 10:32:07 -07002930qla2x00_loop_resync(scsi_qla_host_t *ha)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002931{
2932 int rval;
2933 uint32_t wait_time;
2934
2935 rval = QLA_SUCCESS;
2936
2937 atomic_set(&ha->loop_state, LOOP_UPDATE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002938 clear_bit(ISP_ABORT_RETRY, &ha->dpc_flags);
2939 if (ha->flags.online) {
2940 if (!(rval = qla2x00_fw_ready(ha))) {
2941 /* Wait at most MAX_TARGET RSCNs for a stable link. */
2942 wait_time = 256;
2943 do {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002944 atomic_set(&ha->loop_state, LOOP_UPDATE);
2945
Andrew Vasquez0107109e2005-07-06 10:31:37 -07002946 /* Issue a marker after FW becomes ready. */
2947 qla2x00_marker(ha, 0, 0, MK_SYNC_ALL);
2948 ha->marker_needed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002949
2950 /* Remap devices on Loop. */
2951 clear_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags);
2952
2953 qla2x00_configure_loop(ha);
2954 wait_time--;
2955 } while (!atomic_read(&ha->loop_down_timer) &&
2956 !(test_bit(ISP_ABORT_NEEDED, &ha->dpc_flags)) &&
2957 wait_time &&
2958 (test_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags)));
2959 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002960 }
2961
2962 if (test_bit(ISP_ABORT_NEEDED, &ha->dpc_flags)) {
2963 return (QLA_FUNCTION_FAILED);
2964 }
2965
2966 if (rval) {
2967 DEBUG2_3(printk("%s(): **** FAILED ****\n", __func__));
2968 }
2969
2970 return (rval);
2971}
2972
Linus Torvalds1da177e2005-04-16 15:20:36 -07002973void
2974qla2x00_rescan_fcports(scsi_qla_host_t *ha)
2975{
2976 int rescan_done;
2977 fc_port_t *fcport;
2978
2979 rescan_done = 0;
2980 list_for_each_entry(fcport, &ha->fcports, list) {
2981 if ((fcport->flags & FCF_RESCAN_NEEDED) == 0)
2982 continue;
2983
2984 qla2x00_update_fcport(ha, fcport);
2985 fcport->flags &= ~FCF_RESCAN_NEEDED;
2986
2987 rescan_done = 1;
2988 }
Andrew Vasquezfa2a1ce2005-07-06 10:32:07 -07002989 qla2x00_probe_for_all_luns(ha);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002990}
2991
2992/*
2993* qla2x00_abort_isp
2994* Resets ISP and aborts all outstanding commands.
2995*
2996* Input:
2997* ha = adapter block pointer.
2998*
2999* Returns:
3000* 0 = success
3001*/
3002int
3003qla2x00_abort_isp(scsi_qla_host_t *ha)
3004{
3005 unsigned long flags = 0;
3006 uint16_t cnt;
3007 srb_t *sp;
3008 uint8_t status = 0;
3009
3010 if (ha->flags.online) {
3011 ha->flags.online = 0;
3012 clear_bit(ISP_ABORT_NEEDED, &ha->dpc_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003013
3014 qla_printk(KERN_INFO, ha,
3015 "Performing ISP error recovery - ha= %p.\n", ha);
Andrew Vasquezabbd8872005-07-06 10:30:05 -07003016 ha->isp_ops.reset_chip(ha);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003017
3018 atomic_set(&ha->loop_down_timer, LOOP_DOWN_TIME);
3019 if (atomic_read(&ha->loop_state) != LOOP_DOWN) {
3020 atomic_set(&ha->loop_state, LOOP_DOWN);
3021 qla2x00_mark_all_devices_lost(ha);
3022 } else {
3023 if (!atomic_read(&ha->loop_down_timer))
3024 atomic_set(&ha->loop_down_timer,
3025 LOOP_DOWN_TIME);
3026 }
3027
3028 spin_lock_irqsave(&ha->hardware_lock, flags);
3029 /* Requeue all commands in outstanding command list. */
3030 for (cnt = 1; cnt < MAX_OUTSTANDING_COMMANDS; cnt++) {
3031 sp = ha->outstanding_cmds[cnt];
3032 if (sp) {
3033 ha->outstanding_cmds[cnt] = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003034 sp->flags = 0;
f4f051e2005-04-17 15:02:26 -05003035 sp->cmd->result = DID_RESET << 16;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003036 sp->cmd->host_scribble = (unsigned char *)NULL;
f4f051e2005-04-17 15:02:26 -05003037 qla2x00_sp_compl(ha, sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003038 }
3039 }
3040 spin_unlock_irqrestore(&ha->hardware_lock, flags);
3041
Andrew Vasquezabbd8872005-07-06 10:30:05 -07003042 ha->isp_ops.nvram_config(ha);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003043
3044 if (!qla2x00_restart_isp(ha)) {
3045 clear_bit(RESET_MARKER_NEEDED, &ha->dpc_flags);
3046
3047 if (!atomic_read(&ha->loop_down_timer)) {
3048 /*
3049 * Issue marker command only when we are going
3050 * to start the I/O .
3051 */
3052 ha->marker_needed = 1;
3053 }
3054
3055 ha->flags.online = 1;
3056
Andrew Vasquezabbd8872005-07-06 10:30:05 -07003057 ha->isp_ops.enable_intrs(ha);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003058
Andrew Vasquezfa2a1ce2005-07-06 10:32:07 -07003059 ha->isp_abort_cnt = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003060 clear_bit(ISP_ABORT_RETRY, &ha->dpc_flags);
3061 } else { /* failed the ISP abort */
3062 ha->flags.online = 1;
3063 if (test_bit(ISP_ABORT_RETRY, &ha->dpc_flags)) {
3064 if (ha->isp_abort_cnt == 0) {
3065 qla_printk(KERN_WARNING, ha,
3066 "ISP error recovery failed - "
3067 "board disabled\n");
Andrew Vasquezfa2a1ce2005-07-06 10:32:07 -07003068 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003069 * The next call disables the board
3070 * completely.
3071 */
Andrew Vasquezabbd8872005-07-06 10:30:05 -07003072 ha->isp_ops.reset_adapter(ha);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003073 ha->flags.online = 0;
3074 clear_bit(ISP_ABORT_RETRY,
3075 &ha->dpc_flags);
3076 status = 0;
3077 } else { /* schedule another ISP abort */
3078 ha->isp_abort_cnt--;
3079 DEBUG(printk("qla%ld: ISP abort - "
Andrew Vasquez0107109e2005-07-06 10:31:37 -07003080 "retry remaining %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003081 ha->host_no, ha->isp_abort_cnt);)
3082 status = 1;
3083 }
3084 } else {
3085 ha->isp_abort_cnt = MAX_RETRIES_OF_ISP_ABORT;
3086 DEBUG(printk("qla2x00(%ld): ISP error recovery "
3087 "- retrying (%d) more times\n",
3088 ha->host_no, ha->isp_abort_cnt);)
3089 set_bit(ISP_ABORT_RETRY, &ha->dpc_flags);
3090 status = 1;
3091 }
3092 }
Andrew Vasquezfa2a1ce2005-07-06 10:32:07 -07003093
Linus Torvalds1da177e2005-04-16 15:20:36 -07003094 }
3095
3096 if (status) {
3097 qla_printk(KERN_INFO, ha,
3098 "qla2x00_abort_isp: **** FAILED ****\n");
3099 } else {
3100 DEBUG(printk(KERN_INFO
3101 "qla2x00_abort_isp(%ld): exiting.\n",
3102 ha->host_no);)
3103 }
3104
3105 return(status);
3106}
3107
3108/*
3109* qla2x00_restart_isp
3110* restarts the ISP after a reset
3111*
3112* Input:
3113* ha = adapter block pointer.
3114*
3115* Returns:
3116* 0 = success
3117*/
3118static int
3119qla2x00_restart_isp(scsi_qla_host_t *ha)
3120{
3121 uint8_t status = 0;
Andrew Vasquez3d716442005-07-06 10:30:26 -07003122 struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003123 unsigned long flags = 0;
3124 uint32_t wait_time;
3125
3126 /* If firmware needs to be loaded */
3127 if (qla2x00_isp_firmware(ha)) {
3128 ha->flags.online = 0;
Andrew Vasquezabbd8872005-07-06 10:30:05 -07003129 if (!(status = ha->isp_ops.chip_diag(ha))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003130 if (IS_QLA2100(ha) || IS_QLA2200(ha)) {
3131 status = qla2x00_setup_chip(ha);
3132 goto done;
3133 }
3134
Linus Torvalds1da177e2005-04-16 15:20:36 -07003135 spin_lock_irqsave(&ha->hardware_lock, flags);
3136
Andrew Vasquez0107109e2005-07-06 10:31:37 -07003137 if (!IS_QLA24XX(ha) && !IS_QLA25XX(ha)) {
3138 /*
3139 * Disable SRAM, Instruction RAM and GP RAM
3140 * parity.
3141 */
3142 WRT_REG_WORD(&reg->hccr,
3143 (HCCR_ENABLE_PARITY + 0x0));
3144 RD_REG_WORD(&reg->hccr);
3145 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003146
3147 spin_unlock_irqrestore(&ha->hardware_lock, flags);
Andrew Vasquezfa2a1ce2005-07-06 10:32:07 -07003148
Linus Torvalds1da177e2005-04-16 15:20:36 -07003149 status = qla2x00_setup_chip(ha);
3150
3151 spin_lock_irqsave(&ha->hardware_lock, flags);
Andrew Vasquezfa2a1ce2005-07-06 10:32:07 -07003152
Andrew Vasquez0107109e2005-07-06 10:31:37 -07003153 if (!IS_QLA24XX(ha) && !IS_QLA25XX(ha)) {
3154 /* Enable proper parity */
3155 if (IS_QLA2300(ha))
3156 /* SRAM parity */
3157 WRT_REG_WORD(&reg->hccr,
3158 (HCCR_ENABLE_PARITY + 0x1));
3159 else
3160 /*
3161 * SRAM, Instruction RAM and GP RAM
3162 * parity.
3163 */
3164 WRT_REG_WORD(&reg->hccr,
3165 (HCCR_ENABLE_PARITY + 0x7));
3166 RD_REG_WORD(&reg->hccr);
3167 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003168
3169 spin_unlock_irqrestore(&ha->hardware_lock, flags);
3170 }
3171 }
3172
3173 done:
3174 if (!status && !(status = qla2x00_init_rings(ha))) {
3175 clear_bit(RESET_MARKER_NEEDED, &ha->dpc_flags);
3176 if (!(status = qla2x00_fw_ready(ha))) {
3177 DEBUG(printk("%s(): Start configure loop, "
Andrew Vasquez0107109e2005-07-06 10:31:37 -07003178 "status = %d\n", __func__, status);)
3179
3180 /* Issue a marker after FW becomes ready. */
3181 qla2x00_marker(ha, 0, 0, MK_SYNC_ALL);
3182
Linus Torvalds1da177e2005-04-16 15:20:36 -07003183 ha->flags.online = 1;
3184 /* Wait at most MAX_TARGET RSCNs for a stable link. */
3185 wait_time = 256;
3186 do {
3187 clear_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags);
3188 qla2x00_configure_loop(ha);
3189 wait_time--;
3190 } while (!atomic_read(&ha->loop_down_timer) &&
3191 !(test_bit(ISP_ABORT_NEEDED, &ha->dpc_flags)) &&
3192 wait_time &&
3193 (test_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags)));
3194 }
3195
3196 /* if no cable then assume it's good */
Andrew Vasquezfa2a1ce2005-07-06 10:32:07 -07003197 if ((ha->device_flags & DFLG_NO_CABLE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003198 status = 0;
3199
3200 DEBUG(printk("%s(): Configure loop done, status = 0x%x\n",
3201 __func__,
3202 status);)
3203 }
3204 return (status);
3205}
3206
3207/*
3208* qla2x00_reset_adapter
3209* Reset adapter.
3210*
3211* Input:
3212* ha = adapter block pointer.
3213*/
Andrew Vasquezabbd8872005-07-06 10:30:05 -07003214void
Linus Torvalds1da177e2005-04-16 15:20:36 -07003215qla2x00_reset_adapter(scsi_qla_host_t *ha)
3216{
3217 unsigned long flags = 0;
Andrew Vasquez3d716442005-07-06 10:30:26 -07003218 struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003219
3220 ha->flags.online = 0;
Andrew Vasquezabbd8872005-07-06 10:30:05 -07003221 ha->isp_ops.disable_intrs(ha);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003222
Linus Torvalds1da177e2005-04-16 15:20:36 -07003223 spin_lock_irqsave(&ha->hardware_lock, flags);
3224 WRT_REG_WORD(&reg->hccr, HCCR_RESET_RISC);
3225 RD_REG_WORD(&reg->hccr); /* PCI Posting. */
3226 WRT_REG_WORD(&reg->hccr, HCCR_RELEASE_RISC);
3227 RD_REG_WORD(&reg->hccr); /* PCI Posting. */
3228 spin_unlock_irqrestore(&ha->hardware_lock, flags);
3229}
Andrew Vasquez0107109e2005-07-06 10:31:37 -07003230
3231void
3232qla24xx_reset_adapter(scsi_qla_host_t *ha)
3233{
3234 unsigned long flags = 0;
3235 struct device_reg_24xx __iomem *reg = &ha->iobase->isp24;
3236
3237 ha->flags.online = 0;
3238 ha->isp_ops.disable_intrs(ha);
3239
3240 spin_lock_irqsave(&ha->hardware_lock, flags);
3241 WRT_REG_DWORD(&reg->hccr, HCCRX_SET_RISC_RESET);
3242 RD_REG_DWORD(&reg->hccr);
3243 WRT_REG_DWORD(&reg->hccr, HCCRX_REL_RISC_PAUSE);
3244 RD_REG_DWORD(&reg->hccr);
3245 spin_unlock_irqrestore(&ha->hardware_lock, flags);
3246}
3247
3248int
3249qla24xx_nvram_config(scsi_qla_host_t *ha)
3250{
3251 int rval;
3252 struct init_cb_24xx *icb;
3253 struct nvram_24xx *nv;
3254 uint32_t *dptr;
3255 uint8_t *dptr1, *dptr2;
3256 uint32_t chksum;
3257 uint16_t cnt;
3258
3259 rval = QLA_SUCCESS;
3260 icb = (struct init_cb_24xx *)ha->init_cb;
3261 nv = (struct nvram_24xx *)ha->request_ring;
3262
3263 /* Determine NVRAM starting address. */
3264 ha->nvram_size = sizeof(struct nvram_24xx);
3265 ha->nvram_base = FA_NVRAM_FUNC0_ADDR;
3266 if (PCI_FUNC(ha->pdev->devfn))
3267 ha->nvram_base = FA_NVRAM_FUNC1_ADDR;
3268
3269 /* Get NVRAM data and calculate checksum. */
3270 dptr = (uint32_t *)nv;
3271 ha->isp_ops.read_nvram(ha, (uint8_t *)dptr, ha->nvram_base,
3272 ha->nvram_size);
3273 for (cnt = 0, chksum = 0; cnt < ha->nvram_size >> 2; cnt++)
3274 chksum += le32_to_cpu(*dptr++);
3275
3276 DEBUG5(printk("scsi(%ld): Contents of NVRAM\n", ha->host_no));
3277 DEBUG5(qla2x00_dump_buffer((uint8_t *)ha->request_ring,
3278 ha->nvram_size));
3279
3280 /* Bad NVRAM data, set defaults parameters. */
3281 if (chksum || nv->id[0] != 'I' || nv->id[1] != 'S' || nv->id[2] != 'P'
3282 || nv->id[3] != ' ' ||
3283 nv->nvram_version < __constant_cpu_to_le16(ICB_VERSION)) {
3284 /* Reset NVRAM data. */
3285 qla_printk(KERN_WARNING, ha, "Inconsistent NVRAM detected: "
3286 "checksum=0x%x id=%c version=0x%x.\n", chksum, nv->id[0],
3287 le16_to_cpu(nv->nvram_version));
3288 qla_printk(KERN_WARNING, ha, "Falling back to functioning (yet "
3289 "invalid -- WWPN) defaults.\n");
3290
3291 /*
3292 * Set default initialization control block.
3293 */
3294 memset(nv, 0, ha->nvram_size);
3295 nv->nvram_version = __constant_cpu_to_le16(ICB_VERSION);
3296 nv->version = __constant_cpu_to_le16(ICB_VERSION);
3297 nv->frame_payload_size = __constant_cpu_to_le16(2048);
3298 nv->execution_throttle = __constant_cpu_to_le16(0xFFFF);
3299 nv->exchange_count = __constant_cpu_to_le16(0);
3300 nv->hard_address = __constant_cpu_to_le16(124);
3301 nv->port_name[0] = 0x21;
3302 nv->port_name[1] = 0x00 + PCI_FUNC(ha->pdev->devfn);
3303 nv->port_name[2] = 0x00;
3304 nv->port_name[3] = 0xe0;
3305 nv->port_name[4] = 0x8b;
3306 nv->port_name[5] = 0x1c;
3307 nv->port_name[6] = 0x55;
3308 nv->port_name[7] = 0x86;
3309 nv->node_name[0] = 0x20;
3310 nv->node_name[1] = 0x00;
3311 nv->node_name[2] = 0x00;
3312 nv->node_name[3] = 0xe0;
3313 nv->node_name[4] = 0x8b;
3314 nv->node_name[5] = 0x1c;
3315 nv->node_name[6] = 0x55;
3316 nv->node_name[7] = 0x86;
3317 nv->login_retry_count = __constant_cpu_to_le16(8);
3318 nv->link_down_timeout = __constant_cpu_to_le16(200);
3319 nv->interrupt_delay_timer = __constant_cpu_to_le16(0);
3320 nv->login_timeout = __constant_cpu_to_le16(0);
3321 nv->firmware_options_1 =
3322 __constant_cpu_to_le32(BIT_14|BIT_13|BIT_2|BIT_1);
3323 nv->firmware_options_2 = __constant_cpu_to_le32(2 << 4);
3324 nv->firmware_options_2 |= __constant_cpu_to_le32(BIT_12);
3325 nv->firmware_options_3 = __constant_cpu_to_le32(2 << 13);
3326 nv->host_p = __constant_cpu_to_le32(BIT_11|BIT_10);
3327 nv->efi_parameters = __constant_cpu_to_le32(0);
3328 nv->reset_delay = 5;
3329 nv->max_luns_per_target = __constant_cpu_to_le16(128);
3330 nv->port_down_retry_count = __constant_cpu_to_le16(30);
3331 nv->link_down_timeout = __constant_cpu_to_le16(30);
3332
3333 rval = 1;
3334 }
3335
3336 /* Reset Initialization control block */
3337 memset(icb, 0, sizeof(struct init_cb_24xx));
3338
3339 /* Copy 1st segment. */
3340 dptr1 = (uint8_t *)icb;
3341 dptr2 = (uint8_t *)&nv->version;
3342 cnt = (uint8_t *)&icb->response_q_inpointer - (uint8_t *)&icb->version;
3343 while (cnt--)
3344 *dptr1++ = *dptr2++;
3345
3346 icb->login_retry_count = nv->login_retry_count;
3347 icb->link_down_timeout = nv->link_down_timeout;
3348
3349 /* Copy 2nd segment. */
3350 dptr1 = (uint8_t *)&icb->interrupt_delay_timer;
3351 dptr2 = (uint8_t *)&nv->interrupt_delay_timer;
3352 cnt = (uint8_t *)&icb->reserved_3 -
3353 (uint8_t *)&icb->interrupt_delay_timer;
3354 while (cnt--)
3355 *dptr1++ = *dptr2++;
3356
3357 /*
3358 * Setup driver NVRAM options.
3359 */
3360 if (memcmp(nv->model_name, BINZERO, sizeof(nv->model_name)) != 0) {
3361 char *st, *en;
3362 uint16_t index;
3363
3364 strncpy(ha->model_number, nv->model_name,
3365 sizeof(nv->model_name));
3366 st = en = ha->model_number;
3367 en += sizeof(nv->model_name) - 1;
3368 while (en > st) {
3369 if (*en != 0x20 && *en != 0x00)
3370 break;
3371 *en-- = '\0';
3372 }
3373
3374 index = (ha->pdev->subsystem_device & 0xff);
3375 if (index < QLA_MODEL_NAMES)
3376 ha->model_desc = qla2x00_model_desc[index];
3377 } else
3378 strcpy(ha->model_number, "QLA2462");
3379
3380 /* Prepare nodename */
3381 if ((icb->firmware_options_1 & BIT_14) == 0) {
3382 /*
3383 * Firmware will apply the following mask if the nodename was
3384 * not provided.
3385 */
3386 memcpy(icb->node_name, icb->port_name, WWN_SIZE);
3387 icb->node_name[0] &= 0xF0;
3388 }
3389
3390 /* Set host adapter parameters. */
3391 ha->flags.disable_risc_code_load = 0;
3392 ha->flags.enable_lip_reset = 1;
3393 ha->flags.enable_lip_full_login = 1;
3394 ha->flags.enable_target_reset = 1;
3395 ha->flags.enable_led_scheme = 0;
3396
3397 ha->operating_mode =
3398 (icb->firmware_options_2 & (BIT_6 | BIT_5 | BIT_4)) >> 4;
3399
3400 memcpy(ha->fw_seriallink_options24, nv->seriallink_options,
3401 sizeof(ha->fw_seriallink_options24));
3402
3403 /* save HBA serial number */
3404 ha->serial0 = icb->port_name[5];
3405 ha->serial1 = icb->port_name[6];
3406 ha->serial2 = icb->port_name[7];
3407 ha->node_name = icb->node_name;
3408 ha->port_name = icb->port_name;
3409
3410 ha->retry_count = le16_to_cpu(nv->login_retry_count);
3411
3412 /* Set minimum login_timeout to 4 seconds. */
3413 if (le16_to_cpu(nv->login_timeout) < ql2xlogintimeout)
3414 nv->login_timeout = cpu_to_le16(ql2xlogintimeout);
3415 if (le16_to_cpu(nv->login_timeout) < 4)
3416 nv->login_timeout = __constant_cpu_to_le16(4);
3417 ha->login_timeout = le16_to_cpu(nv->login_timeout);
3418 icb->login_timeout = cpu_to_le16(nv->login_timeout);
3419
3420 /* Set minimum RATOV to 200 tenths of a second. */
3421 ha->r_a_tov = 200;
3422
3423 ha->loop_reset_delay = nv->reset_delay;
3424
3425 /* Link Down Timeout = 0:
3426 *
3427 * When Port Down timer expires we will start returning
3428 * I/O's to OS with "DID_NO_CONNECT".
3429 *
3430 * Link Down Timeout != 0:
3431 *
3432 * The driver waits for the link to come up after link down
3433 * before returning I/Os to OS with "DID_NO_CONNECT".
3434 */
3435 if (le16_to_cpu(nv->link_down_timeout) == 0) {
3436 ha->loop_down_abort_time =
3437 (LOOP_DOWN_TIME - LOOP_DOWN_TIMEOUT);
3438 } else {
3439 ha->link_down_timeout = le16_to_cpu(nv->link_down_timeout);
3440 ha->loop_down_abort_time =
3441 (LOOP_DOWN_TIME - ha->link_down_timeout);
3442 }
3443
3444 /* Need enough time to try and get the port back. */
3445 ha->port_down_retry_count = le16_to_cpu(nv->port_down_retry_count);
3446 if (qlport_down_retry)
3447 ha->port_down_retry_count = qlport_down_retry;
3448
3449 /* Set login_retry_count */
3450 ha->login_retry_count = le16_to_cpu(nv->login_retry_count);
3451 if (ha->port_down_retry_count ==
3452 le16_to_cpu(nv->port_down_retry_count) &&
3453 ha->port_down_retry_count > 3)
3454 ha->login_retry_count = ha->port_down_retry_count;
3455 else if (ha->port_down_retry_count > (int)ha->login_retry_count)
3456 ha->login_retry_count = ha->port_down_retry_count;
3457 if (ql2xloginretrycount)
3458 ha->login_retry_count = ql2xloginretrycount;
3459
Andrew Vasquez4fdfefe2005-10-27 11:09:48 -07003460 /* Enable ZIO. */
3461 if (!ha->flags.init_done) {
3462 ha->zio_mode = le32_to_cpu(icb->firmware_options_2) &
3463 (BIT_3 | BIT_2 | BIT_1 | BIT_0);
3464 ha->zio_timer = le16_to_cpu(icb->interrupt_delay_timer) ?
3465 le16_to_cpu(icb->interrupt_delay_timer): 2;
3466 }
3467 icb->firmware_options_2 &= __constant_cpu_to_le32(
3468 ~(BIT_3 | BIT_2 | BIT_1 | BIT_0));
3469 ha->flags.process_response_queue = 0;
3470 if (ha->zio_mode != QLA_ZIO_DISABLED) {
3471 DEBUG2(printk("scsi(%ld): ZIO mode %d enabled; timer delay "
3472 "(%d us).\n", ha->host_no, ha->zio_mode,
3473 ha->zio_timer * 100));
3474 qla_printk(KERN_INFO, ha,
3475 "ZIO mode %d enabled; timer delay (%d us).\n",
3476 ha->zio_mode, ha->zio_timer * 100);
3477
3478 icb->firmware_options_2 |= cpu_to_le32(
3479 (uint32_t)ha->zio_mode);
3480 icb->interrupt_delay_timer = cpu_to_le16(ha->zio_timer);
3481 ha->flags.process_response_queue = 1;
3482 }
3483
Andrew Vasquez0107109e2005-07-06 10:31:37 -07003484 if (rval) {
3485 DEBUG2_3(printk(KERN_WARNING
3486 "scsi(%ld): NVRAM configuration failed!\n", ha->host_no));
3487 }
3488 return (rval);
3489}
3490
Andrew Vasquez54333832005-11-09 15:49:04 -08003491#if defined(CONFIG_SCSI_QLA2XXX_EMBEDDED_FIRMWARE)
3492
Andrew Vasquez0107109e2005-07-06 10:31:37 -07003493int
3494qla2x00_load_risc(scsi_qla_host_t *ha, uint32_t *srisc_addr)
3495{
Andrew Vasquez54333832005-11-09 15:49:04 -08003496 int rval, num, i;
3497 uint32_t cnt;
3498 uint16_t *risc_code;
3499 uint32_t risc_addr, risc_size;
3500 uint16_t *req_ring;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07003501 struct qla_fw_info *fw_iter;
3502
3503 rval = QLA_SUCCESS;
3504
3505 /* Load firmware sequences */
3506 fw_iter = ha->brd_info->fw_info;
3507 *srisc_addr = *ha->brd_info->fw_info->fwstart;
3508 while (fw_iter->addressing != FW_INFO_ADDR_NOMORE) {
3509 risc_code = fw_iter->fwcode;
Andrew Vasquez54333832005-11-09 15:49:04 -08003510 risc_size = *fw_iter->fwlen;
3511 if (fw_iter->addressing == FW_INFO_ADDR_NORMAL)
3512 risc_addr = *fw_iter->fwstart;
3513 else
3514 risc_addr = *fw_iter->lfwstart;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07003515
3516 num = 0;
3517 rval = 0;
Andrew Vasquez54333832005-11-09 15:49:04 -08003518 while (risc_size > 0 && !rval) {
Andrew Vasquez0107109e2005-07-06 10:31:37 -07003519 cnt = (uint16_t)(ha->fw_transfer_size >> 1);
Andrew Vasquez54333832005-11-09 15:49:04 -08003520 if (cnt > risc_size)
3521 cnt = risc_size;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07003522
3523 DEBUG7(printk("scsi(%ld): Loading risc segment@ "
3524 "addr %p, number of bytes 0x%x, offset 0x%lx.\n",
Andrew Vasquez54333832005-11-09 15:49:04 -08003525 ha->host_no, risc_code, cnt, risc_addr));
Andrew Vasquez0107109e2005-07-06 10:31:37 -07003526
3527 req_ring = (uint16_t *)ha->request_ring;
3528 for (i = 0; i < cnt; i++)
3529 req_ring[i] = cpu_to_le16(risc_code[i]);
3530
Andrew Vasquez54333832005-11-09 15:49:04 -08003531 rval = qla2x00_load_ram(ha, ha->request_dma, risc_addr,
3532 cnt);
Andrew Vasquez0107109e2005-07-06 10:31:37 -07003533 if (rval) {
3534 DEBUG(printk("scsi(%ld): [ERROR] Failed to "
3535 "load segment %d of firmware\n",
3536 ha->host_no, num));
3537 qla_printk(KERN_WARNING, ha,
3538 "[ERROR] Failed to load segment %d of "
3539 "firmware\n", num);
3540
3541 qla2x00_dump_regs(ha);
3542 break;
3543 }
3544
3545 risc_code += cnt;
Andrew Vasquez54333832005-11-09 15:49:04 -08003546 risc_addr += cnt;
3547 risc_size -= cnt;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07003548 num++;
3549 }
3550
3551 /* Next firmware sequence */
3552 fw_iter++;
3553 }
Andrew Vasquez54333832005-11-09 15:49:04 -08003554 return rval;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07003555}
3556
3557int
Andrew Vasquez331e3472005-11-09 15:49:19 -08003558qla24xx_load_risc(scsi_qla_host_t *ha, uint32_t *srisc_addr)
3559{
3560 int rval, num, i;
3561 uint32_t cnt;
3562 uint32_t *risc_code;
3563 uint32_t risc_addr, risc_size;
3564 uint32_t *req_ring;
3565 struct qla_fw_info *fw_iter;
3566
3567 rval = QLA_SUCCESS;
3568
3569 /* Load firmware sequences */
3570 fw_iter = ha->brd_info->fw_info;
3571 *srisc_addr = *((uint32_t *)fw_iter->lfwstart);
3572 while (fw_iter->addressing != FW_INFO_ADDR_NOMORE) {
3573 risc_code = (uint32_t *)fw_iter->fwcode;
3574 risc_size = *((uint32_t *)fw_iter->fwlen);
3575 risc_addr = *((uint32_t *)fw_iter->lfwstart);
3576
3577 num = 0;
3578 rval = 0;
3579 while (risc_size > 0 && !rval) {
3580 cnt = (uint32_t)(ha->fw_transfer_size >> 2);
3581 if (cnt > risc_size)
3582 cnt = risc_size;
3583
3584 DEBUG7(printk("scsi(%ld): Loading risc segment@ "
3585 "addr %p, number of bytes 0x%x, offset 0x%lx.\n",
3586 ha->host_no, risc_code, cnt, risc_addr));
3587
3588 req_ring = (uint32_t *)ha->request_ring;
3589 for (i = 0; i < cnt; i++)
3590 req_ring[i] = cpu_to_le32(risc_code[i]);
3591
3592 rval = qla2x00_load_ram(ha, ha->request_dma, risc_addr,
3593 cnt);
3594 if (rval) {
3595 DEBUG(printk("scsi(%ld): [ERROR] Failed to "
3596 "load segment %d of firmware\n",
3597 ha->host_no, num));
3598 qla_printk(KERN_WARNING, ha,
3599 "[ERROR] Failed to load segment %d of "
3600 "firmware\n", num);
3601
3602 qla2x00_dump_regs(ha);
3603 break;
3604 }
3605
3606 risc_code += cnt;
3607 risc_addr += cnt;
3608 risc_size -= cnt;
3609 num++;
3610 }
3611
3612 /* Next firmware sequence */
3613 fw_iter++;
3614 }
3615 return rval;
3616}
3617
3618int
Andrew Vasquez0107109e2005-07-06 10:31:37 -07003619qla24xx_load_risc_flash(scsi_qla_host_t *ha, uint32_t *srisc_addr)
3620{
3621 int rval;
3622 int segments, fragment;
3623 uint32_t faddr;
3624 uint32_t *dcode, dlen;
3625 uint32_t risc_addr;
3626 uint32_t risc_size;
3627 uint32_t i;
3628
3629 rval = QLA_SUCCESS;
3630
3631 segments = FA_RISC_CODE_SEGMENTS;
3632 faddr = FA_RISC_CODE_ADDR;
3633 dcode = (uint32_t *)ha->request_ring;
3634 *srisc_addr = 0;
3635
3636 /* Validate firmware image by checking version. */
3637 qla24xx_read_flash_data(ha, dcode, faddr + 4, 4);
3638 for (i = 0; i < 4; i++)
3639 dcode[i] = be32_to_cpu(dcode[i]);
3640 if ((dcode[0] == 0xffffffff && dcode[1] == 0xffffffff &&
3641 dcode[2] == 0xffffffff && dcode[3] == 0xffffffff) ||
3642 (dcode[0] == 0 && dcode[1] == 0 && dcode[2] == 0 &&
3643 dcode[3] == 0)) {
3644 qla_printk(KERN_WARNING, ha,
3645 "Unable to verify integrity of flash firmware image!\n");
3646 qla_printk(KERN_WARNING, ha,
3647 "Firmware data: %08x %08x %08x %08x!\n", dcode[0],
3648 dcode[1], dcode[2], dcode[3]);
3649
3650 return QLA_FUNCTION_FAILED;
3651 }
3652
3653 while (segments && rval == QLA_SUCCESS) {
3654 /* Read segment's load information. */
3655 qla24xx_read_flash_data(ha, dcode, faddr, 4);
3656
3657 risc_addr = be32_to_cpu(dcode[2]);
3658 *srisc_addr = *srisc_addr == 0 ? risc_addr : *srisc_addr;
3659 risc_size = be32_to_cpu(dcode[3]);
3660
3661 fragment = 0;
3662 while (risc_size > 0 && rval == QLA_SUCCESS) {
3663 dlen = (uint32_t)(ha->fw_transfer_size >> 2);
3664 if (dlen > risc_size)
3665 dlen = risc_size;
3666
3667 DEBUG7(printk("scsi(%ld): Loading risc segment@ risc "
3668 "addr %x, number of dwords 0x%x, offset 0x%x.\n",
3669 ha->host_no, risc_addr, dlen, faddr));
3670
3671 qla24xx_read_flash_data(ha, dcode, faddr, dlen);
3672 for (i = 0; i < dlen; i++)
3673 dcode[i] = swab32(dcode[i]);
3674
andrew.vasquez@qlogic.com590f98e2006-01-13 17:05:37 -08003675 rval = qla2x00_load_ram(ha, ha->request_dma, risc_addr,
3676 dlen);
Andrew Vasquez0107109e2005-07-06 10:31:37 -07003677 if (rval) {
3678 DEBUG(printk("scsi(%ld):[ERROR] Failed to load "
3679 "segment %d of firmware\n", ha->host_no,
3680 fragment));
3681 qla_printk(KERN_WARNING, ha,
3682 "[ERROR] Failed to load segment %d of "
3683 "firmware\n", fragment);
3684 break;
3685 }
3686
3687 faddr += dlen;
3688 risc_addr += dlen;
3689 risc_size -= dlen;
3690 fragment++;
3691 }
3692
3693 /* Next segment. */
3694 segments--;
3695 }
3696
3697 return rval;
3698}
3699
Andrew Vasquez54333832005-11-09 15:49:04 -08003700#else /* !defined(CONFIG_SCSI_QLA2XXX_EMBEDDED_FIRMWARE) */
3701
Andrew Vasquez0107109e2005-07-06 10:31:37 -07003702int
Andrew Vasquez54333832005-11-09 15:49:04 -08003703qla2x00_load_risc(scsi_qla_host_t *ha, uint32_t *srisc_addr)
3704{
3705 int rval;
3706 int i, fragment;
3707 uint16_t *wcode, *fwcode;
3708 uint32_t risc_addr, risc_size, fwclen, wlen, *seg;
3709 struct fw_blob *blob;
3710
3711 /* Load firmware blob. */
3712 blob = qla2x00_request_firmware(ha);
3713 if (!blob) {
3714 qla_printk(KERN_ERR, ha, "Firmware image unavailable.\n");
3715 return QLA_FUNCTION_FAILED;
3716 }
3717
3718 rval = QLA_SUCCESS;
3719
3720 wcode = (uint16_t *)ha->request_ring;
3721 *srisc_addr = 0;
3722 fwcode = (uint16_t *)blob->fw->data;
3723 fwclen = 0;
3724
3725 /* Validate firmware image by checking version. */
3726 if (blob->fw->size < 8 * sizeof(uint16_t)) {
3727 qla_printk(KERN_WARNING, ha,
3728 "Unable to verify integrity of firmware image (%Zd)!\n",
3729 blob->fw->size);
3730 goto fail_fw_integrity;
3731 }
3732 for (i = 0; i < 4; i++)
3733 wcode[i] = be16_to_cpu(fwcode[i + 4]);
3734 if ((wcode[0] == 0xffff && wcode[1] == 0xffff && wcode[2] == 0xffff &&
3735 wcode[3] == 0xffff) || (wcode[0] == 0 && wcode[1] == 0 &&
3736 wcode[2] == 0 && wcode[3] == 0)) {
3737 qla_printk(KERN_WARNING, ha,
3738 "Unable to verify integrity of firmware image!\n");
3739 qla_printk(KERN_WARNING, ha,
3740 "Firmware data: %04x %04x %04x %04x!\n", wcode[0],
3741 wcode[1], wcode[2], wcode[3]);
3742 goto fail_fw_integrity;
3743 }
3744
3745 seg = blob->segs;
3746 while (*seg && rval == QLA_SUCCESS) {
3747 risc_addr = *seg;
3748 *srisc_addr = *srisc_addr == 0 ? *seg : *srisc_addr;
3749 risc_size = be16_to_cpu(fwcode[3]);
3750
3751 /* Validate firmware image size. */
3752 fwclen += risc_size * sizeof(uint16_t);
3753 if (blob->fw->size < fwclen) {
3754 qla_printk(KERN_WARNING, ha,
3755 "Unable to verify integrity of firmware image "
3756 "(%Zd)!\n", blob->fw->size);
3757 goto fail_fw_integrity;
3758 }
3759
3760 fragment = 0;
3761 while (risc_size > 0 && rval == QLA_SUCCESS) {
3762 wlen = (uint16_t)(ha->fw_transfer_size >> 1);
3763 if (wlen > risc_size)
3764 wlen = risc_size;
3765
3766 DEBUG7(printk("scsi(%ld): Loading risc segment@ risc "
3767 "addr %x, number of words 0x%x.\n", ha->host_no,
3768 risc_addr, wlen));
3769
3770 for (i = 0; i < wlen; i++)
3771 wcode[i] = swab16(fwcode[i]);
3772
3773 rval = qla2x00_load_ram(ha, ha->request_dma, risc_addr,
3774 wlen);
3775 if (rval) {
3776 DEBUG(printk("scsi(%ld):[ERROR] Failed to load "
3777 "segment %d of firmware\n", ha->host_no,
3778 fragment));
3779 qla_printk(KERN_WARNING, ha,
3780 "[ERROR] Failed to load segment %d of "
3781 "firmware\n", fragment);
3782 break;
3783 }
3784
3785 fwcode += wlen;
3786 risc_addr += wlen;
3787 risc_size -= wlen;
3788 fragment++;
3789 }
3790
3791 /* Next segment. */
3792 seg++;
3793 }
3794 return rval;
3795
3796fail_fw_integrity:
3797 return QLA_FUNCTION_FAILED;
3798}
3799
3800int
3801qla24xx_load_risc(scsi_qla_host_t *ha, uint32_t *srisc_addr)
Andrew Vasquez0107109e2005-07-06 10:31:37 -07003802{
3803 int rval;
3804 int segments, fragment;
3805 uint32_t *dcode, dlen;
3806 uint32_t risc_addr;
3807 uint32_t risc_size;
3808 uint32_t i;
Andrew Vasquez54333832005-11-09 15:49:04 -08003809 struct fw_blob *blob;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07003810 uint32_t *fwcode, fwclen;
3811
Andrew Vasquez54333832005-11-09 15:49:04 -08003812 /* Load firmware blob. */
3813 blob = qla2x00_request_firmware(ha);
3814 if (!blob) {
3815 qla_printk(KERN_ERR, ha, "Firmware image unavailable.\n");
Andrew Vasquez0107109e2005-07-06 10:31:37 -07003816 return QLA_FUNCTION_FAILED;
3817 }
3818
3819 rval = QLA_SUCCESS;
3820
3821 segments = FA_RISC_CODE_SEGMENTS;
3822 dcode = (uint32_t *)ha->request_ring;
3823 *srisc_addr = 0;
Andrew Vasquez54333832005-11-09 15:49:04 -08003824 fwcode = (uint32_t *)blob->fw->data;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07003825 fwclen = 0;
3826
3827 /* Validate firmware image by checking version. */
Andrew Vasquez54333832005-11-09 15:49:04 -08003828 if (blob->fw->size < 8 * sizeof(uint32_t)) {
Andrew Vasquez0107109e2005-07-06 10:31:37 -07003829 qla_printk(KERN_WARNING, ha,
Andrew Vasquez54333832005-11-09 15:49:04 -08003830 "Unable to verify integrity of firmware image (%Zd)!\n",
3831 blob->fw->size);
Andrew Vasquez0107109e2005-07-06 10:31:37 -07003832 goto fail_fw_integrity;
3833 }
3834 for (i = 0; i < 4; i++)
3835 dcode[i] = be32_to_cpu(fwcode[i + 4]);
3836 if ((dcode[0] == 0xffffffff && dcode[1] == 0xffffffff &&
3837 dcode[2] == 0xffffffff && dcode[3] == 0xffffffff) ||
3838 (dcode[0] == 0 && dcode[1] == 0 && dcode[2] == 0 &&
3839 dcode[3] == 0)) {
3840 qla_printk(KERN_WARNING, ha,
Andrew Vasquez54333832005-11-09 15:49:04 -08003841 "Unable to verify integrity of firmware image!\n");
Andrew Vasquez0107109e2005-07-06 10:31:37 -07003842 qla_printk(KERN_WARNING, ha,
3843 "Firmware data: %08x %08x %08x %08x!\n", dcode[0],
3844 dcode[1], dcode[2], dcode[3]);
3845 goto fail_fw_integrity;
3846 }
3847
3848 while (segments && rval == QLA_SUCCESS) {
3849 risc_addr = be32_to_cpu(fwcode[2]);
3850 *srisc_addr = *srisc_addr == 0 ? risc_addr : *srisc_addr;
3851 risc_size = be32_to_cpu(fwcode[3]);
3852
3853 /* Validate firmware image size. */
3854 fwclen += risc_size * sizeof(uint32_t);
Andrew Vasquez54333832005-11-09 15:49:04 -08003855 if (blob->fw->size < fwclen) {
Andrew Vasquez0107109e2005-07-06 10:31:37 -07003856 qla_printk(KERN_WARNING, ha,
Andrew Vasquez54333832005-11-09 15:49:04 -08003857 "Unable to verify integrity of firmware image "
3858 "(%Zd)!\n", blob->fw->size);
3859
Andrew Vasquez0107109e2005-07-06 10:31:37 -07003860 goto fail_fw_integrity;
3861 }
3862
3863 fragment = 0;
3864 while (risc_size > 0 && rval == QLA_SUCCESS) {
3865 dlen = (uint32_t)(ha->fw_transfer_size >> 2);
3866 if (dlen > risc_size)
3867 dlen = risc_size;
3868
3869 DEBUG7(printk("scsi(%ld): Loading risc segment@ risc "
3870 "addr %x, number of dwords 0x%x.\n", ha->host_no,
3871 risc_addr, dlen));
3872
3873 for (i = 0; i < dlen; i++)
3874 dcode[i] = swab32(fwcode[i]);
3875
andrew.vasquez@qlogic.com590f98e2006-01-13 17:05:37 -08003876 rval = qla2x00_load_ram(ha, ha->request_dma, risc_addr,
3877 dlen);
Andrew Vasquez0107109e2005-07-06 10:31:37 -07003878 if (rval) {
3879 DEBUG(printk("scsi(%ld):[ERROR] Failed to load "
3880 "segment %d of firmware\n", ha->host_no,
3881 fragment));
3882 qla_printk(KERN_WARNING, ha,
3883 "[ERROR] Failed to load segment %d of "
3884 "firmware\n", fragment);
3885 break;
3886 }
3887
3888 fwcode += dlen;
3889 risc_addr += dlen;
3890 risc_size -= dlen;
3891 fragment++;
3892 }
3893
3894 /* Next segment. */
3895 segments--;
3896 }
Andrew Vasquez0107109e2005-07-06 10:31:37 -07003897 return rval;
3898
3899fail_fw_integrity:
Andrew Vasquez0107109e2005-07-06 10:31:37 -07003900 return QLA_FUNCTION_FAILED;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07003901}
Andrew Vasquez54333832005-11-09 15:49:04 -08003902#endif